diff --git a/NineChess/src/config.h b/NineChess/src/config.h index 875543f3..622745cb 100644 --- a/NineChess/src/config.h +++ b/NineChess/src/config.h @@ -1,15 +1,15 @@ #ifndef CONFIG_H #define CONFIG_H -//#define DEBUG +#define DEBUG -#define RANDOM_MOVE +//#define RANDOM_MOVE -#define DEAL_WITH_HORIZON_EFFECT +//#define DEAL_WITH_HORIZON_EFFECT //#define RANDOM_BEST_MOVE -//#define HASH_MAP_ENABLE +#define HASH_MAP_ENABLE //#define DONOT_DELETE_TREE @@ -48,7 +48,7 @@ #define DRAW_SEAT_NUMBER #endif -#define IDS_SUPPORT +//#define IDS_SUPPORT #define SAVE_CHESSBOOK_WHEN_ACTION_NEW_TRIGGERED diff --git a/NineChess/src/hashmap.cpp b/NineChess/src/hashmap.cpp index e4e484c0..44270d35 100644 --- a/NineChess/src/hashmap.cpp +++ b/NineChess/src/hashmap.cpp @@ -1,48 +1,37 @@ #include "hashmap.h" -template -HashMap::HashMap(): - capacity(0), - size(0), - pool(nullptr) -{ - this->capacity = 0x20000000; // TODO -} -template -HashMap::HashMap(size_t capacity) -{ - this->capacity = capacity; - this->size = 0; - construct(); -} +// template +// HashMap(uint64_t capacity, uint64_t size, T *pool) +// { +// this->capacity = capacity; +// size = size; +// //HashMap::construct(); +// } -template -HashMap::~HashMap() -{ - clear(); -} template bool HashMap::construct() { - pool = new T[capacity]; + HashMap::pool = new T[capacity]; - if (pool == nullptr) { + if (HashMap::pool == nullptr) { return false; } return true; } + + template T& HashMap::at(uint64_t i) { if (i >= capacity) { qDebug() << "Error"; - return pool[0]; + return HashMap::pool[0]; } - return pool[i]; + return HashMap::pool[i]; } template @@ -52,7 +41,7 @@ size_t HashMap::getSize() } template -size_t HashMap::getCapacity() +uint64_t HashMap::getCapacity() { return capacity; } @@ -64,7 +53,7 @@ uint64_t HashMap::hashToAddr(uint64_t hash) } template -void HashMap::insert(uint64_t hash, const T &hashValue) +void HashMap::insert(uint64_t hash, T &hashValue) { uint64_t addr = hashToAddr(hash); @@ -78,3 +67,6 @@ void HashMap::clear() pool = nullptr; } +template +HashMap* HashMap::instance = new HashMap(capacity = 1024, size = 0); + diff --git a/NineChess/src/hashmap.h b/NineChess/src/hashmap.h index e511352f..1550f30d 100644 --- a/NineChess/src/hashmap.h +++ b/NineChess/src/hashmap.h @@ -2,55 +2,87 @@ #define HASHMAP_H #include +#include +#include #include template class HashMap { public: - HashMap(); - HashMap(size_t capacity); - ~HashMap(); + //HashMap(size_t capacity, size_t size, T* pool); + //~HashMap(); + HashMap() = default; - enum FindResult + static HashMap *getInstance() { - HASHMAP_NOTFOUND = INT32_MAX, - }; +#if 0 + static std::once_flag s_flag; + std::call_once(s_flag, [&]() { + instance.reset(new HashMap); + }); +#endif + if (instance) - T& at(uint64_t i); + return *instance; + } + static void lock() + { + hashMapMutex.lock(); + } + + static void unlock() + { + hashMapMutex.unlock(); + } + + static T& at(uint64_t i); + +#if 0 T& operator[](uint64_t hash) { uint64_t addr = hashToAddr(hash); return pool[addr]; } +#endif - uint64_t hashToAddr(uint64_t hash); + static uint64_t hashToAddr(uint64_t hash); - T &find(uint64_t hash) + static char* find(uint64_t hash) { - uint64_t addr = hashToAddr(hash); + // uint64_t addr = hashToAddr(hash); - return pool[addr]; + return pool[hash <<32 >>32]; } - size_t getSize(); - size_t getCapacity(); + static size_t getSize(); + static size_t getCapacity(); - void clear(); + static void clear(); - void insert(uint64_t hash, const T &hashValue); + static void insert(uint64_t hash, T &hashValue); - bool construct(); + static bool construct(); -private: - size_t capacity; - size_t size; +public: + static const uint64_t capacity; + static uint64_t size; - T *pool; + static char *pool; + static std::mutex hashMapMutex; + + //static std::auto_ptr> instance; + static HashMap* instance; +public: + // 防止外部构造。 + //HashMap() = default; + // 防止拷贝和赋值。 + HashMap &operator=(const HashMap &) = delete; HashMap(const HashMap &another) = delete; }; + #endif // HASHMAP_H diff --git a/NineChess/src/ninechessai_ab.cpp b/NineChess/src/ninechessai_ab.cpp index 0886c13b..9fb1a12a 100644 --- a/NineChess/src/ninechessai_ab.cpp +++ b/NineChess/src/ninechessai_ab.cpp @@ -13,18 +13,26 @@ #include #include "ninechessai_ab.h" +#include "hashmap.h" + +#ifdef HASH_MAP_ENABLE +static std::unique_ptr> instance; +#endif NineChessAi_ab::NineChessAi_ab() : rootNode(nullptr), requiredQuit(false), nodeCount(0), - evaluatedNodeCount(0), - hashHitCount(0) +#ifdef HASH_MAP_ENABLE + hashHitCount(0), + //hashmap(HashMap::getInstance()), +#endif + evaluatedNodeCount(0) { buildRoot(); #ifdef HASH_MAP_ENABLE - hashMap.construct(); // TODO + //hashmap = HashMap::getInstance(); #endif } @@ -99,7 +107,7 @@ struct NineChessAi_ab::Node *NineChessAi_ab::addNode(Node *parent, int value, in // 闈欐乭ashmap鍒濆鍖 //mutex NineChessAi_ab::hashMapMutex; //HashMap NineChessAi_ab::hashmap; - +//std::unique_ptr> HashMap::instance; #ifdef MOVE_PRIORITY_TABLE_SUPPORT @@ -636,9 +644,9 @@ int NineChessAi_ab::alphaBetaPruning(int depth, int alpha, int beta, Node *node) uint64_t hash = chessTemp.getHash(); node->hash = hash; - hashMapMutex.lock(); + //hashMapMutex.lock(); - HashValue hashValue = findHash(hash); + HashValue hashValue;// = NineChessAi_ab::findHash(hash_); if (node != rootNode && hashValue.hash == hash && @@ -655,13 +663,13 @@ int NineChessAi_ab::alphaBetaPruning(int depth, int alpha, int beta, Node *node) else node->value -= hashValue.depth - depth; - hashMapMutex.unlock(); + //hashMapMutex::unlock(); hashHitCount++; return node->value; } - hashMapMutex.unlock(); + //hashMapMutex.unlock(); #endif /* HASH_MAP_ENABLE */ // 鎼滅储鍒板彾瀛愯妭鐐癸紙鍐宠儨灞闈級 @@ -831,12 +839,12 @@ int NineChessAi_ab::alphaBetaPruning(int depth, int alpha, int beta, Node *node) } // 鏇存柊鏇存繁灞傛暟鎹 else { - hashMapMutex.lock(); + //hashMapMutex.lock(); if (hashValue.depth < depth) { hashValue.value = node->value; hashValue.depth = depth; } - hashMapMutex.unlock(); + //hashMapMutex.unlock(); } #endif @@ -848,12 +856,13 @@ int NineChessAi_ab::alphaBetaPruning(int depth, int alpha, int beta, Node *node) } #ifdef HASH_MAP_ENABLE -int NineChessAi_ab::recordHash(const HashValue &hashValue) +int NineChessAi_ab::recordHash(HashValue &hashValue) { #ifdef HASH_MAP_ENABLE - hashMapMutex.lock(); - hashMap.insert(hashValue.hash, hashValue); - hashMapMutex.unlock(); + //hashMapMutex.lock(); + //HashMap::insert(hashValue.hash, hashValue); + + //hashMapMutex.unlock(); #endif // HASH_MAP_ENABLE return 0; @@ -956,9 +965,10 @@ const char *NineChessAi_ab::move2string(int move) } #ifdef HASH_MAP_ENABLE +#if 0 NineChessAi_ab::HashValue NineChessAi_ab::findHash(uint64_t hash) { - NineChessAi_ab::HashValue hashValue = hashMap.find(hash); + // NineChessAi_ab::HashValue hashValue = hashmap.find(hash); // TODO: 鍙樻崲灞闈 #if 0 @@ -984,13 +994,14 @@ NineChessAi_ab::HashValue NineChessAi_ab::findHash(uint64_t hash) } #endif - return hashValue; + return 0; } +#endif void NineChessAi_ab::clearHashMap() { - hashMapMutex.lock(); - hashMap.clear(); - hashMapMutex.unlock(); + //hashMapMutex.lock(); + //hashMap.clear(); + //hashMapMutex.unlock(); } -#endif /* HASH_MAP_ENABLE */ \ No newline at end of file +#endif /* HASH_MAP_ENABLE */ diff --git a/NineChess/src/ninechessai_ab.h b/NineChess/src/ninechessai_ab.h index 5dd6a7c5..7f61d22d 100644 --- a/NineChess/src/ninechessai_ab.h +++ b/NineChess/src/ninechessai_ab.h @@ -29,7 +29,7 @@ class NineChessAi_ab public: #ifdef HASH_MAP_ENABLE // 瀹氫箟鍝堝笇鍊肩殑绫诲瀷 - enum HashType : int16_t + enum HashType { hashfEMPTY = 0, hashfALPHA = 1, @@ -129,11 +129,6 @@ public: static bool nodeLess(const Node *first, const Node *second); static bool nodeGreater(const Node *first, const Node *second); -#ifdef HASH_MAP_ENABLE - static std::mutex hashMapMutex; - static HashMap hashMap; -#endif - protected: // 鐢熸垚鎵鏈夊悎娉曠殑鐫娉曞苟寤虹珛瀛愯妭鐐 void generateLegalMoves(Node *node); @@ -152,7 +147,7 @@ protected: #ifdef HASH_MAP_ENABLE // 鎻掑叆鍝堝笇琛 - int recordHash(const HashValue &hashValue); + int recordHash(HashValue &hashValue); #endif // 璇勪环鍑芥暟 @@ -213,9 +208,6 @@ private: array movePriorityTable; #endif // MOVE_PRIORITY_TABLE_SUPPORT - // 鍝堝笇琛ㄧ殑榛樿澶у皬 - static const size_t maxHashCount = 1024 * 1024; - // 瀹氫箟鏋佸ぇ鍊 static const int INF_VALUE = 0x1 << 30; @@ -225,6 +217,10 @@ private: private: // 鍛戒护琛 char cmdline[32]; + +#ifdef HASH_MAP_ENABLE + HashMap hashmap; +#endif }; #endif