-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
parallelize_in
helper function (#46)
Multi-threading of witness generation is tricky because one has to ensure the circuit column assignment order stays deterministic. To ensure good developer experience / avoiding pitfalls, we provide a new helper function for this. Co-authored-by: Jonathan Wang <[email protected]>
- Loading branch information
1 parent
805a21c
commit 0fff063
Showing
6 changed files
with
94 additions
and
73 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use itertools::Itertools; | ||
use rayon::prelude::*; | ||
|
||
use crate::{utils::ScalarField, Context}; | ||
|
||
use super::GateThreadBuilder; | ||
|
||
/// Utility function to parallelize an operation involving [`Context`]s in phase `phase`. | ||
pub fn parallelize_in<F, T, R, FR>( | ||
phase: usize, | ||
builder: &mut GateThreadBuilder<F>, | ||
input: Vec<T>, | ||
f: FR, | ||
) -> Vec<R> | ||
where | ||
F: ScalarField, | ||
T: Send, | ||
R: Send, | ||
FR: Fn(&mut Context<F>, T) -> R + Send + Sync, | ||
{ | ||
let witness_gen_only = builder.witness_gen_only(); | ||
// to prevent concurrency issues with context id, we generate all the ids first | ||
let ctx_ids = input.iter().map(|_| builder.get_new_thread_id()).collect_vec(); | ||
let (outputs, mut ctxs): (Vec<_>, Vec<_>) = input | ||
.into_par_iter() | ||
.zip(ctx_ids.into_par_iter()) | ||
.map(|(input, ctx_id)| { | ||
// create new context | ||
let mut ctx = Context::new(witness_gen_only, ctx_id); | ||
let output = f(&mut ctx, input); | ||
(output, ctx) | ||
}) | ||
.unzip(); | ||
// we collect the new threads to ensure they are a FIXED order, otherwise later `assign_threads_in` will get confused | ||
builder.threads[phase].append(&mut ctxs); | ||
|
||
outputs | ||
} |
This file contains 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
This file contains 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
This file contains 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