Skip to content

Commit

Permalink
Remove get() interface and change platforms to live inside adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
omarahmed1111 committed Oct 18, 2024
1 parent 7252088 commit f585bc7
Show file tree
Hide file tree
Showing 23 changed files with 526 additions and 517 deletions.
7 changes: 1 addition & 6 deletions source/adapters/opencl/adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,10 @@
//
//===----------------------------------------------------------------------===//

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

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 void globalAdapterShutdown() {
Expand Down
11 changes: 9 additions & 2 deletions source/adapters/opencl/adapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "logger/ur_logger.hpp"
#include "platform.hpp"

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

extern ur_adapter_handle_t_ adapter;
std::vector<std::unique_ptr<ur_platform_handle_t_>> URPlatforms;
uint32_t NumPlatforms = 0;
};
40 changes: 20 additions & 20 deletions source/adapters/opencl/command_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ commandHandleReleaseInternal(ur_exp_command_buffer_command_handle_t Command) {
ur_exp_command_buffer_handle_t_::~ur_exp_command_buffer_handle_t_() {
urQueueRelease(hInternalQueue);

cl_context CLContext = hContext->get();
cl_context CLContext = hContext->CLContext;
cl_ext::clReleaseCommandBufferKHR_fn clReleaseCommandBufferKHR = nullptr;
cl_int Res =
cl_ext::getExtFuncFromContext<decltype(clReleaseCommandBufferKHR)>(
Expand All @@ -66,7 +66,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferCreateExp(
ur_queue_handle_t Queue = nullptr;
UR_RETURN_ON_FAILURE(urQueueCreate(hContext, hDevice, nullptr, &Queue));

cl_context CLContext = hContext->get();
cl_context CLContext = hContext->CLContext;
cl_ext::clCreateCommandBufferKHR_fn clCreateCommandBufferKHR = nullptr;
UR_RETURN_ON_FAILURE(
cl_ext::getExtFuncFromContext<decltype(clCreateCommandBufferKHR)>(
Expand All @@ -77,7 +77,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferCreateExp(
pCommandBufferDesc ? pCommandBufferDesc->isUpdatable : false;

ur_device_command_buffer_update_capability_flags_t UpdateCapabilities;
cl_device_id CLDevice = hDevice->get();
cl_device_id CLDevice = hDevice->CLDevice;
CL_RETURN_ON_FAILURE(
getDeviceCommandBufferUpdateCapabilities(CLDevice, UpdateCapabilities));
bool DeviceSupportsUpdate = UpdateCapabilities > 0;
Expand All @@ -91,7 +91,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferCreateExp(
IsUpdatable ? CL_COMMAND_BUFFER_MUTABLE_KHR : 0u, 0};

cl_int Res = CL_SUCCESS;
const cl_command_queue CLQueue = Queue->get();
const cl_command_queue CLQueue = Queue->CLQueue;
auto CLCommandBuffer =
clCreateCommandBufferKHR(1, &CLQueue, Properties, &Res);
CL_RETURN_ON_FAILURE_AND_SET_NULL(Res, phCommandBuffer);
Expand Down Expand Up @@ -132,7 +132,7 @@ urCommandBufferReleaseExp(ur_exp_command_buffer_handle_t hCommandBuffer) {

UR_APIEXPORT ur_result_t UR_APICALL
urCommandBufferFinalizeExp(ur_exp_command_buffer_handle_t hCommandBuffer) {
cl_context CLContext = hCommandBuffer->hContext->get();
cl_context CLContext = hCommandBuffer->hContext->CLContext;
cl_ext::clFinalizeCommandBufferKHR_fn clFinalizeCommandBufferKHR = nullptr;
UR_RETURN_ON_FAILURE(
cl_ext::getExtFuncFromContext<decltype(clFinalizeCommandBufferKHR)>(
Expand Down Expand Up @@ -160,7 +160,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
(void)phEventWaitList;
(void)phEvent;

cl_context CLContext = hCommandBuffer->hContext->get();
cl_context CLContext = hCommandBuffer->hContext->CLContext;
cl_ext::clCommandNDRangeKernelKHR_fn clCommandNDRangeKernelKHR = nullptr;
UR_RETURN_ON_FAILURE(
cl_ext::getExtFuncFromContext<decltype(clCommandNDRangeKernelKHR)>(
Expand All @@ -182,7 +182,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
cl_command_properties_khr *Properties =
hCommandBuffer->IsUpdatable ? UpdateProperties : nullptr;
CL_RETURN_ON_FAILURE(clCommandNDRangeKernelKHR(
hCommandBuffer->CLCommandBuffer, nullptr, Properties, hKernel->get(),
hCommandBuffer->CLCommandBuffer, nullptr, Properties, hKernel->CLKernel,
workDim, pGlobalWorkOffset, pGlobalWorkSize, pLocalWorkSize,
numSyncPointsInWaitList, pSyncPointWaitList, pSyncPoint,
OutCommandHandle));
Expand Down Expand Up @@ -246,16 +246,16 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendMemBufferCopyExp(
(void)phEventWaitList;
(void)phEvent;
(void)phCommand;
cl_context CLContext = hCommandBuffer->hContext->get();
cl_context CLContext = hCommandBuffer->hContext->CLContext;
cl_ext::clCommandCopyBufferKHR_fn clCommandCopyBufferKHR = nullptr;
UR_RETURN_ON_FAILURE(
cl_ext::getExtFuncFromContext<decltype(clCommandCopyBufferKHR)>(
CLContext, cl_ext::ExtFuncPtrCache->clCommandCopyBufferKHRCache,
cl_ext::CommandCopyBufferName, &clCommandCopyBufferKHR));

CL_RETURN_ON_FAILURE(clCommandCopyBufferKHR(
hCommandBuffer->CLCommandBuffer, nullptr, nullptr, hSrcMem->get(),
hDstMem->get(), srcOffset, dstOffset, size, numSyncPointsInWaitList,
hCommandBuffer->CLCommandBuffer, nullptr, nullptr, hSrcMem->CLMemory,
hDstMem->CLMemory, srcOffset, dstOffset, size, numSyncPointsInWaitList,
pSyncPointWaitList, pSyncPoint, nullptr));

return UR_RESULT_SUCCESS;
Expand Down Expand Up @@ -283,16 +283,16 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendMemBufferCopyRectExp(
size_t OpenCLDstRect[3]{dstOrigin.x, dstOrigin.y, dstOrigin.z};
size_t OpenCLRegion[3]{region.width, region.height, region.depth};

cl_context CLContext = hCommandBuffer->hContext->get();
cl_context CLContext = hCommandBuffer->hContext->CLContext;
cl_ext::clCommandCopyBufferRectKHR_fn clCommandCopyBufferRectKHR = nullptr;
UR_RETURN_ON_FAILURE(
cl_ext::getExtFuncFromContext<decltype(clCommandCopyBufferRectKHR)>(
CLContext, cl_ext::ExtFuncPtrCache->clCommandCopyBufferRectKHRCache,
cl_ext::CommandCopyBufferRectName, &clCommandCopyBufferRectKHR));

CL_RETURN_ON_FAILURE(clCommandCopyBufferRectKHR(
hCommandBuffer->CLCommandBuffer, nullptr, nullptr, hSrcMem->get(),
hDstMem->get(), OpenCLOriginRect, OpenCLDstRect, OpenCLRegion,
hCommandBuffer->CLCommandBuffer, nullptr, nullptr, hSrcMem->CLMemory,
hDstMem->CLMemory, OpenCLOriginRect, OpenCLDstRect, OpenCLRegion,
srcRowPitch, srcSlicePitch, dstRowPitch, dstSlicePitch,
numSyncPointsInWaitList, pSyncPointWaitList, pSyncPoint, nullptr));

Expand Down Expand Up @@ -386,15 +386,15 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendMemBufferFillExp(
[[maybe_unused]] ur_event_handle_t *phEvent,
[[maybe_unused]] ur_exp_command_buffer_command_handle_t *phCommand) {

cl_context CLContext = hCommandBuffer->hContext->get();
cl_context CLContext = hCommandBuffer->hContext->CLContext;
cl_ext::clCommandFillBufferKHR_fn clCommandFillBufferKHR = nullptr;
UR_RETURN_ON_FAILURE(
cl_ext::getExtFuncFromContext<decltype(clCommandFillBufferKHR)>(
CLContext, cl_ext::ExtFuncPtrCache->clCommandFillBufferKHRCache,
cl_ext::CommandFillBufferName, &clCommandFillBufferKHR));

CL_RETURN_ON_FAILURE(clCommandFillBufferKHR(
hCommandBuffer->CLCommandBuffer, nullptr, nullptr, hBuffer->get(),
hCommandBuffer->CLCommandBuffer, nullptr, nullptr, hBuffer->CLMemory,
pPattern, patternSize, offset, size, numSyncPointsInWaitList,
pSyncPointWaitList, pSyncPoint, nullptr));

Expand Down Expand Up @@ -452,7 +452,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferEnqueueExp(
uint32_t numEventsInWaitList, const ur_event_handle_t *phEventWaitList,
ur_event_handle_t *phEvent) {

cl_context CLContext = hCommandBuffer->hContext->get();
cl_context CLContext = hCommandBuffer->hContext->CLContext;
cl_ext::clEnqueueCommandBufferKHR_fn clEnqueueCommandBufferKHR = nullptr;
UR_RETURN_ON_FAILURE(
cl_ext::getExtFuncFromContext<decltype(clEnqueueCommandBufferKHR)>(
Expand All @@ -463,9 +463,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferEnqueueExp(
cl_event Event;
std::vector<cl_event> CLWaitEvents(numEventsInWaitList);
for (uint32_t i = 0; i < numEventsInWaitList; i++) {
CLWaitEvents[i] = phEventWaitList[i]->get();
CLWaitEvents[i] = phEventWaitList[i]->CLEvent;
}
cl_command_queue CLQueue = hQueue->get();
cl_command_queue CLQueue = hQueue->CLQueue;
CL_RETURN_ON_FAILURE(clEnqueueCommandBufferKHR(
NumberOfQueues, &CLQueue, hCommandBuffer->CLCommandBuffer,
numEventsInWaitList, CLWaitEvents.data(), &Event));
Expand Down Expand Up @@ -532,7 +532,7 @@ void updateKernelArgs(std::vector<cl_mutable_dispatch_arg_khr> &CLArgs,
for (uint32_t i = 0; i < NumMemobjArgs; i++) {
const ur_exp_command_buffer_update_memobj_arg_desc_t &URMemObjArg =
ArgMemobjList[i];
cl_mem arg_value = URMemObjArg.hNewMemObjArg->get();
cl_mem arg_value = URMemObjArg.hNewMemObjArg->CLMemory;
cl_mutable_dispatch_arg_khr CLArg{
URMemObjArg.argIndex, // arg_index
sizeof(cl_mem), // arg_size
Expand Down Expand Up @@ -567,7 +567,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferUpdateKernelLaunchExp(
}

ur_exp_command_buffer_handle_t hCommandBuffer = hCommand->hCommandBuffer;
cl_context CLContext = hCommandBuffer->hContext->get();
cl_context CLContext = hCommandBuffer->hContext->CLContext;

cl_ext::clUpdateMutableCommandsKHR_fn clUpdateMutableCommandsKHR = nullptr;
UR_RETURN_ON_FAILURE(
Expand Down
57 changes: 52 additions & 5 deletions source/adapters/opencl/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,61 @@
#include <set>
#include <unordered_map>

ur_result_t
ur_context_handle_t_::makeWithNative(native_type Ctx, uint32_t DevCount,
const ur_device_handle_t *phDevices,
ur_context_handle_t &Context) {
try {
uint32_t CLDeviceCount;
CL_RETURN_ON_FAILURE(clGetContextInfo(Ctx, CL_CONTEXT_NUM_DEVICES,
sizeof(CLDeviceCount), &CLDeviceCount,
nullptr));
std::vector<cl_device_id> CLDevices(CLDeviceCount);
CL_RETURN_ON_FAILURE(clGetContextInfo(
Ctx, CL_CONTEXT_DEVICES, sizeof(CLDevices), CLDevices.data(), nullptr));
std::vector<ur_device_handle_t> URDevices;
if (DevCount) {
if (DevCount != CLDeviceCount) {
return UR_RESULT_ERROR_INVALID_CONTEXT;
}
for (uint32_t i = 0; i < DevCount; i++) {
if (phDevices[i]->CLDevice != CLDevices[i]) {
return UR_RESULT_ERROR_INVALID_CONTEXT;
}
URDevices.push_back(phDevices[i]);
}
} else {
DevCount = CLDeviceCount;
for (uint32_t i = 0; i < CLDeviceCount; i++) {
ur_device_handle_t UrDevice = nullptr;
ur_native_handle_t hNativeHandle =
reinterpret_cast<ur_native_handle_t>(CLDevices[i]);
UR_RETURN_ON_FAILURE(urDeviceCreateWithNativeHandle(
hNativeHandle, nullptr, nullptr, &UrDevice));
URDevices.push_back(UrDevice);
}
}

auto URContext =
std::make_unique<ur_context_handle_t_>(Ctx, DevCount, URDevices.data());
Context = URContext.release();
} catch (std::bad_alloc &) {
return UR_RESULT_ERROR_OUT_OF_RESOURCES;
} catch (...) {
return UR_RESULT_ERROR_UNKNOWN;
}

return UR_RESULT_SUCCESS;
}

UR_APIEXPORT ur_result_t UR_APICALL urContextCreate(
uint32_t DeviceCount, const ur_device_handle_t *phDevices,
const ur_context_properties_t *, ur_context_handle_t *phContext) {

cl_int Ret;
std::vector<cl_device_id> CLDevices(DeviceCount);
for (size_t i = 0; i < DeviceCount; i++) {
CLDevices[i] = phDevices[i]->get();
CLDevices[i] = phDevices[i]->CLDevice;
}

try {
Expand Down Expand Up @@ -82,7 +129,7 @@ urContextRelease(ur_context_handle_t hContext) {
// should drastically reduce the chances of the pathological case described
// in the comments in common.hpp.
static std::mutex contextReleaseMutex;
auto clContext = hContext->get();
auto clContext = hContext->CLContext;

std::lock_guard<std::mutex> lock(contextReleaseMutex);
size_t refCount = hContext->getReferenceCount();
Expand All @@ -108,7 +155,7 @@ urContextRetain(ur_context_handle_t hContext) {
UR_APIEXPORT ur_result_t UR_APICALL urContextGetNativeHandle(
ur_context_handle_t hContext, ur_native_handle_t *phNativeContext) {

*phNativeContext = reinterpret_cast<ur_native_handle_t>(hContext->get());
*phNativeContext = reinterpret_cast<ur_native_handle_t>(hContext->CLContext);
return UR_RESULT_SUCCESS;
}

Expand Down Expand Up @@ -172,8 +219,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urContextSetExtendedDeleter(
auto *C = static_cast<ContextCallback *>(pUserData);
C->execute();
};
CL_RETURN_ON_FAILURE(
clSetContextDestructorCallback(hContext->get(), ClCallback, Callback));
CL_RETURN_ON_FAILURE(clSetContextDestructorCallback(hContext->CLContext,
ClCallback, Callback));

return UR_RESULT_SUCCESS;
}
56 changes: 4 additions & 52 deletions source/adapters/opencl/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

struct ur_context_handle_t_ {
using native_type = cl_context;
native_type Context;
native_type CLContext;
std::vector<ur_device_handle_t> Devices;
uint32_t DeviceCount;
std::atomic<uint32_t> RefCount = 0;
bool IsNativeHandleOwned = true;

ur_context_handle_t_(native_type Ctx, uint32_t DevCount,
const ur_device_handle_t *phDevices)
: Context(Ctx), DeviceCount(DevCount) {
: CLContext(Ctx), DeviceCount(DevCount) {
for (uint32_t i = 0; i < DeviceCount; i++) {
Devices.emplace_back(phDevices[i]);
urDeviceRetain(phDevices[i]);
Expand All @@ -40,61 +40,13 @@ struct ur_context_handle_t_ {

static ur_result_t makeWithNative(native_type Ctx, uint32_t DevCount,
const ur_device_handle_t *phDevices,
ur_context_handle_t &Context) {
try {
uint32_t CLDeviceCount;
CL_RETURN_ON_FAILURE(clGetContextInfo(Ctx, CL_CONTEXT_NUM_DEVICES,
sizeof(CLDeviceCount),
&CLDeviceCount, nullptr));
std::vector<cl_device_id> CLDevices(CLDeviceCount);
CL_RETURN_ON_FAILURE(clGetContextInfo(Ctx, CL_CONTEXT_DEVICES,
sizeof(CLDevices), CLDevices.data(),
nullptr));
std::vector<ur_device_handle_t> URDevices;
if (DevCount) {
if (DevCount != CLDeviceCount) {
return UR_RESULT_ERROR_INVALID_CONTEXT;
}
for (uint32_t i = 0; i < DevCount; i++) {
if (phDevices[i]->get() != CLDevices[i]) {
return UR_RESULT_ERROR_INVALID_CONTEXT;
}
URDevices.push_back(phDevices[i]);
}
} else {
DevCount = CLDeviceCount;
for (uint32_t i = 0; i < CLDeviceCount; i++) {
ur_device_handle_t UrDevice = nullptr;
ur_native_handle_t hNativeHandle =
reinterpret_cast<ur_native_handle_t>(CLDevices[i]);
UR_RETURN_ON_FAILURE(urDeviceCreateWithNativeHandle(
hNativeHandle, nullptr, nullptr, &UrDevice));
URDevices.push_back(UrDevice);
}
}

auto URContext = std::make_unique<ur_context_handle_t_>(Ctx, DevCount,
URDevices.data());
Context = URContext.release();
} catch (std::bad_alloc &) {
return UR_RESULT_ERROR_OUT_OF_RESOURCES;
} catch (...) {
return UR_RESULT_ERROR_UNKNOWN;
}

return UR_RESULT_SUCCESS;
}

ur_context_handle_t &Context);
~ur_context_handle_t_() {
for (uint32_t i = 0; i < DeviceCount; i++) {
urDeviceRelease(Devices[i]);
}
if (IsNativeHandleOwned) {
clReleaseContext(Context);
clReleaseContext(CLContext);
}
}

native_type get() { return Context; }

const std::vector<ur_device_handle_t> &getDevices() { return Devices; }
};
Loading

0 comments on commit f585bc7

Please sign in to comment.