Biomedical Engineering Reference
In-Depth Information
1: #include <stdio.h>
2: #include <mpi.h>
3:
4: char *msg = "May everyone be happy. May everyone be h$$
5:
6: #define TOKEN_TAG 1
7:
8: int main(int argc, char *argv[])
9: {
10: int size, rank;
11: int token = 0;
12: MPI_Status stat;
13:
14: MPI_Init(&argc, &argv);
15: MPI_Comm_size(MPI_COMM_WORLD, &size);
16: MPI_Comm_rank(MPI_COMM_WORLD, &rank);
17:
18: if (rank != 0)
19: MPI_Recv(&token, 1, MPI_INTEGER, rank - 1, \
20: TOKEN_TAG, MPI_COMM_WORLD, &stat);
21:
22: printf("Hello World. There are %d MPI processes. \
23: My rank is %d. My message is: %s\n", size, rank, msg);
24:
25: if (rank <= size - 2)
26: MPI_Send(&token, 1, MPI_INTEGER, rank + 1,
27: TOKEN_TAG, MPI_COMM_WORLD);
28:
29: MPI_Finalize();
30: return 0;
31: }
Figure 5.11 In the program mpi-hello4.c, token passing is used to stagger the output.
The fourth argument to MPI Send is the rank of the destination process, to
which the message is to be sent. In the example above, every process passes the
token to the process with one greater rank. Thus, this argument is set to rank + 1 .
The fifth argument is called the tag in MPI terminology. It is an integer which
identifies the ''channel'' on which the data is being sent. Between any pair of
communicating processes, the data may be sent on multiple channels. The receiving
process can choose to receive message on a particular channel identified by the tag.
This functionality is very useful if multiple types of messages are exchanged. In
the example above, if the processes were also communicating data (say, in floating
point) in addition to the token, then a separate tag could have been used for
sending the additional data. This enhances modularity and simplyfies the task of
bookkeeping of the messages. For example, libraries built on top of MPI may use
a disjoint set of tags for exchanging messages. This will ensure that the messages
sent by the libraries do not interfere with other messages of the programs.
Search WWH ::




Custom Search