Compare commits

..

No commits in common. "8f631cb6b50e3a877f73f4126fab46ce16bd1979" and "9af5bc0269ce526aede15caba5aaa386a0c42deb" have entirely different histories.

2 changed files with 1 additions and 74 deletions

View File

@ -1,8 +0,0 @@
use powlib::{gen::{POWRange, POWChallenge}, solver::POWSolver};
fn main() {
let challenge = POWChallenge::make(POWRange::new(0, 20480));
let mut solver = POWSolver::new(challenge);
println!("Found {} with 8 threads", solver.solve_blocking(8, Some(|x| println!("{x}"))));
}

View File

@ -1,8 +1,5 @@
use std::{sync::{mpsc::{self, Receiver, SyncSender}, Arc, atomic::{AtomicBool, Ordering}}, thread::{self, JoinHandle}};
use crate::gen::POWChallenge;
use crate::{gen::POWChallenge, num::Num};
#[derive(Debug, Clone)]
pub struct POWSolver {
challenge: POWChallenge,
result: Option<u128>
@ -63,68 +60,6 @@ impl POWSolver {
return self.chunk_solve(self.challenge.range.min, self.challenge.range.max);
}
pub fn chunksize(self: &POWSolver, threads: u8) -> u128 {
let range = self.challenge.range.max - self.challenge.range.min;
range / threads as u128
}
pub fn solve_blocking(self: &mut POWSolver, threads: u8, callback: Option<fn(u128)>) -> u128 {
let (send, recv) = mpsc::sync_channel::<u128>(1);
let mut thread_start: u128 = self.challenge.range.min;
let size = self.chunksize(threads);
let mut handles: Vec<JoinHandle<()>> = vec![];
let stop = Arc::new(AtomicBool::new(false));
let range = self.challenge.range.max - self.challenge.range.min;
for i in 0..threads {
let solver = self.clone();
let send = send.clone();
let stop = stop.clone();
let mut end = thread_start + size;
if end > range {
end -= end % range;
}
handles.push(
thread::spawn(move || {
for j in thread_start..end {
if stop.load(Ordering::Relaxed) { break }
if callback.is_some() {
callback.unwrap()(j);
}
if solver.challenge.check(Num::from(j as u128)) {
stop.store(true, Ordering::Relaxed);
send.send(j as u128).unwrap();
}
}
}
)
);
thread_start = thread_start + size + 1;
}
let result = recv.recv().unwrap();
stop.load(Ordering::Relaxed);
loop {
let mut all_finished = true;
for handle in handles.iter() {
if !handle.is_finished() {
all_finished = false
}
}
if all_finished { break }
}
self.result = Some(result);
result
}
pub fn new(challenge: POWChallenge) -> POWSolver {
POWSolver { challenge, result: None }
}