diff options
author | eug-vs <eugene@eug-vs.xyz> | 2023-02-24 17:35:35 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2023-02-24 17:35:35 +0300 |
commit | 10a68da1249931a6220ca06affa063c187836bbd (patch) | |
tree | 7c691ed0c37a369d41e286263d2758f0597e1906 /src/grossmeister/search.rs | |
parent | a7a26f7810e9bc5fec6020324a83a2a89b69ff79 (diff) | |
download | chessnost-10a68da1249931a6220ca06affa063c187836bbd.tar.gz |
feat: add initial multi-threaded UCI impl
Diffstat (limited to 'src/grossmeister/search.rs')
-rw-r--r-- | src/grossmeister/search.rs | 24 |
1 files changed, 12 insertions, 12 deletions
diff --git a/src/grossmeister/search.rs b/src/grossmeister/search.rs index ff6375c..20e1dd9 100644 --- a/src/grossmeister/search.rs +++ b/src/grossmeister/search.rs @@ -1,5 +1,7 @@ use std::cmp::Ordering; -use std::{time::{Instant, Duration}, f32::INFINITY}; +use std::sync::Arc; +use std::sync::atomic::AtomicBool; +use std::{time::Instant, f32::INFINITY}; use crate::{moves::{Move, MoveKind}, board::io::IO}; @@ -17,7 +19,7 @@ pub struct PerftResult { } impl Grossmeister { - pub fn negamax_search(&mut self, mut alpha: f32, beta: f32, depth_left: u8, parent_killers: &mut Vec<Move>, deadline: Instant) -> (f32, Vec<Move>) { + pub fn negamax_search(&mut self, mut alpha: f32, beta: f32, depth_left: u8, parent_killers: &mut Vec<Move>) -> (f32, Vec<Move>) { let mut principal_variation = Vec::new(); let mut killer_moves = Vec::new(); let color = self.board.color(); @@ -65,16 +67,16 @@ impl Grossmeister { let (mut score, mut subtree_pv) = if should_pv_search { // Assume PV-node is high in the list (if move ordering is good) - self.negamax_search(-beta, -alpha, depth_left - 1, &mut killer_moves, deadline) + self.negamax_search(-beta, -alpha, depth_left - 1, &mut killer_moves) } else { // After we have PV-node (that raised alpha) all other nodes will be searched // with zero-window first to confirm this assumption // TODO: changing 0.001 -> 0.0001 leads to a weird bug - let result = self.negamax_search(-(alpha + 0.001), -alpha, depth_left - 1, &mut killer_moves, deadline); + let result = self.negamax_search(-(alpha + 0.001), -alpha, depth_left - 1, &mut killer_moves); // In case some of the other nodes raises alpha, then it's true PV node now, // let's research with full window to find its exact value if -result.0 > alpha { - self.negamax_search(-beta, -alpha, depth_left - 1, &mut killer_moves, deadline) + self.negamax_search(-beta, -alpha, depth_left - 1, &mut killer_moves) } else { result } @@ -128,7 +130,7 @@ impl Grossmeister { } // Could not finish in time, return what we have so far - if Instant::now() > deadline { + if self.should_halt.load(std::sync::atomic::Ordering::Relaxed) { break; } } @@ -190,9 +192,7 @@ impl Grossmeister { alpha } - pub fn iterative_deepening(&mut self, max_depth: u8, duration: Duration, print: bool) -> (f32, Vec<Move>) { - let start = Instant::now(); - let deadline = start + duration; + pub fn iterative_deepening(&mut self, max_depth: u8, print: bool) -> (f32, Vec<Move>) { let mut result = None; let mut depth = 1; let mut alpha = -INFINITY; @@ -206,13 +206,13 @@ impl Grossmeister { if print { println!("\nSearching depth({}) in the window {:?}", depth, (alpha, beta)); } - let search_result = self.negamax_search(alpha, beta, depth, &mut root_killers, deadline); + let search_result = self.negamax_search(alpha, beta, depth, &mut root_killers); if search_result.0.abs() >= VALUE_WIN { return search_result } - if Instant::now() > deadline { + if self.should_halt.load(std::sync::atomic::Ordering::Relaxed) { if print { println!("Aborting..."); } @@ -220,7 +220,7 @@ impl Grossmeister { } if print { - println!("Finished depth({}) {:?} [{:?} left]", depth, search_result, deadline - Instant::now()); + println!("Finished depth({}) {:?}", depth, search_result); } if search_result.0 <= alpha { // Alpha-cutoff |