Skip to content

Commit

Permalink
Refractor & Fix g++ warnings - #55
Browse files Browse the repository at this point in the history
Refractor & Fix g++ warnings

STC simplification:
ELO   | 2.28 +- 3.45 (95%)
SPRT  | 10.0+0.10s Threads=1 Hash=16MB
LLR   | 2.95 (-2.94, 2.94) [-3.00, 1.00]
GAMES | N: 19640 W: 5042 L: 4913 D: 9685

Bench: 2141682
  • Loading branch information
SzilBalazs authored Aug 10, 2023
2 parents 3fc67c8 + 5a42c62 commit cc9902b
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ endif
EVALFILE = weights/master.bin
TMP_EVALFILE = tmp.bin
DEFINE_FLAGS += -DVERSION=\"v$(VERSION_MAJOR).$(VERSION_MINOR).$(HASH)\" -DNDEBUG -D_CRT_SECURE_NO_WARNINGS
CXXFLAGS = $(DEFINE_FLAGS) $(ARCH_FLAGS) -std=c++20 -O3 -flto -pthread -Wall
CXXFLAGS = $(DEFINE_FLAGS) $(ARCH_FLAGS) -std=c++20 -O3 -flto=auto -pthread -Wall
EXE = $(NAME)
OUTPUT_BINARY = $(EXE)$(SUFFIX)
INCBIN_TOOL = incbin_tool$(SUFFIX)
Expand Down
6 changes: 3 additions & 3 deletions src/chess/bitboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ namespace chess {
// Used for referencing the 64 squares of a board using a 64-bit number
struct Bitboard {

U64 bb = 0;
uint64_t bb = 0;

constexpr Bitboard(U64 value) {
constexpr Bitboard(uint64_t value) {
bb = value;
}

Expand Down Expand Up @@ -143,7 +143,7 @@ namespace chess {
return bb;
}

[[nodiscard]] constexpr explicit operator U64() const {
[[nodiscard]] constexpr explicit operator uint64_t() const {
return bb;
}
};
Expand Down
1 change: 0 additions & 1 deletion src/chess/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#define AVX2
#endif

using U64 = uint64_t;
using Score = int32_t;
using Depth = int8_t;
using Ply = int8_t;
Expand Down
2 changes: 1 addition & 1 deletion src/chess/magic.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ namespace chess {
} while (occ != 0);

for (size_t i = 0; i < length; i++) {
U64 index = get_magic_index(magic, occupied[i]);
uint64_t index = get_magic_index(magic, occupied[i]);

magic.ptr[index] = attacked[i];
}
Expand Down
12 changes: 6 additions & 6 deletions src/chess/randoms.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@
#include "constants.h"

namespace chess {
extern const U64 rand_table[793];
extern const uint64_t rand_table[793];

constexpr U64 const *rand_table_pieces = rand_table;
constexpr U64 const *rand_table_castling = rand_table + 768;
constexpr U64 const *rand_table_ep = rand_table + 784;
constexpr U64 const *rand_table_color = rand_table + 792;
constexpr uint64_t const *rand_table_pieces = rand_table;
constexpr uint64_t const *rand_table_castling = rand_table + 768;
constexpr uint64_t const *rand_table_ep = rand_table + 784;
constexpr uint64_t const *rand_table_color = rand_table + 792;

// 2 colors * 6 types * 64 square = 768
// 16 for castling rights
// 8 number for the file of the epSquare
// 1 number if the side is black
constexpr U64 rand_table[793] = {
constexpr uint64_t rand_table[793] = {
0x4ef488bfae17abbaULL, 0x1b608e38d5cf7308ULL,
0x851dabc8d9c029daULL, 0x62ff3fd5ca1fe189ULL,
0xb58f435e51ec54d9ULL, 0xd6478aa5957c39eaULL,
Expand Down
6 changes: 3 additions & 3 deletions src/chess/zobrist.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
namespace chess {
struct Zobrist {

U64 hash = 0;
uint64_t hash = 0;

Zobrist() = default;

explicit Zobrist(U64 hash) : hash(hash) {}
explicit Zobrist(uint64_t hash) : hash(hash) {}

[[nodiscard]] operator U64() const {
[[nodiscard]] operator uint64_t() const {
return hash;
}

Expand Down
3 changes: 3 additions & 0 deletions src/network/layers/accumulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ namespace nn::layers {
void push(std::array<T, OUT> &result) {

#ifdef AVX2
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wignored-attributes"
static_assert(std::is_invocable_r_v<__m256i, decltype(ACTIVATION::_mm256_forward_epi16), __m256i>, "ACTIVATION::forward doesn't support AVX2 registers");
#pragma GCC diagnostic pop

for (size_t i = 0; i < chunk_count; i++) {
const size_t offset = i * register_width;
Expand Down
6 changes: 3 additions & 3 deletions src/network/train.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ namespace nn {
std::thread th_loading = std::thread(&DataParser::read_batch, &training_parser, batch_size, entries_next, std::ref(is_new_epoch));

std::vector<std::thread> ths;
for (int id = 0; id < thread_count; id++) {
for (size_t id = 0; id < thread_count; id++) {
ths.emplace_back(&Trainer::process_batch<true>, this, id);
}

Expand All @@ -93,7 +93,7 @@ namespace nn {

adam.update(gradients, network);

for (int id = 0; id < thread_count; id++) {
for (size_t id = 0; id < thread_count; id++) {
checkpoint_error += errors[id];
checkpoint_accuracy += accuracy[id];
}
Expand Down Expand Up @@ -177,7 +177,7 @@ namespace nn {
float val_loss = 0.0f;
int correct = 0.0f;

for (int id = 0; id < thread_count; id++) {
for (size_t id = 0; id < thread_count; id++) {
val_loss += errors[id];
correct += accuracy[id];
}
Expand Down
52 changes: 32 additions & 20 deletions src/search/transposition_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,39 +34,51 @@ namespace search {
TT_BETA = 3
};

struct TTEntry { // Total: 16 bytes
U64 hash; // 8 bytes
Score eval; // 4 bytes
chess::Move hash_move; // 2 bytes
Depth depth; // 1 byte
TTFlag flag; // 1 byte
struct TTEntry { // Total: 16 bytes
uint64_t hash = 0; // 8 bytes
Score eval = 0; // 4 bytes
chess::Move hash_move = chess::NULL_MOVE; // 2 bytes
Depth depth = 0; // 1 byte
TTFlag flag = TT_NONE; // 1 byte

constexpr TTEntry() = default;
};

static_assert(sizeof(TTEntry) == 16);

class TT {
public:
void resize(unsigned int MB) {
if (bucket_count)
~TT() {
free_table();
}

void free_table() {
if (bucket_count) {
free(table);
bucket_count = 0;
}
}

unsigned int i = 10;
void resize(unsigned int MB) {
free_table();

uint64_t i = 10;
while ((1ULL << i) <= MB * 1024ULL * 1024ULL / sizeof(TTEntry))
i++;

bucket_count = (1ULL << (i - 1));
mask = bucket_count - 1ULL;

table = (TTEntry *) malloc(bucket_count * sizeof(TTEntry));

clear();
}

void clear() {
std::memset(table, 0, bucket_count * sizeof(TTEntry));
for (uint64_t i = 0; i < bucket_count; i++) {
table[i] = TTEntry();
}
}

std::optional<TTEntry> probe(U64 hash) {
std::optional<TTEntry> probe(uint64_t hash) {
TTEntry entry = *get_entry(hash);

if (entry.hash != hash)
Expand All @@ -75,7 +87,7 @@ namespace search {
return entry;
}

void save(U64 hash, Depth depth, Score eval, TTFlag flag, chess::Move best_move) {
void save(uint64_t hash, Depth depth, Score eval, TTFlag flag, chess::Move best_move) {
TTEntry *entry = get_entry(hash);

if (flag == TT_ALPHA && entry->hash == hash) {
Expand All @@ -94,23 +106,23 @@ namespace search {
}
}

void prefetch(U64 hash) {
void prefetch(uint64_t hash) {
__builtin_prefetch(get_entry(hash), 0);
}

chess::Move get_hash_move(U64 hash) {
chess::Move get_hash_move(uint64_t hash) {
TTEntry *entry = get_entry(hash);
if (entry->hash == hash)
return entry->hash_move;
return chess::NULL_MOVE;
}

private:
TTEntry *table;
unsigned int bucket_count = 0;
U64 mask = 0;
TTEntry *table = nullptr;
uint64_t bucket_count = 0;
uint64_t mask = 0;

TTEntry *get_entry(U64 hash) {
TTEntry *get_entry(uint64_t hash) {
return table + (hash & mask);
}
};
Expand Down
1 change: 1 addition & 0 deletions src/selfplay/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace selfplay {
void init(unsigned int hash_size, unsigned int thread_count) {
sm.allocate_hash(hash_size);
sm.allocate_threads(thread_count);
sm.tt_clear();
}

std::pair<chess::Move, Score> search(const chess::Board &board, const search::Limits &limits) {
Expand Down
2 changes: 1 addition & 1 deletion src/tests/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace test {
std::vector<std::string> moves;
chess::Zobrist hash;

Test(std::string fen, std::vector<std::string> moves, U64 hash) : fen(std::move(fen)), moves(std::move(moves)), hash(hash) {}
Test(std::string fen, std::vector<std::string> moves, uint64_t hash) : fen(std::move(fen)), moves(std::move(moves)), hash(hash) {}
};

const std::vector<Test> tests = {
Expand Down
2 changes: 1 addition & 1 deletion src/uci/uci.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ namespace uci {
});
commands.emplace_back("perft", [&](context tokens) {
int depth = find_element<int>(tokens, "perft").value_or(5);
U64 node_count = test::perft<true, false>(board, depth);
uint64_t node_count = test::perft<true, false>(board, depth);
Logger("Total node count: ", node_count);
});
commands.emplace_back("go", [&](context tokens) {
Expand Down

0 comments on commit cc9902b

Please sign in to comment.