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:
Disservin
2024-01-08 19:48:46 +01:00
parent 6deb88728f
commit a107910951
27 changed files with 1200 additions and 1001 deletions

View File

@@ -26,8 +26,10 @@
#include <fstream>
#include <iomanip>
#include <iostream>
#include <optional>
#include <sstream>
#include <string_view>
#include <type_traits>
#include <unordered_map>
#include "../evaluate.h"
@@ -51,8 +53,6 @@ AlignedPtr<Network<TransformedFeatureDimensionsBig, L2Big, L3Big>> network
AlignedPtr<Network<TransformedFeatureDimensionsSmall, L2Small, L3Small>> networkSmall[LayerStacks];
// Evaluation function file names
std::string fileName[2];
std::string netDescription[2];
namespace Detail {
@@ -136,10 +136,10 @@ static bool write_header(std::ostream& stream, std::uint32_t hashValue, const st
}
// Read network parameters
static bool read_parameters(std::istream& stream, NetSize netSize) {
static bool read_parameters(std::istream& stream, NetSize netSize, std::string& netDescription) {
std::uint32_t hashValue;
if (!read_header(stream, &hashValue, &netDescription[netSize]))
if (!read_header(stream, &hashValue, &netDescription))
return false;
if (hashValue != HashValue[netSize])
return false;
@@ -158,9 +158,10 @@ static bool read_parameters(std::istream& stream, NetSize netSize) {
}
// Write network parameters
static bool write_parameters(std::ostream& stream, NetSize netSize) {
static bool
write_parameters(std::ostream& stream, NetSize netSize, const std::string& netDescription) {
if (!write_header(stream, HashValue[netSize], netDescription[netSize]))
if (!write_header(stream, HashValue[netSize], netDescription))
return false;
if (netSize == Big && !Detail::write_parameters(stream, *featureTransformerBig))
return false;
@@ -424,24 +425,30 @@ std::string trace(Position& pos) {
// Load eval, from a file stream or a memory stream
bool load_eval(const std::string name, std::istream& stream, NetSize netSize) {
std::optional<std::string> load_eval(std::istream& stream, NetSize netSize) {
initialize(netSize);
fileName[netSize] = name;
return read_parameters(stream, netSize);
std::string netDescription;
return read_parameters(stream, netSize, netDescription) ? std::make_optional(netDescription)
: std::nullopt;
}
// Save eval, to a file stream or a memory stream
bool save_eval(std::ostream& stream, NetSize netSize) {
bool save_eval(std::ostream& stream,
NetSize netSize,
const std::string& name,
const std::string& netDescription) {
if (fileName[netSize].empty())
if (name.empty() || name == "None")
return false;
return write_parameters(stream, netSize);
return write_parameters(stream, netSize, netDescription);
}
// Save eval, to a file given by its name
bool save_eval(const std::optional<std::string>& filename, NetSize netSize) {
bool save_eval(const std::optional<std::string>& filename,
NetSize netSize,
const std::unordered_map<Eval::NNUE::NetSize, Eval::EvalFile>& evalFiles) {
std::string actualFilename;
std::string msg;
@@ -450,7 +457,7 @@ bool save_eval(const std::optional<std::string>& filename, NetSize netSize) {
actualFilename = filename.value();
else
{
if (EvalFiles.at(netSize).selected_name
if (evalFiles.at(netSize).current
!= (netSize == Small ? EvalFileDefaultNameSmall : EvalFileDefaultNameBig))
{
msg = "Failed to export a net. "
@@ -463,7 +470,8 @@ bool save_eval(const std::optional<std::string>& filename, NetSize netSize) {
}
std::ofstream stream(actualFilename, std::ios_base::binary);
bool saved = save_eval(stream, netSize);
bool saved = save_eval(stream, netSize, evalFiles.at(netSize).current,
evalFiles.at(netSize).netDescription);
msg = saved ? "Network saved successfully to " + actualFilename : "Failed to export a net";