Skip to content

Commit

Permalink
Remove Global Variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Disservin committed Jan 8, 2024
1 parent 6deb887 commit 9525fae
Show file tree
Hide file tree
Showing 29 changed files with 1,082 additions and 932 deletions.
1 change: 1 addition & 0 deletions .github/workflows/stockfish.yml
Expand Up @@ -11,6 +11,7 @@ on:
branches:
- master
- tools

jobs:
Prerelease:
if: github.ref == 'refs/heads/master'
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/stockfish_format_check.yml
Expand Up @@ -11,6 +11,7 @@ on:
paths:
- '**.cpp'
- '**.h'

jobs:
Stockfish:
name: clang-format check
Expand Down
2 changes: 1 addition & 1 deletion src/Makefile
Expand Up @@ -63,7 +63,7 @@ HEADERS = benchmark.h bitboard.h evaluate.h misc.h movegen.h movepick.h \
nnue/layers/sqr_clipped_relu.h nnue/nnue_accumulator.h nnue/nnue_architecture.h \
nnue/nnue_common.h nnue/nnue_feature_transformer.h position.h \
search.h syzygy/tbprobe.h thread.h thread_win32_osx.h timeman.h \
tt.h tune.h types.h uci.h
tt.h tune.h types.h uci.h ucioption.h

OBJS = $(notdir $(SRCS:.cpp=.o))

Expand Down
33 changes: 16 additions & 17 deletions src/evaluate.cpp
Expand Up @@ -34,7 +34,7 @@
#include "nnue/evaluate_nnue.h"
#include "nnue/nnue_architecture.h"
#include "position.h"
#include "thread.h"
#include "search.h"
#include "types.h"
#include "uci.h"

