From 43b1e31f096978332d31495e7a217d185106bf0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20P=C3=B6schel?= Date: Mon, 23 Nov 2020 13:32:46 +0100 Subject: [PATCH] Move MPI functions to own object file --- CMakeLists.txt | 1 + include/openPMD/auxiliary/MPI.hpp | 122 +--------------------------- src/auxiliary/MPI.cpp | 131 ++++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+), 120 deletions(-) create mode 100644 src/auxiliary/MPI.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 7aea952d07..1327ac7596 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -351,6 +351,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 diff --git a/include/openPMD/auxiliary/MPI.hpp b/include/openPMD/auxiliary/MPI.hpp index 04504d42af..9df26c4a14 100644 --- a/include/openPMD/auxiliary/MPI.hpp +++ b/include/openPMD/auxiliary/MPI.hpp @@ -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 diff --git a/src/auxiliary/MPI.cpp b/src/auxiliary/MPI.cpp new file mode 100644 index 0000000000..7501ccf7f1 --- /dev/null +++ b/src/auxiliary/MPI.cpp @@ -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 \ No newline at end of file