Skip to content

Commit 6d6edf0

Browse files
Fix time checking in multithreading (#797)
Elo | 6.12 +- 3.17 (95%) SPRT | 5.0+0.05s Threads=8 Hash=256MB LLR | 2.99 (-2.25, 2.89) [0.00, 3.00] Games | N: 10792 W: 2739 L: 2549 D: 5504 Penta | [8, 1126, 2946, 1300, 16] https://recklesschess.space/test/12455/ Elo | 2.58 +- 2.67 (95%) SPRT | 25.0+0.25s Threads=8 Hash=512MB LLR | 1.40 (-2.25, 2.89) [0.00, 3.00] Games | N: 13738 W: 3415 L: 3313 D: 7010 Penta | [1, 1397, 3971, 1499, 1] https://recklesschess.space/test/12461/ Bench: 2795637
1 parent 3eadf2a commit 6d6edf0

3 files changed

Lines changed: 24 additions & 27 deletions

File tree

src/search.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ impl NodeType for NonPV {
5353

5454
pub fn start(td: &mut ThreadData, report: Report, thread_count: usize) {
5555
td.completed_depth = 0;
56-
td.stopped = false;
5756

5857
td.pv_table.clear(0);
5958
td.nnue.full_refresh(&td.board);
@@ -130,7 +129,7 @@ pub fn start(td: &mut ThreadData, report: Report, thread_count: usize) {
130129

131130
td.root_moves[td.pv_index..td.pv_end].sort_by_key(|rm| std::cmp::Reverse(rm.score));
132131

133-
if td.stopped {
132+
if td.shared.status.get() == Status::STOPPED {
134133
break;
135134
}
136135

@@ -171,13 +170,15 @@ pub fn start(td: &mut ThreadData, report: Report, thread_count: usize) {
171170
}
172171
}
173172

174-
if !td.stopped {
173+
if td.shared.status.get() != Status::STOPPED {
175174
td.completed_depth = depth;
176175
}
177176

178177
if report == Report::Full
179-
&& !(is_loss(td.root_moves[0].display_score) && td.stopped)
180-
&& (td.stopped || td.pv_index + 1 == td.multi_pv || td.shared.nodes.aggregate() > 10_000_000)
178+
&& !(is_loss(td.root_moves[0].display_score) && td.shared.status.get() == Status::STOPPED)
179+
&& (td.shared.status.get() == Status::STOPPED
180+
|| td.pv_index + 1 == td.multi_pv
181+
|| td.shared.nodes.aggregate() > 10_000_000)
181182
{
182183
td.print_uci_info(depth);
183184
}
@@ -196,7 +197,10 @@ pub fn start(td: &mut ThreadData, report: Report, thread_count: usize) {
196197

197198
best_move_changes += td.best_move_changes;
198199

199-
if td.root_moves[0].score != -Score::INFINITE && is_loss(td.root_moves[0].score) && td.stopped {
200+
if td.root_moves[0].score != -Score::INFINITE
201+
&& is_loss(td.root_moves[0].score)
202+
&& td.shared.status.get() == Status::STOPPED
203+
{
200204
if let Some(pos) = td.root_moves.iter().position(|rm| rm.mv == last_best_rootmove.mv) {
201205
td.root_moves.remove(pos);
202206
td.root_moves.insert(0, last_best_rootmove.clone());
@@ -205,7 +209,7 @@ pub fn start(td: &mut ThreadData, report: Report, thread_count: usize) {
205209
last_best_rootmove = td.root_moves[0].clone();
206210
}
207211

208-
if td.stopped {
212+
if td.shared.status.get() == Status::STOPPED {
209213
break;
210214
}
211215

@@ -239,7 +243,6 @@ pub fn start(td: &mut ThreadData, report: Report, thread_count: usize) {
239243
}
240244

241245
if td.shared.status.get() == Status::STOPPED {
242-
td.stopped = true;
243246
break;
244247
}
245248
}
@@ -264,7 +267,7 @@ fn search<NODE: NodeType>(
264267
td.pv_table.clear(ply as usize);
265268
}
266269

267-
if td.stopped {
270+
if td.shared.status.get() == Status::STOPPED {
268271
return Score::ZERO;
269272
}
270273

@@ -284,8 +287,8 @@ fn search<NODE: NodeType>(
284287
td.sel_depth = td.sel_depth.max(ply as i32);
285288
}
286289

287-
if td.time_manager.check_time(td) {
288-
td.stopped = true;
290+
if td.id == 0 && td.time_manager.check_time(td) {
291+
td.shared.status.set(Status::STOPPED);
289292
return Score::ZERO;
290293
}
291294

@@ -553,7 +556,7 @@ fn search<NODE: NodeType>(
553556

554557
td.board.undo_null_move();
555558

556-
if td.stopped {
559+
if td.shared.status.get() == Status::STOPPED {
557560
return Score::ZERO;
558561
}
559562

@@ -566,7 +569,7 @@ fn search<NODE: NodeType>(
566569
let verified_score = search::<NonPV>(td, beta - 1, beta, depth - r, false, ply);
567570
td.nmp_min_ply = 0;
568571

569-
if td.stopped {
572+
if td.shared.status.get() == Status::STOPPED {
570573
return Score::ZERO;
571574
}
572575

@@ -617,7 +620,7 @@ fn search<NODE: NodeType>(
617620

618621
undo_move(td, mv);
619622

620-
if td.stopped {
623+
if td.shared.status.get() == Status::STOPPED {
621624
return Score::ZERO;
622625
}
623626

@@ -644,7 +647,7 @@ fn search<NODE: NodeType>(
644647
let score = search::<NonPV>(td, singular_beta - 1, singular_beta, singular_depth, cut_node, ply);
645648
td.stack[ply].excluded = Move::NULL;
646649

647-
if td.stopped {
650+
if td.shared.status.get() == Status::STOPPED {
648651
return Score::ZERO;
649652
}
650653

@@ -901,7 +904,7 @@ fn search<NODE: NodeType>(
901904

902905
undo_move(td, mv);
903906

904-
if td.stopped {
907+
if td.shared.status.get() == Status::STOPPED {
905908
return Score::ZERO;
906909
}
907910

@@ -1108,8 +1111,8 @@ fn qsearch<NODE: NodeType>(td: &mut ThreadData, mut alpha: i32, beta: i32, ply:
11081111
td.sel_depth = td.sel_depth.max(ply as i32);
11091112
}
11101113

1111-
if td.time_manager.check_time(td) {
1112-
td.stopped = true;
1114+
if td.id == 0 && td.time_manager.check_time(td) {
1115+
td.shared.status.set(Status::STOPPED);
11131116
return Score::ZERO;
11141117
}
11151118

@@ -1223,7 +1226,7 @@ fn qsearch<NODE: NodeType>(td: &mut ThreadData, mut alpha: i32, beta: i32, ply:
12231226

12241227
undo_move(td, mv);
12251228

1226-
if td.stopped {
1229+
if td.shared.status.get() == Status::STOPPED {
12271230
return Score::ZERO;
12281231
}
12291232

src/thread.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ pub struct ThreadData {
141141
pub continuation_corrhist: ContinuationCorrectionHistory,
142142
pub best_move_changes: usize,
143143
pub optimism: [i32; 2],
144-
pub stopped: bool,
145144
pub root_depth: i32,
146145
pub root_delta: i32,
147146
pub sel_depth: i32,
@@ -173,7 +172,6 @@ impl ThreadData {
173172
continuation_corrhist: ContinuationCorrectionHistory::default(),
174173
best_move_changes: 0,
175174
optimism: [0; 2],
176-
stopped: false,
177175
root_depth: 0,
178176
root_delta: 0,
179177
sel_depth: 0,

src/time.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::time::{Duration, Instant};
22

3-
use crate::thread::{Status, ThreadData};
3+
use crate::thread::ThreadData;
44

55
#[derive(Clone, Debug)]
66
pub enum Limits {
@@ -82,14 +82,10 @@ impl TimeManager {
8282
return false;
8383
}
8484

85-
if td.nodes() & 2047 == 2047 && td.shared.status.get() == Status::STOPPED {
86-
return true;
87-
}
88-
8985
match self.limits {
9086
Limits::Infinite | Limits::Depth(_) => false,
9187
Limits::Nodes(maximum) => td.shared.nodes.aggregate() > maximum,
92-
_ => td.id == 0 && td.nodes() & 2047 == 2047 && self.start_time.elapsed() >= self.hard_bound,
88+
_ => td.nodes() & 2047 == 2047 && self.start_time.elapsed() >= self.hard_bound,
9389
}
9490
}
9591

0 commit comments

Comments
 (0)