prefetch: 增加置换表的预读取特性但暂不启用

对于自对弈,因为局面模拟走下一步,入栈出栈花了7s时间,
而 prefetch 预读置换表,节5s时间,总时间还是增加2s。
故暂不启用。
This commit is contained in:
Calcitem 2020-04-19 02:53:04 +08:00
parent 4b2a5ba5fd
commit e4ec6e22ee
5 changed files with 39 additions and 0 deletions

View File

@ -95,6 +95,8 @@
//#define TRANSPOSITION_TABLE_DEBUG
#endif
//#define PREFETCH_SUPPORT
//#define USE_STD_STACK
//#define RAPID_GAME

View File

@ -863,6 +863,17 @@ value_t AIAlgorithm::search(depth_t depth, value_t alpha, value_t beta, Node *no
int nchild = node->childrenSize;
for (int i = 0; i < nchild; i++) {
#ifdef PREFETCH_SUPPORT
if (i + 1 < nchild)
{
stashPosition();
doMove(node->children[i + 1]->move);
hash_t nextHash = st->getHash();
TT::prefetchHash(nextHash);
undoMove();
}
#endif // PREFETCH_SUPPORT
// 棋局入栈保存,以便后续撤销着法
stashPosition();

View File

@ -112,6 +112,11 @@ bool TT::findHash(const hash_t &hash, TT::HashValue &hashValue)
#endif
}
void TT::prefetchHash(const hash_t &hash)
{
transpositionTable.prefetch(hash);
}
int TT::recordHash(const value_t &value,
const depth_t &depth,
const TT::HashType &type,

View File

@ -82,6 +82,9 @@ public:
// 清空置换表
static void clear();
// 预读取
static void prefetchHash(const hash_t &hash);
};
using TT = TranspositionTable;

View File

@ -76,6 +76,24 @@ namespace CTSL //Concurrent Thread Safe Library
#endif
}
void prefetch(const K &key)
{
K hashValue = hashFn(key) & (hashSize - 1);
const V *addr = &(hashTable[hashValue].getValue());
# if defined(__INTEL_COMPILER)
// This hack prevents prefetches from being optimized away by
// Intel compiler. Both MSVC and gcc seem not be affected by this.
__asm__("");
# endif
# if defined(__INTEL_COMPILER) || defined(_MSC_VER)
_mm_prefetch((const char *)addr, _MM_HINT_T0);
# else
__builtin_prefetch(addr);
# endif
}
//Function to insert into the hash map.
//If key already exists, update the value, else insert a new node in the bucket with the <key, value> pair.
K insert(const K &key, const V &value)