Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split Glue File & Prepare for Sf 17 #2

Merged
merged 3 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@
stockfish_repo = "https://github.com/official-stockfish/Stockfish"
fairy_stockfish_repo = "https://github.com/fairy-stockfish/Fairy-Stockfish"

# compatiblity modes:
# - sfhce: VERSION_0
# - fsf14, sf16-7, sf16-40: VERSION_1
# - sf16.1: VERSION_2

targets = {
"fsf14": {"url": fairy_stockfish_repo, "commit": "a621470", "cxx_flags": "-DLSFW_NNUE_COUNT=1"},
"sf16-7": {"url": stockfish_repo, "commit": "68e1e9b", "cxx_flags": "-DLSFW_NNUE_COUNT=1"},
"sf16-40": {"url": stockfish_repo, "commit": "68e1e9b", "cxx_flags": "-DLSFW_NNUE_COUNT=1"},
"sf161-70": {"url": stockfish_repo, "commit": "e67cc97", "cxx_flags": "-DLSFW_NNUE_COUNT=2"}, # 16.1
"sfhce": {"url": stockfish_repo, "commit": "9587eee"}, # sf classical
"fsf14": {"url": fairy_stockfish_repo, "commit": "a621470", "cxx_flags": "", "version": "version_1"},
"sf16-7": {"url": stockfish_repo, "commit": "68e1e9b", "cxx_flags": "", "version": "version_1"},
"sf16-40": {"url": stockfish_repo, "commit": "68e1e9b", "cxx_flags": "", "version": "version_1"},
"sf161-70": {"url": stockfish_repo, "commit": "e67cc97", "cxx_flags": "", "version": "version_2"}, # 16.1
"sfhce": {"url": stockfish_repo, "commit": "9587eee", "version": "version_0"}, # sf classical
}

script_dir = os.path.dirname(os.path.realpath(__file__))
Expand All @@ -25,6 +30,8 @@

def makefile(target, sources, flags, link_flags):
flags = " ".join([flags.strip(), targets[target].get("cxx_flags", "").strip()])
version = targets[target].get("version")
glue_filename = f"glue_{version}"
# DO NOT replace tabs with spaces
# fmt: off
return f"""
Expand All @@ -43,15 +50,15 @@ def makefile(target, sources, flags, link_flags):
-sALLOW_BLOCKING_ON_MAIN_THREAD=0 -sEXIT_RUNTIME -Wno-pthreads-mem-growth

SRCS = {sources}
OBJS = $(addprefix src/, $(SRCS:.cpp=.o)) src/glue.o
OBJS = $(addprefix src/, $(SRCS:.cpp=.o)) src/{glue_filename}.o

$(EXE).js: $(OBJS)
$(CXX) $(CXX_FLAGS) $(LD_FLAGS) $(OBJS) -o $(EXE).js

%.o: %.cpp
$(CXX) $(CXX_FLAGS) -c $< -o $@

src/glue.o: ../../src/glue.cpp
src/{glue_filename}.o: ../../src/{glue_filename}.cpp
$(CXX) $(CXX_FLAGS) -c $< -o $@

"""
Expand Down
116 changes: 0 additions & 116 deletions src/glue.cpp

This file was deleted.

58 changes: 58 additions & 0 deletions src/glue.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#pragma once

#include <emscripten.h>
#include <emscripten/bind.h>
#include <queue>

struct Command : public std::streambuf {
enum { UCI, NNUE } type;
std::string uci;
std::shared_ptr<void> ptr = nullptr;
int index;

Command(const char* text) : type(UCI), uci(text) { std::free((void*)text); }
Command(char* buf, size_t sz, int index)
: type(NNUE), ptr((void*)buf, std::free), index(index) {
setg(buf, buf, buf + sz);
}

using std::streambuf::seekoff;
using std::streambuf::seekpos;
};

struct {
std::mutex m;
std::queue<Command> q;
std::condition_variable cv;

void push(Command el) {
std::unique_lock<std::mutex> lock(m);
q.push(el);
lock.unlock();
cv.notify_one();
}

Command pop() {
std::unique_lock<std::mutex> lock(m);
while (q.empty()) cv.wait(lock);
Command el = std::move(q.front());
q.pop();
return el;
}
} inQ;

const char* getRecommendedNnueName(int index);

EMSCRIPTEN_KEEPALIVE std::string js_getline();

extern "C" {
EMSCRIPTEN_KEEPALIVE void uci(const char* utf8) { inQ.push(Command(utf8)); }

EMSCRIPTEN_KEEPALIVE void setNnueBuffer(char* buf, size_t sz, int index) {
inQ.push(Command(buf, sz, index));
}

EMSCRIPTEN_KEEPALIVE const char* getRecommendedNnue(int index) {
return getRecommendedNnueName(index);
}
}
33 changes: 33 additions & 0 deletions src/glue_version_0.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "glue.hpp"

