1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//! Lazy SMP: several threads deepen the same root, sharing one transposition table
//! (feature `parallel`).
use Vec;
use ;
use panic;
use thread;
use Budget;
use crate::;
/// Deepens `position` with `threads` threads that share `table` (Lazy SMP) and returns the
/// result of the thread that got furthest.
///
/// Every thread runs iterative deepening with `algorithm`; every other helper starts one
/// ply ahead, and each helper rotates every position's moves by its own index after the
/// game's ordering, so the threads search the tree in different orders instead of in
/// lockstep. They do not split the work explicitly: entries one thread stores answer or
/// order the others' searches. The search ends when the main thread is done, spending
/// `budget` as [`Searcher::deepen`] would, or when any thread reaches `max_depth` or proves
/// the score: every thread then stops, the main thread included. The result is a finished
/// thread's if there is one, the deepest thread's otherwise, ties going to the main thread;
/// a helper stopped before it finished its first iteration has no result.
///
/// The gain depends on what a node costs. In the `parallel` benchmarks, with an evaluation
/// of about a microsecond, eight threads finish in about 60% of one thread's time; with
/// nodes of a few dozen nanoseconds, the shared table's synchronisation eats the gain, so
/// measure it on the game at hand. With the default
/// [`Game::satisfices`](crate::Game::satisfices), the score equals a single-threaded
/// search's at the same depth, but the line, the root moves' bounds and the statistics,
/// which are the chosen thread's, vary from run to run.
///
/// `table` is a shared reference to a table for several threads: a
/// [`SharedTable`](crate::SharedTable), which takes any move and score, or a
/// [`PackedTable`](crate::PackedTable), lock-free for moves and scores that pack into a
/// few bits. It keeps its entries for later searches; clear it before searching a
/// different game. A `threads` of `0` runs one thread.
///
/// Available with the `parallel` feature, which needs `std`.
///
/// # Panics
///
/// Panics if a thread panics, for example in the game's own code; the first such panic is
/// passed on.
///
/// # Examples
///
/// ```
/// # use game_tree_search::{Game, Hashed, Side};
/// # struct Nim;
/// # impl Game for Nim {
/// # type Position = (u32, Side);
/// # type Move = u32;
/// # type Score = i32;
/// # fn side_to_move(&self, &(_, side): &Self::Position) -> Side { side }
/// # fn moves(&self, &(stones, _): &Self::Position, moves: &mut Vec<u32>) {
/// # moves.extend((1..=2).filter(|&take| take <= stones));
/// # }
/// # fn play(&self, &(stones, side): &Self::Position, take: &u32) -> Self::Position {
/// # (stones - take, if side == Side::Max { Side::Min } else { Side::Max })
/// # }
/// # fn evaluate(&self, &(stones, side): &Self::Position) -> i32 {
/// # match (stones, side) { (0, Side::Max) => -1, (0, Side::Min) => 1, _ => 0 }
/// # }
/// # }
/// # impl Hashed for Nim {
/// # type Key = u64;
/// # fn key(&self, &(stones, side): &Self::Position) -> u64 {
/// # u64::from(stones) * 2 + u64::from(side == Side::Max)
/// # }
/// # }
/// use game_tree_search::{Algorithm, SharedTable, Stats, deepen_shared};
///
/// let table = SharedTable::new(1 << 12, 8);
/// let unlimited = |_: &Stats| false;
/// let root = (20, Side::Max);
/// let result = deepen_shared(&Nim, &root, 21, (4, Algorithm::AlphaBeta), &table, unlimited);
/// assert_eq!(result.best(), Some(&2));
/// ```
/// Whether `result` answers the search: it reached `max_depth` or proved its score.
const
/// Tells the helper threads to stop when dropped.
;
/// The value of a thread that finished, or its panic passed on to the caller.
pub