Skip to content

Commit

Permalink
write parent by default; never write in null move search
Browse files Browse the repository at this point in the history
No other functional change
  • Loading branch information
dhbloo committed Jul 11, 2024
1 parent 1439a59 commit 87ae137
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Rapfi/command/gomocup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ void queryDatabaseOne(bool getPosition)
if (Search::Threads.dbStorage()) {
DBClient dbClient(*Search::Threads.dbStorage(), RECORD_MASK_ALL);
DBRecord record;
if (dbClient.query(*board, options.rule, record) && !record.isNull())
if (dbClient.query(*board, options.rule, record))
MESSAGEL("DATABASE ONE " << int(record.label) << ' ' << record.value << ' '
<< record.depth() << ' ' << int(record.bound()) << ' '
<< record.displayLabel());
Expand Down
11 changes: 7 additions & 4 deletions Rapfi/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ std::string DatabaseURL;
/// Database storage factory function
std::function<std::unique_ptr<::Database::DBStorage>(std::string)> DatabaseMaker;
/// Database client cache sizes
size_t DatabaseCacheSize = 2048;
size_t DatabaseRecordCacheSize = 16384;
size_t DatabaseCacheSize = 4096;
size_t DatabaseRecordCacheSize = 32768;

// Library import options

Expand All @@ -169,6 +169,8 @@ bool DatabaseLibIgnoreBoardText = false;

/// Whether to write/update the database in search
bool DatabaseReadonlyMode = false;
/// Whether to always write parent node if any of the children are written
bool DatabaseMandatoryParentWrite = true;

/// Search before this ply is required to query the database
int DatabaseQueryPly = 3;
Expand Down Expand Up @@ -693,8 +695,9 @@ void Config::readDatabase(const cpptoml::table &t)
}

