Explorar o código

examples/multithreaded_mining: use all available threads to initialize dataset

skoupidi hai 9 meses
pai
achega
49e59631db
Modificáronse 1 ficheiros con 34 adicións e 32 borrados
  1. 34 32
      examples/multithreaded_mining.rs

+ 34 - 32
examples/multithreaded_mining.rs

@@ -45,13 +45,42 @@ fn get_mining_flags() -> RandomXFlags {
     RandomXFlags::get_recommended_flags() | RandomXFlags::FULLMEM
 }
 
+fn init_dataset(flags: RandomXFlags, input: &[u8; 32]) -> RandomXDataset {
+    // Allocate cache and dataset
+    let cache = RandomXCache::new(flags, &input[..]).unwrap();
+    let dataset_item_count = RandomXDataset::count().unwrap();
+    let dataset = RandomXDataset::new(flags, cache, dataset_item_count).unwrap();
+
+    // Multithreaded dataset init using all available threads
+    let threads = thread::available_parallelism()
+        .map(|n| n.get())
+        .unwrap_or(1);
+    println!("Initializing RandomX dataset using {threads} threads...");
+    let mut handles = Vec::with_capacity(threads);
+    let threads_u32 = threads as u32;
+    let per_thread = dataset_item_count / threads_u32;
+    let remainder = dataset_item_count % threads_u32;
+    for t in 0..threads_u32 {
+        let dataset = dataset.clone();
+        let start_item = t * per_thread;
+        let count = per_thread + if t == threads_u32 - 1 { remainder } else { 0 };
+        handles.push(thread::spawn(move || {
+            dataset.init(start_item, count);
+        }));
+    }
+
+    // Wait for threads to finish setup
+    for handle in handles {
+        let _ = handle.join();
+    }
+    dataset
+}
+
 fn single_thread_mine(header: &mut Header, target: &BigUint, input: &[u8; 32]) {
     println!("Initializing RandomX cache and dataset...");
     let setup_start = Instant::now();
     let flags = get_mining_flags();
-    let cache = RandomXCache::new(flags, &input[..]).unwrap();
-    let dataset_item_count = RandomXDataset::count().unwrap();
-    let dataset = RandomXDataset::new_init(flags, cache, 0, dataset_item_count).unwrap();
+    let dataset = init_dataset(flags, input);
     println!("Setup time: {:?}", setup_start.elapsed());
 
     println!("Initializing RandomX VM...");
@@ -81,38 +110,11 @@ fn multi_thread_mine(threads: usize, header: &mut Header, target: &BigUint, inpu
     println!("Initializing RandomX cache and dataset...");
     let setup_start = Instant::now();
     let flags = get_mining_flags();
-    let cache = RandomXCache::new(flags, &input[..]).unwrap();
-    let dataset_item_count = RandomXDataset::count().unwrap();
-    let dataset = RandomXDataset::new(flags, cache, dataset_item_count).unwrap();
-
-    // Multithreaded dataset init
-    let mut handles = Vec::with_capacity(threads);
-    let threads_u32 = threads as u32;
-    let per_thread = dataset_item_count / threads_u32;
-    let remainder = dataset_item_count % threads_u32;
-    for t in 0..threads_u32 {
-        let dataset = dataset.clone();
-        let start_item = t * per_thread;
-        let count = per_thread + if t == threads_u32 - 1 { remainder } else { 0 };
-        handles.push(thread::spawn(move || {
-            println!("Initializing RandomX dataset for thread #{t}...");
-            let ds_start = Instant::now();
-            dataset.init(start_item, count);
-            println!(
-                "Initialized RandomX dataset for thread #{t} in {:?}",
-                ds_start.elapsed()
-            );
-        }));
-    }
-
-    // Wait for threads to finish setup
-    for handle in handles {
-        let _ = handle.join();
-    }
+    let dataset = init_dataset(flags, input);
     println!("Setup time: {:?}", setup_start.elapsed());
 
     println!("Initializing mining threads...");
-    handles = Vec::with_capacity(threads);
+    let mut handles = Vec::with_capacity(threads);
     let found_header = Arc::new(AtomicBool::new(false));
     let found_nonce = Arc::new(AtomicU64::new(0));
     let threads_u64 = threads as u64;