Saturday, September 19, 2015

Configure PETSc and Eclipse Parallel on OSX / Linux [C/C++/Fortran]

Nowadays I'm working on my master project which requires use of PETSc framework in c++ language.
Those who develop Finite Element codes know how essential it is to use Sparse matrices and fast solvers to solve FEM's stiff matrix. PETSc does this, it makes it extremely easy to define and use sparse matrices and provides many direct/iterative modern solvers.

The steps below are checked on both Mac OSX Yosemitte and Linux Ubuntu 14.04 LTS. Installation on other linux distributions is the same, just the package names differ (openmpi-devel on redhat vs openmpi-dev on debian).
1- Install openmpi, blas and lapack using your OS package manager. You need to install their devel pacakges if you're using linux.

2- Download latest version of PETSc from their website. I recommend the lite version.

3- Extract the package and enter it.

4- Set these two environment variables in your bash_rc:
PETSC_DIR= /path/to/your/petsc/directory
PETSC_ARCH=debug


5- run ./configure

PETSc will automatically detect your mpi compilers and gives you the next command to compile the whole package.


6- Follow the commands that PETSc gives you until the compilation / testing finish.

7- you compiled the debug version of PETSc. now again, set the PETSC_ARCH environment variable value to:
PETSC_ARCH=release
and run ./configure with no debugging:

./configure --with-debugging=0 COPTFLAGS='-O3' CXXOPTFLAGS='-O3' FOPTFLAGS='-O3'
and again follow PETSc commands to finish compilation.

8- It's now the time to choose an IDE. I myself recommend Eclipse. It's free and lightweight and supports C, C++ and Fortran.
Download the "Eclipse for Parallel Application Developers" from their website.
Eclipse requires Java which can easily get installed from OS package manager.
If you want to program Fortran in Eclipse, you need to download and install the Photran plugin on Eclipse.

9- Openmpi compilers are installed in /usr/local/bin on Mac OSx. You might get the following errors in case the /usr/local/bin path isn't loaded by default in Eclipse:
"/bin/sh: mpicc: command not found"
"/bin/sh: mpic++: command not found"
"/bin/sh: mpif90: command not found"

The fix is simple:
cd /usr/bin
sudo ln -s /usr/local/bin/mpicc
sudo ln -s /usr/local/bin/mpicxx
sudo ln -s /usr/local/bin/mpic++
sudo ln -s /usr/local/bin/mpif90

10- Open Eclipse and create a new MPI C/C++/Fortran project. Check if you can run hello world without any problem.
Now it's time to set eclipse to include PETSc library. (Read PETSc Manual -> OTHER PETSC FEATURES -> Eclipse Users)

Right click on your project -> Properties.
10-1- C/C++ Build -> Environment
Add PETSC_DIR with path to your directory. Then add PETSC_ARCH with value of "debug" for the "Debug" configuration and "release" for the "Release" configuration of Eclipse.
10-2- C/C++ Build -> Settings -> C++ Linker -> Libraries ("Fortran Build -> Settings -> Fortran Linker -> Libraries" in case of Fortran compiler)
Add
petsc
to list of Libraries and
${PETSC_DIR}/${PETSC_ARCH}/lib
to "Library Search Path":

10-3- C/C++ Build -> Settings -> C++ Compiler -> Includes ("Fortran Build -> Settings -> Fortran Compiler -> Directories" in case of Fortran compiler)
Add the following values to Include Paths :
${PETSC_DIR}/include
${PETSC_DIR}/${PETSC_ARCH}/include

10-4 C/C++ Build -> Settings -> C++ Linker ->Miscellaneous ("Fortran Build -> Settings -> Fortran Linker -> Miscellaneous" in case of Fortran compiler)
Add
-Wl,-rpath,"${PETSC_DIR}/${PETSC_ARCH}/lib"
to Linker flags:

10-5- That's all, check if you can run PETSc hello world:

#include <iostream>
#include "petsc.h"
using namespace std;

int main(int argc, char *argv[]) {
    PetscInitialize(&argc,&argv,PETSC_NULL,PETSC_NULL);
    PetscPrintf(PETSC_COMM_WORLD,"Hello World\n");
    PetscFinalize();

    return 0;
}

On Mac OSX:


On Ubuntu 14.04 LTS:


10-6- Fortran compiler needs the following extra setting:

10-6-1 Fortran Build -> Settings -> Fortran Compiler -> Miscellaneous
Add "-cpp -dM" flag.



That's all!

How to disable Debian 12 sleep on production servers

 Debian 12 has power saver enabled by default which causes your server to go to sleep if there is no mouse / keyboard interaction. To resolv...