如何在CUDA Fortran中查看GPU线程与线程块可用信息及工具使用问题
解决方法:查询GPU线程/线程块参数
方法1:编译官方deviceQuery样本
Nvidia HPC SDK里的deviceQuery不是预编译好的,需要自己编译CUDA示例程序:
- 找到示例路径:
<你的Nvhpc安装路径>/22.9/cuda/11.7/samples/1_Utilities/deviceQuery - 进入该目录后执行
make编译(确保已经source了HPC SDK的环境配置脚本,比如source <Nvhpc路径>/22.9/setenv.sh) - 编译完成后运行
./deviceQuery,输出里会包含你需要的每个线程块最大线程数、网格最大尺寸(对应线程块数量上限)、每个SM最大线程数等关键参数。
方法2:写极简CUDA程序直接查询
如果不想找样本编译,自己写几行代码更直接:
- 创建
test.cu文件,内容如下:
#include <stdio.h> #include <cuda_runtime.h> int main() { int deviceId; cudaGetDevice(&deviceId); // 查询每个线程块最大线程数 int maxThreadsPerBlock; cudaDeviceGetAttribute(&maxThreadsPerBlock, cudaDevAttrMaxThreadsPerBlock, deviceId); printf("每个线程块最大线程数: %d\n", maxThreadsPerBlock); // 查询线程块网格的最大尺寸(对应X/Y/Z三个维度的线程块数量上限) int maxGridX, maxGridY, maxGridZ; cudaDeviceGetAttribute(&maxGridX, cudaDevAttrMaxGridDimX, deviceId); cudaDeviceGetAttribute(&maxGridY, cudaDevAttrMaxGridDimY, deviceId); cudaDeviceGetAttribute(&maxGridZ, cudaDevAttrMaxGridDimZ, deviceId); printf("线程块网格最大尺寸(X/Y/Z): %d / %d / %d\n", maxGridX, maxGridY, maxGridZ); // 查询每个SM(流式多处理器)支持的最大线程数 int maxThreadsPerSM; cudaDeviceGetAttribute(&maxThreadsPerSM, cudaDevAttrMaxThreadsPerMultiProcessor, deviceId); printf("每个SM最大线程数: %d\n", maxThreadsPerSM); return 0; }
- 用nvcc编译:
nvcc test.cu -o test - 运行程序:
./test,直接得到你需要的核心参数。
关于__nvcc_device_query的说明
这个工具是nvcc编译器内部用来检测设备的辅助程序,不是给用户直接调用的,所以运行后无输出是正常现象,不用纠结。
内容的提问来源于stack exchange,提问作者implicit_some
相关产品推荐
相关产品推荐

