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
//! 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
/// main thread's result.
///
/// Every thread runs iterative deepening with `algorithm`; every other helper starts one
/// ply ahead. They do not split the work explicitly: entries one thread stores answer or
/// order the others' searches, and a helper running ahead has the main thread's next
/// iteration partly in the table before it starts. On the random test trees, eight threads
/// cut the main thread's positions by about 40%, but on searches of a few milliseconds the
/// thread start-up and lock contention cost more time than that saves: Lazy SMP pays off on
/// long searches, so measure it on the game at hand. The main thread spends
/// `budget` and decides the result, as [`Searcher::deepen`] would; the helpers stop when
/// it is done. 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 main thread's, vary from run to run.
///
/// `table` keeps its entries for later searches; clear it with [`SharedTable::clear`]
/// 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));
/// ```
/// Tells the helper threads to stop when dropped.
;
/// The value of a thread that finished, or its panic passed on to the caller.
pub