MPI (Message Passing Interface) is a library that allows you to write parallel programs in C or Fortran77. The library uses commonly available operating system services to create parallel processes and exchange information among these processes.
This method starts a communication channel with a persistent request handle. The request handle is activated when the MPI_Start
function returns.
int MPI_Start(MPI_Request *request)
request
is a communication request handle.The function returns an error if it is unsuccessful. By default, the error aborts the MPI job.
In case of success, it returns MPI_SUCCESS
- the value returned upon successful termination of any MPI routine.
The following example illustrates how we can use MPI_Start
with two processes where one is a sender and the other is a receiver:
#include <stdio.h>#include <stdlib.h>#include <mpi.h>int main(int argc, char* argv[]){MPI_Init(&argc, &argv);// Get the number of processes and check only 2 processes are usedint size;MPI_Comm_size(MPI_COMM_WORLD, &size);if(size != 2){printf("This application is meant to be run with 2 processes.\n");MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);}// Get my rank and do the corresponding jobenum role_ranks { SENDER, RECEIVER };int my_rank;MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);switch(my_rank){case SENDER:{int data_sent;MPI_Request request;// Prepare the send request handleMPI_Send_init(&data_sent, 1, MPI_INT, RECEIVER, 0, MPI_COMM_WORLD, &request);data_sent = 12345;// Launch the sendMPI_Start(&request);printf("MPI process %d sends value %d.\n", my_rank, buffer_sent);break;}case RECEIVER:{int received;MPI_Recv(&received, 1, MPI_INT, SENDER, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);printf("MPI process %d received value %d.\n", my_rank, received);break;}}MPI_Finalize();return EXIT_SUCCESS;}
In the program above, we perform the following steps:
mpi.h
library to use MPI functions.MPI_Comm_size
function and check that the application is run with 2 processes, i.e., a sender and a receiver (line 9).MPI_Send_Init
function (line 26), and launch the send using the MPI_Start
function (line 29).MPI_Recv
function.Image credits:
Icons made by Pixel perfect from www.flaticon.com
Free Resources