The cost of check & discovered check in bitboards

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

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

Re: The cost of check & discovered check in bitboards

Post by Chessnut1071 »

pedrojdm2021 wrote: Tue Sep 14, 2021 1:37 am In my C# engine i use only pseudo legal move generation with make/undo and i am getting ~700.000 NPS in my CPU.

if you want know an simple approarch to detect checks and attacks to a square, go to "chessprogramming.org" and search for "Square attacked by"
it seems to be faster than a doing a lot of conditions, but not so fast

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.

Also, C/C++ is easier to optimize because you don't have to deal with all the trash that the C# puts into your assembly if you are not careful even doing simple things such as reading an array database

I have been dealing with the C# performance for quiet a bit, and i haven't seen any super fast C# that does not do so many tricks to reach a good performance.

That's just my humble recommendation if you don't want to smash your head over the keyboard :mrgreen:
Thx for the information. I'm recently converted to C# from VB.Net. I just wanted to learn C# and figured I would write a chess engine in C#. Now, I'm addicted so to speak.

I would shuffle to C++, however, I love that editor in C#. Is C++ available with a free editor as good as C#. I really need speed and focused on minimum moves to checkmate, I could care less about ELO right now. I have an optimizing program for checkmate only and found only 3 significant metrics: 1) history by ply; 2) check; 3) material balance. My engine is slow compared to most, however, I think I make up for that with my optimized evaluation function. I doesn't prune, but, the time to solution goes up almost linearly with resect ply level.

Also, are you sure about the 2x speed difference between C# and C++?
Mergi
Posts: 127
Joined: Sat Aug 21, 2021 9:55 pm
Full name: Jen

Re: The cost of check & discovered check in bitboards

Post by Mergi »

pedrojdm2021 wrote: Tue Sep 14, 2021 1:37 am In my C# engine i use only pseudo legal move generation with make/undo and i am getting ~700.000 NPS in my CPU.
I know everyone here seems to love pseudo legal move generators (and i do too, my own engine uses pseudo legal), but especially for a mate finder, i'd imagine that (almost) full legal would be superior. Presumably, there would be a lot of checks and illegal moves involved in positions where you'd want to find a mate.
pedrojdm2021
Posts: 157
Joined: Fri Apr 30, 2021 7:19 am
Full name: Pedro Duran

Re: The cost of check & discovered check in bitboards

Post by pedrojdm2021 »

Chessnut1071 wrote: Tue Sep 14, 2021 6:09 am
pedrojdm2021 wrote: Tue Sep 14, 2021 1:37 am In my C# engine i use only pseudo legal move generation with make/undo and i am getting ~700.000 NPS in my CPU.

if you want know an simple approarch to detect checks and attacks to a square, go to "chessprogramming.org" and search for "Square attacked by"
it seems to be faster than a doing a lot of conditions, but not so fast

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.

Also, C/C++ is easier to optimize because you don't have to deal with all the trash that the C# puts into your assembly if you are not careful even doing simple things such as reading an array database

I have been dealing with the C# performance for quiet a bit, and i haven't seen any super fast C# that does not do so many tricks to reach a good performance.

That's just my humble recommendation if you don't want to smash your head over the keyboard :mrgreen:
Thx for the information. I'm recently converted to C# from VB.Net. I just wanted to learn C# and figured I would write a chess engine in C#. Now, I'm addicted so to speak.

I would shuffle to C++, however, I love that editor in C#. Is C++ available with a free editor as good as C#. I really need speed and focused on minimum moves to checkmate, I could care less about ELO right now. I have an optimizing program for checkmate only and found only 3 significant metrics: 1) history by ply; 2) check; 3) material balance. My engine is slow compared to most, however, I think I make up for that with my optimized evaluation function. I doesn't prune, but, the time to solution goes up almost linearly with resect ply level.

