|
ADS
Algorithms and Data Structures
|
The parallel merge of \( k \) sorted ranges, beyond the book. More...
Files | |
| file | ADS/parallel/ParallelMerge.h |
| The parallel merge of \( k \) sorted ranges, beyond the book: multiwayMerge( ex , R , out , less ) writes into out what multiwayMerge( R , out , less ) [see MultiwayMerge.h] writes, the same items in the same order (on equal items the range of lower index first), on the threads of the executor ex [see Executor.h]. | |
Functions | |
| template<std::random_access_iterator I, class C = std::less<>> | |
| std::vector< std::size_t > | ADS::multisequenceSplit (const std::vector< std::pair< I, I > > &R, std::size_t r, C less=C()) |
| the split of the sorted ranges \( R_1 , \ldots , R_k \) at the rank \( r \) of the stable merge, i.e., the number of items that each range gives to the \( r \) least | |
| template<Executor E, std::random_access_iterator I, std::random_access_iterator O, class C = std::less<>> | |
| O | ADS::multiwayMerge (E &ex, const std::vector< std::pair< I, I > > &R, O out, C less=C()) |
| the items of the sorted ranges \( R_1 , \ldots , R_k \) merged into out on the threads of ex, the range of lower index first on equal items; the end of the output | |
The parallel merge of \( k \) sorted ranges, beyond the book.
The definitions, the invariants and the costs of the area are in Theory of parallelism.
|
nodiscard |
the split of the sorted ranges \( R_1 , \ldots , R_k \) at the rank \( r \) of the stable merge, i.e., the number of items that each range gives to the \( r \) least
In pseudocode (the multisequence selection of Varman et al., beyond the book), \( R_i[ 0 .. |R_i| - 1 ] \) and the items ordered by \( ( \mathit{item} , \mathit{range} , \mathit{position} ) \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{multisequenceSplit}( R_1 , \ldots , R_k , r ) \cr 2 & \quad \textbf{foreach}\ i\ \textbf{do}\ \mathit{lo}_i \gets 0; \ \mathit{hi}_i \gets |R_i| \cr 3 & \quad \textbf{loop} \cr 4 & \qquad C \gets \{ i : \mathit{lo}_i < \mathit{hi}_i \};\ \textbf{if} \ C = \emptyset\ \textbf{then return}\ \mathit{lo} \cr 5 & \qquad \textbf{foreach}\ i \in C\ \textbf{do}\ m_i \gets \mathit{lo}_i + \lfloor ( \mathit{hi}_i - \mathit{lo}_i ) / 2 \rfloor \cr 6 & \qquad j \gets \text{the median of } C \text{ by } ( R_i[ m_i ] , i ) \text{, weighted by } \mathit{hi}_i - \mathit{lo}_i \cr 7 & \qquad x \gets R_j[ m_j ];\ c_j \gets m_j \cr 8 & \qquad \textbf{foreach}\ i < j\ \textbf{do}\ c_i \gets \text{the } \text{first place of } [ \mathit{lo}_i , \mathit{hi}_i ) \text{ with } R_i[ c_i ] > x \cr 9 & \qquad \textbf{foreach}\ i > j\ \textbf{do}\ c_i \gets \text{the } \text{first place of } [ \mathit{lo}_i , \mathit{hi}_i ) \text{ with } R_i[ c_i ] \ge x \cr 10 & \qquad \textbf{if}\ \sum_i c_i < r\ \textbf{then}\ \mathit{lo} \gets c;\ \mathit{lo}_j \gets m_j + 1\quad \triangleright\ x \text{ to the } \text{left} \cr 11 & \qquad \textbf{else}\ \mathit{hi} \gets c;\ \mathit{hi}_j \gets m_j \end{array}} \]
| O ADS::multiwayMerge | ( | E & | ex, |
| const std::vector< std::pair< I, I > > & | R, | ||
| O | out, | ||
| C | less = C() ) |
the items of the sorted ranges \( R_1 , \ldots , R_k \) merged into out on the threads of ex, the range of lower index first on equal items; the end of the output
In pseudocode (the parallel merge of Varman et al., beyond the book), on \( p \) threads:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{multiwayMerge}( \mathit{ex} , R_1 , \ldots , R_k , \mathit{out} ) \cr 2 & \quad N \gets \sum_i |R_i| \cr 3 & \quad \textbf{if}\ p = 1\ \textbf{or}\ k < 2\ \textbf{or}\ N < 2^{16}\ \textbf{then return}\ \mathrm{multiwayMerge}( R_1 , \ldots , R_k , \mathit{out} ) \cr 4 & \quad B \gets \max\{ 2 , \min\{ 4 p , \lfloor N / 2^{14} \rfloor \} \} \cr 5 & \quad \textbf{for}\ q \gets 0\ \textbf{to}\ B \ \textbf{do in parallel}\ S[ q ] \gets \mathrm{multisequenceSplit}( R_1 , \ldots , R_k , \lfloor N q / B \rfloor ) \cr 6 & \quad \textbf{for}\ q \gets 0\ \textbf{to}\ B - 1 \ \textbf{do in parallel} \cr 7 & \qquad \mathrm{multiwayMerge}( \text{the pieces } R_i[ S[ q ]_i .. S[ q + 1 ]_i - 1 ] \text{ of each } i , \mathit{out} + \lfloor N q / B \rfloor ) \cr 8 & \quad \textbf{return}\ \mathit{out} + N \end{array}} \]