Skip to content

Commit 6da1db3

Browse files
committed
Move MPI functions to own object file
1 parent 3c56cd9 commit 6da1db3

File tree

3 files changed

+134
-120
lines changed

3 files changed

+134
-120
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ set(CORE_SOURCE
318318
src/auxiliary/Date.cpp
319319
src/auxiliary/Filesystem.cpp
320320
src/auxiliary/JSON.cpp
321+
src/auxiliary/MPI.cpp
321322
src/backend/Attributable.cpp
322323
src/backend/BaseRecordComponent.cpp
323324
src/backend/MeshRecordComponent.cpp

include/openPMD/auxiliary/MPI.hpp

Lines changed: 2 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -12,134 +12,16 @@ namespace openPMD
1212
{
1313
namespace auxiliary
1414
{
15-
inline
1615
std::vector< std::string >
1716
collectStringsTo(
1817
MPI_Comm communicator,
1918
int destRank,
20-
std::string const & thisRankString )
21-
{
22-
int rank, size;
23-
MPI_Comm_rank( communicator, &rank );
24-
MPI_Comm_size( communicator, &size );
25-
int sendLength = thisRankString.size() + 1;
19+
std::string const & thisRankString );
2620

27-
int * sizesBuffer = nullptr;
28-
int * displs = nullptr;
29-
if( rank == destRank )
30-
{
31-
sizesBuffer = new int[ size ];
32-
displs = new int[ size ];
33-
}
34-
35-
MPI_Gather(
36-
&sendLength,
37-
1,
38-
MPI_INT,
39-
sizesBuffer,
40-
1,
41-
MPI_INT,
42-
destRank,
43-
MPI_COMM_WORLD );
44-
45-
char * namesBuffer = nullptr;
46-
if( rank == destRank )
47-
{
48-
size_t sum = 0;
49-
for( int i = 0; i < size; ++i )
50-
{
51-
displs[ i ] = sum;
52-
sum += sizesBuffer[ i ];
53-
}
54-
namesBuffer = new char[ sum ];
55-
}
56-
57-
MPI_Gatherv(
58-
thisRankString.c_str(),
59-
sendLength,
60-
MPI_CHAR,
61-
namesBuffer,
62-
sizesBuffer,
63-
displs,
64-
MPI_CHAR,
65-
destRank,
66-
MPI_COMM_WORLD );
67-
68-
if( rank == destRank )
69-
{
70-
std::vector< std::string > hostnames( size );
71-
for( int i = 0; i < size; ++i )
72-
{
73-
hostnames[ i ] = std::string( namesBuffer + displs[ i ] );
74-
}
75-
76-
delete[] sizesBuffer;
77-
delete[] displs;
78-
delete[] namesBuffer;
79-
return hostnames;
80-
}
81-
else
82-
{
83-
return std::vector< std::string >();
84-
}
85-
}
86-
87-
inline
8821
std::vector< std::string >
8922
distributeStringsToAllRanks(
9023
MPI_Comm communicator,
91-
std::string const & thisRankString )
92-
{
93-
int rank, size;
94-
MPI_Comm_rank( communicator, &rank );
95-
MPI_Comm_size( communicator, &size );
96-
int sendLength = thisRankString.size() + 1;
97-
98-
int * sizesBuffer = new int[ size ];
99-
int * displs = new int[ size ];
100-
101-
MPI_Allgather(
102-
&sendLength,
103-
1,
104-
MPI_INT,
105-
sizesBuffer,
106-
1,
107-
MPI_INT,
108-
MPI_COMM_WORLD );
109-
110-
char * namesBuffer;
111-
{
112-
size_t sum = 0;
113-
for( int i = 0; i < size; ++i )
114-
{
115-
displs[ i ] = sum;
116-
sum += sizesBuffer[ i ];
117-
}
118-
namesBuffer = new char[ sum ];
119-
}
120-
121-
MPI_Allgatherv(
122-
thisRankString.c_str(),
123-
sendLength,
124-
MPI_CHAR,
125-
namesBuffer,
126-
sizesBuffer,
127-
displs,
128-
MPI_CHAR,
129-
MPI_COMM_WORLD );
130-
131-
std::vector< std::string > hostnames( size );
132-
for( int i = 0; i < size; ++i )
133-
{
134-
hostnames[ i ] = std::string( namesBuffer + displs[ i ] );
135-
}
136-
137-
delete[] sizesBuffer;
138-
delete[] displs;
139-
delete[] namesBuffer;
140-
return hostnames;
141-
142-
}
24+
std::string const & thisRankString );
14325
} // namespace auxiliary
14426
} // namespace openPMD
14527

