Debian Codegraph @

libgomp/testsuite/libgomp.c++/task-reduction-9.C

raw
1 #include <omp.h>
2 #include <stdlib.h>
3
4 struct S { S (); S (long int, long int); ~S (); static int cnt1, cnt2, cnt3; long int s, t; };
5
6 int S::cnt1;
7 int S::cnt2;
8 int S::cnt3;
9
10 S::S ()
11 {
12 #pragma omp atomic
13 cnt1++;
14 }
15
16 S::S (long int x, long int y) : s (x), t (y)
17 {
18 #pragma omp atomic update
19 ++cnt2;
20 }
21
22 S::~S ()
23 {
24 #pragma omp atomic
25 cnt3 = cnt3 + 1;
26 if (t < 3 || t > 9 || (t & 1) == 0)
27 abort ();
28 }
29
30 void
31 bar (S *p, S *o)
32 {
33 p->s = 1;
34 if (o->t != 5)
35 abort ();
36 p->t = 9;
37 }
38
39 static inline void
40 baz (S *o, S *i)
41 {
42 if (o->t != 5 || i->t != 9)
43 abort ();
44 o->s *= i->s;
45 }
46
47 #pragma omp declare reduction (+: S : omp_out.s += omp_in.s) initializer (omp_priv (0, 3))
48 #pragma omp declare reduction (*: S : baz (&omp_out, &omp_in)) initializer (bar (&omp_priv, &omp_orig))
49
50 S a = { 0, 7 };
51 S b (1, 5);
52
53 void
54 foo ()
55 {
56 int i;
57 for (i = 0; i < 2; i++)
58 #pragma omp task in_reduction (*: b) in_reduction (+: a)
59 {
60 a.s += 7;
61 b.s *= 2;
62 if ((a.t != 7 && a.t != 3) || (b.t != 5 && b.t != 9))
63 abort ();
64 }
65 }
66
67 void
68 test ()
69 {
70 S c = { 0, 7 };
71 int t;
72 #pragma omp parallel num_threads (1)
73 {
74 S d (1, 5);
75 int r = 0;
76 #pragma omp parallel reduction (task, +: a, c) reduction (task, *: b, d) \
77 reduction (+: r)
78 {
79 int i;
80 #pragma omp master
81 t = omp_get_num_threads ();
82 r++;
83 a.s += 3;
84 c.s += 4;
85 #pragma omp for
86 for (i = 0; i < 4; i++)
87 #pragma omp task in_reduction (*: b, d) in_reduction (+: a, c)
88 {
89 int j;
90 a.s += 7;
91 b.s *= 2;
92 for (j = 0; j < 2; j++)
93 #pragma omp task in_reduction (+: a) in_reduction (*: b) \
94 in_reduction (+: c) in_reduction (*: d)
95 {
96 a.s += 7;
97 b.s *= 2;
98 c.s += 9;
99 d.s *= 3;
100 foo ();
101 if ((a.t != 7 && a.t != 3) || (b.t != 5 && b.t != 9)
102 || (c.t != 7 && c.t != 3) || (d.t != 5 && d.t != 9))
103 abort ();
104 }
105 c.s += 9;
106 d.s *= 3;
107 if ((a.t != 7 && a.t != 3) || (b.t != 5 && b.t != 9)
108 || (c.t != 7 && c.t != 3) || (d.t != 5 && d.t != 9))
109 abort ();
110 }
111 }
112 #define THREEP4 (3L * 3L * 3L * 3L)
113 if (d.s != (THREEP4 * THREEP4 * THREEP4) || d.t != 5 || r != t)
114 abort ();
115 }
116 if (a.s != 28 * 7 + 3 * t || a.t != 7 || b.s != (1L << 28) || b.t != 5
117 || c.s != 12 * 9 + 4 * t || c.t != 7)
118 abort ();
119 }
120
121 int
122 main ()
123 {
124 int c1 = S::cnt1, c2 = S::cnt2, c3 = S::cnt3;
125 test ();
126 if (S::cnt1 + S::cnt2 - c1 - c2 != S::cnt3 - c3)
127 abort ();
128 }