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 <cuda_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_CUDA_NODISCARD
20 #if defined(__has_cpp_attribute)
21 #if __has_cpp_attribute(nodiscard) >= 201603L
22 #define COOPSYNC_TBB_CUDA_NODISCARD [[nodiscard]]
23 #endif
24 #elif defined(__cplusplus) && __cplusplus >= 201703L
25 #define COOPSYNC_TBB_CUDA_NODISCARD [[nodiscard]]
26 #endif
27 #ifndef COOPSYNC_TBB_CUDA_NODISCARD
28 #define COOPSYNC_TBB_CUDA_NODISCARD
29 #endif
30#endif
31
32// cudaStreamAddCallback is pending deprecation as of CUDA 10.0;
33// the replacement is cudaLaunchHostFunc.
34// Since CUDA 13.2, cudaLaunchHostFunc_v2 allows to configure
35// the behaviour of the callback thread.
36#ifndef COOPSYNC_TBB_CUDA_HAS_HOST_FUNC_
37 #if defined(CUDART_VERSION) && CUDART_VERSION >= 13020
38 #define COOPSYNC_TBB_CUDA_HAS_HOST_FUNC_ 2 // NOLINT(cppcoreguidelines-macro-to-enum,cppcoreguidelines-macro-usage)
39 #elif defined(CUDART_VERSION) && CUDART_VERSION >= 10000
40 #define COOPSYNC_TBB_CUDA_HAS_HOST_FUNC_ 1 // NOLINT(cppcoreguidelines-macro-to-enum,cppcoreguidelines-macro-usage)
41 #else
42 #define COOPSYNC_TBB_CUDA_HAS_HOST_FUNC_ 0 // NOLINT(cppcoreguidelines-macro-to-enum,cppcoreguidelines-macro-usage)
43 #endif
44#endif
45// clang-format on
46
47namespace coopsync_tbb {
49namespace cuda {
50
51namespace detail {
52
53template <typename T>
54struct is_cuda_stream
55 : std::is_same<typename std::decay<T>::type, ::cudaStream_t> {};
56
57template <bool...>
58struct bool_pack;
59
60template <bool... Bs>
61struct all_true : std::is_same<bool_pack<Bs..., true>, bool_pack<true, Bs...>> {
62};
63
64template <typename... Ts>
65struct all_cuda_stream : all_true<is_cuda_stream<Ts>::value...> {};
66
67template <typename Context>
68static void CUDART_CB resumption_host_func(void* self) {
69 static_cast<Context*>(self)->notify(::cudaSuccess);
70}
71
72template <typename Context>
73static void CUDART_CB resumption_callback(::cudaStream_t, ::cudaError_t status,
74 void* self) {
75 static_cast<Context*>(self)->notify(status);
76}
77
78template <typename Context>
79static inline void register_callback(::cudaStream_t stream, Context& context,
80 unsigned int sync_mode) {
81#if COOPSYNC_TBB_CUDA_HAS_HOST_FUNC_ == 2
82 const auto err = ::cudaLaunchHostFunc_v2(
83 stream, &resumption_host_func<Context>, &context, sync_mode);
84#elif COOPSYNC_TBB_CUDA_HAS_HOST_FUNC_ == 1
85 const auto err =
86 ::cudaLaunchHostFunc(stream, &resumption_host_func<Context>, &context);
87#else
88 const auto err = ::cudaStreamAddCallback(
89 stream, &resumption_callback<Context>, &context, 0);
90#endif
91 if (err != ::cudaSuccess) {
92 context.notify(err);
93 }
94}
95
96struct single_stream_context {
97 void notify(::cudaError_t status) {
98 err = status;
99 ::tbb::task::resume(suspend_point);
100 }
101
102 ::tbb::task::suspend_point suspend_point{};
103 ::cudaError_t err{::cudaSuccess};
104};
105
106struct multi_stream_context {
107 explicit multi_stream_context(std::size_t n) : pending(n) {}
108
109 void notify_one() {
110 if (pending.fetch_sub(1, std::memory_order_acq_rel) == 1) {
111 ::tbb::task::resume(suspend_point);
112 }
113 }
114
115 ::tbb::task::suspend_point suspend_point{};
116 std::atomic<std::size_t> pending;
117};
118
119struct multi_stream_item {
120 multi_stream_context* context{nullptr};
121 ::cudaError_t* out{nullptr};
122
123 void notify(::cudaError_t status) {
124 *out = status;
125 context->notify_one();
126 }
127};
128
129} // namespace detail
130
141COOPSYNC_TBB_CUDA_NODISCARD static inline ::cudaError_t wait_for(
142 ::cudaStream_t stream, unsigned int sync_mode = 0u) {
143 auto context = detail::single_stream_context{};
144 ::tbb::task::suspend([&](::tbb::task::suspend_point tag) {
145 context.suspend_point = tag;
146 detail::register_callback(stream, context, sync_mode);
147 });
148 return context.err;
149}
150
165template <typename... StreamTs>
166COOPSYNC_TBB_CUDA_NODISCARD static inline std::array<::cudaError_t,
167 sizeof...(StreamTs)>
168wait_for_all(unsigned int sync_mode, StreamTs... streams) {
169 static_assert(detail::all_cuda_stream<StreamTs...>::value,
170 "wait_for_all(streams...) requires cudaStream_t arguments");
171
172 const std::size_t N = sizeof...(StreamTs);
173 auto errs = std::array<::cudaError_t, sizeof...(StreamTs)>{};
174 errs.fill(::cudaSuccess);
175
176 if (N == 0) {
177 return errs;
178 }
179
180 const auto stream_array =
181 std::array<::cudaStream_t, sizeof...(StreamTs)>{{streams...}};
182 auto context = detail::multi_stream_context(N);
183 auto items = std::array<detail::multi_stream_item, N>{};
184
185 ::tbb::task::suspend([&](::tbb::task::suspend_point tag) {
186 context.suspend_point = tag;
187 for (std::size_t i = 0; i < N; ++i) {
188 items.at(i) = {&context, &errs.at(i)};
189 detail::register_callback(stream_array.at(i), items.at(i),
190 sync_mode);
191 }
192 });
193
194 return errs;
195}
196
203template <typename... StreamTs>
204COOPSYNC_TBB_CUDA_NODISCARD static inline std::array<::cudaError_t,
205 sizeof...(StreamTs)>
206wait_for_all(StreamTs... streams) {
207 return wait_for_all(0u, streams...);
208}
209
226template <typename ForwardIt, typename OutputIt>
227static inline OutputIt wait_for_range(ForwardIt first, ForwardIt last,
228 OutputIt out,
229 unsigned int sync_mode = 0u) {
230 static_assert(
231 detail::is_cuda_stream<
232 typename std::iterator_traits<ForwardIt>::value_type>::value,
233 "wait_for_range(first,last,out) requires cudaStream_t values");
234
235 if (first == last) {
236 return out;
237 }
238
239 const auto n = static_cast<std::size_t>(std::distance(first, last));
240 auto context = detail::multi_stream_context(n);
241 auto errs = std::vector<::cudaError_t>(n, ::cudaSuccess);
242 auto items = std::vector<detail::multi_stream_item>(n);
243
244 ::tbb::task::suspend([&](::tbb::task::suspend_point tag) {
245 context.suspend_point = tag;
246 std::size_t i = 0;
247 for (auto it = first; it != last; ++it, ++i) {
248 // NOLINTBEGIN(cppcoreguidelines-pro-bounds-avoid-unchecked-container-access)
249 items[i] = {&context, &errs[i]};
250 detail::register_callback(*it, items[i], sync_mode);
251 // NOLINTEND(cppcoreguidelines-pro-bounds-avoid-unchecked-container-access)
252 }
253 });
254
255 return std::copy(errs.begin(), errs.end(), out);
256}
257} // namespace cuda
258} // namespace coopsync_tbb
259#undef COOPSYNC_TBB_CUDA_NODISCARD
CUDA integration.
Definition wait_for.hpp:49
static COOPSYNC_TBB_CUDA_NODISCARD std::array<::cudaError_t, sizeof...(StreamTs)> wait_for_all(unsigned int sync_mode, StreamTs... streams)
Suspends the current TBB task until all the work in all the provided CUDA streams is completed....
Definition wait_for.hpp:168
static COOPSYNC_TBB_CUDA_NODISCARDinline ::cudaError_t wait_for(::cudaStream_t stream, unsigned int sync_mode=0u)
Suspends the current TBB task until all the work in a CUDA stream completes. Internally a CUDA callba...
Definition wait_for.hpp:141
static OutputIt wait_for_range(ForwardIt first, ForwardIt last, OutputIt out, unsigned int sync_mode=0u)
Suspends the current TBB task until all the work in all the provided CUDA streams completes....
Definition wait_for.hpp:227
Main namespace of the library, containing all public APIs.
Definition wait_for.hpp:11