version 3.11-dev
Loading...
Searching...
No Matches
nonblockingsparseexchange.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3//
4// SPDX-FileCopyrightText: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
18#ifndef DUMUX_PARALLEL_NONBLOCKING_SPARSE_EXCHANGE_HH
19#define DUMUX_PARALLEL_NONBLOCKING_SPARSE_EXCHANGE_HH
20
21#include <vector>
22#include <cstddef>
23#include <cassert>
24
25#if HAVE_MPI
26#include <mpi.h>
27#endif
28
29namespace Dumux::Detail {
30
46template<class Communication>
47std::vector<char> exchangeSparse(const Communication& comm,
48 std::vector<std::vector<char>>& sendBufs,
49 [[maybe_unused]] int tag = 7373)
50{
51 std::vector<char> recvBuf;
52
53#if HAVE_MPI
54 const int myRank = comm.rank();
55 const int numProc = comm.size();
56 if (numProc <= 1)
57 return recvBuf;
58
59 // Dune::Communication<MPI_Comm> exposes operator MPI_Comm() const
60 const MPI_Comm mpiComm = comm;
61
62 // 1. Post a nonblocking synchronous send for every non-empty message. An Issend
63 // completes locally only once the matching receive has been posted remotely,
64 // which is what lets the barrier below detect global completion.
65 std::vector<MPI_Request> sendRequests;
66 sendRequests.reserve(numProc);
67 for (int to = 0; to < numProc; ++to)
68 {
69 if (to == myRank || sendBufs[to].empty())
70 continue;
71 sendRequests.emplace_back();
72 MPI_Issend(sendBufs[to].data(), static_cast<int>(sendBufs[to].size()),
73 MPI_BYTE, to, tag, mpiComm, &sendRequests.back());
74 }
75
76 // Receive (via matched probe) every message that can currently be matched,
77 // appending its bytes to the receive buffer.
78 const auto receivePending = [&]
79 {
80 int flag = 0;
81 MPI_Message message;
82 MPI_Status status;
83 MPI_Improbe(MPI_ANY_SOURCE, tag, mpiComm, &flag, &message, &status);
84 while (flag)
85 {
86 int count = 0;
87 MPI_Get_count(&status, MPI_BYTE, &count);
88 const std::size_t offset = recvBuf.size();
89 recvBuf.resize(offset + static_cast<std::size_t>(count));
90 MPI_Mrecv(recvBuf.data() + offset, count, MPI_BYTE, &message, MPI_STATUS_IGNORE);
91 MPI_Improbe(MPI_ANY_SOURCE, tag, mpiComm, &flag, &message, &status);
92 }
93 };
94
95 // 2./3. Nonblocking consensus: keep draining incoming messages. Once all of our
96 // own sends have been matched we enter a nonblocking barrier; its completion
97 // means every rank's sends have been matched globally, so we are done.
98 MPI_Request barrierRequest = MPI_REQUEST_NULL;
99 bool barrierActive = false;
100 bool done = false;
101 while (!done)
102 {
103 receivePending();
104
105 if (barrierActive)
106 {
107 int barrierDone = 0;
108 MPI_Test(&barrierRequest, &barrierDone, MPI_STATUS_IGNORE);
109 done = (barrierDone != 0);
110 }
111 else
112 {
113 int sendsDone = 0;
114 MPI_Testall(static_cast<int>(sendRequests.size()),
115 sendRequests.data(), &sendsDone, MPI_STATUSES_IGNORE);
116 if (sendsDone)
117 {
118 MPI_Ibarrier(mpiComm, &barrierRequest);
119 barrierActive = true;
120 }
121 }
122 }
123
124 // A process that has seen the barrier complete may start a subsequent exchange with the same tag
125 // while others still probe for messages of this one and would receive its messages here.
126 MPI_Barrier(mpiComm);
127#else
128 assert(comm.size() == 1 && "Sparse data exchange between multiple ranks requires MPI");
129 (void)sendBufs;
130#endif
131
132 return recvBuf;
133}
134
135} // end namespace Dumux::Detail
136
137#endif
std::vector< char > exchangeSparse(const Communication &comm, std::vector< std::vector< char > > &sendBufs, int tag=7373)
Exchange variable-sized byte messages with a sparse set of peer processes.
Definition nonblockingsparseexchange.hh:47
Definition cvfelocalresidual.hh:25