@@ -46,11 +46,20 @@ public void Stop()
46
46
}
47
47
}
48
48
49
+ public void CancelSearch ( )
50
+ {
51
+ foreach ( var searcher in Searchers )
52
+ {
53
+ searcher . Stop ( ) ;
54
+ }
55
+ }
56
+
49
57
public ( List < uint > pv , int depthSearched , int score , long nodes , TimeSpan duration ) NodeBoundSearch (
50
58
GameState state , int nodeLimit = 0 , int maxDepth = 0 )
51
59
{
52
60
var start = DateTime . Now ;
53
- var searchResult = Searchers [ 0 ] . Search ( state , nodeLimit : nodeLimit , depthLimit : maxDepth ) ;
61
+ Searchers [ 0 ] . Reset ( state ) ;
62
+ var searchResult = Searchers [ 0 ] . Search ( state , CancelSearch , nodeLimit : nodeLimit , depthLimit : maxDepth ) ;
54
63
return ( searchResult . pv , searchResult . depthSearched , searchResult . score ,
55
64
searchResult . nodes , DateTime . Now - start ) ;
56
65
}
@@ -69,7 +78,6 @@ public void Stop()
69
78
// Prevent a previous searches timeout cancelling a new search
70
79
return ;
71
80
}
72
-
73
81
// Stop all searchers once think time has been reached
74
82
foreach ( var searcher in Searchers )
75
83
{
@@ -79,10 +87,12 @@ public void Stop()
79
87
}
80
88
81
89
var start = DateTime . Now ;
90
+ DateTime ? end = thinkTime > 0 ? DateTime . Now . AddMilliseconds ( thinkTime ) : null ;
82
91
83
92
if ( Searchers . Count == 1 )
84
93
{
85
- var searchResult = Searchers [ 0 ] . Search ( state , writeInfo : true ) ;
94
+ Searchers [ 0 ] . Reset ( state ) ;
95
+ var searchResult = Searchers [ 0 ] . Search ( state , CancelSearch , timeLimit : end , threadId : 0 ) ;
86
96
return ( searchResult . pv , searchResult . depthSearched , searchResult . score ,
87
97
searchResult . nodes , DateTime . Now - start ) ;
88
98
}
@@ -92,10 +102,28 @@ public void Stop()
92
102
new ThreadLocal < ( List < uint > move , int depthSearched , int score , long nodes ) > (
93
103
( ) => ( new List < uint > ( ) , 0 , int . MinValue , 0 ) , true ) ;
94
104
105
+ foreach ( var searcher in Searchers )
106
+ {
107
+ searcher . Reset ( state ) ;
108
+ }
95
109
96
110
// Parallel search, with thread-local best move
97
- Parallel . For ( 0 , Searchers . Count ,
98
- i => { results . Value = Searchers [ i ] . Search ( state , searchers : Searchers , writeInfo : i == 0 ) ; } ) ;
111
+ var threads = new Thread [ Searchers . Count ] ;
112
+ for ( int i = 0 ; i < Searchers . Count ; i ++ )
113
+ {
114
+ int threadId = i ;
115
+ threads [ i ] = new Thread ( ( ) =>
116
+ {
117
+ results . Value = Searchers [ threadId ] . Search ( state , CancelSearch , timeLimit : end , threadId : threadId ) ;
118
+ } ) ;
119
+ threads [ i ] . Start ( ) ;
120
+ }
121
+
122
+ // Wait for all to complete
123
+ foreach ( var thread in threads )
124
+ {
125
+ thread . Join ( ) ;
126
+ }
99
127
100
128
var dt = DateTime . Now - start ;
101
129
@@ -120,7 +148,8 @@ public void Stop()
120
148
121
149
if ( resultList . Count == 0 )
122
150
{
123
- var searchResult = Searchers [ 0 ] . Search ( state , depthLimit : 0 , writeInfo : true ) ;
151
+ Searchers [ 0 ] . Reset ( state ) ;
152
+ var searchResult = Searchers [ 0 ] . Search ( state , CancelSearch , depthLimit : 0 , threadId : 0 ) ;
124
153
return ( searchResult . pv , searchResult . depthSearched , searchResult . score ,
125
154
searchResult . nodes , DateTime . Now - start ) ;
126
155
}
@@ -161,17 +190,22 @@ public void Stop()
161
190
var searchId = Guid . NewGuid ( ) ;
162
191
_prevSearchId = searchId ;
163
192
164
-
165
193
// Thread-local storage for best move in each thread
166
194
var results =
167
195
new ThreadLocal < ( List < uint > move , int depthSearched , int score , long nodes ) > (
168
196
( ) => ( new List < uint > ( ) , 0 , int . MinValue , 0 ) , true ) ;
169
197
170
198
var start = DateTime . Now ;
171
199
200
+
201
+ foreach ( var searcher in Searchers )
202
+ {
203
+ searcher . Reset ( state ) ;
204
+ }
205
+
172
206
// Parallel search, with thread-local best move
173
207
Parallel . For ( 0 , Searchers . Count ,
174
- i => { results . Value = Searchers [ i ] . Search ( state , depthLimit : depth , writeInfo : i == 0 ) ; } ) ;
208
+ i => { results . Value = Searchers [ i ] . Search ( state , CancelSearch , depthLimit : depth , threadId : i ) ; } ) ;
175
209
var dt = DateTime . Now - start ;
176
210
177
211
Span < int > voteMap = stackalloc int [ 64 * 64 ] ;
0 commit comments