Skip to content

Commit 45cc745

Browse files
omarahmed1111RossBrunton
authored andcommitted
Add handles to opencl adapter
1 parent 567215b commit 45cc745

23 files changed

+1954
-1055
lines changed

unified-runtime/source/adapters/opencl/adapter.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
88
//
99
//===----------------------------------------------------------------------===//
10+
#include "logger/ur_logger.hpp"
11+
#include "platform.hpp"
1012

1113
#include "CL/cl.h"
1214
#include "logger/ur_logger.hpp"
@@ -18,6 +20,9 @@ struct ur_adapter_handle_t_ {
1820
std::mutex Mutex;
1921
logger::Logger &log = logger::get_logger("opencl");
2022

23+
std::vector<std::unique_ptr<ur_platform_handle_t_>> URPlatforms;
24+
uint32_t NumPlatforms = 0;
25+
2126
// Function pointers to core OpenCL entry points which may not exist in older
2227
// versions of the OpenCL-ICD-Loader are tracked here and initialized by
2328
// dynamically loading the symbol by name.

unified-runtime/source/adapters/opencl/command_buffer.cpp

Lines changed: 57 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@
1010

1111
#include "command_buffer.hpp"
1212
#include "common.hpp"
13+
#include "context.hpp"
14+
#include "event.hpp"
15+
#include "kernel.hpp"
16+
#include "memory.hpp"
17+
#include "queue.hpp"
1318

1419
/// The ur_exp_command_buffer_handle_t_ destructor calls CL release
1520
/// command-buffer to free the underlying object.
1621
ur_exp_command_buffer_handle_t_::~ur_exp_command_buffer_handle_t_() {
1722
urQueueRelease(hInternalQueue);
1823

19-
cl_context CLContext = cl_adapter::cast<cl_context>(hContext);
24+
cl_context CLContext = hContext->CLContext;
2025
cl_ext::clReleaseCommandBufferKHR_fn clReleaseCommandBufferKHR = nullptr;
2126
cl_int Res =
2227
cl_ext::getExtFuncFromContext<decltype(clReleaseCommandBufferKHR)>(
@@ -44,7 +49,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferCreateExp(
4449
UR_RETURN_ON_FAILURE(
4550
urQueueCreate(hContext, hDevice, &QueueProperties, &Queue));
4651

47-
cl_context CLContext = cl_adapter::cast<cl_context>(hContext);
52+
cl_context CLContext = hContext->CLContext;
4853
cl_ext::clCreateCommandBufferKHR_fn clCreateCommandBufferKHR = nullptr;
4954
UR_RETURN_ON_FAILURE(
5055
cl_ext::getExtFuncFromContext<decltype(clCreateCommandBufferKHR)>(
@@ -54,7 +59,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferCreateExp(
5459
const bool IsUpdatable = pCommandBufferDesc->isUpdatable;
5560

5661
ur_device_command_buffer_update_capability_flags_t UpdateCapabilities;
57-
cl_device_id CLDevice = cl_adapter::cast<cl_device_id>(hDevice);
62+
cl_device_id CLDevice = hDevice->CLDevice;
5863
CL_RETURN_ON_FAILURE(
5964
getDeviceCommandBufferUpdateCapabilities(CLDevice, UpdateCapabilities));
6065
bool DeviceSupportsUpdate = UpdateCapabilities > 0;
@@ -68,16 +73,19 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferCreateExp(
6873
IsUpdatable ? CL_COMMAND_BUFFER_MUTABLE_KHR : 0u, 0};
6974

7075
cl_int Res = CL_SUCCESS;
71-
auto CLCommandBuffer = clCreateCommandBufferKHR(
72-
1, cl_adapter::cast<cl_command_queue *>(&Queue), Properties, &Res);
76+
const cl_command_queue CLQueue = Queue->CLQueue;
77+
auto CLCommandBuffer =
78+
clCreateCommandBufferKHR(1, &CLQueue, Properties, &Res);
7379
CL_RETURN_ON_FAILURE_AND_SET_NULL(Res, phCommandBuffer);
7480

7581
try {
7682
auto URCommandBuffer = std::make_unique<ur_exp_command_buffer_handle_t_>(
7783
Queue, hContext, hDevice, CLCommandBuffer, IsUpdatable, IsInOrder);
7884
*phCommandBuffer = URCommandBuffer.release();
79-
} catch (...) {
85+
} catch (std::bad_alloc &) {
8086
return UR_RESULT_ERROR_OUT_OF_RESOURCES;
87+
} catch (...) {
88+
return UR_RESULT_ERROR_UNKNOWN;
8189
}
8290

8391
CL_RETURN_ON_FAILURE(Res);
@@ -102,7 +110,7 @@ urCommandBufferReleaseExp(ur_exp_command_buffer_handle_t hCommandBuffer) {
102110
UR_APIEXPORT ur_result_t UR_APICALL
103111
urCommandBufferFinalizeExp(ur_exp_command_buffer_handle_t hCommandBuffer) {
104112
UR_ASSERT(!hCommandBuffer->IsFinalized, UR_RESULT_ERROR_INVALID_OPERATION);
105-
cl_context CLContext = cl_adapter::cast<cl_context>(hCommandBuffer->hContext);
113+
cl_context CLContext = hCommandBuffer->hContext->CLContext;
106114
cl_ext::clFinalizeCommandBufferKHR_fn clFinalizeCommandBufferKHR = nullptr;
107115
UR_RETURN_ON_FAILURE(
108116
cl_ext::getExtFuncFromContext<decltype(clFinalizeCommandBufferKHR)>(
@@ -134,7 +142,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
134142
UR_ASSERT(!(phCommandHandle && !hCommandBuffer->IsUpdatable),
135143
UR_RESULT_ERROR_INVALID_OPERATION);
136144

137-
cl_context CLContext = cl_adapter::cast<cl_context>(hCommandBuffer->hContext);
145+
cl_context CLContext = hCommandBuffer->hContext->CLContext;
138146
cl_ext::clCommandNDRangeKernelKHR_fn clCommandNDRangeKernelKHR = nullptr;
139147
UR_RETURN_ON_FAILURE(
140148
cl_ext::getExtFuncFromContext<decltype(clCommandNDRangeKernelKHR)>(
@@ -162,10 +170,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
162170
IsInOrder ? nullptr : pSyncPointWaitList;
163171
uint32_t WaitListSize = IsInOrder ? 0 : numSyncPointsInWaitList;
164172
CL_RETURN_ON_FAILURE(clCommandNDRangeKernelKHR(
165-
hCommandBuffer->CLCommandBuffer, nullptr, Properties,
166-
cl_adapter::cast<cl_kernel>(hKernel), workDim, pGlobalWorkOffset,
167-
pGlobalWorkSize, pLocalWorkSize, WaitListSize, SyncPointWaitList,
168-
RetSyncPoint, OutCommandHandle));
173+
hCommandBuffer->CLCommandBuffer, nullptr, Properties, hKernel->CLKernel,
174+
workDim, pGlobalWorkOffset, pGlobalWorkSize, pLocalWorkSize, WaitListSize,
175+
SyncPointWaitList, RetSyncPoint, OutCommandHandle));
169176

170177
try {
171178
auto Handle = std::make_unique<ur_exp_command_buffer_command_handle_t_>(
@@ -225,7 +232,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendMemBufferCopyExp(
225232
(void)phEventWaitList;
226233
(void)phEvent;
227234
(void)phCommand;
228-
cl_context CLContext = cl_adapter::cast<cl_context>(hCommandBuffer->hContext);
235+
cl_context CLContext = hCommandBuffer->hContext->CLContext;
229236
cl_ext::clCommandCopyBufferKHR_fn clCommandCopyBufferKHR = nullptr;
230237
UR_RETURN_ON_FAILURE(
231238
cl_ext::getExtFuncFromContext<decltype(clCommandCopyBufferKHR)>(
@@ -238,10 +245,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendMemBufferCopyExp(
238245
IsInOrder ? nullptr : pSyncPointWaitList;
239246
uint32_t WaitListSize = IsInOrder ? 0 : numSyncPointsInWaitList;
240247
CL_RETURN_ON_FAILURE(clCommandCopyBufferKHR(
241-
hCommandBuffer->CLCommandBuffer, nullptr, nullptr,
242-
cl_adapter::cast<cl_mem>(hSrcMem), cl_adapter::cast<cl_mem>(hDstMem),
243-
srcOffset, dstOffset, size, WaitListSize, SyncPointWaitList, RetSyncPoint,
244-
nullptr));
248+
hCommandBuffer->CLCommandBuffer, nullptr, nullptr, hSrcMem->CLMemory,
249+
hDstMem->CLMemory, srcOffset, dstOffset, size, WaitListSize,
250+
SyncPointWaitList, RetSyncPoint, nullptr));
245251

246252
return UR_RESULT_SUCCESS;
247253
}
@@ -268,7 +274,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendMemBufferCopyRectExp(
268274
size_t OpenCLDstRect[3]{dstOrigin.x, dstOrigin.y, dstOrigin.z};
269275
size_t OpenCLRegion[3]{region.width, region.height, region.depth};
270276

271-
cl_context CLContext = cl_adapter::cast<cl_context>(hCommandBuffer->hContext);
277+
cl_context CLContext = hCommandBuffer->hContext->CLContext;
272278
cl_ext::clCommandCopyBufferRectKHR_fn clCommandCopyBufferRectKHR = nullptr;
273279
UR_RETURN_ON_FAILURE(
274280
cl_ext::getExtFuncFromContext<decltype(clCommandCopyBufferRectKHR)>(
@@ -281,11 +287,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendMemBufferCopyRectExp(
281287
IsInOrder ? nullptr : pSyncPointWaitList;
282288
uint32_t WaitListSize = IsInOrder ? 0 : numSyncPointsInWaitList;
283289
CL_RETURN_ON_FAILURE(clCommandCopyBufferRectKHR(
284-
hCommandBuffer->CLCommandBuffer, nullptr, nullptr,
285-
cl_adapter::cast<cl_mem>(hSrcMem), cl_adapter::cast<cl_mem>(hDstMem),
286-
OpenCLOriginRect, OpenCLDstRect, OpenCLRegion, srcRowPitch, srcSlicePitch,
287-
dstRowPitch, dstSlicePitch, WaitListSize, SyncPointWaitList, RetSyncPoint,
288-
nullptr));
290+
hCommandBuffer->CLCommandBuffer, nullptr, nullptr, hSrcMem->CLMemory,
291+
hDstMem->CLMemory, OpenCLOriginRect, OpenCLDstRect, OpenCLRegion,
292+
srcRowPitch, srcSlicePitch, dstRowPitch, dstSlicePitch, WaitListSize,
293+
SyncPointWaitList, RetSyncPoint, nullptr));
289294

290295
return UR_RESULT_SUCCESS;
291296
}
@@ -377,7 +382,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendMemBufferFillExp(
377382
[[maybe_unused]] ur_event_handle_t *phEvent,
378383
[[maybe_unused]] ur_exp_command_buffer_command_handle_t *phCommand) {
379384

380-
cl_context CLContext = cl_adapter::cast<cl_context>(hCommandBuffer->hContext);
385+
cl_context CLContext = hCommandBuffer->hContext->CLContext;
381386
cl_ext::clCommandFillBufferKHR_fn clCommandFillBufferKHR = nullptr;
382387
UR_RETURN_ON_FAILURE(
383388
cl_ext::getExtFuncFromContext<decltype(clCommandFillBufferKHR)>(
@@ -390,9 +395,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendMemBufferFillExp(
390395
IsInOrder ? nullptr : pSyncPointWaitList;
391396
uint32_t WaitListSize = IsInOrder ? 0 : numSyncPointsInWaitList;
392397
CL_RETURN_ON_FAILURE(clCommandFillBufferKHR(
393-
hCommandBuffer->CLCommandBuffer, nullptr, nullptr,
394-
cl_adapter::cast<cl_mem>(hBuffer), pPattern, patternSize, offset, size,
395-
WaitListSize, SyncPointWaitList, RetSyncPoint, nullptr));
398+
hCommandBuffer->CLCommandBuffer, nullptr, nullptr, hBuffer->CLMemory,
399+
pPattern, patternSize, offset, size, WaitListSize, SyncPointWaitList,
400+
RetSyncPoint, nullptr));
396401

397402
return UR_RESULT_SUCCESS;
398403
}
@@ -448,21 +453,34 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferEnqueueExp(
448453
uint32_t numEventsInWaitList, const ur_event_handle_t *phEventWaitList,
449454
ur_event_handle_t *phEvent) {
450455

451-
cl_context CLContext = cl_adapter::cast<cl_context>(hCommandBuffer->hContext);
456+
cl_context CLContext = hCommandBuffer->hContext->CLContext;
452457
cl_ext::clEnqueueCommandBufferKHR_fn clEnqueueCommandBufferKHR = nullptr;
453458
UR_RETURN_ON_FAILURE(
454459
cl_ext::getExtFuncFromContext<decltype(clEnqueueCommandBufferKHR)>(
455460
CLContext, cl_ext::ExtFuncPtrCache->clEnqueueCommandBufferKHRCache,
456461
cl_ext::EnqueueCommandBufferName, &clEnqueueCommandBufferKHR));
457462

458463
const uint32_t NumberOfQueues = 1;
459-
464+
cl_event Event;
465+
std::vector<cl_event> CLWaitEvents(numEventsInWaitList);
466+
for (uint32_t i = 0; i < numEventsInWaitList; i++) {
467+
CLWaitEvents[i] = phEventWaitList[i]->CLEvent;
468+
}
469+
cl_command_queue CLQueue = hQueue->CLQueue;
460470
CL_RETURN_ON_FAILURE(clEnqueueCommandBufferKHR(
461-
NumberOfQueues, cl_adapter::cast<cl_command_queue *>(&hQueue),
462-
hCommandBuffer->CLCommandBuffer, numEventsInWaitList,
463-
cl_adapter::cast<const cl_event *>(phEventWaitList),
464-
cl_adapter::cast<cl_event *>(phEvent)));
465-
471+
NumberOfQueues, &CLQueue, hCommandBuffer->CLCommandBuffer,
472+
numEventsInWaitList, CLWaitEvents.data(), &Event));
473+
if (phEvent) {
474+
try {
475+
auto UREvent =
476+
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
477+
*phEvent = UREvent.release();
478+
} catch (std::bad_alloc &) {
479+
return UR_RESULT_ERROR_OUT_OF_RESOURCES;
480+
} catch (...) {
481+
return UR_RESULT_ERROR_UNKNOWN;
482+
}
483+
}
466484
return UR_RESULT_SUCCESS;
467485
}
468486

@@ -502,11 +520,11 @@ void updateKernelArgs(std::vector<cl_mutable_dispatch_arg_khr> &CLArgs,
502520
for (uint32_t i = 0; i < NumMemobjArgs; i++) {
503521
const ur_exp_command_buffer_update_memobj_arg_desc_t &URMemObjArg =
504522
ArgMemobjList[i];
523+
cl_mem arg_value = URMemObjArg.hNewMemObjArg->CLMemory;
505524
cl_mutable_dispatch_arg_khr CLArg{
506525
URMemObjArg.argIndex, // arg_index
507526
sizeof(cl_mem), // arg_size
508-
cl_adapter::cast<const cl_mem *>(
509-
&URMemObjArg.hNewMemObjArg) // arg_value
527+
&arg_value // arg_value
510528
};
511529

512530
CLArgs.push_back(CLArg);
@@ -541,7 +559,7 @@ ur_result_t validateCommandDesc(
541559
// Verify that the device supports updating the aspects of the kernel that
542560
// the user is requesting.
543561
ur_device_handle_t URDevice = Command->hCommandBuffer->hDevice;
544-
cl_device_id CLDevice = cl_adapter::cast<cl_device_id>(URDevice);
562+
cl_device_id CLDevice = URDevice->CLDevice;
545563

546564
ur_device_command_buffer_update_capability_flags_t UpdateCapabilities = 0;
547565
CL_RETURN_ON_FAILURE(
@@ -592,7 +610,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferUpdateKernelLaunchExp(
592610
UR_RETURN_ON_FAILURE(validateCommandDesc(hCommand, pUpdateKernelLaunch));
593611

594612
ur_exp_command_buffer_handle_t hCommandBuffer = hCommand->hCommandBuffer;
595-
cl_context CLContext = cl_adapter::cast<cl_context>(hCommandBuffer->hContext);
613+
cl_context CLContext = hCommandBuffer->hContext->CLContext;
596614

597615
cl_ext::clUpdateMutableCommandsKHR_fn clUpdateMutableCommandsKHR = nullptr;
598616
UR_RETURN_ON_FAILURE(
@@ -636,8 +654,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferUpdateKernelLaunchExp(
636654
updateNDRange(CLLocalWorkSize, LocalWorkSizePtr);
637655
}
638656

639-
cl_mutable_command_khr command =
640-
cl_adapter::cast<cl_mutable_command_khr>(hCommand->CLMutableCommand);
657+
cl_mutable_command_khr command = hCommand->CLMutableCommand;
641658
cl_mutable_dispatch_config_khr dispatch_config = {
642659
command,
643660
static_cast<cl_uint>(CLArgs.size()), // num_args

unified-runtime/source/adapters/opencl/common.hpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -158,20 +158,6 @@ extern thread_local char ErrorMessage[MaxMessageSize];
158158
ur_result_t ErrorCode);
159159

160160
[[noreturn]] void die(const char *Message);
161-
162-
template <class To, class From> To cast(From Value) {
163-
164-
if constexpr (std::is_pointer_v<From>) {
165-
static_assert(std::is_pointer_v<From> == std::is_pointer_v<To>,
166-
"Cast failed pointer check");
167-
return reinterpret_cast<To>(Value);
168-
} else {
169-
static_assert(sizeof(From) == sizeof(To), "Cast failed size check");
170-
static_assert(std::is_signed_v<From> == std::is_signed_v<To>,
171-
"Cast failed sign check");
172-
return static_cast<To>(Value);
173-
}
174-
}
175161
} // namespace cl_adapter
176162

177163
namespace cl_ext {

0 commit comments

Comments
 (0)