Some basic stuff using MPI

Here you can find some basic programs using MPI. The goal here is to show you how to use some communication tools providd by MPI with simple programs.


This is the most simple program you can find. It envolves two processes. As in any MPI program, the processes first get their rank: here you have process #0 and process #1. A process sends a message only when it has received one from the other process. The initiator process is #0. Here we use blocking communications. The code is here.

You can calculate the communication time by using time measurements with MPI_Wtime() and devide the obtained time by the number of exchanges.

Token rink

Quite similar to the previous example, token ring consists of a sent of processes that send and receive a token. Each process receives a message (the token) from the previous node and sends ti to the following node. A node can send a message only once it has received it, like the ping-pong. Of course, here you can use more than 2 processes, otherwise it would be a ping-pong again. Here again, we use blocking communications. The code is here.


The goal here is to determine the maximum value in a table. Then we split the table into parts which are distributed among the processes. Each process determines its local maximum, and the global maximum of all the local maxima is determined. There are two ways to determine this global maximum:

Other materials

The Argonne National Laboratory provides some documents and examples to learn MPI.

Still from ANL, several examples here.

Another page of very short examples that illustrates global communications such as MPI_Scatter and MPI_Gather ant MPI_Type_struct buildings here.

Back to the lab's page
Camille Coti
Last modified: Tue Nov 21 14:48:45 CET 2006