alpaka
Abstraction Library for Parallel Kernel Acceleration
EventGenericSycl.hpp
Go to the documentation of this file.
1 /* Copyright 2024 Jan Stephan, Antonio Di Pilato, Aurora Perego
2  * SPDX-License-Identifier: MPL-2.0
3  */
4 
5 #pragma once
6 
7 #include "alpaka/core/Sycl.hpp"
8 #include "alpaka/dev/Traits.hpp"
12 #include "alpaka/wait/Traits.hpp"
13 
14 #include <functional>
15 #include <memory>
16 #include <stdexcept>
17 
18 #ifdef ALPAKA_ACC_SYCL_ENABLED
19 
20 # include <sycl/sycl.hpp>
21 
22 namespace alpaka
23 {
24  //! The SYCL device event.
25  template<concepts::Tag TTag>
26  class EventGenericSycl final
27  {
28  public:
29  explicit EventGenericSycl(DevGenericSycl<TTag> const& dev) : m_dev{dev}
30  {
31  }
32 
33  friend auto operator==(EventGenericSycl const& lhs, EventGenericSycl const& rhs) -> bool
34  {
35  return (lhs.m_event == rhs.m_event);
36  }
37 
38  friend auto operator!=(EventGenericSycl const& lhs, EventGenericSycl const& rhs) -> bool
39  {
40  return !(lhs == rhs);
41  }
42 
43  [[nodiscard]] auto getNativeHandle() const
44  {
45  return m_event;
46  }
47 
48  void setEvent(sycl::event const& event)
49  {
50  m_event = event;
51  }
52 
53  DevGenericSycl<TTag> m_dev;
54 
55  private:
56  sycl::event m_event{};
57  };
58 } // namespace alpaka
59 
60 namespace alpaka::trait
61 {
62  //! The SYCL device event device get trait specialization.
63  template<concepts::Tag TTag>
64  struct GetDev<EventGenericSycl<TTag>>
65  {
66  static auto getDev(EventGenericSycl<TTag> const& event) -> DevGenericSycl<TTag>
67  {
68  return event.m_dev;
69  }
70  };
71 
72  //! The SYCL device event test trait specialization.
73  template<concepts::Tag TTag>
74  struct IsComplete<EventGenericSycl<TTag>>
75  {
76  static auto isComplete(EventGenericSycl<TTag> const& event)
77  {
78  auto const status
79  = event.getNativeHandle().template get_info<sycl::info::event::command_execution_status>();
80  return (status == sycl::info::event_command_status::complete);
81  }
82  };
83 
84  //! The SYCL queue enqueue trait specialization.
85  template<concepts::Tag TTag>
86  struct Enqueue<QueueGenericSyclNonBlocking<TTag>, EventGenericSycl<TTag>>
87  {
88  static auto enqueue(QueueGenericSyclNonBlocking<TTag>& queue, EventGenericSycl<TTag>& event)
89  {
90  event.setEvent(queue.m_spQueueImpl->get_last_event());
91  }
92  };
93 
94  //! The SYCL queue enqueue trait specialization.
95  template<concepts::Tag TTag>
96  struct Enqueue<QueueGenericSyclBlocking<TTag>, EventGenericSycl<TTag>>
97  {
98  static auto enqueue(QueueGenericSyclBlocking<TTag>& queue, EventGenericSycl<TTag>& event)
99  {
100  event.setEvent(queue.m_spQueueImpl->get_last_event());
101  }
102  };
103 
104  //! The SYCL device event thread wait trait specialization.
105  //!
106  //! Waits until the event itself and therefore all tasks preceding it in the queue it is enqueued to have been
107  //! completed. If the event is not enqueued to a queue the method returns immediately.
108  template<concepts::Tag TTag>
109  struct CurrentThreadWaitFor<EventGenericSycl<TTag>>
110  {
111  static auto currentThreadWaitFor(EventGenericSycl<TTag> const& event)
112  {
113  event.getNativeHandle().wait_and_throw();
114  }
115  };
116 
117  //! The SYCL queue event wait trait specialization.
118  template<concepts::Tag TTag>
119  struct WaiterWaitFor<QueueGenericSyclNonBlocking<TTag>, EventGenericSycl<TTag>>
120  {
121  static auto waiterWaitFor(QueueGenericSyclNonBlocking<TTag>& queue, EventGenericSycl<TTag> const& event)
122  {
123  queue.m_spQueueImpl->register_dependency(event.getNativeHandle());
124  }
125  };
126 
127  //! The SYCL queue event wait trait specialization.
128  template<concepts::Tag TTag>
129  struct WaiterWaitFor<QueueGenericSyclBlocking<TTag>, EventGenericSycl<TTag>>
130  {
131  static auto waiterWaitFor(QueueGenericSyclBlocking<TTag>& queue, EventGenericSycl<TTag> const& event)
132  {
133  queue.m_spQueueImpl->register_dependency(event.getNativeHandle());
134  }
135  };
136 
137  //! The SYCL device event wait trait specialization.
138  //!
139  //! Any future work submitted in any queue of this device will wait for event to complete before beginning
140  //! execution.
141  template<concepts::Tag TTag>
142  struct WaiterWaitFor<DevGenericSycl<TTag>, EventGenericSycl<TTag>>
143  {
144  static auto waiterWaitFor(DevGenericSycl<TTag>& dev, EventGenericSycl<TTag> const& event)
145  {
146  dev.m_impl->register_dependency(event.getNativeHandle());
147  }
148  };
149 
150  //! The SYCL device event native handle trait specialization.
151  template<concepts::Tag TTag>
152  struct NativeHandle<EventGenericSycl<TTag>>
153  {
154  [[nodiscard]] static auto getNativeHandle(EventGenericSycl<TTag> const& event)
155  {
156  return event.getNativeHandle();
157  }
158  };
159 } // namespace alpaka::trait
160 
161 #endif
constexpr ALPAKA_FN_HOST_ACC bool operator==(Complex< T > const &lhs, Complex< T > const &rhs)
Equality of two complex numbers.
Definition: Complex.hpp:292
constexpr ALPAKA_FN_HOST_ACC bool operator!=(Complex< T > const &lhs, Complex< T > const &rhs)
Inequality of two complex numbers.
Definition: Complex.hpp:318
The accelerator traits.
The alpaka accelerator library.
ALPAKA_FN_HOST auto isComplete(TEvent const &event) -> bool
Tests if the given event has already been completed.
Definition: Traits.hpp:34
decltype(getNativeHandle(std::declval< TImpl >())) NativeHandle
Alias to the type of the native handle.
Definition: Traits.hpp:36
ALPAKA_FN_HOST auto getDev(T const &t)
Definition: Traits.hpp:68
ALPAKA_FN_HOST auto getNativeHandle(TImpl const &impl)
Get the native handle of the alpaka object. It will return the alpaka object handle if there is any,...
Definition: Traits.hpp:29
ALPAKA_FN_HOST auto enqueue(TQueue &queue, TTask &&task) -> void
Queues the given task in the given queue.
Definition: Traits.hpp:47
static auto getNativeHandle(TImpl const &)
Definition: Traits.hpp:18