Stockfish port to C#

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

whittenizer
Posts: 85
Joined: Sun May 29, 2011 11:56 pm
Location: San Diego

Stockfish port to C#

Post by whittenizer »

Hi all,

Just wanted to let you all know the entire code base for Stockfish has been ported over to C#. It's bringing back moves but the performance is insanely slow. There were alot of areas where no direct conversions were possible so work arounds were implemented such as for the pointer arithmetic and template stuff. Keep in mind this is for Silverlight so this had to be 100% managed code.

These work arounds may have caused the performance to drop so we're looking at ways to improve it. The search method literally takes minutes to go to only depth 9.

Any ideas on work arounds for template and pointer arithmetic would be helpful.

Thanks so much
smatovic
Posts: 2639
Joined: Wed Mar 10, 2010 10:18 pm
Location: Hamburg, Germany
Full name: Srdja Matovic

Re: Stockfish port to C#

Post by smatovic »

Any ideas on work arounds for template and pointer arithmetic would be helpful.
...hope you dont copy hole boards to do/undo a move...

--
Srdja
whittenizer
Posts: 85
Joined: Sun May 29, 2011 11:56 pm
Location: San Diego

Re: Stockfish port to C#

Post by whittenizer »

Not sure what you mean.
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: Stockfish port to C#

Post by mcostalba »

whittenizer wrote: These work arounds may have caused the performance to drop so we're looking at ways to improve it. The search method literally takes minutes to go to only depth 9.
Just for your information, there is another ongoing effort to port SF to C# and is almost finished (it has same functionality with SF 2.2.2), in my opinion the author made an exceptional work because the speed is just about the half of the original. He was very kind to keep me informed by e-mail of his progress and of the troubles he found. One of the critical areas is memory management because SF relies heavily on stack allocations, while in .Net fundamental stuff like the arrays have to be allocated on the heap: this kills performance. So he needed to implement some clever stuff to workaround the limitations of the platform.

I don't know the details of his porting (I even don't have the code), I wrote this post just to make you know that it is possible to get something comparable to the original in C++, but it is really a tricky and very difficult job.
Richard Allbert
Posts: 792
Joined: Wed Jul 19, 2006 9:58 am

Re: Stockfish port to C#

Post by Richard Allbert »

Hi David

There is a C# performance thread on here. I started rewriting Jabba in C#, and it was massively slow. After some good advice on the thread, and some work with a profiler, I ended up at 70% of the C++ program speed, with full searching, hashing, move ordering.

On my i3, Jabba C++ searches with 1400knps and the C# version 1000knps.

The main issue was keeping the heap declarations small (which the profiler showed), and unrolling 2D arrays into 1D arrays. plus some other tricks as well.

Richard
whittenizer
Posts: 85
Joined: Sun May 29, 2011 11:56 pm
Location: San Diego

Re: Stockfish port to C#

Post by whittenizer »

Hi,

I did see the thread on here actually. Seems like you've worked on your project really hard. Actually the person I'm working with did some pretty cool things as well. His implementation of pointers and templates in a non pointer/template language, well at least in the managed C# world, was something else. I was inquiring here if there were any other ways to work around those limitations in managed C#. Also, removing alot of function calls, and simply putting the code inline, helped alot too as C# doesnt have inline functions.

I'll be sharing the code at some point but not sure how I'm going to go about it yet. The search/evalution algorithms are still slow but they're getting looked at.

I was also looking at Houdini, and the fact the Houdini can search to a greater depth in less the time so may just totally rewrite the search/evalution. But first, need to get it running fast then will work on modifying the search.

Anyways, keep up the hard work.

David
whittenizer
Posts: 85
Joined: Sun May 29, 2011 11:56 pm
Location: San Diego

Re: Stockfish port to C#

Post by whittenizer »

Thanks for the info. Yes it is difficult. The person I'm working with is very bright. He's done an amazing job thus far.

I'll keep you all posted on the progress.
Richard Allbert
Posts: 792
Joined: Wed Jul 19, 2006 9:58 am

Re: Stockfish port to C#

Post by Richard Allbert »

Well, I've had a peep at the SF code just now, and wow, that is some task to convert....

From my experiance with Jabba, trying to mimic the C++ code is the wrong way to go about it. It just ends up slower - if you start using the fixed keyword a lot, for example, you'll soon see on a profiler that this ends up taking more time than if you just used a static array of managed objects.

Another thing I tried was importing some unmanaged windows .dll files, for example to use memset. Again, this turned out slower than just looping throught the array in the normal manner.

As I said above, the real hit came from Heap allocations / deletions. Here you have to be very careful!

Good luck (with a massive undertaking!)

Richard
Dave_N
Posts: 153
Joined: Fri Sep 30, 2011 7:48 am

Re: Stockfish port to C#

Post by Dave_N »

I didn't use much C# however I do remember that there was an "unsafe" mode that allowed programmers to use pointers directly in c#

http://msdn.microsoft.com/en-us/library ... s.80).aspx

and blocks could be made with [unsafe] [/unsafe] (might have the syntax wrong).
Richard Allbert
Posts: 792
Joined: Wed Jul 19, 2006 9:58 am

Re: Stockfish port to C#

Post by Richard Allbert »

Yes, you can, that was what I meant with the fixed keyword - you declare an unsafe block of code and within this block you can use pointers, but only within this block.

So it isn't really practical to have a global pointer to the TTable, for example.

And when using the pointer on an array, you have to "fix" the array in memory using the fixed keyword, which slows the program down.