#include <nvml.h>
#include <iostream>

using std::cout;
using std::endl;

int main(){
  unsigned int device_id = 0, device_count;
  cout << "Using GPU " << device_id << "." << endl;
  nvmlDevice_t device;
  nvmlReturn_t result;
  result = nvmlInit();
  result = nvmlDeviceGetCount(&device_count);
  result = nvmlDeviceGetHandleByIndex(device_id, &device);
  if (NVML_SUCCESS != result) {
      cout << "Failed to get handle for device " << device_id << ": " << nvmlErrorString(result) << endl;
      return -1;
  }
  nvmlUtilization_t utilization;
  result = nvmlDeviceGetUtilizationRates(device, &utilization);
  cout << "GPU " << device_id << ", utilization: " << utilization.gpu << endl;
  cout << "GPU " << device_id << ", memory: " << utilization.memory << endl;
  return 0;
}
