"When I try to use printf on pointers created using cudaMalloc, the program crashes"
If you have this:
int *d_data, *h_data;
cudaMalloc(&d_data, DSIZE);
You cannot do this:
printf(" %d ", *d_data);
as this requires dereferencing a device pointer (d_data
) in host code which is normally illegal in CUDA.
Instead you can do this:
h_data = (int *)malloc(DSIZE);
cudaMemcpy(h_data, d_data, DSIZE, cudaMemcpyDeviceToHost);
printf(" %d ", *h_data);
You can also investigate Unified Memory which is new in CUDA 6, and see if it will serve your purposes.
And, as mentioned in the comments, devices of cc2.0 or greater support printf
from the kernel, which operates on device data (only).