Skip to content

Commit

Permalink
Merge pull request #2219 from kbenzie/benie/cl-adapter-opt-core-func
Browse files Browse the repository at this point in the history
[CL] Support using old ICD loaders
  • Loading branch information
aarongreig authored Oct 25, 2024
2 parents ab0a706 + 77975b8 commit d3a518a
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 55 deletions.
47 changes: 40 additions & 7 deletions source/adapters/opencl/adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,49 @@
//
//===----------------------------------------------------------------------===//

#include "adapter.hpp"
#include "common.hpp"
#include "logger/ur_logger.hpp"
#include "ur/ur.hpp"

#ifdef _MSC_VER
#include <Windows.h>
#else
#include <dlfcn.h>
#endif

ur_adapter_handle_t_::ur_adapter_handle_t_() {
#ifdef _MSC_VER
// Loading OpenCL.dll increments the libraries internal reference count.
auto handle = LoadLibraryA("OpenCL.dll");

#define CL_CORE_FUNCTION(FUNC) \
FUNC = reinterpret_cast<decltype(::FUNC) *>(GetProcAddress(handle, "FUNC"));
#include "core_functions.def"
#undef CL_CORE_FUNCTION

// So we can safely decrement it here wihtout actually unloading OpenCL.dll.
FreeLibrary(handle);
#else
// Loading libOpenCL.so to get the library handle but don't dlclose it as
// this causes a segfault when attempting to call any OpenCL entry point.
auto handle = dlopen("libOpenCL.so", RTLD_LOCAL);

#define CL_CORE_FUNCTION(FUNC) \
FUNC = reinterpret_cast<decltype(::FUNC) *>(dlsym(handle, #FUNC));
#include "core_functions.def"
#undef CL_CORE_FUNCTION

#endif
}

struct ur_adapter_handle_t_ {
std::atomic<uint32_t> RefCount = 0;
std::mutex Mutex;
logger::Logger &log = logger::get_logger("opencl");
};
static ur_adapter_handle_t adapter = nullptr;

static ur_adapter_handle_t_ *adapter = nullptr;
ur_adapter_handle_t ur::cl::getAdapter() {
if (!adapter) {
die("OpenCL adapter used before initalization or after destruction");
}
return adapter;
}

static void globalAdapterShutdown() {
if (cl_ext::ExtFuncPtrCache) {
Expand Down
24 changes: 22 additions & 2 deletions source/adapters/opencl/adapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@
//
//===----------------------------------------------------------------------===//

struct ur_adapter_handle_t_;
#include "CL/cl.h"
#include "logger/ur_logger.hpp"

extern ur_adapter_handle_t_ adapter;
struct ur_adapter_handle_t_ {
ur_adapter_handle_t_();

std::atomic<uint32_t> RefCount = 0;
std::mutex Mutex;
logger::Logger &log = logger::get_logger("opencl");

// Function pointers to core OpenCL entry points which may not exist in older
// versions of the OpenCL-ICD-Loader are tracked here and initialized by
// dynamically loading the symbol by name.
#define CL_CORE_FUNCTION(FUNC) decltype(::FUNC) *FUNC = nullptr;
#include "core_functions.def"
#undef CL_CORE_FUNCTION
};

namespace ur {
namespace cl {
ur_adapter_handle_t getAdapter();
} // namespace cl
} // namespace ur
4 changes: 0 additions & 4 deletions source/adapters/opencl/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,6 @@ cl_int(CL_API_CALL *)(cl_command_queue, cl_program, const char *, cl_bool,
size_t, size_t, void *, cl_uint, const cl_event *,
cl_event *);

using clSetProgramSpecializationConstant_fn = CL_API_ENTRY
cl_int(CL_API_CALL *)(cl_program program, cl_uint spec_id, size_t spec_size,
const void *spec_value);

using clEnqueueReadHostPipeINTEL_fn = CL_API_ENTRY
cl_int(CL_API_CALL *)(cl_command_queue queue, cl_program program,
const char *pipe_symbol, cl_bool blocking, void *ptr,
Expand Down
10 changes: 9 additions & 1 deletion source/adapters/opencl/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//===----------------------------------------------------------------------===//

#include "context.hpp"
#include "adapter.hpp"

#include <mutex>
#include <set>
Expand Down Expand Up @@ -169,6 +170,13 @@ UR_APIEXPORT ur_result_t UR_APICALL urContextCreateWithNativeHandle(
UR_APIEXPORT ur_result_t UR_APICALL urContextSetExtendedDeleter(
ur_context_handle_t hContext, ur_context_extended_deleter_t pfnDeleter,
void *pUserData) {
if (!ur::cl::getAdapter()->clSetContextDestructorCallback) {
ur::cl::getAdapter()->log.warning(
"clSetContextDestructorCallback not found, consider upgrading the "
"OpenCL-ICD-Loader to the latest version.");
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
}

static std::unordered_map<ur_context_handle_t,
std::set<ur_context_extended_deleter_t>>
ContextCallbackMap;
Expand Down Expand Up @@ -212,7 +220,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urContextSetExtendedDeleter(
auto *C = static_cast<ContextCallback *>(pUserData);
C->execute();
};
CL_RETURN_ON_FAILURE(clSetContextDestructorCallback(
CL_RETURN_ON_FAILURE(ur::cl::getAdapter()->clSetContextDestructorCallback(
cl_adapter::cast<cl_context>(hContext), ClCallback, Callback));

return UR_RESULT_SUCCESS;
Expand Down
14 changes: 14 additions & 0 deletions source/adapters/opencl/core_functions.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Introduced in OpenCL 2.0
// TODO: clCreateCommandQueueWithProperties

// Introduced in OpenCL 2.1
// TODO: clGetKernelSubGroupInfo
// TODO: clCreateProgramWithIL
// TODO: clGetHostTimer
// TODO: clGetDeviceAndHostTimer

// Introduced in OpenCL 2.2
CL_CORE_FUNCTION(clSetProgramSpecializationConstant)

// Introduced in OpenCL 3.0
CL_CORE_FUNCTION(clSetContextDestructorCallback)
1 change: 0 additions & 1 deletion source/adapters/opencl/extension_functions.def
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ CL_EXTENSION_FUNC(clEnqueueWriteGlobalVariable)
CL_EXTENSION_FUNC(clEnqueueReadGlobalVariable)
CL_EXTENSION_FUNC(clEnqueueReadHostPipeINTEL)
CL_EXTENSION_FUNC(clEnqueueWriteHostPipeINTEL)
CL_EXTENSION_FUNC(clSetProgramSpecializationConstant)
CL_EXTENSION_FUNC(clCreateCommandBufferKHR)
CL_EXTENSION_FUNC(clRetainCommandBufferKHR)
CL_EXTENSION_FUNC(clReleaseCommandBufferKHR)
Expand Down
48 changes: 8 additions & 40 deletions source/adapters/opencl/program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//
//===----------------------------------------------------------------------===//

#include "adapter.hpp"
#include "common.hpp"
#include "context.hpp"
#include "device.hpp"
Expand Down Expand Up @@ -392,50 +393,17 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramSetSpecializationConstants(
sizeof(cl_platform_id), &CurPlatform,
nullptr));

oclv::OpenCLVersion PlatVer;
cl_adapter::getPlatformVersion(CurPlatform, PlatVer);

bool UseExtensionLookup = false;
if (PlatVer < oclv::V2_2) {
UseExtensionLookup = true;
} else {
for (cl_device_id Dev : *DevicesInCtx) {
oclv::OpenCLVersion DevVer;

UR_RETURN_ON_FAILURE(cl_adapter::getDeviceVersion(Dev, DevVer));

if (DevVer < oclv::V2_2) {
UseExtensionLookup = true;
break;
}
}
}

if (UseExtensionLookup == false) {
if (ur::cl::getAdapter()->clSetProgramSpecializationConstant) {
for (uint32_t i = 0; i < count; ++i) {
CL_RETURN_ON_FAILURE(clSetProgramSpecializationConstant(
CLProg, pSpecConstants[i].id, pSpecConstants[i].size,
pSpecConstants[i].pValue));
CL_RETURN_ON_FAILURE(
ur::cl::getAdapter()->clSetProgramSpecializationConstant(
CLProg, pSpecConstants[i].id, pSpecConstants[i].size,
pSpecConstants[i].pValue));
}
} else {
cl_ext::clSetProgramSpecializationConstant_fn
SetProgramSpecializationConstant = nullptr;
const ur_result_t URResult = cl_ext::getExtFuncFromContext<
decltype(SetProgramSpecializationConstant)>(
Ctx, cl_ext::ExtFuncPtrCache->clSetProgramSpecializationConstantCache,
cl_ext::SetProgramSpecializationConstantName,
&SetProgramSpecializationConstant);

if (URResult != UR_RESULT_SUCCESS) {
return URResult;
}

for (uint32_t i = 0; i < count; ++i) {
CL_RETURN_ON_FAILURE(SetProgramSpecializationConstant(
CLProg, pSpecConstants[i].id, pSpecConstants[i].size,
pSpecConstants[i].pValue));
}
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
}

return UR_RESULT_SUCCESS;
}

Expand Down

0 comments on commit d3a518a

Please sign in to comment.