gpu_info_cuda.c 2.4 KB

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