Performance Optimization/Multiple nodes (MPI): Difference between revisions

From HPCwiki
Jump to navigation Jump to search
No edit summary
IA migration §8: rewrite — keep OpenMPI hello-world (+ bucket load), drop stale mvapich2/ib0 B4F example, TODO for current MPI/interconnect setup (via update-page on MediaWiki MCP Server)
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
MPI (the Message Passing Interface) lets a single program run across many cores, and even many nodes, at once by passing messages between its processes. Use it for software written to scale beyond one node. To use several cores within a single node, see [[Performance Optimization/Multiple CPUs]]; for many independent tasks, see [[Performance Optimization/Multiple nodes (arrayjobs)]].


== A simple 'Hello World' example ==
== Compiling an MPI program ==
Consider the following simple MPI version, in C, of the 'Hello World' example:


<source lang='cpp'>
Load a software bucket, then a compiler and an MPI library, through the module system. A bucket has to be loaded before its modules are visible (see [[Environment Modules]]). To avoid library conflicts it is safest to start from a clean environment — note that purging also removes the <code>slurm</code> module, so reload it:
 
<syntaxhighlight lang="bash">
module purge
module load 2024
module load gcc openmpi/gcc slurm
</syntaxhighlight>
 
As a simple example, here is the classic MPI "Hello World" in C:
 
<syntaxhighlight lang="c">
#include <stdio.h>
#include <stdio.h>
#include <mpi.h>
#include <mpi.h>
int main(int argc, char ** argv) {
int main(int argc, char **argv) {
  int size,rank,namelen;
    int size, rank, namelen;
  char processor_name[MPI_MAX_PROCESSOR_NAME];
    char processor_name[MPI_MAX_PROCESSOR_NAME];
  MPI_Init(&argc, &argv);
    MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  MPI_Comm_size(MPI_COMM_WORLD,&size);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
  MPI_Get_processor_name(processor_name, &namelen);
    MPI_Get_processor_name(processor_name, &namelen);
  printf("Hello MPI! Process %d of %d on %s\n", rank, size, processor_name);
    printf("Hello MPI! Process %d of %d on %s\n", rank, size, processor_name);
  MPI_Finalize();
    MPI_Finalize();
}
}
</source>
</syntaxhighlight>


Before compiling, make sure that the compilers that are required available.
Compile it with the MPI compiler wrapper:
<source lang='bash'>
module list
</source>


To avoid conflicts between libraries, the safest way is purging all modules:
<syntaxhighlight lang="bash">
<source lang='bash'>
mpicc hello_mpi.c -o hello_mpi
module purge
</syntaxhighlight>
</source>


The load both gcc and openmpi libraries. If modules were purged, then slurm needs to be reloaded too.
== Running an MPI program ==
<source lang='bash'>
module load gcc/4.8.1 openmpi/gcc/64/1.6.5 slurm/2.5.7
</source>


Compile the <code>hello_mpi.c</code> code.
Launch the MPI processes with <code>srun</code>, which spreads them across the nodes your job was allocated. For example, two nodes with four tasks each:
<source lang='bash'>
mpicc hello_mpi.c -o test_hello_world
</source>


If desired, a list of libraries compiled into the executable can be viewed:
<syntaxhighlight lang="bash">
<source lang='bash'>
srun --nodes=2 --ntasks-per-node=4 ./hello_mpi
ldd test_hello_world
</syntaxhighlight>
</source>


  linux-vdso.so.1 => (0x00002aaaaaacb000)
In a batch job, request the nodes and tasks with <code>#SBATCH</code> and launch with <code>srun</code>:
  libmpi.so.1 => /cm/shared/apps/openmpi/gcc/64/1.6.5/lib64/libmpi.so.1 (0x00002aaaaaccd000)
  libdl.so.2 => /lib64/libdl.so.2 (0x00002aaaab080000)
  libm.so.6 => /lib64/libm.so.6 (0x00002aaaab284000)
  libnuma.so.1 => /usr/lib64/libnuma.so.1 (0x0000003e29400000)
  librt.so.1 => /lib64/librt.so.1 (0x00002aaaab509000)
  libnsl.so.1 => /lib64/libnsl.so.1 (0x00002aaaab711000)
  libutil.so.1 => /lib64/libutil.so.1 (0x00002aaaab92a000)
  libpthread.so.0 => /lib64/libpthread.so.0 (0x00002aaaabb2e000)
  libc.so.6 => /lib64/libc.so.6 (0x00002aaaabd4b000)
  /lib64/ld-linux-x86-64.so.2 (0x00002aaaaaaab000)