if (auto s = t.get_table("search")) {
DatabaseReadonlyMode = s->get_as<bool>("readonly_mode").value_or(false);
DatabaseQueryPly = s->get_as<int>("query_ply").value_or(DatabaseQueryPly);
DatabaseReadonlyMode = s->get_as<bool>("readonly_mode").value_or(false);
DatabaseMandatoryParentWrite = s->get_as<bool>("mandatory_parent_write").value_or(true);
DatabaseQueryPly = s->get_as<int>("query_ply").value_or(DatabaseQueryPly);
DatabaseQueryPVIterPerPlyIncrement = s->get_as<int>("pv_iter_per_ply_increment")
.value_or(DatabaseQueryPVIterPerPlyIncrement);
DatabaseQueryNonPVIterPerPlyIncrement =
Expand Down
1 change: 1 addition & 0 deletions Rapfi/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ extern bool DatabaseLibIgnoreBoardText;

// Database search options
extern bool DatabaseReadonlyMode;
extern bool DatabaseMandatoryParentWrite;
extern int DatabaseQueryPly;
extern int DatabaseQueryPVIterPerPlyIncrement;
extern int DatabaseQueryNonPVIterPerPlyIncrement;
Expand Down
36 changes: 32 additions & 4 deletions Rapfi/search/ab/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,9 @@ Value search(Board &board, SearchStack *ss, Value alpha, Value beta, Depth depth
// starts with statScore = 0. Later grandchildren start with the last calculated
// statScore of the previous grandchild.
(ss + 2)->statScore = 0;

// Pass current number of null moves to next ply
(ss + 1)->numNullMoves = ss->numNullMoves;
}
else
searchData->rootDelta = beta - alpha;
Expand Down Expand Up @@ -860,10 +863,12 @@ Value search(Board &board, SearchStack *ss, Value alpha, Value beta, Depth depth
Depth r = nullMoveReduction<Rule>(depth);
ss->currentMove = Pos::PASS;

(ss + 1)->numNullMoves++;
board.doPassMove();
TT.prefetch(board.zobristKey());
value = -search<Rule, NonPV>(board, ss + 1, -beta, -beta + 1, depth - r, !cutNode);
board.undoPassMove();
(ss + 1)->numNullMoves--;

if (value >= beta) {
// Do not return unproven mate scores
Expand Down Expand Up @@ -912,6 +917,7 @@ Value search(Board &board, SearchStack *ss, Value alpha, Value beta, Depth depth
ABSearcher *searcher = static_cast<ABSearcher *>(thisThread->threads.searcher());
TimeControl &timectl = searcher->timectl;
uint64_t curNumNodes = 0;
ss->dbChildWritten = false;

// Calculate a complexity metric for current position
uint16_t complexCount = 1;
Expand Down Expand Up @@ -1211,8 +1217,18 @@ Value search(Board &board, SearchStack *ss, Value alpha, Value beta, Depth depth
// Step 18. Check for a new best move
// Finished searching the move. If a stop occurred, the return value of the search cannot
// be trusted, so we return immediately without updating best move, PV and TT.
if (thisThread->threads.isTerminating())
if (thisThread->threads.isTerminating()) {
if (thisThread->dbClient
&& !dbHit // Write when no dbHit, we never overwrite any existing record with null
&& !Config::DatabaseReadonlyMode // Never write in database readonly mode
&& ss->dbChildWritten // Write anyway if we have children that have already written
) {
Database::DBRecord newRecord {Database::LABEL_NULL, 0, 0};
thisThread->dbClient->save(board, Rule, newRecord, Config::DatabaseOverwriteRule);
(ss - 1)->dbChildWritten = true;
}
return VALUE_NONE;
}
// This move is blocked from database record
if (value == VALUE_BLOCKED)
continue;
Expand Down Expand Up @@ -1351,7 +1367,11 @@ Value search(Board &board, SearchStack *ss, Value alpha, Value beta, Depth depth

// Step 20. Update database record
Bound bound = bestValue >= beta ? BOUND_LOWER : PvNode && bestMove ? BOUND_EXACT : BOUND_UPPER;
if (thisThread->dbClient && !Config::DatabaseReadonlyMode && !skipMove && !options.balanceMode
if (thisThread->dbClient
&& !Config::DatabaseReadonlyMode // Never write in database readonly mode
&& !options.balanceMode // Never write when we are doing balanced search
&& (!skipMove || ss->dbChildWritten) // Never write when in singular extension
&& ss->numNullMoves == 0 // Never write when in null move search
&& !(RootNode && (searchData->pvIdx || options.blockMoves.size()))) {
bool exact = PvNode && bound == BOUND_EXACT;
bool isWin = bestValue > VALUE_MATE_IN_MAX_PLY && (bound & BOUND_LOWER);
Expand Down Expand Up @@ -1382,6 +1402,7 @@ Value search(Board &board, SearchStack *ss, Value alpha, Value beta, Depth depth
}

if (RootNode
|| ss->dbChildWritten // Write anyway if we have children that have already written
|| PvNode && ss->ply <= 1 + isLoss
&& options.multiPV > 1 // Always add new record in multipv
|| ss->ply <= writePly - isWin // Loss label are recorded one ply less
Expand All @@ -1406,8 +1427,15 @@ Value search(Board &board, SearchStack *ss, Value alpha, Value beta, Depth depth

// Write if there is no db hit, or the new record satisfy the overwrite rule
if (!dbHit
|| Database::checkOverwrite(dbRecord, newRecord, Config::DatabaseOverwriteRule))
thisThread->dbClient->save(board, Rule, newRecord, Database::OverwriteRule::Always);
|| Database::checkOverwrite(dbRecord, newRecord, Config::DatabaseOverwriteRule)) {
thisThread->dbClient->save(board,
Rule,
newRecord,
dbHit ? Database::OverwriteRule::Always
: Config::DatabaseOverwriteRule);
if (Config::DatabaseMandatoryParentWrite)
(ss - 1)->dbChildWritten = true;
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions Rapfi/search/ab/searchstack.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ struct SearchStack
Pos skipMove;
Pos killers[2];
Pattern4 moveP4[SIDE_NB];
int16_t numNullMoves;
bool ttPv;
bool dbChildWritten;

/// Append current move and to the end of child PV.
void updatePv(Pos move)
Expand Down

0 comments on commit 87ae137

Please sign in to comment.