fairy-stockfish / Fairy-Stockfish

chess variant engine supporting Xiangqi, Shogi, Janggi, Makruk, S-Chess, Crazyhouse, Bughouse, and many more
https://fairy-stockfish.github.io/
GNU General Public License v3.0
603 stars 189 forks source link

Performance improvements #253

Open ianfab opened 3 years ago

ianfab commented 3 years ago

The generalizations and feature additions compared to official Stockfish by necessity cause some overhead and hence a slowdown in NPS in Fairy-Stockfish. However, there should still be quite some room for regaining some of that performance e.g. by using faster code for variants for which the generalizations are not necessary, and/or pre-calculating derived properties from the variant rules.

Observations

Some methods that seem to be especially slow are:

Related tests/ideas

http://www.variantfishtest.org:6543/tests/view/60268e806e23db669974e8f0 http://www.variantfishtest.org:6543/tests/view/5fd4cf0f6e23db221d9e9511 http://www.variantfishtest.org:6543/tests/view/5fd4da786e23db221d9e9519 http://www.variantfishtest.org:6543/tests/view/5fc37df16e23db221d9e9430 http://www.variantfishtest.org:6543/tests/view/6026a0606e23db669974e8f7

Profiling

E.g., graphical profiling can be used to identify bottlenecks. Steps (also see http://talkchess.com/forum3/viewtopic.php?t=61373):

  1. change compiler flags to enable profiling information. Different optimization levels (O3, Os, ...) can sometimes also make quite a difference, but using too much optimization can make it hard to interpret the results.

    • CXXFLAGS += -Os -g -pg
  2. run bench. Optionally increase depth to get more accurate results, and run bench for different variants to check how it behaves depending on the rules of the game:

    • ./stockfish bench
  3. Create graphical profile:

huynq55 commented 2 years ago

I tested the bench command on my machine. Looks like it doesn't support NNUE. image And I used 1 thread because somehow when running more than 1 thread FS freezes.

ianfab commented 2 years ago

See the bench arguments at https://github.com/ianfab/Fairy-Stockfish/blob/67209f150d44b6ac5fcc01659442fcc9a4815e95/src/benchmark.cpp#L131-L136

The default mixed means that it uses non-NNUE on odd numbered positions and NNUE on even numbered positions. Since we only have one position here, it uses non-NNUE. If you want to enforce usage of NNUE, you need to specify it, e.g., like bench xiangqi 16 1 13 default depth NNUE.