Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3135f430ad | ||
|
|
c07858dab4 | ||
|
|
08ac91b842 | ||
|
|
9cadd512cf | ||
|
|
53cad5f0cb | ||
|
|
817d589042 | ||
|
|
ec46fddfe5 | ||
|
|
0fd1add2c4 | ||
|
|
d9f3d0f15f | ||
|
|
da248009a4 | ||
|
|
ff471034f7 | ||
|
|
462cb5b8ef | ||
|
|
82255ee46d | ||
|
|
760894dd1a | ||
|
|
311fc28c1c | ||
|
|
feb11b0b08 | ||
|
|
a4c1a87110 | ||
|
|
5bd44bfd48 | ||
|
|
20f1202c45 | ||
|
|
0e88db9b4c | ||
|
|
21148655dd | ||
|
|
092ca2a2d1 | ||
|
|
b74f6f4cbb | ||
|
|
f581d919b9 | ||
|
|
cdfb752bfa | ||
|
|
f0280a77ff | ||
|
|
994f39421d | ||
|
|
8e56bd786b | ||
|
|
b60860b82d | ||
|
|
720fd40176 | ||
|
|
e6318f6ff4 | ||
|
|
7887e04cc5 | ||
|
|
efad38fb0f | ||
|
|
dbeec1b9c6 | ||
|
|
68377cbf7b | ||
|
|
a077d9bd3e | ||
|
|
62b9e205fc | ||
|
|
eda2480d59 | ||
|
|
31149c7450 | ||
|
|
7dbde30f08 | ||
|
|
82a4fcdeaf | ||
|
|
96411d9c8f | ||
|
|
d85edd9faf | ||
|
|
dbde65f083 | ||
|
|
e435ae0818 | ||
|
|
f666d1ea05 | ||
|
|
1832d460c5 | ||
|
|
43c59f29f9 | ||
|
|
20c636a3b4 | ||
|
|
33bafd461a | ||
|
|
dab5bad089 | ||
|
|
2ab4868c75 | ||
|
|
4fe645ff1b | ||
|
|
2181d446bf | ||
|
|
db565c6100 | ||
|
|
d7c4383783 | ||
|
|
c58d29ac61 | ||
|
|
84f2fd0ea8 | ||
|
|
64a85bf780 | ||
|
|
8d6314aef3 | ||
|
|
9979441138 | ||
|
|
0e48eefc72 | ||
|
|
5e4b1faa0a | ||
|
|
c890b04a47 | ||
|
|
bec0f3275f | ||
|
|
4564d10bcf | ||
|
|
dec219e4d7 | ||
|
|
b1fd8e6021 | ||
|
|
5a45d29f66 |
No files matched your search
@@ -1,42 +1,70 @@
|
|||||||
# Define directories
|
# Define directories
|
||||||
SRC_DIR := source
|
SRC_DIR := source
|
||||||
OBJ_DIR := obj
|
OBJ_DIR := obj
|
||||||
|
FMT_DIR := external/fmt
|
||||||
|
|
||||||
CXX := clang++
|
CXX := clang++
|
||||||
EXE ?= Eleanor
|
EXE ?= Eleanor
|
||||||
EVALFILE := ./nnue.bin
|
EVALFILE := ./nnue.bin
|
||||||
|
|
||||||
ifeq ($(OS),Windows_NT)
|
ifeq ($(OS),Windows_NT)
|
||||||
|
UNAME_S := Windows
|
||||||
EXE_EXT := .exe
|
EXE_EXT := .exe
|
||||||
MKDIR := mkdir
|
MKDIR := mkdir
|
||||||
RM := rmdir /s /q
|
RM := rmdir /s /q
|
||||||
DEL := del /q
|
DEL := del /q
|
||||||
|
PLATFORM := windows
|
||||||
else
|
else
|
||||||
|
UNAME_S := $(shell uname -s)
|
||||||
|
ARCH := $(shell uname -m)
|
||||||
EXE_EXT :=
|
EXE_EXT :=
|
||||||
MKDIR := mkdir -p
|
MKDIR := mkdir -p
|
||||||
RM := rm -rf
|
RM := rm -rf
|
||||||
DEL := rm -f
|
DEL := rm -f
|
||||||
|
ifeq ($(UNAME_S),Darwin)
|
||||||
|
PLATFORM := mac
|
||||||
|
else
|
||||||
|
PLATFORM := unix
|
||||||
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
CXXFLAGS := -std=c++20 -O3 -flto -Wall
|
||||||
|
LDFLAGS := -O3 -flto
|
||||||
|
|
||||||
|
ifeq ($(PLATFORM),mac)
|
||||||
|
ifeq ($(ARCH),arm64)
|
||||||
|
ARCH_FLAGS :=
|
||||||
|
else
|
||||||
|
ARCH_FLAGS := -march=native
|
||||||
|
endif
|
||||||
|
else
|
||||||
|
ARCH_FLAGS := -march=native
|
||||||
|
LDFLAGS += -fuse-ld=lld -static
|
||||||
|
endif
|
||||||
|
|
||||||
|
CXXFLAGS += $(ARCH_FLAGS)
|
||||||
|
LDFLAGS += $(ARCH_FLAGS)
|
||||||
|
|
||||||
MAKEFLAGS += -j
|
MAKEFLAGS += -j
|
||||||
|
|
||||||
# Source and object files
|
|
||||||
SRCS := $(wildcard $(SRC_DIR)/*.cpp)
|
SRCS := $(wildcard $(SRC_DIR)/*.cpp)
|
||||||
OBJS := $(patsubst $(SRC_DIR)/%.cpp, $(OBJ_DIR)/%.o, $(SRCS))
|
OBJS := $(patsubst $(SRC_DIR)/%.cpp, $(OBJ_DIR)/%.o, $(SRCS))
|
||||||
|
|
||||||
# Final binary
|
FMT_SRC := $(FMT_DIR)/format.cc
|
||||||
$(EXE)$(EXE_EXT): $(OBJS)
|
FMT_OBJ := $(OBJ_DIR)/format.o
|
||||||
$(CXX) -DEVALFILE=\"$(EVALFILE)\" $^ ./external/fmt/format.cc -o $@ -O3 -fuse-ld=lld -march=native -flto -static
|
|
||||||
|
$(EXE)$(EXE_EXT): $(OBJS) $(FMT_OBJ)
|
||||||
|
$(CXX) $(CXXFLAGS) -DEVALFILE=\"$(EVALFILE)\" $^ -o $@ $(LDFLAGS)
|
||||||
|
|
||||||
# Compile .cpp -> .o
|
|
||||||
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp | $(OBJ_DIR)
|
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp | $(OBJ_DIR)
|
||||||
$(CXX) -std=c++20 -c -o $@ $< -O3 -march=native -flto -static
|
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
||||||
|
|
||||||
|
$(FMT_OBJ): $(FMT_SRC) | $(OBJ_DIR)
|
||||||
|
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
||||||
|
|
||||||
# Create object directory
|
|
||||||
$(OBJ_DIR):
|
$(OBJ_DIR):
|
||||||
$(MKDIR) $(OBJ_DIR)
|
$(MKDIR) $(OBJ_DIR)
|
||||||
|
|
||||||
# Clean up and rebuild
|
|
||||||
.PHONY: clean
|
.PHONY: clean
|
||||||
clean:
|
clean:
|
||||||
ifeq ($(OS),Windows_NT)
|
ifeq ($(OS),Windows_NT)
|
||||||
|
|||||||
Vendored
+23
@@ -0,0 +1,23 @@
|
|||||||
|
// Stolen from Ciekce - Stormphrax
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
namespace internal
|
||||||
|
{
|
||||||
|
template <typename T, size_t N, size_t ... Ns>
|
||||||
|
struct MultiArrayImpl
|
||||||
|
{
|
||||||
|
using Type = std::array<typename MultiArrayImpl<T, Ns...>::Type, N>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T, size_t N>
|
||||||
|
struct MultiArrayImpl<T, N>
|
||||||
|
{
|
||||||
|
using Type = std::array<T, N>;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, size_t ... Ns>
|
||||||
|
using MultiArray = typename internal::MultiArrayImpl<T, Ns...>::Type;
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <memory>
|
||||||
#include "benchmark.h"
|
#include "benchmark.h"
|
||||||
#include "search.h"
|
#include "search.h"
|
||||||
#include "stopwatch.h"
|
#include "stopwatch.h"
|
||||||
@@ -62,15 +63,14 @@ constexpr std::array<std::string_view, 50> fenPositions = {
|
|||||||
|
|
||||||
void RunBenchmark() {
|
void RunBenchmark() {
|
||||||
Board board;
|
Board board;
|
||||||
int depth = BENCH_DEPTH;
|
|
||||||
|
|
||||||
SearchContext ctx;
|
auto ctx = std::make_unique<SearchContext>();
|
||||||
|
|
||||||
Stopwatch sw;
|
Stopwatch sw;
|
||||||
for (int i = 0; i < fenPositions.size(); i++) {
|
for (int i = 0; i < fenPositions.size(); i++) {
|
||||||
board.SetByFen(fenPositions[i]);
|
board.SetByFen(fenPositions[i]);
|
||||||
SearchPosition<bench>(board, SearchParams(), ctx);
|
SearchPosition<bench>(board, SearchParams(), ctx.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << ctx.nodes << " nodes " << int(ctx.nodes/sw.GetElapsedSec()) << " nps" << std::endl;
|
std::cout << ctx->nodes << " nodes " << int(ctx->nodes/sw.GetElapsedSec()) << " nps" << std::endl;
|
||||||
}
|
}
|
||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
constexpr int BENCH_DEPTH = 11;
|
constexpr int BENCH_DEPTH = 13;
|
||||||
|
|
||||||
void RunBenchmark();
|
void RunBenchmark();
|
||||||
+119
-30
@@ -2,6 +2,7 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
#include <tuple>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include "datagen.h"
|
#include "datagen.h"
|
||||||
@@ -10,8 +11,11 @@
|
|||||||
#include "movegen.h"
|
#include "movegen.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
|
// OS-dependent threading includes
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
#else
|
||||||
|
#include <pthread.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
@@ -30,21 +34,21 @@ static void ShowCursor() {
|
|||||||
cursorInfo.bVisible = TRUE;
|
cursorInfo.bVisible = TRUE;
|
||||||
SetConsoleCursorInfo(hConsole, &cursorInfo);
|
SetConsoleCursorInfo(hConsole, &cursorInfo);
|
||||||
}
|
}
|
||||||
#endif
|
#else
|
||||||
|
static void HideCursor() {
|
||||||
static void HideCursorLinux() {
|
|
||||||
std::cout << "\033[?25l";
|
std::cout << "\033[?25l";
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ShowCursorLinux() {
|
static void ShowCursor() {
|
||||||
std::cout << "\033[?25h";
|
std::cout << "\033[?25h";
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace DATAGEN {
|
namespace DATAGEN {
|
||||||
|
|
||||||
static std::mutex fileOpenMutex;
|
static std::mutex fileOpenMutex;
|
||||||
|
|
||||||
static bool IsGameOver(Board &board, SEARCH::SearchContext& ctx) {
|
static bool IsGameOver(Board &board, SEARCH::SearchContext* ctx) {
|
||||||
if (SEARCH::IsDraw(board, ctx)) return true;
|
if (SEARCH::IsDraw(board, ctx)) return true;
|
||||||
|
|
||||||
int moveSeen = 0;
|
int moveSeen = 0;
|
||||||
@@ -60,7 +64,7 @@ static bool IsGameOver(Board &board, SEARCH::SearchContext& ctx) {
|
|||||||
return moveSeen == 0;
|
return moveSeen == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void PlayRandMoves(Board &board, SEARCH::SearchContext& ctx) {
|
static void PlayRandMoves(Board &board, SEARCH::SearchContext* ctx) {
|
||||||
std::random_device dev;
|
std::random_device dev;
|
||||||
std::mt19937_64 rng(dev());
|
std::mt19937_64 rng(dev());
|
||||||
|
|
||||||
@@ -77,7 +81,7 @@ static void PlayRandMoves(Board &board, SEARCH::SearchContext& ctx) {
|
|||||||
count--;
|
count--;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
ctx.positionHistory[board.positionIndex] = board.hashKey;
|
ctx->positionHistory[board.positionIndex] = board.hashKey;
|
||||||
MOVEGEN::GenerateMoves<All>(board);
|
MOVEGEN::GenerateMoves<All>(board);
|
||||||
if (IsGameOver(board, ctx)) break;
|
if (IsGameOver(board, ctx)) break;
|
||||||
}
|
}
|
||||||
@@ -127,13 +131,32 @@ static void PlayGames(int id, std::atomic<int>& positions, std::atomic<bool>& st
|
|||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
while (!stopFlag) {
|
const int evenityMargin = 200;
|
||||||
Game game;
|
|
||||||
Board board;
|
|
||||||
SEARCH::SearchContext ctx;
|
|
||||||
PlayRandMoves(board, ctx);
|
|
||||||
if (IsGameOver(board, ctx)) continue;
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
const int winAdjMoveCount = 3;
|
||||||
|
const int winAdjScore = 400;
|
||||||
|
|
||||||
|
const int drawAdjMoveNumber = 40;
|
||||||
|
const int drawAdjMoveCount = 8;
|
||||||
|
const int drawAdjScore = 10;
|
||||||
|
*/
|
||||||
|
|
||||||
|
while (!stopFlag) {
|
||||||
|
Board board;
|
||||||
|
auto ctx = std::make_unique<SEARCH::SearchContext>();
|
||||||
|
|
||||||
|
PlayRandMoves(board, ctx.get());
|
||||||
|
if (IsGameOver(board, ctx.get())) continue;
|
||||||
|
|
||||||
|
// Filter out games with a highly uneven starting position
|
||||||
|
const int startingScore = SEARCH::SearchPosition<SEARCH::datagen>(board, SearchParams(), ctx.get()).score;
|
||||||
|
|
||||||
|
if (std::abs(startingScore) >= evenityMargin) continue;
|
||||||
|
|
||||||
|
ctx->nodes = 0;
|
||||||
|
|
||||||
|
Game game;
|
||||||
Board startpos = board;
|
Board startpos = board;
|
||||||
|
|
||||||
SearchResults safeResults;
|
SearchResults safeResults;
|
||||||
@@ -141,8 +164,8 @@ static void PlayGames(int id, std::atomic<int>& positions, std::atomic<bool>& st
|
|||||||
int staticEval = UTILS::ConvertToWhiteRelative(board, NNUE::net.Evaluate(board));
|
int staticEval = UTILS::ConvertToWhiteRelative(board, NNUE::net.Evaluate(board));
|
||||||
int wdl = 1;
|
int wdl = 1;
|
||||||
|
|
||||||
while (!IsGameOver(board, ctx)) {
|
while (!IsGameOver(board, ctx.get())) {
|
||||||
SearchResults results = SEARCH::SearchPosition<SEARCH::datagen>(board, SearchParams(), ctx);
|
SearchResults results = SEARCH::SearchPosition<SEARCH::datagen>(board, SearchParams(), ctx.get());
|
||||||
|
|
||||||
if (!results.bestMove) break;
|
if (!results.bestMove) break;
|
||||||
safeResults = results;
|
safeResults = results;
|
||||||
@@ -151,14 +174,15 @@ static void PlayGames(int id, std::atomic<int>& positions, std::atomic<bool>& st
|
|||||||
UTILS::ConvertToWhiteRelative(board, results.score)));
|
UTILS::ConvertToWhiteRelative(board, results.score)));
|
||||||
|
|
||||||
board.MakeMove(results.bestMove);
|
board.MakeMove(results.bestMove);
|
||||||
ctx.positionHistory[board.positionIndex] = board.hashKey;
|
ctx->positionHistory[board.positionIndex] = board.hashKey;
|
||||||
|
|
||||||
positions++;
|
positions++;
|
||||||
|
|
||||||
|
|
||||||
MOVEGEN::GenerateMoves<All>(board);
|
MOVEGEN::GenerateMoves<All>(board);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!SEARCH::IsDraw(board, ctx)) {
|
if (!SEARCH::IsDraw(board, ctx.get())) {
|
||||||
wdl = board.sideToMove ? 2 : 0;
|
wdl = board.sideToMove ? 2 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -178,24 +202,62 @@ static void PlayGames(int id, std::atomic<int>& positions, std::atomic<bool>& st
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
// Windows implementation using std::thread
|
||||||
|
static void CreateWorkerThread(int id, std::atomic<int>& positions, std::atomic<bool>& stopFlag, std::vector<std::thread>& threads) {
|
||||||
|
threads.emplace_back(PlayGames, id, std::ref(positions), std::ref(stopFlag));
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
// Unix/Linux implementation using pthread
|
||||||
|
static void* ThreadFunc(void* arg) {
|
||||||
|
auto* tup = static_cast<std::tuple<int, std::atomic<int>*, std::atomic<bool>*>*>(arg);
|
||||||
|
PlayGames(std::get<0>(*tup), *std::get<1>(*tup), *std::get<2>(*tup));
|
||||||
|
delete tup;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
void Run(int targetPositions, int threads) {
|
void Run(int targetPositions, int threads) {
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
system("cls");
|
system("cls");
|
||||||
HideCursor();
|
HideCursor();
|
||||||
#else
|
#else
|
||||||
std::cout << "\033[2J\033[H";
|
std::cout << "\033[2J\033[H";
|
||||||
HideCursorLinux();
|
HideCursor();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
std::atomic<int> positions = 0;
|
std::atomic<int> positions = 0;
|
||||||
std::atomic<bool> stopFlag = false;
|
std::atomic<bool> stopFlag = false;
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
// Windows implementation using std::thread
|
||||||
std::vector<std::thread> workerThreads;
|
std::vector<std::thread> workerThreads;
|
||||||
|
|
||||||
// Start N-1 threads
|
for (int i = 0; i < threads - 1; ++i) {
|
||||||
for (int i = 0; i < threads-1; i++) {
|
CreateWorkerThread(i, positions, stopFlag, workerThreads);
|
||||||
workerThreads.emplace_back(std::thread(PlayGames, i, std::ref(positions), std::ref(stopFlag)));
|
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
// Unix/Linux implementation using pthread
|
||||||
|
std::vector<pthread_t> workerThreads;
|
||||||
|
|
||||||
|
for (int i = 0; i < threads - 1; ++i) {
|
||||||
|
pthread_attr_t attr;
|
||||||
|
pthread_attr_init(&attr);
|
||||||
|
pthread_attr_setstacksize(&attr, 8 * 1024 * 1024); // 8 MB stack
|
||||||
|
|
||||||
|
auto* args = new std::tuple<int, std::atomic<int>*, std::atomic<bool>*>(i, &positions, &stopFlag);
|
||||||
|
|
||||||
|
pthread_t thread;
|
||||||
|
if (pthread_create(&thread, &attr, ThreadFunc, args) == 0) {
|
||||||
|
workerThreads.push_back(thread);
|
||||||
|
} else {
|
||||||
|
std::cerr << "Failed to create thread " << i << "\n";
|
||||||
|
delete args;
|
||||||
|
}
|
||||||
|
|
||||||
|
pthread_attr_destroy(&attr);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
Stopwatch sw;
|
Stopwatch sw;
|
||||||
while (positions < targetPositions) {
|
while (positions < targetPositions) {
|
||||||
@@ -203,22 +265,28 @@ void Run(int targetPositions, int threads) {
|
|||||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
PrintProgress(positions, targetPositions, sw, threads);
|
PrintProgress(positions, targetPositions, sw, threads);
|
||||||
|
|
||||||
stopFlag = true;
|
stopFlag = true;
|
||||||
|
|
||||||
for (auto& thread : workerThreads) {
|
#ifdef _WIN32
|
||||||
|
// Join Windows threads
|
||||||
|
for (std::thread& thread : workerThreads) {
|
||||||
if (thread.joinable()) {
|
if (thread.joinable()) {
|
||||||
thread.join();
|
thread.join();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
// Join pthread threads
|
||||||
|
for (pthread_t& thread : workerThreads) {
|
||||||
|
pthread_join(thread, nullptr);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
ShowCursor();
|
ShowCursor();
|
||||||
#else
|
#else
|
||||||
ShowCursorLinux();
|
ShowCursor();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -243,9 +311,6 @@ void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int
|
|||||||
lastElapsed = elapsed;
|
lastElapsed = elapsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
int positionsInThousands = static_cast<int>(round(positions / 1000.0));
|
|
||||||
int targetInThousands = static_cast<int>(round(targetPositions / 1000.0));
|
|
||||||
|
|
||||||
std::cout << "\033[H";
|
std::cout << "\033[H";
|
||||||
|
|
||||||
int width = 60;
|
int width = 60;
|
||||||
@@ -285,6 +350,31 @@ void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int
|
|||||||
|
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
double remainingTime = 0.0;
|
||||||
|
if (positions > 0 && elapsed > 0.0) {
|
||||||
|
double positionsPerSec = positions / elapsed;
|
||||||
|
remainingTime = (targetPositions - positions) / positionsPerSec;
|
||||||
|
}
|
||||||
|
|
||||||
|
remainingTime = (std::max)(0.0, remainingTime);
|
||||||
|
|
||||||
|
int remainingHours = static_cast<int>(remainingTime) / 3600;
|
||||||
|
int remainingMinutes = (static_cast<int>(remainingTime) % 3600) / 60;
|
||||||
|
int remainingSeconds = static_cast<int>(remainingTime) % 60;
|
||||||
|
|
||||||
|
std::ostringstream remainingTimeStream;
|
||||||
|
remainingTimeStream << "Estimated Time Left: "
|
||||||
|
<< std::setfill('0') << std::setw(2) << remainingHours << "h "
|
||||||
|
<< std::setw(2) << remainingMinutes << "m "
|
||||||
|
<< std::setw(2) << remainingSeconds << "s";
|
||||||
|
|
||||||
|
std::string remainingTimeText = remainingTimeStream.str();
|
||||||
|
int remainingPadding = (width - remainingTimeText.length()) / 2;
|
||||||
|
std::cout << COLOR_SECTION << std::setw(remainingPadding + remainingTimeText.length())
|
||||||
|
<< remainingTimeText << COLOR_RESET << std::endl;
|
||||||
|
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
int barWidth = 50;
|
int barWidth = 50;
|
||||||
double progress = (double)positions / targetPositions;
|
double progress = (double)positions / targetPositions;
|
||||||
int pos = (int)(progress * barWidth);
|
int pos = (int)(progress * barWidth);
|
||||||
@@ -309,5 +399,4 @@ void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int
|
|||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -299,120 +299,6 @@ Bitboard getPawnPushes(int square, bool color, Bitboard &occupancy) {
|
|||||||
return pushes;
|
return pushes;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the direction to target from square
|
|
||||||
static int GetDirection(int square, int target) {
|
|
||||||
int rankDiff = (target / 8) - (square / 8);
|
|
||||||
int fileDiff = (target % 8) - (square % 8);
|
|
||||||
|
|
||||||
// Main directions
|
|
||||||
if (rankDiff == 0 && fileDiff < 0) {
|
|
||||||
return west;
|
|
||||||
} else if (rankDiff == 0 && fileDiff > 0) {
|
|
||||||
return east;
|
|
||||||
} else if (rankDiff < 0 && fileDiff == 0) {
|
|
||||||
return south;
|
|
||||||
} else if (rankDiff > 0 && fileDiff == 0) {
|
|
||||||
return north;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Diagonals
|
|
||||||
if (abs(rankDiff) == abs(fileDiff)) {
|
|
||||||
if (rankDiff < 0 && fileDiff < 0) {
|
|
||||||
return soWe;
|
|
||||||
} else if (rankDiff < 0 && fileDiff > 0) {
|
|
||||||
return soEa;
|
|
||||||
} else if (rankDiff > 0 && fileDiff < 0) {
|
|
||||||
return noWe;
|
|
||||||
} else if (rankDiff > 0 && fileDiff > 0) {
|
|
||||||
return noEa;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns a bitboard of the squares from attacker to target
|
|
||||||
static Bitboard SliderRayToSquare(int attacker, int target) {
|
|
||||||
Bitboard ray;
|
|
||||||
int direction = GetDirection(attacker, target);
|
|
||||||
|
|
||||||
if (direction) {
|
|
||||||
while (attacker != target) {
|
|
||||||
ray.SetBit(attacker);
|
|
||||||
attacker += direction;
|
|
||||||
}
|
|
||||||
ray.SetBit(target);
|
|
||||||
}
|
|
||||||
|
|
||||||
return ray;
|
|
||||||
}
|
|
||||||
|
|
||||||
static Bitboard GetPinningRay(Board &board, int square, int direction) {
|
|
||||||
Bitboard ray;
|
|
||||||
int currentSquare = (board.pieces[King] & board.colors[board.sideToMove]).getLS1BIndex();
|
|
||||||
int previousRank = currentSquare / 8;
|
|
||||||
|
|
||||||
currentSquare += direction;
|
|
||||||
while (currentSquare < 64 && currentSquare >= 0) {
|
|
||||||
int currentRank = currentSquare / 8;
|
|
||||||
|
|
||||||
int difference = abs(currentRank - previousRank);
|
|
||||||
|
|
||||||
if ((abs(direction) == 7 || abs(direction) == 9)
|
|
||||||
&& difference != 1) {
|
|
||||||
break;
|
|
||||||
} else if (abs(direction) == 1 && difference) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
previousRank = currentRank;
|
|
||||||
|
|
||||||
ray.SetBit(currentSquare);
|
|
||||||
if (board.occupied.IsSet(currentSquare)) {
|
|
||||||
if (currentSquare == square) {
|
|
||||||
currentSquare += direction;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
int attackerType = board.GetPieceType(currentSquare);
|
|
||||||
int attackerColor = board.GetPieceColor(currentSquare);
|
|
||||||
|
|
||||||
if (attackerColor != board.sideToMove) {
|
|
||||||
// Pinned by rook or queen
|
|
||||||
if (abs(direction) == 1 || abs(direction) == 8) {
|
|
||||||
if (attackerType == Rook || attackerType == Queen) {
|
|
||||||
return ray;
|
|
||||||
}
|
|
||||||
// Pinned by bishop or queen
|
|
||||||
} else if (direction % 2 != 0) {
|
|
||||||
if (attackerType == Bishop || attackerType == Queen) {
|
|
||||||
return ray;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
currentSquare += direction;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0ULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool IsPinned(Board &board, int square) {
|
|
||||||
int kingSquare = (board.colors[board.sideToMove] & board.pieces[King]).getLS1BIndex();
|
|
||||||
int directionToKing = GetDirection(square, kingSquare);
|
|
||||||
|
|
||||||
if (directionToKing) {
|
|
||||||
// Checking if there are any pieces between square and kingSquare
|
|
||||||
if ((SliderRayToSquare(square, kingSquare)
|
|
||||||
& board.occupied).PopCount() != 2) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return GetPinningRay(board, square, -directionToKing).PopCount();
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void GenThreatMaps(Board &board) {
|
void GenThreatMaps(Board &board) {
|
||||||
for (int color = White; color <= Black; color++) {
|
for (int color = White; color <= Black; color++) {
|
||||||
board.colorThreats[color] = 0ULL;
|
board.colorThreats[color] = 0ULL;
|
||||||
|
|||||||
+42
-7
@@ -74,7 +74,6 @@ using nativeVector = __m256i;
|
|||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
#pragma message("Using SSE NNUE inference")
|
#pragma message("Using SSE NNUE inference")
|
||||||
// Assumes SSE support here
|
|
||||||
using nativeVector = __m128i;
|
using nativeVector = __m128i;
|
||||||
#define set1_epi16 _mm_set1_epi16
|
#define set1_epi16 _mm_set1_epi16
|
||||||
#define load_epi16 _mm_load_si128
|
#define load_epi16 _mm_load_si128
|
||||||
@@ -93,6 +92,47 @@ using nativeVector = __m128i;
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#elif defined(__aarch64__)
|
||||||
|
#pragma message("Using ARM64 (NEON) NNUE inference")
|
||||||
|
#include <arm_neon.h>
|
||||||
|
using nativeVector = int16x8_t;
|
||||||
|
static inline nativeVector set1_epi16(int16_t v) {
|
||||||
|
return vdupq_n_s16(v);
|
||||||
|
}
|
||||||
|
static inline nativeVector load_epi16(const nativeVector* ptr) {
|
||||||
|
return *ptr;
|
||||||
|
}
|
||||||
|
static inline nativeVector min_epi16(nativeVector a, nativeVector b) {
|
||||||
|
return vminq_s16(a, b);
|
||||||
|
}
|
||||||
|
static inline nativeVector max_epi16(nativeVector a, nativeVector b) {
|
||||||
|
return vmaxq_s16(a, b);
|
||||||
|
}
|
||||||
|
static inline int32x4_t madd_epi16(nativeVector a, nativeVector b) {
|
||||||
|
int32x4_t lo = vmull_s16(vget_low_s16(a), vget_low_s16(b));
|
||||||
|
int32x4_t hi = vmull_s16(vget_high_s16(a), vget_high_s16(b));
|
||||||
|
int32x4_t sum_lo = vpaddq_s32(lo, lo);
|
||||||
|
int32x4_t sum_hi = vpaddq_s32(hi, hi);
|
||||||
|
return vcombine_s32(vget_low_s32(sum_lo), vget_low_s32(sum_hi));
|
||||||
|
}
|
||||||
|
static inline nativeVector mullo_epi16(nativeVector a, nativeVector b) {
|
||||||
|
return vmulq_s16(a, b);
|
||||||
|
}
|
||||||
|
static inline int32x4_t add_epi32(int32x4_t a, int32x4_t b) {
|
||||||
|
return vaddq_s32(a, b);
|
||||||
|
}
|
||||||
|
static inline int reduce_epi32(int32x4_t v) {
|
||||||
|
int32x2_t pair = vadd_s32(vget_low_s32(v), vget_high_s32(v));
|
||||||
|
int32x2_t total = vpadd_s32(pair, pair);
|
||||||
|
return vget_lane_s32(total, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
#pragma message("Using fallback scalar NNUE inference")
|
||||||
|
using nativeVector = void*;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
static int32_t VectorizedSCReLU(const Board& board, const Network& net, size_t outputBucket) {
|
static int32_t VectorizedSCReLU(const Board& board, const Network& net, size_t outputBucket) {
|
||||||
const size_t VECTOR_SIZE = sizeof(nativeVector) / sizeof(int16_t);
|
const size_t VECTOR_SIZE = sizeof(nativeVector) / sizeof(int16_t);
|
||||||
static_assert(HL_SIZE % VECTOR_SIZE == 0, "HL size must be divisible by the native register size of your CPU for vectorization to work");
|
static_assert(HL_SIZE % VECTOR_SIZE == 0, "HL size must be divisible by the native register size of your CPU for vectorization to work");
|
||||||
@@ -128,17 +168,11 @@ static int32_t VectorizedSCReLU(const Board& board, const Network& net, size_t o
|
|||||||
|
|
||||||
return reduce_epi32(accumulator);
|
return reduce_epi32(accumulator);
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
int Forward(const Board& board, const Network& net) {
|
int Forward(const Board& board, const Network& net) {
|
||||||
bool stm = board.sideToMove;
|
|
||||||
|
|
||||||
const size_t divisor = 32 / OUTPUT_BUCKETS;
|
const size_t divisor = 32 / OUTPUT_BUCKETS;
|
||||||
const size_t outputBucket = (board.occupied.PopCount() - 2) / divisor;
|
const size_t outputBucket = (board.occupied.PopCount() - 2) / divisor;
|
||||||
|
|
||||||
const ACC::Accumulator& stmACC = stm ? board.accPair.white : board.accPair.black;
|
|
||||||
const ACC::Accumulator& nstmACC = !stm ? board.accPair.white : board.accPair.black;
|
|
||||||
|
|
||||||
int64_t eval = 0;
|
int64_t eval = 0;
|
||||||
|
|
||||||
eval = VectorizedSCReLU(board, net, outputBucket);
|
eval = VectorizedSCReLU(board, net, outputBucket);
|
||||||
@@ -147,6 +181,7 @@ int Forward(const Board& board, const Network& net) {
|
|||||||
|
|
||||||
eval += net.output_bias[outputBucket];
|
eval += net.output_bias[outputBucket];
|
||||||
|
|
||||||
|
|
||||||
return (eval * SCALE) / (QA * QB);
|
return (eval * SCALE) / (QA * QB);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -8,7 +8,7 @@
|
|||||||
class Board;
|
class Board;
|
||||||
|
|
||||||
#ifdef __AVX512F__
|
#ifdef __AVX512F__
|
||||||
constexpr usize ALIGNMENT = 64;
|
constexpr size_t ALIGNMENT = 64;
|
||||||
#else
|
#else
|
||||||
constexpr size_t ALIGNMENT = 32;
|
constexpr size_t ALIGNMENT = 32;
|
||||||
#endif
|
#endif
|
||||||
@@ -16,7 +16,7 @@ constexpr size_t ALIGNMENT = 32;
|
|||||||
namespace NNUE {
|
namespace NNUE {
|
||||||
|
|
||||||
constexpr size_t INPUT_SIZE = 768;
|
constexpr size_t INPUT_SIZE = 768;
|
||||||
constexpr size_t HL_SIZE = 256;
|
constexpr size_t HL_SIZE = 1024;
|
||||||
constexpr size_t OUTPUT_BUCKETS = 8;
|
constexpr size_t OUTPUT_BUCKETS = 8;
|
||||||
|
|
||||||
constexpr int16_t SCALE = 400;
|
constexpr int16_t SCALE = 400;
|
||||||
|
|||||||
+240
-118
@@ -22,10 +22,30 @@ void InitLMRTable() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ScoreMove(Board &board, Move &move, int ply, SearchContext& ctx) {
|
int16_t ContHistory::GetOnePly(Board& board, Move& move, SearchContext* ctx, int ply) {
|
||||||
TTEntry *current = ctx.TT.GetRawEntry(board.hashKey);
|
int prevType = ctx->ss[ply-1].pieceType;
|
||||||
if (current->hashKey == board.hashKey && current->bestMove == move) {
|
int prevTo = ctx->ss[ply-1].moveTo;
|
||||||
return 50000;
|
int pieceType = board.GetPieceType(move.MoveFrom());
|
||||||
|
int to = move.MoveTo();
|
||||||
|
bool otherColor = ctx->ss[ply-1].side;
|
||||||
|
|
||||||
|
return ctx->conthist[otherColor][prevType][prevTo][board.sideToMove][pieceType][to];
|
||||||
|
}
|
||||||
|
|
||||||
|
int16_t ContHistory::GetTwoPly(Board& board, Move& move, SearchContext* ctx, int ply) {
|
||||||
|
int prevType = ctx->ss[ply-2].pieceType;
|
||||||
|
int prevTo = ctx->ss[ply-2].moveTo;
|
||||||
|
int pieceType = board.GetPieceType(move.MoveFrom());
|
||||||
|
int to = move.MoveTo();
|
||||||
|
bool otherColor = ctx->ss[ply-2].side;
|
||||||
|
|
||||||
|
return ctx->conthist[otherColor][prevType][prevTo][board.sideToMove][pieceType][to];
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) {
|
||||||
|
TTEntry current = ctx->TT.GetRawEntry(board.hashKey);
|
||||||
|
if (current.hashKey == board.hashKey && current.bestMove == move) {
|
||||||
|
return 100000;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (move.IsCapture()) {
|
if (move.IsCapture()) {
|
||||||
@@ -36,22 +56,33 @@ static int ScoreMove(Board &board, Move &move, int ply, SearchContext& ctx) {
|
|||||||
targetType = Pawn;
|
targetType = Pawn;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 30000 * ((SEE(board, move, -100))) + moveScoreTable[attackerType][targetType];
|
return 50000 * ((SEE(board, move, -100))) + (100 * targetType - attackerType + 105);
|
||||||
} else {
|
} else {
|
||||||
if (ctx.killerMoves[0][ply] == move) {
|
if (ctx->killerMoves[0][ply] == move) {
|
||||||
return 20000;
|
return 41000;
|
||||||
} else if (ctx.killerMoves[1][ply] == move) {
|
} else if (ctx->killerMoves[1][ply] == move) {
|
||||||
return 18000;
|
return 40000;
|
||||||
} else {
|
} else {
|
||||||
// Max 16384
|
int historyScore = ctx->history[board.sideToMove][move.MoveFrom()][move.MoveTo()];
|
||||||
return ctx.history[board.sideToMove][move.MoveFrom()][move.MoveTo()];
|
int conthistScore = 0;
|
||||||
|
|
||||||
|
if (ply > 0) {
|
||||||
|
conthistScore = ctx->conthist.GetOnePly(board, move, ctx, ply);
|
||||||
|
/*
|
||||||
|
if (ply > 1) {
|
||||||
|
conthistScore += ctx->conthist.GetTwoPly(board, move, ctx, ply);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
return 20000 + historyScore + conthistScore;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void SortMoves(Board &board, int ply, SearchContext& ctx) {
|
static void SortMoves(Board &board, int ply, SearchContext* ctx) {
|
||||||
if (board.currentMoveIndex <= 1) {
|
if (board.currentMoveIndex <= 1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -78,7 +109,7 @@ static void SortMoves(Board &board, int ply, SearchContext& ctx) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ListScores(Board &board, int ply, SearchContext& ctx) {
|
void ListScores(Board &board, int ply, SearchContext* ctx) {
|
||||||
SortMoves(board, ply, ctx);
|
SortMoves(board, ply, ctx);
|
||||||
|
|
||||||
for (int i = 0; i < board.currentMoveIndex; i++) {
|
for (int i = 0; i < board.currentMoveIndex; i++) {
|
||||||
@@ -89,9 +120,9 @@ void ListScores(Board &board, int ply, SearchContext& ctx) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool IsThreefold(Board &board, SearchContext& ctx) {
|
static bool IsThreefold(Board &board, SearchContext* ctx) {
|
||||||
for (int i = 0; i < board.positionIndex; i++) {
|
for (int i = 0; i < board.positionIndex; i++) {
|
||||||
if (ctx.positionHistory[i] == board.hashKey) {
|
if (ctx->positionHistory[i] == board.hashKey) {
|
||||||
// repetition found
|
// repetition found
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -110,22 +141,28 @@ static bool IsInsuffMat(Board &board) {
|
|||||||
&& !(board.pieces[Pawn] | board.pieces[Queen] | board.pieces[Rook]));
|
&& !(board.pieces[Pawn] | board.pieces[Queen] | board.pieces[Rook]));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IsDraw(Board &board, SearchContext& ctx) {
|
bool IsDraw(Board &board, SearchContext* ctx) {
|
||||||
return IsFifty(board) || IsInsuffMat(board) || IsThreefold(board, ctx);
|
return IsFifty(board) || IsInsuffMat(board) || IsThreefold(board, ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int ply, bool cutnode, SearchContext& ctx) {
|
template <bool isPV>
|
||||||
|
static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int ply, bool cutnode, SearchContext* ctx) {
|
||||||
int reduction = 0;
|
int reduction = 0;
|
||||||
|
|
||||||
// Late Move Reduction
|
// Late Move Reduction
|
||||||
if (depth >= 3 && moveSeen >= 3 && !move.IsCapture()) {
|
if (depth >= 3 && moveSeen >= 2 + (2 * isPV) && !move.IsCapture()) {
|
||||||
reduction = lmrTable[depth][moveSeen];
|
reduction = lmrTable[depth][moveSeen];
|
||||||
|
|
||||||
if (cutnode)
|
if (cutnode)
|
||||||
reduction += 2;
|
reduction += 2;
|
||||||
|
|
||||||
// History LMR
|
// History LMR
|
||||||
int historyReduction = ctx.history[board.sideToMove][move.MoveFrom()][move.MoveTo()] / 8192;
|
int historyReduction = ctx->history[board.sideToMove][move.MoveFrom()][move.MoveTo()];
|
||||||
|
|
||||||
|
if (ply > 0)
|
||||||
|
historyReduction += ctx->conthist.GetOnePly(board, move, ctx, ply);
|
||||||
|
|
||||||
|
historyReduction /= 8192;
|
||||||
reduction -= historyReduction;
|
reduction -= historyReduction;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -205,37 +242,39 @@ bool SEE(Board& board, Move& move, int threshold) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <searchMode mode>
|
template <searchMode mode>
|
||||||
static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, SearchContext& ctx) {
|
static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, SearchContext* ctx) {
|
||||||
if constexpr (mode != nodesMode) {
|
if constexpr (mode != nodesMode) {
|
||||||
if constexpr (mode != bench) {
|
if constexpr (mode != bench) {
|
||||||
if (ctx.nodes % 1024 == 0) {
|
if (ctx->nodes % 1024 == 0) {
|
||||||
if (ctx.sw.GetElapsedMS() >= ctx.timeToSearch) {
|
if (ctx->sw.GetElapsedMS() >= ctx->timeToSearch) {
|
||||||
ctx.searchStopped = true;
|
ctx->searchStopped = true;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if constexpr (mode == nodesMode) {
|
} else if constexpr (mode == nodesMode) {
|
||||||
if (ctx.nodes > ctx.nodesToGo) {
|
if (ctx->nodes > ctx->nodesToGo) {
|
||||||
ctx.searchStopped = true;
|
ctx->searchStopped = true;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
} else if constexpr (mode == datagen) {
|
} else if constexpr (mode == datagen) {
|
||||||
if (ctx.nodes > DATAGEN::HARD_NODES) {
|
if (ctx->nodes > DATAGEN::HARD_NODES) {
|
||||||
ctx.searchStopped = true;
|
ctx->searchStopped = true;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ply > ctx.seldepth)
|
if (ply > ctx->seldepth)
|
||||||
ctx.seldepth = ply;
|
ctx->seldepth = ply;
|
||||||
|
|
||||||
int bestScore = NNUE::net.Evaluate(board);
|
int bestScore = NNUE::net.Evaluate(board);
|
||||||
ctx.ss[ply].eval = bestScore;
|
ctx->ss[ply].eval = bestScore;
|
||||||
|
|
||||||
TTEntry *entry = ctx.TT.GetRawEntry(board.hashKey);
|
if (!ctx->excluded) {
|
||||||
if (entry->hashKey == board.hashKey) {
|
TTEntry entry = ctx->TT.GetRawEntry(board.hashKey);
|
||||||
bestScore = entry->score;
|
if (entry.hashKey == board.hashKey) {
|
||||||
|
bestScore = entry.score;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -258,14 +297,18 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear
|
|||||||
int isLegal = copy.MakeMove(board.moveList[i]);
|
int isLegal = copy.MakeMove(board.moveList[i]);
|
||||||
if (!isLegal) continue;
|
if (!isLegal) continue;
|
||||||
|
|
||||||
|
ctx->ss[ply].pieceType = board.GetPieceType(board.moveList[i].MoveFrom());
|
||||||
|
ctx->ss[ply].moveTo = board.moveList[i].MoveTo();
|
||||||
|
ctx->ss[ply].side = board.sideToMove;
|
||||||
|
|
||||||
if (!SEE(board, board.moveList[i], 0))
|
if (!SEE(board, board.moveList[i], 0))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (copy.positionIndex >= ctx.positionHistory.size()) {
|
if (copy.positionIndex >= ctx->positionHistory.size()) {
|
||||||
ctx.positionHistory.resize(copy.positionIndex + 100);
|
ctx->positionHistory.resize(copy.positionIndex + 100);
|
||||||
}
|
}
|
||||||
ctx.positionHistory[copy.positionIndex] = copy.hashKey;
|
ctx->positionHistory[copy.positionIndex] = copy.hashKey;
|
||||||
ctx.nodes++;
|
ctx->nodes++;
|
||||||
int score = -Quiescence<mode>(copy, -beta, -alpha, ply + 1, ctx).score;
|
int score = -Quiescence<mode>(copy, -beta, -alpha, ply + 1, ctx).score;
|
||||||
|
|
||||||
if (score >= beta) {
|
if (score >= beta) {
|
||||||
@@ -281,86 +324,97 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear
|
|||||||
}
|
}
|
||||||
|
|
||||||
results.score = bestScore;
|
results.score = bestScore;
|
||||||
if (ctx.searchStopped) return 0;
|
if (ctx->searchStopped) return 0;
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <bool isPV, searchMode mode>
|
template <bool isPV, searchMode mode>
|
||||||
SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchContext& ctx, bool cutnode) {
|
SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchContext* ctx, bool cutnode) {
|
||||||
if constexpr (mode != bench && mode != nodesMode) {
|
if constexpr (mode != bench && mode != nodesMode) {
|
||||||
if (ctx.nodes % 1024 == 0) {
|
if (ctx->nodes % 1024 == 0) {
|
||||||
if constexpr (mode == normal) {
|
if constexpr (mode == normal) {
|
||||||
if (ctx.sw.GetElapsedMS() >= ctx.timeToSearch) {
|
if (ctx->sw.GetElapsedMS() >= ctx->timeToSearch) {
|
||||||
ctx.searchStopped = true;
|
ctx->searchStopped = true;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if constexpr (mode == nodesMode) {
|
} else if constexpr (mode == nodesMode) {
|
||||||
if (ctx.nodes > ctx.nodesToGo) {
|
if (ctx->nodes > ctx->nodesToGo) {
|
||||||
ctx.searchStopped = true;
|
ctx->searchStopped = true;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
} else if constexpr (mode == datagen) {
|
} else if constexpr (mode == datagen) {
|
||||||
if (ctx.nodes > DATAGEN::HARD_NODES) {
|
if (ctx->nodes > DATAGEN::HARD_NODES) {
|
||||||
ctx.searchStopped = true;
|
ctx->searchStopped = true;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ply > ctx.seldepth)
|
if (ply > ctx->seldepth)
|
||||||
ctx.seldepth = ply;
|
ctx->seldepth = ply;
|
||||||
|
|
||||||
|
|
||||||
ctx.pvLine.SetLength(ply);
|
ctx->pvLine.SetLength(ply);
|
||||||
if (ply && (IsDraw(board, ctx))) return 0;
|
if (ply && (IsDraw(board, ctx))) return 0;
|
||||||
|
|
||||||
|
|
||||||
// if NOT PV node then we try to hit the TTable
|
TTEntry entry;
|
||||||
|
if (!ctx->excluded)
|
||||||
|
entry = ctx->TT.GetRawEntry(board.hashKey);
|
||||||
|
|
||||||
|
const bool ttHit = entry.hashKey == board.hashKey;
|
||||||
|
|
||||||
if constexpr (!isPV) {
|
if constexpr (!isPV) {
|
||||||
SearchResults entry = ctx.TT.ReadEntry(board.hashKey, depth, alpha, beta);
|
if (ttHit) {
|
||||||
if (entry.score != invalidEntry) {
|
if (entry.depth >= depth &&
|
||||||
return entry;
|
((entry.nodeType == PV) ||
|
||||||
|
(entry.nodeType == AllNode && entry.score <= alpha) ||
|
||||||
|
(entry.nodeType == CutNode && entry.score >= beta))) {
|
||||||
|
|
||||||
|
return SearchResults(entry.score, entry.bestMove);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (depth <= 0) return Quiescence<mode>(board, alpha, beta, ply, ctx);
|
if (depth <= 0) return Quiescence<mode>(board, alpha, beta, ply, ctx);
|
||||||
|
|
||||||
const int staticEval = NNUE::net.Evaluate(board);
|
const int staticEval = NNUE::net.Evaluate(board);
|
||||||
ctx.ss[ply].eval = staticEval;
|
ctx->ss[ply].eval = staticEval;
|
||||||
|
|
||||||
const bool improving = [&]
|
const bool improving = [&]
|
||||||
{
|
{
|
||||||
if (board.InCheck())
|
if (board.InCheck())
|
||||||
return false;
|
return false;
|
||||||
if (ply > 1 && ctx.ss[ply - 2].eval != ScoreNone)
|
if (ply > 1 && ctx->ss[ply - 2].eval != ScoreNone)
|
||||||
return staticEval > ctx.ss[ply - 2].eval;
|
return staticEval > ctx->ss[ply - 2].eval;
|
||||||
if (ply > 3 && ctx.ss[ply - 4].eval != ScoreNone)
|
if (ply > 3 && ctx->ss[ply - 4].eval != ScoreNone)
|
||||||
return staticEval > ctx.ss[ply - 4].eval;
|
return staticEval > ctx->ss[ply - 4].eval;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}();
|
}();
|
||||||
|
|
||||||
if (!board.InCheck()) {
|
if (!board.InCheck() && !ctx->excluded) {
|
||||||
if (ply) {
|
if (ply) {
|
||||||
// Reverse Futility Pruning
|
// Reverse Futility Pruning
|
||||||
int margin = 100 * (depth - improving);
|
int margin = 100 * (depth - improving);
|
||||||
if (staticEval - margin >= beta && depth < 7) {
|
if (!ttHit && staticEval - margin >= beta && depth < 7) {
|
||||||
return (beta + (staticEval - beta) / 3);
|
return (beta + (staticEval - beta) / 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Null Move Pruning
|
// Null Move Pruning
|
||||||
if (!ctx.doingNullMove && staticEval >= beta) {
|
if (!ctx->doingNullMove && staticEval >= beta) {
|
||||||
if (depth >= 3 && !board.InPossibleZug()) {
|
if (depth > 1 && !board.InPossibleZug()) {
|
||||||
Board copy = board;
|
Board copy = board;
|
||||||
bool isLegal = copy.MakeMove(Move());
|
copy.MakeMove(Move());
|
||||||
// Always legal so we dont check it
|
|
||||||
|
|
||||||
ctx.doingNullMove = true;
|
const int reduction = 3 + improving;
|
||||||
int score = -PVS<false, mode>(copy, depth - 3, -beta, -beta + 1, ply + 1, ctx, !cutnode).score;
|
|
||||||
ctx.doingNullMove = false;
|
|
||||||
|
|
||||||
if (ctx.searchStopped) return 0;
|
ctx->doingNullMove = true;
|
||||||
|
int score = -PVS<false, mode>(copy, depth - reduction, -beta, -beta + 1, ply + 1, ctx, !cutnode).score;
|
||||||
|
ctx->doingNullMove = false;
|
||||||
|
|
||||||
|
if (ctx->searchStopped) return 0;
|
||||||
if (score >= beta) return score;
|
if (score >= beta) return score;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -383,6 +437,9 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
for (int i = 0; i < board.currentMoveIndex; i++) {
|
for (int i = 0; i < board.currentMoveIndex; i++) {
|
||||||
Move currMove = board.moveList[i];
|
Move currMove = board.moveList[i];
|
||||||
|
|
||||||
|
if (ctx->excluded == currMove)
|
||||||
|
continue;
|
||||||
|
|
||||||
bool notMated = results.score > (-MATE_SCORE + MAX_PLY);
|
bool notMated = results.score > (-MATE_SCORE + MAX_PLY);
|
||||||
|
|
||||||
// Late move pruning
|
// Late move pruning
|
||||||
@@ -391,6 +448,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
if (!isPV && !board.InCheck() && currMove.IsQuiet() && notMated) {
|
if (!isPV && !board.InCheck() && currMove.IsQuiet() && notMated) {
|
||||||
int lmpBase = 7;
|
int lmpBase = 7;
|
||||||
|
|
||||||
|
|
||||||
int lmpThreshold = lmpBase + 4 * depth;
|
int lmpThreshold = lmpBase + 4 * depth;
|
||||||
|
|
||||||
if (moveSeen >= lmpThreshold) {
|
if (moveSeen >= lmpThreshold) {
|
||||||
@@ -401,22 +459,49 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
// Futility pruning
|
// Futility pruning
|
||||||
// If our static eval is far below alpha, there is only a small chance
|
// If our static eval is far below alpha, there is only a small chance
|
||||||
// that a quiet move will help us so we skip them
|
// that a quiet move will help us so we skip them
|
||||||
int fpMargin = 100 * depth;
|
int historyScore = ctx->history[board.sideToMove][currMove.MoveFrom()][currMove.MoveTo()];
|
||||||
|
int fpMargin = 100 * depth + historyScore / 32;
|
||||||
|
|
||||||
if (!isPV && ply && currMove.IsQuiet()
|
if (!isPV && ply && currMove.IsQuiet()
|
||||||
&& depth <= 5 && staticEval + fpMargin < alpha && notMated) {
|
&& depth <= 5 && staticEval + fpMargin < alpha && notMated) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Board copy = board;
|
Board copy = board;
|
||||||
bool isLegal = copy.MakeMove(currMove);
|
bool isLegal = copy.MakeMove(currMove);
|
||||||
|
|
||||||
if (!isLegal) continue;
|
if (!isLegal) continue;
|
||||||
if (copy.positionIndex >= ctx.positionHistory.size()) {
|
|
||||||
ctx.positionHistory.resize(copy.positionIndex + 100);
|
ctx->ss[ply].pieceType = board.GetPieceType(currMove.MoveFrom());
|
||||||
|
ctx->ss[ply].moveTo = currMove.MoveTo();
|
||||||
|
ctx->ss[ply].side = board.sideToMove;
|
||||||
|
|
||||||
|
if (copy.positionIndex >= ctx->positionHistory.size()) {
|
||||||
|
ctx->positionHistory.resize(copy.positionIndex + 100);
|
||||||
|
}
|
||||||
|
ctx->positionHistory[copy.positionIndex] = copy.hashKey;
|
||||||
|
ctx->nodes++;
|
||||||
|
|
||||||
|
int extension = 0;
|
||||||
|
|
||||||
|
if (ply
|
||||||
|
&& depth >= 8
|
||||||
|
&& ttHit
|
||||||
|
&& currMove == entry.bestMove
|
||||||
|
&& ctx->excluded == 0
|
||||||
|
&& entry.depth >= depth - 3
|
||||||
|
&& entry.nodeType != AllNode)
|
||||||
|
{
|
||||||
|
const int sBeta = std::max(-inf + 1, entry.score - depth * 2);
|
||||||
|
const int sDepth = (depth - 1) / 2;
|
||||||
|
|
||||||
|
ctx->excluded = currMove;
|
||||||
|
const int singularScore = PVS<false, mode>(board, sDepth, sBeta-1, sBeta, ply, ctx, cutnode).score;
|
||||||
|
ctx->excluded = Move();
|
||||||
|
|
||||||
|
if (singularScore < sBeta)
|
||||||
|
extension = 1;
|
||||||
}
|
}
|
||||||
ctx.positionHistory[copy.positionIndex] = copy.hashKey;
|
|
||||||
ctx.nodes++;
|
|
||||||
|
|
||||||
// PVS SEE
|
// PVS SEE
|
||||||
int SEEThreshold = currMove.IsQuiet() ? -80 * depth : -30 * depth * depth;
|
int SEEThreshold = currMove.IsQuiet() ? -80 * depth : -30 * depth * depth;
|
||||||
@@ -424,10 +509,9 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
if (ply && depth <= 10 && !SEE(board, currMove, SEEThreshold))
|
if (ply && depth <= 10 && !SEE(board, currMove, SEEThreshold))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
int reductions = GetReductions<isPV>(board, currMove, depth, moveSeen, ply, cutnode, ctx);
|
||||||
|
|
||||||
int reductions = GetReductions(board, currMove, depth, moveSeen, ply, cutnode, ctx);
|
int newDepth = depth + copy.InCheck() - 1 + extension;
|
||||||
|
|
||||||
int newDepth = depth + copy.InCheck() - 1;
|
|
||||||
|
|
||||||
// First move (suspected PV node)
|
// First move (suspected PV node)
|
||||||
if (!moveSeen) {
|
if (!moveSeen) {
|
||||||
@@ -457,7 +541,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
|
|
||||||
moveSeen++;
|
moveSeen++;
|
||||||
|
|
||||||
if (ctx.searchStopped) return 0;
|
if (ctx->searchStopped) return 0;
|
||||||
|
|
||||||
if (currMove != 0 && currMove.IsQuiet()) {
|
if (currMove != 0 && currMove.IsQuiet()) {
|
||||||
seenQuiets[seenQuietsCount] = currMove;
|
seenQuiets[seenQuietsCount] = currMove;
|
||||||
@@ -467,20 +551,57 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
// Fail high (beta cutoff)
|
// Fail high (beta cutoff)
|
||||||
if (score >= beta) {
|
if (score >= beta) {
|
||||||
if (!currMove.IsCapture()) {
|
if (!currMove.IsCapture()) {
|
||||||
ctx.killerMoves[1][ply] = ctx.killerMoves[0][ply];
|
ctx->killerMoves[1][ply] = ctx->killerMoves[0][ply];
|
||||||
ctx.killerMoves[0][ply] = currMove;
|
ctx->killerMoves[0][ply] = currMove;
|
||||||
|
|
||||||
int bonus = 300 * depth - 250;
|
int bonus = 300 * depth - 250;
|
||||||
|
|
||||||
ctx.history.Update(board.sideToMove, currMove, bonus);
|
ctx->history.Update(board.sideToMove, currMove, bonus);
|
||||||
|
|
||||||
|
if (ply > 0) {
|
||||||
|
int prevType = ctx->ss[ply-1].pieceType;
|
||||||
|
int prevTo = ctx->ss[ply-1].moveTo;
|
||||||
|
int pieceType = ctx->ss[ply].pieceType;
|
||||||
|
int to = ctx->ss[ply].moveTo;
|
||||||
|
bool otherColor = ctx->ss[ply-1].side;
|
||||||
|
|
||||||
|
ctx->conthist.Update(board.sideToMove, otherColor, prevType, prevTo, pieceType, to, bonus);
|
||||||
|
|
||||||
|
// Malus
|
||||||
|
for (int moveIndex = 0; moveIndex < seenQuietsCount - 1; moveIndex++) {
|
||||||
|
int pieceType = board.GetPieceType(seenQuiets[moveIndex].MoveFrom());
|
||||||
|
int to = seenQuiets[moveIndex].MoveTo();
|
||||||
|
|
||||||
|
ctx->conthist.Update(board.sideToMove, otherColor, prevType, prevTo, pieceType, to, -bonus);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ply > 1) {
|
||||||
|
prevType = ctx->ss[ply-2].pieceType;
|
||||||
|
prevTo = ctx->ss[ply-2].moveTo;
|
||||||
|
otherColor = ctx->ss[ply-2].side;
|
||||||
|
|
||||||
|
ctx->conthist.Update(board.sideToMove, otherColor, prevType, prevTo, pieceType, to, bonus);
|
||||||
|
|
||||||
|
// Malus
|
||||||
|
for (int moveIndex = 0; moveIndex < seenQuietsCount - 1; moveIndex++) {
|
||||||
|
int pieceType = board.GetPieceType(seenQuiets[moveIndex].MoveFrom());
|
||||||
|
int to = seenQuiets[moveIndex].MoveTo();
|
||||||
|
|
||||||
|
ctx->conthist.Update(board.sideToMove, otherColor, prevType, prevTo, pieceType, to, -bonus);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Malus
|
// Malus
|
||||||
for (int moveIndex = 0; moveIndex < seenQuietsCount - 1; moveIndex++) {
|
for (int moveIndex = 0; moveIndex < seenQuietsCount - 1; moveIndex++) {
|
||||||
ctx.history.Update(board.sideToMove, seenQuiets[moveIndex], -bonus);
|
ctx->history.Update(board.sideToMove, seenQuiets[moveIndex], -bonus);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.TT.WriteEntry(board.hashKey, depth, score, CutNode, Move());
|
if (!ctx->excluded)
|
||||||
|
ctx->TT.WriteEntry(board.hashKey, depth, score, CutNode, currMove);
|
||||||
return score;
|
return score;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -490,7 +611,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
nodeType = PV;
|
nodeType = PV;
|
||||||
alpha = score;
|
alpha = score;
|
||||||
results.bestMove = currMove;
|
results.bestMove = currMove;
|
||||||
ctx.pvLine.SetMove(ply, currMove);
|
ctx->pvLine.SetMove(ply, currMove);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -502,8 +623,9 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ctx.searchStopped) return 0;
|
if (ctx->searchStopped) return 0;
|
||||||
ctx.TT.WriteEntry(board.hashKey, depth, results.score, nodeType, results.bestMove);
|
if (!ctx->excluded)
|
||||||
|
ctx->TT.WriteEntry(board.hashKey, depth, results.score, nodeType, results.bestMove);
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -533,7 +655,7 @@ public:
|
|||||||
|
|
||||||
// Iterative deepening
|
// Iterative deepening
|
||||||
template <searchMode mode>
|
template <searchMode mode>
|
||||||
static SearchResults ID(Board &board, SearchParams params, SearchContext& ctx) {
|
static SearchResults ID(Board &board, SearchParams params, SearchContext* ctx) {
|
||||||
int fullTime = board.sideToMove ? params.btime : params.wtime;
|
int fullTime = board.sideToMove ? params.btime : params.wtime;
|
||||||
int inc = board.sideToMove ? params.binc : params.winc;
|
int inc = board.sideToMove ? params.binc : params.winc;
|
||||||
int movesToGo = params.movesToGo ? params.movesToGo : 20;
|
int movesToGo = params.movesToGo ? params.movesToGo : 20;
|
||||||
@@ -551,14 +673,14 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext& ctx) {
|
|||||||
|
|
||||||
int elapsed = 0;
|
int elapsed = 0;
|
||||||
|
|
||||||
ctx.sw.Restart();
|
ctx->sw.Restart();
|
||||||
|
|
||||||
for (int depth = 1; depth <= toDepth; depth++) {
|
for (int depth = 1; depth <= toDepth; depth++) {
|
||||||
ctx.timeToSearch = std::max((fullTime / movesToGo) + (inc / 2), 4);
|
ctx->timeToSearch = std::max((fullTime / movesToGo) + (inc / 2), 4);
|
||||||
int softTime = ctx.timeToSearch * 0.65;
|
int softTime = ctx->timeToSearch * 0.65;
|
||||||
|
|
||||||
SearchResults currentResults = PVS<true, mode>(board, depth, aw.alpha, aw.beta, 0, ctx, false);
|
SearchResults currentResults = PVS<true, mode>(board, depth, aw.alpha, aw.beta, 0, ctx, false);
|
||||||
elapsed = ctx.sw.GetElapsedMS();
|
elapsed = ctx->sw.GetElapsedMS();
|
||||||
|
|
||||||
if (aw.alpha != -inf && currentResults.score <= aw.alpha) {
|
if (aw.alpha != -inf && currentResults.score <= aw.alpha) {
|
||||||
aw.WidenDown();
|
aw.WidenDown();
|
||||||
@@ -573,7 +695,7 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext& ctx) {
|
|||||||
|
|
||||||
aw.Set(currentResults.score);
|
aw.Set(currentResults.score);
|
||||||
|
|
||||||
if (ctx.searchStopped) {
|
if (ctx->searchStopped) {
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
if (currentResults.bestMove) {
|
if (currentResults.bestMove) {
|
||||||
@@ -583,24 +705,24 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext& ctx) {
|
|||||||
if constexpr (mode == normal || mode == nodesMode) {
|
if constexpr (mode == normal || mode == nodesMode) {
|
||||||
std::cout << "info ";
|
std::cout << "info ";
|
||||||
std::cout << "depth " << depth;
|
std::cout << "depth " << depth;
|
||||||
std::cout << " seldepth " << ctx.seldepth;
|
std::cout << " seldepth " << ctx->seldepth;
|
||||||
std::cout << " time " << elapsed;
|
std::cout << " time " << elapsed;
|
||||||
std::cout << " score cp " << UTILS::ConvertToWhiteRelative(board, safeResults.score);
|
std::cout << " score cp " << safeResults.score;
|
||||||
std::cout << " nodes " << ctx.nodes << " nps " << int(ctx.nodes/ctx.sw.GetElapsedSec());
|
std::cout << " nodes " << ctx->nodes << " nps " << int(ctx->nodes/ctx->sw.GetElapsedSec());
|
||||||
std::cout << " hashfull " << ctx.TT.GetUsedPercentage();
|
std::cout << " hashfull " << ctx->TT.GetUsedPercentage();
|
||||||
std::cout << " pv ";
|
std::cout << " pv ";
|
||||||
ctx.pvLine.Print(0);
|
ctx->pvLine.Print(0);
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
if constexpr (mode == normal) {
|
if constexpr (mode == normal) {
|
||||||
if (ctx.sw.GetElapsedMS() >= softTime) {
|
if (ctx->sw.GetElapsedMS() >= softTime) {
|
||||||
ctx.searchStopped = true;
|
ctx->searchStopped = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if constexpr (mode == datagen) {
|
} else if constexpr (mode == datagen) {
|
||||||
if (ctx.nodes >= DATAGEN::SOFT_NODES) {
|
if (ctx->nodes >= DATAGEN::SOFT_NODES) {
|
||||||
ctx.searchStopped = true;
|
ctx->searchStopped = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -611,17 +733,17 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext& ctx) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <searchMode mode>
|
template <searchMode mode>
|
||||||
SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx) {
|
SearchResults SearchPosition(Board &board, SearchParams params, SearchContext* ctx) {
|
||||||
ctx.searchStopped = false;
|
ctx->searchStopped = false;
|
||||||
ctx.seldepth = 0;
|
ctx->seldepth = 0;
|
||||||
if constexpr (mode != bench) {
|
if constexpr (mode != bench) {
|
||||||
ctx.nodes = 0;
|
ctx->nodes = 0;
|
||||||
|
|
||||||
if constexpr (mode == nodesMode) {
|
if constexpr (mode == nodesMode) {
|
||||||
ctx.nodesToGo = params.nodes;
|
ctx->nodesToGo = params.nodes;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ctx.pvLine.Clear();
|
ctx->pvLine.Clear();
|
||||||
|
|
||||||
SearchResults results = ID<mode>(board, params, ctx);
|
SearchResults results = ID<mode>(board, params, ctx);
|
||||||
|
|
||||||
@@ -656,16 +778,16 @@ int MoveEstimatedValue(Board& board, Move& move) {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
template SearchResults PVS<true, searchMode::bench>(Board&, int, int, int, int, SearchContext& ctx, bool cutnode);
|
template SearchResults PVS<true, searchMode::bench>(Board&, int, int, int, int, SearchContext* ctx, bool cutnode);
|
||||||
template SearchResults PVS<false, searchMode::bench>(Board&, int, int, int, int, SearchContext& ctx, bool cutnode);
|
template SearchResults PVS<false, searchMode::bench>(Board&, int, int, int, int, SearchContext* ctx, bool cutnode);
|
||||||
template SearchResults PVS<true, searchMode::normal>(Board&, int, int, int, int, SearchContext& ctx, bool cutnode);
|
template SearchResults PVS<true, searchMode::normal>(Board&, int, int, int, int, SearchContext* ctx, bool cutnode);
|
||||||
template SearchResults PVS<false, searchMode::normal>(Board&, int, int, int, int, SearchContext& ctx, bool cutnode);
|
template SearchResults PVS<false, searchMode::normal>(Board&, int, int, int, int, SearchContext* ctx, bool cutnode);
|
||||||
template SearchResults PVS<true, searchMode::datagen>(Board&, int, int, int, int, SearchContext& ctx, bool cutnode);
|
template SearchResults PVS<true, searchMode::datagen>(Board&, int, int, int, int, SearchContext* ctx, bool cutnode);
|
||||||
template SearchResults PVS<false, searchMode::datagen>(Board&, int, int, int, int, SearchContext& ctx, bool cutnode);
|
template SearchResults PVS<false, searchMode::datagen>(Board&, int, int, int, int, SearchContext* ctx, bool cutnode);
|
||||||
|
|
||||||
template SearchResults SearchPosition<normal>(Board &board, SearchParams params, SearchContext& ctx);
|
template SearchResults SearchPosition<normal>(Board &board, SearchParams params, SearchContext* ctx);
|
||||||
template SearchResults SearchPosition<bench>(Board &board, SearchParams params, SearchContext& ctx);
|
template SearchResults SearchPosition<bench>(Board &board, SearchParams params, SearchContext* ctx);
|
||||||
template SearchResults SearchPosition<datagen>(Board &board, SearchParams params, SearchContext& ctx);
|
template SearchResults SearchPosition<datagen>(Board &board, SearchParams params, SearchContext* ctx);
|
||||||
template SearchResults SearchPosition<nodesMode>(Board &board, SearchParams params, SearchContext& ctx);
|
template SearchResults SearchPosition<nodesMode>(Board &board, SearchParams params, SearchContext* ctx);
|
||||||
|
|
||||||
}
|
}
|
||||||
+45
-24
@@ -7,6 +7,8 @@
|
|||||||
#include "stopwatch.h"
|
#include "stopwatch.h"
|
||||||
#include "tt.h"
|
#include "tt.h"
|
||||||
|
|
||||||
|
#include "../external/multi_array.h"
|
||||||
|
|
||||||
namespace SEARCH {
|
namespace SEARCH {
|
||||||
|
|
||||||
enum searchMode {
|
enum searchMode {
|
||||||
@@ -17,8 +19,8 @@ enum searchMode {
|
|||||||
};
|
};
|
||||||
|
|
||||||
constexpr int MATE_SCORE = 32767;
|
constexpr int MATE_SCORE = 32767;
|
||||||
constexpr int MAX_DEPTH = 64;
|
constexpr int MAX_DEPTH = 128;
|
||||||
constexpr int MAX_PLY = 128;
|
constexpr int MAX_PLY = 512;
|
||||||
constexpr int MAX_HISTORY = 16384;
|
constexpr int MAX_HISTORY = 16384;
|
||||||
|
|
||||||
constexpr int32_t ScoreNone = -255000;
|
constexpr int32_t ScoreNone = -255000;
|
||||||
@@ -31,10 +33,35 @@ constexpr std::array<int, 6> SEEPieceValues = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
void InitLMRTable();
|
void InitLMRTable();
|
||||||
|
class SearchContext;
|
||||||
|
|
||||||
|
class ContHistory {
|
||||||
|
private:
|
||||||
|
// indexed by [other color][prev move piece][stm][prev move to][piece][to]
|
||||||
|
MultiArray<int16_t, 2, 6, 64, 2, 6, 64> contHistMoves;
|
||||||
|
public:
|
||||||
|
void Update(bool stm, bool otherColor, int prevType, int prevTo, int type, int to, int bonus) {
|
||||||
|
int clampedBonus = std::clamp(bonus, -MAX_HISTORY, MAX_HISTORY);
|
||||||
|
|
||||||
|
contHistMoves[otherColor][prevType][prevTo][stm][type][to] +=
|
||||||
|
clampedBonus - contHistMoves[otherColor][prevType][prevTo][stm][type][to] * std::abs(clampedBonus) / MAX_HISTORY;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Clear() {
|
||||||
|
std::fill(&contHistMoves[0][0][0][0][0][0], &contHistMoves[0][0][0][0][0][0] + sizeof(contHistMoves) / sizeof(int16_t), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int16_t GetOnePly(Board& board, Move& move, SearchContext* ctx, int ply);
|
||||||
|
int16_t GetTwoPly(Board& board, Move& move, SearchContext* ctx, int ply);
|
||||||
|
|
||||||
|
auto& operator[](int index) {
|
||||||
|
return contHistMoves[index];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
class History {
|
class History {
|
||||||
private:
|
private:
|
||||||
int historyMoves[2][64][64];
|
MultiArray<int, 2, 64, 64> historyMoves;
|
||||||
public:
|
public:
|
||||||
void Update(bool stm, Move move, int bonus) {
|
void Update(bool stm, Move move, int bonus) {
|
||||||
int clampedBonus = std::clamp(bonus, -MAX_HISTORY, MAX_HISTORY);
|
int clampedBonus = std::clamp(bonus, -MAX_HISTORY, MAX_HISTORY);
|
||||||
@@ -43,13 +70,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Clear() {
|
void Clear() {
|
||||||
for (auto &side : historyMoves) {
|
std::fill(&historyMoves[0][0][0], &historyMoves[0][0][0] + sizeof(historyMoves) / sizeof(int), 0);
|
||||||
for (auto &from : side) {
|
|
||||||
for (auto &to : from) {
|
|
||||||
to = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto& operator[](int index) {
|
auto& operator[](int index) {
|
||||||
@@ -92,6 +113,10 @@ public:
|
|||||||
class Stack {
|
class Stack {
|
||||||
public:
|
public:
|
||||||
int32_t eval = ScoreNone;
|
int32_t eval = ScoreNone;
|
||||||
|
|
||||||
|
int pieceType = -1;
|
||||||
|
int moveTo = -1;
|
||||||
|
bool side = -1;
|
||||||
};
|
};
|
||||||
|
|
||||||
class SearchContext {
|
class SearchContext {
|
||||||
@@ -102,11 +127,14 @@ public:
|
|||||||
|
|
||||||
int seldepth = 0;
|
int seldepth = 0;
|
||||||
|
|
||||||
|
Move excluded = Move();
|
||||||
|
|
||||||
bool doingNullMove = false;
|
bool doingNullMove = false;
|
||||||
bool searchStopped = false;
|
bool searchStopped = false;
|
||||||
|
|
||||||
PVLine pvLine;
|
PVLine pvLine;
|
||||||
History history;
|
History history;
|
||||||
|
ContHistory conthist;
|
||||||
|
|
||||||
std::array<std::array<int, MAX_PLY>, 2> killerMoves{};
|
std::array<std::array<int, MAX_PLY>, 2> killerMoves{};
|
||||||
|
|
||||||
@@ -121,33 +149,26 @@ public:
|
|||||||
SearchContext(){
|
SearchContext(){
|
||||||
pvLine.Clear();
|
pvLine.Clear();
|
||||||
history.Clear();
|
history.Clear();
|
||||||
|
conthist.Clear();
|
||||||
TT.Clear();
|
TT.Clear();
|
||||||
sw.Restart();
|
sw.Restart();
|
||||||
killerMoves = {};
|
killerMoves = {};
|
||||||
positionHistory.reserve(1000);
|
ss = {};
|
||||||
|
positionHistory.resize(1000);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
bool SEE(Board& board, Move& move, int threshold);
|
bool SEE(Board& board, Move& move, int threshold);
|
||||||
|
|
||||||
template <bool isPV, searchMode mode>
|
template <bool isPV, searchMode mode>
|
||||||
SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchContext& ctx, bool cutnode);
|
SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchContext* ctx, bool cutnode);
|
||||||
|
|
||||||
template <searchMode mode>
|
template <searchMode mode>
|
||||||
SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx);
|
SearchResults SearchPosition(Board &board, SearchParams params, SearchContext* ctx);
|
||||||
|
|
||||||
bool IsDraw(Board &board, SearchContext& ctx);
|
bool IsDraw(Board &board, SearchContext* ctx);
|
||||||
|
|
||||||
constexpr int moveScoreTable[6][6] = {
|
void ListScores(Board &board, SearchContext* ctx);
|
||||||
105, 205, 305, 405, 505, 605,
|
|
||||||
104, 204, 304, 404, 504, 604,
|
|
||||||
103, 203, 303, 403, 503, 603,
|
|
||||||
102, 202, 302, 402, 502, 602,
|
|
||||||
101, 201, 301, 401, 501, 601,
|
|
||||||
100, 200, 300, 400, 500, 600
|
|
||||||
};
|
|
||||||
|
|
||||||
void ListScores(Board &board, SearchContext& ctx);
|
|
||||||
|
|
||||||
int MoveEstimatedValue(Board& board, Move& move);
|
int MoveEstimatedValue(Board& board, Move& move);
|
||||||
}
|
}
|
||||||
@@ -1,24 +1,5 @@
|
|||||||
#include "tt.h"
|
#include "tt.h"
|
||||||
|
|
||||||
SearchResults TTable::ReadEntry(U64 &hashKey, int depth, int alpha, int beta) {
|
|
||||||
TTEntry *current = &table[hashKey % table.size()];
|
|
||||||
|
|
||||||
if (current->hashKey == hashKey) {
|
|
||||||
if (current->depth >= depth) {
|
|
||||||
if (current->nodeType == PV){
|
|
||||||
return {current->score, current->bestMove};
|
|
||||||
} else if (current->nodeType == AllNode && current->score <= alpha) {
|
|
||||||
return current->score;
|
|
||||||
} else if (current->nodeType == CutNode && current->score >= beta) {
|
|
||||||
return current->score;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// returning invalid value
|
|
||||||
return invalidEntry;
|
|
||||||
}
|
|
||||||
|
|
||||||
void TTable::WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move bestMove) {
|
void TTable::WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move bestMove) {
|
||||||
TTEntry *current = &table[hashKey % table.size()];
|
TTEntry *current = &table[hashKey % table.size()];
|
||||||
|
|
||||||
|
|||||||
+12
-6
@@ -27,17 +27,23 @@ public:
|
|||||||
uint8_t depth = 0;
|
uint8_t depth = 0;
|
||||||
uint8_t nodeType = 0;
|
uint8_t nodeType = 0;
|
||||||
|
|
||||||
|
TTEntry() {}
|
||||||
|
|
||||||
|
TTEntry(int16_t score) {
|
||||||
|
this->score = score;
|
||||||
|
}
|
||||||
|
|
||||||
operator U64() {
|
operator U64() {
|
||||||
return hashKey;
|
return hashKey;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 64 MB
|
// 1024 MB
|
||||||
constexpr U64 maxHash = (1024 * 1000000) / sizeof(TTEntry);
|
constexpr U64 maxHash = (1024 * 1000000) / sizeof(TTEntry);
|
||||||
// 5 MB
|
// 8 MB
|
||||||
constexpr U64 defaultHash = (8 * 1000000) / sizeof(TTEntry);
|
constexpr U64 defaultHash = (8 * 1000000) / sizeof(TTEntry);
|
||||||
|
|
||||||
constexpr int invalidEntry = 111111;
|
constexpr int16_t invalidEntry = 11111;
|
||||||
|
|
||||||
class TTable {
|
class TTable {
|
||||||
private:
|
private:
|
||||||
@@ -55,9 +61,9 @@ public:
|
|||||||
table.resize(size);
|
table.resize(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
TTEntry* GetRawEntry(U64 &hashKey) {
|
TTEntry GetRawEntry(U64 &hashKey) {
|
||||||
TTEntry *current = &table[hashKey % table.size()];
|
TTEntry *current = &table[hashKey % table.size()];
|
||||||
return current;
|
return *current;
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns used space per mill
|
// returns used space per mill
|
||||||
@@ -71,7 +77,7 @@ public:
|
|||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
SearchResults ReadEntry(U64 &hashKey, int depth, int alpha, int beta);
|
TTEntry ReadEntry(U64 &hashKey, int depth, int alpha, int beta);
|
||||||
|
|
||||||
void WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move besteMove);
|
void WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move besteMove);
|
||||||
};
|
};
|
||||||
+80
-25
@@ -1,9 +1,9 @@
|
|||||||
#include "uci.h"
|
#include "uci.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <algorithm>
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
#include <tuple>
|
||||||
|
#include <memory>
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
#include "movegen.h"
|
#include "movegen.h"
|
||||||
#include "search.h"
|
#include "search.h"
|
||||||
@@ -12,7 +12,14 @@
|
|||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include "datagen.h"
|
#include "datagen.h"
|
||||||
|
|
||||||
static void ParsePosition(Board &board, std::string_view command, SEARCH::SearchContext& ctx) {
|
// OS-dependent threading includes
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <thread>
|
||||||
|
#else
|
||||||
|
#include <pthread.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static void ParsePosition(Board &board, std::string_view command, SEARCH::SearchContext* ctx) {
|
||||||
if (command.find("startpos") != std::string::npos) {
|
if (command.find("startpos") != std::string::npos) {
|
||||||
board.SetByFen(StartingFen);
|
board.SetByFen(StartingFen);
|
||||||
}
|
}
|
||||||
@@ -32,8 +39,8 @@ static void ParsePosition(Board &board, std::string_view command, SEARCH::Search
|
|||||||
std::vector<std::string> moves = UTILS::split(command.substr(movesIndex + 6, command.length() - (movesIndex + 5)), ' ');
|
std::vector<std::string> moves = UTILS::split(command.substr(movesIndex + 6, command.length() - (movesIndex + 5)), ' ');
|
||||||
|
|
||||||
for (std::string_view move : moves) {
|
for (std::string_view move : moves) {
|
||||||
bool isLegal = board.MakeMove(UTILS::parseMove(board, move));
|
board.MakeMove(UTILS::parseMove(board, move));
|
||||||
ctx.positionHistory[board.positionIndex] = board.hashKey;
|
ctx->positionHistory[board.positionIndex] = board.hashKey;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,7 +76,56 @@ static int ReadParam(std::string param, std::string &command) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext& ctx) {
|
#ifdef _WIN32
|
||||||
|
// Windows implementation using std::thread
|
||||||
|
template <SEARCH::searchMode mode>
|
||||||
|
static void ThreadFunc(Board* board, SearchParams params, SEARCH::SearchContext* ctx) {
|
||||||
|
SEARCH::SearchPosition<mode>(*board, params, ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void StartSearchThread(Board& board, SearchParams params, SEARCH::SearchContext* ctx) {
|
||||||
|
std::thread searchThread;
|
||||||
|
|
||||||
|
if (params.nodes) {
|
||||||
|
searchThread = std::thread(ThreadFunc<SEARCH::nodesMode>, &board, params, ctx);
|
||||||
|
} else {
|
||||||
|
searchThread = std::thread(ThreadFunc<SEARCH::normal>, &board, params, ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
searchThread.detach();
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
// Unix/Linux implementation using pthread
|
||||||
|
template <SEARCH::searchMode mode>
|
||||||
|
static void* ThreadFunc(void* arg) {
|
||||||
|
auto* tup = static_cast<std::tuple<Board*, SearchParams, SEARCH::SearchContext*>*>(arg);
|
||||||
|
SEARCH::SearchPosition<mode>(*std::get<0>(*tup), std::get<1>(*tup), std::get<2>(*tup));
|
||||||
|
delete tup;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void StartSearchThread(Board& board, SearchParams params, SEARCH::SearchContext* ctx) {
|
||||||
|
pthread_attr_t attr;
|
||||||
|
pthread_attr_init(&attr);
|
||||||
|
pthread_attr_setstacksize(&attr, 8 * 1024 * 1024); // 8 MB stack
|
||||||
|
|
||||||
|
pthread_t thread;
|
||||||
|
|
||||||
|
if (params.nodes) {
|
||||||
|
auto* arg = new std::tuple<Board*, SearchParams, SEARCH::SearchContext*>(&board, params, ctx);
|
||||||
|
pthread_create(&thread, &attr, ThreadFunc<SEARCH::nodesMode>, arg);
|
||||||
|
} else {
|
||||||
|
auto* arg = new std::tuple<Board*, SearchParams, SEARCH::SearchContext*>(&board, params, ctx);
|
||||||
|
pthread_create(&thread, &attr, ThreadFunc<SEARCH::normal>, arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
pthread_attr_destroy(&attr);
|
||||||
|
pthread_detach(thread);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext* ctx) {
|
||||||
SearchParams params;
|
SearchParams params;
|
||||||
|
|
||||||
params.wtime = ReadParam("wtime", command);
|
params.wtime = ReadParam("wtime", command);
|
||||||
@@ -82,26 +138,23 @@ static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext& c
|
|||||||
if (command.find("movetime") != std::string::npos) {
|
if (command.find("movetime") != std::string::npos) {
|
||||||
params.wtime = ReadParam("movetime", command);
|
params.wtime = ReadParam("movetime", command);
|
||||||
params.btime = ReadParam("movetime", command);
|
params.btime = ReadParam("movetime", command);
|
||||||
|
} else if (command.find("infinite") != std::string::npos) {
|
||||||
|
params.wtime = 99999999;
|
||||||
|
params.btime = 99999999;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (params.nodes) {
|
StartSearchThread(board, params, ctx);
|
||||||
auto worker = std::thread(SEARCH::SearchPosition<SEARCH::nodesMode>, std::ref(board), params, std::ref(ctx));
|
|
||||||
worker.detach();
|
|
||||||
} else {
|
|
||||||
auto worker = std::thread(SEARCH::SearchPosition<SEARCH::normal>, std::ref(board), params, std::ref(ctx));
|
|
||||||
worker.detach();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void SetOption(std::string &command, SEARCH::SearchContext& ctx) {
|
static void SetOption(std::string &command, SEARCH::SearchContext* ctx) {
|
||||||
if (command.find("Hash") != std::string::npos) {
|
if (command.find("Hash") != std::string::npos) {
|
||||||
U64 hashSize = (ReadParam("value", command) * 1000000) / sizeof(TTEntry);
|
U64 hashSize = (ReadParam("value", command) * 1000000) / sizeof(TTEntry);
|
||||||
ctx.TT.Resize(hashSize);
|
ctx->TT.Resize(hashSize);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void PrintEngineInfo() {
|
static void PrintEngineInfo() {
|
||||||
std::cout << "id name Eleanor v1.0" << std::endl;
|
std::cout << "id name Eleanor v2.0" << std::endl;
|
||||||
std::cout << "id author rektdie" << std::endl;
|
std::cout << "id author rektdie" << std::endl;
|
||||||
std::cout << "option name Hash type spin default 8 min 1 max 1024" << std::endl;
|
std::cout << "option name Hash type spin default 8 min 1 max 1024" << std::endl;
|
||||||
std::cout << "option name Threads type spin default 1 min 1 max 1" << std::endl;
|
std::cout << "option name Threads type spin default 1 min 1 max 1" << std::endl;
|
||||||
@@ -111,7 +164,7 @@ static void PrintEngineInfo() {
|
|||||||
void UCILoop(Board &board) {
|
void UCILoop(Board &board) {
|
||||||
std::string input = "";
|
std::string input = "";
|
||||||
|
|
||||||
SEARCH::SearchContext ctx;
|
auto ctx = std::make_unique<SEARCH::SearchContext>();
|
||||||
|
|
||||||
// main loop
|
// main loop
|
||||||
while (true) {
|
while (true) {
|
||||||
@@ -125,7 +178,7 @@ void UCILoop(Board &board) {
|
|||||||
|
|
||||||
// parse UCI "position" command
|
// parse UCI "position" command
|
||||||
if (input.find("position") != std::string::npos) {
|
if (input.find("position") != std::string::npos) {
|
||||||
ParsePosition(board, input, ctx);
|
ParsePosition(board, input, ctx.get());
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -135,18 +188,20 @@ void UCILoop(Board &board) {
|
|||||||
board.SetByFen(StartingFen);
|
board.SetByFen(StartingFen);
|
||||||
|
|
||||||
// Clearing
|
// Clearing
|
||||||
ctx.TT.Clear();
|
ctx->TT.Clear();
|
||||||
|
|
||||||
ctx.killerMoves = {};
|
ctx->killerMoves = {};
|
||||||
ctx.history.Clear();
|
ctx->history.Clear();
|
||||||
ctx.positionHistory = {};
|
ctx->conthist.Clear();
|
||||||
|
ctx->positionHistory = {};
|
||||||
|
ctx->ss = {};
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse UCI "go" command
|
// parse UCI "go" command
|
||||||
if (input.find("go") != std::string::npos) {
|
if (input.find("go") != std::string::npos) {
|
||||||
ParseGo(board, input, ctx);
|
ParseGo(board, input, ctx.get());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,7 +214,7 @@ void UCILoop(Board &board) {
|
|||||||
// parse UCI "stop" command
|
// parse UCI "stop" command
|
||||||
if (input.find("stop") != std::string::npos) {
|
if (input.find("stop") != std::string::npos) {
|
||||||
// stop the loop
|
// stop the loop
|
||||||
ctx.searchStopped = true;
|
ctx->searchStopped = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -171,7 +226,7 @@ void UCILoop(Board &board) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (input.find("setoption") != std::string::npos) {
|
if (input.find("setoption") != std::string::npos) {
|
||||||
SetOption(input, ctx);
|
SetOption(input, ctx.get());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user