Skip to content

Commit

Permalink
fix a bug that main thread does not load dbclient
Browse files Browse the repository at this point in the history
[skip_fishtest]
  • Loading branch information
dhbloo committed Oct 14, 2024
1 parent ed79856 commit c37e166
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 37 deletions.
3 changes: 3 additions & 0 deletions Rapfi/database/dbclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
53 changes: 28 additions & 25 deletions Rapfi/search/ab/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down
22 changes: 10 additions & 12 deletions Rapfi/search/searchthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Database::DBClient>(*threads.dbStorage(),
Database::RECORD_MASK_LVDB,
Config::DatabaseCacheSize,
Config::DatabaseRecordCacheSize);
}
}

void MainSearchThread::clear()
Expand Down Expand Up @@ -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<Database::DBClient>(*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;
Expand Down

0 comments on commit c37e166

Please sign in to comment.