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