|
20 | 20 | */ |
21 | 21 | #include "openPMD/auxiliary/Date.hpp" |
22 | 22 | #include "openPMD/auxiliary/Filesystem.hpp" |
| 23 | +#include "openPMD/auxiliary/MPI.hpp" |
23 | 24 | #include "openPMD/auxiliary/StringManip.hpp" |
24 | 25 | #include "openPMD/IO/AbstractIOHandler.hpp" |
25 | 26 | #include "openPMD/IO/AbstractIOHandlerHelper.hpp" |
@@ -146,6 +147,91 @@ SeriesImpl::setMeshesPath(std::string const& mp) |
146 | 147 | return *this; |
147 | 148 | } |
148 | 149 |
|
| 150 | +chunk_assignment::RankMeta |
| 151 | +SeriesImpl::mpiRanksMetaInfo() const |
| 152 | +{ |
| 153 | + std::vector< std::string > asContiguousVector; |
| 154 | + try |
| 155 | + { |
| 156 | + asContiguousVector = |
| 157 | + getAttribute( "rankMetaInfo" ).get< std::vector< std::string > >(); |
| 158 | + } |
| 159 | + catch( std::runtime_error const & ) |
| 160 | + { |
| 161 | + // workaround: if vector has length 1, some backends may report a |
| 162 | + // single value instead of a vector |
| 163 | + asContiguousVector = { |
| 164 | + getAttribute( "rankMetaInfo" ).get< std::string >() |
| 165 | + }; |
| 166 | + } |
| 167 | + chunk_assignment::RankMeta res; |
| 168 | + for( size_t i = 0; i < asContiguousVector.size(); ++i ) |
| 169 | + { |
| 170 | + res[ i ] = std::move( asContiguousVector[ i ] ); |
| 171 | + } |
| 172 | + return res; |
| 173 | +} |
| 174 | + |
| 175 | +SeriesImpl & |
| 176 | +SeriesImpl::setMpiRanksMetaInfo( chunk_assignment::RankMeta rankMeta ) |
| 177 | +{ |
| 178 | + std::vector< std::string > asContiguousVector; |
| 179 | + asContiguousVector.reserve( rankMeta.size() ); |
| 180 | + try |
| 181 | + { |
| 182 | + for( auto & hostname : rankMeta ) |
| 183 | + { |
| 184 | + asContiguousVector.emplace_back( std::move( hostname.second ) ); |
| 185 | + } |
| 186 | + } |
| 187 | + catch( std::out_of_range const & ) |
| 188 | + { |
| 189 | + throw std::runtime_error( "[setMpiRanksMetaInfo] Can only set meta " |
| 190 | + "info for contiguous ranks 0 to n." ); |
| 191 | + } |
| 192 | + |
| 193 | + setAttribute( "rankMetaInfo", std::move( asContiguousVector ) ); |
| 194 | + return *this; |
| 195 | +} |
| 196 | + |
| 197 | +SeriesImpl & |
| 198 | +SeriesImpl::setMpiRanksMetaInfo( std::string const & myRankInfo ) |
| 199 | +{ |
| 200 | + if( auxiliary::starts_with( myRankInfo, '@' ) ) |
| 201 | + { |
| 202 | + // read from file |
| 203 | + std::string fileName = auxiliary::replace_first( myRankInfo, "@", "" ); |
| 204 | + std::vector< std::string > rankMeta; |
| 205 | + try |
| 206 | + { |
| 207 | + rankMeta = auxiliary::read_file_by_lines( fileName ); |
| 208 | + } |
| 209 | + catch( auxiliary::no_such_file_error const & ) |
| 210 | + { |
| 211 | + std::cerr << "Not setting rank meta information, because file has " |
| 212 | + "not been found: " |
| 213 | + << fileName << std::endl; |
| 214 | + return *this; |
| 215 | + } |
| 216 | + setAttribute( "rankMetaInfo", rankMeta ); |
| 217 | + return *this; |
| 218 | + } |
| 219 | +#if openPMD_HAVE_MPI |
| 220 | + auto & series = get(); |
| 221 | + int rank; |
| 222 | + MPI_Comm_rank( series.m_communicator, &rank ); |
| 223 | + std::vector< std::string > rankMeta = |
| 224 | + auxiliary::collectStringsTo( series.m_communicator, 0, myRankInfo ); |
| 225 | + if( rank == 0 ) |
| 226 | + { |
| 227 | + setAttribute( "rankMetaInfo", std::move( rankMeta ) ); |
| 228 | + } |
| 229 | +#else |
| 230 | + setAttribute( "rankMetaInfo", std::vector< std::string >{ myRankInfo } ); |
| 231 | +#endif |
| 232 | + return *this; |
| 233 | +} |
| 234 | + |
149 | 235 | std::string |
150 | 236 | SeriesImpl::particlesPath() const |
151 | 237 | { |
@@ -1156,6 +1242,7 @@ SeriesInternal::SeriesInternal( |
1156 | 1242 | static_cast< internal::SeriesData * >( this ), |
1157 | 1243 | static_cast< internal::AttributableData * >( this ) } |
1158 | 1244 | { |
| 1245 | + m_communicator = comm; |
1159 | 1246 | auto input = parseInput( filepath ); |
1160 | 1247 | auto handler = |
1161 | 1248 | createIOHandler( input->path, at, input->format, comm, options ); |
@@ -1322,4 +1409,4 @@ namespace |
1322 | 1409 | } |
1323 | 1410 | } |
1324 | 1411 | } // namespace [anonymous] |
1325 | | - } // namespace openPMD |
| 1412 | +} // namespace openPMD |
0 commit comments