1 // RUN: %libomp-cxx-compile-and-run
2 
3 /*
4  * This test aims to check whether hidden helper thread has right gtid. We also
5  * test if there is mixed dependences between regular tasks and hidden helper
6  * tasks, the tasks are executed by right set of threads. It is equivalent to
7  * the following code:
8  *
9  * #pragma omp parallel for
10  * for (int i = 0; i < N; ++i) {
11  *   int data1 = -1, data2 = -1, data3 = -1;
12  *   int depvar;
13  * #pragma omp task shared(data1) depend(inout: depvar)
14  *   {
15  *     data1 = omp_get_global_thread_id();
16  *   }
17  * #pragma omp task hidden helper shared(data2) depend(inout: depvar)
18  *   {
19  *     data2 = omp_get_global_thread_id();
20  *   }
21  * #pragma omp task shared(data3) depend(inout: depvar)
22  *   {
23  *     data3 = omp_get_global_thread_id();
24  *   }
25  * #pragma omp taskwait
26  *   assert(data1 == 0 || data1 > __kmp_num_hidden_helper_threads);
27  *   assert(data2 > 0 && data2 <= __kmp_num_hidden_helper_threads);
28  *   assert(data3 == 0 || data3 > __kmp_num_hidden_helper_threads);
29  * }
30  */
31 
32 #include "common.h"
33 
34 extern "C" {
35 struct kmp_task_t_with_privates {
36   kmp_task_t task;
37 };
38 
39 struct anon {
40   int32_t *data;
41 };
42 }
43 
44 kmp_int32 __kmp_hidden_helper_threads_num;
45 
omp_task_entry(kmp_int32 gtid,kmp_task_t_with_privates * task)46 kmp_int32 omp_task_entry(kmp_int32 gtid, kmp_task_t_with_privates *task) {
47   auto shareds = reinterpret_cast<anon *>(task->task.shareds);
48   auto p = shareds->data;
49   *p = __kmpc_global_thread_num(nullptr);
50   return 0;
51 }
52 
assert_gtid(int v)53 template <bool hidden_helper_task> void assert_gtid(int v) {
54   if (__kmp_hidden_helper_threads_num) {
55     if (hidden_helper_task) {
56       assert(v > 0 && v <= __kmp_hidden_helper_threads_num);
57     } else {
58       assert(v == 0 || v > __kmp_hidden_helper_threads_num);
59     }
60   } else {
61     assert(v >= 0);
62   }
63 }
64 
main(int argc,char * argv[])65 int main(int argc, char *argv[]) {
66   __kmp_hidden_helper_threads_num = get_num_hidden_helper_threads();
67 
68   constexpr const int N = 1024;
69 #pragma omp parallel for
70   for (int i = 0; i < N; ++i) {
71     int32_t data1 = -1, data2 = -1, data3 = -1;
72     int depvar;
73     int32_t gtid = __kmpc_global_thread_num(nullptr);
74 
75     // Task 1, regular task
76     auto task1 = __kmpc_omp_task_alloc(
77         nullptr, gtid, 1, sizeof(kmp_task_t_with_privates), sizeof(anon),
78         reinterpret_cast<kmp_routine_entry_t>(omp_task_entry));
79     auto shareds = reinterpret_cast<anon *>(task1->shareds);
80     shareds->data = &data1;
81 
82     kmp_depend_info_t depinfo1;
83     depinfo1.base_addr = reinterpret_cast<intptr_t>(&depvar);
84     depinfo1.flags.in = 1;
85     depinfo1.flags.out = 1;
86     depinfo1.len = 4;
87 
88     __kmpc_omp_task_with_deps(nullptr, gtid, task1, 1, &depinfo1, 0, nullptr);
89 
90     // Task 2, hidden helper task
91     auto task2 = __kmpc_omp_target_task_alloc(
92         nullptr, gtid, 1, sizeof(kmp_task_t_with_privates), sizeof(anon),
93         reinterpret_cast<kmp_routine_entry_t>(omp_task_entry), -1);
94     shareds = reinterpret_cast<anon *>(task2->shareds);
95     shareds->data = &data2;
96 
97     kmp_depend_info_t depinfo2;
98     depinfo2.base_addr = reinterpret_cast<intptr_t>(&depvar);
99     depinfo2.flags.in = 1;
100     depinfo2.flags.out = 1;
101     depinfo2.len = 4;
102 
103     __kmpc_omp_task_with_deps(nullptr, gtid, task2, 1, &depinfo2, 0, nullptr);
104 
105     // Task 3, regular task
106     auto task3 = __kmpc_omp_task_alloc(
107         nullptr, gtid, 1, sizeof(kmp_task_t_with_privates), sizeof(anon),
108         reinterpret_cast<kmp_routine_entry_t>(omp_task_entry));
109     shareds = reinterpret_cast<anon *>(task3->shareds);
110     shareds->data = &data3;
111 
112     kmp_depend_info_t depinfo3;
113     depinfo3.base_addr = reinterpret_cast<intptr_t>(&depvar);
114     depinfo3.flags.in = 1;
115     depinfo3.flags.out = 1;
116     depinfo3.len = 4;
117 
118     __kmpc_omp_task_with_deps(nullptr, gtid, task3, 1, &depinfo3, 0, nullptr);
119 
120     __kmpc_omp_taskwait(nullptr, gtid);
121 
122     // FIXME: 8 here is not accurate
123     assert_gtid<false>(data1);
124     assert_gtid<true>(data2);
125     assert_gtid<false>(data3);
126   }
127 
128   std::cout << "PASS\n";
129   return 0;
130 }
131 
132 // CHECK: PASS
133