c++ccudacallocmemset

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


Solution

  • 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.