@@ -20,64 +20,69 @@ Move Search::search(ChessBoard &board, const int timeAllowed) {
20
20
const auto start = std::chrono::steady_clock::now ();
21
21
22
22
int i = 1 ;
23
- try {
24
- for (; i < 64 ; ++i) {
25
- search. logger . log ( std::format ( " info searching depth {} \n " , i) );
26
- search.logger . logToFile ( std::format ( " starting depth {} \n " , i) );
23
+ {
24
+ std::thread thread (&Search::threadedSearch, &search, i);
25
+ thread. join ( );
26
+ search. lastPV = search.collectPV (i );
27
27
28
- std::thread thread (&Search::threadedSearch, &search, i);
28
+ i = search.lastPV .size () + 1 ;
29
+ }
29
30
30
- std::unique_lock<std::mutex> lk (search. cv_m );
31
- search.stop = false ;
32
- search.finished = false ;
31
+ for (; i < 64 ; ++i) {
32
+ search.logger . log ( std::format ( " info searching depth {} \n " , i)) ;
33
+ search.logger . logToFile ( std::format ( " starting depth {} \n " , i)) ;
33
34
34
- const auto timeAvailable = start + timeOut - std::chrono::steady_clock::now ( );
35
+ std::thread thread (&Search::threadedSearch, &search, i );
35
36
36
- if (search.cv .wait_for (lk, timeAvailable, [&] { return search.finished ; })) {
37
- thread.join ();
38
- } else {
39
- search.stop = true ;
40
- thread.join ();
41
- break ;
42
- }
37
+ std::unique_lock<std::mutex> lk (search.cv_m );
38
+ search.stop = false ;
39
+ search.finished = false ;
43
40
44
- bool endEarly = false ;
41
+ const auto now = std::chrono::steady_clock::now ();
42
+ if (now - start >= timeOut){
43
+ search.stop = true ;
44
+ thread.join ();
45
+ break ;
46
+ }
47
+ const auto timeAvailable = start + timeOut -now;
45
48
46
- search.lastPV = search.collectPV (i, endEarly);
49
+ if (search.cv .wait_for (lk, timeAvailable, [&] { return search.finished ; })) {
50
+ thread.join ();
51
+ } else {
52
+ search.stop = true ;
53
+ thread.join ();
54
+ break ;
55
+ }
47
56
48
- if ( endEarly) break ;
57
+ bool endEarly = false ;
49
58
50
- if (search.lastPV .size () > i) {
51
- i = search.lastPV .size ();
52
- }
53
- }
54
- #ifdef wasm
55
- printf (" Depth: %d\n " , i - 1 );
56
- int score = Evaluator::evaluate (board);
57
- if (tt.contains (board.hashCode )) {
58
- const TranspositionTable::Entry entry = tt.getEntry (board.hashCode , 0 );
59
- score = entry.score ;
60
- }
61
- printf (" Evaluation: %d\n PV: " , score);
62
- for (const Move&move: search.lastPV ) {
63
- printf (" %s%s " , Util::positionToString (move.start ).c_str (), Util::positionToString (move.end ).c_str ());
64
- }
65
- const int occupancy = tt.occupancy ();
66
- printf (" \n Board hash: %llu" , board.hashCode );
67
- printf (" \n TT reads: %d" , tt.reads );
68
- printf (" \n TT writes: %d" , tt.writes );
69
- printf (" \n TT collisions: %d" , tt.collisions );
70
- printf (" \n TT occupancy: %d" , occupancy);
71
- printf (" \n **************************\n " );
72
- search.logger .sendInt (" updateDepth" , i - 1 );
73
- search.logger .sendInt (" updateTTOccupancy" , tt.occupancy ());
74
- #endif
59
+ search.lastPV = search.collectPV (i, endEarly);
75
60
76
- tt. resetCounters () ;
61
+ if (endEarly) break ;
77
62
}
78
- catch (std::exception &e) {
79
- std::cout << " Exception while searching: " << e.what () << " | Will try to return PV[0]" << std::endl;
63
+ #ifdef wasm
64
+ printf (" Depth: %d\n " , i - 1 );
65
+ int score = Evaluator::evaluate (board);
66
+ if (tt.contains (board.hashCode )) {
67
+ const TranspositionTable::Entry entry = tt.getEntry (board.hashCode , 0 );
68
+ score = entry.score ;
69
+ }
70
+ printf (" Evaluation: %d\n PV: " , score);
71
+ for (const Move&move: search.lastPV ) {
72
+ printf (" %s%s " , Util::positionToString (move.start ).c_str (), Util::positionToString (move.end ).c_str ());
80
73
}
74
+ const int occupancy = tt.occupancy ();
75
+ printf (" \n Board hash: %llu" , board.hashCode );
76
+ printf (" \n TT reads: %d" , tt.reads );
77
+ printf (" \n TT writes: %d" , tt.writes );
78
+ printf (" \n TT collisions: %d" , tt.collisions );
79
+ printf (" \n TT occupancy: %d" , occupancy);
80
+ printf (" \n **************************\n " );
81
+ search.logger .sendInt (" updateDepth" , i - 1 );
82
+ search.logger .sendInt (" updateTTOccupancy" , tt.occupancy ());
83
+ #endif
84
+
85
+ tt.resetCounters ();
81
86
82
87
search.lastPV = search.collectPV (i);
83
88
@@ -90,6 +95,7 @@ Move Search::search(ChessBoard &board, const int timeAllowed) {
90
95
}
91
96
92
97
search.logger .end ();
98
+ std::cout << " Logger stopped" << std::endl;
93
99
94
100
return search.lastPV [0 ];
95
101
}
0 commit comments