Maverick is written in C. I'd like to add multi-core search. Since C++11 has cross platform support for threads it may be wise to do a modest rewrite to C++11 (or latest version of C++). Also, I don't really know much C++ at all so it could be fun to learn.
Before I jump in I have a couple of questions for any C++ gurus - I'd appreciate your counsel:
1. In practice does C++11 have good enough multi-platform thread support to write a SMP chess engine without resorting to IFDEFs?
2. Do C++ templates offer any advantages for chess engine developers? (I think Stockfish uses them for move generation). If so what are the advantages - speed? readability? maintainability?
3. Is it advisable to utilize the object orientated feature of C++ and create objects for everything (e.g. boards, pieces, moves, move lists), just the board, or stick to procedural code
Thanks for your advice!
Steve
Advantages of C++11 for Chess?
Moderator: Ras
-
- Posts: 1254
- Joined: Wed Mar 08, 2006 8:28 pm
- Location: Florida, USA
Advantages of C++11 for Chess?
http://www.chessprogramming.net - Maverick Chess Engine
-
- Posts: 4391
- Joined: Fri Mar 10, 2006 5:23 am
- Location: http://www.arasanchess.org
Re: Advantages of C++11 for Chess?
Yes, pretty much. You may have to set the thread stack size for some OS's (Mac) and that is not so portable. But that is about it.Steve Maughan wrote:
1. In practice does C++11 have good enough multi-platform thread support to write a SMP chess engine without resorting to IFDEFs?
It is kind of up to you how much you want to use this feature. The main advantage is that you can write "generic" code that works with any template arguments, but when you instantiate the template, the compiler frequently will generate a customized version for the specific argument you have chosen. So for example if you have a generateMoves<Black> method, it will generate a custom version of that method for Black and so cut out all the code that might only be executed in case the side is White. No guarantees the compiler will do this but it is commonly what happens.2. Do C++ templates offer any advantages for chess engine developers? (I think Stockfish uses them for move generation). If so what are the advantages - speed? readability? maintainability?
It is quite easy to overdo this. Primitive types are very efficient. If you wrap what is basically a primitive type in a class (such as making a Square class that is really a wrapper over an int), you may get more readability and programming convenience, but you can pay a runtime cost (for example, classes with virtual methods need to have a function table generally, which adds to the class size; also classes with constructors can be less efficient than primitive types, because the constructor needs to be called every time the class is instantiated). Also, avoid heavy use of inheritance. if you need it you need it, but many beginning programmers start to build up a big class hierarchy when they don't actually need it.3. Is it advisable to utilize the object orientated feature of C++ and create objects for everything (e.g. boards, pieces, moves, move lists), just the board, or stick to procedural code
This advice is less true for languages like Ruby where everything is an object anyway, but it goes for C++ IMO.
--Jon
-
- Posts: 1514
- Joined: Wed Apr 21, 2010 4:58 am
- Location: Australia
- Full name: Nguyen Hong Pham
Re: Advantages of C++11 for Chess?
I have just worked on that issue. My engine used some large size variables (such as movelist, declared as a local variable in the search function). Whenever the search is called, the program will take some memory from stacks and be overflowed when reached some deeper plies (for non-main threads only since they have limited stacks).jdart wrote: Yes, pretty much. You may have to set the thread stack size for some OS's (Mac) and that is not so portable. But that is about it.
I don't want to change the stack size (since it is a bit tricky), thus instead of using a large local movelist variable, I declare them as global pointers and allocate them when starting.
-
- Posts: 749
- Joined: Tue May 22, 2007 11:13 am
Re: Advantages of C++11 for Chess?
Yes, all major compilers support this. The only platform specific stuff should be in your build config's linker options.Steve Maughan wrote:Maverick is written in C. I'd like to add multi-core search. Since C++11 has cross platform support for threads it may be wise to do a modest rewrite to C++11 (or latest version of C++). Also, I don't really know much C++ at all so it could be fun to learn.
Before I jump in I have a couple of questions for any C++ gurus - I'd appreciate your counsel:
1. In practice does C++11 have good enough multi-platform thread support to write a SMP chess engine without resorting to IFDEFs?
Templates can eliminate source code duplication. Coming from C, you probably don't want to start with them.2. Do C++ templates offer any advantages for chess engine developers? (I think Stockfish uses them for move generation). If so what are the advantages - speed? readability? maintainability?
The easiest way to get started with C++ is probably to first compile your current C program with a C++ compiler. There are some minor incompatibilities between C and C++, but most of your source should continue to compile. Try to get it to compile without errors. Then try to get it to compile without warnings. C++ is generally more strict and type-safe than C, so this might take a bit of effort. Now you have a working C++ program. It is the C++ dialect called "a better C". You can use C++ threads at this point.3. Is it advisable to utilize the object orientated feature of C++ and create objects for everything (e.g. boards, pieces, moves, move lists), just the board, or stick to procedural code
Thanks for your advice!
Steve
Then you can try and take advantage of C++ features. First, const-correctness. Then eliminate macros and type casts and use nullptr. Or better yet, use automatic resource management: change some of your structs into classes with destructors, and start using the Standard Library containers / strings / smart pointers.
Note that you don't have to make classes for pieces where enums will do just fine. That stuff is only useful for GUIs or if you want to track piece movements across the board (some composers like certain themes).
-
- Posts: 2929
- Joined: Sat Jan 22, 2011 12:42 am
- Location: NL
Re: Advantages of C++11 for Chess?
Have you considered C11?
Personally, I use a library called TinyCThread that implements the C11 threads interface on top of whatever native threads you have (Posix or Windows). Seems to work well.
If you want to move to C++, go for it, but if threads are the only reason for doing so, that's probably overkill.
Personally, I use a library called TinyCThread that implements the C11 threads interface on top of whatever native threads you have (Posix or Windows). Seems to work well.
If you want to move to C++, go for it, but if threads are the only reason for doing so, that's probably overkill.
-
- Posts: 395
- Joined: Fri Aug 12, 2016 8:43 pm
Re: Advantages of C++11 for Chess?
The Standard Template Library is the most important advantage over C.Steve Maughan wrote:
2. Do C++ templates offer any advantages for chess engine developers? (I think Stockfish uses them for move generation). If so what are the advantages - speed? readability? maintainability?
Steve
This is a famous talk by Sean Parent (Adobe):
https://channel9.msdn.com/Events/GoingN ... -Seasoning
It's mind blowing how he takes old C-like spaghetti code (from google chrome if I remember correctly) and replace it using the STL.
-
- Posts: 931
- Joined: Tue Mar 09, 2010 3:46 pm
- Location: New York
- Full name: Álvaro Begué (RuyDos)
Re: Advantages of C++11 for Chess?
I believe this is generally true, but less applicable to chess than to most other programming. I use C++11 for my own engine, because that's the language I am most comfortable with and because of std::string: I don't think I could program without std::string these days. But I didn't find many opportunities to use standard-library containers (probably what you are calling "STL", which I don't think is quite the right name).Fulvio wrote:The Standard Template Library is the most important advantage over C.Steve Maughan wrote:
2. Do C++ templates offer any advantages for chess engine developers? (I think Stockfish uses them for move generation). If so what are the advantages - speed? readability? maintainability?
Steve
This is a famous talk by Sean Parent (Adobe):
https://channel9.msdn.com/Events/GoingN ... -Seasoning
It's mind blowing how he takes old C-like spaghetti code (from google chrome if I remember correctly) and replace it using the STL.
-
- Posts: 1514
- Joined: Wed Apr 21, 2010 4:58 am
- Location: Australia
- Full name: Nguyen Hong Pham
Re: Advantages of C++11 for Chess?
I don’t use much std / stl for board representation, search, move generate functions as well as direct involving ones since their performances are so bad. However for the rest they help me much for an easier life, e.g.: lists of threads, search trees, input / output parsers, test, analysis functions...AlvaroBegue wrote: I believe this is generally true, but less applicable to chess than to most other programming. I use C++11 for my own engine, because that's the language I am most comfortable with and because of std::string: I don't think I could program without std::string these days. But I didn't find many opportunities to use standard-library containers (probably what you are calling "STL", which I don't think is quite the right name).
-
- Posts: 855
- Joined: Sun May 23, 2010 1:32 pm
Re: Advantages of C++11 for Chess?
I'm using string, vector,list, sort,thread from Standard lib.phhnguyen wrote:
I don’t use much std / stl for board representation, search, move generate functions as well as direct involving ones since their performances are so bad. However for the rest they help me much for an easier life, e.g.: lists of threads, search trees, input / output parsers, test, analysis functions...
-
- Posts: 1514
- Joined: Wed Apr 21, 2010 4:58 am
- Location: Australia
- Full name: Nguyen Hong Pham
Re: Advantages of C++11 for Chess?
An open “external” question to all gurus: instead of mentioned C++11 in this topic, do you consider to use newer standards of C++? Say C++ 14 (I have been using that) or C++ 17? What are pros and cons (of using lower / higher standards) for chess in your views?