6 extern void abort (void);
8 #define NUMBER_OF_THREADS 4
10 int synch
[NUMBER_OF_THREADS
];
11 int work
[NUMBER_OF_THREADS
];
12 int result
[NUMBER_OF_THREADS
];
29 omp_set_num_threads (NUMBER_OF_THREADS
);
30 #pragma omp parallel private(iam,neighbor) shared(work,synch)
32 iam
= omp_get_thread_num ();
35 /*Do computation into my portion of work array */
36 work
[iam
] = fn1 (iam
);
37 /* Announce that I am done with my work. The first flush
38 * ensures that my work is made visible before synch.
39 * The second flush ensures that synch is made visible.
41 #pragma omp flush(work,synch)
43 #pragma omp flush(synch)
44 /* Wait for neighbor. The first flush ensures that synch is read
45 * from memory, rather than from the temporary view of memory.
46 * The second flush ensures that work is read from memory, and
47 * is done so after the while loop exits.
49 neighbor
= (iam
> 0 ? iam
: omp_get_num_threads ()) - 1;
50 while (synch
[neighbor
] == 0)
52 #pragma omp flush(synch)
54 #pragma omp flush(work,synch)
55 /* Read neighbor's values of work array */
56 result
[iam
] = fn2 (work
[neighbor
], work
[iam
]);
58 /* output result here */
59 for (i
= 0; i
< NUMBER_OF_THREADS
; i
++)
61 neighbor
= (i
> 0 ? i
: NUMBER_OF_THREADS
) - 1;
62 if (result
[i
] != i
* 2 + neighbor
* 2)