Clang vs GCC [stockfish 4]

Discussion of chess software programming and technical issues.

Moderator: Ras

User avatar
JuLieN
Posts: 2949
Joined: Mon May 05, 2008 12:16 pm
Location: Bordeaux (France)
Full name: Julien Marcel

Clang vs GCC [stockfish 4]

Post by JuLieN »

I compiled the official Stockfish 4's sources with clang, and got an exe significantly faster than the official gcc build for mac included in the archive. Here are the compared results of the bench command and a search to depth 30 in the starting position:

Code: Select all

Official:
===========================
Total time (ms) : 2836
Nodes searched  : 4132374
Nodes/second    : 1457113

info depth 30 seldepth 46 score cp 30 nodes 1699401570 nps 1207059 time 1407886 multipv 1 pv d2d4 g8f6 g1f3 e7e6 c2c4 f8b4 c1d2 b4d2 d1d2 b7b6 e2e3 e8g8 f1e2 f6e4 d2d3 c8b7 e1g1 d7d6 f3d2 e4d2 d3d2 d8g5 g2g3 b8d7 b1c3 d7f6 a1d1
info nodes 1699401570 time 1407886
bestmove d2d4 ponder g8f6

clang:
===========================
Total time (ms) : 2691
Nodes searched  : 4132374
Nodes/second    : 1535627

info depth 30 seldepth 46 score cp 30 nodes 1699401570 nps 1293199 time 1314106 multipv 1 pv d2d4 g8f6 g1f3 e7e6 c2c4 f8b4 c1d2 b4d2 d1d2 b7b6 e2e3 e8g8 f1e2 f6e4 d2d3 c8b7 e1g1 d7d6 f3d2 e4d2 d3d2 d8g5 g2g3 b8d7 b1c3 d7f6 a1d1
info nodes 1699401570 time 1314106
bestmove d2d4 ponder g8f6
the GCC build takes 23mn and 27 seconds, while the clang one takes 21mn and 54 seconds on my corei7 CPU.

