Skip to main content
QUICK REVIEW

[논문 리뷰] BlockQuicksort: How Branch Mispredictions don't affect Quicksort

Stefan Edelkamp, A. Weiss|arXiv (Cornell University)|2016. 04. 22.
Advanced Data Storage Technologies참고 문헌 13인용 수 8
한 줄 요약

BlockQuicksort는 Quicksort에서 발생하는 분기 잘못 예측를 제거하기 위해 고정 크기의 블록(예: 128개 요소)으로 데이터를 분할하고, 비교 결과를 버퍼에 저장한 후 요소를 재배열함으로써 제어 흐름과 데이터 의존적 비교를 분리하는 새로운 인-place 정렬 알고리즘이다. 이 방법은 평균 분기 잘못 예측 수를 최대 $5c n\log n + \mathcal{O}(n)$로 줄이며, 랜덤 정수 데이터에서 GCC의 std::sort보다 80퍼센트 이상 빠른 정렬 성능을 달성하고, 일부 워크로드에서는 Super Scalar Sample Sort조차도 능가한다.

ABSTRACT

Since the work of Kaligosi and Sanders (2006), it is well-known that Quicksort -- which is commonly considered as one of the fastest in-place sorting algorithms -- suffers in an essential way from branch mispredictions. We present a novel approach to address this problem by partially decoupling control from data flow: in order to perform the partitioning, we split the input in blocks of constant size (we propose 128 data elements); then, all elements in one block are compared with the pivot and the outcomes of the comparisons are stored in a buffer. In a second pass, the respective elements are rearranged. By doing so, we avoid conditional branches based on outcomes of comparisons at all (except for the final Insertionsort). Moreover, we prove that for a static branch predictor the average total number of branch mispredictions is at most $εn \log n + O(n)$ for some small $ε$ depending on the block size when sorting $n$ elements. Our experimental results are promising: when sorting random integer data, we achieve an increase in speed of 80% over the GCC implementation of C++ std::sort. Also for many other types of data and non-random inputs, there is still a significant speedup over std::sort. Only in few special cases like sorted or almost sorted inputs, std::sort can beat out implementation. Moreover, even on random input permutations, our implementation is even slightly faster than an implementation of the highly tuned Super Scalar Sample Sort, which uses a linear amount of additional space.

연구 동기 및 목표

  • 현대 파이ipel라인 프로세서에서 브랜치 잘못 예측로 인한 성능 저하 문제를 해결하기 위해.
  • 복잡한 피벗 선택이나 외부 메모리에 의존하지 않고도 분기 잘못 예측를 최소화하는 인-place 정렬 알고리즘을 설계하기 위해.
  • 랜덤, 거의 정렬된, 중복 요소가 많은 입력과 같은 다양한 데이터 유형에서 높은 성능을 유지하기 위해.
  • Super Scalar Sample Sort와 같은 고도로 최적화된 알고리즘과 경쟁 가능한 성능을 달성하면서도 인-place로 유지하기 위해.

제안 방법

  • 입력 배열을 고정 크기의 블록(예: 128개 요소)으로 나누어 제어 흐름과 데이터 의존적 비교를 분리한다.
  • 각 블록에 대해 피벗과 모든 요소를 비교하고, 비교 결과(예: '작다' 또는 '크다')를 버퍼에 저장하여 비교 중 조건부 브랜치를 방지한다.
  • 모든 비교가 완료된 후 두 번째 패ass를 수행하여 저장된 비교 결과에 기반해 요소를 재배열함으로써 분할 과정에서 데이터 의존적 브랜치를 제거한다.
  • 재귀의 기저 사례로 삽입 정렬을 사용하지만, 버퍼 사용 및 블록 처리를 최적화하여 전체적인 분기 잘못 예측 수를 줄인다.
  • 정적 브랜치 예측 모델을 적용하여 분기 잘못 예측 수를 이론적으로 $5c n\log n + \mathcal{O}(n)$로 경계짓는다. 여기서 $5c$는 블록 크기에 따라 달라진다.
  • 피벗 선택에 중앙값-($\sqrt{n}$) 또는 중앙값-5를 사용하여 성능와 예측 가능성의 균형을 이룬다.

실험 결과

연구 질문

  • RQ1비교 결과를 제어 흐름 결정에서 분리함으로써 Quicksort의 분기 잘못 예측를 상당히 줄일 수 있는가?
  • RQ2블록 단위로 비교 결과를 버퍼링하면 깊은 파이ipel라인을 가진 현대 프로세서에서 성능 향상이 눈에 띄게 발생하는가?
  • RQ3동적 메모리 할당 없이도 인-place 정렬 알고리즘이 Super Scalar Sample Sort와 같은 고도로 최적화된 알고리즘과 경쟁 가능한 성능을 달성할 수 있는가?
  • RQ4블록 기반 접근 방식은 랜덤, 거의 정렬된, 중복 요소가 많은 다양한 입력 유형에서 어떻게 성능을 발휘하는가?
  • RQ5이론적 분기 잘못 예측 수 경계는 실질적으로 달성 가능한가? 그리고 블록 크기 변화에 따라 어떻게 스케일링되는가?

주요 결과

  • 정적 브랜치 예측 모델 하에서 BlockQuicksort는 평균 분기 잘못 예측 수를 최대 $5c n\log n + \mathcal{O}(n)$로 줄이며, $5c$는 블록 크기에 따라 결정되는 작은 값이다.
  • 랜덤 정수 데이터에서 BlockQuicksort는 GCC의 std::sort보다 요소 정렬 속도(초당 정렬 요소 수)가 80퍼센트 이상 빠르다.
  • 랜덤 순열, 거의 정렬된 배열, 중복 요소가 많은 데이터를 포함한 모든 테스트 데이터 유형에서 BlockQuicksort는 std::sort를 능가하지만, 정렬된 입력과 같은 희귀한 경우를 제외하고는 대부분의 경우에서 성능이 뛰어나다.
  • 랜덤 순열에서도 BlockQuicksort는 선형 추가 메모리를 사용하는 Super Scalar Sample Sort보다 略적으로 더 빠르며, 더 나은 브랜치 예측과 캐시 행동 덕분이다.
  • 알고리즘은 낮은 명령어 수와 캐시 미스 비율을 달성했으며, 평균적으로 요소당 2.25회의 분기 잘못 예측를 기록하여 std::sort의 10.23보다 훨씬 낮다.
  • 중복 요소 확인(duplication-checking, dc)과 중앙값-$\sqrt{n}$ 피벗 선택을 사용함으로써, 특히 반복 요소가 많은 데이터에서 성능 향상이 뚜렷하다.

더 나은 연구,지금 바로 시작하세요

논문 읽기부터 검토까지, 연구 시간을 획기적으로 줄여보세요.

카드 등록 없음 · 무료 플랜 제공

이 리뷰는 AI가 만들고, 인간 에디터가 검토했습니다.