-
Notifications
You must be signed in to change notification settings - Fork 0
perf: parallelize FRI fold with Rayon #597
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,19 +5,50 @@ use math::field::{ | |
| element::FieldElement, | ||
| traits::{IsFFTField, IsField, IsSubFieldOf}, | ||
| }; | ||
| #[cfg(feature = "parallel")] | ||
| use rayon::prelude::*; | ||
|
|
||
| /// Minimum number of elements for parallel FRI fold. Below this threshold | ||
| /// the overhead of Rayon dispatch exceeds the benefit. | ||
| #[cfg(feature = "parallel")] | ||
| const FRI_FOLD_PARALLEL_THRESHOLD: usize = 4096; | ||
|
|
||
| /// Evaluation-form FRI fold: given evaluations in bit-reversed order where | ||
| /// consecutive pairs (2j, 2j+1) are conjugates (p(x_j), p(-x_j)), compute | ||
| /// the folded evaluations: (lo + hi) + inv_twiddle[j] * zeta * (lo - hi) | ||
| /// = 2 * (p_even(x_j²) + zeta * p_odd(x_j²)) | ||
| /// | ||
| /// After folding, the N/2 results are evaluations on the squared coset | ||
| /// in bit-reversed order, preserving conjugate pairing for the next fold. | ||
| pub fn fold_evaluations_in_place<F: IsSubFieldOf<E>, E: IsField>( | ||
| pub fn fold_evaluations_in_place<F, E>( | ||
| evals: &mut Vec<FieldElement<E>>, | ||
| zeta: &FieldElement<E>, | ||
| inv_twiddles: &[FieldElement<F>], | ||
| ) { | ||
| ) where | ||
| F: IsSubFieldOf<E> + Send + Sync, | ||
| E: IsField + Send + Sync, | ||
| FieldElement<E>: Send + Sync, | ||
| FieldElement<F>: Send + Sync, | ||
| { | ||
| let half = evals.len() / 2; | ||
|
|
||
| #[cfg(feature = "parallel")] | ||
| if half >= FRI_FOLD_PARALLEL_THRESHOLD { | ||
| // Parallel: compute folded values into a new buffer to avoid | ||
| // read/write aliasing on the same slice. | ||
| let folded: Vec<FieldElement<E>> = evals | ||
| .par_chunks_exact(2) | ||
| .zip(inv_twiddles.par_iter()) | ||
|
Comment on lines
+40
to
+41
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 –
The caller ( debug_assert_eq!(inv_twiddles.len(), evals.len() / 2); |
||
| .map(|(pair, tw)| { | ||
| let sum = &pair[0] + &pair[1]; | ||
| let diff = &pair[0] - &pair[1]; | ||
| &sum + &(tw * &(zeta * &diff)) | ||
| }) | ||
| .collect(); | ||
| *evals = folded; | ||
| return; | ||
| } | ||
|
|
||
| for j in 0..half { | ||
| let lo = &evals[2 * j]; | ||
| let hi = &evals[2 * j + 1]; | ||
|
|
||
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 –
Send + Syncbounds leak into non-parallel buildsThese four bounds are unconditional, so they apply even when the
parallelfeature is off. Any caller with a field element type that doesn't implementSend + Sync(e.g. a type containingRcorCell) will fail to compile, even in a single-threaded build. In practice, all concrete field element types in this crate areSend + Sync, but the bounds still unnecessarily restrict the API surface.The cleanest fix without duplicating the function body is to gate them with a custom supertrait:
then replace
Send + SyncwithMaybeParallelthroughout. Alternatively, if all callers satisfy the bounds unconditionally, document that assumption and leave it as-is — the important thing is the intent is clear.