2 * Copyright (c) 2004-2006 The Trustees of Indiana University and Indiana
3 * University Research and Technology
4 * Corporation. All rights reserved.
5 * Copyright (c) 2006 Cisco Systems, Inc. All rights reserved.
7 * Simple ring test program
14 int main(int argc, char *argv[])
16 int rank, size, next, prev, message, tag = 201;
20 MPI_Init(&argc, &argv);
21 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
22 MPI_Comm_size(MPI_COMM_WORLD, &size);
24 /* Calculate the rank of the next process in the ring. Use the
25 modulus operator so that the last process "wraps around" to
28 next = (rank + 1) % size;
29 prev = (rank + size - 1) % size;
31 /* If we are the "master" process (i.e., MPI_COMM_WORLD rank 0),
32 put the number of times to go around the ring in the
38 printf("Process 0 sending %d to %d, tag %d (%d processes in ring)\n",
39 message, next, tag, size);
40 MPI_Send(&message, 1, MPI_INT, next, tag, MPI_COMM_WORLD);
41 printf("Process 0 sent to %d\n", next);
44 /* Pass the message around the ring. The exit mechanism works as
45 follows: the message (a positive integer) is passed around the
46 ring. Each time it passes rank 0, it is decremented. When
47 each processes receives a message containing a 0 value, it
48 passes the message on to the next process and then quits. By
49 passing the 0 message first, every process gets the 0 message
50 and can quit normally. */
55 MPI_Recv(&message, 1, MPI_INT, prev, tag, MPI_COMM_WORLD,
60 printf("Process 0 decremented value: %d\n", message);
63 MPI_Send(&message, 1, MPI_INT, next, tag, MPI_COMM_WORLD);
65 printf("Process %d exiting\n", rank);
70 /* The last process does one extra send to process 0, which needs
71 to be received before the program can exit */
74 MPI_Recv(&message, 1, MPI_INT, prev, tag, MPI_COMM_WORLD,