Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does CUDA really not have a calloc()-like API call?

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().

  • Is there really no API functionality for allocating a buffer initialized to all-zeros?
  • Is there something better I can do than call cudaMalloc() and then cudaMemset()?
like image 472
einpoklum Avatar asked Jan 20 '14 12:01

einpoklum


2 Answers

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.

like image 134
Robert Crovella Avatar answered Nov 08 '22 00:11

Robert Crovella


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.

like image 1
Jared Hoberock Avatar answered Nov 07 '22 22:11

Jared Hoberock