Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
PatchScheduler.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
22
25#include "shambase/time.hpp"
30#include <nlohmann/json.hpp>
31#include <unordered_set>
32#include <fstream>
33#include <functional>
34#include <memory>
35#include <stdexcept>
36#include <tuple>
37#include <vector>
38// #include "shamrock/scheduler/SerialPatchTree.hpp"
41// #include "shamrock/legacy/patch/patchdata_buffer.hpp"
43#include "shambackends/math.hpp"
52
54 u64 split_load_value = 0_u64;
55 u64 merge_load_value = 0_u64;
56};
57
64inline void to_json(nlohmann::json &j, const PatchSchedulerConfig &p) {
65 j = nlohmann::json{
66 {"split_load_value", p.split_load_value},
67 {"merge_load_value", p.merge_load_value},
68 };
69}
70
77inline void from_json(const nlohmann::json &j, PatchSchedulerConfig &p) {
78 j.at("split_load_value").get_to<u64>(p.split_load_value);
79 j.at("merge_load_value").get_to<u64>(p.merge_load_value);
80}
81
86class PatchScheduler {
87
89
90 public:
91 static constexpr u64 max_axis_patch_coord = LoadBalancer::max_box_sz;
92 static constexpr u64 max_axis_patch_coord_length = LoadBalancer::max_box_sz + 1;
93
94 using PatchTree = shamrock::scheduler::PatchTree;
95 using SchedulerPatchData = shamrock::scheduler::SchedulerPatchData;
97
98 std::shared_ptr<shamrock::patch::PatchDataLayerLayout> pdl_ptr;
99
102
104 SchedulerPatchData patch_data;
105 PatchTree patch_tree;
106 SynchronizedData synchronized_data;
107
108 // using unordered set is not an issue since we use the find command after
109 std::unordered_set<u64> owned_patch_id;
111
112 inline shamrock::patch::PatchDataLayerLayout &pdl_old() {
113 return shambase::get_check_ref(pdl_ptr);
114 }
115
116 inline std::shared_ptr<shamrock::patch::PatchDataLayerLayout> get_layout_ptr_old() const {
117 return pdl_ptr;
118 }
119
126 void scheduler_step(bool do_split_merge, bool do_load_balancing);
127
128 void init_mpi_required_types();
129
130 void free_mpi_required_types();
131
132 PatchScheduler(
133 const std::shared_ptr<shamrock::patch::PatchDataLayerLayout> &pdl_ptr,
134 u64 crit_split,
135 u64 crit_merge);
136
137 ~PatchScheduler();
138
139 std::string dump_status();
140
141 inline void update_local_load_value(std::function<u64(shamrock::patch::Patch)> load_function) {
142 for (u64 id : owned_patch_id) {
143 shamrock::patch::Patch &p = patch_list.local[patch_list.id_patch_to_local_idx[id]];
144 p.load_value = load_function(p);
145 }
146 patch_list.is_load_values_up_to_date = true;
147 }
148
149 template<class vectype>
150 std::tuple<vectype, vectype> get_box_tranform();
151
152 template<class vectype>
153 std::tuple<vectype, vectype> get_box_volume();
154
155 bool should_resize_box(bool node_in);
156
164 template<class vectype>
165 void set_coord_domain_bound(vectype bmin, vectype bmax) {
166
167 if (!pdl_old().check_main_field_type<vectype>()) {
168 std::invalid_argument(
169 std::string("the main field is not of the correct type to call this function\n")
170 + "fct called : " + __PRETTY_FUNCTION__
171 + "current patch data layout : " + pdl_old().get_description_str());
172 }
173
174 patch_data.sim_box.set_bounding_box<vectype>({bmin, bmax});
175
176 shamlog_debug_ln("PatchScheduler", "box resized to :", bmin, bmax);
177 }
178
186
187 template<u32 dim>
188 void make_patch_base_grid(std::array<u32, dim> patch_count);
189
196 template<class vectype>
197 void set_coord_domain_bound(std::tuple<vectype, vectype> box) {
198 auto [a, b] = box;
200 }
201
202 std::string format_patch_coord(shamrock::patch::Patch p);
203
204 void check_patchdata_locality_correctness();
205
206 [[deprecated]]
207 void dump_local_patches(std::string filename);
208
209 std::vector<std::unique_ptr<shamrock::patch::PatchDataLayer>> gather_data(u32 rank);
210
219 //[[deprecated]]
220 // inline u64 add_patch(shamrock::patch::Patch p, shamrock::patch::PatchData && pdat){
221 // p.id_patch = patch_list._next_patch_id;
222 // patch_list._next_patch_id ++;
223 //
224 // patch_list.global.push_back(p);
225 //
226 // patch_data.owned_data.insert({p.id_patch , pdat});
227 //
228 // return p.id_patch;
229 //}
230
231 void add_root_patch();
232
233 [[deprecated]]
234 void sync_build_LB(bool global_patch_sync, bool balance_load);
235
236 template<class vec>
237 inline shamrock::patch::PatchCoordTransform<vec> get_patch_transform() {
238 return get_sim_box().template get_patch_transform<vec>();
239 }
240
241 // template<class vec>
242 // inline SerialPatchTree<vec> make_serial_ptree(){
243 // return SerialPatchTree<vec>(patch_tree, get_patch_transform<vec>());
244 // }
245
260 template<class Function>
261 inline void for_each_patch_data(Function &&fct) {
262
263 patch_data.for_each_patchdata([&](u64 patch_id, shamrock::patch::PatchDataLayer &pdat) {
265 = patch_list.global[patch_list.id_patch_to_global_idx[patch_id]];
266
267 if (!cur_p.is_err_mode()) {
268 fct(patch_id, cur_p, pdat);
269 }
270 });
271 }
272
274 template<class Function>
275 inline void for_each_patch_data_ordered(Function &&fct) {
276
277 patch_data.for_each_patchdata_ordered(
278 [&](u64 patch_id, shamrock::patch::PatchDataLayer &pdat) {
280 = patch_list.global[patch_list.id_patch_to_global_idx[patch_id]];
281
282 if (!cur_p.is_err_mode()) {
283 fct(patch_id, cur_p, pdat);
284 }
285 });
286 }
287
288 template<class Function>
289 inline void for_each_patch(Function &&fct) {
290
291 patch_data.for_each_patchdata([&](u64 patch_id, shamrock::patch::PatchDataLayer &pdat) {
294
295 // TODO should feed the sycl queue to the lambda
296 if (!cur_p.is_err_mode()) {
297 fct(patch_id, cur_p);
298 }
299 });
300 }
301
302 inline void for_each_global_patch(
303 const std::function<void(const shamrock::patch::Patch &)> &fct) {
304 for (const shamrock::patch::Patch &p : patch_list.global) {
305 if (!p.is_err_mode()) {
306 fct(p);
307 }
308 }
309 }
310
311 inline void for_each_local_patch(
312 const std::function<void(const shamrock::patch::Patch &)> &fct) {
313 for (const shamrock::patch::Patch &p : patch_list.local) {
314 if (!p.is_err_mode()) {
315 fct(p);
316 }
317 }
318 }
319
320 inline void for_each_local_patchdata(
321 const std::function<void(const shamrock::patch::Patch &, shamrock::patch::PatchDataLayer &)>
322 &fct) {
323 for (const shamrock::patch::Patch &p : patch_list.local) {
324 if (!p.is_err_mode()) {
325 fct(p, patch_data.get_pdat(p.id_patch));
326 }
327 }
328 }
329
330 inline void for_each_local_patch_nonempty(
331 std::function<void(const shamrock::patch::Patch &)> fct) {
332 patch_data.for_each_patchdata([&](u64 patch_id, shamrock::patch::PatchDataLayer &pdat) {
333 shamrock::patch::Patch &cur_p
334 = patch_list.global[patch_list.id_patch_to_global_idx.at(patch_id)];
335
336 if ((!cur_p.is_err_mode()) && (!pdat.is_empty())) {
337 fct(cur_p);
338 }
339 });
340 }
341
342 inline const shamrock::patch::Patch &get_patch(u64 patch_id) const {
343 return patch_list.global[patch_list.id_patch_to_global_idx.at(patch_id)];
344 }
345
346 inline u32 get_patch_rank_owner(u64 patch_id) {
347 shamrock::patch::Patch &cur_p
348 = patch_list.global[patch_list.id_patch_to_global_idx.at(patch_id)];
349 return cur_p.node_owner_id;
350 }
351
352 inline void for_each_patchdata_nonempty(
353 std::function<void(const shamrock::patch::Patch, shamrock::patch::PatchDataLayer &)> fct) {
354 patch_data.for_each_patchdata([&](u64 patch_id, shamrock::patch::PatchDataLayer &pdat) {
355 shamrock::patch::Patch &cur_p
356 = patch_list.global[patch_list.id_patch_to_global_idx.at(patch_id)];
357
358 if ((!cur_p.is_err_mode()) && (!pdat.is_empty())) {
359 fct(cur_p, pdat);
360 }
361 });
362 }
363
366 std::function<void(const shamrock::patch::Patch, shamrock::patch::PatchDataLayer &)> fct) {
367 patch_data.for_each_patchdata_ordered(
368 [&](u64 patch_id, shamrock::patch::PatchDataLayer &pdat) {
370 = patch_list.global[patch_list.id_patch_to_global_idx.at(patch_id)];
371
372 if ((!cur_p.is_err_mode()) && (!pdat.is_empty())) {
373 fct(cur_p, pdat);
374 }
375 });
376 }
377
378 template<class T>
379 inline shambase::DistributedData<T> map_owned_patchdata(
380 std::function<T(const shamrock::patch::Patch, shamrock::patch::PatchDataLayer &pdat)> fct) {
382
383 using namespace shamrock::patch;
384 for_each_patch_data([&](u64 id_patch, Patch cur_p, PatchDataLayer &pdat) {
385 ret.add_obj(id_patch, fct(cur_p, pdat));
386 });
387
388 return ret;
389 }
390
391 template<class T>
392 inline shambase::DistributedData<T> distrib_data_local_to_all_simple(
393 shambase::DistributedData<T> &src) {
394 using namespace shamrock::patch;
395
396 // TODO : after a split the scheduler patch list state does not match global =
397 // allgather(local) but here it is implicitely assumed, that's ... bad
398 return shamalgs::collective::fetch_all_simple<T, Patch>(
399 src, patch_list.local, patch_list.global, [](Patch p) {
400 return p.id_patch;
401 });
402 }
403
404 template<class T>
405 inline shambase::DistributedData<T> distrib_data_local_to_all_load_store(
406 shambase::DistributedData<T> &src) {
407 using namespace shamrock::patch;
408
409 return shamalgs::collective::fetch_all_storeload<T, Patch>(
410 src, patch_list.local, patch_list.global, [](Patch p) {
411 return p.id_patch;
412 });
413 }
414
415 template<class T>
416 inline shambase::DistributedData<T> map_owned_patchdata_fetch_simple(
417 std::function<T(const shamrock::patch::Patch, shamrock::patch::PatchDataLayer &pdat)> fct) {
418 shambase::DistributedData<T> ret;
419
420 using namespace shamrock::patch;
421 for_each_patch_data([&](u64 id_patch, Patch cur_p, PatchDataLayer &pdat) {
422 ret.add_obj(id_patch, fct(cur_p, pdat));
423 });
424
425 return distrib_data_local_to_all_simple(ret);
426 }
427
428 template<class T>
429 inline shambase::DistributedData<T> map_owned_patchdata_fetch_load_store(
430 std::function<T(const shamrock::patch::Patch, shamrock::patch::PatchDataLayer &pdat)> fct) {
431 shambase::DistributedData<T> ret;
432
433 using namespace shamrock::patch;
434 for_each_patch_data([&](u64 id_patch, Patch cur_p, PatchDataLayer &pdat) {
435 ret.add_obj(id_patch, fct(cur_p, pdat));
436 });
437
438 return distrib_data_local_to_all_load_store(ret);
439 }
440
441 template<class T>
442 inline shamrock::patch::PatchField<T> map_owned_to_patch_field_simple(
443 std::function<T(const shamrock::patch::Patch, shamrock::patch::PatchDataLayer &pdat)> fct) {
444 return shamrock::patch::PatchField<T>(map_owned_patchdata_fetch_simple(fct));
445 }
446
447 template<class T>
448 inline shamrock::patch::PatchField<T> map_owned_to_patch_field_load_store(
449 std::function<T(const shamrock::patch::Patch, shamrock::patch::PatchDataLayer &pdat)> fct) {
450 return shamrock::patch::PatchField<T>(map_owned_patchdata_fetch_load_store(fct));
451 }
452
453 inline u64 get_rank_count() {
454 StackEntry stack_loc{};
455 using namespace shamrock::patch;
456 u64 num_obj = 0; // TODO get_rank_count() in scheduler
457 for_each_patch_data([&](u64 id_patch, Patch cur_p, PatchDataLayer &pdat) {
458 num_obj += pdat.get_obj_cnt();
459 });
460
461 return num_obj;
462 }
463
464 inline u64 get_total_obj_count() {
465 StackEntry stack_loc{};
466 u64 part_cnt = get_rank_count();
467 return shamalgs::collective::allreduce_sum(part_cnt);
468 }
469
470 template<class T>
471 inline std::unique_ptr<sycl::buffer<T>> rankgather_field(u32 field_idx) {
472 StackEntry stack_loc{};
473 std::unique_ptr<sycl::buffer<T>> ret;
474
475 auto fd = pdl_old().get_field<T>(field_idx);
476 u64 nvar = fd.nvar;
477
478 u64 num_obj = get_rank_count();
479
480 if (num_obj > 0) {
481 ret = std::make_unique<sycl::buffer<T>>(num_obj * nvar);
482
483 using namespace shamrock::patch;
484
485 u64 ptr = 0; // TODO accumulate_field() in scheduler ?
486 for_each_patch_data_ordered([&](u64 id_patch, Patch cur_p, PatchDataLayer &pdat) {
487 using namespace shamalgs::memory;
488 using namespace shambase;
489
490 if (pdat.get_obj_cnt() > 0) {
491 write_with_offset_into(
492 shamsys::instance::get_compute_scheduler().get_queue(),
493 get_check_ref(ret),
494 pdat.get_field<T>(field_idx).get_buf(),
495 ptr,
496 pdat.get_obj_cnt() * nvar);
497
498 ptr += pdat.get_obj_cnt() * nvar;
499 }
500 });
501 }
502
503 return ret;
504 }
505
506 // template<class Function, class Pfield>
507 // inline void compute_patch_field(Pfield & field, MPI_Datatype & dtype , Function && lambda){
508 // field.local_nodes_value.resize(patch_list.local.size());
509 //
510 //
511 //
512 // for (u64 idx = 0; idx < patch_list.local.size(); idx++) {
513 //
514 // Patch &cur_p = patch_list.local[idx];
515 //
516 // PatchDataBuffer pdatbuf =
517 // attach_to_patchData(patch_data.owned_data.at(cur_p.id_patch));
518 //
519 // field.local_nodes_value[idx] =
520 // lambda(shamsys::instance::get_compute_queue(),cur_p,pdatbuf);
521 //
522 // }
523 //
524 // field.build_global(dtype);
525 //
526 // }
527
528 template<class Function, class Pfield>
529 inline void compute_patch_field(Pfield &field, MPI_Datatype &dtype, Function &&lambda) {
530 field.local_nodes_value.resize(patch_list.local.size());
531
532 for (u64 idx = 0; idx < patch_list.local.size(); idx++) {
533
534 shamrock::patch::Patch &cur_p = patch_list.local[idx];
535
536 if (!cur_p.is_err_mode()) {
537 field.local_nodes_value[idx] = lambda(
538 shamsys::instance::get_compute_queue(),
539 cur_p,
540 patch_data.owned_data.get(cur_p.id_patch));
541 }
542 }
543
544 field.build_global(dtype);
545 }
546
547 inline auto get_node_set_edge_patchdata_layer_refs() {
548 shamrock::solvergraph::NodeSetEdge<shamrock::solvergraph::PatchDataLayerRefs> node_set_edge(
549 [&](shamrock::solvergraph::PatchDataLayerRefs &edge) {
550 edge.free_alloc();
551 using namespace shamrock::patch;
552 for_each_patchdata_nonempty([&](Patch cur_p, PatchDataLayer &pdat) {
553 edge.patchdatas.add_obj(cur_p.id_patch, std::ref(pdat));
554 });
555 });
556
557 return std::make_shared<decltype(node_set_edge)>(std::move(node_set_edge));
558 };
559
566 std::vector<u64> add_root_patches(std::vector<shamrock::patch::PatchCoord<3>> coords);
567
568 shamrock::patch::SimulationBoxInfo &get_sim_box() { return patch_data.sim_box; }
569
570 nlohmann::json serialize_patch_metadata();
571
572 private:
573 void split_patches(std::unordered_set<u64> split_rq);
574 void merge_patches(std::unordered_set<u64> merge_rq);
575
576 void set_patch_pack_values(std::unordered_set<u64> merge_rq);
577};
function to run load balancing with the hilbert curve
Node that applies a custom function to modify connected edges.
Defines the PatchDataLayerRefs class for managing distributed references to patch data layers.
void to_json(nlohmann::json &j, const PatchSchedulerConfig &p)
Converts a PatchSchedulerConfig object to a JSON object.
void from_json(const nlohmann::json &j, PatchSchedulerConfig &p)
Deserializes a PatchSchedulerConfig object from a JSON object.
Header file for the patch struct and related function.
PatchData handling.
Declare a class to register and retrieve nodes and edges from a unique container.
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
void set_coord_domain_bound(std::tuple< vectype, vectype > box)
modify the bounding box of the patch domain
void for_each_patch_data(Function &&fct)
for each macro for patchadata example usage
SchedulerPatchData patch_data
handle the data of the patches of the scheduler
u64 crit_patch_split
splitting limit (if load value > crit_patch_split => patch split)
PatchTree patch_tree
handle the tree structure of the patches
void scheduler_step(bool do_split_merge, bool do_load_balancing)
scheduler step
void set_coord_domain_bound(vectype bmin, vectype bmax)
modify the bounding box of the patch domain
SynchronizedData synchronized_data
data that is synchroneous across all ranks
SchedulerPatchList patch_list
handle the list of the patches of the scheduler
std::unordered_set< u64 > owned_patch_id
(owned_patch_id = patch_list.build_local())
std::vector< u64 > add_root_patches(std::vector< shamrock::patch::PatchCoord< 3 > > coords)
add a root patch to the scheduler
u64 crit_patch_merge
merging limit (if load value < crit_patch_merge => patch merge)
void allpush_data(shamrock::patch::PatchDataLayer &pdat)
push data in the scheduler The content of pdat as to be the same for each node
void for_each_patchdata_nonempty_ordered(std::function< void(const shamrock::patch::Patch, shamrock::patch::PatchDataLayer &)> fct)
Same as for_each_patchdata_nonempty but iterates in increasing patch id order.
void add_root_patch()
add patch to the scheduler
void for_each_patch_data_ordered(Function &&fct)
Same as for_each_patch_data but iterates in increasing patch id order.
Handle the patch list of the mpi scheduler.
std::vector< shamrock::patch::Patch > global
contain the list of all patches in the simulation
std::unordered_map< u64, u64 > id_patch_to_global_idx
id_patch_to_global_idx[patch_id] = index in global patch list
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.
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
Patch Tree : Tree structure organisation for an abstract list of patches Nb : this tree is compatible...
Definition PatchTree.hpp:29
Class to handle PatchData owned by the node.
virtual void free_alloc() override
Free allocated memory.
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
header for PatchData related function and declaration
Class to handle the patch list of the mpi scheduler.
This file contains the definition for the stacktrace related functionality.
shambase::details::BasicStackEntry StackEntry
Alias for shambase::details::BasicStackEntry.
Patch object that contain generic patch information.
Definition Patch.hpp:33
bool is_err_mode() const
check if a patch is in error mode
Definition Patch.hpp:119
u32 node_owner_id
node rank owner of this patch
Definition Patch.hpp:93
u64 load_value
if synchronized contain the load value of the patch
Definition Patch.hpp:88
u64 id_patch
unique key that identify the patch
Definition Patch.hpp:86
header file to manage sycl