Skip to content

Commit

Permalink
GPU: detect GPU index (#1267)
Browse files Browse the repository at this point in the history
  • Loading branch information
aiwantaozi authored Sep 19, 2024
1 parent f804236 commit 9332f42
Show file tree
Hide file tree
Showing 15 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/detection/gpu/gpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const char* detectByOpenGL(FFlist* gpus)
ffStrbufInitMove(&gpu->name, &result.renderer);
ffStrbufInitMove(&gpu->driver, &result.vendor);
ffStrbufInitF(&gpu->platformApi, "OpenGL %s", result.version.chars);
gpu->index = FF_GPU_INDEX_UNSET;
gpu->temperature = FF_GPU_TEMP_UNSET;
gpu->coreCount = FF_GPU_CORE_COUNT_UNSET;
gpu->frequency = FF_GPU_FREQUENCY_UNSET;
Expand Down
2 changes: 2 additions & 0 deletions src/detection/gpu/gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define FF_GPU_VMEM_SIZE_UNSET ((uint64_t)-1)
#define FF_GPU_FREQUENCY_UNSET 0
#define FF_GPU_CORE_USAGE_UNSET (0/0.0)
#define FF_GPU_INDEX_UNSET ((uint8_t)-1)

extern const char* FF_GPU_VENDOR_NAME_APPLE;
extern const char* FF_GPU_VENDOR_NAME_AMD;
Expand All @@ -27,6 +28,7 @@ typedef struct FFGPUMemory

typedef struct FFGPUResult
{
uint8_t index;
FFGPUType type;
FFstrbuf vendor;
FFstrbuf name;
Expand Down
1 change: 1 addition & 0 deletions src/detection/gpu/gpu_apple.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
ffStrbufSetStatic(&gpu->platformApi, "Metal Common 1");

gpu->type = device.hasUnifiedMemory ? FF_GPU_TYPE_INTEGRATED : FF_GPU_TYPE_DISCRETE;
gpu->index = (uint8_t)device.locationNumber;
#endif
}
return NULL;
Expand Down
2 changes: 2 additions & 0 deletions src/detection/gpu/gpu_bsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const char* ffDetectGPUImpl(const FFGPUOptions* options, FFlist* gpus)
ffStrbufInit(&gpu->name);
ffStrbufInitS(&gpu->driver, pc->pd_name);
ffStrbufInit(&gpu->platformApi);
gpu->index = FF_GPU_INDEX_UNSET;
gpu->temperature = FF_GPU_TEMP_UNSET;
gpu->coreCount = FF_GPU_CORE_COUNT_UNSET;
gpu->coreUsage = FF_GPU_CORE_USAGE_UNSET;
Expand Down Expand Up @@ -90,6 +91,7 @@ const char* ffDetectGPUImpl(const FFGPUOptions* options, FFlist* gpus)
.func = pc->pc_sel.pc_func,
},
}, (FFGpuDriverResult) {
.index = &gpu->index,
.temp = options->temp ? &gpu->temperature : NULL,
.memory = options->driverSpecific ? &gpu->dedicated : NULL,
.coreCount = options->driverSpecific ? (uint32_t*) &gpu->coreCount : NULL,
Expand Down
1 change: 1 addition & 0 deletions src/detection/gpu/gpu_driver_specific.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ typedef struct FFGpuDriverCondition
// detect x if not NULL
typedef struct FFGpuDriverResult
{
uint8_t* index;
double* temp;
FFGPUMemory* memory;
uint32_t* coreCount;
Expand Down
2 changes: 2 additions & 0 deletions src/detection/gpu/gpu_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ static const char* detectPci(const FFGPUOptions* options, FFlist* gpus, FFstrbuf
ffStrbufInit(&gpu->name);
ffStrbufInit(&gpu->driver);
ffStrbufInit(&gpu->platformApi);
gpu->index = FF_GPU_INDEX_UNSET;
gpu->temperature = FF_GPU_TEMP_UNSET;
gpu->coreUsage = FF_GPU_CORE_USAGE_UNSET;
gpu->coreCount = FF_GPU_CORE_COUNT_UNSET;
Expand Down Expand Up @@ -366,6 +367,7 @@ static const char* detectPci(const FFGPUOptions* options, FFlist* gpus, FFstrbuf
.func = pciFunc,
},
}, (FFGpuDriverResult) {
.index = &gpu->index,
.temp = options->temp ? &gpu->temperature : NULL,
.memory = options->driverSpecific ? &gpu->dedicated : NULL,
.coreCount = options->driverSpecific ? (uint32_t*) &gpu->coreCount : NULL,
Expand Down
7 changes: 7 additions & 0 deletions src/detection/gpu/gpu_mthreads.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ const char *ffDetectMthreadsGpuInfo(const FFGpuDriverCondition *cond, FFGpuDrive
}
}

if (result.index)
{
unsigned int value;
if (mtmlData.ffmtmlDeviceGetIndex(device, &value) == MTML_SUCCESS)
*result.index = (uint8_t)value;
}

if (result.temp)
{
MtmlGpu *gpu = NULL;
Expand Down
10 changes: 10 additions & 0 deletions src/detection/gpu/gpu_nvidia.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct FFNvmlData {
FF_LIBRARY_SYMBOL(nvmlDeviceGetMaxClockInfo)
FF_LIBRARY_SYMBOL(nvmlDeviceGetUtilizationRates)
FF_LIBRARY_SYMBOL(nvmlDeviceGetBrand)
FF_LIBRARY_SYMBOL(nvmlDeviceGetIndex)
FF_LIBRARY_SYMBOL(nvmlDeviceGetName)

bool inited;
Expand All @@ -39,6 +40,7 @@ const char* ffDetectNvidiaGpuInfo(const FFGpuDriverCondition* cond, FFGpuDriverR
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libnvml, nvmlData, nvmlDeviceGetMaxClockInfo)
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libnvml, nvmlData, nvmlDeviceGetUtilizationRates)
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libnvml, nvmlData, nvmlDeviceGetBrand)
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libnvml, nvmlData, nvmlDeviceGetIndex)
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libnvml, nvmlData, nvmlDeviceGetName)

