File: omp51_task_dep_inoutset.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 (258 lines) | stat: -rw-r--r-- 7,565 bytes parent folder | download | duplicates (19)
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
// RUN: %libomp-compile-and-run
// RUN: %libomp-cxx-compile-and-run
// UNSUPPORTED: gcc

// Tests OMP 5.0 task dependences "mutexinoutset" and 5.1 "inoutset",
// emulates compiler codegen for new dep kinds
// Mutually exclusive tasks get same input dependency info array
//
// Task tree created:
//      task0 - task1 (in)
//             \
//        task2 - task3 (inoutset)
//             /
//      task3 - task4 (in)
//           /
//  task6 <-->task7  (mutexinoutset)
//       \    /
//       task8 (in)
//
#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 volatile checker = 0;
static int err = 0;
#ifndef DELAY
#define DELAY 100
#endif

// ---------------------------------------------------------------------------
// internal data to emulate compiler codegen
typedef struct DEP {
  size_t addr;
  size_t len;
  unsigned char flags;
} dep;
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 MTX dependency
int thunk_m(int gtid, task_t* ptask) {
  int th = omp_get_thread_num();
  #pragma omp atomic
    ++checker;
  printf("task _%d, th %d\n", ptask->f_priv, th);
  if (checker != 1) { // no more than 1 task at a time
    err++;
    printf("Error1, checker %d != 1\n", checker);
  }
  mysleep(DELAY);
  if (checker != 1) { // no more than 1 task at a time
    err++;
    printf("Error2, checker %d != 1\n", checker);
  }
  #pragma omp atomic
    --checker;
  return 0;
}
// thunk routine for tasks with inoutset dependency
int thunk_s(int gtid, task_t* ptask) {
  int th = omp_get_thread_num();
  #pragma omp atomic
    ++checker;
  printf("task _%d, th %d\n", ptask->f_priv, th);
  if (checker > 2) { // no more than 2 tasks concurrently
    err++;
    printf("Error1, checker %d > 2\n", checker);
  }
  mysleep(DELAY);
  if (checker > 2) { // no more than 2 tasks concurrently
    err++;
    printf("Error2, checker %d > 2\n", checker);
  }
  #pragma omp atomic
    --checker;
  return 0;
}

#ifdef __cplusplus
extern "C" {
#endif
int __kmpc_global_thread_num(id*);
extern 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 nd, dep *dep_lst,
                          int nd_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(4);
  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();
      #pragma omp task depend(in: i1, i2)
      { int th = omp_get_thread_num();
        printf("task 0_%d, th %d\n", t, th);
        #pragma omp atomic
          ++checker;
        if (checker > 2) { // no more than 2 tasks concurrently
          err++;
          printf("Error1, checker %d > 2\n", checker);
        }
        mysleep(DELAY);
        if (checker > 2) { // no more than 2 tasks concurrently
          err++;
          printf("Error1, checker %d > 2\n", checker);
        }
        #pragma omp atomic
          --checker;
      }
      #pragma omp task depend(in: i1, i2)
      { int th = omp_get_thread_num();
        printf("task 1_%d, th %d\n", t, th);
        #pragma omp atomic
          ++checker;
        if (checker > 2) { // no more than 2 tasks concurrently
          err++;
          printf("Error1, checker %d > 2\n", checker);
        }
        mysleep(DELAY);
        if (checker > 2) { // no more than 2 tasks concurrently
          err++;
          printf("Error1, checker %d > 2\n", checker);
        }
        #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_s);
      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
      t = omp_get_thread_num();
      #pragma omp task depend(in: i1, i2)
      { int th = omp_get_thread_num();
        printf("task 4_%d, th %d\n", t, th);
        #pragma omp atomic
          ++checker;
        if (checker > 2) { // no more than 2 tasks concurrently
          err++;
          printf("Error1, checker %d > 2\n", checker);
        }
        mysleep(DELAY);
        if (checker > 2) { // no more than 2 tasks concurrently
          err++;
          printf("Error1, checker %d > 2\n", checker);
        }
        #pragma omp atomic
          --checker;
      }
      #pragma omp task depend(in: i1, i2)
      { int th = omp_get_thread_num();
        printf("task 5_%d, th %d\n", t, th);
        #pragma omp atomic
          ++checker;
        if (checker > 2) { // no more than 2 tasks concurrently
          err++;
          printf("Error1, checker %d > 2\n", checker);
        }
        mysleep(DELAY);
        if (checker > 2) { // no more than 2 tasks concurrently
          err++;
          printf("Error1, checker %d > 2\n", checker);
        }
        #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)&i1;
      sdep[0].len = 0;   // not used
      sdep[0].flags = 4; // MUTEXINOUTSET
      sdep[1].addr = (size_t)&i3;
      sdep[1].len = 0;   // not used
      sdep[1].flags = 4; // MUTEXINOUTSET
      ptr->f_priv = t + 30; // init single first-private variable
      __kmpc_omp_task_with_deps(&loc, gtid, ptr, 2, sdep, 0, 0);

      // task7
      ptr = __kmpc_omp_task_alloc(&loc, gtid, TIED, sizeof(task_t), 0, thunk_m);
      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)
      { int th = omp_get_thread_num();
        printf("task 8_%d, th %d\n", t, th);
        #pragma omp atomic
          ++checker;
        if (checker != 1) { // last task should run exclusively
          err++;
          printf("Error1, checker %d != 1\n", checker); }
        mysleep(DELAY);
        if (checker != 1) { // last task should run exclusively
          err++;
          printf("Error1, checker %d != 1\n", checker); }
        #pragma omp atomic
          --checker;
      }
    } // single
  } // parallel
  if (err == 0) {
    printf("passed\n");
    return 0;
  } else {
    printf("failed\n");
    return 1;
  }
}