Skip to content

Commit 26c0845

Browse files
Ewan Crawfordkbenzie
authored andcommitted
Map copy/fill to SVM (#18177)
There is currently no cl_khr_command_buffer or cl_intel_unified_shared_memory entry-point for appending USM copy or fill commands to a command-buffer. This prevents these commands from being added to a graph for the OpenCL backend. The long term solution to this is the OpenCL USVM extension which will align USM and SVM, allowing the existing SVM entry-points to be used. To prepare for this, map the UR entry-points to the cl_khr_command_buffer SVM copy/fill commands. This will work on OpenCL implementations that share an implementation for USM and SVM. Signed-off-by: Ewan Crawford <ewan@codeplay.com>
1 parent 34d625c commit 26c0845

5 files changed

Lines changed: 74 additions & 32 deletions

File tree

source/adapters/opencl/command_buffer.cpp

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -204,33 +204,59 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
204204
}
205205

206206
UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendUSMMemcpyExp(
207-
[[maybe_unused]] ur_exp_command_buffer_handle_t hCommandBuffer,
208-
[[maybe_unused]] void *pDst, [[maybe_unused]] const void *pSrc,
209-
[[maybe_unused]] size_t size,
210-
[[maybe_unused]] uint32_t numSyncPointsInWaitList,
211-
[[maybe_unused]] const ur_exp_command_buffer_sync_point_t
212-
*pSyncPointWaitList,
213-
[[maybe_unused]] uint32_t numEventsInWaitList,
214-
[[maybe_unused]] const ur_event_handle_t *phEventWaitList,
215-
[[maybe_unused]] ur_exp_command_buffer_sync_point_t *pSyncPoint,
216-
[[maybe_unused]] ur_event_handle_t *phEvent,
217-
[[maybe_unused]] ur_exp_command_buffer_command_handle_t *phCommand) {
218-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
207+
ur_exp_command_buffer_handle_t hCommandBuffer, void *pDst, const void *pSrc,
208+
size_t size, uint32_t numSyncPointsInWaitList,
209+
const ur_exp_command_buffer_sync_point_t *pSyncPointWaitList, uint32_t,
210+
const ur_event_handle_t *, ur_exp_command_buffer_sync_point_t *pSyncPoint,
211+
ur_event_handle_t *, ur_exp_command_buffer_command_handle_t *) {
212+
// No extension entry-point exists for USM memcpy, use SVM memcpy in
213+
// preparation for USVM.
214+
cl_context CLContext = hCommandBuffer->hContext->CLContext;
215+
cl_ext::clCommandSVMMemcpyKHR_fn clCommandSVMMemcpyKHR = nullptr;
216+
UR_RETURN_ON_FAILURE(
217+
cl_ext::getExtFuncFromContext<decltype(clCommandSVMMemcpyKHR)>(
218+
CLContext, ur::cl::getAdapter()->fnCache.clCommandSVMMemcpyKHRCache,
219+
cl_ext::CommandSVMMemcpyName, &clCommandSVMMemcpyKHR));
220+
221+
const bool IsInOrder = hCommandBuffer->IsInOrder;
222+
cl_sync_point_khr *RetSyncPoint = IsInOrder ? nullptr : pSyncPoint;
223+
const cl_sync_point_khr *SyncPointWaitList =
224+
IsInOrder ? nullptr : pSyncPointWaitList;
225+
uint32_t WaitListSize = IsInOrder ? 0 : numSyncPointsInWaitList;
226+
CL_RETURN_ON_FAILURE(clCommandSVMMemcpyKHR(
227+
hCommandBuffer->CLCommandBuffer, nullptr, nullptr, pDst, pSrc, size,
228+
WaitListSize, SyncPointWaitList, RetSyncPoint, nullptr));
229+
230+
return UR_RESULT_SUCCESS;
219231
}
220232

