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
6#pragma once
7
8#pragma once
9
16#include "alpaka/api/util.hpp"
21#include "alpaka/onAcc/Acc.hpp"
28
29#if ALPAKA_LANG_CUDA || ALPAKA_LANG_HIP
30
32
33# include <cstdint>
34# include <sstream>
35
36namespace alpaka::onHost
37{
38 namespace unifiedCudaHip
39 {
40 template<typename T_Device>
41 struct Event : std::enable_shared_from_this<Event<T_Device>>
42 {
43 using ApiInterface = typename T_Device::ApiInterface;
44
45 public:
46 Event(internal::concepts::DeviceHandle auto device, uint32_t const idx)
47 : m_device(std::move(device))
48 , m_idx(idx)
49 {
50 ALPAKA_LOG_FUNCTION(onHost::logger::event);
51 // Set the current device.
52 ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(
53 ApiInterface,
54 ApiInterface::setDevice(internal::getNativeHandle(*m_device.get())));
55
56 // Create the event on the current device with the specified flags. Valid flags include:
57 // - cuda/hip-EventDefault: Default event creation flag.
58 // - cuda/hip-EventBlockingSync : Specifies that event should use blocking synchronization.
59 // A host thread that uses cuda/hip-EventSynchronize() to wait on an event created with this flag
60 // will block until the event actually completes. (currently not used, @todo: check if this is
61 // required, in mainline alpaka this is configuable in the constructor.
62 // - cuda/hip-EventDisableTiming : Specifies that the created event does not need to record timing
63 // data.
64 // Events created with this flag specified and the cuda/hip-EventBlockingSync flag not specified
65 // will provide the best performance when used with cudaStreamWaitEvent() and cudaEventQuery().
66 ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(
67 ApiInterface,
68 ApiInterface::eventCreateWithFlags(
69 &m_nativeEvent,
70 ApiInterface::eventDefault | ApiInterface::eventDisableTiming));
71 }
72
73 ~Event()
74 {
75 ALPAKA_LOG_FUNCTION(onHost::logger::event);
76 onHost::internal::wait(*this);
77 ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK_NOEXCEPT(ApiInterface, ApiInterface::eventDestroy(getNativeHandle()));
78 }
79
80 Event(Event const&) = delete;
81 Event& operator=(Event const&) = delete;
82
83 Event(Event&&) = delete;
84 Event& operator=(Event&&) = delete;
85
86 bool operator==(Event const& other) const
87 {
88 return m_idx == other.m_idx && m_device == other.m_device;
89 }
90
91 bool operator!=(Event const& other) const
92 {
93 return !(*this == other);
94 }
95
96 private:
97 Handle<T_Device> m_device;
98 uint32_t m_idx = 0u;
99 typename ApiInterface::Event_t m_nativeEvent;
100
101 friend struct alpaka::internal::GetName;
102
103 std::string getName() const
104 {
105 return std::string("unifiedCudaHip::Event id=") + std::to_string(m_idx);
106 }
107
108 friend struct onHost::internal::GetNativeHandle;
109
110 [[nodiscard]] auto getNativeHandle() const noexcept
111 {
112 return m_nativeEvent;
113 }
114
115 friend struct onHost::internal::Enqueue;
116
117 friend struct onHost::internal::Wait;
118
119 void wait() const
120 {
121 ALPAKA_LOG_FUNCTION(onHost::logger::event);
122 ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(ApiInterface, ApiInterface::eventSynchronize(getNativeHandle()));
123 }
124
125 friend struct alpaka::internal::GetDeviceType;
126
127 auto getDeviceKind() const
128 {
129 return alpaka::internal::getDeviceKind(*m_device.get());
130 }
131
132 auto getDevice() const
133 {
134 return m_device;
135 }
136
137 std::shared_ptr<Event> getSharedPtr()
138 {
139 return this->shared_from_this();
140 }
141
142 friend struct onHost::internal::IsEventComplete;
143
144 bool isEventComplete() noexcept
145 {
146 typename ApiInterface::Error_t ret = ApiInterface::success;
147 ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK_IGNORE(
148 ApiInterface,
149 ret = ApiInterface::eventQuery(m_nativeEvent),
150 ApiInterface::errorNotReady);
151 return (ret == ApiInterface::success);
152 }
153
154 friend struct onHost::internal::GetDevice;
155
156 friend struct alpaka::internal::GetApi;
157 };
158
159 } // namespace unifiedCudaHip
160} // namespace alpaka::onHost
161
162namespace alpaka::internal
163{
164 template<typename T_Device>
165 struct GetApi::Op<onHost::unifiedCudaHip::Event<T_Device>>
166 {
167 inline constexpr auto operator()(auto&& queue) const
168 {
169 return getApi(queue.m_device);
170 }
171 };
172} // namespace alpaka::internal
173#endif
#define ALPAKA_LOG_FUNCTION(logLvl)
Log the entry and exit of a scope.
Definition logger.hpp:95
constexpr bool operator!=(alpaka::concepts::Api auto lhs, alpaka::concepts::Api auto rhs)
Definition api.hpp:53
constexpr bool operator==(alpaka::concepts::Api auto lhs, alpaka::concepts::Api auto rhs)
Definition api.hpp:48
alpaka internal implementations.
Definition generic.hpp:19
constexpr auto getApi(auto &&any)
Definition interface.hpp:62
constexpr auto getDeviceKind(auto &&any)
Definition interface.hpp:85
constexpr Device device
Definition scope.hpp:70
bool isEventComplete(auto &&any)
constexpr auto getDevice(auto &&any)
Definition interface.hpp:77
constexpr auto queue
Definition lvl.hpp:127
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 auto operator()(auto &&any) const
Definition interface.hpp:55