Skip to content

Commit

Permalink
Merge pull request #33 from SzilBalazs/v1-prep
Browse files Browse the repository at this point in the history
V1 preparation
  • Loading branch information
SzilBalazs authored Aug 27, 2022
2 parents 1196816 + f0b628a commit 98b55c9
Show file tree
Hide file tree
Showing 10 changed files with 345 additions and 62 deletions.
103 changes: 82 additions & 21 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,89 @@
name: Perft test and benchmark
name: Build

on:
push:
branches: [ "master" , "dev"]
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
build:

runs-on: ubuntu-latest
BlackCore:
name: ${{matrix.config.name}}
runs-on: ${{matrix.config.os}}
strategy:
matrix:
config:
- {
name: "Ubuntu g++ popcnt",
os: ubuntu-latest,
compiler: g++-11,
arch: popcnt,
target: BlackCore-popcnt-linux,
upload_target: true,
}
- {
name: "Ubuntu g++ modern",
os: ubuntu-latest,
compiler: g++-11,
arch: modern,
target: BlackCore-modern-linux,
upload_target: true,
}
- {
name: "Ubuntu g++ bmi2",
os: ubuntu-latest,
compiler: g++-11,
arch: bmi2,
target: BlackCore-bmi2-linux,
upload_target: true,
}
- {
name: "Windows g++ popcnt",
os: windows-latest,
compiler: g++,
arch: popcnt,
target: BlackCore-popcnt-win.exe,
upload_target: true,
}
- {
name: "Windows g++ modern",
os: windows-latest,
compiler: g++,
arch: popcnt,
target: BlackCore-modern-win.exe,
upload_target: true,
}
- {
name: "Windows g++ bmi2",
os: windows-latest,
compiler: g++,
arch: bmi2,
target: BlackCore-bmi2-win.exe,
upload_target: true,
}

steps:
- name: Updating g++
run: |
sudo apt-get update
sudo apt install g++-10
g++ --version
make --version
- uses: actions/checkout@v3
- name: Making BlackCore
run:
make -C src CXX=g++-10
- name: Running perft test
run:
make -C src perft CXX=g++10
- name: Running benchmark
run:
make -C src bench CXX=g++10
- uses: actions/checkout@v3
- name: Installing packages
if: runner.os == 'Linux'
run: |
sudo apt-get update
sudo apt-get install g++-11
- name: Making BlackCore
run: |
cd src
make CXX=${{matrix.config.compiler}} EXE=${{matrix.config.target}} ARCH=${{matrix.config.target}}
- name: Running perft
run: |
cd src
./${{matrix.config.target}} perft
- name: Running bench
run: |
cd src
./${{matrix.config.target}} bench
- name: Upload artifact
if: ${{matrix.config.upload_target}}
uses: actions/upload-artifact@v3
with:
name: BlackCore
path: src/${{matrix.config.target}}
155 changes: 155 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<h1 align="center">BlackCore</h1>
<div align="center">
<img src="https://github.com/SzilBalazs/BlackCore/actions/workflows/build.yml/badge.svg" alt="Test status">
</div>

## Overview

BlackCore is a UCI compatible c++ chess engine written from scratch, capable of completing high performance tasks.
Its alpha beta search uses various pruning techniques, powered by a handcrafted evaluation function and a blazing fast
move generator.

## Files

This project contains the following files:

- **README.md** the file that you are reading.
- **LICENSE** containing the license of this repository.
- **.github** folder contains automated GitHub workflows like building this project.
- **src** folder contains the source code of BlackCore

## Features

* UCI support
* Perft test
* Up to ~230M nps
* Intel i3-7100 3.9Ghz CPU
* Single-threaded
* Hashing disabled
* Benchmark
* Fixed depth search on a set of custom positions
* 16 bit encoded moves
* Bitboard representation
* Engine
* Search
* Iterative deepening
* Alpha-Beta
* Negamax
* Transposition table
* Cut-offs
* Entry aging
* Bucket system
* 1 always replace
* 1 depth preferred
* Principal variation search
* Late move pruning
* R = LMR_BASE + (sqrt(index - 1) + sqrt(depth - 1)) / LMR_SCALE
* Razoring
* Dropping into qsearch at frontier nodes
* Reverse futility pruning
* With improving detection
* Null move pruning
* Reduction depends on depth searched
* Quintessence search
* Stand-pat
* Delta pruning
* Static-exchange-evaluation pruning
* Move ordering
* Hash move
* Promotions
* Under promotions
* Captures
* MVV-LVA
* SEE - currently disabled
* Quiet moves
* Killer heuristic
* History heuristic
* Fast repetition detection
* Time management
* Sudden death
* Increment per move
* Move-time
* Moves to go support
* Handcrafted evaluation
* Tapered eval
* Mid-game and end-game
* Material balance
* Pawn structure
* Double pawns
* Isolated pawns
* Passed pawns
* King safety
* Pawn king shield
* Bonus for castled king
* Trapped rooks restricted by uncastled king
* Knight mobility
* Restricted by enemy pawns
* Bishop mobility
* Depending on the pawn structure
* Rooks
* Mobility
* Bonuses for open and half open files
* Tempo

## Installation

### Building from source

After downloading the sources (preferably the source of the latest release) you can run the following commands, to build
a native binary.
BlackCore uses c++20 standard, so older versions of compilers might not work.

```
cd src
make clean build CXX=g++-11 ARCH=native
```

ARCH = popcnt/modern/bmi2/native

CXX = the compiler of your choice (I recommend using g++, as it gives the best performance)

### Downloading prebuilt binary

You can download the latest release <a href="https://github.com/SzilBalazs/BlackCore/releases/latest">here</a> both for
Windows and Linux.
To select the right binary, choose the bmi2 build if you have a fairly new CPU, otherwise you can use the modern build
or in case of an older
processor use the popcnt build. Only 64 bits CPUs with popcnt are supported at the moment.

