VirtualFluids 0.2.0
Parallel CFD LBM Solver
Loading...
Searching...
No Matches
TbTransmitterMpiPool.h
Go to the documentation of this file.
1//=======================================================================================
2// ____ ____ __ ______ __________ __ __ __ __
3// \ \ | | | | | _ \ |___ ___| | | | | / \ | |
4// \ \ | | | | | |_) | | | | | | | / \ | |
5// \ \ | | | | | _ / | | | | | | / /\ \ | |
6// \ \ | | | | | | \ \ | | | \__/ | / ____ \ | |____
7// \ \ | | |__| |__| \__\ |__| \________/ /__/ \__\ |_______|
8// \ \ | | ________________________________________________________________
9// \ \ | | | ______________________________________________________________|
10// \ \| | | | __ __ __ __ ______ _______
11// \ | | |_____ | | | | | | | | | _ \ / _____)
12// \ | | _____| | | | | | | | | | | \ \ \_______
13// \ | | | | |_____ | \_/ | | | | |_/ / _____ |
14// \ _____| |__| |________| \_______/ |__| |______/ (_______/
15//
16// This file is part of VirtualFluids. VirtualFluids is free software: you can
17// redistribute it and/or modify it under the terms of the GNU General Public
18// License as published by the Free Software Foundation, either version 3 of
19// the License, or (at your option) any later version.
20//
21// VirtualFluids is distributed in the hope that it will be useful, but WITHOUT
22// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24// for more details.
25//
26// SPDX-License-Identifier: GPL-3.0-or-later
27// SPDX-FileCopyrightText: Copyright © VirtualFluids Project contributors, see AUTHORS.md in root folder
28//
33//=======================================================================================
34#ifndef PARALLEL_TBTRANSMITTERMPIPOOL_H
35#define PARALLEL_TBTRANSMITTERMPIPOOL_H
36
37#ifdef VF_MPI
38
39#include <iomanip>
40#include <iostream>
41#include <map>
42#include <sstream>
43#include <vector>
44
45#include <mpi.h>
46
50
52
55// TbCbVectorMpiPoolSender/Receiver
56// diese verschicken immer einen VectorPool. Letztlich einen langen vector,
57// der eigentlich aus vielen kleinen besteht
58// jeder MpiPoolVector hat einen pointer auf die startadresse in diesem vector
59// die informationen werden im TbMpiVectorPool verwaltet
60// MpiPoolVector verhaelt sich nach aussen hin mit einschraenkungen wie ein std::vector
61// und kann somit bei den vector connector verwendet werden
62// man kann die klassen theoretisch verallgemeinern.
63
64template <typename T>
66template <typename T>
68
69/*==================================================================*/
70template <typename T>
72{
73public:
75
77 using MpiPoolPtrMap = std::map<std::string, MpiPoolPtr>;
78 using MpiPoolPtrMapIter = typename MpiPoolPtrMap::iterator;
79
80 // da BasisKlasse templateKlasse ist MUSS man hier die typedefs nochmal wiederholen!
83 using Pool = std::vector<value_type>;
84
85 using CbVectorKey = std::string;
86 using CbVectorMap = std::map<CbVectorKey, CbVector<value_type> *>;
87 using CbVectorMapIter = typename CbVectorMap::iterator;
88
90 friend class TbCbVectorSenderMpiPool<T>;
91 friend class TbCbVectorReceiverMpiPool<T>;
92
93protected:
96
97public:
99 // STATIC MEMBERS
101 // createTbCbVectorMpiPool:
102 // poolKey : Schluessel fuer eindeutige Indizierung in Map
103 // mpiRemoteRank: mpi-rank des Empfaengers/Senders
104 // mpiTag : mpi-tag mit dem empfangen/gesendet wird
106 size_type startPoolSize = 20000) // startPoolSize*sizeof(T)/1024/1024 [MB]
107
108 {
109 if (poolMap.find(poolKey) != poolMap.end()) {
110 throw UbException(UB_EXARGS, "es ist bereits ein Pool mit dem key vorhanden!!!");
111 }
112
113 // pool erstellen
115
116 // pool "speichern"
118
119 return mpiPool;
120 }
122 {
124 if (it == poolMap.end()) {
125 throw UbException(UB_EXARGS, "kein Pool mit dem key vorhanden");
126 }
128 }
129 /*==================================================================*/
131 {
134 return it->second;
135 }
136 return MpiPoolPtr();
137 }
138 /*==================================================================*/
139 static std::string getInfoString()
140 {
141 std::stringstream out;
142 out << "TbCbVectorMpiPool<" << typeid(T).name() << ") - Info:" << std::endl;
143 for (MpiPoolPtrMapIter it = poolMap.begin(); it != poolMap.end(); ++it)
144 out << "pool with key(" << std::setw(15) << it->first << ") "
145 << "stores " << std::setw(12) << it->second->getNofStoredVectors() << " vectors "
146 << ", elements to transfer = " << std::setw(15) << it->second->getPoolSize() << " ( "
147 << it->second->getPoolSize() * sizeof(T) / (1024.0 * 1024.0) << " MB )" << std::endl;
148 return out.str();
149 }
150 /*==================================================================*/
151 // checks if all vectors have one to one pool-entries
152 static bool consistencyCheck()
153 {
154 for (MpiPoolPtrMapIter it = poolMap.begin(); it != poolMap.end(); ++it) {
155 if (!it->second->CbVectorPool<T>::consistencyCheck()) {
156 return false;
157 }
158 }
159
160 return true;
161 }
163 static void eraseMap() { poolMap.clear(); }
164
165protected:
169 nofStoredVectors(0) //=Anzahl an Vectoren im Pool, wird bei send/receiveDataOrder gesetzt
170 ,
174 //, sendRequest(MPI_REQUEST_NULL)
175 ,
177 {
178 if ((std::string) typeid(value_type).name() == (std::string) typeid(double).name())
180 else if ((std::string) typeid(value_type).name() == (std::string) typeid(float).name())
182 else if ((std::string) typeid(value_type).name() == (std::string) typeid(int).name())
184 else
185 throw UbException(UB_EXARGS, "no MpiDataType for T" + (std::string) typeid(T).name());
186
187 for (int i = 0; i < 3; i++) {
189 }
190 }
191
192public:
193 /*==================================================================*/
194 // returns key of Pool in MpiPoolMap
195 CbVectorKey getPoolKey() const { return this->poolKey; }
196 /*==================================================================*/
197 // returns rank of process pool data will be send to/received from
198 int getRemoteRank() const { return this->mpiRemoteRank; }
199 /*==================================================================*/
200 // returns tag of process pool data will be send to/received from
201 int getRemoteTag() const { return this->mpiTag; }
202
203protected:
204 /*==================================================================*/
206 {
208 if (counterSendDataOrder == this->cbVectorMap.size()) {
209 // allg.: bei MPI muss man darauf achten, dass bei unblocked operationen die puffer (aus dem oder in den
210 // geschrieben wird auch noch vorhanden sind!!! wuerde man hier z.B. einen lokalen vector mit Isend() los-
211 // schicken, dann wurde der scope verlassen werden und der vector evtl geloescht werden, bevor mpi den
212 // vorgang abgeschlossen hat!!! -> tmpOrderVec ist class-member!!!
213 unsigned nofElements = (unsigned)this->cbVectorMap.size() * 3 + 1;
214 tmpSendOrderVec.resize(nofElements); // std::vector< unsigned > vec(nofElements);
215 unsigned index = 0;
216 tmpSendOrderVec[index++] = (unsigned)this->pool.size(); //= laenge des vectors
217 if (this->nextCbVectorStartIndexInPool != this->pool.size())
219 "an dieser Stelle sollten nextStartIndex und pool.size() identisch sein!!!");
220 for (CbVectorMapIter it = this->cbVectorMap.begin(); it != this->cbVectorMap.end(); ++it) {
222 size_type dataSize = 0, startIndexInPool = 0;
223 this->getCbVectorData(*it->second /*vec*/, vectorKey, startIndexInPool, dataSize);
224 if (it->first != vectorKey)
225 throw UbException(UB_EXARGS, "key mismatch!");
226
227 tmpSendOrderKeyVec += vectorKey; // vectorKey == allocator.getAllocatorKey()
228 tmpSendOrderVec[index++] = (unsigned)vectorKey.length();
229 tmpSendOrderVec[index++] = (unsigned)startIndexInPool; // startIndex in poolVector
230 tmpSendOrderVec[index++] = (unsigned)dataSize; // dataSize
231 }
232
234 &sendRequest[0]);
235
239 comm, &sendRequest[2]);
240
242
243 nofStoredVectors = this->cbVectorMap.size();
244
245 UBLOG(logDEBUG5, "TbCbVectorMpiPool::sendDataOrder()"
246 << " mpiRemoteRank=" << mpiRemoteRank << " mpiTag=" << mpiTag);
247
248#ifdef VF_DEBUG
250#endif
251 }
252 }
253 /*==================================================================*/
255 {
257 if (counterReceiveDataOrder == this->cbVectorMap.size()) {
258 // receiveRequest.Wait();
259 unsigned nofElements =
260 (unsigned)this->cbVectorMap.size() * 3 +
261 1; // map MUSS auf beiden seiten gleich gross sein, sonst hat man ein grundsaetzliches problem ;-)
262
263 UBLOG(logDEBUG5, "TbCbVectorMpiPool::receiveDataOrder()"
264 << " mpiRemoteRank=" << mpiRemoteRank << " mpiTag=" << mpiTag);
265
266 std::vector<unsigned> tmpRecvOrderVec;
268
269 std::vector<char> tmpRecvOrderKeyVec;
270
271 // MPI_Status status;
273
274 unsigned rcount;
278
279 if (nofElements != (unsigned)tmpRecvOrderVec.size())
280 throw UbException(UB_EXARGS, "error... vec size stimmt nicht");
281
282 unsigned index = 0;
283 size_type index2 = 0;
284 this->nextCbVectorStartIndexInPool = tmpRecvOrderVec[index++]; //= laenge des vectors
285 this->pool.resize(this->nextCbVectorStartIndexInPool);
286 CbVectorMapIter it = this->cbVectorMap.begin();
287 for (/*index*/; index < nofElements; index += 3, ++it) {
289 size_type startIndexInPool = (size_type)tmpRecvOrderVec.at(index + 1);
290 size_type dataSize = (size_type)tmpRecvOrderVec.at(index + 2);
293
294 // if(it==this->cbVectorMap.end() || it->first != vectorKey )
295 // throw UbException(UB_EXARGS, "entweder hat map nicht die gleiche reihenfolge oder vectorKey =
296 // "+UbSystem::toString(vectorKey)+" nicht vorhanden");
297 if (it == this->cbVectorMap.end())
298 throw UbException(UB_EXARGS, "map ist leer");
299 else if (it->first != vectorKey)
300 throw UbException(UB_EXARGS, "vectorKey = " + ub_system::toString(vectorKey) +
301 " nicht vorhanden it->first =" + ub_system::toString(it->first));
302
303 this->setCbVectorData(*it->second /*vec*/, vectorKey, startIndexInPool, dataSize);
304 }
305 if (it != this->cbVectorMap.end())
306 throw UbException(UB_EXARGS, "error... in der map sind scheinbar noch weiter elemente vorhanden, die "
307 "es auf der send seite nicht gibt...");
308
310 nofStoredVectors = this->cbVectorMap.size();
311
312#ifdef VF_DEBUG
314#endif
315 }
316 }
317 /*==================================================================*/
319 {
320 // da sendDataOrder einen request verwendet muss man hier immer abfragen
321 if (counterPrepareForSend == 0) {
322 UBLOG(logDEBUG5, "TbCbVectorMpiPool::prepareForSendData():start"
323 << " mpiRemoteRank=" << mpiRemoteRank << " mpiTag=" << mpiTag);
324 // if(sendRequest != MPI_REQUEST_NULL) MPI_Wait(&sendRequest, MPI_STATUS_IGNORE);
327 UBLOG(logDEBUG5, "TbCbVectorMpiPool::prepareForSendData():end"
328 << " mpiRemoteRank=" << mpiRemoteRank << " mpiTag=" << mpiTag);
329 }
330
332
335 }
336
337 // A - non blocking
342 // counterPrepareForSend++;
343 // if(counterPrepareForSend==1)
344 //{
345 // if(sendRequest != MPI::REQUEST_NULL) sendRequest.Wait();
346 //}
347 //
348 // if(counterPrepareForSend==nofStoredVectors)
349 // counterPrepareForSend=0;
350 }
351 /*==================================================================*/
352 void sendData()
353 {
354 // A - non blocking
355 // der LETZTE is entscheidend
356 // counterSend++;
357 // if(counterSend==nofStoredVectors)
358 //{
359 // //std::cout<<"Isend von "<<(int)nextStartIndex<<"elementen von "<<mpiRemoteRank<<" mit
360 // tag="<<mpiTag<<std::endl; sendRequest = comm.Isend(&pool[0],(int)nextCbVectorStartIndexInPool, mpiDataType,
361 // mpiRemoteRank, mpiTag); counterSend=0;
362 //}
363 // B - blocking
364 // der LETZTE is entscheidend
365 counterSend++;
367 // std::cout<<"Isend von "<<(int)nextStartIndex<<"elementen von "<<mpiRemoteRank<<" mit
368 // tag="<<mpiTag<<std::endl;
369 UBLOG(logDEBUG5, "TbCbVectorMpiPool::sendData():start"
370 << " mpiRemoteRank=" << mpiRemoteRank << " mpiTag=" << mpiTag);
371
372 // synchronous send
373 // comm.Ssend(&this->pool[0],(int)this->nextCbVectorStartIndexInPool, mpiDataType, mpiRemoteRank, mpiTag);
374#ifdef VF_DEBUG
375 if (this->orgPoolVectorStartPointer != &this->pool[0])
376 throw UbException(UB_EXARGS, "ups, pool array adress changed - unknown behavoir");
377#endif
378
379 // standard send
381 UBLOG(logDEBUG5, "TbCbVectorMpiPool::sendData():end"
382 << " mpiRemoteRank=" << mpiRemoteRank << " mpiTag=" << mpiTag);
384 // DEBUG///////////////////////////////////////
385 // int irank;
386 // MPI_Comm_rank(MPI_COMM_WORLD, &irank);
387 // std::cout << "MPI_Send: " << irank << " " << mpiRemoteRank << " " <<mpiTag<<std::endl;
389 counterSend = 0;
390 }
391 }
392 /*==================================================================*/
394 {
395 // A - non blocking
396 // sobald der Letzte kann man den den request holen.
397 // andernfalls kann nicht gewaehrleistet werden, dass evtl noch mit dem buffer gearbeitet wird!!!
400 UBLOG(logDEBUG5, "TbCbVectorMpiPool::prepareForReceiveData():start"
401 << " mpiRemoteRank=" << mpiRemoteRank << " mpiTag=" << mpiTag);
402#ifdef VF_DEBUG
403 if (this->orgPoolVectorStartPointer != &this->pool[0])
404 throw UbException(UB_EXARGS, "ups, pool array adress changed - unknown behavoir");
405#endif
408 UBLOG(logDEBUG5, "TbCbVectorMpiPool::prepareForReceiveData():end"
409 << " mpiRemoteRank=" << mpiRemoteRank << " mpiTag=" << mpiTag);
411 }
412 }
413 /*==================================================================*/
415 {
416 // A - non blocking
417 // sobald der ERSTE reinkommt muss man warten, bis received wurde!!!
418 // denn erst anschliessend stehen die empfangenen daten zur verfuegung
419 if (counterReceive == 0) {
420 UBLOG(logDEBUG5, "TbCbVectorMpiPool::receiveData():start"
421 << " mpiRemoteRank=" << mpiRemoteRank << " mpiTag=" << mpiTag);
423 UBLOG(logDEBUG5, "TbCbVectorMpiPool::receiveData():end"
424 << " mpiRemoteRank=" << mpiRemoteRank << " mpiTag=" << mpiTag);
425 }
427 if (counterReceive == this->nofStoredVectors) // alle receiver waren hier
428 {
429 counterReceive = 0;
430 }
431
435 // if(counterReceive==0)
436 //{
437 // comm.Recv(&this->pool[0],(int)this->nextCbVectorStartIndexInPool, mpiDataType, mpiRemoteRank, mpiTag);
438 //}
439 // counterReceive++;
440 // if(counterReceive==this->nofStoredVectors) //alle receiver waren hier
441 // counterReceive=0;
442 }
443
444protected:
445 CbVectorKey poolKey; // eindeutiger schluessel fuer pool
447
455
456 std::vector<unsigned> tmpSendOrderVec; // wird zur temp speicherung der anordnung benoetigt
459
462 // MPI_Request sendRequest;
464 // MPI_Status sendStatus;
465 // MPI_Status receiveStatus;
467
469
470#ifdef VF_DEBUG
472#endif
473};
474
475template <typename T>
477
479// TbSenderMpiPool
481template <typename T>
482class TbCbVectorSenderMpiPool : public TbTransmitter<CbVector<T>>
483{
484public:
486
487public:
489 {
490 this->getData().setAllocator(new CbVectorAllocatorPool<T>(cbVectorKey, this->mpiVectorPool));
491 }
493 {
494 if (this->mpiVectorPool->getNofStoredVectors() == 1) // last entry!
495 {
497 }
498 }
499
500 bool isLocalTransmitter() const override { return false; }
501 bool isRemoteTransmitter() const override { return !this->isLocalTransmitter(); }
502
503 void sendDataSize() override { this->mpiVectorPool->sendDataOrder(); }
504 void receiveDataSize() override { throw UbException(UB_EXARGS, "TbMpiPoolSender sends only"); }
505 CbVector<T> &receiveData() override { throw UbException(UB_EXARGS, "TbMpiPoolSender sends only"); }
506 void prepareForSend() override { this->mpiVectorPool->prepareForSendData(); }
507 void sendData() override { this->mpiVectorPool->sendData(); }
508
509 // info-section (usable for remote transmitter)
510 int getSendTbRank() const { return this->mpiVectorPool->getRemoteRank(); }
511 int getSendTbTag() const { return this->mpiVectorPool->getRemoteTag(); }
512 int getRecvFromRank() const override { throw UbException(UB_EXARGS, "TbCbVectorSenderMpiPool sends only"); }
513 int getRecvFromTag() const override { throw UbException(UB_EXARGS, "TbCbVectorSenderMpiPool sends only"); }
514
515 std::string toString() const override
516 {
517 return "TbCbVectorSenderMpiPool<" + (std::string) typeid(T).name() + " to rank (tag)" +
519 }
520
521protected:
523};
524
525/*==================================================================*/
526template <typename T>
527class TbCbVectorReceiverMpiPool : public TbTransmitter<CbVector<T>>
528{
529public:
531
532public:
535 {
536 this->getData().setAllocator(new CbVectorAllocatorPool<T>(cbVectorKey, this->mpiVectorPool));
537 }
539 {
540 if (this->mpiVectorPool->getNofStoredVectors() == 1) // last entry!
541 {
543 }
544 }
545 bool isLocalTransmitter() const override { return false; }
546 bool isRemoteTransmitter() const override { return !this->isLocalTransmitter(); }
547
548 void sendDataSize() override { throw UbException(UB_EXARGS, "TbCbVectorReceiverMpiPool receives only"); }
549 void receiveDataSize() override { this->mpiVectorPool->receiveDataOrder(); }
550 void sendData() override { throw UbException(UB_EXARGS, "TbCbVectorReceiverMpiPool receives only"); }
551 void prepareForReceive() override { this->mpiVectorPool->prepareForReceiveData(); }
553 {
554 this->mpiVectorPool->receiveData();
555 return this->getData();
556 }
557
558 // info-section (usable for remote transmitter)
559 int getSendTbRank() const { throw UbException(UB_EXARGS, "TbCbVectorReceiverMpiPool receives only"); }
560 int getSendTbTag() const { throw UbException(UB_EXARGS, "TbCbVectorReceiverMpiPool receives only"); }
561 int getRecvFromRank() const override { return this->mpiVectorPool->getRemoteRank(); }
562 int getRecvFromTag() const override { return this->mpiVectorPool->getRemoteTag(); }
563
564 std::string toString() const override
565 {
566 return "TbCbVectorReceiverMpiPool<" + (std::string) typeid(T).name() + " to rank (tag)" +
568 }
569
570protected:
572};
573
574#endif // VF_MPI
575
576#endif // TBTRANSMITTERMPIPOOL_H
577
std::size_t size_type
Definition CbVector.h:71
T value_type
Definition CbVector.h:69
Pool::size_type nextCbVectorStartIndexInPool
void getCbVectorData(const CbVector< value_type > &vec, CbVectorKey &vectorKey, size_type &startIndexInPool, size_type &dataSize)
CbVectorMap cbVectorMap
void setCbVectorData(CbVector< value_type > &vec, const CbVectorKey &vectorKey, const size_type &startIndexInPool, const size_type &dataSize)
static bool consistencyCheck()
typename CbVector< T >::size_type size_type
typename MpiPoolPtrMap::iterator MpiPoolPtrMapIter
static MpiPoolPtr createTbCbVectorMpiPool(CbVectorKey poolKey, int mpiRemoteRank, int mpiTag, MPI_Comm comm, size_type startPoolSize=20000)
std::vector< unsigned > tmpSendOrderVec
SPtr< TbCbVectorMpiPool< T > > MpiPoolPtr
TbCbVectorMpiPool(CbVectorKey poolKey, int mpiRemoteRank, int mpiTag, MPI_Comm comm, size_type startPoolSize)
static void deleteTbCbVectorMpiPool(CbVectorKey poolKey)
size_type counterPrepareReceiveDataOrder
typename CbVectorMap::iterator CbVectorMapIter
std::map< CbVectorKey, CbVector< value_type > * > CbVectorMap
CbVectorKey getPoolKey() const
std::vector< value_type > Pool
static MpiPoolPtr getTbCbVectorMpiPool(CbVectorKey poolKey)
typename CbVector< T >::value_type value_type
static std::string getInfoString()
std::map< std::string, MpiPoolPtr > MpiPoolPtrMap
TbCbVectorReceiverMpiPool(std::string cbVectorKey, TbCbVectorMpiPool< T > *mpiVectorPool)
bool isRemoteTransmitter() const override
int getRecvFromRank() const override
int getRecvFromTag() const override
TbCbVectorMpiPool< T > * mpiVectorPool
bool isLocalTransmitter() const override
CbVector< T > & receiveData() override
std::string toString() const override
int getRecvFromRank() const override
std::string toString() const override
CbVector< T > & receiveData() override
TbCbVectorSenderMpiPool(std::string cbVectorKey, TbCbVectorMpiPool< T > *mpiVectorPool)
int getRecvFromTag() const override
bool isLocalTransmitter() const override
bool isRemoteTransmitter() const override
TbCbVectorMpiPool< T > * mpiVectorPool
std::shared_ptr< T > SPtr
static MpiPoolPtrMap poolMap
#define UBLOG(level, logtext)
Definition UbLogger.h:327
#define UB_EXARGS
Definition UbException.h:73
@ logDEBUG5
Definition UbLogger.h:54
std::string toString(const T &x, int precision=15)
Definition UbSystem.h:199