Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
ReattributeDataUtility.hpp
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
10#pragma once
11
17
18#include "shambase/string.hpp"
20#include "shamalgs/memory.hpp"
27#include <vector>
28
29namespace shamrock {
30
39 PatchScheduler &sched;
40
41 public:
47 ReattributeDataUtility(PatchScheduler &sched) : sched(sched) {}
48
61 template<class T>
63 SerialPatchTree<T> &sptree, u32 ipos) {
64
65 StackEntry stack_loc{};
66
68
69 auto transform = sched.get_patch_transform<T>();
70
71 sched.patch_data.for_each_patchdata([&](u64 id, shamrock::patch::PatchDataLayer &pdat) {
72 if (!pdat.is_empty()) {
73
74 PatchDataField<T> &pos_field = pdat.get_field<T>(ipos);
75
76 if (pos_field.get_nvar() != 1) {
78 }
79
81 = sched.patch_list.global[sched.patch_list.id_patch_to_global_idx.at(id)];
82 auto cur_box = transform.to_obj_coord(cur_p);
83
84 newid_buf_map.add_obj(
85 id,
86 sptree.compute_patch_owner(
87 shamsys::instance::get_compute_scheduler_ptr(),
88 pos_field.get_buf(),
89 pos_field.get_obj_cnt(),
90 id,
91 cur_box.lower,
92 cur_box.upper));
93
94 bool err_id_in_newid = false;
95 {
96 sycl::host_accessor nid{newid_buf_map.get(id), sycl::read_only};
97 for (u32 i = 0; i < pdat.get_obj_cnt(); i++) {
98 bool err = nid[i] == u64_max;
99 err_id_in_newid = err_id_in_newid || (err);
100 }
101 }
102
103 if (err_id_in_newid) {
105 "a new id could not be computed");
106 }
107 }
108 });
109
110 return newid_buf_map;
111 }
112
126 shambase::DistributedData<sycl::buffer<u64>> new_pid) {
128
129 StackEntry stack_loc{};
130
131 using namespace shamrock::patch;
132
133 std::unordered_map<u64, u64> histogram_extract;
134
135 sched.patch_data.for_each_patchdata(
136 [&](u64 current_pid, shamrock::patch::PatchDataLayer &pdat) {
137 histogram_extract[current_pid] = 0;
138 if (!pdat.is_empty()) {
139
140 sycl::host_accessor nid{new_pid.get(current_pid), sycl::read_only};
141
142 if (false) {
143
144 const u32 cnt = pdat.get_obj_cnt();
145
146 for (u32 i = cnt - 1; i < cnt; i--) {
147 u64 new_pid = nid[i];
148 if (current_pid != new_pid) {
149
150 if (!part_exchange.has_key(current_pid, new_pid)) {
151 part_exchange.add_obj(
152 current_pid,
153 new_pid,
154 PatchDataLayer(sched.get_layout_ptr_old()));
155 }
156
157 part_exchange.for_each(
158 [&](u64 _old_id, u64 _new_id, PatchDataLayer &pdat_int) {
159 if (_old_id == current_pid && _new_id == new_pid) {
160 pdat.extract_element(i, pdat_int);
161 histogram_extract[current_pid]++;
162 }
163 });
164 }
165 }
166 } else {
167 std::vector<u32> keep_ids;
168 std::unordered_map<u64, std::vector<u32>> extract_indexes;
169
170 const u32 cnt = pdat.get_obj_cnt();
171 for (u32 i = 0; i < cnt; i++) {
172 u64 new_pid = nid[i];
173 if (current_pid != new_pid) {
174 extract_indexes[new_pid].push_back(i);
175 histogram_extract[current_pid]++;
176 } else {
177 keep_ids.push_back(i);
178 }
179 }
180
181 for (auto &[new_id, vec] : extract_indexes) {
182
183 u64 new_pid = new_id;
184 std::vector<u32> &idx_extract = vec;
185
186 if (!part_exchange.has_key(current_pid, new_pid)) {
187 part_exchange.add_obj(
188 current_pid,
189 new_pid,
190 PatchDataLayer(sched.get_layout_ptr_old()));
191 }
192
193 part_exchange.for_each(
194 [&](u64 _old_id, u64 _new_id, PatchDataLayer &pdat_int) {
195 if (_old_id == current_pid && _new_id == new_pid) {
196 pdat.append_subset_to(idx_extract, pdat_int);
197 }
198 });
199 }
200
201 sycl::buffer<u32> keep_idx = shamalgs::memory::vec_to_buf(keep_ids);
202 pdat.keep_ids(keep_idx, keep_ids.size());
203 }
204 }
205 });
206
207 for (auto &[k, v] : histogram_extract) {
208 shamlog_debug_ln("ReattributeDataUtility", "patch", k, "extract=", v);
209 }
210
211 return part_exchange;
212 }
213
224 template<class T>
226 SerialPatchTree<T> &sptree, std::string position_field) {
227 StackEntry stack_loc{};
228
229 using namespace shambase;
230 using namespace shamrock::patch;
231
232 u32 ipos = sched.pdl_old().get_field_idx<T>(position_field);
233
234 DistributedData<sycl::buffer<u64>> new_pid = compute_new_pid(sptree, ipos);
235
237
238 part_exchange.for_each([](u64 sender, u64 receiver, PatchDataLayer &pdat) {
239 shamlog_debug_ln("ReattributeDataUtility", sender, receiver, pdat.get_obj_cnt());
240 });
241
243
245
246 shamalgs::collective::serialize_sparse_comm<PatchDataLayer>(
247 shamsys::instance::get_compute_scheduler_ptr(),
248 std::move(part_exchange),
249 recv_dat,
250 [&](u64 id) {
251 return sched.get_patch_rank_owner(id);
252 },
253 [](PatchDataLayer &pdat) {
254 shamalgs::SerializeHelper ser(shamsys::instance::get_compute_scheduler_ptr());
255 ser.allocate(pdat.serialize_buf_byte_size());
256 pdat.serialize_buf(ser);
257 return ser.finalize();
258 },
259 [&](sham::DeviceBuffer<u8> &&buf) {
260 // exchange the buffer held by the distrib data and give it to the serializer
262 shamsys::instance::get_compute_scheduler_ptr(),
263 std::forward<sham::DeviceBuffer<u8>>(buf));
264 return PatchDataLayer::deserialize_buf(ser, sched.get_layout_ptr_old());
265 },
266 cache);
267
268 recv_dat.for_each([&](u64 sender, u64 receiver, PatchDataLayer &pdat) {
269 shamlog_debug_ln("Part Exchanges", format("send = {} recv = {}", sender, receiver));
270 sched.patch_data.get_pdat(receiver).insert_elements(pdat);
271 });
272 }
273 };
274
275} // namespace shamrock
Header file describing a Node Instance.
MPI scheduler.
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
The MPI scheduler.
A buffer allocated in USM (Unified Shared Memory).
Container for objects shared between two distributed data elements.
bool has_key(u64 left_id, u64 right_id) const
Check if a patch pair exists in the container.
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.
Represents a collection of objects distributed across patches identified by a u64 id.
iterator add_obj(u64 id, T &&obj)
Adds a new object to the collection.
T & get(u64 id)
Returns a reference to an object in the collection.
ReattributeDataUtility(PatchScheduler &sched)
Constructor.
shambase::DistributedDataShared< shamrock::patch::PatchDataLayer > extract_elements(shambase::DistributedData< sycl::buffer< u64 > > new_pid)
Extracts elements that do not belong to a patch from the patch data based on the new patch IDs.
shambase::DistributedData< sycl::buffer< u64 > > compute_new_pid(SerialPatchTree< T > &sptree, u32 ipos)
Computes the new patch owner IDs for the objects in the patches based on their position in space.
void reatribute_patch_objects(SerialPatchTree< T > &sptree, std::string position_field)
Reattribute objects based on a given position field.
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
void extract_element(u32 pidx, PatchDataLayer &out_pdat)
extract particle at index pidx and insert it in the provided vectors
sycl::buffer< T > vec_to_buf(const std::vector< T > &buf)
Convert a std::vector to a sycl::buffer.
Definition memory.cpp:29
namespace for basic c++ utilities
ExcptTypes make_except_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Create an exception with a message and a location.
void throw_unimplemented(SourceLocation loc=SourceLocation{})
Throw a std::runtime_error saying that the function is unimplemented.
namespace for the main framework
Definition __init__.py:1
constexpr u64 u64_max
u64 max value
main include file for memory algorithms
shambase::details::BasicStackEntry StackEntry
Alias for shambase::details::BasicStackEntry.
Patch object that contain generic patch information.
Definition Patch.hpp:33