MPI C

The Message Passing Interface (MPI)

The MPI standard is created and maintained by the MPI Forum, an open group consisting of parallel computing experts from both industry and academia. MPI defines an API that is used for a specific type of portable, high-performance inter-process communication (IPC): message passing. Specifically, the MPI document describes the reliable transfer of discrete, typed messages between MPI processes. Although the definition of an “MPI process” is subject to interpretation on a given platform, it usually corresponds to the operating system’s concept of a process (e.g., a POSIX process). MPI is specifically intended to be implemented as middle-ware, meaning that upper-level applications call MPI functions to perform message passing.

Slurm Script

 

#!/bin/bash

#SBATCH -J c_codejob # Job name
#SBATCH -o c_code.%j.out # Name of stdout output file (%j expands to jobId)
#SBATCH -p CUIQue # Queue name
#SBATCH -N 8 # Total number of nodes requested
#SBATCH -n 128 # Total number of mpi tasks requested
#SBATCH -t 01:30:00 # Run time (hh:mm:ss) – 1.5 hours

# Launch MPI-based executable
mpirun hello.out

hello.c Code

#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
int num_procs, num_local;
char mach_name[MPI_MAX_PROCESSOR_NAME];
int mach_len;

MPI_Init (&argc,&argv);
MPI_Comm_size (MPI_COMM_WORLD, &num_procs);
MPI_Comm_rank (MPI_COMM_WORLD, &num_local);
MPI_Get_processor_name(mach_name,&mach_len);

MPI_Barrier(MPI_COMM_WORLD);

if(num_local == 0)
printf("\n Hello, world (%i procs total)\n",num_procs);

MPI_Barrier(MPI_COMM_WORLD);

printf(" --> Process # %3i of %3i is alive. -> %s\n",
num_local,num_procs,mach_name);

MPI_Finalize();
return 0;
}

Compilation

The GNU wrapper compiler “mpicc” can be used to compile your C code.

mpicc -O3 hello.c -o hello.out

 

Submit the job to the scheduler as

sbatch CCodeScript.slurm

For managing your “job” , refer to this guide.

For more on OPENMPI, refer to their official documentation
https://www.open-mpi.org/doc/