【问题标题】:How to get properties from active CUDA device?如何从活动 CUDA 设备获取属性?
【发布时间】:2013-01-25 20:07:34
【问题描述】:

从 CUDA 设备 (!) 获取属性的众所周知的代码是枚举所有设备,然后从那里获取属性。然后我看到了这样的片段,它激活了给定的设备。

我遇到了相反的问题——假设设备已经被选中,我想获取它(活动的)的属性,而不是系统中存在的所有设备。

我希望我以正确的方式写了这篇文章,因为我是 CUDA 的新手。

【问题讨论】:

    标签: cuda nvidia


    【解决方案1】:

    只需调用cudaGetDevice() 获取活动上下文的设备号,然后调用cudaGetDeviceProperties 获取该设备的属性。在类似这样的代码中:

    int device;
    cudaGetDevice(&device);
    
    struct cudaDeviceProp props;
    cudaGetDeviceProperties(&props, device);
    

    [免责声明:在浏览器中编写,从未编译或测试过。使用风险自负]

    【讨论】:

      【解决方案2】:

      我在这里有一个脚本,它可以识别所有活动设备,并且每个设备都会在屏幕上返回一些有用的信息。

      #include <stdio.h>
      
      int main() {
      
        int nDevices;
        cudaGetDeviceCount(&nDevices);
        
        printf("Number of devices: %d\n", nDevices);
        
        for (int i = 0; i < nDevices; i++) {
          cudaDeviceProp prop;
          cudaGetDeviceProperties(&prop, i);
          printf("Device Number: %d\n", i);
          printf("  Device name: %s\n", prop.name);
          printf("  Memory Clock Rate (MHz): %d\n",
                 prop.memoryClockRate/1024);
          printf("  Memory Bus Width (bits): %d\n",
                 prop.memoryBusWidth);
          printf("  Peak Memory Bandwidth (GB/s): %.1f\n",
                 2.0*prop.memoryClockRate*(prop.memoryBusWidth/8)/1.0e6);
          printf("  Total global memory (Gbytes) %.1f\n",(float)(prop.totalGlobalMem)/1024.0/1024.0/1024.0);
          printf("  Shared memory per block (Kbytes) %.1f\n",(float)(prop.sharedMemPerBlock)/1024.0);
          printf("  minor-major: %d-%d\n", prop.minor, prop.major);
          printf("  Warp-size: %d\n", prop.warpSize);
          printf("  Concurrent kernels: %s\n", prop.concurrentKernels ? "yes" : "no");
          printf("  Concurrent computation/communication: %s\n\n",prop.deviceOverlap ? "yes" : "no");
        }
      }
      

      注意:将代码 sn-p 放在 .cu 文件中,并使用 nvcc 编译。

      例如,在我的带有一个 GPU 的桌面上,屏幕上的消息是:

      Number of devices: 1
      Device Number: 0
        Device name: Quadro K5200
        Memory Clock Rate (MHz): 2933
        Memory Bus Width (bits): 256
        Peak Memory Bandwidth (GB/s): 192.3
        Total global memory (Gbytes) 7.4
        Shared memory per block (Kbytes) 48.0
        minor-major: 5-3
        Warp-size: 32
        Concurrent kernels: yes
        Concurrent computation/communication: yes
      

      【讨论】:

        猜你喜欢
        • 2020-08-14
        • 1970-01-01
        • 2015-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-08
        相关资源
        最近更新 更多