Skip to end of metadata
Go to start of metadata

MPI Guide

DescriptionCompiling and running example MPI code
Last modified

Start an Interactive Test and Development session

[rtm25@balena-01 mpi]$ sinteractive --time=15:00
salloc: Granted job allocation 18683
srun: Job step created
[rtm25@itd-ngpu-01 mpi]

Source code

helloworld.c
// www.mpitutorial.com
// An intro MPI hello world program that uses MPI_Init, MPI_Comm_size,
// MPI_Comm_rank, MPI_Finalize, and MPI_Get_processor_name.

#include <stdio.h>
#include <mpi.h>


int main(int argc, char** argv) {
    // Initialize the MPI environment
    MPI_Init(NULL, NULL);

    // Get the number of processes
    int world_size;
    MPI_Comm_size(MPI_COMM_WORLD, &world_size);

    // Get the rank of the process
    int world_rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);

    // Get the name of the processor
    char processor_name[MPI_MAX_PROCESSOR_NAME];
    int name_len;
    MPI_Get_processor_name(processor_name, &name_len);

    // Print off a hello world message
    printf("Hello world from processor %s, rank %d"
           " out of %d processors\n",
           processor_name, world_rank, world_size);

    // Finalize the MPI environment.
    MPI_Finalize();
}

Compiling your code

[rtm25@itd-ngpu-01 mpi]$ module load intel/compiler
[rtm25@itd-ngpu-01 mpi]$ module load intel/mpi
[rtm25@itd-ngpu-01 mpi]$ mpicc helloworld.c -o helloworld.exe

Create the slurm job script

#!/bin/bash

# set the account to be used for the job
#SBATCH --account=prj-cc001

# set name of job
#SBATCH --job-name=helloworld
#SBATCH --output=helloworld.%j.o
#SBATCH --error=helloworld.%j.e

# set the number of nodes and partition
#SBATCH --nodes=2
#SBATCH --ntasks-per-node=16
#SBATCH --partition=batch-acc

# set max wallclock time
#SBATCH --time=04:00:00

# mail alert at start, end and abortion of execution
#SBATCH --mail-type=END

# send mail to this address
#SBATCH --mail-user=rtm25@bath.ac.uk

# Load dependant modules
module load intel/mpi

# run the application
mpirun -np $SLURM_NTASKS ./helloworld.exe

Submit job to the scheduler

[rtm25@itd-ngpu-01 mpi]$ sbatch jobscript.slm 
Submitted batch job 18685

Check job

[rtm25@itd-ngpu-01 mpi]$ squeue -u rtm25
     JOBID       NAME       USER    ACCOUNT  PARTITION    ST NODES  CPUS  MIN_MEMORY           START_TIME     TIME_LEFT  PRIORITY NODELIST(REASON)
     18685 helloworld      rtm25  prj-cc001  batch-acc     R     2    32         62K  2015-07-23T12:08:57       3:59:57     13537 node-as-agpu-001,node-as-ngpu-005
     18683 interactiv      rtm25       free        itd     R     1     1           0  2015-07-23T12:04:58         10:58        89 itd-ngpu-01

Job Output

[rtm25@itd-ngpu-01 mpi]$ ls
helloworld.18685.e helloworld.18685.o helloworld.c helloworld.exe jobscript.slm
[rtm25@itd-ngpu-01 mpi]$ cat helloworld.18685.o
Hello world from processor node-as-agpu-001, rank 1 out of 32 processors
Hello world from processor node-as-agpu-001, rank 2 out of 32 processors
Hello world from processor node-as-ngpu-005, rank 17 out of 32 processors
Hello world from processor node-as-agpu-001, rank 3 out of 32 processors
Hello world from processor node-as-ngpu-005, rank 21 out of 32 processors
Hello world from processor node-as-agpu-001, rank 4 out of 32 processors
Hello world from processor node-as-ngpu-005, rank 16 out of 32 processors
Hello world from processor node-as-agpu-001, rank 5 out of 32 processors
Hello world from processor node-as-ngpu-005, rank 18 out of 32 processors
Hello world from processor node-as-agpu-001, rank 6 out of 32 processors
Hello world from processor node-as-ngpu-005, rank 19 out of 32 processors
Hello world from processor node-as-agpu-001, rank 7 out of 32 processors
Hello world from processor node-as-ngpu-005, rank 20 out of 32 processors
Hello world from processor node-as-agpu-001, rank 0 out of 32 processors
Hello world from processor node-as-ngpu-005, rank 22 out of 32 processors
Hello world from processor node-as-ngpu-005, rank 23 out of 32 processors
Hello world from processor node-as-ngpu-005, rank 24 out of 32 processors
Hello world from processor node-as-ngpu-005, rank 25 out of 32 processors
Hello world from processor node-as-ngpu-005, rank 26 out of 32 processors
Hello world from processor node-as-ngpu-005, rank 27 out of 32 processors
Hello world from processor node-as-ngpu-005, rank 28 out of 32 processors
Hello world from processor node-as-ngpu-005, rank 29 out of 32 processors
Hello world from processor node-as-ngpu-005, rank 30 out of 32 processors
Hello world from processor node-as-ngpu-005, rank 31 out of 32 processors
Hello world from processor node-as-agpu-001, rank 8 out of 32 processors
Hello world from processor node-as-agpu-001, rank 9 out of 32 processors
Hello world from processor node-as-agpu-001, rank 10 out of 32 processors
Hello world from processor node-as-agpu-001, rank 11 out of 32 processors
Hello world from processor node-as-agpu-001, rank 12 out of 32 processors
Hello world from processor node-as-agpu-001, rank 13 out of 32 processors
Hello world from processor node-as-agpu-001, rank 14 out of 32 processors
Hello world from processor node-as-agpu-001, rank 15 out of 32 processors