## Usage

BlackCore in itself is a command line program, and requires a UCI compatible
Chess GUI (like <a href="https://github.com/cutechess/cutechess">Cute Chess</a>
or <a href="http://www.playwitharena.de/">Arena</a>) for the best user experience.

### UCI Options

- **Hash** - The size of the Hash table in MB.
- **Threads** - Currently BlackCore only supports single threaded search, but this will probably change in the future.
- **Move Overhead** - The delay (in ms) between finding the best move and the GUI reacting to it. You may want to make
this
higher if you notice that the engine often runs out of time.

## Big thanks to

### <a href="https://www.chessprogramming.org/Main_Page">Chess Programming Wiki</a>

The Chess Programming Wiki is the greatest
resource for everybody who wants to be informed about the basics and the state-of-the-art technologies of chess
programming.

### <a href="https://github.com/AndyGrant/OpenBench">OpenBench</a> by Andrew Grant

OpenBench is an usefull SPRT testing framework, which contributed
to the development of BlackCore substantially.

### <a href="https://github.com/official-stockfish/Stockfish">StockFish</a> by The StockFish team

Thanks to the StockFish team for making such a wonderful and an easy-to-read codebase, that inspired me to get into
chess programming in the first place.

### <a href="https://github.com/Disservin/Smallbrain">Smallbrain</a> by <a href="https://github.com/Disservin">Disservin</a>

Smallbrain is a great chess engine which helped me understand many important concepts, and thanks to Disservin for
giving me many great ideas how can I further improve my engine.

60 changes: 41 additions & 19 deletions src/Makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,52 @@
CXX = g++
CXXFLAGS = -std=c++20 -Ofast -march=native -flto=auto -pthread -Wall
TARGET_FLAGS = -static -static-libgcc -static-libstdc++
ARCH=native
NAME = BlackCore
VERSION = _v0
EXE = $(NAME)$(VERSION)

VERSION_MAJOR = 1
VERSION_MINOR = 0
OBJECT_DIR = objects

SOURCES := $(wildcard *.cpp)
OBJECTS := $(patsubst %.cpp, $(OBJECT_DIR)/%.o, $(SOURCES))

ifeq ($(OS),Windows_NT)
uname_S := Windows
else
uname_S := $(shell uname -s)
endif


ifeq ($(uname_S), Windows)
SUFFIX = .exe
else
SUFFIX =
endif

ifeq ($(ARCH), native)
ARCH_FLAGS=-march=native
DEFINE_FLAGS=-DNATIVE
endif

ifeq ($(ARCH), bmi2)
ARCH_FLAGS = -march=x86-64 -mpopcnt -msse -msse2 -msse3 -mssse3 -msse4.1 -mbmi2
DEFINE_FLAGS = -DBMI2
endif

ifeq ($(ARCH), modern)
ARCH_FLAGS = -march=x86-64 -mpopcnt -msse -msse2 -msse3 -mssse3 -msse4.1
endif

ifeq ($(ARCH), popcnt)
ARCH_FLAGS = -march=x86-64 -mpopcnt
endif

CXXFLAGS = -std=c++20 -O3 -flto -pthread -Wall -Wno-class-memaccess $(DEFINE_FLAGS) $(ARCH_FLAGS)
EXE = $(NAME)_v$(VERSION_MAJOR)-$(VERSION_MINOR)$(SUFFIX)

default: $(EXE)

$(EXE): $(OBJECTS)
@echo Linking $(NAME)
@$(CXX) $(CXXFLAGS) -o $@ $^
@$(CXX) $(TARGET_FLAGS) $(CXXFLAGS) -o $@ $^
@echo Build has finished.

$(OBJECTS): | $(OBJECT_DIR)
Expand All @@ -25,22 +58,11 @@ $(OBJECT_DIR)/%.o: %.cpp
@echo Compiling $<
@$(CXX) $(CXXFLAGS) -c -o $@ $<

all: build perft bench
all: clean build

build: $(EXE)

uci: $(EXE)
./$(EXE) uci

perft: $(EXE)
@RESULT=$$(./$(EXE) perft) && \
echo "$$RESULT" && \
echo $$RESULT | grep -q "PERFT OK"

bench: $(EXE)
@./$(EXE) bench

clean:
@rm -r objects $(EXE) || true

.PHONY: all build uci perft bench clean
.PHONY: all build clean
3 changes: 2 additions & 1 deletion src/bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const unsigned int posCount = 10;

const unsigned int searchTestHashSize = 8;

const Depth SEARCH_DEPTH = 11;
const Depth SEARCH_DEPTH = 13;

const TestPosition testPositions[posCount] = {
// Positions from CPW
Expand Down Expand Up @@ -86,6 +86,7 @@ void testPerft() {
std::cout << "PERFT OK\n" << totalNodes << " nodes " << nps << " nps" << std::endl;
} else {
std::cout << "PERFT FAILED" << std::endl;
exit(1);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/bitboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void initBitboard() {

if (value) {
commonRay[sq][sq2] = value;
LineType type;
LineType type=HORIZONTAL;
switch (dir) {
case NORTH:
case SOUTH:
Expand Down Expand Up @@ -132,7 +132,7 @@ void initMagic(const Magic *magics, PieceType type) {
} while (occ != 0);

for (unsigned int i = 0; i < length; i++) {
U64 index = (((occupied[i] & magic.mask) * magic.magic) >> (64 - magic.shift)).bb;
U64 index = getMagicIndex(magic, occupied[i]);

magic.ptr[index] = attacked[i];
}
Expand Down
Loading

0 comments on commit 98b55c9

Please sign in to comment.