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

Optimize Magics #5623

Closed
wants to merge 1 commit into from
Closed

Conversation

mstembera
Copy link
Contributor

Reduce the size of the Magics table by half on modern cpu's and lay it out to match our access pattern. Namely we typically access the magics for the same square for both bishop and rook back to back so we want those to be in the same cache line.

https://tests.stockfishchess.org/tests/view/6701c9b386d5ee47d953bcf4
LLR: 2.94 (-2.94,2.94) <0.00,2.00>
Total: 121664 W: 31931 L: 31497 D: 58236
Ptnml(0-2): 395, 13658, 32322, 14032, 425

A similar patch minus the size reduction finished yellow
https://tests.stockfishchess.org/tests/view/6695f03f4ff211be9d4ec16c
LLR: -2.94 (-2.94,2.94) <0.00,2.00>
Total: 310688 W: 80940 L: 80746 D: 149002
Ptnml(0-2): 1119, 35032, 82846, 35230, 1117

No functional change
bench: 1511354

Copy link

github-actions bot commented Oct 6, 2024

clang-format 18 needs to be run on this PR.
If you do not have clang-format installed, the maintainer will run it when merging.
For the exact version please see https://packages.ubuntu.com/noble/clang-format-18.

(execution 11205050881 / attempt 1)

@Disservin
Copy link
Member

a while ago @sqrmax tried doing something like this, not sure if this was ever submitted to fishtest ?
master...sqrmax:Stockfish:magics-experiment

@cj5716
Copy link
Contributor

cj5716 commented Oct 7, 2024

BTW - I do not think this reduces the size of the magics table at all given it just goes from 2 1D tables to 1 2D table.

as for the sqrmax test - I recall seeing it on fishtest at some point but can't confirm

@AndyGrant
Copy link

I believe this is what he is referring to when he says the tables are cut in half. Which is true. The underlying magic lookups are still the same size, however. Just this meta-wrapper per-square.

image

@cj5716
Copy link
Contributor

cj5716 commented Oct 7, 2024

I see, thanks

@mstembera
Copy link
Contributor Author

Yes sizeof(Magic) is now 16 bytes instead of 32 bytes on machines w/ pext. Also importantly the table is now
Magics[SQUARE_NB][2]; and not just Magics[2][SQUARE_NB]; which puts corresponding bishop and rook squares next to each other in memory. I missed the patch by @sqrmax but have no problem giving him credit w/ this PR.

@Disservin Disservin added the to be merged Will be merged shortly label Oct 12, 2024
@Disservin Disservin closed this in 76923bb Oct 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants