The cost of check & discovered check in bitboards

Discussion of chess software programming and technical issues.

Moderators: hgm, Dann Corbit, Harvey Williamson

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
pedrojdm2021
Posts: 90
Joined: Fri Apr 30, 2021 5:19 am
Full name: Pedro Duran

Re: The cost of check & discovered check in bitboards

Post by pedrojdm2021 » Tue Sep 14, 2021 6:52 pm

lithander wrote:
Tue Sep 14, 2021 10:07 am
pedrojdm2021 wrote:
Mon Sep 13, 2021 11:37 pm
Most of the fasest C# chess engine seems to do something like black magic IMO. if you want to keep a clear code without so many tricks and hacks, i'd recommend to translate the engine to C/C++ code and you will gain at least 2x performance boost for free.
I'm curious: Which C# engine *is* considered fastest? And does it really do a good job at leveraging the performance modern C# has to offer?

That it uses black magic, tricks and hacks and ends up 2x slower than clear, readable C++ seems hard to believe to me. Also the fastest C++ engines are not exactly very readable, either... you always sacrifice some maintainability if you want to go for the best performance. In any language.
pedrojdm2021 wrote:
Mon Sep 13, 2021 11:37 pm
In my C# engine i use only pseudo legal move generation with make/undo and i am getting ~700.000 NPS in my CPU.
Chessnut1071 wrote:
Mon Sep 13, 2021 9:32 pm
So, my engine using, nodes, reaches 39,179,952 raw pseudo moves, 33,712,000 pseudo + check, and 24,277,456 pseudo + check + discovered check.
Do I understand that right? You're both using C# but the programmer with a 0.7M NPS pseudo legal move gen explains to the programmer with a 39M nps pseudo legal move gen why C# is a really bad choice and can't be used to make a speedy engine? ...and that OP will end up smashing his head over the keyboard eventually if he doesn't migrate his engine to C++?

That you have been dealing with C# performance for a long time doesn't make you automatically an expert, Pedro. It seems like there's a missed learning opportunity here. And I'm not talking about OP.
Seach on github "Cosette" chess engine, that thing is lighting fast. but he seems to be used unmanaged C# for some important stuff, and he is used . net 5 with the stackalloc stuff.

about the things in C++ : bit-twiding operations, even a method call, math operations are so much faster in C++ than C#

You can write a simple benchmark,
do a simple bitboard set, some bits to it, and then do some bit-twidding operations like AND, OR , XOR
call the bitwise operations 50,000,000 times in a for loop, then mesaure performance difference between the C# console application and the C++ app the difference is quite big,

So a simpler or pretty code will be much faster than the same code in C# it maybe sound strange, but in C# even calling a empty method has a performance impact :?

pedrojdm2021
Posts: 90
Joined: Fri Apr 30, 2021 5:19 am
Full name: Pedro Duran

Re: The cost of check & discovered check in bitboards

Post by pedrojdm2021 » Tue Sep 14, 2021 7:04 pm

