Skip to content

Commit

Permalink
use multithread and singlethreaded
Browse files Browse the repository at this point in the history
  • Loading branch information
kevaundray committed Aug 27, 2024
1 parent 118c1c0 commit 69c2c73
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion bindings/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ napi = { version = "2.12.2", default-features = false, features = [
"async",
] }
napi-derive = "2.12.2"
rust_eth_kzg = { workspace = true, features = ["multithreading"] }
rust_eth_kzg = { workspace = true, features = ["multithreaded"] }

[build-dependencies]
napi-build = "2.0.1"
4 changes: 2 additions & 2 deletions cryptography/kzg_multi_open/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ criterion = "0.5.1"
rand = "0.8.4"

[features]
singlethreading = ["bls12_381/blst-no-threads"]
multithreading = ["maybe_rayon/multithreading"]
singlethreaded = ["bls12_381/blst-no-threads"]
multithreaded = ["maybe_rayon/multithreaded"]

[[bench]]
name = "benchmark"
Expand Down
4 changes: 2 additions & 2 deletions eip7594/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"

[features]
multithreading = ["rayon"]
multithreaded = ["rayon"]

[dev-dependencies]
criterion = "0.5.1"
Expand All @@ -31,4 +31,4 @@ serde_yaml = "0.9.34"
[[bench]]
name = "benchmark"
harness = false
required-features = ["multithreading"]
required-features = ["multithreaded"]
26 changes: 13 additions & 13 deletions eip7594/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ use constants::{BYTES_PER_BLOB, BYTES_PER_CELL, BYTES_PER_COMMITMENT};
use prover::ProverContext;
use verifier::VerifierContext;

#[cfg(feature = "multithreading")]
#[cfg(feature = "multithreaded")]
use rayon::ThreadPool;
#[cfg(feature = "multithreading")]
#[cfg(feature = "multithreaded")]
use std::sync::Arc;

/// ThreadCount indicates whether we want to use a single thread or multiple threads
Expand All @@ -70,23 +70,23 @@ pub enum ThreadCount {
Single,
/// Initializes the threadpool with the number of threads
/// denoted by this enum variant.
#[cfg(feature = "multithreading")]
#[cfg(feature = "multithreaded")]
Multi(usize),
/// Initializes the threadpool with a sensible default number of
/// threads. This is currently set to `RAYON_NUM_THREADS`.
#[cfg(feature = "multithreading")]
#[cfg(feature = "multithreaded")]
SensibleDefault,
}

impl From<ThreadCount> for usize {
fn from(value: ThreadCount) -> Self {
match value {
ThreadCount::Single => 1,
#[cfg(feature = "multithreading")]
#[cfg(feature = "multithreaded")]
ThreadCount::Multi(num_threads) => num_threads,
// Setting this to `0` will tell ThreadPool to use
// `RAYON_NUM_THREADS`.
#[cfg(feature = "multithreading")]
#[cfg(feature = "multithreaded")]
ThreadCount::SensibleDefault => 0,
}
}
Expand All @@ -95,21 +95,21 @@ 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")]
#[cfg(feature = "multithreaded")]
thread_pool: Arc<ThreadPool>,
pub prover_ctx: ProverContext,
pub verifier_ctx: VerifierContext,
}

#[cfg(feature = "multithreading")]
#[cfg(feature = "multithreaded")]
impl Default for DASContext {
fn default() -> Self {
let trusted_setup = TrustedSetup::default();
const DEFAULT_NUM_THREADS: ThreadCount = ThreadCount::Single;
DASContext::with_threads(&trusted_setup, DEFAULT_NUM_THREADS, UsePrecomp::No)
}
}
#[cfg(not(feature = "multithreading"))]
#[cfg(not(feature = "multithreaded"))]
impl Default for DASContext {
fn default() -> Self {
let trusted_setup = TrustedSetup::default();
Expand All @@ -119,13 +119,13 @@ impl Default for DASContext {
}

impl DASContext {
#[cfg(feature = "multithreading")]
#[cfg(feature = "multithreaded")]
pub fn with_threads(
trusted_setup: &TrustedSetup,
num_threads: ThreadCount,
use_precomp: UsePrecomp,
) -> Self {
#[cfg(feature = "multithreading")]
#[cfg(feature = "multithreaded")]
let thread_pool = std::sync::Arc::new(
rayon::ThreadPoolBuilder::new()
.num_threads(num_threads.into())
Expand All @@ -134,14 +134,14 @@ impl DASContext {
);

DASContext {
#[cfg(feature = "multithreading")]
#[cfg(feature = "multithreaded")]
thread_pool,
prover_ctx: ProverContext::new(trusted_setup, use_precomp),
verifier_ctx: VerifierContext::new(trusted_setup),
}
}

#[cfg(not(feature = "multithreading"))]
#[cfg(not(feature = "multithreaded"))]
pub fn new(
trusted_setup: &TrustedSetup,
// This parameter indicates whether we should allocate memory
Expand Down
4 changes: 2 additions & 2 deletions eip7594/src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#[macro_export]
macro_rules! with_optional_threadpool {
($self:expr, $body:expr) => {{
#[cfg(feature = "multithreading")]
#[cfg(feature = "multithreaded")]
{
$self.thread_pool.install(|| $body)
}
#[cfg(not(feature = "multithreading"))]
#[cfg(not(feature = "multithreaded"))]
{
$body
}
Expand Down
2 changes: 1 addition & 1 deletion maybe_rayon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ repository = { workspace = true }
rayon = { workspace = true, optional = true }

[features]
multithreading = ["rayon"]
multithreaded = ["rayon"]
10 changes: 5 additions & 5 deletions maybe_rayon/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#[cfg(feature = "multithreading")]
#[cfg(feature = "multithreaded")]
mod multi_threaded;
#[cfg(not(feature = "multithreading"))]
#[cfg(not(feature = "multithreaded"))]
mod single_threaded;

#[cfg(feature = "multithreading")]
#[cfg(feature = "multithreaded")]
pub use multi_threaded::*;
#[cfg(not(feature = "multithreading"))]
#[cfg(not(feature = "multithreaded"))]
pub use single_threaded::*;

pub mod prelude {
pub use crate::MaybeParallelRefExt;
pub use crate::MaybeParallelRefMutExt;
pub use crate::*;
#[cfg(feature = "multithreading")]
#[cfg(feature = "multithreaded")]
pub use rayon::prelude::*;
}

0 comments on commit 69c2c73

Please sign in to comment.