Skip to content

Commit 725bbee

Browse files
committed
Python bindings
Also: use distribution strategies in openpmd-pipe
1 parent a1bb1e0 commit 725bbee

12 files changed

Lines changed: 342 additions & 194 deletions

File tree

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,12 +1170,12 @@ if(openPMD_BUILD_TESTING)
11701170
)
11711171
add_test(NAME CLI.pipe.py
11721172
COMMAND sh -c
1173-
"${MPI_TEST_EXE} ${Python_EXECUTABLE} \
1173+
"${MPI_TEST_EXE} -n 2 ${Python_EXECUTABLE} \
11741174
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/openpmd-pipe \
11751175
--infile ../samples/git-sample/data%T.h5 \
11761176
--outfile ../samples/git-sample/data%T.bp && \
11771177
\
1178-
${MPI_TEST_EXE} ${Python_EXECUTABLE} \
1178+
${MPI_TEST_EXE} -n 2 ${Python_EXECUTABLE} \
11791179
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/openpmd-pipe \
11801180
--infile ../samples/git-sample/thetaMode/data%T.h5 \
11811181
--outfile ../samples/git-sample/thetaMode/data%T.bp && \

include/openPMD/ChunkInfo.hpp

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@
2020
*/
2121
#pragma once
2222

23-
#include "openPMD/benchmark/mpi/BlockSlicer.hpp"
23+
#include "openPMD/config.hpp"
24+
2425
#include "openPMD/Dataset.hpp" // Offset, Extent
26+
#include "openPMD/benchmark/mpi/BlockSlicer.hpp"
2527

28+
#if openPMD_HAVE_MPI
29+
#include <mpi.h>
30+
#endif
2631
#include <vector>
2732

28-
2933
namespace openPMD
3034
{
3135
/**
@@ -115,12 +119,13 @@ namespace chunk_assignment
115119
* @param out Meta information on reading processes, e.g. hostnames.
116120
* @return ChunkTable A table that assigns chunks to reading processes.
117121
*/
118-
virtual ChunkTable
119-
assign(
122+
virtual ChunkTable assign(
120123
PartialAssignment partialAssignment,
121124
RankMeta const & in,
122125
RankMeta const & out ) = 0;
123126

127+
virtual std::unique_ptr< Strategy > clone() const = 0;
128+
124129
virtual ~Strategy() = default;
125130
};
126131

@@ -149,12 +154,13 @@ namespace chunk_assignment
149154
* that were not assigned and one that assigns chunks to
150155
* reading processes.
151156
*/
152-
virtual PartialAssignment
153-
assign(
157+
virtual PartialAssignment assign(
154158
PartialAssignment partialAssignment,
155159
RankMeta const & in,
156160
RankMeta const & out ) = 0;
157161

162+
virtual std::unique_ptr< PartialStrategy > clone() const = 0;
163+
158164
virtual ~PartialStrategy() = default;
159165
};
160166

@@ -192,6 +198,12 @@ namespace chunk_assignment
192198
virtual ChunkTable
193199
assign( PartialAssignment, RankMeta const & in, RankMeta const & out );
194200

201+
virtual std::unique_ptr< Strategy > clone() const override
202+
{
203+
return std::unique_ptr< Strategy >( new FromPartialStrategy(
204+
m_firstPass->clone(), m_secondPass->clone() ) );
205+
}
206+
195207
private:
196208
std::unique_ptr< PartialStrategy > m_firstPass;
197209
std::unique_ptr< Strategy > m_secondPass;
@@ -206,6 +218,11 @@ namespace chunk_assignment
206218
{
207219
ChunkTable
208220
assign( PartialAssignment, RankMeta const & in, RankMeta const & out );
221+
222+
virtual std::unique_ptr< Strategy > clone() const override
223+
{
224+
return std::unique_ptr< Strategy >( new RoundRobin );
225+
}
209226
};
210227

211228
/**
@@ -224,6 +241,12 @@ namespace chunk_assignment
224241
assign( PartialAssignment, RankMeta const & in, RankMeta const & out )
225242
override;
226243

244+
virtual std::unique_ptr< PartialStrategy > clone() const override
245+
{
246+
return std::unique_ptr< PartialStrategy >(
247+
new ByHostname( m_withinNode->clone() ) );
248+
}
249+
227250
private:
228251
std::unique_ptr< Strategy > m_withinNode;
229252
};
@@ -246,9 +269,16 @@ namespace chunk_assignment
246269
unsigned int mpi_rank,
247270
unsigned int mpi_size );
248271

249-
ChunkTable
250-
assign( PartialAssignment, RankMeta const & in, RankMeta const & out )
251-
override;
272+
ChunkTable assign(
273+
PartialAssignment,
274+
RankMeta const & in,
275+
RankMeta const & out ) override;
276+
277+
virtual std::unique_ptr< Strategy > clone() const override
278+
{
279+
return std::unique_ptr< Strategy >( new ByCuboidSlice(
280+
blockSlicer->clone(), totalExtent, mpi_rank, mpi_size ) );
281+
}
252282

253283
private:
254284
std::unique_ptr< BlockSlicer > blockSlicer;
@@ -278,32 +308,32 @@ namespace chunk_assignment
278308
*/
279309
BinPacking( size_t splitAlongDimension = 0 );
280310

281-
ChunkTable
282-
assign( PartialAssignment, RankMeta const & in, RankMeta const & out )
283-
override;
284-
};
311+
ChunkTable assign(
312+
PartialAssignment,
313+
RankMeta const & in,
314+
RankMeta const & out ) override;
285315

286-
/**
287-
* C++11 doesn't have it and it's useful for some of these.
288-
*/
289-
template< typename T, typename... Args >
290-
std::unique_ptr< T >
291-
make_unique( Args &&... args )
292-
{
293-
return std::unique_ptr< T >( new T( std::forward< Args >( args )... ) );
294-
}
295-
} // namespace chunk_assignment
316+
virtual std::unique_ptr< Strategy > clone() const override
317+
{
318+
return std::unique_ptr< Strategy >(
319+
new BinPacking( splitAlongDimension ) );
320+
}
321+
};
322+
} // namespace chunk_assignment
296323

