Joachim Protze | 3865c69 | 2018-04-12 17:23:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * ompt-specific.cpp -- OMPT internal functions |
| 3 | */ |
| 4 | |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 7 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 8 | // See https://llvm.org/LICENSE.txt for license information. |
| 9 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Joachim Protze | 3865c69 | 2018-04-12 17:23:26 +0000 | [diff] [blame] | 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 13 | //****************************************************************************** |
| 14 | // include files |
| 15 | //****************************************************************************** |
| 16 | |
| 17 | #include "kmp.h" |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 18 | #include "ompt-specific.h" |
| 19 | |
Joachim Protze | 82e94a5 | 2017-11-01 10:08:30 +0000 | [diff] [blame] | 20 | #if KMP_OS_UNIX |
| 21 | #include <dlfcn.h> |
| 22 | #endif |
| 23 | |
| 24 | #if KMP_OS_WINDOWS |
| 25 | #define THREAD_LOCAL __declspec(thread) |
| 26 | #else |
| 27 | #define THREAD_LOCAL __thread |
| 28 | #endif |
| 29 | |
Jonathan Peyton | 40039ac | 2017-11-07 23:32:13 +0000 | [diff] [blame] | 30 | #define OMPT_WEAK_ATTRIBUTE KMP_WEAK_ATTRIBUTE |
| 31 | |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 32 | //****************************************************************************** |
| 33 | // macros |
| 34 | //****************************************************************************** |
| 35 | |
Joachim Protze | 82e94a5 | 2017-11-01 10:08:30 +0000 | [diff] [blame] | 36 | #define LWT_FROM_TEAM(team) (team)->t.ompt_serialized_team_info |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 37 | |
| 38 | #define OMPT_THREAD_ID_BITS 16 |
| 39 | |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 40 | //****************************************************************************** |
| 41 | // private operations |
| 42 | //****************************************************************************** |
| 43 | |
| 44 | //---------------------------------------------------------- |
| 45 | // traverse the team and task hierarchy |
Joachim Protze | 82e94a5 | 2017-11-01 10:08:30 +0000 | [diff] [blame] | 46 | // note: __ompt_get_teaminfo and __ompt_get_task_info_object |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 47 | // traverse the hierarchy similarly and need to be |
| 48 | // kept consistent |
| 49 | //---------------------------------------------------------- |
| 50 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 51 | ompt_team_info_t *__ompt_get_teaminfo(int depth, int *size) { |
| 52 | kmp_info_t *thr = ompt_get_thread(); |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 53 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 54 | if (thr) { |
| 55 | kmp_team *team = thr->th.th_team; |
| 56 | if (team == NULL) |
| 57 | return NULL; |
Jonathan Peyton | f0344bb | 2015-10-09 17:42:52 +0000 | [diff] [blame] | 58 | |
Joachim Protze | 82e94a5 | 2017-11-01 10:08:30 +0000 | [diff] [blame] | 59 | ompt_lw_taskteam_t *next_lwt = LWT_FROM_TEAM(team), *lwt = NULL; |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 60 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 61 | while (depth > 0) { |
| 62 | // next lightweight team (if any) |
| 63 | if (lwt) |
| 64 | lwt = lwt->parent; |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 65 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 66 | // next heavyweight team (if any) after |
| 67 | // lightweight teams are exhausted |
| 68 | if (!lwt && team) { |
Joachim Protze | 82e94a5 | 2017-11-01 10:08:30 +0000 | [diff] [blame] | 69 | if (next_lwt) { |
| 70 | lwt = next_lwt; |
| 71 | next_lwt = NULL; |
| 72 | } else { |
| 73 | team = team->t.t_parent; |
| 74 | if (team) { |
| 75 | next_lwt = LWT_FROM_TEAM(team); |
| 76 | } |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 77 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 78 | } |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 79 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 80 | depth--; |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 83 | if (lwt) { |
| 84 | // lightweight teams have one task |
| 85 | if (size) |
| 86 | *size = 1; |
| 87 | |
| 88 | // return team info for lightweight team |
| 89 | return &lwt->ompt_team_info; |
| 90 | } else if (team) { |
| 91 | // extract size from heavyweight team |
| 92 | if (size) |
| 93 | *size = team->t.t_nproc; |
| 94 | |
| 95 | // return team info for heavyweight team |
| 96 | return &team->t.ompt_team_info; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return NULL; |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Joachim Protze | 82e94a5 | 2017-11-01 10:08:30 +0000 | [diff] [blame] | 103 | ompt_task_info_t *__ompt_get_task_info_object(int depth) { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 104 | ompt_task_info_t *info = NULL; |
| 105 | kmp_info_t *thr = ompt_get_thread(); |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 106 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 107 | if (thr) { |
| 108 | kmp_taskdata_t *taskdata = thr->th.th_current_task; |
Joachim Protze | 82e94a5 | 2017-11-01 10:08:30 +0000 | [diff] [blame] | 109 | ompt_lw_taskteam_t *lwt = NULL, |
| 110 | *next_lwt = LWT_FROM_TEAM(taskdata->td_team); |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 111 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 112 | while (depth > 0) { |
| 113 | // next lightweight team (if any) |
| 114 | if (lwt) |
| 115 | lwt = lwt->parent; |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 116 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 117 | // next heavyweight team (if any) after |
| 118 | // lightweight teams are exhausted |
| 119 | if (!lwt && taskdata) { |
Joachim Protze | 82e94a5 | 2017-11-01 10:08:30 +0000 | [diff] [blame] | 120 | if (next_lwt) { |
| 121 | lwt = next_lwt; |
| 122 | next_lwt = NULL; |
| 123 | } else { |
| 124 | taskdata = taskdata->td_parent; |
| 125 | if (taskdata) { |
| 126 | next_lwt = LWT_FROM_TEAM(taskdata->td_team); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | depth--; |
| 131 | } |
| 132 | |
| 133 | if (lwt) { |
| 134 | info = &lwt->ompt_task_info; |
| 135 | } else if (taskdata) { |
| 136 | info = &taskdata->ompt_task_info; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | return info; |
| 141 | } |
| 142 | |
| 143 | ompt_task_info_t *__ompt_get_scheduling_taskinfo(int depth) { |
| 144 | ompt_task_info_t *info = NULL; |
| 145 | kmp_info_t *thr = ompt_get_thread(); |
| 146 | |
| 147 | if (thr) { |
| 148 | kmp_taskdata_t *taskdata = thr->th.th_current_task; |
| 149 | |
| 150 | ompt_lw_taskteam_t *lwt = NULL, |
| 151 | *next_lwt = LWT_FROM_TEAM(taskdata->td_team); |
| 152 | |
| 153 | while (depth > 0) { |
| 154 | // next lightweight team (if any) |
| 155 | if (lwt) |
| 156 | lwt = lwt->parent; |
| 157 | |
| 158 | // next heavyweight team (if any) after |
| 159 | // lightweight teams are exhausted |
| 160 | if (!lwt && taskdata) { |
| 161 | // first try scheduling parent (for explicit task scheduling) |
| 162 | if (taskdata->ompt_task_info.scheduling_parent) { |
| 163 | taskdata = taskdata->ompt_task_info.scheduling_parent; |
| 164 | } else if (next_lwt) { |
| 165 | lwt = next_lwt; |
| 166 | next_lwt = NULL; |
| 167 | } else { |
| 168 | // then go for implicit tasks |
| 169 | taskdata = taskdata->td_parent; |
| 170 | if (taskdata) { |
| 171 | next_lwt = LWT_FROM_TEAM(taskdata->td_team); |
| 172 | } |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 173 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 174 | } |
| 175 | depth--; |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 178 | if (lwt) { |
| 179 | info = &lwt->ompt_task_info; |
| 180 | } else if (taskdata) { |
| 181 | info = &taskdata->ompt_task_info; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | return info; |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 188 | //****************************************************************************** |
| 189 | // interface operations |
| 190 | //****************************************************************************** |
| 191 | |
| 192 | //---------------------------------------------------------- |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 193 | // thread support |
| 194 | //---------------------------------------------------------- |
| 195 | |
Joachim Protze | 82e94a5 | 2017-11-01 10:08:30 +0000 | [diff] [blame] | 196 | ompt_data_t *__ompt_get_thread_data_internal() { |
| 197 | if (__kmp_get_gtid() >= 0) { |
| 198 | kmp_info_t *thread = ompt_get_thread(); |
| 199 | if (thread == NULL) |
| 200 | return NULL; |
| 201 | return &(thread->th.ompt_thread_info.thread_data); |
| 202 | } |
| 203 | return NULL; |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | //---------------------------------------------------------- |
| 207 | // state support |
| 208 | //---------------------------------------------------------- |
| 209 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 210 | void __ompt_thread_assign_wait_id(void *variable) { |
Joachim Protze | 82e94a5 | 2017-11-01 10:08:30 +0000 | [diff] [blame] | 211 | kmp_info_t *ti = ompt_get_thread(); |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 212 | |
Joachim Protze | 0e0d6cd | 2018-12-18 08:52:30 +0000 | [diff] [blame] | 213 | ti->th.ompt_thread_info.wait_id = (ompt_wait_id_t)variable; |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Joachim Protze | 2b46d30 | 2019-01-15 15:36:53 +0000 | [diff] [blame] | 216 | int __ompt_get_state_internal(ompt_wait_id_t *omp_wait_id) { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 217 | kmp_info_t *ti = ompt_get_thread(); |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 218 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 219 | if (ti) { |
Joachim Protze | 4063613 | 2018-05-28 08:16:08 +0000 | [diff] [blame] | 220 | if (omp_wait_id) |
| 221 | *omp_wait_id = ti->th.ompt_thread_info.wait_id; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 222 | return ti->th.ompt_thread_info.state; |
| 223 | } |
Joachim Protze | 0e0d6cd | 2018-12-18 08:52:30 +0000 | [diff] [blame] | 224 | return ompt_state_undefined; |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 227 | //---------------------------------------------------------- |
| 228 | // parallel region support |
| 229 | //---------------------------------------------------------- |
| 230 | |
Joachim Protze | 82e94a5 | 2017-11-01 10:08:30 +0000 | [diff] [blame] | 231 | int __ompt_get_parallel_info_internal(int ancestor_level, |
| 232 | ompt_data_t **parallel_data, |
| 233 | int *team_size) { |
Joachim Protze | 1dc2afd | 2018-01-17 10:05:55 +0000 | [diff] [blame] | 234 | if (__kmp_get_gtid() >= 0) { |
| 235 | ompt_team_info_t *info; |
| 236 | if (team_size) { |
| 237 | info = __ompt_get_teaminfo(ancestor_level, team_size); |
| 238 | } else { |
| 239 | info = __ompt_get_teaminfo(ancestor_level, NULL); |
| 240 | } |
| 241 | if (parallel_data) { |
| 242 | *parallel_data = info ? &(info->parallel_data) : NULL; |
| 243 | } |
| 244 | return info ? 2 : 0; |
Joachim Protze | 82e94a5 | 2017-11-01 10:08:30 +0000 | [diff] [blame] | 245 | } else { |
Joachim Protze | 1dc2afd | 2018-01-17 10:05:55 +0000 | [diff] [blame] | 246 | return 0; |
Joachim Protze | 82e94a5 | 2017-11-01 10:08:30 +0000 | [diff] [blame] | 247 | } |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 250 | //---------------------------------------------------------- |
| 251 | // lightweight task team support |
| 252 | //---------------------------------------------------------- |
| 253 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 254 | void __ompt_lw_taskteam_init(ompt_lw_taskteam_t *lwt, kmp_info_t *thr, int gtid, |
Joachim Protze | 82e94a5 | 2017-11-01 10:08:30 +0000 | [diff] [blame] | 255 | ompt_data_t *ompt_pid, void *codeptr) { |
| 256 | // initialize parallel_data with input, return address to parallel_data on |
| 257 | // exit |
| 258 | lwt->ompt_team_info.parallel_data = *ompt_pid; |
| 259 | lwt->ompt_team_info.master_return_address = codeptr; |
| 260 | lwt->ompt_task_info.task_data.value = 0; |
Joachim Protze | 0e0d6cd | 2018-12-18 08:52:30 +0000 | [diff] [blame] | 261 | lwt->ompt_task_info.frame.enter_frame = ompt_data_none; |
| 262 | lwt->ompt_task_info.frame.exit_frame = ompt_data_none; |
Joachim Protze | 82e94a5 | 2017-11-01 10:08:30 +0000 | [diff] [blame] | 263 | lwt->ompt_task_info.scheduling_parent = NULL; |
| 264 | lwt->ompt_task_info.deps = NULL; |
| 265 | lwt->ompt_task_info.ndeps = 0; |
| 266 | lwt->heap = 0; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 267 | lwt->parent = 0; |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Joachim Protze | 82e94a5 | 2017-11-01 10:08:30 +0000 | [diff] [blame] | 270 | void __ompt_lw_taskteam_link(ompt_lw_taskteam_t *lwt, kmp_info_t *thr, |
| 271 | int on_heap) { |
| 272 | ompt_lw_taskteam_t *link_lwt = lwt; |
| 273 | if (thr->th.th_team->t.t_serialized > |
| 274 | 1) { // we already have a team, so link the new team and swap values |
| 275 | if (on_heap) { // the lw_taskteam cannot stay on stack, allocate it on heap |
| 276 | link_lwt = |
| 277 | (ompt_lw_taskteam_t *)__kmp_allocate(sizeof(ompt_lw_taskteam_t)); |
| 278 | } |
| 279 | link_lwt->heap = on_heap; |
| 280 | |
| 281 | // would be swap in the (on_stack) case. |
| 282 | ompt_team_info_t tmp_team = lwt->ompt_team_info; |
| 283 | link_lwt->ompt_team_info = *OMPT_CUR_TEAM_INFO(thr); |
| 284 | *OMPT_CUR_TEAM_INFO(thr) = tmp_team; |
| 285 | |
| 286 | ompt_task_info_t tmp_task = lwt->ompt_task_info; |
| 287 | link_lwt->ompt_task_info = *OMPT_CUR_TASK_INFO(thr); |
| 288 | *OMPT_CUR_TASK_INFO(thr) = tmp_task; |
| 289 | |
| 290 | // link the taskteam into the list of taskteams: |
| 291 | ompt_lw_taskteam_t *my_parent = |
| 292 | thr->th.th_team->t.ompt_serialized_team_info; |
| 293 | link_lwt->parent = my_parent; |
| 294 | thr->th.th_team->t.ompt_serialized_team_info = link_lwt; |
| 295 | } else { |
| 296 | // this is the first serialized team, so we just store the values in the |
| 297 | // team and drop the taskteam-object |
| 298 | *OMPT_CUR_TEAM_INFO(thr) = lwt->ompt_team_info; |
| 299 | *OMPT_CUR_TASK_INFO(thr) = lwt->ompt_task_info; |
| 300 | } |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Joachim Protze | 82e94a5 | 2017-11-01 10:08:30 +0000 | [diff] [blame] | 303 | void __ompt_lw_taskteam_unlink(kmp_info_t *thr) { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 304 | ompt_lw_taskteam_t *lwtask = thr->th.th_team->t.ompt_serialized_team_info; |
Joachim Protze | 82e94a5 | 2017-11-01 10:08:30 +0000 | [diff] [blame] | 305 | if (lwtask) { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 306 | thr->th.th_team->t.ompt_serialized_team_info = lwtask->parent; |
Joachim Protze | 82e94a5 | 2017-11-01 10:08:30 +0000 | [diff] [blame] | 307 | |
| 308 | ompt_team_info_t tmp_team = lwtask->ompt_team_info; |
| 309 | lwtask->ompt_team_info = *OMPT_CUR_TEAM_INFO(thr); |
| 310 | *OMPT_CUR_TEAM_INFO(thr) = tmp_team; |
| 311 | |
| 312 | ompt_task_info_t tmp_task = lwtask->ompt_task_info; |
| 313 | lwtask->ompt_task_info = *OMPT_CUR_TASK_INFO(thr); |
| 314 | *OMPT_CUR_TASK_INFO(thr) = tmp_task; |
| 315 | |
| 316 | if (lwtask->heap) { |
| 317 | __kmp_free(lwtask); |
| 318 | lwtask = NULL; |
| 319 | } |
| 320 | } |
| 321 | // return lwtask; |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 324 | //---------------------------------------------------------- |
| 325 | // task support |
| 326 | //---------------------------------------------------------- |
| 327 | |
Joachim Protze | 82e94a5 | 2017-11-01 10:08:30 +0000 | [diff] [blame] | 328 | int __ompt_get_task_info_internal(int ancestor_level, int *type, |
| 329 | ompt_data_t **task_data, |
Joachim Protze | 0e0d6cd | 2018-12-18 08:52:30 +0000 | [diff] [blame] | 330 | ompt_frame_t **task_frame, |
Joachim Protze | 82e94a5 | 2017-11-01 10:08:30 +0000 | [diff] [blame] | 331 | ompt_data_t **parallel_data, |
| 332 | int *thread_num) { |
Joachim Protze | 1dc2afd | 2018-01-17 10:05:55 +0000 | [diff] [blame] | 333 | if (__kmp_get_gtid() < 0) |
| 334 | return 0; |
| 335 | |
Joachim Protze | 82e94a5 | 2017-11-01 10:08:30 +0000 | [diff] [blame] | 336 | if (ancestor_level < 0) |
| 337 | return 0; |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 338 | |
Joachim Protze | 82e94a5 | 2017-11-01 10:08:30 +0000 | [diff] [blame] | 339 | // copied from __ompt_get_scheduling_taskinfo |
| 340 | ompt_task_info_t *info = NULL; |
| 341 | ompt_team_info_t *team_info = NULL; |
| 342 | kmp_info_t *thr = ompt_get_thread(); |
Joachim Protze | 4a73ae1 | 2018-07-02 09:13:24 +0000 | [diff] [blame] | 343 | int level = ancestor_level; |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 344 | |
Joachim Protze | 82e94a5 | 2017-11-01 10:08:30 +0000 | [diff] [blame] | 345 | if (thr) { |
| 346 | kmp_taskdata_t *taskdata = thr->th.th_current_task; |
| 347 | if (taskdata == NULL) |
| 348 | return 0; |
Joachim Protze | 4a73ae1 | 2018-07-02 09:13:24 +0000 | [diff] [blame] | 349 | kmp_team *team = thr->th.th_team, *prev_team = NULL; |
Joachim Protze | 82e94a5 | 2017-11-01 10:08:30 +0000 | [diff] [blame] | 350 | if (team == NULL) |
| 351 | return 0; |
| 352 | ompt_lw_taskteam_t *lwt = NULL, |
Joachim Protze | 4a73ae1 | 2018-07-02 09:13:24 +0000 | [diff] [blame] | 353 | *next_lwt = LWT_FROM_TEAM(taskdata->td_team), |
| 354 | *prev_lwt = NULL; |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 355 | |
Joachim Protze | 82e94a5 | 2017-11-01 10:08:30 +0000 | [diff] [blame] | 356 | while (ancestor_level > 0) { |
Joachim Protze | 4a73ae1 | 2018-07-02 09:13:24 +0000 | [diff] [blame] | 357 | // needed for thread_num |
| 358 | prev_team = team; |
| 359 | prev_lwt = lwt; |
Joachim Protze | 82e94a5 | 2017-11-01 10:08:30 +0000 | [diff] [blame] | 360 | // next lightweight team (if any) |
| 361 | if (lwt) |
| 362 | lwt = lwt->parent; |
| 363 | |
| 364 | // next heavyweight team (if any) after |
| 365 | // lightweight teams are exhausted |
| 366 | if (!lwt && taskdata) { |
| 367 | // first try scheduling parent (for explicit task scheduling) |
| 368 | if (taskdata->ompt_task_info.scheduling_parent) { |
| 369 | taskdata = taskdata->ompt_task_info.scheduling_parent; |
| 370 | } else if (next_lwt) { |
| 371 | lwt = next_lwt; |
| 372 | next_lwt = NULL; |
| 373 | } else { |
| 374 | // then go for implicit tasks |
| 375 | taskdata = taskdata->td_parent; |
| 376 | if (team == NULL) |
| 377 | return 0; |
| 378 | team = team->t.t_parent; |
| 379 | if (taskdata) { |
| 380 | next_lwt = LWT_FROM_TEAM(taskdata->td_team); |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | ancestor_level--; |
| 385 | } |
| 386 | |
| 387 | if (lwt) { |
| 388 | info = &lwt->ompt_task_info; |
| 389 | team_info = &lwt->ompt_team_info; |
| 390 | if (type) { |
| 391 | *type = ompt_task_implicit; |
| 392 | } |
| 393 | } else if (taskdata) { |
| 394 | info = &taskdata->ompt_task_info; |
| 395 | team_info = &team->t.ompt_team_info; |
| 396 | if (type) { |
| 397 | if (taskdata->td_parent) { |
| 398 | *type = (taskdata->td_flags.tasktype ? ompt_task_explicit |
| 399 | : ompt_task_implicit) | |
| 400 | TASK_TYPE_DETAILS_FORMAT(taskdata); |
| 401 | } else { |
| 402 | *type = ompt_task_initial; |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | if (task_data) { |
| 407 | *task_data = info ? &info->task_data : NULL; |
| 408 | } |
| 409 | if (task_frame) { |
| 410 | // OpenMP spec asks for the scheduling task to be returned. |
| 411 | *task_frame = info ? &info->frame : NULL; |
| 412 | } |
| 413 | if (parallel_data) { |
| 414 | *parallel_data = team_info ? &(team_info->parallel_data) : NULL; |
| 415 | } |
Joachim Protze | aa2022e | 2018-02-28 17:36:18 +0000 | [diff] [blame] | 416 | if (thread_num) { |
Joachim Protze | 4a73ae1 | 2018-07-02 09:13:24 +0000 | [diff] [blame] | 417 | if (level == 0) |
| 418 | *thread_num = __kmp_get_tid(); |
| 419 | else if (prev_lwt) |
| 420 | *thread_num = 0; |
| 421 | else |
| 422 | *thread_num = prev_team->t.t_master_tid; |
| 423 | // *thread_num = team->t.t_master_tid; |
Joachim Protze | aa2022e | 2018-02-28 17:36:18 +0000 | [diff] [blame] | 424 | } |
Joachim Protze | 82e94a5 | 2017-11-01 10:08:30 +0000 | [diff] [blame] | 425 | return info ? 2 : 0; |
| 426 | } |
| 427 | return 0; |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 430 | //---------------------------------------------------------- |
| 431 | // team support |
| 432 | //---------------------------------------------------------- |
| 433 | |
Joachim Protze | 82e94a5 | 2017-11-01 10:08:30 +0000 | [diff] [blame] | 434 | void __ompt_team_assign_id(kmp_team_t *team, ompt_data_t ompt_pid) { |
| 435 | team->t.ompt_team_info.parallel_data = ompt_pid; |
| 436 | } |
| 437 | |
| 438 | //---------------------------------------------------------- |
| 439 | // misc |
| 440 | //---------------------------------------------------------- |
| 441 | |
| 442 | static uint64_t __ompt_get_unique_id_internal() { |
| 443 | static uint64_t thread = 1; |
| 444 | static THREAD_LOCAL uint64_t ID = 0; |
| 445 | if (ID == 0) { |
| 446 | uint64_t new_thread = KMP_TEST_THEN_INC64((kmp_int64 *)&thread); |
| 447 | ID = new_thread << (sizeof(uint64_t) * 8 - OMPT_THREAD_ID_BITS); |
| 448 | } |
| 449 | return ++ID; |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 450 | } |
Jonathan Peyton | ad1ad7a | 2019-02-28 20:55:39 +0000 | [diff] [blame] | 451 | |
| 452 | ompt_sync_region_t __ompt_get_barrier_kind(enum barrier_type bt, |
| 453 | kmp_info_t *thr) { |
| 454 | if (bt == bs_forkjoin_barrier) |
| 455 | return ompt_sync_region_barrier_implicit; |
| 456 | |
| 457 | if (bt != bs_plain_barrier) |
| 458 | return ompt_sync_region_barrier_implementation; |
| 459 | |
| 460 | if (!thr->th.th_ident) |
| 461 | return ompt_sync_region_barrier; |
| 462 | |
| 463 | kmp_int32 flags = thr->th.th_ident->flags; |
| 464 | |
| 465 | if ((flags & KMP_IDENT_BARRIER_EXPL) != 0) |
| 466 | return ompt_sync_region_barrier_explicit; |
| 467 | |
| 468 | if ((flags & KMP_IDENT_BARRIER_IMPL) != 0) |
| 469 | return ompt_sync_region_barrier_implicit; |
| 470 | |
| 471 | return ompt_sync_region_barrier_implementation; |
| 472 | } |