Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
NodeSumReduction.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
18
20#include "shambase/string.hpp"
26
27namespace shamrock::solvergraph {
28
29 template<class T>
31 auto edges = get_edges();
32
33 edges.spans_in.check_sizes(edges.sizes.indexes);
34
35 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
36
37 T loc_val = {};
38
39 // floating-point addition is not associative: accumulate the patches in a fixed order
40 edges.spans_in.get_refs().for_each_ordered(
41 [&](u64 id_patch, const std::reference_wrapper<PatchDataField<T>> &field_ref) {
42 const PatchDataField<T> &field = field_ref.get();
43 u32 val_cnt = edges.sizes.indexes.get(id_patch) * nvar;
44
45 // the field may hold more values than summed (e.g. ghost blocks stored after the
46 // patch blocks), but never less
47 // TODO: this is only allowed to keep the behaviour of the self-gravity solver of
48 // #1942, whose CG/BiCGSTAB loops pass sizes without ghost zones with fields that
49 // include them. Check how this impacts the self-gravity solver, then change it to
50 // pass sizes matching the field (e.g. a field without the ghost blocks) and
51 // require an exact size match here.
52 if (field.get_val_cnt() < val_cnt) {
54 "field too small on patch {}: expected at least {} values, got {}",
55 id_patch,
56 val_cnt,
57 field.get_val_cnt()));
58 }
59
60 if (val_cnt == 0) {
61 return;
62 }
63
64 loc_val += shamalgs::primitives::sum(dev_sched, field.get_buf(), 0, val_cnt);
65 });
66
67 edges.out_scal.data = shamalgs::collective::allreduce_sum(loc_val);
68 }
69
70 template<class T>
72 std::string tex = R"tex(
73 Global sum reduction
74 \begin{equation}
75 {out_scal} = \sum_{i} {spans_in}_i
76 \end{equation}
77 )tex";
78
79 replace_edges_tex_symbols(tex);
80
81 return tex;
82 }
83
84} // namespace shamrock::solvergraph
85
Header file describing a Node Instance.
Node computing the global sum of a field over all patches and ranks.
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
u32 get_val_cnt() const
Get the number of values stored in the field.
Compute the sum of spans_in over the first sizes * nvar values of every patch on every rank.
virtual std::string _impl_get_tex() const
get the tex of the node
void _impl_evaluate_internal()
evaluate the node
This header file contains utility functions related to exception handling in the code.
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.
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.