From 14395cbb9fb8dbd2bbfde210da1a976a46bf56d9 Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 31 Mar 2025 00:07:56 +0200 Subject: [PATCH 01/27] introduced marlinformat and compress pieces function in utils --- source/datagen.h | 29 +++++++++++++++++++++++++++++ source/utils.cpp | 29 +++++++++++++++++++++++++++++ source/utils.h | 3 +++ 3 files changed, 61 insertions(+) create mode 100644 source/datagen.h diff --git a/source/datagen.h b/source/datagen.h new file mode 100644 index 0000000..4e47452 --- /dev/null +++ b/source/datagen.h @@ -0,0 +1,29 @@ +#pragma once +#include "board.h" +#include "utils.h" + +struct PackedBoard { + uint64_t occupancy; // 8 bytes: Bitboard representing all occupied squares (includes all pieces) + std::array pieces; // 32 bytes: 64 squares stored in 4-bit format (2 squares per byte) + uint8_t sideToMove; // 1 byte: 0 = White to move, 1 = Black to move + uint8_t castlingRights; // 1 byte: 4-bit bitmask indicating available castling rights + uint8_t enPassantTarget; // 1 byte: En passant target square (0-63) or 64 if no en passant is available + uint8_t halfmoveClock; // 1 byte: Halfmove counter for the 50-move rule (incremented after each move) + uint16_t fullmoveNumber; // 2 bytes: Full move counter, starting at 1 (increments after Black's move) + int16_t eval; // 2 bytes: Static evaluation score (e.g., centipawns, positive = White advantage) + uint8_t wdl; // 1 byte: Win-Draw-Loss classification (for training or result storage) + uint8_t extra; // 1 byte: Reserved for future use or additional data + + void packFrom(Board& board, int16_t eval, uint8_t wdl) { + occupancy = board.occupied; + pieces = UTILS::CompressPieces(board); + sideToMove = board.sideToMove; + castlingRights = board.castlingRights; + enPassantTarget = (board.enPassantTarget == noEPTarget) ? 64 : board.enPassantTarget; + halfmoveClock = board.halfMoves; + fullmoveNumber = board.fullMoves; + this->eval = eval; + this->wdl = wdl; + extra = 0; + } +}; diff --git a/source/utils.cpp b/source/utils.cpp index a9b645c..b8a057f 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -146,4 +146,33 @@ Move parseMove(Board &board, std::string_view str) { return Move(from, to, flag); } + +std::array CompressPieces(Board &board) { + std::array compressed; + + for (int square = a1; square <= h8; square++) { + int pieceCode = 0; + + for (int pieceType = Pawn; pieceType <= King; pieceType++) { + if (board.pieces[pieceType].IsSet(square)) { + if (board.pieces[pieceType] & board.colors[White]) { + pieceCode = pieceType + 1; // White pieces: 1-6 + } else { + pieceCode = pieceType + 7; // Black pieces: 7-12 + } + break; + } + } + + int byteIndex = square / 2; + + if (square % 2 == 0) { + compressed[byteIndex] = (compressed[byteIndex] & 0xF0) | (pieceCode & 0x0F); + } else { + compressed[byteIndex] = (compressed[byteIndex] & 0x0F) | ((pieceCode & 0x0F) << 4); + } + } + + return compressed; +} } diff --git a/source/utils.h b/source/utils.h index 0156f55..3a8c575 100644 --- a/source/utils.h +++ b/source/utils.h @@ -29,4 +29,7 @@ std::vector split(std::string_view str, char delim); int parseSquare(std::string_view str); Move parseMove(Board &board, std::string_view str); + +std::array CompressPieces(Board &board); + } -- 2.47.3 From c80adecb6e27bb8cf45484e09481e05ed39b7ec3 Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 31 Mar 2025 00:45:32 +0200 Subject: [PATCH 02/27] changed to match marlinformat --- source/datagen.h | 8 ++------ source/utils.cpp | 32 +++++++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/source/datagen.h b/source/datagen.h index 4e47452..9e149e2 100644 --- a/source/datagen.h +++ b/source/datagen.h @@ -5,9 +5,7 @@ struct PackedBoard { uint64_t occupancy; // 8 bytes: Bitboard representing all occupied squares (includes all pieces) std::array pieces; // 32 bytes: 64 squares stored in 4-bit format (2 squares per byte) - uint8_t sideToMove; // 1 byte: 0 = White to move, 1 = Black to move - uint8_t castlingRights; // 1 byte: 4-bit bitmask indicating available castling rights - uint8_t enPassantTarget; // 1 byte: En passant target square (0-63) or 64 if no en passant is available + uint8_t stmEPSquare; // 1 byte: STM (side to move) in bit 7, EP square in bits 0-5 uint8_t halfmoveClock; // 1 byte: Halfmove counter for the 50-move rule (incremented after each move) uint16_t fullmoveNumber; // 2 bytes: Full move counter, starting at 1 (increments after Black's move) int16_t eval; // 2 bytes: Static evaluation score (e.g., centipawns, positive = White advantage) @@ -17,9 +15,7 @@ struct PackedBoard { void packFrom(Board& board, int16_t eval, uint8_t wdl) { occupancy = board.occupied; pieces = UTILS::CompressPieces(board); - sideToMove = board.sideToMove; - castlingRights = board.castlingRights; - enPassantTarget = (board.enPassantTarget == noEPTarget) ? 64 : board.enPassantTarget; + uint8_t stm_ep_square = (board.sideToMove<< 7) | (board.enPassantTarget & 0x3F); halfmoveClock = board.halfMoves; fullmoveNumber = board.fullMoves; this->eval = eval; diff --git a/source/utils.cpp b/source/utils.cpp index b8a057f..44e118b 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -148,24 +148,45 @@ Move parseMove(Board &board, std::string_view str) { } std::array CompressPieces(Board &board) { + const uint8_t UNMOVED_ROOK = 0x10; // Special value for unmoved rooks + std::array compressed; - + for (int square = a1; square <= h8; square++) { int pieceCode = 0; for (int pieceType = Pawn; pieceType <= King; pieceType++) { - if (board.pieces[pieceType].IsSet(square)) { - if (board.pieces[pieceType] & board.colors[White]) { - pieceCode = pieceType + 1; // White pieces: 1-6 + Bitboard pieceBitboard = board.pieces[pieceType]; + + if (pieceBitboard.IsSet(square)) { + if (board.colors[White] & board.pieces[pieceType]) { + pieceCode = pieceType + 1; // White pieces: 1-6 } else { - pieceCode = pieceType + 7; // Black pieces: 7-12 + pieceCode = pieceType + 7; // Black pieces: 7-12 + } + + // If it's a rook and hasn't moved (check castling rights) + if (pieceType == Rook) { + if (board.sideToMove == White) { + // If the white rooks are still eligible for castling, they haven't moved + if ((board.castlingRights & (whiteKingRight | whiteQueenRight)) != 0) { + pieceCode |= UNMOVED_ROOK; // Mark as unmoved rook + } + } else if (board.sideToMove == Black) { + // If the black rooks are still eligible for castling, they haven't moved + if ((board.castlingRights & (blackKingRight | blackQueenRight)) != 0) { + pieceCode |= UNMOVED_ROOK; // Mark as unmoved rook + } + } } break; } } + // Determine the byte index for storing the piece code (since we are packing 2 pieces per byte) int byteIndex = square / 2; + // Pack the piece code into the byte array if (square % 2 == 0) { compressed[byteIndex] = (compressed[byteIndex] & 0xF0) | (pieceCode & 0x0F); } else { @@ -175,4 +196,5 @@ std::array CompressPieces(Board &board) { return compressed; } + } -- 2.47.3 From 3fe79ab4972cd90005bfb641e546aea249e721c9 Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 31 Mar 2025 01:14:57 +0200 Subject: [PATCH 03/27] made some extra structs and function --- source/datagen.cpp | 23 +++++++++++++++++++++++ source/datagen.h | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 source/datagen.cpp diff --git a/source/datagen.cpp b/source/datagen.cpp new file mode 100644 index 0000000..8f46e0e --- /dev/null +++ b/source/datagen.cpp @@ -0,0 +1,23 @@ +#include +#include "datagen.h" +#include "movegen.h" + +namespace DATAGEN { + +static void PlayRandMoves(Board &board) { + std::random_device dev; + std::mt19937 rng(dev()); + + for (int count = 0; count < RAND_MOVES; count++) { + MOVEGEN::GenerateMoves(board); + std::uniform_int_distribution dist(0, board.currentMoveIndex); + + bool isLegal = board.MakeMove(board.moveList[dist(rng)]); + if (!isLegal) count--; + } +} + +void Run() { + +} +} \ No newline at end of file diff --git a/source/datagen.h b/source/datagen.h index 9e149e2..c9093bb 100644 --- a/source/datagen.h +++ b/source/datagen.h @@ -2,14 +2,20 @@ #include "board.h" #include "utils.h" -struct PackedBoard { +namespace DATAGEN { + +constexpr int SOFT_NODES = 5000; +constexpr int HARD_NODES = 100000; +constexpr int RAND_MOVES = 8; + +struct MarlinFormat { uint64_t occupancy; // 8 bytes: Bitboard representing all occupied squares (includes all pieces) std::array pieces; // 32 bytes: 64 squares stored in 4-bit format (2 squares per byte) uint8_t stmEPSquare; // 1 byte: STM (side to move) in bit 7, EP square in bits 0-5 uint8_t halfmoveClock; // 1 byte: Halfmove counter for the 50-move rule (incremented after each move) uint16_t fullmoveNumber; // 2 bytes: Full move counter, starting at 1 (increments after Black's move) - int16_t eval; // 2 bytes: Static evaluation score (e.g., centipawns, positive = White advantage) - uint8_t wdl; // 1 byte: Win-Draw-Loss classification (for training or result storage) + int16_t eval; // 2 bytes: Search score (e.g., centipawns, positive = White advantage) + uint8_t wdl; // 1 byte: 0 = white loss, 1 = draw, 2 = white win uint8_t extra; // 1 byte: Reserved for future use or additional data void packFrom(Board& board, int16_t eval, uint8_t wdl) { @@ -23,3 +29,26 @@ struct PackedBoard { extra = 0; } }; + +struct ScoredMove { + uint16_t move; + int16_t score; + + ScoredMove(Move m, int s) { + move = m; + score = s; + } +}; + +struct Game { + MarlinFormat format; + std::vector moves; + + Game(MarlinFormat f, std::vector m) { + format = f; + moves = m; + } +}; + +void Run(); +} \ No newline at end of file -- 2.47.3 From 15a8e1c1340c7e923b5de1c192689e359d0fe07a Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 31 Mar 2025 02:11:42 +0200 Subject: [PATCH 04/27] changed search functions to templates --- source/benchmark.cpp | 4 +- source/benchmark.h | 2 + source/search.cpp | 103 +++++++++++++++++++++++++++++++------------ source/search.h | 9 +++- source/uci.cpp | 2 +- 5 files changed, 87 insertions(+), 33 deletions(-) diff --git a/source/benchmark.cpp b/source/benchmark.cpp index fce2932..120024e 100644 --- a/source/benchmark.cpp +++ b/source/benchmark.cpp @@ -63,7 +63,7 @@ constexpr std::array fenPositions = { void RunBenchmark() { Board board; - int depth = 8; + int depth = BENCH_DEPTH; int ply = 0; benchStarted = true; searchStopped = false; @@ -80,7 +80,7 @@ void RunBenchmark() { Stopwatch sw; for (int i = 0; i < fenPositions.size(); i++) { board.SetByFen(fenPositions[i]); - PVS(board, depth, -inf, inf, ply); + SearchPosition(board, SearchParams()); } std::cout << nodes << " nodes " << int(nodes/sw.GetElapsedSec()) << " nps" << std::endl; diff --git a/source/benchmark.h b/source/benchmark.h index 69c9c98..b40f633 100644 --- a/source/benchmark.h +++ b/source/benchmark.h @@ -1,4 +1,6 @@ #pragma once #include +constexpr int BENCH_DEPTH = 8; + void RunBenchmark(); diff --git a/source/search.cpp b/source/search.cpp index e8ef5a1..c208b2a 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -5,6 +5,8 @@ #include #include "tt.h" #include "stopwatch.h" +#include "datagen.h" +#include "benchmark.h" namespace SEARCH { @@ -187,12 +189,21 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply) { return results; } -template +template SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { - if (!benchStarted && nodes % 1024 == 0) { - if (sw.GetElapsedMS() >= timeToSearch) { - StopSearch(); - return 0; + if constexpr (mode != bench) { + if (nodes % 1024 == 0) { + if constexpr (mode == normal) { + if (sw.GetElapsedMS() >= timeToSearch) { + StopSearch(); + return 0; + } + } else { + if (nodes >= DATAGEN::HARD_NODES) { + StopSearch(); + return 0; + } + } } } @@ -228,7 +239,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { // Always legal so we dont check it doingNullMove = true; - int score = -PVS(copy, depth - 3, -beta, -beta + 1, ply + 1).score; + int score = -PVS(copy, depth - 3, -beta, -beta + 1, ply + 1).score; doingNullMove = false; if (searchStopped) return 0; @@ -275,23 +286,23 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { // First move (suspected PV node) if (!moveSeen) { // Full search - score = -PVS(copy, newDepth, -beta, -alpha, ply + 1).score; + score = -PVS(copy, newDepth, -beta, -alpha, ply + 1).score; } else if (reductions) { // Null-window search with reductions - score = -PVS(copy, newDepth - reductions, -alpha-1, -alpha, ply + 1).score; + score = -PVS(copy, newDepth - reductions, -alpha-1, -alpha, ply + 1).score; if (score > alpha) { // Null-window search now without the reduction - score = -PVS(copy, newDepth, -alpha-1, -alpha, ply + 1).score; + score = -PVS(copy, newDepth, -alpha-1, -alpha, ply + 1).score; } } else { // Null-window search - score = -PVS(copy, newDepth, -alpha-1, -alpha, ply + 1).score; + score = -PVS(copy, newDepth, -alpha-1, -alpha, ply + 1).score; } // Check if we need to do full window re-search if (moveSeen && score > alpha && score < beta) { - score = -PVS(copy, newDepth, -beta, -alpha, ply + 1).score; + score = -PVS(copy, newDepth, -beta, -alpha, ply + 1).score; } if (searchStopped) return 0; @@ -347,6 +358,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { } // Iterative deepening +template static SearchResults ID(Board &board, SearchParams params) { int fullTime = board.sideToMove ? params.btime : params.wtime; int inc = board.sideToMove ? params.binc : params.winc; @@ -354,6 +366,12 @@ static SearchResults ID(Board &board, SearchParams params) { SearchResults safeResults; safeResults.score = -inf; + int toDepth = MAX_DEPTH; + + if constexpr (mode == bench) { + toDepth = BENCH_DEPTH; + } + int alpha = -inf; int beta = inf; @@ -362,13 +380,16 @@ static SearchResults ID(Board &board, SearchParams params) { int ply = 0; int elapsed = 0; - sw.Restart(); - for (int depth = 1; depth <= MAX_DEPTH; depth++) { + if constexpr (mode == normal) { + sw.Restart(); + } + + for (int depth = 1; depth <= toDepth; depth++) { timeToSearch = std::max((fullTime / 20) + (inc / 2), 4); int softTime = timeToSearch * 0.65; - SearchResults currentResults = PVS(board, depth, alpha, beta, ply); + SearchResults currentResults = PVS(board, depth, alpha, beta, ply); elapsed = sw.GetElapsedMS(); // If we fell outside the window, try again with full width @@ -396,19 +417,26 @@ static SearchResults ID(Board &board, SearchParams params) { safeResults = currentResults; } - std::cout << "info "; - std::cout << "depth " << depth; - std::cout << " time " << elapsed; - std::cout << " score cp " << safeResults.score; - std::cout << " nodes " << nodes << " nps " << int(nodes/sw.GetElapsedSec()); - std::cout << " hashfull " << TT.GetUsedPercentage(); - std::cout << " pv "; - pvLine.Print(0); - std::cout << std::endl; - - if (sw.GetElapsedMS() >= softTime) { - StopSearch(); - break; + if constexpr (mode == normal) { + std::cout << "info "; + std::cout << "depth " << depth; + std::cout << " time " << elapsed; + std::cout << " score cp " << safeResults.score; + std::cout << " nodes " << nodes << " nps " << int(nodes/sw.GetElapsedSec()); + std::cout << " hashfull " << TT.GetUsedPercentage(); + std::cout << " pv "; + pvLine.Print(0); + std::cout << std::endl; + + if (sw.GetElapsedMS() >= softTime) { + StopSearch(); + break; + } + } else if constexpr (mode == datagen) { + if (nodes >= DATAGEN::SOFT_NODES) { + StopSearch(); + break; + } } } } @@ -416,12 +444,17 @@ static SearchResults ID(Board &board, SearchParams params) { return safeResults; } +template void SearchPosition(Board &board, SearchParams params) { searchStopped = false; - nodes = 0; + if constexpr (mode != bench) { + nodes = 0; + } pvLine.Clear(); - SearchResults results = ID(board, params); + SearchResults results = ID(board, params); + + if constexpr (mode != normal) return; std::cout << "bestmove "; results.bestMove.PrintMove(); @@ -431,4 +464,16 @@ void SearchPosition(Board &board, SearchParams params) { void StopSearch() { searchStopped = true; } + +template SearchResults PVS(Board, int, int, int, int); +template SearchResults PVS(Board, int, int, int, int); +template SearchResults PVS(Board, int, int, int, int); +template SearchResults PVS(Board, int, int, int, int); +template SearchResults PVS(Board, int, int, int, int); +template SearchResults PVS(Board, int, int, int, int); + +template void SearchPosition(Board &board, SearchParams params); +template void SearchPosition(Board &board, SearchParams params); +template void SearchPosition(Board &board, SearchParams params); + } diff --git a/source/search.h b/source/search.h index f286246..a4fea4a 100644 --- a/source/search.h +++ b/source/search.h @@ -7,6 +7,12 @@ namespace SEARCH { +enum searchMode { + normal, + bench, + datagen +}; + constexpr int MATE_SCORE = -99000; constexpr int MAX_DEPTH = 64; constexpr int MAX_HISTORY = 16384; @@ -101,9 +107,10 @@ public: } }; -template +template SearchResults PVS(Board board, int depth, int alpha, int beta, int ply); +template void SearchPosition(Board &board, SearchParams params); void StopSearch(); diff --git a/source/uci.cpp b/source/uci.cpp index d1ae052..948fbd6 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -82,7 +82,7 @@ static void ParseGo(Board &board, std::string &command) { params.btime = ReadParam("movetime", command); } - auto worker = std::thread(SEARCH::SearchPosition, std::ref(board), params); + auto worker = std::thread(SEARCH::SearchPosition, std::ref(board), params); worker.detach(); } -- 2.47.3 From d703ea6010773ee1debb7bea3c5b464f167c985e Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 31 Mar 2025 03:22:32 +0200 Subject: [PATCH 05/27] fixed marlinformat pieces --- source/datagen.h | 2 +- source/utils.cpp | 4 ++-- source/utils.h | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/source/datagen.h b/source/datagen.h index c9093bb..0e3568a 100644 --- a/source/datagen.h +++ b/source/datagen.h @@ -10,7 +10,7 @@ constexpr int RAND_MOVES = 8; struct MarlinFormat { uint64_t occupancy; // 8 bytes: Bitboard representing all occupied squares (includes all pieces) - std::array pieces; // 32 bytes: 64 squares stored in 4-bit format (2 squares per byte) + std::array pieces; // 16 bytes: 64 squares stored in 4-bit format (2 squares per byte) uint8_t stmEPSquare; // 1 byte: STM (side to move) in bit 7, EP square in bits 0-5 uint8_t halfmoveClock; // 1 byte: Halfmove counter for the 50-move rule (incremented after each move) uint16_t fullmoveNumber; // 2 bytes: Full move counter, starting at 1 (increments after Black's move) diff --git a/source/utils.cpp b/source/utils.cpp index 44e118b..0dc7d10 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -147,10 +147,10 @@ Move parseMove(Board &board, std::string_view str) { return Move(from, to, flag); } -std::array CompressPieces(Board &board) { +std::array CompressPieces(Board &board) { const uint8_t UNMOVED_ROOK = 0x10; // Special value for unmoved rooks - std::array compressed; + std::array compressed; for (int square = a1; square <= h8; square++) { int pieceCode = 0; diff --git a/source/utils.h b/source/utils.h index 3a8c575..fbd09b1 100644 --- a/source/utils.h +++ b/source/utils.h @@ -30,6 +30,6 @@ int parseSquare(std::string_view str); Move parseMove(Board &board, std::string_view str); -std::array CompressPieces(Board &board); +std::array CompressPieces(Board &board); } -- 2.47.3 From ebdf295eecc3a3663d3d93180945c8e27286c158 Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 31 Mar 2025 05:02:44 +0200 Subject: [PATCH 06/27] added some selfplay logic --- source/datagen.cpp | 46 +++++++++++++++++++++++++++++++++++++++++++++- source/datagen.h | 2 +- source/main.cpp | 7 +++++++ source/search.cpp | 16 +++++++++++----- source/search.h | 4 +++- 5 files changed, 67 insertions(+), 8 deletions(-) diff --git a/source/datagen.cpp b/source/datagen.cpp index 8f46e0e..ca32451 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -1,9 +1,14 @@ #include +#include #include "datagen.h" #include "movegen.h" +#include "evaluate.h" +#include "search.h" +#include "movegen.h" namespace DATAGEN { +// TODO: Causes segment fault static void PlayRandMoves(Board &board) { std::random_device dev; std::mt19937 rng(dev()); @@ -17,7 +22,46 @@ static void PlayRandMoves(Board &board) { } } -void Run() { +static bool IsGameOver(Board &board) { + Board copy = board; + // Checkmate / stalemate + int moveCounter = 0; + for (int i = 0; board.currentMoveIndex; i++) { + bool isLegal = copy.MakeMove(board.moveList[i]); + if (!isLegal) continue; + positionIndex--; + moveCounter++; + } + if (moveCounter == 0) return true; + + // Draws + return SEARCH::IsDraw(board); +} + +void Run(int games) { + for (int i = 0; i < games; i++) { + Board board; + MarlinFormat position; + uint8_t wdl = 1; + positionIndex = 0; + std::memset(SEARCH::killerMoves, 0, sizeof(SEARCH::killerMoves)); + SEARCH::history.Clear(); + + //PlayRandMoves(board); + + SEARCH::SearchResults safeResults; + + // TODO: Game loop + + if (std::abs(safeResults.score) >= SEARCH::inf - SEARCH::MAX_DEPTH) { + wdl = board.sideToMove ? 2 : 0; + } + + position.packFrom(board, HCE::Evaluate(board), wdl); + board.PrintBoard(); + std::cout << "Eval: " << position.eval << std::endl; + std::cout << "WDL: " << int(position.wdl) << std::endl; + } } } \ No newline at end of file diff --git a/source/datagen.h b/source/datagen.h index 0e3568a..03a5b18 100644 --- a/source/datagen.h +++ b/source/datagen.h @@ -50,5 +50,5 @@ struct Game { } }; -void Run(); +void Run(int games); } \ No newline at end of file diff --git a/source/main.cpp b/source/main.cpp index 000c9d5..818c263 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -4,6 +4,7 @@ #include "utils.h" #include "benchmark.h" #include "search.h" +#include "datagen.h" int main(int argc, char* argv[]) { MOVEGEN::initLeaperAttacks(); @@ -16,6 +17,12 @@ int main(int argc, char* argv[]) { if (argc > 1) { if (std::string(argv[1]) == "bench") { RunBenchmark(); + } else if (std::string(argv[1]) == "datagen") { + int count = 1; + if (argc > 2) { + count = std::stoi(argv[2]); + } + DATAGEN::Run(count); } } else { UCILoop(board); diff --git a/source/search.cpp b/source/search.cpp index c208b2a..d65ffdb 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -124,6 +124,10 @@ static bool IsInsuffMat(Board &board) { && !(board.pieces[Pawn] | board.pieces[Queen] | board.pieces[Rook])); } +bool IsDraw(Board &board) { + return IsFifty || IsInsuffMat || IsThreefold; +} + static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int ply) { int reduction = 0; @@ -445,7 +449,7 @@ static SearchResults ID(Board &board, SearchParams params) { } template -void SearchPosition(Board &board, SearchParams params) { +SearchResults SearchPosition(Board &board, SearchParams params) { searchStopped = false; if constexpr (mode != bench) { nodes = 0; @@ -454,11 +458,13 @@ void SearchPosition(Board &board, SearchParams params) { SearchResults results = ID(board, params); - if constexpr (mode != normal) return; + if constexpr (mode != normal) return results; std::cout << "bestmove "; results.bestMove.PrintMove(); std::cout << std::endl; + + return results; } void StopSearch() { @@ -472,8 +478,8 @@ template SearchResults PVS(Board, int, int, int, int) template SearchResults PVS(Board, int, int, int, int); template SearchResults PVS(Board, int, int, int, int); -template void SearchPosition(Board &board, SearchParams params); -template void SearchPosition(Board &board, SearchParams params); -template void SearchPosition(Board &board, SearchParams params); +template SearchResults SearchPosition(Board &board, SearchParams params); +template SearchResults SearchPosition(Board &board, SearchParams params); +template SearchResults SearchPosition(Board &board, SearchParams params); } diff --git a/source/search.h b/source/search.h index a4fea4a..dd0160b 100644 --- a/source/search.h +++ b/source/search.h @@ -111,9 +111,11 @@ template SearchResults PVS(Board board, int depth, int alpha, int beta, int ply); template -void SearchPosition(Board &board, SearchParams params); +SearchResults SearchPosition(Board &board, SearchParams params); void StopSearch(); +bool IsDraw(Board &board); + constexpr int moveScoreTable[6][6] = { 105, 205, 305, 405, 505, 605, 104, 204, 304, 404, 504, 604, -- 2.47.3 From ee59c8a4559e8844a3ea0b3ce7afc3f56b687569 Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 31 Mar 2025 05:06:28 +0200 Subject: [PATCH 07/27] fixed PlayRandMoves() --- source/datagen.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/source/datagen.cpp b/source/datagen.cpp index ca32451..56bce11 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -8,14 +8,13 @@ namespace DATAGEN { -// TODO: Causes segment fault static void PlayRandMoves(Board &board) { std::random_device dev; std::mt19937 rng(dev()); for (int count = 0; count < RAND_MOVES; count++) { MOVEGEN::GenerateMoves(board); - std::uniform_int_distribution dist(0, board.currentMoveIndex); + std::uniform_int_distribution dist(0, board.currentMoveIndex - 1); bool isLegal = board.MakeMove(board.moveList[dist(rng)]); if (!isLegal) count--; @@ -48,7 +47,7 @@ void Run(int games) { std::memset(SEARCH::killerMoves, 0, sizeof(SEARCH::killerMoves)); SEARCH::history.Clear(); - //PlayRandMoves(board); + PlayRandMoves(board); SEARCH::SearchResults safeResults; -- 2.47.3 From 43c7e0caaf4c36ea93a2fdb985dcf4fd73d45153 Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 31 Mar 2025 05:24:00 +0200 Subject: [PATCH 08/27] fixed some horrible syntax errors --- source/datagen.cpp | 24 +++++++++--------------- source/search.cpp | 2 +- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/source/datagen.cpp b/source/datagen.cpp index 56bce11..9af39ce 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -22,20 +22,7 @@ static void PlayRandMoves(Board &board) { } static bool IsGameOver(Board &board) { - Board copy = board; - // Checkmate / stalemate - int moveCounter = 0; - for (int i = 0; board.currentMoveIndex; i++) { - bool isLegal = copy.MakeMove(board.moveList[i]); - if (!isLegal) continue; - positionIndex--; - moveCounter++; - } - - if (moveCounter == 0) return true; - - // Draws - return SEARCH::IsDraw(board); + // TODO } void Run(int games) { @@ -51,7 +38,14 @@ void Run(int games) { SEARCH::SearchResults safeResults; - // TODO: Game loop + // while (!IsGameOver(board)) { + // SEARCH::SearchResults results = SEARCH::SearchPosition(board, SearchParams()); + + // if (!results.bestMove) break; + // safeResults = results; + + // board.MakeMove(results.bestMove); + // } if (std::abs(safeResults.score) >= SEARCH::inf - SEARCH::MAX_DEPTH) { wdl = board.sideToMove ? 2 : 0; diff --git a/source/search.cpp b/source/search.cpp index d65ffdb..b3542b8 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -125,7 +125,7 @@ static bool IsInsuffMat(Board &board) { } bool IsDraw(Board &board) { - return IsFifty || IsInsuffMat || IsThreefold; + return IsFifty(board) || IsInsuffMat(board) || IsThreefold(board); } static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int ply) { -- 2.47.3 From 44af8510815e46a960eed7017cf9642aba9834a6 Mon Sep 17 00:00:00 2001 From: rektdie Date: Tue, 1 Apr 2025 03:36:07 +0200 Subject: [PATCH 09/27] resetting board correctly on non legal makemove | bench 1857933 --- source/board.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/source/board.cpp b/source/board.cpp index 6b1736f..dbedc09 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -222,6 +222,7 @@ void Board::Promote(int square, int pieceType, int color, bool isCapture) { } bool Board::MakeMove(Move move) { + Board save = *this; // Null Move if (!move) { int newEpTarget = noEPTarget; @@ -347,7 +348,10 @@ bool Board::MakeMove(Move move) { MOVEGEN::GenThreatMaps(*this); sideToMove = !sideToMove; - if (InCheck()) return false; + if (InCheck()) { + *this = save; + return false; + } sideToMove = !sideToMove; if (attackerColor == Black) fullMoves++; -- 2.47.3 From 250347dbab92e9760c915935258c7ff66c68d1f6 Mon Sep 17 00:00:00 2001 From: rektdie Date: Tue, 1 Apr 2025 04:55:30 +0200 Subject: [PATCH 10/27] added pgntouci script | bench 1857933 --- Scripts/game.pgn | 90 +++++++++++++++++++++++++++++++++++++++++++++ Scripts/pgntouci.py | 19 ++++++++++ 2 files changed, 109 insertions(+) create mode 100644 Scripts/game.pgn create mode 100644 Scripts/pgntouci.py diff --git a/Scripts/game.pgn b/Scripts/game.pgn new file mode 100644 index 0000000..c3934b1 --- /dev/null +++ b/Scripts/game.pgn @@ -0,0 +1,90 @@ +[Event "?"] +[Site "?"] +[Date "2025.04.01"] +[Round "1"] +[White "Eleanor-dev"] +[Black "Eleanor-base"] +[Result "0-1"] +[FEN "rn1qk2r/pp1b1ppp/1b1p1n2/2p5/Q2P4/2P1PN2/PP3PPP/RNB1KB1R w KQkq - 0 1"] +[GameDuration "00:00:06"] +[GameEndTime "2025-04-01T01:51:57.150 UTC"] +[GameStartTime "2025-04-01T01:51:50.577 UTC"] +[PlyCount "154"] +[SetUp "1"] +[Termination "abandoned"] +[TimeControl "1.98+0.02"] + +1. Qc2 {+0.56 9/0 110 119255} O-O {-0.44 9/0 97 184822} +2. dxc5 {+0.54 10/0 80 151089} Bxc5 {-0.51 10/0 94 170016} +3. Bd3 {+0.54 9/0 107 45057} Nc6 {-0.52 10/0 77 150284} +4. O-O {+0.48 9/0 98 68958} Rc8 {-0.50 9/0 99 89408} +5. Nbd2 {+0.58 9/0 94 89122} d5 {-0.57 9/0 95 84311} 6. a3 {+0.66 9/0 86 185406} +h6 {-0.69 8/0 88 187437} 7. b4 {+0.82 9/0 64 135786} Bd6 {-0.80 8/0 88 82460} +8. Bb2 {+0.67 8/0 85 50375} Qc7 {-0.67 7/0 85 2526} 9. c4 {+1.00 9/0 60 128299} +Ne5 {-0.99 9/0 54 113747} 10. Nxe5 {+0.97 10/0 79 109831} +Bxe5 {-1.02 10/0 72 161422} 11. Bxe5 {+0.99 9/0 90 30236} +Qxe5 {-1.02 10/0 57 126009} 12. c5 {+0.99 8/0 48 110594} +Qc7 {-1.00 7/0 75 48053} 13. Rac1 {+0.98 7/0 69 159928} a6 {-0.98 8/0 55 121892} +14. Rcd1 {+0.99 8/0 69 85541} Bb5 {-0.98 9/0 57 125809} +15. Bxb5 {+1.02 9/0 45 96916} axb5 {-1.02 10/0 45 102206} +16. Qf5 {+1.02 9/0 65 90099} b6 {-0.97 9/0 44 69960} +17. Nb3 {+0.97 9/0 63 19401} bxc5 {-0.93 9/0 66 59180} +18. Nxc5 {+0.96 9/0 72 60870} Rfe8 {-0.92 9/0 64 75468} +19. Rfe1 {+0.98 9/0 42 99528} Rcd8 {-0.90 9/0 52 122983} +20. Qd3 {+0.87 9/0 45 108212} Ng4 {-0.69 10/0 60 71540} +21. g3 {+0.81 9/0 40 84016} Ne5 {-0.68 11/0 57 123606} +22. Qf5 {+0.71 9/0 55 31589} Qa7 {-0.68 9/0 56 5288} +23. Ra1 {+0.56 8/0 53 23556} Qe7 {-0.70 8/0 55 29567} +24. Red1 {+0.65 9/0 52 50338} Qg5 {-0.64 10/0 54 108269} +25. Qxg5 {+0.73 11/0 33 67482} hxg5 {-0.74 10/0 36 83485} +26. h3 {+0.73 9/0 49 9083} Nc4 {-0.75 9/0 50 68270} 27. Rd4 {+0.88 9/0 48 42588} +g6 {-0.80 9/0 37 90004} 28. Kg2 {+0.97 9/0 46 57129} Kg7 {-0.80 8/0 48 38609} +29. a4 {+1.28 9/0 45 43802} bxa4 {-1.31 9/0 38 95103} +30. Rxa4 {+1.36 9/0 44 34076} Rb8 {-1.36 8/0 46 59882} +31. e4 {+1.38 10/0 29 67046} Nb6 {-1.44 10/0 44 50322} +32. Ra7 {+1.48 10/0 42 50043} Re5 {-1.61 10/0 43 48881} +33. f4 {+2.08 11/0 28 61368} gxf4 {-2.02 11/0 32 68798} +34. gxf4 {+1.54 11/0 37 86167} Rh5 {-2.02 9/0 42 7187} +35. e5 {+1.55 10/0 40 42189} Nc4 {-1.96 8/0 40 57969} +36. e6 {+1.66 10/0 30 58501} Rxb4 {-1.53 8/0 35 81508} +37. Rxf7+ {+1.66 8/0 38 5569} Kg8 {-1.58 10/0 35 70226} +38. Kg3 {+1.56 9/0 38 26186} Rb2 {-1.43 9/0 38 39009} +39. Rb7 {+1.52 10/0 37 43834} Rxb7 {-1.49 9/0 31 63390} +40. Nxb7 {+1.49 10/0 36 49837} Kf8 {-0.93 10/0 26 63957} +41. Rd1 {+0.85 10/0 31 68559} Ke7 {-0.29 11/0 35 77141} +42. Re1 {+0.28 10/0 31 51185} d4 {-0.07 11/0 35 48860} +43. Re4 {+0.23 10/0 34 41610} Ne3 {-0.28 11/0 35 34113} +44. Kf2 {+0.25 10/0 33 41279} Rd5 {+1.20 10/0 28 66275} +45. Re5 {-1.24 10/0 23 55865} Rxe5 {+1.19 13/0 26 66334} +46. fxe5 {-1.11 12/0 25 61781} Kxe6 {+1.24 12/0 33 40940} +47. Nc5+ {-1.15 12/0 31 85420} Kxe5 {+1.15 12/0 33 38536} +48. Kf3 {-1.15 11/0 32 8992} Nd5 {+1.15 12/0 32 35729} +49. Nd3+ {-1.24 12/0 26 73374} Kf5 {+1.22 13/0 31 27642} +50. h4 {-1.36 12/0 30 84016} Nb6 {+1.66 13/0 31 27667} +51. Nc5 {-1.52 13/0 23 63749} Nc4 {+1.66 12/0 30 44643} +52. Ke2 {-1.32 12/0 20 56047} Ke5 {+1.72 11/0 22 60715} +53. Nd3+ {-1.58 12/0 21 62779} Kd5 {+1.81 13/0 29 78788} +54. Nf4+ {-1.58 12/0 30 32007} Ke4 {+1.86 12/0 29 32961} +55. Nd3 {-1.72 12/0 29 32480} Ne5 {+1.84 12/0 29 28961} +56. Nc5+ {-1.71 12/0 29 26321} Kd5 {+1.87 12/0 28 4733} +57. Nb7 {-1.96 10/0 29 33195} d3+ {+2.02 12/0 28 44864} +58. Ke3 {-1.96 13/0 25 64793} Kc4 {+1.96 12/0 27 6462} +59. Nd6+ {-1.96 11/0 28 1843} Kc3 {+2.42 12/0 27 1425} +60. Nb5+ {-2.15 12/0 19 45605} Kc2 {+2.93 12/0 23 49349} +61. Nd4+ {-2.15 12/0 24 61836} Kc3 {+2.93 13/0 25 62111} +62. Nb5+ {-2.20 13/0 25 64766} Kb4 {+2.93 12/0 26 1010} +63. Na7 {-2.54 12/0 17 44236} Kc4 {+2.97 11/0 26 23531} +64. Nc8 {-2.57 12/0 27 32772} Kc3 {+3.35 12/0 19 45174} +65. Nd6 {-3.49 12/0 30 36269} Nc4+ {+3.59 11/0 26 8828} +66. Nxc4 {-2.09 15/0 16 44937} Kxc4 {+2.18 15/0 21 57452} +67. Kd2 {-2.09 17/0 27 101248} Kd4 {+2.11 17/0 18 57825} +68. Kd1 {-1.74 17/0 20 58596} Kd5 {+2.08 18/0 20 68999} +69. Kd2 {-1.66 17/0 26 55701} Ke5 {+1.61 18/0 22 90879} +70. Kxd3 {-2.39 17/0 22 92503} Kf4 {+1.15 17/0 18 69472} +71. h5 {-1.26 19/0 21 92160} gxh5 {+1.14 19/0 20 73476} +72. Ke2 {-1.26 24/0 18 92487} Kg3 {+1.14 22/0 18 70462} +73. Kf1 {0.00 30/0 16 65084} Kh2 {+0.35 22/0 20 84458} +74. Kf2 {0.00 37/0 17 41974} Kh3 {0.00 26/0 16 63213} +75. Kf3 {0.00 45/0 23 103454} Kh2 {0.00 34/0 19 87233} +76. Kf2 {0.00 47/0 26 37602} h4 {0.00 41/0 16 78046} +77. Kf1 {0.00 60/0 16 59832} Kh3 {0.00 61/0 17 98252, White disconnects} 0-1 \ No newline at end of file diff --git a/Scripts/pgntouci.py b/Scripts/pgntouci.py new file mode 100644 index 0000000..76a3223 --- /dev/null +++ b/Scripts/pgntouci.py @@ -0,0 +1,19 @@ +########################################## +# convert pgn to uci +########################################## +import chess +import chess.pgn + +pgn = open("game.pgn") + +game = chess.pgn.read_game(pgn) + +board = game.board() + +moves = "position fen " + board.fen() + " moves" + +for move in game.mainline_moves(): + board.push(move) + moves += " " + move.uci() + +print(moves) \ No newline at end of file -- 2.47.3 From a6459dda00d3257c629d4f72fa268b66cc3853c0 Mon Sep 17 00:00:00 2001 From: rektdie Date: Tue, 1 Apr 2025 05:41:21 +0200 Subject: [PATCH 11/27] added go nodes | bench 1857933 --- source/search.cpp | 47 +++++++++++++++++++++++++++++++++-------------- source/search.h | 3 ++- source/uci.cpp | 12 +++++++++--- source/uci.h | 1 + 4 files changed, 45 insertions(+), 18 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index b3542b8..4f4397f 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -11,6 +11,7 @@ namespace SEARCH { U64 nodes = 0; +U64 nodesToGo = 0; bool benchStarted = false; static inline int timeToSearch = 0; @@ -139,14 +140,22 @@ static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int return reduction; } +template static SearchResults Quiescence(Board board, int alpha, int beta, int ply) { - if (!benchStarted && nodes % 1024 == 0) { - if (sw.GetElapsedMS() >= timeToSearch) { + if constexpr (mode != nodesMode) { + if (!benchStarted && nodes % 1024 == 0) { + if (sw.GetElapsedMS() >= timeToSearch) { + StopSearch(); + return 0; + } + } + } else { + if (nodes >= nodesToGo) { StopSearch(); return 0; } } - + int bestScore = HCE::Evaluate(board); TTEntry *entry = TT.GetRawEntry(board.hashKey); @@ -173,7 +182,7 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply) { Board copy = board; int isLegal = copy.MakeMove(board.moveList[i]); if (!isLegal) continue; - int score = -Quiescence(copy, -beta, -alpha, ply + 1).score; + int score = -Quiescence(copy, -beta, -alpha, ply + 1).score; positionIndex--; if (score >= beta) { @@ -195,20 +204,25 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply) { template SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { - if constexpr (mode != bench) { + if constexpr (mode != bench || mode != nodesMode) { if (nodes % 1024 == 0) { if constexpr (mode == normal) { if (sw.GetElapsedMS() >= timeToSearch) { StopSearch(); return 0; } - } else { + } else if constexpr (mode == datagen) { if (nodes >= DATAGEN::HARD_NODES) { StopSearch(); return 0; } } } + } else if constexpr (mode == nodesMode) { + if (nodes >= nodesToGo) { + StopSearch(); + return 0; + } } pvLine.SetLength(ply); @@ -223,7 +237,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { } } - if (depth <= 0) return Quiescence(board, alpha, beta, ply); + if (depth <= 0) return Quiescence(board, alpha, beta, ply); const int staticEval = HCE::Evaluate(board); @@ -385,9 +399,7 @@ static SearchResults ID(Board &board, SearchParams params) { int elapsed = 0; - if constexpr (mode == normal) { - sw.Restart(); - } + sw.Restart(); for (int depth = 1; depth <= toDepth; depth++) { timeToSearch = std::max((fullTime / 20) + (inc / 2), 4); @@ -421,7 +433,7 @@ static SearchResults ID(Board &board, SearchParams params) { safeResults = currentResults; } - if constexpr (mode == normal) { + if constexpr (mode == normal || mode == nodesMode) { std::cout << "info "; std::cout << "depth " << depth; std::cout << " time " << elapsed; @@ -432,9 +444,11 @@ static SearchResults ID(Board &board, SearchParams params) { pvLine.Print(0); std::cout << std::endl; - if (sw.GetElapsedMS() >= softTime) { - StopSearch(); - break; + if constexpr (mode == normal) { + if (sw.GetElapsedMS() >= softTime) { + StopSearch(); + break; + } } } else if constexpr (mode == datagen) { if (nodes >= DATAGEN::SOFT_NODES) { @@ -453,6 +467,10 @@ SearchResults SearchPosition(Board &board, SearchParams params) { searchStopped = false; if constexpr (mode != bench) { nodes = 0; + + if constexpr (mode == nodesMode) { + nodesToGo = params.nodes; + } } pvLine.Clear(); @@ -481,5 +499,6 @@ template SearchResults PVS(Board, int, int, int, int template SearchResults SearchPosition(Board &board, SearchParams params); template SearchResults SearchPosition(Board &board, SearchParams params); template SearchResults SearchPosition(Board &board, SearchParams params); +template SearchResults SearchPosition(Board &board, SearchParams params); } diff --git a/source/search.h b/source/search.h index dd0160b..fa8855c 100644 --- a/source/search.h +++ b/source/search.h @@ -10,7 +10,8 @@ namespace SEARCH { enum searchMode { normal, bench, - datagen + datagen, + nodesMode }; constexpr int MATE_SCORE = -99000; diff --git a/source/uci.cpp b/source/uci.cpp index 948fbd6..53d8121 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -76,14 +76,20 @@ static void ParseGo(Board &board, std::string &command) { params.winc = ReadParam("winc", command); params.binc = ReadParam("binc", command); params.movesToGo = ReadParam("movestogo", command); + params.nodes = ReadParam("nodes", command); if (command.find("movetime") != std::string::npos) { params.wtime = ReadParam("movetime", command); params.btime = ReadParam("movetime", command); } - - auto worker = std::thread(SEARCH::SearchPosition, std::ref(board), params); - worker.detach(); + + if (params.nodes) { + auto worker = std::thread(SEARCH::SearchPosition, std::ref(board), params); + worker.detach(); + } else { + auto worker = std::thread(SEARCH::SearchPosition, std::ref(board), params); + worker.detach(); + } } static void SetOption(std::string &command) { diff --git a/source/uci.h b/source/uci.h index 11a231e..1f2553d 100644 --- a/source/uci.h +++ b/source/uci.h @@ -9,6 +9,7 @@ public: int winc = 0; int binc = 0; int movesToGo = 0; + int nodes = 0; SearchParams(){} }; -- 2.47.3 From 656d71dd5d26e2825b21304068c3cfebe76e3d28 Mon Sep 17 00:00:00 2001 From: rektdie Date: Tue, 1 Apr 2025 05:45:42 +0200 Subject: [PATCH 12/27] fixed nodestogo check --- source/search.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index 4f4397f..a42e8a2 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -150,7 +150,7 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply) { } } } else { - if (nodes >= nodesToGo) { + if (nodes > nodesToGo) { StopSearch(); return 0; } @@ -219,7 +219,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { } } } else if constexpr (mode == nodesMode) { - if (nodes >= nodesToGo) { + if (nodes > nodesToGo) { StopSearch(); return 0; } -- 2.47.3 From 3b363551362777e963b3f64fc972101a6469e11f Mon Sep 17 00:00:00 2001 From: rektdie Date: Tue, 1 Apr 2025 18:06:13 +0200 Subject: [PATCH 13/27] increased max ply cause of a bug | bench 1857933 --- Scripts/game.pgn | 115 ++++++++++++++--------------------------------- source/search.h | 5 ++- 2 files changed, 37 insertions(+), 83 deletions(-) diff --git a/Scripts/game.pgn b/Scripts/game.pgn index c3934b1..6ae6017 100644 --- a/Scripts/game.pgn +++ b/Scripts/game.pgn @@ -4,87 +4,40 @@ [Round "1"] [White "Eleanor-dev"] [Black "Eleanor-base"] -[Result "0-1"] -[FEN "rn1qk2r/pp1b1ppp/1b1p1n2/2p5/Q2P4/2P1PN2/PP3PPP/RNB1KB1R w KQkq - 0 1"] -[GameDuration "00:00:06"] -[GameEndTime "2025-04-01T01:51:57.150 UTC"] -[GameStartTime "2025-04-01T01:51:50.577 UTC"] -[PlyCount "154"] +[Result "1-0"] +[FEN "2r3k1/2B1Rpp1/p1P5/5b1p/PP6/5P2/6P1/6K1 b - - 0 1"] +[GameDuration "00:00:03"] +[GameEndTime "2025-04-01T04:05:33.741 UTC"] +[GameStartTime "2025-04-01T04:05:30.048 UTC"] +[PlyCount "54"] [SetUp "1"] [Termination "abandoned"] -[TimeControl "1.98+0.02"] +[TimeControl "2.029+0.02"] -1. Qc2 {+0.56 9/0 110 119255} O-O {-0.44 9/0 97 184822} -2. dxc5 {+0.54 10/0 80 151089} Bxc5 {-0.51 10/0 94 170016} -3. Bd3 {+0.54 9/0 107 45057} Nc6 {-0.52 10/0 77 150284} -4. O-O {+0.48 9/0 98 68958} Rc8 {-0.50 9/0 99 89408} -5. Nbd2 {+0.58 9/0 94 89122} d5 {-0.57 9/0 95 84311} 6. a3 {+0.66 9/0 86 185406} -h6 {-0.69 8/0 88 187437} 7. b4 {+0.82 9/0 64 135786} Bd6 {-0.80 8/0 88 82460} -8. Bb2 {+0.67 8/0 85 50375} Qc7 {-0.67 7/0 85 2526} 9. c4 {+1.00 9/0 60 128299} -Ne5 {-0.99 9/0 54 113747} 10. Nxe5 {+0.97 10/0 79 109831} -Bxe5 {-1.02 10/0 72 161422} 11. Bxe5 {+0.99 9/0 90 30236} -Qxe5 {-1.02 10/0 57 126009} 12. c5 {+0.99 8/0 48 110594} -Qc7 {-1.00 7/0 75 48053} 13. Rac1 {+0.98 7/0 69 159928} a6 {-0.98 8/0 55 121892} -14. Rcd1 {+0.99 8/0 69 85541} Bb5 {-0.98 9/0 57 125809} -15. Bxb5 {+1.02 9/0 45 96916} axb5 {-1.02 10/0 45 102206} -16. Qf5 {+1.02 9/0 65 90099} b6 {-0.97 9/0 44 69960} -17. Nb3 {+0.97 9/0 63 19401} bxc5 {-0.93 9/0 66 59180} -18. Nxc5 {+0.96 9/0 72 60870} Rfe8 {-0.92 9/0 64 75468} -19. Rfe1 {+0.98 9/0 42 99528} Rcd8 {-0.90 9/0 52 122983} -20. Qd3 {+0.87 9/0 45 108212} Ng4 {-0.69 10/0 60 71540} -21. g3 {+0.81 9/0 40 84016} Ne5 {-0.68 11/0 57 123606} -22. Qf5 {+0.71 9/0 55 31589} Qa7 {-0.68 9/0 56 5288} -23. Ra1 {+0.56 8/0 53 23556} Qe7 {-0.70 8/0 55 29567} -24. Red1 {+0.65 9/0 52 50338} Qg5 {-0.64 10/0 54 108269} -25. Qxg5 {+0.73 11/0 33 67482} hxg5 {-0.74 10/0 36 83485} -26. h3 {+0.73 9/0 49 9083} Nc4 {-0.75 9/0 50 68270} 27. Rd4 {+0.88 9/0 48 42588} -g6 {-0.80 9/0 37 90004} 28. Kg2 {+0.97 9/0 46 57129} Kg7 {-0.80 8/0 48 38609} -29. a4 {+1.28 9/0 45 43802} bxa4 {-1.31 9/0 38 95103} -30. Rxa4 {+1.36 9/0 44 34076} Rb8 {-1.36 8/0 46 59882} -31. e4 {+1.38 10/0 29 67046} Nb6 {-1.44 10/0 44 50322} -32. Ra7 {+1.48 10/0 42 50043} Re5 {-1.61 10/0 43 48881} -33. f4 {+2.08 11/0 28 61368} gxf4 {-2.02 11/0 32 68798} -34. gxf4 {+1.54 11/0 37 86167} Rh5 {-2.02 9/0 42 7187} -35. e5 {+1.55 10/0 40 42189} Nc4 {-1.96 8/0 40 57969} -36. e6 {+1.66 10/0 30 58501} Rxb4 {-1.53 8/0 35 81508} -37. Rxf7+ {+1.66 8/0 38 5569} Kg8 {-1.58 10/0 35 70226} -38. Kg3 {+1.56 9/0 38 26186} Rb2 {-1.43 9/0 38 39009} -39. Rb7 {+1.52 10/0 37 43834} Rxb7 {-1.49 9/0 31 63390} -40. Nxb7 {+1.49 10/0 36 49837} Kf8 {-0.93 10/0 26 63957} -41. Rd1 {+0.85 10/0 31 68559} Ke7 {-0.29 11/0 35 77141} -42. Re1 {+0.28 10/0 31 51185} d4 {-0.07 11/0 35 48860} -43. Re4 {+0.23 10/0 34 41610} Ne3 {-0.28 11/0 35 34113} -44. Kf2 {+0.25 10/0 33 41279} Rd5 {+1.20 10/0 28 66275} -45. Re5 {-1.24 10/0 23 55865} Rxe5 {+1.19 13/0 26 66334} -46. fxe5 {-1.11 12/0 25 61781} Kxe6 {+1.24 12/0 33 40940} -47. Nc5+ {-1.15 12/0 31 85420} Kxe5 {+1.15 12/0 33 38536} -48. Kf3 {-1.15 11/0 32 8992} Nd5 {+1.15 12/0 32 35729} -49. Nd3+ {-1.24 12/0 26 73374} Kf5 {+1.22 13/0 31 27642} -50. h4 {-1.36 12/0 30 84016} Nb6 {+1.66 13/0 31 27667} -51. Nc5 {-1.52 13/0 23 63749} Nc4 {+1.66 12/0 30 44643} -52. Ke2 {-1.32 12/0 20 56047} Ke5 {+1.72 11/0 22 60715} -53. Nd3+ {-1.58 12/0 21 62779} Kd5 {+1.81 13/0 29 78788} -54. Nf4+ {-1.58 12/0 30 32007} Ke4 {+1.86 12/0 29 32961} -55. Nd3 {-1.72 12/0 29 32480} Ne5 {+1.84 12/0 29 28961} -56. Nc5+ {-1.71 12/0 29 26321} Kd5 {+1.87 12/0 28 4733} -57. Nb7 {-1.96 10/0 29 33195} d3+ {+2.02 12/0 28 44864} -58. Ke3 {-1.96 13/0 25 64793} Kc4 {+1.96 12/0 27 6462} -59. Nd6+ {-1.96 11/0 28 1843} Kc3 {+2.42 12/0 27 1425} -60. Nb5+ {-2.15 12/0 19 45605} Kc2 {+2.93 12/0 23 49349} -61. Nd4+ {-2.15 12/0 24 61836} Kc3 {+2.93 13/0 25 62111} -62. Nb5+ {-2.20 13/0 25 64766} Kb4 {+2.93 12/0 26 1010} -63. Na7 {-2.54 12/0 17 44236} Kc4 {+2.97 11/0 26 23531} -64. Nc8 {-2.57 12/0 27 32772} Kc3 {+3.35 12/0 19 45174} -65. Nd6 {-3.49 12/0 30 36269} Nc4+ {+3.59 11/0 26 8828} -66. Nxc4 {-2.09 15/0 16 44937} Kxc4 {+2.18 15/0 21 57452} -67. Kd2 {-2.09 17/0 27 101248} Kd4 {+2.11 17/0 18 57825} -68. Kd1 {-1.74 17/0 20 58596} Kd5 {+2.08 18/0 20 68999} -69. Kd2 {-1.66 17/0 26 55701} Ke5 {+1.61 18/0 22 90879} -70. Kxd3 {-2.39 17/0 22 92503} Kf4 {+1.15 17/0 18 69472} -71. h5 {-1.26 19/0 21 92160} gxh5 {+1.14 19/0 20 73476} -72. Ke2 {-1.26 24/0 18 92487} Kg3 {+1.14 22/0 18 70462} -73. Kf1 {0.00 30/0 16 65084} Kh2 {+0.35 22/0 20 84458} -74. Kf2 {0.00 37/0 17 41974} Kh3 {0.00 26/0 16 63213} -75. Kf3 {0.00 45/0 23 103454} Kh2 {0.00 34/0 19 87233} -76. Kf2 {0.00 47/0 26 37602} h4 {0.00 41/0 16 78046} -77. Kf1 {0.00 60/0 16 59832} Kh3 {0.00 61/0 17 98252, White disconnects} 0-1 \ No newline at end of file +1... Kf8 {-0.96 14/0 100 227191} 2. Re5 {+1.00 14/0 81 216018} +Rxc7 {-1.08 14/0 108 289288} 3. Rxf5 {+1.10 14/0 75 183753} +Rxc6 {-1.11 14/0 103 272990} 4. Rxh5 {+1.10 12/0 106 90738} +Ke7 {-1.11 12/0 99 23080} 5. Rh8 {+1.15 11/0 89 240527} +Rb6 {-1.03 10/0 95 90255} 6. Ra8 {+1.26 11/0 98 110373} +Kd6 {-1.30 11/0 82 198678} 7. a5 {+1.17 13/0 77 230009} +Rxb4 {-1.17 12/0 62 180719} 8. Rxa6+ {+1.11 12/0 92 156151} +Kc7 {-1.11 13/0 91 222192} 9. g4 {+1.04 11/0 88 129794} +Kb7 {-1.12 12/0 65 174745} 10. Rd6 {+1.02 12/0 85 89925} +Kc7 {-1.14 12/0 61 150972} 11. Rd5 {+1.02 12/0 66 169533} +g6 {-1.03 11/0 58 153259} 12. Kf2 {+1.04 11/0 79 76310} +Kc6 {-1.05 12/0 77 104110} 13. Rd8 {+1.03 13/0 76 130262} +Ra4 {-1.09 12/0 52 149145} 14. Rf8 {+1.07 13/0 76 103977} +Rxa5 {-1.09 13/0 67 173629} 15. Rxf7 {+1.08 12/0 71 6550} +g5 {-1.13 12/0 70 87384} 16. Rf5 {+1.17 13/0 52 156470} +Rxf5 {-1.70 15/0 67 119960} 17. gxf5 {+2.54 19/0 66 252419} +Kd6 {-2.43 18/0 51 177302} 18. Kg3 {+2.60 19/0 56 207073} +Ke5 {-2.16 22/0 57 234443} 19. Kg4 {+2.60 21/0 62 107886} +Kf6 {0.00 27/0 44 189069} 20. f4 {0.00 27/0 48 212931} +gxf4 {0.00 41/0 43 205787} 21. Kxf4 {0.00 42/0 38 152370} +Kf7 {0.00 48/0 41 174874} 22. Kg5 {0.00 45/0 44 184855} +Kg7 {0.00 50/0 42 148502} 23. Kh5 {0.00 49/0 44 181071} +Kf7 {0.00 55/0 51 204137} 24. Kh4 {0.00 55/0 56 65546} Kf6 {0.00 60/0 53 207131} +25. Kg4 {0.00 57/0 55 49048} Kf7 {0.00 60/0 36 153699} +26. Kg5 {0.00 60/0 52 35277} Kg7 {0.00 61/0 42 132702} +27. f6+ {0.00 60/0 50 126713} Kf7 {0.00 64/0 37 46021} +28. Kf5 {0.00 63/0 42 168465, Black disconnects} 1-0 \ No newline at end of file diff --git a/source/search.h b/source/search.h index fa8855c..38bd2d4 100644 --- a/source/search.h +++ b/source/search.h @@ -16,6 +16,7 @@ enum searchMode { constexpr int MATE_SCORE = -99000; constexpr int MAX_DEPTH = 64; +constexpr int MAX_PLY = 128; constexpr int MAX_HISTORY = 16384; extern U64 nodes; @@ -78,8 +79,8 @@ inline History history; class PVLine { private: - int length[MAX_DEPTH] = {}; - Move table[MAX_DEPTH][MAX_DEPTH] = {}; + int length[MAX_PLY] = {}; + Move table[MAX_PLY][MAX_PLY] = {}; public: void SetLength(int ply) { length[ply] = ply; -- 2.47.3 From f4eeec2b4eed40a81e3a4f8e8c43944695b1ebbc Mon Sep 17 00:00:00 2001 From: rektdie Date: Tue, 1 Apr 2025 18:24:43 +0200 Subject: [PATCH 14/27] forgot to init killers to max ply | bench 1857933 --- .gitignore | 3 +- Scripts/game.pgn | 72 +++++++++++++++++++++++++----------------------- source/search.h | 2 +- 3 files changed, 40 insertions(+), 37 deletions(-) diff --git a/.gitignore b/.gitignore index 52d905e..0a6146f 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ obj/ Eleanor # Other -.vscode \ No newline at end of file +.vscode +Scripts/game.pgn diff --git a/Scripts/game.pgn b/Scripts/game.pgn index 6ae6017..d73d958 100644 --- a/Scripts/game.pgn +++ b/Scripts/game.pgn @@ -2,42 +2,44 @@ [Site "?"] [Date "2025.04.01"] [Round "1"] -[White "Eleanor-dev"] -[Black "Eleanor-base"] +[White "Eleanor-base"] +[Black "Eleanor-dev"] [Result "1-0"] -[FEN "2r3k1/2B1Rpp1/p1P5/5b1p/PP6/5P2/6P1/6K1 b - - 0 1"] -[GameDuration "00:00:03"] -[GameEndTime "2025-04-01T04:05:33.741 UTC"] -[GameStartTime "2025-04-01T04:05:30.048 UTC"] -[PlyCount "54"] +[FEN "2r5/1k6/2p4p/4p3/3b1p1B/5B1P/2R5/7K b - - 0 1"] +[GameDuration "00:00:05"] +[GameEndTime "2025-04-01T18:10:20.331 Central Europe Daylight Time"] +[GameStartTime "2025-04-01T18:10:14.365 Central Europe Daylight Time"] +[PlyCount "58"] [SetUp "1"] [Termination "abandoned"] -[TimeControl "2.029+0.02"] +[TimeControl "2.53+0.03"] -1... Kf8 {-0.96 14/0 100 227191} 2. Re5 {+1.00 14/0 81 216018} -Rxc7 {-1.08 14/0 108 289288} 3. Rxf5 {+1.10 14/0 75 183753} -Rxc6 {-1.11 14/0 103 272990} 4. Rxh5 {+1.10 12/0 106 90738} -Ke7 {-1.11 12/0 99 23080} 5. Rh8 {+1.15 11/0 89 240527} -Rb6 {-1.03 10/0 95 90255} 6. Ra8 {+1.26 11/0 98 110373} -Kd6 {-1.30 11/0 82 198678} 7. a5 {+1.17 13/0 77 230009} -Rxb4 {-1.17 12/0 62 180719} 8. Rxa6+ {+1.11 12/0 92 156151} -Kc7 {-1.11 13/0 91 222192} 9. g4 {+1.04 11/0 88 129794} -Kb7 {-1.12 12/0 65 174745} 10. Rd6 {+1.02 12/0 85 89925} -Kc7 {-1.14 12/0 61 150972} 11. Rd5 {+1.02 12/0 66 169533} -g6 {-1.03 11/0 58 153259} 12. Kf2 {+1.04 11/0 79 76310} -Kc6 {-1.05 12/0 77 104110} 13. Rd8 {+1.03 13/0 76 130262} -Ra4 {-1.09 12/0 52 149145} 14. Rf8 {+1.07 13/0 76 103977} -Rxa5 {-1.09 13/0 67 173629} 15. Rxf7 {+1.08 12/0 71 6550} -g5 {-1.13 12/0 70 87384} 16. Rf5 {+1.17 13/0 52 156470} -Rxf5 {-1.70 15/0 67 119960} 17. gxf5 {+2.54 19/0 66 252419} -Kd6 {-2.43 18/0 51 177302} 18. Kg3 {+2.60 19/0 56 207073} -Ke5 {-2.16 22/0 57 234443} 19. Kg4 {+2.60 21/0 62 107886} -Kf6 {0.00 27/0 44 189069} 20. f4 {0.00 27/0 48 212931} -gxf4 {0.00 41/0 43 205787} 21. Kxf4 {0.00 42/0 38 152370} -Kf7 {0.00 48/0 41 174874} 22. Kg5 {0.00 45/0 44 184855} -Kg7 {0.00 50/0 42 148502} 23. Kh5 {0.00 49/0 44 181071} -Kf7 {0.00 55/0 51 204137} 24. Kh4 {0.00 55/0 56 65546} Kf6 {0.00 60/0 53 207131} -25. Kg4 {0.00 57/0 55 49048} Kf7 {0.00 60/0 36 153699} -26. Kg5 {0.00 60/0 52 35277} Kg7 {0.00 61/0 42 132702} -27. f6+ {0.00 60/0 50 126713} Kf7 {0.00 64/0 37 46021} -28. Kf5 {0.00 63/0 42 168465, Black disconnects} 1-0 \ No newline at end of file +1... Kb6 {-0.43 11/0 127 176492} 2. Kg2 {+0.45 10/0 117 182773} +Kb5 {-0.43 10/0 139 43190} 3. Bf2 {+0.48 10/0 94 154681} +Rg8+ {-0.41 10/0 104 181006} 4. Kf1 {+0.43 11/0 137 107880} +c5 {-0.42 11/0 114 186919} 5. Ke2 {+0.42 10/0 131 26558} +Rd8 {-0.31 11/0 126 178073} 6. Rd2 {+0.36 8/0 129 64726} +c4 {+0.11 11/0 124 101858} 7. Bxd4 {-0.11 11/0 86 135900} +Rxd4 {+0.10 11/0 124 121262} 8. Rxd4 {-0.01 13/0 117 207646} +exd4 {0.00 14/0 95 197856} 9. h4 {0.00 13/0 105 197264} +Kb4 {0.00 14/0 91 195163} 10. Kd2 {0.00 13/0 111 105382} +Kc5 {0.00 13/0 109 62420} 11. h5 {0.00 14/0 78 150612} Kb4 {0.00 16/0 79 181784} +12. Bd1 {0.00 15/0 86 193192} Kc5 {0.00 16/0 97 111839} +13. Kc2 {0.00 17/0 90 192373} Kb4 {0.00 17/0 95 120438} +14. Bf3 {0.00 17/0 76 176571} Kc5 {0.00 17/0 101 66844} +15. Ba8 {0.00 18/0 75 165249} Kb4 {0.00 18/0 86 198233} +16. Bf3 {0.00 18/0 77 183908} d3+ {+0.01 16/0 69 164278} +17. Kd2 {-0.03 16/0 81 145118} Kb3 {0.00 17/0 84 107987} +18. Be4 {0.00 16/0 61 125373} f3 {+0.09 17/0 75 163654} +19. Bxd3 {-0.01 16/0 86 109653} cxd3 {+0.57 18/0 55 140500} +20. Kxd3 {-0.97 22/0 73 227257} f2 {+0.98 22/0 71 204108} +21. Ke2 {-0.99 20/0 81 29772} Kc4 {+1.48 22/0 62 180691} +22. Kxf2 {-2.11 23/0 59 175088} Kd4 {+2.51 27/0 52 189697} +23. Kf3 {-3.34 26/0 62 187791} Ke5 {+2.50 32/0 48 167062} +24. Ke3 {-2.24 28/0 54 178563} Kf5 {+2.15 31/0 72 166014} +25. Kf3 {-2.22 32/0 52 188757} Kg5 {0.00 35/0 49 143292} +26. Ke4 {-2.09 32/0 62 209505} Kxh5 {0.00 39/0 48 159744} +27. Kf5 {-2.09 33/0 70 226933} Kh4 {0.00 52/0 46 161548} +28. Kf4 {0.00 37/0 51 178144} h5 {0.00 64/0 29 95868} +29. Kf3 {0.00 51/0 46 165319} Kh3 {0.00 64/0 15 14259} +30. Kf4 {0.00 52/0 64 186059, Black disconnects} 1-0 \ No newline at end of file diff --git a/source/search.h b/source/search.h index 38bd2d4..485070f 100644 --- a/source/search.h +++ b/source/search.h @@ -27,7 +27,7 @@ constexpr int inf = 100000; inline bool searchStopped = false; // [id][ply] -inline int killerMoves[2][64]; +inline int killerMoves[2][MAX_PLY]; inline int lmrTable[MAX_DEPTH+1][MAX_MOVES]; -- 2.47.3 From 649bee6490a917f7eedc43f58638c83e78c0c64b Mon Sep 17 00:00:00 2001 From: rektdie Date: Tue, 1 Apr 2025 20:15:44 +0200 Subject: [PATCH 15/27] playing matches in datagen | bench 1857933 --- source/datagen.cpp | 51 ++++++++++++++++++++++++++++++++-------------- source/datagen.h | 2 ++ source/search.cpp | 6 +++--- source/utils.cpp | 36 ++++++++++---------------------- 4 files changed, 52 insertions(+), 43 deletions(-) diff --git a/source/datagen.cpp b/source/datagen.cpp index 9af39ce..373dc89 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -19,16 +19,34 @@ static void PlayRandMoves(Board &board) { bool isLegal = board.MakeMove(board.moveList[dist(rng)]); if (!isLegal) count--; } + + MOVEGEN::GenerateMoves(board); } static bool IsGameOver(Board &board) { - // TODO + bool isDraw = SEARCH::IsDraw(board); + if (isDraw) return true; + + int moveSeen = 0; + + for (int i = 0; i < board.currentMoveIndex; i++) { + Board copy = board; + bool isLegal = copy.MakeMove(board.moveList[i]); + + if (!isLegal) continue; + positionIndex--; + moveSeen++; + } + + if (moveSeen == 0) return true; + return false; } void Run(int games) { for (int i = 0; i < games; i++) { + Game game; Board board; - MarlinFormat position; + uint8_t wdl = 1; positionIndex = 0; std::memset(SEARCH::killerMoves, 0, sizeof(SEARCH::killerMoves)); @@ -36,25 +54,28 @@ void Run(int games) { PlayRandMoves(board); + int staticEval = HCE::Evaluate(board); + SEARCH::SearchResults safeResults; - - // while (!IsGameOver(board)) { - // SEARCH::SearchResults results = SEARCH::SearchPosition(board, SearchParams()); - // if (!results.bestMove) break; - // safeResults = results; + MOVEGEN::GenerateMoves(board); - // board.MakeMove(results.bestMove); - // } - - if (std::abs(safeResults.score) >= SEARCH::inf - SEARCH::MAX_DEPTH) { + while (!IsGameOver(board)) { + SEARCH::SearchResults results = SEARCH::SearchPosition(board, SearchParams()); + + if (!results.bestMove) break; + safeResults = results; + + game.moves.push_back(ScoredMove(results.bestMove, results.score)); + board.MakeMove(results.bestMove); + MOVEGEN::GenerateMoves(board); + } + + if (std::abs(safeResults.score) >= std::abs(SEARCH::MATE_SCORE) - SEARCH::MAX_PLY) { wdl = board.sideToMove ? 2 : 0; } - position.packFrom(board, HCE::Evaluate(board), wdl); - board.PrintBoard(); - std::cout << "Eval: " << position.eval << std::endl; - std::cout << "WDL: " << int(position.wdl) << std::endl; + game.format.packFrom(board, staticEval, wdl); } } } \ No newline at end of file diff --git a/source/datagen.h b/source/datagen.h index 03a5b18..41eb5fd 100644 --- a/source/datagen.h +++ b/source/datagen.h @@ -44,6 +44,8 @@ struct Game { MarlinFormat format; std::vector moves; + Game(){}; + Game(MarlinFormat f, std::vector m) { format = f; moves = m; diff --git a/source/search.cpp b/source/search.cpp index a42e8a2..9ab0558 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -323,6 +323,9 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { score = -PVS(copy, newDepth, -beta, -alpha, ply + 1).score; } + moveSeen++; + positionIndex--; + if (searchStopped) return 0; if (currMove.IsQuiet()) { @@ -330,9 +333,6 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { seenQuietsCount++; } - moveSeen++; - positionIndex--; - // Fail high (beta cutoff) if (score >= beta) { if (!currMove.IsCapture()) { diff --git a/source/utils.cpp b/source/utils.cpp index 0dc7d10..a2ca6cb 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -148,11 +148,10 @@ Move parseMove(Board &board, std::string_view str) { } std::array CompressPieces(Board &board) { - const uint8_t UNMOVED_ROOK = 0x10; // Special value for unmoved rooks + const uint8_t UNMOVED_ROOK = 0x08; // We only have 4 bits, so we use bit 3 to mark unmoved rooks + std::array compressed{}; // Zero-initialize - std::array compressed; - - for (int square = a1; square <= h8; square++) { + for (int square = 0; square < 64; square++) { int pieceCode = 0; for (int pieceType = Pawn; pieceType <= King; pieceType++) { @@ -160,38 +159,25 @@ std::array CompressPieces(Board &board) { if (pieceBitboard.IsSet(square)) { if (board.colors[White] & board.pieces[pieceType]) { - pieceCode = pieceType + 1; // White pieces: 1-6 + pieceCode = pieceType; // White: 1-6 } else { - pieceCode = pieceType + 7; // Black pieces: 7-12 + pieceCode = pieceType + 6; // Black: 7-12 } - // If it's a rook and hasn't moved (check castling rights) + // Check for unmoved rook if (pieceType == Rook) { - if (board.sideToMove == White) { - // If the white rooks are still eligible for castling, they haven't moved - if ((board.castlingRights & (whiteKingRight | whiteQueenRight)) != 0) { - pieceCode |= UNMOVED_ROOK; // Mark as unmoved rook - } - } else if (board.sideToMove == Black) { - // If the black rooks are still eligible for castling, they haven't moved - if ((board.castlingRights & (blackKingRight | blackQueenRight)) != 0) { - pieceCode |= UNMOVED_ROOK; // Mark as unmoved rook - } + if ((board.castlingRights & (whiteKingRight | whiteQueenRight | blackKingRight | blackQueenRight)) != 0) { + pieceCode |= UNMOVED_ROOK; // Mark as unmoved rook (bit 3 set) } } break; } } - // Determine the byte index for storing the piece code (since we are packing 2 pieces per byte) - int byteIndex = square / 2; + int byteIndex = square / 4; + int shift = (square % 4) * 4; - // Pack the piece code into the byte array - if (square % 2 == 0) { - compressed[byteIndex] = (compressed[byteIndex] & 0xF0) | (pieceCode & 0x0F); - } else { - compressed[byteIndex] = (compressed[byteIndex] & 0x0F) | ((pieceCode & 0x0F) << 4); - } + compressed[byteIndex] |= (pieceCode & 0x0F) << shift; // Pack 4-bit pieceCode } return compressed; -- 2.47.3 From 7c62a3e6725afe0fe113b102094df74fc1d85904 Mon Sep 17 00:00:00 2001 From: rektdie Date: Tue, 1 Apr 2025 21:29:00 +0200 Subject: [PATCH 16/27] writing into binpack file | bench 1857933 --- source/datagen.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++++++ source/datagen.h | 2 ++ source/utils.cpp | 9 ++++-- 3 files changed, 83 insertions(+), 3 deletions(-) diff --git a/source/datagen.cpp b/source/datagen.cpp index 373dc89..b3edbcc 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "datagen.h" #include "movegen.h" #include "evaluate.h" @@ -42,6 +43,79 @@ static bool IsGameOver(Board &board) { return false; } + +// Serialize MarlinFormat +std::ostream &operator<<(std::ostream &os, const MarlinFormat &format) { + os.write(reinterpret_cast(&format), sizeof(MarlinFormat)); + return os; +} + +// Deserialize MarlinFormat +std::istream &operator>>(std::istream &is, MarlinFormat &format) { + is.read(reinterpret_cast(&format), sizeof(MarlinFormat)); + return is; +} + +// Serialize ScoredMove +std::ostream &operator<<(std::ostream &os, const ScoredMove &move) { + os.write(reinterpret_cast(&move), sizeof(ScoredMove)); + return os; +} + +// Deserialize ScoredMove +std::istream &operator>>(std::istream &is, ScoredMove &move) { + is.read(reinterpret_cast(&move), sizeof(ScoredMove)); + return is; +} + +// Serialize Game +std::ostream &operator<<(std::ostream &os, const Game &game) { + os << game.format; // Write MarlinFormat + + uint32_t moveCount = game.moves.size(); + os.write(reinterpret_cast(&moveCount), sizeof(moveCount)); // Write move count + + for (const auto &move : game.moves) { + os << move; // Write each move + } + + return os; +} + +// Deserialize Game +std::istream &operator>>(std::istream &is, Game &game) { + is >> game.format; // Read MarlinFormat + + uint32_t moveCount; + is.read(reinterpret_cast(&moveCount), sizeof(moveCount)); // Read move count + + game.moves.resize(moveCount); + for (auto &move : game.moves) { + is >> move; // Read each move + } + + return is; +} + +void WriteToFile(const Game &game, const std::string &filename) { + std::ofstream file(filename, std::ios::binary); + if (!file) throw std::runtime_error("Failed to open file for writing"); + + // Write MarlinFormat (full format) + file.write(reinterpret_cast(&game.format), sizeof(MarlinFormat)); + + // Write each ScoredMove + for (const auto &move : game.moves) { + file.write(reinterpret_cast(&move), sizeof(ScoredMove)); + } + + // Write 4 zero bytes at the end + uint32_t zero = 0; + file.write(reinterpret_cast(&zero), sizeof(zero)); + + file.close(); +} + void Run(int games) { for (int i = 0; i < games; i++) { Game game; @@ -76,6 +150,7 @@ void Run(int games) { } game.format.packFrom(board, staticEval, wdl); + WriteToFile(game, "data.binpack"); } } } \ No newline at end of file diff --git a/source/datagen.h b/source/datagen.h index 41eb5fd..88eb057 100644 --- a/source/datagen.h +++ b/source/datagen.h @@ -34,6 +34,8 @@ struct ScoredMove { uint16_t move; int16_t score; + ScoredMove() : move(0), score(0) {} + ScoredMove(Move m, int s) { move = m; score = s; diff --git a/source/utils.cpp b/source/utils.cpp index a2ca6cb..599b3fc 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -148,7 +148,7 @@ Move parseMove(Board &board, std::string_view str) { } std::array CompressPieces(Board &board) { - const uint8_t UNMOVED_ROOK = 0x08; // We only have 4 bits, so we use bit 3 to mark unmoved rooks + const uint8_t UNMOVED_ROOK = 0x08; // We use bit 3 to mark unmoved rooks std::array compressed{}; // Zero-initialize for (int square = 0; square < 64; square++) { @@ -164,9 +164,12 @@ std::array CompressPieces(Board &board) { pieceCode = pieceType + 6; // Black: 7-12 } - // Check for unmoved rook + // Check for unmoved rooks (based solely on castling rights) if (pieceType == Rook) { - if ((board.castlingRights & (whiteKingRight | whiteQueenRight | blackKingRight | blackQueenRight)) != 0) { + if ((board.colors[White] & board.pieces[Rook]) && (board.castlingRights & (whiteKingRight | whiteQueenRight))) { + pieceCode |= UNMOVED_ROOK; // Mark as unmoved rook (bit 3 set) + } + if ((board.colors[Black] & board.pieces[Rook]) && (board.castlingRights & (blackKingRight | blackQueenRight))) { pieceCode |= UNMOVED_ROOK; // Mark as unmoved rook (bit 3 set) } } -- 2.47.3 From 93a199db17ddd27fb43e4f5ffefea883b4ad7dc0 Mon Sep 17 00:00:00 2001 From: rektdie Date: Tue, 1 Apr 2025 23:38:20 +0200 Subject: [PATCH 17/27] single threaded datagen with printing | bench 1857933 --- .gitignore | 1 + source/datagen.cpp | 108 ++++++++++++++++++++++++++++++++++++++------- source/datagen.h | 2 +- source/main.cpp | 11 +++-- source/utils.cpp | 11 +++-- 5 files changed, 107 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index 0a6146f..74eaf9f 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ Eleanor # Other .vscode Scripts/game.pgn +data.binpack \ No newline at end of file diff --git a/source/datagen.cpp b/source/datagen.cpp index b3edbcc..45cfbe9 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -1,11 +1,48 @@ #include #include #include +#include +#include #include "datagen.h" #include "movegen.h" #include "evaluate.h" #include "search.h" #include "movegen.h" +#include "stopwatch.h" + +#ifdef _WIN32 +#include +#endif + +// Function to hide the cursor on Windows +#ifdef _WIN32 +void HideCursor() { + HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + CONSOLE_CURSOR_INFO cursorInfo; + GetConsoleCursorInfo(hConsole, &cursorInfo); + cursorInfo.bVisible = FALSE; // Set cursor visibility to false + SetConsoleCursorInfo(hConsole, &cursorInfo); +} + +// Function to show the cursor on Windows +void ShowCursor() { + HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + CONSOLE_CURSOR_INFO cursorInfo; + GetConsoleCursorInfo(hConsole, &cursorInfo); + cursorInfo.bVisible = TRUE; // Set cursor visibility to true + SetConsoleCursorInfo(hConsole, &cursorInfo); +} +#endif + +// Function to hide the cursor on Linux/macOS +void HideCursorLinux() { + std::cout << "\033[?25l"; // ANSI escape sequence to hide the cursor +} + +// Function to show the cursor on Linux/macOS +void ShowCursorLinux() { + std::cout << "\033[?25h"; // ANSI escape sequence to show the cursor +} namespace DATAGEN { @@ -43,26 +80,21 @@ static bool IsGameOver(Board &board) { return false; } - -// Serialize MarlinFormat std::ostream &operator<<(std::ostream &os, const MarlinFormat &format) { os.write(reinterpret_cast(&format), sizeof(MarlinFormat)); return os; } -// Deserialize MarlinFormat std::istream &operator>>(std::istream &is, MarlinFormat &format) { is.read(reinterpret_cast(&format), sizeof(MarlinFormat)); return is; } -// Serialize ScoredMove std::ostream &operator<<(std::ostream &os, const ScoredMove &move) { os.write(reinterpret_cast(&move), sizeof(ScoredMove)); return os; } -// Deserialize ScoredMove std::istream &operator>>(std::istream &is, ScoredMove &move) { is.read(reinterpret_cast(&move), sizeof(ScoredMove)); return is; @@ -70,13 +102,13 @@ std::istream &operator>>(std::istream &is, ScoredMove &move) { // Serialize Game std::ostream &operator<<(std::ostream &os, const Game &game) { - os << game.format; // Write MarlinFormat + os << game.format; uint32_t moveCount = game.moves.size(); - os.write(reinterpret_cast(&moveCount), sizeof(moveCount)); // Write move count + os.write(reinterpret_cast(&moveCount), sizeof(moveCount)); for (const auto &move : game.moves) { - os << move; // Write each move + os << move; } return os; @@ -84,40 +116,70 @@ std::ostream &operator<<(std::ostream &os, const Game &game) { // Deserialize Game std::istream &operator>>(std::istream &is, Game &game) { - is >> game.format; // Read MarlinFormat + is >> game.format; uint32_t moveCount; - is.read(reinterpret_cast(&moveCount), sizeof(moveCount)); // Read move count + is.read(reinterpret_cast(&moveCount), sizeof(moveCount)); game.moves.resize(moveCount); for (auto &move : game.moves) { - is >> move; // Read each move + is >> move; } return is; } void WriteToFile(const Game &game, const std::string &filename) { - std::ofstream file(filename, std::ios::binary); + std::ofstream file(filename, std::ios::binary | std::ios::app); if (!file) throw std::runtime_error("Failed to open file for writing"); // Write MarlinFormat (full format) file.write(reinterpret_cast(&game.format), sizeof(MarlinFormat)); - // Write each ScoredMove for (const auto &move : game.moves) { file.write(reinterpret_cast(&move), sizeof(ScoredMove)); } - // Write 4 zero bytes at the end uint32_t zero = 0; file.write(reinterpret_cast(&zero), sizeof(zero)); file.close(); } -void Run(int games) { - for (int i = 0; i < games; i++) { +void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch) { + double elapsed = stopwatch.GetElapsedSec(); + double positionsPerSec = elapsed > 0 ? positions / elapsed : 0; + + double positionsInThousands = positions / 1000.0; + double targetInThousands = targetPositions / 1000.0; + + std::cout << "\033[H"; + std::cout << "Datagen: " << std::fixed << std::setprecision(2) << std::setw(6) + << positionsInThousands << "K positions processed | " + << std::setw(6) << targetInThousands << "K target" << std::endl; + + std::cout << "Elapsed Time: " << std::fixed << std::setprecision(2) << elapsed << " sec" << std::endl; + std::cout << "Positions/sec: " << std::fixed << std::setprecision(2) << positionsPerSec << std::endl; + std::cout.flush(); +} + +void Run(int targetPositions, int threads) { + // Clear the console and hide the cursor + #ifdef _WIN32 + system("cls"); + HideCursor(); // Hide the cursor on Windows + #else + std::cout << "\033[2J\033[H"; // Clear console and move cursor to top-left on Linux/macOS + HideCursorLinux(); // Hide the cursor on Linux/macOS + #endif + + Stopwatch stopwatch; + stopwatch.Restart(); + + int positions = 0; + int lastPrintedPosition = 0; + + while (positions < targetPositions * 1000000) { Game game; Board board; @@ -143,6 +205,8 @@ void Run(int games) { game.moves.push_back(ScoredMove(results.bestMove, results.score)); board.MakeMove(results.bestMove); MOVEGEN::GenerateMoves(board); + + positions++; } if (std::abs(safeResults.score) >= std::abs(SEARCH::MATE_SCORE) - SEARCH::MAX_PLY) { @@ -151,6 +215,18 @@ void Run(int games) { game.format.packFrom(board, staticEval, wdl); WriteToFile(game, "data.binpack"); + + if (positions / 100 > lastPrintedPosition) { + lastPrintedPosition = positions / 1000; + PrintProgress(positions, targetPositions * 1000000, stopwatch); + } } + + // Show the cursor again after the process is done + #ifdef _WIN32 + ShowCursor(); // Show the cursor on Windows + #else + ShowCursorLinux(); // Show the cursor on Linux/macOS + #endif } } \ No newline at end of file diff --git a/source/datagen.h b/source/datagen.h index 88eb057..629115a 100644 --- a/source/datagen.h +++ b/source/datagen.h @@ -54,5 +54,5 @@ struct Game { } }; -void Run(int games); +void Run(int targetPositions, int threads); } \ No newline at end of file diff --git a/source/main.cpp b/source/main.cpp index 818c263..486cee2 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -18,11 +18,16 @@ int main(int argc, char* argv[]) { if (std::string(argv[1]) == "bench") { RunBenchmark(); } else if (std::string(argv[1]) == "datagen") { - int count = 1; + int positions = 1; + int threads = 1; if (argc > 2) { - count = std::stoi(argv[2]); + positions = std::stoi(argv[2]); + + if (argc > 3) { + threads = std::stoi(argv[3]); + } } - DATAGEN::Run(count); + DATAGEN::Run(positions, threads); } } else { UCILoop(board); diff --git a/source/utils.cpp b/source/utils.cpp index 599b3fc..aff6ff8 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -148,8 +148,8 @@ Move parseMove(Board &board, std::string_view str) { } std::array CompressPieces(Board &board) { - const uint8_t UNMOVED_ROOK = 0x08; // We use bit 3 to mark unmoved rooks - std::array compressed{}; // Zero-initialize + const uint8_t UNMOVED_ROOK = 0x08; + std::array compressed{}; for (int square = 0; square < 64; square++) { int pieceCode = 0; @@ -164,13 +164,12 @@ std::array CompressPieces(Board &board) { pieceCode = pieceType + 6; // Black: 7-12 } - // Check for unmoved rooks (based solely on castling rights) if (pieceType == Rook) { if ((board.colors[White] & board.pieces[Rook]) && (board.castlingRights & (whiteKingRight | whiteQueenRight))) { - pieceCode |= UNMOVED_ROOK; // Mark as unmoved rook (bit 3 set) + pieceCode |= UNMOVED_ROOK; } if ((board.colors[Black] & board.pieces[Rook]) && (board.castlingRights & (blackKingRight | blackQueenRight))) { - pieceCode |= UNMOVED_ROOK; // Mark as unmoved rook (bit 3 set) + pieceCode |= UNMOVED_ROOK; } } break; @@ -180,7 +179,7 @@ std::array CompressPieces(Board &board) { int byteIndex = square / 4; int shift = (square % 4) * 4; - compressed[byteIndex] |= (pieceCode & 0x0F) << shift; // Pack 4-bit pieceCode + compressed[byteIndex] |= (pieceCode & 0x0F) << shift; } return compressed; -- 2.47.3 From ac2b8cc0a4de7c4a3457e5749afa694384fd5311 Mon Sep 17 00:00:00 2001 From: rektdie Date: Wed, 2 Apr 2025 00:00:00 +0200 Subject: [PATCH 18/27] prettier printing | bench 1857933 --- source/datagen.cpp | 121 +++++++++++++++++++++++++++++++-------------- 1 file changed, 85 insertions(+), 36 deletions(-) diff --git a/source/datagen.cpp b/source/datagen.cpp index 45cfbe9..3a71e77 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -14,34 +14,30 @@ #include #endif -// Function to hide the cursor on Windows #ifdef _WIN32 -void HideCursor() { +static void HideCursor() { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO cursorInfo; GetConsoleCursorInfo(hConsole, &cursorInfo); - cursorInfo.bVisible = FALSE; // Set cursor visibility to false + cursorInfo.bVisible = FALSE; SetConsoleCursorInfo(hConsole, &cursorInfo); } -// Function to show the cursor on Windows -void ShowCursor() { +static void ShowCursor() { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO cursorInfo; GetConsoleCursorInfo(hConsole, &cursorInfo); - cursorInfo.bVisible = TRUE; // Set cursor visibility to true + cursorInfo.bVisible = TRUE; SetConsoleCursorInfo(hConsole, &cursorInfo); } #endif -// Function to hide the cursor on Linux/macOS -void HideCursorLinux() { - std::cout << "\033[?25l"; // ANSI escape sequence to hide the cursor +static void HideCursorLinux() { + std::cout << "\033[?25l"; } -// Function to show the cursor on Linux/macOS -void ShowCursorLinux() { - std::cout << "\033[?25h"; // ANSI escape sequence to show the cursor +static void ShowCursorLinux() { + std::cout << "\033[?25h"; } namespace DATAGEN { @@ -100,7 +96,6 @@ std::istream &operator>>(std::istream &is, ScoredMove &move) { return is; } -// Serialize Game std::ostream &operator<<(std::ostream &os, const Game &game) { os << game.format; @@ -114,7 +109,6 @@ std::ostream &operator<<(std::ostream &os, const Game &game) { return os; } -// Deserialize Game std::istream &operator>>(std::istream &is, Game &game) { is >> game.format; @@ -129,19 +123,29 @@ std::istream &operator>>(std::istream &is, Game &game) { return is; } -void WriteToFile(const Game &game, const std::string &filename) { - std::ofstream file(filename, std::ios::binary | std::ios::app); - if (!file) throw std::runtime_error("Failed to open file for writing"); +static void WriteToFile(const Game &game, const std::string &filename) { + // Create a buffer to store the serialized data [prevents corruption] + std::vector buffer; - // Write MarlinFormat (full format) - file.write(reinterpret_cast(&game.format), sizeof(MarlinFormat)); + const size_t formatSize = sizeof(MarlinFormat); + buffer.resize(formatSize + sizeof(uint32_t) * game.moves.size() + sizeof(uint32_t)); + + std::memcpy(buffer.data(), &game.format, formatSize); + + size_t offset = formatSize; for (const auto &move : game.moves) { - file.write(reinterpret_cast(&move), sizeof(ScoredMove)); + std::memcpy(buffer.data() + offset, &move, sizeof(ScoredMove)); + offset += sizeof(ScoredMove); } uint32_t zero = 0; - file.write(reinterpret_cast(&zero), sizeof(zero)); + std::memcpy(buffer.data() + offset, &zero, sizeof(zero)); + + std::ofstream file(filename, std::ios::binary | std::ios::out | std::ios::trunc); + if (!file) throw std::runtime_error("Failed to open file for writing"); + + file.write(buffer.data(), buffer.size()); file.close(); } @@ -150,27 +154,73 @@ void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch) { double elapsed = stopwatch.GetElapsedSec(); double positionsPerSec = elapsed > 0 ? positions / elapsed : 0; - double positionsInThousands = positions / 1000.0; - double targetInThousands = targetPositions / 1000.0; + int positionsInThousands = static_cast(round(positions / 1000.0)); + int targetInThousands = static_cast(round(targetPositions / 1000.0)); std::cout << "\033[H"; - std::cout << "Datagen: " << std::fixed << std::setprecision(2) << std::setw(6) - << positionsInThousands << "K positions processed | " - << std::setw(6) << targetInThousands << "K target" << std::endl; - std::cout << "Elapsed Time: " << std::fixed << std::setprecision(2) << elapsed << " sec" << std::endl; - std::cout << "Positions/sec: " << std::fixed << std::setprecision(2) << positionsPerSec << std::endl; - std::cout.flush(); + int width = 60; + + std::string title = "Eleanor - Datagen"; + int titlePadding = (width - title.length()) / 2; + std::cout << std::setw(titlePadding + title.length()) << title << std::endl; + + std::cout << std::string(width, '=') << std::endl; + + std::cout << std::endl; + + std::cout << std::fixed << std::setprecision(0); + + std::ostringstream datagenTextStream; + datagenTextStream << std::fixed << std::setprecision(2) + << "Positions processed: " << positions / 1000.0 << "K | Target: " << targetPositions / 1000.0 << "K"; + std::string datagenText = datagenTextStream.str(); + int datagenPadding = (width - datagenText.length()) / 2; + std::cout << std::setw(datagenPadding + datagenText.length()) << datagenText << std::endl; + + std::cout << std::endl; + + std::string elapsedText = "Elapsed Time: " + std::to_string(static_cast(round(elapsed))) + " sec"; + int elapsedPadding = (width - elapsedText.length()) / 2; + std::cout << std::setw(elapsedPadding + elapsedText.length()) << elapsedText << std::endl; + + std::string positionsPerSecText = "Positions/sec: " + std::to_string(static_cast(round(positionsPerSec))); + int positionsPerSecPadding = (width - positionsPerSecText.length()) / 2; + std::cout << std::setw(positionsPerSecPadding + positionsPerSecText.length()) << positionsPerSecText << std::endl; + + std::cout << std::endl; + + int barWidth = 50; + double progress = (double)positions / targetPositions; + int pos = (int)(progress * barWidth); + + std::string progressBar = "["; + for (int i = 0; i < barWidth; ++i) { + if (i < pos) { + progressBar += "="; + } else if (i == pos) { + progressBar += ">"; + } else { + progressBar += " "; + } + } + progressBar += "] " + std::to_string(static_cast(round(progress * 100.0))) + " %"; + + int progressPadding = (width - progressBar.length()) / 2; + std::cout << std::setw(progressPadding + progressBar.length()) << progressBar << std::endl; + + std::cout << std::endl; + + std::cout << std::string(width, '-') << std::endl; } void Run(int targetPositions, int threads) { - // Clear the console and hide the cursor #ifdef _WIN32 system("cls"); - HideCursor(); // Hide the cursor on Windows + HideCursor(); #else - std::cout << "\033[2J\033[H"; // Clear console and move cursor to top-left on Linux/macOS - HideCursorLinux(); // Hide the cursor on Linux/macOS + std::cout << "\033[2J\033[H"; + HideCursorLinux(); #endif Stopwatch stopwatch; @@ -222,11 +272,10 @@ void Run(int targetPositions, int threads) { } } - // Show the cursor again after the process is done #ifdef _WIN32 - ShowCursor(); // Show the cursor on Windows + ShowCursor(); #else - ShowCursorLinux(); // Show the cursor on Linux/macOS + ShowCursorLinux(); #endif } } \ No newline at end of file -- 2.47.3 From 8287ac739a45edc32c972876103043292d06b308 Mon Sep 17 00:00:00 2001 From: rektdie Date: Wed, 2 Apr 2025 00:20:06 +0200 Subject: [PATCH 19/27] printing thread count --- source/datagen.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/source/datagen.cpp b/source/datagen.cpp index 3a71e77..ec537df 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -150,7 +150,7 @@ static void WriteToFile(const Game &game, const std::string &filename) { file.close(); } -void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch) { +void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int threads) { double elapsed = stopwatch.GetElapsedSec(); double positionsPerSec = elapsed > 0 ? positions / elapsed : 0; @@ -190,6 +190,12 @@ void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch) { std::cout << std::endl; + std::string threadsText = "Threads: " + std::to_string(static_cast(threads)); + int threadsTextPadding = (width - threadsText.length()) / 2; + std::cout << std::setw(threadsTextPadding + threadsText.length()) << threadsText << std::endl; + + std::cout << std::endl; + int barWidth = 50; double progress = (double)positions / targetPositions; int pos = (int)(progress * barWidth); @@ -268,7 +274,7 @@ void Run(int targetPositions, int threads) { if (positions / 100 > lastPrintedPosition) { lastPrintedPosition = positions / 1000; - PrintProgress(positions, targetPositions * 1000000, stopwatch); + PrintProgress(positions, targetPositions * 1000000, stopwatch, threads); } } -- 2.47.3 From 114ef9728abe982d2e4c4ab7c454476faf9bdab2 Mon Sep 17 00:00:00 2001 From: rektdie Date: Wed, 2 Apr 2025 00:48:11 +0200 Subject: [PATCH 20/27] appending to file + printing fixes --- source/datagen.cpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/source/datagen.cpp b/source/datagen.cpp index ec537df..8e7f7be 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -142,7 +142,7 @@ static void WriteToFile(const Game &game, const std::string &filename) { uint32_t zero = 0; std::memcpy(buffer.data() + offset, &zero, sizeof(zero)); - std::ofstream file(filename, std::ios::binary | std::ios::out | std::ios::trunc); + std::ofstream file(filename, std::ios::binary | std::ios::out | std::ios::app); if (!file) throw std::runtime_error("Failed to open file for writing"); file.write(buffer.data(), buffer.size()); @@ -234,8 +234,9 @@ void Run(int targetPositions, int threads) { int positions = 0; int lastPrintedPosition = 0; + double lastPrintedTime = stopwatch.GetElapsedSec(); - while (positions < targetPositions * 1000000) { + while (positions < targetPositions * 1000) { Game game; Board board; @@ -262,7 +263,10 @@ void Run(int targetPositions, int threads) { board.MakeMove(results.bestMove); MOVEGEN::GenerateMoves(board); - positions++; + // Increment positions, but do not exceed target + if (positions < targetPositions * 1000) { + positions++; + } } if (std::abs(safeResults.score) >= std::abs(SEARCH::MATE_SCORE) - SEARCH::MAX_PLY) { @@ -272,16 +276,21 @@ void Run(int targetPositions, int threads) { game.format.packFrom(board, staticEval, wdl); WriteToFile(game, "data.binpack"); - if (positions / 100 > lastPrintedPosition) { - lastPrintedPosition = positions / 1000; - PrintProgress(positions, targetPositions * 1000000, stopwatch, threads); + // Print progress every second + double currentTime = stopwatch.GetElapsedSec(); + if (currentTime - lastPrintedTime >= 1) { + lastPrintedTime = currentTime; + PrintProgress(positions, targetPositions * 1000, stopwatch, threads); } } + PrintProgress(positions, targetPositions * 1000, stopwatch, threads); + #ifdef _WIN32 ShowCursor(); #else ShowCursorLinux(); #endif } + } \ No newline at end of file -- 2.47.3 From 63f5c61063ec39011072d21a1e45199c62cd247d Mon Sep 17 00:00:00 2001 From: rektdie Date: Wed, 2 Apr 2025 05:35:12 +0200 Subject: [PATCH 21/27] semi-fixed marlinformat --- README.md | 65 ------------------------------------------------ source/datagen.h | 25 ++++++++++++++++++- source/utils.cpp | 47 ++++++++++++++++------------------ 3 files changed, 45 insertions(+), 92 deletions(-) delete mode 100644 README.md diff --git a/README.md b/README.md deleted file mode 100644 index 57c3b17..0000000 --- a/README.md +++ /dev/null @@ -1,65 +0,0 @@ -![Eleanor](https://i.ibb.co/T1ZV9wN/91dea356-a725-45fd-b7a2-cd92afddcba4-1.jpg) -# Eleanor is a chess engine -## Features - -- Bitboard board representation -- Magic Bitboards -- Legal move generation -- Precalculated attack tables -- Storing moves in a 16 bit word -- Copy/make moving -- Simple evaluation (material and piece-square tables, MVV-LVA) -- Principal Variation Search -- Move ordering -- Time management -- UCI - -## Installation - -> Prerequisites: -> - A C++ compiler -> - Access to `make` command - - 1. Clone the repo to your PC - - 2. Run the command `make` in the root directory - > You can specify the compiler with the flag `CXX` - > and the executable name with `EXE`. - > By default they are `g++` and `Eleanor` respectively. - -3. Run the executable - -## How to use -There are some commands that you can use the engine with. - -- **`position`**: Set the current position of the board with [FEN](https://www.chess.com/terms/fen-chess). -`position [FEN]` or `position startpos` can be used. -- **`ucinewgame`** resets the position and game state -- **`quit`** terminates the program -- **`go`** is the command that asks what is the best move in the current position (search), -but we have to give it some parameters: -`wtime` `btime` `winc` `binc` -The first letter tells the color of player -wtime sets the remaining time of the white player -and winc sets the increments for the white player. -**It's important that these parameters are given in milliseconds!** -- **`stop`** stops the current search -- **`bench`** tests the search speed of the engine on a set of positions - -Also, the engine can be connected to any GUI software that has UCI support. - -## Sources I used -[Chess Programming Wiki](https://www.chessprogramming.org/Main_Page "Chess Programming Wiki") - -[Maksim Korzh's chess engine in C series](https://www.youtube.com/watch?v=QUNP-UjujBM&list=PLmN0neTso3Jxh8ZIylk74JpwfiWNI76Cs "Maksim Korzh's chess engine in C series") - -[Stockfish discord helping community](https://discord.com/invite/GWDRS3kU6R "Stockfish discord community") - -[Logo is generated by AI](https://deepai.org/ "Logo is generated by AI") - -
- -**The project is still in progress and developed constantly. Contributions are highly appreciated.** - -###### Last edited: 2025-01-23 - diff --git a/source/datagen.h b/source/datagen.h index 629115a..684f71e 100644 --- a/source/datagen.h +++ b/source/datagen.h @@ -1,6 +1,7 @@ #pragma once #include "board.h" #include "utils.h" +#include namespace DATAGEN { @@ -19,15 +20,37 @@ struct MarlinFormat { uint8_t extra; // 1 byte: Reserved for future use or additional data void packFrom(Board& board, int16_t eval, uint8_t wdl) { + const uint8_t stm = board.sideToMove == Black ? (1 << 7) : 0; + occupancy = board.occupied; pieces = UTILS::CompressPieces(board); - uint8_t stm_ep_square = (board.sideToMove<< 7) | (board.enPassantTarget & 0x3F); + stmEPSquare = stm | (board.enPassantTarget == noEPTarget ? 64 : board.enPassantTarget); + halfmoveClock = board.halfMoves; fullmoveNumber = board.fullMoves; this->eval = eval; this->wdl = wdl; extra = 0; } + + void writeToFile(const std::string& filename) const { + std::ofstream outFile(filename, std::ios::binary); + if (!outFile) { + std::cerr << "Error opening file for writing!" << std::endl; + return; + } + + outFile.write(reinterpret_cast(&occupancy), sizeof(occupancy)); + outFile.write(reinterpret_cast(&pieces), sizeof(pieces)); + outFile.write(reinterpret_cast(&stmEPSquare), sizeof(stmEPSquare)); + outFile.write(reinterpret_cast(&halfmoveClock), sizeof(halfmoveClock)); + outFile.write(reinterpret_cast(&fullmoveNumber), sizeof(fullmoveNumber)); + outFile.write(reinterpret_cast(&eval), sizeof(eval)); + outFile.write(reinterpret_cast(&wdl), sizeof(wdl)); + outFile.write(reinterpret_cast(&extra), sizeof(extra)); + + outFile.close(); + } }; struct ScoredMove { diff --git a/source/utils.cpp b/source/utils.cpp index aff6ff8..5de5d36 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -148,38 +148,33 @@ Move parseMove(Board &board, std::string_view str) { } std::array CompressPieces(Board &board) { - const uint8_t UNMOVED_ROOK = 0x08; + const uint8_t UNMOVED_ROOK = 6; std::array compressed{}; + Bitboard occupancy = board.occupied; + int index = 0; - for (int square = 0; square < 64; square++) { - int pieceCode = 0; + while (occupancy) { + int square = occupancy.getLS1BIndex(); + int pieceType = board.GetPieceType(square); + int pieceCode = pieceType; - for (int pieceType = Pawn; pieceType <= King; pieceType++) { - Bitboard pieceBitboard = board.pieces[pieceType]; - - if (pieceBitboard.IsSet(square)) { - if (board.colors[White] & board.pieces[pieceType]) { - pieceCode = pieceType; // White: 1-6 - } else { - pieceCode = pieceType + 6; // Black: 7-12 - } - - if (pieceType == Rook) { - if ((board.colors[White] & board.pieces[Rook]) && (board.castlingRights & (whiteKingRight | whiteQueenRight))) { - pieceCode |= UNMOVED_ROOK; - } - if ((board.colors[Black] & board.pieces[Rook]) && (board.castlingRights & (blackKingRight | blackQueenRight))) { - pieceCode |= UNMOVED_ROOK; - } - } - break; - } + if (pieceType == Rook) { + if ((square == a1 && (board.castlingRights & whiteQueenRight)) + || (square == h1 && (board.castlingRights & whiteKingRight)) + || (square == a8 && (board.castlingRights & blackQueenRight)) + || (square == h8 && (board.castlingRights & blackKingRight))) + pieceCode = UNMOVED_ROOK; } - int byteIndex = square / 4; - int shift = (square % 4) * 4; + pieceCode |= (board.sideToMove == Black ? (1 << 3) : 0); - compressed[byteIndex] |= (pieceCode & 0x0F) << shift; + int byteIndex = index / 2; + int bitShift = (index % 2) * 4; + + compressed[byteIndex] |= (pieceCode & 0x0F) << bitShift; + + index++; + occupancy.PopBit(square); } return compressed; -- 2.47.3 From 4889715e47130eea364a6197d97d1917a8960aec Mon Sep 17 00:00:00 2001 From: rektdie Date: Wed, 2 Apr 2025 06:23:03 +0200 Subject: [PATCH 22/27] fixed marlinformat --- source/utils.cpp | 9 +++++---- source/utils.h | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/source/utils.cpp b/source/utils.cpp index 5de5d36..b9bd037 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -147,15 +147,17 @@ Move parseMove(Board &board, std::string_view str) { return Move(from, to, flag); } -std::array CompressPieces(Board &board) { +std::array CompressPieces(Board board) { const uint8_t UNMOVED_ROOK = 6; std::array compressed{}; + Bitboard occupancy = board.occupied; int index = 0; while (occupancy) { int square = occupancy.getLS1BIndex(); int pieceType = board.GetPieceType(square); + int pieceColor = board.GetPieceColor(square); int pieceCode = pieceType; if (pieceType == Rook) { @@ -166,7 +168,7 @@ std::array CompressPieces(Board &board) { pieceCode = UNMOVED_ROOK; } - pieceCode |= (board.sideToMove == Black ? (1 << 3) : 0); + pieceCode |= (pieceColor << 3); // Shift the color to the 3rd bit (bit 3) int byteIndex = index / 2; int bitShift = (index % 2) * 4; @@ -179,5 +181,4 @@ std::array CompressPieces(Board &board) { return compressed; } - -} +} \ No newline at end of file diff --git a/source/utils.h b/source/utils.h index fbd09b1..61b5ebc 100644 --- a/source/utils.h +++ b/source/utils.h @@ -30,6 +30,6 @@ int parseSquare(std::string_view str); Move parseMove(Board &board, std::string_view str); -std::array CompressPieces(Board &board); +std::array CompressPieces(Board board); } -- 2.47.3 From 5ad924f3c64a19e04a894cb75535641042704fd8 Mon Sep 17 00:00:00 2001 From: rektdie Date: Wed, 2 Apr 2025 07:25:22 +0200 Subject: [PATCH 23/27] tried to implement viri move format --- source/datagen.cpp | 2 +- source/datagen.h | 2 +- source/move.h | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/source/datagen.cpp b/source/datagen.cpp index 8e7f7be..c2702cd 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -259,7 +259,7 @@ void Run(int targetPositions, int threads) { if (!results.bestMove) break; safeResults = results; - game.moves.push_back(ScoredMove(results.bestMove, results.score)); + game.moves.push_back(ScoredMove(results.bestMove.ConvertToViriMoveFormat(), results.score)); board.MakeMove(results.bestMove); MOVEGEN::GenerateMoves(board); diff --git a/source/datagen.h b/source/datagen.h index 684f71e..28003ea 100644 --- a/source/datagen.h +++ b/source/datagen.h @@ -59,7 +59,7 @@ struct ScoredMove { ScoredMove() : move(0), score(0) {} - ScoredMove(Move m, int s) { + ScoredMove(uint16_t m, int16_t s) { move = m; score = s; } diff --git a/source/move.h b/source/move.h index 6a0e7aa..f66ee2b 100644 --- a/source/move.h +++ b/source/move.h @@ -76,6 +76,48 @@ public: } } + uint16_t ConvertToViriMoveFormat() { + uint16_t viriMove = 0; + int flags = GetFlags(); + + // Set from and to squares (6 bits each) + viriMove |= MoveFrom(); + viriMove |= MoveTo() << 6; + + // Determine move type (bits 12-13) + uint16_t moveType = 0; + uint16_t promoPiece = 0; + + if (flags == kingCastle || flags == queenCastle) { + moveType = 1; // Castling + } + else if (flags == epCapture) { + moveType = 2; // En passant + } + else if (flags >= knightPromotion && flags <= queenPromoCapture) { + moveType = 3; // Promotion + // Set promo piece (bits 14-15) + if (flags == knightPromotion || flags == knightPromoCapture) { + promoPiece = 0; // Knight + } + else if (flags == bishopPromotion || flags == bishopPromoCapture) { + promoPiece = 1; // Bishop + } + else if (flags == rookPromotion || flags == rookPromoCapture) { + promoPiece = 2; // Rook + } + else if (flags == queenPromotion || flags == queenPromoCapture) { + promoPiece = 3; // Queen + } + } + // else moveType remains 0 (normal move) + + viriMove |= moveType << 12; + viriMove |= promoPiece << 14; + + return viriMove; + } + operator uint16_t() { return m_move; } -- 2.47.3 From 34f5bbdd43002d299d4729673c4e7dbe40de3a40 Mon Sep 17 00:00:00 2001 From: rektdie Date: Wed, 2 Apr 2025 08:27:46 +0200 Subject: [PATCH 24/27] fixed viri move format --- source/move.h | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/source/move.h b/source/move.h index f66ee2b..ac7b9b7 100644 --- a/source/move.h +++ b/source/move.h @@ -79,15 +79,21 @@ public: uint16_t ConvertToViriMoveFormat() { uint16_t viriMove = 0; int flags = GetFlags(); - - // Set from and to squares (6 bits each) - viriMove |= MoveFrom(); - viriMove |= MoveTo() << 6; - + + // Define masks for different parts of the move + const uint16_t FROM_MASK = 0x3F; // 6 bits for the 'from' square (0-63) + const uint16_t TO_MASK = 0x3F; // 6 bits for the 'to' square (0-63) + const uint16_t MOVE_TYPE_MASK = 0x3 << 12; // 2 bits for the move type (castling, en passant, promotion, etc.) + const uint16_t PROMO_PIECE_MASK = 0x3 << 14; // 2 bits for the promotion piece (knight, bishop, etc.) + + // Set 'from' and 'to' squares + viriMove |= (MoveFrom() & FROM_MASK); // Mask the 'from' square + viriMove |= (MoveTo() & TO_MASK) << 6; // Mask the 'to' square + // Determine move type (bits 12-13) uint16_t moveType = 0; uint16_t promoPiece = 0; - + if (flags == kingCastle || flags == queenCastle) { moveType = 1; // Castling } @@ -98,23 +104,25 @@ public: moveType = 3; // Promotion // Set promo piece (bits 14-15) if (flags == knightPromotion || flags == knightPromoCapture) { - promoPiece = 0; // Knight + promoPiece = 1; // Knight } else if (flags == bishopPromotion || flags == bishopPromoCapture) { - promoPiece = 1; // Bishop + promoPiece = 2; // Bishop } else if (flags == rookPromotion || flags == rookPromoCapture) { - promoPiece = 2; // Rook + promoPiece = 3; // Rook } else if (flags == queenPromotion || flags == queenPromoCapture) { - promoPiece = 3; // Queen + promoPiece = 4; // Queen } } - // else moveType remains 0 (normal move) - - viriMove |= moveType << 12; - viriMove |= promoPiece << 14; - + + // Apply move type (bits 12-13) using mask + viriMove |= (moveType & MOVE_TYPE_MASK); + + // Apply promotion piece (bits 14-15) using mask + viriMove |= (promoPiece & PROMO_PIECE_MASK); + return viriMove; } -- 2.47.3 From c0b76d81bbeaa0db03fa76db9e9888c87a29363d Mon Sep 17 00:00:00 2001 From: rektdie Date: Thu, 3 Apr 2025 03:15:45 +0200 Subject: [PATCH 25/27] separated playing games from run function --- source/board.h | 4 ++-- source/datagen.cpp | 57 +++++++++++++++++++++++++++------------------- source/search.cpp | 14 ++++++------ source/search.h | 12 +++++----- source/tt.h | 4 ++-- 5 files changed, 50 insertions(+), 41 deletions(-) diff --git a/source/board.h b/source/board.h index 4223eca..9b5f369 100644 --- a/source/board.h +++ b/source/board.h @@ -9,8 +9,8 @@ constexpr int noEPTarget = -1; constexpr int MAX_MOVES = 218; -inline int positionIndex = 0; -inline U64 positionHistory[1000]; +thread_local inline int positionIndex = 0; +thread_local inline U64 positionHistory[1000]; class Board { public: diff --git a/source/datagen.cpp b/source/datagen.cpp index c2702cd..e5265bc 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "datagen.h" #include "movegen.h" #include "evaluate.h" @@ -220,23 +221,8 @@ void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int std::cout << std::string(width, '-') << std::endl; } -void Run(int targetPositions, int threads) { - #ifdef _WIN32 - system("cls"); - HideCursor(); - #else - std::cout << "\033[2J\033[H"; - HideCursorLinux(); - #endif - - Stopwatch stopwatch; - stopwatch.Restart(); - - int positions = 0; - int lastPrintedPosition = 0; - double lastPrintedTime = stopwatch.GetElapsedSec(); - - while (positions < targetPositions * 1000) { +void PlayGame(std::atomic& positions, int targetPositions) { + while (positions.load(std::memory_order_relaxed) < targetPositions) { Game game; Board board; @@ -264,18 +250,41 @@ void Run(int targetPositions, int threads) { MOVEGEN::GenerateMoves(board); // Increment positions, but do not exceed target - if (positions < targetPositions * 1000) { - positions++; + if (positions.load(std::memory_order_relaxed) < targetPositions) { + positions.fetch_add(1, std::memory_order_relaxed); + + if (std::abs(safeResults.score) >= std::abs(SEARCH::MATE_SCORE) - SEARCH::MAX_PLY) { + wdl = board.sideToMove ? 2 : 0; + } + + } } - - if (std::abs(safeResults.score) >= std::abs(SEARCH::MATE_SCORE) - SEARCH::MAX_PLY) { - wdl = board.sideToMove ? 2 : 0; - } - game.format.packFrom(board, staticEval, wdl); WriteToFile(game, "data.binpack"); + } +} +void Run(int targetPositions, int threads) { + #ifdef _WIN32 + system("cls"); + HideCursor(); + #else + std::cout << "\033[2J\033[H"; + HideCursorLinux(); + #endif + + Stopwatch stopwatch; + + std::atomic positions = 0; + + auto worker = std::thread(PlayGame, std::ref(positions), targetPositions * 1000); + worker.detach(); + + int lastPrintedPosition = 0; + double lastPrintedTime = stopwatch.GetElapsedSec(); + + while (positions.load(std::memory_order_relaxed) < targetPositions * 1000) { // Print progress every second double currentTime = stopwatch.GetElapsedSec(); if (currentTime - lastPrintedTime >= 1) { diff --git a/source/search.cpp b/source/search.cpp index 9ab0558..4784f86 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -10,16 +10,16 @@ namespace SEARCH { -U64 nodes = 0; -U64 nodesToGo = 0; -bool benchStarted = false; +thread_local U64 nodes = 0; +thread_local U64 nodesToGo = 0; +thread_local bool benchStarted = false; -static inline int timeToSearch = 0; -static inline bool doingNullMove = false; +thread_local static inline int timeToSearch = 0; +thread_local static inline bool doingNullMove = false; -inline PVLine pvLine; +thread_local inline PVLine pvLine; -Stopwatch sw; +thread_local Stopwatch sw; void InitLMRTable() { for (int depth = 0; depth <= MAX_DEPTH; depth++) { diff --git a/source/search.h b/source/search.h index 485070f..71336a4 100644 --- a/source/search.h +++ b/source/search.h @@ -19,15 +19,15 @@ constexpr int MAX_DEPTH = 64; constexpr int MAX_PLY = 128; constexpr int MAX_HISTORY = 16384; -extern U64 nodes; -extern bool benchStarted; +thread_local extern U64 nodes; +thread_local extern bool benchStarted; constexpr int inf = 100000; -inline bool searchStopped = false; +thread_local inline bool searchStopped = false; -// [id][ply] -inline int killerMoves[2][MAX_PLY]; +// [id][ply] +thread_local inline int killerMoves[2][MAX_PLY]; inline int lmrTable[MAX_DEPTH+1][MAX_MOVES]; @@ -75,7 +75,7 @@ public: } }; -inline History history; +thread_local inline History history; class PVLine { private: diff --git a/source/tt.h b/source/tt.h index e8607c4..a6e4136 100644 --- a/source/tt.h +++ b/source/tt.h @@ -21,7 +21,7 @@ constexpr U64 maxHash = (64* 1000000) / sizeof(TTEntry); // 5 MB constexpr U64 defaultHash = (5 * 1000000) / sizeof(TTEntry); -inline U64 hashSize = defaultHash; +thread_local inline U64 hashSize = defaultHash; constexpr int invalidEntry = 111111; @@ -61,4 +61,4 @@ public: void WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move besteMove); }; -inline TTable TT; +thread_local inline TTable TT; -- 2.47.3 From 5619ced265a522b6b480ec7f594875aa14b3ceb1 Mon Sep 17 00:00:00 2001 From: rektdie Date: Thu, 3 Apr 2025 03:42:33 +0200 Subject: [PATCH 26/27] added multithreading to datagen --- source/datagen.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/source/datagen.cpp b/source/datagen.cpp index e5265bc..c3334d3 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "datagen.h" #include "movegen.h" #include "evaluate.h" @@ -43,6 +44,8 @@ static void ShowCursorLinux() { namespace DATAGEN { +std::mutex mutex; + static void PlayRandMoves(Board &board) { std::random_device dev; std::mt19937 rng(dev()); @@ -125,6 +128,7 @@ std::istream &operator>>(std::istream &is, Game &game) { } static void WriteToFile(const Game &game, const std::string &filename) { + std::lock_guard lock(mutex); // Create a buffer to store the serialized data [prevents corruption] std::vector buffer; @@ -278,8 +282,10 @@ void Run(int targetPositions, int threads) { std::atomic positions = 0; - auto worker = std::thread(PlayGame, std::ref(positions), targetPositions * 1000); - worker.detach(); + for (int i = 0; i < threads; i++) { + auto worker = std::thread(PlayGame, std::ref(positions), targetPositions * 1000); + worker.detach(); + } int lastPrintedPosition = 0; double lastPrintedTime = stopwatch.GetElapsedSec(); -- 2.47.3 From 61a68e5bcf06034311e09142f32209e2a4499391 Mon Sep 17 00:00:00 2001 From: rektdie Date: Thu, 3 Apr 2025 03:53:22 +0200 Subject: [PATCH 27/27] printing in ID and storing scores in datagen in white relative format | bench 1857933 --- source/datagen.cpp | 4 ++-- source/search.cpp | 2 +- source/utils.cpp | 5 +++++ source/utils.h | 2 ++ 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/source/datagen.cpp b/source/datagen.cpp index c3334d3..0f300a0 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -249,7 +249,7 @@ void PlayGame(std::atomic& positions, int targetPositions) { if (!results.bestMove) break; safeResults = results; - game.moves.push_back(ScoredMove(results.bestMove.ConvertToViriMoveFormat(), results.score)); + game.moves.push_back(ScoredMove(results.bestMove.ConvertToViriMoveFormat(), UTILS::ConvertToWhiteRelative(board, results.score))); board.MakeMove(results.bestMove); MOVEGEN::GenerateMoves(board); @@ -264,7 +264,7 @@ void PlayGame(std::atomic& positions, int targetPositions) { } } - game.format.packFrom(board, staticEval, wdl); + game.format.packFrom(board, UTILS::ConvertToWhiteRelative(board, staticEval), wdl); WriteToFile(game, "data.binpack"); } } diff --git a/source/search.cpp b/source/search.cpp index 4784f86..0b76e9d 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -437,7 +437,7 @@ static SearchResults ID(Board &board, SearchParams params) { std::cout << "info "; std::cout << "depth " << depth; std::cout << " time " << elapsed; - std::cout << " score cp " << safeResults.score; + std::cout << " score cp " << UTILS::ConvertToWhiteRelative(board, safeResults.score); std::cout << " nodes " << nodes << " nps " << int(nodes/sw.GetElapsedSec()); std::cout << " hashfull " << TT.GetUsedPercentage(); std::cout << " pv "; diff --git a/source/utils.cpp b/source/utils.cpp index b9bd037..4a5bb6f 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -181,4 +181,9 @@ std::array CompressPieces(Board board) { return compressed; } + +int ConvertToWhiteRelative(Board &board, int score) { + return board.sideToMove == White ? score : -score; +} + } \ No newline at end of file diff --git a/source/utils.h b/source/utils.h index 61b5ebc..37aa02b 100644 --- a/source/utils.h +++ b/source/utils.h @@ -32,4 +32,6 @@ Move parseMove(Board &board, std::string_view str); std::array CompressPieces(Board board); +int ConvertToWhiteRelative(Board &board, int score); + } -- 2.47.3