Skip to content

Commit 8681d2f

Browse files
committed
GPU (Windows): fix code smells
1 parent 2b20ed8 commit 8681d2f

File tree

4 files changed

+31
-22
lines changed

4 files changed

+31
-22
lines changed

src/detection/gpu/gpu_amd.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const char* ffDetectAmdGpuInfo(const FFGpuDriverCondition* cond, FFGpuDriverResu
6565

6666
FF_AUTO_FREE AdapterInfo* devices = NULL;
6767
int numDevices = 0;
68-
if (adlData.ffADL2_Adapter_AdapterInfoX3_Get(adlData.apiHandle, -1, &numDevices, &devices) == 0)
68+
if (adlData.ffADL2_Adapter_AdapterInfoX3_Get(adlData.apiHandle, -1, &numDevices, &devices) == 0) // Return 1 on success
6969
return "ffADL2_Adapter_AdapterInfoX3_Get() failed";
7070

7171
const AdapterInfo* device = NULL;
@@ -146,7 +146,7 @@ const char* ffDetectAmdGpuInfo(const FFGpuDriverCondition* cond, FFGpuDriverResu
146146
if (adlData.ffADL2_Adapter_ASICFamilyType_Get(adlData.apiHandle, device->iAdapterIndex, &asicTypes, &valids) == ADL_OK)
147147
{
148148
asicTypes &= valids; // This design is strange
149-
*result.type = asicTypes & ADL_ASIC_INTEGRATED ? FF_GPU_TYPE_INTEGRATED : FF_GPU_TYPE_DISCRETE;
149+
*result.type = (asicTypes & ADL_ASIC_INTEGRATED) ? FF_GPU_TYPE_INTEGRATED : FF_GPU_TYPE_DISCRETE;
150150
}
151151
}
152152

src/detection/gpu/gpu_intel.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ const char* ffDetectIntelGpuInfo(const FFGpuDriverCondition* cond, FFGpuDriverRe
215215
availableCount++;
216216
}
217217
}
218-
*result.temp = sumValue / availableCount;
218+
if (availableCount > 0)
219+
*result.temp = sumValue / availableCount;
219220
}
220221
}
221222

src/detection/gpu/gpu_nvidia.c

+25-18
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ struct FFNvapiData {
3333
bool inited;
3434
} nvapiData;
3535

36-
const char* detectMemTypeByNvapi(FFGpuDriverResult* result)
36+
static const char* detectMemTypeByNvapi(FFGpuDriverResult* result)
3737
{
3838
if (!nvapiData.inited)
3939
{
@@ -79,11 +79,14 @@ const char* detectMemTypeByNvapi(FFGpuDriverResult* result)
7979

8080
uint32_t gpuIndex = *result->index;
8181

82-
if ((uint32_t) gpuCount < gpuIndex)
82+
if (gpuIndex >= (uint32_t) gpuCount)
8383
return "GPU index out of range";
8484

85+
// Not very sure. Need to check in multi-GPU system
86+
NvPhysicalGpuHandle gpuHandle = handles[gpuIndex];
87+
8588
NvApiGPUMemoryType memType;
86-
if (nvapiData.ffnvapi_GPU_GetRamType(handles[gpuIndex], &memType) < 0)
89+
if (nvapiData.ffnvapi_GPU_GetRamType(gpuHandle, &memType) < 0)
8790
return "NvAPI_GPU_GetRamType() failed";
8891

8992
switch (memType)
@@ -160,7 +163,7 @@ const char* ffDetectNvidiaGpuInfo(const FFGpuDriverCondition* cond, FFGpuDriverR
160163
if (cond->type & FF_GPU_DRIVER_CONDITION_TYPE_BUS_ID)
161164
{
162165
char pciBusIdStr[32];
163-
snprintf(pciBusIdStr, ARRAY_SIZE(pciBusIdStr) - 1, "%04x:%02x:%02x.%d", cond->pciBusId.domain, cond->pciBusId.bus, cond->pciBusId.device, cond->pciBusId.func);
166+
snprintf(pciBusIdStr, ARRAY_SIZE(pciBusIdStr), "%04x:%02x:%02x.%d", cond->pciBusId.domain, cond->pciBusId.bus, cond->pciBusId.device, cond->pciBusId.func);
164167

165168
nvmlReturn_t ret = nvmlData.ffnvmlDeviceGetHandleByPciBusId_v2(pciBusIdStr, &device);
166169
if (ret != NVML_SUCCESS)
@@ -187,24 +190,28 @@ const char* ffDetectNvidiaGpuInfo(const FFGpuDriverCondition* cond, FFGpuDriverR
187190

188191
break;
189192
}
190-
if (!device) return "Device not found";
191193
}
192194

193-
nvmlBrandType_t brand;
194-
if (nvmlData.ffnvmlDeviceGetBrand(device, &brand) == NVML_SUCCESS)
195+
if (!device) return "Device not found";
196+
197+
if (result.type)
195198
{
196-
switch (brand)
199+
nvmlBrandType_t brand;
200+
if (nvmlData.ffnvmlDeviceGetBrand(device, &brand) == NVML_SUCCESS)
197201
{
198-
case NVML_BRAND_NVIDIA_RTX:
199-
case NVML_BRAND_QUADRO_RTX:
200-
case NVML_BRAND_GEFORCE:
201-
case NVML_BRAND_TITAN:
202-
case NVML_BRAND_TESLA:
203-
case NVML_BRAND_QUADRO:
204-
*result.type = FF_GPU_TYPE_DISCRETE;
205-
break;
206-
default:
207-
break;
202+
switch (brand)
203+
{
204+
case NVML_BRAND_NVIDIA_RTX:
205+
case NVML_BRAND_QUADRO_RTX:
206+
case NVML_BRAND_GEFORCE:
207+
case NVML_BRAND_TITAN:
208+
case NVML_BRAND_TESLA:
209+
case NVML_BRAND_QUADRO:
210+
*result.type = FF_GPU_TYPE_DISCRETE;
211+
break;
212+
default:
213+
break;
214+
}
208215
}
209216
}
210217

src/detection/gpu/gpu_windows.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,14 @@ const char* ffDetectGPUImpl(FF_MAYBE_UNUSED const FFGPUOptions* options, FFlist*
4848
gpu->deviceId = 0;
4949
gpu->frequency = FF_GPU_FREQUENCY_UNSET;
5050

51-
uint32_t pciBus = 0, pciAddr = UINT32_MAX, pciDev = 0, pciFunc = 0;
51+
uint32_t pciBus = 0, pciAddr = 0, pciDev = 0, pciFunc = 0;
5252
if (SetupDiGetDeviceRegistryPropertyW(hdev, &did, SPDRP_BUSNUMBER, NULL, (PBYTE) &pciBus, sizeof(pciBus), NULL) &&
5353
SetupDiGetDeviceRegistryPropertyW(hdev, &did, SPDRP_ADDRESS, NULL, (PBYTE) &pciAddr, sizeof(pciAddr), NULL))
5454
{
5555
pciDev = (pciAddr >> 16) & 0xFFFF;
5656
pciFunc = pciAddr & 0xFFFF;
5757
gpu->deviceId = (pciBus * 1000ull) + (pciDev * 10ull) + pciFunc;
58+
pciAddr = 1; // Set to 1 to indicate that the device is a PCI device
5859
}
5960

6061
wchar_t buffer[256];

0 commit comments

Comments
 (0)