LLVM OpenMP* Runtime Library
kmp_gsupport.cpp
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_end: 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, KMP_STR(func)); \
527  KA_TRACE( \
528  20, \
529  (KMP_STR( \
530  func) ": T#%d, lb 0x%lx, ub 0x%lx, str 0x%lx, chunk_sz 0x%lx\n", \
531  gtid, lb, ub, str, chunk_sz)); \
532  \
533  if ((str > 0) ? (lb < ub) : (lb > ub)) { \
534  IF_OMPT_SUPPORT(OMPT_STORE_RETURN_ADDRESS(gtid);) \
535  KMP_DISPATCH_INIT(&loc, gtid, (schedule), lb, \
536  (str > 0) ? (ub - 1) : (ub + 1), str, chunk_sz, \
537  (schedule) != kmp_sch_static); \
538  IF_OMPT_SUPPORT(OMPT_STORE_RETURN_ADDRESS(gtid);) \
539  status = KMP_DISPATCH_NEXT(&loc, gtid, NULL, (kmp_int *)p_lb, \
540  (kmp_int *)p_ub, (kmp_int *)&stride); \
541  if (status) { \
542  KMP_DEBUG_ASSERT(stride == str); \
543  *p_ub += (str > 0) ? 1 : -1; \
544  } \
545  } else { \
546  status = 0; \
547  } \
548  \
549  KA_TRACE( \
550  20, \
551  (KMP_STR( \
552  func) " exit: T#%d, *p_lb 0x%lx, *p_ub 0x%lx, returning %d\n", \
553  gtid, *p_lb, *p_ub, status)); \
554  return status; \
555  }
556 
557 #define LOOP_RUNTIME_START(func, schedule) \
558  int func(long lb, long ub, long str, long *p_lb, long *p_ub) { \
559  int status; \
560  long stride; \
561  long chunk_sz = 0; \
562  int gtid = __kmp_entry_gtid(); \
563  MKLOC(loc, KMP_STR(func)); \
564  KA_TRACE( \
565  20, \
566  (KMP_STR(func) ": T#%d, lb 0x%lx, ub 0x%lx, str 0x%lx, chunk_sz %d\n", \
567  gtid, lb, ub, str, chunk_sz)); \
568  \
569  if ((str > 0) ? (lb < ub) : (lb > ub)) { \
570  IF_OMPT_SUPPORT(OMPT_STORE_RETURN_ADDRESS(gtid);) \
571  KMP_DISPATCH_INIT(&loc, gtid, (schedule), lb, \
572  (str > 0) ? (ub - 1) : (ub + 1), str, chunk_sz, TRUE); \
573  IF_OMPT_SUPPORT(OMPT_STORE_RETURN_ADDRESS(gtid);) \
574  status = KMP_DISPATCH_NEXT(&loc, gtid, NULL, (kmp_int *)p_lb, \
575  (kmp_int *)p_ub, (kmp_int *)&stride); \
576  if (status) { \
577  KMP_DEBUG_ASSERT(stride == str); \
578  *p_ub += (str > 0) ? 1 : -1; \
579  } \
580  } else { \
581  status = 0; \
582  } \
583  \
584  KA_TRACE( \
585  20, \
586  (KMP_STR( \
587  func) " exit: T#%d, *p_lb 0x%lx, *p_ub 0x%lx, returning %d\n", \
588  gtid, *p_lb, *p_ub, status)); \
589  return status; \
590  }
591 
592 #if OMP_45_ENABLED
593 #define KMP_DOACROSS_FINI(status, gtid) \
594  if (!status && __kmp_threads[gtid]->th.th_dispatch->th_doacross_flags) { \
595  __kmpc_doacross_fini(NULL, gtid); \
596  }
597 #else
598 #define KMP_DOACROSS_FINI(status, gtid) /* Nothing */
599 #endif
600 
601 #define LOOP_NEXT(func, fini_code) \
602  int func(long *p_lb, long *p_ub) { \
603  int status; \
604  long stride; \
605  int gtid = __kmp_get_gtid(); \
606  MKLOC(loc, KMP_STR(func)); \
607  KA_TRACE(20, (KMP_STR(func) ": T#%d\n", gtid)); \
608  \
609  IF_OMPT_SUPPORT(OMPT_STORE_RETURN_ADDRESS(gtid);) \
610  fini_code status = KMP_DISPATCH_NEXT(&loc, gtid, NULL, (kmp_int *)p_lb, \
611  (kmp_int *)p_ub, (kmp_int *)&stride); \
612  if (status) { \
613  *p_ub += (stride > 0) ? 1 : -1; \
614  } \
615  KMP_DOACROSS_FINI(status, gtid) \
616  \
617  KA_TRACE( \
618  20, \
619  (KMP_STR(func) " exit: T#%d, *p_lb 0x%lx, *p_ub 0x%lx, stride 0x%lx, " \
620  "returning %d\n", \
621  gtid, *p_lb, *p_ub, stride, status)); \
622  return status; \
623  }
624 
625 LOOP_START(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_STATIC_START), kmp_sch_static)
626 LOOP_NEXT(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_STATIC_NEXT), {})
627 LOOP_START(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_DYNAMIC_START),
628  kmp_sch_dynamic_chunked)
629 LOOP_NEXT(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_DYNAMIC_NEXT), {})
630 LOOP_START(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_GUIDED_START),
632 LOOP_NEXT(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_GUIDED_NEXT), {})
633 LOOP_RUNTIME_START(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_RUNTIME_START),
634  kmp_sch_runtime)
635 LOOP_NEXT(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_RUNTIME_NEXT), {})
636 
637 LOOP_START(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ORDERED_STATIC_START),
639 LOOP_NEXT(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ORDERED_STATIC_NEXT),
640  { KMP_DISPATCH_FINI_CHUNK(&loc, gtid); })
641 LOOP_START(KMP_EXPAND_NAME(KMP_API_NAME_GOMP_LOOP_ORDERED_DYNAMIC_START),