Stockfish port to C#

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

RoadWarrior
Posts: 73
Joined: Fri Jan 13, 2012 12:39 am
Location: London, England

Re: Stockfish port to C#

Post by RoadWarrior »

whittenizer wrote:Also, removing alot of function calls, and simply putting the code inline, helped alot too as C# doesnt have inline functions.
The C# JIT compiler will inline methods with specific characteristics. Eric Gunnerson has an interesting (but ancient) take on why C# has no "force inline" attribute. His basic argument is that in the majority of cases, the JIT compiler does a much better job than the developer on making inline decisions. But if you're using version 4.5 (or later) of the .NET Framework, you now have the AggressiveInlining attribute flag (compiler hint).

What hot spots does the performance profiling show?
Marco wrote: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.
This is exactly what I've found in my engine Amaia. The JIT compiler creates array elements on the heap even if they're value types. I believe this is related to a rule about object lifetimes. This hasn't proved to be a big issue for me as most of my arrays survive for the complete program lifetime anyway. They're allocated and populated once at program start-up and de-allocated at program close-down.
Richard wrote:From my experiance with Jabba, trying to mimic the C++ code is the wrong way to go about it.
I think Richard is absolutely correct. When converting to a language with a very different paradigm, a straight conversion is unlikely to give you equal performance. It's better to be idiomatic in your use of C#, and also you need to be aggressive in using performance and memory profilers to remove significant speed bumps.
There are two types of people in the world: Avoid them both.
whittenizer
Posts: 85
Joined: Sun May 29, 2011 11:56 pm
Location: San Diego

Re: Stockfish port to C#

Post by whittenizer »

Hey there,

Thanks for the reply. Most of it was due to array initializations. Alot of similar things Richard was facing. The conversion is complete and seems to be working fairly well. Just some performance hurdles to get over.

Yes, the inline methods were replaced by simply putting the code inline. We hade methods to replace the inline but this put the program at a screatching halt.

So, now just a few spots in the search and evaluation routines that need adjusting. This guy Im working with has done a seemingly impossible feat of the conversion to C#. Once things get stable, I'll share osme of the techniques.

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

Re: Stockfish port to C#

Post by whittenizer »

This is for a silverlight project so this had to be 100% managed code.

THanks for the reply though. Everything on here helps :-)
whittenizer
Posts: 85
Joined: Sun May 29, 2011 11:56 pm
Location: San Diego

Re: Stockfish port to C#

Post by whittenizer »

As stated before, this is silverlight so it has to be managed, so no unsafe code. His work around for this was just unreal. I'll share some code once things are stable.
Dave_N
Posts: 153
Joined: Fri Sep 30, 2011 7:48 am

Re: Stockfish port to C#

Post by Dave_N »

I realized you would probably have unsafe blocks of code if possible :)
That is all I know about real C# other than the obvious java similarities, I think if you search for porting C++ to Java there might be some optimizations, especially with older mobile phone programming resources - there are definitely some tricks with memory and processor usage for java that would work in C#.