Skip to content
This repository was archived by the owner on May 21, 2022. It is now read-only.

Commit 6fd4800

Browse files
committed
Added support for hooking into the ttable store path (for training)
1 parent 591beb6 commit 6fd4800

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

search.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -275,15 +275,15 @@ Score Search(RootSearchContext &context, std::vector<Move> &pv, Board &board, Sc
275275
{
276276
if (ret >= beta)
277277
{
278-
context.transpositionTable->Store(board.GetHash(), pv.size() > 0 ? pv[0] : 0, ret, 0, LOWERBOUND);
278+
context.transpositionTable->Store(board, pv.size() > 0 ? pv[0] : 0, ret, 0, LOWERBOUND);
279279
}
280280
else if (ret <= alpha)
281281
{
282-
context.transpositionTable->Store(board.GetHash(), 0, ret, 0, UPPERBOUND);
282+
context.transpositionTable->Store(board, 0, ret, 0, UPPERBOUND);
283283
}
284284
else
285285
{
286-
context.transpositionTable->Store(board.GetHash(), pv.size() > 0 ? pv[0] : 0, ret, 0, EXACT);
286+
context.transpositionTable->Store(board, pv.size() > 0 ? pv[0] : 0, ret, 0, EXACT);
287287
}
288288
}
289289

@@ -397,7 +397,7 @@ Score Search(RootSearchContext &context, std::vector<Move> &pv, Board &board, Sc
397397
{
398398
if (ENABLE_TT)
399399
{
400-
context.transpositionTable->Store(board.GetHash(), 0, nmScore, originalNodeBudget, LOWERBOUND);
400+
context.transpositionTable->Store(board, 0, nmScore, originalNodeBudget, LOWERBOUND);
401401
}
402402

403403
return beta;
@@ -537,7 +537,7 @@ Score Search(RootSearchContext &context, std::vector<Move> &pv, Board &board, Sc
537537
{
538538
if (ENABLE_TT)
539539
{
540-
context.transpositionTable->Store(board.GetHash(), mv, score, originalNodeBudget, LOWERBOUND);
540+
context.transpositionTable->Store(board, mv, score, originalNodeBudget, LOWERBOUND);
541541
}
542542

543543
context.moveEvaluator->NotifyBestMove(board, si, miList, mv, numMovesSearched + 1);
@@ -573,13 +573,20 @@ Score Search(RootSearchContext &context, std::vector<Move> &pv, Board &board, Sc
573573
}
574574
}
575575

576+
if (originalNodeBudget == 64)
577+
{
578+
std::cout << "End" << std::endl;
579+
std::cout << "Best score: " << bestScore << std::endl;
580+
std::cout << "Alpha: " << alpha << std::endl;
581+
}
582+
576583
if (!context.Stopping())
577584
{
578-
if (bestScore > alpha)
585+
if (bestScore >= alpha)
579586
{
580587
if (ENABLE_TT)
581588
{
582-
context.transpositionTable->Store(board.GetHash(), pv[0], bestScore, originalNodeBudget, EXACT);
589+
context.transpositionTable->Store(board, pv[0], bestScore, originalNodeBudget, EXACT);
583590
}
584591

585592
context.moveEvaluator->NotifyBestMove(board, si, miList, pv[0], miList.GetSize());
@@ -589,7 +596,7 @@ Score Search(RootSearchContext &context, std::vector<Move> &pv, Board &board, Sc
589596
// otherwise we failed low (and may have prunned all nodes)
590597
if (ENABLE_TT)
591598
{
592-
context.transpositionTable->Store(board.GetHash(), pv.size() > 0 ? pv[0] : 0, bestScore, originalNodeBudget, UPPERBOUND);
599+
context.transpositionTable->Store(board, pv.size() > 0 ? pv[0] : 0, bestScore, originalNodeBudget, UPPERBOUND);
593600
}
594601
}
595602
}

ttable.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ void TTable::Resize(size_t newSize)
2929
m_data.resize(newSize);
3030
}
3131

32-
void TTable::Store(uint64_t hash, Move bestMove, Score score, NodeBudget nodeBudget, TTEntryType entryType)
32+
void TTable::Store(const Board &board, Move bestMove, Score score, NodeBudget nodeBudget, TTEntryType entryType)
3333
{
34+
uint64_t hash = board.GetHash();
35+
3436
TTEntry *slot = &m_data[hash % m_data.size()];
3537

3638
bool replace = false;
@@ -55,6 +57,11 @@ void TTable::Store(uint64_t hash, Move bestMove, Score score, NodeBudget nodeBud
5557
slot->nodeBudget = nodeBudget;
5658
slot->entryType = entryType;
5759
slot->birthday = m_currentGeneration;
60+
61+
if (m_storeCallback)
62+
{
63+
m_storeCallback(board, *slot);
64+
}
5865
}
5966
}
6067

ttable.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#ifndef TTABLE_H
1919
#define TTABLE_H
2020

21+
#include <functional>
2122
#include <memory>
2223
#include <vector>
2324

@@ -26,6 +27,7 @@
2627

2728
#include "types.h"
2829
#include "move.h"
30+
#include "board.h"
2931

3032
enum TTEntryType
3133
{
@@ -49,6 +51,8 @@ struct TTEntry
4951
TTEntryType entryType;
5052
};
5153

54+
using TTEntryCallback = std::function<void(const Board &b, const TTEntry &)>;
55+
5256
class TTable
5357
{
5458
public:
@@ -78,7 +82,7 @@ class TTable
7882
__builtin_prefetch(&m_data[hash % m_data.size()]);
7983
}
8084

81-
void Store(uint64_t hash, Move bestMove, Score score, NodeBudget nodeBudget, TTEntryType entryType);
85+
void Store(const Board &b, Move bestMove, Score score, NodeBudget nodeBudget, TTEntryType entryType);
8286

8387
void AgeTable() { ++m_currentGeneration; }
8488

@@ -93,10 +97,18 @@ class TTable
9397
}
9498
}
9599

100+
void SetStoreCallback(TTEntryCallback callback)
101+
{
102+
m_storeCallback = callback;
103+
}
104+
96105
private:
97106
std::vector<TTEntry> m_data;
98107

99108
int32_t m_currentGeneration;
109+
110+
// during TreeStrap we have to record write to the ttable
111+
TTEntryCallback m_storeCallback;
100112
};
101113

102114
#endif // TTABLE_H

0 commit comments

Comments
 (0)