1 /*! \file */
2 /*
3  * kmp.h -- KPTS runtime header file.
4  */
5 
6 //===----------------------------------------------------------------------===//
7 //
8 //                     The LLVM Compiler Infrastructure
9 //
10 // This file is dual licensed under the MIT and the University of Illinois Open
11 // Source Licenses. See LICENSE.txt for details.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef KMP_H
16 #define KMP_H
17 
18 #include "kmp_config.h"
19 
20 /* #define BUILD_PARALLEL_ORDERED 1 */
21 
22 /* This fix replaces gettimeofday with clock_gettime for better scalability on
23    the Altix.  Requires user code to be linked with -lrt. */
24 //#define FIX_SGI_CLOCK
25 
26 /* Defines for OpenMP 3.0 tasking and auto scheduling */
27 
28 #ifndef KMP_STATIC_STEAL_ENABLED
29 #define KMP_STATIC_STEAL_ENABLED 1
30 #endif
31 
32 #define TASK_CURRENT_NOT_QUEUED 0
33 #define TASK_CURRENT_QUEUED 1
34 
35 #ifdef BUILD_TIED_TASK_STACK
36 #define TASK_STACK_EMPTY 0 // entries when the stack is empty
37 #define TASK_STACK_BLOCK_BITS 5 // Used in TASK_STACK_SIZE and TASK_STACK_MASK
38 // Number of entries in each task stack array
39 #define TASK_STACK_BLOCK_SIZE (1 << TASK_STACK_BLOCK_BITS)
40 // Mask for determining index into stack block
41 #define TASK_STACK_INDEX_MASK (TASK_STACK_BLOCK_SIZE - 1)
42 #endif // BUILD_TIED_TASK_STACK
43 
44 #define TASK_NOT_PUSHED 1
45 #define TASK_SUCCESSFULLY_PUSHED 0
46 #define TASK_TIED 1
47 #define TASK_UNTIED 0
48 #define TASK_EXPLICIT 1
49 #define TASK_IMPLICIT 0
50 #define TASK_PROXY 1
51 #define TASK_FULL 0
52 
53 #define KMP_CANCEL_THREADS
54 #define KMP_THREAD_ATTR
55 
56 // Android does not have pthread_cancel.  Undefine KMP_CANCEL_THREADS if being
57 // built on Android
58 #if defined(__ANDROID__)
59 #undef KMP_CANCEL_THREADS
60 #endif
61 
62 #include <signal.h>
63 #include <stdarg.h>
64 #include <stddef.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 /* include <ctype.h> don't use; problems with /MD on Windows* OS NT due to bad
69    Microsoft library. Some macros provided below to replace these functions  */
70 #ifndef __ABSOFT_WIN
71 #include <sys/types.h>
72 #endif
73 #include <limits.h>
74 #include <time.h>
75 
76 #include <errno.h>
77 
78 #include "kmp_os.h"
79 
80 #include "kmp_safe_c_api.h"
81 
82 #if KMP_STATS_ENABLED
83 class kmp_stats_list;
84 #endif
85 
86 #if KMP_USE_HWLOC && KMP_AFFINITY_SUPPORTED
87 #include "hwloc.h"
88 #ifndef HWLOC_OBJ_NUMANODE
89 #define HWLOC_OBJ_NUMANODE HWLOC_OBJ_NODE
90 #endif
91 #ifndef HWLOC_OBJ_PACKAGE
92 #define HWLOC_OBJ_PACKAGE HWLOC_OBJ_SOCKET
93 #endif
94 #endif
95 
96 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
97 #include <xmmintrin.h>
98 #endif
99 
100 #include "kmp_debug.h"
101 #include "kmp_lock.h"
102 #include "kmp_version.h"
103 #if USE_DEBUGGER
104 #include "kmp_debugger.h"
105 #endif
106 #include "kmp_i18n.h"
107 
108 #define KMP_HANDLE_SIGNALS (KMP_OS_UNIX || KMP_OS_WINDOWS)
109 
110 #include "kmp_wrapper_malloc.h"
111 #if KMP_OS_UNIX
112 #include <unistd.h>
113 #if !defined NSIG && defined _NSIG
114 #define NSIG _NSIG
115 #endif
116 #endif
117 
118 #if KMP_OS_LINUX
119 #pragma weak clock_gettime
120 #endif
121 
122 #if OMPT_SUPPORT
123 #include "ompt-internal.h"
124 #endif
125 
126 /*Select data placement in NUMA memory */
127 #define NO_FIRST_TOUCH 0
128 #define FIRST_TOUCH 1 /* Exploit SGI's first touch page placement algo */
129 
130 /* If not specified on compile command line, assume no first touch */
131 #ifndef BUILD_MEMORY
132 #define BUILD_MEMORY NO_FIRST_TOUCH
133 #endif
134 
135 // 0 - no fast memory allocation, alignment: 8-byte on x86, 16-byte on x64.
136 // 3 - fast allocation using sync, non-sync free lists of any size, non-self
137 // free lists of limited size.
138 #ifndef USE_FAST_MEMORY
139 #define USE_FAST_MEMORY 3
140 #endif
141 
142 #ifndef KMP_NESTED_HOT_TEAMS
143 #define KMP_NESTED_HOT_TEAMS 0
144 #define USE_NESTED_HOT_ARG(x)
145 #else
146 #if KMP_NESTED_HOT_TEAMS
147 #if OMP_40_ENABLED
148 #define USE_NESTED_HOT_ARG(x) , x
149 #else
150 // Nested hot teams feature depends on omp 4.0, disable it for earlier versions
151 #undef KMP_NESTED_HOT_TEAMS
152 #define KMP_NESTED_HOT_TEAMS 0
153 #define USE_NESTED_HOT_ARG(x)
154 #endif
155 #else
156 #define USE_NESTED_HOT_ARG(x)
157 #endif
158 #endif
159 
160 // Assume using BGET compare_exchange instruction instead of lock by default.
161 #ifndef USE_CMP_XCHG_FOR_BGET
162 #define USE_CMP_XCHG_FOR_BGET 1
163 #endif
164 
165 // Test to see if queuing lock is better than bootstrap lock for bget
166 // #ifndef USE_QUEUING_LOCK_FOR_BGET
167 // #define USE_QUEUING_LOCK_FOR_BGET
168 // #endif
169 
170 #define KMP_NSEC_PER_SEC 1000000000L
171 #define KMP_USEC_PER_SEC 1000000L
172 
173 /*!
174 @ingroup BASIC_TYPES
175 @{
176 */
177 
178 // FIXME DOXYGEN... need to group these flags somehow (Making them an anonymous
179 // enum would do it...)
180 /*!
181 Values for bit flags used in the ident_t to describe the fields.
182 */
183 /*! Use trampoline for internal microtasks */
184 #define KMP_IDENT_IMB 0x01
185 /*! Use c-style ident structure */
186 #define KMP_IDENT_KMPC 0x02
187 /* 0x04 is no longer used */
188 /*! Entry point generated by auto-parallelization */
189 #define KMP_IDENT_AUTOPAR 0x08
190 /*! Compiler generates atomic reduction option for kmpc_reduce* */
191 #define KMP_IDENT_ATOMIC_REDUCE 0x10
192 /*! To mark a 'barrier' directive in user code */
193 #define KMP_IDENT_BARRIER_EXPL 0x20
194 /*! To Mark implicit barriers. */
195 #define KMP_IDENT_BARRIER_IMPL 0x0040
196 #define KMP_IDENT_BARRIER_IMPL_MASK 0x01C0
197 #define KMP_IDENT_BARRIER_IMPL_FOR 0x0040
198 #define KMP_IDENT_BARRIER_IMPL_SECTIONS 0x00C0
199 
200 #define KMP_IDENT_BARRIER_IMPL_SINGLE 0x0140
201 #define KMP_IDENT_BARRIER_IMPL_WORKSHARE 0x01C0
202 
203 #define KMP_IDENT_WORK_LOOP 0x200 // static loop
204 #define KMP_IDENT_WORK_SECTIONS 0x400 // sections
205 #define KMP_IDENT_WORK_DISTRIBUTE 0x800 // distribute
206 
207 /*!
208  * The ident structure that describes a source location.
209  */
210 typedef struct ident {
211   kmp_int32 reserved_1; /**<  might be used in Fortran; see above  */
212   kmp_int32 flags; /**<  also f.flags; KMP_IDENT_xxx flags; KMP_IDENT_KMPC
213                       identifies this union member  */
214   kmp_int32 reserved_2; /**<  not really used in Fortran any more; see above */
215 #if USE_ITT_BUILD
216 /*  but currently used for storing region-specific ITT */
217 /*  contextual information. */
218 #endif /* USE_ITT_BUILD */
219   kmp_int32 reserved_3; /**< source[4] in Fortran, do not use for C++  */
220   char const *psource; /**< String describing the source location.
221                        The string is composed of semi-colon separated fields
222                        which describe the source file, the function and a pair
223                        of line numbers that delimit the construct. */
224 } ident_t;
225 /*!
226 @}
227 */
228 
229 // Some forward declarations.
230 typedef union kmp_team kmp_team_t;
231 typedef struct kmp_taskdata kmp_taskdata_t;
232 typedef union kmp_task_team kmp_task_team_t;
233 typedef union kmp_team kmp_team_p;
234 typedef union kmp_info kmp_info_p;
235 typedef union kmp_root kmp_root_p;
236 
237 #ifdef __cplusplus
238 extern "C" {
239 #endif
240 
241 /* ------------------------------------------------------------------------ */
242 
243 /* Pack two 32-bit signed integers into a 64-bit signed integer */
244 /* ToDo: Fix word ordering for big-endian machines. */
245 #define KMP_PACK_64(HIGH_32, LOW_32)                                           \
246   ((kmp_int64)((((kmp_uint64)(HIGH_32)) << 32) | (kmp_uint64)(LOW_32)))
247 
248 // Generic string manipulation macros. Assume that _x is of type char *
249 #define SKIP_WS(_x)                                                            \
250   {                                                                            \
251     while (*(_x) == ' ' || *(_x) == '\t')                                      \
252       (_x)++;                                                                  \
253   }
254 #define SKIP_DIGITS(_x)                                                        \
255   {                                                                            \
256     while (*(_x) >= '0' && *(_x) <= '9')                                       \
257       (_x)++;                                                                  \
258   }
259 #define SKIP_TO(_x, _c)                                                        \
260   {                                                                            \
261     while (*(_x) != '\0' && *(_x) != (_c))                                     \
262       (_x)++;                                                                  \
263   }
264 
265 /* ------------------------------------------------------------------------ */
266 
267 #define KMP_MAX(x, y) ((x) > (y) ? (x) : (y))
268 #define KMP_MIN(x, y) ((x) < (y) ? (x) : (y))
269 
270 /* ------------------------------------------------------------------------ */
271 /* Enumeration types */
272 
273 enum kmp_state_timer {
274   ts_stop,
275   ts_start,
276   ts_pause,
277 
278   ts_last_state
279 };
280 
281 enum dynamic_mode {
282   dynamic_default,
283 #ifdef USE_LOAD_BALANCE
284   dynamic_load_balance,
285 #endif /* USE_LOAD_BALANCE */
286   dynamic_random,
287   dynamic_thread_limit,
288   dynamic_max
289 };
290 
291 /* external schedule constants, duplicate enum omp_sched in omp.h in order to
292  * not include it here */
293 #ifndef KMP_SCHED_TYPE_DEFINED
294 #define KMP_SCHED_TYPE_DEFINED
295 typedef enum kmp_sched {
296   kmp_sched_lower = 0, // lower and upper bounds are for routine parameter check
297   // Note: need to adjust __kmp_sch_map global array in case enum is changed
298   kmp_sched_static = 1, // mapped to kmp_sch_static_chunked           (33)
299   kmp_sched_dynamic = 2, // mapped to kmp_sch_dynamic_chunked          (35)
300   kmp_sched_guided = 3, // mapped to kmp_sch_guided_chunked           (36)
301   kmp_sched_auto = 4, // mapped to kmp_sch_auto                     (38)
302   kmp_sched_upper_std = 5, // upper bound for standard schedules
303   kmp_sched_lower_ext = 100, // lower bound of Intel extension schedules
304   kmp_sched_trapezoidal = 101, // mapped to kmp_sch_trapezoidal (39)
305 #if KMP_STATIC_STEAL_ENABLED
306   kmp_sched_static_steal = 102, // mapped to kmp_sch_static_steal (44)
307 #endif
308   kmp_sched_upper,
309   kmp_sched_default = kmp_sched_static // default scheduling
310 } kmp_sched_t;
311 #endif
312 
313 /*!
314  @ingroup WORK_SHARING
315  * Describes the loop schedule to be used for a parallel for loop.
316  */
317 enum sched_type : kmp_int32 {
318   kmp_sch_lower = 32, /**< lower bound for unordered values */
319   kmp_sch_static_chunked = 33,
320   kmp_sch_static = 34, /**< static unspecialized */
321   kmp_sch_dynamic_chunked = 35,
322   kmp_sch_guided_chunked = 36, /**< guided unspecialized */
323   kmp_sch_runtime = 37,
324   kmp_sch_auto = 38, /**< auto */
325   kmp_sch_trapezoidal = 39,
326 
327   /* accessible only through KMP_SCHEDULE environment variable */
328   kmp_sch_static_greedy = 40,
329   kmp_sch_static_balanced = 41,
330   /* accessible only through KMP_SCHEDULE environment variable */
331   kmp_sch_guided_iterative_chunked = 42,
332   kmp_sch_guided_analytical_chunked = 43,
333   /* accessible only through KMP_SCHEDULE environment variable */
334   kmp_sch_static_steal = 44,
335 
336 #if OMP_45_ENABLED
337   /* static with chunk adjustment (e.g., simd) */
338   kmp_sch_static_balanced_chunked = 45,
339   kmp_sch_guided_simd = 46, /**< guided with chunk adjustment */
340   kmp_sch_runtime_simd = 47, /**< runtime with chunk adjustment */
341 #endif
342 
343   /* accessible only through KMP_SCHEDULE environment variable */
344   kmp_sch_upper, /**< upper bound for unordered values */
345 
346   kmp_ord_lower = 64, /**< lower bound for ordered values, must be power of 2 */
347   kmp_ord_static_chunked = 65,
348   kmp_ord_static = 66, /**< ordered static unspecialized */
349   kmp_ord_dynamic_chunked = 67,
350   kmp_ord_guided_chunked = 68,
351   kmp_ord_runtime = 69,
352   kmp_ord_auto = 70, /**< ordered auto */
353   kmp_ord_trapezoidal = 71,
354   kmp_ord_upper, /**< upper bound for ordered values */
355 
356 #if OMP_40_ENABLED
357   /* Schedules for Distribute construct */
358   kmp_distribute_static_chunked = 91, /**< distribute static chunked */
359   kmp_distribute_static = 92, /**< distribute static unspecialized */
360 #endif
361 
362   /* For the "nomerge" versions, kmp_dispatch_next*() will always return a
363      single iteration/chunk, even if the loop is serialized. For the schedule
364      types listed above, the entire iteration vector is returned if the loop is
365      serialized. This doesn't work for gcc/gcomp sections. */
366   kmp_nm_lower = 160, /**< lower bound for nomerge values */
367 
368   kmp_nm_static_chunked =
369       (kmp_sch_static_chunked - kmp_sch_lower + kmp_nm_lower),
370   kmp_nm_static = 162, /**< static unspecialized */
371   kmp_nm_dynamic_chunked = 163,
372   kmp_nm_guided_chunked = 164, /**< guided unspecialized */
373   kmp_nm_runtime = 165,
374   kmp_nm_auto = 166, /**< auto */
375   kmp_nm_trapezoidal = 167,
376 
377   /* accessible only through KMP_SCHEDULE environment variable */
378   kmp_nm_static_greedy = 168,
379   kmp_nm_static_balanced = 169,
380   /* accessible only through KMP_SCHEDULE environment variable */
381   kmp_nm_guided_iterative_chunked = 170,
382   kmp_nm_guided_analytical_chunked = 171,
383   kmp_nm_static_steal =
384       172, /* accessible only through OMP_SCHEDULE environment variable */
385 
386   kmp_nm_ord_static_chunked = 193,
387   kmp_nm_ord_static = 194, /**< ordered static unspecialized */
388   kmp_nm_ord_dynamic_chunked = 195,
389   kmp_nm_ord_guided_chunked = 196,
390   kmp_nm_ord_runtime = 197,
391   kmp_nm_ord_auto = 198, /**< auto */
392   kmp_nm_ord_trapezoidal = 199,
393   kmp_nm_upper, /**< upper bound for nomerge values */
394 
395 #if OMP_45_ENABLED
396   /* Support for OpenMP 4.5 monotonic and nonmonotonic schedule modifiers. Since
397      we need to distinguish the three possible cases (no modifier, monotonic
398      modifier, nonmonotonic modifier), we need separate bits for each modifier.
399      The absence of monotonic does not imply nonmonotonic, especially since 4.5
400      says that the behaviour of the "no modifier" case is implementation defined
401      in 4.5, but will become "nonmonotonic" in 5.0.
402 
403      Since we're passing a full 32 bit value, we can use a couple of high bits
404      for these flags; out of paranoia we avoid the sign bit.
405 
406      These modifiers can be or-ed into non-static schedules by the compiler to
407      pass the additional information. They will be stripped early in the
408      processing in __kmp_dispatch_init when setting up schedules, so most of the
409      code won't ever see schedules with these bits set.  */
410   kmp_sch_modifier_monotonic =
411       (1 << 29), /**< Set if the monotonic schedule modifier was present */
412   kmp_sch_modifier_nonmonotonic =
413       (1 << 30), /**< Set if the nonmonotonic schedule modifier was present */
414 
415 #define SCHEDULE_WITHOUT_MODIFIERS(s)                                          \
416   (enum sched_type)(                                                           \
417       (s) & ~(kmp_sch_modifier_nonmonotonic | kmp_sch_modifier_monotonic))
418 #define SCHEDULE_HAS_MONOTONIC(s) (((s)&kmp_sch_modifier_monotonic) != 0)
419 #define SCHEDULE_HAS_NONMONOTONIC(s) (((s)&kmp_sch_modifier_nonmonotonic) != 0)
420 #define SCHEDULE_HAS_NO_MODIFIERS(s)                                           \
421   (((s) & (kmp_sch_modifier_nonmonotonic | kmp_sch_modifier_monotonic)) == 0)
422 #else
423 /* By doing this we hope to avoid multiple tests on OMP_45_ENABLED. Compilers
424    can now eliminate tests on compile time constants and dead code that results
425    from them, so we can leave code guarded by such an if in place.  */
426 #define SCHEDULE_WITHOUT_MODIFIERS(s) (s)
427 #define SCHEDULE_HAS_MONOTONIC(s) false
428 #define SCHEDULE_HAS_NONMONOTONIC(s) false
429 #define SCHEDULE_HAS_NO_MODIFIERS(s) true
430 #endif
431 
432   kmp_sch_default = kmp_sch_static /**< default scheduling algorithm */
433 };
434 
435 /* Type to keep runtime schedule set via OMP_SCHEDULE or omp_set_schedule() */
436 typedef union kmp_r_sched {
437   struct {
438     enum sched_type r_sched_type;
439     int chunk;
440   };
441   kmp_int64 sched;
442 } kmp_r_sched_t;
443 
444 extern enum sched_type __kmp_sch_map[]; // map OMP 3.0 schedule types with our
445 // internal schedule types
446 
447 enum library_type {
448   library_none,
449   library_serial,
450   library_turnaround,
451   library_throughput
452 };
453 
454 #if KMP_OS_LINUX
455 enum clock_function_type {
456   clock_function_gettimeofday,
457   clock_function_clock_gettime
458 };
459 #endif /* KMP_OS_LINUX */
460 
461 #if KMP_MIC_SUPPORTED
462 enum mic_type { non_mic, mic1, mic2, mic3, dummy };
463 #endif
464 
465 /* -- fast reduction stuff ------------------------------------------------ */
466 
467 #undef KMP_FAST_REDUCTION_BARRIER
468 #define KMP_FAST_REDUCTION_BARRIER 1
469 
470 #undef KMP_FAST_REDUCTION_CORE_DUO
471 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
472 #define KMP_FAST_REDUCTION_CORE_DUO 1
473 #endif
474 
475 enum _reduction_method {
476   reduction_method_not_defined = 0,
477   critical_reduce_block = (1 << 8),
478   atomic_reduce_block = (2 << 8),
479   tree_reduce_block = (3 << 8),
480   empty_reduce_block = (4 << 8)
481 };
482 
483 // Description of the packed_reduction_method variable:
484 // The packed_reduction_method variable consists of two enum types variables
485 // that are packed together into 0-th byte and 1-st byte:
486 // 0: (packed_reduction_method & 0x000000FF) is a 'enum barrier_type' value of
487 // barrier that will be used in fast reduction: bs_plain_barrier or
488 // bs_reduction_barrier
489 // 1: (packed_reduction_method & 0x0000FF00) is a reduction method that will
490 // be used in fast reduction;
491 // Reduction method is of 'enum _reduction_method' type and it's defined the way
492 // so that the bits of 0-th byte are empty, so no need to execute a shift
493 // instruction while packing/unpacking
494 
495 #if KMP_FAST_REDUCTION_BARRIER
496 #define PACK_REDUCTION_METHOD_AND_BARRIER(reduction_method, barrier_type)      \
497   ((reduction_method) | (barrier_type))
498 
499 #define UNPACK_REDUCTION_METHOD(packed_reduction_method)                       \
500   ((enum _reduction_method)((packed_reduction_method) & (0x0000FF00)))
501 
502 #define UNPACK_REDUCTION_BARRIER(packed_reduction_method)                      \
503   ((enum barrier_type)((packed_reduction_method) & (0x000000FF)))
504 #else
505 #define PACK_REDUCTION_METHOD_AND_BARRIER(reduction_method, barrier_type)      \
506   (reduction_method)
507 
508 #define UNPACK_REDUCTION_METHOD(packed_reduction_method)                       \
509   (packed_reduction_method)
510 
511 #define UNPACK_REDUCTION_BARRIER(packed_reduction_method) (bs_plain_barrier)
512 #endif
513 
514 #define TEST_REDUCTION_METHOD(packed_reduction_method, which_reduction_block)  \
515   ((UNPACK_REDUCTION_METHOD(packed_reduction_method)) ==                       \
516    (which_reduction_block))
517 
518 #if KMP_FAST_REDUCTION_BARRIER
519 #define TREE_REDUCE_BLOCK_WITH_REDUCTION_BARRIER                               \
520   (PACK_REDUCTION_METHOD_AND_BARRIER(tree_reduce_block, bs_reduction_barrier))
521 
522 #define TREE_REDUCE_BLOCK_WITH_PLAIN_BARRIER                                   \
523   (PACK_REDUCTION_METHOD_AND_BARRIER(tree_reduce_block, bs_plain_barrier))
524 #endif
525 
526 typedef int PACKED_REDUCTION_METHOD_T;
527 
528 /* -- end of fast reduction stuff ----------------------------------------- */
529 
530 #if KMP_OS_WINDOWS
531 #define USE_CBLKDATA
532 #pragma warning(push)
533 #pragma warning(disable : 271 310)
534 #include <windows.h>
535 #pragma warning(pop)
536 #endif
537 
538 #if KMP_OS_UNIX
539 #include <dlfcn.h>
540 #include <pthread.h>
541 #endif
542 
543 /* Only Linux* OS and Windows* OS support thread affinity. */
544 #if KMP_AFFINITY_SUPPORTED
545 
546 // GROUP_AFFINITY is already defined for _MSC_VER>=1600 (VS2010 and later).
547 #if KMP_OS_WINDOWS
548 #if _MSC_VER < 1600
549 typedef struct GROUP_AFFINITY {
550   KAFFINITY Mask;
551   WORD Group;
552   WORD Reserved[3];
553 } GROUP_AFFINITY;
554 #endif /* _MSC_VER < 1600 */
555 #if KMP_GROUP_AFFINITY
556 extern int __kmp_num_proc_groups;
557 #else
558 static const int __kmp_num_proc_groups = 1;
559 #endif /* KMP_GROUP_AFFINITY */
560 typedef DWORD (*kmp_GetActiveProcessorCount_t)(WORD);
561 extern kmp_GetActiveProcessorCount_t __kmp_GetActiveProcessorCount;
562 
563 typedef WORD (*kmp_GetActiveProcessorGroupCount_t)(void);
564 extern kmp_GetActiveProcessorGroupCount_t __kmp_GetActiveProcessorGroupCount;
565 
566 typedef BOOL (*kmp_GetThreadGroupAffinity_t)(HANDLE, GROUP_AFFINITY *);
567 extern kmp_GetThreadGroupAffinity_t __kmp_GetThreadGroupAffinity;
568 
569 typedef BOOL (*kmp_SetThreadGroupAffinity_t)(HANDLE, const GROUP_AFFINITY *,
570                                              GROUP_AFFINITY *);
571 extern kmp_SetThreadGroupAffinity_t __kmp_SetThreadGroupAffinity;
572 #endif /* KMP_OS_WINDOWS */
573 
574 #if KMP_USE_HWLOC
575 extern hwloc_topology_t __kmp_hwloc_topology;
576 extern int __kmp_hwloc_error;
577 extern int __kmp_numa_detected;
578 extern int __kmp_tile_depth;
579 #endif
580 
581 extern size_t __kmp_affin_mask_size;
582 #define KMP_AFFINITY_CAPABLE() (__kmp_affin_mask_size > 0)
583 #define KMP_AFFINITY_DISABLE() (__kmp_affin_mask_size = 0)
584 #define KMP_AFFINITY_ENABLE(mask_size) (__kmp_affin_mask_size = mask_size)
585 #define KMP_CPU_SET_ITERATE(i, mask)                                           \
586   for (i = (mask)->begin(); i != (mask)->end(); i = (mask)->next(i))
587 #define KMP_CPU_SET(i, mask) (mask)->set(i)
588 #define KMP_CPU_ISSET(i, mask) (mask)->is_set(i)
589 #define KMP_CPU_CLR(i, mask) (mask)->clear(i)
590 #define KMP_CPU_ZERO(mask) (mask)->zero()
591 #define KMP_CPU_COPY(dest, src) (dest)->copy(src)
592 #define KMP_CPU_AND(dest, src) (dest)->bitwise_and(src)
593 #define KMP_CPU_COMPLEMENT(max_bit_number, mask) (mask)->bitwise_not()
594 #define KMP_CPU_UNION(dest, src) (dest)->bitwise_or(src)
595 #define KMP_CPU_ALLOC(ptr) (ptr = __kmp_affinity_dispatch->allocate_mask())
596 #define KMP_CPU_FREE(ptr) __kmp_affinity_dispatch->deallocate_mask(ptr)
597 #define KMP_CPU_ALLOC_ON_STACK(ptr) KMP_CPU_ALLOC(ptr)
598 #define KMP_CPU_FREE_FROM_STACK(ptr) KMP_CPU_FREE(ptr)
599 #define KMP_CPU_INTERNAL_ALLOC(ptr) KMP_CPU_ALLOC(ptr)
600 #define KMP_CPU_INTERNAL_FREE(ptr) KMP_CPU_FREE(ptr)
601 #define KMP_CPU_INDEX(arr, i) __kmp_affinity_dispatch->index_mask_array(arr, i)
602 #define KMP_CPU_ALLOC_ARRAY(arr, n)                                            \
603   (arr = __kmp_affinity_dispatch->allocate_mask_array(n))
604 #define KMP_CPU_FREE_ARRAY(arr, n)                                             \
605   __kmp_affinity_dispatch->deallocate_mask_array(arr)
606 #define KMP_CPU_INTERNAL_ALLOC_ARRAY(arr, n) KMP_CPU_ALLOC_ARRAY(arr, n)
607 #define KMP_CPU_INTERNAL_FREE_ARRAY(arr, n) KMP_CPU_FREE_ARRAY(arr, n)
608 #define __kmp_get_system_affinity(mask, abort_bool)                            \
609   (mask)->get_system_affinity(abort_bool)
610 #define __kmp_set_system_affinity(mask, abort_bool)                            \
611   (mask)->set_system_affinity(abort_bool)
612 #define __kmp_get_proc_group(mask) (mask)->get_proc_group()
613 
614 class KMPAffinity {
615 public:
616   class Mask {
617   public:
618     void *operator new(size_t n);
619     void operator delete(void *p);
620     void *operator new[](size_t n);
621     void operator delete[](void *p);
622     virtual ~Mask() {}
623     // Set bit i to 1
624     virtual void set(int i) {}
625     // Return bit i
626     virtual bool is_set(int i) const { return false; }
627     // Set bit i to 0
628     virtual void clear(int i) {}
629     // Zero out entire mask
630     virtual void zero() {}
631     // Copy src into this mask
632     virtual void copy(const Mask *src) {}
633     // this &= rhs
634     virtual void bitwise_and(const Mask *rhs) {}
635     // this |= rhs
636     virtual void bitwise_or(const Mask *rhs) {}
637     // this = ~this
638     virtual void bitwise_not() {}
639     // API for iterating over an affinity mask
640     // for (int i = mask->begin(); i != mask->end(); i = mask->next(i))
641     virtual int begin() const { return 0; }
642     virtual int end() const { return 0; }
643     virtual int next(int previous) const { return 0; }
644     // Set the system's affinity to this affinity mask's value
645     virtual int set_system_affinity(bool abort_on_error) const { return -1; }
646     // Set this affinity mask to the current system affinity
647     virtual int get_system_affinity(bool abort_on_error) { return -1; }
648     // Only 1 DWORD in the mask should have any procs set.
649     // Return the appropriate index, or -1 for an invalid mask.
650     virtual int get_proc_group() const { return -1; }
651   };
652   void *operator new(size_t n);
653   void operator delete(void *p);
654   // Need virtual destructor
655   virtual ~KMPAffinity() = default;
656   // Determine if affinity is capable
657   virtual void determine_capable(const char *env_var) {}
658   // Bind the current thread to os proc
659   virtual void bind_thread(int proc) {}
660   // Factory functions to allocate/deallocate a mask
661   virtual Mask *allocate_mask() { return nullptr; }
662   virtual void deallocate_mask(Mask *m) {}
663   virtual Mask *allocate_mask_array(int num) { return nullptr; }
664   virtual void deallocate_mask_array(Mask *m) {}
665   virtual Mask *index_mask_array(Mask *m, int index) { return nullptr; }
666   static void pick_api();
667   static void destroy_api();
668   enum api_type {
669     NATIVE_OS
670 #if KMP_USE_HWLOC
671     ,
672     HWLOC
673 #endif
674   };
675   virtual api_type get_api_type() const {
676     KMP_ASSERT(0);
677     return NATIVE_OS;
678   }
679 
680 private:
681   static bool picked_api;
682 };
683 
684 typedef KMPAffinity::Mask kmp_affin_mask_t;
685 extern KMPAffinity *__kmp_affinity_dispatch;
686 
687 // Declare local char buffers with this size for printing debug and info
688 // messages, using __kmp_affinity_print_mask().
689 #define KMP_AFFIN_MASK_PRINT_LEN 1024
690 
691 enum affinity_type {
692   affinity_none = 0,
693   affinity_physical,
694   affinity_logical,
695   affinity_compact,
696   affinity_scatter,
697   affinity_explicit,
698   affinity_balanced,
699   affinity_disabled, // not used outsize the env var parser
700   affinity_default
701 };
702 
703 enum affinity_gran {
704   affinity_gran_fine = 0,
705   affinity_gran_thread,
706   affinity_gran_core,
707   affinity_gran_tile,
708   affinity_gran_numa,
709   affinity_gran_package,
710   affinity_gran_node,
711 #if KMP_GROUP_AFFINITY
712   // The "group" granularity isn't necesssarily coarser than all of the
713   // other levels, but we put it last in the enum.
714   affinity_gran_group,
715 #endif /* KMP_GROUP_AFFINITY */
716   affinity_gran_default
717 };
718 
719 enum affinity_top_method {
720   affinity_top_method_all = 0, // try all (supported) methods, in order
721 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
722   affinity_top_method_apicid,
723   affinity_top_method_x2apicid,
724 #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
725   affinity_top_method_cpuinfo, // KMP_CPUINFO_FILE is usable on Windows* OS, too
726 #if KMP_GROUP_AFFINITY
727   affinity_top_method_group,
728 #endif /* KMP_GROUP_AFFINITY */
729   affinity_top_method_flat,
730 #if KMP_USE_HWLOC
731   affinity_top_method_hwloc,
732 #endif
733   affinity_top_method_default
734 };
735 
736 #define affinity_respect_mask_default (-1)
737 
738 extern enum affinity_type __kmp_affinity_type; /* Affinity type */
739 extern enum affinity_gran __kmp_affinity_gran; /* Affinity granularity */
740 extern int __kmp_affinity_gran_levels; /* corresponding int value */
741 extern int __kmp_affinity_dups; /* Affinity duplicate masks */
742 extern enum affinity_top_method __kmp_affinity_top_method;
743 extern int __kmp_affinity_compact; /* Affinity 'compact' value */
744 extern int __kmp_affinity_offset; /* Affinity offset value  */
745 extern int __kmp_affinity_verbose; /* Was verbose specified for KMP_AFFINITY? */
746 extern int __kmp_affinity_warnings; /* KMP_AFFINITY warnings enabled ? */
747 extern int __kmp_affinity_respect_mask; // Respect process' init affinity mask?
748 extern char *__kmp_affinity_proclist; /* proc ID list */
749 extern kmp_affin_mask_t *__kmp_affinity_masks;
750 extern unsigned __kmp_affinity_num_masks;
751 extern void __kmp_affinity_bind_thread(int which);
752 
753 extern kmp_affin_mask_t *__kmp_affin_fullMask;
754 extern char *__kmp_cpuinfo_file;
755 
756 #endif /* KMP_AFFINITY_SUPPORTED */
757 
758 #if OMP_40_ENABLED
759 
760 // This needs to be kept in sync with the values in omp.h !!!
761 typedef enum kmp_proc_bind_t {
762   proc_bind_false = 0,
763   proc_bind_true,
764   proc_bind_master,
765   proc_bind_close,
766   proc_bind_spread,
767   proc_bind_intel, // use KMP_AFFINITY interface
768   proc_bind_default
769 } kmp_proc_bind_t;
770 
771 typedef struct kmp_nested_proc_bind_t {
772   kmp_proc_bind_t *bind_types;
773   int size;
774   int used;
775 } kmp_nested_proc_bind_t;
776 
777 extern kmp_nested_proc_bind_t __kmp_nested_proc_bind;
778 
779 #endif /* OMP_40_ENABLED */
780 
781 #if KMP_AFFINITY_SUPPORTED
782 #define KMP_PLACE_ALL (-1)
783 #define KMP_PLACE_UNDEFINED (-2)
784 #endif /* KMP_AFFINITY_SUPPORTED */
785 
786 extern int __kmp_affinity_num_places;
787 
788 #if OMP_40_ENABLED
789 typedef enum kmp_cancel_kind_t {
790   cancel_noreq = 0,
791   cancel_parallel = 1,
792   cancel_loop = 2,
793   cancel_sections = 3,
794   cancel_taskgroup = 4
795 } kmp_cancel_kind_t;
796 #endif // OMP_40_ENABLED
797 
798 // KMP_HW_SUBSET support:
799 typedef struct kmp_hws_item {
800   int num;
801   int offset;
802 } kmp_hws_item_t;
803 
804 extern kmp_hws_item_t __kmp_hws_socket;
805 extern kmp_hws_item_t __kmp_hws_node;
806 extern kmp_hws_item_t __kmp_hws_tile;
807 extern kmp_hws_item_t __kmp_hws_core;
808 extern kmp_hws_item_t __kmp_hws_proc;
809 extern int __kmp_hws_requested;
810 extern int __kmp_hws_abs_flag; // absolute or per-item number requested
811 
812 /* ------------------------------------------------------------------------ */
813 
814 #define KMP_PAD(type, sz)                                                      \
815   (sizeof(type) + (sz - ((sizeof(type) - 1) % (sz)) - 1))
816 
817 // We need to avoid using -1 as a GTID as +1 is added to the gtid
818 // when storing it in a lock, and the value 0 is reserved.
819 #define KMP_GTID_DNE (-2) /* Does not exist */
820 #define KMP_GTID_SHUTDOWN (-3) /* Library is shutting down */
821 #define KMP_GTID_MONITOR (-4) /* Monitor thread ID */
822 #define KMP_GTID_UNKNOWN (-5) /* Is not known */
823 #define KMP_GTID_MIN (-6) /* Minimal gtid for low bound check in DEBUG */
824 
825 #define __kmp_get_gtid() __kmp_get_global_thread_id()
826 #define __kmp_entry_gtid() __kmp_get_global_thread_id_reg()
827 
828 #define __kmp_tid_from_gtid(gtid)                                              \
829   (KMP_DEBUG_ASSERT((gtid) >= 0), __kmp_threads[(gtid)]->th.th_info.ds.ds_tid)
830 
831 #define __kmp_get_tid() (__kmp_tid_from_gtid(__kmp_get_gtid()))
832 #define __kmp_gtid_from_tid(tid, team)                                         \
833   (KMP_DEBUG_ASSERT((tid) >= 0 && (team) != NULL),                             \
834    team->t.t_threads[(tid)]->th.th_info.ds.ds_gtid)
835 
836 #define __kmp_get_team() (__kmp_threads[(__kmp_get_gtid())]->th.th_team)
837 #define __kmp_team_from_gtid(gtid)                                             \
838   (KMP_DEBUG_ASSERT((gtid) >= 0), __kmp_threads[(gtid)]->th.th_team)
839 
840 #define __kmp_thread_from_gtid(gtid)                                           \
841   (KMP_DEBUG_ASSERT((gtid) >= 0), __kmp_threads[(gtid)])
842 #define __kmp_get_thread() (__kmp_thread_from_gtid(__kmp_get_gtid()))
843 
844 // Returns current thread (pointer to kmp_info_t). In contrast to
845 // __kmp_get_thread(), it works with registered and not-yet-registered threads.
846 #define __kmp_gtid_from_thread(thr)                                            \
847   (KMP_DEBUG_ASSERT((thr) != NULL), (thr)->th.th_info.ds.ds_gtid)
848 
849 // AT: Which way is correct?
850 // AT: 1. nproc = __kmp_threads[ ( gtid ) ] -> th.th_team -> t.t_nproc;
851 // AT: 2. nproc = __kmp_threads[ ( gtid ) ] -> th.th_team_nproc;
852 #define __kmp_get_team_num_threads(gtid)                                       \
853   (__kmp_threads[(gtid)]->th.th_team->t.t_nproc)
854 
855 /* ------------------------------------------------------------------------ */
856 
857 #define KMP_UINT64_MAX                                                         \
858   (~((kmp_uint64)1 << ((sizeof(kmp_uint64) * (1 << 3)) - 1)))
859 
860 #define KMP_MIN_NTH 1
861 
862 #ifndef KMP_MAX_NTH
863 #if defined(PTHREAD_THREADS_MAX) && PTHREAD_THREADS_MAX < INT_MAX
864 #define KMP_MAX_NTH PTHREAD_THREADS_MAX
865 #else
866 #define KMP_MAX_NTH INT_MAX
867 #endif
868 #endif /* KMP_MAX_NTH */
869 
870 #ifdef PTHREAD_STACK_MIN
871 #define KMP_MIN_STKSIZE PTHREAD_STACK_MIN
872 #else
873 #define KMP_MIN_STKSIZE ((size_t)(32 * 1024))
874 #endif
875 
876 #define KMP_MAX_STKSIZE (~((size_t)1 << ((sizeof(size_t) * (1 << 3)) - 1)))
877 
878 #if KMP_ARCH_X86
879 #define KMP_DEFAULT_STKSIZE ((size_t)(2 * 1024 * 1024))
880 #elif KMP_ARCH_X86_64
881 #define KMP_DEFAULT_STKSIZE ((size_t)(4 * 1024 * 1024))
882 #define KMP_BACKUP_STKSIZE ((size_t)(2 * 1024 * 1024))
883 #else
884 #define KMP_DEFAULT_STKSIZE ((size_t)(1024 * 1024))
885 #endif
886 
887 #define KMP_DEFAULT_MALLOC_POOL_INCR ((size_t)(1024 * 1024))
888 #define KMP_MIN_MALLOC_POOL_INCR ((size_t)(4 * 1024))
889 #define KMP_MAX_MALLOC_POOL_INCR                                               \
890   (~((size_t)1 << ((sizeof(size_t) * (1 << 3)) - 1)))
891 
892 #define KMP_MIN_STKOFFSET (0)
893 #define KMP_MAX_STKOFFSET KMP_MAX_STKSIZE
894 #if KMP_OS_DARWIN
895 #define KMP_DEFAULT_STKOFFSET KMP_MIN_STKOFFSET
896 #else
897 #define KMP_DEFAULT_STKOFFSET CACHE_LINE
898 #endif
899 
900 #define KMP_MIN_STKPADDING (0)
901 #define KMP_MAX_STKPADDING (2 * 1024 * 1024)
902 
903 #define KMP_BLOCKTIME_MULTIPLIER                                               \
904   (1000) /* number of blocktime units per second */
905 #define KMP_MIN_BLOCKTIME (0)
906 #define KMP_MAX_BLOCKTIME                                                      \
907   (INT_MAX) /* Must be this for "infinite" setting the work */
908 #define KMP_DEFAULT_BLOCKTIME (200) /*  __kmp_blocktime is in milliseconds  */
909 
910 #if KMP_USE_MONITOR
911 #define KMP_DEFAULT_MONITOR_STKSIZE ((size_t)(64 * 1024))
912 #define KMP_MIN_MONITOR_WAKEUPS (1) // min times monitor wakes up per second
913 #define KMP_MAX_MONITOR_WAKEUPS (1000) // max times monitor can wake up per sec
914 
915 /* Calculate new number of monitor wakeups for a specific block time based on
916    previous monitor_wakeups. Only allow increasing number of wakeups */
917 #define KMP_WAKEUPS_FROM_BLOCKTIME(blocktime, monitor_wakeups)                 \
918   (((blocktime) == KMP_MAX_BLOCKTIME)                                          \
919        ? (monitor_wakeups)                                                     \
920        : ((blocktime) == KMP_MIN_BLOCKTIME)                                    \
921              ? KMP_MAX_MONITOR_WAKEUPS                                         \
922              : ((monitor_wakeups) > (KMP_BLOCKTIME_MULTIPLIER / (blocktime)))  \
923                    ? (monitor_wakeups)                                         \
924                    : (KMP_BLOCKTIME_MULTIPLIER) / (blocktime))
925 
926 /* Calculate number of intervals for a specific block time based on
927    monitor_wakeups */
928 #define KMP_INTERVALS_FROM_BLOCKTIME(blocktime, monitor_wakeups)               \
929   (((blocktime) + (KMP_BLOCKTIME_MULTIPLIER / (monitor_wakeups)) - 1) /        \
930    (KMP_BLOCKTIME_MULTIPLIER / (monitor_wakeups)))
931 #else
932 #define KMP_BLOCKTIME(team, tid)                                               \
933   (get__bt_set(team, tid) ? get__blocktime(team, tid) : __kmp_dflt_blocktime)
934 #if KMP_OS_UNIX && (KMP_ARCH_X86 || KMP_ARCH_X86_64)
935 // HW TSC is used to reduce overhead (clock tick instead of nanosecond).
936 extern kmp_uint64 __kmp_ticks_per_msec;
937 #if KMP_COMPILER_ICC
938 #define KMP_NOW() _rdtsc()
939 #else
940 #define KMP_NOW() __kmp_hardware_timestamp()
941 #endif
942 #define KMP_NOW_MSEC() (KMP_NOW() / __kmp_ticks_per_msec)
943 #define KMP_BLOCKTIME_INTERVAL(team, tid)                                      \
944   (KMP_BLOCKTIME(team, tid) * __kmp_ticks_per_msec)
945 #define KMP_BLOCKING(goal, count) ((goal) > KMP_NOW())
946 #else
947 // System time is retrieved sporadically while blocking.
948 extern kmp_uint64 __kmp_now_nsec();
949 #define KMP_NOW() __kmp_now_nsec()
950 #define KMP_NOW_MSEC() (KMP_NOW() / KMP_USEC_PER_SEC)
951 #define KMP_BLOCKTIME_INTERVAL(team, tid)                                      \
952   (KMP_BLOCKTIME(team, tid) * KMP_USEC_PER_SEC)
953 #define KMP_BLOCKING(goal, count) ((count) % 1000 != 0 || (goal) > KMP_NOW())
954 #endif
955 #define KMP_YIELD_NOW()                                                        \
956   (KMP_NOW_MSEC() / KMP_MAX(__kmp_dflt_blocktime, 1) %                         \
957        (__kmp_yield_on_count + __kmp_yield_off_count) <                        \
958    (kmp_uint32)__kmp_yield_on_count)
959 #endif // KMP_USE_MONITOR
960 
961 #define KMP_MIN_STATSCOLS 40
962 #define KMP_MAX_STATSCOLS 4096
963 #define KMP_DEFAULT_STATSCOLS 80
964 
965 #define KMP_MIN_INTERVAL 0
966 #define KMP_MAX_INTERVAL (INT_MAX - 1)
967 #define KMP_DEFAULT_INTERVAL 0
968 
969 #define KMP_MIN_CHUNK 1
970 #define KMP_MAX_CHUNK (INT_MAX - 1)
971 #define KMP_DEFAULT_CHUNK 1
972 
973 #define KMP_MIN_INIT_WAIT 1
974 #define KMP_MAX_INIT_WAIT (INT_MAX / 2)
975 #define KMP_DEFAULT_INIT_WAIT 2048U
976 
977 #define KMP_MIN_NEXT_WAIT 1
978 #define KMP_MAX_NEXT_WAIT (INT_MAX / 2)
979 #define KMP_DEFAULT_NEXT_WAIT 1024U
980 
981 #define KMP_DFLT_DISP_NUM_BUFF 7
982 #define KMP_MAX_ORDERED 8
983 
984 #define KMP_MAX_FIELDS 32
985 
986 #define KMP_MAX_BRANCH_BITS 31
987 
988 #define KMP_MAX_ACTIVE_LEVELS_LIMIT INT_MAX
989 
990 #define KMP_MAX_DEFAULT_DEVICE_LIMIT INT_MAX
991 
992 #define KMP_MAX_TASK_PRIORITY_LIMIT INT_MAX
993 
994 /* Minimum number of threads before switch to TLS gtid (experimentally
995    determined) */
996 /* josh TODO: what about OS X* tuning? */
997 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
998 #define KMP_TLS_GTID_MIN 5
999 #else
1000 #define KMP_TLS_GTID_MIN INT_MAX
1001 #endif
1002 
1003 #define KMP_MASTER_TID(tid) ((tid) == 0)
1004 #define KMP_WORKER_TID(tid) ((tid) != 0)
1005 
1006 #define KMP_MASTER_GTID(gtid) (__kmp_tid_from_gtid((gtid)) == 0)
1007 #define KMP_WORKER_GTID(gtid) (__kmp_tid_from_gtid((gtid)) != 0)
1008 #define KMP_UBER_GTID(gtid)                                                    \
1009   (KMP_DEBUG_ASSERT((gtid) >= KMP_GTID_MIN),                                   \
1010    KMP_DEBUG_ASSERT((gtid) < __kmp_threads_capacity),                          \
1011    (gtid) >= 0 && __kmp_root[(gtid)] && __kmp_threads[(gtid)] &&               \
1012        (__kmp_threads[(gtid)] == __kmp_root[(gtid)]->r.r_uber_thread))
1013 #define KMP_INITIAL_GTID(gtid) ((gtid) == 0)
1014 
1015 #ifndef TRUE
1016 #define FALSE 0
1017 #define TRUE (!FALSE)
1018 #endif
1019 
1020 /* NOTE: all of the following constants must be even */
1021 
1022 #if KMP_OS_WINDOWS
1023 #define KMP_INIT_WAIT 64U /* initial number of spin-tests   */
1024 #define KMP_NEXT_WAIT 32U /* susequent number of spin-tests */
1025 #elif KMP_OS_CNK
1026 #define KMP_INIT_WAIT 16U /* initial number of spin-tests   */
1027 #define KMP_NEXT_WAIT 8U /* susequent number of spin-tests */
1028 #elif KMP_OS_LINUX
1029 #define KMP_INIT_WAIT 1024U /* initial number of spin-tests   */
1030 #define KMP_NEXT_WAIT 512U /* susequent number of spin-tests */
1031 #elif KMP_OS_DARWIN
1032 /* TODO: tune for KMP_OS_DARWIN */
1033 #define KMP_INIT_WAIT 1024U /* initial number of spin-tests   */
1034 #define KMP_NEXT_WAIT 512U /* susequent number of spin-tests */
1035 #elif KMP_OS_FREEBSD
1036 /* TODO: tune for KMP_OS_FREEBSD */
1037 #define KMP_INIT_WAIT 1024U /* initial number of spin-tests   */
1038 #define KMP_NEXT_WAIT 512U /* susequent number of spin-tests */
1039 #elif KMP_OS_NETBSD
1040 /* TODO: tune for KMP_OS_NETBSD */
1041 #define KMP_INIT_WAIT 1024U /* initial number of spin-tests   */
1042 #define KMP_NEXT_WAIT 512U /* susequent number of spin-tests */
1043 #endif
1044 
1045 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
1046 typedef struct kmp_cpuid {
1047   kmp_uint32 eax;
1048   kmp_uint32 ebx;
1049   kmp_uint32 ecx;
1050   kmp_uint32 edx;
1051 } kmp_cpuid_t;
1052 extern void __kmp_x86_cpuid(int mode, int mode2, struct kmp_cpuid *p);
1053 #if KMP_ARCH_X86
1054 extern void __kmp_x86_pause(void);
1055 #elif KMP_MIC
1056 // Performance testing on KNC (C0QS-7120 P/A/X/D, 61-core, 16 GB Memory) showed
1057 // regression after removal of extra PAUSE from KMP_YIELD_SPIN(). Changing
1058 // the delay from 100 to 300 showed even better performance than double PAUSE
1059 // on Spec OMP2001 and LCPC tasking tests, no regressions on EPCC.
1060 static void __kmp_x86_pause(void) { _mm_delay_32(300); }
1061 #else
1062 static void __kmp_x86_pause(void) { _mm_pause(); }
1063 #endif
1064 #define KMP_CPU_PAUSE() __kmp_x86_pause()
1065 #elif KMP_ARCH_PPC64
1066 #define KMP_PPC64_PRI_LOW() __asm__ volatile("or 1, 1, 1")
1067 #define KMP_PPC64_PRI_MED() __asm__ volatile("or 2, 2, 2")
1068 #define KMP_PPC64_PRI_LOC_MB() __asm__ volatile("" : : : "memory")
1069 #define KMP_CPU_PAUSE()                                                        \
1070   do {                                                                         \
1071     KMP_PPC64_PRI_LOW();                                                       \
1072     KMP_PPC64_PRI_MED();                                                       \
1073     KMP_PPC64_PRI_LOC_MB();                                                    \
1074   } while (0)
1075 #else
1076 #define KMP_CPU_PAUSE() /* nothing to do */
1077 #endif
1078 
1079 #define KMP_INIT_YIELD(count)                                                  \
1080   { (count) = __kmp_yield_init; }
1081 
1082 #define KMP_YIELD(cond)                                                        \
1083   {                                                                            \
1084     KMP_CPU_PAUSE();                                                           \
1085     __kmp_yield((cond));                                                       \
1086   }
1087 
1088 // Note the decrement of 2 in the following Macros. With KMP_LIBRARY=turnaround,
1089 // there should be no yielding since initial value from KMP_INIT_YIELD() is odd.
1090 
1091 #define KMP_YIELD_WHEN(cond, count)                                            \
1092   {                                                                            \
1093     KMP_CPU_PAUSE();                                                           \
1094     (count) -= 2;                                                              \
1095     if (!(count)) {                                                            \
1096       __kmp_yield(cond);                                                       \
1097       (count) = __kmp_yield_next;                                              \
1098     }                                                                          \
1099   }
1100 #define KMP_YIELD_SPIN(count)                                                  \
1101   {                                                                            \
1102     KMP_CPU_PAUSE();                                                           \
1103     (count) -= 2;                                                              \
1104     if (!(count)) {                                                            \
1105       __kmp_yield(1);                                                          \
1106       (count) = __kmp_yield_next;                                              \
1107     }                                                                          \
1108   }
1109 
1110 /* ------------------------------------------------------------------------ */
1111 /* Support datatypes for the orphaned construct nesting checks.             */
1112 /* ------------------------------------------------------------------------ */
1113 
1114 enum cons_type {
1115   ct_none,
1116   ct_parallel,
1117   ct_pdo,
1118   ct_pdo_ordered,
1119   ct_psections,
1120   ct_psingle,
1121 
1122   /* the following must be left in order and not split up */
1123   ct_taskq,
1124   ct_task, // really task inside non-ordered taskq, considered worksharing type
1125   ct_task_ordered, /* really task inside ordered taskq, considered a worksharing
1126                       type */
1127   /* the preceding must be left in order and not split up */
1128 
1129   ct_critical,
1130   ct_ordered_in_parallel,
1131   ct_ordered_in_pdo,
1132   ct_ordered_in_taskq,
1133   ct_master,
1134   ct_reduce,
1135   ct_barrier
1136 };
1137 
1138 /* test to see if we are in a taskq construct */
1139 #define IS_CONS_TYPE_TASKQ(ct)                                                 \
1140   (((int)(ct)) >= ((int)ct_taskq) && ((int)(ct)) <= ((int)ct_task_ordered))
1141 #define IS_CONS_TYPE_ORDERED(ct)                                               \
1142   ((ct) == ct_pdo_ordered || (ct) == ct_task_ordered)
1143 
1144 struct cons_data {
1145   ident_t const *ident;
1146   enum cons_type type;
1147   int prev;
1148   kmp_user_lock_p
1149       name; /* address exclusively for critical section name comparison */
1150 };
1151 
1152 struct cons_header {
1153   int p_top, w_top, s_top;
1154   int stack_size, stack_top;
1155   struct cons_data *stack_data;
1156 };
1157 
1158 struct kmp_region_info {
1159   char *text;
1160   int offset[KMP_MAX_FIELDS];
1161   int length[KMP_MAX_FIELDS];
1162 };
1163 
1164 /* ---------------------------------------------------------------------- */
1165 /* ---------------------------------------------------------------------- */
1166 
1167 #if KMP_OS_WINDOWS
1168 typedef HANDLE kmp_thread_t;
1169 typedef DWORD kmp_key_t;
1170 #endif /* KMP_OS_WINDOWS */
1171 
1172 #if KMP_OS_UNIX
1173 typedef pthread_t kmp_thread_t;
1174 typedef pthread_key_t kmp_key_t;
1175 #endif
1176 
1177 extern kmp_key_t __kmp_gtid_threadprivate_key;
1178 
1179 typedef struct kmp_sys_info {
1180   long maxrss; /* the maximum resident set size utilized (in kilobytes)     */
1181   long minflt; /* the number of page faults serviced without any I/O        */
1182   long majflt; /* the number of page faults serviced that required I/O      */
1183   long nswap; /* the number of times a process was "swapped" out of memory */
1184   long inblock; /* the number of times the file system had to perform input  */
1185   long oublock; /* the number of times the file system had to perform output */
1186   long nvcsw; /* the number of times a context switch was voluntarily      */
1187   long nivcsw; /* the number of times a context switch was forced           */
1188 } kmp_sys_info_t;
1189 
1190 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
1191 typedef struct kmp_cpuinfo {
1192   int initialized; // If 0, other fields are not initialized.
1193   int signature; // CPUID(1).EAX
1194   int family; // CPUID(1).EAX[27:20]+CPUID(1).EAX[11:8] (Extended Family+Family)
1195   int model; // ( CPUID(1).EAX[19:16] << 4 ) + CPUID(1).EAX[7:4] ( ( Extended
1196   // Model << 4 ) + Model)
1197   int stepping; // CPUID(1).EAX[3:0] ( Stepping )
1198   int sse2; // 0 if SSE2 instructions are not supported, 1 otherwise.
1199   int rtm; // 0 if RTM instructions are not supported, 1 otherwise.
1200   int cpu_stackoffset;
1201   int apic_id;
1202   int physical_id;
1203   int logical_id;
1204   kmp_uint64 frequency; // Nominal CPU frequency in Hz.
1205   char name[3 * sizeof(kmp_cpuid_t)]; // CPUID(0x80000002,0x80000003,0x80000004)
1206 } kmp_cpuinfo_t;
1207 #endif
1208 
1209 #if USE_ITT_BUILD
1210 // We cannot include "kmp_itt.h" due to circular dependency. Declare the only
1211 // required type here. Later we will check the type meets requirements.
1212 typedef int kmp_itt_mark_t;
1213 #define KMP_ITT_DEBUG 0
1214 #endif /* USE_ITT_BUILD */
1215 
1216 /* Taskq data structures */
1217 
1218 #define HIGH_WATER_MARK(nslots) (((nslots)*3) / 4)
1219 // num thunks that each thread can simultaneously execute from a task queue
1220 #define __KMP_TASKQ_THUNKS_PER_TH 1
1221 
1222 /* flags for taskq_global_flags, kmp_task_queue_t tq_flags, kmpc_thunk_t
1223    th_flags  */
1224 
1225 #define TQF_IS_ORDERED 0x0001 // __kmpc_taskq interface, taskq ordered
1226 //  __kmpc_taskq interface, taskq with lastprivate list
1227 #define TQF_IS_LASTPRIVATE 0x0002
1228 #define TQF_IS_NOWAIT 0x0004 // __kmpc_taskq interface, end taskq nowait
1229 // __kmpc_taskq interface, use heuristics to decide task queue size
1230 #define TQF_HEURISTICS 0x0008
1231 
1232 // __kmpc_taskq interface, reserved for future use
1233 #define TQF_INTERFACE_RESERVED1 0x0010
1234 // __kmpc_taskq interface, reserved for future use
1235 #define TQF_INTERFACE_RESERVED2 0x0020
1236 // __kmpc_taskq interface, reserved for future use
1237 #define TQF_INTERFACE_RESERVED3 0x0040
1238 // __kmpc_taskq interface, reserved for future use
1239 #define TQF_INTERFACE_RESERVED4 0x0080
1240 
1241 #define TQF_INTERFACE_FLAGS 0x00ff // all the __kmpc_taskq interface flags
1242 // internal/read by instrumentation; only used with TQF_IS_LASTPRIVATE
1243 #define TQF_IS_LAST_TASK 0x0100
1244 // internal use only; this thunk->th_task is the taskq_task
1245 #define TQF_TASKQ_TASK 0x0200
1246 // internal use only; must release worker threads once ANY queued task
1247 // exists (global)
1248 #define TQF_RELEASE_WORKERS 0x0400
1249 // internal use only; notify workers that master has finished enqueuing tasks
1250 #define TQF_ALL_TASKS_QUEUED 0x0800
1251 // internal use only: this queue encountered in parallel context: not serialized
1252 #define TQF_PARALLEL_CONTEXT 0x1000
1253 // internal use only; this queue is on the freelist and not in use
1254 #define TQF_DEALLOCATED 0x2000
1255 
1256 #define TQF_INTERNAL_FLAGS 0x3f00 // all the internal use only flags
1257 
1258 typedef struct KMP_ALIGN_CACHE kmpc_aligned_int32_t {
1259   kmp_int32 ai_data;
1260 } kmpc_aligned_int32_t;
1261 
1262 typedef struct KMP_ALIGN_CACHE kmpc_aligned_queue_slot_t {
1263   struct kmpc_thunk_t *qs_thunk;
1264 } kmpc_aligned_queue_slot_t;
1265 
1266 typedef struct kmpc_task_queue_t {
1267   /* task queue linkage fields for n-ary tree of queues (locked with global
1268      taskq_tree_lck) */
1269   kmp_lock_t tq_link_lck; /* lock for child link, child next/prev links and
1270                              child ref counts */
1271   union {
1272     struct kmpc_task_queue_t *tq_parent; // pointer to parent taskq, not locked
1273     // for taskq internal freelists, locked with global taskq_freelist_lck
1274     struct kmpc_task_queue_t *tq_next_free;
1275   } tq;
1276   // pointer to linked-list of children, locked by tq's tq_link_lck
1277   volatile struct kmpc_task_queue_t *tq_first_child;
1278   // next child in linked-list, locked by parent tq's tq_link_lck
1279   struct kmpc_task_queue_t *tq_next_child;
1280   // previous child in linked-list, locked by parent tq's tq_link_lck
1281   struct kmpc_task_queue_t *tq_prev_child;
1282   // reference count of threads with access to this task queue
1283   volatile kmp_int32 tq_ref_count;
1284   /* (other than the thread executing the kmpc_end_taskq call) */
1285   /* locked by parent tq's tq_link_lck */
1286 
1287   /* shared data for task queue */
1288   /* per-thread array of pointers to shared variable structures */
1289   struct kmpc_aligned_shared_vars_t *tq_shareds;
1290   /* only one array element exists for all but outermost taskq */
1291 
1292   /* bookkeeping for ordered task queue */
1293   kmp_uint32 tq_tasknum_queuing; // ordered task # assigned while queuing tasks
1294   // ordered number of next task to be served (executed)
1295   volatile kmp_uint32 tq_tasknum_serving;
1296 
1297   /* thunk storage management for task queue */
1298   kmp_lock_t tq_free_thunks_lck; /* lock for thunk freelist manipulation */
1299   // thunk freelist, chained via th.th_next_free
1300   struct kmpc_thunk_t *tq_free_thunks;
1301   // space allocated for thunks for this task queue
1302   struct kmpc_thunk_t *tq_thunk_space;
1303 
1304   /* data fields for queue itself */
1305   kmp_lock_t tq_queue_lck; /* lock for [de]enqueue operations: tq_queue,
1306                               tq_head, tq_tail, tq_nfull */
1307   /* array of queue slots to hold thunks for tasks */
1308   kmpc_aligned_queue_slot_t *tq_queue;
1309   volatile struct kmpc_thunk_t *tq_taskq_slot; /* special slot for taskq task
1310                                                   thunk, occupied if not NULL */
1311   kmp_int32 tq_nslots; /* # of tq_thunk_space thunks alloc'd (not incl.
1312                           tq_taskq_slot space)  */
1313   kmp_int32 tq_head; // enqueue puts item here (index into tq_queue array)
1314   kmp_int32 tq_tail; // dequeue takes item from here (index into tq_queue array)
1315   volatile kmp_int32 tq_nfull; // # of occupied entries in task queue right now
1316   kmp_int32 tq_hiwat; /* high-water mark for tq_nfull and queue scheduling  */
1317   volatile kmp_int32 tq_flags; /*  TQF_xxx  */
1318 
1319   /* bookkeeping for outstanding thunks */
1320 
1321   /* per-thread array for # of regular thunks currently being executed */
1322   struct kmpc_aligned_int32_t *tq_th_thunks;
1323   kmp_int32 tq_nproc; /* number of thunks in the th_thunks array */
1324 
1325   /* statistics library bookkeeping */
1326   ident_t *tq_loc; /*  source location information for taskq directive */
1327 } kmpc_task_queue_t;
1328 
1329 typedef void (*kmpc_task_t)(kmp_int32 global_tid, struct kmpc_thunk_t *thunk);
1330 
1331 /*  sizeof_shareds passed as arg to __kmpc_taskq call  */
1332 typedef struct kmpc_shared_vars_t { /* aligned during dynamic allocation */
1333   kmpc_task_queue_t *sv_queue; /* (pointers to) shared vars */
1334 } kmpc_shared_vars_t;
1335 
1336 typedef struct KMP_ALIGN_CACHE kmpc_aligned_shared_vars_t {
1337   volatile struct kmpc_shared_vars_t *ai_data;
1338 } kmpc_aligned_shared_vars_t;
1339 
1340 /* sizeof_thunk passed as arg to kmpc_taskq call */
1341 typedef struct kmpc_thunk_t { /* aligned during dynamic allocation */
1342   union { /* field used for internal freelists too */
1343     kmpc_shared_vars_t *th_shareds;
1344     struct kmpc_thunk_t *th_next_free; /* freelist of individual thunks within
1345                                           queue, head at tq_free_thunks */
1346   } th;
1347   kmpc_task_t th_task; /* taskq_task if flags & TQF_TASKQ_TASK */
1348   struct kmpc_thunk_t *th_encl_thunk; /* pointer to dynamically enclosing thunk
1349                                          on this thread's call stack */
1350   // TQF_xxx(tq_flags interface plus possible internal flags)
1351   kmp_int32 th_flags;
1352 
1353   kmp_int32 th_status;
1354   kmp_uint32 th_tasknum; /* task number assigned in order of queuing, used for
1355                             ordered sections */
1356   /* private vars */
1357 } kmpc_thunk_t;
1358 
1359 typedef struct KMP_ALIGN_CACHE kmp_taskq {
1360   int tq_curr_thunk_capacity;
1361 
1362   kmpc_task_queue_t *tq_root;
1363   kmp_int32 tq_global_flags;
1364 
1365   kmp_lock_t tq_freelist_lck;
1366   kmpc_task_queue_t *tq_freelist;
1367 
1368   kmpc_thunk_t **tq_curr_thunk;
1369 } kmp_taskq_t;
1370 
1371 /* END Taskq data structures */
1372 
1373 typedef kmp_int32 kmp_critical_name[8];
1374 
1375 /*!
1376 @ingroup PARALLEL
1377 The type for a microtask which gets passed to @ref __kmpc_fork_call().
1378 The arguments to the outlined function are
1379 @param global_tid the global thread identity of the thread executing the
1380 function.
1381 @param bound_tid  the local identitiy of the thread executing the function
1382 @param ... pointers to shared variables accessed by the function.
1383 */
1384 typedef void (*kmpc_micro)(kmp_int32 *global_tid, kmp_int32 *bound_tid, ...);
1385 typedef void (*kmpc_micro_bound)(kmp_int32 *bound_tid, kmp_int32 *bound_nth,
1386                                  ...);
1387 
1388 /*!
1389 @ingroup THREADPRIVATE
1390 @{
1391 */
1392 /* ---------------------------------------------------------------------------
1393  */
1394 /* Threadprivate initialization/finalization function declarations */
1395 
1396 /*  for non-array objects:  __kmpc_threadprivate_register()  */
1397 
1398 /*!
1399  Pointer to the constructor function.
1400  The first argument is the <tt>this</tt> pointer
1401 */
1402 typedef void *(*kmpc_ctor)(void *);
1403 
1404 /*!
1405  Pointer to the destructor function.
1406  The first argument is the <tt>this</tt> pointer
1407 */
1408 typedef void (*kmpc_dtor)(
1409     void * /*, size_t */); /* 2nd arg: magic number for KCC unused by Intel
1410                               compiler */
1411 /*!
1412  Pointer to an alternate constructor.
1413  The first argument is the <tt>this</tt> pointer.
1414 */
1415 typedef void *(*kmpc_cctor)(void *, void *);
1416 
1417 /* for array objects: __kmpc_threadprivate_register_vec() */
1418 /* First arg: "this" pointer */
1419 /* Last arg: number of array elements */
1420 /*!
1421  Array constructor.
1422  First argument is the <tt>this</tt> pointer
1423  Second argument the number of array elements.
1424 */
1425 typedef void *(*kmpc_ctor_vec)(void *, size_t);
1426 /*!
1427  Pointer to the array destructor function.
1428  The first argument is the <tt>this</tt> pointer
1429  Second argument the number of array elements.
1430 */
1431 typedef void (*kmpc_dtor_vec)(void *, size_t);
1432 /*!
1433  Array constructor.
1434  First argument is the <tt>this</tt> pointer
1435  Third argument the number of array elements.
1436 */
1437 typedef void *(*kmpc_cctor_vec)(void *, void *,
1438                                 size_t); /* function unused by compiler */
1439 
1440 /*!
1441 @}
1442 */
1443 
1444 /* keeps tracked of threadprivate cache allocations for cleanup later */
1445 typedef struct kmp_cached_addr {
1446   void **addr; /* address of allocated cache */
1447   struct kmp_cached_addr *next; /* pointer to next cached address */
1448 } kmp_cached_addr_t;
1449 
1450 struct private_data {
1451   struct private_data *next; /* The next descriptor in the list      */
1452   void *data; /* The data buffer for this descriptor  */
1453   int more; /* The repeat count for this descriptor */
1454   size_t size; /* The data size for this descriptor    */
1455 };
1456 
1457 struct private_common {
1458   struct private_common *next;
1459   struct private_common *link;
1460   void *gbl_addr;
1461   void *par_addr; /* par_addr == gbl_addr for MASTER thread */
1462   size_t cmn_size;
1463 };
1464 
1465 struct shared_common {
1466   struct shared_common *next;
1467   struct private_data *pod_init;
1468   void *obj_init;
1469   void *gbl_addr;
1470   union {
1471     kmpc_ctor ctor;
1472     kmpc_ctor_vec ctorv;
1473   } ct;
1474   union {
1475     kmpc_cctor cctor;
1476     kmpc_cctor_vec cctorv;
1477   } cct;
1478   union {
1479     kmpc_dtor dtor;
1480     kmpc_dtor_vec dtorv;
1481   } dt;
1482   size_t vec_len;
1483   int is_vec;
1484   size_t cmn_size;
1485 };
1486 
1487 #define KMP_HASH_TABLE_LOG2 9 /* log2 of the hash table size */
1488 #define KMP_HASH_TABLE_SIZE                                                    \
1489   (1 << KMP_HASH_TABLE_LOG2) /* size of the hash table */
1490 #define KMP_HASH_SHIFT 3 /* throw away this many low bits from the address */
1491 #define KMP_HASH(x)                                                            \
1492   ((((kmp_uintptr_t)x) >> KMP_HASH_SHIFT) & (KMP_HASH_TABLE_SIZE - 1))
1493 
1494 struct common_table {
1495   struct private_common *data[KMP_HASH_TABLE_SIZE];
1496 };
1497 
1498 struct shared_table {
1499   struct shared_common *data[KMP_HASH_TABLE_SIZE];
1500 };
1501 
1502 /* ------------------------------------------------------------------------ */
1503 
1504 #if KMP_STATIC_STEAL_ENABLED
1505 typedef struct KMP_ALIGN_CACHE dispatch_private_info32 {
1506   kmp_int32 count;
1507   kmp_int32 ub;
1508   /* Adding KMP_ALIGN_CACHE here doesn't help / can hurt performance */
1509   kmp_int32 lb;
1510   kmp_int32 st;
1511   kmp_int32 tc;
1512   kmp_int32 static_steal_counter; /* for static_steal only; maybe better to put
1513                                      after ub */
1514 
1515   // KMP_ALIGN( 16 ) ensures ( if the KMP_ALIGN macro is turned on )
1516   //    a) parm3 is properly aligned and
1517   //    b) all parm1-4 are in the same cache line.
1518   // Because of parm1-4 are used together, performance seems to be better
1519   // if they are in the same line (not measured though).
1520 
1521   struct KMP_ALIGN(32) { // AC: changed 16 to 32 in order to simplify template
1522     kmp_int32 parm1; //     structures in kmp_dispatch.cpp. This should
1523     kmp_int32 parm2; //     make no real change at least while padding is off.
1524     kmp_int32 parm3;
1525     kmp_int32 parm4;
1526   };
1527 
1528   kmp_uint32 ordered_lower;
1529   kmp_uint32 ordered_upper;
1530 #if KMP_OS_WINDOWS
1531   // This var can be placed in the hole between 'tc' and 'parm1', instead of
1532   // 'static_steal_counter'. It would be nice to measure execution times.
1533   // Conditional if/endif can be removed at all.
1534   kmp_int32 last_upper;
1535 #endif /* KMP_OS_WINDOWS */
1536 } dispatch_private_info32_t;
1537 
1538 typedef struct KMP_ALIGN_CACHE dispatch_private_info64 {
1539   kmp_int64 count; // current chunk number for static & static-steal scheduling
1540   kmp_int64 ub; /* upper-bound */
1541   /* Adding KMP_ALIGN_CACHE here doesn't help / can hurt performance */
1542   kmp_int64 lb; /* lower-bound */
1543   kmp_int64 st; /* stride */
1544   kmp_int64 tc; /* trip count (number of iterations) */
1545   kmp_int64 static_steal_counter; /* for static_steal only; maybe better to put
1546                                      after ub */
1547 
1548   /* parm[1-4] are used in different ways by different scheduling algorithms */
1549 
1550   // KMP_ALIGN( 32 ) ensures ( if the KMP_ALIGN macro is turned on )
1551   //    a) parm3 is properly aligned and
1552   //    b) all parm1-4 are in the same cache line.
1553   // Because of parm1-4 are used together, performance seems to be better
1554   // if they are in the same line (not measured though).
1555 
1556   struct KMP_ALIGN(32) {
1557     kmp_int64 parm1;
1558     kmp_int64 parm2;
1559     kmp_int64 parm3;
1560     kmp_int64 parm4;
1561   };
1562 
1563   kmp_uint64 ordered_lower;
1564   kmp_uint64 ordered_upper;
1565 #if KMP_OS_WINDOWS
1566   // This var can be placed in the hole between 'tc' and 'parm1', instead of
1567   // 'static_steal_counter'. It would be nice to measure execution times.
1568   // Conditional if/endif can be removed at all.
1569   kmp_int64 last_upper;
1570 #endif /* KMP_OS_WINDOWS */
1571 } dispatch_private_info64_t;
1572 #else /* KMP_STATIC_STEAL_ENABLED */
1573 typedef struct KMP_ALIGN_CACHE dispatch_private_info32 {
1574   kmp_int32 lb;
1575   kmp_int32 ub;
1576   kmp_int32 st;
1577   kmp_int32 tc;
1578 
1579   kmp_int32 parm1;
1580   kmp_int32 parm2;
1581   kmp_int32 parm3;
1582   kmp_int32 parm4;
1583 
1584   kmp_int32 count;
1585 
1586   kmp_uint32 ordered_lower;
1587   kmp_uint32 ordered_upper;
1588 #if KMP_OS_WINDOWS
1589   kmp_int32 last_upper;
1590 #endif /* KMP_OS_WINDOWS */
1591 } dispatch_private_info32_t;
1592 
1593 typedef struct KMP_ALIGN_CACHE dispatch_private_info64 {
1594   kmp_int64 lb; /* lower-bound */
1595   kmp_int64 ub; /* upper-bound */
1596   kmp_int64 st; /* stride */
1597   kmp_int64 tc; /* trip count (number of iterations) */
1598 
1599   /* parm[1-4] are used in different ways by different scheduling algorithms */
1600   kmp_int64 parm1;
1601   kmp_int64 parm2;
1602   kmp_int64 parm3;
1603   kmp_int64 parm4;
1604 
1605   kmp_int64 count; /* current chunk number for static scheduling */
1606 
1607   kmp_uint64 ordered_lower;
1608   kmp_uint64 ordered_upper;
1609 #if KMP_OS_WINDOWS
1610   kmp_int64 last_upper;
1611 #endif /* KMP_OS_WINDOWS */
1612 } dispatch_private_info64_t;
1613 #endif /* KMP_STATIC_STEAL_ENABLED */
1614 
1615 typedef struct KMP_ALIGN_CACHE dispatch_private_info {
1616   union private_info {
1617     dispatch_private_info32_t p32;
1618     dispatch_private_info64_t p64;
1619   } u;
1620   enum sched_type schedule; /* scheduling algorithm */
1621   kmp_int32 ordered; /* ordered clause specified */
1622   kmp_int32 ordered_bumped;
1623   // To retain the structure size after making ordered_iteration scalar
1624   kmp_int32 ordered_dummy[KMP_MAX_ORDERED - 3];
1625   // Stack of buffers for nest of serial regions
1626   struct dispatch_private_info *next;
1627   kmp_int32 nomerge; /* don't merge iters if serialized */
1628   kmp_int32 type_size; /* the size of types in private_info */
1629   enum cons_type pushed_ws;
1630 } dispatch_private_info_t;
1631 
1632 typedef struct dispatch_shared_info32 {
1633   /* chunk index under dynamic, number of idle threads under static-steal;
1634      iteration index otherwise */
1635   volatile kmp_uint32 iteration;
1636   volatile kmp_uint32 num_done;
1637   volatile kmp_uint32 ordered_iteration;
1638   // Dummy to retain the structure size after making ordered_iteration scalar
1639   kmp_int32 ordered_dummy[KMP_MAX_ORDERED - 1];
1640 } dispatch_shared_info32_t;
1641 
1642 typedef struct dispatch_shared_info64 {
1643   /* chunk index under dynamic, number of idle threads under static-steal;
1644      iteration index otherwise */
1645   volatile kmp_uint64 iteration;
1646   volatile kmp_uint64 num_done;
1647   volatile kmp_uint64 ordered_iteration;
1648   // Dummy to retain the structure size after making ordered_iteration scalar
1649   kmp_int64 ordered_dummy[KMP_MAX_ORDERED - 3];
1650 } dispatch_shared_info64_t;
1651 
1652 typedef struct dispatch_shared_info {
1653   union shared_info {
1654     dispatch_shared_info32_t s32;
1655     dispatch_shared_info64_t s64;
1656   } u;
1657   volatile kmp_uint32 buffer_index;
1658 #if OMP_45_ENABLED
1659   volatile kmp_int32 doacross_buf_idx; // teamwise index
1660   volatile kmp_uint32 *doacross_flags; // shared array of iteration flags (0/1)
1661   kmp_int32 doacross_num_done; // count finished threads
1662 #endif
1663 #if KMP_USE_HWLOC
1664   // When linking with libhwloc, the ORDERED EPCC test slows down on big
1665   // machines (> 48 cores). Performance analysis showed that a cache thrash
1666   // was occurring and this padding helps alleviate the problem.
1667   char padding[64];
1668 #endif
1669 } dispatch_shared_info_t;
1670 
1671 typedef struct kmp_disp {
1672   /* Vector for ORDERED SECTION */
1673   void (*th_deo_fcn)(int *gtid, int *cid, ident_t *);
1674   /* Vector for END ORDERED SECTION */
1675   void (*th_dxo_fcn)(int *gtid, int *cid, ident_t *);
1676 
1677   dispatch_shared_info_t *th_dispatch_sh_current;
1678   dispatch_private_info_t *th_dispatch_pr_current;
1679 
1680   dispatch_private_info_t *th_disp_buffer;
1681   kmp_int32 th_disp_index;
1682 #if OMP_45_ENABLED
1683   kmp_int32 th_doacross_buf_idx; // thread's doacross buffer index
1684   volatile kmp_uint32 *th_doacross_flags; // pointer to shared array of flags
1685   union { // we can use union here because doacross cannot be used in
1686     // nonmonotonic loops
1687     kmp_int64 *th_doacross_info; // info on loop bounds
1688     kmp_lock_t *th_steal_lock; // lock used for chunk stealing (8-byte variable)
1689   };
1690 #else
1691 #if KMP_STATIC_STEAL_ENABLED
1692   kmp_lock_t *th_steal_lock; // lock used for chunk stealing (8-byte variable)
1693   void *dummy_padding[1]; // make it 64 bytes on Intel(R) 64
1694 #else
1695   void *dummy_padding[2]; // make it 64 bytes on Intel(R) 64
1696 #endif
1697 #endif
1698 #if KMP_USE_INTERNODE_ALIGNMENT
1699   char more_padding[INTERNODE_CACHE_LINE];
1700 #endif
1701 } kmp_disp_t;
1702 
1703 /* ------------------------------------------------------------------------ */
1704 /* Barrier stuff */
1705 
1706 /* constants for barrier state update */
1707 #define KMP_INIT_BARRIER_STATE 0 /* should probably start from zero */
1708 #define KMP_BARRIER_SLEEP_BIT 0 /* bit used for suspend/sleep part of state */
1709 #define KMP_BARRIER_UNUSED_BIT 1 // bit that must never be set for valid state
1710 #define KMP_BARRIER_BUMP_BIT 2 /* lsb used for bump of go/arrived state */
1711 
1712 #define KMP_BARRIER_SLEEP_STATE (1 << KMP_BARRIER_SLEEP_BIT)
1713 #define KMP_BARRIER_UNUSED_STATE (1 << KMP_BARRIER_UNUSED_BIT)
1714 #define KMP_BARRIER_STATE_BUMP (1 << KMP_BARRIER_BUMP_BIT)
1715 
1716 #if (KMP_BARRIER_SLEEP_BIT >= KMP_BARRIER_BUMP_BIT)
1717 #error "Barrier sleep bit must be smaller than barrier bump bit"
1718 #endif
1719 #if (KMP_BARRIER_UNUSED_BIT >= KMP_BARRIER_BUMP_BIT)
1720 #error "Barrier unused bit must be smaller than barrier bump bit"
1721 #endif
1722 
1723 // Constants for release barrier wait state: currently, hierarchical only
1724 #define KMP_BARRIER_NOT_WAITING 0 // Normal state; worker not in wait_sleep
1725 #define KMP_BARRIER_OWN_FLAG                                                   \
1726   1 // Normal state; worker waiting on own b_go flag in release
1727 #define KMP_BARRIER_PARENT_FLAG                                                \
1728   2 // Special state; worker waiting on parent's b_go flag in release
1729 #define KMP_BARRIER_SWITCH_TO_OWN_FLAG                                         \
1730   3 // Special state; tells worker to shift from parent to own b_go
1731 #define KMP_BARRIER_SWITCHING                                                  \
1732   4 // Special state; worker resets appropriate flag on wake-up
1733 
1734 #define KMP_NOT_SAFE_TO_REAP                                                   \
1735   0 // Thread th_reap_state: not safe to reap (tasking)
1736 #define KMP_SAFE_TO_REAP 1 // Thread th_reap_state: safe to reap (not tasking)
1737 
1738 enum barrier_type {
1739   bs_plain_barrier = 0, /* 0, All non-fork/join barriers (except reduction
1740                            barriers if enabled) */
1741   bs_forkjoin_barrier, /* 1, All fork/join (parallel region) barriers */
1742 #if KMP_FAST_REDUCTION_BARRIER
1743   bs_reduction_barrier, /* 2, All barriers that are used in reduction */
1744 #endif // KMP_FAST_REDUCTION_BARRIER
1745   bs_last_barrier /* Just a placeholder to mark the end */
1746 };
1747 
1748 // to work with reduction barriers just like with plain barriers
1749 #if !KMP_FAST_REDUCTION_BARRIER
1750 #define bs_reduction_barrier bs_plain_barrier
1751 #endif // KMP_FAST_REDUCTION_BARRIER
1752 
1753 typedef enum kmp_bar_pat { /* Barrier communication patterns */
1754                            bp_linear_bar =
1755                                0, /* Single level (degenerate) tree */
1756                            bp_tree_bar =
1757                                1, /* Balanced tree with branching factor 2^n */
1758                            bp_hyper_bar =
1759                                2, /* Hypercube-embedded tree with min branching
1760                                      factor 2^n */
1761                            bp_hierarchical_bar = 3, /* Machine hierarchy tree */
1762                            bp_last_bar /* Placeholder to mark the end */
1763 } kmp_bar_pat_e;
1764 
1765 #define KMP_BARRIER_ICV_PUSH 1
1766 
1767 /* Record for holding the values of the internal controls stack records */
1768 typedef struct kmp_internal_control {
1769   int serial_nesting_level; /* corresponds to the value of the
1770                                th_team_serialized field */
1771   kmp_int8 nested; /* internal control for nested parallelism (per thread) */
1772   kmp_int8 dynamic; /* internal control for dynamic adjustment of threads (per
1773                        thread) */
1774   kmp_int8
1775       bt_set; /* internal control for whether blocktime is explicitly set */
1776   int blocktime; /* internal control for blocktime */
1777 #if KMP_USE_MONITOR
1778   int bt_intervals; /* internal control for blocktime intervals */
1779 #endif
1780   int nproc; /* internal control for #threads for next parallel region (per
1781                 thread) */
1782   int max_active_levels; /* internal control for max_active_levels */
1783   kmp_r_sched_t
1784       sched; /* internal control for runtime schedule {sched,chunk} pair */
1785 #if OMP_40_ENABLED
1786   kmp_proc_bind_t proc_bind; /* internal control for affinity  */
1787   kmp_int32 default_device; /* internal control for default device */
1788 #endif // OMP_40_ENABLED
1789   struct kmp_internal_control *next;
1790 } kmp_internal_control_t;
1791 
1792 static inline void copy_icvs(kmp_internal_control_t *dst,
1793                              kmp_internal_control_t *src) {
1794   *dst = *src;
1795 }
1796 
1797 /* Thread barrier needs volatile barrier fields */
1798 typedef struct KMP_ALIGN_CACHE kmp_bstate {
1799   // th_fixed_icvs is aligned by virtue of kmp_bstate being aligned (and all
1800   // uses of it). It is not explicitly aligned below, because we *don't* want
1801   // it to be padded -- instead, we fit b_go into the same cache line with
1802   // th_fixed_icvs, enabling NGO cache lines stores in the hierarchical barrier.
1803   kmp_internal_control_t th_fixed_icvs; // Initial ICVs for the thread
1804   // Tuck b_go into end of th_fixed_icvs cache line, so it can be stored with
1805   // same NGO store
1806   volatile kmp_uint64 b_go; // STATE => task should proceed (hierarchical)
1807   KMP_ALIGN_CACHE volatile kmp_uint64
1808       b_arrived; // STATE => task reached synch point.
1809   kmp_uint32 *skip_per_level;
1810   kmp_uint32 my_level;
1811   kmp_int32 parent_tid;
1812   kmp_int32 old_tid;
1813   kmp_uint32 depth;
1814   struct kmp_bstate *parent_bar;
1815   kmp_team_t *team;
1816   kmp_uint64 leaf_state;
1817   kmp_uint32 nproc;
1818   kmp_uint8 base_leaf_kids;
1819   kmp_uint8 leaf_kids;
1820   kmp_uint8 offset;
1821   kmp_uint8 wait_flag;
1822   kmp_uint8 use_oncore_barrier;
1823 #if USE_DEBUGGER
1824   // The following field is intended for the debugger solely. Only the worker
1825   // thread itself accesses this field: the worker increases it by 1 when it
1826   // arrives to a barrier.
1827   KMP_ALIGN_CACHE kmp_uint b_worker_arrived;
1828 #endif /* USE_DEBUGGER */
1829 } kmp_bstate_t;
1830 
1831 union KMP_ALIGN_CACHE kmp_barrier_union {
1832   double b_align; /* use worst case alignment */
1833   char b_pad[KMP_PAD(kmp_bstate_t, CACHE_LINE)];
1834   kmp_bstate_t bb;
1835 };
1836 
1837 typedef union kmp_barrier_union kmp_balign_t;
1838 
1839 /* Team barrier needs only non-volatile arrived counter */
1840 union KMP_ALIGN_CACHE kmp_barrier_team_union {
1841   double b_align; /* use worst case alignment */
1842   char b_pad[CACHE_LINE];
1843   struct {
1844     kmp_uint64 b_arrived; /* STATE => task reached synch point. */
1845 #if USE_DEBUGGER
1846     // The following two fields are indended for the debugger solely. Only
1847     // master of the team accesses these fields: the first one is increased by
1848     // 1 when master arrives to a barrier, the second one is increased by one
1849     // when all the threads arrived.
1850     kmp_uint b_master_arrived;
1851     kmp_uint b_team_arrived;
1852 #endif
1853   };
1854 };
1855 
1856 typedef union kmp_barrier_team_union kmp_balign_team_t;
1857 
1858 /* Padding for Linux* OS pthreads condition variables and mutexes used to signal
1859    threads when a condition changes.  This is to workaround an NPTL bug where
1860    padding was added to pthread_cond_t which caused the initialization routine
1861    to write outside of the structure if compiled on pre-NPTL threads.  */
1862 #if KMP_OS_WINDOWS
1863 typedef struct kmp_win32_mutex {
1864   /* The Lock */
1865   CRITICAL_SECTION cs;
1866 } kmp_win32_mutex_t;
1867 
1868 typedef struct kmp_win32_cond {
1869   /* Count of the number of waiters. */
1870   int waiters_count_;
1871 
1872   /* Serialize access to <waiters_count_> */
1873   kmp_win32_mutex_t waiters_count_lock_;
1874 
1875   /* Number of threads to release via a <cond_broadcast> or a <cond_signal> */
1876   int release_count_;
1877 
1878   /* Keeps track of the current "generation" so that we don't allow */
1879   /* one thread to steal all the "releases" from the broadcast. */
1880   int wait_generation_count_;
1881 
1882   /* A manual-reset event that's used to block and release waiting threads. */
1883   HANDLE event_;
1884 } kmp_win32_cond_t;
1885 #endif
1886 
1887 #if KMP_OS_UNIX
1888 
1889 union KMP_ALIGN_CACHE kmp_cond_union {
1890   double c_align;
1891   char c_pad[CACHE_LINE];
1892   pthread_cond_t c_cond;
1893 };
1894 
1895 typedef union kmp_cond_union kmp_cond_align_t;
1896 
1897 union KMP_ALIGN_CACHE kmp_mutex_union {
1898   double m_align;
1899   char m_pad[CACHE_LINE];
1900   pthread_mutex_t m_mutex;
1901 };
1902 
1903 typedef union kmp_mutex_union kmp_mutex_align_t;
1904 
1905 #endif /* KMP_OS_UNIX */
1906 
1907 typedef struct kmp_desc_base {
1908   void *ds_stackbase;
1909   size_t ds_stacksize;
1910   int ds_stackgrow;
1911   kmp_thread_t ds_thread;
1912   volatile int ds_tid;
1913   int ds_gtid;
1914 #if KMP_OS_WINDOWS
1915   volatile int ds_alive;
1916   DWORD ds_thread_id;
1917 /* ds_thread keeps thread handle on Windows* OS. It is enough for RTL purposes.
1918    However, debugger support (libomp_db) cannot work with handles, because they
1919    uncomparable. For example, debugger requests info about thread with handle h.
1920    h is valid within debugger process, and meaningless within debugee process.
1921    Even if h is duped by call to DuplicateHandle(), so the result h' is valid
1922    within debugee process, but it is a *new* handle which does *not* equal to
1923    any other handle in debugee... The only way to compare handles is convert
1924    them to system-wide ids. GetThreadId() function is available only in
1925    Longhorn and Server 2003. :-( In contrast, GetCurrentThreadId() is available
1926    on all Windows* OS flavours (including Windows* 95). Thus, we have to get
1927    thread id by call to GetCurrentThreadId() from within the thread and save it
1928    to let libomp_db identify threads.  */
1929 #endif /* KMP_OS_WINDOWS */
1930 } kmp_desc_base_t;
1931 
1932 typedef union KMP_ALIGN_CACHE kmp_desc {
1933   double ds_align; /* use worst case alignment */
1934   char ds_pad[KMP_PAD(kmp_desc_base_t, CACHE_LINE)];
1935   kmp_desc_base_t ds;
1936 } kmp_desc_t;
1937 
1938 typedef struct kmp_local {
1939   volatile int this_construct; /* count of single's encountered by thread */
1940   void *reduce_data;
1941 #if KMP_USE_BGET
1942   void *bget_data;
1943   void *bget_list;
1944 #if !USE_CMP_XCHG_FOR_BGET
1945 #ifdef USE_QUEUING_LOCK_FOR_BGET
1946   kmp_lock_t bget_lock; /* Lock for accessing bget free list */
1947 #else
1948   kmp_bootstrap_lock_t bget_lock; // Lock for accessing bget free list. Must be
1949 // bootstrap lock so we can use it at library
1950 // shutdown.
1951 #endif /* USE_LOCK_FOR_BGET */
1952 #endif /* ! USE_CMP_XCHG_FOR_BGET */
1953 #endif /* KMP_USE_BGET */
1954 
1955   PACKED_REDUCTION_METHOD_T
1956   packed_reduction_method; /* stored by __kmpc_reduce*(), used by
1957                               __kmpc_end_reduce*() */
1958 
1959 } kmp_local_t;
1960 
1961 #define KMP_CHECK_UPDATE(a, b)                                                 \
1962   if ((a) != (b))                                                              \
1963   (a) = (b)
1964 #define KMP_CHECK_UPDATE_SYNC(a, b)                                            \
1965   if ((a) != (b))                                                              \
1966   TCW_SYNC_PTR((a), (b))
1967 
1968 #define get__blocktime(xteam, xtid)                                            \
1969   ((xteam)->t.t_threads[(xtid)]->th.th_current_task->td_icvs.blocktime)
1970 #define get__bt_set(xteam, xtid)                                               \
1971   ((xteam)->t.t_threads[(xtid)]->th.th_current_task->td_icvs.bt_set)
1972 #if KMP_USE_MONITOR
1973 #define get__bt_intervals(xteam, xtid)                                         \
1974   ((xteam)->t.t_threads[(xtid)]->th.th_current_task->td_icvs.bt_intervals)
1975 #endif
1976 
1977 #define get__nested_2(xteam, xtid)                                             \
1978   ((xteam)->t.t_threads[(xtid)]->th.th_current_task->td_icvs.nested)
1979 #define get__dynamic_2(xteam, xtid)                                            \
1980   ((xteam)->t.t_threads[(xtid)]->th.th_current_task->td_icvs.dynamic)
1981 #define get__nproc_2(xteam, xtid)                                              \
1982   ((xteam)->t.t_threads[(xtid)]->th.th_current_task->td_icvs.nproc)
1983 #define get__sched_2(xteam, xtid)                                              \
1984   ((xteam)->t.t_threads[(xtid)]->th.th_current_task->td_icvs.sched)
1985 
1986 #define set__blocktime_team(xteam, xtid, xval)                                 \
1987   (((xteam)->t.t_threads[(xtid)]->th.th_current_task->td_icvs.blocktime) =     \
1988        (xval))
1989 
1990 #if KMP_USE_MONITOR
1991 #define set__bt_intervals_team(xteam, xtid, xval)                              \
1992   (((xteam)->t.t_threads[(xtid)]->th.th_current_task->td_icvs.bt_intervals) =  \
1993        (xval))
1994 #endif
1995 
1996 #define set__bt_set_team(xteam, xtid, xval)                                    \
1997   (((xteam)->t.t_threads[(xtid)]->th.th_current_task->td_icvs.bt_set) = (xval))
1998 
1999 #define set__nested(xthread, xval)                                             \
2000   (((xthread)->th.th_current_task->td_icvs.nested) = (xval))
2001 #define get__nested(xthread)                                                   \
2002   (((xthread)->th.th_current_task->td_icvs.nested) ? (FTN_TRUE) : (FTN_FALSE))
2003 
2004 #define set__dynamic(xthread, xval)                                            \
2005   (((xthread)->th.th_current_task->td_icvs.dynamic) = (xval))
2006 #define get__dynamic(xthread)                                                  \
2007   (((xthread)->th.th_current_task->td_icvs.dynamic) ? (FTN_TRUE) : (FTN_FALSE))
2008 
2009 #define set__nproc(xthread, xval)                                              \
2010   (((xthread)->th.th_current_task->td_icvs.nproc) = (xval))
2011 
2012 #define set__max_active_levels(xthread, xval)                                  \
2013   (((xthread)->th.th_current_task->td_icvs.max_active_levels) = (xval))
2014 
2015 #define set__sched(xthread, xval)                                              \
2016   (((xthread)->th.th_current_task->td_icvs.sched) = (xval))
2017 
2018 #if OMP_40_ENABLED
2019 
2020 #define set__proc_bind(xthread, xval)                                          \
2021   (((xthread)->th.th_current_task->td_icvs.proc_bind) = (xval))
2022 #define get__proc_bind(xthread)                                                \
2023   ((xthread)->th.th_current_task->td_icvs.proc_bind)
2024 
2025 #endif /* OMP_40_ENABLED */
2026 
2027 // OpenMP tasking data structures
2028 
2029 typedef enum kmp_tasking_mode {
2030   tskm_immediate_exec = 0,
2031   tskm_extra_barrier = 1,
2032   tskm_task_teams = 2,
2033   tskm_max = 2
2034 } kmp_tasking_mode_t;
2035 
2036 extern kmp_tasking_mode_t
2037     __kmp_tasking_mode; /* determines how/when to execute tasks */
2038 extern int __kmp_task_stealing_constraint;
2039 #if OMP_40_ENABLED
2040 extern kmp_int32 __kmp_default_device; // Set via OMP_DEFAULT_DEVICE if
2041 // specified, defaults to 0 otherwise
2042 #endif
2043 #if OMP_45_ENABLED
2044 // Set via OMP_MAX_TASK_PRIORITY if specified, defaults to 0 otherwise
2045 extern kmp_int32 __kmp_max_task_priority;
2046 // Set via KMP_TASKLOOP_MIN_TASKS if specified, defaults to 0 otherwise
2047 extern kmp_uint64 __kmp_taskloop_min_tasks;
2048 #endif
2049 
2050 /* NOTE: kmp_taskdata_t and kmp_task_t structures allocated in single block with
2051    taskdata first */
2052 #define KMP_TASK_TO_TASKDATA(task) (((kmp_taskdata_t *)task) - 1)
2053 #define KMP_TASKDATA_TO_TASK(taskdata) (kmp_task_t *)(taskdata + 1)
2054 
2055 // The tt_found_tasks flag is a signal to all threads in the team that tasks
2056 // were spawned and queued since the previous barrier release.
2057 #define KMP_TASKING_ENABLED(task_team)                                         \
2058   (TCR_SYNC_4((task_team)->tt.tt_found_tasks) == TRUE)
2059 /*!
2060 @ingroup BASIC_TYPES
2061 @{
2062 */
2063 
2064 /*!
2065  */
2066 typedef kmp_int32 (*kmp_routine_entry_t)(kmp_int32, void *);
2067 
2068 #if OMP_40_ENABLED || OMP_45_ENABLED
2069 typedef union kmp_cmplrdata {
2070 #if OMP_45_ENABLED
2071   kmp_int32 priority; /**< priority specified by user for the task */
2072 #endif // OMP_45_ENABLED
2073 #if OMP_40_ENABLED
2074   kmp_routine_entry_t
2075       destructors; /* pointer to function to invoke deconstructors of
2076                       firstprivate C++ objects */
2077 #endif // OMP_40_ENABLED
2078   /* future data */
2079 } kmp_cmplrdata_t;
2080 #endif
2081 
2082 /*  sizeof_kmp_task_t passed as arg to kmpc_omp_task call  */
2083 /*!
2084  */
2085 typedef struct kmp_task { /* GEH: Shouldn't this be aligned somehow? */
2086   void *shareds; /**< pointer to block of pointers to shared vars   */
2087   kmp_routine_entry_t
2088       routine; /**< pointer to routine to call for executing task */
2089   kmp_int32 part_id; /**< part id for the task                          */
2090 #if OMP_40_ENABLED || OMP_45_ENABLED
2091   kmp_cmplrdata_t
2092       data1; /* Two known optional additions: destructors and priority */
2093   kmp_cmplrdata_t data2; /* Process destructors first, priority second */
2094 /* future data */
2095 #endif
2096   /*  private vars  */
2097 } kmp_task_t;
2098 
2099 /*!
2100 @}
2101 */
2102 
2103 #if OMP_40_ENABLED
2104 typedef struct kmp_taskgroup {
2105   kmp_int32 count; // number of allocated and not yet complete tasks
2106   kmp_int32 cancel_request; // request for cancellation of this taskgroup
2107   struct kmp_taskgroup *parent; // parent taskgroup
2108 // TODO: change to OMP_50_ENABLED, need to change build tools for this to work
2109 #if OMP_45_ENABLED
2110   // Block of data to perform task reduction
2111   void *reduce_data; // reduction related info
2112   kmp_int32 reduce_num_data; // number of data items to reduce
2113 #endif
2114 } kmp_taskgroup_t;
2115 
2116 // forward declarations
2117 typedef union kmp_depnode kmp_depnode_t;
2118 typedef struct kmp_depnode_list kmp_depnode_list_t;
2119 typedef struct kmp_dephash_entry kmp_dephash_entry_t;
2120 
2121 typedef struct kmp_depend_info {
2122   kmp_intptr_t base_addr;
2123   size_t len;
2124   struct {
2125     bool in : 1;
2126     bool out : 1;
2127   } flags;
2128 } kmp_depend_info_t;
2129 
2130 struct kmp_depnode_list {
2131   kmp_depnode_t *node;
2132   kmp_depnode_list_t *next;
2133 };
2134 
2135 typedef struct kmp_base_depnode {
2136   kmp_depnode_list_t *successors;
2137   kmp_task_t *task;
2138 
2139   kmp_lock_t lock;
2140 
2141 #if KMP_SUPPORT_GRAPH_OUTPUT
2142   kmp_uint32 id;
2143 #endif
2144 
2145   volatile kmp_int32 npredecessors;
2146   volatile kmp_int32 nrefs;
2147 } kmp_base_depnode_t;
2148 
2149 union KMP_ALIGN_CACHE kmp_depnode {
2150   double dn_align; /* use worst case alignment */
2151   char dn_pad[KMP_PAD(kmp_base_depnode_t, CACHE_LINE)];
2152   kmp_base_depnode_t dn;
2153 };
2154 
2155 struct kmp_dephash_entry {
2156   kmp_intptr_t addr;
2157   kmp_depnode_t *last_out;
2158   kmp_depnode_list_t *last_ins;
2159   kmp_dephash_entry_t *next_in_bucket;
2160 };
2161 
2162 typedef struct kmp_dephash {
2163   kmp_dephash_entry_t **buckets;
2164   size_t size;
2165 #ifdef KMP_DEBUG
2166   kmp_uint32 nelements;
2167   kmp_uint32 nconflicts;
2168 #endif
2169 } kmp_dephash_t;
2170 
2171 #endif
2172 
2173 #ifdef BUILD_TIED_TASK_STACK
2174 
2175 /* Tied Task stack definitions */
2176 typedef struct kmp_stack_block {
2177   kmp_taskdata_t *sb_block[TASK_STACK_BLOCK_SIZE];
2178   struct kmp_stack_block *sb_next;
2179   struct kmp_stack_block *sb_prev;
2180 } kmp_stack_block_t;
2181 
2182 typedef struct kmp_task_stack {
2183   kmp_stack_block_t ts_first_block; // first block of stack entries
2184   kmp_taskdata_t **ts_top; // pointer to the top of stack
2185   kmp_int32 ts_entries; // number of entries on the stack
2186 } kmp_task_stack_t;
2187 
2188 #endif // BUILD_TIED_TASK_STACK
2189 
2190 typedef struct kmp_tasking_flags { /* Total struct must be exactly 32 bits */
2191   /* Compiler flags */ /* Total compiler flags must be 16 bits */
2192   unsigned tiedness : 1; /* task is either tied (1) or untied (0) */
2193   unsigned final : 1; /* task is final(1) so execute immediately */
2194   unsigned merged_if0 : 1; /* no __kmpc_task_{begin/complete}_if0 calls in if0
2195                               code path */
2196 #if OMP_40_ENABLED
2197   unsigned destructors_thunk : 1; /* set if the compiler creates a thunk to
2198                                      invoke destructors from the runtime */
2199 #if OMP_45_ENABLED
2200   unsigned proxy : 1; /* task is a proxy task (it will be executed outside the
2201                          context of the RTL) */
2202   unsigned priority_specified : 1; /* set if the compiler provides priority
2203                                       setting for the task */
2204   unsigned reserved : 10; /* reserved for compiler use */
2205 #else
2206   unsigned reserved : 12; /* reserved for compiler use */
2207 #endif
2208 #else // OMP_40_ENABLED
2209   unsigned reserved : 13; /* reserved for compiler use */
2210 #endif // OMP_40_ENABLED
2211 
2212   /* Library flags */ /* Total library flags must be 16 bits */
2213   unsigned tasktype : 1; /* task is either explicit(1) or implicit (0) */
2214   unsigned task_serial : 1; // task is executed immediately (1) or deferred (0)
2215   unsigned tasking_ser : 1; // all tasks in team are either executed immediately
2216   // (1) or may be deferred (0)
2217   unsigned team_serial : 1; // entire team is serial (1) [1 thread] or parallel
2218   // (0) [>= 2 threads]
2219   /* If either team_serial or tasking_ser is set, task team may be NULL */
2220   /* Task State Flags: */
2221   unsigned started : 1; /* 1==started, 0==not started     */
2222   unsigned executing : 1; /* 1==executing, 0==not executing */
2223   unsigned complete : 1; /* 1==complete, 0==not complete   */
2224   unsigned freed : 1; /* 1==freed, 0==allocateed        */
2225   unsigned native : 1; /* 1==gcc-compiled task, 0==intel */
2226   unsigned reserved31 : 7; /* reserved for library use */
2227 
2228 } kmp_tasking_flags_t;
2229 
2230 struct kmp_taskdata { /* aligned during dynamic allocation       */
2231   kmp_int32 td_task_id; /* id, assigned by debugger                */
2232   kmp_tasking_flags_t td_flags; /* task flags                              */
2233   kmp_team_t *td_team; /* team for this task                      */
2234   kmp_info_p *td_alloc_thread; /* thread that allocated data structures   */
2235   /* Currently not used except for perhaps IDB */
2236   kmp_taskdata_t *td_parent; /* parent task                             */
2237   kmp_int32 td_level; /* task nesting level                      */
2238   kmp_int32 td_untied_count; /* untied task active parts counter        */
2239   ident_t *td_ident; /* task identifier                         */
2240   // Taskwait data.
2241   ident_t *td_taskwait_ident;
2242   kmp_uint32 td_taskwait_counter;
2243   kmp_int32 td_taskwait_thread; /* gtid + 1 of thread encountered taskwait */
2244   KMP_ALIGN_CACHE kmp_internal_control_t
2245       td_icvs; /* Internal control variables for the task */
2246   KMP_ALIGN_CACHE volatile kmp_int32
2247       td_allocated_child_tasks; /* Child tasks (+ current task) not yet
2248                                    deallocated */
2249   volatile kmp_int32
2250       td_incomplete_child_tasks; /* Child tasks not yet complete */
2251 #if OMP_40_ENABLED
2252   kmp_taskgroup_t
2253       *td_taskgroup; // Each task keeps pointer to its current taskgroup
2254   kmp_dephash_t
2255       *td_dephash; // Dependencies for children tasks are tracked from here
2256   kmp_depnode_t
2257       *td_depnode; // Pointer to graph node if this task has dependencies
2258 #endif // OMP_40_ENABLED
2259 #if OMP_45_ENABLED
2260   kmp_task_team_t *td_task_team;
2261   kmp_int32 td_size_alloc; // The size of task structure, including shareds etc.
2262 #endif // OMP_45_ENABLED
2263   kmp_taskdata_t *td_last_tied; // keep tied task for task scheduling constraint
2264 #if OMPT_SUPPORT
2265   ompt_task_info_t ompt_task_info;
2266 #endif
2267 }; // struct kmp_taskdata
2268 
2269 // Make sure padding above worked
2270 KMP_BUILD_ASSERT(sizeof(kmp_taskdata_t) % sizeof(void *) == 0);
2271 
2272 // Data for task team but per thread
2273 typedef struct kmp_base_thread_data {
2274   kmp_info_p *td_thr; // Pointer back to thread info
2275   // Used only in __kmp_execute_tasks_template, maybe not avail until task is
2276   // queued?
2277   kmp_bootstrap_lock_t td_deque_lock; // Lock for accessing deque
2278   kmp_taskdata_t *
2279       *td_deque; // Deque of tasks encountered by td_thr, dynamically allocated
2280   kmp_int32 td_deque_size; // Size of deck
2281   kmp_uint32 td_deque_head; // Head of deque (will wrap)
2282   kmp_uint32 td_deque_tail; // Tail of deque (will wrap)
2283   kmp_int32 td_deque_ntasks; // Number of tasks in deque
2284   // GEH: shouldn't this be volatile since used in while-spin?
2285   kmp_int32 td_deque_last_stolen; // Thread number of last successful steal
2286 #ifdef BUILD_TIED_TASK_STACK
2287   kmp_task_stack_t td_susp_tied_tasks; // Stack of suspended tied tasks for task
2288 // scheduling constraint
2289 #endif // BUILD_TIED_TASK_STACK
2290 } kmp_base_thread_data_t;
2291 
2292 #define TASK_DEQUE_BITS 8 // Used solely to define INITIAL_TASK_DEQUE_SIZE
2293 #define INITIAL_TASK_DEQUE_SIZE (1 << TASK_DEQUE_BITS)
2294 
2295 #define TASK_DEQUE_SIZE(td) ((td).td_deque_size)
2296 #define TASK_DEQUE_MASK(td) ((td).td_deque_size - 1)
2297 
2298 typedef union KMP_ALIGN_CACHE kmp_thread_data {
2299   kmp_base_thread_data_t td;
2300   double td_align; /* use worst case alignment */
2301   char td_pad[KMP_PAD(kmp_base_thread_data_t, CACHE_LINE)];
2302 } kmp_thread_data_t;
2303 
2304 // Data for task teams which are used when tasking is enabled for the team
2305 typedef struct kmp_base_task_team {
2306   kmp_bootstrap_lock_t
2307       tt_threads_lock; /* Lock used to allocate per-thread part of task team */
2308   /* must be bootstrap lock since used at library shutdown*/
2309   kmp_task_team_t *tt_next; /* For linking the task team free list */
2310   kmp_thread_data_t
2311       *tt_threads_data; /* Array of per-thread structures for task team */
2312   /* Data survives task team deallocation */
2313   kmp_int32 tt_found_tasks; /* Have we found tasks and queued them while
2314                                executing this team? */
2315   /* TRUE means tt_threads_data is set up and initialized */
2316   kmp_int32 tt_nproc; /* #threads in team           */
2317   kmp_int32
2318       tt_max_threads; /* number of entries allocated for threads_data array */
2319 #if OMP_45_ENABLED
2320   kmp_int32
2321       tt_found_proxy_tasks; /* Have we found proxy tasks since last barrier */
2322 #endif
2323   kmp_int32 tt_untied_task_encountered;
2324 
2325   KMP_ALIGN_CACHE
2326   volatile kmp_int32 tt_unfinished_threads; /* #threads still active      */
2327 
2328   KMP_ALIGN_CACHE
2329   volatile kmp_uint32
2330       tt_active; /* is the team still actively executing tasks */
2331 } kmp_base_task_team_t;
2332 
2333 union KMP_ALIGN_CACHE kmp_task_team {
2334   kmp_base_task_team_t tt;
2335   double tt_align; /* use worst case alignment */
2336   char tt_pad[KMP_PAD(kmp_base_task_team_t, CACHE_LINE)];
2337 };
2338 
2339 #if (USE_FAST_MEMORY == 3) || (USE_FAST_MEMORY == 5)
2340 // Free lists keep same-size free memory slots for fast memory allocation
2341 // routines
2342 typedef struct kmp_free_list {
2343   void *th_free_list_self; // Self-allocated tasks free list
2344   void *th_free_list_sync; // Self-allocated tasks stolen/returned by other
2345   // threads
2346   void *th_free_list_other; // Non-self free list (to be returned to owner's
2347   // sync list)
2348 } kmp_free_list_t;
2349 #endif
2350 #if KMP_NESTED_HOT_TEAMS
2351 // Hot teams array keeps hot teams and their sizes for given thread. Hot teams
2352 // are not put in teams pool, and they don't put threads in threads pool.
2353 typedef struct kmp_hot_team_ptr {
2354   kmp_team_p *hot_team; // pointer to hot_team of given nesting level
2355   kmp_int32 hot_team_nth; // number of threads allocated for the hot_team
2356 } kmp_hot_team_ptr_t;
2357 #endif
2358 #if OMP_40_ENABLED
2359 typedef struct kmp_teams_size {
2360   kmp_int32 nteams; // number of teams in a league
2361   kmp_int32 nth; // number of threads in each team of the league
2362 } kmp_teams_size_t;
2363 #endif
2364 
2365 // OpenMP thread data structures
2366 
2367 typedef struct KMP_ALIGN_CACHE kmp_base_info {
2368   /* Start with the readonly data which is cache aligned and padded. This is
2369      written before the thread starts working by the master. Uber masters may
2370      update themselves later. Usage does not consider serialized regions.  */
2371   kmp_desc_t th_info;
2372   kmp_team_p *th_team; /* team we belong to */
2373   kmp_root_p *th_root; /* pointer to root of task hierarchy */
2374   kmp_info_p *th_next_pool; /* next available thread in the pool */
2375   kmp_disp_t *th_dispatch; /* thread's dispatch data */
2376   int th_in_pool; /* in thread pool (32 bits for TCR/TCW) */
2377 
2378   /* The following are cached from the team info structure */
2379   /* TODO use these in more places as determined to be needed via profiling */
2380   int th_team_nproc; /* number of threads in a team */
2381   kmp_info_p *th_team_master; /* the team's master thread */
2382   int th_team_serialized; /* team is serialized */
2383 #if OMP_40_ENABLED
2384   microtask_t th_teams_microtask; /* save entry address for teams construct */
2385   int th_teams_level; /* save initial level of teams construct */
2386 /* it is 0 on device but may be any on host */
2387 #endif
2388 
2389 /* The blocktime info is copied from the team struct to the thread sruct */
2390 /* at the start of a barrier, and the values stored in the team are used */
2391 /* at points in the code where the team struct is no longer guaranteed   */
2392 /* to exist (from the POV of worker threads).                            */
2393 #if KMP_USE_MONITOR
2394   int th_team_bt_intervals;
2395   int th_team_bt_set;
2396 #else
2397   kmp_uint64 th_team_bt_intervals;
2398 #endif
2399 
2400 #if KMP_AFFINITY_SUPPORTED
2401   kmp_affin_mask_t *th_affin_mask; /* thread's current affinity mask */
2402 #endif
2403 
2404   /* The data set by the master at reinit, then R/W by the worker */
2405   KMP_ALIGN_CACHE int
2406       th_set_nproc; /* if > 0, then only use this request for the next fork */
2407 #if KMP_NESTED_HOT_TEAMS
2408   kmp_hot_team_ptr_t *th_hot_teams; /* array of hot teams */
2409 #endif
2410 #if OMP_40_ENABLED
2411   kmp_proc_bind_t
2412       th_set_proc_bind; /* if != proc_bind_default, use request for next fork */
2413   kmp_teams_size_t
2414       th_teams_size; /* number of teams/threads in teams construct */
2415 #if KMP_AFFINITY_SUPPORTED
2416   int th_current_place; /* place currently bound to */
2417   int th_new_place; /* place to bind to in par reg */
2418   int th_first_place; /* first place in partition */
2419   int th_last_place; /* last place in partition */
2420 #endif
2421 #endif
2422 #if USE_ITT_BUILD
2423   kmp_uint64 th_bar_arrive_time; /* arrival to barrier timestamp */
2424   kmp_uint64 th_bar_min_time; /* minimum arrival time at the barrier */
2425   kmp_uint64 th_frame_time; /* frame timestamp */
2426 #endif /* USE_ITT_BUILD */
2427   kmp_local_t th_local;
2428   struct private_common *th_pri_head;
2429 
2430   /* Now the data only used by the worker (after initial allocation) */
2431   /* TODO the first serial team should actually be stored in the info_t
2432      structure.  this will help reduce initial allocation overhead */
2433   KMP_ALIGN_CACHE kmp_team_p
2434       *th_serial_team; /*serialized team held in reserve*/
2435 
2436 #if OMPT_SUPPORT
2437   ompt_thread_info_t ompt_thread_info;
2438 #endif
2439 
2440   /* The following are also read by the master during reinit */
2441   struct common_table *th_pri_common;
2442 
2443   volatile kmp_uint32 th_spin_here; /* thread-local location for spinning */
2444   /* while awaiting queuing lock acquire */
2445 
2446   volatile void *th_sleep_loc; // this points at a kmp_flag<T>
2447 
2448   ident_t *th_ident;
2449   unsigned th_x; // Random number generator data
2450   unsigned th_a; // Random number generator data
2451 
2452   /* Tasking-related data for the thread */
2453   kmp_task_team_t *th_task_team; // Task team struct
2454   kmp_taskdata_t *th_current_task; // Innermost Task being executed
2455   kmp_uint8 th_task_state; // alternating 0/1 for task team identification
2456   kmp_uint8 *th_task_state_memo_stack; // Stack holding memos of th_task_state
2457   // at nested levels
2458   kmp_uint32 th_task_state_top; // Top element of th_task_state_memo_stack
2459   kmp_uint32 th_task_state_stack_sz; // Size of th_task_state_memo_stack
2460   kmp_uint32 th_reap_state; // Non-zero indicates thread is not
2461   // tasking, thus safe to reap
2462 
2463   /* More stuff for keeping track of active/sleeping threads (this part is
2464      written by the worker thread) */
2465   kmp_uint8 th_active_in_pool; // included in count of #active threads in pool
2466   int th_active; // ! sleeping; 32 bits for TCR/TCW
2467   struct cons_header *th_cons; // used for consistency check
2468 
2469   /* Add the syncronizing data which is cache aligned and padded. */
2470   KMP_ALIGN_CACHE kmp_balign_t th_bar[bs_last_barrier];
2471 
2472   KMP_ALIGN_CACHE volatile kmp_int32
2473       th_next_waiting; /* gtid+1 of next thread on lock wait queue, 0 if none */
2474 
2475 #if (USE_FAST_MEMORY == 3) || (USE_FAST_MEMORY == 5)
2476 #define NUM_LISTS 4
2477   kmp_free_list_t th_free_lists[NUM_LISTS]; // Free lists for fast memory
2478 // allocation routines
2479 #endif
2480 
2481 #if KMP_OS_WINDOWS
2482   kmp_win32_cond_t th_suspend_cv;
2483   kmp_win32_mutex_t th_suspend_mx;
2484   int th_suspend_init;
2485 #endif
2486 #if KMP_OS_UNIX
2487   kmp_cond_align_t th_suspend_cv;
2488   kmp_mutex_align_t th_suspend_mx;
2489   int th_suspend_init_count;
2490 #endif
2491 
2492 #if USE_ITT_BUILD
2493   kmp_itt_mark_t th_itt_mark_single;
2494 // alignment ???
2495 #endif /* USE_ITT_BUILD */
2496 #if KMP_STATS_ENABLED
2497   kmp_stats_list *th_stats;
2498 #endif
2499 } kmp_base_info_t;
2500 
2501 typedef union KMP_ALIGN_CACHE kmp_info {
2502   double th_align; /* use worst case alignment */
2503   char th_pad[KMP_PAD(kmp_base_info_t, CACHE_LINE)];
2504   kmp_base_info_t th;
2505 } kmp_info_t;
2506 
2507 // OpenMP thread team data structures
2508 
2509 typedef struct kmp_base_data { volatile kmp_uint32 t_value; } kmp_base_data_t;
2510 
2511 typedef union KMP_ALIGN_CACHE kmp_sleep_team {
2512   double dt_align; /* use worst case alignment */
2513   char dt_pad[KMP_PAD(kmp_base_data_t, CACHE_LINE)];
2514   kmp_base_data_t dt;
2515 } kmp_sleep_team_t;
2516 
2517 typedef union KMP_ALIGN_CACHE kmp_ordered_team {
2518   double dt_align; /* use worst case alignment */
2519   char dt_pad[KMP_PAD(kmp_base_data_t, CACHE_LINE)];
2520   kmp_base_data_t dt;
2521 } kmp_ordered_team_t;
2522 
2523 typedef int (*launch_t)(int gtid);
2524 
2525 /* Minimum number of ARGV entries to malloc if necessary */
2526 #define KMP_MIN_MALLOC_ARGV_ENTRIES 100
2527 
2528 // Set up how many argv pointers will fit in cache lines containing
2529 // t_inline_argv. Historically, we have supported at least 96 bytes. Using a
2530 // larger value for more space between the master write/worker read section and
2531 // read/write by all section seems to buy more performance on EPCC PARALLEL.
2532 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
2533 #define KMP_INLINE_ARGV_BYTES                                                  \
2534   (4 * CACHE_LINE -                                                            \
2535    ((3 * KMP_PTR_SKIP + 2 * sizeof(int) + 2 * sizeof(kmp_int8) +               \
2536      sizeof(kmp_int16) + sizeof(kmp_uint32)) %                                 \
2537     CACHE_LINE))
2538 #else
2539 #define KMP_INLINE_ARGV_BYTES                                                  \
2540   (2 * CACHE_LINE - ((3 * KMP_PTR_SKIP + 2 * sizeof(int)) % CACHE_LINE))
2541 #endif
2542 #define KMP_INLINE_ARGV_ENTRIES (int)(KMP_INLINE_ARGV_BYTES / KMP_PTR_SKIP)
2543 
2544 typedef struct KMP_ALIGN_CACHE kmp_base_team {
2545   // Synchronization Data
2546   // ---------------------------------------------------------------------------
2547   KMP_ALIGN_CACHE kmp_ordered_team_t t_ordered;
2548   kmp_balign_team_t t_bar[bs_last_barrier];
2549   volatile int t_construct; // count of single directive encountered by team
2550   char pad[sizeof(kmp_lock_t)]; // padding to maintain performance on big iron
2551 
2552   // Master only
2553   // ---------------------------------------------------------------------------
2554   KMP_ALIGN_CACHE int t_master_tid; // tid of master in parent team
2555   int t_master_this_cons; // "this_construct" single counter of master in parent
2556   // team
2557   ident_t *t_ident; // if volatile, have to change too much other crud to
2558   // volatile too
2559   kmp_team_p *t_parent; // parent team
2560   kmp_team_p *t_next_pool; // next free team in the team pool
2561   kmp_disp_t *t_dispatch; // thread's dispatch data
2562   kmp_task_team_t *t_task_team[2]; // Task team struct; switch between 2
2563 #if OMP_40_ENABLED
2564   kmp_proc_bind_t t_proc_bind; // bind type for par region
2565 #endif // OMP_40_ENABLED
2566 #if USE_ITT_BUILD
2567   kmp_uint64 t_region_time; // region begin timestamp
2568 #endif /* USE_ITT_BUILD */
2569 
2570   // Master write, workers read
2571   // --------------------------------------------------------------------------
2572   KMP_ALIGN_CACHE void **t_argv;
2573   int t_argc;
2574   int t_nproc; // number of threads in team
2575   microtask_t t_pkfn;
2576   launch_t t_invoke; // procedure to launch the microtask
2577 
2578 #if OMPT_SUPPORT
2579   ompt_team_info_t ompt_team_info;
2580   ompt_lw_taskteam_t *ompt_serialized_team_info;
2581 #endif
2582 
2583 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
2584   kmp_int8 t_fp_control_saved;
2585   kmp_int8 t_pad2b;
2586   kmp_int16 t_x87_fpu_control_word; // FP control regs
2587   kmp_uint32 t_mxcsr;
2588 #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
2589 
2590   void *t_inline_argv[KMP_INLINE_ARGV_ENTRIES];
2591 
2592   KMP_ALIGN_CACHE kmp_info_t **t_threads;
2593   kmp_taskdata_t
2594       *t_implicit_task_taskdata; // Taskdata for the thread's implicit task
2595   int t_level; // nested parallel level
2596 
2597   KMP_ALIGN_CACHE int t_max_argc;
2598   int t_max_nproc; // max threads this team can handle (dynamicly expandable)
2599   int t_serialized; // levels deep of serialized teams
2600   dispatch_shared_info_t *t_disp_buffer; // buffers for dispatch system
2601   int t_id; // team's id, assigned by debugger.
2602   int t_active_level; // nested active parallel level
2603   kmp_r_sched_t t_sched; // run-time schedule for the team
2604 #if OMP_40_ENABLED && KMP_AFFINITY_SUPPORTED
2605   int t_first_place; // first & last place in parent thread's partition.
2606   int t_last_place; // Restore these values to master after par region.
2607 #endif // OMP_40_ENABLED && KMP_AFFINITY_SUPPORTED
2608   int t_size_changed; // team size was changed?: 0: no, 1: yes, -1: changed via
2609 // omp_set_num_threads() call
2610 
2611 // Read/write by workers as well
2612 #if (KMP_ARCH_X86 || KMP_ARCH_X86_64)
2613   // Using CACHE_LINE=64 reduces memory footprint, but causes a big perf
2614   // regression of epcc 'parallel' and 'barrier' on fxe256lin01. This extra
2615   // padding serves to fix the performance of epcc 'parallel' and 'barrier' when
2616   // CACHE_LINE=64. TODO: investigate more and get rid if this padding.
2617   char dummy_padding[1024];
2618 #endif
2619   // Internal control stack for additional nested teams.
2620   KMP_ALIGN_CACHE kmp_internal_control_t *t_control_stack_top;
2621 // for SERIALIZED teams nested 2 or more levels deep
2622 #if OMP_40_ENABLED
2623   // typed flag to store request state of cancellation
2624   kmp_int32 t_cancel_request;
2625 #endif
2626   int t_master_active; // save on fork, restore on join
2627   kmp_taskq_t t_taskq; // this team's task queue
2628   void *t_copypriv_data; // team specific pointer to copyprivate data array
2629   kmp_uint32 t_copyin_counter;
2630 #if USE_ITT_BUILD
2631   void *t_stack_id; // team specific stack stitching id (for ittnotify)
2632 #endif /* USE_ITT_BUILD */
2633 } kmp_base_team_t;
2634 
2635 union KMP_ALIGN_CACHE kmp_team {
2636   kmp_base_team_t t;
2637   double t_align; /* use worst case alignment */
2638   char t_pad[KMP_PAD(kmp_base_team_t, CACHE_LINE)];
2639 };
2640 
2641 typedef union KMP_ALIGN_CACHE kmp_time_global {
2642   double dt_align; /* use worst case alignment */
2643   char dt_pad[KMP_PAD(kmp_base_data_t, CACHE_LINE)];
2644   kmp_base_data_t dt;
2645 } kmp_time_global_t;
2646 
2647 typedef struct kmp_base_global {
2648   /* cache-aligned */
2649   kmp_time_global_t g_time;
2650 
2651   /* non cache-aligned */
2652   volatile int g_abort;
2653   volatile int g_done;
2654 
2655   int g_dynamic;
2656   enum dynamic_mode g_dynamic_mode;
2657 } kmp_base_global_t;
2658 
2659 typedef union KMP_ALIGN_CACHE kmp_global {
2660   kmp_base_global_t g;
2661   double g_align; /* use worst case alignment */
2662   char g_pad[KMP_PAD(kmp_base_global_t, CACHE_LINE)];
2663 } kmp_global_t;
2664 
2665 typedef struct kmp_base_root {
2666   // TODO: GEH - combine r_active with r_in_parallel then r_active ==
2667   // (r_in_parallel>= 0)
2668   // TODO: GEH - then replace r_active with t_active_levels if we can to reduce
2669   // the synch overhead or keeping r_active
2670   volatile int r_active; /* TRUE if some region in a nest has > 1 thread */
2671   // GEH: This is misnamed, should be r_in_parallel
2672   volatile int r_nested; // TODO: GEH - This is unused, just remove it entirely.
2673   int r_in_parallel; /* keeps a count of active parallel regions per root */
2674   // GEH: This is misnamed, should be r_active_levels
2675   kmp_team_t *r_root_team;
2676   kmp_team_t *r_hot_team;
2677   kmp_info_t *r_uber_thread;
2678   kmp_lock_t r_begin_lock;
2679   volatile int r_begin;
2680   int r_blocktime; /* blocktime for this root and descendants */
2681   int r_cg_nthreads; // count of active threads in a contention group
2682 } kmp_base_root_t;
2683 
2684 typedef union KMP_ALIGN_CACHE kmp_root {
2685   kmp_base_root_t r;
2686   double r_align; /* use worst case alignment */
2687   char r_pad[KMP_PAD(kmp_base_root_t, CACHE_LINE)];
2688 } kmp_root_t;
2689 
2690 struct fortran_inx_info {
2691   kmp_int32 data;
2692 };
2693 
2694 /* ------------------------------------------------------------------------ */
2695 
2696 extern int __kmp_settings;
2697 extern int __kmp_duplicate_library_ok;
2698 #if USE_ITT_BUILD
2699 extern int __kmp_forkjoin_frames;
2700 extern int __kmp_forkjoin_frames_mode;
2701 #endif
2702 extern PACKED_REDUCTION_METHOD_T __kmp_force_reduction_method;
2703 extern int __kmp_determ_red;
2704 
2705 #ifdef KMP_DEBUG
2706 extern int kmp_a_debug;
2707 extern int kmp_b_debug;
2708 extern int kmp_c_debug;
2709 extern int kmp_d_debug;
2710 extern int kmp_e_debug;
2711 extern int kmp_f_debug;
2712 #endif /* KMP_DEBUG */
2713 
2714 /* For debug information logging using rotating buffer */
2715 #define KMP_DEBUG_BUF_LINES_INIT 512
2716 #define KMP_DEBUG_BUF_LINES_MIN 1
2717 
2718 #define KMP_DEBUG_BUF_CHARS_INIT 128
2719 #define KMP_DEBUG_BUF_CHARS_MIN 2
2720 
2721 extern int
2722     __kmp_debug_buf; /* TRUE means use buffer, FALSE means print to stderr */
2723 extern int __kmp_debug_buf_lines; /* How many lines of debug stored in buffer */
2724 extern int
2725     __kmp_debug_buf_chars; /* How many characters allowed per line in buffer */
2726 extern int __kmp_debug_buf_atomic; /* TRUE means use atomic update of buffer
2727                                       entry pointer */
2728 
2729 extern char *__kmp_debug_buffer; /* Debug buffer itself */
2730 extern int __kmp_debug_count; /* Counter for number of lines printed in buffer
2731                                  so far */
2732 extern int __kmp_debug_buf_warn_chars; /* Keep track of char increase
2733                                           recommended in warnings */
2734 /* end rotating debug buffer */
2735 
2736 #ifdef KMP_DEBUG
2737 extern int __kmp_par_range; /* +1 => only go par for constructs in range */
2738 
2739 #define KMP_PAR_RANGE_ROUTINE_LEN 1024
2740 extern char __kmp_par_range_routine[KMP_PAR_RANGE_ROUTINE_LEN];
2741 #define KMP_PAR_RANGE_FILENAME_LEN 1024
2742 extern char __kmp_par_range_filename[KMP_PAR_RANGE_FILENAME_LEN];
2743 extern int __kmp_par_range_lb;
2744 extern int __kmp_par_range_ub;
2745 #endif
2746 
2747 /* For printing out dynamic storage map for threads and teams */
2748 extern int
2749     __kmp_storage_map; /* True means print storage map for threads and teams */
2750 extern int __kmp_storage_map_verbose; /* True means storage map includes
2751                                          placement info */
2752 extern int __kmp_storage_map_verbose_specified;
2753 
2754 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
2755 extern kmp_cpuinfo_t __kmp_cpuinfo;
2756 #endif
2757 
2758 extern volatile int __kmp_init_serial;
2759 extern volatile int __kmp_init_gtid;
2760 extern volatile int __kmp_init_common;
2761 extern volatile int __kmp_init_middle;
2762 extern volatile int __kmp_init_parallel;
2763 #if KMP_USE_MONITOR
2764 extern volatile int __kmp_init_monitor;
2765 #endif
2766 extern volatile int __kmp_init_user_locks;
2767 extern int __kmp_init_counter;
2768 extern int __kmp_root_counter;
2769 extern int __kmp_version;
2770 
2771 /* list of address of allocated caches for commons */
2772 extern kmp_cached_addr_t *__kmp_threadpriv_cache_list;
2773 
2774 /* Barrier algorithm types and options */
2775 extern kmp_uint32 __kmp_barrier_gather_bb_dflt;
2776 extern kmp_uint32 __kmp_barrier_release_bb_dflt;
2777 extern kmp_bar_pat_e __kmp_barrier_gather_pat_dflt;
2778 extern kmp_bar_pat_e __kmp_barrier_release_pat_dflt;
2779 extern kmp_uint32 __kmp_barrier_gather_branch_bits[bs_last_barrier];
2780 extern kmp_uint32 __kmp_barrier_release_branch_bits[bs_last_barrier];
2781 extern kmp_bar_pat_e __kmp_barrier_gather_pattern[bs_last_barrier];
2782 extern kmp_bar_pat_e __kmp_barrier_release_pattern[bs_last_barrier];
2783 extern char const *__kmp_barrier_branch_bit_env_name[bs_last_barrier];
2784 extern char const *__kmp_barrier_pattern_env_name[bs_last_barrier];
2785 extern char const *__kmp_barrier_type_name[bs_last_barrier];
2786 extern char const *__kmp_barrier_pattern_name[bp_last_bar];
2787 
2788 /* Global Locks */
2789 extern kmp_bootstrap_lock_t __kmp_initz_lock; /* control initialization */
2790 extern kmp_bootstrap_lock_t __kmp_forkjoin_lock; /* control fork/join access */
2791 extern kmp_bootstrap_lock_t
2792     __kmp_exit_lock; /* exit() is not always thread-safe */
2793 #if KMP_USE_MONITOR
2794 extern kmp_bootstrap_lock_t
2795     __kmp_monitor_lock; /* control monitor thread creation */
2796 #endif
2797 extern kmp_bootstrap_lock_t
2798     __kmp_tp_cached_lock; /* used for the hack to allow threadprivate cache and
2799                              __kmp_threads expansion to co-exist */
2800 
2801 extern kmp_lock_t __kmp_global_lock; /* control OS/global access  */
2802 extern kmp_queuing_lock_t __kmp_dispatch_lock; /* control dispatch access  */
2803 extern kmp_lock_t __kmp_debug_lock; /* control I/O access for KMP_DEBUG */
2804 
2805 /* used for yielding spin-waits */
2806 extern unsigned int __kmp_init_wait; /* initial number of spin-tests   */
2807 extern unsigned int __kmp_next_wait; /* susequent number of spin-tests */
2808 
2809 extern enum library_type __kmp_library;
2810 
2811 extern enum sched_type __kmp_sched; /* default runtime scheduling */
2812 extern enum sched_type __kmp_static; /* default static scheduling method */
2813 extern enum sched_type __kmp_guided; /* default guided scheduling method */
2814 extern enum sched_type __kmp_auto; /* default auto scheduling method */
2815 extern int __kmp_chunk; /* default runtime chunk size */
2816 
2817 extern size_t __kmp_stksize; /* stack size per thread         */
2818 #if KMP_USE_MONITOR
2819 extern size_t __kmp_monitor_stksize; /* stack size for monitor thread */
2820 #endif
2821 extern size_t __kmp_stkoffset; /* stack offset per thread       */
2822 extern int __kmp_stkpadding; /* Should we pad root thread(s) stack */
2823 
2824 extern size_t
2825     __kmp_malloc_pool_incr; /* incremental size of pool for kmp_malloc() */
2826 extern int __kmp_env_stksize; /* was KMP_STACKSIZE specified? */
2827 extern int __kmp_env_blocktime; /* was KMP_BLOCKTIME specified? */
2828 extern int __kmp_env_checks; /* was KMP_CHECKS specified?    */
2829 extern int __kmp_env_consistency_check; // was KMP_CONSISTENCY_CHECK specified?
2830 extern int __kmp_generate_warnings; /* should we issue warnings? */
2831 extern int __kmp_reserve_warn; /* have we issued reserve_threads warning? */
2832 
2833 #ifdef DEBUG_SUSPEND
2834 extern int __kmp_suspend_count; /* count inside __kmp_suspend_template() */
2835 #endif
2836 
2837 extern kmp_uint32 __kmp_yield_init;
2838 extern kmp_uint32 __kmp_yield_next;
2839 
2840 #if KMP_USE_MONITOR
2841 extern kmp_uint32 __kmp_yielding_on;
2842 #endif
2843 extern kmp_uint32 __kmp_yield_cycle;
2844 extern kmp_int32 __kmp_yield_on_count;
2845 extern kmp_int32 __kmp_yield_off_count;
2846 
2847 /* ------------------------------------------------------------------------- */
2848 extern int __kmp_allThreadsSpecified;
2849 
2850 extern size_t __kmp_align_alloc;
2851 /* following data protected by initialization routines */
2852 extern int __kmp_xproc; /* number of processors in the system */
2853 extern int __kmp_avail_proc; /* number of processors available to the process */
2854 extern size_t __kmp_sys_min_stksize; /* system-defined minimum stack size */
2855 extern int __kmp_sys_max_nth; /* system-imposed maximum number of threads */
2856 // maximum total number of concurrently-existing threads on device
2857 extern int __kmp_max_nth;
2858 // maximum total number of concurrently-existing threads in a contention group
2859 extern int __kmp_cg_max_nth;
2860 extern int __kmp_teams_max_nth; // max threads used in a teams construct
2861 extern int __kmp_threads_capacity; /* capacity of the arrays __kmp_threads and
2862                                       __kmp_root */
2863 extern int __kmp_dflt_team_nth; /* default number of threads in a parallel
2864                                    region a la OMP_NUM_THREADS */
2865 extern int __kmp_dflt_team_nth_ub; /* upper bound on "" determined at serial
2866                                       initialization */
2867 extern int __kmp_tp_capacity; /* capacity of __kmp_threads if threadprivate is
2868                                  used (fixed) */
2869 extern int __kmp_tp_cached; /* whether threadprivate cache has been created
2870                                (__kmpc_threadprivate_cached()) */
2871 extern int __kmp_dflt_nested; /* nested parallelism enabled by default a la
2872                                  OMP_NESTED */
2873 extern int __kmp_dflt_blocktime; /* number of milliseconds to wait before
2874                                     blocking (env setting) */
2875 #if KMP_USE_MONITOR
2876 extern int
2877     __kmp_monitor_wakeups; /* number of times monitor wakes up per second */
2878 extern int __kmp_bt_intervals; /* number of monitor timestamp intervals before
2879                                   blocking */
2880 #endif
2881 #ifdef KMP_ADJUST_BLOCKTIME
2882 extern int __kmp_zero_bt; /* whether blocktime has been forced to zero */
2883 #endif /* KMP_ADJUST_BLOCKTIME */
2884 #ifdef KMP_DFLT_NTH_CORES
2885 extern int __kmp_ncores; /* Total number of cores for threads placement */
2886 #endif
2887 /* Number of millisecs to delay on abort for Intel(R) VTune(TM) tools */
2888 extern int __kmp_abort_delay;
2889 
2890 extern int __kmp_need_register_atfork_specified;
2891 extern int
2892     __kmp_need_register_atfork; /* At initialization, call pthread_atfork to
2893                                    install fork handler */
2894 extern int __kmp_gtid_mode; /* Method of getting gtid, values:
2895                                0 - not set, will be set at runtime
2896                                1 - using stack search
2897                                2 - dynamic TLS (pthread_getspecific(Linux* OS/OS
2898                                    X*) or TlsGetValue(Windows* OS))
2899                                3 - static TLS (__declspec(thread) __kmp_gtid),
2900                                    Linux* OS .so only.  */
2901 extern int
2902     __kmp_adjust_gtid_mode; /* If true, adjust method based on #threads */
2903 #ifdef KMP_TDATA_GTID
2904 extern KMP_THREAD_LOCAL int __kmp_gtid;
2905 #endif
2906 extern int __kmp_tls_gtid_min; /* #threads below which use sp search for gtid */
2907 extern int __kmp_foreign_tp; // If true, separate TP var for each foreign thread
2908 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
2909 extern int __kmp_inherit_fp_control; // copy fp creg(s) parent->workers at fork
2910 extern kmp_int16 __kmp_init_x87_fpu_control_word; // init thread's FP ctrl reg
2911 extern kmp_uint32 __kmp_init_mxcsr; /* init thread's mxscr */
2912 #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
2913 
2914 extern int __kmp_dflt_max_active_levels; /* max_active_levels for nested
2915                                             parallelism enabled by default via
2916                                             OMP_MAX_ACTIVE_LEVELS */
2917 extern int __kmp_dispatch_num_buffers; /* max possible dynamic loops in
2918                                           concurrent execution per team */
2919 #if KMP_NESTED_HOT_TEAMS
2920 extern int __kmp_hot_teams_mode;
2921 extern int __kmp_hot_teams_max_level;
2922 #endif
2923 
2924 #if KMP_OS_LINUX
2925 extern enum clock_function_type __kmp_clock_function;
2926 extern int __kmp_clock_function_param;
2927 #endif /* KMP_OS_LINUX */
2928 
2929 #if KMP_MIC_SUPPORTED
2930 extern enum mic_type __kmp_mic_type;
2931 #endif
2932 
2933 #ifdef USE_LOAD_BALANCE
2934 extern double __kmp_load_balance_interval; // load balance algorithm interval
2935 #endif /* USE_LOAD_BALANCE */
2936 
2937 // OpenMP 3.1 - Nested num threads array
2938 typedef struct kmp_nested_nthreads_t {
2939   int *nth;
2940   int size;
2941   int used;
2942 } kmp_nested_nthreads_t;
2943 
2944 extern kmp_nested_nthreads_t __kmp_nested_nth;
2945 
2946 #if KMP_USE_ADAPTIVE_LOCKS
2947 
2948 // Parameters for the speculative lock backoff system.
2949 struct kmp_adaptive_backoff_params_t {
2950   // Number of soft retries before it counts as a hard retry.
2951   kmp_uint32 max_soft_retries;
2952   // Badness is a bit mask : 0,1,3,7,15,... on each hard failure we move one to
2953   // the right
2954   kmp_uint32 max_badness;
2955 };
2956 
2957 extern kmp_adaptive_backoff_params_t __kmp_adaptive_backoff_params;
2958 
2959 #if KMP_DEBUG_ADAPTIVE_LOCKS
2960 extern char *__kmp_speculative_statsfile;
2961 #endif
2962 
2963 #endif // KMP_USE_ADAPTIVE_LOCKS
2964 
2965 #if OMP_40_ENABLED
2966 extern int __kmp_display_env; /* TRUE or FALSE */
2967 extern int __kmp_display_env_verbose; /* TRUE if OMP_DISPLAY_ENV=VERBOSE */
2968 extern int __kmp_omp_cancellation; /* TRUE or FALSE */
2969 #endif
2970 
2971 /* ------------------------------------------------------------------------- */
2972 
2973 /* the following are protected by the fork/join lock */
2974 /* write: lock  read: anytime */
2975 extern kmp_info_t **__kmp_threads; /* Descriptors for the threads */
2976 /* read/write: lock */
2977 extern volatile kmp_team_t *__kmp_team_pool;
2978 extern volatile kmp_info_t *__kmp_thread_pool;
2979 
2980 // total num threads reachable from some root thread including all root threads
2981 extern volatile int __kmp_nth;
2982 /* total number of threads reachable from some root thread including all root
2983    threads, and those in the thread pool */
2984 extern volatile int __kmp_all_nth;
2985 extern int __kmp_thread_pool_nth;
2986 extern volatile int __kmp_thread_pool_active_nth;
2987 
2988 extern kmp_root_t **__kmp_root; /* root of thread hierarchy */
2989 /* end data protected by fork/join lock */
2990 /* ------------------------------------------------------------------------- */
2991 
2992 extern kmp_global_t __kmp_global; /* global status */
2993 
2994 extern kmp_info_t __kmp_monitor;
2995 extern volatile kmp_uint32 __kmp_team_counter; // For Debugging Support Library
2996 extern volatile kmp_uint32 __kmp_task_counter; // For Debugging Support Library
2997 
2998 #if USE_DEBUGGER
2999 
3000 #define _KMP_GEN_ID(counter)                                                   \
3001   (__kmp_debugging ? KMP_TEST_THEN_INC32((volatile kmp_int32 *)&counter) + 1   \
3002                    : ~0)
3003 #else
3004 #define _KMP_GEN_ID(counter) (~0)
3005 #endif /* USE_DEBUGGER */
3006 
3007 #define KMP_GEN_TASK_ID() _KMP_GEN_ID(__kmp_task_counter)
3008 #define KMP_GEN_TEAM_ID() _KMP_GEN_ID(__kmp_team_counter)
3009 
3010 /* ------------------------------------------------------------------------ */
3011 
3012 extern void __kmp_print_storage_map_gtid(int gtid, void *p1, void *p2,
3013                                          size_t size, char const *format, ...);
3014 
3015 extern void __kmp_serial_initialize(void);
3016 extern void __kmp_middle_initialize(void);
3017 extern void __kmp_parallel_initialize(void);
3018 
3019 extern void __kmp_internal_begin(void);
3020 extern void __kmp_internal_end_library(int gtid);
3021 extern void __kmp_internal_end_thread(int gtid);
3022 extern void __kmp_internal_end_atexit(void);
3023 extern void __kmp_internal_end_fini(void);
3024 extern void __kmp_internal_end_dtor(void);
3025 extern void __kmp_internal_end_dest(void *);
3026 
3027 extern int __kmp_register_root(int initial_thread);
3028 extern void __kmp_unregister_root(int gtid);
3029 
3030 extern int __kmp_ignore_mppbeg(void);
3031 extern int __kmp_ignore_mppend(void);
3032 
3033 extern int __kmp_enter_single(int gtid, ident_t *id_ref, int push_ws);
3034 extern void __kmp_exit_single(int gtid);
3035 
3036 extern void __kmp_parallel_deo(int *gtid_ref, int *cid_ref, ident_t *loc_ref);
3037 extern void __kmp_parallel_dxo(int *gtid_ref, int *cid_ref, ident_t *loc_ref);
3038 
3039 #ifdef USE_LOAD_BALANCE
3040 extern int __kmp_get_load_balance(int);
3041 #endif
3042 
3043 extern int __kmp_get_global_thread_id(void);
3044 extern int __kmp_get_global_thread_id_reg(void);
3045 extern void __kmp_exit_thread(int exit_status);
3046 extern void __kmp_abort(char const *format, ...);
3047 extern void __kmp_abort_thread(void);
3048 KMP_NORETURN extern void __kmp_abort_process(void);
3049 extern void __kmp_warn(char const *format, ...);
3050 
3051 extern void __kmp_set_num_threads(int new_nth, int gtid);
3052 
3053 // Returns current thread (pointer to kmp_info_t). Current thread *must* be
3054 // registered.
3055 static inline kmp_info_t *__kmp_entry_thread() {
3056   int gtid = __kmp_entry_gtid();
3057 
3058   return __kmp_threads[gtid];
3059 }
3060 
3061 extern void __kmp_set_max_active_levels(int gtid, int new_max_active_levels);
3062 extern int __kmp_get_max_active_levels(int gtid);
3063 extern int __kmp_get_ancestor_thread_num(int gtid, int level);
3064 extern int __kmp_get_team_size(int gtid, int level);
3065 extern void __kmp_set_schedule(int gtid, kmp_sched_t new_sched, int chunk);
3066 extern void __kmp_get_schedule(int gtid, kmp_sched_t *sched, int *chunk);
3067 
3068 extern unsigned short __kmp_get_random(kmp_info_t *thread);
3069 extern void __kmp_init_random(kmp_info_t *thread);
3070 
3071 extern kmp_r_sched_t __kmp_get_schedule_global(void);
3072 extern void __kmp_adjust_num_threads(int new_nproc);
3073 
3074 extern void *___kmp_allocate(size_t size KMP_SRC_LOC_DECL);
3075 extern void *___kmp_page_allocate(size_t size KMP_SRC_LOC_DECL);
3076 extern void ___kmp_free(void *ptr KMP_SRC_LOC_DECL);
3077 #define __kmp_allocate(size) ___kmp_allocate((size)KMP_SRC_LOC_CURR)
3078 #define __kmp_page_allocate(size) ___kmp_page_allocate((size)KMP_SRC_LOC_CURR)
3079 #define __kmp_free(ptr) ___kmp_free((ptr)KMP_SRC_LOC_CURR)
3080 
3081 #if USE_FAST_MEMORY
3082 extern void *___kmp_fast_allocate(kmp_info_t *this_thr,
3083                                   size_t size KMP_SRC_LOC_DECL);
3084 extern void ___kmp_fast_free(kmp_info_t *this_thr, void *ptr KMP_SRC_LOC_DECL);
3085 extern void __kmp_free_fast_memory(kmp_info_t *this_thr);
3086 extern void __kmp_initialize_fast_memory(kmp_info_t *this_thr);
3087 #define __kmp_fast_allocate(this_thr, size)                                    \
3088   ___kmp_fast_allocate((this_thr), (size)KMP_SRC_LOC_CURR)
3089 #define __kmp_fast_free(this_thr, ptr)                                         \
3090   ___kmp_fast_free((this_thr), (ptr)KMP_SRC_LOC_CURR)
3091 #endif
3092 
3093 extern void *___kmp_thread_malloc(kmp_info_t *th, size_t size KMP_SRC_LOC_DECL);
3094 extern void *___kmp_thread_calloc(kmp_info_t *th, size_t nelem,
3095                                   size_t elsize KMP_SRC_LOC_DECL);
3096 extern void *___kmp_thread_realloc(kmp_info_t *th, void *ptr,
3097                                    size_t size KMP_SRC_LOC_DECL);
3098 extern void ___kmp_thread_free(kmp_info_t *th, void *ptr KMP_SRC_LOC_DECL);
3099 #define __kmp_thread_malloc(th, size)                                          \
3100   ___kmp_thread_malloc((th), (size)KMP_SRC_LOC_CURR)
3101 #define __kmp_thread_calloc(th, nelem, elsize)                                 \
3102   ___kmp_thread_calloc((th), (nelem), (elsize)KMP_SRC_LOC_CURR)
3103 #define __kmp_thread_realloc(th, ptr, size)                                    \
3104   ___kmp_thread_realloc((th), (ptr), (size)KMP_SRC_LOC_CURR)
3105 #define __kmp_thread_free(th, ptr)                                             \
3106   ___kmp_thread_free((th), (ptr)KMP_SRC_LOC_CURR)
3107 
3108 #define KMP_INTERNAL_MALLOC(sz) malloc(sz)
3109 #define KMP_INTERNAL_FREE(p) free(p)
3110 #define KMP_INTERNAL_REALLOC(p, sz) realloc((p), (sz))
3111 #define KMP_INTERNAL_CALLOC(n, sz) calloc((n), (sz))
3112 
3113 extern void __kmp_push_num_threads(ident_t *loc, int gtid, int num_threads);
3114 
3115 #if OMP_40_ENABLED
3116 extern void __kmp_push_proc_bind(ident_t *loc, int gtid,
3117                                  kmp_proc_bind_t proc_bind);
3118 extern void __kmp_push_num_teams(ident_t *loc, int gtid, int num_teams,
3119                                  int num_threads);
3120 #endif
3121 
3122 extern void __kmp_yield(int cond);
3123 
3124 extern void __kmpc_dispatch_init_4(ident_t *loc, kmp_int32 gtid,
3125                                    enum sched_type schedule, kmp_int32 lb,
3126                                    kmp_int32 ub, kmp_int32 st, kmp_int32 chunk);
3127 extern void __kmpc_dispatch_init_4u(ident_t *loc, kmp_int32 gtid,
3128                                     enum sched_type schedule, kmp_uint32 lb,
3129                                     kmp_uint32 ub, kmp_int32 st,
3130                                     kmp_int32 chunk);
3131 extern void __kmpc_dispatch_init_8(ident_t *loc, kmp_int32 gtid,
3132                                    enum sched_type schedule, kmp_int64 lb,
3133                                    kmp_int64 ub, kmp_int64 st, kmp_int64 chunk);
3134 extern void __kmpc_dispatch_init_8u(ident_t *loc, kmp_int32 gtid,
3135                                     enum sched_type schedule, kmp_uint64 lb,
3136                                     kmp_uint64 ub, kmp_int64 st,
3137                                     kmp_int64 chunk);
3138 
3139 extern int __kmpc_dispatch_next_4(ident_t *loc, kmp_int32 gtid,
3140                                   kmp_int32 *p_last, kmp_int32 *p_lb,
3141                                   kmp_int32 *p_ub, kmp_int32 *p_st);
3142 extern int __kmpc_dispatch_next_4u(ident_t *loc, kmp_int32 gtid,
3143                                    kmp_int32 *p_last, kmp_uint32 *p_lb,
3144                                    kmp_uint32 *p_ub, kmp_int32 *p_st);
3145 extern int __kmpc_dispatch_next_8(ident_t *loc, kmp_int32 gtid,
3146                                   kmp_int32 *p_last, kmp_int64 *p_lb,
3147                                   kmp_int64 *p_ub, kmp_int64 *p_st);
3148 extern int __kmpc_dispatch_next_8u(ident_t *loc, kmp_int32 gtid,
3149                                    kmp_int32 *p_last, kmp_uint64 *p_lb,
3150                                    kmp_uint64 *p_ub, kmp_int64 *p_st);
3151 
3152 extern void __kmpc_dispatch_fini_4(ident_t *loc, kmp_int32 gtid);
3153 extern void __kmpc_dispatch_fini_8(ident_t *loc, kmp_int32 gtid);
3154 extern void __kmpc_dispatch_fini_4u(ident_t *loc, kmp_int32 gtid);
3155 extern void __kmpc_dispatch_fini_8u(ident_t *loc, kmp_int32 gtid);
3156 
3157 #ifdef KMP_GOMP_COMPAT
3158 
3159 extern void __kmp_aux_dispatch_init_4(ident_t *loc, kmp_int32 gtid,
3160                                       enum sched_type schedule, kmp_int32 lb,
3161                                       kmp_int32 ub, kmp_int32 st,
3162                                       kmp_int32 chunk, int push_ws);
3163 extern void __kmp_aux_dispatch_init_4u(ident_t *loc, kmp_int32 gtid,
3164                                        enum sched_type schedule, kmp_uint32 lb,
3165                                        kmp_uint32 ub, kmp_int32 st,
3166                                        kmp_int32 chunk, int push_ws);
3167 extern void __kmp_aux_dispatch_init_8(ident_t *loc, kmp_int32 gtid,
3168                                       enum sched_type schedule, kmp_int64 lb,
3169                                       kmp_int64 ub, kmp_int64 st,
3170                                       kmp_int64 chunk, int push_ws);
3171 extern void __kmp_aux_dispatch_init_8u(ident_t *loc, kmp_int32 gtid,
3172                                        enum sched_type schedule, kmp_uint64 lb,
3173                                        kmp_uint64 ub, kmp_int64 st,
3174                                        kmp_int64 chunk, int push_ws);
3175 extern void __kmp_aux_dispatch_fini_chunk_4(ident_t *loc, kmp_int32 gtid);
3176 extern void __kmp_aux_dispatch_fini_chunk_8(ident_t *loc, kmp_int32 gtid);
3177 extern void __kmp_aux_dispatch_fini_chunk_4u(ident_t *loc, kmp_int32 gtid);
3178 extern void __kmp_aux_dispatch_fini_chunk_8u(ident_t *loc, kmp_int32 gtid);
3179 
3180 #endif /* KMP_GOMP_COMPAT */
3181 
3182 extern kmp_uint32 __kmp_eq_4(kmp_uint32 value, kmp_uint32 checker);
3183 extern kmp_uint32 __kmp_neq_4(kmp_uint32 value, kmp_uint32 checker);
3184 extern kmp_uint32 __kmp_lt_4(kmp_uint32 value, kmp_uint32 checker);
3185 extern kmp_uint32 __kmp_ge_4(kmp_uint32 value, kmp_uint32 checker);
3186 extern kmp_uint32 __kmp_le_4(kmp_uint32 value, kmp_uint32 checker);
3187 extern kmp_uint32 __kmp_wait_yield_4(kmp_uint32 volatile *spinner,
3188                                      kmp_uint32 checker,
3189                                      kmp_uint32 (*pred)(kmp_uint32, kmp_uint32),
3190                                      void *obj);
3191 extern void __kmp_wait_yield_4_ptr(void *spinner, kmp_uint32 checker,
3192                                    kmp_uint32 (*pred)(void *, kmp_uint32),
3193                                    void *obj);
3194 
3195 class kmp_flag_32;
3196 class kmp_flag_64;
3197 class kmp_flag_oncore;
3198 extern void __kmp_wait_64(kmp_info_t *this_thr, kmp_flag_64 *flag,
3199                           int final_spin
3200 #if USE_ITT_BUILD
3201                           ,
3202                           void *itt_sync_obj
3203 #endif
3204                           );
3205 extern void __kmp_release_64(kmp_flag_64 *flag);
3206 
3207 extern void __kmp_infinite_loop(void);
3208 
3209 extern void __kmp_cleanup(void);
3210 
3211 #if KMP_HANDLE_SIGNALS
3212 extern int __kmp_handle_signals;
3213 extern void __kmp_install_signals(int parallel_init);
3214 extern void __kmp_remove_signals(void);
3215 #endif
3216 
3217 extern void __kmp_clear_system_time(void);
3218 extern void __kmp_read_system_time(double *delta);
3219 
3220 extern void __kmp_check_stack_overlap(kmp_info_t *thr);
3221 
3222 extern void __kmp_expand_host_name(char *buffer, size_t size);
3223 extern void __kmp_expand_file_name(char *result, size_t rlen, char *pattern);
3224 
3225 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
3226 extern void
3227 __kmp_initialize_system_tick(void); /* Initialize timer tick value */
3228 #endif
3229 
3230 extern void
3231 __kmp_runtime_initialize(void); /* machine specific initialization */
3232 extern void __kmp_runtime_destroy(void);
3233 
3234 #if KMP_AFFINITY_SUPPORTED
3235 extern char *__kmp_affinity_print_mask(char *buf, int buf_len,
3236                                        kmp_affin_mask_t *mask);
3237 extern void __kmp_affinity_initialize(void);
3238 extern void __kmp_affinity_uninitialize(void);
3239 extern void __kmp_affinity_set_init_mask(
3240     int gtid, int isa_root); /* set affinity according to KMP_AFFINITY */
3241 #if OMP_40_ENABLED
3242 extern void __kmp_affinity_set_place(int gtid);
3243 #endif
3244 extern void __kmp_affinity_determine_capable(const char *env_var);
3245 extern int __kmp_aux_set_affinity(void **mask);
3246 extern int __kmp_aux_get_affinity(void **mask);
3247 extern int __kmp_aux_get_affinity_max_proc();
3248 extern int __kmp_aux_set_affinity_mask_proc(int proc, void **mask);
3249 extern int __kmp_aux_unset_affinity_mask_proc(int proc, void **mask);
3250 extern int __kmp_aux_get_affinity_mask_proc(int proc, void **mask);
3251 extern void __kmp_balanced_affinity(int tid, int team_size);
3252 #if KMP_OS_LINUX
3253 extern int kmp_set_thread_affinity_mask_initial(void);
3254 #endif
3255 #endif /* KMP_AFFINITY_SUPPORTED */
3256 
3257 extern void __kmp_cleanup_hierarchy();
3258 extern void __kmp_get_hierarchy(kmp_uint32 nproc, kmp_bstate_t *thr_bar);
3259 
3260 #if KMP_USE_FUTEX
3261 
3262 extern int __kmp_futex_determine_capable(void);
3263 
3264 #endif // KMP_USE_FUTEX
3265 
3266 extern void __kmp_gtid_set_specific(int gtid);
3267 extern int __kmp_gtid_get_specific(void);
3268 
3269 extern double __kmp_read_cpu_time(void);
3270 
3271 extern int __kmp_read_system_info(struct kmp_sys_info *info);
3272 
3273 #if KMP_USE_MONITOR
3274 extern void __kmp_create_monitor(kmp_info_t *th);
3275 #endif
3276 
3277 extern void *__kmp_launch_thread(kmp_info_t *thr);
3278 
3279 extern void __kmp_create_worker(int gtid, kmp_info_t *th, size_t stack_size);
3280 
3281 #if KMP_OS_WINDOWS
3282 extern int __kmp_still_running(kmp_info_t *th);
3283 extern int __kmp_is_thread_alive(kmp_info_t *th, DWORD *exit_val);
3284 extern void __kmp_free_handle(kmp_thread_t tHandle);
3285 #endif
3286 
3287 #if KMP_USE_MONITOR
3288 extern void __kmp_reap_monitor(kmp_info_t *th);
3289 #endif
3290 extern void __kmp_reap_worker(kmp_info_t *th);
3291 extern void __kmp_terminate_thread(int gtid);
3292 
3293 extern void __kmp_suspend_32(int th_gtid, kmp_flag_32 *flag);
3294 extern void __kmp_suspend_64(int th_gtid, kmp_flag_64 *flag);
3295 extern void __kmp_suspend_oncore(int th_gtid, kmp_flag_oncore *flag);
3296 extern void __kmp_resume_32(int target_gtid, kmp_flag_32 *flag);
3297 extern void __kmp_resume_64(int target_gtid, kmp_flag_64 *flag);
3298 extern void __kmp_resume_oncore(int target_gtid, kmp_flag_oncore *flag);
3299 
3300 extern void __kmp_elapsed(double *);
3301 extern void __kmp_elapsed_tick(double *);
3302 
3303 extern void __kmp_enable(int old_state);
3304 extern void __kmp_disable(int *old_state);
3305 
3306 extern void __kmp_thread_sleep(int millis);
3307 
3308 extern void __kmp_common_initialize(void);
3309 extern void __kmp_common_destroy(void);
3310 extern void __kmp_common_destroy_gtid(int gtid);
3311 
3312 #if KMP_OS_UNIX
3313 extern void __kmp_register_atfork(void);
3314 #endif
3315 extern void __kmp_suspend_initialize(void);
3316 extern void __kmp_suspend_uninitialize_thread(kmp_info_t *th);
3317 
3318 extern kmp_info_t *__kmp_allocate_thread(kmp_root_t *root, kmp_team_t *team,
3319                                          int tid);
3320 #if OMP_40_ENABLED
3321 extern kmp_team_t *
3322 __kmp_allocate_team(kmp_root_t *root, int new_nproc, int max_nproc,
3323 #if OMPT_SUPPORT
3324                     ompt_data_t ompt_parallel_data,
3325 #endif
3326                     kmp_proc_bind_t proc_bind, kmp_internal_control_t *new_icvs,
3327                     int argc USE_NESTED_HOT_ARG(kmp_info_t *thr));
3328 #else
3329 extern kmp_team_t *
3330 __kmp_allocate_team(kmp_root_t *root, int new_nproc, int max_nproc,
3331 #if OMPT_SUPPORT
3332                     ompt_id_t ompt_parallel_id,
3333 #endif
3334                     kmp_internal_control_t *new_icvs,
3335                     int argc USE_NESTED_HOT_ARG(kmp_info_t *thr));
3336 #endif // OMP_40_ENABLED
3337 extern void __kmp_free_thread(kmp_info_t *);
3338 extern void __kmp_free_team(kmp_root_t *,
3339                             kmp_team_t *USE_NESTED_HOT_ARG(kmp_info_t *));
3340 extern kmp_team_t *__kmp_reap_team(kmp_team_t *);
3341 
3342 /* ------------------------------------------------------------------------ */
3343 
3344 extern void __kmp_initialize_bget(kmp_info_t *th);
3345 extern void __kmp_finalize_bget(kmp_info_t *th);
3346 
3347 KMP_EXPORT void *kmpc_malloc(size_t size);
3348 KMP_EXPORT void *kmpc_aligned_malloc(size_t size, size_t alignment);
3349 KMP_EXPORT void *kmpc_calloc(size_t nelem, size_t elsize);
3350 KMP_EXPORT void *kmpc_realloc(void *ptr, size_t size);
3351 KMP_EXPORT void kmpc_free(void *ptr);
3352 
3353 /* declarations for internal use */
3354 
3355 extern int __kmp_barrier(enum barrier_type bt, int gtid, int is_split,
3356                          size_t reduce_size, void *reduce_data,
3357                          void (*reduce)(void *, void *));
3358 extern void __kmp_end_split_barrier(enum barrier_type bt, int gtid);
3359 
3360 /*!
3361  * Tell the fork call which compiler generated the fork call, and therefore how
3362  * to deal with the call.
3363  */
3364 enum fork_context_e {
3365   fork_context_gnu, /**< Called from GNU generated code, so must not invoke the
3366                        microtask internally. */
3367   fork_context_intel, /**< Called from Intel generated code.  */
3368   fork_context_last
3369 };
3370 extern int __kmp_fork_call(ident_t *loc, int gtid,
3371                            enum fork_context_e fork_context, kmp_int32 argc,
3372                            microtask_t microtask, launch_t invoker,
3373 /* TODO: revert workaround for Intel(R) 64 tracker #96 */
3374 #if (KMP_ARCH_ARM || KMP_ARCH_X86_64 || KMP_ARCH_AARCH64) && KMP_OS_LINUX
3375                            va_list *ap
3376 #else
3377                            va_list ap
3378 #endif
3379                            );
3380 
3381 extern void __kmp_join_call(ident_t *loc, int gtid
3382 #if OMPT_SUPPORT
3383                             ,
3384                             enum fork_context_e fork_context
3385 #endif
3386 #if OMP_40_ENABLED
3387                             ,
3388                             int exit_teams = 0
3389 #endif
3390                             );
3391 
3392 extern void __kmp_serialized_parallel(ident_t *id, kmp_int32 gtid);
3393 extern void __kmp_internal_fork(ident_t *id, int gtid, kmp_team_t *team);
3394 extern void __kmp_internal_join(ident_t *id, int gtid, kmp_team_t *team);
3395 extern int __kmp_invoke_task_func(int gtid);
3396 extern void __kmp_run_before_invoked_task(int gtid, int tid,
3397                                           kmp_info_t *this_thr,
3398                                           kmp_team_t *team);
3399 extern void __kmp_run_after_invoked_task(int gtid, int tid,
3400                                          kmp_info_t *this_thr,
3401                                          kmp_team_t *team);
3402 
3403 // should never have been exported
3404 KMP_EXPORT int __kmpc_invoke_task_func(int gtid);
3405 #if OMP_40_ENABLED
3406 extern int __kmp_invoke_teams_master(int gtid);
3407 extern void __kmp_teams_master(int gtid);
3408 #endif
3409 extern void __kmp_save_internal_controls(kmp_info_t *thread);
3410 extern void __kmp_user_set_library(enum library_type arg);
3411 extern void __kmp_aux_set_library(enum library_type arg);
3412 extern void __kmp_aux_set_stacksize(size_t arg);
3413 extern void __kmp_aux_set_blocktime(int arg, kmp_info_t *thread, int tid);
3414 extern void __kmp_aux_set_defaults(char const *str, int len);
3415 
3416 /* Functions called from __kmp_aux_env_initialize() in kmp_settings.cpp */
3417 void kmpc_set_blocktime(int arg);
3418 void ompc_set_nested(int flag);
3419 void ompc_set_dynamic(int flag);
3420 void ompc_set_num_threads(int arg);
3421 
3422 extern void __kmp_push_current_task_to_thread(kmp_info_t *this_thr,
3423                                               kmp_team_t *team, int tid);
3424 extern void __kmp_pop_current_task_from_thread(kmp_info_t *this_thr);
3425 extern kmp_task_t *__kmp_task_alloc(ident_t *loc_ref, kmp_int32 gtid,
3426                                     kmp_tasking_flags_t *flags,
3427                                     size_t sizeof_kmp_task_t,
3428                                     size_t sizeof_shareds,
3429                                     kmp_routine_entry_t task_entry);
3430 extern void __kmp_init_implicit_task(ident_t *loc_ref, kmp_info_t *this_thr,
3431                                      kmp_team_t *team, int tid,
3432                                      int set_curr_task);
3433 extern void __kmp_finish_implicit_task(kmp_info_t *this_thr);
3434 extern void __kmp_free_implicit_task(kmp_info_t *this_thr);
3435 int __kmp_execute_tasks_32(kmp_info_t *thread, kmp_int32 gtid,
3436                            kmp_flag_32 *flag, int final_spin,
3437                            int *thread_finished,
3438 #if USE_ITT_BUILD
3439                            void *itt_sync_obj,
3440 #endif /* USE_ITT_BUILD */
3441                            kmp_int32 is_constrained);
3442 int __kmp_execute_tasks_64(kmp_info_t *thread, kmp_int32 gtid,
3443                            kmp_flag_64 *flag, int final_spin,
3444                            int *thread_finished,
3445 #if USE_ITT_BUILD
3446                            void *itt_sync_obj,
3447 #endif /* USE_ITT_BUILD */
3448                            kmp_int32 is_constrained);
3449 int __kmp_execute_tasks_oncore(kmp_info_t *thread, kmp_int32 gtid,
3450                                kmp_flag_oncore *flag, int final_spin,
3451                                int *thread_finished,
3452 #if USE_ITT_BUILD
3453                                void *itt_sync_obj,
3454 #endif /* USE_ITT_BUILD */
3455                                kmp_int32 is_constrained);
3456 
3457 extern void __kmp_free_task_team(kmp_info_t *thread,
3458                                  kmp_task_team_t *task_team);
3459 extern void __kmp_reap_task_teams(void);
3460 extern void __kmp_wait_to_unref_task_teams(void);
3461 extern void __kmp_task_team_setup(kmp_info_t *this_thr, kmp_team_t *team,
3462                                   int always);
3463 extern void __kmp_task_team_sync(kmp_info_t *this_thr, kmp_team_t *team);
3464 extern void __kmp_task_team_wait(kmp_info_t *this_thr, kmp_team_t *team
3465 #if USE_ITT_BUILD
3466                                  ,
3467                                  void *itt_sync_obj
3468 #endif /* USE_ITT_BUILD */
3469                                  ,
3470                                  int wait = 1);
3471 extern void __kmp_tasking_barrier(kmp_team_t *team, kmp_info_t *thread,
3472                                   int gtid);
3473 
3474 extern int __kmp_is_address_mapped(void *addr);
3475 extern kmp_uint64 __kmp_hardware_timestamp(void);
3476 
3477 #if KMP_OS_UNIX
3478 extern int __kmp_read_from_file(char const *path, char const *format, ...);
3479 #endif
3480 
3481 /* ------------------------------------------------------------------------ */
3482 //
3483 // Assembly routines that have no compiler intrinsic replacement
3484 //
3485 
3486 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
3487 
3488 extern void __kmp_query_cpuid(kmp_cpuinfo_t *p);
3489 
3490 #define __kmp_load_mxcsr(p) _mm_setcsr(*(p))
3491 static inline void __kmp_store_mxcsr(kmp_uint32 *p) { *p = _mm_getcsr(); }
3492 
3493 extern void __kmp_load_x87_fpu_control_word(kmp_int16 *p);
3494 extern void __kmp_store_x87_fpu_control_word(kmp_int16 *p);
3495 extern void __kmp_clear_x87_fpu_status_word();
3496 #define KMP_X86_MXCSR_MASK 0xffffffc0 /* ignore status flags (6 lsb) */
3497 
3498 #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
3499 
3500 extern int __kmp_invoke_microtask(microtask_t pkfn, int gtid, int npr, int argc,
3501                                   void *argv[]
3502 #if OMPT_SUPPORT
3503                                   ,
3504                                   void **exit_frame_ptr
3505 #endif
3506                                   );
3507 
3508 /* ------------------------------------------------------------------------ */
3509 
3510 KMP_EXPORT void __kmpc_begin(ident_t *, kmp_int32 flags);
3511 KMP_EXPORT void __kmpc_end(ident_t *);
3512 
3513 KMP_EXPORT void __kmpc_threadprivate_register_vec(ident_t *, void *data,
3514                                                   kmpc_ctor_vec ctor,
3515                                                   kmpc_cctor_vec cctor,
3516                                                   kmpc_dtor_vec dtor,
3517                                                   size_t vector_length);
3518 KMP_EXPORT void __kmpc_threadprivate_register(ident_t *, void *data,
3519                                               kmpc_ctor ctor, kmpc_cctor cctor,
3520                                               kmpc_dtor dtor);
3521 KMP_EXPORT void *__kmpc_threadprivate(ident_t *, kmp_int32 global_tid,
3522                                       void *data, size_t size);
3523 
3524 KMP_EXPORT kmp_int32 __kmpc_global_thread_num(ident_t *);
3525 KMP_EXPORT kmp_int32 __kmpc_global_num_threads(ident_t *);
3526 KMP_EXPORT kmp_int32 __kmpc_bound_thread_num(ident_t *);
3527 KMP_EXPORT kmp_int32 __kmpc_bound_num_threads(ident_t *);
3528 
3529 KMP_EXPORT kmp_int32 __kmpc_ok_to_fork(ident_t *);
3530 KMP_EXPORT void __kmpc_fork_call(ident_t *, kmp_int32 nargs,
3531                                  kmpc_micro microtask, ...);
3532 
3533 KMP_EXPORT void __kmpc_serialized_parallel(ident_t *, kmp_int32 global_tid);
3534 KMP_EXPORT void __kmpc_end_serialized_parallel(ident_t *, kmp_int32 global_tid);
3535 
3536 KMP_EXPORT void __kmpc_flush(ident_t *);
3537 KMP_EXPORT void __kmpc_barrier(ident_t *, kmp_int32 global_tid);
3538 KMP_EXPORT kmp_int32 __kmpc_master(ident_t *, kmp_int32 global_tid);
3539 KMP_EXPORT void __kmpc_end_master(ident_t *, kmp_int32 global_tid);
3540 KMP_EXPORT void __kmpc_ordered(ident_t *, kmp_int32 global_tid);
3541 KMP_EXPORT void __kmpc_end_ordered(ident_t *, kmp_int32 global_tid);
3542 KMP_EXPORT void __kmpc_critical(ident_t *, kmp_int32 global_tid,
3543                                 kmp_critical_name *);
3544 KMP_EXPORT void __kmpc_end_critical(ident_t *, kmp_int32 global_tid,
3545                                     kmp_critical_name *);
3546 
3547 #if OMP_45_ENABLED
3548 KMP_EXPORT void __kmpc_critical_with_hint(ident_t *, kmp_int32 global_tid,
3549                                           kmp_critical_name *, uintptr_t hint);
3550 #endif
3551 
3552 KMP_EXPORT kmp_int32 __kmpc_barrier_master(ident_t *, kmp_int32 global_tid);
3553 KMP_EXPORT void __kmpc_end_barrier_master(ident_t *, kmp_int32 global_tid);
3554 
3555 KMP_EXPORT kmp_int32 __kmpc_barrier_master_nowait(ident_t *,
3556                                                   kmp_int32 global_tid);
3557 
3558 KMP_EXPORT kmp_int32 __kmpc_single(ident_t *, kmp_int32 global_tid);
3559 KMP_EXPORT void __kmpc_end_single(ident_t *, kmp_int32 global_tid);
3560 
3561 KMP_EXPORT void KMPC_FOR_STATIC_INIT(ident_t *loc, kmp_int32 global_tid,
3562                                      kmp_int32 schedtype, kmp_int32 *plastiter,
3563                                      kmp_int *plower, kmp_int *pupper,
3564                                      kmp_int *pstride, kmp_int incr,
3565                                      kmp_int chunk);
3566 
3567 KMP_EXPORT void __kmpc_for_static_fini(ident_t *loc, kmp_int32 global_tid);
3568 
3569 KMP_EXPORT void __kmpc_copyprivate(ident_t *loc, kmp_int32 global_tid,
3570                                    size_t cpy_size, void *cpy_data,
3571                                    void (*cpy_func)(void *, void *),
3572                                    kmp_int32 didit);
3573 
3574 extern void KMPC_SET_NUM_THREADS(int arg);
3575 extern void KMPC_SET_DYNAMIC(int flag);
3576 extern void KMPC_SET_NESTED(int flag);
3577 
3578 /* Taskq interface routines */
3579 KMP_EXPORT kmpc_thunk_t *__kmpc_taskq(ident_t *loc, kmp_int32 global_tid,
3580                                       kmpc_task_t taskq_task,
3581                                       size_t sizeof_thunk,
3582                                       size_t sizeof_shareds, kmp_int32 flags,
3583                                       kmpc_shared_vars_t **shareds);
3584 KMP_EXPORT void __kmpc_end_taskq(ident_t *loc, kmp_int32 global_tid,
3585                                  kmpc_thunk_t *thunk);
3586 KMP_EXPORT kmp_int32 __kmpc_task(ident_t *loc, kmp_int32 global_tid,
3587                                  kmpc_thunk_t *thunk);
3588 KMP_EXPORT void __kmpc_taskq_task(ident_t *loc, kmp_int32 global_tid,
3589                                   kmpc_thunk_t *thunk, kmp_int32 status);
3590 KMP_EXPORT void __kmpc_end_taskq_task(ident_t *loc, kmp_int32 global_tid,
3591                                       kmpc_thunk_t *thunk);
3592 KMP_EXPORT kmpc_thunk_t *__kmpc_task_buffer(ident_t *loc, kmp_int32 global_tid,
3593                                             kmpc_thunk_t *taskq_thunk,
3594                                             kmpc_task_t task);
3595 
3596 /* OMP 3.0 tasking interface routines */
3597 KMP_EXPORT kmp_int32 __kmpc_omp_task(ident_t *loc_ref, kmp_int32 gtid,
3598                                      kmp_task_t *new_task);
3599 KMP_EXPORT kmp_task_t *__kmpc_omp_task_alloc(ident_t *loc_ref, kmp_int32 gtid,
3600                                              kmp_int32 flags,
3601                                              size_t sizeof_kmp_task_t,
3602                                              size_t sizeof_shareds,
3603                                              kmp_routine_entry_t task_entry);
3604 KMP_EXPORT void __kmpc_omp_task_begin_if0(ident_t *loc_ref, kmp_int32 gtid,
3605                                           kmp_task_t *task);
3606 KMP_EXPORT void __kmpc_omp_task_complete_if0(ident_t *loc_ref, kmp_int32 gtid,
3607                                              kmp_task_t *task);
3608 KMP_EXPORT kmp_int32 __kmpc_omp_task_parts(ident_t *loc_ref, kmp_int32 gtid,
3609                                            kmp_task_t *new_task);
3610 KMP_EXPORT kmp_int32 __kmpc_omp_taskwait(ident_t *loc_ref, kmp_int32 gtid);
3611 
3612 KMP_EXPORT kmp_int32 __kmpc_omp_taskyield(ident_t *loc_ref, kmp_int32 gtid,
3613                                           int end_part);
3614 
3615 #if TASK_UNUSED
3616 void __kmpc_omp_task_begin(ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *task);
3617 void __kmpc_omp_task_complete(ident_t *loc_ref, kmp_int32 gtid,
3618                               kmp_task_t *task);
3619 #endif // TASK_UNUSED
3620 
3621 /* ------------------------------------------------------------------------ */
3622 
3623 #if OMP_40_ENABLED
3624 
3625 KMP_EXPORT void __kmpc_taskgroup(ident_t *loc, int gtid);
3626 KMP_EXPORT void __kmpc_end_taskgroup(ident_t *loc, int gtid);
3627 
3628 KMP_EXPORT kmp_int32 __kmpc_omp_task_with_deps(
3629     ident_t *loc_ref, kmp_int32 gtid, kmp_task_t *new_task, kmp_int32 ndeps,
3630     kmp_depend_info_t *dep_list, kmp_int32 ndeps_noalias,
3631     kmp_depend_info_t *noalias_dep_list);
3632 KMP_EXPORT void __kmpc_omp_wait_deps(ident_t *loc_ref, kmp_int32 gtid,
3633                                      kmp_int32 ndeps,
3634                                      kmp_depend_info_t *dep_list,
3635                                      kmp_int32 ndeps_noalias,
3636                                      kmp_depend_info_t *noalias_dep_list);
3637 extern void __kmp_release_deps(kmp_int32 gtid, kmp_taskdata_t *task);
3638 extern void __kmp_dephash_free_entries(kmp_info_t *thread, kmp_dephash_t *h);
3639 extern void __kmp_dephash_free(kmp_info_t *thread, kmp_dephash_t *h);
3640 
3641 extern kmp_int32 __kmp_omp_task(kmp_int32 gtid, kmp_task_t *new_task,
3642                                 bool serialize_immediate);
3643 
3644 KMP_EXPORT kmp_int32 __kmpc_cancel(ident_t *loc_ref, kmp_int32 gtid,
3645                                    kmp_int32 cncl_kind);
3646 KMP_EXPORT kmp_int32 __kmpc_cancellationpoint(ident_t *loc_ref, kmp_int32 gtid,
3647                                               kmp_int32 cncl_kind);
3648 KMP_EXPORT kmp_int32 __kmpc_cancel_barrier(ident_t *loc_ref, kmp_int32 gtid);
3649 KMP_EXPORT int __kmp_get_cancellation_status(int cancel_kind);
3650 
3651 #if OMP_45_ENABLED
3652 
3653 KMP_EXPORT void __kmpc_proxy_task_completed(kmp_int32 gtid, kmp_task_t *ptask);
3654 KMP_EXPORT void __kmpc_proxy_task_completed_ooo(kmp_task_t *ptask);
3655 KMP_EXPORT void __kmpc_taskloop(ident_t *loc, kmp_int32 gtid, kmp_task_t *task,
3656                                 kmp_int32 if_val, kmp_uint64 *lb,
3657                                 kmp_uint64 *ub, kmp_int64 st, kmp_int32 nogroup,
3658                                 kmp_int32 sched, kmp_uint64 grainsize,
3659                                 void *task_dup);
3660 #endif
3661 // TODO: change to OMP_50_ENABLED, need to change build tools for this to work
3662 #if OMP_45_ENABLED
3663 KMP_EXPORT void *__kmpc_task_reduction_init(int gtid, int num_data, void *data);
3664 KMP_EXPORT void *__kmpc_task_reduction_get_th_data(int gtid, void *tg, void *d);
3665 #endif
3666 
3667 #endif
3668 
3669 /* Lock interface routines (fast versions with gtid passed in) */
3670 KMP_EXPORT void __kmpc_init_lock(ident_t *loc, kmp_int32 gtid,
3671                                  void **user_lock);
3672 KMP_EXPORT void __kmpc_init_nest_lock(ident_t *loc, kmp_int32 gtid,
3673                                       void **user_lock);
3674 KMP_EXPORT void __kmpc_destroy_lock(ident_t *loc, kmp_int32 gtid,
3675                                     void **user_lock);
3676 KMP_EXPORT void __kmpc_destroy_nest_lock(ident_t *loc, kmp_int32 gtid,
3677                                          void **user_lock);
3678 KMP_EXPORT void __kmpc_set_lock(ident_t *loc, kmp_int32 gtid, void **user_lock);
3679 KMP_EXPORT void __kmpc_set_nest_lock(ident_t *loc, kmp_int32 gtid,
3680                                      void **user_lock);
3681 KMP_EXPORT void __kmpc_unset_lock(ident_t *loc, kmp_int32 gtid,
3682                                   void **user_lock);
3683 KMP_EXPORT void __kmpc_unset_nest_lock(ident_t *loc, kmp_int32 gtid,
3684                                        void **user_lock);
3685 KMP_EXPORT int __kmpc_test_lock(ident_t *loc, kmp_int32 gtid, void **user_lock);
3686 KMP_EXPORT int __kmpc_test_nest_lock(ident_t *loc, kmp_int32 gtid,
3687                                      void **user_lock);
3688 
3689 #if OMP_45_ENABLED
3690 KMP_EXPORT void __kmpc_init_lock_with_hint(ident_t *loc, kmp_int32 gtid,
3691                                            void **user_lock, uintptr_t hint);
3692 KMP_EXPORT void __kmpc_init_nest_lock_with_hint(ident_t *loc, kmp_int32 gtid,
3693                                                 void **user_lock,
3694                                                 uintptr_t hint);
3695 #endif
3696 
3697 /* Interface to fast scalable reduce methods routines */
3698 
3699 KMP_EXPORT kmp_int32 __kmpc_reduce_nowait(
3700     ident_t *loc, kmp_int32 global_tid, kmp_int32 num_vars, size_t reduce_size,
3701     void *reduce_data, void (*reduce_func)(void *lhs_data, void *rhs_data),
3702     kmp_critical_name *lck);
3703 KMP_EXPORT void __kmpc_end_reduce_nowait(ident_t *loc, kmp_int32 global_tid,
3704                                          kmp_critical_name *lck);
3705 KMP_EXPORT kmp_int32 __kmpc_reduce(
3706     ident_t *loc, kmp_int32 global_tid, kmp_int32 num_vars, size_t reduce_size,
3707     void *reduce_data, void (*reduce_func)(void *lhs_data, void *rhs_data),
3708     kmp_critical_name *lck);
3709 KMP_EXPORT void __kmpc_end_reduce(ident_t *loc, kmp_int32 global_tid,
3710                                   kmp_critical_name *lck);
3711 
3712 /* Internal fast reduction routines */
3713 
3714 extern PACKED_REDUCTION_METHOD_T __kmp_determine_reduction_method(
3715     ident_t *loc, kmp_int32 global_tid, kmp_int32 num_vars, size_t reduce_size,
3716     void *reduce_data, void (*reduce_func)(void *lhs_data, void *rhs_data),
3717     kmp_critical_name *lck);
3718 
3719 // this function is for testing set/get/determine reduce method
3720 KMP_EXPORT kmp_int32 __kmp_get_reduce_method(void);
3721 
3722 KMP_EXPORT kmp_uint64 __kmpc_get_taskid();
3723 KMP_EXPORT kmp_uint64 __kmpc_get_parent_taskid();
3724 
3725 // C++ port
3726 // missing 'extern "C"' declarations
3727 
3728 KMP_EXPORT kmp_int32 __kmpc_in_parallel(ident_t *loc);
3729 KMP_EXPORT void __kmpc_pop_num_threads(ident_t *loc, kmp_int32 global_tid);
3730 KMP_EXPORT void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid,
3731                                         kmp_int32 num_threads);
3732 
3733 #if OMP_40_ENABLED
3734 KMP_EXPORT void __kmpc_push_proc_bind(ident_t *loc, kmp_int32 global_tid,
3735                                       int proc_bind);
3736 KMP_EXPORT void __kmpc_push_num_teams(ident_t *loc, kmp_int32 global_tid,
3737                                       kmp_int32 num_teams,
3738                                       kmp_int32 num_threads);
3739 KMP_EXPORT void __kmpc_fork_teams(ident_t *loc, kmp_int32 argc,
3740                                   kmpc_micro microtask, ...);
3741 #endif
3742 #if OMP_45_ENABLED
3743 struct kmp_dim { // loop bounds info casted to kmp_int64
3744   kmp_int64 lo; // lower
3745   kmp_int64 up; // upper
3746   kmp_int64 st; // stride
3747 };
3748 KMP_EXPORT void __kmpc_doacross_init(ident_t *loc, kmp_int32 gtid,
3749                                      kmp_int32 num_dims, struct kmp_dim *dims);
3750 KMP_EXPORT void __kmpc_doacross_wait(ident_t *loc, kmp_int32 gtid,
3751                                      kmp_int64 *vec);
3752 KMP_EXPORT void __kmpc_doacross_post(ident_t *loc, kmp_int32 gtid,
3753                                      kmp_int64 *vec);
3754 KMP_EXPORT void __kmpc_doacross_fini(ident_t *loc, kmp_int32 gtid);
3755 #endif
3756 
3757 KMP_EXPORT void *__kmpc_threadprivate_cached(ident_t *loc, kmp_int32 global_tid,
3758                                              void *data, size_t size,
3759                                              void ***cache);
3760 
3761 // Symbols for MS mutual detection.
3762 extern int _You_must_link_with_exactly_one_OpenMP_library;
3763 extern int _You_must_link_with_Intel_OpenMP_library;
3764 #if KMP_OS_WINDOWS && (KMP_VERSION_MAJOR > 4)
3765 extern int _You_must_link_with_Microsoft_OpenMP_library;
3766 #endif
3767 
3768 // The routines below are not exported.
3769 // Consider making them 'static' in corresponding source files.
3770 void kmp_threadprivate_insert_private_data(int gtid, void *pc_addr,
3771                                            void *data_addr, size_t pc_size);
3772 struct private_common *kmp_threadprivate_insert(int gtid, void *pc_addr,
3773                                                 void *data_addr,
3774                                                 size_t pc_size);
3775 
3776 // ompc_, kmpc_ entries moved from omp.h.
3777 #if KMP_OS_WINDOWS
3778 #define KMPC_CONVENTION __cdecl
3779 #else
3780 #define KMPC_CONVENTION
3781 #endif
3782 
3783 #ifndef __OMP_H
3784 typedef enum omp_sched_t {
3785   omp_sched_static = 1,
3786   omp_sched_dynamic = 2,
3787   omp_sched_guided = 3,
3788   omp_sched_auto = 4
3789 } omp_sched_t;
3790 typedef void *kmp_affinity_mask_t;
3791 #endif
3792 
3793 KMP_EXPORT void KMPC_CONVENTION ompc_set_max_active_levels(int);
3794 KMP_EXPORT void KMPC_CONVENTION ompc_set_schedule(omp_sched_t, int);
3795 KMP_EXPORT int KMPC_CONVENTION ompc_get_ancestor_thread_num(int);
3796 KMP_EXPORT int KMPC_CONVENTION ompc_get_team_size(int);
3797 KMP_EXPORT int KMPC_CONVENTION
3798 kmpc_set_affinity_mask_proc(int, kmp_affinity_mask_t *);
3799 KMP_EXPORT int KMPC_CONVENTION
3800 kmpc_unset_affinity_mask_proc(int, kmp_affinity_mask_t *);
3801 KMP_EXPORT int KMPC_CONVENTION
3802 kmpc_get_affinity_mask_proc(int, kmp_affinity_mask_t *);
3803 
3804 KMP_EXPORT void KMPC_CONVENTION kmpc_set_stacksize(int);
3805 KMP_EXPORT void KMPC_CONVENTION kmpc_set_stacksize_s(size_t);
3806 KMP_EXPORT void KMPC_CONVENTION kmpc_set_library(int);
3807 KMP_EXPORT void KMPC_CONVENTION kmpc_set_defaults(char const *);
3808 KMP_EXPORT void KMPC_CONVENTION kmpc_set_disp_num_buffers(int);
3809 
3810 #ifdef __cplusplus
3811 }
3812 #endif
3813 
3814 #endif /* KMP_H */
3815