| 1 |
/* { dg-do run } */ |
| 2 |
/* { dg-set-target-env-var OMP_MAX_TASK_PRIORITY "10" } */ |
| 3 |
|
| 4 |
/* This test verifies that the "priority" clause of omp task works as |
| 5 |
advertised. |
| 6 |
|
| 7 |
Testing the OpenMP task scheduler is a bit tricky, especially when |
| 8 |
trying to determine what ran first (without explicitly calling |
| 9 |
time() and/or synchronizing between threads). What we do here is |
| 10 |
run in single threaded mode which guarantees that we won't run into |
| 11 |
data races while accessing the "prio" array. |
| 12 |
|
| 13 |
We give each task a priority from 0..63, while setting |
| 14 |
OMP_MAX_TASK_PRIORITY to 10, which basically gives us 10 lower |
| 15 |
priority tasks, and the rest scheduled to run earlier. We verify |
| 16 |
that the priority < 10 tasks run last. */ |
| 17 |
|
| 18 |
#include <omp.h> |
| 19 |
#include <stdlib.h> |
| 20 |
|
| 21 |
#define N 64 |
| 22 |
|
| 23 |
int main() |
| 24 |
{ |
| 25 |
int tsknum=0, prio[N]; |
| 26 |
int max_priority = omp_get_max_task_priority (); |
| 27 |
int saved_tsknum = -1; |
| 28 |
int i; |
| 29 |
|
| 30 |
#pragma omp parallel num_threads(1) |
| 31 |
#pragma omp single private (i) |
| 32 |
{ |
| 33 |
for (i = 0; i < N; i++) |
| 34 |
#pragma omp task priority(i ^ 1) |
| 35 |
{ |
| 36 |
int t; |
| 37 |
#pragma omp atomic capture seq_cst |
| 38 |
t = tsknum++; |
| 39 |
prio[t] = i ^ 1; |
| 40 |
} |
| 41 |
#pragma omp atomic read seq_cst |
| 42 |
saved_tsknum = tsknum; |
| 43 |
} |
| 44 |
|
| 45 |
/* If any of the tasks have run before all tasks were created, don't |
| 46 |
make any assumption on the task order. Otherwise, we should have |
| 47 |
tasks with >= max_priority scheduled first in arbitrary order, |
| 48 |
followed by the rest of tasks in decreasing priority order, as |
| 49 |
there is only one thread that can schedule them. */ |
| 50 |
if (saved_tsknum == 0) |
| 51 |
{ |
| 52 |
for (i = 0; i < N; i++) |
| 53 |
if (i < N - max_priority) |
| 54 |
{ |
| 55 |
if (prio[i] < max_priority) |
| 56 |
abort (); |
| 57 |
} |
| 58 |
else if (i != N - prio[i] - 1) |
| 59 |
abort (); |
| 60 |
} |
| 61 |
return 0; |
| 62 |
} |