I don't say that Clang produces faster code than GCC, but in some occurrences, like this one, it clearly does. So if you have access to both compilers you might want to try them both for your engine.
"The only good bug is a dead bug." (Don Dailey)
[Blog: http://tinyurl.com/predateur ] [Facebook: http://tinyurl.com/fbpredateur ] [MacEngines: http://tinyurl.com/macengines ]
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Clang vs GCC [stockfish 4]

Post by bob »

JuLieN wrote:I compiled the official Stockfish 4's sources with clang, and got an exe significantly faster than the official gcc build for mac included in the archive. Here are the compared results of the bench command and a search to depth 30 in the starting position:

Code: Select all

Official:
===========================
Total time (ms) : 2836
Nodes searched  : 4132374
Nodes/second    : 1457113

info depth 30 seldepth 46 score cp 30 nodes 1699401570 nps 1207059 time 1407886 multipv 1 pv d2d4 g8f6 g1f3 e7e6 c2c4 f8b4 c1d2 b4d2 d1d2 b7b6 e2e3 e8g8 f1e2 f6e4 d2d3 c8b7 e1g1 d7d6 f3d2 e4d2 d3d2 d8g5 g2g3 b8d7 b1c3 d7f6 a1d1
info nodes 1699401570 time 1407886
bestmove d2d4 ponder g8f6

clang:
===========================
Total time (ms) : 2691
Nodes searched  : 4132374
Nodes/second    : 1535627

info depth 30 seldepth 46 score cp 30 nodes 1699401570 nps 1293199 time 1314106 multipv 1 pv d2d4 g8f6 g1f3 e7e6 c2c4 f8b4 c1d2 b4d2 d1d2 b7b6 e2e3 e8g8 f1e2 f6e4 d2d3 c8b7 e1g1 d7d6 f3d2 e4d2 d3d2 d8g5 g2g3 b8d7 b1c3 d7f6 a1d1
info nodes 1699401570 time 1314106
bestmove d2d4 ponder g8f6
the GCC build takes 23mn and 27 seconds, while the clang one takes 21mn and 54 seconds on my corei7 CPU.

I don't say that Clang produces faster code than GCC, but in some occurrences, like this one, it clearly does. So if you have access to both compilers you might want to try them both for your engine.
clang has bugs. The version that came with xcode on my 8-month-old macbook will NOT compile a functional crafty. gcc 4.7.2 works perfectly, however. I don't trust clang, period. Did you PGO and such?

clang is an old gcc version...
User avatar
JuLieN
Posts: 2949
Joined: Mon May 05, 2008 12:16 pm
Location: Bordeaux (France)
Full name: Julien Marcel

Re: Clang vs GCC [stockfish 4]

Post by JuLieN »

bob wrote: clang has bugs. The version that came with xcode on my 8-month-old macbook will NOT compile a functional crafty. gcc 4.7.2 works perfectly, however. I don't trust clang, period. Did you PGO and such?

clang is an old gcc version...
Yes I did. And I also compiled Crafty with clang with no problem at all and it runs very well. Could you please describe the problem you had with your clang Crafty ?
"The only good bug is a dead bug." (Don Dailey)
[Blog: http://tinyurl.com/predateur ] [Facebook: http://tinyurl.com/fbpredateur ] [MacEngines: http://tinyurl.com/macengines ]
User avatar
JuLieN
Posts: 2949
Joined: Mon May 05, 2008 12:16 pm
Location: Bordeaux (France)
Full name: Julien Marcel

Re: Clang vs GCC [stockfish 4]

Post by JuLieN »

@Bob:
Here's my build of Crafty with Clang, if you want to test it.

Note that no compiler is free of bugs. :) With Tim Kosse we ran into a GCC 4.8.x bug when trying to compile Octochess, and had to go back to GCC 4.7. Since then I've made a clang compile of Octochess as well.
"The only good bug is a dead bug." (Don Dailey)
[Blog: http://tinyurl.com/predateur ] [Facebook: http://tinyurl.com/fbpredateur ] [MacEngines: http://tinyurl.com/macengines ]
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Clang vs GCC [stockfish 4]

Post by bob »

JuLieN wrote:
bob wrote: clang has bugs. The version that came with xcode on my 8-month-old macbook will NOT compile a functional crafty. gcc 4.7.2 works perfectly, however. I don't trust clang, period. Did you PGO and such?

clang is an old gcc version...
Yes I did. And I also compiled Crafty with clang with no problem at all and it runs very well. Could you please describe the problem you had with your clang Crafty ?
Crashed instantly. There have been updates, perhaps it was fixed. It is STILL an old version of gcc...

When I typed "crafty" I got an instant crash. I can test the most recent clang since I keep up to date, will take a bit to figure out how to avoid using the newer gcc...
User avatar
velmarin
Posts: 1600
Joined: Mon Feb 21, 2011 9:48 am

Re: Clang vs GCC [stockfish 4]

Post by velmarin »

JuLieN wrote:
the GCC build takes 23mn and 27 seconds, while the clang one takes 21mn and 54 seconds on my corei7 CPU.
Excuse my ignorance.
Refers to compilation 21 minutes. :shock:
User avatar
JuLieN
Posts: 2949
Joined: Mon May 05, 2008 12:16 pm
Location: Bordeaux (France)
Full name: Julien Marcel

Re: Clang vs GCC [stockfish 4]

Post by JuLieN »

velmarin wrote:
JuLieN wrote:
the GCC build takes 23mn and 27 seconds, while the clang one takes 21mn and 54 seconds on my corei7 CPU.
Excuse my ignorance.
Refers to compilation 21 minutes. :shock:
No, sorry for the confusion, Jose. :) It was the time both versions of Stockfish took to reach depth 30. In both cases the compilation time was a matter of second (clang being a bit faster in compiling.)
"The only good bug is a dead bug." (Don Dailey)
[Blog: http://tinyurl.com/predateur ] [Facebook: http://tinyurl.com/fbpredateur ] [MacEngines: http://tinyurl.com/macengines ]
zullil
Posts: 6442
Joined: Tue Jan 09, 2007 12:31 am
Location: PA USA
Full name: Louis Zulli

Re: Clang vs GCC [stockfish 4]

Post by zullil »

JuLieN wrote:I compiled the official Stockfish 4's sources with clang, and got an exe significantly faster than the official gcc build for mac included in the archive. Here are the compared results of the bench command and a search to depth 30 in the starting position:

Code: Select all

Official:
===========================
Total time (ms) : 2836
Nodes searched  : 4132374
Nodes/second    : 1457113

info depth 30 seldepth 46 score cp 30 nodes 1699401570 nps 1207059 time 1407886 multipv 1 pv d2d4 g8f6 g1f3 e7e6 c2c4 f8b4 c1d2 b4d2 d1d2 b7b6 e2e3 e8g8 f1e2 f6e4 d2d3 c8b7 e1g1 d7d6 f3d2 e4d2 d3d2 d8g5 g2g3 b8d7 b1c3 d7f6 a1d1
info nodes 1699401570 time 1407886
bestmove d2d4 ponder g8f6

clang:
===========================
Total time (ms) : 2691
Nodes searched  : 4132374
Nodes/second    : 1535627

info depth 30 seldepth 46 score cp 30 nodes 1699401570 nps 1293199 time 1314106 multipv 1 pv d2d4 g8f6 g1f3 e7e6 c2c4 f8b4 c1d2 b4d2 d1d2 b7b6 e2e3 e8g8 f1e2 f6e4 d2d3 c8b7 e1g1 d7d6 f3d2 e4d2 d3d2 d8g5 g2g3 b8d7 b1c3 d7f6 a1d1
info nodes 1699401570 time 1314106
bestmove d2d4 ponder g8f6
According to the bench, your clang binary is about 5% faster than the official one. I've tried all recent versions of both gcc and clang (llvm). My best is a binary that is about 11% faster than the official one. This was produced using gcc-4.6 from MacPorts. popcount was used, PGO was used, but not LTO. Add -fno-tree-pre to the optimization flags.

clang is improving, but for me it has never produced a faster binary than some version of gcc. Don't have icc, since it's not free!

my gcc-4.6 binary:

Code: Select all

Total time (ms) : 3198
Nodes searched  : 4132374
Nodes/second    : 1292174
official binary:

Code: Select all

Total time (ms) : 3552
Nodes searched  : 4132374
Nodes/second    : 1163393
This is on an old Nehalem Xeon running at 2.26 GHz. One thread.
Rein Halbersma
Posts: 771
Joined: Tue May 22, 2007 11:13 am

Re: Clang vs GCC [stockfish 4]

Post by Rein Halbersma »

bob wrote:
JuLieN wrote:I compiled the official Stockfish 4's sources with clang, and got an exe significantly faster than the official gcc build for mac included in the archive. Here are the compared results of the bench command and a search to depth 30 in the starting position:

Code: Select all

Official:
===========================
Total time (ms) : 2836
Nodes searched  : 4132374
Nodes/second    : 1457113

info depth 30 seldepth 46 score cp 30 nodes 1699401570 nps 1207059 time 1407886 multipv 1 pv d2d4 g8f6 g1f3 e7e6 c2c4 f8b4 c1d2 b4d2 d1d2 b7b6 e2e3 e8g8 f1e2 f6e4 d2d3 c8b7 e1g1 d7d6 f3d2 e4d2 d3d2 d8g5 g2g3 b8d7 b1c3 d7f6 a1d1
info nodes 1699401570 time 1407886
bestmove d2d4 ponder g8f6

clang:
===========================
Total time (ms) : 2691
Nodes searched  : 4132374
Nodes/second    : 1535627

info depth 30 seldepth 46 score cp 30 nodes 1699401570 nps 1293199 time 1314106 multipv 1 pv d2d4 g8f6 g1f3 e7e6 c2c4 f8b4 c1d2 b4d2 d1d2 b7b6 e2e3 e8g8 f1e2 f6e4 d2d3 c8b7 e1g1 d7d6 f3d2 e4d2 d3d2 d8g5 g2g3 b8d7 b1c3 d7f6 a1d1
info nodes 1699401570 time 1314106
bestmove d2d4 ponder g8f6
the GCC build takes 23mn and 27 seconds, while the clang one takes 21mn and 54 seconds on my corei7 CPU.

I don't say that Clang produces faster code than GCC, but in some occurrences, like this one, it clearly does. So if you have access to both compilers you might want to try them both for your engine.
clang has bugs. The version that came with xcode on my 8-month-old macbook will NOT compile a functional crafty. gcc 4.7.2 works perfectly, however. I don't trust clang, period. Did you PGO and such?

clang is an old gcc version...
This is a known issue for some versions of XCode. It includes an old (v2.9) Clang compiler and an even older C/C++ Standard Library (based on gcc 4.2). There are plenty of links out there on how to install the latest Clang compiler and C/C++ headers (or the LLVM new libc++ headers). You might want to check clang --version on your system to see if you are up to date.
zullil
Posts: 6442
Joined: Tue Jan 09, 2007 12:31 am
Location: PA USA
Full name: Louis Zulli

Re: Clang vs GCC [stockfish 4]

Post by zullil »

Rein Halbersma wrote: There are plenty of links out there on how to install the latest Clang compiler and C/C++ headers (or the LLVM new libc++ headers). You might want to check clang --version on your system to see if you are up to date.
I think Bob has mentioned MacPorts. He can get everything he wants there.