Reputation: 337
I have a quite simple question but I don't get it. I'm working with the 4.2 cuda.
I have the following code:
in my cudaClass.h:
unsigned char *dev_trimapExpanded;
in my 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);
}
in my file kernel.cu i have :
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;
}
The output is:
dev_trimapExpanded 0 before function
dev_trimapExpanded 0 before cudaMalloc
dev_trimapExpanded 93323264 after cudaMalloc
dev_trimapExpanded 0 after function
The address of my pointer is 0 before the cudaMalloc call which is normal but I don't understand why the address is 0 after the function. The address of the pointer dev_trimapExpanded shouldn't change. Is the scope of the cudaMalloc only in the function ? The memory is not free neither. How can I keep trace of my global memory between different kernel call ?
Upvotes: 1
Views: 1948
Reputation: 848
The behaviour is absolutely correct, your problem is, that you are passing the pointer "by value" - not "by reference" as you are calling the function. So what happens right now is, as the function is called youre pointer is copied, the value of the copy is changed inside the function, but as the function is finished the old pointer is not affected at all (since all the changes happend only to the copy).
Try this for you function header instead:
cudaError_t expansionTrimapCuda(unsigned char *& dev_trimapExpanded, ... )
See also: Pointer to Pointer / Reference to Pointer
Upvotes: 3