cuda_test.cu 1.6 KB
Newer Older
L
liaogang 已提交
1
#include <cuda_runtime.h>
L
liaogang 已提交
2
#include <stdio.h>
L
liaogang 已提交
3 4
#include "gtest/gtest.h"

L
liaogang 已提交
5 6 7 8 9 10 11 12
#define CHECK_ERR(x)                 \
  if (x != cudaSuccess) {            \
    fprintf(stderr,                  \
            "%s in %s at line %d\n", \
            cudaGetErrorString(err), \
            __FILE__,                \
            __LINE__);               \
    exit(-1);                        \
L
liaogang 已提交
13
  }
L
liaogang 已提交
14

L
liaogang 已提交
15
__global__ void vecAdd(float *d_A, float *d_B, float *d_C, int n) {
L
liaogang 已提交
16 17 18 19 20 21 22 23 24
  int i = blockDim.x * blockIdx.x + threadIdx.x;
  if (i < n) {
    d_C[i] = d_A[i] + d_B[i];
  }
}

TEST(Cuda, Equality) {
  int n = 10;
  // Memory allocation for h_A, h_B and h_C (in the host)
L
liaogang 已提交
25 26
  float h_A[10] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 0.0};
  float h_B[10] = {0.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0};
L
liaogang 已提交
27 28
  float h_C[10];
  float *d_A, *d_B, *d_C;
L
liaogang 已提交
29
  cudaError_t err;
L
liaogang 已提交
30
  // Memory allocation for d_A, d_B and d_C (in the device)
L
liaogang 已提交
31
  err = cudaMalloc((void **)&d_A, sizeof(float) * n);
L
liaogang 已提交
32 33
  CHECK_ERR(err);

L
liaogang 已提交
34
  err = cudaMalloc((void **)&d_B, sizeof(float) * n);
L
liaogang 已提交
35 36
  CHECK_ERR(err);

L
liaogang 已提交
37
  err = cudaMalloc((void **)&d_C, sizeof(float) * n);
L
liaogang 已提交
38
  CHECK_ERR(err);
L
liaogang 已提交
39

L
liaogang 已提交
40
  // Copying memory to device
L
liaogang 已提交
41
  err = cudaMemcpy(d_A, h_A, sizeof(float) * n, cudaMemcpyHostToDevice);
L
liaogang 已提交
42 43
  CHECK_ERR(err);

L
liaogang 已提交
44
  err = cudaMemcpy(d_B, h_B, sizeof(float) * n, cudaMemcpyHostToDevice);
L
liaogang 已提交
45 46 47
  CHECK_ERR(err);

  // Calling the kernel
L
liaogang 已提交
48
  vecAdd<<<ceil(n / 256.0), 256>>>(d_A, d_B, d_C, n);
L
liaogang 已提交
49 50

  // Copying results back to host
L
liaogang 已提交
51
  err = cudaMemcpy(h_C, d_C, sizeof(float) * n, cudaMemcpyDeviceToHost);
L
liaogang 已提交
52
  CHECK_ERR(err);
L
liaogang 已提交
53

L
liaogang 已提交
54 55
  EXPECT_EQ(h_C[0], 1.0);
  for (int i = 1; i < n - 1; ++i) {
L
liaogang 已提交
56 57
    EXPECT_EQ(h_C[i], 11.0);
  }
L
liaogang 已提交
58
  EXPECT_EQ(h_C[9], 1.0);
L
liaogang 已提交
59
}