1 // RUN: %libomp-compile && env OMP_MAX_TASK_PRIORITY='2' %libomp-run
3 // Test OMP 4.5 task priorities
4 // Higher priority task supposed to be executed before lower priority task.
9 #include "omp_my_sleep.h"
10 // delay(n) - sleep n ms
11 #define delay(n) my_sleep(((double)n)/1000.0)
15 passed
= (omp_get_max_task_priority() == 2);
16 printf("Got %d max priority via env\n", omp_get_max_task_priority());
21 printf("parallel 1 spawns 4 tasks for primary thread to execute\n");
22 #pragma omp parallel num_threads(2)
24 int th
= omp_get_thread_num();
25 if (th
== 0) // primary thread
27 #pragma omp task priority(1)
29 int val
, t
= omp_get_thread_num();
30 #pragma omp atomic capture
32 printf("P1: val = %d, thread gen %d, thread exe %d\n", val
, th
, t
);
33 delay(10); // sleep 10 ms
35 #pragma omp task priority(2)
37 int val
, t
= omp_get_thread_num();
38 #pragma omp atomic capture
40 printf("P2: val = %d, thread gen %d, thread exe %d\n", val
, th
, t
);
41 delay(20); // sleep 20 ms
43 #pragma omp task priority(0)
44 { // low priority specified explicitly
45 int val
, t
= omp_get_thread_num();
46 #pragma omp atomic capture
48 printf("P0exp: val = %d, thread gen %d, thread exe %d\n", val
, th
, t
);
49 delay(1); // sleep 1 ms
52 { // low priority by default
53 int val
, t
= omp_get_thread_num();
54 #pragma omp atomic capture
56 printf("P0imp: val = %d, thread gen %d, thread exe %d\n", val
, th
, t
);
57 delay(1); // sleep 1 ms
60 // wait for the primary thread to finish all tasks
64 #pragma omp atomic read
69 printf("parallel 2 spawns 4 tasks for worker thread to execute\n");
70 #pragma omp parallel num_threads(2)
72 int th
= omp_get_thread_num();
73 if (th
== 0) // primary thread
75 #pragma omp task priority(1)
77 int val
, t
= omp_get_thread_num();
78 #pragma omp atomic capture
80 printf("P1: val = %d, thread gen %d, thread exe %d\n", val
, th
, t
);
81 delay(10); // sleep 10 ms
83 #pragma omp task priority(2)
85 int val
, t
= omp_get_thread_num();
86 #pragma omp atomic capture
88 printf("P2: val = %d, thread gen %d, thread exe %d\n", val
, th
, t
);
89 delay(20); // sleep 20 ms
91 #pragma omp task priority(0)
92 { // low priority specified explicitly
93 int val
, t
= omp_get_thread_num();
94 #pragma omp atomic capture
96 printf("P0exp: val = %d, thread gen %d, thread exe %d\n", val
, th
, t
);
97 delay(1); // sleep 1 ms
100 { // low priority by default
101 int val
, t
= omp_get_thread_num();
102 #pragma omp atomic capture
104 printf("P0imp: val = %d, thread gen %d, thread exe %d\n", val
, th
, t
);
105 delay(1); // sleep 1 ms
107 // signal creation of all tasks: passed = 5 + 1 = 6
110 // wait for completion of all 4 tasks
114 #pragma omp atomic read
116 } while (wait
< 10); // passed = 6 + 4 = 10
118 // wait for the primary thread to create all tasks
122 #pragma omp atomic read
125 // go execute 4 tasks created by primary thread
129 printf("failed, passed = %d (should be 10)\n", passed
);
140 // CHECK-NEXT: parallel 2