From 65a67a0323341f583d1578c0bbb2d05ee4f04039 Mon Sep 17 00:00:00 2001 From: Bartosz Nowak Date: Sun, 8 Sep 2024 21:45:27 +0200 Subject: [PATCH 1/2] cpu backend parallel grinding support --- crates/prover/src/core/backend/cpu/grind.rs | 31 ++++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/crates/prover/src/core/backend/cpu/grind.rs b/crates/prover/src/core/backend/cpu/grind.rs index c5d27a1da..28d317b90 100644 --- a/crates/prover/src/core/backend/cpu/grind.rs +++ b/crates/prover/src/core/backend/cpu/grind.rs @@ -1,18 +1,35 @@ +#[cfg(feature = "parallel")] +use rayon::prelude::*; + use super::CpuBackend; use crate::core::channel::Channel; use crate::core::proof_of_work::GrindOps; -impl GrindOps for CpuBackend { +impl GrindOps for CpuBackend +where + C: Channel + std::marker::Sync, +{ fn grind(channel: &C, pow_bits: u32) -> u64 { // TODO(spapini): This is a naive implementation. Optimize it. - let mut nonce = 0; - loop { + + let check_nonce = |nonce: &u64| { let mut channel = channel.clone(); - channel.mix_nonce(nonce); - if channel.trailing_zeros() >= pow_bits { - return nonce; + channel.mix_nonce(*nonce); + channel.trailing_zeros() >= pow_bits + }; + + { + let range = 0..u64::MAX; + #[cfg(not(feature = "parallel"))] + { + range.into_iter().find(check_nonce) + } + + #[cfg(feature = "parallel")] + { + range.into_par_iter().find_any(check_nonce) } - nonce += 1; } + .expect("Nonce not found") } } From 2ca63622747a58d896d010d3a966dc81748215c3 Mon Sep 17 00:00:00 2001 From: Bartosz Nowak Date: Sun, 8 Sep 2024 21:56:35 +0200 Subject: [PATCH 2/2] change grinding error message --- crates/prover/src/core/backend/cpu/grind.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/prover/src/core/backend/cpu/grind.rs b/crates/prover/src/core/backend/cpu/grind.rs index 28d317b90..b6d61b4d5 100644 --- a/crates/prover/src/core/backend/cpu/grind.rs +++ b/crates/prover/src/core/backend/cpu/grind.rs @@ -30,6 +30,6 @@ where range.into_par_iter().find_any(check_nonce) } } - .expect("Nonce not found") + .expect("Grind failed to find a solution.") } }