Skip to content

Commit

Permalink
Merge branch 'develop' into bprather/pep1
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrete authored Oct 18, 2023
2 parents 5b14ea4 + 7395ed5 commit ae77025
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 68 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,20 @@
- [[PR 885]](https://github.com/parthenon-hpc-lab/parthenon/pull/885) Expose PackDescriptor and use uids in SparsePacks

### Fixed (not changing behavior/API/variables/...)
- [[PR 955]](https://github.com/parthenon-hpc-lab/parthenon/pull/955) Only permit rank0 to mkdir when -d flag specified
- [[PR 952]](https://github.com/parthenon-hpc-lab/parthenon/pull/954) Fix format string in sparse advection example
- [[PR 947]](https://github.com/parthenon-hpc-lab/parthenon/pull/947) Add missing ForceRemeshComm dependencies
- [[PR 928]](https://github.com/parthenon-hpc-lab/parthenon/pull/928) Fix boundary comms during refinement next to refined blocks
- [[PR 937]](https://github.com/parthenon-hpc-lab/parthenon/pull/937) Fix multiple line continuations
- [[PR 933]](https://github.com/parthenon-hpc-lab/parthenon/pull/933) Remove extraneous debug check
- [[PR 917]](https://github.com/parthenon-hpc-lab/parthenon/pull/917) Update Iterative Tasking Infrastructure
- [[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 938]](https://github.com/parthenon-hpc-lab/parthenon/pull/938) Restructure buffer packing/unpacking kernel hierarchical parallelism
- [[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 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

### Removed (removing behavior/API/varaibles/...)
Expand Down
4 changes: 2 additions & 2 deletions example/sparse_advection/parthenon_app_inputs.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// (C) (or copyright) 2021. Triad National Security, LLC. All rights reserved.
// (C) (or copyright) 2023. Triad National Security, LLC. All rights reserved.
//
// This program was produced under U.S. Government contract 89233218CNA000001 for Los
// Alamos National Laboratory (LANL), which is operated by Triad National Security, LLC
Expand Down Expand Up @@ -176,7 +176,7 @@ void PostStepDiagnosticsInLoop(Mesh *mesh, ParameterInput *pin, const SimTime &t
}
std::printf("\n");
Real mem_avg = static_cast<Real>(mem_tot) / static_cast<Real>(blocks_tot);
std::printf("\tMem used/block in bytes [min, max, avg] = [%ld, %ld, %.14e]\n",
std::printf("\tMem used/block in bytes [min, max, avg] = [%lu, %lu, %.14e]\n",
mem_min, mem_max, mem_avg);
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/bvals/bvals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <memory>
#include <string>
#include <unordered_set>
#include <vector>

#include "basic_types.hpp"
Expand Down Expand Up @@ -78,7 +79,9 @@ class BoundaryBase {
static int BufferID(int dim, bool multilevel);
static int FindBufferID(int ox1, int ox2, int ox3, int fi1, int fi2);

void SearchAndSetNeighbors(MeshBlockTree &tree, int *ranklist, int *nslist);
void
SearchAndSetNeighbors(MeshBlockTree &tree, int *ranklist, int *nslist,
const std::unordered_set<LogicalLocation> &newly_refined = {});

protected:
// 1D refined or unrefined=2
Expand All @@ -90,7 +93,8 @@ class BoundaryBase {
RegionSize block_size_;
ParArrayND<Real> sarea_[2];

void SetNeighborOwnership();
void
SetNeighborOwnership(const std::unordered_set<LogicalLocation> &newly_refined = {});

private:
// calculate 3x shared static data members when constructing only the 1st class instance
Expand Down
17 changes: 10 additions & 7 deletions src/bvals/bvals_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <string> // c_str()

#include "globals.hpp"
#include "mesh/logical_location.hpp"
#include "mesh/mesh.hpp"
#include "utils/buffer_utils.hpp"
#include "utils/error_checking.hpp"
Expand Down Expand Up @@ -300,8 +301,9 @@ int BoundaryBase::CreateBvalsMPITag(int lid, int bufid) {

// TODO(felker): break-up this long function

void BoundaryBase::SearchAndSetNeighbors(MeshBlockTree &tree, int *ranklist,
int *nslist) {
void BoundaryBase::SearchAndSetNeighbors(
MeshBlockTree &tree, int *ranklist, int *nslist,
const std::unordered_set<LogicalLocation> &newly_refined) {
Kokkos::Profiling::pushRegion("SearchAndSetNeighbors");
MeshBlockTree *neibt;
int myox1, myox2 = 0, myox3 = 0, myfx1, myfx2, myfx3;
Expand Down Expand Up @@ -368,7 +370,7 @@ void BoundaryBase::SearchAndSetNeighbors(MeshBlockTree &tree, int *ranklist,
}
}
if (block_size_.nx(X2DIR) == 1) {
SetNeighborOwnership();
SetNeighborOwnership(newly_refined);
Kokkos::Profiling::popRegion(); // SearchAndSetNeighbors
return;
}
Expand Down Expand Up @@ -503,7 +505,7 @@ void BoundaryBase::SearchAndSetNeighbors(MeshBlockTree &tree, int *ranklist,
}

if (block_size_.nx(X3DIR) == 1) {
SetNeighborOwnership();
SetNeighborOwnership(newly_refined);
Kokkos::Profiling::popRegion(); // SearchAndSetNeighbors
return;
}
Expand Down Expand Up @@ -626,11 +628,12 @@ void BoundaryBase::SearchAndSetNeighbors(MeshBlockTree &tree, int *ranklist,
}
}

SetNeighborOwnership();
SetNeighborOwnership(newly_refined);
Kokkos::Profiling::popRegion(); // SearchAndSetNeighbors
}

void BoundaryBase::SetNeighborOwnership() {
void BoundaryBase::SetNeighborOwnership(
const std::unordered_set<LogicalLocation> &newly_refined) {
// Set neighbor block ownership
std::set<LogicalLocation> allowed_neighbors;
allowed_neighbors.insert(loc); // Insert the location of this block
Expand All @@ -642,7 +645,7 @@ void BoundaryBase::SetNeighborOwnership() {
RootGridInfo rg_info = pmy_mesh_->GetRootGridInfo();
for (int n = 0; n < nneighbor; ++n) {
neighbor[n].ownership =
DetermineOwnership(neighbor[n].loc, allowed_neighbors, rg_info);
DetermineOwnership(neighbor[n].loc, allowed_neighbors, rg_info, newly_refined);
neighbor[n].ownership.initialized = true;
}
}
Expand Down
70 changes: 52 additions & 18 deletions src/bvals/comms/boundary_communication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ TaskStatus SendBoundBufs(std::shared_ptr<MeshData<Real>> &md) {
PARTHENON_DEBUG_REQUIRE(bnd_info.size() == nbound, "Need same size for boundary info");
auto &sending_nonzero_flags = cache.sending_non_zero_flags;
auto &sending_nonzero_flags_h = cache.sending_non_zero_flags_h;

Kokkos::parallel_for(
"SendBoundBufs",
Kokkos::TeamPolicy<>(parthenon::DevExecSpace(), nbound, Kokkos::AUTO),
Expand All @@ -98,13 +99,26 @@ TaskStatus SendBoundBufs(std::shared_ptr<MeshData<Real>> &md) {
int idx_offset = 0;
for (int iel = 0; iel < bnd_info(b).ntopological_elements; ++iel) {
auto &idxer = bnd_info(b).idxer[iel];
const int Ni = idxer.template EndIdx<5>() - idxer.template StartIdx<5>() + 1;
Kokkos::parallel_reduce(
Kokkos::TeamThreadRange<>(team_member, idxer.size()),
Kokkos::TeamThreadRange<>(team_member, idxer.size() / Ni),
[&](const int idx, bool &lnon_zero) {
const auto [t, u, v, k, j, i] = idxer(idx);
const Real &val = bnd_info(b).var(iel, t, u, v, k, j, i);
bnd_info(b).buf(idx + idx_offset) = val;
lnon_zero = lnon_zero || (std::abs(val) >= threshold);
const auto [t, u, v, k, j, i] = idxer(idx * Ni);
Real *var = &bnd_info(b).var(iel, t, u, v, k, j, i);
Real *buf = &bnd_info(b).buf(idx * Ni + idx_offset);

Kokkos::parallel_for(Kokkos::ThreadVectorRange<>(team_member, Ni),
[&](int m) { buf[m] = var[m]; });

bool mnon_zero = false;
Kokkos::parallel_reduce(
Kokkos::ThreadVectorRange<>(team_member, Ni),
[&](int m, bool &llnon_zero) {
llnon_zero = llnon_zero || (std::abs(buf[m]) >= threshold);
},
Kokkos::LOr<bool, parthenon::DevMemSpace>(mnon_zero));

lnon_zero = lnon_zero || mnon_zero;
},
Kokkos::LOr<bool, parthenon::DevMemSpace>(non_zero[iel]));
idx_offset += idxer.size();
Expand Down Expand Up @@ -143,7 +157,7 @@ template <BoundaryType bound_type>
TaskStatus StartReceiveBoundBufs(std::shared_ptr<MeshData<Real>> &md) {
Kokkos::Profiling::pushRegion("Task_StartReceiveBoundBufs");
Mesh *pmesh = md->GetMeshPointer();
auto &cache = md->GetBvarsCache().GetSubCache(BoundaryType::flxcor_send, false);
auto &cache = md->GetBvarsCache().GetSubCache(bound_type, false);
if (cache.buf_vec.size() == 0)
InitializeBufferCache<bound_type>(md, &(pmesh->boundary_comm_map), &cache, ReceiveKey,
false);
Expand Down Expand Up @@ -229,21 +243,41 @@ TaskStatus SetBounds(std::shared_ptr<MeshData<Real>> &md) {
int idx_offset = 0;
for (int iel = 0; iel < bnd_info(b).ntopological_elements; ++iel) {
auto &idxer = bnd_info(b).idxer[iel];
const int Ni = idxer.template EndIdx<5>() - idxer.template StartIdx<5>() + 1;
if (bnd_info(b).buf_allocated && bnd_info(b).allocated) {
Kokkos::parallel_for(Kokkos::TeamThreadRange<>(team_member, idxer.size()),
[&](const int idx) {
const auto [t, u, v, k, j, i] = idxer(idx);
if (idxer.IsActive(k, j, i))
bnd_info(b).var(iel, t, u, v, k, j, i) =
bnd_info(b).buf(idx + idx_offset);
});
Kokkos::parallel_for(
Kokkos::TeamThreadRange<>(team_member, idxer.size() / Ni),
[&](const int idx) {
const auto [t, u, v, k, j, i] = idxer(idx * Ni);
Real *var = &bnd_info(b).var(iel, t, u, v, k, j, i);
Real *buf = &bnd_info(b).buf(idx * Ni + idx_offset);
// Have to do this because of some weird issue about structure bindings
// being captured
const int kk = k;
const int jj = j;
const int ii = i;
Kokkos::parallel_for(Kokkos::ThreadVectorRange<>(team_member, Ni),
[&](int m) {
if (idxer.IsActive(kk, jj, ii + m))
var[m] = buf[m];
});
});
} else if (bnd_info(b).allocated) {
const Real default_val = bnd_info(b).var.sparse_default_val;
Kokkos::parallel_for(Kokkos::TeamThreadRange<>(team_member, idxer.size()),
[&](const int idx) {
const auto [t, u, v, k, j, i] = idxer(idx);
bnd_info(b).var(iel, t, u, v, k, j, i) = default_val;
});
Kokkos::parallel_for(
Kokkos::TeamThreadRange<>(team_member, idxer.size() / Ni),
[&](const int idx) {
const auto [t, u, v, k, j, i] = idxer(idx * Ni);
Real *var = &bnd_info(b).var(iel, t, u, v, k, j, i);
const int kk = k;
const int jj = j;
const int ii = i;
Kokkos::parallel_for(Kokkos::ThreadVectorRange<>(team_member, Ni),
[&](int m) {
if (idxer.IsActive(kk, jj, ii + m))
var[m] = default_val;
});
});
}
idx_offset += idxer.size();
}
Expand Down
11 changes: 7 additions & 4 deletions src/interface/variable.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//========================================================================================
// (C) (or copyright) 2020-2022. Triad National Security, LLC. All rights reserved.
// (C) (or copyright) 2020-2023. Triad National Security, LLC. All rights reserved.
//
// This program was produced under U.S. Government contract 89233218CNA000001 for Los
// Alamos National Laboratory (LANL), which is operated by Triad National Security, LLC
Expand Down Expand Up @@ -81,7 +81,8 @@ void Variable<T>::CopyFluxesAndBdryVar(const Variable<T> *src) {
}
}

if (IsSet(Metadata::FillGhost) || IsSet(Metadata::Independent)) {
if (IsSet(Metadata::FillGhost) || IsSet(Metadata::Independent) ||
IsSet(Metadata::ForceRemeshComm)) {
// no need to check mesh->multilevel, if false, we're just making a shallow copy of
// an empty ParArrayND
coarse_s = src->coarse_s;
Expand Down Expand Up @@ -172,7 +173,8 @@ void Variable<T>::AllocateFluxesAndCoarse(std::weak_ptr<MeshBlock> wpmb) {
}

// Create the boundary object
if (IsSet(Metadata::FillGhost) || IsSet(Metadata::Independent)) {
if (IsSet(Metadata::FillGhost) || IsSet(Metadata::Independent) ||
IsSet(Metadata::ForceRemeshComm)) {
if (wpmb.expired()) return;
std::shared_ptr<MeshBlock> pmb = wpmb.lock();

Expand Down Expand Up @@ -205,7 +207,8 @@ std::int64_t Variable<T>::Deallocate() {
}
}

if (IsSet(Metadata::FillGhost) || IsSet(Metadata::Independent)) {
if (IsSet(Metadata::FillGhost) || IsSet(Metadata::Independent) ||
IsSet(Metadata::ForceRemeshComm)) {
mem_size += coarse_s.size() * sizeof(T);
coarse_s.Reset();
}
Expand Down
44 changes: 35 additions & 9 deletions src/mesh/amr_loadbalance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -702,13 +702,18 @@ void Mesh::RedistributeAndRefineMeshBlocks(ParameterInput *pin, ApplicationInput
oldtonew[mb_idx] = ntot - 1;

current_level = 0;
std::unordered_set<LogicalLocation> newly_refined;
for (int n = 0; n < ntot; n++) {
// "on" = "old n" = "old gid" = "old global MeshBlock ID"
int on = newtoold[n];
if (newloc[n].level() > current_level) // set the current max level
current_level = newloc[n].level();
if (newloc[n].level() >= loclist[on].level()) { // same or refined
newcost[n] = costlist[on];
// Keep a list of all blocks refined for below
if (newloc[n].level() > loclist[on].level()) {
newly_refined.insert(newloc[n]);
}
} else {
double acost = 0.0;
for (int l = 0; l < nleaf; l++)
Expand Down Expand Up @@ -917,28 +922,49 @@ void Mesh::RedistributeAndRefineMeshBlocks(ParameterInput *pin, ApplicationInput
}
}
prolongation_cache.CopyToDevice();

refinement::ProlongateShared(resolved_packages.get(), prolongation_cache,
block_list[0]->cellbounds, block_list[0]->c_cellbounds);
refinement::ProlongateInternal(resolved_packages.get(), prolongation_cache,
block_list[0]->cellbounds, block_list[0]->c_cellbounds);

// update the lists
loclist = std::move(newloc);
ranklist = std::move(newrank);
costlist = std::move(newcost);

// A block newly refined and prolongated may have neighbors which were
// already refined to the new level.
// If so, the prolongated versions of shared elements will not reflect
// the true, finer versions present in the neighbor block.
// We must create any new fine buffers and fill them from these neighbors
// in order to maintain a consistent global state.
// Thus we rebuild and synchronize the mesh now, but using a unique
// neighbor precedence favoring the "old" fine blocks over "new" ones
for (auto &pmb : block_list) {
pmb->pbval->SearchAndSetNeighbors(tree, ranklist.data(), nslist.data(),
newly_refined);
}
// Make sure all old sends/receives are done before we reconfigure the mesh
#ifdef MPI_PARALLEL
if (send_reqs.size() != 0)
PARTHENON_MPI_CHECK(
MPI_Waitall(send_reqs.size(), send_reqs.data(), MPI_STATUSES_IGNORE));
#endif
Kokkos::Profiling::popRegion(); // AMR: Recv data and unpack
// Re-initialize the mesh with our temporary ownership/neighbor configurations.
// No buffers are different when we switch to the final precedence order.
Initialize(false, pin, app_in);

// update the lists
loclist = std::move(newloc);
ranklist = std::move(newrank);
costlist = std::move(newcost);
// Internal refinement relies on the fine shared values, which are only consistent after
// being updated with any previously fine versions
refinement::ProlongateInternal(resolved_packages.get(), prolongation_cache,
block_list[0]->cellbounds, block_list[0]->c_cellbounds);

// re-initialize the MeshBlocks
// Rebuild just the ownership model, this time weighting the "new" fine blocks just like
// any other blocks at their level.
for (auto &pmb : block_list) {
pmb->pbval->SearchAndSetNeighbors(tree, ranklist.data(), nslist.data());
}
Initialize(false, pin, app_in);

Kokkos::Profiling::popRegion(); // AMR: Recv data and unpack

ResetLoadBalanceVariables();

Expand Down
Loading

0 comments on commit ae77025

Please sign in to comment.