|
ADS
Algorithms and Data Structures
|
The parallel sorts, beyond the book. More...
Files | |
| file | ADS/parallel/ParallelSort.h |
| The parallel sorts, beyond the book: sort( ex , A , less ) and stableSort( ex , A , less ) sort the vector \( A \) on the threads of the executor ex [see Executor.h], with the same result as sort( A , less ) and stableSort( A , less ) [see Sort.h], which they call on the parts they cut \( A \) into; with fewer than kParallelSortThreads threads, or fewer than kParallelSort items, they are those sequential sorts. | |
Functions | |
| template<Executor E, SortStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>> | |
| void | ADS::sort (E &ex, Store &&A, C less=C()) |
| the vector \( A \) sorted by less on the threads of ex, not necessarily stable | |
| template<Executor E, class T, class C = std::less<>> | |
| void | ADS::sort (E &ex, std::vector< T > &A, C less=C()) |
| sort() above on a vector | |
| template<Executor E, SortStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>> | |
| void | ADS::stableSort (E &ex, Store &&A, C less=C()) |
| the vector \( A \) sorted by less on the threads of ex, the equal items in their order | |
| template<Executor E, class T, class C = std::less<>> | |
| void | ADS::stableSort (E &ex, std::vector< T > &A, C less=C()) |
| stableSort() above on a vector | |
| template<Executor E, SortStore Store, VectorKey T = std::ranges::range_value_t< std::remove_cvref_t< Store > >> | |
| void | ADS::learnedSort (E &ex, Store &&A) |
| the vector \( A \) of numbers sorted in their natural order on the threads of ex, distributed by the model of the CDF of a sample [see LearnedSort.h] when it fits, as sort( ex , A ) otherwise and for the buckets; not necessarily stable | |
| template<Executor E, VectorKey T> | |
| void | ADS::learnedSort (E &ex, std::vector< T > &A) |
| learnedSort() above on a vector | |
| template<Executor E, SortStore Store, VectorKey T = std::ranges::range_value_t< std::remove_cvref_t< Store > >> | |
| void | ADS::learnedStableSort (E &ex, Store &&A) |
| the vector \( A \) of numbers sorted as learnedSort( ex , A ) sorts it, the equal items in their order (\( -0 \) and \( +0 \) among them), as stableSort( ex , A ) | |
| template<Executor E, VectorKey T> | |
| void | ADS::learnedStableSort (E &ex, std::vector< T > &A) |
| learnedStableSort() above on a vector | |
| template<Executor E, SortRange R, class... P> requires requires( E & ex , std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { sort( ex , v , std::forward< P > ( p )... ); } | |
| decltype(auto) | ADS::sort (E &ex, R &&A, P &&... p) |
| sort( ex , A ) on the range \( A \) [see SortRange], the other arguments as for a vector: in place on the span of its items if \( A \) is contiguous, through a vector of its items otherwise, since the code of the sort reads the items by their addresses | |
| template<Executor E, SortRange R, class... P> requires requires( E & ex , std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { stableSort( ex , v , std::forward < P >( p )... ); } | |
| decltype(auto) | ADS::stableSort (E &ex, R &&A, P &&... p) |
| stableSort( ex , A ) on the range \( A \) [see SortRange], the other arguments as for a vector: in place on the span of its items if \( A \) is contiguous, through a vector of its items otherwise, since the code of the sort reads the items by their addresses | |
| template<Executor E, SortRange R, class... P> requires requires( E & ex , std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { learnedSort( ex , v , std::forward < P >( p )... ); } | |
| decltype(auto) | ADS::learnedSort (E &ex, R &&A, P &&... p) |
| learnedSort( ex , A ) on the range \( A \) [see SortRange], the other arguments as for a vector: in place on the span of its items if \( A \) is contiguous, through a vector of its items otherwise, since the code of the sort reads the items by their addresses | |
| template<Executor E, SortRange R, class... P> requires requires( E & ex , std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { learnedStableSort( ex , v , std::forward< P >( p )... ); } | |
| decltype(auto) | ADS::learnedStableSort (E &ex, R &&A, P &&... p) |
| learnedStableSort( ex , A ) on the range \( A \) [see SortRange], the other arguments as for a vector: in place on the span of its items if \( A
\) is contiguous, through a vector of its items otherwise, since the code of the sort reads the items by their addresses | |
Variables | |
| constexpr std::size_t | ADS::kParallelSort = std::size_t( 1 ) << 15 |
| the items from which the parallel sorts use more than one thread, \( 2^{15} \) | |
| constexpr std::size_t | ADS::kParallelSortThreads = 3 |
| the threads from which the parallel sorts distribute: on 2 threads the passes of the distribution cost more than the second thread saves (on the benchmark, 0.6 to 1.5 times the time of the sequential sort, 0.8 in the median), and the sequential sort runs instead | |
The parallel sorts, beyond the book.
The definitions, the invariants and the costs of the area are in Theory of parallelism.
| decltype(auto) ADS::learnedSort | ( | E & | ex, |
| R && | A, | ||
| P &&... | p ) |
learnedSort( ex , A ) on the range \( A \) [see SortRange], the other arguments as for a vector: in place on the span of its items if \( A \) is contiguous, through a vector of its items otherwise, since the code of the sort reads the items by their addresses
| void ADS::learnedSort | ( | E & | ex, |
| std::vector< T > & | A ) |
learnedSort() above on a vector
| void ADS::learnedSort | ( | E & | ex, |
| Store && | A ) |
the vector \( A \) of numbers sorted in their natural order on the threads of ex, distributed by the model of the CDF of a sample [see LearnedSort.h] when it fits, as sort( ex , A ) otherwise and for the buckets; not necessarily stable
In pseudocode (the learned sort of Kristo et al., beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{learnedSort}( \mathit{ex} , A ) \cr 2 & \quad \textbf{if}\ p \ge 3\ \textbf{and}\ n \ge 2^{15}\ \textbf{then} \cr 3 & \qquad M \gets \text{the model of the CDF of a sample of 4096 keys, } \text{for 256 buckets} \cr 4 & \qquad \textbf{if}\ M \text{ fits the sample}\ \textbf{then} \ \mathrm{distribute}( \mathit{ex} , A , M );\ \textbf{return} \cr 5 & \quad \mathrm{sort}( \mathit{ex} , A ) \end{array}} \]
| decltype(auto) ADS::learnedStableSort | ( | E & | ex, |
| R && | A, | ||
| P &&... | p ) |
learnedStableSort( ex , A ) on the range \( A \) [see SortRange], the other arguments as for a vector: in place on the span of its items if \( A \) is contiguous, through a vector of its items otherwise, since the code of the sort reads the items by their addresses
| void ADS::learnedStableSort | ( | E & | ex, |
| std::vector< T > & | A ) |
learnedStableSort() above on a vector
| void ADS::learnedStableSort | ( | E & | ex, |
| Store && | A ) |
the vector \( A \) of numbers sorted as learnedSort( ex , A ) sorts it, the equal items in their order (\( -0 \) and \( +0 \) among them), as stableSort( ex , A )
In pseudocode: the procedure of learnedSort( ex , A ), with the stable distribution and stableSort( ex , A ) in place of sort( ex , A ).
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{learnedStableSort}( \mathit{ex} , A ) \cr 2 & \quad \mathrm{learnedSort}( \mathit{ex} , A )\quad \triangleright \ \text{with } \mathrm{stableSort} \text{ for } \mathrm{sort} \end{array}} \]
| decltype(auto) ADS::sort | ( | E & | ex, |
| R && | A, | ||
| P &&... | p ) |
sort( ex , A ) on the range \( A \) [see SortRange], the other arguments as for a vector: in place on the span of its items if \( A \) is contiguous, through a vector of its items otherwise, since the code of the sort reads the items by their addresses
| void ADS::sort | ( | E & | ex, |
| std::vector< T > & | A, | ||
| C | less = C() ) |
sort() above on a vector
| void ADS::sort | ( | E & | ex, |
| Store && | A, | ||
| C | less = C() ) |
the vector \( A \) sorted by less on the threads of ex, not necessarily stable
In pseudocode (the samplesort of Sanders and Winkel and of IPS4o, beyond the book), on \( p \) threads, with the recursion of line 18 at most 3 deep:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{sort}( \mathit{ex} , A , \mathit{less} ) \cr 2 & \quad \textbf{if}\ p < 3\ \textbf{or}\ n < 2^{15}\ \textbf{then} \ \mathrm{sort}( A , \mathit{less} );\ \textbf{return}\quad \triangleright\ \text{sequential} \cr 3 & \quad \textbf{if}\ A \text{ is sorted}\ \textbf{then return}\quad \triangleright\ \text{a parallel reduction by blocks} \cr 4 & \quad \textbf{if}\ A \text{ is decreasing}\ \textbf{then} \ \text{reverse } A \text{ in parallel};\ \textbf{return} \cr 5 & \quad \textbf{if}\ \text{each block of } A \text{ has at most 8 } \text{runs}\ \textbf{then} \cr 6 & \qquad \text{reverse the decreasing runs};\ \mathrm{multiwayMerge}( \mathit{ex} , \text{the runs} , A );\ \textbf{return} \cr 7 & \quad \textbf{if}\ \text{the items are numbers in their natural } \text{order}\ \textbf{then} \cr 8 & \qquad K \gets \text{the classifier by the highest bits of the keys } \text{of a sample of 1024} \cr 9 & \qquad \textbf{if}\ K \text{ spreads the sample}\ \textbf{then} \ \mathrm{distribute}( \mathit{ex} , A , K );\ \textbf{return} \cr 10 & \qquad \textbf{if}\ \text{the sample has at most 32 values, and } \text{so has } A\ \textbf{then} \cr 11 & \qquad\quad \text{count the items of each value, fill } A \text{ in } \text{order};\ \textbf{return} \cr 12 & \quad K \gets \text{the classifier by the } k - 1 \text{ splitters } \text{of a sorted sample of } 16 k \cr 13 & \quad \mathrm{distribute}( \mathit{ex} , A , K ) \cr 14 & \textbf{procedure}\ \mathrm{distribute}( \mathit{ex} , A , K ) \cr 15 & \quad \textbf{for}\ b \gets 1\ \textbf{to}\ B\ \textbf{do in } \textbf{parallel}\ c[ \beta ][ b ] \gets \text{the number of the } \text{items of the block } b \text{ in the bucket } \beta \cr 16 & \quad \mathit{start} \gets \text{the prefix sums of } c \text{ in } \text{the order } ( \beta , b ) \cr 17 & \quad \textbf{for}\ b \gets 1\ \textbf{to}\ B\ \textbf{do in } \textbf{parallel}\ \text{move the items of } b \text{, in order, to } \text{their places in a buffer} \cr 18 & \quad \textbf{foreach}\ \text{bucket } \beta \text{ with } |\beta| > n / p\ \textbf{do}\ \mathrm{sort}( \mathit{ex} , \beta , \mathit{less} ) \text{ into its place in } A \cr 19 & \quad \textbf{foreach}\ \text{other bucket } \beta \text{, from the } \text{largest, claimed by a free thread}\ \textbf{do} \cr 20 & \qquad \mathrm{sort}( \beta , \mathit{less} ) \text{ into its place } \text{in } A\quad \triangleright\ \text{a bucket of equal items as } \text{it is} \end{array}} \]
| decltype(auto) ADS::stableSort | ( | E & | ex, |
| R && | A, | ||
| P &&... | p ) |
stableSort( ex , A ) on the range \( A \) [see SortRange], the other arguments as for a vector: in place on the span of its items if \( A \) is contiguous, through a vector of its items otherwise, since the code of the sort reads the items by their addresses
| void ADS::stableSort | ( | E & | ex, |
| std::vector< T > & | A, | ||
| C | less = C() ) |
stableSort() above on a vector
| void ADS::stableSort | ( | E & | ex, |
| Store && | A, | ||
| C | less = C() ) |
the vector \( A \) sorted by less on the threads of ex, the equal items in their order
In pseudocode: the procedure of sort( ex , A , less ), whose distribution is stable, with \( A \) decreasing only if strictly, the stable sequential sort on the buckets and the stable merge of the runs.
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{stableSort}( \mathit{ex} , A , \mathit{less} ) \cr 2 & \quad \mathrm{sort}( \mathit{ex} , A , \mathit{less} )\quad \triangleright\ \text{with } \mathrm{stableSort}( \beta , \mathit{less} ) \text{ on the buckets} \end{array}} \]
|
inlineconstexpr |
the items from which the parallel sorts use more than one thread, \( 2^{15} \)
|
inlineconstexpr |
the threads from which the parallel sorts distribute: on 2 threads the passes of the distribution cost more than the second thread saves (on the benchmark, 0.6 to 1.5 times the time of the sequential sort, 0.8 in the median), and the sequential sort runs instead