Fast and Robust Vectorized In-Place Sorting of Primitive Types

Mark Blacher, Joachim Giesen, Lars KuehneSEA 2021

TL;DR

This paper presents a novel hybrid sorting algorithm that leverages SIMD (Single Instruction, Multiple Data) vector instructions to achieve significant speedups for sorting primitive data types like integers. By optimizing both sorting networks for smaller arrays and Quicksort for larger ones, and introducing techniques for robustness against various data distributions, their implementation outperforms existing high-performance solutions by at least 30%.

What Problem Does This Paper Address?

Sorting is a fundamental operation in computer science, crucial for countless applications. Modern CPUs feature powerful SIMD instructions that can process multiple data elements simultaneously, offering a huge potential for accelerating computations. However, traditional sorting algorithms are not inherently designed for SIMD, making their direct vectorization challenging and often inefficient. Existing vectorized sorting solutions either don't fully exploit SIMD capabilities, require significant extra memory, or struggle with diverse data distributions (e.g., arrays with many duplicate keys or skewed distributions). This paper aims to bridge this gap by developing a fast, robust, and memory-efficient vectorized sorting algorithm for primitive types.

Methods

The authors propose a hybrid approach combining vectorized sorting networks and Quicksort.

  • Vectorized Sorting Networks: For small to medium-sized arrays (up to 512 elements), they employ sorting networks. Their key innovation here is to fully utilize the capacity of SIMD vector registers (e.g., AVX2's 256-bit registers) by carefully orchestrating shuffle and compare-exchange operations. This differs from prior work that might only use half the register capacity. For larger arrays, they adapt this technique to sort columns of a matrix representation, avoiding costly transpositions.
  • Vectorized Quicksort: For larger arrays, they build upon existing vectorized Quicksort implementations.
  • In-place Partitioning: They combine techniques from Gueron & Krasnov and Bramas to achieve an in-place partitioning function using AVX2 instructions. This involves caching vectors and using permutation masks to rearrange elements efficiently within the vector registers.
  • Robust Pivot Selection: To address Quicksort's sensitivity to data distributions, they introduce a two-pronged pivot strategy. The primary strategy uses a vectorized median-of-medians heuristic. If this leads to unbalanced partitions, they switch to a secondary strategy that selects a pivot based on the average of the current bounds, inspired by Radix Exchange Sort, ensuring a guaranteed logarithmic reduction in the search space.
  • Duplicate Key Handling: Their approach implicitly handles arrays with many duplicate keys by detecting when sub-arrays contain identical elements, avoiding unnecessary partitioning.

Datasets

The paper evaluates its algorithm using synthetic datasets designed to test performance across various scenarios:

  • Uniform: Random integers within a wide range.
  • Gaussian: Normally distributed random numbers, leading to many duplicate values.
  • Zero: Arrays where all elements are identical (zero entropy).
  • Almost Sorted: Arrays with a small number of misplaced elements.

The authors do not introduce new benchmark datasets but rather use these standard distributions to compare against existing sorting algorithms.

Key Insights and Ideas

  • Full SIMD Register Utilization is Key: Simply applying SIMD to existing algorithms isn't enough. The authors demonstrate that creatively using the entire vector register capacity, especially in sorting networks, is crucial for achieving maximum performance.
  • Hybrid Approach is Optimal: No single sorting algorithm excels in all scenarios. Combining the strengths of sorting networks (for small, predictable workloads) with the adaptability of Quicksort (for larger, variable workloads) is a winning strategy.
  • Robustness is as Important as Speed: Traditional vectorized Quicksort implementations can degrade significantly with non-uniform data. The proposed dual pivot strategy effectively mitigates this, making the algorithm reliable across diverse inputs.
  • In-place is Achievable and Beneficial: Achieving in-place sorting with SIMD is a significant engineering feat. It reduces memory overhead, which is critical for large datasets and can improve cache performance.

Key Results

  • The implemented hybrid sorting algorithm, using AVX2 intrinsics, achieves a speedup of at least 30% compared to state-of-the-art high-performance sorting alternatives like Intel's IPP radix sort and sort AVX-512.
  • The algorithm demonstrates superior performance across various data distributions, particularly outperforming sort AVX-512 on arrays with many duplicate keys.
  • The algorithm is in-place, requiring only O(log n) additional memory on average, unlike some other vectorized solutions.
  • The worst-case running time is O(kn), where k is the number of bits per key, and the average-case running time is O(n log n).
  • Performance indicators show high CPU utilization (instruction-level parallelism) with a low number of branch misses, contributing to its speed.

Limitations and Future Work

  • Single-Threaded Focus: The current implementation and evaluation focus on single-threaded performance. While vectorization and parallelization are orthogonal concepts, the authors acknowledge that multi-threaded performance might be limited by memory bandwidth.
  • AVX2 Specificity: The implementation uses AVX2 intrinsics for broader compatibility. Future work could explore leveraging AVX-512 for even greater performance gains on compatible hardware.
  • Extensibility: The authors mention that the algorithm is easily extensible to other primitive types (unsigned int, float, 64-bit types) and plan to explore this further.
  • Parallelization: A significant area for future work is the efficient combination of their vectorization techniques with parallelization strategies.

Summary updated

Abstract

Modern CPUs provide single instruction-multiple data (SIMD) instructions. SIMD instructions process several elements of a primitive data type simultaneously in fixed-size vectors. Classical sorting algorithms are not directly expressible in SIMD instructions. Accelerating sorting algorithms with SIMD instruction is therefore a creative endeavor. A promising approach for sorting with SIMD instructions is to use sorting networks for small arrays and Quicksort for large arrays. In this paper we improve vectorization techniques for sorting networks and Quicksort. In particular, we show how to use the full capacity of vector registers in sorting networks and how to make vectorized Quicksort robust with respect to different key distributions. To demonstrate the performance of our techniques we implement an in-place hybrid sorting algorithm for the data type int with AVX2 intrinsics. Our implementation is at least 30% faster than state-of-the-art high-performance sorting alternatives.
BibTeX
@inproceedings{blacher2021fast,
  author = {Mark Blacher and Joachim Giesen and Lars Kuehne},
  title = {{Fast and Robust Vectorized In-Place Sorting of Primitive Types}},
  booktitle = {SEA},
  year = {2021},
  url = {https://openreview.net/forum?id=vUids8FB52},
}
Figure