297324
namespace host_info
298325
{
299-
enum class Method
300-
{
301-
HOSTNAME
302-
};
326+
enum class Method
327+
{
328+
HOSTNAME
329+
};
330+
331+
std::string byMethod( Method );
303332

304-
std::string byMethod( Method );
333+
#if openPMD_HAVE_MPI
334+
chunk_assignment::RankMeta byMethodCollective( MPI_Comm, Method );
335+
#endif
305336

306-
std::string
307-
hostname();
337+
std::string hostname();
308338
} // namespace host_info
309339
} // namespace openPMD

include/openPMD/benchmark/mpi/BlockSlicer.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
#include "openPMD/Dataset.hpp"
2525

26+
#include <memory>
27+
2628

2729
namespace openPMD
2830
{
@@ -49,6 +51,8 @@ namespace openPMD
4951
int rank
5052
) = 0;
5153

54+
virtual std::unique_ptr< BlockSlicer > clone() const = 0;
55+
5256
/** This class will be derived from
5357
*/
5458
virtual ~BlockSlicer() = default;

include/openPMD/benchmark/mpi/OneDimensionalBlockSlicer.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,7 @@ namespace openPMD
4343
int size,
4444
int rank
4545
) override;
46+
47+
virtual std::unique_ptr< BlockSlicer > clone() const override;
4648
};
4749
}

src/ChunkInfo.cpp

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
*/
2121
#include "openPMD/ChunkInfo.hpp"
2222

23-
#include <utility>
24-
23+
#include "openPMD/auxiliary/MPI.hpp"
2524

2625
#include <algorithm> // std::sort
2726
#include <iostream>
2827
#include <list>
2928
#include <map>
3029
#include <unistd.h>
30+
#include <utility>
3131

