29#include <pybind11/cast.h>
30#include <pybind11/complex.h>
43 [](
u32 n,
const T *rhs, T *lhs) {
56 [](
u32 n,
const T *rhs, T *lhs) {
61 lhs[n] = std::numeric_limits<f64>::quiet_NaN();
70 auto &m = root_module;
72 py::enum_<shammodels::NeighCacheStrategy>(
76 Strategy used to build the neighbours cache out of the tree traversal.
80 >>> from shamrock import NeighCacheStrategy
81 >>> cfg.set_neigh_cache_strategy(NeighCacheStrategy.SingleStage)
87 Single tree traversal per particle.
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.
98 Two stage neighbours search (see the shamrock paper). This is the default.
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.
106 "SingleStageSharedOffload",
109 Single tree traversal per particle, shared memory offload variant.
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.
116 "TwoStageSharedOffload",
119 Two stage neighbours search, shared memory offload variant.
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.
128 [](std::vector<f64> bin_edges,
131 bool do_average) -> std::vector<f64> {
132 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
134 u32 nx = bin_edges.size() - 1;
135 std::vector<f64> bin_edge_inf(nx);
136 std::vector<f64> bin_edge_sup(nx);
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];
148 bin_inf.copy_from_stdvec(bin_edge_inf);
149 bin_sup.copy_from_stdvec(bin_edge_sup);
153 obj_cnts.
for_each([&](
u64 id_patch,
const unsigned int &obj_cnt) {
154 ret += shamalgs::primitives::compute_histogram<f64>(
159 [](
const f64 &bin_edge_inf,
160 const f64 &bin_edge_sup,
164 has_value = x_val >= bin_edge_inf && x_val < bin_edge_sup;
165 return has_value ? y_val : 0;
167 x_field.get_buf(id_patch),
168 y_field.get_buf(id_patch));
171 shamalgs::collective::reduce_buffer_in_place_sum(ret, MPI_COMM_WORLD);
178 obj_cnts.
for_each([&](
u64 id_patch,
const unsigned int &obj_cnt) {
182 norm += shamalgs::primitives::compute_histogram<f64>(
187 [](
const f64 &bin_edge_inf,
188 const f64 &bin_edge_sup,
192 has_value = x_val >= bin_edge_inf && x_val < bin_edge_sup;
193 return has_value ? y_val : 0;
195 x_field.get_buf(id_patch),
199 shamalgs::collective::reduce_buffer_in_place_sum(norm, MPI_COMM_WORLD);
204 return ret.copy_to_stdvec();
207 py::arg(
"bin_edges"),
210 py::arg(
"do_average") =
false);
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();
221 u32 nx = bin_edges.size() - 1;
222 std::vector<f64> bin_edge_inf(nx);
223 std::vector<f64> bin_edge_sup(nx);
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];
235 bin_inf.copy_from_stdvec(bin_edge_inf);
236 bin_sup.copy_from_stdvec(bin_edge_sup);
240 obj_cnts.
for_each([&](
u64 id_patch,
const unsigned int &obj_cnt) {
241 ret += shamalgs::primitives::compute_histogram<f64>(
246 [](
const f64 &bin_edge_inf,
247 const f64 &bin_edge_sup,
253 = x_val >= bin_edge_inf - size_val && x_val < bin_edge_sup + size_val;
254 return has_value ? y_val : 0;
256 x_field.get_buf(id_patch),
257 y_field.get_buf(id_patch),
258 size_field.get_buf(id_patch));
261 shamalgs::collective::reduce_buffer_in_place_sum(ret, MPI_COMM_WORLD);
268 obj_cnts.
for_each([&](
u64 id_patch,
const unsigned int &obj_cnt) {
272 norm += shamalgs::primitives::compute_histogram<f64>(
277 [](
const f64 &bin_edge_inf,
278 const f64 &bin_edge_sup,
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;
287 x_field.get_buf(id_patch),
289 size_field.get_buf(id_patch));
292 shamalgs::collective::reduce_buffer_in_place_sum(norm, MPI_COMM_WORLD);
297 return ret.copy_to_stdvec();
300 py::arg(
"bin_edges"),
303 py::arg(
"size_field"),
304 py::arg(
"do_average") =
false);
307 "compute_histogram_2d",
308 [](std::vector<f64> bin_edges_x,
309 std::vector<f64> bin_edges_y,
312 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
314 u32 nx = bin_edges_x.size() - 1;
315 u32 ny = bin_edges_y.size() - 1;
324 binsx.copy_from_stdvec(bin_edges_x);
325 binsy.copy_from_stdvec(bin_edges_y);
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)},
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) {
345 f64 x_val = x_field[
id];
346 f64 y_val = y_field[
id];
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];
351 if (!(is_in_x_range && is_in_y_range)) {
356 x_bins, 0, nx + 1, x_val);
358 y_bins, 0, ny + 1, y_val);
360 if (ix >= nx || iy >= ny) {
364 using atomic_ref_T = sycl::atomic_ref<
366 sycl::memory_order_relaxed,
367 sycl::memory_scope_device,
368 sycl::access::address_space::global_space>;
370 atomic_ref_T pic_ref(pic[get_pic_coord(ix, iy)]);
375 shamalgs::collective::reduce_buffer_in_place_sum(ret, MPI_COMM_WORLD);
377 return ret.copy_to_stdvec();
380 py::arg(
"bin_edges_x"),
381 py::arg(
"bin_edges_y"),
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.
@ SingleStageSharedOffload
@ 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.
GPU compatible implementation of std::upper_bound.