Skip to content

Commit 451ecfb

Browse files
authored
[SYCL] Profile host events only if the submitted queue has profiling enabled (#18982)
Currently profiling for host tasks is always enabled. Track profiling info only if the submitted queue (where host task is submitted) has profiling enabled. This is a bug, according to SYCL specification 4.6.6. Event class: "Calls to get_profiling_info must throw an exception with the errc::invalid error code if the SYCL queue which submitted the [command group](https://registry.khronos.org/SYCL/specs/sycl-2020/html/sycl-2020.html#command-group) this SYCL event is associated with was not constructed with the property::queue::enable_profiling property." This can be considered as performance bug as well.
1 parent 2799ba3 commit 451ecfb

File tree

4 files changed

+61
-30
lines changed

4 files changed

+61
-30
lines changed

sycl/source/detail/event_impl.cpp

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -186,23 +186,9 @@ event_impl::event_impl(queue_impl &Queue, private_tag)
186186
}
187187

188188
event_impl::event_impl(HostEventState State, private_tag) : MState(State) {
189-
switch (State) {
190-
case HES_Discarded:
191-
case HES_Complete: {
189+
MIsHostEvent = true;
190+
if (State == HES_Discarded || State == HES_Complete)
192191
MIsFlushed = true;
193-
MIsHostEvent = true;
194-
break;
195-
}
196-
case HES_NotComplete: {
197-
MIsProfilingEnabled = true;
198-
MHostProfilingInfo.reset(new HostProfilingInfo());
199-
if (!MHostProfilingInfo)
200-
throw sycl::exception(
201-
sycl::make_error_code(sycl::errc::runtime),
202-
"Out of host memory " +
203-
codeToString(UR_RESULT_ERROR_OUT_OF_HOST_MEMORY));
204-
}
205-
}
206192
}
207193

208194
void event_impl::setQueue(queue_impl &Queue) {
@@ -215,14 +201,23 @@ void event_impl::setQueue(queue_impl &Queue) {
215201
MIsDefaultConstructed = false;
216202
}
217203

204+
void event_impl::initHostProfilingInfo() {
205+
assert(isHost() && "This must be a host event");
206+
assert(MState == HES_NotComplete &&
207+
"Host event must be incomplete to initialize profiling info");
208+
209+
std::shared_ptr<queue_impl> QueuePtr = MSubmittedQueue.lock();
210+
assert(QueuePtr && "Queue must be valid to initialize host profiling info");
211+
assert(QueuePtr->MIsProfilingEnabled && "Queue must have profiling enabled");
212+
213+
MIsProfilingEnabled = true;
214+
MHostProfilingInfo = std::make_unique<HostProfilingInfo>();
215+
device_impl &Device = QueuePtr->getDeviceImpl();
216+
MHostProfilingInfo->setDevice(&Device);
217+
}
218+
218219
void event_impl::setSubmittedQueue(std::weak_ptr<queue_impl> SubmittedQueue) {
219220
MSubmittedQueue = std::move(SubmittedQueue);
220-
if (MHostProfilingInfo) {
221-
if (std::shared_ptr<queue_impl> QueuePtr = MSubmittedQueue.lock()) {
222-
device_impl &Device = QueuePtr->getDeviceImpl();
223-
MHostProfilingInfo->setDevice(&Device);
224-
}
225-
}
226221
}
227222

228223
void *event_impl::instrumentationProlog(std::string &Name, int32_t StreamID,
@@ -621,12 +616,7 @@ bool event_impl::isCompleted() {
621616
info::event_command_status::complete;
622617
}
623618

624-
void event_impl::setCommand(void *Cmd) {
625-
MCommand = Cmd;
626-
auto TypedCommand = static_cast<Command *>(Cmd);
627-
if (TypedCommand)
628-
MIsHostEvent = TypedCommand->getWorkerContext() == nullptr;
629-
}
619+
void event_impl::setCommand(void *Cmd) { MCommand = Cmd; }
630620

631621
} // namespace detail
632622
} // namespace _V1

sycl/source/detail/event_impl.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,9 @@ class event_impl : public std::enable_shared_from_this<event_impl> {
378378
return MEvent && MQueue.expired() && !MIsEnqueued && !MCommand;
379379
}
380380

381+
// Initializes the host profiling info for the event.
382+
void initHostProfilingInfo();
383+
381384
protected:
382385
// When instrumentation is enabled emits trace event for event wait begin and
383386
// returns the telemetry event generated for the wait

sycl/source/detail/scheduler/commands.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,8 +1954,15 @@ ExecCGCommand::ExecCGCommand(
19541954
: Command(CommandType::RUN_CG, Queue, CommandBuffer, Dependencies),
19551955
MEventNeeded(EventNeeded), MCommandGroup(std::move(CommandGroup)) {
19561956
if (MCommandGroup->getType() == detail::CGType::CodeplayHostTask) {
1957-
MEvent->setSubmittedQueue(
1958-
static_cast<detail::CGHostTask *>(MCommandGroup.get())->MQueue);
1957+
queue_impl *SubmitQueue =
1958+
static_cast<detail::CGHostTask *>(MCommandGroup.get())->MQueue.get();
1959+
assert(SubmitQueue &&
1960+
"Host task command group must have a valid submit queue");
1961+
1962+
MEvent->setSubmittedQueue(SubmitQueue->weak_from_this());
1963+
// Initialize host profiling info if the queue has profiling enabled.
1964+
if (SubmitQueue->MIsProfilingEnabled)
1965+
MEvent->initHostProfilingInfo();
19591966
}
19601967
if (MCommandGroup->getType() == detail::CGType::ProfilingTag)
19611968
MEvent->markAsProfilingTagEvent();

sycl/unittests/queue/GetProfilingInfo.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,3 +345,34 @@ TEST(GetProfilingInfo, check_command_submission_time_with_host_accessor) {
345345

346346
EXPECT_TRUE(DeviceTimerCalled);
347347
}
348+
349+
// Check that query fails for host task if queue doesn't have profiling
350+
// enabled.
351+
TEST(GetProfilingInfo, check_host_task_profiling_info) {
352+
using namespace sycl;
353+
[[maybe_unused]] unittest::UrMock<> Mock;
354+
queue Queue;
355+
event E = Queue.submit([&](handler &cgh) { cgh.host_task([]() {}); });
356+
357+
auto expect_profiling_exception = [&](auto profiling_query) {
358+
try {
359+
std::ignore = profiling_query();
360+
FAIL();
361+
} catch (sycl::exception const &e) {
362+
EXPECT_STREQ(
363+
e.what(),
364+
"Profiling information is unavailable as the queue associated "
365+
"with the event does not have the 'enable_profiling' property.");
366+
}
367+
};
368+
369+
expect_profiling_exception([&] {
370+
return E.get_profiling_info<info::event_profiling::command_submit>();
371+
});
372+
expect_profiling_exception([&] {
373+
return E.get_profiling_info<info::event_profiling::command_start>();
374+
});
375+
expect_profiling_exception([&] {
376+
return E.get_profiling_info<info::event_profiling::command_end>();
377+
});
378+
}

0 commit comments

Comments
 (0)