Merge from mainline
[official-gcc.git] / libgomp / testsuite / libgomp.c / appendix-a / a.18.1.c
blob388763e59d162a77cf5fd3e40996adc25aaea37a
1 /* { dg-do run } */
3 #include <omp.h>
4 #include <stdio.h>
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];
13 int
14 fn1 (int i)
16 return i * 2;
19 int
20 fn2 (int a, int b)
22 return a + b;
25 int
26 main ()
28 int i, iam, neighbor;
29 omp_set_num_threads (NUMBER_OF_THREADS);
30 #pragma omp parallel private(iam,neighbor) shared(work,synch)
32 iam = omp_get_thread_num ();
33 synch[iam] = 0;
34 #pragma omp barrier
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)
42 synch[iam] = 1;
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)
63 abort ();
66 return 0;