Skip to content

Commit

Permalink
Merge pull request #52 from SzilBalazs/dev
Browse files Browse the repository at this point in the history
BlackCore v3.0
Bench: 2870227
  • Loading branch information
SzilBalazs authored Nov 9, 2022
2 parents e9bcf29 + dc27599 commit 3224ee0
Show file tree
Hide file tree
Showing 34 changed files with 1,168 additions and 741 deletions.
65 changes: 65 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
BasedOnStyle: LLVM
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: None
AlignOperands: Align
AllowAllArgumentsOnNextLine: false
AllowAllConstructorInitializersOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Always
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Always
AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterReturnType: None
AlwaysBreakTemplateDeclarations: Yes
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterUnion: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: true
BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
ColumnLimit: 0
CompactNamespaces: false
ContinuationIndentWidth: 8
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth: 4
KeepEmptyLinesAtTheStartOfBlocks: true
MaxEmptyLinesToKeep: 2
NamespaceIndentation: All
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PointerAlignment: Right
ReflowComments: false
SpaceAfterCStyleCast: true
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 0
SpacesInAngles: Never
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
TabWidth: 4
UseTab: Never
56 changes: 28 additions & 28 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,34 @@ jobs:
strategy:
matrix:
config:
- {
name: "Ubuntu g++ popcnt",
os: ubuntu-latest,
compiler: g++-11,
arch: popcnt,
target: BlackCore-popcnt-linux,
}
- {
name: "Ubuntu g++ modern",
os: ubuntu-latest,
compiler: g++-11,
arch: modern,
target: BlackCore-modern-linux,
}
- {
name: "Ubuntu g++ avx2",
os: ubuntu-latest,
compiler: g++-11,
arch: avx2,
target: BlackCore-avx2-linux,
}
- {
name: "Ubuntu g++ bmi2",
os: ubuntu-latest,
compiler: g++-11,
arch: bmi2,
target: BlackCore-bmi2-linux,
}
#- {
# name: "Ubuntu g++ popcnt",
# os: ubuntu-latest,
# compiler: g++-11,
# arch: popcnt,
# target: BlackCore-popcnt-linux,
#}
#- {
# name: "Ubuntu g++ modern",
# os: ubuntu-latest,
# compiler: g++-11,
# arch: modern,
# target: BlackCore-modern-linux,
#}
#- {
# name: "Ubuntu g++ avx2",
# os: ubuntu-latest,
# compiler: g++-11,
# arch: avx2,
# target: BlackCore-avx2-linux,
#}
#- {
# name: "Ubuntu g++ bmi2",
# os: ubuntu-latest,
# compiler: g++-11,
# arch: bmi2,
# target: BlackCore-bmi2-linux,
#}
- {
name: "Windows g++ popcnt",
os: windows-latest,
Expand Down
7 changes: 3 additions & 4 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ CXX = g++
TARGET_FLAGS = -static -static-libgcc -static-libstdc++
ARCH=native
NAME = BlackCore
VERSION_MAJOR = 2
VERSION_MAJOR = 3
VERSION_MINOR = 0
OBJECT_DIR = objects
SOURCES := $(wildcard *.cpp)
Expand All @@ -14,7 +14,6 @@ else
uname_S := $(shell uname -s)
endif


ifeq ($(uname_S), Windows)
SUFFIX = .exe
else
Expand Down Expand Up @@ -44,8 +43,8 @@ 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)
CXXFLAGS = -std=c++20 -O3 -flto -pthread -Wall -Wno-class-memaccess -DVERSION=\"v$(VERSION_MAJOR).$(VERSION_MINOR)\" $(DEFINE_FLAGS) $(ARCH_FLAGS)
EXE = $(NAME)-v$(VERSION_MAJOR)-$(VERSION_MINOR)$(SUFFIX)

default: $(EXE)

