Wednesday, September 15, 2010

Compiling OpenMPI with GCC-4.x.x


In order to compile OpenMPI, you will need a working compiler for the desired target languages. For the purposes of this post, I will assume the use of the GCC-4.2.1 or later, with target languages of C,C++, and GFORTRAN. See previous posts for information on compiling the GCC-4.x.x compilers and their prerequisites. From my personal experience as a C/C++/FORTRAN developer, it is usually best to avoid using the base compilers on most linux distros, especially when linking against existing scientific libraries, in order to avoid shared library versioning problems. So, for best results, build your own GCC compilers with a prefix directory similar to "/usr/local/gcc421". ALWAYS use "--enable-static --disable-shared --with-pic" flags when configuring your GCC compiler builds to avoid shared library compiler dependencies in your compiled code. Make sure you add the bin and lib directories to the front of your PATH and LD_LIBRARY_PATH variables in your shell environment (.bashrc or .profile). Using the above prefix directory as an example:


export PATH=/usr/local/gcc421/bin:$PATH
64-bit:
export LD_LIBRARY_PATH=/usr/local/gcc421/lib64:$LD_LIBRARY_PATH
32-bit:
export LD_LIBRARY_PATH=/usr/local/gcc421/lib:$LD_LIBRARY_PATH

Additionally, you will want to set the following environment variables to point to the proper compilers:

export CC=/usr/local/gcc421/bin/gcc
export CXX=/usr/local/gcc421/bin/g++
export F77=/usr/local/gcc421/bin/gfortran
export F90=/usr/local/gcc421/bin/gfortran
export FC=/usr/local/gcc421/bin/gfortran

Once you have the compilers installed, download a source tarball of the latest stable version of OpenMPI and untar it somewhere in your home directory. When configuring the openmpi source, use a prefix directory name that indicates what version of openmpi and gcc are being used. For example, if your base compiler is gcc-4.2.1 and your are compiling openmpi-1.4.2, your prefix could be as follows:

"--prefix=/usr/local/ompi142-gcc421

In my experience, correctly labeling of the prefix directory avoids version confusion and allows your to easily switch between compiler/openmpi versions in the future by changing your PATH and LD_LIBRARY_PATH environment variables as follows:

gccver="421"
if [[ $gccver == "421" ]]
then
export CC=/usr/local/gcc421/bin/gcc
export CXX=/usr/local/gcc421/bin/g++
export F77=/usr/local/gcc421/bin/gfortran
export F90=/usr/local/gcc421/bin/gfortran
export FC=/usr/local/gcc421/bin/gfortran
ompiver="142" #change to match your desired version
if [[ $ompiver == "142" ]]
then
export PATH=/usr/local/ompi142-gcc421/bin:/usr/local/gcc421/bin:$PATH
#For 64-bit
export LD_LIBRARY_PATH=/usr/local/ompi142-gcc421/lib:/usr/local/gcc421/lib64:$LD_LIBRARY_PATH
#For 32-bit, comment the line above and uncomment the below
#export LD_LIBRARY_PATH=/usr/local/ompi142-gcc421/lib:/usr/local/gcc421/lib:$LD_LIBRARY_PATH
elif [[ $ompiver=="???" ]] #other installed versions
....
fi

elif [[ $gccver == "???" ]] #other installed versions
then
....
fi

In general, I build openmpi as follows using the listed configuration flags:

./configure --prefix=/usr/local/ompi142-gcc421 --enable-static --disable-shared --with-pic
make 2>&1 | tee make.log

If your prefix is directory is located somewhere within a root owned directory, then do either
sudo make install
or
su root
make install

Otherwise, if you are installing in a user owned directory, just do
make install

If you've followed all these steps, you should now have a working OpenMPI installation. In order to automatically include the proper libraries and paths, remember to use the proper mpi compiler wrapper (mpicc, mpic++, mpif90) to compile your code. To see which compiler the wrapper is pointing to you can use the following to return the underlying compiler version information.
mpicc -v

I hope you have found this blog post helpful in you parallel computing endeavors.

No comments: