-
Notifications
You must be signed in to change notification settings - Fork 1
Parallelize in_place_bit_reverse_permute #562
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jotabulacios
wants to merge
3
commits into
main
Choose a base branch
from
perf/parallel-bit-reverse
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,7 +1,42 @@ | ||||||||||
| /// In-place bit-reverse permutation algorithm. Requires input length to be a power of two. | ||||||||||
| pub fn in_place_bit_reverse_permute<E>(input: &mut [E]) { | ||||||||||
| for i in 0..input.len() { | ||||||||||
| let bit_reversed_index = reverse_index(i, input.len() as u64); | ||||||||||
| pub fn in_place_bit_reverse_permute<E: Send>(input: &mut [E]) { | ||||||||||
| let n = input.len(); | ||||||||||
| #[cfg(feature = "parallel")] | ||||||||||
| { | ||||||||||
| // Pair-parallel swap: each pair (i, br(i)) with i < br(i) is independent of all | ||||||||||
| // other pairs (disjoint indices), so threads can swap concurrently provided they | ||||||||||
| // never touch the same memory location. `if br > i` selects exactly one owner | ||||||||||
| // per pair, so no two threads ever write the same slot. | ||||||||||
| const PARALLEL_BITREV_THRESHOLD: usize = 1 << 14; | ||||||||||
| if n >= PARALLEL_BITREV_THRESHOLD { | ||||||||||
| use rayon::prelude::*; | ||||||||||
| struct SendPtr<E>(*mut E); | ||||||||||
| impl<E> Copy for SendPtr<E> {} | ||||||||||
| impl<E> Clone for SendPtr<E> { | ||||||||||
| fn clone(&self) -> Self { | ||||||||||
| *self | ||||||||||
| } | ||||||||||
| } | ||||||||||
| unsafe impl<E> Send for SendPtr<E> {} | ||||||||||
| unsafe impl<E> Sync for SendPtr<E> {} | ||||||||||
| let ptr = SendPtr(input.as_mut_ptr()); | ||||||||||
| (0..n).into_par_iter().for_each(|i| { | ||||||||||
| let br = reverse_index(i, n as u64); | ||||||||||
| if br > i { | ||||||||||
| // SAFETY: (i, br) uniquely identifies this pair (smaller index is owner), | ||||||||||
| // so no two threads race on the same `ptr.0.add(k)` slot. Both indices | ||||||||||
| // are in-bounds since i < n and br < n. | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Low: redundant alias
Suggested change
|
||||||||||
| let p = ptr; | ||||||||||
| unsafe { | ||||||||||
| core::ptr::swap(p.0.add(i), p.0.add(br)); | ||||||||||
| } | ||||||||||
| } | ||||||||||
| }); | ||||||||||
| return; | ||||||||||
| } | ||||||||||
| } | ||||||||||
| for i in 0..n { | ||||||||||
| let bit_reversed_index = reverse_index(i, n as u64); | ||||||||||
| if bit_reversed_index > i { | ||||||||||
| input.swap(i, bit_reversed_index); | ||||||||||
| } | ||||||||||
|
|
||||||||||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Low: missing invariant assertion before unsafe code
The entire function — including the doc comment — requires
nto be a power of two. Violating that causesreverse_indexto produce out-of-bounds indices, which is a silent panic in the serial path but undefined behaviour in the parallel path (raw pointer dereference out of bounds).All call-sites in
polynomial.rsandbowers_fft.rsalready guard withis_power_of_two()before calling this function, but adebug_asserthere makes the safety invariant explicit and catches future misuse: