2023-11-29 19:00:37 +00:00
|
|
|
#ifndef __APPLE__ // TODO - maybe consider nvidia support on intel macs?
|
|
|
|
|
|
|
|
#include "gpu_info_cuda.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2024-01-10 22:39:51 +00:00
|
|
|
void cuda_init(char *cuda_lib_path, cuda_init_resp_t *resp) {
|
2023-12-14 01:26:47 +00:00
|
|
|
nvmlReturn_t ret;
|
2023-11-29 19:00:37 +00:00
|
|
|
resp->err = NULL;
|
|
|
|
const int buflen = 256;
|
|
|
|
char buf[buflen + 1];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
struct lookup {
|
|
|
|
char *s;
|
|
|
|
void **p;
|
2024-01-24 18:32:00 +00:00
|
|
|
} l[] = {
|
|
|
|
{"nvmlInit_v2", (void *)&resp->ch.nvmlInit_v2},
|
|
|
|
{"nvmlShutdown", (void *)&resp->ch.nvmlShutdown},
|
|
|
|
{"nvmlDeviceGetHandleByIndex", (void *)&resp->ch.nvmlDeviceGetHandleByIndex},
|
|
|
|
{"nvmlDeviceGetMemoryInfo", (void *)&resp->ch.nvmlDeviceGetMemoryInfo},
|
|
|
|
{"nvmlDeviceGetCount_v2", (void *)&resp->ch.nvmlDeviceGetCount_v2},
|
|
|
|
{"nvmlDeviceGetCudaComputeCapability", (void *)&resp->ch.nvmlDeviceGetCudaComputeCapability},
|
2024-01-23 00:03:32 +00:00
|
|
|
{"nvmlSystemGetDriverVersion", (void *)&resp->ch.nvmlSystemGetDriverVersion},
|
|
|
|
{"nvmlDeviceGetName", (void *)&resp->ch.nvmlDeviceGetName},
|
|
|
|
{"nvmlDeviceGetSerial", (void *)&resp->ch.nvmlDeviceGetSerial},
|
|
|
|
{"nvmlDeviceGetVbiosVersion", (void *)&resp->ch.nvmlDeviceGetVbiosVersion},
|
|
|
|
{"nvmlDeviceGetBoardPartNumber", (void *)&resp->ch.nvmlDeviceGetBoardPartNumber},
|
|
|
|
{"nvmlDeviceGetBrand", (void *)&resp->ch.nvmlDeviceGetBrand},
|
2024-01-24 18:32:00 +00:00
|
|
|
{NULL, NULL},
|
2023-11-29 19:00:37 +00:00
|
|
|
};
|
|
|
|
|
2024-01-10 22:39:51 +00:00
|
|
|
resp->ch.handle = LOAD_LIBRARY(cuda_lib_path, RTLD_LAZY);
|
2023-11-29 19:00:37 +00:00
|
|
|
if (!resp->ch.handle) {
|
2023-12-22 23:43:31 +00:00
|
|
|
char *msg = LOAD_ERR();
|
2024-01-24 18:32:00 +00:00
|
|
|
LOG(resp->ch.verbose, "library %s load err: %s\n", cuda_lib_path, msg);
|
2023-11-29 19:00:37 +00:00
|
|
|
snprintf(buf, buflen,
|
|
|
|
"Unable to load %s library to query for Nvidia GPUs: %s",
|
2024-01-10 22:39:51 +00:00
|
|
|
cuda_lib_path, msg);
|
2023-12-22 23:43:31 +00:00
|
|
|
free(msg);
|
2023-11-29 19:00:37 +00:00
|
|
|
resp->err = strdup(buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-24 18:32:00 +00:00
|
|
|
// TODO once we've squashed the remaining corner cases remove this log
|
|
|
|
LOG(resp->ch.verbose, "wiring nvidia management library functions in %s\n", cuda_lib_path);
|
|
|
|
|
|
|
|
for (i = 0; l[i].s != NULL; i++) {
|
|
|
|
// TODO once we've squashed the remaining corner cases remove this log
|
|
|
|
LOG(resp->ch.verbose, "dlsym: %s\n", l[i].s);
|
|
|
|
|
2023-11-29 19:00:37 +00:00
|
|
|
*l[i].p = LOAD_SYMBOL(resp->ch.handle, l[i].s);
|
|
|
|
if (!l[i].p) {
|
|
|
|
resp->ch.handle = NULL;
|
2023-12-22 23:43:31 +00:00
|
|
|
char *msg = LOAD_ERR();
|
2024-01-24 18:32:00 +00:00
|
|
|
LOG(resp->ch.verbose, "dlerr: %s\n", msg);
|
|
|
|
UNLOAD_LIBRARY(resp->ch.handle);
|
2023-11-29 19:00:37 +00:00
|
|
|
snprintf(buf, buflen, "symbol lookup for %s failed: %s", l[i].s,
|
2023-12-22 23:43:31 +00:00
|
|
|
msg);
|
|
|
|
free(msg);
|
2023-11-29 19:00:37 +00:00
|
|
|
resp->err = strdup(buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2023-12-14 01:26:47 +00:00
|
|
|
|
2024-01-24 18:32:00 +00:00
|
|
|
ret = (*resp->ch.nvmlInit_v2)();
|
2023-12-14 01:26:47 +00:00
|
|
|
if (ret != NVML_SUCCESS) {
|
2024-01-24 18:32:00 +00:00
|
|
|
LOG(resp->ch.verbose, "nvmlInit_v2 err: %d\n", ret);
|
2024-01-10 22:39:51 +00:00
|
|
|
UNLOAD_LIBRARY(resp->ch.handle);
|
|
|
|
resp->ch.handle = NULL;
|
2023-12-14 01:26:47 +00:00
|
|
|
snprintf(buf, buflen, "nvml vram init failure: %d", ret);
|
|
|
|
resp->err = strdup(buf);
|
2024-01-26 17:18:33 +00:00
|
|
|
return;
|
2023-12-14 01:26:47 +00:00
|
|
|
}
|
|
|
|
|
2024-01-23 00:03:32 +00:00
|
|
|
// Report driver version if we're in verbose mode, ignore errors
|
|
|
|
ret = (*resp->ch.nvmlSystemGetDriverVersion)(buf, buflen);
|
|
|
|
if (ret != NVML_SUCCESS) {
|
|
|
|
LOG(resp->ch.verbose, "nvmlSystemGetDriverVersion failed: %d\n", ret);
|
|
|
|
} else {
|
|
|
|
LOG(resp->ch.verbose, "CUDA driver version: %s\n", buf);
|
|
|
|
}
|
2023-11-29 19:00:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cuda_check_vram(cuda_handle_t h, mem_info_t *resp) {
|
|
|
|
resp->err = NULL;
|
|
|
|
nvmlDevice_t device;
|
|
|
|
nvmlMemory_t memInfo = {0};
|
|
|
|
nvmlReturn_t ret;
|
|
|
|
const int buflen = 256;
|
|
|
|
char buf[buflen + 1];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (h.handle == NULL) {
|
|
|
|
resp->err = strdup("nvml handle sn't initialized");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-24 18:32:00 +00:00
|
|
|
ret = (*h.nvmlDeviceGetCount_v2)(&resp->count);
|
2023-11-29 19:00:37 +00:00
|
|
|
if (ret != NVML_SUCCESS) {
|
2024-01-05 16:25:58 +00:00
|
|
|
snprintf(buf, buflen, "unable to get device count: %d", ret);
|
2023-11-29 19:00:37 +00:00
|
|
|
resp->err = strdup(buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-05 16:25:58 +00:00
|
|
|
resp->total = 0;
|
|
|
|
resp->free = 0;
|
2024-01-09 20:53:33 +00:00
|
|
|
for (i = 0; i < resp->count; i++) {
|
2024-01-24 18:32:00 +00:00
|
|
|
ret = (*h.nvmlDeviceGetHandleByIndex)(i, &device);
|
2024-01-05 16:25:58 +00:00
|
|
|
if (ret != NVML_SUCCESS) {
|
|
|
|
snprintf(buf, buflen, "unable to get device handle %d: %d", i, ret);
|
|
|
|
resp->err = strdup(buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-24 18:32:00 +00:00
|
|
|
ret = (*h.nvmlDeviceGetMemoryInfo)(device, &memInfo);
|
2024-01-05 16:25:58 +00:00
|
|
|
if (ret != NVML_SUCCESS) {
|
|
|
|
snprintf(buf, buflen, "device memory info lookup failure %d: %d", i, ret);
|
|
|
|
resp->err = strdup(buf);
|
|
|
|
return;
|
|
|
|
}
|
2024-01-23 00:03:32 +00:00
|
|
|
if (h.verbose) {
|
|
|
|
nvmlBrandType_t brand = 0;
|
|
|
|
// When in verbose mode, report more information about
|
|
|
|
// the card we discover, but don't fail on error
|
|
|
|
ret = (*h.nvmlDeviceGetName)(device, buf, buflen);
|
|
|
|
if (ret != RSMI_STATUS_SUCCESS) {
|
|
|
|
LOG(h.verbose, "nvmlDeviceGetName failed: %d\n", ret);
|
|
|
|
} else {
|
|
|
|
LOG(h.verbose, "[%d] CUDA device name: %s\n", i, buf);
|
|
|
|
}
|
|
|
|
ret = (*h.nvmlDeviceGetBoardPartNumber)(device, buf, buflen);
|
|
|
|
if (ret != RSMI_STATUS_SUCCESS) {
|
|
|
|
LOG(h.verbose, "nvmlDeviceGetBoardPartNumber failed: %d\n", ret);
|
|
|
|
} else {
|
|
|
|
LOG(h.verbose, "[%d] CUDA part number: %s\n", i, buf);
|
|
|
|
}
|
|
|
|
ret = (*h.nvmlDeviceGetSerial)(device, buf, buflen);
|
|
|
|
if (ret != RSMI_STATUS_SUCCESS) {
|
|
|
|
LOG(h.verbose, "nvmlDeviceGetSerial failed: %d\n", ret);
|
|
|
|
} else {
|
|
|
|
LOG(h.verbose, "[%d] CUDA S/N: %s\n", i, buf);
|
|
|
|
}
|
|
|
|
ret = (*h.nvmlDeviceGetVbiosVersion)(device, buf, buflen);
|
|
|
|
if (ret != RSMI_STATUS_SUCCESS) {
|
|
|
|
LOG(h.verbose, "nvmlDeviceGetVbiosVersion failed: %d\n", ret);
|
|
|
|
} else {
|
|
|
|
LOG(h.verbose, "[%d] CUDA vbios version: %s\n", i, buf);
|
|
|
|
}
|
|
|
|
ret = (*h.nvmlDeviceGetBrand)(device, &brand);
|
|
|
|
if (ret != RSMI_STATUS_SUCCESS) {
|
|
|
|
LOG(h.verbose, "nvmlDeviceGetBrand failed: %d\n", ret);
|
|
|
|
} else {
|
|
|
|
LOG(h.verbose, "[%d] CUDA brand: %d\n", i, brand);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(h.verbose, "[%d] CUDA totalMem %ld\n", i, memInfo.total);
|
|
|
|
LOG(h.verbose, "[%d] CUDA usedMem %ld\n", i, memInfo.free);
|
2024-01-05 16:25:58 +00:00
|
|
|
|
|
|
|
resp->total += memInfo.total;
|
|
|
|
resp->free += memInfo.free;
|
2023-11-29 19:00:37 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-07 05:40:04 +00:00
|
|
|
|
|
|
|
void cuda_compute_capability(cuda_handle_t h, cuda_compute_capability_t *resp) {
|
|
|
|
resp->err = NULL;
|
|
|
|
resp->major = 0;
|
|
|
|
resp->minor = 0;
|
|
|
|
nvmlDevice_t device;
|
|
|
|
int major = 0;
|
|
|
|
int minor = 0;
|
|
|
|
nvmlReturn_t ret;
|
|
|
|
const int buflen = 256;
|
|
|
|
char buf[buflen + 1];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (h.handle == NULL) {
|
|
|
|
resp->err = strdup("nvml handle not initialized");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int devices;
|
2024-01-24 18:32:00 +00:00
|
|
|
ret = (*h.nvmlDeviceGetCount_v2)(&devices);
|
2024-01-07 05:40:04 +00:00
|
|
|
if (ret != NVML_SUCCESS) {
|
|
|
|
snprintf(buf, buflen, "unable to get device count: %d", ret);
|
|
|
|
resp->err = strdup(buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < devices; i++) {
|
2024-01-24 18:32:00 +00:00
|
|
|
ret = (*h.nvmlDeviceGetHandleByIndex)(i, &device);
|
2024-01-07 05:40:04 +00:00
|
|
|
if (ret != NVML_SUCCESS) {
|
|
|
|
snprintf(buf, buflen, "unable to get device handle %d: %d", i, ret);
|
|
|
|
resp->err = strdup(buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-24 18:32:00 +00:00
|
|
|
ret = (*h.nvmlDeviceGetCudaComputeCapability)(device, &major, &minor);
|
2024-01-07 05:40:04 +00:00
|
|
|
if (ret != NVML_SUCCESS) {
|
|
|
|
snprintf(buf, buflen, "device compute capability lookup failure %d: %d", i, ret);
|
|
|
|
resp->err = strdup(buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Report the lowest major.minor we detect as that limits our compatibility
|
|
|
|
if (resp->major == 0 || resp->major > major ) {
|
|
|
|
resp->major = major;
|
|
|
|
resp->minor = minor;
|
|
|
|
} else if ( resp->major == major && resp->minor > minor ) {
|
|
|
|
resp->minor = minor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-29 19:00:37 +00:00
|
|
|
#endif // __APPLE__
|