C++ 读取英伟达显卡名称、架构及算力
- 通过CUDA Runtime API获取计算能力(推荐)
- CUDA计算能力(Compute Capability)的版本号直接对应显卡架构(如8.6=Ampere,9.0=Hopper)。
实现步骤:
1.安装依赖:
- 安装 NVIDIA CUDA Toolkit。
- 确保显卡驱动支持CUDA。
2. C++代码示例:
#include <cuda_runtime.h>
#include <iostream>
#include <string>
std::string get_architecture_name(int major, int minor) {int compute_ver = major * 10 + minor;switch (compute_ver) {case 86: return "Ada Lovelace (e.g. RTX 40系列)";case 89: return "Ada Lovelace (e.g. H100)";case 80: return "Ampere (e.g. A100/RTX 30系列)";case 75: return "Turing (e.g. RTX 20系列/T4)";case 70: return "Volta (e.g. V100)";case 62: return "Pascal (e.g. P100/GTX 10系列)";default: return "Unknown Architecture";}
}int main() {int device_count;cudaGetDeviceCount(&device_count);if (device_count == 0) {std::cerr << "未检测到NVIDIA显卡" << std::endl;return -1;}for (int i = 0; i < device_count; ++i) {cudaDeviceProp prop;cudaGetDeviceProperties(&prop, i);std::string arch = get_architecture_name(prop.major, prop.minor);std::cout << "GPU " << i << ": " << prop.name << std::endl;std::cout << "架构: " << arch << std::endl;std::cout << "计算能力: " << prop.major << "." << prop.minor << "\n\n";}return 0;
}
3. 编译与运行:
nvcc detect_architecture.cu -o detect_arch
./detect_arch
- 将上述代码复制到 VS 中编译运行即可,.cpp 就可运行
GPU 0: NVIDIA GeForce RTX 4090
架构: Ada Lovelace (e.g. RTX 40系列)
计算能力: 8.9
显卡算力表