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