From dbd1f9635376c604c7f2b0c4f08fc8e4462807c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 20 Jun 2024 19:44:08 +0800 Subject: [PATCH] OpenCL: don't use NULL as platform id --- src/detection/opencl/opencl.c | 186 +++++++++++++++++----------------- 1 file changed, 93 insertions(+), 93 deletions(-) diff --git a/src/detection/opencl/opencl.c b/src/detection/opencl/opencl.c index d783eb381..3432665d4 100644 --- a/src/detection/opencl/opencl.c +++ b/src/detection/opencl/opencl.c @@ -21,6 +21,7 @@ typedef struct OpenCLData { + FF_LIBRARY_SYMBOL(clGetPlatformIDs) FF_LIBRARY_SYMBOL(clGetPlatformInfo) FF_LIBRARY_SYMBOL(clGetDeviceInfo) FF_LIBRARY_SYMBOL(clGetDeviceIDs) @@ -28,113 +29,110 @@ typedef struct OpenCLData static const char* openCLHandleData(OpenCLData* data, FFOpenCLResult* result) { - char buffer[1024] = ""; - cl_int res = data->ffclGetPlatformInfo(NULL, CL_PLATFORM_VERSION, sizeof(buffer), buffer, NULL); - if (res != CL_SUCCESS) + cl_platform_id platforms[32]; + cl_uint numPlatforms = 0; + if (data->ffclGetPlatformIDs(sizeof(platforms) / sizeof(platforms[0]), platforms, &numPlatforms) != CL_SUCCESS) + return "clGetPlatformIDs() failed"; + + if (numPlatforms == 0) + return "clGetPlatformIDs returned 0 platforms"; + + char buffer[1024]; + for (cl_uint iplat = 0; iplat < numPlatforms; ++iplat) { - switch (res) + if (data->ffclGetPlatformInfo(platforms[iplat], CL_PLATFORM_VERSION, sizeof(buffer), buffer, NULL) != CL_SUCCESS) + return "clGetPlatformInfo() failed"; + + if (iplat == 0) { - case CL_INVALID_PLATFORM: - return "clGetPlatformInfo(NULL, CL_PLATFORM_VERSION) failed: CL_INVALID_PLATFORM"; - case CL_INVALID_VALUE: - return "clGetPlatformInfo(NULL, CL_PLATFORM_VERSION) failed: CL_INVALID_VALUE"; - case CL_OUT_OF_HOST_MEMORY: - return "clGetPlatformInfo(NULL, CL_PLATFORM_VERSION) failed: CL_OUT_OF_HOST_MEMORY"; - default: - return "clGetPlatformInfo(NULL, CL_PLATFORM_VERSION) failed: unknown error"; - } - } + const char* versionPretty = buffer; + if(ffStrStartsWithIgnCase(buffer, "OpenCL ")) + versionPretty = buffer + strlen("OpenCL "); + ffStrbufSetS(&result->version, versionPretty); + ffStrbufTrim(&result->version, ' '); - { - const char* versionPretty = buffer; - if(ffStrStartsWithIgnCase(buffer, "OpenCL ")) - versionPretty = buffer + strlen("OpenCL "); - ffStrbufSetS(&result->version, versionPretty); - ffStrbufTrim(&result->version, ' '); - } + if (data->ffclGetPlatformInfo(platforms[iplat], CL_PLATFORM_NAME, sizeof(buffer), buffer, NULL) == CL_SUCCESS) + ffStrbufSetS(&result->name, buffer); - if (data->ffclGetPlatformInfo(NULL, CL_PLATFORM_NAME, sizeof(buffer), buffer, NULL) == CL_SUCCESS) - ffStrbufSetS(&result->name, buffer); - - if (data->ffclGetPlatformInfo(NULL, CL_PLATFORM_VENDOR, sizeof(buffer), buffer, NULL) == CL_SUCCESS) - ffStrbufSetS(&result->vendor, buffer); - - cl_device_id deviceIDs[32]; - cl_uint numDevices = (cl_uint) (sizeof(deviceIDs) / sizeof(deviceIDs[0])); - data->ffclGetDeviceIDs(NULL, CL_DEVICE_TYPE_GPU, numDevices, deviceIDs, &numDevices); - - for (cl_uint index = 0; index < numDevices; ++index) - { - cl_device_id deviceID = deviceIDs[index]; - if (data->ffclGetDeviceInfo(deviceID, CL_DEVICE_NAME, sizeof(buffer), buffer, NULL) != CL_SUCCESS) - continue; - - FFGPUResult* gpu = ffListAdd(&result->gpus); - ffStrbufInitS(&gpu->name, buffer); - ffStrbufInit(&gpu->vendor); - ffStrbufInit(&gpu->driver); - ffStrbufInit(&gpu->platformApi); - gpu->temperature = FF_GPU_TEMP_UNSET; - gpu->coreCount = FF_GPU_CORE_COUNT_UNSET; - gpu->type = FF_GPU_TYPE_UNKNOWN; - gpu->dedicated.total = gpu->dedicated.used = gpu->shared.total = gpu->shared.used = FF_GPU_VMEM_SIZE_UNSET; - gpu->deviceId = index + 1; - gpu->frequency = FF_GPU_FREQUENCY_UNSET; - - if (data->ffclGetDeviceInfo(deviceID, CL_DEVICE_VERSION, sizeof(buffer), buffer, NULL) == CL_SUCCESS) - { - ffStrbufSetS(&gpu->platformApi, buffer); - ffStrbufTrimRight(&gpu->platformApi, ' '); - } - else - ffStrbufSetStatic(&gpu->platformApi, "OpenCL"); - - if (data->ffclGetDeviceInfo(deviceID, CL_DEVICE_VENDOR, sizeof(buffer), buffer, NULL) == CL_SUCCESS) - ffStrbufSetS(&gpu->vendor, buffer); - - if (data->ffclGetDeviceInfo(deviceID, CL_DRIVER_VERSION, sizeof(buffer), buffer, NULL) == CL_SUCCESS) - { - const char* versionPretty = strchr(buffer, ' '); - if (versionPretty && *versionPretty) - ffStrbufSetS(&gpu->driver, versionPretty + 1); - else - ffStrbufSetS(&gpu->driver, buffer); + if (data->ffclGetPlatformInfo(platforms[iplat], CL_PLATFORM_VENDOR, sizeof(buffer), buffer, NULL) == CL_SUCCESS) + ffStrbufSetS(&result->vendor, buffer); } - { - cl_uint value; - if (data->ffclGetDeviceInfo(deviceID, CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(value), &value, NULL) == CL_SUCCESS) - gpu->coreCount = (int32_t) value; - } + cl_device_id deviceIDs[32]; + cl_uint numDevices = (cl_uint) (sizeof(deviceIDs) / sizeof(deviceIDs[0])); + data->ffclGetDeviceIDs(platforms[iplat], CL_DEVICE_TYPE_GPU, numDevices, deviceIDs, &numDevices); + for (cl_uint idev = 0; idev < numDevices; ++idev) { - cl_uint value; - if (data->ffclGetDeviceInfo(deviceID, CL_DEVICE_MAX_CLOCK_FREQUENCY, sizeof(value), &value, NULL) == CL_SUCCESS) - gpu->frequency = value / 1000.; - } + cl_device_id deviceID = deviceIDs[idev]; + if (data->ffclGetDeviceInfo(deviceID, CL_DEVICE_NAME, sizeof(buffer), buffer, NULL) != CL_SUCCESS) + continue; - { - cl_bool value; - if (data->ffclGetDeviceInfo(deviceID, CL_DEVICE_HOST_UNIFIED_MEMORY, sizeof(value), &value, NULL) == CL_SUCCESS) - gpu->type = value ? FF_GPU_TYPE_INTEGRATED : FF_GPU_TYPE_DISCRETE; - } + FFGPUResult* gpu = ffListAdd(&result->gpus); + ffStrbufInitS(&gpu->name, buffer); + ffStrbufInit(&gpu->vendor); + ffStrbufInit(&gpu->driver); + ffStrbufInit(&gpu->platformApi); + gpu->temperature = FF_GPU_TEMP_UNSET; + gpu->coreCount = FF_GPU_CORE_COUNT_UNSET; + gpu->type = FF_GPU_TYPE_UNKNOWN; + gpu->dedicated.total = gpu->dedicated.used = gpu->shared.total = gpu->shared.used = FF_GPU_VMEM_SIZE_UNSET; + gpu->deviceId = 0; + gpu->frequency = FF_GPU_FREQUENCY_UNSET; - { - cl_device_local_mem_type memType; - if (data->ffclGetDeviceInfo(deviceID, CL_DEVICE_LOCAL_MEM_TYPE, sizeof(memType), &memType, NULL) == CL_SUCCESS && memType == CL_LOCAL) + if (data->ffclGetDeviceInfo(deviceID, CL_DEVICE_VERSION, sizeof(buffer), buffer, NULL) == CL_SUCCESS) { - cl_ulong value; - // 32 KiB is the minimum value required by OpenCL spec, don't use it - if (data->ffclGetDeviceInfo(deviceID, CL_DEVICE_LOCAL_MEM_SIZE, sizeof(value), &value, NULL) == CL_SUCCESS && value > 32 * 1024) - gpu->dedicated.total = value; + ffStrbufSetS(&gpu->platformApi, buffer); + ffStrbufTrimRight(&gpu->platformApi, ' '); } - } + else + ffStrbufSetStatic(&gpu->platformApi, "OpenCL"); - { - cl_ulong value; - if (data->ffclGetDeviceInfo(deviceID, CL_DEVICE_GLOBAL_MEM_SIZE, sizeof(value), &value, NULL) == CL_SUCCESS) - gpu->shared.total = value; + { + cl_uint vendorId; + if (data->ffclGetDeviceInfo(deviceID, CL_DEVICE_VENDOR_ID, sizeof(vendorId), &vendorId, NULL) == CL_SUCCESS) + ffStrbufSetStatic(&gpu->vendor, ffGetGPUVendorString(vendorId)); + if (gpu->vendor.length == 0 && data->ffclGetDeviceInfo(deviceID, CL_DEVICE_VENDOR, sizeof(buffer), buffer, NULL) == CL_SUCCESS) + ffStrbufSetS(&gpu->vendor, buffer); + } + if (data->ffclGetDeviceInfo(deviceID, CL_DRIVER_VERSION, sizeof(buffer), buffer, NULL) == CL_SUCCESS) + { + const char* versionPretty = strchr(buffer, ' '); + if (versionPretty && *versionPretty) + ffStrbufSetS(&gpu->driver, versionPretty + 1); + else + ffStrbufSetS(&gpu->driver, buffer); + } + + { + cl_uint value; + if (data->ffclGetDeviceInfo(deviceID, CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(value), &value, NULL) == CL_SUCCESS) + gpu->coreCount = (int32_t) value; + } + + { + cl_uint value; + if (data->ffclGetDeviceInfo(deviceID, CL_DEVICE_MAX_CLOCK_FREQUENCY, sizeof(value), &value, NULL) == CL_SUCCESS) + gpu->frequency = value / 1000.; + } + + { + cl_bool value; + if (data->ffclGetDeviceInfo(deviceID, CL_DEVICE_HOST_UNIFIED_MEMORY, sizeof(value), &value, NULL) == CL_SUCCESS) + { + gpu->type = value ? FF_GPU_TYPE_INTEGRATED : FF_GPU_TYPE_DISCRETE; + + cl_ulong memSize; + if (data->ffclGetDeviceInfo(deviceID, CL_DEVICE_GLOBAL_MEM_SIZE, sizeof(memSize), &memSize, NULL) == CL_SUCCESS) + { + if (gpu->type == FF_GPU_TYPE_INTEGRATED) + gpu->shared.total = memSize; + else + gpu->dedicated.total = memSize; + } + } + } } } @@ -153,6 +151,7 @@ static const char* detectOpenCL(FFOpenCLResult* result) #endif "libOpenCL"FF_LIBRARY_EXTENSION, 1 ); + FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(opencl, data, clGetPlatformIDs); FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(opencl, data, clGetPlatformInfo); FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(opencl, data, clGetDeviceIDs); FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(opencl, data, clGetDeviceInfo); @@ -161,6 +160,7 @@ static const char* detectOpenCL(FFOpenCLResult* result) #else + data.ffclGetPlatformIDs = clGetPlatformIDs; data.ffclGetPlatformInfo = clGetPlatformInfo; data.ffclGetDeviceIDs = clGetDeviceIDs; data.ffclGetDeviceInfo = clGetDeviceInfo;