Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[L0][OpenCL] Emulate Fill with copy when patternSize is not a power of 2 #1412

Merged
merged 3 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 35 additions & 13 deletions source/adapters/level_zero/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <algorithm>
#include <climits>
#include <string.h>
#include <ur/ur.hpp>

#include "context.hpp"
#include "event.hpp"
Expand Down Expand Up @@ -183,9 +184,6 @@ static ur_result_t enqueueMemFillHelper(ur_command_t CommandType,
uint32_t NumEventsInWaitList,
const ur_event_handle_t *EventWaitList,
ur_event_handle_t *OutEvent) {
// Pattern size must be a power of two.
UR_ASSERT((PatternSize > 0) && ((PatternSize & (PatternSize - 1)) == 0),
UR_RESULT_ERROR_INVALID_VALUE);
auto &Device = Queue->Device;

// Make sure that pattern size matches the capability of the copy queues.
Expand Down Expand Up @@ -237,18 +235,42 @@ static ur_result_t enqueueMemFillHelper(ur_command_t CommandType,
const auto &ZeCommandList = CommandList->first;
const auto &WaitList = (*Event)->WaitList;

ZE2UR_CALL(zeCommandListAppendMemoryFill,
(ZeCommandList, Ptr, Pattern, PatternSize, Size, ZeEvent,
WaitList.Length, WaitList.ZeEventList));
// PatternSize must be a power of two for zeCommandListAppendMemoryFill.
// When it's not, the fill is emulated with zeCommandListAppendMemoryCopy.
if (isPowerOf2(PatternSize)) {
ZE2UR_CALL(zeCommandListAppendMemoryFill,
(ZeCommandList, Ptr, Pattern, PatternSize, Size, ZeEvent,
WaitList.Length, WaitList.ZeEventList));

logger::debug("calling zeCommandListAppendMemoryFill() with"
" ZeEvent {}",
ur_cast<uint64_t>(ZeEvent));
printZeEventList(WaitList);
logger::debug("calling zeCommandListAppendMemoryFill() with"
" ZeEvent {}",
ur_cast<uint64_t>(ZeEvent));
printZeEventList(WaitList);

// Execute command list asynchronously, as the event will be used
// to track down its completion.
UR_CALL(Queue->executeCommandList(CommandList, false, OkToBatch));
// Execute command list asynchronously, as the event will be used
// to track down its completion.
UR_CALL(Queue->executeCommandList(CommandList, false, OkToBatch));
} else {
// Copy pattern into every entry in memory array pointed by Ptr.
uint32_t NumOfCopySteps = Size / PatternSize;
const void *Src = Pattern;

for (uint32_t step = 0; step < NumOfCopySteps; ++step) {
void *Dst = reinterpret_cast<void *>(reinterpret_cast<uint8_t *>(Ptr) +
step * PatternSize);
ZE2UR_CALL(zeCommandListAppendMemoryCopy,
(ZeCommandList, Dst, Src, PatternSize, ZeEvent,
WaitList.Length, WaitList.ZeEventList));
}

logger::debug("calling zeCommandListAppendMemoryCopy() with"
" ZeEvent {}",
ur_cast<uint64_t>(ZeEvent));
printZeEventList(WaitList);

// Execute command list synchronously.
UR_CALL(Queue->executeCommandList(CommandList, true, OkToBatch));
}

return UR_RESULT_SUCCESS;
}
Expand Down
4 changes: 3 additions & 1 deletion source/adapters/opencl/usm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
//
//===----------------------------------------------------------------------===//

#include <ur/ur.hpp>

#include "common.hpp"

inline cl_mem_alloc_flags_intel
Expand Down Expand Up @@ -239,7 +241,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueUSMFill(
return mapCLErrorToUR(CLErr);
}

if (patternSize <= 128) {
if (patternSize <= 128 && isPowerOf2(patternSize)) {
clEnqueueMemFillINTEL_fn EnqueueMemFill = nullptr;
UR_RETURN_ON_FAILURE(
cl_ext::getExtFuncFromContext<clEnqueueMemFillINTEL_fn>(
Expand Down