diff --git a/source/main.cpp b/source/main.cpp index 193a48a..61aec65 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -11,6 +11,7 @@ #include #include +#include #ifndef EVALFILE #define EVALFILE "./nnue.bin" @@ -57,89 +58,295 @@ static void ArgError(const char* value, const char* name) { << "\nRun 'Eleanor datagen --help' for usage.\n"; } -int main(int argc, char* argv[]) { - auto loadDefaultNet = [&]([[maybe_unused]] bool warnMSVC = false) { - #if defined(_MSC_VER) && !defined(__clang__) - NNUE::net.Load(EVALFILE); - if (warnMSVC) - cerr << "WARNING: This file was compiled with MSVC, this means that an nnue was NOT embedded into the exe." << endl; - #else - NNUE::net = *reinterpret_cast(gEVALData); - #endif - }; - - loadDefaultNet(true); +static void PrintUsage() { + std::cout << "Eleanor " << EleanorVersion << " - chess engine" << std::endl; + std::cout << std::endl; + std::cout << "Usage:" << std::endl; + std::cout << " Eleanor" << std::endl; + std::cout << " Eleanor bench" << std::endl; + std::cout << " Eleanor datagen [positions] [threads] [username]" << std::endl; + std::cout << " Eleanor datagen [options]" << std::endl; + std::cout << " Eleanor --help" << std::endl; + std::cout << " Eleanor --version" << std::endl; + std::cout << std::endl; + std::cout << "Commands:" << std::endl; + std::cout << " bench Run benchmark" << std::endl; + std::cout << " datagen Generate training data" << std::endl; + std::cout << " help Show this help" << std::endl; + std::cout << " version Show version" << std::endl; + std::cout << std::endl; + std::cout << "Options:" << std::endl; + std::cout << " -h, --help Show this help" << std::endl; + std::cout << " -v, --version Show version" << std::endl; + std::cout << " --bench Same as bench" << std::endl; + std::cout << " --datagen Same as datagen" << std::endl; + std::cout << " -p, --positions N Target size in thousands for datagen" << std::endl; + std::cout << " -t, --threads N Threads for datagen or UCI mode" << std::endl; + std::cout << " -u, --username NAME Online mode username for datagen" << std::endl; + std::cout << " --hash N Hash size in MB for UCI mode" << std::endl; + std::cout << " --evalfile PATH NNUE file to load for UCI mode" << std::endl; + std::cout << std::endl; + std::cout << "Datagen examples:" << std::endl; + std::cout << " Eleanor datagen 1000 22" << std::endl; + std::cout << " Eleanor datagen --positions 1000 --threads 22" << std::endl; + std::cout << " Eleanor datagen 1000 22 myuser" << std::endl; + std::cout << std::endl; + std::cout << "UCI go:" << std::endl; + std::cout << " go depth N" << std::endl; + std::cout << " go mate N" << std::endl; + std::cout << " go nodes N" << std::endl; + std::cout << " go movetime N" << std::endl; + std::cout << " go infinite" << std::endl; + std::cout << " go searchmoves MOVE ..." << std::endl; + std::cout << " go wtime N btime N winc N binc N movestogo N" << std::endl; +} - +static void PrintVersion() { + std::cout << "Eleanor " << EleanorVersion << std::endl; +} - MOVEGEN::initLeaperAttacks(); - MOVEGEN::initSliderAttacks(); +int main(int argc, char* argv[]) { + auto loadDefaultNet = [&]([[maybe_unused]] bool warnMSVC = false) { + #if defined(_MSC_VER) && !defined(__clang__) + NNUE::net.Load(EVALFILE); + if (warnMSVC) + cerr << "WARNING: This file was compiled with MSVC, this means that an nnue was NOT embedded into the exe." << endl; + #else + NNUE::net = *reinterpret_cast(gEVALData); + #endif + }; + loadDefaultNet(true); + MOVEGEN::initLeaperAttacks(); + MOVEGEN::initSliderAttacks(); UTILS::InitZobrist(); #ifdef TUNING SEARCH::RefreshTunableCaches(); #else SEARCH::InitLMRTable(); #endif + Board board; + if (argc <= 1) { + UCILoop(board); + return 0; + } + std::string first = argv[1]; + if (first == "--help" || first == "-h" || first == "help") { + PrintUsage(); + return 0; + } + if (first == "--version" || first == "-v" || first == "version") { + PrintVersion(); + return 0; + } + if (first == "bench" || first == "--bench") { + if (argc > 2) { + std::string second = argv[2]; + if (second == "--help" || second == "-h" || second == "help") { + PrintUsage(); + return 0; + } + } + RunBenchmark(); + return 0; + } + if (first == "datagen" || first == "--datagen") { + int positions = 0; + int datagenThreads = 1; + std::string username = ""; + bool hasPositions = false; + bool hasThreads = false; + std::vector positional; + for (int i = 2; i < argc; i++) { + std::string arg = argv[i]; + if (arg == "--help" || arg == "-h" || arg == "help") { + DATAGEN::PrintUsage(); + return 0; + } else if (arg == "--positions" || arg == "-p") { + if (i + 1 >= argc) { + std::cerr << "error: missing value for '" << arg << "'." << std::endl; + std::cerr << std::endl; + std::cerr << "Run 'Eleanor datagen --help' for usage." << std::endl; + return 1; + } + positions = ParsePositiveInt(argv[++i]); + if (positions == 0) { + ArgError(argv[i], "positions"); + return 1; + } + positions *= 1000; + hasPositions = true; + } else if (arg.rfind("--positions=", 0) == 0) { + positions = ParsePositiveInt(arg.substr(12).c_str()); + if (positions == 0) { + ArgError(arg.substr(12).c_str(), "positions"); + return 1; + } + positions *= 1000; + hasPositions = true; + } else if (arg == "--threads" || arg == "-t") { + if (i + 1 >= argc) { + std::cerr << "error: missing value for '" << arg << "'." << std::endl; + std::cerr << std::endl; + std::cerr << "Run 'Eleanor datagen --help' for usage." << std::endl; + return 1; + } + datagenThreads = ParsePositiveInt(argv[++i]); + if (datagenThreads == 0) { + ArgError(argv[i], "threads"); + return 1; + } + hasThreads = true; + } else if (arg.rfind("--threads=", 0) == 0) { + datagenThreads = ParsePositiveInt(arg.substr(10).c_str()); + if (datagenThreads == 0) { + ArgError(arg.substr(10).c_str(), "threads"); + return 1; + } + hasThreads = true; + } else if (arg == "--username" || arg == "-u") { + if (i + 1 >= argc) { + std::cerr << "error: missing value for '" << arg << "'." << std::endl; + std::cerr << std::endl; + std::cerr << "Run 'Eleanor datagen --help' for usage." << std::endl; + return 1; + } + username = argv[++i]; + } else if (arg.rfind("--username=", 0) == 0) { + username = arg.substr(11); + } else if (!arg.empty() && arg[0] == '-') { + std::cerr << "error: unknown option '" << arg << "'." << std::endl; + std::cerr << std::endl; + std::cerr << "Run 'Eleanor datagen --help' for usage." << std::endl; + return 1; + } else { + positional.push_back(arg); + } + } + size_t posIndex = 0; + if (!hasPositions && posIndex < positional.size()) { + positions = ParsePositiveInt(positional[posIndex].c_str()); + if (positions == 0) { + ArgError(positional[posIndex].c_str(), "positions"); + return 1; + } + positions *= 1000; + hasPositions = true; + posIndex++; + } + if (!hasThreads && posIndex < positional.size()) { + datagenThreads = ParsePositiveInt(positional[posIndex].c_str()); + if (datagenThreads == 0) { + ArgError(positional[posIndex].c_str(), "threads"); + return 1; + } + hasThreads = true; + posIndex++; + } + if (username.empty() && posIndex < positional.size()) { + username = positional[posIndex]; + posIndex++; + } + if (posIndex < positional.size()) { + std::cerr << "error: unexpected argument '" << positional[posIndex] << "'." << std::endl; + std::cerr << std::endl; + std::cerr << "Run 'Eleanor datagen --help' for usage." << std::endl; + return 1; + } + if (!hasPositions) + positions = 1000; + if (!hasThreads) { + std::cerr << "error: threads must be at least 2." << std::endl; + std::cerr << std::endl; + std::cerr << "Run 'Eleanor datagen --help' for usage." << std::endl; + return 1; + } + if (datagenThreads < 2) { + std::cerr << "error: threads must be at least 2 (got " << datagenThreads << "). The main thread only renders the progress screen, so " << datagenThreads << " leaves no worker to generate." << std::endl; + std::cerr << std::endl; + std::cerr << "Run 'Eleanor datagen --help' for usage." << std::endl; + return 1; + } + if (!username.empty()) { + DATAGEN::RunOnline(username, positions, datagenThreads); + } else { + DATAGEN::Run(positions, datagenThreads); + } + return 0; + } + if (!first.empty() && first[0] == '-') { + for (int i = 1; i < argc; i++) { + std::string arg = argv[i]; + if (arg == "--help" || arg == "-h" || arg == "help") { + PrintUsage(); + return 0; + } else if (arg == "--version" || arg == "-v" || arg == "version") { + PrintVersion(); + return 0; + } else if (arg == "--threads" || arg == "-t") { + if (i + 1 >= argc) { + std::cerr << "error: missing value for '" << arg << "'." << std::endl; + std::cerr << std::endl; + std::cerr << "Run 'Eleanor --help' for usage." << std::endl; + return 1; + } + int value = ParsePositiveInt(argv[++i]); + if (value < 1 || value > 512) { + ArgError(argv[i], "threads"); + return 1; + } + threads = value; + } else if (arg.rfind("--threads=", 0) == 0) { + int value = ParsePositiveInt(arg.substr(10).c_str()); + if (value < 1 || value > 512) { + ArgError(arg.substr(10).c_str(), "threads"); + return 1; + } + threads = value; + } else if (arg == "--hash") { + if (i + 1 >= argc) { + std::cerr << "error: missing value for '" << arg << "'." << std::endl; + std::cerr << std::endl; + std::cerr << "Run 'Eleanor --help' for usage." << std::endl; + return 1; + } + int value = ParsePositiveInt(argv[++i]); + if (value < 1 || value > 1024) { + ArgError(argv[i], "hash"); + return 1; + } + U64 hashSize = (U64(value) * 1000000ULL) / sizeof(TTBucket); + SharedTT.Resize(hashSize); + } else if (arg.rfind("--hash=", 0) == 0) { + int value = ParsePositiveInt(arg.substr(7).c_str()); + if (value < 1 || value > 1024) { + ArgError(arg.substr(7).c_str(), "hash"); + return 1; + } + U64 hashSize = (U64(value) * 1000000ULL) / sizeof(TTBucket); + SharedTT.Resize(hashSize); + } else if (arg == "--evalfile") { + if (i + 1 >= argc) { + std::cerr << "error: missing value for '" << arg << "'." << std::endl; + std::cerr << std::endl; + std::cerr << "Run 'Eleanor --help' for usage." << std::endl; + return 1; + } + NNUE::net.Load(argv[++i]); + } else if (arg.rfind("--evalfile=", 0) == 0) { + NNUE::net.Load(arg.substr(11)); + } else { + std::cerr << "error: unknown option '" << arg << "'." << std::endl; + std::cerr << std::endl; + std::cerr << "Run 'Eleanor --help' for usage." << std::endl; + return 1; + } + } + UCILoop(board); + return 0; + } + std::cerr << "error: unknown command '" << first << "'." << std::endl; + std::cerr << std::endl; + std::cerr << "Run 'Eleanor --help' for usage." << std::endl; + return 1; +} - Board board; - - if (argc > 1) { - if (std::string(argv[1]) == "bench") { - RunBenchmark(); - } else if (std::string(argv[1]) == "datagen") { - if (argc > 2) { - const std::string arg = argv[2]; - - if (arg == "--help" || arg == "-h" || arg == "help") { - DATAGEN::PrintUsage(); - return 0; - } - } - - int positions = 1; - int threads = 1; - std::string username = ""; - - if (argc > 2) { - positions = ParsePositiveInt(argv[2]); - if (positions == 0) { - ArgError(argv[2], "positions"); - return 1; - } - positions *= 1000; - - if (argc > 3) { - threads = ParsePositiveInt(argv[3]); - if (threads == 0) { - ArgError(argv[3], "threads"); - return 1; - } - - if (argc > 4) { - username = argv[4]; - } - } - } - - // Only the main thread renders, so it never generates anything. - // Checked outside the argc guards so a bare "datagen" is caught too. - if (threads < 2) { - std::cerr << "error: threads must be at least 2 (got " << threads - << "). The main thread only renders the progress" - << " screen, so " << threads << " leaves no worker to generate.\n" - << "\nRun 'Eleanor datagen --help' for usage.\n"; - return 1; - } - - if (!username.empty()) { - DATAGEN::RunOnline(username, positions, threads); - } else { - DATAGEN::Run(positions, threads); - } - } - } else { - UCILoop(board); - } - - return 0; -} diff --git a/source/search.cpp b/source/search.cpp index 02d709a..4163913 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -584,6 +584,18 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC if (ctx->excluded == currMove) continue; + if (ply == 0 && !ctx->searchMoves.empty()) { + bool found = false; + for (Move &m : ctx->searchMoves) { + if (m.MoveFrom() == currMove.MoveFrom() && m.MoveTo() == currMove.MoveTo() && m.GetFlags() == currMove.GetFlags()) { + found = true; + break; + } + } + if (!found) + continue; + } + bool notMated = results.score > (-MATE_SCORE + MAX_DEPTH); int lmrDepth = depth - lmrTable[currMove.IsQuiet()][depth][moveSeen]; @@ -855,6 +867,13 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext* ctx) { if constexpr (mode == bench) { toDepth = BENCH_DEPTH; + } else if constexpr (mode != datagen) { + int depthLimit = MAX_DEPTH; + if (params.depth > 0) + depthLimit = std::min(params.depth, MAX_DEPTH); + if (params.mate > 0) + depthLimit = std::min({depthLimit, params.mate * 2, MAX_DEPTH}); + toDepth = depthLimit; } AspirationWindow aw; @@ -865,9 +884,23 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext* ctx) { ctx->sw.Restart(); + bool hasTime = (fullTime > 0) || (params.movetime > 0); + bool useTime = hasTime && !params.infinite; + for (int depth = 1; depth <= toDepth; depth++) { - ctx->timeToSearch = std::max((fullTime / movesToGo) + (inc / 2), 4); - int softTime = ctx->timeToSearch * 0.65 * nodeScaling; + if (params.movetime > 0 && !params.infinite) { + ctx->timeToSearch = params.movetime; + } else if (useTime) { + ctx->timeToSearch = std::max((fullTime / movesToGo) + (inc / 2), 4); + } else { + ctx->timeToSearch = 2000000000; + } + int softTime = 2000000000; + if (params.movetime > 0 && !params.infinite) { + softTime = params.movetime; + } else if (useTime) { + softTime = int(ctx->timeToSearch * 0.65 * nodeScaling); + } ctx->seldepth = 0; ctx->rootDepth = depth; @@ -904,7 +937,7 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext* ctx) { PrintSearchInfo(board, ctx, safeResults, depth, elapsed); if constexpr (mode == normal) { - if (ctx->sw.GetElapsedMS() >= softTime) { + if (useTime && ctx->sw.GetElapsedMS() >= softTime) { searchStopped.store(true, std::memory_order_relaxed); break; } @@ -933,6 +966,7 @@ SearchResults SearchPosition(Board &board, SearchParams params, SearchContext* c ctx->seldepth = 0; ctx->nodesTable = {}; + ctx->searchMoves = params.searchMoves; if constexpr (mode != bench) { ctx->nodes = 0; diff --git a/source/search.h b/source/search.h index 37ec93a..4fceee4 100644 --- a/source/search.h +++ b/source/search.h @@ -218,6 +218,7 @@ public: U64 nodes = 0; U64 nodesToGo = 0; int timeToSearch = 0; + std::vector searchMoves; std::array nodesTable{}; diff --git a/source/uci.cpp b/source/uci.cpp index 9991abd..b346f00 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -208,6 +208,10 @@ static void StartSearchThread(Board& board, SearchParams params, SEARCH::SearchC } #endif +static bool IsGoToken(const std::string &token) { + return token == "searchmoves" || token == "ponder" || token == "wtime" || token == "btime" || token == "winc" || token == "binc" || token == "movestogo" || token == "depth" || token == "nodes" || token == "mate" || token == "movetime" || token == "infinite"; +} + static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext* ctx) { StopSearchThreads(); searchStopped.store(false, std::memory_order_relaxed); @@ -221,15 +225,46 @@ static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext* c params.binc = ReadParam("binc", command); params.movesToGo = ReadParam("movestogo", command); params.nodes = ReadParam("nodes", command); + params.depth = ReadParam("depth", command); + params.mate = ReadParam("mate", command); + params.movetime = ReadParam("movetime", command); - if (command.find("movetime") != std::string::npos) { - params.wtime = ReadParam("movetime", command); - params.btime = ReadParam("movetime", command); - } else if (command.find("infinite") != std::string::npos) { - params.wtime = 99999999; - params.btime = 99999999; + if (params.depth < 0) + params.depth = 0; + if (params.mate < 0) + params.mate = 0; + if (params.movetime < 0) + params.movetime = 0; + if (params.nodes < 0) + params.nodes = 0; + if (params.depth > SEARCH::MAX_DEPTH) + params.depth = SEARCH::MAX_DEPTH; + if (params.mate * 2 > SEARCH::MAX_DEPTH) + params.mate = SEARCH::MAX_DEPTH / 2; + + if (command.find("infinite") != std::string::npos) + params.infinite = true; + if (command.find("ponder") != std::string::npos) + params.infinite = true; + + size_t searchmovesPos = command.find("searchmoves"); + if (searchmovesPos != std::string::npos) { + std::vector tokens = UTILS::split(command.substr(searchmovesPos + 11), ' '); + for (std::string &token : tokens) { + if (token.empty()) + continue; + if (IsGoToken(token)) + break; + if (token.size() < 4) + continue; + Move m = UTILS::parseMove(board, token); + if (m != 0) + params.searchMoves.push_back(m); + } } + ctx->searchMoves = params.searchMoves; + searchThreads.reserve(threads); for (int i = 0; i < threads; i++) { StartSearchThread(board, params, ctx, i); @@ -301,7 +336,7 @@ static void SetOption(std::string& command, SEARCH::SearchContext* ctx) { } static void PrintEngineInfo() { - std::cout << "id name Eleanor v4.1" << std::endl; + std::cout << "id name Eleanor v" << EleanorVersion << std::endl; std::cout << "id author rektdie" << std::endl; std::cout << "option name Hash type spin default 8 min 1 max 1024" << std::endl; std::cout << "option name Threads type spin default 1 min 1 max 512" << std::endl; @@ -355,7 +390,7 @@ void UCILoop(Board &board) { } // parse UCI "go" command - if (input.find("go") != std::string::npos) { + if (input == "go" || input.rfind("go ", 0) == 0) { ParseGo(board, input, ctx.get()); continue; } diff --git a/source/uci.h b/source/uci.h index 4e3553c..e81046e 100644 --- a/source/uci.h +++ b/source/uci.h @@ -1,5 +1,7 @@ #pragma once #include +#include +#include #include "move.h" #include "board.h" @@ -8,6 +10,8 @@ inline bool UCIShowWDL = false; inline int threads = 1; inline std::atomic searchStopped = false; +constexpr std::string_view EleanorVersion = "4.1"; + class SearchParams { public: int wtime = 0; @@ -16,6 +20,11 @@ public: int binc = 0; int movesToGo = 0; int nodes = 0; + int depth = 0; + int mate = 0; + int movetime = 0; + bool infinite = false; + std::vector searchMoves; int threads = 1;