-
Notifications
You must be signed in to change notification settings - Fork 525
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 magic bitboards #640
Movegen speedup via magic bitboards #640
Conversation
…agic-bb Resolving merge conflicts
…aere/lc0 into movegen-speedup-magic-bb
…o save some instructions
On my machine current master (includes #638):
this PR (without PEXT):
This PR (with PEXT, built with
|
This PR is ready for review. |
This reverts commit 7f1374d.
src/chess/bitboard.h
Outdated
public: | ||
// Constructor is called once in bitboard.cc to initialize structures, should | ||
// not be called again! | ||
MagicBitBoards(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does using a singleton instead make it much slower?
E.g.
class MagicBitBoards {
public:
static const MagicBitBoards& Get() {
static MagicBitBoards boards_;
return boards;
}
private:
MagicBitBoards() { ... }
};
(and removing static from everywhere)
It would be neater (I guess), but I can see that it could be slower indeed..
On other hand, unused static variable can be optimized out, so the constructor will never run.
(we had this problem with backend registration btw, which are also unused static variables,
after some massaging linker now doesn't optimize them, but it didn't before so it's fragile)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's not slower, then doing MagicBitBoards::Get()
in main()
so that time is not spent during actual game is fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it is slower, then still calling some InitializeMagicBitboards()
from main() seems to be more robust approach.
In that case, maybe it's better to move everything into anonymous namespace inside board.cc
(not bitboard.cc
, for me magicbitboards are more part of movegen than bitboard itself, it feels similar only because of name), and only expose one function InitializeMagicBitboards()
in .h
file. Also in this case the class wrapper (which only serves purpose of namespace) is not necessary I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually what I wrote in the last paragraph I like more than singleton even..
But I wonder what's your opinion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought the current way of using a constructor was quite neat (I was a bit in Java mindset where static initialization is very easy and common, even without this constructor trick), but this fails miserably if the dummy instance gets optimized away.
I like your last suggestion to move everything into anonymous namespace of board.cc (dropping the class on top) and initialize explicitly. This has the added benefit that we have full control where to do the initialization (it will probably take only few milliseconds, but still...). I'll implement the changes for this last approach in the coming days.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I implemented the requested changes.
One additional benefit of the current approach is the fact that it's probably less sensitive to whether LTO is enabled, as all magic bitboard structures and code are now in the same compilation unit where they are used/called.
This PR adds magic bitboards (introduced in #628) as a layer on top of #638.
Unconditional speedup for random backend up to 10% (tested with LTO builds on Linux Fedora 29 64-bit,
gcc 8.2.1
).The measurements details (command line and considered positions) are given in #638.
For 1 search thread:
6a639b6
For 2 search threads:
6a639b6