Skip to content
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

Parallel chiapos #1453

Merged
merged 4 commits into from
May 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/subspace-core-primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ kzg = { git = "https://github.com/subspace/rust-kzg", rev = "1058cc8c8af8461b490
num-traits = { version = "0.2.15", default-features = false }
parity-scale-codec = { version = "3.4.0", default-features = false, features = ["derive", "max-encoded-len"] }
parking_lot = { version = "0.12.1", optional = true }
rayon = { version = "1.6.1", optional = true }
rayon = { version = "1.7.0", optional = true }
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
serde = { version = "1.0.159", optional = true, features = ["alloc", "derive"] }
serde_arrays = { version = "0.1.0", optional = true }
Expand Down
4 changes: 2 additions & 2 deletions crates/subspace-farmer-components/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ lru = "0.10.0"
parity-scale-codec = "3.4.0"
parking_lot = "0.12.1"
rand = "0.8.5"
rayon = "1.6.1"
rayon = "1.7.0"
schnorrkel = "0.9.1"
serde = { version = "1.0.159", features = ["derive"] }
static_assertions = "1.1.0"
subspace-archiving = { version = "0.1.0", path = "../subspace-archiving" }
subspace-core-primitives = { version = "0.1.0", path = "../subspace-core-primitives" }
subspace-erasure-coding = { version = "0.1.0", path = "../subspace-erasure-coding" }
subspace-proof-of-space = { version = "0.1.0", path = "../subspace-proof-of-space" }
subspace-proof-of-space = { version = "0.1.0", path = "../subspace-proof-of-space", features = ["parallel"] }
subspace-verification = { version = "0.1.0", path = "../subspace-verification" }
thiserror = "1.0.38"
tokio = { version = "1.27.0", features = ["macros", "parking_lot", "rt-multi-thread", "signal", "sync"] }
Expand Down
4 changes: 1 addition & 3 deletions crates/subspace-farmer-components/src/proving.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,8 @@ where
}
}

