25#ifndef PARTICLE_RELOCATION_H
26#define PARTICLE_RELOCATION_H
38namespace communication {
41template<
typename T,
typename PARTICLETYPE>
45 using namespace descriptors;
46 static_assert(PARTICLETYPE::template providesNested<SURFACE,SURFACE_ID>(),
"Field SURFACE_ID has to be provided");
48 auto particle = particleSystem.
get(iP);
50 std::size_t idxSurface = particle.template getField<SURFACE,SURFACE_ID>();
53 auto& vectorOfIndicators = particleSystem.template getAssociatedData<
54 std::vector<std::unique_ptr<SIndicatorBaseType>>>();
55 auto sIndicatorPtr = vectorOfIndicators.at( idxSurface ).
get();
57 particle.template setField<SURFACE,SINDICATOR>( sIndicatorPtr );
62template<
typename T,
typename PARTICLETYPE>
65 std::uint8_t rawBuffer[])
68 auto& particleContainer = particleSystem.
get();
69 particleContainer.push_back();
71 std::size_t idxParticleNew = particleSystem.
size()-1;
72 auto particleNew = particleSystem.
get(idxParticleNew);
80 return particleNew.getId();
84template<
typename T,
typename PARTICLETYPE>
87 std::size_t idxParticle,
88 std::uint8_t rawBuffer[])
90 using namespace descriptors;
92 auto particle = particleSystem.
get(idxParticle);
94#ifdef VERBOSE_COMMUNICATION
95 auto globiCcentreLast = particle.template getField<PARALLELIZATION,IC>();
102#ifdef VERBOSE_COMMUNICATION
103 auto globiCcentre = particle.template getField<PARALLELIZATION,IC>();
105 if(globiCcentre!=globiCcentreLast){
107 std::cout << std::endl <<
"[Particle] "
109 << globiCcentreLast <<
"->" << globiCcentre <<
")"
110 <<
" at receiving at iC=" <<
"X"
111 <<
"(rank=" << rank <<
")" << std::endl;
120 return particle.getId();
127template<
typename T,
typename PARTICLETYPE>
130 std::uint8_t* rawBuffer )
132 using namespace descriptors;
134 std::size_t particleID;
142 if (particleSystem.
size()==0){
147 auto particleTmp = particleSystemTmp.
get(0);
150 std::size_t globalID = particleTmp.template getField<PARALLELIZATION,ID>();
157 for (std::size_t iP=0; iP<particleSystem.
size(); ++iP){
158 auto particle = particleSystem.
get(iP);
159 auto globalIDiP = particle.template getField<PARALLELIZATION,ID>();
160 if (globalIDiP==globalID){
181template<
typename T,
typename PARTICLETYPE>
185 int globiC,
int globiCdest,
186 std::multimap<
int,std::unique_ptr<std::uint8_t[]>>& rankDataMapRelocationInter,
187 std::vector<std::unique_ptr<std::uint8_t[]>>& dataListRelocationIntra,
188 std::size_t serialSize )
193 std::unique_ptr<std::uint8_t[]> buffer(
new std::uint8_t[serialSize]{ });
200 int rankDest = loadBalancer.rank(globiCdest);
201 if (rank != rankDest){
203 rankDataMapRelocationInter.insert(std::make_pair(rankDest, std::move(buffer)));
206 dataListRelocationIntra.push_back(std::move(buffer));
209#ifdef VERBOSE_COMMUNICATION
210 auto globalID = particle.template getField<
212 std::cout << std::endl <<
"[Particle] SENDING";
213 if (rank != rankDest){ std::cout <<
"(inter)"; }
else { std::cout <<
"(intra)"; }
214 std::cout <<
" Particle"
215 <<
"(globalID=" << globalID <<
") "
216 <<
"from iC" << globiC <<
"(rank=" << rank <<
") to iC"
217 << globiCdest <<
"(rank=" << rankDest <<
")" << std::endl;
218 particle.template print<true>();
224template<
typename T,
typename PARTICLETYPE>
229 std::multimap<
int,std::unique_ptr<std::uint8_t[]>>& rankDataMapRelocationInter,
230 std::vector<std::unique_ptr<std::uint8_t[]>>& dataListRelocationIntra,
239 const int globiCcentre = particle.template getField<descriptors::PARALLELIZATION,descriptors::IC>();
242 [&](
const int globiCdest){
244 rankDataMapRelocationInter, dataListRelocationIntra, serialSize);
251template<
typename T,
typename PARTICLETYPE>
255 std::multimap<
int,std::unique_ptr<std::uint8_t[]>>& rankDataMapRelocationInter,
256 std::vector<std::unique_ptr<std::uint8_t[]>>& dataListRelocationIntra,
259 using namespace descriptors;
260 using namespace particles::access;
261 constexpr unsigned D = PARTICLETYPE::d;
275 int globiCcentreLast= -1;
276 if constexpr( providesSurface<PARTICLETYPE>() ){
277 globiCcentreLast = particle.template getField<PARALLELIZATION,IC>();
282 periodicity, particle.template getField<GENERAL,POSITION>());
284 int globiCcentre = -1;
286 auto iC = cuboidDecomposition.getC(position);
287 const bool particleCentreInDomain = iC.operator bool();
288 if (particleCentreInDomain) {
303 particle.template setField<GENERAL, POSITION>(position);
316 if constexpr( providesSurface<PARTICLETYPE>() ){
317 particle.template setField<PARALLELIZATION,IC>(globiCcentre);
321 if (particleCentreInDomain){
326 if (globiCcentre == globiC){
328 if constexpr( providesSurface<PARTICLETYPE>() ){
330 rankDataMapRelocationInter, dataListRelocationIntra, serialSize, periodicity );
335 if constexpr( providesSurface<PARTICLETYPE>() ){
340 if(globiCcentre!=globiCcentreLast){
341#ifdef VERBOSE_COMMUNICATION
343 std::cout << std::endl <<
"[Particle] "
345 << globiCcentreLast <<
"->" << globiCcentre <<
")"
346 <<
" at sending from iC=" << globiC
347 <<
"(rank=" << rank <<
")" << std::endl;
352 particle, globiC, globiCcentre,
353 rankDataMapRelocationInter, dataListRelocationIntra, serialSize);
355 if constexpr( providesSurface<PARTICLETYPE>() ){
359 particle, globiCcentre, globiC,
360 rankDataMapRelocationInter, dataListRelocationIntra, serialSize);
364 rankDataMapRelocationInter, dataListRelocationIntra, serialSize, periodicity );
371 particle, globiC, globiCcentre,
372 rankDataMapRelocationInter, dataListRelocationIntra, serialSize);
374 particle.template setField<GENERAL,INVALID>(
true);
379 particle.template setField<GENERAL,INVALID>(
true);
382 auto globalID = particle.template getField<PARALLELIZATION,ID>();
383 std::cout <<
">>>> WARNING: Particle " << globalID
384 <<
" lost domain at iC="<< globiC
385 <<
" (rank=" << rank <<
") <<<<" << std::endl;
392 std::multimap<
int,std::unique_ptr<std::uint8_t[]>>& rankDataMap,
393 std::map<
int, std::vector<std::uint8_t>>& rankDataMapSorted,
394 std::size_t serialSize )
397 for (
auto& rankDataPair : rankDataMap) {
399 int rankDest = rankDataPair.first;
400 std::uint8_t* buffer = rankDataPair.second.get();
403 rankDataMapSorted[rankDest].insert(
404 rankDataMapSorted[rankDest].end(),
406 buffer + serialSize );
410#ifdef PARALLEL_MODE_MPI
413 std::map<
int, std::vector<std::uint8_t> >& rankDataMapSorted,
414 const std::set<int>& availableRanks,
415 const std::size_t serialSize,
421 int noRelevantRanks = availableRanks.size();
424 mpiNbHelper.
allocate(noRelevantRanks);
427 for (
int rankEval : availableRanks) {
430 rankDataMapSorted[rankEval].data(),
431 rankDataMapSorted[rankEval].size(),
445template<
typename T,
typename PARTICLETYPE>
448 std::vector<std::vector<std::size_t>>& receivedLocalIDs,
451 using namespace descriptors;
464 int lociC = loadBalancer.loc(globiC);
467 std::size_t locId = particle.
getId();
469#ifdef VERBOSE_COMMUNICATION
470 std::cout <<
"ReceivedLocalIDs:";
471 for (
int i=0; i<receivedLocalIDs[lociC].size(); ++i){
472 auto idPrint = receivedLocalIDs[lociC][i];
473 std::cout << idPrint;
474 if (i<receivedLocalIDs.size()){ std::cout <<
","; }
476 std::cout << std::endl;
480 if( std::find(receivedLocalIDs[lociC].begin(),receivedLocalIDs[lociC].end(),
481 locId) == receivedLocalIDs[lociC].end()){
483 particle.template setField<GENERAL,INVALID>(
true);
485#ifdef VERBOSE_COMMUNICATION
487 std::size_t globalID = particle.template getField<PARALLELIZATION,ID>();
488 std::cout << std::endl <<
"[Particle] "
489 <<
"INVALIDATING Particle"
490 <<
"(globalID=" << globalID <<
") "
491 <<
"on iC" << globiC <<
"(rank=" << rank <<
")" << std::endl;
492 particle.template print<true>();
520template<
typename T,
typename PARTICLETYPE>
523 std::vector<std::vector<std::size_t>>& receivedLocalIDs,
525 std::uint8_t* rawBuffer )
527 using namespace descriptors;
538 if (globiC == globiCdest){
541 [[maybe_unused]] std::size_t particleID =
543 particleSystem, rawBuffer);
547 receivedLocalIDs[iC].push_back(particleID);
550#ifdef VERBOSE_COMMUNICATION
552 auto particle = particleSystem.
get(particleID);
553 std::size_t globalID = particle.template getField<PARALLELIZATION,ID>();
554 std::cout << std::endl <<
"[Particle] RECEIVING";
555 if (rankOrig != rank){ std::cout <<
"(inter)"; }
else { std::cout <<
"(intra)"; }
556 std::cout <<
" Particle"
557 <<
"(globalID=" << globalID <<
") "
558 <<
"from iC" <<
"X" <<
"(rank=" << rankOrig <<
") to iC"
559 << globiCdest <<
"(rank=" << rank <<
")" << std::endl;
560 particle.template print<true>();
570#ifdef PARALLEL_MODE_MPI
577 const std::set<int>& availableRanks,
578 std::size_t serialSize,
584 for (
int rankOrig : availableRanks) {
588 std::vector<std::uint8_t> recv_buffer(noBytesReceived);
589#ifdef VERBOSE_COMMUNICATION
591 std::cout <<
"Serial(rank=" << rank <<
"): "
592 <<
"noBytesToBeReceived=" << noBytesReceived <<
"; " << std::endl;
602 for (
unsigned iByte=0; iByte < noBytesReceived; iByte += serialSize) {
604 std::uint8_t* rawBuffer = &recv_buffer[iByte];
606 f( rankOrig, rawBuffer );
607#ifdef VERBOSE_COMMUNICATION
609 std::cout <<
"Serial(rank=" << rank <<
"): "
610 <<
"noBytesReceived=" << noBytesReceived <<
"; "
611 <<
"iByte=" << iByte << std::endl;
624template<
typename T,
typename PARTICLETYPE>
627 std::vector<std::unique_ptr<std::uint8_t[]>>& dataListRelocationIntra,
628 std::size_t serialSize,
629 MPI_Comm particleDistributionComm,
633 using namespace particles::access;
641 std::vector<std::vector<std::size_t>> receivedLocalIDs(
642 loadBalancer.size());
645 for (
auto& buffer : dataListRelocationIntra ){
647 std::uint8_t* rawBuffer = buffer.get();
656 particleDistributionComm, mpiNbHelper,
657 [&](
int rankOrig, std::uint8_t* rawBuffer){
663 if constexpr( providesSurface<PARTICLETYPE>() ){
670template<
typename T,
typename PARTICLETYPE>
674#ifdef PARALLEL_MODE_MPI
675 MPI_Comm particleDistributionComm,
695 std::size_t serialSizeField = communicatable.size(index);
696 serialSize += serialSizeField;
699 std::multimap<int,std::unique_ptr<std::uint8_t[]>> rankDataMapRelocationInter;
700 std::vector<std::unique_ptr<std::uint8_t[]>> dataListRelocationIntra;
701 checkRelocation( sParticleSystem, physDeltaX, rankDataMapRelocationInter,
702 dataListRelocationIntra, serialSize, periodicity );
704#ifdef PARALLEL_MODE_MPI
713 std::map<int, std::vector<std::uint8_t> > rankDataMapSorted;
716 fillSendBuffer( rankDataMapRelocationInter, rankDataMapSorted, serialSize );
720 serialSize, particleDistributionComm, mpiNbHelper );
724 serialSize, particleDistributionComm, mpiNbHelper, periodicity );
728 <<
"ERROR: Invalidation treatement for sequential runs not implemented yet!"
735#ifdef PARALLEL_MODE_MPI
737template<
typename DATA>
739 std::set<int>& availableRanks,
741 MPI_Comm commGroup = MPI_COMM_WORLD)
745 const int serialSize = communicatable.size();
748 serialSize, commGroup, mpiNbHelper,
749 [&](
int rankOrig, std::uint8_t* rawBuffer){
751 communicatable.deserialize( rawBuffer );
753 dataVector.push_back(data);
SoA storage for instances of a single FIELD.
LoadBalancer< T > & getLoadBalancer()
Read and write access to the load balancer.
CuboidDecomposition< T, D > & getCuboidDecomposition()
Read and write access to cuboid geometry.
auto & get()
Expose container.
constexpr std::size_t size()
Size of ParticleSystem.
std::size_t getId() const
Return memory ID of the currently represented particle.
SuperStructure< T, PARTICLETYPE::d > & getSuperStructure()
const std::set< int > & getExtendedNeighbourRanks()
std::size_t getSerialSize() const
void iSend(T *buf, int count, int dest, MPI_Request *request, int tag=0, MPI_Comm comm=MPI_COMM_WORLD)
Sends data at *buf, non blocking.
int getRank() const
Returns the process ID.
std::size_t probeReceiveSize(int source, MPI_Datatype type, int tag=0, MPI_Comm comm=MPI_COMM_WORLD)
Probe size of incoming message.
void waitAll(MpiNonBlockingHelper &mpiNbHelper)
Complete a series of non-blocking MPI operations.
void receive(T *buf, int count, int source, int tag=0, MPI_Comm comm=MPI_COMM_WORLD)
Receives data at *buf, blocking.
Helper class for non blocking MPI communication.
void allocate(unsigned i)
Allocates memory.
MPI_Request * get_mpiRequest(int i=0) const
Get the specified request object.
constexpr bool providesSurface()
Provides group SURFACE.
Vector< T, PARTICLETYPE::d > getPosition(Particle< T, PARTICLETYPE > particle)
void receiveAndExecuteForData(const std::set< int > &availableRanks, std::size_t serialSize, MPI_Comm Comm, singleton::MpiNonBlockingHelper &mpiNbHelper, F f)
void forSystemsInSuperParticleSystem(SuperParticleSystem< T, PARTICLETYPE > &sParticleSystem, F f)
std::size_t insertSerializedParticle(ParticleSystem< T, PARTICLETYPE > &particleSystem, std::size_t idxParticle, std::uint8_t rawBuffer[])
void assignParticleToIC(SuperParticleSystem< T, PARTICLETYPE > &sParticleSystem, std::vector< std::vector< std::size_t > > &receivedLocalIDs, int rankOrig, std::uint8_t *rawBuffer)
std::set< int > getSurfaceTouchingICs(CuboidDecomposition< T, D > &cuboidDecomposition, Vector< T, D > position, T circumRadius, const Vector< bool, D > &periodicity, int globiC, F f=[](int){})
Get a set of surface touching iCs (that are not globiC) Allows to run an optional function per unique...
void sendMappedData(std::map< int, std::vector< std::uint8_t > > &rankDataMapSorted, const std::set< int > &availableRanks, const std::size_t serialSize, MPI_Comm Comm, singleton::MpiNonBlockingHelper &mpiNbHelper)
void updateParticleCuboidDistribution(SuperParticleSystem< T, PARTICLETYPE > &sParticleSystem, const T physDeltaX, MPI_Comm particleDistributionComm, const Vector< bool, PARTICLETYPE::d > &periodicity)
std::size_t appendSerializedParticle(ParticleSystem< T, PARTICLETYPE > &particleSystem, std::uint8_t rawBuffer[])
std::size_t deserializeIcDest(int &globiCdest, std::uint8_t *bufferRaw)
void fillSendBuffer(std::multimap< int, std::unique_ptr< std::uint8_t[]> > &rankDataMap, std::map< int, std::vector< std::uint8_t > > &rankDataMapSorted, std::size_t serialSize)
void updateSurfacePtr(ParticleSystem< T, PARTICLETYPE > &particleSystem, std::size_t iP)
void checkInvalidationOnReceival(SuperParticleSystem< T, PARTICLETYPE > &sParticleSystem, std::vector< std::vector< std::size_t > > &receivedLocalIDs, const Vector< bool, PARTICLETYPE::d > &periodicity)
void receiveParticles(SuperParticleSystem< T, PARTICLETYPE > &sParticleSystem, std::vector< std::unique_ptr< std::uint8_t[]> > &dataListRelocationIntra, std::size_t serialSize, MPI_Comm particleDistributionComm, singleton::MpiNonBlockingHelper &mpiNbHelper, const Vector< bool, PARTICLETYPE::d > &periodicity)
void forParticlesInSuperParticleSystem(SuperParticleSystem< T, PARTICLETYPE > &sParticleSystem, F f)
void prepareRelocation(SuperParticleSystem< T, PARTICLETYPE > &sParticleSystem, Particle< T, PARTICLETYPE > &particle, int globiC, int globiCdest, std::multimap< int, std::unique_ptr< std::uint8_t[]> > &rankDataMapRelocationInter, std::vector< std::unique_ptr< std::uint8_t[]> > &dataListRelocationIntra, std::size_t serialSize)
std::size_t attachSerializedParticle(ParticleSystem< T, PARTICLETYPE > &particleSystem, std::uint8_t *rawBuffer)
void checkRelocation(SuperParticleSystem< T, PARTICLETYPE > &sParticleSystem, const T physDeltaX, std::multimap< int, std::unique_ptr< std::uint8_t[]> > &rankDataMapRelocationInter, std::vector< std::unique_ptr< std::uint8_t[]> > &dataListRelocationIntra, std::size_t serialSize, const Vector< bool, PARTICLETYPE::d > &periodicity)
void collectDataAndAppendVector(std::vector< DATA > &dataVector, std::set< int > &availableRanks, singleton::MpiNonBlockingHelper &mpiNbHelper, MPI_Comm commGroup=MPI_COMM_WORLD)
void deserializeIcDestAndParticle(int &globiCdest, Particle< T, PARTICLETYPE > &particle, std::uint8_t *bufferiCandParticle)
T applyPeriodicityToPosition(const bool isPeriodic, T position, const T max, const T min)
void serializeIcDestAndParticle(int globiCdest, Particle< T, PARTICLETYPE > &particle, std::uint8_t *bufferiCandParticle)
void checkSurfaceRelocation(SuperParticleSystem< T, PARTICLETYPE > &sParticleSystem, const T physDeltaX, Particle< T, PARTICLETYPE > &particle, int globiC, std::multimap< int, std::unique_ptr< std::uint8_t[]> > &rankDataMapRelocationInter, std::vector< std::unique_ptr< std::uint8_t[]> > &dataListRelocationIntra, std::size_t serialSize, const Vector< bool, PARTICLETYPE::d > &periodicity)
T evalCircumRadius(T const contactDetectionDistance, T const circumRadius, T const epsilon)
Top level namespace for all of OpenLB.
std::conditional_t< D==2, SmoothIndicatorF2D< T, T, PARTICLE >, SmoothIndicatorF3D< T, T, PARTICLE > > SmoothIndicatorF