1// RUN: %libomp-compile-and-run
2// The runtime currently does not get dependency information from GCC.
3// UNSUPPORTED: gcc
4
5// Tests OMP 5.x task dependence "omp_all_memory",
6// emulates compiler codegen versions for new dep kind
7//
8// Task tree created:
9// task0 - task1 (in: i1, i2)
10// \
11// task2 (inoutset: i2), (in: i1)
12// /
13// task3 (omp_all_memory) via flag=0x80
14// /
15// task4 - task5 (in: i1, i2)
16// /
17// task6 (omp_all_memory) via addr=-1
18// /
19// task7 (omp_all_memory) via flag=0x80
20// /
21// task8 (in: i3)
22//
23#include <stdio.h>
24#include <omp.h>
25
26#ifdef _WIN32
27#include <windows.h>
28#define mysleep(n) Sleep(n)
29#else
30#include <unistd.h>
31#define mysleep(n) usleep((n)*1000)
32#endif
33
34// to check the # of concurrent tasks (must be 1 for MTX, <3 for other kinds)
35static int checker = 0;
36static int err = 0;
37#ifndef DELAY
38#define DELAY 100
39#endif
40
41// ---------------------------------------------------------------------------
42// internal data to emulate compiler codegen
43typedef struct DEP {
44 size_t addr;
45 size_t len;
46 unsigned char flags;
47} dep;
48#define DEP_ALL_MEM 0x80
49typedef struct task {
50 void** shareds;
51 void* entry;
52 int part_id;
53 void* destr_thunk;
54 int priority;
55 long long device_id;
56 int f_priv;
57} task_t;
58#define TIED 1
59typedef int(*entry_t)(int, task_t*);
60typedef struct ID {
61 int reserved_1;
62 int flags;
63 int reserved_2;
64 int reserved_3;
65 char *psource;
66} id;
67// thunk routine for tasks with ALL dependency
68int thunk_m(int gtid, task_t* ptask) {
69 int lcheck, th;
70 #pragma omp atomic capture
71 lcheck = ++checker;
72 th = omp_get_thread_num();
73 printf(format: "task m_%d, th %d, checker %d\n", ptask->f_priv, th, lcheck);
74 if (lcheck != 1) { // no more than 1 task at a time
75 err++;
76 printf(format: "Error m1, checker %d != 1\n", lcheck);
77 }
78 mysleep(DELAY);
79 #pragma omp atomic read
80 lcheck = checker; // must still be equal to 1
81 if (lcheck != 1) {
82 err++;
83 printf(format: "Error m2, checker %d != 1\n", lcheck);
84 }
85 #pragma omp atomic
86 --checker;
87 return 0;
88}
89// thunk routine for tasks with inoutset dependency
90int thunk_s(int gtid, task_t* ptask) {
91 int lcheck, th;
92 #pragma omp atomic capture
93 lcheck = ++checker; // 1
94 th = omp_get_thread_num();
95 printf(format: "task 2_%d, th %d, checker %d\n", ptask->f_priv, th, lcheck);
96 if (lcheck != 1) { // no more than 1 task at a time
97 err++;
98 printf(format: "Error s1, checker %d != 1\n", lcheck);
99 }
100 mysleep(DELAY);
101 #pragma omp atomic read
102 lcheck = checker; // must still be equal to 1
103 if (lcheck != 1) {
104 err++;
105 printf(format: "Error s2, checker %d != 1\n", lcheck);
106 }
107 #pragma omp atomic
108 --checker;
109 return 0;
110}
111
112#ifdef __cplusplus
113extern "C" {
114#endif
115int __kmpc_global_thread_num(id*);
116task_t *__kmpc_omp_task_alloc(id *loc, int gtid, int flags,
117 size_t sz, size_t shar, entry_t rtn);
118int __kmpc_omp_task_with_deps(id *loc, int gtid, task_t *task, int ndeps,
119 dep *dep_lst, int nd_noalias, dep *noalias_lst);
120static id loc = {0, 2, 0, 0, ";file;func;0;0;;"};
121#ifdef __cplusplus
122} // extern "C"
123#endif
124// End of internal data
125// ---------------------------------------------------------------------------
126
127int main()
128{
129 int i1,i2,i3;
130 omp_set_num_threads(8);
131 omp_set_dynamic(0);
132 #pragma omp parallel
133 {
134 #pragma omp single nowait
135 {
136 dep sdep[2];
137 task_t *ptr;
138 int gtid = __kmpc_global_thread_num(&loc);
139 int t = omp_get_thread_num();
140 #pragma omp task depend(in: i1, i2)
141 { // task 0
142 int lcheck, th;
143 #pragma omp atomic capture
144 lcheck = ++checker; // 1 or 2
145 th = omp_get_thread_num();
146 printf(format: "task 0_%d, th %d, checker %d\n", t, th, lcheck);
147 if (lcheck > 2 || lcheck < 1) {
148 err++; // no more than 2 tasks concurrently
149 printf(format: "Error1, checker %d, not 1 or 2\n", lcheck);
150 }
151 mysleep(DELAY);
152 #pragma omp atomic read
153 lcheck = checker; // 1 or 2
154 if (lcheck > 2 || lcheck < 1) {
155 #pragma omp atomic
156 err++;
157 printf(format: "Error2, checker %d, not 1 or 2\n", lcheck);
158 }
159 #pragma omp atomic
160 --checker;
161 }
162 #pragma omp task depend(in: i1, i2)
163 { // task 1
164 int lcheck, th;
165 #pragma omp atomic capture
166 lcheck = ++checker; // 1 or 2
167 th = omp_get_thread_num();
168 printf(format: "task 1_%d, th %d, checker %d\n", t, th, lcheck);
169 if (lcheck > 2 || lcheck < 1) {
170 err++; // no more than 2 tasks concurrently
171 printf(format: "Error3, checker %d, not 1 or 2\n", lcheck);
172 }
173 mysleep(DELAY);
174 #pragma omp atomic read
175 lcheck = checker; // 1 or 2
176 if (lcheck > 2 || lcheck < 1) {
177 err++;
178 printf(format: "Error4, checker %d, not 1 or 2\n", lcheck);
179 }
180 #pragma omp atomic
181 --checker;
182 }
183// compiler codegen start
184 // task2
185 ptr = __kmpc_omp_task_alloc(loc: &loc, gtid, TIED, sz: sizeof(task_t), shar: 0, rtn: thunk_s);
186 sdep[0].addr = (size_t)&i1;
187 sdep[0].len = 0; // not used
188 sdep[0].flags = 1; // IN
189 sdep[1].addr = (size_t)&i2;
190 sdep[1].len = 0; // not used
191 sdep[1].flags = 8; // INOUTSET
192 ptr->f_priv = t + 10; // init single first-private variable
193 __kmpc_omp_task_with_deps(loc: &loc, gtid, task: ptr, ndeps: 2, dep_lst: sdep, nd_noalias: 0, noalias_lst: 0);
194
195 // task3
196 ptr = __kmpc_omp_task_alloc(loc: &loc, gtid, TIED, sz: sizeof(task_t), shar: 0, rtn: thunk_m);
197 sdep[0].addr = (size_t)&i1; // to be ignored
198 sdep[0].len = 0; // not used
199 sdep[0].flags = 1; // IN
200 sdep[1].addr = 0;
201 sdep[1].len = 0; // not used
202 sdep[1].flags = DEP_ALL_MEM; // omp_all_memory
203 ptr->f_priv = t + 20; // init single first-private variable
204 __kmpc_omp_task_with_deps(loc: &loc, gtid, task: ptr, ndeps: 2, dep_lst: sdep, nd_noalias: 0, noalias_lst: 0);
205// compiler codegen end
206 #pragma omp task depend(in: i1, i2)
207 { // task 4
208 int lcheck, th;
209 #pragma omp atomic capture
210 lcheck = ++checker; // 1 or 2
211 th = omp_get_thread_num();
212 printf(format: "task 4_%d, th %d, checker %d\n", t, th, lcheck);
213 if (lcheck > 2 || lcheck < 1) {
214 err++; // no more than 2 tasks concurrently
215 printf(format: "Error5, checker %d, not 1 or 2\n", lcheck);
216 }
217 mysleep(DELAY);
218 #pragma omp atomic read
219 lcheck = checker; // 1 or 2
220 if (lcheck > 2 || lcheck < 1) {
221 err++;
222 printf(format: "Error6, checker %d, not 1 or 2\n", lcheck);
223 }
224 #pragma omp atomic
225 --checker;
226 }
227 #pragma omp task depend(in: i1, i2)
228 { // task 5
229 int lcheck, th;
230 #pragma omp atomic capture
231 lcheck = ++checker; // 1 or 2
232 th = omp_get_thread_num();
233 printf(format: "task 5_%d, th %d, checker %d\n", t, th, lcheck);
234 if (lcheck > 2 || lcheck < 1) {
235 err++; // no more than 2 tasks concurrently
236 printf(format: "Error7, checker %d, not 1 or 2\n", lcheck);
237 }
238 mysleep(DELAY);
239 #pragma omp atomic read
240 lcheck = checker; // 1 or 2
241 if (lcheck > 2 || lcheck < 1) {
242 err++;
243 printf(format: "Error8, checker %d, not 1 or 2\n", lcheck);
244 }
245 #pragma omp atomic
246 --checker;
247 }
248// compiler codegen start
249 // task6
250 ptr = __kmpc_omp_task_alloc(loc: &loc, gtid, TIED, sz: sizeof(task_t), shar: 0, rtn: thunk_m);
251 sdep[0].addr = (size_t)(-1); // omp_all_memory
252 sdep[0].len = 0; // not used
253 sdep[0].flags = 2; // OUT
254 ptr->f_priv = t + 30; // init single first-private variable
255 __kmpc_omp_task_with_deps(loc: &loc, gtid, task: ptr, ndeps: 1, dep_lst: sdep, nd_noalias: 0, noalias_lst: 0);
256
257 // task7
258 ptr = __kmpc_omp_task_alloc(loc: &loc, gtid, TIED, sz: sizeof(task_t), shar: 0, rtn: thunk_m);
259 sdep[0].addr = 0;
260 sdep[0].len = 0; // not used
261 sdep[0].flags = DEP_ALL_MEM; // omp_all_memory
262 sdep[1].addr = (size_t)&i3; // to be ignored
263 sdep[1].len = 0; // not used
264 sdep[1].flags = 4; // MUTEXINOUTSET
265 ptr->f_priv = t + 40; // init single first-private variable
266 __kmpc_omp_task_with_deps(loc: &loc, gtid, task: ptr, ndeps: 2, dep_lst: sdep, nd_noalias: 0, noalias_lst: 0);
267// compiler codegen end
268 #pragma omp task depend(in: i3)
269 { // task 8
270 int lcheck, th;
271 #pragma omp atomic capture
272 lcheck = ++checker; // 1
273 th = omp_get_thread_num();
274 printf(format: "task 8_%d, th %d, checker %d\n", t, th, lcheck);
275 if (lcheck != 1) {
276 err++;
277 printf(format: "Error9, checker %d, != 1\n", lcheck);
278 }
279 mysleep(DELAY);
280 #pragma omp atomic read
281 lcheck = checker;
282 if (lcheck != 1) {
283 err++;
284 printf(format: "Error10, checker %d, != 1\n", lcheck);
285 }
286 #pragma omp atomic
287 --checker;
288 }
289 } // single
290 } // parallel
291 if (err == 0 && checker == 0) {
292 printf(format: "passed\n");
293 return 0;
294 } else {
295 printf(format: "failed, err = %d, checker = %d\n", err, checker);
296 return 1;
297 }
298}
299

source code of openmp/runtime/test/tasking/kmp_task_depend_all.c