c++cuda 和 nvidia-smi 获取 GPU的UUID

创建日期: 2023-06-07 16:52 | 作者: 风波 | 浏览次数: 20 | 分类: CUDA

方法一:使用命令 nvidia-smi -L 获取 GPU UUID

$ nvidia-smi -L
GPU 0: NVIDIA GeForce RTX 3090 (UUID: GPU-54b57206-11f1-c847-74b9-24c57c5f8e19)
GPU 1: NVIDIA GeForce RTX 3090 (UUID: GPU-6436186f-827a-6a09-0b3e-b43d84f59610)
GPU 2: NVIDIA GeForce RTX 3090 (UUID: GPU-543d3c13-19cf-2389-f70b-87cf44cfb336)
GPU 3: NVIDIA GeForce RTX 3090 (UUID: GPU-2ce035fc-5468-06c3-9642-824f87a80ebd)
GPU 4: NVIDIA GeForce RTX 3090 (UUID: GPU-f12c3253-551f-b0fc-2e7b-13a7739c316b)
GPU 5: NVIDIA GeForce RTX 3090 (UUID: GPU-9df64ffc-2ab9-c939-b23a-e307f2341e91)
GPU 6: NVIDIA GeForce RTX 3090 (UUID: GPU-ea5dadf0-8482-bca9-9746-866fc4bb8022)
GPU 7: NVIDIA GeForce RTX 3090 (UUID: GPU-6f90b2d2-4dc0-93aa-b3fe-15adf7af1afc)

方法二:使用 cuda 的库通过 c++ 代码获取

参考:https://stackoverflow.com/questions/74788577/read-gpu-information-from-console-c

用到的类:cudaDeviceProp - https://docs.nvidia.com/cuda/cuda-runtime-api/structcudaDeviceProp.html 用到的接口:cudaGetDeviceProperties - https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__DEVICE.html#group__CUDART__DEVICE_1g1bf9d625a931d657e08db2b4391170f0

英伟达 cuda 接口列表:https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__DEVICE.html#group__CUDART__DEVICE_1g1bf9d625a931d657e08db2b4391170f0

C++ 代码

#include <iostream>
#include <cuda_runtime_api.h>
#include <sstream>


int main() {
    int gpu_count = 0;
    cudaGetDeviceCount(&gpu_count);
    std::cout << "device count: " << gpu_count << std::endl;

    for (auto i = 0; i < gpu_count; i++) {
        // Get the name of the attached GPU
        cudaDeviceProp device_properties;
        cudaGetDeviceProperties(&device_properties, i); 
        std::string gpu_name = device_properties.name;
        std::cout << "Attached GPU[" << i << "] name: " << gpu_name << " ";
        unsigned char low_mask = 0b00001111;
        unsigned char high_mask = 0b11110000;
        std::stringstream stream;
        for (auto j = 0; j < sizeof(device_properties.uuid); j++) {
            stream << std::hex << (size_t)((device_properties.uuid.bytes[j] & high_mask) >> 4)
                << std::hex << (size_t)(device_properties.uuid.bytes[j] & low_mask);
        }
        std::cout << "UUID: " << stream.str() << std::endl;
    }

    return 0;
}

CMakeLists.txt

PROJECT (test_cuda LANGUAGES CXX CUDA)

SET(CMAKE_VERBOSE_MAKEFILE ON)

if(CUDA_ENABLE)
    ENABLE_LANGUAGE(CUDA)
endif()

ADD_EXECUTABLE (main main.cc)
#TARGET_COMPILE_FEATURES(main PUBLIC cxx_std_11)

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

SET (HAND_CUDA_INCLUDE_DIR "/usr/local/cuda/targets/x86_64-linux/include")
MESSAGE (STATUS "HAND_CUDA_INCLUDE_DIR: " ${HAND_CUDA_INCLUDE_DIR})
INCLUDE_DIRECTORIES (main ${HAND_CUDA_INCLUDE_DIR})

SET (HAND_CUDA_LIBRARY "/usr/local/cuda/targets/x86_64-linux/lib/libcudart.so")
MESSAGE (STATUS "HAND_CUDA_LIBRARY: " ${HAND_CUDA_LIBRARY})
TARGET_LINK_LIBRARIES (main ${HAND_CUDA_LIBRARY})

编译

mkdir build && cmake .. && make

运行结果

./main
device count: 8
Attached GPU[0] name: NVIDIA GeForce RTX 3090 UUID: 54b5720611f1c84774b924c57c5f8e19
Attached GPU[1] name: NVIDIA GeForce RTX 3090 UUID: 6436186f827a6a090b3eb43d84f59610
Attached GPU[2] name: NVIDIA GeForce RTX 3090 UUID: 543d3c1319cf2389f70b87cf44cfb336
Attached GPU[3] name: NVIDIA GeForce RTX 3090 UUID: 2ce035fc546806c39642824f87a80ebd
Attached GPU[4] name: NVIDIA GeForce RTX 3090 UUID: f12c3253551fb0fc2e7b13a7739c316b
Attached GPU[5] name: NVIDIA GeForce RTX 3090 UUID: 9df64ffc2ab9c939b23ae307f2341e91
Attached GPU[6] name: NVIDIA GeForce RTX 3090 UUID: ea5dadf08482bca99746866fc4bb8022
Attached GPU[7] name: NVIDIA GeForce RTX 3090 UUID: 6f90b2d24dc093aab3fe15adf7af1afc
20 浏览
10 爬虫
0 评论