|
| template<SortStore Store, VectorKey T = std::ranges::range_value_t< std::remove_cvref_t< Store > >> |
| void | ADS::vectorQuickSort (Store &&A) |
| | the quicksort of \( A \) in the lanes of the vector registers, by the natural order (the total order of IEEE 754 for floating point numbers)
|
| template<VectorKey T> |
| void | ADS::vectorQuickSort (std::vector< T > &A) |
| | vectorQuickSort() above on a vector
|
| template<VectorKey T> |
| void | ADS::vectorQuickSort (T *a, std::size_t n) |
| | the sort of vectorQuickSort() above on \( a[ 0 .. n - 1 ] \)
|
template<SortRange R, class... P>
requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { vectorQuickSort( v , std::forward< P >( p )... ); } |
| decltype(auto) | ADS::vectorQuickSort (R &&A, P &&... p) |
| | vectorQuickSort() 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
|
The quicksort in the lanes of the vector registers, beyond the book: vectorQuickSort() sorts integers and floating point numbers of 32 or 64 bits by their natural order.
With ADS_WITH_HIGHWAY it is the vectorized quicksort of Wassenberg, Blacher, Giesen and Sanders, hwy::VQSort() of the library of Highway, on the vectors of the processor found when the program runs; without it, the quicksort below, in portable code that partitions \( W \) keys per step, \( W = 32 / w \) for keys of \( w \) bytes, and sorts the parts of at most \( 16 W \) keys by a sorting network held in 16 registers of \( W \) lanes, arrays that the compiler vectorizes as it can.
- The keys: an integer is its own key, made signed by flipping its sign bit if it is unsigned. A floating point number is sorted as the signed integer \( b \) of its bits, with no conversion: \( b \) keeps the order of the numbers with the sign bit clear and reverses that of those with it set, which come all first, since for them \( b = -2^{w-1} + | x |_{\mathrm{bits}} \) grows with the magnitude; once sorted, their block, found by a binary search, is reversed in place, a pass over the negative numbers alone instead of a conversion of all of them before and after the sort. The order is that of IEEE 754 extended to a total order, \( -0 \) before \( +0 \) and the NaN at the ends, so that a NaN breaks nothing.
- The partition of a part \( a[ 0 .. n - 1 ] \) by a pivot \( p \), as in the quicksort of Bramas: the first and the last \( U W \) keys (\( U = 8 \)) are loaded aside, which leaves \( U W \) free places at each end, and then \( U \) blocks of \( W \) keys at a time are read from the end with fewer free places; each block is compared with \( p \) into a mask \( m \) of \( W \) bits, and its \( \ell = | m | \) keys below \( p \) are written at the left end and the \( W - \ell \) others at the right one. Before a read the two ends have \( f_L + f_R = 2 U W \) free places, and the read is from the end with \( \min\{ f_L , f_R \} \leq U W \), so that after it both have at least \( U W \): no write overtakes a key not yet read, and the partition is in place with no branch on the keys (the choice of the end is one branch per \( U \) blocks): the \( \ell \) keys and the others are put apart in an array of \( 2 W \) keys and copied to the two ends. Each read of \( U \) blocks prefetches the \( U \) blocks \( 8 U W \) keys further in the same direction, for a pass over a part larger than the cache of the second level, where the hardware prefetcher alone falls behind the two ends read in turn.
- The pivot is the median of a sample of \( S = \max\{ 8 , W \} \) keys, one from each of \( S \) equal stretches of the part at the same random offset, sorted by the network below. If the sample repeats the pivot, the partition keeps the minimum and the maximum of the part and a second pass puts the keys equal to \( p \) apart. A pivot that is the minimum of the part, so that no key is below it, also gets that second pass.
- The smaller side recurs and the larger one loops, with a stack of \( O( \log n ) \); past \( 2 \lfloor \log_2 n \rfloor \) levels a part is sorted by heapsort, as in the introsort of Musser, so that the worst case is \( O( n \log n ) \).
- A part of at most \( 16 W \) keys is loaded into \( M = 2^{\lceil \log_2 \lceil n / W \rceil \rceil} \) registers, the lanes past \( n \) filled with the largest key, and sorted by a network of compare-exchange steps, each a minimum and a maximum. With \( M \geq W \) the keys are held by columns, the key of index \( g = i M + h \) in the lane \( i \) of the register \( h \): the columns are sorted by the odd-even merge sort of Batcher, register against register (63 comparators for \( M = 16 \), each a minimum and a maximum of whole registers), then merged by the bitonic network in its variant whose comparators all go upwards, the index \( g \) compared first with \( g \oplus ( K - 1 ) \) for blocks of \( K \) keys and then with \( g \oplus J \) for \( J = K / 4 , \dots , 1 \), a step between registers for \( J < M \) and within them, by a shuffle of the lanes, otherwise, and at last each block of \( W \) registers is transposed by \( \log_2 W \) steps of exchanges of blocks of lanes. The steps within registers, the dearer ones (a shuffle besides the minimum and the maximum), are then \( \frac{1}{2} \log_2 W ( \log_2 W - 1 ) \) in all, besides a reversal per merge and the transposition, 14 for \( M = W = 16 \) against the 26 of the bitonic network on registers held by rows, which the smaller networks use, with constant masks of the lanes that keep the minimum.
- Two sorted runs are merged in the registers by mergeRuns(), as in the merge sort of Chhugani et al.: the register of the keys carried over is merged with the next register of the run whose next key is smaller (the second one reversed, a minimum and a maximum, then the steps of a bitonic merge within each register), the smaller \( W \) keys written and the larger carried; the writes never overtake the reads, so that one run may stay in place while the other is a copy aside, forward from the smallest keys or backward from the largest. sort() merges with it the runs of the integers of 32 bits [see Sort.h].
kVectorSort tells whether the sort is that of Highway, and ADS_VECTOR_SORT set to 0 (or ADS_SIMD_SCALAR set to 1 [see Simd.h]) takes the portable code with Highway too. The keys of the floating point numbers are the same on both ways, so that the order is the same.
vectorQuickSort() is not stable (equal integers cannot be told apart, but \( -0 \) and \( +0 \) can). The contracts [see Contract.h]: the vector ends with the same items sorted, an audit. The functions are defined in VectorQuickSort.tpp.
- See also
- B. Bramas, A novel hybrid quicksort algorithm vectorized using AVX-512 on Intel Skylake, International Journal of Advanced Computer Science and Applications 8(10), article 44, 2017, doi:10.14569/IJACSA.2017.081044
-
S. Gueron, V. Krasnov, Fast quicksort implementation using AVX instructions, The Computer Journal 59(1), 83-90, 2016, doi:10.1093/comjnl/bxv063
-
J. Wassenberg, M. Blacher, J. Giesen, P. Sanders, Vectorized and performance-portable quicksort, Software: Practice and Experience 52(12), 2684-2699, 2022, doi:10.1002/spe.3142
-
J. Chhugani, A. D. Nguyen, V. W. Lee, W. Macy, M. Hagog, Y.-K. Chen, A. Baransi, S. Kumar, P. Dubey, Efficient implementation of sorting on multi-core SIMD CPU architecture, Proceedings of the VLDB Endowment 1(2), 1313-1324, 2008, doi:10.14778/1454159.1454171
-
K. E. Batcher, Sorting networks and their applications, Proceedings of the AFIPS Spring Joint Computer Conference 32, 307-314, 1968, doi:10.1145/1468075.1468121
-
D. R. Musser, Introspective sorting and selection algorithms, Software: Practice and Experience 27(8), 983-993, 1997, doi:10.1002/(SICI)1097-024X(199708)27:8<983::AID-SPE117>3.0.CO;2-#
- Author
- Donato Meoli
Dipartimento di Informatica
Università di Pisa
- Copyright
- © by Donato Meoli