Skip to main content

Implementing a Distributed Program in MPI

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

#define MASTER 0

int main (int argc, char *argv[]) {
int numtasks, rank, len;
char hostname[MPI_MAX_PROCESSOR_NAME];

MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Get_processor_name(hostname, &len);
if (rank == MASTER)
printf("MASTER: Number of MPI tasks is: %d\n",numtasks);
else
printf("WORKER: Rank: %d\n",rank);

MPI_Finalize();
}

A communicator (MPI_Comm) represents a group of processes that communicate with each other. MPI_COMM_WORLD represents the default communicator, to which all processes belong.

Functions:

  • MPI_Init - initializes the MPI program, creating the context in which processes run. Command-line arguments are passed to the context for process execution.
  • MPI_Comm_size - a function that determines the number of processes (numtasks) running within the communicator (usually MPI_COMM_WORLD).
  • MPI_Comm_rank - a function that determines the identifier (rank) of the current process within the communicator.
  • MPI_Get_processor_name - determines the processor's name.
  • MPI_Finalize - triggers the termination of the MPI program.

During data exchange between processes, it is always necessary to specify their type. In MPI, the MPI_Datatype enum is used, which maps to data types in C/C++, as shown in the table below:

MPI_DatatypeEquivalent in C/C++
MPI_INTint
MPI_LONGlong
MPI_CHARchar
MPI_FLOATfloat
MPI_DOUBLEdouble

We can create communicators from another communicator using the MPI_Comm_split function, which divides a communicator into multiple smaller communicators. The function's signature is as follows:

int MPI_Comm_split(MPI_Comm comm, int color, int key, MPI_Comm * newcomm)

Where:

  • comm - the communicator being split
  • color - an identifier of the new communicator, to which a process belongs (usually rank_old_process / size_new_communicator)
  • key - a number used to determine the process rank in the new communicator. Ranks are assigned in ascending order of the keys. For example, if we set the key to old_rank, the processes will have the same order in the new communicator, whereas if we set it to -rank, they will be in reverse order.
  • newcomm - The newly created communicator (the one to which the current process belongs). A total of as many communicators as there are distinct colors are created.

Aveți mai jos o imagine care ilustrează cum funcționează MPI_Comm_split, atunci când culoarea este old_rank / dimensiune_comunicator_nou', iar cheia este 'vechiul_rank':

img
note

In the new communicator, processes will always have ranks ranging from 0 to new_communicator_size - 1.