if (ffnvmlInit_v2() != NVML_SUCCESS)
Expand Down Expand Up @@ -105,6 +107,14 @@ const char* ffDetectNvidiaGpuInfo(const FFGpuDriverCondition* cond, FFGpuDriverR
}
}

if (result.index)
{
unsigned int value;
if (nvmlData.ffnvmlDeviceGetIndex(device, &value) == NVML_SUCCESS)
*result.index = (uint8_t)value;
}


if (result.temp)
{
uint32_t value;
Expand Down
1 change: 1 addition & 0 deletions src/detection/gpu/gpu_sunos.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const char* ffDetectGPUImpl(FF_MAYBE_UNUSED const FFGPUOptions* options, FFlist*
ffStrbufInit(&gpu->name);
ffStrbufInit(&gpu->driver);
ffStrbufInit(&gpu->platformApi);
gpu->index = FF_GPU_INDEX_UNSET;
gpu->temperature = FF_GPU_TEMP_UNSET;
gpu->coreCount = FF_GPU_CORE_COUNT_UNSET;
gpu->coreUsage = FF_GPU_CORE_USAGE_UNSET;
Expand Down
4 changes: 3 additions & 1 deletion src/detection/gpu/gpu_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const char* ffDetectGPUImpl(FF_MAYBE_UNUSED const FFGPUOptions* options, FFlist*
ffStrbufInit(&gpu->name);
ffStrbufInit(&gpu->driver);
ffStrbufInitStatic(&gpu->platformApi, "SetupAPI");
gpu->index = FF_GPU_INDEX_UNSET;
gpu->temperature = FF_GPU_TEMP_UNSET;
gpu->coreCount = FF_GPU_CORE_COUNT_UNSET;
gpu->coreUsage = FF_GPU_CORE_USAGE_UNSET;
Expand Down Expand Up @@ -157,7 +158,8 @@ const char* ffDetectGPUImpl(FF_MAYBE_UNUSED const FFGPUOptions* options, FFlist*
},
.luid = gpu->deviceId,
},
(FFGpuDriverResult) {
(FFGpuDriverResult){
.index = &gpu->index,
.temp = options->temp ? &gpu->temperature : NULL,
.memory = options->driverSpecific ? &gpu->dedicated : NULL,
.coreCount = options->driverSpecific ? (uint32_t*) &gpu->coreCount : NULL,
Expand Down
4 changes: 3 additions & 1 deletion src/detection/gpu/gpu_wsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const char* ffGPUDetectByDirectX(FF_MAYBE_UNUSED const FFGPUOptions* options, FF

FFGPUResult* gpu = (FFGPUResult*) ffListAdd(gpus);
ffStrbufInitS(&gpu->name, desc);
gpu->index = FF_GPU_INDEX_UNSET;
gpu->coreCount = FF_GPU_CORE_COUNT_UNSET;
gpu->coreUsage = FF_GPU_CORE_USAGE_UNSET;
gpu->temperature = FF_GPU_TEMP_UNSET;
Expand Down Expand Up @@ -117,7 +118,8 @@ const char* ffGPUDetectByDirectX(FF_MAYBE_UNUSED const FFGPUOptions* options, FF
.revId = hardwareId.revision,
},
};
ffDetectNvidiaGpuInfo(&cond, (FFGpuDriverResult) {
ffDetectNvidiaGpuInfo(&cond, (FFGpuDriverResult){
.index = &gpu->index,
.temp = options->temp ? &gpu->temperature : NULL,
.memory = options->driverSpecific ? &gpu->dedicated : NULL,
.coreCount = options->driverSpecific ? (uint32_t*) &gpu->coreCount : NULL,
Expand Down
2 changes: 2 additions & 0 deletions src/detection/gpu/nvml.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,7 @@ extern nvmlReturn_t nvmlDeviceGetMaxClockInfo(nvmlDevice_t device, nvmlClockType
extern nvmlReturn_t nvmlDeviceGetBrand(nvmlDevice_t device, nvmlBrandType_t* type);
// Retrieves the current utilization rates for the device
extern nvmlReturn_t nvmlDeviceGetUtilizationRates(nvmlDevice_t device, nvmlUtilization_t *utilization);
// Retrieves the globally unique immutable UUID associated with this device, as a 5 part hexadecimal string, that augments the immutable, board serial identifier.
extern nvmlReturn_t nvmlDeviceGetIndex(nvmlDevice_t device, unsigned int *index);
// Retrieves the name of this device.
extern nvmlReturn_t nvmlDeviceGetName(nvmlDevice_t device, char *name, unsigned int length);
1 change: 1 addition & 0 deletions src/detection/opencl/opencl.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ static const char* openCLHandleData(OpenCLData* data, FFOpenCLResult* result)
ffStrbufInit(&gpu->vendor);
ffStrbufInit(&gpu->driver);
ffStrbufInit(&gpu->platformApi);
gpu->index = FF_GPU_INDEX_UNSET;
gpu->temperature = FF_GPU_TEMP_UNSET;
gpu->coreCount = FF_GPU_CORE_COUNT_UNSET;
gpu->type = FF_GPU_TYPE_UNKNOWN;
Expand Down
1 change: 1 addition & 0 deletions src/detection/vulkan/vulkan.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ static const char* detectVulkan(FFVulkanResult* result)
}

//No way to detect those using vulkan
gpu->index = FF_GPU_INDEX_UNSET;
gpu->coreCount = FF_GPU_CORE_COUNT_UNSET;
gpu->temperature = FF_GPU_TEMP_UNSET;
gpu->frequency = FF_GPU_FREQUENCY_UNSET;
Expand Down
9 changes: 9 additions & 0 deletions src/modules/gpu/gpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,15 @@ void ffGenerateGPUJsonResult(FFGPUOptions* options, yyjson_mut_doc* doc, yyjson_
FF_LIST_FOR_EACH(FFGPUResult, gpu, gpus)
{
yyjson_mut_val* obj = yyjson_mut_arr_add_obj(doc, arr);

if (gpu->index != FF_GPU_INDEX_UNSET){
yyjson_mut_obj_add_uint(doc, obj, "index", (uint64_t)gpu->index);
}
else
{
yyjson_mut_obj_add_null(doc, obj, "index");
}

if (gpu->coreCount != FF_GPU_CORE_COUNT_UNSET)
yyjson_mut_obj_add_int(doc, obj, "coreCount", gpu->coreCount);
else
Expand Down

0 comments on commit 9332f42

Please sign in to comment.