|
ADS
Algorithms and Data Structures
|
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. More...
#include "ADS/Contract.h"#include "ADS/memory/HugePageAllocator.h"#include "ADS/parallel/Executor.h"#include "ADS/parallel/ParallelMerge.h"#include "ADS/parallel/Scratch.h"#include "ADS/sort/LearnedSort.h"#include "ADS/sort/RadixSort.h"#include "ADS/sort/Sort.h"#include "ADS/sort/VectorQuickSort.h"#include <algorithm>#include <cstddef>#include <cstdint>#include <functional>#include <vector>#include "ADS/parallel/impl/ParallelSort.tpp"Go to the source code of this file.
Namespaces | |
| namespace | ADS |
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: 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.
A first pass, a parallel reduction by blocks, finds whether \( A \) is already sorted (then nothing moves) or in decreasing order (strictly, for the stable sort: then a parallel reversal), by stretches of 32 pairs with no branch inside (which vectorize); a block stops at the first stretch out of both orders, so that on most inputs the pass reads little. A second pass, by blocks as well, cuts each of 8 blocks (or \( p \) blocks, if more) into its maximal runs, non-decreasing or strictly decreasing, and gives up at the ninth run of a block, which again on most inputs comes after a few items. If no block gave up, \( A \) is made of at most 8 runs per block (e.g., the vectors made of a few sorted sequences, or of an increasing and a decreasing one), whose decreasing ones are reversed and which are merged by the parallel multiwayMerge() [see ParallelMerge.h] through a buffer, stably, in \( O( ( n / p ) \log r ) \) time for \( r \) runs, as powerSort() [see RunSorts.h] merges them in the sequential sort (a distribution would forget that order). Otherwise the items are distributed into \( k \) buckets whose concatenation is sorted, by one of two classifiers:
\[ \min\bigl( \max( \mathrm{key}( x ) - \ell , 0 ) \gg s , ( u - \ell ) \gg s \bigr) , \]
with \( s \) the least shift that leaves at most 256 buckets, i.e., the 8 highest bits in which the keys of the sample differ (the parallel radix sort of IPS2Ra takes them from the least and the greatest of all the keys, a pass more over the input); the keys out of the range of the sample go to the two buckets at the ends, which the sort of the buckets takes care of. The bits must spread the keys, which they do not when the keys are the floating point numbers of a few exponents, whose highest bits are the sign and the exponent, or integers of a law with a heavy tail: if a bucket gets more than 4 times its share of the sample (and 32 more), the numbers go to the other classifier. When the sample shows at most 32 distinct values that the bits do not separate, the numbers are counted instead (not the floating point numbers of the stable sort): each item is found among those values by counting the keys less than its own (32 comparisons in vectors), the keys telling apart all the values, \( -0 \) and \( +0 \) included, and \( A \) is filled with them in order, 2 passes over \( A \); an item of another value sends \( A \) to the other classifier, whose buckets of equality move the items once. When the sample holds at most 256 distinct keys, each key has a bucket of its own; if the integers of \( A \) all fall within the bounds of the sample (which the pass of the counts checks), the counts are those of a counting sort, and each bucket is filled with its key, read in the same pass, with no move of the items: 2 passes over \( A \) in all;\( k \) is 256, or \( 4 p \) rounded up to a power of 2 when \( n \) is small, with \( p \) the threads. The classification is a parallel loop on blocks of \( A \), each of which counts its items per bucket (and keeps the bucket of each item when the classifier compares, so that the items are classified once); the counts give, by a prefix sum in the order (bucket, block), where each block puts its items of each bucket in a buffer of \( n \) items, and a second parallel loop moves them there. Items copied as bytes, several to a line of the cache, go first to a line of 64 bytes per bucket on the stack of the thread, which is copied to the buffer when full (the software write-combining of the radix sorts): the \( k \) streams of stores then leave whole lines, instead of reading each line of the buffer before writing an item into it. The buffer is kept for the next call [see Scratch.h], since the faults of a fresh one cost as much as the moves of the items into it. A block moves its items in their order and the blocks take consecutive places, so that the distribution is stable.
Last, the threads claim the buckets from the largest down (the schedule LPT of Graham, within \( 4 / 3 \) of the best), and each moves the bucket into a vector of its own, sorts it by the sequential sort, and moves it into its place in \( A \). A bucket of numbers of 32 or 64 bits in their natural order goes instead straight to its place, where vectorQuickSort() [see VectorQuickSort.h], the sort that sort() would choose for it, sorts it unless a scan finds it already in order (e.g., a bucket of equal keys at an end) or made of at most kRuns runs, which go to the sequential sort and its merges (one move less per item; not for the stable sort of floating point numbers, which must keep the order of \( -0 \) and \( +0 \)), and a bucket of equal items is moved back as it is. A bucket larger than \( n / p \), which one thread would take too long on, is sorted first by the same parallel sort, on all the threads, down to a depth of 3 recursions, below which the sequential sort runs.
With \( n \) items and \( p \) threads the sort moves each item 4 times (into the buffer, into the vector of its bucket, back to \( A \), plus the moves of the sequential sort) and makes \( O( n \log k ) \) comparisons to classify, in \( O( n / p + k p ) \) time for the classification and \( O( ( n / p ) \log( n / k ) ) \) for the buckets when they are balanced; the buffer takes \( n \) items and the counts \( k B \) words for \( B \) blocks.
learnedSort( ex , A ) and learnedStableSort( ex , A ) distribute the numbers of 32 or 64 bits by the model of the CDF of LearnedSort.h instead of the two classifiers above, a grid on the range of a sample of 4096 keys that finds the bucket of a key among 256 by its cell and 4 comparisons with no branch (the tree takes 8 dependent steps), when the sample shows a distribution that the model fits, and are sort( ex , A ) and stableSort( ex , A ) otherwise; the buckets go on as above. sort( ex , A ) does not choose them by itself, since on the measures they did not beat the bits and the tree beyond the noise.
The contracts [see Contract.h]: less is a strict weak order, and its calls from several threads at once do not race; the vector ends with the same items sorted (an audit). The functions are defined in ParallelSort.tpp.