Skip to content

Commit b4fbfc1

Browse files
committed
Add hostname attribute to Series
1 parent 2536ffa commit b4fbfc1

File tree

2 files changed

+118
-1
lines changed

2 files changed

+118
-1
lines changed

include/openPMD/Series.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class SeriesData : public AttributableData
6969
friend class openPMD::SeriesImpl;
7070
friend class openPMD::Iteration;
7171
friend class openPMD::Series;
72+
friend class SeriesInternal;
7273

7374
public:
7475
explicit SeriesData() = default;
@@ -99,6 +100,9 @@ OPENPMD_private :
99100
* one among both flags.
100101
*/
101102
StepStatus m_stepStatus = StepStatus::NoStep;
103+
#if openPMD_HAVE_MPI
104+
MPI_Comm m_communicator;
105+
#endif
102106
}; // SeriesData
103107

104108
class SeriesInternal;
@@ -169,6 +173,32 @@ class SeriesImpl : public AttributableImpl
169173
*/
170174
SeriesImpl& setMeshesPath(std::string const& meshesPath);
171175

176+
/**
177+
* @throw no_such_attribute_error If optional attribute is not present.
178+
* @return Vector with a String per (writing) MPI rank, indicating user-
179+
* defined meta information per rank. Example: host name.
180+
*/
181+
chunk_assignment::RankMeta
182+
mpiRanksMetaInfo() const;
183+
184+
/**
185+
* @brief Set the Mpi Ranks Meta Info attribute, i.e. a Vector with
186+
* a String per (writing) MPI rank, indicating user-
187+
* defined meta information per rank. Example: host name.
188+
*
189+
* @return Reference to modified series.
190+
*/
191+
SeriesImpl & setMpiRanksMetaInfo( chunk_assignment::RankMeta );
192+
193+
/**
194+
* @brief Set the Mpi Ranks Meta Info attribute, i.e. a Vector with
195+
* a String per (writing) MPI rank, indicating user-
196+
* defined meta information per rank. Example: host name.
197+
*
198+
* @return Reference to modified series.
199+
*/
200+
SeriesImpl & setMpiRanksMetaInfo( std::string const & myRankInfo );
201+
172202
/**
173203
* @throw no_such_attribute_error If optional attribute is not present.
174204
* @return String representing the path to particle species, relative(!) to <CODE>basePath</CODE>.

src/Series.cpp

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*/
2121
#include "openPMD/auxiliary/Date.hpp"
2222
#include "openPMD/auxiliary/Filesystem.hpp"
23+
#include "openPMD/auxiliary/MPI.hpp"
2324
#include "openPMD/auxiliary/StringManip.hpp"
2425
#include "openPMD/IO/AbstractIOHandler.hpp"
2526
#include "openPMD/IO/AbstractIOHandlerHelper.hpp"
@@ -146,6 +147,91 @@ SeriesImpl::setMeshesPath(std::string const& mp)
146147
return *this;
147148
}
148149

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+
149235
std::string
150236
SeriesImpl::particlesPath() const
151237
{
@@ -1156,6 +1242,7 @@ SeriesInternal::SeriesInternal(
11561242
static_cast< internal::SeriesData * >( this ),
11571243
static_cast< internal::AttributableData * >( this ) }
11581244
{
1245+
m_communicator = comm;
11591246
auto input = parseInput( filepath );
11601247
auto handler =
11611248
createIOHandler( input->path, at, input->format, comm, options );
@@ -1322,4 +1409,4 @@ namespace
13221409
}
13231410
}
13241411
} // namespace [anonymous]
1325-
} // namespace openPMD
1412+
} // namespace openPMD

0 commit comments

Comments
 (0)