Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
is_all_true.cpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
15
18#include "shambase/memory.hpp"
26
27namespace {
28
29 template<class T>
30 bool is_all_true_host(sham::DeviceBuffer<T> &buf, u32 cnt) {
31
32 {
33 auto tmp = buf.copy_to_stdvec();
34
35 for (u32 i = 0; i < cnt; i++) {
36 if (tmp[i] == 0) {
37 return false;
38 }
39 }
40 }
41
42 return true;
43 }
44
45 template<class T>
46 bool is_all_true_sum_reduction(sham::DeviceBuffer<T> &buf, u32 cnt) {
47
48 if (cnt == 0) {
49 return true;
50 }
51
52 auto dev_sched = buf.get_dev_scheduler_ptr();
53
54 sham::DeviceBuffer<u32> tmp(cnt, dev_sched);
55
57 shambase::get_check_ref(dev_sched).get_queue(),
58 sham::MultiRef{buf},
59 sham::MultiRef{tmp},
60 cnt,
61 [](u32 i, const T *in, u32 *out) {
62 out[i] = in[i] != 0;
63 });
64
65 auto count_true = shamalgs::primitives::sum(dev_sched, tmp, 0, cnt);
66
67 return count_true == cnt;
68 }
69
70 template<class T>
71 bool is_all_true_early_group_exit(sham::DeviceBuffer<T> &buf, u32 cnt, u32 group_size) {
72
73 if (cnt == 0) {
74 return true;
75 }
76
77 auto dev_sched = buf.get_dev_scheduler_ptr();
78 auto &q = dev_sched->get_queue();
79
80 sham::DeviceBuffer<u32> stop_flag(1, dev_sched);
81 stop_flag.fill(0);
82
83 /*
84 // To test to further optimize we can do something like:
85 // (i tried and only got +30% which good but less than i expected)
86 // A.K.A as something to do in another PR or never or I spam Claude on it
87
88 auto range = sham::make_ndrange(group_size, (cnt + 3) / 4);
89
90 <...>
91
92 // fetch the u8 4 by 4, complete with 0x01 (s) if idx + 4 > cnt
93 auto fetch_4i8 = [ptr = buf, cnt](u32 idx) -> u32 {
94 if (idx + 4 <= cnt)
95 return *reinterpret_cast<const u32 *>(ptr + idx);
96
97 u32 v = 0;
98 u32 i = 0;
99#pragma unroll
100 for (; idx + i < cnt; ++i)
101 v |= u32(ptr[idx + i]) << (i * 8);
102 if (i < 4)
103 v |= u32(0x01) << (i * 8);
104 return v;
105 };
106
107 u32 gid = item.get_global_linear_id();
108
109 // if there are
110 bool local = (gid < cnt) ? (fetch_4i8(gid * 4) == 0x01010101) : true;
111 */
112
113 // TODO: switch to the check version when available
114 auto range = sham::make_ndrange(group_size, cnt);
115
116 sham::kernel_call_hndl(
117 q,
118 sham::MultiRef{buf},
119 sham::MultiRef{stop_flag},
120 u32{1}, // TODO that when we have the new variant without it
121 [=](u32, const T *buf, u32 *stop) {
122 return [=](sycl::handler &cgh) {
123 cgh.parallel_for(range, [=](sycl::nd_item<1> item) {
124 auto grp = item.get_group();
125 u32 lid = item.get_local_linear_id();
126
127 // Only the group leader reads the stop flag from device memory,
128 // then broadcast that single value to the rest of the group instead
129 // of every work-item issuing its own global memory load.
130 u32 stop_val = sycl::group_broadcast(grp, (lid == 0) ? *stop : u32{0}, 0);
131
132 // early exit the whole group if the flag is set
133 if (stop_val) {
134 return;
135 }
136
137 u32 gid = item.get_global_linear_id();
138
139 bool local = (gid < cnt) ? (buf[gid] != 0) : true;
140
141 // reduce in lid==0 the sum of local
142 bool result = sycl::all_of_group(grp, local);
143
144 if (lid == 0) {
145 // if there is a false we set the stop flag
146 if (!result && !(*stop)) {
147 sycl::atomic_ref<
148 u32,
149 sycl::memory_order_relaxed,
150 sycl::memory_scope_device,
151 sycl::access::address_space::global_space>
152 atom(*stop);
153 atom |= 1_u32;
154 }
155 }
156 });
157 };
158 });
159
160 return stop_flag.get_val_at_idx(0) == 0;
161 }
162
163} // namespace
164
166
168 struct Host {
169 static constexpr std::string_view variant_type_name = "host";
170 };
171
174 static constexpr std::string_view variant_type_name = "sum_reduction";
175 };
176
179 static constexpr std::string_view variant_type_name = "atomic_early_exit";
180 u32 group_size = 256;
181
184 static std::vector<AtomicEarlyExit> variant_custom_defaults() {
185 return {AtomicEarlyExit{64}, AtomicEarlyExit{256}};
186 }
187 };
188} // namespace shamalgs::primitives::impl
189
190template<>
192 static nlohmann::json to_json(const shamalgs::primitives::impl::AtomicEarlyExit &p) {
193 return {{"group_size", p.group_size}};
194 }
195 static shamalgs::primitives::impl::AtomicEarlyExit from_json(const nlohmann::json &j) {
197 if (j.contains("group_size")) {
198 p.group_size = j.at("group_size").get<u32>();
199 }
200 return p;
201 }
202};
203
204namespace shamalgs::primitives {
205
207 namespace impl {
208
210 constexpr std::string_view is_all_true_impl_name = "is_all_true";
211
213 [](const sham::DeviceScheduler_ptr &, auto &self) {
214 self.set(Host{});
215 }};
216
217 // Must come after the global it registers: same TU, so it is initialized after it
219
220 } // namespace impl
221
222 template<class T>
224
225 if (!impl::is_all_true_impl.is_set()) {
228 }
229
230 return std::visit(
232 [&](impl::Host) {
233 return is_all_true_host(buf, cnt);
234 },
235 [&](impl::SumReduction) {
236 return is_all_true_sum_reduction(buf, cnt);
237 },
238 [&](impl::AtomicEarlyExit cfg) {
239 return is_all_true_early_group_exit(buf, cnt, cfg.group_size);
240 },
241 },
242 impl::is_all_true_impl.get());
243 }
244
245 template bool is_all_true(sham::DeviceBuffer<u8> &buf, u32 cnt);
246
247} // namespace shamalgs::primitives
248
249template<class T>
250bool shamalgs::primitives::is_all_true(sycl::buffer<T> &buf, u32 cnt) {
251
252 // TODO do it on GPU pleeeaze
253 {
254 sycl::host_accessor acc{buf, sycl::read_only};
255
256 for (u32 i = 0; i < cnt; i++) {
257 if (acc[i] == 0) {
258 return false;
259 }
260 }
261 }
262
263 return true;
264}
265
266template bool shamalgs::primitives::is_all_true(sycl::buffer<u8> &buf, u32 cnt);
Generic std::variant-based implementation selector.
std::uint32_t u32
32 bit unsigned integer
A buffer allocated in USM (Unified Shared Memory).
std::shared_ptr< DeviceScheduler > & get_dev_scheduler_ptr()
Gets the Device scheduler pointer corresponding to the held allocation.
std::vector< T > copy_to_stdvec() const
Copy the content of the buffer to a std::vector.
Drop-in replacement for the hand-rolled "global variable + enum + name mapping.
Name-keyed registry of the implementation selectors (IImplVariant) of every algorithm.
#define SHAMALGS_REGISTER_IMPL(name, impl)
Register the implementation selector impl under name at static initialization.
void autoselect_impl(std::string_view alg, const sham::DeviceScheduler_ptr &sched)
Select the default implementation of alg for the device behind sched.
Boolean reduction algorithm for checking if all elements are non-zero.
sycl::nd_range< 1 > make_ndrange(u32 wg_size, u32 nthread)
void kernel_call(sham::DeviceQueue &q, RefIn in, RefOut in_out, u32 n, Functor &&func, SourceLocation &&callsite=SourceLocation{})
Submit a kernel to a SYCL queue.
namespace to control implementation behavior
constexpr std::string_view is_all_true_impl_name
Registry name, shared by its registration and the dispatch site(s).
namespace for primitive algorithm (e.g. sort, scan, reductions, ...)
T sum(const sham::DeviceScheduler_ptr &sched, const sham::DeviceBuffer< T > &buf1, u32 start_id, u32 end_id)
Compute the sum of elements in a device buffer within a specified range.
bool is_all_true(sycl::buffer< T > &buf, u32 cnt)
Check if all elements in a sycl::buffer are non-zero.
namespace to contain everything implemented by shamalgs
Definition algorithm.hpp:21
T & get_check_ref(const std::unique_ptr< T > &ptr, SourceLocation loc=SourceLocation())
Takes a std::unique_ptr and returns a reference to the object it holds. It throws a std::runtime_erro...
Definition memory.hpp:112
A class that references multiple buffers or similar objects.
Definition MultiRef.hpp:33
Customization point controlling how an alternative's fields (if any) are serialized to / parsed from ...
static Alt from_json(const nlohmann::json &)
Parse the alternative's fields back (default: no fields, ignored).
static nlohmann::json to_json(const Alt &)
Serialize the alternative's fields (default: no fields, empty object).
Check all elements via a sum reduction on device.
static std::vector< AtomicEarlyExit > variant_custom_defaults()
Check all elements on host after copying the buffer back.
Check all elements via a sum reduction on device.
Build an overload set out of several callables, for use with std::visit.