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

[UR] [V2] Add wait before enqueue in command buffer #17709

Merged
merged 8 commits into from
Apr 2, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,28 @@ ur_result_t ur_exp_command_buffer_handle_t_::finalizeCommandBuffer() {
isFinalized = true;
return UR_RESULT_SUCCESS;
}
ur_event_handle_t ur_exp_command_buffer_handle_t_::getExecutionEventUnlocked() {
return currentExecution;
}

ur_result_t ur_exp_command_buffer_handle_t_::registerExecutionEventUnlocked(
ur_event_handle_t nextExecutionEvent) {
if (currentExecution) {
UR_CALL(currentExecution->release());
currentExecution = nullptr;
}
if (nextExecutionEvent) {
currentExecution = nextExecutionEvent;
UR_CALL(nextExecutionEvent->retain());
}
return UR_RESULT_SUCCESS;
}

ur_exp_command_buffer_handle_t_::~ur_exp_command_buffer_handle_t_() {
if (currentExecution) {
currentExecution->release();
}
}
namespace ur::level_zero {

ur_result_t
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ struct ur_exp_command_buffer_handle_t_ : public _ur_object {
v2::raii::command_list_unique_handle &&commandList,
const ur_exp_command_buffer_desc_t *desc);

~ur_exp_command_buffer_handle_t_() = default;
~ur_exp_command_buffer_handle_t_();

ur_event_handle_t getExecutionEventUnlocked();
ur_result_t
registerExecutionEventUnlocked(ur_event_handle_t nextExecutionEvent);

lockable<ur_command_list_manager> commandListManager;

Expand All @@ -36,6 +40,8 @@ struct ur_exp_command_buffer_handle_t_ : public _ur_object {
private:
// Indicates if command-buffer was finalized.
bool isFinalized = false;

ur_event_handle_t currentExecution = nullptr;
};

struct ur_exp_command_buffer_command_handle_t_ : public _ur_object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,20 @@ ur_result_t ur_command_list_manager::appendRegionCopyUnlocked(
return UR_RESULT_SUCCESS;
}

wait_list_view
ur_command_list_manager::getWaitListView(const ur_event_handle_t *phWaitEvents,
uint32_t numWaitEvents) {
wait_list_view ur_command_list_manager::getWaitListView(
const ur_event_handle_t *phWaitEvents, uint32_t numWaitEvents,
ur_event_handle_t additionalWaitEvent) {

waitList.resize(numWaitEvents);
uint32_t totalNumWaitEvents =
numWaitEvents + (additionalWaitEvent != nullptr ? 1 : 0);
waitList.resize(totalNumWaitEvents);
for (uint32_t i = 0; i < numWaitEvents; i++) {
waitList[i] = phWaitEvents[i]->getZeEvent();
}

return {waitList.data(), static_cast<uint32_t>(numWaitEvents)};
if (additionalWaitEvent != nullptr) {
waitList[totalNumWaitEvents - 1] = additionalWaitEvent->getZeEvent();
}
return {waitList.data(), static_cast<uint32_t>(totalNumWaitEvents)};
}

ze_event_handle_t
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ struct ur_command_list_manager {

ze_command_list_handle_t getZeCommandList();

wait_list_view getWaitListView(const ur_event_handle_t *phWaitEvents,
uint32_t numWaitEvents);
wait_list_view
getWaitListView(const ur_event_handle_t *phWaitEvents, uint32_t numWaitEvents,
ur_event_handle_t additionalWaitEvent = nullptr);
ze_event_handle_t getSignalEvent(ur_event_handle_t *hUserEvent,
ur_command_t commandType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ namespace v2 {

wait_list_view ur_queue_immediate_in_order_t::getWaitListView(
locked<ur_command_list_manager> &commandList,
const ur_event_handle_t *phWaitEvents, uint32_t numWaitEvents) {
return commandList->getWaitListView(phWaitEvents, numWaitEvents);
const ur_event_handle_t *phWaitEvents, uint32_t numWaitEvents,
ur_event_handle_t additionalWaitEvent) {
return commandList->getWaitListView(phWaitEvents, numWaitEvents,
additionalWaitEvent);
}

static int32_t getZeOrdinal(ur_device_handle_t hDevice) {
Expand Down Expand Up @@ -898,7 +900,8 @@ ur_result_t ur_queue_immediate_in_order_t::enqueueTimestampRecordingExp(
ur_result_t ur_queue_immediate_in_order_t::enqueueGenericCommandListsExp(
uint32_t numCommandLists, ze_command_list_handle_t *phCommandLists,
ur_event_handle_t *phEvent, uint32_t numEventsInWaitList,
const ur_event_handle_t *phEventWaitList, ur_command_t callerCommand) {
const ur_event_handle_t *phEventWaitList, ur_command_t callerCommand,
ur_event_handle_t additionalWaitEvent) {
TRACK_SCOPE_LATENCY(
"ur_queue_immediate_in_order_t::enqueueGenericCommandListsExp");

Expand All @@ -907,7 +910,8 @@ ur_result_t ur_queue_immediate_in_order_t::enqueueGenericCommandListsExp(
getSignalEvent(commandListLocked, phEvent, callerCommand);

auto [pWaitEvents, numWaitEvents] =
getWaitListView(commandListLocked, phEventWaitList, numEventsInWaitList);
getWaitListView(commandListLocked, phEventWaitList, numEventsInWaitList,
additionalWaitEvent);
// zeCommandListImmediateAppendCommandListsExp is not working with in-order
// immediate lists what causes problems with synchronization
// TODO: remove synchronization when it is not needed
Expand All @@ -928,9 +932,21 @@ ur_result_t ur_queue_immediate_in_order_t::enqueueCommandBufferExp(
auto commandListLocked = hCommandBuffer->commandListManager.lock();
ze_command_list_handle_t commandBufferCommandList =
commandListLocked->getZeCommandList();
return enqueueGenericCommandListsExp(1, &commandBufferCommandList, phEvent,
numEventsInWaitList, phEventWaitList,
UR_COMMAND_ENQUEUE_COMMAND_BUFFER_EXP);
ur_event_handle_t internalEvent = nullptr;
if (phEvent == nullptr) {
phEvent = &internalEvent;
}
ur_event_handle_t executionEvent =
hCommandBuffer->getExecutionEventUnlocked();

UR_CALL(enqueueGenericCommandListsExp(
1, &commandBufferCommandList, phEvent, numEventsInWaitList,
phEventWaitList, UR_COMMAND_ENQUEUE_COMMAND_BUFFER_EXP, executionEvent));
UR_CALL(hCommandBuffer->registerExecutionEventUnlocked(*phEvent));
if (internalEvent != nullptr) {
internalEvent->release();
}
return UR_RESULT_SUCCESS;
}

ur_result_t ur_queue_immediate_in_order_t::enqueueKernelLaunchCustomExp(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ struct ur_queue_immediate_in_order_t : _ur_object, public ur_queue_t_ {
std::vector<ur_event_handle_t> deferredEvents;
std::vector<ur_kernel_handle_t> submittedKernels;

wait_list_view getWaitListView(locked<ur_command_list_manager> &commandList,
const ur_event_handle_t *phWaitEvents,
uint32_t numWaitEvents);
wait_list_view
getWaitListView(locked<ur_command_list_manager> &commandList,
const ur_event_handle_t *phWaitEvents, uint32_t numWaitEvents,
ur_event_handle_t additionalWaitEvent = nullptr);

ze_event_handle_t getSignalEvent(locked<ur_command_list_manager> &commandList,
ur_event_handle_t *hUserEvent,
Expand All @@ -56,7 +57,8 @@ struct ur_queue_immediate_in_order_t : _ur_object, public ur_queue_t_ {
ur_result_t enqueueGenericCommandListsExp(
uint32_t numCommandLists, ze_command_list_handle_t *phCommandLists,
ur_event_handle_t *phEvent, uint32_t numEventsInWaitList,
const ur_event_handle_t *phEventWaitList, ur_command_t callerCommand);
const ur_event_handle_t *phEventWaitList, ur_command_t callerCommand,
ur_event_handle_t additionalWaitEvent);

ur_result_t
enqueueEventsWaitWithBarrierImpl(uint32_t numEventsInWaitList,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ static void checkCommandBufferUpdateSupport(

struct urCommandBufferExpTest : uur::urContextTest {
void SetUp() override {
UUR_KNOWN_FAILURE_ON(uur::LevelZeroV2{});

UUR_RETURN_ON_FATAL_FAILURE(uur::urContextTest::SetUp());

Expand All @@ -72,7 +71,6 @@ struct urCommandBufferExpTest : uur::urContextTest {
template <class T>
struct urCommandBufferExpTestWithParam : urQueueTestWithParam<T> {
void SetUp() override {
UUR_KNOWN_FAILURE_ON(uur::LevelZeroV2{});

UUR_RETURN_ON_FATAL_FAILURE(uur::urQueueTestWithParam<T>::SetUp());

Expand All @@ -97,7 +95,6 @@ struct urCommandBufferExpTestWithParam : urQueueTestWithParam<T> {

struct urCommandBufferExpExecutionTest : uur::urKernelExecutionTest {
void SetUp() override {
UUR_KNOWN_FAILURE_ON(uur::LevelZeroV2{});

UUR_RETURN_ON_FATAL_FAILURE(uur::urKernelExecutionTest::SetUp());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ TEST_P(InvalidUpdateTest, CommandBufferMismatch) {
// that isn't supported.
struct InvalidUpdateCommandBufferExpExecutionTest : uur::urKernelExecutionTest {
void SetUp() override {
UUR_KNOWN_FAILURE_ON(uur::LevelZeroV2{});

program_name = "fill_usm";
UUR_RETURN_ON_FATAL_FAILURE(uur::urKernelExecutionTest::SetUp());

Expand Down
Loading