Also, are you sure about the 2x speed difference between C# and C++?
Portfish (C# port of the famous C++ chess engine 'stockfish') is 2x slow than the original C++ engine
i have been comparing the speeds of doing math operations, and method invoke and C++ seems to be much faster at least ~2x faster

Also, C++ has so many nice features that will help you optimize your engine even more, and it is garbage collector free, no intermediate language, it is only one step away from assembly, so there is not much "magic" under the hood. Only the compilation and that's all.

In C# you have the JIT , that translates your C# compiled code (IL code) in runtime to machine code (bytecode) and that process is usually slow!

But if you are just learning C# and want to have some fun with it, go for it, enjoy the ride :D

VB . net was my first programming language, and i used to love it, both languages at the end of the day VB. net and C# are the same thing to the compiler, they both gets compiled into IL code.
pedrojdm2021
Posts: 157
Joined: Fri Apr 30, 2021 7:19 am
Full name: Pedro Duran

Re: The cost of check & discovered check in bitboards

Post by pedrojdm2021 »

Mergi wrote: Tue Sep 14, 2021 7:28 am
pedrojdm2021 wrote: Tue Sep 14, 2021 1:37 am In my C# engine i use only pseudo legal move generation with make/undo and i am getting ~700.000 NPS in my CPU.
I know everyone here seems to love pseudo legal move generators (and i do too, my own engine uses pseudo legal), but especially for a mate finder, i'd imagine that (almost) full legal would be superior. Presumably, there would be a lot of checks and illegal moves involved in positions where you'd want to find a mate.
Yeah the problem with doing a fully legal move generator is the way of doing things,
you have to take care of checks, discovered checks, pinned pieces and so on...

if you do it wrong it will give you worse performance than pseudo legal move generator. So if you plan to do a fully legal move generator you have to be really smart while coding that and ensure that you do that in few instructions that requires less compute force than the pseudo legal move generator.
R. Tomasi
Posts: 307
Joined: Wed Sep 01, 2021 4:08 pm
Location: Germany
Full name: Roland Tomasi

Re: The cost of check & discovered check in bitboards

Post by R. Tomasi »

Chessnut1071 wrote: Tue Sep 14, 2021 6:09 am I would shuffle to C++, however, I love that editor in C#. Is C++ available with a free editor as good as C#.
It depends on which platform your are developing on: if you're developing on a windows machine, then Microsoft Visual Studio definitely is the gold standard for both C# and C++. The community edition is free and does not lack any important functionality (I used to work with the professional and ultimate versions and recently switched to community. I barely notice any difference).

Concerning C# vs C++: If you want to stick with C# because of it's many positive aspects, then I feel you. It's a fantastic language. On the other hand, if you chose C# because of the managed/portability aspects, then C++ could still be an option, since you actually can compile to IL/.net aswell. In that case you will loose the performance gain that usually is associated with C++.
User avatar
lithander
Posts: 880
Joined: Sun Dec 27, 2020 2:40 am
Location: Bremen, Germany
Full name: Thomas Jahn

Re: The cost of check & discovered check in bitboards

Post by lithander »

And some C# programmers that use Visual Studio also like to use Resharper. Here's a list of features.
Minimal Chess (simple, open source, C#) - Youtube & Github
Leorik (competitive, in active development, C#) - Github & Lichess
User avatar
lithander
Posts: 880
Joined: Sun Dec 27, 2020 2:40 am
Location: Bremen, Germany
Full name: Thomas Jahn

Re: The cost of check & discovered check in bitboards

Post by lithander »

pedrojdm2021 wrote: Tue Sep 14, 2021 1:37 am 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: Tue Sep 14, 2021 1:37 am 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 11: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.
Minimal Chess (simple, open source, C#) - Youtube & Github
Leorik (competitive, in active development, C#) - Github & Lichess
R. Tomasi
Posts: 307
Joined: Wed Sep 01, 2021 4:08 pm
Location: Germany
Full name: Roland Tomasi

Re: The cost of check & discovered check in bitboards

Post by R. Tomasi »

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#...
klx
Posts: 179
Joined: Tue Jun 15, 2021 8:11 pm
Full name: Emanuel Torres

Re: The cost of check & discovered check in bitboards

Post by klx »

Chessnut1071 wrote: Tue Sep 14, 2021 6:09 am the time to solution goes up almost linearly with resect ply level.
Huh? This doesn't make sense. This would mean that searching 40 plies deep would take twice as long as 20 plies. And 80 plies four times as long. That's nonsense. The growth you see is most likely exponential.
[Moderation warning] This signature violated the rule against commercial exhortations.
klx
Posts: 179
Joined: Tue Jun 15, 2021 8:11 pm
Full name: Emanuel Torres

Re: The cost of check & discovered check in bitboards

Post by klx »

R. Tomasi wrote: Tue Sep 14, 2021 12:19 pm 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#...
I linked to Benchmark Games before which aims to answer this question. The programs there see a 1-5x speedup in C++ vs C#. I think 2x is reasonable for a chess engine.
[Moderation warning] This signature violated the rule against commercial exhortations.