diff --git a/Makefile b/Makefile index 3dc2ea1..fb5ba98 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/src/chess/bitboard.h b/src/chess/bitboard.h index 7496cf2..a818ccf 100644 --- a/src/chess/bitboard.h +++ b/src/chess/bitboard.h @@ -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; } @@ -143,7 +143,7 @@ namespace chess { return bb; } - [[nodiscard]] constexpr explicit operator U64() const { + [[nodiscard]] constexpr explicit operator uint64_t() const { return bb; } }; diff --git a/src/chess/constants.h b/src/chess/constants.h index abaa2d2..1049195 100644 --- a/src/chess/constants.h +++ b/src/chess/constants.h @@ -32,7 +32,6 @@ #define AVX2 #endif -using U64 = uint64_t; using Score = int32_t; using Depth = int8_t; using Ply = int8_t; diff --git a/src/chess/magic.h b/src/chess/magic.h index dd47c31..21f6a85 100644 --- a/src/chess/magic.h +++ b/src/chess/magic.h @@ -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]; } diff --git a/src/chess/randoms.h b/src/chess/randoms.h index 8976fad..e1bb16d 100644 --- a/src/chess/randoms.h +++ b/src/chess/randoms.h @@ -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, diff --git a/src/chess/zobrist.h b/src/chess/zobrist.h index 33bf3d5..2f5c2e6 100644 --- a/src/chess/zobrist.h +++ b/src/chess/zobrist.h @@ -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; } diff --git a/src/network/layers/accumulator.h b/src/network/layers/accumulator.h index b797b00..4558e0a 100644 --- a/src/network/layers/accumulator.h +++ b/src/network/layers/accumulator.h @@ -96,7 +96,10 @@ namespace nn::layers { void push(std::array &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; diff --git a/src/network/train.h b/src/network/train.h index a688a93..e65ae7b 100644 --- a/src/network/train.h +++ b/src/network/train.h @@ -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 ths; - for (int id = 0; id < thread_count; id++) { + for (size_t id = 0; id < thread_count; id++) { ths.emplace_back(&Trainer::process_batch, this, id); } @@ -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]; } @@ -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]; } diff --git a/src/search/transposition_table.h b/src/search/transposition_table.h index 3f125e3..2974287 100644 --- a/src/search/transposition_table.h +++ b/src/search/transposition_table.h @@ -34,23 +34,35 @@ 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++; @@ -58,15 +70,15 @@ namespace search { 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 probe(U64 hash) { + std::optional probe(uint64_t hash) { TTEntry entry = *get_entry(hash); if (entry.hash != hash) @@ -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) { @@ -94,11 +106,11 @@ 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; @@ -106,11 +118,11 @@ namespace search { } 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); } }; diff --git a/src/selfplay/engine.h b/src/selfplay/engine.h index 004f3fe..0f5f88a 100644 --- a/src/selfplay/engine.h +++ b/src/selfplay/engine.h @@ -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 search(const chess::Board &board, const search::Limits &limits) { diff --git a/src/tests/hash.h b/src/tests/hash.h index 9bf65fa..a2016fb 100644 --- a/src/tests/hash.h +++ b/src/tests/hash.h @@ -32,7 +32,7 @@ namespace test { std::vector moves; chess::Zobrist hash; - Test(std::string fen, std::vector moves, U64 hash) : fen(std::move(fen)), moves(std::move(moves)), hash(hash) {} + Test(std::string fen, std::vector moves, uint64_t hash) : fen(std::move(fen)), moves(std::move(moves)), hash(hash) {} }; const std::vector tests = { diff --git a/src/uci/uci.h b/src/uci/uci.h index 8b30870..1fd61e4 100644 --- a/src/uci/uci.h +++ b/src/uci/uci.h @@ -131,7 +131,7 @@ namespace uci { }); commands.emplace_back("perft", [&](context tokens) { int depth = find_element(tokens, "perft").value_or(5); - U64 node_count = test::perft(board, depth); + uint64_t node_count = test::perft(board, depth); Logger("Total node count: ", node_count); }); commands.emplace_back("go", [&](context tokens) {