Skip to content

Commit 1132d89

Browse files
xu-shawnDisservin
authored andcommitted
Simplify Accumulator Updates
Passed Non-regression STC: LLR: 2.93 (-2.94,2.94) <-1.75,0.25> Total: 146848 W: 37915 L: 37820 D: 71113 Ptnml(0-2): 415, 16159, 40186, 16244, 420 https://tests.stockfishchess.org/tests/view/691772217ca878185233202b closes #6421 No functional change
1 parent d9fd516 commit 1132d89

File tree

2 files changed

+26
-32
lines changed

2 files changed

+26
-32
lines changed

src/nnue/nnue_accumulator.cpp

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
#include "nnue_accumulator.h"
2020

21-
#include <algorithm>
2221
#include <cassert>
2322
#include <cstdint>
2423
#include <new>
@@ -338,22 +337,23 @@ struct AccumulatorUpdateContext {
338337
};
339338

340339
fused_row_reduce<Vec16Wrapper, Dimensions, ops...>(
341-
(from.template acc<Dimensions>()).accumulation[perspective],
342-
(to.template acc<Dimensions>()).accumulation[perspective], to_weight_vector(indices)...);
340+
(from.template acc<Dimensions>()).accumulation[perspective].data(),
341+
(to.template acc<Dimensions>()).accumulation[perspective].data(),
342+
to_weight_vector(indices)...);
343343

344344
fused_row_reduce<Vec32Wrapper, PSQTBuckets, ops...>(
345-
(from.template acc<Dimensions>()).psqtAccumulation[perspective],
346-
(to.template acc<Dimensions>()).psqtAccumulation[perspective],
345+
(from.template acc<Dimensions>()).psqtAccumulation[perspective].data(),
346+
(to.template acc<Dimensions>()).psqtAccumulation[perspective].data(),
347347
to_psqt_weight_vector(indices)...);
348348
}
349349

350350
void apply(const typename FeatureSet::IndexList& added,
351351
const typename FeatureSet::IndexList& removed) {
352-
const auto fromAcc = from.template acc<Dimensions>().accumulation[perspective];
353-
const auto toAcc = to.template acc<Dimensions>().accumulation[perspective];
352+
const auto& fromAcc = from.template acc<Dimensions>().accumulation[perspective];
353+
auto& toAcc = to.template acc<Dimensions>().accumulation[perspective];
354354

355-
const auto fromPsqtAcc = from.template acc<Dimensions>().psqtAccumulation[perspective];
356-
const auto toPsqtAcc = to.template acc<Dimensions>().psqtAccumulation[perspective];
355+
const auto& fromPsqtAcc = from.template acc<Dimensions>().psqtAccumulation[perspective];
356+
auto& toPsqtAcc = to.template acc<Dimensions>().psqtAccumulation[perspective];
357357

358358
#ifdef VECTOR
359359
using Tiling = SIMDTiling<Dimensions, Dimensions, PSQTBuckets>;
@@ -448,8 +448,8 @@ struct AccumulatorUpdateContext {
448448

449449
#else
450450

451-
std::copy_n(fromAcc, Dimensions, toAcc);
452-
std::copy_n(fromPsqtAcc, PSQTBuckets, toPsqtAcc);
451+
toAcc = fromAcc;
452+
toPsqtAcc = fromPsqtAcc;
453453

454454
for (const auto index : removed)
455455
{
@@ -589,13 +589,10 @@ void update_accumulator_incremental(
589589
auto& targetAcc = target_state.template acc<TransformedFeatureDimensions>();
590590
const auto& sourceAcc = computed.template acc<TransformedFeatureDimensions>();
591591

592-
std::memcpy(targetAcc.accumulation[perspective], sourceAcc.accumulation[perspective],
593-
sizeof(targetAcc.accumulation[perspective]));
594-
std::memcpy(targetAcc.psqtAccumulation[perspective],
595-
sourceAcc.psqtAccumulation[perspective],
596-
sizeof(targetAcc.psqtAccumulation[perspective]));
592+
targetAcc.accumulation[perspective] = sourceAcc.accumulation[perspective];
593+
targetAcc.psqtAccumulation[perspective] = sourceAcc.psqtAccumulation[perspective];
594+
targetAcc.computed[perspective] = true;
597595

598-
targetAcc.computed[perspective] = true;
599596
return;
600597
}
601598

@@ -643,15 +640,16 @@ void update_accumulator_incremental(
643640
(target_state.template acc<TransformedFeatureDimensions>()).computed[perspective] = true;
644641
}
645642

646-
Bitboard get_changed_pieces(const Piece oldPieces[SQUARE_NB], const Piece newPieces[SQUARE_NB]) {
643+
Bitboard get_changed_pieces(const std::array<Piece, SQUARE_NB>& oldPieces,
644+
const std::array<Piece, SQUARE_NB>& newPieces) {
647645
#if defined(USE_AVX512) || defined(USE_AVX2)
648646
static_assert(sizeof(Piece) == 1);
649647
Bitboard sameBB = 0;
650648

651649
for (int i = 0; i < 64; i += 32)
652650
{
653-
const __m256i old_v = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(oldPieces + i));
654-
const __m256i new_v = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(newPieces + i));
651+
const __m256i old_v = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(&oldPieces[i]));
652+
const __m256i new_v = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(&newPieces[i]));
655653
const __m256i cmpEqual = _mm256_cmpeq_epi8(old_v, new_v);
656654
const std::uint32_t equalMask = _mm256_movemask_epi8(cmpEqual);
657655
sameBB |= static_cast<Bitboard>(equalMask) << i;
@@ -680,7 +678,7 @@ void update_accumulator_refresh_cache(Color pers
680678
auto& entry = cache[ksq][perspective];
681679
PSQFeatureSet::IndexList removed, added;
682680

683-
const Bitboard changedBB = get_changed_pieces(entry.pieces, pos.piece_array().data());
681+
const Bitboard changedBB = get_changed_pieces(entry.pieces, pos.piece_array());
684682
Bitboard removedBB = changedBB & entry.pieceBB;
685683
Bitboard addedBB = changedBB & pos.pieces();
686684

@@ -696,7 +694,7 @@ void update_accumulator_refresh_cache(Color pers
696694
}
697695

698696
entry.pieceBB = pos.pieces();
699-
std::copy_n(pos.piece_array().begin(), SQUARE_NB, entry.pieces);
697+
entry.pieces = pos.piece_array();
700698

701699
auto& accumulator = accumulatorState.acc<Dimensions>();
702700
accumulator.computed[perspective] = true;
@@ -812,12 +810,8 @@ void update_accumulator_refresh_cache(Color pers
812810

813811
// The accumulator of the refresh entry has been updated.
814812
// Now copy its content to the actual accumulator we were refreshing.
815-
816-
std::memcpy(accumulator.accumulation[perspective], entry.accumulation.data(),
817-
sizeof(BiasType) * Dimensions);
818-
819-
std::memcpy(accumulator.psqtAccumulation[perspective], entry.psqtAccumulation.data(),
820-
sizeof(int32_t) * PSQTBuckets);
813+
accumulator.accumulation[perspective] = entry.accumulation;
814+
accumulator.psqtAccumulation[perspective] = entry.psqtAccumulation;
821815
#endif
822816
}
823817

src/nnue/nnue_accumulator.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ class FeatureTransformer;
4646
// Class that holds the result of affine transformation of input features
4747
template<IndexType Size>
4848
struct alignas(CacheLineSize) Accumulator {
49-
std::int16_t accumulation[COLOR_NB][Size];
50-
std::int32_t psqtAccumulation[COLOR_NB][PSQTBuckets];
51-
std::array<bool, COLOR_NB> computed = {};
49+
std::array<std::array<std::int16_t, Size>, COLOR_NB> accumulation;
50+
std::array<std::array<std::int32_t, PSQTBuckets>, COLOR_NB> psqtAccumulation;
51+
std::array<bool, COLOR_NB> computed = {};
5252
};
5353

5454

@@ -71,7 +71,7 @@ struct AccumulatorCaches {
7171
struct alignas(CacheLineSize) Entry {
7272
std::array<BiasType, Size> accumulation;
7373
std::array<PSQTWeightType, PSQTBuckets> psqtAccumulation;
74-
Piece pieces[SQUARE_NB];
74+
std::array<Piece, SQUARE_NB> pieces;
7575
Bitboard pieceBB;
7676

7777
// To initialize a refresh entry, we set all its bitboards empty,

0 commit comments

Comments
 (0)