Last active
October 21, 2022 23:11
-
-
Save sakamoto-poteko/44d6cd19552fa7721b99 to your computer and use it in GitHub Desktop.
NVML Get GPU Utilization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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