49 : std::is_same<typename std::decay<T>::type, ::hipStream_t> {};
55struct all_true : std::is_same<bool_pack<Bs..., true>, bool_pack<true, Bs...>> {
58template <
typename... Ts>
59struct all_hip_stream : all_true<is_hip_stream<Ts>::value...> {};
61template <
typename Context>
62static void resumption_host_func(
void* self) {
63 static_cast<Context*
>(self)->notify(::hipSuccess);
66template <
typename Context>
67static void resumption_callback(::hipStream_t, ::hipError_t status,
69 static_cast<Context*
>(self)->notify(status);
72template <
typename Context>
73static inline void register_callback(::hipStream_t stream, Context& context) {
74#if COOPSYNC_TBB_HIP_HAS_HOST_FUNC_
76 ::hipLaunchHostFunc(stream, &resumption_host_func<Context>, &context);
78 const auto err = ::hipStreamAddCallback(
79 stream, &resumption_callback<Context>, &context, 0);
81 if (err != ::hipSuccess) {
86struct single_stream_context {
87 void notify(::hipError_t status) {
89 ::tbb::task::resume(suspend_point);
92 ::tbb::task::suspend_point suspend_point{};
93 ::hipError_t err{::hipSuccess};
96struct multi_stream_context {
97 explicit multi_stream_context(std::size_t n) : pending(n) {}
100 if (pending.fetch_sub(1, std::memory_order_acq_rel) == 1) {
101 ::tbb::task::resume(suspend_point);
105 ::tbb::task::suspend_point suspend_point{};
106 std::atomic<std::size_t> pending;
109struct multi_stream_item {
110 multi_stream_context* context{
nullptr};
111 ::hipError_t* out{
nullptr};
113 void notify(::hipError_t status) {
115 context->notify_one();
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);
145template <
typename... StreamTs>
146COOPSYNC_TBB_HIP_NODISCARD
static inline std::array<::hipError_t,
149 static_assert(detail::all_hip_stream<StreamTs...>::value,
150 "wait_for_all(streams...) requires hipStream_t arguments");
152 const std::size_t N =
sizeof...(StreamTs);
153 auto errs = std::array<::hipError_t,
sizeof...(StreamTs)>{};
154 errs.fill(::hipSuccess);
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>{};
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));
188template <
typename ForwardIt,
typename OutputIt>
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");
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);
205 ::tbb::task::suspend([&](::tbb::task::suspend_point tag) {
206 context.suspend_point = tag;
208 for (
auto it = first; it != last; ++it, ++i) {
210 items[i] = {&context, &errs[i]};
211 detail::register_callback(*it, items[i]);
216 return std::copy(errs.begin(), errs.end(), out);
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