gpt4 book ai didi

cudaMemcpy 无法复制值

转载 作者:太空宇宙 更新时间:2023-11-04 02:19:12 26 4
gpt4 key购买 nike

我正在调用 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 被调用..?

最佳答案

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

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

关于cudaMemcpy 无法复制值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3464146/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com