Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Movegen speedup via king attack info #638

Merged
merged 9 commits into from
Jan 5, 2019
Merged

Movegen speedup via king attack info #638

merged 9 commits into from
Jan 5, 2019

Conversation

ddobbelaere
Copy link
Contributor

@ddobbelaere ddobbelaere commented Dec 31, 2018

Various movegen optimizations centered around the introduction of precomputed king attack info (attacking lines, pinned pieces, double check detection) that is used to speedup several parts of legal move detection (for all moves).

Unconditional speedup for random backend up to 6% (tested with LTO builds on Linux Fedora 29 64-bit, gcc 8.2.1).

The following measurements all share the command line lc0 benchmark --backend=random --smart-pruning-factor=0 --nodes=1000000 --nncache=1000000. A 95% confidence interval for the average of the last reported nps value over 25 runs is given for the starting position, the opening position https://lichess.org/editor/rn2kb1r/pp3ppp/2p1pnb1/8/3P1N1q/4B1N1/PPPQ1PPP/R3KB1R_b_KQkq_-_5_9 and the endgame position https://lichess.org/editor/8/2R2P2/1p4K1/6R1/1r6/5rP1/1k6/2q5_w_-_-_0_55 .

The measurements are performed on an Intel Core i5-5300U CPU @ 2.30GHz with 4 cores and L1d cache: 32K, L1i cache: 32K, L2 cache: 256K, L3 cache: 3072K.

For 1 search thread:

position master 6a639b6 this PR speedup
start 208222 ± 448 215247 ± 726 3.4%
opening 161321 ± 216 168776 ± 335 4.6%
endgame 176833 ± 245 186576 ± 283 5.5%

For 2 search threads:

position master 6a639b6 this PR speedup
start 271137 ± 830 272221 ± 882 0.4%
opening 224128 ± 422 227968 ± 551 1.7%
endgame 238111 ± 511 243892 ± 758 2.4%

@ddobbelaere ddobbelaere changed the title [WIP] Movegen speedup [WIP] Movegen speedup via king attack info Jan 1, 2019
This was referenced Jan 1, 2019
@ddobbelaere ddobbelaere changed the title [WIP] Movegen speedup via king attack info Movegen speedup via king attack info Jan 2, 2019
@ddobbelaere
Copy link
Contributor Author

This PR is ready for review.

@mooskagh
Copy link
Member

mooskagh commented Jan 5, 2019

On my machine chessboard_test with uncommented tests:
master:

[ RUN      ] ChessBoard.MoveGenStartingPos
[       OK ] ChessBoard.MoveGenStartingPos (6676 ms)
[ RUN      ] ChessBoard.MoveGenKiwipete
[       OK ] ChessBoard.MoveGenKiwipete (11287 ms)
[ RUN      ] ChessBoard.MoveGenPosition3
[       OK ] ChessBoard.MoveGenPosition3 (868 ms)
[ RUN      ] ChessBoard.MoveGenPosition4
[       OK ] ChessBoard.MoveGenPosition4 (1052 ms)
[ RUN      ] ChessBoard.MoveGenPosition5
[       OK ] ChessBoard.MoveGenPosition5 (5088 ms)
[ RUN      ] ChessBoard.MoveGenPosition6
[       OK ] ChessBoard.MoveGenPosition6 (202 ms)

With this PR

[ RUN      ] ChessBoard.MoveGenStartingPos
[       OK ] ChessBoard.MoveGenStartingPos (5931 ms)
[ RUN      ] ChessBoard.MoveGenKiwipete
[       OK ] ChessBoard.MoveGenKiwipete (9873 ms)
[ RUN      ] ChessBoard.MoveGenPosition3
[       OK ] ChessBoard.MoveGenPosition3 (739 ms)
[ RUN      ] ChessBoard.MoveGenPosition4
[       OK ] ChessBoard.MoveGenPosition4 (892 ms)
[ RUN      ] ChessBoard.MoveGenPosition5
[       OK ] ChessBoard.MoveGenPosition5 (4413 ms)
[ RUN      ] ChessBoard.MoveGenPosition6
[       OK ] ChessBoard.MoveGenPosition6 (184 ms)

Looks good!

(for the reference, uncommented lines:

--- a/src/chess/board_test.cc
+++ b/src/chess/board_test.cc
@@ -94,7 +94,7 @@ int Perft(const ChessBoard& board, int max_depth, bool dump = false,
 }
 }  // namespace
 
-/* TEST(ChessBoard, MoveGenStartingPos) {
+TEST(ChessBoard, MoveGenStartingPos) {
   ChessBoard board;
   board.SetFromFen(ChessBoard::kStartposFen);
 
@@ -105,7 +105,7 @@ int Perft(const ChessBoard& board, int max_depth, bool dump = false,
   EXPECT_EQ(Perft(board, 4), 197281);
   EXPECT_EQ(Perft(board, 5), 4865609);
   EXPECT_EQ(Perft(board, 6), 119060324);
-} */
+}
 
 TEST(ChessBoard, MoveGenKiwipete) {
   ChessBoard board;
@@ -116,7 +116,7 @@ TEST(ChessBoard, MoveGenKiwipete) {
   EXPECT_EQ(Perft(board, 2), 2039);
   EXPECT_EQ(Perft(board, 3), 97862);
   EXPECT_EQ(Perft(board, 4), 4085603);
-//  EXPECT_EQ(Perft(board, 5), 193690690);
+  EXPECT_EQ(Perft(board, 5), 193690690);
 }
 
 TEST(ChessBoard, MoveGenPosition3) {
@@ -151,7 +151,7 @@ TEST(ChessBoard, MoveGenPosition5) {
   EXPECT_EQ(Perft(board, 2), 1486);
   EXPECT_EQ(Perft(board, 3), 62379);
   EXPECT_EQ(Perft(board, 4), 2103487);
-//  EXPECT_EQ(Perft(board, 5), 89941194);
+  EXPECT_EQ(Perft(board, 5), 89941194);
 }
 
 TEST(ChessBoard, MoveGenPosition6) {

)

@mooskagh mooskagh merged commit 3ac3745 into LeelaChessZero:master Jan 5, 2019
@ddobbelaere ddobbelaere deleted the movegen-speedup branch January 19, 2019 14:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants