Difference between revisions of "MPI hello world in C"

From ScientificComputing
Jump to: navigation, search
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
<table style="width: 100%;">
+
{{back_to_tutorials}}
<tr valign=top>
 
<td style="width: 30%; text-align:left">
 
< [[Running applications | Tutorials]]
 
</td>
 
</tr>
 
</table>
 
  
 
== Load modules ==
 
== Load modules ==
 
We will use the new software stack in this tutorial:
 
We will use the new software stack in this tutorial:
  
   [user@eu-login-10 ~]$ env2lmod   
+
   [jarunanp@eu-login-10 ~]$ env2lmod   
   [user@eu-login-10 ~]$ module load gcc/6.3.0 openmpi/4.0.2
+
   [jarunanp@eu-login-10 ~]$ module load gcc/6.3.0 openmpi/4.0.2
 
+
 
   The following have been reloaded with a version change:
 
   The following have been reloaded with a version change:
 
     1) gcc/4.8.5 => gcc/6.3.0
 
     1) gcc/4.8.5 => gcc/6.3.0
 
+
   [user@eu-login-10 ~]$ which mpirun
+
   [jarunanp@eu-login-10 ~]$ which mpirun
 
   /cluster/apps/gcc-6.3.0/openmpi-4.0.2-4airvo32ypyuapzgi4fp2kjea5psqu3t/bin/mpirun
 
   /cluster/apps/gcc-6.3.0/openmpi-4.0.2-4airvo32ypyuapzgi4fp2kjea5psqu3t/bin/mpirun
  
 
== MPI Hello World ==
 
== MPI Hello World ==
 
* Go to $SCRATCH and create a work directory
 
* Go to $SCRATCH and create a work directory
   [user@eu-login-10 ~]$ cd $SCRATCH
+
   [jarunanp@eu-login-10 ~]$ cd $SCRATCH
   [user@eu-login-10 user]$ pwd
+
   [jarunanp@eu-login-10 jarunanp]$ pwd
   /cluster/scratch/user
+
   /cluster/scratch/jarunanp
   [user@eu-login-10 user]$ mkdir test_mpi
+
   [jarunanp@eu-login-10 jarunanp]$ mkdir test_mpi
   [user@eu-login-10 user]$ cd test_mpi
+
   [jarunanp@eu-login-10 jarunanp]$ cd test_mpi
   [user@eu-login-10 test_mpi]$  
+
   [jarunanp@eu-login-10 test_mpi]$  
  
 
* Download the MPI Hello World example
 
* Download the MPI Hello World example
   [user@eu-login-10 test_mpi]$ wget https://scicomp.ethz.ch/public/examples/mpi/mpi_hello_world.c
+
   [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
 
* Compile the code
   [user@eu-login-10 test_mpi]$ mpicc -o mpi_hello_world mpi_hello_world.c
+
   [jarunanp@eu-login-10 test_mpi]$ mpicc -o hello_mpi hello_mpi.c
  
 
* Run the executable
 
* Run the executable
   [user@eu-login-10 test_mpi]$ mpirun -np 2 mpi_hello_world
+
   [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 0 out of 2 processors
 
   Hello world from processor eu-login-10, rank 1 out of 2 processors
 
   Hello world from processor eu-login-10, rank 1 out of 2 processors
Line 43: Line 37:
  
 
* Request an interactive session on a compute node:
 
* Request an interactive session on a compute node:
   [user@eu-login-10 test_mpi]$ bsub -n 4 -W 01:00 -Is bash
+
   [jarunanp@eu-login-10 test_mpi]$ bsub -n 4 -W 01:00 -Is bash
 
   Generic job.
 
   Generic job.
 
   Job <155089738> is submitted to queue <normal.4h>.
 
   Job <155089738> is submitted to queue <normal.4h>.
 
   <<Waiting for dispatch ...>>
 
   <<Waiting for dispatch ...>>
 
   <<Starting on eu-ms-022-14>>
 
   <<Starting on eu-ms-022-14>>
   [user@eu-ms-022-14 test_mpi]$
+
   [jarunanp@eu-ms-022-14 test_mpi]$
  
 
* Run the executable
 
* Run the executable
   [user@eu-ms-022-14 test_mpi]$ mpirun -np 4 mpi_hello_world
+
   [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 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 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 2 out of 4 processors  
 
   Hello world from processor eu-ms-022-14, rank 3 out of 4 processors
 
   Hello world from processor eu-ms-022-14, rank 3 out of 4 processors
   [user@eu-ms-022-14 test_mpi]$
+
   [jarunanp@eu-ms-022-14 test_mpi]$
  
 
* Exit the interactive session
 
* Exit the interactive session
   [user@eu-ms-022-14 test_mpi]$ exit
+
   [jarunanp@eu-ms-022-14 test_mpi]$ exit
 
   exit
 
   exit
   [user@eu-login-10 test_mpi]$
+
   [jarunanp@eu-login-10 test_mpi]$
  
  
 
== Submit a batch job with BSUB command line ==
 
== Submit a batch job with BSUB command line ==
 
* You can submit a job by using BSUB command line
 
* You can submit a job by using BSUB command line
   [user@eu-login-10 test_mpi]$ bsub -n 4 -W 10 "mpirun mpi_hello_world"
+
   [jarunanp@eu-login-10 test_mpi]$ bsub -n 4 -W 10 "mpirun hello_mpi"
 
   MPI job.
 
   MPI job.
 
   Job <155090084> is submitted to queue <normal.4h>.
 
   Job <155090084> is submitted to queue <normal.4h>.
  
 
* Check the job status
 
* Check the job status
   [user@eu-login-10 test_mpi]$ bjobs
+
   [jarunanp@eu-login-10 test_mpi]$ bjobs
 
   JOBID      USER    STAT  QUEUE      FROM_HOST  EXEC_HOST  JOB_NAME  SUBMIT_TIME
 
   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
 
   155090084  user PEND  normal.4h  eu-login-10            *llo_world Dec  8 15:53
   [user@eu-login-10 test_mpi]$
+
   [jarunanp@eu-login-10 test_mpi]$
  
 
* Check the output file lsf.o155090084
 
* Check the output file lsf.o155090084
   [user@eu-login-10 test_mpi]$ cat lsf.o155090084
+
   [jarunanp@eu-login-10 test_mpi]$ cat lsf.o155090084
  
 
== Create a job script ==
 
== Create a job script ==
  
* Create a job script called job_script.bsub
+
* Create a job script called ''job_script.bsub''
  
 
   #!/usr/bin/bash
 
   #!/usr/bin/bash
Line 87: Line 81:
 
   #BSUB -W 10
 
   #BSUB -W 10
 
   #BSUB -J test_mpi
 
   #BSUB -J test_mpi
   mpirun mpi_hello_world
+
 +
  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
 
* Submit a job using the job script
   [user@eu-login-10 test_mpi]$ bsub < job_script.bsub  
+
   [jarunanp@eu-login-10 test_mpi]$ bsub < job_script.bsub  
 
   MPI job.
 
   MPI job.
 
   Job <155090224> is submitted to queue <normal.4h>.
 
   Job <155090224> is submitted to queue <normal.4h>.
  
  
<table style="width: 100%;">
+
{{back_to_tutorials}}
<tr valign=top>
 
<td style="width: 30%; text-align:left">
 
< [[Running applications | Tutorials]]
 
</td>
 
</tr>
 
</table>
 

Latest revision as of 15:01, 8 November 2021

< 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