preCICE v3.1.2
Loading...
Searching...
No Matches
M2N.cpp
Go to the documentation of this file.
1#include <utility>
2
5#include "M2N.hpp"
8#include "mesh/Mesh.hpp"
10#include "profiling/Event.hpp"
11#include "utils/IntraComm.hpp"
12#include "utils/assertion.hpp"
13
15
16namespace precice {
17extern bool syncMode;
18
19namespace m2n {
20
21M2N::M2N(com::PtrCommunication intraComm, DistributedComFactory::SharedPointer distrFactory, bool useOnlyPrimaryCom, bool useTwoLevelInit)
22 : _intraComm(std::move(intraComm)),
23 _distrFactory(std::move(distrFactory)),
24 _useOnlyPrimaryCom(useOnlyPrimaryCom),
25 _useTwoLevelInit(useTwoLevelInit)
26{
27}
28
30{
31 if (isConnected()) {
33 }
34}
35
37{
39}
40
42 const std::string &acceptorName,
43 const std::string &requesterName)
44{
45 PRECICE_TRACE(acceptorName, requesterName);
46
47 Event e("m2n.acceptPrimaryRankConnection." + requesterName, profiling::Fundamental, profiling::Synchronize);
48
50 PRECICE_DEBUG("Accept primary connection");
52 _intraComm->acceptConnection(acceptorName, requesterName, "PRIMARYCOM", utils::IntraComm::getRank());
53 _isPrimaryRankConnected = _intraComm->isConnected();
54 }
55
57}
58
60 const std::string &acceptorName,
61 const std::string &requesterName)
62{
63 PRECICE_TRACE(acceptorName, requesterName);
64
65 Event e("m2n.requestPrimaryRankConnection." + acceptorName, profiling::Fundamental, profiling::Synchronize);
66
69 PRECICE_DEBUG("Request primary connection");
70 _intraComm->requestConnection(acceptorName, requesterName, "PRIMARYCOM", 0, 1);
71 _isPrimaryRankConnected = _intraComm->isConnected();
72 }
73
75}
76
78 const std::string &acceptorName,
79 const std::string &requesterName)
80{
81 PRECICE_TRACE(acceptorName, requesterName);
83 Event e("m2n.acceptSecondaryRanksConnection", profiling::Synchronize);
84
86 for (const auto &pair : _distComs) {
87 PRECICE_DEBUG("Accept secondary connections");
88 pair.second->acceptConnection(acceptorName, requesterName);
89 _areSecondaryRanksConnected = _areSecondaryRanksConnected && pair.second->isConnected();
90 }
92}
93
95 const std::string &acceptorName,
96 const std::string &requesterName)
97{
98 PRECICE_TRACE(acceptorName, requesterName);
100 Event e("m2n.requestSecondaryRanksConnection", profiling::Synchronize);
101
103 for (const auto &pair : _distComs) {
104 PRECICE_DEBUG("Request secondary connections");
105 pair.second->requestConnection(acceptorName, requesterName);
106 _areSecondaryRanksConnected = _areSecondaryRanksConnected && pair.second->isConnected();
107 }
109}
110
111void M2N::prepareEstablishment(const std::string &acceptorName,
112 const std::string &requesterName)
113{
115 _intraComm->prepareEstablishment(acceptorName, requesterName);
116}
117
118void M2N::cleanupEstablishment(const std::string &acceptorName,
119 const std::string &requesterName)
120{
122 _intraComm->cleanupEstablishment(acceptorName, requesterName);
123}
124
126 const std::string &acceptorName,
127 const std::string &requesterName)
128{
129 PRECICE_TRACE(acceptorName, requesterName);
132 for (const auto &pair : _distComs) {
133 pair.second->acceptPreConnection(acceptorName, requesterName);
134 _areSecondaryRanksConnected = _areSecondaryRanksConnected && pair.second->isConnected();
135 }
137}
138
140 const std::string &acceptorName,
141 const std::string &requesterName)
142{
143 PRECICE_TRACE(acceptorName, requesterName);
146 for (const auto &pair : _distComs) {
147 pair.second->requestPreConnection(acceptorName, requesterName);
148 _areSecondaryRanksConnected = _areSecondaryRanksConnected && pair.second->isConnected();
149 }
151}
152
154{
156 for (const auto &pair : _distComs) {
157 pair.second->completeSecondaryRanksConnection();
158 }
159}
160
167
179
181{
183 if (_useOnlyPrimaryCom) {
184 return;
185 }
186
188 for (const auto &pair : _distComs) {
189 pair.second->closeConnection();
190 _areSecondaryRanksConnected |= pair.second->isConnected();
191 }
193}
194
200
202{
205 _distComs[mesh->getID()] = _distrFactory->newDistributedCommunication(mesh);
206}
207
209 precice::span<double const> itemsToSend,
210 int meshID,
211 int valueDimension)
212{
213 if (not _useOnlyPrimaryCom) {
215 PRECICE_ASSERT(_distComs.find(meshID) != _distComs.end());
216 PRECICE_ASSERT(_distComs[meshID].get() != nullptr);
217
219 bool ack = true;
220 _intraComm->send(ack, 0);
221 _intraComm->receive(ack, 0);
222 _intraComm->send(ack, 0);
223 }
224
225 Event e("m2n.sendData", profiling::Synchronize);
226
227 _distComs[meshID]->send(itemsToSend, valueDimension);
228 } else {
230 _intraComm->send(itemsToSend, 0);
231 }
232}
233
234void M2N::send(bool itemToSend)
235{
238 _intraComm->send(itemToSend, 0);
239 }
240}
241
242void M2N::send(double itemToSend)
243{
246 _intraComm->send(itemToSend, 0);
247 }
248}
249
251{
254 _intraComm->send(itemsToSend, 0);
255 }
256}
257
258void M2N::send(int itemToSend)
259{
262 _intraComm->send(itemToSend, 0);
263 }
264}
265
267{
268 MeshID meshID = mesh.getID();
270 "This method can only be used for parallel participants");
272 PRECICE_ASSERT(_distComs.find(meshID) != _distComs.end());
273 PRECICE_ASSERT(_distComs[meshID].get() != nullptr);
274 _distComs[meshID]->broadcastSendMesh();
275}
276
278 mesh::Mesh & mesh)
279{
281 "This method can only be used for parallel participants");
282 MeshID meshID = mesh.getID();
284 _distComs[meshID]->scatterAllCommunicationMap(localCommunicationMap);
285}
286
287void M2N::broadcastSend(int itemToSend, mesh::Mesh &mesh)
288{
290 "This method can only be used for parallel participants");
291 MeshID meshID = mesh.getID();
293 _distComs[meshID]->broadcastSend(itemToSend);
294}
295
297 int meshID,
298 int valueDimension)
299{
300 if (not _useOnlyPrimaryCom) {
302 PRECICE_ASSERT(_distComs.find(meshID) != _distComs.end());
303 PRECICE_ASSERT(_distComs[meshID].get() != nullptr);
304
305 if (precice::syncMode) {
307 bool ack;
308
309 _intraComm->receive(ack, 0);
310 _intraComm->send(ack, 0);
311 _intraComm->receive(ack, 0);
312 }
313 }
314
315 Event e("m2n.receiveData", profiling::Synchronize);
316
317 _distComs[meshID]->receive(itemsToReceive, valueDimension);
318 } else {
320 _intraComm->receive(itemsToReceive, 0);
321 }
322}
323
324void M2N::receive(bool &itemToReceive)
325{
328 _intraComm->receive(itemToReceive, 0);
329 }
330
331 utils::IntraComm::broadcast(itemToReceive);
332
333 PRECICE_DEBUG("receive(bool): {}", itemToReceive);
334}
335
336void M2N::receive(double &itemToReceive)
337{
340 _intraComm->receive(itemToReceive, 0);
341 }
342
343 utils::IntraComm::broadcast(itemToReceive);
344
345 PRECICE_DEBUG("receive(double): {}", itemToReceive);
346}
347
349{
352 _intraComm->receive(itemsToReceive, 0);
353 }
354
355 utils::IntraComm::broadcast(itemsToReceive);
356
357 PRECICE_DEBUG("receive(span<double>) .size() = {}", itemsToReceive.size());
358}
359
360void M2N::receive(int &itemToReceive)
361{
364 _intraComm->receive(itemToReceive, 0);
365 }
366
367 utils::IntraComm::broadcast(itemToReceive);
368}
369
371{
373 "This method can only be used for parallel participants");
374 MeshID meshID = mesh.getID();
376 _distComs[meshID]->broadcastReceiveAll(itemToReceive);
377}
378
380{
382 "This method can only be used for parallel participants");
383 MeshID meshID = mesh.getID();
385 PRECICE_ASSERT(_distComs.find(meshID) != _distComs.end());
386 PRECICE_ASSERT(_distComs[meshID].get() != nullptr);
387 _distComs[meshID]->broadcastReceiveAllMesh();
388}
389
391{
393 "This method can only be used for parallel participants");
394 MeshID meshID = mesh.getID();
396 _distComs[meshID]->gatherAllCommunicationMap(localCommunicationMap);
397}
398
399} // namespace m2n
400} // namespace precice
#define PRECICE_DEBUG(...)
Definition LogMacros.hpp:64
#define PRECICE_TRACE(...)
Definition LogMacros.hpp:95
#define PRECICE_ASSERT(...)
Definition assertion.hpp:87
void scatterAllCommunicationMap(std::map< int, std::vector< int > > &localCommunicationMap, mesh::Mesh &mesh)
Scatters a communication map over connected ranks on remote participant (concerning the given mesh)
Definition M2N.cpp:277
com::PtrCommunication _intraComm
Definition M2N.hpp:229
void broadcastReceiveAll(std::vector< int > &itemToReceive, mesh::Mesh &mesh)
Receives an int per connected rank on remote participant (concerning the given mesh) @para[out] itemT...
Definition M2N.cpp:370
bool _isPrimaryRankConnected
Definition M2N.hpp:233
void closeDistributedConnections()
Disconnects all connections of the DistributedCommunication.
Definition M2N.cpp:180
void createDistributedCommunication(const mesh::PtrMesh &mesh)
Creates a new distributes communication for that mesh, stores the pointer in _distComs.
Definition M2N.cpp:201
void closePrimaryRankConnection()
Disconnects the primary connection.
Definition M2N.cpp:168
std::map< int, DistributedCommunication::SharedPointer > _distComs
mesh::getID() -> Pointer to distributed communication
Definition M2N.hpp:227
void cleanupEstablishment(const std::string &acceptorName, const std::string &requesterName)
cleans-up to establish the connections
Definition M2N.cpp:118
void gatherAllCommunicationMap(std::map< int, std::vector< int > > &localCommunicationMap, mesh::Mesh &mesh)
Gathers a communication maps from connected ranks on remote participant (concerning the given mesh)
Definition M2N.cpp:390
void broadcastSendMesh(mesh::Mesh &mesh)
Broadcasts a mesh to connected ranks on remote participant (concerning the given mesh)
Definition M2N.cpp:266
void closeConnection()
Disconnects from communication space, i.e. participant.
Definition M2N.cpp:161
void requestPrimaryRankConnection(const std::string &acceptorName, const std::string &requesterName)
Connects to another participant, which has to call acceptConnection().
Definition M2N.cpp:59
com::PtrCommunication getPrimaryRankCommunication()
Get the basic communication between the 2 primary ranks.
Definition M2N.cpp:195
void acceptSecondaryRanksPreConnection(const std::string &acceptorName, const std::string &requesterName)
Definition M2N.cpp:125
bool _useOnlyPrimaryCom
between two serial participants, only use the primary com and no secondary com
Definition M2N.hpp:246
void acceptSecondaryRanksConnection(const std::string &acceptorName, const std::string &requesterName)
Connects to another participant, which has to call requestConnection().
Definition M2N.cpp:77
void requestSecondaryRanksConnection(const std::string &acceptorName, const std::string &requesterName)
Connects to another participant, which has to call acceptConnection().
Definition M2N.cpp:94
bool _areSecondaryRanksConnected
Definition M2N.hpp:235
void broadcastReceiveAllMesh(mesh::Mesh &mesh)
Receive mesh partitions per connected rank on remote participant (concerning the given mesh)
Definition M2N.cpp:379
void requestSecondaryRanksPreConnection(const std::string &acceptorName, const std::string &requesterName)
Definition M2N.cpp:139
DistributedComFactory::SharedPointer _distrFactory
Definition M2N.hpp:231
M2N(com::PtrCommunication intraComm, DistributedComFactory::SharedPointer distrFactory, bool useOnlyPrimaryCom=false, bool useTwoLevelInit=false)
Definition M2N.cpp:21
bool isConnected()
Returns true, if a connection to a remote participant has been setup.
Definition M2N.cpp:36
void broadcastSend(int itemToSend, mesh::Mesh &mesh)
Broadcasts an int to connected ranks on remote participant (concerning the given mesh)
Definition M2N.cpp:287
void completeSecondaryRanksConnection()
Definition M2N.cpp:153
void prepareEstablishment(const std::string &acceptorName, const std::string &requesterName)
prepares to establish the connections
Definition M2N.cpp:111
~M2N()
Destructor, empty.
Definition M2N.cpp:29
void send(precice::span< double const > itemsToSend, int meshID, int valueDimension)
Sends an array of double values from all ranks (different for each rank).
Definition M2N.cpp:208
void receive(precice::span< double > itemsToReceive, int meshID, int valueDimension)
Definition M2N.cpp:296
void acceptPrimaryRankConnection(const std::string &acceptorName, const std::string &requesterName)
Connects to another participant, which has to call requestConnection().
Definition M2N.cpp:41
Container and creator for meshes.
Definition Mesh.hpp:39
MeshID getID() const
Returns the base ID of the mesh.
Definition Mesh.cpp:223
A C++ 11 implementation of the non-owning C++20 std::span type.
Definition span.hpp:284
constexpr size_type size() const noexcept
Definition span.hpp:469
static Rank getRank()
Current rank.
Definition IntraComm.cpp:42
static void broadcast(bool &value)
static bool isParallel()
True if this process is running in parallel.
Definition IntraComm.cpp:62
static bool isSecondary()
True if this process is running a secondary rank.
Definition IntraComm.cpp:57
static constexpr FundamentalTag Fundamental
Convenience instance of the FundamentalTag.
Definition Event.hpp:19
static constexpr SynchronizeTag Synchronize
Convenience instance of the SynchronizeTag.
Definition Event.hpp:21
Main namespace of the precice library.
int MeshID
Definition Types.hpp:30
constexpr auto get(span< E, S > s) -> decltype(s[N])
Definition span.hpp:602
bool syncMode
Enabled further inter- and intra-solver synchronisation.
STL namespace.