
Write First Program in MPI Programming with C++
In the previous two tutorial I teach to Setup WSL and Setup Runtime Environment. In this tutorial I will teach you how to write the first program in MPI Programming with C++.
As, you are going through MPI Programming so, it is known that you have been through C/C++ programming. So, we don’t need to discuss about the basic things of C/C++ here. But if you have any problem you can comment here.
First MPI Program in C++
Firstly, we will write a MPI program to print Hello from the processes. The following code is in our Hello.cpp
program.
#include<bits/stdc++.h> #include<mpi.h> using namespace std; int main(){ const int N = 100; char greeting[N]; int NoOfProcess, ProcessNo; MPI_Init(NULL, NULL); MPI_Comm_size(MPI_COMM_WORLD, &NoOfProcess); MPI_Comm_rank(MPI_COMM_WORLD, &ProcessNo); if (ProcessNo != 0){ sprintf(greeting, "Hello from process %d of %d!\n", ProcessNo, NoOfProcess); MPI_Send(greeting, strlen(greeting)+1, MPI_CHAR, 0, 0, MPI_COMM_WORLD); } else{ printf("Hello from process %d of %d!\n", ProcessNo, NoOfProcess); for (int q = 1; q < NoOfProcess; q++){ MPI_Recv(greeting, N, MPI_CHAR, q, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); cout<<greeting<<endl; } } MPI_Finalize(); return 0; }
Compile & Run First MPI Program
To compile the program use following command:
$ mpic++ Hello.cpp -o Hello.o
To run the program use following command:
$ mpirun -np 2 Hello.o
Hope your program has successfully executed. If there is no error. The output will be like:
Hello from process 0 of 2!
Hello from process 1 of 2!

Explanation of First MPI Program Line by Line
MPI has a wide variation of functions. Each of those functions has huge prototypes. So, it is a bit hard to write MPI program if you don’t understand well. We won’t discuss the basics of C++ here. Just follow the main code.
Variable Declaration
const int N = 100; char greeting[N]; int NoOfProcess, ProcessNo;
Those are required variables. You can declare these variables with any name. But ensure you are using correct name at correct place.
Here,
N – Maximum length of string
greeting – String to print
NoOfProcess – No of process or core on your computer
ProcessNo – Index/Rank of the current process among all process/core on your computer
MPI Basic
To write a MPI program you must initialize MPI at start of program and must finalize MPI at ending of program.
MPI_Init(NULL, NULL); ............. ............. MPI_Finalize();
Above two lines initialize and finalize MPI respectively.
Accessing Process Information
Following two lines access the total number of process and index/rank of current process respectively.
MPI_Comm_size(MPI_COMM_WORLD, &NoOfProcess); MPI_Comm_rank(MPI_COMM_WORLD, &ProcessNo);
Processing Non-Zero’th Processes
The if condition below process all non-zero’th process initially.
if (ProcessNo != 0){ sprintf(greeting, "Hello from process %d of %d!\n", ProcessNo, NoOfProcess); MPI_Send(greeting, strlen(greeting)+1, MPI_CHAR, 0, 0, MPI_COMM_WORLD); }
If it is not 0’th process (not first process or main core) then MPI will instruct to assign a string to predeclared variable greeting
.
sprintf in C/C++
sprintf is a C/C++ function which writes a value to a string. sprintf function has 2 default parameters and optional parameters like printf. Default parameters are:
- String Variable
- String Content
sprintf(greeting, "Hello from process %d of %d!\n", ProcessNo, NoOfProcess);
In the above line parameters are defined as follow:
greeting – String variable to which we will write string
“Hello from process %d of %d!\n” – The string to write
ProcessNo – A variable for first %d
NoOfProcess – A variable for second %d
MPI_Send
MPI_Send sends the processor data to the main core which need to receive in main core or 0’th process.
MPI_Send(greeting, strlen(greeting)+1, MPI_CHAR, 0, 0, MPI_COMM_WORLD);
The standard structure of MPI_Send is as follow:
MPI_Send(
data,
data size,
datatype,
destination process no,
tag,
communicator);
Processing Central (0’th) Process
We have written the codes for 0’th process or central process in the else condition. The else condition in our program:
else{ printf("Hello from process %d of %d!\n", ProcessNo, NoOfProcess); for (int q = 1; q < NoOfProcess; q++){ MPI_Recv(greeting, N, MPI_CHAR, q, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); cout<<greeting<<endl; } }
Here the first line just print a Hello on behalf of the central processor own.
In the loop we process the values received from other processes.
In the loop there are two lines. One is MPI_Recv and another is cout to print the message received with MPI_Recv.
MPI_Recv
MPI_Send receives the processor data sent from other processors to the main core or 0’th process.
MPI_Recv(greeting, N, MPI_CHAR, q, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
The standard structure of MPI_Recv is as follow:
MPI_Recv(
data,
data size,
datatype,
source process no,
tag,
communicator,
status);
As, MPI_Recv receives the data sent from MPI_Send so the prototypes are almost same. There are just two difference.
- Source/Destination: While send you need to use destination process no. That is usually 0. While receive you need to use source process no. That is usually a integer greater than 0 depending on core or process index of your computer.
- Status: While sending from MPI you don’t need to use a status flag. But, in case of receiving you have to use status to check whether it is received successfully or what.
Note that, tag is an identity number for your communication and communicator is a point through which you communicate among processes.
In any MPI program, main core does his job and show output as usual. And receives the task results assigned to others. While other perform their tasks and send the result to the main core/process.
Hope you have understood the First Program well.