Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
SPHSetup.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
20#include "shambase/memory.hpp"
21#include "shambase/string.hpp"
22#include "shambase/tabulate.hpp"
28#include "shamcomm/logs.hpp"
30#include "shamcomm/wrapper.hpp"
47#include <mpi.h>
48#include <vector>
49
50template<class Tvec, template<class> class SPHKernel>
51inline std::shared_ptr<shammodels::sph::modules::ISPHSetupNode> shammodels::sph::modules::
52 SPHSetup<Tvec, SPHKernel>::make_generator_lattice_hcp(
53 Tscal dr, std::pair<Tvec, Tvec> box, bool discontinuous, Tscal init_h_factor) {
54 if (discontinuous) {
55 return std::shared_ptr<ISPHSetupNode>(
56 new GeneratorLatticeHCP<Tvec, true>(context, dr, box, init_h_factor));
57 } else {
58 return std::shared_ptr<ISPHSetupNode>(
59 new GeneratorLatticeHCP<Tvec, false>(context, dr, box, init_h_factor));
60 }
61}
62
63template<class Tvec, template<class> class SPHKernel>
64inline std::shared_ptr<shammodels::sph::modules::ISPHSetupNode> shammodels::sph::modules::
65 SPHSetup<Tvec, SPHKernel>::make_generator_lattice_fcc(
66 Tscal dr, std::pair<Tvec, Tvec> box, bool discontinuous, Tscal init_h_factor) {
67 if (discontinuous) {
68 return std::shared_ptr<ISPHSetupNode>(
69 new GeneratorLatticeFCC<Tvec, true>(context, dr, box, init_h_factor));
70 } else {
71 return std::shared_ptr<ISPHSetupNode>(
72 new GeneratorLatticeFCC<Tvec, false>(context, dr, box, init_h_factor));
73 }
74}
75
76template<class Tvec, template<class> class SPHKernel>
77inline std::shared_ptr<shammodels::sph::modules::ISPHSetupNode> shammodels::sph::modules::
78 SPHSetup<Tvec, SPHKernel>::make_generator_lattice_cubic(Tscal dr, std::pair<Tvec, Tvec> box) {
79 return std::shared_ptr<ISPHSetupNode>(new GeneratorLatticeCubic<Tvec>(context, dr, box));
80}
81
82template<class Tvec, template<class> class SPHKernel>
83inline std::shared_ptr<shammodels::sph::modules::ISPHSetupNode> shammodels::sph::modules::
84 SPHSetup<Tvec, SPHKernel>::make_generator_disc_mc(
85 Tscal part_mass,
86 Tscal disc_mass,
87 Tscal r_in,
88 Tscal r_out,
89 std::function<Tscal(Tscal)> sigma_profile,
90 std::function<Tscal(Tscal)> H_profile,
91 std::function<Tvec(Tvec)> vel_profile,
92 std::function<Tscal(Tvec)> cs_profile,
93 std::mt19937_64 eng,
94 Tscal init_h_factor) {
95 return std::shared_ptr<ISPHSetupNode>(new GeneratorMCDisc<Tvec, SPHKernel>(
96 context,
97 solver_config,
98 part_mass,
99 disc_mass,
100 r_in,
101 r_out,
102 sigma_profile,
103 H_profile,
104 vel_profile,
105 cs_profile,
106 eng,
107 init_h_factor));
108}
109
110template<class Tvec, template<class> class SPHKernel>
111inline std::shared_ptr<shammodels::sph::modules::ISPHSetupNode> shammodels::sph::modules::
112 SPHSetup<Tvec, SPHKernel>::make_generator_from_context(ShamrockCtx &context_other) {
113 return std::shared_ptr<ISPHSetupNode>(
114 new GeneratorFromOtherContext<Tvec>(context, context_other));
115}
116
117template<class Tvec, template<class> class SPHKernel>
118inline std::shared_ptr<shammodels::sph::modules::ISPHSetupNode> shammodels::sph::modules::
119 SPHSetup<Tvec, SPHKernel>::make_combiner_add(SetupNodePtr parent1, SetupNodePtr parent2) {
120 return std::shared_ptr<ISPHSetupNode>(new CombinerAdd<Tvec>(context, parent1, parent2));
121}
122
123template<class Tvec, template<class> class SPHKernel>
124void shammodels::sph::modules::SPHSetup<Tvec, SPHKernel>::apply_setup(
125 SetupNodePtr setup, bool part_reordering, std::optional<u32> insert_step) {
126
127 if (!bool(setup)) {
128 shambase::throw_with_loc<std::invalid_argument>("The setup shared pointer is empty");
129 }
130
131 shambase::Timer time_setup;
132 time_setup.start();
133 StackEntry stack_loc{};
134
135 PatchScheduler &sched = shambase::get_check_ref(context.sched);
136
137 auto compute_load = [&]() {
138 modules::ComputeLoadBalanceValue<Tvec, SPHKernel>(context, solver_config, storage)
139 .update_load_balancing();
140 };
141
142 auto has_pdat = [&]() {
143 bool ret = false;
144 using namespace shamrock::patch;
145 sched.for_each_local_patchdata([&](const Patch &p, PatchDataLayer &pdat) {
146 ret = true;
147 });
148 return ret;
149 };
150
151 shamrock::DataInserterUtility inserter(sched);
152 u32 _insert_step = sched.crit_patch_split * 8;
153 if (bool(insert_step)) {
154 _insert_step = insert_step.value();
155 }
156
157 while (!setup->is_done()) {
158
159 shamrock::patch::PatchDataLayer pdat = setup->next_n((has_pdat()) ? _insert_step : 0);
160
161 if (solver_config.track_particles_id) {
162 // This bit set the tracking id of the particles
163 // But be carefull this assume that the particle injection order
164 // is independant from the MPI world size. It should be the case for most setups
165 // but some generator could miss this assumption.
166 // If that is the case please report the issue
167
168 u64 loc_inj = pdat.get_obj_cnt();
169
170 u64 offset_init = 0;
172 &loc_inj, &offset_init, 1, get_mpi_type<u64>(), MPI_SUM, MPI_COMM_WORLD);
173
174 // we must add the number of already injected part such that the
175 // offset start at the right spot.
176 // The only thing that bothers me is that this can not handle the case where multiple
177 // setups of things like that are applied. But in principle no sane person would do such
178 // a thing...
179 offset_init += injected_parts;
180
181 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
182 auto &q = shambase::get_check_ref(dev_sched).get_queue();
183
184 if (loc_inj > 0) {
185 sham::DeviceBuffer<u64> part_ids(loc_inj, dev_sched);
186
188 q,
190 sham::MultiRef{part_ids},
191 loc_inj,
192 [offset_init](u32 i, u64 *__restrict part_ids) {
193 part_ids[i] = i + offset_init;
194 });
195
196 pdat.get_field<u64>(pdat.pdl().get_field_idx<u64>("part_id"))
197 .overwrite(part_ids, loc_inj);
198 }
199 }
200
201 u64 injected
202 = inserter.push_patch_data<Tvec>(pdat, "xyz", sched.crit_patch_split * 8, compute_load);
203
204 injected_parts += injected;
205 }
206
207 u32 final_balancing_steps = 3;
208 for (u32 i = 0; i < final_balancing_steps; i++) {
209 ON_RANK_0(
211 "SPH setup", "Final load balancing step", i, "of", final_balancing_steps));
212 inserter.balance_load(compute_load);
213 }
214
215 if (part_reordering) {
216 modules::ParticleReordering<Tvec, u32, SPHKernel>(context, solver_config, storage)
217 .reorder_particles();
218 }
219
220 time_setup.stop();
221 if (shamcomm::world_rank() == 0) {
222 logger::info_ln("SPH setup", "the setup took :", time_setup.elapsed_sec(), "s");
223 }
224}
225
226struct SetupLog {
227 struct State {
228 std::vector<u64> count_per_rank;
229 std::vector<std::tuple<u32, u32, u64>> msg_list;
230 } state;
231
232 u64 step_counter = 0;
233
234 nlohmann::json json_data = nlohmann::json::array();
235
236 void log_state() {
237 nlohmann::json step_data;
238 step_data["step_counter"] = step_counter;
239 step_data["count_per_rank"] = state.count_per_rank;
240 step_data["msg_list"] = state.msg_list;
241 json_data.push_back(step_data);
242 }
243
244 void dump_state() {
245 std::string fname = "setup_log_step.json";
246 if (shamcomm::world_rank() == 0) {
247 logger::normal_ln("SPH setup", "dumping setup log to ", fname);
248 }
249
250 std::ofstream file(fname);
251 file << json_data.dump(4);
252 file.close();
253
254 step_counter++;
255 }
256
257 void update_count_per_rank(u64 count) {
258 std::vector<u64> tmp{count};
259 std::vector<u64> recv_count_per_rank;
260 shamalgs::collective::vector_allgatherv(tmp, recv_count_per_rank, MPI_COMM_WORLD);
261 state.count_per_rank = recv_count_per_rank;
262 log_state();
263 if (step_counter % 20 == 0)
264 dump_state();
265 }
266
267 void update_msg_list(std::vector<std::tuple<u32, u32, u64>> &msg_list) {
268 state.msg_list = msg_list;
269 log_state();
270 if (step_counter % 20 == 0)
271 dump_state();
272 }
273};
274
275inline constexpr f64 golden_number = 1.61803398874989484820458683436563;
276
277template<class Tvec, template<class> class SPHKernel>
278void shammodels::sph::modules::SPHSetup<Tvec, SPHKernel>::apply_setup_new(
279 SetupNodePtr setup,
280 bool part_reordering,
281 std::optional<u32> gen_count_per_step,
282 std::optional<u32> insert_count_per_step,
283 std::optional<u64> max_msg_count_per_rank_per_step,
284 std::optional<u64> max_data_count_per_rank_per_step,
285 std::optional<u64> max_msg_size,
286 bool do_setup_log,
287 bool speculative_balancing) {
288
290
291 if (!bool(setup)) {
292 shambase::throw_with_loc<std::invalid_argument>("The setup shared pointer is empty");
293 }
294
295 std::optional<SetupLog> setup_log
296 = (do_setup_log) ? std::make_optional<SetupLog>() : std::nullopt;
297
298 shambase::Timer time_setup;
299 time_setup.start();
300 PatchScheduler &sched = shambase::get_check_ref(context.sched);
301 shamrock::DataInserterUtility inserter(sched);
302
303 u32 insert_step = sched.crit_patch_split * 2;
304 if (bool(insert_count_per_step)) {
305 insert_step = insert_count_per_step.value();
306 }
307
308 u32 gen_step = std::max(sched.crit_patch_split / 8, 1_u64);
309 if (bool(gen_count_per_step)) {
310 gen_step = gen_count_per_step.value();
311 }
312
313 u64 msg_limit = 1024;
314 if (bool(max_msg_count_per_rank_per_step)) {
315 msg_limit = max_msg_count_per_rank_per_step.value();
316 }
317 u64 data_count_limit = insert_step;
318 if (bool(max_data_count_per_rank_per_step)) {
319 data_count_limit = max_data_count_per_rank_per_step.value();
320 }
321 u64 max_message_size = std::max(insert_step / 16, 1_u32);
322 if (bool(max_msg_size)) {
323 max_message_size = max_msg_size.value();
324 }
325
326 shamrock::patch::PatchDataLayer to_insert(sched.get_layout_ptr_old());
327
328 u64 speculative_last_npatch = 0;
329 shambase::DistributedData<u64> speculative_load_values = {};
330
331 auto compute_load = [&]() {
332 if (speculative_balancing) {
333
334 StackEntry stack_loc{};
335
336 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
337
338 u64 npatch = scheduler().patch_list.global.size();
339
340 // check if the number of patches has changed, rebuild otherwise
341 if (npatch != speculative_last_npatch) {
342
343 shambase::details::NamedBasicStackEntry stack_loc2{"compute_load"};
344
345 if (shamcomm::world_rank() == 0) {
347 "SPH setup",
348 "number of patches has changed, rebuilding speculative load values");
349 }
350
351 // reset the load values
352 speculative_last_npatch = npatch;
353 speculative_load_values.reset();
354
355 // Compute the AABB of all the patches
356
357 std::vector<Tvec> patch_aabb_min(npatch);
358 std::vector<Tvec> patch_aabb_max(npatch);
359
360 auto &global_patch_list = scheduler().patch_list.global;
362 = sched.get_sim_box().get_patch_transform<Tvec>();
363
364 for (size_t i = 0; i < global_patch_list.size(); i++) {
365 const shamrock::patch::Patch &p = global_patch_list[i];
366 if (!p.is_err_mode()) {
367 shammath::CoordRange<Tvec> patch_coord = ptransf.to_obj_coord(p);
368 patch_aabb_min[i] = patch_coord.lower;
369 patch_aabb_max[i] = patch_coord.upper;
370 }
371 }
372
373 sham::DeviceBuffer<Tvec> buf_patch_aabb_min(npatch, dev_sched);
374 sham::DeviceBuffer<Tvec> buf_patch_aabb_max(npatch, dev_sched);
375
376 buf_patch_aabb_min.copy_from_stdvec(patch_aabb_min);
377 buf_patch_aabb_max.copy_from_stdvec(patch_aabb_max);
378
379 // count the number of particles in each patch
380
381 sham::DeviceBuffer<u64> local_load_values(npatch, dev_sched);
382 local_load_values.fill(0);
383
384 PatchDataField<Tvec> &xyz = to_insert.get_field<Tvec>(0);
385
386 if (xyz.get_obj_cnt() > 0) {
388 shamsys::instance::get_compute_scheduler().get_queue(),
389 sham::MultiRef{xyz.get_buf(), buf_patch_aabb_min, buf_patch_aabb_max},
390 sham::MultiRef{local_load_values},
391 xyz.get_obj_cnt(),
392 [npatch](
393 u32 i,
394 const Tvec *__restrict xyz,
395 const Tvec *__restrict patch_aabb_min,
396 const Tvec *__restrict patch_aabb_max,
397 u64 *__restrict local_load_values) {
398 Tvec pos = xyz[i];
399 for (size_t j = 0; j < npatch; j++) {
401 = {patch_aabb_min[j], patch_aabb_max[j]};
402 if (patch_coord.contain_pos(pos)) {
403 sycl::atomic_ref<
404 u64,
405 sycl::memory_order::relaxed,
406 sycl::memory_scope::device>
407 atomic_local_load_values(local_load_values[j]);
408 atomic_local_load_values++;
409 }
410 }
411 });
412 }
413
414 // recover data
415
416 auto local_load_values_host = local_load_values.copy_to_stdvec();
417
418 std::vector<u64> reduced_load_values(npatch);
419
420 // reduce the load values
421
423 local_load_values_host.data(),
424 reduced_load_values.data(),
425 npatch,
426 get_mpi_type<u64>(),
427 MPI_SUM,
428 MPI_COMM_WORLD);
429
430 // convert to DistributedData
431
432 for (size_t i = 0; i < npatch; i++) {
433 speculative_load_values.add_obj(
434 global_patch_list[i].id_patch, u64(reduced_load_values[i]));
435 }
436
437 // Add the already injected parts to the load values
438
439 auto &patch_list = scheduler().patch_list;
440
441 for (u64 id : scheduler().owned_patch_id) {
443 = patch_list.local[patch_list.id_patch_to_local_idx[id]];
444 speculative_load_values.get(id)
445 += scheduler().patch_data.owned_data.get(id).get_obj_cnt();
446 }
447 }
448
449 // update load values
450
451 scheduler().update_local_load_value([&](shamrock::patch::Patch p) {
452 return speculative_load_values.get(p.id_patch);
453 });
454
455 } else {
456 modules::ComputeLoadBalanceValue<Tvec, SPHKernel>(context, solver_config, storage)
457 .update_load_balancing();
458 }
459 };
460
461 auto has_pdat = [&]() {
462 bool ret = false;
463 using namespace shamrock::patch;
464 sched.for_each_local_patchdata([&](const Patch &p, PatchDataLayer &pdat) {
465 ret = true;
466 });
467 return ret;
468 };
469
470 shambase::Timer time_part_gen;
471 time_part_gen.start();
472
473 if (shamcomm::world_rank() == 0) {
474 logger::normal_ln("SPH setup", "generating particles ...");
475 }
476
477 while (!setup->is_done()) {
478 shambase::Timer timer_gen;
479 timer_gen.start();
480
481 shamrock::patch::PatchDataLayer tmp = setup->next_n(gen_step);
482
483 if (solver_config.track_particles_id) {
484 // This bit set the tracking id of the particles
485 // But be carefull this assume that the particle injection order
486 // is independant from the MPI world size. It should be the case for most setups
487 // but some generator could miss this assumption.
488 // If that is the case please report the issue
489
490 u64 loc_inj = tmp.get_obj_cnt();
491
492 u64 offset_init = 0;
494 &loc_inj, &offset_init, 1, get_mpi_type<u64>(), MPI_SUM, MPI_COMM_WORLD);
495
496 // we must add the number of already injected part such that the
497 // offset start at the right spot.
498 // The only thing that bothers me is that this can not handle the case where multiple
499 // setups of things like that are applied. But in principle no sane person would do such
500 // a thing...
501 offset_init += injected_parts;
502
503 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
504 auto &q = shambase::get_check_ref(dev_sched).get_queue();
505
506 if (loc_inj > 0) {
507 sham::DeviceBuffer<u64> part_ids(loc_inj, dev_sched);
508
510 q,
512 sham::MultiRef{part_ids},
513 loc_inj,
514 [offset_init](u32 i, u64 *__restrict part_ids) {
515 part_ids[i] = i + offset_init;
516 });
517
518 tmp.get_field<u64>(tmp.pdl().get_field_idx<u64>("part_id"))
519 .overwrite(part_ids, loc_inj);
520 }
521 }
522
523 to_insert.insert_elements(tmp);
524
525 u64 sum_push = shamalgs::collective::allreduce_sum<u64>(tmp.get_obj_cnt());
526 u64 sum_all = shamalgs::collective::allreduce_sum<u64>(to_insert.get_obj_cnt());
527
528 u64 min_rank = shamalgs::collective::allreduce_min<u64>(to_insert.get_obj_cnt());
529 u64 max_rank = shamalgs::collective::allreduce_max<u64>(to_insert.get_obj_cnt());
530
531 timer_gen.stop();
532
533 if (shamcomm::world_rank() == 0) {
534 f64 part_per_sec = f64(sum_push) / f64(timer_gen.elapsed_sec());
536 "SPH setup",
537 sham::format(
538 "Nstep = {} ( {:.1e} ) Ntotal = {} ( {:.1e} rank min = {:.1e} max = {:.1e}) "
539 "rate = {:e} N.s^-1",
540 sum_push,
541 f64(sum_push),
542 sum_all,
543 f64(sum_all),
544 part_per_sec,
545 f64(min_rank),
546 f64(max_rank)));
547 }
548
549 if (setup_log) {
550 setup_log.value().update_count_per_rank(to_insert.get_obj_cnt());
551 }
552
553 injected_parts += sum_push;
554 }
555
556 time_part_gen.stop();
557 if (shamcomm::world_rank() == 0) {
559 "SPH setup", "the generation step took :", time_part_gen.elapsed_sec(), "s");
560 }
561
562 if (shamcomm::world_rank() == 0) {
564 "SPH setup", "final particle count =", injected_parts, "beginning injection ...");
565 }
566
568 f64 mpi_timer_start = shamcomm::mpi::get_timer("total");
569
570 // injection part (holy shit this is hard)
571
572 shambase::Timer time_part_inject;
573 time_part_inject.start();
574
575 auto log_inject_status = [&](std::string log_suffix = "") {
576 u64 sum_all = shamalgs::collective::allreduce_sum<u64>(to_insert.get_obj_cnt());
577
578 u32 rank_without_patch
579 = shamalgs::collective::allreduce_sum<u32>(sched.patch_list.local.size() == 0 ? 1 : 0);
580
581 if (shamcomm::world_rank() == 0) {
583 "SPH setup",
584 sham::format(
585 "injected {:12} / {:} => {:5.1f}% | ranks with patchs = {:d} / {:d} {}",
586 injected_parts - sum_all,
587 injected_parts,
588 f64(injected_parts - sum_all) / f64(injected_parts) * 100.0,
589 shamcomm::world_size() - rank_without_patch,
591 log_suffix));
592 }
593
594 if (setup_log) {
595 setup_log.value().update_count_per_rank(to_insert.get_obj_cnt());
596 }
597 };
598
599 auto inject_in_local_domains =
600 [&sched, &inserter, &compute_load, &insert_step, &log_inject_status](
603
604 bool has_been_limited = true;
605
606 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
607 sham::DeviceBuffer<u32> mask_get_ids_where(0, dev_sched);
608
609 while (has_been_limited) {
610 has_been_limited = false;
611 using namespace shamrock::patch;
612
613 // inject in local domains first
614 PatchCoordTransform<Tvec> ptransf = sched.get_sim_box().get_patch_transform<Tvec>();
615 sched.for_each_local_patchdata([&](const Patch &p, PatchDataLayer &pdat) {
616 shammath::CoordRange<Tvec> patch_coord = ptransf.to_obj_coord(p);
617
618 PatchDataField<Tvec> &xyz = to_insert.get_field<Tvec>(0);
619
620 auto ids = xyz.get_ids_where_recycle_buffer(
621 mask_get_ids_where,
622 [](auto access, u32 id, shammath::CoordRange<Tvec> patch_coord) {
623 Tvec tmp = access[id];
624 return patch_coord.contain_pos(tmp);
625 },
626 patch_coord);
627
628 if (ids.get_size() > insert_step) {
629 ids.resize(insert_step);
630 has_been_limited = true;
631 }
632
633 if (ids.get_size() > 0) {
634 to_insert.extract_elements(ids, pdat);
635 }
636 });
637
638 sched.check_patchdata_locality_correctness();
639
640 inserter.balance_load(compute_load);
641
642 has_been_limited
643 = !shamalgs::collective::are_all_rank_true(!has_been_limited, MPI_COMM_WORLD);
644
645 if (has_been_limited) {
646 // since we will restart this one let's print
647 log_inject_status(" -> local loop <-");
648 }
649 }
650 };
651
652 auto get_index_per_ranks = [&](f64 &timer_result) {
654
655 shambase::Timer time_get_index_per_ranks;
656 time_get_index_per_ranks.start();
657
658 SerialPatchTree<Tvec> sptree = SerialPatchTree<Tvec>::build(sched);
659 sptree.attach_buf();
660
661 // find where each particle should be inserted
662 PatchDataField<Tvec> &pos_field = to_insert.get_field<Tvec>(0);
663
664 if (pos_field.get_nvar() != 1) {
666 }
667
668 sycl::buffer<u64> new_id_buf = sptree.compute_patch_owner(
669 shamsys::instance::get_compute_scheduler_ptr(),
670 pos_field.get_buf(),
671 pos_field.get_obj_cnt());
672
673 std::unordered_map<i32, std::vector<u32>> index_per_ranks;
674 bool err_id_in_newid = false;
675 {
676 sycl::host_accessor nid{new_id_buf, sycl::read_only};
677 for (u32 i = 0; i < pos_field.get_obj_cnt(); i++) {
678 u64 patch_id = nid[i];
679 bool err = patch_id == u64_max;
680 err_id_in_newid = err_id_in_newid || (err);
681
682 i32 rank = sched.get_patch_rank_owner(patch_id);
683 index_per_ranks[rank].push_back(i);
684 }
685 }
686
687 if (err_id_in_newid) {
689 "a new id could not be computed");
690 }
691
692 time_get_index_per_ranks.stop();
693 timer_result = time_get_index_per_ranks.elapsed_sec();
694
695 return index_per_ranks;
696 };
697
698 f64 total_time_rank_getter = 0;
699 f64 max_time_rank_getter = 0;
700
702 u32 step_count = 0;
703 while (!shamalgs::collective::are_all_rank_true(to_insert.is_empty(), MPI_COMM_WORLD)) {
704
705 // assume that the sched is synchronized and that there is at least a patch.
706 // TODO actually check that
707
708 using namespace shamrock::patch;
709
710 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
711
712 inject_in_local_domains(to_insert);
713
714 f64 timer_get_index_per_ranks = 0;
715 std::unordered_map<i32, std::vector<u32>> index_per_ranks
716 = get_index_per_ranks(timer_get_index_per_ranks);
717 total_time_rank_getter += timer_get_index_per_ranks;
718 max_time_rank_getter = std::max(max_time_rank_getter, timer_get_index_per_ranks);
719
720 // allgather the list of messages
721 // format:(u32_2(sender_rank, receiver_rank), u64(indices_size))
722 std::vector<u64> send_msg;
723 for (auto &[rank, indices] : index_per_ranks) {
724 send_msg.push_back(sham::pack32(shamcomm::world_rank(), rank));
725 send_msg.push_back(indices.size());
726 }
727
728 u64 max_send = (1 << 24) / shamcomm::world_size();
729 bool sync_limited = false;
730 if (send_msg.size() > max_send) {
731
732 // here we must pack the send_msg infos in structs in order to keep
733 // them together during shuffle
734
735 struct tmp {
736 u64 ranks, size;
737 };
738
739 // build the vector of structs
740 std::vector<tmp> tmp_vec;
741 tmp_vec.reserve(send_msg.size() / 2);
742 for (u64 i = 0; i < send_msg.size(); i += 2) {
743 tmp_vec.push_back({send_msg[i], send_msg[i + 1]});
744 }
745
746 // shuffle the messages infos
747 u64 local_seed = u64(golden_number * 1000 * step_count + shamcomm::world_rank());
748 std::mt19937_64 eng_local_msg(local_seed);
749 std::shuffle(tmp_vec.begin(), tmp_vec.end(), eng_local_msg);
750
751 // build the new send_msg
752 std::vector<u64> send_msg_new;
753 send_msg_new.reserve(max_send);
754 for (auto &t : tmp_vec) {
755 if (send_msg_new.size() >= max_send) {
756 break;
757 }
758 send_msg_new.push_back(t.ranks);
759 send_msg_new.push_back(t.size);
760 }
761
762 send_msg = send_msg_new;
763 sync_limited = true;
764 }
765
766 std::vector<u64> recv_msg;
767 shamalgs::collective::vector_allgatherv(send_msg, recv_msg, MPI_COMM_WORLD);
768
769 std::vector<std::tuple<u32, u32, u64>> msg_list;
770 for (u64 i = 0; i < recv_msg.size(); i += 2) {
771 u32_2 sender_receiver = sham::unpack32(recv_msg[i]);
772 u64 indices_size = recv_msg[i + 1];
773
774 u32 sender_rank = sender_receiver.x();
775 u32 receiver_rank = sender_receiver.y();
776
777 if (sender_rank == receiver_rank) {
778 continue; // only mean that it was not fully inserted in the patch
779 }
780
781 msg_list.push_back(std::make_tuple(sender_rank, receiver_rank, indices_size));
782 }
783
784 if (setup_log) {
785 setup_log.value().update_msg_list(msg_list);
786 }
787
788 // shuffle msg_list according to seed golden_number*1000*step_count
789 std::mt19937 eng_global_msg(u64(golden_number * 1000 * step_count));
790 std::shuffle(msg_list.begin(), msg_list.end(), eng_global_msg);
791
792 // now that we are in sync we can determine who should send to who
793
794 std::vector<u64> msg_count_rank(shamcomm::world_size());
795 std::vector<u64> comm_size_rank(shamcomm::world_size());
796
797 std::vector<std::tuple<u32, u32, u64>> rank_msg_list;
798
799 bool was_count_limited = false;
800 bool was_size_limited = false;
801 bool was_msg_size_limited = false;
802
803 for (auto &[sender_rank, receiver_rank, indices_size] : msg_list) {
804
805 bool msg_count_limit_not_reached = msg_count_rank.at(receiver_rank) < msg_limit
806 && msg_count_rank.at(sender_rank) < msg_limit;
807
808 bool recv_size_limit_not_reached = comm_size_rank.at(receiver_rank) < data_count_limit
809 && comm_size_rank.at(sender_rank) < data_count_limit;
810
811 was_count_limited = was_count_limited || !msg_count_limit_not_reached;
812 was_size_limited = was_size_limited || !recv_size_limit_not_reached;
813
814 bool can_send_recv = msg_count_limit_not_reached && recv_size_limit_not_reached;
815
816 u64 msg_size = std::min(indices_size, max_message_size);
817 msg_size = std::min(msg_size, data_count_limit);
818 was_msg_size_limited = was_msg_size_limited || (msg_size < indices_size);
819
820 if (can_send_recv) {
821 if (sender_rank == shamcomm::world_rank()
822 || receiver_rank == shamcomm::world_rank()) {
823 if (msg_size > 0) {
824 rank_msg_list.push_back(
825 std::make_tuple(sender_rank, receiver_rank, msg_size));
826 }
827 }
828 }
829
830 msg_count_rank.at(receiver_rank) += 1;
831 msg_count_rank.at(sender_rank) += 1;
832 comm_size_rank.at(receiver_rank) += msg_size;
833 comm_size_rank.at(sender_rank) += msg_size;
834 }
835
836 // logger::raw_ln(
837 // shamcomm::world_rank(),
838 // was_count_limited,
839 // was_size_limited,
840 // msg_count_rank,
841 // comm_size_rank);
842
843 // logger::info_ln(
844 // "SPH setup", "rank", shamcomm::world_rank(), "rank_msg_list", rank_msg_list);
845
846 // extract the data
848 sham::DeviceBuffer idx_to_rem = sham::DeviceBuffer<u32>(0, dev_sched);
849 for (auto &[sender_rank, receiver_rank, indices_size] : rank_msg_list) {
850 if (sender_rank == shamcomm::world_rank()) {
851 std::vector<u32> &idx_to_extract = index_per_ranks[receiver_rank];
852 sham::DeviceBuffer _tmp = sham::DeviceBuffer<u32>(idx_to_extract.size(), dev_sched);
853 _tmp.copy_from_stdvec(idx_to_extract);
854
855 if (_tmp.get_size() > indices_size) {
856 _tmp.resize(indices_size);
857 }
858
859 PatchDataLayer _tmp_pdat = PatchDataLayer(sched.get_layout_ptr_old());
860 to_insert.append_subset_to(_tmp, _tmp.get_size(), _tmp_pdat);
861
862 idx_to_rem.append(_tmp);
863
864 send_data.add_obj(sender_rank, receiver_rank, std::move(_tmp_pdat));
865 }
866 }
867
868 to_insert.remove_ids(idx_to_rem, idx_to_rem.get_size());
869
870 // comm the data to the right ranks
872
873 shamalgs::collective::serialize_sparse_comm<PatchDataLayer>(
874 dev_sched,
875 std::move(send_data),
876 recv_dat,
877 [&](u64 id) {
878 return id; // here the ids in the DDshared are the MPI ranks
879 },
880 [&](PatchDataLayer &pdat) {
881 shamalgs::SerializeHelper ser(dev_sched);
882 ser.allocate(pdat.serialize_buf_byte_size());
883 pdat.serialize_buf(ser);
884 return ser.finalize();
885 },
886 [&](sham::DeviceBuffer<u8> &&buf) {
887 // exchange the buffer held by the distrib data and give it to the
888 // serializer
889 shamalgs::SerializeHelper ser(dev_sched, std::forward<sham::DeviceBuffer<u8>>(buf));
890 return PatchDataLayer::deserialize_buf(ser, sched.get_layout_ptr_old());
891 },
892 comm_cache);
893
894 // insert the data into the data to be inserted
895 recv_dat.for_each([&](u64 sender, u64 receiver, PatchDataLayer &pdat) {
896 to_insert.insert_elements(pdat);
897 });
898
899 was_count_limited
900 = !shamalgs::collective::are_all_rank_true(!was_count_limited, MPI_COMM_WORLD);
901 was_size_limited
902 = !shamalgs::collective::are_all_rank_true(!was_size_limited, MPI_COMM_WORLD);
903 was_msg_size_limited
904 = !shamalgs::collective::are_all_rank_true(!was_msg_size_limited, MPI_COMM_WORLD);
905 bool was_sync_limited
906 = !shamalgs::collective::are_all_rank_true(!sync_limited, MPI_COMM_WORLD);
907
908 std::string log_suffix = "";
909 if (was_count_limited) {
910 log_suffix += " (msg count limited)";
911 }
912 if (was_size_limited) {
913 log_suffix += " (total msg size limited)";
914 }
915 if (was_msg_size_limited) {
916 log_suffix += " (msg size limited)";
917 }
918 if (was_sync_limited) {
919 log_suffix += " (sync limited)";
920 }
921 log_suffix += sham::format(" (msg count : {})", recv_msg.size());
922 log_inject_status(" <- global loop ->" + log_suffix);
923
924 f64 worst_time_get_index_per_ranks
925 = shamalgs::collective::allreduce_max<f64>(timer_get_index_per_ranks);
926
927 step_count++;
928 }
929
930 if (setup_log) {
931 setup_log.value().dump_state();
932 }
933
934 shamcomm::mpi::Barrier(MPI_COMM_WORLD);
935 time_part_inject.stop();
936 if (shamcomm::world_rank() == 0) {
938 "SPH setup", "the injection step took :", time_part_inject.elapsed_sec(), "s");
939 }
940
942
943 f64 delta_mpi_timer = shamcomm::mpi::get_timer("total") - mpi_timer_start;
944 f64 t_dev_alloc
945 = (mem_perf_infos_end.time_alloc_device - mem_perf_infos_start.time_alloc_device)
946 + (mem_perf_infos_end.time_free_device - mem_perf_infos_start.time_free_device);
947 f64 t_host_alloc = (mem_perf_infos_end.time_alloc_host - mem_perf_infos_start.time_alloc_host)
948 + (mem_perf_infos_end.time_free_host - mem_perf_infos_start.time_free_host);
949
950 { // perf infos
951 std::vector<f64> time_rank_getter_all_ranks
952 = shamalgs::collective::gather(total_time_rank_getter);
953 std::vector<f64> max_time_rank_getter_all_ranks
954 = shamalgs::collective::gather(max_time_rank_getter);
955 std::vector<f64> mpi_timer_all_ranks = shamalgs::collective::gather(delta_mpi_timer);
956 std::vector<f64> alloc_time_device_all_ranks = shamalgs::collective::gather(t_dev_alloc);
957 std::vector<f64> alloc_time_host_all_ranks = shamalgs::collective::gather(t_host_alloc);
958 std::vector<size_t> max_mem_device_all_ranks
959 = shamalgs::collective::gather(mem_perf_infos_end.max_allocated_byte_device);
960 std::vector<size_t> max_mem_host_all_ranks
961 = shamalgs::collective::gather(mem_perf_infos_end.max_allocated_byte_host);
962
963 if (shamcomm::world_rank() == 0) {
964 f64 time_part_inject_sec = time_part_inject.elapsed_sec();
965 f64 sum_t = time_part_inject_sec * shamcomm::world_size();
966
967 f64 sum_time_rank_getter = std::accumulate(
968 time_rank_getter_all_ranks.begin(), time_rank_getter_all_ranks.end(), 0.0);
969 f64 max_time_rank_getter = *std::max_element(
970 max_time_rank_getter_all_ranks.begin(), max_time_rank_getter_all_ranks.end());
971 f64 sum_mpi
972 = std::accumulate(mpi_timer_all_ranks.begin(), mpi_timer_all_ranks.end(), 0.0);
973 f64 sum_alloc_device = std::accumulate(
974 alloc_time_device_all_ranks.begin(), alloc_time_device_all_ranks.end(), 0.0);
975 f64 sum_alloc_host = std::accumulate(
976 alloc_time_host_all_ranks.begin(), alloc_time_host_all_ranks.end(), 0.0);
977 size_t sum_mem_device_total = std::accumulate(
978 max_mem_device_all_ranks.begin(), max_mem_device_all_ranks.end(), 0_u64);
979 size_t sum_mem_host_total = std::accumulate(
980 max_mem_host_all_ranks.begin(), max_mem_host_all_ranks.end(), 0_u64);
981
982 static constexpr u32 cols_count = 6;
983
984 using Table = shambase::table;
985
986 Table table(6);
987
988 table.add_double_rule();
989 table.add_data(
990 {"rank", "rank get (sum/max)", "MPI", "alloc d% h%", "mem (max) d", "mem (max) h"},
991 Table::center);
992 table.add_double_rule();
993 for (u32 i = 0; i < shamcomm::world_size(); i++) {
994 table.add_data(
995 {sham::format("{:<4}", i),
996 sham::format(
997 "{:.2f}s / {:.2f}s",
998 time_rank_getter_all_ranks[i],
999 max_time_rank_getter_all_ranks[i]),
1000 sham::format("{:.2f}s", mpi_timer_all_ranks[i]),
1001 sham::format(
1002 "{:>.1f}% {:<.1f}%",
1003 100 * (alloc_time_device_all_ranks[i] / time_part_inject_sec),
1004 100 * (alloc_time_host_all_ranks[i] / time_part_inject_sec)),
1005 sham::format("{}", shambase::readable_sizeof(max_mem_device_all_ranks[i])),
1006 sham::format("{}", shambase::readable_sizeof(max_mem_host_all_ranks[i]))},
1007 Table::right);
1008 }
1009 if (shamcomm::world_size() > 1) {
1010 table.add_rulled_data({"", "<avg> / <max>", "<avg>", "<avg>", "<sum>", "<sum>"});
1011 table.add_data(
1012 {"all",
1013 sham::format(
1014 "{:.2f}s / {:.2f}s",
1015 sum_time_rank_getter / shamcomm::world_size(),
1016 max_time_rank_getter),
1017 sham::format("{:.2f}s", sum_mpi / shamcomm::world_size()),
1018 sham::format(
1019 "{:>.1f}% {:<.1f}%",
1020 100 * (sum_alloc_device / sum_t),
1021 100 * (sum_alloc_host / sum_t)),
1022 sham::format("{}", shambase::readable_sizeof(sum_mem_device_total)),
1023 sham::format("{}", shambase::readable_sizeof(sum_mem_host_total))},
1024 Table::right);
1025 }
1026 table.add_rule();
1027 logger::info_ln("SPH setup", "injection perf report:" + table.render());
1028 }
1029 }
1030
1031 if (part_reordering) {
1032 modules::ParticleReordering<Tvec, u32, SPHKernel>(context, solver_config, storage)
1033 .reorder_particles();
1034 }
1035
1036 time_setup.stop();
1037 if (shamcomm::world_rank() == 0) {
1038 logger::normal_ln("SPH setup", "the setup took :", time_setup.elapsed_sec(), "s");
1039 }
1040}
1041
1042template<class Tvec, template<class> class SPHKernel>
1043inline std::shared_ptr<shammodels::sph::modules::ISPHSetupNode> shammodels::sph::modules::
1044 SPHSetup<Tvec, SPHKernel>::make_modifier_warp_disc(
1045 SetupNodePtr parent, Tscal Rwarp, Tscal Hwarp, Tscal inclination, Tscal posangle) {
1046 return std::shared_ptr<ISPHSetupNode>(new ModifierApplyDiscWarp<Tvec, SPHKernel>(
1047 context, solver_config, parent, Rwarp, Hwarp, inclination, posangle));
1048}
1049
1050template<class Tvec, template<class> class SPHKernel>
1051inline std::shared_ptr<shammodels::sph::modules::ISPHSetupNode> shammodels::sph::modules::
1052 SPHSetup<Tvec, SPHKernel>::make_modifier_custom_warp(
1053 SetupNodePtr parent,
1054 std::function<Tscal(Tscal)> inc_profile,
1055 std::function<Tscal(Tscal)> psi_profile,
1056 std::function<Tvec(Tscal)> k_profile) {
1057 return std::shared_ptr<ISPHSetupNode>(new ModifierApplyCustomWarp<Tvec, SPHKernel>(
1058 context, solver_config, parent, inc_profile, psi_profile, k_profile));
1059}
1060
1061template<class Tvec, template<class> class SPHKernel>
1062inline std::shared_ptr<shammodels::sph::modules::ISPHSetupNode> shammodels::sph::modules::
1063 SPHSetup<Tvec, SPHKernel>::make_modifier_add_offset(
1064 SetupNodePtr parent, Tvec offset_postion, Tvec offset_velocity) {
1065
1066 return std::shared_ptr<ISPHSetupNode>(
1067 new ModifierOffset<Tvec>(context, parent, offset_postion, offset_velocity));
1068}
1069
1070template<class Tvec, template<class> class SPHKernel>
1071inline std::shared_ptr<shammodels::sph::modules::ISPHSetupNode> shammodels::sph::modules::SPHSetup<
1072 Tvec,
1073 SPHKernel>::make_modifier_filter(SetupNodePtr parent, std::function<bool(Tvec)> filter) {
1074
1075 return std::shared_ptr<ISPHSetupNode>(
1076 new ModifierFilter<Tvec, SPHKernel>(context, parent, filter));
1077}
1078
1079template<class Tvec, template<class> class SPHKernel>
1080inline std::shared_ptr<shammodels::sph::modules::ISPHSetupNode> shammodels::sph::modules::
1081 SPHSetup<Tvec, SPHKernel>::make_modifier_split_part(
1082 SetupNodePtr parent, u64 n_split, u64 seed, Tscal h_scaling) {
1083 return std::shared_ptr<ISPHSetupNode>(
1084 new ModifierSplitPart<Tvec>(context, parent, n_split, seed, h_scaling));
1085}
1086
1087using namespace shammath;
1091
constexpr const char * xyz
Position field (3D coordinates).
SPH setup generator placing particles on a cubic lattice.
SPH setup generator placing particles on a HCP lattice.
Header file describing a Node Instance.
double f64
Alias for double.
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
std::int32_t i32
32 bit integer
Collective boolean reduction to check if all ranks have true as input.
bool are_all_rank_true(bool input, MPI_Comm comm)
return true only if all ranks have true as input
The MPI scheduler.
u64 crit_patch_split
splitting limit (if load value > crit_patch_split => patch split)
SchedulerPatchList patch_list
handle the list of the patches of the scheduler
std::vector< shamrock::patch::Patch > local
contain the list of patch owned by the current node
A buffer allocated in USM (Unified Shared Memory).
void copy_from_stdvec(const std::vector< T > &vec)
Copy the content of a std::vector into the buffer.
void resize(size_t new_size, bool keep_data=true)
Resizes the buffer to a given size.
void append(const DeviceBuffer &other)
Append the content of another buffer to this one.
size_t get_size() const
Gets the number of elements in the buffer.
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.
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.
void reset()
Reset the collection to its initial state.
Class Timer measures the time elapsed since the timer was started.
Definition Timer.hpp:36
f64 elapsed_sec() const
Converts the stored nanosecond time to a floating point representation in seconds.
Definition Timer.hpp:88
void start()
Starts the timer.
Definition Timer.hpp:51
void stop()
Stops the timer and stores the elapsed time in nanoseconds.
Definition Timer.hpp:65
Class to insert data in the PatchScheduler.
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
PatchCoordTransform< T > get_patch_transform() const
Get a PatchCoordTransform object that describes the conversion between patch coordinates and domain c...
Definition SimBox.hpp:286
std::vector< int > vector_allgatherv(const std::vector< T > &send_vec, const MPI_Datatype &send_type, std::vector< T > &recv_vec, const MPI_Datatype &recv_type, const MPI_Comm comm)
allgatherv on vector with size query (size querying variant of vector_allgatherv_ks) //TODO add fault...
Definition exchanges.hpp:98
MemPerfInfos get_mem_perf_info()
Retrieve the memory performance information.
Boolean reduction algorithm for checking if all elements are non-zero.
void kernel_call(sham::DeviceQueue &q, RefIn in, RefOut in_out, u32 n, Functor &&func, SourceLocation &&callsite=SourceLocation{})
Submit a kernel to a SYCL queue.
std::string readable_sizeof(double size)
given a sizeof value return a readble string Example : readable_sizeof(1e9) -> "1....
Definition string.hpp:80
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
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
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.
i32 world_rank()
Gives the rank of the current process in the MPI communicator.
Definition worldInfo.cpp:41
i32 world_size()
Gives the size of the MPI communicator.
Definition worldInfo.cpp:39
namespace for math utility
Definition AABB.hpp:26
constexpr u64 u64_max
u64 max value
void normal_ln(std::string module_name, Types... var2)
Prints a log message with multiple arguments followed by a newline.
Definition logs.hpp:132
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 err(std::string module_name, Types... var2)
Prints a log message with multiple arguments.
Definition logs.hpp:132
#define __shamrock_stack_entry()
Macro to create a stack entry.
shambase::details::BasicStackEntry StackEntry
Alias for shambase::details::BasicStackEntry.
Structure to store the performance informations about memory allocation and deallocation.
f64 time_alloc_host
Time spent allocating memory on the host.
size_t max_allocated_byte_host
max bytes allocated on the host
f64 time_free_device
Time spent deallocating memory on the device.
size_t max_allocated_byte_device
max bytes allocated on the device
f64 time_alloc_device
Time spent allocating memory on the device.
f64 time_free_host
Time spent deallocating memory on the host.
A class that references multiple buffers or similar objects.
Definition MultiRef.hpp:33
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
u64 id_patch
unique key that identify the patch
Definition Patch.hpp:86
Functions related to the MPI communicator.
#define ON_RANK_0(x)
Macro to execute code only on rank 0.
Definition worldInfo.hpp:73
void Exscan(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
MPI wrapper for MPI_Exscan.
Definition wrapper.cpp:168
void Barrier(MPI_Comm comm)
MPI wrapper for MPI_Barrier.
Definition wrapper.cpp:196
f64 get_timer(std::string timername)
get a timer value
Definition wrapper.cpp:46
void Allreduce(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
MPI wrapper for MPI_Allreduce.
Definition wrapper.cpp:121