Expand Down Expand Up @@ -62,10 +62,6 @@ namespace Stockfish {

namespace Eval {

std::unordered_map<NNUE::NetSize, EvalFile> EvalFiles = {
{NNUE::Big, {"EvalFile", EvalFileDefaultNameBig, "None"}},
{NNUE::Small, {"EvalFileSmall", EvalFileDefaultNameSmall, "None"}}};


// Tries to load a NNUE network at startup time, or when the engine
// receives a UCI command "setoption name EvalFile value nn-[a-z0-9]{12}.nnue"
Expand All @@ -74,7 +70,9 @@ std::unordered_map<NNUE::NetSize, EvalFile> EvalFiles = {
// network may be embedded in the binary), in the active working directory and
// in the engine directory. Distro packagers may define the DEFAULT_NNUE_DIRECTORY
// variable to have the engine search in a special directory in their distro.
void NNUE::init() {
void NNUE::init(const std::string& binaryDirectory,
const OptionsMap& Options,
std::unordered_map<Eval::NNUE::NetSize, EvalFile>& EvalFiles) {

for (auto& [netSize, evalFile] : EvalFiles)
{
Expand All @@ -88,10 +86,10 @@ void NNUE::init() {
user_eval_file = evalFile.default_name;

#if defined(DEFAULT_NNUE_DIRECTORY)
std::vector<std::string> dirs = {"<internal>", "", CommandLine::binaryDirectory,
std::vector<std::string> dirs = {"<internal>", "", binaryDirectory,
stringify(DEFAULT_NNUE_DIRECTORY)};
#else
std::vector<std::string> dirs = {"<internal>", "", CommandLine::binaryDirectory};
std::vector<std::string> dirs = {"<internal>", "", binaryDirectory};
#endif

for (const std::string& directory : dirs)
Expand Down Expand Up @@ -133,7 +131,8 @@ void NNUE::init() {
}

// Verifies that the last net used was loaded successfully
void NNUE::verify() {
void NNUE::verify(const OptionsMap& Options,
const std::unordered_map<Eval::NNUE::NetSize, EvalFile>& EvalFiles) {

for (const auto& [netSize, evalFile] : EvalFiles)
{
Expand Down Expand Up @@ -183,7 +182,7 @@ int Eval::simple_eval(const Position& pos, Color c) {

// Evaluate is the evaluator for the outer world. It returns a static evaluation
// of the position from the point of view of the side to move.
Value Eval::evaluate(const Position& pos) {
Value Eval::evaluate(const Position& pos, const Search::Worker& workerThread) {

assert(!pos.checkers());

Expand All @@ -204,7 +203,7 @@ Value Eval::evaluate(const Position& pos) {
Value nnue = smallNet ? NNUE::evaluate<NNUE::Small>(pos, true, &nnueComplexity)
: NNUE::evaluate<NNUE::Big>(pos, true, &nnueComplexity);

int optimism = pos.this_thread()->optimism[stm];
int optimism = workerThread.optimism[stm];

// Blend optimism and eval with nnue complexity and material imbalance
optimism += optimism * (nnueComplexity + std::abs(simpleEval - nnue)) / 512;
Expand All @@ -227,16 +226,16 @@ Value Eval::evaluate(const Position& pos) {
// a string (suitable for outputting to stdout) that contains the detailed
// descriptions and values of each evaluation term. Useful for debugging.
// Trace scores are from white's point of view
std::string Eval::trace(Position& pos) {
std::string Eval::trace(Position& pos, Search::Worker& workerThread) {

if (pos.checkers())
return "Final evaluation: none (in check)";

// Reset any global variable used in eval
pos.this_thread()->bestValue = VALUE_ZERO;
pos.this_thread()->rootSimpleEval = VALUE_ZERO;
pos.this_thread()->optimism[WHITE] = VALUE_ZERO;
pos.this_thread()->optimism[BLACK] = VALUE_ZERO;
workerThread.iterBestValue = VALUE_ZERO;
workerThread.rootSimpleEval = VALUE_ZERO;
workerThread.optimism[WHITE] = VALUE_ZERO;
workerThread.optimism[BLACK] = VALUE_ZERO;

std::stringstream ss;
ss << std::showpoint << std::noshowpos << std::fixed << std::setprecision(2);
Expand All @@ -249,7 +248,7 @@ std::string Eval::trace(Position& pos) {
v = pos.side_to_move() == WHITE ? v : -v;
ss << "NNUE evaluation " << 0.01 * UCI::to_cp(v) << " (white side)\n";

v = evaluate(pos);
v = evaluate(pos, workerThread);
v = pos.side_to_move() == WHITE ? v : -v;
ss << "Final evaluation " << 0.01 * UCI::to_cp(v) << " (white side)";
ss << " [with scaled NNUE, ...]";
Expand Down
25 changes: 15 additions & 10 deletions src/evaluate.h
Expand Up @@ -27,35 +27,40 @@
namespace Stockfish {

class Position;
namespace Search {
class Worker;
}

namespace Eval {

std::string trace(Position& pos);
std::string trace(Position& pos, Search::Worker& workerThread);

int simple_eval(const Position& pos, Color c);
Value evaluate(const Position& pos);
Value evaluate(const Position& pos, const Search::Worker& workerThread);

// The default net name MUST follow the format nn-[SHA256 first 12 digits].nnue
// for the build process (profile-build and fishtest) to work. Do not change the
// name of the macro, as it is used in the Makefile.
#define EvalFileDefaultNameBig "nn-baff1edbea57.nnue"
#define EvalFileDefaultNameSmall "nn-baff1ede1f90.nnue"

struct EvalFile {
std::string option_name;
std::string default_name;
std::string selected_name;
};

namespace NNUE {

enum NetSize : int;

void init();
void verify();
void init(const std::string& binaryDirector,
const OptionsMap& Options,
std::unordered_map<Eval::NNUE::NetSize, EvalFile>&);
void verify(const OptionsMap& Options, const std::unordered_map<Eval::NNUE::NetSize, EvalFile>&);

} // namespace NNUE

struct EvalFile {
std::string option_name;
std::string default_name;
std::string selected_name;
};

extern std::unordered_map<NNUE::NetSize, EvalFile> EvalFiles;

} // namespace Eval
Expand Down
17 changes: 6 additions & 11 deletions src/main.cpp
Expand Up @@ -16,36 +16,31 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <cstddef>
#include <iostream>

#include "bitboard.h"
#include "evaluate.h"
#include "misc.h"
#include "position.h"
#include "search.h"
#include "thread.h"
#include "tune.h"
#include "types.h"
#include "uci.h"
#include "ucioption.h"

using namespace Stockfish;

int main(int argc, char* argv[]) {

std::cout << engine_info() << std::endl;

CommandLine::init(argc, argv);
UCI::init(Options);
Tune::init();
UCI uci(argc, argv);

Tune::init(uci.options);
Bitboards::init();
Position::init();
Threads.set(size_t(Options["Threads"]));
Search::clear(); // After threads are up
Eval::NNUE::init();
Eval::NNUE::init(uci.workingDirectory(), uci.options, uci.EvalFiles);

UCI::loop(argc, argv);
uci.loop();

Threads.set(0);
return 0;
}
15 changes: 4 additions & 11 deletions src/misc.cpp
Expand Up @@ -721,17 +721,13 @@ void bindThisThread(size_t idx) {
#define GETCWD getcwd
#endif

namespace CommandLine {

std::string argv0; // path+name of the executable binary, as given by argv[0]
std::string binaryDirectory; // path of the executable directory
std::string workingDirectory; // path of the working directory

void init([[maybe_unused]] int argc, char* argv[]) {
CommandLine::CommandLine(int _argc, char** _argv) :
argc(_argc),
argv(_argv) {
std::string pathSeparator;

// Extract the path+name of the executable binary
argv0 = argv[0];
std::string argv0 = argv[0];

#ifdef _WIN32
pathSeparator = "\\";
Expand Down Expand Up @@ -766,7 +762,4 @@ void init([[maybe_unused]] int argc, char* argv[]) {
binaryDirectory.replace(0, 1, workingDirectory);
}


} // namespace CommandLine

} // namespace Stockfish
15 changes: 10 additions & 5 deletions src/misc.h
Expand Up @@ -176,12 +176,17 @@ namespace WinProcGroup {
void bindThisThread(size_t idx);
}

namespace CommandLine {
void init(int argc, char* argv[]);

extern std::string binaryDirectory; // path of the executable directory
extern std::string workingDirectory; // path of the working directory
}
struct CommandLine {
public:
CommandLine(int argc, char** argv);

int argc;
char** argv;

std::string binaryDirectory; // path of the executable directory
std::string workingDirectory; // path of the working directory
};

} // namespace Stockfish

Expand Down
4 changes: 3 additions & 1 deletion src/nnue/evaluate_nnue.cpp
Expand Up @@ -441,7 +441,9 @@ bool save_eval(std::ostream& stream, NetSize netSize) {
}

// 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;
Expand Down
8 changes: 6 additions & 2 deletions src/nnue/evaluate_nnue.h
Expand Up @@ -26,11 +26,13 @@
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>

#include "../evaluate.h"
#include "../misc.h"
#include "../types.h"
#include "nnue_architecture.h"
#include "nnue_feature_transformer.h"
#include "../types.h"

namespace Stockfish {
class Position;
Expand Down Expand Up @@ -75,7 +77,9 @@ void hint_common_parent_position(const Position& pos);

bool load_eval(const std::string name, std::istream& stream, NetSize netSize);
bool save_eval(std::ostream& stream, NetSize netSize);
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>&);

} // namespace Stockfish::Eval::NNUE

Expand Down
18 changes: 7 additions & 11 deletions src/position.cpp
Expand Up @@ -19,7 +19,6 @@
#include "position.h"

#include <algorithm>
#include <atomic>
#include <cassert>
#include <cctype>
#include <cstddef>
Expand All @@ -36,7 +35,6 @@
#include "movegen.h"
#include "nnue/nnue_common.h"
#include "syzygy/tbprobe.h"
#include "thread.h"
#include "tt.h"
#include "uci.h"

Expand Down Expand Up @@ -87,7 +85,7 @@ std::ostream& operator<<(std::ostream& os, const Position& pos) {
ASSERT_ALIGNED(&st, Eval::NNUE::CacheLineSize);

Position p;
p.set(pos.fen(), pos.is_chess960(), &st, pos.this_thread());
p.set(pos.fen(), pos.is_chess960(), &st);
Tablebases::ProbeState s1, s2;
Tablebases::WDLScore wdl = Tablebases::probe_wdl(p, &s1);
int dtz = Tablebases::probe_dtz(p, &s2);
Expand Down Expand Up @@ -160,7 +158,7 @@ void Position::init() {
// Initializes the position object with the given FEN string.
// This function is not very robust - make sure that input FENs are correct,
// this is assumed to be the responsibility of the GUI.
Position& Position::set(const string& fenStr, bool isChess960, StateInfo* si, Thread* th) {
Position& Position::set(const string& fenStr, bool isChess960, StateInfo* si) {
/*
A FEN string defines a particular position using only the ASCII character set.
Expand Down Expand Up @@ -286,8 +284,7 @@ Position& Position::set(const string& fenStr, bool isChess960, StateInfo* si, Th
// handle also common incorrect FEN with fullmove = 0.
gamePly = std::max(2 * (gamePly - 1), 0) + (sideToMove == BLACK);

chess960 = isChess960;
thisThread = th;
chess960 = isChess960;
set_state();

assert(pos_is_ok());
Expand Down Expand Up @@ -388,7 +385,7 @@ Position& Position::set(const string& code, Color c, StateInfo* si) {
string fenStr = "8/" + sides[0] + char(8 - sides[0].length() + '0') + "/8/8/8/8/" + sides[1]
+ char(8 - sides[1].length() + '0') + "/8 w - - 0 10";

return set(fenStr, false, si, nullptr);
return set(fenStr, false, si);
}


Expand Down Expand Up @@ -667,7 +664,6 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) {
assert(m.is_ok());
assert(&newSt != st);

thisThread->nodes.fetch_add(1, std::memory_order_relaxed);
Key k = st->key ^ Zobrist::side;

// Copy some fields of the old state to our new StateInfo object except the
Expand Down Expand Up @@ -959,7 +955,7 @@ void Position::do_castling(Color us, Square from, Square& to, Square& rfrom, Squ

// Used to do a "null move": it flips
// the side to move without executing any move on the board.
void Position::do_null_move(StateInfo& newSt) {
void Position::do_null_move(StateInfo& newSt, TranspositionTable& tt) {

assert(!checkers());
assert(&newSt != st);
Expand All @@ -982,7 +978,7 @@ void Position::do_null_move(StateInfo& newSt) {

st->key ^= Zobrist::side;
++st->rule50;
prefetch(TT.first_entry(key()));
prefetch(tt.first_entry(key()));

st->pliesFromNull = 0;

Expand Down Expand Up @@ -1235,7 +1231,7 @@ void Position::flip() {
std::getline(ss, token); // Half and full moves
f += token;

set(f, is_chess960(), st, this_thread());
set(f, is_chess960(), st);

assert(pos_is_ok());
}
Expand Down

0 comments on commit 9525fae

Please sign in to comment.