How to build GCC* cilkplus branch in 64bit Ubuntu* 12.04

Introduction:
Intel® Cilk™ Plus is an open source project now. It is supported in GCC* but still not merged into the mainline GCC* version. We can build the 'cilkplus' branch of GCC* to make it support the Intel® Cilk™ Plus extensions. The steps are exact same with building GCC* upstream code, so this article is targeted for those who are not familiar with building GCC and want to use Cilk Plus with GCC.

(1) Prerequisites
OS: Ubuntu* 12.04 LTS 64bit.
You will need the following packages to build GCC.
1. Source packages. Note that the links below are for the versions needed to build the initial release of the cilkplus GCC branch. Newer versions may need to be used with future versions of the compiler.
A. GNU Multiprecision Arithmetic Library (GMP) for arbitrary precision arithmetic: (ftp://ftp.gmplib.org/pub/) download the latest version.
B. GNU MPFR Library multiple-precision floating-point computations with correct rounding: (http://www.mpfr.org/mpfr-current), download the latest version.
C. GNU MPC Library for arithmetic using complex numbers: (http://www.multiprecision.org/mpc/), download the latest version.
Notes: you'd better always download the latest version of these packages, as newer versions may need newer versions of these packages, please do not just use the same version with me in below content, alwasy using the latest. Also, you may find these packages in ftp://gcc.gnu.org/pub/gcc/infrastructure/.
2. In addition to the sources above, you will also need the following tools installed in your machine
A. flex or lex - Lexical analyzer
B. bison or Yacc - Parser generator
C. Make utility (example gmake) to automatically create executables
D. GCC/G++ Compiler (version 4.4 or above) with the appropriate binary utilities
E. GNU AutoGen utility - Automatic program generator
F. SVN or GIT source control packages
If you are using same version of OS with me (Ubuntu 12.04 LTS), you only need to run below command:
[shell]sudo apt-get install binutils build-essential m4 autogen bison[/shell] 3. Diskspace: You will need approximately 2 GB of diskspace to hold the sources, executables and libraries. Along with this, you will also require ~3 GB of diskspace to hold the temporary files.
4. Time: This process is a bit time-consuming and so please have ~4-5 hours in-hand to complete the process. The most time consuming of all is the step to run 'make' to build gcc. This step is not interactive and so you can let this step run overnight if you wish.

(2) Install gmp
1. download latest gmp (for me it is: gmp-4.3.2.tar.bz2).
2. compile and install gmp:
[shell]sudo mkdir -p /opt/gmp-4.3.2 tar -jxvf gmp-4.3.2.tar.bz2 cd gmp-4.3.2 ./configure --prefix=/opt/gmp-4.3.2 make && make check && sudo make install[/shell] Notes: It is recommended to run 'make check'.

(3) Install mpfr
1. download latest mpfr (for me it is: mpfr-2.4.2.tar.bz2).
2. compile and install mpfr:
[shell]sudo mkdir -p /opt/mpfr-2.4.2 tar -jxvf mpfr-2.4.2.tar.bz2 cd mpfr-2.4.2 ./configure --prefix=/opt/mpfr-2.4.2 --with-gmp=/opt/gmp-4.3.2 make && make check && sudo make install[/shell] Notes: You must install gmp before installing 'mpfr' as 'mpfr' depends on 'gmp'.

(4) install mpc
1. download latest mpc (for me it is: mpc-0.8.1.tar.gz).
2. compile and install mpc:
[shell]sudo mkdir -p /opt/mpc-0.8.1 tar -zxvf mpc-0.8.1.tar.gz cd mpc-0.8.1 ./configure --prefix=/opt/mpc-0.8.1 --with-gmp=/opt/gmp-4.3.2 --with-mpfr=/opt/mpfr-2.4.2 make && make check && sudo make install[/shell] Notes: 'mpc' depends on 'gmp' and 'mpfr'.

(5) build and install GCC with Cilk Plus
1. download the source code of 'cilkplus' branch. There are various ways to download it.
SVN:
GIT:
Here, I download the git mirror for cilkplus branch through browser directly. (The snapshot is 'gcc-0dfa790.tar.gz')
If you are a beginner, I recommend you to use same way as me, use browser to find the download link in the mirrors, but if you want the latest development branch, you have to use source control tools like SVN or GIT.
If you are using SVN, run below command line to get the development branch:
[shell]svn co http://gcc.gnu.org/svn/gcc/branches/cilkplus cilkplus-gcc[/shell] If you are using GIT, run below command line to get the development branch:
[shell]git clone http://gcc.gnu.org/git/gcc.git cilkplus-gcc cd cilkplus-gcc git checkout -b my_cilkplus_branch origin/cilkplus cd ..[/shell]

2. compile and install gcc cilkplus branch:
A. you need to set "LD_LIBRARY_PATH", "C_INCLUDE_PATH" and "LIBRARY_PATH"(this is only needed for Ubuntu, for some OSes, you have no need to set LIBRAR_PATH to '/usr/lib/x86_64-linux-gnu')
B. Unpack the source code if you are also using the git mirror snapshots like me, but if you are using SVN or GIT, you can skip this. Assume the root path of your source is <cilk-plus-dir>.
C. Create a directory (I use '/opt/gcc-cilkplus-install') to install the gcc cilkplus compiler.
D. Create a directory (I use '<cilk-plus-dir>/../gcc_cilkplus_build') to store the object files generated from building GCC.
E. 'cd' to 'gcc_cilkplu_build' foler, and run the configuration script like:
<cilk-plus-dir>/configure --prefix=gcc-cilkplus-install --with-gmp=/opt/gmp-4.3.2 --with-mpfr=/opt/mpfr-2.4.2 --with-mpc=/opt/mpc-0.8.1 --disable-multilib --enable-languages=c,c++

F. Run 'make' comand to build GCC source code
G. Run 'make install' comand to install it to '/opt/gcc-cilkplus-install'
Below are all the commands I have run, please take it as a reference:
[shell]export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/gmp-4.3.2/lib:/opt/mpfr-2.4.2/lib:/opt/mpc-0.8.1/lib export C_INCLUDE_PATH=/usr/include/x86_64-linux-gnu && export CPLUS_INCLUDE_PATH=$C_INCLUDE_PATH && export OBJC_INCLUDE_PATH=$C_INCLUDE_PATH export LIBRARY_PATH=/usr/lib/x86_64-linux-gnu tar -zxvf gcc-0dfa790.tar.gz sudo mkdir -p /opt/gcc-cilkplus-install mkdir gcc_cilkplus_build && cd gcc_cilkplus_build ../gcc-0dfa790/configure --prefix=gcc-cilkplus-install --with-gmp=/opt/gmp-4.3.2 --with-mpfr=/opt/mpfr-2.4.2 --with-mpc=/opt/mpc-0.8.1 --disable-multilib --enable-languages=c,c++ make -j8 sudo make install[/shell] Notes:
(1) It is suggested to run 'configure' in a standalone foler instead of running it in the source tree of gcc cilkplus.
(2) 'make -j8', this step will build the source code, it will take quite a long time, you may change the argument of '-j' according to your system.


(6) Create the 'gccvars.sh' script
This is optional, you may create a script to help to set the environment of the built gcc. Following is a reference for 'gccvars.sh':
[shell]# filename: gccvars.sh # 'source gccvars.sh' to set the environment of gcc export C_INCLUDE_PATH=/usr/include/x86_64-linux-gnu:$C_INCLUDE_PATH export CPLUS_INCLUDE_PATH=$C_INCLUDE_PATH export OBJC_INCLUDE_PATH=$C_INCLUDE_PATH export LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:$LIBRARY_PATH export GCCDIR=/opt/gcc-cilkplus-install export PATH=$GCCDIR/bin:$PATH export LD_LIBRARY_PATH=$GCCDIR/lib:$GCCDIR/lib64:/opt/gmp-4.3.2/lib:/opt/mpfr-2.4.2/lib:/opt/mpc-0.8.1/lib:$LD_LIBRARY_PATH export MANPATH=$GCCDIR/share/man:$MANPATH[/shell] Notes:
1. GCCDIR is the installed path of the built gcc.
2. To use the GCC cilkplus compiler, you need to make sure it is in your PATH and the libraries in your LIBRARY_PATH and LD_LIBRARY_PATH. Please reference to above 'gccvars.sh' to set the correct environment for you.
3. With this script, you can use "source gccvars.sh" to set up the environment of gcc, after it is set up, just use "gcc <src_files> -lcilkrts" to build your cilkplus code.

(7) Test Cilk Plus with GCC
To use cilk plus with gcc, you need to explicitly add '-lcilkrts' flag to the command line. See below test case for more details (it contains how to compile and the results):
// filename: test_cilkplus.cpp
// compile: g++ test_cilkplus.cpp -lcilkrts

#include <stdio.h>
#include <unistd.h>

#include <cilk/cilk.h>
#include <cilk/cilk_api.h>

void task(int i) {
    printf("task: %d, workder id: %dn", i, __cilkrts_get_worker_number());
    sleep(1);
}

int main() {
    for(int i=0;i<10;i++)
	    cilk_spawn task(i);
    cilk_spawn task(-1);
    cilk_sync;
    return 0;
}

/* compile and result:
#g++ test_cilkplus.cpp -lcilkrts
#./a.out 
task: 0, workder id: 0
task: 1, workder id: 1
task: 2, workder id: 2
task: 3, workder id: 0
task: 4, workder id: 1
task: 5, workder id: 2
task: 6, workder id: 0
task: 7, workder id: 1
task: 8, workder id: 2
task: 9, workder id: 0
task: -1, workder id: 1
#
notes: you can use 'export CILK_NWORKERS=N' to set the max workders of cilk plus runtime.
*/
(8) Resources for the Cilk Plus Open Source Project
If you have any issues regarding this, please feel free to contact me.
Einzelheiten zur Compiler-Optimierung finden Sie in unserem Optimierungshinweis.