OpenCL: split detection code

This commit is contained in:
李通洲
2023-04-25 19:03:20 +08:00
committed by Carter Li
parent 2fd3643632
commit aa236b4da0
4 changed files with 146 additions and 113 deletions
+1
View File
@@ -248,6 +248,7 @@ set(LIBFASTFETCH_SRC
src/detection/host/host.c
src/detection/locale/locale.c
src/detection/media/media.c
src/detection/opencl/opencl.c
src/detection/os/os.c
src/detection/packages/packages.c
src/detection/terminalfont/terminalfont.c
+102
View File
@@ -0,0 +1,102 @@
#include "detection/opencl/opencl.h"
#if defined(FF_HAVE_OPENCL) || defined(__APPLE__)
#include "common/library.h"
#include "common/parsing.h"
#include "util/stringUtils.h"
#include <string.h>
#define CL_TARGET_OPENCL_VERSION 100
#ifdef FF_HAVE_OPENCL
#include <CL/cl.h>
#else
#include <OpenCL/cl.h>
#endif
typedef struct OpenCLData
{
FF_LIBRARY_SYMBOL(clGetPlatformIDs)
FF_LIBRARY_SYMBOL(clGetDeviceIDs)
FF_LIBRARY_SYMBOL(clGetDeviceInfo)
} OpenCLData;
static const char* openCLHandleData(OpenCLData* data, FFOpenCLResult* result)
{
cl_platform_id platformID;
cl_uint numPlatforms;
data->ffclGetPlatformIDs(1, &platformID, &numPlatforms);
if(numPlatforms == 0)
return "clGetPlatformIDs returned 0 platforms";
cl_device_id deviceID;
cl_uint numDevices;
data->ffclGetDeviceIDs(platformID, CL_DEVICE_TYPE_GPU, 1, &deviceID, &numDevices);
if(numDevices == 0)
data->ffclGetDeviceIDs(platformID, CL_DEVICE_TYPE_ALL, 1, &deviceID, &numDevices);
if(numDevices == 0)
return "clGetDeviceIDs returned 0 devices";
char version[64];
data->ffclGetDeviceInfo(deviceID, CL_DEVICE_VERSION, sizeof(version), version, NULL);
if(!ffStrSet(version))
return "clGetDeviceInfo returned NULL or empty string";
const char* versionPretty = version;
const char* prefix = "OpenCL ";
if(strncasecmp(version, prefix, sizeof(prefix) - 1) == 0)
versionPretty = version + sizeof(prefix) - 1;
ffStrbufSetS(&result->version, versionPretty);
ffStrbufEnsureFree(&result->device, 128);
data->ffclGetDeviceInfo(deviceID, CL_DEVICE_NAME, result->device.allocated, result->device.chars, NULL);
ffStrbufRecalculateLength(&result->device);
ffStrbufEnsureFree(&result->vendor, 32);
data->ffclGetDeviceInfo(deviceID, CL_DEVICE_VENDOR, result->vendor.allocated, result->vendor.chars, NULL);
ffStrbufRecalculateLength(&result->vendor);
return NULL;
}
#endif // defined(FF_HAVE_OPENCL) || defined(__APPLE__)
const char* ffDetectOpenCL(FFinstance* instance, FFOpenCLResult* result)
{
#ifdef FF_HAVE_OPENCL
OpenCLData data;
FF_LIBRARY_LOAD(opencl, &instance->config.libOpenCL, "dlopen libOpenCL"FF_LIBRARY_EXTENSION" failed",
#ifdef _WIN32
"OpenCL"FF_LIBRARY_EXTENSION, -1,
#endif
"libOpenCL"FF_LIBRARY_EXTENSION, 1
);
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(opencl, data, clGetPlatformIDs);
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(opencl, data, clGetDeviceIDs);
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(opencl, data, clGetDeviceInfo);
return openCLHandleData(&data, result);
#elif defined(__APPLE__) // FF_HAVE_OPENCL
FF_UNUSED(instance);
OpenCLData data;
data.ffclGetPlatformIDs = clGetPlatformIDs;
data.ffclGetDeviceIDs = clGetDeviceIDs;
data.ffclGetDeviceInfo = clGetDeviceInfo;
return openCLHandleData(&data, result);
#else
FF_UNUSED(instance, result);
return "Fastfetch was build without OpenCL support";
#endif // FF_HAVE_OPENCL
}
+17
View File
@@ -0,0 +1,17 @@
#pragma once
#ifndef FF_INCLUDED_detection_opencl_opencl
#define FF_INCLUDED_detection_opencl_opencl
#include "fastfetch.h"
typedef struct FFOpenCLResult
{
FFstrbuf version;
FFstrbuf device;
FFstrbuf vendor;
} FFOpenCLResult;
const char* ffDetectOpenCL(FFinstance* instance, FFOpenCLResult* result);
#endif
+26 -113
View File
@@ -1,126 +1,39 @@
#include "fastfetch.h"
#include "common/printing.h"
#include "detection/opencl/opencl.h"
#define FF_OPENCL_MODULE_NAME "OpenCL"
#define FF_OPENCL_NUM_FORMAT_ARGS 3
#if defined(FF_HAVE_OPENCL) || defined(__APPLE__)
#include "common/library.h"
#include "common/parsing.h"
#include "util/stringUtils.h"
#include <string.h>
#define CL_TARGET_OPENCL_VERSION 100
#ifdef FF_HAVE_OPENCL
#include <CL/cl.h>
#else
#include <OpenCL/cl.h>
#endif
typedef struct OpenCLData
{
FF_LIBRARY_SYMBOL(clGetPlatformIDs)
FF_LIBRARY_SYMBOL(clGetDeviceIDs)
FF_LIBRARY_SYMBOL(clGetDeviceInfo)
} OpenCLData;
static const char* openCLHandleData(FFinstance* instance, OpenCLData* data)
{
cl_platform_id platformID;
cl_uint numPlatforms;
data->ffclGetPlatformIDs(1, &platformID, &numPlatforms);
if(numPlatforms == 0)
return "clGetPlatformIDs returned 0 platforms";
cl_device_id deviceID;
cl_uint numDevices;
data->ffclGetDeviceIDs(platformID, CL_DEVICE_TYPE_GPU, 1, &deviceID, &numDevices);
if(numDevices == 0)
data->ffclGetDeviceIDs(platformID, CL_DEVICE_TYPE_ALL, 1, &deviceID, &numDevices);
if(numDevices == 0)
return "clGetDeviceIDs returned 0 devices";
char version[64];
data->ffclGetDeviceInfo(deviceID, CL_DEVICE_VERSION, sizeof(version), version, NULL);
if(!ffStrSet(version))
return "clGetDeviceInfo returned NULL or empty string";
const char* versionPretty = version;
const char* prefix = "OpenCL ";
if(strncasecmp(version, prefix, sizeof(prefix) - 1) == 0)
versionPretty = version + sizeof(prefix) - 1;
char device[128];
data->ffclGetDeviceInfo(deviceID, CL_DEVICE_NAME, sizeof(device), device, NULL);
char vendor[32];
data->ffclGetDeviceInfo(deviceID, CL_DEVICE_VENDOR, sizeof(vendor), vendor, NULL);
if(instance->config.openCL.outputFormat.length == 0)
{
ffPrintLogoAndKey(instance, FF_OPENCL_MODULE_NAME, 0, &instance->config.openCL.key);
puts(versionPretty);
}
else
{
ffPrintFormat(instance, FF_OPENCL_MODULE_NAME, 0, &instance->config.openCL, FF_OPENCL_NUM_FORMAT_ARGS, (FFformatarg[]) {
{FF_FORMAT_ARG_TYPE_STRING, versionPretty},
{FF_FORMAT_ARG_TYPE_STRING, device},
{FF_FORMAT_ARG_TYPE_STRING, vendor}
});
}
return NULL;
}
#endif // FF_HAVE_OPENCL || __APPLE__
#ifdef FF_HAVE_OPENCL
static const char* printOpenCL(FFinstance* instance)
{
OpenCLData data;
FF_LIBRARY_LOAD(opencl, &instance->config.libOpenCL, "dlopen libOpenCL"FF_LIBRARY_EXTENSION" failed",
#ifdef _WIN32
"OpenCL"FF_LIBRARY_EXTENSION, -1,
#endif
"libOpenCL"FF_LIBRARY_EXTENSION, 1
);
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(opencl, data, clGetPlatformIDs);
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(opencl, data, clGetDeviceIDs);
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(opencl, data, clGetDeviceInfo);
return openCLHandleData(instance, &data);
}
#elif defined(__APPLE__) // FF_HAVE_OPENCL
static const char* printOpenCL(FFinstance* instance)
{
OpenCLData data;
data.ffclGetPlatformIDs = clGetPlatformIDs;
data.ffclGetDeviceIDs = clGetDeviceIDs;
data.ffclGetDeviceInfo = clGetDeviceInfo;
return openCLHandleData(instance, &data);
}
#endif // FF_HAVE_OPENCL
void ffPrintOpenCL(FFinstance* instance)
{
const char* error;
FFOpenCLResult opencl;
ffStrbufInit(&opencl.version);
ffStrbufInit(&opencl.device);
ffStrbufInit(&opencl.vendor);
#if defined(FF_HAVE_OPENCL) || defined(__APPLE__)
error = printOpenCL(instance);
#else
error = "Fastfetch was build without OpenCL support";
#endif
const char* error = ffDetectOpenCL(instance, &opencl);
if(error != NULL)
ffPrintError(instance, FF_OPENCL_MODULE_NAME, 0, &instance->config.openCL, "%s", error);
else
{
if(instance->config.openCL.outputFormat.length == 0)
{
ffPrintLogoAndKey(instance, FF_OPENCL_MODULE_NAME, 0, &instance->config.openCL.key);
ffStrbufPutTo(&opencl.version, stdout);
}
else
{
ffPrintFormat(instance, FF_OPENCL_MODULE_NAME, 0, &instance->config.openCL, FF_OPENCL_NUM_FORMAT_ARGS, (FFformatarg[]) {
{FF_FORMAT_ARG_TYPE_STRBUF, &opencl.version},
{FF_FORMAT_ARG_TYPE_STRBUF, &opencl.device},
{FF_FORMAT_ARG_TYPE_STRBUF, &opencl.vendor},
});
}
}
ffStrbufDestroy(&opencl.version);
ffStrbufDestroy(&opencl.device);
ffStrbufDestroy(&opencl.vendor);
}