I just finished replacing my magic bitboards with PEXT. I logged a 22.5% increase in speed over magic bitboards, now 7,462,686 nodes per second. PEXT eliminated a lot of code and magic number arrays, and I found it a lot simpler to code. I'm really interested in how H.G. Muller is getting 290 million nodes per second. Is he using a personal computer to achieve that speed? I'm using an old 2.6 GHz, i7. Also, I'm not yet using any parallel features or taking advantage of multiple cores and threads. What's the next step?Chessnut1071 wrote: ↑Tue May 17, 2022 2:01 amChessnut1071 wrote: ↑Sun May 15, 2022 10:43 pmthanks, it's working. I'm thinking about trying C++, but, it was a huge leap for me coming from visual basic. When I first joined, my pseudo move generator crept along at a robust 12,230 moves per second in release mode. Now, it's clocking 6,238,303 pseudo moves/sec in release mode. Hopefully, the PEXT will take me to a new level. I dread to think what comes next.dangi12012 wrote: ↑Sun May 15, 2022 9:13 pmIt definitely is not PDEP - but this function here:Chessnut1071 wrote: ↑Sun May 15, 2022 8:24 pm I'm using C# windows OS target 7.0 and can not seem to get "PDEP" - C# version of PEXT - to work. Anybody using C# 7.0 have it working?
https://docs.microsoft.com/en-us/dotnet ... ew=net-6.0
Namespace: System.Runtime.Intrinsics
Available in: .NET Core 3.0, Core 3.1, 5, 6, 7When you get it running - it also might interestCode: Select all
Bmi2.X64.ParallelBitExtract(UInt64, UInt64)
You know... Thinking of it. I originally started programming with C# and switched to C++ out of necessity (more performance and better pay) but I quite like the language. So I might even translate my "comparison of all known lookups" to C#.
Could be a fun weekend project.
Magic Numbers ?
Moderator: Ras
-
- Posts: 313
- Joined: Tue Aug 03, 2021 2:41 pm
- Full name: Bill Beame
Re: Magic Numbers ?
-
- Posts: 7251
- Joined: Mon May 27, 2013 10:31 am
Re: Magic Numbers ?
I only have system.runtime.intrinsics installed. So there is no PEXT available.
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
-
- Posts: 1062
- Joined: Tue Apr 28, 2020 10:03 pm
- Full name: Daniel Infuehr
Re: Magic Numbers ?
Well you just convinced me to port some of the algos to C#.Chessnut1071 wrote: ↑Tue May 17, 2022 2:13 amI just finished replacing my magic bitboards with PEXT. I logged a 22.5% increase in speed over magic bitboards, now 7,462,686 nodes per second. PEXT eliminated a lot of code and magic number arrays, and I found it a lot simpler to code. I'm really interested in how H.G. Muller is getting 290 million nodes per second. Is he using a personal computer to achieve that speed? I'm using an old 2.6 GHz, i7. Also, I'm not yet using any parallel features or taking advantage of multiple cores and threads. What's the next step?Chessnut1071 wrote: ↑Tue May 17, 2022 2:01 amChessnut1071 wrote: ↑Sun May 15, 2022 10:43 pmthanks, it's working. I'm thinking about trying C++, but, it was a huge leap for me coming from visual basic. When I first joined, my pseudo move generator crept along at a robust 12,230 moves per second in release mode. Now, it's clocking 6,238,303 pseudo moves/sec in release mode. Hopefully, the PEXT will take me to a new level. I dread to think what comes next.dangi12012 wrote: ↑Sun May 15, 2022 9:13 pmIt definitely is not PDEP - but this function here:Chessnut1071 wrote: ↑Sun May 15, 2022 8:24 pm I'm using C# windows OS target 7.0 and can not seem to get "PDEP" - C# version of PEXT - to work. Anybody using C# 7.0 have it working?
https://docs.microsoft.com/en-us/dotnet ... ew=net-6.0
Namespace: System.Runtime.Intrinsics
Available in: .NET Core 3.0, Core 3.1, 5, 6, 7When you get it running - it also might interestCode: Select all
Bmi2.X64.ParallelBitExtract(UInt64, UInt64)
You know... Thinking of it. I originally started programming with C# and switched to C++ out of necessity (more performance and better pay) but I quite like the language. So I might even translate my "comparison of all known lookups" to C#.
Could be a fun weekend project.
Last time I compared C++ to C# in a performance manner was 15 years ago. A lot changed since then and both languages and compilers evolved a lot.
Worlds-fastest-Bitboard-Chess-Movegenerator
Daniel Inführ - Software Developer
Daniel Inführ - Software Developer
-
- Posts: 313
- Joined: Tue Aug 03, 2021 2:41 pm
- Full name: Bill Beame
Re: Magic Numbers ?
Microsoft is really focused on C#. With Net 7.0, you have all the Intrinsics you need that C++ includes. People who use both claim they like C# better; however, they claim it's 10% slower on some applications. That was a few years ago, there may not be much difference now with Net 7.0. Consider the fact that until August, I was developing in Visual Basic, using integer arrays and clocking around 9,000 pseudo calls/second. PEXT got me to 7.5 million/second. I would love to find out how Muller is getting 290 million/sec. I'm not sure I can do that without a supper computer. Does Muller have some public document discussing how he did that, or, is his method private? I'm desperate for speed and no where close to where I need to be yet.dangi12012 wrote: ↑Tue May 17, 2022 6:44 pmWell you just convinced me to port some of the algos to C#.Chessnut1071 wrote: ↑Tue May 17, 2022 2:13 amI just finished replacing my magic bitboards with PEXT. I logged a 22.5% increase in speed over magic bitboards, now 7,462,686 nodes per second. PEXT eliminated a lot of code and magic number arrays, and I found it a lot simpler to code. I'm really interested in how H.G. Muller is getting 290 million nodes per second. Is he using a personal computer to achieve that speed? I'm using an old 2.6 GHz, i7. Also, I'm not yet using any parallel features or taking advantage of multiple cores and threads. What's the next step?Chessnut1071 wrote: ↑Tue May 17, 2022 2:01 amChessnut1071 wrote: ↑Sun May 15, 2022 10:43 pmthanks, it's working. I'm thinking about trying C++, but, it was a huge leap for me coming from visual basic. When I first joined, my pseudo move generator crept along at a robust 12,230 moves per second in release mode. Now, it's clocking 6,238,303 pseudo moves/sec in release mode. Hopefully, the PEXT will take me to a new level. I dread to think what comes next.dangi12012 wrote: ↑Sun May 15, 2022 9:13 pmIt definitely is not PDEP - but this function here:Chessnut1071 wrote: ↑Sun May 15, 2022 8:24 pm I'm using C# windows OS target 7.0 and can not seem to get "PDEP" - C# version of PEXT - to work. Anybody using C# 7.0 have it working?
https://docs.microsoft.com/en-us/dotnet ... ew=net-6.0
Namespace: System.Runtime.Intrinsics
Available in: .NET Core 3.0, Core 3.1, 5, 6, 7When you get it running - it also might interestCode: Select all
Bmi2.X64.ParallelBitExtract(UInt64, UInt64)
You know... Thinking of it. I originally started programming with C# and switched to C++ out of necessity (more performance and better pay) but I quite like the language. So I might even translate my "comparison of all known lookups" to C#.
Could be a fun weekend project.
Last time I compared C++ to C# in a performance manner was 15 years ago. A lot changed since then and both languages and compilers evolved a lot.
-
- Posts: 325
- Joined: Tue Aug 31, 2021 10:32 pm
- Full name: Mateo
Re: Magic Numbers ?
for a fast perft you absolutely need a legal move generator and an hash table.
here there's the source code for qpeft https://home.hccnet.nl/h.g.muller/perft.c
here there's the source code for qpeft https://home.hccnet.nl/h.g.muller/perft.c
-
- Posts: 915
- Joined: Sun Dec 27, 2020 2:40 am
- Location: Bremen, Germany
- Full name: Thomas Jahn
Re: Magic Numbers ?
Great news for us C# fans! Looking forward to your results!dangi12012 wrote: ↑Tue May 17, 2022 6:44 pm Well you just convinced me to port some of the algos to C#.
Last time I compared C++ to C# in a performance manner was 15 years ago. A lot changed since then and both languages and compilers evolved a lot.
-
- Posts: 1062
- Joined: Tue Apr 28, 2020 10:03 pm
- Full name: Daniel Infuehr
Re: Magic Numbers ?
You know - with C# you can dynamically compile code at runtime - and pretty easily I might add that.lithander wrote: ↑Tue May 17, 2022 10:22 pmGreat news for us C# fans! Looking forward to your results!dangi12012 wrote: ↑Tue May 17, 2022 6:44 pm Well you just convinced me to port some of the algos to C#.
Last time I compared C++ to C# in a performance manner was 15 years ago. A lot changed since then and both languages and compilers evolved a lot.
With C++ its an insane headache to ship llvm with your app for dynamic compilation. C# has this baked in.
https://stackoverflow.com/questions/826 ... -fragments
You position does not contain knights? -> throw the code away and call the new version.
All pawns are stuck or none left -> remove all pawn code and call the new version.
You would exactly know how many pieces there are not in terms of a variable but compiled into a dynamically created binary.
Or you create the permutation of all pieces ahead of time.
Oh If I only had more time...
Code: Select all
Engine_2Knights_2Bishops_1Queen_16Pawns.exe
Engine_1Knight_2Bishops_2Queens_11Pawns.exe
.....
Back to topic:
One last thing I would like for you to try - Chessnut1071 and maybe lithander
If you compile the exe - put it into the C# project and DllImport the C++ version into C# how much difference do you get in terms of nps/ELO?
I have started translation and added __declspec(dllexport) directives into the repository. That means you can call the (insanely fast) C++ functions from C#



