Repository navigation
Require a power-of-two block_size for MergeSort - #173
Merged
Merged
Conversation
The merge tile kernel sorts a tile of 2 * block_size elements in passes that double the run length while it is at most block_size, and hands runs to threads as if the run length divided block_size. Any other block_size leaves part of each tile unmerged and indexes local memory past the tile: sort!, sortperm! and sort_by_key! returned wrong orders, or hit an illegal address once the input spanned more than a few tiles. Check for a power of two in MergeSort's field checks, as BitonicSort and RadixSort do. Resolution runs them after filling the fields from the tuning, so a tuned block size is checked too. Fixes JuliaGPU#172.
maleadt
force-pushed
the
ab/merge-sort-pow2-block-size
branch
from
October 26, 2026 19:03
490789a to
613e08f
Compare
Member
|
Rebased and slightly reworked. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The merge tile kernel sorts a tile of
2 * block_sizeelements in passes that double the run length while it is at mostblock_size, and hands runs to threads as if the run length dividedblock_size. With any otherblock_size, part of each tile is never merged, and the last pair of runs reaches past the tile in local memory.MergeSort's field check accepted any positive size, and its docstring said so. On main,sort!,sortperm!andsort_by_key!with e.g.MergeSort(block_size=3)return wrong orders. Withblock_size=96they corrupt the data or hitCUDA_ERROR_ILLEGAL_ADDRESSonce the input spans a few tiles.This PR rejects such sizes with an
ArgumentError, asBitonicSortandRadixSortalready do. Resolution checks the fields after filling them from the tuning, so a tunedmerge_block_sizeis checked too. The default of 256, and the power of two it shrinks to for large elements, are unaffected.Supporting any block size in the kernel would take more passes and guards against the tile's end. That isn't worth it for a setting that is a power of two in practice.
Fixes #172.