Skip to content

Commit

Permalink
Gull Syzygy Patch.
Browse files Browse the repository at this point in the history
This patch adds the Syzygy TB to Gull using Fathom
(https://github.com/basil00/Fathom).

Tested with 5-man syzygy (4000 games) using 60"+0.4" 2-threads:
Score: +673-565=2762
Winning fraction: 0.5135
Elo difference: +9.38304
LOS: 0.998928
  • Loading branch information
basil00 committed Nov 18, 2015
1 parent f59422d commit 8ab40a7
Show file tree
Hide file tree
Showing 4 changed files with 300 additions and 10 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
Gull 3 Chess (LINUX and MACOSX)
===============================
Gull 3 Chess (LINUX and MACOSX and SYZYGY)
==========================================

This is a Linux/MacOSX port of the Gull chess engine version 3!

To build simply run `make` (from a terminal) in the `src` directory.
Use `make CC=g++` to use `g++` instead of the default `clang++`.

To build syzygy Gull run `make syzygy` (from a terminal) in the `src`
directory. Note that the `Makefile` uses `wget` to automatically download
the Fathom dependency (https://github.com/basil00/Fathom).

Pre-built binaries are available here: https://github.com/basil00/Gull/releases

Expand Down
139 changes: 134 additions & 5 deletions src/Gull.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ const uint8 UpdateCastling[64] = {
0xFF^CanCastle_ooo,0xFF,0xFF,0xFF,0xFF^(CanCastle_oo|CanCastle_ooo),0xFF,0xFF,0xFF^CanCastle_oo
};

const uint64 BMagic[64] = {
extern const uint64 BMagic[64] = {
0x0048610528020080, 0x00c4100212410004, 0x0004180181002010, 0x0004040188108502,
0x0012021008003040, 0x0002900420228000, 0x0080808410c00100, 0x000600410c500622,
0x00c0056084140184, 0x0080608816830050, 0x00a010050200b0c0, 0x0000510400800181,
Expand All @@ -186,7 +186,7 @@ const uint64 BMagic[64] = {
0x000008021020a200, 0x0000414128092100, 0x0000042002024200, 0x0002081204004200
};

const uint64 RMagic[64] = {
extern const uint64 RMagic[64] = {
0x00800011400080a6, 0x004000100120004e, 0x0080100008600082, 0x0080080016500080,
0x0080040008000280, 0x0080020005040080, 0x0080108046000100, 0x0080010000204080,
0x0010800424400082, 0x00004002c8201000, 0x000c802000100080, 0x00810010002100b8,
Expand All @@ -205,7 +205,7 @@ const uint64 RMagic[64] = {
0x0011001800021025, 0x00c9000400620811, 0x0032009001080224, 0x001400810044086a
};

const int BShift[64] = {
extern const int BShift[64] = {
58, 59, 59, 59, 59, 59, 59, 58,
59, 59, 59, 59, 59, 59, 59, 59,
59, 59, 57, 57, 57, 57, 59, 59,
Expand All @@ -227,7 +227,7 @@ const int BOffset[64] = {
4928, 4992, 5024, 5056, 5088, 5120, 5152, 5184
};

const int RShift[64] = {
extern const int RShift[64] = {
52, 53, 53, 53, 53, 53, 53, 52,
53, 54, 54, 54, 54, 54, 54, 53,
53, 54, 54, 54, 54, 54, 54, 53,
Expand Down Expand Up @@ -356,6 +356,12 @@ char GullCppFile[16384][256];
#define KpkValue 300
#define EvalValue 30000
#define MateValue 32760
#ifdef TB
#define TBMateValue 30000
#define TBCursedMateValue 3
const int TbValues[5] = {-TBMateValue, -TBCursedMateValue, 0, TBCursedMateValue, TBMateValue};
#define TbDepth (depth+6>=127?127:depth+6)
#endif

/*
general move:
Expand Down Expand Up @@ -995,6 +1001,9 @@ typedef struct {
volatile sint64 hash_size;
volatile int PrN;
GSP Sp[MaxSplitPoints];
#if TB
char tb_path[1024];
#endif
} GSMPI;

#define SharedMaterialOffset (sizeof(GSMPI))
Expand Down Expand Up @@ -1089,6 +1098,10 @@ void check_state();
int input();
void uci();

#ifdef TB
#include "tbprobe.h"
#endif

#include "Linux.cpp"

#ifdef TUNER
Expand Down Expand Up @@ -4774,6 +4787,7 @@ template <bool me, bool pv> int q_search(int alpha, int beta, int depth, int fla
#endif
}
#endif

if (flags & FlagCallEvaluation) evaluate();
if (Check(me)) return q_evasion<me, pv>(alpha, beta, depth, FlagHashCheck);
score = Current->score + 3;
Expand Down Expand Up @@ -5220,7 +5234,29 @@ template <bool me, bool exclusion> int search(int beta, int depth, int flags) {
if (F(flags & FlagReturnBestMove)) return Entry->low;
}
}
if (depth >= 20) if (GPVEntry * PVEntry = probe_pv_hash()) {

#if TB
if (hash_depth < 0 && TB_LARGEST > 0 && popcnt(PieceAll) <= TB_LARGEST) {
unsigned res = tb_probe_wdl(Piece(White), Piece(Black),
King(White) | King(Black),
Queen(White) | Queen(Black),
Rook(White) | Rook(Black),
Bishop(White) | Bishop(Black),
Knight(White) | Knight(Black),
Pawn(White) | Pawn(Black),
Current->ply,
Current->castle_flags,
Current->ep_square,
(me == White));
if (res != TB_RESULT_FAILED) {
hash_high(TbValues[res], TbDepth);
hash_low(0, TbValues[res], TbDepth);
return TbValues[res];
}
}
#endif

if (depth >= 20) if (GPVEntry * PVEntry = probe_pv_hash()) {
hash_low(PVEntry->move,PVEntry->value,PVEntry->depth);
hash_high(PVEntry->value,PVEntry->depth);
if (PVEntry->depth >= depth) {
Expand Down Expand Up @@ -5480,6 +5516,27 @@ template <bool me, bool exclusion> int search_evasion(int beta, int depth, int f
}
}

#if TB
if (hash_depth < 0 && TB_LARGEST > 0 && popcnt(PieceAll) <= TB_LARGEST) {
unsigned res = tb_probe_wdl(Piece(White), Piece(Black),
King(White) | King(Black),
Queen(White) | Queen(Black),
Rook(White) | Rook(Black),
Bishop(White) | Bishop(Black),
Knight(White) | Knight(Black),
Pawn(White) | Pawn(Black),
Current->ply,
Current->castle_flags,
Current->ep_square,
(me == White));
if (res != TB_RESULT_FAILED) {
hash_high(TbValues[res], TbDepth);
hash_low(0, TbValues[res], TbDepth);
return TbValues[res];
}
}
#endif

if (hash_depth >= depth && hash_value > -EvalValue) score = Min(beta - 1, Max(score, hash_value));
if (flags & FlagCallEvaluation) evaluate();

Expand Down Expand Up @@ -5849,6 +5906,48 @@ template <bool me> void root() {
#endif
memcpy(Data,Current,sizeof(GData));
Current = Data;

#ifdef TB
if (popcnt(PieceAll) <= TB_LARGEST) {
unsigned res = tb_probe_root(Piece(White), Piece(Black),
King(White) | King(Black),
Queen(White) | Queen(Black),
Rook(White) | Rook(Black),
Bishop(White) | Bishop(Black),
Knight(White) | Knight(Black),
Pawn(White) | Pawn(Black),
Current->ply,
Current->castle_flags,
Current->ep_square,
(me == White), NULL);
if (res != TB_RESULT_FAILED) {
best_score = TbValues[TB_GET_WDL(res)];
int flags = 0;
unsigned to = TB_GET_TO(res);
switch (TB_GET_PROMOTES(res)) {
case TB_PROMOTES_QUEEN:
flags |= FlagPQueen; break;
case TB_PROMOTES_ROOK:
flags |= FlagPRook; break;
case TB_PROMOTES_BISHOP:
flags |= (Bit(to) & LightArea? FlagPLight: FlagPDark);
case TB_PROMOTES_KNIGHT:
flags |= FlagPKnight; break;
default:
break;
}
best_move = (TB_GET_FROM(res) << 6) | to | flags;
char str[32];
move_to_string(best_move,str);
printf("info depth 1 seldepth 1 score cp %d nodes 1 nps 0 pv %s\n", best_score, str); // Fake PV
send_best_move();
Searching = 0;
if (MaxPrN > 1) ZERO_BIT_64(Smpi->searching, 0);
return;
}
}
#endif

evaluate();
gen_root_moves<me>();
if (PVN > 1) {
Expand Down Expand Up @@ -6671,10 +6770,18 @@ void uci() {
ptr = strchr(mstring, '\n');
if (ptr != NULL) *ptr = 0;
if (!strcmp(mstring, "uci")) {
#ifdef TB
#ifndef W32_BUILD
fprintf(stdout,"id name Gull 3 x64 (syzygy)\n");
#else
fprintf(stdout,"id name Gull 3 (syzygy)\n");
#endif
#else
#ifndef W32_BUILD
fprintf(stdout,"id name Gull 3 x64\n");
#else
fprintf(stdout,"id name Gull 3\n");
#endif
#endif
fprintf(stdout,"id author ThinkingALot\n");
#ifndef W32_BUILD
Expand All @@ -6697,6 +6804,9 @@ void uci() {
fprintf(stdout, "option name Threads type spin min 1 max %d default %d\n", Min(CPUs, MaxPrN), PrN);
#ifdef LARGE_PAGES
fprintf(stdout, "option name Large memory pages type check default true\n");
#endif
#ifdef TB
fprintf(stdout, "option name SyzygyPath type string default <empty>\n");
#endif
fprintf(stdout,"uciok\n");
if (F(Searching)) init_search(1);
Expand Down Expand Up @@ -6736,6 +6846,13 @@ void uci() {
ResetHash = 0;
longjmp(ResetJump, 1);
}
#ifdef TB
} else if (!memcmp(ptr, "SyzygyPath", 10)) {
ptr += 17;
strncpy(Smpi->tb_path,ptr,sizeof(Smpi->tb_path)-1);
ResetHash = 0;
longjmp(ResetJump, 1);
#endif
} else if (!memcmp(ptr, "MultiPV", 7)) {
ptr += 14;
PVN = atoi(ptr);
Expand Down Expand Up @@ -6877,11 +6994,19 @@ int main(int argc, char *argv[]) {
fflush(NULL);

if (parent)
#ifdef TB
#ifndef W32_BUILD
fprintf(stdout, "Gull 3 x64 (syzygy)\n");
#else
fprintf(stdout, "Gull 3 (syzygy)\n");
#endif
#else
#ifndef W32_BUILD
fprintf(stdout, "Gull 3 x64\n");
#else
fprintf(stdout, "Gull 3\n");
#endif
#endif

reset_jump:
#ifndef TUNER
Expand Down Expand Up @@ -6913,6 +7038,10 @@ int main(int argc, char *argv[]) {
if (ResetHash) init_hash();
init_search(0);

#ifdef TB
tb_init(Smpi->tb_path);
#endif

if (child) while (true) check_state();
if (parent) for (i = 1; i < PrN; i++) ChildPr[i] = CreateChildProcess(i);

Expand Down
39 changes: 37 additions & 2 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,54 @@ endif
ifeq ($(UNAME),Darwin)
TARGET=gull.macosx
endif
CC=clang++
ifeq ($(UNAME),Linux)
TARGET_SYZYGY=gull.syzygy.linux
endif
ifeq ($(UNAME),Darwin)
TARGET_SYZYGY=gull.syzygy.macosx
endif
CC=g++
STRIP=strip
CFLAGS=-msse4.1 -mpopcnt -fno-exceptions -fno-rtti -Wno-parentheses -O2
CFLAGS=-msse4.1 -mpopcnt -fno-exceptions -fno-rtti -Wno-parentheses -O3

main: $(TARGET)

syzygy: $(TARGET_SYZYGY)

gull.linux:
$(CC) $(CFLAGS) -D LINUX Gull.cpp -o Gull
$(STRIP) Gull
cp Gull Gull.linux

gull.syzygy.linux: tbprobe.o
$(CC) $(CFLAGS) -D LINUX -D TB Gull.cpp tbprobe.o -o Gull
$(STRIP) Gull
cp Gull Gull.syzygy.linux

gull.macosx:
$(CC) $(CFLAGS) -D MACOSX Gull.cpp -o Gull
$(STRIP) Gull
cp Gull Gull.macosx

gull.syzygy.macosx:
$(CC) $(CFLAGS) -D MACOSX -D TB Gull.cpp tbprobe.o -o Gull
$(STRIP) Gull
cp Gull Gull.syzygy.macosx

tbprobe.o: tbprobe.c tbcore.c
$(CC) $(CFLAGS) -c tbprobe.c

tbprobe.c: Fathom-master.zip
unzip -o -j Fathom-master.zip "Fathom-master/src/tbprobe.h" -d "./"
unzip -o -j Fathom-master.zip "Fathom-master/src/tbprobe.c" -d "./"

tbcore.c: Fathom-master.zip
unzip -o -j Fathom-master.zip "Fathom-master/src/tbcore.h" -d "./"
unzip -o -j Fathom-master.zip "Fathom-master/src/tbcore.c" -d "./"

Fathom-master.zip:
wget https://github.com/basil00/Fathom/archive/master.zip \
-O Fathom-master.zip

clean:
rm -f Gull
Expand Down
Loading

0 comments on commit 8ab40a7

Please sign in to comment.