R. Tomasi wrote:
Tue Sep 14, 2021 10:19 am
I suspect that the 0.7 MNPS vs the 39 MNPS may be a comparision between apples and oranges, so to speak. I would guess that the 39 MPS are pure perft metrics (that's how I understand OP is measuring, kind of), whilst the 0.7 MNPS might be the "real" NPS during search (where the order of magnitude seems to be in a reasonable range).

I do agree with lithander, though, that the 2x speed-up seems to be suspicous. Yes, C++ is faster. But it's not faster by such a huge amount compared to well coded C#...
That's the thing, the "well coded C#" is more complex and more time consuming than implementing optimizations in a C++ chess engine
as i said before, the C# compiler puts a lot of garbage in your assembly even for doing some bitwise operations that sould be really fast.

You have to almost never trust what the C# compiler does in this use cases.

i'm not saying that C# is a bad language or it is impossible to do, just it is harder to opimize than C++

pedrojdm2021
Posts: 90
Joined: Fri Apr 30, 2021 5:19 am
Full name: Pedro Duran

Re: The cost of check & discovered check in bitboards

Post by pedrojdm2021 » Tue Sep 14, 2021 7:10 pm

R. Tomasi wrote:
Tue Sep 14, 2021 10:19 am
I suspect that the 0.7 MNPS vs the 39 MNPS may be a comparision between apples and oranges, so to speak. I would guess that the 39 MPS are pure perft metrics (that's how I understand OP is measuring, kind of), whilst the 0.7 MNPS might be the "real" NPS during search (where the order of magnitude seems to be in a reasonable range).

I do agree with lithander, though, that the 2x speed-up seems to be suspicous. Yes, C++ is faster. But it's not faster by such a huge amount compared to well coded C#...
Also, my tests are using a single core from my AMD FX 8350 CPU. And eveyone knows that the FX processors are usually worse than even some modern quad core Intel pentium CPU's

i don't even do the AI search on other thread, everything is from the main thread

jswaff
Posts: 67
Joined: Sun Jun 08, 2014 10:22 pm
Contact:

Re: The cost of check & discovered check in bitboards

Post by jswaff » Tue Sep 14, 2021 8:02 pm

Chessnut1071 wrote:
Tue Sep 14, 2021 4:09 am

Also, are you sure about the 2x speed difference between C# and C++?
I have a Java engine and a C engine. The data structures are different, obviously, but the search algorithms and evaluation are *exactly* the same. Java is not C#, but without looking it up I'd expect similar performance. In my case, the C engine is indeed 2-3 times faster than the Java engine.

EDIT: "and C is not C++"

That said, unless you're trying to create a world beater, speed isn't everything. IMO.
--
James

User avatar
emadsen
Posts: 319
Joined: Wed Apr 25, 2012 11:51 pm
Location: Oak Park, IL, USA
Full name: Erik Madsen
Contact:

Re: The cost of check & discovered check in bitboards

Post by emadsen » Tue Sep 14, 2021 8:58 pm

lithander wrote:
Tue Sep 14, 2021 10:07 am
Do I understand that right? You're both using C# but the programmer with a 0.7M NPS pseudo legal move gen explains to the programmer with a 39M nps pseudo legal move gen why C# is a really bad choice and can't be used to make a speedy engine?
LOL... Of course C# is fast enough. Besides, a 2x speedup equates to what, 50 - 70 ELO?
My C# chess engine: https://www.madchess.net

amanjpro
Posts: 563
Joined: Sat Mar 13, 2021 12:47 am
Full name: Amanj Sherwany
Contact:

Re: The cost of check & discovered check in bitboards

Post by amanjpro » Tue Sep 14, 2021 9:30 pm

emadsen wrote:
Tue Sep 14, 2021 8:58 pm
lithander wrote:
Tue Sep 14, 2021 10:07 am
Do I understand that right? You're both using C# but the programmer with a 0.7M NPS pseudo legal move gen explains to the programmer with a 39M nps pseudo legal move gen why C# is a really bad choice and can't be used to make a speedy engine?
LOL... Of course C# is fast enough. Besides, a 2x speedup equates to what, 50 - 70 ELO?
Apparently we non-C/C++ engine authors are all geniouses, that we use a handicapped language and still compete with those fast engines :D

klx
Posts: 131
Joined: Tue Jun 15, 2021 6:11 pm
Full name: Emanuel Torres

Re: The cost of check & discovered check in bitboards

Post by klx » Wed Sep 15, 2021 12:27 am

emadsen wrote:
Tue Sep 14, 2021 8:58 pm
LOL... Of course C# is fast enough. Besides, a 2x speedup equates to what, 50 - 70 ELO?
It's fast enough for engines of modest strength, since there are much easier ways to gain Elo than switching to a new language. But once you get to the top, that performance will matter. ~60 Elo would make the difference between the world's #1 strongest engine and #4. Or #4 to #13.

User avatar
lithander
Posts: 365
Joined: Sun Dec 27, 2020 1:40 am
Location: Bremen, Germany
Full name: Thomas Jahn

Re: The cost of check & discovered check in bitboards

Post by lithander » Wed Sep 15, 2021 12:36 am

pedrojdm2021 wrote:
Tue Sep 14, 2021 6:52 pm
Seach on github "Cosette" chess engine, that thing is lighting fast. but he seems to be used unmanaged C# for some important stuff, and he is used . net 5 with the stackalloc stuff.
On my computer Cosette's perft command searches some 15 Million nodes per second. But is that a lot? Could it be much more? Without looking into the details perft performance tells you nothing.

In my engine perft uses a hash table so if someone just runs perft without looking at the code he might think the engine is quiet fast. If I comment that out I'm down to 4.9M nps. Oh, that's a damn slow engine! But it checks for legal moves by playing the move and testing the resulting position for check. If it's legal it plays the move again. Let's fix that. Oh nice... 6M nps. How about we don't create a new Board instance for every node we visit? Let's just create a bunch (one per ply) and reuse them. Oh, nice 7.5M nps. But wait... we can make perft faster by removing features: let's not update the eval incrementally anymore. And don't update the Zobrist key! (perft doesn't need those features.) Wow, 10M nps!! I just made my engine look twice as fast in 10 minutes of work.

It's pointless to compare the perft stat of a random C# engine with a random C++ one and assume that the difference is due to the speed of the programming language.

If we wanted to make a fair comparison one should make an effort to implement exactly the same thing in C# and in C++ and compare that. And of course you will want to use "stackalloc stuff" or otherwise the C++ version shouldn't be allowed to create arrays on the stack either. Of course you will want to use hardware intrinsics in C# if the C++ version does use them. These things matter in a fair comparision!
jswaff wrote:
Tue Sep 14, 2021 8:02 pm
I have a Java engine and a C engine. The data structures are different, obviously, but the search algorithms and evaluation are *exactly* the same. Java is not C#, but without looking it up I'd expect similar performance. In my case, the C engine is indeed 2-3 times faster than the Java engine.
This is what I mean. The only way to compare apples with apples. But nothing like that exists for C# vs C++ as far as I know. Benchmark Games is a great resource but I've looked at a few of the sources and they definitely don't compare apples with apples there, either! ;)

(I'm not saying that C# is as fast as C++, I just say that we lack the basis to come to any credible conclusion)
Minimal Chess. A simple engine written in C#! Details on Youtube & Github

R. Tomasi
Posts: 138
Joined: Wed Sep 01, 2021 2:08 pm
Location: Germany
Full name: Roland Tomasi

Re: The cost of check & discovered check in bitboards

Post by R. Tomasi » Wed Sep 15, 2021 1:50 am

Am I the only one who finds these never-ending discussions about which language is better to be quite tiresome? Like every 2nd thread seems to derail into one of these...

Chessnut1071
Posts: 59
Joined: Tue Aug 03, 2021 12:41 pm
Full name: Bill Beame

Re: The cost of check & discovered check in bitboards

Post by Chessnut1071 » Wed Sep 15, 2021 2:30 am

R. Tomasi wrote:
Wed Sep 15, 2021 1:50 am
Am I the only one who finds these never-ending discussions about which language is better to be quite tiresome? Like every 2nd thread seems to derail into one of these...

Post Reply