// task2 (inoutset: i2), (in: i1)
#include "callback.h"
#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
static int checker = 0;
static int err = 0;
#ifndef DELAY
#define DELAY 100
#endif
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;
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) {
err++;
printf("Error m1, checker %d != 1\n", lcheck);
}
mysleep(DELAY);
#pragma omp atomic read
lcheck = checker;
if (lcheck != 1) {
err++;
printf("Error m2, checker %d != 1\n", lcheck);
}
#pragma omp atomic
--checker;
return 0;
}
int thunk_s(int gtid, task_t *ptask) {
int lcheck, th;
#pragma omp atomic capture
lcheck = ++checker;
th = omp_get_thread_num();
printf("task 2_%d, th %d, checker %d\n", ptask->f_priv, th, lcheck);
if (lcheck != 1) {
err++;
printf("Error s1, checker %d != 1\n", lcheck);
}
mysleep(DELAY);
#pragma omp atomic read
lcheck = checker;
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);
static id loc = {0, 2, 0, 0, ";file;func;0;0;;"};
#ifdef __cplusplus
}
#endif
int main() {
char *ompx_all_memory = (void *)0xffffffffffffffff;
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();
#pragma omp task depend(in : i1, i2)
{
int lcheck, th;
#pragma omp atomic capture
lcheck = ++checker;
th = omp_get_thread_num();
printf("task 0_%d, th %d, checker %d\n", t, th, lcheck);
if (lcheck > 2 || lcheck < 1) {
err++;
printf("Error1, checker %d, not 1 or 2\n", lcheck);
}
mysleep(DELAY);
#pragma omp atomic read
lcheck = checker;
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)
{
int lcheck, th;
#pragma omp atomic capture
lcheck = ++checker;
th = omp_get_thread_num();
printf("task 1_%d, th %d, checker %d\n", t, th, lcheck);
if (lcheck > 2 || lcheck < 1) {
err++;
printf("Error3, checker %d, not 1 or 2\n", lcheck);
}
mysleep(DELAY);
#pragma omp atomic read
lcheck = checker;
if (lcheck > 2 || lcheck < 1) {
err++;
printf("Error4, checker %d, not 1 or 2\n", lcheck);
}
#pragma omp atomic
--checker;
}
ptr = __kmpc_omp_task_alloc(&loc, gtid, TIED, sizeof(task_t), 0, thunk_s);
sdep[0].addr = (size_t)&i1;
sdep[0].len = 0;
sdep[0].flags = 1;
sdep[1].addr = (size_t)&i2;
sdep[1].len = 0;
sdep[1].flags = 8;
ptr->f_priv = t + 10;
__kmpc_omp_task_with_deps(&loc, gtid, ptr, 2, sdep, 0, 0);
#pragma omp task depend(in : i1) depend(inout : ompx_all_memory[0])
{
int lcheck, th;
#pragma omp atomic capture
lcheck = ++checker;
th = omp_get_thread_num();
printf("task m_%d, th %d, checker %d\n", t, th, lcheck);
if (lcheck != 1) {
err++;
printf("Error m1, checker %d != 1\n", lcheck);
}
mysleep(DELAY);
#pragma omp atomic read
lcheck = checker;
if (lcheck != 1) {
err++;
printf("Error m2, checker %d != 1\n", lcheck);
}
#pragma omp atomic
--checker;
}
#pragma omp task depend(in : i1, i2)
{
int lcheck, th;
#pragma omp atomic capture
lcheck = ++checker;
th = omp_get_thread_num();
printf("task 4_%d, th %d, checker %d\n", t, th, lcheck);
if (lcheck > 2 || lcheck < 1) {
err++;
printf("Error5, checker %d, not 1 or 2\n", lcheck);
}
mysleep(DELAY);
#pragma omp atomic read
lcheck = checker;
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)
{
int lcheck, th;
#pragma omp atomic capture
lcheck = ++checker;
th = omp_get_thread_num();
printf("task 5_%d, th %d, checker %d\n", t, th, lcheck);
if (lcheck > 2 || lcheck < 1) {
err++;
printf("Error7, checker %d, not 1 or 2\n", lcheck);
}
mysleep(DELAY);
#pragma omp atomic read
lcheck = checker;
if (lcheck > 2 || lcheck < 1) {
err++;
printf("Error8, checker %d, not 1 or 2\n", lcheck);
}
#pragma omp atomic
--checker;
}
#pragma omp task depend(inout : ompx_all_memory[0])
{
int lcheck, th;
#pragma omp atomic capture
lcheck = ++checker;
th = omp_get_thread_num();
printf("task m_%d, th %d, checker %d\n", t, th, lcheck);
if (lcheck != 1) {
err++;
printf("Error m1, checker %d != 1\n", lcheck);
}
mysleep(DELAY);
#pragma omp atomic read
lcheck = checker;
if (lcheck != 1) {
err++;
printf("Error m2, checker %d != 1\n", lcheck);
}
#pragma omp atomic
--checker;
}
#pragma omp task depend(inout : ompx_all_memory[0]) depend(mutexinoutset : i3)
{
int lcheck, th;
#pragma omp atomic capture
lcheck = ++checker;
th = omp_get_thread_num();
printf("task m_%d, th %d, checker %d\n", t, th, lcheck);
if (lcheck != 1) {
err++;
printf("Error m1, checker %d != 1\n", lcheck);
}
mysleep(DELAY);
#pragma omp atomic read
lcheck = checker;
if (lcheck != 1) {
err++;
printf("Error m2, checker %d != 1\n", lcheck);
}
#pragma omp atomic
--checker;
}
#pragma omp task depend(in : i3)
{
int lcheck, th;
#pragma omp atomic capture
lcheck = ++checker;
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;
}
}
}
if (err == 0 && checker == 0) {
printf("passed\n");
return 0;
} else {
printf("failed, err = %d, checker = %d\n", err, checker);
return 1;
}
}