Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
ResidualDot.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"
25#include <functional>
26
28
29 template<class T>
31 auto edges = get_edges();
32
33 Tscal loc_val = {};
34
35 // floating-point addition is not associative: accumulate the patches in a fixed order
36 edges.spans_phi_res.get_refs().for_each_ordered(
37 [&](u64 id_patch, const std::reference_wrapper<PatchDataField<T>> &field_ref) {
38 PatchDataField<T> &field = field_ref.get();
39 u32 val_cnt = edges.sizes.indexes.get(id_patch) * nvar;
40
41 // the field may hold more values than summed (ghost blocks), but never less
42 if (field.get_val_cnt() < val_cnt) {
44 "field too small on patch {}: expected at least {} values, got {}",
45 id_patch,
46 val_cnt,
47 field.get_val_cnt()));
48 }
49
50 loc_val += shamalgs::primitives::dot_sum(field.get_buf(), 0, val_cnt);
51 });
52
53 edges.res_ddot.data = shamalgs::collective::allreduce_sum(loc_val);
54 }
55
56 template<class T>
57 std::string ResidualDot<T>::_impl_get_tex() const {
58 auto field = get_ro_edge_base(1).get_tex_symbol();
59 auto residual_dot = get_rw_edge_base(0).get_tex_symbol();
61 std::string tex = R"tex(
62 Compute L2-norm squared of residual vector
63 \begin{equation}
64 {residual_dot} &= \sum_{i \in [0,N_{field})} {field}_i \cdot {field}_i
65 \end{equation}
66 )tex";
67
68 shambase::replace_all(tex, "{field}", field);
69 shambase::replace_all(tex, "{residual_dot}", residual_dot);
70
71 return tex;
72 }
73
74} // namespace shammodels::basegodunov::modules
75
Squared L2 norm of a field, summed 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 res_ddot <- sum_i <res_i, res_i> over the first sizes * nvar values of every patch on every r...
virtual std::string _impl_get_tex() const
get the tex of the node
IEdge & get_rw_edge_base(int slot)
Get a reference to a read write edge and cast it to the type IEdge.
Definition INode.hpp:147
const IEdge & get_ro_edge_base(int slot)
Get a reference to a read only edge.
Definition INode.hpp:138
Provides functions to compute the sum of dot products of elements in a device buffer with themselves.
This header file contains utility functions related to exception handling in the code.
shambase::VecComponent< T > dot_sum(sham::DeviceBuffer< T > &buf1, u32 start_id, u32 end_id)
Compute the sum of dot products of elements in a device buffer with themselves.
Definition dot_sum.cpp:26
void replace_all(std::string &inout, std::string_view what, std::string_view with)
replace all occurence of a search string with another
Definition string.hpp:106
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
namespace for the basegodunov model modules