|
21 | 21 | #include "openPMD/auxiliary/Date.hpp" |
22 | 22 | #include "openPMD/auxiliary/Filesystem.hpp" |
23 | 23 | #include "openPMD/auxiliary/JSON.hpp" |
| 24 | +#include "openPMD/auxiliary/MPI.hpp" |
24 | 25 | #include "openPMD/auxiliary/StringManip.hpp" |
25 | 26 | #include "openPMD/IO/AbstractIOHandler.hpp" |
26 | 27 | #include "openPMD/IO/AbstractIOHandlerHelper.hpp" |
@@ -162,6 +163,91 @@ SeriesImpl::setMeshesPath(std::string const& mp) |
162 | 163 | return *this; |
163 | 164 | } |
164 | 165 |
|
| 166 | +chunk_assignment::RankMeta |
| 167 | +SeriesImpl::mpiRanksMetaInfo() const |
| 168 | +{ |
| 169 | + std::vector< std::string > asContiguousVector; |
| 170 | + try |
| 171 | + { |
| 172 | + asContiguousVector = |
| 173 | + getAttribute( "rankMetaInfo" ).get< std::vector< std::string > >(); |
| 174 | + } |
| 175 | + catch( std::runtime_error const & ) |
| 176 | + { |
| 177 | + // workaround: if vector has length 1, some backends may report a |
| 178 | + // single value instead of a vector |
| 179 | + asContiguousVector = { |
| 180 | + getAttribute( "rankMetaInfo" ).get< std::string >() |
| 181 | + }; |
| 182 | + } |
| 183 | + chunk_assignment::RankMeta res; |
| 184 | + for( size_t i = 0; i < asContiguousVector.size(); ++i ) |
| 185 | + { |
| 186 | + res[ i ] = std::move( asContiguousVector[ i ] ); |
| 187 | + } |
| 188 | + return res; |
| 189 | +} |
| 190 | + |
| 191 | +SeriesImpl & |
| 192 | +SeriesImpl::setMpiRanksMetaInfo( chunk_assignment::RankMeta rankMeta ) |
| 193 | +{ |
| 194 | + std::vector< std::string > asContiguousVector; |
| 195 | + asContiguousVector.reserve( rankMeta.size() ); |
| 196 | + try |
| 197 | + { |
| 198 | + for( auto & hostname : rankMeta ) |
| 199 | + { |
| 200 | + asContiguousVector.emplace_back( std::move( hostname.second ) ); |
| 201 | + } |
| 202 | + } |
| 203 | + catch( std::out_of_range const & ) |
| 204 | + { |
| 205 | + throw std::runtime_error( "[setMpiRanksMetaInfo] Can only set meta " |
| 206 | + "info for contiguous ranks 0 to n." ); |
| 207 | + } |
| 208 | + |
| 209 | + setAttribute( "rankMetaInfo", std::move( asContiguousVector ) ); |
| 210 | + return *this; |
| 211 | +} |
| 212 | + |
| 213 | +SeriesImpl & |
| 214 | +SeriesImpl::setMpiRanksMetaInfo( std::string const & myRankInfo ) |
| 215 | +{ |
| 216 | + if( auxiliary::starts_with( myRankInfo, '@' ) ) |
| 217 | + { |
| 218 | + // read from file |
| 219 | + std::string fileName = auxiliary::replace_first( myRankInfo, "@", "" ); |
| 220 | + std::vector< std::string > rankMeta; |
| 221 | + try |
| 222 | + { |
| 223 | + rankMeta = auxiliary::read_file_by_lines( fileName ); |
| 224 | + } |
| 225 | + catch( auxiliary::no_such_file_error const & ) |
| 226 | + { |
| 227 | + std::cerr << "Not setting rank meta information, because file has " |
| 228 | + "not been found: " |
| 229 | + << fileName << std::endl; |
| 230 | + return *this; |
| 231 | + } |
| 232 | + setAttribute( "rankMetaInfo", rankMeta ); |
| 233 | + return *this; |
| 234 | + } |
| 235 | +#if openPMD_HAVE_MPI |
| 236 | + auto & series = get(); |
| 237 | + int rank; |
| 238 | + MPI_Comm_rank( series.m_communicator, &rank ); |
| 239 | + std::vector< std::string > rankMeta = |
| 240 | + auxiliary::collectStringsTo( series.m_communicator, 0, myRankInfo ); |
| 241 | + if( rank == 0 ) |
| 242 | + { |
| 243 | + setAttribute( "rankMetaInfo", std::move( rankMeta ) ); |
| 244 | + } |
| 245 | +#else |
| 246 | + setAttribute( "rankMetaInfo", std::vector< std::string >{ myRankInfo } ); |
| 247 | +#endif |
| 248 | + return *this; |
| 249 | +} |
| 250 | + |
165 | 251 | std::string |
166 | 252 | SeriesImpl::particlesPath() const |
167 | 253 | { |
@@ -1403,6 +1489,7 @@ SeriesInternal::SeriesInternal( |
1403 | 1489 | { |
1404 | 1490 | nlohmann::json optionsJson = auxiliary::parseOptions( options, comm ); |
1405 | 1491 | parseJsonOptions( *this, optionsJson ); |
| 1492 | + m_communicator = comm; |
1406 | 1493 | auto input = parseInput( filepath ); |
1407 | 1494 | auto handler = createIOHandler( |
1408 | 1495 | input->path, at, input->format, comm, std::move( optionsJson ) ); |
|
0 commit comments