Skip to content

Speed up randint array-bounds via chunking Lemire - #173

Open
vlad-perevezentsev wants to merge 7 commits into
masterfrom
chunk-randint-broadcast-words
Open

vlad-perevezentsev wants to merge 7 commits into
masterfrom
chunk-randint-broadcast-words

Conversation

@vlad-perevezentsev

@vlad-perevezentsev vlad-perevezentsev commented Sep 4, 2026 •

Copy link
Copy Markdown
Collaborator

This PR implements the word-generation speedup suggested for randint array-like bounds in the #168 (comment)

It changes irk_rand_bounded_broadcast to:

  • consume MKL words in cache-sized chunks instead of one full-length pass, avoiding a memory round-trip over the whole array;
  • pick the Lemire rejection test by range width: lo < s is free for narrow ranges but mispredicts for wide ones, so its hits are counted on the first chunk and the remaining chunks switch to the branchless test with the threshold memoized per range.

Performance (Intel(R) Xeon(R) Platinum 8480+)

(low=0, high=1e9):

Size (N) dtype Before After Speedup
5,000,000 int32 52.00 ms 31.44 ms 1.65x
5,000,000 int64 48.82 ms 35.53 ms 1.37x
50,000,000 int32 642.91 ms 428.80 ms 1.50x
50,000,000 int64 602.30 ms 466.51 ms 1.29x

idx[wpos++] = j;
continue;
/* retry the chunk's rejects locally with fresh words */
while (n_pending > 0) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One idea is to move this loop out, and track n_pending outside of the chunk loop (i.e., before the for loop) then retry all of the rejects at the end. We'd have to set the idx allocation to len instead of chunk_cap too.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants