简体   繁体   English

如何在没有 nvcc 的情况下在编译时获取 CUDA Toolkit 版本?

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

I have some calls in a .cpp file, to the cuSPARSE library, that are not available in older toolkits.我在 .cpp 文件中有一些对 cuSPARSE 库的调用,这些调用在旧工具包中不可用。 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.特别是,我想使用旧工具包的 CSR 格式矩阵和新工具包的 BSR 格式矩阵来解决稀疏三角系统。

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.问题是,在本节中,我没有使用 nvcc 编译任何实际的 CUDA 代码,只是进行库调用,因此无法使用 nvcc 宏来获取版本信息。

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. cuda.h 中有 __CUDA_API_VERSION 的#define,但在编译我的 .cpp 时它是 0,无论我是否包含 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++.我在 CentOS 7 中工作并使用 g++ 编译我的 .cpp。

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 )在您的 .cpp 文件中包含 cuda_runtime_api.h(例如,如果您正在使用任何 CUDA 运行时 API 函数,例如cudaMalloc ,则您可能已经这样做了)

  2. Use the CUDART_VERSION define that is included from that header file:使用包含在该头文件中的CUDART_VERSION定义:

     #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.或类似。

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.对于最新版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM