cudaMemcpy无法复制值

时间:2010-08-12 01:37:43

标签: c cuda gpu gpgpu

我正在调用cudaMemcpy并且副本成功返回,但源值未被复制到目标。我使用memcpy()写了一个类似的作品,并且工作正常。我在这里缺少什么?

// host externs
extern unsigned char landmask[DIMX * DIMY];

// use device constant memory for landmask
unsigned char *tempmask;
__device__ unsigned char *landmask_d;

...

void checkCUDAError(const char* msg) {
cudaError_t err = cudaGetLastError();
  if (cudaSuccess != err) {
    fprintf(stderr, "Cuda error: %s: %s.\n", msg, cudaGetErrorString(err));
    exit(EXIT_FAILURE);
  }
}

..

// try a memcpy
size_t landMemSize_t landMemSize = DIMX * DIMY * sizeof(char);
tempmask = (unsigned char*)malloc(landMemSize);
memcpy(tempmask, landmask, DIMX * DIMY);
if (landmask[0] != *tempmask) printf("FAILURE!\n");

// allocate device memory for landmask
cudaMalloc((void **)&landmask_d, landMemSize);
printf("allocating %ld Kbytes for landmask\n", landMemSize/1024);
checkCUDAError("memory allocation");

// copy landmask from host to device
cudaMemcpy(landmask_d, landmask, landMemSize, cudaMemcpyHostToDevice);
checkCUDAError("memory copy");
if (landmask[0] != *landmask_d) printf("FAILURE!\n");

没有一个CUDAErrors报告任何问题,但第二个FAILURE被调用..?

1 个答案:

答案 0 :(得分:3)

 if (landmask[0] != *landmask_d) printf("FAILURE!\n");

您正在比较不同内存域中的值。 您应该首先将内存从设备复制到cpu然后进行比较