Skip to content

Commit

Permalink
Improve UCI output (#90)
Browse files Browse the repository at this point in the history
No functional change.

Bench: 1733801
  • Loading branch information
jw1912 authored Jan 9, 2025
1 parent dae18b0 commit fee9c52
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/chess/attacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,15 @@ const BISHOP: [Mask; 64] = init!(|sq, 64|

const RANK_SHIFT: [usize; 64] = init!(|sq, 64| sq - (sq & 7) + 1);

const RANK: [[u64; 64]; 64] = init!(|sq, 64| init!(|occ, 64| {
static RANK: [[u64; 64]; 64] = init!(|sq, 64| init!(|occ, 64| {
let file = sq & 7;
let mask = (occ << 1) as u64;
let east = ((EAST[file] & mask) | (1 << 63)).trailing_zeros() as usize;
let west = ((WEST[file] & mask) | 1).leading_zeros() as usize ^ 63;
(EAST[file] ^ EAST[east] | WEST[file] ^ WEST[west]) << (sq - file)
}));

const FILE: [[u64; 64]; 64] = init!(|sq, 64| init!(|occ, 64| (RANK[7 - sq / 8][occ]
static FILE: [[u64; 64]; 64] = init!(|sq, 64| init!(|occ, 64| (RANK[7 - sq / 8][occ]
.wrapping_mul(DIAG)
& File::H)
>> (7 - (sq & 7))));
Expand Down
29 changes: 27 additions & 2 deletions src/mcts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub struct SearchStats {
pub total_iters: AtomicUsize,
pub main_iters: AtomicUsize,
pub avg_depth: AtomicUsize,
pub seldepth: AtomicUsize,
}

pub struct Searcher<'a> {
Expand Down Expand Up @@ -65,6 +66,7 @@ impl<'a> Searcher<'a> {
&self,
limits: &Limits,
timer: &Instant,
timer_last_output: &mut Instant,
search_stats: &SearchStats,
best_move: &mut Move,
best_move_changes: &mut i32,
Expand All @@ -75,6 +77,7 @@ impl<'a> Searcher<'a> {
self.check_limits(
limits,
timer,
timer_last_output,
search_stats,
best_move,
best_move_changes,
Expand Down Expand Up @@ -115,6 +118,9 @@ impl<'a> Searcher<'a> {
search_stats
.total_nodes
.fetch_add(this_depth, Ordering::Relaxed);
search_stats
.seldepth
.fetch_max(this_depth - 1, Ordering::Relaxed);
if main_thread {
search_stats.main_iters.fetch_add(1, Ordering::Relaxed);
}
Expand All @@ -140,6 +146,7 @@ impl<'a> Searcher<'a> {
&self,
limits: &Limits,
timer: &Instant,
timer_last_output: &mut Instant,
search_stats: &SearchStats,
best_move: &mut Move,
best_move_changes: &mut i32,
Expand Down Expand Up @@ -207,12 +214,27 @@ impl<'a> Searcher<'a> {
if uci_output {
self.search_report(
new_depth,
search_stats.seldepth.load(Ordering::Relaxed),
timer,
search_stats.total_nodes.load(Ordering::Relaxed),
);

*timer_last_output = Instant::now();
}
}

#[cfg(not(feature = "uci-minimal"))]
if uci_output && iters % 8192 == 0 && timer_last_output.elapsed().as_secs() >= 15 {
self.search_report(
search_stats.avg_depth.load(Ordering::Relaxed),
search_stats.seldepth.load(Ordering::Relaxed),
timer,
search_stats.total_nodes.load(Ordering::Relaxed),
);

*timer_last_output = Instant::now();
}

false
}

Expand All @@ -224,6 +246,7 @@ impl<'a> Searcher<'a> {
update_nodes: &mut usize,
) -> (Move, f32) {
let timer = Instant::now();
let mut timer_last_output = Instant::now();

let node = self.tree.root_node();

Expand Down Expand Up @@ -275,6 +298,7 @@ impl<'a> Searcher<'a> {
self.playout_until_full_main(
&limits,
&timer,
&mut timer_last_output,
&search_stats,
&mut best_move,
&mut best_move_changes,
Expand All @@ -299,6 +323,7 @@ impl<'a> Searcher<'a> {
if uci_output {
self.search_report(
search_stats.avg_depth.load(Ordering::Relaxed).max(1),
search_stats.seldepth.load(Ordering::Relaxed),
&timer,
search_stats.total_nodes.load(Ordering::Relaxed),
);
Expand Down Expand Up @@ -426,8 +451,8 @@ impl<'a> Searcher<'a> {
})
}

fn search_report(&self, depth: usize, timer: &Instant, nodes: usize) {
print!("info depth {depth} ");
fn search_report(&self, depth: usize, seldepth: usize, timer: &Instant, nodes: usize) {
print!("info depth {depth} seldepth {seldepth} ");
let (pv_line, score) = self.get_pv(depth);

if score > 1.0 {
Expand Down

0 comments on commit fee9c52

Please sign in to comment.