Line data Source code
1 : #include "attack.hpp"
2 : #include "bitboardTools.hpp"
3 : #include "cli.hpp"
4 : #include "com.hpp"
5 : #include "definition.hpp"
6 : #include "distributed.h"
7 : #include "dynamicConfig.hpp"
8 : #include "egt.hpp"
9 : #include "energyMonitor.hpp"
10 : #include "evalConfig.hpp"
11 : #include "hash.hpp"
12 : #include "kpk.hpp"
13 : #include "logging.hpp"
14 : #include "material.hpp"
15 : #include "nnue.hpp"
16 : #include "option.hpp"
17 : #include "pgnparser.hpp"
18 : #include "searchConfig.hpp"
19 : #include "searcher.hpp"
20 : #include "testSuite.hpp"
21 : #include "evaluationTuning.hpp"
22 : #include "threading.hpp"
23 : #include "timeMan.hpp"
24 : #include "timers.hpp"
25 : #include "tools.hpp"
26 : #include "transposition.hpp"
27 : #include "uci.hpp"
28 : #include "xboard.hpp"
29 :
30 :
31 22 : inline void sizeOf(){
32 22 : Logging::LogIt(Logging::logInfo) << "Size of Position " << sizeof(Position) << "b";
33 22 : Logging::LogIt(Logging::logInfo) << "Size of PVSData " << sizeof(Searcher::PVSData) << "b";
34 22 : }
35 :
36 : // Initialize all the things that should be ...
37 22 : inline void init(int argc, char** argv) {
38 : Distributed::init();
39 22 : Logging::hellooo();
40 22 : Options::initOptions(argc, argv);
41 22 : Logging::init(); // after reading options
42 22 : Zobrist::initHash();
43 22 : TT::initTable();
44 22 : BBTools::initMask();
45 : #ifdef WITH_MAGIC
46 22 : BBTools::MagicBB::initMagic();
47 : #endif
48 22 : KPK::init();
49 22 : MaterialHash::MaterialHashInitializer::init();
50 22 : EvalConfig::initEval();
51 22 : ThreadPool::instance().setup();
52 : Distributed::lateInit();
53 : #ifdef WITH_SYZYGY
54 22 : SyzygyTb::initTB();
55 : #endif
56 : #ifdef WITH_NNUE
57 22 : NNUEWrapper::init();
58 : #endif
59 22 : COM::init(COM::p_uci); // let's do this ... (usefull to reset position in case of NNUE)
60 22 : sizeOf();
61 : // Start energy monitoring
62 22 : initEnergyMonitor(500);
63 22 : }
64 :
65 21 : inline void finalize() {
66 21 : Logging::LogIt(Logging::logInfo) << "Stopping all threads";
67 21 : ThreadPool::instance().stop();
68 21 : Logging::LogIt(Logging::logInfo) << "Waiting all threads";
69 21 : ThreadPool::instance().wait(); ///@todo is this necessary ?
70 21 : Logging::LogIt(Logging::logInfo) << "Deleting all threads";
71 21 : ThreadPool::instance().resize(0);
72 21 : Logging::LogIt(Logging::logInfo) << "Syncing process...";
73 21 : Distributed::sync(Distributed::_commInput, __PRETTY_FUNCTION__);
74 : // Report and cleanup energy monitoring
75 21 : finalizeEnergyMonitor();
76 21 : Logging::LogIt(Logging::logInfo) << "Bye bye...";
77 21 : Logging::finalize();
78 21 : Logging::LogIt(Logging::logInfo) << "See you soon...";
79 : Distributed::finalize();
80 21 : }
81 :
82 : #define RETURN(x) \
83 : { \
84 : const bool ret = x; \
85 : finalize(); \
86 : return ret; \
87 : }
88 :
89 : #ifdef __EMSCRIPTEN__
90 : #include <emscripten/emscripten.h>
91 : #ifdef __cplusplus
92 : extern "C" {
93 : #endif
94 : EMSCRIPTEN_KEEPALIVE void processCommand(const char *str){
95 : const std::string command(str);
96 : UCI::processCommand(command);
97 : }
98 : #ifdef __cplusplus
99 : }
100 : #endif
101 : #endif
102 :
103 0 : bool help() {
104 : const std::string h1 =
105 0 : R"(
106 : Available options are :
107 : * -uci : starts in UCI mode (this is also default)
108 : * -xboard : starts in XBOARD move
109 : )";
110 : const std::string h2 =
111 0 : R"(
112 : Depending on compilation parameters you may use :
113 : * -tuning
114 : * -pgn
115 : * -bin2plain
116 : * -plain2bin
117 : * -pgn2bin
118 : * -rescore
119 : )";
120 : #ifdef DEBUG_TOOL
121 : const std::string h3 =
122 0 : R"(
123 : DEV / TEST options available are :
124 : * -selfplay [depth=15] [games=1] : run self play matches. Usefull to generate training data
125 : * -perft_test : run small perf test on 4 well-known positions
126 : * -perft_test_long_fischer : run a long perf test for FRC
127 : * -perft_test_long : run a long perf test
128 : * -see_test : run a SEE test (most positions taken from Vajolet by Marco Belli a.k.a elcabesa)
129 : * -evalSpeed [filename] : run an evaluation performance test
130 : * -timeTest [initial=50000] [incr=0] [moveInTC=-1] [guiLag=0] : run a TC simulation
131 : * bench [depth=16] : used for OpenBench output
132 : Next commands needs at least a position
133 : * -qsearch [pos]: run a qsearch
134 : * -see [pos] [move=\"e2e4\"] : run a SEE_GE on the given square
135 : * -attacked [pos] [square=0..63] : print BB attack
136 : * -cov [pos] [square=0..63] : print each piece type attack
137 : * -eval [pos] : run an evaluation (NNUE)
138 : * -evalHCE [pos] : run an evaluation (HCE)
139 : * -gen [pos] : generate available moves
140 : * -testmove [TODO] : test move application
141 : * -perft [pos] [depth=5] : run a perft on the given position for the given depth
142 : * -analyze [pos] [depth=15] : run an analysis for the given position to the given depth
143 : * -mateFinder [pos] [depth=10] : run an analysis in mate finder mode for the given position to the given depth
144 : * -probe [pos] : TB probe
145 : * -kpk [pos] : KPK probe
146 : Use --help_test do display available test suites (depends on compilation option)
147 : )";
148 : #endif
149 0 : Logging::LogIt(Logging::logGUI) << h1;
150 0 : Logging::LogIt(Logging::logGUI) << h2;
151 : #ifdef DEBUG_TOOL
152 0 : Logging::LogIt(Logging::logGUI) << h3;
153 : #endif
154 0 : return true;
155 : }
156 :
157 : #ifdef WITH_TEST_SUITE
158 1 : bool helpTest(){
159 : const std::string h =
160 1 : R"(
161 : Available analysis tests
162 : * MEA
163 : * TTT
164 : * opening200
165 : * opening1000
166 : * middle200
167 : * middle1000
168 : * hard2020
169 : * BT2630
170 : * WAC
171 : * arasan
172 : * arasan_sym
173 : * CCROHT
174 : * BKTest
175 : * EETest
176 : * KMTest
177 : * LCTest
178 : * sbdTest
179 : * STS
180 : * ERET
181 : * MATE
182 : )";
183 1 : Logging::LogIt(Logging::logGUI) << h;
184 1 : return true;
185 : }
186 : #endif
187 :
188 22 : inline bool minic(int argc, char** argv) {
189 : START_TIMER
190 :
191 : // initialization is **always** called
192 : // for sure this can be optimized in some situations
193 : // but it may not worth the pain/risks
194 22 : init(argc, argv);
195 :
196 22 : std::string firstArg = argc > 1 ? argv[1] : "";
197 : const bool hasParam = argc > 2;
198 22 : const std::string secondArg = hasParam ? argv[2] : "";
199 :
200 : #ifdef WITH_TEST_SUITE
201 22 : if (firstArg == "help_test" || firstArg == "-help_test" || firstArg == "--help_test") {
202 1 : return helpTest();
203 : }
204 : #endif
205 :
206 21 : if (firstArg == "-h" || firstArg == "-help" || firstArg == "--help" || firstArg == "help") {
207 0 : return help();
208 : }
209 :
210 : #ifndef __EMSCRIPTEN__ // if emscripten build, them everything is skipped
211 :
212 : #ifdef WITH_TEST_SUITE
213 : // this will run a test suite if requiered
214 21 : if (test(firstArg)) RETURN(true)
215 : #endif // WITH_TEST_SUITE
216 :
217 : #ifdef WITH_EVAL_TUNING
218 : if (hasParam && std::string(firstArg) == "-tuning") {
219 : evaluationTuning(secondArg);
220 : RETURN(true)
221 : }
222 : #endif // WITH_EVAL_TUNING
223 :
224 : #ifdef WITH_PGN_PARSER
225 : if (hasParam && std::string(firstArg) == "-pgn") { RETURN(PGNParse(secondArg)) }
226 : #endif // WITH_PGN_PARSER
227 :
228 : #ifdef WITH_DATA2BIN
229 63 : if (hasParam && std::string(firstArg) == "-plain2bin") {
230 0 : RETURN(convert_plain_to_bin({secondArg}, secondArg + ".bin", 1, 300))
231 : }
232 63 : if (hasParam && std::string(firstArg) == "-pgn2bin") {
233 0 : RETURN(convert_bin_from_pgn_extract({secondArg}, secondArg + ".bin", true, false))
234 : }
235 63 : if (hasParam && std::string(firstArg) == "-bin2plain") {
236 0 : RETURN(convert_bin_to_plain({secondArg}, secondArg + ".plain"))
237 : }
238 63 : if (hasParam && std::string(firstArg) == "-rescore") {
239 0 : RETURN(rescore({secondArg}, secondArg + ".rescored"))
240 : }
241 : #endif // WITH_DATA2BIN
242 :
243 : #ifdef DEBUG_TOOL
244 21 : if (firstArg.empty()) {
245 0 : Logging::LogIt(Logging::logInfo) << "Hint: You can use -xboard command line option to enter xboard mode (default is uci)";
246 : firstArg = "-uci"; // default is uci
247 : }
248 21 : const bool ret = cliManagement(firstArg, argc, argv);
249 : STOP_AND_SUM_TIMER(Total)
250 : Timers::Display();
251 21 : finalize();
252 : return ret;
253 :
254 : #else // DEBUG_TOOL
255 : // only init TimeMan if needed ...
256 : TimeMan::init();
257 :
258 : #ifdef WITH_UCI // UCI is prefered default option
259 : UCI::init();
260 : UCI::uci();
261 : #else // WITH_UCI
262 : #ifdef WITH_XBOARD
263 : XBoard::init();
264 : XBoard::xboard();
265 : #endif // WITH_XBOARD
266 : #endif // WITH_UCI
267 :
268 : STOP_AND_SUM_TIMER(Total)
269 :
270 : #ifdef WITH_TIMER
271 : Timers::Display();
272 : #endif // WITH_TIMER
273 :
274 : RETURN(true)
275 : #endif // DEBUG_TOOL
276 :
277 : #endif // __EMSCRIPTEN__
278 : return true;
279 : }
|