[llvm] Stop including unordered_map (NFC)
[llvm-project.git] / openmp / runtime / test / tasking / hidden_helper_task / gtid.cpp
blobbc02caccb69ed943e4aff2b58e6ea26c0599aa91
1 // RUN: %libomp-cxx-compile-and-run
2 // RUN: %libomp-cxx-compile && env OMP_NUM_THREADS=1 %libomp-run
3 // REQUIRES: hidden-helper
5 /*
6 * This test aims to check whether hidden helper thread has right gtid. We also
7 * test if there is mixed dependences between regular tasks and hidden helper
8 * tasks, the tasks are executed by right set of threads. It is equivalent to
9 * the following code:
11 * #pragma omp parallel for
12 * for (int i = 0; i < N; ++i) {
13 * int data1 = -1, data2 = -1, data3 = -1;
14 * int depvar;
15 * #pragma omp task shared(data1) depend(inout: depvar)
16 * {
17 * data1 = omp_get_global_thread_id();
18 * }
19 * #pragma omp task hidden helper shared(data2) depend(inout: depvar)
20 * {
21 * data2 = omp_get_global_thread_id();
22 * }
23 * #pragma omp task shared(data3) depend(inout: depvar)
24 * {
25 * data3 = omp_get_global_thread_id();
26 * }
27 * #pragma omp taskwait
28 * assert(data1 == 0 || data1 > __kmp_num_hidden_helper_threads);
29 * assert(data2 > 0 && data2 <= __kmp_num_hidden_helper_threads);
30 * assert(data3 == 0 || data3 > __kmp_num_hidden_helper_threads);
31 * }
34 #include "common.h"
36 extern "C" {
37 struct kmp_task_t_with_privates {
38 kmp_task_t task;
41 struct anon {
42 int32_t *data;
46 kmp_int32 __kmp_hidden_helper_threads_num;
48 kmp_int32 omp_task_entry(kmp_int32 gtid, kmp_task_t_with_privates *task) {
49 auto shareds = reinterpret_cast<anon *>(task->task.shareds);
50 auto p = shareds->data;
51 *p = __kmpc_global_thread_num(nullptr);
52 return 0;
55 template <bool hidden_helper_task> void assert_gtid(int v) {
56 if (__kmp_hidden_helper_threads_num) {
57 if (hidden_helper_task) {
58 assert(v > 0 && v <= __kmp_hidden_helper_threads_num);
59 } else {
60 assert(v == 0 || v > __kmp_hidden_helper_threads_num);
62 } else {
63 assert(v >= 0);
67 int main(int argc, char *argv[]) {
68 __kmp_hidden_helper_threads_num = get_num_hidden_helper_threads();
70 constexpr const int N = 1024;
71 #pragma omp parallel for
72 for (int i = 0; i < N; ++i) {
73 int32_t data1 = -1, data2 = -1, data3 = -1;
74 int depvar;
75 int32_t gtid = __kmpc_global_thread_num(nullptr);
77 // Task 1, regular task
78 auto task1 = __kmpc_omp_task_alloc(
79 nullptr, gtid, 1, sizeof(kmp_task_t_with_privates), sizeof(anon),
80 reinterpret_cast<kmp_routine_entry_t>(omp_task_entry));
81 auto shareds = reinterpret_cast<anon *>(task1->shareds);
82 shareds->data = &data1;
84 kmp_depend_info_t depinfo1;
85 depinfo1.base_addr = reinterpret_cast<intptr_t>(&depvar);
86 depinfo1.flag = 3; // INOUT
87 depinfo1.len = 4;
89 __kmpc_omp_task_with_deps(nullptr, gtid, task1, 1, &depinfo1, 0, nullptr);
91 // Task 2, hidden helper task
92 auto task2 = __kmpc_omp_target_task_alloc(
93 nullptr, gtid, 1, sizeof(kmp_task_t_with_privates), sizeof(anon),
94 reinterpret_cast<kmp_routine_entry_t>(omp_task_entry), -1);
95 shareds = reinterpret_cast<anon *>(task2->shareds);
96 shareds->data = &data2;
98 kmp_depend_info_t depinfo2;
99 depinfo2.base_addr = reinterpret_cast<intptr_t>(&depvar);
100 depinfo2.flag = 3; // INOUT
101 depinfo2.len = 4;
103 __kmpc_omp_task_with_deps(nullptr, gtid, task2, 1, &depinfo2, 0, nullptr);
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;
112 kmp_depend_info_t depinfo3;
113 depinfo3.base_addr = reinterpret_cast<intptr_t>(&depvar);
114 depinfo3.flag = 3; // INOUT
115 depinfo3.len = 4;
117 __kmpc_omp_task_with_deps(nullptr, gtid, task3, 1, &depinfo3, 0, nullptr);
119 __kmpc_omp_taskwait(nullptr, gtid);
121 // FIXME: 8 here is not accurate
122 assert_gtid<false>(data1);
123 assert_gtid<true>(data2);
124 assert_gtid<false>(data3);
127 std::cout << "PASS\n";
128 return 0;
131 // CHECK: PASS