- Terms
- Sample Programs
- Include Files and Function Calls
- Include Files
- Function Calls
- Running "hello"
The MPI library provides a rich collection of communication routines.
You can, however, start writing MPI code using only a few of those routines.
Before you start writing, you should understand two basic terms
that are involved in
almost all MPI function calls: communicator and rank.
A communicator is the domain of communications -- a
group of processors within which communications may take place. MPI
function calls take this as an argument to define which processors will be
involved in an operation.
A rank is a unique integer that identifes the processor within a given
communicator domain. If a processor is defined in more then one
communicator, it has a unique, different rank in each communicator.
Below are sample "hello" MPI programs shown
in both FORTRAN and C languages.
program hello
include "mpif.h"
integer rank, size, ierr
call MPI_INIT(ierr)
call MPI_COMM_SIZE(MPI_COMM_WORLD, size, ierr)
call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr)
print *, "Hello world! I'm ", rank+1," of ", size
call MPI_FINALIZE(ierr)
stop
end
This is the equivalent C version:
#include <stdio.h>
#include <mpi.h>
main(int *argc, char** argv)
{
int size, rank;
MPI_Init(argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
printf("Hello world! I'm %d of %d\n", rank+1, size);
MPI_Finalize();
}
The different pieces and function calls in the program are important to
understand.
Both mpif.h and mpi.h are MPI include files that contain declarations and type definitions for
fields used in the MPI function calls. One of these files must be included in
every MPI program. Use the appropriate include file depending on
your language:
Notice that the order of printing is random and can be different each time
you run the program.