Skip to content

Commit c68ff97

Browse files
committed
Add hostname attribute to Series
1 parent bdf1035 commit c68ff97

2 files changed

Lines changed: 124 additions & 0 deletions

File tree

include/openPMD/Series.hpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ namespace internal
6666
*/
6767
class SeriesData : public AttributableData
6868
{
69+
// friend class openPMD::SeriesImpl;
70+
// friend class openPMD::Iteration;
71+
// friend class openPMD::Series;
72+
// friend class SeriesInternal;
73+
6974
public:
7075
explicit SeriesData() = default;
7176

@@ -96,6 +101,12 @@ class SeriesData : public AttributableData
96101
*/
97102
StepStatus m_stepStatus = StepStatus::NoStep;
98103
bool m_parseLazily = false;
104+
#if openPMD_HAVE_MPI
105+
/*
106+
* @todo find a better place to put MPI-specific members
107+
*/
108+
MPI_Comm m_communicator;
109+
#endif
99110
}; // SeriesData
100111

101112
class SeriesInternal;
@@ -167,6 +178,32 @@ class SeriesImpl : public AttributableImpl
167178
*/
168179
SeriesImpl& setMeshesPath(std::string const& meshesPath);
169180

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

src/Series.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "openPMD/auxiliary/Date.hpp"
2222
#include "openPMD/auxiliary/Filesystem.hpp"
2323
#include "openPMD/auxiliary/JSON.hpp"
24+
#include "openPMD/auxiliary/MPI.hpp"
2425
#include "openPMD/auxiliary/StringManip.hpp"
2526
#include "openPMD/IO/AbstractIOHandler.hpp"
2627
#include "openPMD/IO/AbstractIOHandlerHelper.hpp"
@@ -162,6 +163,91 @@ SeriesImpl::setMeshesPath(std::string const& mp)
162163
return *this;
163164
}
164165

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+
165251
std::string
166252
SeriesImpl::particlesPath() const
167253
{
@@ -1403,6 +1489,7 @@ SeriesInternal::SeriesInternal(
14031489
{
14041490
nlohmann::json optionsJson = auxiliary::parseOptions( options, comm );
14051491
parseJsonOptions( *this, optionsJson );
1492+
m_communicator = comm;
14061493
auto input = parseInput( filepath );
14071494
auto handler = createIOHandler(
14081495
input->path, at, input->format, comm, std::move( optionsJson ) );

0 commit comments

Comments
 (0)