Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
official-stockfish
GitHub Repository: official-stockfish/Stockfish
Path: blob/master/src/tt.h
376 views
1
/*
2
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3
Copyright (C) 2004-2025 The Stockfish developers (see AUTHORS file)
4
5
Stockfish is free software: you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation, either version 3 of the License, or
8
(at your option) any later version.
9
10
Stockfish is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
14
15
You should have received a copy of the GNU General Public License
16
along with this program. If not, see <http://www.gnu.org/licenses/>.
17
*/
18
19
#ifndef TT_H_INCLUDED
20
#define TT_H_INCLUDED
21
22
#include <cstddef>
23
#include <cstdint>
24
#include <tuple>
25
26
#include "memory.h"
27
#include "types.h"
28
29
namespace Stockfish {
30
31
class ThreadPool;
32
struct TTEntry;
33
struct Cluster;
34
35
// There is only one global hash table for the engine and all its threads. For chess in particular, we even allow racy
36
// updates between threads to and from the TT, as taking the time to synchronize access would cost thinking time and
37
// thus elo. As a hash table, collisions are possible and may cause chess playing issues (bizarre blunders, faulty mate
38
// reports, etc). Fixing these also loses elo; however such risk decreases quickly with larger TT size.
39
//
40
// `probe` is the primary method: given a board position, we lookup its entry in the table, and return a tuple of:
41
// 1) whether the entry already has this position
42
// 2) a copy of the prior data (if any) (may be inconsistent due to read races)
43
// 3) a writer object to this entry
44
// The copied data and the writer are separated to maintain clear boundaries between local vs global objects.
45
46
47
// A copy of the data already in the entry (possibly collided). `probe` may be racy, resulting in inconsistent data.
48
struct TTData {
49
Move move;
50
Value value, eval;
51
Depth depth;
52
Bound bound;
53
bool is_pv;
54
55
TTData() = delete;
56
57
// clang-format off
58
TTData(Move m, Value v, Value ev, Depth d, Bound b, bool pv) :
59
move(m),
60
value(v),
61
eval(ev),
62
depth(d),
63
bound(b),
64
is_pv(pv) {};
65
// clang-format on
66
};
67
68
69
// This is used to make racy writes to the global TT.
70
struct TTWriter {
71
public:
72
void write(Key k, Value v, bool pv, Bound b, Depth d, Move m, Value ev, uint8_t generation8);
73
74
private:
75
friend class TranspositionTable;
76
TTEntry* entry;
77
TTWriter(TTEntry* tte);
78
};
79
80
81
class TranspositionTable {
82
83
public:
84
~TranspositionTable() { aligned_large_pages_free(table); }
85
86
void resize(size_t mbSize, ThreadPool& threads); // Set TT size
87
void clear(ThreadPool& threads); // Re-initialize memory, multithreaded
88
int hashfull(int maxAge = 0)
89
const; // Approximate what fraction of entries (permille) have been written to during this root search
90
91
void
92
new_search(); // This must be called at the beginning of each root search to track entry aging
93
uint8_t generation() const; // The current age, used when writing new data to the TT
94
std::tuple<bool, TTData, TTWriter>
95
probe(const Key key) const; // The main method, whose retvals separate local vs global objects
96
TTEntry* first_entry(const Key key)
97
const; // This is the hash function; its only external use is memory prefetching.
98
99
private:
100
friend struct TTEntry;
101
102
size_t clusterCount;
103
Cluster* table = nullptr;
104
105
uint8_t generation8 = 0; // Size must be not bigger than TTEntry::genBound8
106
};
107
108
} // namespace Stockfish
109
110
#endif // #ifndef TT_H_INCLUDED
111
112