Skip to content

Commit bf25df4

Browse files
committed
Merge branch 'remove_memcpy_entirely_pls_gain'
bench: 4271540 ``` ELO | 7.83 +- 5.54 (95%) CONF | 8.0+0.08s Threads=1 Hash=8MB GAMES | N: 7192 W: 1796 L: 1634 D: 3762 ```
2 parents d3ddd32 + c27b8d3 commit bf25df4

File tree

9 files changed

+177
-39
lines changed

9 files changed

+177
-39
lines changed

src_files/nn/accumulator.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
1-
//
2-
// Created by Luecx on 28.01.2023.
3-
//
1+
/****************************************************************************************************
2+
* *
3+
* Koivisto UCI Chess engine *
4+
* by. Kim Kahre and Finn Eggers *
5+
* *
6+
* Koivisto is free software: you can redistribute it and/or modify *
7+
* it under the terms of the GNU General Public License as published by *
8+
* the Free Software Foundation, either version 3 of the License, or *
9+
* (at your option) any later version. *
10+
* Koivisto is distributed in the hope that it will be useful, *
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13+
* GNU General Public License for more details. *
14+
* You should have received a copy of the GNU General Public License *
15+
* along with Koivisto. If not, see <http://www.gnu.org/licenses/>. *
16+
* *
17+
****************************************************************************************************/
18+
419
#include "accumulator.h"
520
#include "../board.h"
621

@@ -48,7 +63,7 @@ void nn::AccumulatorTable::use(bb::Color view, Board* board, nn::Evaluator& eval
4863
}
4964
}
5065

51-
std::memcpy(evaluator.history.back().summation[view], entry.accumulator.summation[view],sizeof(int16_t) * HIDDEN_SIZE);
66+
std::memcpy(evaluator.history[evaluator.history_index].summation[view], entry.accumulator.summation[view],sizeof(int16_t) * HIDDEN_SIZE);
5267
}
5368

