The MPI_COMM_WORLD constant is a "communicator", and is defined for all processes that are initialized for MPI. A communicator gives a message an identifying context to distinguish it from messages of other communicators. In many programs only a single communicator is needed but in more complicated situations and in library development, multiple contexts may be required. A communicator is analogous to a channel of a citizen's band transceiver: chatter on one channel does not interfere (cannot be heard) on any of the other channels. The MPI_COMM_WORLD constant might have just as well been named MPI_COMM_ORIGINAL because it is a pre-defined communicator assigned to the initial group of all processes.
There are two information subprograms that you will probably want to use at the beginning of all programs; and both require the world communicator. MPI_Comm_size and MPI_Comm_rank return the number of processors and a process number, respectively. In MPI jargon, the unique number assigned to a process is its rank. The world communicator ranks range from 0 to npes-1, the number of processors less one. The syntax for these two functions is:
ierr= MPI_Comm_size (icomm, npes) ierr= MPI_Comm_rank (icomm, irank)
where:
| Parameter | Description | Status |
|---|---|---|
icomm (MPI_Comm)
| communicator (use constant MPI_COMM_WORLD as predefined context for initial processes) | [IN] |
npes (int *)
|
number of processes | [OUT] |
irank (int *)
|
process number, numbered between 0 and npes-1
|
[OUT] |
ierr (int)
|
MPI error number (0 = no error) | [OUT] |
In the following code the number of processes and the rank of each process are reported. The first argument of the size and rank functions is the communicator and the second is the respective returned value. The functions return an int error value.
#include <stdio.h>
#include <mpi.h>
main(int argc, char **argv){
  int npes, irank, ierr;
  MPI_Init(&argc, &argv);
  ierr = MPI_Comm_size(MPI_COMM_WORLD, &npes);
  ierr = MPI_Comm_rank(MPI_COMM_WORLD, &irank);
  printf("%d %d = Total procs, process rank.\n",npes,irank);
  MPI_Finalize();
}
OUTPUT (4 PEs): 4 0 = Total procs, process rank. 4 1 = Total procs, process rank. 4 2 = Total procs, process rank. 4 3 = Total procs, process rank.



|
July 20, 2008 |
Office of the Vice President for Research
© Copyright 2002 The University of Texas at Austin |



