1 /*
2 * Copyright (c) 2014 Mellanox Technologies, Inc.
3 * All rights reserved.
4 * $COPYRIGHT$
5 *
6 * Additional copyrights may follow
7 *
8 * $HEADER$
9 */
10
11 #include <shmem.h>
12 #include <stdio.h>
13
14 #if !defined(OSHMEM_SPEC_VERSION) || OSHMEM_SPEC_VERSION < 10200
15 #error This application uses API 1.2 and up
16 #endif
17
18 int main (int argc, char * argv[])
19 {
20 static int rbuf = -1;
21 int proc, nproc, next;
22 int message = 10;
23
24 shmem_init();
25 nproc = shmem_n_pes();
26 proc = shmem_my_pe();
27
28 /* Calculate the PE number of the next process in the ring. Use the
29 modulus operator so that the last process "wraps around" to PE 0. */
30
31 next = (proc + 1) % nproc;
32
33 if(proc == 0)
34 {
35 printf("Process 0 puts message %d to %d (%d processes in ring)\n", message, next, nproc);
36 shmem_int_put(&rbuf, &message, 1, next);
37 }
38
39 /* Pass the message around the ring. The exit mechanism works as
40 follows: the message (a positive integer) is passed around the
41 ring. Each time it passes PE 0, it is decremented. When each
42 processes receives a message containing a 0 value, it passes the
43 message on to the next process and then quits. By passing the 0
44 message first, every process gets the 0 message and can quit
45 normally. */
46
47 while(message > 0) {
48 shmem_int_wait_until(&rbuf, SHMEM_CMP_EQ, message);
49 if(proc == 0) {
50 --message;
51 printf("Process 0 decremented value: %d\n", message);
52 }
53 shmem_int_put(&rbuf, &message, 1, next);
54 if(proc != 0) {
55 --message;
56 }
57 }
58 shmem_finalize();
59
60 /* All done */
61
62 printf("Process %d exiting\n", proc);
63
64 return 0;
65 }