基于CUDA查询显卡型号和显存大小

安装驱动后,我们可以用 nvidia-smi 命令来获取显卡信息。

nvidia-smi 的一个比较好的博客 nvidia-smi详解

nvidia-smi

Tue Sep 27 11:08:37 2022       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 515.65.01    Driver Version: 515.65.01    CUDA Version: 11.7     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  NVIDIA GeForce ...  Off  | 00000000:3B:00.0 Off |                  N/A |
| 38%   53C    P2   104W / 320W |   3839MiB / 10240MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
|   1  NVIDIA GeForce ...  Off  | 00000000:86:00.0 Off |                  N/A |
| 30%   36C    P8    19W / 320W |     13MiB / 10240MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
|   2  NVIDIA GeForce ...  Off  | 00000000:AF:00.0 Off |                  N/A |
| 30%   34C    P8    13W / 320W |     13MiB / 10240MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|    0   N/A  N/A      1398      G   /usr/lib/xorg/Xorg                  4MiB |
|    0   N/A  N/A      2340      C   java                             3826MiB |
|    0   N/A  N/A      2680      G   /usr/lib/xorg/Xorg                  4MiB |
|    1   N/A  N/A      1398      G   /usr/lib/xorg/Xorg                  4MiB |
|    1   N/A  N/A      2680      G   /usr/lib/xorg/Xorg                  4MiB |
|    2   N/A  N/A      1398      G   /usr/lib/xorg/Xorg                  4MiB |
|    2   N/A  N/A      2680      G   /usr/lib/xorg/Xorg                  4MiB |
+-----------------------------------------------------------------------------+

列出可用设备

nvidia-smi -L

GPU 0: NVIDIA GeForce RTX 3080 (UUID: GPU-d6daa455-a57f-f91a-a780-19d2a2c3e059)
GPU 1: NVIDIA GeForce RTX 3080 (UUID: GPU-aff98a0d-53bd-a538-d35f-70e0714cb8d2)
GPU 2: NVIDIA GeForce RTX 3080 (UUID: GPU-b1f86dba-045e-01a8-6e08-d170c25663da)

 

 

基于cuda获取显卡信息:

#define checkCudaErrors(S) do {CUresult  status; \
        status = S; \
        if (status != CUDA_SUCCESS ) std::cout << __LINE__ <<" checkCudaErrors - status = " << status << std::endl; \
        } while (false)

int CheckCUDAProperty( int devId )
{
    cuInit(0);

    CUdevice dev = devId;
    size_t memSize = 0;
    char devName[256] = {0};
    int major = 0, minor = 0;
    CUresult rlt = CUDA_SUCCESS;

    rlt = cuDeviceComputeCapability( &major, &minor, dev );
    checkCudaErrors( rlt );

    rlt = cuDeviceGetName( devName, sizeof( devName ), dev );
    checkCudaErrors( rlt );

    printf( "Using GPU Device %d: %s has SM %d.%d compute capability\n",
            dev, devName, major, minor );

    rlt = cuDeviceTotalMem( &memSize, dev );
    checkCudaErrors( rlt );

    printf( "Total amount of global memory:   %4.4f MB\n",
           (float)memSize / ( 1024 * 1024 ) );

    return 0;
}

运行结果:

Using GPU Device 1: NVIDIA GeForce RTX 3080 has SM 8.6 compute capability
Total amount of global memory:   10018.0625 MB

 

 

 

 

 

 

 

 

 

 


 

posted @ 2022-09-27 20:46  yeren2046  阅读(990)  评论(0编辑  收藏  举报