src/auxiliary/MPI.cpp

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#include "openPMD/auxiliary/MPI.hpp"
2+
3+
#if openPMD_HAVE_MPI
4+
5+
namespace openPMD
6+
{
7+
namespace auxiliary
8+
{
9+
std::vector< std::string >
10+
collectStringsTo(
11+
MPI_Comm communicator,
12+
int destRank,
13+
std::string const & thisRankString )
14+
{
15+
int rank, size;
16+
MPI_Comm_rank( communicator, &rank );
17+
MPI_Comm_size( communicator, &size );
18+
int sendLength = thisRankString.size() + 1;
19+
20+
int * sizesBuffer = nullptr;
21+
int * displs = nullptr;
22+
if( rank == destRank )
23+
{
24+
sizesBuffer = new int[ size ];
25+
displs = new int[ size ];
26+
}
27+
28+
MPI_Gather(
29+
&sendLength,
30+
1,
31+
MPI_INT,
32+
sizesBuffer,
33+
1,
34+
MPI_INT,
35+
destRank,
36+
MPI_COMM_WORLD );
37+
38+
char * namesBuffer = nullptr;
39+
if( rank == destRank )
40+
{
41+
size_t sum = 0;
42+
for( int i = 0; i < size; ++i )
43+
{
44+
displs[ i ] = sum;
45+
sum += sizesBuffer[ i ];
46+
}
47+
namesBuffer = new char[ sum ];
48+
}
49+
50+
MPI_Gatherv(
51+
thisRankString.c_str(),
52+
sendLength,
53+
MPI_CHAR,
54+
namesBuffer,
55+
sizesBuffer,
56+
displs,
57+
MPI_CHAR,
58+
destRank,
59+
MPI_COMM_WORLD );
60+
61+
if( rank == destRank )
62+
{
63+
std::vector< std::string > hostnames( size );
64+
for( int i = 0; i < size; ++i )
65+
{
66+
hostnames[ i ] = std::string( namesBuffer + displs[ i ] );
67+
}
68+
69+
delete[] sizesBuffer;
70+
delete[] displs;
71+
delete[] namesBuffer;
72+
return hostnames;
73+
}
74+
else
75+
{
76+
return std::vector< std::string >();
77+
}
78+
}
79+
80+
std::vector< std::string >
81+
distributeStringsToAllRanks(
82+
MPI_Comm communicator,
83+
std::string const & thisRankString )
84+
{
85+
int rank, size;
86+
MPI_Comm_rank( communicator, &rank );
87+
MPI_Comm_size( communicator, &size );
88+
int sendLength = thisRankString.size() + 1;
89+
90+
int * sizesBuffer = new int[ size ];
91+
int * displs = new int[ size ];
92+
93+
MPI_Allgather(
94+
&sendLength, 1, MPI_INT, sizesBuffer, 1, MPI_INT, MPI_COMM_WORLD );
95+
96+
char * namesBuffer;
97+
{
98+
size_t sum = 0;
99+
for( int i = 0; i < size; ++i )
100+
{
101+
displs[ i ] = sum;
102+
sum += sizesBuffer[ i ];
103+
}
104+
namesBuffer = new char[ sum ];
105+
}
106+
107+
MPI_Allgatherv(
108+
thisRankString.c_str(),
109+
sendLength,
110+
MPI_CHAR,
111+
namesBuffer,
112+
sizesBuffer,
113+
displs,
114+
MPI_CHAR,
115+
MPI_COMM_WORLD );
116+
117+
std::vector< std::string > hostnames( size );
118+
for( int i = 0; i < size; ++i )
119+
{
120+
hostnames[ i ] = std::string( namesBuffer + displs[ i ] );
121+
}
122+
123+
delete[] sizesBuffer;
124+
delete[] displs;
125+
delete[] namesBuffer;
126+
return hostnames;
127+
}
128+
} // namespace auxiliary
129+
} // namespace openPMD
130+
131+
#endif

0 commit comments

Comments
 (0)