From 10a68da1249931a6220ca06affa063c187836bbd Mon Sep 17 00:00:00 2001 From: eug-vs Date: Fri, 24 Feb 2023 17:35:35 +0300 Subject: feat: add initial multi-threaded UCI impl --- src/grossmeister/search.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'src/grossmeister/search.rs') 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, deadline: Instant) -> (f32, Vec) { + pub fn negamax_search(&mut self, mut alpha: f32, beta: f32, depth_left: u8, parent_killers: &mut Vec) -> (f32, Vec) { 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) { - let start = Instant::now(); - let deadline = start + duration; + pub fn iterative_deepening(&mut self, max_depth: u8, print: bool) -> (f32, Vec) { 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 -- cgit v1.2.3