File: kmp_taskwait_depend_all.c

package info (click to toggle)
llvm-toolchain-15 1%3A15.0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,554,644 kB
  • sloc: cpp: 5,922,452; ansic: 1,012,136; asm: 674,362; python: 191,568; objc: 73,855; f90: 42,327; lisp: 31,913; pascal: 11,973; javascript: 10,144; sh: 9,421; perl: 7,447; ml: 5,527; awk: 3,523; makefile: 2,520; xml: 885; cs: 573; fortran: 567
file content (334 lines) | stat: -rw-r--r-- 10,602 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// RUN: %libomp-compile-and-run
// The runtime currently does not get dependency information from GCC.
// UNSUPPORTED: gcc

// Tests OMP 5.x task dependence "omp_all_memory",
// emulates compiler codegen versions for new dep kind
//
// Task tree created:
//      task0 - task1 (in: i1, i2)
//             \
//        task2 (inoutset: i2), (in: i1)
//             /
//        task3 (omp_all_memory) via flag=0x80
//             /
//      task4 - task5 (in: i1, i2)
//           /
//       task6 (omp_all_memory) via addr=-1
//           /
//       task7 (omp_all_memory) via flag=0x80
//           /
//       task8 (in: i3)
//           /
//       task9 - no dependences
//           /
//       taskwait (omp_all_memory) (should not wait for task9, see prints)
//
#include <stdio.h>
#include <omp.h>

#ifdef _WIN32
#include <windows.h>
#define mysleep(n) Sleep(n)
#else
#include <unistd.h>
#define mysleep(n) usleep((n)*1000)
#endif

// to check the # of concurrent tasks (must be 1 for MTX, <3 for other kinds)
static int checker = 0;
static int err = 0;
static int taskwait_flag = 0;
#ifndef DELAY
// set delay interval in ms for dependent tasks
#define DELAY 100
#endif

// ---------------------------------------------------------------------------
// internal data to emulate compiler codegen
typedef struct DEP {
  size_t addr;
  size_t len;
  unsigned char flags;
} dep;
#define DEP_ALL_MEM 0x80
typedef struct task {
  void** shareds;
  void* entry;
  int part_id;
  void* destr_thunk;
  int priority;
  long long device_id;
  int f_priv;
} task_t;
#define TIED 1
typedef int(*entry_t)(int, task_t*);
typedef struct ID {
  int reserved_1;
  int flags;
  int reserved_2;
  int reserved_3;
  char *psource;
} id;
// thunk routine for tasks with ALL dependency
int thunk_m(int gtid, task_t* ptask) {
  int lcheck, th;
  #pragma omp atomic capture
    lcheck = ++checker;
  th = omp_get_thread_num();
  printf("task m_%d, th %d, checker %d\n", ptask->f_priv, th, lcheck);
  if (lcheck != 1) { // no more than 1 task at a time
    err++;
    printf("Error m1, checker %d != 1\n", lcheck);
  }
  mysleep(DELAY);
  #pragma omp atomic read
    lcheck = checker; // must still be equal to 1
  if (lcheck != 1) {
    err++;
    printf("Error m2, checker %d != 1\n", lcheck);
  }
  #pragma omp atomic
    --checker;
  return 0;
}
// thunk routine for tasks with inoutset dependency
int thunk_s(int gtid, task_t* ptask) {
  int lcheck, th;
  #pragma omp atomic capture
    lcheck = ++checker; // 1
  th = omp_get_thread_num();
  printf("task 2_%d, th %d, checker %d\n", ptask->f_priv, th, lcheck);
  if (lcheck != 1) { // no more than 1 task at a time
    err++;
    printf("Error s1, checker %d != 1\n", lcheck);
  }
  mysleep(DELAY);
  #pragma omp atomic read
    lcheck = checker; // must still be equal to 1
  if (lcheck != 1) {
    err++;
    printf("Error s2, checker %d != 1\n", lcheck);
  }
  #pragma omp atomic
    --checker;
  return 0;
}

