ai: tt: 使用更新年龄机制来取代每次全部清空置换表
memset 大块内存效率较低, 故在哈希表中增加年龄纪录, 当年龄不一致时即认为哈希表项过时, 就更新它. 如果年龄一致则从中读取数据. 年龄增长到255时才清空一次置换表, 这样自对弈一盘棋只需要清2次. 修改使用 TRANSPOSITION_TABLE_FAKE_CLEAN 宏控制. 代价是会增加哈希表的体积, 空间换时间. 自对弈时长由 63s 缩短到 54s, 提速 15%.
This commit is contained in:
parent
2918b9a23d
commit
8e54962233
|
@ -66,6 +66,7 @@
|
|||
|
||||
#ifdef TRANSPOSITION_TABLE_ENABLE
|
||||
#define CLEAR_TRANSPOSITION_TABLE
|
||||
#define TRANSPOSITION_TABLE_FAKE_CLEAN
|
||||
#define TRANSPOSITION_TABLE_CUTDOWN
|
||||
//#define TRANSPOSITION_TABLE_DEBUG
|
||||
#endif
|
||||
|
|
|
@ -4,6 +4,10 @@
|
|||
static constexpr int TRANSPOSITION_TABLE_SIZE = 0x2000000; // 8-128M:102s, 4-64M:93s 2-32M:91s 1-16M: 冲突
|
||||
HashMap<hash_t, TT::HashValue> transpositionTable(TRANSPOSITION_TABLE_SIZE);
|
||||
|
||||
#ifdef TRANSPOSITION_TABLE_FAKE_CLEAN
|
||||
uint8_t transpositionTableAge;
|
||||
#endif // TRANSPOSITION_TABLE_FAKE_CLEAN
|
||||
|
||||
value_t TT::probeHash(hash_t hash,
|
||||
depth_t depth, value_t alpha, value_t beta,
|
||||
move_t &bestMove, HashType &type)
|
||||
|
@ -14,6 +18,13 @@ value_t TT::probeHash(hash_t hash,
|
|||
return VALUE_UNKNOWN;
|
||||
}
|
||||
|
||||
#ifdef TRANSPOSITION_TABLE_FAKE_CLEAN
|
||||
if (hashValue.age != transpositionTableAge)
|
||||
{
|
||||
return VALUE_UNKNOWN;
|
||||
}
|
||||
#endif // TRANSPOSITION_TABLE_FAKE_CLEAN
|
||||
|
||||
if (depth > hashValue.depth) {
|
||||
goto out;
|
||||
}
|
||||
|
@ -74,19 +85,30 @@ int TT::recordHash(value_t value, depth_t depth, TT::HashType type, hash_t hash,
|
|||
// 注意: 每走一步以前都必须把散列表中所有的标志项置为 hashfEMPTY
|
||||
|
||||
//hashMapMutex.lock();
|
||||
HashValue hashValue{};
|
||||
HashValue hashValue {};
|
||||
|
||||
if (findHash(hash, hashValue) &&
|
||||
hashValue.type != hashfEMPTY &&
|
||||
if (findHash(hash, hashValue)) {
|
||||
#ifdef TRANSPOSITION_TABLE_FAKE_CLEAN
|
||||
if (hashValue.age == transpositionTableAge) {
|
||||
#endif // TRANSPOSITION_TABLE_FAKE_CLEAN
|
||||
if (hashValue.type != hashfEMPTY &&
|
||||
hashValue.depth > depth) {
|
||||
return -1;
|
||||
}
|
||||
#ifdef TRANSPOSITION_TABLE_FAKE_CLEAN
|
||||
}
|
||||
#endif // TRANSPOSITION_TABLE_FAKE_CLEAN
|
||||
}
|
||||
|
||||
hashValue.value = value;
|
||||
hashValue.depth = depth;
|
||||
hashValue.type = type;
|
||||
hashValue.bestMove = bestMove;
|
||||
|
||||
#ifdef TRANSPOSITION_TABLE_FAKE_CLEAN
|
||||
hashValue.age = transpositionTableAge;
|
||||
#endif // TRANSPOSITION_TABLE_FAKE_CLEAN
|
||||
|
||||
transpositionTable.insert(hash, hashValue);
|
||||
|
||||
//hashMapMutex.unlock();
|
||||
|
@ -96,7 +118,18 @@ int TT::recordHash(value_t value, depth_t depth, TT::HashType type, hash_t hash,
|
|||
|
||||
void TT::clear()
|
||||
{
|
||||
#ifdef TRANSPOSITION_TABLE_FAKE_CLEAN
|
||||
if (transpositionTableAge == UINT8_MAX)
|
||||
{
|
||||
loggerDebug("Clean TT\n");
|
||||
transpositionTable.clear();
|
||||
transpositionTableAge = 0;
|
||||
} else {
|
||||
transpositionTableAge++;
|
||||
}
|
||||
#else
|
||||
transpositionTable.clear();
|
||||
#endif // TRANSPOSITION_TABLE_FAKE_CLEAN
|
||||
}
|
||||
|
||||
#endif /* TRANSPOSITION_TABLE_ENABLE */
|
||||
|
|
|
@ -32,6 +32,9 @@ public:
|
|||
depth_t depth;
|
||||
enum HashType type;
|
||||
move_t bestMove;
|
||||
#ifdef TRANSPOSITION_TABLE_FAKE_CLEAN
|
||||
uint8_t age;
|
||||
#endif // TRANSPOSITION_TABLE_FAKE_CLEAN
|
||||
};
|
||||
|
||||
// 查找哈希表
|
||||
|
@ -49,6 +52,10 @@ using TT = TranspositionTable;
|
|||
|
||||
extern HashMap<hash_t, TT::HashValue> transpositionTable;
|
||||
|
||||
#ifdef TRANSPOSITION_TABLE_FAKE_CLEAN
|
||||
extern uint8_t transpositionTableAge;
|
||||
#endif // TRANSPOSITION_TABLE_FAKE_CLEAN
|
||||
|
||||
#endif // TRANSPOSITION_TABLE_ENABLE
|
||||
|
||||
#endif /* TT_H */
|
||||
|
|
Loading…
Reference in New Issue