14 #include "kmp_affinity.h" 15 #include "kmp_atomic.h" 16 #include "kmp_environment.h" 17 #include "kmp_error.h" 21 #include "kmp_settings.h" 22 #include "kmp_stats.h" 24 #include "kmp_wait_release.h" 25 #include "kmp_wrapper_getpid.h" 26 #include "kmp_dispatch.h" 27 #if KMP_USE_HIER_SCHED 28 #include "kmp_dispatch_hier.h" 32 #include "ompt-specific.h" 35 #include "ompd-specific.h" 38 #if OMP_PROFILING_SUPPORT 39 #include "llvm/Support/TimeProfiler.h" 40 static char *ProfileTraceFile =
nullptr;
44 #define KMP_USE_PRCTL 0 59 #if defined(KMP_GOMP_COMPAT) 60 char const __kmp_version_alt_comp[] =
61 KMP_VERSION_PREFIX
"alternative compiler support: yes";
64 char const __kmp_version_omp_api[] =
65 KMP_VERSION_PREFIX
"API version: 5.0 (201611)";
68 char const __kmp_version_lock[] =
69 KMP_VERSION_PREFIX
"lock type: run time selectable";
72 #define KMP_MIN(x, y) ((x) < (y) ? (x) : (y)) 77 kmp_info_t __kmp_monitor;
82 void __kmp_cleanup(
void);
84 static void __kmp_initialize_info(kmp_info_t *, kmp_team_t *,
int tid,
86 static void __kmp_initialize_team(kmp_team_t *team,
int new_nproc,
87 kmp_internal_control_t *new_icvs,
89 #if KMP_AFFINITY_SUPPORTED 90 static void __kmp_partition_places(kmp_team_t *team,
91 int update_master_only = 0);
93 static void __kmp_do_serial_initialize(
void);
94 void __kmp_fork_barrier(
int gtid,
int tid);
95 void __kmp_join_barrier(
int gtid);
96 void __kmp_setup_icv_copy(kmp_team_t *team,
int new_nproc,
97 kmp_internal_control_t *new_icvs,
ident_t *loc);
99 #ifdef USE_LOAD_BALANCE 100 static int __kmp_load_balance_nproc(kmp_root_t *root,
int set_nproc);
103 static int __kmp_expand_threads(
int nNeed);
105 static int __kmp_unregister_root_other_thread(
int gtid);
107 static void __kmp_reap_thread(kmp_info_t *thread,
int is_root);
108 kmp_info_t *__kmp_thread_pool_insert_pt = NULL;
113 int __kmp_get_global_thread_id() {
115 kmp_info_t **other_threads;
123 (
"*** __kmp_get_global_thread_id: entering, nproc=%d all_nproc=%d\n",
124 __kmp_nth, __kmp_all_nth));
131 if (!TCR_4(__kmp_init_gtid))
134 #ifdef KMP_TDATA_GTID 135 if (TCR_4(__kmp_gtid_mode) >= 3) {
136 KA_TRACE(1000, (
"*** __kmp_get_global_thread_id: using TDATA\n"));
140 if (TCR_4(__kmp_gtid_mode) >= 2) {
141 KA_TRACE(1000, (
"*** __kmp_get_global_thread_id: using keyed TLS\n"));
142 return __kmp_gtid_get_specific();
144 KA_TRACE(1000, (
"*** __kmp_get_global_thread_id: using internal alg.\n"));
146 stack_addr = (
char *)&stack_data;
147 other_threads = __kmp_threads;
160 for (i = 0; i < __kmp_threads_capacity; i++) {
162 kmp_info_t *thr = (kmp_info_t *)TCR_SYNC_PTR(other_threads[i]);
166 stack_size = (size_t)TCR_PTR(thr->th.th_info.ds.ds_stacksize);
167 stack_base = (
char *)TCR_PTR(thr->th.th_info.ds.ds_stackbase);
171 if (stack_addr <= stack_base) {
172 size_t stack_diff = stack_base - stack_addr;
174 if (stack_diff <= stack_size) {
177 KMP_DEBUG_ASSERT(__kmp_gtid_get_specific() == i);
185 (
"*** __kmp_get_global_thread_id: internal alg. failed to find " 186 "thread, using TLS\n"));
187 i = __kmp_gtid_get_specific();
197 if (!TCR_4(other_threads[i]->th.th_info.ds.ds_stackgrow)) {
198 KMP_FATAL(StackOverflow, i);
201 stack_base = (
char *)other_threads[i]->th.th_info.ds.ds_stackbase;
202 if (stack_addr > stack_base) {
203 TCW_PTR(other_threads[i]->th.th_info.ds.ds_stackbase, stack_addr);
204 TCW_PTR(other_threads[i]->th.th_info.ds.ds_stacksize,
205 other_threads[i]->th.th_info.ds.ds_stacksize + stack_addr -
208 TCW_PTR(other_threads[i]->th.th_info.ds.ds_stacksize,
209 stack_base - stack_addr);
213 if (__kmp_storage_map) {
214 char *stack_end = (
char *)other_threads[i]->th.th_info.ds.ds_stackbase;
215 char *stack_beg = stack_end - other_threads[i]->th.th_info.ds.ds_stacksize;
216 __kmp_print_storage_map_gtid(i, stack_beg, stack_end,
217 other_threads[i]->th.th_info.ds.ds_stacksize,
218 "th_%d stack (refinement)", i);
223 int __kmp_get_global_thread_id_reg() {
226 if (!__kmp_init_serial) {
229 #ifdef KMP_TDATA_GTID 230 if (TCR_4(__kmp_gtid_mode) >= 3) {
231 KA_TRACE(1000, (
"*** __kmp_get_global_thread_id_reg: using TDATA\n"));
235 if (TCR_4(__kmp_gtid_mode) >= 2) {
236 KA_TRACE(1000, (
"*** __kmp_get_global_thread_id_reg: using keyed TLS\n"));
237 gtid = __kmp_gtid_get_specific();
240 (
"*** __kmp_get_global_thread_id_reg: using internal alg.\n"));
241 gtid = __kmp_get_global_thread_id();
245 if (gtid == KMP_GTID_DNE) {
247 (
"__kmp_get_global_thread_id_reg: Encountered new root thread. " 248 "Registering a new gtid.\n"));
249 __kmp_acquire_bootstrap_lock(&__kmp_initz_lock);
250 if (!__kmp_init_serial) {
251 __kmp_do_serial_initialize();
252 gtid = __kmp_gtid_get_specific();
254 gtid = __kmp_register_root(FALSE);
256 __kmp_release_bootstrap_lock(&__kmp_initz_lock);
260 KMP_DEBUG_ASSERT(gtid >= 0);
266 void __kmp_check_stack_overlap(kmp_info_t *th) {
268 char *stack_beg = NULL;
269 char *stack_end = NULL;
272 KA_TRACE(10, (
"__kmp_check_stack_overlap: called\n"));
273 if (__kmp_storage_map) {
274 stack_end = (
char *)th->th.th_info.ds.ds_stackbase;
275 stack_beg = stack_end - th->th.th_info.ds.ds_stacksize;
277 gtid = __kmp_gtid_from_thread(th);
279 if (gtid == KMP_GTID_MONITOR) {
280 __kmp_print_storage_map_gtid(
281 gtid, stack_beg, stack_end, th->th.th_info.ds.ds_stacksize,
282 "th_%s stack (%s)",
"mon",
283 (th->th.th_info.ds.ds_stackgrow) ?
"initial" :
"actual");
285 __kmp_print_storage_map_gtid(
286 gtid, stack_beg, stack_end, th->th.th_info.ds.ds_stacksize,
287 "th_%d stack (%s)", gtid,
288 (th->th.th_info.ds.ds_stackgrow) ?
"initial" :
"actual");
294 gtid = __kmp_gtid_from_thread(th);
295 if (__kmp_env_checks == TRUE && !KMP_UBER_GTID(gtid)) {
297 (
"__kmp_check_stack_overlap: performing extensive checking\n"));
298 if (stack_beg == NULL) {
299 stack_end = (
char *)th->th.th_info.ds.ds_stackbase;
300 stack_beg = stack_end - th->th.th_info.ds.ds_stacksize;
303 for (f = 0; f < __kmp_threads_capacity; f++) {
304 kmp_info_t *f_th = (kmp_info_t *)TCR_SYNC_PTR(__kmp_threads[f]);
306 if (f_th && f_th != th) {
307 char *other_stack_end =
308 (
char *)TCR_PTR(f_th->th.th_info.ds.ds_stackbase);
309 char *other_stack_beg =
310 other_stack_end - (size_t)TCR_PTR(f_th->th.th_info.ds.ds_stacksize);
311 if ((stack_beg > other_stack_beg && stack_beg < other_stack_end) ||
312 (stack_end > other_stack_beg && stack_end < other_stack_end)) {
315 if (__kmp_storage_map)
316 __kmp_print_storage_map_gtid(
317 -1, other_stack_beg, other_stack_end,
318 (
size_t)TCR_PTR(f_th->th.th_info.ds.ds_stacksize),
319 "th_%d stack (overlapped)", __kmp_gtid_from_thread(f_th));
321 __kmp_fatal(KMP_MSG(StackOverlap), KMP_HNT(ChangeStackLimit),
327 KA_TRACE(10, (
"__kmp_check_stack_overlap: returning\n"));
332 void __kmp_infinite_loop(
void) {
333 static int done = FALSE;
340 #define MAX_MESSAGE 512 342 void __kmp_print_storage_map_gtid(
int gtid,
void *p1,
void *p2,
size_t size,
343 char const *format, ...) {
344 char buffer[MAX_MESSAGE];
347 va_start(ap, format);
348 KMP_SNPRINTF(buffer,
sizeof(buffer),
"OMP storage map: %p %p%8lu %s\n", p1,
349 p2, (
unsigned long)size, format);
350 __kmp_acquire_bootstrap_lock(&__kmp_stdio_lock);
351 __kmp_vprintf(kmp_err, buffer, ap);
352 #if KMP_PRINT_DATA_PLACEMENT 355 if (p1 <= p2 && (
char *)p2 - (
char *)p1 == size) {
356 if (__kmp_storage_map_verbose) {
357 node = __kmp_get_host_node(p1);
359 __kmp_storage_map_verbose = FALSE;
363 int localProc = __kmp_get_cpu_from_gtid(gtid);
365 const int page_size = KMP_GET_PAGE_SIZE();
367 p1 = (
void *)((
size_t)p1 & ~((size_t)page_size - 1));
368 p2 = (
void *)(((
size_t)p2 - 1) & ~((
size_t)page_size - 1));
370 __kmp_printf_no_lock(
" GTID %d localNode %d\n", gtid,
373 __kmp_printf_no_lock(
" GTID %d\n", gtid);
382 (
char *)p1 += page_size;
383 }
while (p1 <= p2 && (node = __kmp_get_host_node(p1)) == lastNode);
384 __kmp_printf_no_lock(
" %p-%p memNode %d\n", last, (
char *)p1 - 1,
388 __kmp_printf_no_lock(
" %p-%p memNode %d\n", p1,
389 (
char *)p1 + (page_size - 1),
390 __kmp_get_host_node(p1));
392 __kmp_printf_no_lock(
" %p-%p memNode %d\n", p2,
393 (
char *)p2 + (page_size - 1),
394 __kmp_get_host_node(p2));
400 __kmp_printf_no_lock(
" %s\n", KMP_I18N_STR(StorageMapWarning));
403 __kmp_release_bootstrap_lock(&__kmp_stdio_lock);
406 void __kmp_warn(
char const *format, ...) {
407 char buffer[MAX_MESSAGE];
410 if (__kmp_generate_warnings == kmp_warnings_off) {
414 va_start(ap, format);
416 KMP_SNPRINTF(buffer,
sizeof(buffer),
"OMP warning: %s\n", format);
417 __kmp_acquire_bootstrap_lock(&__kmp_stdio_lock);
418 __kmp_vprintf(kmp_err, buffer, ap);
419 __kmp_release_bootstrap_lock(&__kmp_stdio_lock);
424 void __kmp_abort_process() {
426 __kmp_acquire_bootstrap_lock(&__kmp_exit_lock);
428 if (__kmp_debug_buf) {
429 __kmp_dump_debug_buffer();
432 if (KMP_OS_WINDOWS) {
435 __kmp_global.g.g_abort = SIGABRT;
449 __kmp_unregister_library();
453 __kmp_infinite_loop();
454 __kmp_release_bootstrap_lock(&__kmp_exit_lock);
458 void __kmp_abort_thread(
void) {
461 __kmp_infinite_loop();
467 static void __kmp_print_thread_storage_map(kmp_info_t *thr,
int gtid) {
468 __kmp_print_storage_map_gtid(gtid, thr, thr + 1,
sizeof(kmp_info_t),
"th_%d",
471 __kmp_print_storage_map_gtid(gtid, &thr->th.th_info, &thr->th.th_team,
472 sizeof(kmp_desc_t),
"th_%d.th_info", gtid);
474 __kmp_print_storage_map_gtid(gtid, &thr->th.th_local, &thr->th.th_pri_head,
475 sizeof(kmp_local_t),
"th_%d.th_local", gtid);
477 __kmp_print_storage_map_gtid(
478 gtid, &thr->th.th_bar[0], &thr->th.th_bar[bs_last_barrier],
479 sizeof(kmp_balign_t) * bs_last_barrier,
"th_%d.th_bar", gtid);
481 __kmp_print_storage_map_gtid(gtid, &thr->th.th_bar[bs_plain_barrier],
482 &thr->th.th_bar[bs_plain_barrier + 1],
483 sizeof(kmp_balign_t),
"th_%d.th_bar[plain]",
486 __kmp_print_storage_map_gtid(gtid, &thr->th.th_bar[bs_forkjoin_barrier],
487 &thr->th.th_bar[bs_forkjoin_barrier + 1],
488 sizeof(kmp_balign_t),
"th_%d.th_bar[forkjoin]",
491 #if KMP_FAST_REDUCTION_BARRIER 492 __kmp_print_storage_map_gtid(gtid, &thr->th.th_bar[bs_reduction_barrier],
493 &thr->th.th_bar[bs_reduction_barrier + 1],
494 sizeof(kmp_balign_t),
"th_%d.th_bar[reduction]",
496 #endif // KMP_FAST_REDUCTION_BARRIER 502 static void __kmp_print_team_storage_map(
const char *header, kmp_team_t *team,
503 int team_id,
int num_thr) {
504 int num_disp_buff = team->t.t_max_nproc > 1 ? __kmp_dispatch_num_buffers : 2;
505 __kmp_print_storage_map_gtid(-1, team, team + 1,
sizeof(kmp_team_t),
"%s_%d",
508 __kmp_print_storage_map_gtid(-1, &team->t.t_bar[0],
509 &team->t.t_bar[bs_last_barrier],
510 sizeof(kmp_balign_team_t) * bs_last_barrier,
511 "%s_%d.t_bar", header, team_id);
513 __kmp_print_storage_map_gtid(-1, &team->t.t_bar[bs_plain_barrier],
514 &team->t.t_bar[bs_plain_barrier + 1],
515 sizeof(kmp_balign_team_t),
"%s_%d.t_bar[plain]",
518 __kmp_print_storage_map_gtid(-1, &team->t.t_bar[bs_forkjoin_barrier],
519 &team->t.t_bar[bs_forkjoin_barrier + 1],
520 sizeof(kmp_balign_team_t),
521 "%s_%d.t_bar[forkjoin]", header, team_id);
523 #if KMP_FAST_REDUCTION_BARRIER 524 __kmp_print_storage_map_gtid(-1, &team->t.t_bar[bs_reduction_barrier],
525 &team->t.t_bar[bs_reduction_barrier + 1],
526 sizeof(kmp_balign_team_t),
527 "%s_%d.t_bar[reduction]", header, team_id);
528 #endif // KMP_FAST_REDUCTION_BARRIER 530 __kmp_print_storage_map_gtid(
531 -1, &team->t.t_dispatch[0], &team->t.t_dispatch[num_thr],
532 sizeof(kmp_disp_t) * num_thr,
"%s_%d.t_dispatch", header, team_id);
534 __kmp_print_storage_map_gtid(
535 -1, &team->t.t_threads[0], &team->t.t_threads[num_thr],
536 sizeof(kmp_info_t *) * num_thr,
"%s_%d.t_threads", header, team_id);
538 __kmp_print_storage_map_gtid(-1, &team->t.t_disp_buffer[0],
539 &team->t.t_disp_buffer[num_disp_buff],
540 sizeof(dispatch_shared_info_t) * num_disp_buff,
541 "%s_%d.t_disp_buffer", header, team_id);
544 static void __kmp_init_allocator() {
545 __kmp_init_memkind();
546 __kmp_init_target_mem();
548 static void __kmp_fini_allocator() { __kmp_fini_memkind(); }
555 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpReserved) {
560 case DLL_PROCESS_ATTACH:
561 KA_TRACE(10, (
"DllMain: PROCESS_ATTACH\n"));
565 case DLL_PROCESS_DETACH:
566 KA_TRACE(10, (
"DllMain: PROCESS_DETACH T#%d\n", __kmp_gtid_get_specific()));
579 if (lpReserved == NULL)
580 __kmp_internal_end_library(__kmp_gtid_get_specific());
584 case DLL_THREAD_ATTACH:
585 KA_TRACE(10, (
"DllMain: THREAD_ATTACH\n"));
591 case DLL_THREAD_DETACH:
592 KA_TRACE(10, (
"DllMain: THREAD_DETACH T#%d\n", __kmp_gtid_get_specific()));
594 __kmp_internal_end_thread(__kmp_gtid_get_specific());
605 void __kmp_parallel_deo(
int *gtid_ref,
int *cid_ref,
ident_t *loc_ref) {
606 int gtid = *gtid_ref;
607 #ifdef BUILD_PARALLEL_ORDERED 608 kmp_team_t *team = __kmp_team_from_gtid(gtid);
611 if (__kmp_env_consistency_check) {
612 if (__kmp_threads[gtid]->th.th_root->r.r_active)
613 #if KMP_USE_DYNAMIC_LOCK 614 __kmp_push_sync(gtid, ct_ordered_in_parallel, loc_ref, NULL, 0);
616 __kmp_push_sync(gtid, ct_ordered_in_parallel, loc_ref, NULL);
619 #ifdef BUILD_PARALLEL_ORDERED 620 if (!team->t.t_serialized) {
622 KMP_WAIT(&team->t.t_ordered.dt.t_value, __kmp_tid_from_gtid(gtid), KMP_EQ,
630 void __kmp_parallel_dxo(
int *gtid_ref,
int *cid_ref,
ident_t *loc_ref) {
631 int gtid = *gtid_ref;
632 #ifdef BUILD_PARALLEL_ORDERED 633 int tid = __kmp_tid_from_gtid(gtid);
634 kmp_team_t *team = __kmp_team_from_gtid(gtid);
637 if (__kmp_env_consistency_check) {
638 if (__kmp_threads[gtid]->th.th_root->r.r_active)
639 __kmp_pop_sync(gtid, ct_ordered_in_parallel, loc_ref);
641 #ifdef BUILD_PARALLEL_ORDERED 642 if (!team->t.t_serialized) {
647 team->t.t_ordered.dt.t_value = ((tid + 1) % team->t.t_nproc);
657 int __kmp_enter_single(
int gtid,
ident_t *id_ref,
int push_ws) {
662 if (!TCR_4(__kmp_init_parallel))
663 __kmp_parallel_initialize();
664 __kmp_resume_if_soft_paused();
666 th = __kmp_threads[gtid];
667 team = th->th.th_team;
670 th->th.th_ident = id_ref;
672 if (team->t.t_serialized) {
675 kmp_int32 old_this = th->th.th_local.this_construct;
677 ++th->th.th_local.this_construct;
681 if (team->t.t_construct == old_this) {
682 status = __kmp_atomic_compare_store_acq(&team->t.t_construct, old_this,
683 th->th.th_local.this_construct);
686 if (__itt_metadata_add_ptr && __kmp_forkjoin_frames_mode == 3 &&
687 KMP_MASTER_GTID(gtid) && th->th.th_teams_microtask == NULL &&
688 team->t.t_active_level == 1) {
690 __kmp_itt_metadata_single(id_ref);
695 if (__kmp_env_consistency_check) {
696 if (status && push_ws) {
697 __kmp_push_workshare(gtid, ct_psingle, id_ref);
699 __kmp_check_workshare(gtid, ct_psingle, id_ref);
704 __kmp_itt_single_start(gtid);
710 void __kmp_exit_single(
int gtid) {
712 __kmp_itt_single_end(gtid);
714 if (__kmp_env_consistency_check)
715 __kmp_pop_workshare(gtid, ct_psingle, NULL);
724 static int __kmp_reserve_threads(kmp_root_t *root, kmp_team_t *parent_team,
725 int master_tid,
int set_nthreads,
729 KMP_DEBUG_ASSERT(__kmp_init_serial);
730 KMP_DEBUG_ASSERT(root && parent_team);
731 kmp_info_t *this_thr = parent_team->t.t_threads[master_tid];
735 new_nthreads = set_nthreads;
736 if (!get__dynamic_2(parent_team, master_tid)) {
739 #ifdef USE_LOAD_BALANCE 740 else if (__kmp_global.g.g_dynamic_mode == dynamic_load_balance) {
741 new_nthreads = __kmp_load_balance_nproc(root, set_nthreads);
742 if (new_nthreads == 1) {
743 KC_TRACE(10, (
"__kmp_reserve_threads: T#%d load balance reduced " 744 "reservation to 1 thread\n",
748 if (new_nthreads < set_nthreads) {
749 KC_TRACE(10, (
"__kmp_reserve_threads: T#%d load balance reduced " 750 "reservation to %d threads\n",
751 master_tid, new_nthreads));
755 else if (__kmp_global.g.g_dynamic_mode == dynamic_thread_limit) {
756 new_nthreads = __kmp_avail_proc - __kmp_nth +
757 (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc);
758 if (new_nthreads <= 1) {
759 KC_TRACE(10, (
"__kmp_reserve_threads: T#%d thread limit reduced " 760 "reservation to 1 thread\n",
764 if (new_nthreads < set_nthreads) {
765 KC_TRACE(10, (
"__kmp_reserve_threads: T#%d thread limit reduced " 766 "reservation to %d threads\n",
767 master_tid, new_nthreads));
769 new_nthreads = set_nthreads;
771 }
else if (__kmp_global.g.g_dynamic_mode == dynamic_random) {
772 if (set_nthreads > 2) {
773 new_nthreads = __kmp_get_random(parent_team->t.t_threads[master_tid]);
774 new_nthreads = (new_nthreads % set_nthreads) + 1;
775 if (new_nthreads == 1) {
776 KC_TRACE(10, (
"__kmp_reserve_threads: T#%d dynamic random reduced " 777 "reservation to 1 thread\n",
781 if (new_nthreads < set_nthreads) {
782 KC_TRACE(10, (
"__kmp_reserve_threads: T#%d dynamic random reduced " 783 "reservation to %d threads\n",
784 master_tid, new_nthreads));
792 if (__kmp_nth + new_nthreads -
793 (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc) >
795 int tl_nthreads = __kmp_max_nth - __kmp_nth +
796 (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc);
797 if (tl_nthreads <= 0) {
802 if (!get__dynamic_2(parent_team, master_tid) && (!__kmp_reserve_warn)) {
803 __kmp_reserve_warn = 1;
804 __kmp_msg(kmp_ms_warning,
805 KMP_MSG(CantFormThrTeam, set_nthreads, tl_nthreads),
806 KMP_HNT(Unset_ALL_THREADS), __kmp_msg_null);
808 if (tl_nthreads == 1) {
809 KC_TRACE(10, (
"__kmp_reserve_threads: T#%d KMP_DEVICE_THREAD_LIMIT " 810 "reduced reservation to 1 thread\n",
814 KC_TRACE(10, (
"__kmp_reserve_threads: T#%d KMP_DEVICE_THREAD_LIMIT reduced " 815 "reservation to %d threads\n",
816 master_tid, tl_nthreads));
817 new_nthreads = tl_nthreads;
821 int cg_nthreads = this_thr->th.th_cg_roots->cg_nthreads;
822 int max_cg_threads = this_thr->th.th_cg_roots->cg_thread_limit;
823 if (cg_nthreads + new_nthreads -
824 (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc) >
826 int tl_nthreads = max_cg_threads - cg_nthreads +
827 (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc);
828 if (tl_nthreads <= 0) {
833 if (!get__dynamic_2(parent_team, master_tid) && (!__kmp_reserve_warn)) {
834 __kmp_reserve_warn = 1;
835 __kmp_msg(kmp_ms_warning,
836 KMP_MSG(CantFormThrTeam, set_nthreads, tl_nthreads),
837 KMP_HNT(Unset_ALL_THREADS), __kmp_msg_null);
839 if (tl_nthreads == 1) {
840 KC_TRACE(10, (
"__kmp_reserve_threads: T#%d OMP_THREAD_LIMIT " 841 "reduced reservation to 1 thread\n",
845 KC_TRACE(10, (
"__kmp_reserve_threads: T#%d OMP_THREAD_LIMIT reduced " 846 "reservation to %d threads\n",
847 master_tid, tl_nthreads));
848 new_nthreads = tl_nthreads;
854 capacity = __kmp_threads_capacity;
855 if (TCR_PTR(__kmp_threads[0]) == NULL) {
861 if (__kmp_enable_hidden_helper && !TCR_4(__kmp_init_hidden_helper_threads)) {
862 capacity -= __kmp_hidden_helper_threads_num;
864 if (__kmp_nth + new_nthreads -
865 (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc) >
868 int slotsRequired = __kmp_nth + new_nthreads -
869 (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc) -
871 int slotsAdded = __kmp_expand_threads(slotsRequired);
872 if (slotsAdded < slotsRequired) {
874 new_nthreads -= (slotsRequired - slotsAdded);
875 KMP_ASSERT(new_nthreads >= 1);
878 if (!get__dynamic_2(parent_team, master_tid) && (!__kmp_reserve_warn)) {
879 __kmp_reserve_warn = 1;
880 if (__kmp_tp_cached) {
881 __kmp_msg(kmp_ms_warning,
882 KMP_MSG(CantFormThrTeam, set_nthreads, new_nthreads),
883 KMP_HNT(Set_ALL_THREADPRIVATE, __kmp_tp_capacity),
884 KMP_HNT(PossibleSystemLimitOnThreads), __kmp_msg_null);
886 __kmp_msg(kmp_ms_warning,
887 KMP_MSG(CantFormThrTeam, set_nthreads, new_nthreads),
888 KMP_HNT(SystemLimitOnThreads), __kmp_msg_null);
895 if (new_nthreads == 1) {
897 (
"__kmp_reserve_threads: T#%d serializing team after reclaiming " 898 "dead roots and rechecking; requested %d threads\n",
899 __kmp_get_gtid(), set_nthreads));
901 KC_TRACE(10, (
"__kmp_reserve_threads: T#%d allocating %d threads; requested" 903 __kmp_get_gtid(), new_nthreads, set_nthreads));
912 static void __kmp_fork_team_threads(kmp_root_t *root, kmp_team_t *team,
913 kmp_info_t *master_th,
int master_gtid) {
917 KA_TRACE(10, (
"__kmp_fork_team_threads: new_nprocs = %d\n", team->t.t_nproc));
918 KMP_DEBUG_ASSERT(master_gtid == __kmp_get_gtid());
922 master_th->th.th_info.ds.ds_tid = 0;
923 master_th->th.th_team = team;
924 master_th->th.th_team_nproc = team->t.t_nproc;
925 master_th->th.th_team_master = master_th;
926 master_th->th.th_team_serialized = FALSE;
927 master_th->th.th_dispatch = &team->t.t_dispatch[0];
930 #if KMP_NESTED_HOT_TEAMS 932 kmp_hot_team_ptr_t *hot_teams = master_th->th.th_hot_teams;
935 int level = team->t.t_active_level - 1;
936 if (master_th->th.th_teams_microtask) {
937 if (master_th->th.th_teams_size.nteams > 1) {
941 if (team->t.t_pkfn != (microtask_t)__kmp_teams_master &&
942 master_th->th.th_teams_level == team->t.t_level) {
947 if (level < __kmp_hot_teams_max_level) {
948 if (hot_teams[level].hot_team) {
950 KMP_DEBUG_ASSERT(hot_teams[level].hot_team == team);
954 hot_teams[level].hot_team = team;
955 hot_teams[level].hot_team_nth = team->t.t_nproc;
962 use_hot_team = team == root->r.r_hot_team;
967 team->t.t_threads[0] = master_th;
968 __kmp_initialize_info(master_th, team, 0, master_gtid);
971 for (i = 1; i < team->t.t_nproc; i++) {
974 kmp_info_t *thr = __kmp_allocate_thread(root, team, i);
975 team->t.t_threads[i] = thr;
976 KMP_DEBUG_ASSERT(thr);
977 KMP_DEBUG_ASSERT(thr->th.th_team == team);
979 KA_TRACE(20, (
"__kmp_fork_team_threads: T#%d(%d:%d) init arrived " 980 "T#%d(%d:%d) join =%llu, plain=%llu\n",
981 __kmp_gtid_from_tid(0, team), team->t.t_id, 0,
982 __kmp_gtid_from_tid(i, team), team->t.t_id, i,
983 team->t.t_bar[bs_forkjoin_barrier].b_arrived,
984 team->t.t_bar[bs_plain_barrier].b_arrived));
985 thr->th.th_teams_microtask = master_th->th.th_teams_microtask;
986 thr->th.th_teams_level = master_th->th.th_teams_level;
987 thr->th.th_teams_size = master_th->th.th_teams_size;
990 kmp_balign_t *balign = team->t.t_threads[i]->th.th_bar;
991 for (b = 0; b < bs_last_barrier; ++b) {
992 balign[b].bb.b_arrived = team->t.t_bar[b].b_arrived;
993 KMP_DEBUG_ASSERT(balign[b].bb.wait_flag != KMP_BARRIER_PARENT_FLAG);
995 balign[b].bb.b_worker_arrived = team->t.t_bar[b].b_team_arrived;
1001 #if KMP_AFFINITY_SUPPORTED 1002 __kmp_partition_places(team);
1006 if (__kmp_display_affinity && team->t.t_display_affinity != 1) {
1007 for (i = 0; i < team->t.t_nproc; i++) {
1008 kmp_info_t *thr = team->t.t_threads[i];
1009 if (thr->th.th_prev_num_threads != team->t.t_nproc ||
1010 thr->th.th_prev_level != team->t.t_level) {
1011 team->t.t_display_affinity = 1;
1020 #if KMP_ARCH_X86 || KMP_ARCH_X86_64 1024 inline static void propagateFPControl(kmp_team_t *team) {
1025 if (__kmp_inherit_fp_control) {
1026 kmp_int16 x87_fpu_control_word;
1030 __kmp_store_x87_fpu_control_word(&x87_fpu_control_word);
1031 __kmp_store_mxcsr(&mxcsr);
1032 mxcsr &= KMP_X86_MXCSR_MASK;
1043 KMP_CHECK_UPDATE(team->t.t_x87_fpu_control_word, x87_fpu_control_word);
1044 KMP_CHECK_UPDATE(team->t.t_mxcsr, mxcsr);
1047 KMP_CHECK_UPDATE(team->t.t_fp_control_saved, TRUE);
1051 KMP_CHECK_UPDATE(team->t.t_fp_control_saved, FALSE);
1057 inline static void updateHWFPControl(kmp_team_t *team) {
1058 if (__kmp_inherit_fp_control && team->t.t_fp_control_saved) {
1061 kmp_int16 x87_fpu_control_word;
1063 __kmp_store_x87_fpu_control_word(&x87_fpu_control_word);
1064 __kmp_store_mxcsr(&mxcsr);
1065 mxcsr &= KMP_X86_MXCSR_MASK;
1067 if (team->t.t_x87_fpu_control_word != x87_fpu_control_word) {
1068 __kmp_clear_x87_fpu_status_word();
1069 __kmp_load_x87_fpu_control_word(&team->t.t_x87_fpu_control_word);
1072 if (team->t.t_mxcsr != mxcsr) {
1073 __kmp_load_mxcsr(&team->t.t_mxcsr);
1078 #define propagateFPControl(x) ((void)0) 1079 #define updateHWFPControl(x) ((void)0) 1082 static void __kmp_alloc_argv_entries(
int argc, kmp_team_t *team,
1087 void __kmp_serialized_parallel(
ident_t *loc, kmp_int32 global_tid) {
1088 kmp_info_t *this_thr;
1089 kmp_team_t *serial_team;
1091 KC_TRACE(10, (
"__kmpc_serialized_parallel: called by T#%d\n", global_tid));
1098 if (!TCR_4(__kmp_init_parallel))
1099 __kmp_parallel_initialize();
1100 __kmp_resume_if_soft_paused();
1102 this_thr = __kmp_threads[global_tid];
1103 serial_team = this_thr->th.th_serial_team;
1106 KMP_DEBUG_ASSERT(serial_team);
1109 if (__kmp_tasking_mode != tskm_immediate_exec) {
1111 this_thr->th.th_task_team ==
1112 this_thr->th.th_team->t.t_task_team[this_thr->th.th_task_state]);
1113 KMP_DEBUG_ASSERT(serial_team->t.t_task_team[this_thr->th.th_task_state] ==
1115 KA_TRACE(20, (
"__kmpc_serialized_parallel: T#%d pushing task_team %p / " 1116 "team %p, new task_team = NULL\n",
1117 global_tid, this_thr->th.th_task_team, this_thr->th.th_team));
1118 this_thr->th.th_task_team = NULL;
1121 kmp_proc_bind_t proc_bind = this_thr->th.th_set_proc_bind;
1122 if (this_thr->th.th_current_task->td_icvs.proc_bind == proc_bind_false) {
1123 proc_bind = proc_bind_false;
1124 }
else if (proc_bind == proc_bind_default) {
1127 proc_bind = this_thr->th.th_current_task->td_icvs.proc_bind;
1130 this_thr->th.th_set_proc_bind = proc_bind_default;
1133 ompt_data_t ompt_parallel_data = ompt_data_none;
1134 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(global_tid);
1135 if (ompt_enabled.enabled &&
1136 this_thr->th.ompt_thread_info.state != ompt_state_overhead) {
1138 ompt_task_info_t *parent_task_info;
1139 parent_task_info = OMPT_CUR_TASK_INFO(this_thr);
1141 parent_task_info->frame.enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
1142 if (ompt_enabled.ompt_callback_parallel_begin) {
1145 ompt_callbacks.ompt_callback(ompt_callback_parallel_begin)(
1146 &(parent_task_info->task_data), &(parent_task_info->frame),
1147 &ompt_parallel_data, team_size,
1148 ompt_parallel_invoker_program | ompt_parallel_team, codeptr);
1151 #endif // OMPT_SUPPORT 1153 if (this_thr->th.th_team != serial_team) {
1155 int level = this_thr->th.th_team->t.t_level;
1157 if (serial_team->t.t_serialized) {
1160 kmp_team_t *new_team;
1162 __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock);
1165 __kmp_allocate_team(this_thr->th.th_root, 1, 1,
1169 proc_bind, &this_thr->th.th_current_task->td_icvs,
1170 0 USE_NESTED_HOT_ARG(NULL));
1171 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
1172 KMP_ASSERT(new_team);
1175 new_team->t.t_threads[0] = this_thr;
1176 new_team->t.t_parent = this_thr->th.th_team;
1177 serial_team = new_team;
1178 this_thr->th.th_serial_team = serial_team;
1182 (
"__kmpc_serialized_parallel: T#%d allocated new serial team %p\n",
1183 global_tid, serial_team));
1191 (
"__kmpc_serialized_parallel: T#%d reusing cached serial team %p\n",
1192 global_tid, serial_team));
1196 KMP_DEBUG_ASSERT(serial_team->t.t_threads);
1197 KMP_DEBUG_ASSERT(serial_team->t.t_threads[0] == this_thr);
1198 KMP_DEBUG_ASSERT(this_thr->th.th_team != serial_team);
1199 serial_team->t.t_ident = loc;
1200 serial_team->t.t_serialized = 1;
1201 serial_team->t.t_nproc = 1;
1202 serial_team->t.t_parent = this_thr->th.th_team;
1203 serial_team->t.t_sched.sched = this_thr->th.th_team->t.t_sched.sched;
1204 this_thr->th.th_team = serial_team;
1205 serial_team->t.t_master_tid = this_thr->th.th_info.ds.ds_tid;
1207 KF_TRACE(10, (
"__kmpc_serialized_parallel: T#d curtask=%p\n", global_tid,
1208 this_thr->th.th_current_task));
1209 KMP_ASSERT(this_thr->th.th_current_task->td_flags.executing == 1);
1210 this_thr->th.th_current_task->td_flags.executing = 0;
1212 __kmp_push_current_task_to_thread(this_thr, serial_team, 0);
1217 copy_icvs(&this_thr->th.th_current_task->td_icvs,
1218 &this_thr->th.th_current_task->td_parent->td_icvs);
1222 if (__kmp_nested_nth.used && (level + 1 < __kmp_nested_nth.used)) {
1223 this_thr->th.th_current_task->td_icvs.nproc =
1224 __kmp_nested_nth.nth[level + 1];
1227 if (__kmp_nested_proc_bind.used &&
1228 (level + 1 < __kmp_nested_proc_bind.used)) {
1229 this_thr->th.th_current_task->td_icvs.proc_bind =
1230 __kmp_nested_proc_bind.bind_types[level + 1];
1234 serial_team->t.t_pkfn = (microtask_t)(~0);
1236 this_thr->th.th_info.ds.ds_tid = 0;
1239 this_thr->th.th_team_nproc = 1;
1240 this_thr->th.th_team_master = this_thr;
1241 this_thr->th.th_team_serialized = 1;
1243 serial_team->t.t_level = serial_team->t.t_parent->t.t_level + 1;
1244 serial_team->t.t_active_level = serial_team->t.t_parent->t.t_active_level;
1245 serial_team->t.t_def_allocator = this_thr->th.th_def_allocator;
1247 propagateFPControl(serial_team);
1250 KMP_DEBUG_ASSERT(serial_team->t.t_dispatch);
1251 if (!serial_team->t.t_dispatch->th_disp_buffer) {
1252 serial_team->t.t_dispatch->th_disp_buffer =
1253 (dispatch_private_info_t *)__kmp_allocate(
1254 sizeof(dispatch_private_info_t));
1256 this_thr->th.th_dispatch = serial_team->t.t_dispatch;
1263 KMP_DEBUG_ASSERT(this_thr->th.th_team == serial_team);
1264 KMP_DEBUG_ASSERT(serial_team->t.t_threads);
1265 KMP_DEBUG_ASSERT(serial_team->t.t_threads[0] == this_thr);
1266 ++serial_team->t.t_serialized;
1267 this_thr->th.th_team_serialized = serial_team->t.t_serialized;
1270 int level = this_thr->th.th_team->t.t_level;
1273 if (__kmp_nested_nth.used && (level + 1 < __kmp_nested_nth.used)) {
1274 this_thr->th.th_current_task->td_icvs.nproc =
1275 __kmp_nested_nth.nth[level + 1];
1277 serial_team->t.t_level++;
1278 KF_TRACE(10, (
"__kmpc_serialized_parallel: T#%d increasing nesting level " 1279 "of serial team %p to %d\n",
1280 global_tid, serial_team, serial_team->t.t_level));
1283 KMP_DEBUG_ASSERT(serial_team->t.t_dispatch);
1285 dispatch_private_info_t *disp_buffer =
1286 (dispatch_private_info_t *)__kmp_allocate(
1287 sizeof(dispatch_private_info_t));
1288 disp_buffer->next = serial_team->t.t_dispatch->th_disp_buffer;
1289 serial_team->t.t_dispatch->th_disp_buffer = disp_buffer;
1291 this_thr->th.th_dispatch = serial_team->t.t_dispatch;
1295 KMP_CHECK_UPDATE(serial_team->t.t_cancel_request, cancel_noreq);
1299 if (__kmp_display_affinity) {
1300 if (this_thr->th.th_prev_level != serial_team->t.t_level ||
1301 this_thr->th.th_prev_num_threads != 1) {
1303 __kmp_aux_display_affinity(global_tid, NULL);
1304 this_thr->th.th_prev_level = serial_team->t.t_level;
1305 this_thr->th.th_prev_num_threads = 1;
1309 if (__kmp_env_consistency_check)
1310 __kmp_push_parallel(global_tid, NULL);
1312 serial_team->t.ompt_team_info.master_return_address = codeptr;
1313 if (ompt_enabled.enabled &&
1314 this_thr->th.ompt_thread_info.state != ompt_state_overhead) {
1315 OMPT_CUR_TASK_INFO(this_thr)->frame.exit_frame.ptr =
1316 OMPT_GET_FRAME_ADDRESS(0);
1318 ompt_lw_taskteam_t lw_taskteam;
1319 __ompt_lw_taskteam_init(&lw_taskteam, this_thr, global_tid,
1320 &ompt_parallel_data, codeptr);
1322 __ompt_lw_taskteam_link(&lw_taskteam, this_thr, 1);
1326 if (ompt_enabled.ompt_callback_implicit_task) {
1327 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
1328 ompt_scope_begin, OMPT_CUR_TEAM_DATA(this_thr),
1329 OMPT_CUR_TASK_DATA(this_thr), 1, __kmp_tid_from_gtid(global_tid),
1330 ompt_task_implicit);
1331 OMPT_CUR_TASK_INFO(this_thr)->thread_num =
1332 __kmp_tid_from_gtid(global_tid);
1336 this_thr->th.ompt_thread_info.state = ompt_state_work_parallel;
1337 OMPT_CUR_TASK_INFO(this_thr)->frame.exit_frame.ptr =
1338 OMPT_GET_FRAME_ADDRESS(0);
1345 int __kmp_fork_call(
ident_t *loc,
int gtid,
1346 enum fork_context_e call_context,
1347 kmp_int32 argc, microtask_t microtask, launch_t invoker,
1352 int master_this_cons;
1354 kmp_team_t *parent_team;
1355 kmp_info_t *master_th;
1359 int master_set_numthreads;
1363 #if KMP_NESTED_HOT_TEAMS 1364 kmp_hot_team_ptr_t **p_hot_teams;
1367 KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(KMP_fork_call);
1370 KA_TRACE(20, (
"__kmp_fork_call: enter T#%d\n", gtid));
1371 if (__kmp_stkpadding > 0 && __kmp_root[gtid] != NULL) {
1374 void *dummy = KMP_ALLOCA(__kmp_stkpadding);
1376 if (__kmp_stkpadding > KMP_MAX_STKPADDING)
1377 __kmp_stkpadding += (short)((kmp_int64)dummy);
1383 if (!TCR_4(__kmp_init_parallel))
1384 __kmp_parallel_initialize();
1385 __kmp_resume_if_soft_paused();
1388 master_th = __kmp_threads[gtid];
1390 parent_team = master_th->th.th_team;
1391 master_tid = master_th->th.th_info.ds.ds_tid;
1392 master_this_cons = master_th->th.th_local.this_construct;
1393 root = master_th->th.th_root;
1394 master_active = root->r.r_active;
1395 master_set_numthreads = master_th->th.th_set_nproc;
1398 ompt_data_t ompt_parallel_data = ompt_data_none;
1399 ompt_data_t *parent_task_data;
1400 ompt_frame_t *ompt_frame;
1401 ompt_data_t *implicit_task_data;
1402 void *return_address = NULL;
1404 if (ompt_enabled.enabled) {
1405 __ompt_get_task_info_internal(0, NULL, &parent_task_data, &ompt_frame,
1407 return_address = OMPT_LOAD_RETURN_ADDRESS(gtid);
1412 __kmp_assign_root_init_mask();
1415 level = parent_team->t.t_level;
1417 active_level = parent_team->t.t_active_level;
1419 teams_level = master_th->th.th_teams_level;
1420 #if KMP_NESTED_HOT_TEAMS 1421 p_hot_teams = &master_th->th.th_hot_teams;
1422 if (*p_hot_teams == NULL && __kmp_hot_teams_max_level > 0) {
1423 *p_hot_teams = (kmp_hot_team_ptr_t *)__kmp_allocate(
1424 sizeof(kmp_hot_team_ptr_t) * __kmp_hot_teams_max_level);
1425 (*p_hot_teams)[0].hot_team = root->r.r_hot_team;
1427 (*p_hot_teams)[0].hot_team_nth = 1;
1432 if (ompt_enabled.enabled) {
1433 if (ompt_enabled.ompt_callback_parallel_begin) {
1434 int team_size = master_set_numthreads
1435 ? master_set_numthreads
1436 : get__nproc_2(parent_team, master_tid);
1437 int flags = OMPT_INVOKER(call_context) |
1438 ((microtask == (microtask_t)__kmp_teams_master)
1439 ? ompt_parallel_league
1440 : ompt_parallel_team);
1441 ompt_callbacks.ompt_callback(ompt_callback_parallel_begin)(
1442 parent_task_data, ompt_frame, &ompt_parallel_data, team_size, flags,
1445 master_th->th.ompt_thread_info.state = ompt_state_overhead;
1449 master_th->th.th_ident = loc;
1451 if (master_th->th.th_teams_microtask && ap &&
1452 microtask != (microtask_t)__kmp_teams_master && level == teams_level) {
1456 parent_team->t.t_ident = loc;
1457 __kmp_alloc_argv_entries(argc, parent_team, TRUE);
1458 parent_team->t.t_argc = argc;
1459 argv = (
void **)parent_team->t.t_argv;
1460 for (i = argc - 1; i >= 0; --i)
1461 *argv++ = va_arg(kmp_va_deref(ap),
void *);
1463 if (parent_team == master_th->th.th_serial_team) {
1466 KMP_DEBUG_ASSERT(parent_team->t.t_serialized > 1);
1468 if (call_context == fork_context_gnu) {
1471 parent_team->t.t_serialized--;
1476 parent_team->t.t_pkfn = microtask;
1481 void **exit_frame_p;
1483 ompt_lw_taskteam_t lw_taskteam;
1485 if (ompt_enabled.enabled) {
1486 __ompt_lw_taskteam_init(&lw_taskteam, master_th, gtid,
1487 &ompt_parallel_data, return_address);
1488 exit_frame_p = &(lw_taskteam.ompt_task_info.frame.exit_frame.ptr);
1490 __ompt_lw_taskteam_link(&lw_taskteam, master_th, 0);
1494 implicit_task_data = OMPT_CUR_TASK_DATA(master_th);
1495 if (ompt_enabled.ompt_callback_implicit_task) {
1496 OMPT_CUR_TASK_INFO(master_th)->thread_num =
1497 __kmp_tid_from_gtid(gtid);
1498 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
1499 ompt_scope_begin, OMPT_CUR_TEAM_DATA(master_th),
1500 implicit_task_data, 1,
1501 OMPT_CUR_TASK_INFO(master_th)->thread_num, ompt_task_implicit);
1505 master_th->th.ompt_thread_info.state = ompt_state_work_parallel;
1507 exit_frame_p = &dummy;
1512 parent_team->t.t_serialized--;
1515 KMP_TIME_PARTITIONED_BLOCK(OMP_parallel);
1516 KMP_SET_THREAD_STATE_BLOCK(IMPLICIT_TASK);
1517 __kmp_invoke_microtask(microtask, gtid, 0, argc, parent_team->t.t_argv
1526 if (ompt_enabled.enabled) {
1527 *exit_frame_p = NULL;
1528 OMPT_CUR_TASK_INFO(master_th)->frame.exit_frame = ompt_data_none;
1529 if (ompt_enabled.ompt_callback_implicit_task) {
1530 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
1531 ompt_scope_end, NULL, implicit_task_data, 1,
1532 OMPT_CUR_TASK_INFO(master_th)->thread_num, ompt_task_implicit);
1534 ompt_parallel_data = *OMPT_CUR_TEAM_DATA(master_th);
1535 __ompt_lw_taskteam_unlink(master_th);
1536 if (ompt_enabled.ompt_callback_parallel_end) {
1537 ompt_callbacks.ompt_callback(ompt_callback_parallel_end)(
1538 &ompt_parallel_data, OMPT_CUR_TASK_DATA(master_th),
1539 OMPT_INVOKER(call_context) | ompt_parallel_team,
1542 master_th->th.ompt_thread_info.state = ompt_state_overhead;
1548 parent_team->t.t_pkfn = microtask;
1549 parent_team->t.t_invoke = invoker;
1550 KMP_ATOMIC_INC(&root->r.r_in_parallel);
1551 parent_team->t.t_active_level++;
1552 parent_team->t.t_level++;
1553 parent_team->t.t_def_allocator = master_th->th.th_def_allocator;
1556 if (ompt_enabled.enabled) {
1557 ompt_lw_taskteam_t lw_taskteam;
1558 __ompt_lw_taskteam_init(&lw_taskteam, master_th, gtid,
1559 &ompt_parallel_data, return_address);
1560 __ompt_lw_taskteam_link(&lw_taskteam, master_th, 1,
true);
1565 if (master_set_numthreads) {
1566 if (master_set_numthreads < master_th->th.th_teams_size.nth) {
1568 kmp_info_t **other_threads = parent_team->t.t_threads;
1569 parent_team->t.t_nproc = master_set_numthreads;
1570 for (i = 0; i < master_set_numthreads; ++i) {
1571 other_threads[i]->th.th_team_nproc = master_set_numthreads;
1575 master_th->th.th_set_nproc = 0;
1579 if (__kmp_debugging) {
1580 int nth = __kmp_omp_num_threads(loc);
1582 master_set_numthreads = nth;
1587 #if USE_ITT_BUILD && USE_ITT_NOTIFY 1588 if (((__itt_frame_submit_v3_ptr && __itt_get_timestamp_ptr) ||
1590 __kmp_forkjoin_frames_mode == 3 &&
1591 parent_team->t.t_active_level == 1
1592 && master_th->th.th_teams_size.nteams == 1) {
1593 kmp_uint64 tmp_time = __itt_get_timestamp();
1594 master_th->th.th_frame_time = tmp_time;
1595 parent_team->t.t_region_time = tmp_time;
1597 if (__itt_stack_caller_create_ptr) {
1598 KMP_DEBUG_ASSERT(parent_team->t.t_stack_id == NULL);
1600 parent_team->t.t_stack_id = __kmp_itt_stack_caller_create();
1604 KF_TRACE(10, (
"__kmp_fork_call: before internal fork: root=%p, team=%p, " 1605 "master_th=%p, gtid=%d\n",
1606 root, parent_team, master_th, gtid));
1607 __kmp_internal_fork(loc, gtid, parent_team);
1608 KF_TRACE(10, (
"__kmp_fork_call: after internal fork: root=%p, team=%p, " 1609 "master_th=%p, gtid=%d\n",
1610 root, parent_team, master_th, gtid));
1612 if (call_context == fork_context_gnu)
1616 KA_TRACE(20, (
"__kmp_fork_call: T#%d(%d:0) invoke microtask = %p\n", gtid,
1617 parent_team->t.t_id, parent_team->t.t_pkfn));
1619 if (!parent_team->t.t_invoke(gtid)) {
1620 KMP_ASSERT2(0,
"cannot invoke microtask for PRIMARY thread");
1622 KA_TRACE(20, (
"__kmp_fork_call: T#%d(%d:0) done microtask = %p\n", gtid,
1623 parent_team->t.t_id, parent_team->t.t_pkfn));
1626 KA_TRACE(20, (
"__kmp_fork_call: parallel exit T#%d\n", gtid));
1632 if (__kmp_tasking_mode != tskm_immediate_exec) {
1633 KMP_DEBUG_ASSERT(master_th->th.th_task_team ==
1634 parent_team->t.t_task_team[master_th->th.th_task_state]);
1638 int enter_teams = 0;
1639 if (parent_team->t.t_active_level >=
1640 master_th->th.th_current_task->td_icvs.max_active_levels) {
1643 enter_teams = ((ap == NULL && active_level == 0) ||
1644 (ap && teams_level > 0 && teams_level == level));
1646 master_set_numthreads
1647 ? master_set_numthreads
1656 if ((get__max_active_levels(master_th) == 1 &&
1657 (root->r.r_in_parallel && !enter_teams)) ||
1658 (__kmp_library == library_serial)) {
1659 KC_TRACE(10, (
"__kmp_fork_call: T#%d serializing team; requested %d" 1667 __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock);
1672 nthreads = __kmp_reserve_threads(root, parent_team, master_tid,
1673 nthreads, enter_teams);
1674 if (nthreads == 1) {
1678 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
1682 KMP_DEBUG_ASSERT(nthreads > 0);
1685 master_th->th.th_set_nproc = 0;
1688 if (nthreads == 1) {
1690 #if KMP_OS_LINUX && \ 1691 (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64) 1694 void **args = (
void **)KMP_ALLOCA(argc *
sizeof(
void *));
1699 (
"__kmp_fork_call: T#%d serializing parallel region\n", gtid));
1704 master_th->th.th_serial_team->t.t_pkfn = microtask;
1707 if (call_context == fork_context_intel) {
1709 master_th->th.th_serial_team->t.t_ident = loc;
1712 master_th->th.th_serial_team->t.t_level--;
1717 void **exit_frame_p;
1718 ompt_task_info_t *task_info;
1720 ompt_lw_taskteam_t lw_taskteam;
1722 if (ompt_enabled.enabled) {
1723 __ompt_lw_taskteam_init(&lw_taskteam, master_th, gtid,
1724 &ompt_parallel_data, return_address);
1726 __ompt_lw_taskteam_link(&lw_taskteam, master_th, 0);
1729 task_info = OMPT_CUR_TASK_INFO(master_th);
1730 exit_frame_p = &(task_info->frame.exit_frame.ptr);
1731 if (ompt_enabled.ompt_callback_implicit_task) {
1732 OMPT_CUR_TASK_INFO(master_th)->thread_num =
1733 __kmp_tid_from_gtid(gtid);
1734 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
1735 ompt_scope_begin, OMPT_CUR_TEAM_DATA(master_th),
1736 &(task_info->task_data), 1,
1737 OMPT_CUR_TASK_INFO(master_th)->thread_num,
1738 ompt_task_implicit);
1742 master_th->th.ompt_thread_info.state = ompt_state_work_parallel;
1744 exit_frame_p = &dummy;
1749 KMP_TIME_PARTITIONED_BLOCK(OMP_parallel);
1750 KMP_SET_THREAD_STATE_BLOCK(IMPLICIT_TASK);
1751 __kmp_invoke_microtask(microtask, gtid, 0, argc,
1752 parent_team->t.t_argv
1761 if (ompt_enabled.enabled) {
1762 *exit_frame_p = NULL;
1763 if (ompt_enabled.ompt_callback_implicit_task) {
1764 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
1765 ompt_scope_end, NULL, &(task_info->task_data), 1,
1766 OMPT_CUR_TASK_INFO(master_th)->thread_num,
1767 ompt_task_implicit);
1769 ompt_parallel_data = *OMPT_CUR_TEAM_DATA(master_th);
1770 __ompt_lw_taskteam_unlink(master_th);
1771 if (ompt_enabled.ompt_callback_parallel_end) {
1772 ompt_callbacks.ompt_callback(ompt_callback_parallel_end)(
1773 &ompt_parallel_data, parent_task_data,
1774 OMPT_INVOKER(call_context) | ompt_parallel_team,
1777 master_th->th.ompt_thread_info.state = ompt_state_overhead;
1780 }
else if (microtask == (microtask_t)__kmp_teams_master) {
1781 KMP_DEBUG_ASSERT(master_th->th.th_team ==
1782 master_th->th.th_serial_team);
1783 team = master_th->th.th_team;
1785 team->t.t_invoke = invoker;
1786 __kmp_alloc_argv_entries(argc, team, TRUE);
1787 team->t.t_argc = argc;
1788 argv = (
void **)team->t.t_argv;
1790 for (i = argc - 1; i >= 0; --i)
1791 *argv++ = va_arg(kmp_va_deref(ap),
void *);
1793 for (i = 0; i < argc; ++i)
1795 argv[i] = parent_team->t.t_argv[i];
1803 if (ompt_enabled.enabled) {
1804 ompt_task_info_t *task_info = OMPT_CUR_TASK_INFO(master_th);
1805 if (ompt_enabled.ompt_callback_implicit_task) {
1806 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
1807 ompt_scope_end, NULL, &(task_info->task_data), 0,
1808 OMPT_CUR_TASK_INFO(master_th)->thread_num, ompt_task_initial);
1810 if (ompt_enabled.ompt_callback_parallel_end) {
1811 ompt_callbacks.ompt_callback(ompt_callback_parallel_end)(
1812 &ompt_parallel_data, parent_task_data,
1813 OMPT_INVOKER(call_context) | ompt_parallel_league,
1816 master_th->th.ompt_thread_info.state = ompt_state_overhead;
1821 for (i = argc - 1; i >= 0; --i)
1822 *argv++ = va_arg(kmp_va_deref(ap),
void *);
1827 void **exit_frame_p;
1828 ompt_task_info_t *task_info;
1830 ompt_lw_taskteam_t lw_taskteam;
1832 if (ompt_enabled.enabled) {
1833 __ompt_lw_taskteam_init(&lw_taskteam, master_th, gtid,
1834 &ompt_parallel_data, return_address);
1835 __ompt_lw_taskteam_link(&lw_taskteam, master_th, 0);
1837 task_info = OMPT_CUR_TASK_INFO(master_th);
1838 exit_frame_p = &(task_info->frame.exit_frame.ptr);
1841 implicit_task_data = OMPT_CUR_TASK_DATA(master_th);
1842 if (ompt_enabled.ompt_callback_implicit_task) {
1843 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
1844 ompt_scope_begin, OMPT_CUR_TEAM_DATA(master_th),
1845 implicit_task_data, 1, __kmp_tid_from_gtid(gtid),
1846 ompt_task_implicit);
1847 OMPT_CUR_TASK_INFO(master_th)->thread_num =
1848 __kmp_tid_from_gtid(gtid);
1852 master_th->th.ompt_thread_info.state = ompt_state_work_parallel;
1854 exit_frame_p = &dummy;
1859 KMP_TIME_PARTITIONED_BLOCK(OMP_parallel);
1860 KMP_SET_THREAD_STATE_BLOCK(IMPLICIT_TASK);
1861 __kmp_invoke_microtask(microtask, gtid, 0, argc, args
1870 if (ompt_enabled.enabled) {
1871 *exit_frame_p = NULL;
1872 if (ompt_enabled.ompt_callback_implicit_task) {
1873 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
1874 ompt_scope_end, NULL, &(task_info->task_data), 1,
1875 OMPT_CUR_TASK_INFO(master_th)->thread_num,
1876 ompt_task_implicit);
1879 ompt_parallel_data = *OMPT_CUR_TEAM_DATA(master_th);
1880 __ompt_lw_taskteam_unlink(master_th);
1881 if (ompt_enabled.ompt_callback_parallel_end) {
1882 ompt_callbacks.ompt_callback(ompt_callback_parallel_end)(
1883 &ompt_parallel_data, parent_task_data,
1884 OMPT_INVOKER(call_context) | ompt_parallel_team,
1887 master_th->th.ompt_thread_info.state = ompt_state_overhead;
1891 }
else if (call_context == fork_context_gnu) {
1893 ompt_lw_taskteam_t lwt;
1894 __ompt_lw_taskteam_init(&lwt, master_th, gtid, &ompt_parallel_data,
1897 lwt.ompt_task_info.frame.exit_frame = ompt_data_none;
1898 __ompt_lw_taskteam_link(&lwt, master_th, 1);
1903 KA_TRACE(20, (
"__kmp_fork_call: T#%d serial exit\n", gtid));
1906 KMP_ASSERT2(call_context < fork_context_last,
1907 "__kmp_fork_call: unknown fork_context parameter");
1910 KA_TRACE(20, (
"__kmp_fork_call: T#%d serial exit\n", gtid));
1917 KF_TRACE(10, (
"__kmp_fork_call: parent_team_aclevel=%d, master_th=%p, " 1918 "curtask=%p, curtask_max_aclevel=%d\n",
1919 parent_team->t.t_active_level, master_th,
1920 master_th->th.th_current_task,
1921 master_th->th.th_current_task->td_icvs.max_active_levels));
1925 master_th->th.th_current_task->td_flags.executing = 0;
1927 if (!master_th->th.th_teams_microtask || level > teams_level) {
1929 KMP_ATOMIC_INC(&root->r.r_in_parallel);
1933 int nthreads_icv = master_th->th.th_current_task->td_icvs.nproc;
1934 if ((level + 1 < __kmp_nested_nth.used) &&
1935 (__kmp_nested_nth.nth[level + 1] != nthreads_icv)) {
1936 nthreads_icv = __kmp_nested_nth.nth[level + 1];
1942 kmp_proc_bind_t proc_bind = master_th->th.th_set_proc_bind;
1943 kmp_proc_bind_t proc_bind_icv =
1945 if (master_th->th.th_current_task->td_icvs.proc_bind == proc_bind_false) {
1946 proc_bind = proc_bind_false;
1948 if (proc_bind == proc_bind_default) {
1951 proc_bind = master_th->th.th_current_task->td_icvs.proc_bind;
1957 if ((level + 1 < __kmp_nested_proc_bind.used) &&
1958 (__kmp_nested_proc_bind.bind_types[level + 1] !=
1959 master_th->th.th_current_task->td_icvs.proc_bind)) {
1960 proc_bind_icv = __kmp_nested_proc_bind.bind_types[level + 1];
1965 master_th->th.th_set_proc_bind = proc_bind_default;
1967 if ((nthreads_icv > 0) || (proc_bind_icv != proc_bind_default)) {
1968 kmp_internal_control_t new_icvs;
1969 copy_icvs(&new_icvs, &master_th->th.th_current_task->td_icvs);
1970 new_icvs.next = NULL;
1971 if (nthreads_icv > 0) {
1972 new_icvs.nproc = nthreads_icv;
1974 if (proc_bind_icv != proc_bind_default) {
1975 new_icvs.proc_bind = proc_bind_icv;
1979 KF_TRACE(10, (
"__kmp_fork_call: before __kmp_allocate_team\n"));
1980 team = __kmp_allocate_team(root, nthreads, nthreads,
1984 proc_bind, &new_icvs,
1985 argc USE_NESTED_HOT_ARG(master_th));
1988 KF_TRACE(10, (
"__kmp_fork_call: before __kmp_allocate_team\n"));
1989 team = __kmp_allocate_team(root, nthreads, nthreads,
1994 &master_th->th.th_current_task->td_icvs,
1995 argc USE_NESTED_HOT_ARG(master_th));
1998 10, (
"__kmp_fork_call: after __kmp_allocate_team - team = %p\n", team));
2001 KMP_CHECK_UPDATE(team->t.t_master_tid, master_tid);
2002 KMP_CHECK_UPDATE(team->t.t_master_this_cons, master_this_cons);
2003 KMP_CHECK_UPDATE(team->t.t_ident, loc);
2004 KMP_CHECK_UPDATE(team->t.t_parent, parent_team);
2005 KMP_CHECK_UPDATE_SYNC(team->t.t_pkfn, microtask);
2007 KMP_CHECK_UPDATE_SYNC(team->t.ompt_team_info.master_return_address,
2010 KMP_CHECK_UPDATE(team->t.t_invoke, invoker);
2012 if (!master_th->th.th_teams_microtask || level > teams_level) {
2013 int new_level = parent_team->t.t_level + 1;
2014 KMP_CHECK_UPDATE(team->t.t_level, new_level);
2015 new_level = parent_team->t.t_active_level + 1;
2016 KMP_CHECK_UPDATE(team->t.t_active_level, new_level);
2019 int new_level = parent_team->t.t_level;
2020 KMP_CHECK_UPDATE(team->t.t_level, new_level);
2021 new_level = parent_team->t.t_active_level;
2022 KMP_CHECK_UPDATE(team->t.t_active_level, new_level);
2024 kmp_r_sched_t new_sched = get__sched_2(parent_team, master_tid);
2026 KMP_CHECK_UPDATE(team->t.t_sched.sched, new_sched.sched);
2028 KMP_CHECK_UPDATE(team->t.t_cancel_request, cancel_noreq);
2029 KMP_CHECK_UPDATE(team->t.t_def_allocator, master_th->th.th_def_allocator);
2032 propagateFPControl(team);
2034 if (ompd_state & OMPD_ENABLE_BP)
2035 ompd_bp_parallel_begin();
2038 if (__kmp_tasking_mode != tskm_immediate_exec) {
2041 KMP_DEBUG_ASSERT(master_th->th.th_task_team ==
2042 parent_team->t.t_task_team[master_th->th.th_task_state]);
2043 KA_TRACE(20, (
"__kmp_fork_call: Primary T#%d pushing task_team %p / team " 2044 "%p, new task_team %p / team %p\n",
2045 __kmp_gtid_from_thread(master_th),
2046 master_th->th.th_task_team, parent_team,
2047 team->t.t_task_team[master_th->th.th_task_state], team));
2049 if (active_level || master_th->th.th_task_team) {
2051 KMP_DEBUG_ASSERT(master_th->th.th_task_state_memo_stack);
2052 if (master_th->th.th_task_state_top >=
2053 master_th->th.th_task_state_stack_sz) {
2054 kmp_uint32 new_size = 2 * master_th->th.th_task_state_stack_sz;
2055 kmp_uint8 *old_stack, *new_stack;
2057 new_stack = (kmp_uint8 *)__kmp_allocate(new_size);
2058 for (i = 0; i < master_th->th.th_task_state_stack_sz; ++i) {
2059 new_stack[i] = master_th->th.th_task_state_memo_stack[i];
2061 for (i = master_th->th.th_task_state_stack_sz; i < new_size;
2065 old_stack = master_th->th.th_task_state_memo_stack;
2066 master_th->th.th_task_state_memo_stack = new_stack;
2067 master_th->th.th_task_state_stack_sz = new_size;
2068 __kmp_free(old_stack);
2072 .th_task_state_memo_stack[master_th->th.th_task_state_top] =
2073 master_th->th.th_task_state;
2074 master_th->th.th_task_state_top++;
2075 #if KMP_NESTED_HOT_TEAMS 2076 if (master_th->th.th_hot_teams &&
2077 active_level < __kmp_hot_teams_max_level &&
2078 team == master_th->th.th_hot_teams[active_level].hot_team) {
2080 master_th->th.th_task_state =
2082 .th_task_state_memo_stack[master_th->th.th_task_state_top];
2085 master_th->th.th_task_state = 0;
2086 #if KMP_NESTED_HOT_TEAMS 2090 #if !KMP_NESTED_HOT_TEAMS 2091 KMP_DEBUG_ASSERT((master_th->th.th_task_team == NULL) ||
2092 (team == root->r.r_hot_team));
2098 (
"__kmp_fork_call: T#%d(%d:%d)->(%d:0) created a team of %d threads\n",
2099 gtid, parent_team->t.t_id, team->t.t_master_tid, team->t.t_id,