ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
DistributedSort.h File Reference

The sort of the items spread over the processes of MPI, beyond the book, compiled only with ADS_WITH_MPI (the CMake option of the same name, OFF by default) and an implementation of MPI (Open MPI or MPICH): each of the \( P \) processes of a communicator holds a vector of items, and after sort( comm , A , less ) each holds a sorted vector, those of the ranks \( 0 , \ldots , P - 1 \) in this order being the sorted items of all; the vectors change their sizes. More...

#include "ADS/Contract.h"
#include "ADS/parallel/Executor.h"
#include "ADS/parallel/ParallelMerge.h"
#include "ADS/parallel/ParallelSort.h"
#include "ADS/priority/MultiwayMerge.h"
#include "ADS/sort/Sort.h"
#include <mpi.h>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <limits>
#include <stdexcept>
#include <type_traits>
#include <utility>
#include <vector>

Go to the source code of this file.

Namespaces

namespace  ADS
namespace  ADS::distributed

Macros

#define OMPI_SKIP_MPICXX   1
#define MPICH_SKIP_MPICXX   1

Functions

template<Executor E, class T, class C = std::less<>>
void ADS::distributed::sort (E &ex, MPI_Comm comm, std::vector< T > &A, C less=C())
 the items of \( A \) of all the processes of comm sorted by less, the threads of ex sorting and merging within each process, after which \( A \) holds the part of the process, the parts in the order of the ranks; it throws std::length_error, in every process, if the bytes of a piece or of the pieces a process receives exceed the counts of MPI (those of int before MPI 4, whose MPI_Alltoallv_c counts them in 64 bits)
template<class T, class C = std::less<>>
void ADS::distributed::sort (MPI_Comm comm, std::vector< T > &A, C less=C())
 the items of \( A \) of all the processes of comm sorted by less as sort() above, each process on one thread

Detailed description

The sort of the items spread over the processes of MPI, beyond the book, compiled only with ADS_WITH_MPI (the CMake option of the same name, OFF by default) and an implementation of MPI (Open MPI or MPICH): each of the \( P \) processes of a communicator holds a vector of items, and after sort( comm , A , less ) each holds a sorted vector, those of the ranks \( 0 , \ldots , P - 1 \) in this order being the sorted items of all; the vectors change their sizes.

The items are copied as bytes (trivially copyable). An executor [see Executor.h] may be given as well, and then the work inside each process runs on its threads (MPI between the nodes, threads within each, the hybrid scheme of the clusters of today).

The sort is the parallel sort by regular sampling of Shi and Schaeffer, in one round of communication: each process sorts its \( n_i \) items (by sort() of ADS, or its parallel version on the executor), takes \( P - 1 \) samples at the ranks \( \lfloor ( j + 1 ) n_i / P \rfloor \), and all the \( P ( P - 1 ) \) samples, gathered by MPI_Allgather and sorted, give the \( P - 1 \) splitters at the ranks \( ( j + 1 ) ( P - 1 ) \); each process cuts its sorted items at the splitters by binary searches, the piece \( j \) taking its items strictly between the splitters \( j \) and \( j + 1 \) (the items equal to a run of \( k \) equal splitters are cut into \( k + 1 \) parts whose sizes differ by at most one, the lowest part the smallest and the highest the largest, so that a key repeated many times is spread over as many processes as it fills and a single item equal to a splitter goes to the piece above it), sends each piece to its process by one MPI_Alltoallv (after an MPI_Alltoall of the counts), and merges the \( P \) sorted pieces it receives by multiwayMerge() [see MultiwayMerge.h and ParallelMerge.h]. With the same number of items in each process, no process receives more than \( 2 n / P \) items (the bound of regular sampling, for distinct keys); the work is \( O( ( n / P ) \log n ) \) per process, the volume of communication \( O( n / P + P^2 ) \) per process in 3 collective operations, i.e., \( O( \alpha P + \beta n / P ) \) time for latency \( \alpha \) and time \( \beta \) per byte with a direct all-to-all. Beyond some thousands of processes the \( P^2 \) samples and the \( P \) messages of each process dominate, and the multi-level sorts of Axtmann, Bingmann, Sanders and Schulz (which group the processes and exchange in \( O( \log P ) \) levels) take over.

The contracts [see Contract.h]: T is trivially copyable; less is a strict weak order, the same in every process; every process of comm calls sort() (a collective operation); the local result is sorted (an audit).

See also
H. Shi, J. Schaeffer, Parallel sorting by regular sampling, Journal of Parallel and Distributed Computing 14(4), 361-372, 1992, doi:10.1016/0743-7315(92)90075-X
M. Axtmann, T. Bingmann, P. Sanders, C. Schulz, Practical massively parallel sorting, ACM Symposium on Parallelism in Algorithms and Architectures (SPAA), 13-23, 2015, doi:10.1145/2755573.2755595
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa

Macro definition documentation

◆ MPICH_SKIP_MPICXX

#define MPICH_SKIP_MPICXX   1

◆ OMPI_SKIP_MPICXX

#define OMPI_SKIP_MPICXX   1