Home

Reference

Tutorials

Applications

 

Basic MPI Routines

Making the MPI definitions accessible to your program

MPI defines a large number of system wide data structures and constants. To make your program aware of these always begin your code with:

INCLUDE 'mpif.h'

System initialization and shutdown

Before any call to the MPI library may be made the system must be initialized by:

CALL MPI_INIT(ierr)

ierr - INTEGER error code with the result of the initialization

After completing the computation a clean shutdown of the MPI system should be achieved by calling:

CALL MPI_FINALIZE(ierr)

 

Finding information about the run

In a simple MPI program a single code is run on multiple processes. Typically the code uses a 'master' process that provides overall control and 'drone' or 'slave' processes that carry out computational tasks. All MPI processes have an integer identification number (idproc) running from 0 to np-1, where np is the number of processes running. By convention idproc=0 is the master process.

To find out how many processes are active:

CALL MPI_COMM_SIZE(MPI_COMM_WORLD,np,ierr)

To find out the ID of the particular process running the code at present:

CALL MPI_COMM_RANK(MPI_COMM_WORLD,idproc,ierr)

MPI_COMM_WORLD is defined in mpif.h.

 

Basic communication

Processes communicate information by sending and receiving messages. The MPI routine that sends a message is invoked by

CALL MPI_SEND(buf, count, type, iddest, itag, MPI_COMM_WORLD, ierr)

where:

buf - a buffer holding the data to be sent

count - the number of items in the buffer

type - the type of the items in the buffer. Common predefined types include MPI_INTEGER, MPI_REAL, MPI_DOUBLE_PRECISION, MPI_COMPLEX, MPI_LOGICAL with obvious Fortran equivalents.

iddest - the ID of the destination process.

itag - an integer tag attached to the message for identification purposes

The MPI routine that receives a message is invoked by

CALL MPI_RECV(buf, count, type, idsrc, itag, MPI_COMM_WORLD, ierr)

where:

buf - a buffer to hold the received data

count - the number of items in the buffer

type - the type of the items in the buffer.

idsrc - the ID of the process from which to receive the message. A special value MPI_ANY_SOURCE allows reception from any process.

itag - an integer tag attached to the message for identification purposes. A special value MPI_ANY_TAG allows reception of a message irrespective of its tag.

Timing

Of course we'd like to know how parallel techniques are cutting down the wall clock time. Use the function

twall=MPI_WTIME()

to find the current time in seconds (twall should be DOUBLE PRECISION). You'll probably want only the master process to keep track of elapsed time.

Example

To see an example of the above basic routines go to the 2D diffusion application.