#include "evaluate.h"
#include "syzygy/tbprobe.h"
#include "uci.h"

#define GET_USE_NNUE(x) ""
#define EvalFileDefaultNameBig ""
#define EvalFileDefaultNameSmall ""

EMSCRIPTEN_KEEPALIVE std::string js_getline() {
auto cmd = inQ.pop();
if (cmd.type == cmd.UCI) return cmd.uci;

return "";
}

const char* getRecommendedNnueName(int index) {
return index == 1 ? EvalFileDefaultNameSmall : EvalFileDefaultNameBig;
}

// stubs for tbprobe.cpp (so we don't need -sALLOW_UNIMPLEMENTED_SYSCALLS)

namespace Tablebases {
int MaxCardinality = 0;
void init(const std::string& paths) {}
WDLScore probe_wdl(Position& pos, ProbeState* result) { return WDLDraw; }
int probe_dtz(Position& pos, ProbeState* result) { return 0; }

bool root_probe(Position& pos, Search::RootMoves& rootMoves) { return false; }
bool root_probe_wdl(Position& pos, Search::RootMoves& rootMoves) { return false; }
} // namespace Tablebases

44 changes: 44 additions & 0 deletions src/glue_version_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "glue.hpp"

#include "evaluate.h"
#include "syzygy/tbprobe.h"
#include "uci.h"

# include "nnue/evaluate_nnue.h"
# include "nnue/nnue_architecture.h"

#define GET_USE_NNUE(x) Stockfish::Options.count("Use NNUE") > 0 ? "setoption name Use NNUE value " + std::string(x ? "true" : "false") : ""
#define EvalFileDefaultNameBig EvalFileDefaultName
#define EvalFileDefaultNameSmall EvalFileDefaultName

EMSCRIPTEN_KEEPALIVE std::string js_getline() {
auto cmd = inQ.pop();
if (cmd.type == cmd.UCI)
return cmd.uci;
else if (cmd.type == cmd.NNUE) {
if (!cmd.ptr) return GET_USE_NNUE(false);
std::istream in(&cmd);
auto success =
Stockfish::Eval::NNUE::load_eval("", in) ? std::make_optional(0) : std::nullopt;
if (!success.has_value()) std::cerr << "BAD_NNUE " << cmd.index << std::endl;
return GET_USE_NNUE(success.has_value());
}
return "";
}

const char* getRecommendedNnueName(int index) {
return index == 1 ? EvalFileDefaultNameSmall : EvalFileDefaultNameBig;
}

// stubs for tbprobe.cpp (so we don't need -sALLOW_UNIMPLEMENTED_SYSCALLS)
namespace Stockfish::Tablebases {

int MaxCardinality = 0;
void init(const std::string& paths) {}
WDLScore probe_wdl(Position& pos, ProbeState* result) { return WDLDraw; }
int probe_dtz(Position& pos, ProbeState* result) { return 0; }

bool root_probe(Position& pos, Search::RootMoves& rootMoves) { return false; }
bool root_probe_wdl(Position& pos, Search::RootMoves& rootMoves) { return false; }
} // namespace Stockfish::Tablebases

44 changes: 44 additions & 0 deletions src/glue_version_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "glue.hpp"

#include "evaluate.h"
#include "syzygy/tbprobe.h"
#include "uci.h"

# include "nnue/evaluate_nnue.h"
# include "nnue/nnue_architecture.h"

#define GET_USE_NNUE(x) ""

EMSCRIPTEN_KEEPALIVE std::string js_getline() {
auto cmd = inQ.pop();
if (cmd.type == cmd.UCI)
return cmd.uci;
else if (cmd.type == cmd.NNUE) {
if (!cmd.ptr) return GET_USE_NNUE(false);
std::istream in(&cmd);
auto success =
Stockfish::Eval::NNUE::load_eval(in, Stockfish::Eval::NNUE::NetSize(cmd.index));
if (!success.has_value()) std::cerr << "BAD_NNUE " << cmd.index << std::endl;
return GET_USE_NNUE(success.has_value());
}
return "";
}

const char* getRecommendedNnueName(int index) {
return index == 1 ? EvalFileDefaultNameSmall : EvalFileDefaultNameBig;
}

// stubs for tbprobe.cpp (so we don't need -sALLOW_UNIMPLEMENTED_SYSCALLS)
namespace Stockfish::Tablebases {

int MaxCardinality = 0;
void init(const std::string& paths) {}
WDLScore probe_wdl(Position& pos, ProbeState* result) { return WDLDraw; }
int probe_dtz(Position& pos, ProbeState* result) { return 0; }
bool root_probe(Position& pos, Search::RootMoves& rootMoves, bool rule50) { return false; }
bool root_probe_wdl(Position& pos, Search::RootMoves& rootMoves, bool rule50) { return false; }
Config rank_root_moves(const OptionsMap& options, Position& pos, Search::RootMoves& rootMoves) {
return Config();
}
} // namespace Stockfish::Tablebases