MPI hello world in C

From ScientificComputing
Revision as of 14:01, 8 November 2021 by Jarunanp (talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

< Examples

Load modules

We will use the new software stack in this tutorial:

 [jarunanp@eu-login-10 ~]$ env2lmod  
 [jarunanp@eu-login-10 ~]$ module load gcc/6.3.0 openmpi/4.0.2

 The following have been reloaded with a version change:
   1) gcc/4.8.5 => gcc/6.3.0

 [jarunanp@eu-login-10 ~]$ which mpirun
 /cluster/apps/gcc-6.3.0/openmpi-4.0.2-4airvo32ypyuapzgi4fp2kjea5psqu3t/bin/mpirun

MPI Hello World

  • Go to $SCRATCH and create a work directory
 [jarunanp@eu-login-10 ~]$ cd $SCRATCH
 [jarunanp@eu-login-10 jarunanp]$ pwd
 /cluster/scratch/jarunanp
 [jarunanp@eu-login-10 jarunanp]$ mkdir test_mpi
 [jarunanp@eu-login-10 jarunanp]$ cd test_mpi
 [jarunanp@eu-login-10 test_mpi]$ 
  • Download the MPI Hello World example
 [jarunanp@eu-login-10 test_mpi]$ wget -c https://gitlab.ethz.ch/jarunanp/hpc-examples/-/raw/main/mpi/c/hello_mpi.c?inline=false -O hello_mpi.c
  • Compile the code
 [jarunanp@eu-login-10 test_mpi]$ mpicc -o hello_mpi hello_mpi.c
  • Run the executable
 [jarunanp@eu-login-10 test_mpi]$ mpirun -np 2 hello_mpi
 Hello world from processor eu-login-10, rank 0 out of 2 processors
 Hello world from processor eu-login-10, rank 1 out of 2 processors

Run a BSUB interactive session

  • Request an interactive session on a compute node:
 [jarunanp@eu-login-10 test_mpi]$ bsub -n 4 -W 01:00 -Is bash
 Generic job.
 Job <155089738> is submitted to queue <normal.4h>.
 <<Waiting for dispatch ...>>
 <<Starting on eu-ms-022-14>>
 [jarunanp@eu-ms-022-14 test_mpi]$
  • Run the executable
 [jarunanp@eu-ms-022-14 test_mpi]$ mpirun -np 4 hello_mpi
 Hello world from processor eu-ms-022-14, rank 0 out of 4 processors
 Hello world from processor eu-ms-022-14, rank 1 out of 4 processors
 Hello world from processor eu-ms-022-14, rank 2 out of 4 processors 
 Hello world from processor eu-ms-022-14, rank 3 out of 4 processors
 [jarunanp@eu-ms-022-14 test_mpi]$
  • Exit the interactive session
 [jarunanp@eu-ms-022-14 test_mpi]$ exit
 exit
 [jarunanp@eu-login-10 test_mpi]$


Submit a batch job with BSUB command line

  • You can submit a job by using BSUB command line
 [jarunanp@eu-login-10 test_mpi]$ bsub -n 4 -W 10 "mpirun hello_mpi"
 MPI job.
 Job <155090084> is submitted to queue <normal.4h>.
  • Check the job status
 [jarunanp@eu-login-10 test_mpi]$ bjobs
 JOBID      USER    STAT  QUEUE      FROM_HOST   EXEC_HOST   JOB_NAME   SUBMIT_TIME
 155090084  user PEND  normal.4h  eu-login-10             *llo_world Dec  8 15:53
 [jarunanp@eu-login-10 test_mpi]$
  • Check the output file lsf.o155090084
 [jarunanp@eu-login-10 test_mpi]$ cat lsf.o155090084

Create a job script

  • Create a job script called job_script.bsub
 #!/usr/bin/bash
 #BSUB -n 4
 #BSUB -W 10
 #BSUB -J test_mpi

 source /cluster/apps/local/env2lmod.sh
 module load gcc/6.3.0 openmpi/4.0.2
 export OMP_NUM_THREADS=0
 mpirun hello_mpi
  • Submit a job using the job script
 [jarunanp@eu-login-10 test_mpi]$ bsub < job_script.bsub 
 MPI job.
 Job <155090224> is submitted to queue <normal.4h>.


< Examples