5469
void nn::AccumulatorTable::reset() {

src_files/nn/accumulator.h

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1-
//
2-
// Created by Luecx on 28.01.2023.
3-
//
1+
/****************************************************************************************************
2+
* *
3+
* Koivisto UCI Chess engine *
4+
* by. Kim Kahre and Finn Eggers *
5+
* *
6+
* Koivisto is free software: you can redistribute it and/or modify *
7+
* it under the terms of the GNU General Public License as published by *
8+
* the Free Software Foundation, either version 3 of the License, or *
9+
* (at your option) any later version. *
10+
* Koivisto is distributed in the hope that it will be useful, *
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13+
* GNU General Public License for more details. *
14+
* You should have received a copy of the GNU General Public License *
15+
* along with Koivisto. If not, see <http://www.gnu.org/licenses/>. *
16+
* *
17+
****************************************************************************************************/
418

519
#ifndef KOIVISTO_ACCUMULATOR_H
620
#define KOIVISTO_ACCUMULATOR_H
@@ -45,30 +59,36 @@ struct AccumulatorTable {
4559
void reset();
4660
} __attribute__((aligned(128)));
4761

62+
63+
4864
// adding weights to an accumulator
4965
template<bool V>
50-
inline void addWeightsToAccumulator(const int idx, int16_t* accumulator){
66+
inline void addWeightsToAccumulator(const int idx, int16_t* src, int16_t* target){
5167
const auto wgt = (avx_register_type_16*) (nn::inputWeights[idx]);
52-
const auto sum = (avx_register_type_16*) (accumulator);
68+
const auto inp = (avx_register_type_16*) (src);
69+
const auto out = (avx_register_type_16*) (target);
5370
if constexpr (V) {
5471
for (int i = 0; i < HIDDEN_SIZE / STRIDE_16_BIT / 4; i++) {
55-
sum[i * 4 + 0] = avx_add_epi16(sum[i * 4 + 0], wgt[i * 4 + 0]);
56-
sum[i * 4 + 1] = avx_add_epi16(sum[i * 4 + 1], wgt[i * 4 + 1]);
57-
sum[i * 4 + 2] = avx_add_epi16(sum[i * 4 + 2], wgt[i * 4 + 2]);
58-
sum[i * 4 + 3] = avx_add_epi16(sum[i * 4 + 3], wgt[i * 4 + 3]);
72+
out[i * 4 + 0] = avx_add_epi16(inp[i * 4 + 0], wgt[i * 4 + 0]);
73+
out[i * 4 + 1] = avx_add_epi16(inp[i * 4 + 1], wgt[i * 4 + 1]);
74+
out[i * 4 + 2] = avx_add_epi16(inp[i * 4 + 2], wgt[i * 4 + 2]);
75+
out[i * 4 + 3] = avx_add_epi16(inp[i * 4 + 3], wgt[i * 4 + 3]);
5976
}
6077
} else {
6178
for (int i = 0; i < HIDDEN_SIZE / STRIDE_16_BIT / 4; i++) {
62-
sum[i * 4 + 0] = avx_sub_epi16(sum[i * 4 + 0], wgt[i * 4 + 0]);
63-
sum[i * 4 + 1] = avx_sub_epi16(sum[i * 4 + 1], wgt[i * 4 + 1]);
64-
sum[i * 4 + 2] = avx_sub_epi16(sum[i * 4 + 2], wgt[i * 4 + 2]);
65-
sum[i * 4 + 3] = avx_sub_epi16(sum[i * 4 + 3], wgt[i * 4 + 3]);
79+
out[i * 4 + 0] = avx_sub_epi16(inp[i * 4 + 0], wgt[i * 4 + 0]);
80+
out[i * 4 + 1] = avx_sub_epi16(inp[i * 4 + 1], wgt[i * 4 + 1]);
81+
out[i * 4 + 2] = avx_sub_epi16(inp[i * 4 + 2], wgt[i * 4 + 2]);
82+
out[i * 4 + 3] = avx_sub_epi16(inp[i * 4 + 3], wgt[i * 4 + 3]);
6683
}
6784
}
6885
}
6986

70-
71-
87+
// adding weights to an accumulator
88+
template<bool V>
89+
inline void addWeightsToAccumulator(const int idx, int16_t* accumulator){
90+
addWeightsToAccumulator<V>(idx, accumulator, accumulator);
91+
}
7292

7393
}
7494

src_files/nn/defs.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1-
//
2-
// Created by Luecx on 28.01.2023.
3-
//
1+
/****************************************************************************************************
2+
* *
3+
* Koivisto UCI Chess engine *
4+
* by. Kim Kahre and Finn Eggers *
5+
* *
6+
* Koivisto is free software: you can redistribute it and/or modify *
7+
* it under the terms of the GNU General Public License as published by *
8+
* the Free Software Foundation, either version 3 of the License, or *
9+
* (at your option) any later version. *
10+
* Koivisto is distributed in the hope that it will be useful, *
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13+
* GNU General Public License for more details. *
14+
* You should have received a copy of the GNU General Public License *
15+
* along with Koivisto. If not, see <http://www.gnu.org/licenses/>. *
16+
* *
17+
****************************************************************************************************/
418

519
#ifndef KOIVISTO_DEFS_H
620
#define KOIVISTO_DEFS_H

src_files/nn/eval.cpp

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,26 @@ void nn::Evaluator::setPieceOnSquareAccumulator(bb::Color side, bb::PieceType pi
6363
bb::Color pieceColor, bb::Square square,
6464
bb::Square kingSquare) {
6565
const int idx = index(pieceType, pieceColor, square, side, kingSquare);
66-
addWeightsToAccumulator<value>(idx, history.back().summation[side]);
66+
67+
if(!accumulator_is_initialised[side]){
68+
addWeightsToAccumulator<value>(idx, history[history_index-1].summation[side], history[history_index].summation[side]);
69+
accumulator_is_initialised[side] = true;
70+
}
71+
else{
72+
addWeightsToAccumulator<value>(idx, history[history_index].summation[side]);
73+
}
6774
}
6875

6976
void nn::Evaluator::reset(Board* board) {
7077
history.resize(1);
78+
this->history_index = 0;
7179
resetAccumulator(board, bb::WHITE);
7280
resetAccumulator(board, bb::BLACK);
7381
}
7482

7583
void nn::Evaluator::resetAccumulator(Board* board, bb::Color color) {
7684
accumulator_table->use(color, board, *this);
85+
accumulator_is_initialised[color] = true;
7786
}
7887

7988
int nn::Evaluator::evaluate(bb::Color activePlayer, Board* board) {
@@ -82,8 +91,8 @@ int nn::Evaluator::evaluate(bb::Color activePlayer, Board* board) {
8291
}
8392
constexpr avx_register_type_16 reluBias {};
8493

85-
const auto acc_act = (avx_register_type_16*) history.back().summation[activePlayer];
86-
const auto acc_nac = (avx_register_type_16*) history.back().summation[!activePlayer];
94+
const auto acc_act = (avx_register_type_16*) history[history_index].summation[activePlayer];
95+
const auto acc_nac = (avx_register_type_16*) history[history_index].summation[!activePlayer];
8796

8897
// compute the dot product
8998
avx_register_type_32 res {};
@@ -102,24 +111,40 @@ int nn::Evaluator::evaluate(bb::Color activePlayer, Board* board) {
102111

103112
nn::Evaluator::Evaluator() {
104113
this->history.push_back(Accumulator {});
114+
this->history_index = 0;
105115
this->accumulator_table->reset();
106116
}
107117

108118
nn::Evaluator::Evaluator(const nn::Evaluator& evaluator) {
109119
history = evaluator.history;
120+
history_index = evaluator.history_index;
110121
}
111122
nn::Evaluator& nn::Evaluator::operator=(const nn::Evaluator& evaluator) {
112123
this->history = evaluator.history;
124+
this->history_index = evaluator.history_index;
113125
return *this;
114126
}
115127

116-
void nn::Evaluator::addNewAccumulation() { this->history.emplace_back(this->history.back()); }
128+
void nn::Evaluator::addNewAccumulation() {
129+
history_index ++;
130+
// enlarge history if required
131+
if(history_index >= this->history.size()){
132+
this->history.resize(history_index + 1);
133+
}
134+
accumulator_is_initialised[bb::WHITE] = false;
135+
accumulator_is_initialised[bb::BLACK] = false;
136+
}
117137

118-
void nn::Evaluator::popAccumulation() { this->history.pop_back(); }
138+
void nn::Evaluator::popAccumulation() {
139+
history_index --;
140+
accumulator_is_initialised[bb::WHITE] = true;
141+
accumulator_is_initialised[bb::BLACK] = true;
142+
}
119143

120144
void nn::Evaluator::clearHistory() {
121145
this->history.clear();
122146
this->history.push_back(Accumulator {});
147+
this->history_index = 0;
123148
}
124149

125150
template void nn::Evaluator::setPieceOnSquare<true>(bb::PieceType pieceType, bb::Color pieceColor,

src_files/nn/eval.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ struct Evaluator {
3434
std::vector<Accumulator> history;
3535
std::unique_ptr<AccumulatorTable> accumulator_table =
3636
std::make_unique<AccumulatorTable>(AccumulatorTable());
37+
38+
// pointer to which accumulator in the history is the current accumulator so we dont need
39+
// to use popback but instead increment /decrement a counter
40+
uint32_t history_index = 0;
41+
42+
// true if the accumulator is initialised, otherwise false and we need to use the previous
43+
// depth accumulator as an input when updating accumulator
44+
bool accumulator_is_initialised[bb::N_COLORS] = {false, false};
3745

3846
Evaluator();
3947

src_files/nn/index.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1-
//
2-
// Created by Luecx on 28.01.2023.
3-
//
1+
/****************************************************************************************************
2+
* *
3+
* Koivisto UCI Chess engine *
4+
* by. Kim Kahre and Finn Eggers *
5+
* *
6+
* Koivisto is free software: you can redistribute it and/or modify *
7+
* it under the terms of the GNU General Public License as published by *
8+
* the Free Software Foundation, either version 3 of the License, or *
9+
* (at your option) any later version. *
10+
* Koivisto is distributed in the hope that it will be useful, *
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13+
* GNU General Public License for more details. *
14+
* You should have received a copy of the GNU General Public License *
15+
* along with Koivisto. If not, see <http://www.gnu.org/licenses/>. *
16+
* *
17+
****************************************************************************************************/

src_files/nn/index.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1-
//
2-
// Created by Luecx on 28.01.2023.
3-
//
1+
/****************************************************************************************************
2+
* *
3+
* Koivisto UCI Chess engine *
4+
* by. Kim Kahre and Finn Eggers *
5+
* *
6+
* Koivisto is free software: you can redistribute it and/or modify *
7+
* it under the terms of the GNU General Public License as published by *
8+
* the Free Software Foundation, either version 3 of the License, or *
9+
* (at your option) any later version. *
10+
* Koivisto is distributed in the hope that it will be useful, *
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13+
* GNU General Public License for more details. *
14+
* You should have received a copy of the GNU General Public License *
15+
* along with Koivisto. If not, see <http://www.gnu.org/licenses/>. *
16+
* *
17+
****************************************************************************************************/
418

519
#ifndef KOIVISTO_INDEX_H
620
#define KOIVISTO_INDEX_H

src_files/nn/weights.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1-
//
2-
// Created by Luecx on 28.01.2023.
3-
//
1+
/****************************************************************************************************
2+
* *
3+
* Koivisto UCI Chess engine *
4+
* by. Kim Kahre and Finn Eggers *
5+
* *
6+
* Koivisto is free software: you can redistribute it and/or modify *
7+
* it under the terms of the GNU General Public License as published by *
8+
* the Free Software Foundation, either version 3 of the License, or *
9+
* (at your option) any later version. *
10+
* Koivisto is distributed in the hope that it will be useful, *
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13+
* GNU General Public License for more details. *
14+
* You should have received a copy of the GNU General Public License *
15+
* along with Koivisto. If not, see <http://www.gnu.org/licenses/>. *
16+
* *
17+
****************************************************************************************************/
418
#include "weights.h"
519

620
#define INCBIN_STYLE INCBIN_STYLE_CAMEL

src_files/nn/weights.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1-
//
2-
// Created by Luecx on 28.01.2023.
3-
//
1+
/****************************************************************************************************
2+
* *
3+
* Koivisto UCI Chess engine *
4+
* by. Kim Kahre and Finn Eggers *
5+
* *
6+
* Koivisto is free software: you can redistribute it and/or modify *
7+
* it under the terms of the GNU General Public License as published by *
8+
* the Free Software Foundation, either version 3 of the License, or *
9+
* (at your option) any later version. *
10+
* Koivisto is distributed in the hope that it will be useful, *
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13+
* GNU General Public License for more details. *
14+
* You should have received a copy of the GNU General Public License *
15+
* along with Koivisto. If not, see <http://www.gnu.org/licenses/>. *
16+
* *
17+
****************************************************************************************************/
418

519
#ifndef KOIVISTO_WEIGHTS_H
620
#define KOIVISTO_WEIGHTS_H

0 commit comments

Comments
 (0)