-
Notifications
You must be signed in to change notification settings - Fork 34
[Patch] Follow up to #1531 - Layers: Add layers in layout, and replace it in scheduler #1701
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 21 commits
b4970ff
28dae97
b6306de
4ec5272
80d52df
98e3e7d
05532cc
0254cc9
528b839
830e4f1
077a6bd
1370bd5
61505cf
305114d
cf7fc6b
8bb890a
9f6b209
b39e5f1
4ed61da
fdbd39c
755eddc
c0fb6bd
d042116
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| // -------------------------------------------------------// | ||
| // | ||
| // SHAMROCK code for hydrodynamics | ||
| // Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me> | ||
| // SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1 | ||
| // Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information | ||
| // | ||
| // -------------------------------------------------------// | ||
|
|
||
| #pragma once | ||
|
|
||
| /** | ||
| * @file PatchDataLayout.hpp | ||
| * @author Timothée David--Cléris (tim.shamrock@proton.me) | ||
| * @author Yona Lapeyre (yona.lapeyre@ens-lyon.fr) | ||
| * @brief | ||
| */ | ||
|
|
||
| #include "shambase/memory.hpp" | ||
| #include "shamrock/patch/PatchDataLayerLayout.hpp" | ||
| #include <initializer_list> | ||
| #include <memory> | ||
| #include <vector> | ||
|
|
||
| namespace shamrock::patch { | ||
|
|
||
| /** | ||
| * @brief PatchDataLayerLayout container class | ||
| * This class store the layout of the layers | ||
| */ | ||
| class PatchDataLayout { | ||
| public: | ||
| struct LayerEntry { | ||
| std::shared_ptr<shamrock::patch::PatchDataLayerLayout> layout; | ||
| std::string name; | ||
|
|
||
| inline friend bool operator==(const LayerEntry &lhs, const LayerEntry &rhs) { | ||
| return lhs.name == rhs.name | ||
| && shambase::get_check_ref(lhs.layout) | ||
| == shambase::get_check_ref(rhs.layout); | ||
| } | ||
| }; | ||
|
|
||
| std::vector<LayerEntry> layer_layouts; | ||
|
|
||
| PatchDataLayout() = default; | ||
|
|
||
| PatchDataLayout(const std::vector<std::string> &layer_names) | ||
| : layer_layouts{layer_names.size()} { | ||
| for (size_t i = 0; i < layer_layouts.size(); i++) { | ||
| layer_layouts[i].name = layer_names[i]; | ||
| layer_layouts[i].layout = std::make_shared<PatchDataLayerLayout>(); | ||
| } | ||
| } | ||
|
Comment on lines
+48
to
+54
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This constructor can be implemented more idiomatically using modern C++ features like PatchDataLayout(const std::vector<std::string> &layer_names) {
layer_layouts.reserve(layer_names.size());
for (const auto &name : layer_names) {
layer_layouts.push_back({std::make_shared<PatchDataLayerLayout>(), name});
}
} |
||
|
|
||
| PatchDataLayout(std::initializer_list<std::string> layer_names) | ||
| : PatchDataLayout(std::vector<std::string>(layer_names)) {} | ||
|
|
||
| size_t get_layer_count() const { return layer_layouts.size(); } | ||
|
|
||
| size_t get_layer_index(const std::string &name) const { | ||
| for (size_t i = 0; i < layer_layouts.size(); i++) { | ||
| if (layer_layouts[i].name == name) { | ||
| return i; | ||
| } | ||
| } | ||
| throw shambase::make_except_with_loc<std::invalid_argument>( | ||
| "the requested layer does not exists"); | ||
| } | ||
|
Comment on lines
+61
to
+69
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This loop can be replaced with size_t get_layer_index(const std::string &name) const {
auto it = std::find_if(layer_layouts.begin(), layer_layouts.end(),
[&name](const LayerEntry &entry) {
return entry.name == name;
});
if (it != layer_layouts.end()) {
return std::distance(layer_layouts.begin(), it);
}
throw shambase::make_except_with_loc<std::invalid_argument>(
"the requested layer does not exists");
} |
||
|
|
||
| inline std::shared_ptr<PatchDataLayerLayout> &get_layer_ptr(size_t idx) { | ||
| return layer_layouts.at(idx).layout; | ||
| } | ||
|
|
||
| inline const std::shared_ptr<PatchDataLayerLayout> &get_layer_ptr(size_t idx) const { | ||
| return layer_layouts.at(idx).layout; | ||
| } | ||
|
|
||
| inline PatchDataLayerLayout &get_layer_ref(size_t idx) { | ||
| return shambase::get_check_ref(get_layer_ptr(idx)); | ||
| } | ||
|
|
||
| inline std::shared_ptr<PatchDataLayerLayout> &get_layer_ptr(const std::string &name) { | ||
| return get_layer_ptr(get_layer_index(name)); | ||
| } | ||
|
|
||
| inline const std::shared_ptr<PatchDataLayerLayout> &get_layer_ptr( | ||
| const std::string &name) const { | ||
| return get_layer_ptr(get_layer_index(name)); | ||
| } | ||
|
|
||
| inline PatchDataLayerLayout &get_layer_ref(const std::string &name) { | ||
| return get_layer_ref(get_layer_index(name)); | ||
| } | ||
|
|
||
| inline friend bool operator==(const PatchDataLayout &lhs, const PatchDataLayout &rhs) { | ||
| return lhs.layer_layouts == rhs.layer_layouts; | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Serialize a PatchDataLayout object to a JSON object | ||
| * | ||
| * This function takes a PatchDataLayout object and serializes it to a JSON object. | ||
| * It is used to convert the PatchDataLayout object to a JSON string. | ||
| * | ||
| * @param j The JSON object to serialize the PatchDataLayout object to | ||
| * @param p The PatchDataLayout object to serialize | ||
| */ | ||
| void to_json(nlohmann::json &j, const PatchDataLayout &p); | ||
|
|
||
| /** | ||
| * @brief Deserialize a PatchDataLayout object from a JSON object | ||
| * | ||
| * This function takes a JSON object and deserializes it to a PatchDataLayout object. | ||
| * It is used to convert a JSON string to a PatchDataLayout object. | ||
| * | ||
| * @param j The JSON object to deserialize the PatchDataLayout object from | ||
| * @param p The PatchDataLayout object to deserialize | ||
| */ | ||
| void from_json(const nlohmann::json &j, PatchDataLayout &p); | ||
|
|
||
| } // namespace shamrock::patch | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ | |
| #include "shambase/time.hpp" | ||
| #include "shamalgs/collective/distributedDataComm.hpp" | ||
| #include "shamrock/legacy/patch/utility/patch_field.hpp" | ||
| #include "shamrock/patch/PatchDataLayout.hpp" | ||
| #include "shamrock/solvergraph/NodeSetEdge.hpp" | ||
| #include "shamrock/solvergraph/PatchDataLayerRefs.hpp" | ||
| #include <nlohmann/json.hpp> | ||
|
|
@@ -64,7 +65,7 @@ class PatchScheduler { | |
| using PatchTree = shamrock::scheduler::PatchTree; | ||
| using SchedulerPatchData = shamrock::scheduler::SchedulerPatchData; | ||
|
|
||
| std::shared_ptr<shamrock::patch::PatchDataLayerLayout> pdl_ptr; | ||
| std::shared_ptr<shamrock::patch::PatchDataLayout> pdl_ptr; | ||
|
|
||
| u64 crit_patch_split; ///< splitting limit (if load value > crit_patch_split => patch split) | ||
| u64 crit_patch_merge; ///< merging limit (if load value < crit_patch_merge => patch merge) | ||
|
|
@@ -77,12 +78,29 @@ class PatchScheduler { | |
| std::unordered_set<u64> owned_patch_id; ///< list of owned patch ids updated with | ||
| ///< (owned_patch_id = patch_list.build_local()) | ||
|
|
||
| inline shamrock::patch::PatchDataLayout &pdl() { return shambase::get_check_ref(pdl_ptr); } | ||
|
|
||
| inline std::shared_ptr<shamrock::patch::PatchDataLayout> get_layout_ptr() const { | ||
| return pdl_ptr; | ||
| } | ||
|
|
||
| /// Will be deprecated in favor of the new one with layout support | ||
| inline shamrock::patch::PatchDataLayerLayout &pdl_old() { | ||
| return shambase::get_check_ref(pdl_ptr); | ||
| auto &ref = pdl(); | ||
| if (ref.get_layer_count() != 1) { | ||
| throw shambase::make_except_with_loc<std::invalid_argument>( | ||
| "pdl_old is not supported for multiple layers"); | ||
| } | ||
| return ref.get_layer_ref(0); | ||
| } | ||
|
Comment on lines
117
to
124
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To improve consistency with inline shamrock::patch::PatchDataLayer &pdl_old() {
auto &ref = shambase::get_check_ref(pdl_ptr);
if (ref.get_layer_count() != 1) {
throw shambase::make_except_with_loc<std::invalid_argument>(
"pdl_old is not supported for multiple layers");
}
return ref.get_layer_ref(0);
} |
||
|
|
||
| inline std::shared_ptr<shamrock::patch::PatchDataLayerLayout> get_layout_ptr_old() const { | ||
| return pdl_ptr; | ||
| auto &ref = shambase::get_check_ref(pdl_ptr); | ||
| if (ref.get_layer_count() != 1) { | ||
| throw shambase::make_except_with_loc<std::invalid_argument>( | ||
| "get_layout_ptr_old is not supported for multiple layers"); | ||
| } | ||
| return ref.get_layer_ptr(0); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -98,7 +116,7 @@ class PatchScheduler { | |
| void free_mpi_required_types(); | ||
|
|
||
| PatchScheduler( | ||
| const std::shared_ptr<shamrock::patch::PatchDataLayerLayout> &pdl_ptr, | ||
| const std::shared_ptr<shamrock::patch::PatchDataLayout> &pdl_ptr, | ||
| u64 crit_split, | ||
| u64 crit_merge); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,14 +41,14 @@ class ShamAPIException : public std::exception { | |
|
|
||
| class ShamrockCtx { | ||
| public: | ||
| std::shared_ptr<shamrock::patch::PatchDataLayerLayout> pdl; | ||
| std::shared_ptr<shamrock::patch::PatchDataLayout> pdl; | ||
| std::unique_ptr<PatchScheduler> sched; | ||
|
|
||
| inline void pdata_layout_new() { | ||
| if (sched) { | ||
| throw ShamAPIException("cannot modify patch data layout while the scheduler is on"); | ||
| } | ||
| pdl = std::make_shared<shamrock::patch::PatchDataLayerLayout>(); | ||
| pdl = std::make_shared<shamrock::patch::PatchDataLayout>(std::vector<std::string>{"main"}); | ||
| } | ||
|
|
||
| // inline void pdata_layout_do_double_prec_mode(){ | ||
|
|
@@ -65,7 +65,7 @@ class ShamrockCtx { | |
| // pdl->xyz_mode = xyz32; | ||
| //} | ||
|
|
||
| inline shamrock::patch::PatchDataLayerLayout &get_pdl_write() { | ||
| inline shamrock::patch::PatchDataLayout &get_pdl_write() { | ||
| if (sched) { | ||
| throw ShamAPIException("cannot modify patch data layout while the scheduler is on"); | ||
| } | ||
|
|
@@ -77,21 +77,22 @@ class ShamrockCtx { | |
| if (sched) { | ||
| throw ShamAPIException("cannot modify patch data layout while the scheduler is on"); | ||
| } | ||
| pdl->add_field<T>(fname, nvar); | ||
| shambase::get_check_ref(pdl).get_layer_ref("main").add_field<T>(fname, nvar); | ||
| } | ||
|
|
||
| inline void pdata_layout_add_field_t(std::string fname, u32 nvar, std::string type) { | ||
| if (sched) { | ||
| throw ShamAPIException("cannot modify patch data layout while the scheduler is on"); | ||
| } | ||
| pdl->add_field_t(fname, nvar, type); | ||
| shambase::get_check_ref(pdl).get_layer_ref("main").add_field_t(fname, nvar, type); | ||
| } | ||
|
Comment on lines
83
to
88
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| inline void pdata_layout_print() { | ||
| if (!pdl) { | ||
| throw ShamAPIException("patch data layout is not initialized"); | ||
| } | ||
| std::cout << pdl->get_description_str() << std::endl; | ||
| std::cout << shambase::get_check_ref(pdl).get_layer_ref("main").get_description_str() | ||
| << std::endl; | ||
| } | ||
|
Comment on lines
90
to
96
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| inline void dump_status() { | ||
|
|
@@ -154,9 +155,13 @@ class ShamrockCtx { | |
| throw ShamAPIException("scheduler is not initialized"); | ||
| } | ||
|
|
||
| if (pdl->get_layer_count() > 1) { | ||
| throw ShamAPIException("set_coord_domain_bound is not supported for multiple layers"); | ||
| } | ||
|
|
||
| auto [a, b] = box; | ||
|
|
||
| if (pdl->check_main_field_type<f32_3>()) { | ||
| if (pdl->get_layer_ref("main").check_main_field_type<f32_3>()) { | ||
| auto conv_vec = [](f64_3 v) -> f32_3 { | ||
| return {v.x(), v.y(), v.z()}; | ||
| }; | ||
|
|
@@ -165,7 +170,7 @@ class ShamrockCtx { | |
| f32_3 vec1 = conv_vec(b); | ||
|
|
||
| sched->set_coord_domain_bound<f32_3>(vec0, vec1); | ||
| } else if (pdl->check_main_field_type<f64_3>()) { | ||
| } else if (pdl->get_layer_ref("main").check_main_field_type<f64_3>()) { | ||
|
|
||
| sched->set_coord_domain_bound<f64_3>(a, b); | ||
| } else { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.