Skip to content

Commit

Permalink
Move MPI functions to own object file
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed Mar 8, 2021
1 parent b2ce2b9 commit bb7fa45
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 120 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ set(CORE_SOURCE
src/auxiliary/Date.cpp
src/auxiliary/Filesystem.cpp
src/auxiliary/JSON.cpp
src/auxiliary/MPI.cpp
src/backend/Attributable.cpp
src/backend/BaseRecordComponent.cpp
src/backend/MeshRecordComponent.cpp
Expand Down
122 changes: 2 additions & 120 deletions include/openPMD/auxiliary/MPI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,134 +12,16 @@ namespace openPMD
{
namespace auxiliary
{
inline
std::vector< std::string >
collectStringsTo(
MPI_Comm communicator,
int destRank,
std::string const & thisRankString )
{
int rank, size;
MPI_Comm_rank( communicator, &rank );
MPI_Comm_size( communicator, &size );
int sendLength = thisRankString.size() + 1;
std::string const & thisRankString );

int * sizesBuffer = nullptr;
int * displs = nullptr;
if( rank == destRank )
{
sizesBuffer = new int[ size ];
displs = new int[ size ];
}

MPI_Gather(
&sendLength,
1,
MPI_INT,
sizesBuffer,
1,
MPI_INT,
destRank,
MPI_COMM_WORLD );

char * namesBuffer = nullptr;
if( rank == destRank )
{
size_t sum = 0;
for( int i = 0; i < size; ++i )
{
displs[ i ] = sum;
sum += sizesBuffer[ i ];
}
namesBuffer = new char[ sum ];
}

MPI_Gatherv(
thisRankString.c_str(),
sendLength,
MPI_CHAR,
namesBuffer,
sizesBuffer,
displs,
MPI_CHAR,
destRank,
MPI_COMM_WORLD );

if( rank == destRank )
{
std::vector< std::string > hostnames( size );
for( int i = 0; i < size; ++i )
{
hostnames[ i ] = std::string( namesBuffer + displs[ i ] );
}

delete[] sizesBuffer;
delete[] displs;
delete[] namesBuffer;
return hostnames;
}
else
{
return std::vector< std::string >();
}
}

inline
std::vector< std::string >
distributeStringsToAllRanks(
MPI_Comm communicator,
std::string const & thisRankString )
{
int rank, size;
MPI_Comm_rank( communicator, &rank );
MPI_Comm_size( communicator, &size );
int sendLength = thisRankString.size() + 1;

int * sizesBuffer = new int[ size ];
int * displs = new int[ size ];

MPI_Allgather(
&sendLength,
1,
MPI_INT,
sizesBuffer,
1,
MPI_INT,
MPI_COMM_WORLD );

char * namesBuffer;
{
size_t sum = 0;
for( int i = 0; i < size; ++i )
{
displs[ i ] = sum;
sum += sizesBuffer[ i ];
}
namesBuffer = new char[ sum ];
}

MPI_Allgatherv(
thisRankString.c_str(),
sendLength,
MPI_CHAR,
namesBuffer,
sizesBuffer,
displs,
MPI_CHAR,
MPI_COMM_WORLD );

std::vector< std::string > hostnames( size );
for( int i = 0; i < size; ++i )
{
hostnames[ i ] = std::string( namesBuffer + displs[ i ] );
}

delete[] sizesBuffer;
delete[] displs;
delete[] namesBuffer;
return hostnames;

}
std::string const & thisRankString );
} // namespace auxiliary
} // namespace openPMD

Expand Down
131 changes: 131 additions & 0 deletions src/auxiliary/MPI.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#include "openPMD/auxiliary/MPI.hpp"

#if openPMD_HAVE_MPI

namespace openPMD
{
namespace auxiliary
{
std::vector< std::string >
collectStringsTo(
MPI_Comm communicator,
int destRank,
std::string const & thisRankString )
{
int rank, size;
MPI_Comm_rank( communicator, &rank );
MPI_Comm_size( communicator, &size );
int sendLength = thisRankString.size() + 1;

int * sizesBuffer = nullptr;
int * displs = nullptr;
if( rank == destRank )
{
sizesBuffer = new int[ size ];
displs = new int[ size ];
}

MPI_Gather(
&sendLength,
1,
MPI_INT,
sizesBuffer,
1,
MPI_INT,
destRank,
MPI_COMM_WORLD );

char * namesBuffer = nullptr;
if( rank == destRank )
{
size_t sum = 0;
for( int i = 0; i < size; ++i )
{
displs[ i ] = sum;
sum += sizesBuffer[ i ];
}
namesBuffer = new char[ sum ];
}

MPI_Gatherv(
thisRankString.c_str(),
sendLength,
MPI_CHAR,
namesBuffer,
sizesBuffer,
displs,
MPI_CHAR,
destRank,
MPI_COMM_WORLD );

if( rank == destRank )
{
std::vector< std::string > hostnames( size );
for( int i = 0; i < size; ++i )
{
hostnames[ i ] = std::string( namesBuffer + displs[ i ] );
}

delete[] sizesBuffer;
delete[] displs;
delete[] namesBuffer;
return hostnames;
}
else
{
return std::vector< std::string >();
}
}

std::vector< std::string >
distributeStringsToAllRanks(
MPI_Comm communicator,
std::string const & thisRankString )
{
int rank, size;
MPI_Comm_rank( communicator, &rank );
MPI_Comm_size( communicator, &size );
int sendLength = thisRankString.size() + 1;

int * sizesBuffer = new int[ size ];
int * displs = new int[ size ];

MPI_Allgather(
&sendLength, 1, MPI_INT, sizesBuffer, 1, MPI_INT, MPI_COMM_WORLD );

char * namesBuffer;
{
size_t sum = 0;
for( int i = 0; i < size; ++i )
{
displs[ i ] = sum;
sum += sizesBuffer[ i ];
}
namesBuffer = new char[ sum ];
}

MPI_Allgatherv(
thisRankString.c_str(),
sendLength,
MPI_CHAR,
namesBuffer,
sizesBuffer,
displs,
MPI_CHAR,
MPI_COMM_WORLD );

std::vector< std::string > hostnames( size );
for( int i = 0; i < size; ++i )
{
hostnames[ i ] = std::string( namesBuffer + displs[ i ] );
}

delete[] sizesBuffer;
delete[] displs;
delete[] namesBuffer;
return hostnames;
}
} // namespace auxiliary
} // namespace openPMD

#endif

0 comments on commit bb7fa45

Please sign in to comment.