Last active
October 12, 2018 06:46
-
-
Save serihiro/0688aa25048be1e6f4f27e92159c3d9a to your computer and use it in GitHub Desktop.
nvml sample(written in c), nvml API reference is here: https://docs.nvidia.com/deploy/nvml-api/index.html
This file contains hidden or 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 <stdio.h> | |
#include <nvml.h> | |
int main(int argc, char **argv) | |
{ | |
nvmlReturn_t result; | |
result = nvmlInit(); | |
if (NVML_SUCCESS != result) | |
{ | |
printf("Failed to initialize NVML: %s\n", nvmlErrorString(result)); | |
printf("Press ENTER to continue...\n"); | |
getchar(); | |
return 1; | |
} | |
unsigned int device_count; | |
result = nvmlDeviceGetCount(&device_count); | |
if (NVML_SUCCESS != result) | |
{ | |
printf("Failed to query device count: %s\n", nvmlErrorString(result)); | |
return 1; | |
} | |
for (int i = 0; i < device_count; i++) | |
{ | |
nvmlDevice_t device; | |
result = nvmlDeviceGetHandleByIndex(i, &device); | |
if (NVML_SUCCESS != result) | |
{ | |
printf("Failed to get device handle: %s\n", nvmlErrorString(result)); | |
return 1; | |
} | |
nvmlUtilization_t util; | |
result = nvmlDeviceGetUtilizationRates(device, &util); | |
if (NVML_SUCCESS != result) | |
{ | |
printf("Failed to get utilization rates: %s\n", nvmlErrorString(result)); | |
return 1; | |
} | |
printf("gpu: %d\n", util.gpu); | |
printf("memory: %d\n", util.gpu); | |
nvmlMemory_t memory; | |
result = nvmlDeviceGetMemoryInfo(device, &memory); | |
if (NVML_SUCCESS != result) | |
{ | |
printf("Failed to get device memory info: %s\n", nvmlErrorString(result)); | |
return 1; | |
} | |
printf("free: %lld\n", memory.free); | |
printf("used: %lld\n", memory.used); | |
printf("total: %lld\n", memory.total); | |
} | |
nvmlShutdown(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
build sample