#ifdef __cplusplus
extern "C" {
#endif
int __kmpc_global_thread_num(id*);
task_t *__kmpc_omp_task_alloc(id *loc, int gtid, int flags,
                              size_t sz, size_t shar, entry_t rtn);
int __kmpc_omp_task_with_deps(id *loc, int gtid, task_t *task, int ndeps,
                              dep *dep_lst, int nd_noalias, dep *noalias_lst);
void __kmpc_omp_wait_deps(id *loc, int gtid, int ndeps, dep *dep_lst,
                          int ndeps_noalias, dep *noalias_dep_lst);
static id loc = {0, 2, 0, 0, ";file;func;0;0;;"};
#ifdef __cplusplus
} // extern "C"
#endif
// End of internal data
// ---------------------------------------------------------------------------

int main()
{
  int i1,i2,i3;
  omp_set_num_threads(8);
  omp_set_dynamic(0);
  #pragma omp parallel
  {
    #pragma omp single nowait
    {
      dep sdep[2];
      task_t *ptr;
      int gtid = __kmpc_global_thread_num(&loc);
      int t = omp_get_thread_num();
      // Create longest task first to ensure it is stolen.
      // The test may hang if the task created last and
      // executed by a thread which executes taskwait.
      #pragma omp task
      { // task 9 - long running task
        int flag;
        int th = omp_get_thread_num();
        printf("signalled independent task 9_%d, th %d started....\n", t, th);
        // Wait for taskwait depend() to finish
        // If the taskwait depend() improperly depends on this task
        // to finish, then the test will hang and a timeout should trigger
        while (1) {
          #pragma omp atomic read
          flag = taskwait_flag;
          if (flag == 1)
            break;
        }
        printf("signalled independent task 9_%d, th %d finished....\n", t, th);
      }
      #pragma omp task depend(in: i1, i2)
      { // task 0
        int lcheck, th;
        #pragma omp atomic capture
          lcheck = ++checker; // 1 or 2
        th = omp_get_thread_num();
        printf("task 0_%d, th %d, checker %d\n", t, th, lcheck);
        if (lcheck > 2 || lcheck < 1) {
          err++; // no more than 2 tasks concurrently
          printf("Error1, checker %d, not 1 or 2\n", lcheck);
        }
        mysleep(DELAY);
        #pragma omp atomic read
          lcheck = checker; // 1 or 2
        if (lcheck > 2 || lcheck < 1) {
          #pragma omp atomic
            err++;
          printf("Error2, checker %d, not 1 or 2\n", lcheck);
        }
        #pragma omp atomic
          --checker;
      }
      #pragma omp task depend(in: i1, i2)
      { // task 1
        int lcheck, th;
        #pragma omp atomic capture
          lcheck = ++checker; // 1 or 2
        th = omp_get_thread_num();
        printf("task 1_%d, th %d, checker %d\n", t, th, lcheck);
        if (lcheck > 2 || lcheck < 1) {
          err++; // no more than 2 tasks concurrently
          printf("Error3, checker %d, not 1 or 2\n", lcheck);
        }
        mysleep(DELAY);
        #pragma omp atomic read
          lcheck = checker; // 1 or 2
        if (lcheck > 2 || lcheck < 1) {
          err++;
          printf("Error4, checker %d, not 1 or 2\n", lcheck);
        }
        #pragma omp atomic
          --checker;
      }
// compiler codegen start
      // task2
      ptr = __kmpc_omp_task_alloc(&loc, gtid, TIED, sizeof(task_t), 0, thunk_s);
      sdep[0].addr = (size_t)&i1;
      sdep[0].len = 0;   // not used
      sdep[0].flags = 1; // IN
      sdep[1].addr = (size_t)&i2;
      sdep[1].len = 0;   // not used
      sdep[1].flags = 8; // INOUTSET
      ptr->f_priv = t + 10; // init single first-private variable
      __kmpc_omp_task_with_deps(&loc, gtid, ptr, 2, sdep, 0, 0);

      // task3
      ptr = __kmpc_omp_task_alloc(&loc, gtid, TIED, sizeof(task_t), 0, thunk_m);
      sdep[0].addr = (size_t)&i1; // to be ignored
      sdep[0].len = 0;   // not used
      sdep[0].flags = 1; // IN
      sdep[1].addr = 0;
      sdep[1].len = 0;   // not used
      sdep[1].flags = DEP_ALL_MEM; // omp_all_memory
      ptr->f_priv = t + 20; // init single first-private variable
      __kmpc_omp_task_with_deps(&loc, gtid, ptr, 2, sdep, 0, 0);
// compiler codegen end
      #pragma omp task depend(in: i1, i2)
      { // task 4
        int lcheck, th;
        #pragma omp atomic capture
          lcheck = ++checker; // 1 or 2
        th = omp_get_thread_num();
        printf("task 4_%d, th %d, checker %d\n", t, th, lcheck);
        if (lcheck > 2 || lcheck < 1) {
          err++; // no more than 2 tasks concurrently
          printf("Error5, checker %d, not 1 or 2\n", lcheck);
        }
        mysleep(DELAY);
        #pragma omp atomic read
          lcheck = checker; // 1 or 2
        if (lcheck > 2 || lcheck < 1) {
          err++;
          printf("Error6, checker %d, not 1 or 2\n", lcheck);
        }
        #pragma omp atomic
          --checker;
      }
      #pragma omp task depend(in: i1, i2)
      { // task 5
        int lcheck, th;
        #pragma omp atomic capture
          lcheck = ++checker; // 1 or 2
        th = omp_get_thread_num();
        printf("task 5_%d, th %d, checker %d\n", t, th, lcheck);
        if (lcheck > 2 || lcheck < 1) {
          err++; // no more than 2 tasks concurrently
          printf("Error7, checker %d, not 1 or 2\n", lcheck);
        }
        mysleep(DELAY);
        #pragma omp atomic read
          lcheck = checker; // 1 or 2
        if (lcheck > 2 || lcheck < 1) {
          err++;
          printf("Error8, checker %d, not 1 or 2\n", lcheck);
        }
        #pragma omp atomic
          --checker;
      }
// compiler codegen start
      // task6
      ptr = __kmpc_omp_task_alloc(&loc, gtid, TIED, sizeof(task_t), 0, thunk_m);
      sdep[0].addr = (size_t)(-1); // omp_all_memory
      sdep[0].len = 0;   // not used
      sdep[0].flags = 2; // OUT
      ptr->f_priv = t + 30; // init single first-private variable
      __kmpc_omp_task_with_deps(&loc, gtid, ptr, 1, sdep, 0, 0);

      // task7
      ptr = __kmpc_omp_task_alloc(&loc, gtid, TIED, sizeof(task_t), 0, thunk_m);
      sdep[0].addr = 0;
      sdep[0].len = 0;   // not used
      sdep[0].flags = DEP_ALL_MEM; // omp_all_memory
      sdep[1].addr = (size_t)&i3; // to be ignored
      sdep[1].len = 0;   // not used
      sdep[1].flags = 4; // MUTEXINOUTSET
      ptr->f_priv = t + 40; // init single first-private variable
      __kmpc_omp_task_with_deps(&loc, gtid, ptr, 2, sdep, 0, 0);
// compiler codegen end
      #pragma omp task depend(in: i3)
      { // task 8
        int lcheck, th;
        #pragma omp atomic capture
          lcheck = ++checker; // 1
        th = omp_get_thread_num();
        printf("task 8_%d, th %d, checker %d\n", t, th, lcheck);
        if (lcheck != 1) {
          err++;
          printf("Error9, checker %d, != 1\n", lcheck);
        }
        mysleep(DELAY);
        #pragma omp atomic read
          lcheck = checker;
        if (lcheck != 1) {
          err++;
          printf("Error10, checker %d, != 1\n", lcheck);
        }
        #pragma omp atomic
          --checker;
      }
      mysleep(1); // wait a bit to ensure at least first task is stolen
//  #pragma omp taskwait depend(omp_all_memory: out)
      printf("all 10 tasks generated;\n"
             "taskwait depend(omp_all_memory: out)  started, th %d\n", t);
      __kmpc_omp_wait_deps(&loc, gtid, 1, sdep, 0, 0);
      #pragma omp atomic write
        taskwait_flag = 1;
      printf("taskwait depend(omp_all_memory: out)  passed, th %d\n", t);
      fflush(0);
    } // single
  } // parallel
  if (err == 0 && checker == 0) {
    printf("passed\n");
    return 0;
  } else {
    printf("failed, err = %d, checker = %d\n", err, checker);
    return 1;
  }
}