Эх сурвалжийг харах

fixed multithreaded mining dataset init and droped subset_init fn

skoupidi 9 сар өмнө
parent
commit
8faed6f02a

+ 22 - 13
examples/multithreaded_mining.rs

@@ -1,7 +1,6 @@
 //! randomx example that mines a dummy header using multiple threads
 
 use std::{
-    collections::VecDeque,
     sync::{
         atomic::{AtomicBool, AtomicU64, Ordering},
         Arc,
@@ -85,25 +84,35 @@ fn multi_thread_mine(threads: usize, header: &mut Header, target: &BigUint, inpu
     let cache = RandomXCache::new(flags, &input[..]).unwrap();
     let dataset_item_count = RandomXDataset::count().unwrap();
     let dataset = RandomXDataset::new(flags, cache, dataset_item_count).unwrap();
-    let mut subsets = VecDeque::with_capacity(threads);
 
     // 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 {
-        println!("Initializing RandomX dataset for thread #{t}...");
-        let ds_start = Instant::now();
-        let a = (dataset_item_count * t) / threads_u32;
-        let b = (dataset_item_count * (t + 1)) / threads_u32;
-        subsets.push_back(dataset.subset_init(a, b - a));
-        println!(
-            "Initialized RandomX dataset for thread #{t} in {:?}",
-            ds_start.elapsed()
-        );
+        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();
     }
     println!("Setup time: {:?}", setup_start.elapsed());
 
     println!("Initializing mining threads...");
-    let mut handles = Vec::with_capacity(threads);
+    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;
@@ -119,7 +128,7 @@ fn multi_thread_mine(threads: usize, header: &mut Header, target: &BigUint, inpu
         thread_header.nonce = t;
         let found_header = Arc::clone(&found_header);
         let found_nonce = Arc::clone(&found_nonce);
-        let dataset = subsets.pop_front().unwrap();
+        let dataset = dataset.clone();
 
         handles.push(thread::spawn(move || {
             println!("Initializing RandomX VM #{t}...");

+ 0 - 11
src/lib.rs

@@ -305,17 +305,6 @@ impl RandomXDataset {
         Ok(dataset)
     }
 
-    /// Creates a new dataset subset and initializes it.
-    ///
-    /// `start_item` is the item number where initialization should start.
-    ///
-    /// `item_count` is the total item count in the subset.
-    pub fn subset_init(&self, start_item: u32, item_count: u32) -> Self {
-        let subset = self.clone();
-        subset.init(start_item, item_count);
-        subset
-    }
-
     /// Returns the number of items in the `dataset` or an error on failure.
     pub fn count() -> Result<u32, RandomXError> {
         match unsafe { randomx_dataset_item_count() } {