3232
namespace openPMD
3333
{
@@ -100,6 +100,11 @@ namespace chunk_assignment
100100
RankMeta const & rankOut,
101101
Strategy & strategy )
102102
{
103+
if( rankOut.size() == 0 )
104+
{
105+
throw std::runtime_error(
106+
"[assignChunks] No output ranks defined" );
107+
}
103108
return strategy.assign(
104109
PartialAssignment( std::move( table ) ), rankIn, rankOut );
105110
}
@@ -517,23 +522,36 @@ namespace chunk_assignment
517522

518523
namespace host_info
519524
{
520-
constexpr size_t MAX_HOSTNAME_LENGTH = 200;
525+
constexpr size_t MAX_HOSTNAME_LENGTH = 200;
521526

522-
std::string
523-
byMethod( Method method )
524-
{
525-
static std::map< Method, std::string ( * )() > map{ { Method::HOSTNAME,
526-
&hostname } };
527-
return ( *map[ method ] )();
528-
}
527+
std::string byMethod( Method method )
528+
{
529+
static std::map< Method, std::string ( * )() > map{
530+
{ Method::HOSTNAME, &hostname } };
531+
return ( *map[ method ] )();
532+
}
529533

530-
std::string
531-
hostname()
534+
#if openPMD_HAVE_MPI
535+
chunk_assignment::RankMeta byMethodCollective( MPI_Comm comm, Method method )
536+
{
537+
auto myHostname = byMethod( method );
538+
chunk_assignment::RankMeta res;
539+
auto allHostnames =
540+
auxiliary::distributeStringsToAllRanks( comm, myHostname );
541+
for( size_t i = 0; i < allHostnames.size(); ++i )
532542
{
533-
char hostname[ MAX_HOSTNAME_LENGTH ];
534-
gethostname( hostname, MAX_HOSTNAME_LENGTH );
535-
std::string res( hostname );
536-
return res;
543+
res[ i ] = allHostnames[ i ];
537544
}
545+
return res;
546+
}
547+
#endif
548+
549+
std::string hostname()
550+
{
551+
char hostname[ MAX_HOSTNAME_LENGTH ];
552+
gethostname( hostname, MAX_HOSTNAME_LENGTH );
553+
std::string res( hostname );
554+
return res;
555+
}
538556
} // namespace host_info
539557
} // namespace openPMD

src/Series.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,19 +166,17 @@ SeriesImpl::setMeshesPath(std::string const& mp)
166166
chunk_assignment::RankMeta
167167
SeriesImpl::mpiRanksMetaInfo() const
168168
{
169+
auto attribute = getAttribute( "rankMetaInfo" );
169170
std::vector< std::string > asContiguousVector;
170171
try
171172
{
172-
asContiguousVector =
173-
getAttribute( "rankMetaInfo" ).get< std::vector< std::string > >();
173+
asContiguousVector = attribute.get< std::vector< std::string > >();
174174
}
175175
catch( std::runtime_error const & )
176176
{
177177
// workaround: if vector has length 1, some backends may report a
178178
// single value instead of a vector
179-
asContiguousVector = {
180-
getAttribute( "rankMetaInfo" ).get< std::string >()
181-
};
179+
asContiguousVector = { attribute.get< std::string >() };
182180
}
183181
chunk_assignment::RankMeta res;
184182
for( size_t i = 0; i < asContiguousVector.size(); ++i )

src/benchmark/mpi/OneDimensionalBlockSlicer.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,10 @@ namespace openPMD
9393
std::move( localExtent )
9494
);
9595
}
96+
97+
std::unique_ptr< BlockSlicer > OneDimensionalBlockSlicer::clone() const
98+
{
99+
return std::unique_ptr< BlockSlicer >(
100+
new OneDimensionalBlockSlicer( m_dim ) );
101+
}
96102
}

0 commit comments

Comments
 (0)