1

For safe (or error-checked) CUDA calls (to functions like cudaMemcpy, cudaMalloc, cudaFree) we can define a wrapper, something like this:

#define cuSafe(ans) { gpuCuSafe((ans), __FILE__, __LINE__); }
inline void gpuCuSafe(cudaError_t code, const char *file, int line) {
    if (code != cudaSuccess) {
        fprintf(stderr,
                "GPU ERROR: '%s' in file %s (line %d)\n",
                cudaGetErrorString(code),  /* <----< */
                file,
                line);
        exit(EXIT_FAILURE);
    }
}

These CUDA function calls return values of type cudaError_t. We can call cudaGetErrorString(cudaError_t c) to get the error string that might provide information to the users and developers of the program (an error string explaining the error is better than an error code).

In the cuRAND API Documentation, they use a similar approach to wrap curand function calls. curand functions return values of type curandStatus_t.

I am looking for a function that return a string representing the returned curandStatus_t value (could be CURAND_STATUS_INITIALIZATION_FAILED, CURAND_STATUS_NOT_INITIALIZED, etc), however, I couldn't find any function like that in the docs.

Just to be clear, what I want to be able to do is create a curandSafe wrapper (similar to the cuSafe example above)

#define curandSafe(ans) { gpuCuRandSafe((ans), __FILE__, __LINE__);}
inline void gpuCuRandSafe(curandStatus_t status, const char *file, int line) {
    if (status != CURAND_STATUS_SUCCESS) {
        fprintf(stderr,
                "GPU curand ERROR in file %s (line %d)\n",
                /*curandGetStatusString(status)*/  // OR SOMETHING LIKE THAT
                file,
                line);
        exit(EXIT_FAILURE);
    }
}

I am thinking about manually implementing it with a switch-case, but would like to know if there is a built-in function for it, so it takes care of possible, new status codes.

Community
  • 1
  • 1
Vince Varga
  • 6,101
  • 6
  • 43
  • 60

1 Answers1

4

Oh, I think I found a similar question. In the NVIDIA Samples cuda_helper.c, you can see the function that handles enum values for curand.

#ifdef CURAND_H_
// cuRAND API errors
static const char *curandGetErrorString(curandStatus_t error)
{
    switch (error)
    {
        case CURAND_STATUS_SUCCESS:
            return "CURAND_STATUS_SUCCESS";

        case CURAND_STATUS_VERSION_MISMATCH:
            return "CURAND_STATUS_VERSION_MISMATCH";

        case CURAND_STATUS_NOT_INITIALIZED:
            return "CURAND_STATUS_NOT_INITIALIZED";

        case CURAND_STATUS_ALLOCATION_FAILED:
            return "CURAND_STATUS_ALLOCATION_FAILED";

        case CURAND_STATUS_TYPE_ERROR:
            return "CURAND_STATUS_TYPE_ERROR";

        case CURAND_STATUS_OUT_OF_RANGE:
            return "CURAND_STATUS_OUT_OF_RANGE";

        case CURAND_STATUS_LENGTH_NOT_MULTIPLE:
            return "CURAND_STATUS_LENGTH_NOT_MULTIPLE";

        case CURAND_STATUS_DOUBLE_PRECISION_REQUIRED:
            return "CURAND_STATUS_DOUBLE_PRECISION_REQUIRED";

        case CURAND_STATUS_LAUNCH_FAILURE:
            return "CURAND_STATUS_LAUNCH_FAILURE";

        case CURAND_STATUS_PREEXISTING_FAILURE:
            return "CURAND_STATUS_PREEXISTING_FAILURE";

        case CURAND_STATUS_INITIALIZATION_FAILED:
            return "CURAND_STATUS_INITIALIZATION_FAILED";

        case CURAND_STATUS_ARCH_MISMATCH:
            return "CURAND_STATUS_ARCH_MISMATCH";

        case CURAND_STATUS_INTERNAL_ERROR:
            return "CURAND_STATUS_INTERNAL_ERROR";
    }

    return "<unknown>";
}
#endif

Yet, it would be nice a built-in, thus version-independent solution for this problem.

Community
  • 1
  • 1
Vince Varga
  • 6,101
  • 6
  • 43
  • 60
  • The « similar question » asks about cuBLAS error status, while this post specifically asks for Curand. – Dimitri Lesnoff Nov 04 '22 at 14:19
  • Yes, and? I'm not sure I get your point. The tldr is that there was no built-in function, so you can create your own where you just convert the enum into a string by defining all the potential cases you know in a switch-case. Yes, the similar question was for cublas, but the same principle and solution (or workaround) applies for curand, too. – Vince Varga Nov 06 '22 at 14:52
  • Sorry I realized after that you answered your own post. I just wanted to say that the question was not already answered. – Dimitri Lesnoff Nov 06 '22 at 20:36