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

[SYCL] Fix the barrier dependency for OOO profiling tags #16112

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions sycl/include/sycl/ext/oneapi/experimental/profiling_tag.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ inline event submit_profiling_tag(queue &Queue,
const sycl::detail::code_location &CodeLoc =
sycl::detail::code_location::current()) {
if (Queue.get_device().has(aspect::ext_oneapi_queue_profiling_tag)) {
// If the queue is out-of-order and profiling is enabled, the implementation
// can save some operations by just using the required barrier event
// directly.
if (!Queue.is_in_order() &&
Queue.has_property<sycl::property::queue::enable_profiling>())
return Queue.ext_oneapi_submit_barrier();

// Otherwise, we use the internal implementation of the profiling tag.
return Queue.submit(
[=](handler &CGH) {
sycl::detail::HandlerAccess::internalProfilingTagImpl(CGH);
Expand Down
38 changes: 30 additions & 8 deletions sycl/source/detail/scheduler/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3441,25 +3441,47 @@ ur_result_t ExecCGCommand::enqueueImpQueue() {
case CGType::ProfilingTag: {
assert(MQueue && "Profiling tag requires a valid queue");
const auto &Adapter = MQueue->getAdapter();

bool IsInOrderQueue = MQueue->isInOrder();
ur_event_handle_t *TimestampDeps = nullptr;
size_t NumTimestampDeps = 0;

// If the queue is not in-order, the implementation will need to first
// insert a marker event that the timestamp waits for.
// Note that with newer versions, MQueue should never have event profiling
// enabled here, as an optimization path in the headers will simply return
// the event from a barrier.
ur_event_handle_t PreTimestampMarkerEvent{};
if (!IsInOrderQueue) {
Adapter->call<UrApiKind::urEnqueueEventsWait>(
MQueue->getHandleRef(),
/*num_events_in_wait_list=*/0,
/*event_wait_list=*/nullptr, &PreTimestampMarkerEvent);
TimestampDeps = &PreTimestampMarkerEvent;
NumTimestampDeps = 1;
}

Adapter->call<UrApiKind::urEnqueueTimestampRecordingExp>(
MQueue->getHandleRef(),
/*blocking=*/false, NumTimestampDeps, TimestampDeps, Event);

// If the queue is not in-order, we need to insert a barrier. This barrier
// does not need output events as it will implicitly enforce the following
// enqueue is blocked until it finishes.
if (!MQueue->isInOrder()) {
if (!IsInOrderQueue) {
// We also need to release the timestamp event from the marker.
Adapter->call<UrApiKind::urEventRelease>(PreTimestampMarkerEvent);
// FIXME: Due to a bug in the L0 UR adapter, we will leak events if we do
// not pass an output event to the UR call. Once that is fixed,
// this immediately-deleted event can be removed.
ur_event_handle_t PreTimestampBarrierEvent{};
ur_event_handle_t PostTimestampBarrierEvent{};
Adapter->call<UrApiKind::urEnqueueEventsWaitWithBarrier>(
MQueue->getHandleRef(),
/*num_events_in_wait_list=*/0,
/*event_wait_list=*/nullptr, &PreTimestampBarrierEvent);
Adapter->call<UrApiKind::urEventRelease>(PreTimestampBarrierEvent);
/*event_wait_list=*/nullptr, &PostTimestampBarrierEvent);
Adapter->call<UrApiKind::urEventRelease>(PostTimestampBarrierEvent);
}

Adapter->call<UrApiKind::urEnqueueTimestampRecordingExp>(
MQueue->getHandleRef(),
/*blocking=*/false,
/*num_events_in_wait_list=*/0, /*event_wait_list=*/nullptr, Event);
if (Event)
MEvent->setHandle(*Event);
return UR_RESULT_SUCCESS;
Expand Down
70 changes: 70 additions & 0 deletions sycl/unittests/Extensions/ProfilingTag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ TEST_F(ProfilingTagTest, ProfilingTagSupportedDefaultQueue) {
"urEnqueueTimestampRecordingExp", &after_urEnqueueTimestampRecordingExp);
mock::getCallbacks().set_after_callback("urEventGetProfilingInfo",
&after_urEventGetProfilingInfo);
mock::getCallbacks().set_after_callback(
"urEnqueueEventsWaitWithBarrier", &after_urEnqueueEventsWaitWithBarrier);

sycl::context Ctx{sycl::platform()};
sycl::queue Queue{Ctx, sycl::default_selector_v};
Expand All @@ -75,6 +77,37 @@ TEST_F(ProfilingTagTest, ProfilingTagSupportedDefaultQueue) {

sycl::event E = sycl::ext::oneapi::experimental::submit_profiling_tag(Queue);
ASSERT_EQ(size_t{1}, counter_urEnqueueTimestampRecordingExp);
ASSERT_EQ(size_t{1}, counter_urEnqueueEventsWaitWithBarrier);

E.get_profiling_info<sycl::info::event_profiling::command_start>();
ASSERT_TRUE(LatestProfilingQuery.has_value());
ASSERT_EQ(*LatestProfilingQuery, UR_PROFILING_INFO_COMMAND_START);

E.get_profiling_info<sycl::info::event_profiling::command_end>();
ASSERT_TRUE(LatestProfilingQuery.has_value());
ASSERT_EQ(*LatestProfilingQuery, UR_PROFILING_INFO_COMMAND_END);
}

TEST_F(ProfilingTagTest, ProfilingTagSupportedInOrderQueue) {
mock::getCallbacks().set_after_callback("urDeviceGetInfo",
&after_urDeviceGetInfo<true>);
mock::getCallbacks().set_after_callback(
"urEnqueueTimestampRecordingExp", &after_urEnqueueTimestampRecordingExp);
mock::getCallbacks().set_after_callback("urEventGetProfilingInfo",
&after_urEventGetProfilingInfo);
mock::getCallbacks().set_after_callback(
"urEnqueueEventsWaitWithBarrier", &after_urEnqueueEventsWaitWithBarrier);

sycl::context Ctx{sycl::platform()};
sycl::queue Queue{
Ctx, sycl::default_selector_v, {sycl::property::queue::in_order()}};
sycl::device Dev = Queue.get_device();

ASSERT_TRUE(Dev.has(sycl::aspect::ext_oneapi_queue_profiling_tag));

sycl::event E = sycl::ext::oneapi::experimental::submit_profiling_tag(Queue);
ASSERT_EQ(size_t{1}, counter_urEnqueueTimestampRecordingExp);
ASSERT_EQ(size_t{0}, counter_urEnqueueEventsWaitWithBarrier);

E.get_profiling_info<sycl::info::event_profiling::command_start>();
ASSERT_TRUE(LatestProfilingQuery.has_value());
Expand All @@ -92,6 +125,8 @@ TEST_F(ProfilingTagTest, ProfilingTagSupportedProfilingQueue) {
"urEnqueueTimestampRecordingExp", &after_urEnqueueTimestampRecordingExp);
mock::getCallbacks().set_after_callback("urEventGetProfilingInfo",
&after_urEventGetProfilingInfo);
mock::getCallbacks().set_after_callback(
"urEnqueueEventsWaitWithBarrier", &after_urEnqueueEventsWaitWithBarrier);

sycl::context Ctx{sycl::platform()};
sycl::queue Queue{Ctx,
Expand All @@ -101,8 +136,43 @@ TEST_F(ProfilingTagTest, ProfilingTagSupportedProfilingQueue) {

ASSERT_TRUE(Dev.has(sycl::aspect::ext_oneapi_queue_profiling_tag));

// As an optimization, the implementation will use a single barrier when
// submitting a profiling tag on an out-of-order queue with profiling enabled.
sycl::event E = sycl::ext::oneapi::experimental::submit_profiling_tag(Queue);
ASSERT_EQ(size_t{0}, counter_urEnqueueTimestampRecordingExp);
ASSERT_EQ(size_t{1}, counter_urEnqueueEventsWaitWithBarrier);

E.get_profiling_info<sycl::info::event_profiling::command_start>();
ASSERT_TRUE(LatestProfilingQuery.has_value());
ASSERT_EQ(*LatestProfilingQuery, UR_PROFILING_INFO_COMMAND_START);

E.get_profiling_info<sycl::info::event_profiling::command_end>();
ASSERT_TRUE(LatestProfilingQuery.has_value());
ASSERT_EQ(*LatestProfilingQuery, UR_PROFILING_INFO_COMMAND_END);
}

TEST_F(ProfilingTagTest, ProfilingTagSupportedProfilingInOrderQueue) {
mock::getCallbacks().set_after_callback("urDeviceGetInfo",
&after_urDeviceGetInfo<true>);
mock::getCallbacks().set_after_callback(
"urEnqueueTimestampRecordingExp", &after_urEnqueueTimestampRecordingExp);
mock::getCallbacks().set_after_callback("urEventGetProfilingInfo",
&after_urEventGetProfilingInfo);
mock::getCallbacks().set_after_callback(
"urEnqueueEventsWaitWithBarrier", &after_urEnqueueEventsWaitWithBarrier);

sycl::context Ctx{sycl::platform()};
sycl::queue Queue{Ctx,
sycl::default_selector_v,
{sycl::property::queue::enable_profiling(),
sycl::property::queue::in_order()}};
sycl::device Dev = Queue.get_device();

ASSERT_TRUE(Dev.has(sycl::aspect::ext_oneapi_queue_profiling_tag));

sycl::event E = sycl::ext::oneapi::experimental::submit_profiling_tag(Queue);
ASSERT_EQ(size_t{1}, counter_urEnqueueTimestampRecordingExp);
ASSERT_EQ(size_t{0}, counter_urEnqueueEventsWaitWithBarrier);

E.get_profiling_info<sycl::info::event_profiling::command_start>();
ASSERT_TRUE(LatestProfilingQuery.has_value());
Expand Down
Loading