mirror of
https://github.com/official-stockfish/Stockfish.git
synced 2026-07-23 13:17:12 +00:00
Previously, we had two type aliases, LargePagePtr and AlignedPtr, which required manually initializing the aligned memory for the pointer. The new helpers: - make_unique_aligned - make_unique_large_page are now available for allocating aligned memory (with large pages). They behave similarly to std::make_unique, ensuring objects allocated with these functions follow RAII. The old approach had issues with initializing non-trivial types or arrays of objects. The evaluation function of the network is now a unique pointer to an array instead of an array of unique pointers. Memory related functions have been moved into memory.h Passed High Hash Pressure Test Non-Regression STC: https://tests.stockfishchess.org/tests/view/665b2b36586058766677cfd2 LLR: 2.93 (-2.94,2.94) <-1.75,0.25> Total: 476992 W: 122426 L: 122677 D: 231889 Ptnml(0-2): 1145, 51027, 134419, 50744, 1161 Failed Normal Non-Regression STC: https://tests.stockfishchess.org/tests/view/665b2997586058766677cfc8 LLR: -2.94 (-2.94,2.94) <-1.75,0.25> Total: 877312 W: 225233 L: 226395 D: 425684 Ptnml(0-2): 2110, 94642, 246239, 93630, 2035 Probably a fluke since there shouldn't be a real slowndown and it has also passed the high hash pressure test. closes https://github.com/official-stockfish/Stockfish/pull/5332 No functional change
155 lines
5.5 KiB
C++
155 lines
5.5 KiB
C++
/*
|
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
Copyright (C) 2004-2024 The Stockfish developers (see AUTHORS file)
|
|
|
|
Stockfish is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
Stockfish is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "tt.h"
|
|
|
|
#include <cassert>
|
|
#include <cstdint>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <iostream>
|
|
|
|
#include "memory.h"
|
|
#include "syzygy/tbprobe.h"
|
|
#include "thread.h"
|
|
|
|
namespace Stockfish {
|
|
|
|
// DEPTH_ENTRY_OFFSET exists because 1) we use `bool(depth8)` as the occupancy check, but
|
|
// 2) we need to store negative depths for QS. (`depth8` is the only field with "spare bits":
|
|
// we sacrifice the ability to store depths greater than 1<<8 less the offset, as asserted below.)
|
|
|
|
// Populates the TTEntry with a new node's data, possibly
|
|
// overwriting an old position. The update is not atomic and can be racy.
|
|
void TTEntry::save(
|
|
Key k, Value v, bool pv, Bound b, Depth d, Move m, Value ev, uint8_t generation8) {
|
|
|
|
// Preserve the old ttmove if we don't have a new one
|
|
if (m || uint16_t(k) != key16)
|
|
move16 = m;
|
|
|
|
// Overwrite less valuable entries (cheapest checks first)
|
|
if (b == BOUND_EXACT || uint16_t(k) != key16 || d - DEPTH_ENTRY_OFFSET + 2 * pv > depth8 - 4
|
|
|| relative_age(generation8))
|
|
{
|
|
assert(d > DEPTH_ENTRY_OFFSET);
|
|
assert(d < 256 + DEPTH_ENTRY_OFFSET);
|
|
|
|
key16 = uint16_t(k);
|
|
depth8 = uint8_t(d - DEPTH_ENTRY_OFFSET);
|
|
genBound8 = uint8_t(generation8 | uint8_t(pv) << 2 | b);
|
|
value16 = int16_t(v);
|
|
eval16 = int16_t(ev);
|
|
}
|
|
}
|
|
|
|
|
|
uint8_t TTEntry::relative_age(const uint8_t generation8) const {
|
|
// Due to our packed storage format for generation and its cyclic
|
|
// nature we add GENERATION_CYCLE (256 is the modulus, plus what
|
|
// is needed to keep the unrelated lowest n bits from affecting
|
|
// the result) to calculate the entry age correctly even after
|
|
// generation8 overflows into the next cycle.
|
|
|
|
return (TranspositionTable::GENERATION_CYCLE + generation8 - genBound8)
|
|
& TranspositionTable::GENERATION_MASK;
|
|
}
|
|
|
|
|
|
// Sets the size of the transposition table,
|
|
// measured in megabytes. Transposition table consists
|
|
// of clusters and each cluster consists of ClusterSize number of TTEntry.
|
|
void TranspositionTable::resize(size_t mbSize, ThreadPool& threads) {
|
|
clusterCount = mbSize * 1024 * 1024 / sizeof(Cluster);
|
|
|
|
table = make_unique_large_page<Cluster[]>(clusterCount);
|
|
|
|
if (!table)
|
|
{
|
|
std::cerr << "Failed to allocate " << mbSize << "MB for transposition table." << std::endl;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
clear(threads);
|
|
}
|
|
|
|
|
|
// Initializes the entire transposition table to zero,
|
|
// in a multi-threaded way.
|
|
void TranspositionTable::clear(ThreadPool& threads) {
|
|
const size_t threadCount = threads.num_threads();
|
|
|
|
for (size_t i = 0; i < threadCount; ++i)
|
|
{
|
|
threads.run_on_thread(i, [this, i, threadCount]() {
|
|
// Each thread will zero its part of the hash table
|
|
const size_t stride = clusterCount / threadCount;
|
|
const size_t start = stride * i;
|
|
const size_t len = i + 1 != threadCount ? stride : clusterCount - start;
|
|
|
|
std::memset(&table[start], 0, len * sizeof(Cluster));
|
|
});
|
|
}
|
|
|
|
for (size_t i = 0; i < threadCount; ++i)
|
|
threads.wait_on_thread(i);
|
|
}
|
|
|
|
|
|
// Looks up the current position in the transposition
|
|
// table. It returns true and a pointer to the TTEntry if the position is found.
|
|
// Otherwise, it returns false and a pointer to an empty or least valuable TTEntry
|
|
// to be replaced later. The replace value of an entry is calculated as its depth
|
|
// minus 8 times its relative age. TTEntry t1 is considered more valuable than
|
|
// TTEntry t2 if its replace value is greater than that of t2.
|
|
TTEntry* TranspositionTable::probe(const Key key, bool& found) const {
|
|
|
|
TTEntry* const tte = first_entry(key);
|
|
const uint16_t key16 = uint16_t(key); // Use the low 16 bits as key inside the cluster
|
|
|
|
for (int i = 0; i < ClusterSize; ++i)
|
|
if (tte[i].key16 == key16)
|
|
return found = bool(tte[i].depth8), &tte[i];
|
|
|
|
// Find an entry to be replaced according to the replacement strategy
|
|
TTEntry* replace = tte;
|
|
for (int i = 1; i < ClusterSize; ++i)
|
|
if (replace->depth8 - replace->relative_age(generation8) * 2
|
|
> tte[i].depth8 - tte[i].relative_age(generation8) * 2)
|
|
replace = &tte[i];
|
|
|
|
return found = false, replace;
|
|
}
|
|
|
|
|
|
// Returns an approximation of the hashtable
|
|
// occupation during a search. The hash is x permill full, as per UCI protocol.
|
|
// Only counts entries which match the current generation.
|
|
int TranspositionTable::hashfull() const {
|
|
|
|
int cnt = 0;
|
|
for (int i = 0; i < 1000; ++i)
|
|
for (int j = 0; j < ClusterSize; ++j)
|
|
cnt += table[i].entry[j].depth8
|
|
&& (table[i].entry[j].genBound8 & GENERATION_MASK) == generation8;
|
|
|
|
return cnt / ClusterSize;
|
|
}
|
|
|
|
} // namespace Stockfish
|