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