alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
Event.hpp
Go to the documentation of this file.
1/* Copyright 2025 René Widera
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
7#include "alpaka/api/util.hpp"
10#include "alpaka/interface.hpp"
15
16#include <algorithm>
17#include <shared_mutex>
18#include <sstream>
19
20#if ALPAKA_LANG_SYCL
21
22# include <sycl/sycl.hpp>
23
24namespace alpaka::onHost
25{
26 namespace syclGeneric
27 {
28 template<typename T_Device>
29 struct Event : std::enable_shared_from_this<Event<T_Device>>
30 {
31 private:
32 friend struct alpaka::internal::GetApi;
33
34 public:
35 Event(internal::concepts::DeviceHandle auto device, uint32_t const idx)
36 : m_device(std::move(device))
37 , m_idx(idx)
38 {
39 ALPAKA_LOG_FUNCTION(onHost::logger::event);
40 }
41
42 Event(Event const&) = delete;
43 Event& operator=(Event const&) = delete;
44
45 Event(Event&&) = delete;
46 Event& operator=(Event&&) = delete;
47
48 ~Event()
49 {
50 ALPAKA_LOG_FUNCTION(onHost::logger::event);
51 try
52 {
53 getEvent().wait_and_throw();
54 }
55 catch(sycl::exception const& err)
56 {
57 std::cerr << "Caught SYCL exception while destructing a SYCL event: " << err.what() << " ("
58 << err.code() << ')' << std::endl;
59 }
60 catch(std::exception const& err)
61 {
62 std::cerr << "The following runtime error(s) occurred while destructing a SYCL event:"
63 << err.what() << std::endl;
64 }
65 }
66
67 std::shared_ptr<Event> getSharedPtr()
68 {
69 return this->shared_from_this();
70 }
71
72 [[nodiscard]] auto getNativeHandle() const noexcept
73 {
74 return getEvent();
75 }
76
77 void wait()
78 {
79 ALPAKA_LOG_FUNCTION(onHost::logger::event);
80 getEvent().wait_and_throw();
81 }
82
83 std::string getName() const
84 {
85 std::stringstream ss;
86 ss << "Queue<" << getApi(m_device).getName() << ">";
87 ss << " id=" << m_idx;
88 return ss.str();
89 }
90
91 private:
92 friend struct alpaka::internal::GetDeviceType;
93 friend struct alpaka::onHost::internal::Enqueue;
94
95 auto getDeviceKind() const
96 {
97 return alpaka::internal::getDeviceKind(*m_device.get());
98 }
99
100 auto getDevice() const
101 {
102 return m_device;
103 }
104
105 friend struct onHost::internal::GetDevice;
106
107 friend struct onHost::internal::IsEventComplete;
108
109 /** Check if the event is complete.
110 *
111 * @return true if the event is complete, false otherwise
112 */
113 bool isEventComplete() noexcept
114 {
115 auto const status = getEvent().template get_info<sycl::info::event::command_execution_status>();
116 return (status == sycl::info::event_command_status::complete);
117 }
118
119 friend struct internal::WaitFor;
120 friend struct internal::Wait;
121
122 void setEvent(sycl::event const& event)
123 {
124 std::unique_lock<std::shared_mutex> lock{m_eventGuard};
125 m_event = event;
126 }
127
128 sycl::event getEvent() const
129 {
130 std::shared_lock<std::shared_mutex> lock{m_eventGuard};
131 return m_event;
132 }
133
134 Handle<T_Device> m_device;
135 //! secure that two threads can change the event at the same time
136 mutable std::shared_mutex m_eventGuard;
137
138 //! You should not use the event directly, use always getEvent() or setEvent()
139 sycl::event m_event{};
140 uint32_t m_idx = 0u;
141 };
142
143
144 } // namespace syclGeneric
145} // namespace alpaka::onHost
146
147namespace alpaka::internal
148
149{
150 template<typename T_Device>
151 struct GetApi::Op<alpaka::onHost::syclGeneric::Event<T_Device>>
152 {
153 inline constexpr auto operator()(auto&& event) const
154 {
155 return alpaka::getApi(event.m_device);
156 }
157 };
158} // namespace alpaka::internal
159
160#endif
#define ALPAKA_LOG_FUNCTION(logLvl)
Log the entry and exit of a scope.
Definition logger.hpp:95
constexpr auto alpaka
Definition fn.hpp:66
constexpr Device device
Definition scope.hpp:70
constexpr auto event
Definition lvl.hpp:97
Functionality which is usable on the host CPU controller thread.
Definition api.hpp:40
auto getNativeHandle(auto const &handle)
Get the native handle of an handle.
Event(Handle< T_Event > &&) -> Event< Device< ALPAKA_TYPEOF(alpaka::internal::getApi(std::declval< T_Event >())), ALPAKA_TYPEOF(alpaka::internal::getDeviceKind(std::declval< T_Event >()))> >
std::convertible_to< std::string > auto getName(auto &&any)
Runtime name for a given object.
Definition interface.hpp:96
void wait(alpaka::concepts::HasGet auto &handle)
wait for all work to be finished
constexpr decltype(auto) getDeviceKind(auto &&any)
Get the device type of an object.
Definition interface.hpp:52
constexpr decltype(auto) getApi(auto &&any)
Get the API an object depends on.
Definition interface.hpp:23