alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
Event.hpp
Go to the documentation of this file.
1/* Copyright 2023 Axel Hübl, Benjamin Worpitz, Matthias Werner, René Widera, Jan Stephan, Bernhard Manfred Gruber
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
7
14
15#include <cstdint>
16#include <cstring>
17#include <future>
18#include <sstream>
19
20namespace alpaka::onHost
21{
22 namespace cpu
23 {
24 template<typename T_Device>
25 struct Event : std::enable_shared_from_this<Event<T_Device>>
26 {
27 public:
28 Event(internal::concepts::DeviceHandle auto device, uint32_t const idx)
29 : m_device(std::move(device))
30 , m_idx(idx)
31 {
33 }
34
40
41 Event(Event const&) = delete;
42 Event& operator=(Event const&) = delete;
43
44 Event(Event&&) = delete;
45 Event& operator=(Event&&) = delete;
46
47 bool operator==(Event const& other) const
48 {
49 return m_idx == other.m_idx && m_device == other.m_device;
50 }
51
52 bool operator!=(Event const& other) const
53 {
54 return !(*this == other);
55 }
56
57 private:
59 uint32_t m_idx = 0u;
60
61 //!< The mutex used to synchronize access to the event.
62 std::mutex mutable m_mutex;
63 //!< The future signaling the event completion.
64 std::shared_future<void> m_future;
65 //!< The number of times this event has been enqueued.
66 std::size_t m_enqueueCount = 0u;
67 //!< The time this event has been ready the last time.
68 //!< Ready means that the event was not waiting within a queue
69 //!< (not enqueued or already completed). If m_enqueueCount ==
70 //!< m_LastReadyEnqueueCount, the event is currently not enqueued
71 std::size_t m_LastReadyEnqueueCount = 0u;
72
74
75 std::string getName() const
76 {
77 return std::string("host::Event id=") + std::to_string(m_idx);
78 }
79
81 friend struct internal::Enqueue;
83
84 auto getDeviceKind() const
85 {
87 }
88
89 auto getDevice() const
90 {
91 return m_device;
92 }
93
94 std::shared_ptr<Event> getSharedPtr()
95 {
96 return this->shared_from_this();
97 }
98
100
102
103 /** Check if the event is ready.
104 *
105 * @attention Do not call this method without holding the event lock.
106 *
107 * @return true if the event is ready, false otherwise
108 */
114
115 /** Check if the event is complete.
116 *
117 * @attention Should not be called if the event lock is acquired, because it could lead to a deadlock.
118 *
119 * @return true if the event is complete, false otherwise
120 */
121 bool isEventComplete() noexcept
122 {
124 std::lock_guard<std::mutex> lk(m_mutex);
125 return isReady();
126 }
127
128 friend struct internal::WaitFor;
129 friend struct internal::Wait;
130
131 void wait()
132 {
134 std::unique_lock<std::mutex> lk(m_mutex);
135 size_t enqueueCount = m_enqueueCount;
136
137 while(enqueueCount > m_LastReadyEnqueueCount)
138 {
139 auto future = m_future;
140 lk.unlock();
141 future.get();
142 lk.lock();
143 }
144 }
145
147 };
148 } // namespace cpu
149} // namespace alpaka::onHost
150
151namespace alpaka::internal
152{
153 template<typename T_Device>
154 struct GetApi::Op<onHost::cpu::Event<T_Device>>
155 {
156 inline constexpr auto operator()(auto&& event) const
157 {
158 return alpaka::getApi(event.m_device);
159 }
160 };
161} // namespace alpaka::internal
#define ALPAKA_LOG_FUNCTION(logLvl)
Log the entry and exit of a scope.
Definition logger.hpp:95
alpaka internal implementations.
Definition generic.hpp:19
constexpr auto getDeviceKind(auto &&any)
Definition interface.hpp:85
void wait(auto &&any)
constexpr auto event
Definition lvl.hpp:97
Functionality which is usable on the host CPU controller thread.
Definition api.hpp:40
std::shared_ptr< T > Handle
Definition Handle.hpp:30
constexpr decltype(auto) getApi(auto &&any)
Get the API an object depends on.
Definition interface.hpp:23
STL namespace.
std::size_t m_LastReadyEnqueueCount
Definition Event.hpp:71
std::shared_future< void > m_future
The number of times this event has been enqueued.
Definition Event.hpp:64
Event(Event const &)=delete
std::string getName() const
Definition Event.hpp:75
std::size_t m_enqueueCount
The time this event has been ready the last time. Ready means that the event was not waiting within a...
Definition Event.hpp:66
bool operator!=(Event const &other) const
Definition Event.hpp:52
auto getDeviceKind() const
Definition Event.hpp:84
auto getDevice() const
Definition Event.hpp:89
bool isReady() noexcept
Check if the event is ready.
Definition Event.hpp:109
bool isEventComplete() noexcept
Check if the event is complete.
Definition Event.hpp:121
Handle< T_Device > m_device
Definition Event.hpp:58
std::shared_ptr< Event > getSharedPtr()
Definition Event.hpp:94
Event(Event &&)=delete
bool operator==(Event const &other) const
Definition Event.hpp:47
Event & operator=(Event const &)=delete
Event & operator=(Event &&)=delete
std::mutex m_mutex
The future signaling the event completion.
Definition Event.hpp:62
Event(internal::concepts::DeviceHandle auto device, uint32_t const idx)
Definition Event.hpp:28
uint32_t m_idx
The mutex used to synchronize access to the event.
Definition Event.hpp:59