Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
GSPHGhostHandler.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
16
20#include <functional>
21#include <vector>
22
23using namespace shammodels::gsph;
24
25template<class vec>
26auto GSPHGhostHandler<vec>::gen_id_table_interfaces(GeneratorMap &&gen)
28 StackEntry stack_loc{};
29 using namespace shamrock::patch;
30
32
33 std::map<u64, f64> send_count_stats;
34
35 const u32 ixyz = sched.pdl_old().template get_field_idx<vec>("xyz");
36
37 gen.for_each([&](u64 sender, u64 receiver, InterfaceBuildInfos &build) {
38 shamrock::patch::PatchDataLayer &src = sched.patch_data.get_pdat(sender);
39 PatchDataField<vec> &xyz = src.get_field<vec>(ixyz);
40
41 sham::DeviceBuffer<u32> idxs_res = xyz.get_ids_where(
42 [](auto access, u32 id, vec vmin, vec vmax) {
43 return Patch::is_in_patch_converted(access[id], vmin, vmax);
44 },
45 build.cut_volume.lower,
46 build.cut_volume.upper);
47
48 u32 pcnt = idxs_res.get_size();
49
50 // prevent sending empty patches
51 if (pcnt == 0) {
52 return;
53 }
54
55 f64 ratio = f64(pcnt) / f64(src.get_obj_cnt());
56
57 shamlog_debug_ln(
58 "InterfaceGen",
59 "gen interface :",
60 sender,
61 "->",
62 receiver,
63 "volume ratio:",
64 build.volume_ratio,
65 "part_ratio:",
66 ratio);
67
68 res.add_obj(sender, receiver, InterfaceIdTable{build, std::move(idxs_res), ratio});
69
70 send_count_stats[sender] += ratio;
71 });
72
73 bool has_warn = false;
74
75 std::string warn_log = "";
76
77 for (auto &[k, v] : send_count_stats) {
78 if (v > 0.2) {
79 has_warn = true;
80 }
81 }
82
83 if (has_warn && shamcomm::world_rank() == 0) {
84 warn_log = "\n High interf/patch volume. This can lead to high mpi "
85 "overhead, try to increase the patch split crit"
86 + warn_log;
87 }
88
89 if (has_warn) {
90 logger::warn_ln("InterfaceGen", "High interface/patch volume ratio." + warn_log);
91 }
92
93 return res;
94}
95
96template<class vec>
97void GSPHGhostHandler<vec>::gen_debug_patch_ghost(
99 StackEntry stack_loc{};
100
101 static u32 cnt_dump_debug = 0;
102
103 std::string loc_graph = "";
104 interf_info.for_each([&loc_graph](u64 send, u64 recv, InterfaceIdTable &info) {
105 loc_graph += sham::format(" p{} -> p{}\n", send, recv);
106 });
107
108 sched.for_each_patch_data(
110 if (pdat.get_obj_cnt() > 0) {
111 loc_graph += sham::format(
112 " p{} [label= \"id={} N={}\"]\n", id, id, pdat.get_obj_cnt());
113 }
114 });
115
116 std::string dot_graph = "";
117 shamalgs::collective::gather_str(loc_graph, dot_graph);
118
119 dot_graph = "strict digraph {\n" + dot_graph + "}";
120
121 if (shamcomm::world_rank() == 0) {
122 std::string fname = sham::format("ghost_graph_{}.dot", cnt_dump_debug);
123 logger::info_ln("GSPH Ghost", "writing", fname);
124 shambase::write_string_to_file(fname, dot_graph);
125 cnt_dump_debug++;
126 }
127}
128
constexpr const char * xyz
Position field (3D coordinates).
GSPH-specific ghost handler using Newtonian physics field names.
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).
Container for objects shared between two distributed data elements.
void for_each(std::function< void(u64, u64, T &)> &&f)
Apply a function to all stored objects.
iterator add_obj(u64 left_id, u64 right_id, T &&obj)
Add an object associated with a patch pair.
PatchDataLayer container class, the layout is described in patchdata_layout.
u32 get_obj_cnt() const
get the number of objects (particles) stored in this layer
This header file contains utility functions related to exception handling in the code.
MPI string gather / allgather helpers (declarations; implementations in shamalgs/src/collective/gathe...
void gather_str(const std::string &send_vec, std::string &recv_vec)
Gathers a string from all nodes and store the result in a std::string.
void write_string_to_file(std::string filename, std::string s)
dump a string to a file
Definition string.hpp:91
i32 world_rank()
Gives the rank of the current process in the MPI communicator.
Definition worldInfo.cpp:41
void info_ln(std::string module_name, Types... var2)
Prints a log message with multiple arguments followed by a newline.
Definition logs.hpp:132
void warn_ln(std::string module_name, Types... var2)
Prints a log message with multiple arguments followed by a newline.
Definition logs.hpp:132
shambase::details::BasicStackEntry StackEntry
Alias for shambase::details::BasicStackEntry.
Patch object that contain generic patch information.
Definition Patch.hpp:33
static bool is_in_patch_converted(sycl::vec< T, 3 > val, sycl::vec< T, 3 > min_val, sycl::vec< T, 3 > max_val)
check if particle is in the asked range, given the output of @convert_coord
Definition Patch.hpp:210