Skip to content

profkris/OpenMPI

Repository files navigation

OpenMPI Program Guide

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).


Requirements

  • OpenMPI installed on your system
  • mpicc and mpirun available in your terminal/command prompt
  • C compiler (e.g. GCC)

Check installation with:

mpicc --version
mpirun --version

How to Compile an MPI Program

Use mpicc to compile .c files:

mpicc filename.c -o outputname

Example:

mpicc hello_mpi.c -o hello

How to Run the Program

Use mpirun (or mpiexec) to launch the program with multiple processes:

mpirun -np <num_processes> ./outputname

Example:

mpirun -np 4 ./hello

This runs the program using 4 processes.


Example MPI Program

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

Notes

  • MPI is used for distributed memory parallelism (multiple processes).
  • mpicc is a wrapper around gcc with MPI support.
  • mpirun launches multiple instances of the program.
  • For multi-node execution, you'll need a hostfile and passwordless SSH between nodes.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages