|
ADS
Algorithms and Data Structures
|
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). More...
Files | |
| file | ADS/distributed/DistributedSort.h |
| 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. | |
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 | |
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).
The definitions, the invariants and the costs of the area are in Theory of distributed algorithms.
| 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)
In pseudocode (the sort by regular sampling of Shi and Schaeffer, beyond the book), run by each of the \( P \) processes:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{sort}( \mathit{ex} , \mathit{comm} , A ) \cr 2 & \quad \mathrm{sort}( \mathit{ex} , A );\ \textbf{if}\ P = 1 \ \textbf{then return} \cr 3 & \quad n \gets |A|;\ \textbf{for}\ j \gets 0\ \textbf{to}\ P - 2 \ \textbf{do}\ s[ j ] \gets A[ \lfloor ( j + 1 ) n / P \rfloor ] \cr 4 & \quad S \gets \text{the samples } s \text{ of the processes with } n > 0\quad \triangleright\ \mathrm{MPI\_Allgather} \cr 5 & \quad \mathrm{sort}( S );\ \mathit{cut}[ 0 ] \gets 0;\ \mathit{cut}[ P ] \gets n \cr 6 & \quad \textbf{for}\ j \gets 1\ \textbf{to}\ P - 1\ \textbf{do} \cr 7 & \qquad x \gets S[ \min\{ |S| - 1 , \lfloor j |S| / P \rfloor \} ] \quad \triangleright\ \text{the splitter } j \cr 8 & \qquad [ \mathit{lo} , \mathit{hi} ) \gets \text{the places of the } \text{items of } A \text{ equal to } x \text{, by binary searches} \cr 9 & \qquad [ f , l ) \gets \text{the indices of the splitters equal to } x \cr 10 & \qquad \mathit{cut}[ j ] \gets \mathit{lo} + \lfloor ( \mathit{hi} - \mathit{lo} ) ( j - f + 1 ) / ( l - f + 1 ) \rfloor \cr 11 & \quad \textbf{for}\ j \gets 1\ \textbf{to}\ P\ \textbf{do} \ \mathit{cut}[ j ] \gets \max\{ \mathit{cut}[ j ] , \mathit{cut}[ j - 1 ] \}\quad \triangleright\ \text{a safeguard} \cr 12 & \quad \text{send } A[ \mathit{cut}[ j ] .. \mathit{cut}[ j + 1 ] - 1 ] \text{ to the process } j \text{, for each } j\quad \triangleright \ \mathrm{MPI\_Alltoallv} \cr 13 & \quad A \gets \mathrm{multiwayMerge}( \mathit{ex} , \text{the } P \text{ pieces received} ) \end{array}} \]
| 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
In pseudocode (beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{sort}( \mathit{comm} , A ) \cr 2 & \quad \mathrm{sort}( \text{the executor } \mathrm{Sequential} , \mathit{comm} , A ) \end{array}} \]