Magic Numbers ?

Discussion of chess software programming and technical issues.

Moderator: Ras

Chessnut1071
Posts: 313
Joined: Tue Aug 03, 2021 2:41 pm
Full name: Bill Beame

Re: Magic Numbers ?

Post by Chessnut1071 »

Chessnut1071 wrote: Tue May 17, 2022 2:01 am
Chessnut1071 wrote: Sun May 15, 2022 10:43 pm
dangi12012 wrote: Sun May 15, 2022 9:13 pm
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?
It definitely is not PDEP - but this function here:

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, 7

Code: Select all

Bmi2.X64.ParallelBitExtract(UInt64, UInt64)
When you get it running - it also might interest
lithander wrote: Mon May 09, 2022 4:20 pm
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.
thanks, 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.
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?
Henk
Posts: 7251
Joined: Mon May 27, 2013 10:31 am

Re: Magic Numbers ?

Post by Henk »

I only have system.runtime.intrinsics installed. So there is no PEXT available.
<TargetFramework>net6.0</TargetFramework>
dangi12012
Posts: 1062
Joined: Tue Apr 28, 2020 10:03 pm
Full name: Daniel Infuehr

Re: Magic Numbers ?

Post by dangi12012 »

Chessnut1071 wrote: Tue May 17, 2022 2:13 am
Chessnut1071 wrote: Tue May 17, 2022 2:01 am
Chessnut1071 wrote: Sun May 15, 2022 10:43 pm
dangi12012 wrote: Sun May 15, 2022 9:13 pm
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?
It definitely is not PDEP - but this function here:

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, 7

Code: Select all

Bmi2.X64.ParallelBitExtract(UInt64, UInt64)
When you get it running - it also might interest
lithander wrote: Mon May 09, 2022 4:20 pm
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.
thanks, 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.
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?
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.
Worlds-fastest-Bitboard-Chess-Movegenerator
Daniel Inführ - Software Developer
Chessnut1071
Posts: 313
Joined: Tue Aug 03, 2021 2:41 pm
Full name: Bill Beame

Re: Magic Numbers ?

Post by Chessnut1071 »

dangi12012 wrote: Tue May 17, 2022 6:44 pm
Chessnut1071 wrote: Tue May 17, 2022 2:13 am
Chessnut1071 wrote: Tue May 17, 2022 2:01 am
Chessnut1071 wrote: Sun May 15, 2022 10:43 pm
dangi12012 wrote: Sun May 15, 2022 9:13 pm
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?
It definitely is not PDEP - but this function here:

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, 7

Code: Select all

Bmi2.X64.ParallelBitExtract(UInt64, UInt64)
When you get it running - it also might interest
lithander wrote: Mon May 09, 2022 4:20 pm
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.
thanks, 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.
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?
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.
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.
tcusr
Posts: 325
Joined: Tue Aug 31, 2021 10:32 pm
Full name: Mateo

Re: Magic Numbers ?

Post by tcusr »

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
User avatar
lithander
Posts: 915
Joined: Sun Dec 27, 2020 2:40 am
Location: Bremen, Germany
Full name: Thomas Jahn

Re: Magic Numbers ?

Post by lithander »

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.
Great news for us C# fans! Looking forward to your results!
Minimal Chess (simple, open source, C#) - Youtube & Github
Leorik (competitive, in active development, C#) - Github & Lichess
dangi12012
Posts: 1062
Joined: Tue Apr 28, 2020 10:03 pm
Full name: Daniel Infuehr

Re: Magic Numbers ?

Post by dangi12012 »

lithander wrote: Tue May 17, 2022 10:22 pm
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.
Great news for us C# fans! Looking forward to your results!
You know - with C# you can dynamically compile code at runtime - and pretty easily I might add that.
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
.....
Disclaimer: Could be a horrible idea - but worth trying.

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#
:D :mrgreen: :D

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) :twisted:
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
syzygy
Posts: 5693
Joined: Tue Feb 28, 2012 11:56 pm

Re: Magic Numbers ?

Post by syzygy »

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.
It is certainly valid today. And it will remain valid in the ARM future.
Recommendation:
Modern x64 (Zen3+, SandyBride+) -> PEXT
SandyBridge is from 2011 or so and does not support pext at all.
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.
dangi12012
Posts: 1062
Joined: Tue Apr 28, 2020 10:03 pm
Full name: Daniel Infuehr

Re: Magic Numbers ?

Post by dangi12012 »

syzygy wrote: Wed May 18, 2022 1:43 am Magic works very well. Instead of variable shift it is likely better to use fixed shift.
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