Skip to content
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

WIP: DataCollection/StateDescriptor niceties #923

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 57 additions & 14 deletions src/interface/data_collection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,37 +42,80 @@ DataCollection<T>::Add(const std::string &name, const std::shared_ptr<T> &src,
return containers_[name];
}
template <typename T>
std::shared_ptr<T> &DataCollection<T>::Add(const std::string &label,
const std::shared_ptr<T> &src,
const std::vector<std::string> &flags) {
return Add(label, src, flags, false);
std::shared_ptr<T> &DataCollection<T>::Add(const std::string &name,
const std::string src_name,
const std::vector<std::string> &field_names) {
return Add(name, containers_[src_name], field_names, false);
}
template <typename T>
std::shared_ptr<T> &DataCollection<T>::AddShallow(const std::string &label,
const std::shared_ptr<T> &src,
const std::vector<std::string> &flags) {
return Add(label, src, flags, true);
std::shared_ptr<T> &
DataCollection<T>::AddShallow(const std::string &name, const std::string src_name,
const std::vector<std::string> &field_names) {
return Add(name, containers_[src_name], field_names, true);
}
template <typename T>
std::shared_ptr<T> &
DataCollection<T>::AddShallow(const std::string &name, const std::shared_ptr<T> &src,
const std::vector<std::string> &field_names) {
return Add(name, src, field_names, true);
}
template <typename T>
std::shared_ptr<T> &DataCollection<T>::Add(const std::string &name,
const std::vector<std::string> &field_names) {
return Add(name, containers_["base"], field_names, false);
}
template <typename T>
std::shared_ptr<T> &
DataCollection<T>::AddShallow(const std::string &name,
const std::vector<std::string> &field_names) {
return Add(name, containers_["base"], field_names, true);
}

template <>
std::shared_ptr<MeshData<Real>> &
DataCollection<MeshData<Real>>::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<std::string, std::shared_ptr<MeshData<Real>>> &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);
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<MeshData<Real>>(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<MeshData<Real>> &
DataCollection<MeshData<Real>>::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<MeshData<Real>> &
DataCollection<MeshData<Real>>::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<MeshData<Real>>;
template class DataCollection<MeshBlockData<Real>>;

Expand Down
31 changes: 16 additions & 15 deletions src/interface/data_collection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,22 @@ class DataCollection {

void SetMeshPointer(Mesh *pmesh) { pmy_mesh_ = pmesh; }

std::shared_ptr<T> &Add(const std::string &label, const std::shared_ptr<T> &src,
const std::vector<std::string> &flags, const bool shallow);
std::shared_ptr<T> &Add(const std::string &label, const std::shared_ptr<T> &src,
const std::vector<std::string> &flags = {});
std::shared_ptr<T> &AddShallow(const std::string &label, const std::shared_ptr<T> &src,
const std::vector<std::string> &flags = {});
std::shared_ptr<T> &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<T>();
return containers_[label];
}
// Full versions: add a MeshData w/given name, fields from 'src' selected by field_names
std::shared_ptr<T> &Add(const std::string &name, const std::shared_ptr<T> &src,
const std::vector<std::string> &field_names = {},
const bool shallow = false);
std::shared_ptr<T> &AddShallow(const std::string &name, const std::shared_ptr<T> &src,
const std::vector<std::string> &field_names = {});
// Allow naming src rather than passing pointer
std::shared_ptr<T> &Add(const std::string &name, const std::string src_name,
const std::vector<std::string> &field_names = {});
std::shared_ptr<T> &AddShallow(const std::string &name, const std::string src_name,
const std::vector<std::string> &field_names = {});
// Allow omitting the source name entirely, defaulting to "base"
std::shared_ptr<T> &Add(const std::string &name,
const std::vector<std::string> &field_names = {});
std::shared_ptr<T> &AddShallow(const std::string &name,
const std::vector<std::string> &field_names = {});

auto &Stages() { return containers_; }
const auto &Stages() const { return containers_; }
Expand Down
33 changes: 33 additions & 0 deletions src/interface/state_descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> &req_names,
const Metadata::FlagCollection &flags,
const std::vector<int> &sparse_ids) {
std::vector<std::string> 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<std::string> &req_names,
const std::vector<int> &sparse_ids) {
return GetPackDimension(req_names, Metadata::FlagCollection(), sparse_ids);
}
int StateDescriptor::GetPackDimension(const Metadata::FlagCollection &flags,
const std::vector<int> &sparse_ids) {
return GetPackDimension({}, flags, sparse_ids);
}
int StateDescriptor::GetPackDimension(const std::vector<std::string> &req_names) {
return GetPackDimension(req_names, Metadata::FlagCollection(), {});
}
int StateDescriptor::GetPackDimension(const Metadata::FlagCollection &flags) {
return GetPackDimension({}, flags, {});
}

} // namespace parthenon
10 changes: 10 additions & 0 deletions src/interface/state_descriptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,16 @@ class StateDescriptor {
std::vector<std::string> GetVariableNames(const std::vector<std::string> &req_names);
std::vector<std::string> GetVariableNames(const Metadata::FlagCollection &flags);

int GetPackDimension(const std::vector<std::string> &req_names,
const Metadata::FlagCollection &flags,
const std::vector<int> &sparse_ids);
int GetPackDimension(const std::vector<std::string> &req_names,
const std::vector<int> &sparse_ids);
int GetPackDimension(const Metadata::FlagCollection &flags,
const std::vector<int> &sparse_ids);
int GetPackDimension(const std::vector<std::string> &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);
Expand Down
Loading