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 1d2b12a commit f5db24e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
46 changes: 46 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 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_SetForcePstate_t)(NvPhysicalGpuHandle, NvU32, NvU32);
typedef NvAPI_Status (*NvAPI_GetErrorMessage_t)(NvAPI_Status, NvAPI_ShortString);
typedef NvAPI_Status (*NvAPI_Initialize_t)();
Expand All @@ -25,6 +26,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_SetForcePstate_t _NvAPI_GPU_SetForcePstate;
static NvAPI_GetErrorMessage_t _NvAPI_GetErrorMessage;
static NvAPI_Initialize_t _NvAPI_Initialize;
Expand All @@ -48,6 +50,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_SetForcePstate(NvPhysicalGpuHandle hPhysicalGpu, NvU32 pstateId, NvU32 fallbackState) {
// Ensure the function pointer is valid
NVAPI_POINTER(_NvAPI_GPU_SetForcePstate);
Expand Down Expand Up @@ -114,6 +124,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_SetForcePstate = (NvAPI_GPU_SetForcePstate_t) nvapi_QueryInterface(0x025bfb10);
_NvAPI_GetErrorMessage = (NvAPI_GetErrorMessage_t) nvapi_QueryInterface(0x6c2d048c);
_NvAPI_Initialize = (NvAPI_Initialize_t) nvapi_QueryInterface(0x0150e828);
Expand Down

0 comments on commit f5db24e

Please sign in to comment.