#include <stdio.h>
#include <omp.h>
#include "omp_my_sleep.h"
#define delay(n) my_sleep(((double)n)/1000.0)
int main ( void ) {
int passed;
passed = (omp_get_max_task_priority() == 2);
printf("Got %d max priority via env\n", omp_get_max_task_priority());
if(!passed) {
printf( "failed\n" );
return 1;
}
printf("parallel 1 spawns 4 tasks for primary thread to execute\n");
#pragma omp parallel num_threads(2)
{
int th = omp_get_thread_num();
if (th == 0)
{
#pragma omp task priority(1)
{
int val, t = omp_get_thread_num();
#pragma omp atomic capture
val = passed++;
printf("P1: val = %d, thread gen %d, thread exe %d\n", val, th, t);
delay(10);
}
#pragma omp task priority(2)
{
int val, t = omp_get_thread_num();
#pragma omp atomic capture
val = passed++;
printf("P2: val = %d, thread gen %d, thread exe %d\n", val, th, t);
delay(20);
}
#pragma omp task priority(0)
{
int val, t = omp_get_thread_num();
#pragma omp atomic capture
val = passed++;
printf("P0exp: val = %d, thread gen %d, thread exe %d\n", val, th, t);
delay(1);
}
#pragma omp task
{
int val, t = omp_get_thread_num();
#pragma omp atomic capture
val = passed++;
printf("P0imp: val = %d, thread gen %d, thread exe %d\n", val, th, t);
delay(1);
}
} else {
int wait = 0;
do {
delay(5);
#pragma omp atomic read
wait = passed;
} while (wait < 5);
}
}
printf("parallel 2 spawns 4 tasks for worker thread to execute\n");
#pragma omp parallel num_threads(2)
{
int th = omp_get_thread_num();
if (th == 0)
{
#pragma omp task priority(1)
{
int val, t = omp_get_thread_num();
#pragma omp atomic capture
val = passed++;
printf("P1: val = %d, thread gen %d, thread exe %d\n", val, th, t);
delay(10);
}
#pragma omp task priority(2)
{
int val, t = omp_get_thread_num();
#pragma omp atomic capture
val = passed++;
printf("P2: val = %d, thread gen %d, thread exe %d\n", val, th, t);
delay(20);
}
#pragma omp task priority(0)
{
int val, t = omp_get_thread_num();
#pragma omp atomic capture
val = passed++;
printf("P0exp: val = %d, thread gen %d, thread exe %d\n", val, th, t);
delay(1);
}
#pragma omp task
{
int val, t = omp_get_thread_num();
#pragma omp atomic capture
val = passed++;
printf("P0imp: val = %d, thread gen %d, thread exe %d\n", val, th, t);
delay(1);
}
#pragma omp atomic
passed++;
int wait = 0;
do {
delay(5);
#pragma omp atomic read
wait = passed;
} while (wait < 10);
} else {
int wait = 0;
do {
delay(5);
#pragma omp atomic read
wait = passed;
} while (wait < 6);
}
}
if (passed != 10) {
printf("failed, passed = %d (should be 10)\n", passed);
return 1;
}
printf("passed\n");
return 0;
}