17#ifndef PECLET_CORE_HALO_GRID_HALO_TOPOLOGY_HPP
18#define PECLET_CORE_HALO_GRID_HALO_TOPOLOGY_HPP
39 void pack(
Index localIdx,
char* dst)
const { std::memcpy(dst, &
data[localIdx],
sizeof(T)); }
40 void unpack(
Index localIdx,
const char* src) { std::memcpy(&
data[localIdx], src,
sizeof(T)); }
44 std::memcpy(&v, src,
sizeof(T));
69 indexer_.init(dec.
origins()[rank], dec.
sizes()[rank], ghostWidth);
73 std::map<int, std::vector<Index>> recvIdxByRank;
74 std::map<int, std::vector<Index>> recvGlobByRank;
77 indexer_.forEachAll([&](
const IVec<Dim>& lmd) {
78 if (indexer_.isInner(lmd))
82 for (
int i = 0; i < Dim; ++i)
84 (lmd[i] < ghostWidth || lmd[i] >= ghostWidth + indexer_.sizeInner()[i]) ? 1 : 0;
88 if (depth >= 0 && depth < ghostWidth)
89 for (
int i = 0; i < Dim; ++i) {
90 const Index hi = ghostWidth + indexer_.sizeInner()[i];
92 lmd[i] < ghostWidth ? ghostWidth - lmd[i] : (lmd[i] >= hi ? lmd[i] - hi + 1 : 0);
98 for (
int i = 0; i < Dim; ++i) {
99 g[i] = lmd[i] + indexer_.originInclGhost()[i];
101 if (c < 0 || c >= gsize[i]) {
103 c =
wrap(c, gsize[i]);
114 Index ghostLocal = indexer_.localMdToLocal(lmd);
115 if (owner == rank_) {
117 selfDst_.push_back(ghostLocal);
118 selfSrc_.push_back(indexer_.globalToLocal(gw));
120 recvIdxByRank[owner].push_back(ghostLocal);
125 for (
auto& [r, idx] : recvIdxByRank) {
126 recvRanks_.push_back(r);
127 recvIdx_.push_back(std::move(idx));
128 recvGlob_.push_back(std::move(recvGlobByRank[r]));
134 auto packNext = [&](std::vector<char>& out) ->
int {
135 if (k >= recvRanks_.size())
137 int dest = recvRanks_[k];
138 const auto& g = recvGlob_[k];
140 out.resize(g.size() *
sizeof(
Index));
141 std::memcpy(out.data(), g.data(), out.size());
144 auto onRecv = [&](
int src, std::vector<char>& msg) {
145 std::size_t n = msg.size() /
sizeof(
Index);
146 const Index* g =
reinterpret_cast<const Index*
>(msg.data());
147 std::vector<Index> sidx(n);
148 for (std::size_t i = 0; i < n; ++i) {
149 sidx[i] = indexer_.globalToLocal(dec.
multiGlobal(g[i]));
151 sendRanks_.push_back(src);
152 sendIdx_.push_back(std::move(sidx));
154 nbx.
exchange(packNext, onRecv, 7301);
161 long cnt[2] = {0, 0};
162 for (
const auto& g : recvGlob_)
163 cnt[0] +=
static_cast<long>(g.size());
164 for (
const auto& sidx : sendIdx_)
165 cnt[1] +=
static_cast<long>(sidx.size());
166 long tot[2] = {0, 0};
168 if (tot[0] != tot[1])
169 throw std::runtime_error(
"peclet.core GridHaloTopology: asymmetric halo topology (" +
170 std::to_string(tot[0]) +
" cells requested, " +
171 std::to_string(tot[1]) +
172 " promised) -- the NBX round lost messages");
176 persistentReady_ =
false;
183 template <
typename Field>
184 void start(Field& field,
int tag = 0) {
185 applySelfCopy(field);
186 const std::size_t es = field.bytesPerElem();
190 recvBufs_.resize(recvRanks_.size());
191 for (std::size_t k = 0; k < recvRanks_.size(); ++k) {
192 recvBufs_[k].resize(recvIdx_[k].size() * es);
193 MPI_Irecv(recvBufs_[k].data(),
static_cast<int>(recvBufs_[k].size()),
MPI_BYTE, recvRanks_[k],
194 tag, comm_, &recvReqs_[k]);
198 sendBufs_.resize(sendRanks_.size());
199 for (std::size_t k = 0; k < sendRanks_.size(); ++k) {
200 auto& buf = sendBufs_[k];
201 buf.resize(sendIdx_[k].size() * es);
202 for (std::size_t i = 0; i < sendIdx_[k].size(); ++i) {
203 field.pack(sendIdx_[k][i], buf.data() + i * es);
205 MPI_Isend(buf.data(),
static_cast<int>(buf.size()),
MPI_BYTE, sendRanks_[k], tag, comm_,
212 template <
typename Field>
214 const std::size_t es = field.bytesPerElem();
216 for (std::size_t done = 0; done < recvReqs_.size(); ++done) {
221 for (std::size_t i = 0; i < recvIdx_[k].size(); ++i) {
222 field.unpack(recvIdx_[k][i], recvBufs_[k].data() + i * es);
225 if (!sendReqs_.empty()) {
231 template <
typename Field>
242 template <
typename Field>
244 applySelfCopy(field);
245 const std::size_t es = field.bytesPerElem();
249 for (std::size_t k = 0; k < sendRanks_.size(); ++k) {
250 char* dst = graphSendBuf_.data() + graphSendDispl_[k];
251 for (std::size_t i = 0; i < sendIdx_[k].size(); ++i) {
252 field.pack(sendIdx_[k][i], dst + i * es);
256 MPI_BYTE, graphRecvBuf_.data(), graphRecvCnt_.data(),
257 graphRecvDispl_.data(),
MPI_BYTE, graphComm_);
259 for (std::size_t k = 0; k < recvRanks_.size(); ++k) {
260 const char* src = graphRecvBuf_.data() + graphRecvDispl_[k];
261 for (std::size_t i = 0; i < recvIdx_[k].size(); ++i) {
262 field.unpack(recvIdx_[k][i], src + i * es);
273 template <
typename Field>
275 const std::size_t es = field.bytesPerElem();
277 std::vector<char> tmp(es);
278 for (std::size_t i = 0; i < selfSrc_.size(); ++i) {
279 field.pack(selfDst_[i], tmp.data());
280 field.addFrom(selfSrc_[i], tmp.data());
283 std::vector<std::vector<char>> rbuf(sendRanks_.size());
285 for (std::size_t k = 0; k < sendRanks_.size(); ++k) {
286 rbuf[k].resize(sendIdx_[k].size() * es);
287 MPI_Irecv(rbuf[k].data(),
static_cast<int>(rbuf[k].size()),
MPI_BYTE, sendRanks_[k], tag,
291 std::vector<std::vector<char>> sbuf(recvRanks_.size());
293 for (std::size_t k = 0; k < recvRanks_.size(); ++k) {
294 sbuf[k].resize(recvIdx_[k].size() * es);
295 for (std::size_t i = 0; i < recvIdx_[k].size(); ++i)
296 field.pack(recvIdx_[k][i], sbuf[k].data() + i * es);
297 MPI_Isend(sbuf[k].data(),
static_cast<int>(sbuf[k].size()),
MPI_BYTE, recvRanks_[k], tag,
301 for (std::size_t done = 0; done < sendRanks_.size(); ++done) {
306 for (std::size_t i = 0; i < sendIdx_[k].size(); ++i)
307 field.addFrom(sendIdx_[k][i], rbuf[k].data() + i * es);
330 const auto rep = [&](
const std::vector<Index>& src,
const std::vector<int>& counts,
331 std::vector<Index>& dst, std::vector<int>* outCounts) {
333 for (std::size_t g = 0; g < counts.size(); ++g) {
334 for (
int j = 0; j < k; ++j)
335 for (
int i = 0; i < counts[g]; ++i)
336 dst.push_back(src[off +
static_cast<std::size_t
>(i)] +
337 static_cast<Index>(j) * stride);
338 off +=
static_cast<std::size_t
>(counts[g]);
340 outCounts->push_back(counts[g] * k);
345 const std::vector<int> selfCount(1,
static_cast<int>(
selfSrc.size()));
355 t.
sendRanks = std::vector<int>(sendRanks_.begin(), sendRanks_.end());
356 t.
recvRanks = std::vector<int>(recvRanks_.begin(), recvRanks_.end());
357 for (
const auto& v : sendIdx_) {
358 t.
sendCounts.push_back(
static_cast<int>(v.size()));
361 for (
const auto& v : recvIdx_) {
362 t.
recvCounts.push_back(
static_cast<int>(v.size()));
375 for (
auto& v : recvIdx_)
388 void freeGraphComm() {
407 persistentReady_ =
false;
410 void buildRecvLookup() {
411 recvRankPos_.clear();
412 for (std::size_t k = 0; k < recvRanks_.size(); ++k)
413 recvRankPos_[recvRanks_[k]] = k;
416 template <
typename Field>
417 void applySelfCopy(Field& field) {
420 if (selfSrc_.empty())
422 std::vector<char> tmp(field.bytesPerElem());
423 for (std::size_t i = 0; i < selfSrc_.size(); ++i) {
424 field.pack(selfSrc_[i], tmp.data());
425 field.unpack(selfDst_[i], tmp.data());
429 void ensureGraphComm(std::size_t es) {
430 if (persistentReady_ && graphElemSize_ == es)
436 std::vector<int> sources(recvRanks_.begin(), recvRanks_.end());
437 std::vector<int> dests(sendRanks_.begin(), sendRanks_.end());
444 graphSendCnt_.resize(dests.size());
445 graphSendDispl_.resize(dests.size());
447 for (std::size_t k = 0; k < dests.size(); ++k) {
448 graphSendCnt_[k] =
static_cast<int>(sendIdx_[k].size() * es);
449 graphSendDispl_[k] = off;
450 off += graphSendCnt_[k];
452 graphSendBuf_.resize(off);
454 graphRecvCnt_.resize(sources.size());
455 graphRecvDispl_.resize(sources.size());
457 for (std::size_t k = 0; k < sources.size(); ++k) {
458 graphRecvCnt_[k] =
static_cast<int>(recvIdx_[k].size() * es);
459 graphRecvDispl_[k] = off;
460 off += graphRecvCnt_[k];
462 graphRecvBuf_.resize(off);
465 persistentReady_ =
true;
470 decomp::BlockIndexer<Dim> indexer_;
473 std::vector<int> sendRanks_;
474 std::vector<std::vector<Index>> sendIdx_;
475 std::vector<int> recvRanks_;
476 std::vector<std::vector<Index>> recvIdx_;
477 std::vector<std::vector<Index>> recvGlob_;
478 std::map<int, std::size_t> recvRankPos_;
479 std::vector<Index> selfSrc_, selfDst_;
482 std::vector<MPI_Request> sendReqs_, recvReqs_;
483 std::vector<std::vector<char>> sendBufs_, recvBufs_;
488 bool persistentReady_ =
false;
489 std::size_t graphElemSize_ = 0;
490 std::vector<int> graphSendCnt_, graphSendDispl_, graphRecvCnt_, graphRecvDispl_;
491 std::vector<char> graphSendBuf_, graphRecvBuf_;
Orthogonal recursive bisection of a Dim-dimensional global cell grid into numBlocks axis-aligned bloc...
const std::vector< IVec< Dim > > & sizes() const
Extent in cells of every block, indexed by block.
const std::vector< IVec< Dim > > & origins() const
Inclusive lower corner of every block, in global cell coordinates, indexed by block.
const IVec< Dim > & globalSize() const
Global grid size in cells per axis.
Index linearGlobal(const IVec< Dim > &g) const
Global multi-index -> global linear index (x-fastest: I = x + y*nx + z*nx*ny).
int ownerOf(const IVec< Dim > &g) const
Owning block index of a global cell coordinate. Caller must wrap into [0, globalSize) first.
IVec< Dim > multiGlobal(Index lin) const
Global linear index -> global multi-index (inverse of linearGlobal).
void start(Field &field, int tag=0)
Post all sends/recvs; returns immediately so the caller can compute the block interior.
void reverseAdd(Field &field, int tag=0)
void buildTopology(const decomp::BlockDecomposer< Dim > &dec, int rank, int ghostWidth, const std::array< bool, Dim > &periodic, MPI_Comm comm=MPI_COMM_WORLD, Neighborhood nb=Neighborhood::Full, int depth=-1)
Build the halo topology for rank's block of dec, with the given ghost width and per-axis periodicity.
const decomp::BlockIndexer< Dim > & indexer() const
void exchangeNbx(Field &field, int tag=0)
Convenience: full blocking exchange via the NBX-style engine (start + wait).
void wait(Field &field)
Complete a started exchange: wait for receives and scatter them into the ghost cells.
GridHaloTopology(const GridHaloTopology &)=delete
std::size_t numGhostRecv() const
FlatTopology flatten() const
GridHaloTopology & operator=(const GridHaloTopology &)=delete
std::size_t numNeighbors() const
void exchangePersistent(Field &field)
Full exchange via MPI_Neighbor_alltoallv on a cached distributed-graph communicator.
GridHaloTopology()=default
void exchange(PackNext &&packNext, OnRecv &&onRecv, int baseTag=0)
int MPI_Allreduce(const void *sbuf, void *rbuf, int count, MPI_Datatype dt, MPI_Op, MPI_Comm)
int MPI_Waitall(int, MPI_Request *, MPI_Status *)
int MPI_Comm_free(MPI_Comm *)
#define MPI_STATUS_IGNORE
#define MPI_STATUSES_IGNORE
int MPI_Waitany(int, MPI_Request *, int *idx, MPI_Status *)
int MPI_Isend(const void *, int, MPI_Datatype, int, int, MPI_Comm, MPI_Request *)
int MPI_Finalized(int *f)
int MPI_Irecv(void *, int, MPI_Datatype, int, int, MPI_Comm, MPI_Request *)
int MPI_Neighbor_alltoallv(const void *, const int *, const int *, MPI_Datatype, void *, const int *, const int *, MPI_Datatype, MPI_Comm)
int MPI_Dist_graph_create_adjacent(MPI_Comm c, int, const int *, const int *, int, const int *, const int *, MPI_Info, int, MPI_Comm *out)
Neighborhood
Which ghost cells a topology fills.
Index wrap(Index x, Index n)
Periodic wrap of a coordinate into [0, n): wrap(x, n) = (x % n + n) % n.
std::int64_t Index
Signed index type for grids and particles (supersedes block_decomposer's long int IndxT).
std::array< Index, Dim > IVec
Multi-dimensional integer index / coordinate (x-fastest order by convention).
A contiguous local array of T (one per cell of the extended block) viewed as a packable field.
void addFrom(Index localIdx, const char *src)
void pack(Index localIdx, char *dst) const
void unpack(Index localIdx, const char *src)
std::size_t bytesPerElem() const
std::vector< int > recvCounts
std::vector< int > sendRanks
FlatTopology stacked(int k, Index stride) const
The same pattern for k fields at once: copy j of every list is offset by j * stride (the fields lie a...
std::vector< int > sendCounts
std::vector< Index > recvIdx
std::vector< int > recvRanks
std::vector< Index > selfSrc
std::vector< Index > selfDst
std::vector< Index > sendIdx