222void sort( E & ex , MPI_Comm comm , std::vector< T > & A , C less )
224 static_assert( std::is_trivially_copyable_v< T > ,
225 "the items travel as bytes" );
228 check( MPI_Comm_size( comm , &P ) ,
"MPI_Comm_size" );
229 check( MPI_Comm_rank( comm , &me ) ,
"MPI_Comm_rank" );
233 const std::size_t n = A.size() , p = std::size_t( P );
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 ,
243 std::vector< T > all( p * ( p - 1 ) );
245 const int bytes = detail::fits< int >( ( p - 1 ) *
sizeof( T ) , ok );
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 ,
253 for( std::size_t q = 0 ; q < p ; ++q )
255 S.insert( S.end() , all.begin() + std::ptrdiff_t( q * ( p - 1 ) ) ,
256 all.begin() + std::ptrdiff_t( ( q + 1 ) * ( p - 1 ) ) );
265 std::vector< std::size_t > cut( p + 1 , 0 );
267 for( std::size_t j = 1 ; j < p ; ++j ) {
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() ) ,
277 std::upper_bound( A.begin() , A.end() , s ,
278 less ) - A.begin() );
280 std::size_t first = j , last = j + 1;
283 ! less( S[ std::min( S.size() - 1 , ( first - 1 ) * S.size() / p ) ] ,
286 while( ( last < p ) &&
287 ! less( s , S[ std::min( S.size() - 1 , last * S.size() / p ) ] ) )
290 cut[ j ] = lo + ( hi - lo ) * ( j - first + 1 ) / ( last - first + 1 );
292 for( std::size_t j = 1 ; j <= p ; ++j )
293 cut[ j ] = std::max( cut[ j ] , cut[ j - 1 ] );
295 using detail::Displacement;
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 ) ,
301 check( MPI_Alltoall( counts.data() , 1 , detail::countType() , got.data() ,
303 detail::countType() , comm ) ,
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 ] );
315 check( MPI_Allreduce( MPI_IN_PLACE , &fit , 1 , MPI_INT , MPI_MIN , comm ) ,
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 ,
327 check( MPI_Alltoallv( A.data() , counts.data() , sd.data() , MPI_BYTE ,
329 got.data() , rd.data() , MPI_BYTE , comm ) ,
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 ) ,
337 ( std::size_t( rd[ j ] ) + std::size_t( got[ j ] ) ) /
339 A.resize( R.size() );
341 ADS_AUDIT( std::is_sorted( A.begin() , A.end() , less ) );