From dad254a7bbbc6f366303fec00bbfeba1edcdd92e Mon Sep 17 00:00:00 2001 From: nemo Date: Thu, 18 Mar 2021 08:46:33 -0400 Subject: [PATCH 1/6] fix: remove deprecated calls to avoid compiler warnings --- .../src/stacked/vanilla/memory_handling.rs | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/storage-proofs-porep/src/stacked/vanilla/memory_handling.rs b/storage-proofs-porep/src/stacked/vanilla/memory_handling.rs index 327e4f963..04deb4761 100644 --- a/storage-proofs-porep/src/stacked/vanilla/memory_handling.rs +++ b/storage-proofs-porep/src/stacked/vanilla/memory_handling.rs @@ -30,6 +30,16 @@ struct IncrementingCursor { cur_safe: AtomicUsize, } +fn compare_and_swap(atomic: &AtomicUsize, before: usize, after: usize) -> usize { + match atomic.compare_exchange_weak(before, after, Ordering::SeqCst, Ordering::SeqCst) { + Ok(x) => { + assert_eq!(x, before); + before + } + _ => after, + } +} + /// IncrementingCursor provides an atomic variable which can be incremented such that only one thread attempting the /// increment is selected to perform actions required to effect the transition. Unselected threads wait until the /// transition has completed. Transition and wait condition are both specified by closures supplied by the caller. @@ -40,15 +50,17 @@ impl IncrementingCursor { cur_safe: AtomicUsize::new(val), } } + fn store(&self, val: usize) { self.cur.store(val, Ordering::SeqCst); self.cur_safe.store(val, Ordering::SeqCst); } + fn compare_and_swap(&self, before: usize, after: usize) { - self.cur.compare_and_swap(before, after, Ordering::SeqCst); - self.cur_safe - .compare_and_swap(before, after, Ordering::SeqCst); + compare_and_swap(&self.cur, before, after); + compare_and_swap(&self.cur_safe, before, after); } + fn increment bool, G: Fn()>(&self, target: usize, wait_fn: F, advance_fn: G) { // Check using `cur_safe`, to ensure we wait until the current cursor value is safe to use. // If we were to instead check `cur`, it could have been incremented but not yet safe. @@ -56,8 +68,7 @@ impl IncrementingCursor { if target > cur { // Only one producer will successfully increment `cur`. We need this second atomic because we cannot // increment `cur_safe` until after the underlying resource has been advanced. - let instant_cur = self.cur.compare_and_swap(cur, cur + 1, Ordering::SeqCst); - + let instant_cur = compare_and_swap(&self.cur, cur, cur + 1); if instant_cur == cur { // We successfully incremented `self.cur`, so we are responsible for advancing the resource. { @@ -132,9 +143,11 @@ impl CacheReader { pub unsafe fn increment_consumer(&self) { self.consumer.fetch_add(1, Ordering::SeqCst); } + pub fn store_consumer(&self, val: u64) { self.consumer.store(val, Ordering::SeqCst); } + pub fn get_consumer(&self) -> u64 { self.consumer.load(Ordering::SeqCst) } @@ -164,6 +177,7 @@ impl CacheReader { bufs[0] = buf0; Ok(()) } + pub fn finish_reset(&self) -> Result<()> { let buf1 = Self::map_buf(self.window_size as u64, self.window_size, &self.file)?; let bufs = unsafe { self.get_mut_bufs() }; From 6537ee1aed3dceda20dbf70952c35292e975c1df Mon Sep 17 00:00:00 2001 From: nemo Date: Thu, 18 Mar 2021 08:50:53 -0400 Subject: [PATCH 2/6] feat: remove unnecessary scope/spawn in gpu2 code feat: add some minor parallelization where we can --- filecoin-proofs/src/api/seal.rs | 3 +- storage-proofs-core/src/compound_proof.rs | 2 +- storage-proofs-porep/Cargo.toml | 1 + .../src/stacked/vanilla/proof.rs | 49 +++++++++---------- 4 files changed, 27 insertions(+), 28 deletions(-) diff --git a/filecoin-proofs/src/api/seal.rs b/filecoin-proofs/src/api/seal.rs index a898e0e49..376162e39 100644 --- a/filecoin-proofs/src/api/seal.rs +++ b/filecoin-proofs/src/api/seal.rs @@ -137,13 +137,12 @@ where base_tree_leafs, ); - // MT for original data is always named tree-d, and it will be - // referenced later in the process as such. let mut config = StoreConfig::new( cache_path.as_ref(), CacheKey::CommDTree.to_string(), default_rows_to_discard(base_tree_leafs, BINARY_ARITY), ); + let data_tree = create_base_merkle_tree::>( Some(config.clone()), base_tree_leafs, diff --git a/storage-proofs-core/src/compound_proof.rs b/storage-proofs-core/src/compound_proof.rs index 11bfbae86..524d046d4 100644 --- a/storage-proofs-core/src/compound_proof.rs +++ b/storage-proofs-core/src/compound_proof.rs @@ -264,7 +264,7 @@ where groth_proofs .into_iter() .map(|groth_proof| { - let mut proof_vec = vec![]; + let mut proof_vec = Vec::new(); groth_proof.write(&mut proof_vec)?; let gp = groth16::Proof::::read(&proof_vec[..])?; Ok(gp) diff --git a/storage-proofs-porep/Cargo.toml b/storage-proofs-porep/Cargo.toml index fb58028da..ca3c4a34c 100644 --- a/storage-proofs-porep/Cargo.toml +++ b/storage-proofs-porep/Cargo.toml @@ -15,6 +15,7 @@ storage-proofs-core = { path = "../storage-proofs-core", version = "^6.0.0", def sha2raw = { path = "../sha2raw", version = "^2.0.0"} filecoin-hashers = { path = "../filecoin-hashers", version = "1.0.0", default-features = false, features = ["poseidon", "sha256"]} rand = "0.7" +memmap = "0.7" merkletree = "0.21.0" mapr = "0.8.0" num-bigint = "0.2" diff --git a/storage-proofs-porep/src/stacked/vanilla/proof.rs b/storage-proofs-porep/src/stacked/vanilla/proof.rs index 84c960dca..3b28484bd 100644 --- a/storage-proofs-porep/src/stacked/vanilla/proof.rs +++ b/storage-proofs-porep/src/stacked/vanilla/proof.rs @@ -103,9 +103,12 @@ impl<'a, Tree: 'static + MerkleTreeTrait, G: 'static + Hasher> StackedDrg<'a, Tr let mut parents = vec![0; base_degree]; graph.base_parents(x, &mut parents)?; - for parent in &parents { - columns.push(t_aux.column(*parent)?); - } + columns.extend( + parents + .into_par_iter() + .map(|parent| t_aux.column(parent).expect("failed to get parent column")) + .collect::>>(), + ); debug_assert!(columns.len() == base_degree); @@ -116,7 +119,10 @@ impl<'a, Tree: 'static + MerkleTreeTrait, G: 'static + Hasher> StackedDrg<'a, Tr let mut parents = vec![0; graph.expansion_degree()]; graph.expanded_parents(x, &mut parents)?; - parents.iter().map(|parent| t_aux.column(*parent)).collect() + parents + .into_par_iter() + .map(|parent| t_aux.column(parent)) + .collect() }; (0..partition_count) @@ -194,7 +200,7 @@ impl<'a, Tree: 'static + MerkleTreeTrait, G: 'static + Hasher> StackedDrg<'a, Tr graph.base_parents(challenge, &mut parents)?; parents - .into_iter() + .into_par_iter() .map(|parent| t_aux.domain_node_at_layer(layer, parent)) .collect::>()? } else { @@ -203,7 +209,7 @@ impl<'a, Tree: 'static + MerkleTreeTrait, G: 'static + Hasher> StackedDrg<'a, Tr let base_parents_count = graph.base_graph().degree(); parents - .into_iter() + .into_par_iter() .enumerate() .map(|(i, parent)| { if i < base_parents_count { @@ -502,25 +508,18 @@ impl<'a, Tree: 'static + MerkleTreeTrait, G: 'static + Hasher> StackedDrg<'a, Tr layers ]; - rayon::scope(|s| { - // capture a shadowed version of layer_data. - let layer_data: &mut Vec<_> = &mut layer_data; - - // gather all layer data in parallel. - s.spawn(move |_| { - for (layer_index, mut layer_bytes) in - layer_data.iter_mut().enumerate() - { - let store = labels.labels_for_layer(layer_index + 1); - let start = (i * nodes_count) + node_index; - let end = start + chunked_nodes_count; - - store - .read_range_into(start, end, &mut layer_bytes) - .expect("failed to read store range"); - } - }); - }); + // gather all layer data. + for (layer_index, mut layer_bytes) in + layer_data.iter_mut().enumerate() + { + let store = labels.labels_for_layer(layer_index + 1); + let start = (i * nodes_count) + node_index; + let end = start + chunked_nodes_count; + + store + .read_range_into(start, end, &mut layer_bytes) + .expect("failed to read store range"); + } (0..chunked_nodes_count) .into_par_iter() From a70a946fe4193ee8557cf28df5efd6f6574f7a88 Mon Sep 17 00:00:00 2001 From: nemo Date: Thu, 18 Mar 2021 08:52:46 -0400 Subject: [PATCH 3/6] fix: revert added dep that's not yet needed --- storage-proofs-porep/Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/storage-proofs-porep/Cargo.toml b/storage-proofs-porep/Cargo.toml index ca3c4a34c..fb58028da 100644 --- a/storage-proofs-porep/Cargo.toml +++ b/storage-proofs-porep/Cargo.toml @@ -15,7 +15,6 @@ storage-proofs-core = { path = "../storage-proofs-core", version = "^6.0.0", def sha2raw = { path = "../sha2raw", version = "^2.0.0"} filecoin-hashers = { path = "../filecoin-hashers", version = "1.0.0", default-features = false, features = ["poseidon", "sha256"]} rand = "0.7" -memmap = "0.7" merkletree = "0.21.0" mapr = "0.8.0" num-bigint = "0.2" From 6e479b9ce24760993454056f2f560814f0b3647c Mon Sep 17 00:00:00 2001 From: nemo Date: Wed, 31 Mar 2021 11:14:24 -0400 Subject: [PATCH 4/6] fix: clean-up some additional warnings --- filecoin-proofs/src/api/post_util.rs | 2 +- storage-proofs-porep/src/stacked/vanilla/memory_handling.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/filecoin-proofs/src/api/post_util.rs b/filecoin-proofs/src/api/post_util.rs index 0960b2001..3a299c344 100644 --- a/filecoin-proofs/src/api/post_util.rs +++ b/filecoin-proofs/src/api/post_util.rs @@ -147,7 +147,7 @@ pub fn generate_single_vanilla_proof( let comm_c = replica.safe_comm_c(); let comm_r_last = replica.safe_comm_r_last(); - let mut priv_sectors = vec![fallback::PrivateSector { + let priv_sectors = vec![fallback::PrivateSector { tree, comm_c, comm_r_last, diff --git a/storage-proofs-porep/src/stacked/vanilla/memory_handling.rs b/storage-proofs-porep/src/stacked/vanilla/memory_handling.rs index 04deb4761..a404c29e5 100644 --- a/storage-proofs-porep/src/stacked/vanilla/memory_handling.rs +++ b/storage-proofs-porep/src/stacked/vanilla/memory_handling.rs @@ -3,7 +3,7 @@ use std::fs::File; use std::hint::spin_loop; use std::marker::{PhantomData, Sync}; use std::mem::size_of; -use std::path::{Path, PathBuf}; +use std::path::Path; use std::slice; use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering}; From 0ef9a64c955b73635ad83224911baadafc52e868 Mon Sep 17 00:00:00 2001 From: nemo Date: Thu, 1 Apr 2021 11:05:28 -0400 Subject: [PATCH 5/6] fix: apply review feedback --- storage-proofs-porep/src/stacked/vanilla/proof.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/storage-proofs-porep/src/stacked/vanilla/proof.rs b/storage-proofs-porep/src/stacked/vanilla/proof.rs index 3b28484bd..9f1b8f703 100644 --- a/storage-proofs-porep/src/stacked/vanilla/proof.rs +++ b/storage-proofs-porep/src/stacked/vanilla/proof.rs @@ -106,8 +106,8 @@ impl<'a, Tree: 'static + MerkleTreeTrait, G: 'static + Hasher> StackedDrg<'a, Tr columns.extend( parents .into_par_iter() - .map(|parent| t_aux.column(parent).expect("failed to get parent column")) - .collect::>>(), + .map(|parent| t_aux.column(parent)) + .collect::>>>()? ); debug_assert!(columns.len() == base_degree); From 8816ae8467c4458ffe2644b4d783f0e27f38fb47 Mon Sep 17 00:00:00 2001 From: nemo Date: Mon, 5 Apr 2021 08:02:58 -0400 Subject: [PATCH 6/6] style: rust fmt --- storage-proofs-porep/src/stacked/vanilla/proof.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/storage-proofs-porep/src/stacked/vanilla/proof.rs b/storage-proofs-porep/src/stacked/vanilla/proof.rs index 9f1b8f703..9189d6d7c 100644 --- a/storage-proofs-porep/src/stacked/vanilla/proof.rs +++ b/storage-proofs-porep/src/stacked/vanilla/proof.rs @@ -107,7 +107,7 @@ impl<'a, Tree: 'static + MerkleTreeTrait, G: 'static + Hasher> StackedDrg<'a, Tr parents .into_par_iter() .map(|parent| t_aux.column(parent)) - .collect::>>>()? + .collect::>>>()?, ); debug_assert!(columns.len() == base_degree);