gpu_info_cuda.c 2.5 KB

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