Running the executable on two nodes, with four tasks per node, can be done like this:
<syntaxhighlight lang="bash">
<source lang='bash'>
#!/bin/bash
srun --nodes=2 --ntasks-per-node=4 --partition=ABGC --mpi=openmpi ./test_hello_world
#SBATCH --job-name=mpi-test
</source>
#SBATCH --nodes=2
#SBATCH --ntasks-per-node=4
#SBATCH --time=00:30:00
#SBATCH --output=mpi-%j.out


This will result in the following output:
module purge
  Hello MPI! Process 4 of 8 on node011
module load 2024
  Hello MPI! Process 1 of 8 on node010
module load gcc openmpi/gcc slurm
  Hello MPI! Process 7 of 8 on node011
  Hello MPI! Process 6 of 8 on node011
  Hello MPI! Process 5 of 8 on node011
  Hello MPI! Process 2 of 8 on node010
  Hello MPI! Process 0 of 8 on node010
  Hello MPI! Process 3 of 8 on node010
 
== A mvapich2 sbatch example ==
A mpi job using mvapich2 on 32 cores, using the normal compute nodes and the fast infiniband interconnect for RDMA traffic.
<source lang='bash'>
$ module load mvapich2/gcc
$ vim batch.sh
#!/bin/sh
#SBATCH --comment=projectx
#SBATCH --time=0
#SBATCH  -n 32
#SBATCH --constraint=normalmem
#SBATCH --output=output_%j.txt
#SBATCH --error=error_output_%j.txt
#SBATCH --job-name=MPItest
#SBATCH --partition=ABGC_Production
#SBATCH --mail-type=ALL
#SBATCH --mail-user=user@wur.nl
echo "Starting at `date`"
echo "Running on hosts: $SLURM_NODELIST"
echo "Running on $SLURM_NNODES nodes."
echo "Running on $SLURM_NPROCS processors."
echo "Current working directory is `pwd`"
# echo "Env var MPIR_CVAR_NEMESIS_TCP_NETWORK_IFACE is $MPIR_CVAR_NEMESIS_TCP_NETWORK_IFACE"
# export MPIR_CVAR_NEMESIS_TCP_NETWORK_IFACE=ib0
 
mpirun -iface ib0 -np 32 ./tmf_par.out -NX 480 -NY 240 -alpha  11 -chi 1.3 -psi_b 5e-2  -beta  0.0 -zeta 3.5 -kT 0.10


echo "Program finished with exit code $? at: `date`"
srun ./hello_mpi
</syntaxhighlight>


$ sbatch batch.sh
<!-- TODO: confirm the recommended MPI library and module names for the current cluster, any required `srun --mpi=...` plugin flag, and whether specific interconnect/fabric tuning is needed. The previous page documented an mvapich2 + InfiniBand (ib0) setup from the Breed4Food cluster, which may no longer match Anunna's hardware. -->


</source>
== See also ==
* [[Performance Optimization/Multiple CPUs]]
* [[Performance Optimization/Multiple nodes (arrayjobs)]]
* [[Environment Modules]]
* [[Batch Jobs]]
* [[Scheduler Overview (Slurm)]]
* [[Cluster Architecture Overview]]

Latest revision as of 13:03, 18 June 2026

MPI (the Message Passing Interface) lets a single program run across many cores, and even many nodes, at once by passing messages between its processes. Use it for software written to scale beyond one node. To use several cores within a single node, see Performance Optimization/Multiple CPUs; for many independent tasks, see Performance Optimization/Multiple nodes (arrayjobs).

Compiling an MPI program

Load a software bucket, then a compiler and an MPI library, through the module system. A bucket has to be loaded before its modules are visible (see Environment Modules). To avoid library conflicts it is safest to start from a clean environment — note that purging also removes the slurm module, so reload it:

module purge
module load 2024
module load gcc openmpi/gcc slurm

As a simple example, here is the classic MPI "Hello World" in C:

#include <stdio.h>
#include <mpi.h>
int main(int argc, char **argv) {
    int size, rank, namelen;
    char processor_name[MPI_MAX_PROCESSOR_NAME];
    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Get_processor_name(processor_name, &namelen);
    printf("Hello MPI! Process %d of %d on %s\n", rank, size, processor_name);
    MPI_Finalize();
}

Compile it with the MPI compiler wrapper:

mpicc hello_mpi.c -o hello_mpi

Running an MPI program

Launch the MPI processes with srun, which spreads them across the nodes your job was allocated. For example, two nodes with four tasks each:

srun --nodes=2 --ntasks-per-node=4 ./hello_mpi

In a batch job, request the nodes and tasks with #SBATCH and launch with srun:

#!/bin/bash
#SBATCH --job-name=mpi-test
#SBATCH --nodes=2
#SBATCH --ntasks-per-node=4
#SBATCH --time=00:30:00
#SBATCH --output=mpi-%j.out

module purge
module load 2024
module load gcc openmpi/gcc slurm

srun ./hello_mpi


See also