gpu_info_rocm.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #ifndef __APPLE__
  2. #include "gpu_info_rocm.h"
  3. #include <string.h>
  4. #ifndef _WIN32
  5. const char *rocm_lib_paths[] = {
  6. "librocm_smi64.so",
  7. "/opt/rocm/lib/librocm_smi64.so",
  8. NULL,
  9. };
  10. #else
  11. // TODO untested
  12. const char *rocm_lib_paths[] = {
  13. "rocm_smi64.dll",
  14. "/opt/rocm/lib/rocm_smi64.dll",
  15. NULL,
  16. };
  17. #endif
  18. void rocm_init(rocm_init_resp_t *resp) {
  19. rsmi_status_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. {"rsmi_init", (void *)&resp->rh.initFn},
  29. {"rsmi_shut_down", (void *)&resp->rh.shutdownFn},
  30. {"rsmi_dev_memory_total_get", (void *)&resp->rh.totalMemFn},
  31. {"rsmi_dev_memory_usage_get", (void *)&resp->rh.usageMemFn},
  32. // { "rsmi_dev_id_get", (void*)&resp->rh.getHandle },
  33. };
  34. for (i = 0; rocm_lib_paths[i] != NULL && resp->rh.handle == NULL; i++) {
  35. resp->rh.handle = LOAD_LIBRARY(rocm_lib_paths[i], RTLD_LAZY);
  36. }
  37. if (!resp->rh.handle) {
  38. snprintf(buf, buflen,
  39. "Unable to load %s library to query for Radeon GPUs: %s\n",
  40. rocm_lib_paths[0], LOAD_ERR());
  41. resp->err = strdup(buf);
  42. return;
  43. }
  44. for (i = 0; i < 4; i++) {
  45. *l[i].p = LOAD_SYMBOL(resp->rh.handle, l[i].s);
  46. if (!l[i].p) {
  47. UNLOAD_LIBRARY(resp->rh.handle);
  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->rh.initFn)(0);
  55. if (ret != RSMI_STATUS_SUCCESS) {
  56. snprintf(buf, buflen, "rocm vram init failure: %d", ret);
  57. resp->err = strdup(buf);
  58. }
  59. return;
  60. }
  61. void rocm_check_vram(rocm_handle_t h, mem_info_t *resp) {
  62. resp->err = NULL;
  63. // uint32_t num_devices;
  64. // uint16_t device;
  65. uint64_t totalMem = 0;
  66. uint64_t usedMem = 0;
  67. rsmi_status_t ret;
  68. const int buflen = 256;
  69. char buf[buflen + 1];
  70. int i;
  71. if (h.handle == NULL) {
  72. resp->err = strdup("nvml handle sn't initialized");
  73. return;
  74. }
  75. // TODO - iterate through devices... ret =
  76. // rsmi_num_monitor_devices(&num_devices);
  77. // ret = (*h.getHandle)(0, &device);
  78. // if (ret != RSMI_STATUS_SUCCESS) {
  79. // printf("rocm vram device lookup failure: %d\n", ret);
  80. // return -1;
  81. // }
  82. // Get total memory - used memory for available memory
  83. ret = (*h.totalMemFn)(0, RSMI_MEM_TYPE_VRAM, &totalMem);
  84. if (ret != RSMI_STATUS_SUCCESS) {
  85. snprintf(buf, buflen, "rocm total mem lookup failure: %d", ret);
  86. resp->err = strdup(buf);
  87. return;
  88. }
  89. ret = (*h.usageMemFn)(0, RSMI_MEM_TYPE_VRAM, &usedMem);
  90. if (ret != RSMI_STATUS_SUCCESS) {
  91. snprintf(buf, buflen, "rocm usage mem lookup failure: %d", ret);
  92. resp->err = strdup(buf);
  93. return;
  94. }
  95. resp->total = totalMem;
  96. resp->free = totalMem - usedMem;
  97. return;
  98. }
  99. #endif // __APPLE__