Daily bump.
[official-gcc.git] / libgomp / testsuite / libgomp.c / priority.c
blob012f09d8f77c82fea361f009ef7d7925e579d107
1 /* { dg-do run } */
2 /* { dg-set-target-env-var OMP_MAX_TASK_PRIORITY "10" } */
4 /* This test verifies that the "priority" clause of omp task works as
5 advertised.
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.
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. */
18 #include <omp.h>
19 #include <stdlib.h>
21 #define N 64
23 int main()
25 int tsknum=0, prio[N];
26 int max_priority = omp_get_max_task_priority ();
27 int saved_tsknum = -1;
28 int i;
30 #pragma omp parallel num_threads(1)
31 #pragma omp single private (i)
33 for (i = 0; i < N; i++)
34 #pragma omp task priority(i ^ 1)
36 int t;
37 #pragma omp atomic capture seq_cst
38 t = tsknum++;
39 prio[t] = i ^ 1;
41 #pragma omp atomic read seq_cst
42 saved_tsknum = tsknum;
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)
52 for (i = 0; i < N; i++)
53 if (i < N - max_priority)
55 if (prio[i] < max_priority)
56 abort ();
58 else if (i != N - prio[i] - 1)
59 abort ();
61 return 0;