NERSC logo National Energy Research Scientific Computing Center
  A DOE Office of Science User Facility
  at Lawrence Berkeley National Laboratory
 

Initialization

To use the MPI library you must include header files which contain definitions and declarations that are needed by the MPI library routines. In Fortran 90 you can use the mpi module. The following line must appear at the top of any source code file that will make an MPI call.

Fortran

 INCLUDE 'mpif.h'
Fortran 90 codes should use instead:
 USE mpi 

C/C++:

 #include "mpi.h"

MPI_Init

The first MPI call must be MPI_INIT, which initializes the message passing routines, for example:

Fortran:

INTEGER:: ierr

CALL MPI_INIT(ierr) 

where ierr is an integer which holds an error code when the call returns.

NOTE: The value of ierr is really of little use since, by default, MPI aborts the program when it encounters an error. However, ierr must be included with MPI calls anyway.

C

int MPI_Init( int *argc, char ***argv)

where argc and argv are the arguments passed to main. MPI does not use these arguments in any way, however, and in MPI-2 implementations, NULL may be passed instead.

MPI_Finalize

When you are finished with the message passing routines, you must close out the MPI routines. The command for doing this finalization is

Fortran

       CALL MPI_FINALIZE(ierr)

C

int MPI_Finalize(void)

Inquiry routines

Two other MPI calls are usually made soon after initialization. They are

MPI_COMM_SIZE()

MPI_COMM_SIZE is used to find the number of tasks in a specified MPI communicator. In MPI, you can divide your total number of tasks into groups, called communicators. In this tutorial we will only refer to the communicator that includes all MPI processes: MPI_COMM_WORLD.

The format for MPI_COMM_SIZE is as follows:

Fortran

INTEGER:: totTasks, ierr

CALL MPI_COMM_SIZE( MPI_COMM_WORLD, totTasks, ierr )

C

int MPI_Comm_size( MPI_COMM_WORLD, int *totTasks )

where the total number of tasks in MPI_COMM_WORLD is returned in the variable totTasks and ierr returns an error code.

MPI_COMM_RANK()

MPI_COMM_RANK is used to find the rank (the name or identifier) of the tasks running the code. Each task in a communicator is assigned an identifying number from 0 to totTasks-1. The format for the call is as follows:

Fortran

INTEGER:: task_id, ierr

CALL MPI_COMM_RANK( MPI_COMM_WORLD, task_id, ierr )

C

int MPI_Comm_rank( MPI_COMM_WORLD, int *task_id )

where task_id is an integer that identifies the task.

Example

This example, hello.f90, available in $EXAMPLES/mpi/intro/hello demonstrates these calls.

If we compile (you can type "make" to compile if you are using the example code) and run this code on franklin, we get

franklin% ftn -o hello hello.f90
franklin% qsub -I -lmppwidth=4
franklin% cd $PBS_O_WORKDIR
franklin% aprun -n 4 ./hello 
 task_no is  3  of total  4  tasks
 task_no is  1  of total  4  tasks
 task_no is  0  of total  4  tasks
 task_no is  2  of total  4  tasks

LBNL Home
Page last modified: Fri, 07 Mar 2008 00:10:40 GMT
Page URL: http://www.nersc.gov/nusers/help/tutorials/mpi/intro/init.php
Web contact: webmaster@nersc.gov
Computing questions: consult@nersc.gov

Privacy and Security Notice
DOE Office of Science