我有一个非常简单的问题,但我不明白。我正在使用 4.2 cuda。
我有以下代码:
在我的 cudaClass.h 中:
unsigned char *dev_trimapExpanded;
在我的 cudaClass.cpp 中:
void cudaClass::expansionTrimap() {
printf("dev_trimapExpanded %d before function \n", dev_trimapExpanded);
//Call cuda function
cudaError_t cudaStatus = expansionTrimapCuda(dev_trimapExpanded, width, height);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "expansionTrimapCuda failed!\n");
}
printf("dev_trimapExpanded %d after function \n", dev_trimapExpanded);
}
在我的文件 kernel.cu 我有:
cudaError_t expansionTrimapCuda(unsigned char *dev_trimapExpanded, size_t width, size_t height)
{
size_t size = width*height;
cudaError_t cudaStatus;
// Choose which GPU to run on, change this on a multi-GPU system.
cudaStatus = cudaSetDevice(0);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaSetDevice failed! Do you have a CUDA-capable GPU installed?");
return cudaStatus;
}
printf("dev_trimapExpanded %d before cudaMalloc \n", dev_trimapExpanded);
cudaStatus = cudaMalloc((void**)&dev_trimapExpanded, size);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMalloc dev_trimapExpanded failed!");
return cudaStatus;
}
printf("dev_trimapExpanded %d after cudaMalloc \n", dev_trimapExpanded);
return cudaStatus;
}
输出是:
dev_trimapExpanded 0 before function
dev_trimapExpanded 0 before cudaMalloc
dev_trimapExpanded 93323264 after cudaMalloc
dev_trimapExpanded 0 after function
在 cudaMalloc 调用之前,我的指针的地址是 0,这是正常的,但我不明白为什么函数之后的地址是 0。指针 dev_trimapExpanded 的地址不应更改。cudaMalloc 的范围仅在函数中吗?内存也不是免费的。如何在不同的内核调用之间跟踪我的全局内存?