| 1 | // RUN: %libomp-cxx-compile-and-run |
| 2 | // RUN: %libomp-cxx-compile && env OMP_NUM_THREADS=1 %libomp-run |
| 3 | // REQUIRES: hidden-helper |
| 4 | |
| 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: |
| 10 | * |
| 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 | * } |
| 32 | */ |
| 33 | |
| 34 | #include "common.h" |
| 35 | |
| 36 | extern "C" { |
| 37 | struct kmp_task_t_with_privates { |
| 38 | kmp_task_t task; |
| 39 | }; |
| 40 | |
| 41 | struct anon { |
| 42 | int32_t *data; |
| 43 | }; |
| 44 | } |
| 45 | |
| 46 | static kmp_int32 __kmp_hidden_helper_threads_num; |
| 47 | |
| 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; |
| 53 | } |
| 54 | |
| 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); |
| 61 | } |
| 62 | } else { |
| 63 | assert(v >= 0); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | int main(int argc, char *argv[]) { |
| 68 | __kmp_hidden_helper_threads_num = get_num_hidden_helper_threads(); |
| 69 | |
| 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); |
| 76 | |
| 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; |
| 83 | |
| 84 | kmp_depend_info_t depinfo1; |
| 85 | depinfo1.base_addr = reinterpret_cast<intptr_t>(&depvar); |
| 86 | depinfo1.flag = 3; // INOUT |
| 87 | depinfo1.len = 4; |
| 88 | |
| 89 | __kmpc_omp_task_with_deps(loc_ref: nullptr, gtid, new_task: task1, ndeps: 1, dep_list: &depinfo1, ndeps_noalias: 0, noalias_dep_list: nullptr); |
| 90 | |
| 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; |
| 97 | |
| 98 | kmp_depend_info_t depinfo2; |
| 99 | depinfo2.base_addr = reinterpret_cast<intptr_t>(&depvar); |
| 100 | depinfo2.flag = 3; // INOUT |
| 101 | depinfo2.len = 4; |
| 102 | |
| 103 | __kmpc_omp_task_with_deps(loc_ref: nullptr, gtid, new_task: task2, ndeps: 1, dep_list: &depinfo2, ndeps_noalias: 0, noalias_dep_list: 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.flag = 3; // INOUT |
| 115 | depinfo3.len = 4; |
| 116 | |
| 117 | __kmpc_omp_task_with_deps(loc_ref: nullptr, gtid, new_task: task3, ndeps: 1, dep_list: &depinfo3, ndeps_noalias: 0, noalias_dep_list: nullptr); |
| 118 | |
| 119 | __kmpc_omp_taskwait(nullptr, gtid); |
| 120 | |
| 121 | // FIXME: 8 here is not accurate |
| 122 | assert_gtid<false>(v: data1); |
| 123 | assert_gtid<true>(v: data2); |
| 124 | assert_gtid<false>(v: data3); |
| 125 | } |
| 126 | |
| 127 | std::cout << "PASS\n" ; |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | // CHECK: PASS |
| 132 | |