4
votes

I have some calls in a .cpp file, to the cuSPARSE library, that are not available in older toolkits. In order to support systems with older toolkits, I want to compile different sections of code using compiler directives. In particular, I want to solve sparse triangular systems using matrices in CSR format for old toolkits and BSR format for new toolkits.

The problem is, I'm not compiling any actual CUDA code in this section with nvcc, just making library calls, so the nvcc macros to get version information are not available.

There is a #define of __CUDA_API_VERSION in cuda.h, but it is 0 when compiling my .cpp regardless of whether I include cuda.h.

How can I get information about the toolkit version at compile time in this case? I'm working in CentOS 7 and compiling my .cpp with g++.

2
One possible method: include cuda_runtime_api.h in your .cpp file, and use the CUDART_VERSION define that is included there. 8000 = CUDA 8.0. 7050 = CUDA 7.5, etc. You are probably including cuda_runtime_api.h anyway if you are for example using cudaMalloc/cudaMemcpy in your .cpp fileRobert Crovella
@RobertCrovella That worked! Thank you!user1777820

2 Answers

8
votes

One possible method:

  1. include cuda_runtime_api.h in your .cpp file (you may be doing this already if you are for example using any CUDA runtime API functions there, such as cudaMalloc)

  2. Use the CUDART_VERSION define that is included from that header file:

    #include <cuda_runtime_api.h>
    ...
    #ifndef CUDART_VERSION
    #error CUDART_VERSION Undefined!
    #elif (CUDART_VERSION == 8000)
    // your code for the CUDA 8.0 case
    #elif (CUDART_VERSION == 7050)
    // your code for the CUDA 7.5 case
    #elif ...
    //etc.
    #else
    #error Unknown CUDART_VERSION!
    #endif
    

    or similar.

1
votes

Another way:

#include <fmt/format.h>
#include <cuda.h>
...
fmt::print("CUDA v{}.{}\n", CUDA_VERSION/1000, CUDA_VERSION/10%100);

will print:

CUDA v11.1

for the newest version.