Skip to content

Instantly share code, notes, and snippets.

@sakamoto-poteko
Last active October 21, 2022 23:11
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sakamoto-poteko/44d6cd19552fa7721b99 to your computer and use it in GitHub Desktop.
Save sakamoto-poteko/44d6cd19552fa7721b99 to your computer and use it in GitHub Desktop.
NVML Get GPU Utilization
#include <cstdio>
#include <nvml.h>
#pragma comment(lib, "nvml")
int main(int argc, char* argv[])
{
nvmlReturn_t result;
unsigned int device_count;
result = nvmlInit();
if (result != NVML_SUCCESS)
return 1;
result = nvmlDeviceGetCount(&device_count);
if (result != NVML_SUCCESS)
return 2;
for (int i = 0; i < device_count; ++i) {
nvmlDevice_t device;
result = nvmlDeviceGetHandleByIndex(0, &device);
if (result != NVML_SUCCESS)
return 3;
char device_name[NVML_DEVICE_NAME_BUFFER_SIZE];
result = nvmlDeviceGetName(device, device_name, NVML_DEVICE_NAME_BUFFER_SIZE);
if (result != NVML_SUCCESS)
return 4;
std::printf("Device %d: %s\n", i, device_name);
nvmlUtilization_st device_utilization;
result = nvmlDeviceGetUtilizationRates(device, &device_utilization);
if (result != NVML_SUCCESS)
return 5;
std::printf("GPU Util: %u, Mem Util: %u\n", device_utilization.gpu, device_utilization.memory);
}
nvmlShutdown();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment