How to install setup mpi or mpich and run hello world program in Linux cluster with slurm files?

At first, you need to download the mpich file from mpich website. After getting the link, in the linux command prompt we need to write the command

wget http://www.mpich.org/static/downloads/3.3.2/mpich-3.3.2.tar.gz

To unzip the file, we need to use tar unzip command

tar -xvf mpich-3.3.2.tar.gz

Then we need to export path environment variable like the below command.


export PATH=$PATH:/home/username/mpi/mpich_install/bin

echo $PATH will show the path values like this.


/cm/shared/apps/mpich/ge/gcc/64/3.3/bin:/cm/shared/apps/slurm/18.08.9/sbin:/cm/shared/apps/slurm/18.08.9/bin:/cm/local/apps/gcc/8.2.0/bin:/usr/lib64/qt-3.3/bin:/share/apps/rc/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/ibutils/bin:/opt/dell/srvadmin/bin:/home/username/.local/bin:/home/username/bin:/home/username/mpi/mpich_install/bin

Now, we need to write the hello_world problem.
the hello_world.c program is below:


#include
#include
#include

int main(int argc, char *argv[])
{

int my_rank; int size;

MPI_Init(&argc, &argv); /*START MPI */

/*DETERMINE RANK OF THIS PROCESSOR*/
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

/*DETERMINE TOTAL NUMBER OF PROCESSORS*/
MPI_Comm_size(MPI_COMM_WORLD, &size);

printf("Hello world! I'm rank (processor number) %d of %d processor \n", my_rank, size);

MPI_Finalize(); /* EXIT MPI */
return 0;
}

We need to compile the program. The code is below:


mpicc hello_world.c -o hello_world.exe

The executable file will be hello_world.exe. We can run the mpi file by the command

mpirun ./hello_world1.exe

The output will be
Hello world! I'm rank (processor number) 0 of 1 processor

If we run the command with n tasks, it will show the below output.

$ mpirun -n 4 ./hello_world1.exe
Hello world! I'm rank (processor number) 3 of 4 processor
Hello world! I'm rank (processor number) 0 of 4 processor
Hello world! I'm rank (processor number) 2 of 4 processor
Hello world! I'm rank (processor number) 1 of 4 processor

Now, we need to run the mpi program from slurm script. By using the vim we can add the bash scripting file to the folder.

vim submit_helloworld.sh

it will show the editor where we can insert values. To insert text we need to press i, and after the editing we need to press escape key(esc) and :wq (to save and exit).
we can add the text to the file


#!/bin/bash -x

#SBATCH--job-name=hello_world
#SBATCH--output=out_hello_world.%j
#SBATCH--error=err_hello_world.%j

#SBATCH--partition=express
#SBATCH--nodes=1
#SBATCH--ntasks=4

module load mpich/ge/gcc/64/3.3

#mpiexec ./hello_world.exe
mpirun ./hello_world.exe


here, job_name is hello_world, output file is out_hello_world, error file is err_hello_world. %j is for the job id.
Number of tasks ntasks is 4, number of nodes is 1.
We need to load mpich module form the module loader

My cat command we can see the text.


cat submit_helloworld.sh

we can submit the shell script to the cluster by the below command and it will show us the job id


sbatch submit_helloworld.sh
Submitted batch job 6266850

Here job id is 6266850.

To see the job status, we can use squeue command


squeue --user=userid

After the finishing the job, it squeue will not show no task in the queue.

By pressing ls command we can see the out_hello_world.6266850 file in the directory.

By cat out_hello_world.6266850 we can see the output.


cat out_hello_world.6266850
Hello world! I'm rank (processor number) 0 of 4 processor
Hello world! I'm rank (processor number) 1 of 4 processor
Hello world! I'm rank (processor number) 2 of 4 processor
Hello world! I'm rank (processor number) 3 of 4 processor

How to search mpi module available to load in Linux cluster?

To find the module to load Linux cluster, we need to type

module spider module_name_key
module_name_key is the partial name which we can use for searching.
it will bring us the modules which are available for us to load.
For example,
I wanted to load openmpi module from the Linux cluster. So I need to know the exact name of mpi module of the cluster. I search it by
module spider openmpi

it showed me the openmpi modules which are available for loading.


-------------------------------------------------------------------------------------------------------------------------------------------------------------------
OpenMPI:
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
Description:
The Open MPI Project is an open source MPI-3 implementation.

Versions:
OpenMPI/1.6.4-GCC-4.7.2
OpenMPI/1.6.5-GCC-4.8.1
OpenMPI/1.6.5-GCC-4.8.2
OpenMPI/1.8.4-GCC-4.8.4
OpenMPI/1.8.8-GNU-4.9.3-2.25
OpenMPI/1.8.8-iccifort-2015.3.187-GNU-4.9.3-2.25
OpenMPI/1.10.2-GCC-4.9.3-2.25
OpenMPI/1.10.3-GCC-5.4.0-2.26
OpenMPI/1.10.4-intel-2016a
OpenMPI/1.10.7-GCC-6.1.0-2.27
OpenMPI/1.10.7-intel-2017a
OpenMPI/2.0.2-GCC-6.3.0-2.27
OpenMPI/2.0.2-gcccuda-2017.01
OpenMPI/2.1.1-GCC-6.4.0-2.28
OpenMPI/2.1.2-GCC-6.4.0-2.28
OpenMPI/2.1.2-gcccuda-2018a
OpenMPI/3.0.0-iccifort-2017.1.132-GCC-6.3.0-2.27
OpenMPI/3.1.1-GCC-7.3.0-2.30
OpenMPI/3.1.1-gcccuda-2018b
OpenMPI/3.1.2-gcccuda-2018b
OpenMPI/3.1.3-GCC-8.2.0-2.31.1
OpenMPI/4.0.1-GCC-8.3.0-2.32

--------------------------------------------------------------------------------------------------------------------------------------------------------------------
For detailed information about a specific "OpenMPI" module (including how to load the modules) use the module's full name.


Suppose, we want to load the module OpenMPI/1.6.4-GCC-4.7.2

We need to load the module by this command

module load OpenMPI/1.6.4-GCC-4.7.2

By typing the following command I can check the module list in the cluster.


module list

It will show you the list which is loaded for you in the cluster.

To be specific


$ module list

Currently Loaded Modules:
1) shared 2) rc-base 3) DefaultModules 4) gcc/8.2.0 5) slurm/18.08.9 6) mpich/ge/gcc/64/3.3

$ module load OpenMPI/1.6.4-GCC-4.7.2
$ module list

Currently Loaded Modules:
1) shared 3) DefaultModules 5) slurm/18.08.9 7) GCC/4.7.2 9) OpenMPI/1.6.4-GCC-4.7.2
2) rc-base 4) gcc/8.2.0 6) mpich/ge/gcc/64/3.3 8) hwloc/1.6.2-GCC-4.7.2