|
| 1 | +#ifndef AMREX_PTL_OPENPMD_API_H |
| 2 | +#define AMREX_PTL_OPENPMD_API_H |
| 3 | + |
| 4 | +//#include <openPMD/openpmd.hpp> |
| 5 | + |
| 6 | + |
| 7 | +struct AMReX_PtlCounter |
| 8 | +{ |
| 9 | + int m_MPIRank = 0; |
| 10 | + int m_MPISize = 1; |
| 11 | + |
| 12 | + unsigned long long m_Total = 0; |
| 13 | + |
| 14 | + std::vector<unsigned long long> m_ParticleCounterByLevel; |
| 15 | + |
| 16 | + unsigned long GetTotalNumParticles () const { return m_Total;} |
| 17 | + |
| 18 | + std::vector<unsigned long long> m_ParticleOffsetAtRank; |
| 19 | + std::vector<unsigned long long> m_ParticleSizeAtRank; |
| 20 | +}; |
| 21 | + |
| 22 | + |
| 23 | +void CountParticles() |
| 24 | +{ |
| 25 | + |
| 26 | + m_PtlCounter.m_MPISize = amrex::ParallelDescriptor::NProcs(); |
| 27 | + m_PtlCounter.m_MPIRank = amrex::ParallelDescriptor::MyProc(); |
| 28 | + |
| 29 | + m_PtlCounter.m_ParticleCounterByLevel.resize(this->finestLevel()+1); |
| 30 | + m_PtlCounter.m_ParticleOffsetAtRank.resize(this->finestLevel()+1); |
| 31 | + m_PtlCounter.m_ParticleSizeAtRank.resize(this->finestLevel()+1); |
| 32 | + |
| 33 | + auto lf_GetParticleOffsetOfProcessor = [&](const long& numParticles, |
| 34 | + unsigned long long& offset, |
| 35 | + unsigned long long& sum) -> void |
| 36 | + { |
| 37 | + std::vector<long> result(m_PtlCounter.m_MPISize, 0); |
| 38 | + amrex::ParallelGather::Gather (numParticles, result.data(), -1, amrex::ParallelDescriptor::Communicator()); |
| 39 | + |
| 40 | + sum = 0; |
| 41 | + offset = 0; |
| 42 | + for (int i=0; i<result.size(); i++) { |
| 43 | + sum += result[i]; |
| 44 | + if ( i < m_PtlCounter.m_MPIRank) |
| 45 | + offset += result[i]; |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + for (auto currentLevel = 0; currentLevel <= this->finestLevel(); currentLevel++) |
| 50 | + { |
| 51 | + long numParticles = 0; // numParticles in this processor |
| 52 | + |
| 53 | + //for (ParIter pti(*this, currentLevel); pti.isValid(); ++pti) { |
| 54 | + for (ParConstIterType pti(*this, currentLevel); pti.isValid(); ++pti) { |
| 55 | + auto numParticleOnTile = pti.numParticles(); |
| 56 | + numParticles += numParticleOnTile; |
| 57 | + } |
| 58 | + |
| 59 | + unsigned long long offset=0; // offset of this level |
| 60 | + unsigned long long sum=0; // numParticles in this level (sum from all processors) |
| 61 | + lf_GetParticleOffsetOfProcessor(numParticles, offset, sum); |
| 62 | + |
| 63 | + m_PtlCounter.m_ParticleCounterByLevel[currentLevel] = sum; |
| 64 | + m_PtlCounter.m_ParticleOffsetAtRank[currentLevel] = offset; |
| 65 | + m_PtlCounter.m_ParticleSizeAtRank[currentLevel] = numParticles; |
| 66 | + |
| 67 | + // adjust offset, it should be numbered after particles from previous levels |
| 68 | + for (auto lv=0; lv<currentLevel; lv++) |
| 69 | + { |
| 70 | + m_PtlCounter.m_ParticleOffsetAtRank[currentLevel] += m_PtlCounter.m_ParticleCounterByLevel[lv]; |
| 71 | + } |
| 72 | + |
| 73 | + m_PtlCounter.m_Total += sum; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +AMReX_PtlCounter m_PtlCounter; |
| 78 | +#endif // AMREX_PTL_OPENPMD_API_H |
| 79 | + |
| 80 | + |
0 commit comments