2018-01-22 Sebastian Perta <sebastian.perta@renesas.com>
[official-gcc.git] / libgomp / testsuite / libgomp.c++ / ctor-13.C
blob8c7a09f315d9c1ef9ac165eed08242e87a66b69c
1 // { dg-do run }
3 #include <omp.h>
4 #include <assert.h>
6 struct B
8   static int ic, dc, xc, ac, cc;
10   B();
11   B(const B &);
12   ~B();
13   B& operator=(const B &);
14   void doit();
15   static void clear();
18 int B::ic;
19 int B::dc;
20 int B::xc;
21 int B::cc;
22 int B::ac;
24 B::B()
26   #pragma omp atomic
27     ic++;
30 B::~B()
32   #pragma omp atomic
33     dc++;
36 B::B(const B &)
38   #pragma omp atomic
39     cc++;
42 B& B::operator=(const B &)
44   #pragma omp atomic
45     ac++;
46   return *this;
49 void B::doit()
51   #pragma omp atomic
52     xc++;
55 void B::clear()
57   ic = 0;
58   dc = 0;
59   cc = 0;
60   ac = 0;
61   xc = 0;
64 static int n;
66 void f1(B &a)
68   B b;
69   B &c = b;
70   #pragma omp parallel default(none) private(a, c) shared (n)
71     {
72       #pragma omp master
73         n = omp_get_num_threads ();
74       a.doit();
75       c.doit();
76     }
79 void f2(B &a)
81   B b;
82   B &c = b;
83   #pragma omp parallel default(none) firstprivate(a, c) shared(n)
84     {
85       #pragma omp master
86         n = omp_get_num_threads ();
87       a.doit();
88       c.doit();
89     }
92 void f3(B &a)
94   B b;
95   B &c = b;
96   #pragma omp parallel default(none) shared(n, a, c)
97     {
98       #pragma omp master
99         n = omp_get_num_threads ();
100       #pragma omp for lastprivate (a, c)
101       for (int i = 0; i < omp_get_num_threads (); i++)
102         {
103           a.doit();
104           c.doit();
105         }
106     }
109 void f4()
111   B b;
112   B &c = b;
113   #pragma omp parallel default(none) private (c) shared (n)
114     {
115       B d;
116       B &e = d;
117       #pragma omp single copyprivate (c, e)
118       {
119         c.doit();
120         e.doit();
121       }
122       c.doit();
123       e.doit();
124     }
127 void f5(B (&a)[2])
129   B b[2];
130   B (&c)[2] = b;
131   #pragma omp parallel default(none) private(a, c) shared (n)
132     {
133       #pragma omp master
134         n = omp_get_num_threads ();
135       a[0].doit();
136       a[1].doit();
137       c[0].doit();
138       c[1].doit();
139     }
142 void f6(B (&a)[2])
144   B b[2];
145   B (&c)[2] = b;
146   #pragma omp parallel default(none) firstprivate(a, c) shared (n)
147     {
148       #pragma omp master
149         n = omp_get_num_threads ();
150       a[0].doit();
151       a[1].doit();
152       c[0].doit();
153       c[1].doit();
154     }
157 void f7(B (&a)[2])
159   B b[2];
160   B (&c)[2] = b;
161   #pragma omp parallel default(none) shared(n, a, c)
162     {
163       #pragma omp master
164         n = omp_get_num_threads ();
165       #pragma omp for lastprivate (a, c)
166       for (int i = 0; i < omp_get_num_threads (); i++)
167         {
168           a[0].doit();
169           a[1].doit();
170           c[0].doit();
171           c[1].doit();
172         }
173     }
176 void f8()
178   B b[2];
179   B (&c)[2] = b;
180   #pragma omp parallel default(none) private (c) shared (n)
181     {
182       B d[2];
183       B (&e)[2] = d;
184       #pragma omp single copyprivate (c, e)
185       {
186         c[0].doit();
187         c[1].doit();
188         e[0].doit();
189         e[1].doit();
190       }
191       c[0].doit();
192       c[1].doit();
193       e[0].doit();
194       e[1].doit();
195     }
198 int main()
200   {
201     B a;
202     f1(a);
203   }
204   assert (B::xc == 2*n && B::ic == 2*n+2 && B::dc == 2*n+2 && B::ac == 0 && B::cc == 0);
205   B::clear();
206   {
207     B a;
208     f2(a);
209   }
210   assert (B::xc == 2*n && B::ic == 2 && B::dc == 2*n+2 && B::ac == 0 && B::cc == 2*n);
211   B::clear();
212   {
213     B a;
214     f3(a);
215   }
216   assert (B::xc == 2*n && B::ic == 2*n+2 && B::dc == 2*n+2 && B::ac == 2 && B::cc == 0);
217   B::clear();
218   f4();
219   assert (B::xc == 2*n+2 && B::ic == 2*n+1 && B::dc == 2*n+1 && B::ac == 2*n-2 && B::cc == 0);
220   B::clear();
221   {
222     B a[2];
223     f5(a);
224   }
225   assert (B::xc == 4*n && B::ic == 4*n+4 && B::dc == 4*n+4 && B::ac == 0 && B::cc == 0);
226   B::clear();
227   {
228     B a[2];
229     f6(a);
230   }
231   assert (B::xc == 4*n && B::ic == 4 && B::dc == 4*n+4 && B::ac == 0 && B::cc == 4*n);
232   B::clear();
233   {
234     B a[2];
235     f7(a);
236   }
237   assert (B::xc == 4*n && B::ic == 4*n+4 && B::dc == 4*n+4 && B::ac == 4 && B::cc == 0);
238   B::clear();
239   f8();
240   assert (B::xc == 4*n+4 && B::ic == 4*n+2 && B::dc == 4*n+2 && B::ac == 4*n-4 && B::cc == 0);
241   return 0;