Skip to content

Commit

Permalink
Merge pull request #944 from parthenon-hpc-lab/jdolence/move_string_c…
Browse files Browse the repository at this point in the history
…reation

Move creation of SparsePackCache identifier string
  • Loading branch information
jdolence authored Sep 28, 2023
2 parents 39ad758 + 06868ac commit 4f153d4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 40 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
- [[PR 890]](https://github.com/parthenon-hpc-lab/parthenon/pull/890) Fix bugs in sparse communication and prolongation

### Infrastructure (changes irrelevant to downstream codes)
- [[PR 944]](https://github.com/parthenon-hpc-lab/parthenon/pull/944) Move sparse pack identifier creation to descriptor
- [[PR 904]](https://github.com/parthenon-hpc-lab/parthenon/pull/904) Move to prolongation/restriction in one for AMR and communicate non-cell centered fields
- [[PR 918]](https://github.com/parthenon-hpc-lab/parthenon/pull/918) Refactor RegionSize
- [[PR 901]](https://github.com/parthenon-hpc-lab/parthenon/pull/901) Implement shared element ownership model
Expand Down
55 changes: 21 additions & 34 deletions src/interface/sparse_pack_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,21 +276,28 @@ template SparsePackBase SparsePackBase::Build<MeshData<Real>>(MeshData<Real> *,
template <class T>
SparsePackBase &SparsePackCache::Get(T *pmd, const PackDescriptor &desc,
const std::vector<bool> &include_block) {
std::string ident = GetIdentifier(desc, include_block);
if (pack_map.count(ident) > 0) {
auto &pack = pack_map[ident].first;
if (pack_map.count(desc.identifier) > 0) {
auto &cache_tuple = pack_map[desc.identifier];
auto &pack = std::get<0>(cache_tuple);
auto alloc_status_in = SparsePackBase::GetAllocStatus(pmd, desc, include_block);
auto &alloc_status = pack_map[ident].second;
auto &alloc_status = std::get<1>(cache_tuple);
if (alloc_status.size() != alloc_status_in.size())
return BuildAndAdd(pmd, desc, ident, include_block);
return BuildAndAdd(pmd, desc, include_block);
for (int i = 0; i < alloc_status_in.size(); ++i) {
if (alloc_status[i] != alloc_status_in[i])
return BuildAndAdd(pmd, desc, ident, include_block);
return BuildAndAdd(pmd, desc, include_block);
}
auto &include_status = std::get<2>(cache_tuple);
if (include_status.size() != include_block.size())
return BuildAndAdd(pmd, desc, include_block);
for (int i = 0; i < include_block.size(); ++i) {
if (include_status[i] != include_block[i])
return BuildAndAdd(pmd, desc, include_block);
}
// Cached version is not stale, so just return a reference to it
return pack_map[ident].first;
return std::get<0>(cache_tuple);
}
return BuildAndAdd(pmd, desc, ident, include_block);
return BuildAndAdd(pmd, desc, include_block);
}
template SparsePackBase &SparsePackCache::Get<MeshData<Real>>(MeshData<Real> *,
const PackDescriptor &,
Expand All @@ -301,37 +308,17 @@ SparsePackCache::Get<MeshBlockData<Real>>(MeshBlockData<Real> *, const PackDescr

template <class T>
SparsePackBase &SparsePackCache::BuildAndAdd(T *pmd, const PackDescriptor &desc,
const std::string &ident,
const std::vector<bool> &include_block) {
if (pack_map.count(ident) > 0) pack_map.erase(ident);
pack_map[ident] = {SparsePackBase::Build(pmd, desc, include_block),
SparsePackBase::GetAllocStatus(pmd, desc, include_block)};
return pack_map[ident].first;
if (pack_map.count(desc.identifier) > 0) pack_map.erase(desc.identifier);
pack_map[desc.identifier] = {SparsePackBase::Build(pmd, desc, include_block),
SparsePackBase::GetAllocStatus(pmd, desc, include_block),
include_block};
return std::get<0>(pack_map[desc.identifier]);
}
template SparsePackBase &
SparsePackCache::BuildAndAdd<MeshData<Real>>(MeshData<Real> *, const PackDescriptor &,
const std::string &,
const std::vector<bool> &);
template SparsePackBase &SparsePackCache::BuildAndAdd<MeshBlockData<Real>>(
MeshBlockData<Real> *, const PackDescriptor &, const std::string &,
const std::vector<bool> &);

std::string SparsePackCache::GetIdentifier(const PackDescriptor &desc,
const std::vector<bool> &include_block) const {
std::string identifier("");
for (const auto &vgroup : desc.var_groups) {
for (const auto &[vid, uid] : vgroup) {
identifier += std::to_string(uid) + "_";
}
identifier += "|";
}
identifier += std::to_string(desc.with_fluxes);
identifier += std::to_string(desc.coarse);
identifier += std::to_string(desc.flat);
for (const auto b : include_block) {
identifier += std::to_string(b);
}
return identifier;
}
MeshBlockData<Real> *, const PackDescriptor &, const std::vector<bool> &);

} // namespace parthenon
26 changes: 20 additions & 6 deletions src/interface/sparse_pack_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class SparsePackBase {
friend class SparsePackCache;

using alloc_t = std::vector<int>;
using include_t = std::vector<bool>;
using pack_t = ParArray3D<ParArray3D<Real, VariableState>>;
using bounds_t = ParArray3D<int>;
using bounds_h_t = typename ParArray3D<int>::HostMirror;
Expand Down Expand Up @@ -114,12 +115,10 @@ class SparsePackCache {

template <class T>
SparsePackBase &BuildAndAdd(T *pmd, const impl::PackDescriptor &desc,
const std::string &ident,
const std::vector<bool> &include_block);

std::string GetIdentifier(const impl::PackDescriptor &desc,
const std::vector<bool> &include_block) const;
std::unordered_map<std::string, std::pair<SparsePackBase, SparsePackBase::alloc_t>>
std::unordered_map<std::string, std::tuple<SparsePackBase, SparsePackBase::alloc_t,
SparsePackBase::include_t>>
pack_map;

friend class SparsePackBase;
Expand All @@ -136,15 +135,16 @@ struct PackDescriptor {
// default constructor needed for certain use cases
PackDescriptor()
: nvar_groups(0), var_group_names({}), var_groups({}), with_fluxes(false),
coarse(false), flat(false) {}
coarse(false), flat(false), identifier("") {}

template <class GROUP_t, class SELECTOR_t>
PackDescriptor(StateDescriptor *psd, const std::vector<GROUP_t> &var_groups_in,
const SELECTOR_t &selector, const std::set<PDOpt> &options)
: nvar_groups(var_groups_in.size()), var_group_names(MakeGroupNames(var_groups_in)),
var_groups(BuildUids(var_groups_in.size(), psd, selector)),
with_fluxes(options.count(PDOpt::WithFluxes)),
coarse(options.count(PDOpt::Coarse)), flat(options.count(PDOpt::Flatten)) {
coarse(options.count(PDOpt::Coarse)), flat(options.count(PDOpt::Flatten)),
identifier(GetIdentifier()) {
PARTHENON_REQUIRE(!(with_fluxes && coarse),
"Probably shouldn't be making a coarse pack with fine fluxes.");
}
Expand All @@ -155,8 +155,22 @@ struct PackDescriptor {
const bool with_fluxes;
const bool coarse;
const bool flat;
const std::string identifier;

private:
std::string GetIdentifier() {
std::string ident("");
for (const auto &vgroup : var_groups) {
for (const auto &[vid, uid] : vgroup) {
ident += std::to_string(uid) + "_";
}
ident += "|";
}
ident += std::to_string(with_fluxes);
ident += std::to_string(coarse);
ident += std::to_string(flat);
return ident;
}
template <class FUNC_t>
std::vector<PackDescriptor::VariableGroup_t>
BuildUids(int nvgs, const StateDescriptor *const psd, const FUNC_t &selector) {
Expand Down

0 comments on commit 4f153d4

Please sign in to comment.