Use incremental updates more often

Use incremental updates for accumulators for up to 2 plies.
Do not copy accumulator. About 2% speedup.

Passed STC:
LLR: 2.95 (-2.94,2.94) {-0.25,1.25}
Total: 21752 W: 2583 L: 2403 D: 16766
Ptnml(0-2): 128, 1761, 6923, 1931, 133
https://tests.stockfishchess.org/tests/view/5f7150cf3b22d6afa5069412

closes https://github.com/official-stockfish/Stockfish/pull/3157

No functional change
This commit is contained in:
noobpwnftw
2020-09-28 02:29:21 +08:00
committed by Joost VandeVondele
parent 1dbd2a1ad5
commit c065abdcaf
4 changed files with 87 additions and 30 deletions

View File

@@ -61,26 +61,69 @@ namespace Eval::NNUE::Features {
const PositionType& pos, TriggerEvent trigger,
IndexListType removed[2], IndexListType added[2], bool reset[2]) {
const auto& dp = pos.state()->dirtyPiece;
if (dp.dirty_num == 0) return;
for (Color perspective : { WHITE, BLACK }) {
reset[perspective] = false;
switch (trigger) {
case TriggerEvent::kFriendKingMoved:
reset[perspective] = dp.piece[0] == make_piece(perspective, KING);
break;
default:
assert(false);
break;
auto collect_for_one = [&](const DirtyPiece& dp) {
for (Color perspective : { WHITE, BLACK }) {
switch (trigger) {
case TriggerEvent::kFriendKingMoved:
reset[perspective] = dp.piece[0] == make_piece(perspective, KING);
break;
default:
assert(false);
break;
}
if (reset[perspective]) {
Derived::CollectActiveIndices(
pos, trigger, perspective, &added[perspective]);
} else {
Derived::CollectChangedIndices(
pos, dp, trigger, perspective,
&removed[perspective], &added[perspective]);
}
}
if (reset[perspective]) {
Derived::CollectActiveIndices(
pos, trigger, perspective, &added[perspective]);
};
auto collect_for_two = [&](const DirtyPiece& dp1, const DirtyPiece& dp2) {
for (Color perspective : { WHITE, BLACK }) {
switch (trigger) {
case TriggerEvent::kFriendKingMoved:
reset[perspective] = dp1.piece[0] == make_piece(perspective, KING)
|| dp2.piece[0] == make_piece(perspective, KING);
break;
default:
assert(false);
break;
}
if (reset[perspective]) {
Derived::CollectActiveIndices(
pos, trigger, perspective, &added[perspective]);
} else {
Derived::CollectChangedIndices(
pos, dp1, trigger, perspective,
&removed[perspective], &added[perspective]);
Derived::CollectChangedIndices(
pos, dp2, trigger, perspective,
&removed[perspective], &added[perspective]);
}
}
};
if (pos.state()->previous->accumulator.computed_accumulation) {
const auto& prev_dp = pos.state()->dirtyPiece;
if (prev_dp.dirty_num == 0) return;
collect_for_one(prev_dp);
} else {
const auto& prev_dp = pos.state()->previous->dirtyPiece;
if (prev_dp.dirty_num == 0) {
const auto& prev2_dp = pos.state()->dirtyPiece;
if (prev2_dp.dirty_num == 0) return;
collect_for_one(prev2_dp);
} else {
Derived::CollectChangedIndices(
pos, trigger, perspective,
&removed[perspective], &added[perspective]);
const auto& prev2_dp = pos.state()->dirtyPiece;
if (prev2_dp.dirty_num == 0) {
collect_for_one(prev_dp);
} else {
collect_for_two(prev_dp, prev2_dp);
}
}
}
}
@@ -115,11 +158,11 @@ namespace Eval::NNUE::Features {
// Get a list of indices for recently changed features
static void CollectChangedIndices(
const Position& pos, const TriggerEvent trigger, const Color perspective,
const Position& pos, const DirtyPiece& dp, const TriggerEvent trigger, const Color perspective,
IndexList* const removed, IndexList* const added) {
if (FeatureType::kRefreshTrigger == trigger) {
FeatureType::AppendChangedIndices(pos, perspective, removed, added);
FeatureType::AppendChangedIndices(pos, dp, perspective, removed, added);
}
}

View File

@@ -52,11 +52,10 @@ namespace Eval::NNUE::Features {
// Get a list of indices for recently changed features
template <Side AssociatedKing>
void HalfKP<AssociatedKing>::AppendChangedIndices(
const Position& pos, Color perspective,
const Position& pos, const DirtyPiece& dp, Color perspective,
IndexList* removed, IndexList* added) {
Square ksq = orient(perspective, pos.square<KING>(perspective));
const auto& dp = pos.state()->dirtyPiece;
for (int i = 0; i < dp.dirty_num; ++i) {
Piece pc = dp.piece[i];
if (type_of(pc) == KING) continue;

View File

@@ -50,7 +50,7 @@ namespace Eval::NNUE::Features {
IndexList* active);
// Get a list of indices for recently changed features
static void AppendChangedIndices(const Position& pos, Color perspective,
static void AppendChangedIndices(const Position& pos, const DirtyPiece& dp, Color perspective,
IndexList* removed, IndexList* added);
private: