ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
DistributedSort.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------- File DistributedSort.h -------------------------*/
3/*--------------------------------------------------------------------------*/
62/*--------------------------------------------------------------------------*/
63/*----------------------------- DEFINITIONS --------------------------------*/
64/*--------------------------------------------------------------------------*/
65
66#ifndef __ADS_DistributedSort
67 #define __ADS_DistributedSort
68/* self-identification: #endif constant */
69
70#if defined( ADS_WITH_MPI )
71
72/*--------------------------------------------------------------------------*/
73/*------------------------------ INCLUDES ----------------------------------*/
74/*--------------------------------------------------------------------------*/
75
76#include "ADS/Contract.h"
81#include "ADS/sort/Sort.h"
82
83// the C interface of MPI only: the bindings of C++, removed from the
84// standard, do not compile cleanly
85#ifndef OMPI_SKIP_MPICXX
86 #define OMPI_SKIP_MPICXX 1
87#endif
88#ifndef MPICH_SKIP_MPICXX
89 #define MPICH_SKIP_MPICXX 1
90#endif
91#include <mpi.h>
92
93#include <algorithm>
94#include <cstddef>
95#include <functional>
96#include <limits>
97#include <stdexcept>
98#include <type_traits>
99#include <utility>
100#include <vector>
101
102/*--------------------------------------------------------------------------*/
103/*--------------------------- NAMESPACE ------------------------------------*/
104/*--------------------------------------------------------------------------*/
105
107{
108
153template< Executor E , class T , class C = std::less<> >
154void sort( E & ex , MPI_Comm comm , std::vector< T > & A , C less = C() );
155
167template< class T , class C = std::less<> >
168void sort( MPI_Comm comm , std::vector< T > & A , C less = C() )
169{
170 Sequential s;
171 sort( s , comm , A , less );
172 }
173
174/*--------------------------------------------------------------------------*/
175
176namespace detail
177{
178
180inline void check( int code , const char * what )
181{
182 if( code != MPI_SUCCESS )
183 throw(
184 std::runtime_error( std::string( "ADS::distributed::sort: " ) + what ) );
185 }
186
187// the counts and the displacements of the bytes in MPI_Alltoallv: those of
188// MPI_Alltoallv_c, of 64 bits, with MPI 4, else those of int
189#if defined( MPI_VERSION ) && ( MPI_VERSION >= 4 )
190 #define ADS_MPI_LARGE_COUNTS
191using Count = MPI_Count;
192using Displacement = MPI_Aint;
193#else
194using Count = int;
195using Displacement = int;
196#endif
197
199inline MPI_Datatype countType( void )
200{
201#ifdef ADS_MPI_LARGE_COUNTS
202 return( MPI_COUNT );
203#else
204 return( MPI_INT );
205#endif
206 }
207
210template< class N >
211N fits( std::size_t b , bool & ok )
212{
213 if( b <= std::size_t( std::numeric_limits< N >::max() ) )
214 return( N( b ) );
215 ok = false;
216 return( N( 0 ) );
217 }
218
219} // end( namespace detail )
220
221template< Executor E , class T , class C >
222void sort( E & ex , MPI_Comm comm , std::vector< T > & A , C less )
223{
224 static_assert( std::is_trivially_copyable_v< T > ,
225 "the items travel as bytes" );
226 using detail::check;
227 int P = 1 , me = 0;
228 check( MPI_Comm_size( comm , &P ) , "MPI_Comm_size" );
229 check( MPI_Comm_rank( comm , &me ) , "MPI_Comm_rank" );
230 ADS::sort( ex , A , less );
231 if( P == 1 )
232 return;
233 const std::size_t n = A.size() , p = std::size_t( P );
234 // P - 1 regular samples of each process, and a flag for an empty one
235 std::vector< T > mine( p - 1 );
236 std::vector< int > has( p );
237 const int full = n > 0;
238 for( std::size_t j = 0 ; j + 1 < p && n ; ++j )
239 mine[ j ] = A[ ( j + 1 ) * n / p ];
240 check( MPI_Allgather( &full , 1 , MPI_INT , has.data() , 1 , MPI_INT ,
241 comm ) ,
242 "MPI_Allgather" );
243 std::vector< T > all( p * ( p - 1 ) );
244 bool ok = true; // all the bytes fit in MPI
245 const int bytes = detail::fits< int >( ( p - 1 ) * sizeof( T ) , ok );
246 if( ! ok ) // the same in every process
247 throw( std::length_error( "ADS::distributed::sort: samples of more "
248 "bytes than MPI counts" ) );
249 check( MPI_Allgather( mine.data() , bytes , MPI_BYTE , all.data() , bytes ,
250 MPI_BYTE , comm ) ,
251 "MPI_Allgather" );
252 std::vector< T > S; // the samples of the full ones
253 for( std::size_t q = 0 ; q < p ; ++q )
254 if( has[ q ] )
255 S.insert( S.end() , all.begin() + std::ptrdiff_t( q * ( p - 1 ) ) ,
256 all.begin() + std::ptrdiff_t( ( q + 1 ) * ( p - 1 ) ) );
257 ADS::sort( S , less );
258 // the cuts: piece j of this process holds its items strictly between
259 // the splitters j and j + 1 (none below the first piece, none above the
260 // last), and the items equal to a run of k equal splitters are cut into
261 // k + 1 parts by the floor of their size times 1 / ( k + 1 ), 2 / ( k + 1 ),
262 // ..., so that the sizes of the parts differ by at most one (the lowest
263 // the smallest, the highest the largest), and a single item equal to a
264 // splitter goes to the piece above it
265 std::vector< std::size_t > cut( p + 1 , 0 );
266 cut[ p ] = n;
267 for( std::size_t j = 1 ; j < p ; ++j ) {
268 if( S.empty() ) {
269 cut[ j ] = n;
270 continue;
271 }
272 const T & s = S[ std::min( S.size() - 1 , j * S.size() / p ) ];
273 const auto lo = std::size_t(
274 std::lower_bound( A.begin() , A.end() , s ,
275 less ) - A.begin() ) ,
276 hi = std::size_t(
277 std::upper_bound( A.begin() , A.end() , s ,
278 less ) - A.begin() );
279 // the splitters equal to s: [ first , last ) of the indices j
280 std::size_t first = j , last = j + 1;
281 while(
282 ( first > 1 ) &&
283 ! less( S[ std::min( S.size() - 1 , ( first - 1 ) * S.size() / p ) ] ,
284 s ) )
285 --first;
286 while( ( last < p ) &&
287 ! less( s , S[ std::min( S.size() - 1 , last * S.size() / p ) ] ) )
288 ++last;
289 // the equal items [ lo , hi ) shared among the cuts first .. last - 1
290 cut[ j ] = lo + ( hi - lo ) * ( j - first + 1 ) / ( last - first + 1 );
291 }
292 for( std::size_t j = 1 ; j <= p ; ++j )
293 cut[ j ] = std::max( cut[ j ] , cut[ j - 1 ] );
294 using detail::Count;
295 using detail::Displacement;
296 using detail::fits;
297 std::vector< Count > counts( p ) , got( p );
298 for( std::size_t j = 0 ; j < p ; ++j )
299 counts[ j ] = fits< Count >( ( cut[ j + 1 ] - cut[ j ] ) * sizeof( T ) ,
300 ok );
301 check( MPI_Alltoall( counts.data() , 1 , detail::countType() , got.data() ,
302 1 ,
303 detail::countType() , comm ) ,
304 "MPI_Alltoall" );
305 std::vector< Displacement > sd( p , 0 ) , rd( p , 0 );
306 std::size_t total = 0;
307 for( std::size_t j = 0 ; j < p ; ++j ) {
308 sd[ j ] = fits< Displacement >( cut[ j ] * sizeof( T ) , ok );
309 rd[ j ] = fits< Displacement >( total , ok );
310 total += std::size_t( got[ j ] );
311 }
312 // a process whose bytes do not fit stops all of them, which would
313 // otherwise wait for it in the exchange
314 int fit = ok;
315 check( MPI_Allreduce( MPI_IN_PLACE , &fit , 1 , MPI_INT , MPI_MIN , comm ) ,
316 "MPI_Allreduce" );
317 if( ! fit )
318 throw( std::length_error( "ADS::distributed::sort: pieces of more bytes "
319 "than MPI counts" ) );
320 std::vector< T > R( total / sizeof( T ) );
321#ifdef ADS_MPI_LARGE_COUNTS
322 check( MPI_Alltoallv_c( A.data() , counts.data() , sd.data() , MPI_BYTE ,
323 R.data() , got.data() , rd.data() , MPI_BYTE ,
324 comm ) ,
325 "MPI_Alltoallv_c" );
326#else
327 check( MPI_Alltoallv( A.data() , counts.data() , sd.data() , MPI_BYTE ,
328 R.data() ,
329 got.data() , rd.data() , MPI_BYTE , comm ) ,
330 "MPI_Alltoallv" );
331#endif
332 // the P sorted pieces received, merged
333 std::vector< std::pair< const T * , const T * > > runs;
334 for( std::size_t j = 0 ; j < p ; ++j )
335 runs.emplace_back( R.data() + std::size_t( rd[ j ] ) / sizeof( T ) ,
336 R.data() +
337 ( std::size_t( rd[ j ] ) + std::size_t( got[ j ] ) ) /
338 sizeof( T ) );
339 A.resize( R.size() );
340 ADS::multiwayMerge( ex , runs , A.begin() , less );
341 ADS_AUDIT( std::is_sorted( A.begin() , A.end() , less ) );
342 }
343
344/*--------------------------------------------------------------------------*/
345
346} // end( namespace ADS::distributed )
347
348#endif /* ADS_WITH_MPI */
349
350/*--------------------------------------------------------------------------*/
351
352#endif /* DistributedSort.h included */
353
354/*--------------------------------------------------------------------------*/
355/*---------------------- End File DistributedSort.h ------------------------*/
356/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The layer of execution of the parallel algorithms of ADS, beyond the book: an executor runs the calls...
The engineered merge of sorted sequences, beyond the book [see LoserTree.h for the tree of losers of...
The parallel merge of sorted ranges, beyond the book: multiwayMerge( ex , R , out ,...
The parallel sorts, beyond the book: sort( ex , A , less ) and stableSort( ex , A ,...
The sort that chooses its algorithm, beyond the book: sort() and stableSort() look at what they sort ...
the executor of the calling thread alone, which makes the calls in their order
Definition Executor.h:265
#define ADS_AUDIT(cond)
a precondition or a postcondition dearer than the operation, checked from level 3
Definition Contract.h:131
bool check(const Grid &S, std::size_t x, std::size_t y, int c)
whether can be written in the square of , i.e., it is not on its row, its column or its block
void sort(E &ex, MPI_Comm comm, std::vector< T > &A, C less=C())
the items of of all the processes of comm sorted by less, the threads of ex sorting and merging with...
Definition DistributedSort.h:222
O multiwayMerge(E &ex, const std::vector< std::pair< I, I > > &R, O out, C less=C())
the items of the sorted ranges merged into out on the threads of ex, the range of lower index first ...
void sort(E &ex, Store &&A, C less=C())
the vector sorted by less on the threads of ex, not necessarily stable
Definition DistributedSort.h:107