// TODO: Can potentially happen in parallel with other work below, saving a bit of
// end-to-end latency
// Derive PoSpace table
let pos_table = PosTable::generate(
let pos_table = PosTable::generate_parallel(
&self
.sector_id
.evaluation_seed(piece_offset, self.sector_metadata.history_size),
Expand Down
2 changes: 1 addition & 1 deletion crates/subspace-farmer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ zeroize = "1.6.0"
jemallocator = "0.5.0"

[dev-dependencies]
rayon = "1.6.1"
rayon = "1.7.0"
4 changes: 4 additions & 0 deletions crates/subspace-proof-of-space/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ bench = false
bitvec = { version = "1.0.1", default-features = false, features = ["alloc", "atomic"], optional = true }
blake3 = { version = "1.3.3", default-features = false, optional = true }
chacha20 = { version = "0.9.1", default-features = false, optional = true }
rayon = { version = "1.7.0", optional = true }
subspace-chiapos = { git = "https://github.com/subspace/chiapos", rev = "3b1ab3ca24764d25da30e0c8243e0bf304b776a5", optional = true }
subspace-core-primitives = { version = "0.1.0", path = "../subspace-core-primitives", default-features = false }

Expand Down Expand Up @@ -52,6 +53,9 @@ std = [
"chacha20?/std",
"subspace-core-primitives/std",
]
parallel = [
"dep:rayon",
]
# Enable Chia proof of space support (legacy implementation uses C++ chiapos), only works in `std` environment for now
chia-legacy = [
"std",
Expand Down
22 changes: 21 additions & 1 deletion crates/subspace-proof-of-space/benches/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#[cfg(any(feature = "chia-legacy", feature = "chia", feature = "shim"))]
use criterion::black_box;
use criterion::{criterion_group, criterion_main, Criterion};
#[cfg(feature = "parallel")]
use rayon::ThreadPoolBuilder;
#[cfg(any(feature = "chia-legacy", feature = "chia", feature = "shim"))]
use subspace_core_primitives::PosSeed;
#[cfg(any(feature = "chia-legacy", feature = "chia", feature = "shim"))]
Expand All @@ -23,14 +25,32 @@ fn pos_bench<PosTable>(
) where
PosTable: Table,
{
#[cfg(feature = "parallel")]
{
// Repeated initialization is not supported, we just ignore errors here because of it
let _ = ThreadPoolBuilder::new()
// Change number of threads if necessary
.num_threads(4)
.build_global();
}

let mut group = c.benchmark_group(name);

group.bench_function("table", |b| {
group.bench_function("table/single", |b| {
b.iter(|| {
PosTable::generate(black_box(&SEED));
});
});

#[cfg(feature = "parallel")]
{
group.bench_function("table/parallel", |b| {
b.iter(|| {
PosTable::generate_parallel(black_box(&SEED));
});
});
}

let table = PosTable::generate(&SEED);

group.bench_function("quality/no-solution", |b| {
Expand Down
18 changes: 18 additions & 0 deletions crates/subspace-proof-of-space/src/chia.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Chia proof of space implementation
use crate::chiapos::Tables;
#[cfg(any(feature = "parallel", test))]
use crate::chiapos::TablesCache;
use crate::{PosTableType, Quality, Table};
use core::mem;
use subspace_core_primitives::{PosProof, PosQualityBytes, PosSeed};
Expand Down Expand Up @@ -50,6 +52,13 @@ impl Table for ChiaTable {
}
}

#[cfg(any(feature = "parallel", test))]
fn generate_parallel(seed: &PosSeed) -> ChiaTable {
Self {
tables: Tables::<K>::create_parallel((*seed).into(), &mut TablesCache::default()),
}
}

fn find_quality(&self, challenge_index: u32) -> Option<Self::Quality<'_>> {
let mut challenge = [0; 32];
challenge[..mem::size_of::<u32>()].copy_from_slice(&challenge_index.to_le_bytes());
Expand Down Expand Up @@ -84,12 +93,21 @@ mod tests {
#[test]
fn basic() {
let table = ChiaTable::generate(&SEED);
let table_parallel = ChiaTable::generate_parallel(&SEED);

assert!(table.find_quality(1232460437).is_none());
assert!(table_parallel.find_quality(1232460437).is_none());

{
let challenge_index = 124537303;
let quality = table.find_quality(challenge_index).unwrap();
assert_eq!(
quality.to_bytes(),
table_parallel
.find_quality(challenge_index)
.unwrap()
.to_bytes()
);
let proof = quality.create_proof();
let maybe_quality = ChiaTable::is_proof_valid(&SEED, challenge_index, &proof);
assert_eq!(maybe_quality, Some(quality.to_bytes()));
Expand Down
13 changes: 12 additions & 1 deletion crates/subspace-proof-of-space/src/chiapos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ macro_rules! impl_any {
($($k: expr$(,)? )*) => {
$(
impl Tables<$k> {
/// Create Chia proof of space tables.
/// Create Chia proof of space tables. There also exists [`Self::create_parallel()`] that trades
/// CPU efficiency and memory usage for lower latency.
///
/// Advanced version of [`Self::create_simple`] that allows to reuse cache.
pub fn create(seed: Seed, cache: &mut TablesCache<$k>) -> Self {
Expand All @@ -46,6 +47,16 @@ impl Tables<$k> {
))
}

/// Almost the same as [`Self::create()`], but uses parallelism internally for better
/// performance (though not efficiency of CPU and memory usage), if you create multiple tables
/// in parallel, prefer [`Self::create()`] for better overall performance.
#[cfg(any(feature = "parallel", test))]
pub fn create_parallel(seed: Seed, cache: &mut TablesCache<$k>) -> Self {
Self(TablesGeneric::<$k>::create_parallel(
seed, cache,
))
}

/// Create Chia proof of space tables.
///
/// Simpler version of [`Self::create`].
Expand Down
Loading