gpu_info_cuda.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #ifndef __APPLE__ // TODO - maybe consider nvidia support on intel macs?
  2. #include "gpu_info_cuda.h"
  3. #include <string.h>
  4. #ifndef _WIN32
  5. const char *cuda_lib_paths[] = {
  6. "libnvidia-ml.so",
  7. "/usr/local/cuda/lib64/libnvidia-ml.so",
  8. "/usr/lib/x86_64-linux-gnu/nvidia/current/libnvidia-ml.so",
  9. "/usr/lib/x86_64-linux-gnu/libnvidia-ml.so.1",
  10. "/usr/lib/wsl/lib/libnvidia-ml.so.1", // TODO Maybe glob?
  11. NULL,
  12. };
  13. #else
  14. const char *cuda_lib_paths[] = {
  15. "nvml.dll",
  16. "",
  17. NULL,
  18. };
  19. #endif
  20. #define CUDA_LOOKUP_SIZE 5
  21. void cuda_init(cuda_init_resp_t *resp) {
  22. nvmlReturn_t ret;
  23. resp->err = NULL;
  24. const int buflen = 256;
  25. char buf[buflen + 1];
  26. int i;
  27. struct lookup {
  28. char *s;
  29. void **p;
  30. } l[CUDA_LOOKUP_SIZE] = {
  31. {"nvmlInit_v2", (void *)&resp->ch.initFn},
  32. {"nvmlShutdown", (void *)&resp->ch.shutdownFn},
  33. {"nvmlDeviceGetHandleByIndex", (void *)&resp->ch.getHandle},
  34. {"nvmlDeviceGetMemoryInfo", (void *)&resp->ch.getMemInfo},
  35. {"nvmlDeviceGetCount_v2", (void *)&resp->ch.getCount},
  36. };
  37. for (i = 0; cuda_lib_paths[i] != NULL && resp->ch.handle == NULL; i++) {
  38. resp->ch.handle = LOAD_LIBRARY(cuda_lib_paths[i], RTLD_LAZY);
  39. }
  40. if (!resp->ch.handle) {
  41. // TODO improve error message, as the LOAD_ERR will have typically have the
  42. // final path that was checked which might be confusing.
  43. char *msg = LOAD_ERR();
  44. snprintf(buf, buflen,
  45. "Unable to load %s library to query for Nvidia GPUs: %s",
  46. cuda_lib_paths[0], msg);
  47. free(msg);
  48. resp->err = strdup(buf);
  49. return;
  50. }
  51. for (i = 0; i < CUDA_LOOKUP_SIZE; i++) { // TODO - fix this to use a null terminated list
  52. *l[i].p = LOAD_SYMBOL(resp->ch.handle, l[i].s);
  53. if (!l[i].p) {
  54. UNLOAD_LIBRARY(resp->ch.handle);
  55. resp->ch.handle = NULL;
  56. char *msg = LOAD_ERR();
  57. snprintf(buf, buflen, "symbol lookup for %s failed: %s", l[i].s,
  58. msg);
  59. free(msg);
  60. resp->err = strdup(buf);
  61. return;
  62. }
  63. }
  64. ret = (*resp->ch.initFn)();
  65. if (ret != NVML_SUCCESS) {
  66. snprintf(buf, buflen, "nvml vram init failure: %d", ret);
  67. resp->err = strdup(buf);
  68. }
  69. return;
  70. }
  71. void cuda_check_vram(cuda_handle_t h, mem_info_t *resp) {
  72. resp->err = NULL;
  73. nvmlDevice_t device;
  74. nvmlMemory_t memInfo = {0};
  75. nvmlReturn_t ret;
  76. const int buflen = 256;
  77. char buf[buflen + 1];
  78. int i;
  79. if (h.handle == NULL) {
  80. resp->err = strdup("nvml handle sn't initialized");
  81. return;
  82. }
  83. unsigned int devices;
  84. ret = (*h.getCount)(&devices);
  85. if (ret != NVML_SUCCESS) {
  86. snprintf(buf, buflen, "unable to get device count: %d", ret);
  87. resp->err = strdup(buf);
  88. return;
  89. }
  90. resp->total = 0;
  91. resp->free = 0;
  92. for (i = 0; i < devices; i++) {
  93. ret = (*h.getHandle)(i, &device);
  94. if (ret != NVML_SUCCESS) {
  95. snprintf(buf, buflen, "unable to get device handle %d: %d", i, ret);
  96. resp->err = strdup(buf);
  97. return;
  98. }
  99. ret = (*h.getMemInfo)(device, &memInfo);
  100. if (ret != NVML_SUCCESS) {
  101. snprintf(buf, buflen, "device memory info lookup failure %d: %d", i, ret);
  102. resp->err = strdup(buf);
  103. return;
  104. }
  105. resp->total += memInfo.total;
  106. resp->free += memInfo.free;
  107. }
  108. }
  109. #endif // __APPLE__