LLVM OpenMP* Runtime Library
kmp_csupport.cpp
1/*
2 * kmp_csupport.cpp -- kfront linkage support for OpenMP.
3 */
4
5//===----------------------------------------------------------------------===//
6//
7// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
8// See https://llvm.org/LICENSE.txt for license information.
9// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
10//
11//===----------------------------------------------------------------------===//
12
13#define __KMP_IMP
14#include "omp.h" /* extern "C" declarations of user-visible routines */
15#include "kmp.h"
16#include "kmp_error.h"
17#include "kmp_i18n.h"
18#include "kmp_itt.h"
19#include "kmp_lock.h"
20#include "kmp_stats.h"
21#include "ompt-specific.h"
22
23#define MAX_MESSAGE 512
24
25// flags will be used in future, e.g. to implement openmp_strict library
26// restrictions
27
36void __kmpc_begin(ident_t *loc, kmp_int32 flags) {
37 // By default __kmpc_begin() is no-op.
38 char *env;
39 if ((env = getenv("KMP_INITIAL_THREAD_BIND")) != NULL &&
40 __kmp_str_match_true(env)) {
41 __kmp_middle_initialize();
42 __kmp_assign_root_init_mask();
43 KC_TRACE(10, ("__kmpc_begin: middle initialization called\n"));
44 } else if (__kmp_ignore_mppbeg() == FALSE) {
45 // By default __kmp_ignore_mppbeg() returns TRUE.
46 __kmp_internal_begin();
47 KC_TRACE(10, ("__kmpc_begin: called\n"));
48 }
49}
50
59void __kmpc_end(ident_t *loc) {
60 // By default, __kmp_ignore_mppend() returns TRUE which makes __kmpc_end()
61 // call no-op. However, this can be overridden with KMP_IGNORE_MPPEND
62 // environment variable. If KMP_IGNORE_MPPEND is 0, __kmp_ignore_mppend()
63 // returns FALSE and __kmpc_end() will unregister this root (it can cause
64 // library shut down).
65 if (__kmp_ignore_mppend() == FALSE) {
66 KC_TRACE(10, ("__kmpc_end: called\n"));
67 KA_TRACE(30, ("__kmpc_end\n"));
68
69 __kmp_internal_end_thread(-1);
70 }
71#if KMP_OS_WINDOWS && OMPT_SUPPORT
72 // Normal exit process on Windows does not allow worker threads of the final
73 // parallel region to finish reporting their events, so shutting down the
74 // library here fixes the issue at least for the cases where __kmpc_end() is
75 // placed properly.
76 if (ompt_enabled.enabled)
77 __kmp_internal_end_library(__kmp_gtid_get_specific());
78#endif
79}
80
100 kmp_int32 gtid = __kmp_entry_gtid();
101
102 KC_TRACE(10, ("__kmpc_global_thread_num: T#%d\n", gtid));
103
104 return gtid;
105}
106
122 KC_TRACE(10,
123 ("__kmpc_global_num_threads: num_threads = %d\n", __kmp_all_nth));
124
125 return TCR_4(__kmp_all_nth);
126}
127
135 KC_TRACE(10, ("__kmpc_bound_thread_num: called\n"));
136 return __kmp_tid_from_gtid(__kmp_entry_gtid());
137}
138
145 KC_TRACE(10, ("__kmpc_bound_num_threads: called\n"));
146
147 return __kmp_entry_thread()->th.th_team->t.t_nproc;
148}
149
156kmp_int32 __kmpc_ok_to_fork(ident_t *loc) {
157#ifndef KMP_DEBUG
158
159 return TRUE;
160
161#else
162
163 const char *semi2;
164 const char *semi3;
165 int line_no;
166
167 if (__kmp_par_range == 0) {
168 return TRUE;
169 }
170 semi2 = loc->psource;
171 if (semi2 == NULL) {
172 return TRUE;
173 }
174 semi2 = strchr(semi2, ';');
175 if (semi2 == NULL) {
176 return TRUE;
177 }
178 semi2 = strchr(semi2 + 1, ';');
179 if (semi2 == NULL) {
180 return TRUE;
181 }
182 if (__kmp_par_range_filename[0]) {
183 const char *name = semi2 - 1;
184 while ((name > loc->psource) && (*name != '/') && (*name != ';')) {
185 name--;
186 }
187 if ((*name == '/') || (*name == ';')) {
188 name++;
189 }
190 if (strncmp(__kmp_par_range_filename, name, semi2 - name)) {
191 return __kmp_par_range < 0;
192 }
193 }
194 semi3 = strchr(semi2 + 1, ';');
195 if (__kmp_par_range_routine[0]) {
196 if ((semi3 != NULL) && (semi3 > semi2) &&
197 (strncmp(__kmp_par_range_routine, semi2 + 1, semi3 - semi2 - 1))) {
198 return __kmp_par_range < 0;
199 }
200 }
201 if (KMP_SSCANF(semi3 + 1, "%d", &line_no) == 1) {
202 if ((line_no >= __kmp_par_range_lb) && (line_no <= __kmp_par_range_ub)) {
203 return __kmp_par_range > 0;
204 }
205 return __kmp_par_range < 0;
206 }
207 return TRUE;
208
209#endif /* KMP_DEBUG */
210}
211
219 return __kmp_entry_thread()->th.th_root->r.r_active;
220}
221
231void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid,
232 kmp_int32 num_threads) {
233 KA_TRACE(20, ("__kmpc_push_num_threads: enter T#%d num_threads=%d\n",
234 global_tid, num_threads));
235 __kmp_assert_valid_gtid(global_tid);
236 __kmp_push_num_threads(loc, global_tid, num_threads);
237}
238
239void __kmpc_pop_num_threads(ident_t *loc, kmp_int32 global_tid) {
240 KA_TRACE(20, ("__kmpc_pop_num_threads: enter\n"));
241 /* the num_threads are automatically popped */
242}
243
244void __kmpc_push_proc_bind(ident_t *loc, kmp_int32 global_tid,
245 kmp_int32 proc_bind) {
246 KA_TRACE(20, ("__kmpc_push_proc_bind: enter T#%d proc_bind=%d\n", global_tid,
247 proc_bind));
248 __kmp_assert_valid_gtid(global_tid);
249 __kmp_push_proc_bind(loc, global_tid, (kmp_proc_bind_t)proc_bind);
250}
251
262void __kmpc_fork_call(ident_t *loc, kmp_int32 argc, kmpc_micro microtask, ...) {
263 int gtid = __kmp_entry_gtid();
264
265#if (KMP_STATS_ENABLED)
266 // If we were in a serial region, then stop the serial timer, record
267 // the event, and start parallel region timer
268 stats_state_e previous_state = KMP_GET_THREAD_STATE();
269 if (previous_state == stats_state_e::SERIAL_REGION) {
270 KMP_EXCHANGE_PARTITIONED_TIMER(OMP_parallel_overhead);
271 } else {
272 KMP_PUSH_PARTITIONED_TIMER(OMP_parallel_overhead);
273 }
274 int inParallel = __kmpc_in_parallel(loc);
275 if (inParallel) {
276 KMP_COUNT_BLOCK(OMP_NESTED_PARALLEL);
277 } else {
278 KMP_COUNT_BLOCK(OMP_PARALLEL);
279 }
280#endif
281
282 // maybe to save thr_state is enough here
283 {
284 va_list ap;
285 va_start(ap, microtask);
286
287#if OMPT_SUPPORT
288 ompt_frame_t *ompt_frame;
289 if (ompt_enabled.enabled) {
290 kmp_info_t *master_th = __kmp_threads[gtid];
291 ompt_frame = &master_th->th.th_current_task->ompt_task_info.frame;
292 ompt_frame->enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
293 }
294 OMPT_STORE_RETURN_ADDRESS(gtid);
295#endif
296
297#if INCLUDE_SSC_MARKS
298 SSC_MARK_FORKING();
299#endif
300 __kmp_fork_call(loc, gtid, fork_context_intel, argc,
301 VOLATILE_CAST(microtask_t) microtask, // "wrapped" task
302 VOLATILE_CAST(launch_t) __kmp_invoke_task_func,
303 kmp_va_addr_of(ap));
304#if INCLUDE_SSC_MARKS
305 SSC_MARK_JOINING();
306#endif
307 __kmp_join_call(loc, gtid
308#if OMPT_SUPPORT
309 ,
310 fork_context_intel
311#endif
312 );
313
314 va_end(ap);
315
316#if OMPT_SUPPORT
317 if (ompt_enabled.enabled) {
318 ompt_frame->enter_frame = ompt_data_none;
319 }
320#endif
321 }
322
323#if KMP_STATS_ENABLED
324 if (previous_state == stats_state_e::SERIAL_REGION) {
325 KMP_EXCHANGE_PARTITIONED_TIMER(OMP_serial);
326 KMP_SET_THREAD_STATE(previous_state);
327 } else {
328 KMP_POP_PARTITIONED_TIMER();
329 }
330#endif // KMP_STATS_ENABLED
331}
332
344void __kmpc_push_num_teams(ident_t *loc, kmp_int32 global_tid,
345 kmp_int32 num_teams, kmp_int32 num_threads) {
346 KA_TRACE(20,
347 ("__kmpc_push_num_teams: enter T#%d num_teams=%d num_threads=%d\n",
348 global_tid, num_teams, num_threads));
349 __kmp_assert_valid_gtid(global_tid);
350 __kmp_push_num_teams(loc, global_tid, num_teams, num_threads);
351}
352
369void __kmpc_push_num_teams_51(ident_t *loc, kmp_int32 global_tid,
370 kmp_int32 num_teams_lb, kmp_int32 num_teams_ub,
371 kmp_int32 num_threads) {
372 KA_TRACE(20, ("__kmpc_push_num_teams_51: enter T#%d num_teams_lb=%d"
373 " num_teams_ub=%d num_threads=%d\n",
374 global_tid, num_teams_lb, num_teams_ub, num_threads));
375 __kmp_assert_valid_gtid(global_tid);
376 __kmp_push_num_teams_51(loc, global_tid, num_teams_lb, num_teams_ub,
377 num_threads);
378}
379
390void __kmpc_fork_teams(ident_t *loc, kmp_int32 argc, kmpc_micro microtask,
391 ...) {
392 int gtid = __kmp_entry_gtid();
393 kmp_info_t *this_thr = __kmp_threads[gtid];
394 va_list ap;
395 va_start(ap, microtask);
396
397#if KMP_STATS_ENABLED
398 KMP_COUNT_BLOCK(OMP_TEAMS);
399 stats_state_e previous_state = KMP_GET_THREAD_STATE();
400 if (previous_state == stats_state_e::SERIAL_REGION) {
401 KMP_EXCHANGE_PARTITIONED_TIMER(OMP_teams_overhead);
402 } else {
403 KMP_PUSH_PARTITIONED_TIMER(OMP_teams_overhead);
404 }
405#endif
406
407 // remember teams entry point and nesting level
408 this_thr->th.th_teams_microtask = microtask;
409 this_thr->th.th_teams_level =
410 this_thr->th.th_team->t.t_level; // AC: can be >0 on host
411
412#if OMPT_SUPPORT
413 kmp_team_t *parent_team = this_thr->th.th_team;
414 int tid = __kmp_tid_from_gtid(gtid);
415 if (ompt_enabled.enabled) {
416 parent_team->t.t_implicit_task_taskdata[tid]
417 .ompt_task_info.frame.enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
418 }
419 OMPT_STORE_RETURN_ADDRESS(gtid);
420#endif
421
422 // check if __kmpc_push_num_teams called, set default number of teams
423 // otherwise
424 if (this_thr->th.th_teams_size.nteams == 0) {
425 __kmp_push_num_teams(loc, gtid, 0, 0);
426 }
427 KMP_DEBUG_ASSERT(this_thr->th.th_set_nproc >= 1);
428 KMP_DEBUG_ASSERT(this_thr->th.th_teams_size.nteams >= 1);
429 KMP_DEBUG_ASSERT(this_thr->th.th_teams_size.nth >= 1);
430
431 __kmp_fork_call(
432 loc, gtid, fork_context_intel, argc,
433 VOLATILE_CAST(microtask_t) __kmp_teams_master, // "wrapped" task
434 VOLATILE_CAST(launch_t) __kmp_invoke_teams_master, kmp_va_addr_of(ap));
435 __kmp_join_call(loc, gtid
436#if OMPT_SUPPORT
437 ,
438 fork_context_intel
439#endif
440 );
441
442 // Pop current CG root off list
443 KMP_DEBUG_ASSERT(this_thr->th.th_cg_roots);
444 kmp_cg_root_t *tmp = this_thr->th.th_cg_roots;
445 this_thr->th.th_cg_roots = tmp->up;
446 KA_TRACE(100, ("__kmpc_fork_teams: Thread %p popping node %p and moving up"
447 " to node %p. cg_nthreads was %d\n",
448 this_thr, tmp, this_thr->th.th_cg_roots, tmp->cg_nthreads));
449 KMP_DEBUG_ASSERT(tmp->cg_nthreads);
450 int i = tmp->cg_nthreads--;
451 if (i == 1) { // check is we are the last thread in CG (not always the case)
452 __kmp_free(tmp);
453 }
454 // Restore current task's thread_limit from CG root
455 KMP_DEBUG_ASSERT(this_thr->th.th_cg_roots);
456 this_thr->th.th_current_task->td_icvs.thread_limit =
457 this_thr->th.th_cg_roots->cg_thread_limit;
458
459 this_thr->th.th_teams_microtask = NULL;
460 this_thr->th.th_teams_level = 0;
461 *(kmp_int64 *)(&this_thr->th.th_teams_size) = 0L;
462 va_end(ap);
463#if KMP_STATS_ENABLED
464 if (previous_state == stats_state_e::SERIAL_REGION) {
465 KMP_EXCHANGE_PARTITIONED_TIMER(OMP_serial);
466 KMP_SET_THREAD_STATE(previous_state);
467 } else {
468 KMP_POP_PARTITIONED_TIMER();
469 }
470#endif // KMP_STATS_ENABLED
471}
472
473// I don't think this function should ever have been exported.
474// The __kmpc_ prefix was misapplied. I'm fairly certain that no generated
475// openmp code ever called it, but it's been exported from the RTL for so
476// long that I'm afraid to remove the definition.
477int __kmpc_invoke_task_func(int gtid) { return __kmp_invoke_task_func(gtid); }
478
491void __kmpc_serialized_parallel(ident_t *loc, kmp_int32 global_tid) {
492 // The implementation is now in kmp_runtime.cpp so that it can share static
493 // functions with kmp_fork_call since the tasks to be done are similar in
494 // each case.
495 __kmp_assert_valid_gtid(global_tid);
496#if OMPT_SUPPORT
497 OMPT_STORE_RETURN_ADDRESS(global_tid);
498#endif
499 __kmp_serialized_parallel(loc, global_tid);
500}
501
509void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32 global_tid) {
510 kmp_internal_control_t *top;
511 kmp_info_t *this_thr;
512 kmp_team_t *serial_team;
513
514 KC_TRACE(10,
515 ("__kmpc_end_serialized_parallel: called by T#%d\n", global_tid));
516
517 /* skip all this code for autopar serialized loops since it results in
518 unacceptable overhead */
519 if (loc != NULL && (loc->flags & KMP_IDENT_AUTOPAR))
520 return;
521
522 // Not autopar code
523 __kmp_assert_valid_gtid(global_tid);
524 if (!TCR_4(__kmp_init_parallel))
525 __kmp_parallel_initialize();
526
527 __kmp_resume_if_soft_paused();
528
529 this_thr = __kmp_threads[global_tid];
530 serial_team = this_thr->th.th_serial_team;
531
532 kmp_task_team_t *task_team = this_thr->th.th_task_team;
533 // we need to wait for the proxy tasks before finishing the thread
534 if (task_team != NULL && (task_team->tt.tt_found_proxy_tasks ||
535 task_team->tt.tt_hidden_helper_task_encountered))
536 __kmp_task_team_wait(this_thr, serial_team USE_ITT_BUILD_ARG(NULL));
537
538 KMP_MB();
539 KMP_DEBUG_ASSERT(serial_team);
540 KMP_ASSERT(serial_team->t.t_serialized);
541 KMP_DEBUG_ASSERT(this_thr->th.th_team == serial_team);
542 KMP_DEBUG_ASSERT(serial_team != this_thr->th.th_root->r.r_root_team);
543 KMP_DEBUG_ASSERT(serial_team->t.t_threads);
544 KMP_DEBUG_ASSERT(serial_team->t.t_threads[0] == this_thr);
545
546#if OMPT_SUPPORT
547 if (ompt_enabled.enabled &&
548 this_thr->th.ompt_thread_info.state != ompt_state_overhead) {
549 OMPT_CUR_TASK_INFO(this_thr)->frame.exit_frame = ompt_data_none;
550 if (ompt_enabled.ompt_callback_implicit_task) {
551 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
552 ompt_scope_end, NULL, OMPT_CUR_TASK_DATA(this_thr), 1,
553 OMPT_CUR_TASK_INFO(this_thr)->thread_num, ompt_task_implicit);
554 }
555
556 // reset clear the task id only after unlinking the task
557 ompt_data_t *parent_task_data;
558 __ompt_get_task_info_internal(1, NULL, &parent_task_data, NULL, NULL, NULL);
559
560 if (ompt_enabled.ompt_callback_parallel_end) {
561 ompt_callbacks.ompt_callback(ompt_callback_parallel_end)(
562 &(serial_team->t.ompt_team_info.parallel_data), parent_task_data,
563 ompt_parallel_invoker_program | ompt_parallel_team,
564 OMPT_LOAD_RETURN_ADDRESS(global_tid));
565 }
566 __ompt_lw_taskteam_unlink(this_thr);
567 this_thr->th.ompt_thread_info.state = ompt_state_overhead;
568 }
569#endif
570
571 /* If necessary, pop the internal control stack values and replace the team
572 * values */
573 top = serial_team->t.t_control_stack_top;
574 if (top && top->serial_nesting_level == serial_team->t.t_serialized) {
575 copy_icvs(&serial_team->t.t_threads[0]->th.th_current_task->td_icvs, top);
576 serial_team->t.t_control_stack_top = top->next;
577 __kmp_free(top);
578 }
579
580 /* pop dispatch buffers stack */
581 KMP_DEBUG_ASSERT(serial_team->t.t_dispatch->th_disp_buffer);
582 {
583 dispatch_private_info_t *disp_buffer =
584 serial_team->t.t_dispatch->th_disp_buffer;
585 serial_team->t.t_dispatch->th_disp_buffer =
586 serial_team->t.t_dispatch->th_disp_buffer->next;
587 __kmp_free(disp_buffer);
588 }
589 this_thr->th.th_def_allocator = serial_team->t.t_def_allocator; // restore
590
591 --serial_team->t.t_serialized;
592 if (serial_team->t.t_serialized == 0) {
593
594 /* return to the parallel section */
595
596#if KMP_ARCH_X86 || KMP_ARCH_X86_64
597 if (__kmp_inherit_fp_control && serial_team->t.t_fp_control_saved) {
598 __kmp_clear_x87_fpu_status_word();
599 __kmp_load_x87_fpu_control_word(&serial_team->t.t_x87_fpu_control_word);
600 __kmp_load_mxcsr(&serial_team->t.t_mxcsr);
601 }
602#endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
603
604 __kmp_pop_current_task_from_thread(this_thr);
605#if OMPD_SUPPORT
606 if (ompd_state & OMPD_ENABLE_BP)
607 ompd_bp_parallel_end();
608#endif
609
610 this_thr->th.th_team = serial_team->t.t_parent;
611 this_thr->th.th_info.ds.ds_tid = serial_team->t.t_master_tid;
612
613 /* restore values cached in the thread */
614 this_thr->th.th_team_nproc = serial_team->t.t_parent->t.t_nproc; /* JPH */
615 this_thr->th.th_team_master =
616 serial_team->t.t_parent->t.t_threads[0]; /* JPH */
617 this_thr->th.th_team_serialized = this_thr->th.th_team->t.t_serialized;
618
619 /* TODO the below shouldn't need to be adjusted for serialized teams */
620 this_thr->th.th_dispatch =
621 &this_thr->th.th_team->t.t_dispatch[serial_team->t.t_master_tid];
622
623 KMP_ASSERT(this_thr->th.th_current_task->td_flags.executing == 0);
624 this_thr->th.th_current_task->td_flags.executing = 1;
625
626 if (__kmp_tasking_mode != tskm_immediate_exec) {
627 // Copy the task team from the new child / old parent team to the thread.
628 this_thr->th.th_task_team =
629 this_thr->th.th_team->t.t_task_team[this_thr->th.th_task_state];
630 KA_TRACE(20,
631 ("__kmpc_end_serialized_parallel: T#%d restoring task_team %p / "
632 "team %p\n",
633 global_tid, this_thr->th.th_task_team, this_thr->th.th_team));
634 }
635#if KMP_AFFINITY_SUPPORTED
636 if (this_thr->th.th_team->t.t_level == 0 && __kmp_affin_reset) {
637 __kmp_reset_root_init_mask(global_tid);
638 }
639#endif
640 } else {
641 if (__kmp_tasking_mode != tskm_immediate_exec) {
642 KA_TRACE(20, ("__kmpc_end_serialized_parallel: T#%d decreasing nesting "
643 "depth of serial team %p to %d\n",
644 global_tid, serial_team, serial_team->t.t_serialized));
645 }
646 }
647
648 serial_team->t.t_level--;
649 if (__kmp_env_consistency_check)
650 __kmp_pop_parallel(global_tid, NULL);
651#if OMPT_SUPPORT
652 if (ompt_enabled.enabled)
653 this_thr->th.ompt_thread_info.state =
654 ((this_thr->th.th_team_serialized) ? ompt_state_work_serial
655 : ompt_state_work_parallel);
656#endif
657}
658
668 KC_TRACE(10, ("__kmpc_flush: called\n"));
669
670 /* need explicit __mf() here since use volatile instead in library */
671 KMP_MFENCE(); /* Flush all pending memory write invalidates. */
672
673#if OMPT_SUPPORT && OMPT_OPTIONAL
674 if (ompt_enabled.ompt_callback_flush) {
675 ompt_callbacks.ompt_callback(ompt_callback_flush)(
676 __ompt_get_thread_data_internal(), OMPT_GET_RETURN_ADDRESS(0));
677 }
678#endif
679}
680
681/* -------------------------------------------------------------------------- */
689void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid) {
690 KMP_COUNT_BLOCK(OMP_BARRIER);
691 KC_TRACE(10, ("__kmpc_barrier: called T#%d\n", global_tid));
692 __kmp_assert_valid_gtid(global_tid);
693
694 if (!TCR_4(__kmp_init_parallel))
695 __kmp_parallel_initialize();
696
697 __kmp_resume_if_soft_paused();
698
699 if (__kmp_env_consistency_check) {
700 if (loc == 0) {
701 KMP_WARNING(ConstructIdentInvalid); // ??? What does it mean for the user?
702 }
703 __kmp_check_barrier(global_tid, ct_barrier, loc);
704 }
705
706#if OMPT_SUPPORT
707 ompt_frame_t *ompt_frame;
708 if (ompt_enabled.enabled) {
709 __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
710 if (ompt_frame->enter_frame.ptr == NULL)
711 ompt_frame->enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
712 }
713 OMPT_STORE_RETURN_ADDRESS(global_tid);
714#endif
715 __kmp_threads[global_tid]->th.th_ident = loc;
716 // TODO: explicit barrier_wait_id:
717 // this function is called when 'barrier' directive is present or
718 // implicit barrier at the end of a worksharing construct.
719 // 1) better to add a per-thread barrier counter to a thread data structure
720 // 2) set to 0 when a new team is created
721 // 4) no sync is required
722
723 __kmp_barrier(bs_plain_barrier, global_tid, FALSE, 0, NULL, NULL);
724#if OMPT_SUPPORT && OMPT_OPTIONAL
725 if (ompt_enabled.enabled) {
726 ompt_frame->enter_frame = ompt_data_none;
727 }
728#endif
729}
730
731/* The BARRIER for a MASTER section is always explicit */
738kmp_int32 __kmpc_master(ident_t *loc, kmp_int32 global_tid) {
739 int status = 0;
740
741 KC_TRACE(10, ("__kmpc_master: called T#%d\n", global_tid));
742 __kmp_assert_valid_gtid(global_tid);
743
744 if (!TCR_4(__kmp_init_parallel))
745 __kmp_parallel_initialize();
746
747 __kmp_resume_if_soft_paused();
748
749 if (KMP_MASTER_GTID(global_tid)) {
750 KMP_COUNT_BLOCK(OMP_MASTER);
751 KMP_PUSH_PARTITIONED_TIMER(OMP_master);
752 status = 1;
753 }
754
755#if OMPT_SUPPORT && OMPT_OPTIONAL
756 if (status) {
757 if (ompt_enabled.ompt_callback_masked) {
758 kmp_info_t *this_thr = __kmp_threads[global_tid];
759 kmp_team_t *team = this_thr->th.th_team;
760
761 int tid = __kmp_tid_from_gtid(global_tid);
762 ompt_callbacks.ompt_callback(ompt_callback_masked)(
763 ompt_scope_begin, &(team->t.ompt_team_info.parallel_data),
764 &(team->t.t_implicit_task_taskdata[tid].ompt_task_info.task_data),
765 OMPT_GET_RETURN_ADDRESS(0));
766 }
767 }
768#endif
769
770 if (__kmp_env_consistency_check) {
771#if KMP_USE_DYNAMIC_LOCK
772 if (status)
773 __kmp_push_sync(global_tid, ct_master, loc, NULL, 0);
774 else
775 __kmp_check_sync(global_tid, ct_master, loc, NULL, 0);
776#else
777 if (status)
778 __kmp_push_sync(global_tid, ct_master, loc, NULL);
779 else
780 __kmp_check_sync(global_tid, ct_master, loc, NULL);
781#endif
782 }
783
784 return status;
785}
786
795void __kmpc_end_master(ident_t *loc, kmp_int32 global_tid) {
796 KC_TRACE(10, ("__kmpc_end_master: called T#%d\n", global_tid));
797 __kmp_assert_valid_gtid(global_tid);
798 KMP_DEBUG_ASSERT(KMP_MASTER_GTID(global_tid));
799 KMP_POP_PARTITIONED_TIMER();
800
801#if OMPT_SUPPORT && OMPT_OPTIONAL
802 kmp_info_t *this_thr = __kmp_threads[global_tid];
803 kmp_team_t *team = this_thr->th.th_team;
804 if (ompt_enabled.ompt_callback_masked) {
805 int tid = __kmp_tid_from_gtid(global_tid);
806 ompt_callbacks.ompt_callback(ompt_callback_masked)(
807 ompt_scope_end, &(team->t.ompt_team_info.parallel_data),
808 &(team->t.t_implicit_task_taskdata[tid].ompt_task_info.task_data),
809 OMPT_GET_RETURN_ADDRESS(0));
810 }
811#endif
812
813 if (__kmp_env_consistency_check) {
814 if (KMP_MASTER_GTID(global_tid))
815 __kmp_pop_sync(global_tid, ct_master, loc);
816 }
817}
818
827kmp_int32 __kmpc_masked(ident_t *loc, kmp_int32 global_tid, kmp_int32 filter) {
828 int status = 0;
829 int tid;
830 KC_TRACE(10, ("__kmpc_masked: called T#%d\n", global_tid));
831 __kmp_assert_valid_gtid(global_tid);
832
833 if (!TCR_4(__kmp_init_parallel))
834 __kmp_parallel_initialize();
835
836 __kmp_resume_if_soft_paused();
837
838 tid = __kmp_tid_from_gtid(global_tid);
839 if (tid == filter) {
840 KMP_COUNT_BLOCK(OMP_MASKED);
841 KMP_PUSH_PARTITIONED_TIMER(OMP_masked);
842 status = 1;
843 }
844
845#if OMPT_SUPPORT && OMPT_OPTIONAL
846 if (status) {
847 if (ompt_enabled.ompt_callback_masked) {
848 kmp_info_t *this_thr = __kmp_threads[global_tid];
849 kmp_team_t *team = this_thr->th.th_team;
850 ompt_callbacks.ompt_callback(ompt_callback_masked)(
851 ompt_scope_begin, &(team->t.ompt_team_info.parallel_data),
852 &(team->t.t_implicit_task_taskdata[tid].ompt_task_info.task_data),
853 OMPT_GET_RETURN_ADDRESS(0));
854 }
855 }
856#endif
857
858 if (__kmp_env_consistency_check) {
859#if KMP_USE_DYNAMIC_LOCK
860 if (status)
861 __kmp_push_sync(global_tid, ct_masked, loc, NULL, 0);
862 else
863 __kmp_check_sync(global_tid, ct_masked, loc, NULL, 0);
864#else
865 if (status)
866 __kmp_push_sync(global_tid, ct_masked, loc, NULL);
867 else
868 __kmp_check_sync(global_tid, ct_masked, loc, NULL);
869#endif
870 }
871
872 return status;
873}
874
883void __kmpc_end_masked(ident_t *loc, kmp_int32 global_tid) {
884 KC_TRACE(10, ("__kmpc_end_masked: called T#%d\n", global_tid));
885 __kmp_assert_valid_gtid(global_tid);
886 KMP_POP_PARTITIONED_TIMER();
887
888#if OMPT_SUPPORT && OMPT_OPTIONAL
889 kmp_info_t *this_thr = __kmp_threads[global_tid];
890 kmp_team_t *team = this_thr->th.th_team;
891 if (ompt_enabled.ompt_callback_masked) {
892 int tid = __kmp_tid_from_gtid(global_tid);
893 ompt_callbacks.ompt_callback(ompt_callback_masked)(
894 ompt_scope_end, &(team->t.ompt_team_info.parallel_data),
895 &(team->t.t_implicit_task_taskdata[tid].ompt_task_info.task_data),
896 OMPT_GET_RETURN_ADDRESS(0));
897 }
898#endif
899
900 if (__kmp_env_consistency_check) {
901 __kmp_pop_sync(global_tid, ct_masked, loc);
902 }
903}
904
912void __kmpc_ordered(ident_t *loc, kmp_int32 gtid) {
913 int cid = 0;
914 kmp_info_t *th;
915 KMP_DEBUG_ASSERT(__kmp_init_serial);
916
917 KC_TRACE(10, ("__kmpc_ordered: called T#%d\n", gtid));
918 __kmp_assert_valid_gtid(gtid);
919
920 if (!TCR_4(__kmp_init_parallel))
921 __kmp_parallel_initialize();
922
923 __kmp_resume_if_soft_paused();
924
925#if USE_ITT_BUILD
926 __kmp_itt_ordered_prep(gtid);
927// TODO: ordered_wait_id
928#endif /* USE_ITT_BUILD */
929
930 th = __kmp_threads[gtid];
931
932#if OMPT_SUPPORT && OMPT_OPTIONAL
933 kmp_team_t *team;
934 ompt_wait_id_t lck;
935 void *codeptr_ra;
936 OMPT_STORE_RETURN_ADDRESS(gtid);
937 if (ompt_enabled.enabled) {
938 team = __kmp_team_from_gtid(gtid);
939 lck = (ompt_wait_id_t)(uintptr_t)&team->t.t_ordered.dt.t_value;
940 /* OMPT state update */
941 th->th.ompt_thread_info.wait_id = lck;
942 th->th.ompt_thread_info.state = ompt_state_wait_ordered;
943
944 /* OMPT event callback */
945 codeptr_ra = OMPT_LOAD_RETURN_ADDRESS(gtid);
946 if (ompt_enabled.ompt_callback_mutex_acquire) {
947 ompt_callbacks.ompt_callback(ompt_callback_mutex_acquire)(
948 ompt_mutex_ordered, omp_lock_hint_none, kmp_mutex_impl_spin, lck,
949 codeptr_ra);
950 }
951 }
952#endif
953
954 if (th->th.th_dispatch->th_deo_fcn != 0)
955 (*th->th.th_dispatch->th_deo_fcn)(&gtid, &cid, loc);
956 else
957 __kmp_parallel_deo(&gtid, &cid, loc);
958
959#if OMPT_SUPPORT && OMPT_OPTIONAL
960 if (ompt_enabled.enabled) {
961 /* OMPT state update */
962 th->th.ompt_thread_info.state = ompt_state_work_parallel;
963 th->th.ompt_thread_info.wait_id = 0;
964
965 /* OMPT event callback */
966 if (ompt_enabled.ompt_callback_mutex_acquired) {
967 ompt_callbacks.ompt_callback(ompt_callback_mutex_acquired)(
968 ompt_mutex_ordered, (ompt_wait_id_t)(uintptr_t)lck, codeptr_ra);
969 }
970 }
971#endif
972
973#if USE_ITT_BUILD
974 __kmp_itt_ordered_start(gtid);
975#endif /* USE_ITT_BUILD */
976}
977
985void __kmpc_end_ordered(ident_t *loc, kmp_int32 gtid) {
986 int cid = 0;
987 kmp_info_t *th;
988
989 KC_TRACE(10, ("__kmpc_end_ordered: called T#%d\n", gtid));
990 __kmp_assert_valid_gtid(gtid);
991
992#if USE_ITT_BUILD
993 __kmp_itt_ordered_end(gtid);
994// TODO: ordered_wait_id
995#endif /* USE_ITT_BUILD */
996
997 th = __kmp_threads[gtid];
998
999 if (th->th.th_dispatch->th_dxo_fcn != 0)
1000 (*th->th.th_dispatch->th_dxo_fcn)(&gtid, &cid, loc);
1001 else
1002 __kmp_parallel_dxo(&gtid, &cid, loc);
1003
1004#if OMPT_SUPPORT && OMPT_OPTIONAL
1005 OMPT_STORE_RETURN_ADDRESS(gtid);
1006 if (ompt_enabled.ompt_callback_mutex_released) {
1007 ompt_callbacks.ompt_callback(ompt_callback_mutex_released)(
1008 ompt_mutex_ordered,
1009 (ompt_wait_id_t)(uintptr_t)&__kmp_team_from_gtid(gtid)
1010 ->t.t_ordered.dt.t_value,
1011 OMPT_LOAD_RETURN_ADDRESS(gtid));
1012 }
1013#endif
1014}
1015
1016#if KMP_USE_DYNAMIC_LOCK
1017
1018static __forceinline void
1019__kmp_init_indirect_csptr(kmp_critical_name *crit, ident_t const *loc,
1020 kmp_int32 gtid, kmp_indirect_locktag_t tag) {
1021 // Pointer to the allocated indirect lock is written to crit, while indexing
1022 // is ignored.
1023 void *idx;
1024 kmp_indirect_lock_t **lck;
1025 lck = (kmp_indirect_lock_t **)crit;
1026 kmp_indirect_lock_t *ilk = __kmp_allocate_indirect_lock(&idx, gtid, tag);
1027 KMP_I_LOCK_FUNC(ilk, init)(ilk->lock);
1028 KMP_SET_I_LOCK_LOCATION(ilk, loc);
1029 KMP_SET_I_LOCK_FLAGS(ilk, kmp_lf_critical_section);
1030 KA_TRACE(20,
1031 ("__kmp_init_indirect_csptr: initialized indirect lock #%d\n", tag));
1032#if USE_ITT_BUILD
1033 __kmp_itt_critical_creating(ilk->lock, loc);
1034#endif
1035 int status = KMP_COMPARE_AND_STORE_PTR(lck, nullptr, ilk);
1036 if (status == 0) {
1037#if USE_ITT_BUILD
1038 __kmp_itt_critical_destroyed(ilk->lock);
1039#endif
1040 // We don't really need to destroy the unclaimed lock here since it will be
1041 // cleaned up at program exit.
1042 // KMP_D_LOCK_FUNC(&idx, destroy)((kmp_dyna_lock_t *)&idx);
1043 }
1044 KMP_DEBUG_ASSERT(*lck != NULL);
1045}
1046
1047// Fast-path acquire tas lock
1048#define KMP_ACQUIRE_TAS_LOCK(lock, gtid) \
1049 { \
1050 kmp_tas_lock_t *l = (kmp_tas_lock_t *)lock; \
1051 kmp_int32 tas_free = KMP_LOCK_FREE(tas); \
1052 kmp_int32 tas_busy = KMP_LOCK_BUSY(gtid + 1, tas); \
1053 if (KMP_ATOMIC_LD_RLX(&l->lk.poll) != tas_free || \
1054 !__kmp_atomic_compare_store_acq(&l->lk.poll, tas_free, tas_busy)) { \
1055 kmp_uint32 spins; \
1056 KMP_FSYNC_PREPARE(l); \
1057 KMP_INIT_YIELD(spins); \
1058 kmp_backoff_t backoff = __kmp_spin_backoff_params; \
1059 do { \
1060 if (TCR_4(__kmp_nth) > \
1061 (__kmp_avail_proc ? __kmp_avail_proc : __kmp_xproc)) { \
1062 KMP_YIELD(TRUE); \
1063 } else { \
1064 KMP_YIELD_SPIN(spins); \
1065 } \
1066 __kmp_spin_backoff(&backoff); \
1067 } while ( \
1068 KMP_ATOMIC_LD_RLX(&l->lk.poll) != tas_free || \
1069 !__kmp_atomic_compare_store_acq(&l->lk.poll, tas_free, tas_busy)); \
1070 } \
1071 KMP_FSYNC_ACQUIRED(l); \
1072 }
1073
1074// Fast-path test tas lock
1075#define KMP_TEST_TAS_LOCK(lock, gtid, rc) \
1076 { \
1077 kmp_tas_lock_t *l = (kmp_tas_lock_t *)lock; \
1078 kmp_int32 tas_free = KMP_LOCK_FREE(tas); \
1079 kmp_int32 tas_busy = KMP_LOCK_BUSY(gtid + 1, tas); \
1080 rc = KMP_ATOMIC_LD_RLX(&l->lk.poll) == tas_free && \
1081 __kmp_atomic_compare_store_acq(&l->lk.poll, tas_free, tas_busy); \
1082 }
1083
1084// Fast-path release tas lock
1085#define KMP_RELEASE_TAS_LOCK(lock, gtid) \
1086 { KMP_ATOMIC_ST_REL(&((kmp_tas_lock_t *)lock)->lk.poll, KMP_LOCK_FREE(tas)); }
1087
1088#if KMP_USE_FUTEX
1089
1090#include <sys/syscall.h>
1091#include <unistd.h>
1092#ifndef FUTEX_WAIT
1093#define FUTEX_WAIT 0
1094#endif
1095#ifndef FUTEX_WAKE
1096#define FUTEX_WAKE 1
1097#endif
1098
1099// Fast-path acquire futex lock
1100#define KMP_ACQUIRE_FUTEX_LOCK(lock, gtid) \
1101 { \
1102 kmp_futex_lock_t *ftx = (kmp_futex_lock_t *)lock; \
1103 kmp_int32 gtid_code = (gtid + 1) << 1; \
1104 KMP_MB(); \
1105 KMP_FSYNC_PREPARE(ftx); \
1106 kmp_int32 poll_val; \
1107 while ((poll_val = KMP_COMPARE_AND_STORE_RET32( \
1108 &(ftx->lk.poll), KMP_LOCK_FREE(futex), \
1109 KMP_LOCK_BUSY(gtid_code, futex))) != KMP_LOCK_FREE(futex)) { \
1110 kmp_int32 cond = KMP_LOCK_STRIP(poll_val) & 1; \
1111 if (!cond) { \
1112 if (!KMP_COMPARE_AND_STORE_RET32(&(ftx->lk.poll), poll_val, \
1113 poll_val | \
1114 KMP_LOCK_BUSY(1, futex))) { \
1115 continue; \
1116 } \
1117 poll_val |= KMP_LOCK_BUSY(1, futex); \
1118 } \
1119 kmp_int32 rc; \
1120 if ((rc = syscall(__NR_futex, &(ftx->lk.poll), FUTEX_WAIT, poll_val, \
1121 NULL, NULL, 0)) != 0) { \
1122 continue; \
1123 } \
1124 gtid_code |= 1; \
1125 } \
1126 KMP_FSYNC_ACQUIRED(ftx); \
1127 }
1128
1129// Fast-path test futex lock
1130#define KMP_TEST_FUTEX_LOCK(lock, gtid, rc) \
1131 { \
1132 kmp_futex_lock_t *ftx = (kmp_futex_lock_t *)lock; \
1133 if (KMP_COMPARE_AND_STORE_ACQ32(&(ftx->lk.poll), KMP_LOCK_FREE(futex), \
1134 KMP_LOCK_BUSY(gtid + 1 << 1, futex))) { \
1135 KMP_FSYNC_ACQUIRED(ftx); \
1136 rc = TRUE; \
1137 } else { \
1138 rc = FALSE; \
1139 } \
1140 }
1141
1142// Fast-path release futex lock
1143#define KMP_RELEASE_FUTEX_LOCK(lock, gtid) \
1144 { \
1145 kmp_futex_lock_t *ftx = (kmp_futex_lock_t *)lock; \
1146 KMP_MB(); \
1147 KMP_FSYNC_RELEASING(ftx); \
1148 kmp_int32 poll_val = \
1149 KMP_XCHG_FIXED32(&(ftx->lk.poll), KMP_LOCK_FREE(futex)); \
1150 if (KMP_LOCK_STRIP(poll_val) & 1) { \
1151 syscall(__NR_futex, &(ftx->lk.poll), FUTEX_WAKE, \
1152 KMP_LOCK_BUSY(1, futex), NULL, NULL, 0); \
1153 } \
1154 KMP_MB(); \
1155 KMP_YIELD_OVERSUB(); \
1156 }
1157
1158#endif // KMP_USE_FUTEX
1159
1160#else // KMP_USE_DYNAMIC_LOCK
1161
1162static kmp_user_lock_p __kmp_get_critical_section_ptr(kmp_critical_name *crit,
1163 ident_t const *loc,
1164 kmp_int32 gtid) {
1165 kmp_user_lock_p *lck_pp = (kmp_user_lock_p *)crit;
1166
1167 // Because of the double-check, the following load doesn't need to be volatile
1168 kmp_user_lock_p lck = (kmp_user_lock_p)TCR_PTR(*lck_pp);
1169
1170 if (lck == NULL) {
1171 void *idx;
1172
1173 // Allocate & initialize the lock.
1174 // Remember alloc'ed locks in table in order to free them in __kmp_cleanup()
1175 lck = __kmp_user_lock_allocate(&idx, gtid, kmp_lf_critical_section);
1176 __kmp_init_user_lock_with_checks(lck);
1177 __kmp_set_user_lock_location(lck, loc);
1178#if USE_ITT_BUILD
1179 __kmp_itt_critical_creating(lck);
1180// __kmp_itt_critical_creating() should be called *before* the first usage
1181// of underlying lock. It is the only place where we can guarantee it. There
1182// are chances the lock will destroyed with no usage, but it is not a
1183// problem, because this is not real event seen by user but rather setting
1184// name for object (lock). See more details in kmp_itt.h.
1185#endif /* USE_ITT_BUILD */
1186
1187 // Use a cmpxchg instruction to slam the start of the critical section with
1188 // the lock pointer. If another thread beat us to it, deallocate the lock,
1189 // and use the lock that the other thread allocated.
1190 int status = KMP_COMPARE_AND_STORE_PTR(lck_pp, 0, lck);
1191
1192 if (status == 0) {
1193// Deallocate the lock and reload the value.
1194#if USE_ITT_BUILD
1195 __kmp_itt_critical_destroyed(lck);
1196// Let ITT know the lock is destroyed and the same memory location may be reused
1197// for another purpose.
1198#endif /* USE_ITT_BUILD */
1199 __kmp_destroy_user_lock_with_checks(lck);
1200 __kmp_user_lock_free(&idx, gtid, lck);
1201 lck = (kmp_user_lock_p)TCR_PTR(*lck_pp);
1202 KMP_DEBUG_ASSERT(lck != NULL);
1203 }
1204 }
1205 return lck;
1206}
1207
1208#endif // KMP_USE_DYNAMIC_LOCK
1209
1220void __kmpc_critical(ident_t *loc, kmp_int32 global_tid,
1221 kmp_critical_name *crit) {
1222#if KMP_USE_DYNAMIC_LOCK
1223#if OMPT_SUPPORT && OMPT_OPTIONAL
1224 OMPT_STORE_RETURN_ADDRESS(global_tid);
1225#endif // OMPT_SUPPORT
1226 __kmpc_critical_with_hint(loc, global_tid, crit, omp_lock_hint_none);
1227#else
1228 KMP_COUNT_BLOCK(OMP_CRITICAL);
1229#if OMPT_SUPPORT && OMPT_OPTIONAL
1230 ompt_state_t prev_state = ompt_state_undefined;
1231 ompt_thread_info_t ti;
1232#endif
1233 kmp_user_lock_p lck;
1234
1235 KC_TRACE(10, ("__kmpc_critical: called T#%d\n", global_tid));
1236 __kmp_assert_valid_gtid(global_tid);
1237
1238 // TODO: add THR_OVHD_STATE
1239
1240 KMP_PUSH_PARTITIONED_TIMER(OMP_critical_wait);
1241 KMP_CHECK_USER_LOCK_INIT();
1242
1243 if ((__kmp_user_lock_kind == lk_tas) &&
1244 (sizeof(lck->tas.lk.poll) <= OMP_CRITICAL_SIZE)) {
1245 lck = (kmp_user_lock_p)crit;
1246 }
1247#if KMP_USE_FUTEX
1248 else if ((__kmp_user_lock_kind == lk_futex) &&
1249 (sizeof(lck->futex.lk.poll) <= OMP_CRITICAL_SIZE)) {
1250 lck = (kmp_user_lock_p)crit;
1251 }
1252#endif
1253 else { // ticket, queuing or drdpa
1254 lck = __kmp_get_critical_section_ptr(crit, loc, global_tid);
1255 }
1256
1257 if (__kmp_env_consistency_check)
1258 __kmp_push_sync(global_tid, ct_critical, loc, lck);
1259
1260 // since the critical directive binds to all threads, not just the current
1261 // team we have to check this even if we are in a serialized team.
1262 // also, even if we are the uber thread, we still have to conduct the lock,
1263 // as we have to contend with sibling threads.
1264
1265#if USE_ITT_BUILD
1266 __kmp_itt_critical_acquiring(lck);
1267#endif /* USE_ITT_BUILD */
1268#if OMPT_SUPPORT && OMPT_OPTIONAL
1269 OMPT_STORE_RETURN_ADDRESS(gtid);
1270 void *codeptr_ra = NULL;
1271 if (ompt_enabled.enabled) {
1272 ti = __kmp_threads[global_tid]->th.ompt_thread_info;
1273 /* OMPT state update */
1274 prev_state = ti.state;
1275 ti.wait_id = (ompt_wait_id_t)(uintptr_t)lck;
1276 ti.state = ompt_state_wait_critical;
1277
1278 /* OMPT event callback */
1279 codeptr_ra = OMPT_LOAD_RETURN_ADDRESS(gtid);
1280 if (ompt_enabled.ompt_callback_mutex_acquire) {
1281 ompt_callbacks.ompt_callback(ompt_callback_mutex_acquire)(
1282 ompt_mutex_critical, omp_lock_hint_none, __ompt_get_mutex_impl_type(),
1283 (ompt_wait_id_t)(uintptr_t)lck, codeptr_ra);
1284 }
1285 }
1286#endif
1287 // Value of 'crit' should be good for using as a critical_id of the critical
1288 // section directive.
1289 __kmp_acquire_user_lock_with_checks(lck, global_tid);
1290
1291#if USE_ITT_BUILD
1292 __kmp_itt_critical_acquired(lck);
1293#endif /* USE_ITT_BUILD */
1294#if OMPT_SUPPORT && OMPT_OPTIONAL
1295 if (ompt_enabled.enabled) {
1296 /* OMPT state update */
1297 ti.state = prev_state;
1298 ti.wait_id = 0;
1299
1300 /* OMPT event callback */
1301 if (ompt_enabled.ompt_callback_mutex_acquired) {
1302 ompt_callbacks.ompt_callback(ompt_callback_mutex_acquired)(
1303 ompt_mutex_critical, (ompt_wait_id_t)(uintptr_t)lck, codeptr_ra);
1304 }
1305 }
1306#endif
1307 KMP_POP_PARTITIONED_TIMER();
1308
1309 KMP_PUSH_PARTITIONED_TIMER(OMP_critical);
1310 KA_TRACE(15, ("__kmpc_critical: done T#%d\n", global_tid));
1311#endif // KMP_USE_DYNAMIC_LOCK
1312}
1313
1314#if KMP_USE_DYNAMIC_LOCK
1315
1316// Converts the given hint to an internal lock implementation
1317static __forceinline kmp_dyna_lockseq_t __kmp_map_hint_to_lock(uintptr_t hint) {
1318#if KMP_USE_TSX
1319#define KMP_TSX_LOCK(seq) lockseq_##seq
1320#else
1321#define KMP_TSX_LOCK(seq) __kmp_user_lock_seq
1322#endif
1323
1324#if KMP_ARCH_X86 || KMP_ARCH_X86_64
1325#define KMP_CPUINFO_RTM (__kmp_cpuinfo.flags.rtm)
1326#else
1327#define KMP_CPUINFO_RTM 0
1328#endif
1329
1330 // Hints that do not require further logic
1331 if (hint & kmp_lock_hint_hle)
1332 return KMP_TSX_LOCK(hle);
1333 if (hint & kmp_lock_hint_rtm)
1334 return KMP_CPUINFO_RTM ? KMP_TSX_LOCK(rtm_queuing) : __kmp_user_lock_seq;
1335 if (hint & kmp_lock_hint_adaptive)
1336 return KMP_CPUINFO_RTM ? KMP_TSX_LOCK(adaptive) : __kmp_user_lock_seq;
1337
1338 // Rule out conflicting hints first by returning the default lock
1339 if ((hint & omp_lock_hint_contended) && (hint & omp_lock_hint_uncontended))
1340 return __kmp_user_lock_seq;
1341 if ((hint & omp_lock_hint_speculative) &&
1342 (hint & omp_lock_hint_nonspeculative))
1343 return __kmp_user_lock_seq;
1344
1345 // Do not even consider speculation when it appears to be contended
1346 if (hint & omp_lock_hint_contended)
1347 return lockseq_queuing;
1348
1349 // Uncontended lock without speculation
1350 if ((hint & omp_lock_hint_uncontended) && !(hint & omp_lock_hint_speculative))
1351 return lockseq_tas;
1352
1353 // Use RTM lock for speculation
1354 if (hint & omp_lock_hint_speculative)
1355 return KMP_CPUINFO_RTM ? KMP_TSX_LOCK(rtm_spin) : __kmp_user_lock_seq;
1356
1357 return __kmp_user_lock_seq;
1358}
1359
1360#if OMPT_SUPPORT && OMPT_OPTIONAL
1361#if KMP_USE_DYNAMIC_LOCK
1362static kmp_mutex_impl_t
1363__ompt_get_mutex_impl_type(void *user_lock, kmp_indirect_lock_t *ilock = 0) {
1364 if (user_lock) {
1365 switch (KMP_EXTRACT_D_TAG(user_lock)) {
1366 case 0:
1367 break;
1368#if KMP_USE_FUTEX
1369 case locktag_futex:
1370 return kmp_mutex_impl_queuing;
1371#endif
1372 case locktag_tas:
1373 return kmp_mutex_impl_spin;
1374#if KMP_USE_TSX
1375 case locktag_hle:
1376 case locktag_rtm_spin:
1377 return kmp_mutex_impl_speculative;
1378#endif
1379 default:
1380 return kmp_mutex_impl_none;
1381 }
1382 ilock = KMP_LOOKUP_I_LOCK(user_lock);
1383 }
1384 KMP_ASSERT(ilock);
1385 switch (ilock->type) {
1386#if KMP_USE_TSX
1387 case locktag_adaptive:
1388 case locktag_rtm_queuing:
1389 return kmp_mutex_impl_speculative;
1390#endif
1391 case locktag_nested_tas:
1392 return kmp_mutex_impl_spin;
1393#if KMP_USE_FUTEX
1394 case locktag_nested_futex:
1395#endif
1396 case locktag_ticket:
1397 case locktag_queuing:
1398 case locktag_drdpa:
1399 case locktag_nested_ticket:
1400 case locktag_nested_queuing:
1401 case locktag_nested_drdpa:
1402 return kmp_mutex_impl_queuing;
1403 default:
1404 return kmp_mutex_impl_none;
1405 }
1406}
1407#else
1408// For locks without dynamic binding
1409static kmp_mutex_impl_t __ompt_get_mutex_impl_type() {
1410 switch (__kmp_user_lock_kind) {
1411 case lk_tas:
1412 return kmp_mutex_impl_spin;
1413#if KMP_USE_FUTEX
1414 case lk_futex:
1415#endif
1416 case lk_ticket:
1417 case lk_queuing:
1418 case lk_drdpa:
1419 return kmp_mutex_impl_queuing;
1420#if KMP_USE_TSX
1421 case lk_hle:
1422 case lk_rtm_queuing:
1423 case lk_rtm_spin:
1424 case lk_adaptive:
1425 return kmp_mutex_impl_speculative;
1426#endif
1427 default:
1428 return kmp_mutex_impl_none;
1429 }
1430}
1431#endif // KMP_USE_DYNAMIC_LOCK
1432#endif // OMPT_SUPPORT && OMPT_OPTIONAL
1433
1447void __kmpc_critical_with_hint(ident_t *loc, kmp_int32 global_tid,
1448 kmp_critical_name *crit, uint32_t hint) {
1449 KMP_COUNT_BLOCK(OMP_CRITICAL);
1450 kmp_user_lock_p lck;
1451#if OMPT_SUPPORT && OMPT_OPTIONAL
1452 ompt_state_t prev_state = ompt_state_undefined;
1453 ompt_thread_info_t ti;
1454 // This is the case, if called from __kmpc_critical:
1455 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(global_tid);
1456 if (!codeptr)
1457 codeptr = OMPT_GET_RETURN_ADDRESS(0);
1458#endif
1459
1460 KC_TRACE(10, ("__kmpc_critical: called T#%d\n", global_tid));
1461 __kmp_assert_valid_gtid(global_tid);
1462
1463 kmp_dyna_lock_t *lk = (kmp_dyna_lock_t *)crit;
1464 // Check if it is initialized.
1465 KMP_PUSH_PARTITIONED_TIMER(OMP_critical_wait);
1466 kmp_dyna_lockseq_t lockseq = __kmp_map_hint_to_lock(hint);
1467 if (*lk == 0) {
1468 if (KMP_IS_D_LOCK(lockseq)) {
1469 KMP_COMPARE_AND_STORE_ACQ32((volatile kmp_int32 *)crit, 0,
1470 KMP_GET_D_TAG(lockseq));
1471 } else {
1472 __kmp_init_indirect_csptr(crit, loc, global_tid, KMP_GET_I_TAG(lockseq));
1473 }
1474 }
1475 // Branch for accessing the actual lock object and set operation. This
1476 // branching is inevitable since this lock initialization does not follow the
1477 // normal dispatch path (lock table is not used).
1478 if (KMP_EXTRACT_D_TAG(lk) != 0) {
1479 lck = (kmp_user_lock_p)lk;
1480 if (__kmp_env_consistency_check) {
1481 __kmp_push_sync(global_tid, ct_critical, loc, lck,
1482 __kmp_map_hint_to_lock(hint));
1483 }
1484#if USE_ITT_BUILD
1485 __kmp_itt_critical_acquiring(lck);
1486#endif
1487#if OMPT_SUPPORT && OMPT_OPTIONAL
1488 if (ompt_enabled.enabled) {
1489 ti = __kmp_threads[global_tid]->th.ompt_thread_info;
1490 /* OMPT state update */
1491 prev_state = ti.state;
1492 ti.wait_id = (ompt_wait_id_t)(uintptr_t)lck;
1493 ti.state = ompt_state_wait_critical;
1494
1495 /* OMPT event callback */
1496 if (ompt_enabled.ompt_callback_mutex_acquire) {
1497 ompt_callbacks.ompt_callback(ompt_callback_mutex_acquire)(
1498 ompt_mutex_critical, (unsigned int)hint,
1499 __ompt_get_mutex_impl_type(crit), (ompt_wait_id_t)(uintptr_t)lck,
1500 codeptr);
1501 }
1502 }
1503#endif
1504#if KMP_USE_INLINED_TAS
1505 if (lockseq == lockseq_tas && !__kmp_env_consistency_check) {
1506 KMP_ACQUIRE_TAS_LOCK(lck, global_tid);
1507 } else
1508#elif KMP_USE_INLINED_FUTEX
1509 if (lockseq == lockseq_futex && !__kmp_env_consistency_check) {
1510 KMP_ACQUIRE_FUTEX_LOCK(lck, global_tid);
1511 } else
1512#endif
1513 {
1514 KMP_D_LOCK_FUNC(lk, set)(lk, global_tid);
1515 }
1516 } else {
1517 kmp_indirect_lock_t *ilk = *((kmp_indirect_lock_t **)lk);
1518 lck = ilk->lock;
1519 if (__kmp_env_consistency_check) {
1520 __kmp_push_sync(global_tid, ct_critical, loc, lck,
1521 __kmp_map_hint_to_lock(hint));
1522 }
1523#if USE_ITT_BUILD
1524 __kmp_itt_critical_acquiring(lck);
1525#endif
1526#if OMPT_SUPPORT && OMPT_OPTIONAL
1527 if (ompt_enabled.enabled) {
1528 ti = __kmp_threads[global_tid]->th.ompt_thread_info;
1529 /* OMPT state update */
1530 prev_state = ti.state;
1531 ti.wait_id = (ompt_wait_id_t)(uintptr_t)lck;
1532 ti.state = ompt_state_wait_critical;
1533
1534 /* OMPT event callback */
1535 if (ompt_enabled.ompt_callback_mutex_acquire) {
1536 ompt_callbacks.ompt_callback(ompt_callback_mutex_acquire)(
1537 ompt_mutex_critical, (unsigned int)hint,
1538 __ompt_get_mutex_impl_type(0, ilk), (ompt_wait_id_t)(uintptr_t)lck,
1539 codeptr);
1540 }
1541 }
1542#endif
1543 KMP_I_LOCK_FUNC(ilk, set)(lck, global_tid);
1544 }
1545 KMP_POP_PARTITIONED_TIMER();
1546
1547#if USE_ITT_BUILD
1548 __kmp_itt_critical_acquired(lck);
1549#endif /* USE_ITT_BUILD */
1550#if OMPT_SUPPORT && OMPT_OPTIONAL
1551 if (ompt_enabled.enabled) {
1552 /* OMPT state update */
1553 ti.state = prev_state;
1554 ti.wait_id = 0;
1555
1556 /* OMPT event callback */
1557 if (ompt_enabled.ompt_callback_mutex_acquired) {
1558 ompt_callbacks.ompt_callback(ompt_callback_mutex_acquired)(
1559 ompt_mutex_critical, (ompt_wait_id_t)(uintptr_t)lck, codeptr);
1560 }
1561 }
1562#endif
1563
1564 KMP_PUSH_PARTITIONED_TIMER(OMP_critical);
1565 KA_TRACE(15, ("__kmpc_critical: done T#%d\n", global_tid));
1566} // __kmpc_critical_with_hint
1567
1568#endif // KMP_USE_DYNAMIC_LOCK
1569
1579void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid,
1580 kmp_critical_name *crit) {
1581 kmp_user_lock_p lck;
1582
1583 KC_TRACE(10, ("__kmpc_end_critical: called T#%d\n", global_tid));
1584
1585#if KMP_USE_DYNAMIC_LOCK
1586 int locktag = KMP_EXTRACT_D_TAG(crit);
1587 if (locktag) {
1588 lck = (kmp_user_lock_p)crit;
1589 KMP_ASSERT(lck != NULL);
1590 if (__kmp_env_consistency_check) {
1591 __kmp_pop_sync(global_tid, ct_critical, loc);
1592 }
1593#if USE_ITT_BUILD
1594 __kmp_itt_critical_releasing(lck);
1595#endif
1596#if KMP_USE_INLINED_TAS
1597 if (locktag == locktag_tas && !__kmp_env_consistency_check) {
1598 KMP_RELEASE_TAS_LOCK(lck, global_tid);
1599 } else
1600#elif KMP_USE_INLINED_FUTEX
1601 if (locktag == locktag_futex && !__kmp_env_consistency_check) {
1602 KMP_RELEASE_FUTEX_LOCK(lck, global_tid);
1603 } else
1604#endif
1605 {
1606 KMP_D_LOCK_FUNC(lck, unset)((kmp_dyna_lock_t *)lck, global_tid);
1607 }
1608 } else {
1609 kmp_indirect_lock_t *ilk =
1610 (kmp_indirect_lock_t *)TCR_PTR(*((kmp_indirect_lock_t **)crit));
1611 KMP_ASSERT(ilk != NULL);
1612 lck = ilk->lock;
1613 if (__kmp_env_consistency_check) {
1614 __kmp_pop_sync(global_tid, ct_critical, loc);
1615 }
1616#if USE_ITT_BUILD
1617 __kmp_itt_critical_releasing(lck);
1618#endif
1619 KMP_I_LOCK_FUNC(ilk, unset)(lck, global_tid);
1620 }
1621
1622#else // KMP_USE_DYNAMIC_LOCK
1623
1624 if ((__kmp_user_lock_kind == lk_tas) &&
1625 (sizeof(lck->tas.lk.poll) <= OMP_CRITICAL_SIZE)) {
1626 lck = (kmp_user_lock_p)crit;
1627 }
1628#if KMP_USE_FUTEX
1629 else if ((__kmp_user_lock_kind == lk_futex) &&
1630 (sizeof(lck->futex.lk.poll) <= OMP_CRITICAL_SIZE)) {
1631 lck = (kmp_user_lock_p)crit;
1632 }
1633#endif
1634 else { // ticket, queuing or drdpa
1635 lck = (kmp_user_lock_p)TCR_PTR(*((kmp_user_lock_p *)crit));
1636 }
1637
1638 KMP_ASSERT(lck != NULL);
1639
1640 if (__kmp_env_consistency_check)
1641 __kmp_pop_sync(global_tid, ct_critical, loc);
1642
1643#if USE_ITT_BUILD
1644 __kmp_itt_critical_releasing(lck);
1645#endif /* USE_ITT_BUILD */
1646 // Value of 'crit' should be good for using as a critical_id of the critical
1647 // section directive.
1648 __kmp_release_user_lock_with_checks(lck, global_tid);
1649
1650#endif // KMP_USE_DYNAMIC_LOCK
1651
1652#if OMPT_SUPPORT && OMPT_OPTIONAL
1653 /* OMPT release event triggers after lock is released; place here to trigger
1654 * for all #if branches */
1655 OMPT_STORE_RETURN_ADDRESS(global_tid);
1656 if (ompt_enabled.ompt_callback_mutex_released) {
1657 ompt_callbacks.ompt_callback(ompt_callback_mutex_released)(
1658 ompt_mutex_critical, (ompt_wait_id_t)(uintptr_t)lck,
1659 OMPT_LOAD_RETURN_ADDRESS(0));
1660 }
1661#endif
1662
1663 KMP_POP_PARTITIONED_TIMER();
1664 KA_TRACE(15, ("__kmpc_end_critical: done T#%d\n", global_tid));
1665}
1666
1676kmp_int32 __kmpc_barrier_master(ident_t *loc, kmp_int32 global_tid) {
1677 int status;
1678 KC_TRACE(10, ("__kmpc_barrier_master: called T#%d\n", global_tid));
1679 __kmp_assert_valid_gtid(global_tid);
1680
1681 if (!TCR_4(__kmp_init_parallel))
1682 __kmp_parallel_initialize();
1683
1684 __kmp_resume_if_soft_paused();
1685
1686 if (__kmp_env_consistency_check)
1687 __kmp_check_barrier(global_tid, ct_barrier, loc);
1688
1689#if OMPT_SUPPORT
1690 ompt_frame_t *ompt_frame;
1691 if (ompt_enabled.enabled) {
1692 __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
1693 if (ompt_frame->enter_frame.ptr == NULL)
1694 ompt_frame->enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
1695 }
1696 OMPT_STORE_RETURN_ADDRESS(global_tid);
1697#endif
1698#if USE_ITT_NOTIFY
1699 __kmp_threads[global_tid]->th.th_ident = loc;
1700#endif
1701 status = __kmp_barrier(bs_plain_barrier, global_tid, TRUE, 0, NULL, NULL);
1702#if OMPT_SUPPORT && OMPT_OPTIONAL
1703 if (ompt_enabled.enabled) {
1704 ompt_frame->enter_frame = ompt_data_none;
1705 }
1706#endif
1707
1708 return (status != 0) ? 0 : 1;
1709}
1710
1720void __kmpc_end_barrier_master(ident_t *loc, kmp_int32 global_tid) {
1721 KC_TRACE(10, ("__kmpc_end_barrier_master: called T#%d\n", global_tid));
1722 __kmp_assert_valid_gtid(global_tid);
1723 __kmp_end_split_barrier(bs_plain_barrier, global_tid);
1724}
1725
1736kmp_int32 __kmpc_barrier_master_nowait(ident_t *loc, kmp_int32 global_tid) {
1737 kmp_int32 ret;
1738 KC_TRACE(10, ("__kmpc_barrier_master_nowait: called T#%d\n", global_tid));
1739 __kmp_assert_valid_gtid(global_tid);
1740
1741 if (!TCR_4(__kmp_init_parallel))
1742 __kmp_parallel_initialize();
1743
1744 __kmp_resume_if_soft_paused();
1745
1746 if (__kmp_env_consistency_check) {
1747 if (loc == 0) {
1748 KMP_WARNING(ConstructIdentInvalid); // ??? What does it mean for the user?
1749 }
1750 __kmp_check_barrier(global_tid, ct_barrier, loc);
1751 }
1752
1753#if OMPT_SUPPORT
1754 ompt_frame_t *ompt_frame;
1755 if (ompt_enabled.enabled) {
1756 __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
1757 if (ompt_frame->enter_frame.ptr == NULL)
1758 ompt_frame->enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
1759 }
1760 OMPT_STORE_RETURN_ADDRESS(global_tid);
1761#endif
1762#if USE_ITT_NOTIFY
1763 __kmp_threads[global_tid]->th.th_ident = loc;
1764#endif
1765 __kmp_barrier(bs_plain_barrier, global_tid, FALSE, 0, NULL, NULL);
1766#if OMPT_SUPPORT && OMPT_OPTIONAL
1767 if (ompt_enabled.enabled) {
1768 ompt_frame->enter_frame = ompt_data_none;
1769 }
1770#endif
1771
1772 ret = __kmpc_master(loc, global_tid);
1773
1774 if (__kmp_env_consistency_check) {
1775 /* there's no __kmpc_end_master called; so the (stats) */
1776 /* actions of __kmpc_end_master are done here */
1777 if (ret) {
1778 /* only one thread should do the pop since only */
1779 /* one did the push (see __kmpc_master()) */
1780 __kmp_pop_sync(global_tid, ct_master, loc);
1781 }
1782 }
1783
1784 return (ret);
1785}
1786
1787/* The BARRIER for a SINGLE process section is always explicit */
1799kmp_int32 __kmpc_single(ident_t *loc, kmp_int32 global_tid) {
1800 __kmp_assert_valid_gtid(global_tid);
1801 kmp_int32 rc = __kmp_enter_single(global_tid, loc, TRUE);
1802
1803 if (rc) {
1804 // We are going to execute the single statement, so we should count it.
1805 KMP_COUNT_BLOCK(OMP_SINGLE);
1806 KMP_PUSH_PARTITIONED_TIMER(OMP_single);
1807 }
1808
1809#if OMPT_SUPPORT && OMPT_OPTIONAL
1810 kmp_info_t *this_thr = __kmp_threads[global_tid];
1811 kmp_team_t *team = this_thr->th.th_team;
1812 int tid = __kmp_tid_from_gtid(global_tid);
1813
1814 if (ompt_enabled.enabled) {
1815 if (rc) {
1816 if (ompt_enabled.ompt_callback_work) {
1817 ompt_callbacks.ompt_callback(ompt_callback_work)(
1818 ompt_work_single_executor, ompt_scope_begin,
1819 &(team->t.ompt_team_info.parallel_data),
1820 &(team->t.t_implicit_task_taskdata[tid].ompt_task_info.task_data),
1821 1, OMPT_GET_RETURN_ADDRESS(0));
1822 }
1823 } else {
1824 if (ompt_enabled.ompt_callback_work) {
1825 ompt_callbacks.ompt_callback(ompt_callback_work)(
1826 ompt_work_single_other, ompt_scope_begin,
1827 &(team->t.ompt_team_info.parallel_data),
1828 &(team->t.t_implicit_task_taskdata[tid].ompt_task_info.task_data),
1829 1, OMPT_GET_RETURN_ADDRESS(0));
1830 ompt_callbacks.ompt_callback(ompt_callback_work)(
1831 ompt_work_single_other, ompt_scope_end,
1832 &(team->t.ompt_team_info.parallel_data),
1833 &(team->t.t_implicit_task_taskdata[tid].ompt_task_info.task_data),
1834 1, OMPT_GET_RETURN_ADDRESS(0));
1835 }
1836 }
1837 }
1838#endif
1839
1840 return rc;
1841}
1842
1852void __kmpc_end_single(ident_t *loc, kmp_int32 global_tid) {
1853 __kmp_assert_valid_gtid(global_tid);
1854 __kmp_exit_single(global_tid);
1855 KMP_POP_PARTITIONED_TIMER();
1856
1857#if OMPT_SUPPORT && OMPT_OPTIONAL
1858 kmp_info_t *this_thr = __kmp_threads[global_tid];
1859 kmp_team_t *team = this_thr->th.th_team;
1860 int tid = __kmp_tid_from_gtid(global_tid);
1861
1862 if (ompt_enabled.ompt_callback_work) {
1863 ompt_callbacks.ompt_callback(ompt_callback_work)(
1864 ompt_work_single_executor, ompt_scope_end,
1865 &(team->t.ompt_team_info.parallel_data),
1866 &(team->t.t_implicit_task_taskdata[tid].ompt_task_info.task_data), 1,
1867 OMPT_GET_RETURN_ADDRESS(0));
1868 }
1869#endif
1870}
1871
1879void __kmpc_for_static_fini(ident_t *loc, kmp_int32 global_tid) {
1880 KMP_POP_PARTITIONED_TIMER();
1881 KE_TRACE(10, ("__kmpc_for_static_fini called T#%d\n", global_tid));
1882
1883#if OMPT_SUPPORT && OMPT_OPTIONAL
1884 if (ompt_enabled.ompt_callback_work) {
1885 ompt_work_t ompt_work_type = ompt_work_loop;
1886 ompt_team_info_t *team_info = __ompt_get_teaminfo(0, NULL);
1887 ompt_task_info_t *task_info = __ompt_get_task_info_object(0);
1888 // Determine workshare type
1889 if (loc != NULL) {
1890 if ((loc->flags & KMP_IDENT_WORK_LOOP) != 0) {
1891 ompt_work_type = ompt_work_loop;
1892 } else if ((loc->flags & KMP_IDENT_WORK_SECTIONS) != 0) {
1893 ompt_work_type = ompt_work_sections;
1894 } else if ((loc->flags & KMP_IDENT_WORK_DISTRIBUTE) != 0) {
1895 ompt_work_type = ompt_work_distribute;
1896 } else {
1897 // use default set above.
1898 // a warning about this case is provided in __kmpc_for_static_init
1899 }
1900 KMP_DEBUG_ASSERT(ompt_work_type);
1901 }
1902 ompt_callbacks.ompt_callback(ompt_callback_work)(
1903 ompt_work_type, ompt_scope_end, &(team_info->parallel_data),
1904 &(task_info->task_data), 0, OMPT_GET_RETURN_ADDRESS(0));
1905 }
1906#endif
1907 if (__kmp_env_consistency_check)
1908 __kmp_pop_workshare(global_tid, ct_pdo, loc);
1909}
1910
1911// User routines which take C-style arguments (call by value)
1912// different from the Fortran equivalent routines
1913
1914void ompc_set_num_threads(int arg) {
1915 // !!!!! TODO: check the per-task binding
1916 __kmp_set_num_threads(arg, __kmp_entry_gtid());
1917}
1918
1919void ompc_set_dynamic(int flag) {
1920 kmp_info_t *thread;
1921
1922 /* For the thread-private implementation of the internal controls */
1923 thread = __kmp_entry_thread();
1924
1925 __kmp_save_internal_controls(thread);
1926
1927 set__dynamic(thread, flag ? true : false);
1928}
1929
1930void ompc_set_nested(int flag) {
1931 kmp_info_t *thread;
1932
1933 /* For the thread-private internal controls implementation */
1934 thread = __kmp_entry_thread();
1935
1936 __kmp_save_internal_controls(thread);
1937
1938 set__max_active_levels(thread, flag ? __kmp_dflt_max_active_levels : 1);
1939}
1940
1941void ompc_set_max_active_levels(int max_active_levels) {
1942 /* TO DO */
1943 /* we want per-task implementation of this internal control */
1944
1945 /* For the per-thread internal controls implementation */
1946 __kmp_set_max_active_levels(__kmp_entry_gtid(), max_active_levels);
1947}
1948
1949void ompc_set_schedule(omp_sched_t kind, int modifier) {
1950 // !!!!! TODO: check the per-task binding
1951 __kmp_set_schedule(__kmp_entry_gtid(), (kmp_sched_t)kind, modifier);
1952}
1953
1954int ompc_get_ancestor_thread_num(int level) {
1955 return __kmp_get_ancestor_thread_num(__kmp_entry_gtid(), level);
1956}
1957
1958int ompc_get_team_size(int level) {
1959 return __kmp_get_team_size(__kmp_entry_gtid(), level);
1960}
1961
1962/* OpenMP 5.0 Affinity Format API */
1963void KMP_EXPAND_NAME(ompc_set_affinity_format)(char const *format) {
1964 if (!__kmp_init_serial) {
1965 __kmp_serial_initialize();
1966 }
1967 __kmp_strncpy_truncate(__kmp_affinity_format, KMP_AFFINITY_FORMAT_SIZE,
1968 format, KMP_STRLEN(format) + 1);
1969}
1970
1971size_t KMP_EXPAND_NAME(ompc_get_affinity_format)(char *buffer, size_t size) {
1972 size_t format_size;
1973 if (!__kmp_init_serial) {
1974 __kmp_serial_initialize();
1975 }
1976 format_size = KMP_STRLEN(__kmp_affinity_format);
1977 if (buffer && size) {
1978 __kmp_strncpy_truncate(buffer, size, __kmp_affinity_format,
1979 format_size + 1);
1980 }
1981 return format_size;
1982}
1983
1984void KMP_EXPAND_NAME(ompc_display_affinity)(char const *format) {
1985 int gtid;
1986 if (!TCR_4(__kmp_init_middle)) {
1987 __kmp_middle_initialize();
1988 }
1989 __kmp_assign_root_init_mask();
1990 gtid = __kmp_get_gtid();
1991#if KMP_AFFINITY_SUPPORTED
1992 if (__kmp_threads[gtid]->th.th_team->t.t_level == 0 && __kmp_affin_reset) {
1993 __kmp_reset_root_init_mask(gtid);
1994 }
1995#endif
1996 __kmp_aux_display_affinity(gtid, format);
1997}
1998
1999size_t KMP_EXPAND_NAME(ompc_capture_affinity)(char *buffer, size_t buf_size,
2000 char const *format) {
2001 int gtid;
2002 size_t num_required;
2003 kmp_str_buf_t capture_buf;
2004 if (!TCR_4(__kmp_init_middle)) {
2005 __kmp_middle_initialize();
2006 }
2007 __kmp_assign_root_init_mask();
2008 gtid = __kmp_get_gtid();
2009#if KMP_AFFINITY_SUPPORTED
2010 if (__kmp_threads[gtid]->th.th_team->t.t_level == 0 && __kmp_affin_reset) {
2011 __kmp_reset_root_init_mask(gtid);
2012 }
2013#endif
2014 __kmp_str_buf_init(&capture_buf);
2015 num_required = __kmp_aux_capture_affinity(gtid, format, &capture_buf);
2016 if (buffer && buf_size) {
2017 __kmp_strncpy_truncate(buffer, buf_size, capture_buf.str,
2018 capture_buf.used + 1);
2019 }
2020 __kmp_str_buf_free(&capture_buf);
2021 return num_required;
2022}
2023
2024void kmpc_set_stacksize(int arg) {
2025 // __kmp_aux_set_stacksize initializes the library if needed
2026 __kmp_aux_set_stacksize(arg);
2027}
2028
2029void kmpc_set_stacksize_s(size_t arg) {
2030 // __kmp_aux_set_stacksize initializes the library if needed
2031 __kmp_aux_set_stacksize(arg);
2032}
2033
2034void kmpc_set_blocktime(int arg) {
2035 int gtid, tid;
2036 kmp_info_t *thread;
2037
2038 gtid = __kmp_entry_gtid();
2039 tid = __kmp_tid_from_gtid(gtid);
2040 thread = __kmp_thread_from_gtid(gtid);
2041
2042 __kmp_aux_set_blocktime(arg, thread, tid);
2043}
2044
2045void kmpc_set_library(int arg) {
2046 // __kmp_user_set_library initializes the library if needed
2047 __kmp_user_set_library((enum library_type)arg);
2048}
2049
2050void kmpc_set_defaults(char const *str) {
2051 // __kmp_aux_set_defaults initializes the library if needed
2052 __kmp_aux_set_defaults(str, KMP_STRLEN(str));
2053}
2054
2055void kmpc_set_disp_num_buffers(int arg) {
2056 // ignore after initialization because some teams have already
2057 // allocated dispatch buffers
2058 if (__kmp_init_serial == FALSE && arg >= KMP_MIN_DISP_NUM_BUFF &&
2059 arg <= KMP_MAX_DISP_NUM_BUFF) {
2060 __kmp_dispatch_num_buffers = arg;
2061 }
2062}
2063
2064int kmpc_set_affinity_mask_proc(int proc, void **mask) {
2065#if defined(KMP_STUB) || !KMP_AFFINITY_SUPPORTED
2066 return -1;
2067#else
2068 if (!TCR_4(__kmp_init_middle)) {
2069 __kmp_middle_initialize();
2070 }
2071 __kmp_assign_root_init_mask();
2072 return __kmp_aux_set_affinity_mask_proc(proc, mask);
2073#endif
2074}
2075
2076int kmpc_unset_affinity_mask_proc(int proc, void **mask) {
2077#if defined(KMP_STUB) || !KMP_AFFINITY_SUPPORTED
2078 return -1;
2079#else
2080 if (!TCR_4(__kmp_init_middle)) {
2081 __kmp_middle_initialize();
2082 }
2083 __kmp_assign_root_init_mask();
2084 return __kmp_aux_unset_affinity_mask_proc(proc, mask);
2085#endif
2086}
2087
2088int kmpc_get_affinity_mask_proc(int proc, void **mask) {
2089#if defined(KMP_STUB) || !KMP_AFFINITY_SUPPORTED
2090 return -1;
2091#else
2092 if (!TCR_4(__kmp_init_middle)) {
2093 __kmp_middle_initialize();
2094 }
2095 __kmp_assign_root_init_mask();
2096 return __kmp_aux_get_affinity_mask_proc(proc, mask);
2097#endif
2098}
2099
2100/* -------------------------------------------------------------------------- */
2145void __kmpc_copyprivate(ident_t *loc, kmp_int32 gtid, size_t cpy_size,
2146 void *cpy_data, void (*cpy_func)(void *, void *),
2147 kmp_int32 didit) {
2148 void **data_ptr;
2149 KC_TRACE(10, ("__kmpc_copyprivate: called T#%d\n", gtid));
2150 __kmp_assert_valid_gtid(gtid);
2151
2152 KMP_MB();
2153
2154 data_ptr = &__kmp_team_from_gtid(gtid)->t.t_copypriv_data;
2155
2156 if (__kmp_env_consistency_check) {
2157 if (loc == 0) {
2158 KMP_WARNING(ConstructIdentInvalid);
2159 }
2160 }
2161
2162 // ToDo: Optimize the following two barriers into some kind of split barrier
2163
2164 if (didit)
2165 *data_ptr = cpy_data;
2166
2167#if OMPT_SUPPORT
2168 ompt_frame_t *ompt_frame;
2169 if (ompt_enabled.enabled) {
2170 __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
2171 if (ompt_frame->enter_frame.ptr == NULL)
2172 ompt_frame->enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
2173 }
2174 OMPT_STORE_RETURN_ADDRESS(gtid);
2175#endif
2176/* This barrier is not a barrier region boundary */
2177#if USE_ITT_NOTIFY
2178 __kmp_threads[gtid]->th.th_ident = loc;
2179#endif
2180 __kmp_barrier(bs_plain_barrier, gtid, FALSE, 0, NULL, NULL);
2181
2182 if (!didit)
2183 (*cpy_func)(cpy_data, *data_ptr);
2184
2185 // Consider next barrier a user-visible barrier for barrier region boundaries
2186 // Nesting checks are already handled by the single construct checks
2187 {
2188#if OMPT_SUPPORT
2189 OMPT_STORE_RETURN_ADDRESS(gtid);
2190#endif
2191#if USE_ITT_NOTIFY
2192 __kmp_threads[gtid]->th.th_ident = loc; // TODO: check if it is needed (e.g.
2193// tasks can overwrite the location)
2194#endif
2195 __kmp_barrier(bs_plain_barrier, gtid, FALSE, 0, NULL, NULL);
2196#if OMPT_SUPPORT && OMPT_OPTIONAL
2197 if (ompt_enabled.enabled) {
2198 ompt_frame->enter_frame = ompt_data_none;
2199 }
2200#endif
2201 }
2202}
2203
2204/* --------------------------------------------------------------------------*/
2221void *__kmpc_copyprivate_light(ident_t *loc, kmp_int32 gtid, void *cpy_data) {
2222 void **data_ptr;
2223
2224 KC_TRACE(10, ("__kmpc_copyprivate_light: called T#%d\n", gtid));
2225
2226 KMP_MB();
2227
2228 data_ptr = &__kmp_team_from_gtid(gtid)->t.t_copypriv_data;
2229
2230 if (__kmp_env_consistency_check) {
2231 if (loc == 0) {
2232 KMP_WARNING(ConstructIdentInvalid);
2233 }
2234 }
2235
2236 // ToDo: Optimize the following barrier
2237
2238 if (cpy_data)
2239 *data_ptr = cpy_data;
2240
2241#if OMPT_SUPPORT
2242 ompt_frame_t *ompt_frame;
2243 if (ompt_enabled.enabled) {
2244 __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
2245 if (ompt_frame->enter_frame.ptr == NULL)
2246 ompt_frame->enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
2247 OMPT_STORE_RETURN_ADDRESS(gtid);
2248 }
2249#endif
2250/* This barrier is not a barrier region boundary */
2251#if USE_ITT_NOTIFY
2252 __kmp_threads[gtid]->th.th_ident = loc;
2253#endif
2254 __kmp_barrier(bs_plain_barrier, gtid, FALSE, 0, NULL, NULL);
2255
2256 return *data_ptr;
2257}
2258
2259/* -------------------------------------------------------------------------- */
2260
2261#define INIT_LOCK __kmp_init_user_lock_with_checks
2262#define INIT_NESTED_LOCK __kmp_init_nested_user_lock_with_checks
2263#define ACQUIRE_LOCK __kmp_acquire_user_lock_with_checks
2264#define ACQUIRE_LOCK_TIMED __kmp_acquire_user_lock_with_checks_timed
2265#define ACQUIRE_NESTED_LOCK __kmp_acquire_nested_user_lock_with_checks
2266#define ACQUIRE_NESTED_LOCK_TIMED \
2267 __kmp_acquire_nested_user_lock_with_checks_timed
2268#define RELEASE_LOCK __kmp_release_user_lock_with_checks
2269#define RELEASE_NESTED_LOCK __kmp_release_nested_user_lock_with_checks
2270#define TEST_LOCK __kmp_test_user_lock_with_checks
2271#define TEST_NESTED_LOCK __kmp_test_nested_user_lock_with_checks
2272#define DESTROY_LOCK __kmp_destroy_user_lock_with_checks
2273#define DESTROY_NESTED_LOCK __kmp_destroy_nested_user_lock_with_checks
2274
2275// TODO: Make check abort messages use location info & pass it into
2276// with_checks routines
2277
2278#if KMP_USE_DYNAMIC_LOCK
2279
2280// internal lock initializer
2281static __forceinline void __kmp_init_lock_with_hint(ident_t *loc, void **lock,
2282 kmp_dyna_lockseq_t seq) {
2283 if (KMP_IS_D_LOCK(seq)) {
2284 KMP_INIT_D_LOCK(lock, seq);
2285#if USE_ITT_BUILD
2286 __kmp_itt_lock_creating((kmp_user_lock_p)lock, NULL);
2287#endif
2288 } else {
2289 KMP_INIT_I_LOCK(lock, seq);
2290#if USE_ITT_BUILD
2291 kmp_indirect_lock_t *ilk = KMP_LOOKUP_I_LOCK(lock);
2292 __kmp_itt_lock_creating(ilk->lock, loc);
2293#endif
2294 }
2295}
2296
2297// internal nest lock initializer
2298static __forceinline void
2299__kmp_init_nest_lock_with_hint(ident_t *loc, void **lock,
2300 kmp_dyna_lockseq_t seq) {
2301#if KMP_USE_TSX
2302 // Don't have nested lock implementation for speculative locks
2303 if (seq == lockseq_hle || seq == lockseq_rtm_queuing ||
2304 seq == lockseq_rtm_spin || seq == lockseq_adaptive)
2305 seq = __kmp_user_lock_seq;
2306#endif
2307 switch (seq) {
2308 case lockseq_tas:
2309 seq = lockseq_nested_tas;
2310 break;
2311#if KMP_USE_FUTEX
2312 case lockseq_futex:
2313 seq = lockseq_nested_futex;
2314 break;
2315#endif
2316 case lockseq_ticket:
2317 seq = lockseq_nested_ticket;
2318 break;
2319 case lockseq_queuing:
2320 seq = lockseq_nested_queuing;
2321 break;
2322 case lockseq_drdpa:
2323 seq = lockseq_nested_drdpa;
2324 break;
2325 default:
2326 seq = lockseq_nested_queuing;
2327 }
2328 KMP_INIT_I_LOCK(lock, seq);
2329#if USE_ITT_BUILD
2330 kmp_indirect_lock_t *ilk = KMP_LOOKUP_I_LOCK(lock);
2331 __kmp_itt_lock_creating(ilk->lock, loc);
2332#endif
2333}
2334
2335/* initialize the lock with a hint */
2336void __kmpc_init_lock_with_hint(ident_t *loc, kmp_int32 gtid, void **user_lock,
2337 uintptr_t hint) {
2338 KMP_DEBUG_ASSERT(__kmp_init_serial);
2339 if (__kmp_env_consistency_check && user_lock == NULL) {
2340 KMP_FATAL(LockIsUninitialized, "omp_init_lock_with_hint");
2341 }
2342
2343 __kmp_init_lock_with_hint(loc, user_lock, __kmp_map_hint_to_lock(hint));
2344
2345#if OMPT_SUPPORT && OMPT_OPTIONAL
2346 // This is the case, if called from omp_init_lock_with_hint:
2347 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2348 if (!codeptr)
2349 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2350 if (ompt_enabled.ompt_callback_lock_init) {
2351 ompt_callbacks.ompt_callback(ompt_callback_lock_init)(
2352 ompt_mutex_lock, (omp_lock_hint_t)hint,
2353 __ompt_get_mutex_impl_type(user_lock),
2354 (ompt_wait_id_t)(uintptr_t)user_lock, codeptr);
2355 }
2356#endif
2357}
2358
2359/* initialize the lock with a hint */
2360void __kmpc_init_nest_lock_with_hint(ident_t *loc, kmp_int32 gtid,
2361 void **user_lock, uintptr_t hint) {
2362 KMP_DEBUG_ASSERT(__kmp_init_serial);
2363 if (__kmp_env_consistency_check && user_lock == NULL) {
2364 KMP_FATAL(LockIsUninitialized, "omp_init_nest_lock_with_hint");
2365 }
2366
2367 __kmp_init_nest_lock_with_hint(loc, user_lock, __kmp_map_hint_to_lock(hint));
2368
2369#if OMPT_SUPPORT && OMPT_OPTIONAL
2370 // This is the case, if called from omp_init_lock_with_hint:
2371 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2372 if (!codeptr)
2373 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2374 if (ompt_enabled.ompt_callback_lock_init) {
2375 ompt_callbacks.ompt_callback(ompt_callback_lock_init)(
2376 ompt_mutex_nest_lock, (omp_lock_hint_t)hint,
2377 __ompt_get_mutex_impl_type(user_lock),
2378 (ompt_wait_id_t)(uintptr_t)user_lock, codeptr);
2379 }
2380#endif
2381}
2382
2383#endif // KMP_USE_DYNAMIC_LOCK
2384
2385/* initialize the lock */
2386void __kmpc_init_lock(ident_t *loc, kmp_int32 gtid, void **user_lock) {
2387#if KMP_USE_DYNAMIC_LOCK
2388
2389 KMP_DEBUG_ASSERT(__kmp_init_serial);
2390 if (__kmp_env_consistency_check && user_lock == NULL) {
2391 KMP_FATAL(LockIsUninitialized, "omp_init_lock");
2392 }
2393 __kmp_init_lock_with_hint(loc, user_lock, __kmp_user_lock_seq);
2394
2395#if OMPT_SUPPORT && OMPT_OPTIONAL
2396 // This is the case, if called from omp_init_lock_with_hint:
2397 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2398 if (!codeptr)
2399 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2400 if (ompt_enabled.ompt_callback_lock_init) {
2401 ompt_callbacks.ompt_callback(ompt_callback_lock_init)(
2402 ompt_mutex_lock, omp_lock_hint_none,
2403 __ompt_get_mutex_impl_type(user_lock),
2404 (ompt_wait_id_t)(uintptr_t)user_lock, codeptr);
2405 }
2406#endif
2407
2408#else // KMP_USE_DYNAMIC_LOCK
2409
2410 static char const *const func = "omp_init_lock";
2411 kmp_user_lock_p lck;
2412 KMP_DEBUG_ASSERT(__kmp_init_serial);
2413
2414 if (__kmp_env_consistency_check) {
2415 if (user_lock == NULL) {
2416 KMP_FATAL(LockIsUninitialized, func);
2417 }
2418 }
2419
2420 KMP_CHECK_USER_LOCK_INIT();
2421
2422 if ((__kmp_user_lock_kind == lk_tas) &&
2423 (sizeof(lck->tas.lk.poll) <= OMP_LOCK_T_SIZE)) {
2424 lck = (kmp_user_lock_p)user_lock;
2425 }
2426#if KMP_USE_FUTEX
2427 else if ((__kmp_user_lock_kind == lk_futex) &&
2428 (sizeof(lck->futex.lk.poll) <= OMP_LOCK_T_SIZE)) {
2429 lck = (kmp_user_lock_p)user_lock;
2430 }
2431#endif
2432 else {
2433 lck = __kmp_user_lock_allocate(user_lock, gtid, 0);
2434 }
2435 INIT_LOCK(lck);
2436 __kmp_set_user_lock_location(lck, loc);
2437
2438#if OMPT_SUPPORT && OMPT_OPTIONAL
2439 // This is the case, if called from omp_init_lock_with_hint:
2440 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2441 if (!codeptr)
2442 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2443 if (ompt_enabled.ompt_callback_lock_init) {
2444 ompt_callbacks.ompt_callback(ompt_callback_lock_init)(
2445 ompt_mutex_lock, omp_lock_hint_none, __ompt_get_mutex_impl_type(),
2446 (ompt_wait_id_t)(uintptr_t)user_lock, codeptr);
2447 }
2448#endif
2449
2450#if USE_ITT_BUILD
2451 __kmp_itt_lock_creating(lck);
2452#endif /* USE_ITT_BUILD */
2453
2454#endif // KMP_USE_DYNAMIC_LOCK
2455} // __kmpc_init_lock
2456
2457/* initialize the lock */
2458void __kmpc_init_nest_lock(ident_t *loc, kmp_int32 gtid, void **user_lock) {
2459#if KMP_USE_DYNAMIC_LOCK
2460
2461 KMP_DEBUG_ASSERT(__kmp_init_serial);
2462 if (__kmp_env_consistency_check && user_lock == NULL) {
2463 KMP_FATAL(LockIsUninitialized, "omp_init_nest_lock");
2464 }
2465 __kmp_init_nest_lock_with_hint(loc, user_lock, __kmp_user_lock_seq);
2466
2467#if OMPT_SUPPORT && OMPT_OPTIONAL
2468 // This is the case, if called from omp_init_lock_with_hint:
2469 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2470 if (!codeptr)
2471 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2472 if (ompt_enabled.ompt_callback_lock_init) {
2473 ompt_callbacks.ompt_callback(ompt_callback_lock_init)(
2474 ompt_mutex_nest_lock, omp_lock_hint_none,
2475 __ompt_get_mutex_impl_type(user_lock),
2476 (ompt_wait_id_t)(uintptr_t)user_lock, codeptr);
2477 }
2478#endif
2479
2480#else // KMP_USE_DYNAMIC_LOCK
2481
2482 static char const *const func = "omp_init_nest_lock";
2483 kmp_user_lock_p lck;
2484 KMP_DEBUG_ASSERT(__kmp_init_serial);
2485
2486 if (__kmp_env_consistency_check) {
2487 if (user_lock == NULL) {
2488 KMP_FATAL(LockIsUninitialized, func);
2489 }
2490 }
2491
2492 KMP_CHECK_USER_LOCK_INIT();
2493
2494 if ((__kmp_user_lock_kind == lk_tas) &&
2495 (sizeof(lck->tas.lk.poll) + sizeof(lck->tas.lk.depth_locked) <=
2496 OMP_NEST_LOCK_T_SIZE)) {
2497 lck = (kmp_user_lock_p)user_lock;
2498 }
2499#if KMP_USE_FUTEX
2500 else if ((__kmp_user_lock_kind == lk_futex) &&
2501 (sizeof(lck->futex.lk.poll) + sizeof(lck->futex.lk.depth_locked) <=
2502 OMP_NEST_LOCK_T_SIZE)) {
2503 lck = (kmp_user_lock_p)user_lock;
2504 }
2505#endif
2506 else {
2507 lck = __kmp_user_lock_allocate(user_lock, gtid, 0);
2508 }
2509
2510 INIT_NESTED_LOCK(lck);
2511 __kmp_set_user_lock_location(lck, loc);
2512
2513#if OMPT_SUPPORT && OMPT_OPTIONAL
2514 // This is the case, if called from omp_init_lock_with_hint:
2515 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2516 if (!codeptr)
2517 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2518 if (ompt_enabled.ompt_callback_lock_init) {
2519 ompt_callbacks.ompt_callback(ompt_callback_lock_init)(
2520 ompt_mutex_nest_lock, omp_lock_hint_none, __ompt_get_mutex_impl_type(),
2521 (ompt_wait_id_t)(uintptr_t)user_lock, codeptr);
2522 }
2523#endif
2524
2525#if USE_ITT_BUILD
2526 __kmp_itt_lock_creating(lck);
2527#endif /* USE_ITT_BUILD */
2528
2529#endif // KMP_USE_DYNAMIC_LOCK
2530} // __kmpc_init_nest_lock
2531
2532void __kmpc_destroy_lock(ident_t *loc, kmp_int32 gtid, void **user_lock) {
2533#if KMP_USE_DYNAMIC_LOCK
2534
2535#if USE_ITT_BUILD
2536 kmp_user_lock_p lck;
2537 if (KMP_EXTRACT_D_TAG(user_lock) == 0) {
2538 lck = ((kmp_indirect_lock_t *)KMP_LOOKUP_I_LOCK(user_lock))->lock;
2539 } else {
2540 lck = (kmp_user_lock_p)user_lock;
2541 }
2542 __kmp_itt_lock_destroyed(lck);
2543#endif
2544#if OMPT_SUPPORT && OMPT_OPTIONAL
2545 // This is the case, if called from omp_init_lock_with_hint:
2546 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2547 if (!codeptr)
2548 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2549 if (ompt_enabled.ompt_callback_lock_destroy) {
2550 ompt_callbacks.ompt_callback(ompt_callback_lock_destroy)(
2551 ompt_mutex_lock, (ompt_wait_id_t)(uintptr_t)user_lock, codeptr);
2552 }
2553#endif
2554 KMP_D_LOCK_FUNC(user_lock, destroy)((kmp_dyna_lock_t *)user_lock);
2555#else
2556 kmp_user_lock_p lck;
2557
2558 if ((__kmp_user_lock_kind == lk_tas) &&
2559 (sizeof(lck->tas.lk.poll) <= OMP_LOCK_T_SIZE)) {
2560 lck = (kmp_user_lock_p)user_lock;
2561 }
2562#if KMP_USE_FUTEX
2563 else if ((__kmp_user_lock_kind == lk_futex) &&
2564 (sizeof(lck->futex.lk.poll) <= OMP_LOCK_T_SIZE)) {
2565 lck = (kmp_user_lock_p)user_lock;
2566 }
2567#endif
2568 else {
2569 lck = __kmp_lookup_user_lock(user_lock, "omp_destroy_lock");
2570 }
2571
2572#if OMPT_SUPPORT && OMPT_OPTIONAL
2573 // This is the case, if called from omp_init_lock_with_hint:
2574 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2575 if (!codeptr)
2576 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2577 if (ompt_enabled.ompt_callback_lock_destroy) {
2578 ompt_callbacks.ompt_callback(ompt_callback_lock_destroy)(
2579 ompt_mutex_lock, (ompt_wait_id_t)(uintptr_t)user_lock, codeptr);
2580 }
2581#endif
2582
2583#if USE_ITT_BUILD
2584 __kmp_itt_lock_destroyed(lck);
2585#endif /* USE_ITT_BUILD */
2586 DESTROY_LOCK(lck);
2587
2588 if ((__kmp_user_lock_kind == lk_tas) &&
2589 (sizeof(lck->tas.lk.poll) <= OMP_LOCK_T_SIZE)) {
2590 ;
2591 }
2592#if KMP_USE_FUTEX
2593 else if ((__kmp_user_lock_kind == lk_futex) &&
2594 (sizeof(lck->futex.lk.poll) <= OMP_LOCK_T_SIZE)) {
2595 ;
2596 }
2597#endif
2598 else {
2599 __kmp_user_lock_free(user_lock, gtid, lck);
2600 }
2601#endif // KMP_USE_DYNAMIC_LOCK
2602} // __kmpc_destroy_lock
2603
2604/* destroy the lock */
2605void __kmpc_destroy_nest_lock(ident_t *loc, kmp_int32 gtid, void **user_lock) {
2606#if KMP_USE_DYNAMIC_LOCK
2607
2608#if USE_ITT_BUILD
2609 kmp_indirect_lock_t *ilk = KMP_LOOKUP_I_LOCK(user_lock);
2610 __kmp_itt_lock_destroyed(ilk->lock);
2611#endif
2612#if OMPT_SUPPORT && OMPT_OPTIONAL
2613 // This is the case, if called from omp_init_lock_with_hint:
2614 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2615 if (!codeptr)
2616 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2617 if (ompt_enabled.ompt_callback_lock_destroy) {
2618 ompt_callbacks.ompt_callback(ompt_callback_lock_destroy)(
2619 ompt_mutex_nest_lock, (ompt_wait_id_t)(uintptr_t)user_lock, codeptr);
2620 }
2621#endif
2622 KMP_D_LOCK_FUNC(user_lock, destroy)((kmp_dyna_lock_t *)user_lock);
2623
2624#else // KMP_USE_DYNAMIC_LOCK
2625
2626 kmp_user_lock_p lck;
2627
2628 if ((__kmp_user_lock_kind == lk_tas) &&
2629 (sizeof(lck->tas.lk.poll) + sizeof(lck->tas.lk.depth_locked) <=
2630 OMP_NEST_LOCK_T_SIZE)) {
2631 lck = (kmp_user_lock_p)user_lock;
2632 }
2633#if KMP_USE_FUTEX
2634 else if ((__kmp_user_lock_kind == lk_futex) &&
2635 (sizeof(lck->futex.lk.poll) + sizeof(lck->futex.lk.depth_locked) <=
2636 OMP_NEST_LOCK_T_SIZE)) {
2637 lck = (kmp_user_lock_p)user_lock;
2638 }
2639#endif
2640 else {
2641 lck = __kmp_lookup_user_lock(user_lock, "omp_destroy_nest_lock");
2642 }
2643
2644#if OMPT_SUPPORT && OMPT_OPTIONAL
2645 // This is the case, if called from omp_init_lock_with_hint:
2646 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2647 if (!codeptr)
2648 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2649 if (ompt_enabled.ompt_callback_lock_destroy) {
2650 ompt_callbacks.ompt_callback(ompt_callback_lock_destroy)(
2651 ompt_mutex_nest_lock, (ompt_wait_id_t)(uintptr_t)user_lock, codeptr);
2652 }
2653#endif
2654
2655#if USE_ITT_BUILD
2656 __kmp_itt_lock_destroyed(lck);
2657#endif /* USE_ITT_BUILD */
2658
2659 DESTROY_NESTED_LOCK(lck);
2660
2661 if ((__kmp_user_lock_kind == lk_tas) &&
2662 (sizeof(lck->tas.lk.poll) + sizeof(lck->tas.lk.depth_locked) <=
2663 OMP_NEST_LOCK_T_SIZE)) {
2664 ;
2665 }
2666#if KMP_USE_FUTEX
2667 else if ((__kmp_user_lock_kind == lk_futex) &&
2668 (sizeof(lck->futex.lk.poll) + sizeof(lck->futex.lk.depth_locked) <=
2669 OMP_NEST_LOCK_T_SIZE)) {
2670 ;
2671 }
2672#endif
2673 else {
2674 __kmp_user_lock_free(user_lock, gtid, lck);
2675 }
2676#endif // KMP_USE_DYNAMIC_LOCK
2677} // __kmpc_destroy_nest_lock
2678
2679void __kmpc_set_lock(ident_t *loc, kmp_int32 gtid, void **user_lock) {
2680 KMP_COUNT_BLOCK(OMP_set_lock);
2681#if KMP_USE_DYNAMIC_LOCK
2682 int tag = KMP_EXTRACT_D_TAG(user_lock);
2683#if USE_ITT_BUILD
2684 __kmp_itt_lock_acquiring(
2685 (kmp_user_lock_p)
2686 user_lock); // itt function will get to the right lock object.
2687#endif
2688#if OMPT_SUPPORT && OMPT_OPTIONAL
2689 // This is the case, if called from omp_init_lock_with_hint:
2690 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2691 if (!codeptr)
2692 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2693 if (ompt_enabled.ompt_callback_mutex_acquire) {
2694 ompt_callbacks.ompt_callback(ompt_callback_mutex_acquire)(
2695 ompt_mutex_lock, omp_lock_hint_none,
2696 __ompt_get_mutex_impl_type(user_lock),
2697 (ompt_wait_id_t)(uintptr_t)user_lock, codeptr);
2698 }
2699#endif
2700#if KMP_USE_INLINED_TAS
2701 if (tag == locktag_tas && !__kmp_env_consistency_check) {
2702 KMP_ACQUIRE_TAS_LOCK(user_lock, gtid);
2703 } else
2704#elif KMP_USE_INLINED_FUTEX
2705 if (tag == locktag_futex && !__kmp_env_consistency_check) {
2706 KMP_ACQUIRE_FUTEX_LOCK(user_lock, gtid);
2707 } else
2708#endif
2709 {
2710 __kmp_direct_set[tag]((kmp_dyna_lock_t *)user_lock, gtid);
2711 }
2712#if USE_ITT_BUILD
2713 __kmp_itt_lock_acquired((kmp_user_lock_p)user_lock);
2714#endif
2715#if OMPT_SUPPORT && OMPT_OPTIONAL
2716 if (ompt_enabled.ompt_callback_mutex_acquired) {
2717 ompt_callbacks.ompt_callback(ompt_callback_mutex_acquired)(
2718 ompt_mutex_lock, (ompt_wait_id_t)(uintptr_t)user_lock, codeptr);
2719 }
2720#endif
2721
2722#else // KMP_USE_DYNAMIC_LOCK
2723
2724 kmp_user_lock_p lck;
2725
2726 if ((__kmp_user_lock_kind == lk_tas) &&
2727 (sizeof(lck->tas.lk.poll) <= OMP_LOCK_T_SIZE)) {
2728 lck = (kmp_user_lock_p)user_lock;
2729 }
2730#if KMP_USE_FUTEX
2731 else if ((__kmp_user_lock_kind == lk_futex) &&
2732 (sizeof(lck->futex.lk.poll) <= OMP_LOCK_T_SIZE)) {
2733 lck = (kmp_user_lock_p)user_lock;
2734 }
2735#endif
2736 else {
2737 lck = __kmp_lookup_user_lock(user_lock, "omp_set_lock");
2738 }
2739
2740#if USE_ITT_BUILD
2741 __kmp_itt_lock_acquiring(lck);
2742#endif /* USE_ITT_BUILD */
2743#if OMPT_SUPPORT && OMPT_OPTIONAL
2744 // This is the case, if called from omp_init_lock_with_hint:
2745 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2746 if (!codeptr)
2747 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2748 if (ompt_enabled.ompt_callback_mutex_acquire) {
2749 ompt_callbacks.ompt_callback(ompt_callback_mutex_acquire)(
2750 ompt_mutex_lock, omp_lock_hint_none, __ompt_get_mutex_impl_type(),
2751 (ompt_wait_id_t)(uintptr_t)lck, codeptr);
2752 }
2753#endif
2754
2755 ACQUIRE_LOCK(lck, gtid);
2756
2757#if USE_ITT_BUILD
2758 __kmp_itt_lock_acquired(lck);
2759#endif /* USE_ITT_BUILD */
2760
2761#if OMPT_SUPPORT && OMPT_OPTIONAL
2762 if (ompt_enabled.ompt_callback_mutex_acquired) {
2763 ompt_callbacks.ompt_callback(ompt_callback_mutex_acquired)(
2764 ompt_mutex_lock, (ompt_wait_id_t)(uintptr_t)lck, codeptr);
2765 }
2766#endif
2767
2768#endif // KMP_USE_DYNAMIC_LOCK
2769}
2770
2771void __kmpc_set_nest_lock(ident_t *loc, kmp_int32 gtid, void **user_lock) {
2772#if KMP_USE_DYNAMIC_LOCK
2773
2774#if USE_ITT_BUILD
2775 __kmp_itt_lock_acquiring((kmp_user_lock_p)user_lock);
2776#endif
2777#if OMPT_SUPPORT && OMPT_OPTIONAL
2778 // This is the case, if called from omp_init_lock_with_hint:
2779 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2780 if (!codeptr)
2781 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2782 if (ompt_enabled.enabled) {
2783 if (ompt_enabled.ompt_callback_mutex_acquire) {
2784 ompt_callbacks.ompt_callback(ompt_callback_mutex_acquire)(
2785 ompt_mutex_nest_lock, omp_lock_hint_none,
2786 __ompt_get_mutex_impl_type(user_lock),
2787 (ompt_wait_id_t)(uintptr_t)user_lock, codeptr);
2788 }
2789 }
2790#endif
2791 int acquire_status =
2792 KMP_D_LOCK_FUNC(user_lock, set)((kmp_dyna_lock_t *)user_lock, gtid);
2793 (void)acquire_status;
2794#if USE_ITT_BUILD
2795 __kmp_itt_lock_acquired((kmp_user_lock_p)user_lock);
2796#endif
2797
2798#if OMPT_SUPPORT && OMPT_OPTIONAL
2799 if (ompt_enabled.enabled) {
2800 if (acquire_status == KMP_LOCK_ACQUIRED_FIRST) {
2801 if (ompt_enabled.ompt_callback_mutex_acquired) {
2802 // lock_first
2803 ompt_callbacks.ompt_callback(ompt_callback_mutex_acquired)(
2804 ompt_mutex_nest_lock, (ompt_wait_id_t)(uintptr_t)user_lock,
2805 codeptr);
2806 }
2807 } else {
2808 if (ompt_enabled.ompt_callback_nest_lock) {
2809 // lock_next
2810 ompt_callbacks.ompt_callback(ompt_callback_nest_lock)(
2811 ompt_scope_begin, (ompt_wait_id_t)(uintptr_t)user_lock, codeptr);
2812 }
2813 }
2814 }
2815#endif
2816
2817#else // KMP_USE_DYNAMIC_LOCK
2818 int acquire_status;
2819 kmp_user_lock_p lck;
2820
2821 if ((__kmp_user_lock_kind == lk_tas) &&
2822 (sizeof(lck->tas.lk.poll) + sizeof(lck->tas.lk.depth_locked) <=
2823 OMP_NEST_LOCK_T_SIZE)) {
2824 lck = (kmp_user_lock_p)user_lock;
2825 }
2826#if KMP_USE_FUTEX
2827 else if ((__kmp_user_lock_kind == lk_futex) &&
2828 (sizeof(lck->futex.lk.poll) + sizeof(lck->futex.lk.depth_locked) <=
2829 OMP_NEST_LOCK_T_SIZE)) {
2830 lck = (kmp_user_lock_p)user_lock;
2831 }
2832#endif
2833 else {
2834 lck = __kmp_lookup_user_lock(user_lock, "omp_set_nest_lock");
2835 }
2836
2837#if USE_ITT_BUILD
2838 __kmp_itt_lock_acquiring(lck);
2839#endif /* USE_ITT_BUILD */
2840#if OMPT_SUPPORT && OMPT_OPTIONAL
2841 // This is the case, if called from omp_init_lock_with_hint:
2842 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2843 if (!codeptr)
2844 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2845 if (ompt_enabled.enabled) {
2846 if (ompt_enabled.ompt_callback_mutex_acquire) {
2847 ompt_callbacks.ompt_callback(ompt_callback_mutex_acquire)(
2848 ompt_mutex_nest_lock, omp_lock_hint_none,
2849 __ompt_get_mutex_impl_type(), (ompt_wait_id_t)(uintptr_t)lck,
2850 codeptr);
2851 }
2852 }
2853#endif
2854
2855 ACQUIRE_NESTED_LOCK(lck, gtid, &acquire_status);
2856
2857#if USE_ITT_BUILD
2858 __kmp_itt_lock_acquired(lck);
2859#endif /* USE_ITT_BUILD */
2860
2861#if OMPT_SUPPORT && OMPT_OPTIONAL
2862 if (ompt_enabled.enabled) {
2863 if (acquire_status == KMP_LOCK_ACQUIRED_FIRST) {
2864 if (ompt_enabled.ompt_callback_mutex_acquired) {
2865 // lock_first
2866 ompt_callbacks.ompt_callback(ompt_callback_mutex_acquired)(
2867 ompt_mutex_nest_lock, (ompt_wait_id_t)(uintptr_t)lck, codeptr);
2868 }
2869 } else {
2870 if (ompt_enabled.ompt_callback_nest_lock) {
2871 // lock_next
2872 ompt_callbacks.ompt_callback(ompt_callback_nest_lock)(
2873 ompt_scope_begin, (ompt_wait_id_t)(uintptr_t)lck, codeptr);
2874 }
2875 }
2876 }
2877#endif
2878
2879#endif // KMP_USE_DYNAMIC_LOCK
2880}
2881
2882void __kmpc_unset_lock(ident_t *loc, kmp_int32 gtid, void **user_lock) {
2883#if KMP_USE_DYNAMIC_LOCK
2884
2885 int tag = KMP_EXTRACT_D_TAG(user_lock);
2886#if USE_ITT_BUILD
2887 __kmp_itt_lock_releasing((kmp_user_lock_p)user_lock);
2888#endif
2889#if KMP_USE_INLINED_TAS
2890 if (tag == locktag_tas && !__kmp_env_consistency_check) {
2891 KMP_RELEASE_TAS_LOCK(user_lock, gtid);
2892 } else
2893#elif KMP_USE_INLINED_FUTEX
2894 if (tag == locktag_futex && !__kmp_env_consistency_check) {
2895 KMP_RELEASE_FUTEX_LOCK(user_lock, gtid);
2896 } else
2897#endif
2898 {
2899 __kmp_direct_unset[tag]((kmp_dyna_lock_t *)user_lock, gtid);
2900 }
2901
2902#if OMPT_SUPPORT && OMPT_OPTIONAL
2903 // This is the case, if called from omp_init_lock_with_hint:
2904 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2905 if (!codeptr)
2906 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2907 if (ompt_enabled.ompt_callback_mutex_released) {
2908 ompt_callbacks.ompt_callback(ompt_callback_mutex_released)(
2909 ompt_mutex_lock, (ompt_wait_id_t)(uintptr_t)user_lock, codeptr);
2910 }
2911#endif
2912
2913#else // KMP_USE_DYNAMIC_LOCK
2914
2915 kmp_user_lock_p lck;
2916
2917 /* Can't use serial interval since not block structured */
2918 /* release the lock */
2919
2920 if ((__kmp_user_lock_kind == lk_tas) &&
2921 (sizeof(lck->tas.lk.poll) <= OMP_LOCK_T_SIZE)) {
2922#if KMP_OS_LINUX && \
2923 (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64)
2924// "fast" path implemented to fix customer performance issue
2925#if USE_ITT_BUILD
2926 __kmp_itt_lock_releasing((kmp_user_lock_p)user_lock);
2927#endif /* USE_ITT_BUILD */
2928 TCW_4(((kmp_user_lock_p)user_lock)->tas.lk.poll, 0);
2929 KMP_MB();
2930
2931#if OMPT_SUPPORT && OMPT_OPTIONAL
2932 // This is the case, if called from omp_init_lock_with_hint:
2933 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2934 if (!codeptr)
2935 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2936 if (ompt_enabled.ompt_callback_mutex_released) {
2937 ompt_callbacks.ompt_callback(ompt_callback_mutex_released)(
2938 ompt_mutex_lock, (ompt_wait_id_t)(uintptr_t)lck, codeptr);
2939 }
2940#endif
2941
2942 return;
2943#else
2944 lck = (kmp_user_lock_p)user_lock;
2945#endif
2946 }
2947#if KMP_USE_FUTEX
2948 else if ((__kmp_user_lock_kind == lk_futex) &&
2949 (sizeof(lck->futex.lk.poll) <= OMP_LOCK_T_SIZE)) {
2950 lck = (kmp_user_lock_p)user_lock;
2951 }
2952#endif
2953 else {
2954 lck = __kmp_lookup_user_lock(user_lock, "omp_unset_lock");
2955 }
2956
2957#if USE_ITT_BUILD
2958 __kmp_itt_lock_releasing(lck);
2959#endif /* USE_ITT_BUILD */
2960
2961 RELEASE_LOCK(lck, gtid);
2962
2963#if OMPT_SUPPORT && OMPT_OPTIONAL
2964 // This is the case, if called from omp_init_lock_with_hint:
2965 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2966 if (!codeptr)
2967 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2968 if (ompt_enabled.ompt_callback_mutex_released) {
2969 ompt_callbacks.ompt_callback(ompt_callback_mutex_released)(
2970 ompt_mutex_lock, (ompt_wait_id_t)(uintptr_t)lck, codeptr);
2971 }
2972#endif
2973
2974#endif // KMP_USE_DYNAMIC_LOCK
2975}
2976
2977/* release the lock */
2978void __kmpc_unset_nest_lock(ident_t *loc, kmp_int32 gtid, void **user_lock) {
2979#if KMP_USE_DYNAMIC_LOCK
2980
2981#if USE_ITT_BUILD
2982 __kmp_itt_lock_releasing((kmp_user_lock_p)user_lock);
2983#endif
2984 int release_status =
2985 KMP_D_LOCK_FUNC(user_lock, unset)((kmp_dyna_lock_t *)user_lock, gtid);
2986 (void)release_status;
2987
2988#if OMPT_SUPPORT && OMPT_OPTIONAL
2989 // This is the case, if called from omp_init_lock_with_hint:
2990 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
2991 if (!codeptr)
2992 codeptr = OMPT_GET_RETURN_ADDRESS(0);
2993 if (ompt_enabled.enabled) {
2994 if (release_status == KMP_LOCK_RELEASED) {
2995 if (ompt_enabled.ompt_callback_mutex_released) {
2996 // release_lock_last
2997 ompt_callbacks.ompt_callback(ompt_callback_mutex_released)(
2998 ompt_mutex_nest_lock, (ompt_wait_id_t)(uintptr_t)user_lock,
2999 codeptr);
3000 }
3001 } else if (ompt_enabled.ompt_callback_nest_lock) {
3002 // release_lock_prev
3003 ompt_callbacks.ompt_callback(ompt_callback_nest_lock)(
3004 ompt_scope_end, (ompt_wait_id_t)(uintptr_t)user_lock, codeptr);
3005 }
3006 }
3007#endif
3008
3009#else // KMP_USE_DYNAMIC_LOCK
3010
3011 kmp_user_lock_p lck;
3012
3013 /* Can't use serial interval since not block structured */
3014
3015 if ((__kmp_user_lock_kind == lk_tas) &&
3016 (sizeof(lck->tas.lk.poll) + sizeof(lck->tas.lk.depth_locked) <=
3017 OMP_NEST_LOCK_T_SIZE)) {
3018#if KMP_OS_LINUX && \
3019 (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64)
3020 // "fast" path implemented to fix customer performance issue
3021 kmp_tas_lock_t *tl = (kmp_tas_lock_t *)user_lock;
3022#if USE_ITT_BUILD
3023 __kmp_itt_lock_releasing((kmp_user_lock_p)user_lock);
3024#endif /* USE_ITT_BUILD */
3025
3026#if OMPT_SUPPORT && OMPT_OPTIONAL
3027 int release_status = KMP_LOCK_STILL_HELD;
3028#endif
3029
3030 if (--(tl->lk.depth_locked) == 0) {
3031 TCW_4(tl->lk.poll, 0);
3032#if OMPT_SUPPORT && OMPT_OPTIONAL
3033 release_status = KMP_LOCK_RELEASED;
3034#endif
3035 }
3036 KMP_MB();
3037
3038#if OMPT_SUPPORT && OMPT_OPTIONAL
3039 // This is the case, if called from omp_init_lock_with_hint:
3040 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
3041 if (!codeptr)
3042 codeptr = OMPT_GET_RETURN_ADDRESS(0);
3043 if (ompt_enabled.enabled) {
3044 if (release_status == KMP_LOCK_RELEASED) {
3045 if (ompt_enabled.ompt_callback_mutex_released) {
3046 // release_lock_last
3047 ompt_callbacks.ompt_callback(ompt_callback_mutex_released)(
3048 ompt_mutex_nest_lock, (ompt_wait_id_t)(uintptr_t)lck, codeptr);
3049 }
3050 } else if (ompt_enabled.ompt_callback_nest_lock) {
3051 // release_lock_previous
3052 ompt_callbacks.ompt_callback(ompt_callback_nest_lock)(
3053 ompt_mutex_scope_end, (ompt_wait_id_t)(uintptr_t)lck, codeptr);
3054 }
3055 }
3056#endif
3057
3058 return;
3059#else
3060 lck = (kmp_user_lock_p)user_lock;
3061#endif
3062 }
3063#if KMP_USE_FUTEX
3064 else if ((__kmp_user_lock_kind == lk_futex) &&
3065 (sizeof(lck->futex.lk.poll) + sizeof(lck->futex.lk.depth_locked) <=
3066 OMP_NEST_LOCK_T_SIZE)) {
3067 lck = (kmp_user_lock_p)user_lock;
3068 }
3069#endif
3070 else {
3071 lck = __kmp_lookup_user_lock(user_lock, "omp_unset_nest_lock");
3072 }
3073
3074#if USE_ITT_BUILD
3075 __kmp_itt_lock_releasing(lck);
3076#endif /* USE_ITT_BUILD */
3077
3078 int release_status;
3079 release_status = RELEASE_NESTED_LOCK(lck, gtid);
3080#if OMPT_SUPPORT && OMPT_OPTIONAL
3081 // This is the case, if called from omp_init_lock_with_hint:
3082 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
3083 if (!codeptr)
3084 codeptr = OMPT_GET_RETURN_ADDRESS(0);
3085 if (ompt_enabled.enabled) {
3086 if (release_status == KMP_LOCK_RELEASED) {
3087 if (ompt_enabled.ompt_callback_mutex_released) {
3088 // release_lock_last
3089 ompt_callbacks.ompt_callback(ompt_callback_mutex_released)(
3090 ompt_mutex_nest_lock, (ompt_wait_id_t)(uintptr_t)lck, codeptr);
3091 }
3092 } else if (ompt_enabled.ompt_callback_nest_lock) {
3093 // release_lock_previous
3094 ompt_callbacks.ompt_callback(ompt_callback_nest_lock)(
3095 ompt_mutex_scope_end, (ompt_wait_id_t)(uintptr_t)lck, codeptr);
3096 }
3097 }
3098#endif
3099
3100#endif // KMP_USE_DYNAMIC_LOCK
3101}
3102
3103/* try to acquire the lock */
3104int __kmpc_test_lock(ident_t *loc, kmp_int32 gtid, void **user_lock) {
3105 KMP_COUNT_BLOCK(OMP_test_lock);
3106
3107#if KMP_USE_DYNAMIC_LOCK
3108 int rc;
3109 int tag = KMP_EXTRACT_D_TAG(user_lock);
3110#if USE_ITT_BUILD
3111 __kmp_itt_lock_acquiring((kmp_user_lock_p)user_lock);
3112#endif
3113#if OMPT_SUPPORT && OMPT_OPTIONAL
3114 // This is the case, if called from omp_init_lock_with_hint:
3115 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
3116 if (!codeptr)
3117 codeptr = OMPT_GET_RETURN_ADDRESS(0);
3118 if (ompt_enabled.ompt_callback_mutex_acquire) {
3119 ompt_callbacks.ompt_callback(ompt_callback_mutex_acquire)(
3120 ompt_mutex_lock, omp_lock_hint_none,
3121 __ompt_get_mutex_impl_type(user_lock),
3122 (ompt_wait_id_t)(uintptr_t)user_lock, codeptr);
3123 }
3124#endif
3125#if KMP_USE_INLINED_TAS
3126 if (tag == locktag_tas && !__kmp_env_consistency_check) {
3127 KMP_TEST_TAS_LOCK(user_lock, gtid, rc);
3128 } else
3129#elif KMP_USE_INLINED_FUTEX
3130 if (tag == locktag_futex && !__kmp_env_consistency_check) {
3131 KMP_TEST_FUTEX_LOCK(user_lock, gtid, rc);
3132 } else
3133#endif
3134 {
3135 rc = __kmp_direct_test[tag]((kmp_dyna_lock_t *)user_lock, gtid);
3136 }
3137 if (rc) {
3138#if USE_ITT_BUILD
3139 __kmp_itt_lock_acquired((kmp_user_lock_p)user_lock);
3140#endif
3141#if OMPT_SUPPORT && OMPT_OPTIONAL
3142 if (ompt_enabled.ompt_callback_mutex_acquired) {
3143 ompt_callbacks.ompt_callback(ompt_callback_mutex_acquired)(
3144 ompt_mutex_lock, (ompt_wait_id_t)(uintptr_t)user_lock, codeptr);
3145 }
3146#endif
3147 return FTN_TRUE;
3148 } else {
3149#if USE_ITT_BUILD
3150 __kmp_itt_lock_cancelled((kmp_user_lock_p)user_lock);
3151#endif
3152 return FTN_FALSE;
3153 }
3154
3155#else // KMP_USE_DYNAMIC_LOCK
3156
3157 kmp_user_lock_p lck;
3158 int rc;
3159
3160 if ((__kmp_user_lock_kind == lk_tas) &&
3161 (sizeof(lck->tas.lk.poll) <= OMP_LOCK_T_SIZE)) {
3162 lck = (kmp_user_lock_p)user_lock;
3163 }
3164#if KMP_USE_FUTEX
3165 else if ((__kmp_user_lock_kind == lk_futex) &&
3166 (sizeof(lck->futex.lk.poll) <= OMP_LOCK_T_SIZE)) {
3167 lck = (kmp_user_lock_p)user_lock;
3168 }
3169#endif
3170 else {
3171 lck = __kmp_lookup_user_lock(user_lock, "omp_test_lock");
3172 }
3173
3174#if USE_ITT_BUILD
3175 __kmp_itt_lock_acquiring(lck);
3176#endif /* USE_ITT_BUILD */
3177#if OMPT_SUPPORT && OMPT_OPTIONAL
3178 // This is the case, if called from omp_init_lock_with_hint:
3179 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
3180 if (!codeptr)
3181 codeptr = OMPT_GET_RETURN_ADDRESS(0);
3182 if (ompt_enabled.ompt_callback_mutex_acquire) {
3183 ompt_callbacks.ompt_callback(ompt_callback_mutex_acquire)(
3184 ompt_mutex_lock, omp_lock_hint_none, __ompt_get_mutex_impl_type(),
3185 (ompt_wait_id_t)(uintptr_t)lck, codeptr);
3186 }
3187#endif
3188
3189 rc = TEST_LOCK(lck, gtid);
3190#if USE_ITT_BUILD
3191 if (rc) {
3192 __kmp_itt_lock_acquired(lck);
3193 } else {
3194 __kmp_itt_lock_cancelled(lck);
3195 }
3196#endif /* USE_ITT_BUILD */
3197#if OMPT_SUPPORT && OMPT_OPTIONAL
3198 if (rc && ompt_enabled.ompt_callback_mutex_acquired) {
3199 ompt_callbacks.ompt_callback(ompt_callback_mutex_acquired)(
3200 ompt_mutex_lock, (ompt_wait_id_t)(uintptr_t)lck, codeptr);
3201 }
3202#endif
3203
3204 return (rc ? FTN_TRUE : FTN_FALSE);
3205
3206 /* Can't use serial interval since not block structured */
3207
3208#endif // KMP_USE_DYNAMIC_LOCK
3209}
3210
3211/* try to acquire the lock */
3212int __kmpc_test_nest_lock(ident_t *loc, kmp_int32 gtid, void **user_lock) {
3213#if KMP_USE_DYNAMIC_LOCK
3214 int rc;
3215#if USE_ITT_BUILD
3216 __kmp_itt_lock_acquiring((kmp_user_lock_p)user_lock);
3217#endif
3218#if OMPT_SUPPORT && OMPT_OPTIONAL
3219 // This is the case, if called from omp_init_lock_with_hint:
3220 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
3221 if (!codeptr)
3222 codeptr = OMPT_GET_RETURN_ADDRESS(0);
3223 if (ompt_enabled.ompt_callback_mutex_acquire) {
3224 ompt_callbacks.ompt_callback(ompt_callback_mutex_acquire)(
3225 ompt_mutex_nest_lock, omp_lock_hint_none,
3226 __ompt_get_mutex_impl_type(user_lock),
3227 (ompt_wait_id_t)(uintptr_t)user_lock, codeptr);
3228 }
3229#endif
3230 rc = KMP_D_LOCK_FUNC(user_lock, test)((kmp_dyna_lock_t *)user_lock, gtid);
3231#if USE_ITT_BUILD
3232 if (rc) {
3233 __kmp_itt_lock_acquired((kmp_user_lock_p)user_lock);
3234 } else {
3235 __kmp_itt_lock_cancelled((kmp_user_lock_p)user_lock);
3236 }
3237#endif
3238#if OMPT_SUPPORT && OMPT_OPTIONAL
3239 if (ompt_enabled.enabled && rc) {
3240 if (rc == 1) {
3241 if (ompt_enabled.ompt_callback_mutex_acquired) {
3242 // lock_first
3243 ompt_callbacks.ompt_callback(ompt_callback_mutex_acquired)(
3244 ompt_mutex_nest_lock, (ompt_wait_id_t)(uintptr_t)user_lock,
3245 codeptr);
3246 }
3247 } else {
3248 if (ompt_enabled.ompt_callback_nest_lock) {
3249 // lock_next
3250 ompt_callbacks.ompt_callback(ompt_callback_nest_lock)(
3251 ompt_scope_begin, (ompt_wait_id_t)(uintptr_t)user_lock, codeptr);
3252 }
3253 }
3254 }
3255#endif
3256 return rc;
3257
3258#else // KMP_USE_DYNAMIC_LOCK
3259
3260 kmp_user_lock_p lck;
3261 int rc;
3262
3263 if ((__kmp_user_lock_kind == lk_tas) &&
3264 (sizeof(lck->tas.lk.poll) + sizeof(lck->tas.lk.depth_locked) <=
3265 OMP_NEST_LOCK_T_SIZE)) {
3266 lck = (kmp_user_lock_p)user_lock;
3267 }
3268#if KMP_USE_FUTEX
3269 else if ((__kmp_user_lock_kind == lk_futex) &&
3270 (sizeof(lck->futex.lk.poll) + sizeof(lck->futex.lk.depth_locked) <=
3271 OMP_NEST_LOCK_T_SIZE)) {
3272 lck = (kmp_user_lock_p)user_lock;
3273 }
3274#endif
3275 else {
3276 lck = __kmp_lookup_user_lock(user_lock, "omp_test_nest_lock");
3277 }
3278
3279#if USE_ITT_BUILD
3280 __kmp_itt_lock_acquiring(lck);
3281#endif /* USE_ITT_BUILD */
3282
3283#if OMPT_SUPPORT && OMPT_OPTIONAL
3284 // This is the case, if called from omp_init_lock_with_hint:
3285 void *codeptr = OMPT_LOAD_RETURN_ADDRESS(gtid);
3286 if (!codeptr)
3287 codeptr = OMPT_GET_RETURN_ADDRESS(0);
3288 if (ompt_enabled.enabled) &&
3289 ompt_enabled.ompt_callback_mutex_acquire) {
3290 ompt_callbacks.ompt_callback(ompt_callback_mutex_acquire)(
3291 ompt_mutex_nest_lock, omp_lock_hint_none,
3292 __ompt_get_mutex_impl_type(), (ompt_wait_id_t)(uintptr_t)lck,
3293 codeptr);
3294 }
3295#endif
3296
3297 rc = TEST_NESTED_LOCK(lck, gtid);
3298#if USE_ITT_BUILD
3299 if (rc) {
3300 __kmp_itt_lock_acquired(lck);
3301 } else {
3302 __kmp_itt_lock_cancelled(lck);
3303 }
3304#endif /* USE_ITT_BUILD */
3305#if OMPT_SUPPORT && OMPT_OPTIONAL
3306 if (ompt_enabled.enabled && rc) {
3307 if (rc == 1) {
3308 if (ompt_enabled.ompt_callback_mutex_acquired) {
3309 // lock_first
3310 ompt_callbacks.ompt_callback(ompt_callback_mutex_acquired)(
3311 ompt_mutex_nest_lock, (ompt_wait_id_t)(uintptr_t)lck, codeptr);
3312 }
3313 } else {
3314 if (ompt_enabled.ompt_callback_nest_lock) {
3315 // lock_next
3316 ompt_callbacks.ompt_callback(ompt_callback_nest_lock)(
3317 ompt_mutex_scope_begin, (ompt_wait_id_t)(uintptr_t)lck, codeptr);
3318 }
3319 }
3320 }
3321#endif
3322 return rc;
3323
3324 /* Can't use serial interval since not block structured */
3325
3326#endif // KMP_USE_DYNAMIC_LOCK
3327}
3328
3329// Interface to fast scalable reduce methods routines
3330
3331// keep the selected method in a thread local structure for cross-function
3332// usage: will be used in __kmpc_end_reduce* functions;
3333// another solution: to re-determine the method one more time in
3334// __kmpc_end_reduce* functions (new prototype required then)
3335// AT: which solution is better?
3336#define __KMP_SET_REDUCTION_METHOD(gtid, rmethod) \
3337 ((__kmp_threads[(gtid)]->th.th_local.packed_reduction_method) = (rmethod))
3338
3339#define __KMP_GET_REDUCTION_METHOD(gtid) \
3340 (__kmp_threads[(gtid)]->th.th_local.packed_reduction_method)
3341
3342// description of the packed_reduction_method variable: look at the macros in
3343// kmp.h
3344
3345// used in a critical section reduce block
3346static __forceinline void
3347__kmp_enter_critical_section_reduce_block(ident_t *loc, kmp_int32 global_tid,
3348 kmp_critical_name *crit) {
3349
3350 // this lock was visible to a customer and to the threading profile tool as a
3351 // serial overhead span (although it's used for an internal purpose only)
3352 // why was it visible in previous implementation?
3353 // should we keep it visible in new reduce block?
3354 kmp_user_lock_p lck;
3355
3356#if KMP_USE_DYNAMIC_LOCK
3357
3358 kmp_dyna_lock_t *lk = (kmp_dyna_lock_t *)crit;
3359 // Check if it is initialized.
3360 if (*lk == 0) {
3361 if (KMP_IS_D_LOCK(__kmp_user_lock_seq)) {
3362 KMP_COMPARE_AND_STORE_ACQ32((volatile kmp_int32 *)crit, 0,
3363 KMP_GET_D_TAG(__kmp_user_lock_seq));
3364 } else {
3365 __kmp_init_indirect_csptr(crit, loc, global_tid,
3366 KMP_GET_I_TAG(__kmp_user_lock_seq));
3367 }
3368 }
3369 // Branch for accessing the actual lock object and set operation. This
3370 // branching is inevitable since this lock initialization does not follow the
3371 // normal dispatch path (lock table is not used).
3372 if (KMP_EXTRACT_D_TAG(lk) != 0) {
3373 lck = (kmp_user_lock_p)lk;
3374 KMP_DEBUG_ASSERT(lck != NULL);
3375 if (__kmp_env_consistency_check) {
3376 __kmp_push_sync(global_tid, ct_critical, loc, lck, __kmp_user_lock_seq);
3377 }
3378 KMP_D_LOCK_FUNC(lk, set)(lk, global_tid);
3379 } else {
3380 kmp_indirect_lock_t *ilk = *((kmp_indirect_lock_t **)lk);
3381 lck = ilk->lock;
3382 KMP_DEBUG_ASSERT(lck != NULL);
3383 if (__kmp_env_consistency_check) {
3384 __kmp_push_sync(global_tid, ct_critical, loc, lck, __kmp_user_lock_seq);
3385 }
3386 KMP_I_LOCK_FUNC(ilk, set)(lck, global_tid);
3387 }
3388
3389#else // KMP_USE_DYNAMIC_LOCK
3390
3391 // We know that the fast reduction code is only emitted by Intel compilers
3392 // with 32 byte critical sections. If there isn't enough space, then we
3393 // have to use a pointer.
3394 if (__kmp_base_user_lock_size <= INTEL_CRITICAL_SIZE) {
3395 lck = (kmp_user_lock_p)crit;
3396 } else {
3397 lck = __kmp_get_critical_section_ptr(crit, loc, global_tid);
3398 }
3399 KMP_DEBUG_ASSERT(lck != NULL);
3400
3401 if (__kmp_env_consistency_check)
3402 __kmp_push_sync(global_tid, ct_critical, loc, lck);
3403
3404 __kmp_acquire_user_lock_with_checks(lck, global_tid);
3405
3406#endif // KMP_USE_DYNAMIC_LOCK
3407}
3408
3409// used in a critical section reduce block
3410static __forceinline void
3411__kmp_end_critical_section_reduce_block(ident_t *loc, kmp_int32 global_tid,
3412 kmp_critical_name *crit) {
3413
3414 kmp_user_lock_p lck;
3415
3416#if KMP_USE_DYNAMIC_LOCK
3417
3418 if (KMP_IS_D_LOCK(__kmp_user_lock_seq)) {
3419 lck = (kmp_user_lock_p)crit;
3420 if (__kmp_env_consistency_check)
3421 __kmp_pop_sync(global_tid, ct_critical, loc);
3422 KMP_D_LOCK_FUNC(lck, unset)((kmp_dyna_lock_t *)lck, global_tid);
3423 } else {
3424 kmp_indirect_lock_t *ilk =
3425 (kmp_indirect_lock_t *)TCR_PTR(*((kmp_indirect_lock_t **)crit));
3426 if (__kmp_env_consistency_check)
3427 __kmp_pop_sync(global_tid, ct_critical, loc);
3428 KMP_I_LOCK_FUNC(ilk, unset)(ilk->lock, global_tid);
3429 }
3430
3431#else // KMP_USE_DYNAMIC_LOCK
3432
3433 // We know that the fast reduction code is only emitted by Intel compilers
3434 // with 32 byte critical sections. If there isn't enough space, then we have
3435 // to use a pointer.
3436 if (__kmp_base_user_lock_size > 32) {
3437 lck = *((kmp_user_lock_p *)crit);
3438 KMP_ASSERT(lck != NULL);
3439 } else {
3440 lck = (kmp_user_lock_p)crit;
3441 }
3442
3443 if (__kmp_env_consistency_check)
3444 __kmp_pop_sync(global_tid, ct_critical, loc);
3445
3446 __kmp_release_user_lock_with_checks(lck, global_tid);
3447
3448#endif // KMP_USE_DYNAMIC_LOCK
3449} // __kmp_end_critical_section_reduce_block
3450
3451static __forceinline int
3452__kmp_swap_teams_for_teams_reduction(kmp_info_t *th, kmp_team_t **team_p,
3453 int *task_state) {
3454 kmp_team_t *team;
3455
3456 // Check if we are inside the teams construct?
3457 if (th->th.th_teams_microtask) {
3458 *team_p = team = th->th.th_team;
3459 if (team->t.t_level == th->th.th_teams_level) {
3460 // This is reduction at teams construct.
3461 KMP_DEBUG_ASSERT(!th->th.th_info.ds.ds_tid); // AC: check that tid == 0
3462 // Let's swap teams temporarily for the reduction.
3463 th->th.th_info.ds.ds_tid = team->t.t_master_tid;
3464 th->th.th_team = team->t.t_parent;
3465 th->th.th_team_nproc = th->th.th_team->t.t_nproc;
3466 th->th.th_task_team = th->th.th_team->t.t_task_team[0];
3467 *task_state = th->th.th_task_state;
3468 th->th.th_task_state = 0;
3469
3470 return 1;
3471 }
3472 }
3473 return 0;
3474}
3475
3476static __forceinline void
3477__kmp_restore_swapped_teams(kmp_info_t *th, kmp_team_t *team, int task_state) {
3478 // Restore thread structure swapped in __kmp_swap_teams_for_teams_reduction.
3479 th->th.th_info.ds.ds_tid = 0;
3480 th->th.th_team = team;
3481 th->th.th_team_nproc = team->t.t_nproc;
3482 th->th.th_task_team = team->t.t_task_team[task_state];
3483 __kmp_type_convert(task_state, &(th->th.th_task_state));
3484}
3485
3486/* 2.a.i. Reduce Block without a terminating barrier */
3502kmp_int32
3503__kmpc_reduce_nowait(ident_t *loc, kmp_int32 global_tid, kmp_int32 num_vars,
3504 size_t reduce_size, void *reduce_data,
3505 void (*reduce_func)(void *lhs_data, void *rhs_data),
3506 kmp_critical_name *lck) {
3507
3508 KMP_COUNT_BLOCK(REDUCE_nowait);
3509 int retval = 0;
3510 PACKED_REDUCTION_METHOD_T packed_reduction_method;
3511 kmp_info_t *th;
3512 kmp_team_t *team;
3513 int teams_swapped = 0, task_state;
3514 KA_TRACE(10, ("__kmpc_reduce_nowait() enter: called T#%d\n", global_tid));
3515 __kmp_assert_valid_gtid(global_tid);
3516
3517 // why do we need this initialization here at all?
3518 // Reduction clause can not be used as a stand-alone directive.
3519
3520 // do not call __kmp_serial_initialize(), it will be called by
3521 // __kmp_parallel_initialize() if needed
3522 // possible detection of false-positive race by the threadchecker ???
3523 if (!TCR_4(__kmp_init_parallel))
3524 __kmp_parallel_initialize();
3525
3526 __kmp_resume_if_soft_paused();
3527
3528// check correctness of reduce block nesting
3529#if KMP_USE_DYNAMIC_LOCK
3530 if (__kmp_env_consistency_check)
3531 __kmp_push_sync(global_tid, ct_reduce, loc, NULL, 0);
3532#else
3533 if (__kmp_env_consistency_check)
3534 __kmp_push_sync(global_tid, ct_reduce, loc, NULL);
3535#endif
3536
3537 th = __kmp_thread_from_gtid(global_tid);
3538 teams_swapped = __kmp_swap_teams_for_teams_reduction(th, &team, &task_state);
3539
3540 // packed_reduction_method value will be reused by __kmp_end_reduce* function,
3541 // the value should be kept in a variable
3542 // the variable should be either a construct-specific or thread-specific
3543 // property, not a team specific property
3544 // (a thread can reach the next reduce block on the next construct, reduce
3545 // method may differ on the next construct)
3546 // an ident_t "loc" parameter could be used as a construct-specific property
3547 // (what if loc == 0?)
3548 // (if both construct-specific and team-specific variables were shared,
3549 // then unness extra syncs should be needed)
3550 // a thread-specific variable is better regarding two issues above (next
3551 // construct and extra syncs)
3552 // a thread-specific "th_local.reduction_method" variable is used currently
3553 // each thread executes 'determine' and 'set' lines (no need to execute by one
3554 // thread, to avoid unness extra syncs)
3555
3556 packed_reduction_method = __kmp_determine_reduction_method(
3557 loc, global_tid, num_vars, reduce_size, reduce_data, reduce_func, lck);
3558 __KMP_SET_REDUCTION_METHOD(global_tid, packed_reduction_method);
3559
3560 OMPT_REDUCTION_DECL(th, global_tid);
3561 if (packed_reduction_method == critical_reduce_block) {
3562
3563 OMPT_REDUCTION_BEGIN;
3564
3565 __kmp_enter_critical_section_reduce_block(loc, global_tid, lck);
3566 retval = 1;
3567
3568 } else if (packed_reduction_method == empty_reduce_block) {
3569
3570 OMPT_REDUCTION_BEGIN;
3571
3572 // usage: if team size == 1, no synchronization is required ( Intel
3573 // platforms only )
3574 retval = 1;
3575
3576 } else if (packed_reduction_method == atomic_reduce_block) {
3577
3578 retval = 2;
3579
3580 // all threads should do this pop here (because __kmpc_end_reduce_nowait()
3581 // won't be called by the code gen)
3582 // (it's not quite good, because the checking block has been closed by
3583 // this 'pop',
3584 // but atomic operation has not been executed yet, will be executed
3585 // slightly later, literally on next instruction)
3586 if (__kmp_env_consistency_check)
3587 __kmp_pop_sync(global_tid, ct_reduce, loc);
3588
3589 } else if (TEST_REDUCTION_METHOD(packed_reduction_method,
3590 tree_reduce_block)) {
3591
3592// AT: performance issue: a real barrier here
3593// AT: (if primary thread is slow, other threads are blocked here waiting for
3594// the primary thread to come and release them)
3595// AT: (it's not what a customer might expect specifying NOWAIT clause)
3596// AT: (specifying NOWAIT won't result in improvement of performance, it'll
3597// be confusing to a customer)
3598// AT: another implementation of *barrier_gather*nowait() (or some other design)
3599// might go faster and be more in line with sense of NOWAIT
3600// AT: TO DO: do epcc test and compare times
3601
3602// this barrier should be invisible to a customer and to the threading profile
3603// tool (it's neither a terminating barrier nor customer's code, it's
3604// used for an internal purpose)
3605#if OMPT_SUPPORT
3606 // JP: can this barrier potentially leed to task scheduling?
3607 // JP: as long as there is a barrier in the implementation, OMPT should and
3608 // will provide the barrier events
3609 // so we set-up the necessary frame/return addresses.
3610 ompt_frame_t *ompt_frame;
3611 if (ompt_enabled.enabled) {
3612 __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
3613 if (ompt_frame->enter_frame.ptr == NULL)
3614 ompt_frame->enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
3615 }
3616 OMPT_STORE_RETURN_ADDRESS(global_tid);
3617#endif
3618#if USE_ITT_NOTIFY
3619 __kmp_threads[global_tid]->th.th_ident = loc;
3620#endif
3621 retval =
3622 __kmp_barrier(UNPACK_REDUCTION_BARRIER(packed_reduction_method),
3623 global_tid, FALSE, reduce_size, reduce_data, reduce_func);
3624 retval = (retval != 0) ? (0) : (1);
3625#if OMPT_SUPPORT && OMPT_OPTIONAL
3626 if (ompt_enabled.enabled) {
3627 ompt_frame->enter_frame = ompt_data_none;
3628 }
3629#endif
3630
3631 // all other workers except primary thread should do this pop here
3632 // ( none of other workers will get to __kmpc_end_reduce_nowait() )
3633 if (__kmp_env_consistency_check) {
3634 if (retval == 0) {
3635 __kmp_pop_sync(global_tid, ct_reduce, loc);
3636 }
3637 }
3638
3639 } else {
3640
3641 // should never reach this block
3642 KMP_ASSERT(0); // "unexpected method"
3643 }
3644 if (teams_swapped) {
3645 __kmp_restore_swapped_teams(th, team, task_state);
3646 }
3647 KA_TRACE(
3648 10,
3649 ("__kmpc_reduce_nowait() exit: called T#%d: method %08x, returns %08x\n",
3650 global_tid, packed_reduction_method, retval));
3651
3652 return retval;
3653}
3654
3663void __kmpc_end_reduce_nowait(ident_t *loc, kmp_int32 global_tid,
3664 kmp_critical_name *lck) {
3665
3666 PACKED_REDUCTION_METHOD_T packed_reduction_method;
3667
3668 KA_TRACE(10, ("__kmpc_end_reduce_nowait() enter: called T#%d\n", global_tid));
3669 __kmp_assert_valid_gtid(global_tid);
3670
3671 packed_reduction_method = __KMP_GET_REDUCTION_METHOD(global_tid);
3672
3673 OMPT_REDUCTION_DECL(__kmp_thread_from_gtid(global_tid), global_tid);
3674
3675 if (packed_reduction_method == critical_reduce_block) {
3676
3677 __kmp_end_critical_section_reduce_block(loc, global_tid, lck);
3678 OMPT_REDUCTION_END;
3679
3680 } else if (packed_reduction_method == empty_reduce_block) {
3681
3682 // usage: if team size == 1, no synchronization is required ( on Intel
3683 // platforms only )
3684
3685 OMPT_REDUCTION_END;
3686
3687 } else if (packed_reduction_method == atomic_reduce_block) {
3688
3689 // neither primary thread nor other workers should get here
3690 // (code gen does not generate this call in case 2: atomic reduce block)
3691 // actually it's better to remove this elseif at all;
3692 // after removal this value will checked by the 'else' and will assert
3693
3694 } else if (TEST_REDUCTION_METHOD(packed_reduction_method,
3695 tree_reduce_block)) {
3696
3697 // only primary thread gets here
3698 // OMPT: tree reduction is annotated in the barrier code
3699
3700 } else {
3701
3702 // should never reach this block
3703 KMP_ASSERT(0); // "unexpected method"
3704 }
3705
3706 if (__kmp_env_consistency_check)
3707 __kmp_pop_sync(global_tid, ct_reduce, loc);
3708
3709 KA_TRACE(10, ("__kmpc_end_reduce_nowait() exit: called T#%d: method %08x\n",
3710 global_tid, packed_reduction_method));
3711
3712 return;
3713}
3714
3715/* 2.a.ii. Reduce Block with a terminating barrier */
3716
3732kmp_int32 __kmpc_reduce(ident_t *loc, kmp_int32 global_tid, kmp_int32 num_vars,
3733 size_t reduce_size, void *reduce_data,
3734 void (*reduce_func)(void *lhs_data, void *rhs_data),
3735 kmp_critical_name *lck) {
3736 KMP_COUNT_BLOCK(REDUCE_wait);
3737 int retval = 0;
3738 PACKED_REDUCTION_METHOD_T packed_reduction_method;
3739 kmp_info_t *th;
3740 kmp_team_t *team;
3741 int teams_swapped = 0, task_state;
3742
3743 KA_TRACE(10, ("__kmpc_reduce() enter: called T#%d\n", global_tid));
3744 __kmp_assert_valid_gtid(global_tid);
3745
3746 // why do we need this initialization here at all?
3747 // Reduction clause can not be a stand-alone directive.
3748
3749 // do not call __kmp_serial_initialize(), it will be called by
3750 // __kmp_parallel_initialize() if needed
3751 // possible detection of false-positive race by the threadchecker ???
3752 if (!TCR_4(__kmp_init_parallel))
3753 __kmp_parallel_initialize();
3754
3755 __kmp_resume_if_soft_paused();
3756
3757// check correctness of reduce block nesting
3758#if KMP_USE_DYNAMIC_LOCK
3759 if (__kmp_env_consistency_check)
3760 __kmp_push_sync(global_tid, ct_reduce, loc, NULL, 0);
3761#else
3762 if (__kmp_env_consistency_check)
3763 __kmp_push_sync(global_tid, ct_reduce, loc, NULL);
3764#endif
3765
3766 th = __kmp_thread_from_gtid(global_tid);
3767 teams_swapped = __kmp_swap_teams_for_teams_reduction(th, &team, &task_state);
3768
3769 packed_reduction_method = __kmp_determine_reduction_method(
3770 loc, global_tid, num_vars, reduce_size, reduce_data, reduce_func, lck);
3771 __KMP_SET_REDUCTION_METHOD(global_tid, packed_reduction_method);
3772
3773 OMPT_REDUCTION_DECL(th, global_tid);
3774
3775 if (packed_reduction_method == critical_reduce_block) {
3776
3777 OMPT_REDUCTION_BEGIN;
3778 __kmp_enter_critical_section_reduce_block(loc, global_tid, lck);
3779 retval = 1;
3780
3781 } else if (packed_reduction_method == empty_reduce_block) {
3782
3783 OMPT_REDUCTION_BEGIN;
3784 // usage: if team size == 1, no synchronization is required ( Intel
3785 // platforms only )
3786 retval = 1;
3787
3788 } else if (packed_reduction_method == atomic_reduce_block) {
3789
3790 retval = 2;
3791
3792 } else if (TEST_REDUCTION_METHOD(packed_reduction_method,
3793 tree_reduce_block)) {
3794
3795// case tree_reduce_block:
3796// this barrier should be visible to a customer and to the threading profile
3797// tool (it's a terminating barrier on constructs if NOWAIT not specified)
3798#if OMPT_SUPPORT
3799 ompt_frame_t *ompt_frame;
3800 if (ompt_enabled.enabled) {
3801 __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
3802 if (ompt_frame->enter_frame.ptr == NULL)
3803 ompt_frame->enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
3804 }
3805 OMPT_STORE_RETURN_ADDRESS(global_tid);
3806#endif
3807#if USE_ITT_NOTIFY
3808 __kmp_threads[global_tid]->th.th_ident =
3809 loc; // needed for correct notification of frames
3810#endif
3811 retval =
3812 __kmp_barrier(UNPACK_REDUCTION_BARRIER(packed_reduction_method),
3813 global_tid, TRUE, reduce_size, reduce_data, reduce_func);
3814 retval = (retval != 0) ? (0) : (1);
3815#if OMPT_SUPPORT && OMPT_OPTIONAL
3816 if (ompt_enabled.enabled) {
3817 ompt_frame->enter_frame = ompt_data_none;
3818 }
3819#endif
3820
3821 // all other workers except primary thread should do this pop here
3822 // (none of other workers except primary will enter __kmpc_end_reduce())
3823 if (__kmp_env_consistency_check) {
3824 if (retval == 0) { // 0: all other workers; 1: primary thread
3825 __kmp_pop_sync(global_tid, ct_reduce, loc);
3826 }
3827 }
3828
3829 } else {
3830
3831 // should never reach this block
3832 KMP_ASSERT(0); // "unexpected method"
3833 }
3834 if (teams_swapped) {
3835 __kmp_restore_swapped_teams(th, team, task_state);
3836 }
3837
3838 KA_TRACE(10,
3839 ("__kmpc_reduce() exit: called T#%d: method %08x, returns %08x\n",
3840 global_tid, packed_reduction_method, retval));
3841 return retval;
3842}
3843
3854void __kmpc_end_reduce(ident_t *loc, kmp_int32 global_tid,
3855 kmp_critical_name *lck) {
3856
3857 PACKED_REDUCTION_METHOD_T packed_reduction_method;
3858 kmp_info_t *th;
3859 kmp_team_t *team;
3860 int teams_swapped = 0, task_state;
3861
3862 KA_TRACE(10, ("__kmpc_end_reduce() enter: called T#%d\n", global_tid));
3863 __kmp_assert_valid_gtid(global_tid);
3864
3865 th = __kmp_thread_from_gtid(global_tid);
3866 teams_swapped = __kmp_swap_teams_for_teams_reduction(th, &team, &task_state);
3867
3868 packed_reduction_method = __KMP_GET_REDUCTION_METHOD(global_tid);
3869
3870 // this barrier should be visible to a customer and to the threading profile
3871 // tool (it's a terminating barrier on constructs if NOWAIT not specified)
3872 OMPT_REDUCTION_DECL(th, global_tid);
3873
3874 if (packed_reduction_method == critical_reduce_block) {
3875 __kmp_end_critical_section_reduce_block(loc, global_tid, lck);
3876
3877 OMPT_REDUCTION_END;
3878
3879// TODO: implicit barrier: should be exposed
3880#if OMPT_SUPPORT
3881 ompt_frame_t *ompt_frame;
3882 if (ompt_enabled.enabled) {
3883 __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
3884 if (ompt_frame->enter_frame.ptr == NULL)
3885 ompt_frame->enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
3886 }
3887 OMPT_STORE_RETURN_ADDRESS(global_tid);
3888#endif
3889#if USE_ITT_NOTIFY
3890 __kmp_threads[global_tid]->th.th_ident = loc;
3891#endif
3892 __kmp_barrier(bs_plain_barrier, global_tid, FALSE, 0, NULL, NULL);
3893#if OMPT_SUPPORT && OMPT_OPTIONAL
3894 if (ompt_enabled.enabled) {
3895 ompt_frame->enter_frame = ompt_data_none;
3896 }
3897#endif
3898
3899 } else if (packed_reduction_method == empty_reduce_block) {
3900
3901 OMPT_REDUCTION_END;
3902
3903// usage: if team size==1, no synchronization is required (Intel platforms only)
3904
3905// TODO: implicit barrier: should be exposed
3906#if OMPT_SUPPORT
3907 ompt_frame_t *ompt_frame;
3908 if (ompt_enabled.enabled) {
3909 __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
3910 if (ompt_frame->enter_frame.ptr == NULL)
3911 ompt_frame->enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
3912 }
3913 OMPT_STORE_RETURN_ADDRESS(global_tid);
3914#endif
3915#if USE_ITT_NOTIFY
3916 __kmp_threads[global_tid]->th.th_ident = loc;
3917#endif
3918 __kmp_barrier(bs_plain_barrier, global_tid, FALSE, 0, NULL, NULL);
3919#if OMPT_SUPPORT && OMPT_OPTIONAL
3920 if (ompt_enabled.enabled) {
3921 ompt_frame->enter_frame = ompt_data_none;
3922 }
3923#endif
3924
3925 } else if (packed_reduction_method == atomic_reduce_block) {
3926
3927#if OMPT_SUPPORT
3928 ompt_frame_t *ompt_frame;
3929 if (ompt_enabled.enabled) {
3930 __ompt_get_task_info_internal(0, NULL, NULL, &ompt_frame, NULL, NULL);
3931 if (ompt_frame->enter_frame.ptr == NULL)
3932 ompt_frame->enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
3933 }
3934 OMPT_STORE_RETURN_ADDRESS(global_tid);
3935#endif
3936// TODO: implicit barrier: should be exposed
3937#if USE_ITT_NOTIFY
3938 __kmp_threads[global_tid]->th.th_ident = loc;
3939#endif
3940 __kmp_barrier(bs_plain_barrier, global_tid, FALSE, 0, NULL, NULL);
3941#if OMPT_SUPPORT && OMPT_OPTIONAL
3942 if (ompt_enabled.enabled) {
3943 ompt_frame->enter_frame = ompt_data_none;
3944 }
3945#endif
3946
3947 } else if (TEST_REDUCTION_METHOD(packed_reduction_method,
3948 tree_reduce_block)) {
3949
3950 // only primary thread executes here (primary releases all other workers)
3951 __kmp_end_split_barrier(UNPACK_REDUCTION_BARRIER(packed_reduction_method),
3952 global_tid);
3953
3954 } else {
3955
3956 // should never reach this block
3957 KMP_ASSERT(0); // "unexpected method"
3958 }
3959 if (teams_swapped) {
3960 __kmp_restore_swapped_teams(th, team, task_state);
3961 }
3962
3963 if (__kmp_env_consistency_check)
3964 __kmp_pop_sync(global_tid, ct_reduce, loc);
3965
3966 KA_TRACE(10, ("__kmpc_end_reduce() exit: called T#%d: method %08x\n",
3967 global_tid, packed_reduction_method));
3968
3969 return;
3970}
3971
3972#undef __KMP_GET_REDUCTION_METHOD
3973#undef __KMP_SET_REDUCTION_METHOD
3974
3975/* end of interface to fast scalable reduce routines */
3976
3977kmp_uint64 __kmpc_get_taskid() {
3978
3979 kmp_int32 gtid;
3980 kmp_info_t *thread;
3981
3982 gtid = __kmp_get_gtid();
3983 if (gtid < 0) {
3984 return 0;
3985 }
3986 thread = __kmp_thread_from_gtid(gtid);
3987 return thread->th.th_current_task->td_task_id;
3988
3989} // __kmpc_get_taskid
3990
3991kmp_uint64 __kmpc_get_parent_taskid() {
3992
3993 kmp_int32 gtid;
3994 kmp_info_t *thread;
3995 kmp_taskdata_t *parent_task;
3996
3997 gtid = __kmp_get_gtid();
3998 if (gtid < 0) {
3999 return 0;
4000 }
4001 thread = __kmp_thread_from_gtid(gtid);
4002 parent_task = thread->th.th_current_task->td_parent;
4003 return (parent_task == NULL ? 0 : parent_task->td_task_id);
4004
4005} // __kmpc_get_parent_taskid
4006
4018void __kmpc_doacross_init(ident_t *loc, int gtid, int num_dims,
4019 const struct kmp_dim *dims) {
4020 __kmp_assert_valid_gtid(gtid);
4021 int j, idx;
4022 kmp_int64 last, trace_count;
4023 kmp_info_t *th = __kmp_threads[gtid];
4024 kmp_team_t *team = th->th.th_team;
4025 kmp_uint32 *flags;
4026 kmp_disp_t *pr_buf = th->th.th_dispatch;
4027 dispatch_shared_info_t *sh_buf;
4028
4029 KA_TRACE(
4030 20,
4031 ("__kmpc_doacross_init() enter: called T#%d, num dims %d, active %d\n",
4032 gtid, num_dims, !team->t.t_serialized));
4033 KMP_DEBUG_ASSERT(dims != NULL);
4034 KMP_DEBUG_ASSERT(num_dims > 0);
4035
4036 if (team->t.t_serialized) {
4037 KA_TRACE(20, ("__kmpc_doacross_init() exit: serialized team\n"));
4038 return; // no dependencies if team is serialized
4039 }
4040 KMP_DEBUG_ASSERT(team->t.t_nproc > 1);
4041 idx = pr_buf->th_doacross_buf_idx++; // Increment index of shared buffer for
4042 // the next loop
4043 sh_buf = &team->t.t_disp_buffer[idx % __kmp_dispatch_num_buffers];
4044
4045 // Save bounds info into allocated private buffer
4046 KMP_DEBUG_ASSERT(pr_buf->th_doacross_info == NULL);
4047 pr_buf->th_doacross_info = (kmp_int64 *)__kmp_thread_malloc(
4048 th, sizeof(kmp_int64) * (4 * num_dims + 1));
4049 KMP_DEBUG_ASSERT(pr_buf->th_doacross_info != NULL);
4050 pr_buf->th_doacross_info[0] =
4051 (kmp_int64)num_dims; // first element is number of dimensions
4052 // Save also address of num_done in order to access it later without knowing
4053 // the buffer index
4054 pr_buf->th_doacross_info[1] = (kmp_int64)&sh_buf->doacross_num_done;
4055 pr_buf->th_doacross_info[2] = dims[0].lo;
4056 pr_buf->th_doacross_info[3] = dims[0].up;
4057 pr_buf->th_doacross_info[4] = dims[0].st;
4058 last = 5;
4059 for (j = 1; j < num_dims; ++j) {
4060 kmp_int64
4061 range_length; // To keep ranges of all dimensions but the first dims[0]
4062 if (dims[j].st == 1) { // most common case
4063 // AC: should we care of ranges bigger than LLONG_MAX? (not for now)
4064 range_length = dims[j].up - dims[j].lo + 1;
4065 } else {
4066 if (dims[j].st > 0) {
4067 KMP_DEBUG_ASSERT(dims[j].up > dims[j].lo);
4068 range_length = (kmp_uint64)(dims[j].up - dims[j].lo) / dims[j].st + 1;
4069 } else { // negative increment
4070 KMP_DEBUG_ASSERT(dims[j].lo > dims[j].up);
4071 range_length =
4072 (kmp_uint64)(dims[j].lo - dims[j].up) / (-dims[j].st) + 1;
4073 }
4074 }
4075 pr_buf->th_doacross_info[last++] = range_length;
4076 pr_buf->th_doacross_info[last++] = dims[j].lo;
4077 pr_buf->th_doacross_info[last++] = dims[j].up;
4078 pr_buf->th_doacross_info[last++] = dims[j].st;
4079 }
4080
4081 // Compute total trip count.
4082 // Start with range of dims[0] which we don't need to keep in the buffer.
4083 if (dims[0].st == 1) { // most common case
4084 trace_count = dims[0].up - dims[0].lo + 1;
4085 } else if (dims[0].st > 0) {
4086 KMP_DEBUG_ASSERT(dims[0].up > dims[0].lo);
4087 trace_count = (kmp_uint64)(dims[0].up - dims[0].lo) / dims[0].st + 1;
4088 } else { // negative increment
4089 KMP_DEBUG_ASSERT(dims[0].lo > dims[0].up);
4090 trace_count = (kmp_uint64)(dims[0].lo - dims[0].up) / (-dims[0].st) + 1;
4091 }
4092 for (j = 1; j < num_dims; ++j) {
4093 trace_count *= pr_buf->th_doacross_info[4 * j + 1]; // use kept ranges
4094 }
4095 KMP_DEBUG_ASSERT(trace_count > 0);
4096
4097 // Check if shared buffer is not occupied by other loop (idx -
4098 // __kmp_dispatch_num_buffers)
4099 if (idx != sh_buf->doacross_buf_idx) {
4100 // Shared buffer is occupied, wait for it to be free
4101 __kmp_wait_4((volatile kmp_uint32 *)&sh_buf->doacross_buf_idx, idx,
4102 __kmp_eq_4, NULL);
4103 }
4104#if KMP_32_BIT_ARCH
4105 // Check if we are the first thread. After the CAS the first thread gets 0,
4106 // others get 1 if initialization is in progress, allocated pointer otherwise.
4107 // Treat pointer as volatile integer (value 0 or 1) until memory is allocated.
4108 flags = (kmp_uint32 *)KMP_COMPARE_AND_STORE_RET32(
4109 (volatile kmp_int32 *)&sh_buf->doacross_flags, NULL, 1);
4110#else
4111 flags = (kmp_uint32 *)KMP_COMPARE_AND_STORE_RET64(
4112 (volatile kmp_int64 *)&sh_buf->doacross_flags, NULL, 1LL);
4113#endif
4114 if (flags == NULL) {
4115 // we are the first thread, allocate the array of flags
4116 size_t size =
4117 (size_t)trace_count / 8 + 8; // in bytes, use single bit per iteration
4118 flags = (kmp_uint32 *)__kmp_thread_calloc(th, size, 1);
4119 KMP_MB();
4120 sh_buf->doacross_flags = flags;
4121 } else if (flags == (kmp_uint32 *)1) {
4122#if KMP_32_BIT_ARCH
4123 // initialization is still in progress, need to wait
4124 while (*(volatile kmp_int32 *)&sh_buf->doacross_flags == 1)
4125#else
4126 while (*(volatile kmp_int64 *)&sh_buf->doacross_flags == 1LL)
4127#endif
4128 KMP_YIELD(TRUE);
4129 KMP_MB();
4130 } else {
4131 KMP_MB();
4132 }
4133 KMP_DEBUG_ASSERT(sh_buf->doacross_flags > (kmp_uint32 *)1); // check ptr value
4134 pr_buf->th_doacross_flags =
4135 sh_buf->doacross_flags; // save private copy in order to not
4136 // touch shared buffer on each iteration
4137 KA_TRACE(20, ("__kmpc_doacross_init() exit: T#%d\n", gtid));
4138}
4139
4140void __kmpc_doacross_wait(ident_t *loc, int gtid, const kmp_int64 *vec) {
4141 __kmp_assert_valid_gtid(gtid);
4142 kmp_int64 shft;
4143 size_t num_dims, i;
4144 kmp_uint32 flag;
4145 kmp_int64 iter_number; // iteration number of "collapsed" loop nest
4146 kmp_info_t *th = __kmp_threads[gtid];
4147 kmp_team_t *team = th->th.th_team;
4148 kmp_disp_t *pr_buf;
4149 kmp_int64 lo, up, st;
4150
4151 KA_TRACE(20, ("__kmpc_doacross_wait() enter: called T#%d\n", gtid));
4152 if (team->t.t_serialized) {
4153 KA_TRACE(20, ("__kmpc_doacross_wait() exit: serialized team\n"));
4154 return; // no dependencies if team is serialized
4155 }
4156
4157 // calculate sequential iteration number and check out-of-bounds condition
4158 pr_buf = th->th.th_dispatch;
4159 KMP_DEBUG_ASSERT(pr_buf->th_doacross_info != NULL);
4160 num_dims = (size_t)pr_buf->th_doacross_info[0];
4161 lo = pr_buf->th_doacross_info[2];
4162 up = pr_buf->th_doacross_info[3];
4163 st = pr_buf->th_doacross_info[4];
4164#if OMPT_SUPPORT && OMPT_OPTIONAL
4165 ompt_dependence_t deps[num_dims];
4166#endif
4167 if (st == 1) { // most common case
4168 if (vec[0] < lo || vec[0] > up) {
4169 KA_TRACE(20, ("__kmpc_doacross_wait() exit: T#%d iter %lld is out of "
4170 "bounds [%lld,%lld]\n",
4171 gtid, vec[0], lo, up));
4172 return;
4173 }
4174 iter_number = vec[0] - lo;
4175 } else if (st > 0) {
4176 if (vec[0] < lo || vec[0] > up) {
4177 KA_TRACE(20, ("__kmpc_doacross_wait() exit: T#%d iter %lld is out of "
4178 "bounds [%lld,%lld]\n",
4179 gtid, vec[0], lo, up));
4180 return;
4181 }
4182 iter_number = (kmp_uint64)(vec[0] - lo) / st;
4183 } else { // negative increment
4184 if (vec[0] > lo || vec[0] < up) {
4185 KA_TRACE(20, ("__kmpc_doacross_wait() exit: T#%d iter %lld is out of "
4186 "bounds [%lld,%lld]\n",
4187 gtid, vec[0], lo, up));
4188 return;
4189 }
4190 iter_number = (kmp_uint64)(lo - vec[0]) / (-st);
4191 }
4192#if OMPT_SUPPORT && OMPT_OPTIONAL
4193 deps[0].variable.value = iter_number;
4194 deps[0].dependence_type = ompt_dependence_type_sink;
4195#endif
4196 for (i = 1; i < num_dims; ++i) {
4197 kmp_int64 iter, ln;
4198 size_t j = i * 4;
4199 ln = pr_buf->th_doacross_info[j + 1];
4200 lo = pr_buf->th_doacross_info[j + 2];
4201 up = pr_buf->th_doacross_info[j + 3];
4202 st = pr_buf->th_doacross_info[j + 4];
4203 if (st == 1) {
4204 if (vec[i] < lo || vec[i] > up) {
4205 KA_TRACE(20, ("__kmpc_doacross_wait() exit: T#%d iter %lld is out of "
4206 "bounds [%lld,%lld]\n",
4207 gtid, vec[i], lo, up));
4208 return;
4209 }
4210 iter = vec[i] - lo;
4211 } else if (st > 0) {
4212 if (vec[i] < lo || vec[i] > up) {
4213 KA_TRACE(20, ("__kmpc_doacross_wait() exit: T#%d iter %lld is out of "
4214 "bounds [%lld,%lld]\n",
4215 gtid, vec[i], lo, up));
4216 return;
4217 }
4218 iter = (kmp_uint64)(vec[i] - lo) / st;
4219 } else { // st < 0
4220 if (vec[i] > lo || vec[i] < up) {
4221 KA_TRACE(20, ("__kmpc_doacross_wait() exit: T#%d iter %lld is out of "
4222 "bounds [%lld,%lld]\n",
4223 gtid, vec[i], lo, up));
4224 return;
4225 }
4226 iter = (kmp_uint64)(lo - vec[i]) / (-st);
4227 }
4228 iter_number = iter + ln * iter_number;
4229#if OMPT_SUPPORT && OMPT_OPTIONAL
4230 deps[i].variable.value = iter;
4231 deps[i].dependence_type = ompt_dependence_type_sink;
4232#endif
4233 }
4234 shft = iter_number % 32; // use 32-bit granularity
4235 iter_number >>= 5; // divided by 32
4236 flag = 1 << shft;
4237 while ((flag & pr_buf->th_doacross_flags[iter_number]) == 0) {
4238 KMP_YIELD(TRUE);
4239 }
4240 KMP_MB();
4241#if OMPT_SUPPORT && OMPT_OPTIONAL
4242 if (ompt_enabled.ompt_callback_dependences) {
4243 ompt_callbacks.ompt_callback(ompt_callback_dependences)(
4244 &(OMPT_CUR_TASK_INFO(th)->task_data), deps, (kmp_uint32)num_dims);
4245 }
4246#endif
4247 KA_TRACE(20,
4248 ("__kmpc_doacross_wait() exit: T#%d wait for iter %lld completed\n",
4249 gtid, (iter_number << 5) + shft));
4250}
4251
4252void __kmpc_doacross_post(ident_t *loc, int gtid, const kmp_int64 *vec) {
4253 __kmp_assert_valid_gtid(gtid);
4254 kmp_int64 shft;
4255 size_t num_dims, i;
4256 kmp_uint32 flag;
4257 kmp_int64 iter_number; // iteration number of "collapsed" loop nest
4258 kmp_info_t *th = __kmp_threads[gtid];
4259 kmp_team_t *team = th->th.th_team;
4260 kmp_disp_t *pr_buf;
4261 kmp_int64 lo, st;
4262
4263 KA_TRACE(20, ("__kmpc_doacross_post() enter: called T#%d\n", gtid));
4264 if (team->t.t_serialized) {
4265 KA_TRACE(20, ("__kmpc_doacross_post() exit: serialized team\n"));
4266 return; // no dependencies if team is serialized
4267 }
4268
4269 // calculate sequential iteration number (same as in "wait" but no
4270 // out-of-bounds checks)
4271 pr_buf = th->th.th_dispatch;
4272 KMP_DEBUG_ASSERT(pr_buf->th_doacross_info != NULL);
4273 num_dims = (size_t)pr_buf->th_doacross_info[0];
4274 lo = pr_buf->th_doacross_info[2];
4275 st = pr_buf->th_doacross_info[4];
4276#if OMPT_SUPPORT && OMPT_OPTIONAL
4277 ompt_dependence_t deps[num_dims];
4278#endif
4279 if (st == 1) { // most common case
4280 iter_number = vec[0] - lo;
4281 } else if (st > 0) {
4282 iter_number = (kmp_uint64)(vec[0] - lo) / st;
4283 } else { // negative increment
4284 iter_number = (kmp_uint64)(lo - vec[0]) / (-st);
4285 }
4286#if OMPT_SUPPORT && OMPT_OPTIONAL
4287 deps[0].variable.value = iter_number;
4288 deps[0].dependence_type = ompt_dependence_type_source;
4289#endif
4290 for (i = 1; i < num_dims; ++i) {
4291 kmp_int64 iter, ln;
4292 size_t j = i * 4;
4293 ln = pr_buf->th_doacross_info[j + 1];
4294 lo = pr_buf->th_doacross_info[j + 2];
4295 st = pr_buf->th_doacross_info[j + 4];
4296 if (st == 1) {
4297 iter = vec[i] - lo;
4298 } else if (st > 0) {
4299 iter = (kmp_uint64)(vec[i] - lo) / st;
4300 } else { // st < 0
4301 iter = (kmp_uint64)(lo - vec[i]) / (-st);
4302 }
4303 iter_number = iter + ln * iter_number;
4304#if OMPT_SUPPORT && OMPT_OPTIONAL
4305 deps[i].variable.value = iter;
4306 deps[i].dependence_type = ompt_dependence_type_source;
4307#endif
4308 }
4309#if OMPT_SUPPORT && OMPT_OPTIONAL
4310 if (ompt_enabled.ompt_callback_dependences) {
4311 ompt_callbacks.ompt_callback(ompt_callback_dependences)(
4312 &(OMPT_CUR_TASK_INFO(th)->task_data), deps, (kmp_uint32)num_dims);
4313 }
4314#endif
4315 shft = iter_number % 32; // use 32-bit granularity
4316 iter_number >>= 5; // divided by 32
4317 flag = 1 << shft;
4318 KMP_MB();
4319 if ((flag & pr_buf->th_doacross_flags[iter_number]) == 0)
4320 KMP_TEST_THEN_OR32(&pr_buf->th_doacross_flags[iter_number], flag);
4321 KA_TRACE(20, ("__kmpc_doacross_post() exit: T#%d iter %lld posted\n", gtid,
4322 (iter_number << 5) + shft));
4323}
4324
4325void __kmpc_doacross_fini(ident_t *loc, int gtid) {
4326 __kmp_assert_valid_gtid(gtid);
4327 kmp_int32 num_done;
4328 kmp_info_t *th = __kmp_threads[gtid];
4329 kmp_team_t *team = th->th.th_team;
4330 kmp_disp_t *pr_buf = th->th.th_dispatch;
4331
4332 KA_TRACE(20, ("__kmpc_doacross_fini() enter: called T#%d\n", gtid));
4333 if (team->t.t_serialized) {
4334 KA_TRACE(20, ("__kmpc_doacross_fini() exit: serialized team %p\n", team));
4335 return; // nothing to do
4336 }
4337 num_done =
4338 KMP_TEST_THEN_INC32((kmp_uintptr_t)(pr_buf->th_doacross_info[1])) + 1;
4339 if (num_done == th->th.th_team_nproc) {
4340 // we are the last thread, need to free shared resources
4341 int idx = pr_buf->th_doacross_buf_idx - 1;
4342 dispatch_shared_info_t *sh_buf =
4343 &team->t.t_disp_buffer[idx % __kmp_dispatch_num_buffers];
4344 KMP_DEBUG_ASSERT(pr_buf->th_doacross_info[1] ==
4345 (kmp_int64)&sh_buf->doacross_num_done);
4346 KMP_DEBUG_ASSERT(num_done == sh_buf->doacross_num_done);
4347 KMP_DEBUG_ASSERT(idx == sh_buf->doacross_buf_idx);
4348 __kmp_thread_free(th, CCAST(kmp_uint32 *, sh_buf->doacross_flags));
4349 sh_buf->doacross_flags = NULL;
4350 sh_buf->doacross_num_done = 0;
4351 sh_buf->doacross_buf_idx +=
4352 __kmp_dispatch_num_buffers; // free buffer for future re-use
4353 }
4354 // free private resources (need to keep buffer index forever)
4355 pr_buf->th_doacross_flags = NULL;
4356 __kmp_thread_free(th, (void *)pr_buf->th_doacross_info);
4357 pr_buf->th_doacross_info = NULL;
4358 KA_TRACE(20, ("__kmpc_doacross_fini() exit: T#%d\n", gtid));
4359}
4360
4361/* OpenMP 5.1 Memory Management routines */
4362void *omp_alloc(size_t size, omp_allocator_handle_t allocator) {
4363 return __kmp_alloc(__kmp_entry_gtid(), 0, size, allocator);
4364}
4365
4366void *omp_aligned_alloc(size_t align, size_t size,
4367 omp_allocator_handle_t allocator) {
4368 return __kmp_alloc(__kmp_entry_gtid(), align, size, allocator);
4369}
4370
4371void *omp_calloc(size_t nmemb, size_t size, omp_allocator_handle_t allocator) {
4372 return __kmp_calloc(__kmp_entry_gtid(), 0, nmemb, size, allocator);
4373}
4374
4375void *omp_aligned_calloc(size_t align, size_t nmemb, size_t size,
4376 omp_allocator_handle_t allocator) {
4377 return __kmp_calloc(__kmp_entry_gtid(), align, nmemb, size, allocator);
4378}
4379
4380void *omp_realloc(void *ptr, size_t size, omp_allocator_handle_t allocator,
4381 omp_allocator_handle_t free_allocator) {
4382 return __kmp_realloc(__kmp_entry_gtid(), ptr, size, allocator,
4383 free_allocator);
4384}
4385
4386void omp_free(void *ptr, omp_allocator_handle_t allocator) {
4387 ___kmpc_free(__kmp_entry_gtid(), ptr, allocator);
4388}
4389/* end of OpenMP 5.1 Memory Management routines */
4390
4391int __kmpc_get_target_offload(void) {
4392 if (!__kmp_init_serial) {
4393 __kmp_serial_initialize();
4394 }
4395 return __kmp_target_offload;
4396}
4397
4398int __kmpc_pause_resource(kmp_pause_status_t level) {
4399 if (!__kmp_init_serial) {
4400 return 1; // Can't pause if runtime is not initialized
4401 }
4402 return __kmp_pause_resource(level);
4403}
4404
4405void __kmpc_error(ident_t *loc, int severity, const char *message) {
4406 if (!__kmp_init_serial)
4407 __kmp_serial_initialize();
4408
4409 KMP_ASSERT(severity == severity_warning || severity == severity_fatal);
4410
4411#if OMPT_SUPPORT
4412 if (ompt_enabled.enabled && ompt_enabled.ompt_callback_error) {
4413 ompt_callbacks.ompt_callback(ompt_callback_error)(
4414 (ompt_severity_t)severity, message, KMP_STRLEN(message),
4415 OMPT_GET_RETURN_ADDRESS(0));
4416 }
4417#endif // OMPT_SUPPORT
4418
4419 char *src_loc;
4420 if (loc && loc->psource) {
4421 kmp_str_loc_t str_loc = __kmp_str_loc_init(loc->psource, false);
4422 src_loc =
4423 __kmp_str_format("%s:%s:%s", str_loc.file, str_loc.line, str_loc.col);
4424 __kmp_str_loc_free(&str_loc);
4425 } else {
4426 src_loc = __kmp_str_format("unknown");
4427 }
4428
4429 if (severity == severity_warning)
4430 KMP_WARNING(UserDirectedWarning, src_loc, message);
4431 else
4432 KMP_FATAL(UserDirectedError, src_loc, message);
4433
4434 __kmp_str_free(&src_loc);
4435}
4436
4437// Mark begin of scope directive.
4438void __kmpc_scope(ident_t *loc, kmp_int32 gtid, void *reserved) {
4439// reserved is for extension of scope directive and not used.
4440#if OMPT_SUPPORT && OMPT_OPTIONAL
4441 if (ompt_enabled.enabled && ompt_enabled.ompt_callback_work) {
4442 kmp_team_t *team = __kmp_threads[gtid]->th.th_team;
4443 int tid = __kmp_tid_from_gtid(gtid);
4444 ompt_callbacks.ompt_callback(ompt_callback_work)(
4445 ompt_work_scope, ompt_scope_begin,
4446 &(team->t.ompt_team_info.parallel_data),
4447 &(team->t.t_implicit_task_taskdata[tid].ompt_task_info.task_data), 1,
4448 OMPT_GET_RETURN_ADDRESS(0));
4449 }
4450#endif // OMPT_SUPPORT && OMPT_OPTIONAL
4451}
4452
4453// Mark end of scope directive
4454void __kmpc_end_scope(ident_t *loc, kmp_int32 gtid, void *reserved) {
4455// reserved is for extension of scope directive and not used.
4456#if OMPT_SUPPORT && OMPT_OPTIONAL
4457 if (ompt_enabled.enabled && ompt_enabled.ompt_callback_work) {
4458 kmp_team_t *team = __kmp_threads[gtid]->th.th_team;
4459 int tid = __kmp_tid_from_gtid(gtid);
4460 ompt_callbacks.ompt_callback(ompt_callback_work)(
4461 ompt_work_scope, ompt_scope_end,
4462 &(team->t.ompt_team_info.parallel_data),
4463 &(team->t.t_implicit_task_taskdata[tid].ompt_task_info.task_data), 1,
4464 OMPT_GET_RETURN_ADDRESS(0));
4465 }
4466#endif // OMPT_SUPPORT && OMPT_OPTIONAL
4467}
4468
4469#ifdef KMP_USE_VERSION_SYMBOLS
4470// For GOMP compatibility there are two versions of each omp_* API.
4471// One is the plain C symbol and one is the Fortran symbol with an appended
4472// underscore. When we implement a specific ompc_* version of an omp_*
4473// function, we want the plain GOMP versioned symbol to alias the ompc_* version
4474// instead of the Fortran versions in kmp_ftn_entry.h
4475extern "C" {
4476// Have to undef these from omp.h so they aren't translated into
4477// their ompc counterparts in the KMP_VERSION_OMPC_SYMBOL macros below
4478#ifdef omp_set_affinity_format
4479#undef omp_set_affinity_format
4480#endif
4481#ifdef omp_get_affinity_format
4482#undef omp_get_affinity_format
4483#endif
4484#ifdef omp_display_affinity
4485#undef omp_display_affinity
4486#endif
4487#ifdef omp_capture_affinity
4488#undef omp_capture_affinity
4489#endif
4490KMP_VERSION_OMPC_SYMBOL(ompc_set_affinity_format, omp_set_affinity_format, 50,
4491 "OMP_5.0");
4492KMP_VERSION_OMPC_SYMBOL(ompc_get_affinity_format, omp_get_affinity_format, 50,
4493 "OMP_5.0");
4494KMP_VERSION_OMPC_SYMBOL(ompc_display_affinity, omp_display_affinity, 50,
4495 "OMP_5.0");
4496KMP_VERSION_OMPC_SYMBOL(ompc_capture_affinity, omp_capture_affinity, 50,
4497 "OMP_5.0");
4498} // extern "C"
4499#endif
@ KMP_IDENT_WORK_LOOP
Definition: kmp.h:214
@ KMP_IDENT_WORK_SECTIONS
Definition: kmp.h:216
@ KMP_IDENT_AUTOPAR
Definition: kmp.h:199
@ KMP_IDENT_WORK_DISTRIBUTE
Definition: kmp.h:218
kmp_int32 __kmpc_ok_to_fork(ident_t *loc)
void __kmpc_fork_teams(ident_t *loc, kmp_int32 argc, kmpc_micro microtask,...)
void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid, kmp_int32 num_threads)
void __kmpc_serialized_parallel(ident_t *loc, kmp_int32 global_tid)
void __kmpc_push_num_teams(ident_t *loc, kmp_int32 global_tid, kmp_int32 num_teams, kmp_int32 num_threads)
void __kmpc_fork_call(ident_t *loc, kmp_int32 argc, kmpc_micro microtask,...)
void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32 global_tid)
void(* kmpc_micro)(kmp_int32 *global_tid, kmp_int32 *bound_tid,...)
Definition: kmp.h:1601
void __kmpc_push_num_teams_51(ident_t *loc, kmp_int32 global_tid, kmp_int32 num_teams_lb, kmp_int32 num_teams_ub, kmp_int32 num_threads)
void __kmpc_begin(ident_t *loc, kmp_int32 flags)
void __kmpc_end(ident_t *loc)
#define KMP_COUNT_BLOCK(name)
Increments specified counter (name).
Definition: kmp_stats.h:908
stats_state_e
the states which a thread can be in
Definition: kmp_stats.h:63
void __kmpc_end_reduce(ident_t *loc, kmp_int32 global_tid, kmp_critical_name *lck)
void __kmpc_end_barrier_master(ident_t *loc, kmp_int32 global_tid)
kmp_int32 __kmpc_barrier_master_nowait(ident_t *loc, kmp_int32 global_tid)
void __kmpc_end_reduce_nowait(ident_t *loc, kmp_int32 global_tid, kmp_critical_name *lck)
kmp_int32 __kmpc_reduce(ident_t *loc, kmp_int32 global_tid, kmp_int32 num_vars, size_t reduce_size, void *reduce_data, void(*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name *lck)
void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid)
void __kmpc_flush(ident_t *loc)
kmp_int32 __kmpc_barrier_master(ident_t *loc, kmp_int32 global_tid)
kmp_int32 __kmpc_reduce_nowait(ident_t *loc, kmp_int32 global_tid, kmp_int32 num_vars, size_t reduce_size, void *reduce_data, void(*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name *lck)
void __kmpc_copyprivate(ident_t *loc, kmp_int32 gtid, size_t cpy_size, void *cpy_data, void(*cpy_func)(void *, void *), kmp_int32 didit)
void * __kmpc_copyprivate_light(ident_t *loc, kmp_int32 gtid, void *cpy_data)
kmp_int32 __kmpc_global_num_threads(ident_t *loc)
kmp_int32 __kmpc_global_thread_num(ident_t *loc)
kmp_int32 __kmpc_in_parallel(ident_t *loc)
kmp_int32 __kmpc_bound_thread_num(ident_t *loc)
kmp_int32 __kmpc_bound_num_threads(ident_t *loc)
void __kmpc_end_ordered(ident_t *loc, kmp_int32 gtid)
void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid, kmp_critical_name *crit)
void __kmpc_for_static_fini(ident_t *loc, kmp_int32 global_tid)
void __kmpc_end_masked(ident_t *loc, kmp_int32 global_tid)
kmp_int32 __kmpc_master(ident_t *loc, kmp_int32 global_tid)
void __kmpc_critical_with_hint(ident_t *loc, kmp_int32 global_tid, kmp_critical_name *crit, uint32_t hint)
kmp_int32 __kmpc_single(ident_t *loc, kmp_int32 global_tid)
void __kmpc_doacross_init(ident_t *loc, int gtid, int num_dims, const struct kmp_dim *dims)
void __kmpc_end_master(ident_t *loc, kmp_int32 global_tid)
void __kmpc_end_single(ident_t *loc, kmp_int32 global_tid)
void __kmpc_ordered(ident_t *loc, kmp_int32 gtid)
kmp_int32 __kmpc_masked(ident_t *loc, kmp_int32 global_tid, kmp_int32 filter)
void __kmpc_critical(ident_t *loc, kmp_int32 global_tid, kmp_critical_name *crit)
Definition: kmp.h:234
char const * psource
Definition: kmp.h:244
kmp_int32 flags
Definition: kmp.h:236