diff --git a/Rapfi/database/dbclient.h b/Rapfi/database/dbclient.h index 1226f90..fc221dc 100644 --- a/Rapfi/database/dbclient.h +++ b/Rapfi/database/dbclient.h @@ -81,6 +81,9 @@ class DBClient /// This will can sync() before destroying current database instance. ~DBClient(); + /// Returns the underlying database storage instance. + DBStorage &getStorage() const { return storage; } + /// Query db record of the current position. /// @return Whether current position exists in the database. bool query(const Board &board, Rule rule, DBRecord &record); diff --git a/Rapfi/search/ab/search.cpp b/Rapfi/search/ab/search.cpp index d9f78e6..8c16140 100644 --- a/Rapfi/search/ab/search.cpp +++ b/Rapfi/search/ab/search.cpp @@ -758,34 +758,37 @@ Value search(Board &board, SearchStack *ss, Value alpha, Value beta, Depth depth dbLabelBound = BOUND_EXACT; goto try_database_cut; default: // Read depth, bound, value + goto try_database_cut; + try_database_cut: - if (!RootNode) { - int dbDepth = dbRecord.depth() + Config::DatabaseQueryResultDepthBoundBias; - bool dbCut = - dbDepth > depth - && (dbValue >= beta ? (dbBound & BOUND_LOWER) : (dbBound & BOUND_UPPER)); - if (!PvNode && dbCut - || dbLabelBound == BOUND_LOWER && dbValue >= beta // Win-score-cut - || dbLabelBound == BOUND_UPPER && dbValue <= alpha // Loss-score-cut - || dbLabelBound == BOUND_EXACT // Draw-score-cut - ) { - TT.store(posKey, - dbValue, - ss->staticEval, - ss->ttPv, - dbBound, - Pos::NONE, - dbLabelBound == BOUND_EXACT - ? (int)DEPTH_UPPER_BOUND - : std::min(dbDepth, (int)DEPTH_UPPER_BOUND), - ss->ply); - return dbValue; - } + if (RootNode) + break; - // Expand search window for database cut in PvNode - if (PvNode && (dbCut || dbLabelBound != BOUND_NONE)) - alpha = -VALUE_INFINITE, beta = VALUE_INFINITE; + int dbDepth = dbRecord.depth() + Config::DatabaseQueryResultDepthBoundBias; + bool dbCut = + dbDepth > depth + && (dbValue >= beta ? (dbBound & BOUND_LOWER) : (dbBound & BOUND_UPPER)); + if (!PvNode && dbCut + || dbLabelBound == BOUND_LOWER && dbValue >= beta // Win-score-cut + || dbLabelBound == BOUND_UPPER && dbValue <= alpha // Loss-score-cut + || dbLabelBound == BOUND_EXACT // Draw-score-cut + ) { + TT.store(posKey, + dbValue, + ss->staticEval, + ss->ttPv, + dbBound, + Pos::NONE, + dbLabelBound == BOUND_EXACT + ? (int)DEPTH_UPPER_BOUND + : std::min(dbDepth, (int)DEPTH_UPPER_BOUND), + ss->ply); + return dbValue; } + + // Expand search window for database cut in PvNode + if (PvNode && (dbCut || dbLabelBound != BOUND_NONE)) + alpha = -VALUE_INFINITE, beta = VALUE_INFINITE; } } } diff --git a/Rapfi/search/searchthread.cpp b/Rapfi/search/searchthread.cpp index 0c0a183..fcdd904 100644 --- a/Rapfi/search/searchthread.cpp +++ b/Rapfi/search/searchthread.cpp @@ -129,6 +129,14 @@ void SearchThread::clear() balance2Moves.clear(); numNodes = 0; selDepth = 0; + + // Setup dbClient for each thread + if (threads.dbStorage() && (!dbClient || &dbClient->getStorage() != threads.dbStorage())) { + dbClient = std::make_unique(*threads.dbStorage(), + Database::RECORD_MASK_LVDB, + Config::DatabaseCacheSize, + Config::DatabaseRecordCacheSize); + } } void MainSearchThread::clear() @@ -307,18 +315,8 @@ void ThreadPool::startThinking(const Board &board, const SearchOptions &options, assert(searcher()); // Clear and init all threads state - for (size_t i = 1; i < size(); i++) { - (*this)[i]->runTask([this](SearchThread &th) { - th.clear(); - - // Create dbClient for each thread - if (dbStorage() && !th.dbClient) - th.dbClient = std::make_unique(*dbStorage(), - Database::RECORD_MASK_LVDB, - Config::DatabaseCacheSize, - Config::DatabaseRecordCacheSize); - }); - } + for (size_t i = 1; i < size(); i++) + (*this)[i]->runTask([this](SearchThread &th) { th.clear(); }); main()->clear(); main()->inPonder = inPonder;