1 /*
2  * kmp_gsupport.cpp
3  */
4 
5 //===----------------------------------------------------------------------===//
6 //
7 //                     The LLVM Compiler Infrastructure
8 //
9 // This file is dual licensed under the MIT and the University of Illinois Open
10 // Source Licenses. See LICENSE.txt for details.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "kmp.h"
15 #include "kmp_atomic.h"
16 
17 #if OMPT_SUPPORT
18 #include "ompt-specific.h"
19 #endif
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif // __cplusplus
24 
25 #define MKLOC(loc, routine)                                                    \
26   static ident_t(loc) = {0, KMP_IDENT_KMPC, 0, 0, ";unknown;unknown;0;0;;"};
27 
28 #include "kmp_ftn_os.h"
29 
30 void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_BARRIER)(void) {
31   int gtid = __kmp_entry_gtid();
32   MKLOC(loc, "GOMP_barrier");
33   KA_TRACE(20, ("GOMP_barrier: T#%d\n", gtid));
34 #if OMPT_SUPPORT && OMPT_OPTIONAL
35   omp_frame_t *ompt_frame;
36   if (ompt_enabled.enabled) {
37     __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
38     ompt_frame->enter_frame = OMPT_GET_FRAME_ADDRESS(1);
39     OMPT_STORE_RETURN_ADDRESS(gtid);
40   }
41 #endif
42   __kmpc_barrier(&loc, gtid);
43 #if OMPT_SUPPORT && OMPT_OPTIONAL
44   if (ompt_enabled.enabled) {
45     ompt_frame->enter_frame = NULL;
46   }
47 #endif
48 }
49 
50 // Mutual exclusion
51 
52 // The symbol that icc/ifort generates for unnamed for unnamed critical sections
53 // - .gomp_critical_user_ - is defined using .comm in any objects reference it.
54 // We can't reference it directly here in C code, as the symbol contains a ".".
55 //
56 // The RTL contains an assembly language definition of .gomp_critical_user_
57 // with another symbol __kmp_unnamed_critical_addr initialized with it's
58 // address.
59 extern kmp_critical_name *__kmp_unnamed_critical_addr;
60 
61 void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_CRITICAL_START)(void) {
62   int gtid = __kmp_entry_gtid();
63   MKLOC(loc, "GOMP_critical_start");
64   KA_TRACE(20, ("GOMP_critical_start: T#%d\n", gtid));
65 #if OMPT_SUPPORT && OMPT_OPTIONAL
66   OMPT_STORE_RETURN_ADDRESS(gtid);
67 #endif
68   __kmpc_critical(&loc, gtid, __kmp_unnamed_critical_addr);
69 }
70 
71 void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_CRITICAL_END)(void) {
72   int gtid = __kmp_get_gtid();
73   MKLOC(loc, "GOMP_critical_end");
74   KA_TRACE(20, ("GOMP_critical_end: T#%d\n", gtid));
75 #if OMPT_SUPPORT && OMPT_OPTIONAL
76   OMPT_STORE_RETURN_ADDRESS(gtid);
77 #endif
78   __kmpc_end_critical(&loc, gtid, __kmp_unnamed_critical_addr);
79 }
80 
81 void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_CRITICAL_NAME_START)(void **pptr) {
82   int gtid = __kmp_entry_gtid();
83   MKLOC(loc, "GOMP_critical_name_start");
84   KA_TRACE(20, ("GOMP_critical_name_start: T#%d\n", gtid));
85   __kmpc_critical(&loc, gtid, (kmp_critical_name *)pptr);
86 }
87 
88 void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_CRITICAL_NAME_END)(void **pptr) {
89   int gtid = __kmp_get_gtid();
90   MKLOC(loc, "GOMP_critical_name_end");
91   KA_TRACE(20, ("GOMP_critical_name_end: T#%d\n", gtid));
92   __kmpc_end_critical(&loc, gtid, (kmp_critical_name *)pptr);
93 }
94 
95 // The Gnu codegen tries to use locked operations to perform atomic updates
96 // inline.  If it can't, then it calls GOMP_atomic_start() before performing
97 // the update and GOMP_atomic_end() afterward, regardless of the data type.
98 void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_ATOMIC_START)(void) {
99   int gtid = __kmp_entry_gtid();
100   KA_TRACE(20, ("GOMP_atomic_start: T#%d\n", gtid));
101 
102 #if OMPT_SUPPORT
103   __ompt_thread_assign_wait_id(0);
104 #endif
105 
106   __kmp_acquire_atomic_lock(&__kmp_atomic_lock, gtid);
107 }
108 
109 void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_ATOMIC_END)(void) {
110   int gtid = __kmp_get_gtid();
111   KA_TRACE(20, ("GOMP_atomic_start: T#%d\n", gtid));
112   __kmp_release_atomic_lock(&__kmp_atomic_lock, gtid);
113 }
114 
115 int KMP_EXPAND_NAME(KMP_API_NAME_GOMP_SINGLE_START)(void) {
116   int gtid = __kmp_entry_gtid();
117   MKLOC(loc, "GOMP_single_start");
118   KA_TRACE(20, ("GOMP_single_start: T#%d\n", gtid));
119 
120   if (!TCR_4(__kmp_init_parallel))
121     __kmp_parallel_initialize();
122 
123   // 3rd parameter == FALSE prevents kmp_enter_single from pushing a
124   // workshare when USE_CHECKS is defined.  We need to avoid the push,
125   // as there is no corresponding GOMP_single_end() call.
126   kmp_int32 rc = __kmp_enter_single(gtid, &loc, FALSE);
127 
128 #if OMPT_SUPPORT && OMPT_OPTIONAL
129   kmp_info_t *this_thr = __kmp_threads[gtid];
130   kmp_team_t *team = this_thr->th.th_team;
131   int tid = __kmp_tid_from_gtid(gtid);
132 
133   if (ompt_enabled.enabled) {
134     if (rc) {
135       if (ompt_enabled.ompt_callback_work) {
136         ompt_callbacks.ompt_callback(ompt_callback_work)(
137             ompt_work_single_executor, ompt_scope_begin,
138             &(team->t.ompt_team_info.parallel_data),
139             &(team->t.t_implicit_task_taskdata[tid].ompt_task_info.task_data),
140             1, OMPT_GET_RETURN_ADDRESS(0));
141       }
142     } else {
143       if (ompt_enabled.ompt_callback_work) {
144         ompt_callbacks.ompt_callback(ompt_callback_work)(
145             ompt_work_single_other, ompt_scope_begin,
146             &(team->t.ompt_team_info.parallel_data),
147             &(team->t.t_implicit_task_taskdata[tid].ompt_task_info.task_data),
148             1, OMPT_GET_RETURN_ADDRESS(0));
149         ompt_callbacks.ompt_callback(ompt_callback_work)(
150             ompt_work_single_other, ompt_scope_end,
151             &(team->t.ompt_team_info.parallel_data),
152             &(team->t.t_implicit_task_taskdata[tid].ompt_task_info.task_data),
153             1, OMPT_GET_RETURN_ADDRESS(0));
154       }
155     }
156   }
157 #endif
158 
159   return rc;
160 }
161 
162 void *KMP_EXPAND_NAME(KMP_API_NAME_GOMP_SINGLE_COPY_START)(void) {
163   void *retval;
164   int gtid = __kmp_entry_gtid();
165   MKLOC(loc, "GOMP_single_copy_start");
166   KA_TRACE(20, ("GOMP_single_copy_start: T#%d\n", gtid));
167 
168   if (!TCR_4(__kmp_init_parallel))
169     __kmp_parallel_initialize();
170 
171   // If this is the first thread to enter, return NULL.  The generated code will
172   // then call GOMP_single_copy_end() for this thread only, with the
173   // copyprivate data pointer as an argument.
174   if (__kmp_enter_single(gtid, &loc, FALSE))
175     return NULL;
176 
177 // Wait for the first thread to set the copyprivate data pointer,
178 // and for all other threads to reach this point.
179 
180 #if OMPT_SUPPORT && OMPT_OPTIONAL
181   omp_frame_t *ompt_frame;
182   if (ompt_enabled.enabled) {
183     __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
184     ompt_frame->enter_frame = OMPT_GET_FRAME_ADDRESS(1);
185     OMPT_STORE_RETURN_ADDRESS(gtid);
186   }
187 #endif
188   __kmp_barrier(bs_plain_barrier, gtid, FALSE, 0, NULL, NULL);
189 
190   // Retrieve the value of the copyprivate data point, and wait for all
191   // threads to do likewise, then return.
192   retval = __kmp_team_from_gtid(gtid)->t.t_copypriv_data;
193 #if OMPT_SUPPORT && OMPT_OPTIONAL
194   if (ompt_enabled.enabled) {
195     OMPT_STORE_RETURN_ADDRESS(gtid);
196   }
197 #endif
198   __kmp_barrier(bs_plain_barrier, gtid, FALSE, 0, NULL, NULL);
199 #if OMPT_SUPPORT && OMPT_OPTIONAL
200   if (ompt_enabled.enabled) {
201     ompt_frame->enter_frame = NULL;
202   }
203 #endif
204   return retval;
205 }
206 
207 void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_SINGLE_COPY_END)(void *data) {
208   int gtid = __kmp_get_gtid();
209   KA_TRACE(20, ("GOMP_single_copy_end: T#%d\n", gtid));
210 
211   // Set the copyprivate data pointer fo the team, then hit the barrier so that
212   // the other threads will continue on and read it.  Hit another barrier before
213   // continuing, so that the know that the copyprivate data pointer has been
214   // propagated to all threads before trying to reuse the t_copypriv_data field.
215   __kmp_team_from_gtid(gtid)->t.t_copypriv_data = data;
216 #if OMPT_SUPPORT && OMPT_OPTIONAL
217   omp_frame_t *ompt_frame;
218   if (ompt_enabled.enabled) {
219     __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
220     ompt_frame->enter_frame = OMPT_GET_FRAME_ADDRESS(1);
221     OMPT_STORE_RETURN_ADDRESS(gtid);
222   }
223 #endif
224   __kmp_barrier(bs_plain_barrier, gtid, FALSE, 0, NULL, NULL);
225 #if OMPT_SUPPORT && OMPT_OPTIONAL
226   if (ompt_enabled.enabled) {
227     OMPT_STORE_RETURN_ADDRESS(gtid);
228   }
229 #endif
230   __kmp_barrier(bs_plain_barrier, gtid, FALSE, 0, NULL, NULL);
231 #if OMPT_SUPPORT && OMPT_OPTIONAL
232   if (ompt_enabled.enabled) {
233     ompt_frame->enter_frame = NULL;
234   }
235 #endif
236 }
237 
238 void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_ORDERED_START)(void) {
239   int gtid = __kmp_entry_gtid();
240   MKLOC(loc, "GOMP_ordered_start");
241   KA_TRACE(20, ("GOMP_ordered_start: T#%d\n", gtid));
242 #if OMPT_SUPPORT && OMPT_OPTIONAL
243   OMPT_STORE_RETURN_ADDRESS(gtid);
244 #endif
245   __kmpc_ordered(&loc, gtid);
246 }
247 
248 void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_ORDERED_END)(void) {
249   int gtid = __kmp_get_gtid();
250   MKLOC(loc, "GOMP_ordered_end");
251   KA_TRACE(20, ("GOMP_ordered_start: T#%d\n", gtid));
252 #if OMPT_SUPPORT && OMPT_OPTIONAL
253   OMPT_STORE_RETURN_ADDRESS(gtid);
254 #endif
255   __kmpc_end_ordered(&loc, gtid);
256 }
257 
258 // Dispatch macro defs
259 //
260 // They come in two flavors: 64-bit unsigned, and either 32-bit signed
261 // (IA-32 architecture) or 64-bit signed (Intel(R) 64).
262 
263 #if KMP_ARCH_X86 || KMP_ARCH_ARM || KMP_ARCH_MIPS
264 #define KMP_DISPATCH_INIT __kmp_aux_dispatch_init_4
265 #define KMP_DISPATCH_FINI_CHUNK __kmp_aux_dispatch_fini_chunk_4
266 #define KMP_DISPATCH_NEXT __kmpc_dispatch_next_4
267 #else
268 #define KMP_DISPATCH_INIT __kmp_aux_dispatch_init_8
269 #define KMP_DISPATCH_FINI_CHUNK __kmp_aux_dispatch_fini_chunk_8
270 #define KMP_DISPATCH_NEXT __kmpc_dispatch_next_8
271 #endif /* KMP_ARCH_X86 */
272 
273 #define KMP_DISPATCH_INIT_ULL __kmp_aux_dispatch_init_8u
274 #define KMP_DISPATCH_FINI_CHUNK_ULL __kmp_aux_dispatch_fini_chunk_8u
275 #define KMP_DISPATCH_NEXT_ULL __kmpc_dispatch_next_8u
276 
277 // The parallel contruct
278 
279 #ifndef KMP_DEBUG
280 static
281 #endif /* KMP_DEBUG */
282     void
283     __kmp_GOMP_microtask_wrapper(int *gtid, int *npr, void (*task)(void *),
284                                  void *data) {
285 #if OMPT_SUPPORT
286   kmp_info_t *thr;
287   omp_frame_t *ompt_frame;
288   omp_state_t enclosing_state;
289 
290   if (ompt_enabled.enabled) {
291     // get pointer to thread data structure
292     thr = __kmp_threads[*gtid];
293 
294     // save enclosing task state; set current state for task
295     enclosing_state = thr->th.ompt_thread_info.state;
296     thr->th.ompt_thread_info.state = omp_state_work_parallel;
297 
298     // set task frame
299     __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
300     ompt_frame->exit_frame = OMPT_GET_FRAME_ADDRESS(0);
301   }
302 #endif
303 
304   task(data);
305 
306 #if OMPT_SUPPORT
307   if (ompt_enabled.enabled) {
308     // clear task frame
309     ompt_frame->exit_frame = NULL;
310 
311     // restore enclosing state
312     thr->th.ompt_thread_info.state = enclosing_state;
313   }
314 #endif
315 }
316 
317 #ifndef KMP_DEBUG
318 static
319 #endif /* KMP_DEBUG */
320     void
321     __kmp_GOMP_parallel_microtask_wrapper(int *gtid, int *npr,
322                                           void (*task)(void *), void *data,
323                                           unsigned num_threads, ident_t *loc,
324                                           enum sched_type schedule, long start,
325                                           long end, long incr,
326                                           long chunk_size) {
327   // Intialize the loop worksharing construct.
328 
329   KMP_DISPATCH_INIT(loc, *gtid, schedule, start, end, incr, chunk_size,
330                     schedule != kmp_sch_static);
331 
332 #if OMPT_SUPPORT
333   kmp_info_t *thr;
334   omp_frame_t *ompt_frame;
335   omp_state_t enclosing_state;
336 
337   if (ompt_enabled.enabled) {
338     thr = __kmp_threads[*gtid];
339     // save enclosing task state; set current state for task
340     enclosing_state = thr->th.ompt_thread_info.state;
341     thr->th.ompt_thread_info.state = omp_state_work_parallel;
342 
343     // set task frame
344     __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
345     ompt_frame->exit_frame = OMPT_GET_FRAME_ADDRESS(0);
346   }
347 #endif
348 
349   // Now invoke the microtask.
350   task(data);
351 
352 #if OMPT_SUPPORT
353   if (ompt_enabled.enabled) {
354     // clear task frame
355     ompt_frame->exit_frame = NULL;
356 
357     // reset enclosing state
358     thr->th.ompt_thread_info.state = enclosing_state;
359   }
360 #endif
361 }
362 
363 #ifndef KMP_DEBUG
364 static
365 #endif /* KMP_DEBUG */
366     void
367     __kmp_GOMP_fork_call(ident_t *loc, int gtid, void (*unwrapped_task)(void *),
368                          microtask_t wrapper, int argc, ...) {
369   int rc;
370   kmp_info_t *thr = __kmp_threads[gtid];
371   kmp_team_t *team = thr->th.th_team;
372   int tid = __kmp_tid_from_gtid(gtid);
373 
374   va_list ap;
375   va_start(ap, argc);
376 
377   rc = __kmp_fork_call(loc, gtid, fork_context_gnu, argc, wrapper,
378                        __kmp_invoke_task_func,
379 #if (KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64) && KMP_OS_LINUX
380                        &ap
381 #else
382                        ap
383 #endif
384                        );
385 
386   va_end(ap);
387 
388   if (rc) {
389     __kmp_run_before_invoked_task(gtid, tid, thr, team);
390   }
391 
392 #if OMPT_SUPPORT
393   int ompt_team_size;
394   if (ompt_enabled.enabled) {
395     ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
396     ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
397 
398     // implicit task callback
399     if (ompt_enabled.ompt_callback_implicit_task) {
400       ompt_team_size = __kmp_team_from_gtid(gtid)->t.t_nproc;
401       ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
402           ompt_scope_begin, &(team_info->parallel_data),
403           &(task_info->task_data), ompt_team_size, __kmp_tid_from_gtid(gtid));
404       task_info->thread_num = __kmp_tid_from_gtid(gtid);
405     }
406     thr->th.ompt_thread_info.state = omp_state_work_parallel;
407   }
408 #endif
409 }
410 
411 static void __kmp_GOMP_serialized_parallel(ident_t *loc, kmp_int32 gtid,
412                                            void (*task)(void *)) {
413 #if OMPT_SUPPORT
414   OMPT_STORE_RETURN_ADDRESS(gtid);
415 #endif
416   __kmp_serialized_parallel(loc, gtid);
417 }
418 
419 void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_START)(void (*task)(void *),
420                                                        void *data,
421                                                        unsigned num_threads) {
422   int gtid = __kmp_entry_gtid();
423 
424 #if OMPT_SUPPORT
425   omp_frame_t *parent_frame, *frame;
426 
427   if (ompt_enabled.enabled) {
428     __ompt_get_task_info_internal(0, NULL, NULL, &parent_frame, NULL, NULL);
429     parent_frame->enter_frame = OMPT_GET_FRAME_ADDRESS(1);
430     OMPT_STORE_RETURN_ADDRESS(gtid);
431   }
432 #endif
433 
434   MKLOC(loc, "GOMP_parallel_start");
435   KA_TRACE(20, ("GOMP_parallel_start: T#%d\n", gtid));
436 
437   if (__kmpc_ok_to_fork(&loc) && (num_threads != 1)) {
438     if (num_threads != 0) {
439       __kmp_push_num_threads(&loc, gtid, num_threads);
440     }
441     __kmp_GOMP_fork_call(&loc, gtid, task,
442                          (microtask_t)__kmp_GOMP_microtask_wrapper, 2, task,
443                          data);
444   } else {
445     __kmp_GOMP_serialized_parallel(&loc, gtid, task);
446   }
447 
448 #if OMPT_SUPPORT
449   if (ompt_enabled.enabled) {
450     __ompt_get_task_info_internal(0, NULL, NULL, &frame, NULL, NULL);
451     frame->exit_frame = OMPT_GET_FRAME_ADDRESS(1);
452   }
453 #endif
454 }
455 
456 void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_END)(void) {
457   int gtid = __kmp_get_gtid();
458   kmp_info_t *thr;
459   int ompt_team_size = __kmp_team_from_gtid(gtid)->t.t_nproc;
460 
461   thr = __kmp_threads[gtid];
462 
463   MKLOC(loc, "GOMP_parallel_end");
464   KA_TRACE(20, ("GOMP_parallel_end: T#%d\n", gtid));
465 
466   if (!thr->th.th_team->t.t_serialized) {
467     __kmp_run_after_invoked_task(gtid, __kmp_tid_from_gtid(gtid), thr,
468                                  thr->th.th_team);
469 
470 #if OMPT_SUPPORT
471     if (ompt_enabled.enabled) {
472       // Implicit task is finished here, in the barrier we might schedule
473       // deferred tasks,
474       // these don't see the implicit task on the stack
475       OMPT_CUR_TASK_INFO(thr)->frame.exit_frame = NULL;
476     }
477 #endif
478 
479     __kmp_join_call(&loc, gtid
480 #if OMPT_SUPPORT
481                     ,
482                     fork_context_gnu
483 #endif
484                     );
485   } else {
486     __kmpc_end_serialized_parallel(&loc, gtid);
487   }
488 }
489 
490 // Loop worksharing constructs
491 
492 // The Gnu codegen passes in an exclusive upper bound for the overall range,
493 // but the libguide dispatch code expects an inclusive upper bound, hence the
494 // "end - incr" 5th argument to KMP_DISPATCH_INIT (and the " ub - str" 11th
495 // argument to __kmp_GOMP_fork_call).
496 //
497 // Conversely, KMP_DISPATCH_NEXT returns and inclusive upper bound in *p_ub,
498 // but the Gnu codegen expects an excluside upper bound, so the adjustment
499 // "*p_ub += stride" compenstates for the discrepancy.
500 //
501 // Correction: the gnu codegen always adjusts the upper bound by +-1, not the
502 // stride value.  We adjust the dispatch parameters accordingly (by +-1), but
503 // we still adjust p_ub by the actual stride value.
504 //
505 // The "runtime" versions do not take a chunk_sz parameter.
506 //
507 // The profile lib cannot support construct checking of unordered loops that
508 // are predetermined by the compiler to be statically scheduled, as the gcc
509 // codegen will not always emit calls to GOMP_loop_static_next() to get the
510 // next iteration.  Instead, it emits inline code to call omp_get_thread_num()
511 // num and calculate the iteration space using the result.  It doesn't do this
512 // with ordered static loop, so they can be checked.
513 
514 #if OMPT_SUPPORT
515 #define IF_OMPT_SUPPORT(code) code
516 #else
517 #define IF_OMPT_SUPPORT(code)
518 #endif
519 
520 #define LOOP_START(func, schedule)                                             \
521   int func(long lb, long ub, long str, long chunk_sz, long *p_lb,              \
522            long *p_ub) {                                                       \
523     int status;                                                                \
524     long stride;                                                               \
525     int gtid = __kmp_entry_gtid();                                             \
526     MKLOC(loc, #func);                                                         \
527     KA_TRACE(20,                                                               \
528              (#func ": T#%d, lb 0x%lx, ub 0x%lx, str 0x%lx, chunk_sz 0x%lx\n", \
529               gtid, lb, ub, str, chunk_sz));                                   \
530                                                                                \
531     if ((str > 0) ? (lb < ub) : (lb > ub)) {                                   \
532       IF_OMPT_SUPPORT(OMPT_STORE_RETURN_ADDRESS(gtid);)                        \
533       KMP_DISPATCH_INIT(&loc, gtid, (schedule), lb,                            \
534                         (str > 0) ? (ub - 1) : (ub + 1), str, chunk_sz,        \
535                         (schedule) != kmp_sch_static);                         \
536       IF_OMPT_SUPPORT(OMPT_STORE_RETURN_ADDRESS(gtid);)                        \
537       status = KMP_DISPATCH_NEXT(&loc, gtid, NULL, (kmp_int *)p_lb,            \
538                                  (kmp_int *)p_ub, (kmp_int *)&stride);         \
539       if (status) {                                                            \
540         KMP_DEBUG_ASSERT(stride == str);                                       \
541         *p_ub += (str > 0) ? 1 : -1;                                           \
542       }                                                                        \
543     } else {                                                                   \
544       status = 0;                                                              \
545     }                                                                          \
546                                                                                \
547     KA_TRACE(20,                                                               \
548              (#func " exit: T#%d, *p_lb 0x%lx, *p_ub 0x%lx, returning %d\n",   \
549               gtid, *p_lb, *p_ub, status));                                    \
550     return status;                                                             \
551   }
552 
553 #define LOOP_RUNTIME_START(func, schedule)                                     \
554   int func(long lb, long ub, long str, long *p_lb, long *p_ub) {               \
555     int status;                                                                \
556     long stride;                                                               \
557     long chunk_sz = 0;                                                         \
558     int gtid = __kmp_entry_gtid();                                             \
559     MKLOC(loc, #func);                                                         \
560     KA_TRACE(20,                                                               \
561              (#func ": T#%d, lb 0x%lx, ub 0x%lx, str 0x%lx, chunk_sz %d\n",    \
562               gtid, lb, ub, str, chunk_sz));                                   \
563                                                                                \
564     if ((str > 0) ? (lb < ub) : (lb > ub)) {                                   \
565       IF_OMPT_SUPPORT(OMPT_STORE_RETURN_ADDRESS(gtid);)                        \
566       KMP_DISPATCH_INIT(&loc, gtid, (schedule), lb,                            \
567                         (str > 0) ? (ub - 1) : (ub + 1), str, chunk_sz, TRUE); \
568       IF_OMPT_SUPPORT(OMPT_STORE_RETURN_ADDRESS(gtid);)                        \
569       status = KMP_DISPATCH_NEXT(&loc, gtid, NULL, (kmp_int *)p_lb,            \
570                                  (kmp_int *)p_ub, (kmp_int *)&stride);         \
571       if (status) {                                                            \
572         KMP_DEBUG_ASSERT(stride == str);                                       \
573         *p_ub += (str > 0) ? 1 : -1;                                           \
574       }                                                                        \
575     } else {                                                                   \
576       status = 0;                                                              \
577     }                                                                          \
578                                                                                \
579     KA_TRACE(20,                                                               \
580              (#func " exit: T#%d, *p_lb 0x%lx, *p_ub 0x%lx, returning %d\n",   \
581               gtid, *p_lb, *p_ub, status));                                    \
582     return status;                                                             \
583   }
584 
585 #define LOOP_NEXT(func, fini_code)                                             \
586   int func(long *p_lb, long *p_ub) {                                           \
587     int status;                                                                \
588     long stride;                                                               \
589     int gtid = __kmp_get_gtid();                                               \
590     MKLOC(loc, #func);                                                         \
591     KA_TRACE(20, (#func ": T#%d\n", gtid));                                    \
592                                                                                \
593     IF_OMPT_SUPPORT(OMPT_STORE_RETURN_ADDRESS(gtid);)                          \
594     fini_code status = KMP_DISPATCH_NEXT(&loc, gtid, NULL, (kmp_int *)p_lb,    \
595                                          (kmp_int *)p_ub, (kmp_int *)&stride); \
596     if (status) {                                                              \
597       *p_ub += (stride > 0) ? 1 : -1;                                          \
598     }                                                                          \
599                                                                                \
600     KA_TRACE(20,                                                               \
601              (#func " exit: T#%d, *p_lb 0x%lx, *p_ub 0x%lx, stride 0x%lx, "    \
602                     "returning %d\n",                                          \
603               gtid, *p_lb, *p_ub, stride, status));                            \
604     return status;                                                             \
605   }
606 
607 LOOP_START(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_STATIC_START), kmp_sch_static)
608 LOOP_NEXT(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_STATIC_NEXT), {})
609 LOOP_START(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_DYNAMIC_START),
610            kmp_sch_dynamic_chunked)
611 LOOP_NEXT(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_DYNAMIC_NEXT), {})
612 LOOP_START(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_GUIDED_START),
613            kmp_sch_guided_chunked)
614 LOOP_NEXT(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_GUIDED_NEXT), {})
615 LOOP_RUNTIME_START(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_RUNTIME_START),
616                    kmp_sch_runtime)
617 LOOP_NEXT(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_RUNTIME_NEXT), {})
618 
619 LOOP_START(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ORDERED_STATIC_START),
620            kmp_ord_static)
621 LOOP_NEXT(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ORDERED_STATIC_NEXT),
622           { KMP_DISPATCH_FINI_CHUNK(&loc, gtid); })
623 LOOP_START(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ORDERED_DYNAMIC_START),
624            kmp_ord_dynamic_chunked)
625 LOOP_NEXT(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ORDERED_DYNAMIC_NEXT),
626           { KMP_DISPATCH_FINI_CHUNK(&loc, gtid); })
627 LOOP_START(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ORDERED_GUIDED_START),
628            kmp_ord_guided_chunked)
629 LOOP_NEXT(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ORDERED_GUIDED_NEXT),
630           { KMP_DISPATCH_FINI_CHUNK(&loc, gtid); })
631 LOOP_RUNTIME_START(
632     KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ORDERED_RUNTIME_START),
633     kmp_ord_runtime)
634 LOOP_NEXT(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ORDERED_RUNTIME_NEXT),
635           { KMP_DISPATCH_FINI_CHUNK(&loc, gtid); })
636 
637 void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_END)(void) {
638   int gtid = __kmp_get_gtid();
639   KA_TRACE(20, ("GOMP_loop_end: T#%d\n", gtid))
640 
641 #if OMPT_SUPPORT && OMPT_OPTIONAL
642   omp_frame_t *ompt_frame;
643   if (ompt_enabled.enabled) {
644     __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
645     ompt_frame->enter_frame = OMPT_GET_FRAME_ADDRESS(1);
646     OMPT_STORE_RETURN_ADDRESS(gtid);
647   }
648 #endif
649   __kmp_barrier(bs_plain_barrier, gtid, FALSE, 0, NULL, NULL);
650 #if OMPT_SUPPORT && OMPT_OPTIONAL
651   if (ompt_enabled.enabled) {
652     ompt_frame->enter_frame = NULL;
653   }
654 #endif
655 
656   KA_TRACE(20, ("GOMP_loop_end exit: T#%d\n", gtid))
657 }
658 
659 void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_END_NOWAIT)(void) {
660   KA_TRACE(20, ("GOMP_loop_end_nowait: T#%d\n", __kmp_get_gtid()))
661 }
662 
663 // Unsigned long long loop worksharing constructs
664 //
665 // These are new with gcc 4.4
666 
667 #define LOOP_START_ULL(func, schedule)                                         \
668   int func(int up, unsigned long long lb, unsigned long long ub,               \
669            unsigned long long str, unsigned long long chunk_sz,                \
670            unsigned long long *p_lb, unsigned long long *p_ub) {               \
671     int status;                                                                \
672     long long str2 = up ? ((long long)str) : -((long long)str);                \
673     long long stride;                                                          \
674     int gtid = __kmp_entry_gtid();                                             \
675     MKLOC(loc, #func);                                                         \
676                                                                                \
677     KA_TRACE(                                                                  \
678         20,                                                                    \
679         (#func                                                                 \
680          ": T#%d, up %d, lb 0x%llx, ub 0x%llx, str 0x%llx, chunk_sz 0x%llx\n", \
681          gtid, up, lb, ub, str, chunk_sz));                                    \
682                                                                                \
683     if ((str > 0) ? (lb < ub) : (lb > ub)) {                                   \
684       KMP_DISPATCH_INIT_ULL(&loc, gtid, (schedule), lb,                        \
685                             (str2 > 0) ? (ub - 1) : (ub + 1), str2, chunk_sz,  \
686                             (schedule) != kmp_sch_static);                     \
687       status =                                                                 \
688           KMP_DISPATCH_NEXT_ULL(&loc, gtid, NULL, (kmp_uint64 *)p_lb,          \
689                                 (kmp_uint64 *)p_ub, (kmp_int64 *)&stride);     \
690       if (status) {                                                            \
691         KMP_DEBUG_ASSERT(stride == str2);                                      \
692         *p_ub += (str > 0) ? 1 : -1;                                           \
693       }                                                                        \
694     } else {                                                                   \
695       status = 0;                                                              \
696     }                                                                          \
697                                                                                \
698     KA_TRACE(20,                                                               \
699              (#func " exit: T#%d, *p_lb 0x%llx, *p_ub 0x%llx, returning %d\n", \
700               gtid, *p_lb, *p_ub, status));                                    \
701     return status;                                                             \
702   }
703 
704 #define LOOP_RUNTIME_START_ULL(func, schedule)                                 \
705   int func(int up, unsigned long long lb, unsigned long long ub,               \
706            unsigned long long str, unsigned long long *p_lb,                   \
707            unsigned long long *p_ub) {                                         \
708     int status;                                                                \
709     long long str2 = up ? ((long long)str) : -((long long)str);                \
710     unsigned long long stride;                                                 \
711     unsigned long long chunk_sz = 0;                                           \
712     int gtid = __kmp_entry_gtid();                                             \
713     MKLOC(loc, #func);                                                         \
714                                                                                \
715     KA_TRACE(                                                                  \
716         20,                                                                    \
717         (#func                                                                 \
718          ": T#%d, up %d, lb 0x%llx, ub 0x%llx, str 0x%llx, chunk_sz 0x%llx\n", \
719          gtid, up, lb, ub, str, chunk_sz));                                    \
720                                                                                \
721     if ((str > 0) ? (lb < ub) : (lb > ub)) {                                   \
722       KMP_DISPATCH_INIT_ULL(&loc, gtid, (schedule), lb,                        \
723                             (str2 > 0) ? (ub - 1) : (ub + 1), str2, chunk_sz,  \
724                             TRUE);                                             \
725       status =                                                                 \
726           KMP_DISPATCH_NEXT_ULL(&loc, gtid, NULL, (kmp_uint64 *)p_lb,          \
727                                 (kmp_uint64 *)p_ub, (kmp_int64 *)&stride);     \
728       if (status) {                                                            \
729         KMP_DEBUG_ASSERT((long long)stride == str2);                           \
730         *p_ub += (str > 0) ? 1 : -1;                                           \
731       }                                                                        \
732     } else {                                                                   \
733       status = 0;                                                              \
734     }                                                                          \
735                                                                                \
736     KA_TRACE(20,                                                               \
737              (#func " exit: T#%d, *p_lb 0x%llx, *p_ub 0x%llx, returning %d\n", \
738               gtid, *p_lb, *p_ub, status));                                    \
739     return status;                                                             \
740   }
741 
742 #define LOOP_NEXT_ULL(func, fini_code)                                         \
743   int func(unsigned long long *p_lb, unsigned long long *p_ub) {               \
744     int status;                                                                \
745     long long stride;                                                          \
746     int gtid = __kmp_get_gtid();                                               \
747     MKLOC(loc, #func);                                                         \
748     KA_TRACE(20, (#func ": T#%d\n", gtid));                                    \
749                                                                                \
750     fini_code status =                                                         \
751         KMP_DISPATCH_NEXT_ULL(&loc, gtid, NULL, (kmp_uint64 *)p_lb,            \
752                               (kmp_uint64 *)p_ub, (kmp_int64 *)&stride);       \
753     if (status) {                                                              \
754       *p_ub += (stride > 0) ? 1 : -1;                                          \
755     }                                                                          \
756                                                                                \
757     KA_TRACE(20,                                                               \
758              (#func " exit: T#%d, *p_lb 0x%llx, *p_ub 0x%llx, stride 0x%llx, " \
759                     "returning %d\n",                                          \
760               gtid, *p_lb, *p_ub, stride, status));                            \
761     return status;                                                             \
762   }
763 
764 LOOP_START_ULL(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_STATIC_START),
765                kmp_sch_static)
766 LOOP_NEXT_ULL(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_STATIC_NEXT), {})
767 LOOP_START_ULL(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_DYNAMIC_START),
768                kmp_sch_dynamic_chunked)
769 LOOP_NEXT_ULL(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_DYNAMIC_NEXT), {})
770 LOOP_START_ULL(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_GUIDED_START),
771                kmp_sch_guided_chunked)
772 LOOP_NEXT_ULL(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_GUIDED_NEXT), {})
773 LOOP_RUNTIME_START_ULL(
774     KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_RUNTIME_START), kmp_sch_runtime)
775 LOOP_NEXT_ULL(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_RUNTIME_NEXT), {})
776 
777 LOOP_START_ULL(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_ORDERED_STATIC_START),
778                kmp_ord_static)
779 LOOP_NEXT_ULL(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_ORDERED_STATIC_NEXT),
780               { KMP_DISPATCH_FINI_CHUNK_ULL(&loc, gtid); })
781 LOOP_START_ULL(
782     KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_ORDERED_DYNAMIC_START),
783     kmp_ord_dynamic_chunked)
784 LOOP_NEXT_ULL(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_ORDERED_DYNAMIC_NEXT),
785               { KMP_DISPATCH_FINI_CHUNK_ULL(&loc, gtid); })
786 LOOP_START_ULL(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_ORDERED_GUIDED_START),
787                kmp_ord_guided_chunked)
788 LOOP_NEXT_ULL(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_ORDERED_GUIDED_NEXT),
789               { KMP_DISPATCH_FINI_CHUNK_ULL(&loc, gtid); })
790 LOOP_RUNTIME_START_ULL(
791     KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_ORDERED_RUNTIME_START),
792     kmp_ord_runtime)
793 LOOP_NEXT_ULL(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ULL_ORDERED_RUNTIME_NEXT),
794               { KMP_DISPATCH_FINI_CHUNK_ULL(&loc, gtid); })
795 
796 // Combined parallel / loop worksharing constructs
797 //
798 // There are no ull versions (yet).
799 
800 #define PARALLEL_LOOP_START(func, schedule, ompt_pre, ompt_post)               \
801   void func(void (*task)(void *), void *data, unsigned num_threads, long lb,   \
802             long ub, long str, long chunk_sz) {                                \
803     int gtid = __kmp_entry_gtid();                                             \
804     MKLOC(loc, #func);                                                         \
805     KA_TRACE(20,                                                               \
806              (#func ": T#%d, lb 0x%lx, ub 0x%lx, str 0x%lx, chunk_sz 0x%lx\n", \
807               gtid, lb, ub, str, chunk_sz));                                   \
808                                                                                \
809     ompt_pre();                                                                \
810                                                                                \
811     if (__kmpc_ok_to_fork(&loc) && (num_threads != 1)) {                       \
812       if (num_threads != 0) {                                                  \
813         __kmp_push_num_threads(&loc, gtid, num_threads);                       \
814       }                                                                        \
815       __kmp_GOMP_fork_call(&loc, gtid, task,                                   \
816                            (microtask_t)__kmp_GOMP_parallel_microtask_wrapper, \
817                            9, task, data, num_threads, &loc, (schedule), lb,   \
818                            (str > 0) ? (ub - 1) : (ub + 1), str, chunk_sz);    \
819       IF_OMPT_SUPPORT(OMPT_STORE_RETURN_ADDRESS(gtid));                        \
820     } else {                                                                   \
821       __kmp_GOMP_serialized_parallel(&loc, gtid, task);                        \
822       IF_OMPT_SUPPORT(OMPT_STORE_RETURN_ADDRESS(gtid));                        \
823     }                                                                          \
824                                                                                \
825     KMP_DISPATCH_INIT(&loc, gtid, (schedule), lb,                              \
826                       (str > 0) ? (ub - 1) : (ub + 1), str, chunk_sz,          \
827                       (schedule) != kmp_sch_static);                           \
828                                                                                \
829     ompt_post();                                                               \
830                                                                                \
831     KA_TRACE(20, (#func " exit: T#%d\n", gtid));                               \
832   }
833 
834 #if OMPT_SUPPORT && OMPT_OPTIONAL
835 
836 #define OMPT_LOOP_PRE()                                                        \
837   omp_frame_t *parent_frame;                                                   \
838   if (ompt_enabled.enabled) {                                                  \
839     __ompt_get_task_info_internal(0, NULL, NULL, &parent_frame, NULL, NULL);   \
840     parent_frame->enter_frame = OMPT_GET_FRAME_ADDRESS(1);                     \
841     OMPT_STORE_RETURN_ADDRESS(gtid);                                           \
842   }
843 
844 #define OMPT_LOOP_POST()                                                       \
845   if (ompt_enabled.enabled) {                                                  \
846     parent_frame->enter_frame = NULL;                                          \
847   }
848 
849 #else
850 
851 #define OMPT_LOOP_PRE()
852 
853 #define OMPT_LOOP_POST()
854 
855 #endif
856 
857 PARALLEL_LOOP_START(
858     KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_LOOP_STATIC_START),
859     kmp_sch_static, OMPT_LOOP_PRE, OMPT_LOOP_POST)
860 PARALLEL_LOOP_START(
861     KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_LOOP_DYNAMIC_START),
862     kmp_sch_dynamic_chunked, OMPT_LOOP_PRE, OMPT_LOOP_POST)
863 PARALLEL_LOOP_START(
864     KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_LOOP_GUIDED_START),
865     kmp_sch_guided_chunked, OMPT_LOOP_PRE, OMPT_LOOP_POST)
866 PARALLEL_LOOP_START(
867     KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_LOOP_RUNTIME_START),
868     kmp_sch_runtime, OMPT_LOOP_PRE, OMPT_LOOP_POST)
869 
870 // Tasking constructs
871 
872 void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_TASK)(void (*func)(void *), void *data,
873                                              void (*copy_func)(void *, void *),
874                                              long arg_size, long arg_align,
875                                              bool if_cond, unsigned gomp_flags
876 #if OMP_40_ENABLED
877                                              ,
878                                              void **depend
879 #endif
880                                              ) {
881   MKLOC(loc, "GOMP_task");
882   int gtid = __kmp_entry_gtid();
883   kmp_int32 flags = 0;
884   kmp_tasking_flags_t *input_flags = (kmp_tasking_flags_t *)&flags;
885 
886   KA_TRACE(20, ("GOMP_task: T#%d\n", gtid));
887 
888   // The low-order bit is the "untied" flag
889   if (!(gomp_flags & 1)) {
890     input_flags->tiedness = 1;
891   }
892   // The second low-order bit is the "final" flag
893   if (gomp_flags & 2) {
894     input_flags->final = 1;
895   }
896   input_flags->native = 1;
897   // __kmp_task_alloc() sets up all other flags
898 
899   if (!if_cond) {
900     arg_size = 0;
901   }
902 
903   kmp_task_t *task = __kmp_task_alloc(
904       &loc, gtid, input_flags, sizeof(kmp_task_t),
905       arg_size ? arg_size + arg_align - 1 : 0, (kmp_routine_entry_t)func);
906 
907   if (arg_size > 0) {
908     if (arg_align > 0) {
909       task->shareds = (void *)((((size_t)task->shareds) + arg_align - 1) /
910                                arg_align * arg_align);
911     }
912     // else error??
913 
914     if (copy_func) {
915       (*copy_func)(task->shareds, data);
916     } else {
917       KMP_MEMCPY(task->shareds, data, arg_size);
918     }
919   }
920 
921 #if OMPT_SUPPORT
922   kmp_taskdata_t *current_task;
923   if (ompt_enabled.enabled) {
924     OMPT_STORE_RETURN_ADDRESS(gtid);
925     current_task = __kmp_threads[gtid]->th.th_current_task;
926     current_task->ompt_task_info.frame.enter_frame = OMPT_GET_FRAME_ADDRESS(1);
927   }
928 #endif
929 
930   if (if_cond) {
931 #if OMP_40_ENABLED
932     if (gomp_flags & 8) {
933       KMP_ASSERT(depend);
934       const size_t ndeps = (kmp_intptr_t)depend[0];
935       const size_t nout = (kmp_intptr_t)depend[1];
936       kmp_depend_info_t dep_list[ndeps];
937 
938       for (size_t i = 0U; i < ndeps; i++) {
939         dep_list[i].base_addr = (kmp_intptr_t)depend[2U + i];
940         dep_list[i].len = 0U;
941         dep_list[i].flags.in = 1;
942         dep_list[i].flags.out = (i < nout);
943       }
944       __kmpc_omp_task_with_deps(&loc, gtid, task, ndeps, dep_list, 0, NULL);
945     } else {
946 #endif
947       __kmpc_omp_task(&loc, gtid, task);
948     }
949   } else {
950 #if OMPT_SUPPORT
951     ompt_thread_info_t oldInfo;
952     kmp_info_t *thread;
953     kmp_taskdata_t *taskdata;
954     kmp_taskdata_t *current_task;
955     if (ompt_enabled.enabled) {
956       // Store the threads states and restore them after the task
957       thread = __kmp_threads[gtid];
958       taskdata = KMP_TASK_TO_TASKDATA(task);
959       oldInfo = thread->th.ompt_thread_info;
960       thread->th.ompt_thread_info.wait_id = 0;
961       thread->th.ompt_thread_info.state = omp_state_work_parallel;
962       taskdata->ompt_task_info.frame.exit_frame = OMPT_GET_FRAME_ADDRESS(0);
963       OMPT_STORE_RETURN_ADDRESS(gtid);
964     }
965 #endif
966 
967     __kmpc_omp_task_begin_if0(&loc, gtid, task);
968     func(data);
969     __kmpc_omp_task_complete_if0(&loc, gtid, task);
970 
971 #if OMPT_SUPPORT
972     if (ompt_enabled.enabled) {
973       thread->th.ompt_thread_info = oldInfo;
974       taskdata->ompt_task_info.frame.exit_frame = NULL;
975     }
976 #endif
977   }
978 #if OMPT_SUPPORT
979   if (ompt_enabled.enabled) {
980     current_task->ompt_task_info.frame.enter_frame = NULL;
981   }
982 #endif
983 
984   KA_TRACE(20, ("GOMP_task exit: T#%d\n", gtid));
985 }
986 
987 void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_TASKWAIT)(void) {
988   MKLOC(loc, "GOMP_taskwait");
989   int gtid = __kmp_entry_gtid();
990 
991 #if OMPT_SUPPORT
992   if (ompt_enabled.enabled)
993     OMPT_STORE_RETURN_ADDRESS(gtid);
994 #endif
995 
996   KA_TRACE(20, ("GOMP_taskwait: T#%d\n", gtid));
997 
998   __kmpc_omp_taskwait(&loc, gtid);
999 
1000   KA_TRACE(20, ("GOMP_taskwait exit: T#%d\n", gtid));
1001 }
1002 
1003 // Sections worksharing constructs
1004 //
1005 // For the sections construct, we initialize a dynamically scheduled loop
1006 // worksharing construct with lb 1 and stride 1, and use the iteration #'s
1007 // that its returns as sections ids.
1008 //
1009 // There are no special entry points for ordered sections, so we always use
1010 // the dynamically scheduled workshare, even if the sections aren't ordered.
1011 
1012 unsigned KMP_EXPAND_NAME(KMP_API_NAME_GOMP_SECTIONS_START)(unsigned count) {
1013   int status;
1014   kmp_int lb, ub, stride;
1015   int gtid = __kmp_entry_gtid();
1016   MKLOC(loc, "GOMP_sections_start");
1017   KA_TRACE(20, ("GOMP_sections_start: T#%d\n", gtid));
1018 
1019   KMP_DISPATCH_INIT(&loc, gtid, kmp_nm_dynamic_chunked, 1, count, 1, 1, TRUE);
1020 
1021   status = KMP_DISPATCH_NEXT(&loc, gtid, NULL, &lb, &ub, &stride);
1022   if (status) {
1023     KMP_DEBUG_ASSERT(stride == 1);
1024     KMP_DEBUG_ASSERT(lb > 0);
1025     KMP_ASSERT(lb == ub);
1026   } else {
1027     lb = 0;
1028   }
1029 
1030   KA_TRACE(20, ("GOMP_sections_start exit: T#%d returning %u\n", gtid,
1031                 (unsigned)lb));
1032   return (unsigned)lb;
1033 }
1034 
1035 unsigned KMP_EXPAND_NAME(KMP_API_NAME_GOMP_SECTIONS_NEXT)(void) {
1036   int status;
1037   kmp_int lb, ub, stride;
1038   int gtid = __kmp_get_gtid();
1039   MKLOC(loc, "GOMP_sections_next");
1040   KA_TRACE(20, ("GOMP_sections_next: T#%d\n", gtid));
1041 
1042 #if OMPT_SUPPORT
1043   OMPT_STORE_RETURN_ADDRESS(gtid);
1044 #endif
1045 
1046   status = KMP_DISPATCH_NEXT(&loc, gtid, NULL, &lb, &ub, &stride);
1047   if (status) {
1048     KMP_DEBUG_ASSERT(stride == 1);
1049     KMP_DEBUG_ASSERT(lb > 0);
1050     KMP_ASSERT(lb == ub);
1051   } else {
1052     lb = 0;
1053   }
1054 
1055   KA_TRACE(
1056       20, ("GOMP_sections_next exit: T#%d returning %u\n", gtid, (unsigned)lb));
1057   return (unsigned)lb;
1058 }
1059 
1060 void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_SECTIONS_START)(
1061     void (*task)(void *), void *data, unsigned num_threads, unsigned count) {
1062   int gtid = __kmp_entry_gtid();
1063 
1064 #if OMPT_SUPPORT
1065   omp_frame_t *parent_frame;
1066 
1067   if (ompt_enabled.enabled) {
1068     __ompt_get_task_info_internal(0, NULL, NULL, &parent_frame, NULL, NULL);
1069     parent_frame->enter_frame = OMPT_GET_FRAME_ADDRESS(1);
1070     OMPT_STORE_RETURN_ADDRESS(gtid);
1071   }
1072 #endif
1073 
1074   MKLOC(loc, "GOMP_parallel_sections_start");
1075   KA_TRACE(20, ("GOMP_parallel_sections_start: T#%d\n", gtid));
1076 
1077   if (__kmpc_ok_to_fork(&loc) && (num_threads != 1)) {
1078     if (num_threads != 0) {
1079       __kmp_push_num_threads(&loc, gtid, num_threads);
1080     }
1081     __kmp_GOMP_fork_call(&loc, gtid, task,
1082                          (microtask_t)__kmp_GOMP_parallel_microtask_wrapper, 9,
1083                          task, data, num_threads, &loc, kmp_nm_dynamic_chunked,
1084                          (kmp_int)1, (kmp_int)count, (kmp_int)1, (kmp_int)1);
1085   } else {
1086     __kmp_GOMP_serialized_parallel(&loc, gtid, task);
1087   }
1088 
1089 #if OMPT_SUPPORT
1090   if (ompt_enabled.enabled) {
1091     parent_frame->enter_frame = NULL;
1092   }
1093 #endif
1094 
1095   KMP_DISPATCH_INIT(&loc, gtid, kmp_nm_dynamic_chunked, 1, count, 1, 1, TRUE);
1096 
1097   KA_TRACE(20, ("GOMP_parallel_sections_start exit: T#%d\n", gtid));
1098 }
1099 
1100 void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_SECTIONS_END)(void) {
1101   int gtid = __kmp_get_gtid();
1102   KA_TRACE(20, ("GOMP_sections_end: T#%d\n", gtid))
1103 
1104 #if OMPT_SUPPORT
1105   omp_frame_t *ompt_frame;
1106   if (ompt_enabled.enabled) {
1107     __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
1108     ompt_frame->enter_frame = OMPT_GET_FRAME_ADDRESS(1);
1109     OMPT_STORE_RETURN_ADDRESS(gtid);
1110   }
1111 #endif
1112   __kmp_barrier(bs_plain_barrier, gtid, FALSE, 0, NULL, NULL);
1113 #if OMPT_SUPPORT
1114   if (ompt_enabled.enabled) {
1115     ompt_frame->enter_frame = NULL;
1116   }
1117 #endif
1118 
1119   KA_TRACE(20, ("GOMP_sections_end exit: T#%d\n", gtid))
1120 }
1121 
1122 void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_SECTIONS_END_NOWAIT)(void) {
1123   KA_TRACE(20, ("GOMP_sections_end_nowait: T#%d\n", __kmp_get_gtid()))
1124 }
1125 
1126 // libgomp has an empty function for GOMP_taskyield as of 2013-10-10
1127 void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_TASKYIELD)(void) {
1128   KA_TRACE(20, ("GOMP_taskyield: T#%d\n", __kmp_get_gtid()))
1129   return;
1130 }
1131 
1132 #if OMP_40_ENABLED // these are new GOMP_4.0 entry points
1133 
1134 void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL)(void (*task)(void *),
1135                                                  void *data,
1136                                                  unsigned num_threads,
1137                                                  unsigned int flags) {
1138   int gtid = __kmp_entry_gtid();
1139   MKLOC(loc, "GOMP_parallel");
1140   KA_TRACE(20, ("GOMP_parallel: T#%d\n", gtid));
1141 
1142 #if OMPT_SUPPORT
1143   ompt_task_info_t *parent_task_info, *task_info;
1144   if (ompt_enabled.enabled) {
1145     parent_task_info = __ompt_get_task_info_object(0);
1146     parent_task_info->frame.enter_frame = OMPT_GET_FRAME_ADDRESS(1);
1147     OMPT_STORE_RETURN_ADDRESS(gtid);
1148   }
1149 #endif
1150   if (__kmpc_ok_to_fork(&loc) && (num_threads != 1)) {
1151     if (num_threads != 0) {
1152       __kmp_push_num_threads(&loc, gtid, num_threads);
1153     }
1154     if (flags != 0) {
1155       __kmp_push_proc_bind(&loc, gtid, (kmp_proc_bind_t)flags);
1156     }
1157     __kmp_GOMP_fork_call(&loc, gtid, task,
1158                          (microtask_t)__kmp_GOMP_microtask_wrapper, 2, task,
1159                          data);
1160   } else {
1161     __kmp_GOMP_serialized_parallel(&loc, gtid, task);
1162   }
1163 #if OMPT_SUPPORT
1164   if (ompt_enabled.enabled) {
1165     task_info = __ompt_get_task_info_object(0);
1166     task_info->frame.exit_frame = OMPT_GET_FRAME_ADDRESS(0);
1167   }
1168 #endif
1169   task(data);
1170 #if OMPT_SUPPORT
1171   if (ompt_enabled.enabled) {
1172     OMPT_STORE_RETURN_ADDRESS(gtid);
1173   }
1174 #endif
1175   KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_END)();
1176 #if OMPT_SUPPORT
1177   if (ompt_enabled.enabled) {
1178     task_info->frame.exit_frame = NULL;
1179     parent_task_info->frame.enter_frame = NULL;
1180   }
1181 #endif
1182 }
1183 
1184 void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_SECTIONS)(void (*task)(void *),
1185                                                           void *data,
1186                                                           unsigned num_threads,
1187                                                           unsigned count,
1188                                                           unsigned flags) {
1189   int gtid = __kmp_entry_gtid();
1190   MKLOC(loc, "GOMP_parallel_sections");
1191   KA_TRACE(20, ("GOMP_parallel_sections: T#%d\n", gtid));
1192 
1193 #if OMPT_SUPPORT
1194   OMPT_STORE_RETURN_ADDRESS(gtid);
1195 #endif
1196 
1197   if (__kmpc_ok_to_fork(&loc) && (num_threads != 1)) {
1198     if (num_threads != 0) {
1199       __kmp_push_num_threads(&loc, gtid, num_threads);
1200     }
1201     if (flags != 0) {
1202       __kmp_push_proc_bind(&loc, gtid, (kmp_proc_bind_t)flags);
1203     }
1204     __kmp_GOMP_fork_call(&loc, gtid, task,
1205                          (microtask_t)__kmp_GOMP_parallel_microtask_wrapper, 9,
1206                          task, data, num_threads, &loc, kmp_nm_dynamic_chunked,
1207                          (kmp_int)1, (kmp_int)count, (kmp_int)1, (kmp_int)1);
1208   } else {
1209     __kmp_GOMP_serialized_parallel(&loc, gtid, task);
1210   }
1211 
1212 #if OMPT_SUPPORT
1213   OMPT_STORE_RETURN_ADDRESS(gtid);
1214 #endif
1215 
1216   KMP_DISPATCH_INIT(&loc, gtid, kmp_nm_dynamic_chunked, 1, count, 1, 1, TRUE);
1217 
1218   task(data);
1219   KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_END)();
1220   KA_TRACE(20, ("GOMP_parallel_sections exit: T#%d\n", gtid));
1221 }
1222 
1223 #define PARALLEL_LOOP(func, schedule, ompt_pre, ompt_post)                     \
1224   void func(void (*task)(void *), void *data, unsigned num_threads, long lb,   \
1225             long ub, long str, long chunk_sz, unsigned flags) {                \
1226     int gtid = __kmp_entry_gtid();                                             \
1227     MKLOC(loc, #func);                                                         \
1228     KA_TRACE(20,                                                               \
1229              (#func ": T#%d, lb 0x%lx, ub 0x%lx, str 0x%lx, chunk_sz 0x%lx\n", \
1230               gtid, lb, ub, str, chunk_sz));                                   \
1231                                                                                \
1232     ompt_pre();                                                                \
1233     if (__kmpc_ok_to_fork(&loc) && (num_threads != 1)) {                       \
1234       if (num_threads != 0) {                                                  \
1235         __kmp_push_num_threads(&loc, gtid, num_threads);                       \
1236       }                                                                        \
1237       if (flags != 0) {                                                        \
1238         __kmp_push_proc_bind(&loc, gtid, (kmp_proc_bind_t)flags);              \
1239       }                                                                        \
1240       __kmp_GOMP_fork_call(&loc, gtid, task,                                   \
1241                            (microtask_t)__kmp_GOMP_parallel_microtask_wrapper, \
1242                            9, task, data, num_threads, &loc, (schedule), lb,   \
1243                            (str > 0) ? (ub - 1) : (ub + 1), str, chunk_sz);    \
1244     } else {                                                                   \
1245       __kmp_GOMP_serialized_parallel(&loc, gtid, task);                        \
1246     }                                                                          \
1247                                                                                \
1248     IF_OMPT_SUPPORT(OMPT_STORE_RETURN_ADDRESS(gtid);)                          \
1249     KMP_DISPATCH_INIT(&loc, gtid, (schedule), lb,                              \
1250                       (str > 0) ? (ub - 1) : (ub + 1), str, chunk_sz,          \
1251                       (schedule) != kmp_sch_static);                           \
1252     task(data);                                                                \
1253     KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_END)();                         \
1254     ompt_post();                                                               \
1255                                                                                \
1256     KA_TRACE(20, (#func " exit: T#%d\n", gtid));                               \
1257   }
1258 
1259 PARALLEL_LOOP(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_LOOP_STATIC),
1260               kmp_sch_static, OMPT_LOOP_PRE, OMPT_LOOP_POST)
1261 PARALLEL_LOOP(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_LOOP_DYNAMIC),
1262               kmp_sch_dynamic_chunked, OMPT_LOOP_PRE, OMPT_LOOP_POST)
1263 PARALLEL_LOOP(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_LOOP_GUIDED),
1264               kmp_sch_guided_chunked, OMPT_LOOP_PRE, OMPT_LOOP_POST)
1265 PARALLEL_LOOP(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_PARALLEL_LOOP_RUNTIME),
1266               kmp_sch_runtime, OMPT_LOOP_PRE, OMPT_LOOP_POST)
1267 
1268 void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_TASKGROUP_START)(void) {
1269   int gtid = __kmp_entry_gtid();
1270   MKLOC(loc, "GOMP_taskgroup_start");
1271   KA_TRACE(20, ("GOMP_taskgroup_start: T#%d\n", gtid));
1272 
1273 #if OMPT_SUPPORT
1274   if (ompt_enabled.enabled)
1275     OMPT_STORE_RETURN_ADDRESS(gtid);
1276 #endif
1277 
1278   __kmpc_taskgroup(&loc, gtid);
1279 
1280   return;
1281 }
1282 
1283 void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_TASKGROUP_END)(void) {
1284   int gtid = __kmp_get_gtid();
1285   MKLOC(loc, "GOMP_taskgroup_end");
1286   KA_TRACE(20, ("GOMP_taskgroup_end: T#%d\n", gtid));
1287 
1288 #if OMPT_SUPPORT
1289   if (ompt_enabled.enabled)
1290     OMPT_STORE_RETURN_ADDRESS(gtid);
1291 #endif
1292 
1293   __kmpc_end_taskgroup(&loc, gtid);
1294 
1295   return;
1296 }
1297 
1298 #ifndef KMP_DEBUG
1299 static
1300 #endif /* KMP_DEBUG */
1301     kmp_int32
1302     __kmp_gomp_to_omp_cancellation_kind(int gomp_kind) {
1303   kmp_int32 cncl_kind = 0;
1304   switch (gomp_kind) {
1305   case 1:
1306     cncl_kind = cancel_parallel;
1307     break;
1308   case 2:
1309     cncl_kind = cancel_loop;
1310     break;
1311   case 4:
1312     cncl_kind = cancel_sections;
1313     break;
1314   case 8:
1315     cncl_kind = cancel_taskgroup;
1316     break;
1317   }
1318   return cncl_kind;
1319 }
1320 
1321 bool KMP_EXPAND_NAME(KMP_API_NAME_GOMP_CANCELLATION_POINT)(int which) {
1322   if (__kmp_omp_cancellation) {
1323     KMP_FATAL(NoGompCancellation);
1324   }
1325   int gtid = __kmp_get_gtid();
1326   MKLOC(loc, "GOMP_cancellation_point");
1327   KA_TRACE(20, ("GOMP_cancellation_point: T#%d\n", gtid));
1328 
1329   kmp_int32 cncl_kind = __kmp_gomp_to_omp_cancellation_kind(which);
1330 
1331   return __kmpc_cancellationpoint(&loc, gtid, cncl_kind);
1332 }
1333 
1334 bool KMP_EXPAND_NAME(KMP_API_NAME_GOMP_BARRIER_CANCEL)(void) {
1335   if (__kmp_omp_cancellation) {
1336     KMP_FATAL(NoGompCancellation);
1337   }
1338   KMP_FATAL(NoGompCancellation);
1339   int gtid = __kmp_get_gtid();
1340   MKLOC(loc, "GOMP_barrier_cancel");
1341   KA_TRACE(20, ("GOMP_barrier_cancel: T#%d\n", gtid));
1342 
1343   return __kmpc_cancel_barrier(&loc, gtid);
1344 }
1345 
1346 bool KMP_EXPAND_NAME(KMP_API_NAME_GOMP_CANCEL)(int which, bool do_cancel) {
1347   if (__kmp_omp_cancellation) {
1348     KMP_FATAL(NoGompCancellation);
1349   } else {
1350     return FALSE;
1351   }
1352 
1353   int gtid = __kmp_get_gtid();
1354   MKLOC(loc, "GOMP_cancel");
1355   KA_TRACE(20, ("GOMP_cancel: T#%d\n", gtid));
1356 
1357   kmp_int32 cncl_kind = __kmp_gomp_to_omp_cancellation_kind(which);
1358 
1359   if (do_cancel == FALSE) {
1360     return KMP_EXPAND_NAME(KMP_API_NAME_GOMP_CANCELLATION_POINT)(which);
1361   } else {
1362     return __kmpc_cancel(&loc, gtid, cncl_kind);
1363   }
1364 }
1365 
1366 bool KMP_EXPAND_NAME(KMP_API_NAME_GOMP_SECTIONS_END_CANCEL)(void) {
1367   if (__kmp_omp_cancellation) {
1368     KMP_FATAL(NoGompCancellation);
1369   }
1370   int gtid = __kmp_get_gtid();
1371   MKLOC(loc, "GOMP_sections_end_cancel");
1372   KA_TRACE(20, ("GOMP_sections_end_cancel: T#%d\n", gtid));
1373 
1374   return __kmpc_cancel_barrier(&loc, gtid);
1375 }
1376 
1377 bool KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_END_CANCEL)(void) {
1378   if (__kmp_omp_cancellation) {
1379     KMP_FATAL(NoGompCancellation);
1380   }
1381   int gtid = __kmp_get_gtid();
1382   MKLOC(loc, "GOMP_loop_end_cancel");
1383   KA_TRACE(20, ("GOMP_loop_end_cancel: T#%d\n", gtid));
1384 
1385   return __kmpc_cancel_barrier(&loc, gtid);
1386 }
1387 
1388 // All target functions are empty as of 2014-05-29
1389 void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_TARGET)(int device, void (*fn)(void *),
1390                                                const void *openmp_target,
1391                                                size_t mapnum, void **hostaddrs,
1392                                                size_t *sizes,
1393                                                unsigned char *kinds) {
1394   return;
1395 }
1396 
1397 void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_TARGET_DATA)(
1398     int device, const void *openmp_target, size_t mapnum, void **hostaddrs,
1399     size_t *sizes, unsigned char *kinds) {
1400   return;
1401 }
1402 
1403 void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_TARGET_END_DATA)(void) { return; }
1404 
1405 void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_TARGET_UPDATE)(
1406     int device, const void *openmp_target, size_t mapnum, void **hostaddrs,
1407     size_t *sizes, unsigned char *kinds) {
1408   return;
1409 }
1410 
1411 void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_TEAMS)(unsigned int num_teams,
1412                                               unsigned int thread_limit) {
1413   return;
1414 }
1415 #endif // OMP_40_ENABLED
1416 
1417 #if OMP_45_ENABLED
1418 
1419 // Task duplication function which copies src to dest (both are
1420 // preallocated task structures)
1421 static void __kmp_gomp_task_dup(kmp_task_t *dest, kmp_task_t *src,
1422                                 kmp_int32 last_private) {
1423   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(src);
1424   if (taskdata->td_copy_func) {
1425     (taskdata->td_copy_func)(dest->shareds, src->shareds);
1426   }
1427 }
1428 
1429 #ifdef __cplusplus
1430 } // extern "C"
1431 #endif
1432 
1433 template <typename T>
1434 void __GOMP_taskloop(void (*func)(void *), void *data,
1435                      void (*copy_func)(void *, void *), long arg_size,
1436                      long arg_align, unsigned gomp_flags,
1437                      unsigned long num_tasks, int priority, T start, T end,
1438                      T step) {
1439   typedef void (*p_task_dup_t)(kmp_task_t *, kmp_task_t *, kmp_int32);
1440   MKLOC(loc, "GOMP_taskloop");
1441   int sched;
1442   T *loop_bounds;
1443   int gtid = __kmp_entry_gtid();
1444   kmp_int32 flags = 0;
1445   int if_val = gomp_flags & (1u << 10);
1446   int nogroup = gomp_flags & (1u << 11);
1447   int up = gomp_flags & (1u << 8);
1448   p_task_dup_t task_dup = NULL;
1449   kmp_tasking_flags_t *input_flags = (kmp_tasking_flags_t *)&flags;
1450 #ifdef KMP_DEBUG
1451   {
1452     char *buff;
1453     buff = __kmp_str_format(
1454         "GOMP_taskloop: T#%%d: func:%%p data:%%p copy_func:%%p "
1455         "arg_size:%%ld arg_align:%%ld gomp_flags:0x%%x num_tasks:%%lu "
1456         "priority:%%d start:%%%s end:%%%s step:%%%s\n",
1457         traits_t<T>::spec, traits_t<T>::spec, traits_t<T>::spec);
1458     KA_TRACE(20, (buff, gtid, func, data, copy_func, arg_size, arg_align,
1459                   gomp_flags, num_tasks, priority, start, end, step));
1460     __kmp_str_free(&buff);
1461   }
1462 #endif
1463   KMP_ASSERT((size_t)arg_size >= 2 * sizeof(T));
1464   KMP_ASSERT(arg_align > 0);
1465   // The low-order bit is the "untied" flag
1466   if (!(gomp_flags & 1)) {
1467     input_flags->tiedness = 1;
1468   }
1469   // The second low-order bit is the "final" flag
1470   if (gomp_flags & 2) {
1471     input_flags->final = 1;
1472   }
1473   // Negative step flag
1474   if (!up) {
1475     // If step is flagged as negative, but isn't properly sign extended
1476     // Then manually sign extend it.  Could be a short, int, char embedded
1477     // in a long.  So cannot assume any cast.
1478     if (step > 0) {
1479       for (int i = sizeof(T) * CHAR_BIT - 1; i >= 0L; --i) {
1480         // break at the first 1 bit
1481         if (step & ((T)1 << i))
1482           break;
1483         step |= ((T)1 << i);
1484       }
1485     }
1486   }
1487   input_flags->native = 1;
1488   // Figure out if none/grainsize/num_tasks clause specified
1489   if (num_tasks > 0) {
1490     if (gomp_flags & (1u << 9))
1491       sched = 1; // grainsize specified
1492     else
1493       sched = 2; // num_tasks specified
1494     // neither grainsize nor num_tasks specified
1495   } else {
1496     sched = 0;
1497   }
1498 
1499   // __kmp_task_alloc() sets up all other flags
1500   kmp_task_t *task =
1501       __kmp_task_alloc(&loc, gtid, input_flags, sizeof(kmp_task_t),
1502                        arg_size + arg_align - 1, (kmp_routine_entry_t)func);
1503   kmp_taskdata_t *taskdata = KMP_TASK_TO_TASKDATA(task);
1504   taskdata->td_copy_func = copy_func;
1505   taskdata->td_size_loop_bounds = sizeof(T);
1506 
1507   // re-align shareds if needed and setup firstprivate copy constructors
1508   // through the task_dup mechanism
1509   task->shareds = (void *)((((size_t)task->shareds) + arg_align - 1) /
1510                            arg_align * arg_align);
1511   if (copy_func) {
1512     task_dup = __kmp_gomp_task_dup;
1513   }
1514   KMP_MEMCPY(task->shareds, data, arg_size);
1515 
1516   loop_bounds = (T *)task->shareds;
1517   loop_bounds[0] = start;
1518   loop_bounds[1] = end + (up ? -1 : 1);
1519   __kmpc_taskloop(&loc, gtid, task, if_val, (kmp_uint64 *)&(loop_bounds[0]),
1520                   (kmp_uint64 *)&(loop_bounds[1]), (kmp_int64)step, nogroup,
1521                   sched, (kmp_uint64)num_tasks, (void *)task_dup);
1522 }
1523 
1524 #ifdef __cplusplus
1525 extern "C" {
1526 #endif // __cplusplus
1527 
1528 void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_TASKLOOP)(
1529     void (*func)(void *), void *data, void (*copy_func)(void *, void *),
1530     long arg_size, long arg_align, unsigned gomp_flags, unsigned long num_tasks,
1531     int priority, long start, long end, long step) {
1532   __GOMP_taskloop<long>(func, data, copy_func, arg_size, arg_align, gomp_flags,
1533                         num_tasks, priority, start, end, step);
1534 }
1535 
1536 void KMP_EXPAND_NAME(KMP_API_NAME_GOMP_TASKLOOP_ULL)(
1537     void (*func)(void *), void *data, void (*copy_func)(void *, void *),
1538     long arg_size, long arg_align, unsigned gomp_flags, unsigned long num_tasks,
1539     int priority, unsigned long long start, unsigned long long end,
1540     unsigned long long step) {
1541   __GOMP_taskloop<unsigned long long>(func, data, copy_func, arg_size,
1542                                       arg_align, gomp_flags, num_tasks,
1543                                       priority, start, end, step);
1544 }
1545 
1546 #endif
1547 
1548 /* The following sections of code create aliases for the GOMP_* functions, then
1549    create versioned symbols using the assembler directive .symver. This is only
1550    pertinent for ELF .so library. The KMP_VERSION_SYMBOL macro is defined in
1551    kmp_os.h  */
1552 
1553 #ifdef KMP_USE_VERSION_SYMBOLS
1554 // GOMP_1.0 versioned symbols
1555 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_ATOMIC_END, 10, "GOMP_1.0");
1556 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_ATOMIC_START, 10, "GOMP_1.0");
1557 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_BARRIER, 10, "GOMP_1.0");
1558 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_CRITICAL_END, 10, "GOMP_1.0");
1559 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_CRITICAL_NAME_END, 10, "GOMP_1.0");
1560 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_CRITICAL_NAME_START, 10, "GOMP_1.0");
1561 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_CRITICAL_START, 10, "GOMP_1.0");
1562 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_DYNAMIC_NEXT, 10, "GOMP_1.0");
1563 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_DYNAMIC_START, 10, "GOMP_1.0");
1564 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_END, 10, "GOMP_1.0");
1565 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_END_NOWAIT, 10, "GOMP_1.0");
1566 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_GUIDED_NEXT, 10, "GOMP_1.0");
1567 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_GUIDED_START, 10, "GOMP_1.0");
1568 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ORDERED_DYNAMIC_NEXT, 10, "GOMP_1.0");
1569 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ORDERED_DYNAMIC_START, 10,
1570                    "GOMP_1.0");
1571 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ORDERED_GUIDED_NEXT, 10, "GOMP_1.0");
1572 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ORDERED_GUIDED_START, 10, "GOMP_1.0");
1573 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ORDERED_RUNTIME_NEXT, 10, "GOMP_1.0");
1574 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ORDERED_RUNTIME_START, 10,
1575                    "GOMP_1.0");
1576 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ORDERED_STATIC_NEXT, 10, "GOMP_1.0");
1577 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ORDERED_STATIC_START, 10, "GOMP_1.0");
1578 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_RUNTIME_NEXT, 10, "GOMP_1.0");
1579 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_RUNTIME_START, 10, "GOMP_1.0");
1580 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_STATIC_NEXT, 10, "GOMP_1.0");
1581 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_STATIC_START, 10, "GOMP_1.0");
1582 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_ORDERED_END, 10, "GOMP_1.0");
1583 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_ORDERED_START, 10, "GOMP_1.0");
1584 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_PARALLEL_END, 10, "GOMP_1.0");
1585 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_PARALLEL_LOOP_DYNAMIC_START, 10,
1586                    "GOMP_1.0");
1587 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_PARALLEL_LOOP_GUIDED_START, 10,
1588                    "GOMP_1.0");
1589 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_PARALLEL_LOOP_RUNTIME_START, 10,
1590                    "GOMP_1.0");
1591 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_PARALLEL_LOOP_STATIC_START, 10,
1592                    "GOMP_1.0");
1593 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_PARALLEL_SECTIONS_START, 10, "GOMP_1.0");
1594 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_PARALLEL_START, 10, "GOMP_1.0");
1595 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_SECTIONS_END, 10, "GOMP_1.0");
1596 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_SECTIONS_END_NOWAIT, 10, "GOMP_1.0");
1597 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_SECTIONS_NEXT, 10, "GOMP_1.0");
1598 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_SECTIONS_START, 10, "GOMP_1.0");
1599 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_SINGLE_COPY_END, 10, "GOMP_1.0");
1600 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_SINGLE_COPY_START, 10, "GOMP_1.0");
1601 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_SINGLE_START, 10, "GOMP_1.0");
1602 
1603 // GOMP_2.0 versioned symbols
1604 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_TASK, 20, "GOMP_2.0");
1605 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_TASKWAIT, 20, "GOMP_2.0");
1606 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_DYNAMIC_NEXT, 20, "GOMP_2.0");
1607 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_DYNAMIC_START, 20, "GOMP_2.0");
1608 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_GUIDED_NEXT, 20, "GOMP_2.0");
1609 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_GUIDED_START, 20, "GOMP_2.0");
1610 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_ORDERED_DYNAMIC_NEXT, 20,
1611                    "GOMP_2.0");
1612 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_ORDERED_DYNAMIC_START, 20,
1613                    "GOMP_2.0");
1614 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_ORDERED_GUIDED_NEXT, 20,
1615                    "GOMP_2.0");
1616 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_ORDERED_GUIDED_START, 20,
1617                    "GOMP_2.0");
1618 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_ORDERED_RUNTIME_NEXT, 20,
1619                    "GOMP_2.0");
1620 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_ORDERED_RUNTIME_START, 20,
1621                    "GOMP_2.0");
1622 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_ORDERED_STATIC_NEXT, 20,
1623                    "GOMP_2.0");
1624 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_ORDERED_STATIC_START, 20,
1625                    "GOMP_2.0");
1626 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_RUNTIME_NEXT, 20, "GOMP_2.0");
1627 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_RUNTIME_START, 20, "GOMP_2.0");
1628 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_STATIC_NEXT, 20, "GOMP_2.0");
1629 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_ULL_STATIC_START, 20, "GOMP_2.0");
1630 
1631 // GOMP_3.0 versioned symbols
1632 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_TASKYIELD, 30, "GOMP_3.0");
1633 
1634 // GOMP_4.0 versioned symbols
1635 #if OMP_40_ENABLED
1636 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_PARALLEL, 40, "GOMP_4.0");
1637 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_PARALLEL_SECTIONS, 40, "GOMP_4.0");
1638 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_PARALLEL_LOOP_DYNAMIC, 40, "GOMP_4.0");
1639 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_PARALLEL_LOOP_GUIDED, 40, "GOMP_4.0");
1640 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_PARALLEL_LOOP_RUNTIME, 40, "GOMP_4.0");
1641 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_PARALLEL_LOOP_STATIC, 40, "GOMP_4.0");
1642 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_TASKGROUP_START, 40, "GOMP_4.0");
1643 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_TASKGROUP_END, 40, "GOMP_4.0");
1644 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_BARRIER_CANCEL, 40, "GOMP_4.0");
1645 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_CANCEL, 40, "GOMP_4.0");
1646 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_CANCELLATION_POINT, 40, "GOMP_4.0");
1647 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_LOOP_END_CANCEL, 40, "GOMP_4.0");
1648 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_SECTIONS_END_CANCEL, 40, "GOMP_4.0");
1649 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_TARGET, 40, "GOMP_4.0");
1650 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_TARGET_DATA, 40, "GOMP_4.0");
1651 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_TARGET_END_DATA, 40, "GOMP_4.0");
1652 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_TARGET_UPDATE, 40, "GOMP_4.0");
1653 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_TEAMS, 40, "GOMP_4.0");
1654 #endif
1655 
1656 #if OMP_45_ENABLED
1657 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_TASKLOOP, 45, "GOMP_4.5");
1658 KMP_VERSION_SYMBOL(KMP_API_NAME_GOMP_TASKLOOP_ULL, 45, "GOMP_4.5");
1659 #endif
1660 
1661 #endif // KMP_USE_VERSION_SYMBOLS
1662 
1663 #ifdef __cplusplus
1664 } // extern "C"
1665 #endif // __cplusplus
1666