Skip to content

Commit

Permalink
Sort NVAPI handles
Browse files Browse the repository at this point in the history
  • Loading branch information
sasha0552 authored Oct 28, 2024
1 parent beac363 commit 6fba7c9
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
Expand Down
11 changes: 11 additions & 0 deletions src/nvapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 6fba7c9

Please sign in to comment.