我刚开始学习CUDA。有没有办法通过编程获取显卡规格?
您可以使用cudaGetDeviceCount和cudaGetDeviceProperties API。
void DisplayHeader()
{
const int kb = 1024;
const int mb = kb * kb;
wcout << "NBody.GPU" << endl << "=========" << endl << endl;
wcout << "CUDA version: v" << CUDART_VERSION << endl;
wcout << "Thrust version: v" << THRUST_MAJOR_VERSION << "." << THRUST_MINOR_VERSION << endl << endl;
int devCount;
cudaGetDeviceCount(&devCount);
wcout << "CUDA Devices: " << endl << endl;
for(int i = 0; i < devCount; ++i)
{
cudaDeviceProp props;
cudaGetDeviceProperties(&props, i);
wcout << i << ": " << props.name << ": " << props.major << "." << props.minor << endl;
wcout << " Global memory: " << props.totalGlobalMem / mb << "mb" << endl;
wcout << " Shared memory: " << props.sharedMemPerBlock / kb << "kb" << endl;
wcout << " Constant memory: " << props.totalConstMem / kb << "kb" << endl;
wcout << " Block registers: " << props.regsPerBlock << endl << endl;
wcout << " Warp size: " << props.warpSize << endl;
wcout << " Threads per block: " << props.maxThreadsPerBlock << endl;
wcout << " Max block dimensions: [ " << props.maxThreadsDim[0] << ", " << props.maxThreadsDim[1] << ", " << props.maxThreadsDim[2] << " ]" << endl;
wcout << " Max grid dimensions: [ " << props.maxGridSize[0] << ", " << props.maxGridSize[1] << ", " << props.maxGridSize[2] << " ]" << endl;
wcout << endl;
}
}
%NVSDKCOMPUTE_ROOT%\C\src
目录中的deviceQuery
项目。它展示了如何使用CUDA Runtime API调用查询所有设备属性。
CUDA编程指南在3.2.3节中有更详细的说明。
deviceQuery
示例不再与CUDA Toolkit捆绑在一起,您可以在GitHub上找到它。