Advantages of C++11 for Chess?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Fulvio
Posts: 395
Joined: Fri Aug 12, 2016 8:43 pm

Re: Advantages of C++11 for Chess?

Post by Fulvio »

AlvaroBegue wrote: 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).
https://en.wikipedia.org/wiki/Standard_Template_Library

Algorithms in particular is what the talk is about.
AlvaroBegue
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?

Post by AlvaroBegue »

phhnguyen wrote: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?
C++14 and C++17 didn't add anything that I have found useful so far. Except for maybe binary literals in C++14 (so instead of `0xaa' you can say `0b10101010').
Fulvio
Posts: 395
Joined: Fri Aug 12, 2016 8:43 pm

Re: Advantages of C++11 for Chess?

Post by Fulvio »

phhnguyen wrote: since their performances are so bad.
This is very strange, can you make an example?
jdart
Posts: 4366
Joined: Fri Mar 10, 2006 5:23 am
Location: http://www.arasanchess.org

Re: Advantages of C++11 for Chess?

Post by jdart »

I am concerned about portability so I have so far avoided C++14 and later.

I think most of the really useful features came in with C++ 11 and I can't think of much that I really need from later standards, although C++17 does fix some annoying features of "constexpr" from earlier versions.

--Jon
AlvaroBegue
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?

Post by AlvaroBegue »

Fulvio wrote:
AlvaroBegue wrote: 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).
https://en.wikipedia.org/wiki/Standard_Template_Library

Algorithms in particular is what the talk is about.
Oh, algorithms! Of course std::sort is useful (although qsort was nearly as good in C). Besides that, I think I use std::rotate to bring a move to the front of the list when I find an improvement to alpha at the root (which could be done with a one-line loop too). That's about it.

Which of the standard algorithms do you find useful in a chess engine? I am genuinely interested, not being sarcastic or anything: Tone is hard to get right when writing in a foreign language.
Fulvio
Posts: 395
Joined: Fri Aug 12, 2016 8:43 pm

Re: Advantages of C++11 for Chess?

Post by Fulvio »

AlvaroBegue wrote: Which of the standard algorithms do you find useful in a chess engine? I am genuinely interested, not being sarcastic or anything: Tone is hard to get right when writing in a foreign language.
All the sorting ones, like std::nth_element:
let's say you have 4 threads and want to let them analyze the best 4 moves, without sorting all the movelist

All the std::copy, std::fill, std::remove, etc... because if you tell the compiler exactly what you want it will generate faster code

All the find function, std::find, std::lower_bound, adjacent_find, etc.., for example to find a position

The general idea is to know and use all the available algorithm. In the talk he invented the "no raw loops"; it is pretty extreme and mind blowing: every time you start writing a C-style for or while loop you should stop and take your time to think about it, because you are probably doing something wrong.
elcabesa
Posts: 855
Joined: Sun May 23, 2010 1:32 pm

Re: Advantages of C++11 for Chess?

Post by elcabesa »

i also like for loops, iterator, and in some case "auto"
jdart
Posts: 4366
Joined: Fri Mar 10, 2006 5:23 am
Location: http://www.arasanchess.org

Re: Advantages of C++11 for Chess?

Post by jdart »

std::sort is not that useful, given its performance and the fact that it is probably quicksort underneath, which is optimized for larger sort jobs than you usually have in chess. But there are cases where performance is not critical and it does the job.

However, I also like the STL containers. std::array is a nice efficient wrapper over C arrays with bounds checking. std::unordered_map (C++11) is a useful key/value store.

--Jon
syzygy
Posts: 5557
Joined: Tue Feb 28, 2012 11:56 pm

Re: Advantages of C++11 for Chess?

Post by syzygy »

jdart wrote:std::sort is not that useful, given its performance and the fact that it is probably quicksort underneath, which is optimized for larger sort jobs than you usually have in chess.
Stockfish likes to use it though... in time-critical code for sorting lists having on average about 1 element ;-)
Rein Halbersma
Posts: 741
Joined: Tue May 22, 2007 11:13 am

Re: Advantages of C++11 for Chess?

Post by Rein Halbersma »

syzygy wrote:
jdart wrote:std::sort is not that useful, given its performance and the fact that it is probably quicksort underneath, which is optimized for larger sort jobs than you usually have in chess.
Stockfish likes to use it though... in time-critical code for sorting lists having on average about 1 element ;-)
And Stockfish is totally fine calling that. Looking at the libstdc++ implementation of std::sort, it uses intro_sort which will check the input size. For 16 elements or less, it will call an optimized version of insertion_sort, and above it, it will do the recursive quick_sort, again terminating each recursion branch with an insertion_sort when the sub-range is less than 16.