OpenCL: don't use NULL as platform id

This commit is contained in:
李通洲
2024-06-20 19:44:08 +08:00
parent 8b3cff33da
commit dbd1f96353
+93 -93
View File
@@ -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;