Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
ShamrockCtx.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
23#include <map>
24#include <memory>
25#include <tuple>
26#include <vector>
27
28class ShamAPIException : public std::exception {
29 public:
30 explicit ShamAPIException(const char *message) : msg_(message) {}
31
32 explicit ShamAPIException(const std::string &message) : msg_(message) {}
33
34 virtual ~ShamAPIException() noexcept {}
35
36 virtual const char *what() const noexcept { return msg_.c_str(); }
37
38 protected:
39 std::string msg_;
40};
41
43 public:
44 std::shared_ptr<shamrock::patch::PatchDataLayerLayout> pdl;
45 std::unique_ptr<PatchScheduler> sched;
46
47 inline void pdata_layout_new() {
48 if (sched) {
49 throw ShamAPIException("cannot modify patch data layout while the scheduler is on");
50 }
51 pdl = std::make_shared<shamrock::patch::PatchDataLayerLayout>();
52 }
53
54 // inline void pdata_layout_do_double_prec_mode(){
55 // if(sched){
56 // throw ShamAPIException("cannot modify patch data layout while the scheduler is on");
57 // }
58 // pdl->xyz_mode = xyz64;
59 // }
60 //
61 // inline void pdata_layout_do_single_prec_mode(){
62 // if(sched){
63 // throw ShamAPIException("cannot modify patch data layout while the scheduler is on");
64 // }
65 // pdl->xyz_mode = xyz32;
66 //}
67
68 inline shamrock::patch::PatchDataLayerLayout &get_pdl_write() {
69 if (sched) {
70 throw ShamAPIException("cannot modify patch data layout while the scheduler is on");
71 }
72 return shambase::get_check_ref(pdl);
73 }
74
75 template<class T>
76 inline void pdata_layout_add_field(std::string fname, u32 nvar) {
77 if (sched) {
78 throw ShamAPIException("cannot modify patch data layout while the scheduler is on");
79 }
80 pdl->add_field<T>(fname, nvar);
81 }
82
83 inline void pdata_layout_add_field_t(std::string fname, u32 nvar, std::string type) {
84 if (sched) {
85 throw ShamAPIException("cannot modify patch data layout while the scheduler is on");
86 }
87 pdl->add_field_t(fname, nvar, type);
88 }
89
90 inline void pdata_layout_print() {
91 if (!pdl) {
92 throw ShamAPIException("patch data layout is not initialized");
93 }
94 std::cout << pdl->get_description_str() << std::endl;
95 }
96
97 inline void dump_status() {
98 if (!sched) {
99 throw ShamAPIException("scheduler is not initialized");
100 }
101
102 logger::raw_ln(sched->dump_status());
103 }
104
105 inline void init_sched(u64 crit_split, u64 crit_merge) {
106
107 if (!pdl) {
108 throw ShamAPIException("patch data layout is not initialized");
109 }
110
111 sched = std::make_unique<PatchScheduler>(pdl, crit_split, crit_merge);
112 sched->init_mpi_required_types();
113 }
114
115 inline void close_sched() { sched.reset(); }
116
117 inline ShamrockCtx() {
118 // logfiles::open_log_files();
119 }
120
121 inline ~ShamrockCtx() {
122 // logfiles::close_log_files();
123 }
124
125 inline std::vector<std::unique_ptr<shamrock::patch::PatchDataLayer>> gather_data(u32 rank) {
126 return sched->gather_data(rank);
127 }
128
129 inline std::vector<std::unique_ptr<shamrock::patch::PatchDataLayer>> allgather_data() {
130
131 using namespace shamsys::instance;
132 using namespace shamrock::patch;
133
134 std::vector<std::unique_ptr<PatchDataLayer>> recv_data;
135
136 for (u32 i = 0; i < shamcomm::world_size(); i++) {
137 if (i == shamcomm::world_rank()) {
138 recv_data = sched->gather_data(i);
139 } else {
140 sched->gather_data(i);
141 }
142 }
143
144 return recv_data;
145 }
146
147 void set_coord_domain_bound(std::tuple<f64_3, f64_3> box) {
148
149 if (!pdl) {
150 throw ShamAPIException("patch data layout is not initialized");
151 }
152
153 if (!sched) {
154 throw ShamAPIException("scheduler is not initialized");
155 }
156
157 auto [a, b] = box;
158
159 if (pdl->check_main_field_type<f32_3>()) {
160 auto conv_vec = [](f64_3 v) -> f32_3 {
161 return {v.x(), v.y(), v.z()};
162 };
163
164 f32_3 vec0 = conv_vec(a);
165 f32_3 vec1 = conv_vec(b);
166
167 sched->set_coord_domain_bound<f32_3>(vec0, vec1);
168 } else if (pdl->check_main_field_type<f64_3>()) {
169
170 sched->set_coord_domain_bound<f64_3>(a, b);
171 } else {
173 "the chosen type for the main field is not handled");
174 }
175 }
176
177 inline void scheduler_step(bool do_split_merge, bool do_load_balancing) {
178 if (!sched) {
179 throw ShamAPIException("scheduler is not initialized");
180 }
181 sched->scheduler_step(do_split_merge, do_load_balancing);
182 }
183
184 inline std::vector<shamrock::patch::Patch> get_patch_list_global() {
185 if (!sched) {
186 throw ShamAPIException("scheduler is not initialized");
187 }
188 return sched->patch_list.global;
189 }
190
192 inline bool is_scheduler_initialized() { return bool(sched); }
193};
MPI scheduler.
Header file for the patch struct and related function.
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
bool is_scheduler_initialized()
returns true if the scheduler is initialized
This header file contains utility functions related to exception handling in the code.
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:110
i32 world_rank()
Gives the rank of the current process in the MPI communicator.
Definition worldInfo.cpp:40
i32 world_size()
Gives the size of the MPI communicator.
Definition worldInfo.cpp:38
header for PatchData related function and declaration