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
|
// REQUIRES: ompx_taskgraph
// RUN: %libomp-cxx-compile-and-run
#include <iostream>
#include <cassert>
#define NT 20
#define MULTIPLIER 100
#define DECREMENT 5
// Compiler-generated code (emulation)
typedef struct ident {
void* dummy;
} ident_t;
int val;
#ifdef __cplusplus
extern "C" {
int __kmpc_global_thread_num(ident_t *);
int __kmpc_start_record_task(ident_t *, int, int, int);
void __kmpc_end_record_task(ident_t *, int, int , int);
}
#endif
void sub() {
#pragma omp atomic
val -= DECREMENT;
}
void add() {
#pragma omp atomic
val += DECREMENT;
}
void mult() {
// no atomicity needed, can only be executed by 1 thread
// and no concurrency with other tasks possible
val *= MULTIPLIER;
}
int main() {
int num_tasks = 0;
int *x, *y;
#pragma omp parallel
#pragma omp single
for (int iter = 0; iter < NT; ++iter) {
int gtid = __kmpc_global_thread_num(nullptr);
int res = __kmpc_start_record_task(nullptr, gtid, /* kmp_tdg_flags */ 0, /* tdg_id */0);
if (res) {
num_tasks++;
#pragma omp task depend(out:y)
add();
#pragma omp task depend(out:x)
sub();
#pragma omp task depend(in:x,y)
mult();
}
__kmpc_end_record_task(nullptr, gtid, /* kmp_tdg_flags */0, /* tdg_id */0);
res = __kmpc_start_record_task(nullptr, gtid, /* kmp_tdg_flags */ 0, /* tdg_id */1);
if (res) {
num_tasks++;
#pragma omp task depend(out:y)
add();
#pragma omp task depend(out:x)
sub();
#pragma omp task depend(in:x,y)
mult();
}
__kmpc_end_record_task(nullptr, gtid, /* kmp_tdg_flags */0, /* tdg_id */1);
}
assert(num_tasks==2);
assert(val==0);
std::cout << "Passed" << std::endl;
return 0;
}
// CHECK: Passed
|