Added Compiler Support for _Cilk_spawn and _Cilk_sync for C++.
[official-gcc.git] / gcc / testsuite / g++.dg / cilk-plus / CK / lambda_spawns.cc
blob7448d1a8a30b0d1426e8e2185fdf1a511432fd94
1 /* { dg-options "-fcilkplus" } */
2 /* { dg-do run { target i?86-*-* x86_64-*-* arm*-*-* } } */
3 /* { dg-options "-std=c++11 -fcilkplus -lcilkrts" { target { i?86-*-* x86_64-*-* arm*-*-* } } } */
4 #define FIRST_NUMBER 5
5 #define SECOND_NUMBER 3
6 #define HAVE_IO 0
7 #if HAVE_IO
8 #include <stdio.h>
9 #endif
11 #include <stdlib.h>
13 int global_var;
15 void foo1(int *array, int size)
17 #if HAVE_IO
18 for (int ii = 0; ii < size; ii++)
19 printf("%2d\t", array[ii]);
20 printf("\n");
21 fflush (stdout);
22 #else
23 if (size != 2)
24 __builtin_abort ();
25 if (array[0] != FIRST_NUMBER)
26 __builtin_abort ();
27 if (array[1] != SECOND_NUMBER)
28 __builtin_abort ();
29 #endif
30 global_var++;
32 void foo1_c(const int *array, int size)
34 #if HAVE_IO
35 for (int ii = 0; ii < size; ii++)
36 printf("%2d\t", array[ii]);
37 printf("\n");
38 fflush (stdout);
39 #else
40 if (size != 2)
41 __builtin_abort ();
42 if (array[0] != FIRST_NUMBER)
43 __builtin_abort ();
44 if (array[1] != SECOND_NUMBER)
45 __builtin_abort ();
46 #endif
47 global_var++;
51 int main2 (int argc) {
52 int A[2] = {FIRST_NUMBER, SECOND_NUMBER};
53 int B[2] = {FIRST_NUMBER, SECOND_NUMBER};
54 int main_size = argc+1; /* We know argc is 1, and so 1+1 = 2. */
55 int q = 0;
57 global_var = 0;
58 auto func0 = [=](){ foo1_c(A, 2); };
59 _Cilk_spawn func0();
60 foo1 (A, 2);
61 _Cilk_sync;
62 if (global_var != 2)
63 return (++q);
65 global_var = 0;
66 auto func1 = [=](int *Aa){ foo1(Aa, 2); };
67 _Cilk_spawn func1 (A);
68 foo1 (A, 2);
69 _Cilk_sync;
70 if (global_var != 2)
71 return (++q);
73 global_var = 0;
74 auto func2 = [=](int *Aa, int size){ foo1(Aa, size); };
75 _Cilk_spawn func2 (A, 2);
76 foo1 (A, 2);
77 _Cilk_sync;
78 if (global_var != 2)
79 return (++q);
81 global_var = 0;
82 auto func3 = [=](int *Aa, int size){ int new_size = (size % 2 + 2);
83 foo1(Aa, size); };
84 _Cilk_spawn func3 (A, 2);
85 foo1 (A, 2);
86 _Cilk_sync;
87 if (global_var != 2)
88 return (++q);
90 global_var = 0;
91 auto func4 = [](int *Aa){ foo1(Aa, 2); };
92 _Cilk_spawn func4 (A);
93 foo1 (A, 2);
94 _Cilk_sync;
95 if (global_var != 2)
96 return (++q);
98 global_var = 0;
99 auto func5 = [](int *Aa, int size){ foo1(Aa, size); };
100 _Cilk_spawn func5 (A, 2);
101 foo1 (A, 2);
102 _Cilk_sync;
103 if (global_var != 2)
104 return (++q);
106 global_var = 0;
107 auto func6 = [&](int *Aa){ foo1(Aa, 2); };
108 _Cilk_spawn func6 (A);
109 foo1 (A, 2);
110 _Cilk_sync;
111 if (global_var != 2)
112 return (++q);
114 global_var = 0;
115 auto func7 = [&](int *Aa, int size){ foo1(Aa, size); };
116 _Cilk_spawn func7 (A, 2);
117 foo1 (A, 2);
118 _Cilk_sync;
119 if (global_var != 2)
120 return (++q);
122 global_var = 0;
123 auto func8 = [&](){ foo1(A, 2); };
124 _Cilk_spawn func8 ();
125 foo1 (A, 2);
126 _Cilk_sync;
127 if (global_var != 2)
128 return (++q);
130 global_var = 0;
131 /* We ignore the first param here and pass in A from the outer fn. */
132 auto func9 = [&](int *Aa, int size){ foo1(A, size); };
133 _Cilk_spawn func9 (A, 2);
134 foo1 (A, 2);
135 _Cilk_sync;
136 if (global_var != 2)
137 return (++q);
139 global_var = 0;
140 auto func10 = [=](){ foo1_c(A, main_size); };
141 _Cilk_spawn func10 ();
142 foo1 (A, 2);
143 _Cilk_sync;
144 if (global_var != 2)
145 return (++q);
147 global_var = 0;
148 auto func11 = [&](){ foo1(A, main_size); };
149 _Cilk_spawn func11 ();
150 foo1 (A, 2);
151 _Cilk_sync;
152 if (global_var != 2)
153 return (++q);
155 global_var = 0;
156 /* We ignore the first & second param here and pass in A from the
157 outer fn. */
158 auto func12 = [&](int *Aa, int size){ foo1(A, main_size); };
159 _Cilk_spawn func12 (A, 2);
160 foo1 (A, 2);
161 _Cilk_sync;
162 if (global_var != 2)
163 return (++q);
165 global_var = 0;
166 _Cilk_spawn [&](int *Aa){ foo1(Aa, 2); }(A);
167 foo1 (A, 2);
168 _Cilk_sync;
169 if (global_var != 2)
170 return (++q);
172 global_var = 0;
173 _Cilk_spawn [&](int *Aa, int size){ foo1(Aa, size); }(A, 2);
174 foo1 (A, 2);
175 _Cilk_sync;
176 if (global_var != 2)
177 return (++q);
179 global_var = 0;
180 _Cilk_spawn [=](int *Aa){ foo1(Aa, 2); }(A);
181 foo1 (A, 2);
182 _Cilk_sync;
183 if (global_var != 2)
184 return (++q);
186 global_var = 0;
187 _Cilk_spawn [=](int *Aa, int size){ foo1(Aa, size); }(A, 2);
188 foo1 (A, 2);
189 _Cilk_sync;
190 if (global_var != 2)
191 return (++q);
193 global_var = 0;
194 /* We ignore the first param here. */
195 _Cilk_spawn [=](int *Aa, int size){ foo1_c(A, size); }(A, 2);
196 foo1 (A, 2);
197 _Cilk_sync;
198 if (global_var != 2)
199 return (++q);
201 global_var = 0;
202 /* We ignore the first and second param here. */
203 _Cilk_spawn [=](int *Aa, int size){ foo1_c(A, size); }(B, 2);
204 foo1 (A, 2);
205 _Cilk_sync;
206 if (global_var != 2)
207 return (++q);
209 global_var = 0;
210 _Cilk_spawn [&](){ foo1(A, 2); }();
211 [&](){ foo1(A, 2); }();
212 _Cilk_sync;
213 if (global_var != 2)
214 return (++q);
216 global_var = 0;
217 _Cilk_spawn [=](){ foo1_c (A, main_size); }();
218 foo1 (A, 2);
219 _Cilk_sync;
220 if (global_var != 2)
221 return (++q);
223 global_var = 0;
224 _Cilk_spawn [&](){ foo1(A, main_size); }();
225 [&](){ foo1(A, 2); }();
226 _Cilk_sync;
227 if (global_var != 2)
228 return (++q);
230 return q;
233 int main (void)
235 return main2 (1);