Skip to content

Commit 5772ec9

Browse files
committed
handle ctrl+c during datagen properly on non-windows
Bench: 4827402
1 parent 3fc5eb1 commit 5772ec9

File tree

7 files changed

+121
-21
lines changed

7 files changed

+121
-21
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ set(STORMPHRAX_COMMON_SRC src/types.h src/main.cpp src/uci.h src/uci.cpp src/cor
6363
src/eval/nnue/layers/scale.h src/eval/nnue/layers/dequantize.h src/util/simd/x64common.h src/util/simd/avx512.h
6464
src/util/simd/avx2.h src/util/simd/sse41.h src/util/simd/neon.h src/util/simd/none.h src/util/align.h
6565
src/3rdparty/zstd/zstddeclib.c src/eval/nnue/io_impl.h src/eval/nnue/io_impl.cpp src/datagen/fen.h
66-
src/datagen/fen.cpp)
66+
src/datagen/fen.cpp src/util/ctrlc.h src/util/ctrlc.cpp)
6767

6868
set(STORMPHRAX_BMI2_SRC src/attacks/bmi2/data.h src/attacks/bmi2/attacks.h src/attacks/bmi2/attacks.cpp)
6969
set(STORMPHRAX_NON_BMI2_SRC src/attacks/black_magic/data.h src/attacks/black_magic/attacks.h

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ endif
1414
PGO = on
1515
COMMIT_HASH = off
1616

17-
SOURCES_COMMON := src/main.cpp src/uci.cpp src/util/split.cpp src/position/position.cpp src/movegen.cpp src/search.cpp src/util/timer.cpp src/pretty.cpp src/ttable.cpp src/limit/time.cpp src/eval/nnue.cpp src/perft.cpp src/bench.cpp src/tunable.cpp src/opts.cpp src/3rdparty/fathom/tbprobe.cpp src/datagen/datagen.cpp src/wdl.cpp src/cuckoo.cpp src/datagen/marlinformat.cpp src/datagen/viriformat.cpp src/datagen/fen.cpp src/tb.cpp src/3rdparty/zstd/zstddeclib.c src/eval/nnue/io_impl.cpp
17+
SOURCES_COMMON := src/main.cpp src/uci.cpp src/util/split.cpp src/position/position.cpp src/movegen.cpp src/search.cpp src/util/timer.cpp src/pretty.cpp src/ttable.cpp src/limit/time.cpp src/eval/nnue.cpp src/perft.cpp src/bench.cpp src/tunable.cpp src/opts.cpp src/3rdparty/fathom/tbprobe.cpp src/datagen/datagen.cpp src/wdl.cpp src/cuckoo.cpp src/datagen/marlinformat.cpp src/datagen/viriformat.cpp src/datagen/fen.cpp src/tb.cpp src/3rdparty/zstd/zstddeclib.c src/eval/nnue/io_impl.cpp src/util/ctrlc.cpp
1818
SOURCES_BMI2 := src/attacks/bmi2/attacks.cpp
1919
SOURCES_BLACK_MAGIC := src/attacks/black_magic/attacks.cpp
2020

src/datagen/datagen.cpp

+5-18
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,6 @@
2626
#include <optional>
2727
#include <cassert>
2828

29-
#ifdef _WIN32
30-
#define WIN32_LEAN_AND_MEAN
31-
#define NOMINMAX
32-
#include <Windows.h>
33-
#else
34-
//TODO
35-
#endif
36-
3729
#include "../limit/limit.h"
3830
#include "../search.h"
3931
#include "../movegen.h"
@@ -44,6 +36,7 @@
4436
#include "viriformat.h"
4537
#include "marlinformat.h"
4638
#include "fen.h"
39+
#include "../util/ctrlc.h"
4740

4841
// abandon hope all ye who enter here
4942
// my search was not written with this in mind
@@ -58,16 +51,10 @@ namespace stormphrax::datagen
5851

5952
auto initCtrlCHandler()
6053
{
61-
#ifdef _WIN32
62-
if (!SetConsoleCtrlHandler([](DWORD dwCtrlType) -> BOOL
63-
{
64-
s_stop.store(true, std::memory_order::seq_cst);
65-
return TRUE;
66-
}, TRUE))
67-
std::cerr << "failed to set ctrl+c handler" << std::endl;
68-
#else
69-
//TODO
70-
#endif
54+
util::signal::addCtrlCHandler([]
55+
{
56+
s_stop.store(true, std::memory_order::seq_cst);
57+
});
7158
}
7259

7360
class DatagenNodeLimiter final : public limit::ISearchLimiter

src/main.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "eval/nnue.h"
2424
#include "tunable.h"
2525
#include "cuckoo.h"
26+
#include "util/ctrlc.h"
2627

2728
#if SP_EXTERNAL_TUNE
2829
#include "util/split.h"
@@ -32,6 +33,8 @@ using namespace stormphrax;
3233

3334
auto main(i32 argc, const char *argv[]) -> i32
3435
{
36+
util::signal::init();
37+
3538
tunable::init();
3639
cuckoo::init();
3740

src/util/ctrlc.cpp

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Stormphrax, a UCI chess engine
3+
* Copyright (C) 2024 Ciekce
4+
*
5+
* Stormphrax is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* Stormphrax is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with Stormphrax. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
#include "ctrlc.h"
20+
21+
#include <iostream>
22+
23+
#ifdef _WIN32
24+
#define WIN32_LEAN_AND_MEAN
25+
#define NOMINMAX
26+
#include <Windows.h>
27+
#else
28+
#include <signal.h>
29+
#endif
30+
31+
namespace stormphrax::util::signal
32+
{
33+
namespace
34+
{
35+
std::vector<CtrlCHandler> s_handlers{};
36+
}
37+
38+
auto addCtrlCHandler(CtrlCHandler handler) -> void
39+
{
40+
s_handlers.push_back(std::move(handler));
41+
}
42+
43+
auto init() -> void
44+
{
45+
#ifdef _WIN32
46+
const auto result = SetConsoleCtrlHandler([](DWORD dwCtrlType) -> BOOL
47+
{
48+
if (dwCtrlType == CTRL_BREAK_EVENT)
49+
return FALSE;
50+
51+
for (auto &handler : s_handlers)
52+
{
53+
handler();
54+
}
55+
56+
return TRUE;
57+
}, TRUE);
58+
59+
if (!result)
60+
std::cerr << "failed to set ctrl+c handler" << std::endl;
61+
#else
62+
struct sigaction action {
63+
.sa_flags = SA_RESTART
64+
};
65+
66+
// on some platforms this is a union, and C++ doesn't support nested designated initialisers
67+
action.sa_handler = [](int signal)
68+
{
69+
for (auto &handler : s_handlers)
70+
{
71+
handler();
72+
}
73+
};
74+
75+
if (sigaction(SIGINT, &action, nullptr))
76+
std::cerr << "failed to set ctrl+c handler" << std::endl;
77+
#endif
78+
}
79+
}

src/util/ctrlc.h

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Stormphrax, a UCI chess engine
3+
* Copyright (C) 2024 Ciekce
4+
*
5+
* Stormphrax is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* Stormphrax is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with Stormphrax. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
#pragma once
20+
21+
#include "../types.h"
22+
23+
#include <functional>
24+
25+
namespace stormphrax::util::signal
26+
{
27+
using CtrlCHandler = std::function<void()>;
28+
auto addCtrlCHandler(CtrlCHandler handler) -> void;
29+
30+
auto init() -> void;
31+
}

version.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
5.0.29
1+
5.0.30

0 commit comments

Comments
 (0)