gpu_info_cuda.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. snprintf(buf, buflen,
  41. "Unable to load %s library to query for Nvidia GPUs: %s",
  42. cuda_lib_paths[0], LOAD_ERR());
  43. resp->err = strdup(buf);
  44. return;
  45. }
  46. for (i = 0; i < 4; i++) { // TODO - fix this to use a null terminated list
  47. *l[i].p = LOAD_SYMBOL(resp->ch.handle, l[i].s);
  48. if (!l[i].p) {
  49. UNLOAD_LIBRARY(resp->ch.handle);
  50. resp->ch.handle = NULL;
  51. snprintf(buf, buflen, "symbol lookup for %s failed: %s", l[i].s,
  52. LOAD_ERR());
  53. resp->err = strdup(buf);
  54. return;
  55. }
  56. }
  57. ret = (*resp->ch.initFn)();
  58. if (ret != NVML_SUCCESS) {
  59. snprintf(buf, buflen, "nvml vram init failure: %d", ret);
  60. resp->err = strdup(buf);
  61. }
  62. return;
  63. }
  64. void cuda_check_vram(cuda_handle_t h, mem_info_t *resp) {
  65. resp->err = NULL;
  66. nvmlDevice_t device;
  67. nvmlMemory_t memInfo = {0};
  68. nvmlReturn_t ret;
  69. const int buflen = 256;
  70. char buf[buflen + 1];
  71. int i;
  72. if (h.handle == NULL) {
  73. resp->err = strdup("nvml handle sn't initialized");
  74. return;
  75. }
  76. // TODO - handle multiple GPUs
  77. ret = (*h.getHandle)(0, &device);
  78. if (ret != NVML_SUCCESS) {
  79. snprintf(buf, buflen, "unable to get device handle: %d", ret);
  80. resp->err = strdup(buf);
  81. return;
  82. }
  83. ret = (*h.getMemInfo)(device, &memInfo);
  84. if (ret != NVML_SUCCESS) {
  85. snprintf(buf, buflen, "device memory info lookup failure: %d", ret);
  86. resp->err = strdup(buf);
  87. return;
  88. }
  89. resp->total = memInfo.total;
  90. resp->free = memInfo.free;
  91. return;
  92. }
  93. #endif // __APPLE__