mirror of
https://github.com/official-stockfish/Stockfish.git
synced 2026-07-22 20:57:10 +00:00
Refactor global variables
This aims to remove some of the annoying global structure which Stockfish has. Overall there is no major elo regression to be expected. Non regression SMP STC (paused, early version): https://tests.stockfishchess.org/tests/view/65983d7979aa8af82b9608f1 LLR: 0.23 (-2.94,2.94) <-1.75,0.25> Total: 76232 W: 19035 L: 19096 D: 38101 Ptnml(0-2): 92, 8735, 20515, 8690, 84 Non regression STC (early version): https://tests.stockfishchess.org/tests/view/6595b3a479aa8af82b95da7f LLR: 2.93 (-2.94,2.94) <-1.75,0.25> Total: 185344 W: 47027 L: 46972 D: 91345 Ptnml(0-2): 571, 21285, 48943, 21264, 609 Non regression SMP STC: https://tests.stockfishchess.org/tests/view/65a0715c79aa8af82b96b7e4 LLR: 2.94 (-2.94,2.94) <-1.75,0.25> Total: 142936 W: 35761 L: 35662 D: 71513 Ptnml(0-2): 209, 16400, 38135, 16531, 193 These global structures/variables add hidden dependencies and allow data to be mutable from where it shouldn't it be (i.e. options). They also prevent Stockfish from internal selfplay, which would be a nice thing to be able to do, i.e. instantiate two Stockfish instances and let them play against each other. It will also allow us to make Stockfish a library, which can be easier used on other platforms. For consistency with the old search code, `thisThread` has been kept, even though it is not strictly necessary anymore. This the first major refactor of this kind (in recent time), and future changes are required, to achieve the previously described goals. This includes cleaning up the dependencies, transforming the network to be self contained and coming up with a plan to deal with proper tablebase memory management (see comments for more information on this). The removal of these global structures has been discussed in parts with Vondele and Sopel. closes https://github.com/official-stockfish/Stockfish/pull/4968 No functional change
This commit is contained in:
@@ -19,77 +19,70 @@
|
||||
#ifndef UCI_H_INCLUDED
|
||||
#define UCI_H_INCLUDED
|
||||
|
||||
#include <cstddef>
|
||||
#include <iosfwd>
|
||||
#include <map>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "types.h"
|
||||
#include "evaluate.h"
|
||||
#include "misc.h"
|
||||
#include "position.h"
|
||||
#include "thread.h"
|
||||
#include "tt.h"
|
||||
#include "ucioption.h"
|
||||
|
||||
namespace Stockfish {
|
||||
|
||||
class Position;
|
||||
namespace Eval::NNUE {
|
||||
enum NetSize : int;
|
||||
}
|
||||
|
||||
namespace UCI {
|
||||
namespace Search {
|
||||
class Worker;
|
||||
}
|
||||
|
||||
// Normalizes the internal value as reported by evaluate or search
|
||||
// to the UCI centipawn result used in output. This value is derived from
|
||||
// the win_rate_model() such that Stockfish outputs an advantage of
|
||||
// "100 centipawns" for a position if the engine has a 50% probability to win
|
||||
// from this position in self-play at fishtest LTC time control.
|
||||
const int NormalizeToPawnValue = 328;
|
||||
|
||||
class Option;
|
||||
|
||||
// Define a custom comparator, because the UCI options should be case-insensitive
|
||||
struct CaseInsensitiveLess {
|
||||
bool operator()(const std::string&, const std::string&) const;
|
||||
};
|
||||
|
||||
// The options container is defined as a std::map
|
||||
using OptionsMap = std::map<std::string, Option, CaseInsensitiveLess>;
|
||||
|
||||
// The Option class implements each option as specified by the UCI protocol
|
||||
class Option {
|
||||
|
||||
using OnChange = void (*)(const Option&);
|
||||
class Move;
|
||||
enum Square : int;
|
||||
using Value = int;
|
||||
|
||||
class UCI {
|
||||
public:
|
||||
Option(OnChange = nullptr);
|
||||
Option(bool v, OnChange = nullptr);
|
||||
Option(const char* v, OnChange = nullptr);
|
||||
Option(double v, int minv, int maxv, OnChange = nullptr);
|
||||
Option(const char* v, const char* cur, OnChange = nullptr);
|
||||
UCI(int argc, char** argv);
|
||||
|
||||
Option& operator=(const std::string&);
|
||||
void operator<<(const Option&);
|
||||
operator int() const;
|
||||
operator std::string() const;
|
||||
bool operator==(const char*) const;
|
||||
void loop();
|
||||
|
||||
static int to_cp(Value v);
|
||||
static std::string value(Value v);
|
||||
static std::string square(Square s);
|
||||
static std::string move(Move m, bool chess960);
|
||||
static std::string pv(const Search::Worker& workerThread,
|
||||
TimePoint elapsed,
|
||||
uint64_t nodesSearched,
|
||||
uint64_t tb_hits,
|
||||
int hashfull,
|
||||
bool rootInTB);
|
||||
static std::string wdl(Value v, int ply);
|
||||
static Move to_move(const Position& pos, std::string& str);
|
||||
|
||||
const std::string& workingDirectory() const { return cli.workingDirectory; }
|
||||
|
||||
OptionsMap options;
|
||||
|
||||
std::unordered_map<Eval::NNUE::NetSize, Eval::EvalFile> evalFiles;
|
||||
|
||||
private:
|
||||
friend std::ostream& operator<<(std::ostream&, const OptionsMap&);
|
||||
TranspositionTable tt;
|
||||
ThreadPool threads;
|
||||
CommandLine cli;
|
||||
|
||||
std::string defaultValue, currentValue, type;
|
||||
int min, max;
|
||||
size_t idx;
|
||||
OnChange on_change;
|
||||
void go(Position& pos, std::istringstream& is, StateListPtr& states);
|
||||
void bench(Position& pos, std::istream& args, StateListPtr& states);
|
||||
void position(Position& pos, std::istringstream& is, StateListPtr& states);
|
||||
void trace_eval(Position& pos);
|
||||
void search_clear();
|
||||
void setoption(std::istringstream& is);
|
||||
};
|
||||
|
||||
void init(OptionsMap&);
|
||||
void loop(int argc, char* argv[]);
|
||||
int to_cp(Value v);
|
||||
std::string value(Value v);
|
||||
std::string square(Square s);
|
||||
std::string move(Move m, bool chess960);
|
||||
std::string pv(const Position& pos, Depth depth);
|
||||
std::string wdl(Value v, int ply);
|
||||
Move to_move(const Position& pos, std::string& str);
|
||||
|
||||
} // namespace UCI
|
||||
|
||||
extern UCI::OptionsMap Options;
|
||||
|
||||
} // namespace Stockfish
|
||||
|
||||
#endif // #ifndef UCI_H_INCLUDED
|
||||
|
||||
Reference in New Issue
Block a user