From 94984ece16ada0fff7cb6243225a29651a281574 Mon Sep 17 00:00:00 2001 From: Ben Prather Date: Wed, 16 Aug 2023 09:39:40 -0600 Subject: [PATCH 1/4] Add some convenience functions/overloads to StateDescriptor and DataCollection --- src/interface/data_collection.cpp | 47 +++++++++++++++++++++++++++++- src/interface/state_descriptor.cpp | 33 +++++++++++++++++++++ src/interface/state_descriptor.hpp | 10 +++++++ src/mesh/mesh.cpp | 12 ++++---- 4 files changed, 95 insertions(+), 7 deletions(-) diff --git a/src/interface/data_collection.cpp b/src/interface/data_collection.cpp index 53dd6d8cc968..e1173b68bee9 100644 --- a/src/interface/data_collection.cpp +++ b/src/interface/data_collection.cpp @@ -60,10 +60,55 @@ std::shared_ptr &DataCollection::AddShallow(const std::string &label, const std::vector &flags) { return Add(label, src, flags, true); } +template +std::shared_ptr & +DataCollection::Add(const std::string &name, const std::string src_name, + const std::vector &field_names, const bool shallow) { + return Add(name, containers_[src_name], field_names, false); +} +template +std::shared_ptr &DataCollection::Add(const std::string &name, + const std::string src_name, + const std::vector &field_names) { + return Add(name, containers_[src_name], field_names, false); +} +template +std::shared_ptr & +DataCollection::AddShallow(const std::string &name, const std::string src_name, + const std::vector &field_names) { + return Add(name, containers_[src_name], field_names, true); +} +template +std::shared_ptr &DataCollection::Add(const std::string &name, + const std::string src_name) { + return Add(name, containers_[src_name], {}, false); +} +template +std::shared_ptr &DataCollection::AddShallow(const std::string &name, + const std::string src_name) { + return Add(name, containers_[src_name], {}, true); +} +template +std::shared_ptr & +DataCollection::Add(const std::string &name, + const std::vector &field_names, const bool shallow) { + return Add(name, containers_["base"], field_names, false); +} +template +std::shared_ptr &DataCollection::Add(const std::string &name, + const std::vector &field_names) { + return Add(name, containers_["base"], field_names, false); +} +template +std::shared_ptr & +DataCollection::AddShallow(const std::string &name, + const std::vector &field_names) { + return Add(name, containers_["base"], field_names, true); +} template <> std::shared_ptr> & -DataCollection>::GetOrAdd(const std::string &mbd_label, +DataCollection>::GetOrAddByPartition(const std::string &mbd_label, const int &partition_id) { const std::string label = mbd_label + "_part-" + std::to_string(partition_id); auto it = containers_.find(label); diff --git a/src/interface/state_descriptor.cpp b/src/interface/state_descriptor.cpp index 3ab618a722d1..74d922b6ecbc 100644 --- a/src/interface/state_descriptor.cpp +++ b/src/interface/state_descriptor.cpp @@ -530,4 +530,37 @@ StateDescriptor::GetVariableNames(const Metadata::FlagCollection &flags) { return GetVariableNames({}, flags, {}); } +// Get the total length of this StateDescriptor's variables when packed +int StateDescriptor::GetPackDimension(const std::vector &req_names, + const Metadata::FlagCollection &flags, + const std::vector &sparse_ids) { + std::vector names = GetVariableNames(req_names, flags, sparse_ids); + int nvar = 0; + for (auto name : names) { + const auto &meta = metadataMap_[VarID(name)]; + int var_len = 0; + if (meta.Shape().size() < 1) { + var_len = 1; + } else { + var_len = meta.Shape()[0]; + } + nvar += var_len; + } + return nvar; +} +int StateDescriptor::GetPackDimension(const std::vector &req_names, + const std::vector &sparse_ids) { + return GetPackDimension(req_names, Metadata::FlagCollection(), sparse_ids); +} +int StateDescriptor::GetPackDimension(const Metadata::FlagCollection &flags, + const std::vector &sparse_ids) { + return GetPackDimension({}, flags, sparse_ids); +} +int StateDescriptor::GetPackDimension(const std::vector &req_names) { + return GetPackDimension(req_names, Metadata::FlagCollection(), {}); +} +int StateDescriptor::GetPackDimension(const Metadata::FlagCollection &flags) { + return GetPackDimension({}, flags, {}); +} + } // namespace parthenon diff --git a/src/interface/state_descriptor.hpp b/src/interface/state_descriptor.hpp index 06537a87d88a..a53a69245e6e 100644 --- a/src/interface/state_descriptor.hpp +++ b/src/interface/state_descriptor.hpp @@ -272,6 +272,16 @@ class StateDescriptor { std::vector GetVariableNames(const std::vector &req_names); std::vector GetVariableNames(const Metadata::FlagCollection &flags); + int GetPackDimension(const std::vector &req_names, + const Metadata::FlagCollection &flags, + const std::vector &sparse_ids); + int GetPackDimension(const std::vector &req_names, + const std::vector &sparse_ids); + int GetPackDimension(const Metadata::FlagCollection &flags, + const std::vector &sparse_ids); + int GetPackDimension(const std::vector &req_names); + int GetPackDimension(const Metadata::FlagCollection &flags); + std::size_t RefinementFuncID(const refinement::RefinementFunctions_t &funcs) const noexcept { return refinementFuncMaps_.funcs_to_ids.at(funcs); diff --git a/src/mesh/mesh.cpp b/src/mesh/mesh.cpp index 8eb59faf8a60..f87223546c2f 100644 --- a/src/mesh/mesh.cpp +++ b/src/mesh/mesh.cpp @@ -96,9 +96,9 @@ Mesh::Mesh(ParameterInput *pin, ApplicationInput *app_in, Packages_t &packages, // private members: num_mesh_threads_(pin->GetOrAddInteger("parthenon/mesh", "num_threads", 1)), tree(this), use_uniform_meshgen_fn_{true, true, true, true}, lb_flag_(true), - lb_automatic_(), - lb_manual_(), MeshGenerator_{nullptr, UniformMeshGeneratorX1, - UniformMeshGeneratorX2, UniformMeshGeneratorX3}, + lb_automatic_(), lb_manual_(), + MeshGenerator_{nullptr, UniformMeshGeneratorX1, UniformMeshGeneratorX2, + UniformMeshGeneratorX3}, MeshBndryFnctn{nullptr, nullptr, nullptr, nullptr, nullptr, nullptr} { std::stringstream msg; RegionSize block_size; @@ -541,9 +541,9 @@ Mesh::Mesh(ParameterInput *pin, ApplicationInput *app_in, RestartReader &rr, // private members: num_mesh_threads_(pin->GetOrAddInteger("parthenon/mesh", "num_threads", 1)), tree(this), use_uniform_meshgen_fn_{true, true, true, true}, lb_flag_(true), - lb_automatic_(), - lb_manual_(), MeshGenerator_{nullptr, UniformMeshGeneratorX1, - UniformMeshGeneratorX2, UniformMeshGeneratorX3}, + lb_automatic_(), lb_manual_(), + MeshGenerator_{nullptr, UniformMeshGeneratorX1, UniformMeshGeneratorX2, + UniformMeshGeneratorX3}, MeshBndryFnctn{nullptr, nullptr, nullptr, nullptr, nullptr, nullptr} { std::stringstream msg; RegionSize block_size; From e722ee0fa2fa58c7e00bad96b38d5ebfdb23e4b2 Mon Sep 17 00:00:00 2001 From: Ben Prather Date: Thu, 17 Aug 2023 18:14:58 -0600 Subject: [PATCH 2/4] Basic fixes. Try default args, maybe not great for tasking --- src/interface/data_collection.cpp | 36 +------------------------------ src/interface/data_collection.hpp | 31 +++++++++++++------------- 2 files changed, 17 insertions(+), 50 deletions(-) diff --git a/src/interface/data_collection.cpp b/src/interface/data_collection.cpp index e1173b68bee9..9fd940c348ae 100644 --- a/src/interface/data_collection.cpp +++ b/src/interface/data_collection.cpp @@ -49,24 +49,6 @@ DataCollection::Add(const std::string &name, const std::shared_ptr &src, return containers_[name]; } template -std::shared_ptr &DataCollection::Add(const std::string &label, - const std::shared_ptr &src, - const std::vector &flags) { - return Add(label, src, flags, false); -} -template -std::shared_ptr &DataCollection::AddShallow(const std::string &label, - const std::shared_ptr &src, - const std::vector &flags) { - return Add(label, src, flags, true); -} -template -std::shared_ptr & -DataCollection::Add(const std::string &name, const std::string src_name, - const std::vector &field_names, const bool shallow) { - return Add(name, containers_[src_name], field_names, false); -} -template std::shared_ptr &DataCollection::Add(const std::string &name, const std::string src_name, const std::vector &field_names) { @@ -79,22 +61,6 @@ DataCollection::AddShallow(const std::string &name, const std::string src_nam return Add(name, containers_[src_name], field_names, true); } template -std::shared_ptr &DataCollection::Add(const std::string &name, - const std::string src_name) { - return Add(name, containers_[src_name], {}, false); -} -template -std::shared_ptr &DataCollection::AddShallow(const std::string &name, - const std::string src_name) { - return Add(name, containers_[src_name], {}, true); -} -template -std::shared_ptr & -DataCollection::Add(const std::string &name, - const std::vector &field_names, const bool shallow) { - return Add(name, containers_["base"], field_names, false); -} -template std::shared_ptr &DataCollection::Add(const std::string &name, const std::vector &field_names) { return Add(name, containers_["base"], field_names, false); @@ -108,7 +74,7 @@ DataCollection::AddShallow(const std::string &name, template <> std::shared_ptr> & -DataCollection>::GetOrAddByPartition(const std::string &mbd_label, +DataCollection>::GetOrAdd(const std::string &mbd_label, const int &partition_id) { const std::string label = mbd_label + "_part-" + std::to_string(partition_id); auto it = containers_.find(label); diff --git a/src/interface/data_collection.hpp b/src/interface/data_collection.hpp index 264a272ecd1b..9fb165e6fff8 100644 --- a/src/interface/data_collection.hpp +++ b/src/interface/data_collection.hpp @@ -38,21 +38,22 @@ class DataCollection { void SetMeshPointer(Mesh *pmesh) { pmy_mesh_ = pmesh; } - std::shared_ptr &Add(const std::string &label, const std::shared_ptr &src, - const std::vector &flags, const bool shallow); - std::shared_ptr &Add(const std::string &label, const std::shared_ptr &src, - const std::vector &flags = {}); - std::shared_ptr &AddShallow(const std::string &label, const std::shared_ptr &src, - const std::vector &flags = {}); - std::shared_ptr &Add(const std::string &label) { - // error check for duplicate names - auto it = containers_.find(label); - if (it != containers_.end()) { - return it->second; - } - containers_[label] = std::make_shared(); - return containers_[label]; - } + // Full versions: add a MeshData w/given name, fields from 'src' selected by field_names + std::shared_ptr &Add(const std::string &name, const std::shared_ptr &src, + const std::vector &field_names = {}, + const bool shallow = false); + std::shared_ptr &AddShallow(const std::string &name, const std::shared_ptr &src, + const std::vector &field_names = {}); + // Allow naming src rather than passing pointer + std::shared_ptr &Add(const std::string &name, const std::string src_name, + const std::vector &field_names = {}); + std::shared_ptr &AddShallow(const std::string &name, const std::string src_name, + const std::vector &field_names = {}); + // Allow omitting the source name entirely, defaulting to "base" + std::shared_ptr &Add(const std::string &name, + const std::vector &field_names = {}); + std::shared_ptr &AddShallow(const std::string &name, + const std::vector &field_names = {}); auto &Stages() { return containers_; } const auto &Stages() const { return containers_; } From 3116c06a13e978fc4aa6ccdffd25512275f6d961 Mon Sep 17 00:00:00 2001 From: Ben Prather Date: Thu, 17 Aug 2023 18:18:10 -0600 Subject: [PATCH 3/4] Fix mis-merge from unsaved resolution --- src/mesh/mesh.cpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/mesh/mesh.cpp b/src/mesh/mesh.cpp index 2f30955c8bd3..a53c89f647a1 100644 --- a/src/mesh/mesh.cpp +++ b/src/mesh/mesh.cpp @@ -524,15 +524,9 @@ Mesh::Mesh(ParameterInput *pin, ApplicationInput *app_in, RestartReader &rr, // private members: num_mesh_threads_(pin->GetOrAddInteger("parthenon/mesh", "num_threads", 1)), tree(this), use_uniform_meshgen_fn_{true, true, true, true}, lb_flag_(true), -<<<<<<< HEAD - lb_automatic_(), lb_manual_(), - MeshGenerator_{nullptr, UniformMeshGeneratorX1, UniformMeshGeneratorX2, - UniformMeshGeneratorX3}, -======= lb_automatic_(), lb_manual_(), MeshGenerator_{nullptr, UniformMeshGenerator, UniformMeshGenerator, UniformMeshGenerator}, ->>>>>>> develop MeshBndryFnctn{nullptr, nullptr, nullptr, nullptr, nullptr, nullptr} { std::stringstream msg; RegionSize block_size; From 59adef6ba84af3c7af35fe58c7a7f12c134f34f2 Mon Sep 17 00:00:00 2001 From: Ben Prather Date: Tue, 7 Nov 2023 16:53:54 -0500 Subject: [PATCH 4/4] Add implementation for DataCollection::AddShallow on SharedPtr source --- src/interface/data_collection.cpp | 57 ++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 16 deletions(-) diff --git a/src/interface/data_collection.cpp b/src/interface/data_collection.cpp index 9fd940c348ae..9c9be6ced4df 100644 --- a/src/interface/data_collection.cpp +++ b/src/interface/data_collection.cpp @@ -34,18 +34,11 @@ DataCollection::Add(const std::string &name, const std::shared_ptr &src, return it->second; } - auto c = std::make_shared(); + auto c = std::make_shared(name); c->Initialize(src.get(), field_names, shallow); Set(name, c); - if constexpr (std::is_same>::value) { - for (int b = 0; b < pmy_mesh_->block_list.size(); b++) { - auto &mbd = pmy_mesh_->block_list[b]->meshblock_data; - mbd.Set(name, c->GetBlockData(b)); - } - } - return containers_[name]; } template @@ -61,6 +54,12 @@ DataCollection::AddShallow(const std::string &name, const std::string src_nam return Add(name, containers_[src_name], field_names, true); } template +std::shared_ptr & +DataCollection::AddShallow(const std::string &name, const std::shared_ptr &src, + const std::vector &field_names) { + return Add(name, src, field_names, true); +} +template std::shared_ptr &DataCollection::Add(const std::string &name, const std::vector &field_names) { return Add(name, containers_["base"], field_names, false); @@ -72,25 +71,51 @@ DataCollection::AddShallow(const std::string &name, return Add(name, containers_["base"], field_names, true); } -template <> std::shared_ptr> & -DataCollection>::GetOrAdd(const std::string &mbd_label, - const int &partition_id) { - const std::string label = mbd_label + "_part-" + std::to_string(partition_id); +GetOrAdd_impl(Mesh *pmy_mesh_, + std::map>> &containers_, + BlockList_t &block_list, const std::string &mbd_label, + const int &partition_id, const int gmg_level) { + std::string label = mbd_label + "_part-" + std::to_string(partition_id); + if (gmg_level >= 0) label = label + "_gmg-" + std::to_string(gmg_level); auto it = containers_.find(label); if (it == containers_.end()) { // TODO(someone) add caching of partitions to Mesh at some point const int pack_size = pmy_mesh_->DefaultPackSize(); - auto partitions = partition::ToSizeN(pmy_mesh_->block_list, pack_size); + auto partitions = partition::ToSizeN(block_list, pack_size); for (auto i = 0; i < partitions.size(); i++) { - const std::string md_label = mbd_label + "_part-" + std::to_string(i); - containers_[md_label] = std::make_shared>(); - containers_[md_label]->Set(partitions[i], mbd_label); + std::string md_label = mbd_label + "_part-" + std::to_string(i); + if (gmg_level >= 0) md_label = md_label + "_gmg-" + std::to_string(gmg_level); + containers_[md_label] = std::make_shared>(mbd_label); + containers_[md_label]->Set(partitions[i]); + if (gmg_level >= 0) { + int min_gmg_logical_level = pmy_mesh_->GetGMGMinLogicalLevel(); + containers_[md_label]->grid = GridIdentifier{GridType::two_level_composite, + gmg_level + min_gmg_logical_level}; + } else { + containers_[md_label]->grid = GridIdentifier{GridType::leaf, 0}; + } } } return containers_[label]; } +template <> +std::shared_ptr> & +DataCollection>::GetOrAdd(const std::string &mbd_label, + const int &partition_id) { + return GetOrAdd_impl(pmy_mesh_, containers_, pmy_mesh_->block_list, mbd_label, + partition_id, -1); +} + +template <> +std::shared_ptr> & +DataCollection>::GetOrAdd(int gmg_level, const std::string &mbd_label, + const int &partition_id) { + return GetOrAdd_impl(pmy_mesh_, containers_, pmy_mesh_->gmg_block_lists[gmg_level], + mbd_label, partition_id, gmg_level); +} + template class DataCollection>; template class DataCollection>;