Skip to content

Commit

Permalink
According cpu cores to decide how many coins in one thread
Browse files Browse the repository at this point in the history
Signed-off-by: Dengjianping <[email protected]>
  • Loading branch information
Dengjianping committed Aug 21, 2023
1 parent 6d6c143 commit 6209468
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 23 deletions.
16 changes: 4 additions & 12 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions pallets/manta-sbt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ precompute-coins = [
"manta-pay/bip32",
"manta-pay/arkworks",
"manta-util/std",
"num_cpus",
"rand_chacha",
"tempfile",
]
Expand All @@ -95,6 +96,7 @@ anyhow = { version = "1.0.55", optional = true, default-features = false }
base64 = { version = "0.20", default-features = false, features = ["alloc"] }
indoc = { version = "2.0.1", optional = true, default-features = false }
libsecp256k1 = { version = "0.7", default-features = false, features = ["hmac", "static-context"], optional = true }
num_cpus = { version = "1.16.0", optional = true }
rand_chacha = { version = "0.3.1", optional = true, default-features = false }
sha3 = { version = "0.10.6", default-features = false }
tempfile = { version = "3.3.0", optional = true, default-features = false }
Expand Down
89 changes: 78 additions & 11 deletions pallets/manta-sbt/src/bin/precompute_coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,74 @@ macro_rules! write_const_array {
/// Builds sample transactions for testing.
#[inline]
fn main() -> Result<()> {
let target_file = env::args()
.nth(1)
.map(PathBuf::from)
.unwrap_or(env::current_dir()?.join("precomputed_coins.rs"));
assert!(
!target_file.exists(),
"Specify a file to place the generated files: {target_file:?}.",
);
fs::create_dir_all(
target_file
.parent()
.expect("This file should have a parent."),
)?;

let directory = tempfile::tempdir().expect("Unable to generate temporary test directory.");
println!("[INFO] Temporary Directory: {directory:?}");

let mut rng = ChaCha20Rng::from_seed([0; 32]);
let (proving_context, _, parameters, utxo_accumulator_model) =
load_parameters(directory.path()).expect("Unable to load parameters.");
let mut utxo_accumulator = UtxoAccumulator::new(utxo_accumulator_model);
let asset_id = 1.into();

let to_private = to_private_example(
&proving_context.to_private,
&parameters,
&mut utxo_accumulator,
asset_id,
1,
&mut rng,
);

let mut target_file = OpenOptions::new()
.create_new(true)
.write(true)
.open(target_file)?;

writeln!(
target_file,
indoc! {r"
// Copyright 2020-2023 Manta Network.
// This file is part of Manta.
//
// Manta is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Manta is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Manta. If not, see <http://www.gnu.org/licenses/>.
//! Precomputed Coins
//!
//! THIS FILE IS AUTOMATICALLY GENERATED by `src/bin/precompute_coins.rs`. DO NOT EDIT.
"}
)?;

write_const_array!(target_file, TO_PRIVATE, to_private)?;
Ok(directory.close()?)
}

#[allow(dead_code)]
fn generate_coins_by_multithreading() -> Result<()> {
use std::time::Instant;
let now = Instant::now();

Expand All @@ -101,15 +169,17 @@ fn main() -> Result<()> {
let directory = tempfile::tempdir().expect("Unable to generate temporary test directory.");
println!("[INFO] Temporary Directory: {directory:?}");

let mut rng = Arc::new(ChaCha20Rng::from_seed([0; 32]));
let rng = Arc::new(ChaCha20Rng::from_seed([0; 32]));
let (proving_context, _, parameters, utxo_accumulator_model) =
load_parameters(directory.path()).expect("Unable to load parameters.");

let step = 20;
let cpu_num = num_cpus::get() as u128;
let total_coins_to_be_minted = 1024;
let batch = total_coins_to_be_minted / cpu_num; // how many coins in one thread
let mut threads = vec![];
for i in 0..4 {
let start = i * step;
let end = step + start;
for i in 0..cpu_num {
let start = i * batch;
let end = batch + start;
let utxo = utxo_accumulator_model.clone();
let mut utxo_accumulator = Arc::new(UtxoAccumulator::new(utxo));
let mut r = rng.clone();
Expand All @@ -132,22 +202,19 @@ fn main() -> Result<()> {
mints.push(to_private.clone());
println!("to_private size: {:?}", to_private.encode().len());
}
let path = format!(
"/home/jamie/my-repo/Manta/pallets/manta-sbt/precomputed_mints-{}-to_{}",
start, end
);
let path = format!("precomputed_mints-{}-to_{}", start, end);
let mut file = File::create(path).unwrap();
file.write_all(&<[TransferPost]>::encode(&mints)).unwrap();
});
threads.push(thread_join_handle);
}

for t in threads {
t.join();
let _ = t.join();
}

let elapsed = now.elapsed();
println!("Elapsed: {:.2?}", elapsed);

Ok(directory.close()?)
Ok(())
}

0 comments on commit 6209468

Please sign in to comment.