Skip to content

Commit

Permalink
feat(groth16): control parallel proving threads
Browse files Browse the repository at this point in the history
Uses the existing env variable to restrict the number of threads used for parallel proving.

Cleaned up version of #81
  • Loading branch information
dignifiedquire committed Jun 8, 2020
1 parent e0ac6b8 commit 0c1868d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/groth16/mod.rs
Expand Up @@ -693,7 +693,7 @@ pub struct BatchPreparedVerifyingKey<E: Engine> {
ic: Vec<E::G1Affine>,
}

pub trait ParameterSource<E: Engine> {
pub trait ParameterSource<E: Engine>: Send {
type G1Builder: SourceBuilder<E::G1Affine>;
type G2Builder: SourceBuilder<E::G2Affine>;

Expand Down
18 changes: 16 additions & 2 deletions src/groth16/prover.rs
Expand Up @@ -10,7 +10,7 @@ use rayon::prelude::*;
use super::{ParameterSource, Proof};
use crate::domain::{EvaluationDomain, Scalar};
use crate::gpu::{LockedFFTKernel, LockedMultiexpKernel};
use crate::multicore::Worker;
use crate::multicore::{Worker, THREAD_POOL};
use crate::multiexp::{multiexp, DensityTracker, FullDensity};
use crate::{
Circuit, ConstraintSystem, Index, LinearCombination, SynthesisError, Variable, BELLMAN_VERSION,
Expand Down Expand Up @@ -180,7 +180,7 @@ where

pub fn create_proof_batch_priority<E, C, P: ParameterSource<E>>(
circuits: Vec<C>,
mut params: P,
params: P,
r_s: Vec<E::Fr>,
s_s: Vec<E::Fr>,
priority: bool,
Expand All @@ -191,6 +191,20 @@ where
{
info!("Bellperson {} is being used!", BELLMAN_VERSION);

THREAD_POOL.install(|| create_proof_batch_priority_inner(circuits, params, r_s, s_s, priority))
}

fn create_proof_batch_priority_inner<E, C, P: ParameterSource<E>>(
circuits: Vec<C>,
mut params: P,
r_s: Vec<E::Fr>,
s_s: Vec<E::Fr>,
priority: bool,
) -> Result<Vec<Proof<E>>, SynthesisError>
where
E: Engine,
C: Circuit<E> + Send,
{
let mut provers = circuits
.into_par_iter()
.map(|circuit| -> Result<_, SynthesisError> {
Expand Down
29 changes: 18 additions & 11 deletions src/multicore.rs
Expand Up @@ -10,9 +10,26 @@ mod implementation {
use crossbeam::{self, thread::Scope};
use futures::{Future, IntoFuture, Poll};
use futures_cpupool::{CpuFuture, CpuPool};
use lazy_static::lazy_static;
use num_cpus;
use std::env;

lazy_static! {
static ref NUM_CPUS: usize = if let Ok(num) = env::var("BELLMAN_NUM_CPUS") {
if let Ok(num) = num.parse() {
num
} else {
num_cpus::get()
}
} else {
num_cpus::get()
};
pub static ref THREAD_POOL: rayon::ThreadPool = rayon::ThreadPoolBuilder::new()
.num_threads(*NUM_CPUS)
.build()
.unwrap();
}

#[derive(Clone)]
pub struct Worker {
cpus: usize,
Expand All @@ -31,17 +48,7 @@ mod implementation {
}

pub fn new() -> Worker {
let cpus = if let Ok(num) = env::var("BELLMAN_NUM_CPUS") {
if let Ok(num) = num.parse() {
num
} else {
num_cpus::get()
}
} else {
num_cpus::get()
};

Self::new_with_cpus(cpus)
Self::new_with_cpus(*NUM_CPUS)
}

pub fn log_num_cpus(&self) -> u32 {
Expand Down

0 comments on commit 0c1868d

Please sign in to comment.