linux下调用cuda接口获取gpu显存使用率和gpu使用率
linux下调用cuda接口获取gpu显存使用率和gpu使用率
#include <iostream> #include<string> #include<unistd.h> #include "nvml.h" using namespace std; int main() { nvmlReturn_t result; unsigned int device_count, i; // First initialize NVML library result = nvmlInit(); result = nvmlDeviceGetCount(&device_count); if (NVML_SUCCESS != result) { std::cout << "Failed to query device count: " << nvmlErrorString(result); } std::cout << "Found" << device_count <<" device" << endl; std::cout << "Listing devices:"; while (true) { for (i = 0; i < device_count; i++) { nvmlDevice_t device; char name[NVML_DEVICE_NAME_BUFFER_SIZE]; nvmlPciInfo_t pci; result = nvmlDeviceGetHandleByIndex(i, &device); if (NVML_SUCCESS != result) { std::cout << "get device failed " << endl; } result = nvmlDeviceGetName(device, name, NVML_DEVICE_NAME_BUFFER_SIZE); if (NVML_SUCCESS != result) { std::cout << "GPU name: " << name << endl; } //使用率 nvmlUtilization_t utilization; result = nvmlDeviceGetUtilizationRates(device, &utilization); if (NVML_SUCCESS == result) { std::cout << " 卡"<< i <<"使用率 "; std::cout << "GPU 使用率: " << utilization.gpu << " 显存使用率 " << utilization.memory << endl; } } std::cout << "==========================="<<endl; sleep(10); } return 0; }
编译命令:
g++ test.cc -lnvidia-ml -L /lib64
执行
./a.out
结果
下一篇:
Wofav网页收藏&分享按钮的使用方法