gpu_info_cuda.c 2.8 KB

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