|
| 1 | +//==-------- CommandsWaitForEvents.cpp --- Scheduler unit tests ------------==// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "SchedulerTest.hpp" |
| 10 | +#include "SchedulerTestUtils.hpp" |
| 11 | +#include <helpers/PiMock.hpp> |
| 12 | + |
| 13 | +using namespace cl::sycl; |
| 14 | + |
| 15 | +struct TestCtx { |
| 16 | + queue &Q1; |
| 17 | + queue &Q2; |
| 18 | + |
| 19 | + std::shared_ptr<detail::context_impl> Ctx1; |
| 20 | + std::shared_ptr<detail::context_impl> Ctx2; |
| 21 | + |
| 22 | + pi_event EventCtx1 = reinterpret_cast<pi_event>(0x01); |
| 23 | + pi_event EventCtx2 = reinterpret_cast<pi_event>(0x02); |
| 24 | + |
| 25 | + bool EventCtx1WasWaited = false; |
| 26 | + bool EventCtx2WasWaited = false; |
| 27 | + |
| 28 | + TestCtx(queue &Queue1, queue &Queue2) |
| 29 | + : Q1(Queue1), Q2(Queue2), Ctx1{detail::getSyclObjImpl(Q1.get_context())}, |
| 30 | + Ctx2{detail::getSyclObjImpl(Q2.get_context())} {} |
| 31 | +}; |
| 32 | + |
| 33 | +std::unique_ptr<TestCtx> TestContext; |
| 34 | + |
| 35 | +pi_result waitFunc(pi_uint32 N, const pi_event *List) { |
| 36 | + EXPECT_EQ(N, 1u) << "piEventsWait called for different contexts\n"; |
| 37 | + |
| 38 | + EXPECT_TRUE((TestContext->EventCtx1 == *List) || |
| 39 | + (TestContext->EventCtx2 == *List)) |
| 40 | + << "piEventsWait called for unknown event"; |
| 41 | + |
| 42 | + if (TestContext->EventCtx1 == *List) |
| 43 | + TestContext->EventCtx1WasWaited = true; |
| 44 | + |
| 45 | + if (TestContext->EventCtx2 == *List) |
| 46 | + TestContext->EventCtx2WasWaited = true; |
| 47 | + |
| 48 | + return PI_SUCCESS; |
| 49 | +} |
| 50 | + |
| 51 | +pi_result retainReleaseFunc(pi_event) { return PI_SUCCESS; } |
| 52 | + |
| 53 | +pi_result getEventInfoFunc(pi_event Event, pi_event_info PName, size_t PVSize, |
| 54 | + void *PV, size_t *PVSizeRet) { |
| 55 | + EXPECT_EQ(PName, PI_EVENT_INFO_CONTEXT) << "Unknown param name"; |
| 56 | + |
| 57 | + if (Event == TestContext->EventCtx1) |
| 58 | + *reinterpret_cast<pi_context *>(PV) = |
| 59 | + reinterpret_cast<pi_context>(TestContext->Ctx1->get()); |
| 60 | + else if (Event == TestContext->EventCtx2) |
| 61 | + *reinterpret_cast<pi_context *>(PV) = |
| 62 | + reinterpret_cast<pi_context>(TestContext->Ctx2->get()); |
| 63 | + |
| 64 | + return PI_SUCCESS; |
| 65 | +} |
| 66 | + |
| 67 | +TEST_F(SchedulerTest, CommandsWaitForEvents) { |
| 68 | + default_selector Selector{}; |
| 69 | + if (Selector.select_device().is_host()) { |
| 70 | + std::cerr << "Not run due to host-only environment\n"; |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + queue Q1; |
| 75 | + queue Q2; |
| 76 | + |
| 77 | + unittest::PiMock Mock1(Q1); |
| 78 | + unittest::PiMock Mock2(Q2); |
| 79 | + |
| 80 | + Mock1.redefine<detail::PiApiKind::piEventsWait>(waitFunc); |
| 81 | + Mock1.redefine<detail::PiApiKind::piEventRetain>(retainReleaseFunc); |
| 82 | + Mock1.redefine<detail::PiApiKind::piEventRelease>(retainReleaseFunc); |
| 83 | + Mock1.redefine<detail::PiApiKind::piEventGetInfo>(getEventInfoFunc); |
| 84 | + |
| 85 | + Mock2.redefine<detail::PiApiKind::piEventsWait>(waitFunc); |
| 86 | + Mock2.redefine<detail::PiApiKind::piEventRetain>(retainReleaseFunc); |
| 87 | + Mock2.redefine<detail::PiApiKind::piEventRelease>(retainReleaseFunc); |
| 88 | + Mock2.redefine<detail::PiApiKind::piEventGetInfo>(getEventInfoFunc); |
| 89 | + |
| 90 | + TestContext.reset(new TestCtx(Q1, Q2)); |
| 91 | + |
| 92 | + std::shared_ptr<detail::event_impl> E1( |
| 93 | + new detail::event_impl(TestContext->EventCtx1, Q1.get_context())); |
| 94 | + std::shared_ptr<detail::event_impl> E2( |
| 95 | + new detail::event_impl(TestContext->EventCtx2, Q2.get_context())); |
| 96 | + |
| 97 | + sycl::device HostDevice; |
| 98 | + std::shared_ptr<detail::queue_impl> DefaultHostQueue(new detail::queue_impl( |
| 99 | + detail::getSyclObjImpl(HostDevice), /*AsyncHandler=*/{}, |
| 100 | + detail::QueueOrder::Ordered, /*PropList=*/{})); |
| 101 | + |
| 102 | + MockCommand Cmd(DefaultHostQueue); |
| 103 | + |
| 104 | + std::vector<std::shared_ptr<detail::event_impl>> Events; |
| 105 | + Events.push_back(E1); |
| 106 | + Events.push_back(E2); |
| 107 | + |
| 108 | + pi_event EventResult = nullptr; |
| 109 | + |
| 110 | + Cmd.waitForEventsCall(DefaultHostQueue, Events, EventResult); |
| 111 | + |
| 112 | + ASSERT_TRUE(TestContext->EventCtx1WasWaited && |
| 113 | + TestContext->EventCtx2WasWaited) |
| 114 | + << "Not all events were waited for"; |
| 115 | +} |
0 commit comments