Skip to content

Commit

Permalink
Use PCI bus id for sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
sasha0552 authored Oct 28, 2024
1 parent 083c0c4 commit 025c7c6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 26 deletions.
34 changes: 14 additions & 20 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,27 +226,21 @@ 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 NVML device identifiers
NvU32 nvmlIdentifiers[NVAPI_MAX_PHYSICAL_GPUS];

// Array to hold NVAPI device serial numbers, each with up to 32 characters
char nvapiSerials[NVAPI_MAX_PHYSICAL_GPUS][32];
// Array to hold NVAPI device identifiers
NvU32 nvapiIdentifiers[NVAPI_MAX_PHYSICAL_GPUS];

// Step 1: Loop through each device to retrieve and store NVML and NVAPI serials
// Step 1: Loop through each device to retrieve and store NVML and NVAPI identifiers
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);
nvmlPciInfo_t nvmlPciInfo;
NVML_CALL(nvmlDeviceGetPciInfo(nvmlDevices[i], &nvmlPciInfo), errored);
nvmlIdentifiers[i] = nvmlPciInfo.bus;

// 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);
NvU32 nvapiBusId;
NVAPI_CALL(NvAPI_GPU_GetBusId(nvapiDevices[i], &nvapiBusId), errored);
nvapiIdentifiers[i] = nvapiBusId;
}

// Array to store NVAPI device handles in sorted order
Expand All @@ -255,9 +249,9 @@ int main(int argc, char *argv[]) {
// 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
// Compare NVML and NVAPI identifiers
if (nvmlIdentifiers[i] == nvapiIdentifiers[j]) {
//
sortedNvapiDevices[i] = nvapiDevices[j];

// Exit the inner loop
Expand Down
12 changes: 6 additions & 6 deletions src/nvapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +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_GetBusId_t)(NvPhysicalGpuHandle, NvU32 *);
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 @@ -27,7 +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_GetBusId_t _NvAPI_GPU_GetBusId;
static NvAPI_GPU_GetFullName_t _NvAPI_GPU_GetFullName;
static NvAPI_GPU_SetForcePstate_t _NvAPI_GPU_SetForcePstate;
static NvAPI_GetErrorMessage_t _NvAPI_GetErrorMessage;
Expand All @@ -52,12 +52,12 @@ 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) {
NvAPI_Status NvAPI_GPU_GetBusId(NvPhysicalGpuHandle hPhysicalGpu, NvU32 * pBusId) {
// Ensure the function pointer is valid
NVAPI_POINTER(_NvAPI_GPU_GetBoardInfo);
NVAPI_POINTER(_NvAPI_GPU_GetBusId);

// Invoke the function using the provided parameters
return _NvAPI_GPU_GetBoardInfo(hPhysicalGpu, pBoardInfo);
return _NvAPI_GPU_GetBusId(hPhysicalGpu, pBusId);
}

NvAPI_Status NvAPI_GPU_GetFullName(NvPhysicalGpuHandle hPhysicalGpu, NvAPI_ShortString szName) {
Expand Down Expand Up @@ -134,7 +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_GetBusId = (NvAPI_GPU_GetBusId_t) nvapi_QueryInterface(0x1be0b8e5);
_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 025c7c6

Please sign in to comment.