Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Get CUDA Toolkit Version at Compile Time Without nvcc?

Tags:

c++

cuda

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

like image 434
user1777820 Avatar asked Jun 22 '16 14:06

user1777820


People also ask

How do I find the CUDA Compiler version?

nvcc --version (or /usr/local/cuda/bin/nvcc --version) gives the CUDA compiler version (which matches the toolkit version). From application code, you can query the runtime API version with

Does cudatoolkit come with nvcc?

cudatoolkit that is installed with pytorch is runtime only and does not come with the development compiler nvcc. To get nvcc you need to install cudatoolkit-dev which I believe is available from the conda-forge channel. Show activity on this post.

Why is my CUDA toolkit showing the wrong version?

However, if there is another version of the CUDA toolkit installed other than the one symlinked from /usr/local/cuda, this may report an inaccurate version if another version is earlier in your PATH than the above, so use with caution. Show activity on this post. You can also get some insights into which CUDA versions are installed with:

Why NVCC and nvidia-smi drivers are different versions of CUDA?

If there is a version mismatch between nvcc and nvidia-smi then different versions of cuda are used as driver and run time environemtn. To ensure same version of CUDA drivers are used what you need to do is to get CUDA on system path.


1 Answers

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.

like image 59
Robert Crovella Avatar answered Nov 02 '22 00:11

Robert Crovella