1 /* 2 * kmp_tasking.cpp -- OpenMP 3.0 tasking support. 3 */ 4 5 //===----------------------------------------------------------------------===// 6 // 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 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "kmp.h" 14 #include "kmp_i18n.h" 15 #include "kmp_itt.h" 16 #include "kmp_stats.h" 17 #include "kmp_wait_release.h" 18 #include "kmp_taskdeps.h" 19 20 #if OMPT_SUPPORT 21 #include "ompt-specific.h" 22 #endif 23 24 #include "tsan_annotations.h" 25 26 /* forward declaration */ 27 static void __kmp_enable_tasking(kmp_task_team_t *task_team, 28 kmp_info_t *this_thr); 29 static void __kmp_alloc_task_deque(kmp_info_t *thread, 30 kmp_thread_data_t *thread_data); 31 static int __kmp_realloc_task_threads_data(kmp_info_t *thread, 32 kmp_task_team_t *task_team); 33 static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask); 34 35 #ifdef BUILD_TIED_TASK_STACK 36 37 // __kmp_trace_task_stack: print the tied tasks from the task stack in order 38 // from top do bottom 39 // 40 // gtid: global thread identifier for thread containing stack 41 // thread_data: thread data for task team thread containing stack 42 // threshold: value above which the trace statement triggers 43 // location: string identifying call site of this function (for trace) 44 static void __kmp_trace_task_stack(kmp_int32 gtid, 45 kmp_thread_data_t *thread_data, 46 int threshold, char *location) { 47 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks; 48 kmp_taskdata_t **stack_top = task_stack->ts_top; 49 kmp_int32 entries = task_stack->ts_entries; 50 kmp_taskdata_t *tied_task; 51 52 KA_TRACE( 53 threshold, 54 ("__kmp_trace_task_stack(start): location = %s, gtid = %d, entries = %d, " 55 "first_block = %p, stack_top = %p \n", 56 location, gtid, entries, task_stack->ts_first_block, stack_top)); 57 58 KMP_DEBUG_ASSERT(stack_top != NULL); 59 KMP_DEBUG_ASSERT(entries > 0); 60 61 while (entries != 0) { 62 KMP_DEBUG_ASSERT(stack_top != &task_stack->ts_first_block.sb_block[0]); 63 // fix up ts_top if we need to pop from previous block 64 if (entries & TASK_STACK_INDEX_MASK == 0) { 65 kmp_stack_block_t *stack_block = (kmp_stack_block_t *)(stack_top); 66 67 stack_block = stack_block->sb_prev; 68 stack_top = &stack_block->sb_block[TASK_STACK_BLOCK_SIZE]; 69 } 70 71 // finish bookkeeping 72 stack_top--; 73 entries--; 74 75 tied_task = *stack_top; 76 77 KMP_DEBUG_ASSERT(tied_task != NULL); 78 KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED); 79 80 KA_TRACE(threshold, 81 ("__kmp_trace_task_stack(%s): gtid=%d, entry=%d, " 82 "stack_top=%p, tied_task=%p\n", 83 location, gtid, entries, stack_top, tied_task)); 84 } 85 KMP_DEBUG_ASSERT(stack_top == &task_stack->ts_first_block.sb_block[0]); 86 87 KA_TRACE(threshold, 88 ("__kmp_trace_task_stack(exit): location = %s, gtid = %d\n", 89 location, gtid)); 90 } 91 92 // __kmp_init_task_stack: initialize the task stack for the first time 93 // after a thread_data structure is created. 94 // It should not be necessary to do this again (assuming the stack works). 95 // 96 // gtid: global thread identifier of calling thread 97 // thread_data: thread data for task team thread containing stack 98 static void __kmp_init_task_stack(kmp_int32 gtid, 99 kmp_thread_data_t *thread_data) { 100 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks; 101 kmp_stack_block_t *first_block; 102 103 // set up the first block of the stack 104 first_block = &task_stack->ts_first_block; 105 task_stack->ts_top = (kmp_taskdata_t **)first_block; 106 memset((void *)first_block, '\0', 107 TASK_STACK_BLOCK_SIZE * sizeof(kmp_taskdata_t *)); 108 109 // initialize the stack to be empty 110 task_stack->ts_entries = TASK_STACK_EMPTY; 111 first_block->sb_next = NULL; 112 first_block->sb_prev = NULL; 113 } 114 115 // __kmp_free_task_stack: free the task stack when thread_data is destroyed. 116 // 117 // gtid: global thread identifier for calling thread 118 // thread_data: thread info for thread containing stack 119 static void __kmp_free_task_stack(kmp_int32 gtid, 120 kmp_thread_data_t *thread_data) { 121 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks; 122 kmp_stack_block_t *stack_block = &task_stack->ts_first_block; 123 124 KMP_DEBUG_ASSERT(task_stack->ts_entries == TASK_STACK_EMPTY); 125 // free from the second block of the stack 126 while (stack_block != NULL) { 127 kmp_stack_block_t *next_block = (stack_block) ? stack_block->sb_next : NULL; 128 129 stack_block->sb_next = NULL; 130 stack_block->sb_prev = NULL; 131 if (stack_block != &task_stack->ts_first_block) { 132 __kmp_thread_free(thread, 133 stack_block); // free the block, if not the first 134 } 135 stack_block = next_block; 136 } 137 // initialize the stack to be empty 138 task_stack->ts_entries = 0; 139 task_stack->ts_top = NULL; 140 } 141 142 // __kmp_push_task_stack: Push the tied task onto the task stack. 143 // Grow the stack if necessary by allocating another block. 144 // 145 // gtid: global thread identifier for calling thread 146 // thread: thread info for thread containing stack 147 // tied_task: the task to push on the stack 148 static void __kmp_push_task_stack(kmp_int32 gtid, kmp_info_t *thread, 149 kmp_taskdata_t *tied_task) { 150 // GEH - need to consider what to do if tt_threads_data not allocated yet 151 kmp_thread_data_t *thread_data = 152 &thread->th.th_task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)]; 153 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks; 154 155 if (tied_task->td_flags.team_serial || tied_task->td_flags.tasking_ser) { 156 return; // Don't push anything on stack if team or team tasks are serialized 157 } 158 159 KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED); 160 KMP_DEBUG_ASSERT(task_stack->ts_top != NULL); 161 162 KA_TRACE(20, 163 ("__kmp_push_task_stack(enter): GTID: %d; THREAD: %p; TASK: %p\n", 164 gtid, thread, tied_task)); 165 // Store entry 166 *(task_stack->ts_top) = tied_task; 167 168 // Do bookkeeping for next push 169 task_stack->ts_top++; 170 task_stack->ts_entries++; 171 172 if (task_stack->ts_entries & TASK_STACK_INDEX_MASK == 0) { 173 // Find beginning of this task block 174 kmp_stack_block_t *stack_block = 175 (kmp_stack_block_t *)(task_stack->ts_top - TASK_STACK_BLOCK_SIZE); 176 177 // Check if we already have a block 178 if (stack_block->sb_next != 179 NULL) { // reset ts_top to beginning of next block 180 task_stack->ts_top = &stack_block->sb_next->sb_block[0]; 181 } else { // Alloc new block and link it up 182 kmp_stack_block_t *new_block = (kmp_stack_block_t *)__kmp_thread_calloc( 183 thread, sizeof(kmp_stack_block_t)); 184 185 task_stack->ts_top = &new_block->sb_block[0]; 186 stack_block->sb_next = new_block; 187 new_block->sb_prev = stack_block; 188 new_block->sb_next = NULL; 189 190 KA_TRACE( 191 30, 192 ("__kmp_push_task_stack(): GTID: %d; TASK: %p; Alloc new block: %p\n", 193 gtid, tied_task, new_block)); 194 } 195 } 196 KA_TRACE(20, ("__kmp_push_task_stack(exit): GTID: %d; TASK: %p\n", gtid, 197 tied_task)); 198 } 199 200 // __kmp_pop_task_stack: Pop the tied task from the task stack. Don't return 201 // the task, just check to make sure it matches the ending task passed in. 202 // 203 // gtid: global thread identifier for the calling thread 204 // thread: thread info structure containing stack 205 // tied_task: the task popped off the stack 206 // ending_task: the task that is ending (should match popped task) 207 static void __kmp_pop_task_stack(kmp_int32 gtid, kmp_info_t *thread, 208 kmp_taskdata_t *ending_task) { 209 // GEH - need to consider what to do if tt_threads_data not allocated yet 210 kmp_thread_data_t *thread_data = 211 &thread->th.th_task_team->tt_threads_data[__kmp_tid_from_gtid(gtid)]; 212 kmp_task_stack_t *task_stack = &thread_data->td.td_susp_tied_tasks; 213 kmp_taskdata_t *tied_task; 214 215 if (ending_task->td_flags.team_serial || ending_task->td_flags.tasking_ser) { 216 // Don't pop anything from stack if team or team tasks are serialized 217 return; 218 } 219 220 KMP_DEBUG_ASSERT(task_stack->ts_top != NULL); 221 KMP_DEBUG_ASSERT(task_stack->ts_entries > 0); 222 223 KA_TRACE(20, ("__kmp_pop_task_stack(enter): GTID: %d; THREAD: %p\n", gtid, 224 thread)); 225 226 // fix up ts_top if we need to pop from previous block 227 if (task_stack->ts_entries & TASK_STACK_INDEX_MASK == 0) { 228 kmp_stack_block_t *stack_block = (kmp_stack_block_t *)(task_stack->ts_top); 229 230 stack_block = stack_block->sb_prev; 231 task_stack->ts_top = &stack_block->sb_block[TASK_STACK_BLOCK_SIZE]; 232 } 233 234 // finish bookkeeping 235 task_stack->ts_top--; 236 task_stack->ts_entries--; 237 238 tied_task = *(task_stack->ts_top); 239 240 KMP_DEBUG_ASSERT(tied_task != NULL); 241 KMP_DEBUG_ASSERT(tied_task->td_flags.tasktype == TASK_TIED); 242 KMP_DEBUG_ASSERT(tied_task == ending_task); // If we built the stack correctly 243 244 KA_TRACE(20, ("__kmp_pop_task_stack(exit): GTID: %d; TASK: %p\n", gtid, 245 tied_task)); 246 return; 247 } 248 #endif /* BUILD_TIED_TASK_STACK */ 249 250 // returns 1 if new task is allowed to execute, 0 otherwise 251 // checks Task Scheduling constraint (if requested) and 252 // mutexinoutset dependencies if any 253 static bool __kmp_task_is_allowed(int gtid, const kmp_int32 is_constrained, 254 const kmp_taskdata_t *tasknew, 255 const kmp_taskdata_t *taskcurr) { 256 if (is_constrained && (tasknew->td_flags.tiedness == TASK_TIED)) { 257 // Check if the candidate obeys the Task Scheduling Constraints (TSC) 258 // only descendant of all deferred tied tasks can be scheduled, checking 259 // the last one is enough, as it in turn is the descendant of all others 260 kmp_taskdata_t *current = taskcurr->td_last_tied; 261 KMP_DEBUG_ASSERT(current != NULL); 262 // check if the task is not suspended on barrier 263 if (current->td_flags.tasktype == TASK_EXPLICIT || 264 current->td_taskwait_thread > 0) { // <= 0 on barrier 265 kmp_int32 level = current->td_level; 266 kmp_taskdata_t *parent = tasknew->td_parent; 267 while (parent != current && parent->td_level > level) { 268 // check generation up to the level of the current task 269 parent = parent->td_parent; 270 KMP_DEBUG_ASSERT(parent != NULL); 271 } 272 if (parent != current) 273 return false; 274 } 275 } 276 // Check mutexinoutset dependencies, acquire locks 277 kmp_depnode_t *node = tasknew->td_depnode; 278 if (UNLIKELY(node && (node->dn.mtx_num_locks > 0))) { 279 for (int i = 0; i < node->dn.mtx_num_locks; ++i) { 280 KMP_DEBUG_ASSERT(node->dn.mtx_locks[i] != NULL); 281 if (__kmp_test_lock(node->dn.mtx_locks[i], gtid)) 282 continue; 283 // could not get the lock, release previous locks 284 for (int j = i - 1; j >= 0; --j) 285 __kmp_release_lock(node->dn.mtx_locks[j], gtid); 286 return false; 287 } 288 // negative num_locks means all locks acquired successfully 289 node->dn.mtx_num_locks = -node->dn.mtx_num_locks; 290 } 291 return true; 292 } 293 294 // __kmp_realloc_task_deque: 295 // Re-allocates a task deque for a particular thread, copies the content from 296 // the old deque and adjusts the necessary data structures relating to the 297 // deque. This operation must be done with the deque_lock being held 298 static void __kmp_realloc_task_deque(kmp_info_t *thread, 299 kmp_thread_data_t *thread_data) { 300 kmp_int32 size = TASK_DEQUE_SIZE(thread_data->td); 301 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == size); 302 kmp_int32 new_size = 2 * size; 303 304 KE_TRACE(10, ("__kmp_realloc_task_deque: T#%d reallocating deque[from %d to " 305 "%d] for thread_data %p\n", 306 __kmp_gtid_from_thread(thread), size, new_size, thread_data)); 307 308 kmp_taskdata_t **new_deque = 309 (kmp_taskdata_t **)__kmp_allocate(new_size * sizeof(kmp_taskdata_t *)); 310 311 int i, j; 312 for (i = thread_data->td.td_deque_head, j = 0; j < size; 313 i = (i + 1) & TASK_DEQUE_MASK(thread_data->td), j++) 314 new_deque[j] = thread_data->td.td_deque[i]; 315 316 __kmp_free(thread_data->td.td_deque); 317 318 thread_data->td.td_deque_head = 0; 319 thread_data->td.td_deque_tail = size; 320 thread_data->td.td_deque = new_deque; 321 thread_data->td.td_deque_size = new_size; 322 } 323 324 // __kmp_push_task: Add a task to the thread's deque 325 static kmp_int32 __kmp_push_task(kmp_int32 gtid, kmp_task_t *task) { 326 kmp_info_t *thread = __kmp_threads[gtid]; 327 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task); 328 329 if (taskdata->td_flags.hidden_helper) { 330 gtid = KMP_GTID_TO_SHADOW_GTID(gtid); 331 thread = __kmp_threads[gtid]; 332 } 333 334 kmp_task_team_t *task_team = thread->th.th_task_team; 335 kmp_int32 tid = __kmp_tid_from_gtid(gtid); 336 kmp_thread_data_t *thread_data; 337 338 KA_TRACE(20, 339 ("__kmp_push_task: T#%d trying to push task %p.\n", gtid, taskdata)); 340 341 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) { 342 // untied task needs to increment counter so that the task structure is not 343 // freed prematurely 344 kmp_int32 counter = 1 + KMP_ATOMIC_INC(&taskdata->td_untied_count); 345 KMP_DEBUG_USE_VAR(counter); 346 KA_TRACE( 347 20, 348 ("__kmp_push_task: T#%d untied_count (%d) incremented for task %p\n", 349 gtid, counter, taskdata)); 350 } 351 352 // The first check avoids building task_team thread data if serialized 353 if (UNLIKELY(taskdata->td_flags.task_serial)) { 354 KA_TRACE(20, ("__kmp_push_task: T#%d team serialized; returning " 355 "TASK_NOT_PUSHED for task %p\n", 356 gtid, taskdata)); 357 return TASK_NOT_PUSHED; 358 } 359 360 // Now that serialized tasks have returned, we can assume that we are not in 361 // immediate exec mode 362 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec); 363 if (UNLIKELY(!KMP_TASKING_ENABLED(task_team))) { 364 __kmp_enable_tasking(task_team, thread); 365 } 366 KMP_DEBUG_ASSERT(TCR_4(task_team->tt.tt_found_tasks) == TRUE); 367 KMP_DEBUG_ASSERT(TCR_PTR(task_team->tt.tt_threads_data) != NULL); 368 369 // Find tasking deque specific to encountering thread 370 thread_data = &task_team->tt.tt_threads_data[tid]; 371 372 // No lock needed since only owner can allocate. If the task is hidden_helper, 373 // we don't need it either because we have initialized the dequeue for hidden 374 // helper thread data. 375 if (UNLIKELY(thread_data->td.td_deque == NULL)) { 376 __kmp_alloc_task_deque(thread, thread_data); 377 } 378 379 int locked = 0; 380 // Check if deque is full 381 if (TCR_4(thread_data->td.td_deque_ntasks) >= 382 TASK_DEQUE_SIZE(thread_data->td)) { 383 if (__kmp_enable_task_throttling && 384 __kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata, 385 thread->th.th_current_task)) { 386 KA_TRACE(20, ("__kmp_push_task: T#%d deque is full; returning " 387 "TASK_NOT_PUSHED for task %p\n", 388 gtid, taskdata)); 389 return TASK_NOT_PUSHED; 390 } else { 391 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock); 392 locked = 1; 393 if (TCR_4(thread_data->td.td_deque_ntasks) >= 394 TASK_DEQUE_SIZE(thread_data->td)) { 395 // expand deque to push the task which is not allowed to execute 396 __kmp_realloc_task_deque(thread, thread_data); 397 } 398 } 399 } 400 // Lock the deque for the task push operation 401 if (!locked) { 402 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock); 403 // Need to recheck as we can get a proxy task from thread outside of OpenMP 404 if (TCR_4(thread_data->td.td_deque_ntasks) >= 405 TASK_DEQUE_SIZE(thread_data->td)) { 406 if (__kmp_enable_task_throttling && 407 __kmp_task_is_allowed(gtid, __kmp_task_stealing_constraint, taskdata, 408 thread->th.th_current_task)) { 409 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock); 410 KA_TRACE(20, ("__kmp_push_task: T#%d deque is full on 2nd check; " 411 "returning TASK_NOT_PUSHED for task %p\n", 412 gtid, taskdata)); 413 return TASK_NOT_PUSHED; 414 } else { 415 // expand deque to push the task which is not allowed to execute 416 __kmp_realloc_task_deque(thread, thread_data); 417 } 418 } 419 } 420 // Must have room since no thread can add tasks but calling thread 421 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) < 422 TASK_DEQUE_SIZE(thread_data->td)); 423 424 thread_data->td.td_deque[thread_data->td.td_deque_tail] = 425 taskdata; // Push taskdata 426 // Wrap index. 427 thread_data->td.td_deque_tail = 428 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td); 429 TCW_4(thread_data->td.td_deque_ntasks, 430 TCR_4(thread_data->td.td_deque_ntasks) + 1); // Adjust task count 431 KMP_FSYNC_RELEASING(thread->th.th_current_task); // releasing self 432 KMP_FSYNC_RELEASING(taskdata); // releasing child 433 KA_TRACE(20, ("__kmp_push_task: T#%d returning TASK_SUCCESSFULLY_PUSHED: " 434 "task=%p ntasks=%d head=%u tail=%u\n", 435 gtid, taskdata, thread_data->td.td_deque_ntasks, 436 thread_data->td.td_deque_head, thread_data->td.td_deque_tail)); 437 438 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock); 439 440 // Signal one worker thread to execute the task 441 if (taskdata->td_flags.hidden_helper) { 442 // Wake hidden helper threads up if they're sleeping 443 __kmp_hidden_helper_worker_thread_signal(); 444 } 445 446 return TASK_SUCCESSFULLY_PUSHED; 447 } 448 449 // __kmp_pop_current_task_from_thread: set up current task from called thread 450 // when team ends 451 // 452 // this_thr: thread structure to set current_task in. 453 void __kmp_pop_current_task_from_thread(kmp_info_t *this_thr) { 454 KF_TRACE(10, ("__kmp_pop_current_task_from_thread(enter): T#%d " 455 "this_thread=%p, curtask=%p, " 456 "curtask_parent=%p\n", 457 0, this_thr, this_thr->th.th_current_task, 458 this_thr->th.th_current_task->td_parent)); 459 460 this_thr->th.th_current_task = this_thr->th.th_current_task->td_parent; 461 462 KF_TRACE(10, ("__kmp_pop_current_task_from_thread(exit): T#%d " 463 "this_thread=%p, curtask=%p, " 464 "curtask_parent=%p\n", 465 0, this_thr, this_thr->th.th_current_task, 466 this_thr->th.th_current_task->td_parent)); 467 } 468 469 // __kmp_push_current_task_to_thread: set up current task in called thread for a 470 // new team 471 // 472 // this_thr: thread structure to set up 473 // team: team for implicit task data 474 // tid: thread within team to set up 475 void __kmp_push_current_task_to_thread(kmp_info_t *this_thr, kmp_team_t *team, 476 int tid) { 477 // current task of the thread is a parent of the new just created implicit 478 // tasks of new team 479 KF_TRACE(10, ("__kmp_push_current_task_to_thread(enter): T#%d this_thread=%p " 480 "curtask=%p " 481 "parent_task=%p\n", 482 tid, this_thr, this_thr->th.th_current_task, 483 team->t.t_implicit_task_taskdata[tid].td_parent)); 484 485 KMP_DEBUG_ASSERT(this_thr != NULL); 486 487 if (tid == 0) { 488 if (this_thr->th.th_current_task != &team->t.t_implicit_task_taskdata[0]) { 489 team->t.t_implicit_task_taskdata[0].td_parent = 490 this_thr->th.th_current_task; 491 this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[0]; 492 } 493 } else { 494 team->t.t_implicit_task_taskdata[tid].td_parent = 495 team->t.t_implicit_task_taskdata[0].td_parent; 496 this_thr->th.th_current_task = &team->t.t_implicit_task_taskdata[tid]; 497 } 498 499 KF_TRACE(10, ("__kmp_push_current_task_to_thread(exit): T#%d this_thread=%p " 500 "curtask=%p " 501 "parent_task=%p\n", 502 tid, this_thr, this_thr->th.th_current_task, 503 team->t.t_implicit_task_taskdata[tid].td_parent)); 504 } 505 506 // __kmp_task_start: bookkeeping for a task starting execution 507 // 508 // GTID: global thread id of calling thread 509 // task: task starting execution 510 // current_task: task suspending 511 static void __kmp_task_start(kmp_int32 gtid, kmp_task_t *task, 512 kmp_taskdata_t *current_task) { 513 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task); 514 kmp_info_t *thread = __kmp_threads[gtid]; 515 516 KA_TRACE(10, 517 ("__kmp_task_start(enter): T#%d starting task %p: current_task=%p\n", 518 gtid, taskdata, current_task)); 519 520 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT); 521 522 // mark currently executing task as suspended 523 // TODO: GEH - make sure root team implicit task is initialized properly. 524 // KMP_DEBUG_ASSERT( current_task -> td_flags.executing == 1 ); 525 current_task->td_flags.executing = 0; 526 527 // Add task to stack if tied 528 #ifdef BUILD_TIED_TASK_STACK 529 if (taskdata->td_flags.tiedness == TASK_TIED) { 530 __kmp_push_task_stack(gtid, thread, taskdata); 531 } 532 #endif /* BUILD_TIED_TASK_STACK */ 533 534 // mark starting task as executing and as current task 535 thread->th.th_current_task = taskdata; 536 537 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 0 || 538 taskdata->td_flags.tiedness == TASK_UNTIED); 539 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0 || 540 taskdata->td_flags.tiedness == TASK_UNTIED); 541 taskdata->td_flags.started = 1; 542 taskdata->td_flags.executing = 1; 543 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0); 544 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0); 545 546 // GEH TODO: shouldn't we pass some sort of location identifier here? 547 // APT: yes, we will pass location here. 548 // need to store current thread state (in a thread or taskdata structure) 549 // before setting work_state, otherwise wrong state is set after end of task 550 551 KA_TRACE(10, ("__kmp_task_start(exit): T#%d task=%p\n", gtid, taskdata)); 552 553 return; 554 } 555 556 #if OMPT_SUPPORT 557 //------------------------------------------------------------------------------ 558 // __ompt_task_init: 559 // Initialize OMPT fields maintained by a task. This will only be called after 560 // ompt_start_tool, so we already know whether ompt is enabled or not. 561 562 static inline void __ompt_task_init(kmp_taskdata_t *task, int tid) { 563 // The calls to __ompt_task_init already have the ompt_enabled condition. 564 task->ompt_task_info.task_data.value = 0; 565 task->ompt_task_info.frame.exit_frame = ompt_data_none; 566 task->ompt_task_info.frame.enter_frame = ompt_data_none; 567 task->ompt_task_info.frame.exit_frame_flags = ompt_frame_runtime | ompt_frame_framepointer; 568 task->ompt_task_info.frame.enter_frame_flags = ompt_frame_runtime | ompt_frame_framepointer; 569 } 570 571 // __ompt_task_start: 572 // Build and trigger task-begin event 573 static inline void __ompt_task_start(kmp_task_t *task, 574 kmp_taskdata_t *current_task, 575 kmp_int32 gtid) { 576 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task); 577 ompt_task_status_t status = ompt_task_switch; 578 if (__kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded) { 579 status = ompt_task_yield; 580 __kmp_threads[gtid]->th.ompt_thread_info.ompt_task_yielded = 0; 581 } 582 /* let OMPT know that we're about to run this task */ 583 if (ompt_enabled.ompt_callback_task_schedule) { 584 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)( 585 &(current_task->ompt_task_info.task_data), status, 586 &(taskdata->ompt_task_info.task_data)); 587 } 588 taskdata->ompt_task_info.scheduling_parent = current_task; 589 } 590 591 // __ompt_task_finish: 592 // Build and trigger final task-schedule event 593 static inline void __ompt_task_finish(kmp_task_t *task, 594 kmp_taskdata_t *resumed_task, 595 ompt_task_status_t status) { 596 if (ompt_enabled.ompt_callback_task_schedule) { 597 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task); 598 if (__kmp_omp_cancellation && taskdata->td_taskgroup && 599 taskdata->td_taskgroup->cancel_request == cancel_taskgroup) { 600 status = ompt_task_cancel; 601 } 602 603 /* let OMPT know that we're returning to the callee task */ 604 ompt_callbacks.ompt_callback(ompt_callback_task_schedule)( 605 &(taskdata->ompt_task_info.task_data), status, 606 (resumed_task ? &(resumed_task->ompt_task_info.task_data) : NULL)); 607 } 608 } 609 #endif 610 611 template <bool ompt> 612 static void __kmpc_omp_task_begin_if0_template(ident_t *loc_ref, kmp_int32 gtid, 613 kmp_task_t *task, 614 void *frame_address, 615 void *return_address) { 616 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task); 617 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task; 618 619 KA_TRACE(10, ("__kmpc_omp_task_begin_if0(enter): T#%d loc=%p task=%p " 620 "current_task=%p\n", 621 gtid, loc_ref, taskdata, current_task)); 622 623 if (taskdata->td_flags.tiedness == TASK_UNTIED) { 624 // untied task needs to increment counter so that the task structure is not 625 // freed prematurely 626 kmp_int32 counter = 1 + KMP_ATOMIC_INC(&taskdata->td_untied_count); 627 KMP_DEBUG_USE_VAR(counter); 628 KA_TRACE(20, ("__kmpc_omp_task_begin_if0: T#%d untied_count (%d) " 629 "incremented for task %p\n", 630 gtid, counter, taskdata)); 631 } 632 633 taskdata->td_flags.task_serial = 634 1; // Execute this task immediately, not deferred. 635 __kmp_task_start(gtid, task, current_task); 636 637 #if OMPT_SUPPORT 638 if (ompt) { 639 if (current_task->ompt_task_info.frame.enter_frame.ptr == NULL) { 640 current_task->ompt_task_info.frame.enter_frame.ptr = 641 taskdata->ompt_task_info.frame.exit_frame.ptr = frame_address; 642 current_task->ompt_task_info.frame.enter_frame_flags = 643 taskdata->ompt_task_info.frame.exit_frame_flags = ompt_frame_application | ompt_frame_framepointer; 644 } 645 if (ompt_enabled.ompt_callback_task_create) { 646 ompt_task_info_t *parent_info = &(current_task->ompt_task_info); 647 ompt_callbacks.ompt_callback(ompt_callback_task_create)( 648 &(parent_info->task_data), &(parent_info->frame), 649 &(taskdata->ompt_task_info.task_data), 650 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(taskdata), 0, 651 return_address); 652 } 653 __ompt_task_start(task, current_task, gtid); 654 } 655 #endif // OMPT_SUPPORT 656 657 KA_TRACE(10, ("__kmpc_omp_task_begin_if0(exit): T#%d loc=%p task=%p,\n", gtid, 658 loc_ref, taskdata)); 659 } 660 661 #if OMPT_SUPPORT 662 OMPT_NOINLINE 663 static void __kmpc_omp_task_begin_if0_ompt(ident_t *loc_ref, kmp_int32 gtid, 664 kmp_task_t *task, 665 void *frame_address, 666 void *return_address) { 667 __kmpc_omp_task_begin_if0_template<true>(loc_ref, gtid, task, frame_address, 668 return_address); 669 } 670 #endif // OMPT_SUPPORT 671 672 // __kmpc_omp_task_begin_if0: report that a given serialized task has started 673 // execution 674 // 675 // loc_ref: source location information; points to beginning of task block. 676 // gtid: global thread number. 677 // task: task thunk for the started task. 678 void __kmpc_omp_task_begin_if0(ident_t *loc_ref, kmp_int32 gtid, 679 kmp_task_t *task) { 680 #if OMPT_SUPPORT 681 if (UNLIKELY(ompt_enabled.enabled)) { 682 OMPT_STORE_RETURN_ADDRESS(gtid); 683 __kmpc_omp_task_begin_if0_ompt(loc_ref, gtid, task, 684 OMPT_GET_FRAME_ADDRESS(1), 685 OMPT_LOAD_RETURN_ADDRESS(gtid)); 686 return; 687 } 688 #endif 689 __kmpc_omp_task_begin_if0_template<false>(loc_ref, gtid, task, NULL, NULL); 690 } 691 692 #ifdef TASK_UNUSED 693 // __kmpc_omp_task_begin: report that a given task has started execution 694 // NEVER GENERATED BY COMPILER, DEPRECATED!!! 695 void __kmpc_omp_task_begin(ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *task) { 696 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task; 697 698 KA_TRACE( 699 10, 700 ("__kmpc_omp_task_begin(enter): T#%d loc=%p task=%p current_task=%p\n", 701 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task), current_task)); 702 703 __kmp_task_start(gtid, task, current_task); 704 705 KA_TRACE(10, ("__kmpc_omp_task_begin(exit): T#%d loc=%p task=%p,\n", gtid, 706 loc_ref, KMP_TASK_TO_TASKDATA(task))); 707 return; 708 } 709 #endif // TASK_UNUSED 710 711 // __kmp_free_task: free the current task space and the space for shareds 712 // 713 // gtid: Global thread ID of calling thread 714 // taskdata: task to free 715 // thread: thread data structure of caller 716 static void __kmp_free_task(kmp_int32 gtid, kmp_taskdata_t *taskdata, 717 kmp_info_t *thread) { 718 KA_TRACE(30, ("__kmp_free_task: T#%d freeing data from task %p\n", gtid, 719 taskdata)); 720 721 // Check to make sure all flags and counters have the correct values 722 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT); 723 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 0); 724 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 1); 725 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0); 726 KMP_DEBUG_ASSERT(taskdata->td_allocated_child_tasks == 0 || 727 taskdata->td_flags.task_serial == 1); 728 KMP_DEBUG_ASSERT(taskdata->td_incomplete_child_tasks == 0); 729 730 taskdata->td_flags.freed = 1; 731 ANNOTATE_HAPPENS_BEFORE(taskdata); 732 // deallocate the taskdata and shared variable blocks associated with this task 733 #if USE_FAST_MEMORY 734 __kmp_fast_free(thread, taskdata); 735 #else /* ! USE_FAST_MEMORY */ 736 __kmp_thread_free(thread, taskdata); 737 #endif 738 KA_TRACE(20, ("__kmp_free_task: T#%d freed task %p\n", gtid, taskdata)); 739 } 740 741 // __kmp_free_task_and_ancestors: free the current task and ancestors without 742 // children 743 // 744 // gtid: Global thread ID of calling thread 745 // taskdata: task to free 746 // thread: thread data structure of caller 747 static void __kmp_free_task_and_ancestors(kmp_int32 gtid, 748 kmp_taskdata_t *taskdata, 749 kmp_info_t *thread) { 750 // Proxy tasks must always be allowed to free their parents 751 // because they can be run in background even in serial mode. 752 kmp_int32 team_serial = 753 (taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) && 754 !taskdata->td_flags.proxy; 755 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT); 756 757 kmp_int32 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1; 758 KMP_DEBUG_ASSERT(children >= 0); 759 760 // Now, go up the ancestor tree to see if any ancestors can now be freed. 761 while (children == 0) { 762 kmp_taskdata_t *parent_taskdata = taskdata->td_parent; 763 764 KA_TRACE(20, ("__kmp_free_task_and_ancestors(enter): T#%d task %p complete " 765 "and freeing itself\n", 766 gtid, taskdata)); 767 768 // --- Deallocate my ancestor task --- 769 __kmp_free_task(gtid, taskdata, thread); 770 771 taskdata = parent_taskdata; 772 773 if (team_serial) 774 return; 775 // Stop checking ancestors at implicit task instead of walking up ancestor 776 // tree to avoid premature deallocation of ancestors. 777 if (taskdata->td_flags.tasktype == TASK_IMPLICIT) { 778 if (taskdata->td_dephash) { // do we need to cleanup dephash? 779 int children = KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks); 780 kmp_tasking_flags_t flags_old = taskdata->td_flags; 781 if (children == 0 && flags_old.complete == 1) { 782 kmp_tasking_flags_t flags_new = flags_old; 783 flags_new.complete = 0; 784 if (KMP_COMPARE_AND_STORE_ACQ32( 785 RCAST(kmp_int32 *, &taskdata->td_flags), 786 *RCAST(kmp_int32 *, &flags_old), 787 *RCAST(kmp_int32 *, &flags_new))) { 788 KA_TRACE(100, ("__kmp_free_task_and_ancestors: T#%d cleans " 789 "dephash of implicit task %p\n", 790 gtid, taskdata)); 791 // cleanup dephash of finished implicit task 792 __kmp_dephash_free_entries(thread, taskdata->td_dephash); 793 } 794 } 795 } 796 return; 797 } 798 // Predecrement simulated by "- 1" calculation 799 children = KMP_ATOMIC_DEC(&taskdata->td_allocated_child_tasks) - 1; 800 KMP_DEBUG_ASSERT(children >= 0); 801 } 802 803 KA_TRACE( 804 20, ("__kmp_free_task_and_ancestors(exit): T#%d task %p has %d children; " 805 "not freeing it yet\n", 806 gtid, taskdata, children)); 807 } 808 809 // __kmp_task_finish: bookkeeping to do when a task finishes execution 810 // 811 // gtid: global thread ID for calling thread 812 // task: task to be finished 813 // resumed_task: task to be resumed. (may be NULL if task is serialized) 814 // 815 // template<ompt>: effectively ompt_enabled.enabled!=0 816 // the version with ompt=false is inlined, allowing to optimize away all ompt 817 // code in this case 818 template <bool ompt> 819 static void __kmp_task_finish(kmp_int32 gtid, kmp_task_t *task, 820 kmp_taskdata_t *resumed_task) { 821 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task); 822 kmp_info_t *thread = __kmp_threads[gtid]; 823 kmp_task_team_t *task_team = 824 thread->th.th_task_team; // might be NULL for serial teams... 825 kmp_int32 children = 0; 826 827 KA_TRACE(10, ("__kmp_task_finish(enter): T#%d finishing task %p and resuming " 828 "task %p\n", 829 gtid, taskdata, resumed_task)); 830 831 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT); 832 833 // Pop task from stack if tied 834 #ifdef BUILD_TIED_TASK_STACK 835 if (taskdata->td_flags.tiedness == TASK_TIED) { 836 __kmp_pop_task_stack(gtid, thread, taskdata); 837 } 838 #endif /* BUILD_TIED_TASK_STACK */ 839 840 if (UNLIKELY(taskdata->td_flags.tiedness == TASK_UNTIED)) { 841 // untied task needs to check the counter so that the task structure is not 842 // freed prematurely 843 kmp_int32 counter = KMP_ATOMIC_DEC(&taskdata->td_untied_count) - 1; 844 KA_TRACE( 845 20, 846 ("__kmp_task_finish: T#%d untied_count (%d) decremented for task %p\n", 847 gtid, counter, taskdata)); 848 if (counter > 0) { 849 // untied task is not done, to be continued possibly by other thread, do 850 // not free it now 851 if (resumed_task == NULL) { 852 KMP_DEBUG_ASSERT(taskdata->td_flags.task_serial); 853 resumed_task = taskdata->td_parent; // In a serialized task, the resumed 854 // task is the parent 855 } 856 thread->th.th_current_task = resumed_task; // restore current_task 857 resumed_task->td_flags.executing = 1; // resume previous task 858 KA_TRACE(10, ("__kmp_task_finish(exit): T#%d partially done task %p, " 859 "resuming task %p\n", 860 gtid, taskdata, resumed_task)); 861 return; 862 } 863 } 864 865 // bookkeeping for resuming task: 866 // GEH - note tasking_ser => task_serial 867 KMP_DEBUG_ASSERT( 868 (taskdata->td_flags.tasking_ser || taskdata->td_flags.task_serial) == 869 taskdata->td_flags.task_serial); 870 if (taskdata->td_flags.task_serial) { 871 if (resumed_task == NULL) { 872 resumed_task = taskdata->td_parent; // In a serialized task, the resumed 873 // task is the parent 874 } 875 } else { 876 KMP_DEBUG_ASSERT(resumed_task != 877 NULL); // verify that resumed task is passed as argument 878 } 879 880 /* If the tasks' destructor thunk flag has been set, we need to invoke the 881 destructor thunk that has been generated by the compiler. The code is 882 placed here, since at this point other tasks might have been released 883 hence overlapping the destructor invocations with some other work in the 884 released tasks. The OpenMP spec is not specific on when the destructors 885 are invoked, so we should be free to choose. */ 886 if (taskdata->td_flags.destructors_thunk) { 887 kmp_routine_entry_t destr_thunk = task->data1.destructors; 888 KMP_ASSERT(destr_thunk); 889 destr_thunk(gtid, task); 890 } 891 892 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0); 893 KMP_DEBUG_ASSERT(taskdata->td_flags.started == 1); 894 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0); 895 896 bool detach = false; 897 if (taskdata->td_flags.detachable == TASK_DETACHABLE) { 898 if (taskdata->td_allow_completion_event.type == 899 KMP_EVENT_ALLOW_COMPLETION) { 900 // event hasn't been fulfilled yet. Try to detach task. 901 __kmp_acquire_tas_lock(&taskdata->td_allow_completion_event.lock, gtid); 902 if (taskdata->td_allow_completion_event.type == 903 KMP_EVENT_ALLOW_COMPLETION) { 904 // task finished execution 905 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1); 906 taskdata->td_flags.executing = 0; // suspend the finishing task 907 908 #if OMPT_SUPPORT 909 // For a detached task, which is not completed, we switch back 910 // the omp_fulfill_event signals completion 911 // locking is necessary to avoid a race with ompt_task_late_fulfill 912 if (ompt) 913 __ompt_task_finish(task, resumed_task, ompt_task_detach); 914 #endif 915 916 // no access to taskdata after this point! 917 // __kmp_fulfill_event might free taskdata at any time from now 918 919 taskdata->td_flags.proxy = TASK_PROXY; // proxify! 920 detach = true; 921 } 922 __kmp_release_tas_lock(&taskdata->td_allow_completion_event.lock, gtid); 923 } 924 } 925 926 if (!detach) { 927 taskdata->td_flags.complete = 1; // mark the task as completed 928 929 #if OMPT_SUPPORT 930 // This is not a detached task, we are done here 931 if (ompt) 932 __ompt_task_finish(task, resumed_task, ompt_task_complete); 933 #endif 934 935 if (taskdata->td_flags.hidden_helper) { 936 KMP_DEBUG_ASSERT(taskdata->td_parent_task_team); 937 KMP_ATOMIC_DEC( 938 &taskdata->td_parent_task_team->tt.tt_unfinished_hidden_helper_tasks); 939 } 940 941 // Only need to keep track of count if team parallel and tasking not 942 // serialized, or task is detachable and event has already been fulfilled 943 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser) || 944 taskdata->td_flags.detachable == TASK_DETACHABLE) { 945 // Predecrement simulated by "- 1" calculation 946 children = 947 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks) - 1; 948 KMP_DEBUG_ASSERT(children >= 0); 949 if (taskdata->td_taskgroup) 950 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count); 951 __kmp_release_deps(gtid, taskdata); 952 } else if (task_team && task_team->tt.tt_found_proxy_tasks) { 953 // if we found proxy tasks there could exist a dependency chain 954 // with the proxy task as origin 955 __kmp_release_deps(gtid, taskdata); 956 } 957 // td_flags.executing must be marked as 0 after __kmp_release_deps has been 958 // called. Othertwise, if a task is executed immediately from the 959 // release_deps code, the flag will be reset to 1 again by this same 960 // function 961 KMP_DEBUG_ASSERT(taskdata->td_flags.executing == 1); 962 taskdata->td_flags.executing = 0; // suspend the finishing task 963 } 964 965 966 KA_TRACE( 967 20, ("__kmp_task_finish: T#%d finished task %p, %d incomplete children\n", 968 gtid, taskdata, children)); 969 970 // Free this task and then ancestor tasks if they have no children. 971 // Restore th_current_task first as suggested by John: 972 // johnmc: if an asynchronous inquiry peers into the runtime system 973 // it doesn't see the freed task as the current task. 974 thread->th.th_current_task = resumed_task; 975 if (!detach) 976 __kmp_free_task_and_ancestors(gtid, taskdata, thread); 977 978 // TODO: GEH - make sure root team implicit task is initialized properly. 979 // KMP_DEBUG_ASSERT( resumed_task->td_flags.executing == 0 ); 980 resumed_task->td_flags.executing = 1; // resume previous task 981 982 KA_TRACE( 983 10, ("__kmp_task_finish(exit): T#%d finished task %p, resuming task %p\n", 984 gtid, taskdata, resumed_task)); 985 986 return; 987 } 988 989 template <bool ompt> 990 static void __kmpc_omp_task_complete_if0_template(ident_t *loc_ref, 991 kmp_int32 gtid, 992 kmp_task_t *task) { 993 KA_TRACE(10, ("__kmpc_omp_task_complete_if0(enter): T#%d loc=%p task=%p\n", 994 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task))); 995 __kmp_assert_valid_gtid(gtid); 996 // this routine will provide task to resume 997 __kmp_task_finish<ompt>(gtid, task, NULL); 998 999 KA_TRACE(10, ("__kmpc_omp_task_complete_if0(exit): T#%d loc=%p task=%p\n", 1000 gtid, loc_ref, KMP_TASK_TO_TASKDATA(task))); 1001 1002 #if OMPT_SUPPORT 1003 if (ompt) { 1004 ompt_frame_t *ompt_frame; 1005 __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL); 1006 ompt_frame->enter_frame = ompt_data_none; 1007 ompt_frame->enter_frame_flags = ompt_frame_runtime | ompt_frame_framepointer; 1008 } 1009 #endif 1010 1011 return; 1012 } 1013 1014 #if OMPT_SUPPORT 1015 OMPT_NOINLINE 1016 void __kmpc_omp_task_complete_if0_ompt(ident_t *loc_ref, kmp_int32 gtid, 1017 kmp_task_t *task) { 1018 __kmpc_omp_task_complete_if0_template<true>(loc_ref, gtid, task); 1019 } 1020 #endif // OMPT_SUPPORT 1021 1022 // __kmpc_omp_task_complete_if0: report that a task has completed execution 1023 // 1024 // loc_ref: source location information; points to end of task block. 1025 // gtid: global thread number. 1026 // task: task thunk for the completed task. 1027 void __kmpc_omp_task_complete_if0(ident_t *loc_ref, kmp_int32 gtid, 1028 kmp_task_t *task) { 1029 #if OMPT_SUPPORT 1030 if (UNLIKELY(ompt_enabled.enabled)) { 1031 __kmpc_omp_task_complete_if0_ompt(loc_ref, gtid, task); 1032 return; 1033 } 1034 #endif 1035 __kmpc_omp_task_complete_if0_template<false>(loc_ref, gtid, task); 1036 } 1037 1038 #ifdef TASK_UNUSED 1039 // __kmpc_omp_task_complete: report that a task has completed execution 1040 // NEVER GENERATED BY COMPILER, DEPRECATED!!! 1041 void __kmpc_omp_task_complete(ident_t *loc_ref, kmp_int32 gtid, 1042 kmp_task_t *task) { 1043 KA_TRACE(10, ("__kmpc_omp_task_complete(enter): T#%d loc=%p task=%p\n", gtid, 1044 loc_ref, KMP_TASK_TO_TASKDATA(task))); 1045 1046 __kmp_task_finish<false>(gtid, task, 1047 NULL); // Not sure how to find task to resume 1048 1049 KA_TRACE(10, ("__kmpc_omp_task_complete(exit): T#%d loc=%p task=%p\n", gtid, 1050 loc_ref, KMP_TASK_TO_TASKDATA(task))); 1051 return; 1052 } 1053 #endif // TASK_UNUSED 1054 1055 // __kmp_init_implicit_task: Initialize the appropriate fields in the implicit 1056 // task for a given thread 1057 // 1058 // loc_ref: reference to source location of parallel region 1059 // this_thr: thread data structure corresponding to implicit task 1060 // team: team for this_thr 1061 // tid: thread id of given thread within team 1062 // set_curr_task: TRUE if need to push current task to thread 1063 // NOTE: Routine does not set up the implicit task ICVS. This is assumed to 1064 // have already been done elsewhere. 1065 // TODO: Get better loc_ref. Value passed in may be NULL 1066 void __kmp_init_implicit_task(ident_t *loc_ref, kmp_info_t *this_thr, 1067 kmp_team_t *team, int tid, int set_curr_task) { 1068 kmp_taskdata_t *task = &team->t.t_implicit_task_taskdata[tid]; 1069 1070 KF_TRACE( 1071 10, 1072 ("__kmp_init_implicit_task(enter): T#:%d team=%p task=%p, reinit=%s\n", 1073 tid, team, task, set_curr_task ? "TRUE" : "FALSE")); 1074 1075 task->td_task_id = KMP_GEN_TASK_ID(); 1076 task->td_team = team; 1077 // task->td_parent = NULL; // fix for CQ230101 (broken parent task info 1078 // in debugger) 1079 task->td_ident = loc_ref; 1080 task->td_taskwait_ident = NULL; 1081 task->td_taskwait_counter = 0; 1082 task->td_taskwait_thread = 0; 1083 1084 task->td_flags.tiedness = TASK_TIED; 1085 task->td_flags.tasktype = TASK_IMPLICIT; 1086 task->td_flags.proxy = TASK_FULL; 1087 1088 // All implicit tasks are executed immediately, not deferred 1089 task->td_flags.task_serial = 1; 1090 task->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec); 1091 task->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0; 1092 1093 task->td_flags.started = 1; 1094 task->td_flags.executing = 1; 1095 task->td_flags.complete = 0; 1096 task->td_flags.freed = 0; 1097 1098 task->td_depnode = NULL; 1099 task->td_last_tied = task; 1100 task->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED; 1101 1102 if (set_curr_task) { // only do this init first time thread is created 1103 KMP_ATOMIC_ST_REL(&task->td_incomplete_child_tasks, 0); 1104 // Not used: don't need to deallocate implicit task 1105 KMP_ATOMIC_ST_REL(&task->td_allocated_child_tasks, 0); 1106 task->td_taskgroup = NULL; // An implicit task does not have taskgroup 1107 task->td_dephash = NULL; 1108 __kmp_push_current_task_to_thread(this_thr, team, tid); 1109 } else { 1110 KMP_DEBUG_ASSERT(task->td_incomplete_child_tasks == 0); 1111 KMP_DEBUG_ASSERT(task->td_allocated_child_tasks == 0); 1112 } 1113 1114 #if OMPT_SUPPORT 1115 if (UNLIKELY(ompt_enabled.enabled)) 1116 __ompt_task_init(task, tid); 1117 #endif 1118 1119 KF_TRACE(10, ("__kmp_init_implicit_task(exit): T#:%d team=%p task=%p\n", tid, 1120 team, task)); 1121 } 1122 1123 // __kmp_finish_implicit_task: Release resources associated to implicit tasks 1124 // at the end of parallel regions. Some resources are kept for reuse in the next 1125 // parallel region. 1126 // 1127 // thread: thread data structure corresponding to implicit task 1128 void __kmp_finish_implicit_task(kmp_info_t *thread) { 1129 kmp_taskdata_t *task = thread->th.th_current_task; 1130 if (task->td_dephash) { 1131 int children; 1132 task->td_flags.complete = 1; 1133 children = KMP_ATOMIC_LD_ACQ(&task->td_incomplete_child_tasks); 1134 kmp_tasking_flags_t flags_old = task->td_flags; 1135 if (children == 0 && flags_old.complete == 1) { 1136 kmp_tasking_flags_t flags_new = flags_old; 1137 flags_new.complete = 0; 1138 if (KMP_COMPARE_AND_STORE_ACQ32(RCAST(kmp_int32 *, &task->td_flags), 1139 *RCAST(kmp_int32 *, &flags_old), 1140 *RCAST(kmp_int32 *, &flags_new))) { 1141 KA_TRACE(100, ("__kmp_finish_implicit_task: T#%d cleans " 1142 "dephash of implicit task %p\n", 1143 thread->th.th_info.ds.ds_gtid, task)); 1144 __kmp_dephash_free_entries(thread, task->td_dephash); 1145 } 1146 } 1147 } 1148 } 1149 1150 // __kmp_free_implicit_task: Release resources associated to implicit tasks 1151 // when these are destroyed regions 1152 // 1153 // thread: thread data structure corresponding to implicit task 1154 void __kmp_free_implicit_task(kmp_info_t *thread) { 1155 kmp_taskdata_t *task = thread->th.th_current_task; 1156 if (task && task->td_dephash) { 1157 __kmp_dephash_free(thread, task->td_dephash); 1158 task->td_dephash = NULL; 1159 } 1160 } 1161 1162 // Round up a size to a power of two specified by val: Used to insert padding 1163 // between structures co-allocated using a single malloc() call 1164 static size_t __kmp_round_up_to_val(size_t size, size_t val) { 1165 if (size & (val - 1)) { 1166 size &= ~(val - 1); 1167 if (size <= KMP_SIZE_T_MAX - val) { 1168 size += val; // Round up if there is no overflow. 1169 } 1170 } 1171 return size; 1172 } // __kmp_round_up_to_va 1173 1174 // __kmp_task_alloc: Allocate the taskdata and task data structures for a task 1175 // 1176 // loc_ref: source location information 1177 // gtid: global thread number. 1178 // flags: include tiedness & task type (explicit vs. implicit) of the ''new'' 1179 // task encountered. Converted from kmp_int32 to kmp_tasking_flags_t in routine. 1180 // sizeof_kmp_task_t: Size in bytes of kmp_task_t data structure including 1181 // private vars accessed in task. 1182 // sizeof_shareds: Size in bytes of array of pointers to shared vars accessed 1183 // in task. 1184 // task_entry: Pointer to task code entry point generated by compiler. 1185 // returns: a pointer to the allocated kmp_task_t structure (task). 1186 kmp_task_t *__kmp_task_alloc(ident_t *loc_ref, kmp_int32 gtid, 1187 kmp_tasking_flags_t *flags, 1188 size_t sizeof_kmp_task_t, size_t sizeof_shareds, 1189 kmp_routine_entry_t task_entry) { 1190 kmp_task_t *task; 1191 kmp_taskdata_t *taskdata; 1192 kmp_info_t *thread = __kmp_threads[gtid]; 1193 kmp_info_t *encountering_thread = thread; 1194 kmp_int32 encountering_gtid = gtid; 1195 kmp_team_t *team = thread->th.th_team; 1196 kmp_taskdata_t *parent_task = thread->th.th_current_task; 1197 size_t shareds_offset; 1198 1199 if (UNLIKELY(!TCR_4(__kmp_init_middle))) 1200 __kmp_middle_initialize(); 1201 1202 if (flags->hidden_helper) { 1203 if (__kmp_enable_hidden_helper) { 1204 if (!TCR_4(__kmp_init_hidden_helper)) 1205 __kmp_hidden_helper_initialize(); 1206 1207 // For a hidden helper task encountered by a regular thread, we will push 1208 // the task to the (gtid%__kmp_hidden_helper_threads_num)-th hidden helper 1209 // thread. 1210 if (!KMP_HIDDEN_HELPER_THREAD(gtid)) { 1211 thread = __kmp_threads[KMP_GTID_TO_SHADOW_GTID(gtid)]; 1212 team = thread->th.th_team; 1213 // We don't change the parent-child relation for hidden helper task as 1214 // we need that to do per-task-region synchronization. 1215 } 1216 } else { 1217 // If the hidden helper task is not enabled, reset the flag to FALSE. 1218 flags->hidden_helper = FALSE; 1219 } 1220 } 1221 1222 KA_TRACE(10, ("__kmp_task_alloc(enter): T#%d loc=%p, flags=(0x%x) " 1223 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n", 1224 gtid, loc_ref, *((kmp_int32 *)flags), sizeof_kmp_task_t, 1225 sizeof_shareds, task_entry)); 1226 1227 if (parent_task->td_flags.final) { 1228 if (flags->merged_if0) { 1229 } 1230 flags->final = 1; 1231 } 1232 1233 if (flags->tiedness == TASK_UNTIED && !team->t.t_serialized) { 1234 // Untied task encountered causes the TSC algorithm to check entire deque of 1235 // the victim thread. If no untied task encountered, then checking the head 1236 // of the deque should be enough. 1237 KMP_CHECK_UPDATE(thread->th.th_task_team->tt.tt_untied_task_encountered, 1); 1238 } 1239 1240 // Detachable tasks are not proxy tasks yet but could be in the future. Doing 1241 // the tasking setup 1242 // when that happens is too late. 1243 if (flags->proxy == TASK_PROXY || flags->detachable == TASK_DETACHABLE) { 1244 if (flags->proxy == TASK_PROXY) { 1245 flags->tiedness = TASK_UNTIED; 1246 flags->merged_if0 = 1; 1247 } 1248 /* are we running in a sequential parallel or tskm_immediate_exec... we need 1249 tasking support enabled */ 1250 if ((thread->th.th_task_team) == NULL) { 1251 /* This should only happen if the team is serialized 1252 setup a task team and propagate it to the thread */ 1253 KMP_DEBUG_ASSERT(team->t.t_serialized); 1254 KA_TRACE(30, 1255 ("T#%d creating task team in __kmp_task_alloc for proxy task\n", 1256 gtid)); 1257 __kmp_task_team_setup( 1258 thread, team, 1259 1); // 1 indicates setup the current team regardless of nthreads 1260 thread->th.th_task_team = team->t.t_task_team[thread->th.th_task_state]; 1261 } 1262 kmp_task_team_t *task_team = thread->th.th_task_team; 1263 1264 /* tasking must be enabled now as the task might not be pushed */ 1265 if (!KMP_TASKING_ENABLED(task_team)) { 1266 KA_TRACE( 1267 30, 1268 ("T#%d enabling tasking in __kmp_task_alloc for proxy task\n", gtid)); 1269 __kmp_enable_tasking(task_team, thread); 1270 kmp_int32 tid = thread->th.th_info.ds.ds_tid; 1271 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid]; 1272 // No lock needed since only owner can allocate 1273 if (thread_data->td.td_deque == NULL) { 1274 __kmp_alloc_task_deque(thread, thread_data); 1275 } 1276 } 1277 1278 if (task_team->tt.tt_found_proxy_tasks == FALSE) 1279 TCW_4(task_team->tt.tt_found_proxy_tasks, TRUE); 1280 } 1281 1282 // Calculate shared structure offset including padding after kmp_task_t struct 1283 // to align pointers in shared struct 1284 shareds_offset = sizeof(kmp_taskdata_t) + sizeof_kmp_task_t; 1285 shareds_offset = __kmp_round_up_to_val(shareds_offset, sizeof(void *)); 1286 1287 // Allocate a kmp_taskdata_t block and a kmp_task_t block. 1288 KA_TRACE(30, ("__kmp_task_alloc: T#%d First malloc size: %ld\n", gtid, 1289 shareds_offset)); 1290 KA_TRACE(30, ("__kmp_task_alloc: T#%d Second malloc size: %ld\n", gtid, 1291 sizeof_shareds)); 1292 1293 // Avoid double allocation here by combining shareds with taskdata 1294 #if USE_FAST_MEMORY 1295 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate( 1296 encountering_thread, shareds_offset + sizeof_shareds); 1297 #else /* ! USE_FAST_MEMORY */ 1298 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc( 1299 encountering_thread, shareds_offset + sizeof_shareds); 1300 #endif /* USE_FAST_MEMORY */ 1301 ANNOTATE_HAPPENS_AFTER(taskdata); 1302 1303 task = KMP_TASKDATA_TO_TASK(taskdata); 1304 1305 // Make sure task & taskdata are aligned appropriately 1306 #if KMP_ARCH_X86 || KMP_ARCH_PPC64 || !KMP_HAVE_QUAD 1307 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (sizeof(double) - 1)) == 0); 1308 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (sizeof(double) - 1)) == 0); 1309 #else 1310 KMP_DEBUG_ASSERT((((kmp_uintptr_t)taskdata) & (sizeof(_Quad) - 1)) == 0); 1311 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task) & (sizeof(_Quad) - 1)) == 0); 1312 #endif 1313 if (sizeof_shareds > 0) { 1314 // Avoid double allocation here by combining shareds with taskdata 1315 task->shareds = &((char *)taskdata)[shareds_offset]; 1316 // Make sure shareds struct is aligned to pointer size 1317 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (sizeof(void *) - 1)) == 1318 0); 1319 } else { 1320 task->shareds = NULL; 1321 } 1322 task->routine = task_entry; 1323 task->part_id = 0; // AC: Always start with 0 part id 1324 1325 taskdata->td_task_id = KMP_GEN_TASK_ID(); 1326 taskdata->td_team = team; 1327 taskdata->td_alloc_thread = encountering_thread; 1328 taskdata->td_parent = parent_task; 1329 taskdata->td_level = parent_task->td_level + 1; // increment nesting level 1330 KMP_ATOMIC_ST_RLX(&taskdata->td_untied_count, 0); 1331 taskdata->td_ident = loc_ref; 1332 taskdata->td_taskwait_ident = NULL; 1333 taskdata->td_taskwait_counter = 0; 1334 taskdata->td_taskwait_thread = 0; 1335 KMP_DEBUG_ASSERT(taskdata->td_parent != NULL); 1336 // avoid copying icvs for proxy tasks 1337 if (flags->proxy == TASK_FULL) 1338 copy_icvs(&taskdata->td_icvs, &taskdata->td_parent->td_icvs); 1339 1340 taskdata->td_flags.tiedness = flags->tiedness; 1341 taskdata->td_flags.final = flags->final; 1342 taskdata->td_flags.merged_if0 = flags->merged_if0; 1343 taskdata->td_flags.destructors_thunk = flags->destructors_thunk; 1344 taskdata->td_flags.proxy = flags->proxy; 1345 taskdata->td_flags.detachable = flags->detachable; 1346 taskdata->td_flags.hidden_helper = flags->hidden_helper; 1347 taskdata->td_parent_task_team = encountering_thread->th.th_task_team; 1348 taskdata->encountering_gtid = encountering_gtid; 1349 taskdata->td_task_team = thread->th.th_task_team; 1350 taskdata->td_size_alloc = shareds_offset + sizeof_shareds; 1351 taskdata->td_flags.tasktype = TASK_EXPLICIT; 1352 1353 // GEH - TODO: fix this to copy parent task's value of tasking_ser flag 1354 taskdata->td_flags.tasking_ser = (__kmp_tasking_mode == tskm_immediate_exec); 1355 1356 // GEH - TODO: fix this to copy parent task's value of team_serial flag 1357 taskdata->td_flags.team_serial = (team->t.t_serialized) ? 1 : 0; 1358 1359 // GEH - Note we serialize the task if the team is serialized to make sure 1360 // implicit parallel region tasks are not left until program termination to 1361 // execute. Also, it helps locality to execute immediately. 1362 1363 taskdata->td_flags.task_serial = 1364 (parent_task->td_flags.final || taskdata->td_flags.team_serial || 1365 taskdata->td_flags.tasking_ser || flags->merged_if0); 1366 1367 taskdata->td_flags.started = 0; 1368 taskdata->td_flags.executing = 0; 1369 taskdata->td_flags.complete = 0; 1370 taskdata->td_flags.freed = 0; 1371 1372 taskdata->td_flags.native = flags->native; 1373 1374 KMP_ATOMIC_ST_RLX(&taskdata->td_incomplete_child_tasks, 0); 1375 // start at one because counts current task and children 1376 KMP_ATOMIC_ST_RLX(&taskdata->td_allocated_child_tasks, 1); 1377 taskdata->td_taskgroup = 1378 parent_task->td_taskgroup; // task inherits taskgroup from the parent task 1379 taskdata->td_dephash = NULL; 1380 taskdata->td_depnode = NULL; 1381 if (flags->tiedness == TASK_UNTIED) 1382 taskdata->td_last_tied = NULL; // will be set when the task is scheduled 1383 else 1384 taskdata->td_last_tied = taskdata; 1385 taskdata->td_allow_completion_event.type = KMP_EVENT_UNINITIALIZED; 1386 #if OMPT_SUPPORT 1387 if (UNLIKELY(ompt_enabled.enabled)) 1388 __ompt_task_init(taskdata, gtid); 1389 #endif 1390 // Only need to keep track of child task counts if team parallel and tasking not 1391 // serialized or if it is a proxy or detachable task 1392 if (flags->proxy == TASK_PROXY || 1393 flags->detachable == TASK_DETACHABLE || 1394 !(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) 1395 { 1396 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks); 1397 if (parent_task->td_taskgroup) 1398 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count); 1399 // Only need to keep track of allocated child tasks for explicit tasks since 1400 // implicit not deallocated 1401 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT) { 1402 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks); 1403 } 1404 } 1405 1406 if (flags->hidden_helper) { 1407 // Increment the number of hidden helper tasks to be executed 1408 KMP_ATOMIC_INC(&__kmp_unexecuted_hidden_helper_tasks); 1409 if (kmp_task_team_t *parent_team = taskdata->td_parent_task_team) { 1410 KMP_ATOMIC_INC(&parent_team->tt.tt_unfinished_hidden_helper_tasks); 1411 if (!parent_team->tt.tt_hidden_helper_task_encountered) { 1412 TCW_4(parent_team->tt.tt_hidden_helper_task_encountered, TRUE); 1413 } 1414 } 1415 } 1416 1417 KA_TRACE(20, ("__kmp_task_alloc(exit): T#%d created task %p parent=%p\n", 1418 gtid, taskdata, taskdata->td_parent)); 1419 ANNOTATE_HAPPENS_BEFORE(task); 1420 1421 return task; 1422 } 1423 1424 kmp_task_t *__kmpc_omp_task_alloc(ident_t *loc_ref, kmp_int32 gtid, 1425 kmp_int32 flags, size_t sizeof_kmp_task_t, 1426 size_t sizeof_shareds, 1427 kmp_routine_entry_t task_entry) { 1428 kmp_task_t *retval; 1429 kmp_tasking_flags_t *input_flags = (kmp_tasking_flags_t *)&flags; 1430 __kmp_assert_valid_gtid(gtid); 1431 input_flags->native = FALSE; 1432 // __kmp_task_alloc() sets up all other runtime flags 1433 KA_TRACE(10, ("__kmpc_omp_task_alloc(enter): T#%d loc=%p, flags=(%s %s %s) " 1434 "sizeof_task=%ld sizeof_shared=%ld entry=%p\n", 1435 gtid, loc_ref, input_flags->tiedness ? "tied " : "untied", 1436 input_flags->proxy ? "proxy" : "", 1437 input_flags->detachable ? "detachable" : "", sizeof_kmp_task_t, 1438 sizeof_shareds, task_entry)); 1439 1440 retval = __kmp_task_alloc(loc_ref, gtid, input_flags, sizeof_kmp_task_t, 1441 sizeof_shareds, task_entry); 1442 1443 KA_TRACE(20, ("__kmpc_omp_task_alloc(exit): T#%d retval %p\n", gtid, retval)); 1444 1445 return retval; 1446 } 1447 1448 kmp_task_t *__kmpc_omp_target_task_alloc(ident_t *loc_ref, kmp_int32 gtid, 1449 kmp_int32 flags, 1450 size_t sizeof_kmp_task_t, 1451 size_t sizeof_shareds, 1452 kmp_routine_entry_t task_entry, 1453 kmp_int64 device_id) { 1454 if (__kmp_enable_hidden_helper) { 1455 auto &input_flags = reinterpret_cast<kmp_tasking_flags_t &>(flags); 1456 input_flags.hidden_helper = TRUE; 1457 // Hidden helper thread is always final for now because it is created by the 1458 // compiler and used only for async offloading 1459 input_flags.final = TRUE; 1460 } 1461 1462 return __kmpc_omp_task_alloc(loc_ref, gtid, flags, sizeof_kmp_task_t, 1463 sizeof_shareds, task_entry); 1464 } 1465 1466 /*! 1467 @ingroup TASKING 1468 @param loc_ref location of the original task directive 1469 @param gtid Global Thread ID of encountering thread 1470 @param new_task task thunk allocated by __kmpc_omp_task_alloc() for the ''new 1471 task'' 1472 @param naffins Number of affinity items 1473 @param affin_list List of affinity items 1474 @return Returns non-zero if registering affinity information was not successful. 1475 Returns 0 if registration was successful 1476 This entry registers the affinity information attached to a task with the task 1477 thunk structure kmp_taskdata_t. 1478 */ 1479 kmp_int32 1480 __kmpc_omp_reg_task_with_affinity(ident_t *loc_ref, kmp_int32 gtid, 1481 kmp_task_t *new_task, kmp_int32 naffins, 1482 kmp_task_affinity_info_t *affin_list) { 1483 return 0; 1484 } 1485 1486 // __kmp_invoke_task: invoke the specified task 1487 // 1488 // gtid: global thread ID of caller 1489 // task: the task to invoke 1490 // current_task: the task to resume after task invocation 1491 static void __kmp_invoke_task(kmp_int32 gtid, kmp_task_t *task, 1492 kmp_taskdata_t *current_task) { 1493 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task); 1494 kmp_info_t *thread; 1495 int discard = 0 /* false */; 1496 KA_TRACE( 1497 30, ("__kmp_invoke_task(enter): T#%d invoking task %p, current_task=%p\n", 1498 gtid, taskdata, current_task)); 1499 KMP_DEBUG_ASSERT(task); 1500 if (UNLIKELY(taskdata->td_flags.proxy == TASK_PROXY && 1501 taskdata->td_flags.complete == 1)) { 1502 // This is a proxy task that was already completed but it needs to run 1503 // its bottom-half finish 1504 KA_TRACE( 1505 30, 1506 ("__kmp_invoke_task: T#%d running bottom finish for proxy task %p\n", 1507 gtid, taskdata)); 1508 1509 __kmp_bottom_half_finish_proxy(gtid, task); 1510 1511 KA_TRACE(30, ("__kmp_invoke_task(exit): T#%d completed bottom finish for " 1512 "proxy task %p, resuming task %p\n", 1513 gtid, taskdata, current_task)); 1514 1515 return; 1516 } 1517 1518 #if OMPT_SUPPORT 1519 // For untied tasks, the first task executed only calls __kmpc_omp_task and 1520 // does not execute code. 1521 ompt_thread_info_t oldInfo; 1522 if (UNLIKELY(ompt_enabled.enabled)) { 1523 // Store the threads states and restore them after the task 1524 thread = __kmp_threads[gtid]; 1525 oldInfo = thread->th.ompt_thread_info; 1526 thread->th.ompt_thread_info.wait_id = 0; 1527 thread->th.ompt_thread_info.state = (thread->th.th_team_serialized) 1528 ? ompt_state_work_serial 1529 : ompt_state_work_parallel; 1530 taskdata->ompt_task_info.frame.exit_frame.ptr = OMPT_GET_FRAME_ADDRESS(0); 1531 } 1532 #endif 1533 1534 // Decreament the counter of hidden helper tasks to be executed 1535 if (taskdata->td_flags.hidden_helper) { 1536 // Hidden helper tasks can only be executed by hidden helper threads 1537 KMP_ASSERT(KMP_HIDDEN_HELPER_THREAD(gtid)); 1538 KMP_ATOMIC_DEC(&__kmp_unexecuted_hidden_helper_tasks); 1539 } 1540 1541 // Proxy tasks are not handled by the runtime 1542 if (taskdata->td_flags.proxy != TASK_PROXY) { 1543 ANNOTATE_HAPPENS_AFTER(task); 1544 __kmp_task_start(gtid, task, current_task); // OMPT only if not discarded 1545 } 1546 1547 // TODO: cancel tasks if the parallel region has also been cancelled 1548 // TODO: check if this sequence can be hoisted above __kmp_task_start 1549 // if cancellation has been enabled for this run ... 1550 if (UNLIKELY(__kmp_omp_cancellation)) { 1551 thread = __kmp_threads[gtid]; 1552 kmp_team_t *this_team = thread->th.th_team; 1553 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup; 1554 if ((taskgroup && taskgroup->cancel_request) || 1555 (this_team->t.t_cancel_request == cancel_parallel)) { 1556 #if OMPT_SUPPORT && OMPT_OPTIONAL 1557 ompt_data_t *task_data; 1558 if (UNLIKELY(ompt_enabled.ompt_callback_cancel)) { 1559 __ompt_get_task_info_internal(0, NULL, &task_data, NULL, NULL, NULL); 1560 ompt_callbacks.ompt_callback(ompt_callback_cancel)( 1561 task_data, 1562 ((taskgroup && taskgroup->cancel_request) ? ompt_cancel_taskgroup 1563 : ompt_cancel_parallel) | 1564 ompt_cancel_discarded_task, 1565 NULL); 1566 } 1567 #endif 1568 KMP_COUNT_BLOCK(TASK_cancelled); 1569 // this task belongs to a task group and we need to cancel it 1570 discard = 1 /* true */; 1571 } 1572 } 1573 1574 // Invoke the task routine and pass in relevant data. 1575 // Thunks generated by gcc take a different argument list. 1576 if (!discard) { 1577 if (taskdata->td_flags.tiedness == TASK_UNTIED) { 1578 taskdata->td_last_tied = current_task->td_last_tied; 1579 KMP_DEBUG_ASSERT(taskdata->td_last_tied); 1580 } 1581 #if KMP_STATS_ENABLED 1582 KMP_COUNT_BLOCK(TASK_executed); 1583 switch (KMP_GET_THREAD_STATE()) { 1584 case FORK_JOIN_BARRIER: 1585 KMP_PUSH_PARTITIONED_TIMER(OMP_task_join_bar); 1586 break; 1587 case PLAIN_BARRIER: 1588 KMP_PUSH_PARTITIONED_TIMER(OMP_task_plain_bar); 1589 break; 1590 case TASKYIELD: 1591 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskyield); 1592 break; 1593 case TASKWAIT: 1594 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskwait); 1595 break; 1596 case TASKGROUP: 1597 KMP_PUSH_PARTITIONED_TIMER(OMP_task_taskgroup); 1598 break; 1599 default: 1600 KMP_PUSH_PARTITIONED_TIMER(OMP_task_immediate); 1601 break; 1602 } 1603 #endif // KMP_STATS_ENABLED 1604 1605 // OMPT task begin 1606 #if OMPT_SUPPORT 1607 if (UNLIKELY(ompt_enabled.enabled)) 1608 __ompt_task_start(task, current_task, gtid); 1609 #endif 1610 1611 #if USE_ITT_BUILD && USE_ITT_NOTIFY 1612 kmp_uint64 cur_time; 1613 kmp_int32 kmp_itt_count_task = 1614 __kmp_forkjoin_frames_mode == 3 && !taskdata->td_flags.task_serial && 1615 current_task->td_flags.tasktype == TASK_IMPLICIT; 1616 if (kmp_itt_count_task) { 1617 thread = __kmp_threads[gtid]; 1618 // Time outer level explicit task on barrier for adjusting imbalance time 1619 if (thread->th.th_bar_arrive_time) 1620 cur_time = __itt_get_timestamp(); 1621 else 1622 kmp_itt_count_task = 0; // thread is not on a barrier - skip timing 1623 } 1624 KMP_FSYNC_ACQUIRED(taskdata); // acquired self (new task) 1625 #endif 1626 1627 #ifdef KMP_GOMP_COMPAT 1628 if (taskdata->td_flags.native) { 1629 ((void (*)(void *))(*(task->routine)))(task->shareds); 1630 } else 1631 #endif /* KMP_GOMP_COMPAT */ 1632 { 1633 (*(task->routine))(gtid, task); 1634 } 1635 KMP_POP_PARTITIONED_TIMER(); 1636 1637 #if USE_ITT_BUILD && USE_ITT_NOTIFY 1638 if (kmp_itt_count_task) { 1639 // Barrier imbalance - adjust arrive time with the task duration 1640 thread->th.th_bar_arrive_time += (__itt_get_timestamp() - cur_time); 1641 } 1642 KMP_FSYNC_CANCEL(taskdata); // destroy self (just executed) 1643 KMP_FSYNC_RELEASING(taskdata->td_parent); // releasing parent 1644 #endif 1645 1646 } 1647 1648 // Proxy tasks are not handled by the runtime 1649 if (taskdata->td_flags.proxy != TASK_PROXY) { 1650 ANNOTATE_HAPPENS_BEFORE(taskdata->td_parent); 1651 #if OMPT_SUPPORT 1652 if (UNLIKELY(ompt_enabled.enabled)) { 1653 thread->th.ompt_thread_info = oldInfo; 1654 if (taskdata->td_flags.tiedness == TASK_TIED) { 1655 taskdata->ompt_task_info.frame.exit_frame = ompt_data_none; 1656 } 1657 __kmp_task_finish<true>(gtid, task, current_task); 1658 } else 1659 #endif 1660 __kmp_task_finish<false>(gtid, task, current_task); 1661 } 1662 1663 KA_TRACE( 1664 30, 1665 ("__kmp_invoke_task(exit): T#%d completed task %p, resuming task %p\n", 1666 gtid, taskdata, current_task)); 1667 return; 1668 } 1669 1670 // __kmpc_omp_task_parts: Schedule a thread-switchable task for execution 1671 // 1672 // loc_ref: location of original task pragma (ignored) 1673 // gtid: Global Thread ID of encountering thread 1674 // new_task: task thunk allocated by __kmp_omp_task_alloc() for the ''new task'' 1675 // Returns: 1676 // TASK_CURRENT_NOT_QUEUED (0) if did not suspend and queue current task to 1677 // be resumed later. 1678 // TASK_CURRENT_QUEUED (1) if suspended and queued the current task to be 1679 // resumed later. 1680 kmp_int32 __kmpc_omp_task_parts(ident_t *loc_ref, kmp_int32 gtid, 1681 kmp_task_t *new_task) { 1682 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task); 1683 1684 KA_TRACE(10, ("__kmpc_omp_task_parts(enter): T#%d loc=%p task=%p\n", gtid, 1685 loc_ref, new_taskdata)); 1686 1687 #if OMPT_SUPPORT 1688 kmp_taskdata_t *parent; 1689 if (UNLIKELY(ompt_enabled.enabled)) { 1690 parent = new_taskdata->td_parent; 1691 if (ompt_enabled.ompt_callback_task_create) { 1692 ompt_data_t task_data = ompt_data_none; 1693 ompt_callbacks.ompt_callback(ompt_callback_task_create)( 1694 parent ? &(parent->ompt_task_info.task_data) : &task_data, 1695 parent ? &(parent->ompt_task_info.frame) : NULL, 1696 &(new_taskdata->ompt_task_info.task_data), ompt_task_explicit, 0, 1697 OMPT_GET_RETURN_ADDRESS(0)); 1698 } 1699 } 1700 #endif 1701 1702 /* Should we execute the new task or queue it? For now, let's just always try 1703 to queue it. If the queue fills up, then we'll execute it. */ 1704 1705 if (__kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED) // if cannot defer 1706 { // Execute this task immediately 1707 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task; 1708 new_taskdata->td_flags.task_serial = 1; 1709 __kmp_invoke_task(gtid, new_task, current_task); 1710 } 1711 1712 KA_TRACE( 1713 10, 1714 ("__kmpc_omp_task_parts(exit): T#%d returning TASK_CURRENT_NOT_QUEUED: " 1715 "loc=%p task=%p, return: TASK_CURRENT_NOT_QUEUED\n", 1716 gtid, loc_ref, new_taskdata)); 1717 1718 ANNOTATE_HAPPENS_BEFORE(new_task); 1719 #if OMPT_SUPPORT 1720 if (UNLIKELY(ompt_enabled.enabled)) { 1721 parent->ompt_task_info.frame.enter_frame = ompt_data_none; 1722 } 1723 #endif 1724 return TASK_CURRENT_NOT_QUEUED; 1725 } 1726 1727 // __kmp_omp_task: Schedule a non-thread-switchable task for execution 1728 // 1729 // gtid: Global Thread ID of encountering thread 1730 // new_task:non-thread-switchable task thunk allocated by __kmp_omp_task_alloc() 1731 // serialize_immediate: if TRUE then if the task is executed immediately its 1732 // execution will be serialized 1733 // Returns: 1734 // TASK_CURRENT_NOT_QUEUED (0) if did not suspend and queue current task to 1735 // be resumed later. 1736 // TASK_CURRENT_QUEUED (1) if suspended and queued the current task to be 1737 // resumed later. 1738 kmp_int32 __kmp_omp_task(kmp_int32 gtid, kmp_task_t *new_task, 1739 bool serialize_immediate) { 1740 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task); 1741 1742 /* Should we execute the new task or queue it? For now, let's just always try 1743 to queue it. If the queue fills up, then we'll execute it. */ 1744 if (new_taskdata->td_flags.proxy == TASK_PROXY || 1745 __kmp_push_task(gtid, new_task) == TASK_NOT_PUSHED) // if cannot defer 1746 { // Execute this task immediately 1747 kmp_taskdata_t *current_task = __kmp_threads[gtid]->th.th_current_task; 1748 if (serialize_immediate) 1749 new_taskdata->td_flags.task_serial = 1; 1750 __kmp_invoke_task(gtid, new_task, current_task); 1751 } 1752 1753 ANNOTATE_HAPPENS_BEFORE(new_task); 1754 return TASK_CURRENT_NOT_QUEUED; 1755 } 1756 1757 // __kmpc_omp_task: Wrapper around __kmp_omp_task to schedule a 1758 // non-thread-switchable task from the parent thread only! 1759 // 1760 // loc_ref: location of original task pragma (ignored) 1761 // gtid: Global Thread ID of encountering thread 1762 // new_task: non-thread-switchable task thunk allocated by 1763 // __kmp_omp_task_alloc() 1764 // Returns: 1765 // TASK_CURRENT_NOT_QUEUED (0) if did not suspend and queue current task to 1766 // be resumed later. 1767 // TASK_CURRENT_QUEUED (1) if suspended and queued the current task to be 1768 // resumed later. 1769 kmp_int32 __kmpc_omp_task(ident_t *loc_ref, kmp_int32 gtid, 1770 kmp_task_t *new_task) { 1771 kmp_int32 res; 1772 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK); 1773 1774 #if KMP_DEBUG || OMPT_SUPPORT 1775 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task); 1776 #endif 1777 KA_TRACE(10, ("__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref, 1778 new_taskdata)); 1779 __kmp_assert_valid_gtid(gtid); 1780 1781 #if OMPT_SUPPORT 1782 kmp_taskdata_t *parent = NULL; 1783 if (UNLIKELY(ompt_enabled.enabled)) { 1784 if (!new_taskdata->td_flags.started) { 1785 OMPT_STORE_RETURN_ADDRESS(gtid); 1786 parent = new_taskdata->td_parent; 1787 if (!parent->ompt_task_info.frame.enter_frame.ptr) { 1788 parent->ompt_task_info.frame.enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0); 1789 } 1790 if (ompt_enabled.ompt_callback_task_create) { 1791 ompt_data_t task_data = ompt_data_none; 1792 ompt_callbacks.ompt_callback(ompt_callback_task_create)( 1793 parent ? &(parent->ompt_task_info.task_data) : &task_data, 1794 parent ? &(parent->ompt_task_info.frame) : NULL, 1795 &(new_taskdata->ompt_task_info.task_data), 1796 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0, 1797 OMPT_LOAD_RETURN_ADDRESS(gtid)); 1798 } 1799 } else { 1800 // We are scheduling the continuation of an UNTIED task. 1801 // Scheduling back to the parent task. 1802 __ompt_task_finish(new_task, 1803 new_taskdata->ompt_task_info.scheduling_parent, 1804 ompt_task_switch); 1805 new_taskdata->ompt_task_info.frame.exit_frame = ompt_data_none; 1806 } 1807 } 1808 #endif 1809 1810 res = __kmp_omp_task(gtid, new_task, true); 1811 1812 KA_TRACE(10, ("__kmpc_omp_task(exit): T#%d returning " 1813 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n", 1814 gtid, loc_ref, new_taskdata)); 1815 #if OMPT_SUPPORT 1816 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) { 1817 parent->ompt_task_info.frame.enter_frame = ompt_data_none; 1818 } 1819 #endif 1820 return res; 1821 } 1822 1823 // __kmp_omp_taskloop_task: Wrapper around __kmp_omp_task to schedule 1824 // a taskloop task with the correct OMPT return address 1825 // 1826 // loc_ref: location of original task pragma (ignored) 1827 // gtid: Global Thread ID of encountering thread 1828 // new_task: non-thread-switchable task thunk allocated by 1829 // __kmp_omp_task_alloc() 1830 // codeptr_ra: return address for OMPT callback 1831 // Returns: 1832 // TASK_CURRENT_NOT_QUEUED (0) if did not suspend and queue current task to 1833 // be resumed later. 1834 // TASK_CURRENT_QUEUED (1) if suspended and queued the current task to be 1835 // resumed later. 1836 kmp_int32 __kmp_omp_taskloop_task(ident_t *loc_ref, kmp_int32 gtid, 1837 kmp_task_t *new_task, void *codeptr_ra) { 1838 kmp_int32 res; 1839 KMP_SET_THREAD_STATE_BLOCK(EXPLICIT_TASK); 1840 1841 #if KMP_DEBUG || OMPT_SUPPORT 1842 kmp_taskdata_t *new_taskdata = KMP_TASK_TO_TASKDATA(new_task); 1843 #endif 1844 KA_TRACE(10, ("__kmpc_omp_task(enter): T#%d loc=%p task=%p\n", gtid, loc_ref, 1845 new_taskdata)); 1846 1847 #if OMPT_SUPPORT 1848 kmp_taskdata_t *parent = NULL; 1849 if (UNLIKELY(ompt_enabled.enabled && !new_taskdata->td_flags.started)) { 1850 parent = new_taskdata->td_parent; 1851 if (!parent->ompt_task_info.frame.enter_frame.ptr) 1852 parent->ompt_task_info.frame.enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0); 1853 if (ompt_enabled.ompt_callback_task_create) { 1854 ompt_data_t task_data = ompt_data_none; 1855 ompt_callbacks.ompt_callback(ompt_callback_task_create)( 1856 parent ? &(parent->ompt_task_info.task_data) : &task_data, 1857 parent ? &(parent->ompt_task_info.frame) : NULL, 1858 &(new_taskdata->ompt_task_info.task_data), 1859 ompt_task_explicit | TASK_TYPE_DETAILS_FORMAT(new_taskdata), 0, 1860 codeptr_ra); 1861 } 1862 } 1863 #endif 1864 1865 res = __kmp_omp_task(gtid, new_task, true); 1866 1867 KA_TRACE(10, ("__kmpc_omp_task(exit): T#%d returning " 1868 "TASK_CURRENT_NOT_QUEUED: loc=%p task=%p\n", 1869 gtid, loc_ref, new_taskdata)); 1870 #if OMPT_SUPPORT 1871 if (UNLIKELY(ompt_enabled.enabled && parent != NULL)) { 1872 parent->ompt_task_info.frame.enter_frame = ompt_data_none; 1873 } 1874 #endif 1875 return res; 1876 } 1877 1878 template <bool ompt> 1879 static kmp_int32 __kmpc_omp_taskwait_template(ident_t *loc_ref, kmp_int32 gtid, 1880 void *frame_address, 1881 void *return_address) { 1882 kmp_taskdata_t *taskdata; 1883 kmp_info_t *thread; 1884 int thread_finished = FALSE; 1885 KMP_SET_THREAD_STATE_BLOCK(TASKWAIT); 1886 1887 KA_TRACE(10, ("__kmpc_omp_taskwait(enter): T#%d loc=%p\n", gtid, loc_ref)); 1888 __kmp_assert_valid_gtid(gtid); 1889 1890 if (__kmp_tasking_mode != tskm_immediate_exec) { 1891 thread = __kmp_threads[gtid]; 1892 taskdata = thread->th.th_current_task; 1893 1894 #if OMPT_SUPPORT && OMPT_OPTIONAL 1895 ompt_data_t *my_task_data; 1896 ompt_data_t *my_parallel_data; 1897 1898 if (ompt) { 1899 my_task_data = &(taskdata->ompt_task_info.task_data); 1900 my_parallel_data = OMPT_CUR_TEAM_DATA(thread); 1901 1902 taskdata->ompt_task_info.frame.enter_frame.ptr = frame_address; 1903 1904 if (ompt_enabled.ompt_callback_sync_region) { 1905 ompt_callbacks.ompt_callback(ompt_callback_sync_region)( 1906 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data, 1907 my_task_data, return_address); 1908 } 1909 1910 if (ompt_enabled.ompt_callback_sync_region_wait) { 1911 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)( 1912 ompt_sync_region_taskwait, ompt_scope_begin, my_parallel_data, 1913 my_task_data, return_address); 1914 } 1915 } 1916 #endif // OMPT_SUPPORT && OMPT_OPTIONAL 1917 1918 // Debugger: The taskwait is active. Store location and thread encountered the 1919 // taskwait. 1920 #if USE_ITT_BUILD 1921 // Note: These values are used by ITT events as well. 1922 #endif /* USE_ITT_BUILD */ 1923 taskdata->td_taskwait_counter += 1; 1924 taskdata->td_taskwait_ident = loc_ref; 1925 taskdata->td_taskwait_thread = gtid + 1; 1926 1927 #if USE_ITT_BUILD 1928 void *itt_sync_obj = __kmp_itt_taskwait_object(gtid); 1929 if (UNLIKELY(itt_sync_obj != NULL)) 1930 __kmp_itt_taskwait_starting(gtid, itt_sync_obj); 1931 #endif /* USE_ITT_BUILD */ 1932 1933 bool must_wait = 1934 !taskdata->td_flags.team_serial && !taskdata->td_flags.final; 1935 1936 must_wait = must_wait || (thread->th.th_task_team != NULL && 1937 thread->th.th_task_team->tt.tt_found_proxy_tasks); 1938 // If hidden helper thread is encountered, we must enable wait here. 1939 must_wait = 1940 must_wait || 1941 (__kmp_enable_hidden_helper && thread->th.th_task_team != NULL && 1942 thread->th.th_task_team->tt.tt_hidden_helper_task_encountered); 1943 1944 if (must_wait) { 1945 kmp_flag_32<false, false> flag( 1946 RCAST(std::atomic<kmp_uint32> *, 1947 &(taskdata->td_incomplete_child_tasks)), 1948 0U); 1949 while (KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) != 0) { 1950 flag.execute_tasks(thread, gtid, FALSE, 1951 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), 1952 __kmp_task_stealing_constraint); 1953 } 1954 } 1955 #if USE_ITT_BUILD 1956 if (UNLIKELY(itt_sync_obj != NULL)) 1957 __kmp_itt_taskwait_finished(gtid, itt_sync_obj); 1958 KMP_FSYNC_ACQUIRED(taskdata); // acquire self - sync with children 1959 #endif /* USE_ITT_BUILD */ 1960 1961 // Debugger: The taskwait is completed. Location remains, but thread is 1962 // negated. 1963 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread; 1964 1965 #if OMPT_SUPPORT && OMPT_OPTIONAL 1966 if (ompt) { 1967 if (ompt_enabled.ompt_callback_sync_region_wait) { 1968 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)( 1969 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data, 1970 my_task_data, return_address); 1971 } 1972 if (ompt_enabled.ompt_callback_sync_region) { 1973 ompt_callbacks.ompt_callback(ompt_callback_sync_region)( 1974 ompt_sync_region_taskwait, ompt_scope_end, my_parallel_data, 1975 my_task_data, return_address); 1976 } 1977 taskdata->ompt_task_info.frame.enter_frame = ompt_data_none; 1978 } 1979 #endif // OMPT_SUPPORT && OMPT_OPTIONAL 1980 1981 ANNOTATE_HAPPENS_AFTER(taskdata); 1982 } 1983 1984 KA_TRACE(10, ("__kmpc_omp_taskwait(exit): T#%d task %p finished waiting, " 1985 "returning TASK_CURRENT_NOT_QUEUED\n", 1986 gtid, taskdata)); 1987 1988 return TASK_CURRENT_NOT_QUEUED; 1989 } 1990 1991 #if OMPT_SUPPORT && OMPT_OPTIONAL 1992 OMPT_NOINLINE 1993 static kmp_int32 __kmpc_omp_taskwait_ompt(ident_t *loc_ref, kmp_int32 gtid, 1994 void *frame_address, 1995 void *return_address) { 1996 return __kmpc_omp_taskwait_template<true>(loc_ref, gtid, frame_address, 1997 return_address); 1998 } 1999 #endif // OMPT_SUPPORT && OMPT_OPTIONAL 2000 2001 // __kmpc_omp_taskwait: Wait until all tasks generated by the current task are 2002 // complete 2003 kmp_int32 __kmpc_omp_taskwait(ident_t *loc_ref, kmp_int32 gtid) { 2004 #if OMPT_SUPPORT && OMPT_OPTIONAL 2005 if (UNLIKELY(ompt_enabled.enabled)) { 2006 OMPT_STORE_RETURN_ADDRESS(gtid); 2007 return __kmpc_omp_taskwait_ompt(loc_ref, gtid, OMPT_GET_FRAME_ADDRESS(0), 2008 OMPT_LOAD_RETURN_ADDRESS(gtid)); 2009 } 2010 #endif 2011 return __kmpc_omp_taskwait_template<false>(loc_ref, gtid, NULL, NULL); 2012 } 2013 2014 // __kmpc_omp_taskyield: switch to a different task 2015 kmp_int32 __kmpc_omp_taskyield(ident_t *loc_ref, kmp_int32 gtid, int end_part) { 2016 kmp_taskdata_t *taskdata; 2017 kmp_info_t *thread; 2018 int thread_finished = FALSE; 2019 2020 KMP_COUNT_BLOCK(OMP_TASKYIELD); 2021 KMP_SET_THREAD_STATE_BLOCK(TASKYIELD); 2022 2023 KA_TRACE(10, ("__kmpc_omp_taskyield(enter): T#%d loc=%p end_part = %d\n", 2024 gtid, loc_ref, end_part)); 2025 __kmp_assert_valid_gtid(gtid); 2026 2027 if (__kmp_tasking_mode != tskm_immediate_exec && __kmp_init_parallel) { 2028 thread = __kmp_threads[gtid]; 2029 taskdata = thread->th.th_current_task; 2030 // Should we model this as a task wait or not? 2031 // Debugger: The taskwait is active. Store location and thread encountered the 2032 // taskwait. 2033 #if USE_ITT_BUILD 2034 // Note: These values are used by ITT events as well. 2035 #endif /* USE_ITT_BUILD */ 2036 taskdata->td_taskwait_counter += 1; 2037 taskdata->td_taskwait_ident = loc_ref; 2038 taskdata->td_taskwait_thread = gtid + 1; 2039 2040 #if USE_ITT_BUILD 2041 void *itt_sync_obj = __kmp_itt_taskwait_object(gtid); 2042 if (UNLIKELY(itt_sync_obj != NULL)) 2043 __kmp_itt_taskwait_starting(gtid, itt_sync_obj); 2044 #endif /* USE_ITT_BUILD */ 2045 if (!taskdata->td_flags.team_serial) { 2046 kmp_task_team_t *task_team = thread->th.th_task_team; 2047 if (task_team != NULL) { 2048 if (KMP_TASKING_ENABLED(task_team)) { 2049 #if OMPT_SUPPORT 2050 if (UNLIKELY(ompt_enabled.enabled)) 2051 thread->th.ompt_thread_info.ompt_task_yielded = 1; 2052 #endif 2053 __kmp_execute_tasks_32( 2054 thread, gtid, (kmp_flag_32<> *)NULL, FALSE, 2055 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), 2056 __kmp_task_stealing_constraint); 2057 #if OMPT_SUPPORT 2058 if (UNLIKELY(ompt_enabled.enabled)) 2059 thread->th.ompt_thread_info.ompt_task_yielded = 0; 2060 #endif 2061 } 2062 } 2063 } 2064 #if USE_ITT_BUILD 2065 if (UNLIKELY(itt_sync_obj != NULL)) 2066 __kmp_itt_taskwait_finished(gtid, itt_sync_obj); 2067 #endif /* USE_ITT_BUILD */ 2068 2069 // Debugger: The taskwait is completed. Location remains, but thread is 2070 // negated. 2071 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread; 2072 } 2073 2074 KA_TRACE(10, ("__kmpc_omp_taskyield(exit): T#%d task %p resuming, " 2075 "returning TASK_CURRENT_NOT_QUEUED\n", 2076 gtid, taskdata)); 2077 2078 return TASK_CURRENT_NOT_QUEUED; 2079 } 2080 2081 // Task Reduction implementation 2082 // 2083 // Note: initial implementation didn't take into account the possibility 2084 // to specify omp_orig for initializer of the UDR (user defined reduction). 2085 // Corrected implementation takes into account the omp_orig object. 2086 // Compiler is free to use old implementation if omp_orig is not specified. 2087 2088 /*! 2089 @ingroup BASIC_TYPES 2090 @{ 2091 */ 2092 2093 /*! 2094 Flags for special info per task reduction item. 2095 */ 2096 typedef struct kmp_taskred_flags { 2097 /*! 1 - use lazy alloc/init (e.g. big objects, #tasks < #threads) */ 2098 unsigned lazy_priv : 1; 2099 unsigned reserved31 : 31; 2100 } kmp_taskred_flags_t; 2101 2102 /*! 2103 Internal struct for reduction data item related info set up by compiler. 2104 */ 2105 typedef struct kmp_task_red_input { 2106 void *reduce_shar; /**< shared between tasks item to reduce into */ 2107 size_t reduce_size; /**< size of data item in bytes */ 2108 // three compiler-generated routines (init, fini are optional): 2109 void *reduce_init; /**< data initialization routine (single parameter) */ 2110 void *reduce_fini; /**< data finalization routine */ 2111 void *reduce_comb; /**< data combiner routine */ 2112 kmp_taskred_flags_t flags; /**< flags for additional info from compiler */ 2113 } kmp_task_red_input_t; 2114 2115 /*! 2116 Internal struct for reduction data item related info saved by the library. 2117 */ 2118 typedef struct kmp_taskred_data { 2119 void *reduce_shar; /**< shared between tasks item to reduce into */ 2120 size_t reduce_size; /**< size of data item */ 2121 kmp_taskred_flags_t flags; /**< flags for additional info from compiler */ 2122 void *reduce_priv; /**< array of thread specific items */ 2123 void *reduce_pend; /**< end of private data for faster comparison op */ 2124 // three compiler-generated routines (init, fini are optional): 2125 void *reduce_comb; /**< data combiner routine */ 2126 void *reduce_init; /**< data initialization routine (two parameters) */ 2127 void *reduce_fini; /**< data finalization routine */ 2128 void *reduce_orig; /**< original item (can be used in UDR initializer) */ 2129 } kmp_taskred_data_t; 2130 2131 /*! 2132 Internal struct for reduction data item related info set up by compiler. 2133 2134 New interface: added reduce_orig field to provide omp_orig for UDR initializer. 2135 */ 2136 typedef struct kmp_taskred_input { 2137 void *reduce_shar; /**< shared between tasks item to reduce into */ 2138 void *reduce_orig; /**< original reduction item used for initialization */ 2139 size_t reduce_size; /**< size of data item */ 2140 // three compiler-generated routines (init, fini are optional): 2141 void *reduce_init; /**< data initialization routine (two parameters) */ 2142 void *reduce_fini; /**< data finalization routine */ 2143 void *reduce_comb; /**< data combiner routine */ 2144 kmp_taskred_flags_t flags; /**< flags for additional info from compiler */ 2145 } kmp_taskred_input_t; 2146 /*! 2147 @} 2148 */ 2149 2150 template <typename T> void __kmp_assign_orig(kmp_taskred_data_t &item, T &src); 2151 template <> 2152 void __kmp_assign_orig<kmp_task_red_input_t>(kmp_taskred_data_t &item, 2153 kmp_task_red_input_t &src) { 2154 item.reduce_orig = NULL; 2155 } 2156 template <> 2157 void __kmp_assign_orig<kmp_taskred_input_t>(kmp_taskred_data_t &item, 2158 kmp_taskred_input_t &src) { 2159 if (src.reduce_orig != NULL) { 2160 item.reduce_orig = src.reduce_orig; 2161 } else { 2162 item.reduce_orig = src.reduce_shar; 2163 } // non-NULL reduce_orig means new interface used 2164 } 2165 2166 template <typename T> void __kmp_call_init(kmp_taskred_data_t &item, size_t j); 2167 template <> 2168 void __kmp_call_init<kmp_task_red_input_t>(kmp_taskred_data_t &item, 2169 size_t offset) { 2170 ((void (*)(void *))item.reduce_init)((char *)(item.reduce_priv) + offset); 2171 } 2172 template <> 2173 void __kmp_call_init<kmp_taskred_input_t>(kmp_taskred_data_t &item, 2174 size_t offset) { 2175 ((void (*)(void *, void *))item.reduce_init)( 2176 (char *)(item.reduce_priv) + offset, item.reduce_orig); 2177 } 2178 2179 template <typename T> 2180 void *__kmp_task_reduction_init(int gtid, int num, T *data) { 2181 __kmp_assert_valid_gtid(gtid); 2182 kmp_info_t *thread = __kmp_threads[gtid]; 2183 kmp_taskgroup_t *tg = thread->th.th_current_task->td_taskgroup; 2184 kmp_uint32 nth = thread->th.th_team_nproc; 2185 kmp_taskred_data_t *arr; 2186 2187 // check input data just in case 2188 KMP_ASSERT(tg != NULL); 2189 KMP_ASSERT(data != NULL); 2190 KMP_ASSERT(num > 0); 2191 if (nth == 1) { 2192 KA_TRACE(10, ("__kmpc_task_reduction_init: T#%d, tg %p, exiting nth=1\n", 2193 gtid, tg)); 2194 return (void *)tg; 2195 } 2196 KA_TRACE(10, ("__kmpc_task_reduction_init: T#%d, taskgroup %p, #items %d\n", 2197 gtid, tg, num)); 2198 arr = (kmp_taskred_data_t *)__kmp_thread_malloc( 2199 thread, num * sizeof(kmp_taskred_data_t)); 2200 for (int i = 0; i < num; ++i) { 2201 size_t size = data[i].reduce_size - 1; 2202 // round the size up to cache line per thread-specific item 2203 size += CACHE_LINE - size % CACHE_LINE; 2204 KMP_ASSERT(data[i].reduce_comb != NULL); // combiner is mandatory 2205 arr[i].reduce_shar = data[i].reduce_shar; 2206 arr[i].reduce_size = size; 2207 arr[i].flags = data[i].flags; 2208 arr[i].reduce_comb = data[i].reduce_comb; 2209 arr[i].reduce_init = data[i].reduce_init; 2210 arr[i].reduce_fini = data[i].reduce_fini; 2211 __kmp_assign_orig<T>(arr[i], data[i]); 2212 if (!arr[i].flags.lazy_priv) { 2213 // allocate cache-line aligned block and fill it with zeros 2214 arr[i].reduce_priv = __kmp_allocate(nth * size); 2215 arr[i].reduce_pend = (char *)(arr[i].reduce_priv) + nth * size; 2216 if (arr[i].reduce_init != NULL) { 2217 // initialize all thread-specific items 2218 for (size_t j = 0; j < nth; ++j) { 2219 __kmp_call_init<T>(arr[i], j * size); 2220 } 2221 } 2222 } else { 2223 // only allocate space for pointers now, 2224 // objects will be lazily allocated/initialized if/when requested 2225 // note that __kmp_allocate zeroes the allocated memory 2226 arr[i].reduce_priv = __kmp_allocate(nth * sizeof(void *)); 2227 } 2228 } 2229 tg->reduce_data = (void *)arr; 2230 tg->reduce_num_data = num; 2231 return (void *)tg; 2232 } 2233 2234 /*! 2235 @ingroup TASKING 2236 @param gtid Global thread ID 2237 @param num Number of data items to reduce 2238 @param data Array of data for reduction 2239 @return The taskgroup identifier 2240 2241 Initialize task reduction for the taskgroup. 2242 2243 Note: this entry supposes the optional compiler-generated initializer routine 2244 has single parameter - pointer to object to be initialized. That means 2245 the reduction either does not use omp_orig object, or the omp_orig is accessible 2246 without help of the runtime library. 2247 */ 2248 void *__kmpc_task_reduction_init(int gtid, int num, void *data) { 2249 return __kmp_task_reduction_init(gtid, num, (kmp_task_red_input_t *)data); 2250 } 2251 2252 /*! 2253 @ingroup TASKING 2254 @param gtid Global thread ID 2255 @param num Number of data items to reduce 2256 @param data Array of data for reduction 2257 @return The taskgroup identifier 2258 2259 Initialize task reduction for the taskgroup. 2260 2261 Note: this entry supposes the optional compiler-generated initializer routine 2262 has two parameters, pointer to object to be initialized and pointer to omp_orig 2263 */ 2264 void *__kmpc_taskred_init(int gtid, int num, void *data) { 2265 return __kmp_task_reduction_init(gtid, num, (kmp_taskred_input_t *)data); 2266 } 2267 2268 // Copy task reduction data (except for shared pointers). 2269 template <typename T> 2270 void __kmp_task_reduction_init_copy(kmp_info_t *thr, int num, T *data, 2271 kmp_taskgroup_t *tg, void *reduce_data) { 2272 kmp_taskred_data_t *arr; 2273 KA_TRACE(20, ("__kmp_task_reduction_init_copy: Th %p, init taskgroup %p," 2274 " from data %p\n", 2275 thr, tg, reduce_data)); 2276 arr = (kmp_taskred_data_t *)__kmp_thread_malloc( 2277 thr, num * sizeof(kmp_taskred_data_t)); 2278 // threads will share private copies, thunk routines, sizes, flags, etc.: 2279 KMP_MEMCPY(arr, reduce_data, num * sizeof(kmp_taskred_data_t)); 2280 for (int i = 0; i < num; ++i) { 2281 arr[i].reduce_shar = data[i].reduce_shar; // init unique shared pointers 2282 } 2283 tg->reduce_data = (void *)arr; 2284 tg->reduce_num_data = num; 2285 } 2286 2287 /*! 2288 @ingroup TASKING 2289 @param gtid Global thread ID 2290 @param tskgrp The taskgroup ID (optional) 2291 @param data Shared location of the item 2292 @return The pointer to per-thread data 2293 2294 Get thread-specific location of data item 2295 */ 2296 void *__kmpc_task_reduction_get_th_data(int gtid, void *tskgrp, void *data) { 2297 __kmp_assert_valid_gtid(gtid); 2298 kmp_info_t *thread = __kmp_threads[gtid]; 2299 kmp_int32 nth = thread->th.th_team_nproc; 2300 if (nth == 1) 2301 return data; // nothing to do 2302 2303 kmp_taskgroup_t *tg = (kmp_taskgroup_t *)tskgrp; 2304 if (tg == NULL) 2305 tg = thread->th.th_current_task->td_taskgroup; 2306 KMP_ASSERT(tg != NULL); 2307 kmp_taskred_data_t *arr = (kmp_taskred_data_t *)(tg->reduce_data); 2308 kmp_int32 num = tg->reduce_num_data; 2309 kmp_int32 tid = thread->th.th_info.ds.ds_tid; 2310 2311 KMP_ASSERT(data != NULL); 2312 while (tg != NULL) { 2313 for (int i = 0; i < num; ++i) { 2314 if (!arr[i].flags.lazy_priv) { 2315 if (data == arr[i].reduce_shar || 2316 (data >= arr[i].reduce_priv && data < arr[i].reduce_pend)) 2317 return (char *)(arr[i].reduce_priv) + tid * arr[i].reduce_size; 2318 } else { 2319 // check shared location first 2320 void **p_priv = (void **)(arr[i].reduce_priv); 2321 if (data == arr[i].reduce_shar) 2322 goto found; 2323 // check if we get some thread specific location as parameter 2324 for (int j = 0; j < nth; ++j) 2325 if (data == p_priv[j]) 2326 goto found; 2327 continue; // not found, continue search 2328 found: 2329 if (p_priv[tid] == NULL) { 2330 // allocate thread specific object lazily 2331 p_priv[tid] = __kmp_allocate(arr[i].reduce_size); 2332 if (arr[i].reduce_init != NULL) { 2333 if (arr[i].reduce_orig != NULL) { // new interface 2334 ((void (*)(void *, void *))arr[i].reduce_init)( 2335 p_priv[tid], arr[i].reduce_orig); 2336 } else { // old interface (single parameter) 2337 ((void (*)(void *))arr[i].reduce_init)(p_priv[tid]); 2338 } 2339 } 2340 } 2341 return p_priv[tid]; 2342 } 2343 } 2344 tg = tg->parent; 2345 arr = (kmp_taskred_data_t *)(tg->reduce_data); 2346 num = tg->reduce_num_data; 2347 } 2348 KMP_ASSERT2(0, "Unknown task reduction item"); 2349 return NULL; // ERROR, this line never executed 2350 } 2351 2352 // Finalize task reduction. 2353 // Called from __kmpc_end_taskgroup() 2354 static void __kmp_task_reduction_fini(kmp_info_t *th, kmp_taskgroup_t *tg) { 2355 kmp_int32 nth = th->th.th_team_nproc; 2356 KMP_DEBUG_ASSERT(nth > 1); // should not be called if nth == 1 2357 kmp_taskred_data_t *arr = (kmp_taskred_data_t *)tg->reduce_data; 2358 kmp_int32 num = tg->reduce_num_data; 2359 for (int i = 0; i < num; ++i) { 2360 void *sh_data = arr[i].reduce_shar; 2361 void (*f_fini)(void *) = (void (*)(void *))(arr[i].reduce_fini); 2362 void (*f_comb)(void *, void *) = 2363 (void (*)(void *, void *))(arr[i].reduce_comb); 2364 if (!arr[i].flags.lazy_priv) { 2365 void *pr_data = arr[i].reduce_priv; 2366 size_t size = arr[i].reduce_size; 2367 for (int j = 0; j < nth; ++j) { 2368 void *priv_data = (char *)pr_data + j * size; 2369 f_comb(sh_data, priv_data); // combine results 2370 if (f_fini) 2371 f_fini(priv_data); // finalize if needed 2372 } 2373 } else { 2374 void **pr_data = (void **)(arr[i].reduce_priv); 2375 for (int j = 0; j < nth; ++j) { 2376 if (pr_data[j] != NULL) { 2377 f_comb(sh_data, pr_data[j]); // combine results 2378 if (f_fini) 2379 f_fini(pr_data[j]); // finalize if needed 2380 __kmp_free(pr_data[j]); 2381 } 2382 } 2383 } 2384 __kmp_free(arr[i].reduce_priv); 2385 } 2386 __kmp_thread_free(th, arr); 2387 tg->reduce_data = NULL; 2388 tg->reduce_num_data = 0; 2389 } 2390 2391 // Cleanup task reduction data for parallel or worksharing, 2392 // do not touch task private data other threads still working with. 2393 // Called from __kmpc_end_taskgroup() 2394 static void __kmp_task_reduction_clean(kmp_info_t *th, kmp_taskgroup_t *tg) { 2395 __kmp_thread_free(th, tg->reduce_data); 2396 tg->reduce_data = NULL; 2397 tg->reduce_num_data = 0; 2398 } 2399 2400 template <typename T> 2401 void *__kmp_task_reduction_modifier_init(ident_t *loc, int gtid, int is_ws, 2402 int num, T *data) { 2403 __kmp_assert_valid_gtid(gtid); 2404 kmp_info_t *thr = __kmp_threads[gtid]; 2405 kmp_int32 nth = thr->th.th_team_nproc; 2406 __kmpc_taskgroup(loc, gtid); // form new taskgroup first 2407 if (nth == 1) { 2408 KA_TRACE(10, 2409 ("__kmpc_reduction_modifier_init: T#%d, tg %p, exiting nth=1\n", 2410 gtid, thr->th.th_current_task->td_taskgroup)); 2411 return (void *)thr->th.th_current_task->td_taskgroup; 2412 } 2413 kmp_team_t *team = thr->th.th_team; 2414 void *reduce_data; 2415 kmp_taskgroup_t *tg; 2416 reduce_data = KMP_ATOMIC_LD_RLX(&team->t.t_tg_reduce_data[is_ws]); 2417 if (reduce_data == NULL && 2418 __kmp_atomic_compare_store(&team->t.t_tg_reduce_data[is_ws], reduce_data, 2419 (void *)1)) { 2420 // single thread enters this block to initialize common reduction data 2421 KMP_DEBUG_ASSERT(reduce_data == NULL); 2422 // first initialize own data, then make a copy other threads can use 2423 tg = (kmp_taskgroup_t *)__kmp_task_reduction_init<T>(gtid, num, data); 2424 reduce_data = __kmp_thread_malloc(thr, num * sizeof(kmp_taskred_data_t)); 2425 KMP_MEMCPY(reduce_data, tg->reduce_data, num * sizeof(kmp_taskred_data_t)); 2426 // fini counters should be 0 at this point 2427 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[0]) == 0); 2428 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&team->t.t_tg_fini_counter[1]) == 0); 2429 KMP_ATOMIC_ST_REL(&team->t.t_tg_reduce_data[is_ws], reduce_data); 2430 } else { 2431 while ( 2432 (reduce_data = KMP_ATOMIC_LD_ACQ(&team->t.t_tg_reduce_data[is_ws])) == 2433 (void *)1) { // wait for task reduction initialization 2434 KMP_CPU_PAUSE(); 2435 } 2436 KMP_DEBUG_ASSERT(reduce_data > (void *)1); // should be valid pointer here 2437 tg = thr->th.th_current_task->td_taskgroup; 2438 __kmp_task_reduction_init_copy<T>(thr, num, data, tg, reduce_data); 2439 } 2440 return tg; 2441 } 2442 2443 /*! 2444 @ingroup TASKING 2445 @param loc Source location info 2446 @param gtid Global thread ID 2447 @param is_ws Is 1 if the reduction is for worksharing, 0 otherwise 2448 @param num Number of data items to reduce 2449 @param data Array of data for reduction 2450 @return The taskgroup identifier 2451 2452 Initialize task reduction for a parallel or worksharing. 2453 2454 Note: this entry supposes the optional compiler-generated initializer routine 2455 has single parameter - pointer to object to be initialized. That means 2456 the reduction either does not use omp_orig object, or the omp_orig is accessible 2457 without help of the runtime library. 2458 */ 2459 void *__kmpc_task_reduction_modifier_init(ident_t *loc, int gtid, int is_ws, 2460 int num, void *data) { 2461 return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num, 2462 (kmp_task_red_input_t *)data); 2463 } 2464 2465 /*! 2466 @ingroup TASKING 2467 @param loc Source location info 2468 @param gtid Global thread ID 2469 @param is_ws Is 1 if the reduction is for worksharing, 0 otherwise 2470 @param num Number of data items to reduce 2471 @param data Array of data for reduction 2472 @return The taskgroup identifier 2473 2474 Initialize task reduction for a parallel or worksharing. 2475 2476 Note: this entry supposes the optional compiler-generated initializer routine 2477 has two parameters, pointer to object to be initialized and pointer to omp_orig 2478 */ 2479 void *__kmpc_taskred_modifier_init(ident_t *loc, int gtid, int is_ws, int num, 2480 void *data) { 2481 return __kmp_task_reduction_modifier_init(loc, gtid, is_ws, num, 2482 (kmp_taskred_input_t *)data); 2483 } 2484 2485 /*! 2486 @ingroup TASKING 2487 @param loc Source location info 2488 @param gtid Global thread ID 2489 @param is_ws Is 1 if the reduction is for worksharing, 0 otherwise 2490 2491 Finalize task reduction for a parallel or worksharing. 2492 */ 2493 void __kmpc_task_reduction_modifier_fini(ident_t *loc, int gtid, int is_ws) { 2494 __kmpc_end_taskgroup(loc, gtid); 2495 } 2496 2497 // __kmpc_taskgroup: Start a new taskgroup 2498 void __kmpc_taskgroup(ident_t *loc, int gtid) { 2499 __kmp_assert_valid_gtid(gtid); 2500 kmp_info_t *thread = __kmp_threads[gtid]; 2501 kmp_taskdata_t *taskdata = thread->th.th_current_task; 2502 kmp_taskgroup_t *tg_new = 2503 (kmp_taskgroup_t *)__kmp_thread_malloc(thread, sizeof(kmp_taskgroup_t)); 2504 KA_TRACE(10, ("__kmpc_taskgroup: T#%d loc=%p group=%p\n", gtid, loc, tg_new)); 2505 KMP_ATOMIC_ST_RLX(&tg_new->count, 0); 2506 KMP_ATOMIC_ST_RLX(&tg_new->cancel_request, cancel_noreq); 2507 tg_new->parent = taskdata->td_taskgroup; 2508 tg_new->reduce_data = NULL; 2509 tg_new->reduce_num_data = 0; 2510 taskdata->td_taskgroup = tg_new; 2511 2512 #if OMPT_SUPPORT && OMPT_OPTIONAL 2513 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) { 2514 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid); 2515 if (!codeptr) 2516 codeptr = OMPT_GET_RETURN_ADDRESS(0); 2517 kmp_team_t *team = thread->th.th_team; 2518 ompt_data_t my_task_data = taskdata->ompt_task_info.task_data; 2519 // FIXME: I think this is wrong for lwt! 2520 ompt_data_t my_parallel_data = team->t.ompt_team_info.parallel_data; 2521 2522 ompt_callbacks.ompt_callback(ompt_callback_sync_region)( 2523 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data), 2524 &(my_task_data), codeptr); 2525 } 2526 #endif 2527 } 2528 2529 // __kmpc_end_taskgroup: Wait until all tasks generated by the current task 2530 // and its descendants are complete 2531 void __kmpc_end_taskgroup(ident_t *loc, int gtid) { 2532 __kmp_assert_valid_gtid(gtid); 2533 kmp_info_t *thread = __kmp_threads[gtid]; 2534 kmp_taskdata_t *taskdata = thread->th.th_current_task; 2535 kmp_taskgroup_t *taskgroup = taskdata->td_taskgroup; 2536 int thread_finished = FALSE; 2537 2538 #if OMPT_SUPPORT && OMPT_OPTIONAL 2539 kmp_team_t *team; 2540 ompt_data_t my_task_data; 2541 ompt_data_t my_parallel_data; 2542 void *codeptr; 2543 if (UNLIKELY(ompt_enabled.enabled)) { 2544 team = thread->th.th_team; 2545 my_task_data = taskdata->ompt_task_info.task_data; 2546 // FIXME: I think this is wrong for lwt! 2547 my_parallel_data = team->t.ompt_team_info.parallel_data; 2548 codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid); 2549 if (!codeptr) 2550 codeptr = OMPT_GET_RETURN_ADDRESS(0); 2551 } 2552 #endif 2553 2554 KA_TRACE(10, ("__kmpc_end_taskgroup(enter): T#%d loc=%p\n", gtid, loc)); 2555 KMP_DEBUG_ASSERT(taskgroup != NULL); 2556 KMP_SET_THREAD_STATE_BLOCK(TASKGROUP); 2557 2558 if (__kmp_tasking_mode != tskm_immediate_exec) { 2559 // mark task as waiting not on a barrier 2560 taskdata->td_taskwait_counter += 1; 2561 taskdata->td_taskwait_ident = loc; 2562 taskdata->td_taskwait_thread = gtid + 1; 2563 #if USE_ITT_BUILD 2564 // For ITT the taskgroup wait is similar to taskwait until we need to 2565 // distinguish them 2566 void *itt_sync_obj = __kmp_itt_taskwait_object(gtid); 2567 if (UNLIKELY(itt_sync_obj != NULL)) 2568 __kmp_itt_taskwait_starting(gtid, itt_sync_obj); 2569 #endif /* USE_ITT_BUILD */ 2570 2571 #if OMPT_SUPPORT && OMPT_OPTIONAL 2572 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) { 2573 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)( 2574 ompt_sync_region_taskgroup, ompt_scope_begin, &(my_parallel_data), 2575 &(my_task_data), codeptr); 2576 } 2577 #endif 2578 2579 if (!taskdata->td_flags.team_serial || 2580 (thread->th.th_task_team != NULL && 2581 thread->th.th_task_team->tt.tt_found_proxy_tasks)) { 2582 kmp_flag_32<false, false> flag( 2583 RCAST(std::atomic<kmp_uint32> *, &(taskgroup->count)), 0U); 2584 while (KMP_ATOMIC_LD_ACQ(&taskgroup->count) != 0) { 2585 flag.execute_tasks(thread, gtid, FALSE, 2586 &thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), 2587 __kmp_task_stealing_constraint); 2588 } 2589 } 2590 taskdata->td_taskwait_thread = -taskdata->td_taskwait_thread; // end waiting 2591 2592 #if OMPT_SUPPORT && OMPT_OPTIONAL 2593 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region_wait)) { 2594 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)( 2595 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data), 2596 &(my_task_data), codeptr); 2597 } 2598 #endif 2599 2600 #if USE_ITT_BUILD 2601 if (UNLIKELY(itt_sync_obj != NULL)) 2602 __kmp_itt_taskwait_finished(gtid, itt_sync_obj); 2603 KMP_FSYNC_ACQUIRED(taskdata); // acquire self - sync with descendants 2604 #endif /* USE_ITT_BUILD */ 2605 } 2606 KMP_DEBUG_ASSERT(taskgroup->count == 0); 2607 2608 if (taskgroup->reduce_data != NULL) { // need to reduce? 2609 int cnt; 2610 void *reduce_data; 2611 kmp_team_t *t = thread->th.th_team; 2612 kmp_taskred_data_t *arr = (kmp_taskred_data_t *)taskgroup->reduce_data; 2613 // check if <priv> data of the first reduction variable shared for the team 2614 void *priv0 = arr[0].reduce_priv; 2615 if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[0])) != NULL && 2616 ((kmp_taskred_data_t *)reduce_data)[0].reduce_priv == priv0) { 2617 // finishing task reduction on parallel 2618 cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[0]); 2619 if (cnt == thread->th.th_team_nproc - 1) { 2620 // we are the last thread passing __kmpc_reduction_modifier_fini() 2621 // finalize task reduction: 2622 __kmp_task_reduction_fini(thread, taskgroup); 2623 // cleanup fields in the team structure: 2624 // TODO: is relaxed store enough here (whole barrier should follow)? 2625 __kmp_thread_free(thread, reduce_data); 2626 KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[0], NULL); 2627 KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[0], 0); 2628 } else { 2629 // we are not the last thread passing __kmpc_reduction_modifier_fini(), 2630 // so do not finalize reduction, just clean own copy of the data 2631 __kmp_task_reduction_clean(thread, taskgroup); 2632 } 2633 } else if ((reduce_data = KMP_ATOMIC_LD_ACQ(&t->t.t_tg_reduce_data[1])) != 2634 NULL && 2635 ((kmp_taskred_data_t *)reduce_data)[0].reduce_priv == priv0) { 2636 // finishing task reduction on worksharing 2637 cnt = KMP_ATOMIC_INC(&t->t.t_tg_fini_counter[1]); 2638 if (cnt == thread->th.th_team_nproc - 1) { 2639 // we are the last thread passing __kmpc_reduction_modifier_fini() 2640 __kmp_task_reduction_fini(thread, taskgroup); 2641 // cleanup fields in team structure: 2642 // TODO: is relaxed store enough here (whole barrier should follow)? 2643 __kmp_thread_free(thread, reduce_data); 2644 KMP_ATOMIC_ST_REL(&t->t.t_tg_reduce_data[1], NULL); 2645 KMP_ATOMIC_ST_REL(&t->t.t_tg_fini_counter[1], 0); 2646 } else { 2647 // we are not the last thread passing __kmpc_reduction_modifier_fini(), 2648 // so do not finalize reduction, just clean own copy of the data 2649 __kmp_task_reduction_clean(thread, taskgroup); 2650 } 2651 } else { 2652 // finishing task reduction on taskgroup 2653 __kmp_task_reduction_fini(thread, taskgroup); 2654 } 2655 } 2656 // Restore parent taskgroup for the current task 2657 taskdata->td_taskgroup = taskgroup->parent; 2658 __kmp_thread_free(thread, taskgroup); 2659 2660 KA_TRACE(10, ("__kmpc_end_taskgroup(exit): T#%d task %p finished waiting\n", 2661 gtid, taskdata)); 2662 ANNOTATE_HAPPENS_AFTER(taskdata); 2663 2664 #if OMPT_SUPPORT && OMPT_OPTIONAL 2665 if (UNLIKELY(ompt_enabled.ompt_callback_sync_region)) { 2666 ompt_callbacks.ompt_callback(ompt_callback_sync_region)( 2667 ompt_sync_region_taskgroup, ompt_scope_end, &(my_parallel_data), 2668 &(my_task_data), codeptr); 2669 } 2670 #endif 2671 } 2672 2673 // __kmp_remove_my_task: remove a task from my own deque 2674 static kmp_task_t *__kmp_remove_my_task(kmp_info_t *thread, kmp_int32 gtid, 2675 kmp_task_team_t *task_team, 2676 kmp_int32 is_constrained) { 2677 kmp_task_t *task; 2678 kmp_taskdata_t *taskdata; 2679 kmp_thread_data_t *thread_data; 2680 kmp_uint32 tail; 2681 2682 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec); 2683 KMP_DEBUG_ASSERT(task_team->tt.tt_threads_data != 2684 NULL); // Caller should check this condition 2685 2686 thread_data = &task_team->tt.tt_threads_data[__kmp_tid_from_gtid(gtid)]; 2687 2688 KA_TRACE(10, ("__kmp_remove_my_task(enter): T#%d ntasks=%d head=%u tail=%u\n", 2689 gtid, thread_data->td.td_deque_ntasks, 2690 thread_data->td.td_deque_head, thread_data->td.td_deque_tail)); 2691 2692 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) { 2693 KA_TRACE(10, 2694 ("__kmp_remove_my_task(exit #1): T#%d No tasks to remove: " 2695 "ntasks=%d head=%u tail=%u\n", 2696 gtid, thread_data->td.td_deque_ntasks, 2697 thread_data->td.td_deque_head, thread_data->td.td_deque_tail)); 2698 return NULL; 2699 } 2700 2701 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock); 2702 2703 if (TCR_4(thread_data->td.td_deque_ntasks) == 0) { 2704 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock); 2705 KA_TRACE(10, 2706 ("__kmp_remove_my_task(exit #2): T#%d No tasks to remove: " 2707 "ntasks=%d head=%u tail=%u\n", 2708 gtid, thread_data->td.td_deque_ntasks, 2709 thread_data->td.td_deque_head, thread_data->td.td_deque_tail)); 2710 return NULL; 2711 } 2712 2713 tail = (thread_data->td.td_deque_tail - 1) & 2714 TASK_DEQUE_MASK(thread_data->td); // Wrap index. 2715 taskdata = thread_data->td.td_deque[tail]; 2716 2717 if (!__kmp_task_is_allowed(gtid, is_constrained, taskdata, 2718 thread->th.th_current_task)) { 2719 // The TSC does not allow to steal victim task 2720 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock); 2721 KA_TRACE(10, 2722 ("__kmp_remove_my_task(exit #3): T#%d TSC blocks tail task: " 2723 "ntasks=%d head=%u tail=%u\n", 2724 gtid, thread_data->td.td_deque_ntasks, 2725 thread_data->td.td_deque_head, thread_data->td.td_deque_tail)); 2726 return NULL; 2727 } 2728 2729 thread_data->td.td_deque_tail = tail; 2730 TCW_4(thread_data->td.td_deque_ntasks, thread_data->td.td_deque_ntasks - 1); 2731 2732 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock); 2733 2734 KA_TRACE(10, ("__kmp_remove_my_task(exit #4): T#%d task %p removed: " 2735 "ntasks=%d head=%u tail=%u\n", 2736 gtid, taskdata, thread_data->td.td_deque_ntasks, 2737 thread_data->td.td_deque_head, thread_data->td.td_deque_tail)); 2738 2739 task = KMP_TASKDATA_TO_TASK(taskdata); 2740 return task; 2741 } 2742 2743 // __kmp_steal_task: remove a task from another thread's deque 2744 // Assume that calling thread has already checked existence of 2745 // task_team thread_data before calling this routine. 2746 static kmp_task_t *__kmp_steal_task(kmp_info_t *victim_thr, kmp_int32 gtid, 2747 kmp_task_team_t *task_team, 2748 std::atomic<kmp_int32> *unfinished_threads, 2749 int *thread_finished, 2750 kmp_int32 is_constrained) { 2751 kmp_task_t *task; 2752 kmp_taskdata_t *taskdata; 2753 kmp_taskdata_t *current; 2754 kmp_thread_data_t *victim_td, *threads_data; 2755 kmp_int32 target; 2756 kmp_int32 victim_tid; 2757 2758 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec); 2759 2760 threads_data = task_team->tt.tt_threads_data; 2761 KMP_DEBUG_ASSERT(threads_data != NULL); // Caller should check this condition 2762 2763 victim_tid = victim_thr->th.th_info.ds.ds_tid; 2764 victim_td = &threads_data[victim_tid]; 2765 2766 KA_TRACE(10, ("__kmp_steal_task(enter): T#%d try to steal from T#%d: " 2767 "task_team=%p ntasks=%d head=%u tail=%u\n", 2768 gtid, __kmp_gtid_from_thread(victim_thr), task_team, 2769 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head, 2770 victim_td->td.td_deque_tail)); 2771 2772 if (TCR_4(victim_td->td.td_deque_ntasks) == 0) { 2773 KA_TRACE(10, ("__kmp_steal_task(exit #1): T#%d could not steal from T#%d: " 2774 "task_team=%p ntasks=%d head=%u tail=%u\n", 2775 gtid, __kmp_gtid_from_thread(victim_thr), task_team, 2776 victim_td->td.td_deque_ntasks, victim_td->td.td_deque_head, 2777 victim_td->td.td_deque_tail)); 2778 return NULL; 2779 } 2780 2781 __kmp_acquire_bootstrap_lock(&victim_td->td.td_deque_lock); 2782 2783 int ntasks = TCR_4(victim_td->td.td_deque_ntasks); 2784 // Check again after we acquire the lock 2785 if (ntasks == 0) { 2786 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock); 2787 KA_TRACE(10, ("__kmp_steal_task(exit #2): T#%d could not steal from T#%d: " 2788 "task_team=%p ntasks=%d head=%u tail=%u\n", 2789 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks, 2790 victim_td->td.td_deque_head, victim_td->td.td_deque_tail)); 2791 return NULL; 2792 } 2793 2794 KMP_DEBUG_ASSERT(victim_td->td.td_deque != NULL); 2795 current = __kmp_threads[gtid]->th.th_current_task; 2796 taskdata = victim_td->td.td_deque[victim_td->td.td_deque_head]; 2797 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) { 2798 // Bump head pointer and Wrap. 2799 victim_td->td.td_deque_head = 2800 (victim_td->td.td_deque_head + 1) & TASK_DEQUE_MASK(victim_td->td); 2801 } else { 2802 if (!task_team->tt.tt_untied_task_encountered) { 2803 // The TSC does not allow to steal victim task 2804 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock); 2805 KA_TRACE(10, ("__kmp_steal_task(exit #3): T#%d could not steal from " 2806 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n", 2807 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks, 2808 victim_td->td.td_deque_head, victim_td->td.td_deque_tail)); 2809 return NULL; 2810 } 2811 int i; 2812 // walk through victim's deque trying to steal any task 2813 target = victim_td->td.td_deque_head; 2814 taskdata = NULL; 2815 for (i = 1; i < ntasks; ++i) { 2816 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td); 2817 taskdata = victim_td->td.td_deque[target]; 2818 if (__kmp_task_is_allowed(gtid, is_constrained, taskdata, current)) { 2819 break; // found victim task 2820 } else { 2821 taskdata = NULL; 2822 } 2823 } 2824 if (taskdata == NULL) { 2825 // No appropriate candidate to steal found 2826 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock); 2827 KA_TRACE(10, ("__kmp_steal_task(exit #4): T#%d could not steal from " 2828 "T#%d: task_team=%p ntasks=%d head=%u tail=%u\n", 2829 gtid, __kmp_gtid_from_thread(victim_thr), task_team, ntasks, 2830 victim_td->td.td_deque_head, victim_td->td.td_deque_tail)); 2831 return NULL; 2832 } 2833 int prev = target; 2834 for (i = i + 1; i < ntasks; ++i) { 2835 // shift remaining tasks in the deque left by 1 2836 target = (target + 1) & TASK_DEQUE_MASK(victim_td->td); 2837 victim_td->td.td_deque[prev] = victim_td->td.td_deque[target]; 2838 prev = target; 2839 } 2840 KMP_DEBUG_ASSERT( 2841 victim_td->td.td_deque_tail == 2842 (kmp_uint32)((target + 1) & TASK_DEQUE_MASK(victim_td->td))); 2843 victim_td->td.td_deque_tail = target; // tail -= 1 (wrapped)) 2844 } 2845 if (*thread_finished) { 2846 // We need to un-mark this victim as a finished victim. This must be done 2847 // before releasing the lock, or else other threads (starting with the 2848 // master victim) might be prematurely released from the barrier!!! 2849 kmp_int32 count; 2850 2851 count = KMP_ATOMIC_INC(unfinished_threads); 2852 2853 KA_TRACE( 2854 20, 2855 ("__kmp_steal_task: T#%d inc unfinished_threads to %d: task_team=%p\n", 2856 gtid, count + 1, task_team)); 2857 2858 *thread_finished = FALSE; 2859 } 2860 TCW_4(victim_td->td.td_deque_ntasks, ntasks - 1); 2861 2862 __kmp_release_bootstrap_lock(&victim_td->td.td_deque_lock); 2863 2864 KMP_COUNT_BLOCK(TASK_stolen); 2865 KA_TRACE(10, 2866 ("__kmp_steal_task(exit #5): T#%d stole task %p from T#%d: " 2867 "task_team=%p ntasks=%d head=%u tail=%u\n", 2868 gtid, taskdata, __kmp_gtid_from_thread(victim_thr), task_team, 2869 ntasks, victim_td->td.td_deque_head, victim_td->td.td_deque_tail)); 2870 2871 task = KMP_TASKDATA_TO_TASK(taskdata); 2872 return task; 2873 } 2874 2875 // __kmp_execute_tasks_template: Choose and execute tasks until either the 2876 // condition is statisfied (return true) or there are none left (return false). 2877 // 2878 // final_spin is TRUE if this is the spin at the release barrier. 2879 // thread_finished indicates whether the thread is finished executing all 2880 // the tasks it has on its deque, and is at the release barrier. 2881 // spinner is the location on which to spin. 2882 // spinner == NULL means only execute a single task and return. 2883 // checker is the value to check to terminate the spin. 2884 template <class C> 2885 static inline int __kmp_execute_tasks_template( 2886 kmp_info_t *thread, kmp_int32 gtid, C *flag, int final_spin, 2887 int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj), 2888 kmp_int32 is_constrained) { 2889 kmp_task_team_t *task_team = thread->th.th_task_team; 2890 kmp_thread_data_t *threads_data; 2891 kmp_task_t *task; 2892 kmp_info_t *other_thread; 2893 kmp_taskdata_t *current_task = thread->th.th_current_task; 2894 std::atomic<kmp_int32> *unfinished_threads; 2895 kmp_int32 nthreads, victim_tid = -2, use_own_tasks = 1, new_victim = 0, 2896 tid = thread->th.th_info.ds.ds_tid; 2897 2898 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec); 2899 KMP_DEBUG_ASSERT(thread == __kmp_threads[gtid]); 2900 2901 if (task_team == NULL || current_task == NULL) 2902 return FALSE; 2903 2904 KA_TRACE(15, ("__kmp_execute_tasks_template(enter): T#%d final_spin=%d " 2905 "*thread_finished=%d\n", 2906 gtid, final_spin, *thread_finished)); 2907 2908 thread->th.th_reap_state = KMP_NOT_SAFE_TO_REAP; 2909 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data); 2910 2911 // This can happen when hidden helper task is enabled 2912 if (__kmp_enable_hidden_helper && threads_data == nullptr) 2913 return FALSE; 2914 2915 KMP_DEBUG_ASSERT(threads_data != NULL); 2916 2917 nthreads = task_team->tt.tt_nproc; 2918 unfinished_threads = &(task_team->tt.tt_unfinished_threads); 2919 KMP_DEBUG_ASSERT(nthreads > 1 || task_team->tt.tt_found_proxy_tasks); 2920 KMP_DEBUG_ASSERT(*unfinished_threads >= 0); 2921 2922 while (1) { // Outer loop keeps trying to find tasks in case of single thread 2923 // getting tasks from target constructs 2924 while (1) { // Inner loop to find a task and execute it 2925 task = NULL; 2926 if (use_own_tasks) { // check on own queue first 2927 task = __kmp_remove_my_task(thread, gtid, task_team, is_constrained); 2928 } 2929 if ((task == NULL) && (nthreads > 1)) { // Steal a task 2930 int asleep = 1; 2931 use_own_tasks = 0; 2932 // Try to steal from the last place I stole from successfully. 2933 if (victim_tid == -2) { // haven't stolen anything yet 2934 victim_tid = threads_data[tid].td.td_deque_last_stolen; 2935 if (victim_tid != 2936 -1) // if we have a last stolen from victim, get the thread 2937 other_thread = threads_data[victim_tid].td.td_thr; 2938 } 2939 if (victim_tid != -1) { // found last victim 2940 asleep = 0; 2941 } else if (!new_victim) { // no recent steals and we haven't already 2942 // used a new victim; select a random thread 2943 do { // Find a different thread to steal work from. 2944 // Pick a random thread. Initial plan was to cycle through all the 2945 // threads, and only return if we tried to steal from every thread, 2946 // and failed. Arch says that's not such a great idea. 2947 victim_tid = __kmp_get_random(thread) % (nthreads - 1); 2948 if (victim_tid >= tid) { 2949 ++victim_tid; // Adjusts random distribution to exclude self 2950 } 2951 // Found a potential victim 2952 other_thread = threads_data[victim_tid].td.td_thr; 2953 // There is a slight chance that __kmp_enable_tasking() did not wake 2954 // up all threads waiting at the barrier. If victim is sleeping, 2955 // then wake it up. Since we were going to pay the cache miss 2956 // penalty for referencing another thread's kmp_info_t struct 2957 // anyway, 2958 // the check shouldn't cost too much performance at this point. In 2959 // extra barrier mode, tasks do not sleep at the separate tasking 2960 // barrier, so this isn't a problem. 2961 asleep = 0; 2962 if ((__kmp_tasking_mode == tskm_task_teams) && 2963 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) && 2964 (TCR_PTR(CCAST(void *, other_thread->th.th_sleep_loc)) != 2965 NULL)) { 2966 asleep = 1; 2967 __kmp_null_resume_wrapper(__kmp_gtid_from_thread(other_thread), 2968 other_thread->th.th_sleep_loc); 2969 // A sleeping thread should not have any tasks on it's queue. 2970 // There is a slight possibility that it resumes, steals a task 2971 // from another thread, which spawns more tasks, all in the time 2972 // that it takes this thread to check => don't write an assertion 2973 // that the victim's queue is empty. Try stealing from a 2974 // different thread. 2975 } 2976 } while (asleep); 2977 } 2978 2979 if (!asleep) { 2980 // We have a victim to try to steal from 2981 task = __kmp_steal_task(other_thread, gtid, task_team, 2982 unfinished_threads, thread_finished, 2983 is_constrained); 2984 } 2985 if (task != NULL) { // set last stolen to victim 2986 if (threads_data[tid].td.td_deque_last_stolen != victim_tid) { 2987 threads_data[tid].td.td_deque_last_stolen = victim_tid; 2988 // The pre-refactored code did not try more than 1 successful new 2989 // vicitm, unless the last one generated more local tasks; 2990 // new_victim keeps track of this 2991 new_victim = 1; 2992 } 2993 } else { // No tasks found; unset last_stolen 2994 KMP_CHECK_UPDATE(threads_data[tid].td.td_deque_last_stolen, -1); 2995 victim_tid = -2; // no successful victim found 2996 } 2997 } 2998 2999 if (task == NULL) 3000 break; // break out of tasking loop 3001 3002 // Found a task; execute it 3003 #if USE_ITT_BUILD && USE_ITT_NOTIFY 3004 if (__itt_sync_create_ptr || KMP_ITT_DEBUG) { 3005 if (itt_sync_obj == NULL) { // we are at fork barrier where we could not 3006 // get the object reliably 3007 itt_sync_obj = __kmp_itt_barrier_object(gtid, bs_forkjoin_barrier); 3008 } 3009 __kmp_itt_task_starting(itt_sync_obj); 3010 } 3011 #endif /* USE_ITT_BUILD && USE_ITT_NOTIFY */ 3012 __kmp_invoke_task(gtid, task, current_task); 3013 #if USE_ITT_BUILD 3014 if (itt_sync_obj != NULL) 3015 __kmp_itt_task_finished(itt_sync_obj); 3016 #endif /* USE_ITT_BUILD */ 3017 // If this thread is only partway through the barrier and the condition is 3018 // met, then return now, so that the barrier gather/release pattern can 3019 // proceed. If this thread is in the last spin loop in the barrier, 3020 // waiting to be released, we know that the termination condition will not 3021 // be satisfied, so don't waste any cycles checking it. 3022 if (flag == NULL || (!final_spin && flag->done_check())) { 3023 KA_TRACE( 3024 15, 3025 ("__kmp_execute_tasks_template: T#%d spin condition satisfied\n", 3026 gtid)); 3027 return TRUE; 3028 } 3029 if (thread->th.th_task_team == NULL) { 3030 break; 3031 } 3032 KMP_YIELD(__kmp_library == library_throughput); // Yield before next task 3033 // If execution of a stolen task results in more tasks being placed on our 3034 // run queue, reset use_own_tasks 3035 if (!use_own_tasks && TCR_4(threads_data[tid].td.td_deque_ntasks) != 0) { 3036 KA_TRACE(20, ("__kmp_execute_tasks_template: T#%d stolen task spawned " 3037 "other tasks, restart\n", 3038 gtid)); 3039 use_own_tasks = 1; 3040 new_victim = 0; 3041 } 3042 } 3043 3044 // The task source has been exhausted. If in final spin loop of barrier, 3045 // check if termination condition is satisfied. The work queue may be empty 3046 // but there might be proxy tasks still executing. 3047 if (final_spin && 3048 KMP_ATOMIC_LD_ACQ(¤t_task->td_incomplete_child_tasks) == 0) { 3049 // First, decrement the #unfinished threads, if that has not already been 3050 // done. This decrement might be to the spin location, and result in the 3051 // termination condition being satisfied. 3052 if (!*thread_finished) { 3053 kmp_int32 count; 3054 3055 count = KMP_ATOMIC_DEC(unfinished_threads) - 1; 3056 KA_TRACE(20, ("__kmp_execute_tasks_template: T#%d dec " 3057 "unfinished_threads to %d task_team=%p\n", 3058 gtid, count, task_team)); 3059 *thread_finished = TRUE; 3060 } 3061 3062 // It is now unsafe to reference thread->th.th_team !!! 3063 // Decrementing task_team->tt.tt_unfinished_threads can allow the master 3064 // thread to pass through the barrier, where it might reset each thread's 3065 // th.th_team field for the next parallel region. If we can steal more 3066 // work, we know that this has not happened yet. 3067 if (flag != NULL && flag->done_check()) { 3068 KA_TRACE( 3069 15, 3070 ("__kmp_execute_tasks_template: T#%d spin condition satisfied\n", 3071 gtid)); 3072 return TRUE; 3073 } 3074 } 3075 3076 // If this thread's task team is NULL, master has recognized that there are 3077 // no more tasks; bail out 3078 if (thread->th.th_task_team == NULL) { 3079 KA_TRACE(15, 3080 ("__kmp_execute_tasks_template: T#%d no more tasks\n", gtid)); 3081 return FALSE; 3082 } 3083 3084 // We could be getting tasks from target constructs; if this is the only 3085 // thread, keep trying to execute tasks from own queue 3086 if (nthreads == 1) 3087 use_own_tasks = 1; 3088 else { 3089 KA_TRACE(15, 3090 ("__kmp_execute_tasks_template: T#%d can't find work\n", gtid)); 3091 return FALSE; 3092 } 3093 } 3094 } 3095 3096 template <bool C, bool S> 3097 int __kmp_execute_tasks_32( 3098 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_32<C, S> *flag, int final_spin, 3099 int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj), 3100 kmp_int32 is_constrained) { 3101 return __kmp_execute_tasks_template( 3102 thread, gtid, flag, final_spin, 3103 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained); 3104 } 3105 3106 template <bool C, bool S> 3107 int __kmp_execute_tasks_64( 3108 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_64<C, S> *flag, int final_spin, 3109 int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj), 3110 kmp_int32 is_constrained) { 3111 return __kmp_execute_tasks_template( 3112 thread, gtid, flag, final_spin, 3113 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained); 3114 } 3115 3116 int __kmp_execute_tasks_oncore( 3117 kmp_info_t *thread, kmp_int32 gtid, kmp_flag_oncore *flag, int final_spin, 3118 int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj), 3119 kmp_int32 is_constrained) { 3120 return __kmp_execute_tasks_template( 3121 thread, gtid, flag, final_spin, 3122 thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained); 3123 } 3124 3125 template int 3126 __kmp_execute_tasks_32<false, false>(kmp_info_t *, kmp_int32, 3127 kmp_flag_32<false, false> *, int, 3128 int *USE_ITT_BUILD_ARG(void *), kmp_int32); 3129 3130 template int __kmp_execute_tasks_64<false, true>(kmp_info_t *, kmp_int32, 3131 kmp_flag_64<false, true> *, 3132 int, 3133 int *USE_ITT_BUILD_ARG(void *), 3134 kmp_int32); 3135 3136 template int __kmp_execute_tasks_64<true, false>(kmp_info_t *, kmp_int32, 3137 kmp_flag_64<true, false> *, 3138 int, 3139 int *USE_ITT_BUILD_ARG(void *), 3140 kmp_int32); 3141 3142 // __kmp_enable_tasking: Allocate task team and resume threads sleeping at the 3143 // next barrier so they can assist in executing enqueued tasks. 3144 // First thread in allocates the task team atomically. 3145 static void __kmp_enable_tasking(kmp_task_team_t *task_team, 3146 kmp_info_t *this_thr) { 3147 kmp_thread_data_t *threads_data; 3148 int nthreads, i, is_init_thread; 3149 3150 KA_TRACE(10, ("__kmp_enable_tasking(enter): T#%d\n", 3151 __kmp_gtid_from_thread(this_thr))); 3152 3153 KMP_DEBUG_ASSERT(task_team != NULL); 3154 KMP_DEBUG_ASSERT(this_thr->th.th_team != NULL); 3155 3156 nthreads = task_team->tt.tt_nproc; 3157 KMP_DEBUG_ASSERT(nthreads > 0); 3158 KMP_DEBUG_ASSERT(nthreads == this_thr->th.th_team->t.t_nproc); 3159 3160 // Allocate or increase the size of threads_data if necessary 3161 is_init_thread = __kmp_realloc_task_threads_data(this_thr, task_team); 3162 3163 if (!is_init_thread) { 3164 // Some other thread already set up the array. 3165 KA_TRACE( 3166 20, 3167 ("__kmp_enable_tasking(exit): T#%d: threads array already set up.\n", 3168 __kmp_gtid_from_thread(this_thr))); 3169 return; 3170 } 3171 threads_data = (kmp_thread_data_t *)TCR_PTR(task_team->tt.tt_threads_data); 3172 KMP_DEBUG_ASSERT(threads_data != NULL); 3173 3174 if (__kmp_tasking_mode == tskm_task_teams && 3175 (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME)) { 3176 // Release any threads sleeping at the barrier, so that they can steal 3177 // tasks and execute them. In extra barrier mode, tasks do not sleep 3178 // at the separate tasking barrier, so this isn't a problem. 3179 for (i = 0; i < nthreads; i++) { 3180 volatile void *sleep_loc; 3181 kmp_info_t *thread = threads_data[i].td.td_thr; 3182 3183 if (i == this_thr->th.th_info.ds.ds_tid) { 3184 continue; 3185 } 3186 // Since we haven't locked the thread's suspend mutex lock at this 3187 // point, there is a small window where a thread might be putting 3188 // itself to sleep, but hasn't set the th_sleep_loc field yet. 3189 // To work around this, __kmp_execute_tasks_template() periodically checks 3190 // see if other threads are sleeping (using the same random mechanism that 3191 // is used for task stealing) and awakens them if they are. 3192 if ((sleep_loc = TCR_PTR(CCAST(void *, thread->th.th_sleep_loc))) != 3193 NULL) { 3194 KF_TRACE(50, ("__kmp_enable_tasking: T#%d waking up thread T#%d\n", 3195 __kmp_gtid_from_thread(this_thr), 3196 __kmp_gtid_from_thread(thread))); 3197 __kmp_null_resume_wrapper(__kmp_gtid_from_thread(thread), sleep_loc); 3198 } else { 3199 KF_TRACE(50, ("__kmp_enable_tasking: T#%d don't wake up thread T#%d\n", 3200 __kmp_gtid_from_thread(this_thr), 3201 __kmp_gtid_from_thread(thread))); 3202 } 3203 } 3204 } 3205 3206 KA_TRACE(10, ("__kmp_enable_tasking(exit): T#%d\n", 3207 __kmp_gtid_from_thread(this_thr))); 3208 } 3209 3210 /* // TODO: Check the comment consistency 3211 * Utility routines for "task teams". A task team (kmp_task_t) is kind of 3212 * like a shadow of the kmp_team_t data struct, with a different lifetime. 3213 * After a child * thread checks into a barrier and calls __kmp_release() from 3214 * the particular variant of __kmp_<barrier_kind>_barrier_gather(), it can no 3215 * longer assume that the kmp_team_t structure is intact (at any moment, the 3216 * master thread may exit the barrier code and free the team data structure, 3217 * and return the threads to the thread pool). 3218 * 3219 * This does not work with the tasking code, as the thread is still 3220 * expected to participate in the execution of any tasks that may have been 3221 * spawned my a member of the team, and the thread still needs access to all 3222 * to each thread in the team, so that it can steal work from it. 3223 * 3224 * Enter the existence of the kmp_task_team_t struct. It employs a reference 3225 * counting mechanism, and is allocated by the master thread before calling 3226 * __kmp_<barrier_kind>_release, and then is release by the last thread to 3227 * exit __kmp_<barrier_kind>_release at the next barrier. I.e. the lifetimes 3228 * of the kmp_task_team_t structs for consecutive barriers can overlap 3229 * (and will, unless the master thread is the last thread to exit the barrier 3230 * release phase, which is not typical). The existence of such a struct is 3231 * useful outside the context of tasking. 3232 * 3233 * We currently use the existence of the threads array as an indicator that 3234 * tasks were spawned since the last barrier. If the structure is to be 3235 * useful outside the context of tasking, then this will have to change, but 3236 * not setting the field minimizes the performance impact of tasking on 3237 * barriers, when no explicit tasks were spawned (pushed, actually). 3238 */ 3239 3240 static kmp_task_team_t *__kmp_free_task_teams = 3241 NULL; // Free list for task_team data structures 3242 // Lock for task team data structures 3243 kmp_bootstrap_lock_t __kmp_task_team_lock = 3244 KMP_BOOTSTRAP_LOCK_INITIALIZER(__kmp_task_team_lock); 3245 3246 // __kmp_alloc_task_deque: 3247 // Allocates a task deque for a particular thread, and initialize the necessary 3248 // data structures relating to the deque. This only happens once per thread 3249 // per task team since task teams are recycled. No lock is needed during 3250 // allocation since each thread allocates its own deque. 3251 static void __kmp_alloc_task_deque(kmp_info_t *thread, 3252 kmp_thread_data_t *thread_data) { 3253 __kmp_init_bootstrap_lock(&thread_data->td.td_deque_lock); 3254 KMP_DEBUG_ASSERT(thread_data->td.td_deque == NULL); 3255 3256 // Initialize last stolen task field to "none" 3257 thread_data->td.td_deque_last_stolen = -1; 3258 3259 KMP_DEBUG_ASSERT(TCR_4(thread_data->td.td_deque_ntasks) == 0); 3260 KMP_DEBUG_ASSERT(thread_data->td.td_deque_head == 0); 3261 KMP_DEBUG_ASSERT(thread_data->td.td_deque_tail == 0); 3262 3263 KE_TRACE( 3264 10, 3265 ("__kmp_alloc_task_deque: T#%d allocating deque[%d] for thread_data %p\n", 3266 __kmp_gtid_from_thread(thread), INITIAL_TASK_DEQUE_SIZE, thread_data)); 3267 // Allocate space for task deque, and zero the deque 3268 // Cannot use __kmp_thread_calloc() because threads not around for 3269 // kmp_reap_task_team( ). 3270 thread_data->td.td_deque = (kmp_taskdata_t **)__kmp_allocate( 3271 INITIAL_TASK_DEQUE_SIZE * sizeof(kmp_taskdata_t *)); 3272 thread_data->td.td_deque_size = INITIAL_TASK_DEQUE_SIZE; 3273 } 3274 3275 // __kmp_free_task_deque: 3276 // Deallocates a task deque for a particular thread. Happens at library 3277 // deallocation so don't need to reset all thread data fields. 3278 static void __kmp_free_task_deque(kmp_thread_data_t *thread_data) { 3279 if (thread_data->td.td_deque != NULL) { 3280 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock); 3281 TCW_4(thread_data->td.td_deque_ntasks, 0); 3282 __kmp_free(thread_data->td.td_deque); 3283 thread_data->td.td_deque = NULL; 3284 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock); 3285 } 3286 3287 #ifdef BUILD_TIED_TASK_STACK 3288 // GEH: Figure out what to do here for td_susp_tied_tasks 3289 if (thread_data->td.td_susp_tied_tasks.ts_entries != TASK_STACK_EMPTY) { 3290 __kmp_free_task_stack(__kmp_thread_from_gtid(gtid), thread_data); 3291 } 3292 #endif // BUILD_TIED_TASK_STACK 3293 } 3294 3295 // __kmp_realloc_task_threads_data: 3296 // Allocates a threads_data array for a task team, either by allocating an 3297 // initial array or enlarging an existing array. Only the first thread to get 3298 // the lock allocs or enlarges the array and re-initializes the array elements. 3299 // That thread returns "TRUE", the rest return "FALSE". 3300 // Assumes that the new array size is given by task_team -> tt.tt_nproc. 3301 // The current size is given by task_team -> tt.tt_max_threads. 3302 static int __kmp_realloc_task_threads_data(kmp_info_t *thread, 3303 kmp_task_team_t *task_team) { 3304 kmp_thread_data_t **threads_data_p; 3305 kmp_int32 nthreads, maxthreads; 3306 int is_init_thread = FALSE; 3307 3308 if (TCR_4(task_team->tt.tt_found_tasks)) { 3309 // Already reallocated and initialized. 3310 return FALSE; 3311 } 3312 3313 threads_data_p = &task_team->tt.tt_threads_data; 3314 nthreads = task_team->tt.tt_nproc; 3315 maxthreads = task_team->tt.tt_max_threads; 3316 3317 // All threads must lock when they encounter the first task of the implicit 3318 // task region to make sure threads_data fields are (re)initialized before 3319 // used. 3320 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock); 3321 3322 if (!TCR_4(task_team->tt.tt_found_tasks)) { 3323 // first thread to enable tasking 3324 kmp_team_t *team = thread->th.th_team; 3325 int i; 3326 3327 is_init_thread = TRUE; 3328 if (maxthreads < nthreads) { 3329 3330 if (*threads_data_p != NULL) { 3331 kmp_thread_data_t *old_data = *threads_data_p; 3332 kmp_thread_data_t *new_data = NULL; 3333 3334 KE_TRACE( 3335 10, 3336 ("__kmp_realloc_task_threads_data: T#%d reallocating " 3337 "threads data for task_team %p, new_size = %d, old_size = %d\n", 3338 __kmp_gtid_from_thread(thread), task_team, nthreads, maxthreads)); 3339 // Reallocate threads_data to have more elements than current array 3340 // Cannot use __kmp_thread_realloc() because threads not around for 3341 // kmp_reap_task_team( ). Note all new array entries are initialized 3342 // to zero by __kmp_allocate(). 3343 new_data = (kmp_thread_data_t *)__kmp_allocate( 3344 nthreads * sizeof(kmp_thread_data_t)); 3345 // copy old data to new data 3346 KMP_MEMCPY_S((void *)new_data, nthreads * sizeof(kmp_thread_data_t), 3347 (void *)old_data, maxthreads * sizeof(kmp_thread_data_t)); 3348 3349 #ifdef BUILD_TIED_TASK_STACK 3350 // GEH: Figure out if this is the right thing to do 3351 for (i = maxthreads; i < nthreads; i++) { 3352 kmp_thread_data_t *thread_data = &(*threads_data_p)[i]; 3353 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data); 3354 } 3355 #endif // BUILD_TIED_TASK_STACK 3356 // Install the new data and free the old data 3357 (*threads_data_p) = new_data; 3358 __kmp_free(old_data); 3359 } else { 3360 KE_TRACE(10, ("__kmp_realloc_task_threads_data: T#%d allocating " 3361 "threads data for task_team %p, size = %d\n", 3362 __kmp_gtid_from_thread(thread), task_team, nthreads)); 3363 // Make the initial allocate for threads_data array, and zero entries 3364 // Cannot use __kmp_thread_calloc() because threads not around for 3365 // kmp_reap_task_team( ). 3366 ANNOTATE_IGNORE_WRITES_BEGIN(); 3367 *threads_data_p = (kmp_thread_data_t *)__kmp_allocate( 3368 nthreads * sizeof(kmp_thread_data_t)); 3369 ANNOTATE_IGNORE_WRITES_END(); 3370 #ifdef BUILD_TIED_TASK_STACK 3371 // GEH: Figure out if this is the right thing to do 3372 for (i = 0; i < nthreads; i++) { 3373 kmp_thread_data_t *thread_data = &(*threads_data_p)[i]; 3374 __kmp_init_task_stack(__kmp_gtid_from_thread(thread), thread_data); 3375 } 3376 #endif // BUILD_TIED_TASK_STACK 3377 } 3378 task_team->tt.tt_max_threads = nthreads; 3379 } else { 3380 // If array has (more than) enough elements, go ahead and use it 3381 KMP_DEBUG_ASSERT(*threads_data_p != NULL); 3382 } 3383 3384 // initialize threads_data pointers back to thread_info structures 3385 for (i = 0; i < nthreads; i++) { 3386 kmp_thread_data_t *thread_data = &(*threads_data_p)[i]; 3387 thread_data->td.td_thr = team->t.t_threads[i]; 3388 3389 if (thread_data->td.td_deque_last_stolen >= nthreads) { 3390 // The last stolen field survives across teams / barrier, and the number 3391 // of threads may have changed. It's possible (likely?) that a new 3392 // parallel region will exhibit the same behavior as previous region. 3393 thread_data->td.td_deque_last_stolen = -1; 3394 } 3395 } 3396 3397 KMP_MB(); 3398 TCW_SYNC_4(task_team->tt.tt_found_tasks, TRUE); 3399 } 3400 3401 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock); 3402 return is_init_thread; 3403 } 3404 3405 // __kmp_free_task_threads_data: 3406 // Deallocates a threads_data array for a task team, including any attached 3407 // tasking deques. Only occurs at library shutdown. 3408 static void __kmp_free_task_threads_data(kmp_task_team_t *task_team) { 3409 __kmp_acquire_bootstrap_lock(&task_team->tt.tt_threads_lock); 3410 if (task_team->tt.tt_threads_data != NULL) { 3411 int i; 3412 for (i = 0; i < task_team->tt.tt_max_threads; i++) { 3413 __kmp_free_task_deque(&task_team->tt.tt_threads_data[i]); 3414 } 3415 __kmp_free(task_team->tt.tt_threads_data); 3416 task_team->tt.tt_threads_data = NULL; 3417 } 3418 __kmp_release_bootstrap_lock(&task_team->tt.tt_threads_lock); 3419 } 3420 3421 // __kmp_allocate_task_team: 3422 // Allocates a task team associated with a specific team, taking it from 3423 // the global task team free list if possible. Also initializes data 3424 // structures. 3425 static kmp_task_team_t *__kmp_allocate_task_team(kmp_info_t *thread, 3426 kmp_team_t *team) { 3427 kmp_task_team_t *task_team = NULL; 3428 int nthreads; 3429 3430 KA_TRACE(20, ("__kmp_allocate_task_team: T#%d entering; team = %p\n", 3431 (thread ? __kmp_gtid_from_thread(thread) : -1), team)); 3432 3433 if (TCR_PTR(__kmp_free_task_teams) != NULL) { 3434 // Take a task team from the task team pool 3435 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock); 3436 if (__kmp_free_task_teams != NULL) { 3437 task_team = __kmp_free_task_teams; 3438 TCW_PTR(__kmp_free_task_teams, task_team->tt.tt_next); 3439 task_team->tt.tt_next = NULL; 3440 } 3441 __kmp_release_bootstrap_lock(&__kmp_task_team_lock); 3442 } 3443 3444 if (task_team == NULL) { 3445 KE_TRACE(10, ("__kmp_allocate_task_team: T#%d allocating " 3446 "task team for team %p\n", 3447 __kmp_gtid_from_thread(thread), team)); 3448 // Allocate a new task team if one is not available. Cannot use 3449 // __kmp_thread_malloc because threads not around for kmp_reap_task_team. 3450 task_team = (kmp_task_team_t *)__kmp_allocate(sizeof(kmp_task_team_t)); 3451 __kmp_init_bootstrap_lock(&task_team->tt.tt_threads_lock); 3452 #if USE_ITT_BUILD && USE_ITT_NOTIFY && KMP_DEBUG 3453 // suppress race conditions detection on synchronization flags in debug mode 3454 // this helps to analyze library internals eliminating false positives 3455 __itt_suppress_mark_range( 3456 __itt_suppress_range, __itt_suppress_threading_errors, 3457 &task_team->tt.tt_found_tasks, sizeof(task_team->tt.tt_found_tasks)); 3458 __itt_suppress_mark_range(__itt_suppress_range, 3459 __itt_suppress_threading_errors, 3460 CCAST(kmp_uint32 *, &task_team->tt.tt_active), 3461 sizeof(task_team->tt.tt_active)); 3462 #endif /* USE_ITT_BUILD && USE_ITT_NOTIFY && KMP_DEBUG */ 3463 // Note: __kmp_allocate zeroes returned memory, othewise we would need: 3464 // task_team->tt.tt_threads_data = NULL; 3465 // task_team->tt.tt_max_threads = 0; 3466 // task_team->tt.tt_next = NULL; 3467 } 3468 3469 TCW_4(task_team->tt.tt_found_tasks, FALSE); 3470 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE); 3471 task_team->tt.tt_nproc = nthreads = team->t.t_nproc; 3472 3473 KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads, nthreads); 3474 KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_hidden_helper_tasks, 0); 3475 TCW_4(task_team->tt.tt_hidden_helper_task_encountered, FALSE); 3476 TCW_4(task_team->tt.tt_active, TRUE); 3477 3478 KA_TRACE(20, ("__kmp_allocate_task_team: T#%d exiting; task_team = %p " 3479 "unfinished_threads init'd to %d\n", 3480 (thread ? __kmp_gtid_from_thread(thread) : -1), task_team, 3481 KMP_ATOMIC_LD_RLX(&task_team->tt.tt_unfinished_threads))); 3482 return task_team; 3483 } 3484 3485 // __kmp_free_task_team: 3486 // Frees the task team associated with a specific thread, and adds it 3487 // to the global task team free list. 3488 void __kmp_free_task_team(kmp_info_t *thread, kmp_task_team_t *task_team) { 3489 KA_TRACE(20, ("__kmp_free_task_team: T#%d task_team = %p\n", 3490 thread ? __kmp_gtid_from_thread(thread) : -1, task_team)); 3491 3492 // Put task team back on free list 3493 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock); 3494 3495 KMP_DEBUG_ASSERT(task_team->tt.tt_next == NULL); 3496 task_team->tt.tt_next = __kmp_free_task_teams; 3497 TCW_PTR(__kmp_free_task_teams, task_team); 3498 3499 __kmp_release_bootstrap_lock(&__kmp_task_team_lock); 3500 } 3501 3502 // __kmp_reap_task_teams: 3503 // Free all the task teams on the task team free list. 3504 // Should only be done during library shutdown. 3505 // Cannot do anything that needs a thread structure or gtid since they are 3506 // already gone. 3507 void __kmp_reap_task_teams(void) { 3508 kmp_task_team_t *task_team; 3509 3510 if (TCR_PTR(__kmp_free_task_teams) != NULL) { 3511 // Free all task_teams on the free list 3512 __kmp_acquire_bootstrap_lock(&__kmp_task_team_lock); 3513 while ((task_team = __kmp_free_task_teams) != NULL) { 3514 __kmp_free_task_teams = task_team->tt.tt_next; 3515 task_team->tt.tt_next = NULL; 3516 3517 // Free threads_data if necessary 3518 if (task_team->tt.tt_threads_data != NULL) { 3519 __kmp_free_task_threads_data(task_team); 3520 } 3521 __kmp_free(task_team); 3522 } 3523 __kmp_release_bootstrap_lock(&__kmp_task_team_lock); 3524 } 3525 } 3526 3527 // __kmp_wait_to_unref_task_teams: 3528 // Some threads could still be in the fork barrier release code, possibly 3529 // trying to steal tasks. Wait for each thread to unreference its task team. 3530 void __kmp_wait_to_unref_task_teams(void) { 3531 kmp_info_t *thread; 3532 kmp_uint32 spins; 3533 int done; 3534 3535 KMP_INIT_YIELD(spins); 3536 3537 for (;;) { 3538 done = TRUE; 3539 3540 // TODO: GEH - this may be is wrong because some sync would be necessary 3541 // in case threads are added to the pool during the traversal. Need to 3542 // verify that lock for thread pool is held when calling this routine. 3543 for (thread = CCAST(kmp_info_t *, __kmp_thread_pool); thread != NULL; 3544 thread = thread->th.th_next_pool) { 3545 #if KMP_OS_WINDOWS 3546 DWORD exit_val; 3547 #endif 3548 if (TCR_PTR(thread->th.th_task_team) == NULL) { 3549 KA_TRACE(10, ("__kmp_wait_to_unref_task_team: T#%d task_team == NULL\n", 3550 __kmp_gtid_from_thread(thread))); 3551 continue; 3552 } 3553 #if KMP_OS_WINDOWS 3554 // TODO: GEH - add this check for Linux* OS / OS X* as well? 3555 if (!__kmp_is_thread_alive(thread, &exit_val)) { 3556 thread->th.th_task_team = NULL; 3557 continue; 3558 } 3559 #endif 3560 3561 done = FALSE; // Because th_task_team pointer is not NULL for this thread 3562 3563 KA_TRACE(10, ("__kmp_wait_to_unref_task_team: Waiting for T#%d to " 3564 "unreference task_team\n", 3565 __kmp_gtid_from_thread(thread))); 3566 3567 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) { 3568 volatile void *sleep_loc; 3569 // If the thread is sleeping, awaken it. 3570 if ((sleep_loc = TCR_PTR(CCAST(void *, thread->th.th_sleep_loc))) != 3571 NULL) { 3572 KA_TRACE( 3573 10, 3574 ("__kmp_wait_to_unref_task_team: T#%d waking up thread T#%d\n", 3575 __kmp_gtid_from_thread(thread), __kmp_gtid_from_thread(thread))); 3576 __kmp_null_resume_wrapper(__kmp_gtid_from_thread(thread), sleep_loc); 3577 } 3578 } 3579 } 3580 if (done) { 3581 break; 3582 } 3583 3584 // If oversubscribed or have waited a bit, yield. 3585 KMP_YIELD_OVERSUB_ELSE_SPIN(spins); 3586 } 3587 } 3588 3589 // __kmp_task_team_setup: Create a task_team for the current team, but use 3590 // an already created, unused one if it already exists. 3591 void __kmp_task_team_setup(kmp_info_t *this_thr, kmp_team_t *team, int always) { 3592 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec); 3593 3594 // If this task_team hasn't been created yet, allocate it. It will be used in 3595 // the region after the next. 3596 // If it exists, it is the current task team and shouldn't be touched yet as 3597 // it may still be in use. 3598 if (team->t.t_task_team[this_thr->th.th_task_state] == NULL && 3599 (always || team->t.t_nproc > 1)) { 3600 team->t.t_task_team[this_thr->th.th_task_state] = 3601 __kmp_allocate_task_team(this_thr, team); 3602 KA_TRACE(20, ("__kmp_task_team_setup: Master T#%d created new task_team %p " 3603 "for team %d at parity=%d\n", 3604 __kmp_gtid_from_thread(this_thr), 3605 team->t.t_task_team[this_thr->th.th_task_state], 3606 ((team != NULL) ? team->t.t_id : -1), 3607 this_thr->th.th_task_state)); 3608 } 3609 3610 // After threads exit the release, they will call sync, and then point to this 3611 // other task_team; make sure it is allocated and properly initialized. As 3612 // threads spin in the barrier release phase, they will continue to use the 3613 // previous task_team struct(above), until they receive the signal to stop 3614 // checking for tasks (they can't safely reference the kmp_team_t struct, 3615 // which could be reallocated by the master thread). No task teams are formed 3616 // for serialized teams. 3617 if (team->t.t_nproc > 1) { 3618 int other_team = 1 - this_thr->th.th_task_state; 3619 if (team->t.t_task_team[other_team] == NULL) { // setup other team as well 3620 team->t.t_task_team[other_team] = 3621 __kmp_allocate_task_team(this_thr, team); 3622 KA_TRACE(20, ("__kmp_task_team_setup: Master T#%d created second new " 3623 "task_team %p for team %d at parity=%d\n", 3624 __kmp_gtid_from_thread(this_thr), 3625 team->t.t_task_team[other_team], 3626 ((team != NULL) ? team->t.t_id : -1), other_team)); 3627 } else { // Leave the old task team struct in place for the upcoming region; 3628 // adjust as needed 3629 kmp_task_team_t *task_team = team->t.t_task_team[other_team]; 3630 if (!task_team->tt.tt_active || 3631 team->t.t_nproc != task_team->tt.tt_nproc) { 3632 TCW_4(task_team->tt.tt_nproc, team->t.t_nproc); 3633 TCW_4(task_team->tt.tt_found_tasks, FALSE); 3634 TCW_4(task_team->tt.tt_found_proxy_tasks, FALSE); 3635 KMP_ATOMIC_ST_REL(&task_team->tt.tt_unfinished_threads, 3636 team->t.t_nproc); 3637 TCW_4(task_team->tt.tt_active, TRUE); 3638 } 3639 // if team size has changed, the first thread to enable tasking will 3640 // realloc threads_data if necessary 3641 KA_TRACE(20, ("__kmp_task_team_setup: Master T#%d reset next task_team " 3642 "%p for team %d at parity=%d\n", 3643 __kmp_gtid_from_thread(this_thr), 3644 team->t.t_task_team[other_team], 3645 ((team != NULL) ? team->t.t_id : -1), other_team)); 3646 } 3647 } 3648 3649 // For regular thread, task enabling should be called when the task is going 3650 // to be pushed to a dequeue. However, for the hidden helper thread, we need 3651 // it ahead of time so that some operations can be performed without race 3652 // condition. 3653 if (this_thr == __kmp_hidden_helper_main_thread) { 3654 for (int i = 0; i < 2; ++i) { 3655 kmp_task_team_t *task_team = team->t.t_task_team[i]; 3656 if (KMP_TASKING_ENABLED(task_team)) { 3657 continue; 3658 } 3659 __kmp_enable_tasking(task_team, this_thr); 3660 for (int j = 0; j < task_team->tt.tt_nproc; ++j) { 3661 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[j]; 3662 if (thread_data->td.td_deque == NULL) { 3663 __kmp_alloc_task_deque(__kmp_hidden_helper_threads[j], thread_data); 3664 } 3665 } 3666 } 3667 } 3668 } 3669 3670 // __kmp_task_team_sync: Propagation of task team data from team to threads 3671 // which happens just after the release phase of a team barrier. This may be 3672 // called by any thread, but only for teams with # threads > 1. 3673 void __kmp_task_team_sync(kmp_info_t *this_thr, kmp_team_t *team) { 3674 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec); 3675 3676 // Toggle the th_task_state field, to switch which task_team this thread 3677 // refers to 3678 this_thr->th.th_task_state = (kmp_uint8)(1 - this_thr->th.th_task_state); 3679 3680 // It is now safe to propagate the task team pointer from the team struct to 3681 // the current thread. 3682 TCW_PTR(this_thr->th.th_task_team, 3683 team->t.t_task_team[this_thr->th.th_task_state]); 3684 KA_TRACE(20, 3685 ("__kmp_task_team_sync: Thread T#%d task team switched to task_team " 3686 "%p from Team #%d (parity=%d)\n", 3687 __kmp_gtid_from_thread(this_thr), this_thr->th.th_task_team, 3688 ((team != NULL) ? team->t.t_id : -1), this_thr->th.th_task_state)); 3689 } 3690 3691 // __kmp_task_team_wait: Master thread waits for outstanding tasks after the 3692 // barrier gather phase. Only called by master thread if #threads in team > 1 or 3693 // if proxy tasks were created. 3694 // 3695 // wait is a flag that defaults to 1 (see kmp.h), but waiting can be turned off 3696 // by passing in 0 optionally as the last argument. When wait is zero, master 3697 // thread does not wait for unfinished_threads to reach 0. 3698 void __kmp_task_team_wait( 3699 kmp_info_t *this_thr, 3700 kmp_team_t *team USE_ITT_BUILD_ARG(void *itt_sync_obj), int wait) { 3701 kmp_task_team_t *task_team = team->t.t_task_team[this_thr->th.th_task_state]; 3702 3703 KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec); 3704 KMP_DEBUG_ASSERT(task_team == this_thr->th.th_task_team); 3705 3706 if ((task_team != NULL) && KMP_TASKING_ENABLED(task_team)) { 3707 if (wait) { 3708 KA_TRACE(20, ("__kmp_task_team_wait: Master T#%d waiting for all tasks " 3709 "(for unfinished_threads to reach 0) on task_team = %p\n", 3710 __kmp_gtid_from_thread(this_thr), task_team)); 3711 // Worker threads may have dropped through to release phase, but could 3712 // still be executing tasks. Wait here for tasks to complete. To avoid 3713 // memory contention, only master thread checks termination condition. 3714 kmp_flag_32<false, false> flag( 3715 RCAST(std::atomic<kmp_uint32> *, 3716 &task_team->tt.tt_unfinished_threads), 3717 0U); 3718 flag.wait(this_thr, TRUE USE_ITT_BUILD_ARG(itt_sync_obj)); 3719 } 3720 // Deactivate the old task team, so that the worker threads will stop 3721 // referencing it while spinning. 3722 KA_TRACE( 3723 20, 3724 ("__kmp_task_team_wait: Master T#%d deactivating task_team %p: " 3725 "setting active to false, setting local and team's pointer to NULL\n", 3726 __kmp_gtid_from_thread(this_thr), task_team)); 3727 KMP_DEBUG_ASSERT(task_team->tt.tt_nproc > 1 || 3728 task_team->tt.tt_found_proxy_tasks == TRUE); 3729 TCW_SYNC_4(task_team->tt.tt_found_proxy_tasks, FALSE); 3730 KMP_CHECK_UPDATE(task_team->tt.tt_untied_task_encountered, 0); 3731 TCW_SYNC_4(task_team->tt.tt_active, FALSE); 3732 KMP_MB(); 3733 3734 TCW_PTR(this_thr->th.th_task_team, NULL); 3735 } 3736 3737 if (__kmp_enable_hidden_helper && task_team && 3738 task_team->tt.tt_hidden_helper_task_encountered) 3739 while (KMP_ATOMIC_LD_ACQ(&task_team->tt.tt_unfinished_hidden_helper_tasks)) 3740 ; 3741 } 3742 3743 // __kmp_tasking_barrier: 3744 // This routine is called only when __kmp_tasking_mode == tskm_extra_barrier. 3745 // Internal function to execute all tasks prior to a regular barrier or a join 3746 // barrier. It is a full barrier itself, which unfortunately turns regular 3747 // barriers into double barriers and join barriers into 1 1/2 barriers. 3748 void __kmp_tasking_barrier(kmp_team_t *team, kmp_info_t *thread, int gtid) { 3749 std::atomic<kmp_uint32> *spin = RCAST( 3750 std::atomic<kmp_uint32> *, 3751 &team->t.t_task_team[thread->th.th_task_state]->tt.tt_unfinished_threads); 3752 int flag = FALSE; 3753 KMP_DEBUG_ASSERT(__kmp_tasking_mode == tskm_extra_barrier); 3754 3755 #if USE_ITT_BUILD 3756 KMP_FSYNC_SPIN_INIT(spin, NULL); 3757 #endif /* USE_ITT_BUILD */ 3758 kmp_flag_32<false, false> spin_flag(spin, 0U); 3759 while (!spin_flag.execute_tasks(thread, gtid, TRUE, 3760 &flag USE_ITT_BUILD_ARG(NULL), 0)) { 3761 #if USE_ITT_BUILD 3762 // TODO: What about itt_sync_obj?? 3763 KMP_FSYNC_SPIN_PREPARE(RCAST(void *, spin)); 3764 #endif /* USE_ITT_BUILD */ 3765 3766 if (TCR_4(__kmp_global.g.g_done)) { 3767 if (__kmp_global.g.g_abort) 3768 __kmp_abort_thread(); 3769 break; 3770 } 3771 KMP_YIELD(TRUE); 3772 } 3773 #if USE_ITT_BUILD 3774 KMP_FSYNC_SPIN_ACQUIRED(RCAST(void *, spin)); 3775 #endif /* USE_ITT_BUILD */ 3776 } 3777 3778 // __kmp_give_task puts a task into a given thread queue if: 3779 // - the queue for that thread was created 3780 // - there's space in that queue 3781 // Because of this, __kmp_push_task needs to check if there's space after 3782 // getting the lock 3783 static bool __kmp_give_task(kmp_info_t *thread, kmp_int32 tid, kmp_task_t *task, 3784 kmp_int32 pass) { 3785 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task); 3786 kmp_task_team_t *task_team = taskdata->td_task_team; 3787 3788 KA_TRACE(20, ("__kmp_give_task: trying to give task %p to thread %d.\n", 3789 taskdata, tid)); 3790 3791 // If task_team is NULL something went really bad... 3792 KMP_DEBUG_ASSERT(task_team != NULL); 3793 3794 bool result = false; 3795 kmp_thread_data_t *thread_data = &task_team->tt.tt_threads_data[tid]; 3796 3797 if (thread_data->td.td_deque == NULL) { 3798 // There's no queue in this thread, go find another one 3799 // We're guaranteed that at least one thread has a queue 3800 KA_TRACE(30, 3801 ("__kmp_give_task: thread %d has no queue while giving task %p.\n", 3802 tid, taskdata)); 3803 return result; 3804 } 3805 3806 if (TCR_4(thread_data->td.td_deque_ntasks) >= 3807 TASK_DEQUE_SIZE(thread_data->td)) { 3808 KA_TRACE( 3809 30, 3810 ("__kmp_give_task: queue is full while giving task %p to thread %d.\n", 3811 taskdata, tid)); 3812 3813 // if this deque is bigger than the pass ratio give a chance to another 3814 // thread 3815 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass) 3816 return result; 3817 3818 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock); 3819 if (TCR_4(thread_data->td.td_deque_ntasks) >= 3820 TASK_DEQUE_SIZE(thread_data->td)) { 3821 // expand deque to push the task which is not allowed to execute 3822 __kmp_realloc_task_deque(thread, thread_data); 3823 } 3824 3825 } else { 3826 3827 __kmp_acquire_bootstrap_lock(&thread_data->td.td_deque_lock); 3828 3829 if (TCR_4(thread_data->td.td_deque_ntasks) >= 3830 TASK_DEQUE_SIZE(thread_data->td)) { 3831 KA_TRACE(30, ("__kmp_give_task: queue is full while giving task %p to " 3832 "thread %d.\n", 3833 taskdata, tid)); 3834 3835 // if this deque is bigger than the pass ratio give a chance to another 3836 // thread 3837 if (TASK_DEQUE_SIZE(thread_data->td) / INITIAL_TASK_DEQUE_SIZE >= pass) 3838 goto release_and_exit; 3839 3840 __kmp_realloc_task_deque(thread, thread_data); 3841 } 3842 } 3843 3844 // lock is held here, and there is space in the deque 3845 3846 thread_data->td.td_deque[thread_data->td.td_deque_tail] = taskdata; 3847 // Wrap index. 3848 thread_data->td.td_deque_tail = 3849 (thread_data->td.td_deque_tail + 1) & TASK_DEQUE_MASK(thread_data->td); 3850 TCW_4(thread_data->td.td_deque_ntasks, 3851 TCR_4(thread_data->td.td_deque_ntasks) + 1); 3852 3853 result = true; 3854 KA_TRACE(30, ("__kmp_give_task: successfully gave task %p to thread %d.\n", 3855 taskdata, tid)); 3856 3857 release_and_exit: 3858 __kmp_release_bootstrap_lock(&thread_data->td.td_deque_lock); 3859 3860 return result; 3861 } 3862 3863 /* The finish of the proxy tasks is divided in two pieces: 3864 - the top half is the one that can be done from a thread outside the team 3865 - the bottom half must be run from a thread within the team 3866 3867 In order to run the bottom half the task gets queued back into one of the 3868 threads of the team. Once the td_incomplete_child_task counter of the parent 3869 is decremented the threads can leave the barriers. So, the bottom half needs 3870 to be queued before the counter is decremented. The top half is therefore 3871 divided in two parts: 3872 - things that can be run before queuing the bottom half 3873 - things that must be run after queuing the bottom half 3874 3875 This creates a second race as the bottom half can free the task before the 3876 second top half is executed. To avoid this we use the 3877 td_incomplete_child_task of the proxy task to synchronize the top and bottom 3878 half. */ 3879 static void __kmp_first_top_half_finish_proxy(kmp_taskdata_t *taskdata) { 3880 KMP_DEBUG_ASSERT(taskdata->td_flags.tasktype == TASK_EXPLICIT); 3881 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY); 3882 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 0); 3883 KMP_DEBUG_ASSERT(taskdata->td_flags.freed == 0); 3884 3885 taskdata->td_flags.complete = 1; // mark the task as completed 3886 3887 if (taskdata->td_taskgroup) 3888 KMP_ATOMIC_DEC(&taskdata->td_taskgroup->count); 3889 3890 // Create an imaginary children for this task so the bottom half cannot 3891 // release the task before we have completed the second top half 3892 KMP_ATOMIC_INC(&taskdata->td_incomplete_child_tasks); 3893 } 3894 3895 static void __kmp_second_top_half_finish_proxy(kmp_taskdata_t *taskdata) { 3896 kmp_int32 children = 0; 3897 3898 // Predecrement simulated by "- 1" calculation 3899 children = 3900 KMP_ATOMIC_DEC(&taskdata->td_parent->td_incomplete_child_tasks) - 1; 3901 KMP_DEBUG_ASSERT(children >= 0); 3902 3903 // Remove the imaginary children 3904 KMP_ATOMIC_DEC(&taskdata->td_incomplete_child_tasks); 3905 } 3906 3907 static void __kmp_bottom_half_finish_proxy(kmp_int32 gtid, kmp_task_t *ptask) { 3908 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask); 3909 kmp_info_t *thread = __kmp_threads[gtid]; 3910 3911 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY); 3912 KMP_DEBUG_ASSERT(taskdata->td_flags.complete == 3913 1); // top half must run before bottom half 3914 3915 // We need to wait to make sure the top half is finished 3916 // Spinning here should be ok as this should happen quickly 3917 while (KMP_ATOMIC_LD_ACQ(&taskdata->td_incomplete_child_tasks) > 0) 3918 ; 3919 3920 __kmp_release_deps(gtid, taskdata); 3921 __kmp_free_task_and_ancestors(gtid, taskdata, thread); 3922 } 3923 3924 /*! 3925 @ingroup TASKING 3926 @param gtid Global Thread ID of encountering thread 3927 @param ptask Task which execution is completed 3928 3929 Execute the completion of a proxy task from a thread of that is part of the 3930 team. Run first and bottom halves directly. 3931 */ 3932 void __kmpc_proxy_task_completed(kmp_int32 gtid, kmp_task_t *ptask) { 3933 KMP_DEBUG_ASSERT(ptask != NULL); 3934 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask); 3935 KA_TRACE( 3936 10, ("__kmp_proxy_task_completed(enter): T#%d proxy task %p completing\n", 3937 gtid, taskdata)); 3938 __kmp_assert_valid_gtid(gtid); 3939 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY); 3940 3941 __kmp_first_top_half_finish_proxy(taskdata); 3942 __kmp_second_top_half_finish_proxy(taskdata); 3943 __kmp_bottom_half_finish_proxy(gtid, ptask); 3944 3945 KA_TRACE(10, 3946 ("__kmp_proxy_task_completed(exit): T#%d proxy task %p completing\n", 3947 gtid, taskdata)); 3948 } 3949 3950 /*! 3951 @ingroup TASKING 3952 @param ptask Task which execution is completed 3953 3954 Execute the completion of a proxy task from a thread that could not belong to 3955 the team. 3956 */ 3957 void __kmpc_proxy_task_completed_ooo(kmp_task_t *ptask) { 3958 KMP_DEBUG_ASSERT(ptask != NULL); 3959 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask); 3960 3961 KA_TRACE( 3962 10, 3963 ("__kmp_proxy_task_completed_ooo(enter): proxy task completing ooo %p\n", 3964 taskdata)); 3965 3966 KMP_DEBUG_ASSERT(taskdata->td_flags.proxy == TASK_PROXY); 3967 3968 __kmp_first_top_half_finish_proxy(taskdata); 3969 3970 // Enqueue task to complete bottom half completion from a thread within the 3971 // corresponding team 3972 kmp_team_t *team = taskdata->td_team; 3973 kmp_int32 nthreads = team->t.t_nproc; 3974 kmp_info_t *thread; 3975 3976 // This should be similar to start_k = __kmp_get_random( thread ) % nthreads 3977 // but we cannot use __kmp_get_random here 3978 kmp_int32 start_k = 0; 3979 kmp_int32 pass = 1; 3980 kmp_int32 k = start_k; 3981 3982 do { 3983 // For now we're just linearly trying to find a thread 3984 thread = team->t.t_threads[k]; 3985 k = (k + 1) % nthreads; 3986 3987 // we did a full pass through all the threads 3988 if (k == start_k) 3989 pass = pass << 1; 3990 3991 } while (!__kmp_give_task(thread, k, ptask, pass)); 3992 3993 __kmp_second_top_half_finish_proxy(taskdata); 3994 3995 KA_TRACE( 3996 10, 3997 ("__kmp_proxy_task_completed_ooo(exit): proxy task completing ooo %p\n", 3998 taskdata)); 3999 } 4000 4001 kmp_event_t *__kmpc_task_allow_completion_event(ident_t *loc_ref, int gtid, 4002 kmp_task_t *task) { 4003 kmp_taskdata_t *td = KMP_TASK_TO_TASKDATA(task); 4004 if (td->td_allow_completion_event.type == KMP_EVENT_UNINITIALIZED) { 4005 td->td_allow_completion_event.type = KMP_EVENT_ALLOW_COMPLETION; 4006 td->td_allow_completion_event.ed.task = task; 4007 __kmp_init_tas_lock(&td->td_allow_completion_event.lock); 4008 } 4009 return &td->td_allow_completion_event; 4010 } 4011 4012 void __kmp_fulfill_event(kmp_event_t *event) { 4013 if (event->type == KMP_EVENT_ALLOW_COMPLETION) { 4014 kmp_task_t *ptask = event->ed.task; 4015 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(ptask); 4016 bool detached = false; 4017 int gtid = __kmp_get_gtid(); 4018 4019 // The associated task might have completed or could be completing at this 4020 // point. 4021 // We need to take the lock to avoid races 4022 __kmp_acquire_tas_lock(&event->lock, gtid); 4023 if (taskdata->td_flags.proxy == TASK_PROXY) { 4024 detached = true; 4025 } else { 4026 #if OMPT_SUPPORT 4027 // The OMPT event must occur under mutual exclusion, 4028 // otherwise the tool might access ptask after free 4029 if (UNLIKELY(ompt_enabled.enabled)) 4030 __ompt_task_finish(ptask, NULL, ompt_task_early_fulfill); 4031 #endif 4032 } 4033 event->type = KMP_EVENT_UNINITIALIZED; 4034 __kmp_release_tas_lock(&event->lock, gtid); 4035 4036 if (detached) { 4037 #if OMPT_SUPPORT 4038 // We free ptask afterwards and know the task is finished, 4039 // so locking is not necessary 4040 if (UNLIKELY(ompt_enabled.enabled)) 4041 __ompt_task_finish(ptask, NULL, ompt_task_late_fulfill); 4042 #endif 4043 // If the task detached complete the proxy task 4044 if (gtid >= 0) { 4045 kmp_team_t *team = taskdata->td_team; 4046 kmp_info_t *thread = __kmp_get_thread(); 4047 if (thread->th.th_team == team) { 4048 __kmpc_proxy_task_completed(gtid, ptask); 4049 return; 4050 } 4051 } 4052 4053 // fallback 4054 __kmpc_proxy_task_completed_ooo(ptask); 4055 } 4056 } 4057 } 4058 4059 // __kmp_task_dup_alloc: Allocate the taskdata and make a copy of source task 4060 // for taskloop 4061 // 4062 // thread: allocating thread 4063 // task_src: pointer to source task to be duplicated 4064 // returns: a pointer to the allocated kmp_task_t structure (task). 4065 kmp_task_t *__kmp_task_dup_alloc(kmp_info_t *thread, kmp_task_t *task_src) { 4066 kmp_task_t *task; 4067 kmp_taskdata_t *taskdata; 4068 kmp_taskdata_t *taskdata_src = KMP_TASK_TO_TASKDATA(task_src); 4069 kmp_taskdata_t *parent_task = taskdata_src->td_parent; // same parent task 4070 size_t shareds_offset; 4071 size_t task_size; 4072 4073 KA_TRACE(10, ("__kmp_task_dup_alloc(enter): Th %p, source task %p\n", thread, 4074 task_src)); 4075 KMP_DEBUG_ASSERT(taskdata_src->td_flags.proxy == 4076 TASK_FULL); // it should not be proxy task 4077 KMP_DEBUG_ASSERT(taskdata_src->td_flags.tasktype == TASK_EXPLICIT); 4078 task_size = taskdata_src->td_size_alloc; 4079 4080 // Allocate a kmp_taskdata_t block and a kmp_task_t block. 4081 KA_TRACE(30, ("__kmp_task_dup_alloc: Th %p, malloc size %ld\n", thread, 4082 task_size)); 4083 #if USE_FAST_MEMORY 4084 taskdata = (kmp_taskdata_t *)__kmp_fast_allocate(thread, task_size); 4085 #else 4086 taskdata = (kmp_taskdata_t *)__kmp_thread_malloc(thread, task_size); 4087 #endif /* USE_FAST_MEMORY */ 4088 KMP_MEMCPY(taskdata, taskdata_src, task_size); 4089 4090 task = KMP_TASKDATA_TO_TASK(taskdata); 4091 4092 // Initialize new task (only specific fields not affected by memcpy) 4093 taskdata->td_task_id = KMP_GEN_TASK_ID(); 4094 if (task->shareds != NULL) { // need setup shareds pointer 4095 shareds_offset = (char *)task_src->shareds - (char *)taskdata_src; 4096 task->shareds = &((char *)taskdata)[shareds_offset]; 4097 KMP_DEBUG_ASSERT((((kmp_uintptr_t)task->shareds) & (sizeof(void *) - 1)) == 4098 0); 4099 } 4100 taskdata->td_alloc_thread = thread; 4101 taskdata->td_parent = parent_task; 4102 // task inherits the taskgroup from the parent task 4103 taskdata->td_taskgroup = parent_task->td_taskgroup; 4104 // tied task needs to initialize the td_last_tied at creation, 4105 // untied one does this when it is scheduled for execution 4106 if (taskdata->td_flags.tiedness == TASK_TIED) 4107 taskdata->td_last_tied = taskdata; 4108 4109 // Only need to keep track of child task counts if team parallel and tasking 4110 // not serialized 4111 if (!(taskdata->td_flags.team_serial || taskdata->td_flags.tasking_ser)) { 4112 KMP_ATOMIC_INC(&parent_task->td_incomplete_child_tasks); 4113 if (parent_task->td_taskgroup) 4114 KMP_ATOMIC_INC(&parent_task->td_taskgroup->count); 4115 // Only need to keep track of allocated child tasks for explicit tasks since 4116 // implicit not deallocated 4117 if (taskdata->td_parent->td_flags.tasktype == TASK_EXPLICIT) 4118 KMP_ATOMIC_INC(&taskdata->td_parent->td_allocated_child_tasks); 4119 } 4120 4121 KA_TRACE(20, 4122 ("__kmp_task_dup_alloc(exit): Th %p, created task %p, parent=%p\n", 4123 thread, taskdata, taskdata->td_parent)); 4124 #if OMPT_SUPPORT 4125 if (UNLIKELY(ompt_enabled.enabled)) 4126 __ompt_task_init(taskdata, thread->th.th_info.ds.ds_gtid); 4127 #endif 4128 return task; 4129 } 4130 4131 // Routine optionally generated by the compiler for setting the lastprivate flag 4132 // and calling needed constructors for private/firstprivate objects 4133 // (used to form taskloop tasks from pattern task) 4134 // Parameters: dest task, src task, lastprivate flag. 4135 typedef void (*p_task_dup_t)(kmp_task_t *, kmp_task_t *, kmp_int32); 4136 4137 KMP_BUILD_ASSERT(sizeof(long) == 4 || sizeof(long) == 8); 4138 4139 // class to encapsulate manipulating loop bounds in a taskloop task. 4140 // this abstracts away the Intel vs GOMP taskloop interface for setting/getting 4141 // the loop bound variables. 4142 class kmp_taskloop_bounds_t { 4143 kmp_task_t *task; 4144 const kmp_taskdata_t *taskdata; 4145 size_t lower_offset; 4146 size_t upper_offset; 4147 4148 public: 4149 kmp_taskloop_bounds_t(kmp_task_t *_task, kmp_uint64 *lb, kmp_uint64 *ub) 4150 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(task)), 4151 lower_offset((char *)lb - (char *)task), 4152 upper_offset((char *)ub - (char *)task) { 4153 KMP_DEBUG_ASSERT((char *)lb > (char *)_task); 4154 KMP_DEBUG_ASSERT((char *)ub > (char *)_task); 4155 } 4156 kmp_taskloop_bounds_t(kmp_task_t *_task, const kmp_taskloop_bounds_t &bounds) 4157 : task(_task), taskdata(KMP_TASK_TO_TASKDATA(_task)), 4158 lower_offset(bounds.lower_offset), upper_offset(bounds.upper_offset) {} 4159 size_t get_lower_offset() const { return lower_offset; } 4160 size_t get_upper_offset() const { return upper_offset; } 4161 kmp_uint64 get_lb() const { 4162 kmp_int64 retval; 4163 #if defined(KMP_GOMP_COMPAT) 4164 // Intel task just returns the lower bound normally 4165 if (!taskdata->td_flags.native) { 4166 retval = *(kmp_int64 *)((char *)task + lower_offset); 4167 } else { 4168 // GOMP task has to take into account the sizeof(long) 4169 if (taskdata->td_size_loop_bounds == 4) { 4170 kmp_int32 *lb = RCAST(kmp_int32 *, task->shareds); 4171 retval = (kmp_int64)*lb; 4172 } else { 4173 kmp_int64 *lb = RCAST(kmp_int64 *, task->shareds); 4174 retval = (kmp_int64)*lb; 4175 } 4176 } 4177 #else 4178 retval = *(kmp_int64 *)((char *)task + lower_offset); 4179 #endif // defined(KMP_GOMP_COMPAT) 4180 return retval; 4181 } 4182 kmp_uint64 get_ub() const { 4183 kmp_int64 retval; 4184 #if defined(KMP_GOMP_COMPAT) 4185 // Intel task just returns the upper bound normally 4186 if (!taskdata->td_flags.native) { 4187 retval = *(kmp_int64 *)((char *)task + upper_offset); 4188 } else { 4189 // GOMP task has to take into account the sizeof(long) 4190 if (taskdata->td_size_loop_bounds == 4) { 4191 kmp_int32 *ub = RCAST(kmp_int32 *, task->shareds) + 1; 4192 retval = (kmp_int64)*ub; 4193 } else { 4194 kmp_int64 *ub = RCAST(kmp_int64 *, task->shareds) + 1; 4195 retval = (kmp_int64)*ub; 4196 } 4197 } 4198 #else 4199 retval = *(kmp_int64 *)((char *)task + upper_offset); 4200 #endif // defined(KMP_GOMP_COMPAT) 4201 return retval; 4202 } 4203 void set_lb(kmp_uint64 lb) { 4204 #if defined(KMP_GOMP_COMPAT) 4205 // Intel task just sets the lower bound normally 4206 if (!taskdata->td_flags.native) { 4207 *(kmp_uint64 *)((char *)task + lower_offset) = lb; 4208 } else { 4209 // GOMP task has to take into account the sizeof(long) 4210 if (taskdata->td_size_loop_bounds == 4) { 4211 kmp_uint32 *lower = RCAST(kmp_uint32 *, task->shareds); 4212 *lower = (kmp_uint32)lb; 4213 } else { 4214 kmp_uint64 *lower = RCAST(kmp_uint64 *, task->shareds); 4215 *lower = (kmp_uint64)lb; 4216 } 4217 } 4218 #else 4219 *(kmp_uint64 *)((char *)task + lower_offset) = lb; 4220 #endif // defined(KMP_GOMP_COMPAT) 4221 } 4222 void set_ub(kmp_uint64 ub) { 4223 #if defined(KMP_GOMP_COMPAT) 4224 // Intel task just sets the upper bound normally 4225 if (!taskdata->td_flags.native) { 4226 *(kmp_uint64 *)((char *)task + upper_offset) = ub; 4227 } else { 4228 // GOMP task has to take into account the sizeof(long) 4229 if (taskdata->td_size_loop_bounds == 4) { 4230 kmp_uint32 *upper = RCAST(kmp_uint32 *, task->shareds) + 1; 4231 *upper = (kmp_uint32)ub; 4232 } else { 4233 kmp_uint64 *upper = RCAST(kmp_uint64 *, task->shareds) + 1; 4234 *upper = (kmp_uint64)ub; 4235 } 4236 } 4237 #else 4238 *(kmp_uint64 *)((char *)task + upper_offset) = ub; 4239 #endif // defined(KMP_GOMP_COMPAT) 4240 } 4241 }; 4242 4243 // __kmp_taskloop_linear: Start tasks of the taskloop linearly 4244 // 4245 // loc Source location information 4246 // gtid Global thread ID 4247 // task Pattern task, exposes the loop iteration range 4248 // lb Pointer to loop lower bound in task structure 4249 // ub Pointer to loop upper bound in task structure 4250 // st Loop stride 4251 // ub_glob Global upper bound (used for lastprivate check) 4252 // num_tasks Number of tasks to execute 4253 // grainsize Number of loop iterations per task 4254 // extras Number of chunks with grainsize+1 iterations 4255 // last_chunk Reduction of grainsize for last task 4256 // tc Iterations count 4257 // task_dup Tasks duplication routine 4258 // codeptr_ra Return address for OMPT events 4259 void __kmp_taskloop_linear(ident_t *loc, int gtid, kmp_task_t *task, 4260 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, 4261 kmp_uint64 ub_glob, kmp_uint64 num_tasks, 4262 kmp_uint64 grainsize, kmp_uint64 extras, 4263 kmp_int64 last_chunk, kmp_uint64 tc, 4264 #if OMPT_SUPPORT 4265 void *codeptr_ra, 4266 #endif 4267 void *task_dup) { 4268 KMP_COUNT_BLOCK(OMP_TASKLOOP); 4269 KMP_TIME_PARTITIONED_BLOCK(OMP_taskloop_scheduling); 4270 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup; 4271 // compiler provides global bounds here 4272 kmp_taskloop_bounds_t task_bounds(task, lb, ub); 4273 kmp_uint64 lower = task_bounds.get_lb(); 4274 kmp_uint64 upper = task_bounds.get_ub(); 4275 kmp_uint64 i; 4276 kmp_info_t *thread = __kmp_threads[gtid]; 4277 kmp_taskdata_t *current_task = thread->th.th_current_task; 4278 kmp_task_t *next_task; 4279 kmp_int32 lastpriv = 0; 4280 4281 KMP_DEBUG_ASSERT( 4282 tc == num_tasks * grainsize + (last_chunk < 0 ? last_chunk : extras)); 4283 KMP_DEBUG_ASSERT(num_tasks > extras); 4284 KMP_DEBUG_ASSERT(num_tasks > 0); 4285 KA_TRACE(20, ("__kmp_taskloop_linear: T#%d: %lld tasks, grainsize %lld, " 4286 "extras %lld, last_chunk %lld, i=%lld,%lld(%d)%lld, dup %p\n", 4287 gtid, num_tasks, grainsize, extras, last_chunk, lower, upper, 4288 ub_glob, st, task_dup)); 4289 4290 // Launch num_tasks tasks, assign grainsize iterations each task 4291 for (i = 0; i < num_tasks; ++i) { 4292 kmp_uint64 chunk_minus_1; 4293 if (extras == 0) { 4294 chunk_minus_1 = grainsize - 1; 4295 } else { 4296 chunk_minus_1 = grainsize; 4297 --extras; // first extras iterations get bigger chunk (grainsize+1) 4298 } 4299 upper = lower + st * chunk_minus_1; 4300 if (upper > *ub) { 4301 upper = *ub; 4302 } 4303 if (i == num_tasks - 1) { 4304 // schedule the last task, set lastprivate flag if needed 4305 if (st == 1) { // most common case 4306 KMP_DEBUG_ASSERT(upper == *ub); 4307 if (upper == ub_glob) 4308 lastpriv = 1; 4309 } else if (st > 0) { // positive loop stride 4310 KMP_DEBUG_ASSERT((kmp_uint64)st > *ub - upper); 4311 if ((kmp_uint64)st > ub_glob - upper) 4312 lastpriv = 1; 4313 } else { // negative loop stride 4314 KMP_DEBUG_ASSERT(upper + st < *ub); 4315 if (upper - ub_glob < (kmp_uint64)(-st)) 4316 lastpriv = 1; 4317 } 4318 } 4319 next_task = __kmp_task_dup_alloc(thread, task); // allocate new task 4320 kmp_taskdata_t *next_taskdata = KMP_TASK_TO_TASKDATA(next_task); 4321 kmp_taskloop_bounds_t next_task_bounds = 4322 kmp_taskloop_bounds_t(next_task, task_bounds); 4323 4324 // adjust task-specific bounds 4325 next_task_bounds.set_lb(lower); 4326 if (next_taskdata->td_flags.native) { 4327 next_task_bounds.set_ub(upper + (st > 0 ? 1 : -1)); 4328 } else { 4329 next_task_bounds.set_ub(upper); 4330 } 4331 if (ptask_dup != NULL) // set lastprivate flag, construct firstprivates, 4332 // etc. 4333 ptask_dup(next_task, task, lastpriv); 4334 KA_TRACE(40, 4335 ("__kmp_taskloop_linear: T#%d; task #%llu: task %p: lower %lld, " 4336 "upper %lld stride %lld, (offsets %p %p)\n", 4337 gtid, i, next_task, lower, upper, st, 4338 next_task_bounds.get_lower_offset(), 4339 next_task_bounds.get_upper_offset())); 4340 #if OMPT_SUPPORT 4341 __kmp_omp_taskloop_task(NULL, gtid, next_task, 4342 codeptr_ra); // schedule new task 4343 #else 4344 __kmp_omp_task(gtid, next_task, true); // schedule new task 4345 #endif 4346 lower = upper + st; // adjust lower bound for the next iteration 4347 } 4348 // free the pattern task and exit 4349 __kmp_task_start(gtid, task, current_task); // make internal bookkeeping 4350 // do not execute the pattern task, just do internal bookkeeping 4351 __kmp_task_finish<false>(gtid, task, current_task); 4352 } 4353 4354 // Structure to keep taskloop parameters for auxiliary task 4355 // kept in the shareds of the task structure. 4356 typedef struct __taskloop_params { 4357 kmp_task_t *task; 4358 kmp_uint64 *lb; 4359 kmp_uint64 *ub; 4360 void *task_dup; 4361 kmp_int64 st; 4362 kmp_uint64 ub_glob; 4363 kmp_uint64 num_tasks; 4364 kmp_uint64 grainsize; 4365 kmp_uint64 extras; 4366 kmp_int64 last_chunk; 4367 kmp_uint64 tc; 4368 kmp_uint64 num_t_min; 4369 #if OMPT_SUPPORT 4370 void *codeptr_ra; 4371 #endif 4372 } __taskloop_params_t; 4373 4374 void __kmp_taskloop_recur(ident_t *, int, kmp_task_t *, kmp_uint64 *, 4375 kmp_uint64 *, kmp_int64, kmp_uint64, kmp_uint64, 4376 kmp_uint64, kmp_uint64, kmp_int64, kmp_uint64, 4377 kmp_uint64, 4378 #if OMPT_SUPPORT 4379 void *, 4380 #endif 4381 void *); 4382 4383 // Execute part of the taskloop submitted as a task. 4384 int __kmp_taskloop_task(int gtid, void *ptask) { 4385 __taskloop_params_t *p = 4386 (__taskloop_params_t *)((kmp_task_t *)ptask)->shareds; 4387 kmp_task_t *task = p->task; 4388 kmp_uint64 *lb = p->lb; 4389 kmp_uint64 *ub = p->ub; 4390 void *task_dup = p->task_dup; 4391 // p_task_dup_t ptask_dup = (p_task_dup_t)task_dup; 4392 kmp_int64 st = p->st; 4393 kmp_uint64 ub_glob = p->ub_glob; 4394 kmp_uint64 num_tasks = p->num_tasks; 4395 kmp_uint64 grainsize = p->grainsize; 4396 kmp_uint64 extras = p->extras; 4397 kmp_int64 last_chunk = p->last_chunk; 4398 kmp_uint64 tc = p->tc; 4399 kmp_uint64 num_t_min = p->num_t_min; 4400 #if OMPT_SUPPORT 4401 void *codeptr_ra = p->codeptr_ra; 4402 #endif 4403 #if KMP_DEBUG 4404 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task); 4405 KMP_DEBUG_ASSERT(task != NULL); 4406 KA_TRACE(20, 4407 ("__kmp_taskloop_task: T#%d, task %p: %lld tasks, grainsize" 4408 " %lld, extras %lld, last_chunk %lld, i=%lld,%lld(%d), dup %p\n", 4409 gtid, taskdata, num_tasks, grainsize, extras, last_chunk, *lb, *ub, 4410 st, task_dup)); 4411 #endif 4412 KMP_DEBUG_ASSERT(num_tasks * 2 + 1 > num_t_min); 4413 if (num_tasks > num_t_min) 4414 __kmp_taskloop_recur(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks, 4415 grainsize, extras, last_chunk, tc, num_t_min, 4416 #if OMPT_SUPPORT 4417 codeptr_ra, 4418 #endif 4419 task_dup); 4420 else 4421 __kmp_taskloop_linear(NULL, gtid, task, lb, ub, st, ub_glob, num_tasks, 4422 grainsize, extras, last_chunk, tc, 4423 #if OMPT_SUPPORT 4424 codeptr_ra, 4425 #endif 4426 task_dup); 4427 4428 KA_TRACE(40, ("__kmp_taskloop_task(exit): T#%d\n", gtid)); 4429 return 0; 4430 } 4431 4432 // Schedule part of the taskloop as a task, 4433 // execute the rest of the taskloop. 4434 // 4435 // loc Source location information 4436 // gtid Global thread ID 4437 // task Pattern task, exposes the loop iteration range 4438 // lb Pointer to loop lower bound in task structure 4439 // ub Pointer to loop upper bound in task structure 4440 // st Loop stride 4441 // ub_glob Global upper bound (used for lastprivate check) 4442 // num_tasks Number of tasks to execute 4443 // grainsize Number of loop iterations per task 4444 // extras Number of chunks with grainsize+1 iterations 4445 // last_chunk Reduction of grainsize for last task 4446 // tc Iterations count 4447 // num_t_min Threshold to launch tasks recursively 4448 // task_dup Tasks duplication routine 4449 // codeptr_ra Return address for OMPT events 4450 void __kmp_taskloop_recur(ident_t *loc, int gtid, kmp_task_t *task, 4451 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, 4452 kmp_uint64 ub_glob, kmp_uint64 num_tasks, 4453 kmp_uint64 grainsize, kmp_uint64 extras, 4454 kmp_int64 last_chunk, kmp_uint64 tc, 4455 kmp_uint64 num_t_min, 4456 #if OMPT_SUPPORT 4457 void *codeptr_ra, 4458 #endif 4459 void *task_dup) { 4460 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task); 4461 KMP_DEBUG_ASSERT(task != NULL); 4462 KMP_DEBUG_ASSERT(num_tasks > num_t_min); 4463 KA_TRACE(20, 4464 ("__kmp_taskloop_recur: T#%d, task %p: %lld tasks, grainsize" 4465 " %lld, extras %lld, last_chunk %lld, i=%lld,%lld(%d), dup %p\n", 4466 gtid, taskdata, num_tasks, grainsize, extras, last_chunk, *lb, *ub, 4467 st, task_dup)); 4468 p_task_dup_t ptask_dup = (p_task_dup_t)task_dup; 4469 kmp_uint64 lower = *lb; 4470 kmp_info_t *thread = __kmp_threads[gtid]; 4471 // kmp_taskdata_t *current_task = thread->th.th_current_task; 4472 kmp_task_t *next_task; 4473 size_t lower_offset = 4474 (char *)lb - (char *)task; // remember offset of lb in the task structure 4475 size_t upper_offset = 4476 (char *)ub - (char *)task; // remember offset of ub in the task structure 4477 4478 KMP_DEBUG_ASSERT( 4479 tc == num_tasks * grainsize + (last_chunk < 0 ? last_chunk : extras)); 4480 KMP_DEBUG_ASSERT(num_tasks > extras); 4481 KMP_DEBUG_ASSERT(num_tasks > 0); 4482 4483 // split the loop in two halves 4484 kmp_uint64 lb1, ub0, tc0, tc1, ext0, ext1; 4485 kmp_int64 last_chunk0 = 0, last_chunk1 = 0; 4486 kmp_uint64 gr_size0 = grainsize; 4487 kmp_uint64 n_tsk0 = num_tasks >> 1; // num_tasks/2 to execute 4488 kmp_uint64 n_tsk1 = num_tasks - n_tsk0; // to schedule as a task 4489 if (last_chunk < 0) { 4490 ext0 = ext1 = 0; 4491 last_chunk1 = last_chunk; 4492 tc0 = grainsize * n_tsk0; 4493 tc1 = tc - tc0; 4494 } else if (n_tsk0 <= extras) { 4495 gr_size0++; // integrate extras into grainsize 4496 ext0 = 0; // no extra iters in 1st half 4497 ext1 = extras - n_tsk0; // remaining extras 4498 tc0 = gr_size0 * n_tsk0; 4499 tc1 = tc - tc0; 4500 } else { // n_tsk0 > extras 4501 ext1 = 0; // no extra iters in 2nd half 4502 ext0 = extras; 4503 tc1 = grainsize * n_tsk1; 4504 tc0 = tc - tc1; 4505 } 4506 ub0 = lower + st * (tc0 - 1); 4507 lb1 = ub0 + st; 4508 4509 // create pattern task for 2nd half of the loop 4510 next_task = __kmp_task_dup_alloc(thread, task); // duplicate the task 4511 // adjust lower bound (upper bound is not changed) for the 2nd half 4512 *(kmp_uint64 *)((char *)next_task + lower_offset) = lb1; 4513 if (ptask_dup != NULL) // construct firstprivates, etc. 4514 ptask_dup(next_task, task, 0); 4515 *ub = ub0; // adjust upper bound for the 1st half 4516 4517 // create auxiliary task for 2nd half of the loop 4518 // make sure new task has same parent task as the pattern task 4519 kmp_taskdata_t *current_task = thread->th.th_current_task; 4520 thread->th.th_current_task = taskdata->td_parent; 4521 kmp_task_t *new_task = 4522 __kmpc_omp_task_alloc(loc, gtid, 1, 3 * sizeof(void *), 4523 sizeof(__taskloop_params_t), &__kmp_taskloop_task); 4524 // restore current task 4525 thread->th.th_current_task = current_task; 4526 __taskloop_params_t *p = (__taskloop_params_t *)new_task->shareds; 4527 p->task = next_task; 4528 p->lb = (kmp_uint64 *)((char *)next_task + lower_offset); 4529 p->ub = (kmp_uint64 *)((char *)next_task + upper_offset); 4530 p->task_dup = task_dup; 4531 p->st = st; 4532 p->ub_glob = ub_glob; 4533 p->num_tasks = n_tsk1; 4534 p->grainsize = grainsize; 4535 p->extras = ext1; 4536 p->last_chunk = last_chunk1; 4537 p->tc = tc1; 4538 p->num_t_min = num_t_min; 4539 #if OMPT_SUPPORT 4540 p->codeptr_ra = codeptr_ra; 4541 #endif 4542 4543 #if OMPT_SUPPORT 4544 // schedule new task with correct return address for OMPT events 4545 __kmp_omp_taskloop_task(NULL, gtid, new_task, codeptr_ra); 4546 #else 4547 __kmp_omp_task(gtid, new_task, true); // schedule new task 4548 #endif 4549 4550 // execute the 1st half of current subrange 4551 if (n_tsk0 > num_t_min) 4552 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0, gr_size0, 4553 ext0, last_chunk0, tc0, num_t_min, 4554 #if OMPT_SUPPORT 4555 codeptr_ra, 4556 #endif 4557 task_dup); 4558 else 4559 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, n_tsk0, 4560 gr_size0, ext0, last_chunk0, tc0, 4561 #if OMPT_SUPPORT 4562 codeptr_ra, 4563 #endif 4564 task_dup); 4565 4566 KA_TRACE(40, ("__kmp_taskloop_recur(exit): T#%d\n", gtid)); 4567 } 4568 4569 static void __kmp_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int if_val, 4570 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, 4571 int nogroup, int sched, kmp_uint64 grainsize, 4572 int modifier, void *task_dup) { 4573 kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task); 4574 KMP_DEBUG_ASSERT(task != NULL); 4575 if (nogroup == 0) { 4576 #if OMPT_SUPPORT && OMPT_OPTIONAL 4577 OMPT_STORE_RETURN_ADDRESS(gtid); 4578 #endif 4579 __kmpc_taskgroup(loc, gtid); 4580 } 4581 4582 // ========================================================================= 4583 // calculate loop parameters 4584 kmp_taskloop_bounds_t task_bounds(task, lb, ub); 4585 kmp_uint64 tc; 4586 // compiler provides global bounds here 4587 kmp_uint64 lower = task_bounds.get_lb(); 4588 kmp_uint64 upper = task_bounds.get_ub(); 4589 kmp_uint64 ub_glob = upper; // global upper used to calc lastprivate flag 4590 kmp_uint64 num_tasks = 0, extras = 0; 4591 kmp_int64 last_chunk = 4592 0; // reduce grainsize of last task by last_chunk in strict mode 4593 kmp_uint64 num_tasks_min = __kmp_taskloop_min_tasks; 4594 kmp_info_t *thread = __kmp_threads[gtid]; 4595 kmp_taskdata_t *current_task = thread->th.th_current_task; 4596 4597 KA_TRACE(20, ("__kmp_taskloop: T#%d, task %p, lb %lld, ub %lld, st %lld, " 4598 "grain %llu(%d, %d), dup %p\n", 4599 gtid, taskdata, lower, upper, st, grainsize, sched, modifier, 4600 task_dup)); 4601 4602 // compute trip count 4603 if (st == 1) { // most common case 4604 tc = upper - lower + 1; 4605 } else if (st < 0) { 4606 tc = (lower - upper) / (-st) + 1; 4607 } else { // st > 0 4608 tc = (upper - lower) / st + 1; 4609 } 4610 if (tc == 0) { 4611 KA_TRACE(20, ("__kmp_taskloop(exit): T#%d zero-trip loop\n", gtid)); 4612 // free the pattern task and exit 4613 __kmp_task_start(gtid, task, current_task); 4614 // do not execute anything for zero-trip loop 4615 __kmp_task_finish<false>(gtid, task, current_task); 4616 return; 4617 } 4618 4619 #if OMPT_SUPPORT && OMPT_OPTIONAL 4620 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL); 4621 ompt_task_info_t *task_info = __ompt_get_task_info_object(0); 4622 if (ompt_enabled.ompt_callback_work) { 4623 ompt_callbacks.ompt_callback(ompt_callback_work)( 4624 ompt_work_taskloop, ompt_scope_begin, &(team_info->parallel_data), 4625 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0)); 4626 } 4627 #endif 4628 4629 if (num_tasks_min == 0) 4630 // TODO: can we choose better default heuristic? 4631 num_tasks_min = 4632 KMP_MIN(thread->th.th_team_nproc * 10, INITIAL_TASK_DEQUE_SIZE); 4633 4634 // compute num_tasks/grainsize based on the input provided 4635 switch (sched) { 4636 case 0: // no schedule clause specified, we can choose the default 4637 // let's try to schedule (team_size*10) tasks 4638 grainsize = thread->th.th_team_nproc * 10; 4639 KMP_FALLTHROUGH(); 4640 case 2: // num_tasks provided 4641 if (grainsize > tc) { 4642 num_tasks = tc; // too big num_tasks requested, adjust values 4643 grainsize = 1; 4644 extras = 0; 4645 } else { 4646 num_tasks = grainsize; 4647 grainsize = tc / num_tasks; 4648 extras = tc % num_tasks; 4649 } 4650 break; 4651 case 1: // grainsize provided 4652 if (grainsize > tc) { 4653 num_tasks = 1; 4654 grainsize = tc; // too big grainsize requested, adjust values 4655 extras = 0; 4656 } else { 4657 if (modifier) { 4658 num_tasks = (tc + grainsize - 1) / grainsize; 4659 last_chunk = tc - (num_tasks * grainsize); 4660 extras = 0; 4661 } else { 4662 num_tasks = tc / grainsize; 4663 // adjust grainsize for balanced distribution of iterations 4664 grainsize = tc / num_tasks; 4665 extras = tc % num_tasks; 4666 } 4667 } 4668 break; 4669 default: 4670 KMP_ASSERT2(0, "unknown scheduling of taskloop"); 4671 } 4672 4673 KMP_DEBUG_ASSERT( 4674 tc == num_tasks * grainsize + (last_chunk < 0 ? last_chunk : extras)); 4675 KMP_DEBUG_ASSERT(num_tasks > extras); 4676 KMP_DEBUG_ASSERT(num_tasks > 0); 4677 // ========================================================================= 4678 4679 // check if clause value first 4680 // Also require GOMP_taskloop to reduce to linear (taskdata->td_flags.native) 4681 if (if_val == 0) { // if(0) specified, mark task as serial 4682 taskdata->td_flags.task_serial = 1; 4683 taskdata->td_flags.tiedness = TASK_TIED; // AC: serial task cannot be untied 4684 // always start serial tasks linearly 4685 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks, 4686 grainsize, extras, last_chunk, tc, 4687 #if OMPT_SUPPORT 4688 OMPT_GET_RETURN_ADDRESS(0), 4689 #endif 4690 task_dup); 4691 // !taskdata->td_flags.native => currently force linear spawning of tasks 4692 // for GOMP_taskloop 4693 } else if (num_tasks > num_tasks_min && !taskdata->td_flags.native) { 4694 KA_TRACE(20, ("__kmp_taskloop: T#%d, go recursive: tc %llu, #tasks %llu" 4695 "(%lld), grain %llu, extras %llu, last_chunk %lld\n", 4696 gtid, tc, num_tasks, num_tasks_min, grainsize, extras, 4697 last_chunk)); 4698 __kmp_taskloop_recur(loc, gtid, task, lb, ub, st, ub_glob, num_tasks, 4699 grainsize, extras, last_chunk, tc, num_tasks_min, 4700 #if OMPT_SUPPORT 4701 OMPT_GET_RETURN_ADDRESS(0), 4702 #endif 4703 task_dup); 4704 } else { 4705 KA_TRACE(20, ("__kmp_taskloop: T#%d, go linear: tc %llu, #tasks %llu" 4706 "(%lld), grain %llu, extras %llu, last_chunk %lld\n", 4707 gtid, tc, num_tasks, num_tasks_min, grainsize, extras, 4708 last_chunk)); 4709 __kmp_taskloop_linear(loc, gtid, task, lb, ub, st, ub_glob, num_tasks, 4710 grainsize, extras, last_chunk, tc, 4711 #if OMPT_SUPPORT 4712 OMPT_GET_RETURN_ADDRESS(0), 4713 #endif 4714 task_dup); 4715 } 4716 4717 #if OMPT_SUPPORT && OMPT_OPTIONAL 4718 if (ompt_enabled.ompt_callback_work) { 4719 ompt_callbacks.ompt_callback(ompt_callback_work)( 4720 ompt_work_taskloop, ompt_scope_end, &(team_info->parallel_data), 4721 &(task_info->task_data), tc, OMPT_GET_RETURN_ADDRESS(0)); 4722 } 4723 #endif 4724 4725 if (nogroup == 0) { 4726 #if OMPT_SUPPORT && OMPT_OPTIONAL 4727 OMPT_STORE_RETURN_ADDRESS(gtid); 4728 #endif 4729 __kmpc_end_taskgroup(loc, gtid); 4730 } 4731 KA_TRACE(20, ("__kmp_taskloop(exit): T#%d\n", gtid)); 4732 } 4733 4734 /*! 4735 @ingroup TASKING 4736 @param loc Source location information 4737 @param gtid Global thread ID 4738 @param task Task structure 4739 @param if_val Value of the if clause 4740 @param lb Pointer to loop lower bound in task structure 4741 @param ub Pointer to loop upper bound in task structure 4742 @param st Loop stride 4743 @param nogroup Flag, 1 if nogroup clause specified, 0 otherwise 4744 @param sched Schedule specified 0/1/2 for none/grainsize/num_tasks 4745 @param grainsize Schedule value if specified 4746 @param task_dup Tasks duplication routine 4747 4748 Execute the taskloop construct. 4749 */ 4750 void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int if_val, 4751 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, 4752 int sched, kmp_uint64 grainsize, void *task_dup) { 4753 __kmp_assert_valid_gtid(gtid); 4754 KA_TRACE(20, ("__kmpc_taskloop(enter): T#%d\n", gtid)); 4755 __kmp_taskloop(loc, gtid, task, if_val, lb, ub, st, nogroup, sched, grainsize, 4756 0, task_dup); 4757 KA_TRACE(20, ("__kmpc_taskloop(exit): T#%d\n", gtid)); 4758 } 4759 4760 /*! 4761 @ingroup TASKING 4762 @param loc Source location information 4763 @param gtid Global thread ID 4764 @param task Task structure 4765 @param if_val Value of the if clause 4766 @param lb Pointer to loop lower bound in task structure 4767 @param ub Pointer to loop upper bound in task structure 4768 @param st Loop stride 4769 @param nogroup Flag, 1 if nogroup clause specified, 0 otherwise 4770 @param sched Schedule specified 0/1/2 for none/grainsize/num_tasks 4771 @param grainsize Schedule value if specified 4772 @param modifer Modifier 'strict' for sched, 1 if present, 0 otherwise 4773 @param task_dup Tasks duplication routine 4774 4775 Execute the taskloop construct. 4776 */ 4777 void __kmpc_taskloop_5(ident_t *loc, int gtid, kmp_task_t *task, int if_val, 4778 kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, 4779 int nogroup, int sched, kmp_uint64 grainsize, 4780 int modifier, void *task_dup) { 4781 __kmp_assert_valid_gtid(gtid); 4782 KA_TRACE(20, ("__kmpc_taskloop_5(enter): T#%d\n", gtid)); 4783 __kmp_taskloop(loc, gtid, task, if_val, lb, ub, st, nogroup, sched, grainsize, 4784 modifier, task_dup); 4785 KA_TRACE(20, ("__kmpc_taskloop_5(exit): T#%d\n", gtid)); 4786 } 4787