cudaGetDeviceProperties
cudaError_t cudaGetDeviceProperties(cudaDeviceProp* prop, int device);
函数接受两个参数:
- prop:指向cudaDeviceProp结构体的指针,用于存储获取到的GPU设备的属性信息。
- device:要查询的GPU设备的索引,通常从0开始。
以下是一些常用的cudaDeviceProp结构体的成员:
- name:设备名称,以字符串形式存储。
- major:设备的主计算能力版本号的主版本号。
- minor:设备的主计算能力版本号的次版本号。
- totalGlobalMem:设备的全局内存总大小(字节)。
- sharedMemPerBlock:每个线程块可用的共享内存大小(字节)。
- maxThreadsPerBlock:每个线程块可容纳的最大线程数量。
- maxThreadsDim:一个包含三个整数值的数组,分别表示每个线程块的最大维度。
- maxGridSize:一个包含三个整数值的数组,分别表示每个网格的最大维度。
例子:
#include<cuda_runtime.h>
cudaDeviceProp prop;
cudaGetDeviceProperties(&prop, 0);
std::cout << "Device name: " << prop.name << std::endl;
std::cout << "Compute capability: " << prop.major << "." << prop.minor << std::endl;
cudaDeviceGetAttribute
cudaError_t cudaDeviceGetAttribute(int* value, cudaDeviceAttr attr, int device);
函数接受三个参数:
- value:指向整数的指针,用于
存储查询的属性值
。 - attr:
要查询
的设备属性,是一个枚举值,用于指定要获取的属性类型。 - device:要查询的GPU设备的索引,通常从0开始。
以下是一些常用的设备(cudaDeviceAttr):
- cudaDevAttrMaxThreadsPerBlock:每个线程块可容纳的最大线程数量。
- cudaDevAttrMaxSharedMemoryPerBlock:每个线程块可用的共享内存大小(字节)。
- cudaDevAttrMaxGridSize:每个网格的最大维度。
- cudaDevAttrComputeCapabilityMajor:设备的计算能力主版本号。
- cudaDevAttrComputeCapabilityMinor:设备的计算能力次版本号。
例子:
cudaDeviceGetAttribute(&fp16_support, cudaDevAttrComputeCapabilityMajor, 0);
if (fp16_support >= 7) {
std::cout << "GPU Device supports FP16 computation" << std::endl;
} else {
std::cout << "GPU Device does not support FP16 computation" << std::endl;
}
cudaMemGetInfo
cudaError_t cudaMemGetInfo(size_t* free, size_t*
接受两个参数 free 和 total,都是指向 size_t 类型的指针。调用该函数后,它会将当前设备上的可用内存
大小存储在 free 中,将总内存
大小存储在 total 中
size_t free_byte;
size_t total_byte;
cudaMemGetInfo(&free_byte, &total_byte);
double free_db = (double)free_byte;
double total_db = (double)total_byte;
double free_db_g = free_db / 1024.0 / 1024.0 / 1024.0;
double total_db_g = total_db / 1024.0 / 1024.0 / 1024.0;
std::cout << "Total global memory: " << total_db_g << "G" << std::endl;
std::cout << "Total available memory: " << free_db_g << "G" << std::endl;
要除以 1024.0 三次,是因为通常我们将内存大小以字节为单位表示。在这里,除以 1024.0 三次即可将字节转换为千兆字节(GB)