Description
http://www.talkchess.com/forum3/viewtop ... 00#p925970
Prototype in the .sln ready to check out (there you can see what I mean exactly)

https://github.com/Gigantua/Chess_Movegen
https://github.com/Gigantua/Chess_Moveg ... h.cs#L1864
Worlds-fastest-Bitboard-Chess-Movegenerator
Daniel Inführ - Software Developer
Daniel Inführ - Software Developer
-
- Posts: 5693
- Joined: Tue Feb 28, 2012 11:56 pm
Re: Magic Numbers ?
It is certainly valid today. And it will remain valid in the ARM future.dangi12012 wrote: ↑Sun May 15, 2022 7:56 pm The point I am making:
Having the default assumption that magic is *always* one of the fastest and picking it by default is not valid.
SandyBridge is from 2011 or so and does not support pext at all.Recommendation:
Modern x64 (Zen3+, SandyBride+) -> PEXT
On Intel, pext is available and sufficiently efficient starting from Haswell. Whether this covers all "modern" Intel CPUs is up for debate. If you want many people to use your engine but don't want complaints, you should probably support pre-Haswell. I'd say it should by now be OK to ignore 32 bit.
On AMD, there might be some pre-Zen CPUs with a good enough pext implementation (Wikipedia says Excavator supported BMI2, but I don't know how good it is).
Certainly pext is totally useless on Zen and Zen2, which in my view are still modern CPUs. Threadrippers are basically all Zen and Zen2.
ARM simply does not have pext at all and likely will never have it.
Magic works very well. Instead of variable shift it is likely better to use fixed shift.
-
- Posts: 1062
- Joined: Tue Apr 28, 2020 10:03 pm
- Full name: Daniel Infuehr
Re: Magic Numbers ?
The thing about magic is that it uses 64 bit multiplication which does not exist for AVX2. Meaning CLANG cannot fully vectorize a call to magic.
Other algos dont have this issue meaning that in actual engine code the compiler might emit code that looks up 4 slidersquares at once.
So it is not a cut and dry or an obvious answer.
Thats why a uniform interface without dependencies was important to me. Now everyone can pick and choose what is fastest.
Also when you have AVX512 you get this beauty to work with:
https://builders.intel.com/docs/network ... -guide.pdf
Back to topic:
For the C# programmers. This will be interesting:
Code: Select all
[DllImport("Runtime\\Movegen_Compare.exe")]
public static extern ulong Pext_t_Queen(int sq, ulong occupy);
[DllImport("Runtime\\Movegen_Compare.exe")]
public static extern ulong Fancy_t_Queen(int sq, ulong occupy);
[DllImport("Runtime\\Movegen_Compare.exe")]
public static extern ulong Leorik_t_Queen(int sq, ulong occupy);
Worlds-fastest-Bitboard-Chess-Movegenerator
Daniel Inführ - Software Developer
Daniel Inführ - Software Developer