CoopSync for TBB 0.1.0
Cooperative synchronization primitives for TBB resumable tasks
Loading...
Searching...
No Matches
wait_for.hpp
1// SPDX-FileCopyrightText: 2026 CERN
2//
3// SPDX-License-Identifier: Apache-2.0
4
5#pragma once
6
7#include <hip/hip_runtime_api.h>
8#include <oneapi/tbb/task.h>
9
10#include <algorithm>
11#include <array>
12#include <atomic>
13#include <cstddef>
14#include <iterator>
15#include <type_traits>
16#include <vector>
17
18// clang-format off
19#ifndef COOPSYNC_TBB_HIP_NODISCARD
20 #if defined(__has_cpp_attribute)
21 #if __has_cpp_attribute(nodiscard) >= 201603L
22 #define COOPSYNC_TBB_HIP_NODISCARD [[nodiscard]]
23 #endif
24 #elif defined(__cplusplus) && __cplusplus >= 201703L
25 #define COOPSYNC_TBB_HIP_NODISCARD [[nodiscard]]
26 #endif
27 #ifndef COOPSYNC_TBB_HIP_NODISCARD
28 #define COOPSYNC_TBB_HIP_NODISCARD
29 #endif
30#endif
31
32#ifndef COOPSYNC_TBB_HIP_HAS_HOST_FUNC_
33 #if defined(HIP_VERSION) && HIP_VERSION >= 50200000
34 #define COOPSYNC_TBB_HIP_HAS_HOST_FUNC_ 1 // NOLINT(cppcoreguidelines-macro-to-enum,cppcoreguidelines-macro-usage)
35 #else
36 #define COOPSYNC_TBB_HIP_HAS_HOST_FUNC_ 0 // NOLINT(cppcoreguidelines-macro-to-enum,cppcoreguidelines-macro-usage)
37 #endif
38#endif
39// clang-format on
40
41namespace coopsync_tbb {
43namespace hip {
44
45namespace detail {
46
47template <typename T>
48struct is_hip_stream
49 : std::is_same<typename std::decay<T>::type, ::hipStream_t> {};
50
51template <bool...>
52struct bool_pack;
53
54template <bool... Bs>
55struct all_true : std::is_same<bool_pack<Bs..., true>, bool_pack<true, Bs...>> {
56};
57
58template <typename... Ts>
59struct all_hip_stream : all_true<is_hip_stream<Ts>::value...> {};
60
61template <typename Context>
62static void resumption_host_func(void* self) {
63 static_cast<Context*>(self)->notify(::hipSuccess);
64}
65
66template <typename Context>
67static void resumption_callback(::hipStream_t, ::hipError_t status,
68 void* self) {
69 static_cast<Context*>(self)->notify(status);
70}
71
72template <typename Context>
73static inline void register_callback(::hipStream_t stream, Context& context) {
74#if COOPSYNC_TBB_HIP_HAS_HOST_FUNC_
75 const auto err =
76 ::hipLaunchHostFunc(stream, &resumption_host_func<Context>, &context);
77#else
78 const auto err = ::hipStreamAddCallback(
79 stream, &resumption_callback<Context>, &context, 0);
80#endif
81 if (err != ::hipSuccess) {
82 context.notify(err);
83 }
84}
85
86struct single_stream_context {
87 void notify(::hipError_t status) {
88 err = status;
89 ::tbb::task::resume(suspend_point);
90 }
91
92 ::tbb::task::suspend_point suspend_point{};
93 ::hipError_t err{::hipSuccess};
94};
95
96struct multi_stream_context {
97 explicit multi_stream_context(std::size_t n) : pending(n) {}
98
99 void notify_one() {
100 if (pending.fetch_sub(1, std::memory_order_acq_rel) == 1) {
101 ::tbb::task::resume(suspend_point);
102 }
103 }
104
105 ::tbb::task::suspend_point suspend_point{};
106 std::atomic<std::size_t> pending;
107};
108
109struct multi_stream_item {
110 multi_stream_context* context{nullptr};
111 ::hipError_t* out{nullptr};
112
113 void notify(::hipError_t status) {
114 *out = status;
115 context->notify_one();
116 }
117};
118
119} // namespace detail
120
127COOPSYNC_TBB_HIP_NODISCARD static inline ::hipError_t wait_for(
128 ::hipStream_t stream) {
129 auto context = detail::single_stream_context{};
130 ::tbb::task::suspend([&](::tbb::task::suspend_point tag) {
131 context.suspend_point = tag;
132 detail::register_callback(stream, context);
133 });
134 return context.err;
135}
136
145template <typename... StreamTs>
146COOPSYNC_TBB_HIP_NODISCARD static inline std::array<::hipError_t,
147 sizeof...(StreamTs)>
148wait_for_all(StreamTs... streams) {
149 static_assert(detail::all_hip_stream<StreamTs...>::value,
150 "wait_for_all(streams...) requires hipStream_t arguments");
151
152 const std::size_t N = sizeof...(StreamTs);
153 auto errs = std::array<::hipError_t, sizeof...(StreamTs)>{};
154 errs.fill(::hipSuccess);
155
156 if (N == 0) {
157 return errs;
158 }
159
160 const auto stream_array =
161 std::array<::hipStream_t, sizeof...(StreamTs)>{{streams...}};
162 auto context = detail::multi_stream_context(N);
163 auto items = std::array<detail::multi_stream_item, N>{};
164
165 ::tbb::task::suspend([&](::tbb::task::suspend_point tag) {
166 context.suspend_point = tag;
167 for (std::size_t i = 0; i < N; ++i) {
168 items.at(i) = {&context, &errs.at(i)};
169 detail::register_callback(stream_array.at(i), items.at(i));
170 }
171 });
172
173 return errs;
174}
175
188template <typename ForwardIt, typename OutputIt>
189static inline OutputIt wait_for_range(ForwardIt first, ForwardIt last,
190 OutputIt out) {
191 static_assert(
192 detail::is_hip_stream<
193 typename std::iterator_traits<ForwardIt>::value_type>::value,
194 "wait_for_range(first,last,out) requires hipStream_t values");
195
196 if (first == last) {
197 return out;
198 }
199
200 const auto n = static_cast<std::size_t>(std::distance(first, last));
201 auto context = detail::multi_stream_context(n);
202 auto errs = std::vector<::hipError_t>(n, ::hipSuccess);
203 auto items = std::vector<detail::multi_stream_item>(n);
204
205 ::tbb::task::suspend([&](::tbb::task::suspend_point tag) {
206 context.suspend_point = tag;
207 std::size_t i = 0;
208 for (auto it = first; it != last; ++it, ++i) {
209 // NOLINTBEGIN(cppcoreguidelines-pro-bounds-avoid-unchecked-container-access)
210 items[i] = {&context, &errs[i]};
211 detail::register_callback(*it, items[i]);
212 // NOLINTEND(cppcoreguidelines-pro-bounds-avoid-unchecked-container-access)
213 }
214 });
215
216 return std::copy(errs.begin(), errs.end(), out);
217}
218} // namespace hip
219} // namespace coopsync_tbb
220
221#undef COOPSYNC_TBB_HIP_NODISCARD
HIP integration.
Definition wait_for.hpp:43
static COOPSYNC_TBB_HIP_NODISCARDinline ::hipError_t wait_for(::hipStream_t stream)
Suspends the current TBB task until all the work in a HIP stream completes. Internally a HIP callback...
Definition wait_for.hpp:127
static COOPSYNC_TBB_HIP_NODISCARD std::array<::hipError_t, sizeof...(StreamTs)> wait_for_all(StreamTs... streams)
Suspends the current TBB task until all the work in all the provided HIP streams is completed....
Definition wait_for.hpp:148
static OutputIt wait_for_range(ForwardIt first, ForwardIt last, OutputIt out)
Suspends the current TBB task until all the work in all the provided HIP streams completes....
Definition wait_for.hpp:189
Main namespace of the library, containing all public APIs.
Definition wait_for.hpp:11