blob: d876ba1c133092c72789b1758b79f38dc66b5d2c [file] [log] [blame]
Jim Cownie5e8470a2013-09-27 10:38:44 +00001/*
Jonathan Peytonde4749b2016-12-14 23:01:24 +00002 * kmp_runtime.cpp -- KPTS runtime support library
Jim Cownie5e8470a2013-09-27 10:38:44 +00003 */
4
Jim Cownie5e8470a2013-09-27 10:38:44 +00005//===----------------------------------------------------------------------===//
6//
7// The LLVM Compiler Infrastructure
8//
9// This file is dual licensed under the MIT and the University of Illinois Open
10// Source Licenses. See LICENSE.txt for details.
11//
12//===----------------------------------------------------------------------===//
13
Jim Cownie5e8470a2013-09-27 10:38:44 +000014#include "kmp.h"
Jonathan Peyton30419822017-05-12 18:01:32 +000015#include "kmp_affinity.h"
Jim Cownie5e8470a2013-09-27 10:38:44 +000016#include "kmp_atomic.h"
Jim Cownie5e8470a2013-09-27 10:38:44 +000017#include "kmp_environment.h"
Jonathan Peyton30419822017-05-12 18:01:32 +000018#include "kmp_error.h"
Jim Cownie5e8470a2013-09-27 10:38:44 +000019#include "kmp_i18n.h"
20#include "kmp_io.h"
Jonathan Peyton30419822017-05-12 18:01:32 +000021#include "kmp_itt.h"
22#include "kmp_settings.h"
Jim Cownie4cc4bb42014-10-07 16:25:50 +000023#include "kmp_stats.h"
Jonathan Peyton30419822017-05-12 18:01:32 +000024#include "kmp_str.h"
Jim Cownie4cc4bb42014-10-07 16:25:50 +000025#include "kmp_wait_release.h"
Jonathan Peyton30419822017-05-12 18:01:32 +000026#include "kmp_wrapper_getpid.h"
Jonathan Peytonf6399362018-07-09 17:51:13 +000027#include "kmp_dispatch.h"
28#if KMP_USE_HIER_SCHED
29#include "kmp_dispatch_hier.h"
30#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +000031
Andrey Churbanovd7d088f2015-04-29 16:42:24 +000032#if OMPT_SUPPORT
33#include "ompt-specific.h"
34#endif
35
Jim Cownie5e8470a2013-09-27 10:38:44 +000036/* these are temporary issues to be dealt with */
37#define KMP_USE_PRCTL 0
Jim Cownie5e8470a2013-09-27 10:38:44 +000038
Jim Cownie5e8470a2013-09-27 10:38:44 +000039#if KMP_OS_WINDOWS
40#include <process.h>
41#endif
42
Jonas Hahnfeld50fed042016-11-07 15:58:36 +000043#include "tsan_annotations.h"
Jim Cownie5e8470a2013-09-27 10:38:44 +000044
45#if defined(KMP_GOMP_COMPAT)
Jonathan Peyton30419822017-05-12 18:01:32 +000046char const __kmp_version_alt_comp[] =
47 KMP_VERSION_PREFIX "alternative compiler support: yes";
Jim Cownie5e8470a2013-09-27 10:38:44 +000048#endif /* defined(KMP_GOMP_COMPAT) */
49
50char const __kmp_version_omp_api[] = KMP_VERSION_PREFIX "API version: "
Jonathan Peytone844a542017-03-06 22:07:40 +000051#if OMP_50_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +000052 "5.0 (201611)";
Jonathan Peytone844a542017-03-06 22:07:40 +000053#elif OMP_45_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +000054 "4.5 (201511)";
Jonathan Peyton74f3ffc2016-09-30 15:50:14 +000055#elif OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +000056 "4.0 (201307)";
Jim Cownie5e8470a2013-09-27 10:38:44 +000057#else
Jonathan Peyton30419822017-05-12 18:01:32 +000058 "3.1 (201107)";
Jim Cownie5e8470a2013-09-27 10:38:44 +000059#endif
60
61#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +000062char const __kmp_version_lock[] =
63 KMP_VERSION_PREFIX "lock type: run time selectable";
Jim Cownie5e8470a2013-09-27 10:38:44 +000064#endif /* KMP_DEBUG */
65
Jonathan Peyton30419822017-05-12 18:01:32 +000066#define KMP_MIN(x, y) ((x) < (y) ? (x) : (y))
Jim Cownie181b4bb2013-12-23 17:28:57 +000067
Jim Cownie5e8470a2013-09-27 10:38:44 +000068/* ------------------------------------------------------------------------ */
Jim Cownie5e8470a2013-09-27 10:38:44 +000069
Jonathan Peyton37e2ef52018-07-09 17:36:22 +000070#if KMP_USE_MONITOR
Jim Cownie5e8470a2013-09-27 10:38:44 +000071kmp_info_t __kmp_monitor;
Jonathan Peyton37e2ef52018-07-09 17:36:22 +000072#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +000073
Jim Cownie5e8470a2013-09-27 10:38:44 +000074/* Forward declarations */
75
Jonathan Peyton30419822017-05-12 18:01:32 +000076void __kmp_cleanup(void);
Jim Cownie5e8470a2013-09-27 10:38:44 +000077
Jonathan Peyton30419822017-05-12 18:01:32 +000078static void __kmp_initialize_info(kmp_info_t *, kmp_team_t *, int tid,
79 int gtid);
80static void __kmp_initialize_team(kmp_team_t *team, int new_nproc,
81 kmp_internal_control_t *new_icvs,
82 ident_t *loc);
Jonathan Peyton2321d572015-06-08 19:25:25 +000083#if OMP_40_ENABLED && KMP_AFFINITY_SUPPORTED
Jonathan Peyton30419822017-05-12 18:01:32 +000084static void __kmp_partition_places(kmp_team_t *team,
85 int update_master_only = 0);
Jonathan Peyton2321d572015-06-08 19:25:25 +000086#endif
Jonathan Peyton30419822017-05-12 18:01:32 +000087static void __kmp_do_serial_initialize(void);
88void __kmp_fork_barrier(int gtid, int tid);
89void __kmp_join_barrier(int gtid);
90void __kmp_setup_icv_copy(kmp_team_t *team, int new_nproc,
91 kmp_internal_control_t *new_icvs, ident_t *loc);
Jim Cownie5e8470a2013-09-27 10:38:44 +000092
Jim Cownie5e8470a2013-09-27 10:38:44 +000093#ifdef USE_LOAD_BALANCE
Jonathan Peyton30419822017-05-12 18:01:32 +000094static int __kmp_load_balance_nproc(kmp_root_t *root, int set_nproc);
Jim Cownie5e8470a2013-09-27 10:38:44 +000095#endif
96
Jonathan Peyton1800ece2018-01-10 18:27:01 +000097static int __kmp_expand_threads(int nNeed);
Jonathan Peyton2321d572015-06-08 19:25:25 +000098#if KMP_OS_WINDOWS
Jonathan Peyton30419822017-05-12 18:01:32 +000099static int __kmp_unregister_root_other_thread(int gtid);
Jonathan Peyton2321d572015-06-08 19:25:25 +0000100#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000101static void __kmp_unregister_library(void); // called by __kmp_internal_end()
102static void __kmp_reap_thread(kmp_info_t *thread, int is_root);
Jonathan Peytoneaa9e402018-01-10 18:21:48 +0000103kmp_info_t *__kmp_thread_pool_insert_pt = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000104
Jim Cownie5e8470a2013-09-27 10:38:44 +0000105/* Calculate the identifier of the current thread */
Jonathan Peyton30419822017-05-12 18:01:32 +0000106/* fast (and somewhat portable) way to get unique identifier of executing
107 thread. Returns KMP_GTID_DNE if we haven't been assigned a gtid. */
Jonathan Peyton30419822017-05-12 18:01:32 +0000108int __kmp_get_global_thread_id() {
109 int i;
110 kmp_info_t **other_threads;
111 size_t stack_data;
112 char *stack_addr;
113 size_t stack_size;
114 char *stack_base;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000115
Jonathan Peyton30419822017-05-12 18:01:32 +0000116 KA_TRACE(
117 1000,
118 ("*** __kmp_get_global_thread_id: entering, nproc=%d all_nproc=%d\n",
119 __kmp_nth, __kmp_all_nth));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000120
Jonathan Peyton30419822017-05-12 18:01:32 +0000121 /* JPH - to handle the case where __kmpc_end(0) is called immediately prior to
122 a parallel region, made it return KMP_GTID_DNE to force serial_initialize
123 by caller. Had to handle KMP_GTID_DNE at all call-sites, or else guarantee
124 __kmp_init_gtid for this to work. */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000125
Jonathan Peyton30419822017-05-12 18:01:32 +0000126 if (!TCR_4(__kmp_init_gtid))
127 return KMP_GTID_DNE;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000128
129#ifdef KMP_TDATA_GTID
Jonathan Peyton30419822017-05-12 18:01:32 +0000130 if (TCR_4(__kmp_gtid_mode) >= 3) {
131 KA_TRACE(1000, ("*** __kmp_get_global_thread_id: using TDATA\n"));
132 return __kmp_gtid;
133 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000134#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000135 if (TCR_4(__kmp_gtid_mode) >= 2) {
136 KA_TRACE(1000, ("*** __kmp_get_global_thread_id: using keyed TLS\n"));
137 return __kmp_gtid_get_specific();
138 }
139 KA_TRACE(1000, ("*** __kmp_get_global_thread_id: using internal alg.\n"));
140
141 stack_addr = (char *)&stack_data;
142 other_threads = __kmp_threads;
143
144 /* ATT: The code below is a source of potential bugs due to unsynchronized
145 access to __kmp_threads array. For example:
146 1. Current thread loads other_threads[i] to thr and checks it, it is
147 non-NULL.
148 2. Current thread is suspended by OS.
149 3. Another thread unregisters and finishes (debug versions of free()
150 may fill memory with something like 0xEF).
151 4. Current thread is resumed.
152 5. Current thread reads junk from *thr.
153 TODO: Fix it. --ln */
154
155 for (i = 0; i < __kmp_threads_capacity; i++) {
156
157 kmp_info_t *thr = (kmp_info_t *)TCR_SYNC_PTR(other_threads[i]);
158 if (!thr)
159 continue;
160
161 stack_size = (size_t)TCR_PTR(thr->th.th_info.ds.ds_stacksize);
162 stack_base = (char *)TCR_PTR(thr->th.th_info.ds.ds_stackbase);
163
164 /* stack grows down -- search through all of the active threads */
165
166 if (stack_addr <= stack_base) {
167 size_t stack_diff = stack_base - stack_addr;
168
169 if (stack_diff <= stack_size) {
170 /* The only way we can be closer than the allocated */
171 /* stack size is if we are running on this thread. */
172 KMP_DEBUG_ASSERT(__kmp_gtid_get_specific() == i);
173 return i;
174 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000175 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000176 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000177
Jonathan Peyton30419822017-05-12 18:01:32 +0000178 /* get specific to try and determine our gtid */
179 KA_TRACE(1000,
180 ("*** __kmp_get_global_thread_id: internal alg. failed to find "
181 "thread, using TLS\n"));
182 i = __kmp_gtid_get_specific();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000183
Jonathan Peyton30419822017-05-12 18:01:32 +0000184 /*fprintf( stderr, "=== %d\n", i ); */ /* GROO */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000185
Jonathan Peyton30419822017-05-12 18:01:32 +0000186 /* if we havn't been assigned a gtid, then return code */
187 if (i < 0)
Jim Cownie5e8470a2013-09-27 10:38:44 +0000188 return i;
Jonathan Peyton30419822017-05-12 18:01:32 +0000189
190 /* dynamically updated stack window for uber threads to avoid get_specific
191 call */
192 if (!TCR_4(other_threads[i]->th.th_info.ds.ds_stackgrow)) {
193 KMP_FATAL(StackOverflow, i);
194 }
195
196 stack_base = (char *)other_threads[i]->th.th_info.ds.ds_stackbase;
197 if (stack_addr > stack_base) {
198 TCW_PTR(other_threads[i]->th.th_info.ds.ds_stackbase, stack_addr);
199 TCW_PTR(other_threads[i]->th.th_info.ds.ds_stacksize,
200 other_threads[i]->th.th_info.ds.ds_stacksize + stack_addr -
201 stack_base);
202 } else {
203 TCW_PTR(other_threads[i]->th.th_info.ds.ds_stacksize,
204 stack_base - stack_addr);
205 }
206
207 /* Reprint stack bounds for ubermaster since they have been refined */
208 if (__kmp_storage_map) {
209 char *stack_end = (char *)other_threads[i]->th.th_info.ds.ds_stackbase;
210 char *stack_beg = stack_end - other_threads[i]->th.th_info.ds.ds_stacksize;
211 __kmp_print_storage_map_gtid(i, stack_beg, stack_end,
212 other_threads[i]->th.th_info.ds.ds_stacksize,
213 "th_%d stack (refinement)", i);
214 }
215 return i;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000216}
217
Jonathan Peyton30419822017-05-12 18:01:32 +0000218int __kmp_get_global_thread_id_reg() {
219 int gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000220
Jonathan Peyton30419822017-05-12 18:01:32 +0000221 if (!__kmp_init_serial) {
222 gtid = KMP_GTID_DNE;
223 } else
Jim Cownie5e8470a2013-09-27 10:38:44 +0000224#ifdef KMP_TDATA_GTID
Jonathan Peyton30419822017-05-12 18:01:32 +0000225 if (TCR_4(__kmp_gtid_mode) >= 3) {
226 KA_TRACE(1000, ("*** __kmp_get_global_thread_id_reg: using TDATA\n"));
227 gtid = __kmp_gtid;
228 } else
Jim Cownie5e8470a2013-09-27 10:38:44 +0000229#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000230 if (TCR_4(__kmp_gtid_mode) >= 2) {
231 KA_TRACE(1000, ("*** __kmp_get_global_thread_id_reg: using keyed TLS\n"));
232 gtid = __kmp_gtid_get_specific();
233 } else {
234 KA_TRACE(1000,
235 ("*** __kmp_get_global_thread_id_reg: using internal alg.\n"));
236 gtid = __kmp_get_global_thread_id();
237 }
238
239 /* we must be a new uber master sibling thread */
240 if (gtid == KMP_GTID_DNE) {
241 KA_TRACE(10,
242 ("__kmp_get_global_thread_id_reg: Encountered new root thread. "
243 "Registering a new gtid.\n"));
244 __kmp_acquire_bootstrap_lock(&__kmp_initz_lock);
245 if (!__kmp_init_serial) {
246 __kmp_do_serial_initialize();
247 gtid = __kmp_gtid_get_specific();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000248 } else {
Jonathan Peyton30419822017-05-12 18:01:32 +0000249 gtid = __kmp_register_root(FALSE);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000250 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000251 __kmp_release_bootstrap_lock(&__kmp_initz_lock);
252 /*__kmp_printf( "+++ %d\n", gtid ); */ /* GROO */
253 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000254
Jonathan Peyton30419822017-05-12 18:01:32 +0000255 KMP_DEBUG_ASSERT(gtid >= 0);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000256
Jonathan Peyton30419822017-05-12 18:01:32 +0000257 return gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000258}
259
260/* caller must hold forkjoin_lock */
Jonathan Peyton30419822017-05-12 18:01:32 +0000261void __kmp_check_stack_overlap(kmp_info_t *th) {
262 int f;
263 char *stack_beg = NULL;
264 char *stack_end = NULL;
265 int gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000266
Jonathan Peyton30419822017-05-12 18:01:32 +0000267 KA_TRACE(10, ("__kmp_check_stack_overlap: called\n"));
268 if (__kmp_storage_map) {
269 stack_end = (char *)th->th.th_info.ds.ds_stackbase;
270 stack_beg = stack_end - th->th.th_info.ds.ds_stacksize;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000271
Jonathan Peyton30419822017-05-12 18:01:32 +0000272 gtid = __kmp_gtid_from_thread(th);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000273
Jonathan Peyton30419822017-05-12 18:01:32 +0000274 if (gtid == KMP_GTID_MONITOR) {
275 __kmp_print_storage_map_gtid(
276 gtid, stack_beg, stack_end, th->th.th_info.ds.ds_stacksize,
277 "th_%s stack (%s)", "mon",
278 (th->th.th_info.ds.ds_stackgrow) ? "initial" : "actual");
Jim Cownie5e8470a2013-09-27 10:38:44 +0000279 } else {
Jonathan Peyton30419822017-05-12 18:01:32 +0000280 __kmp_print_storage_map_gtid(
281 gtid, stack_beg, stack_end, th->th.th_info.ds.ds_stacksize,
282 "th_%d stack (%s)", gtid,
283 (th->th.th_info.ds.ds_stackgrow) ? "initial" : "actual");
284 }
285 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000286
Jonathan Peyton30419822017-05-12 18:01:32 +0000287 /* No point in checking ubermaster threads since they use refinement and
288 * cannot overlap */
289 gtid = __kmp_gtid_from_thread(th);
290 if (__kmp_env_checks == TRUE && !KMP_UBER_GTID(gtid)) {
291 KA_TRACE(10,
292 ("__kmp_check_stack_overlap: performing extensive checking\n"));
293 if (stack_beg == NULL) {
294 stack_end = (char *)th->th.th_info.ds.ds_stackbase;
295 stack_beg = stack_end - th->th.th_info.ds.ds_stacksize;
296 }
297
298 for (f = 0; f < __kmp_threads_capacity; f++) {
299 kmp_info_t *f_th = (kmp_info_t *)TCR_SYNC_PTR(__kmp_threads[f]);
300
301 if (f_th && f_th != th) {
302 char *other_stack_end =
303 (char *)TCR_PTR(f_th->th.th_info.ds.ds_stackbase);
304 char *other_stack_beg =
305 other_stack_end - (size_t)TCR_PTR(f_th->th.th_info.ds.ds_stacksize);
306 if ((stack_beg > other_stack_beg && stack_beg < other_stack_end) ||
307 (stack_end > other_stack_beg && stack_end < other_stack_end)) {
308
309 /* Print the other stack values before the abort */
310 if (__kmp_storage_map)
311 __kmp_print_storage_map_gtid(
312 -1, other_stack_beg, other_stack_end,
313 (size_t)TCR_PTR(f_th->th.th_info.ds.ds_stacksize),
314 "th_%d stack (overlapped)", __kmp_gtid_from_thread(f_th));
315
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000316 __kmp_fatal(KMP_MSG(StackOverlap), KMP_HNT(ChangeStackLimit),
317 __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +0000318 }
319 }
320 }
321 }
322 KA_TRACE(10, ("__kmp_check_stack_overlap: returning\n"));
323}
324
325/* ------------------------------------------------------------------------ */
326
327void __kmp_infinite_loop(void) {
328 static int done = FALSE;
329
330 while (!done) {
331 KMP_YIELD(1);
332 }
333}
334
335#define MAX_MESSAGE 512
336
337void __kmp_print_storage_map_gtid(int gtid, void *p1, void *p2, size_t size,
338 char const *format, ...) {
339 char buffer[MAX_MESSAGE];
340 va_list ap;
341
342 va_start(ap, format);
343 KMP_SNPRINTF(buffer, sizeof(buffer), "OMP storage map: %p %p%8lu %s\n", p1,
344 p2, (unsigned long)size, format);
345 __kmp_acquire_bootstrap_lock(&__kmp_stdio_lock);
346 __kmp_vprintf(kmp_err, buffer, ap);
347#if KMP_PRINT_DATA_PLACEMENT
348 int node;
349 if (gtid >= 0) {
350 if (p1 <= p2 && (char *)p2 - (char *)p1 == size) {
351 if (__kmp_storage_map_verbose) {
352 node = __kmp_get_host_node(p1);
353 if (node < 0) /* doesn't work, so don't try this next time */
354 __kmp_storage_map_verbose = FALSE;
355 else {
356 char *last;
357 int lastNode;
358 int localProc = __kmp_get_cpu_from_gtid(gtid);
359
360 const int page_size = KMP_GET_PAGE_SIZE();
361
362 p1 = (void *)((size_t)p1 & ~((size_t)page_size - 1));
363 p2 = (void *)(((size_t)p2 - 1) & ~((size_t)page_size - 1));
364 if (localProc >= 0)
365 __kmp_printf_no_lock(" GTID %d localNode %d\n", gtid,
366 localProc >> 1);
367 else
368 __kmp_printf_no_lock(" GTID %d\n", gtid);
369#if KMP_USE_PRCTL
370 /* The more elaborate format is disabled for now because of the prctl
371 * hanging bug. */
372 do {
373 last = p1;
374 lastNode = node;
375 /* This loop collates adjacent pages with the same host node. */
376 do {
377 (char *)p1 += page_size;
378 } while (p1 <= p2 && (node = __kmp_get_host_node(p1)) == lastNode);
379 __kmp_printf_no_lock(" %p-%p memNode %d\n", last, (char *)p1 - 1,
380 lastNode);
381 } while (p1 <= p2);
382#else
383 __kmp_printf_no_lock(" %p-%p memNode %d\n", p1,
384 (char *)p1 + (page_size - 1),
385 __kmp_get_host_node(p1));
386 if (p1 < p2) {
387 __kmp_printf_no_lock(" %p-%p memNode %d\n", p2,
388 (char *)p2 + (page_size - 1),
389 __kmp_get_host_node(p2));
390 }
391#endif
392 }
393 }
394 } else
395 __kmp_printf_no_lock(" %s\n", KMP_I18N_STR(StorageMapWarning));
396 }
397#endif /* KMP_PRINT_DATA_PLACEMENT */
398 __kmp_release_bootstrap_lock(&__kmp_stdio_lock);
399}
400
401void __kmp_warn(char const *format, ...) {
402 char buffer[MAX_MESSAGE];
403 va_list ap;
404
405 if (__kmp_generate_warnings == kmp_warnings_off) {
406 return;
407 }
408
409 va_start(ap, format);
410
411 KMP_SNPRINTF(buffer, sizeof(buffer), "OMP warning: %s\n", format);
412 __kmp_acquire_bootstrap_lock(&__kmp_stdio_lock);
413 __kmp_vprintf(kmp_err, buffer, ap);
414 __kmp_release_bootstrap_lock(&__kmp_stdio_lock);
415
416 va_end(ap);
417}
418
419void __kmp_abort_process() {
420 // Later threads may stall here, but that's ok because abort() will kill them.
421 __kmp_acquire_bootstrap_lock(&__kmp_exit_lock);
422
423 if (__kmp_debug_buf) {
424 __kmp_dump_debug_buffer();
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000425 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000426
427 if (KMP_OS_WINDOWS) {
428 // Let other threads know of abnormal termination and prevent deadlock
429 // if abort happened during library initialization or shutdown
430 __kmp_global.g.g_abort = SIGABRT;
431
432 /* On Windows* OS by default abort() causes pop-up error box, which stalls
433 nightly testing. Unfortunately, we cannot reliably suppress pop-up error
434 boxes. _set_abort_behavior() works well, but this function is not
435 available in VS7 (this is not problem for DLL, but it is a problem for
436 static OpenMP RTL). SetErrorMode (and so, timelimit utility) does not
437 help, at least in some versions of MS C RTL.
438
439 It seems following sequence is the only way to simulate abort() and
440 avoid pop-up error box. */
441 raise(SIGABRT);
442 _exit(3); // Just in case, if signal ignored, exit anyway.
443 } else {
444 abort();
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000445 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000446
447 __kmp_infinite_loop();
448 __kmp_release_bootstrap_lock(&__kmp_exit_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000449
450} // __kmp_abort_process
451
Jonathan Peyton30419822017-05-12 18:01:32 +0000452void __kmp_abort_thread(void) {
453 // TODO: Eliminate g_abort global variable and this function.
454 // In case of abort just call abort(), it will kill all the threads.
455 __kmp_infinite_loop();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000456} // __kmp_abort_thread
457
Jonathan Peyton30419822017-05-12 18:01:32 +0000458/* Print out the storage map for the major kmp_info_t thread data structures
459 that are allocated together. */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000460
Jonathan Peyton30419822017-05-12 18:01:32 +0000461static void __kmp_print_thread_storage_map(kmp_info_t *thr, int gtid) {
462 __kmp_print_storage_map_gtid(gtid, thr, thr + 1, sizeof(kmp_info_t), "th_%d",
463 gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000464
Jonathan Peyton30419822017-05-12 18:01:32 +0000465 __kmp_print_storage_map_gtid(gtid, &thr->th.th_info, &thr->th.th_team,
466 sizeof(kmp_desc_t), "th_%d.th_info", gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000467
Jonathan Peyton30419822017-05-12 18:01:32 +0000468 __kmp_print_storage_map_gtid(gtid, &thr->th.th_local, &thr->th.th_pri_head,
469 sizeof(kmp_local_t), "th_%d.th_local", gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000470
Jonathan Peyton30419822017-05-12 18:01:32 +0000471 __kmp_print_storage_map_gtid(
472 gtid, &thr->th.th_bar[0], &thr->th.th_bar[bs_last_barrier],
473 sizeof(kmp_balign_t) * bs_last_barrier, "th_%d.th_bar", gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000474
Jonathan Peyton30419822017-05-12 18:01:32 +0000475 __kmp_print_storage_map_gtid(gtid, &thr->th.th_bar[bs_plain_barrier],
476 &thr->th.th_bar[bs_plain_barrier + 1],
477 sizeof(kmp_balign_t), "th_%d.th_bar[plain]",
478 gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000479
Jonathan Peyton30419822017-05-12 18:01:32 +0000480 __kmp_print_storage_map_gtid(gtid, &thr->th.th_bar[bs_forkjoin_barrier],
481 &thr->th.th_bar[bs_forkjoin_barrier + 1],
482 sizeof(kmp_balign_t), "th_%d.th_bar[forkjoin]",
483 gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000484
Jonathan Peyton30419822017-05-12 18:01:32 +0000485#if KMP_FAST_REDUCTION_BARRIER
486 __kmp_print_storage_map_gtid(gtid, &thr->th.th_bar[bs_reduction_barrier],
487 &thr->th.th_bar[bs_reduction_barrier + 1],
488 sizeof(kmp_balign_t), "th_%d.th_bar[reduction]",
489 gtid);
490#endif // KMP_FAST_REDUCTION_BARRIER
Jim Cownie5e8470a2013-09-27 10:38:44 +0000491}
492
Jonathan Peyton30419822017-05-12 18:01:32 +0000493/* Print out the storage map for the major kmp_team_t team data structures
494 that are allocated together. */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000495
Jonathan Peyton30419822017-05-12 18:01:32 +0000496static void __kmp_print_team_storage_map(const char *header, kmp_team_t *team,
497 int team_id, int num_thr) {
498 int num_disp_buff = team->t.t_max_nproc > 1 ? __kmp_dispatch_num_buffers : 2;
499 __kmp_print_storage_map_gtid(-1, team, team + 1, sizeof(kmp_team_t), "%s_%d",
500 header, team_id);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000501
Jonathan Peyton30419822017-05-12 18:01:32 +0000502 __kmp_print_storage_map_gtid(-1, &team->t.t_bar[0],
503 &team->t.t_bar[bs_last_barrier],
504 sizeof(kmp_balign_team_t) * bs_last_barrier,
505 "%s_%d.t_bar", header, team_id);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000506
Jonathan Peyton30419822017-05-12 18:01:32 +0000507 __kmp_print_storage_map_gtid(-1, &team->t.t_bar[bs_plain_barrier],
508 &team->t.t_bar[bs_plain_barrier + 1],
509 sizeof(kmp_balign_team_t), "%s_%d.t_bar[plain]",
510 header, team_id);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000511
Jonathan Peyton30419822017-05-12 18:01:32 +0000512 __kmp_print_storage_map_gtid(-1, &team->t.t_bar[bs_forkjoin_barrier],
513 &team->t.t_bar[bs_forkjoin_barrier + 1],
514 sizeof(kmp_balign_team_t),
515 "%s_%d.t_bar[forkjoin]", header, team_id);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000516
Jonathan Peyton30419822017-05-12 18:01:32 +0000517#if KMP_FAST_REDUCTION_BARRIER
518 __kmp_print_storage_map_gtid(-1, &team->t.t_bar[bs_reduction_barrier],
519 &team->t.t_bar[bs_reduction_barrier + 1],
520 sizeof(kmp_balign_team_t),
521 "%s_%d.t_bar[reduction]", header, team_id);
522#endif // KMP_FAST_REDUCTION_BARRIER
Jim Cownie5e8470a2013-09-27 10:38:44 +0000523
Jonathan Peyton30419822017-05-12 18:01:32 +0000524 __kmp_print_storage_map_gtid(
525 -1, &team->t.t_dispatch[0], &team->t.t_dispatch[num_thr],
526 sizeof(kmp_disp_t) * num_thr, "%s_%d.t_dispatch", header, team_id);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000527
Jonathan Peyton30419822017-05-12 18:01:32 +0000528 __kmp_print_storage_map_gtid(
529 -1, &team->t.t_threads[0], &team->t.t_threads[num_thr],
530 sizeof(kmp_info_t *) * num_thr, "%s_%d.t_threads", header, team_id);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000531
Jonathan Peyton30419822017-05-12 18:01:32 +0000532 __kmp_print_storage_map_gtid(-1, &team->t.t_disp_buffer[0],
533 &team->t.t_disp_buffer[num_disp_buff],
534 sizeof(dispatch_shared_info_t) * num_disp_buff,
535 "%s_%d.t_disp_buffer", header, team_id);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000536
Jonathan Peyton30419822017-05-12 18:01:32 +0000537 __kmp_print_storage_map_gtid(-1, &team->t.t_taskq, &team->t.t_copypriv_data,
538 sizeof(kmp_taskq_t), "%s_%d.t_taskq", header,
539 team_id);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000540}
541
542static void __kmp_init_allocator() {}
543static void __kmp_fini_allocator() {}
Jim Cownie5e8470a2013-09-27 10:38:44 +0000544
545/* ------------------------------------------------------------------------ */
546
Jonathan Peyton99016992015-05-26 17:32:53 +0000547#ifdef KMP_DYNAMIC_LIB
Jonathan Peyton30419822017-05-12 18:01:32 +0000548#if KMP_OS_WINDOWS
Jim Cownie5e8470a2013-09-27 10:38:44 +0000549
Jonathan Peyton30419822017-05-12 18:01:32 +0000550static void __kmp_reset_lock(kmp_bootstrap_lock_t *lck) {
551 // TODO: Change to __kmp_break_bootstrap_lock().
552 __kmp_init_bootstrap_lock(lck); // make the lock released
Jim Cownie5e8470a2013-09-27 10:38:44 +0000553}
554
Jonathan Peyton30419822017-05-12 18:01:32 +0000555static void __kmp_reset_locks_on_process_detach(int gtid_req) {
556 int i;
557 int thread_count;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000558
Jonathan Peyton30419822017-05-12 18:01:32 +0000559 // PROCESS_DETACH is expected to be called by a thread that executes
560 // ProcessExit() or FreeLibrary(). OS terminates other threads (except the one
561 // calling ProcessExit or FreeLibrary). So, it might be safe to access the
562 // __kmp_threads[] without taking the forkjoin_lock. However, in fact, some
563 // threads can be still alive here, although being about to be terminated. The
564 // threads in the array with ds_thread==0 are most suspicious. Actually, it
565 // can be not safe to access the __kmp_threads[].
Jim Cownie5e8470a2013-09-27 10:38:44 +0000566
Jonathan Peyton30419822017-05-12 18:01:32 +0000567 // TODO: does it make sense to check __kmp_roots[] ?
Jim Cownie5e8470a2013-09-27 10:38:44 +0000568
Jonathan Peyton30419822017-05-12 18:01:32 +0000569 // Let's check that there are no other alive threads registered with the OMP
570 // lib.
571 while (1) {
572 thread_count = 0;
573 for (i = 0; i < __kmp_threads_capacity; ++i) {
574 if (!__kmp_threads)
575 continue;
576 kmp_info_t *th = __kmp_threads[i];
577 if (th == NULL)
578 continue;
579 int gtid = th->th.th_info.ds.ds_gtid;
580 if (gtid == gtid_req)
581 continue;
582 if (gtid < 0)
583 continue;
584 DWORD exit_val;
585 int alive = __kmp_is_thread_alive(th, &exit_val);
586 if (alive) {
587 ++thread_count;
588 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000589 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000590 if (thread_count == 0)
591 break; // success
592 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000593
Jonathan Peyton30419822017-05-12 18:01:32 +0000594 // Assume that I'm alone. Now it might be safe to check and reset locks.
595 // __kmp_forkjoin_lock and __kmp_stdio_lock are expected to be reset.
596 __kmp_reset_lock(&__kmp_forkjoin_lock);
597#ifdef KMP_DEBUG
598 __kmp_reset_lock(&__kmp_stdio_lock);
599#endif // KMP_DEBUG
Jim Cownie5e8470a2013-09-27 10:38:44 +0000600}
601
Jonathan Peyton30419822017-05-12 18:01:32 +0000602BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpReserved) {
603 //__kmp_acquire_bootstrap_lock( &__kmp_initz_lock );
Jim Cownie5e8470a2013-09-27 10:38:44 +0000604
Jonathan Peyton30419822017-05-12 18:01:32 +0000605 switch (fdwReason) {
Jim Cownie5e8470a2013-09-27 10:38:44 +0000606
Jonathan Peyton30419822017-05-12 18:01:32 +0000607 case DLL_PROCESS_ATTACH:
608 KA_TRACE(10, ("DllMain: PROCESS_ATTACH\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000609
610 return TRUE;
Jonathan Peyton30419822017-05-12 18:01:32 +0000611
612 case DLL_PROCESS_DETACH:
613 KA_TRACE(10, ("DllMain: PROCESS_DETACH T#%d\n", __kmp_gtid_get_specific()));
614
615 if (lpReserved != NULL) {
616 // lpReserved is used for telling the difference:
617 // lpReserved == NULL when FreeLibrary() was called,
618 // lpReserved != NULL when the process terminates.
619 // When FreeLibrary() is called, worker threads remain alive. So they will
620 // release the forkjoin lock by themselves. When the process terminates,
621 // worker threads disappear triggering the problem of unreleased forkjoin
622 // lock as described below.
623
624 // A worker thread can take the forkjoin lock. The problem comes up if
625 // that worker thread becomes dead before it releases the forkjoin lock.
626 // The forkjoin lock remains taken, while the thread executing
627 // DllMain()->PROCESS_DETACH->__kmp_internal_end_library() below will try
628 // to take the forkjoin lock and will always fail, so that the application
629 // will never finish [normally]. This scenario is possible if
630 // __kmpc_end() has not been executed. It looks like it's not a corner
631 // case, but common cases:
632 // - the main function was compiled by an alternative compiler;
633 // - the main function was compiled by icl but without /Qopenmp
634 // (application with plugins);
635 // - application terminates by calling C exit(), Fortran CALL EXIT() or
636 // Fortran STOP.
637 // - alive foreign thread prevented __kmpc_end from doing cleanup.
638 //
639 // This is a hack to work around the problem.
640 // TODO: !!! figure out something better.
641 __kmp_reset_locks_on_process_detach(__kmp_gtid_get_specific());
642 }
643
644 __kmp_internal_end_library(__kmp_gtid_get_specific());
645
646 return TRUE;
647
648 case DLL_THREAD_ATTACH:
649 KA_TRACE(10, ("DllMain: THREAD_ATTACH\n"));
650
651 /* if we want to register new siblings all the time here call
652 * __kmp_get_gtid(); */
653 return TRUE;
654
655 case DLL_THREAD_DETACH:
656 KA_TRACE(10, ("DllMain: THREAD_DETACH T#%d\n", __kmp_gtid_get_specific()));
657
658 __kmp_internal_end_thread(__kmp_gtid_get_specific());
659 return TRUE;
660 }
661
662 return TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000663}
664
Jonathan Peyton30419822017-05-12 18:01:32 +0000665#endif /* KMP_OS_WINDOWS */
Jonathan Peyton99016992015-05-26 17:32:53 +0000666#endif /* KMP_DYNAMIC_LIB */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000667
Jim Cownie5e8470a2013-09-27 10:38:44 +0000668/* Change the library type to "status" and return the old type */
669/* called from within initialization routines where __kmp_initz_lock is held */
Jonathan Peyton30419822017-05-12 18:01:32 +0000670int __kmp_change_library(int status) {
671 int old_status;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000672
Jonathan Peyton30419822017-05-12 18:01:32 +0000673 old_status = __kmp_yield_init &
674 1; // check whether KMP_LIBRARY=throughput (even init count)
Jim Cownie5e8470a2013-09-27 10:38:44 +0000675
Jonathan Peyton30419822017-05-12 18:01:32 +0000676 if (status) {
677 __kmp_yield_init |= 1; // throughput => turnaround (odd init count)
678 } else {
679 __kmp_yield_init &= ~1; // turnaround => throughput (even init count)
680 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000681
Jonathan Peyton30419822017-05-12 18:01:32 +0000682 return old_status; // return previous setting of whether
683 // KMP_LIBRARY=throughput
Jim Cownie5e8470a2013-09-27 10:38:44 +0000684}
685
Jonathan Peyton30419822017-05-12 18:01:32 +0000686/* __kmp_parallel_deo -- Wait until it's our turn. */
687void __kmp_parallel_deo(int *gtid_ref, int *cid_ref, ident_t *loc_ref) {
688 int gtid = *gtid_ref;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000689#ifdef BUILD_PARALLEL_ORDERED
Jonathan Peyton30419822017-05-12 18:01:32 +0000690 kmp_team_t *team = __kmp_team_from_gtid(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000691#endif /* BUILD_PARALLEL_ORDERED */
692
Jonathan Peyton30419822017-05-12 18:01:32 +0000693 if (__kmp_env_consistency_check) {
694 if (__kmp_threads[gtid]->th.th_root->r.r_active)
Andrey Churbanov5c56fb52015-02-20 18:05:17 +0000695#if KMP_USE_DYNAMIC_LOCK
Jonathan Peyton30419822017-05-12 18:01:32 +0000696 __kmp_push_sync(gtid, ct_ordered_in_parallel, loc_ref, NULL, 0);
Andrey Churbanov5c56fb52015-02-20 18:05:17 +0000697#else
Jonathan Peyton30419822017-05-12 18:01:32 +0000698 __kmp_push_sync(gtid, ct_ordered_in_parallel, loc_ref, NULL);
Andrey Churbanov5c56fb52015-02-20 18:05:17 +0000699#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000700 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000701#ifdef BUILD_PARALLEL_ORDERED
Jonathan Peyton30419822017-05-12 18:01:32 +0000702 if (!team->t.t_serialized) {
703 KMP_MB();
704 KMP_WAIT_YIELD(&team->t.t_ordered.dt.t_value, __kmp_tid_from_gtid(gtid),
705 KMP_EQ, NULL);
706 KMP_MB();
707 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000708#endif /* BUILD_PARALLEL_ORDERED */
709}
710
Jonathan Peyton30419822017-05-12 18:01:32 +0000711/* __kmp_parallel_dxo -- Signal the next task. */
712void __kmp_parallel_dxo(int *gtid_ref, int *cid_ref, ident_t *loc_ref) {
713 int gtid = *gtid_ref;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000714#ifdef BUILD_PARALLEL_ORDERED
Jonathan Peyton30419822017-05-12 18:01:32 +0000715 int tid = __kmp_tid_from_gtid(gtid);
716 kmp_team_t *team = __kmp_team_from_gtid(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000717#endif /* BUILD_PARALLEL_ORDERED */
718
Jonathan Peyton30419822017-05-12 18:01:32 +0000719 if (__kmp_env_consistency_check) {
720 if (__kmp_threads[gtid]->th.th_root->r.r_active)
721 __kmp_pop_sync(gtid, ct_ordered_in_parallel, loc_ref);
722 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000723#ifdef BUILD_PARALLEL_ORDERED
Jonathan Peyton30419822017-05-12 18:01:32 +0000724 if (!team->t.t_serialized) {
725 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000726
Jonathan Peyton30419822017-05-12 18:01:32 +0000727 /* use the tid of the next thread in this team */
728 /* TODO replace with general release procedure */
729 team->t.t_ordered.dt.t_value = ((tid + 1) % team->t.t_nproc);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000730
Jonathan Peyton30419822017-05-12 18:01:32 +0000731 KMP_MB(); /* Flush all pending memory write invalidates. */
732 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000733#endif /* BUILD_PARALLEL_ORDERED */
734}
735
736/* ------------------------------------------------------------------------ */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000737/* The BARRIER for a SINGLE process section is always explicit */
738
Jonathan Peyton30419822017-05-12 18:01:32 +0000739int __kmp_enter_single(int gtid, ident_t *id_ref, int push_ws) {
740 int status;
741 kmp_info_t *th;
742 kmp_team_t *team;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000743
Jonathan Peyton30419822017-05-12 18:01:32 +0000744 if (!TCR_4(__kmp_init_parallel))
745 __kmp_parallel_initialize();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000746
Jonathan Peyton30419822017-05-12 18:01:32 +0000747 th = __kmp_threads[gtid];
748 team = th->th.th_team;
749 status = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000750
Jonathan Peyton30419822017-05-12 18:01:32 +0000751 th->th.th_ident = id_ref;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000752
Jonathan Peyton30419822017-05-12 18:01:32 +0000753 if (team->t.t_serialized) {
754 status = 1;
755 } else {
756 kmp_int32 old_this = th->th.th_local.this_construct;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000757
Jonathan Peyton30419822017-05-12 18:01:32 +0000758 ++th->th.th_local.this_construct;
759 /* try to set team count to thread count--success means thread got the
760 single block */
761 /* TODO: Should this be acquire or release? */
762 if (team->t.t_construct == old_this) {
Jonathan Peyton37e2ef52018-07-09 17:36:22 +0000763 status = __kmp_atomic_compare_store_acq(&team->t.t_construct, old_this,
764 th->th.th_local.this_construct);
Jonathan Peyton30419822017-05-12 18:01:32 +0000765 }
Andrey Churbanov51aecb82015-05-06 19:22:36 +0000766#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000767 if (__itt_metadata_add_ptr && __kmp_forkjoin_frames_mode == 3 &&
768 KMP_MASTER_GTID(gtid) &&
Andrey Churbanov51aecb82015-05-06 19:22:36 +0000769#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +0000770 th->th.th_teams_microtask == NULL &&
Andrey Churbanov51aecb82015-05-06 19:22:36 +0000771#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000772 team->t.t_active_level ==
773 1) { // Only report metadata by master of active team at level 1
774 __kmp_itt_metadata_single(id_ref);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000775 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000776#endif /* USE_ITT_BUILD */
777 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000778
Jonathan Peyton30419822017-05-12 18:01:32 +0000779 if (__kmp_env_consistency_check) {
780 if (status && push_ws) {
781 __kmp_push_workshare(gtid, ct_psingle, id_ref);
782 } else {
783 __kmp_check_workshare(gtid, ct_psingle, id_ref);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000784 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000785 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000786#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000787 if (status) {
788 __kmp_itt_single_start(gtid);
789 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000790#endif /* USE_ITT_BUILD */
Jonathan Peyton30419822017-05-12 18:01:32 +0000791 return status;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000792}
793
Jonathan Peyton30419822017-05-12 18:01:32 +0000794void __kmp_exit_single(int gtid) {
Jim Cownie5e8470a2013-09-27 10:38:44 +0000795#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000796 __kmp_itt_single_end(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000797#endif /* USE_ITT_BUILD */
Jonathan Peyton30419822017-05-12 18:01:32 +0000798 if (__kmp_env_consistency_check)
799 __kmp_pop_workshare(gtid, ct_psingle, NULL);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000800}
801
Jonathan Peyton30419822017-05-12 18:01:32 +0000802/* determine if we can go parallel or must use a serialized parallel region and
Jim Cownie5e8470a2013-09-27 10:38:44 +0000803 * how many threads we can use
804 * set_nproc is the number of threads requested for the team
805 * returns 0 if we should serialize or only use one thread,
806 * otherwise the number of threads to use
Jonathan Peyton30419822017-05-12 18:01:32 +0000807 * The forkjoin lock is held by the caller. */
808static int __kmp_reserve_threads(kmp_root_t *root, kmp_team_t *parent_team,
809 int master_tid, int set_nthreads
Jim Cownie5e8470a2013-09-27 10:38:44 +0000810#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +0000811 ,
812 int enter_teams
Jim Cownie5e8470a2013-09-27 10:38:44 +0000813#endif /* OMP_40_ENABLED */
Jonathan Peyton30419822017-05-12 18:01:32 +0000814 ) {
815 int capacity;
816 int new_nthreads;
817 KMP_DEBUG_ASSERT(__kmp_init_serial);
818 KMP_DEBUG_ASSERT(root && parent_team);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000819
Jonathan Peyton30419822017-05-12 18:01:32 +0000820 // If dyn-var is set, dynamically adjust the number of desired threads,
821 // according to the method specified by dynamic_mode.
822 new_nthreads = set_nthreads;
823 if (!get__dynamic_2(parent_team, master_tid)) {
824 ;
825 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000826#ifdef USE_LOAD_BALANCE
Jonathan Peyton30419822017-05-12 18:01:32 +0000827 else if (__kmp_global.g.g_dynamic_mode == dynamic_load_balance) {
828 new_nthreads = __kmp_load_balance_nproc(root, set_nthreads);
829 if (new_nthreads == 1) {
830 KC_TRACE(10, ("__kmp_reserve_threads: T#%d load balance reduced "
831 "reservation to 1 thread\n",
832 master_tid));
833 return 1;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000834 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000835 if (new_nthreads < set_nthreads) {
836 KC_TRACE(10, ("__kmp_reserve_threads: T#%d load balance reduced "
837 "reservation to %d threads\n",
838 master_tid, new_nthreads));
839 }
840 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000841#endif /* USE_LOAD_BALANCE */
Jonathan Peyton30419822017-05-12 18:01:32 +0000842 else if (__kmp_global.g.g_dynamic_mode == dynamic_thread_limit) {
843 new_nthreads = __kmp_avail_proc - __kmp_nth +
844 (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc);
845 if (new_nthreads <= 1) {
846 KC_TRACE(10, ("__kmp_reserve_threads: T#%d thread limit reduced "
847 "reservation to 1 thread\n",
848 master_tid));
849 return 1;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000850 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000851 if (new_nthreads < set_nthreads) {
852 KC_TRACE(10, ("__kmp_reserve_threads: T#%d thread limit reduced "
853 "reservation to %d threads\n",
854 master_tid, new_nthreads));
855 } else {
856 new_nthreads = set_nthreads;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000857 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000858 } else if (__kmp_global.g.g_dynamic_mode == dynamic_random) {
859 if (set_nthreads > 2) {
860 new_nthreads = __kmp_get_random(parent_team->t.t_threads[master_tid]);
861 new_nthreads = (new_nthreads % set_nthreads) + 1;
862 if (new_nthreads == 1) {
863 KC_TRACE(10, ("__kmp_reserve_threads: T#%d dynamic random reduced "
864 "reservation to 1 thread\n",
865 master_tid));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000866 return 1;
Jonathan Peyton30419822017-05-12 18:01:32 +0000867 }
868 if (new_nthreads < set_nthreads) {
869 KC_TRACE(10, ("__kmp_reserve_threads: T#%d dynamic random reduced "
870 "reservation to %d threads\n",
871 master_tid, new_nthreads));
872 }
873 }
874 } else {
875 KMP_ASSERT(0);
876 }
877
Jonathan Peytonf4392462017-07-27 20:58:41 +0000878 // Respect KMP_ALL_THREADS/KMP_DEVICE_THREAD_LIMIT.
Jonathan Peyton30419822017-05-12 18:01:32 +0000879 if (__kmp_nth + new_nthreads -
880 (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc) >
881 __kmp_max_nth) {
882 int tl_nthreads = __kmp_max_nth - __kmp_nth +
883 (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc);
884 if (tl_nthreads <= 0) {
885 tl_nthreads = 1;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000886 }
887
Jonathan Peyton30419822017-05-12 18:01:32 +0000888 // If dyn-var is false, emit a 1-time warning.
889 if (!get__dynamic_2(parent_team, master_tid) && (!__kmp_reserve_warn)) {
890 __kmp_reserve_warn = 1;
891 __kmp_msg(kmp_ms_warning,
892 KMP_MSG(CantFormThrTeam, set_nthreads, tl_nthreads),
893 KMP_HNT(Unset_ALL_THREADS), __kmp_msg_null);
894 }
895 if (tl_nthreads == 1) {
Jonathan Peytonf4392462017-07-27 20:58:41 +0000896 KC_TRACE(10, ("__kmp_reserve_threads: T#%d KMP_DEVICE_THREAD_LIMIT "
897 "reduced reservation to 1 thread\n",
Jonathan Peyton30419822017-05-12 18:01:32 +0000898 master_tid));
899 return 1;
900 }
Jonathan Peytonf4392462017-07-27 20:58:41 +0000901 KC_TRACE(10, ("__kmp_reserve_threads: T#%d KMP_DEVICE_THREAD_LIMIT reduced "
902 "reservation to %d threads\n",
903 master_tid, tl_nthreads));
904 new_nthreads = tl_nthreads;
905 }
906
907 // Respect OMP_THREAD_LIMIT
908 if (root->r.r_cg_nthreads + new_nthreads -
909 (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc) >
910 __kmp_cg_max_nth) {
911 int tl_nthreads = __kmp_cg_max_nth - root->r.r_cg_nthreads +
912 (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc);
913 if (tl_nthreads <= 0) {
914 tl_nthreads = 1;
915 }
916
917 // If dyn-var is false, emit a 1-time warning.
918 if (!get__dynamic_2(parent_team, master_tid) && (!__kmp_reserve_warn)) {
919 __kmp_reserve_warn = 1;
920 __kmp_msg(kmp_ms_warning,
921 KMP_MSG(CantFormThrTeam, set_nthreads, tl_nthreads),
922 KMP_HNT(Unset_ALL_THREADS), __kmp_msg_null);
923 }
924 if (tl_nthreads == 1) {
925 KC_TRACE(10, ("__kmp_reserve_threads: T#%d OMP_THREAD_LIMIT "
926 "reduced reservation to 1 thread\n",
927 master_tid));
928 return 1;
929 }
930 KC_TRACE(10, ("__kmp_reserve_threads: T#%d OMP_THREAD_LIMIT reduced "
Jonathan Peyton30419822017-05-12 18:01:32 +0000931 "reservation to %d threads\n",
932 master_tid, tl_nthreads));
933 new_nthreads = tl_nthreads;
934 }
935
936 // Check if the threads array is large enough, or needs expanding.
Jonathan Peyton30419822017-05-12 18:01:32 +0000937 // See comment in __kmp_register_root() about the adjustment if
938 // __kmp_threads[0] == NULL.
939 capacity = __kmp_threads_capacity;
940 if (TCR_PTR(__kmp_threads[0]) == NULL) {
941 --capacity;
942 }
943 if (__kmp_nth + new_nthreads -
944 (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc) >
945 capacity) {
946 // Expand the threads array.
947 int slotsRequired = __kmp_nth + new_nthreads -
948 (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc) -
949 capacity;
Jonathan Peyton1800ece2018-01-10 18:27:01 +0000950 int slotsAdded = __kmp_expand_threads(slotsRequired);
Jonathan Peyton30419822017-05-12 18:01:32 +0000951 if (slotsAdded < slotsRequired) {
952 // The threads array was not expanded enough.
953 new_nthreads -= (slotsRequired - slotsAdded);
954 KMP_ASSERT(new_nthreads >= 1);
955
956 // If dyn-var is false, emit a 1-time warning.
957 if (!get__dynamic_2(parent_team, master_tid) && (!__kmp_reserve_warn)) {
958 __kmp_reserve_warn = 1;
959 if (__kmp_tp_cached) {
960 __kmp_msg(kmp_ms_warning,
961 KMP_MSG(CantFormThrTeam, set_nthreads, new_nthreads),
962 KMP_HNT(Set_ALL_THREADPRIVATE, __kmp_tp_capacity),
963 KMP_HNT(PossibleSystemLimitOnThreads), __kmp_msg_null);
964 } else {
965 __kmp_msg(kmp_ms_warning,
966 KMP_MSG(CantFormThrTeam, set_nthreads, new_nthreads),
967 KMP_HNT(SystemLimitOnThreads), __kmp_msg_null);
968 }
969 }
970 }
971 }
972
Jonathan Peyton642688b2017-06-01 16:46:36 +0000973#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +0000974 if (new_nthreads == 1) {
975 KC_TRACE(10,
976 ("__kmp_reserve_threads: T#%d serializing team after reclaiming "
977 "dead roots and rechecking; requested %d threads\n",
978 __kmp_get_gtid(), set_nthreads));
Jonathan Peyton642688b2017-06-01 16:46:36 +0000979 } else {
980 KC_TRACE(10, ("__kmp_reserve_threads: T#%d allocating %d threads; requested"
981 " %d threads\n",
982 __kmp_get_gtid(), new_nthreads, set_nthreads));
Jonathan Peyton30419822017-05-12 18:01:32 +0000983 }
Jonathan Peyton642688b2017-06-01 16:46:36 +0000984#endif // KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +0000985 return new_nthreads;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000986}
987
Jonathan Peyton30419822017-05-12 18:01:32 +0000988/* Allocate threads from the thread pool and assign them to the new team. We are
989 assured that there are enough threads available, because we checked on that
990 earlier within critical section forkjoin */
991static void __kmp_fork_team_threads(kmp_root_t *root, kmp_team_t *team,
992 kmp_info_t *master_th, int master_gtid) {
993 int i;
994 int use_hot_team;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000995
Jonathan Peyton30419822017-05-12 18:01:32 +0000996 KA_TRACE(10, ("__kmp_fork_team_threads: new_nprocs = %d\n", team->t.t_nproc));
997 KMP_DEBUG_ASSERT(master_gtid == __kmp_get_gtid());
998 KMP_MB();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000999
Jonathan Peyton30419822017-05-12 18:01:32 +00001000 /* first, let's setup the master thread */
1001 master_th->th.th_info.ds.ds_tid = 0;
1002 master_th->th.th_team = team;
1003 master_th->th.th_team_nproc = team->t.t_nproc;
1004 master_th->th.th_team_master = master_th;
1005 master_th->th.th_team_serialized = FALSE;
1006 master_th->th.th_dispatch = &team->t.t_dispatch[0];
Jim Cownie5e8470a2013-09-27 10:38:44 +00001007
Jonathan Peyton30419822017-05-12 18:01:32 +00001008/* make sure we are not the optimized hot team */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001009#if KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00001010 use_hot_team = 0;
1011 kmp_hot_team_ptr_t *hot_teams = master_th->th.th_hot_teams;
1012 if (hot_teams) { // hot teams array is not allocated if
1013 // KMP_HOT_TEAMS_MAX_LEVEL=0
1014 int level = team->t.t_active_level - 1; // index in array of hot teams
1015 if (master_th->th.th_teams_microtask) { // are we inside the teams?
1016 if (master_th->th.th_teams_size.nteams > 1) {
1017 ++level; // level was not increased in teams construct for
1018 // team_of_masters
1019 }
1020 if (team->t.t_pkfn != (microtask_t)__kmp_teams_master &&
1021 master_th->th.th_teams_level == team->t.t_level) {
1022 ++level; // level was not increased in teams construct for
1023 // team_of_workers before the parallel
1024 } // team->t.t_level will be increased inside parallel
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001025 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001026 if (level < __kmp_hot_teams_max_level) {
1027 if (hot_teams[level].hot_team) {
1028 // hot team has already been allocated for given level
1029 KMP_DEBUG_ASSERT(hot_teams[level].hot_team == team);
1030 use_hot_team = 1; // the team is ready to use
1031 } else {
1032 use_hot_team = 0; // AC: threads are not allocated yet
1033 hot_teams[level].hot_team = team; // remember new hot team
1034 hot_teams[level].hot_team_nth = team->t.t_nproc;
1035 }
1036 } else {
1037 use_hot_team = 0;
1038 }
1039 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001040#else
Jonathan Peyton30419822017-05-12 18:01:32 +00001041 use_hot_team = team == root->r.r_hot_team;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001042#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001043 if (!use_hot_team) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001044
Jonathan Peyton30419822017-05-12 18:01:32 +00001045 /* install the master thread */
1046 team->t.t_threads[0] = master_th;
1047 __kmp_initialize_info(master_th, team, 0, master_gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001048
Jonathan Peyton30419822017-05-12 18:01:32 +00001049 /* now, install the worker threads */
1050 for (i = 1; i < team->t.t_nproc; i++) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001051
Jonathan Peyton30419822017-05-12 18:01:32 +00001052 /* fork or reallocate a new thread and install it in team */
1053 kmp_info_t *thr = __kmp_allocate_thread(root, team, i);
1054 team->t.t_threads[i] = thr;
1055 KMP_DEBUG_ASSERT(thr);
1056 KMP_DEBUG_ASSERT(thr->th.th_team == team);
1057 /* align team and thread arrived states */
1058 KA_TRACE(20, ("__kmp_fork_team_threads: T#%d(%d:%d) init arrived "
1059 "T#%d(%d:%d) join =%llu, plain=%llu\n",
1060 __kmp_gtid_from_tid(0, team), team->t.t_id, 0,
1061 __kmp_gtid_from_tid(i, team), team->t.t_id, i,
1062 team->t.t_bar[bs_forkjoin_barrier].b_arrived,
1063 team->t.t_bar[bs_plain_barrier].b_arrived));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001064#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00001065 thr->th.th_teams_microtask = master_th->th.th_teams_microtask;
1066 thr->th.th_teams_level = master_th->th.th_teams_level;
1067 thr->th.th_teams_size = master_th->th.th_teams_size;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001068#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001069 { // Initialize threads' barrier data.
1070 int b;
1071 kmp_balign_t *balign = team->t.t_threads[i]->th.th_bar;
1072 for (b = 0; b < bs_last_barrier; ++b) {
1073 balign[b].bb.b_arrived = team->t.t_bar[b].b_arrived;
1074 KMP_DEBUG_ASSERT(balign[b].bb.wait_flag != KMP_BARRIER_PARENT_FLAG);
Jonathan Peyton8fbb49a2015-07-09 18:16:58 +00001075#if USE_DEBUGGER
Jonathan Peyton30419822017-05-12 18:01:32 +00001076 balign[b].bb.b_worker_arrived = team->t.t_bar[b].b_team_arrived;
Jonathan Peyton8fbb49a2015-07-09 18:16:58 +00001077#endif
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001078 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001079 }
1080 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001081
Alp Toker98758b02014-03-02 04:12:06 +00001082#if OMP_40_ENABLED && KMP_AFFINITY_SUPPORTED
Jonathan Peyton30419822017-05-12 18:01:32 +00001083 __kmp_partition_places(team);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001084#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001085 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001086
Jonathan Peyton30419822017-05-12 18:01:32 +00001087 KMP_MB();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001088}
1089
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001090#if KMP_ARCH_X86 || KMP_ARCH_X86_64
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001091// Propagate any changes to the floating point control registers out to the team
Jonathan Peyton30419822017-05-12 18:01:32 +00001092// We try to avoid unnecessary writes to the relevant cache line in the team
1093// structure, so we don't make changes unless they are needed.
1094inline static void propagateFPControl(kmp_team_t *team) {
1095 if (__kmp_inherit_fp_control) {
1096 kmp_int16 x87_fpu_control_word;
1097 kmp_uint32 mxcsr;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001098
Jonathan Peyton30419822017-05-12 18:01:32 +00001099 // Get master values of FPU control flags (both X87 and vector)
1100 __kmp_store_x87_fpu_control_word(&x87_fpu_control_word);
1101 __kmp_store_mxcsr(&mxcsr);
1102 mxcsr &= KMP_X86_MXCSR_MASK;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001103
Jonathan Peyton94a114f2017-10-20 19:30:57 +00001104 // There is no point looking at t_fp_control_saved here.
1105 // If it is TRUE, we still have to update the values if they are different
Jonas Hahnfeldf0a1c652017-11-03 18:28:19 +00001106 // from those we now have. If it is FALSE we didn't save anything yet, but
1107 // our objective is the same. We have to ensure that the values in the team
1108 // are the same as those we have.
Jonathan Peyton94a114f2017-10-20 19:30:57 +00001109 // So, this code achieves what we need whether or not t_fp_control_saved is
1110 // true. By checking whether the value needs updating we avoid unnecessary
1111 // writes that would put the cache-line into a written state, causing all
1112 // threads in the team to have to read it again.
Jonathan Peyton30419822017-05-12 18:01:32 +00001113 KMP_CHECK_UPDATE(team->t.t_x87_fpu_control_word, x87_fpu_control_word);
1114 KMP_CHECK_UPDATE(team->t.t_mxcsr, mxcsr);
1115 // Although we don't use this value, other code in the runtime wants to know
1116 // whether it should restore them. So we must ensure it is correct.
1117 KMP_CHECK_UPDATE(team->t.t_fp_control_saved, TRUE);
1118 } else {
1119 // Similarly here. Don't write to this cache-line in the team structure
1120 // unless we have to.
1121 KMP_CHECK_UPDATE(team->t.t_fp_control_saved, FALSE);
1122 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001123}
1124
Jonathan Peyton30419822017-05-12 18:01:32 +00001125// Do the opposite, setting the hardware registers to the updated values from
1126// the team.
1127inline static void updateHWFPControl(kmp_team_t *team) {
1128 if (__kmp_inherit_fp_control && team->t.t_fp_control_saved) {
1129 // Only reset the fp control regs if they have been changed in the team.
1130 // the parallel region that we are exiting.
1131 kmp_int16 x87_fpu_control_word;
1132 kmp_uint32 mxcsr;
1133 __kmp_store_x87_fpu_control_word(&x87_fpu_control_word);
1134 __kmp_store_mxcsr(&mxcsr);
1135 mxcsr &= KMP_X86_MXCSR_MASK;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001136
Jonathan Peyton30419822017-05-12 18:01:32 +00001137 if (team->t.t_x87_fpu_control_word != x87_fpu_control_word) {
1138 __kmp_clear_x87_fpu_status_word();
1139 __kmp_load_x87_fpu_control_word(&team->t.t_x87_fpu_control_word);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001140 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001141
1142 if (team->t.t_mxcsr != mxcsr) {
1143 __kmp_load_mxcsr(&team->t.t_mxcsr);
1144 }
1145 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001146}
1147#else
Jonathan Peyton30419822017-05-12 18:01:32 +00001148#define propagateFPControl(x) ((void)0)
1149#define updateHWFPControl(x) ((void)0)
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001150#endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
1151
Jonathan Peyton30419822017-05-12 18:01:32 +00001152static void __kmp_alloc_argv_entries(int argc, kmp_team_t *team,
1153 int realloc); // forward declaration
Jim Cownie5e8470a2013-09-27 10:38:44 +00001154
Jonathan Peyton30419822017-05-12 18:01:32 +00001155/* Run a parallel region that has been serialized, so runs only in a team of the
1156 single master thread. */
1157void __kmp_serialized_parallel(ident_t *loc, kmp_int32 global_tid) {
1158 kmp_info_t *this_thr;
1159 kmp_team_t *serial_team;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001160
Jonathan Peyton30419822017-05-12 18:01:32 +00001161 KC_TRACE(10, ("__kmpc_serialized_parallel: called by T#%d\n", global_tid));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001162
Jonathan Peyton30419822017-05-12 18:01:32 +00001163 /* Skip all this code for autopar serialized loops since it results in
1164 unacceptable overhead */
1165 if (loc != NULL && (loc->flags & KMP_IDENT_AUTOPAR))
1166 return;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001167
Jonathan Peyton30419822017-05-12 18:01:32 +00001168 if (!TCR_4(__kmp_init_parallel))
1169 __kmp_parallel_initialize();
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001170
Jonathan Peyton30419822017-05-12 18:01:32 +00001171 this_thr = __kmp_threads[global_tid];
1172 serial_team = this_thr->th.th_serial_team;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001173
Jonathan Peyton30419822017-05-12 18:01:32 +00001174 /* utilize the serialized team held by this thread */
1175 KMP_DEBUG_ASSERT(serial_team);
1176 KMP_MB();
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001177
Jonathan Peyton30419822017-05-12 18:01:32 +00001178 if (__kmp_tasking_mode != tskm_immediate_exec) {
1179 KMP_DEBUG_ASSERT(
1180 this_thr->th.th_task_team ==
1181 this_thr->th.th_team->t.t_task_team[this_thr->th.th_task_state]);
1182 KMP_DEBUG_ASSERT(serial_team->t.t_task_team[this_thr->th.th_task_state] ==
1183 NULL);
1184 KA_TRACE(20, ("__kmpc_serialized_parallel: T#%d pushing task_team %p / "
1185 "team %p, new task_team = NULL\n",
1186 global_tid, this_thr->th.th_task_team, this_thr->th.th_team));
1187 this_thr->th.th_task_team = NULL;
1188 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001189
1190#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00001191 kmp_proc_bind_t proc_bind = this_thr->th.th_set_proc_bind;
1192 if (this_thr->th.th_current_task->td_icvs.proc_bind == proc_bind_false) {
1193 proc_bind = proc_bind_false;
1194 } else if (proc_bind == proc_bind_default) {
1195 // No proc_bind clause was specified, so use the current value
1196 // of proc-bind-var for this parallel region.
1197 proc_bind = this_thr->th.th_current_task->td_icvs.proc_bind;
1198 }
1199 // Reset for next parallel region
1200 this_thr->th.th_set_proc_bind = proc_bind_default;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001201#endif /* OMP_40_ENABLED */
1202
Joachim Protze82e94a52017-11-01 10:08:30 +00001203#if OMPT_SUPPORT
1204 ompt_data_t ompt_parallel_data;
1205 ompt_parallel_data.ptr = NULL;
1206 ompt_data_t *implicit_task_data;
1207 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(global_tid);
1208 if (ompt_enabled.enabled &&
1209 this_thr->th.ompt_thread_info.state != omp_state_overhead) {
1210
1211 ompt_task_info_t *parent_task_info;
1212 parent_task_info = OMPT_CUR_TASK_INFO(this_thr);
1213
Joachim Protzec255ca72017-11-05 14:11:10 +00001214 parent_task_info->frame.enter_frame = OMPT_GET_FRAME_ADDRESS(1);
Joachim Protze82e94a52017-11-01 10:08:30 +00001215 if (ompt_enabled.ompt_callback_parallel_begin) {
1216 int team_size = 1;
1217
1218 ompt_callbacks.ompt_callback(ompt_callback_parallel_begin)(
1219 &(parent_task_info->task_data), &(parent_task_info->frame),
1220 &ompt_parallel_data, team_size, ompt_invoker_program, codeptr);
1221 }
1222 }
1223#endif // OMPT_SUPPORT
1224
Jonathan Peyton30419822017-05-12 18:01:32 +00001225 if (this_thr->th.th_team != serial_team) {
1226 // Nested level will be an index in the nested nthreads array
1227 int level = this_thr->th.th_team->t.t_level;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001228
Jonathan Peyton30419822017-05-12 18:01:32 +00001229 if (serial_team->t.t_serialized) {
1230 /* this serial team was already used
1231 TODO increase performance by making this locks more specific */
1232 kmp_team_t *new_team;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001233
Jonathan Peyton30419822017-05-12 18:01:32 +00001234 __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001235
Jonathan Peyton30419822017-05-12 18:01:32 +00001236 new_team = __kmp_allocate_team(this_thr->th.th_root, 1, 1,
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001237#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00001238 ompt_parallel_data,
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001239#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001240#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00001241 proc_bind,
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001242#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001243 &this_thr->th.th_current_task->td_icvs,
1244 0 USE_NESTED_HOT_ARG(NULL));
1245 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
1246 KMP_ASSERT(new_team);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001247
Jonathan Peyton30419822017-05-12 18:01:32 +00001248 /* setup new serialized team and install it */
1249 new_team->t.t_threads[0] = this_thr;
1250 new_team->t.t_parent = this_thr->th.th_team;
1251 serial_team = new_team;
1252 this_thr->th.th_serial_team = serial_team;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001253
Jonathan Peyton30419822017-05-12 18:01:32 +00001254 KF_TRACE(
1255 10,
1256 ("__kmpc_serialized_parallel: T#%d allocated new serial team %p\n",
1257 global_tid, serial_team));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001258
Jonathan Peyton30419822017-05-12 18:01:32 +00001259 /* TODO the above breaks the requirement that if we run out of resources,
1260 then we can still guarantee that serialized teams are ok, since we may
1261 need to allocate a new one */
1262 } else {
1263 KF_TRACE(
1264 10,
1265 ("__kmpc_serialized_parallel: T#%d reusing cached serial team %p\n",
1266 global_tid, serial_team));
1267 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001268
Jonathan Peyton30419822017-05-12 18:01:32 +00001269 /* we have to initialize this serial team */
1270 KMP_DEBUG_ASSERT(serial_team->t.t_threads);
1271 KMP_DEBUG_ASSERT(serial_team->t.t_threads[0] == this_thr);
1272 KMP_DEBUG_ASSERT(this_thr->th.th_team != serial_team);
1273 serial_team->t.t_ident = loc;
1274 serial_team->t.t_serialized = 1;
1275 serial_team->t.t_nproc = 1;
1276 serial_team->t.t_parent = this_thr->th.th_team;
Jonathan Peytonba55a7b2017-11-29 22:47:52 +00001277 serial_team->t.t_sched.sched = this_thr->th.th_team->t.t_sched.sched;
Jonathan Peyton30419822017-05-12 18:01:32 +00001278 this_thr->th.th_team = serial_team;
1279 serial_team->t.t_master_tid = this_thr->th.th_info.ds.ds_tid;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001280
Jonathan Peyton30419822017-05-12 18:01:32 +00001281 KF_TRACE(10, ("__kmpc_serialized_parallel: T#d curtask=%p\n", global_tid,
1282 this_thr->th.th_current_task));
1283 KMP_ASSERT(this_thr->th.th_current_task->td_flags.executing == 1);
1284 this_thr->th.th_current_task->td_flags.executing = 0;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001285
Jonathan Peyton30419822017-05-12 18:01:32 +00001286 __kmp_push_current_task_to_thread(this_thr, serial_team, 0);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001287
Jonathan Peyton30419822017-05-12 18:01:32 +00001288 /* TODO: GEH: do ICVs work for nested serialized teams? Don't we need an
1289 implicit task for each serialized task represented by
1290 team->t.t_serialized? */
1291 copy_icvs(&this_thr->th.th_current_task->td_icvs,
1292 &this_thr->th.th_current_task->td_parent->td_icvs);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001293
Jonathan Peyton30419822017-05-12 18:01:32 +00001294 // Thread value exists in the nested nthreads array for the next nested
1295 // level
1296 if (__kmp_nested_nth.used && (level + 1 < __kmp_nested_nth.used)) {
1297 this_thr->th.th_current_task->td_icvs.nproc =
1298 __kmp_nested_nth.nth[level + 1];
1299 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001300
1301#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00001302 if (__kmp_nested_proc_bind.used &&
1303 (level + 1 < __kmp_nested_proc_bind.used)) {
1304 this_thr->th.th_current_task->td_icvs.proc_bind =
1305 __kmp_nested_proc_bind.bind_types[level + 1];
1306 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001307#endif /* OMP_40_ENABLED */
1308
Jonathan Peyton8fbb49a2015-07-09 18:16:58 +00001309#if USE_DEBUGGER
Jonathan Peyton30419822017-05-12 18:01:32 +00001310 serial_team->t.t_pkfn = (microtask_t)(~0); // For the debugger.
Jonathan Peyton8fbb49a2015-07-09 18:16:58 +00001311#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001312 this_thr->th.th_info.ds.ds_tid = 0;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001313
Jonathan Peyton30419822017-05-12 18:01:32 +00001314 /* set thread cache values */
1315 this_thr->th.th_team_nproc = 1;
1316 this_thr->th.th_team_master = this_thr;
1317 this_thr->th.th_team_serialized = 1;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001318
Jonathan Peyton30419822017-05-12 18:01:32 +00001319 serial_team->t.t_level = serial_team->t.t_parent->t.t_level + 1;
1320 serial_team->t.t_active_level = serial_team->t.t_parent->t.t_active_level;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001321
Jonathan Peyton30419822017-05-12 18:01:32 +00001322 propagateFPControl(serial_team);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001323
Jonathan Peyton30419822017-05-12 18:01:32 +00001324 /* check if we need to allocate dispatch buffers stack */
1325 KMP_DEBUG_ASSERT(serial_team->t.t_dispatch);
1326 if (!serial_team->t.t_dispatch->th_disp_buffer) {
1327 serial_team->t.t_dispatch->th_disp_buffer =
1328 (dispatch_private_info_t *)__kmp_allocate(
1329 sizeof(dispatch_private_info_t));
1330 }
1331 this_thr->th.th_dispatch = serial_team->t.t_dispatch;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001332
Jonathan Peyton30419822017-05-12 18:01:32 +00001333 KMP_MB();
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001334
Jonathan Peyton30419822017-05-12 18:01:32 +00001335 } else {
1336 /* this serialized team is already being used,
1337 * that's fine, just add another nested level */
1338 KMP_DEBUG_ASSERT(this_thr->th.th_team == serial_team);
1339 KMP_DEBUG_ASSERT(serial_team->t.t_threads);
1340 KMP_DEBUG_ASSERT(serial_team->t.t_threads[0] == this_thr);
1341 ++serial_team->t.t_serialized;
1342 this_thr->th.th_team_serialized = serial_team->t.t_serialized;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001343
Jonathan Peyton30419822017-05-12 18:01:32 +00001344 // Nested level will be an index in the nested nthreads array
1345 int level = this_thr->th.th_team->t.t_level;
1346 // Thread value exists in the nested nthreads array for the next nested
1347 // level
1348 if (__kmp_nested_nth.used && (level + 1 < __kmp_nested_nth.used)) {
1349 this_thr->th.th_current_task->td_icvs.nproc =
1350 __kmp_nested_nth.nth[level + 1];
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001351 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001352 serial_team->t.t_level++;
1353 KF_TRACE(10, ("__kmpc_serialized_parallel: T#%d increasing nesting level "
1354 "of serial team %p to %d\n",
1355 global_tid, serial_team, serial_team->t.t_level));
1356
1357 /* allocate/push dispatch buffers stack */
1358 KMP_DEBUG_ASSERT(serial_team->t.t_dispatch);
1359 {
1360 dispatch_private_info_t *disp_buffer =
1361 (dispatch_private_info_t *)__kmp_allocate(
1362 sizeof(dispatch_private_info_t));
1363 disp_buffer->next = serial_team->t.t_dispatch->th_disp_buffer;
1364 serial_team->t.t_dispatch->th_disp_buffer = disp_buffer;
1365 }
1366 this_thr->th.th_dispatch = serial_team->t.t_dispatch;
1367
1368 KMP_MB();
1369 }
Olga Malyshevadbdcfa12017-04-04 13:56:50 +00001370#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00001371 KMP_CHECK_UPDATE(serial_team->t.t_cancel_request, cancel_noreq);
Olga Malyshevadbdcfa12017-04-04 13:56:50 +00001372#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001373
Jonathan Peyton30419822017-05-12 18:01:32 +00001374 if (__kmp_env_consistency_check)
1375 __kmp_push_parallel(global_tid, NULL);
Joachim Protze82e94a52017-11-01 10:08:30 +00001376#if OMPT_SUPPORT
1377 serial_team->t.ompt_team_info.master_return_address = codeptr;
1378 if (ompt_enabled.enabled &&
1379 this_thr->th.ompt_thread_info.state != omp_state_overhead) {
Joachim Protzec255ca72017-11-05 14:11:10 +00001380 OMPT_CUR_TASK_INFO(this_thr)->frame.exit_frame = OMPT_GET_FRAME_ADDRESS(1);
Joachim Protze82e94a52017-11-01 10:08:30 +00001381
1382 ompt_lw_taskteam_t lw_taskteam;
1383 __ompt_lw_taskteam_init(&lw_taskteam, this_thr, global_tid,
1384 &ompt_parallel_data, codeptr);
1385
1386 __ompt_lw_taskteam_link(&lw_taskteam, this_thr, 1);
1387 // don't use lw_taskteam after linking. content was swaped
1388
1389 /* OMPT implicit task begin */
1390 implicit_task_data = OMPT_CUR_TASK_DATA(this_thr);
1391 if (ompt_enabled.ompt_callback_implicit_task) {
1392 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
1393 ompt_scope_begin, OMPT_CUR_TEAM_DATA(this_thr),
1394 OMPT_CUR_TASK_DATA(this_thr), 1, __kmp_tid_from_gtid(global_tid));
Joachim Protze9be9cf22018-05-07 12:42:21 +00001395 OMPT_CUR_TASK_INFO(this_thr)
1396 ->thread_num = __kmp_tid_from_gtid(global_tid);
Joachim Protze82e94a52017-11-01 10:08:30 +00001397 }
1398
1399 /* OMPT state */
1400 this_thr->th.ompt_thread_info.state = omp_state_work_parallel;
Joachim Protzec255ca72017-11-05 14:11:10 +00001401 OMPT_CUR_TASK_INFO(this_thr)->frame.exit_frame = OMPT_GET_FRAME_ADDRESS(1);
Joachim Protze82e94a52017-11-01 10:08:30 +00001402 }
1403#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001404}
Jim Cownie181b4bb2013-12-23 17:28:57 +00001405
Jim Cownie5e8470a2013-09-27 10:38:44 +00001406/* most of the work for a fork */
1407/* return true if we really went parallel, false if serialized */
Jonathan Peyton30419822017-05-12 18:01:32 +00001408int __kmp_fork_call(ident_t *loc, int gtid,
1409 enum fork_context_e call_context, // Intel, GNU, ...
Joachim Protze82e94a52017-11-01 10:08:30 +00001410 kmp_int32 argc, microtask_t microtask, launch_t invoker,
Jim Cownie5e8470a2013-09-27 10:38:44 +00001411/* TODO: revert workaround for Intel(R) 64 tracker #96 */
Andrey Churbanovcbda8682015-01-13 14:43:35 +00001412#if (KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64) && KMP_OS_LINUX
Jonathan Peyton30419822017-05-12 18:01:32 +00001413 va_list *ap
Jim Cownie5e8470a2013-09-27 10:38:44 +00001414#else
Jonathan Peyton30419822017-05-12 18:01:32 +00001415 va_list ap
Jim Cownie5e8470a2013-09-27 10:38:44 +00001416#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001417 ) {
1418 void **argv;
1419 int i;
1420 int master_tid;
1421 int master_this_cons;
1422 kmp_team_t *team;
1423 kmp_team_t *parent_team;
1424 kmp_info_t *master_th;
1425 kmp_root_t *root;
1426 int nthreads;
1427 int master_active;
1428 int master_set_numthreads;
1429 int level;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001430#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00001431 int active_level;
1432 int teams_level;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001433#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001434#if KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00001435 kmp_hot_team_ptr_t **p_hot_teams;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001436#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001437 { // KMP_TIME_BLOCK
Jonathan Peyton5375fe82016-11-14 21:13:44 +00001438 KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(KMP_fork_call);
Jonathan Peyton45be4502015-08-11 21:36:41 +00001439 KMP_COUNT_VALUE(OMP_PARALLEL_args, argc);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001440
Jonathan Peyton30419822017-05-12 18:01:32 +00001441 KA_TRACE(20, ("__kmp_fork_call: enter T#%d\n", gtid));
1442 if (__kmp_stkpadding > 0 && __kmp_root[gtid] != NULL) {
1443 /* Some systems prefer the stack for the root thread(s) to start with */
1444 /* some gap from the parent stack to prevent false sharing. */
1445 void *dummy = KMP_ALLOCA(__kmp_stkpadding);
1446 /* These 2 lines below are so this does not get optimized out */
1447 if (__kmp_stkpadding > KMP_MAX_STKPADDING)
1448 __kmp_stkpadding += (short)((kmp_int64)dummy);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001449 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001450
1451 /* initialize if needed */
Jonathan Peyton30419822017-05-12 18:01:32 +00001452 KMP_DEBUG_ASSERT(
1453 __kmp_init_serial); // AC: potentially unsafe, not in sync with shutdown
1454 if (!TCR_4(__kmp_init_parallel))
1455 __kmp_parallel_initialize();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001456
1457 /* setup current data */
Jonathan Peyton30419822017-05-12 18:01:32 +00001458 master_th = __kmp_threads[gtid]; // AC: potentially unsafe, not in sync with
1459 // shutdown
1460 parent_team = master_th->th.th_team;
1461 master_tid = master_th->th.th_info.ds.ds_tid;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001462 master_this_cons = master_th->th.th_local.this_construct;
Jonathan Peyton30419822017-05-12 18:01:32 +00001463 root = master_th->th.th_root;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001464 master_active = root->r.r_active;
1465 master_set_numthreads = master_th->th.th_set_nproc;
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001466
1467#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00001468 ompt_data_t ompt_parallel_data;
1469 ompt_parallel_data.ptr = NULL;
1470 ompt_data_t *parent_task_data;
Joachim Protzec5836064b2018-05-28 08:14:58 +00001471 omp_frame_t *ompt_frame;
Joachim Protze82e94a52017-11-01 10:08:30 +00001472 ompt_data_t *implicit_task_data;
1473 void *return_address = NULL;
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001474
Joachim Protze82e94a52017-11-01 10:08:30 +00001475 if (ompt_enabled.enabled) {
1476 __ompt_get_task_info_internal(0, NULL, &parent_task_data, &ompt_frame,
1477 NULL, NULL);
1478 return_address = OMPT_LOAD_RETURN_ADDRESS(gtid);
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001479 }
1480#endif
1481
Jim Cownie5e8470a2013-09-27 10:38:44 +00001482 // Nested level will be an index in the nested nthreads array
Jonathan Peyton30419822017-05-12 18:01:32 +00001483 level = parent_team->t.t_level;
1484 // used to launch non-serial teams even if nested is not allowed
1485 active_level = parent_team->t.t_active_level;
Jonathan Peytonc76f9f02016-06-21 19:12:07 +00001486#if OMP_40_ENABLED
Jonathan Peyton642688b2017-06-01 16:46:36 +00001487 // needed to check nesting inside the teams
1488 teams_level = master_th->th.th_teams_level;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001489#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001490#if KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00001491 p_hot_teams = &master_th->th.th_hot_teams;
1492 if (*p_hot_teams == NULL && __kmp_hot_teams_max_level > 0) {
1493 *p_hot_teams = (kmp_hot_team_ptr_t *)__kmp_allocate(
1494 sizeof(kmp_hot_team_ptr_t) * __kmp_hot_teams_max_level);
1495 (*p_hot_teams)[0].hot_team = root->r.r_hot_team;
Jonathan Peyton642688b2017-06-01 16:46:36 +00001496 // it is either actual or not needed (when active_level > 0)
1497 (*p_hot_teams)[0].hot_team_nth = 1;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001498 }
1499#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001500
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001501#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00001502 if (ompt_enabled.enabled) {
1503 if (ompt_enabled.ompt_callback_parallel_begin) {
1504 int team_size = master_set_numthreads
1505 ? master_set_numthreads
1506 : get__nproc_2(parent_team, master_tid);
1507 ompt_callbacks.ompt_callback(ompt_callback_parallel_begin)(
1508 parent_task_data, ompt_frame, &ompt_parallel_data, team_size,
1509 OMPT_INVOKER(call_context), return_address);
1510 }
1511 master_th->th.ompt_thread_info.state = omp_state_overhead;
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001512 }
1513#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001514
Jim Cownie5e8470a2013-09-27 10:38:44 +00001515 master_th->th.th_ident = loc;
1516
1517#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00001518 if (master_th->th.th_teams_microtask && ap &&
1519 microtask != (microtask_t)__kmp_teams_master && level == teams_level) {
1520 // AC: This is start of parallel that is nested inside teams construct.
1521 // The team is actual (hot), all workers are ready at the fork barrier.
1522 // No lock needed to initialize the team a bit, then free workers.
1523 parent_team->t.t_ident = loc;
1524 __kmp_alloc_argv_entries(argc, parent_team, TRUE);
1525 parent_team->t.t_argc = argc;
1526 argv = (void **)parent_team->t.t_argv;
1527 for (i = argc - 1; i >= 0; --i)
Jim Cownie5e8470a2013-09-27 10:38:44 +00001528/* TODO: revert workaround for Intel(R) 64 tracker #96 */
Andrey Churbanovcbda8682015-01-13 14:43:35 +00001529#if (KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64) && KMP_OS_LINUX
Jonathan Peyton30419822017-05-12 18:01:32 +00001530 *argv++ = va_arg(*ap, void *);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001531#else
Jonathan Peyton30419822017-05-12 18:01:32 +00001532 *argv++ = va_arg(ap, void *);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001533#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001534 // Increment our nested depth levels, but not increase the serialization
1535 if (parent_team == master_th->th.th_serial_team) {
1536 // AC: we are in serialized parallel
1537 __kmpc_serialized_parallel(loc, gtid);
1538 KMP_DEBUG_ASSERT(parent_team->t.t_serialized > 1);
1539 // AC: need this in order enquiry functions work
1540 // correctly, will restore at join time
1541 parent_team->t.t_serialized--;
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001542#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00001543 void *dummy;
1544 void **exit_runtime_p;
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001545
Jonathan Peyton30419822017-05-12 18:01:32 +00001546 ompt_lw_taskteam_t lw_taskteam;
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001547
Joachim Protze82e94a52017-11-01 10:08:30 +00001548 if (ompt_enabled.enabled) {
1549 __ompt_lw_taskteam_init(&lw_taskteam, master_th, gtid,
1550 &ompt_parallel_data, return_address);
Joachim Protzec255ca72017-11-05 14:11:10 +00001551 exit_runtime_p = &(lw_taskteam.ompt_task_info.frame.exit_frame);
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001552
Joachim Protze82e94a52017-11-01 10:08:30 +00001553 __ompt_lw_taskteam_link(&lw_taskteam, master_th, 0);
1554 // don't use lw_taskteam after linking. content was swaped
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001555
Jonathan Peyton30419822017-05-12 18:01:32 +00001556 /* OMPT implicit task begin */
Joachim Protze82e94a52017-11-01 10:08:30 +00001557 implicit_task_data = OMPT_CUR_TASK_DATA(master_th);
1558 if (ompt_enabled.ompt_callback_implicit_task) {
1559 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
1560 ompt_scope_begin, OMPT_CUR_TEAM_DATA(master_th),
1561 implicit_task_data, 1, __kmp_tid_from_gtid(gtid));
Joachim Protze9be9cf22018-05-07 12:42:21 +00001562 OMPT_CUR_TASK_INFO(master_th)
1563 ->thread_num = __kmp_tid_from_gtid(gtid);
Jonathan Peyton30419822017-05-12 18:01:32 +00001564 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001565
Jonathan Peyton30419822017-05-12 18:01:32 +00001566 /* OMPT state */
Joachim Protze82e94a52017-11-01 10:08:30 +00001567 master_th->th.ompt_thread_info.state = omp_state_work_parallel;
Jonathan Peyton30419822017-05-12 18:01:32 +00001568 } else {
1569 exit_runtime_p = &dummy;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001570 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001571#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001572
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001573 {
Jonathan Peyton30419822017-05-12 18:01:32 +00001574 KMP_TIME_PARTITIONED_BLOCK(OMP_parallel);
1575 KMP_SET_THREAD_STATE_BLOCK(IMPLICIT_TASK);
1576 __kmp_invoke_microtask(microtask, gtid, 0, argc, parent_team->t.t_argv
1577#if OMPT_SUPPORT
1578 ,
1579 exit_runtime_p
1580#endif
1581 );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001582 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001583
Jonathan Peyton30419822017-05-12 18:01:32 +00001584#if OMPT_SUPPORT
1585 *exit_runtime_p = NULL;
Joachim Protze82e94a52017-11-01 10:08:30 +00001586 if (ompt_enabled.enabled) {
Joachim Protzec255ca72017-11-05 14:11:10 +00001587 OMPT_CUR_TASK_INFO(master_th)->frame.exit_frame = NULL;
Joachim Protze82e94a52017-11-01 10:08:30 +00001588 if (ompt_enabled.ompt_callback_implicit_task) {
1589 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
1590 ompt_scope_end, NULL, implicit_task_data, 1,
Joachim Protze9be9cf22018-05-07 12:42:21 +00001591 OMPT_CUR_TASK_INFO(master_th)->thread_num);
Jonathan Peyton30419822017-05-12 18:01:32 +00001592 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001593 __ompt_lw_taskteam_unlink(master_th);
Jonathan Peyton30419822017-05-12 18:01:32 +00001594
Joachim Protze82e94a52017-11-01 10:08:30 +00001595 if (ompt_enabled.ompt_callback_parallel_end) {
1596 ompt_callbacks.ompt_callback(ompt_callback_parallel_end)(
1597 OMPT_CUR_TEAM_DATA(master_th), OMPT_CUR_TASK_DATA(master_th),
1598 OMPT_INVOKER(call_context), return_address);
Jonathan Peyton30419822017-05-12 18:01:32 +00001599 }
Joachim Protze82e94a52017-11-01 10:08:30 +00001600 master_th->th.ompt_thread_info.state = omp_state_overhead;
Jonathan Peyton30419822017-05-12 18:01:32 +00001601 }
1602#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001603 return TRUE;
Jonathan Peyton30419822017-05-12 18:01:32 +00001604 }
1605
1606 parent_team->t.t_pkfn = microtask;
Jonathan Peyton30419822017-05-12 18:01:32 +00001607 parent_team->t.t_invoke = invoker;
Jonathan Peyton37e2ef52018-07-09 17:36:22 +00001608 KMP_ATOMIC_INC(&root->r.r_in_parallel);
Jonathan Peyton30419822017-05-12 18:01:32 +00001609 parent_team->t.t_active_level++;
1610 parent_team->t.t_level++;
1611
1612 /* Change number of threads in the team if requested */
1613 if (master_set_numthreads) { // The parallel has num_threads clause
1614 if (master_set_numthreads < master_th->th.th_teams_size.nth) {
1615 // AC: only can reduce number of threads dynamically, can't increase
1616 kmp_info_t **other_threads = parent_team->t.t_threads;
1617 parent_team->t.t_nproc = master_set_numthreads;
1618 for (i = 0; i < master_set_numthreads; ++i) {
1619 other_threads[i]->th.th_team_nproc = master_set_numthreads;
1620 }
1621 // Keep extra threads hot in the team for possible next parallels
1622 }
1623 master_th->th.th_set_nproc = 0;
1624 }
1625
1626#if USE_DEBUGGER
1627 if (__kmp_debugging) { // Let debugger override number of threads.
1628 int nth = __kmp_omp_num_threads(loc);
Jonathan Peyton642688b2017-06-01 16:46:36 +00001629 if (nth > 0) { // 0 means debugger doesn't want to change num threads
Jonathan Peyton30419822017-05-12 18:01:32 +00001630 master_set_numthreads = nth;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001631 }
1632 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001633#endif
1634
1635 KF_TRACE(10, ("__kmp_fork_call: before internal fork: root=%p, team=%p, "
1636 "master_th=%p, gtid=%d\n",
1637 root, parent_team, master_th, gtid));
1638 __kmp_internal_fork(loc, gtid, parent_team);
1639 KF_TRACE(10, ("__kmp_fork_call: after internal fork: root=%p, team=%p, "
1640 "master_th=%p, gtid=%d\n",
1641 root, parent_team, master_th, gtid));
1642
1643 /* Invoke microtask for MASTER thread */
1644 KA_TRACE(20, ("__kmp_fork_call: T#%d(%d:0) invoke microtask = %p\n", gtid,
1645 parent_team->t.t_id, parent_team->t.t_pkfn));
1646
1647 {
1648 KMP_TIME_PARTITIONED_BLOCK(OMP_parallel);
1649 KMP_SET_THREAD_STATE_BLOCK(IMPLICIT_TASK);
1650 if (!parent_team->t.t_invoke(gtid)) {
1651 KMP_ASSERT2(0, "cannot invoke microtask for MASTER thread");
1652 }
1653 }
1654 KA_TRACE(20, ("__kmp_fork_call: T#%d(%d:0) done microtask = %p\n", gtid,
1655 parent_team->t.t_id, parent_team->t.t_pkfn));
1656 KMP_MB(); /* Flush all pending memory write invalidates. */
1657
1658 KA_TRACE(20, ("__kmp_fork_call: parallel exit T#%d\n", gtid));
1659
1660 return TRUE;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001661 } // Parallel closely nested in teams construct
Jim Cownie5e8470a2013-09-27 10:38:44 +00001662#endif /* OMP_40_ENABLED */
1663
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001664#if KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00001665 if (__kmp_tasking_mode != tskm_immediate_exec) {
1666 KMP_DEBUG_ASSERT(master_th->th.th_task_team ==
1667 parent_team->t.t_task_team[master_th->th.th_task_state]);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001668 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001669#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001670
Jonathan Peyton30419822017-05-12 18:01:32 +00001671 if (parent_team->t.t_active_level >=
1672 master_th->th.th_current_task->td_icvs.max_active_levels) {
1673 nthreads = 1;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001674 } else {
Andrey Churbanov92effc42015-08-18 10:08:27 +00001675#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00001676 int enter_teams = ((ap == NULL && active_level == 0) ||
1677 (ap && teams_level > 0 && teams_level == level));
Andrey Churbanov92effc42015-08-18 10:08:27 +00001678#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001679 nthreads =
1680 master_set_numthreads
1681 ? master_set_numthreads
1682 : get__nproc_2(
1683 parent_team,
1684 master_tid); // TODO: get nproc directly from current task
Andrey Churbanov92effc42015-08-18 10:08:27 +00001685
Jonathan Peyton30419822017-05-12 18:01:32 +00001686 // Check if we need to take forkjoin lock? (no need for serialized
1687 // parallel out of teams construct). This code moved here from
1688 // __kmp_reserve_threads() to speedup nested serialized parallels.
1689 if (nthreads > 1) {
1690 if ((!get__nested(master_th) && (root->r.r_in_parallel
Andrey Churbanov92effc42015-08-18 10:08:27 +00001691#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00001692 && !enter_teams
Andrey Churbanov92effc42015-08-18 10:08:27 +00001693#endif /* OMP_40_ENABLED */
Jonathan Peyton30419822017-05-12 18:01:32 +00001694 )) ||
1695 (__kmp_library == library_serial)) {
Jonathan Peyton642688b2017-06-01 16:46:36 +00001696 KC_TRACE(10, ("__kmp_fork_call: T#%d serializing team; requested %d"
1697 " threads\n",
1698 gtid, nthreads));
Jonathan Peyton30419822017-05-12 18:01:32 +00001699 nthreads = 1;
Andrey Churbanov92effc42015-08-18 10:08:27 +00001700 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001701 }
1702 if (nthreads > 1) {
1703 /* determine how many new threads we can use */
1704 __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock);
Jonathan Peyton30419822017-05-12 18:01:32 +00001705 nthreads = __kmp_reserve_threads(
1706 root, parent_team, master_tid, nthreads
Jim Cownie5e8470a2013-09-27 10:38:44 +00001707#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00001708 /* AC: If we execute teams from parallel region (on host), then
1709 teams should be created but each can only have 1 thread if
1710 nesting is disabled. If teams called from serial region, then
1711 teams and their threads should be created regardless of the
1712 nesting setting. */
1713 ,
1714 enter_teams
Jim Cownie5e8470a2013-09-27 10:38:44 +00001715#endif /* OMP_40_ENABLED */
Jonathan Peyton30419822017-05-12 18:01:32 +00001716 );
1717 if (nthreads == 1) {
1718 // Free lock for single thread execution here; for multi-thread
1719 // execution it will be freed later after team of threads created
1720 // and initialized
1721 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
Andrey Churbanov92effc42015-08-18 10:08:27 +00001722 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001723 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001724 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001725 KMP_DEBUG_ASSERT(nthreads > 0);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001726
Jonathan Peyton30419822017-05-12 18:01:32 +00001727 // If we temporarily changed the set number of threads then restore it now
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001728 master_th->th.th_set_nproc = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001729
Jim Cownie5e8470a2013-09-27 10:38:44 +00001730 /* create a serialized parallel region? */
Jonathan Peyton30419822017-05-12 18:01:32 +00001731 if (nthreads == 1) {
1732/* josh todo: hypothetical question: what do we do for OS X*? */
1733#if KMP_OS_LINUX && \
1734 (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64)
1735 void *args[argc];
Jim Cownie5e8470a2013-09-27 10:38:44 +00001736#else
Jonathan Peyton30419822017-05-12 18:01:32 +00001737 void **args = (void **)KMP_ALLOCA(argc * sizeof(void *));
1738#endif /* KMP_OS_LINUX && ( KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || \
1739 KMP_ARCH_AARCH64) */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001740
Jonathan Peyton30419822017-05-12 18:01:32 +00001741 KA_TRACE(20,
1742 ("__kmp_fork_call: T#%d serializing parallel region\n", gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001743
Jonathan Peyton30419822017-05-12 18:01:32 +00001744 __kmpc_serialized_parallel(loc, gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001745
Jonathan Peyton30419822017-05-12 18:01:32 +00001746 if (call_context == fork_context_intel) {
1747 /* TODO this sucks, use the compiler itself to pass args! :) */
1748 master_th->th.th_serial_team->t.t_ident = loc;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001749#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00001750 if (!ap) {
1751 // revert change made in __kmpc_serialized_parallel()
1752 master_th->th.th_serial_team->t.t_level--;
1753// Get args from parent team for teams construct
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001754
1755#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00001756 void *dummy;
1757 void **exit_runtime_p;
Joachim Protze82e94a52017-11-01 10:08:30 +00001758 ompt_task_info_t *task_info;
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001759
Jonathan Peyton30419822017-05-12 18:01:32 +00001760 ompt_lw_taskteam_t lw_taskteam;
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001761
Joachim Protze82e94a52017-11-01 10:08:30 +00001762 if (ompt_enabled.enabled) {
Jonathan Peyton30419822017-05-12 18:01:32 +00001763 __ompt_lw_taskteam_init(&lw_taskteam, master_th, gtid,
Joachim Protze82e94a52017-11-01 10:08:30 +00001764 &ompt_parallel_data, return_address);
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001765
Joachim Protze82e94a52017-11-01 10:08:30 +00001766 __ompt_lw_taskteam_link(&lw_taskteam, master_th, 0);
1767 // don't use lw_taskteam after linking. content was swaped
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001768
Joachim Protze82e94a52017-11-01 10:08:30 +00001769 task_info = OMPT_CUR_TASK_INFO(master_th);
Joachim Protzec255ca72017-11-05 14:11:10 +00001770 exit_runtime_p = &(task_info->frame.exit_frame);
Joachim Protze82e94a52017-11-01 10:08:30 +00001771 if (ompt_enabled.ompt_callback_implicit_task) {
1772 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
1773 ompt_scope_begin, OMPT_CUR_TEAM_DATA(master_th),
1774 &(task_info->task_data), 1, __kmp_tid_from_gtid(gtid));
Joachim Protze9be9cf22018-05-07 12:42:21 +00001775 OMPT_CUR_TASK_INFO(master_th)
1776 ->thread_num = __kmp_tid_from_gtid(gtid);
Jonathan Peyton30419822017-05-12 18:01:32 +00001777 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001778
Jonathan Peyton30419822017-05-12 18:01:32 +00001779 /* OMPT state */
Joachim Protze82e94a52017-11-01 10:08:30 +00001780 master_th->th.ompt_thread_info.state = omp_state_work_parallel;
Jonathan Peyton30419822017-05-12 18:01:32 +00001781 } else {
1782 exit_runtime_p = &dummy;
1783 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001784#endif
1785
Jonathan Peyton30419822017-05-12 18:01:32 +00001786 {
1787 KMP_TIME_PARTITIONED_BLOCK(OMP_parallel);
1788 KMP_SET_THREAD_STATE_BLOCK(IMPLICIT_TASK);
1789 __kmp_invoke_microtask(microtask, gtid, 0, argc,
1790 parent_team->t.t_argv
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001791#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00001792 ,
1793 exit_runtime_p
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001794#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001795 );
1796 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001797
1798#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00001799 if (ompt_enabled.enabled) {
1800 exit_runtime_p = NULL;
1801 if (ompt_enabled.ompt_callback_implicit_task) {
1802 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
1803 ompt_scope_end, NULL, &(task_info->task_data), 1,
Joachim Protze9be9cf22018-05-07 12:42:21 +00001804 OMPT_CUR_TASK_INFO(master_th)->thread_num);
Jonathan Peyton30419822017-05-12 18:01:32 +00001805 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001806
Jonathan Peyton30419822017-05-12 18:01:32 +00001807 __ompt_lw_taskteam_unlink(master_th);
Joachim Protze82e94a52017-11-01 10:08:30 +00001808 if (ompt_enabled.ompt_callback_parallel_end) {
1809 ompt_callbacks.ompt_callback(ompt_callback_parallel_end)(
1810 OMPT_CUR_TEAM_DATA(master_th), parent_task_data,
1811 OMPT_INVOKER(call_context), return_address);
Jonathan Peyton30419822017-05-12 18:01:32 +00001812 }
Joachim Protze82e94a52017-11-01 10:08:30 +00001813 master_th->th.ompt_thread_info.state = omp_state_overhead;
Jonathan Peyton30419822017-05-12 18:01:32 +00001814 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001815#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001816 } else if (microtask == (microtask_t)__kmp_teams_master) {
1817 KMP_DEBUG_ASSERT(master_th->th.th_team ==
1818 master_th->th.th_serial_team);
1819 team = master_th->th.th_team;
1820 // team->t.t_pkfn = microtask;
1821 team->t.t_invoke = invoker;
1822 __kmp_alloc_argv_entries(argc, team, TRUE);
1823 team->t.t_argc = argc;
1824 argv = (void **)team->t.t_argv;
1825 if (ap) {
1826 for (i = argc - 1; i >= 0; --i)
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001827// TODO: revert workaround for Intel(R) 64 tracker #96
Andrey Churbanovcbda8682015-01-13 14:43:35 +00001828#if (KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64) && KMP_OS_LINUX
Jonathan Peyton30419822017-05-12 18:01:32 +00001829 *argv++ = va_arg(*ap, void *);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001830#else
Jonathan Peyton30419822017-05-12 18:01:32 +00001831 *argv++ = va_arg(ap, void *);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001832#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001833 } else {
1834 for (i = 0; i < argc; ++i)
1835 // Get args from parent team for teams construct
1836 argv[i] = parent_team->t.t_argv[i];
1837 }
1838 // AC: revert change made in __kmpc_serialized_parallel()
1839 // because initial code in teams should have level=0
1840 team->t.t_level--;
1841 // AC: call special invoker for outer "parallel" of teams construct
1842 {
1843 KMP_TIME_PARTITIONED_BLOCK(OMP_parallel);
1844 KMP_SET_THREAD_STATE_BLOCK(IMPLICIT_TASK);
1845 invoker(gtid);
1846 }
1847 } else {
1848#endif /* OMP_40_ENABLED */
1849 argv = args;
1850 for (i = argc - 1; i >= 0; --i)
1851// TODO: revert workaround for Intel(R) 64 tracker #96
1852#if (KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64) && KMP_OS_LINUX
1853 *argv++ = va_arg(*ap, void *);
1854#else
1855 *argv++ = va_arg(ap, void *);
1856#endif
1857 KMP_MB();
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001858
1859#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00001860 void *dummy;
1861 void **exit_runtime_p;
Joachim Protze82e94a52017-11-01 10:08:30 +00001862 ompt_task_info_t *task_info;
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001863
Jonathan Peyton30419822017-05-12 18:01:32 +00001864 ompt_lw_taskteam_t lw_taskteam;
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001865
Joachim Protze82e94a52017-11-01 10:08:30 +00001866 if (ompt_enabled.enabled) {
Jonathan Peyton30419822017-05-12 18:01:32 +00001867 __ompt_lw_taskteam_init(&lw_taskteam, master_th, gtid,
Joachim Protze82e94a52017-11-01 10:08:30 +00001868 &ompt_parallel_data, return_address);
1869 __ompt_lw_taskteam_link(&lw_taskteam, master_th, 0);
1870 // don't use lw_taskteam after linking. content was swaped
1871 task_info = OMPT_CUR_TASK_INFO(master_th);
Joachim Protzec255ca72017-11-05 14:11:10 +00001872 exit_runtime_p = &(task_info->frame.exit_frame);
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001873
Jonathan Peyton30419822017-05-12 18:01:32 +00001874 /* OMPT implicit task begin */
Joachim Protze82e94a52017-11-01 10:08:30 +00001875 implicit_task_data = OMPT_CUR_TASK_DATA(master_th);
1876 if (ompt_enabled.ompt_callback_implicit_task) {
1877 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
1878 ompt_scope_begin, OMPT_CUR_TEAM_DATA(master_th),
1879 implicit_task_data, 1, __kmp_tid_from_gtid(gtid));
Joachim Protze9be9cf22018-05-07 12:42:21 +00001880 OMPT_CUR_TASK_INFO(master_th)
1881 ->thread_num = __kmp_tid_from_gtid(gtid);
Jonathan Peyton30419822017-05-12 18:01:32 +00001882 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001883
Jonathan Peyton30419822017-05-12 18:01:32 +00001884 /* OMPT state */
Joachim Protze82e94a52017-11-01 10:08:30 +00001885 master_th->th.ompt_thread_info.state = omp_state_work_parallel;
Jonathan Peyton30419822017-05-12 18:01:32 +00001886 } else {
1887 exit_runtime_p = &dummy;
1888 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001889#endif
1890
Jonathan Peyton30419822017-05-12 18:01:32 +00001891 {
1892 KMP_TIME_PARTITIONED_BLOCK(OMP_parallel);
1893 KMP_SET_THREAD_STATE_BLOCK(IMPLICIT_TASK);
1894 __kmp_invoke_microtask(microtask, gtid, 0, argc, args
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001895#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00001896 ,
1897 exit_runtime_p
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001898#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001899 );
1900 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001901
1902#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00001903 if (ompt_enabled.enabled) {
1904 *exit_runtime_p = NULL;
1905 if (ompt_enabled.ompt_callback_implicit_task) {
1906 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
1907 ompt_scope_end, NULL, &(task_info->task_data), 1,
Joachim Protze9be9cf22018-05-07 12:42:21 +00001908 OMPT_CUR_TASK_INFO(master_th)->thread_num);
Jonathan Peyton30419822017-05-12 18:01:32 +00001909 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001910
Joachim Protze82e94a52017-11-01 10:08:30 +00001911 ompt_parallel_data = *OMPT_CUR_TEAM_DATA(master_th);
Jonathan Peyton30419822017-05-12 18:01:32 +00001912 __ompt_lw_taskteam_unlink(master_th);
Joachim Protze82e94a52017-11-01 10:08:30 +00001913 if (ompt_enabled.ompt_callback_parallel_end) {
1914 ompt_callbacks.ompt_callback(ompt_callback_parallel_end)(
1915 &ompt_parallel_data, parent_task_data,
1916 OMPT_INVOKER(call_context), return_address);
Jonathan Peyton30419822017-05-12 18:01:32 +00001917 }
Joachim Protze82e94a52017-11-01 10:08:30 +00001918 master_th->th.ompt_thread_info.state = omp_state_overhead;
Jonathan Peyton30419822017-05-12 18:01:32 +00001919 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001920#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001921#if OMP_40_ENABLED
Jim Cownie5e8470a2013-09-27 10:38:44 +00001922 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001923#endif /* OMP_40_ENABLED */
1924 } else if (call_context == fork_context_gnu) {
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001925#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00001926 ompt_lw_taskteam_t lwt;
1927 __ompt_lw_taskteam_init(&lwt, master_th, gtid, &ompt_parallel_data,
1928 return_address);
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001929
Joachim Protzec255ca72017-11-05 14:11:10 +00001930 lwt.ompt_task_info.frame.exit_frame = NULL;
Joachim Protze82e94a52017-11-01 10:08:30 +00001931 __ompt_lw_taskteam_link(&lwt, master_th, 1);
1932// don't use lw_taskteam after linking. content was swaped
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001933#endif
1934
Jonathan Peyton30419822017-05-12 18:01:32 +00001935 // we were called from GNU native code
1936 KA_TRACE(20, ("__kmp_fork_call: T#%d serial exit\n", gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001937 return FALSE;
Andrey Churbanovc47afcd2017-07-03 11:24:08 +00001938 } else {
Jonathan Peyton30419822017-05-12 18:01:32 +00001939 KMP_ASSERT2(call_context < fork_context_last,
1940 "__kmp_fork_call: unknown fork_context parameter");
1941 }
1942
1943 KA_TRACE(20, ("__kmp_fork_call: T#%d serial exit\n", gtid));
1944 KMP_MB();
1945 return FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001946 }
1947
Jim Cownie5e8470a2013-09-27 10:38:44 +00001948 // GEH: only modify the executing flag in the case when not serialized
1949 // serialized case is handled in kmpc_serialized_parallel
Jonathan Peyton30419822017-05-12 18:01:32 +00001950 KF_TRACE(10, ("__kmp_fork_call: parent_team_aclevel=%d, master_th=%p, "
1951 "curtask=%p, curtask_max_aclevel=%d\n",
1952 parent_team->t.t_active_level, master_th,
1953 master_th->th.th_current_task,
1954 master_th->th.th_current_task->td_icvs.max_active_levels));
1955 // TODO: GEH - cannot do this assertion because root thread not set up as
1956 // executing
Jim Cownie5e8470a2013-09-27 10:38:44 +00001957 // KMP_ASSERT( master_th->th.th_current_task->td_flags.executing == 1 );
1958 master_th->th.th_current_task->td_flags.executing = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001959
1960#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00001961 if (!master_th->th.th_teams_microtask || level > teams_level)
Jim Cownie5e8470a2013-09-27 10:38:44 +00001962#endif /* OMP_40_ENABLED */
1963 {
Jonathan Peyton30419822017-05-12 18:01:32 +00001964 /* Increment our nested depth level */
Jonathan Peyton37e2ef52018-07-09 17:36:22 +00001965 KMP_ATOMIC_INC(&root->r.r_in_parallel);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001966 }
1967
Jim Cownie5e8470a2013-09-27 10:38:44 +00001968 // See if we need to make a copy of the ICVs.
Jim Cownie5e8470a2013-09-27 10:38:44 +00001969 int nthreads_icv = master_th->th.th_current_task->td_icvs.nproc;
Jonathan Peyton30419822017-05-12 18:01:32 +00001970 if ((level + 1 < __kmp_nested_nth.used) &&
1971 (__kmp_nested_nth.nth[level + 1] != nthreads_icv)) {
1972 nthreads_icv = __kmp_nested_nth.nth[level + 1];
1973 } else {
1974 nthreads_icv = 0; // don't update
Jim Cownie5e8470a2013-09-27 10:38:44 +00001975 }
1976
1977#if OMP_40_ENABLED
Jim Cownie5e8470a2013-09-27 10:38:44 +00001978 // Figure out the proc_bind_policy for the new team.
Jim Cownie5e8470a2013-09-27 10:38:44 +00001979 kmp_proc_bind_t proc_bind = master_th->th.th_set_proc_bind;
Jonathan Peyton30419822017-05-12 18:01:32 +00001980 kmp_proc_bind_t proc_bind_icv =
1981 proc_bind_default; // proc_bind_default means don't update
1982 if (master_th->th.th_current_task->td_icvs.proc_bind == proc_bind_false) {
1983 proc_bind = proc_bind_false;
1984 } else {
1985 if (proc_bind == proc_bind_default) {
1986 // No proc_bind clause specified; use current proc-bind-var for this
1987 // parallel region
1988 proc_bind = master_th->th.th_current_task->td_icvs.proc_bind;
1989 }
1990 /* else: The proc_bind policy was specified explicitly on parallel clause.
1991 This overrides proc-bind-var for this parallel region, but does not
1992 change proc-bind-var. */
1993 // Figure the value of proc-bind-var for the child threads.
1994 if ((level + 1 < __kmp_nested_proc_bind.used) &&
1995 (__kmp_nested_proc_bind.bind_types[level + 1] !=
1996 master_th->th.th_current_task->td_icvs.proc_bind)) {
1997 proc_bind_icv = __kmp_nested_proc_bind.bind_types[level + 1];
1998 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001999 }
2000
Jim Cownie5e8470a2013-09-27 10:38:44 +00002001 // Reset for next parallel region
Jim Cownie5e8470a2013-09-27 10:38:44 +00002002 master_th->th.th_set_proc_bind = proc_bind_default;
2003#endif /* OMP_40_ENABLED */
2004
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002005 if ((nthreads_icv > 0)
Jim Cownie5e8470a2013-09-27 10:38:44 +00002006#if OMP_40_ENABLED
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002007 || (proc_bind_icv != proc_bind_default)
Jim Cownie5e8470a2013-09-27 10:38:44 +00002008#endif /* OMP_40_ENABLED */
Jonathan Peyton30419822017-05-12 18:01:32 +00002009 ) {
2010 kmp_internal_control_t new_icvs;
2011 copy_icvs(&new_icvs, &master_th->th.th_current_task->td_icvs);
2012 new_icvs.next = NULL;
2013 if (nthreads_icv > 0) {
2014 new_icvs.nproc = nthreads_icv;
2015 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002016
2017#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00002018 if (proc_bind_icv != proc_bind_default) {
2019 new_icvs.proc_bind = proc_bind_icv;
2020 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002021#endif /* OMP_40_ENABLED */
2022
Jonathan Peyton30419822017-05-12 18:01:32 +00002023 /* allocate a new parallel team */
2024 KF_TRACE(10, ("__kmp_fork_call: before __kmp_allocate_team\n"));
2025 team = __kmp_allocate_team(root, nthreads, nthreads,
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002026#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00002027 ompt_parallel_data,
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002028#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00002029#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00002030 proc_bind,
Jim Cownie5e8470a2013-09-27 10:38:44 +00002031#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00002032 &new_icvs, argc USE_NESTED_HOT_ARG(master_th));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002033 } else {
Jonathan Peyton30419822017-05-12 18:01:32 +00002034 /* allocate a new parallel team */
2035 KF_TRACE(10, ("__kmp_fork_call: before __kmp_allocate_team\n"));
2036 team = __kmp_allocate_team(root, nthreads, nthreads,
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002037#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00002038 ompt_parallel_data,
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002039#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00002040#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00002041 proc_bind,
Jim Cownie5e8470a2013-09-27 10:38:44 +00002042#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00002043 &master_th->th.th_current_task->td_icvs,
2044 argc USE_NESTED_HOT_ARG(master_th));
Jim Cownie5e8470a2013-09-27 10:38:44 +00002045 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002046 KF_TRACE(
2047 10, ("__kmp_fork_call: after __kmp_allocate_team - team = %p\n", team));
Jim Cownie5e8470a2013-09-27 10:38:44 +00002048
2049 /* setup the new team */
Jonathan Peytonb044e4f2016-05-23 18:01:19 +00002050 KMP_CHECK_UPDATE(team->t.t_master_tid, master_tid);
2051 KMP_CHECK_UPDATE(team->t.t_master_this_cons, master_this_cons);
2052 KMP_CHECK_UPDATE(team->t.t_ident, loc);
2053 KMP_CHECK_UPDATE(team->t.t_parent, parent_team);
2054 KMP_CHECK_UPDATE_SYNC(team->t.t_pkfn, microtask);
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002055#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00002056 KMP_CHECK_UPDATE_SYNC(team->t.ompt_team_info.master_return_address,
2057 return_address);
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002058#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00002059 KMP_CHECK_UPDATE(team->t.t_invoke, invoker); // TODO move to root, maybe
2060// TODO: parent_team->t.t_level == INT_MAX ???
Jim Cownie5e8470a2013-09-27 10:38:44 +00002061#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00002062 if (!master_th->th.th_teams_microtask || level > teams_level) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00002063#endif /* OMP_40_ENABLED */
Jonathan Peyton30419822017-05-12 18:01:32 +00002064 int new_level = parent_team->t.t_level + 1;
2065 KMP_CHECK_UPDATE(team->t.t_level, new_level);
2066 new_level = parent_team->t.t_active_level + 1;
2067 KMP_CHECK_UPDATE(team->t.t_active_level, new_level);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002068#if OMP_40_ENABLED
2069 } else {
Jonathan Peyton30419822017-05-12 18:01:32 +00002070 // AC: Do not increase parallel level at start of the teams construct
2071 int new_level = parent_team->t.t_level;
2072 KMP_CHECK_UPDATE(team->t.t_level, new_level);
2073 new_level = parent_team->t.t_active_level;
2074 KMP_CHECK_UPDATE(team->t.t_active_level, new_level);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002075 }
2076#endif /* OMP_40_ENABLED */
Jonathan Peytonb044e4f2016-05-23 18:01:19 +00002077 kmp_r_sched_t new_sched = get__sched_2(parent_team, master_tid);
Jonathan Peytonba55a7b2017-11-29 22:47:52 +00002078 // set master's schedule as new run-time schedule
2079 KMP_CHECK_UPDATE(team->t.t_sched.sched, new_sched.sched);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002080
Jonathan Peyton45ca5da2015-10-19 19:33:38 +00002081#if OMP_40_ENABLED
Jonathan Peytonb044e4f2016-05-23 18:01:19 +00002082 KMP_CHECK_UPDATE(team->t.t_cancel_request, cancel_noreq);
Jonathan Peyton45ca5da2015-10-19 19:33:38 +00002083#endif
2084
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002085 // Update the floating point rounding in the team if required.
2086 propagateFPControl(team);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002087
Jonathan Peyton30419822017-05-12 18:01:32 +00002088 if (__kmp_tasking_mode != tskm_immediate_exec) {
2089 // Set master's task team to team's task team. Unless this is hot team, it
2090 // should be NULL.
Jonathan Peyton30419822017-05-12 18:01:32 +00002091 KMP_DEBUG_ASSERT(master_th->th.th_task_team ==
2092 parent_team->t.t_task_team[master_th->th.th_task_state]);
Jonathan Peyton30419822017-05-12 18:01:32 +00002093 KA_TRACE(20, ("__kmp_fork_call: Master T#%d pushing task_team %p / team "
2094 "%p, new task_team %p / team %p\n",
2095 __kmp_gtid_from_thread(master_th),
2096 master_th->th.th_task_team, parent_team,
2097 team->t.t_task_team[master_th->th.th_task_state], team));
Jonathan Peytond3f2b942016-02-09 22:32:41 +00002098
Jonathan Peyton30419822017-05-12 18:01:32 +00002099 if (active_level || master_th->th.th_task_team) {
2100 // Take a memo of master's task_state
2101 KMP_DEBUG_ASSERT(master_th->th.th_task_state_memo_stack);
2102 if (master_th->th.th_task_state_top >=
2103 master_th->th.th_task_state_stack_sz) { // increase size
2104 kmp_uint32 new_size = 2 * master_th->th.th_task_state_stack_sz;
2105 kmp_uint8 *old_stack, *new_stack;
2106 kmp_uint32 i;
2107 new_stack = (kmp_uint8 *)__kmp_allocate(new_size);
2108 for (i = 0; i < master_th->th.th_task_state_stack_sz; ++i) {
2109 new_stack[i] = master_th->th.th_task_state_memo_stack[i];
2110 }
2111 for (i = master_th->th.th_task_state_stack_sz; i < new_size;
2112 ++i) { // zero-init rest of stack
2113 new_stack[i] = 0;
2114 }
2115 old_stack = master_th->th.th_task_state_memo_stack;
2116 master_th->th.th_task_state_memo_stack = new_stack;
2117 master_th->th.th_task_state_stack_sz = new_size;
2118 __kmp_free(old_stack);
Andrey Churbanov6d224db2015-02-10 18:37:43 +00002119 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002120 // Store master's task_state on stack
2121 master_th->th
2122 .th_task_state_memo_stack[master_th->th.th_task_state_top] =
2123 master_th->th.th_task_state;
2124 master_th->th.th_task_state_top++;
2125#if KMP_NESTED_HOT_TEAMS
Jonathan Peyton642688b2017-06-01 16:46:36 +00002126 if (team == master_th->th.th_hot_teams[active_level].hot_team) {
2127 // Restore master's nested state if nested hot team
Jonathan Peyton30419822017-05-12 18:01:32 +00002128 master_th->th.th_task_state =
2129 master_th->th
2130 .th_task_state_memo_stack[master_th->th.th_task_state_top];
2131 } else {
2132#endif
2133 master_th->th.th_task_state = 0;
2134#if KMP_NESTED_HOT_TEAMS
2135 }
2136#endif
2137 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002138#if !KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00002139 KMP_DEBUG_ASSERT((master_th->th.th_task_team == NULL) ||
2140 (team == root->r.r_hot_team));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002141#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00002142 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002143
Jonathan Peyton30419822017-05-12 18:01:32 +00002144 KA_TRACE(
2145 20,
2146 ("__kmp_fork_call: T#%d(%d:%d)->(%d:0) created a team of %d threads\n",
2147 gtid, parent_team->t.t_id, team->t.t_master_tid, team->t.t_id,
2148 team->t.t_nproc));
2149 KMP_DEBUG_ASSERT(team != root->r.r_hot_team ||
2150 (team->t.t_master_tid == 0 &&
2151 (team->t.t_parent == root->r.r_root_team ||
2152 team->t.t_parent->t.t_serialized)));
Jim Cownie5e8470a2013-09-27 10:38:44 +00002153 KMP_MB();
2154
2155 /* now, setup the arguments */
Jonathan Peyton30419822017-05-12 18:01:32 +00002156 argv = (void **)team->t.t_argv;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002157#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00002158 if (ap) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00002159#endif /* OMP_40_ENABLED */
Jonathan Peyton30419822017-05-12 18:01:32 +00002160 for (i = argc - 1; i >= 0; --i) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002161// TODO: revert workaround for Intel(R) 64 tracker #96
Andrey Churbanovcbda8682015-01-13 14:43:35 +00002162#if (KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64) && KMP_OS_LINUX
Jonathan Peyton30419822017-05-12 18:01:32 +00002163 void *new_argv = va_arg(*ap, void *);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002164#else
Jonathan Peyton30419822017-05-12 18:01:32 +00002165 void *new_argv = va_arg(ap, void *);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002166#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00002167 KMP_CHECK_UPDATE(*argv, new_argv);
2168 argv++;
2169 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002170#if OMP_40_ENABLED
2171 } else {
Jonathan Peyton30419822017-05-12 18:01:32 +00002172 for (i = 0; i < argc; ++i) {
2173 // Get args from parent team for teams construct
2174 KMP_CHECK_UPDATE(argv[i], team->t.t_parent->t.t_argv[i]);
2175 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002176 }
2177#endif /* OMP_40_ENABLED */
2178
2179 /* now actually fork the threads */
Jonathan Peytonb044e4f2016-05-23 18:01:19 +00002180 KMP_CHECK_UPDATE(team->t.t_master_active, master_active);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002181 if (!root->r.r_active) // Only do assignment if it prevents cache ping-pong
Jonathan Peyton30419822017-05-12 18:01:32 +00002182 root->r.r_active = TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002183
Jonathan Peyton30419822017-05-12 18:01:32 +00002184 __kmp_fork_team_threads(root, team, master_th, gtid);
2185 __kmp_setup_icv_copy(team, nthreads,
2186 &master_th->th.th_current_task->td_icvs, loc);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002187
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002188#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00002189 master_th->th.ompt_thread_info.state = omp_state_work_parallel;
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002190#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00002191
Jonathan Peyton30419822017-05-12 18:01:32 +00002192 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002193
Jim Cownie5e8470a2013-09-27 10:38:44 +00002194#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00002195 if (team->t.t_active_level == 1 // only report frames at level 1
2196#if OMP_40_ENABLED
Andrey Churbanov51aecb82015-05-06 19:22:36 +00002197 && !master_th->th.th_teams_microtask // not in teams construct
Jonathan Peyton30419822017-05-12 18:01:32 +00002198#endif /* OMP_40_ENABLED */
2199 ) {
Andrey Churbanov51aecb82015-05-06 19:22:36 +00002200#if USE_ITT_NOTIFY
Jonathan Peyton30419822017-05-12 18:01:32 +00002201 if ((__itt_frame_submit_v3_ptr || KMP_ITT_DEBUG) &&
2202 (__kmp_forkjoin_frames_mode == 3 ||
2203 __kmp_forkjoin_frames_mode == 1)) {
2204 kmp_uint64 tmp_time = 0;
2205 if (__itt_get_timestamp_ptr)
2206 tmp_time = __itt_get_timestamp();
2207 // Internal fork - report frame begin
2208 master_th->th.th_frame_time = tmp_time;
2209 if (__kmp_forkjoin_frames_mode == 3)
2210 team->t.t_region_time = tmp_time;
Jonathan Peyton642688b2017-06-01 16:46:36 +00002211 } else
2212// only one notification scheme (either "submit" or "forking/joined", not both)
Andrey Churbanov51aecb82015-05-06 19:22:36 +00002213#endif /* USE_ITT_NOTIFY */
Jonathan Peyton30419822017-05-12 18:01:32 +00002214 if ((__itt_frame_begin_v3_ptr || KMP_ITT_DEBUG) &&
2215 __kmp_forkjoin_frames && !__kmp_forkjoin_frames_mode) {
Jonathan Peyton8c432f22018-01-04 22:56:47 +00002216 // Mark start of "parallel" region for Intel(R) VTune(TM) analyzer.
Jonathan Peyton30419822017-05-12 18:01:32 +00002217 __kmp_itt_region_forking(gtid, team->t.t_nproc, 0);
2218 }
Andrey Churbanovf6451d92015-01-16 15:58:03 +00002219 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002220#endif /* USE_ITT_BUILD */
2221
2222 /* now go on and do the work */
Jonathan Peyton30419822017-05-12 18:01:32 +00002223 KMP_DEBUG_ASSERT(team == __kmp_threads[gtid]->th.th_team);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002224 KMP_MB();
Jonathan Peyton30419822017-05-12 18:01:32 +00002225 KF_TRACE(10,
2226 ("__kmp_internal_fork : root=%p, team=%p, master_th=%p, gtid=%d\n",
2227 root, team, master_th, gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00002228
2229#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00002230 if (__itt_stack_caller_create_ptr) {
2231 team->t.t_stack_id =
2232 __kmp_itt_stack_caller_create(); // create new stack stitching id
2233 // before entering fork barrier
Jim Cownie5e8470a2013-09-27 10:38:44 +00002234 }
2235#endif /* USE_ITT_BUILD */
2236
2237#if OMP_40_ENABLED
Jonathan Peyton642688b2017-06-01 16:46:36 +00002238 // AC: skip __kmp_internal_fork at teams construct, let only master
2239 // threads execute
2240 if (ap)
Jim Cownie5e8470a2013-09-27 10:38:44 +00002241#endif /* OMP_40_ENABLED */
2242 {
Jonathan Peyton30419822017-05-12 18:01:32 +00002243 __kmp_internal_fork(loc, gtid, team);
2244 KF_TRACE(10, ("__kmp_internal_fork : after : root=%p, team=%p, "
2245 "master_th=%p, gtid=%d\n",
2246 root, team, master_th, gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00002247 }
2248
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002249 if (call_context == fork_context_gnu) {
Jonathan Peyton30419822017-05-12 18:01:32 +00002250 KA_TRACE(20, ("__kmp_fork_call: parallel exit T#%d\n", gtid));
2251 return TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002252 }
2253
2254 /* Invoke microtask for MASTER thread */
Jonathan Peyton30419822017-05-12 18:01:32 +00002255 KA_TRACE(20, ("__kmp_fork_call: T#%d(%d:0) invoke microtask = %p\n", gtid,
2256 team->t.t_id, team->t.t_pkfn));
2257 } // END of timer KMP_fork_call block
Jim Cownie5e8470a2013-09-27 10:38:44 +00002258
Jonathan Peyton30419822017-05-12 18:01:32 +00002259 {
2260 KMP_TIME_PARTITIONED_BLOCK(OMP_parallel);
2261 KMP_SET_THREAD_STATE_BLOCK(IMPLICIT_TASK);
2262 if (!team->t.t_invoke(gtid)) {
2263 KMP_ASSERT2(0, "cannot invoke microtask for MASTER thread");
Jim Cownie5e8470a2013-09-27 10:38:44 +00002264 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002265 }
2266 KA_TRACE(20, ("__kmp_fork_call: T#%d(%d:0) done microtask = %p\n", gtid,
2267 team->t.t_id, team->t.t_pkfn));
2268 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00002269
Jonathan Peyton30419822017-05-12 18:01:32 +00002270 KA_TRACE(20, ("__kmp_fork_call: parallel exit T#%d\n", gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00002271
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002272#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00002273 if (ompt_enabled.enabled) {
2274 master_th->th.ompt_thread_info.state = omp_state_overhead;
Jonathan Peyton30419822017-05-12 18:01:32 +00002275 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002276#endif
2277
Jonathan Peyton30419822017-05-12 18:01:32 +00002278 return TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002279}
2280
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002281#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00002282static inline void __kmp_join_restore_state(kmp_info_t *thread,
2283 kmp_team_t *team) {
2284 // restore state outside the region
2285 thread->th.ompt_thread_info.state =
Joachim Protze82e94a52017-11-01 10:08:30 +00002286 ((team->t.t_serialized) ? omp_state_work_serial
2287 : omp_state_work_parallel);
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002288}
2289
Joachim Protze82e94a52017-11-01 10:08:30 +00002290static inline void __kmp_join_ompt(int gtid, kmp_info_t *thread,
2291 kmp_team_t *team, ompt_data_t *parallel_data,
2292 fork_context_e fork_context, void *codeptr) {
2293 ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
2294 if (ompt_enabled.ompt_callback_parallel_end) {
2295 ompt_callbacks.ompt_callback(ompt_callback_parallel_end)(
2296 parallel_data, &(task_info->task_data), OMPT_INVOKER(fork_context),
2297 codeptr);
Jonathan Peyton30419822017-05-12 18:01:32 +00002298 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002299
Joachim Protzec255ca72017-11-05 14:11:10 +00002300 task_info->frame.enter_frame = NULL;
Jonathan Peyton30419822017-05-12 18:01:32 +00002301 __kmp_join_restore_state(thread, team);
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002302}
2303#endif
2304
Jonathan Peyton30419822017-05-12 18:01:32 +00002305void __kmp_join_call(ident_t *loc, int gtid
Jonathan Peytonf89fbbb2015-08-31 18:15:00 +00002306#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00002307 ,
2308 enum fork_context_e fork_context
Jonathan Peytonf89fbbb2015-08-31 18:15:00 +00002309#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00002310#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00002311 ,
2312 int exit_teams
Jim Cownie5e8470a2013-09-27 10:38:44 +00002313#endif /* OMP_40_ENABLED */
Jonathan Peyton30419822017-05-12 18:01:32 +00002314 ) {
2315 KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(KMP_join_call);
2316 kmp_team_t *team;
2317 kmp_team_t *parent_team;
2318 kmp_info_t *master_th;
2319 kmp_root_t *root;
2320 int master_active;
2321 int i;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002322
Jonathan Peyton30419822017-05-12 18:01:32 +00002323 KA_TRACE(20, ("__kmp_join_call: enter T#%d\n", gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00002324
Jonathan Peyton30419822017-05-12 18:01:32 +00002325 /* setup current data */
2326 master_th = __kmp_threads[gtid];
2327 root = master_th->th.th_root;
2328 team = master_th->th.th_team;
2329 parent_team = team->t.t_parent;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002330
Jonathan Peyton30419822017-05-12 18:01:32 +00002331 master_th->th.th_ident = loc;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002332
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002333#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00002334 if (ompt_enabled.enabled) {
2335 master_th->th.ompt_thread_info.state = omp_state_overhead;
Jonathan Peyton30419822017-05-12 18:01:32 +00002336 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002337#endif
2338
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002339#if KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00002340 if (__kmp_tasking_mode != tskm_immediate_exec && !exit_teams) {
2341 KA_TRACE(20, ("__kmp_join_call: T#%d, old team = %p old task_team = %p, "
2342 "th_task_team = %p\n",
2343 __kmp_gtid_from_thread(master_th), team,
2344 team->t.t_task_team[master_th->th.th_task_state],
2345 master_th->th.th_task_team));
2346 KMP_DEBUG_ASSERT(master_th->th.th_task_team ==
2347 team->t.t_task_team[master_th->th.th_task_state]);
2348 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002349#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00002350
Jonathan Peyton30419822017-05-12 18:01:32 +00002351 if (team->t.t_serialized) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00002352#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00002353 if (master_th->th.th_teams_microtask) {
2354 // We are in teams construct
2355 int level = team->t.t_level;
2356 int tlevel = master_th->th.th_teams_level;
2357 if (level == tlevel) {
2358 // AC: we haven't incremented it earlier at start of teams construct,
2359 // so do it here - at the end of teams construct
2360 team->t.t_level++;
2361 } else if (level == tlevel + 1) {
2362 // AC: we are exiting parallel inside teams, need to increment
2363 // serialization in order to restore it in the next call to
2364 // __kmpc_end_serialized_parallel
2365 team->t.t_serialized++;
2366 }
Andrey Churbanov6d224db2015-02-10 18:37:43 +00002367 }
Jonathan Peyton441f3372015-09-21 17:24:46 +00002368#endif /* OMP_40_ENABLED */
Jonathan Peyton30419822017-05-12 18:01:32 +00002369 __kmpc_end_serialized_parallel(loc, gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002370
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002371#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00002372 if (ompt_enabled.enabled) {
Jonathan Peyton30419822017-05-12 18:01:32 +00002373 __kmp_join_restore_state(master_th, parent_team);
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002374 }
2375#endif
2376
Jonathan Peyton30419822017-05-12 18:01:32 +00002377 return;
2378 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002379
Jonathan Peyton30419822017-05-12 18:01:32 +00002380 master_active = team->t.t_master_active;
2381
2382#if OMP_40_ENABLED
2383 if (!exit_teams)
2384#endif /* OMP_40_ENABLED */
2385 {
2386 // AC: No barrier for internal teams at exit from teams construct.
2387 // But there is barrier for external team (league).
2388 __kmp_internal_join(loc, gtid, team);
2389 }
2390#if OMP_40_ENABLED
2391 else {
2392 master_th->th.th_task_state =
2393 0; // AC: no tasking in teams (out of any parallel)
2394 }
2395#endif /* OMP_40_ENABLED */
2396
2397 KMP_MB();
2398
2399#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00002400 ompt_data_t *parallel_data = &(team->t.ompt_team_info.parallel_data);
2401 void *codeptr = team->t.ompt_team_info.master_return_address;
Jonathan Peyton30419822017-05-12 18:01:32 +00002402#endif
2403
2404#if USE_ITT_BUILD
2405 if (__itt_stack_caller_create_ptr) {
2406 __kmp_itt_stack_caller_destroy(
2407 (__itt_caller)team->t
2408 .t_stack_id); // destroy the stack stitching id after join barrier
2409 }
2410
Jonathan Peyton8c432f22018-01-04 22:56:47 +00002411 // Mark end of "parallel" region for Intel(R) VTune(TM) analyzer.
Jonathan Peyton30419822017-05-12 18:01:32 +00002412 if (team->t.t_active_level == 1
2413#if OMP_40_ENABLED
2414 && !master_th->th.th_teams_microtask /* not in teams construct */
2415#endif /* OMP_40_ENABLED */
2416 ) {
2417 master_th->th.th_ident = loc;
2418 // only one notification scheme (either "submit" or "forking/joined", not
2419 // both)
2420 if ((__itt_frame_submit_v3_ptr || KMP_ITT_DEBUG) &&
2421 __kmp_forkjoin_frames_mode == 3)
2422 __kmp_itt_frame_submit(gtid, team->t.t_region_time,
2423 master_th->th.th_frame_time, 0, loc,
2424 master_th->th.th_team_nproc, 1);
2425 else if ((__itt_frame_end_v3_ptr || KMP_ITT_DEBUG) &&
2426 !__kmp_forkjoin_frames_mode && __kmp_forkjoin_frames)
2427 __kmp_itt_region_joined(gtid);
2428 } // active_level == 1
2429#endif /* USE_ITT_BUILD */
2430
2431#if OMP_40_ENABLED
2432 if (master_th->th.th_teams_microtask && !exit_teams &&
2433 team->t.t_pkfn != (microtask_t)__kmp_teams_master &&
2434 team->t.t_level == master_th->th.th_teams_level + 1) {
2435 // AC: We need to leave the team structure intact at the end of parallel
2436 // inside the teams construct, so that at the next parallel same (hot) team
2437 // works, only adjust nesting levels
2438
2439 /* Decrement our nested depth level */
2440 team->t.t_level--;
2441 team->t.t_active_level--;
Jonathan Peyton37e2ef52018-07-09 17:36:22 +00002442 KMP_ATOMIC_DEC(&root->r.r_in_parallel);
Jonathan Peyton30419822017-05-12 18:01:32 +00002443
2444 /* Restore number of threads in the team if needed */
2445 if (master_th->th.th_team_nproc < master_th->th.th_teams_size.nth) {
2446 int old_num = master_th->th.th_team_nproc;
2447 int new_num = master_th->th.th_teams_size.nth;
2448 kmp_info_t **other_threads = team->t.t_threads;
2449 team->t.t_nproc = new_num;
2450 for (i = 0; i < old_num; ++i) {
2451 other_threads[i]->th.th_team_nproc = new_num;
2452 }
2453 // Adjust states of non-used threads of the team
2454 for (i = old_num; i < new_num; ++i) {
2455 // Re-initialize thread's barrier data.
2456 int b;
2457 kmp_balign_t *balign = other_threads[i]->th.th_bar;
2458 for (b = 0; b < bs_last_barrier; ++b) {
2459 balign[b].bb.b_arrived = team->t.t_bar[b].b_arrived;
2460 KMP_DEBUG_ASSERT(balign[b].bb.wait_flag != KMP_BARRIER_PARENT_FLAG);
2461#if USE_DEBUGGER
2462 balign[b].bb.b_worker_arrived = team->t.t_bar[b].b_team_arrived;
2463#endif
2464 }
2465 if (__kmp_tasking_mode != tskm_immediate_exec) {
2466 // Synchronize thread's task state
2467 other_threads[i]->th.th_task_state = master_th->th.th_task_state;
2468 }
2469 }
2470 }
2471
2472#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00002473 if (ompt_enabled.enabled) {
2474 __kmp_join_ompt(gtid, master_th, parent_team, parallel_data, fork_context,
2475 codeptr);
Jonathan Peyton30419822017-05-12 18:01:32 +00002476 }
2477#endif
2478
2479 return;
2480 }
2481#endif /* OMP_40_ENABLED */
2482
2483 /* do cleanup and restore the parent team */
2484 master_th->th.th_info.ds.ds_tid = team->t.t_master_tid;
2485 master_th->th.th_local.this_construct = team->t.t_master_this_cons;
2486
2487 master_th->th.th_dispatch = &parent_team->t.t_dispatch[team->t.t_master_tid];
2488
2489 /* jc: The following lock has instructions with REL and ACQ semantics,
2490 separating the parallel user code called in this parallel region
2491 from the serial user code called after this function returns. */
2492 __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock);
2493
2494#if OMP_40_ENABLED
2495 if (!master_th->th.th_teams_microtask ||
2496 team->t.t_level > master_th->th.th_teams_level)
2497#endif /* OMP_40_ENABLED */
2498 {
2499 /* Decrement our nested depth level */
Jonathan Peyton37e2ef52018-07-09 17:36:22 +00002500 KMP_ATOMIC_DEC(&root->r.r_in_parallel);
Jonathan Peyton30419822017-05-12 18:01:32 +00002501 }
2502 KMP_DEBUG_ASSERT(root->r.r_in_parallel >= 0);
2503
Joachim Protze82e94a52017-11-01 10:08:30 +00002504#if OMPT_SUPPORT
2505 if (ompt_enabled.enabled) {
2506 ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
2507 if (ompt_enabled.ompt_callback_implicit_task) {
2508 int ompt_team_size = team->t.t_nproc;
2509 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
2510 ompt_scope_end, NULL, &(task_info->task_data), ompt_team_size,
Joachim Protze9be9cf22018-05-07 12:42:21 +00002511 OMPT_CUR_TASK_INFO(master_th)->thread_num);
Jonathan Peyton30419822017-05-12 18:01:32 +00002512 }
Joachim Protze82e94a52017-11-01 10:08:30 +00002513
Joachim Protzec255ca72017-11-05 14:11:10 +00002514 task_info->frame.exit_frame = NULL;
Joachim Protze82e94a52017-11-01 10:08:30 +00002515 task_info->task_data = ompt_data_none;
Jonathan Peyton30419822017-05-12 18:01:32 +00002516 }
2517#endif
2518
2519 KF_TRACE(10, ("__kmp_join_call1: T#%d, this_thread=%p team=%p\n", 0,
2520 master_th, team));
2521 __kmp_pop_current_task_from_thread(master_th);
2522
2523#if OMP_40_ENABLED && KMP_AFFINITY_SUPPORTED
2524 // Restore master thread's partition.
2525 master_th->th.th_first_place = team->t.t_first_place;
2526 master_th->th.th_last_place = team->t.t_last_place;
2527#endif /* OMP_40_ENABLED */
2528
2529 updateHWFPControl(team);
2530
2531 if (root->r.r_active != master_active)
2532 root->r.r_active = master_active;
2533
2534 __kmp_free_team(root, team USE_NESTED_HOT_ARG(
2535 master_th)); // this will free worker threads
2536
2537 /* this race was fun to find. make sure the following is in the critical
2538 region otherwise assertions may fail occasionally since the old team may be
2539 reallocated and the hierarchy appears inconsistent. it is actually safe to
2540 run and won't cause any bugs, but will cause those assertion failures. it's
2541 only one deref&assign so might as well put this in the critical region */
2542 master_th->th.th_team = parent_team;
2543 master_th->th.th_team_nproc = parent_team->t.t_nproc;
2544 master_th->th.th_team_master = parent_team->t.t_threads[0];
2545 master_th->th.th_team_serialized = parent_team->t.t_serialized;
2546
2547 /* restore serialized team, if need be */
2548 if (parent_team->t.t_serialized &&
2549 parent_team != master_th->th.th_serial_team &&
2550 parent_team != root->r.r_root_team) {
2551 __kmp_free_team(root,
2552 master_th->th.th_serial_team USE_NESTED_HOT_ARG(NULL));
2553 master_th->th.th_serial_team = parent_team;
2554 }
2555
2556 if (__kmp_tasking_mode != tskm_immediate_exec) {
2557 if (master_th->th.th_task_state_top >
2558 0) { // Restore task state from memo stack
2559 KMP_DEBUG_ASSERT(master_th->th.th_task_state_memo_stack);
2560 // Remember master's state if we re-use this nested hot team
2561 master_th->th.th_task_state_memo_stack[master_th->th.th_task_state_top] =
2562 master_th->th.th_task_state;
2563 --master_th->th.th_task_state_top; // pop
2564 // Now restore state at this level
2565 master_th->th.th_task_state =
2566 master_th->th
2567 .th_task_state_memo_stack[master_th->th.th_task_state_top];
2568 }
2569 // Copy the task team from the parent team to the master thread
2570 master_th->th.th_task_team =
2571 parent_team->t.t_task_team[master_th->th.th_task_state];
2572 KA_TRACE(20,
2573 ("__kmp_join_call: Master T#%d restoring task_team %p / team %p\n",
2574 __kmp_gtid_from_thread(master_th), master_th->th.th_task_team,
2575 parent_team));
2576 }
2577
2578 // TODO: GEH - cannot do this assertion because root thread not set up as
2579 // executing
2580 // KMP_ASSERT( master_th->th.th_current_task->td_flags.executing == 0 );
2581 master_th->th.th_current_task->td_flags.executing = 1;
2582
2583 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
2584
2585#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00002586 if (ompt_enabled.enabled) {
2587 __kmp_join_ompt(gtid, master_th, parent_team, parallel_data, fork_context,
2588 codeptr);
Jonathan Peyton30419822017-05-12 18:01:32 +00002589 }
2590#endif
2591
2592 KMP_MB();
2593 KA_TRACE(20, ("__kmp_join_call: exit T#%d\n", gtid));
2594}
Jim Cownie5e8470a2013-09-27 10:38:44 +00002595
2596/* Check whether we should push an internal control record onto the
2597 serial team stack. If so, do it. */
Jonathan Peyton30419822017-05-12 18:01:32 +00002598void __kmp_save_internal_controls(kmp_info_t *thread) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00002599
Jonathan Peyton30419822017-05-12 18:01:32 +00002600 if (thread->th.th_team != thread->th.th_serial_team) {
2601 return;
2602 }
2603 if (thread->th.th_team->t.t_serialized > 1) {
2604 int push = 0;
2605
2606 if (thread->th.th_team->t.t_control_stack_top == NULL) {
2607 push = 1;
2608 } else {
2609 if (thread->th.th_team->t.t_control_stack_top->serial_nesting_level !=
2610 thread->th.th_team->t.t_serialized) {
2611 push = 1;
2612 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002613 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002614 if (push) { /* push a record on the serial team's stack */
2615 kmp_internal_control_t *control =
2616 (kmp_internal_control_t *)__kmp_allocate(
2617 sizeof(kmp_internal_control_t));
Jim Cownie5e8470a2013-09-27 10:38:44 +00002618
Jonathan Peyton30419822017-05-12 18:01:32 +00002619 copy_icvs(control, &thread->th.th_current_task->td_icvs);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002620
Jonathan Peyton30419822017-05-12 18:01:32 +00002621 control->serial_nesting_level = thread->th.th_team->t.t_serialized;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002622
Jonathan Peyton30419822017-05-12 18:01:32 +00002623 control->next = thread->th.th_team->t.t_control_stack_top;
2624 thread->th.th_team->t.t_control_stack_top = control;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002625 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002626 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002627}
2628
2629/* Changes set_nproc */
Jonathan Peyton30419822017-05-12 18:01:32 +00002630void __kmp_set_num_threads(int new_nth, int gtid) {
2631 kmp_info_t *thread;
2632 kmp_root_t *root;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002633
Jonathan Peyton30419822017-05-12 18:01:32 +00002634 KF_TRACE(10, ("__kmp_set_num_threads: new __kmp_nth = %d\n", new_nth));
2635 KMP_DEBUG_ASSERT(__kmp_init_serial);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002636
Jonathan Peyton30419822017-05-12 18:01:32 +00002637 if (new_nth < 1)
2638 new_nth = 1;
2639 else if (new_nth > __kmp_max_nth)
2640 new_nth = __kmp_max_nth;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002641
Jonathan Peyton30419822017-05-12 18:01:32 +00002642 KMP_COUNT_VALUE(OMP_set_numthreads, new_nth);
2643 thread = __kmp_threads[gtid];
Jim Cownie5e8470a2013-09-27 10:38:44 +00002644
Jonathan Peyton30419822017-05-12 18:01:32 +00002645 __kmp_save_internal_controls(thread);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002646
Jonathan Peyton30419822017-05-12 18:01:32 +00002647 set__nproc(thread, new_nth);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002648
Jonathan Peyton30419822017-05-12 18:01:32 +00002649 // If this omp_set_num_threads() call will cause the hot team size to be
2650 // reduced (in the absence of a num_threads clause), then reduce it now,
2651 // rather than waiting for the next parallel region.
2652 root = thread->th.th_root;
2653 if (__kmp_init_parallel && (!root->r.r_active) &&
2654 (root->r.r_hot_team->t.t_nproc > new_nth)
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002655#if KMP_NESTED_HOT_TEAMS
2656 && __kmp_hot_teams_max_level && !__kmp_hot_teams_mode
2657#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00002658 ) {
2659 kmp_team_t *hot_team = root->r.r_hot_team;
2660 int f;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002661
Jonathan Peyton30419822017-05-12 18:01:32 +00002662 __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002663
Jonathan Peyton30419822017-05-12 18:01:32 +00002664 // Release the extra threads we don't need any more.
2665 for (f = new_nth; f < hot_team->t.t_nproc; f++) {
2666 KMP_DEBUG_ASSERT(hot_team->t.t_threads[f] != NULL);
2667 if (__kmp_tasking_mode != tskm_immediate_exec) {
2668 // When decreasing team size, threads no longer in the team should unref
2669 // task team.
2670 hot_team->t.t_threads[f]->th.th_task_team = NULL;
2671 }
2672 __kmp_free_thread(hot_team->t.t_threads[f]);
2673 hot_team->t.t_threads[f] = NULL;
2674 }
2675 hot_team->t.t_nproc = new_nth;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002676#if KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00002677 if (thread->th.th_hot_teams) {
2678 KMP_DEBUG_ASSERT(hot_team == thread->th.th_hot_teams[0].hot_team);
2679 thread->th.th_hot_teams[0].hot_team_nth = new_nth;
2680 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002681#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00002682
Jonathan Peyton30419822017-05-12 18:01:32 +00002683 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002684
Jonathan Peyton30419822017-05-12 18:01:32 +00002685 // Update the t_nproc field in the threads that are still active.
2686 for (f = 0; f < new_nth; f++) {
2687 KMP_DEBUG_ASSERT(hot_team->t.t_threads[f] != NULL);
2688 hot_team->t.t_threads[f]->th.th_team_nproc = new_nth;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002689 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002690 // Special flag in case omp_set_num_threads() call
2691 hot_team->t.t_size_changed = -1;
2692 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002693}
2694
Jim Cownie5e8470a2013-09-27 10:38:44 +00002695/* Changes max_active_levels */
Jonathan Peyton30419822017-05-12 18:01:32 +00002696void __kmp_set_max_active_levels(int gtid, int max_active_levels) {
2697 kmp_info_t *thread;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002698
Jonathan Peyton30419822017-05-12 18:01:32 +00002699 KF_TRACE(10, ("__kmp_set_max_active_levels: new max_active_levels for thread "
2700 "%d = (%d)\n",
2701 gtid, max_active_levels));
2702 KMP_DEBUG_ASSERT(__kmp_init_serial);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002703
Jonathan Peyton30419822017-05-12 18:01:32 +00002704 // validate max_active_levels
2705 if (max_active_levels < 0) {
2706 KMP_WARNING(ActiveLevelsNegative, max_active_levels);
2707 // We ignore this call if the user has specified a negative value.
2708 // The current setting won't be changed. The last valid setting will be
2709 // used. A warning will be issued (if warnings are allowed as controlled by
2710 // the KMP_WARNINGS env var).
2711 KF_TRACE(10, ("__kmp_set_max_active_levels: the call is ignored: new "
2712 "max_active_levels for thread %d = (%d)\n",
2713 gtid, max_active_levels));
2714 return;
2715 }
2716 if (max_active_levels <= KMP_MAX_ACTIVE_LEVELS_LIMIT) {
2717 // it's OK, the max_active_levels is within the valid range: [ 0;
2718 // KMP_MAX_ACTIVE_LEVELS_LIMIT ]
2719 // We allow a zero value. (implementation defined behavior)
2720 } else {
2721 KMP_WARNING(ActiveLevelsExceedLimit, max_active_levels,
2722 KMP_MAX_ACTIVE_LEVELS_LIMIT);
2723 max_active_levels = KMP_MAX_ACTIVE_LEVELS_LIMIT;
2724 // Current upper limit is MAX_INT. (implementation defined behavior)
2725 // If the input exceeds the upper limit, we correct the input to be the
2726 // upper limit. (implementation defined behavior)
2727 // Actually, the flow should never get here until we use MAX_INT limit.
2728 }
2729 KF_TRACE(10, ("__kmp_set_max_active_levels: after validation: new "
2730 "max_active_levels for thread %d = (%d)\n",
2731 gtid, max_active_levels));
Jim Cownie5e8470a2013-09-27 10:38:44 +00002732
Jonathan Peyton30419822017-05-12 18:01:32 +00002733 thread = __kmp_threads[gtid];
Jim Cownie5e8470a2013-09-27 10:38:44 +00002734
Jonathan Peyton30419822017-05-12 18:01:32 +00002735 __kmp_save_internal_controls(thread);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002736
Jonathan Peyton30419822017-05-12 18:01:32 +00002737 set__max_active_levels(thread, max_active_levels);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002738}
2739
2740/* Gets max_active_levels */
Jonathan Peyton30419822017-05-12 18:01:32 +00002741int __kmp_get_max_active_levels(int gtid) {
2742 kmp_info_t *thread;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002743
Jonathan Peyton30419822017-05-12 18:01:32 +00002744 KF_TRACE(10, ("__kmp_get_max_active_levels: thread %d\n", gtid));
2745 KMP_DEBUG_ASSERT(__kmp_init_serial);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002746
Jonathan Peyton30419822017-05-12 18:01:32 +00002747 thread = __kmp_threads[gtid];
2748 KMP_DEBUG_ASSERT(thread->th.th_current_task);
2749 KF_TRACE(10, ("__kmp_get_max_active_levels: thread %d, curtask=%p, "
2750 "curtask_maxaclevel=%d\n",
2751 gtid, thread->th.th_current_task,
2752 thread->th.th_current_task->td_icvs.max_active_levels));
2753 return thread->th.th_current_task->td_icvs.max_active_levels;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002754}
2755
2756/* Changes def_sched_var ICV values (run-time schedule kind and chunk) */
Jonathan Peyton30419822017-05-12 18:01:32 +00002757void __kmp_set_schedule(int gtid, kmp_sched_t kind, int chunk) {
2758 kmp_info_t *thread;
2759 // kmp_team_t *team;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002760
Jonathan Peyton30419822017-05-12 18:01:32 +00002761 KF_TRACE(10, ("__kmp_set_schedule: new schedule for thread %d = (%d, %d)\n",
2762 gtid, (int)kind, chunk));
2763 KMP_DEBUG_ASSERT(__kmp_init_serial);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002764
Jonathan Peyton30419822017-05-12 18:01:32 +00002765 // Check if the kind parameter is valid, correct if needed.
2766 // Valid parameters should fit in one of two intervals - standard or extended:
2767 // <lower>, <valid>, <upper_std>, <lower_ext>, <valid>, <upper>
2768 // 2008-01-25: 0, 1 - 4, 5, 100, 101 - 102, 103
2769 if (kind <= kmp_sched_lower || kind >= kmp_sched_upper ||
2770 (kind <= kmp_sched_lower_ext && kind >= kmp_sched_upper_std)) {
2771 // TODO: Hint needs attention in case we change the default schedule.
2772 __kmp_msg(kmp_ms_warning, KMP_MSG(ScheduleKindOutOfRange, kind),
2773 KMP_HNT(DefaultScheduleKindUsed, "static, no chunk"),
2774 __kmp_msg_null);
2775 kind = kmp_sched_default;
2776 chunk = 0; // ignore chunk value in case of bad kind
2777 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002778
Jonathan Peyton30419822017-05-12 18:01:32 +00002779 thread = __kmp_threads[gtid];
Jim Cownie5e8470a2013-09-27 10:38:44 +00002780
Jonathan Peyton30419822017-05-12 18:01:32 +00002781 __kmp_save_internal_controls(thread);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002782
Jonathan Peyton30419822017-05-12 18:01:32 +00002783 if (kind < kmp_sched_upper_std) {
2784 if (kind == kmp_sched_static && chunk < KMP_DEFAULT_CHUNK) {
2785 // differ static chunked vs. unchunked: chunk should be invalid to
2786 // indicate unchunked schedule (which is the default)
2787 thread->th.th_current_task->td_icvs.sched.r_sched_type = kmp_sch_static;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002788 } else {
Jonathan Peyton30419822017-05-12 18:01:32 +00002789 thread->th.th_current_task->td_icvs.sched.r_sched_type =
2790 __kmp_sch_map[kind - kmp_sched_lower - 1];
Jim Cownie5e8470a2013-09-27 10:38:44 +00002791 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002792 } else {
2793 // __kmp_sch_map[ kind - kmp_sched_lower_ext + kmp_sched_upper_std -
2794 // kmp_sched_lower - 2 ];
2795 thread->th.th_current_task->td_icvs.sched.r_sched_type =
2796 __kmp_sch_map[kind - kmp_sched_lower_ext + kmp_sched_upper_std -
2797 kmp_sched_lower - 2];
2798 }
Andrey Churbanovd454c732017-06-05 17:17:33 +00002799 if (kind == kmp_sched_auto || chunk < 1) {
Jonathan Peyton30419822017-05-12 18:01:32 +00002800 // ignore parameter chunk for schedule auto
2801 thread->th.th_current_task->td_icvs.sched.chunk = KMP_DEFAULT_CHUNK;
2802 } else {
2803 thread->th.th_current_task->td_icvs.sched.chunk = chunk;
2804 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002805}
2806
2807/* Gets def_sched_var ICV values */
Jonathan Peyton30419822017-05-12 18:01:32 +00002808void __kmp_get_schedule(int gtid, kmp_sched_t *kind, int *chunk) {
2809 kmp_info_t *thread;
2810 enum sched_type th_type;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002811
Jonathan Peyton30419822017-05-12 18:01:32 +00002812 KF_TRACE(10, ("__kmp_get_schedule: thread %d\n", gtid));
2813 KMP_DEBUG_ASSERT(__kmp_init_serial);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002814
Jonathan Peyton30419822017-05-12 18:01:32 +00002815 thread = __kmp_threads[gtid];
Jim Cownie5e8470a2013-09-27 10:38:44 +00002816
Jonathan Peyton30419822017-05-12 18:01:32 +00002817 th_type = thread->th.th_current_task->td_icvs.sched.r_sched_type;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002818
Jonathan Peyton30419822017-05-12 18:01:32 +00002819 switch (th_type) {
2820 case kmp_sch_static:
2821 case kmp_sch_static_greedy:
2822 case kmp_sch_static_balanced:
2823 *kind = kmp_sched_static;
2824 *chunk = 0; // chunk was not set, try to show this fact via zero value
2825 return;
2826 case kmp_sch_static_chunked:
2827 *kind = kmp_sched_static;
2828 break;
2829 case kmp_sch_dynamic_chunked:
2830 *kind = kmp_sched_dynamic;
2831 break;
2832 case kmp_sch_guided_chunked:
2833 case kmp_sch_guided_iterative_chunked:
2834 case kmp_sch_guided_analytical_chunked:
2835 *kind = kmp_sched_guided;
2836 break;
2837 case kmp_sch_auto:
2838 *kind = kmp_sched_auto;
2839 break;
2840 case kmp_sch_trapezoidal:
2841 *kind = kmp_sched_trapezoidal;
2842 break;
Jonathan Peytona1234cf2016-10-07 18:01:35 +00002843#if KMP_STATIC_STEAL_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00002844 case kmp_sch_static_steal:
2845 *kind = kmp_sched_static_steal;
2846 break;
Jonathan Peytona1234cf2016-10-07 18:01:35 +00002847#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00002848 default:
2849 KMP_FATAL(UnknownSchedulingType, th_type);
2850 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002851
Jonathan Peyton30419822017-05-12 18:01:32 +00002852 *chunk = thread->th.th_current_task->td_icvs.sched.chunk;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002853}
2854
Jonathan Peyton30419822017-05-12 18:01:32 +00002855int __kmp_get_ancestor_thread_num(int gtid, int level) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00002856
Jonathan Peyton30419822017-05-12 18:01:32 +00002857 int ii, dd;
2858 kmp_team_t *team;
2859 kmp_info_t *thr;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002860
Jonathan Peyton30419822017-05-12 18:01:32 +00002861 KF_TRACE(10, ("__kmp_get_ancestor_thread_num: thread %d %d\n", gtid, level));
2862 KMP_DEBUG_ASSERT(__kmp_init_serial);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002863
Jonathan Peyton30419822017-05-12 18:01:32 +00002864 // validate level
2865 if (level == 0)
2866 return 0;
2867 if (level < 0)
2868 return -1;
2869 thr = __kmp_threads[gtid];
2870 team = thr->th.th_team;
2871 ii = team->t.t_level;
2872 if (level > ii)
2873 return -1;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002874
2875#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00002876 if (thr->th.th_teams_microtask) {
2877 // AC: we are in teams region where multiple nested teams have same level
2878 int tlevel = thr->th.th_teams_level; // the level of the teams construct
2879 if (level <=
2880 tlevel) { // otherwise usual algorithm works (will not touch the teams)
2881 KMP_DEBUG_ASSERT(ii >= tlevel);
2882 // AC: As we need to pass by the teams league, we need to artificially
2883 // increase ii
2884 if (ii == tlevel) {
2885 ii += 2; // three teams have same level
2886 } else {
2887 ii++; // two teams have same level
2888 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002889 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002890 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002891#endif
2892
Jonathan Peyton30419822017-05-12 18:01:32 +00002893 if (ii == level)
2894 return __kmp_tid_from_gtid(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002895
Jonathan Peyton30419822017-05-12 18:01:32 +00002896 dd = team->t.t_serialized;
2897 level++;
2898 while (ii > level) {
2899 for (dd = team->t.t_serialized; (dd > 0) && (ii > level); dd--, ii--) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00002900 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002901 if ((team->t.t_serialized) && (!dd)) {
2902 team = team->t.t_parent;
2903 continue;
2904 }
2905 if (ii > level) {
2906 team = team->t.t_parent;
2907 dd = team->t.t_serialized;
2908 ii--;
2909 }
2910 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002911
Jonathan Peyton30419822017-05-12 18:01:32 +00002912 return (dd > 1) ? (0) : (team->t.t_master_tid);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002913}
2914
Jonathan Peyton30419822017-05-12 18:01:32 +00002915int __kmp_get_team_size(int gtid, int level) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00002916
Jonathan Peyton30419822017-05-12 18:01:32 +00002917 int ii, dd;
2918 kmp_team_t *team;
2919 kmp_info_t *thr;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002920
Jonathan Peyton30419822017-05-12 18:01:32 +00002921 KF_TRACE(10, ("__kmp_get_team_size: thread %d %d\n", gtid, level));
2922 KMP_DEBUG_ASSERT(__kmp_init_serial);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002923
Jonathan Peyton30419822017-05-12 18:01:32 +00002924 // validate level
2925 if (level == 0)
2926 return 1;
2927 if (level < 0)
2928 return -1;
2929 thr = __kmp_threads[gtid];
2930 team = thr->th.th_team;
2931 ii = team->t.t_level;
2932 if (level > ii)
2933 return -1;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002934
2935#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00002936 if (thr->th.th_teams_microtask) {
2937 // AC: we are in teams region where multiple nested teams have same level
2938 int tlevel = thr->th.th_teams_level; // the level of the teams construct
2939 if (level <=
2940 tlevel) { // otherwise usual algorithm works (will not touch the teams)
2941 KMP_DEBUG_ASSERT(ii >= tlevel);
2942 // AC: As we need to pass by the teams league, we need to artificially
2943 // increase ii
2944 if (ii == tlevel) {
2945 ii += 2; // three teams have same level
2946 } else {
2947 ii++; // two teams have same level
2948 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002949 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002950 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002951#endif
2952
Jonathan Peyton30419822017-05-12 18:01:32 +00002953 while (ii > level) {
2954 for (dd = team->t.t_serialized; (dd > 0) && (ii > level); dd--, ii--) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00002955 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002956 if (team->t.t_serialized && (!dd)) {
2957 team = team->t.t_parent;
2958 continue;
2959 }
2960 if (ii > level) {
2961 team = team->t.t_parent;
2962 ii--;
2963 }
2964 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002965
Jonathan Peyton30419822017-05-12 18:01:32 +00002966 return team->t.t_nproc;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002967}
2968
Jonathan Peyton30419822017-05-12 18:01:32 +00002969kmp_r_sched_t __kmp_get_schedule_global() {
2970 // This routine created because pairs (__kmp_sched, __kmp_chunk) and
2971 // (__kmp_static, __kmp_guided) may be changed by kmp_set_defaults
2972 // independently. So one can get the updated schedule here.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002973
Jonathan Peyton30419822017-05-12 18:01:32 +00002974 kmp_r_sched_t r_sched;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002975
Jonathan Peyton30419822017-05-12 18:01:32 +00002976 // create schedule from 4 globals: __kmp_sched, __kmp_chunk, __kmp_static,
2977 // __kmp_guided. __kmp_sched should keep original value, so that user can set
2978 // KMP_SCHEDULE multiple times, and thus have different run-time schedules in
2979 // different roots (even in OMP 2.5)
2980 if (__kmp_sched == kmp_sch_static) {
Jonathan Peytonba55a7b2017-11-29 22:47:52 +00002981 // replace STATIC with more detailed schedule (balanced or greedy)
2982 r_sched.r_sched_type = __kmp_static;
Jonathan Peyton30419822017-05-12 18:01:32 +00002983 } else if (__kmp_sched == kmp_sch_guided_chunked) {
Jonathan Peytonba55a7b2017-11-29 22:47:52 +00002984 // replace GUIDED with more detailed schedule (iterative or analytical)
2985 r_sched.r_sched_type = __kmp_guided;
2986 } else { // (STATIC_CHUNKED), or (DYNAMIC_CHUNKED), or other
2987 r_sched.r_sched_type = __kmp_sched;
Jonathan Peyton30419822017-05-12 18:01:32 +00002988 }
2989
Jonathan Peytonba55a7b2017-11-29 22:47:52 +00002990 if (__kmp_chunk < KMP_DEFAULT_CHUNK) {
2991 // __kmp_chunk may be wrong here (if it was not ever set)
Jonathan Peyton30419822017-05-12 18:01:32 +00002992 r_sched.chunk = KMP_DEFAULT_CHUNK;
2993 } else {
2994 r_sched.chunk = __kmp_chunk;
2995 }
2996
2997 return r_sched;
2998}
2999
3000/* Allocate (realloc == FALSE) * or reallocate (realloc == TRUE)
3001 at least argc number of *t_argv entries for the requested team. */
3002static void __kmp_alloc_argv_entries(int argc, kmp_team_t *team, int realloc) {
3003
3004 KMP_DEBUG_ASSERT(team);
3005 if (!realloc || argc > team->t.t_max_argc) {
3006
3007 KA_TRACE(100, ("__kmp_alloc_argv_entries: team %d: needed entries=%d, "
3008 "current entries=%d\n",
3009 team->t.t_id, argc, (realloc) ? team->t.t_max_argc : 0));
3010 /* if previously allocated heap space for args, free them */
3011 if (realloc && team->t.t_argv != &team->t.t_inline_argv[0])
3012 __kmp_free((void *)team->t.t_argv);
3013
3014 if (argc <= KMP_INLINE_ARGV_ENTRIES) {
3015 /* use unused space in the cache line for arguments */
3016 team->t.t_max_argc = KMP_INLINE_ARGV_ENTRIES;
3017 KA_TRACE(100, ("__kmp_alloc_argv_entries: team %d: inline allocate %d "
3018 "argv entries\n",
3019 team->t.t_id, team->t.t_max_argc));
3020 team->t.t_argv = &team->t.t_inline_argv[0];
3021 if (__kmp_storage_map) {
3022 __kmp_print_storage_map_gtid(
3023 -1, &team->t.t_inline_argv[0],
3024 &team->t.t_inline_argv[KMP_INLINE_ARGV_ENTRIES],
3025 (sizeof(void *) * KMP_INLINE_ARGV_ENTRIES), "team_%d.t_inline_argv",
3026 team->t.t_id);
3027 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003028 } else {
Jonathan Peyton30419822017-05-12 18:01:32 +00003029 /* allocate space for arguments in the heap */
3030 team->t.t_max_argc = (argc <= (KMP_MIN_MALLOC_ARGV_ENTRIES >> 1))
3031 ? KMP_MIN_MALLOC_ARGV_ENTRIES
3032 : 2 * argc;
3033 KA_TRACE(100, ("__kmp_alloc_argv_entries: team %d: dynamic allocate %d "
3034 "argv entries\n",
3035 team->t.t_id, team->t.t_max_argc));
3036 team->t.t_argv =
3037 (void **)__kmp_page_allocate(sizeof(void *) * team->t.t_max_argc);
3038 if (__kmp_storage_map) {
3039 __kmp_print_storage_map_gtid(-1, &team->t.t_argv[0],
3040 &team->t.t_argv[team->t.t_max_argc],
3041 sizeof(void *) * team->t.t_max_argc,
3042 "team_%d.t_argv", team->t.t_id);
3043 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003044 }
Jonathan Peyton30419822017-05-12 18:01:32 +00003045 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003046}
3047
Jonathan Peyton30419822017-05-12 18:01:32 +00003048static void __kmp_allocate_team_arrays(kmp_team_t *team, int max_nth) {
3049 int i;
3050 int num_disp_buff = max_nth > 1 ? __kmp_dispatch_num_buffers : 2;
3051 team->t.t_threads =
3052 (kmp_info_t **)__kmp_allocate(sizeof(kmp_info_t *) * max_nth);
3053 team->t.t_disp_buffer = (dispatch_shared_info_t *)__kmp_allocate(
3054 sizeof(dispatch_shared_info_t) * num_disp_buff);
3055 team->t.t_dispatch =
3056 (kmp_disp_t *)__kmp_allocate(sizeof(kmp_disp_t) * max_nth);
3057 team->t.t_implicit_task_taskdata =
3058 (kmp_taskdata_t *)__kmp_allocate(sizeof(kmp_taskdata_t) * max_nth);
3059 team->t.t_max_nproc = max_nth;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003060
Jonathan Peyton30419822017-05-12 18:01:32 +00003061 /* setup dispatch buffers */
3062 for (i = 0; i < num_disp_buff; ++i) {
3063 team->t.t_disp_buffer[i].buffer_index = i;
Jonathan Peytondf6818b2016-06-14 17:57:47 +00003064#if OMP_45_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00003065 team->t.t_disp_buffer[i].doacross_buf_idx = i;
Jonathan Peyton71909c52016-03-02 22:42:06 +00003066#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00003067 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003068}
3069
Jonathan Peyton30419822017-05-12 18:01:32 +00003070static void __kmp_free_team_arrays(kmp_team_t *team) {
3071 /* Note: this does not free the threads in t_threads (__kmp_free_threads) */
3072 int i;
3073 for (i = 0; i < team->t.t_max_nproc; ++i) {
3074 if (team->t.t_dispatch[i].th_disp_buffer != NULL) {
3075 __kmp_free(team->t.t_dispatch[i].th_disp_buffer);
3076 team->t.t_dispatch[i].th_disp_buffer = NULL;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003077 }
3078 }
Jonathan Peytonf6399362018-07-09 17:51:13 +00003079#if KMP_USE_HIER_SCHED
3080 __kmp_dispatch_free_hierarchies(team);
3081#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00003082 __kmp_free(team->t.t_threads);
3083 __kmp_free(team->t.t_disp_buffer);
3084 __kmp_free(team->t.t_dispatch);
3085 __kmp_free(team->t.t_implicit_task_taskdata);
3086 team->t.t_threads = NULL;
3087 team->t.t_disp_buffer = NULL;
3088 team->t.t_dispatch = NULL;
3089 team->t.t_implicit_task_taskdata = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003090}
3091
Jonathan Peyton30419822017-05-12 18:01:32 +00003092static void __kmp_reallocate_team_arrays(kmp_team_t *team, int max_nth) {
3093 kmp_info_t **oldThreads = team->t.t_threads;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003094
Jonathan Peyton30419822017-05-12 18:01:32 +00003095 __kmp_free(team->t.t_disp_buffer);
3096 __kmp_free(team->t.t_dispatch);
3097 __kmp_free(team->t.t_implicit_task_taskdata);
3098 __kmp_allocate_team_arrays(team, max_nth);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003099
Jonathan Peyton30419822017-05-12 18:01:32 +00003100 KMP_MEMCPY(team->t.t_threads, oldThreads,
3101 team->t.t_nproc * sizeof(kmp_info_t *));
Jim Cownie5e8470a2013-09-27 10:38:44 +00003102
Jonathan Peyton30419822017-05-12 18:01:32 +00003103 __kmp_free(oldThreads);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003104}
3105
Jonathan Peyton30419822017-05-12 18:01:32 +00003106static kmp_internal_control_t __kmp_get_global_icvs(void) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00003107
Jonathan Peyton30419822017-05-12 18:01:32 +00003108 kmp_r_sched_t r_sched =
3109 __kmp_get_schedule_global(); // get current state of scheduling globals
Jim Cownie5e8470a2013-09-27 10:38:44 +00003110
3111#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00003112 KMP_DEBUG_ASSERT(__kmp_nested_proc_bind.used > 0);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003113#endif /* OMP_40_ENABLED */
3114
Jonathan Peyton30419822017-05-12 18:01:32 +00003115 kmp_internal_control_t g_icvs = {
3116 0, // int serial_nesting_level; //corresponds to value of th_team_serialized
3117 (kmp_int8)__kmp_dflt_nested, // int nested; //internal control
3118 // for nested parallelism (per thread)
3119 (kmp_int8)__kmp_global.g.g_dynamic, // internal control for dynamic
3120 // adjustment of threads (per thread)
3121 (kmp_int8)__kmp_env_blocktime, // int bt_set; //internal control for
3122 // whether blocktime is explicitly set
3123 __kmp_dflt_blocktime, // int blocktime; //internal control for blocktime
Jonathan Peytone1c7c132016-10-07 18:12:19 +00003124#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +00003125 __kmp_bt_intervals, // int bt_intervals; //internal control for blocktime
3126// intervals
Jonathan Peytone1c7c132016-10-07 18:12:19 +00003127#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00003128 __kmp_dflt_team_nth, // int nproc; //internal control for # of threads for
3129 // next parallel region (per thread)
3130 // (use a max ub on value if __kmp_parallel_initialize not called yet)
3131 __kmp_dflt_max_active_levels, // int max_active_levels; //internal control
3132 // for max_active_levels
3133 r_sched, // kmp_r_sched_t sched; //internal control for runtime schedule
3134// {sched,chunk} pair
Jim Cownie5e8470a2013-09-27 10:38:44 +00003135#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00003136 __kmp_nested_proc_bind.bind_types[0],
3137 __kmp_default_device,
Jim Cownie5e8470a2013-09-27 10:38:44 +00003138#endif /* OMP_40_ENABLED */
Jonathan Peyton30419822017-05-12 18:01:32 +00003139 NULL // struct kmp_internal_control *next;
3140 };
Jim Cownie5e8470a2013-09-27 10:38:44 +00003141
Jonathan Peyton30419822017-05-12 18:01:32 +00003142 return g_icvs;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003143}
3144
Jonathan Peyton30419822017-05-12 18:01:32 +00003145static kmp_internal_control_t __kmp_get_x_global_icvs(const kmp_team_t *team) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00003146
Jonathan Peyton30419822017-05-12 18:01:32 +00003147 kmp_internal_control_t gx_icvs;
3148 gx_icvs.serial_nesting_level =
3149 0; // probably =team->t.t_serial like in save_inter_controls
3150 copy_icvs(&gx_icvs, &team->t.t_threads[0]->th.th_current_task->td_icvs);
3151 gx_icvs.next = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003152
Jonathan Peyton30419822017-05-12 18:01:32 +00003153 return gx_icvs;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003154}
3155
Jonathan Peyton30419822017-05-12 18:01:32 +00003156static void __kmp_initialize_root(kmp_root_t *root) {
3157 int f;
3158 kmp_team_t *root_team;
3159 kmp_team_t *hot_team;
3160 int hot_team_max_nth;
3161 kmp_r_sched_t r_sched =
3162 __kmp_get_schedule_global(); // get current state of scheduling globals
3163 kmp_internal_control_t r_icvs = __kmp_get_global_icvs();
3164 KMP_DEBUG_ASSERT(root);
3165 KMP_ASSERT(!root->r.r_begin);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003166
Jonathan Peyton30419822017-05-12 18:01:32 +00003167 /* setup the root state structure */
3168 __kmp_init_lock(&root->r.r_begin_lock);
3169 root->r.r_begin = FALSE;
3170 root->r.r_active = FALSE;
3171 root->r.r_in_parallel = 0;
3172 root->r.r_blocktime = __kmp_dflt_blocktime;
3173 root->r.r_nested = __kmp_dflt_nested;
Jonathan Peytonf4392462017-07-27 20:58:41 +00003174 root->r.r_cg_nthreads = 1;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003175
Jonathan Peyton30419822017-05-12 18:01:32 +00003176 /* setup the root team for this task */
3177 /* allocate the root team structure */
3178 KF_TRACE(10, ("__kmp_initialize_root: before root_team\n"));
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00003179
Jonathan Peyton30419822017-05-12 18:01:32 +00003180 root_team =
3181 __kmp_allocate_team(root,
3182 1, // new_nproc
3183 1, // max_nproc
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00003184#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00003185 ompt_data_none, // root parallel id
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00003186#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00003187#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00003188 __kmp_nested_proc_bind.bind_types[0],
Jim Cownie5e8470a2013-09-27 10:38:44 +00003189#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00003190 &r_icvs,
3191 0 // argc
3192 USE_NESTED_HOT_ARG(NULL) // master thread is unknown
3193 );
Jonathan Peyton8fbb49a2015-07-09 18:16:58 +00003194#if USE_DEBUGGER
Jonathan Peyton30419822017-05-12 18:01:32 +00003195 // Non-NULL value should be assigned to make the debugger display the root
3196 // team.
3197 TCW_SYNC_PTR(root_team->t.t_pkfn, (microtask_t)(~0));
Jonathan Peyton8fbb49a2015-07-09 18:16:58 +00003198#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00003199
Jonathan Peyton30419822017-05-12 18:01:32 +00003200 KF_TRACE(10, ("__kmp_initialize_root: after root_team = %p\n", root_team));
Jim Cownie5e8470a2013-09-27 10:38:44 +00003201
Jonathan Peyton30419822017-05-12 18:01:32 +00003202 root->r.r_root_team = root_team;
3203 root_team->t.t_control_stack_top = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003204
Jonathan Peyton30419822017-05-12 18:01:32 +00003205 /* initialize root team */
3206 root_team->t.t_threads[0] = NULL;
3207 root_team->t.t_nproc = 1;
3208 root_team->t.t_serialized = 1;
3209 // TODO???: root_team->t.t_max_active_levels = __kmp_dflt_max_active_levels;
Jonathan Peytonba55a7b2017-11-29 22:47:52 +00003210 root_team->t.t_sched.sched = r_sched.sched;
Jonathan Peyton30419822017-05-12 18:01:32 +00003211 KA_TRACE(
3212 20,
3213 ("__kmp_initialize_root: init root team %d arrived: join=%u, plain=%u\n",
3214 root_team->t.t_id, KMP_INIT_BARRIER_STATE, KMP_INIT_BARRIER_STATE));
Jim Cownie5e8470a2013-09-27 10:38:44 +00003215
Jonathan Peyton30419822017-05-12 18:01:32 +00003216 /* setup the hot team for this task */
3217 /* allocate the hot team structure */
3218 KF_TRACE(10, ("__kmp_initialize_root: before hot_team\n"));
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00003219
Jonathan Peyton30419822017-05-12 18:01:32 +00003220 hot_team =
3221 __kmp_allocate_team(root,
3222 1, // new_nproc
3223 __kmp_dflt_team_nth_ub * 2, // max_nproc
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00003224#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00003225 ompt_data_none, // root parallel id
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00003226#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00003227#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00003228 __kmp_nested_proc_bind.bind_types[0],
Jim Cownie5e8470a2013-09-27 10:38:44 +00003229#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00003230 &r_icvs,
3231 0 // argc
3232 USE_NESTED_HOT_ARG(NULL) // master thread is unknown
3233 );
3234 KF_TRACE(10, ("__kmp_initialize_root: after hot_team = %p\n", hot_team));
Jim Cownie5e8470a2013-09-27 10:38:44 +00003235
Jonathan Peyton30419822017-05-12 18:01:32 +00003236 root->r.r_hot_team = hot_team;
3237 root_team->t.t_control_stack_top = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003238
Jonathan Peyton30419822017-05-12 18:01:32 +00003239 /* first-time initialization */
3240 hot_team->t.t_parent = root_team;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003241
Jonathan Peyton30419822017-05-12 18:01:32 +00003242 /* initialize hot team */
3243 hot_team_max_nth = hot_team->t.t_max_nproc;
3244 for (f = 0; f < hot_team_max_nth; ++f) {
3245 hot_team->t.t_threads[f] = NULL;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003246 }
Jonathan Peyton30419822017-05-12 18:01:32 +00003247 hot_team->t.t_nproc = 1;
3248 // TODO???: hot_team->t.t_max_active_levels = __kmp_dflt_max_active_levels;
Jonathan Peytonba55a7b2017-11-29 22:47:52 +00003249 hot_team->t.t_sched.sched = r_sched.sched;
Jonathan Peyton30419822017-05-12 18:01:32 +00003250 hot_team->t.t_size_changed = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003251}
3252
3253#ifdef KMP_DEBUG
3254
Jim Cownie5e8470a2013-09-27 10:38:44 +00003255typedef struct kmp_team_list_item {
Jonathan Peyton30419822017-05-12 18:01:32 +00003256 kmp_team_p const *entry;
3257 struct kmp_team_list_item *next;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003258} kmp_team_list_item_t;
Jonathan Peyton30419822017-05-12 18:01:32 +00003259typedef kmp_team_list_item_t *kmp_team_list_t;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003260
Jonathan Peyton30419822017-05-12 18:01:32 +00003261static void __kmp_print_structure_team_accum( // Add team to list of teams.
3262 kmp_team_list_t list, // List of teams.
3263 kmp_team_p const *team // Team to add.
3264 ) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00003265
Jonathan Peyton30419822017-05-12 18:01:32 +00003266 // List must terminate with item where both entry and next are NULL.
3267 // Team is added to the list only once.
3268 // List is sorted in ascending order by team id.
3269 // Team id is *not* a key.
Jim Cownie5e8470a2013-09-27 10:38:44 +00003270
Jonathan Peyton30419822017-05-12 18:01:32 +00003271 kmp_team_list_t l;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003272
Jonathan Peyton30419822017-05-12 18:01:32 +00003273 KMP_DEBUG_ASSERT(list != NULL);
3274 if (team == NULL) {
3275 return;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003276 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003277
Jonathan Peyton30419822017-05-12 18:01:32 +00003278 __kmp_print_structure_team_accum(list, team->t.t_parent);
3279 __kmp_print_structure_team_accum(list, team->t.t_next_pool);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003280
Jonathan Peyton30419822017-05-12 18:01:32 +00003281 // Search list for the team.
3282 l = list;
3283 while (l->next != NULL && l->entry != team) {
3284 l = l->next;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003285 }
Jonathan Peyton30419822017-05-12 18:01:32 +00003286 if (l->next != NULL) {
3287 return; // Team has been added before, exit.
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003288 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003289
Jonathan Peyton30419822017-05-12 18:01:32 +00003290 // Team is not found. Search list again for insertion point.
3291 l = list;
3292 while (l->next != NULL && l->entry->t.t_id <= team->t.t_id) {
3293 l = l->next;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003294 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003295
Jonathan Peyton30419822017-05-12 18:01:32 +00003296 // Insert team.
3297 {
3298 kmp_team_list_item_t *item = (kmp_team_list_item_t *)KMP_INTERNAL_MALLOC(
3299 sizeof(kmp_team_list_item_t));
3300 *item = *l;
3301 l->entry = team;
3302 l->next = item;
3303 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003304}
3305
Jonathan Peyton30419822017-05-12 18:01:32 +00003306static void __kmp_print_structure_team(char const *title, kmp_team_p const *team
Jim Cownie5e8470a2013-09-27 10:38:44 +00003307
Jonathan Peyton30419822017-05-12 18:01:32 +00003308 ) {
3309 __kmp_printf("%s", title);
3310 if (team != NULL) {
3311 __kmp_printf("%2x %p\n", team->t.t_id, team);
3312 } else {
3313 __kmp_printf(" - (nil)\n");
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003314 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003315}
3316
Jonathan Peyton30419822017-05-12 18:01:32 +00003317static void __kmp_print_structure_thread(char const *title,
3318 kmp_info_p const *thread) {
3319 __kmp_printf("%s", title);
3320 if (thread != NULL) {
3321 __kmp_printf("%2d %p\n", thread->th.th_info.ds.ds_gtid, thread);
3322 } else {
3323 __kmp_printf(" - (nil)\n");
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003324 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003325}
3326
Jonathan Peyton30419822017-05-12 18:01:32 +00003327void __kmp_print_structure(void) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00003328
Jonathan Peyton30419822017-05-12 18:01:32 +00003329 kmp_team_list_t list;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003330
Jonathan Peyton30419822017-05-12 18:01:32 +00003331 // Initialize list of teams.
3332 list =
3333 (kmp_team_list_item_t *)KMP_INTERNAL_MALLOC(sizeof(kmp_team_list_item_t));
3334 list->entry = NULL;
3335 list->next = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003336
Jonathan Peyton30419822017-05-12 18:01:32 +00003337 __kmp_printf("\n------------------------------\nGlobal Thread "
3338 "Table\n------------------------------\n");
3339 {
3340 int gtid;
3341 for (gtid = 0; gtid < __kmp_threads_capacity; ++gtid) {
3342 __kmp_printf("%2d", gtid);
3343 if (__kmp_threads != NULL) {
3344 __kmp_printf(" %p", __kmp_threads[gtid]);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003345 }
Jonathan Peyton30419822017-05-12 18:01:32 +00003346 if (__kmp_root != NULL) {
3347 __kmp_printf(" %p", __kmp_root[gtid]);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003348 }
Jonathan Peyton30419822017-05-12 18:01:32 +00003349 __kmp_printf("\n");
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003350 }
Jonathan Peyton30419822017-05-12 18:01:32 +00003351 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003352
Jonathan Peyton30419822017-05-12 18:01:32 +00003353 // Print out __kmp_threads array.
3354 __kmp_printf("\n------------------------------\nThreads\n--------------------"
3355 "----------\n");
3356 if (__kmp_threads != NULL) {
3357 int gtid;
3358 for (gtid = 0; gtid < __kmp_threads_capacity; ++gtid) {
3359 kmp_info_t const *thread = __kmp_threads[gtid];
3360 if (thread != NULL) {
3361 __kmp_printf("GTID %2d %p:\n", gtid, thread);
3362 __kmp_printf(" Our Root: %p\n", thread->th.th_root);
3363 __kmp_print_structure_team(" Our Team: ", thread->th.th_team);
3364 __kmp_print_structure_team(" Serial Team: ",
3365 thread->th.th_serial_team);
3366 __kmp_printf(" Threads: %2d\n", thread->th.th_team_nproc);
3367 __kmp_print_structure_thread(" Master: ",
3368 thread->th.th_team_master);
3369 __kmp_printf(" Serialized?: %2d\n", thread->th.th_team_serialized);
3370 __kmp_printf(" Set NProc: %2d\n", thread->th.th_set_nproc);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003371#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00003372 __kmp_printf(" Set Proc Bind: %2d\n", thread->th.th_set_proc_bind);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003373#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00003374 __kmp_print_structure_thread(" Next in pool: ",
3375 thread->th.th_next_pool);
3376 __kmp_printf("\n");
3377 __kmp_print_structure_team_accum(list, thread->th.th_team);
3378 __kmp_print_structure_team_accum(list, thread->th.th_serial_team);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003379 }
3380 }
Jonathan Peyton30419822017-05-12 18:01:32 +00003381 } else {
3382 __kmp_printf("Threads array is not allocated.\n");
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003383 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003384
Jonathan Peyton30419822017-05-12 18:01:32 +00003385 // Print out __kmp_root array.
3386 __kmp_printf("\n------------------------------\nUbers\n----------------------"
3387 "--------\n");
3388 if (__kmp_root != NULL) {
3389 int gtid;
3390 for (gtid = 0; gtid < __kmp_threads_capacity; ++gtid) {
3391 kmp_root_t const *root = __kmp_root[gtid];
3392 if (root != NULL) {
3393 __kmp_printf("GTID %2d %p:\n", gtid, root);
3394 __kmp_print_structure_team(" Root Team: ", root->r.r_root_team);
3395 __kmp_print_structure_team(" Hot Team: ", root->r.r_hot_team);
3396 __kmp_print_structure_thread(" Uber Thread: ",
3397 root->r.r_uber_thread);
3398 __kmp_printf(" Active?: %2d\n", root->r.r_active);
3399 __kmp_printf(" Nested?: %2d\n", root->r.r_nested);
Jonathan Peyton37e2ef52018-07-09 17:36:22 +00003400 __kmp_printf(" In Parallel: %2d\n", KMP_ATOMIC_LD_RLX(&root->r.r_in_parallel));
Jonathan Peyton30419822017-05-12 18:01:32 +00003401 __kmp_printf("\n");
3402 __kmp_print_structure_team_accum(list, root->r.r_root_team);
3403 __kmp_print_structure_team_accum(list, root->r.r_hot_team);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003404 }
3405 }
Jonathan Peyton30419822017-05-12 18:01:32 +00003406 } else {
3407 __kmp_printf("Ubers array is not allocated.\n");
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003408 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003409
Jonathan Peyton30419822017-05-12 18:01:32 +00003410 __kmp_printf("\n------------------------------\nTeams\n----------------------"
3411 "--------\n");
3412 while (list->next != NULL) {
3413 kmp_team_p const *team = list->entry;
3414 int i;
3415 __kmp_printf("Team %2x %p:\n", team->t.t_id, team);
3416 __kmp_print_structure_team(" Parent Team: ", team->t.t_parent);
3417 __kmp_printf(" Master TID: %2d\n", team->t.t_master_tid);
3418 __kmp_printf(" Max threads: %2d\n", team->t.t_max_nproc);
3419 __kmp_printf(" Levels of serial: %2d\n", team->t.t_serialized);
3420 __kmp_printf(" Number threads: %2d\n", team->t.t_nproc);
3421 for (i = 0; i < team->t.t_nproc; ++i) {
3422 __kmp_printf(" Thread %2d: ", i);
3423 __kmp_print_structure_thread("", team->t.t_threads[i]);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003424 }
Jonathan Peyton30419822017-05-12 18:01:32 +00003425 __kmp_print_structure_team(" Next in pool: ", team->t.t_next_pool);
3426 __kmp_printf("\n");
3427 list = list->next;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003428 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003429
Jonathan Peyton30419822017-05-12 18:01:32 +00003430 // Print out __kmp_thread_pool and __kmp_team_pool.
3431 __kmp_printf("\n------------------------------\nPools\n----------------------"
3432 "--------\n");
3433 __kmp_print_structure_thread("Thread pool: ",
Andrey Churbanovc47afcd2017-07-03 11:24:08 +00003434 CCAST(kmp_info_t *, __kmp_thread_pool));
Jonathan Peyton30419822017-05-12 18:01:32 +00003435 __kmp_print_structure_team("Team pool: ",
Andrey Churbanovc47afcd2017-07-03 11:24:08 +00003436 CCAST(kmp_team_t *, __kmp_team_pool));
Jonathan Peyton30419822017-05-12 18:01:32 +00003437 __kmp_printf("\n");
Jim Cownie5e8470a2013-09-27 10:38:44 +00003438
Jonathan Peyton30419822017-05-12 18:01:32 +00003439 // Free team list.
3440 while (list != NULL) {
3441 kmp_team_list_item_t *item = list;
3442 list = list->next;
3443 KMP_INTERNAL_FREE(item);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003444 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003445}
3446
3447#endif
3448
Jim Cownie5e8470a2013-09-27 10:38:44 +00003449//---------------------------------------------------------------------------
3450// Stuff for per-thread fast random number generator
3451// Table of primes
Jim Cownie5e8470a2013-09-27 10:38:44 +00003452static const unsigned __kmp_primes[] = {
Jonathan Peyton30419822017-05-12 18:01:32 +00003453 0x9e3779b1, 0xffe6cc59, 0x2109f6dd, 0x43977ab5, 0xba5703f5, 0xb495a877,
3454 0xe1626741, 0x79695e6b, 0xbc98c09f, 0xd5bee2b3, 0x287488f9, 0x3af18231,
3455 0x9677cd4d, 0xbe3a6929, 0xadc6a877, 0xdcf0674b, 0xbe4d6fe9, 0x5f15e201,
3456 0x99afc3fd, 0xf3f16801, 0xe222cfff, 0x24ba5fdb, 0x0620452d, 0x79f149e3,
3457 0xc8b93f49, 0x972702cd, 0xb07dd827, 0x6c97d5ed, 0x085a3d61, 0x46eb5ea7,
3458 0x3d9910ed, 0x2e687b5b, 0x29609227, 0x6eb081f1, 0x0954c4e1, 0x9d114db9,
3459 0x542acfa9, 0xb3e6bd7b, 0x0742d917, 0xe9f3ffa7, 0x54581edb, 0xf2480f45,
3460 0x0bb9288f, 0xef1affc7, 0x85fa0ca7, 0x3ccc14db, 0xe6baf34b, 0x343377f7,
3461 0x5ca19031, 0xe6d9293b, 0xf0a9f391, 0x5d2e980b, 0xfc411073, 0xc3749363,
3462 0xb892d829, 0x3549366b, 0x629750ad, 0xb98294e5, 0x892d9483, 0xc235baf3,
3463 0x3d2402a3, 0x6bdef3c9, 0xbec333cd, 0x40c9520f};
Jim Cownie5e8470a2013-09-27 10:38:44 +00003464
3465//---------------------------------------------------------------------------
3466// __kmp_get_random: Get a random number using a linear congruential method.
Jonathan Peyton30419822017-05-12 18:01:32 +00003467unsigned short __kmp_get_random(kmp_info_t *thread) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00003468 unsigned x = thread->th.th_x;
Jonathan Peyton30419822017-05-12 18:01:32 +00003469 unsigned short r = x >> 16;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003470
Jonathan Peyton30419822017-05-12 18:01:32 +00003471 thread->th.th_x = x * thread->th.th_a + 1;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003472
3473 KA_TRACE(30, ("__kmp_get_random: THREAD: %d, RETURN: %u\n",
Jonathan Peyton30419822017-05-12 18:01:32 +00003474 thread->th.th_info.ds.ds_tid, r));
Jim Cownie5e8470a2013-09-27 10:38:44 +00003475
3476 return r;
3477}
3478//--------------------------------------------------------
3479// __kmp_init_random: Initialize a random number generator
Jonathan Peyton30419822017-05-12 18:01:32 +00003480void __kmp_init_random(kmp_info_t *thread) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00003481 unsigned seed = thread->th.th_info.ds.ds_tid;
3482
Jonathan Peyton30419822017-05-12 18:01:32 +00003483 thread->th.th_a =
3484 __kmp_primes[seed % (sizeof(__kmp_primes) / sizeof(__kmp_primes[0]))];
3485 thread->th.th_x = (seed + 1) * thread->th.th_a + 1;
3486 KA_TRACE(30,
3487 ("__kmp_init_random: THREAD: %u; A: %u\n", seed, thread->th.th_a));
Jim Cownie5e8470a2013-09-27 10:38:44 +00003488}
3489
Jim Cownie5e8470a2013-09-27 10:38:44 +00003490#if KMP_OS_WINDOWS
Jonathan Peyton30419822017-05-12 18:01:32 +00003491/* reclaim array entries for root threads that are already dead, returns number
3492 * reclaimed */
3493static int __kmp_reclaim_dead_roots(void) {
3494 int i, r = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003495
Jonathan Peyton30419822017-05-12 18:01:32 +00003496 for (i = 0; i < __kmp_threads_capacity; ++i) {
3497 if (KMP_UBER_GTID(i) &&
3498 !__kmp_still_running((kmp_info_t *)TCR_SYNC_PTR(__kmp_threads[i])) &&
3499 !__kmp_root[i]
3500 ->r.r_active) { // AC: reclaim only roots died in non-active state
3501 r += __kmp_unregister_root_other_thread(i);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003502 }
Jonathan Peyton30419822017-05-12 18:01:32 +00003503 }
3504 return r;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003505}
3506#endif
3507
Jonathan Peyton30419822017-05-12 18:01:32 +00003508/* This function attempts to create free entries in __kmp_threads and
3509 __kmp_root, and returns the number of free entries generated.
Jim Cownie5e8470a2013-09-27 10:38:44 +00003510
Jonathan Peyton30419822017-05-12 18:01:32 +00003511 For Windows* OS static library, the first mechanism used is to reclaim array
3512 entries for root threads that are already dead.
Jim Cownie5e8470a2013-09-27 10:38:44 +00003513
Jonathan Peyton30419822017-05-12 18:01:32 +00003514 On all platforms, expansion is attempted on the arrays __kmp_threads_ and
3515 __kmp_root, with appropriate update to __kmp_threads_capacity. Array
3516 capacity is increased by doubling with clipping to __kmp_tp_capacity, if
3517 threadprivate cache array has been created. Synchronization with
3518 __kmpc_threadprivate_cached is done using __kmp_tp_cached_lock.
Jim Cownie5e8470a2013-09-27 10:38:44 +00003519
Jonathan Peyton30419822017-05-12 18:01:32 +00003520 After any dead root reclamation, if the clipping value allows array expansion
Jonathan Peyton1800ece2018-01-10 18:27:01 +00003521 to result in the generation of a total of nNeed free slots, the function does
3522 that expansion. If not, nothing is done beyond the possible initial root
3523 thread reclamation.
Jim Cownie5e8470a2013-09-27 10:38:44 +00003524
Jonathan Peyton30419822017-05-12 18:01:32 +00003525 If any argument is negative, the behavior is undefined. */
Jonathan Peyton1800ece2018-01-10 18:27:01 +00003526static int __kmp_expand_threads(int nNeed) {
Jonathan Peyton30419822017-05-12 18:01:32 +00003527 int added = 0;
Andrey Churbanov9e9333a2018-03-05 18:42:01 +00003528 int minimumRequiredCapacity;
3529 int newCapacity;
3530 kmp_info_t **newThreads;
3531 kmp_root_t **newRoot;
3532
3533// All calls to __kmp_expand_threads should be under __kmp_forkjoin_lock, so
3534// resizing __kmp_threads does not need additional protection if foreign
3535// threads are present
Jim Cownie5e8470a2013-09-27 10:38:44 +00003536
Jonathan Peyton99016992015-05-26 17:32:53 +00003537#if KMP_OS_WINDOWS && !defined KMP_DYNAMIC_LIB
Jonathan Peyton30419822017-05-12 18:01:32 +00003538 /* only for Windows static library */
3539 /* reclaim array entries for root threads that are already dead */
3540 added = __kmp_reclaim_dead_roots();
Jim Cownie5e8470a2013-09-27 10:38:44 +00003541
Jonathan Peyton30419822017-05-12 18:01:32 +00003542 if (nNeed) {
3543 nNeed -= added;
3544 if (nNeed < 0)
3545 nNeed = 0;
3546 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003547#endif
Jonathan Peyton1800ece2018-01-10 18:27:01 +00003548 if (nNeed <= 0)
Jim Cownie5e8470a2013-09-27 10:38:44 +00003549 return added;
Jonathan Peyton30419822017-05-12 18:01:32 +00003550
Andrey Churbanov9e9333a2018-03-05 18:42:01 +00003551 // Note that __kmp_threads_capacity is not bounded by __kmp_max_nth. If
3552 // __kmp_max_nth is set to some value less than __kmp_sys_max_nth by the
3553 // user via KMP_DEVICE_THREAD_LIMIT, then __kmp_threads_capacity may become
3554 // > __kmp_max_nth in one of two ways:
3555 //
3556 // 1) The initialization thread (gtid = 0) exits. __kmp_threads[0]
3557 // may not be resused by another thread, so we may need to increase
3558 // __kmp_threads_capacity to __kmp_max_nth + 1.
3559 //
3560 // 2) New foreign root(s) are encountered. We always register new foreign
3561 // roots. This may cause a smaller # of threads to be allocated at
3562 // subsequent parallel regions, but the worker threads hang around (and
3563 // eventually go to sleep) and need slots in the __kmp_threads[] array.
3564 //
3565 // Anyway, that is the reason for moving the check to see if
3566 // __kmp_max_nth was exceeded into __kmp_reserve_threads()
3567 // instead of having it performed here. -BB
Jonathan Peyton30419822017-05-12 18:01:32 +00003568
Andrey Churbanov9e9333a2018-03-05 18:42:01 +00003569 KMP_DEBUG_ASSERT(__kmp_sys_max_nth >= __kmp_threads_capacity);
Jonathan Peyton30419822017-05-12 18:01:32 +00003570
Andrey Churbanov9e9333a2018-03-05 18:42:01 +00003571 /* compute expansion headroom to check if we can expand */
3572 if (__kmp_sys_max_nth - __kmp_threads_capacity < nNeed) {
3573 /* possible expansion too small -- give up */
3574 return added;
Jonathan Peyton30419822017-05-12 18:01:32 +00003575 }
Andrey Churbanov9e9333a2018-03-05 18:42:01 +00003576 minimumRequiredCapacity = __kmp_threads_capacity + nNeed;
3577
3578 newCapacity = __kmp_threads_capacity;
3579 do {
3580 newCapacity = newCapacity <= (__kmp_sys_max_nth >> 1) ? (newCapacity << 1)
3581 : __kmp_sys_max_nth;
3582 } while (newCapacity < minimumRequiredCapacity);
3583 newThreads = (kmp_info_t **)__kmp_allocate(
3584 (sizeof(kmp_info_t *) + sizeof(kmp_root_t *)) * newCapacity + CACHE_LINE);
3585 newRoot =
3586 (kmp_root_t **)((char *)newThreads + sizeof(kmp_info_t *) * newCapacity);
3587 KMP_MEMCPY(newThreads, __kmp_threads,
3588 __kmp_threads_capacity * sizeof(kmp_info_t *));
3589 KMP_MEMCPY(newRoot, __kmp_root,
3590 __kmp_threads_capacity * sizeof(kmp_root_t *));
3591
3592 kmp_info_t **temp_threads = __kmp_threads;
3593 *(kmp_info_t * *volatile *)&__kmp_threads = newThreads;
3594 *(kmp_root_t * *volatile *)&__kmp_root = newRoot;
3595 __kmp_free(temp_threads);
3596 added += newCapacity - __kmp_threads_capacity;
3597 *(volatile int *)&__kmp_threads_capacity = newCapacity;
3598
3599 if (newCapacity > __kmp_tp_capacity) {
3600 __kmp_acquire_bootstrap_lock(&__kmp_tp_cached_lock);
3601 if (__kmp_tp_cached && newCapacity > __kmp_tp_capacity) {
3602 __kmp_threadprivate_resize_cache(newCapacity);
3603 } else { // increase __kmp_tp_capacity to correspond with kmp_threads size
3604 *(volatile int *)&__kmp_tp_capacity = newCapacity;
3605 }
3606 __kmp_release_bootstrap_lock(&__kmp_tp_cached_lock);
3607 }
3608
Jonathan Peyton30419822017-05-12 18:01:32 +00003609 return added;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003610}
3611
Jonathan Peyton30419822017-05-12 18:01:32 +00003612/* Register the current thread as a root thread and obtain our gtid. We must
3613 have the __kmp_initz_lock held at this point. Argument TRUE only if are the
3614 thread that calls from __kmp_do_serial_initialize() */
3615int __kmp_register_root(int initial_thread) {
3616 kmp_info_t *root_thread;
3617 kmp_root_t *root;
3618 int gtid;
3619 int capacity;
3620 __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock);
3621 KA_TRACE(20, ("__kmp_register_root: entered\n"));
3622 KMP_MB();
Jim Cownie5e8470a2013-09-27 10:38:44 +00003623
Jonathan Peyton30419822017-05-12 18:01:32 +00003624 /* 2007-03-02:
3625 If initial thread did not invoke OpenMP RTL yet, and this thread is not an
3626 initial one, "__kmp_all_nth >= __kmp_threads_capacity" condition does not
3627 work as expected -- it may return false (that means there is at least one
3628 empty slot in __kmp_threads array), but it is possible the only free slot
3629 is #0, which is reserved for initial thread and so cannot be used for this
3630 one. Following code workarounds this bug.
Jim Cownie5e8470a2013-09-27 10:38:44 +00003631
Jonathan Peyton30419822017-05-12 18:01:32 +00003632 However, right solution seems to be not reserving slot #0 for initial
3633 thread because:
3634 (1) there is no magic in slot #0,
3635 (2) we cannot detect initial thread reliably (the first thread which does
3636 serial initialization may be not a real initial thread).
3637 */
3638 capacity = __kmp_threads_capacity;
3639 if (!initial_thread && TCR_PTR(__kmp_threads[0]) == NULL) {
3640 --capacity;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003641 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003642
Jonathan Peyton30419822017-05-12 18:01:32 +00003643 /* see if there are too many threads */
Jonathan Peyton1800ece2018-01-10 18:27:01 +00003644 if (__kmp_all_nth >= capacity && !__kmp_expand_threads(1)) {
Jonathan Peyton30419822017-05-12 18:01:32 +00003645 if (__kmp_tp_cached) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +00003646 __kmp_fatal(KMP_MSG(CantRegisterNewThread),
3647 KMP_HNT(Set_ALL_THREADPRIVATE, __kmp_tp_capacity),
3648 KMP_HNT(PossibleSystemLimitOnThreads), __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +00003649 } else {
Jonathan Peyton6a393f72017-09-05 15:43:58 +00003650 __kmp_fatal(KMP_MSG(CantRegisterNewThread), KMP_HNT(SystemLimitOnThreads),
3651 __kmp_msg_null);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003652 }
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003653 }
Jonathan Peyton30419822017-05-12 18:01:32 +00003654
3655 /* find an available thread slot */
3656 /* Don't reassign the zero slot since we need that to only be used by initial
3657 thread */
3658 for (gtid = (initial_thread ? 0 : 1); TCR_PTR(__kmp_threads[gtid]) != NULL;
3659 gtid++)
3660 ;
3661 KA_TRACE(1,
3662 ("__kmp_register_root: found slot in threads array: T#%d\n", gtid));
3663 KMP_ASSERT(gtid < __kmp_threads_capacity);
3664
3665 /* update global accounting */
3666 __kmp_all_nth++;
3667 TCW_4(__kmp_nth, __kmp_nth + 1);
3668
3669 // if __kmp_adjust_gtid_mode is set, then we use method #1 (sp search) for low
3670 // numbers of procs, and method #2 (keyed API call) for higher numbers.
3671 if (__kmp_adjust_gtid_mode) {
3672 if (__kmp_all_nth >= __kmp_tls_gtid_min) {
3673 if (TCR_4(__kmp_gtid_mode) != 2) {
3674 TCW_4(__kmp_gtid_mode, 2);
3675 }
3676 } else {
3677 if (TCR_4(__kmp_gtid_mode) != 1) {
3678 TCW_4(__kmp_gtid_mode, 1);
3679 }
3680 }
3681 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003682
3683#ifdef KMP_ADJUST_BLOCKTIME
Jonathan Peyton30419822017-05-12 18:01:32 +00003684 /* Adjust blocktime to zero if necessary */
3685 /* Middle initialization might not have occurred yet */
3686 if (!__kmp_env_blocktime && (__kmp_avail_proc > 0)) {
3687 if (__kmp_nth > __kmp_avail_proc) {
3688 __kmp_zero_bt = TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003689 }
Jonathan Peyton30419822017-05-12 18:01:32 +00003690 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003691#endif /* KMP_ADJUST_BLOCKTIME */
3692
Jonathan Peyton30419822017-05-12 18:01:32 +00003693 /* setup this new hierarchy */
3694 if (!(root = __kmp_root[gtid])) {
3695 root = __kmp_root[gtid] = (kmp_root_t *)__kmp_allocate(sizeof(kmp_root_t));
3696 KMP_DEBUG_ASSERT(!root->r.r_root_team);
3697 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003698
Jonathan Peyton5375fe82016-11-14 21:13:44 +00003699#if KMP_STATS_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00003700 // Initialize stats as soon as possible (right after gtid assignment).
3701 __kmp_stats_thread_ptr = __kmp_stats_list->push_back(gtid);
3702 KMP_START_EXPLICIT_TIMER(OMP_worker_thread_life);
3703 KMP_SET_THREAD_STATE(SERIAL_REGION);
3704 KMP_INIT_PARTITIONED_TIMERS(OMP_serial);
Jonathan Peyton5375fe82016-11-14 21:13:44 +00003705#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00003706 __kmp_initialize_root(root);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003707
Jonathan Peyton30419822017-05-12 18:01:32 +00003708 /* setup new root thread structure */
3709 if (root->r.r_uber_thread) {
3710 root_thread = root->r.r_uber_thread;
3711 } else {
3712 root_thread = (kmp_info_t *)__kmp_allocate(sizeof(kmp_info_t));
3713 if (__kmp_storage_map) {
3714 __kmp_print_thread_storage_map(root_thread, gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003715 }
Jonathan Peyton30419822017-05-12 18:01:32 +00003716 root_thread->th.th_info.ds.ds_gtid = gtid;
Joachim Protze82e94a52017-11-01 10:08:30 +00003717#if OMPT_SUPPORT
3718 root_thread->th.ompt_thread_info.thread_data.ptr = NULL;
3719#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00003720 root_thread->th.th_root = root;
3721 if (__kmp_env_consistency_check) {
3722 root_thread->th.th_cons = __kmp_allocate_cons_stack(gtid);
3723 }
3724#if USE_FAST_MEMORY
3725 __kmp_initialize_fast_memory(root_thread);
3726#endif /* USE_FAST_MEMORY */
Jim Cownie5e8470a2013-09-27 10:38:44 +00003727
Jonathan Peyton30419822017-05-12 18:01:32 +00003728#if KMP_USE_BGET
3729 KMP_DEBUG_ASSERT(root_thread->th.th_local.bget_data == NULL);
3730 __kmp_initialize_bget(root_thread);
3731#endif
3732 __kmp_init_random(root_thread); // Initialize random number generator
3733 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00003734
Jonathan Peyton30419822017-05-12 18:01:32 +00003735 /* setup the serial team held in reserve by the root thread */
3736 if (!root_thread->th.th_serial_team) {
3737 kmp_internal_control_t r_icvs = __kmp_get_global_icvs();
3738 KF_TRACE(10, ("__kmp_register_root: before serial_team\n"));
3739 root_thread->th.th_serial_team =
3740 __kmp_allocate_team(root, 1, 1,
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00003741#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00003742 ompt_data_none, // root parallel id
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00003743#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00003744#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00003745 proc_bind_default,
Jim Cownie5e8470a2013-09-27 10:38:44 +00003746#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00003747 &r_icvs, 0 USE_NESTED_HOT_ARG(NULL));
3748 }
3749 KMP_ASSERT(root_thread->th.th_serial_team);
3750 KF_TRACE(10, ("__kmp_register_root: after serial_team = %p\n",
3751 root_thread->th.th_serial_team));
Jim Cownie5e8470a2013-09-27 10:38:44 +00003752
Jonathan Peyton30419822017-05-12 18:01:32 +00003753 /* drop root_thread into place */
3754 TCW_SYNC_PTR(__kmp_threads[gtid], root_thread);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003755
Jonathan Peyton30419822017-05-12 18:01:32 +00003756 root->r.r_root_team->t.t_threads[0] = root_thread;
3757 root->r.r_hot_team->t.t_threads[0] = root_thread;
3758 root_thread->th.th_serial_team->t.t_threads[0] = root_thread;
3759 // AC: the team created in reserve, not for execution (it is unused for now).
3760 root_thread->th.th_serial_team->t.t_serialized = 0;
3761 root->r.r_uber_thread = root_thread;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003762
Jonathan Peyton30419822017-05-12 18:01:32 +00003763 /* initialize the thread, get it ready to go */
3764 __kmp_initialize_info(root_thread, root->r.r_root_team, 0, gtid);
3765 TCW_4(__kmp_init_gtid, TRUE);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003766
Jonathan Peyton30419822017-05-12 18:01:32 +00003767 /* prepare the master thread for get_gtid() */
3768 __kmp_gtid_set_specific(gtid);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00003769
Jonathan Peyton7abf9d52016-05-26 18:19:10 +00003770#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00003771 __kmp_itt_thread_name(gtid);
Jonathan Peyton7abf9d52016-05-26 18:19:10 +00003772#endif /* USE_ITT_BUILD */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00003773
Jonathan Peyton30419822017-05-12 18:01:32 +00003774#ifdef KMP_TDATA_GTID
3775 __kmp_gtid = gtid;
Jonathan Peyton8fbb49a2015-07-09 18:16:58 +00003776#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00003777 __kmp_create_worker(gtid, root_thread, __kmp_stksize);
3778 KMP_DEBUG_ASSERT(__kmp_gtid_get_specific() == gtid);
3779
3780 KA_TRACE(20, ("__kmp_register_root: T#%d init T#%d(%d:%d) arrived: join=%u, "
3781 "plain=%u\n",
3782 gtid, __kmp_gtid_from_tid(0, root->r.r_hot_team),
3783 root->r.r_hot_team->t.t_id, 0, KMP_INIT_BARRIER_STATE,
3784 KMP_INIT_BARRIER_STATE));
3785 { // Initialize barrier data.
3786 int b;
3787 for (b = 0; b < bs_last_barrier; ++b) {
3788 root_thread->th.th_bar[b].bb.b_arrived = KMP_INIT_BARRIER_STATE;
3789#if USE_DEBUGGER
3790 root_thread->th.th_bar[b].bb.b_worker_arrived = 0;
3791#endif
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003792 }
Jonathan Peyton30419822017-05-12 18:01:32 +00003793 }
3794 KMP_DEBUG_ASSERT(root->r.r_hot_team->t.t_bar[bs_forkjoin_barrier].b_arrived ==
3795 KMP_INIT_BARRIER_STATE);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003796
Alp Toker763b9392014-02-28 09:42:41 +00003797#if KMP_AFFINITY_SUPPORTED
Jonathan Peyton30419822017-05-12 18:01:32 +00003798#if OMP_40_ENABLED
3799 root_thread->th.th_current_place = KMP_PLACE_UNDEFINED;
3800 root_thread->th.th_new_place = KMP_PLACE_UNDEFINED;
3801 root_thread->th.th_first_place = KMP_PLACE_UNDEFINED;
3802 root_thread->th.th_last_place = KMP_PLACE_UNDEFINED;
3803#endif
Jonathan Peyton2f7c0772016-02-25 18:49:52 +00003804
Jonathan Peyton30419822017-05-12 18:01:32 +00003805 if (TCR_4(__kmp_init_middle)) {
3806 __kmp_affinity_set_init_mask(gtid, TRUE);
3807 }
Alp Toker763b9392014-02-28 09:42:41 +00003808#endif /* KMP_AFFINITY_SUPPORTED */
Jim Cownie5e8470a2013-09-27 10:38:44 +00003809
Jonathan Peyton30419822017-05-12 18:01:32 +00003810 __kmp_root_counter++;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003811
Joachim Protze82e94a52017-11-01 10:08:30 +00003812#if OMPT_SUPPORT
3813 if (!initial_thread && ompt_enabled.enabled) {
3814
3815 ompt_thread_t *root_thread = ompt_get_thread();
3816
3817 ompt_set_thread_state(root_thread, omp_state_overhead);
3818
3819 if (ompt_enabled.ompt_callback_thread_begin) {
3820 ompt_callbacks.ompt_callback(ompt_callback_thread_begin)(
3821 ompt_thread_initial, __ompt_get_thread_data_internal());
3822 }
3823 ompt_data_t *task_data;
3824 __ompt_get_task_info_internal(0, NULL, &task_data, NULL, NULL, NULL);
3825 if (ompt_enabled.ompt_callback_task_create) {
3826 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
3827 NULL, NULL, task_data, ompt_task_initial, 0, NULL);
3828 // initial task has nothing to return to
3829 }
3830
3831 ompt_set_thread_state(root_thread, omp_state_work_serial);
3832 }
3833#endif
3834
Jonathan Peyton30419822017-05-12 18:01:32 +00003835 KMP_MB();
3836 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003837
Jonathan Peyton30419822017-05-12 18:01:32 +00003838 return gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003839}
3840
Jim Cownie4cc4bb42014-10-07 16:25:50 +00003841#if KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00003842static int __kmp_free_hot_teams(kmp_root_t *root, kmp_info_t *thr, int level,
3843 const int max_level) {
3844 int i, n, nth;
3845 kmp_hot_team_ptr_t *hot_teams = thr->th.th_hot_teams;
3846 if (!hot_teams || !hot_teams[level].hot_team) {
3847 return 0;
3848 }
3849 KMP_DEBUG_ASSERT(level < max_level);
3850 kmp_team_t *team = hot_teams[level].hot_team;
3851 nth = hot_teams[level].hot_team_nth;
3852 n = nth - 1; // master is not freed
3853 if (level < max_level - 1) {
3854 for (i = 0; i < nth; ++i) {
3855 kmp_info_t *th = team->t.t_threads[i];
3856 n += __kmp_free_hot_teams(root, th, level + 1, max_level);
3857 if (i > 0 && th->th.th_hot_teams) {
3858 __kmp_free(th->th.th_hot_teams);
3859 th->th.th_hot_teams = NULL;
3860 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00003861 }
Jonathan Peyton30419822017-05-12 18:01:32 +00003862 }
3863 __kmp_free_team(root, team, NULL);
3864 return n;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00003865}
3866#endif
3867
Jonathan Peyton30419822017-05-12 18:01:32 +00003868// Resets a root thread and clear its root and hot teams.
3869// Returns the number of __kmp_threads entries directly and indirectly freed.
3870static int __kmp_reset_root(int gtid, kmp_root_t *root) {
3871 kmp_team_t *root_team = root->r.r_root_team;
3872 kmp_team_t *hot_team = root->r.r_hot_team;
3873 int n = hot_team->t.t_nproc;
3874 int i;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003875
Jonathan Peyton30419822017-05-12 18:01:32 +00003876 KMP_DEBUG_ASSERT(!root->r.r_active);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003877
Jonathan Peyton30419822017-05-12 18:01:32 +00003878 root->r.r_root_team = NULL;
3879 root->r.r_hot_team = NULL;
3880 // __kmp_free_team() does not free hot teams, so we have to clear r_hot_team
3881 // before call to __kmp_free_team().
3882 __kmp_free_team(root, root_team USE_NESTED_HOT_ARG(NULL));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00003883#if KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00003884 if (__kmp_hot_teams_max_level >
3885 0) { // need to free nested hot teams and their threads if any
3886 for (i = 0; i < hot_team->t.t_nproc; ++i) {
3887 kmp_info_t *th = hot_team->t.t_threads[i];
3888 if (__kmp_hot_teams_max_level > 1) {
3889 n += __kmp_free_hot_teams(root, th, 1, __kmp_hot_teams_max_level);
3890 }
3891 if (th->th.th_hot_teams) {
3892 __kmp_free(th->th.th_hot_teams);
3893 th->th.th_hot_teams = NULL;
3894 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00003895 }
Jonathan Peyton30419822017-05-12 18:01:32 +00003896 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00003897#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00003898 __kmp_free_team(root, hot_team USE_NESTED_HOT_ARG(NULL));
Jim Cownie5e8470a2013-09-27 10:38:44 +00003899
Jonathan Peyton30419822017-05-12 18:01:32 +00003900 // Before we can reap the thread, we need to make certain that all other
3901 // threads in the teams that had this root as ancestor have stopped trying to
3902 // steal tasks.
3903 if (__kmp_tasking_mode != tskm_immediate_exec) {
3904 __kmp_wait_to_unref_task_teams();
3905 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003906
Jonathan Peyton30419822017-05-12 18:01:32 +00003907#if KMP_OS_WINDOWS
3908 /* Close Handle of root duplicated in __kmp_create_worker (tr #62919) */
3909 KA_TRACE(
3910 10, ("__kmp_reset_root: free handle, th = %p, handle = %" KMP_UINTPTR_SPEC
3911 "\n",
3912 (LPVOID) & (root->r.r_uber_thread->th),
3913 root->r.r_uber_thread->th.th_info.ds.ds_thread));
3914 __kmp_free_handle(root->r.r_uber_thread->th.th_info.ds.ds_thread);
3915#endif /* KMP_OS_WINDOWS */
Jim Cownie5e8470a2013-09-27 10:38:44 +00003916
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00003917#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00003918 if (ompt_enabled.ompt_callback_thread_end) {
3919 ompt_callbacks.ompt_callback(ompt_callback_thread_end)(
3920 &(root->r.r_uber_thread->th.ompt_thread_info.thread_data));
Jonathan Peyton30419822017-05-12 18:01:32 +00003921 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00003922#endif
3923
Jonathan Peyton30419822017-05-12 18:01:32 +00003924 TCW_4(__kmp_nth,
3925 __kmp_nth - 1); // __kmp_reap_thread will decrement __kmp_all_nth.
Jonathan Peytonf4392462017-07-27 20:58:41 +00003926 root->r.r_cg_nthreads--;
3927
Jonathan Peyton30419822017-05-12 18:01:32 +00003928 __kmp_reap_thread(root->r.r_uber_thread, 1);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003929
Jonathan Peyton30419822017-05-12 18:01:32 +00003930 // We canot put root thread to __kmp_thread_pool, so we have to reap it istead
3931 // of freeing.
3932 root->r.r_uber_thread = NULL;
3933 /* mark root as no longer in use */
3934 root->r.r_begin = FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003935
Jonathan Peyton30419822017-05-12 18:01:32 +00003936 return n;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003937}
3938
Jonathan Peyton30419822017-05-12 18:01:32 +00003939void __kmp_unregister_root_current_thread(int gtid) {
3940 KA_TRACE(1, ("__kmp_unregister_root_current_thread: enter T#%d\n", gtid));
3941 /* this lock should be ok, since unregister_root_current_thread is never
3942 called during an abort, only during a normal close. furthermore, if you
3943 have the forkjoin lock, you should never try to get the initz lock */
3944 __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock);
3945 if (TCR_4(__kmp_global.g.g_done) || !__kmp_init_serial) {
3946 KC_TRACE(10, ("__kmp_unregister_root_current_thread: already finished, "
3947 "exiting T#%d\n",
3948 gtid));
3949 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
3950 return;
3951 }
3952 kmp_root_t *root = __kmp_root[gtid];
Jim Cownie77c2a632014-09-03 11:34:33 +00003953
Jonathan Peyton30419822017-05-12 18:01:32 +00003954 KMP_DEBUG_ASSERT(__kmp_threads && __kmp_threads[gtid]);
3955 KMP_ASSERT(KMP_UBER_GTID(gtid));
3956 KMP_ASSERT(root == __kmp_threads[gtid]->th.th_root);
3957 KMP_ASSERT(root->r.r_active == FALSE);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003958
Jonathan Peyton30419822017-05-12 18:01:32 +00003959 KMP_MB();
Jim Cownie5e8470a2013-09-27 10:38:44 +00003960
Jonathan Peytondf6818b2016-06-14 17:57:47 +00003961#if OMP_45_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00003962 kmp_info_t *thread = __kmp_threads[gtid];
3963 kmp_team_t *team = thread->th.th_team;
3964 kmp_task_team_t *task_team = thread->th.th_task_team;
Andrey Churbanov535b6fa2015-05-07 17:41:51 +00003965
Jonathan Peyton30419822017-05-12 18:01:32 +00003966 // we need to wait for the proxy tasks before finishing the thread
3967 if (task_team != NULL && task_team->tt.tt_found_proxy_tasks) {
Jonathan Peyton6d247f72015-09-10 21:33:50 +00003968#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00003969 // the runtime is shutting down so we won't report any events
Joachim Protze82e94a52017-11-01 10:08:30 +00003970 thread->th.ompt_thread_info.state = omp_state_undefined;
Jonathan Peyton6d247f72015-09-10 21:33:50 +00003971#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00003972 __kmp_task_team_wait(thread, team USE_ITT_BUILD_ARG(NULL));
3973 }
Andrey Churbanov535b6fa2015-05-07 17:41:51 +00003974#endif
3975
Jonathan Peyton30419822017-05-12 18:01:32 +00003976 __kmp_reset_root(gtid, root);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003977
Jonathan Peyton30419822017-05-12 18:01:32 +00003978 /* free up this thread slot */
3979 __kmp_gtid_set_specific(KMP_GTID_DNE);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003980#ifdef KMP_TDATA_GTID
Jonathan Peyton30419822017-05-12 18:01:32 +00003981 __kmp_gtid = KMP_GTID_DNE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003982#endif
3983
Jonathan Peyton30419822017-05-12 18:01:32 +00003984 KMP_MB();
3985 KC_TRACE(10,
3986 ("__kmp_unregister_root_current_thread: T#%d unregistered\n", gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00003987
Jonathan Peyton30419822017-05-12 18:01:32 +00003988 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003989}
3990
Jonathan Peyton2321d572015-06-08 19:25:25 +00003991#if KMP_OS_WINDOWS
Jim Cownie5e8470a2013-09-27 10:38:44 +00003992/* __kmp_forkjoin_lock must be already held
Jonathan Peyton30419822017-05-12 18:01:32 +00003993 Unregisters a root thread that is not the current thread. Returns the number
3994 of __kmp_threads entries freed as a result. */
3995static int __kmp_unregister_root_other_thread(int gtid) {
3996 kmp_root_t *root = __kmp_root[gtid];
3997 int r;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003998
Jonathan Peyton30419822017-05-12 18:01:32 +00003999 KA_TRACE(1, ("__kmp_unregister_root_other_thread: enter T#%d\n", gtid));
4000 KMP_DEBUG_ASSERT(__kmp_threads && __kmp_threads[gtid]);
4001 KMP_ASSERT(KMP_UBER_GTID(gtid));
4002 KMP_ASSERT(root == __kmp_threads[gtid]->th.th_root);
4003 KMP_ASSERT(root->r.r_active == FALSE);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004004
Jonathan Peyton30419822017-05-12 18:01:32 +00004005 r = __kmp_reset_root(gtid, root);
4006 KC_TRACE(10,
4007 ("__kmp_unregister_root_other_thread: T#%d unregistered\n", gtid));
4008 return r;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004009}
Jonathan Peyton2321d572015-06-08 19:25:25 +00004010#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00004011
Jim Cownie5e8470a2013-09-27 10:38:44 +00004012#if KMP_DEBUG
4013void __kmp_task_info() {
4014
Jonathan Peyton30419822017-05-12 18:01:32 +00004015 kmp_int32 gtid = __kmp_entry_gtid();
4016 kmp_int32 tid = __kmp_tid_from_gtid(gtid);
4017 kmp_info_t *this_thr = __kmp_threads[gtid];
4018 kmp_team_t *steam = this_thr->th.th_serial_team;
4019 kmp_team_t *team = this_thr->th.th_team;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004020
Jonathan Peyton30419822017-05-12 18:01:32 +00004021 __kmp_printf("__kmp_task_info: gtid=%d tid=%d t_thread=%p team=%p curtask=%p "
4022 "ptask=%p\n",
4023 gtid, tid, this_thr, team, this_thr->th.th_current_task,
4024 team->t.t_implicit_task_taskdata[tid].td_parent);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004025}
4026#endif // KMP_DEBUG
4027
Jonathan Peyton30419822017-05-12 18:01:32 +00004028/* TODO optimize with one big memclr, take out what isn't needed, split
4029 responsibility to workers as much as possible, and delay initialization of
4030 features as much as possible */
4031static void __kmp_initialize_info(kmp_info_t *this_thr, kmp_team_t *team,
4032 int tid, int gtid) {
4033 /* this_thr->th.th_info.ds.ds_gtid is setup in
4034 kmp_allocate_thread/create_worker.
4035 this_thr->th.th_serial_team is setup in __kmp_allocate_thread */
4036 kmp_info_t *master = team->t.t_threads[0];
4037 KMP_DEBUG_ASSERT(this_thr != NULL);
4038 KMP_DEBUG_ASSERT(this_thr->th.th_serial_team);
4039 KMP_DEBUG_ASSERT(team);
4040 KMP_DEBUG_ASSERT(team->t.t_threads);
4041 KMP_DEBUG_ASSERT(team->t.t_dispatch);
4042 KMP_DEBUG_ASSERT(master);
4043 KMP_DEBUG_ASSERT(master->th.th_root);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004044
Jonathan Peyton30419822017-05-12 18:01:32 +00004045 KMP_MB();
Jim Cownie5e8470a2013-09-27 10:38:44 +00004046
Jonathan Peyton30419822017-05-12 18:01:32 +00004047 TCW_SYNC_PTR(this_thr->th.th_team, team);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004048
Jonathan Peyton30419822017-05-12 18:01:32 +00004049 this_thr->th.th_info.ds.ds_tid = tid;
4050 this_thr->th.th_set_nproc = 0;
4051 if (__kmp_tasking_mode != tskm_immediate_exec)
4052 // When tasking is possible, threads are not safe to reap until they are
4053 // done tasking; this will be set when tasking code is exited in wait
4054 this_thr->th.th_reap_state = KMP_NOT_SAFE_TO_REAP;
4055 else // no tasking --> always safe to reap
4056 this_thr->th.th_reap_state = KMP_SAFE_TO_REAP;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004057#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00004058 this_thr->th.th_set_proc_bind = proc_bind_default;
4059#if KMP_AFFINITY_SUPPORTED
4060 this_thr->th.th_new_place = this_thr->th.th_current_place;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004061#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00004062#endif
4063 this_thr->th.th_root = master->th.th_root;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004064
Jonathan Peyton30419822017-05-12 18:01:32 +00004065 /* setup the thread's cache of the team structure */
4066 this_thr->th.th_team_nproc = team->t.t_nproc;
4067 this_thr->th.th_team_master = master;
4068 this_thr->th.th_team_serialized = team->t.t_serialized;
4069 TCW_PTR(this_thr->th.th_sleep_loc, NULL);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004070
Jonathan Peyton30419822017-05-12 18:01:32 +00004071 KMP_DEBUG_ASSERT(team->t.t_implicit_task_taskdata);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004072
Jonathan Peyton30419822017-05-12 18:01:32 +00004073 KF_TRACE(10, ("__kmp_initialize_info1: T#%d:%d this_thread=%p curtask=%p\n",
4074 tid, gtid, this_thr, this_thr->th.th_current_task));
Jim Cownie5e8470a2013-09-27 10:38:44 +00004075
Jonathan Peyton30419822017-05-12 18:01:32 +00004076 __kmp_init_implicit_task(this_thr->th.th_team_master->th.th_ident, this_thr,
4077 team, tid, TRUE);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004078
Jonathan Peyton30419822017-05-12 18:01:32 +00004079 KF_TRACE(10, ("__kmp_initialize_info2: T#%d:%d this_thread=%p curtask=%p\n",
4080 tid, gtid, this_thr, this_thr->th.th_current_task));
4081 // TODO: Initialize ICVs from parent; GEH - isn't that already done in
4082 // __kmp_initialize_team()?
Jim Cownie5e8470a2013-09-27 10:38:44 +00004083
Jonathan Peyton30419822017-05-12 18:01:32 +00004084 /* TODO no worksharing in speculative threads */
4085 this_thr->th.th_dispatch = &team->t.t_dispatch[tid];
Jim Cownie5e8470a2013-09-27 10:38:44 +00004086
Jonathan Peyton30419822017-05-12 18:01:32 +00004087 this_thr->th.th_local.this_construct = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004088
Jonathan Peyton30419822017-05-12 18:01:32 +00004089 if (!this_thr->th.th_pri_common) {
4090 this_thr->th.th_pri_common =
4091 (struct common_table *)__kmp_allocate(sizeof(struct common_table));
4092 if (__kmp_storage_map) {
4093 __kmp_print_storage_map_gtid(
4094 gtid, this_thr->th.th_pri_common, this_thr->th.th_pri_common + 1,
4095 sizeof(struct common_table), "th_%d.th_pri_common\n", gtid);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00004096 }
Jonathan Peyton30419822017-05-12 18:01:32 +00004097 this_thr->th.th_pri_head = NULL;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00004098 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00004099
Jonathan Peyton30419822017-05-12 18:01:32 +00004100 /* Initialize dynamic dispatch */
4101 {
4102 volatile kmp_disp_t *dispatch = this_thr->th.th_dispatch;
4103 // Use team max_nproc since this will never change for the team.
4104 size_t disp_size =
4105 sizeof(dispatch_private_info_t) *
4106 (team->t.t_max_nproc == 1 ? 1 : __kmp_dispatch_num_buffers);
4107 KD_TRACE(10, ("__kmp_initialize_info: T#%d max_nproc: %d\n", gtid,
4108 team->t.t_max_nproc));
4109 KMP_ASSERT(dispatch);
4110 KMP_DEBUG_ASSERT(team->t.t_dispatch);
4111 KMP_DEBUG_ASSERT(dispatch == &team->t.t_dispatch[tid]);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004112
Jonathan Peyton30419822017-05-12 18:01:32 +00004113 dispatch->th_disp_index = 0;
Jonathan Peytondf6818b2016-06-14 17:57:47 +00004114#if OMP_45_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00004115 dispatch->th_doacross_buf_idx = 0;
Jonathan Peyton71909c52016-03-02 22:42:06 +00004116#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00004117 if (!dispatch->th_disp_buffer) {
4118 dispatch->th_disp_buffer =
4119 (dispatch_private_info_t *)__kmp_allocate(disp_size);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004120
Jonathan Peyton30419822017-05-12 18:01:32 +00004121 if (__kmp_storage_map) {
4122 __kmp_print_storage_map_gtid(
4123 gtid, &dispatch->th_disp_buffer[0],
4124 &dispatch->th_disp_buffer[team->t.t_max_nproc == 1
4125 ? 1
4126 : __kmp_dispatch_num_buffers],
4127 disp_size, "th_%d.th_dispatch.th_disp_buffer "
4128 "(team_%d.t_dispatch[%d].th_disp_buffer)",
4129 gtid, team->t.t_id, gtid);
4130 }
4131 } else {
4132 memset(&dispatch->th_disp_buffer[0], '\0', disp_size);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004133 }
4134
Jonathan Peyton30419822017-05-12 18:01:32 +00004135 dispatch->th_dispatch_pr_current = 0;
4136 dispatch->th_dispatch_sh_current = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004137
Jonathan Peyton30419822017-05-12 18:01:32 +00004138 dispatch->th_deo_fcn = 0; /* ORDERED */
4139 dispatch->th_dxo_fcn = 0; /* END ORDERED */
4140 }
Andrey Churbanov6d224db2015-02-10 18:37:43 +00004141
Jonathan Peyton30419822017-05-12 18:01:32 +00004142 this_thr->th.th_next_pool = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004143
Jonathan Peyton30419822017-05-12 18:01:32 +00004144 if (!this_thr->th.th_task_state_memo_stack) {
4145 size_t i;
4146 this_thr->th.th_task_state_memo_stack =
4147 (kmp_uint8 *)__kmp_allocate(4 * sizeof(kmp_uint8));
4148 this_thr->th.th_task_state_top = 0;
4149 this_thr->th.th_task_state_stack_sz = 4;
4150 for (i = 0; i < this_thr->th.th_task_state_stack_sz;
4151 ++i) // zero init the stack
4152 this_thr->th.th_task_state_memo_stack[i] = 0;
4153 }
4154
4155 KMP_DEBUG_ASSERT(!this_thr->th.th_spin_here);
4156 KMP_DEBUG_ASSERT(this_thr->th.th_next_waiting == 0);
4157
4158 KMP_MB();
Jim Cownie5e8470a2013-09-27 10:38:44 +00004159}
4160
Jonathan Peyton30419822017-05-12 18:01:32 +00004161/* allocate a new thread for the requesting team. this is only called from
4162 within a forkjoin critical section. we will first try to get an available
4163 thread from the thread pool. if none is available, we will fork a new one
4164 assuming we are able to create a new one. this should be assured, as the
4165 caller should check on this first. */
4166kmp_info_t *__kmp_allocate_thread(kmp_root_t *root, kmp_team_t *team,
4167 int new_tid) {
4168 kmp_team_t *serial_team;
4169 kmp_info_t *new_thr;
4170 int new_gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004171
Jonathan Peyton30419822017-05-12 18:01:32 +00004172 KA_TRACE(20, ("__kmp_allocate_thread: T#%d\n", __kmp_get_gtid()));
4173 KMP_DEBUG_ASSERT(root && team);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004174#if !KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00004175 KMP_DEBUG_ASSERT(KMP_MASTER_GTID(__kmp_get_gtid()));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004176#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00004177 KMP_MB();
Jim Cownie5e8470a2013-09-27 10:38:44 +00004178
Jonathan Peyton30419822017-05-12 18:01:32 +00004179 /* first, try to get one from the thread pool */
4180 if (__kmp_thread_pool) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00004181
Andrey Churbanovc47afcd2017-07-03 11:24:08 +00004182 new_thr = CCAST(kmp_info_t *, __kmp_thread_pool);
Jonathan Peyton30419822017-05-12 18:01:32 +00004183 __kmp_thread_pool = (volatile kmp_info_t *)new_thr->th.th_next_pool;
4184 if (new_thr == __kmp_thread_pool_insert_pt) {
4185 __kmp_thread_pool_insert_pt = NULL;
4186 }
4187 TCW_4(new_thr->th.th_in_pool, FALSE);
4188 // Don't touch th_active_in_pool or th_active.
4189 // The worker thread adjusts those flags as it sleeps/awakens.
4190 __kmp_thread_pool_nth--;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004191
Jonathan Peyton30419822017-05-12 18:01:32 +00004192 KA_TRACE(20, ("__kmp_allocate_thread: T#%d using thread T#%d\n",
4193 __kmp_get_gtid(), new_thr->th.th_info.ds.ds_gtid));
4194 KMP_ASSERT(!new_thr->th.th_team);
4195 KMP_DEBUG_ASSERT(__kmp_nth < __kmp_threads_capacity);
4196 KMP_DEBUG_ASSERT(__kmp_thread_pool_nth >= 0);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004197
Jonathan Peyton30419822017-05-12 18:01:32 +00004198 /* setup the thread structure */
4199 __kmp_initialize_info(new_thr, team, new_tid,
4200 new_thr->th.th_info.ds.ds_gtid);
4201 KMP_DEBUG_ASSERT(new_thr->th.th_serial_team);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004202
Jonathan Peyton30419822017-05-12 18:01:32 +00004203 TCW_4(__kmp_nth, __kmp_nth + 1);
Jonathan Peytonf4392462017-07-27 20:58:41 +00004204 root->r.r_cg_nthreads++;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004205
Jonathan Peyton30419822017-05-12 18:01:32 +00004206 new_thr->th.th_task_state = 0;
4207 new_thr->th.th_task_state_top = 0;
4208 new_thr->th.th_task_state_stack_sz = 4;
Andrey Churbanov6d224db2015-02-10 18:37:43 +00004209
Jim Cownie5e8470a2013-09-27 10:38:44 +00004210#ifdef KMP_ADJUST_BLOCKTIME
Jonathan Peyton30419822017-05-12 18:01:32 +00004211 /* Adjust blocktime back to zero if necessary */
4212 /* Middle initialization might not have occurred yet */
4213 if (!__kmp_env_blocktime && (__kmp_avail_proc > 0)) {
4214 if (__kmp_nth > __kmp_avail_proc) {
4215 __kmp_zero_bt = TRUE;
4216 }
4217 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00004218#endif /* KMP_ADJUST_BLOCKTIME */
4219
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004220#if KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00004221 // If thread entered pool via __kmp_free_thread, wait_flag should !=
4222 // KMP_BARRIER_PARENT_FLAG.
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004223 int b;
Jonathan Peyton30419822017-05-12 18:01:32 +00004224 kmp_balign_t *balign = new_thr->th.th_bar;
4225 for (b = 0; b < bs_last_barrier; ++b)
4226 KMP_DEBUG_ASSERT(balign[b].bb.wait_flag != KMP_BARRIER_PARENT_FLAG);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004227#endif
4228
Jonathan Peyton30419822017-05-12 18:01:32 +00004229 KF_TRACE(10, ("__kmp_allocate_thread: T#%d using thread %p T#%d\n",
4230 __kmp_get_gtid(), new_thr, new_thr->th.th_info.ds.ds_gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00004231
Jim Cownie5e8470a2013-09-27 10:38:44 +00004232 KMP_MB();
4233 return new_thr;
Jonathan Peyton30419822017-05-12 18:01:32 +00004234 }
4235
4236 /* no, well fork a new one */
4237 KMP_ASSERT(__kmp_nth == __kmp_all_nth);
4238 KMP_ASSERT(__kmp_all_nth < __kmp_threads_capacity);
4239
4240#if KMP_USE_MONITOR
4241 // If this is the first worker thread the RTL is creating, then also
4242 // launch the monitor thread. We try to do this as early as possible.
4243 if (!TCR_4(__kmp_init_monitor)) {
4244 __kmp_acquire_bootstrap_lock(&__kmp_monitor_lock);
4245 if (!TCR_4(__kmp_init_monitor)) {
4246 KF_TRACE(10, ("before __kmp_create_monitor\n"));
4247 TCW_4(__kmp_init_monitor, 1);
4248 __kmp_create_monitor(&__kmp_monitor);
4249 KF_TRACE(10, ("after __kmp_create_monitor\n"));
4250#if KMP_OS_WINDOWS
4251 // AC: wait until monitor has started. This is a fix for CQ232808.
4252 // The reason is that if the library is loaded/unloaded in a loop with
4253 // small (parallel) work in between, then there is high probability that
4254 // monitor thread started after the library shutdown. At shutdown it is
4255 // too late to cope with the problem, because when the master is in
4256 // DllMain (process detach) the monitor has no chances to start (it is
4257 // blocked), and master has no means to inform the monitor that the
4258 // library has gone, because all the memory which the monitor can access
4259 // is going to be released/reset.
4260 while (TCR_4(__kmp_init_monitor) < 2) {
4261 KMP_YIELD(TRUE);
4262 }
4263 KF_TRACE(10, ("after monitor thread has started\n"));
4264#endif
4265 }
4266 __kmp_release_bootstrap_lock(&__kmp_monitor_lock);
4267 }
4268#endif
4269
4270 KMP_MB();
4271 for (new_gtid = 1; TCR_PTR(__kmp_threads[new_gtid]) != NULL; ++new_gtid) {
4272 KMP_DEBUG_ASSERT(new_gtid < __kmp_threads_capacity);
4273 }
4274
4275 /* allocate space for it. */
4276 new_thr = (kmp_info_t *)__kmp_allocate(sizeof(kmp_info_t));
4277
4278 TCW_SYNC_PTR(__kmp_threads[new_gtid], new_thr);
4279
4280 if (__kmp_storage_map) {
4281 __kmp_print_thread_storage_map(new_thr, new_gtid);
4282 }
4283
4284 // add the reserve serialized team, initialized from the team's master thread
4285 {
4286 kmp_internal_control_t r_icvs = __kmp_get_x_global_icvs(team);
4287 KF_TRACE(10, ("__kmp_allocate_thread: before th_serial/serial_team\n"));
4288 new_thr->th.th_serial_team = serial_team =
4289 (kmp_team_t *)__kmp_allocate_team(root, 1, 1,
4290#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00004291 ompt_data_none, // root parallel id
Jonathan Peyton30419822017-05-12 18:01:32 +00004292#endif
4293#if OMP_40_ENABLED
4294 proc_bind_default,
4295#endif
4296 &r_icvs, 0 USE_NESTED_HOT_ARG(NULL));
4297 }
4298 KMP_ASSERT(serial_team);
4299 serial_team->t.t_serialized = 0; // AC: the team created in reserve, not for
4300 // execution (it is unused for now).
4301 serial_team->t.t_threads[0] = new_thr;
4302 KF_TRACE(10,
4303 ("__kmp_allocate_thread: after th_serial/serial_team : new_thr=%p\n",
4304 new_thr));
4305
4306 /* setup the thread structures */
4307 __kmp_initialize_info(new_thr, team, new_tid, new_gtid);
4308
4309#if USE_FAST_MEMORY
4310 __kmp_initialize_fast_memory(new_thr);
4311#endif /* USE_FAST_MEMORY */
4312
4313#if KMP_USE_BGET
4314 KMP_DEBUG_ASSERT(new_thr->th.th_local.bget_data == NULL);
4315 __kmp_initialize_bget(new_thr);
4316#endif
4317
4318 __kmp_init_random(new_thr); // Initialize random number generator
4319
4320 /* Initialize these only once when thread is grabbed for a team allocation */
4321 KA_TRACE(20,
4322 ("__kmp_allocate_thread: T#%d init go fork=%u, plain=%u\n",
4323 __kmp_get_gtid(), KMP_INIT_BARRIER_STATE, KMP_INIT_BARRIER_STATE));
4324
4325 int b;
4326 kmp_balign_t *balign = new_thr->th.th_bar;
4327 for (b = 0; b < bs_last_barrier; ++b) {
4328 balign[b].bb.b_go = KMP_INIT_BARRIER_STATE;
4329 balign[b].bb.team = NULL;
4330 balign[b].bb.wait_flag = KMP_BARRIER_NOT_WAITING;
4331 balign[b].bb.use_oncore_barrier = 0;
4332 }
4333
4334 new_thr->th.th_spin_here = FALSE;
4335 new_thr->th.th_next_waiting = 0;
4336
4337#if OMP_40_ENABLED && KMP_AFFINITY_SUPPORTED
4338 new_thr->th.th_current_place = KMP_PLACE_UNDEFINED;
4339 new_thr->th.th_new_place = KMP_PLACE_UNDEFINED;
4340 new_thr->th.th_first_place = KMP_PLACE_UNDEFINED;
4341 new_thr->th.th_last_place = KMP_PLACE_UNDEFINED;
4342#endif
4343
4344 TCW_4(new_thr->th.th_in_pool, FALSE);
4345 new_thr->th.th_active_in_pool = FALSE;
4346 TCW_4(new_thr->th.th_active, TRUE);
4347
4348 /* adjust the global counters */
4349 __kmp_all_nth++;
4350 __kmp_nth++;
4351
Jonathan Peytonf4392462017-07-27 20:58:41 +00004352 root->r.r_cg_nthreads++;
4353
Jonathan Peyton30419822017-05-12 18:01:32 +00004354 // if __kmp_adjust_gtid_mode is set, then we use method #1 (sp search) for low
4355 // numbers of procs, and method #2 (keyed API call) for higher numbers.
4356 if (__kmp_adjust_gtid_mode) {
4357 if (__kmp_all_nth >= __kmp_tls_gtid_min) {
4358 if (TCR_4(__kmp_gtid_mode) != 2) {
4359 TCW_4(__kmp_gtid_mode, 2);
4360 }
4361 } else {
4362 if (TCR_4(__kmp_gtid_mode) != 1) {
4363 TCW_4(__kmp_gtid_mode, 1);
4364 }
4365 }
4366 }
4367
4368#ifdef KMP_ADJUST_BLOCKTIME
4369 /* Adjust blocktime back to zero if necessary */
4370 /* Middle initialization might not have occurred yet */
4371 if (!__kmp_env_blocktime && (__kmp_avail_proc > 0)) {
4372 if (__kmp_nth > __kmp_avail_proc) {
4373 __kmp_zero_bt = TRUE;
4374 }
4375 }
4376#endif /* KMP_ADJUST_BLOCKTIME */
4377
4378 /* actually fork it and create the new worker thread */
4379 KF_TRACE(
4380 10, ("__kmp_allocate_thread: before __kmp_create_worker: %p\n", new_thr));
4381 __kmp_create_worker(new_gtid, new_thr, __kmp_stksize);
4382 KF_TRACE(10,
4383 ("__kmp_allocate_thread: after __kmp_create_worker: %p\n", new_thr));
4384
4385 KA_TRACE(20, ("__kmp_allocate_thread: T#%d forked T#%d\n", __kmp_get_gtid(),
4386 new_gtid));
4387 KMP_MB();
4388 return new_thr;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004389}
4390
Jonathan Peyton30419822017-05-12 18:01:32 +00004391/* Reinitialize team for reuse.
4392 The hot team code calls this case at every fork barrier, so EPCC barrier
4393 test are extremely sensitive to changes in it, esp. writes to the team
4394 struct, which cause a cache invalidation in all threads.
4395 IF YOU TOUCH THIS ROUTINE, RUN EPCC C SYNCBENCH ON A BIG-IRON MACHINE!!! */
4396static void __kmp_reinitialize_team(kmp_team_t *team,
4397 kmp_internal_control_t *new_icvs,
4398 ident_t *loc) {
4399 KF_TRACE(10, ("__kmp_reinitialize_team: enter this_thread=%p team=%p\n",
4400 team->t.t_threads[0], team));
4401 KMP_DEBUG_ASSERT(team && new_icvs);
4402 KMP_DEBUG_ASSERT((!TCR_4(__kmp_init_parallel)) || new_icvs->nproc);
4403 KMP_CHECK_UPDATE(team->t.t_ident, loc);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004404
Jonathan Peyton30419822017-05-12 18:01:32 +00004405 KMP_CHECK_UPDATE(team->t.t_id, KMP_GEN_TEAM_ID());
Jonathan Peyton30419822017-05-12 18:01:32 +00004406 // Copy ICVs to the master thread's implicit taskdata
4407 __kmp_init_implicit_task(loc, team->t.t_threads[0], team, 0, FALSE);
4408 copy_icvs(&team->t.t_implicit_task_taskdata[0].td_icvs, new_icvs);
Jim Cownie181b4bb2013-12-23 17:28:57 +00004409
Jonathan Peyton30419822017-05-12 18:01:32 +00004410 KF_TRACE(10, ("__kmp_reinitialize_team: exit this_thread=%p team=%p\n",
4411 team->t.t_threads[0], team));
Jim Cownie181b4bb2013-12-23 17:28:57 +00004412}
4413
Jonathan Peyton30419822017-05-12 18:01:32 +00004414/* Initialize the team data structure.
4415 This assumes the t_threads and t_max_nproc are already set.
4416 Also, we don't touch the arguments */
4417static void __kmp_initialize_team(kmp_team_t *team, int new_nproc,
4418 kmp_internal_control_t *new_icvs,
4419 ident_t *loc) {
4420 KF_TRACE(10, ("__kmp_initialize_team: enter: team=%p\n", team));
Jim Cownie5e8470a2013-09-27 10:38:44 +00004421
Jonathan Peyton30419822017-05-12 18:01:32 +00004422 /* verify */
4423 KMP_DEBUG_ASSERT(team);
4424 KMP_DEBUG_ASSERT(new_nproc <= team->t.t_max_nproc);
4425 KMP_DEBUG_ASSERT(team->t.t_threads);
4426 KMP_MB();
Jim Cownie181b4bb2013-12-23 17:28:57 +00004427
Jonathan Peyton30419822017-05-12 18:01:32 +00004428 team->t.t_master_tid = 0; /* not needed */
4429 /* team->t.t_master_bar; not needed */
4430 team->t.t_serialized = new_nproc > 1 ? 0 : 1;
4431 team->t.t_nproc = new_nproc;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004432
Jonathan Peyton30419822017-05-12 18:01:32 +00004433 /* team->t.t_parent = NULL; TODO not needed & would mess up hot team */
4434 team->t.t_next_pool = NULL;
4435 /* memset( team->t.t_threads, 0, sizeof(kmp_info_t*)*new_nproc ); would mess
4436 * up hot team */
Jim Cownie5e8470a2013-09-27 10:38:44 +00004437
Jonathan Peyton30419822017-05-12 18:01:32 +00004438 TCW_SYNC_PTR(team->t.t_pkfn, NULL); /* not needed */
4439 team->t.t_invoke = NULL; /* not needed */
Jim Cownie5e8470a2013-09-27 10:38:44 +00004440
Jonathan Peyton30419822017-05-12 18:01:32 +00004441 // TODO???: team->t.t_max_active_levels = new_max_active_levels;
Jonathan Peytonba55a7b2017-11-29 22:47:52 +00004442 team->t.t_sched.sched = new_icvs->sched.sched;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004443
4444#if KMP_ARCH_X86 || KMP_ARCH_X86_64
Jonathan Peyton30419822017-05-12 18:01:32 +00004445 team->t.t_fp_control_saved = FALSE; /* not needed */
4446 team->t.t_x87_fpu_control_word = 0; /* not needed */
4447 team->t.t_mxcsr = 0; /* not needed */
Jim Cownie5e8470a2013-09-27 10:38:44 +00004448#endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
4449
Jonathan Peyton30419822017-05-12 18:01:32 +00004450 team->t.t_construct = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004451
Jonathan Peyton30419822017-05-12 18:01:32 +00004452 team->t.t_ordered.dt.t_value = 0;
4453 team->t.t_master_active = FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004454
Jonathan Peyton30419822017-05-12 18:01:32 +00004455 memset(&team->t.t_taskq, '\0', sizeof(kmp_taskq_t));
Jim Cownie5e8470a2013-09-27 10:38:44 +00004456
4457#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00004458 team->t.t_copypriv_data = NULL; /* not necessary, but nice for debugging */
Jim Cownie5e8470a2013-09-27 10:38:44 +00004459#endif
Jonathan Peyton37e2ef52018-07-09 17:36:22 +00004460#if KMP_OS_WINDOWS
Jonathan Peyton30419822017-05-12 18:01:32 +00004461 team->t.t_copyin_counter = 0; /* for barrier-free copyin implementation */
Jonathan Peyton37e2ef52018-07-09 17:36:22 +00004462#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00004463
Jonathan Peyton30419822017-05-12 18:01:32 +00004464 team->t.t_control_stack_top = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004465
Jonathan Peyton30419822017-05-12 18:01:32 +00004466 __kmp_reinitialize_team(team, new_icvs, loc);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004467
Jonathan Peyton30419822017-05-12 18:01:32 +00004468 KMP_MB();
4469 KF_TRACE(10, ("__kmp_initialize_team: exit: team=%p\n", team));
Jim Cownie5e8470a2013-09-27 10:38:44 +00004470}
4471
Alp Toker98758b02014-03-02 04:12:06 +00004472#if KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED
Jim Cownie5e8470a2013-09-27 10:38:44 +00004473/* Sets full mask for thread and returns old mask, no changes to structures. */
4474static void
Jonathan Peyton30419822017-05-12 18:01:32 +00004475__kmp_set_thread_affinity_mask_full_tmp(kmp_affin_mask_t *old_mask) {
4476 if (KMP_AFFINITY_CAPABLE()) {
4477 int status;
4478 if (old_mask != NULL) {
4479 status = __kmp_get_system_affinity(old_mask, TRUE);
4480 int error = errno;
4481 if (status != 0) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +00004482 __kmp_fatal(KMP_MSG(ChangeThreadAffMaskError), KMP_ERR(error),
4483 __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +00004484 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00004485 }
Jonathan Peyton30419822017-05-12 18:01:32 +00004486 __kmp_set_system_affinity(__kmp_affin_fullMask, TRUE);
4487 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00004488}
4489#endif
4490
Alp Toker98758b02014-03-02 04:12:06 +00004491#if OMP_40_ENABLED && KMP_AFFINITY_SUPPORTED
Jim Cownie5e8470a2013-09-27 10:38:44 +00004492
Jim Cownie5e8470a2013-09-27 10:38:44 +00004493// __kmp_partition_places() is the heart of the OpenMP 4.0 affinity mechanism.
4494// It calculats the worker + master thread's partition based upon the parent
Alp Toker8f2d3f02014-02-24 10:40:15 +00004495// thread's partition, and binds each worker to a thread in their partition.
Jim Cownie5e8470a2013-09-27 10:38:44 +00004496// The master thread's partition should already include its current binding.
Jonathan Peyton30419822017-05-12 18:01:32 +00004497static void __kmp_partition_places(kmp_team_t *team, int update_master_only) {
4498 // Copy the master thread's place partion to the team struct
4499 kmp_info_t *master_th = team->t.t_threads[0];
4500 KMP_DEBUG_ASSERT(master_th != NULL);
4501 kmp_proc_bind_t proc_bind = team->t.t_proc_bind;
4502 int first_place = master_th->th.th_first_place;
4503 int last_place = master_th->th.th_last_place;
4504 int masters_place = master_th->th.th_current_place;
4505 team->t.t_first_place = first_place;
4506 team->t.t_last_place = last_place;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004507
Jonathan Peyton30419822017-05-12 18:01:32 +00004508 KA_TRACE(20, ("__kmp_partition_places: enter: proc_bind = %d T#%d(%d:0) "
4509 "bound to place %d partition = [%d,%d]\n",
4510 proc_bind, __kmp_gtid_from_thread(team->t.t_threads[0]),
4511 team->t.t_id, masters_place, first_place, last_place));
Jim Cownie5e8470a2013-09-27 10:38:44 +00004512
Jonathan Peyton30419822017-05-12 18:01:32 +00004513 switch (proc_bind) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00004514
Jonathan Peyton30419822017-05-12 18:01:32 +00004515 case proc_bind_default:
4516 // serial teams might have the proc_bind policy set to proc_bind_default. It
4517 // doesn't matter, as we don't rebind master thread for any proc_bind policy
4518 KMP_DEBUG_ASSERT(team->t.t_nproc == 1);
4519 break;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004520
Jonathan Peyton30419822017-05-12 18:01:32 +00004521 case proc_bind_master: {
4522 int f;
4523 int n_th = team->t.t_nproc;
4524 for (f = 1; f < n_th; f++) {
4525 kmp_info_t *th = team->t.t_threads[f];
4526 KMP_DEBUG_ASSERT(th != NULL);
4527 th->th.th_first_place = first_place;
4528 th->th.th_last_place = last_place;
4529 th->th.th_new_place = masters_place;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004530
Jonathan Peyton30419822017-05-12 18:01:32 +00004531 KA_TRACE(100, ("__kmp_partition_places: master: T#%d(%d:%d) place %d "
4532 "partition = [%d,%d]\n",
4533 __kmp_gtid_from_thread(team->t.t_threads[f]), team->t.t_id,
4534 f, masters_place, first_place, last_place));
Jim Cownie5e8470a2013-09-27 10:38:44 +00004535 }
Jonathan Peyton30419822017-05-12 18:01:32 +00004536 } break;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004537
Jonathan Peyton30419822017-05-12 18:01:32 +00004538 case proc_bind_close: {
4539 int f;
4540 int n_th = team->t.t_nproc;
4541 int n_places;
4542 if (first_place <= last_place) {
4543 n_places = last_place - first_place + 1;
4544 } else {
4545 n_places = __kmp_affinity_num_masks - first_place + last_place + 1;
4546 }
4547 if (n_th <= n_places) {
4548 int place = masters_place;
4549 for (f = 1; f < n_th; f++) {
4550 kmp_info_t *th = team->t.t_threads[f];
4551 KMP_DEBUG_ASSERT(th != NULL);
4552
4553 if (place == last_place) {
4554 place = first_place;
4555 } else if (place == (int)(__kmp_affinity_num_masks - 1)) {
4556 place = 0;
4557 } else {
4558 place++;
4559 }
4560 th->th.th_first_place = first_place;
4561 th->th.th_last_place = last_place;
4562 th->th.th_new_place = place;
4563
4564 KA_TRACE(100, ("__kmp_partition_places: close: T#%d(%d:%d) place %d "
4565 "partition = [%d,%d]\n",
4566 __kmp_gtid_from_thread(team->t.t_threads[f]),
4567 team->t.t_id, f, place, first_place, last_place));
4568 }
4569 } else {
4570 int S, rem, gap, s_count;
4571 S = n_th / n_places;
4572 s_count = 0;
4573 rem = n_th - (S * n_places);
4574 gap = rem > 0 ? n_places / rem : n_places;
4575 int place = masters_place;
4576 int gap_ct = gap;
4577 for (f = 0; f < n_th; f++) {
4578 kmp_info_t *th = team->t.t_threads[f];
4579 KMP_DEBUG_ASSERT(th != NULL);
4580
4581 th->th.th_first_place = first_place;
4582 th->th.th_last_place = last_place;
4583 th->th.th_new_place = place;
4584 s_count++;
4585
4586 if ((s_count == S) && rem && (gap_ct == gap)) {
4587 // do nothing, add an extra thread to place on next iteration
4588 } else if ((s_count == S + 1) && rem && (gap_ct == gap)) {
4589 // we added an extra thread to this place; move to next place
4590 if (place == last_place) {
4591 place = first_place;
4592 } else if (place == (int)(__kmp_affinity_num_masks - 1)) {
4593 place = 0;
4594 } else {
4595 place++;
4596 }
4597 s_count = 0;
4598 gap_ct = 1;
4599 rem--;
4600 } else if (s_count == S) { // place full; don't add extra
4601 if (place == last_place) {
4602 place = first_place;
4603 } else if (place == (int)(__kmp_affinity_num_masks - 1)) {
4604 place = 0;
4605 } else {
4606 place++;
4607 }
4608 gap_ct++;
4609 s_count = 0;
4610 }
4611
4612 KA_TRACE(100,
4613 ("__kmp_partition_places: close: T#%d(%d:%d) place %d "
4614 "partition = [%d,%d]\n",
4615 __kmp_gtid_from_thread(team->t.t_threads[f]), team->t.t_id, f,
4616 th->th.th_new_place, first_place, last_place));
4617 }
4618 KMP_DEBUG_ASSERT(place == masters_place);
4619 }
4620 } break;
4621
4622 case proc_bind_spread: {
4623 int f;
4624 int n_th = team->t.t_nproc;
4625 int n_places;
4626 int thidx;
4627 if (first_place <= last_place) {
4628 n_places = last_place - first_place + 1;
4629 } else {
4630 n_places = __kmp_affinity_num_masks - first_place + last_place + 1;
4631 }
4632 if (n_th <= n_places) {
Paul Osmialowskia0162792017-08-10 23:04:11 +00004633 int place = -1;
Jonathan Peyton30419822017-05-12 18:01:32 +00004634
Paul Osmialowskia0162792017-08-10 23:04:11 +00004635 if (n_places != static_cast<int>(__kmp_affinity_num_masks)) {
4636 int S = n_places / n_th;
4637 int s_count, rem, gap, gap_ct;
4638
4639 place = masters_place;
4640 rem = n_places - n_th * S;
4641 gap = rem ? n_th / rem : 1;
4642 gap_ct = gap;
4643 thidx = n_th;
4644 if (update_master_only == 1)
4645 thidx = 1;
4646 for (f = 0; f < thidx; f++) {
4647 kmp_info_t *th = team->t.t_threads[f];
4648 KMP_DEBUG_ASSERT(th != NULL);
4649
4650 th->th.th_first_place = place;
4651 th->th.th_new_place = place;
4652 s_count = 1;
4653 while (s_count < S) {
4654 if (place == last_place) {
4655 place = first_place;
4656 } else if (place == (int)(__kmp_affinity_num_masks - 1)) {
4657 place = 0;
4658 } else {
4659 place++;
4660 }
4661 s_count++;
4662 }
4663 if (rem && (gap_ct == gap)) {
4664 if (place == last_place) {
4665 place = first_place;
4666 } else if (place == (int)(__kmp_affinity_num_masks - 1)) {
4667 place = 0;
4668 } else {
4669 place++;
4670 }
4671 rem--;
4672 gap_ct = 0;
4673 }
4674 th->th.th_last_place = place;
4675 gap_ct++;
4676
Jonathan Peyton30419822017-05-12 18:01:32 +00004677 if (place == last_place) {
4678 place = first_place;
4679 } else if (place == (int)(__kmp_affinity_num_masks - 1)) {
4680 place = 0;
4681 } else {
4682 place++;
4683 }
Paul Osmialowskia0162792017-08-10 23:04:11 +00004684
Jonathan Peyton94a114f2017-10-20 19:30:57 +00004685 KA_TRACE(100,
4686 ("__kmp_partition_places: spread: T#%d(%d:%d) place %d "
4687 "partition = [%d,%d], __kmp_affinity_num_masks: %u\n",
4688 __kmp_gtid_from_thread(team->t.t_threads[f]), team->t.t_id,
4689 f, th->th.th_new_place, th->th.th_first_place,
4690 th->th.th_last_place, __kmp_affinity_num_masks));
Jonathan Peyton30419822017-05-12 18:01:32 +00004691 }
Paul Osmialowskia0162792017-08-10 23:04:11 +00004692 } else {
4693 /* Having uniform space of available computation places I can create
4694 T partitions of round(P/T) size and put threads into the first
4695 place of each partition. */
4696 double current = static_cast<double>(masters_place);
4697 double spacing =
Jonathan Peyton94a114f2017-10-20 19:30:57 +00004698 (static_cast<double>(n_places + 1) / static_cast<double>(n_th));
Paul Osmialowskia0162792017-08-10 23:04:11 +00004699 int first, last;
4700 kmp_info_t *th;
4701
4702 thidx = n_th + 1;
4703 if (update_master_only == 1)
4704 thidx = 1;
4705 for (f = 0; f < thidx; f++) {
4706 first = static_cast<int>(current);
4707 last = static_cast<int>(current + spacing) - 1;
4708 KMP_DEBUG_ASSERT(last >= first);
4709 if (first >= n_places) {
4710 if (masters_place) {
4711 first -= n_places;
4712 last -= n_places;
4713 if (first == (masters_place + 1)) {
4714 KMP_DEBUG_ASSERT(f == n_th);
4715 first--;
4716 }
4717 if (last == masters_place) {
4718 KMP_DEBUG_ASSERT(f == (n_th - 1));
4719 last--;
4720 }
4721 } else {
4722 KMP_DEBUG_ASSERT(f == n_th);
4723 first = 0;
4724 last = 0;
4725 }
Jonathan Peyton30419822017-05-12 18:01:32 +00004726 }
Paul Osmialowskia0162792017-08-10 23:04:11 +00004727 if (last >= n_places) {
4728 last = (n_places - 1);
4729 }
4730 place = first;
4731 current += spacing;
4732 if (f < n_th) {
4733 KMP_DEBUG_ASSERT(0 <= first);
4734 KMP_DEBUG_ASSERT(n_places > first);
4735 KMP_DEBUG_ASSERT(0 <= last);
4736 KMP_DEBUG_ASSERT(n_places > last);
4737 KMP_DEBUG_ASSERT(last_place >= first_place);
4738 th = team->t.t_threads[f];
4739 KMP_DEBUG_ASSERT(th);
4740 th->th.th_first_place = first;
4741 th->th.th_new_place = place;
4742 th->th.th_last_place = last;
Jonathan Peyton30419822017-05-12 18:01:32 +00004743
Jonathan Peyton94a114f2017-10-20 19:30:57 +00004744 KA_TRACE(100,
4745 ("__kmp_partition_places: spread: T#%d(%d:%d) place %d "
4746 "partition = [%d,%d], spacing = %.4f\n",
4747 __kmp_gtid_from_thread(team->t.t_threads[f]),
4748 team->t.t_id, f, th->th.th_new_place,
4749 th->th.th_first_place, th->th.th_last_place, spacing));
Paul Osmialowskia0162792017-08-10 23:04:11 +00004750 }
Jonathan Peyton30419822017-05-12 18:01:32 +00004751 }
Jonathan Peyton30419822017-05-12 18:01:32 +00004752 }
4753 KMP_DEBUG_ASSERT(update_master_only || place == masters_place);
4754 } else {
4755 int S, rem, gap, s_count;
4756 S = n_th / n_places;
4757 s_count = 0;
4758 rem = n_th - (S * n_places);
4759 gap = rem > 0 ? n_places / rem : n_places;
4760 int place = masters_place;
4761 int gap_ct = gap;
4762 thidx = n_th;
4763 if (update_master_only == 1)
4764 thidx = 1;
4765 for (f = 0; f < thidx; f++) {
4766 kmp_info_t *th = team->t.t_threads[f];
4767 KMP_DEBUG_ASSERT(th != NULL);
4768
4769 th->th.th_first_place = place;
4770 th->th.th_last_place = place;
4771 th->th.th_new_place = place;
4772 s_count++;
4773
4774 if ((s_count == S) && rem && (gap_ct == gap)) {
4775 // do nothing, add an extra thread to place on next iteration
4776 } else if ((s_count == S + 1) && rem && (gap_ct == gap)) {
4777 // we added an extra thread to this place; move on to next place
4778 if (place == last_place) {
4779 place = first_place;
4780 } else if (place == (int)(__kmp_affinity_num_masks - 1)) {
4781 place = 0;
4782 } else {
4783 place++;
4784 }
4785 s_count = 0;
4786 gap_ct = 1;
4787 rem--;
4788 } else if (s_count == S) { // place is full; don't add extra thread
4789 if (place == last_place) {
4790 place = first_place;
4791 } else if (place == (int)(__kmp_affinity_num_masks - 1)) {
4792 place = 0;
4793 } else {
4794 place++;
4795 }
4796 gap_ct++;
4797 s_count = 0;
4798 }
4799
4800 KA_TRACE(100, ("__kmp_partition_places: spread: T#%d(%d:%d) place %d "
4801 "partition = [%d,%d]\n",
4802 __kmp_gtid_from_thread(team->t.t_threads[f]),
4803 team->t.t_id, f, th->th.th_new_place,
4804 th->th.th_first_place, th->th.th_last_place));
4805 }
4806 KMP_DEBUG_ASSERT(update_master_only || place == masters_place);
4807 }
4808 } break;
4809
4810 default:
4811 break;
4812 }
4813
4814 KA_TRACE(20, ("__kmp_partition_places: exit T#%d\n", team->t.t_id));
Jim Cownie5e8470a2013-09-27 10:38:44 +00004815}
4816
Alp Toker98758b02014-03-02 04:12:06 +00004817#endif /* OMP_40_ENABLED && KMP_AFFINITY_SUPPORTED */
Jim Cownie5e8470a2013-09-27 10:38:44 +00004818
Jonathan Peyton30419822017-05-12 18:01:32 +00004819/* allocate a new team data structure to use. take one off of the free pool if
4820 available */
Jim Cownie5e8470a2013-09-27 10:38:44 +00004821kmp_team_t *
Jonathan Peyton30419822017-05-12 18:01:32 +00004822__kmp_allocate_team(kmp_root_t *root, int new_nproc, int max_nproc,
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00004823#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00004824 ompt_data_t ompt_parallel_data,
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00004825#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00004826#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00004827 kmp_proc_bind_t new_proc_bind,
Jim Cownie5e8470a2013-09-27 10:38:44 +00004828#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00004829 kmp_internal_control_t *new_icvs,
4830 int argc USE_NESTED_HOT_ARG(kmp_info_t *master)) {
4831 KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(KMP_allocate_team);
4832 int f;
4833 kmp_team_t *team;
4834 int use_hot_team = !root->r.r_active;
4835 int level = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004836
Jonathan Peyton30419822017-05-12 18:01:32 +00004837 KA_TRACE(20, ("__kmp_allocate_team: called\n"));
4838 KMP_DEBUG_ASSERT(new_nproc >= 1 && argc >= 0);
4839 KMP_DEBUG_ASSERT(max_nproc >= new_nproc);
4840 KMP_MB();
Jim Cownie5e8470a2013-09-27 10:38:44 +00004841
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004842#if KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00004843 kmp_hot_team_ptr_t *hot_teams;
4844 if (master) {
4845 team = master->th.th_team;
4846 level = team->t.t_active_level;
4847 if (master->th.th_teams_microtask) { // in teams construct?
4848 if (master->th.th_teams_size.nteams > 1 &&
4849 ( // #teams > 1
4850 team->t.t_pkfn ==
4851 (microtask_t)__kmp_teams_master || // inner fork of the teams
4852 master->th.th_teams_level <
4853 team->t.t_level)) { // or nested parallel inside the teams
4854 ++level; // not increment if #teams==1, or for outer fork of the teams;
4855 // increment otherwise
4856 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004857 }
Jonathan Peyton30419822017-05-12 18:01:32 +00004858 hot_teams = master->th.th_hot_teams;
4859 if (level < __kmp_hot_teams_max_level && hot_teams &&
4860 hot_teams[level]
4861 .hot_team) { // hot team has already been allocated for given level
4862 use_hot_team = 1;
4863 } else {
4864 use_hot_team = 0;
4865 }
4866 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004867#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00004868 // Optimization to use a "hot" team
4869 if (use_hot_team && new_nproc > 1) {
4870 KMP_DEBUG_ASSERT(new_nproc == max_nproc);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004871#if KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00004872 team = hot_teams[level].hot_team;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004873#else
Jonathan Peyton30419822017-05-12 18:01:32 +00004874 team = root->r.r_hot_team;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004875#endif
4876#if KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00004877 if (__kmp_tasking_mode != tskm_immediate_exec) {
4878 KA_TRACE(20, ("__kmp_allocate_team: hot team task_team[0] = %p "
4879 "task_team[1] = %p before reinit\n",
4880 team->t.t_task_team[0], team->t.t_task_team[1]));
4881 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00004882#endif
4883
Jonathan Peyton30419822017-05-12 18:01:32 +00004884 // Has the number of threads changed?
4885 /* Let's assume the most common case is that the number of threads is
4886 unchanged, and put that case first. */
4887 if (team->t.t_nproc == new_nproc) { // Check changes in number of threads
4888 KA_TRACE(20, ("__kmp_allocate_team: reusing hot team\n"));
4889 // This case can mean that omp_set_num_threads() was called and the hot
Jonathan Peyton642688b2017-06-01 16:46:36 +00004890 // team size was already reduced, so we check the special flag
Jonathan Peyton30419822017-05-12 18:01:32 +00004891 if (team->t.t_size_changed == -1) {
4892 team->t.t_size_changed = 1;
4893 } else {
4894 KMP_CHECK_UPDATE(team->t.t_size_changed, 0);
4895 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004896
Jonathan Peyton30419822017-05-12 18:01:32 +00004897 // TODO???: team->t.t_max_active_levels = new_max_active_levels;
4898 kmp_r_sched_t new_sched = new_icvs->sched;
Jonathan Peytonba55a7b2017-11-29 22:47:52 +00004899 // set master's schedule as new run-time schedule
4900 KMP_CHECK_UPDATE(team->t.t_sched.sched, new_sched.sched);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004901
Jonathan Peyton30419822017-05-12 18:01:32 +00004902 __kmp_reinitialize_team(team, new_icvs,
4903 root->r.r_uber_thread->th.th_ident);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004904
Jonathan Peyton30419822017-05-12 18:01:32 +00004905 KF_TRACE(10, ("__kmp_allocate_team2: T#%d, this_thread=%p team=%p\n", 0,
4906 team->t.t_threads[0], team));
4907 __kmp_push_current_task_to_thread(team->t.t_threads[0], team, 0);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004908
4909#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00004910#if KMP_AFFINITY_SUPPORTED
4911 if ((team->t.t_size_changed == 0) &&
4912 (team->t.t_proc_bind == new_proc_bind)) {
4913 if (new_proc_bind == proc_bind_spread) {
4914 __kmp_partition_places(
4915 team, 1); // add flag to update only master for spread
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004916 }
Jonathan Peyton30419822017-05-12 18:01:32 +00004917 KA_TRACE(200, ("__kmp_allocate_team: reusing hot team #%d bindings: "
4918 "proc_bind = %d, partition = [%d,%d]\n",
4919 team->t.t_id, new_proc_bind, team->t.t_first_place,
4920 team->t.t_last_place));
4921 } else {
4922 KMP_CHECK_UPDATE(team->t.t_proc_bind, new_proc_bind);
4923 __kmp_partition_places(team);
4924 }
4925#else
4926 KMP_CHECK_UPDATE(team->t.t_proc_bind, new_proc_bind);
4927#endif /* KMP_AFFINITY_SUPPORTED */
4928#endif /* OMP_40_ENABLED */
4929 } else if (team->t.t_nproc > new_nproc) {
4930 KA_TRACE(20,
4931 ("__kmp_allocate_team: decreasing hot team thread count to %d\n",
4932 new_nproc));
Jim Cownie5e8470a2013-09-27 10:38:44 +00004933
Jonathan Peyton30419822017-05-12 18:01:32 +00004934 team->t.t_size_changed = 1;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004935#if KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00004936 if (__kmp_hot_teams_mode == 0) {
4937 // AC: saved number of threads should correspond to team's value in this
4938 // mode, can be bigger in mode 1, when hot team has threads in reserve
4939 KMP_DEBUG_ASSERT(hot_teams[level].hot_team_nth == team->t.t_nproc);
4940 hot_teams[level].hot_team_nth = new_nproc;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004941#endif // KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00004942 /* release the extra threads we don't need any more */
4943 for (f = new_nproc; f < team->t.t_nproc; f++) {
4944 KMP_DEBUG_ASSERT(team->t.t_threads[f]);
4945 if (__kmp_tasking_mode != tskm_immediate_exec) {
4946 // When decreasing team size, threads no longer in the team should
4947 // unref task team.
4948 team->t.t_threads[f]->th.th_task_team = NULL;
4949 }
4950 __kmp_free_thread(team->t.t_threads[f]);
4951 team->t.t_threads[f] = NULL;
4952 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004953#if KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00004954 } // (__kmp_hot_teams_mode == 0)
4955 else {
4956 // When keeping extra threads in team, switch threads to wait on own
4957 // b_go flag
4958 for (f = new_nproc; f < team->t.t_nproc; ++f) {
4959 KMP_DEBUG_ASSERT(team->t.t_threads[f]);
4960 kmp_balign_t *balign = team->t.t_threads[f]->th.th_bar;
4961 for (int b = 0; b < bs_last_barrier; ++b) {
4962 if (balign[b].bb.wait_flag == KMP_BARRIER_PARENT_FLAG) {
4963 balign[b].bb.wait_flag = KMP_BARRIER_SWITCH_TO_OWN_FLAG;
Andrey Churbanovd6e1d7e2016-08-11 13:04:00 +00004964 }
Jonathan Peyton30419822017-05-12 18:01:32 +00004965 KMP_CHECK_UPDATE(balign[b].bb.leaf_kids, 0);
4966 }
4967 }
4968 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004969#endif // KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00004970 team->t.t_nproc = new_nproc;
4971 // TODO???: team->t.t_max_active_levels = new_max_active_levels;
Jonathan Peytonba55a7b2017-11-29 22:47:52 +00004972 KMP_CHECK_UPDATE(team->t.t_sched.sched, new_icvs->sched.sched);
Jonathan Peyton30419822017-05-12 18:01:32 +00004973 __kmp_reinitialize_team(team, new_icvs,
4974 root->r.r_uber_thread->th.th_ident);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004975
Jonathan Peyton30419822017-05-12 18:01:32 +00004976 /* update the remaining threads */
4977 for (f = 0; f < new_nproc; ++f) {
4978 team->t.t_threads[f]->th.th_team_nproc = new_nproc;
4979 }
4980 // restore the current task state of the master thread: should be the
4981 // implicit task
4982 KF_TRACE(10, ("__kmp_allocate_team: T#%d, this_thread=%p team=%p\n", 0,
4983 team->t.t_threads[0], team));
Jim Cownie5e8470a2013-09-27 10:38:44 +00004984
Jonathan Peyton30419822017-05-12 18:01:32 +00004985 __kmp_push_current_task_to_thread(team->t.t_threads[0], team, 0);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004986
4987#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00004988 for (f = 0; f < team->t.t_nproc; f++) {
4989 KMP_DEBUG_ASSERT(team->t.t_threads[f] &&
4990 team->t.t_threads[f]->th.th_team_nproc ==
4991 team->t.t_nproc);
4992 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00004993#endif
4994
4995#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00004996 KMP_CHECK_UPDATE(team->t.t_proc_bind, new_proc_bind);
4997#if KMP_AFFINITY_SUPPORTED
4998 __kmp_partition_places(team);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004999#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00005000#endif
5001 } else { // team->t.t_nproc < new_nproc
Alp Toker98758b02014-03-02 04:12:06 +00005002#if KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED
Jonathan Peyton30419822017-05-12 18:01:32 +00005003 kmp_affin_mask_t *old_mask;
5004 if (KMP_AFFINITY_CAPABLE()) {
5005 KMP_CPU_ALLOC(old_mask);
5006 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00005007#endif
5008
Jonathan Peyton30419822017-05-12 18:01:32 +00005009 KA_TRACE(20,
5010 ("__kmp_allocate_team: increasing hot team thread count to %d\n",
5011 new_nproc));
Jim Cownie5e8470a2013-09-27 10:38:44 +00005012
Jonathan Peyton30419822017-05-12 18:01:32 +00005013 team->t.t_size_changed = 1;
Jim Cownie5e8470a2013-09-27 10:38:44 +00005014
Jim Cownie4cc4bb42014-10-07 16:25:50 +00005015#if KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00005016 int avail_threads = hot_teams[level].hot_team_nth;
5017 if (new_nproc < avail_threads)
5018 avail_threads = new_nproc;
5019 kmp_info_t **other_threads = team->t.t_threads;
5020 for (f = team->t.t_nproc; f < avail_threads; ++f) {
5021 // Adjust barrier data of reserved threads (if any) of the team
5022 // Other data will be set in __kmp_initialize_info() below.
Jim Cownie5e8470a2013-09-27 10:38:44 +00005023 int b;
Jonathan Peyton30419822017-05-12 18:01:32 +00005024 kmp_balign_t *balign = other_threads[f]->th.th_bar;
5025 for (b = 0; b < bs_last_barrier; ++b) {
5026 balign[b].bb.b_arrived = team->t.t_bar[b].b_arrived;
5027 KMP_DEBUG_ASSERT(balign[b].bb.wait_flag != KMP_BARRIER_PARENT_FLAG);
Jonathan Peyton8fbb49a2015-07-09 18:16:58 +00005028#if USE_DEBUGGER
Jonathan Peyton30419822017-05-12 18:01:32 +00005029 balign[b].bb.b_worker_arrived = team->t.t_bar[b].b_team_arrived;
Jonathan Peyton8fbb49a2015-07-09 18:16:58 +00005030#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00005031 }
Jonathan Peyton30419822017-05-12 18:01:32 +00005032 }
5033 if (hot_teams[level].hot_team_nth >= new_nproc) {
5034 // we have all needed threads in reserve, no need to allocate any
5035 // this only possible in mode 1, cannot have reserved threads in mode 0
5036 KMP_DEBUG_ASSERT(__kmp_hot_teams_mode == 1);
5037 team->t.t_nproc = new_nproc; // just get reserved threads involved
5038 } else {
5039 // we may have some threads in reserve, but not enough
5040 team->t.t_nproc =
5041 hot_teams[level]
5042 .hot_team_nth; // get reserved threads involved if any
5043 hot_teams[level].hot_team_nth = new_nproc; // adjust hot team max size
5044#endif // KMP_NESTED_HOT_TEAMS
5045 if (team->t.t_max_nproc < new_nproc) {
5046 /* reallocate larger arrays */
5047 __kmp_reallocate_team_arrays(team, new_nproc);
5048 __kmp_reinitialize_team(team, new_icvs, NULL);
5049 }
5050
5051#if KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED
5052 /* Temporarily set full mask for master thread before creation of
5053 workers. The reason is that workers inherit the affinity from master,
5054 so if a lot of workers are created on the single core quickly, they
5055 don't get a chance to set their own affinity for a long time. */
5056 __kmp_set_thread_affinity_mask_full_tmp(old_mask);
5057#endif
5058
5059 /* allocate new threads for the hot team */
5060 for (f = team->t.t_nproc; f < new_nproc; f++) {
5061 kmp_info_t *new_worker = __kmp_allocate_thread(root, team, f);
5062 KMP_DEBUG_ASSERT(new_worker);
5063 team->t.t_threads[f] = new_worker;
5064
5065 KA_TRACE(20,
5066 ("__kmp_allocate_team: team %d init T#%d arrived: "
5067 "join=%llu, plain=%llu\n",
5068 team->t.t_id, __kmp_gtid_from_tid(f, team), team->t.t_id, f,
5069 team->t.t_bar[bs_forkjoin_barrier].b_arrived,
5070 team->t.t_bar[bs_plain_barrier].b_arrived));
5071
5072 { // Initialize barrier data for new threads.
5073 int b;
5074 kmp_balign_t *balign = new_worker->th.th_bar;
5075 for (b = 0; b < bs_last_barrier; ++b) {
5076 balign[b].bb.b_arrived = team->t.t_bar[b].b_arrived;
5077 KMP_DEBUG_ASSERT(balign[b].bb.wait_flag !=
5078 KMP_BARRIER_PARENT_FLAG);
5079#if USE_DEBUGGER
5080 balign[b].bb.b_worker_arrived = team->t.t_bar[b].b_team_arrived;
5081#endif
5082 }
5083 }
5084 }
5085
5086#if KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED
5087 if (KMP_AFFINITY_CAPABLE()) {
5088 /* Restore initial master thread's affinity mask */
5089 __kmp_set_system_affinity(old_mask, TRUE);
5090 KMP_CPU_FREE(old_mask);
5091 }
5092#endif
5093#if KMP_NESTED_HOT_TEAMS
5094 } // end of check of t_nproc vs. new_nproc vs. hot_team_nth
5095#endif // KMP_NESTED_HOT_TEAMS
5096 /* make sure everyone is syncronized */
5097 int old_nproc = team->t.t_nproc; // save old value and use to update only
5098 // new threads below
5099 __kmp_initialize_team(team, new_nproc, new_icvs,
5100 root->r.r_uber_thread->th.th_ident);
5101
5102 /* reinitialize the threads */
5103 KMP_DEBUG_ASSERT(team->t.t_nproc == new_nproc);
5104 for (f = 0; f < team->t.t_nproc; ++f)
5105 __kmp_initialize_info(team->t.t_threads[f], team, f,
5106 __kmp_gtid_from_tid(f, team));
5107 if (level) { // set th_task_state for new threads in nested hot team
5108 // __kmp_initialize_info() no longer zeroes th_task_state, so we should
5109 // only need to set the th_task_state for the new threads. th_task_state
5110 // for master thread will not be accurate until after this in
5111 // __kmp_fork_call(), so we look to the master's memo_stack to get the
5112 // correct value.
5113 for (f = old_nproc; f < team->t.t_nproc; ++f)
5114 team->t.t_threads[f]->th.th_task_state =
5115 team->t.t_threads[0]->th.th_task_state_memo_stack[level];
5116 } else { // set th_task_state for new threads in non-nested hot team
5117 int old_state =
5118 team->t.t_threads[0]->th.th_task_state; // copy master's state
5119 for (f = old_nproc; f < team->t.t_nproc; ++f)
5120 team->t.t_threads[f]->th.th_task_state = old_state;
5121 }
5122
5123#ifdef KMP_DEBUG
5124 for (f = 0; f < team->t.t_nproc; ++f) {
5125 KMP_DEBUG_ASSERT(team->t.t_threads[f] &&
5126 team->t.t_threads[f]->th.th_team_nproc ==
5127 team->t.t_nproc);
5128 }
5129#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00005130
5131#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00005132 KMP_CHECK_UPDATE(team->t.t_proc_bind, new_proc_bind);
5133#if KMP_AFFINITY_SUPPORTED
5134 __kmp_partition_places(team);
5135#endif
5136#endif
5137 } // Check changes in number of threads
5138
5139#if OMP_40_ENABLED
5140 kmp_info_t *master = team->t.t_threads[0];
5141 if (master->th.th_teams_microtask) {
5142 for (f = 1; f < new_nproc; ++f) {
5143 // propagate teams construct specific info to workers
5144 kmp_info_t *thr = team->t.t_threads[f];
5145 thr->th.th_teams_microtask = master->th.th_teams_microtask;
5146 thr->th.th_teams_level = master->th.th_teams_level;
5147 thr->th.th_teams_size = master->th.th_teams_size;
5148 }
5149 }
5150#endif /* OMP_40_ENABLED */
5151#if KMP_NESTED_HOT_TEAMS
5152 if (level) {
5153 // Sync barrier state for nested hot teams, not needed for outermost hot
5154 // team.
5155 for (f = 1; f < new_nproc; ++f) {
5156 kmp_info_t *thr = team->t.t_threads[f];
5157 int b;
5158 kmp_balign_t *balign = thr->th.th_bar;
5159 for (b = 0; b < bs_last_barrier; ++b) {
5160 balign[b].bb.b_arrived = team->t.t_bar[b].b_arrived;
5161 KMP_DEBUG_ASSERT(balign[b].bb.wait_flag != KMP_BARRIER_PARENT_FLAG);
5162#if USE_DEBUGGER
5163 balign[b].bb.b_worker_arrived = team->t.t_bar[b].b_team_arrived;
5164#endif
5165 }
5166 }
5167 }
5168#endif // KMP_NESTED_HOT_TEAMS
5169
5170 /* reallocate space for arguments if necessary */
5171 __kmp_alloc_argv_entries(argc, team, TRUE);
5172 KMP_CHECK_UPDATE(team->t.t_argc, argc);
5173 // The hot team re-uses the previous task team,
5174 // if untouched during the previous release->gather phase.
5175
5176 KF_TRACE(10, (" hot_team = %p\n", team));
5177
5178#if KMP_DEBUG
5179 if (__kmp_tasking_mode != tskm_immediate_exec) {
5180 KA_TRACE(20, ("__kmp_allocate_team: hot team task_team[0] = %p "
5181 "task_team[1] = %p after reinit\n",
5182 team->t.t_task_team[0], team->t.t_task_team[1]));
5183 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00005184#endif
5185
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00005186#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00005187 __ompt_team_assign_id(team, ompt_parallel_data);
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00005188#endif
5189
Jim Cownie5e8470a2013-09-27 10:38:44 +00005190 KMP_MB();
5191
Jim Cownie5e8470a2013-09-27 10:38:44 +00005192 return team;
Jonathan Peyton30419822017-05-12 18:01:32 +00005193 }
5194
5195 /* next, let's try to take one from the team pool */
5196 KMP_MB();
Andrey Churbanovc47afcd2017-07-03 11:24:08 +00005197 for (team = CCAST(kmp_team_t *, __kmp_team_pool); (team);) {
Jonathan Peyton30419822017-05-12 18:01:32 +00005198 /* TODO: consider resizing undersized teams instead of reaping them, now
5199 that we have a resizing mechanism */
5200 if (team->t.t_max_nproc >= max_nproc) {
5201 /* take this team from the team pool */
5202 __kmp_team_pool = team->t.t_next_pool;
5203
5204 /* setup the team for fresh use */
5205 __kmp_initialize_team(team, new_nproc, new_icvs, NULL);
5206
5207 KA_TRACE(20, ("__kmp_allocate_team: setting task_team[0] %p and "
5208 "task_team[1] %p to NULL\n",
5209 &team->t.t_task_team[0], &team->t.t_task_team[1]));
5210 team->t.t_task_team[0] = NULL;
5211 team->t.t_task_team[1] = NULL;
5212
5213 /* reallocate space for arguments if necessary */
5214 __kmp_alloc_argv_entries(argc, team, TRUE);
5215 KMP_CHECK_UPDATE(team->t.t_argc, argc);
5216
5217 KA_TRACE(
5218 20, ("__kmp_allocate_team: team %d init arrived: join=%u, plain=%u\n",
5219 team->t.t_id, KMP_INIT_BARRIER_STATE, KMP_INIT_BARRIER_STATE));
5220 { // Initialize barrier data.
5221 int b;
5222 for (b = 0; b < bs_last_barrier; ++b) {
5223 team->t.t_bar[b].b_arrived = KMP_INIT_BARRIER_STATE;
5224#if USE_DEBUGGER
5225 team->t.t_bar[b].b_master_arrived = 0;
5226 team->t.t_bar[b].b_team_arrived = 0;
5227#endif
5228 }
5229 }
5230
5231#if OMP_40_ENABLED
5232 team->t.t_proc_bind = new_proc_bind;
5233#endif
5234
5235 KA_TRACE(20, ("__kmp_allocate_team: using team from pool %d.\n",
5236 team->t.t_id));
5237
5238#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00005239 __ompt_team_assign_id(team, ompt_parallel_data);
Jonathan Peyton30419822017-05-12 18:01:32 +00005240#endif
5241
5242 KMP_MB();
5243
5244 return team;
5245 }
5246
Jonathan Peyton94a114f2017-10-20 19:30:57 +00005247 /* reap team if it is too small, then loop back and check the next one */
5248 // not sure if this is wise, but, will be redone during the hot-teams
5249 // rewrite.
5250 /* TODO: Use technique to find the right size hot-team, don't reap them */
Jonathan Peyton30419822017-05-12 18:01:32 +00005251 team = __kmp_reap_team(team);
5252 __kmp_team_pool = team;
5253 }
5254
5255 /* nothing available in the pool, no matter, make a new team! */
5256 KMP_MB();
5257 team = (kmp_team_t *)__kmp_allocate(sizeof(kmp_team_t));
5258
5259 /* and set it up */
5260 team->t.t_max_nproc = max_nproc;
5261 /* NOTE well, for some reason allocating one big buffer and dividing it up
5262 seems to really hurt performance a lot on the P4, so, let's not use this */
5263 __kmp_allocate_team_arrays(team, max_nproc);
5264
5265 KA_TRACE(20, ("__kmp_allocate_team: making a new team\n"));
5266 __kmp_initialize_team(team, new_nproc, new_icvs, NULL);
5267
5268 KA_TRACE(20, ("__kmp_allocate_team: setting task_team[0] %p and task_team[1] "
5269 "%p to NULL\n",
5270 &team->t.t_task_team[0], &team->t.t_task_team[1]));
5271 team->t.t_task_team[0] = NULL; // to be removed, as __kmp_allocate zeroes
5272 // memory, no need to duplicate
5273 team->t.t_task_team[1] = NULL; // to be removed, as __kmp_allocate zeroes
5274 // memory, no need to duplicate
5275
5276 if (__kmp_storage_map) {
5277 __kmp_print_team_storage_map("team", team, team->t.t_id, new_nproc);
5278 }
5279
5280 /* allocate space for arguments */
5281 __kmp_alloc_argv_entries(argc, team, FALSE);
5282 team->t.t_argc = argc;
5283
5284 KA_TRACE(20,
5285 ("__kmp_allocate_team: team %d init arrived: join=%u, plain=%u\n",
5286 team->t.t_id, KMP_INIT_BARRIER_STATE, KMP_INIT_BARRIER_STATE));
5287 { // Initialize barrier data.
5288 int b;
5289 for (b = 0; b < bs_last_barrier; ++b) {
5290 team->t.t_bar[b].b_arrived = KMP_INIT_BARRIER_STATE;
5291#if USE_DEBUGGER
5292 team->t.t_bar[b].b_master_arrived = 0;
5293 team->t.t_bar[b].b_team_arrived = 0;
5294#endif
5295 }
5296 }
5297
5298#if OMP_40_ENABLED
5299 team->t.t_proc_bind = new_proc_bind;
5300#endif
5301
5302#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00005303 __ompt_team_assign_id(team, ompt_parallel_data);
Jonathan Peyton30419822017-05-12 18:01:32 +00005304 team->t.ompt_serialized_team_info = NULL;
5305#endif
5306
5307 KMP_MB();
5308
5309 KA_TRACE(20, ("__kmp_allocate_team: done creating a new team %d.\n",
5310 team->t.t_id));
5311
5312 return team;
Jim Cownie5e8470a2013-09-27 10:38:44 +00005313}
5314
5315/* TODO implement hot-teams at all levels */
5316/* TODO implement lazy thread release on demand (disband request) */
5317
5318/* free the team. return it to the team pool. release all the threads
5319 * associated with it */
Jonathan Peyton30419822017-05-12 18:01:32 +00005320void __kmp_free_team(kmp_root_t *root,
5321 kmp_team_t *team USE_NESTED_HOT_ARG(kmp_info_t *master)) {
5322 int f;
5323 KA_TRACE(20, ("__kmp_free_team: T#%d freeing team %d\n", __kmp_get_gtid(),
5324 team->t.t_id));
Jim Cownie5e8470a2013-09-27 10:38:44 +00005325
Jonathan Peyton30419822017-05-12 18:01:32 +00005326 /* verify state */
5327 KMP_DEBUG_ASSERT(root);
5328 KMP_DEBUG_ASSERT(team);
5329 KMP_DEBUG_ASSERT(team->t.t_nproc <= team->t.t_max_nproc);
5330 KMP_DEBUG_ASSERT(team->t.t_threads);
Jim Cownie5e8470a2013-09-27 10:38:44 +00005331
Jonathan Peyton30419822017-05-12 18:01:32 +00005332 int use_hot_team = team == root->r.r_hot_team;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00005333#if KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00005334 int level;
5335 kmp_hot_team_ptr_t *hot_teams;
5336 if (master) {
5337 level = team->t.t_active_level - 1;
5338 if (master->th.th_teams_microtask) { // in teams construct?
5339 if (master->th.th_teams_size.nteams > 1) {
5340 ++level; // level was not increased in teams construct for
5341 // team_of_masters
5342 }
5343 if (team->t.t_pkfn != (microtask_t)__kmp_teams_master &&
5344 master->th.th_teams_level == team->t.t_level) {
5345 ++level; // level was not increased in teams construct for
5346 // team_of_workers before the parallel
5347 } // team->t.t_level will be increased inside parallel
Jim Cownie4cc4bb42014-10-07 16:25:50 +00005348 }
Jonathan Peyton30419822017-05-12 18:01:32 +00005349 hot_teams = master->th.th_hot_teams;
5350 if (level < __kmp_hot_teams_max_level) {
5351 KMP_DEBUG_ASSERT(team == hot_teams[level].hot_team);
5352 use_hot_team = 1;
5353 }
5354 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00005355#endif // KMP_NESTED_HOT_TEAMS
5356
Jonathan Peyton30419822017-05-12 18:01:32 +00005357 /* team is done working */
5358 TCW_SYNC_PTR(team->t.t_pkfn,
5359 NULL); // Important for Debugging Support Library.
Jonathan Peyton37e2ef52018-07-09 17:36:22 +00005360#if KMP_OS_WINDOWS
Jonathan Peyton30419822017-05-12 18:01:32 +00005361 team->t.t_copyin_counter = 0; // init counter for possible reuse
Jonathan Peyton37e2ef52018-07-09 17:36:22 +00005362#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00005363 // Do not reset pointer to parent team to NULL for hot teams.
Jim Cownie5e8470a2013-09-27 10:38:44 +00005364
Jonathan Peyton30419822017-05-12 18:01:32 +00005365 /* if we are non-hot team, release our threads */
5366 if (!use_hot_team) {
5367 if (__kmp_tasking_mode != tskm_immediate_exec) {
5368 // Wait for threads to reach reapable state
5369 for (f = 1; f < team->t.t_nproc; ++f) {
5370 KMP_DEBUG_ASSERT(team->t.t_threads[f]);
5371 kmp_info_t *th = team->t.t_threads[f];
5372 volatile kmp_uint32 *state = &th->th.th_reap_state;
5373 while (*state != KMP_SAFE_TO_REAP) {
Andrey Churbanov581490e2017-02-06 18:53:32 +00005374#if KMP_OS_WINDOWS
Jonathan Peyton30419822017-05-12 18:01:32 +00005375 // On Windows a thread can be killed at any time, check this
5376 DWORD ecode;
5377 if (!__kmp_is_thread_alive(th, &ecode)) {
5378 *state = KMP_SAFE_TO_REAP; // reset the flag for dead thread
5379 break;
5380 }
Andrey Churbanov581490e2017-02-06 18:53:32 +00005381#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00005382 // first check if thread is sleeping
5383 kmp_flag_64 fl(&th->th.th_bar[bs_forkjoin_barrier].bb.b_go, th);
5384 if (fl.is_sleeping())
5385 fl.resume(__kmp_gtid_from_thread(th));
5386 KMP_CPU_PAUSE();
5387 }
5388 }
Andrey Churbanov581490e2017-02-06 18:53:32 +00005389
Jonathan Peyton30419822017-05-12 18:01:32 +00005390 // Delete task teams
5391 int tt_idx;
5392 for (tt_idx = 0; tt_idx < 2; ++tt_idx) {
5393 kmp_task_team_t *task_team = team->t.t_task_team[tt_idx];
5394 if (task_team != NULL) {
5395 for (f = 0; f < team->t.t_nproc;
5396 ++f) { // Have all threads unref task teams
5397 team->t.t_threads[f]->th.th_task_team = NULL;
5398 }
5399 KA_TRACE(
5400 20,
5401 ("__kmp_free_team: T#%d deactivating task_team %p on team %d\n",
5402 __kmp_get_gtid(), task_team, team->t.t_id));
Jonathan Peytonbaaccfa2015-11-16 22:48:41 +00005403#if KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00005404 __kmp_free_task_team(master, task_team);
Jonathan Peytonbaaccfa2015-11-16 22:48:41 +00005405#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00005406 team->t.t_task_team[tt_idx] = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +00005407 }
Jonathan Peyton30419822017-05-12 18:01:32 +00005408 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00005409 }
5410
Jonathan Peyton30419822017-05-12 18:01:32 +00005411 // Reset pointer to parent team only for non-hot teams.
5412 team->t.t_parent = NULL;
5413 team->t.t_level = 0;
5414 team->t.t_active_level = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00005415
Jonathan Peyton30419822017-05-12 18:01:32 +00005416 /* free the worker threads */
5417 for (f = 1; f < team->t.t_nproc; ++f) {
5418 KMP_DEBUG_ASSERT(team->t.t_threads[f]);
5419 __kmp_free_thread(team->t.t_threads[f]);
5420 team->t.t_threads[f] = NULL;
5421 }
5422
5423 /* put the team back in the team pool */
5424 /* TODO limit size of team pool, call reap_team if pool too large */
Andrey Churbanovc47afcd2017-07-03 11:24:08 +00005425 team->t.t_next_pool = CCAST(kmp_team_t *, __kmp_team_pool);
Jonathan Peyton30419822017-05-12 18:01:32 +00005426 __kmp_team_pool = (volatile kmp_team_t *)team;
5427 }
5428
5429 KMP_MB();
5430}
Jim Cownie5e8470a2013-09-27 10:38:44 +00005431
5432/* reap the team. destroy it, reclaim all its resources and free its memory */
Jonathan Peyton30419822017-05-12 18:01:32 +00005433kmp_team_t *__kmp_reap_team(kmp_team_t *team) {
5434 kmp_team_t *next_pool = team->t.t_next_pool;
Jim Cownie5e8470a2013-09-27 10:38:44 +00005435
Jonathan Peyton30419822017-05-12 18:01:32 +00005436 KMP_DEBUG_ASSERT(team);
5437 KMP_DEBUG_ASSERT(team->t.t_dispatch);
5438 KMP_DEBUG_ASSERT(team->t.t_disp_buffer);
5439 KMP_DEBUG_ASSERT(team->t.t_threads);
5440 KMP_DEBUG_ASSERT(team->t.t_argv);
Jim Cownie5e8470a2013-09-27 10:38:44 +00005441
Jonathan Peyton30419822017-05-12 18:01:32 +00005442 /* TODO clean the threads that are a part of this? */
Jim Cownie5e8470a2013-09-27 10:38:44 +00005443
Jonathan Peyton30419822017-05-12 18:01:32 +00005444 /* free stuff */
5445 __kmp_free_team_arrays(team);
5446 if (team->t.t_argv != &team->t.t_inline_argv[0])
5447 __kmp_free((void *)team->t.t_argv);
5448 __kmp_free(team);
Jim Cownie5e8470a2013-09-27 10:38:44 +00005449
Jonathan Peyton30419822017-05-12 18:01:32 +00005450 KMP_MB();
5451 return next_pool;
Jim Cownie5e8470a2013-09-27 10:38:44 +00005452}
5453
Jim Cownie5e8470a2013-09-27 10:38:44 +00005454// Free the thread. Don't reap it, just place it on the pool of available
5455// threads.
5456//
5457// Changes for Quad issue 527845: We need a predictable OMP tid <-> gtid
5458// binding for the affinity mechanism to be useful.
5459//
5460// Now, we always keep the free list (__kmp_thread_pool) sorted by gtid.
5461// However, we want to avoid a potential performance problem by always
5462// scanning through the list to find the correct point at which to insert
5463// the thread (potential N**2 behavior). To do this we keep track of the
5464// last place a thread struct was inserted (__kmp_thread_pool_insert_pt).
5465// With single-level parallelism, threads will always be added to the tail
5466// of the list, kept track of by __kmp_thread_pool_insert_pt. With nested
5467// parallelism, all bets are off and we may need to scan through the entire
5468// free list.
5469//
5470// This change also has a potentially large performance benefit, for some
5471// applications. Previously, as threads were freed from the hot team, they
5472// would be placed back on the free list in inverse order. If the hot team
5473// grew back to it's original size, then the freed thread would be placed
5474// back on the hot team in reverse order. This could cause bad cache
5475// locality problems on programs where the size of the hot team regularly
5476// grew and shrunk.
5477//
5478// Now, for single-level parallelism, the OMP tid is alway == gtid.
Jonathan Peyton30419822017-05-12 18:01:32 +00005479void __kmp_free_thread(kmp_info_t *this_th) {
5480 int gtid;
5481 kmp_info_t **scan;
Jonathan Peytonf4392462017-07-27 20:58:41 +00005482 kmp_root_t *root = this_th->th.th_root;
Jim Cownie5e8470a2013-09-27 10:38:44 +00005483
Jonathan Peyton30419822017-05-12 18:01:32 +00005484 KA_TRACE(20, ("__kmp_free_thread: T#%d putting T#%d back on free pool.\n",
5485 __kmp_get_gtid(), this_th->th.th_info.ds.ds_gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00005486
Jonathan Peyton30419822017-05-12 18:01:32 +00005487 KMP_DEBUG_ASSERT(this_th);
Jim Cownie5e8470a2013-09-27 10:38:44 +00005488
Jonathan Peyton30419822017-05-12 18:01:32 +00005489 // When moving thread to pool, switch thread to wait on own b_go flag, and
5490 // uninitialized (NULL team).
5491 int b;
5492 kmp_balign_t *balign = this_th->th.th_bar;
5493 for (b = 0; b < bs_last_barrier; ++b) {
5494 if (balign[b].bb.wait_flag == KMP_BARRIER_PARENT_FLAG)
5495 balign[b].bb.wait_flag = KMP_BARRIER_SWITCH_TO_OWN_FLAG;
5496 balign[b].bb.team = NULL;
5497 balign[b].bb.leaf_kids = 0;
5498 }
5499 this_th->th.th_task_state = 0;
Andrey Churbanov3336aa02018-03-19 18:05:15 +00005500 this_th->th.th_reap_state = KMP_SAFE_TO_REAP;
Jonathan Peyton30419822017-05-12 18:01:32 +00005501
5502 /* put thread back on the free pool */
5503 TCW_PTR(this_th->th.th_team, NULL);
5504 TCW_PTR(this_th->th.th_root, NULL);
5505 TCW_PTR(this_th->th.th_dispatch, NULL); /* NOT NEEDED */
5506
Jonathan Peytonbff8ded2018-01-10 18:24:09 +00005507 /* If the implicit task assigned to this thread can be used by other threads
5508 * -> multiple threads can share the data and try to free the task at
5509 * __kmp_reap_thread at exit. This duplicate use of the task data can happen
5510 * with higher probability when hot team is disabled but can occurs even when
5511 * the hot team is enabled */
5512 __kmp_free_implicit_task(this_th);
5513 this_th->th.th_current_task = NULL;
5514
Jonathan Peyton30419822017-05-12 18:01:32 +00005515 // If the __kmp_thread_pool_insert_pt is already past the new insert
5516 // point, then we need to re-scan the entire list.
5517 gtid = this_th->th.th_info.ds.ds_gtid;
5518 if (__kmp_thread_pool_insert_pt != NULL) {
5519 KMP_DEBUG_ASSERT(__kmp_thread_pool != NULL);
5520 if (__kmp_thread_pool_insert_pt->th.th_info.ds.ds_gtid > gtid) {
5521 __kmp_thread_pool_insert_pt = NULL;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00005522 }
Jonathan Peyton30419822017-05-12 18:01:32 +00005523 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00005524
Jonathan Peyton30419822017-05-12 18:01:32 +00005525 // Scan down the list to find the place to insert the thread.
5526 // scan is the address of a link in the list, possibly the address of
5527 // __kmp_thread_pool itself.
5528 //
5529 // In the absence of nested parallism, the for loop will have 0 iterations.
5530 if (__kmp_thread_pool_insert_pt != NULL) {
5531 scan = &(__kmp_thread_pool_insert_pt->th.th_next_pool);
5532 } else {
Andrey Churbanovc47afcd2017-07-03 11:24:08 +00005533 scan = CCAST(kmp_info_t **, &__kmp_thread_pool);
Jonathan Peyton30419822017-05-12 18:01:32 +00005534 }
5535 for (; (*scan != NULL) && ((*scan)->th.th_info.ds.ds_gtid < gtid);
5536 scan = &((*scan)->th.th_next_pool))
5537 ;
Jim Cownie5e8470a2013-09-27 10:38:44 +00005538
Jonathan Peyton30419822017-05-12 18:01:32 +00005539 // Insert the new element on the list, and set __kmp_thread_pool_insert_pt
5540 // to its address.
5541 TCW_PTR(this_th->th.th_next_pool, *scan);
5542 __kmp_thread_pool_insert_pt = *scan = this_th;
5543 KMP_DEBUG_ASSERT((this_th->th.th_next_pool == NULL) ||
5544 (this_th->th.th_info.ds.ds_gtid <
5545 this_th->th.th_next_pool->th.th_info.ds.ds_gtid));
5546 TCW_4(this_th->th.th_in_pool, TRUE);
5547 __kmp_thread_pool_nth++;
Jim Cownie5e8470a2013-09-27 10:38:44 +00005548
Jonathan Peyton30419822017-05-12 18:01:32 +00005549 TCW_4(__kmp_nth, __kmp_nth - 1);
Jonathan Peytonf4392462017-07-27 20:58:41 +00005550 root->r.r_cg_nthreads--;
Jim Cownie5e8470a2013-09-27 10:38:44 +00005551
5552#ifdef KMP_ADJUST_BLOCKTIME
Jonathan Peyton30419822017-05-12 18:01:32 +00005553 /* Adjust blocktime back to user setting or default if necessary */
5554 /* Middle initialization might never have occurred */
5555 if (!__kmp_env_blocktime && (__kmp_avail_proc > 0)) {
5556 KMP_DEBUG_ASSERT(__kmp_avail_proc > 0);
5557 if (__kmp_nth <= __kmp_avail_proc) {
5558 __kmp_zero_bt = FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00005559 }
Jonathan Peyton30419822017-05-12 18:01:32 +00005560 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00005561#endif /* KMP_ADJUST_BLOCKTIME */
5562
Jonathan Peyton30419822017-05-12 18:01:32 +00005563 KMP_MB();
Jim Cownie5e8470a2013-09-27 10:38:44 +00005564}
5565
Jim Cownie5e8470a2013-09-27 10:38:44 +00005566/* ------------------------------------------------------------------------ */
5567
Jonathan Peyton30419822017-05-12 18:01:32 +00005568void *__kmp_launch_thread(kmp_info_t *this_thr) {
5569 int gtid = this_thr->th.th_info.ds.ds_gtid;
5570 /* void *stack_data;*/
5571 kmp_team_t *(*volatile pteam);
Jim Cownie5e8470a2013-09-27 10:38:44 +00005572
Jonathan Peyton30419822017-05-12 18:01:32 +00005573 KMP_MB();
5574 KA_TRACE(10, ("__kmp_launch_thread: T#%d start\n", gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00005575
Jonathan Peyton30419822017-05-12 18:01:32 +00005576 if (__kmp_env_consistency_check) {
5577 this_thr->th.th_cons = __kmp_allocate_cons_stack(gtid); // ATT: Memory leak?
5578 }
5579
5580#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00005581 ompt_data_t *thread_data;
5582 if (ompt_enabled.enabled) {
5583 thread_data = &(this_thr->th.ompt_thread_info.thread_data);
5584 thread_data->ptr = NULL;
5585
5586 this_thr->th.ompt_thread_info.state = omp_state_overhead;
Jonathan Peyton30419822017-05-12 18:01:32 +00005587 this_thr->th.ompt_thread_info.wait_id = 0;
Joachim Protze82e94a52017-11-01 10:08:30 +00005588 this_thr->th.ompt_thread_info.idle_frame = OMPT_GET_FRAME_ADDRESS(0);
5589 if (ompt_enabled.ompt_callback_thread_begin) {
5590 ompt_callbacks.ompt_callback(ompt_callback_thread_begin)(
5591 ompt_thread_worker, thread_data);
Jim Cownie5e8470a2013-09-27 10:38:44 +00005592 }
Jonathan Peyton30419822017-05-12 18:01:32 +00005593 }
5594#endif
5595
Joachim Protze82e94a52017-11-01 10:08:30 +00005596#if OMPT_SUPPORT
5597 if (ompt_enabled.enabled) {
5598 this_thr->th.ompt_thread_info.state = omp_state_idle;
5599 }
5600#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00005601 /* This is the place where threads wait for work */
5602 while (!TCR_4(__kmp_global.g.g_done)) {
5603 KMP_DEBUG_ASSERT(this_thr == __kmp_threads[gtid]);
5604 KMP_MB();
5605
5606 /* wait for work to do */
5607 KA_TRACE(20, ("__kmp_launch_thread: T#%d waiting for work\n", gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00005608
Jonathan Peyton30419822017-05-12 18:01:32 +00005609 /* No tid yet since not part of a team */
5610 __kmp_fork_barrier(gtid, KMP_GTID_DNE);
5611
5612#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00005613 if (ompt_enabled.enabled) {
5614 this_thr->th.ompt_thread_info.state = omp_state_overhead;
Jonathan Peyton30419822017-05-12 18:01:32 +00005615 }
5616#endif
5617
5618 pteam = (kmp_team_t * (*))(&this_thr->th.th_team);
5619
5620 /* have we been allocated? */
5621 if (TCR_SYNC_PTR(*pteam) && !TCR_4(__kmp_global.g.g_done)) {
Jonathan Peyton30419822017-05-12 18:01:32 +00005622 /* we were just woken up, so run our new task */
5623 if (TCR_SYNC_PTR((*pteam)->t.t_pkfn) != NULL) {
5624 int rc;
5625 KA_TRACE(20,
5626 ("__kmp_launch_thread: T#%d(%d:%d) invoke microtask = %p\n",
5627 gtid, (*pteam)->t.t_id, __kmp_tid_from_gtid(gtid),
5628 (*pteam)->t.t_pkfn));
5629
5630 updateHWFPControl(*pteam);
5631
5632#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00005633 if (ompt_enabled.enabled) {
5634 this_thr->th.ompt_thread_info.state = omp_state_work_parallel;
Jonathan Peyton30419822017-05-12 18:01:32 +00005635 }
5636#endif
5637
5638 {
5639 KMP_TIME_PARTITIONED_BLOCK(OMP_parallel);
5640 KMP_SET_THREAD_STATE_BLOCK(IMPLICIT_TASK);
5641 rc = (*pteam)->t.t_invoke(gtid);
5642 }
5643 KMP_ASSERT(rc);
5644
Jim Cownie5e8470a2013-09-27 10:38:44 +00005645 KMP_MB();
Jonathan Peyton30419822017-05-12 18:01:32 +00005646 KA_TRACE(20, ("__kmp_launch_thread: T#%d(%d:%d) done microtask = %p\n",
5647 gtid, (*pteam)->t.t_id, __kmp_tid_from_gtid(gtid),
5648 (*pteam)->t.t_pkfn));
5649 }
Joachim Protze82e94a52017-11-01 10:08:30 +00005650#if OMPT_SUPPORT
5651 if (ompt_enabled.enabled) {
5652 /* no frame set while outside task */
Joachim Protzec255ca72017-11-05 14:11:10 +00005653 __ompt_get_task_info_object(0)->frame.exit_frame = NULL;
Joachim Protze82e94a52017-11-01 10:08:30 +00005654
5655 this_thr->th.ompt_thread_info.state = omp_state_overhead;
Jonathan Peyton30419822017-05-12 18:01:32 +00005656 }
5657#endif
Joachim Protze82e94a52017-11-01 10:08:30 +00005658 /* join barrier after parallel region */
5659 __kmp_join_barrier(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +00005660 }
Jonathan Peyton30419822017-05-12 18:01:32 +00005661 }
5662 TCR_SYNC_PTR((intptr_t)__kmp_global.g.g_done);
Jim Cownie5e8470a2013-09-27 10:38:44 +00005663
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00005664#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00005665 if (ompt_enabled.ompt_callback_thread_end) {
5666 ompt_callbacks.ompt_callback(ompt_callback_thread_end)(thread_data);
Jonathan Peyton30419822017-05-12 18:01:32 +00005667 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00005668#endif
5669
Jonathan Peyton30419822017-05-12 18:01:32 +00005670 this_thr->th.th_task_team = NULL;
5671 /* run the destructors for the threadprivate data for this thread */
5672 __kmp_common_destroy_gtid(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +00005673
Jonathan Peyton30419822017-05-12 18:01:32 +00005674 KA_TRACE(10, ("__kmp_launch_thread: T#%d done\n", gtid));
5675 KMP_MB();
5676 return this_thr;
Jim Cownie5e8470a2013-09-27 10:38:44 +00005677}
5678
5679/* ------------------------------------------------------------------------ */
Jim Cownie5e8470a2013-09-27 10:38:44 +00005680
Jonathan Peyton30419822017-05-12 18:01:32 +00005681void __kmp_internal_end_dest(void *specific_gtid) {
5682#if KMP_COMPILER_ICC
5683#pragma warning(push)
5684#pragma warning(disable : 810) // conversion from "void *" to "int" may lose
5685// significant bits
5686#endif
5687 // Make sure no significant bits are lost
5688 int gtid = (kmp_intptr_t)specific_gtid - 1;
5689#if KMP_COMPILER_ICC
5690#pragma warning(pop)
5691#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00005692
Jonathan Peyton30419822017-05-12 18:01:32 +00005693 KA_TRACE(30, ("__kmp_internal_end_dest: T#%d\n", gtid));
5694 /* NOTE: the gtid is stored as gitd+1 in the thread-local-storage
5695 * this is because 0 is reserved for the nothing-stored case */
Jim Cownie5e8470a2013-09-27 10:38:44 +00005696
Jonathan Peyton30419822017-05-12 18:01:32 +00005697 /* josh: One reason for setting the gtid specific data even when it is being
5698 destroyed by pthread is to allow gtid lookup through thread specific data
5699 (__kmp_gtid_get_specific). Some of the code, especially stat code,
5700 that gets executed in the call to __kmp_internal_end_thread, actually
5701 gets the gtid through the thread specific data. Setting it here seems
5702 rather inelegant and perhaps wrong, but allows __kmp_internal_end_thread
5703 to run smoothly.
5704 todo: get rid of this after we remove the dependence on
5705 __kmp_gtid_get_specific */
5706 if (gtid >= 0 && KMP_UBER_GTID(gtid))
5707 __kmp_gtid_set_specific(gtid);
5708#ifdef KMP_TDATA_GTID
5709 __kmp_gtid = gtid;
5710#endif
5711 __kmp_internal_end_thread(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +00005712}
5713
Jonathan Peyton99016992015-05-26 17:32:53 +00005714#if KMP_OS_UNIX && KMP_DYNAMIC_LIB
Jim Cownie5e8470a2013-09-27 10:38:44 +00005715
Jonathan Peyton30419822017-05-12 18:01:32 +00005716// 2009-09-08 (lev): It looks the destructor does not work. In simple test cases
5717// destructors work perfectly, but in real libomp.so I have no evidence it is
5718// ever called. However, -fini linker option in makefile.mk works fine.
Jim Cownie5e8470a2013-09-27 10:38:44 +00005719
Jonathan Peyton30419822017-05-12 18:01:32 +00005720__attribute__((destructor)) void __kmp_internal_end_dtor(void) {
5721 __kmp_internal_end_atexit();
Jim Cownie5e8470a2013-09-27 10:38:44 +00005722}
5723
Jonathan Peyton30419822017-05-12 18:01:32 +00005724void __kmp_internal_end_fini(void) { __kmp_internal_end_atexit(); }
Jim Cownie5e8470a2013-09-27 10:38:44 +00005725
5726#endif
5727
Jonathan Peyton30419822017-05-12 18:01:32 +00005728/* [Windows] josh: when the atexit handler is called, there may still be more
5729 than one thread alive */
5730void __kmp_internal_end_atexit(void) {
5731 KA_TRACE(30, ("__kmp_internal_end_atexit\n"));
5732 /* [Windows]
5733 josh: ideally, we want to completely shutdown the library in this atexit
5734 handler, but stat code that depends on thread specific data for gtid fails
5735 because that data becomes unavailable at some point during the shutdown, so
5736 we call __kmp_internal_end_thread instead. We should eventually remove the
5737 dependency on __kmp_get_specific_gtid in the stat code and use
5738 __kmp_internal_end_library to cleanly shutdown the library.
Jim Cownie5e8470a2013-09-27 10:38:44 +00005739
Jonathan Peyton30419822017-05-12 18:01:32 +00005740 // TODO: Can some of this comment about GVS be removed?
5741 I suspect that the offending stat code is executed when the calling thread
5742 tries to clean up a dead root thread's data structures, resulting in GVS
5743 code trying to close the GVS structures for that thread, but since the stat
5744 code uses __kmp_get_specific_gtid to get the gtid with the assumption that
5745 the calling thread is cleaning up itself instead of another thread, it get
5746 confused. This happens because allowing a thread to unregister and cleanup
5747 another thread is a recent modification for addressing an issue.
5748 Based on the current design (20050722), a thread may end up
5749 trying to unregister another thread only if thread death does not trigger
5750 the calling of __kmp_internal_end_thread. For Linux* OS, there is the
5751 thread specific data destructor function to detect thread death. For
5752 Windows dynamic, there is DllMain(THREAD_DETACH). For Windows static, there
5753 is nothing. Thus, the workaround is applicable only for Windows static
5754 stat library. */
5755 __kmp_internal_end_library(-1);
5756#if KMP_OS_WINDOWS
5757 __kmp_close_console();
5758#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00005759}
5760
Jonathan Peyton30419822017-05-12 18:01:32 +00005761static void __kmp_reap_thread(kmp_info_t *thread, int is_root) {
5762 // It is assumed __kmp_forkjoin_lock is acquired.
Jim Cownie5e8470a2013-09-27 10:38:44 +00005763
Jonathan Peyton30419822017-05-12 18:01:32 +00005764 int gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +00005765
Jonathan Peyton30419822017-05-12 18:01:32 +00005766 KMP_DEBUG_ASSERT(thread != NULL);
Jim Cownie5e8470a2013-09-27 10:38:44 +00005767
Jonathan Peyton30419822017-05-12 18:01:32 +00005768 gtid = thread->th.th_info.ds.ds_gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +00005769
Jonathan Peyton30419822017-05-12 18:01:32 +00005770 if (!is_root) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00005771
Jonathan Peyton30419822017-05-12 18:01:32 +00005772 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) {
5773 /* Assume the threads are at the fork barrier here */
5774 KA_TRACE(
5775 20, ("__kmp_reap_thread: releasing T#%d from fork barrier for reap\n",
5776 gtid));
5777 /* Need release fence here to prevent seg faults for tree forkjoin barrier
5778 * (GEH) */
5779 ANNOTATE_HAPPENS_BEFORE(thread);
5780 kmp_flag_64 flag(&thread->th.th_bar[bs_forkjoin_barrier].bb.b_go, thread);
5781 __kmp_release_64(&flag);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00005782 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00005783
Jonathan Peyton30419822017-05-12 18:01:32 +00005784 // Terminate OS thread.
5785 __kmp_reap_worker(thread);
Jonathan Peyton7ca7ef02016-11-21 16:18:57 +00005786
Jonathan Peyton30419822017-05-12 18:01:32 +00005787 // The thread was killed asynchronously. If it was actively
5788 // spinning in the thread pool, decrement the global count.
5789 //
5790 // There is a small timing hole here - if the worker thread was just waking
5791 // up after sleeping in the pool, had reset it's th_active_in_pool flag but
5792 // not decremented the global counter __kmp_thread_pool_active_nth yet, then
5793 // the global counter might not get updated.
5794 //
5795 // Currently, this can only happen as the library is unloaded,
5796 // so there are no harmful side effects.
5797 if (thread->th.th_active_in_pool) {
5798 thread->th.th_active_in_pool = FALSE;
Jonathan Peyton37e2ef52018-07-09 17:36:22 +00005799 KMP_ATOMIC_DEC(&__kmp_thread_pool_active_nth);
5800 KMP_DEBUG_ASSERT(__kmp_thread_pool_active_nth >= 0);
Jonathan Peyton30419822017-05-12 18:01:32 +00005801 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00005802
Jonathan Peyton30419822017-05-12 18:01:32 +00005803 // Decrement # of [worker] threads in the pool.
5804 KMP_DEBUG_ASSERT(__kmp_thread_pool_nth > 0);
5805 --__kmp_thread_pool_nth;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00005806 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00005807
Jonathan Peyton30419822017-05-12 18:01:32 +00005808 __kmp_free_implicit_task(thread);
Jim Cownie5e8470a2013-09-27 10:38:44 +00005809
Jonathan Peyton30419822017-05-12 18:01:32 +00005810// Free the fast memory for tasking
5811#if USE_FAST_MEMORY
5812 __kmp_free_fast_memory(thread);
5813#endif /* USE_FAST_MEMORY */
5814
5815 __kmp_suspend_uninitialize_thread(thread);
5816
5817 KMP_DEBUG_ASSERT(__kmp_threads[gtid] == thread);
5818 TCW_SYNC_PTR(__kmp_threads[gtid], NULL);
5819
5820 --__kmp_all_nth;
5821// __kmp_nth was decremented when thread is added to the pool.
Jim Cownie5e8470a2013-09-27 10:38:44 +00005822
5823#ifdef KMP_ADJUST_BLOCKTIME
Jonathan Peyton30419822017-05-12 18:01:32 +00005824 /* Adjust blocktime back to user setting or default if necessary */
5825 /* Middle initialization might never have occurred */
5826 if (!__kmp_env_blocktime && (__kmp_avail_proc > 0)) {
5827 KMP_DEBUG_ASSERT(__kmp_avail_proc > 0);
5828 if (__kmp_nth <= __kmp_avail_proc) {
5829 __kmp_zero_bt = FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00005830 }
Jonathan Peyton30419822017-05-12 18:01:32 +00005831 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00005832#endif /* KMP_ADJUST_BLOCKTIME */
5833
Jonathan Peyton30419822017-05-12 18:01:32 +00005834 /* free the memory being used */
5835 if (__kmp_env_consistency_check) {
5836 if (thread->th.th_cons) {
5837 __kmp_free_cons_stack(thread->th.th_cons);
5838 thread->th.th_cons = NULL;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00005839 }
Jonathan Peyton30419822017-05-12 18:01:32 +00005840 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00005841
Jonathan Peyton30419822017-05-12 18:01:32 +00005842 if (thread->th.th_pri_common != NULL) {
5843 __kmp_free(thread->th.th_pri_common);
5844 thread->th.th_pri_common = NULL;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00005845 }
Andrey Churbanov6d224db2015-02-10 18:37:43 +00005846
Jonathan Peyton30419822017-05-12 18:01:32 +00005847 if (thread->th.th_task_state_memo_stack != NULL) {
5848 __kmp_free(thread->th.th_task_state_memo_stack);
5849 thread->th.th_task_state_memo_stack = NULL;
5850 }
5851
5852#if KMP_USE_BGET
5853 if (thread->th.th_local.bget_data != NULL) {
5854 __kmp_finalize_bget(thread);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00005855 }
Jonathan Peyton30419822017-05-12 18:01:32 +00005856#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00005857
Alp Toker98758b02014-03-02 04:12:06 +00005858#if KMP_AFFINITY_SUPPORTED
Jonathan Peyton30419822017-05-12 18:01:32 +00005859 if (thread->th.th_affin_mask != NULL) {
5860 KMP_CPU_FREE(thread->th.th_affin_mask);
5861 thread->th.th_affin_mask = NULL;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00005862 }
Alp Toker98758b02014-03-02 04:12:06 +00005863#endif /* KMP_AFFINITY_SUPPORTED */
Jim Cownie5e8470a2013-09-27 10:38:44 +00005864
Jonathan Peytonf6399362018-07-09 17:51:13 +00005865#if KMP_USE_HIER_SCHED
5866 if (thread->th.th_hier_bar_data != NULL) {
5867 __kmp_free(thread->th.th_hier_bar_data);
5868 thread->th.th_hier_bar_data = NULL;
5869 }
5870#endif
5871
Jonathan Peyton30419822017-05-12 18:01:32 +00005872 __kmp_reap_team(thread->th.th_serial_team);
5873 thread->th.th_serial_team = NULL;
5874 __kmp_free(thread);
Jim Cownie5e8470a2013-09-27 10:38:44 +00005875
Jonathan Peyton30419822017-05-12 18:01:32 +00005876 KMP_MB();
Jim Cownie5e8470a2013-09-27 10:38:44 +00005877
5878} // __kmp_reap_thread
5879
Jonathan Peyton30419822017-05-12 18:01:32 +00005880static void __kmp_internal_end(void) {
5881 int i;
Jim Cownie5e8470a2013-09-27 10:38:44 +00005882
Jonathan Peyton30419822017-05-12 18:01:32 +00005883 /* First, unregister the library */
5884 __kmp_unregister_library();
Jim Cownie5e8470a2013-09-27 10:38:44 +00005885
Jonathan Peyton30419822017-05-12 18:01:32 +00005886#if KMP_OS_WINDOWS
5887 /* In Win static library, we can't tell when a root actually dies, so we
5888 reclaim the data structures for any root threads that have died but not
5889 unregistered themselves, in order to shut down cleanly.
5890 In Win dynamic library we also can't tell when a thread dies. */
5891 __kmp_reclaim_dead_roots(); // AC: moved here to always clean resources of
5892// dead roots
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00005893#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00005894
Jonathan Peyton30419822017-05-12 18:01:32 +00005895 for (i = 0; i < __kmp_threads_capacity; i++)
5896 if (__kmp_root[i])
5897 if (__kmp_root[i]->r.r_active)
5898 break;
5899 KMP_MB(); /* Flush all pending memory write invalidates. */
5900 TCW_SYNC_4(__kmp_global.g.g_done, TRUE);
5901
5902 if (i < __kmp_threads_capacity) {
5903#if KMP_USE_MONITOR
5904 // 2009-09-08 (lev): Other alive roots found. Why do we kill the monitor??
5905 KMP_MB(); /* Flush all pending memory write invalidates. */
5906
Jonathan Peyton94a114f2017-10-20 19:30:57 +00005907 // Need to check that monitor was initialized before reaping it. If we are
5908 // called form __kmp_atfork_child (which sets __kmp_init_parallel = 0), then
5909 // __kmp_monitor will appear to contain valid data, but it is only valid in
5910 // the parent process, not the child.
Jonathan Peyton30419822017-05-12 18:01:32 +00005911 // New behavior (201008): instead of keying off of the flag
5912 // __kmp_init_parallel, the monitor thread creation is keyed off
5913 // of the new flag __kmp_init_monitor.
5914 __kmp_acquire_bootstrap_lock(&__kmp_monitor_lock);
5915 if (TCR_4(__kmp_init_monitor)) {
5916 __kmp_reap_monitor(&__kmp_monitor);
5917 TCW_4(__kmp_init_monitor, 0);
5918 }
5919 __kmp_release_bootstrap_lock(&__kmp_monitor_lock);
5920 KA_TRACE(10, ("__kmp_internal_end: monitor reaped\n"));
5921#endif // KMP_USE_MONITOR
5922 } else {
5923/* TODO move this to cleanup code */
5924#ifdef KMP_DEBUG
5925 /* make sure that everything has properly ended */
5926 for (i = 0; i < __kmp_threads_capacity; i++) {
5927 if (__kmp_root[i]) {
5928 // KMP_ASSERT( ! KMP_UBER_GTID( i ) ); // AC:
5929 // there can be uber threads alive here
5930 KMP_ASSERT(!__kmp_root[i]->r.r_active); // TODO: can they be active?
5931 }
5932 }
5933#endif
5934
5935 KMP_MB();
5936
5937 // Reap the worker threads.
5938 // This is valid for now, but be careful if threads are reaped sooner.
5939 while (__kmp_thread_pool != NULL) { // Loop thru all the thread in the pool.
5940 // Get the next thread from the pool.
Andrey Churbanovc47afcd2017-07-03 11:24:08 +00005941 kmp_info_t *thread = CCAST(kmp_info_t *, __kmp_thread_pool);
Jonathan Peyton30419822017-05-12 18:01:32 +00005942 __kmp_thread_pool = thread->th.th_next_pool;
5943 // Reap it.
5944 KMP_DEBUG_ASSERT(thread->th.th_reap_state == KMP_SAFE_TO_REAP);
5945 thread->th.th_next_pool = NULL;
5946 thread->th.th_in_pool = FALSE;
5947 __kmp_reap_thread(thread, 0);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00005948 }
Jonathan Peyton30419822017-05-12 18:01:32 +00005949 __kmp_thread_pool_insert_pt = NULL;
5950
5951 // Reap teams.
5952 while (__kmp_team_pool != NULL) { // Loop thru all the teams in the pool.
5953 // Get the next team from the pool.
Andrey Churbanovc47afcd2017-07-03 11:24:08 +00005954 kmp_team_t *team = CCAST(kmp_team_t *, __kmp_team_pool);
Jonathan Peyton30419822017-05-12 18:01:32 +00005955 __kmp_team_pool = team->t.t_next_pool;
5956 // Reap it.
5957 team->t.t_next_pool = NULL;
5958 __kmp_reap_team(team);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00005959 }
Jonathan Peyton30419822017-05-12 18:01:32 +00005960
5961 __kmp_reap_task_teams();
5962
5963 for (i = 0; i < __kmp_threads_capacity; ++i) {
5964 // TBD: Add some checking...
5965 // Something like KMP_DEBUG_ASSERT( __kmp_thread[ i ] == NULL );
5966 }
5967
5968 /* Make sure all threadprivate destructors get run by joining with all
5969 worker threads before resetting this flag */
5970 TCW_SYNC_4(__kmp_init_common, FALSE);
5971
5972 KA_TRACE(10, ("__kmp_internal_end: all workers reaped\n"));
5973 KMP_MB();
5974
5975#if KMP_USE_MONITOR
5976 // See note above: One of the possible fixes for CQ138434 / CQ140126
5977 //
5978 // FIXME: push both code fragments down and CSE them?
5979 // push them into __kmp_cleanup() ?
5980 __kmp_acquire_bootstrap_lock(&__kmp_monitor_lock);
5981 if (TCR_4(__kmp_init_monitor)) {
5982 __kmp_reap_monitor(&__kmp_monitor);
5983 TCW_4(__kmp_init_monitor, 0);
5984 }
5985 __kmp_release_bootstrap_lock(&__kmp_monitor_lock);
5986 KA_TRACE(10, ("__kmp_internal_end: monitor reaped\n"));
5987#endif
5988 } /* else !__kmp_global.t_active */
5989 TCW_4(__kmp_init_gtid, FALSE);
5990 KMP_MB(); /* Flush all pending memory write invalidates. */
5991
5992 __kmp_cleanup();
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00005993#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00005994 ompt_fini();
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00005995#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00005996}
5997
Jonathan Peyton30419822017-05-12 18:01:32 +00005998void __kmp_internal_end_library(int gtid_req) {
5999 /* if we have already cleaned up, don't try again, it wouldn't be pretty */
6000 /* this shouldn't be a race condition because __kmp_internal_end() is the
6001 only place to clear __kmp_serial_init */
6002 /* we'll check this later too, after we get the lock */
6003 // 2009-09-06: We do not set g_abort without setting g_done. This check looks
6004 // redundaant, because the next check will work in any case.
6005 if (__kmp_global.g.g_abort) {
6006 KA_TRACE(11, ("__kmp_internal_end_library: abort, exiting\n"));
6007 /* TODO abort? */
6008 return;
6009 }
6010 if (TCR_4(__kmp_global.g.g_done) || !__kmp_init_serial) {
6011 KA_TRACE(10, ("__kmp_internal_end_library: already finished\n"));
6012 return;
6013 }
6014
6015 KMP_MB(); /* Flush all pending memory write invalidates. */
6016
6017 /* find out who we are and what we should do */
6018 {
6019 int gtid = (gtid_req >= 0) ? gtid_req : __kmp_gtid_get_specific();
6020 KA_TRACE(
6021 10, ("__kmp_internal_end_library: enter T#%d (%d)\n", gtid, gtid_req));
6022 if (gtid == KMP_GTID_SHUTDOWN) {
6023 KA_TRACE(10, ("__kmp_internal_end_library: !__kmp_init_runtime, system "
6024 "already shutdown\n"));
6025 return;
6026 } else if (gtid == KMP_GTID_MONITOR) {
6027 KA_TRACE(10, ("__kmp_internal_end_library: monitor thread, gtid not "
6028 "registered, or system shutdown\n"));
6029 return;
6030 } else if (gtid == KMP_GTID_DNE) {
6031 KA_TRACE(10, ("__kmp_internal_end_library: gtid not registered or system "
6032 "shutdown\n"));
6033 /* we don't know who we are, but we may still shutdown the library */
6034 } else if (KMP_UBER_GTID(gtid)) {
6035 /* unregister ourselves as an uber thread. gtid is no longer valid */
6036 if (__kmp_root[gtid]->r.r_active) {
6037 __kmp_global.g.g_abort = -1;
6038 TCW_SYNC_4(__kmp_global.g.g_done, TRUE);
6039 KA_TRACE(10,
6040 ("__kmp_internal_end_library: root still active, abort T#%d\n",
6041 gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00006042 return;
Jonathan Peyton30419822017-05-12 18:01:32 +00006043 } else {
6044 KA_TRACE(
6045 10,
6046 ("__kmp_internal_end_library: unregistering sibling T#%d\n", gtid));
6047 __kmp_unregister_root_current_thread(gtid);
6048 }
6049 } else {
6050/* worker threads may call this function through the atexit handler, if they
6051 * call exit() */
6052/* For now, skip the usual subsequent processing and just dump the debug buffer.
6053 TODO: do a thorough shutdown instead */
6054#ifdef DUMP_DEBUG_ON_EXIT
6055 if (__kmp_debug_buf)
6056 __kmp_dump_debug_buffer();
6057#endif
6058 return;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006059 }
Jonathan Peyton30419822017-05-12 18:01:32 +00006060 }
6061 /* synchronize the termination process */
6062 __kmp_acquire_bootstrap_lock(&__kmp_initz_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006063
Jonathan Peyton30419822017-05-12 18:01:32 +00006064 /* have we already finished */
6065 if (__kmp_global.g.g_abort) {
6066 KA_TRACE(10, ("__kmp_internal_end_library: abort, exiting\n"));
6067 /* TODO abort? */
6068 __kmp_release_bootstrap_lock(&__kmp_initz_lock);
6069 return;
6070 }
6071 if (TCR_4(__kmp_global.g.g_done) || !__kmp_init_serial) {
6072 __kmp_release_bootstrap_lock(&__kmp_initz_lock);
6073 return;
6074 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006075
Jonathan Peyton30419822017-05-12 18:01:32 +00006076 /* We need this lock to enforce mutex between this reading of
6077 __kmp_threads_capacity and the writing by __kmp_register_root.
6078 Alternatively, we can use a counter of roots that is atomically updated by
6079 __kmp_get_global_thread_id_reg, __kmp_do_serial_initialize and
6080 __kmp_internal_end_*. */
6081 __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006082
Jonathan Peyton30419822017-05-12 18:01:32 +00006083 /* now we can safely conduct the actual termination */
6084 __kmp_internal_end();
Jim Cownie5e8470a2013-09-27 10:38:44 +00006085
Jonathan Peyton30419822017-05-12 18:01:32 +00006086 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
6087 __kmp_release_bootstrap_lock(&__kmp_initz_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006088
Jonathan Peyton30419822017-05-12 18:01:32 +00006089 KA_TRACE(10, ("__kmp_internal_end_library: exit\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +00006090
Jonathan Peyton30419822017-05-12 18:01:32 +00006091#ifdef DUMP_DEBUG_ON_EXIT
6092 if (__kmp_debug_buf)
6093 __kmp_dump_debug_buffer();
6094#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00006095
Jonathan Peyton30419822017-05-12 18:01:32 +00006096#if KMP_OS_WINDOWS
6097 __kmp_close_console();
6098#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00006099
Jonathan Peyton30419822017-05-12 18:01:32 +00006100 __kmp_fini_allocator();
Jim Cownie5e8470a2013-09-27 10:38:44 +00006101
6102} // __kmp_internal_end_library
6103
Jonathan Peyton30419822017-05-12 18:01:32 +00006104void __kmp_internal_end_thread(int gtid_req) {
6105 int i;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006106
Jonathan Peyton30419822017-05-12 18:01:32 +00006107 /* if we have already cleaned up, don't try again, it wouldn't be pretty */
6108 /* this shouldn't be a race condition because __kmp_internal_end() is the
6109 * only place to clear __kmp_serial_init */
6110 /* we'll check this later too, after we get the lock */
6111 // 2009-09-06: We do not set g_abort without setting g_done. This check looks
6112 // redundant, because the next check will work in any case.
6113 if (__kmp_global.g.g_abort) {
6114 KA_TRACE(11, ("__kmp_internal_end_thread: abort, exiting\n"));
6115 /* TODO abort? */
6116 return;
6117 }
6118 if (TCR_4(__kmp_global.g.g_done) || !__kmp_init_serial) {
6119 KA_TRACE(10, ("__kmp_internal_end_thread: already finished\n"));
6120 return;
6121 }
6122
6123 KMP_MB(); /* Flush all pending memory write invalidates. */
6124
6125 /* find out who we are and what we should do */
6126 {
6127 int gtid = (gtid_req >= 0) ? gtid_req : __kmp_gtid_get_specific();
6128 KA_TRACE(10,
6129 ("__kmp_internal_end_thread: enter T#%d (%d)\n", gtid, gtid_req));
6130 if (gtid == KMP_GTID_SHUTDOWN) {
6131 KA_TRACE(10, ("__kmp_internal_end_thread: !__kmp_init_runtime, system "
6132 "already shutdown\n"));
6133 return;
6134 } else if (gtid == KMP_GTID_MONITOR) {
6135 KA_TRACE(10, ("__kmp_internal_end_thread: monitor thread, gtid not "
6136 "registered, or system shutdown\n"));
6137 return;
6138 } else if (gtid == KMP_GTID_DNE) {
6139 KA_TRACE(10, ("__kmp_internal_end_thread: gtid not registered or system "
6140 "shutdown\n"));
6141 return;
6142 /* we don't know who we are */
6143 } else if (KMP_UBER_GTID(gtid)) {
6144 /* unregister ourselves as an uber thread. gtid is no longer valid */
6145 if (__kmp_root[gtid]->r.r_active) {
6146 __kmp_global.g.g_abort = -1;
6147 TCW_SYNC_4(__kmp_global.g.g_done, TRUE);
6148 KA_TRACE(10,
6149 ("__kmp_internal_end_thread: root still active, abort T#%d\n",
6150 gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00006151 return;
Jonathan Peyton30419822017-05-12 18:01:32 +00006152 } else {
6153 KA_TRACE(10, ("__kmp_internal_end_thread: unregistering sibling T#%d\n",
6154 gtid));
6155 __kmp_unregister_root_current_thread(gtid);
6156 }
6157 } else {
6158 /* just a worker thread, let's leave */
6159 KA_TRACE(10, ("__kmp_internal_end_thread: worker thread T#%d\n", gtid));
6160
6161 if (gtid >= 0) {
6162 __kmp_threads[gtid]->th.th_task_team = NULL;
6163 }
6164
6165 KA_TRACE(10,
6166 ("__kmp_internal_end_thread: worker thread done, exiting T#%d\n",
6167 gtid));
6168 return;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006169 }
Jonathan Peyton30419822017-05-12 18:01:32 +00006170 }
6171#if defined KMP_DYNAMIC_LIB
6172 // AC: lets not shutdown the Linux* OS dynamic library at the exit of uber
6173 // thread, because we will better shutdown later in the library destructor.
6174 // The reason of this change is performance problem when non-openmp thread in
6175 // a loop forks and joins many openmp threads. We can save a lot of time
6176 // keeping worker threads alive until the program shutdown.
6177 // OM: Removed Linux* OS restriction to fix the crash on OS X* (DPD200239966)
6178 // and Windows(DPD200287443) that occurs when using critical sections from
6179 // foreign threads.
6180 KA_TRACE(10, ("__kmp_internal_end_thread: exiting T#%d\n", gtid_req));
6181 return;
6182#endif
6183 /* synchronize the termination process */
6184 __kmp_acquire_bootstrap_lock(&__kmp_initz_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006185
Jonathan Peyton30419822017-05-12 18:01:32 +00006186 /* have we already finished */
6187 if (__kmp_global.g.g_abort) {
6188 KA_TRACE(10, ("__kmp_internal_end_thread: abort, exiting\n"));
6189 /* TODO abort? */
6190 __kmp_release_bootstrap_lock(&__kmp_initz_lock);
6191 return;
6192 }
6193 if (TCR_4(__kmp_global.g.g_done) || !__kmp_init_serial) {
6194 __kmp_release_bootstrap_lock(&__kmp_initz_lock);
6195 return;
6196 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006197
Jonathan Peyton30419822017-05-12 18:01:32 +00006198 /* We need this lock to enforce mutex between this reading of
6199 __kmp_threads_capacity and the writing by __kmp_register_root.
6200 Alternatively, we can use a counter of roots that is atomically updated by
6201 __kmp_get_global_thread_id_reg, __kmp_do_serial_initialize and
6202 __kmp_internal_end_*. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00006203
Jonathan Peyton30419822017-05-12 18:01:32 +00006204 /* should we finish the run-time? are all siblings done? */
6205 __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006206
Jonathan Peyton30419822017-05-12 18:01:32 +00006207 for (i = 0; i < __kmp_threads_capacity; ++i) {
6208 if (KMP_UBER_GTID(i)) {
6209 KA_TRACE(
6210 10,
6211 ("__kmp_internal_end_thread: remaining sibling task: gtid==%d\n", i));
6212 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
6213 __kmp_release_bootstrap_lock(&__kmp_initz_lock);
6214 return;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00006215 }
Jonathan Peyton30419822017-05-12 18:01:32 +00006216 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006217
Jonathan Peyton30419822017-05-12 18:01:32 +00006218 /* now we can safely conduct the actual termination */
Jim Cownie5e8470a2013-09-27 10:38:44 +00006219
Jonathan Peyton30419822017-05-12 18:01:32 +00006220 __kmp_internal_end();
Jim Cownie5e8470a2013-09-27 10:38:44 +00006221
Jonathan Peyton30419822017-05-12 18:01:32 +00006222 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
6223 __kmp_release_bootstrap_lock(&__kmp_initz_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006224
Jonathan Peyton30419822017-05-12 18:01:32 +00006225 KA_TRACE(10, ("__kmp_internal_end_thread: exit T#%d\n", gtid_req));
Jim Cownie5e8470a2013-09-27 10:38:44 +00006226
Jonathan Peyton30419822017-05-12 18:01:32 +00006227#ifdef DUMP_DEBUG_ON_EXIT
6228 if (__kmp_debug_buf)
6229 __kmp_dump_debug_buffer();
6230#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00006231} // __kmp_internal_end_thread
6232
Jonathan Peyton30419822017-05-12 18:01:32 +00006233// -----------------------------------------------------------------------------
Jim Cownie5e8470a2013-09-27 10:38:44 +00006234// Library registration stuff.
6235
Jonathan Peyton30419822017-05-12 18:01:32 +00006236static long __kmp_registration_flag = 0;
6237// Random value used to indicate library initialization.
6238static char *__kmp_registration_str = NULL;
6239// Value to be saved in env var __KMP_REGISTERED_LIB_<pid>.
Jim Cownie5e8470a2013-09-27 10:38:44 +00006240
Jonathan Peyton30419822017-05-12 18:01:32 +00006241static inline char *__kmp_reg_status_name() {
6242 /* On RHEL 3u5 if linked statically, getpid() returns different values in
6243 each thread. If registration and unregistration go in different threads
6244 (omp_misc_other_root_exit.cpp test case), the name of registered_lib_env
6245 env var can not be found, because the name will contain different pid. */
6246 return __kmp_str_format("__KMP_REGISTERED_LIB_%d", (int)getpid());
Jim Cownie5e8470a2013-09-27 10:38:44 +00006247} // __kmp_reg_status_get
6248
Jonathan Peyton30419822017-05-12 18:01:32 +00006249void __kmp_register_library_startup(void) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00006250
Jonathan Peyton30419822017-05-12 18:01:32 +00006251 char *name = __kmp_reg_status_name(); // Name of the environment variable.
6252 int done = 0;
6253 union {
6254 double dtime;
6255 long ltime;
6256 } time;
6257#if KMP_ARCH_X86 || KMP_ARCH_X86_64
6258 __kmp_initialize_system_tick();
6259#endif
6260 __kmp_read_system_time(&time.dtime);
6261 __kmp_registration_flag = 0xCAFE0000L | (time.ltime & 0x0000FFFFL);
6262 __kmp_registration_str =
6263 __kmp_str_format("%p-%lx-%s", &__kmp_registration_flag,
6264 __kmp_registration_flag, KMP_LIBRARY_FILE);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006265
Jonathan Peyton30419822017-05-12 18:01:32 +00006266 KA_TRACE(50, ("__kmp_register_library_startup: %s=\"%s\"\n", name,
6267 __kmp_registration_str));
Jim Cownie5e8470a2013-09-27 10:38:44 +00006268
Jonathan Peyton30419822017-05-12 18:01:32 +00006269 while (!done) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00006270
Jonathan Peyton30419822017-05-12 18:01:32 +00006271 char *value = NULL; // Actual value of the environment variable.
Jim Cownie5e8470a2013-09-27 10:38:44 +00006272
Jonathan Peyton30419822017-05-12 18:01:32 +00006273 // Set environment variable, but do not overwrite if it is exist.
6274 __kmp_env_set(name, __kmp_registration_str, 0);
6275 // Check the variable is written.
6276 value = __kmp_env_get(name);
6277 if (value != NULL && strcmp(value, __kmp_registration_str) == 0) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00006278
Jonathan Peyton30419822017-05-12 18:01:32 +00006279 done = 1; // Ok, environment variable set successfully, exit the loop.
Jim Cownie5e8470a2013-09-27 10:38:44 +00006280
Jonathan Peyton30419822017-05-12 18:01:32 +00006281 } else {
Jim Cownie5e8470a2013-09-27 10:38:44 +00006282
Jonathan Peyton30419822017-05-12 18:01:32 +00006283 // Oops. Write failed. Another copy of OpenMP RTL is in memory.
6284 // Check whether it alive or dead.
6285 int neighbor = 0; // 0 -- unknown status, 1 -- alive, 2 -- dead.
6286 char *tail = value;
6287 char *flag_addr_str = NULL;
6288 char *flag_val_str = NULL;
6289 char const *file_name = NULL;
6290 __kmp_str_split(tail, '-', &flag_addr_str, &tail);
6291 __kmp_str_split(tail, '-', &flag_val_str, &tail);
6292 file_name = tail;
6293 if (tail != NULL) {
6294 long *flag_addr = 0;
6295 long flag_val = 0;
6296 KMP_SSCANF(flag_addr_str, "%p", &flag_addr);
6297 KMP_SSCANF(flag_val_str, "%lx", &flag_val);
6298 if (flag_addr != 0 && flag_val != 0 && strcmp(file_name, "") != 0) {
6299 // First, check whether environment-encoded address is mapped into
6300 // addr space.
6301 // If so, dereference it to see if it still has the right value.
6302 if (__kmp_is_address_mapped(flag_addr) && *flag_addr == flag_val) {
6303 neighbor = 1;
6304 } else {
6305 // If not, then we know the other copy of the library is no longer
6306 // running.
6307 neighbor = 2;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00006308 }
6309 }
6310 }
Jonathan Peyton30419822017-05-12 18:01:32 +00006311 switch (neighbor) {
6312 case 0: // Cannot parse environment variable -- neighbor status unknown.
6313 // Assume it is the incompatible format of future version of the
6314 // library. Assume the other library is alive.
6315 // WARN( ... ); // TODO: Issue a warning.
6316 file_name = "unknown library";
6317 // Attention! Falling to the next case. That's intentional.
6318 case 1: { // Neighbor is alive.
6319 // Check it is allowed.
6320 char *duplicate_ok = __kmp_env_get("KMP_DUPLICATE_LIB_OK");
6321 if (!__kmp_str_match_true(duplicate_ok)) {
6322 // That's not allowed. Issue fatal error.
Jonathan Peyton6a393f72017-09-05 15:43:58 +00006323 __kmp_fatal(KMP_MSG(DuplicateLibrary, KMP_LIBRARY_FILE, file_name),
6324 KMP_HNT(DuplicateLibrary), __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00006325 }
Jonathan Peyton30419822017-05-12 18:01:32 +00006326 KMP_INTERNAL_FREE(duplicate_ok);
6327 __kmp_duplicate_library_ok = 1;
6328 done = 1; // Exit the loop.
6329 } break;
6330 case 2: { // Neighbor is dead.
6331 // Clear the variable and try to register library again.
6332 __kmp_env_unset(name);
6333 } break;
6334 default: { KMP_DEBUG_ASSERT(0); } break;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00006335 }
6336 }
Jonathan Peyton30419822017-05-12 18:01:32 +00006337 KMP_INTERNAL_FREE((void *)value);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00006338 }
Jonathan Peyton30419822017-05-12 18:01:32 +00006339 KMP_INTERNAL_FREE((void *)name);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006340
6341} // func __kmp_register_library_startup
6342
Jonathan Peyton30419822017-05-12 18:01:32 +00006343void __kmp_unregister_library(void) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00006344
Jonathan Peyton30419822017-05-12 18:01:32 +00006345 char *name = __kmp_reg_status_name();
6346 char *value = __kmp_env_get(name);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006347
Jonathan Peyton30419822017-05-12 18:01:32 +00006348 KMP_DEBUG_ASSERT(__kmp_registration_flag != 0);
6349 KMP_DEBUG_ASSERT(__kmp_registration_str != NULL);
6350 if (value != NULL && strcmp(value, __kmp_registration_str) == 0) {
6351 // Ok, this is our variable. Delete it.
6352 __kmp_env_unset(name);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00006353 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006354
Jonathan Peyton30419822017-05-12 18:01:32 +00006355 KMP_INTERNAL_FREE(__kmp_registration_str);
6356 KMP_INTERNAL_FREE(value);
6357 KMP_INTERNAL_FREE(name);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006358
Jonathan Peyton30419822017-05-12 18:01:32 +00006359 __kmp_registration_flag = 0;
6360 __kmp_registration_str = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006361
6362} // __kmp_unregister_library
6363
Jim Cownie5e8470a2013-09-27 10:38:44 +00006364// End of Library registration stuff.
Jonathan Peyton30419822017-05-12 18:01:32 +00006365// -----------------------------------------------------------------------------
Jim Cownie5e8470a2013-09-27 10:38:44 +00006366
Jonathan Peyton492e0a32017-06-13 17:17:26 +00006367#if KMP_MIC_SUPPORTED
Andrey Churbanov613edeb2015-02-20 18:14:43 +00006368
Jonathan Peyton30419822017-05-12 18:01:32 +00006369static void __kmp_check_mic_type() {
6370 kmp_cpuid_t cpuid_state = {0};
6371 kmp_cpuid_t *cs_p = &cpuid_state;
6372 __kmp_x86_cpuid(1, 0, cs_p);
6373 // We don't support mic1 at the moment
6374 if ((cs_p->eax & 0xff0) == 0xB10) {
6375 __kmp_mic_type = mic2;
6376 } else if ((cs_p->eax & 0xf0ff0) == 0x50670) {
6377 __kmp_mic_type = mic3;
6378 } else {
6379 __kmp_mic_type = non_mic;
6380 }
Andrey Churbanov613edeb2015-02-20 18:14:43 +00006381}
6382
Jonathan Peyton492e0a32017-06-13 17:17:26 +00006383#endif /* KMP_MIC_SUPPORTED */
Andrey Churbanov613edeb2015-02-20 18:14:43 +00006384
Jonathan Peyton30419822017-05-12 18:01:32 +00006385static void __kmp_do_serial_initialize(void) {
6386 int i, gtid;
6387 int size;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006388
Jonathan Peyton30419822017-05-12 18:01:32 +00006389 KA_TRACE(10, ("__kmp_do_serial_initialize: enter\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +00006390
Jonathan Peyton30419822017-05-12 18:01:32 +00006391 KMP_DEBUG_ASSERT(sizeof(kmp_int32) == 4);
6392 KMP_DEBUG_ASSERT(sizeof(kmp_uint32) == 4);
6393 KMP_DEBUG_ASSERT(sizeof(kmp_int64) == 8);
6394 KMP_DEBUG_ASSERT(sizeof(kmp_uint64) == 8);
6395 KMP_DEBUG_ASSERT(sizeof(kmp_intptr_t) == sizeof(void *));
Jim Cownie5e8470a2013-09-27 10:38:44 +00006396
Jonathan Peyton82a13bf2015-09-21 18:01:02 +00006397#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00006398 ompt_pre_init();
Jonathan Peyton82a13bf2015-09-21 18:01:02 +00006399#endif
6400
Jonathan Peyton30419822017-05-12 18:01:32 +00006401 __kmp_validate_locks();
Jim Cownie5e8470a2013-09-27 10:38:44 +00006402
Jonathan Peyton30419822017-05-12 18:01:32 +00006403 /* Initialize internal memory allocator */
6404 __kmp_init_allocator();
Jim Cownie5e8470a2013-09-27 10:38:44 +00006405
Jonathan Peyton30419822017-05-12 18:01:32 +00006406 /* Register the library startup via an environment variable and check to see
6407 whether another copy of the library is already registered. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00006408
Jonathan Peyton30419822017-05-12 18:01:32 +00006409 __kmp_register_library_startup();
Jim Cownie5e8470a2013-09-27 10:38:44 +00006410
Jonathan Peyton30419822017-05-12 18:01:32 +00006411 /* TODO reinitialization of library */
6412 if (TCR_4(__kmp_global.g.g_done)) {
6413 KA_TRACE(10, ("__kmp_do_serial_initialize: reinitialization of library\n"));
6414 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006415
Jonathan Peyton30419822017-05-12 18:01:32 +00006416 __kmp_global.g.g_abort = 0;
6417 TCW_SYNC_4(__kmp_global.g.g_done, FALSE);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006418
Jonathan Peyton30419822017-05-12 18:01:32 +00006419/* initialize the locks */
Jim Cownie5e8470a2013-09-27 10:38:44 +00006420#if KMP_USE_ADAPTIVE_LOCKS
6421#if KMP_DEBUG_ADAPTIVE_LOCKS
Jonathan Peyton30419822017-05-12 18:01:32 +00006422 __kmp_init_speculative_stats();
Jim Cownie5e8470a2013-09-27 10:38:44 +00006423#endif
6424#endif
Jonathan Peytonad579922015-12-17 16:19:05 +00006425#if KMP_STATS_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00006426 __kmp_stats_init();
Jonathan Peytonad579922015-12-17 16:19:05 +00006427#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00006428 __kmp_init_lock(&__kmp_global_lock);
6429 __kmp_init_queuing_lock(&__kmp_dispatch_lock);
6430 __kmp_init_lock(&__kmp_debug_lock);
6431 __kmp_init_atomic_lock(&__kmp_atomic_lock);
6432 __kmp_init_atomic_lock(&__kmp_atomic_lock_1i);
6433 __kmp_init_atomic_lock(&__kmp_atomic_lock_2i);
6434 __kmp_init_atomic_lock(&__kmp_atomic_lock_4i);
6435 __kmp_init_atomic_lock(&__kmp_atomic_lock_4r);
6436 __kmp_init_atomic_lock(&__kmp_atomic_lock_8i);
6437 __kmp_init_atomic_lock(&__kmp_atomic_lock_8r);
6438 __kmp_init_atomic_lock(&__kmp_atomic_lock_8c);
6439 __kmp_init_atomic_lock(&__kmp_atomic_lock_10r);
6440 __kmp_init_atomic_lock(&__kmp_atomic_lock_16r);
6441 __kmp_init_atomic_lock(&__kmp_atomic_lock_16c);
6442 __kmp_init_atomic_lock(&__kmp_atomic_lock_20c);
6443 __kmp_init_atomic_lock(&__kmp_atomic_lock_32c);
6444 __kmp_init_bootstrap_lock(&__kmp_forkjoin_lock);
6445 __kmp_init_bootstrap_lock(&__kmp_exit_lock);
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00006446#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +00006447 __kmp_init_bootstrap_lock(&__kmp_monitor_lock);
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00006448#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00006449 __kmp_init_bootstrap_lock(&__kmp_tp_cached_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006450
Jonathan Peyton30419822017-05-12 18:01:32 +00006451 /* conduct initialization and initial setup of configuration */
Jim Cownie5e8470a2013-09-27 10:38:44 +00006452
Jonathan Peyton30419822017-05-12 18:01:32 +00006453 __kmp_runtime_initialize();
Jim Cownie5e8470a2013-09-27 10:38:44 +00006454
Jonathan Peyton492e0a32017-06-13 17:17:26 +00006455#if KMP_MIC_SUPPORTED
Jonathan Peyton30419822017-05-12 18:01:32 +00006456 __kmp_check_mic_type();
Andrey Churbanov613edeb2015-02-20 18:14:43 +00006457#endif
6458
Jonathan Peyton30419822017-05-12 18:01:32 +00006459// Some global variable initialization moved here from kmp_env_initialize()
Jim Cownie5e8470a2013-09-27 10:38:44 +00006460#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00006461 kmp_diag = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006462#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00006463 __kmp_abort_delay = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006464
Jonathan Peyton30419822017-05-12 18:01:32 +00006465 // From __kmp_init_dflt_team_nth()
6466 /* assume the entire machine will be used */
6467 __kmp_dflt_team_nth_ub = __kmp_xproc;
6468 if (__kmp_dflt_team_nth_ub < KMP_MIN_NTH) {
6469 __kmp_dflt_team_nth_ub = KMP_MIN_NTH;
6470 }
6471 if (__kmp_dflt_team_nth_ub > __kmp_sys_max_nth) {
6472 __kmp_dflt_team_nth_ub = __kmp_sys_max_nth;
6473 }
6474 __kmp_max_nth = __kmp_sys_max_nth;
Jonathan Peytonf4392462017-07-27 20:58:41 +00006475 __kmp_cg_max_nth = __kmp_sys_max_nth;
Jonathan Peyton4f90c822017-08-02 20:04:45 +00006476 __kmp_teams_max_nth = __kmp_xproc; // set a "reasonable" default
6477 if (__kmp_teams_max_nth > __kmp_sys_max_nth) {
6478 __kmp_teams_max_nth = __kmp_sys_max_nth;
6479 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006480
Jonathan Peyton30419822017-05-12 18:01:32 +00006481 // Three vars below moved here from __kmp_env_initialize() "KMP_BLOCKTIME"
6482 // part
6483 __kmp_dflt_blocktime = KMP_DEFAULT_BLOCKTIME;
Jonathan Peytone1c7c132016-10-07 18:12:19 +00006484#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +00006485 __kmp_monitor_wakeups =
6486 KMP_WAKEUPS_FROM_BLOCKTIME(__kmp_dflt_blocktime, __kmp_monitor_wakeups);
6487 __kmp_bt_intervals =
6488 KMP_INTERVALS_FROM_BLOCKTIME(__kmp_dflt_blocktime, __kmp_monitor_wakeups);
Jonathan Peytone1c7c132016-10-07 18:12:19 +00006489#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00006490 // From "KMP_LIBRARY" part of __kmp_env_initialize()
6491 __kmp_library = library_throughput;
6492 // From KMP_SCHEDULE initialization
6493 __kmp_static = kmp_sch_static_balanced;
6494// AC: do not use analytical here, because it is non-monotonous
6495//__kmp_guided = kmp_sch_guided_iterative_chunked;
6496//__kmp_auto = kmp_sch_guided_analytical_chunked; // AC: it is the default, no
6497// need to repeat assignment
6498// Barrier initialization. Moved here from __kmp_env_initialize() Barrier branch
6499// bit control and barrier method control parts
Jim Cownie4cc4bb42014-10-07 16:25:50 +00006500#if KMP_FAST_REDUCTION_BARRIER
Jonathan Peyton30419822017-05-12 18:01:32 +00006501#define kmp_reduction_barrier_gather_bb ((int)1)
6502#define kmp_reduction_barrier_release_bb ((int)1)
6503#define kmp_reduction_barrier_gather_pat bp_hyper_bar
6504#define kmp_reduction_barrier_release_pat bp_hyper_bar
6505#endif // KMP_FAST_REDUCTION_BARRIER
6506 for (i = bs_plain_barrier; i < bs_last_barrier; i++) {
6507 __kmp_barrier_gather_branch_bits[i] = __kmp_barrier_gather_bb_dflt;
6508 __kmp_barrier_release_branch_bits[i] = __kmp_barrier_release_bb_dflt;
6509 __kmp_barrier_gather_pattern[i] = __kmp_barrier_gather_pat_dflt;
6510 __kmp_barrier_release_pattern[i] = __kmp_barrier_release_pat_dflt;
6511#if KMP_FAST_REDUCTION_BARRIER
6512 if (i == bs_reduction_barrier) { // tested and confirmed on ALTIX only (
6513 // lin_64 ): hyper,1
6514 __kmp_barrier_gather_branch_bits[i] = kmp_reduction_barrier_gather_bb;
6515 __kmp_barrier_release_branch_bits[i] = kmp_reduction_barrier_release_bb;
6516 __kmp_barrier_gather_pattern[i] = kmp_reduction_barrier_gather_pat;
6517 __kmp_barrier_release_pattern[i] = kmp_reduction_barrier_release_pat;
Andrey Churbanov613edeb2015-02-20 18:14:43 +00006518 }
Jonathan Peyton30419822017-05-12 18:01:32 +00006519#endif // KMP_FAST_REDUCTION_BARRIER
6520 }
6521#if KMP_FAST_REDUCTION_BARRIER
6522#undef kmp_reduction_barrier_release_pat
6523#undef kmp_reduction_barrier_gather_pat
6524#undef kmp_reduction_barrier_release_bb
6525#undef kmp_reduction_barrier_gather_bb
6526#endif // KMP_FAST_REDUCTION_BARRIER
Jonathan Peyton492e0a32017-06-13 17:17:26 +00006527#if KMP_MIC_SUPPORTED
Jonathan Peyton30419822017-05-12 18:01:32 +00006528 if (__kmp_mic_type == mic2) { // KNC
6529 // AC: plane=3,2, forkjoin=2,1 are optimal for 240 threads on KNC
6530 __kmp_barrier_gather_branch_bits[bs_plain_barrier] = 3; // plain gather
6531 __kmp_barrier_release_branch_bits[bs_forkjoin_barrier] =
6532 1; // forkjoin release
6533 __kmp_barrier_gather_pattern[bs_forkjoin_barrier] = bp_hierarchical_bar;
6534 __kmp_barrier_release_pattern[bs_forkjoin_barrier] = bp_hierarchical_bar;
6535 }
6536#if KMP_FAST_REDUCTION_BARRIER
6537 if (__kmp_mic_type == mic2) { // KNC
6538 __kmp_barrier_gather_pattern[bs_reduction_barrier] = bp_hierarchical_bar;
6539 __kmp_barrier_release_pattern[bs_reduction_barrier] = bp_hierarchical_bar;
6540 }
Jonathan Peyton492e0a32017-06-13 17:17:26 +00006541#endif // KMP_FAST_REDUCTION_BARRIER
6542#endif // KMP_MIC_SUPPORTED
Jim Cownie5e8470a2013-09-27 10:38:44 +00006543
Jonathan Peyton30419822017-05-12 18:01:32 +00006544// From KMP_CHECKS initialization
Jim Cownie5e8470a2013-09-27 10:38:44 +00006545#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00006546 __kmp_env_checks = TRUE; /* development versions have the extra checks */
Jim Cownie5e8470a2013-09-27 10:38:44 +00006547#else
Jonathan Peyton30419822017-05-12 18:01:32 +00006548 __kmp_env_checks = FALSE; /* port versions do not have the extra checks */
Jim Cownie5e8470a2013-09-27 10:38:44 +00006549#endif
6550
Jonathan Peyton30419822017-05-12 18:01:32 +00006551 // From "KMP_FOREIGN_THREADS_THREADPRIVATE" initialization
6552 __kmp_foreign_tp = TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006553
Jonathan Peyton30419822017-05-12 18:01:32 +00006554 __kmp_global.g.g_dynamic = FALSE;
6555 __kmp_global.g.g_dynamic_mode = dynamic_default;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006556
Jonathan Peyton30419822017-05-12 18:01:32 +00006557 __kmp_env_initialize(NULL);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00006558
Jonathan Peyton30419822017-05-12 18:01:32 +00006559// Print all messages in message catalog for testing purposes.
6560#ifdef KMP_DEBUG
6561 char const *val = __kmp_env_get("KMP_DUMP_CATALOG");
6562 if (__kmp_str_match_true(val)) {
6563 kmp_str_buf_t buffer;
6564 __kmp_str_buf_init(&buffer);
6565 __kmp_i18n_dump_catalog(&buffer);
6566 __kmp_printf("%s", buffer.str);
6567 __kmp_str_buf_free(&buffer);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00006568 }
Jonathan Peyton30419822017-05-12 18:01:32 +00006569 __kmp_env_free(&val);
6570#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00006571
Jonathan Peyton30419822017-05-12 18:01:32 +00006572 __kmp_threads_capacity =
6573 __kmp_initial_threads_capacity(__kmp_dflt_team_nth_ub);
6574 // Moved here from __kmp_env_initialize() "KMP_ALL_THREADPRIVATE" part
6575 __kmp_tp_capacity = __kmp_default_tp_capacity(
6576 __kmp_dflt_team_nth_ub, __kmp_max_nth, __kmp_allThreadsSpecified);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006577
Jonathan Peyton30419822017-05-12 18:01:32 +00006578 // If the library is shut down properly, both pools must be NULL. Just in
6579 // case, set them to NULL -- some memory may leak, but subsequent code will
6580 // work even if pools are not freed.
6581 KMP_DEBUG_ASSERT(__kmp_thread_pool == NULL);
6582 KMP_DEBUG_ASSERT(__kmp_thread_pool_insert_pt == NULL);
6583 KMP_DEBUG_ASSERT(__kmp_team_pool == NULL);
6584 __kmp_thread_pool = NULL;
6585 __kmp_thread_pool_insert_pt = NULL;
6586 __kmp_team_pool = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006587
Jonathan Peyton30419822017-05-12 18:01:32 +00006588 /* Allocate all of the variable sized records */
6589 /* NOTE: __kmp_threads_capacity entries are allocated, but the arrays are
6590 * expandable */
6591 /* Since allocation is cache-aligned, just add extra padding at the end */
6592 size =
6593 (sizeof(kmp_info_t *) + sizeof(kmp_root_t *)) * __kmp_threads_capacity +
6594 CACHE_LINE;
6595 __kmp_threads = (kmp_info_t **)__kmp_allocate(size);
6596 __kmp_root = (kmp_root_t **)((char *)__kmp_threads +
6597 sizeof(kmp_info_t *) * __kmp_threads_capacity);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006598
Jonathan Peyton30419822017-05-12 18:01:32 +00006599 /* init thread counts */
6600 KMP_DEBUG_ASSERT(__kmp_all_nth ==
6601 0); // Asserts fail if the library is reinitializing and
6602 KMP_DEBUG_ASSERT(__kmp_nth == 0); // something was wrong in termination.
6603 __kmp_all_nth = 0;
6604 __kmp_nth = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006605
Jonathan Peyton30419822017-05-12 18:01:32 +00006606 /* setup the uber master thread and hierarchy */
6607 gtid = __kmp_register_root(TRUE);
6608 KA_TRACE(10, ("__kmp_do_serial_initialize T#%d\n", gtid));
6609 KMP_ASSERT(KMP_UBER_GTID(gtid));
6610 KMP_ASSERT(KMP_INITIAL_GTID(gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00006611
Jonathan Peyton30419822017-05-12 18:01:32 +00006612 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00006613
Jonathan Peyton30419822017-05-12 18:01:32 +00006614 __kmp_common_initialize();
Jim Cownie5e8470a2013-09-27 10:38:44 +00006615
Jonathan Peyton30419822017-05-12 18:01:32 +00006616#if KMP_OS_UNIX
6617 /* invoke the child fork handler */
6618 __kmp_register_atfork();
6619#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00006620
Jonathan Peyton30419822017-05-12 18:01:32 +00006621#if !defined KMP_DYNAMIC_LIB
6622 {
6623 /* Invoke the exit handler when the program finishes, only for static
6624 library. For dynamic library, we already have _fini and DllMain. */
6625 int rc = atexit(__kmp_internal_end_atexit);
6626 if (rc != 0) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +00006627 __kmp_fatal(KMP_MSG(FunctionError, "atexit()"), KMP_ERR(rc),
6628 __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00006629 }
Jonathan Peyton30419822017-05-12 18:01:32 +00006630 }
6631#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00006632
Jonathan Peyton30419822017-05-12 18:01:32 +00006633#if KMP_HANDLE_SIGNALS
6634#if KMP_OS_UNIX
6635 /* NOTE: make sure that this is called before the user installs their own
6636 signal handlers so that the user handlers are called first. this way they
6637 can return false, not call our handler, avoid terminating the library, and
6638 continue execution where they left off. */
6639 __kmp_install_signals(FALSE);
6640#endif /* KMP_OS_UNIX */
6641#if KMP_OS_WINDOWS
6642 __kmp_install_signals(TRUE);
6643#endif /* KMP_OS_WINDOWS */
6644#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00006645
Jonathan Peyton30419822017-05-12 18:01:32 +00006646 /* we have finished the serial initialization */
6647 __kmp_init_counter++;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006648
Jonathan Peyton30419822017-05-12 18:01:32 +00006649 __kmp_init_serial = TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006650
Jonathan Peyton30419822017-05-12 18:01:32 +00006651 if (__kmp_settings) {
6652 __kmp_env_print();
6653 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006654
6655#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00006656 if (__kmp_display_env || __kmp_display_env_verbose) {
6657 __kmp_env_print_2();
6658 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006659#endif // OMP_40_ENABLED
6660
Jonathan Peyton82a13bf2015-09-21 18:01:02 +00006661#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00006662 ompt_post_init();
Jonathan Peyton82a13bf2015-09-21 18:01:02 +00006663#endif
6664
Jonathan Peyton30419822017-05-12 18:01:32 +00006665 KMP_MB();
Jim Cownie5e8470a2013-09-27 10:38:44 +00006666
Jonathan Peyton30419822017-05-12 18:01:32 +00006667 KA_TRACE(10, ("__kmp_do_serial_initialize: exit\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +00006668}
6669
Jonathan Peyton30419822017-05-12 18:01:32 +00006670void __kmp_serial_initialize(void) {
6671 if (__kmp_init_serial) {
6672 return;
6673 }
6674 __kmp_acquire_bootstrap_lock(&__kmp_initz_lock);
6675 if (__kmp_init_serial) {
6676 __kmp_release_bootstrap_lock(&__kmp_initz_lock);
6677 return;
6678 }
6679 __kmp_do_serial_initialize();
6680 __kmp_release_bootstrap_lock(&__kmp_initz_lock);
6681}
6682
6683static void __kmp_do_middle_initialize(void) {
6684 int i, j;
6685 int prev_dflt_team_nth;
6686
6687 if (!__kmp_init_serial) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00006688 __kmp_do_serial_initialize();
Jonathan Peyton30419822017-05-12 18:01:32 +00006689 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006690
Jonathan Peyton30419822017-05-12 18:01:32 +00006691 KA_TRACE(10, ("__kmp_middle_initialize: enter\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +00006692
Jonathan Peyton30419822017-05-12 18:01:32 +00006693 // Save the previous value for the __kmp_dflt_team_nth so that
6694 // we can avoid some reinitialization if it hasn't changed.
6695 prev_dflt_team_nth = __kmp_dflt_team_nth;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006696
Alp Toker98758b02014-03-02 04:12:06 +00006697#if KMP_AFFINITY_SUPPORTED
Jonathan Peyton30419822017-05-12 18:01:32 +00006698 // __kmp_affinity_initialize() will try to set __kmp_ncores to the
6699 // number of cores on the machine.
6700 __kmp_affinity_initialize();
Jim Cownie5e8470a2013-09-27 10:38:44 +00006701
Jonathan Peyton30419822017-05-12 18:01:32 +00006702 // Run through the __kmp_threads array and set the affinity mask
6703 // for each root thread that is currently registered with the RTL.
6704 for (i = 0; i < __kmp_threads_capacity; i++) {
6705 if (TCR_PTR(__kmp_threads[i]) != NULL) {
6706 __kmp_affinity_set_init_mask(i, TRUE);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006707 }
Jonathan Peyton30419822017-05-12 18:01:32 +00006708 }
Alp Toker98758b02014-03-02 04:12:06 +00006709#endif /* KMP_AFFINITY_SUPPORTED */
Jim Cownie5e8470a2013-09-27 10:38:44 +00006710
Jonathan Peyton30419822017-05-12 18:01:32 +00006711 KMP_ASSERT(__kmp_xproc > 0);
6712 if (__kmp_avail_proc == 0) {
6713 __kmp_avail_proc = __kmp_xproc;
6714 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006715
Jonathan Peyton30419822017-05-12 18:01:32 +00006716 // If there were empty places in num_threads list (OMP_NUM_THREADS=,,2,3),
6717 // correct them now
6718 j = 0;
6719 while ((j < __kmp_nested_nth.used) && !__kmp_nested_nth.nth[j]) {
6720 __kmp_nested_nth.nth[j] = __kmp_dflt_team_nth = __kmp_dflt_team_nth_ub =
6721 __kmp_avail_proc;
6722 j++;
6723 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006724
Jonathan Peyton30419822017-05-12 18:01:32 +00006725 if (__kmp_dflt_team_nth == 0) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00006726#ifdef KMP_DFLT_NTH_CORES
Jonathan Peyton30419822017-05-12 18:01:32 +00006727 // Default #threads = #cores
6728 __kmp_dflt_team_nth = __kmp_ncores;
6729 KA_TRACE(20, ("__kmp_middle_initialize: setting __kmp_dflt_team_nth = "
6730 "__kmp_ncores (%d)\n",
6731 __kmp_dflt_team_nth));
Jim Cownie5e8470a2013-09-27 10:38:44 +00006732#else
Jonathan Peyton30419822017-05-12 18:01:32 +00006733 // Default #threads = #available OS procs
6734 __kmp_dflt_team_nth = __kmp_avail_proc;
6735 KA_TRACE(20, ("__kmp_middle_initialize: setting __kmp_dflt_team_nth = "
6736 "__kmp_avail_proc(%d)\n",
6737 __kmp_dflt_team_nth));
Jim Cownie5e8470a2013-09-27 10:38:44 +00006738#endif /* KMP_DFLT_NTH_CORES */
Jonathan Peyton30419822017-05-12 18:01:32 +00006739 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006740
Jonathan Peyton30419822017-05-12 18:01:32 +00006741 if (__kmp_dflt_team_nth < KMP_MIN_NTH) {
6742 __kmp_dflt_team_nth = KMP_MIN_NTH;
6743 }
6744 if (__kmp_dflt_team_nth > __kmp_sys_max_nth) {
6745 __kmp_dflt_team_nth = __kmp_sys_max_nth;
6746 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006747
Jonathan Peyton30419822017-05-12 18:01:32 +00006748 // There's no harm in continuing if the following check fails,
6749 // but it indicates an error in the previous logic.
6750 KMP_DEBUG_ASSERT(__kmp_dflt_team_nth <= __kmp_dflt_team_nth_ub);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006751
Jonathan Peyton30419822017-05-12 18:01:32 +00006752 if (__kmp_dflt_team_nth != prev_dflt_team_nth) {
6753 // Run through the __kmp_threads array and set the num threads icv for each
6754 // root thread that is currently registered with the RTL (which has not
6755 // already explicitly set its nthreads-var with a call to
6756 // omp_set_num_threads()).
6757 for (i = 0; i < __kmp_threads_capacity; i++) {
6758 kmp_info_t *thread = __kmp_threads[i];
6759 if (thread == NULL)
6760 continue;
6761 if (thread->th.th_current_task->td_icvs.nproc != 0)
6762 continue;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006763
Jonathan Peyton30419822017-05-12 18:01:32 +00006764 set__nproc(__kmp_threads[i], __kmp_dflt_team_nth);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006765 }
Jonathan Peyton30419822017-05-12 18:01:32 +00006766 }
6767 KA_TRACE(
6768 20,
6769 ("__kmp_middle_initialize: final value for __kmp_dflt_team_nth = %d\n",
6770 __kmp_dflt_team_nth));
Jim Cownie5e8470a2013-09-27 10:38:44 +00006771
6772#ifdef KMP_ADJUST_BLOCKTIME
Jonathan Peyton30419822017-05-12 18:01:32 +00006773 /* Adjust blocktime to zero if necessary now that __kmp_avail_proc is set */
6774 if (!__kmp_env_blocktime && (__kmp_avail_proc > 0)) {
6775 KMP_DEBUG_ASSERT(__kmp_avail_proc > 0);
6776 if (__kmp_nth > __kmp_avail_proc) {
6777 __kmp_zero_bt = TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006778 }
Jonathan Peyton30419822017-05-12 18:01:32 +00006779 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006780#endif /* KMP_ADJUST_BLOCKTIME */
6781
Jonathan Peyton30419822017-05-12 18:01:32 +00006782 /* we have finished middle initialization */
6783 TCW_SYNC_4(__kmp_init_middle, TRUE);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006784
Jonathan Peyton30419822017-05-12 18:01:32 +00006785 KA_TRACE(10, ("__kmp_do_middle_initialize: exit\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +00006786}
6787
Jonathan Peyton30419822017-05-12 18:01:32 +00006788void __kmp_middle_initialize(void) {
6789 if (__kmp_init_middle) {
6790 return;
6791 }
6792 __kmp_acquire_bootstrap_lock(&__kmp_initz_lock);
6793 if (__kmp_init_middle) {
6794 __kmp_release_bootstrap_lock(&__kmp_initz_lock);
6795 return;
6796 }
6797 __kmp_do_middle_initialize();
6798 __kmp_release_bootstrap_lock(&__kmp_initz_lock);
6799}
6800
6801void __kmp_parallel_initialize(void) {
6802 int gtid = __kmp_entry_gtid(); // this might be a new root
6803
6804 /* synchronize parallel initialization (for sibling) */
6805 if (TCR_4(__kmp_init_parallel))
6806 return;
6807 __kmp_acquire_bootstrap_lock(&__kmp_initz_lock);
6808 if (TCR_4(__kmp_init_parallel)) {
6809 __kmp_release_bootstrap_lock(&__kmp_initz_lock);
6810 return;
6811 }
6812
6813 /* TODO reinitialization after we have already shut down */
6814 if (TCR_4(__kmp_global.g.g_done)) {
6815 KA_TRACE(
6816 10,
6817 ("__kmp_parallel_initialize: attempt to init while shutting down\n"));
6818 __kmp_infinite_loop();
6819 }
6820
6821 /* jc: The lock __kmp_initz_lock is already held, so calling
6822 __kmp_serial_initialize would cause a deadlock. So we call
6823 __kmp_do_serial_initialize directly. */
6824 if (!__kmp_init_middle) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00006825 __kmp_do_middle_initialize();
Jonathan Peyton30419822017-05-12 18:01:32 +00006826 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006827
Jonathan Peyton30419822017-05-12 18:01:32 +00006828 /* begin initialization */
6829 KA_TRACE(10, ("__kmp_parallel_initialize: enter\n"));
6830 KMP_ASSERT(KMP_UBER_GTID(gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00006831
6832#if KMP_ARCH_X86 || KMP_ARCH_X86_64
Jonathan Peyton30419822017-05-12 18:01:32 +00006833 // Save the FP control regs.
6834 // Worker threads will set theirs to these values at thread startup.
6835 __kmp_store_x87_fpu_control_word(&__kmp_init_x87_fpu_control_word);
6836 __kmp_store_mxcsr(&__kmp_init_mxcsr);
6837 __kmp_init_mxcsr &= KMP_X86_MXCSR_MASK;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006838#endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
6839
6840#if KMP_OS_UNIX
Jonathan Peyton30419822017-05-12 18:01:32 +00006841#if KMP_HANDLE_SIGNALS
6842 /* must be after __kmp_serial_initialize */
6843 __kmp_install_signals(TRUE);
6844#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00006845#endif
6846
Jonathan Peyton30419822017-05-12 18:01:32 +00006847 __kmp_suspend_initialize();
Jim Cownie5e8470a2013-09-27 10:38:44 +00006848
Jonathan Peyton749b4d52016-01-27 21:02:04 +00006849#if defined(USE_LOAD_BALANCE)
Jonathan Peyton30419822017-05-12 18:01:32 +00006850 if (__kmp_global.g.g_dynamic_mode == dynamic_default) {
6851 __kmp_global.g.g_dynamic_mode = dynamic_load_balance;
6852 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006853#else
Jonathan Peyton30419822017-05-12 18:01:32 +00006854 if (__kmp_global.g.g_dynamic_mode == dynamic_default) {
6855 __kmp_global.g.g_dynamic_mode = dynamic_thread_limit;
6856 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006857#endif
6858
Jonathan Peyton30419822017-05-12 18:01:32 +00006859 if (__kmp_version) {
6860 __kmp_print_version_2();
6861 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006862
Jonathan Peyton30419822017-05-12 18:01:32 +00006863 /* we have finished parallel initialization */
6864 TCW_SYNC_4(__kmp_init_parallel, TRUE);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006865
Jonathan Peyton30419822017-05-12 18:01:32 +00006866 KMP_MB();
6867 KA_TRACE(10, ("__kmp_parallel_initialize: exit\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +00006868
Jonathan Peyton30419822017-05-12 18:01:32 +00006869 __kmp_release_bootstrap_lock(&__kmp_initz_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006870}
6871
Jim Cownie5e8470a2013-09-27 10:38:44 +00006872/* ------------------------------------------------------------------------ */
6873
Jonathan Peyton30419822017-05-12 18:01:32 +00006874void __kmp_run_before_invoked_task(int gtid, int tid, kmp_info_t *this_thr,
6875 kmp_team_t *team) {
6876 kmp_disp_t *dispatch;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006877
Jonathan Peyton30419822017-05-12 18:01:32 +00006878 KMP_MB();
Jim Cownie5e8470a2013-09-27 10:38:44 +00006879
Jonathan Peyton30419822017-05-12 18:01:32 +00006880 /* none of the threads have encountered any constructs, yet. */
6881 this_thr->th.th_local.this_construct = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006882#if KMP_CACHE_MANAGE
Jonathan Peyton30419822017-05-12 18:01:32 +00006883 KMP_CACHE_PREFETCH(&this_thr->th.th_bar[bs_forkjoin_barrier].bb.b_arrived);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006884#endif /* KMP_CACHE_MANAGE */
Jonathan Peyton30419822017-05-12 18:01:32 +00006885 dispatch = (kmp_disp_t *)TCR_PTR(this_thr->th.th_dispatch);
6886 KMP_DEBUG_ASSERT(dispatch);
6887 KMP_DEBUG_ASSERT(team->t.t_dispatch);
6888 // KMP_DEBUG_ASSERT( this_thr->th.th_dispatch == &team->t.t_dispatch[
6889 // this_thr->th.th_info.ds.ds_tid ] );
Jim Cownie5e8470a2013-09-27 10:38:44 +00006890
Jonathan Peyton30419822017-05-12 18:01:32 +00006891 dispatch->th_disp_index = 0; /* reset the dispatch buffer counter */
Jonathan Peytondf6818b2016-06-14 17:57:47 +00006892#if OMP_45_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00006893 dispatch->th_doacross_buf_idx =
6894 0; /* reset the doacross dispatch buffer counter */
Jonathan Peyton71909c52016-03-02 22:42:06 +00006895#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00006896 if (__kmp_env_consistency_check)
6897 __kmp_push_parallel(gtid, team->t.t_ident);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006898
Jonathan Peyton30419822017-05-12 18:01:32 +00006899 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00006900}
6901
Jonathan Peyton30419822017-05-12 18:01:32 +00006902void __kmp_run_after_invoked_task(int gtid, int tid, kmp_info_t *this_thr,
6903 kmp_team_t *team) {
6904 if (__kmp_env_consistency_check)
6905 __kmp_pop_parallel(gtid, team->t.t_ident);
Andrey Churbanovdf0d75e2016-10-27 11:43:07 +00006906
Jonathan Peyton30419822017-05-12 18:01:32 +00006907 __kmp_finish_implicit_task(this_thr);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006908}
6909
Jonathan Peyton30419822017-05-12 18:01:32 +00006910int __kmp_invoke_task_func(int gtid) {
6911 int rc;
6912 int tid = __kmp_tid_from_gtid(gtid);
6913 kmp_info_t *this_thr = __kmp_threads[gtid];
6914 kmp_team_t *team = this_thr->th.th_team;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006915
Jonathan Peyton30419822017-05-12 18:01:32 +00006916 __kmp_run_before_invoked_task(gtid, tid, this_thr, team);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006917#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00006918 if (__itt_stack_caller_create_ptr) {
6919 __kmp_itt_stack_callee_enter(
6920 (__itt_caller)
6921 team->t.t_stack_id); // inform ittnotify about entering user's code
6922 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006923#endif /* USE_ITT_BUILD */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00006924#if INCLUDE_SSC_MARKS
Jonathan Peyton30419822017-05-12 18:01:32 +00006925 SSC_MARK_INVOKING();
Jim Cownie4cc4bb42014-10-07 16:25:50 +00006926#endif
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00006927
6928#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00006929 void *dummy;
6930 void **exit_runtime_p;
Joachim Protze82e94a52017-11-01 10:08:30 +00006931 ompt_data_t *my_task_data;
6932 ompt_data_t *my_parallel_data;
6933 int ompt_team_size;
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00006934
Joachim Protze82e94a52017-11-01 10:08:30 +00006935 if (ompt_enabled.enabled) {
Joachim Protzec255ca72017-11-05 14:11:10 +00006936 exit_runtime_p = &(
6937 team->t.t_implicit_task_taskdata[tid].ompt_task_info.frame.exit_frame);
Jonathan Peyton30419822017-05-12 18:01:32 +00006938 } else {
6939 exit_runtime_p = &dummy;
6940 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00006941
Joachim Protze82e94a52017-11-01 10:08:30 +00006942 my_task_data =
6943 &(team->t.t_implicit_task_taskdata[tid].ompt_task_info.task_data);
6944 my_parallel_data = &(team->t.ompt_team_info.parallel_data);
6945 if (ompt_enabled.ompt_callback_implicit_task) {
6946 ompt_team_size = team->t.t_nproc;
6947 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
6948 ompt_scope_begin, my_parallel_data, my_task_data, ompt_team_size,
6949 __kmp_tid_from_gtid(gtid));
Joachim Protze9be9cf22018-05-07 12:42:21 +00006950 OMPT_CUR_TASK_INFO(this_thr)->thread_num = __kmp_tid_from_gtid(gtid);
Jonathan Peyton30419822017-05-12 18:01:32 +00006951 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00006952#endif
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00006953
Jonathan Peyton30419822017-05-12 18:01:32 +00006954 {
6955 KMP_TIME_PARTITIONED_BLOCK(OMP_parallel);
6956 KMP_SET_THREAD_STATE_BLOCK(IMPLICIT_TASK);
6957 rc =
6958 __kmp_invoke_microtask((microtask_t)TCR_SYNC_PTR(team->t.t_pkfn), gtid,
6959 tid, (int)team->t.t_argc, (void **)team->t.t_argv
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00006960#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00006961 ,
6962 exit_runtime_p
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00006963#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00006964 );
Jonas Hahnfeld8a270642016-09-14 13:59:19 +00006965#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00006966 *exit_runtime_p = NULL;
Jonas Hahnfeld8a270642016-09-14 13:59:19 +00006967#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00006968 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00006969
Jim Cownie5e8470a2013-09-27 10:38:44 +00006970#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00006971 if (__itt_stack_caller_create_ptr) {
6972 __kmp_itt_stack_callee_leave(
6973 (__itt_caller)
6974 team->t.t_stack_id); // inform ittnotify about leaving user's code
6975 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006976#endif /* USE_ITT_BUILD */
Jonathan Peyton30419822017-05-12 18:01:32 +00006977 __kmp_run_after_invoked_task(gtid, tid, this_thr, team);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006978
Jonathan Peyton30419822017-05-12 18:01:32 +00006979 return rc;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006980}
6981
6982#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00006983void __kmp_teams_master(int gtid) {
6984 // This routine is called by all master threads in teams construct
6985 kmp_info_t *thr = __kmp_threads[gtid];
6986 kmp_team_t *team = thr->th.th_team;
6987 ident_t *loc = team->t.t_ident;
6988 thr->th.th_set_nproc = thr->th.th_teams_size.nth;
6989 KMP_DEBUG_ASSERT(thr->th.th_teams_microtask);
6990 KMP_DEBUG_ASSERT(thr->th.th_set_nproc);
6991 KA_TRACE(20, ("__kmp_teams_master: T#%d, Tid %d, microtask %p\n", gtid,
6992 __kmp_tid_from_gtid(gtid), thr->th.th_teams_microtask));
6993// Launch league of teams now, but not let workers execute
6994// (they hang on fork barrier until next parallel)
Jim Cownie4cc4bb42014-10-07 16:25:50 +00006995#if INCLUDE_SSC_MARKS
Jonathan Peyton30419822017-05-12 18:01:32 +00006996 SSC_MARK_FORKING();
Jim Cownie4cc4bb42014-10-07 16:25:50 +00006997#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00006998 __kmp_fork_call(loc, gtid, fork_context_intel, team->t.t_argc,
Jonathan Peyton30419822017-05-12 18:01:32 +00006999 (microtask_t)thr->th.th_teams_microtask, // "wrapped" task
7000 VOLATILE_CAST(launch_t) __kmp_invoke_task_func, NULL);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00007001#if INCLUDE_SSC_MARKS
Jonathan Peyton30419822017-05-12 18:01:32 +00007002 SSC_MARK_JOINING();
Jim Cownie4cc4bb42014-10-07 16:25:50 +00007003#endif
Jonathan Peyton61118492016-05-20 19:03:38 +00007004
Jonathan Peyton30419822017-05-12 18:01:32 +00007005 // AC: last parameter "1" eliminates join barrier which won't work because
7006 // worker threads are in a fork barrier waiting for more parallel regions
7007 __kmp_join_call(loc, gtid
Jonathan Peytonf89fbbb2015-08-31 18:15:00 +00007008#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00007009 ,
7010 fork_context_intel
Jonathan Peytonf89fbbb2015-08-31 18:15:00 +00007011#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00007012 ,
7013 1);
Jim Cownie5e8470a2013-09-27 10:38:44 +00007014}
7015
Jonathan Peyton30419822017-05-12 18:01:32 +00007016int __kmp_invoke_teams_master(int gtid) {
7017 kmp_info_t *this_thr = __kmp_threads[gtid];
7018 kmp_team_t *team = this_thr->th.th_team;
7019#if KMP_DEBUG
7020 if (!__kmp_threads[gtid]->th.th_team->t.t_serialized)
7021 KMP_DEBUG_ASSERT((void *)__kmp_threads[gtid]->th.th_team->t.t_pkfn ==
7022 (void *)__kmp_teams_master);
7023#endif
7024 __kmp_run_before_invoked_task(gtid, 0, this_thr, team);
7025 __kmp_teams_master(gtid);
7026 __kmp_run_after_invoked_task(gtid, 0, this_thr, team);
7027 return 1;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007028}
7029#endif /* OMP_40_ENABLED */
7030
7031/* this sets the requested number of threads for the next parallel region
Jonathan Peyton30419822017-05-12 18:01:32 +00007032 encountered by this team. since this should be enclosed in the forkjoin
7033 critical section it should avoid race conditions with assymmetrical nested
7034 parallelism */
Jim Cownie5e8470a2013-09-27 10:38:44 +00007035
Jonathan Peyton30419822017-05-12 18:01:32 +00007036void __kmp_push_num_threads(ident_t *id, int gtid, int num_threads) {
7037 kmp_info_t *thr = __kmp_threads[gtid];
Jim Cownie5e8470a2013-09-27 10:38:44 +00007038
Jonathan Peyton30419822017-05-12 18:01:32 +00007039 if (num_threads > 0)
7040 thr->th.th_set_nproc = num_threads;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007041}
7042
7043#if OMP_40_ENABLED
7044
7045/* this sets the requested number of teams for the teams region and/or
Jonathan Peyton30419822017-05-12 18:01:32 +00007046 the number of threads for the next parallel region encountered */
7047void __kmp_push_num_teams(ident_t *id, int gtid, int num_teams,
7048 int num_threads) {
7049 kmp_info_t *thr = __kmp_threads[gtid];
7050 KMP_DEBUG_ASSERT(num_teams >= 0);
7051 KMP_DEBUG_ASSERT(num_threads >= 0);
Jonathan Peyton1be692e2015-11-30 20:14:05 +00007052
Jonathan Peyton30419822017-05-12 18:01:32 +00007053 if (num_teams == 0)
7054 num_teams = 1; // default number of teams is 1.
Jonathan Peyton4f90c822017-08-02 20:04:45 +00007055 if (num_teams > __kmp_teams_max_nth) { // if too many teams requested?
Jonathan Peyton30419822017-05-12 18:01:32 +00007056 if (!__kmp_reserve_warn) {
7057 __kmp_reserve_warn = 1;
7058 __kmp_msg(kmp_ms_warning,
Jonathan Peyton4f90c822017-08-02 20:04:45 +00007059 KMP_MSG(CantFormThrTeam, num_teams, __kmp_teams_max_nth),
Jonathan Peyton30419822017-05-12 18:01:32 +00007060 KMP_HNT(Unset_ALL_THREADS), __kmp_msg_null);
Jim Cownie5e8470a2013-09-27 10:38:44 +00007061 }
Jonathan Peyton4f90c822017-08-02 20:04:45 +00007062 num_teams = __kmp_teams_max_nth;
Jonathan Peyton30419822017-05-12 18:01:32 +00007063 }
7064 // Set number of teams (number of threads in the outer "parallel" of the
7065 // teams)
7066 thr->th.th_set_nproc = thr->th.th_teams_size.nteams = num_teams;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00007067
Jonathan Peyton30419822017-05-12 18:01:32 +00007068 // Remember the number of threads for inner parallel regions
7069 if (num_threads == 0) {
7070 if (!TCR_4(__kmp_init_middle))
7071 __kmp_middle_initialize(); // get __kmp_avail_proc calculated
7072 num_threads = __kmp_avail_proc / num_teams;
Jonathan Peyton4f90c822017-08-02 20:04:45 +00007073 if (num_teams * num_threads > __kmp_teams_max_nth) {
Jonathan Peyton30419822017-05-12 18:01:32 +00007074 // adjust num_threads w/o warning as it is not user setting
Jonathan Peyton4f90c822017-08-02 20:04:45 +00007075 num_threads = __kmp_teams_max_nth / num_teams;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007076 }
Jonathan Peyton30419822017-05-12 18:01:32 +00007077 } else {
Jonathan Peyton4f90c822017-08-02 20:04:45 +00007078 if (num_teams * num_threads > __kmp_teams_max_nth) {
7079 int new_threads = __kmp_teams_max_nth / num_teams;
Jonathan Peyton30419822017-05-12 18:01:32 +00007080 if (!__kmp_reserve_warn) { // user asked for too many threads
Jonathan Peyton4f90c822017-08-02 20:04:45 +00007081 __kmp_reserve_warn = 1; // that conflicts with KMP_TEAMS_THREAD_LIMIT
Jonathan Peyton30419822017-05-12 18:01:32 +00007082 __kmp_msg(kmp_ms_warning,
7083 KMP_MSG(CantFormThrTeam, num_threads, new_threads),
7084 KMP_HNT(Unset_ALL_THREADS), __kmp_msg_null);
7085 }
7086 num_threads = new_threads;
7087 }
7088 }
7089 thr->th.th_teams_size.nth = num_threads;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007090}
7091
Jim Cownie5e8470a2013-09-27 10:38:44 +00007092// Set the proc_bind var to use in the following parallel region.
Jonathan Peyton30419822017-05-12 18:01:32 +00007093void __kmp_push_proc_bind(ident_t *id, int gtid, kmp_proc_bind_t proc_bind) {
7094 kmp_info_t *thr = __kmp_threads[gtid];
7095 thr->th.th_set_proc_bind = proc_bind;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007096}
7097
7098#endif /* OMP_40_ENABLED */
7099
7100/* Launch the worker threads into the microtask. */
7101
Jonathan Peyton30419822017-05-12 18:01:32 +00007102void __kmp_internal_fork(ident_t *id, int gtid, kmp_team_t *team) {
7103 kmp_info_t *this_thr = __kmp_threads[gtid];
Jim Cownie5e8470a2013-09-27 10:38:44 +00007104
7105#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00007106 int f;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007107#endif /* KMP_DEBUG */
7108
Jonathan Peyton30419822017-05-12 18:01:32 +00007109 KMP_DEBUG_ASSERT(team);
7110 KMP_DEBUG_ASSERT(this_thr->th.th_team == team);
7111 KMP_ASSERT(KMP_MASTER_GTID(gtid));
7112 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00007113
Jonathan Peyton30419822017-05-12 18:01:32 +00007114 team->t.t_construct = 0; /* no single directives seen yet */
7115 team->t.t_ordered.dt.t_value =
7116 0; /* thread 0 enters the ordered section first */
Jim Cownie5e8470a2013-09-27 10:38:44 +00007117
Jonathan Peyton30419822017-05-12 18:01:32 +00007118 /* Reset the identifiers on the dispatch buffer */
7119 KMP_DEBUG_ASSERT(team->t.t_disp_buffer);
7120 if (team->t.t_max_nproc > 1) {
7121 int i;
7122 for (i = 0; i < __kmp_dispatch_num_buffers; ++i) {
7123 team->t.t_disp_buffer[i].buffer_index = i;
Jonathan Peytondf6818b2016-06-14 17:57:47 +00007124#if OMP_45_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00007125 team->t.t_disp_buffer[i].doacross_buf_idx = i;
Jonathan Peyton71909c52016-03-02 22:42:06 +00007126#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00007127 }
Jonathan Peyton30419822017-05-12 18:01:32 +00007128 } else {
7129 team->t.t_disp_buffer[0].buffer_index = 0;
7130#if OMP_45_ENABLED
7131 team->t.t_disp_buffer[0].doacross_buf_idx = 0;
7132#endif
7133 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00007134
Jonathan Peyton30419822017-05-12 18:01:32 +00007135 KMP_MB(); /* Flush all pending memory write invalidates. */
7136 KMP_ASSERT(this_thr->th.th_team == team);
Jim Cownie5e8470a2013-09-27 10:38:44 +00007137
7138#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00007139 for (f = 0; f < team->t.t_nproc; f++) {
7140 KMP_DEBUG_ASSERT(team->t.t_threads[f] &&
7141 team->t.t_threads[f]->th.th_team_nproc == team->t.t_nproc);
7142 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00007143#endif /* KMP_DEBUG */
7144
Jonathan Peyton30419822017-05-12 18:01:32 +00007145 /* release the worker threads so they may begin working */
7146 __kmp_fork_barrier(gtid, 0);
Jim Cownie5e8470a2013-09-27 10:38:44 +00007147}
7148
Jonathan Peyton30419822017-05-12 18:01:32 +00007149void __kmp_internal_join(ident_t *id, int gtid, kmp_team_t *team) {
7150 kmp_info_t *this_thr = __kmp_threads[gtid];
Jim Cownie5e8470a2013-09-27 10:38:44 +00007151
Jonathan Peyton30419822017-05-12 18:01:32 +00007152 KMP_DEBUG_ASSERT(team);
7153 KMP_DEBUG_ASSERT(this_thr->th.th_team == team);
7154 KMP_ASSERT(KMP_MASTER_GTID(gtid));
7155 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00007156
Jonathan Peyton30419822017-05-12 18:01:32 +00007157/* Join barrier after fork */
Jim Cownie5e8470a2013-09-27 10:38:44 +00007158
7159#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00007160 if (__kmp_threads[gtid] &&
7161 __kmp_threads[gtid]->th.th_team_nproc != team->t.t_nproc) {
7162 __kmp_printf("GTID: %d, __kmp_threads[%d]=%p\n", gtid, gtid,
7163 __kmp_threads[gtid]);
7164 __kmp_printf("__kmp_threads[%d]->th.th_team_nproc=%d, TEAM: %p, "
7165 "team->t.t_nproc=%d\n",
7166 gtid, __kmp_threads[gtid]->th.th_team_nproc, team,
7167 team->t.t_nproc);
7168 __kmp_print_structure();
7169 }
7170 KMP_DEBUG_ASSERT(__kmp_threads[gtid] &&
7171 __kmp_threads[gtid]->th.th_team_nproc == team->t.t_nproc);
Jim Cownie5e8470a2013-09-27 10:38:44 +00007172#endif /* KMP_DEBUG */
7173
Jonathan Peyton30419822017-05-12 18:01:32 +00007174 __kmp_join_barrier(gtid); /* wait for everyone */
Joachim Protze82e94a52017-11-01 10:08:30 +00007175#if OMPT_SUPPORT
Jonas Hahnfeld82768d02018-02-23 16:46:25 +00007176 if (ompt_enabled.enabled &&
7177 this_thr->th.ompt_thread_info.state == omp_state_wait_barrier_implicit) {
7178 int ds_tid = this_thr->th.th_info.ds.ds_tid;
7179 ompt_data_t *task_data = OMPT_CUR_TASK_DATA(this_thr);
Joachim Protze82e94a52017-11-01 10:08:30 +00007180 this_thr->th.ompt_thread_info.state = omp_state_overhead;
7181#if OMPT_OPTIONAL
7182 void *codeptr = NULL;
7183 if (KMP_MASTER_TID(ds_tid) &&
7184 (ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait) ||
7185 ompt_callbacks.ompt_callback(ompt_callback_sync_region)))
7186 codeptr = OMPT_CUR_TEAM_INFO(this_thr)->master_return_address;
7187
7188 if (ompt_enabled.ompt_callback_sync_region_wait) {
7189 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
Jonas Hahnfeld82768d02018-02-23 16:46:25 +00007190 ompt_sync_region_barrier, ompt_scope_end, NULL, task_data, codeptr);
Joachim Protze82e94a52017-11-01 10:08:30 +00007191 }
7192 if (ompt_enabled.ompt_callback_sync_region) {
7193 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
Jonas Hahnfeld82768d02018-02-23 16:46:25 +00007194 ompt_sync_region_barrier, ompt_scope_end, NULL, task_data, codeptr);
Joachim Protze82e94a52017-11-01 10:08:30 +00007195 }
7196#endif
7197 if (!KMP_MASTER_TID(ds_tid) && ompt_enabled.ompt_callback_implicit_task) {
7198 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
Jonas Hahnfeld82768d02018-02-23 16:46:25 +00007199 ompt_scope_end, NULL, task_data, 0, ds_tid);
Joachim Protze82e94a52017-11-01 10:08:30 +00007200 }
Joachim Protze82e94a52017-11-01 10:08:30 +00007201 }
7202#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00007203
Jonathan Peyton30419822017-05-12 18:01:32 +00007204 KMP_MB(); /* Flush all pending memory write invalidates. */
7205 KMP_ASSERT(this_thr->th.th_team == team);
Jim Cownie5e8470a2013-09-27 10:38:44 +00007206}
7207
Jim Cownie5e8470a2013-09-27 10:38:44 +00007208/* ------------------------------------------------------------------------ */
7209
7210#ifdef USE_LOAD_BALANCE
7211
Jim Cownie5e8470a2013-09-27 10:38:44 +00007212// Return the worker threads actively spinning in the hot team, if we
7213// are at the outermost level of parallelism. Otherwise, return 0.
Jonathan Peyton30419822017-05-12 18:01:32 +00007214static int __kmp_active_hot_team_nproc(kmp_root_t *root) {
7215 int i;
7216 int retval;
7217 kmp_team_t *hot_team;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007218
Jonathan Peyton30419822017-05-12 18:01:32 +00007219 if (root->r.r_active) {
7220 return 0;
7221 }
7222 hot_team = root->r.r_hot_team;
7223 if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME) {
7224 return hot_team->t.t_nproc - 1; // Don't count master thread
7225 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00007226
Jonathan Peyton30419822017-05-12 18:01:32 +00007227 // Skip the master thread - it is accounted for elsewhere.
7228 retval = 0;
7229 for (i = 1; i < hot_team->t.t_nproc; i++) {
7230 if (hot_team->t.t_threads[i]->th.th_active) {
7231 retval++;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007232 }
Jonathan Peyton30419822017-05-12 18:01:32 +00007233 }
7234 return retval;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007235}
7236
Jim Cownie5e8470a2013-09-27 10:38:44 +00007237// Perform an automatic adjustment to the number of
7238// threads used by the next parallel region.
Jonathan Peyton30419822017-05-12 18:01:32 +00007239static int __kmp_load_balance_nproc(kmp_root_t *root, int set_nproc) {
7240 int retval;
7241 int pool_active;
7242 int hot_team_active;
7243 int team_curr_active;
7244 int system_active;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007245
Jonathan Peyton30419822017-05-12 18:01:32 +00007246 KB_TRACE(20, ("__kmp_load_balance_nproc: called root:%p set_nproc:%d\n", root,
7247 set_nproc));
7248 KMP_DEBUG_ASSERT(root);
7249 KMP_DEBUG_ASSERT(root->r.r_root_team->t.t_threads[0]
7250 ->th.th_current_task->td_icvs.dynamic == TRUE);
7251 KMP_DEBUG_ASSERT(set_nproc > 1);
Jim Cownie5e8470a2013-09-27 10:38:44 +00007252
Jonathan Peyton30419822017-05-12 18:01:32 +00007253 if (set_nproc == 1) {
7254 KB_TRACE(20, ("__kmp_load_balance_nproc: serial execution.\n"));
7255 return 1;
7256 }
7257
7258 // Threads that are active in the thread pool, active in the hot team for this
7259 // particular root (if we are at the outer par level), and the currently
7260 // executing thread (to become the master) are available to add to the new
7261 // team, but are currently contributing to the system load, and must be
7262 // accounted for.
Jonathan Peyton37e2ef52018-07-09 17:36:22 +00007263 pool_active = __kmp_thread_pool_active_nth;
Jonathan Peyton30419822017-05-12 18:01:32 +00007264 hot_team_active = __kmp_active_hot_team_nproc(root);
7265 team_curr_active = pool_active + hot_team_active + 1;
7266
7267 // Check the system load.
7268 system_active = __kmp_get_load_balance(__kmp_avail_proc + team_curr_active);
7269 KB_TRACE(30, ("__kmp_load_balance_nproc: system active = %d pool active = %d "
7270 "hot team active = %d\n",
7271 system_active, pool_active, hot_team_active));
7272
7273 if (system_active < 0) {
7274 // There was an error reading the necessary info from /proc, so use the
7275 // thread limit algorithm instead. Once we set __kmp_global.g.g_dynamic_mode
7276 // = dynamic_thread_limit, we shouldn't wind up getting back here.
7277 __kmp_global.g.g_dynamic_mode = dynamic_thread_limit;
7278 KMP_WARNING(CantLoadBalUsing, "KMP_DYNAMIC_MODE=thread limit");
7279
7280 // Make this call behave like the thread limit algorithm.
7281 retval = __kmp_avail_proc - __kmp_nth +
7282 (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc);
7283 if (retval > set_nproc) {
7284 retval = set_nproc;
7285 }
7286 if (retval < KMP_MIN_NTH) {
7287 retval = KMP_MIN_NTH;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007288 }
7289
Jonathan Peyton30419822017-05-12 18:01:32 +00007290 KB_TRACE(20, ("__kmp_load_balance_nproc: thread limit exit. retval:%d\n",
7291 retval));
Jim Cownie5e8470a2013-09-27 10:38:44 +00007292 return retval;
Jonathan Peyton30419822017-05-12 18:01:32 +00007293 }
7294
7295 // There is a slight delay in the load balance algorithm in detecting new
7296 // running procs. The real system load at this instant should be at least as
7297 // large as the #active omp thread that are available to add to the team.
7298 if (system_active < team_curr_active) {
7299 system_active = team_curr_active;
7300 }
7301 retval = __kmp_avail_proc - system_active + team_curr_active;
7302 if (retval > set_nproc) {
7303 retval = set_nproc;
7304 }
7305 if (retval < KMP_MIN_NTH) {
7306 retval = KMP_MIN_NTH;
7307 }
7308
7309 KB_TRACE(20, ("__kmp_load_balance_nproc: exit. retval:%d\n", retval));
7310 return retval;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007311} // __kmp_load_balance_nproc()
7312
7313#endif /* USE_LOAD_BALANCE */
7314
Jim Cownie5e8470a2013-09-27 10:38:44 +00007315/* ------------------------------------------------------------------------ */
Jim Cownie5e8470a2013-09-27 10:38:44 +00007316
7317/* NOTE: this is called with the __kmp_init_lock held */
Jonathan Peyton30419822017-05-12 18:01:32 +00007318void __kmp_cleanup(void) {
7319 int f;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007320
Jonathan Peyton30419822017-05-12 18:01:32 +00007321 KA_TRACE(10, ("__kmp_cleanup: enter\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +00007322
Jonathan Peyton30419822017-05-12 18:01:32 +00007323 if (TCR_4(__kmp_init_parallel)) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00007324#if KMP_HANDLE_SIGNALS
Jonathan Peyton30419822017-05-12 18:01:32 +00007325 __kmp_remove_signals();
Jim Cownie5e8470a2013-09-27 10:38:44 +00007326#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00007327 TCW_4(__kmp_init_parallel, FALSE);
7328 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00007329
Jonathan Peyton30419822017-05-12 18:01:32 +00007330 if (TCR_4(__kmp_init_middle)) {
Alp Toker763b9392014-02-28 09:42:41 +00007331#if KMP_AFFINITY_SUPPORTED
Jonathan Peyton30419822017-05-12 18:01:32 +00007332 __kmp_affinity_uninitialize();
Alp Toker763b9392014-02-28 09:42:41 +00007333#endif /* KMP_AFFINITY_SUPPORTED */
Jonathan Peyton30419822017-05-12 18:01:32 +00007334 __kmp_cleanup_hierarchy();
7335 TCW_4(__kmp_init_middle, FALSE);
7336 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00007337
Jonathan Peyton30419822017-05-12 18:01:32 +00007338 KA_TRACE(10, ("__kmp_cleanup: go serial cleanup\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +00007339
Jonathan Peyton30419822017-05-12 18:01:32 +00007340 if (__kmp_init_serial) {
7341 __kmp_runtime_destroy();
7342 __kmp_init_serial = FALSE;
7343 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00007344
Andrey Churbanov9e9333a2018-03-05 18:42:01 +00007345 __kmp_cleanup_threadprivate_caches();
7346
Jonathan Peyton30419822017-05-12 18:01:32 +00007347 for (f = 0; f < __kmp_threads_capacity; f++) {
7348 if (__kmp_root[f] != NULL) {
7349 __kmp_free(__kmp_root[f]);
7350 __kmp_root[f] = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007351 }
Jonathan Peyton30419822017-05-12 18:01:32 +00007352 }
7353 __kmp_free(__kmp_threads);
7354 // __kmp_threads and __kmp_root were allocated at once, as single block, so
7355 // there is no need in freeing __kmp_root.
7356 __kmp_threads = NULL;
7357 __kmp_root = NULL;
7358 __kmp_threads_capacity = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007359
Andrey Churbanov5c56fb52015-02-20 18:05:17 +00007360#if KMP_USE_DYNAMIC_LOCK
Jonathan Peyton30419822017-05-12 18:01:32 +00007361 __kmp_cleanup_indirect_user_locks();
Andrey Churbanov5c56fb52015-02-20 18:05:17 +00007362#else
Jonathan Peyton30419822017-05-12 18:01:32 +00007363 __kmp_cleanup_user_locks();
Andrey Churbanov5c56fb52015-02-20 18:05:17 +00007364#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00007365
Jonathan Peyton30419822017-05-12 18:01:32 +00007366#if KMP_AFFINITY_SUPPORTED
Andrey Churbanovc47afcd2017-07-03 11:24:08 +00007367 KMP_INTERNAL_FREE(CCAST(char *, __kmp_cpuinfo_file));
Jonathan Peyton30419822017-05-12 18:01:32 +00007368 __kmp_cpuinfo_file = NULL;
7369#endif /* KMP_AFFINITY_SUPPORTED */
Jim Cownie5e8470a2013-09-27 10:38:44 +00007370
Jonathan Peyton30419822017-05-12 18:01:32 +00007371#if KMP_USE_ADAPTIVE_LOCKS
7372#if KMP_DEBUG_ADAPTIVE_LOCKS
7373 __kmp_print_speculative_stats();
7374#endif
7375#endif
7376 KMP_INTERNAL_FREE(__kmp_nested_nth.nth);
7377 __kmp_nested_nth.nth = NULL;
7378 __kmp_nested_nth.size = 0;
7379 __kmp_nested_nth.used = 0;
7380 KMP_INTERNAL_FREE(__kmp_nested_proc_bind.bind_types);
7381 __kmp_nested_proc_bind.bind_types = NULL;
7382 __kmp_nested_proc_bind.size = 0;
7383 __kmp_nested_proc_bind.used = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007384
Jonathan Peyton30419822017-05-12 18:01:32 +00007385 __kmp_i18n_catclose();
Jim Cownie5e8470a2013-09-27 10:38:44 +00007386
Jonathan Peytonf6399362018-07-09 17:51:13 +00007387#if KMP_USE_HIER_SCHED
7388 __kmp_hier_scheds.deallocate();
7389#endif
7390
Jim Cownie4cc4bb42014-10-07 16:25:50 +00007391#if KMP_STATS_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00007392 __kmp_stats_fini();
Jim Cownie4cc4bb42014-10-07 16:25:50 +00007393#endif
7394
Jonathan Peyton30419822017-05-12 18:01:32 +00007395 KA_TRACE(10, ("__kmp_cleanup: exit\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +00007396}
7397
7398/* ------------------------------------------------------------------------ */
Jonathan Peyton30419822017-05-12 18:01:32 +00007399
7400int __kmp_ignore_mppbeg(void) {
7401 char *env;
7402
7403 if ((env = getenv("KMP_IGNORE_MPPBEG")) != NULL) {
7404 if (__kmp_str_match_false(env))
7405 return FALSE;
7406 }
7407 // By default __kmpc_begin() is no-op.
7408 return TRUE;
7409}
7410
7411int __kmp_ignore_mppend(void) {
7412 char *env;
7413
7414 if ((env = getenv("KMP_IGNORE_MPPEND")) != NULL) {
7415 if (__kmp_str_match_false(env))
7416 return FALSE;
7417 }
7418 // By default __kmpc_end() is no-op.
7419 return TRUE;
7420}
7421
7422void __kmp_internal_begin(void) {
7423 int gtid;
7424 kmp_root_t *root;
7425
7426 /* this is a very important step as it will register new sibling threads
7427 and assign these new uber threads a new gtid */
7428 gtid = __kmp_entry_gtid();
7429 root = __kmp_threads[gtid]->th.th_root;
7430 KMP_ASSERT(KMP_UBER_GTID(gtid));
7431
7432 if (root->r.r_begin)
7433 return;
7434 __kmp_acquire_lock(&root->r.r_begin_lock, gtid);
7435 if (root->r.r_begin) {
7436 __kmp_release_lock(&root->r.r_begin_lock, gtid);
7437 return;
7438 }
7439
7440 root->r.r_begin = TRUE;
7441
7442 __kmp_release_lock(&root->r.r_begin_lock, gtid);
7443}
7444
Jim Cownie5e8470a2013-09-27 10:38:44 +00007445/* ------------------------------------------------------------------------ */
7446
Jonathan Peyton30419822017-05-12 18:01:32 +00007447void __kmp_user_set_library(enum library_type arg) {
7448 int gtid;
7449 kmp_root_t *root;
7450 kmp_info_t *thread;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007451
Jonathan Peyton30419822017-05-12 18:01:32 +00007452 /* first, make sure we are initialized so we can get our gtid */
7453
7454 gtid = __kmp_entry_gtid();
7455 thread = __kmp_threads[gtid];
7456
7457 root = thread->th.th_root;
7458
7459 KA_TRACE(20, ("__kmp_user_set_library: enter T#%d, arg: %d, %d\n", gtid, arg,
7460 library_serial));
7461 if (root->r.r_in_parallel) { /* Must be called in serial section of top-level
7462 thread */
7463 KMP_WARNING(SetLibraryIncorrectCall);
7464 return;
7465 }
7466
7467 switch (arg) {
7468 case library_serial:
7469 thread->th.th_set_nproc = 0;
7470 set__nproc(thread, 1);
7471 break;
7472 case library_turnaround:
7473 thread->th.th_set_nproc = 0;
7474 set__nproc(thread, __kmp_dflt_team_nth ? __kmp_dflt_team_nth
7475 : __kmp_dflt_team_nth_ub);
7476 break;
7477 case library_throughput:
7478 thread->th.th_set_nproc = 0;
7479 set__nproc(thread, __kmp_dflt_team_nth ? __kmp_dflt_team_nth
7480 : __kmp_dflt_team_nth_ub);
7481 break;
7482 default:
7483 KMP_FATAL(UnknownLibraryType, arg);
7484 }
7485
7486 __kmp_aux_set_library(arg);
Jim Cownie5e8470a2013-09-27 10:38:44 +00007487}
7488
Jonathan Peyton30419822017-05-12 18:01:32 +00007489void __kmp_aux_set_stacksize(size_t arg) {
7490 if (!__kmp_init_serial)
7491 __kmp_serial_initialize();
Jim Cownie5e8470a2013-09-27 10:38:44 +00007492
7493#if KMP_OS_DARWIN
Jonathan Peyton30419822017-05-12 18:01:32 +00007494 if (arg & (0x1000 - 1)) {
7495 arg &= ~(0x1000 - 1);
7496 if (arg + 0x1000) /* check for overflow if we round up */
7497 arg += 0x1000;
7498 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00007499#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00007500 __kmp_acquire_bootstrap_lock(&__kmp_initz_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00007501
Jonathan Peyton30419822017-05-12 18:01:32 +00007502 /* only change the default stacksize before the first parallel region */
7503 if (!TCR_4(__kmp_init_parallel)) {
7504 size_t value = arg; /* argument is in bytes */
Jim Cownie5e8470a2013-09-27 10:38:44 +00007505
Jonathan Peyton30419822017-05-12 18:01:32 +00007506 if (value < __kmp_sys_min_stksize)
7507 value = __kmp_sys_min_stksize;
7508 else if (value > KMP_MAX_STKSIZE)
7509 value = KMP_MAX_STKSIZE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007510
Jonathan Peyton30419822017-05-12 18:01:32 +00007511 __kmp_stksize = value;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007512
Jonathan Peyton30419822017-05-12 18:01:32 +00007513 __kmp_env_stksize = TRUE; /* was KMP_STACKSIZE specified? */
7514 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00007515
Jonathan Peyton30419822017-05-12 18:01:32 +00007516 __kmp_release_bootstrap_lock(&__kmp_initz_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00007517}
7518
7519/* set the behaviour of the runtime library */
7520/* TODO this can cause some odd behaviour with sibling parallelism... */
Jonathan Peyton30419822017-05-12 18:01:32 +00007521void __kmp_aux_set_library(enum library_type arg) {
7522 __kmp_library = arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007523
Jonathan Peyton30419822017-05-12 18:01:32 +00007524 switch (__kmp_library) {
7525 case library_serial: {
7526 KMP_INFORM(LibraryIsSerial);
7527 (void)__kmp_change_library(TRUE);
7528 } break;
7529 case library_turnaround:
7530 (void)__kmp_change_library(TRUE);
7531 break;
7532 case library_throughput:
7533 (void)__kmp_change_library(FALSE);
7534 break;
7535 default:
7536 KMP_FATAL(UnknownLibraryType, arg);
7537 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00007538}
7539
7540/* ------------------------------------------------------------------------ */
Jim Cownie5e8470a2013-09-27 10:38:44 +00007541
Jonathan Peyton30419822017-05-12 18:01:32 +00007542void __kmp_aux_set_blocktime(int arg, kmp_info_t *thread, int tid) {
7543 int blocktime = arg; /* argument is in milliseconds */
Jonathan Peytone1c7c132016-10-07 18:12:19 +00007544#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +00007545 int bt_intervals;
Jonathan Peytone1c7c132016-10-07 18:12:19 +00007546#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00007547 int bt_set;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007548
Jonathan Peyton30419822017-05-12 18:01:32 +00007549 __kmp_save_internal_controls(thread);
Jim Cownie5e8470a2013-09-27 10:38:44 +00007550
Jonathan Peyton30419822017-05-12 18:01:32 +00007551 /* Normalize and set blocktime for the teams */
7552 if (blocktime < KMP_MIN_BLOCKTIME)
7553 blocktime = KMP_MIN_BLOCKTIME;
7554 else if (blocktime > KMP_MAX_BLOCKTIME)
7555 blocktime = KMP_MAX_BLOCKTIME;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007556
Jonathan Peyton30419822017-05-12 18:01:32 +00007557 set__blocktime_team(thread->th.th_team, tid, blocktime);
7558 set__blocktime_team(thread->th.th_serial_team, 0, blocktime);
Jim Cownie5e8470a2013-09-27 10:38:44 +00007559
Jonathan Peytone1c7c132016-10-07 18:12:19 +00007560#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +00007561 /* Calculate and set blocktime intervals for the teams */
7562 bt_intervals = KMP_INTERVALS_FROM_BLOCKTIME(blocktime, __kmp_monitor_wakeups);
Jim Cownie5e8470a2013-09-27 10:38:44 +00007563
Jonathan Peyton30419822017-05-12 18:01:32 +00007564 set__bt_intervals_team(thread->th.th_team, tid, bt_intervals);
7565 set__bt_intervals_team(thread->th.th_serial_team, 0, bt_intervals);
Jonathan Peytone1c7c132016-10-07 18:12:19 +00007566#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00007567
Jonathan Peyton30419822017-05-12 18:01:32 +00007568 /* Set whether blocktime has been set to "TRUE" */
7569 bt_set = TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007570
Jonathan Peyton30419822017-05-12 18:01:32 +00007571 set__bt_set_team(thread->th.th_team, tid, bt_set);
7572 set__bt_set_team(thread->th.th_serial_team, 0, bt_set);
Jonathan Peytone1c7c132016-10-07 18:12:19 +00007573#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +00007574 KF_TRACE(10, ("kmp_set_blocktime: T#%d(%d:%d), blocktime=%d, "
7575 "bt_intervals=%d, monitor_updates=%d\n",
7576 __kmp_gtid_from_tid(tid, thread->th.th_team),
7577 thread->th.th_team->t.t_id, tid, blocktime, bt_intervals,
7578 __kmp_monitor_wakeups));
Samuel Antao33515192016-10-20 13:20:17 +00007579#else
Jonathan Peyton30419822017-05-12 18:01:32 +00007580 KF_TRACE(10, ("kmp_set_blocktime: T#%d(%d:%d), blocktime=%d\n",
7581 __kmp_gtid_from_tid(tid, thread->th.th_team),
7582 thread->th.th_team->t.t_id, tid, blocktime));
Jonathan Peytone1c7c132016-10-07 18:12:19 +00007583#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00007584}
7585
Jonathan Peyton30419822017-05-12 18:01:32 +00007586void __kmp_aux_set_defaults(char const *str, int len) {
7587 if (!__kmp_init_serial) {
7588 __kmp_serial_initialize();
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00007589 }
Jonathan Peyton30419822017-05-12 18:01:32 +00007590 __kmp_env_initialize(str);
Jim Cownie5e8470a2013-09-27 10:38:44 +00007591
Jonathan Peyton30419822017-05-12 18:01:32 +00007592 if (__kmp_settings
Jim Cownie5e8470a2013-09-27 10:38:44 +00007593#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00007594 || __kmp_display_env || __kmp_display_env_verbose
Jim Cownie5e8470a2013-09-27 10:38:44 +00007595#endif // OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00007596 ) {
7597 __kmp_env_print();
7598 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00007599} // __kmp_aux_set_defaults
7600
7601/* ------------------------------------------------------------------------ */
Jonathan Peyton30419822017-05-12 18:01:32 +00007602/* internal fast reduction routines */
Jim Cownie5e8470a2013-09-27 10:38:44 +00007603
Jim Cownie5e8470a2013-09-27 10:38:44 +00007604PACKED_REDUCTION_METHOD_T
Jonathan Peyton30419822017-05-12 18:01:32 +00007605__kmp_determine_reduction_method(
7606 ident_t *loc, kmp_int32 global_tid, kmp_int32 num_vars, size_t reduce_size,
7607 void *reduce_data, void (*reduce_func)(void *lhs_data, void *rhs_data),
7608 kmp_critical_name *lck) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00007609
Jonathan Peyton30419822017-05-12 18:01:32 +00007610 // Default reduction method: critical construct ( lck != NULL, like in current
7611 // PAROPT )
7612 // If ( reduce_data!=NULL && reduce_func!=NULL ): the tree-reduction method
7613 // can be selected by RTL
7614 // If loc->flags contains KMP_IDENT_ATOMIC_REDUCE, the atomic reduce method
7615 // can be selected by RTL
7616 // Finally, it's up to OpenMP RTL to make a decision on which method to select
7617 // among generated by PAROPT.
Jim Cownie5e8470a2013-09-27 10:38:44 +00007618
Jonathan Peyton30419822017-05-12 18:01:32 +00007619 PACKED_REDUCTION_METHOD_T retval;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007620
Jonathan Peyton30419822017-05-12 18:01:32 +00007621 int team_size;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007622
Jonathan Peyton30419822017-05-12 18:01:32 +00007623 KMP_DEBUG_ASSERT(loc); // it would be nice to test ( loc != 0 )
7624 KMP_DEBUG_ASSERT(lck); // it would be nice to test ( lck != 0 )
Jim Cownie5e8470a2013-09-27 10:38:44 +00007625
Jonathan Peyton30419822017-05-12 18:01:32 +00007626#define FAST_REDUCTION_ATOMIC_METHOD_GENERATED \
7627 ((loc->flags & (KMP_IDENT_ATOMIC_REDUCE)) == (KMP_IDENT_ATOMIC_REDUCE))
7628#define FAST_REDUCTION_TREE_METHOD_GENERATED ((reduce_data) && (reduce_func))
Jim Cownie5e8470a2013-09-27 10:38:44 +00007629
Jonathan Peyton30419822017-05-12 18:01:32 +00007630 retval = critical_reduce_block;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007631
Jonathan Peyton30419822017-05-12 18:01:32 +00007632 // another choice of getting a team size (with 1 dynamic deference) is slower
7633 team_size = __kmp_get_team_num_threads(global_tid);
7634 if (team_size == 1) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00007635
Jonathan Peyton30419822017-05-12 18:01:32 +00007636 retval = empty_reduce_block;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007637
Jonathan Peyton30419822017-05-12 18:01:32 +00007638 } else {
Jim Cownie5e8470a2013-09-27 10:38:44 +00007639
Jonathan Peyton30419822017-05-12 18:01:32 +00007640 int atomic_available = FAST_REDUCTION_ATOMIC_METHOD_GENERATED;
7641 int tree_available = FAST_REDUCTION_TREE_METHOD_GENERATED;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007642
Jonathan Peyton30419822017-05-12 18:01:32 +00007643#if KMP_ARCH_X86_64 || KMP_ARCH_PPC64 || KMP_ARCH_AARCH64 || KMP_ARCH_MIPS64
Jim Cownie5e8470a2013-09-27 10:38:44 +00007644
Jonathan Peyton30419822017-05-12 18:01:32 +00007645#if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD || KMP_OS_WINDOWS || \
7646 KMP_OS_DARWIN
Jim Cownie5e8470a2013-09-27 10:38:44 +00007647
Jonathan Peyton30419822017-05-12 18:01:32 +00007648 int teamsize_cutoff = 4;
Jonathan Peyton91b78702015-06-08 19:39:07 +00007649
Jonathan Peyton492e0a32017-06-13 17:17:26 +00007650#if KMP_MIC_SUPPORTED
Jonathan Peyton30419822017-05-12 18:01:32 +00007651 if (__kmp_mic_type != non_mic) {
7652 teamsize_cutoff = 8;
7653 }
Andrey Churbanov613edeb2015-02-20 18:14:43 +00007654#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00007655 if (tree_available) {
7656 if (team_size <= teamsize_cutoff) {
7657 if (atomic_available) {
7658 retval = atomic_reduce_block;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007659 }
Jonathan Peyton30419822017-05-12 18:01:32 +00007660 } else {
7661 retval = TREE_REDUCE_BLOCK_WITH_REDUCTION_BARRIER;
7662 }
7663 } else if (atomic_available) {
7664 retval = atomic_reduce_block;
7665 }
7666#else
7667#error "Unknown or unsupported OS"
7668#endif // KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD || KMP_OS_WINDOWS ||
7669// KMP_OS_DARWIN
Jim Cownie5e8470a2013-09-27 10:38:44 +00007670
Jonathan Peyton30419822017-05-12 18:01:32 +00007671#elif KMP_ARCH_X86 || KMP_ARCH_ARM || KMP_ARCH_AARCH || KMP_ARCH_MIPS
7672
7673#if KMP_OS_LINUX || KMP_OS_WINDOWS
7674
7675 // basic tuning
7676
7677 if (atomic_available) {
7678 if (num_vars <= 2) { // && ( team_size <= 8 ) due to false-sharing ???
7679 retval = atomic_reduce_block;
7680 }
7681 } // otherwise: use critical section
7682
7683#elif KMP_OS_DARWIN
7684
7685 if (atomic_available && (num_vars <= 3)) {
7686 retval = atomic_reduce_block;
7687 } else if (tree_available) {
7688 if ((reduce_size > (9 * sizeof(kmp_real64))) &&
7689 (reduce_size < (2000 * sizeof(kmp_real64)))) {
7690 retval = TREE_REDUCE_BLOCK_WITH_PLAIN_BARRIER;
7691 }
7692 } // otherwise: use critical section
7693
7694#else
7695#error "Unknown or unsupported OS"
7696#endif
7697
7698#else
7699#error "Unknown or unsupported architecture"
7700#endif
7701 }
7702
7703 // KMP_FORCE_REDUCTION
7704
7705 // If the team is serialized (team_size == 1), ignore the forced reduction
7706 // method and stay with the unsynchronized method (empty_reduce_block)
7707 if (__kmp_force_reduction_method != reduction_method_not_defined &&
7708 team_size != 1) {
7709
7710 PACKED_REDUCTION_METHOD_T forced_retval = critical_reduce_block;
7711
7712 int atomic_available, tree_available;
7713
7714 switch ((forced_retval = __kmp_force_reduction_method)) {
7715 case critical_reduce_block:
7716 KMP_ASSERT(lck); // lck should be != 0
7717 break;
7718
7719 case atomic_reduce_block:
7720 atomic_available = FAST_REDUCTION_ATOMIC_METHOD_GENERATED;
7721 if (!atomic_available) {
7722 KMP_WARNING(RedMethodNotSupported, "atomic");
7723 forced_retval = critical_reduce_block;
7724 }
7725 break;
7726
7727 case tree_reduce_block:
7728 tree_available = FAST_REDUCTION_TREE_METHOD_GENERATED;
7729 if (!tree_available) {
7730 KMP_WARNING(RedMethodNotSupported, "tree");
7731 forced_retval = critical_reduce_block;
7732 } else {
7733#if KMP_FAST_REDUCTION_BARRIER
7734 forced_retval = TREE_REDUCE_BLOCK_WITH_REDUCTION_BARRIER;
7735#endif
7736 }
7737 break;
7738
7739 default:
7740 KMP_ASSERT(0); // "unsupported method specified"
Jim Cownie5e8470a2013-09-27 10:38:44 +00007741 }
7742
Jonathan Peyton30419822017-05-12 18:01:32 +00007743 retval = forced_retval;
7744 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00007745
Jonathan Peyton30419822017-05-12 18:01:32 +00007746 KA_TRACE(10, ("reduction method selected=%08x\n", retval));
Jim Cownie5e8470a2013-09-27 10:38:44 +00007747
Jonathan Peyton30419822017-05-12 18:01:32 +00007748#undef FAST_REDUCTION_TREE_METHOD_GENERATED
7749#undef FAST_REDUCTION_ATOMIC_METHOD_GENERATED
7750
7751 return (retval);
Jim Cownie5e8470a2013-09-27 10:38:44 +00007752}
7753
7754// this function is for testing set/get/determine reduce method
Jonathan Peyton30419822017-05-12 18:01:32 +00007755kmp_int32 __kmp_get_reduce_method(void) {
7756 return ((__kmp_entry_thread()->th.th_local.packed_reduction_method) >> 8);
Jim Cownie5e8470a2013-09-27 10:38:44 +00007757}