-
Notifications
You must be signed in to change notification settings - Fork 5
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
feat!: add compile time flags to disable rayon #240
Changes from 7 commits
6fc91aa
5de5f06
8f18a55
03e8cdc
a721b20
ec30880
118c1c0
69c2c73
d5038a5
552eef8
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 |
---|---|---|
|
@@ -4,6 +4,8 @@ mod prover; | |
mod serialization; | ||
mod trusted_setup; | ||
mod verifier; | ||
#[macro_use] | ||
pub(crate) mod macros; | ||
|
||
pub use bls12_381::fixed_base_msm::UsePrecomp; | ||
// Exported types | ||
|
@@ -54,9 +56,12 @@ pub type CellIndex = kzg_multi_open::CosetIndex; | |
|
||
use constants::{BYTES_PER_BLOB, BYTES_PER_CELL, BYTES_PER_COMMITMENT}; | ||
use prover::ProverContext; | ||
use verifier::VerifierContext; | ||
|
||
#[cfg(feature = "multithreading")] | ||
use rayon::ThreadPool; | ||
#[cfg(feature = "multithreading")] | ||
use std::sync::Arc; | ||
use verifier::VerifierContext; | ||
|
||
/// ThreadCount indicates whether we want to use a single thread or multiple threads | ||
#[derive(Debug, Copy, Clone)] | ||
|
@@ -65,19 +70,23 @@ pub enum ThreadCount { | |
Single, | ||
/// Initializes the threadpool with the number of threads | ||
/// denoted by this enum variant. | ||
#[cfg(feature = "multithreading")] | ||
Multi(usize), | ||
/// Initializes the threadpool with a sensible default number of | ||
/// threads. This is currently set to `RAYON_NUM_THREADS`. | ||
#[cfg(feature = "multithreading")] | ||
SensibleDefault, | ||
} | ||
|
||
impl From<ThreadCount> for usize { | ||
fn from(value: ThreadCount) -> Self { | ||
match value { | ||
ThreadCount::Single => 1, | ||
#[cfg(feature = "multithreading")] | ||
ThreadCount::Multi(num_threads) => num_threads, | ||
// Setting this to `0` will tell ThreadPool to use | ||
// `RAYON_NUM_THREADS`. | ||
#[cfg(feature = "multithreading")] | ||
ThreadCount::SensibleDefault => 0, | ||
} | ||
} | ||
|
@@ -86,29 +95,37 @@ impl From<ThreadCount> for usize { | |
/// The context that will be used to create and verify opening proofs. | ||
#[derive(Debug)] | ||
pub struct DASContext { | ||
#[cfg(feature = "multithreading")] | ||
thread_pool: Arc<ThreadPool>, | ||
pub prover_ctx: ProverContext, | ||
pub verifier_ctx: VerifierContext, | ||
} | ||
|
||
#[cfg(feature = "multithreading")] | ||
impl Default for DASContext { | ||
fn default() -> Self { | ||
let trusted_setup = TrustedSetup::default(); | ||
const DEFAULT_NUM_THREADS: ThreadCount = ThreadCount::Single; | ||
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. Might make sense to make the default 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. In terms of defaults, single-thread, global threadpool and then a feature flag for local threadpool with threadcount might be the best, or just ask users to import threadpool in their own crate and set the threadpool amount themselves |
||
DASContext::with_threads(&trusted_setup, DEFAULT_NUM_THREADS, UsePrecomp::No) | ||
} | ||
} | ||
#[cfg(not(feature = "multithreading"))] | ||
impl Default for DASContext { | ||
fn default() -> Self { | ||
let trusted_setup = TrustedSetup::default(); | ||
|
||
DASContext::new(&trusted_setup, UsePrecomp::No) | ||
} | ||
} | ||
|
||
impl DASContext { | ||
#[cfg(feature = "multithreading")] | ||
pub fn with_threads( | ||
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. rename this to |
||
trusted_setup: &TrustedSetup, | ||
num_threads: ThreadCount, | ||
// This parameter indicates whether we should allocate memory | ||
// in order to speed up proof creation. Heuristics show that | ||
// if pre-computations are desired, one should set the | ||
// width value to `8` for optimal storage and performance tradeoffs. | ||
use_precomp: UsePrecomp, | ||
) -> Self { | ||
#[cfg(feature = "multithreading")] | ||
let thread_pool = std::sync::Arc::new( | ||
rayon::ThreadPoolBuilder::new() | ||
.num_threads(num_threads.into()) | ||
|
@@ -117,12 +134,28 @@ impl DASContext { | |
); | ||
|
||
DASContext { | ||
#[cfg(feature = "multithreading")] | ||
thread_pool, | ||
prover_ctx: ProverContext::new(trusted_setup, use_precomp), | ||
verifier_ctx: VerifierContext::new(trusted_setup), | ||
} | ||
} | ||
|
||
#[cfg(not(feature = "multithreading"))] | ||
pub fn new( | ||
trusted_setup: &TrustedSetup, | ||
// This parameter indicates whether we should allocate memory | ||
// in order to speed up proof creation. Heuristics show that | ||
// if pre-computations are desired, one should set the | ||
// width value to `8` for optimal storage and performance tradeoffs. | ||
use_precomp: UsePrecomp, | ||
) -> Self { | ||
DASContext { | ||
prover_ctx: ProverContext::new(trusted_setup, use_precomp), | ||
verifier_ctx: VerifierContext::new(trusted_setup), | ||
} | ||
} | ||
|
||
pub fn prover_ctx(&self) -> &ProverContext { | ||
&self.prover_ctx | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#[macro_export] | ||
macro_rules! with_optional_threadpool { | ||
($self:expr, $body:expr) => {{ | ||
#[cfg(feature = "multithreading")] | ||
{ | ||
$self.thread_pool.install(|| $body) | ||
} | ||
#[cfg(not(feature = "multithreading"))] | ||
{ | ||
$body | ||
} | ||
}}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "crate_crypto_internal_eth_kzg_maybe_rayon" | ||
description = "This crate provides an implementation of a wrapper around the rayon crate" | ||
version = { workspace = true } | ||
authors = { workspace = true } | ||
edition = { workspace = true } | ||
license = { workspace = true } | ||
rust-version = { workspace = true } | ||
repository = { workspace = true } | ||
|
||
[dependencies] | ||
rayon = { workspace = true, optional = true } | ||
|
||
[features] | ||
multithreading = ["rayon"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#[cfg(feature = "multithreading")] | ||
mod multi_threaded; | ||
#[cfg(not(feature = "multithreading"))] | ||
mod single_threaded; | ||
|
||
#[cfg(feature = "multithreading")] | ||
pub use multi_threaded::*; | ||
#[cfg(not(feature = "multithreading"))] | ||
pub use single_threaded::*; | ||
|
||
pub mod prelude { | ||
pub use crate::MaybeParallelRefExt; | ||
pub use crate::MaybeParallelRefMutExt; | ||
pub use crate::*; | ||
#[cfg(feature = "multithreading")] | ||
pub use rayon::prelude::*; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
pub use rayon::iter::IntoParallelIterator; | ||
pub use rayon::iter::IntoParallelRefIterator; | ||
pub use rayon::iter::IntoParallelRefMutIterator; | ||
pub use rayon::iter::ParallelIterator; | ||
|
||
pub trait MaybeParallelExt: IntoParallelIterator { | ||
fn maybe_into_par_iter(self) -> <Self as IntoParallelIterator>::Iter | ||
where | ||
Self: Sized, | ||
{ | ||
self.into_par_iter() | ||
} | ||
} | ||
|
||
pub trait MaybeParallelRefExt: for<'a> IntoParallelRefIterator<'a> { | ||
fn maybe_par_iter(&self) -> <Self as IntoParallelRefIterator>::Iter { | ||
self.par_iter() | ||
} | ||
} | ||
|
||
pub trait MaybeParallelRefMutExt: for<'a> IntoParallelRefMutIterator<'a> { | ||
fn maybe_par_iter_mut(&mut self) -> <Self as IntoParallelRefMutIterator>::Iter { | ||
self.par_iter_mut() | ||
} | ||
} | ||
|
||
impl<T: IntoParallelIterator> MaybeParallelExt for T {} | ||
impl<T: for<'a> IntoParallelRefIterator<'a>> MaybeParallelRefExt for T {} | ||
impl<T: for<'a> IntoParallelRefMutIterator<'a>> MaybeParallelRefMutExt for T {} |
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.
Probably should standardize this to
single-thread
,multi-thread