Expand Down
51 changes: 29 additions & 22 deletions src/bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#include <string>
#include <chrono>
#include <iostream>
#include <functional>
#include "bench.h"
#include "tt.h"
#include "search.h"
#include "timeman.h"
#include "tt.h"
#include <chrono>
#include <functional>
#include <iostream>
#include <string>

struct TestPosition {
std::string fen;
Expand All @@ -31,25 +31,24 @@ struct TestPosition {

const unsigned int posCount = 10;

const unsigned int searchTestHashSize = 16;
const unsigned int searchTestHashSize = 32;

const Depth SEARCH_DEPTH = 13;
const Depth SEARCH_DEPTH = 15;

const TestPosition testPositions[posCount] = {
// Positions from CPW
{"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1 ", 6, 119060324},
{"r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - ", 5, 193690690},
{"8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - -", 7, 178633661},
{"r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 1", 5, 15833292},
{"r2q1rk1/pP1p2pp/Q4n2/bbp1p3/Np6/1B3NBn/pPPP1PPP/R3K2R b KQ - 0 1 ", 5, 15833292},
{"rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8 ", 5, 89941194},
{"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1 ", 6, 119060324},
{"r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - ", 5, 193690690},
{"8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - -", 7, 178633661},
{"r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 1", 5, 15833292},
{"r2q1rk1/pP1p2pp/Q4n2/bbp1p3/Np6/1B3NBn/pPPP1PPP/R3K2R b KQ - 0 1 ", 5, 15833292},
{"rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8 ", 5, 89941194},
{"r4rk1/1pp1qppp/p1np1n2/2b1p1B1/2B1P1b1/P1NP1N2/1PP1QPPP/R4RK1 w - - 0 10 ", 5, 164075551},

// Own positions
{"r3kb1r/1p3ppp/pqn1pn2/1Bpp1b2/3P1B2/1QP1PN2/PP1N1PPP/R3K2R w KQkq - 0 9", 5, 140824446},
{"rnb1k2r/pppp1ppp/5q2/2b5/2BNP3/2N5/PPP2KPP/R1BQ3R w kq - 1 8", 5, 19782759},
{"8/pp5p/8/2p2kp1/2Pp4/3P1KPP/PP6/8 w - - 0 32", 7, 13312960}
};
{"r3kb1r/1p3ppp/pqn1pn2/1Bpp1b2/3P1B2/1QP1PN2/PP1N1PPP/R3K2R w KQkq - 0 9", 5, 140824446},
{"rnb1k2r/pppp1ppp/5q2/2b5/2BNP3/2N5/PPP2KPP/R1BQ3R w kq - 1 8", 5, 19782759},
{"8/pp5p/8/2p2kp1/2Pp4/3P1KPP/PP6/8 w - - 0 32", 7, 13312960}};

void testPerft() {
initSearch();
Expand All @@ -71,7 +70,8 @@ void testPerft() {
U64 nps = totalNodes * 1000 / elapsedTime;

if (ok) {
std::cout << "PERFT OK\n" << totalNodes << " nodes " << nps << " nps" << std::endl;
std::cout << "PERFT OK\n"
<< totalNodes << " nodes " << nps << " nps" << std::endl;
} else {
std::cout << "PERFT FAILED" << std::endl;
exit(1);
Expand All @@ -82,13 +82,20 @@ void testSearch() {
initSearch();
ttResize(searchTestHashSize);

startSearch(0, 0, 0, 0);
U64 totalNodes = 0;
U64 nps;

for (const TestPosition &tPos : testPositions) {
ttClear();
Position pos = {tPos.fen};
std::atomic<bool> a;
iterativeDeepening(pos, SEARCH_DEPTH, false, std::ref(a));
SearchInfo info;
info.maxDepth = SEARCH_DEPTH;
info.uciMode = false;
startSearch(info, pos, 1);
joinThread(true);
totalNodes += nodeCount;
nps += getNps();
}

std::cout << nodeCount << " nodes " << getNps() << " nps" << std::endl;
std::cout << totalNodes << " nodes " << nps / posCount << " nps" << std::endl;
}
7 changes: 5 additions & 2 deletions src/bench.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@

#include "movegen.h"

#include <iostream>

template<bool output>
U64 perft(Position &position, Depth depth) {
Move moves[200];
Move *movesEnd = generateMoves(position, moves, false);
if (depth == 1) return movesEnd - moves;
if (depth == 1)
return movesEnd - moves;
U64 nodes = 0;
for (Move *it = moves; it != movesEnd; it++) {
position.makeMove(*it);
Expand All @@ -42,4 +45,4 @@ void testPerft();

void testSearch();

#endif //BLACKCORE_BENCH_H
#endif//BLACKCORE_BENCH_H
24 changes: 14 additions & 10 deletions src/bitboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#include <iostream>
#include <cstring>
#include "bitboard.h"
#include "utils.h"
#include <cstring>
#include <iostream>

Bitboard bitMasks[64], pawnMasks[64][2], knightMasks[64], kingMasks[64], fileMasks[64], rankMasks[64], rookMasks[64], diagonalMasks[64], antiDiagonalMasks[64], bishopMasks[64],
rookAttackTable[102400], bishopAttackTable[5248], commonRay[64][64], adjacentFileMasks[64], adjacentNorthMasks[64], adjacentSouthMasks[64];
Bitboard bitMasks[64], pawnMasks[64][2], knightMasks[64], kingMasks[64], fileMasks[64], rankMasks[64], rookMasks[64],
diagonalMasks[64], antiDiagonalMasks[64], bishopMasks[64],
rookAttackTable[102400], bishopAttackTable[5248], commonRay[64][64], adjacentFileMasks[64], adjacentNorthMasks[64],
adjacentSouthMasks[64];
LineType lineType[64][64];

void initBitboard() {
Expand Down Expand Up @@ -67,7 +69,8 @@ void initBitboard() {
~fileMask(sq) & (adjacentNorthMasks[sq] | adjacentSouthMasks[sq] | step<WEST>(sq) | step<EAST>(sq));

for (Square sq2 = A1; sq2 < 64; sq2 += 1) {
if (sq == sq2) continue;
if (sq == sq2)
continue;
for (Direction dir : DIRECTIONS) {
Bitboard value = slide(dir, sq) & slide(-dir, sq2);

Expand Down Expand Up @@ -101,7 +104,6 @@ void initBitboard() {

initMagic(rookMagics, ROOK);
initMagic(bishopMagics, BISHOP);

}

Bitboard slidingAttacks(Square square, Bitboard occupied, PieceType type) {
Expand Down Expand Up @@ -160,8 +162,10 @@ void findMagics(Bitboard *attackTable, Magic *magics, PieceType type) {
magic.mask = slidingAttacks(square, 0, type) & ~edge;
magic.shift = magic.mask.popCount();

if (square == A1) magic.ptr = attackTable;
else magic.ptr = magics[square - 1].ptr + length;
if (square == A1)
magic.ptr = attackTable;
else
magic.ptr = magics[square - 1].ptr + length;

// Carry-Ripler trick for reference check out: https://www.chessprogramming.org/Traversing_Subsets_of_a_Set
length = 0;
Expand All @@ -178,7 +182,8 @@ void findMagics(Bitboard *attackTable, Magic *magics, PieceType type) {

while (true) {
magic.magic = randBB() & randBB() & randBB();
if (((magic.magic * magic.mask) >> 56).popCount() < 6) continue;
if (((magic.magic * magic.mask) >> 56).popCount() < 6)
continue;

std::memset(used, 0, sizeof(used));

Expand All @@ -204,7 +209,6 @@ void findMagics(Bitboard *attackTable, Magic *magics, PieceType type) {
std::cout << " {bishopAttackTable + " << magic.ptr - bishopAttackTable << ", " << BBToHex(magic.mask)
<< ", "
<< BBToHex(magic.magic) << ", " << magic.shift << "},\n";

}

std::cout << "};\n";
Expand Down
Loading

0 comments on commit 3224ee0

Please sign in to comment.