Skip to content

Commit 726fa83

Browse files
try catch in search
1 parent 53f478a commit 726fa83

File tree

2 files changed

+48
-42
lines changed

2 files changed

+48
-42
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ if (WASM)
3434
COMPILE_FLAGS "-pthread"
3535
)
3636
set_target_properties(ChessEngine PROPERTIES
37-
LINK_FLAGS "-O3 --closure 1 -s MODULARIZE -s EXPORT_ES6=1 -s EXPORT_NAME=Engine -s ENVIRONMENT=web,worker -pthread -s PTHREAD_POOL_SIZE=8 --embed-file ../assets@/ -s INITIAL_MEMORY=512MB -s EXPORTED_FUNCTIONS=['_main','_init','_move','_parseandmove','_unmove','_listPieces','_getMoves','_getAttacks','_setFen','_runPerft','_eval','_getBestMove'] -s EXPORTED_RUNTIME_METHODS=['ccall','cwrap']"
37+
LINK_FLAGS "-O3 --closure 1 -sMODULARIZE -sEXPORT_ES6=1 -sEXPORT_NAME=Engine -sENVIRONMENT=web,worker -pthread -sPTHREAD_POOL_SIZE=8 --embed-file ../assets@/ -sINITIAL_MEMORY=512MB -sEXPORTED_FUNCTIONS=['_main','_init','_move','_parseandmove','_unmove','_listPieces','_getMoves','_getAttacks','_setFen','_runPerft','_eval','_getBestMove'] -sEXPORTED_RUNTIME_METHODS=['ccall','cwrap']"
3838
COMPILE_FLAGS "-pthread"
3939
)
4040
#-s ENVIRONMENT=web / -s ENVIRONMENT=node

src/Search.cpp

+47-41
Original file line numberDiff line numberDiff line change
@@ -17,62 +17,68 @@ Move Search::search(ChessBoard &board, const int timeAllowed) {
1717
search.logger.logToFile("starting search\n");
1818

1919
const auto timeOut = std::chrono::milliseconds(timeAllowed);
20-
2120
const auto start = std::chrono::steady_clock::now();
21+
2222
int i = 1;
23-
for (; i < 64; ++i) {
24-
search.logger.log(std::format("info searching depth {}\n", i));
25-
search.logger.logToFile(std::format("starting depth {}\n", i));
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));
2627

27-
std::thread thread(&Search::threadedSearch, &search, i);
28+
std::thread thread(&Search::threadedSearch, &search, i);
2829

29-
std::unique_lock<std::mutex> lk(search.cv_m);
30-
search.stop = false;
31-
search.finished = false;
30+
std::unique_lock<std::mutex> lk(search.cv_m);
31+
search.stop = false;
32+
search.finished = false;
3233

33-
const auto timeAvailable = start + timeOut - std::chrono::steady_clock::now();
34+
const auto timeAvailable = start + timeOut - std::chrono::steady_clock::now();
3435

35-
if (search.cv.wait_for(lk, timeAvailable, [&] { return search.finished; })) {
36-
thread.join();
37-
} else {
38-
search.stop = true;
39-
thread.join();
40-
break;
41-
}
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+
}
4243

43-
bool endEarly = false;
44+
bool endEarly = false;
4445

45-
search.lastPV = search.collectPV(i, endEarly);
46+
search.lastPV = search.collectPV(i, endEarly);
4647

47-
if (endEarly) break;
48+
if (endEarly) break;
4849

49-
if (search.lastPV.size() > i) {
50-
i = search.lastPV.size();
50+
if (search.lastPV.size() > i) {
51+
i = search.lastPV.size();
52+
}
5153
}
52-
}
5354
#ifdef wasm
54-
printf("Depth: %d\n", i - 1);
55-
int score = Evaluator::evaluate(board);
56-
if (tt.contains(board.hashCode)) {
57-
const TranspositionTable::Entry entry = tt.getEntry(board.hashCode, 0);
58-
score = entry.score;
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
75+
76+
tt.resetCounters();
5977
}
60-
printf("Evaluation: %d\nPV: ", score);
61-
for (const Move&move: search.lastPV) {
62-
printf("%s%s ", Util::positionToString(move.start).c_str(), Util::positionToString(move.end).c_str());
78+
catch (std::exception &e) {
79+
std::cout << "Exception while searching: " << e.what() << "| Will try to return PV[0]" << std::endl;
6380
}
64-
const int occupancy = tt.occupancy();
65-
printf("\nBoard hash: %llu", board.hashCode);
66-
printf("\nTT reads: %d", tt.reads);
67-
printf("\nTT writes: %d", tt.writes);
68-
printf("\nTT collisions: %d", tt.collisions);
69-
printf("\nTT occupancy: %d", occupancy);
70-
printf("\n**************************\n");
71-
search.logger.sendInt("updateDepth", i - 1);
72-
search.logger.sendInt("updateTTOccupancy", tt.occupancy());
73-
#endif
7481

75-
tt.resetCounters();
7682
search.lastPV = search.collectPV(i);
7783

7884
if (search.lastPV.empty()) {

0 commit comments

Comments
 (0)