This repository contains example programs written in C using OpenMPI, a message-passing interface library used for parallel computing across processes (multi-core or multi-node).
- OpenMPI installed on your system
mpiccandmpirunavailable in your terminal/command prompt- C compiler (e.g. GCC)
Check installation with:
mpicc --version
mpirun --versionUse mpicc to compile .c files:
mpicc filename.c -o outputnamempicc hello_mpi.c -o helloUse mpirun (or mpiexec) to launch the program with multiple processes:
mpirun -np <num_processes> ./outputnamempirun -np 4 ./helloThis runs the program using 4 processes.
Create a file named hello_mpi.c and paste the following:
#include <stdio.h>
#include <mpi.h>
int main(int argc, char** argv) {
MPI_Init(&argc, &argv); // Initialize MPI
int rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank); // Get current process ID
MPI_Comm_size(MPI_COMM_WORLD, &size); // Get number of processes
printf("Hello from process %d out of %d processes
", rank, size);
MPI_Finalize(); // Clean up
return 0;
}Then compile and run it:
mpicc hello_mpi.c -o hello
mpirun -np 4 ./hello- MPI is used for distributed memory parallelism (multiple processes).
mpiccis a wrapper aroundgccwith MPI support.mpirunlaunches multiple instances of the program.- For multi-node execution, you'll need a hostfile and passwordless SSH between nodes.