blob: e0c15cdf5ec8e4a18c9e843a780f4e21eaeb3dd7 [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
Jonathan Peyton92ca6182018-09-07 18:25:49 +0000542static void __kmp_init_allocator() {
543#if OMP_50_ENABLED
544 __kmp_init_memkind();
545#endif
546}
547static void __kmp_fini_allocator() {
548#if OMP_50_ENABLED
549 __kmp_fini_memkind();
550#endif
551}
Jim Cownie5e8470a2013-09-27 10:38:44 +0000552
553/* ------------------------------------------------------------------------ */
554
Jonathan Peyton99016992015-05-26 17:32:53 +0000555#ifdef KMP_DYNAMIC_LIB
Jonathan Peyton30419822017-05-12 18:01:32 +0000556#if KMP_OS_WINDOWS
Jim Cownie5e8470a2013-09-27 10:38:44 +0000557
Jonathan Peyton30419822017-05-12 18:01:32 +0000558static void __kmp_reset_lock(kmp_bootstrap_lock_t *lck) {
559 // TODO: Change to __kmp_break_bootstrap_lock().
560 __kmp_init_bootstrap_lock(lck); // make the lock released
Jim Cownie5e8470a2013-09-27 10:38:44 +0000561}
562
Jonathan Peyton30419822017-05-12 18:01:32 +0000563static void __kmp_reset_locks_on_process_detach(int gtid_req) {
564 int i;
565 int thread_count;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000566
Jonathan Peyton30419822017-05-12 18:01:32 +0000567 // PROCESS_DETACH is expected to be called by a thread that executes
568 // ProcessExit() or FreeLibrary(). OS terminates other threads (except the one
569 // calling ProcessExit or FreeLibrary). So, it might be safe to access the
570 // __kmp_threads[] without taking the forkjoin_lock. However, in fact, some
571 // threads can be still alive here, although being about to be terminated. The
572 // threads in the array with ds_thread==0 are most suspicious. Actually, it
573 // can be not safe to access the __kmp_threads[].
Jim Cownie5e8470a2013-09-27 10:38:44 +0000574
Jonathan Peyton30419822017-05-12 18:01:32 +0000575 // TODO: does it make sense to check __kmp_roots[] ?
Jim Cownie5e8470a2013-09-27 10:38:44 +0000576
Jonathan Peyton30419822017-05-12 18:01:32 +0000577 // Let's check that there are no other alive threads registered with the OMP
578 // lib.
579 while (1) {
580 thread_count = 0;
581 for (i = 0; i < __kmp_threads_capacity; ++i) {
582 if (!__kmp_threads)
583 continue;
584 kmp_info_t *th = __kmp_threads[i];
585 if (th == NULL)
586 continue;
587 int gtid = th->th.th_info.ds.ds_gtid;
588 if (gtid == gtid_req)
589 continue;
590 if (gtid < 0)
591 continue;
592 DWORD exit_val;
593 int alive = __kmp_is_thread_alive(th, &exit_val);
594 if (alive) {
595 ++thread_count;
596 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000597 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000598 if (thread_count == 0)
599 break; // success
600 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000601
Jonathan Peyton30419822017-05-12 18:01:32 +0000602 // Assume that I'm alone. Now it might be safe to check and reset locks.
603 // __kmp_forkjoin_lock and __kmp_stdio_lock are expected to be reset.
604 __kmp_reset_lock(&__kmp_forkjoin_lock);
605#ifdef KMP_DEBUG
606 __kmp_reset_lock(&__kmp_stdio_lock);
607#endif // KMP_DEBUG
Jim Cownie5e8470a2013-09-27 10:38:44 +0000608}
609
Jonathan Peyton30419822017-05-12 18:01:32 +0000610BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpReserved) {
611 //__kmp_acquire_bootstrap_lock( &__kmp_initz_lock );
Jim Cownie5e8470a2013-09-27 10:38:44 +0000612
Jonathan Peyton30419822017-05-12 18:01:32 +0000613 switch (fdwReason) {
Jim Cownie5e8470a2013-09-27 10:38:44 +0000614
Jonathan Peyton30419822017-05-12 18:01:32 +0000615 case DLL_PROCESS_ATTACH:
616 KA_TRACE(10, ("DllMain: PROCESS_ATTACH\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000617
618 return TRUE;
Jonathan Peyton30419822017-05-12 18:01:32 +0000619
620 case DLL_PROCESS_DETACH:
621 KA_TRACE(10, ("DllMain: PROCESS_DETACH T#%d\n", __kmp_gtid_get_specific()));
622
623 if (lpReserved != NULL) {
624 // lpReserved is used for telling the difference:
625 // lpReserved == NULL when FreeLibrary() was called,
626 // lpReserved != NULL when the process terminates.
627 // When FreeLibrary() is called, worker threads remain alive. So they will
628 // release the forkjoin lock by themselves. When the process terminates,
629 // worker threads disappear triggering the problem of unreleased forkjoin
630 // lock as described below.
631
632 // A worker thread can take the forkjoin lock. The problem comes up if
633 // that worker thread becomes dead before it releases the forkjoin lock.
634 // The forkjoin lock remains taken, while the thread executing
635 // DllMain()->PROCESS_DETACH->__kmp_internal_end_library() below will try
636 // to take the forkjoin lock and will always fail, so that the application
637 // will never finish [normally]. This scenario is possible if
638 // __kmpc_end() has not been executed. It looks like it's not a corner
639 // case, but common cases:
640 // - the main function was compiled by an alternative compiler;
641 // - the main function was compiled by icl but without /Qopenmp
642 // (application with plugins);
643 // - application terminates by calling C exit(), Fortran CALL EXIT() or
644 // Fortran STOP.
645 // - alive foreign thread prevented __kmpc_end from doing cleanup.
646 //
647 // This is a hack to work around the problem.
648 // TODO: !!! figure out something better.
649 __kmp_reset_locks_on_process_detach(__kmp_gtid_get_specific());
650 }
651
652 __kmp_internal_end_library(__kmp_gtid_get_specific());
653
654 return TRUE;
655
656 case DLL_THREAD_ATTACH:
657 KA_TRACE(10, ("DllMain: THREAD_ATTACH\n"));
658
659 /* if we want to register new siblings all the time here call
660 * __kmp_get_gtid(); */
661 return TRUE;
662
663 case DLL_THREAD_DETACH:
664 KA_TRACE(10, ("DllMain: THREAD_DETACH T#%d\n", __kmp_gtid_get_specific()));
665
666 __kmp_internal_end_thread(__kmp_gtid_get_specific());
667 return TRUE;
668 }
669
670 return TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000671}
672
Jonathan Peyton30419822017-05-12 18:01:32 +0000673#endif /* KMP_OS_WINDOWS */
Jonathan Peyton99016992015-05-26 17:32:53 +0000674#endif /* KMP_DYNAMIC_LIB */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000675
Jim Cownie5e8470a2013-09-27 10:38:44 +0000676/* Change the library type to "status" and return the old type */
677/* called from within initialization routines where __kmp_initz_lock is held */
Jonathan Peyton30419822017-05-12 18:01:32 +0000678int __kmp_change_library(int status) {
679 int old_status;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000680
Jonathan Peyton30419822017-05-12 18:01:32 +0000681 old_status = __kmp_yield_init &
682 1; // check whether KMP_LIBRARY=throughput (even init count)
Jim Cownie5e8470a2013-09-27 10:38:44 +0000683
Jonathan Peyton30419822017-05-12 18:01:32 +0000684 if (status) {
685 __kmp_yield_init |= 1; // throughput => turnaround (odd init count)
686 } else {
687 __kmp_yield_init &= ~1; // turnaround => throughput (even init count)
688 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000689
Jonathan Peyton30419822017-05-12 18:01:32 +0000690 return old_status; // return previous setting of whether
691 // KMP_LIBRARY=throughput
Jim Cownie5e8470a2013-09-27 10:38:44 +0000692}
693
Jonathan Peyton30419822017-05-12 18:01:32 +0000694/* __kmp_parallel_deo -- Wait until it's our turn. */
695void __kmp_parallel_deo(int *gtid_ref, int *cid_ref, ident_t *loc_ref) {
696 int gtid = *gtid_ref;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000697#ifdef BUILD_PARALLEL_ORDERED
Jonathan Peyton30419822017-05-12 18:01:32 +0000698 kmp_team_t *team = __kmp_team_from_gtid(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000699#endif /* BUILD_PARALLEL_ORDERED */
700
Jonathan Peyton30419822017-05-12 18:01:32 +0000701 if (__kmp_env_consistency_check) {
702 if (__kmp_threads[gtid]->th.th_root->r.r_active)
Andrey Churbanov5c56fb52015-02-20 18:05:17 +0000703#if KMP_USE_DYNAMIC_LOCK
Jonathan Peyton30419822017-05-12 18:01:32 +0000704 __kmp_push_sync(gtid, ct_ordered_in_parallel, loc_ref, NULL, 0);
Andrey Churbanov5c56fb52015-02-20 18:05:17 +0000705#else
Jonathan Peyton30419822017-05-12 18:01:32 +0000706 __kmp_push_sync(gtid, ct_ordered_in_parallel, loc_ref, NULL);
Andrey Churbanov5c56fb52015-02-20 18:05:17 +0000707#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000708 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000709#ifdef BUILD_PARALLEL_ORDERED
Jonathan Peyton30419822017-05-12 18:01:32 +0000710 if (!team->t.t_serialized) {
711 KMP_MB();
712 KMP_WAIT_YIELD(&team->t.t_ordered.dt.t_value, __kmp_tid_from_gtid(gtid),
713 KMP_EQ, NULL);
714 KMP_MB();
715 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000716#endif /* BUILD_PARALLEL_ORDERED */
717}
718
Jonathan Peyton30419822017-05-12 18:01:32 +0000719/* __kmp_parallel_dxo -- Signal the next task. */
720void __kmp_parallel_dxo(int *gtid_ref, int *cid_ref, ident_t *loc_ref) {
721 int gtid = *gtid_ref;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000722#ifdef BUILD_PARALLEL_ORDERED
Jonathan Peyton30419822017-05-12 18:01:32 +0000723 int tid = __kmp_tid_from_gtid(gtid);
724 kmp_team_t *team = __kmp_team_from_gtid(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000725#endif /* BUILD_PARALLEL_ORDERED */
726
Jonathan Peyton30419822017-05-12 18:01:32 +0000727 if (__kmp_env_consistency_check) {
728 if (__kmp_threads[gtid]->th.th_root->r.r_active)
729 __kmp_pop_sync(gtid, ct_ordered_in_parallel, loc_ref);
730 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000731#ifdef BUILD_PARALLEL_ORDERED
Jonathan Peyton30419822017-05-12 18:01:32 +0000732 if (!team->t.t_serialized) {
733 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000734
Jonathan Peyton30419822017-05-12 18:01:32 +0000735 /* use the tid of the next thread in this team */
736 /* TODO replace with general release procedure */
737 team->t.t_ordered.dt.t_value = ((tid + 1) % team->t.t_nproc);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000738
Jonathan Peyton30419822017-05-12 18:01:32 +0000739 KMP_MB(); /* Flush all pending memory write invalidates. */
740 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000741#endif /* BUILD_PARALLEL_ORDERED */
742}
743
744/* ------------------------------------------------------------------------ */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000745/* The BARRIER for a SINGLE process section is always explicit */
746
Jonathan Peyton30419822017-05-12 18:01:32 +0000747int __kmp_enter_single(int gtid, ident_t *id_ref, int push_ws) {
748 int status;
749 kmp_info_t *th;
750 kmp_team_t *team;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000751
Jonathan Peyton30419822017-05-12 18:01:32 +0000752 if (!TCR_4(__kmp_init_parallel))
753 __kmp_parallel_initialize();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000754
Jonathan Peyton30419822017-05-12 18:01:32 +0000755 th = __kmp_threads[gtid];
756 team = th->th.th_team;
757 status = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000758
Jonathan Peyton30419822017-05-12 18:01:32 +0000759 th->th.th_ident = id_ref;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000760
Jonathan Peyton30419822017-05-12 18:01:32 +0000761 if (team->t.t_serialized) {
762 status = 1;
763 } else {
764 kmp_int32 old_this = th->th.th_local.this_construct;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000765
Jonathan Peyton30419822017-05-12 18:01:32 +0000766 ++th->th.th_local.this_construct;
767 /* try to set team count to thread count--success means thread got the
768 single block */
769 /* TODO: Should this be acquire or release? */
770 if (team->t.t_construct == old_this) {
Jonathan Peyton37e2ef52018-07-09 17:36:22 +0000771 status = __kmp_atomic_compare_store_acq(&team->t.t_construct, old_this,
772 th->th.th_local.this_construct);
Jonathan Peyton30419822017-05-12 18:01:32 +0000773 }
Andrey Churbanov51aecb82015-05-06 19:22:36 +0000774#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000775 if (__itt_metadata_add_ptr && __kmp_forkjoin_frames_mode == 3 &&
776 KMP_MASTER_GTID(gtid) &&
Andrey Churbanov51aecb82015-05-06 19:22:36 +0000777#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +0000778 th->th.th_teams_microtask == NULL &&
Andrey Churbanov51aecb82015-05-06 19:22:36 +0000779#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000780 team->t.t_active_level ==
781 1) { // Only report metadata by master of active team at level 1
782 __kmp_itt_metadata_single(id_ref);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000783 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000784#endif /* USE_ITT_BUILD */
785 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000786
Jonathan Peyton30419822017-05-12 18:01:32 +0000787 if (__kmp_env_consistency_check) {
788 if (status && push_ws) {
789 __kmp_push_workshare(gtid, ct_psingle, id_ref);
790 } else {
791 __kmp_check_workshare(gtid, ct_psingle, id_ref);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000792 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000793 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000794#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000795 if (status) {
796 __kmp_itt_single_start(gtid);
797 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000798#endif /* USE_ITT_BUILD */
Jonathan Peyton30419822017-05-12 18:01:32 +0000799 return status;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000800}
801
Jonathan Peyton30419822017-05-12 18:01:32 +0000802void __kmp_exit_single(int gtid) {
Jim Cownie5e8470a2013-09-27 10:38:44 +0000803#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000804 __kmp_itt_single_end(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000805#endif /* USE_ITT_BUILD */
Jonathan Peyton30419822017-05-12 18:01:32 +0000806 if (__kmp_env_consistency_check)
807 __kmp_pop_workshare(gtid, ct_psingle, NULL);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000808}
809
Jonathan Peyton30419822017-05-12 18:01:32 +0000810/* determine if we can go parallel or must use a serialized parallel region and
Jim Cownie5e8470a2013-09-27 10:38:44 +0000811 * how many threads we can use
812 * set_nproc is the number of threads requested for the team
813 * returns 0 if we should serialize or only use one thread,
814 * otherwise the number of threads to use
Jonathan Peyton30419822017-05-12 18:01:32 +0000815 * The forkjoin lock is held by the caller. */
816static int __kmp_reserve_threads(kmp_root_t *root, kmp_team_t *parent_team,
817 int master_tid, int set_nthreads
Jim Cownie5e8470a2013-09-27 10:38:44 +0000818#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +0000819 ,
820 int enter_teams
Jim Cownie5e8470a2013-09-27 10:38:44 +0000821#endif /* OMP_40_ENABLED */
Jonathan Peyton30419822017-05-12 18:01:32 +0000822 ) {
823 int capacity;
824 int new_nthreads;
825 KMP_DEBUG_ASSERT(__kmp_init_serial);
826 KMP_DEBUG_ASSERT(root && parent_team);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000827
Jonathan Peyton30419822017-05-12 18:01:32 +0000828 // If dyn-var is set, dynamically adjust the number of desired threads,
829 // according to the method specified by dynamic_mode.
830 new_nthreads = set_nthreads;
831 if (!get__dynamic_2(parent_team, master_tid)) {
832 ;
833 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000834#ifdef USE_LOAD_BALANCE
Jonathan Peyton30419822017-05-12 18:01:32 +0000835 else if (__kmp_global.g.g_dynamic_mode == dynamic_load_balance) {
836 new_nthreads = __kmp_load_balance_nproc(root, set_nthreads);
837 if (new_nthreads == 1) {
838 KC_TRACE(10, ("__kmp_reserve_threads: T#%d load balance reduced "
839 "reservation to 1 thread\n",
840 master_tid));
841 return 1;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000842 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000843 if (new_nthreads < set_nthreads) {
844 KC_TRACE(10, ("__kmp_reserve_threads: T#%d load balance reduced "
845 "reservation to %d threads\n",
846 master_tid, new_nthreads));
847 }
848 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000849#endif /* USE_LOAD_BALANCE */
Jonathan Peyton30419822017-05-12 18:01:32 +0000850 else if (__kmp_global.g.g_dynamic_mode == dynamic_thread_limit) {
851 new_nthreads = __kmp_avail_proc - __kmp_nth +
852 (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc);
853 if (new_nthreads <= 1) {
854 KC_TRACE(10, ("__kmp_reserve_threads: T#%d thread limit reduced "
855 "reservation to 1 thread\n",
856 master_tid));
857 return 1;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000858 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000859 if (new_nthreads < set_nthreads) {
860 KC_TRACE(10, ("__kmp_reserve_threads: T#%d thread limit reduced "
861 "reservation to %d threads\n",
862 master_tid, new_nthreads));
863 } else {
864 new_nthreads = set_nthreads;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000865 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000866 } else if (__kmp_global.g.g_dynamic_mode == dynamic_random) {
867 if (set_nthreads > 2) {
868 new_nthreads = __kmp_get_random(parent_team->t.t_threads[master_tid]);
869 new_nthreads = (new_nthreads % set_nthreads) + 1;
870 if (new_nthreads == 1) {
871 KC_TRACE(10, ("__kmp_reserve_threads: T#%d dynamic random reduced "
872 "reservation to 1 thread\n",
873 master_tid));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000874 return 1;
Jonathan Peyton30419822017-05-12 18:01:32 +0000875 }
876 if (new_nthreads < set_nthreads) {
877 KC_TRACE(10, ("__kmp_reserve_threads: T#%d dynamic random reduced "
878 "reservation to %d threads\n",
879 master_tid, new_nthreads));
880 }
881 }
882 } else {
883 KMP_ASSERT(0);
884 }
885
Jonathan Peytonf4392462017-07-27 20:58:41 +0000886 // Respect KMP_ALL_THREADS/KMP_DEVICE_THREAD_LIMIT.
Jonathan Peyton30419822017-05-12 18:01:32 +0000887 if (__kmp_nth + new_nthreads -
888 (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc) >
889 __kmp_max_nth) {
890 int tl_nthreads = __kmp_max_nth - __kmp_nth +
891 (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc);
892 if (tl_nthreads <= 0) {
893 tl_nthreads = 1;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000894 }
895
Jonathan Peyton30419822017-05-12 18:01:32 +0000896 // If dyn-var is false, emit a 1-time warning.
897 if (!get__dynamic_2(parent_team, master_tid) && (!__kmp_reserve_warn)) {
898 __kmp_reserve_warn = 1;
899 __kmp_msg(kmp_ms_warning,
900 KMP_MSG(CantFormThrTeam, set_nthreads, tl_nthreads),
901 KMP_HNT(Unset_ALL_THREADS), __kmp_msg_null);
902 }
903 if (tl_nthreads == 1) {
Jonathan Peytonf4392462017-07-27 20:58:41 +0000904 KC_TRACE(10, ("__kmp_reserve_threads: T#%d KMP_DEVICE_THREAD_LIMIT "
905 "reduced reservation to 1 thread\n",
Jonathan Peyton30419822017-05-12 18:01:32 +0000906 master_tid));
907 return 1;
908 }
Jonathan Peytonf4392462017-07-27 20:58:41 +0000909 KC_TRACE(10, ("__kmp_reserve_threads: T#%d KMP_DEVICE_THREAD_LIMIT reduced "
910 "reservation to %d threads\n",
911 master_tid, tl_nthreads));
912 new_nthreads = tl_nthreads;
913 }
914
915 // Respect OMP_THREAD_LIMIT
916 if (root->r.r_cg_nthreads + new_nthreads -
917 (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc) >
918 __kmp_cg_max_nth) {
919 int tl_nthreads = __kmp_cg_max_nth - root->r.r_cg_nthreads +
920 (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc);
921 if (tl_nthreads <= 0) {
922 tl_nthreads = 1;
923 }
924
925 // If dyn-var is false, emit a 1-time warning.
926 if (!get__dynamic_2(parent_team, master_tid) && (!__kmp_reserve_warn)) {
927 __kmp_reserve_warn = 1;
928 __kmp_msg(kmp_ms_warning,
929 KMP_MSG(CantFormThrTeam, set_nthreads, tl_nthreads),
930 KMP_HNT(Unset_ALL_THREADS), __kmp_msg_null);
931 }
932 if (tl_nthreads == 1) {
933 KC_TRACE(10, ("__kmp_reserve_threads: T#%d OMP_THREAD_LIMIT "
934 "reduced reservation to 1 thread\n",
935 master_tid));
936 return 1;
937 }
938 KC_TRACE(10, ("__kmp_reserve_threads: T#%d OMP_THREAD_LIMIT reduced "
Jonathan Peyton30419822017-05-12 18:01:32 +0000939 "reservation to %d threads\n",
940 master_tid, tl_nthreads));
941 new_nthreads = tl_nthreads;
942 }
943
944 // Check if the threads array is large enough, or needs expanding.
Jonathan Peyton30419822017-05-12 18:01:32 +0000945 // See comment in __kmp_register_root() about the adjustment if
946 // __kmp_threads[0] == NULL.
947 capacity = __kmp_threads_capacity;
948 if (TCR_PTR(__kmp_threads[0]) == NULL) {
949 --capacity;
950 }
951 if (__kmp_nth + new_nthreads -
952 (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc) >
953 capacity) {
954 // Expand the threads array.
955 int slotsRequired = __kmp_nth + new_nthreads -
956 (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc) -
957 capacity;
Jonathan Peyton1800ece2018-01-10 18:27:01 +0000958 int slotsAdded = __kmp_expand_threads(slotsRequired);
Jonathan Peyton30419822017-05-12 18:01:32 +0000959 if (slotsAdded < slotsRequired) {
960 // The threads array was not expanded enough.
961 new_nthreads -= (slotsRequired - slotsAdded);
962 KMP_ASSERT(new_nthreads >= 1);
963
964 // If dyn-var is false, emit a 1-time warning.
965 if (!get__dynamic_2(parent_team, master_tid) && (!__kmp_reserve_warn)) {
966 __kmp_reserve_warn = 1;
967 if (__kmp_tp_cached) {
968 __kmp_msg(kmp_ms_warning,
969 KMP_MSG(CantFormThrTeam, set_nthreads, new_nthreads),
970 KMP_HNT(Set_ALL_THREADPRIVATE, __kmp_tp_capacity),
971 KMP_HNT(PossibleSystemLimitOnThreads), __kmp_msg_null);
972 } else {
973 __kmp_msg(kmp_ms_warning,
974 KMP_MSG(CantFormThrTeam, set_nthreads, new_nthreads),
975 KMP_HNT(SystemLimitOnThreads), __kmp_msg_null);
976 }
977 }
978 }
979 }
980
Jonathan Peyton642688b2017-06-01 16:46:36 +0000981#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +0000982 if (new_nthreads == 1) {
983 KC_TRACE(10,
984 ("__kmp_reserve_threads: T#%d serializing team after reclaiming "
985 "dead roots and rechecking; requested %d threads\n",
986 __kmp_get_gtid(), set_nthreads));
Jonathan Peyton642688b2017-06-01 16:46:36 +0000987 } else {
988 KC_TRACE(10, ("__kmp_reserve_threads: T#%d allocating %d threads; requested"
989 " %d threads\n",
990 __kmp_get_gtid(), new_nthreads, set_nthreads));
Jonathan Peyton30419822017-05-12 18:01:32 +0000991 }
Jonathan Peyton642688b2017-06-01 16:46:36 +0000992#endif // KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +0000993 return new_nthreads;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000994}
995
Jonathan Peyton30419822017-05-12 18:01:32 +0000996/* Allocate threads from the thread pool and assign them to the new team. We are
997 assured that there are enough threads available, because we checked on that
998 earlier within critical section forkjoin */
999static void __kmp_fork_team_threads(kmp_root_t *root, kmp_team_t *team,
1000 kmp_info_t *master_th, int master_gtid) {
1001 int i;
1002 int use_hot_team;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001003
Jonathan Peyton30419822017-05-12 18:01:32 +00001004 KA_TRACE(10, ("__kmp_fork_team_threads: new_nprocs = %d\n", team->t.t_nproc));
1005 KMP_DEBUG_ASSERT(master_gtid == __kmp_get_gtid());
1006 KMP_MB();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001007
Jonathan Peyton30419822017-05-12 18:01:32 +00001008 /* first, let's setup the master thread */
1009 master_th->th.th_info.ds.ds_tid = 0;
1010 master_th->th.th_team = team;
1011 master_th->th.th_team_nproc = team->t.t_nproc;
1012 master_th->th.th_team_master = master_th;
1013 master_th->th.th_team_serialized = FALSE;
1014 master_th->th.th_dispatch = &team->t.t_dispatch[0];
Jim Cownie5e8470a2013-09-27 10:38:44 +00001015
Jonathan Peyton30419822017-05-12 18:01:32 +00001016/* make sure we are not the optimized hot team */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001017#if KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00001018 use_hot_team = 0;
1019 kmp_hot_team_ptr_t *hot_teams = master_th->th.th_hot_teams;
1020 if (hot_teams) { // hot teams array is not allocated if
1021 // KMP_HOT_TEAMS_MAX_LEVEL=0
1022 int level = team->t.t_active_level - 1; // index in array of hot teams
1023 if (master_th->th.th_teams_microtask) { // are we inside the teams?
1024 if (master_th->th.th_teams_size.nteams > 1) {
1025 ++level; // level was not increased in teams construct for
1026 // team_of_masters
1027 }
1028 if (team->t.t_pkfn != (microtask_t)__kmp_teams_master &&
1029 master_th->th.th_teams_level == team->t.t_level) {
1030 ++level; // level was not increased in teams construct for
1031 // team_of_workers before the parallel
1032 } // team->t.t_level will be increased inside parallel
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001033 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001034 if (level < __kmp_hot_teams_max_level) {
1035 if (hot_teams[level].hot_team) {
1036 // hot team has already been allocated for given level
1037 KMP_DEBUG_ASSERT(hot_teams[level].hot_team == team);
1038 use_hot_team = 1; // the team is ready to use
1039 } else {
1040 use_hot_team = 0; // AC: threads are not allocated yet
1041 hot_teams[level].hot_team = team; // remember new hot team
1042 hot_teams[level].hot_team_nth = team->t.t_nproc;
1043 }
1044 } else {
1045 use_hot_team = 0;
1046 }
1047 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001048#else
Jonathan Peyton30419822017-05-12 18:01:32 +00001049 use_hot_team = team == root->r.r_hot_team;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001050#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001051 if (!use_hot_team) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001052
Jonathan Peyton30419822017-05-12 18:01:32 +00001053 /* install the master thread */
1054 team->t.t_threads[0] = master_th;
1055 __kmp_initialize_info(master_th, team, 0, master_gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001056
Jonathan Peyton30419822017-05-12 18:01:32 +00001057 /* now, install the worker threads */
1058 for (i = 1; i < team->t.t_nproc; i++) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001059
Jonathan Peyton30419822017-05-12 18:01:32 +00001060 /* fork or reallocate a new thread and install it in team */
1061 kmp_info_t *thr = __kmp_allocate_thread(root, team, i);
1062 team->t.t_threads[i] = thr;
1063 KMP_DEBUG_ASSERT(thr);
1064 KMP_DEBUG_ASSERT(thr->th.th_team == team);
1065 /* align team and thread arrived states */
1066 KA_TRACE(20, ("__kmp_fork_team_threads: T#%d(%d:%d) init arrived "
1067 "T#%d(%d:%d) join =%llu, plain=%llu\n",
1068 __kmp_gtid_from_tid(0, team), team->t.t_id, 0,
1069 __kmp_gtid_from_tid(i, team), team->t.t_id, i,
1070 team->t.t_bar[bs_forkjoin_barrier].b_arrived,
1071 team->t.t_bar[bs_plain_barrier].b_arrived));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001072#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00001073 thr->th.th_teams_microtask = master_th->th.th_teams_microtask;
1074 thr->th.th_teams_level = master_th->th.th_teams_level;
1075 thr->th.th_teams_size = master_th->th.th_teams_size;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001076#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001077 { // Initialize threads' barrier data.
1078 int b;
1079 kmp_balign_t *balign = team->t.t_threads[i]->th.th_bar;
1080 for (b = 0; b < bs_last_barrier; ++b) {
1081 balign[b].bb.b_arrived = team->t.t_bar[b].b_arrived;
1082 KMP_DEBUG_ASSERT(balign[b].bb.wait_flag != KMP_BARRIER_PARENT_FLAG);
Jonathan Peyton8fbb49a2015-07-09 18:16:58 +00001083#if USE_DEBUGGER
Jonathan Peyton30419822017-05-12 18:01:32 +00001084 balign[b].bb.b_worker_arrived = team->t.t_bar[b].b_team_arrived;
Jonathan Peyton8fbb49a2015-07-09 18:16:58 +00001085#endif
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001086 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001087 }
1088 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001089
Alp Toker98758b02014-03-02 04:12:06 +00001090#if OMP_40_ENABLED && KMP_AFFINITY_SUPPORTED
Jonathan Peyton30419822017-05-12 18:01:32 +00001091 __kmp_partition_places(team);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001092#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001093 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001094
Jonathan Peyton30419822017-05-12 18:01:32 +00001095 KMP_MB();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001096}
1097
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001098#if KMP_ARCH_X86 || KMP_ARCH_X86_64
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001099// Propagate any changes to the floating point control registers out to the team
Jonathan Peyton30419822017-05-12 18:01:32 +00001100// We try to avoid unnecessary writes to the relevant cache line in the team
1101// structure, so we don't make changes unless they are needed.
1102inline static void propagateFPControl(kmp_team_t *team) {
1103 if (__kmp_inherit_fp_control) {
1104 kmp_int16 x87_fpu_control_word;
1105 kmp_uint32 mxcsr;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001106
Jonathan Peyton30419822017-05-12 18:01:32 +00001107 // Get master values of FPU control flags (both X87 and vector)
1108 __kmp_store_x87_fpu_control_word(&x87_fpu_control_word);
1109 __kmp_store_mxcsr(&mxcsr);
1110 mxcsr &= KMP_X86_MXCSR_MASK;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001111
Jonathan Peyton94a114f2017-10-20 19:30:57 +00001112 // There is no point looking at t_fp_control_saved here.
1113 // If it is TRUE, we still have to update the values if they are different
Jonas Hahnfeldf0a1c652017-11-03 18:28:19 +00001114 // from those we now have. If it is FALSE we didn't save anything yet, but
1115 // our objective is the same. We have to ensure that the values in the team
1116 // are the same as those we have.
Jonathan Peyton94a114f2017-10-20 19:30:57 +00001117 // So, this code achieves what we need whether or not t_fp_control_saved is
1118 // true. By checking whether the value needs updating we avoid unnecessary
1119 // writes that would put the cache-line into a written state, causing all
1120 // threads in the team to have to read it again.
Jonathan Peyton30419822017-05-12 18:01:32 +00001121 KMP_CHECK_UPDATE(team->t.t_x87_fpu_control_word, x87_fpu_control_word);
1122 KMP_CHECK_UPDATE(team->t.t_mxcsr, mxcsr);
1123 // Although we don't use this value, other code in the runtime wants to know
1124 // whether it should restore them. So we must ensure it is correct.
1125 KMP_CHECK_UPDATE(team->t.t_fp_control_saved, TRUE);
1126 } else {
1127 // Similarly here. Don't write to this cache-line in the team structure
1128 // unless we have to.
1129 KMP_CHECK_UPDATE(team->t.t_fp_control_saved, FALSE);
1130 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001131}
1132
Jonathan Peyton30419822017-05-12 18:01:32 +00001133// Do the opposite, setting the hardware registers to the updated values from
1134// the team.
1135inline static void updateHWFPControl(kmp_team_t *team) {
1136 if (__kmp_inherit_fp_control && team->t.t_fp_control_saved) {
1137 // Only reset the fp control regs if they have been changed in the team.
1138 // the parallel region that we are exiting.
1139 kmp_int16 x87_fpu_control_word;
1140 kmp_uint32 mxcsr;
1141 __kmp_store_x87_fpu_control_word(&x87_fpu_control_word);
1142 __kmp_store_mxcsr(&mxcsr);
1143 mxcsr &= KMP_X86_MXCSR_MASK;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001144
Jonathan Peyton30419822017-05-12 18:01:32 +00001145 if (team->t.t_x87_fpu_control_word != x87_fpu_control_word) {
1146 __kmp_clear_x87_fpu_status_word();
1147 __kmp_load_x87_fpu_control_word(&team->t.t_x87_fpu_control_word);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001148 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001149
1150 if (team->t.t_mxcsr != mxcsr) {
1151 __kmp_load_mxcsr(&team->t.t_mxcsr);
1152 }
1153 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001154}
1155#else
Jonathan Peyton30419822017-05-12 18:01:32 +00001156#define propagateFPControl(x) ((void)0)
1157#define updateHWFPControl(x) ((void)0)
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001158#endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
1159
Jonathan Peyton30419822017-05-12 18:01:32 +00001160static void __kmp_alloc_argv_entries(int argc, kmp_team_t *team,
1161 int realloc); // forward declaration
Jim Cownie5e8470a2013-09-27 10:38:44 +00001162
Jonathan Peyton30419822017-05-12 18:01:32 +00001163/* Run a parallel region that has been serialized, so runs only in a team of the
1164 single master thread. */
1165void __kmp_serialized_parallel(ident_t *loc, kmp_int32 global_tid) {
1166 kmp_info_t *this_thr;
1167 kmp_team_t *serial_team;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001168
Jonathan Peyton30419822017-05-12 18:01:32 +00001169 KC_TRACE(10, ("__kmpc_serialized_parallel: called by T#%d\n", global_tid));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001170
Jonathan Peyton30419822017-05-12 18:01:32 +00001171 /* Skip all this code for autopar serialized loops since it results in
1172 unacceptable overhead */
1173 if (loc != NULL && (loc->flags & KMP_IDENT_AUTOPAR))
1174 return;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001175
Jonathan Peyton30419822017-05-12 18:01:32 +00001176 if (!TCR_4(__kmp_init_parallel))
1177 __kmp_parallel_initialize();
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001178
Jonathan Peyton30419822017-05-12 18:01:32 +00001179 this_thr = __kmp_threads[global_tid];
1180 serial_team = this_thr->th.th_serial_team;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001181
Jonathan Peyton30419822017-05-12 18:01:32 +00001182 /* utilize the serialized team held by this thread */
1183 KMP_DEBUG_ASSERT(serial_team);
1184 KMP_MB();
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001185
Jonathan Peyton30419822017-05-12 18:01:32 +00001186 if (__kmp_tasking_mode != tskm_immediate_exec) {
1187 KMP_DEBUG_ASSERT(
1188 this_thr->th.th_task_team ==
1189 this_thr->th.th_team->t.t_task_team[this_thr->th.th_task_state]);
1190 KMP_DEBUG_ASSERT(serial_team->t.t_task_team[this_thr->th.th_task_state] ==
1191 NULL);
1192 KA_TRACE(20, ("__kmpc_serialized_parallel: T#%d pushing task_team %p / "
1193 "team %p, new task_team = NULL\n",
1194 global_tid, this_thr->th.th_task_team, this_thr->th.th_team));
1195 this_thr->th.th_task_team = NULL;
1196 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001197
1198#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00001199 kmp_proc_bind_t proc_bind = this_thr->th.th_set_proc_bind;
1200 if (this_thr->th.th_current_task->td_icvs.proc_bind == proc_bind_false) {
1201 proc_bind = proc_bind_false;
1202 } else if (proc_bind == proc_bind_default) {
1203 // No proc_bind clause was specified, so use the current value
1204 // of proc-bind-var for this parallel region.
1205 proc_bind = this_thr->th.th_current_task->td_icvs.proc_bind;
1206 }
1207 // Reset for next parallel region
1208 this_thr->th.th_set_proc_bind = proc_bind_default;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001209#endif /* OMP_40_ENABLED */
1210
Joachim Protze82e94a52017-11-01 10:08:30 +00001211#if OMPT_SUPPORT
1212 ompt_data_t ompt_parallel_data;
1213 ompt_parallel_data.ptr = NULL;
1214 ompt_data_t *implicit_task_data;
1215 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(global_tid);
1216 if (ompt_enabled.enabled &&
1217 this_thr->th.ompt_thread_info.state != omp_state_overhead) {
1218
1219 ompt_task_info_t *parent_task_info;
1220 parent_task_info = OMPT_CUR_TASK_INFO(this_thr);
1221
Joachim Protzec255ca72017-11-05 14:11:10 +00001222 parent_task_info->frame.enter_frame = OMPT_GET_FRAME_ADDRESS(1);
Joachim Protze82e94a52017-11-01 10:08:30 +00001223 if (ompt_enabled.ompt_callback_parallel_begin) {
1224 int team_size = 1;
1225
1226 ompt_callbacks.ompt_callback(ompt_callback_parallel_begin)(
1227 &(parent_task_info->task_data), &(parent_task_info->frame),
Joachim Protze489cdb72018-09-10 14:34:54 +00001228 &ompt_parallel_data, team_size, ompt_parallel_invoker_program,
1229 codeptr);
Joachim Protze82e94a52017-11-01 10:08:30 +00001230 }
1231 }
1232#endif // OMPT_SUPPORT
1233
Jonathan Peyton30419822017-05-12 18:01:32 +00001234 if (this_thr->th.th_team != serial_team) {
1235 // Nested level will be an index in the nested nthreads array
1236 int level = this_thr->th.th_team->t.t_level;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001237
Jonathan Peyton30419822017-05-12 18:01:32 +00001238 if (serial_team->t.t_serialized) {
1239 /* this serial team was already used
1240 TODO increase performance by making this locks more specific */
1241 kmp_team_t *new_team;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001242
Jonathan Peyton30419822017-05-12 18:01:32 +00001243 __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001244
Jonathan Peyton30419822017-05-12 18:01:32 +00001245 new_team = __kmp_allocate_team(this_thr->th.th_root, 1, 1,
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001246#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00001247 ompt_parallel_data,
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001248#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001249#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00001250 proc_bind,
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001251#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001252 &this_thr->th.th_current_task->td_icvs,
1253 0 USE_NESTED_HOT_ARG(NULL));
1254 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
1255 KMP_ASSERT(new_team);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001256
Jonathan Peyton30419822017-05-12 18:01:32 +00001257 /* setup new serialized team and install it */
1258 new_team->t.t_threads[0] = this_thr;
1259 new_team->t.t_parent = this_thr->th.th_team;
1260 serial_team = new_team;
1261 this_thr->th.th_serial_team = serial_team;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001262
Jonathan Peyton30419822017-05-12 18:01:32 +00001263 KF_TRACE(
1264 10,
1265 ("__kmpc_serialized_parallel: T#%d allocated new serial team %p\n",
1266 global_tid, serial_team));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001267
Jonathan Peyton30419822017-05-12 18:01:32 +00001268 /* TODO the above breaks the requirement that if we run out of resources,
1269 then we can still guarantee that serialized teams are ok, since we may
1270 need to allocate a new one */
1271 } else {
1272 KF_TRACE(
1273 10,
1274 ("__kmpc_serialized_parallel: T#%d reusing cached serial team %p\n",
1275 global_tid, serial_team));
1276 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001277
Jonathan Peyton30419822017-05-12 18:01:32 +00001278 /* we have to initialize this serial team */
1279 KMP_DEBUG_ASSERT(serial_team->t.t_threads);
1280 KMP_DEBUG_ASSERT(serial_team->t.t_threads[0] == this_thr);
1281 KMP_DEBUG_ASSERT(this_thr->th.th_team != serial_team);
1282 serial_team->t.t_ident = loc;
1283 serial_team->t.t_serialized = 1;
1284 serial_team->t.t_nproc = 1;
1285 serial_team->t.t_parent = this_thr->th.th_team;
Jonathan Peytonba55a7b2017-11-29 22:47:52 +00001286 serial_team->t.t_sched.sched = this_thr->th.th_team->t.t_sched.sched;
Jonathan Peyton30419822017-05-12 18:01:32 +00001287 this_thr->th.th_team = serial_team;
1288 serial_team->t.t_master_tid = this_thr->th.th_info.ds.ds_tid;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001289
Jonathan Peyton30419822017-05-12 18:01:32 +00001290 KF_TRACE(10, ("__kmpc_serialized_parallel: T#d curtask=%p\n", global_tid,
1291 this_thr->th.th_current_task));
1292 KMP_ASSERT(this_thr->th.th_current_task->td_flags.executing == 1);
1293 this_thr->th.th_current_task->td_flags.executing = 0;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001294
Jonathan Peyton30419822017-05-12 18:01:32 +00001295 __kmp_push_current_task_to_thread(this_thr, serial_team, 0);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001296
Jonathan Peyton30419822017-05-12 18:01:32 +00001297 /* TODO: GEH: do ICVs work for nested serialized teams? Don't we need an
1298 implicit task for each serialized task represented by
1299 team->t.t_serialized? */
1300 copy_icvs(&this_thr->th.th_current_task->td_icvs,
1301 &this_thr->th.th_current_task->td_parent->td_icvs);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001302
Jonathan Peyton30419822017-05-12 18:01:32 +00001303 // Thread value exists in the nested nthreads array for the next nested
1304 // level
1305 if (__kmp_nested_nth.used && (level + 1 < __kmp_nested_nth.used)) {
1306 this_thr->th.th_current_task->td_icvs.nproc =
1307 __kmp_nested_nth.nth[level + 1];
1308 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001309
1310#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00001311 if (__kmp_nested_proc_bind.used &&
1312 (level + 1 < __kmp_nested_proc_bind.used)) {
1313 this_thr->th.th_current_task->td_icvs.proc_bind =
1314 __kmp_nested_proc_bind.bind_types[level + 1];
1315 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001316#endif /* OMP_40_ENABLED */
1317
Jonathan Peyton8fbb49a2015-07-09 18:16:58 +00001318#if USE_DEBUGGER
Jonathan Peyton30419822017-05-12 18:01:32 +00001319 serial_team->t.t_pkfn = (microtask_t)(~0); // For the debugger.
Jonathan Peyton8fbb49a2015-07-09 18:16:58 +00001320#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001321 this_thr->th.th_info.ds.ds_tid = 0;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001322
Jonathan Peyton30419822017-05-12 18:01:32 +00001323 /* set thread cache values */
1324 this_thr->th.th_team_nproc = 1;
1325 this_thr->th.th_team_master = this_thr;
1326 this_thr->th.th_team_serialized = 1;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001327
Jonathan Peyton30419822017-05-12 18:01:32 +00001328 serial_team->t.t_level = serial_team->t.t_parent->t.t_level + 1;
1329 serial_team->t.t_active_level = serial_team->t.t_parent->t.t_active_level;
Jonathan Peyton92ca6182018-09-07 18:25:49 +00001330#if OMP_50_ENABLED
1331 serial_team->t.t_def_allocator = this_thr->th.th_def_allocator; // save
1332#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001333
Jonathan Peyton30419822017-05-12 18:01:32 +00001334 propagateFPControl(serial_team);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001335
Jonathan Peyton30419822017-05-12 18:01:32 +00001336 /* check if we need to allocate dispatch buffers stack */
1337 KMP_DEBUG_ASSERT(serial_team->t.t_dispatch);
1338 if (!serial_team->t.t_dispatch->th_disp_buffer) {
1339 serial_team->t.t_dispatch->th_disp_buffer =
1340 (dispatch_private_info_t *)__kmp_allocate(
1341 sizeof(dispatch_private_info_t));
1342 }
1343 this_thr->th.th_dispatch = serial_team->t.t_dispatch;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001344
Jonathan Peyton30419822017-05-12 18:01:32 +00001345 KMP_MB();
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001346
Jonathan Peyton30419822017-05-12 18:01:32 +00001347 } else {
1348 /* this serialized team is already being used,
1349 * that's fine, just add another nested level */
1350 KMP_DEBUG_ASSERT(this_thr->th.th_team == serial_team);
1351 KMP_DEBUG_ASSERT(serial_team->t.t_threads);
1352 KMP_DEBUG_ASSERT(serial_team->t.t_threads[0] == this_thr);
1353 ++serial_team->t.t_serialized;
1354 this_thr->th.th_team_serialized = serial_team->t.t_serialized;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001355
Jonathan Peyton30419822017-05-12 18:01:32 +00001356 // Nested level will be an index in the nested nthreads array
1357 int level = this_thr->th.th_team->t.t_level;
1358 // Thread value exists in the nested nthreads array for the next nested
1359 // level
1360 if (__kmp_nested_nth.used && (level + 1 < __kmp_nested_nth.used)) {
1361 this_thr->th.th_current_task->td_icvs.nproc =
1362 __kmp_nested_nth.nth[level + 1];
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001363 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001364 serial_team->t.t_level++;
1365 KF_TRACE(10, ("__kmpc_serialized_parallel: T#%d increasing nesting level "
1366 "of serial team %p to %d\n",
1367 global_tid, serial_team, serial_team->t.t_level));
1368
1369 /* allocate/push dispatch buffers stack */
1370 KMP_DEBUG_ASSERT(serial_team->t.t_dispatch);
1371 {
1372 dispatch_private_info_t *disp_buffer =
1373 (dispatch_private_info_t *)__kmp_allocate(
1374 sizeof(dispatch_private_info_t));
1375 disp_buffer->next = serial_team->t.t_dispatch->th_disp_buffer;
1376 serial_team->t.t_dispatch->th_disp_buffer = disp_buffer;
1377 }
1378 this_thr->th.th_dispatch = serial_team->t.t_dispatch;
1379
1380 KMP_MB();
1381 }
Olga Malyshevadbdcfa12017-04-04 13:56:50 +00001382#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00001383 KMP_CHECK_UPDATE(serial_team->t.t_cancel_request, cancel_noreq);
Olga Malyshevadbdcfa12017-04-04 13:56:50 +00001384#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001385
Jonathan Peyton30419822017-05-12 18:01:32 +00001386 if (__kmp_env_consistency_check)
1387 __kmp_push_parallel(global_tid, NULL);
Joachim Protze82e94a52017-11-01 10:08:30 +00001388#if OMPT_SUPPORT
1389 serial_team->t.ompt_team_info.master_return_address = codeptr;
1390 if (ompt_enabled.enabled &&
1391 this_thr->th.ompt_thread_info.state != omp_state_overhead) {
Joachim Protzec255ca72017-11-05 14:11:10 +00001392 OMPT_CUR_TASK_INFO(this_thr)->frame.exit_frame = OMPT_GET_FRAME_ADDRESS(1);
Joachim Protze82e94a52017-11-01 10:08:30 +00001393
1394 ompt_lw_taskteam_t lw_taskteam;
1395 __ompt_lw_taskteam_init(&lw_taskteam, this_thr, global_tid,
1396 &ompt_parallel_data, codeptr);
1397
1398 __ompt_lw_taskteam_link(&lw_taskteam, this_thr, 1);
1399 // don't use lw_taskteam after linking. content was swaped
1400
1401 /* OMPT implicit task begin */
1402 implicit_task_data = OMPT_CUR_TASK_DATA(this_thr);
1403 if (ompt_enabled.ompt_callback_implicit_task) {
1404 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
1405 ompt_scope_begin, OMPT_CUR_TEAM_DATA(this_thr),
1406 OMPT_CUR_TASK_DATA(this_thr), 1, __kmp_tid_from_gtid(global_tid));
Joachim Protze9be9cf22018-05-07 12:42:21 +00001407 OMPT_CUR_TASK_INFO(this_thr)
1408 ->thread_num = __kmp_tid_from_gtid(global_tid);
Joachim Protze82e94a52017-11-01 10:08:30 +00001409 }
1410
1411 /* OMPT state */
1412 this_thr->th.ompt_thread_info.state = omp_state_work_parallel;
Joachim Protzec255ca72017-11-05 14:11:10 +00001413 OMPT_CUR_TASK_INFO(this_thr)->frame.exit_frame = OMPT_GET_FRAME_ADDRESS(1);
Joachim Protze82e94a52017-11-01 10:08:30 +00001414 }
1415#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001416}
Jim Cownie181b4bb2013-12-23 17:28:57 +00001417
Jim Cownie5e8470a2013-09-27 10:38:44 +00001418/* most of the work for a fork */
1419/* return true if we really went parallel, false if serialized */
Jonathan Peyton30419822017-05-12 18:01:32 +00001420int __kmp_fork_call(ident_t *loc, int gtid,
1421 enum fork_context_e call_context, // Intel, GNU, ...
Joachim Protze82e94a52017-11-01 10:08:30 +00001422 kmp_int32 argc, microtask_t microtask, launch_t invoker,
Jim Cownie5e8470a2013-09-27 10:38:44 +00001423/* TODO: revert workaround for Intel(R) 64 tracker #96 */
Andrey Churbanovcbda8682015-01-13 14:43:35 +00001424#if (KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64) && KMP_OS_LINUX
Jonathan Peyton30419822017-05-12 18:01:32 +00001425 va_list *ap
Jim Cownie5e8470a2013-09-27 10:38:44 +00001426#else
Jonathan Peyton30419822017-05-12 18:01:32 +00001427 va_list ap
Jim Cownie5e8470a2013-09-27 10:38:44 +00001428#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001429 ) {
1430 void **argv;
1431 int i;
1432 int master_tid;
1433 int master_this_cons;
1434 kmp_team_t *team;
1435 kmp_team_t *parent_team;
1436 kmp_info_t *master_th;
1437 kmp_root_t *root;
1438 int nthreads;
1439 int master_active;
1440 int master_set_numthreads;
1441 int level;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001442#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00001443 int active_level;
1444 int teams_level;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001445#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001446#if KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00001447 kmp_hot_team_ptr_t **p_hot_teams;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001448#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001449 { // KMP_TIME_BLOCK
Jonathan Peyton5375fe82016-11-14 21:13:44 +00001450 KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(KMP_fork_call);
Jonathan Peyton45be4502015-08-11 21:36:41 +00001451 KMP_COUNT_VALUE(OMP_PARALLEL_args, argc);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001452
Jonathan Peyton30419822017-05-12 18:01:32 +00001453 KA_TRACE(20, ("__kmp_fork_call: enter T#%d\n", gtid));
1454 if (__kmp_stkpadding > 0 && __kmp_root[gtid] != NULL) {
1455 /* Some systems prefer the stack for the root thread(s) to start with */
1456 /* some gap from the parent stack to prevent false sharing. */
1457 void *dummy = KMP_ALLOCA(__kmp_stkpadding);
1458 /* These 2 lines below are so this does not get optimized out */
1459 if (__kmp_stkpadding > KMP_MAX_STKPADDING)
1460 __kmp_stkpadding += (short)((kmp_int64)dummy);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001461 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001462
1463 /* initialize if needed */
Jonathan Peyton30419822017-05-12 18:01:32 +00001464 KMP_DEBUG_ASSERT(
1465 __kmp_init_serial); // AC: potentially unsafe, not in sync with shutdown
1466 if (!TCR_4(__kmp_init_parallel))
1467 __kmp_parallel_initialize();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001468
1469 /* setup current data */
Jonathan Peyton30419822017-05-12 18:01:32 +00001470 master_th = __kmp_threads[gtid]; // AC: potentially unsafe, not in sync with
1471 // shutdown
1472 parent_team = master_th->th.th_team;
1473 master_tid = master_th->th.th_info.ds.ds_tid;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001474 master_this_cons = master_th->th.th_local.this_construct;
Jonathan Peyton30419822017-05-12 18:01:32 +00001475 root = master_th->th.th_root;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001476 master_active = root->r.r_active;
1477 master_set_numthreads = master_th->th.th_set_nproc;
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001478
1479#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00001480 ompt_data_t ompt_parallel_data;
1481 ompt_parallel_data.ptr = NULL;
1482 ompt_data_t *parent_task_data;
Joachim Protzec5836064b2018-05-28 08:14:58 +00001483 omp_frame_t *ompt_frame;
Joachim Protze82e94a52017-11-01 10:08:30 +00001484 ompt_data_t *implicit_task_data;
1485 void *return_address = NULL;
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001486
Joachim Protze82e94a52017-11-01 10:08:30 +00001487 if (ompt_enabled.enabled) {
1488 __ompt_get_task_info_internal(0, NULL, &parent_task_data, &ompt_frame,
1489 NULL, NULL);
1490 return_address = OMPT_LOAD_RETURN_ADDRESS(gtid);
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001491 }
1492#endif
1493
Jim Cownie5e8470a2013-09-27 10:38:44 +00001494 // Nested level will be an index in the nested nthreads array
Jonathan Peyton30419822017-05-12 18:01:32 +00001495 level = parent_team->t.t_level;
1496 // used to launch non-serial teams even if nested is not allowed
1497 active_level = parent_team->t.t_active_level;
Jonathan Peytonc76f9f02016-06-21 19:12:07 +00001498#if OMP_40_ENABLED
Jonathan Peyton642688b2017-06-01 16:46:36 +00001499 // needed to check nesting inside the teams
1500 teams_level = master_th->th.th_teams_level;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001501#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001502#if KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00001503 p_hot_teams = &master_th->th.th_hot_teams;
1504 if (*p_hot_teams == NULL && __kmp_hot_teams_max_level > 0) {
1505 *p_hot_teams = (kmp_hot_team_ptr_t *)__kmp_allocate(
1506 sizeof(kmp_hot_team_ptr_t) * __kmp_hot_teams_max_level);
1507 (*p_hot_teams)[0].hot_team = root->r.r_hot_team;
Jonathan Peyton642688b2017-06-01 16:46:36 +00001508 // it is either actual or not needed (when active_level > 0)
1509 (*p_hot_teams)[0].hot_team_nth = 1;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001510 }
1511#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001512
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001513#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00001514 if (ompt_enabled.enabled) {
1515 if (ompt_enabled.ompt_callback_parallel_begin) {
1516 int team_size = master_set_numthreads
1517 ? master_set_numthreads
1518 : get__nproc_2(parent_team, master_tid);
1519 ompt_callbacks.ompt_callback(ompt_callback_parallel_begin)(
1520 parent_task_data, ompt_frame, &ompt_parallel_data, team_size,
1521 OMPT_INVOKER(call_context), return_address);
1522 }
1523 master_th->th.ompt_thread_info.state = omp_state_overhead;
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001524 }
1525#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001526
Jim Cownie5e8470a2013-09-27 10:38:44 +00001527 master_th->th.th_ident = loc;
1528
1529#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00001530 if (master_th->th.th_teams_microtask && ap &&
1531 microtask != (microtask_t)__kmp_teams_master && level == teams_level) {
1532 // AC: This is start of parallel that is nested inside teams construct.
1533 // The team is actual (hot), all workers are ready at the fork barrier.
1534 // No lock needed to initialize the team a bit, then free workers.
1535 parent_team->t.t_ident = loc;
1536 __kmp_alloc_argv_entries(argc, parent_team, TRUE);
1537 parent_team->t.t_argc = argc;
1538 argv = (void **)parent_team->t.t_argv;
1539 for (i = argc - 1; i >= 0; --i)
Jim Cownie5e8470a2013-09-27 10:38:44 +00001540/* TODO: revert workaround for Intel(R) 64 tracker #96 */
Andrey Churbanovcbda8682015-01-13 14:43:35 +00001541#if (KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64) && KMP_OS_LINUX
Jonathan Peyton30419822017-05-12 18:01:32 +00001542 *argv++ = va_arg(*ap, void *);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001543#else
Jonathan Peyton30419822017-05-12 18:01:32 +00001544 *argv++ = va_arg(ap, void *);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001545#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001546 // Increment our nested depth levels, but not increase the serialization
1547 if (parent_team == master_th->th.th_serial_team) {
1548 // AC: we are in serialized parallel
1549 __kmpc_serialized_parallel(loc, gtid);
1550 KMP_DEBUG_ASSERT(parent_team->t.t_serialized > 1);
1551 // AC: need this in order enquiry functions work
1552 // correctly, will restore at join time
1553 parent_team->t.t_serialized--;
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001554#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00001555 void *dummy;
1556 void **exit_runtime_p;
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001557
Jonathan Peyton30419822017-05-12 18:01:32 +00001558 ompt_lw_taskteam_t lw_taskteam;
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001559
Joachim Protze82e94a52017-11-01 10:08:30 +00001560 if (ompt_enabled.enabled) {
1561 __ompt_lw_taskteam_init(&lw_taskteam, master_th, gtid,
1562 &ompt_parallel_data, return_address);
Joachim Protzec255ca72017-11-05 14:11:10 +00001563 exit_runtime_p = &(lw_taskteam.ompt_task_info.frame.exit_frame);
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001564
Joachim Protze82e94a52017-11-01 10:08:30 +00001565 __ompt_lw_taskteam_link(&lw_taskteam, master_th, 0);
1566 // don't use lw_taskteam after linking. content was swaped
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001567
Jonathan Peyton30419822017-05-12 18:01:32 +00001568 /* OMPT implicit task begin */
Joachim Protze82e94a52017-11-01 10:08:30 +00001569 implicit_task_data = OMPT_CUR_TASK_DATA(master_th);
1570 if (ompt_enabled.ompt_callback_implicit_task) {
1571 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
1572 ompt_scope_begin, OMPT_CUR_TEAM_DATA(master_th),
1573 implicit_task_data, 1, __kmp_tid_from_gtid(gtid));
Joachim Protze9be9cf22018-05-07 12:42:21 +00001574 OMPT_CUR_TASK_INFO(master_th)
1575 ->thread_num = __kmp_tid_from_gtid(gtid);
Jonathan Peyton30419822017-05-12 18:01:32 +00001576 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001577
Jonathan Peyton30419822017-05-12 18:01:32 +00001578 /* OMPT state */
Joachim Protze82e94a52017-11-01 10:08:30 +00001579 master_th->th.ompt_thread_info.state = omp_state_work_parallel;
Jonathan Peyton30419822017-05-12 18:01:32 +00001580 } else {
1581 exit_runtime_p = &dummy;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001582 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001583#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001584
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001585 {
Jonathan Peyton30419822017-05-12 18:01:32 +00001586 KMP_TIME_PARTITIONED_BLOCK(OMP_parallel);
1587 KMP_SET_THREAD_STATE_BLOCK(IMPLICIT_TASK);
1588 __kmp_invoke_microtask(microtask, gtid, 0, argc, parent_team->t.t_argv
1589#if OMPT_SUPPORT
1590 ,
1591 exit_runtime_p
1592#endif
1593 );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001594 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001595
Jonathan Peyton30419822017-05-12 18:01:32 +00001596#if OMPT_SUPPORT
1597 *exit_runtime_p = NULL;
Joachim Protze82e94a52017-11-01 10:08:30 +00001598 if (ompt_enabled.enabled) {
Joachim Protzec255ca72017-11-05 14:11:10 +00001599 OMPT_CUR_TASK_INFO(master_th)->frame.exit_frame = NULL;
Joachim Protze82e94a52017-11-01 10:08:30 +00001600 if (ompt_enabled.ompt_callback_implicit_task) {
1601 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
1602 ompt_scope_end, NULL, implicit_task_data, 1,
Joachim Protze9be9cf22018-05-07 12:42:21 +00001603 OMPT_CUR_TASK_INFO(master_th)->thread_num);
Jonathan Peyton30419822017-05-12 18:01:32 +00001604 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001605 __ompt_lw_taskteam_unlink(master_th);
Jonathan Peyton30419822017-05-12 18:01:32 +00001606
Joachim Protze82e94a52017-11-01 10:08:30 +00001607 if (ompt_enabled.ompt_callback_parallel_end) {
1608 ompt_callbacks.ompt_callback(ompt_callback_parallel_end)(
1609 OMPT_CUR_TEAM_DATA(master_th), OMPT_CUR_TASK_DATA(master_th),
1610 OMPT_INVOKER(call_context), return_address);
Jonathan Peyton30419822017-05-12 18:01:32 +00001611 }
Joachim Protze82e94a52017-11-01 10:08:30 +00001612 master_th->th.ompt_thread_info.state = omp_state_overhead;
Jonathan Peyton30419822017-05-12 18:01:32 +00001613 }
1614#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001615 return TRUE;
Jonathan Peyton30419822017-05-12 18:01:32 +00001616 }
1617
1618 parent_team->t.t_pkfn = microtask;
Jonathan Peyton30419822017-05-12 18:01:32 +00001619 parent_team->t.t_invoke = invoker;
Jonathan Peyton37e2ef52018-07-09 17:36:22 +00001620 KMP_ATOMIC_INC(&root->r.r_in_parallel);
Jonathan Peyton30419822017-05-12 18:01:32 +00001621 parent_team->t.t_active_level++;
1622 parent_team->t.t_level++;
Jonathan Peyton92ca6182018-09-07 18:25:49 +00001623#if OMP_50_ENABLED
1624 parent_team->t.t_def_allocator = master_th->th.th_def_allocator; // save
1625#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001626
1627 /* Change number of threads in the team if requested */
1628 if (master_set_numthreads) { // The parallel has num_threads clause
1629 if (master_set_numthreads < master_th->th.th_teams_size.nth) {
1630 // AC: only can reduce number of threads dynamically, can't increase
1631 kmp_info_t **other_threads = parent_team->t.t_threads;
1632 parent_team->t.t_nproc = master_set_numthreads;
1633 for (i = 0; i < master_set_numthreads; ++i) {
1634 other_threads[i]->th.th_team_nproc = master_set_numthreads;
1635 }
1636 // Keep extra threads hot in the team for possible next parallels
1637 }
1638 master_th->th.th_set_nproc = 0;
1639 }
1640
1641#if USE_DEBUGGER
1642 if (__kmp_debugging) { // Let debugger override number of threads.
1643 int nth = __kmp_omp_num_threads(loc);
Jonathan Peyton642688b2017-06-01 16:46:36 +00001644 if (nth > 0) { // 0 means debugger doesn't want to change num threads
Jonathan Peyton30419822017-05-12 18:01:32 +00001645 master_set_numthreads = nth;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001646 }
1647 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001648#endif
1649
1650 KF_TRACE(10, ("__kmp_fork_call: before internal fork: root=%p, team=%p, "
1651 "master_th=%p, gtid=%d\n",
1652 root, parent_team, master_th, gtid));
1653 __kmp_internal_fork(loc, gtid, parent_team);
1654 KF_TRACE(10, ("__kmp_fork_call: after internal fork: root=%p, team=%p, "
1655 "master_th=%p, gtid=%d\n",
1656 root, parent_team, master_th, gtid));
1657
1658 /* Invoke microtask for MASTER thread */
1659 KA_TRACE(20, ("__kmp_fork_call: T#%d(%d:0) invoke microtask = %p\n", gtid,
1660 parent_team->t.t_id, parent_team->t.t_pkfn));
1661
Jonathan Peytonf0682ac2018-07-30 17:41:08 +00001662 if (!parent_team->t.t_invoke(gtid)) {
1663 KMP_ASSERT2(0, "cannot invoke microtask for MASTER thread");
Jonathan Peyton30419822017-05-12 18:01:32 +00001664 }
1665 KA_TRACE(20, ("__kmp_fork_call: T#%d(%d:0) done microtask = %p\n", gtid,
1666 parent_team->t.t_id, parent_team->t.t_pkfn));
1667 KMP_MB(); /* Flush all pending memory write invalidates. */
1668
1669 KA_TRACE(20, ("__kmp_fork_call: parallel exit T#%d\n", gtid));
1670
1671 return TRUE;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001672 } // Parallel closely nested in teams construct
Jim Cownie5e8470a2013-09-27 10:38:44 +00001673#endif /* OMP_40_ENABLED */
1674
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001675#if KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00001676 if (__kmp_tasking_mode != tskm_immediate_exec) {
1677 KMP_DEBUG_ASSERT(master_th->th.th_task_team ==
1678 parent_team->t.t_task_team[master_th->th.th_task_state]);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001679 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001680#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001681
Jonathan Peyton30419822017-05-12 18:01:32 +00001682 if (parent_team->t.t_active_level >=
1683 master_th->th.th_current_task->td_icvs.max_active_levels) {
1684 nthreads = 1;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001685 } else {
Andrey Churbanov92effc42015-08-18 10:08:27 +00001686#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00001687 int enter_teams = ((ap == NULL && active_level == 0) ||
1688 (ap && teams_level > 0 && teams_level == level));
Andrey Churbanov92effc42015-08-18 10:08:27 +00001689#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001690 nthreads =
1691 master_set_numthreads
1692 ? master_set_numthreads
1693 : get__nproc_2(
1694 parent_team,
1695 master_tid); // TODO: get nproc directly from current task
Andrey Churbanov92effc42015-08-18 10:08:27 +00001696
Jonathan Peyton30419822017-05-12 18:01:32 +00001697 // Check if we need to take forkjoin lock? (no need for serialized
1698 // parallel out of teams construct). This code moved here from
1699 // __kmp_reserve_threads() to speedup nested serialized parallels.
1700 if (nthreads > 1) {
1701 if ((!get__nested(master_th) && (root->r.r_in_parallel
Andrey Churbanov92effc42015-08-18 10:08:27 +00001702#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00001703 && !enter_teams
Andrey Churbanov92effc42015-08-18 10:08:27 +00001704#endif /* OMP_40_ENABLED */
Jonathan Peyton30419822017-05-12 18:01:32 +00001705 )) ||
1706 (__kmp_library == library_serial)) {
Jonathan Peyton642688b2017-06-01 16:46:36 +00001707 KC_TRACE(10, ("__kmp_fork_call: T#%d serializing team; requested %d"
1708 " threads\n",
1709 gtid, nthreads));
Jonathan Peyton30419822017-05-12 18:01:32 +00001710 nthreads = 1;
Andrey Churbanov92effc42015-08-18 10:08:27 +00001711 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001712 }
1713 if (nthreads > 1) {
1714 /* determine how many new threads we can use */
1715 __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock);
Jonathan Peyton30419822017-05-12 18:01:32 +00001716 nthreads = __kmp_reserve_threads(
1717 root, parent_team, master_tid, nthreads
Jim Cownie5e8470a2013-09-27 10:38:44 +00001718#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00001719 /* AC: If we execute teams from parallel region (on host), then
1720 teams should be created but each can only have 1 thread if
1721 nesting is disabled. If teams called from serial region, then
1722 teams and their threads should be created regardless of the
1723 nesting setting. */
1724 ,
1725 enter_teams
Jim Cownie5e8470a2013-09-27 10:38:44 +00001726#endif /* OMP_40_ENABLED */
Jonathan Peyton30419822017-05-12 18:01:32 +00001727 );
1728 if (nthreads == 1) {
1729 // Free lock for single thread execution here; for multi-thread
1730 // execution it will be freed later after team of threads created
1731 // and initialized
1732 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
Andrey Churbanov92effc42015-08-18 10:08:27 +00001733 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001734 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001735 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001736 KMP_DEBUG_ASSERT(nthreads > 0);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001737
Jonathan Peyton30419822017-05-12 18:01:32 +00001738 // If we temporarily changed the set number of threads then restore it now
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001739 master_th->th.th_set_nproc = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001740
Jim Cownie5e8470a2013-09-27 10:38:44 +00001741 /* create a serialized parallel region? */
Jonathan Peyton30419822017-05-12 18:01:32 +00001742 if (nthreads == 1) {
1743/* josh todo: hypothetical question: what do we do for OS X*? */
1744#if KMP_OS_LINUX && \
1745 (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64)
1746 void *args[argc];
Jim Cownie5e8470a2013-09-27 10:38:44 +00001747#else
Jonathan Peyton30419822017-05-12 18:01:32 +00001748 void **args = (void **)KMP_ALLOCA(argc * sizeof(void *));
1749#endif /* KMP_OS_LINUX && ( KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || \
1750 KMP_ARCH_AARCH64) */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001751
Jonathan Peyton30419822017-05-12 18:01:32 +00001752 KA_TRACE(20,
1753 ("__kmp_fork_call: T#%d serializing parallel region\n", gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001754
Jonathan Peyton30419822017-05-12 18:01:32 +00001755 __kmpc_serialized_parallel(loc, gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001756
Jonathan Peyton30419822017-05-12 18:01:32 +00001757 if (call_context == fork_context_intel) {
1758 /* TODO this sucks, use the compiler itself to pass args! :) */
1759 master_th->th.th_serial_team->t.t_ident = loc;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001760#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00001761 if (!ap) {
1762 // revert change made in __kmpc_serialized_parallel()
1763 master_th->th.th_serial_team->t.t_level--;
1764// Get args from parent team for teams construct
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001765
1766#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00001767 void *dummy;
1768 void **exit_runtime_p;
Joachim Protze82e94a52017-11-01 10:08:30 +00001769 ompt_task_info_t *task_info;
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001770
Jonathan Peyton30419822017-05-12 18:01:32 +00001771 ompt_lw_taskteam_t lw_taskteam;
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001772
Joachim Protze82e94a52017-11-01 10:08:30 +00001773 if (ompt_enabled.enabled) {
Jonathan Peyton30419822017-05-12 18:01:32 +00001774 __ompt_lw_taskteam_init(&lw_taskteam, master_th, gtid,
Joachim Protze82e94a52017-11-01 10:08:30 +00001775 &ompt_parallel_data, return_address);
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001776
Joachim Protze82e94a52017-11-01 10:08:30 +00001777 __ompt_lw_taskteam_link(&lw_taskteam, master_th, 0);
1778 // don't use lw_taskteam after linking. content was swaped
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001779
Joachim Protze82e94a52017-11-01 10:08:30 +00001780 task_info = OMPT_CUR_TASK_INFO(master_th);
Joachim Protzec255ca72017-11-05 14:11:10 +00001781 exit_runtime_p = &(task_info->frame.exit_frame);
Joachim Protze82e94a52017-11-01 10:08:30 +00001782 if (ompt_enabled.ompt_callback_implicit_task) {
1783 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
1784 ompt_scope_begin, OMPT_CUR_TEAM_DATA(master_th),
1785 &(task_info->task_data), 1, __kmp_tid_from_gtid(gtid));
Joachim Protze9be9cf22018-05-07 12:42:21 +00001786 OMPT_CUR_TASK_INFO(master_th)
1787 ->thread_num = __kmp_tid_from_gtid(gtid);
Jonathan Peyton30419822017-05-12 18:01:32 +00001788 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001789
Jonathan Peyton30419822017-05-12 18:01:32 +00001790 /* OMPT state */
Joachim Protze82e94a52017-11-01 10:08:30 +00001791 master_th->th.ompt_thread_info.state = omp_state_work_parallel;
Jonathan Peyton30419822017-05-12 18:01:32 +00001792 } else {
1793 exit_runtime_p = &dummy;
1794 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001795#endif
1796
Jonathan Peyton30419822017-05-12 18:01:32 +00001797 {
1798 KMP_TIME_PARTITIONED_BLOCK(OMP_parallel);
1799 KMP_SET_THREAD_STATE_BLOCK(IMPLICIT_TASK);
1800 __kmp_invoke_microtask(microtask, gtid, 0, argc,
1801 parent_team->t.t_argv
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001802#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00001803 ,
1804 exit_runtime_p
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001805#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001806 );
1807 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001808
1809#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00001810 if (ompt_enabled.enabled) {
1811 exit_runtime_p = NULL;
1812 if (ompt_enabled.ompt_callback_implicit_task) {
1813 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
1814 ompt_scope_end, NULL, &(task_info->task_data), 1,
Joachim Protze9be9cf22018-05-07 12:42:21 +00001815 OMPT_CUR_TASK_INFO(master_th)->thread_num);
Jonathan Peyton30419822017-05-12 18:01:32 +00001816 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001817
Jonathan Peyton30419822017-05-12 18:01:32 +00001818 __ompt_lw_taskteam_unlink(master_th);
Joachim Protze82e94a52017-11-01 10:08:30 +00001819 if (ompt_enabled.ompt_callback_parallel_end) {
1820 ompt_callbacks.ompt_callback(ompt_callback_parallel_end)(
1821 OMPT_CUR_TEAM_DATA(master_th), parent_task_data,
1822 OMPT_INVOKER(call_context), return_address);
Jonathan Peyton30419822017-05-12 18:01:32 +00001823 }
Joachim Protze82e94a52017-11-01 10:08:30 +00001824 master_th->th.ompt_thread_info.state = omp_state_overhead;
Jonathan Peyton30419822017-05-12 18:01:32 +00001825 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001826#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001827 } else if (microtask == (microtask_t)__kmp_teams_master) {
1828 KMP_DEBUG_ASSERT(master_th->th.th_team ==
1829 master_th->th.th_serial_team);
1830 team = master_th->th.th_team;
1831 // team->t.t_pkfn = microtask;
1832 team->t.t_invoke = invoker;
1833 __kmp_alloc_argv_entries(argc, team, TRUE);
1834 team->t.t_argc = argc;
1835 argv = (void **)team->t.t_argv;
1836 if (ap) {
1837 for (i = argc - 1; i >= 0; --i)
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001838// TODO: revert workaround for Intel(R) 64 tracker #96
Andrey Churbanovcbda8682015-01-13 14:43:35 +00001839#if (KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64) && KMP_OS_LINUX
Jonathan Peyton30419822017-05-12 18:01:32 +00001840 *argv++ = va_arg(*ap, void *);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001841#else
Jonathan Peyton30419822017-05-12 18:01:32 +00001842 *argv++ = va_arg(ap, void *);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001843#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001844 } else {
1845 for (i = 0; i < argc; ++i)
1846 // Get args from parent team for teams construct
1847 argv[i] = parent_team->t.t_argv[i];
1848 }
1849 // AC: revert change made in __kmpc_serialized_parallel()
1850 // because initial code in teams should have level=0
1851 team->t.t_level--;
1852 // AC: call special invoker for outer "parallel" of teams construct
Jonathan Peytonf0682ac2018-07-30 17:41:08 +00001853 invoker(gtid);
Jonathan Peyton30419822017-05-12 18:01:32 +00001854 } else {
1855#endif /* OMP_40_ENABLED */
1856 argv = args;
1857 for (i = argc - 1; i >= 0; --i)
1858// TODO: revert workaround for Intel(R) 64 tracker #96
1859#if (KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64) && KMP_OS_LINUX
1860 *argv++ = va_arg(*ap, void *);
1861#else
1862 *argv++ = va_arg(ap, void *);
1863#endif
1864 KMP_MB();
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001865
1866#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00001867 void *dummy;
1868 void **exit_runtime_p;
Joachim Protze82e94a52017-11-01 10:08:30 +00001869 ompt_task_info_t *task_info;
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001870
Jonathan Peyton30419822017-05-12 18:01:32 +00001871 ompt_lw_taskteam_t lw_taskteam;
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001872
Joachim Protze82e94a52017-11-01 10:08:30 +00001873 if (ompt_enabled.enabled) {
Jonathan Peyton30419822017-05-12 18:01:32 +00001874 __ompt_lw_taskteam_init(&lw_taskteam, master_th, gtid,
Joachim Protze82e94a52017-11-01 10:08:30 +00001875 &ompt_parallel_data, return_address);
1876 __ompt_lw_taskteam_link(&lw_taskteam, master_th, 0);
1877 // don't use lw_taskteam after linking. content was swaped
1878 task_info = OMPT_CUR_TASK_INFO(master_th);
Joachim Protzec255ca72017-11-05 14:11:10 +00001879 exit_runtime_p = &(task_info->frame.exit_frame);
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001880
Jonathan Peyton30419822017-05-12 18:01:32 +00001881 /* OMPT implicit task begin */
Joachim Protze82e94a52017-11-01 10:08:30 +00001882 implicit_task_data = OMPT_CUR_TASK_DATA(master_th);
1883 if (ompt_enabled.ompt_callback_implicit_task) {
1884 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
1885 ompt_scope_begin, OMPT_CUR_TEAM_DATA(master_th),
1886 implicit_task_data, 1, __kmp_tid_from_gtid(gtid));
Joachim Protze9be9cf22018-05-07 12:42:21 +00001887 OMPT_CUR_TASK_INFO(master_th)
1888 ->thread_num = __kmp_tid_from_gtid(gtid);
Jonathan Peyton30419822017-05-12 18:01:32 +00001889 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001890
Jonathan Peyton30419822017-05-12 18:01:32 +00001891 /* OMPT state */
Joachim Protze82e94a52017-11-01 10:08:30 +00001892 master_th->th.ompt_thread_info.state = omp_state_work_parallel;
Jonathan Peyton30419822017-05-12 18:01:32 +00001893 } else {
1894 exit_runtime_p = &dummy;
1895 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001896#endif
1897
Jonathan Peyton30419822017-05-12 18:01:32 +00001898 {
1899 KMP_TIME_PARTITIONED_BLOCK(OMP_parallel);
1900 KMP_SET_THREAD_STATE_BLOCK(IMPLICIT_TASK);
1901 __kmp_invoke_microtask(microtask, gtid, 0, argc, args
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001902#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00001903 ,
1904 exit_runtime_p
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001905#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001906 );
1907 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001908
1909#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00001910 if (ompt_enabled.enabled) {
1911 *exit_runtime_p = NULL;
1912 if (ompt_enabled.ompt_callback_implicit_task) {
1913 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
1914 ompt_scope_end, NULL, &(task_info->task_data), 1,
Joachim Protze9be9cf22018-05-07 12:42:21 +00001915 OMPT_CUR_TASK_INFO(master_th)->thread_num);
Jonathan Peyton30419822017-05-12 18:01:32 +00001916 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001917
Joachim Protze82e94a52017-11-01 10:08:30 +00001918 ompt_parallel_data = *OMPT_CUR_TEAM_DATA(master_th);
Jonathan Peyton30419822017-05-12 18:01:32 +00001919 __ompt_lw_taskteam_unlink(master_th);
Joachim Protze82e94a52017-11-01 10:08:30 +00001920 if (ompt_enabled.ompt_callback_parallel_end) {
1921 ompt_callbacks.ompt_callback(ompt_callback_parallel_end)(
1922 &ompt_parallel_data, parent_task_data,
1923 OMPT_INVOKER(call_context), return_address);
Jonathan Peyton30419822017-05-12 18:01:32 +00001924 }
Joachim Protze82e94a52017-11-01 10:08:30 +00001925 master_th->th.ompt_thread_info.state = omp_state_overhead;
Jonathan Peyton30419822017-05-12 18:01:32 +00001926 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001927#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001928#if OMP_40_ENABLED
Jim Cownie5e8470a2013-09-27 10:38:44 +00001929 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001930#endif /* OMP_40_ENABLED */
1931 } else if (call_context == fork_context_gnu) {
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001932#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00001933 ompt_lw_taskteam_t lwt;
1934 __ompt_lw_taskteam_init(&lwt, master_th, gtid, &ompt_parallel_data,
1935 return_address);
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001936
Joachim Protzec255ca72017-11-05 14:11:10 +00001937 lwt.ompt_task_info.frame.exit_frame = NULL;
Joachim Protze82e94a52017-11-01 10:08:30 +00001938 __ompt_lw_taskteam_link(&lwt, master_th, 1);
1939// don't use lw_taskteam after linking. content was swaped
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00001940#endif
1941
Jonathan Peyton30419822017-05-12 18:01:32 +00001942 // we were called from GNU native code
1943 KA_TRACE(20, ("__kmp_fork_call: T#%d serial exit\n", gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001944 return FALSE;
Andrey Churbanovc47afcd2017-07-03 11:24:08 +00001945 } else {
Jonathan Peyton30419822017-05-12 18:01:32 +00001946 KMP_ASSERT2(call_context < fork_context_last,
1947 "__kmp_fork_call: unknown fork_context parameter");
1948 }
1949
1950 KA_TRACE(20, ("__kmp_fork_call: T#%d serial exit\n", gtid));
1951 KMP_MB();
1952 return FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001953 }
1954
Jim Cownie5e8470a2013-09-27 10:38:44 +00001955 // GEH: only modify the executing flag in the case when not serialized
1956 // serialized case is handled in kmpc_serialized_parallel
Jonathan Peyton30419822017-05-12 18:01:32 +00001957 KF_TRACE(10, ("__kmp_fork_call: parent_team_aclevel=%d, master_th=%p, "
1958 "curtask=%p, curtask_max_aclevel=%d\n",
1959 parent_team->t.t_active_level, master_th,
1960 master_th->th.th_current_task,
1961 master_th->th.th_current_task->td_icvs.max_active_levels));
1962 // TODO: GEH - cannot do this assertion because root thread not set up as
1963 // executing
Jim Cownie5e8470a2013-09-27 10:38:44 +00001964 // KMP_ASSERT( master_th->th.th_current_task->td_flags.executing == 1 );
1965 master_th->th.th_current_task->td_flags.executing = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001966
1967#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00001968 if (!master_th->th.th_teams_microtask || level > teams_level)
Jim Cownie5e8470a2013-09-27 10:38:44 +00001969#endif /* OMP_40_ENABLED */
1970 {
Jonathan Peyton30419822017-05-12 18:01:32 +00001971 /* Increment our nested depth level */
Jonathan Peyton37e2ef52018-07-09 17:36:22 +00001972 KMP_ATOMIC_INC(&root->r.r_in_parallel);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001973 }
1974
Jim Cownie5e8470a2013-09-27 10:38:44 +00001975 // See if we need to make a copy of the ICVs.
Jim Cownie5e8470a2013-09-27 10:38:44 +00001976 int nthreads_icv = master_th->th.th_current_task->td_icvs.nproc;
Jonathan Peyton30419822017-05-12 18:01:32 +00001977 if ((level + 1 < __kmp_nested_nth.used) &&
1978 (__kmp_nested_nth.nth[level + 1] != nthreads_icv)) {
1979 nthreads_icv = __kmp_nested_nth.nth[level + 1];
1980 } else {
1981 nthreads_icv = 0; // don't update
Jim Cownie5e8470a2013-09-27 10:38:44 +00001982 }
1983
1984#if OMP_40_ENABLED
Jim Cownie5e8470a2013-09-27 10:38:44 +00001985 // Figure out the proc_bind_policy for the new team.
Jim Cownie5e8470a2013-09-27 10:38:44 +00001986 kmp_proc_bind_t proc_bind = master_th->th.th_set_proc_bind;
Jonathan Peyton30419822017-05-12 18:01:32 +00001987 kmp_proc_bind_t proc_bind_icv =
1988 proc_bind_default; // proc_bind_default means don't update
1989 if (master_th->th.th_current_task->td_icvs.proc_bind == proc_bind_false) {
1990 proc_bind = proc_bind_false;
1991 } else {
1992 if (proc_bind == proc_bind_default) {
1993 // No proc_bind clause specified; use current proc-bind-var for this
1994 // parallel region
1995 proc_bind = master_th->th.th_current_task->td_icvs.proc_bind;
1996 }
1997 /* else: The proc_bind policy was specified explicitly on parallel clause.
1998 This overrides proc-bind-var for this parallel region, but does not
1999 change proc-bind-var. */
2000 // Figure the value of proc-bind-var for the child threads.
2001 if ((level + 1 < __kmp_nested_proc_bind.used) &&
2002 (__kmp_nested_proc_bind.bind_types[level + 1] !=
2003 master_th->th.th_current_task->td_icvs.proc_bind)) {
2004 proc_bind_icv = __kmp_nested_proc_bind.bind_types[level + 1];
2005 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002006 }
2007
Jim Cownie5e8470a2013-09-27 10:38:44 +00002008 // Reset for next parallel region
Jim Cownie5e8470a2013-09-27 10:38:44 +00002009 master_th->th.th_set_proc_bind = proc_bind_default;
2010#endif /* OMP_40_ENABLED */
2011
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002012 if ((nthreads_icv > 0)
Jim Cownie5e8470a2013-09-27 10:38:44 +00002013#if OMP_40_ENABLED
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002014 || (proc_bind_icv != proc_bind_default)
Jim Cownie5e8470a2013-09-27 10:38:44 +00002015#endif /* OMP_40_ENABLED */
Jonathan Peyton30419822017-05-12 18:01:32 +00002016 ) {
2017 kmp_internal_control_t new_icvs;
2018 copy_icvs(&new_icvs, &master_th->th.th_current_task->td_icvs);
2019 new_icvs.next = NULL;
2020 if (nthreads_icv > 0) {
2021 new_icvs.nproc = nthreads_icv;
2022 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002023
2024#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00002025 if (proc_bind_icv != proc_bind_default) {
2026 new_icvs.proc_bind = proc_bind_icv;
2027 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002028#endif /* OMP_40_ENABLED */
2029
Jonathan Peyton30419822017-05-12 18:01:32 +00002030 /* allocate a new parallel team */
2031 KF_TRACE(10, ("__kmp_fork_call: before __kmp_allocate_team\n"));
2032 team = __kmp_allocate_team(root, nthreads, nthreads,
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002033#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00002034 ompt_parallel_data,
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002035#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00002036#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00002037 proc_bind,
Jim Cownie5e8470a2013-09-27 10:38:44 +00002038#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00002039 &new_icvs, argc USE_NESTED_HOT_ARG(master_th));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002040 } else {
Jonathan Peyton30419822017-05-12 18:01:32 +00002041 /* allocate a new parallel team */
2042 KF_TRACE(10, ("__kmp_fork_call: before __kmp_allocate_team\n"));
2043 team = __kmp_allocate_team(root, nthreads, nthreads,
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002044#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00002045 ompt_parallel_data,
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002046#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00002047#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00002048 proc_bind,
Jim Cownie5e8470a2013-09-27 10:38:44 +00002049#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00002050 &master_th->th.th_current_task->td_icvs,
2051 argc USE_NESTED_HOT_ARG(master_th));
Jim Cownie5e8470a2013-09-27 10:38:44 +00002052 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002053 KF_TRACE(
2054 10, ("__kmp_fork_call: after __kmp_allocate_team - team = %p\n", team));
Jim Cownie5e8470a2013-09-27 10:38:44 +00002055
2056 /* setup the new team */
Jonathan Peytonb044e4f2016-05-23 18:01:19 +00002057 KMP_CHECK_UPDATE(team->t.t_master_tid, master_tid);
2058 KMP_CHECK_UPDATE(team->t.t_master_this_cons, master_this_cons);
2059 KMP_CHECK_UPDATE(team->t.t_ident, loc);
2060 KMP_CHECK_UPDATE(team->t.t_parent, parent_team);
2061 KMP_CHECK_UPDATE_SYNC(team->t.t_pkfn, microtask);
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002062#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00002063 KMP_CHECK_UPDATE_SYNC(team->t.ompt_team_info.master_return_address,
2064 return_address);
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002065#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00002066 KMP_CHECK_UPDATE(team->t.t_invoke, invoker); // TODO move to root, maybe
2067// TODO: parent_team->t.t_level == INT_MAX ???
Jim Cownie5e8470a2013-09-27 10:38:44 +00002068#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00002069 if (!master_th->th.th_teams_microtask || level > teams_level) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00002070#endif /* OMP_40_ENABLED */
Jonathan Peyton30419822017-05-12 18:01:32 +00002071 int new_level = parent_team->t.t_level + 1;
2072 KMP_CHECK_UPDATE(team->t.t_level, new_level);
2073 new_level = parent_team->t.t_active_level + 1;
2074 KMP_CHECK_UPDATE(team->t.t_active_level, new_level);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002075#if OMP_40_ENABLED
2076 } else {
Jonathan Peyton30419822017-05-12 18:01:32 +00002077 // AC: Do not increase parallel level at start of the teams construct
2078 int new_level = parent_team->t.t_level;
2079 KMP_CHECK_UPDATE(team->t.t_level, new_level);
2080 new_level = parent_team->t.t_active_level;
2081 KMP_CHECK_UPDATE(team->t.t_active_level, new_level);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002082 }
2083#endif /* OMP_40_ENABLED */
Jonathan Peytonb044e4f2016-05-23 18:01:19 +00002084 kmp_r_sched_t new_sched = get__sched_2(parent_team, master_tid);
Jonathan Peytonba55a7b2017-11-29 22:47:52 +00002085 // set master's schedule as new run-time schedule
2086 KMP_CHECK_UPDATE(team->t.t_sched.sched, new_sched.sched);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002087
Jonathan Peyton45ca5da2015-10-19 19:33:38 +00002088#if OMP_40_ENABLED
Jonathan Peytonb044e4f2016-05-23 18:01:19 +00002089 KMP_CHECK_UPDATE(team->t.t_cancel_request, cancel_noreq);
Jonathan Peyton45ca5da2015-10-19 19:33:38 +00002090#endif
Jonathan Peyton92ca6182018-09-07 18:25:49 +00002091#if OMP_50_ENABLED
2092 KMP_CHECK_UPDATE(team->t.t_def_allocator, master_th->th.th_def_allocator);
2093#endif
Jonathan Peyton45ca5da2015-10-19 19:33:38 +00002094
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002095 // Update the floating point rounding in the team if required.
2096 propagateFPControl(team);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002097
Jonathan Peyton30419822017-05-12 18:01:32 +00002098 if (__kmp_tasking_mode != tskm_immediate_exec) {
2099 // Set master's task team to team's task team. Unless this is hot team, it
2100 // should be NULL.
Jonathan Peyton30419822017-05-12 18:01:32 +00002101 KMP_DEBUG_ASSERT(master_th->th.th_task_team ==
2102 parent_team->t.t_task_team[master_th->th.th_task_state]);
Jonathan Peyton30419822017-05-12 18:01:32 +00002103 KA_TRACE(20, ("__kmp_fork_call: Master T#%d pushing task_team %p / team "
2104 "%p, new task_team %p / team %p\n",
2105 __kmp_gtid_from_thread(master_th),
2106 master_th->th.th_task_team, parent_team,
2107 team->t.t_task_team[master_th->th.th_task_state], team));
Jonathan Peytond3f2b942016-02-09 22:32:41 +00002108
Jonathan Peyton30419822017-05-12 18:01:32 +00002109 if (active_level || master_th->th.th_task_team) {
2110 // Take a memo of master's task_state
2111 KMP_DEBUG_ASSERT(master_th->th.th_task_state_memo_stack);
2112 if (master_th->th.th_task_state_top >=
2113 master_th->th.th_task_state_stack_sz) { // increase size
2114 kmp_uint32 new_size = 2 * master_th->th.th_task_state_stack_sz;
2115 kmp_uint8 *old_stack, *new_stack;
2116 kmp_uint32 i;
2117 new_stack = (kmp_uint8 *)__kmp_allocate(new_size);
2118 for (i = 0; i < master_th->th.th_task_state_stack_sz; ++i) {
2119 new_stack[i] = master_th->th.th_task_state_memo_stack[i];
2120 }
2121 for (i = master_th->th.th_task_state_stack_sz; i < new_size;
2122 ++i) { // zero-init rest of stack
2123 new_stack[i] = 0;
2124 }
2125 old_stack = master_th->th.th_task_state_memo_stack;
2126 master_th->th.th_task_state_memo_stack = new_stack;
2127 master_th->th.th_task_state_stack_sz = new_size;
2128 __kmp_free(old_stack);
Andrey Churbanov6d224db2015-02-10 18:37:43 +00002129 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002130 // Store master's task_state on stack
2131 master_th->th
2132 .th_task_state_memo_stack[master_th->th.th_task_state_top] =
2133 master_th->th.th_task_state;
2134 master_th->th.th_task_state_top++;
2135#if KMP_NESTED_HOT_TEAMS
Jonathan Peytonca10a762018-08-24 18:05:00 +00002136 if (master_th->th.th_hot_teams &&
2137 team == master_th->th.th_hot_teams[active_level].hot_team) {
Jonathan Peyton642688b2017-06-01 16:46:36 +00002138 // Restore master's nested state if nested hot team
Jonathan Peyton30419822017-05-12 18:01:32 +00002139 master_th->th.th_task_state =
2140 master_th->th
2141 .th_task_state_memo_stack[master_th->th.th_task_state_top];
2142 } else {
2143#endif
2144 master_th->th.th_task_state = 0;
2145#if KMP_NESTED_HOT_TEAMS
2146 }
2147#endif
2148 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002149#if !KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00002150 KMP_DEBUG_ASSERT((master_th->th.th_task_team == NULL) ||
2151 (team == root->r.r_hot_team));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002152#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00002153 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002154
Jonathan Peyton30419822017-05-12 18:01:32 +00002155 KA_TRACE(
2156 20,
2157 ("__kmp_fork_call: T#%d(%d:%d)->(%d:0) created a team of %d threads\n",
2158 gtid, parent_team->t.t_id, team->t.t_master_tid, team->t.t_id,
2159 team->t.t_nproc));
2160 KMP_DEBUG_ASSERT(team != root->r.r_hot_team ||
2161 (team->t.t_master_tid == 0 &&
2162 (team->t.t_parent == root->r.r_root_team ||
2163 team->t.t_parent->t.t_serialized)));
Jim Cownie5e8470a2013-09-27 10:38:44 +00002164 KMP_MB();
2165
2166 /* now, setup the arguments */
Jonathan Peyton30419822017-05-12 18:01:32 +00002167 argv = (void **)team->t.t_argv;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002168#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00002169 if (ap) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00002170#endif /* OMP_40_ENABLED */
Jonathan Peyton30419822017-05-12 18:01:32 +00002171 for (i = argc - 1; i >= 0; --i) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002172// TODO: revert workaround for Intel(R) 64 tracker #96
Andrey Churbanovcbda8682015-01-13 14:43:35 +00002173#if (KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64) && KMP_OS_LINUX
Jonathan Peyton30419822017-05-12 18:01:32 +00002174 void *new_argv = va_arg(*ap, void *);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002175#else
Jonathan Peyton30419822017-05-12 18:01:32 +00002176 void *new_argv = va_arg(ap, void *);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002177#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00002178 KMP_CHECK_UPDATE(*argv, new_argv);
2179 argv++;
2180 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002181#if OMP_40_ENABLED
2182 } else {
Jonathan Peyton30419822017-05-12 18:01:32 +00002183 for (i = 0; i < argc; ++i) {
2184 // Get args from parent team for teams construct
2185 KMP_CHECK_UPDATE(argv[i], team->t.t_parent->t.t_argv[i]);
2186 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002187 }
2188#endif /* OMP_40_ENABLED */
2189
2190 /* now actually fork the threads */
Jonathan Peytonb044e4f2016-05-23 18:01:19 +00002191 KMP_CHECK_UPDATE(team->t.t_master_active, master_active);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002192 if (!root->r.r_active) // Only do assignment if it prevents cache ping-pong
Jonathan Peyton30419822017-05-12 18:01:32 +00002193 root->r.r_active = TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002194
Jonathan Peyton30419822017-05-12 18:01:32 +00002195 __kmp_fork_team_threads(root, team, master_th, gtid);
2196 __kmp_setup_icv_copy(team, nthreads,
2197 &master_th->th.th_current_task->td_icvs, loc);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002198
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002199#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00002200 master_th->th.ompt_thread_info.state = omp_state_work_parallel;
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002201#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00002202
Jonathan Peyton30419822017-05-12 18:01:32 +00002203 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002204
Jim Cownie5e8470a2013-09-27 10:38:44 +00002205#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00002206 if (team->t.t_active_level == 1 // only report frames at level 1
2207#if OMP_40_ENABLED
Andrey Churbanov51aecb82015-05-06 19:22:36 +00002208 && !master_th->th.th_teams_microtask // not in teams construct
Jonathan Peyton30419822017-05-12 18:01:32 +00002209#endif /* OMP_40_ENABLED */
2210 ) {
Andrey Churbanov51aecb82015-05-06 19:22:36 +00002211#if USE_ITT_NOTIFY
Jonathan Peyton30419822017-05-12 18:01:32 +00002212 if ((__itt_frame_submit_v3_ptr || KMP_ITT_DEBUG) &&
2213 (__kmp_forkjoin_frames_mode == 3 ||
2214 __kmp_forkjoin_frames_mode == 1)) {
2215 kmp_uint64 tmp_time = 0;
2216 if (__itt_get_timestamp_ptr)
2217 tmp_time = __itt_get_timestamp();
2218 // Internal fork - report frame begin
2219 master_th->th.th_frame_time = tmp_time;
2220 if (__kmp_forkjoin_frames_mode == 3)
2221 team->t.t_region_time = tmp_time;
Jonathan Peyton642688b2017-06-01 16:46:36 +00002222 } else
2223// only one notification scheme (either "submit" or "forking/joined", not both)
Andrey Churbanov51aecb82015-05-06 19:22:36 +00002224#endif /* USE_ITT_NOTIFY */
Jonathan Peyton30419822017-05-12 18:01:32 +00002225 if ((__itt_frame_begin_v3_ptr || KMP_ITT_DEBUG) &&
2226 __kmp_forkjoin_frames && !__kmp_forkjoin_frames_mode) {
Jonathan Peyton8c432f22018-01-04 22:56:47 +00002227 // Mark start of "parallel" region for Intel(R) VTune(TM) analyzer.
Jonathan Peyton30419822017-05-12 18:01:32 +00002228 __kmp_itt_region_forking(gtid, team->t.t_nproc, 0);
2229 }
Andrey Churbanovf6451d92015-01-16 15:58:03 +00002230 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002231#endif /* USE_ITT_BUILD */
2232
2233 /* now go on and do the work */
Jonathan Peyton30419822017-05-12 18:01:32 +00002234 KMP_DEBUG_ASSERT(team == __kmp_threads[gtid]->th.th_team);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002235 KMP_MB();
Jonathan Peyton30419822017-05-12 18:01:32 +00002236 KF_TRACE(10,
2237 ("__kmp_internal_fork : root=%p, team=%p, master_th=%p, gtid=%d\n",
2238 root, team, master_th, gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00002239
2240#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00002241 if (__itt_stack_caller_create_ptr) {
2242 team->t.t_stack_id =
2243 __kmp_itt_stack_caller_create(); // create new stack stitching id
2244 // before entering fork barrier
Jim Cownie5e8470a2013-09-27 10:38:44 +00002245 }
2246#endif /* USE_ITT_BUILD */
2247
2248#if OMP_40_ENABLED
Jonathan Peyton642688b2017-06-01 16:46:36 +00002249 // AC: skip __kmp_internal_fork at teams construct, let only master
2250 // threads execute
2251 if (ap)
Jim Cownie5e8470a2013-09-27 10:38:44 +00002252#endif /* OMP_40_ENABLED */
2253 {
Jonathan Peyton30419822017-05-12 18:01:32 +00002254 __kmp_internal_fork(loc, gtid, team);
2255 KF_TRACE(10, ("__kmp_internal_fork : after : root=%p, team=%p, "
2256 "master_th=%p, gtid=%d\n",
2257 root, team, master_th, gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00002258 }
2259
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002260 if (call_context == fork_context_gnu) {
Jonathan Peyton30419822017-05-12 18:01:32 +00002261 KA_TRACE(20, ("__kmp_fork_call: parallel exit T#%d\n", gtid));
2262 return TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002263 }
2264
2265 /* Invoke microtask for MASTER thread */
Jonathan Peyton30419822017-05-12 18:01:32 +00002266 KA_TRACE(20, ("__kmp_fork_call: T#%d(%d:0) invoke microtask = %p\n", gtid,
2267 team->t.t_id, team->t.t_pkfn));
2268 } // END of timer KMP_fork_call block
Jim Cownie5e8470a2013-09-27 10:38:44 +00002269
Jonathan Peytonf0682ac2018-07-30 17:41:08 +00002270 if (!team->t.t_invoke(gtid)) {
2271 KMP_ASSERT2(0, "cannot invoke microtask for MASTER thread");
Jonathan Peyton30419822017-05-12 18:01:32 +00002272 }
2273 KA_TRACE(20, ("__kmp_fork_call: T#%d(%d:0) done microtask = %p\n", gtid,
2274 team->t.t_id, team->t.t_pkfn));
2275 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00002276
Jonathan Peyton30419822017-05-12 18:01:32 +00002277 KA_TRACE(20, ("__kmp_fork_call: parallel exit T#%d\n", gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00002278
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002279#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00002280 if (ompt_enabled.enabled) {
2281 master_th->th.ompt_thread_info.state = omp_state_overhead;
Jonathan Peyton30419822017-05-12 18:01:32 +00002282 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002283#endif
2284
Jonathan Peyton30419822017-05-12 18:01:32 +00002285 return TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002286}
2287
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002288#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00002289static inline void __kmp_join_restore_state(kmp_info_t *thread,
2290 kmp_team_t *team) {
2291 // restore state outside the region
2292 thread->th.ompt_thread_info.state =
Joachim Protze82e94a52017-11-01 10:08:30 +00002293 ((team->t.t_serialized) ? omp_state_work_serial
2294 : omp_state_work_parallel);
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002295}
2296
Joachim Protze82e94a52017-11-01 10:08:30 +00002297static inline void __kmp_join_ompt(int gtid, kmp_info_t *thread,
2298 kmp_team_t *team, ompt_data_t *parallel_data,
2299 fork_context_e fork_context, void *codeptr) {
2300 ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
2301 if (ompt_enabled.ompt_callback_parallel_end) {
2302 ompt_callbacks.ompt_callback(ompt_callback_parallel_end)(
2303 parallel_data, &(task_info->task_data), OMPT_INVOKER(fork_context),
2304 codeptr);
Jonathan Peyton30419822017-05-12 18:01:32 +00002305 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002306
Joachim Protzec255ca72017-11-05 14:11:10 +00002307 task_info->frame.enter_frame = NULL;
Jonathan Peyton30419822017-05-12 18:01:32 +00002308 __kmp_join_restore_state(thread, team);
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002309}
2310#endif
2311
Jonathan Peyton30419822017-05-12 18:01:32 +00002312void __kmp_join_call(ident_t *loc, int gtid
Jonathan Peytonf89fbbb2015-08-31 18:15:00 +00002313#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00002314 ,
2315 enum fork_context_e fork_context
Jonathan Peytonf89fbbb2015-08-31 18:15:00 +00002316#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00002317#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00002318 ,
2319 int exit_teams
Jim Cownie5e8470a2013-09-27 10:38:44 +00002320#endif /* OMP_40_ENABLED */
Jonathan Peyton30419822017-05-12 18:01:32 +00002321 ) {
2322 KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(KMP_join_call);
2323 kmp_team_t *team;
2324 kmp_team_t *parent_team;
2325 kmp_info_t *master_th;
2326 kmp_root_t *root;
2327 int master_active;
2328 int i;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002329
Jonathan Peyton30419822017-05-12 18:01:32 +00002330 KA_TRACE(20, ("__kmp_join_call: enter T#%d\n", gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00002331
Jonathan Peyton30419822017-05-12 18:01:32 +00002332 /* setup current data */
2333 master_th = __kmp_threads[gtid];
2334 root = master_th->th.th_root;
2335 team = master_th->th.th_team;
2336 parent_team = team->t.t_parent;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002337
Jonathan Peyton30419822017-05-12 18:01:32 +00002338 master_th->th.th_ident = loc;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002339
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002340#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00002341 if (ompt_enabled.enabled) {
2342 master_th->th.ompt_thread_info.state = omp_state_overhead;
Jonathan Peyton30419822017-05-12 18:01:32 +00002343 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002344#endif
2345
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002346#if KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00002347 if (__kmp_tasking_mode != tskm_immediate_exec && !exit_teams) {
2348 KA_TRACE(20, ("__kmp_join_call: T#%d, old team = %p old task_team = %p, "
2349 "th_task_team = %p\n",
2350 __kmp_gtid_from_thread(master_th), team,
2351 team->t.t_task_team[master_th->th.th_task_state],
2352 master_th->th.th_task_team));
2353 KMP_DEBUG_ASSERT(master_th->th.th_task_team ==
2354 team->t.t_task_team[master_th->th.th_task_state]);
2355 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002356#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00002357
Jonathan Peyton30419822017-05-12 18:01:32 +00002358 if (team->t.t_serialized) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00002359#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00002360 if (master_th->th.th_teams_microtask) {
2361 // We are in teams construct
2362 int level = team->t.t_level;
2363 int tlevel = master_th->th.th_teams_level;
2364 if (level == tlevel) {
2365 // AC: we haven't incremented it earlier at start of teams construct,
2366 // so do it here - at the end of teams construct
2367 team->t.t_level++;
2368 } else if (level == tlevel + 1) {
2369 // AC: we are exiting parallel inside teams, need to increment
2370 // serialization in order to restore it in the next call to
2371 // __kmpc_end_serialized_parallel
2372 team->t.t_serialized++;
2373 }
Andrey Churbanov6d224db2015-02-10 18:37:43 +00002374 }
Jonathan Peyton441f3372015-09-21 17:24:46 +00002375#endif /* OMP_40_ENABLED */
Jonathan Peyton30419822017-05-12 18:01:32 +00002376 __kmpc_end_serialized_parallel(loc, gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002377
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002378#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00002379 if (ompt_enabled.enabled) {
Jonathan Peyton30419822017-05-12 18:01:32 +00002380 __kmp_join_restore_state(master_th, parent_team);
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00002381 }
2382#endif
2383
Jonathan Peyton30419822017-05-12 18:01:32 +00002384 return;
2385 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002386
Jonathan Peyton30419822017-05-12 18:01:32 +00002387 master_active = team->t.t_master_active;
2388
2389#if OMP_40_ENABLED
2390 if (!exit_teams)
2391#endif /* OMP_40_ENABLED */
2392 {
2393 // AC: No barrier for internal teams at exit from teams construct.
2394 // But there is barrier for external team (league).
2395 __kmp_internal_join(loc, gtid, team);
2396 }
2397#if OMP_40_ENABLED
2398 else {
2399 master_th->th.th_task_state =
2400 0; // AC: no tasking in teams (out of any parallel)
2401 }
2402#endif /* OMP_40_ENABLED */
2403
2404 KMP_MB();
2405
2406#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00002407 ompt_data_t *parallel_data = &(team->t.ompt_team_info.parallel_data);
2408 void *codeptr = team->t.ompt_team_info.master_return_address;
Jonathan Peyton30419822017-05-12 18:01:32 +00002409#endif
2410
2411#if USE_ITT_BUILD
2412 if (__itt_stack_caller_create_ptr) {
2413 __kmp_itt_stack_caller_destroy(
2414 (__itt_caller)team->t
2415 .t_stack_id); // destroy the stack stitching id after join barrier
2416 }
2417
Jonathan Peyton8c432f22018-01-04 22:56:47 +00002418 // Mark end of "parallel" region for Intel(R) VTune(TM) analyzer.
Jonathan Peyton30419822017-05-12 18:01:32 +00002419 if (team->t.t_active_level == 1
2420#if OMP_40_ENABLED
2421 && !master_th->th.th_teams_microtask /* not in teams construct */
2422#endif /* OMP_40_ENABLED */
2423 ) {
2424 master_th->th.th_ident = loc;
2425 // only one notification scheme (either "submit" or "forking/joined", not
2426 // both)
2427 if ((__itt_frame_submit_v3_ptr || KMP_ITT_DEBUG) &&
2428 __kmp_forkjoin_frames_mode == 3)
2429 __kmp_itt_frame_submit(gtid, team->t.t_region_time,
2430 master_th->th.th_frame_time, 0, loc,
2431 master_th->th.th_team_nproc, 1);
2432 else if ((__itt_frame_end_v3_ptr || KMP_ITT_DEBUG) &&
2433 !__kmp_forkjoin_frames_mode && __kmp_forkjoin_frames)
2434 __kmp_itt_region_joined(gtid);
2435 } // active_level == 1
2436#endif /* USE_ITT_BUILD */
2437
2438#if OMP_40_ENABLED
2439 if (master_th->th.th_teams_microtask && !exit_teams &&
2440 team->t.t_pkfn != (microtask_t)__kmp_teams_master &&
2441 team->t.t_level == master_th->th.th_teams_level + 1) {
2442 // AC: We need to leave the team structure intact at the end of parallel
2443 // inside the teams construct, so that at the next parallel same (hot) team
2444 // works, only adjust nesting levels
2445
2446 /* Decrement our nested depth level */
2447 team->t.t_level--;
2448 team->t.t_active_level--;
Jonathan Peyton37e2ef52018-07-09 17:36:22 +00002449 KMP_ATOMIC_DEC(&root->r.r_in_parallel);
Jonathan Peyton30419822017-05-12 18:01:32 +00002450
2451 /* Restore number of threads in the team if needed */
2452 if (master_th->th.th_team_nproc < master_th->th.th_teams_size.nth) {
2453 int old_num = master_th->th.th_team_nproc;
2454 int new_num = master_th->th.th_teams_size.nth;
2455 kmp_info_t **other_threads = team->t.t_threads;
2456 team->t.t_nproc = new_num;
2457 for (i = 0; i < old_num; ++i) {
2458 other_threads[i]->th.th_team_nproc = new_num;
2459 }
2460 // Adjust states of non-used threads of the team
2461 for (i = old_num; i < new_num; ++i) {
2462 // Re-initialize thread's barrier data.
2463 int b;
2464 kmp_balign_t *balign = other_threads[i]->th.th_bar;
2465 for (b = 0; b < bs_last_barrier; ++b) {
2466 balign[b].bb.b_arrived = team->t.t_bar[b].b_arrived;
2467 KMP_DEBUG_ASSERT(balign[b].bb.wait_flag != KMP_BARRIER_PARENT_FLAG);
2468#if USE_DEBUGGER
2469 balign[b].bb.b_worker_arrived = team->t.t_bar[b].b_team_arrived;
2470#endif
2471 }
2472 if (__kmp_tasking_mode != tskm_immediate_exec) {
2473 // Synchronize thread's task state
2474 other_threads[i]->th.th_task_state = master_th->th.th_task_state;
2475 }
2476 }
2477 }
2478
2479#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00002480 if (ompt_enabled.enabled) {
2481 __kmp_join_ompt(gtid, master_th, parent_team, parallel_data, fork_context,
2482 codeptr);
Jonathan Peyton30419822017-05-12 18:01:32 +00002483 }
2484#endif
2485
2486 return;
2487 }
2488#endif /* OMP_40_ENABLED */
2489
2490 /* do cleanup and restore the parent team */
2491 master_th->th.th_info.ds.ds_tid = team->t.t_master_tid;
2492 master_th->th.th_local.this_construct = team->t.t_master_this_cons;
2493
2494 master_th->th.th_dispatch = &parent_team->t.t_dispatch[team->t.t_master_tid];
2495
2496 /* jc: The following lock has instructions with REL and ACQ semantics,
2497 separating the parallel user code called in this parallel region
2498 from the serial user code called after this function returns. */
2499 __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock);
2500
2501#if OMP_40_ENABLED
2502 if (!master_th->th.th_teams_microtask ||
2503 team->t.t_level > master_th->th.th_teams_level)
2504#endif /* OMP_40_ENABLED */
2505 {
2506 /* Decrement our nested depth level */
Jonathan Peyton37e2ef52018-07-09 17:36:22 +00002507 KMP_ATOMIC_DEC(&root->r.r_in_parallel);
Jonathan Peyton30419822017-05-12 18:01:32 +00002508 }
2509 KMP_DEBUG_ASSERT(root->r.r_in_parallel >= 0);
2510
Joachim Protze82e94a52017-11-01 10:08:30 +00002511#if OMPT_SUPPORT
2512 if (ompt_enabled.enabled) {
2513 ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
2514 if (ompt_enabled.ompt_callback_implicit_task) {
2515 int ompt_team_size = team->t.t_nproc;
2516 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
2517 ompt_scope_end, NULL, &(task_info->task_data), ompt_team_size,
Joachim Protze9be9cf22018-05-07 12:42:21 +00002518 OMPT_CUR_TASK_INFO(master_th)->thread_num);
Jonathan Peyton30419822017-05-12 18:01:32 +00002519 }
Joachim Protze82e94a52017-11-01 10:08:30 +00002520
Joachim Protzec255ca72017-11-05 14:11:10 +00002521 task_info->frame.exit_frame = NULL;
Joachim Protze82e94a52017-11-01 10:08:30 +00002522 task_info->task_data = ompt_data_none;
Jonathan Peyton30419822017-05-12 18:01:32 +00002523 }
2524#endif
2525
2526 KF_TRACE(10, ("__kmp_join_call1: T#%d, this_thread=%p team=%p\n", 0,
2527 master_th, team));
2528 __kmp_pop_current_task_from_thread(master_th);
2529
2530#if OMP_40_ENABLED && KMP_AFFINITY_SUPPORTED
2531 // Restore master thread's partition.
2532 master_th->th.th_first_place = team->t.t_first_place;
2533 master_th->th.th_last_place = team->t.t_last_place;
2534#endif /* OMP_40_ENABLED */
Jonathan Peyton92ca6182018-09-07 18:25:49 +00002535#if OMP_50_ENABLED
2536 master_th->th.th_def_allocator = team->t.t_def_allocator;
2537#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00002538
2539 updateHWFPControl(team);
2540
2541 if (root->r.r_active != master_active)
2542 root->r.r_active = master_active;
2543
2544 __kmp_free_team(root, team USE_NESTED_HOT_ARG(
2545 master_th)); // this will free worker threads
2546
2547 /* this race was fun to find. make sure the following is in the critical
2548 region otherwise assertions may fail occasionally since the old team may be
2549 reallocated and the hierarchy appears inconsistent. it is actually safe to
2550 run and won't cause any bugs, but will cause those assertion failures. it's
2551 only one deref&assign so might as well put this in the critical region */
2552 master_th->th.th_team = parent_team;
2553 master_th->th.th_team_nproc = parent_team->t.t_nproc;
2554 master_th->th.th_team_master = parent_team->t.t_threads[0];
2555 master_th->th.th_team_serialized = parent_team->t.t_serialized;
2556
2557 /* restore serialized team, if need be */
2558 if (parent_team->t.t_serialized &&
2559 parent_team != master_th->th.th_serial_team &&
2560 parent_team != root->r.r_root_team) {
2561 __kmp_free_team(root,
2562 master_th->th.th_serial_team USE_NESTED_HOT_ARG(NULL));
2563 master_th->th.th_serial_team = parent_team;
2564 }
2565
2566 if (__kmp_tasking_mode != tskm_immediate_exec) {
2567 if (master_th->th.th_task_state_top >
2568 0) { // Restore task state from memo stack
2569 KMP_DEBUG_ASSERT(master_th->th.th_task_state_memo_stack);
2570 // Remember master's state if we re-use this nested hot team
2571 master_th->th.th_task_state_memo_stack[master_th->th.th_task_state_top] =
2572 master_th->th.th_task_state;
2573 --master_th->th.th_task_state_top; // pop
2574 // Now restore state at this level
2575 master_th->th.th_task_state =
2576 master_th->th
2577 .th_task_state_memo_stack[master_th->th.th_task_state_top];
2578 }
2579 // Copy the task team from the parent team to the master thread
2580 master_th->th.th_task_team =
2581 parent_team->t.t_task_team[master_th->th.th_task_state];
2582 KA_TRACE(20,
2583 ("__kmp_join_call: Master T#%d restoring task_team %p / team %p\n",
2584 __kmp_gtid_from_thread(master_th), master_th->th.th_task_team,
2585 parent_team));
2586 }
2587
2588 // TODO: GEH - cannot do this assertion because root thread not set up as
2589 // executing
2590 // KMP_ASSERT( master_th->th.th_current_task->td_flags.executing == 0 );
2591 master_th->th.th_current_task->td_flags.executing = 1;
2592
2593 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
2594
2595#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00002596 if (ompt_enabled.enabled) {
2597 __kmp_join_ompt(gtid, master_th, parent_team, parallel_data, fork_context,
2598 codeptr);
Jonathan Peyton30419822017-05-12 18:01:32 +00002599 }
2600#endif
2601
2602 KMP_MB();
2603 KA_TRACE(20, ("__kmp_join_call: exit T#%d\n", gtid));
2604}
Jim Cownie5e8470a2013-09-27 10:38:44 +00002605
2606/* Check whether we should push an internal control record onto the
2607 serial team stack. If so, do it. */
Jonathan Peyton30419822017-05-12 18:01:32 +00002608void __kmp_save_internal_controls(kmp_info_t *thread) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00002609
Jonathan Peyton30419822017-05-12 18:01:32 +00002610 if (thread->th.th_team != thread->th.th_serial_team) {
2611 return;
2612 }
2613 if (thread->th.th_team->t.t_serialized > 1) {
2614 int push = 0;
2615
2616 if (thread->th.th_team->t.t_control_stack_top == NULL) {
2617 push = 1;
2618 } else {
2619 if (thread->th.th_team->t.t_control_stack_top->serial_nesting_level !=
2620 thread->th.th_team->t.t_serialized) {
2621 push = 1;
2622 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002623 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002624 if (push) { /* push a record on the serial team's stack */
2625 kmp_internal_control_t *control =
2626 (kmp_internal_control_t *)__kmp_allocate(
2627 sizeof(kmp_internal_control_t));
Jim Cownie5e8470a2013-09-27 10:38:44 +00002628
Jonathan Peyton30419822017-05-12 18:01:32 +00002629 copy_icvs(control, &thread->th.th_current_task->td_icvs);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002630
Jonathan Peyton30419822017-05-12 18:01:32 +00002631 control->serial_nesting_level = thread->th.th_team->t.t_serialized;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002632
Jonathan Peyton30419822017-05-12 18:01:32 +00002633 control->next = thread->th.th_team->t.t_control_stack_top;
2634 thread->th.th_team->t.t_control_stack_top = control;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002635 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002636 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002637}
2638
2639/* Changes set_nproc */
Jonathan Peyton30419822017-05-12 18:01:32 +00002640void __kmp_set_num_threads(int new_nth, int gtid) {
2641 kmp_info_t *thread;
2642 kmp_root_t *root;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002643
Jonathan Peyton30419822017-05-12 18:01:32 +00002644 KF_TRACE(10, ("__kmp_set_num_threads: new __kmp_nth = %d\n", new_nth));
2645 KMP_DEBUG_ASSERT(__kmp_init_serial);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002646
Jonathan Peyton30419822017-05-12 18:01:32 +00002647 if (new_nth < 1)
2648 new_nth = 1;
2649 else if (new_nth > __kmp_max_nth)
2650 new_nth = __kmp_max_nth;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002651
Jonathan Peyton30419822017-05-12 18:01:32 +00002652 KMP_COUNT_VALUE(OMP_set_numthreads, new_nth);
2653 thread = __kmp_threads[gtid];
Jim Cownie5e8470a2013-09-27 10:38:44 +00002654
Jonathan Peyton30419822017-05-12 18:01:32 +00002655 __kmp_save_internal_controls(thread);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002656
Jonathan Peyton30419822017-05-12 18:01:32 +00002657 set__nproc(thread, new_nth);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002658
Jonathan Peyton30419822017-05-12 18:01:32 +00002659 // If this omp_set_num_threads() call will cause the hot team size to be
2660 // reduced (in the absence of a num_threads clause), then reduce it now,
2661 // rather than waiting for the next parallel region.
2662 root = thread->th.th_root;
2663 if (__kmp_init_parallel && (!root->r.r_active) &&
2664 (root->r.r_hot_team->t.t_nproc > new_nth)
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002665#if KMP_NESTED_HOT_TEAMS
2666 && __kmp_hot_teams_max_level && !__kmp_hot_teams_mode
2667#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00002668 ) {
2669 kmp_team_t *hot_team = root->r.r_hot_team;
2670 int f;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002671
Jonathan Peyton30419822017-05-12 18:01:32 +00002672 __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002673
Jonathan Peyton30419822017-05-12 18:01:32 +00002674 // Release the extra threads we don't need any more.
2675 for (f = new_nth; f < hot_team->t.t_nproc; f++) {
2676 KMP_DEBUG_ASSERT(hot_team->t.t_threads[f] != NULL);
2677 if (__kmp_tasking_mode != tskm_immediate_exec) {
2678 // When decreasing team size, threads no longer in the team should unref
2679 // task team.
2680 hot_team->t.t_threads[f]->th.th_task_team = NULL;
2681 }
2682 __kmp_free_thread(hot_team->t.t_threads[f]);
2683 hot_team->t.t_threads[f] = NULL;
2684 }
2685 hot_team->t.t_nproc = new_nth;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002686#if KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00002687 if (thread->th.th_hot_teams) {
2688 KMP_DEBUG_ASSERT(hot_team == thread->th.th_hot_teams[0].hot_team);
2689 thread->th.th_hot_teams[0].hot_team_nth = new_nth;
2690 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002691#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00002692
Jonathan Peyton30419822017-05-12 18:01:32 +00002693 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002694
Jonathan Peyton30419822017-05-12 18:01:32 +00002695 // Update the t_nproc field in the threads that are still active.
2696 for (f = 0; f < new_nth; f++) {
2697 KMP_DEBUG_ASSERT(hot_team->t.t_threads[f] != NULL);
2698 hot_team->t.t_threads[f]->th.th_team_nproc = new_nth;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002699 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002700 // Special flag in case omp_set_num_threads() call
2701 hot_team->t.t_size_changed = -1;
2702 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002703}
2704
Jim Cownie5e8470a2013-09-27 10:38:44 +00002705/* Changes max_active_levels */
Jonathan Peyton30419822017-05-12 18:01:32 +00002706void __kmp_set_max_active_levels(int gtid, int max_active_levels) {
2707 kmp_info_t *thread;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002708
Jonathan Peyton30419822017-05-12 18:01:32 +00002709 KF_TRACE(10, ("__kmp_set_max_active_levels: new max_active_levels for thread "
2710 "%d = (%d)\n",
2711 gtid, max_active_levels));
2712 KMP_DEBUG_ASSERT(__kmp_init_serial);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002713
Jonathan Peyton30419822017-05-12 18:01:32 +00002714 // validate max_active_levels
2715 if (max_active_levels < 0) {
2716 KMP_WARNING(ActiveLevelsNegative, max_active_levels);
2717 // We ignore this call if the user has specified a negative value.
2718 // The current setting won't be changed. The last valid setting will be
2719 // used. A warning will be issued (if warnings are allowed as controlled by
2720 // the KMP_WARNINGS env var).
2721 KF_TRACE(10, ("__kmp_set_max_active_levels: the call is ignored: new "
2722 "max_active_levels for thread %d = (%d)\n",
2723 gtid, max_active_levels));
2724 return;
2725 }
2726 if (max_active_levels <= KMP_MAX_ACTIVE_LEVELS_LIMIT) {
2727 // it's OK, the max_active_levels is within the valid range: [ 0;
2728 // KMP_MAX_ACTIVE_LEVELS_LIMIT ]
2729 // We allow a zero value. (implementation defined behavior)
2730 } else {
2731 KMP_WARNING(ActiveLevelsExceedLimit, max_active_levels,
2732 KMP_MAX_ACTIVE_LEVELS_LIMIT);
2733 max_active_levels = KMP_MAX_ACTIVE_LEVELS_LIMIT;
2734 // Current upper limit is MAX_INT. (implementation defined behavior)
2735 // If the input exceeds the upper limit, we correct the input to be the
2736 // upper limit. (implementation defined behavior)
2737 // Actually, the flow should never get here until we use MAX_INT limit.
2738 }
2739 KF_TRACE(10, ("__kmp_set_max_active_levels: after validation: new "
2740 "max_active_levels for thread %d = (%d)\n",
2741 gtid, max_active_levels));
Jim Cownie5e8470a2013-09-27 10:38:44 +00002742
Jonathan Peyton30419822017-05-12 18:01:32 +00002743 thread = __kmp_threads[gtid];
Jim Cownie5e8470a2013-09-27 10:38:44 +00002744
Jonathan Peyton30419822017-05-12 18:01:32 +00002745 __kmp_save_internal_controls(thread);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002746
Jonathan Peyton30419822017-05-12 18:01:32 +00002747 set__max_active_levels(thread, max_active_levels);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002748}
2749
2750/* Gets max_active_levels */
Jonathan Peyton30419822017-05-12 18:01:32 +00002751int __kmp_get_max_active_levels(int gtid) {
2752 kmp_info_t *thread;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002753
Jonathan Peyton30419822017-05-12 18:01:32 +00002754 KF_TRACE(10, ("__kmp_get_max_active_levels: thread %d\n", gtid));
2755 KMP_DEBUG_ASSERT(__kmp_init_serial);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002756
Jonathan Peyton30419822017-05-12 18:01:32 +00002757 thread = __kmp_threads[gtid];
2758 KMP_DEBUG_ASSERT(thread->th.th_current_task);
2759 KF_TRACE(10, ("__kmp_get_max_active_levels: thread %d, curtask=%p, "
2760 "curtask_maxaclevel=%d\n",
2761 gtid, thread->th.th_current_task,
2762 thread->th.th_current_task->td_icvs.max_active_levels));
2763 return thread->th.th_current_task->td_icvs.max_active_levels;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002764}
2765
2766/* Changes def_sched_var ICV values (run-time schedule kind and chunk) */
Jonathan Peyton30419822017-05-12 18:01:32 +00002767void __kmp_set_schedule(int gtid, kmp_sched_t kind, int chunk) {
2768 kmp_info_t *thread;
2769 // kmp_team_t *team;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002770
Jonathan Peyton30419822017-05-12 18:01:32 +00002771 KF_TRACE(10, ("__kmp_set_schedule: new schedule for thread %d = (%d, %d)\n",
2772 gtid, (int)kind, chunk));
2773 KMP_DEBUG_ASSERT(__kmp_init_serial);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002774
Jonathan Peyton30419822017-05-12 18:01:32 +00002775 // Check if the kind parameter is valid, correct if needed.
2776 // Valid parameters should fit in one of two intervals - standard or extended:
2777 // <lower>, <valid>, <upper_std>, <lower_ext>, <valid>, <upper>
2778 // 2008-01-25: 0, 1 - 4, 5, 100, 101 - 102, 103
2779 if (kind <= kmp_sched_lower || kind >= kmp_sched_upper ||
2780 (kind <= kmp_sched_lower_ext && kind >= kmp_sched_upper_std)) {
2781 // TODO: Hint needs attention in case we change the default schedule.
2782 __kmp_msg(kmp_ms_warning, KMP_MSG(ScheduleKindOutOfRange, kind),
2783 KMP_HNT(DefaultScheduleKindUsed, "static, no chunk"),
2784 __kmp_msg_null);
2785 kind = kmp_sched_default;
2786 chunk = 0; // ignore chunk value in case of bad kind
2787 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002788
Jonathan Peyton30419822017-05-12 18:01:32 +00002789 thread = __kmp_threads[gtid];
Jim Cownie5e8470a2013-09-27 10:38:44 +00002790
Jonathan Peyton30419822017-05-12 18:01:32 +00002791 __kmp_save_internal_controls(thread);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002792
Jonathan Peyton30419822017-05-12 18:01:32 +00002793 if (kind < kmp_sched_upper_std) {
2794 if (kind == kmp_sched_static && chunk < KMP_DEFAULT_CHUNK) {
2795 // differ static chunked vs. unchunked: chunk should be invalid to
2796 // indicate unchunked schedule (which is the default)
2797 thread->th.th_current_task->td_icvs.sched.r_sched_type = kmp_sch_static;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002798 } else {
Jonathan Peyton30419822017-05-12 18:01:32 +00002799 thread->th.th_current_task->td_icvs.sched.r_sched_type =
2800 __kmp_sch_map[kind - kmp_sched_lower - 1];
Jim Cownie5e8470a2013-09-27 10:38:44 +00002801 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002802 } else {
2803 // __kmp_sch_map[ kind - kmp_sched_lower_ext + kmp_sched_upper_std -
2804 // kmp_sched_lower - 2 ];
2805 thread->th.th_current_task->td_icvs.sched.r_sched_type =
2806 __kmp_sch_map[kind - kmp_sched_lower_ext + kmp_sched_upper_std -
2807 kmp_sched_lower - 2];
2808 }
Andrey Churbanovd454c732017-06-05 17:17:33 +00002809 if (kind == kmp_sched_auto || chunk < 1) {
Jonathan Peyton30419822017-05-12 18:01:32 +00002810 // ignore parameter chunk for schedule auto
2811 thread->th.th_current_task->td_icvs.sched.chunk = KMP_DEFAULT_CHUNK;
2812 } else {
2813 thread->th.th_current_task->td_icvs.sched.chunk = chunk;
2814 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002815}
2816
2817/* Gets def_sched_var ICV values */
Jonathan Peyton30419822017-05-12 18:01:32 +00002818void __kmp_get_schedule(int gtid, kmp_sched_t *kind, int *chunk) {
2819 kmp_info_t *thread;
2820 enum sched_type th_type;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002821
Jonathan Peyton30419822017-05-12 18:01:32 +00002822 KF_TRACE(10, ("__kmp_get_schedule: thread %d\n", gtid));
2823 KMP_DEBUG_ASSERT(__kmp_init_serial);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002824
Jonathan Peyton30419822017-05-12 18:01:32 +00002825 thread = __kmp_threads[gtid];
Jim Cownie5e8470a2013-09-27 10:38:44 +00002826
Jonathan Peyton30419822017-05-12 18:01:32 +00002827 th_type = thread->th.th_current_task->td_icvs.sched.r_sched_type;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002828
Jonathan Peyton30419822017-05-12 18:01:32 +00002829 switch (th_type) {
2830 case kmp_sch_static:
2831 case kmp_sch_static_greedy:
2832 case kmp_sch_static_balanced:
2833 *kind = kmp_sched_static;
2834 *chunk = 0; // chunk was not set, try to show this fact via zero value
2835 return;
2836 case kmp_sch_static_chunked:
2837 *kind = kmp_sched_static;
2838 break;
2839 case kmp_sch_dynamic_chunked:
2840 *kind = kmp_sched_dynamic;
2841 break;
2842 case kmp_sch_guided_chunked:
2843 case kmp_sch_guided_iterative_chunked:
2844 case kmp_sch_guided_analytical_chunked:
2845 *kind = kmp_sched_guided;
2846 break;
2847 case kmp_sch_auto:
2848 *kind = kmp_sched_auto;
2849 break;
2850 case kmp_sch_trapezoidal:
2851 *kind = kmp_sched_trapezoidal;
2852 break;
Jonathan Peytona1234cf2016-10-07 18:01:35 +00002853#if KMP_STATIC_STEAL_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00002854 case kmp_sch_static_steal:
2855 *kind = kmp_sched_static_steal;
2856 break;
Jonathan Peytona1234cf2016-10-07 18:01:35 +00002857#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00002858 default:
2859 KMP_FATAL(UnknownSchedulingType, th_type);
2860 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002861
Jonathan Peyton30419822017-05-12 18:01:32 +00002862 *chunk = thread->th.th_current_task->td_icvs.sched.chunk;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002863}
2864
Jonathan Peyton30419822017-05-12 18:01:32 +00002865int __kmp_get_ancestor_thread_num(int gtid, int level) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00002866
Jonathan Peyton30419822017-05-12 18:01:32 +00002867 int ii, dd;
2868 kmp_team_t *team;
2869 kmp_info_t *thr;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002870
Jonathan Peyton30419822017-05-12 18:01:32 +00002871 KF_TRACE(10, ("__kmp_get_ancestor_thread_num: thread %d %d\n", gtid, level));
2872 KMP_DEBUG_ASSERT(__kmp_init_serial);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002873
Jonathan Peyton30419822017-05-12 18:01:32 +00002874 // validate level
2875 if (level == 0)
2876 return 0;
2877 if (level < 0)
2878 return -1;
2879 thr = __kmp_threads[gtid];
2880 team = thr->th.th_team;
2881 ii = team->t.t_level;
2882 if (level > ii)
2883 return -1;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002884
2885#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00002886 if (thr->th.th_teams_microtask) {
2887 // AC: we are in teams region where multiple nested teams have same level
2888 int tlevel = thr->th.th_teams_level; // the level of the teams construct
2889 if (level <=
2890 tlevel) { // otherwise usual algorithm works (will not touch the teams)
2891 KMP_DEBUG_ASSERT(ii >= tlevel);
2892 // AC: As we need to pass by the teams league, we need to artificially
2893 // increase ii
2894 if (ii == tlevel) {
2895 ii += 2; // three teams have same level
2896 } else {
2897 ii++; // two teams have same level
2898 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002899 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002900 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002901#endif
2902
Jonathan Peyton30419822017-05-12 18:01:32 +00002903 if (ii == level)
2904 return __kmp_tid_from_gtid(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002905
Jonathan Peyton30419822017-05-12 18:01:32 +00002906 dd = team->t.t_serialized;
2907 level++;
2908 while (ii > level) {
2909 for (dd = team->t.t_serialized; (dd > 0) && (ii > level); dd--, ii--) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00002910 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002911 if ((team->t.t_serialized) && (!dd)) {
2912 team = team->t.t_parent;
2913 continue;
2914 }
2915 if (ii > level) {
2916 team = team->t.t_parent;
2917 dd = team->t.t_serialized;
2918 ii--;
2919 }
2920 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002921
Jonathan Peyton30419822017-05-12 18:01:32 +00002922 return (dd > 1) ? (0) : (team->t.t_master_tid);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002923}
2924
Jonathan Peyton30419822017-05-12 18:01:32 +00002925int __kmp_get_team_size(int gtid, int level) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00002926
Jonathan Peyton30419822017-05-12 18:01:32 +00002927 int ii, dd;
2928 kmp_team_t *team;
2929 kmp_info_t *thr;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002930
Jonathan Peyton30419822017-05-12 18:01:32 +00002931 KF_TRACE(10, ("__kmp_get_team_size: thread %d %d\n", gtid, level));
2932 KMP_DEBUG_ASSERT(__kmp_init_serial);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002933
Jonathan Peyton30419822017-05-12 18:01:32 +00002934 // validate level
2935 if (level == 0)
2936 return 1;
2937 if (level < 0)
2938 return -1;
2939 thr = __kmp_threads[gtid];
2940 team = thr->th.th_team;
2941 ii = team->t.t_level;
2942 if (level > ii)
2943 return -1;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002944
2945#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00002946 if (thr->th.th_teams_microtask) {
2947 // AC: we are in teams region where multiple nested teams have same level
2948 int tlevel = thr->th.th_teams_level; // the level of the teams construct
2949 if (level <=
2950 tlevel) { // otherwise usual algorithm works (will not touch the teams)
2951 KMP_DEBUG_ASSERT(ii >= tlevel);
2952 // AC: As we need to pass by the teams league, we need to artificially
2953 // increase ii
2954 if (ii == tlevel) {
2955 ii += 2; // three teams have same level
2956 } else {
2957 ii++; // two teams have same level
2958 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002959 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002960 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002961#endif
2962
Jonathan Peyton30419822017-05-12 18:01:32 +00002963 while (ii > level) {
2964 for (dd = team->t.t_serialized; (dd > 0) && (ii > level); dd--, ii--) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00002965 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002966 if (team->t.t_serialized && (!dd)) {
2967 team = team->t.t_parent;
2968 continue;
2969 }
2970 if (ii > level) {
2971 team = team->t.t_parent;
2972 ii--;
2973 }
2974 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002975
Jonathan Peyton30419822017-05-12 18:01:32 +00002976 return team->t.t_nproc;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002977}
2978
Jonathan Peyton30419822017-05-12 18:01:32 +00002979kmp_r_sched_t __kmp_get_schedule_global() {
2980 // This routine created because pairs (__kmp_sched, __kmp_chunk) and
2981 // (__kmp_static, __kmp_guided) may be changed by kmp_set_defaults
2982 // independently. So one can get the updated schedule here.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002983
Jonathan Peyton30419822017-05-12 18:01:32 +00002984 kmp_r_sched_t r_sched;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002985
Jonathan Peyton30419822017-05-12 18:01:32 +00002986 // create schedule from 4 globals: __kmp_sched, __kmp_chunk, __kmp_static,
2987 // __kmp_guided. __kmp_sched should keep original value, so that user can set
2988 // KMP_SCHEDULE multiple times, and thus have different run-time schedules in
2989 // different roots (even in OMP 2.5)
2990 if (__kmp_sched == kmp_sch_static) {
Jonathan Peytonba55a7b2017-11-29 22:47:52 +00002991 // replace STATIC with more detailed schedule (balanced or greedy)
2992 r_sched.r_sched_type = __kmp_static;
Jonathan Peyton30419822017-05-12 18:01:32 +00002993 } else if (__kmp_sched == kmp_sch_guided_chunked) {
Jonathan Peytonba55a7b2017-11-29 22:47:52 +00002994 // replace GUIDED with more detailed schedule (iterative or analytical)
2995 r_sched.r_sched_type = __kmp_guided;
2996 } else { // (STATIC_CHUNKED), or (DYNAMIC_CHUNKED), or other
2997 r_sched.r_sched_type = __kmp_sched;
Jonathan Peyton30419822017-05-12 18:01:32 +00002998 }
2999
Jonathan Peytonba55a7b2017-11-29 22:47:52 +00003000 if (__kmp_chunk < KMP_DEFAULT_CHUNK) {
3001 // __kmp_chunk may be wrong here (if it was not ever set)
Jonathan Peyton30419822017-05-12 18:01:32 +00003002 r_sched.chunk = KMP_DEFAULT_CHUNK;
3003 } else {
3004 r_sched.chunk = __kmp_chunk;
3005 }
3006
3007 return r_sched;
3008}
3009
3010/* Allocate (realloc == FALSE) * or reallocate (realloc == TRUE)
3011 at least argc number of *t_argv entries for the requested team. */
3012static void __kmp_alloc_argv_entries(int argc, kmp_team_t *team, int realloc) {
3013
3014 KMP_DEBUG_ASSERT(team);
3015 if (!realloc || argc > team->t.t_max_argc) {
3016
3017 KA_TRACE(100, ("__kmp_alloc_argv_entries: team %d: needed entries=%d, "
3018 "current entries=%d\n",
3019 team->t.t_id, argc, (realloc) ? team->t.t_max_argc : 0));
3020 /* if previously allocated heap space for args, free them */
3021 if (realloc && team->t.t_argv != &team->t.t_inline_argv[0])
3022 __kmp_free((void *)team->t.t_argv);
3023
3024 if (argc <= KMP_INLINE_ARGV_ENTRIES) {
3025 /* use unused space in the cache line for arguments */
3026 team->t.t_max_argc = KMP_INLINE_ARGV_ENTRIES;
3027 KA_TRACE(100, ("__kmp_alloc_argv_entries: team %d: inline allocate %d "
3028 "argv entries\n",
3029 team->t.t_id, team->t.t_max_argc));
3030 team->t.t_argv = &team->t.t_inline_argv[0];
3031 if (__kmp_storage_map) {
3032 __kmp_print_storage_map_gtid(
3033 -1, &team->t.t_inline_argv[0],
3034 &team->t.t_inline_argv[KMP_INLINE_ARGV_ENTRIES],
3035 (sizeof(void *) * KMP_INLINE_ARGV_ENTRIES), "team_%d.t_inline_argv",
3036 team->t.t_id);
3037 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003038 } else {
Jonathan Peyton30419822017-05-12 18:01:32 +00003039 /* allocate space for arguments in the heap */
3040 team->t.t_max_argc = (argc <= (KMP_MIN_MALLOC_ARGV_ENTRIES >> 1))
3041 ? KMP_MIN_MALLOC_ARGV_ENTRIES
3042 : 2 * argc;
3043 KA_TRACE(100, ("__kmp_alloc_argv_entries: team %d: dynamic allocate %d "
3044 "argv entries\n",
3045 team->t.t_id, team->t.t_max_argc));
3046 team->t.t_argv =
3047 (void **)__kmp_page_allocate(sizeof(void *) * team->t.t_max_argc);
3048 if (__kmp_storage_map) {
3049 __kmp_print_storage_map_gtid(-1, &team->t.t_argv[0],
3050 &team->t.t_argv[team->t.t_max_argc],
3051 sizeof(void *) * team->t.t_max_argc,
3052 "team_%d.t_argv", team->t.t_id);
3053 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003054 }
Jonathan Peyton30419822017-05-12 18:01:32 +00003055 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003056}
3057
Jonathan Peyton30419822017-05-12 18:01:32 +00003058static void __kmp_allocate_team_arrays(kmp_team_t *team, int max_nth) {
3059 int i;
3060 int num_disp_buff = max_nth > 1 ? __kmp_dispatch_num_buffers : 2;
3061 team->t.t_threads =
3062 (kmp_info_t **)__kmp_allocate(sizeof(kmp_info_t *) * max_nth);
3063 team->t.t_disp_buffer = (dispatch_shared_info_t *)__kmp_allocate(
3064 sizeof(dispatch_shared_info_t) * num_disp_buff);
3065 team->t.t_dispatch =
3066 (kmp_disp_t *)__kmp_allocate(sizeof(kmp_disp_t) * max_nth);
3067 team->t.t_implicit_task_taskdata =
3068 (kmp_taskdata_t *)__kmp_allocate(sizeof(kmp_taskdata_t) * max_nth);
3069 team->t.t_max_nproc = max_nth;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003070
Jonathan Peyton30419822017-05-12 18:01:32 +00003071 /* setup dispatch buffers */
3072 for (i = 0; i < num_disp_buff; ++i) {
3073 team->t.t_disp_buffer[i].buffer_index = i;
Jonathan Peytondf6818b2016-06-14 17:57:47 +00003074#if OMP_45_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00003075 team->t.t_disp_buffer[i].doacross_buf_idx = i;
Jonathan Peyton71909c52016-03-02 22:42:06 +00003076#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00003077 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003078}
3079
Jonathan Peyton30419822017-05-12 18:01:32 +00003080static void __kmp_free_team_arrays(kmp_team_t *team) {
3081 /* Note: this does not free the threads in t_threads (__kmp_free_threads) */
3082 int i;
3083 for (i = 0; i < team->t.t_max_nproc; ++i) {
3084 if (team->t.t_dispatch[i].th_disp_buffer != NULL) {
3085 __kmp_free(team->t.t_dispatch[i].th_disp_buffer);
3086 team->t.t_dispatch[i].th_disp_buffer = NULL;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003087 }
3088 }
Jonathan Peytonf6399362018-07-09 17:51:13 +00003089#if KMP_USE_HIER_SCHED
3090 __kmp_dispatch_free_hierarchies(team);
3091#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00003092 __kmp_free(team->t.t_threads);
3093 __kmp_free(team->t.t_disp_buffer);
3094 __kmp_free(team->t.t_dispatch);
3095 __kmp_free(team->t.t_implicit_task_taskdata);
3096 team->t.t_threads = NULL;
3097 team->t.t_disp_buffer = NULL;
3098 team->t.t_dispatch = NULL;
3099 team->t.t_implicit_task_taskdata = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003100}
3101
Jonathan Peyton30419822017-05-12 18:01:32 +00003102static void __kmp_reallocate_team_arrays(kmp_team_t *team, int max_nth) {
3103 kmp_info_t **oldThreads = team->t.t_threads;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003104
Jonathan Peyton30419822017-05-12 18:01:32 +00003105 __kmp_free(team->t.t_disp_buffer);
3106 __kmp_free(team->t.t_dispatch);
3107 __kmp_free(team->t.t_implicit_task_taskdata);
3108 __kmp_allocate_team_arrays(team, max_nth);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003109
Jonathan Peyton30419822017-05-12 18:01:32 +00003110 KMP_MEMCPY(team->t.t_threads, oldThreads,
3111 team->t.t_nproc * sizeof(kmp_info_t *));
Jim Cownie5e8470a2013-09-27 10:38:44 +00003112
Jonathan Peyton30419822017-05-12 18:01:32 +00003113 __kmp_free(oldThreads);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003114}
3115
Jonathan Peyton30419822017-05-12 18:01:32 +00003116static kmp_internal_control_t __kmp_get_global_icvs(void) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00003117
Jonathan Peyton30419822017-05-12 18:01:32 +00003118 kmp_r_sched_t r_sched =
3119 __kmp_get_schedule_global(); // get current state of scheduling globals
Jim Cownie5e8470a2013-09-27 10:38:44 +00003120
3121#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00003122 KMP_DEBUG_ASSERT(__kmp_nested_proc_bind.used > 0);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003123#endif /* OMP_40_ENABLED */
3124
Jonathan Peyton30419822017-05-12 18:01:32 +00003125 kmp_internal_control_t g_icvs = {
3126 0, // int serial_nesting_level; //corresponds to value of th_team_serialized
3127 (kmp_int8)__kmp_dflt_nested, // int nested; //internal control
3128 // for nested parallelism (per thread)
3129 (kmp_int8)__kmp_global.g.g_dynamic, // internal control for dynamic
3130 // adjustment of threads (per thread)
3131 (kmp_int8)__kmp_env_blocktime, // int bt_set; //internal control for
3132 // whether blocktime is explicitly set
3133 __kmp_dflt_blocktime, // int blocktime; //internal control for blocktime
Jonathan Peytone1c7c132016-10-07 18:12:19 +00003134#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +00003135 __kmp_bt_intervals, // int bt_intervals; //internal control for blocktime
3136// intervals
Jonathan Peytone1c7c132016-10-07 18:12:19 +00003137#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00003138 __kmp_dflt_team_nth, // int nproc; //internal control for # of threads for
3139 // next parallel region (per thread)
3140 // (use a max ub on value if __kmp_parallel_initialize not called yet)
3141 __kmp_dflt_max_active_levels, // int max_active_levels; //internal control
3142 // for max_active_levels
3143 r_sched, // kmp_r_sched_t sched; //internal control for runtime schedule
3144// {sched,chunk} pair
Jim Cownie5e8470a2013-09-27 10:38:44 +00003145#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00003146 __kmp_nested_proc_bind.bind_types[0],
3147 __kmp_default_device,
Jim Cownie5e8470a2013-09-27 10:38:44 +00003148#endif /* OMP_40_ENABLED */
Jonathan Peyton30419822017-05-12 18:01:32 +00003149 NULL // struct kmp_internal_control *next;
3150 };
Jim Cownie5e8470a2013-09-27 10:38:44 +00003151
Jonathan Peyton30419822017-05-12 18:01:32 +00003152 return g_icvs;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003153}
3154
Jonathan Peyton30419822017-05-12 18:01:32 +00003155static kmp_internal_control_t __kmp_get_x_global_icvs(const kmp_team_t *team) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00003156
Jonathan Peyton30419822017-05-12 18:01:32 +00003157 kmp_internal_control_t gx_icvs;
3158 gx_icvs.serial_nesting_level =
3159 0; // probably =team->t.t_serial like in save_inter_controls
3160 copy_icvs(&gx_icvs, &team->t.t_threads[0]->th.th_current_task->td_icvs);
3161 gx_icvs.next = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003162
Jonathan Peyton30419822017-05-12 18:01:32 +00003163 return gx_icvs;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003164}
3165
Jonathan Peyton30419822017-05-12 18:01:32 +00003166static void __kmp_initialize_root(kmp_root_t *root) {
3167 int f;
3168 kmp_team_t *root_team;
3169 kmp_team_t *hot_team;
3170 int hot_team_max_nth;
3171 kmp_r_sched_t r_sched =
3172 __kmp_get_schedule_global(); // get current state of scheduling globals
3173 kmp_internal_control_t r_icvs = __kmp_get_global_icvs();
3174 KMP_DEBUG_ASSERT(root);
3175 KMP_ASSERT(!root->r.r_begin);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003176
Jonathan Peyton30419822017-05-12 18:01:32 +00003177 /* setup the root state structure */
3178 __kmp_init_lock(&root->r.r_begin_lock);
3179 root->r.r_begin = FALSE;
3180 root->r.r_active = FALSE;
3181 root->r.r_in_parallel = 0;
3182 root->r.r_blocktime = __kmp_dflt_blocktime;
3183 root->r.r_nested = __kmp_dflt_nested;
Jonathan Peytonf4392462017-07-27 20:58:41 +00003184 root->r.r_cg_nthreads = 1;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003185
Jonathan Peyton30419822017-05-12 18:01:32 +00003186 /* setup the root team for this task */
3187 /* allocate the root team structure */
3188 KF_TRACE(10, ("__kmp_initialize_root: before root_team\n"));
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00003189
Jonathan Peyton30419822017-05-12 18:01:32 +00003190 root_team =
3191 __kmp_allocate_team(root,
3192 1, // new_nproc
3193 1, // max_nproc
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00003194#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00003195 ompt_data_none, // root parallel id
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00003196#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00003197#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00003198 __kmp_nested_proc_bind.bind_types[0],
Jim Cownie5e8470a2013-09-27 10:38:44 +00003199#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00003200 &r_icvs,
3201 0 // argc
3202 USE_NESTED_HOT_ARG(NULL) // master thread is unknown
3203 );
Jonathan Peyton8fbb49a2015-07-09 18:16:58 +00003204#if USE_DEBUGGER
Jonathan Peyton30419822017-05-12 18:01:32 +00003205 // Non-NULL value should be assigned to make the debugger display the root
3206 // team.
3207 TCW_SYNC_PTR(root_team->t.t_pkfn, (microtask_t)(~0));
Jonathan Peyton8fbb49a2015-07-09 18:16:58 +00003208#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00003209
Jonathan Peyton30419822017-05-12 18:01:32 +00003210 KF_TRACE(10, ("__kmp_initialize_root: after root_team = %p\n", root_team));
Jim Cownie5e8470a2013-09-27 10:38:44 +00003211
Jonathan Peyton30419822017-05-12 18:01:32 +00003212 root->r.r_root_team = root_team;
3213 root_team->t.t_control_stack_top = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003214
Jonathan Peyton30419822017-05-12 18:01:32 +00003215 /* initialize root team */
3216 root_team->t.t_threads[0] = NULL;
3217 root_team->t.t_nproc = 1;
3218 root_team->t.t_serialized = 1;
3219 // TODO???: root_team->t.t_max_active_levels = __kmp_dflt_max_active_levels;
Jonathan Peytonba55a7b2017-11-29 22:47:52 +00003220 root_team->t.t_sched.sched = r_sched.sched;
Jonathan Peyton30419822017-05-12 18:01:32 +00003221 KA_TRACE(
3222 20,
3223 ("__kmp_initialize_root: init root team %d arrived: join=%u, plain=%u\n",
3224 root_team->t.t_id, KMP_INIT_BARRIER_STATE, KMP_INIT_BARRIER_STATE));
Jim Cownie5e8470a2013-09-27 10:38:44 +00003225
Jonathan Peyton30419822017-05-12 18:01:32 +00003226 /* setup the hot team for this task */
3227 /* allocate the hot team structure */
3228 KF_TRACE(10, ("__kmp_initialize_root: before hot_team\n"));
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00003229
Jonathan Peyton30419822017-05-12 18:01:32 +00003230 hot_team =
3231 __kmp_allocate_team(root,
3232 1, // new_nproc
3233 __kmp_dflt_team_nth_ub * 2, // max_nproc
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00003234#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00003235 ompt_data_none, // root parallel id
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00003236#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00003237#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00003238 __kmp_nested_proc_bind.bind_types[0],
Jim Cownie5e8470a2013-09-27 10:38:44 +00003239#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00003240 &r_icvs,
3241 0 // argc
3242 USE_NESTED_HOT_ARG(NULL) // master thread is unknown
3243 );
3244 KF_TRACE(10, ("__kmp_initialize_root: after hot_team = %p\n", hot_team));
Jim Cownie5e8470a2013-09-27 10:38:44 +00003245
Jonathan Peyton30419822017-05-12 18:01:32 +00003246 root->r.r_hot_team = hot_team;
3247 root_team->t.t_control_stack_top = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003248
Jonathan Peyton30419822017-05-12 18:01:32 +00003249 /* first-time initialization */
3250 hot_team->t.t_parent = root_team;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003251
Jonathan Peyton30419822017-05-12 18:01:32 +00003252 /* initialize hot team */
3253 hot_team_max_nth = hot_team->t.t_max_nproc;
3254 for (f = 0; f < hot_team_max_nth; ++f) {
3255 hot_team->t.t_threads[f] = NULL;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003256 }
Jonathan Peyton30419822017-05-12 18:01:32 +00003257 hot_team->t.t_nproc = 1;
3258 // TODO???: hot_team->t.t_max_active_levels = __kmp_dflt_max_active_levels;
Jonathan Peytonba55a7b2017-11-29 22:47:52 +00003259 hot_team->t.t_sched.sched = r_sched.sched;
Jonathan Peyton30419822017-05-12 18:01:32 +00003260 hot_team->t.t_size_changed = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003261}
3262
3263#ifdef KMP_DEBUG
3264
Jim Cownie5e8470a2013-09-27 10:38:44 +00003265typedef struct kmp_team_list_item {
Jonathan Peyton30419822017-05-12 18:01:32 +00003266 kmp_team_p const *entry;
3267 struct kmp_team_list_item *next;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003268} kmp_team_list_item_t;
Jonathan Peyton30419822017-05-12 18:01:32 +00003269typedef kmp_team_list_item_t *kmp_team_list_t;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003270
Jonathan Peyton30419822017-05-12 18:01:32 +00003271static void __kmp_print_structure_team_accum( // Add team to list of teams.
3272 kmp_team_list_t list, // List of teams.
3273 kmp_team_p const *team // Team to add.
3274 ) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00003275
Jonathan Peyton30419822017-05-12 18:01:32 +00003276 // List must terminate with item where both entry and next are NULL.
3277 // Team is added to the list only once.
3278 // List is sorted in ascending order by team id.
3279 // Team id is *not* a key.
Jim Cownie5e8470a2013-09-27 10:38:44 +00003280
Jonathan Peyton30419822017-05-12 18:01:32 +00003281 kmp_team_list_t l;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003282
Jonathan Peyton30419822017-05-12 18:01:32 +00003283 KMP_DEBUG_ASSERT(list != NULL);
3284 if (team == NULL) {
3285 return;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003286 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003287
Jonathan Peyton30419822017-05-12 18:01:32 +00003288 __kmp_print_structure_team_accum(list, team->t.t_parent);
3289 __kmp_print_structure_team_accum(list, team->t.t_next_pool);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003290
Jonathan Peyton30419822017-05-12 18:01:32 +00003291 // Search list for the team.
3292 l = list;
3293 while (l->next != NULL && l->entry != team) {
3294 l = l->next;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003295 }
Jonathan Peyton30419822017-05-12 18:01:32 +00003296 if (l->next != NULL) {
3297 return; // Team has been added before, exit.
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003298 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003299
Jonathan Peyton30419822017-05-12 18:01:32 +00003300 // Team is not found. Search list again for insertion point.
3301 l = list;
3302 while (l->next != NULL && l->entry->t.t_id <= team->t.t_id) {
3303 l = l->next;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003304 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003305
Jonathan Peyton30419822017-05-12 18:01:32 +00003306 // Insert team.
3307 {
3308 kmp_team_list_item_t *item = (kmp_team_list_item_t *)KMP_INTERNAL_MALLOC(
3309 sizeof(kmp_team_list_item_t));
3310 *item = *l;
3311 l->entry = team;
3312 l->next = item;
3313 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003314}
3315
Jonathan Peyton30419822017-05-12 18:01:32 +00003316static void __kmp_print_structure_team(char const *title, kmp_team_p const *team
Jim Cownie5e8470a2013-09-27 10:38:44 +00003317
Jonathan Peyton30419822017-05-12 18:01:32 +00003318 ) {
3319 __kmp_printf("%s", title);
3320 if (team != NULL) {
3321 __kmp_printf("%2x %p\n", team->t.t_id, team);
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 +00003327static void __kmp_print_structure_thread(char const *title,
3328 kmp_info_p const *thread) {
3329 __kmp_printf("%s", title);
3330 if (thread != NULL) {
3331 __kmp_printf("%2d %p\n", thread->th.th_info.ds.ds_gtid, thread);
3332 } else {
3333 __kmp_printf(" - (nil)\n");
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003334 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003335}
3336
Jonathan Peyton30419822017-05-12 18:01:32 +00003337void __kmp_print_structure(void) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00003338
Jonathan Peyton30419822017-05-12 18:01:32 +00003339 kmp_team_list_t list;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003340
Jonathan Peyton30419822017-05-12 18:01:32 +00003341 // Initialize list of teams.
3342 list =
3343 (kmp_team_list_item_t *)KMP_INTERNAL_MALLOC(sizeof(kmp_team_list_item_t));
3344 list->entry = NULL;
3345 list->next = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003346
Jonathan Peyton30419822017-05-12 18:01:32 +00003347 __kmp_printf("\n------------------------------\nGlobal Thread "
3348 "Table\n------------------------------\n");
3349 {
3350 int gtid;
3351 for (gtid = 0; gtid < __kmp_threads_capacity; ++gtid) {
3352 __kmp_printf("%2d", gtid);
3353 if (__kmp_threads != NULL) {
3354 __kmp_printf(" %p", __kmp_threads[gtid]);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003355 }
Jonathan Peyton30419822017-05-12 18:01:32 +00003356 if (__kmp_root != NULL) {
3357 __kmp_printf(" %p", __kmp_root[gtid]);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003358 }
Jonathan Peyton30419822017-05-12 18:01:32 +00003359 __kmp_printf("\n");
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003360 }
Jonathan Peyton30419822017-05-12 18:01:32 +00003361 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003362
Jonathan Peyton30419822017-05-12 18:01:32 +00003363 // Print out __kmp_threads array.
3364 __kmp_printf("\n------------------------------\nThreads\n--------------------"
3365 "----------\n");
3366 if (__kmp_threads != NULL) {
3367 int gtid;
3368 for (gtid = 0; gtid < __kmp_threads_capacity; ++gtid) {
3369 kmp_info_t const *thread = __kmp_threads[gtid];
3370 if (thread != NULL) {
3371 __kmp_printf("GTID %2d %p:\n", gtid, thread);
3372 __kmp_printf(" Our Root: %p\n", thread->th.th_root);
3373 __kmp_print_structure_team(" Our Team: ", thread->th.th_team);
3374 __kmp_print_structure_team(" Serial Team: ",
3375 thread->th.th_serial_team);
3376 __kmp_printf(" Threads: %2d\n", thread->th.th_team_nproc);
3377 __kmp_print_structure_thread(" Master: ",
3378 thread->th.th_team_master);
3379 __kmp_printf(" Serialized?: %2d\n", thread->th.th_team_serialized);
3380 __kmp_printf(" Set NProc: %2d\n", thread->th.th_set_nproc);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003381#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00003382 __kmp_printf(" Set Proc Bind: %2d\n", thread->th.th_set_proc_bind);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003383#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00003384 __kmp_print_structure_thread(" Next in pool: ",
3385 thread->th.th_next_pool);
3386 __kmp_printf("\n");
3387 __kmp_print_structure_team_accum(list, thread->th.th_team);
3388 __kmp_print_structure_team_accum(list, thread->th.th_serial_team);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003389 }
3390 }
Jonathan Peyton30419822017-05-12 18:01:32 +00003391 } else {
3392 __kmp_printf("Threads array is not allocated.\n");
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003393 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003394
Jonathan Peyton30419822017-05-12 18:01:32 +00003395 // Print out __kmp_root array.
3396 __kmp_printf("\n------------------------------\nUbers\n----------------------"
3397 "--------\n");
3398 if (__kmp_root != NULL) {
3399 int gtid;
3400 for (gtid = 0; gtid < __kmp_threads_capacity; ++gtid) {
3401 kmp_root_t const *root = __kmp_root[gtid];
3402 if (root != NULL) {
3403 __kmp_printf("GTID %2d %p:\n", gtid, root);
3404 __kmp_print_structure_team(" Root Team: ", root->r.r_root_team);
3405 __kmp_print_structure_team(" Hot Team: ", root->r.r_hot_team);
3406 __kmp_print_structure_thread(" Uber Thread: ",
3407 root->r.r_uber_thread);
3408 __kmp_printf(" Active?: %2d\n", root->r.r_active);
3409 __kmp_printf(" Nested?: %2d\n", root->r.r_nested);
Jonathan Peyton61d44f12018-07-09 18:09:25 +00003410 __kmp_printf(" In Parallel: %2d\n",
3411 KMP_ATOMIC_LD_RLX(&root->r.r_in_parallel));
Jonathan Peyton30419822017-05-12 18:01:32 +00003412 __kmp_printf("\n");
3413 __kmp_print_structure_team_accum(list, root->r.r_root_team);
3414 __kmp_print_structure_team_accum(list, root->r.r_hot_team);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003415 }
3416 }
Jonathan Peyton30419822017-05-12 18:01:32 +00003417 } else {
3418 __kmp_printf("Ubers array is not allocated.\n");
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003419 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003420
Jonathan Peyton30419822017-05-12 18:01:32 +00003421 __kmp_printf("\n------------------------------\nTeams\n----------------------"
3422 "--------\n");
3423 while (list->next != NULL) {
3424 kmp_team_p const *team = list->entry;
3425 int i;
3426 __kmp_printf("Team %2x %p:\n", team->t.t_id, team);
3427 __kmp_print_structure_team(" Parent Team: ", team->t.t_parent);
3428 __kmp_printf(" Master TID: %2d\n", team->t.t_master_tid);
3429 __kmp_printf(" Max threads: %2d\n", team->t.t_max_nproc);
3430 __kmp_printf(" Levels of serial: %2d\n", team->t.t_serialized);
3431 __kmp_printf(" Number threads: %2d\n", team->t.t_nproc);
3432 for (i = 0; i < team->t.t_nproc; ++i) {
3433 __kmp_printf(" Thread %2d: ", i);
3434 __kmp_print_structure_thread("", team->t.t_threads[i]);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003435 }
Jonathan Peyton30419822017-05-12 18:01:32 +00003436 __kmp_print_structure_team(" Next in pool: ", team->t.t_next_pool);
3437 __kmp_printf("\n");
3438 list = list->next;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003439 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003440
Jonathan Peyton30419822017-05-12 18:01:32 +00003441 // Print out __kmp_thread_pool and __kmp_team_pool.
3442 __kmp_printf("\n------------------------------\nPools\n----------------------"
3443 "--------\n");
3444 __kmp_print_structure_thread("Thread pool: ",
Andrey Churbanovc47afcd2017-07-03 11:24:08 +00003445 CCAST(kmp_info_t *, __kmp_thread_pool));
Jonathan Peyton30419822017-05-12 18:01:32 +00003446 __kmp_print_structure_team("Team pool: ",
Andrey Churbanovc47afcd2017-07-03 11:24:08 +00003447 CCAST(kmp_team_t *, __kmp_team_pool));
Jonathan Peyton30419822017-05-12 18:01:32 +00003448 __kmp_printf("\n");
Jim Cownie5e8470a2013-09-27 10:38:44 +00003449
Jonathan Peyton30419822017-05-12 18:01:32 +00003450 // Free team list.
3451 while (list != NULL) {
3452 kmp_team_list_item_t *item = list;
3453 list = list->next;
3454 KMP_INTERNAL_FREE(item);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003455 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003456}
3457
3458#endif
3459
Jim Cownie5e8470a2013-09-27 10:38:44 +00003460//---------------------------------------------------------------------------
3461// Stuff for per-thread fast random number generator
3462// Table of primes
Jim Cownie5e8470a2013-09-27 10:38:44 +00003463static const unsigned __kmp_primes[] = {
Jonathan Peyton30419822017-05-12 18:01:32 +00003464 0x9e3779b1, 0xffe6cc59, 0x2109f6dd, 0x43977ab5, 0xba5703f5, 0xb495a877,
3465 0xe1626741, 0x79695e6b, 0xbc98c09f, 0xd5bee2b3, 0x287488f9, 0x3af18231,
3466 0x9677cd4d, 0xbe3a6929, 0xadc6a877, 0xdcf0674b, 0xbe4d6fe9, 0x5f15e201,
3467 0x99afc3fd, 0xf3f16801, 0xe222cfff, 0x24ba5fdb, 0x0620452d, 0x79f149e3,
3468 0xc8b93f49, 0x972702cd, 0xb07dd827, 0x6c97d5ed, 0x085a3d61, 0x46eb5ea7,
3469 0x3d9910ed, 0x2e687b5b, 0x29609227, 0x6eb081f1, 0x0954c4e1, 0x9d114db9,
3470 0x542acfa9, 0xb3e6bd7b, 0x0742d917, 0xe9f3ffa7, 0x54581edb, 0xf2480f45,
3471 0x0bb9288f, 0xef1affc7, 0x85fa0ca7, 0x3ccc14db, 0xe6baf34b, 0x343377f7,
3472 0x5ca19031, 0xe6d9293b, 0xf0a9f391, 0x5d2e980b, 0xfc411073, 0xc3749363,
3473 0xb892d829, 0x3549366b, 0x629750ad, 0xb98294e5, 0x892d9483, 0xc235baf3,
3474 0x3d2402a3, 0x6bdef3c9, 0xbec333cd, 0x40c9520f};
Jim Cownie5e8470a2013-09-27 10:38:44 +00003475
3476//---------------------------------------------------------------------------
3477// __kmp_get_random: Get a random number using a linear congruential method.
Jonathan Peyton30419822017-05-12 18:01:32 +00003478unsigned short __kmp_get_random(kmp_info_t *thread) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00003479 unsigned x = thread->th.th_x;
Jonathan Peyton30419822017-05-12 18:01:32 +00003480 unsigned short r = x >> 16;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003481
Jonathan Peyton30419822017-05-12 18:01:32 +00003482 thread->th.th_x = x * thread->th.th_a + 1;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003483
3484 KA_TRACE(30, ("__kmp_get_random: THREAD: %d, RETURN: %u\n",
Jonathan Peyton30419822017-05-12 18:01:32 +00003485 thread->th.th_info.ds.ds_tid, r));
Jim Cownie5e8470a2013-09-27 10:38:44 +00003486
3487 return r;
3488}
3489//--------------------------------------------------------
3490// __kmp_init_random: Initialize a random number generator
Jonathan Peyton30419822017-05-12 18:01:32 +00003491void __kmp_init_random(kmp_info_t *thread) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00003492 unsigned seed = thread->th.th_info.ds.ds_tid;
3493
Jonathan Peyton30419822017-05-12 18:01:32 +00003494 thread->th.th_a =
3495 __kmp_primes[seed % (sizeof(__kmp_primes) / sizeof(__kmp_primes[0]))];
3496 thread->th.th_x = (seed + 1) * thread->th.th_a + 1;
3497 KA_TRACE(30,
3498 ("__kmp_init_random: THREAD: %u; A: %u\n", seed, thread->th.th_a));
Jim Cownie5e8470a2013-09-27 10:38:44 +00003499}
3500
Jim Cownie5e8470a2013-09-27 10:38:44 +00003501#if KMP_OS_WINDOWS
Jonathan Peyton30419822017-05-12 18:01:32 +00003502/* reclaim array entries for root threads that are already dead, returns number
3503 * reclaimed */
3504static int __kmp_reclaim_dead_roots(void) {
3505 int i, r = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003506
Jonathan Peyton30419822017-05-12 18:01:32 +00003507 for (i = 0; i < __kmp_threads_capacity; ++i) {
3508 if (KMP_UBER_GTID(i) &&
3509 !__kmp_still_running((kmp_info_t *)TCR_SYNC_PTR(__kmp_threads[i])) &&
3510 !__kmp_root[i]
3511 ->r.r_active) { // AC: reclaim only roots died in non-active state
3512 r += __kmp_unregister_root_other_thread(i);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003513 }
Jonathan Peyton30419822017-05-12 18:01:32 +00003514 }
3515 return r;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003516}
3517#endif
3518
Jonathan Peyton30419822017-05-12 18:01:32 +00003519/* This function attempts to create free entries in __kmp_threads and
3520 __kmp_root, and returns the number of free entries generated.
Jim Cownie5e8470a2013-09-27 10:38:44 +00003521
Jonathan Peyton30419822017-05-12 18:01:32 +00003522 For Windows* OS static library, the first mechanism used is to reclaim array
3523 entries for root threads that are already dead.
Jim Cownie5e8470a2013-09-27 10:38:44 +00003524
Jonathan Peyton30419822017-05-12 18:01:32 +00003525 On all platforms, expansion is attempted on the arrays __kmp_threads_ and
3526 __kmp_root, with appropriate update to __kmp_threads_capacity. Array
3527 capacity is increased by doubling with clipping to __kmp_tp_capacity, if
3528 threadprivate cache array has been created. Synchronization with
3529 __kmpc_threadprivate_cached is done using __kmp_tp_cached_lock.
Jim Cownie5e8470a2013-09-27 10:38:44 +00003530
Jonathan Peyton30419822017-05-12 18:01:32 +00003531 After any dead root reclamation, if the clipping value allows array expansion
Jonathan Peyton1800ece2018-01-10 18:27:01 +00003532 to result in the generation of a total of nNeed free slots, the function does
3533 that expansion. If not, nothing is done beyond the possible initial root
3534 thread reclamation.
Jim Cownie5e8470a2013-09-27 10:38:44 +00003535
Jonathan Peyton30419822017-05-12 18:01:32 +00003536 If any argument is negative, the behavior is undefined. */
Jonathan Peyton1800ece2018-01-10 18:27:01 +00003537static int __kmp_expand_threads(int nNeed) {
Jonathan Peyton30419822017-05-12 18:01:32 +00003538 int added = 0;
Andrey Churbanov9e9333a2018-03-05 18:42:01 +00003539 int minimumRequiredCapacity;
3540 int newCapacity;
3541 kmp_info_t **newThreads;
3542 kmp_root_t **newRoot;
3543
3544// All calls to __kmp_expand_threads should be under __kmp_forkjoin_lock, so
3545// resizing __kmp_threads does not need additional protection if foreign
3546// threads are present
Jim Cownie5e8470a2013-09-27 10:38:44 +00003547
Jonathan Peyton99016992015-05-26 17:32:53 +00003548#if KMP_OS_WINDOWS && !defined KMP_DYNAMIC_LIB
Jonathan Peyton30419822017-05-12 18:01:32 +00003549 /* only for Windows static library */
3550 /* reclaim array entries for root threads that are already dead */
3551 added = __kmp_reclaim_dead_roots();
Jim Cownie5e8470a2013-09-27 10:38:44 +00003552
Jonathan Peyton30419822017-05-12 18:01:32 +00003553 if (nNeed) {
3554 nNeed -= added;
3555 if (nNeed < 0)
3556 nNeed = 0;
3557 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003558#endif
Jonathan Peyton1800ece2018-01-10 18:27:01 +00003559 if (nNeed <= 0)
Jim Cownie5e8470a2013-09-27 10:38:44 +00003560 return added;
Jonathan Peyton30419822017-05-12 18:01:32 +00003561
Andrey Churbanov9e9333a2018-03-05 18:42:01 +00003562 // Note that __kmp_threads_capacity is not bounded by __kmp_max_nth. If
3563 // __kmp_max_nth is set to some value less than __kmp_sys_max_nth by the
3564 // user via KMP_DEVICE_THREAD_LIMIT, then __kmp_threads_capacity may become
3565 // > __kmp_max_nth in one of two ways:
3566 //
3567 // 1) The initialization thread (gtid = 0) exits. __kmp_threads[0]
3568 // may not be resused by another thread, so we may need to increase
3569 // __kmp_threads_capacity to __kmp_max_nth + 1.
3570 //
3571 // 2) New foreign root(s) are encountered. We always register new foreign
3572 // roots. This may cause a smaller # of threads to be allocated at
3573 // subsequent parallel regions, but the worker threads hang around (and
3574 // eventually go to sleep) and need slots in the __kmp_threads[] array.
3575 //
3576 // Anyway, that is the reason for moving the check to see if
3577 // __kmp_max_nth was exceeded into __kmp_reserve_threads()
3578 // instead of having it performed here. -BB
Jonathan Peyton30419822017-05-12 18:01:32 +00003579
Andrey Churbanov9e9333a2018-03-05 18:42:01 +00003580 KMP_DEBUG_ASSERT(__kmp_sys_max_nth >= __kmp_threads_capacity);
Jonathan Peyton30419822017-05-12 18:01:32 +00003581
Andrey Churbanov9e9333a2018-03-05 18:42:01 +00003582 /* compute expansion headroom to check if we can expand */
3583 if (__kmp_sys_max_nth - __kmp_threads_capacity < nNeed) {
3584 /* possible expansion too small -- give up */
3585 return added;
Jonathan Peyton30419822017-05-12 18:01:32 +00003586 }
Andrey Churbanov9e9333a2018-03-05 18:42:01 +00003587 minimumRequiredCapacity = __kmp_threads_capacity + nNeed;
3588
3589 newCapacity = __kmp_threads_capacity;
3590 do {
3591 newCapacity = newCapacity <= (__kmp_sys_max_nth >> 1) ? (newCapacity << 1)
3592 : __kmp_sys_max_nth;
3593 } while (newCapacity < minimumRequiredCapacity);
3594 newThreads = (kmp_info_t **)__kmp_allocate(
3595 (sizeof(kmp_info_t *) + sizeof(kmp_root_t *)) * newCapacity + CACHE_LINE);
3596 newRoot =
3597 (kmp_root_t **)((char *)newThreads + sizeof(kmp_info_t *) * newCapacity);
3598 KMP_MEMCPY(newThreads, __kmp_threads,
3599 __kmp_threads_capacity * sizeof(kmp_info_t *));
3600 KMP_MEMCPY(newRoot, __kmp_root,
3601 __kmp_threads_capacity * sizeof(kmp_root_t *));
3602
3603 kmp_info_t **temp_threads = __kmp_threads;
3604 *(kmp_info_t * *volatile *)&__kmp_threads = newThreads;
3605 *(kmp_root_t * *volatile *)&__kmp_root = newRoot;
3606 __kmp_free(temp_threads);
3607 added += newCapacity - __kmp_threads_capacity;
3608 *(volatile int *)&__kmp_threads_capacity = newCapacity;
3609
3610 if (newCapacity > __kmp_tp_capacity) {
3611 __kmp_acquire_bootstrap_lock(&__kmp_tp_cached_lock);
3612 if (__kmp_tp_cached && newCapacity > __kmp_tp_capacity) {
3613 __kmp_threadprivate_resize_cache(newCapacity);
3614 } else { // increase __kmp_tp_capacity to correspond with kmp_threads size
3615 *(volatile int *)&__kmp_tp_capacity = newCapacity;
3616 }
3617 __kmp_release_bootstrap_lock(&__kmp_tp_cached_lock);
3618 }
3619
Jonathan Peyton30419822017-05-12 18:01:32 +00003620 return added;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003621}
3622
Jonathan Peyton30419822017-05-12 18:01:32 +00003623/* Register the current thread as a root thread and obtain our gtid. We must
3624 have the __kmp_initz_lock held at this point. Argument TRUE only if are the
3625 thread that calls from __kmp_do_serial_initialize() */
3626int __kmp_register_root(int initial_thread) {
3627 kmp_info_t *root_thread;
3628 kmp_root_t *root;
3629 int gtid;
3630 int capacity;
3631 __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock);
3632 KA_TRACE(20, ("__kmp_register_root: entered\n"));
3633 KMP_MB();
Jim Cownie5e8470a2013-09-27 10:38:44 +00003634
Jonathan Peyton30419822017-05-12 18:01:32 +00003635 /* 2007-03-02:
3636 If initial thread did not invoke OpenMP RTL yet, and this thread is not an
3637 initial one, "__kmp_all_nth >= __kmp_threads_capacity" condition does not
3638 work as expected -- it may return false (that means there is at least one
3639 empty slot in __kmp_threads array), but it is possible the only free slot
3640 is #0, which is reserved for initial thread and so cannot be used for this
3641 one. Following code workarounds this bug.
Jim Cownie5e8470a2013-09-27 10:38:44 +00003642
Jonathan Peyton30419822017-05-12 18:01:32 +00003643 However, right solution seems to be not reserving slot #0 for initial
3644 thread because:
3645 (1) there is no magic in slot #0,
3646 (2) we cannot detect initial thread reliably (the first thread which does
3647 serial initialization may be not a real initial thread).
3648 */
3649 capacity = __kmp_threads_capacity;
3650 if (!initial_thread && TCR_PTR(__kmp_threads[0]) == NULL) {
3651 --capacity;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003652 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003653
Jonathan Peyton30419822017-05-12 18:01:32 +00003654 /* see if there are too many threads */
Jonathan Peyton1800ece2018-01-10 18:27:01 +00003655 if (__kmp_all_nth >= capacity && !__kmp_expand_threads(1)) {
Jonathan Peyton30419822017-05-12 18:01:32 +00003656 if (__kmp_tp_cached) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +00003657 __kmp_fatal(KMP_MSG(CantRegisterNewThread),
3658 KMP_HNT(Set_ALL_THREADPRIVATE, __kmp_tp_capacity),
3659 KMP_HNT(PossibleSystemLimitOnThreads), __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +00003660 } else {
Jonathan Peyton6a393f72017-09-05 15:43:58 +00003661 __kmp_fatal(KMP_MSG(CantRegisterNewThread), KMP_HNT(SystemLimitOnThreads),
3662 __kmp_msg_null);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003663 }
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003664 }
Jonathan Peyton30419822017-05-12 18:01:32 +00003665
3666 /* find an available thread slot */
3667 /* Don't reassign the zero slot since we need that to only be used by initial
3668 thread */
3669 for (gtid = (initial_thread ? 0 : 1); TCR_PTR(__kmp_threads[gtid]) != NULL;
3670 gtid++)
3671 ;
3672 KA_TRACE(1,
3673 ("__kmp_register_root: found slot in threads array: T#%d\n", gtid));
3674 KMP_ASSERT(gtid < __kmp_threads_capacity);
3675
3676 /* update global accounting */
3677 __kmp_all_nth++;
3678 TCW_4(__kmp_nth, __kmp_nth + 1);
3679
3680 // if __kmp_adjust_gtid_mode is set, then we use method #1 (sp search) for low
3681 // numbers of procs, and method #2 (keyed API call) for higher numbers.
3682 if (__kmp_adjust_gtid_mode) {
3683 if (__kmp_all_nth >= __kmp_tls_gtid_min) {
3684 if (TCR_4(__kmp_gtid_mode) != 2) {
3685 TCW_4(__kmp_gtid_mode, 2);
3686 }
3687 } else {
3688 if (TCR_4(__kmp_gtid_mode) != 1) {
3689 TCW_4(__kmp_gtid_mode, 1);
3690 }
3691 }
3692 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003693
3694#ifdef KMP_ADJUST_BLOCKTIME
Jonathan Peyton30419822017-05-12 18:01:32 +00003695 /* Adjust blocktime to zero if necessary */
3696 /* Middle initialization might not have occurred yet */
3697 if (!__kmp_env_blocktime && (__kmp_avail_proc > 0)) {
3698 if (__kmp_nth > __kmp_avail_proc) {
3699 __kmp_zero_bt = TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003700 }
Jonathan Peyton30419822017-05-12 18:01:32 +00003701 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003702#endif /* KMP_ADJUST_BLOCKTIME */
3703
Jonathan Peyton30419822017-05-12 18:01:32 +00003704 /* setup this new hierarchy */
3705 if (!(root = __kmp_root[gtid])) {
3706 root = __kmp_root[gtid] = (kmp_root_t *)__kmp_allocate(sizeof(kmp_root_t));
3707 KMP_DEBUG_ASSERT(!root->r.r_root_team);
3708 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003709
Jonathan Peyton5375fe82016-11-14 21:13:44 +00003710#if KMP_STATS_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00003711 // Initialize stats as soon as possible (right after gtid assignment).
3712 __kmp_stats_thread_ptr = __kmp_stats_list->push_back(gtid);
Jonathan Peytonf0682ac2018-07-30 17:41:08 +00003713 __kmp_stats_thread_ptr->startLife();
Jonathan Peyton30419822017-05-12 18:01:32 +00003714 KMP_SET_THREAD_STATE(SERIAL_REGION);
3715 KMP_INIT_PARTITIONED_TIMERS(OMP_serial);
Jonathan Peyton5375fe82016-11-14 21:13:44 +00003716#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00003717 __kmp_initialize_root(root);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003718
Jonathan Peyton30419822017-05-12 18:01:32 +00003719 /* setup new root thread structure */
3720 if (root->r.r_uber_thread) {
3721 root_thread = root->r.r_uber_thread;
3722 } else {
3723 root_thread = (kmp_info_t *)__kmp_allocate(sizeof(kmp_info_t));
3724 if (__kmp_storage_map) {
3725 __kmp_print_thread_storage_map(root_thread, gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003726 }
Jonathan Peyton30419822017-05-12 18:01:32 +00003727 root_thread->th.th_info.ds.ds_gtid = gtid;
Joachim Protze82e94a52017-11-01 10:08:30 +00003728#if OMPT_SUPPORT
3729 root_thread->th.ompt_thread_info.thread_data.ptr = NULL;
3730#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00003731 root_thread->th.th_root = root;
3732 if (__kmp_env_consistency_check) {
3733 root_thread->th.th_cons = __kmp_allocate_cons_stack(gtid);
3734 }
3735#if USE_FAST_MEMORY
3736 __kmp_initialize_fast_memory(root_thread);
3737#endif /* USE_FAST_MEMORY */
Jim Cownie5e8470a2013-09-27 10:38:44 +00003738
Jonathan Peyton30419822017-05-12 18:01:32 +00003739#if KMP_USE_BGET
3740 KMP_DEBUG_ASSERT(root_thread->th.th_local.bget_data == NULL);
3741 __kmp_initialize_bget(root_thread);
3742#endif
3743 __kmp_init_random(root_thread); // Initialize random number generator
3744 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00003745
Jonathan Peyton30419822017-05-12 18:01:32 +00003746 /* setup the serial team held in reserve by the root thread */
3747 if (!root_thread->th.th_serial_team) {
3748 kmp_internal_control_t r_icvs = __kmp_get_global_icvs();
3749 KF_TRACE(10, ("__kmp_register_root: before serial_team\n"));
3750 root_thread->th.th_serial_team =
3751 __kmp_allocate_team(root, 1, 1,
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00003752#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00003753 ompt_data_none, // root parallel id
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00003754#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00003755#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00003756 proc_bind_default,
Jim Cownie5e8470a2013-09-27 10:38:44 +00003757#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00003758 &r_icvs, 0 USE_NESTED_HOT_ARG(NULL));
3759 }
3760 KMP_ASSERT(root_thread->th.th_serial_team);
3761 KF_TRACE(10, ("__kmp_register_root: after serial_team = %p\n",
3762 root_thread->th.th_serial_team));
Jim Cownie5e8470a2013-09-27 10:38:44 +00003763
Jonathan Peyton30419822017-05-12 18:01:32 +00003764 /* drop root_thread into place */
3765 TCW_SYNC_PTR(__kmp_threads[gtid], root_thread);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003766
Jonathan Peyton30419822017-05-12 18:01:32 +00003767 root->r.r_root_team->t.t_threads[0] = root_thread;
3768 root->r.r_hot_team->t.t_threads[0] = root_thread;
3769 root_thread->th.th_serial_team->t.t_threads[0] = root_thread;
3770 // AC: the team created in reserve, not for execution (it is unused for now).
3771 root_thread->th.th_serial_team->t.t_serialized = 0;
3772 root->r.r_uber_thread = root_thread;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003773
Jonathan Peyton30419822017-05-12 18:01:32 +00003774 /* initialize the thread, get it ready to go */
3775 __kmp_initialize_info(root_thread, root->r.r_root_team, 0, gtid);
3776 TCW_4(__kmp_init_gtid, TRUE);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003777
Jonathan Peyton30419822017-05-12 18:01:32 +00003778 /* prepare the master thread for get_gtid() */
3779 __kmp_gtid_set_specific(gtid);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00003780
Jonathan Peyton7abf9d52016-05-26 18:19:10 +00003781#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00003782 __kmp_itt_thread_name(gtid);
Jonathan Peyton7abf9d52016-05-26 18:19:10 +00003783#endif /* USE_ITT_BUILD */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00003784
Jonathan Peyton30419822017-05-12 18:01:32 +00003785#ifdef KMP_TDATA_GTID
3786 __kmp_gtid = gtid;
Jonathan Peyton8fbb49a2015-07-09 18:16:58 +00003787#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00003788 __kmp_create_worker(gtid, root_thread, __kmp_stksize);
3789 KMP_DEBUG_ASSERT(__kmp_gtid_get_specific() == gtid);
3790
3791 KA_TRACE(20, ("__kmp_register_root: T#%d init T#%d(%d:%d) arrived: join=%u, "
3792 "plain=%u\n",
3793 gtid, __kmp_gtid_from_tid(0, root->r.r_hot_team),
3794 root->r.r_hot_team->t.t_id, 0, KMP_INIT_BARRIER_STATE,
3795 KMP_INIT_BARRIER_STATE));
3796 { // Initialize barrier data.
3797 int b;
3798 for (b = 0; b < bs_last_barrier; ++b) {
3799 root_thread->th.th_bar[b].bb.b_arrived = KMP_INIT_BARRIER_STATE;
3800#if USE_DEBUGGER
3801 root_thread->th.th_bar[b].bb.b_worker_arrived = 0;
3802#endif
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00003803 }
Jonathan Peyton30419822017-05-12 18:01:32 +00003804 }
3805 KMP_DEBUG_ASSERT(root->r.r_hot_team->t.t_bar[bs_forkjoin_barrier].b_arrived ==
3806 KMP_INIT_BARRIER_STATE);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003807
Alp Toker763b9392014-02-28 09:42:41 +00003808#if KMP_AFFINITY_SUPPORTED
Jonathan Peyton30419822017-05-12 18:01:32 +00003809#if OMP_40_ENABLED
3810 root_thread->th.th_current_place = KMP_PLACE_UNDEFINED;
3811 root_thread->th.th_new_place = KMP_PLACE_UNDEFINED;
3812 root_thread->th.th_first_place = KMP_PLACE_UNDEFINED;
3813 root_thread->th.th_last_place = KMP_PLACE_UNDEFINED;
3814#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00003815 if (TCR_4(__kmp_init_middle)) {
3816 __kmp_affinity_set_init_mask(gtid, TRUE);
3817 }
Alp Toker763b9392014-02-28 09:42:41 +00003818#endif /* KMP_AFFINITY_SUPPORTED */
Jonathan Peyton92ca6182018-09-07 18:25:49 +00003819#if OMP_50_ENABLED
3820 root_thread->th.th_def_allocator = __kmp_def_allocator;
3821#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00003822
Jonathan Peyton30419822017-05-12 18:01:32 +00003823 __kmp_root_counter++;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003824
Joachim Protze82e94a52017-11-01 10:08:30 +00003825#if OMPT_SUPPORT
3826 if (!initial_thread && ompt_enabled.enabled) {
3827
Joachim Protze489cdb72018-09-10 14:34:54 +00003828 kmp_info_t *root_thread = ompt_get_thread();
Joachim Protze82e94a52017-11-01 10:08:30 +00003829
3830 ompt_set_thread_state(root_thread, omp_state_overhead);
3831
3832 if (ompt_enabled.ompt_callback_thread_begin) {
3833 ompt_callbacks.ompt_callback(ompt_callback_thread_begin)(
3834 ompt_thread_initial, __ompt_get_thread_data_internal());
3835 }
3836 ompt_data_t *task_data;
3837 __ompt_get_task_info_internal(0, NULL, &task_data, NULL, NULL, NULL);
3838 if (ompt_enabled.ompt_callback_task_create) {
3839 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
3840 NULL, NULL, task_data, ompt_task_initial, 0, NULL);
3841 // initial task has nothing to return to
3842 }
3843
3844 ompt_set_thread_state(root_thread, omp_state_work_serial);
3845 }
3846#endif
3847
Jonathan Peyton30419822017-05-12 18:01:32 +00003848 KMP_MB();
3849 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003850
Jonathan Peyton30419822017-05-12 18:01:32 +00003851 return gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003852}
3853
Jim Cownie4cc4bb42014-10-07 16:25:50 +00003854#if KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00003855static int __kmp_free_hot_teams(kmp_root_t *root, kmp_info_t *thr, int level,
3856 const int max_level) {
3857 int i, n, nth;
3858 kmp_hot_team_ptr_t *hot_teams = thr->th.th_hot_teams;
3859 if (!hot_teams || !hot_teams[level].hot_team) {
3860 return 0;
3861 }
3862 KMP_DEBUG_ASSERT(level < max_level);
3863 kmp_team_t *team = hot_teams[level].hot_team;
3864 nth = hot_teams[level].hot_team_nth;
3865 n = nth - 1; // master is not freed
3866 if (level < max_level - 1) {
3867 for (i = 0; i < nth; ++i) {
3868 kmp_info_t *th = team->t.t_threads[i];
3869 n += __kmp_free_hot_teams(root, th, level + 1, max_level);
3870 if (i > 0 && th->th.th_hot_teams) {
3871 __kmp_free(th->th.th_hot_teams);
3872 th->th.th_hot_teams = NULL;
3873 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00003874 }
Jonathan Peyton30419822017-05-12 18:01:32 +00003875 }
3876 __kmp_free_team(root, team, NULL);
3877 return n;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00003878}
3879#endif
3880
Jonathan Peyton30419822017-05-12 18:01:32 +00003881// Resets a root thread and clear its root and hot teams.
3882// Returns the number of __kmp_threads entries directly and indirectly freed.
3883static int __kmp_reset_root(int gtid, kmp_root_t *root) {
3884 kmp_team_t *root_team = root->r.r_root_team;
3885 kmp_team_t *hot_team = root->r.r_hot_team;
3886 int n = hot_team->t.t_nproc;
3887 int i;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003888
Jonathan Peyton30419822017-05-12 18:01:32 +00003889 KMP_DEBUG_ASSERT(!root->r.r_active);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003890
Jonathan Peyton30419822017-05-12 18:01:32 +00003891 root->r.r_root_team = NULL;
3892 root->r.r_hot_team = NULL;
3893 // __kmp_free_team() does not free hot teams, so we have to clear r_hot_team
3894 // before call to __kmp_free_team().
3895 __kmp_free_team(root, root_team USE_NESTED_HOT_ARG(NULL));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00003896#if KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00003897 if (__kmp_hot_teams_max_level >
3898 0) { // need to free nested hot teams and their threads if any
3899 for (i = 0; i < hot_team->t.t_nproc; ++i) {
3900 kmp_info_t *th = hot_team->t.t_threads[i];
3901 if (__kmp_hot_teams_max_level > 1) {
3902 n += __kmp_free_hot_teams(root, th, 1, __kmp_hot_teams_max_level);
3903 }
3904 if (th->th.th_hot_teams) {
3905 __kmp_free(th->th.th_hot_teams);
3906 th->th.th_hot_teams = NULL;
3907 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00003908 }
Jonathan Peyton30419822017-05-12 18:01:32 +00003909 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00003910#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00003911 __kmp_free_team(root, hot_team USE_NESTED_HOT_ARG(NULL));
Jim Cownie5e8470a2013-09-27 10:38:44 +00003912
Jonathan Peyton30419822017-05-12 18:01:32 +00003913 // Before we can reap the thread, we need to make certain that all other
3914 // threads in the teams that had this root as ancestor have stopped trying to
3915 // steal tasks.
3916 if (__kmp_tasking_mode != tskm_immediate_exec) {
3917 __kmp_wait_to_unref_task_teams();
3918 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00003919
Jonathan Peyton30419822017-05-12 18:01:32 +00003920#if KMP_OS_WINDOWS
3921 /* Close Handle of root duplicated in __kmp_create_worker (tr #62919) */
3922 KA_TRACE(
3923 10, ("__kmp_reset_root: free handle, th = %p, handle = %" KMP_UINTPTR_SPEC
3924 "\n",
3925 (LPVOID) & (root->r.r_uber_thread->th),
3926 root->r.r_uber_thread->th.th_info.ds.ds_thread));
3927 __kmp_free_handle(root->r.r_uber_thread->th.th_info.ds.ds_thread);
3928#endif /* KMP_OS_WINDOWS */
Jim Cownie5e8470a2013-09-27 10:38:44 +00003929
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00003930#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00003931 if (ompt_enabled.ompt_callback_thread_end) {
3932 ompt_callbacks.ompt_callback(ompt_callback_thread_end)(
3933 &(root->r.r_uber_thread->th.ompt_thread_info.thread_data));
Jonathan Peyton30419822017-05-12 18:01:32 +00003934 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00003935#endif
3936
Jonathan Peyton30419822017-05-12 18:01:32 +00003937 TCW_4(__kmp_nth,
3938 __kmp_nth - 1); // __kmp_reap_thread will decrement __kmp_all_nth.
Jonathan Peytonf4392462017-07-27 20:58:41 +00003939 root->r.r_cg_nthreads--;
3940
Jonathan Peyton30419822017-05-12 18:01:32 +00003941 __kmp_reap_thread(root->r.r_uber_thread, 1);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003942
Jonathan Peyton30419822017-05-12 18:01:32 +00003943 // We canot put root thread to __kmp_thread_pool, so we have to reap it istead
3944 // of freeing.
3945 root->r.r_uber_thread = NULL;
3946 /* mark root as no longer in use */
3947 root->r.r_begin = FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003948
Jonathan Peyton30419822017-05-12 18:01:32 +00003949 return n;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003950}
3951
Jonathan Peyton30419822017-05-12 18:01:32 +00003952void __kmp_unregister_root_current_thread(int gtid) {
3953 KA_TRACE(1, ("__kmp_unregister_root_current_thread: enter T#%d\n", gtid));
3954 /* this lock should be ok, since unregister_root_current_thread is never
3955 called during an abort, only during a normal close. furthermore, if you
3956 have the forkjoin lock, you should never try to get the initz lock */
3957 __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock);
3958 if (TCR_4(__kmp_global.g.g_done) || !__kmp_init_serial) {
3959 KC_TRACE(10, ("__kmp_unregister_root_current_thread: already finished, "
3960 "exiting T#%d\n",
3961 gtid));
3962 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
3963 return;
3964 }
3965 kmp_root_t *root = __kmp_root[gtid];
Jim Cownie77c2a632014-09-03 11:34:33 +00003966
Jonathan Peyton30419822017-05-12 18:01:32 +00003967 KMP_DEBUG_ASSERT(__kmp_threads && __kmp_threads[gtid]);
3968 KMP_ASSERT(KMP_UBER_GTID(gtid));
3969 KMP_ASSERT(root == __kmp_threads[gtid]->th.th_root);
3970 KMP_ASSERT(root->r.r_active == FALSE);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003971
Jonathan Peyton30419822017-05-12 18:01:32 +00003972 KMP_MB();
Jim Cownie5e8470a2013-09-27 10:38:44 +00003973
Jonathan Peytondf6818b2016-06-14 17:57:47 +00003974#if OMP_45_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00003975 kmp_info_t *thread = __kmp_threads[gtid];
3976 kmp_team_t *team = thread->th.th_team;
3977 kmp_task_team_t *task_team = thread->th.th_task_team;
Andrey Churbanov535b6fa2015-05-07 17:41:51 +00003978
Jonathan Peyton30419822017-05-12 18:01:32 +00003979 // we need to wait for the proxy tasks before finishing the thread
3980 if (task_team != NULL && task_team->tt.tt_found_proxy_tasks) {
Jonathan Peyton6d247f72015-09-10 21:33:50 +00003981#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00003982 // the runtime is shutting down so we won't report any events
Joachim Protze82e94a52017-11-01 10:08:30 +00003983 thread->th.ompt_thread_info.state = omp_state_undefined;
Jonathan Peyton6d247f72015-09-10 21:33:50 +00003984#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00003985 __kmp_task_team_wait(thread, team USE_ITT_BUILD_ARG(NULL));
3986 }
Andrey Churbanov535b6fa2015-05-07 17:41:51 +00003987#endif
3988
Jonathan Peyton30419822017-05-12 18:01:32 +00003989 __kmp_reset_root(gtid, root);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003990
Jonathan Peyton30419822017-05-12 18:01:32 +00003991 /* free up this thread slot */
3992 __kmp_gtid_set_specific(KMP_GTID_DNE);
Jim Cownie5e8470a2013-09-27 10:38:44 +00003993#ifdef KMP_TDATA_GTID
Jonathan Peyton30419822017-05-12 18:01:32 +00003994 __kmp_gtid = KMP_GTID_DNE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00003995#endif
3996
Jonathan Peyton30419822017-05-12 18:01:32 +00003997 KMP_MB();
3998 KC_TRACE(10,
3999 ("__kmp_unregister_root_current_thread: T#%d unregistered\n", gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00004000
Jonathan Peyton30419822017-05-12 18:01:32 +00004001 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004002}
4003
Jonathan Peyton2321d572015-06-08 19:25:25 +00004004#if KMP_OS_WINDOWS
Jim Cownie5e8470a2013-09-27 10:38:44 +00004005/* __kmp_forkjoin_lock must be already held
Jonathan Peyton30419822017-05-12 18:01:32 +00004006 Unregisters a root thread that is not the current thread. Returns the number
4007 of __kmp_threads entries freed as a result. */
4008static int __kmp_unregister_root_other_thread(int gtid) {
4009 kmp_root_t *root = __kmp_root[gtid];
4010 int r;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004011
Jonathan Peyton30419822017-05-12 18:01:32 +00004012 KA_TRACE(1, ("__kmp_unregister_root_other_thread: enter T#%d\n", gtid));
4013 KMP_DEBUG_ASSERT(__kmp_threads && __kmp_threads[gtid]);
4014 KMP_ASSERT(KMP_UBER_GTID(gtid));
4015 KMP_ASSERT(root == __kmp_threads[gtid]->th.th_root);
4016 KMP_ASSERT(root->r.r_active == FALSE);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004017
Jonathan Peyton30419822017-05-12 18:01:32 +00004018 r = __kmp_reset_root(gtid, root);
4019 KC_TRACE(10,
4020 ("__kmp_unregister_root_other_thread: T#%d unregistered\n", gtid));
4021 return r;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004022}
Jonathan Peyton2321d572015-06-08 19:25:25 +00004023#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00004024
Jim Cownie5e8470a2013-09-27 10:38:44 +00004025#if KMP_DEBUG
4026void __kmp_task_info() {
4027
Jonathan Peyton30419822017-05-12 18:01:32 +00004028 kmp_int32 gtid = __kmp_entry_gtid();
4029 kmp_int32 tid = __kmp_tid_from_gtid(gtid);
4030 kmp_info_t *this_thr = __kmp_threads[gtid];
4031 kmp_team_t *steam = this_thr->th.th_serial_team;
4032 kmp_team_t *team = this_thr->th.th_team;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004033
Jonathan Peytonbaad3f62018-08-09 22:04:30 +00004034 __kmp_printf(
4035 "__kmp_task_info: gtid=%d tid=%d t_thread=%p team=%p steam=%p curtask=%p "
4036 "ptask=%p\n",
4037 gtid, tid, this_thr, team, steam, this_thr->th.th_current_task,
4038 team->t.t_implicit_task_taskdata[tid].td_parent);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004039}
4040#endif // KMP_DEBUG
4041
Jonathan Peyton30419822017-05-12 18:01:32 +00004042/* TODO optimize with one big memclr, take out what isn't needed, split
4043 responsibility to workers as much as possible, and delay initialization of
4044 features as much as possible */
4045static void __kmp_initialize_info(kmp_info_t *this_thr, kmp_team_t *team,
4046 int tid, int gtid) {
4047 /* this_thr->th.th_info.ds.ds_gtid is setup in
4048 kmp_allocate_thread/create_worker.
4049 this_thr->th.th_serial_team is setup in __kmp_allocate_thread */
4050 kmp_info_t *master = team->t.t_threads[0];
4051 KMP_DEBUG_ASSERT(this_thr != NULL);
4052 KMP_DEBUG_ASSERT(this_thr->th.th_serial_team);
4053 KMP_DEBUG_ASSERT(team);
4054 KMP_DEBUG_ASSERT(team->t.t_threads);
4055 KMP_DEBUG_ASSERT(team->t.t_dispatch);
4056 KMP_DEBUG_ASSERT(master);
4057 KMP_DEBUG_ASSERT(master->th.th_root);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004058
Jonathan Peyton30419822017-05-12 18:01:32 +00004059 KMP_MB();
Jim Cownie5e8470a2013-09-27 10:38:44 +00004060
Jonathan Peyton30419822017-05-12 18:01:32 +00004061 TCW_SYNC_PTR(this_thr->th.th_team, team);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004062
Jonathan Peyton30419822017-05-12 18:01:32 +00004063 this_thr->th.th_info.ds.ds_tid = tid;
4064 this_thr->th.th_set_nproc = 0;
4065 if (__kmp_tasking_mode != tskm_immediate_exec)
4066 // When tasking is possible, threads are not safe to reap until they are
4067 // done tasking; this will be set when tasking code is exited in wait
4068 this_thr->th.th_reap_state = KMP_NOT_SAFE_TO_REAP;
4069 else // no tasking --> always safe to reap
4070 this_thr->th.th_reap_state = KMP_SAFE_TO_REAP;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004071#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00004072 this_thr->th.th_set_proc_bind = proc_bind_default;
4073#if KMP_AFFINITY_SUPPORTED
4074 this_thr->th.th_new_place = this_thr->th.th_current_place;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004075#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00004076#endif
4077 this_thr->th.th_root = master->th.th_root;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004078
Jonathan Peyton30419822017-05-12 18:01:32 +00004079 /* setup the thread's cache of the team structure */
4080 this_thr->th.th_team_nproc = team->t.t_nproc;
4081 this_thr->th.th_team_master = master;
4082 this_thr->th.th_team_serialized = team->t.t_serialized;
4083 TCW_PTR(this_thr->th.th_sleep_loc, NULL);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004084
Jonathan Peyton30419822017-05-12 18:01:32 +00004085 KMP_DEBUG_ASSERT(team->t.t_implicit_task_taskdata);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004086
Jonathan Peyton30419822017-05-12 18:01:32 +00004087 KF_TRACE(10, ("__kmp_initialize_info1: T#%d:%d this_thread=%p curtask=%p\n",
4088 tid, gtid, this_thr, this_thr->th.th_current_task));
Jim Cownie5e8470a2013-09-27 10:38:44 +00004089
Jonathan Peyton30419822017-05-12 18:01:32 +00004090 __kmp_init_implicit_task(this_thr->th.th_team_master->th.th_ident, this_thr,
4091 team, tid, TRUE);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004092
Jonathan Peyton30419822017-05-12 18:01:32 +00004093 KF_TRACE(10, ("__kmp_initialize_info2: T#%d:%d this_thread=%p curtask=%p\n",
4094 tid, gtid, this_thr, this_thr->th.th_current_task));
4095 // TODO: Initialize ICVs from parent; GEH - isn't that already done in
4096 // __kmp_initialize_team()?
Jim Cownie5e8470a2013-09-27 10:38:44 +00004097
Jonathan Peyton30419822017-05-12 18:01:32 +00004098 /* TODO no worksharing in speculative threads */
4099 this_thr->th.th_dispatch = &team->t.t_dispatch[tid];
Jim Cownie5e8470a2013-09-27 10:38:44 +00004100
Jonathan Peyton30419822017-05-12 18:01:32 +00004101 this_thr->th.th_local.this_construct = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004102
Jonathan Peyton30419822017-05-12 18:01:32 +00004103 if (!this_thr->th.th_pri_common) {
4104 this_thr->th.th_pri_common =
4105 (struct common_table *)__kmp_allocate(sizeof(struct common_table));
4106 if (__kmp_storage_map) {
4107 __kmp_print_storage_map_gtid(
4108 gtid, this_thr->th.th_pri_common, this_thr->th.th_pri_common + 1,
4109 sizeof(struct common_table), "th_%d.th_pri_common\n", gtid);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00004110 }
Jonathan Peyton30419822017-05-12 18:01:32 +00004111 this_thr->th.th_pri_head = NULL;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00004112 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00004113
Jonathan Peyton30419822017-05-12 18:01:32 +00004114 /* Initialize dynamic dispatch */
4115 {
4116 volatile kmp_disp_t *dispatch = this_thr->th.th_dispatch;
4117 // Use team max_nproc since this will never change for the team.
4118 size_t disp_size =
4119 sizeof(dispatch_private_info_t) *
4120 (team->t.t_max_nproc == 1 ? 1 : __kmp_dispatch_num_buffers);
4121 KD_TRACE(10, ("__kmp_initialize_info: T#%d max_nproc: %d\n", gtid,
4122 team->t.t_max_nproc));
4123 KMP_ASSERT(dispatch);
4124 KMP_DEBUG_ASSERT(team->t.t_dispatch);
4125 KMP_DEBUG_ASSERT(dispatch == &team->t.t_dispatch[tid]);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004126
Jonathan Peyton30419822017-05-12 18:01:32 +00004127 dispatch->th_disp_index = 0;
Jonathan Peytondf6818b2016-06-14 17:57:47 +00004128#if OMP_45_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00004129 dispatch->th_doacross_buf_idx = 0;
Jonathan Peyton71909c52016-03-02 22:42:06 +00004130#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00004131 if (!dispatch->th_disp_buffer) {
4132 dispatch->th_disp_buffer =
4133 (dispatch_private_info_t *)__kmp_allocate(disp_size);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004134
Jonathan Peyton30419822017-05-12 18:01:32 +00004135 if (__kmp_storage_map) {
4136 __kmp_print_storage_map_gtid(
4137 gtid, &dispatch->th_disp_buffer[0],
4138 &dispatch->th_disp_buffer[team->t.t_max_nproc == 1
4139 ? 1
4140 : __kmp_dispatch_num_buffers],
4141 disp_size, "th_%d.th_dispatch.th_disp_buffer "
4142 "(team_%d.t_dispatch[%d].th_disp_buffer)",
4143 gtid, team->t.t_id, gtid);
4144 }
4145 } else {
4146 memset(&dispatch->th_disp_buffer[0], '\0', disp_size);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004147 }
4148
Jonathan Peyton30419822017-05-12 18:01:32 +00004149 dispatch->th_dispatch_pr_current = 0;
4150 dispatch->th_dispatch_sh_current = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004151
Jonathan Peyton30419822017-05-12 18:01:32 +00004152 dispatch->th_deo_fcn = 0; /* ORDERED */
4153 dispatch->th_dxo_fcn = 0; /* END ORDERED */
4154 }
Andrey Churbanov6d224db2015-02-10 18:37:43 +00004155
Jonathan Peyton30419822017-05-12 18:01:32 +00004156 this_thr->th.th_next_pool = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004157
Jonathan Peyton30419822017-05-12 18:01:32 +00004158 if (!this_thr->th.th_task_state_memo_stack) {
4159 size_t i;
4160 this_thr->th.th_task_state_memo_stack =
4161 (kmp_uint8 *)__kmp_allocate(4 * sizeof(kmp_uint8));
4162 this_thr->th.th_task_state_top = 0;
4163 this_thr->th.th_task_state_stack_sz = 4;
4164 for (i = 0; i < this_thr->th.th_task_state_stack_sz;
4165 ++i) // zero init the stack
4166 this_thr->th.th_task_state_memo_stack[i] = 0;
4167 }
4168
4169 KMP_DEBUG_ASSERT(!this_thr->th.th_spin_here);
4170 KMP_DEBUG_ASSERT(this_thr->th.th_next_waiting == 0);
4171
4172 KMP_MB();
Jim Cownie5e8470a2013-09-27 10:38:44 +00004173}
4174
Jonathan Peyton30419822017-05-12 18:01:32 +00004175/* allocate a new thread for the requesting team. this is only called from
4176 within a forkjoin critical section. we will first try to get an available
4177 thread from the thread pool. if none is available, we will fork a new one
4178 assuming we are able to create a new one. this should be assured, as the
4179 caller should check on this first. */
4180kmp_info_t *__kmp_allocate_thread(kmp_root_t *root, kmp_team_t *team,
4181 int new_tid) {
4182 kmp_team_t *serial_team;
4183 kmp_info_t *new_thr;
4184 int new_gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004185
Jonathan Peyton30419822017-05-12 18:01:32 +00004186 KA_TRACE(20, ("__kmp_allocate_thread: T#%d\n", __kmp_get_gtid()));
4187 KMP_DEBUG_ASSERT(root && team);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004188#if !KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00004189 KMP_DEBUG_ASSERT(KMP_MASTER_GTID(__kmp_get_gtid()));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004190#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00004191 KMP_MB();
Jim Cownie5e8470a2013-09-27 10:38:44 +00004192
Jonathan Peyton30419822017-05-12 18:01:32 +00004193 /* first, try to get one from the thread pool */
4194 if (__kmp_thread_pool) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00004195
Andrey Churbanovc47afcd2017-07-03 11:24:08 +00004196 new_thr = CCAST(kmp_info_t *, __kmp_thread_pool);
Jonathan Peyton30419822017-05-12 18:01:32 +00004197 __kmp_thread_pool = (volatile kmp_info_t *)new_thr->th.th_next_pool;
4198 if (new_thr == __kmp_thread_pool_insert_pt) {
4199 __kmp_thread_pool_insert_pt = NULL;
4200 }
4201 TCW_4(new_thr->th.th_in_pool, FALSE);
4202 // Don't touch th_active_in_pool or th_active.
4203 // The worker thread adjusts those flags as it sleeps/awakens.
4204 __kmp_thread_pool_nth--;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004205
Jonathan Peyton30419822017-05-12 18:01:32 +00004206 KA_TRACE(20, ("__kmp_allocate_thread: T#%d using thread T#%d\n",
4207 __kmp_get_gtid(), new_thr->th.th_info.ds.ds_gtid));
4208 KMP_ASSERT(!new_thr->th.th_team);
4209 KMP_DEBUG_ASSERT(__kmp_nth < __kmp_threads_capacity);
4210 KMP_DEBUG_ASSERT(__kmp_thread_pool_nth >= 0);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004211
Jonathan Peyton30419822017-05-12 18:01:32 +00004212 /* setup the thread structure */
4213 __kmp_initialize_info(new_thr, team, new_tid,
4214 new_thr->th.th_info.ds.ds_gtid);
4215 KMP_DEBUG_ASSERT(new_thr->th.th_serial_team);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004216
Jonathan Peyton30419822017-05-12 18:01:32 +00004217 TCW_4(__kmp_nth, __kmp_nth + 1);
Jonathan Peytonf4392462017-07-27 20:58:41 +00004218 root->r.r_cg_nthreads++;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004219
Jonathan Peyton30419822017-05-12 18:01:32 +00004220 new_thr->th.th_task_state = 0;
4221 new_thr->th.th_task_state_top = 0;
4222 new_thr->th.th_task_state_stack_sz = 4;
Andrey Churbanov6d224db2015-02-10 18:37:43 +00004223
Jim Cownie5e8470a2013-09-27 10:38:44 +00004224#ifdef KMP_ADJUST_BLOCKTIME
Jonathan Peyton30419822017-05-12 18:01:32 +00004225 /* Adjust blocktime back to zero if necessary */
4226 /* Middle initialization might not have occurred yet */
4227 if (!__kmp_env_blocktime && (__kmp_avail_proc > 0)) {
4228 if (__kmp_nth > __kmp_avail_proc) {
4229 __kmp_zero_bt = TRUE;
4230 }
4231 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00004232#endif /* KMP_ADJUST_BLOCKTIME */
4233
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004234#if KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00004235 // If thread entered pool via __kmp_free_thread, wait_flag should !=
4236 // KMP_BARRIER_PARENT_FLAG.
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004237 int b;
Jonathan Peyton30419822017-05-12 18:01:32 +00004238 kmp_balign_t *balign = new_thr->th.th_bar;
4239 for (b = 0; b < bs_last_barrier; ++b)
4240 KMP_DEBUG_ASSERT(balign[b].bb.wait_flag != KMP_BARRIER_PARENT_FLAG);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004241#endif
4242
Jonathan Peyton30419822017-05-12 18:01:32 +00004243 KF_TRACE(10, ("__kmp_allocate_thread: T#%d using thread %p T#%d\n",
4244 __kmp_get_gtid(), new_thr, new_thr->th.th_info.ds.ds_gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00004245
Jim Cownie5e8470a2013-09-27 10:38:44 +00004246 KMP_MB();
4247 return new_thr;
Jonathan Peyton30419822017-05-12 18:01:32 +00004248 }
4249
4250 /* no, well fork a new one */
4251 KMP_ASSERT(__kmp_nth == __kmp_all_nth);
4252 KMP_ASSERT(__kmp_all_nth < __kmp_threads_capacity);
4253
4254#if KMP_USE_MONITOR
4255 // If this is the first worker thread the RTL is creating, then also
4256 // launch the monitor thread. We try to do this as early as possible.
4257 if (!TCR_4(__kmp_init_monitor)) {
4258 __kmp_acquire_bootstrap_lock(&__kmp_monitor_lock);
4259 if (!TCR_4(__kmp_init_monitor)) {
4260 KF_TRACE(10, ("before __kmp_create_monitor\n"));
4261 TCW_4(__kmp_init_monitor, 1);
4262 __kmp_create_monitor(&__kmp_monitor);
4263 KF_TRACE(10, ("after __kmp_create_monitor\n"));
4264#if KMP_OS_WINDOWS
4265 // AC: wait until monitor has started. This is a fix for CQ232808.
4266 // The reason is that if the library is loaded/unloaded in a loop with
4267 // small (parallel) work in between, then there is high probability that
4268 // monitor thread started after the library shutdown. At shutdown it is
4269 // too late to cope with the problem, because when the master is in
4270 // DllMain (process detach) the monitor has no chances to start (it is
4271 // blocked), and master has no means to inform the monitor that the
4272 // library has gone, because all the memory which the monitor can access
4273 // is going to be released/reset.
4274 while (TCR_4(__kmp_init_monitor) < 2) {
4275 KMP_YIELD(TRUE);
4276 }
4277 KF_TRACE(10, ("after monitor thread has started\n"));
4278#endif
4279 }
4280 __kmp_release_bootstrap_lock(&__kmp_monitor_lock);
4281 }
4282#endif
4283
4284 KMP_MB();
4285 for (new_gtid = 1; TCR_PTR(__kmp_threads[new_gtid]) != NULL; ++new_gtid) {
4286 KMP_DEBUG_ASSERT(new_gtid < __kmp_threads_capacity);
4287 }
4288
4289 /* allocate space for it. */
4290 new_thr = (kmp_info_t *)__kmp_allocate(sizeof(kmp_info_t));
4291
4292 TCW_SYNC_PTR(__kmp_threads[new_gtid], new_thr);
4293
4294 if (__kmp_storage_map) {
4295 __kmp_print_thread_storage_map(new_thr, new_gtid);
4296 }
4297
4298 // add the reserve serialized team, initialized from the team's master thread
4299 {
4300 kmp_internal_control_t r_icvs = __kmp_get_x_global_icvs(team);
4301 KF_TRACE(10, ("__kmp_allocate_thread: before th_serial/serial_team\n"));
4302 new_thr->th.th_serial_team = serial_team =
4303 (kmp_team_t *)__kmp_allocate_team(root, 1, 1,
4304#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00004305 ompt_data_none, // root parallel id
Jonathan Peyton30419822017-05-12 18:01:32 +00004306#endif
4307#if OMP_40_ENABLED
4308 proc_bind_default,
4309#endif
4310 &r_icvs, 0 USE_NESTED_HOT_ARG(NULL));
4311 }
4312 KMP_ASSERT(serial_team);
4313 serial_team->t.t_serialized = 0; // AC: the team created in reserve, not for
4314 // execution (it is unused for now).
4315 serial_team->t.t_threads[0] = new_thr;
4316 KF_TRACE(10,
4317 ("__kmp_allocate_thread: after th_serial/serial_team : new_thr=%p\n",
4318 new_thr));
4319
4320 /* setup the thread structures */
4321 __kmp_initialize_info(new_thr, team, new_tid, new_gtid);
4322
4323#if USE_FAST_MEMORY
4324 __kmp_initialize_fast_memory(new_thr);
4325#endif /* USE_FAST_MEMORY */
4326
4327#if KMP_USE_BGET
4328 KMP_DEBUG_ASSERT(new_thr->th.th_local.bget_data == NULL);
4329 __kmp_initialize_bget(new_thr);
4330#endif
4331
4332 __kmp_init_random(new_thr); // Initialize random number generator
4333
4334 /* Initialize these only once when thread is grabbed for a team allocation */
4335 KA_TRACE(20,
4336 ("__kmp_allocate_thread: T#%d init go fork=%u, plain=%u\n",
4337 __kmp_get_gtid(), KMP_INIT_BARRIER_STATE, KMP_INIT_BARRIER_STATE));
4338
4339 int b;
4340 kmp_balign_t *balign = new_thr->th.th_bar;
4341 for (b = 0; b < bs_last_barrier; ++b) {
4342 balign[b].bb.b_go = KMP_INIT_BARRIER_STATE;
4343 balign[b].bb.team = NULL;
4344 balign[b].bb.wait_flag = KMP_BARRIER_NOT_WAITING;
4345 balign[b].bb.use_oncore_barrier = 0;
4346 }
4347
4348 new_thr->th.th_spin_here = FALSE;
4349 new_thr->th.th_next_waiting = 0;
Jonathan Peytona764af62018-07-19 19:17:00 +00004350#if KMP_OS_UNIX
4351 new_thr->th.th_blocking = false;
4352#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00004353
4354#if OMP_40_ENABLED && KMP_AFFINITY_SUPPORTED
4355 new_thr->th.th_current_place = KMP_PLACE_UNDEFINED;
4356 new_thr->th.th_new_place = KMP_PLACE_UNDEFINED;
4357 new_thr->th.th_first_place = KMP_PLACE_UNDEFINED;
4358 new_thr->th.th_last_place = KMP_PLACE_UNDEFINED;
4359#endif
Jonathan Peyton92ca6182018-09-07 18:25:49 +00004360#if OMP_50_ENABLED
4361 new_thr->th.th_def_allocator = __kmp_def_allocator;
4362#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00004363
4364 TCW_4(new_thr->th.th_in_pool, FALSE);
4365 new_thr->th.th_active_in_pool = FALSE;
4366 TCW_4(new_thr->th.th_active, TRUE);
4367
4368 /* adjust the global counters */
4369 __kmp_all_nth++;
4370 __kmp_nth++;
4371
Jonathan Peytonf4392462017-07-27 20:58:41 +00004372 root->r.r_cg_nthreads++;
4373
Jonathan Peyton30419822017-05-12 18:01:32 +00004374 // if __kmp_adjust_gtid_mode is set, then we use method #1 (sp search) for low
4375 // numbers of procs, and method #2 (keyed API call) for higher numbers.
4376 if (__kmp_adjust_gtid_mode) {
4377 if (__kmp_all_nth >= __kmp_tls_gtid_min) {
4378 if (TCR_4(__kmp_gtid_mode) != 2) {
4379 TCW_4(__kmp_gtid_mode, 2);
4380 }
4381 } else {
4382 if (TCR_4(__kmp_gtid_mode) != 1) {
4383 TCW_4(__kmp_gtid_mode, 1);
4384 }
4385 }
4386 }
4387
4388#ifdef KMP_ADJUST_BLOCKTIME
4389 /* Adjust blocktime back to zero if necessary */
4390 /* Middle initialization might not have occurred yet */
4391 if (!__kmp_env_blocktime && (__kmp_avail_proc > 0)) {
4392 if (__kmp_nth > __kmp_avail_proc) {
4393 __kmp_zero_bt = TRUE;
4394 }
4395 }
4396#endif /* KMP_ADJUST_BLOCKTIME */
4397
4398 /* actually fork it and create the new worker thread */
4399 KF_TRACE(
4400 10, ("__kmp_allocate_thread: before __kmp_create_worker: %p\n", new_thr));
4401 __kmp_create_worker(new_gtid, new_thr, __kmp_stksize);
4402 KF_TRACE(10,
4403 ("__kmp_allocate_thread: after __kmp_create_worker: %p\n", new_thr));
4404
4405 KA_TRACE(20, ("__kmp_allocate_thread: T#%d forked T#%d\n", __kmp_get_gtid(),
4406 new_gtid));
4407 KMP_MB();
4408 return new_thr;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004409}
4410
Jonathan Peyton30419822017-05-12 18:01:32 +00004411/* Reinitialize team for reuse.
4412 The hot team code calls this case at every fork barrier, so EPCC barrier
4413 test are extremely sensitive to changes in it, esp. writes to the team
4414 struct, which cause a cache invalidation in all threads.
4415 IF YOU TOUCH THIS ROUTINE, RUN EPCC C SYNCBENCH ON A BIG-IRON MACHINE!!! */
4416static void __kmp_reinitialize_team(kmp_team_t *team,
4417 kmp_internal_control_t *new_icvs,
4418 ident_t *loc) {
4419 KF_TRACE(10, ("__kmp_reinitialize_team: enter this_thread=%p team=%p\n",
4420 team->t.t_threads[0], team));
4421 KMP_DEBUG_ASSERT(team && new_icvs);
4422 KMP_DEBUG_ASSERT((!TCR_4(__kmp_init_parallel)) || new_icvs->nproc);
4423 KMP_CHECK_UPDATE(team->t.t_ident, loc);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004424
Jonathan Peyton30419822017-05-12 18:01:32 +00004425 KMP_CHECK_UPDATE(team->t.t_id, KMP_GEN_TEAM_ID());
Jonathan Peyton30419822017-05-12 18:01:32 +00004426 // Copy ICVs to the master thread's implicit taskdata
4427 __kmp_init_implicit_task(loc, team->t.t_threads[0], team, 0, FALSE);
4428 copy_icvs(&team->t.t_implicit_task_taskdata[0].td_icvs, new_icvs);
Jim Cownie181b4bb2013-12-23 17:28:57 +00004429
Jonathan Peyton30419822017-05-12 18:01:32 +00004430 KF_TRACE(10, ("__kmp_reinitialize_team: exit this_thread=%p team=%p\n",
4431 team->t.t_threads[0], team));
Jim Cownie181b4bb2013-12-23 17:28:57 +00004432}
4433
Jonathan Peyton30419822017-05-12 18:01:32 +00004434/* Initialize the team data structure.
4435 This assumes the t_threads and t_max_nproc are already set.
4436 Also, we don't touch the arguments */
4437static void __kmp_initialize_team(kmp_team_t *team, int new_nproc,
4438 kmp_internal_control_t *new_icvs,
4439 ident_t *loc) {
4440 KF_TRACE(10, ("__kmp_initialize_team: enter: team=%p\n", team));
Jim Cownie5e8470a2013-09-27 10:38:44 +00004441
Jonathan Peyton30419822017-05-12 18:01:32 +00004442 /* verify */
4443 KMP_DEBUG_ASSERT(team);
4444 KMP_DEBUG_ASSERT(new_nproc <= team->t.t_max_nproc);
4445 KMP_DEBUG_ASSERT(team->t.t_threads);
4446 KMP_MB();
Jim Cownie181b4bb2013-12-23 17:28:57 +00004447
Jonathan Peyton30419822017-05-12 18:01:32 +00004448 team->t.t_master_tid = 0; /* not needed */
4449 /* team->t.t_master_bar; not needed */
4450 team->t.t_serialized = new_nproc > 1 ? 0 : 1;
4451 team->t.t_nproc = new_nproc;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004452
Jonathan Peyton30419822017-05-12 18:01:32 +00004453 /* team->t.t_parent = NULL; TODO not needed & would mess up hot team */
4454 team->t.t_next_pool = NULL;
4455 /* memset( team->t.t_threads, 0, sizeof(kmp_info_t*)*new_nproc ); would mess
4456 * up hot team */
Jim Cownie5e8470a2013-09-27 10:38:44 +00004457
Jonathan Peyton30419822017-05-12 18:01:32 +00004458 TCW_SYNC_PTR(team->t.t_pkfn, NULL); /* not needed */
4459 team->t.t_invoke = NULL; /* not needed */
Jim Cownie5e8470a2013-09-27 10:38:44 +00004460
Jonathan Peyton30419822017-05-12 18:01:32 +00004461 // TODO???: team->t.t_max_active_levels = new_max_active_levels;
Jonathan Peytonba55a7b2017-11-29 22:47:52 +00004462 team->t.t_sched.sched = new_icvs->sched.sched;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004463
4464#if KMP_ARCH_X86 || KMP_ARCH_X86_64
Jonathan Peyton30419822017-05-12 18:01:32 +00004465 team->t.t_fp_control_saved = FALSE; /* not needed */
4466 team->t.t_x87_fpu_control_word = 0; /* not needed */
4467 team->t.t_mxcsr = 0; /* not needed */
Jim Cownie5e8470a2013-09-27 10:38:44 +00004468#endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
4469
Jonathan Peyton30419822017-05-12 18:01:32 +00004470 team->t.t_construct = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004471
Jonathan Peyton30419822017-05-12 18:01:32 +00004472 team->t.t_ordered.dt.t_value = 0;
4473 team->t.t_master_active = FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004474
Jonathan Peyton30419822017-05-12 18:01:32 +00004475 memset(&team->t.t_taskq, '\0', sizeof(kmp_taskq_t));
Jim Cownie5e8470a2013-09-27 10:38:44 +00004476
4477#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00004478 team->t.t_copypriv_data = NULL; /* not necessary, but nice for debugging */
Jim Cownie5e8470a2013-09-27 10:38:44 +00004479#endif
Jonathan Peyton37e2ef52018-07-09 17:36:22 +00004480#if KMP_OS_WINDOWS
Jonathan Peyton30419822017-05-12 18:01:32 +00004481 team->t.t_copyin_counter = 0; /* for barrier-free copyin implementation */
Jonathan Peyton37e2ef52018-07-09 17:36:22 +00004482#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00004483
Jonathan Peyton30419822017-05-12 18:01:32 +00004484 team->t.t_control_stack_top = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004485
Jonathan Peyton30419822017-05-12 18:01:32 +00004486 __kmp_reinitialize_team(team, new_icvs, loc);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004487
Jonathan Peyton30419822017-05-12 18:01:32 +00004488 KMP_MB();
4489 KF_TRACE(10, ("__kmp_initialize_team: exit: team=%p\n", team));
Jim Cownie5e8470a2013-09-27 10:38:44 +00004490}
4491
Alp Toker98758b02014-03-02 04:12:06 +00004492#if KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED
Jim Cownie5e8470a2013-09-27 10:38:44 +00004493/* Sets full mask for thread and returns old mask, no changes to structures. */
4494static void
Jonathan Peyton30419822017-05-12 18:01:32 +00004495__kmp_set_thread_affinity_mask_full_tmp(kmp_affin_mask_t *old_mask) {
4496 if (KMP_AFFINITY_CAPABLE()) {
4497 int status;
4498 if (old_mask != NULL) {
4499 status = __kmp_get_system_affinity(old_mask, TRUE);
4500 int error = errno;
4501 if (status != 0) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +00004502 __kmp_fatal(KMP_MSG(ChangeThreadAffMaskError), KMP_ERR(error),
4503 __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +00004504 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00004505 }
Jonathan Peyton30419822017-05-12 18:01:32 +00004506 __kmp_set_system_affinity(__kmp_affin_fullMask, TRUE);
4507 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00004508}
4509#endif
4510
Alp Toker98758b02014-03-02 04:12:06 +00004511#if OMP_40_ENABLED && KMP_AFFINITY_SUPPORTED
Jim Cownie5e8470a2013-09-27 10:38:44 +00004512
Jim Cownie5e8470a2013-09-27 10:38:44 +00004513// __kmp_partition_places() is the heart of the OpenMP 4.0 affinity mechanism.
4514// It calculats the worker + master thread's partition based upon the parent
Alp Toker8f2d3f02014-02-24 10:40:15 +00004515// thread's partition, and binds each worker to a thread in their partition.
Jim Cownie5e8470a2013-09-27 10:38:44 +00004516// The master thread's partition should already include its current binding.
Jonathan Peyton30419822017-05-12 18:01:32 +00004517static void __kmp_partition_places(kmp_team_t *team, int update_master_only) {
4518 // Copy the master thread's place partion to the team struct
4519 kmp_info_t *master_th = team->t.t_threads[0];
4520 KMP_DEBUG_ASSERT(master_th != NULL);
4521 kmp_proc_bind_t proc_bind = team->t.t_proc_bind;
4522 int first_place = master_th->th.th_first_place;
4523 int last_place = master_th->th.th_last_place;
4524 int masters_place = master_th->th.th_current_place;
4525 team->t.t_first_place = first_place;
4526 team->t.t_last_place = last_place;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004527
Jonathan Peyton30419822017-05-12 18:01:32 +00004528 KA_TRACE(20, ("__kmp_partition_places: enter: proc_bind = %d T#%d(%d:0) "
4529 "bound to place %d partition = [%d,%d]\n",
4530 proc_bind, __kmp_gtid_from_thread(team->t.t_threads[0]),
4531 team->t.t_id, masters_place, first_place, last_place));
Jim Cownie5e8470a2013-09-27 10:38:44 +00004532
Jonathan Peyton30419822017-05-12 18:01:32 +00004533 switch (proc_bind) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00004534
Jonathan Peyton30419822017-05-12 18:01:32 +00004535 case proc_bind_default:
4536 // serial teams might have the proc_bind policy set to proc_bind_default. It
4537 // doesn't matter, as we don't rebind master thread for any proc_bind policy
4538 KMP_DEBUG_ASSERT(team->t.t_nproc == 1);
4539 break;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004540
Jonathan Peyton30419822017-05-12 18:01:32 +00004541 case proc_bind_master: {
4542 int f;
4543 int n_th = team->t.t_nproc;
4544 for (f = 1; f < n_th; f++) {
4545 kmp_info_t *th = team->t.t_threads[f];
4546 KMP_DEBUG_ASSERT(th != NULL);
4547 th->th.th_first_place = first_place;
4548 th->th.th_last_place = last_place;
4549 th->th.th_new_place = masters_place;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004550
Jonathan Peyton30419822017-05-12 18:01:32 +00004551 KA_TRACE(100, ("__kmp_partition_places: master: T#%d(%d:%d) place %d "
4552 "partition = [%d,%d]\n",
4553 __kmp_gtid_from_thread(team->t.t_threads[f]), team->t.t_id,
4554 f, masters_place, first_place, last_place));
Jim Cownie5e8470a2013-09-27 10:38:44 +00004555 }
Jonathan Peyton30419822017-05-12 18:01:32 +00004556 } break;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004557
Jonathan Peyton30419822017-05-12 18:01:32 +00004558 case proc_bind_close: {
4559 int f;
4560 int n_th = team->t.t_nproc;
4561 int n_places;
4562 if (first_place <= last_place) {
4563 n_places = last_place - first_place + 1;
4564 } else {
4565 n_places = __kmp_affinity_num_masks - first_place + last_place + 1;
4566 }
4567 if (n_th <= n_places) {
4568 int place = masters_place;
4569 for (f = 1; f < n_th; f++) {
4570 kmp_info_t *th = team->t.t_threads[f];
4571 KMP_DEBUG_ASSERT(th != NULL);
4572
4573 if (place == last_place) {
4574 place = first_place;
4575 } else if (place == (int)(__kmp_affinity_num_masks - 1)) {
4576 place = 0;
4577 } else {
4578 place++;
4579 }
4580 th->th.th_first_place = first_place;
4581 th->th.th_last_place = last_place;
4582 th->th.th_new_place = place;
4583
4584 KA_TRACE(100, ("__kmp_partition_places: close: T#%d(%d:%d) place %d "
4585 "partition = [%d,%d]\n",
4586 __kmp_gtid_from_thread(team->t.t_threads[f]),
4587 team->t.t_id, f, place, first_place, last_place));
4588 }
4589 } else {
4590 int S, rem, gap, s_count;
4591 S = n_th / n_places;
4592 s_count = 0;
4593 rem = n_th - (S * n_places);
4594 gap = rem > 0 ? n_places / rem : n_places;
4595 int place = masters_place;
4596 int gap_ct = gap;
4597 for (f = 0; f < n_th; f++) {
4598 kmp_info_t *th = team->t.t_threads[f];
4599 KMP_DEBUG_ASSERT(th != NULL);
4600
4601 th->th.th_first_place = first_place;
4602 th->th.th_last_place = last_place;
4603 th->th.th_new_place = place;
4604 s_count++;
4605
4606 if ((s_count == S) && rem && (gap_ct == gap)) {
4607 // do nothing, add an extra thread to place on next iteration
4608 } else if ((s_count == S + 1) && rem && (gap_ct == gap)) {
4609 // we added an extra thread to this place; move to next place
4610 if (place == last_place) {
4611 place = first_place;
4612 } else if (place == (int)(__kmp_affinity_num_masks - 1)) {
4613 place = 0;
4614 } else {
4615 place++;
4616 }
4617 s_count = 0;
4618 gap_ct = 1;
4619 rem--;
4620 } else if (s_count == S) { // place full; don't add extra
4621 if (place == last_place) {
4622 place = first_place;
4623 } else if (place == (int)(__kmp_affinity_num_masks - 1)) {
4624 place = 0;
4625 } else {
4626 place++;
4627 }
4628 gap_ct++;
4629 s_count = 0;
4630 }
4631
4632 KA_TRACE(100,
4633 ("__kmp_partition_places: close: T#%d(%d:%d) place %d "
4634 "partition = [%d,%d]\n",
4635 __kmp_gtid_from_thread(team->t.t_threads[f]), team->t.t_id, f,
4636 th->th.th_new_place, first_place, last_place));
4637 }
4638 KMP_DEBUG_ASSERT(place == masters_place);
4639 }
4640 } break;
4641
4642 case proc_bind_spread: {
4643 int f;
4644 int n_th = team->t.t_nproc;
4645 int n_places;
4646 int thidx;
4647 if (first_place <= last_place) {
4648 n_places = last_place - first_place + 1;
4649 } else {
4650 n_places = __kmp_affinity_num_masks - first_place + last_place + 1;
4651 }
4652 if (n_th <= n_places) {
Paul Osmialowskia0162792017-08-10 23:04:11 +00004653 int place = -1;
Jonathan Peyton30419822017-05-12 18:01:32 +00004654
Paul Osmialowskia0162792017-08-10 23:04:11 +00004655 if (n_places != static_cast<int>(__kmp_affinity_num_masks)) {
4656 int S = n_places / n_th;
4657 int s_count, rem, gap, gap_ct;
4658
4659 place = masters_place;
4660 rem = n_places - n_th * S;
4661 gap = rem ? n_th / rem : 1;
4662 gap_ct = gap;
4663 thidx = n_th;
4664 if (update_master_only == 1)
4665 thidx = 1;
4666 for (f = 0; f < thidx; f++) {
4667 kmp_info_t *th = team->t.t_threads[f];
4668 KMP_DEBUG_ASSERT(th != NULL);
4669
4670 th->th.th_first_place = place;
4671 th->th.th_new_place = place;
4672 s_count = 1;
4673 while (s_count < S) {
4674 if (place == last_place) {
4675 place = first_place;
4676 } else if (place == (int)(__kmp_affinity_num_masks - 1)) {
4677 place = 0;
4678 } else {
4679 place++;
4680 }
4681 s_count++;
4682 }
4683 if (rem && (gap_ct == gap)) {
4684 if (place == last_place) {
4685 place = first_place;
4686 } else if (place == (int)(__kmp_affinity_num_masks - 1)) {
4687 place = 0;
4688 } else {
4689 place++;
4690 }
4691 rem--;
4692 gap_ct = 0;
4693 }
4694 th->th.th_last_place = place;
4695 gap_ct++;
4696
Jonathan Peyton30419822017-05-12 18:01:32 +00004697 if (place == last_place) {
4698 place = first_place;
4699 } else if (place == (int)(__kmp_affinity_num_masks - 1)) {
4700 place = 0;
4701 } else {
4702 place++;
4703 }
Paul Osmialowskia0162792017-08-10 23:04:11 +00004704
Jonathan Peyton94a114f2017-10-20 19:30:57 +00004705 KA_TRACE(100,
4706 ("__kmp_partition_places: spread: T#%d(%d:%d) place %d "
4707 "partition = [%d,%d], __kmp_affinity_num_masks: %u\n",
4708 __kmp_gtid_from_thread(team->t.t_threads[f]), team->t.t_id,
4709 f, th->th.th_new_place, th->th.th_first_place,
4710 th->th.th_last_place, __kmp_affinity_num_masks));
Jonathan Peyton30419822017-05-12 18:01:32 +00004711 }
Paul Osmialowskia0162792017-08-10 23:04:11 +00004712 } else {
4713 /* Having uniform space of available computation places I can create
4714 T partitions of round(P/T) size and put threads into the first
4715 place of each partition. */
4716 double current = static_cast<double>(masters_place);
4717 double spacing =
Jonathan Peyton94a114f2017-10-20 19:30:57 +00004718 (static_cast<double>(n_places + 1) / static_cast<double>(n_th));
Paul Osmialowskia0162792017-08-10 23:04:11 +00004719 int first, last;
4720 kmp_info_t *th;
4721
4722 thidx = n_th + 1;
4723 if (update_master_only == 1)
4724 thidx = 1;
4725 for (f = 0; f < thidx; f++) {
4726 first = static_cast<int>(current);
4727 last = static_cast<int>(current + spacing) - 1;
4728 KMP_DEBUG_ASSERT(last >= first);
4729 if (first >= n_places) {
4730 if (masters_place) {
4731 first -= n_places;
4732 last -= n_places;
4733 if (first == (masters_place + 1)) {
4734 KMP_DEBUG_ASSERT(f == n_th);
4735 first--;
4736 }
4737 if (last == masters_place) {
4738 KMP_DEBUG_ASSERT(f == (n_th - 1));
4739 last--;
4740 }
4741 } else {
4742 KMP_DEBUG_ASSERT(f == n_th);
4743 first = 0;
4744 last = 0;
4745 }
Jonathan Peyton30419822017-05-12 18:01:32 +00004746 }
Paul Osmialowskia0162792017-08-10 23:04:11 +00004747 if (last >= n_places) {
4748 last = (n_places - 1);
4749 }
4750 place = first;
4751 current += spacing;
4752 if (f < n_th) {
4753 KMP_DEBUG_ASSERT(0 <= first);
4754 KMP_DEBUG_ASSERT(n_places > first);
4755 KMP_DEBUG_ASSERT(0 <= last);
4756 KMP_DEBUG_ASSERT(n_places > last);
4757 KMP_DEBUG_ASSERT(last_place >= first_place);
4758 th = team->t.t_threads[f];
4759 KMP_DEBUG_ASSERT(th);
4760 th->th.th_first_place = first;
4761 th->th.th_new_place = place;
4762 th->th.th_last_place = last;
Jonathan Peyton30419822017-05-12 18:01:32 +00004763
Jonathan Peyton94a114f2017-10-20 19:30:57 +00004764 KA_TRACE(100,
4765 ("__kmp_partition_places: spread: T#%d(%d:%d) place %d "
4766 "partition = [%d,%d], spacing = %.4f\n",
4767 __kmp_gtid_from_thread(team->t.t_threads[f]),
4768 team->t.t_id, f, th->th.th_new_place,
4769 th->th.th_first_place, th->th.th_last_place, spacing));
Paul Osmialowskia0162792017-08-10 23:04:11 +00004770 }
Jonathan Peyton30419822017-05-12 18:01:32 +00004771 }
Jonathan Peyton30419822017-05-12 18:01:32 +00004772 }
4773 KMP_DEBUG_ASSERT(update_master_only || place == masters_place);
4774 } else {
4775 int S, rem, gap, s_count;
4776 S = n_th / n_places;
4777 s_count = 0;
4778 rem = n_th - (S * n_places);
4779 gap = rem > 0 ? n_places / rem : n_places;
4780 int place = masters_place;
4781 int gap_ct = gap;
4782 thidx = n_th;
4783 if (update_master_only == 1)
4784 thidx = 1;
4785 for (f = 0; f < thidx; f++) {
4786 kmp_info_t *th = team->t.t_threads[f];
4787 KMP_DEBUG_ASSERT(th != NULL);
4788
4789 th->th.th_first_place = place;
4790 th->th.th_last_place = place;
4791 th->th.th_new_place = place;
4792 s_count++;
4793
4794 if ((s_count == S) && rem && (gap_ct == gap)) {
4795 // do nothing, add an extra thread to place on next iteration
4796 } else if ((s_count == S + 1) && rem && (gap_ct == gap)) {
4797 // we added an extra thread to this place; move on to next place
4798 if (place == last_place) {
4799 place = first_place;
4800 } else if (place == (int)(__kmp_affinity_num_masks - 1)) {
4801 place = 0;
4802 } else {
4803 place++;
4804 }
4805 s_count = 0;
4806 gap_ct = 1;
4807 rem--;
4808 } else if (s_count == S) { // place is full; don't add extra thread
4809 if (place == last_place) {
4810 place = first_place;
4811 } else if (place == (int)(__kmp_affinity_num_masks - 1)) {
4812 place = 0;
4813 } else {
4814 place++;
4815 }
4816 gap_ct++;
4817 s_count = 0;
4818 }
4819
4820 KA_TRACE(100, ("__kmp_partition_places: spread: T#%d(%d:%d) place %d "
4821 "partition = [%d,%d]\n",
4822 __kmp_gtid_from_thread(team->t.t_threads[f]),
4823 team->t.t_id, f, th->th.th_new_place,
4824 th->th.th_first_place, th->th.th_last_place));
4825 }
4826 KMP_DEBUG_ASSERT(update_master_only || place == masters_place);
4827 }
4828 } break;
4829
4830 default:
4831 break;
4832 }
4833
4834 KA_TRACE(20, ("__kmp_partition_places: exit T#%d\n", team->t.t_id));
Jim Cownie5e8470a2013-09-27 10:38:44 +00004835}
4836
Alp Toker98758b02014-03-02 04:12:06 +00004837#endif /* OMP_40_ENABLED && KMP_AFFINITY_SUPPORTED */
Jim Cownie5e8470a2013-09-27 10:38:44 +00004838
Jonathan Peyton30419822017-05-12 18:01:32 +00004839/* allocate a new team data structure to use. take one off of the free pool if
4840 available */
Jim Cownie5e8470a2013-09-27 10:38:44 +00004841kmp_team_t *
Jonathan Peyton30419822017-05-12 18:01:32 +00004842__kmp_allocate_team(kmp_root_t *root, int new_nproc, int max_nproc,
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00004843#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00004844 ompt_data_t ompt_parallel_data,
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00004845#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00004846#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00004847 kmp_proc_bind_t new_proc_bind,
Jim Cownie5e8470a2013-09-27 10:38:44 +00004848#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00004849 kmp_internal_control_t *new_icvs,
4850 int argc USE_NESTED_HOT_ARG(kmp_info_t *master)) {
4851 KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(KMP_allocate_team);
4852 int f;
4853 kmp_team_t *team;
4854 int use_hot_team = !root->r.r_active;
4855 int level = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00004856
Jonathan Peyton30419822017-05-12 18:01:32 +00004857 KA_TRACE(20, ("__kmp_allocate_team: called\n"));
4858 KMP_DEBUG_ASSERT(new_nproc >= 1 && argc >= 0);
4859 KMP_DEBUG_ASSERT(max_nproc >= new_nproc);
4860 KMP_MB();
Jim Cownie5e8470a2013-09-27 10:38:44 +00004861
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004862#if KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00004863 kmp_hot_team_ptr_t *hot_teams;
4864 if (master) {
4865 team = master->th.th_team;
4866 level = team->t.t_active_level;
4867 if (master->th.th_teams_microtask) { // in teams construct?
4868 if (master->th.th_teams_size.nteams > 1 &&
4869 ( // #teams > 1
4870 team->t.t_pkfn ==
4871 (microtask_t)__kmp_teams_master || // inner fork of the teams
4872 master->th.th_teams_level <
4873 team->t.t_level)) { // or nested parallel inside the teams
4874 ++level; // not increment if #teams==1, or for outer fork of the teams;
4875 // increment otherwise
4876 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004877 }
Jonathan Peyton30419822017-05-12 18:01:32 +00004878 hot_teams = master->th.th_hot_teams;
4879 if (level < __kmp_hot_teams_max_level && hot_teams &&
4880 hot_teams[level]
4881 .hot_team) { // hot team has already been allocated for given level
4882 use_hot_team = 1;
4883 } else {
4884 use_hot_team = 0;
4885 }
4886 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004887#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00004888 // Optimization to use a "hot" team
4889 if (use_hot_team && new_nproc > 1) {
4890 KMP_DEBUG_ASSERT(new_nproc == max_nproc);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004891#if KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00004892 team = hot_teams[level].hot_team;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004893#else
Jonathan Peyton30419822017-05-12 18:01:32 +00004894 team = root->r.r_hot_team;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004895#endif
4896#if KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00004897 if (__kmp_tasking_mode != tskm_immediate_exec) {
4898 KA_TRACE(20, ("__kmp_allocate_team: hot team task_team[0] = %p "
4899 "task_team[1] = %p before reinit\n",
4900 team->t.t_task_team[0], team->t.t_task_team[1]));
4901 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00004902#endif
4903
Jonathan Peyton30419822017-05-12 18:01:32 +00004904 // Has the number of threads changed?
4905 /* Let's assume the most common case is that the number of threads is
4906 unchanged, and put that case first. */
4907 if (team->t.t_nproc == new_nproc) { // Check changes in number of threads
4908 KA_TRACE(20, ("__kmp_allocate_team: reusing hot team\n"));
4909 // This case can mean that omp_set_num_threads() was called and the hot
Jonathan Peyton642688b2017-06-01 16:46:36 +00004910 // team size was already reduced, so we check the special flag
Jonathan Peyton30419822017-05-12 18:01:32 +00004911 if (team->t.t_size_changed == -1) {
4912 team->t.t_size_changed = 1;
4913 } else {
4914 KMP_CHECK_UPDATE(team->t.t_size_changed, 0);
4915 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004916
Jonathan Peyton30419822017-05-12 18:01:32 +00004917 // TODO???: team->t.t_max_active_levels = new_max_active_levels;
4918 kmp_r_sched_t new_sched = new_icvs->sched;
Jonathan Peytonba55a7b2017-11-29 22:47:52 +00004919 // set master's schedule as new run-time schedule
4920 KMP_CHECK_UPDATE(team->t.t_sched.sched, new_sched.sched);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004921
Jonathan Peyton30419822017-05-12 18:01:32 +00004922 __kmp_reinitialize_team(team, new_icvs,
4923 root->r.r_uber_thread->th.th_ident);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004924
Jonathan Peyton30419822017-05-12 18:01:32 +00004925 KF_TRACE(10, ("__kmp_allocate_team2: T#%d, this_thread=%p team=%p\n", 0,
4926 team->t.t_threads[0], team));
4927 __kmp_push_current_task_to_thread(team->t.t_threads[0], team, 0);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004928
4929#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00004930#if KMP_AFFINITY_SUPPORTED
4931 if ((team->t.t_size_changed == 0) &&
4932 (team->t.t_proc_bind == new_proc_bind)) {
4933 if (new_proc_bind == proc_bind_spread) {
4934 __kmp_partition_places(
4935 team, 1); // add flag to update only master for spread
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004936 }
Jonathan Peyton30419822017-05-12 18:01:32 +00004937 KA_TRACE(200, ("__kmp_allocate_team: reusing hot team #%d bindings: "
4938 "proc_bind = %d, partition = [%d,%d]\n",
4939 team->t.t_id, new_proc_bind, team->t.t_first_place,
4940 team->t.t_last_place));
4941 } else {
4942 KMP_CHECK_UPDATE(team->t.t_proc_bind, new_proc_bind);
4943 __kmp_partition_places(team);
4944 }
4945#else
4946 KMP_CHECK_UPDATE(team->t.t_proc_bind, new_proc_bind);
4947#endif /* KMP_AFFINITY_SUPPORTED */
4948#endif /* OMP_40_ENABLED */
4949 } else if (team->t.t_nproc > new_nproc) {
4950 KA_TRACE(20,
4951 ("__kmp_allocate_team: decreasing hot team thread count to %d\n",
4952 new_nproc));
Jim Cownie5e8470a2013-09-27 10:38:44 +00004953
Jonathan Peyton30419822017-05-12 18:01:32 +00004954 team->t.t_size_changed = 1;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004955#if KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00004956 if (__kmp_hot_teams_mode == 0) {
4957 // AC: saved number of threads should correspond to team's value in this
4958 // mode, can be bigger in mode 1, when hot team has threads in reserve
4959 KMP_DEBUG_ASSERT(hot_teams[level].hot_team_nth == team->t.t_nproc);
4960 hot_teams[level].hot_team_nth = new_nproc;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004961#endif // KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00004962 /* release the extra threads we don't need any more */
4963 for (f = new_nproc; f < team->t.t_nproc; f++) {
4964 KMP_DEBUG_ASSERT(team->t.t_threads[f]);
4965 if (__kmp_tasking_mode != tskm_immediate_exec) {
4966 // When decreasing team size, threads no longer in the team should
4967 // unref task team.
4968 team->t.t_threads[f]->th.th_task_team = NULL;
4969 }
4970 __kmp_free_thread(team->t.t_threads[f]);
4971 team->t.t_threads[f] = NULL;
4972 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004973#if KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00004974 } // (__kmp_hot_teams_mode == 0)
4975 else {
4976 // When keeping extra threads in team, switch threads to wait on own
4977 // b_go flag
4978 for (f = new_nproc; f < team->t.t_nproc; ++f) {
4979 KMP_DEBUG_ASSERT(team->t.t_threads[f]);
4980 kmp_balign_t *balign = team->t.t_threads[f]->th.th_bar;
4981 for (int b = 0; b < bs_last_barrier; ++b) {
4982 if (balign[b].bb.wait_flag == KMP_BARRIER_PARENT_FLAG) {
4983 balign[b].bb.wait_flag = KMP_BARRIER_SWITCH_TO_OWN_FLAG;
Andrey Churbanovd6e1d7e2016-08-11 13:04:00 +00004984 }
Jonathan Peyton30419822017-05-12 18:01:32 +00004985 KMP_CHECK_UPDATE(balign[b].bb.leaf_kids, 0);
4986 }
4987 }
4988 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00004989#endif // KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00004990 team->t.t_nproc = new_nproc;
4991 // TODO???: team->t.t_max_active_levels = new_max_active_levels;
Jonathan Peytonba55a7b2017-11-29 22:47:52 +00004992 KMP_CHECK_UPDATE(team->t.t_sched.sched, new_icvs->sched.sched);
Jonathan Peyton30419822017-05-12 18:01:32 +00004993 __kmp_reinitialize_team(team, new_icvs,
4994 root->r.r_uber_thread->th.th_ident);
Jim Cownie5e8470a2013-09-27 10:38:44 +00004995
Jonathan Peyton30419822017-05-12 18:01:32 +00004996 /* update the remaining threads */
4997 for (f = 0; f < new_nproc; ++f) {
4998 team->t.t_threads[f]->th.th_team_nproc = new_nproc;
4999 }
5000 // restore the current task state of the master thread: should be the
5001 // implicit task
5002 KF_TRACE(10, ("__kmp_allocate_team: T#%d, this_thread=%p team=%p\n", 0,
5003 team->t.t_threads[0], team));
Jim Cownie5e8470a2013-09-27 10:38:44 +00005004
Jonathan Peyton30419822017-05-12 18:01:32 +00005005 __kmp_push_current_task_to_thread(team->t.t_threads[0], team, 0);
Jim Cownie5e8470a2013-09-27 10:38:44 +00005006
5007#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00005008 for (f = 0; f < team->t.t_nproc; f++) {
5009 KMP_DEBUG_ASSERT(team->t.t_threads[f] &&
5010 team->t.t_threads[f]->th.th_team_nproc ==
5011 team->t.t_nproc);
5012 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00005013#endif
5014
5015#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00005016 KMP_CHECK_UPDATE(team->t.t_proc_bind, new_proc_bind);
5017#if KMP_AFFINITY_SUPPORTED
5018 __kmp_partition_places(team);
Jim Cownie5e8470a2013-09-27 10:38:44 +00005019#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00005020#endif
5021 } else { // team->t.t_nproc < new_nproc
Alp Toker98758b02014-03-02 04:12:06 +00005022#if KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED
Jonathan Peyton30419822017-05-12 18:01:32 +00005023 kmp_affin_mask_t *old_mask;
5024 if (KMP_AFFINITY_CAPABLE()) {
5025 KMP_CPU_ALLOC(old_mask);
5026 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00005027#endif
5028
Jonathan Peyton30419822017-05-12 18:01:32 +00005029 KA_TRACE(20,
5030 ("__kmp_allocate_team: increasing hot team thread count to %d\n",
5031 new_nproc));
Jim Cownie5e8470a2013-09-27 10:38:44 +00005032
Jonathan Peyton30419822017-05-12 18:01:32 +00005033 team->t.t_size_changed = 1;
Jim Cownie5e8470a2013-09-27 10:38:44 +00005034
Jim Cownie4cc4bb42014-10-07 16:25:50 +00005035#if KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00005036 int avail_threads = hot_teams[level].hot_team_nth;
5037 if (new_nproc < avail_threads)
5038 avail_threads = new_nproc;
5039 kmp_info_t **other_threads = team->t.t_threads;
5040 for (f = team->t.t_nproc; f < avail_threads; ++f) {
5041 // Adjust barrier data of reserved threads (if any) of the team
5042 // Other data will be set in __kmp_initialize_info() below.
Jim Cownie5e8470a2013-09-27 10:38:44 +00005043 int b;
Jonathan Peyton30419822017-05-12 18:01:32 +00005044 kmp_balign_t *balign = other_threads[f]->th.th_bar;
5045 for (b = 0; b < bs_last_barrier; ++b) {
5046 balign[b].bb.b_arrived = team->t.t_bar[b].b_arrived;
5047 KMP_DEBUG_ASSERT(balign[b].bb.wait_flag != KMP_BARRIER_PARENT_FLAG);
Jonathan Peyton8fbb49a2015-07-09 18:16:58 +00005048#if USE_DEBUGGER
Jonathan Peyton30419822017-05-12 18:01:32 +00005049 balign[b].bb.b_worker_arrived = team->t.t_bar[b].b_team_arrived;
Jonathan Peyton8fbb49a2015-07-09 18:16:58 +00005050#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00005051 }
Jonathan Peyton30419822017-05-12 18:01:32 +00005052 }
5053 if (hot_teams[level].hot_team_nth >= new_nproc) {
5054 // we have all needed threads in reserve, no need to allocate any
5055 // this only possible in mode 1, cannot have reserved threads in mode 0
5056 KMP_DEBUG_ASSERT(__kmp_hot_teams_mode == 1);
5057 team->t.t_nproc = new_nproc; // just get reserved threads involved
5058 } else {
5059 // we may have some threads in reserve, but not enough
5060 team->t.t_nproc =
5061 hot_teams[level]
5062 .hot_team_nth; // get reserved threads involved if any
5063 hot_teams[level].hot_team_nth = new_nproc; // adjust hot team max size
5064#endif // KMP_NESTED_HOT_TEAMS
5065 if (team->t.t_max_nproc < new_nproc) {
5066 /* reallocate larger arrays */
5067 __kmp_reallocate_team_arrays(team, new_nproc);
5068 __kmp_reinitialize_team(team, new_icvs, NULL);
5069 }
5070
5071#if KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED
5072 /* Temporarily set full mask for master thread before creation of
5073 workers. The reason is that workers inherit the affinity from master,
5074 so if a lot of workers are created on the single core quickly, they
5075 don't get a chance to set their own affinity for a long time. */
5076 __kmp_set_thread_affinity_mask_full_tmp(old_mask);
5077#endif
5078
5079 /* allocate new threads for the hot team */
5080 for (f = team->t.t_nproc; f < new_nproc; f++) {
5081 kmp_info_t *new_worker = __kmp_allocate_thread(root, team, f);
5082 KMP_DEBUG_ASSERT(new_worker);
5083 team->t.t_threads[f] = new_worker;
5084
5085 KA_TRACE(20,
5086 ("__kmp_allocate_team: team %d init T#%d arrived: "
5087 "join=%llu, plain=%llu\n",
5088 team->t.t_id, __kmp_gtid_from_tid(f, team), team->t.t_id, f,
5089 team->t.t_bar[bs_forkjoin_barrier].b_arrived,
5090 team->t.t_bar[bs_plain_barrier].b_arrived));
5091
5092 { // Initialize barrier data for new threads.
5093 int b;
5094 kmp_balign_t *balign = new_worker->th.th_bar;
5095 for (b = 0; b < bs_last_barrier; ++b) {
5096 balign[b].bb.b_arrived = team->t.t_bar[b].b_arrived;
5097 KMP_DEBUG_ASSERT(balign[b].bb.wait_flag !=
5098 KMP_BARRIER_PARENT_FLAG);
5099#if USE_DEBUGGER
5100 balign[b].bb.b_worker_arrived = team->t.t_bar[b].b_team_arrived;
5101#endif
5102 }
5103 }
5104 }
5105
5106#if KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED
5107 if (KMP_AFFINITY_CAPABLE()) {
5108 /* Restore initial master thread's affinity mask */
5109 __kmp_set_system_affinity(old_mask, TRUE);
5110 KMP_CPU_FREE(old_mask);
5111 }
5112#endif
5113#if KMP_NESTED_HOT_TEAMS
5114 } // end of check of t_nproc vs. new_nproc vs. hot_team_nth
5115#endif // KMP_NESTED_HOT_TEAMS
5116 /* make sure everyone is syncronized */
5117 int old_nproc = team->t.t_nproc; // save old value and use to update only
5118 // new threads below
5119 __kmp_initialize_team(team, new_nproc, new_icvs,
5120 root->r.r_uber_thread->th.th_ident);
5121
5122 /* reinitialize the threads */
5123 KMP_DEBUG_ASSERT(team->t.t_nproc == new_nproc);
5124 for (f = 0; f < team->t.t_nproc; ++f)
5125 __kmp_initialize_info(team->t.t_threads[f], team, f,
5126 __kmp_gtid_from_tid(f, team));
5127 if (level) { // set th_task_state for new threads in nested hot team
5128 // __kmp_initialize_info() no longer zeroes th_task_state, so we should
5129 // only need to set the th_task_state for the new threads. th_task_state
5130 // for master thread will not be accurate until after this in
5131 // __kmp_fork_call(), so we look to the master's memo_stack to get the
5132 // correct value.
5133 for (f = old_nproc; f < team->t.t_nproc; ++f)
5134 team->t.t_threads[f]->th.th_task_state =
5135 team->t.t_threads[0]->th.th_task_state_memo_stack[level];
5136 } else { // set th_task_state for new threads in non-nested hot team
5137 int old_state =
5138 team->t.t_threads[0]->th.th_task_state; // copy master's state
5139 for (f = old_nproc; f < team->t.t_nproc; ++f)
5140 team->t.t_threads[f]->th.th_task_state = old_state;
5141 }
5142
5143#ifdef KMP_DEBUG
5144 for (f = 0; f < team->t.t_nproc; ++f) {
5145 KMP_DEBUG_ASSERT(team->t.t_threads[f] &&
5146 team->t.t_threads[f]->th.th_team_nproc ==
5147 team->t.t_nproc);
5148 }
5149#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00005150
5151#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00005152 KMP_CHECK_UPDATE(team->t.t_proc_bind, new_proc_bind);
5153#if KMP_AFFINITY_SUPPORTED
5154 __kmp_partition_places(team);
5155#endif
5156#endif
5157 } // Check changes in number of threads
5158
5159#if OMP_40_ENABLED
5160 kmp_info_t *master = team->t.t_threads[0];
5161 if (master->th.th_teams_microtask) {
5162 for (f = 1; f < new_nproc; ++f) {
5163 // propagate teams construct specific info to workers
5164 kmp_info_t *thr = team->t.t_threads[f];
5165 thr->th.th_teams_microtask = master->th.th_teams_microtask;
5166 thr->th.th_teams_level = master->th.th_teams_level;
5167 thr->th.th_teams_size = master->th.th_teams_size;
5168 }
5169 }
5170#endif /* OMP_40_ENABLED */
5171#if KMP_NESTED_HOT_TEAMS
5172 if (level) {
5173 // Sync barrier state for nested hot teams, not needed for outermost hot
5174 // team.
5175 for (f = 1; f < new_nproc; ++f) {
5176 kmp_info_t *thr = team->t.t_threads[f];
5177 int b;
5178 kmp_balign_t *balign = thr->th.th_bar;
5179 for (b = 0; b < bs_last_barrier; ++b) {
5180 balign[b].bb.b_arrived = team->t.t_bar[b].b_arrived;
5181 KMP_DEBUG_ASSERT(balign[b].bb.wait_flag != KMP_BARRIER_PARENT_FLAG);
5182#if USE_DEBUGGER
5183 balign[b].bb.b_worker_arrived = team->t.t_bar[b].b_team_arrived;
5184#endif
5185 }
5186 }
5187 }
5188#endif // KMP_NESTED_HOT_TEAMS
5189
5190 /* reallocate space for arguments if necessary */
5191 __kmp_alloc_argv_entries(argc, team, TRUE);
5192 KMP_CHECK_UPDATE(team->t.t_argc, argc);
5193 // The hot team re-uses the previous task team,
5194 // if untouched during the previous release->gather phase.
5195
5196 KF_TRACE(10, (" hot_team = %p\n", team));
5197
5198#if KMP_DEBUG
5199 if (__kmp_tasking_mode != tskm_immediate_exec) {
5200 KA_TRACE(20, ("__kmp_allocate_team: hot team task_team[0] = %p "
5201 "task_team[1] = %p after reinit\n",
5202 team->t.t_task_team[0], team->t.t_task_team[1]));
5203 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00005204#endif
5205
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00005206#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00005207 __ompt_team_assign_id(team, ompt_parallel_data);
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00005208#endif
5209
Jim Cownie5e8470a2013-09-27 10:38:44 +00005210 KMP_MB();
5211
Jim Cownie5e8470a2013-09-27 10:38:44 +00005212 return team;
Jonathan Peyton30419822017-05-12 18:01:32 +00005213 }
5214
5215 /* next, let's try to take one from the team pool */
5216 KMP_MB();
Andrey Churbanovc47afcd2017-07-03 11:24:08 +00005217 for (team = CCAST(kmp_team_t *, __kmp_team_pool); (team);) {
Jonathan Peyton30419822017-05-12 18:01:32 +00005218 /* TODO: consider resizing undersized teams instead of reaping them, now
5219 that we have a resizing mechanism */
5220 if (team->t.t_max_nproc >= max_nproc) {
5221 /* take this team from the team pool */
5222 __kmp_team_pool = team->t.t_next_pool;
5223
5224 /* setup the team for fresh use */
5225 __kmp_initialize_team(team, new_nproc, new_icvs, NULL);
5226
5227 KA_TRACE(20, ("__kmp_allocate_team: setting task_team[0] %p and "
5228 "task_team[1] %p to NULL\n",
5229 &team->t.t_task_team[0], &team->t.t_task_team[1]));
5230 team->t.t_task_team[0] = NULL;
5231 team->t.t_task_team[1] = NULL;
5232
5233 /* reallocate space for arguments if necessary */
5234 __kmp_alloc_argv_entries(argc, team, TRUE);
5235 KMP_CHECK_UPDATE(team->t.t_argc, argc);
5236
5237 KA_TRACE(
5238 20, ("__kmp_allocate_team: team %d init arrived: join=%u, plain=%u\n",
5239 team->t.t_id, KMP_INIT_BARRIER_STATE, KMP_INIT_BARRIER_STATE));
5240 { // Initialize barrier data.
5241 int b;
5242 for (b = 0; b < bs_last_barrier; ++b) {
5243 team->t.t_bar[b].b_arrived = KMP_INIT_BARRIER_STATE;
5244#if USE_DEBUGGER
5245 team->t.t_bar[b].b_master_arrived = 0;
5246 team->t.t_bar[b].b_team_arrived = 0;
5247#endif
5248 }
5249 }
5250
5251#if OMP_40_ENABLED
5252 team->t.t_proc_bind = new_proc_bind;
5253#endif
5254
5255 KA_TRACE(20, ("__kmp_allocate_team: using team from pool %d.\n",
5256 team->t.t_id));
5257
5258#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00005259 __ompt_team_assign_id(team, ompt_parallel_data);
Jonathan Peyton30419822017-05-12 18:01:32 +00005260#endif
5261
5262 KMP_MB();
5263
5264 return team;
5265 }
5266
Jonathan Peyton94a114f2017-10-20 19:30:57 +00005267 /* reap team if it is too small, then loop back and check the next one */
5268 // not sure if this is wise, but, will be redone during the hot-teams
5269 // rewrite.
5270 /* TODO: Use technique to find the right size hot-team, don't reap them */
Jonathan Peyton30419822017-05-12 18:01:32 +00005271 team = __kmp_reap_team(team);
5272 __kmp_team_pool = team;
5273 }
5274
5275 /* nothing available in the pool, no matter, make a new team! */
5276 KMP_MB();
5277 team = (kmp_team_t *)__kmp_allocate(sizeof(kmp_team_t));
5278
5279 /* and set it up */
5280 team->t.t_max_nproc = max_nproc;
5281 /* NOTE well, for some reason allocating one big buffer and dividing it up
5282 seems to really hurt performance a lot on the P4, so, let's not use this */
5283 __kmp_allocate_team_arrays(team, max_nproc);
5284
5285 KA_TRACE(20, ("__kmp_allocate_team: making a new team\n"));
5286 __kmp_initialize_team(team, new_nproc, new_icvs, NULL);
5287
5288 KA_TRACE(20, ("__kmp_allocate_team: setting task_team[0] %p and task_team[1] "
5289 "%p to NULL\n",
5290 &team->t.t_task_team[0], &team->t.t_task_team[1]));
5291 team->t.t_task_team[0] = NULL; // to be removed, as __kmp_allocate zeroes
5292 // memory, no need to duplicate
5293 team->t.t_task_team[1] = NULL; // to be removed, as __kmp_allocate zeroes
5294 // memory, no need to duplicate
5295
5296 if (__kmp_storage_map) {
5297 __kmp_print_team_storage_map("team", team, team->t.t_id, new_nproc);
5298 }
5299
5300 /* allocate space for arguments */
5301 __kmp_alloc_argv_entries(argc, team, FALSE);
5302 team->t.t_argc = argc;
5303
5304 KA_TRACE(20,
5305 ("__kmp_allocate_team: team %d init arrived: join=%u, plain=%u\n",
5306 team->t.t_id, KMP_INIT_BARRIER_STATE, KMP_INIT_BARRIER_STATE));
5307 { // Initialize barrier data.
5308 int b;
5309 for (b = 0; b < bs_last_barrier; ++b) {
5310 team->t.t_bar[b].b_arrived = KMP_INIT_BARRIER_STATE;
5311#if USE_DEBUGGER
5312 team->t.t_bar[b].b_master_arrived = 0;
5313 team->t.t_bar[b].b_team_arrived = 0;
5314#endif
5315 }
5316 }
5317
5318#if OMP_40_ENABLED
5319 team->t.t_proc_bind = new_proc_bind;
5320#endif
5321
5322#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00005323 __ompt_team_assign_id(team, ompt_parallel_data);
Jonathan Peyton30419822017-05-12 18:01:32 +00005324 team->t.ompt_serialized_team_info = NULL;
5325#endif
5326
5327 KMP_MB();
5328
5329 KA_TRACE(20, ("__kmp_allocate_team: done creating a new team %d.\n",
5330 team->t.t_id));
5331
5332 return team;
Jim Cownie5e8470a2013-09-27 10:38:44 +00005333}
5334
5335/* TODO implement hot-teams at all levels */
5336/* TODO implement lazy thread release on demand (disband request) */
5337
5338/* free the team. return it to the team pool. release all the threads
5339 * associated with it */
Jonathan Peyton30419822017-05-12 18:01:32 +00005340void __kmp_free_team(kmp_root_t *root,
5341 kmp_team_t *team USE_NESTED_HOT_ARG(kmp_info_t *master)) {
5342 int f;
5343 KA_TRACE(20, ("__kmp_free_team: T#%d freeing team %d\n", __kmp_get_gtid(),
5344 team->t.t_id));
Jim Cownie5e8470a2013-09-27 10:38:44 +00005345
Jonathan Peyton30419822017-05-12 18:01:32 +00005346 /* verify state */
5347 KMP_DEBUG_ASSERT(root);
5348 KMP_DEBUG_ASSERT(team);
5349 KMP_DEBUG_ASSERT(team->t.t_nproc <= team->t.t_max_nproc);
5350 KMP_DEBUG_ASSERT(team->t.t_threads);
Jim Cownie5e8470a2013-09-27 10:38:44 +00005351
Jonathan Peyton30419822017-05-12 18:01:32 +00005352 int use_hot_team = team == root->r.r_hot_team;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00005353#if KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00005354 int level;
5355 kmp_hot_team_ptr_t *hot_teams;
5356 if (master) {
5357 level = team->t.t_active_level - 1;
5358 if (master->th.th_teams_microtask) { // in teams construct?
5359 if (master->th.th_teams_size.nteams > 1) {
5360 ++level; // level was not increased in teams construct for
5361 // team_of_masters
5362 }
5363 if (team->t.t_pkfn != (microtask_t)__kmp_teams_master &&
5364 master->th.th_teams_level == team->t.t_level) {
5365 ++level; // level was not increased in teams construct for
5366 // team_of_workers before the parallel
5367 } // team->t.t_level will be increased inside parallel
Jim Cownie4cc4bb42014-10-07 16:25:50 +00005368 }
Jonathan Peyton30419822017-05-12 18:01:32 +00005369 hot_teams = master->th.th_hot_teams;
5370 if (level < __kmp_hot_teams_max_level) {
5371 KMP_DEBUG_ASSERT(team == hot_teams[level].hot_team);
5372 use_hot_team = 1;
5373 }
5374 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00005375#endif // KMP_NESTED_HOT_TEAMS
5376
Jonathan Peyton30419822017-05-12 18:01:32 +00005377 /* team is done working */
5378 TCW_SYNC_PTR(team->t.t_pkfn,
5379 NULL); // Important for Debugging Support Library.
Jonathan Peyton37e2ef52018-07-09 17:36:22 +00005380#if KMP_OS_WINDOWS
Jonathan Peyton30419822017-05-12 18:01:32 +00005381 team->t.t_copyin_counter = 0; // init counter for possible reuse
Jonathan Peyton37e2ef52018-07-09 17:36:22 +00005382#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00005383 // Do not reset pointer to parent team to NULL for hot teams.
Jim Cownie5e8470a2013-09-27 10:38:44 +00005384
Jonathan Peyton30419822017-05-12 18:01:32 +00005385 /* if we are non-hot team, release our threads */
5386 if (!use_hot_team) {
5387 if (__kmp_tasking_mode != tskm_immediate_exec) {
5388 // Wait for threads to reach reapable state
5389 for (f = 1; f < team->t.t_nproc; ++f) {
5390 KMP_DEBUG_ASSERT(team->t.t_threads[f]);
5391 kmp_info_t *th = team->t.t_threads[f];
5392 volatile kmp_uint32 *state = &th->th.th_reap_state;
5393 while (*state != KMP_SAFE_TO_REAP) {
Andrey Churbanov581490e2017-02-06 18:53:32 +00005394#if KMP_OS_WINDOWS
Jonathan Peyton30419822017-05-12 18:01:32 +00005395 // On Windows a thread can be killed at any time, check this
5396 DWORD ecode;
5397 if (!__kmp_is_thread_alive(th, &ecode)) {
5398 *state = KMP_SAFE_TO_REAP; // reset the flag for dead thread
5399 break;
5400 }
Andrey Churbanov581490e2017-02-06 18:53:32 +00005401#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00005402 // first check if thread is sleeping
5403 kmp_flag_64 fl(&th->th.th_bar[bs_forkjoin_barrier].bb.b_go, th);
5404 if (fl.is_sleeping())
5405 fl.resume(__kmp_gtid_from_thread(th));
5406 KMP_CPU_PAUSE();
5407 }
5408 }
Andrey Churbanov581490e2017-02-06 18:53:32 +00005409
Jonathan Peyton30419822017-05-12 18:01:32 +00005410 // Delete task teams
5411 int tt_idx;
5412 for (tt_idx = 0; tt_idx < 2; ++tt_idx) {
5413 kmp_task_team_t *task_team = team->t.t_task_team[tt_idx];
5414 if (task_team != NULL) {
5415 for (f = 0; f < team->t.t_nproc;
5416 ++f) { // Have all threads unref task teams
5417 team->t.t_threads[f]->th.th_task_team = NULL;
5418 }
5419 KA_TRACE(
5420 20,
5421 ("__kmp_free_team: T#%d deactivating task_team %p on team %d\n",
5422 __kmp_get_gtid(), task_team, team->t.t_id));
Jonathan Peytonbaaccfa2015-11-16 22:48:41 +00005423#if KMP_NESTED_HOT_TEAMS
Jonathan Peyton30419822017-05-12 18:01:32 +00005424 __kmp_free_task_team(master, task_team);
Jonathan Peytonbaaccfa2015-11-16 22:48:41 +00005425#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00005426 team->t.t_task_team[tt_idx] = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +00005427 }
Jonathan Peyton30419822017-05-12 18:01:32 +00005428 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00005429 }
5430
Jonathan Peyton30419822017-05-12 18:01:32 +00005431 // Reset pointer to parent team only for non-hot teams.
5432 team->t.t_parent = NULL;
5433 team->t.t_level = 0;
5434 team->t.t_active_level = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00005435
Jonathan Peyton30419822017-05-12 18:01:32 +00005436 /* free the worker threads */
5437 for (f = 1; f < team->t.t_nproc; ++f) {
5438 KMP_DEBUG_ASSERT(team->t.t_threads[f]);
5439 __kmp_free_thread(team->t.t_threads[f]);
5440 team->t.t_threads[f] = NULL;
5441 }
5442
5443 /* put the team back in the team pool */
5444 /* TODO limit size of team pool, call reap_team if pool too large */
Andrey Churbanovc47afcd2017-07-03 11:24:08 +00005445 team->t.t_next_pool = CCAST(kmp_team_t *, __kmp_team_pool);
Jonathan Peyton30419822017-05-12 18:01:32 +00005446 __kmp_team_pool = (volatile kmp_team_t *)team;
5447 }
5448
5449 KMP_MB();
5450}
Jim Cownie5e8470a2013-09-27 10:38:44 +00005451
5452/* reap the team. destroy it, reclaim all its resources and free its memory */
Jonathan Peyton30419822017-05-12 18:01:32 +00005453kmp_team_t *__kmp_reap_team(kmp_team_t *team) {
5454 kmp_team_t *next_pool = team->t.t_next_pool;
Jim Cownie5e8470a2013-09-27 10:38:44 +00005455
Jonathan Peyton30419822017-05-12 18:01:32 +00005456 KMP_DEBUG_ASSERT(team);
5457 KMP_DEBUG_ASSERT(team->t.t_dispatch);
5458 KMP_DEBUG_ASSERT(team->t.t_disp_buffer);
5459 KMP_DEBUG_ASSERT(team->t.t_threads);
5460 KMP_DEBUG_ASSERT(team->t.t_argv);
Jim Cownie5e8470a2013-09-27 10:38:44 +00005461
Jonathan Peyton30419822017-05-12 18:01:32 +00005462 /* TODO clean the threads that are a part of this? */
Jim Cownie5e8470a2013-09-27 10:38:44 +00005463
Jonathan Peyton30419822017-05-12 18:01:32 +00005464 /* free stuff */
5465 __kmp_free_team_arrays(team);
5466 if (team->t.t_argv != &team->t.t_inline_argv[0])
5467 __kmp_free((void *)team->t.t_argv);
5468 __kmp_free(team);
Jim Cownie5e8470a2013-09-27 10:38:44 +00005469
Jonathan Peyton30419822017-05-12 18:01:32 +00005470 KMP_MB();
5471 return next_pool;
Jim Cownie5e8470a2013-09-27 10:38:44 +00005472}
5473
Jim Cownie5e8470a2013-09-27 10:38:44 +00005474// Free the thread. Don't reap it, just place it on the pool of available
5475// threads.
5476//
5477// Changes for Quad issue 527845: We need a predictable OMP tid <-> gtid
5478// binding for the affinity mechanism to be useful.
5479//
5480// Now, we always keep the free list (__kmp_thread_pool) sorted by gtid.
5481// However, we want to avoid a potential performance problem by always
5482// scanning through the list to find the correct point at which to insert
5483// the thread (potential N**2 behavior). To do this we keep track of the
5484// last place a thread struct was inserted (__kmp_thread_pool_insert_pt).
5485// With single-level parallelism, threads will always be added to the tail
5486// of the list, kept track of by __kmp_thread_pool_insert_pt. With nested
5487// parallelism, all bets are off and we may need to scan through the entire
5488// free list.
5489//
5490// This change also has a potentially large performance benefit, for some
5491// applications. Previously, as threads were freed from the hot team, they
5492// would be placed back on the free list in inverse order. If the hot team
5493// grew back to it's original size, then the freed thread would be placed
5494// back on the hot team in reverse order. This could cause bad cache
5495// locality problems on programs where the size of the hot team regularly
5496// grew and shrunk.
5497//
5498// Now, for single-level parallelism, the OMP tid is alway == gtid.
Jonathan Peyton30419822017-05-12 18:01:32 +00005499void __kmp_free_thread(kmp_info_t *this_th) {
5500 int gtid;
5501 kmp_info_t **scan;
Jonathan Peytonf4392462017-07-27 20:58:41 +00005502 kmp_root_t *root = this_th->th.th_root;
Jim Cownie5e8470a2013-09-27 10:38:44 +00005503
Jonathan Peyton30419822017-05-12 18:01:32 +00005504 KA_TRACE(20, ("__kmp_free_thread: T#%d putting T#%d back on free pool.\n",
5505 __kmp_get_gtid(), this_th->th.th_info.ds.ds_gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00005506
Jonathan Peyton30419822017-05-12 18:01:32 +00005507 KMP_DEBUG_ASSERT(this_th);
Jim Cownie5e8470a2013-09-27 10:38:44 +00005508
Jonathan Peyton30419822017-05-12 18:01:32 +00005509 // When moving thread to pool, switch thread to wait on own b_go flag, and
5510 // uninitialized (NULL team).
5511 int b;
5512 kmp_balign_t *balign = this_th->th.th_bar;
5513 for (b = 0; b < bs_last_barrier; ++b) {
5514 if (balign[b].bb.wait_flag == KMP_BARRIER_PARENT_FLAG)
5515 balign[b].bb.wait_flag = KMP_BARRIER_SWITCH_TO_OWN_FLAG;
5516 balign[b].bb.team = NULL;
5517 balign[b].bb.leaf_kids = 0;
5518 }
5519 this_th->th.th_task_state = 0;
Andrey Churbanov3336aa02018-03-19 18:05:15 +00005520 this_th->th.th_reap_state = KMP_SAFE_TO_REAP;
Jonathan Peyton30419822017-05-12 18:01:32 +00005521
5522 /* put thread back on the free pool */
5523 TCW_PTR(this_th->th.th_team, NULL);
5524 TCW_PTR(this_th->th.th_root, NULL);
5525 TCW_PTR(this_th->th.th_dispatch, NULL); /* NOT NEEDED */
5526
Jonathan Peytonbff8ded2018-01-10 18:24:09 +00005527 /* If the implicit task assigned to this thread can be used by other threads
5528 * -> multiple threads can share the data and try to free the task at
5529 * __kmp_reap_thread at exit. This duplicate use of the task data can happen
5530 * with higher probability when hot team is disabled but can occurs even when
5531 * the hot team is enabled */
5532 __kmp_free_implicit_task(this_th);
5533 this_th->th.th_current_task = NULL;
5534
Jonathan Peyton30419822017-05-12 18:01:32 +00005535 // If the __kmp_thread_pool_insert_pt is already past the new insert
5536 // point, then we need to re-scan the entire list.
5537 gtid = this_th->th.th_info.ds.ds_gtid;
5538 if (__kmp_thread_pool_insert_pt != NULL) {
5539 KMP_DEBUG_ASSERT(__kmp_thread_pool != NULL);
5540 if (__kmp_thread_pool_insert_pt->th.th_info.ds.ds_gtid > gtid) {
5541 __kmp_thread_pool_insert_pt = NULL;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00005542 }
Jonathan Peyton30419822017-05-12 18:01:32 +00005543 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00005544
Jonathan Peyton30419822017-05-12 18:01:32 +00005545 // Scan down the list to find the place to insert the thread.
5546 // scan is the address of a link in the list, possibly the address of
5547 // __kmp_thread_pool itself.
5548 //
5549 // In the absence of nested parallism, the for loop will have 0 iterations.
5550 if (__kmp_thread_pool_insert_pt != NULL) {
5551 scan = &(__kmp_thread_pool_insert_pt->th.th_next_pool);
5552 } else {
Andrey Churbanovc47afcd2017-07-03 11:24:08 +00005553 scan = CCAST(kmp_info_t **, &__kmp_thread_pool);
Jonathan Peyton30419822017-05-12 18:01:32 +00005554 }
5555 for (; (*scan != NULL) && ((*scan)->th.th_info.ds.ds_gtid < gtid);
5556 scan = &((*scan)->th.th_next_pool))
5557 ;
Jim Cownie5e8470a2013-09-27 10:38:44 +00005558
Jonathan Peyton30419822017-05-12 18:01:32 +00005559 // Insert the new element on the list, and set __kmp_thread_pool_insert_pt
5560 // to its address.
5561 TCW_PTR(this_th->th.th_next_pool, *scan);
5562 __kmp_thread_pool_insert_pt = *scan = this_th;
5563 KMP_DEBUG_ASSERT((this_th->th.th_next_pool == NULL) ||
5564 (this_th->th.th_info.ds.ds_gtid <
5565 this_th->th.th_next_pool->th.th_info.ds.ds_gtid));
5566 TCW_4(this_th->th.th_in_pool, TRUE);
5567 __kmp_thread_pool_nth++;
Jim Cownie5e8470a2013-09-27 10:38:44 +00005568
Jonathan Peyton30419822017-05-12 18:01:32 +00005569 TCW_4(__kmp_nth, __kmp_nth - 1);
Jonathan Peytonf4392462017-07-27 20:58:41 +00005570 root->r.r_cg_nthreads--;
Jim Cownie5e8470a2013-09-27 10:38:44 +00005571
5572#ifdef KMP_ADJUST_BLOCKTIME
Jonathan Peyton30419822017-05-12 18:01:32 +00005573 /* Adjust blocktime back to user setting or default if necessary */
5574 /* Middle initialization might never have occurred */
5575 if (!__kmp_env_blocktime && (__kmp_avail_proc > 0)) {
5576 KMP_DEBUG_ASSERT(__kmp_avail_proc > 0);
5577 if (__kmp_nth <= __kmp_avail_proc) {
5578 __kmp_zero_bt = FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00005579 }
Jonathan Peyton30419822017-05-12 18:01:32 +00005580 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00005581#endif /* KMP_ADJUST_BLOCKTIME */
5582
Jonathan Peyton30419822017-05-12 18:01:32 +00005583 KMP_MB();
Jim Cownie5e8470a2013-09-27 10:38:44 +00005584}
5585
Jim Cownie5e8470a2013-09-27 10:38:44 +00005586/* ------------------------------------------------------------------------ */
5587
Jonathan Peyton30419822017-05-12 18:01:32 +00005588void *__kmp_launch_thread(kmp_info_t *this_thr) {
5589 int gtid = this_thr->th.th_info.ds.ds_gtid;
5590 /* void *stack_data;*/
5591 kmp_team_t *(*volatile pteam);
Jim Cownie5e8470a2013-09-27 10:38:44 +00005592
Jonathan Peyton30419822017-05-12 18:01:32 +00005593 KMP_MB();
5594 KA_TRACE(10, ("__kmp_launch_thread: T#%d start\n", gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00005595
Jonathan Peyton30419822017-05-12 18:01:32 +00005596 if (__kmp_env_consistency_check) {
5597 this_thr->th.th_cons = __kmp_allocate_cons_stack(gtid); // ATT: Memory leak?
5598 }
5599
5600#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00005601 ompt_data_t *thread_data;
5602 if (ompt_enabled.enabled) {
5603 thread_data = &(this_thr->th.ompt_thread_info.thread_data);
5604 thread_data->ptr = NULL;
5605
5606 this_thr->th.ompt_thread_info.state = omp_state_overhead;
Jonathan Peyton30419822017-05-12 18:01:32 +00005607 this_thr->th.ompt_thread_info.wait_id = 0;
Joachim Protze82e94a52017-11-01 10:08:30 +00005608 this_thr->th.ompt_thread_info.idle_frame = OMPT_GET_FRAME_ADDRESS(0);
5609 if (ompt_enabled.ompt_callback_thread_begin) {
5610 ompt_callbacks.ompt_callback(ompt_callback_thread_begin)(
5611 ompt_thread_worker, thread_data);
Jim Cownie5e8470a2013-09-27 10:38:44 +00005612 }
Jonathan Peyton30419822017-05-12 18:01:32 +00005613 }
5614#endif
5615
Joachim Protze82e94a52017-11-01 10:08:30 +00005616#if OMPT_SUPPORT
5617 if (ompt_enabled.enabled) {
5618 this_thr->th.ompt_thread_info.state = omp_state_idle;
5619 }
5620#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00005621 /* This is the place where threads wait for work */
5622 while (!TCR_4(__kmp_global.g.g_done)) {
5623 KMP_DEBUG_ASSERT(this_thr == __kmp_threads[gtid]);
5624 KMP_MB();
5625
5626 /* wait for work to do */
5627 KA_TRACE(20, ("__kmp_launch_thread: T#%d waiting for work\n", gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00005628
Jonathan Peyton30419822017-05-12 18:01:32 +00005629 /* No tid yet since not part of a team */
5630 __kmp_fork_barrier(gtid, KMP_GTID_DNE);
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_overhead;
Jonathan Peyton30419822017-05-12 18:01:32 +00005635 }
5636#endif
5637
5638 pteam = (kmp_team_t * (*))(&this_thr->th.th_team);
5639
5640 /* have we been allocated? */
5641 if (TCR_SYNC_PTR(*pteam) && !TCR_4(__kmp_global.g.g_done)) {
Jonathan Peyton30419822017-05-12 18:01:32 +00005642 /* we were just woken up, so run our new task */
5643 if (TCR_SYNC_PTR((*pteam)->t.t_pkfn) != NULL) {
5644 int rc;
5645 KA_TRACE(20,
5646 ("__kmp_launch_thread: T#%d(%d:%d) invoke microtask = %p\n",
5647 gtid, (*pteam)->t.t_id, __kmp_tid_from_gtid(gtid),
5648 (*pteam)->t.t_pkfn));
5649
5650 updateHWFPControl(*pteam);
5651
5652#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00005653 if (ompt_enabled.enabled) {
5654 this_thr->th.ompt_thread_info.state = omp_state_work_parallel;
Jonathan Peyton30419822017-05-12 18:01:32 +00005655 }
5656#endif
5657
Jonathan Peytonf0682ac2018-07-30 17:41:08 +00005658 rc = (*pteam)->t.t_invoke(gtid);
Jonathan Peyton30419822017-05-12 18:01:32 +00005659 KMP_ASSERT(rc);
5660
Jim Cownie5e8470a2013-09-27 10:38:44 +00005661 KMP_MB();
Jonathan Peyton30419822017-05-12 18:01:32 +00005662 KA_TRACE(20, ("__kmp_launch_thread: T#%d(%d:%d) done microtask = %p\n",
5663 gtid, (*pteam)->t.t_id, __kmp_tid_from_gtid(gtid),
5664 (*pteam)->t.t_pkfn));
5665 }
Joachim Protze82e94a52017-11-01 10:08:30 +00005666#if OMPT_SUPPORT
5667 if (ompt_enabled.enabled) {
5668 /* no frame set while outside task */
Joachim Protzec255ca72017-11-05 14:11:10 +00005669 __ompt_get_task_info_object(0)->frame.exit_frame = NULL;
Joachim Protze82e94a52017-11-01 10:08:30 +00005670
5671 this_thr->th.ompt_thread_info.state = omp_state_overhead;
Jonathan Peyton30419822017-05-12 18:01:32 +00005672 }
5673#endif
Joachim Protze82e94a52017-11-01 10:08:30 +00005674 /* join barrier after parallel region */
5675 __kmp_join_barrier(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +00005676 }
Jonathan Peyton30419822017-05-12 18:01:32 +00005677 }
5678 TCR_SYNC_PTR((intptr_t)__kmp_global.g.g_done);
Jim Cownie5e8470a2013-09-27 10:38:44 +00005679
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00005680#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00005681 if (ompt_enabled.ompt_callback_thread_end) {
5682 ompt_callbacks.ompt_callback(ompt_callback_thread_end)(thread_data);
Jonathan Peyton30419822017-05-12 18:01:32 +00005683 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00005684#endif
5685
Jonathan Peyton30419822017-05-12 18:01:32 +00005686 this_thr->th.th_task_team = NULL;
5687 /* run the destructors for the threadprivate data for this thread */
5688 __kmp_common_destroy_gtid(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +00005689
Jonathan Peyton30419822017-05-12 18:01:32 +00005690 KA_TRACE(10, ("__kmp_launch_thread: T#%d done\n", gtid));
5691 KMP_MB();
5692 return this_thr;
Jim Cownie5e8470a2013-09-27 10:38:44 +00005693}
5694
5695/* ------------------------------------------------------------------------ */
Jim Cownie5e8470a2013-09-27 10:38:44 +00005696
Jonathan Peyton30419822017-05-12 18:01:32 +00005697void __kmp_internal_end_dest(void *specific_gtid) {
5698#if KMP_COMPILER_ICC
5699#pragma warning(push)
5700#pragma warning(disable : 810) // conversion from "void *" to "int" may lose
5701// significant bits
5702#endif
5703 // Make sure no significant bits are lost
5704 int gtid = (kmp_intptr_t)specific_gtid - 1;
5705#if KMP_COMPILER_ICC
5706#pragma warning(pop)
5707#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00005708
Jonathan Peyton30419822017-05-12 18:01:32 +00005709 KA_TRACE(30, ("__kmp_internal_end_dest: T#%d\n", gtid));
5710 /* NOTE: the gtid is stored as gitd+1 in the thread-local-storage
5711 * this is because 0 is reserved for the nothing-stored case */
Jim Cownie5e8470a2013-09-27 10:38:44 +00005712
Jonathan Peyton30419822017-05-12 18:01:32 +00005713 /* josh: One reason for setting the gtid specific data even when it is being
5714 destroyed by pthread is to allow gtid lookup through thread specific data
5715 (__kmp_gtid_get_specific). Some of the code, especially stat code,
5716 that gets executed in the call to __kmp_internal_end_thread, actually
5717 gets the gtid through the thread specific data. Setting it here seems
5718 rather inelegant and perhaps wrong, but allows __kmp_internal_end_thread
5719 to run smoothly.
5720 todo: get rid of this after we remove the dependence on
5721 __kmp_gtid_get_specific */
5722 if (gtid >= 0 && KMP_UBER_GTID(gtid))
5723 __kmp_gtid_set_specific(gtid);
5724#ifdef KMP_TDATA_GTID
5725 __kmp_gtid = gtid;
5726#endif
5727 __kmp_internal_end_thread(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +00005728}
5729
Jonathan Peyton99016992015-05-26 17:32:53 +00005730#if KMP_OS_UNIX && KMP_DYNAMIC_LIB
Jim Cownie5e8470a2013-09-27 10:38:44 +00005731
Jonathan Peyton30419822017-05-12 18:01:32 +00005732// 2009-09-08 (lev): It looks the destructor does not work. In simple test cases
5733// destructors work perfectly, but in real libomp.so I have no evidence it is
5734// ever called. However, -fini linker option in makefile.mk works fine.
Jim Cownie5e8470a2013-09-27 10:38:44 +00005735
Jonathan Peyton30419822017-05-12 18:01:32 +00005736__attribute__((destructor)) void __kmp_internal_end_dtor(void) {
5737 __kmp_internal_end_atexit();
Jim Cownie5e8470a2013-09-27 10:38:44 +00005738}
5739
Jonathan Peyton30419822017-05-12 18:01:32 +00005740void __kmp_internal_end_fini(void) { __kmp_internal_end_atexit(); }
Jim Cownie5e8470a2013-09-27 10:38:44 +00005741
5742#endif
5743
Jonathan Peyton30419822017-05-12 18:01:32 +00005744/* [Windows] josh: when the atexit handler is called, there may still be more
5745 than one thread alive */
5746void __kmp_internal_end_atexit(void) {
5747 KA_TRACE(30, ("__kmp_internal_end_atexit\n"));
5748 /* [Windows]
5749 josh: ideally, we want to completely shutdown the library in this atexit
5750 handler, but stat code that depends on thread specific data for gtid fails
5751 because that data becomes unavailable at some point during the shutdown, so
5752 we call __kmp_internal_end_thread instead. We should eventually remove the
5753 dependency on __kmp_get_specific_gtid in the stat code and use
5754 __kmp_internal_end_library to cleanly shutdown the library.
Jim Cownie5e8470a2013-09-27 10:38:44 +00005755
Jonathan Peyton30419822017-05-12 18:01:32 +00005756 // TODO: Can some of this comment about GVS be removed?
5757 I suspect that the offending stat code is executed when the calling thread
5758 tries to clean up a dead root thread's data structures, resulting in GVS
5759 code trying to close the GVS structures for that thread, but since the stat
5760 code uses __kmp_get_specific_gtid to get the gtid with the assumption that
5761 the calling thread is cleaning up itself instead of another thread, it get
5762 confused. This happens because allowing a thread to unregister and cleanup
5763 another thread is a recent modification for addressing an issue.
5764 Based on the current design (20050722), a thread may end up
5765 trying to unregister another thread only if thread death does not trigger
5766 the calling of __kmp_internal_end_thread. For Linux* OS, there is the
5767 thread specific data destructor function to detect thread death. For
5768 Windows dynamic, there is DllMain(THREAD_DETACH). For Windows static, there
5769 is nothing. Thus, the workaround is applicable only for Windows static
5770 stat library. */
5771 __kmp_internal_end_library(-1);
5772#if KMP_OS_WINDOWS
5773 __kmp_close_console();
5774#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00005775}
5776
Jonathan Peyton30419822017-05-12 18:01:32 +00005777static void __kmp_reap_thread(kmp_info_t *thread, int is_root) {
5778 // It is assumed __kmp_forkjoin_lock is acquired.
Jim Cownie5e8470a2013-09-27 10:38:44 +00005779
Jonathan Peyton30419822017-05-12 18:01:32 +00005780 int gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +00005781
Jonathan Peyton30419822017-05-12 18:01:32 +00005782 KMP_DEBUG_ASSERT(thread != NULL);
Jim Cownie5e8470a2013-09-27 10:38:44 +00005783
Jonathan Peyton30419822017-05-12 18:01:32 +00005784 gtid = thread->th.th_info.ds.ds_gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +00005785
Jonathan Peyton30419822017-05-12 18:01:32 +00005786 if (!is_root) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00005787
Jonathan Peyton30419822017-05-12 18:01:32 +00005788 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) {
5789 /* Assume the threads are at the fork barrier here */
5790 KA_TRACE(
5791 20, ("__kmp_reap_thread: releasing T#%d from fork barrier for reap\n",
5792 gtid));
5793 /* Need release fence here to prevent seg faults for tree forkjoin barrier
5794 * (GEH) */
5795 ANNOTATE_HAPPENS_BEFORE(thread);
5796 kmp_flag_64 flag(&thread->th.th_bar[bs_forkjoin_barrier].bb.b_go, thread);
5797 __kmp_release_64(&flag);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00005798 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00005799
Jonathan Peyton30419822017-05-12 18:01:32 +00005800 // Terminate OS thread.
5801 __kmp_reap_worker(thread);
Jonathan Peyton7ca7ef02016-11-21 16:18:57 +00005802
Jonathan Peyton30419822017-05-12 18:01:32 +00005803 // The thread was killed asynchronously. If it was actively
5804 // spinning in the thread pool, decrement the global count.
5805 //
5806 // There is a small timing hole here - if the worker thread was just waking
5807 // up after sleeping in the pool, had reset it's th_active_in_pool flag but
5808 // not decremented the global counter __kmp_thread_pool_active_nth yet, then
5809 // the global counter might not get updated.
5810 //
5811 // Currently, this can only happen as the library is unloaded,
5812 // so there are no harmful side effects.
5813 if (thread->th.th_active_in_pool) {
5814 thread->th.th_active_in_pool = FALSE;
Jonathan Peyton37e2ef52018-07-09 17:36:22 +00005815 KMP_ATOMIC_DEC(&__kmp_thread_pool_active_nth);
5816 KMP_DEBUG_ASSERT(__kmp_thread_pool_active_nth >= 0);
Jonathan Peyton30419822017-05-12 18:01:32 +00005817 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00005818
Jonathan Peyton30419822017-05-12 18:01:32 +00005819 // Decrement # of [worker] threads in the pool.
5820 KMP_DEBUG_ASSERT(__kmp_thread_pool_nth > 0);
5821 --__kmp_thread_pool_nth;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00005822 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00005823
Jonathan Peyton30419822017-05-12 18:01:32 +00005824 __kmp_free_implicit_task(thread);
Jim Cownie5e8470a2013-09-27 10:38:44 +00005825
Jonathan Peyton30419822017-05-12 18:01:32 +00005826// Free the fast memory for tasking
5827#if USE_FAST_MEMORY
5828 __kmp_free_fast_memory(thread);
5829#endif /* USE_FAST_MEMORY */
5830
5831 __kmp_suspend_uninitialize_thread(thread);
5832
5833 KMP_DEBUG_ASSERT(__kmp_threads[gtid] == thread);
5834 TCW_SYNC_PTR(__kmp_threads[gtid], NULL);
5835
5836 --__kmp_all_nth;
5837// __kmp_nth was decremented when thread is added to the pool.
Jim Cownie5e8470a2013-09-27 10:38:44 +00005838
5839#ifdef KMP_ADJUST_BLOCKTIME
Jonathan Peyton30419822017-05-12 18:01:32 +00005840 /* Adjust blocktime back to user setting or default if necessary */
5841 /* Middle initialization might never have occurred */
5842 if (!__kmp_env_blocktime && (__kmp_avail_proc > 0)) {
5843 KMP_DEBUG_ASSERT(__kmp_avail_proc > 0);
5844 if (__kmp_nth <= __kmp_avail_proc) {
5845 __kmp_zero_bt = FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00005846 }
Jonathan Peyton30419822017-05-12 18:01:32 +00005847 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00005848#endif /* KMP_ADJUST_BLOCKTIME */
5849
Jonathan Peyton30419822017-05-12 18:01:32 +00005850 /* free the memory being used */
5851 if (__kmp_env_consistency_check) {
5852 if (thread->th.th_cons) {
5853 __kmp_free_cons_stack(thread->th.th_cons);
5854 thread->th.th_cons = NULL;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00005855 }
Jonathan Peyton30419822017-05-12 18:01:32 +00005856 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00005857
Jonathan Peyton30419822017-05-12 18:01:32 +00005858 if (thread->th.th_pri_common != NULL) {
5859 __kmp_free(thread->th.th_pri_common);
5860 thread->th.th_pri_common = NULL;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00005861 }
Andrey Churbanov6d224db2015-02-10 18:37:43 +00005862
Jonathan Peyton30419822017-05-12 18:01:32 +00005863 if (thread->th.th_task_state_memo_stack != NULL) {
5864 __kmp_free(thread->th.th_task_state_memo_stack);
5865 thread->th.th_task_state_memo_stack = NULL;
5866 }
5867
5868#if KMP_USE_BGET
5869 if (thread->th.th_local.bget_data != NULL) {
5870 __kmp_finalize_bget(thread);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00005871 }
Jonathan Peyton30419822017-05-12 18:01:32 +00005872#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00005873
Alp Toker98758b02014-03-02 04:12:06 +00005874#if KMP_AFFINITY_SUPPORTED
Jonathan Peyton30419822017-05-12 18:01:32 +00005875 if (thread->th.th_affin_mask != NULL) {
5876 KMP_CPU_FREE(thread->th.th_affin_mask);
5877 thread->th.th_affin_mask = NULL;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00005878 }
Alp Toker98758b02014-03-02 04:12:06 +00005879#endif /* KMP_AFFINITY_SUPPORTED */
Jim Cownie5e8470a2013-09-27 10:38:44 +00005880
Jonathan Peytonf6399362018-07-09 17:51:13 +00005881#if KMP_USE_HIER_SCHED
5882 if (thread->th.th_hier_bar_data != NULL) {
5883 __kmp_free(thread->th.th_hier_bar_data);
5884 thread->th.th_hier_bar_data = NULL;
5885 }
5886#endif
5887
Jonathan Peyton30419822017-05-12 18:01:32 +00005888 __kmp_reap_team(thread->th.th_serial_team);
5889 thread->th.th_serial_team = NULL;
5890 __kmp_free(thread);
Jim Cownie5e8470a2013-09-27 10:38:44 +00005891
Jonathan Peyton30419822017-05-12 18:01:32 +00005892 KMP_MB();
Jim Cownie5e8470a2013-09-27 10:38:44 +00005893
5894} // __kmp_reap_thread
5895
Jonathan Peyton30419822017-05-12 18:01:32 +00005896static void __kmp_internal_end(void) {
5897 int i;
Jim Cownie5e8470a2013-09-27 10:38:44 +00005898
Jonathan Peyton30419822017-05-12 18:01:32 +00005899 /* First, unregister the library */
5900 __kmp_unregister_library();
Jim Cownie5e8470a2013-09-27 10:38:44 +00005901
Jonathan Peyton30419822017-05-12 18:01:32 +00005902#if KMP_OS_WINDOWS
5903 /* In Win static library, we can't tell when a root actually dies, so we
5904 reclaim the data structures for any root threads that have died but not
5905 unregistered themselves, in order to shut down cleanly.
5906 In Win dynamic library we also can't tell when a thread dies. */
5907 __kmp_reclaim_dead_roots(); // AC: moved here to always clean resources of
5908// dead roots
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00005909#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00005910
Jonathan Peyton30419822017-05-12 18:01:32 +00005911 for (i = 0; i < __kmp_threads_capacity; i++)
5912 if (__kmp_root[i])
5913 if (__kmp_root[i]->r.r_active)
5914 break;
5915 KMP_MB(); /* Flush all pending memory write invalidates. */
5916 TCW_SYNC_4(__kmp_global.g.g_done, TRUE);
5917
5918 if (i < __kmp_threads_capacity) {
5919#if KMP_USE_MONITOR
5920 // 2009-09-08 (lev): Other alive roots found. Why do we kill the monitor??
5921 KMP_MB(); /* Flush all pending memory write invalidates. */
5922
Jonathan Peyton94a114f2017-10-20 19:30:57 +00005923 // Need to check that monitor was initialized before reaping it. If we are
5924 // called form __kmp_atfork_child (which sets __kmp_init_parallel = 0), then
5925 // __kmp_monitor will appear to contain valid data, but it is only valid in
5926 // the parent process, not the child.
Jonathan Peyton30419822017-05-12 18:01:32 +00005927 // New behavior (201008): instead of keying off of the flag
5928 // __kmp_init_parallel, the monitor thread creation is keyed off
5929 // of the new flag __kmp_init_monitor.
5930 __kmp_acquire_bootstrap_lock(&__kmp_monitor_lock);
5931 if (TCR_4(__kmp_init_monitor)) {
5932 __kmp_reap_monitor(&__kmp_monitor);
5933 TCW_4(__kmp_init_monitor, 0);
5934 }
5935 __kmp_release_bootstrap_lock(&__kmp_monitor_lock);
5936 KA_TRACE(10, ("__kmp_internal_end: monitor reaped\n"));
5937#endif // KMP_USE_MONITOR
5938 } else {
5939/* TODO move this to cleanup code */
5940#ifdef KMP_DEBUG
5941 /* make sure that everything has properly ended */
5942 for (i = 0; i < __kmp_threads_capacity; i++) {
5943 if (__kmp_root[i]) {
5944 // KMP_ASSERT( ! KMP_UBER_GTID( i ) ); // AC:
5945 // there can be uber threads alive here
5946 KMP_ASSERT(!__kmp_root[i]->r.r_active); // TODO: can they be active?
5947 }
5948 }
5949#endif
5950
5951 KMP_MB();
5952
5953 // Reap the worker threads.
5954 // This is valid for now, but be careful if threads are reaped sooner.
5955 while (__kmp_thread_pool != NULL) { // Loop thru all the thread in the pool.
5956 // Get the next thread from the pool.
Andrey Churbanovc47afcd2017-07-03 11:24:08 +00005957 kmp_info_t *thread = CCAST(kmp_info_t *, __kmp_thread_pool);
Jonathan Peyton30419822017-05-12 18:01:32 +00005958 __kmp_thread_pool = thread->th.th_next_pool;
5959 // Reap it.
5960 KMP_DEBUG_ASSERT(thread->th.th_reap_state == KMP_SAFE_TO_REAP);
5961 thread->th.th_next_pool = NULL;
5962 thread->th.th_in_pool = FALSE;
5963 __kmp_reap_thread(thread, 0);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00005964 }
Jonathan Peyton30419822017-05-12 18:01:32 +00005965 __kmp_thread_pool_insert_pt = NULL;
5966
5967 // Reap teams.
5968 while (__kmp_team_pool != NULL) { // Loop thru all the teams in the pool.
5969 // Get the next team from the pool.
Andrey Churbanovc47afcd2017-07-03 11:24:08 +00005970 kmp_team_t *team = CCAST(kmp_team_t *, __kmp_team_pool);
Jonathan Peyton30419822017-05-12 18:01:32 +00005971 __kmp_team_pool = team->t.t_next_pool;
5972 // Reap it.
5973 team->t.t_next_pool = NULL;
5974 __kmp_reap_team(team);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00005975 }
Jonathan Peyton30419822017-05-12 18:01:32 +00005976
5977 __kmp_reap_task_teams();
5978
Jonathan Peytona764af62018-07-19 19:17:00 +00005979#if KMP_OS_UNIX
5980 // Threads that are not reaped should not access any resources since they
5981 // are going to be deallocated soon, so the shutdown sequence should wait
5982 // until all threads either exit the final spin-waiting loop or begin
5983 // sleeping after the given blocktime.
5984 for (i = 0; i < __kmp_threads_capacity; i++) {
5985 kmp_info_t *thr = __kmp_threads[i];
5986 while (thr && KMP_ATOMIC_LD_ACQ(&thr->th.th_blocking))
5987 KMP_CPU_PAUSE();
5988 }
5989#endif
5990
Jonathan Peyton30419822017-05-12 18:01:32 +00005991 for (i = 0; i < __kmp_threads_capacity; ++i) {
5992 // TBD: Add some checking...
5993 // Something like KMP_DEBUG_ASSERT( __kmp_thread[ i ] == NULL );
5994 }
5995
5996 /* Make sure all threadprivate destructors get run by joining with all
5997 worker threads before resetting this flag */
5998 TCW_SYNC_4(__kmp_init_common, FALSE);
5999
6000 KA_TRACE(10, ("__kmp_internal_end: all workers reaped\n"));
6001 KMP_MB();
6002
6003#if KMP_USE_MONITOR
6004 // See note above: One of the possible fixes for CQ138434 / CQ140126
6005 //
6006 // FIXME: push both code fragments down and CSE them?
6007 // push them into __kmp_cleanup() ?
6008 __kmp_acquire_bootstrap_lock(&__kmp_monitor_lock);
6009 if (TCR_4(__kmp_init_monitor)) {
6010 __kmp_reap_monitor(&__kmp_monitor);
6011 TCW_4(__kmp_init_monitor, 0);
6012 }
6013 __kmp_release_bootstrap_lock(&__kmp_monitor_lock);
6014 KA_TRACE(10, ("__kmp_internal_end: monitor reaped\n"));
6015#endif
6016 } /* else !__kmp_global.t_active */
6017 TCW_4(__kmp_init_gtid, FALSE);
6018 KMP_MB(); /* Flush all pending memory write invalidates. */
6019
6020 __kmp_cleanup();
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00006021#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00006022 ompt_fini();
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00006023#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00006024}
6025
Jonathan Peyton30419822017-05-12 18:01:32 +00006026void __kmp_internal_end_library(int gtid_req) {
6027 /* if we have already cleaned up, don't try again, it wouldn't be pretty */
6028 /* this shouldn't be a race condition because __kmp_internal_end() is the
6029 only place to clear __kmp_serial_init */
6030 /* we'll check this later too, after we get the lock */
6031 // 2009-09-06: We do not set g_abort without setting g_done. This check looks
6032 // redundaant, because the next check will work in any case.
6033 if (__kmp_global.g.g_abort) {
6034 KA_TRACE(11, ("__kmp_internal_end_library: abort, exiting\n"));
6035 /* TODO abort? */
6036 return;
6037 }
6038 if (TCR_4(__kmp_global.g.g_done) || !__kmp_init_serial) {
6039 KA_TRACE(10, ("__kmp_internal_end_library: already finished\n"));
6040 return;
6041 }
6042
6043 KMP_MB(); /* Flush all pending memory write invalidates. */
6044
6045 /* find out who we are and what we should do */
6046 {
6047 int gtid = (gtid_req >= 0) ? gtid_req : __kmp_gtid_get_specific();
6048 KA_TRACE(
6049 10, ("__kmp_internal_end_library: enter T#%d (%d)\n", gtid, gtid_req));
6050 if (gtid == KMP_GTID_SHUTDOWN) {
6051 KA_TRACE(10, ("__kmp_internal_end_library: !__kmp_init_runtime, system "
6052 "already shutdown\n"));
6053 return;
6054 } else if (gtid == KMP_GTID_MONITOR) {
6055 KA_TRACE(10, ("__kmp_internal_end_library: monitor thread, gtid not "
6056 "registered, or system shutdown\n"));
6057 return;
6058 } else if (gtid == KMP_GTID_DNE) {
6059 KA_TRACE(10, ("__kmp_internal_end_library: gtid not registered or system "
6060 "shutdown\n"));
6061 /* we don't know who we are, but we may still shutdown the library */
6062 } else if (KMP_UBER_GTID(gtid)) {
6063 /* unregister ourselves as an uber thread. gtid is no longer valid */
6064 if (__kmp_root[gtid]->r.r_active) {
6065 __kmp_global.g.g_abort = -1;
6066 TCW_SYNC_4(__kmp_global.g.g_done, TRUE);
6067 KA_TRACE(10,
6068 ("__kmp_internal_end_library: root still active, abort T#%d\n",
6069 gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00006070 return;
Jonathan Peyton30419822017-05-12 18:01:32 +00006071 } else {
6072 KA_TRACE(
6073 10,
6074 ("__kmp_internal_end_library: unregistering sibling T#%d\n", gtid));
6075 __kmp_unregister_root_current_thread(gtid);
6076 }
6077 } else {
6078/* worker threads may call this function through the atexit handler, if they
6079 * call exit() */
6080/* For now, skip the usual subsequent processing and just dump the debug buffer.
6081 TODO: do a thorough shutdown instead */
6082#ifdef DUMP_DEBUG_ON_EXIT
6083 if (__kmp_debug_buf)
6084 __kmp_dump_debug_buffer();
6085#endif
6086 return;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006087 }
Jonathan Peyton30419822017-05-12 18:01:32 +00006088 }
6089 /* synchronize the termination process */
6090 __kmp_acquire_bootstrap_lock(&__kmp_initz_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006091
Jonathan Peyton30419822017-05-12 18:01:32 +00006092 /* have we already finished */
6093 if (__kmp_global.g.g_abort) {
6094 KA_TRACE(10, ("__kmp_internal_end_library: abort, exiting\n"));
6095 /* TODO abort? */
6096 __kmp_release_bootstrap_lock(&__kmp_initz_lock);
6097 return;
6098 }
6099 if (TCR_4(__kmp_global.g.g_done) || !__kmp_init_serial) {
6100 __kmp_release_bootstrap_lock(&__kmp_initz_lock);
6101 return;
6102 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006103
Jonathan Peyton30419822017-05-12 18:01:32 +00006104 /* We need this lock to enforce mutex between this reading of
6105 __kmp_threads_capacity and the writing by __kmp_register_root.
6106 Alternatively, we can use a counter of roots that is atomically updated by
6107 __kmp_get_global_thread_id_reg, __kmp_do_serial_initialize and
6108 __kmp_internal_end_*. */
6109 __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006110
Jonathan Peyton30419822017-05-12 18:01:32 +00006111 /* now we can safely conduct the actual termination */
6112 __kmp_internal_end();
Jim Cownie5e8470a2013-09-27 10:38:44 +00006113
Jonathan Peyton30419822017-05-12 18:01:32 +00006114 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
6115 __kmp_release_bootstrap_lock(&__kmp_initz_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006116
Jonathan Peyton30419822017-05-12 18:01:32 +00006117 KA_TRACE(10, ("__kmp_internal_end_library: exit\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +00006118
Jonathan Peyton30419822017-05-12 18:01:32 +00006119#ifdef DUMP_DEBUG_ON_EXIT
6120 if (__kmp_debug_buf)
6121 __kmp_dump_debug_buffer();
6122#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00006123
Jonathan Peyton30419822017-05-12 18:01:32 +00006124#if KMP_OS_WINDOWS
6125 __kmp_close_console();
6126#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00006127
Jonathan Peyton30419822017-05-12 18:01:32 +00006128 __kmp_fini_allocator();
Jim Cownie5e8470a2013-09-27 10:38:44 +00006129
6130} // __kmp_internal_end_library
6131
Jonathan Peyton30419822017-05-12 18:01:32 +00006132void __kmp_internal_end_thread(int gtid_req) {
6133 int i;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006134
Jonathan Peyton30419822017-05-12 18:01:32 +00006135 /* if we have already cleaned up, don't try again, it wouldn't be pretty */
6136 /* this shouldn't be a race condition because __kmp_internal_end() is the
6137 * only place to clear __kmp_serial_init */
6138 /* we'll check this later too, after we get the lock */
6139 // 2009-09-06: We do not set g_abort without setting g_done. This check looks
6140 // redundant, because the next check will work in any case.
6141 if (__kmp_global.g.g_abort) {
6142 KA_TRACE(11, ("__kmp_internal_end_thread: abort, exiting\n"));
6143 /* TODO abort? */
6144 return;
6145 }
6146 if (TCR_4(__kmp_global.g.g_done) || !__kmp_init_serial) {
6147 KA_TRACE(10, ("__kmp_internal_end_thread: already finished\n"));
6148 return;
6149 }
6150
6151 KMP_MB(); /* Flush all pending memory write invalidates. */
6152
6153 /* find out who we are and what we should do */
6154 {
6155 int gtid = (gtid_req >= 0) ? gtid_req : __kmp_gtid_get_specific();
6156 KA_TRACE(10,
6157 ("__kmp_internal_end_thread: enter T#%d (%d)\n", gtid, gtid_req));
6158 if (gtid == KMP_GTID_SHUTDOWN) {
6159 KA_TRACE(10, ("__kmp_internal_end_thread: !__kmp_init_runtime, system "
6160 "already shutdown\n"));
6161 return;
6162 } else if (gtid == KMP_GTID_MONITOR) {
6163 KA_TRACE(10, ("__kmp_internal_end_thread: monitor thread, gtid not "
6164 "registered, or system shutdown\n"));
6165 return;
6166 } else if (gtid == KMP_GTID_DNE) {
6167 KA_TRACE(10, ("__kmp_internal_end_thread: gtid not registered or system "
6168 "shutdown\n"));
6169 return;
6170 /* we don't know who we are */
6171 } else if (KMP_UBER_GTID(gtid)) {
6172 /* unregister ourselves as an uber thread. gtid is no longer valid */
6173 if (__kmp_root[gtid]->r.r_active) {
6174 __kmp_global.g.g_abort = -1;
6175 TCW_SYNC_4(__kmp_global.g.g_done, TRUE);
6176 KA_TRACE(10,
6177 ("__kmp_internal_end_thread: root still active, abort T#%d\n",
6178 gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00006179 return;
Jonathan Peyton30419822017-05-12 18:01:32 +00006180 } else {
6181 KA_TRACE(10, ("__kmp_internal_end_thread: unregistering sibling T#%d\n",
6182 gtid));
6183 __kmp_unregister_root_current_thread(gtid);
6184 }
6185 } else {
6186 /* just a worker thread, let's leave */
6187 KA_TRACE(10, ("__kmp_internal_end_thread: worker thread T#%d\n", gtid));
6188
6189 if (gtid >= 0) {
6190 __kmp_threads[gtid]->th.th_task_team = NULL;
6191 }
6192
6193 KA_TRACE(10,
6194 ("__kmp_internal_end_thread: worker thread done, exiting T#%d\n",
6195 gtid));
6196 return;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006197 }
Jonathan Peyton30419822017-05-12 18:01:32 +00006198 }
6199#if defined KMP_DYNAMIC_LIB
6200 // AC: lets not shutdown the Linux* OS dynamic library at the exit of uber
6201 // thread, because we will better shutdown later in the library destructor.
6202 // The reason of this change is performance problem when non-openmp thread in
6203 // a loop forks and joins many openmp threads. We can save a lot of time
6204 // keeping worker threads alive until the program shutdown.
6205 // OM: Removed Linux* OS restriction to fix the crash on OS X* (DPD200239966)
6206 // and Windows(DPD200287443) that occurs when using critical sections from
6207 // foreign threads.
6208 KA_TRACE(10, ("__kmp_internal_end_thread: exiting T#%d\n", gtid_req));
6209 return;
6210#endif
6211 /* synchronize the termination process */
6212 __kmp_acquire_bootstrap_lock(&__kmp_initz_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006213
Jonathan Peyton30419822017-05-12 18:01:32 +00006214 /* have we already finished */
6215 if (__kmp_global.g.g_abort) {
6216 KA_TRACE(10, ("__kmp_internal_end_thread: abort, exiting\n"));
6217 /* TODO abort? */
6218 __kmp_release_bootstrap_lock(&__kmp_initz_lock);
6219 return;
6220 }
6221 if (TCR_4(__kmp_global.g.g_done) || !__kmp_init_serial) {
6222 __kmp_release_bootstrap_lock(&__kmp_initz_lock);
6223 return;
6224 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006225
Jonathan Peyton30419822017-05-12 18:01:32 +00006226 /* We need this lock to enforce mutex between this reading of
6227 __kmp_threads_capacity and the writing by __kmp_register_root.
6228 Alternatively, we can use a counter of roots that is atomically updated by
6229 __kmp_get_global_thread_id_reg, __kmp_do_serial_initialize and
6230 __kmp_internal_end_*. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00006231
Jonathan Peyton30419822017-05-12 18:01:32 +00006232 /* should we finish the run-time? are all siblings done? */
6233 __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006234
Jonathan Peyton30419822017-05-12 18:01:32 +00006235 for (i = 0; i < __kmp_threads_capacity; ++i) {
6236 if (KMP_UBER_GTID(i)) {
6237 KA_TRACE(
6238 10,
6239 ("__kmp_internal_end_thread: remaining sibling task: gtid==%d\n", i));
6240 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
6241 __kmp_release_bootstrap_lock(&__kmp_initz_lock);
6242 return;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00006243 }
Jonathan Peyton30419822017-05-12 18:01:32 +00006244 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006245
Jonathan Peyton30419822017-05-12 18:01:32 +00006246 /* now we can safely conduct the actual termination */
Jim Cownie5e8470a2013-09-27 10:38:44 +00006247
Jonathan Peyton30419822017-05-12 18:01:32 +00006248 __kmp_internal_end();
Jim Cownie5e8470a2013-09-27 10:38:44 +00006249
Jonathan Peyton30419822017-05-12 18:01:32 +00006250 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
6251 __kmp_release_bootstrap_lock(&__kmp_initz_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006252
Jonathan Peyton30419822017-05-12 18:01:32 +00006253 KA_TRACE(10, ("__kmp_internal_end_thread: exit T#%d\n", gtid_req));
Jim Cownie5e8470a2013-09-27 10:38:44 +00006254
Jonathan Peyton30419822017-05-12 18:01:32 +00006255#ifdef DUMP_DEBUG_ON_EXIT
6256 if (__kmp_debug_buf)
6257 __kmp_dump_debug_buffer();
6258#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00006259} // __kmp_internal_end_thread
6260
Jonathan Peyton30419822017-05-12 18:01:32 +00006261// -----------------------------------------------------------------------------
Jim Cownie5e8470a2013-09-27 10:38:44 +00006262// Library registration stuff.
6263
Jonathan Peyton30419822017-05-12 18:01:32 +00006264static long __kmp_registration_flag = 0;
6265// Random value used to indicate library initialization.
6266static char *__kmp_registration_str = NULL;
6267// Value to be saved in env var __KMP_REGISTERED_LIB_<pid>.
Jim Cownie5e8470a2013-09-27 10:38:44 +00006268
Jonathan Peyton30419822017-05-12 18:01:32 +00006269static inline char *__kmp_reg_status_name() {
6270 /* On RHEL 3u5 if linked statically, getpid() returns different values in
6271 each thread. If registration and unregistration go in different threads
6272 (omp_misc_other_root_exit.cpp test case), the name of registered_lib_env
6273 env var can not be found, because the name will contain different pid. */
6274 return __kmp_str_format("__KMP_REGISTERED_LIB_%d", (int)getpid());
Jim Cownie5e8470a2013-09-27 10:38:44 +00006275} // __kmp_reg_status_get
6276
Jonathan Peyton30419822017-05-12 18:01:32 +00006277void __kmp_register_library_startup(void) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00006278
Jonathan Peyton30419822017-05-12 18:01:32 +00006279 char *name = __kmp_reg_status_name(); // Name of the environment variable.
6280 int done = 0;
6281 union {
6282 double dtime;
6283 long ltime;
6284 } time;
6285#if KMP_ARCH_X86 || KMP_ARCH_X86_64
6286 __kmp_initialize_system_tick();
6287#endif
6288 __kmp_read_system_time(&time.dtime);
6289 __kmp_registration_flag = 0xCAFE0000L | (time.ltime & 0x0000FFFFL);
6290 __kmp_registration_str =
6291 __kmp_str_format("%p-%lx-%s", &__kmp_registration_flag,
6292 __kmp_registration_flag, KMP_LIBRARY_FILE);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006293
Jonathan Peyton30419822017-05-12 18:01:32 +00006294 KA_TRACE(50, ("__kmp_register_library_startup: %s=\"%s\"\n", name,
6295 __kmp_registration_str));
Jim Cownie5e8470a2013-09-27 10:38:44 +00006296
Jonathan Peyton30419822017-05-12 18:01:32 +00006297 while (!done) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00006298
Jonathan Peyton30419822017-05-12 18:01:32 +00006299 char *value = NULL; // Actual value of the environment variable.
Jim Cownie5e8470a2013-09-27 10:38:44 +00006300
Jonathan Peyton30419822017-05-12 18:01:32 +00006301 // Set environment variable, but do not overwrite if it is exist.
6302 __kmp_env_set(name, __kmp_registration_str, 0);
6303 // Check the variable is written.
6304 value = __kmp_env_get(name);
6305 if (value != NULL && strcmp(value, __kmp_registration_str) == 0) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00006306
Jonathan Peyton30419822017-05-12 18:01:32 +00006307 done = 1; // Ok, environment variable set successfully, exit the loop.
Jim Cownie5e8470a2013-09-27 10:38:44 +00006308
Jonathan Peyton30419822017-05-12 18:01:32 +00006309 } else {
Jim Cownie5e8470a2013-09-27 10:38:44 +00006310
Jonathan Peyton30419822017-05-12 18:01:32 +00006311 // Oops. Write failed. Another copy of OpenMP RTL is in memory.
6312 // Check whether it alive or dead.
6313 int neighbor = 0; // 0 -- unknown status, 1 -- alive, 2 -- dead.
6314 char *tail = value;
6315 char *flag_addr_str = NULL;
6316 char *flag_val_str = NULL;
6317 char const *file_name = NULL;
6318 __kmp_str_split(tail, '-', &flag_addr_str, &tail);
6319 __kmp_str_split(tail, '-', &flag_val_str, &tail);
6320 file_name = tail;
6321 if (tail != NULL) {
6322 long *flag_addr = 0;
6323 long flag_val = 0;
Jonathan Peytonbaad3f62018-08-09 22:04:30 +00006324 KMP_SSCANF(flag_addr_str, "%p", RCAST(void**, &flag_addr));
Jonathan Peyton30419822017-05-12 18:01:32 +00006325 KMP_SSCANF(flag_val_str, "%lx", &flag_val);
6326 if (flag_addr != 0 && flag_val != 0 && strcmp(file_name, "") != 0) {
6327 // First, check whether environment-encoded address is mapped into
6328 // addr space.
6329 // If so, dereference it to see if it still has the right value.
6330 if (__kmp_is_address_mapped(flag_addr) && *flag_addr == flag_val) {
6331 neighbor = 1;
6332 } else {
6333 // If not, then we know the other copy of the library is no longer
6334 // running.
6335 neighbor = 2;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00006336 }
6337 }
6338 }
Jonathan Peyton30419822017-05-12 18:01:32 +00006339 switch (neighbor) {
6340 case 0: // Cannot parse environment variable -- neighbor status unknown.
6341 // Assume it is the incompatible format of future version of the
6342 // library. Assume the other library is alive.
6343 // WARN( ... ); // TODO: Issue a warning.
6344 file_name = "unknown library";
6345 // Attention! Falling to the next case. That's intentional.
6346 case 1: { // Neighbor is alive.
6347 // Check it is allowed.
6348 char *duplicate_ok = __kmp_env_get("KMP_DUPLICATE_LIB_OK");
6349 if (!__kmp_str_match_true(duplicate_ok)) {
6350 // That's not allowed. Issue fatal error.
Jonathan Peyton6a393f72017-09-05 15:43:58 +00006351 __kmp_fatal(KMP_MSG(DuplicateLibrary, KMP_LIBRARY_FILE, file_name),
6352 KMP_HNT(DuplicateLibrary), __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00006353 }
Jonathan Peyton30419822017-05-12 18:01:32 +00006354 KMP_INTERNAL_FREE(duplicate_ok);
6355 __kmp_duplicate_library_ok = 1;
6356 done = 1; // Exit the loop.
6357 } break;
6358 case 2: { // Neighbor is dead.
6359 // Clear the variable and try to register library again.
6360 __kmp_env_unset(name);
6361 } break;
6362 default: { KMP_DEBUG_ASSERT(0); } break;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00006363 }
6364 }
Jonathan Peyton30419822017-05-12 18:01:32 +00006365 KMP_INTERNAL_FREE((void *)value);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00006366 }
Jonathan Peyton30419822017-05-12 18:01:32 +00006367 KMP_INTERNAL_FREE((void *)name);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006368
6369} // func __kmp_register_library_startup
6370
Jonathan Peyton30419822017-05-12 18:01:32 +00006371void __kmp_unregister_library(void) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00006372
Jonathan Peyton30419822017-05-12 18:01:32 +00006373 char *name = __kmp_reg_status_name();
6374 char *value = __kmp_env_get(name);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006375
Jonathan Peyton30419822017-05-12 18:01:32 +00006376 KMP_DEBUG_ASSERT(__kmp_registration_flag != 0);
6377 KMP_DEBUG_ASSERT(__kmp_registration_str != NULL);
6378 if (value != NULL && strcmp(value, __kmp_registration_str) == 0) {
6379 // Ok, this is our variable. Delete it.
6380 __kmp_env_unset(name);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00006381 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006382
Jonathan Peyton30419822017-05-12 18:01:32 +00006383 KMP_INTERNAL_FREE(__kmp_registration_str);
6384 KMP_INTERNAL_FREE(value);
6385 KMP_INTERNAL_FREE(name);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006386
Jonathan Peyton30419822017-05-12 18:01:32 +00006387 __kmp_registration_flag = 0;
6388 __kmp_registration_str = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006389
6390} // __kmp_unregister_library
6391
Jim Cownie5e8470a2013-09-27 10:38:44 +00006392// End of Library registration stuff.
Jonathan Peyton30419822017-05-12 18:01:32 +00006393// -----------------------------------------------------------------------------
Jim Cownie5e8470a2013-09-27 10:38:44 +00006394
Jonathan Peyton492e0a32017-06-13 17:17:26 +00006395#if KMP_MIC_SUPPORTED
Andrey Churbanov613edeb2015-02-20 18:14:43 +00006396
Jonathan Peyton30419822017-05-12 18:01:32 +00006397static void __kmp_check_mic_type() {
6398 kmp_cpuid_t cpuid_state = {0};
6399 kmp_cpuid_t *cs_p = &cpuid_state;
6400 __kmp_x86_cpuid(1, 0, cs_p);
6401 // We don't support mic1 at the moment
6402 if ((cs_p->eax & 0xff0) == 0xB10) {
6403 __kmp_mic_type = mic2;
6404 } else if ((cs_p->eax & 0xf0ff0) == 0x50670) {
6405 __kmp_mic_type = mic3;
6406 } else {
6407 __kmp_mic_type = non_mic;
6408 }
Andrey Churbanov613edeb2015-02-20 18:14:43 +00006409}
6410
Jonathan Peyton492e0a32017-06-13 17:17:26 +00006411#endif /* KMP_MIC_SUPPORTED */
Andrey Churbanov613edeb2015-02-20 18:14:43 +00006412
Jonathan Peyton30419822017-05-12 18:01:32 +00006413static void __kmp_do_serial_initialize(void) {
6414 int i, gtid;
6415 int size;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006416
Jonathan Peyton30419822017-05-12 18:01:32 +00006417 KA_TRACE(10, ("__kmp_do_serial_initialize: enter\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +00006418
Jonathan Peyton30419822017-05-12 18:01:32 +00006419 KMP_DEBUG_ASSERT(sizeof(kmp_int32) == 4);
6420 KMP_DEBUG_ASSERT(sizeof(kmp_uint32) == 4);
6421 KMP_DEBUG_ASSERT(sizeof(kmp_int64) == 8);
6422 KMP_DEBUG_ASSERT(sizeof(kmp_uint64) == 8);
6423 KMP_DEBUG_ASSERT(sizeof(kmp_intptr_t) == sizeof(void *));
Jim Cownie5e8470a2013-09-27 10:38:44 +00006424
Jonathan Peyton82a13bf2015-09-21 18:01:02 +00006425#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00006426 ompt_pre_init();
Jonathan Peyton82a13bf2015-09-21 18:01:02 +00006427#endif
6428
Jonathan Peyton30419822017-05-12 18:01:32 +00006429 __kmp_validate_locks();
Jim Cownie5e8470a2013-09-27 10:38:44 +00006430
Jonathan Peyton30419822017-05-12 18:01:32 +00006431 /* Initialize internal memory allocator */
6432 __kmp_init_allocator();
Jim Cownie5e8470a2013-09-27 10:38:44 +00006433
Jonathan Peyton30419822017-05-12 18:01:32 +00006434 /* Register the library startup via an environment variable and check to see
6435 whether another copy of the library is already registered. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00006436
Jonathan Peyton30419822017-05-12 18:01:32 +00006437 __kmp_register_library_startup();
Jim Cownie5e8470a2013-09-27 10:38:44 +00006438
Jonathan Peyton30419822017-05-12 18:01:32 +00006439 /* TODO reinitialization of library */
6440 if (TCR_4(__kmp_global.g.g_done)) {
6441 KA_TRACE(10, ("__kmp_do_serial_initialize: reinitialization of library\n"));
6442 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006443
Jonathan Peyton30419822017-05-12 18:01:32 +00006444 __kmp_global.g.g_abort = 0;
6445 TCW_SYNC_4(__kmp_global.g.g_done, FALSE);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006446
Jonathan Peyton30419822017-05-12 18:01:32 +00006447/* initialize the locks */
Jim Cownie5e8470a2013-09-27 10:38:44 +00006448#if KMP_USE_ADAPTIVE_LOCKS
6449#if KMP_DEBUG_ADAPTIVE_LOCKS
Jonathan Peyton30419822017-05-12 18:01:32 +00006450 __kmp_init_speculative_stats();
Jim Cownie5e8470a2013-09-27 10:38:44 +00006451#endif
6452#endif
Jonathan Peytonad579922015-12-17 16:19:05 +00006453#if KMP_STATS_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00006454 __kmp_stats_init();
Jonathan Peytonad579922015-12-17 16:19:05 +00006455#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00006456 __kmp_init_lock(&__kmp_global_lock);
6457 __kmp_init_queuing_lock(&__kmp_dispatch_lock);
6458 __kmp_init_lock(&__kmp_debug_lock);
6459 __kmp_init_atomic_lock(&__kmp_atomic_lock);
6460 __kmp_init_atomic_lock(&__kmp_atomic_lock_1i);
6461 __kmp_init_atomic_lock(&__kmp_atomic_lock_2i);
6462 __kmp_init_atomic_lock(&__kmp_atomic_lock_4i);
6463 __kmp_init_atomic_lock(&__kmp_atomic_lock_4r);
6464 __kmp_init_atomic_lock(&__kmp_atomic_lock_8i);
6465 __kmp_init_atomic_lock(&__kmp_atomic_lock_8r);
6466 __kmp_init_atomic_lock(&__kmp_atomic_lock_8c);
6467 __kmp_init_atomic_lock(&__kmp_atomic_lock_10r);
6468 __kmp_init_atomic_lock(&__kmp_atomic_lock_16r);
6469 __kmp_init_atomic_lock(&__kmp_atomic_lock_16c);
6470 __kmp_init_atomic_lock(&__kmp_atomic_lock_20c);
6471 __kmp_init_atomic_lock(&__kmp_atomic_lock_32c);
6472 __kmp_init_bootstrap_lock(&__kmp_forkjoin_lock);
6473 __kmp_init_bootstrap_lock(&__kmp_exit_lock);
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00006474#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +00006475 __kmp_init_bootstrap_lock(&__kmp_monitor_lock);
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00006476#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00006477 __kmp_init_bootstrap_lock(&__kmp_tp_cached_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006478
Jonathan Peyton30419822017-05-12 18:01:32 +00006479 /* conduct initialization and initial setup of configuration */
Jim Cownie5e8470a2013-09-27 10:38:44 +00006480
Jonathan Peyton30419822017-05-12 18:01:32 +00006481 __kmp_runtime_initialize();
Jim Cownie5e8470a2013-09-27 10:38:44 +00006482
Jonathan Peyton492e0a32017-06-13 17:17:26 +00006483#if KMP_MIC_SUPPORTED
Jonathan Peyton30419822017-05-12 18:01:32 +00006484 __kmp_check_mic_type();
Andrey Churbanov613edeb2015-02-20 18:14:43 +00006485#endif
6486
Jonathan Peyton30419822017-05-12 18:01:32 +00006487// Some global variable initialization moved here from kmp_env_initialize()
Jim Cownie5e8470a2013-09-27 10:38:44 +00006488#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00006489 kmp_diag = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006490#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00006491 __kmp_abort_delay = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006492
Jonathan Peyton30419822017-05-12 18:01:32 +00006493 // From __kmp_init_dflt_team_nth()
6494 /* assume the entire machine will be used */
6495 __kmp_dflt_team_nth_ub = __kmp_xproc;
6496 if (__kmp_dflt_team_nth_ub < KMP_MIN_NTH) {
6497 __kmp_dflt_team_nth_ub = KMP_MIN_NTH;
6498 }
6499 if (__kmp_dflt_team_nth_ub > __kmp_sys_max_nth) {
6500 __kmp_dflt_team_nth_ub = __kmp_sys_max_nth;
6501 }
6502 __kmp_max_nth = __kmp_sys_max_nth;
Jonathan Peytonf4392462017-07-27 20:58:41 +00006503 __kmp_cg_max_nth = __kmp_sys_max_nth;
Jonathan Peyton4f90c822017-08-02 20:04:45 +00006504 __kmp_teams_max_nth = __kmp_xproc; // set a "reasonable" default
6505 if (__kmp_teams_max_nth > __kmp_sys_max_nth) {
6506 __kmp_teams_max_nth = __kmp_sys_max_nth;
6507 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006508
Jonathan Peyton30419822017-05-12 18:01:32 +00006509 // Three vars below moved here from __kmp_env_initialize() "KMP_BLOCKTIME"
6510 // part
6511 __kmp_dflt_blocktime = KMP_DEFAULT_BLOCKTIME;
Jonathan Peytone1c7c132016-10-07 18:12:19 +00006512#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +00006513 __kmp_monitor_wakeups =
6514 KMP_WAKEUPS_FROM_BLOCKTIME(__kmp_dflt_blocktime, __kmp_monitor_wakeups);
6515 __kmp_bt_intervals =
6516 KMP_INTERVALS_FROM_BLOCKTIME(__kmp_dflt_blocktime, __kmp_monitor_wakeups);
Jonathan Peytone1c7c132016-10-07 18:12:19 +00006517#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00006518 // From "KMP_LIBRARY" part of __kmp_env_initialize()
6519 __kmp_library = library_throughput;
6520 // From KMP_SCHEDULE initialization
6521 __kmp_static = kmp_sch_static_balanced;
6522// AC: do not use analytical here, because it is non-monotonous
6523//__kmp_guided = kmp_sch_guided_iterative_chunked;
6524//__kmp_auto = kmp_sch_guided_analytical_chunked; // AC: it is the default, no
6525// need to repeat assignment
6526// Barrier initialization. Moved here from __kmp_env_initialize() Barrier branch
6527// bit control and barrier method control parts
Jim Cownie4cc4bb42014-10-07 16:25:50 +00006528#if KMP_FAST_REDUCTION_BARRIER
Jonathan Peyton30419822017-05-12 18:01:32 +00006529#define kmp_reduction_barrier_gather_bb ((int)1)
6530#define kmp_reduction_barrier_release_bb ((int)1)
6531#define kmp_reduction_barrier_gather_pat bp_hyper_bar
6532#define kmp_reduction_barrier_release_pat bp_hyper_bar
6533#endif // KMP_FAST_REDUCTION_BARRIER
6534 for (i = bs_plain_barrier; i < bs_last_barrier; i++) {
6535 __kmp_barrier_gather_branch_bits[i] = __kmp_barrier_gather_bb_dflt;
6536 __kmp_barrier_release_branch_bits[i] = __kmp_barrier_release_bb_dflt;
6537 __kmp_barrier_gather_pattern[i] = __kmp_barrier_gather_pat_dflt;
6538 __kmp_barrier_release_pattern[i] = __kmp_barrier_release_pat_dflt;
6539#if KMP_FAST_REDUCTION_BARRIER
6540 if (i == bs_reduction_barrier) { // tested and confirmed on ALTIX only (
6541 // lin_64 ): hyper,1
6542 __kmp_barrier_gather_branch_bits[i] = kmp_reduction_barrier_gather_bb;
6543 __kmp_barrier_release_branch_bits[i] = kmp_reduction_barrier_release_bb;
6544 __kmp_barrier_gather_pattern[i] = kmp_reduction_barrier_gather_pat;
6545 __kmp_barrier_release_pattern[i] = kmp_reduction_barrier_release_pat;
Andrey Churbanov613edeb2015-02-20 18:14:43 +00006546 }
Jonathan Peyton30419822017-05-12 18:01:32 +00006547#endif // KMP_FAST_REDUCTION_BARRIER
6548 }
6549#if KMP_FAST_REDUCTION_BARRIER
6550#undef kmp_reduction_barrier_release_pat
6551#undef kmp_reduction_barrier_gather_pat
6552#undef kmp_reduction_barrier_release_bb
6553#undef kmp_reduction_barrier_gather_bb
6554#endif // KMP_FAST_REDUCTION_BARRIER
Jonathan Peyton492e0a32017-06-13 17:17:26 +00006555#if KMP_MIC_SUPPORTED
Jonathan Peyton30419822017-05-12 18:01:32 +00006556 if (__kmp_mic_type == mic2) { // KNC
6557 // AC: plane=3,2, forkjoin=2,1 are optimal for 240 threads on KNC
6558 __kmp_barrier_gather_branch_bits[bs_plain_barrier] = 3; // plain gather
6559 __kmp_barrier_release_branch_bits[bs_forkjoin_barrier] =
6560 1; // forkjoin release
6561 __kmp_barrier_gather_pattern[bs_forkjoin_barrier] = bp_hierarchical_bar;
6562 __kmp_barrier_release_pattern[bs_forkjoin_barrier] = bp_hierarchical_bar;
6563 }
6564#if KMP_FAST_REDUCTION_BARRIER
6565 if (__kmp_mic_type == mic2) { // KNC
6566 __kmp_barrier_gather_pattern[bs_reduction_barrier] = bp_hierarchical_bar;
6567 __kmp_barrier_release_pattern[bs_reduction_barrier] = bp_hierarchical_bar;
6568 }
Jonathan Peyton492e0a32017-06-13 17:17:26 +00006569#endif // KMP_FAST_REDUCTION_BARRIER
6570#endif // KMP_MIC_SUPPORTED
Jim Cownie5e8470a2013-09-27 10:38:44 +00006571
Jonathan Peyton30419822017-05-12 18:01:32 +00006572// From KMP_CHECKS initialization
Jim Cownie5e8470a2013-09-27 10:38:44 +00006573#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00006574 __kmp_env_checks = TRUE; /* development versions have the extra checks */
Jim Cownie5e8470a2013-09-27 10:38:44 +00006575#else
Jonathan Peyton30419822017-05-12 18:01:32 +00006576 __kmp_env_checks = FALSE; /* port versions do not have the extra checks */
Jim Cownie5e8470a2013-09-27 10:38:44 +00006577#endif
6578
Jonathan Peyton30419822017-05-12 18:01:32 +00006579 // From "KMP_FOREIGN_THREADS_THREADPRIVATE" initialization
6580 __kmp_foreign_tp = TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006581
Jonathan Peyton30419822017-05-12 18:01:32 +00006582 __kmp_global.g.g_dynamic = FALSE;
6583 __kmp_global.g.g_dynamic_mode = dynamic_default;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006584
Jonathan Peyton30419822017-05-12 18:01:32 +00006585 __kmp_env_initialize(NULL);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00006586
Jonathan Peyton30419822017-05-12 18:01:32 +00006587// Print all messages in message catalog for testing purposes.
6588#ifdef KMP_DEBUG
6589 char const *val = __kmp_env_get("KMP_DUMP_CATALOG");
6590 if (__kmp_str_match_true(val)) {
6591 kmp_str_buf_t buffer;
6592 __kmp_str_buf_init(&buffer);
6593 __kmp_i18n_dump_catalog(&buffer);
6594 __kmp_printf("%s", buffer.str);
6595 __kmp_str_buf_free(&buffer);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00006596 }
Jonathan Peyton30419822017-05-12 18:01:32 +00006597 __kmp_env_free(&val);
6598#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00006599
Jonathan Peyton30419822017-05-12 18:01:32 +00006600 __kmp_threads_capacity =
6601 __kmp_initial_threads_capacity(__kmp_dflt_team_nth_ub);
6602 // Moved here from __kmp_env_initialize() "KMP_ALL_THREADPRIVATE" part
6603 __kmp_tp_capacity = __kmp_default_tp_capacity(
6604 __kmp_dflt_team_nth_ub, __kmp_max_nth, __kmp_allThreadsSpecified);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006605
Jonathan Peyton30419822017-05-12 18:01:32 +00006606 // If the library is shut down properly, both pools must be NULL. Just in
6607 // case, set them to NULL -- some memory may leak, but subsequent code will
6608 // work even if pools are not freed.
6609 KMP_DEBUG_ASSERT(__kmp_thread_pool == NULL);
6610 KMP_DEBUG_ASSERT(__kmp_thread_pool_insert_pt == NULL);
6611 KMP_DEBUG_ASSERT(__kmp_team_pool == NULL);
6612 __kmp_thread_pool = NULL;
6613 __kmp_thread_pool_insert_pt = NULL;
6614 __kmp_team_pool = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006615
Jonathan Peyton30419822017-05-12 18:01:32 +00006616 /* Allocate all of the variable sized records */
6617 /* NOTE: __kmp_threads_capacity entries are allocated, but the arrays are
6618 * expandable */
6619 /* Since allocation is cache-aligned, just add extra padding at the end */
6620 size =
6621 (sizeof(kmp_info_t *) + sizeof(kmp_root_t *)) * __kmp_threads_capacity +
6622 CACHE_LINE;
6623 __kmp_threads = (kmp_info_t **)__kmp_allocate(size);
6624 __kmp_root = (kmp_root_t **)((char *)__kmp_threads +
6625 sizeof(kmp_info_t *) * __kmp_threads_capacity);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006626
Jonathan Peyton30419822017-05-12 18:01:32 +00006627 /* init thread counts */
6628 KMP_DEBUG_ASSERT(__kmp_all_nth ==
6629 0); // Asserts fail if the library is reinitializing and
6630 KMP_DEBUG_ASSERT(__kmp_nth == 0); // something was wrong in termination.
6631 __kmp_all_nth = 0;
6632 __kmp_nth = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006633
Jonathan Peyton30419822017-05-12 18:01:32 +00006634 /* setup the uber master thread and hierarchy */
6635 gtid = __kmp_register_root(TRUE);
6636 KA_TRACE(10, ("__kmp_do_serial_initialize T#%d\n", gtid));
6637 KMP_ASSERT(KMP_UBER_GTID(gtid));
6638 KMP_ASSERT(KMP_INITIAL_GTID(gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00006639
Jonathan Peyton30419822017-05-12 18:01:32 +00006640 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00006641
Jonathan Peyton30419822017-05-12 18:01:32 +00006642 __kmp_common_initialize();
Jim Cownie5e8470a2013-09-27 10:38:44 +00006643
Jonathan Peyton30419822017-05-12 18:01:32 +00006644#if KMP_OS_UNIX
6645 /* invoke the child fork handler */
6646 __kmp_register_atfork();
6647#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00006648
Jonathan Peyton30419822017-05-12 18:01:32 +00006649#if !defined KMP_DYNAMIC_LIB
6650 {
6651 /* Invoke the exit handler when the program finishes, only for static
6652 library. For dynamic library, we already have _fini and DllMain. */
6653 int rc = atexit(__kmp_internal_end_atexit);
6654 if (rc != 0) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +00006655 __kmp_fatal(KMP_MSG(FunctionError, "atexit()"), KMP_ERR(rc),
6656 __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00006657 }
Jonathan Peyton30419822017-05-12 18:01:32 +00006658 }
6659#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00006660
Jonathan Peyton30419822017-05-12 18:01:32 +00006661#if KMP_HANDLE_SIGNALS
6662#if KMP_OS_UNIX
6663 /* NOTE: make sure that this is called before the user installs their own
6664 signal handlers so that the user handlers are called first. this way they
6665 can return false, not call our handler, avoid terminating the library, and
6666 continue execution where they left off. */
6667 __kmp_install_signals(FALSE);
6668#endif /* KMP_OS_UNIX */
6669#if KMP_OS_WINDOWS
6670 __kmp_install_signals(TRUE);
6671#endif /* KMP_OS_WINDOWS */
6672#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00006673
Jonathan Peyton30419822017-05-12 18:01:32 +00006674 /* we have finished the serial initialization */
6675 __kmp_init_counter++;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006676
Jonathan Peyton30419822017-05-12 18:01:32 +00006677 __kmp_init_serial = TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006678
Jonathan Peyton30419822017-05-12 18:01:32 +00006679 if (__kmp_settings) {
6680 __kmp_env_print();
6681 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006682
6683#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00006684 if (__kmp_display_env || __kmp_display_env_verbose) {
6685 __kmp_env_print_2();
6686 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006687#endif // OMP_40_ENABLED
6688
Jonathan Peyton82a13bf2015-09-21 18:01:02 +00006689#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00006690 ompt_post_init();
Jonathan Peyton82a13bf2015-09-21 18:01:02 +00006691#endif
6692
Jonathan Peyton30419822017-05-12 18:01:32 +00006693 KMP_MB();
Jim Cownie5e8470a2013-09-27 10:38:44 +00006694
Jonathan Peyton30419822017-05-12 18:01:32 +00006695 KA_TRACE(10, ("__kmp_do_serial_initialize: exit\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +00006696}
6697
Jonathan Peyton30419822017-05-12 18:01:32 +00006698void __kmp_serial_initialize(void) {
6699 if (__kmp_init_serial) {
6700 return;
6701 }
6702 __kmp_acquire_bootstrap_lock(&__kmp_initz_lock);
6703 if (__kmp_init_serial) {
6704 __kmp_release_bootstrap_lock(&__kmp_initz_lock);
6705 return;
6706 }
6707 __kmp_do_serial_initialize();
6708 __kmp_release_bootstrap_lock(&__kmp_initz_lock);
6709}
6710
6711static void __kmp_do_middle_initialize(void) {
6712 int i, j;
6713 int prev_dflt_team_nth;
6714
6715 if (!__kmp_init_serial) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00006716 __kmp_do_serial_initialize();
Jonathan Peyton30419822017-05-12 18:01:32 +00006717 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006718
Jonathan Peyton30419822017-05-12 18:01:32 +00006719 KA_TRACE(10, ("__kmp_middle_initialize: enter\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +00006720
Jonathan Peyton30419822017-05-12 18:01:32 +00006721 // Save the previous value for the __kmp_dflt_team_nth so that
6722 // we can avoid some reinitialization if it hasn't changed.
6723 prev_dflt_team_nth = __kmp_dflt_team_nth;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006724
Alp Toker98758b02014-03-02 04:12:06 +00006725#if KMP_AFFINITY_SUPPORTED
Jonathan Peyton30419822017-05-12 18:01:32 +00006726 // __kmp_affinity_initialize() will try to set __kmp_ncores to the
6727 // number of cores on the machine.
6728 __kmp_affinity_initialize();
Jim Cownie5e8470a2013-09-27 10:38:44 +00006729
Jonathan Peyton30419822017-05-12 18:01:32 +00006730 // Run through the __kmp_threads array and set the affinity mask
6731 // for each root thread that is currently registered with the RTL.
6732 for (i = 0; i < __kmp_threads_capacity; i++) {
6733 if (TCR_PTR(__kmp_threads[i]) != NULL) {
6734 __kmp_affinity_set_init_mask(i, TRUE);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006735 }
Jonathan Peyton30419822017-05-12 18:01:32 +00006736 }
Alp Toker98758b02014-03-02 04:12:06 +00006737#endif /* KMP_AFFINITY_SUPPORTED */
Jim Cownie5e8470a2013-09-27 10:38:44 +00006738
Jonathan Peyton30419822017-05-12 18:01:32 +00006739 KMP_ASSERT(__kmp_xproc > 0);
6740 if (__kmp_avail_proc == 0) {
6741 __kmp_avail_proc = __kmp_xproc;
6742 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006743
Jonathan Peyton30419822017-05-12 18:01:32 +00006744 // If there were empty places in num_threads list (OMP_NUM_THREADS=,,2,3),
6745 // correct them now
6746 j = 0;
6747 while ((j < __kmp_nested_nth.used) && !__kmp_nested_nth.nth[j]) {
6748 __kmp_nested_nth.nth[j] = __kmp_dflt_team_nth = __kmp_dflt_team_nth_ub =
6749 __kmp_avail_proc;
6750 j++;
6751 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006752
Jonathan Peyton30419822017-05-12 18:01:32 +00006753 if (__kmp_dflt_team_nth == 0) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00006754#ifdef KMP_DFLT_NTH_CORES
Jonathan Peyton30419822017-05-12 18:01:32 +00006755 // Default #threads = #cores
6756 __kmp_dflt_team_nth = __kmp_ncores;
6757 KA_TRACE(20, ("__kmp_middle_initialize: setting __kmp_dflt_team_nth = "
6758 "__kmp_ncores (%d)\n",
6759 __kmp_dflt_team_nth));
Jim Cownie5e8470a2013-09-27 10:38:44 +00006760#else
Jonathan Peyton30419822017-05-12 18:01:32 +00006761 // Default #threads = #available OS procs
6762 __kmp_dflt_team_nth = __kmp_avail_proc;
6763 KA_TRACE(20, ("__kmp_middle_initialize: setting __kmp_dflt_team_nth = "
6764 "__kmp_avail_proc(%d)\n",
6765 __kmp_dflt_team_nth));
Jim Cownie5e8470a2013-09-27 10:38:44 +00006766#endif /* KMP_DFLT_NTH_CORES */
Jonathan Peyton30419822017-05-12 18:01:32 +00006767 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006768
Jonathan Peyton30419822017-05-12 18:01:32 +00006769 if (__kmp_dflt_team_nth < KMP_MIN_NTH) {
6770 __kmp_dflt_team_nth = KMP_MIN_NTH;
6771 }
6772 if (__kmp_dflt_team_nth > __kmp_sys_max_nth) {
6773 __kmp_dflt_team_nth = __kmp_sys_max_nth;
6774 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006775
Jonathan Peyton30419822017-05-12 18:01:32 +00006776 // There's no harm in continuing if the following check fails,
6777 // but it indicates an error in the previous logic.
6778 KMP_DEBUG_ASSERT(__kmp_dflt_team_nth <= __kmp_dflt_team_nth_ub);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006779
Jonathan Peyton30419822017-05-12 18:01:32 +00006780 if (__kmp_dflt_team_nth != prev_dflt_team_nth) {
6781 // Run through the __kmp_threads array and set the num threads icv for each
6782 // root thread that is currently registered with the RTL (which has not
6783 // already explicitly set its nthreads-var with a call to
6784 // omp_set_num_threads()).
6785 for (i = 0; i < __kmp_threads_capacity; i++) {
6786 kmp_info_t *thread = __kmp_threads[i];
6787 if (thread == NULL)
6788 continue;
6789 if (thread->th.th_current_task->td_icvs.nproc != 0)
6790 continue;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006791
Jonathan Peyton30419822017-05-12 18:01:32 +00006792 set__nproc(__kmp_threads[i], __kmp_dflt_team_nth);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006793 }
Jonathan Peyton30419822017-05-12 18:01:32 +00006794 }
6795 KA_TRACE(
6796 20,
6797 ("__kmp_middle_initialize: final value for __kmp_dflt_team_nth = %d\n",
6798 __kmp_dflt_team_nth));
Jim Cownie5e8470a2013-09-27 10:38:44 +00006799
6800#ifdef KMP_ADJUST_BLOCKTIME
Jonathan Peyton30419822017-05-12 18:01:32 +00006801 /* Adjust blocktime to zero if necessary now that __kmp_avail_proc is set */
6802 if (!__kmp_env_blocktime && (__kmp_avail_proc > 0)) {
6803 KMP_DEBUG_ASSERT(__kmp_avail_proc > 0);
6804 if (__kmp_nth > __kmp_avail_proc) {
6805 __kmp_zero_bt = TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006806 }
Jonathan Peyton30419822017-05-12 18:01:32 +00006807 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006808#endif /* KMP_ADJUST_BLOCKTIME */
6809
Jonathan Peyton30419822017-05-12 18:01:32 +00006810 /* we have finished middle initialization */
6811 TCW_SYNC_4(__kmp_init_middle, TRUE);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006812
Jonathan Peyton30419822017-05-12 18:01:32 +00006813 KA_TRACE(10, ("__kmp_do_middle_initialize: exit\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +00006814}
6815
Jonathan Peyton30419822017-05-12 18:01:32 +00006816void __kmp_middle_initialize(void) {
6817 if (__kmp_init_middle) {
6818 return;
6819 }
6820 __kmp_acquire_bootstrap_lock(&__kmp_initz_lock);
6821 if (__kmp_init_middle) {
6822 __kmp_release_bootstrap_lock(&__kmp_initz_lock);
6823 return;
6824 }
6825 __kmp_do_middle_initialize();
6826 __kmp_release_bootstrap_lock(&__kmp_initz_lock);
6827}
6828
6829void __kmp_parallel_initialize(void) {
6830 int gtid = __kmp_entry_gtid(); // this might be a new root
6831
6832 /* synchronize parallel initialization (for sibling) */
6833 if (TCR_4(__kmp_init_parallel))
6834 return;
6835 __kmp_acquire_bootstrap_lock(&__kmp_initz_lock);
6836 if (TCR_4(__kmp_init_parallel)) {
6837 __kmp_release_bootstrap_lock(&__kmp_initz_lock);
6838 return;
6839 }
6840
6841 /* TODO reinitialization after we have already shut down */
6842 if (TCR_4(__kmp_global.g.g_done)) {
6843 KA_TRACE(
6844 10,
6845 ("__kmp_parallel_initialize: attempt to init while shutting down\n"));
6846 __kmp_infinite_loop();
6847 }
6848
6849 /* jc: The lock __kmp_initz_lock is already held, so calling
6850 __kmp_serial_initialize would cause a deadlock. So we call
6851 __kmp_do_serial_initialize directly. */
6852 if (!__kmp_init_middle) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00006853 __kmp_do_middle_initialize();
Jonathan Peyton30419822017-05-12 18:01:32 +00006854 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006855
Jonathan Peyton30419822017-05-12 18:01:32 +00006856 /* begin initialization */
6857 KA_TRACE(10, ("__kmp_parallel_initialize: enter\n"));
6858 KMP_ASSERT(KMP_UBER_GTID(gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00006859
6860#if KMP_ARCH_X86 || KMP_ARCH_X86_64
Jonathan Peyton30419822017-05-12 18:01:32 +00006861 // Save the FP control regs.
6862 // Worker threads will set theirs to these values at thread startup.
6863 __kmp_store_x87_fpu_control_word(&__kmp_init_x87_fpu_control_word);
6864 __kmp_store_mxcsr(&__kmp_init_mxcsr);
6865 __kmp_init_mxcsr &= KMP_X86_MXCSR_MASK;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006866#endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
6867
6868#if KMP_OS_UNIX
Jonathan Peyton30419822017-05-12 18:01:32 +00006869#if KMP_HANDLE_SIGNALS
6870 /* must be after __kmp_serial_initialize */
6871 __kmp_install_signals(TRUE);
6872#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00006873#endif
6874
Jonathan Peyton30419822017-05-12 18:01:32 +00006875 __kmp_suspend_initialize();
Jim Cownie5e8470a2013-09-27 10:38:44 +00006876
Jonathan Peyton749b4d52016-01-27 21:02:04 +00006877#if defined(USE_LOAD_BALANCE)
Jonathan Peyton30419822017-05-12 18:01:32 +00006878 if (__kmp_global.g.g_dynamic_mode == dynamic_default) {
6879 __kmp_global.g.g_dynamic_mode = dynamic_load_balance;
6880 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006881#else
Jonathan Peyton30419822017-05-12 18:01:32 +00006882 if (__kmp_global.g.g_dynamic_mode == dynamic_default) {
6883 __kmp_global.g.g_dynamic_mode = dynamic_thread_limit;
6884 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006885#endif
6886
Jonathan Peyton30419822017-05-12 18:01:32 +00006887 if (__kmp_version) {
6888 __kmp_print_version_2();
6889 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006890
Jonathan Peyton30419822017-05-12 18:01:32 +00006891 /* we have finished parallel initialization */
6892 TCW_SYNC_4(__kmp_init_parallel, TRUE);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006893
Jonathan Peyton30419822017-05-12 18:01:32 +00006894 KMP_MB();
6895 KA_TRACE(10, ("__kmp_parallel_initialize: exit\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +00006896
Jonathan Peyton30419822017-05-12 18:01:32 +00006897 __kmp_release_bootstrap_lock(&__kmp_initz_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006898}
6899
Jim Cownie5e8470a2013-09-27 10:38:44 +00006900/* ------------------------------------------------------------------------ */
6901
Jonathan Peyton30419822017-05-12 18:01:32 +00006902void __kmp_run_before_invoked_task(int gtid, int tid, kmp_info_t *this_thr,
6903 kmp_team_t *team) {
6904 kmp_disp_t *dispatch;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006905
Jonathan Peyton30419822017-05-12 18:01:32 +00006906 KMP_MB();
Jim Cownie5e8470a2013-09-27 10:38:44 +00006907
Jonathan Peyton30419822017-05-12 18:01:32 +00006908 /* none of the threads have encountered any constructs, yet. */
6909 this_thr->th.th_local.this_construct = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006910#if KMP_CACHE_MANAGE
Jonathan Peyton30419822017-05-12 18:01:32 +00006911 KMP_CACHE_PREFETCH(&this_thr->th.th_bar[bs_forkjoin_barrier].bb.b_arrived);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006912#endif /* KMP_CACHE_MANAGE */
Jonathan Peyton30419822017-05-12 18:01:32 +00006913 dispatch = (kmp_disp_t *)TCR_PTR(this_thr->th.th_dispatch);
6914 KMP_DEBUG_ASSERT(dispatch);
6915 KMP_DEBUG_ASSERT(team->t.t_dispatch);
6916 // KMP_DEBUG_ASSERT( this_thr->th.th_dispatch == &team->t.t_dispatch[
6917 // this_thr->th.th_info.ds.ds_tid ] );
Jim Cownie5e8470a2013-09-27 10:38:44 +00006918
Jonathan Peyton30419822017-05-12 18:01:32 +00006919 dispatch->th_disp_index = 0; /* reset the dispatch buffer counter */
Jonathan Peytondf6818b2016-06-14 17:57:47 +00006920#if OMP_45_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00006921 dispatch->th_doacross_buf_idx =
6922 0; /* reset the doacross dispatch buffer counter */
Jonathan Peyton71909c52016-03-02 22:42:06 +00006923#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00006924 if (__kmp_env_consistency_check)
6925 __kmp_push_parallel(gtid, team->t.t_ident);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006926
Jonathan Peyton30419822017-05-12 18:01:32 +00006927 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00006928}
6929
Jonathan Peyton30419822017-05-12 18:01:32 +00006930void __kmp_run_after_invoked_task(int gtid, int tid, kmp_info_t *this_thr,
6931 kmp_team_t *team) {
6932 if (__kmp_env_consistency_check)
6933 __kmp_pop_parallel(gtid, team->t.t_ident);
Andrey Churbanovdf0d75e2016-10-27 11:43:07 +00006934
Jonathan Peyton30419822017-05-12 18:01:32 +00006935 __kmp_finish_implicit_task(this_thr);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006936}
6937
Jonathan Peyton30419822017-05-12 18:01:32 +00006938int __kmp_invoke_task_func(int gtid) {
6939 int rc;
6940 int tid = __kmp_tid_from_gtid(gtid);
6941 kmp_info_t *this_thr = __kmp_threads[gtid];
6942 kmp_team_t *team = this_thr->th.th_team;
Jim Cownie5e8470a2013-09-27 10:38:44 +00006943
Jonathan Peyton30419822017-05-12 18:01:32 +00006944 __kmp_run_before_invoked_task(gtid, tid, this_thr, team);
Jim Cownie5e8470a2013-09-27 10:38:44 +00006945#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00006946 if (__itt_stack_caller_create_ptr) {
6947 __kmp_itt_stack_callee_enter(
6948 (__itt_caller)
6949 team->t.t_stack_id); // inform ittnotify about entering user's code
6950 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00006951#endif /* USE_ITT_BUILD */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00006952#if INCLUDE_SSC_MARKS
Jonathan Peyton30419822017-05-12 18:01:32 +00006953 SSC_MARK_INVOKING();
Jim Cownie4cc4bb42014-10-07 16:25:50 +00006954#endif
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00006955
6956#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00006957 void *dummy;
6958 void **exit_runtime_p;
Joachim Protze82e94a52017-11-01 10:08:30 +00006959 ompt_data_t *my_task_data;
6960 ompt_data_t *my_parallel_data;
6961 int ompt_team_size;
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00006962
Joachim Protze82e94a52017-11-01 10:08:30 +00006963 if (ompt_enabled.enabled) {
Joachim Protzec255ca72017-11-05 14:11:10 +00006964 exit_runtime_p = &(
6965 team->t.t_implicit_task_taskdata[tid].ompt_task_info.frame.exit_frame);
Jonathan Peyton30419822017-05-12 18:01:32 +00006966 } else {
6967 exit_runtime_p = &dummy;
6968 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00006969
Joachim Protze82e94a52017-11-01 10:08:30 +00006970 my_task_data =
6971 &(team->t.t_implicit_task_taskdata[tid].ompt_task_info.task_data);
6972 my_parallel_data = &(team->t.ompt_team_info.parallel_data);
6973 if (ompt_enabled.ompt_callback_implicit_task) {
6974 ompt_team_size = team->t.t_nproc;
6975 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
6976 ompt_scope_begin, my_parallel_data, my_task_data, ompt_team_size,
6977 __kmp_tid_from_gtid(gtid));
Joachim Protze9be9cf22018-05-07 12:42:21 +00006978 OMPT_CUR_TASK_INFO(this_thr)->thread_num = __kmp_tid_from_gtid(gtid);
Jonathan Peyton30419822017-05-12 18:01:32 +00006979 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00006980#endif
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00006981
Jonathan Peyton30419822017-05-12 18:01:32 +00006982 {
6983 KMP_TIME_PARTITIONED_BLOCK(OMP_parallel);
6984 KMP_SET_THREAD_STATE_BLOCK(IMPLICIT_TASK);
6985 rc =
6986 __kmp_invoke_microtask((microtask_t)TCR_SYNC_PTR(team->t.t_pkfn), gtid,
6987 tid, (int)team->t.t_argc, (void **)team->t.t_argv
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00006988#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00006989 ,
6990 exit_runtime_p
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00006991#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00006992 );
Jonas Hahnfeld8a270642016-09-14 13:59:19 +00006993#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00006994 *exit_runtime_p = NULL;
Jonas Hahnfeld8a270642016-09-14 13:59:19 +00006995#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00006996 }
Andrey Churbanovd7d088f2015-04-29 16:42:24 +00006997
Jim Cownie5e8470a2013-09-27 10:38:44 +00006998#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00006999 if (__itt_stack_caller_create_ptr) {
7000 __kmp_itt_stack_callee_leave(
7001 (__itt_caller)
7002 team->t.t_stack_id); // inform ittnotify about leaving user's code
7003 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00007004#endif /* USE_ITT_BUILD */
Jonathan Peyton30419822017-05-12 18:01:32 +00007005 __kmp_run_after_invoked_task(gtid, tid, this_thr, team);
Jim Cownie5e8470a2013-09-27 10:38:44 +00007006
Jonathan Peyton30419822017-05-12 18:01:32 +00007007 return rc;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007008}
7009
7010#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00007011void __kmp_teams_master(int gtid) {
7012 // This routine is called by all master threads in teams construct
7013 kmp_info_t *thr = __kmp_threads[gtid];
7014 kmp_team_t *team = thr->th.th_team;
7015 ident_t *loc = team->t.t_ident;
7016 thr->th.th_set_nproc = thr->th.th_teams_size.nth;
7017 KMP_DEBUG_ASSERT(thr->th.th_teams_microtask);
7018 KMP_DEBUG_ASSERT(thr->th.th_set_nproc);
7019 KA_TRACE(20, ("__kmp_teams_master: T#%d, Tid %d, microtask %p\n", gtid,
7020 __kmp_tid_from_gtid(gtid), thr->th.th_teams_microtask));
7021// Launch league of teams now, but not let workers execute
7022// (they hang on fork barrier until next parallel)
Jim Cownie4cc4bb42014-10-07 16:25:50 +00007023#if INCLUDE_SSC_MARKS
Jonathan Peyton30419822017-05-12 18:01:32 +00007024 SSC_MARK_FORKING();
Jim Cownie4cc4bb42014-10-07 16:25:50 +00007025#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00007026 __kmp_fork_call(loc, gtid, fork_context_intel, team->t.t_argc,
Jonathan Peyton30419822017-05-12 18:01:32 +00007027 (microtask_t)thr->th.th_teams_microtask, // "wrapped" task
7028 VOLATILE_CAST(launch_t) __kmp_invoke_task_func, NULL);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00007029#if INCLUDE_SSC_MARKS
Jonathan Peyton30419822017-05-12 18:01:32 +00007030 SSC_MARK_JOINING();
Jim Cownie4cc4bb42014-10-07 16:25:50 +00007031#endif
Jonathan Peyton61118492016-05-20 19:03:38 +00007032
Jonathan Peyton30419822017-05-12 18:01:32 +00007033 // AC: last parameter "1" eliminates join barrier which won't work because
7034 // worker threads are in a fork barrier waiting for more parallel regions
7035 __kmp_join_call(loc, gtid
Jonathan Peytonf89fbbb2015-08-31 18:15:00 +00007036#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00007037 ,
7038 fork_context_intel
Jonathan Peytonf89fbbb2015-08-31 18:15:00 +00007039#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00007040 ,
7041 1);
Jim Cownie5e8470a2013-09-27 10:38:44 +00007042}
7043
Jonathan Peyton30419822017-05-12 18:01:32 +00007044int __kmp_invoke_teams_master(int gtid) {
7045 kmp_info_t *this_thr = __kmp_threads[gtid];
7046 kmp_team_t *team = this_thr->th.th_team;
7047#if KMP_DEBUG
7048 if (!__kmp_threads[gtid]->th.th_team->t.t_serialized)
7049 KMP_DEBUG_ASSERT((void *)__kmp_threads[gtid]->th.th_team->t.t_pkfn ==
7050 (void *)__kmp_teams_master);
7051#endif
7052 __kmp_run_before_invoked_task(gtid, 0, this_thr, team);
7053 __kmp_teams_master(gtid);
7054 __kmp_run_after_invoked_task(gtid, 0, this_thr, team);
7055 return 1;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007056}
7057#endif /* OMP_40_ENABLED */
7058
7059/* this sets the requested number of threads for the next parallel region
Jonathan Peyton30419822017-05-12 18:01:32 +00007060 encountered by this team. since this should be enclosed in the forkjoin
7061 critical section it should avoid race conditions with assymmetrical nested
7062 parallelism */
Jim Cownie5e8470a2013-09-27 10:38:44 +00007063
Jonathan Peyton30419822017-05-12 18:01:32 +00007064void __kmp_push_num_threads(ident_t *id, int gtid, int num_threads) {
7065 kmp_info_t *thr = __kmp_threads[gtid];
Jim Cownie5e8470a2013-09-27 10:38:44 +00007066
Jonathan Peyton30419822017-05-12 18:01:32 +00007067 if (num_threads > 0)
7068 thr->th.th_set_nproc = num_threads;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007069}
7070
7071#if OMP_40_ENABLED
7072
7073/* this sets the requested number of teams for the teams region and/or
Jonathan Peyton30419822017-05-12 18:01:32 +00007074 the number of threads for the next parallel region encountered */
7075void __kmp_push_num_teams(ident_t *id, int gtid, int num_teams,
7076 int num_threads) {
7077 kmp_info_t *thr = __kmp_threads[gtid];
7078 KMP_DEBUG_ASSERT(num_teams >= 0);
7079 KMP_DEBUG_ASSERT(num_threads >= 0);
Jonathan Peyton1be692e2015-11-30 20:14:05 +00007080
Jonathan Peyton30419822017-05-12 18:01:32 +00007081 if (num_teams == 0)
7082 num_teams = 1; // default number of teams is 1.
Jonathan Peyton4f90c822017-08-02 20:04:45 +00007083 if (num_teams > __kmp_teams_max_nth) { // if too many teams requested?
Jonathan Peyton30419822017-05-12 18:01:32 +00007084 if (!__kmp_reserve_warn) {
7085 __kmp_reserve_warn = 1;
7086 __kmp_msg(kmp_ms_warning,
Jonathan Peyton4f90c822017-08-02 20:04:45 +00007087 KMP_MSG(CantFormThrTeam, num_teams, __kmp_teams_max_nth),
Jonathan Peyton30419822017-05-12 18:01:32 +00007088 KMP_HNT(Unset_ALL_THREADS), __kmp_msg_null);
Jim Cownie5e8470a2013-09-27 10:38:44 +00007089 }
Jonathan Peyton4f90c822017-08-02 20:04:45 +00007090 num_teams = __kmp_teams_max_nth;
Jonathan Peyton30419822017-05-12 18:01:32 +00007091 }
7092 // Set number of teams (number of threads in the outer "parallel" of the
7093 // teams)
7094 thr->th.th_set_nproc = thr->th.th_teams_size.nteams = num_teams;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00007095
Jonathan Peyton30419822017-05-12 18:01:32 +00007096 // Remember the number of threads for inner parallel regions
7097 if (num_threads == 0) {
7098 if (!TCR_4(__kmp_init_middle))
7099 __kmp_middle_initialize(); // get __kmp_avail_proc calculated
7100 num_threads = __kmp_avail_proc / num_teams;
Jonathan Peyton4f90c822017-08-02 20:04:45 +00007101 if (num_teams * num_threads > __kmp_teams_max_nth) {
Jonathan Peyton30419822017-05-12 18:01:32 +00007102 // adjust num_threads w/o warning as it is not user setting
Jonathan Peyton4f90c822017-08-02 20:04:45 +00007103 num_threads = __kmp_teams_max_nth / num_teams;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007104 }
Jonathan Peyton30419822017-05-12 18:01:32 +00007105 } else {
Jonathan Peyton4f90c822017-08-02 20:04:45 +00007106 if (num_teams * num_threads > __kmp_teams_max_nth) {
7107 int new_threads = __kmp_teams_max_nth / num_teams;
Jonathan Peyton30419822017-05-12 18:01:32 +00007108 if (!__kmp_reserve_warn) { // user asked for too many threads
Jonathan Peyton4f90c822017-08-02 20:04:45 +00007109 __kmp_reserve_warn = 1; // that conflicts with KMP_TEAMS_THREAD_LIMIT
Jonathan Peyton30419822017-05-12 18:01:32 +00007110 __kmp_msg(kmp_ms_warning,
7111 KMP_MSG(CantFormThrTeam, num_threads, new_threads),
7112 KMP_HNT(Unset_ALL_THREADS), __kmp_msg_null);
7113 }
7114 num_threads = new_threads;
7115 }
7116 }
7117 thr->th.th_teams_size.nth = num_threads;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007118}
7119
Jim Cownie5e8470a2013-09-27 10:38:44 +00007120// Set the proc_bind var to use in the following parallel region.
Jonathan Peyton30419822017-05-12 18:01:32 +00007121void __kmp_push_proc_bind(ident_t *id, int gtid, kmp_proc_bind_t proc_bind) {
7122 kmp_info_t *thr = __kmp_threads[gtid];
7123 thr->th.th_set_proc_bind = proc_bind;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007124}
7125
7126#endif /* OMP_40_ENABLED */
7127
7128/* Launch the worker threads into the microtask. */
7129
Jonathan Peyton30419822017-05-12 18:01:32 +00007130void __kmp_internal_fork(ident_t *id, int gtid, kmp_team_t *team) {
7131 kmp_info_t *this_thr = __kmp_threads[gtid];
Jim Cownie5e8470a2013-09-27 10:38:44 +00007132
7133#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00007134 int f;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007135#endif /* KMP_DEBUG */
7136
Jonathan Peyton30419822017-05-12 18:01:32 +00007137 KMP_DEBUG_ASSERT(team);
7138 KMP_DEBUG_ASSERT(this_thr->th.th_team == team);
7139 KMP_ASSERT(KMP_MASTER_GTID(gtid));
7140 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00007141
Jonathan Peyton30419822017-05-12 18:01:32 +00007142 team->t.t_construct = 0; /* no single directives seen yet */
7143 team->t.t_ordered.dt.t_value =
7144 0; /* thread 0 enters the ordered section first */
Jim Cownie5e8470a2013-09-27 10:38:44 +00007145
Jonathan Peyton30419822017-05-12 18:01:32 +00007146 /* Reset the identifiers on the dispatch buffer */
7147 KMP_DEBUG_ASSERT(team->t.t_disp_buffer);
7148 if (team->t.t_max_nproc > 1) {
7149 int i;
7150 for (i = 0; i < __kmp_dispatch_num_buffers; ++i) {
7151 team->t.t_disp_buffer[i].buffer_index = i;
Jonathan Peytondf6818b2016-06-14 17:57:47 +00007152#if OMP_45_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00007153 team->t.t_disp_buffer[i].doacross_buf_idx = i;
Jonathan Peyton71909c52016-03-02 22:42:06 +00007154#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00007155 }
Jonathan Peyton30419822017-05-12 18:01:32 +00007156 } else {
7157 team->t.t_disp_buffer[0].buffer_index = 0;
7158#if OMP_45_ENABLED
7159 team->t.t_disp_buffer[0].doacross_buf_idx = 0;
7160#endif
7161 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00007162
Jonathan Peyton30419822017-05-12 18:01:32 +00007163 KMP_MB(); /* Flush all pending memory write invalidates. */
7164 KMP_ASSERT(this_thr->th.th_team == team);
Jim Cownie5e8470a2013-09-27 10:38:44 +00007165
7166#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00007167 for (f = 0; f < team->t.t_nproc; f++) {
7168 KMP_DEBUG_ASSERT(team->t.t_threads[f] &&
7169 team->t.t_threads[f]->th.th_team_nproc == team->t.t_nproc);
7170 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00007171#endif /* KMP_DEBUG */
7172
Jonathan Peyton30419822017-05-12 18:01:32 +00007173 /* release the worker threads so they may begin working */
7174 __kmp_fork_barrier(gtid, 0);
Jim Cownie5e8470a2013-09-27 10:38:44 +00007175}
7176
Jonathan Peyton30419822017-05-12 18:01:32 +00007177void __kmp_internal_join(ident_t *id, int gtid, kmp_team_t *team) {
7178 kmp_info_t *this_thr = __kmp_threads[gtid];
Jim Cownie5e8470a2013-09-27 10:38:44 +00007179
Jonathan Peyton30419822017-05-12 18:01:32 +00007180 KMP_DEBUG_ASSERT(team);
7181 KMP_DEBUG_ASSERT(this_thr->th.th_team == team);
7182 KMP_ASSERT(KMP_MASTER_GTID(gtid));
7183 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00007184
Jonathan Peyton30419822017-05-12 18:01:32 +00007185/* Join barrier after fork */
Jim Cownie5e8470a2013-09-27 10:38:44 +00007186
7187#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00007188 if (__kmp_threads[gtid] &&
7189 __kmp_threads[gtid]->th.th_team_nproc != team->t.t_nproc) {
7190 __kmp_printf("GTID: %d, __kmp_threads[%d]=%p\n", gtid, gtid,
7191 __kmp_threads[gtid]);
7192 __kmp_printf("__kmp_threads[%d]->th.th_team_nproc=%d, TEAM: %p, "
7193 "team->t.t_nproc=%d\n",
7194 gtid, __kmp_threads[gtid]->th.th_team_nproc, team,
7195 team->t.t_nproc);
7196 __kmp_print_structure();
7197 }
7198 KMP_DEBUG_ASSERT(__kmp_threads[gtid] &&
7199 __kmp_threads[gtid]->th.th_team_nproc == team->t.t_nproc);
Jim Cownie5e8470a2013-09-27 10:38:44 +00007200#endif /* KMP_DEBUG */
7201
Jonathan Peyton30419822017-05-12 18:01:32 +00007202 __kmp_join_barrier(gtid); /* wait for everyone */
Joachim Protze82e94a52017-11-01 10:08:30 +00007203#if OMPT_SUPPORT
Jonas Hahnfeld82768d02018-02-23 16:46:25 +00007204 if (ompt_enabled.enabled &&
7205 this_thr->th.ompt_thread_info.state == omp_state_wait_barrier_implicit) {
7206 int ds_tid = this_thr->th.th_info.ds.ds_tid;
7207 ompt_data_t *task_data = OMPT_CUR_TASK_DATA(this_thr);
Joachim Protze82e94a52017-11-01 10:08:30 +00007208 this_thr->th.ompt_thread_info.state = omp_state_overhead;
7209#if OMPT_OPTIONAL
7210 void *codeptr = NULL;
7211 if (KMP_MASTER_TID(ds_tid) &&
7212 (ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait) ||
7213 ompt_callbacks.ompt_callback(ompt_callback_sync_region)))
7214 codeptr = OMPT_CUR_TEAM_INFO(this_thr)->master_return_address;
7215
7216 if (ompt_enabled.ompt_callback_sync_region_wait) {
7217 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
Jonas Hahnfeld82768d02018-02-23 16:46:25 +00007218 ompt_sync_region_barrier, ompt_scope_end, NULL, task_data, codeptr);
Joachim Protze82e94a52017-11-01 10:08:30 +00007219 }
7220 if (ompt_enabled.ompt_callback_sync_region) {
7221 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
Jonas Hahnfeld82768d02018-02-23 16:46:25 +00007222 ompt_sync_region_barrier, ompt_scope_end, NULL, task_data, codeptr);
Joachim Protze82e94a52017-11-01 10:08:30 +00007223 }
7224#endif
7225 if (!KMP_MASTER_TID(ds_tid) && ompt_enabled.ompt_callback_implicit_task) {
7226 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
Jonas Hahnfeld82768d02018-02-23 16:46:25 +00007227 ompt_scope_end, NULL, task_data, 0, ds_tid);
Joachim Protze82e94a52017-11-01 10:08:30 +00007228 }
Joachim Protze82e94a52017-11-01 10:08:30 +00007229 }
7230#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00007231
Jonathan Peyton30419822017-05-12 18:01:32 +00007232 KMP_MB(); /* Flush all pending memory write invalidates. */
7233 KMP_ASSERT(this_thr->th.th_team == team);
Jim Cownie5e8470a2013-09-27 10:38:44 +00007234}
7235
Jim Cownie5e8470a2013-09-27 10:38:44 +00007236/* ------------------------------------------------------------------------ */
7237
7238#ifdef USE_LOAD_BALANCE
7239
Jim Cownie5e8470a2013-09-27 10:38:44 +00007240// Return the worker threads actively spinning in the hot team, if we
7241// are at the outermost level of parallelism. Otherwise, return 0.
Jonathan Peyton30419822017-05-12 18:01:32 +00007242static int __kmp_active_hot_team_nproc(kmp_root_t *root) {
7243 int i;
7244 int retval;
7245 kmp_team_t *hot_team;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007246
Jonathan Peyton30419822017-05-12 18:01:32 +00007247 if (root->r.r_active) {
7248 return 0;
7249 }
7250 hot_team = root->r.r_hot_team;
7251 if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME) {
7252 return hot_team->t.t_nproc - 1; // Don't count master thread
7253 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00007254
Jonathan Peyton30419822017-05-12 18:01:32 +00007255 // Skip the master thread - it is accounted for elsewhere.
7256 retval = 0;
7257 for (i = 1; i < hot_team->t.t_nproc; i++) {
7258 if (hot_team->t.t_threads[i]->th.th_active) {
7259 retval++;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007260 }
Jonathan Peyton30419822017-05-12 18:01:32 +00007261 }
7262 return retval;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007263}
7264
Jim Cownie5e8470a2013-09-27 10:38:44 +00007265// Perform an automatic adjustment to the number of
7266// threads used by the next parallel region.
Jonathan Peyton30419822017-05-12 18:01:32 +00007267static int __kmp_load_balance_nproc(kmp_root_t *root, int set_nproc) {
7268 int retval;
7269 int pool_active;
7270 int hot_team_active;
7271 int team_curr_active;
7272 int system_active;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007273
Jonathan Peyton30419822017-05-12 18:01:32 +00007274 KB_TRACE(20, ("__kmp_load_balance_nproc: called root:%p set_nproc:%d\n", root,
7275 set_nproc));
7276 KMP_DEBUG_ASSERT(root);
7277 KMP_DEBUG_ASSERT(root->r.r_root_team->t.t_threads[0]
7278 ->th.th_current_task->td_icvs.dynamic == TRUE);
7279 KMP_DEBUG_ASSERT(set_nproc > 1);
Jim Cownie5e8470a2013-09-27 10:38:44 +00007280
Jonathan Peyton30419822017-05-12 18:01:32 +00007281 if (set_nproc == 1) {
7282 KB_TRACE(20, ("__kmp_load_balance_nproc: serial execution.\n"));
7283 return 1;
7284 }
7285
7286 // Threads that are active in the thread pool, active in the hot team for this
7287 // particular root (if we are at the outer par level), and the currently
7288 // executing thread (to become the master) are available to add to the new
7289 // team, but are currently contributing to the system load, and must be
7290 // accounted for.
Jonathan Peyton37e2ef52018-07-09 17:36:22 +00007291 pool_active = __kmp_thread_pool_active_nth;
Jonathan Peyton30419822017-05-12 18:01:32 +00007292 hot_team_active = __kmp_active_hot_team_nproc(root);
7293 team_curr_active = pool_active + hot_team_active + 1;
7294
7295 // Check the system load.
7296 system_active = __kmp_get_load_balance(__kmp_avail_proc + team_curr_active);
7297 KB_TRACE(30, ("__kmp_load_balance_nproc: system active = %d pool active = %d "
7298 "hot team active = %d\n",
7299 system_active, pool_active, hot_team_active));
7300
7301 if (system_active < 0) {
7302 // There was an error reading the necessary info from /proc, so use the
7303 // thread limit algorithm instead. Once we set __kmp_global.g.g_dynamic_mode
7304 // = dynamic_thread_limit, we shouldn't wind up getting back here.
7305 __kmp_global.g.g_dynamic_mode = dynamic_thread_limit;
7306 KMP_WARNING(CantLoadBalUsing, "KMP_DYNAMIC_MODE=thread limit");
7307
7308 // Make this call behave like the thread limit algorithm.
7309 retval = __kmp_avail_proc - __kmp_nth +
7310 (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc);
7311 if (retval > set_nproc) {
7312 retval = set_nproc;
7313 }
7314 if (retval < KMP_MIN_NTH) {
7315 retval = KMP_MIN_NTH;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007316 }
7317
Jonathan Peyton30419822017-05-12 18:01:32 +00007318 KB_TRACE(20, ("__kmp_load_balance_nproc: thread limit exit. retval:%d\n",
7319 retval));
Jim Cownie5e8470a2013-09-27 10:38:44 +00007320 return retval;
Jonathan Peyton30419822017-05-12 18:01:32 +00007321 }
7322
7323 // There is a slight delay in the load balance algorithm in detecting new
7324 // running procs. The real system load at this instant should be at least as
7325 // large as the #active omp thread that are available to add to the team.
7326 if (system_active < team_curr_active) {
7327 system_active = team_curr_active;
7328 }
7329 retval = __kmp_avail_proc - system_active + team_curr_active;
7330 if (retval > set_nproc) {
7331 retval = set_nproc;
7332 }
7333 if (retval < KMP_MIN_NTH) {
7334 retval = KMP_MIN_NTH;
7335 }
7336
7337 KB_TRACE(20, ("__kmp_load_balance_nproc: exit. retval:%d\n", retval));
7338 return retval;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007339} // __kmp_load_balance_nproc()
7340
7341#endif /* USE_LOAD_BALANCE */
7342
Jim Cownie5e8470a2013-09-27 10:38:44 +00007343/* ------------------------------------------------------------------------ */
Jim Cownie5e8470a2013-09-27 10:38:44 +00007344
7345/* NOTE: this is called with the __kmp_init_lock held */
Jonathan Peyton30419822017-05-12 18:01:32 +00007346void __kmp_cleanup(void) {
7347 int f;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007348
Jonathan Peyton30419822017-05-12 18:01:32 +00007349 KA_TRACE(10, ("__kmp_cleanup: enter\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +00007350
Jonathan Peyton30419822017-05-12 18:01:32 +00007351 if (TCR_4(__kmp_init_parallel)) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00007352#if KMP_HANDLE_SIGNALS
Jonathan Peyton30419822017-05-12 18:01:32 +00007353 __kmp_remove_signals();
Jim Cownie5e8470a2013-09-27 10:38:44 +00007354#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00007355 TCW_4(__kmp_init_parallel, FALSE);
7356 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00007357
Jonathan Peyton30419822017-05-12 18:01:32 +00007358 if (TCR_4(__kmp_init_middle)) {
Alp Toker763b9392014-02-28 09:42:41 +00007359#if KMP_AFFINITY_SUPPORTED
Jonathan Peyton30419822017-05-12 18:01:32 +00007360 __kmp_affinity_uninitialize();
Alp Toker763b9392014-02-28 09:42:41 +00007361#endif /* KMP_AFFINITY_SUPPORTED */
Jonathan Peyton30419822017-05-12 18:01:32 +00007362 __kmp_cleanup_hierarchy();
7363 TCW_4(__kmp_init_middle, FALSE);
7364 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00007365
Jonathan Peyton30419822017-05-12 18:01:32 +00007366 KA_TRACE(10, ("__kmp_cleanup: go serial cleanup\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +00007367
Jonathan Peyton30419822017-05-12 18:01:32 +00007368 if (__kmp_init_serial) {
7369 __kmp_runtime_destroy();
7370 __kmp_init_serial = FALSE;
7371 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00007372
Andrey Churbanov9e9333a2018-03-05 18:42:01 +00007373 __kmp_cleanup_threadprivate_caches();
7374
Jonathan Peyton30419822017-05-12 18:01:32 +00007375 for (f = 0; f < __kmp_threads_capacity; f++) {
7376 if (__kmp_root[f] != NULL) {
7377 __kmp_free(__kmp_root[f]);
7378 __kmp_root[f] = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007379 }
Jonathan Peyton30419822017-05-12 18:01:32 +00007380 }
7381 __kmp_free(__kmp_threads);
7382 // __kmp_threads and __kmp_root were allocated at once, as single block, so
7383 // there is no need in freeing __kmp_root.
7384 __kmp_threads = NULL;
7385 __kmp_root = NULL;
7386 __kmp_threads_capacity = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007387
Andrey Churbanov5c56fb52015-02-20 18:05:17 +00007388#if KMP_USE_DYNAMIC_LOCK
Jonathan Peyton30419822017-05-12 18:01:32 +00007389 __kmp_cleanup_indirect_user_locks();
Andrey Churbanov5c56fb52015-02-20 18:05:17 +00007390#else
Jonathan Peyton30419822017-05-12 18:01:32 +00007391 __kmp_cleanup_user_locks();
Andrey Churbanov5c56fb52015-02-20 18:05:17 +00007392#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00007393
Jonathan Peyton30419822017-05-12 18:01:32 +00007394#if KMP_AFFINITY_SUPPORTED
Andrey Churbanovc47afcd2017-07-03 11:24:08 +00007395 KMP_INTERNAL_FREE(CCAST(char *, __kmp_cpuinfo_file));
Jonathan Peyton30419822017-05-12 18:01:32 +00007396 __kmp_cpuinfo_file = NULL;
7397#endif /* KMP_AFFINITY_SUPPORTED */
Jim Cownie5e8470a2013-09-27 10:38:44 +00007398
Jonathan Peyton30419822017-05-12 18:01:32 +00007399#if KMP_USE_ADAPTIVE_LOCKS
7400#if KMP_DEBUG_ADAPTIVE_LOCKS
7401 __kmp_print_speculative_stats();
7402#endif
7403#endif
7404 KMP_INTERNAL_FREE(__kmp_nested_nth.nth);
7405 __kmp_nested_nth.nth = NULL;
7406 __kmp_nested_nth.size = 0;
7407 __kmp_nested_nth.used = 0;
7408 KMP_INTERNAL_FREE(__kmp_nested_proc_bind.bind_types);
7409 __kmp_nested_proc_bind.bind_types = NULL;
7410 __kmp_nested_proc_bind.size = 0;
7411 __kmp_nested_proc_bind.used = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007412
Jonathan Peyton30419822017-05-12 18:01:32 +00007413 __kmp_i18n_catclose();
Jim Cownie5e8470a2013-09-27 10:38:44 +00007414
Jonathan Peytonf6399362018-07-09 17:51:13 +00007415#if KMP_USE_HIER_SCHED
7416 __kmp_hier_scheds.deallocate();
7417#endif
7418
Jim Cownie4cc4bb42014-10-07 16:25:50 +00007419#if KMP_STATS_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00007420 __kmp_stats_fini();
Jim Cownie4cc4bb42014-10-07 16:25:50 +00007421#endif
7422
Jonathan Peyton30419822017-05-12 18:01:32 +00007423 KA_TRACE(10, ("__kmp_cleanup: exit\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +00007424}
7425
7426/* ------------------------------------------------------------------------ */
Jonathan Peyton30419822017-05-12 18:01:32 +00007427
7428int __kmp_ignore_mppbeg(void) {
7429 char *env;
7430
7431 if ((env = getenv("KMP_IGNORE_MPPBEG")) != NULL) {
7432 if (__kmp_str_match_false(env))
7433 return FALSE;
7434 }
7435 // By default __kmpc_begin() is no-op.
7436 return TRUE;
7437}
7438
7439int __kmp_ignore_mppend(void) {
7440 char *env;
7441
7442 if ((env = getenv("KMP_IGNORE_MPPEND")) != NULL) {
7443 if (__kmp_str_match_false(env))
7444 return FALSE;
7445 }
7446 // By default __kmpc_end() is no-op.
7447 return TRUE;
7448}
7449
7450void __kmp_internal_begin(void) {
7451 int gtid;
7452 kmp_root_t *root;
7453
7454 /* this is a very important step as it will register new sibling threads
7455 and assign these new uber threads a new gtid */
7456 gtid = __kmp_entry_gtid();
7457 root = __kmp_threads[gtid]->th.th_root;
7458 KMP_ASSERT(KMP_UBER_GTID(gtid));
7459
7460 if (root->r.r_begin)
7461 return;
7462 __kmp_acquire_lock(&root->r.r_begin_lock, gtid);
7463 if (root->r.r_begin) {
7464 __kmp_release_lock(&root->r.r_begin_lock, gtid);
7465 return;
7466 }
7467
7468 root->r.r_begin = TRUE;
7469
7470 __kmp_release_lock(&root->r.r_begin_lock, gtid);
7471}
7472
Jim Cownie5e8470a2013-09-27 10:38:44 +00007473/* ------------------------------------------------------------------------ */
7474
Jonathan Peyton30419822017-05-12 18:01:32 +00007475void __kmp_user_set_library(enum library_type arg) {
7476 int gtid;
7477 kmp_root_t *root;
7478 kmp_info_t *thread;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007479
Jonathan Peyton30419822017-05-12 18:01:32 +00007480 /* first, make sure we are initialized so we can get our gtid */
7481
7482 gtid = __kmp_entry_gtid();
7483 thread = __kmp_threads[gtid];
7484
7485 root = thread->th.th_root;
7486
7487 KA_TRACE(20, ("__kmp_user_set_library: enter T#%d, arg: %d, %d\n", gtid, arg,
7488 library_serial));
7489 if (root->r.r_in_parallel) { /* Must be called in serial section of top-level
7490 thread */
7491 KMP_WARNING(SetLibraryIncorrectCall);
7492 return;
7493 }
7494
7495 switch (arg) {
7496 case library_serial:
7497 thread->th.th_set_nproc = 0;
7498 set__nproc(thread, 1);
7499 break;
7500 case library_turnaround:
7501 thread->th.th_set_nproc = 0;
7502 set__nproc(thread, __kmp_dflt_team_nth ? __kmp_dflt_team_nth
7503 : __kmp_dflt_team_nth_ub);
7504 break;
7505 case library_throughput:
7506 thread->th.th_set_nproc = 0;
7507 set__nproc(thread, __kmp_dflt_team_nth ? __kmp_dflt_team_nth
7508 : __kmp_dflt_team_nth_ub);
7509 break;
7510 default:
7511 KMP_FATAL(UnknownLibraryType, arg);
7512 }
7513
7514 __kmp_aux_set_library(arg);
Jim Cownie5e8470a2013-09-27 10:38:44 +00007515}
7516
Jonathan Peyton30419822017-05-12 18:01:32 +00007517void __kmp_aux_set_stacksize(size_t arg) {
7518 if (!__kmp_init_serial)
7519 __kmp_serial_initialize();
Jim Cownie5e8470a2013-09-27 10:38:44 +00007520
7521#if KMP_OS_DARWIN
Jonathan Peyton30419822017-05-12 18:01:32 +00007522 if (arg & (0x1000 - 1)) {
7523 arg &= ~(0x1000 - 1);
7524 if (arg + 0x1000) /* check for overflow if we round up */
7525 arg += 0x1000;
7526 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00007527#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00007528 __kmp_acquire_bootstrap_lock(&__kmp_initz_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00007529
Jonathan Peyton30419822017-05-12 18:01:32 +00007530 /* only change the default stacksize before the first parallel region */
7531 if (!TCR_4(__kmp_init_parallel)) {
7532 size_t value = arg; /* argument is in bytes */
Jim Cownie5e8470a2013-09-27 10:38:44 +00007533
Jonathan Peyton30419822017-05-12 18:01:32 +00007534 if (value < __kmp_sys_min_stksize)
7535 value = __kmp_sys_min_stksize;
7536 else if (value > KMP_MAX_STKSIZE)
7537 value = KMP_MAX_STKSIZE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007538
Jonathan Peyton30419822017-05-12 18:01:32 +00007539 __kmp_stksize = value;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007540
Jonathan Peyton30419822017-05-12 18:01:32 +00007541 __kmp_env_stksize = TRUE; /* was KMP_STACKSIZE specified? */
7542 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00007543
Jonathan Peyton30419822017-05-12 18:01:32 +00007544 __kmp_release_bootstrap_lock(&__kmp_initz_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00007545}
7546
7547/* set the behaviour of the runtime library */
7548/* TODO this can cause some odd behaviour with sibling parallelism... */
Jonathan Peyton30419822017-05-12 18:01:32 +00007549void __kmp_aux_set_library(enum library_type arg) {
7550 __kmp_library = arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007551
Jonathan Peyton30419822017-05-12 18:01:32 +00007552 switch (__kmp_library) {
7553 case library_serial: {
7554 KMP_INFORM(LibraryIsSerial);
7555 (void)__kmp_change_library(TRUE);
7556 } break;
7557 case library_turnaround:
7558 (void)__kmp_change_library(TRUE);
7559 break;
7560 case library_throughput:
7561 (void)__kmp_change_library(FALSE);
7562 break;
7563 default:
7564 KMP_FATAL(UnknownLibraryType, arg);
7565 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00007566}
7567
7568/* ------------------------------------------------------------------------ */
Jim Cownie5e8470a2013-09-27 10:38:44 +00007569
Jonathan Peyton30419822017-05-12 18:01:32 +00007570void __kmp_aux_set_blocktime(int arg, kmp_info_t *thread, int tid) {
7571 int blocktime = arg; /* argument is in milliseconds */
Jonathan Peytone1c7c132016-10-07 18:12:19 +00007572#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +00007573 int bt_intervals;
Jonathan Peytone1c7c132016-10-07 18:12:19 +00007574#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00007575 int bt_set;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007576
Jonathan Peyton30419822017-05-12 18:01:32 +00007577 __kmp_save_internal_controls(thread);
Jim Cownie5e8470a2013-09-27 10:38:44 +00007578
Jonathan Peyton30419822017-05-12 18:01:32 +00007579 /* Normalize and set blocktime for the teams */
7580 if (blocktime < KMP_MIN_BLOCKTIME)
7581 blocktime = KMP_MIN_BLOCKTIME;
7582 else if (blocktime > KMP_MAX_BLOCKTIME)
7583 blocktime = KMP_MAX_BLOCKTIME;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007584
Jonathan Peyton30419822017-05-12 18:01:32 +00007585 set__blocktime_team(thread->th.th_team, tid, blocktime);
7586 set__blocktime_team(thread->th.th_serial_team, 0, blocktime);
Jim Cownie5e8470a2013-09-27 10:38:44 +00007587
Jonathan Peytone1c7c132016-10-07 18:12:19 +00007588#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +00007589 /* Calculate and set blocktime intervals for the teams */
7590 bt_intervals = KMP_INTERVALS_FROM_BLOCKTIME(blocktime, __kmp_monitor_wakeups);
Jim Cownie5e8470a2013-09-27 10:38:44 +00007591
Jonathan Peyton30419822017-05-12 18:01:32 +00007592 set__bt_intervals_team(thread->th.th_team, tid, bt_intervals);
7593 set__bt_intervals_team(thread->th.th_serial_team, 0, bt_intervals);
Jonathan Peytone1c7c132016-10-07 18:12:19 +00007594#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00007595
Jonathan Peyton30419822017-05-12 18:01:32 +00007596 /* Set whether blocktime has been set to "TRUE" */
7597 bt_set = TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007598
Jonathan Peyton30419822017-05-12 18:01:32 +00007599 set__bt_set_team(thread->th.th_team, tid, bt_set);
7600 set__bt_set_team(thread->th.th_serial_team, 0, bt_set);
Jonathan Peytone1c7c132016-10-07 18:12:19 +00007601#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +00007602 KF_TRACE(10, ("kmp_set_blocktime: T#%d(%d:%d), blocktime=%d, "
7603 "bt_intervals=%d, monitor_updates=%d\n",
7604 __kmp_gtid_from_tid(tid, thread->th.th_team),
7605 thread->th.th_team->t.t_id, tid, blocktime, bt_intervals,
7606 __kmp_monitor_wakeups));
Samuel Antao33515192016-10-20 13:20:17 +00007607#else
Jonathan Peyton30419822017-05-12 18:01:32 +00007608 KF_TRACE(10, ("kmp_set_blocktime: T#%d(%d:%d), blocktime=%d\n",
7609 __kmp_gtid_from_tid(tid, thread->th.th_team),
7610 thread->th.th_team->t.t_id, tid, blocktime));
Jonathan Peytone1c7c132016-10-07 18:12:19 +00007611#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00007612}
7613
Jonathan Peyton30419822017-05-12 18:01:32 +00007614void __kmp_aux_set_defaults(char const *str, int len) {
7615 if (!__kmp_init_serial) {
7616 __kmp_serial_initialize();
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00007617 }
Jonathan Peyton30419822017-05-12 18:01:32 +00007618 __kmp_env_initialize(str);
Jim Cownie5e8470a2013-09-27 10:38:44 +00007619
Jonathan Peyton30419822017-05-12 18:01:32 +00007620 if (__kmp_settings
Jim Cownie5e8470a2013-09-27 10:38:44 +00007621#if OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00007622 || __kmp_display_env || __kmp_display_env_verbose
Jim Cownie5e8470a2013-09-27 10:38:44 +00007623#endif // OMP_40_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +00007624 ) {
7625 __kmp_env_print();
7626 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00007627} // __kmp_aux_set_defaults
7628
7629/* ------------------------------------------------------------------------ */
Jonathan Peyton30419822017-05-12 18:01:32 +00007630/* internal fast reduction routines */
Jim Cownie5e8470a2013-09-27 10:38:44 +00007631
Jim Cownie5e8470a2013-09-27 10:38:44 +00007632PACKED_REDUCTION_METHOD_T
Jonathan Peyton30419822017-05-12 18:01:32 +00007633__kmp_determine_reduction_method(
7634 ident_t *loc, kmp_int32 global_tid, kmp_int32 num_vars, size_t reduce_size,
7635 void *reduce_data, void (*reduce_func)(void *lhs_data, void *rhs_data),
7636 kmp_critical_name *lck) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00007637
Jonathan Peyton30419822017-05-12 18:01:32 +00007638 // Default reduction method: critical construct ( lck != NULL, like in current
7639 // PAROPT )
7640 // If ( reduce_data!=NULL && reduce_func!=NULL ): the tree-reduction method
7641 // can be selected by RTL
7642 // If loc->flags contains KMP_IDENT_ATOMIC_REDUCE, the atomic reduce method
7643 // can be selected by RTL
7644 // Finally, it's up to OpenMP RTL to make a decision on which method to select
7645 // among generated by PAROPT.
Jim Cownie5e8470a2013-09-27 10:38:44 +00007646
Jonathan Peyton30419822017-05-12 18:01:32 +00007647 PACKED_REDUCTION_METHOD_T retval;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007648
Jonathan Peyton30419822017-05-12 18:01:32 +00007649 int team_size;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007650
Jonathan Peyton30419822017-05-12 18:01:32 +00007651 KMP_DEBUG_ASSERT(loc); // it would be nice to test ( loc != 0 )
7652 KMP_DEBUG_ASSERT(lck); // it would be nice to test ( lck != 0 )
Jim Cownie5e8470a2013-09-27 10:38:44 +00007653
Jonathan Peyton30419822017-05-12 18:01:32 +00007654#define FAST_REDUCTION_ATOMIC_METHOD_GENERATED \
7655 ((loc->flags & (KMP_IDENT_ATOMIC_REDUCE)) == (KMP_IDENT_ATOMIC_REDUCE))
7656#define FAST_REDUCTION_TREE_METHOD_GENERATED ((reduce_data) && (reduce_func))
Jim Cownie5e8470a2013-09-27 10:38:44 +00007657
Jonathan Peyton30419822017-05-12 18:01:32 +00007658 retval = critical_reduce_block;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007659
Jonathan Peyton30419822017-05-12 18:01:32 +00007660 // another choice of getting a team size (with 1 dynamic deference) is slower
7661 team_size = __kmp_get_team_num_threads(global_tid);
7662 if (team_size == 1) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00007663
Jonathan Peyton30419822017-05-12 18:01:32 +00007664 retval = empty_reduce_block;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007665
Jonathan Peyton30419822017-05-12 18:01:32 +00007666 } else {
Jim Cownie5e8470a2013-09-27 10:38:44 +00007667
Jonathan Peyton30419822017-05-12 18:01:32 +00007668 int atomic_available = FAST_REDUCTION_ATOMIC_METHOD_GENERATED;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007669
Jonathan Peyton30419822017-05-12 18:01:32 +00007670#if KMP_ARCH_X86_64 || KMP_ARCH_PPC64 || KMP_ARCH_AARCH64 || KMP_ARCH_MIPS64
Jim Cownie5e8470a2013-09-27 10:38:44 +00007671
Jonathan Peyton30419822017-05-12 18:01:32 +00007672#if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD || KMP_OS_WINDOWS || \
7673 KMP_OS_DARWIN
Jim Cownie5e8470a2013-09-27 10:38:44 +00007674
Jonathan Peyton30419822017-05-12 18:01:32 +00007675 int teamsize_cutoff = 4;
Jonathan Peyton91b78702015-06-08 19:39:07 +00007676
Jonathan Peyton492e0a32017-06-13 17:17:26 +00007677#if KMP_MIC_SUPPORTED
Jonathan Peyton30419822017-05-12 18:01:32 +00007678 if (__kmp_mic_type != non_mic) {
7679 teamsize_cutoff = 8;
7680 }
Andrey Churbanov613edeb2015-02-20 18:14:43 +00007681#endif
Jonathan Peytonbaad3f62018-08-09 22:04:30 +00007682 int tree_available = FAST_REDUCTION_TREE_METHOD_GENERATED;
Jonathan Peyton30419822017-05-12 18:01:32 +00007683 if (tree_available) {
7684 if (team_size <= teamsize_cutoff) {
7685 if (atomic_available) {
7686 retval = atomic_reduce_block;
Jim Cownie5e8470a2013-09-27 10:38:44 +00007687 }
Jonathan Peyton30419822017-05-12 18:01:32 +00007688 } else {
7689 retval = TREE_REDUCE_BLOCK_WITH_REDUCTION_BARRIER;
7690 }
7691 } else if (atomic_available) {
7692 retval = atomic_reduce_block;
7693 }
7694#else
7695#error "Unknown or unsupported OS"
7696#endif // KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD || KMP_OS_WINDOWS ||
7697// KMP_OS_DARWIN
Jim Cownie5e8470a2013-09-27 10:38:44 +00007698
Jonathan Peyton30419822017-05-12 18:01:32 +00007699#elif KMP_ARCH_X86 || KMP_ARCH_ARM || KMP_ARCH_AARCH || KMP_ARCH_MIPS
7700
7701#if KMP_OS_LINUX || KMP_OS_WINDOWS
7702
7703 // basic tuning
7704
7705 if (atomic_available) {
7706 if (num_vars <= 2) { // && ( team_size <= 8 ) due to false-sharing ???
7707 retval = atomic_reduce_block;
7708 }
7709 } // otherwise: use critical section
7710
7711#elif KMP_OS_DARWIN
7712
Jonathan Peytonbaad3f62018-08-09 22:04:30 +00007713 int tree_available = FAST_REDUCTION_TREE_METHOD_GENERATED;
Jonathan Peyton30419822017-05-12 18:01:32 +00007714 if (atomic_available && (num_vars <= 3)) {
7715 retval = atomic_reduce_block;
7716 } else if (tree_available) {
7717 if ((reduce_size > (9 * sizeof(kmp_real64))) &&
7718 (reduce_size < (2000 * sizeof(kmp_real64)))) {
7719 retval = TREE_REDUCE_BLOCK_WITH_PLAIN_BARRIER;
7720 }
7721 } // otherwise: use critical section
7722
7723#else
7724#error "Unknown or unsupported OS"
7725#endif
7726
7727#else
7728#error "Unknown or unsupported architecture"
7729#endif
7730 }
7731
7732 // KMP_FORCE_REDUCTION
7733
7734 // If the team is serialized (team_size == 1), ignore the forced reduction
7735 // method and stay with the unsynchronized method (empty_reduce_block)
7736 if (__kmp_force_reduction_method != reduction_method_not_defined &&
7737 team_size != 1) {
7738
7739 PACKED_REDUCTION_METHOD_T forced_retval = critical_reduce_block;
7740
7741 int atomic_available, tree_available;
7742
7743 switch ((forced_retval = __kmp_force_reduction_method)) {
7744 case critical_reduce_block:
7745 KMP_ASSERT(lck); // lck should be != 0
7746 break;
7747
7748 case atomic_reduce_block:
7749 atomic_available = FAST_REDUCTION_ATOMIC_METHOD_GENERATED;
7750 if (!atomic_available) {
7751 KMP_WARNING(RedMethodNotSupported, "atomic");
7752 forced_retval = critical_reduce_block;
7753 }
7754 break;
7755
7756 case tree_reduce_block:
7757 tree_available = FAST_REDUCTION_TREE_METHOD_GENERATED;
7758 if (!tree_available) {
7759 KMP_WARNING(RedMethodNotSupported, "tree");
7760 forced_retval = critical_reduce_block;
7761 } else {
7762#if KMP_FAST_REDUCTION_BARRIER
7763 forced_retval = TREE_REDUCE_BLOCK_WITH_REDUCTION_BARRIER;
7764#endif
7765 }
7766 break;
7767
7768 default:
7769 KMP_ASSERT(0); // "unsupported method specified"
Jim Cownie5e8470a2013-09-27 10:38:44 +00007770 }
7771
Jonathan Peyton30419822017-05-12 18:01:32 +00007772 retval = forced_retval;
7773 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00007774
Jonathan Peyton30419822017-05-12 18:01:32 +00007775 KA_TRACE(10, ("reduction method selected=%08x\n", retval));
Jim Cownie5e8470a2013-09-27 10:38:44 +00007776
Jonathan Peyton30419822017-05-12 18:01:32 +00007777#undef FAST_REDUCTION_TREE_METHOD_GENERATED
7778#undef FAST_REDUCTION_ATOMIC_METHOD_GENERATED
7779
7780 return (retval);
Jim Cownie5e8470a2013-09-27 10:38:44 +00007781}
7782
7783// this function is for testing set/get/determine reduce method
Jonathan Peyton30419822017-05-12 18:01:32 +00007784kmp_int32 __kmp_get_reduce_method(void) {
7785 return ((__kmp_entry_thread()->th.th_local.packed_reduction_method) >> 8);
Jim Cownie5e8470a2013-09-27 10:38:44 +00007786}