diff --git a/sycl/source/detail/queue_impl.cpp b/sycl/source/detail/queue_impl.cpp index fbfadfc8c39e8..ac84c9491653e 100644 --- a/sycl/source/detail/queue_impl.cpp +++ b/sycl/source/detail/queue_impl.cpp @@ -615,11 +615,17 @@ EventImplPtr queue_impl::submit_barrier_direct_impl( /*SchedulerBypass*/ true}; } - if (EventForReuse) { + if (EventForReuse || !CallerNeedsEvent) { // Current limitation: reusable events require scheduler bypass so that // the barrier can be submitted directly to the backend with the reusable // event's handle as the output event. Scheduler bypass is not possible // when dependencies include host tasks or cross-context dependencies. + // + // This limitation applies to both: enqueue_signal_event and + // enqueue_wait_event(s). + // + // The !CallerNeedsEvent condition is used to detect the + // enqueue_wait_event(s) function calls. throw sycl::exception(sycl::make_error_code(errc::invalid), "An event cannot be enqueued for signaling behind " "a command which is not enqueued in the backend."); diff --git a/sycl/source/reusable_events.cpp b/sycl/source/reusable_events.cpp index 642edd4e298d8..c103ba0637312 100644 --- a/sycl/source/reusable_events.cpp +++ b/sycl/source/reusable_events.cpp @@ -60,6 +60,18 @@ __SYCL_EXPORT sycl::event make_event(const sycl::context &ctxt, __SYCL_EXPORT void enqueue_wait_event(sycl::queue q, const event &evt) { detail::queue_impl &QueueImpl = *sycl::detail::getSyclObjImpl(q); + detail::event_impl &EventImpl = *sycl::detail::getSyclObjImpl(evt); + + // Current limitation: + // The queue and an event need to be in the same context. The reason + // is, that cross-context dependencies use host tasks, and the wait + // command might be queued in the runtime. This flow is currently + // not supported by the Reusable Events APIs. + if (&QueueImpl.getContextImpl() != &EventImpl.getContextImpl()) { + throw sycl::exception( + sycl::make_error_code(errc::invalid), + "Not implemented yet. Event context must match the queue context."); + } QueueImpl.submit_barrier_direct_without_event( sycl::span(&evt, 1), detail::CGType::BarrierWaitlist, @@ -70,6 +82,21 @@ __SYCL_EXPORT void enqueue_wait_events(sycl::queue q, const std::vector &evts) { detail::queue_impl &QueueImpl = *sycl::detail::getSyclObjImpl(q); + // Current limitation: + // The queue and all the events need to be in the same context. The + // reason is, that cross-context dependencies use host tasks, and the + // wait command might be queued in the runtime. This flow is currently + // not supported by the Reusable Events APIs. + for (sycl::event evt : evts) { + detail::event_impl &EventImpl = *sycl::detail::getSyclObjImpl(evt); + if (&QueueImpl.getContextImpl() != &EventImpl.getContextImpl()) { + throw sycl::exception( + sycl::make_error_code(errc::invalid), + "Not implemented yet. Context of all events must match the " + "queue context."); + } + } + QueueImpl.submit_barrier_direct_without_event( evts, detail::CGType::BarrierWaitlist, detail::code_location::current()); } diff --git a/sycl/unittests/Extensions/ReusableEvents.cpp b/sycl/unittests/Extensions/ReusableEvents.cpp index f572f25e8fcba..828176517660d 100644 --- a/sycl/unittests/Extensions/ReusableEvents.cpp +++ b/sycl/unittests/Extensions/ReusableEvents.cpp @@ -443,7 +443,9 @@ TEST_F(ReusableEventsTest, EventInDependsOn) { Queue.wait(); } -// Cross-context events with wait +// Cross-context event with wait. +// Current limitation is that the cross-context wait +// is not supported. TEST_F(ReusableEventsTest, CrossContextEventWait) { mock::getCallbacks().set_replace_callback("urDeviceGet", &redefinedUrDeviceGet); @@ -467,23 +469,68 @@ TEST_F(ReusableEventsTest, CrossContextEventWait) { auto event = syclex::make_event(Ctx1); - mock::getCallbacks().set_replace_callback( - "urEnqueueEventsWaitWithBarrierExt", - &redefinedUrEnqueueEventsWaitWithBarrierExt_signal); + bool exception = false; - syclex::enqueue_signal_event(Queue1, event); + // Current limiation is that an event from different context + // cannot be used with enqueue_wait_event + try { + syclex::enqueue_wait_event(Queue2, event); + } catch (sycl::exception const &e) { + exception = true; + EXPECT_EQ(e.code(), sycl::errc::invalid); + EXPECT_STREQ(e.what(), "Not implemented yet. Event context must " + "match the queue context."); + } - EXPECT_EQ(RedefinedUrEnqueueEventsWaitWithBarrierExt_signal_counter, 1); + EXPECT_TRUE(exception); - ExpectedNumEventsInWaitList = 1; - mock::getCallbacks().set_replace_callback( - "urEnqueueEventsWaitWithBarrierExt", - &redefinedUrEnqueueEventsWaitWithBarrierExt_wait); + Queue1.wait(); + Queue2.wait(); +} - // Event from different context should still work with enqueue_wait_event - EXPECT_NO_THROW({ syclex::enqueue_wait_event(Queue2, event); }); +// Cross-context events with wait (multiple events). +// Current limitation is that the cross-context wait +// is not supported. +TEST_F(ReusableEventsTest, CrossContextEventsWait) { + mock::getCallbacks().set_replace_callback("urDeviceGet", + &redefinedUrDeviceGet); + mock::getCallbacks().set_replace_callback("urDeviceRelease", + &redefinedUrDeviceRelease); - EXPECT_EQ(RedefinedUrEnqueueEventsWaitWithBarrierExt_wait_counter, 1); + sycl::platform Plt = sycl::platform(); + auto Devices = Plt.get_devices(); + + if (Devices.size() < 2) { + GTEST_SKIP() << "Need at least 2 devices for this test"; + } + + const sycl::device Dev1 = Devices[0]; + const sycl::device Dev2 = Devices[1]; + sycl::context Ctx1{Dev1}; + sycl::context Ctx2{Dev2}; + + sycl::queue Queue1{Ctx1, Dev1}; + sycl::queue Queue2{Ctx2, Dev2}; + + auto event1 = syclex::make_event(Ctx1); + auto event2 = syclex::make_event(Ctx1); + + std::vector events{event1, event2}; + + bool exception = false; + + // Current limiation is that an event from different context + // cannot be used with enqueue_wait_event + try { + syclex::enqueue_wait_events(Queue2, events); + } catch (sycl::exception const &e) { + exception = true; + EXPECT_EQ(e.code(), sycl::errc::invalid); + EXPECT_STREQ(e.what(), "Not implemented yet. Context of all events must " + "match the queue context."); + } + + EXPECT_TRUE(exception); Queue1.wait(); Queue2.wait();