From 6fba7c93ac4fd2af0c17c2dbe3cb5ad1ceb98e10 Mon Sep 17 00:00:00 2001 From: sasha0552 Date: Mon, 28 Oct 2024 05:08:48 +0000 Subject: [PATCH] Sort NVAPI handles --- src/main.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/nvapi.c | 11 +++++++++++ 2 files changed, 63 insertions(+) diff --git a/src/main.c b/src/main.c index 55b57d4..2078ae7 100644 --- a/src/main.c +++ b/src/main.c @@ -224,6 +224,52 @@ int main(int argc, char *argv[]) { } } + /***** SORT NVAPI HANDLES */ + { + // Array to hold NVML device serial numbers, each with up to 32 characters + char nvmlSerials[NVAPI_MAX_PHYSICAL_GPUS][32]; + + // Array to hold NVAPI device serial numbers, each with up to 32 characters + char nvapiSerials[NVAPI_MAX_PHYSICAL_GPUS][32]; + + // Step 1: Loop through each device to retrieve and store NVML and NVAPI serials + for (unsigned int i = 0; i < deviceCount; i++) { + // Get serial number for NVML device and store in nvmlSerials array + NVML_CALL(nvmlDeviceGetSerial(nvmlDevices[i], nvmlSerials[i], 32), errored); + + // Prepare NVAPI board info struct + NV_BOARD_INFO boardInfo = { + .version = NV_BOARD_INFO_VER, + }; + + // Get board info for NVAPI device + NVAPI_CALL(NvAPI_GPU_GetBoardInfo(nvapiDevices[i], &boardInfo), errored); + + // Copy the serial number from board info to the nvapiSerials array + strncpy(nvapiSerials[i], boardInfo.BoardNum, 32); + } + + // Array to store NVAPI device handles in sorted order + NvPhysicalGpuHandle sortedNvapiDevices[NVAPI_MAX_PHYSICAL_GPUS]; + + // Step 2: Match and order NVAPI devices based on serial numbers + for (unsigned int i = 0; i < deviceCount; i++) { + for (unsigned int j = 0; j < deviceCount; j++) { + // Compare NVML and NVAPI serials + if (strcmp(nvmlSerials[i], nvapiSerials[j]) == 0) { + // Set sorted handle + sortedNvapiDevices[i] = nvapiDevices[j]; + + // Exit the inner loop + break; + } + } + } + + // Step 3: Copy sorted handles back to original array + memcpy(nvapiDevices, sortedNvapiDevices, sizeof(sortedNvapiDevices)); + } + /***** INIT *****/ { // Print ids @@ -243,6 +289,12 @@ int main(int argc, char *argv[]) { } } + // If array is empty + if (idsCount == 0) { + // Print "N/A" + printf("N/A"); + } + // Print the count of elements in the array and newline character printf(" (%zu)\n", idsCount); } diff --git a/src/nvapi.c b/src/nvapi.c index 719cf18..6fc10cb 100644 --- a/src/nvapi.c +++ b/src/nvapi.c @@ -15,6 +15,7 @@ typedef void * (*nvapi_QueryInterface_t)(int); typedef NvAPI_Status (*NvAPI_EnumPhysicalGPUs_t)(NvPhysicalGpuHandle[NVAPI_MAX_PHYSICAL_GPUS], NvU32 *); +typedef NvAPI_Status (*NvAPI_GPU_GetBoardInfo_t)(NvPhysicalGpuHandle, NV_BOARD_INFO *); typedef NvAPI_Status (*NvAPI_GPU_GetFullName_t)(NvPhysicalGpuHandle, NvAPI_ShortString); typedef NvAPI_Status (*NvAPI_GPU_SetForcePstate_t)(NvPhysicalGpuHandle, NvU32, NvU32); typedef NvAPI_Status (*NvAPI_GetErrorMessage_t)(NvAPI_Status, NvAPI_ShortString); @@ -26,6 +27,7 @@ typedef NvAPI_Status (*NvAPI_Unload_t)(); static void * lib; static NvAPI_EnumPhysicalGPUs_t _NvAPI_EnumPhysicalGPUs; +static NvAPI_GPU_GetBoardInfo_t _NvAPI_GPU_GetBoardInfo; static NvAPI_GPU_GetFullName_t _NvAPI_GPU_GetFullName; static NvAPI_GPU_SetForcePstate_t _NvAPI_GPU_SetForcePstate; static NvAPI_GetErrorMessage_t _NvAPI_GetErrorMessage; @@ -50,6 +52,14 @@ NvAPI_Status NvAPI_EnumPhysicalGPUs(NvPhysicalGpuHandle nvGPUHandle[NVAPI_MAX_PH return _NvAPI_EnumPhysicalGPUs(nvGPUHandle, pGpuCount); } +NvAPI_Status NvAPI_GPU_GetBoardInfo(NvPhysicalGpuHandle hPhysicalGpu, NV_BOARD_INFO * pBoardInfo) { + // Ensure the function pointer is valid + NVAPI_POINTER(_NvAPI_GPU_GetBoardInfo); + + // Invoke the function using the provided parameters + return _NvAPI_GPU_GetBoardInfo(hPhysicalGpu, pBoardInfo); +} + NvAPI_Status NvAPI_GPU_GetFullName(NvPhysicalGpuHandle hPhysicalGpu, NvAPI_ShortString szName) { // Ensure the function pointer is valid NVAPI_POINTER(_NvAPI_GPU_GetFullName); @@ -124,6 +134,7 @@ NvAPI_Status NvAPI_Initialize() { // Retrieve the addresses of specific NvAPI functions using nvapi_QueryInterface _NvAPI_EnumPhysicalGPUs = (NvAPI_EnumPhysicalGPUs_t) nvapi_QueryInterface(0xe5ac921f); + _NvAPI_GPU_GetBoardInfo = (NvAPI_GPU_GetBoardInfo_t) nvapi_QueryInterface(0x22d54523); _NvAPI_GPU_GetFullName = (NvAPI_GPU_GetFullName_t) nvapi_QueryInterface(0xceee8e9f); _NvAPI_GPU_SetForcePstate = (NvAPI_GPU_SetForcePstate_t) nvapi_QueryInterface(0x025bfb10); _NvAPI_GetErrorMessage = (NvAPI_GetErrorMessage_t) nvapi_QueryInterface(0x6c2d048c);