Skip to content

Commit e3b7b71

Browse files
notify cv
1 parent 726fa83 commit e3b7b71

File tree

4 files changed

+70
-55
lines changed

4 files changed

+70
-55
lines changed

src/CLI.cpp

+8-6
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ namespace Interface {
1717
std::getline(std::cin, input);
1818
currentInstruction = interpret(input);
1919

20-
while (!ready) {
21-
std::this_thread::sleep_for(std::chrono::milliseconds(10));
22-
}
20+
std::unique_lock lk(m);
21+
cv.wait(lk, [this] { return ready; });
2322
ready = false;
2423
std::thread thread(&CLI::handleInstruction, this, currentInstruction);
2524
thread.detach();
@@ -99,16 +98,19 @@ namespace Interface {
9998
}
10099

101100
const Move bestMove = Search::search(board, timeOut);
101+
board.makeMove(bestMove);
102102

103103
std::cout << "bestmove " << bestMove << std::endl;
104104

105-
board.makeMove(bestMove);
106-
107105
break;
108106
}
109107
default:
110108
break;
111109
}
112-
ready = true;
110+
{
111+
std::lock_guard lk(m);
112+
ready = true;
113+
}
114+
cv.notify_one();
113115
}
114116
}

src/CLI.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <string>
44
#include <unordered_map>
55
#include <vector>
6+
#include <condition_variable>
67

78
#include "ChessBoard.h"
89

@@ -43,7 +44,9 @@ namespace Interface {
4344
private:
4445
ChessBoard board;
4546

46-
bool ready = true;
47+
std::mutex m;
48+
std::condition_variable cv;
49+
bool ready = true;
4750

4851
static Instruction interpret(const std::string&string);
4952

src/Logger.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ void Logger::start() {
1919
}
2020

2121
void Logger::end() {
22+
{
23+
std::lock_guard lk(m);
24+
stop = true;
25+
}
26+
cv.notify_one();
2227
stop = true;
2328
processingThread.join();
2429
#ifdef logtofile
@@ -102,7 +107,6 @@ Logger::~Logger() {
102107

103108
void Logger::threadFunc() {
104109
while (!stop) {
105-
// Keep processing the buffer, wait a bit when it's empty
106110
if (!processNode()) {
107111
std::unique_lock lk(m);
108112
empty = true;

src/Search.cpp

+53-47
Original file line numberDiff line numberDiff line change
@@ -20,64 +20,69 @@ Move Search::search(ChessBoard &board, const int timeAllowed) {
2020
const auto start = std::chrono::steady_clock::now();
2121

2222
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);
2727

28-
std::thread thread(&Search::threadedSearch, &search, i);
28+
i = search.lastPV.size() + 1;
29+
}
2930

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));
3334

34-
const auto timeAvailable = start + timeOut - std::chrono::steady_clock::now();
35+
std::thread thread(&Search::threadedSearch, &search, i);
3536

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;
4340

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;
4548

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+
}
4756

48-
if (endEarly) break;
57+
bool endEarly = false;
4958

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\nPV: ", 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("\nBoard hash: %llu", board.hashCode);
67-
printf("\nTT reads: %d", tt.reads);
68-
printf("\nTT writes: %d", tt.writes);
69-
printf("\nTT collisions: %d", tt.collisions);
70-
printf("\nTT 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);
7560

76-
tt.resetCounters();
61+
if (endEarly) break;
7762
}
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\nPV: ", score);
71+
for (const Move&move: search.lastPV) {
72+
printf("%s%s ", Util::positionToString(move.start).c_str(), Util::positionToString(move.end).c_str());
8073
}
74+
const int occupancy = tt.occupancy();
75+
printf("\nBoard hash: %llu", board.hashCode);
76+
printf("\nTT reads: %d", tt.reads);
77+
printf("\nTT writes: %d", tt.writes);
78+
printf("\nTT collisions: %d", tt.collisions);
79+
printf("\nTT 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();
8186

8287
search.lastPV = search.collectPV(i);
8388

@@ -90,6 +95,7 @@ Move Search::search(ChessBoard &board, const int timeAllowed) {
9095
}
9196

9297
search.logger.end();
98+
std::cout << "Logger stopped" << std::endl;
9399

94100
return search.lastPV[0];
95101
}

0 commit comments

Comments
 (0)