Hi, I'm trying to rewrite my engine from VB to C++ (using visual studio express) to get a speed gain, but I've run into a problem. When I compile it in dubug mode, everything works fine, but when I compile it to release mode it often fails. For instance in the starting position a 12 depth search works fine in the debug mode, but in the release mode nothing happens after depth 8.
So I wonder what can be the cause of the differences between a debug compile and a release compile. And I'd be greatful for any suggestions on ways I should debug my program to find the error.
Regards
Jacob
Newbie C++ compiling question
Moderators: hgm, Rebel, chrisw
-
- Posts: 1822
- Joined: Thu Mar 09, 2006 11:54 pm
- Location: The Netherlands
Re: Newbie C++ compiling question
I assume you give it a 'rebuild all' each time.jacobbl wrote:Hi, I'm trying to rewrite my engine from VB to C++ (using visual studio express) to get a speed gain, but I've run into a problem. When I compile it in dubug mode, everything works fine, but when I compile it to release mode it often fails. For instance in the starting position a 12 depth search works fine in the debug mode, but in the release mode nothing happens after depth 8.
So I wonder what can be the cause of the differences between a debug compile and a release compile. And I'd be greatful for any suggestions on ways I should debug my program to find the error.
Regards
Jacob
It's a very common problem you describe. C, C++ or Fortran doesn't really matter there. Realize a chess engine is basically a black box. So it's difficult to debug. In short it's programming errors usually that cause this.
Also it is a good habit when programming C++ to not use too many 'tricks' that Bjarne Stroustrup invented - as he didn't yet invent a technique to program bugfree.
The real good programmers are so good usually because they aren't using anything they don't need. KISS. Keep it simple and stupid.
Program in a manner that is more similar to imperative programming i'd recommend, that already gives enough problems and makes code faster. Avoid difficult to debug and tricky things like templates initially.
After all, if you take a C++ program and look at it from a distance it all looks like C code to me.
-
- Posts: 2684
- Joined: Sat Jun 14, 2008 9:17 pm
Re: Newbie C++ compiling question
Try to compile in release mode but with optimizations removed (option /Od) and see what happens....jacobbl wrote: So I wonder what can be the cause of the differences between a debug compile and a release compile. And I'd be greatful for any suggestions on ways I should debug my program to find the error.
-
- Posts: 73
- Joined: Fri Jan 13, 2012 12:39 am
- Location: London, England
Re: Newbie C++ compiling question
In VB and C#, there is a compile-time compilation to IL bytecode, followed by a run-time (JIT) compilation to machine code. The latter is where nearly all of the optimisation happens for a release build. The JIT compiler is significantly more constrained than the C++ compiler, as it has to execute very fast. So the JIT compiler does significantly fewer optimisations when compared to the C++ compiler.jacobbl wrote:Hi, I'm trying to rewrite my engine from VB to C++ (using visual studio express) to get a speed gain, but I've run into a problem. When I compile it in dubug mode, everything works fine, but when I compile it to release mode it often fails. For instance in the starting position a 12 depth search works fine in the debug mode, but in the release mode nothing happens after depth 8.
So I wonder what can be the cause of the differences between a debug compile and a release compile. And I'd be greatful for any suggestions on ways I should debug my program to find the error.
Regards
Jacob
I would start by compiling your release build with all optimisations removed. Assuming that works, tweak each of the C++ optimisation parameters one by one, experimenting to see the effect on your executable. At some point you will probably find the parameter that's causing your problem. It's likely that the compiler optimisation is making an assumption about your code that isn't true.
There are two types of people in the world: Avoid them both.
-
- Posts: 138
- Joined: Tue Aug 23, 2011 10:25 pm
- Location: Germany
Re: Newbie C++ compiling question
If things work in Debug but not in Release or vice-versa, a common cause is uninitialized variables.
nanos gigantium humeris insidentes
-
- Posts: 80
- Joined: Wed Feb 17, 2010 3:57 pm
Re: Newbie C++ compiling question
Thanks to everyone for helpful answers. When I turn off Stack Frames (/RTCs) it seems to work fine. Only problem is that node count falls from 1.7M nps to 0.76M nps
Does anyone have an idea of what sort of error I made that is dependent on the Stack Frames?
Regards
Jacob
Does anyone have an idea of what sort of error I made that is dependent on the Stack Frames?
Regards
Jacob
-
- Posts: 1564
- Joined: Thu Jul 16, 2009 10:47 am
- Location: Almere, The Netherlands
Re: Newbie C++ compiling question
Basically /RTCs is initializing local variables to zero and checking local array bounds. Your problem is possibly related to an uninitialized local variable.
-
- Posts: 80
- Joined: Wed Feb 17, 2010 3:57 pm
Re: Newbie C++ compiling question
Thaks for the tip. So if I understand you correctly, my problem is either an uninitilized local variable or an local array outside it bounds. I could go through all local variables and initilize them. Is this a good idea? I also use som local arrays (char lVar[90]), could this pose a problem if not initilized? I don't think I use any values before they are given a value.
Regards Jacob
Regards Jacob
-
- Posts: 2684
- Joined: Sat Jun 14, 2008 9:17 pm
Re: Newbie C++ compiling question
In case of using an uninitilized local variable compiler should warn you about this (gcc for sure and probably also VC Express with warning level set to 3), in case of out of bound access in 999 out of 1000 of cases you end up with a crash.jacobbl wrote:Thaks for the tip. So if I understand you correctly, my problem is either an uninitilized local variable or an local array outside it bounds.
-
- Posts: 1564
- Joined: Thu Jul 16, 2009 10:47 am
- Location: Almere, The Netherlands
Re: Newbie C++ compiling question
You have to make sure that your local variables and arrays are initialized before you actually use their contents. VC normally gives you a warning when you use uninitialized variables.jacobbl wrote:So if I understand you correctly, my problem is either an uninitilized local variable or an local array outside it bounds. I could go through all local variables and initilize them. Is this a good idea? I also use som local arrays (char lVar[90]), could this pose a problem if not initilized? I don't think I use any values before they are given a value.
Another possibility is that you encounter a stack-overflow. I don't know how large your local data structures are. VC defaults to 1 MB stack-size.
It's possible that the stack grows automatically when you specify /RTCs, but I'm not sure about this.