Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
CGInit.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
17
22#include "shambackends/vec.hpp"
26#include <shambackends/sycl.hpp>
27#include <type_traits>
28
30
31namespace {
32 using Direction = shammodels::basegodunov::modules::Direction;
33
34 template<class Tvec, class TgridVec>
35 class _Kernel {
36 using Tscal = shambase::VecComponent<Tvec>;
37 using OrientedAMRGraph = shammodels::basegodunov::modules::OrientedAMRGraph<Tvec, TgridVec>;
38 using AMRGraph = shammodels::basegodunov::modules::AMRGraph;
39 using Edges = typename shammodels::basegodunov::modules::CGInit<Tvec, TgridVec>::Edges;
40
41 public:
42 inline static void kernel(Edges &edges, u32 block_size, Tscal fourPiG) {
43 edges.cell_neigh_graph.graph.for_each([&](u64 id,
44 const OrientedAMRGraph &oriented_cell_graph) {
45 auto &cell_sizes_span = edges.spans_block_cell_sizes.get_spans().get(id);
46 auto &phi_span = edges.spans_phi.get_spans().get(id);
47 auto &rho_span = edges.spans_rho.get_spans().get(id);
48 auto &mean_rho = edges.mean_rho.value;
49 auto &phi_res_span = edges.spans_phi_res.get_spans().get(id);
50 auto &phi_p_span = edges.spans_phi_p.get_spans().get(id);
51 auto &rhs_span = edges.spans_rhs.get_spans().get(id);
52 auto &phi_z_span = edges.spans_phi_z.get_spans().get(id);
53
54 AMRGraph &graph_neigh_xp
55 = shambase::get_check_ref(oriented_cell_graph.graph_links[Direction::xp]);
56 AMRGraph &graph_neigh_xm
57 = shambase::get_check_ref(oriented_cell_graph.graph_links[Direction::xm]);
58 AMRGraph &graph_neigh_yp
59 = shambase::get_check_ref(oriented_cell_graph.graph_links[Direction::yp]);
60 AMRGraph &graph_neigh_ym
61 = shambase::get_check_ref(oriented_cell_graph.graph_links[Direction::ym]);
62 AMRGraph &graph_neigh_zp
63 = shambase::get_check_ref(oriented_cell_graph.graph_links[Direction::zp]);
64 AMRGraph &graph_neigh_zm
65 = shambase::get_check_ref(oriented_cell_graph.graph_links[Direction::zm]);
66
67 sham::EventList depends_list;
68
69 auto cell_sizes = cell_sizes_span.get_read_access(depends_list);
70 auto phi = phi_span.get_read_access(depends_list);
71 auto rho = rho_span.get_read_access(depends_list);
72 auto phi_res = phi_res_span.get_write_access(depends_list);
73 auto phi_p = phi_p_span.get_write_access(depends_list);
74 auto rhs = rhs_span.get_write_access(depends_list);
75 auto phi_z = phi_z_span.get_write_access(depends_list);
76
77 auto graph_iter_xp = graph_neigh_xp.get_read_access(depends_list);
78 auto graph_iter_xm = graph_neigh_xm.get_read_access(depends_list);
79 auto graph_iter_yp = graph_neigh_yp.get_read_access(depends_list);
80 auto graph_iter_ym = graph_neigh_ym.get_read_access(depends_list);
81 auto graph_iter_zp = graph_neigh_zp.get_read_access(depends_list);
82 auto graph_iter_zm = graph_neigh_zm.get_read_access(depends_list);
83
84 sham::DeviceQueue &q = shamsys::instance::get_compute_scheduler().get_queue();
85 auto e = q.submit(depends_list, [&](sycl::handler &cgh) {
86 u32 cell_count = (edges.sizes.indexes.get(id)) * block_size;
87
88 shambase::parallel_for(cgh, cell_count, "init step for cg", [=](u64 gid) {
89 const u32 cell_global_id = (u32) gid;
90 const u32 block_id = cell_global_id / block_size;
91 const u32 cell_loc_id = cell_global_id % block_size;
92
93 Tscal delta_cell = cell_sizes[block_id];
94 Tscal Aii = 0;
96 cell_sizes,
97 block_size,
98 cell_global_id,
99 graph_iter_xp,
100 graph_iter_xm,
101 graph_iter_yp,
102 graph_iter_ym,
103 graph_iter_zp,
104 graph_iter_zm,
105 [=](u32 id) {
106 return phi[id];
107 },
108 Aii);
109
110 // laplacian_7pt is volume integrated, hence the dV factor
111 auto dV = delta_cell * delta_cell * delta_cell;
112 auto b_rhs = -fourPiG * (rho[cell_global_id] - mean_rho) * dV;
113
114 auto res = b_rhs - Aphi;
115 phi_res[cell_global_id] = res;
116 rhs[cell_global_id] = b_rhs;
117 // Jacobi preconditioned residual, z = M^-1 res with M = diag(A), and the
118 // first search direction of the preconditioned CG, p = z
119 phi_z[cell_global_id] = res / Aii;
120 phi_p[cell_global_id] = res / Aii;
121 });
122 });
123
124 cell_sizes_span.complete_event_state(e);
125 phi_span.complete_event_state(e);
126 rho_span.complete_event_state(e);
127 phi_res_span.complete_event_state(e);
128 phi_p_span.complete_event_state(e);
129 rhs_span.complete_event_state(e);
130 phi_z_span.complete_event_state(e);
131
132 graph_neigh_xp.complete_event_state(e);
133 graph_neigh_xm.complete_event_state(e);
134 graph_neigh_yp.complete_event_state(e);
135 graph_neigh_ym.complete_event_state(e);
136 graph_neigh_zp.complete_event_state(e);
137 graph_neigh_zm.complete_event_state(e);
138 });
139 }
140 };
141} // namespace
142
144 template<class Tvec, class TgridVec>
146 StackEntry stack_loc{};
147 auto edges = get_edges();
148
149 edges.spans_block_cell_sizes.check_sizes(edges.sizes.indexes);
150 edges.spans_phi.check_sizes(edges.sizes.indexes);
151 edges.spans_rho.check_sizes(edges.sizes.indexes);
152 edges.spans_phi_res.check_sizes(edges.sizes.indexes);
153 edges.spans_phi_p.check_sizes(edges.sizes.indexes);
154 edges.spans_rhs.check_sizes(edges.sizes.indexes);
155 edges.spans_phi_z.check_sizes(edges.sizes.indexes);
156
157 _Kernel<Tvec, TgridVec>::kernel(edges, block_size, fourPiG);
158 }
159
160 template<class Tvec, class TgridVec>
162 std::string sizes = get_ro_edge_base(0).get_tex_symbol();
163 std::string cell_neigh_graph = get_ro_edge_base(1).get_tex_symbol();
164 std::string spans_block_cell_sizes = get_ro_edge_base(2).get_tex_symbol();
165 std::string span_phi = get_ro_edge_base(3).get_tex_symbol();
166 std::string span_rho = get_ro_edge_base(4).get_tex_symbol();
167 std::string mean_rho = get_ro_edge_base(5).get_tex_symbol();
168 std::string span_phi_res = get_rw_edge_base(0).get_tex_symbol();
169 std::string span_phi_p = get_rw_edge_base(1).get_tex_symbol();
170
171 std::string tex = R"tex(
172 Initiation step of CG
173 )tex";
174
175 shambase::replace_all(tex, "{sizes}", sizes);
176 shambase::replace_all(tex, "{cell_neigh_graph}", cell_neigh_graph);
177 shambase::replace_all(tex, "{spans_block_cell_sizes}", spans_block_cell_sizes);
178 shambase::replace_all(tex, "{span_phi}", span_phi);
179 shambase::replace_all(tex, "{span_rhi}", span_rho);
180 shambase::replace_all(tex, "{mean_rho}", mean_rho);
181 shambase::replace_all(tex, "{span_phi_res}", span_phi_res);
182 shambase::replace_all(tex, "{span_phi_p}", span_phi_p);
183
184 return tex;
185 }
186
187} // namespace shammodels::basegodunov::modules
188
7-point stencil for the discrete Laplacian operator
Header file describing a Node Instance.
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
sycl::event submit(Fct &&fct)
Submits a kernel to the SYCL queue.
DeviceQueue & get_queue(u32 id=0)
Get a reference to a DeviceQueue.
virtual std::string _impl_get_tex() const
get the tex of the node
Definition CGInit.cpp:161
void _impl_evaluate_internal()
evaluate the node
Definition CGInit.cpp:145
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
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
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
namespace for the basegodunov model modules
T laplacian_7pt_with_Aii(const shambase::VecComponent< Tvec > *cell_sizes, const u32 block_size, const u32 cell_global_id, const AMRGraphLinkiterator &graph_iter_xp, const AMRGraphLinkiterator &graph_iter_xm, const AMRGraphLinkiterator &graph_iter_yp, const AMRGraphLinkiterator &graph_iter_ym, const AMRGraphLinkiterator &graph_iter_zp, const AMRGraphLinkiterator &graph_iter_zm, ACCField &&field_access, T &Aii)
Get the discretized (finite volume) Laplacian, and the diagonal coefficient of the associated matrix.
shambase::details::BasicStackEntry StackEntry
Alias for shambase::details::BasicStackEntry.