221233
UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendUSMFillExp(
222-
[[maybe_unused]] ur_exp_command_buffer_handle_t hCommandBuffer,
223-
[[maybe_unused]] void *pMemory, [[maybe_unused]] const void *pPattern,
224-
[[maybe_unused]] size_t patternSize, [[maybe_unused]] size_t size,
225-
[[maybe_unused]] uint32_t numSyncPointsInWaitList,
226-
[[maybe_unused]] const ur_exp_command_buffer_sync_point_t
227-
*pSyncPointWaitList,
228-
[[maybe_unused]] uint32_t numEventsInWaitList,
229-
[[maybe_unused]] const ur_event_handle_t *phEventWaitList,
230-
[[maybe_unused]] ur_exp_command_buffer_sync_point_t *pSyncPoint,
231-
[[maybe_unused]] ur_event_handle_t *phEvent,
232-
[[maybe_unused]] ur_exp_command_buffer_command_handle_t *phCommand) {
233-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
234+
ur_exp_command_buffer_handle_t hCommandBuffer, void *pMemory,
235+
const void *pPattern, size_t patternSize, size_t size,
236+
uint32_t numSyncPointsInWaitList,
237+
const ur_exp_command_buffer_sync_point_t *pSyncPointWaitList, uint32_t,
238+
const ur_event_handle_t *, ur_exp_command_buffer_sync_point_t *pSyncPoint,
239+
ur_event_handle_t *, ur_exp_command_buffer_command_handle_t *) {
240+
// No extension entry-point exists for USM fill, use SVM fill in preparation
241+
// for USVM.
242+
cl_context CLContext = hCommandBuffer->hContext->CLContext;
243+
cl_ext::clCommandSVMMemFillKHR_fn clCommandSVMMemFillKHR = nullptr;
244+
UR_RETURN_ON_FAILURE(
245+
cl_ext::getExtFuncFromContext<decltype(clCommandSVMMemFillKHR)>(
246+
CLContext, ur::cl::getAdapter()->fnCache.clCommandSVMMemFillKHRCache,
247+
cl_ext::CommandSVMMemFillName, &clCommandSVMMemFillKHR));
248+
249+
const bool IsInOrder = hCommandBuffer->IsInOrder;
250+
cl_sync_point_khr *RetSyncPoint = IsInOrder ? nullptr : pSyncPoint;
251+
const cl_sync_point_khr *SyncPointWaitList =
252+
IsInOrder ? nullptr : pSyncPointWaitList;
253+
uint32_t WaitListSize = IsInOrder ? 0 : numSyncPointsInWaitList;
254+
CL_RETURN_ON_FAILURE(
255+
clCommandSVMMemFillKHR(hCommandBuffer->CLCommandBuffer, nullptr, nullptr,
256+
pMemory, pPattern, patternSize, size, WaitListSize,
257+
SyncPointWaitList, RetSyncPoint, nullptr));
258+
259+
return UR_RESULT_SUCCESS;
234260
}
235261

