Skip to content

Instantly share code, notes, and snippets.

@serihiro
Last active October 12, 2018 06:46
Show Gist options
  • Save serihiro/0688aa25048be1e6f4f27e92159c3d9a to your computer and use it in GitHub Desktop.
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
#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();
}
@serihiro
Copy link
Author

build sample

$ gcc -std=c99 -Wall nvml_test.c -I /usr/local/include -I /usr/local/cuda-9.1/include -o nvml_test -lnvidia-ml -L /usr/lib64/nvidia

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment