Skip to content

Commit 20d9346

Browse files
author
sergei
authored
[SYCL] Add thread safety to host-task submit to in-order queue (#4395)
Signed-off-by: Sergey Kanaev <[email protected]>
1 parent de2179b commit 20d9346

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

sycl/source/detail/queue_impl.hpp

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,23 @@ class queue_impl {
419419
}
420420

421421
private:
422+
void finalizeHandler(handler &Handler, bool NeedSeparateDependencyMgmt,
423+
event &EventRet) {
424+
if (MIsInorder) {
425+
// Accessing and changing of an event isn't atomic operation.
426+
// Hence, here is the lock for thread-safety.
427+
std::lock_guard<std::mutex> Lock{MLastEventMtx};
428+
429+
if (NeedSeparateDependencyMgmt)
430+
Handler.depends_on(MLastEvent);
431+
432+
EventRet = Handler.finalize();
433+
434+
MLastEvent = EventRet;
435+
} else
436+
EventRet = Handler.finalize();
437+
}
438+
422439
/// Performs command group submission to the queue.
423440
///
424441
/// \param CGF is a function object containing command group.
@@ -437,9 +454,9 @@ class queue_impl {
437454
// Host and interop tasks, however, are not submitted to low-level runtimes
438455
// and require separate dependency management.
439456
const CG::CGTYPE Type = Handler.getType();
440-
if (MIsInorder && (Type == CG::CGTYPE::CodeplayHostTask ||
441-
Type == CG::CGTYPE::CodeplayInteropTask))
442-
Handler.depends_on(MLastEvent);
457+
bool NeedSeparateDependencyMgmt =
458+
MIsInorder && (Type == CG::CGTYPE::CodeplayHostTask ||
459+
Type == CG::CGTYPE::CodeplayInteropTask);
443460

444461
event Event;
445462

@@ -452,14 +469,11 @@ class queue_impl {
452469
: ProgramManager::getInstance().kernelUsesAssert(
453470
Handler.MOSModuleHandle, Handler.MKernelName);
454471

455-
Event = Handler.finalize();
472+
finalizeHandler(Handler, NeedSeparateDependencyMgmt, Event);
456473

457474
(*PostProcess)(IsKernel, KernelUsesAssert, Event);
458475
} else
459-
Event = Handler.finalize();
460-
461-
if (MIsInorder)
462-
MLastEvent = Event;
476+
finalizeHandler(Handler, NeedSeparateDependencyMgmt, Event);
463477

464478
addEvent(Event);
465479
return Event;
@@ -521,7 +535,11 @@ class queue_impl {
521535
// Buffer to store assert failure descriptor
522536
buffer<AssertHappened, 1> MAssertHappenedBuffer;
523537

538+
// This event is employed for enhanced dependency tracking with in-order queue
539+
// Access to the event should be guarded with MLastEventMtx
524540
event MLastEvent;
541+
std::mutex MLastEventMtx;
542+
525543
const bool MIsInorder;
526544
};
527545

0 commit comments

Comments
 (0)