236262
UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendMemBufferCopyExp(

source/adapters/opencl/common.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ CONSTFIX char CommandCopyBufferRectName[] = "clCommandCopyBufferRectKHR";
201201
CONSTFIX char CommandFillBufferName[] = "clCommandFillBufferKHR";
202202
CONSTFIX char CommandBarrierWithWaitListName[] =
203203
"clCommandBarrierWithWaitListKHR";
204+
CONSTFIX char CommandSVMMemcpyName[] = "clCommandSVMMemcpyKHR";
205+
CONSTFIX char CommandSVMMemFillName[] = "clCommandSVMMemFillKHR";
204206
CONSTFIX char EnqueueCommandBufferName[] = "clEnqueueCommandBufferKHR";
205207
CONSTFIX char GetCommandBufferInfoName[] = "clGetCommandBufferInfoKHR";
206208
CONSTFIX char UpdateMutableCommandsName[] = "clUpdateMutableCommandsKHR";
@@ -296,6 +298,21 @@ using clCommandBarrierWithWaitListKHR_fn = CL_API_ENTRY cl_int(CL_API_CALL *)(
296298
const cl_sync_point_khr *sync_point_wait_list,
297299
cl_sync_point_khr *sync_point, cl_mutable_command_khr *mutable_handle);
298300

301+
using clCommandSVMMemcpyKHR_fn = CL_API_ENTRY cl_int(CL_API_CALL *)(
302+
cl_command_buffer_khr command_buffer, cl_command_queue command_queue,
303+
const cl_command_properties_khr *properties, void *dst_ptr,
304+
const void *src_ptr, size_t size, cl_uint num_sync_points_in_wait_list,
305+
const cl_sync_point_khr *sync_point_wait_list,
306+
cl_sync_point_khr *sync_point, cl_mutable_command_khr *mutable_handle);
307+
308+
using clCommandSVMMemFillKHR_fn = CL_API_ENTRY cl_int(CL_API_CALL *)(
309+
cl_command_buffer_khr command_buffer, cl_command_queue command_queue,
310+
const cl_command_properties_khr *properties, void *svm_ptr,
311+
const void *pattern, size_t pattern_size, size_t size,
312+
cl_uint num_sync_points_in_wait_list,
313+
const cl_sync_point_khr *sync_point_wait_list,
314+
cl_sync_point_khr *sync_point, cl_mutable_command_khr *mutable_handle);
315+
299316
using clEnqueueCommandBufferKHR_fn = CL_API_ENTRY
300317
cl_int(CL_API_CALL *)(cl_uint num_queues, cl_command_queue *queues,
301318
cl_command_buffer_khr command_buffer,

source/adapters/opencl/extension_functions.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ CL_EXTENSION_FUNC(clCommandCopyBufferKHR)
2222
CL_EXTENSION_FUNC(clCommandCopyBufferRectKHR)
2323
CL_EXTENSION_FUNC(clCommandFillBufferKHR)
2424
CL_EXTENSION_FUNC(clCommandBarrierWithWaitListKHR)
25+
CL_EXTENSION_FUNC(clCommandSVMMemFillKHR)
26+
CL_EXTENSION_FUNC(clCommandSVMMemcpyKHR)
2527
CL_EXTENSION_FUNC(clEnqueueCommandBufferKHR)
2628
CL_EXTENSION_FUNC(clGetCommandBufferInfoKHR)
2729
CL_EXTENSION_FUNC(clUpdateMutableCommandsKHR)

test/conformance/exp_command_buffer/commands.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,12 @@ struct urCommandBufferCommandsTest
5656
UUR_INSTANTIATE_DEVICE_TEST_SUITE(urCommandBufferCommandsTest);
5757

5858
TEST_P(urCommandBufferCommandsTest, urCommandBufferAppendUSMMemcpyExp) {
59-
// No USM memcpy command in cl_khr_command_buffer
60-
UUR_KNOWN_FAILURE_ON(uur::OpenCL{});
61-
6259
ASSERT_SUCCESS(urCommandBufferAppendUSMMemcpyExp(
6360
cmd_buf_handle, device_ptrs[0], device_ptrs[1], allocation_size, 0,
6461
nullptr, 0, nullptr, nullptr, nullptr, nullptr));
6562
}
6663

6764
TEST_P(urCommandBufferCommandsTest, urCommandBufferAppendUSMFillExp) {
68-
// No USM fill command in cl_khr_command_buffer
69-
UUR_KNOWN_FAILURE_ON(uur::OpenCL{});
70-
7165
uint32_t pattern = 42;
7266
ASSERT_SUCCESS(urCommandBufferAppendUSMFillExp(
7367
cmd_buf_handle, device_ptrs[0], &pattern, sizeof(pattern),

test/conformance/exp_command_buffer/fill.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,11 @@ TEST_P(urCommandBufferFillCommandsTest, ExecuteTwice) {
155155
}
156156

157157
TEST_P(urCommandBufferFillCommandsTest, USM) {
158-
// No USM fill command in cl_khr_command_buffer
159-
UUR_KNOWN_FAILURE_ON(uur::OpenCL{});
158+
if (pattern_size > 128) {
159+
// clCommandSVMMemFillKHR returns an error according to the spec if pattern
160+
// size is not one of {1, 2, 4, 8, 16, 32, 64, 128}
161+
UUR_KNOWN_FAILURE_ON(uur::OpenCL{});
162+
}
160163

161164
ASSERT_SUCCESS(urCommandBufferAppendUSMFillExp(
162165
cmd_buf_handle, device_ptr, pattern.data(), pattern_size, size, 0,

0 commit comments

Comments
 (0)