Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
pyCommonUtils.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
25#include "shamcomm/logs.hpp"
29#include <pybind11/cast.h>
30#include <pybind11/complex.h>
31#include <shambackends/sycl.hpp>
32#include <vector>
33
34// Define the operator += for sham::DeviceBuffer, implementation to be done later.
35namespace sham {
36 template<typename T>
37 DeviceBuffer<T> &operator+=(DeviceBuffer<T> &lhs, const DeviceBuffer<T> &rhs) {
39 rhs.get_queue(),
40 sham::MultiRef{rhs},
41 sham::MultiRef{lhs},
42 lhs.get_size(),
43 [](u32 n, const T *rhs, T *lhs) {
44 lhs[n] += rhs[n];
45 });
46 return lhs;
47 }
48
49 template<typename T>
50 DeviceBuffer<T> &operator/=(DeviceBuffer<T> &lhs, const DeviceBuffer<T> &rhs) {
52 rhs.get_queue(),
53 sham::MultiRef{rhs},
54 sham::MultiRef{lhs},
55 lhs.get_size(),
56 [](u32 n, const T *rhs, T *lhs) {
57 auto r = rhs[n];
58 if (r != 0) {
59 lhs[n] /= r;
60 } else {
61 lhs[n] = std::numeric_limits<f64>::quiet_NaN();
62 }
63 });
64 return lhs;
65 }
66
67} // namespace sham
68
70 auto &m = root_module;
71
72 py::enum_<shammodels::NeighCacheStrategy>(
73 m,
74 "NeighCacheStrategy",
75 R"==(
76 Strategy used to build the neighbours cache out of the tree traversal.
77
78 Usage
79 -----
80 >>> from shamrock import NeighCacheStrategy
81 >>> cfg.set_neigh_cache_strategy(NeighCacheStrategy.SingleStage)
82)==")
83 .value(
84 "SingleStage",
86 R"==(
87 Single tree traversal per particle.
88
89 Each particle walks the tree itself and writes its neighbours straight to the
90 cache. Prefer this one when the tree ends up with giant leaves, as on a chaotic
91 disc: there the leaf bounding boxes grow so large that the two stage search makes
92 each particle scan far more candidates than it keeps.
93)==")
94 .value(
95 "TwoStage",
97 R"==(
98 Two stage neighbours search (see the shamrock paper). This is the default.
99
100 A first pass walks the tree once per leaf to build a leaf to leaf neighbour map,
101 then each particle only scans the particles held by its own leaf's neighbour
102 leaves. This is usually the faster of the two, since the tree traversal is paid
103 once per leaf instead of once per particle.
104)==")
105 .value(
106 "SingleStageSharedOffload",
108 R"==(
109 Single tree traversal per particle, shared memory offload variant.
110
111 Same algorithm as SingleStage, but the tree traversal stack is placed in a
112 work-group local memory allocation (one private slice per work-item) instead of a
113 per-work-item std::array, and the kernel is launched over nd_range work-groups.
114)==")
115 .value(
116 "TwoStageSharedOffload",
118 R"==(
119 Two stage neighbours search, shared memory offload variant.
120
121 Same algorithm as TwoStage, but every tree traversal stack (leaf to leaf search
122 and particle parent-leaf search) is placed in work-group local memory instead of a
123 per-work-item std::array.
124)==");
125
126 m.def(
127 "compute_histogram",
128 [](std::vector<f64> bin_edges,
131 bool do_average) -> std::vector<f64> {
132 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
133
134 u32 nx = bin_edges.size() - 1;
135 std::vector<f64> bin_edge_inf(nx);
136 std::vector<f64> bin_edge_sup(nx);
137
138 for (size_t i = 0; i < nx; i++) {
139 bin_edge_inf[i] = bin_edges[i];
140 bin_edge_sup[i] = bin_edges[i + 1];
141 }
142
143 sham::DeviceBuffer<f64> ret(bin_edge_inf.size(), dev_sched);
144 ret.fill(0);
145
146 sham::DeviceBuffer<f64> bin_inf(bin_edge_inf.size(), dev_sched);
147 sham::DeviceBuffer<f64> bin_sup(bin_edge_inf.size(), dev_sched);
148 bin_inf.copy_from_stdvec(bin_edge_inf);
149 bin_sup.copy_from_stdvec(bin_edge_sup);
150
151 shambase::DistributedData<u32> obj_cnts = x_field.get_obj_cnts();
152
153 obj_cnts.for_each([&](u64 id_patch, const unsigned int &obj_cnt) {
154 ret += shamalgs::primitives::compute_histogram<f64>(
155 dev_sched,
156 bin_inf,
157 bin_sup,
158 obj_cnt,
159 [](const f64 &bin_edge_inf,
160 const f64 &bin_edge_sup,
161 const f64 &x_val,
162 const f64 &y_val,
163 bool &has_value) {
164 has_value = x_val >= bin_edge_inf && x_val < bin_edge_sup;
165 return has_value ? y_val : 0;
166 },
167 x_field.get_buf(id_patch),
168 y_field.get_buf(id_patch));
169 });
170
171 shamalgs::collective::reduce_buffer_in_place_sum(ret, MPI_COMM_WORLD);
172
173 if (do_average) {
174
175 sham::DeviceBuffer<f64> norm(bin_edge_inf.size(), dev_sched);
176 norm.fill(0);
177
178 obj_cnts.for_each([&](u64 id_patch, const unsigned int &obj_cnt) {
179 sham::DeviceBuffer<f64> unit_buf(obj_cnt, dev_sched);
180 unit_buf.fill(1);
181
182 norm += shamalgs::primitives::compute_histogram<f64>(
183 dev_sched,
184 bin_inf,
185 bin_sup,
186 obj_cnt,
187 [](const f64 &bin_edge_inf,
188 const f64 &bin_edge_sup,
189 const f64 &x_val,
190 const f64 &y_val,
191 bool &has_value) {
192 has_value = x_val >= bin_edge_inf && x_val < bin_edge_sup;
193 return has_value ? y_val : 0;
194 },
195 x_field.get_buf(id_patch),
196 unit_buf);
197 });
198
199 shamalgs::collective::reduce_buffer_in_place_sum(norm, MPI_COMM_WORLD);
200
201 ret /= norm;
202 }
203
204 return ret.copy_to_stdvec();
205 },
206 py::kw_only{},
207 py::arg("bin_edges"),
208 py::arg("x_field"),
209 py::arg("y_field"),
210 py::arg("do_average") = false);
211
212 m.def(
213 "compute_histogram_convolve_x",
214 [](std::vector<f64> bin_edges,
218 bool do_average) -> std::vector<f64> {
219 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
220
221 u32 nx = bin_edges.size() - 1;
222 std::vector<f64> bin_edge_inf(nx);
223 std::vector<f64> bin_edge_sup(nx);
224
225 for (size_t i = 0; i < nx; i++) {
226 bin_edge_inf[i] = bin_edges[i];
227 bin_edge_sup[i] = bin_edges[i + 1];
228 }
229
230 sham::DeviceBuffer<f64> ret(bin_edge_inf.size(), dev_sched);
231 ret.fill(0);
232
233 sham::DeviceBuffer<f64> bin_inf(bin_edge_inf.size(), dev_sched);
234 sham::DeviceBuffer<f64> bin_sup(bin_edge_inf.size(), dev_sched);
235 bin_inf.copy_from_stdvec(bin_edge_inf);
236 bin_sup.copy_from_stdvec(bin_edge_sup);
237
238 shambase::DistributedData<u32> obj_cnts = x_field.get_obj_cnts();
239
240 obj_cnts.for_each([&](u64 id_patch, const unsigned int &obj_cnt) {
241 ret += shamalgs::primitives::compute_histogram<f64>(
242 dev_sched,
243 bin_inf,
244 bin_sup,
245 obj_cnt,
246 [](const f64 &bin_edge_inf,
247 const f64 &bin_edge_sup,
248 const f64 &x_val,
249 const f64 &y_val,
250 const f64 &size_val,
251 bool &has_value) {
252 has_value
253 = x_val >= bin_edge_inf - size_val && x_val < bin_edge_sup + size_val;
254 return has_value ? y_val : 0;
255 },
256 x_field.get_buf(id_patch),
257 y_field.get_buf(id_patch),
258 size_field.get_buf(id_patch));
259 });
260
261 shamalgs::collective::reduce_buffer_in_place_sum(ret, MPI_COMM_WORLD);
262
263 if (do_average) {
264
265 sham::DeviceBuffer<f64> norm(bin_edge_inf.size(), dev_sched);
266 norm.fill(0);
267
268 obj_cnts.for_each([&](u64 id_patch, const unsigned int &obj_cnt) {
269 sham::DeviceBuffer<f64> unit_buf(obj_cnt, dev_sched);
270 unit_buf.fill(1);
271
272 norm += shamalgs::primitives::compute_histogram<f64>(
273 dev_sched,
274 bin_inf,
275 bin_sup,
276 obj_cnt,
277 [](const f64 &bin_edge_inf,
278 const f64 &bin_edge_sup,
279 const f64 &x_val,
280 const f64 &y_val,
281 const f64 &size_val,
282 bool &has_value) {
283 has_value = x_val >= bin_edge_inf - size_val
284 && x_val < bin_edge_sup + size_val;
285 return has_value ? y_val : 0;
286 },
287 x_field.get_buf(id_patch),
288 unit_buf,
289 size_field.get_buf(id_patch));
290 });
291
292 shamalgs::collective::reduce_buffer_in_place_sum(norm, MPI_COMM_WORLD);
293
294 ret /= norm;
295 }
296
297 return ret.copy_to_stdvec();
298 },
299 py::kw_only{},
300 py::arg("bin_edges"),
301 py::arg("x_field"),
302 py::arg("y_field"),
303 py::arg("size_field"),
304 py::arg("do_average") = false);
305
306 m.def(
307 "compute_histogram_2d",
308 [](std::vector<f64> bin_edges_x,
309 std::vector<f64> bin_edges_y,
311 shamrock::solvergraph::Field<f64> &y_field) -> std::vector<u64> {
312 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
313
314 u32 nx = bin_edges_x.size() - 1;
315 u32 ny = bin_edges_y.size() - 1;
316
317 sham::DeviceBuffer<u64> ret(nx * ny, dev_sched);
318 ret.fill(0);
319
320 shambase::DistributedData<u32> obj_cnts = x_field.get_obj_cnts();
321
322 sham::DeviceBuffer<f64> binsx(bin_edges_x.size(), dev_sched);
323 sham::DeviceBuffer<f64> binsy(bin_edges_y.size(), dev_sched);
324 binsx.copy_from_stdvec(bin_edges_x);
325 binsy.copy_from_stdvec(bin_edges_y);
326
327 obj_cnts.for_each([&](u64 id_patch, const unsigned int &obj_cnt) {
329 dev_sched->get_queue(),
331 binsx, binsy, x_field.get_buf(id_patch), y_field.get_buf(id_patch)},
332 sham::MultiRef{ret},
333 obj_cnt,
334 [nx, ny](
335 u32 id,
336 const f64 *__restrict x_bins,
337 const f64 *__restrict y_bins,
338 const f64 *__restrict x_field,
339 const f64 *__restrict y_field,
340 u64 *__restrict pic) {
341 auto get_pic_coord = [&](u32 ix, u32 iy) {
342 return ix + iy * nx;
343 };
344
345 f64 x_val = x_field[id];
346 f64 y_val = y_field[id];
347
348 bool is_in_x_range = x_bins[0] <= x_val && x_val <= x_bins[nx];
349 bool is_in_y_range = y_bins[0] <= y_val && y_val <= y_bins[ny];
350
351 if (!(is_in_x_range && is_in_y_range)) {
352 return;
353 }
354
356 x_bins, 0, nx + 1, x_val);
358 y_bins, 0, ny + 1, y_val);
359
360 if (ix >= nx || iy >= ny) {
361 return;
362 }
363
364 using atomic_ref_T = sycl::atomic_ref<
365 u64,
366 sycl::memory_order_relaxed,
367 sycl::memory_scope_device,
368 sycl::access::address_space::global_space>;
369
370 atomic_ref_T pic_ref(pic[get_pic_coord(ix, iy)]);
371 pic_ref++;
372 });
373 });
374
375 shamalgs::collective::reduce_buffer_in_place_sum(ret, MPI_COMM_WORLD);
376
377 return ret.copy_to_stdvec();
378 },
379 py::kw_only{},
380 py::arg("bin_edges_x"),
381 py::arg("bin_edges_y"),
382 py::arg("x_field"),
383 py::arg("y_field"));
384}
Header file describing a Node Instance.
double f64
Alias for double.
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
A buffer allocated in USM (Unified Shared Memory).
Represents a collection of objects distributed across patches identified by a u64 id.
void for_each(std::function< void(u64, T &)> &&f)
Applies a function to each object in the collection.
Neighbour cache build strategy enum + json serialization/deserialization.
namespace for backends this one is named only sham since shambackends is too long to write
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.
constexpr u32 binary_search_upper_bound(const Tkey *__restrict__ key, u32 first, u32 last, const Tkey &value)
GPU compatible implementation of std::upper_bound.
@ SingleStage
Single tree traversal per particle.
@ TwoStage
Two stage neighbours search (see shamrock paper).
Pybind11 include and definitions.
#define ON_PYTHON_INIT
Register a Python module init function using static initialization.
A class that references multiple buffers or similar objects.
Definition MultiRef.hpp:33
GPU compatible implementation of std::upper_bound.