From looking at the CUDA 5.5 API Reference and the CUDA C Programming Guide it seems that there is no cudaCalloc()
, an on-GPU equivalent of the standard C library's calloc()
.
cudaMalloc()
and then cudaMemset()
?Is there really no API functionality for allocating a buffer initialized to all-zeros?
There really is not.
Is there something better I can do that cudaMalloc() followed by cudaMemset()?
You could use a macro, if it's a matter of convenience (you haven't told us what you mean by better, if the answer to the first question is no):
#define cudaCalloc(A, B, C) \
do { \
cudaError_t __cudaCalloc_err = cudaMalloc(A, B*C); \
if (__cudaCalloc_err == cudaSuccess) cudaMemset(*A, 0, B*C); \
} while (0)
The above macro will work with the kind of error checking I usually do (which is based on using cudaGetLastError()
; or you can build your preferred error checking directly into the macro, if you like. See this question about error handling.
If all you want is a simple way to zero out new allocations, you can use thrust::device_vector
, which default constructs its elements. For primitive types, this is the same behavior as calloc
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With