LLVM OpenMP* Runtime Library
Loading...
Searching...
No Matches
kmp_ftn_entry.h
1/*
2 * kmp_ftn_entry.h -- Fortran entry 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#ifndef FTN_STDCALL
14#error The support file kmp_ftn_entry.h should not be compiled by itself.
15#endif
16
17#ifdef KMP_STUB
18#include "kmp_stub.h"
19#endif
20
21#include "kmp_i18n.h"
22
23// For affinity format functions
24#include "kmp_io.h"
25#include "kmp_str.h"
26
27#if OMPT_SUPPORT
28#include "ompt-specific.h"
29#endif
30
31#ifdef __cplusplus
32extern "C" {
33#endif // __cplusplus
34
35/* For compatibility with the Gnu/MS Open MP codegen, omp_set_num_threads(),
36 * omp_set_nested(), and omp_set_dynamic() [in lowercase on MS, and w/o
37 * a trailing underscore on Linux* OS] take call by value integer arguments.
38 * + omp_set_max_active_levels()
39 * + omp_set_schedule()
40 *
41 * For backward compatibility with 9.1 and previous Intel compiler, these
42 * entry points take call by reference integer arguments. */
43#ifdef KMP_GOMP_COMPAT
44#if (KMP_FTN_ENTRIES == KMP_FTN_PLAIN) || (KMP_FTN_ENTRIES == KMP_FTN_UPPER)
45#define PASS_ARGS_BY_VALUE 1
46#endif
47#endif
48#if KMP_OS_WINDOWS
49#if (KMP_FTN_ENTRIES == KMP_FTN_PLAIN) || (KMP_FTN_ENTRIES == KMP_FTN_APPEND)
50#define PASS_ARGS_BY_VALUE 1
51#endif
52#endif
53
54// This macro helps to reduce code duplication.
55#ifdef PASS_ARGS_BY_VALUE
56#define KMP_DEREF
57#define KMP_FTN_PASS(x) (x)
58#else
59#define KMP_DEREF *
60#define KMP_FTN_PASS(x) (&(x))
61#endif
62
63static int __kmp_ftn_int64_to_int(kmp_int64 value) {
64 if (value > (kmp_int64)INT_MAX)
65 return INT_MAX;
66 if (value < (kmp_int64)INT_MIN)
67 return INT_MIN;
68 return (int)value;
69}
70
71// For API with specific C vs. Fortran interfaces (ompc_* exists in
72// kmp_csupport.cpp), only create GOMP versioned symbols of the API for the
73// APPEND Fortran entries in this file. The GOMP versioned symbols of the C API
74// will take place where the ompc_* functions are defined.
75#if KMP_FTN_ENTRIES == KMP_FTN_APPEND
76#define KMP_EXPAND_NAME_IF_APPEND(name) KMP_EXPAND_NAME(name)
77#else
78#define KMP_EXPAND_NAME_IF_APPEND(name) name
79#endif
80
81void FTN_STDCALL FTN_SET_STACKSIZE(int KMP_DEREF arg) {
82#ifdef KMP_STUB
83 __kmps_set_stacksize(KMP_DEREF arg);
84#else
85 // __kmp_aux_set_stacksize initializes the library if needed
86 __kmp_aux_set_stacksize((size_t)KMP_DEREF arg);
87#endif
88}
89
90void FTN_STDCALL FTN_SET_STACKSIZE_8(kmp_int64 KMP_DEREF arg) {
91 int arg32 = __kmp_ftn_int64_to_int(KMP_DEREF arg);
92 FTN_SET_STACKSIZE(KMP_FTN_PASS(arg32));
93}
94
95void FTN_STDCALL FTN_SET_STACKSIZE_S(size_t KMP_DEREF arg) {
96#ifdef KMP_STUB
97 __kmps_set_stacksize(KMP_DEREF arg);
98#else
99 // __kmp_aux_set_stacksize initializes the library if needed
100 __kmp_aux_set_stacksize(KMP_DEREF arg);
101#endif
102}
103
104int FTN_STDCALL FTN_GET_STACKSIZE(void) {
105#ifdef KMP_STUB
106 return (int)__kmps_get_stacksize();
107#else
108 if (!__kmp_init_serial) {
109 __kmp_serial_initialize();
110 }
111 return (int)__kmp_stksize;
112#endif
113}
114
115size_t FTN_STDCALL FTN_GET_STACKSIZE_S(void) {
116#ifdef KMP_STUB
117 return __kmps_get_stacksize();
118#else
119 if (!__kmp_init_serial) {
120 __kmp_serial_initialize();
121 }
122 return __kmp_stksize;
123#endif
124}
125
126void FTN_STDCALL FTN_SET_BLOCKTIME(int KMP_DEREF arg) {
127#ifdef KMP_STUB
128 __kmps_set_blocktime(KMP_DEREF arg);
129#else
130 int gtid, tid, bt = (KMP_DEREF arg);
131 kmp_info_t *thread;
132
133 gtid = __kmp_entry_gtid();
134 tid = __kmp_tid_from_gtid(gtid);
135 thread = __kmp_thread_from_gtid(gtid);
136
137 __kmp_aux_convert_blocktime(&bt);
138 __kmp_aux_set_blocktime(bt, thread, tid);
139#endif
140}
141
142void FTN_STDCALL FTN_SET_BLOCKTIME_8(kmp_int64 KMP_DEREF arg) {
143 int arg32 = __kmp_ftn_int64_to_int(KMP_DEREF arg);
144 FTN_SET_BLOCKTIME(KMP_FTN_PASS(arg32));
145}
146
147// Gets blocktime in units used for KMP_BLOCKTIME, ms otherwise
148int FTN_STDCALL FTN_GET_BLOCKTIME(void) {
149#ifdef KMP_STUB
150 return __kmps_get_blocktime();
151#else
152 int gtid, tid;
153 kmp_team_p *team;
154
155 gtid = __kmp_entry_gtid();
156 tid = __kmp_tid_from_gtid(gtid);
157 team = __kmp_threads[gtid]->th.th_team;
158
159 /* These must match the settings used in __kmp_wait_sleep() */
160 if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME) {
161 KF_TRACE(10, ("kmp_get_blocktime: T#%d(%d:%d), blocktime=%d%cs\n", gtid,
162 team->t.t_id, tid, KMP_MAX_BLOCKTIME, __kmp_blocktime_units));
163 return KMP_MAX_BLOCKTIME;
164 }
165#ifdef KMP_ADJUST_BLOCKTIME
166 else if (__kmp_zero_bt && !get__bt_set(team, tid)) {
167 KF_TRACE(10, ("kmp_get_blocktime: T#%d(%d:%d), blocktime=%d%cs\n", gtid,
168 team->t.t_id, tid, 0, __kmp_blocktime_units));
169 return 0;
170 }
171#endif /* KMP_ADJUST_BLOCKTIME */
172 else {
173 int bt = get__blocktime(team, tid);
174 if (__kmp_blocktime_units == 'm')
175 bt = bt / 1000;
176 KF_TRACE(10, ("kmp_get_blocktime: T#%d(%d:%d), blocktime=%d%cs\n", gtid,
177 team->t.t_id, tid, bt, __kmp_blocktime_units));
178 return bt;
179 }
180#endif
181}
182
183void FTN_STDCALL FTN_SET_LIBRARY_SERIAL(void) {
184#ifdef KMP_STUB
185 __kmps_set_library(library_serial);
186#else
187 // __kmp_user_set_library initializes the library if needed
188 __kmp_user_set_library(library_serial);
189#endif
190}
191
192void FTN_STDCALL FTN_SET_LIBRARY_TURNAROUND(void) {
193#ifdef KMP_STUB
194 __kmps_set_library(library_turnaround);
195#else
196 // __kmp_user_set_library initializes the library if needed
197 __kmp_user_set_library(library_turnaround);
198#endif
199}
200
201void FTN_STDCALL FTN_SET_LIBRARY_THROUGHPUT(void) {
202#ifdef KMP_STUB
203 __kmps_set_library(library_throughput);
204#else
205 // __kmp_user_set_library initializes the library if needed
206 __kmp_user_set_library(library_throughput);
207#endif
208}
209
210void FTN_STDCALL FTN_SET_LIBRARY(int KMP_DEREF arg) {
211#ifdef KMP_STUB
212 __kmps_set_library(KMP_DEREF arg);
213#else
214 enum library_type lib;
215 lib = (enum library_type)KMP_DEREF arg;
216 // __kmp_user_set_library initializes the library if needed
217 __kmp_user_set_library(lib);
218#endif
219}
220
221void FTN_STDCALL FTN_SET_LIBRARY_8(kmp_int64 KMP_DEREF arg) {
222 int arg32 = __kmp_ftn_int64_to_int(KMP_DEREF arg);
223 FTN_SET_LIBRARY(KMP_FTN_PASS(arg32));
224}
225
226int FTN_STDCALL FTN_GET_LIBRARY(void) {
227#ifdef KMP_STUB
228 return __kmps_get_library();
229#else
230 if (!__kmp_init_serial) {
231 __kmp_serial_initialize();
232 }
233 return ((int)__kmp_library);
234#endif
235}
236
237void FTN_STDCALL FTN_SET_DISP_NUM_BUFFERS(int KMP_DEREF arg) {
238#ifdef KMP_STUB
239 ; // empty routine
240#else
241 // ignore after initialization because some teams have already
242 // allocated dispatch buffers
243 int num_buffers = KMP_DEREF arg;
244 if (__kmp_init_serial == FALSE && num_buffers >= KMP_MIN_DISP_NUM_BUFF &&
245 num_buffers <= KMP_MAX_DISP_NUM_BUFF) {
246 __kmp_dispatch_num_buffers = num_buffers;
247 }
248#endif
249}
250
251void FTN_STDCALL FTN_SET_DISP_NUM_BUFFERS_8(kmp_int64 KMP_DEREF arg) {
252 int arg32 = __kmp_ftn_int64_to_int(KMP_DEREF arg);
253 FTN_SET_DISP_NUM_BUFFERS(KMP_FTN_PASS(arg32));
254}
255
256int FTN_STDCALL FTN_SET_AFFINITY(void **mask) {
257#if defined(KMP_STUB) || !KMP_AFFINITY_SUPPORTED
258 return -1;
259#else
260 if (!TCR_4(__kmp_init_middle)) {
261 __kmp_middle_initialize();
262 }
263 __kmp_assign_root_init_mask();
264 return __kmp_aux_set_affinity(mask);
265#endif
266}
267
268int FTN_STDCALL FTN_GET_AFFINITY(void **mask) {
269#if defined(KMP_STUB) || !KMP_AFFINITY_SUPPORTED
270 return -1;
271#else
272 if (!TCR_4(__kmp_init_middle)) {
273 __kmp_middle_initialize();
274 }
275 __kmp_assign_root_init_mask();
276 int gtid = __kmp_get_gtid();
277 if (__kmp_threads[gtid]->th.th_team->t.t_level == 0 &&
278 __kmp_affinity.flags.reset) {
279 __kmp_reset_root_init_mask(gtid);
280 }
281 return __kmp_aux_get_affinity(mask);
282#endif
283}
284
285int FTN_STDCALL FTN_GET_AFFINITY_MAX_PROC(void) {
286#if defined(KMP_STUB) || !KMP_AFFINITY_SUPPORTED
287 return 0;
288#else
289 // We really only NEED serial initialization here.
290 if (!TCR_4(__kmp_init_middle)) {
291 __kmp_middle_initialize();
292 }
293 __kmp_assign_root_init_mask();
294 return __kmp_aux_get_affinity_max_proc();
295#endif
296}
297
298void FTN_STDCALL FTN_CREATE_AFFINITY_MASK(void **mask) {
299#if defined(KMP_STUB) || !KMP_AFFINITY_SUPPORTED
300 *mask = NULL;
301#else
302 // We really only NEED serial initialization here.
303 kmp_affin_mask_t *mask_internals;
304 if (!TCR_4(__kmp_init_middle)) {
305 __kmp_middle_initialize();
306 }
307 __kmp_assign_root_init_mask();
308 mask_internals = __kmp_affinity_dispatch->allocate_mask();
309 KMP_CPU_ZERO(mask_internals);
310 *mask = mask_internals;
311#endif
312}
313
314void FTN_STDCALL FTN_DESTROY_AFFINITY_MASK(void **mask) {
315#if defined(KMP_STUB) || !KMP_AFFINITY_SUPPORTED
316// Nothing
317#else
318 // We really only NEED serial initialization here.
319 kmp_affin_mask_t *mask_internals;
320 if (!TCR_4(__kmp_init_middle)) {
321 __kmp_middle_initialize();
322 }
323 __kmp_assign_root_init_mask();
324 if (__kmp_env_consistency_check) {
325 if (*mask == NULL) {
326 KMP_FATAL(AffinityInvalidMask, "kmp_destroy_affinity_mask");
327 }
328 }
329 mask_internals = (kmp_affin_mask_t *)(*mask);
330 __kmp_affinity_dispatch->deallocate_mask(mask_internals);
331 *mask = NULL;
332#endif
333}
334
335int FTN_STDCALL FTN_SET_AFFINITY_MASK_PROC(int KMP_DEREF proc, void **mask) {
336#if defined(KMP_STUB) || !KMP_AFFINITY_SUPPORTED
337 return -1;
338#else
339 if (!TCR_4(__kmp_init_middle)) {
340 __kmp_middle_initialize();
341 }
342 __kmp_assign_root_init_mask();
343 return __kmp_aux_set_affinity_mask_proc(KMP_DEREF proc, mask);
344#endif
345}
346
347int FTN_STDCALL FTN_SET_AFFINITY_MASK_PROC_8(kmp_int64 KMP_DEREF proc,
348 void **mask) {
349 int proc32 = __kmp_ftn_int64_to_int(KMP_DEREF proc);
350 return FTN_SET_AFFINITY_MASK_PROC(KMP_FTN_PASS(proc32), mask);
351}
352
353int FTN_STDCALL FTN_UNSET_AFFINITY_MASK_PROC(int KMP_DEREF proc, void **mask) {
354#if defined(KMP_STUB) || !KMP_AFFINITY_SUPPORTED
355 return -1;
356#else
357 if (!TCR_4(__kmp_init_middle)) {
358 __kmp_middle_initialize();
359 }
360 __kmp_assign_root_init_mask();
361 return __kmp_aux_unset_affinity_mask_proc(KMP_DEREF proc, mask);
362#endif
363}
364
365int FTN_STDCALL FTN_UNSET_AFFINITY_MASK_PROC_8(kmp_int64 KMP_DEREF proc,
366 void **mask) {
367 int proc32 = __kmp_ftn_int64_to_int(KMP_DEREF proc);
368 return FTN_UNSET_AFFINITY_MASK_PROC(KMP_FTN_PASS(proc32), mask);
369}
370
371int FTN_STDCALL FTN_GET_AFFINITY_MASK_PROC(int KMP_DEREF proc, void **mask) {
372#if defined(KMP_STUB) || !KMP_AFFINITY_SUPPORTED
373 return -1;
374#else
375 if (!TCR_4(__kmp_init_middle)) {
376 __kmp_middle_initialize();
377 }
378 __kmp_assign_root_init_mask();
379 return __kmp_aux_get_affinity_mask_proc(KMP_DEREF proc, mask);
380#endif
381}
382
383int FTN_STDCALL FTN_GET_AFFINITY_MASK_PROC_8(kmp_int64 KMP_DEREF proc,
384 void **mask) {
385 int proc32 = __kmp_ftn_int64_to_int(KMP_DEREF proc);
386 return FTN_GET_AFFINITY_MASK_PROC(KMP_FTN_PASS(proc32), mask);
387}
388
389/* ------------------------------------------------------------------------ */
390
391/* sets the requested number of threads for the next parallel region */
392void FTN_STDCALL KMP_EXPAND_NAME(FTN_SET_NUM_THREADS)(int KMP_DEREF arg) {
393#ifdef KMP_STUB
394// Nothing.
395#else
396 __kmp_set_num_threads(KMP_DEREF arg, __kmp_entry_gtid());
397#endif
398}
399
400/* Same as omp_set_num_threads, but accepts a 64-bit integer argument. */
401void FTN_STDCALL FTN_SET_NUM_THREADS_8(kmp_int64 KMP_DEREF arg) {
402 int arg32 = __kmp_ftn_int64_to_int(KMP_DEREF arg);
403 KMP_EXPAND_NAME(FTN_SET_NUM_THREADS)(KMP_FTN_PASS(arg32));
404}
405
406/* returns the number of threads in current team */
407int FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_NUM_THREADS)(void) {
408#ifdef KMP_STUB
409 return 1;
410#else
411 // __kmpc_bound_num_threads initializes the library if needed
412 return __kmpc_bound_num_threads(NULL);
413#endif
414}
415
416int FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_MAX_THREADS)(void) {
417#ifdef KMP_STUB
418 return 1;
419#else
420 int gtid;
421 kmp_info_t *thread;
422 if (!TCR_4(__kmp_init_middle)) {
423 __kmp_middle_initialize();
424 }
425 gtid = __kmp_entry_gtid();
426 thread = __kmp_threads[gtid];
427#if KMP_AFFINITY_SUPPORTED
428 if (thread->th.th_team->t.t_level == 0 && !__kmp_affinity.flags.reset) {
429 __kmp_assign_root_init_mask();
430 }
431#endif
432 // return thread -> th.th_team -> t.t_current_task[
433 // thread->th.th_info.ds.ds_tid ] -> icvs.nproc;
434 return thread->th.th_current_task->td_icvs.nproc;
435#endif
436}
437
438int FTN_STDCALL FTN_CONTROL_TOOL(int command, int modifier, void *arg) {
439#if defined(KMP_STUB) || !OMPT_SUPPORT
440 return -2;
441#else
442 OMPT_STORE_RETURN_ADDRESS(__kmp_entry_gtid());
443 if (!TCR_4(__kmp_init_middle)) {
444 __kmp_middle_initialize();
445 }
446 kmp_info_t *this_thr = __kmp_threads[__kmp_entry_gtid()];
447 ompt_task_info_t *parent_task_info = OMPT_CUR_TASK_INFO(this_thr);
448 parent_task_info->frame.enter_frame.ptr = OMPT_GET_FRAME_ADDRESS(0);
449 int ret = __kmp_control_tool(command, modifier, arg);
450 parent_task_info->frame.enter_frame.ptr = 0;
451 return ret;
452#endif
453}
454
455/* OpenMP 5.0 Memory Management support */
456omp_allocator_handle_t FTN_STDCALL
457FTN_INIT_ALLOCATOR(omp_memspace_handle_t KMP_DEREF m, int KMP_DEREF ntraits,
458 omp_alloctrait_t tr[]) {
459#ifdef KMP_STUB
460 return NULL;
461#else
462 return __kmpc_init_allocator(__kmp_entry_gtid(), KMP_DEREF m,
463 KMP_DEREF ntraits, tr);
464#endif
465}
466
467void FTN_STDCALL FTN_DESTROY_ALLOCATOR(omp_allocator_handle_t al) {
468#ifndef KMP_STUB
469 __kmpc_destroy_allocator(__kmp_entry_gtid(), al);
470#endif
471}
472void FTN_STDCALL FTN_SET_DEFAULT_ALLOCATOR(omp_allocator_handle_t al) {
473#ifndef KMP_STUB
474 __kmpc_set_default_allocator(__kmp_entry_gtid(), al);
475#endif
476}
477omp_allocator_handle_t FTN_STDCALL FTN_GET_DEFAULT_ALLOCATOR(void) {
478#ifdef KMP_STUB
479 return NULL;
480#else
481 return __kmpc_get_default_allocator(__kmp_entry_gtid());
482#endif
483}
484
485/* OpenMP 6.0 (TR11) Memory Management support */
486omp_memspace_handle_t FTN_STDCALL
487FTN_GET_DEVICES_MEMSPACE(int KMP_DEREF ndevs, const int *devs,
488 omp_memspace_handle_t KMP_DEREF memspace) {
489#ifdef KMP_STUB
490 return NULL;
491#else
492 return __kmp_get_devices_memspace(KMP_DEREF ndevs, devs, KMP_DEREF memspace,
493 0 /* host */);
494#endif
495}
496
497omp_memspace_handle_t FTN_STDCALL FTN_GET_DEVICE_MEMSPACE(
498 int KMP_DEREF dev, omp_memspace_handle_t KMP_DEREF memspace) {
499#ifdef KMP_STUB
500 return NULL;
501#else
502 int dev_num = KMP_DEREF dev;
503 return __kmp_get_devices_memspace(1, &dev_num, KMP_DEREF memspace, 0);
504#endif
505}
506
507omp_memspace_handle_t FTN_STDCALL
508FTN_GET_DEVICES_AND_HOST_MEMSPACE(int KMP_DEREF ndevs, const int *devs,
509 omp_memspace_handle_t KMP_DEREF memspace) {
510#ifdef KMP_STUB
511 return NULL;
512#else
513 return __kmp_get_devices_memspace(KMP_DEREF ndevs, devs, KMP_DEREF memspace,
514 1);
515#endif
516}
517
518omp_memspace_handle_t FTN_STDCALL FTN_GET_DEVICE_AND_HOST_MEMSPACE(
519 int KMP_DEREF dev, omp_memspace_handle_t KMP_DEREF memspace) {
520#ifdef KMP_STUB
521 return NULL;
522#else
523 int dev_num = KMP_DEREF dev;
524 return __kmp_get_devices_memspace(1, &dev_num, KMP_DEREF memspace, 1);
525#endif
526}
527
528omp_memspace_handle_t FTN_STDCALL
529FTN_GET_DEVICES_ALL_MEMSPACE(omp_memspace_handle_t KMP_DEREF memspace) {
530#ifdef KMP_STUB
531 return NULL;
532#else
533 return __kmp_get_devices_memspace(0, NULL, KMP_DEREF memspace, 1);
534#endif
535}
536
537omp_allocator_handle_t FTN_STDCALL
538FTN_GET_DEVICES_ALLOCATOR(int KMP_DEREF ndevs, const int *devs,
539 omp_allocator_handle_t KMP_DEREF memspace) {
540#ifdef KMP_STUB
541 return NULL;
542#else
543 return __kmp_get_devices_allocator(KMP_DEREF ndevs, devs, KMP_DEREF memspace,
544 0 /* host */);
545#endif
546}
547
548omp_allocator_handle_t FTN_STDCALL FTN_GET_DEVICE_ALLOCATOR(
549 int KMP_DEREF dev, omp_allocator_handle_t KMP_DEREF memspace) {
550#ifdef KMP_STUB
551 return NULL;
552#else
553 int dev_num = KMP_DEREF dev;
554 return __kmp_get_devices_allocator(1, &dev_num, KMP_DEREF memspace, 0);
555#endif
556}
557
558omp_allocator_handle_t FTN_STDCALL
559FTN_GET_DEVICES_AND_HOST_ALLOCATOR(int KMP_DEREF ndevs, const int *devs,
560 omp_allocator_handle_t KMP_DEREF memspace) {
561#ifdef KMP_STUB
562 return NULL;
563#else
564 return __kmp_get_devices_allocator(KMP_DEREF ndevs, devs, KMP_DEREF memspace,
565 1);
566#endif
567}
568
569omp_allocator_handle_t FTN_STDCALL FTN_GET_DEVICE_AND_HOST_ALLOCATOR(
570 int KMP_DEREF dev, omp_allocator_handle_t KMP_DEREF memspace) {
571#ifdef KMP_STUB
572 return NULL;
573#else
574 int dev_num = KMP_DEREF dev;
575 return __kmp_get_devices_allocator(1, &dev_num, KMP_DEREF memspace, 1);
576#endif
577}
578
579omp_allocator_handle_t FTN_STDCALL
580FTN_GET_DEVICES_ALL_ALLOCATOR(omp_allocator_handle_t KMP_DEREF memspace) {
581#ifdef KMP_STUB
582 return NULL;
583#else
584 return __kmp_get_devices_allocator(0, NULL, KMP_DEREF memspace, 1);
585#endif
586}
587
588int FTN_STDCALL
589FTN_GET_MEMSPACE_NUM_RESOURCES(omp_memspace_handle_t KMP_DEREF memspace) {
590#ifdef KMP_STUB
591 return 0;
592#else
593 return __kmp_get_memspace_num_resources(KMP_DEREF memspace);
594#endif
595}
596
597omp_memspace_handle_t FTN_STDCALL
598FTN_GET_SUBMEMSPACE(omp_memspace_handle_t KMP_DEREF memspace,
599 int KMP_DEREF num_resources, int *resources) {
600#ifdef KMP_STUB
601 return NULL;
602#else
603 return __kmp_get_submemspace(KMP_DEREF memspace, KMP_DEREF num_resources,
604 resources);
605#endif
606}
607
608// Fortran requires a different calling convention for the following
609// dyn_groupprivate access entry points. Therefore, we need to make sure that
610// we only generate a Fortran mangled version here. Note: the unmangled,
611// C-style symbol does not provide the right ABI and so non-Linux OSes
612// might require some effort here.
613#if KMP_FTN_ENTRIES != KMP_FTN_PLAIN
614void *FTN_STDCALL FTN_GET_DYN_GPRIVATE_PTR(size_t *offset,
615 omp_access_t *access_group) {
616#if KMP_OS_DARWIN || KMP_OS_WASI || defined(KMP_STUB)
617 return NULL;
618#else
619 size_t off = offset ? *offset : 0;
620 omp_access_t ag = access_group ? *access_group : omp_access_cgroup;
621 return omp_get_dyn_gprivate_ptr(off, ag);
622#endif
623}
624
625void *FTN_STDCALL FTN_GET_DYN_GPRIVATE_NOFB_PTR(size_t *offset,
626 omp_access_t *access_group) {
627#if KMP_OS_DARWIN || KMP_OS_WASI || defined(KMP_STUB)
628 return NULL;
629#else
630 size_t off = offset ? *offset : 0;
631 omp_access_t ag = access_group ? *access_group : omp_access_cgroup;
632 return omp_get_dyn_gprivate_nofb_ptr(off, ag);
633#endif
634}
635
636size_t FTN_STDCALL FTN_GET_DYN_GPRIVATE_SIZE(omp_access_t *access_group) {
637#if KMP_OS_DARWIN || KMP_OS_WASI || defined(KMP_STUB)
638 return 0;
639#else
640 omp_access_t ag = access_group ? *access_group : omp_access_cgroup;
641 return omp_get_dyn_gprivate_size(ag);
642#endif
643}
644
645omp_memspace_handle_t FTN_STDCALL
646FTN_GET_DYN_GPRIVATE_MEMSPACE(omp_access_t *access_group) {
647#if KMP_OS_DARWIN || KMP_OS_WASI || defined(KMP_STUB)
648 return omp_null_mem_space;
649#else
650 omp_access_t ag = access_group ? *access_group : omp_access_cgroup;
651 return omp_get_dyn_gprivate_memspace(ag);
652#endif
653}
654
655size_t FTN_STDCALL FTN_GET_GPRIVATE_LIMIT(int *device_num,
656 omp_access_t *access_group) {
657#if KMP_OS_DARWIN || KMP_OS_WASI || defined(KMP_STUB)
658 return 0;
659#else
660 size_t (*fptr)(int, omp_access_t);
661 if ((*(void **)(&fptr) = KMP_DLSYM_NEXT("omp_get_gprivate_limit"))) {
662 int dev = device_num ? *device_num : 0;
663 omp_access_t ag = access_group ? *access_group : omp_access_cgroup;
664 return (*fptr)(dev, ag);
665 }
666 return 0;
667#endif
668}
669#endif // KMP_FTN_ENTRIES != KMP_FTN_PLAIN
670
671/* OpenMP 5.0 affinity format support */
672#ifndef KMP_STUB
673static void __kmp_fortran_strncpy_truncate(char *buffer, size_t buf_size,
674 char const *csrc, size_t csrc_size) {
675 size_t capped_src_size = csrc_size;
676 if (csrc_size >= buf_size) {
677 capped_src_size = buf_size - 1;
678 }
679 KMP_STRNCPY_S(buffer, buf_size, csrc, capped_src_size);
680 if (csrc_size >= buf_size) {
681 KMP_DEBUG_ASSERT(buffer[buf_size - 1] == '\0');
682 buffer[buf_size - 1] = csrc[buf_size - 1];
683 } else {
684 for (size_t i = csrc_size; i < buf_size; ++i)
685 buffer[i] = ' ';
686 }
687}
688
689// Convert a Fortran string to a C string by adding null byte
690class ConvertedString {
691 char *buf;
692
693public:
694 ConvertedString(char const *fortran_str, size_t size) {
695 buf = (char *)KMP_INTERNAL_MALLOC(size + 1);
696 KMP_STRNCPY_S(buf, size + 1, fortran_str, size);
697 buf[size] = '\0';
698 }
699 ~ConvertedString() { KMP_INTERNAL_FREE(buf); }
700 const char *get() const { return buf; }
701};
702#endif // KMP_STUB
703
704/*
705 * Set the value of the affinity-format-var ICV on the current device to the
706 * format specified in the argument.
707 */
708void FTN_STDCALL KMP_EXPAND_NAME_IF_APPEND(FTN_SET_AFFINITY_FORMAT)(
709 char const *format, size_t size) {
710#ifdef KMP_STUB
711 return;
712#else
713 if (!__kmp_init_serial) {
714 __kmp_serial_initialize();
715 }
716 ConvertedString cformat(format, size);
717 // Since the __kmp_affinity_format variable is a C string, do not
718 // use the fortran strncpy function
719 __kmp_strncpy_truncate(__kmp_affinity_format, KMP_AFFINITY_FORMAT_SIZE,
720 cformat.get(), KMP_STRLEN(cformat.get()));
721#endif
722}
723
724/*
725 * Returns the number of characters required to hold the entire affinity format
726 * specification (not including null byte character) and writes the value of the
727 * affinity-format-var ICV on the current device to buffer. If the return value
728 * is larger than size, the affinity format specification is truncated.
729 */
730size_t FTN_STDCALL KMP_EXPAND_NAME_IF_APPEND(FTN_GET_AFFINITY_FORMAT)(
731 char *buffer, size_t size) {
732#ifdef KMP_STUB
733 return 0;
734#else
735 size_t format_size;
736 if (!__kmp_init_serial) {
737 __kmp_serial_initialize();
738 }
739 format_size = KMP_STRLEN(__kmp_affinity_format);
740 if (buffer && size) {
741 __kmp_fortran_strncpy_truncate(buffer, size, __kmp_affinity_format,
742 format_size);
743 }
744 return format_size;
745#endif
746}
747
748/*
749 * Prints the thread affinity information of the current thread in the format
750 * specified by the format argument. If the format is NULL or a zero-length
751 * string, the value of the affinity-format-var ICV is used.
752 */
753void FTN_STDCALL KMP_EXPAND_NAME_IF_APPEND(FTN_DISPLAY_AFFINITY)(
754 char const *format, size_t size) {
755#ifdef KMP_STUB
756 return;
757#else
758 int gtid;
759 if (!TCR_4(__kmp_init_middle)) {
760 __kmp_middle_initialize();
761 }
762 __kmp_assign_root_init_mask();
763 gtid = __kmp_get_gtid();
764#if KMP_AFFINITY_SUPPORTED
765 if (__kmp_threads[gtid]->th.th_team->t.t_level == 0 &&
766 __kmp_affinity.flags.reset) {
767 __kmp_reset_root_init_mask(gtid);
768 }
769#endif
770 ConvertedString cformat(format, size);
771 __kmp_aux_display_affinity(gtid, cformat.get());
772#endif
773}
774
775/*
776 * Returns the number of characters required to hold the entire affinity format
777 * specification (not including null byte) and prints the thread affinity
778 * information of the current thread into the character string buffer with the
779 * size of size in the format specified by the format argument. If the format is
780 * NULL or a zero-length string, the value of the affinity-format-var ICV is
781 * used. The buffer must be allocated prior to calling the routine. If the
782 * return value is larger than size, the affinity format specification is
783 * truncated.
784 */
785size_t FTN_STDCALL KMP_EXPAND_NAME_IF_APPEND(FTN_CAPTURE_AFFINITY)(
786 char *buffer, char const *format, size_t buf_size, size_t for_size) {
787#if defined(KMP_STUB)
788 return 0;
789#else
790 int gtid;
791 size_t num_required;
792 kmp_str_buf_t capture_buf;
793 if (!TCR_4(__kmp_init_middle)) {
794 __kmp_middle_initialize();
795 }
796 __kmp_assign_root_init_mask();
797 gtid = __kmp_get_gtid();
798#if KMP_AFFINITY_SUPPORTED
799 if (__kmp_threads[gtid]->th.th_team->t.t_level == 0 &&
800 __kmp_affinity.flags.reset) {
801 __kmp_reset_root_init_mask(gtid);
802 }
803#endif
804 __kmp_str_buf_init(&capture_buf);
805 ConvertedString cformat(format, for_size);
806 num_required = __kmp_aux_capture_affinity(gtid, cformat.get(), &capture_buf);
807 if (buffer && buf_size) {
808 __kmp_fortran_strncpy_truncate(buffer, buf_size, capture_buf.str,
809 capture_buf.used);
810 }
811 __kmp_str_buf_free(&capture_buf);
812 return num_required;
813#endif
814}
815
816int FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_THREAD_NUM)(void) {
817#ifdef KMP_STUB
818 return 0;
819#else
820 int gtid;
821
822#if KMP_OS_DARWIN || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || \
823 KMP_OS_OPENBSD || KMP_OS_HAIKU || KMP_OS_HURD || KMP_OS_SOLARIS || \
824 KMP_OS_AIX
825 gtid = __kmp_entry_gtid();
826#elif KMP_OS_WINDOWS
827 if (!__kmp_init_parallel ||
828 (gtid = (int)((kmp_intptr_t)TlsGetValue(__kmp_gtid_threadprivate_key))) ==
829 0) {
830 // Either library isn't initialized or thread is not registered
831 // 0 is the correct TID in this case
832 return 0;
833 }
834 --gtid; // We keep (gtid+1) in TLS
835#elif KMP_OS_LINUX || KMP_OS_WASI
836#ifdef KMP_TDATA_GTID
837 if (__kmp_gtid_mode >= 3) {
838 if ((gtid = __kmp_gtid) == KMP_GTID_DNE) {
839 return 0;
840 }
841 } else {
842#endif
843 if (!__kmp_init_parallel ||
844 (gtid = (int)((kmp_intptr_t)(
845 pthread_getspecific(__kmp_gtid_threadprivate_key)))) == 0) {
846 return 0;
847 }
848 --gtid;
849#ifdef KMP_TDATA_GTID
850 }
851#endif
852#else
853#error Unknown or unsupported OS
854#endif
855
856 return __kmp_tid_from_gtid(gtid);
857#endif
858}
859
860int FTN_STDCALL FTN_GET_NUM_KNOWN_THREADS(void) {
861#ifdef KMP_STUB
862 return 1;
863#else
864 if (!__kmp_init_serial) {
865 __kmp_serial_initialize();
866 }
867 /* NOTE: this is not syncronized, so it can change at any moment */
868 /* NOTE: this number also includes threads preallocated in hot-teams */
869 return TCR_4(__kmp_nth);
870#endif
871}
872
873int FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_NUM_PROCS)(void) {
874#ifdef KMP_STUB
875 return 1;
876#else
877 if (!TCR_4(__kmp_init_middle)) {
878 __kmp_middle_initialize();
879 }
880#if KMP_AFFINITY_SUPPORTED
881 if (!__kmp_affinity.flags.reset) {
882 // only bind root here if its affinity reset is not requested
883 int gtid = __kmp_entry_gtid();
884 kmp_info_t *thread = __kmp_threads[gtid];
885 if (thread->th.th_team->t.t_level == 0) {
886 __kmp_assign_root_init_mask();
887 }
888 }
889#endif
890 return __kmp_avail_proc;
891#endif
892}
893
894void FTN_STDCALL KMP_EXPAND_NAME(FTN_SET_NESTED)(int KMP_DEREF flag) {
895#ifdef KMP_STUB
896 __kmps_set_nested(KMP_DEREF flag);
897#else
898 kmp_info_t *thread;
899 /* For the thread-private internal controls implementation */
900 thread = __kmp_entry_thread();
901 KMP_INFORM(APIDeprecated, "omp_set_nested", "omp_set_max_active_levels");
902 __kmp_save_internal_controls(thread);
903 // Somewhat arbitrarily decide where to get a value for max_active_levels
904 int max_active_levels = get__max_active_levels(thread);
905 if (max_active_levels == 1)
906 max_active_levels = KMP_MAX_ACTIVE_LEVELS_LIMIT;
907 set__max_active_levels(thread, (KMP_DEREF flag) ? max_active_levels : 1);
908#endif
909}
910
911int FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_NESTED)(void) {
912#ifdef KMP_STUB
913 return __kmps_get_nested();
914#else
915 kmp_info_t *thread;
916 thread = __kmp_entry_thread();
917 KMP_INFORM(APIDeprecated, "omp_get_nested", "omp_get_max_active_levels");
918 return get__max_active_levels(thread) > 1;
919#endif
920}
921
922void FTN_STDCALL KMP_EXPAND_NAME(FTN_SET_DYNAMIC)(int KMP_DEREF flag) {
923#ifdef KMP_STUB
924 __kmps_set_dynamic(KMP_DEREF flag ? TRUE : FALSE);
925#else
926 kmp_info_t *thread;
927 /* For the thread-private implementation of the internal controls */
928 thread = __kmp_entry_thread();
929 // !!! What if foreign thread calls it?
930 __kmp_save_internal_controls(thread);
931 set__dynamic(thread, KMP_DEREF flag ? true : false);
932#endif
933}
934
935int FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_DYNAMIC)(void) {
936#ifdef KMP_STUB
937 return __kmps_get_dynamic();
938#else
939 kmp_info_t *thread;
940 thread = __kmp_entry_thread();
941 return get__dynamic(thread);
942#endif
943}
944
945int FTN_STDCALL KMP_EXPAND_NAME(FTN_IN_PARALLEL)(void) {
946#ifdef KMP_STUB
947 return 0;
948#else
949 kmp_info_t *th = __kmp_entry_thread();
950 if (th->th.th_teams_microtask) {
951 // AC: r_in_parallel does not work inside teams construct where real
952 // parallel is inactive, but all threads have same root, so setting it in
953 // one team affects other teams.
954 // The solution is to use per-team nesting level
955 return (th->th.th_team->t.t_active_level ? 1 : 0);
956 } else
957 return (th->th.th_root->r.r_in_parallel ? FTN_TRUE : FTN_FALSE);
958#endif
959}
960
961void FTN_STDCALL KMP_EXPAND_NAME(FTN_SET_SCHEDULE)(kmp_sched_t KMP_DEREF kind,
962 int KMP_DEREF modifier) {
963#ifdef KMP_STUB
964 __kmps_set_schedule(KMP_DEREF kind, KMP_DEREF modifier);
965#else
966 /* TO DO: For the per-task implementation of the internal controls */
967 __kmp_set_schedule(__kmp_entry_gtid(), KMP_DEREF kind, KMP_DEREF modifier);
968#endif
969}
970
971void FTN_STDCALL FTN_SET_SCHEDULE_8(kmp_sched_t KMP_DEREF kind,
972 kmp_int64 KMP_DEREF modifier) {
973 int modifier32 = __kmp_ftn_int64_to_int(KMP_DEREF modifier);
974 KMP_EXPAND_NAME(FTN_SET_SCHEDULE)(kind, KMP_FTN_PASS(modifier32));
975}
976
977void FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_SCHEDULE)(kmp_sched_t *kind,
978 int *modifier) {
979#ifdef KMP_STUB
980 __kmps_get_schedule(kind, modifier);
981#else
982 /* TO DO: For the per-task implementation of the internal controls */
983 __kmp_get_schedule(__kmp_entry_gtid(), kind, modifier);
984#endif
985}
986
987void FTN_STDCALL FTN_GET_SCHEDULE_8(kmp_sched_t *kind, kmp_int64 *modifier) {
988 int modifier32;
989 KMP_EXPAND_NAME(FTN_GET_SCHEDULE)(kind, &modifier32);
990 *modifier = modifier32;
991}
992
993void FTN_STDCALL KMP_EXPAND_NAME(FTN_SET_MAX_ACTIVE_LEVELS)(int KMP_DEREF arg) {
994#ifdef KMP_STUB
995// Nothing.
996#else
997 /* TO DO: We want per-task implementation of this internal control */
998 __kmp_set_max_active_levels(__kmp_entry_gtid(), KMP_DEREF arg);
999#endif
1000}
1001
1002void FTN_STDCALL FTN_SET_MAX_ACTIVE_LEVELS_8(kmp_int64 KMP_DEREF arg) {
1003 int arg32 = __kmp_ftn_int64_to_int(KMP_DEREF arg);
1004 KMP_EXPAND_NAME(FTN_SET_MAX_ACTIVE_LEVELS)(KMP_FTN_PASS(arg32));
1005}
1006
1007int FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_MAX_ACTIVE_LEVELS)(void) {
1008#ifdef KMP_STUB
1009 return 0;
1010#else
1011 /* TO DO: We want per-task implementation of this internal control */
1012 if (!TCR_4(__kmp_init_middle)) {
1013 __kmp_middle_initialize();
1014 }
1015 return __kmp_get_max_active_levels(__kmp_entry_gtid());
1016#endif
1017}
1018
1019int FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_ACTIVE_LEVEL)(void) {
1020#ifdef KMP_STUB
1021 return 0; // returns 0 if it is called from the sequential part of the program
1022#else
1023 /* TO DO: For the per-task implementation of the internal controls */
1024 return __kmp_entry_thread()->th.th_team->t.t_active_level;
1025#endif
1026}
1027
1028int FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_LEVEL)(void) {
1029#ifdef KMP_STUB
1030 return 0; // returns 0 if it is called from the sequential part of the program
1031#else
1032 /* TO DO: For the per-task implementation of the internal controls */
1033 return __kmp_entry_thread()->th.th_team->t.t_level;
1034#endif
1035}
1036
1037int FTN_STDCALL
1038KMP_EXPAND_NAME(FTN_GET_ANCESTOR_THREAD_NUM)(int KMP_DEREF level) {
1039#ifdef KMP_STUB
1040 return (KMP_DEREF level) ? (-1) : (0);
1041#else
1042 return __kmp_get_ancestor_thread_num(__kmp_entry_gtid(), KMP_DEREF level);
1043#endif
1044}
1045
1046int FTN_STDCALL FTN_GET_ANCESTOR_THREAD_NUM_8(kmp_int64 KMP_DEREF level) {
1047 int level32 = __kmp_ftn_int64_to_int(KMP_DEREF level);
1048 return KMP_EXPAND_NAME(FTN_GET_ANCESTOR_THREAD_NUM)(KMP_FTN_PASS(level32));
1049}
1050
1051int FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_TEAM_SIZE)(int KMP_DEREF level) {
1052#ifdef KMP_STUB
1053 return (KMP_DEREF level) ? (-1) : (1);
1054#else
1055 return __kmp_get_team_size(__kmp_entry_gtid(), KMP_DEREF level);
1056#endif
1057}
1058
1059int FTN_STDCALL FTN_GET_TEAM_SIZE_8(kmp_int64 KMP_DEREF level) {
1060 int level32 = __kmp_ftn_int64_to_int(KMP_DEREF level);
1061 return KMP_EXPAND_NAME(FTN_GET_TEAM_SIZE)(KMP_FTN_PASS(level32));
1062}
1063
1064int FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_THREAD_LIMIT)(void) {
1065#ifdef KMP_STUB
1066 return 1; // TO DO: clarify whether it returns 1 or 0?
1067#else
1068 int gtid;
1069 kmp_info_t *thread;
1070 if (!__kmp_init_serial) {
1071 __kmp_serial_initialize();
1072 }
1073
1074 gtid = __kmp_entry_gtid();
1075 thread = __kmp_threads[gtid];
1076 // If thread_limit for the target task is defined, return that instead of the
1077 // regular task thread_limit
1078 if (int thread_limit = thread->th.th_current_task->td_icvs.task_thread_limit)
1079 return thread_limit;
1080 return thread->th.th_current_task->td_icvs.thread_limit;
1081#endif
1082}
1083
1084int FTN_STDCALL KMP_EXPAND_NAME(FTN_IN_FINAL)(void) {
1085#ifdef KMP_STUB
1086 return 0; // TO DO: clarify whether it returns 1 or 0?
1087#else
1088 if (!TCR_4(__kmp_init_parallel)) {
1089 return 0;
1090 }
1091 return __kmp_entry_thread()->th.th_current_task->td_flags.final;
1092#endif
1093}
1094
1095kmp_proc_bind_t FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_PROC_BIND)(void) {
1096#ifdef KMP_STUB
1097 return __kmps_get_proc_bind();
1098#else
1099 return get__proc_bind(__kmp_entry_thread());
1100#endif
1101}
1102
1103int FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_NUM_PLACES)(void) {
1104#if defined(KMP_STUB) || !KMP_AFFINITY_SUPPORTED
1105 return 0;
1106#else
1107 if (!TCR_4(__kmp_init_middle)) {
1108 __kmp_middle_initialize();
1109 }
1110 if (!KMP_AFFINITY_CAPABLE())
1111 return 0;
1112 if (!__kmp_affinity.flags.reset) {
1113 // only bind root here if its affinity reset is not requested
1114 int gtid = __kmp_entry_gtid();
1115 kmp_info_t *thread = __kmp_threads[gtid];
1116 if (thread->th.th_team->t.t_level == 0) {
1117 __kmp_assign_root_init_mask();
1118 }
1119 }
1120 return __kmp_affinity.num_masks;
1121#endif
1122}
1123
1124int FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_PLACE_NUM_PROCS)(int place_num) {
1125#if defined(KMP_STUB) || !KMP_AFFINITY_SUPPORTED
1126 return 0;
1127#else
1128 int i;
1129 int retval = 0;
1130 if (!TCR_4(__kmp_init_middle)) {
1131 __kmp_middle_initialize();
1132 }
1133 if (!KMP_AFFINITY_CAPABLE())
1134 return 0;
1135 if (!__kmp_affinity.flags.reset) {
1136 // only bind root here if its affinity reset is not requested
1137 int gtid = __kmp_entry_gtid();
1138 kmp_info_t *thread = __kmp_threads[gtid];
1139 if (thread->th.th_team->t.t_level == 0) {
1140 __kmp_assign_root_init_mask();
1141 }
1142 }
1143 if (place_num < 0 || place_num >= (int)__kmp_affinity.num_masks)
1144 return 0;
1145 kmp_affin_mask_t *mask = KMP_CPU_INDEX(__kmp_affinity.masks, place_num);
1146 KMP_CPU_SET_ITERATE(i, mask) {
1147 if ((!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) ||
1148 (!KMP_CPU_ISSET(i, mask))) {
1149 continue;
1150 }
1151 ++retval;
1152 }
1153 return retval;
1154#endif
1155}
1156
1157int FTN_STDCALL FTN_GET_PLACE_NUM_PROCS_8(kmp_int64 KMP_DEREF place_num) {
1158 return KMP_EXPAND_NAME(FTN_GET_PLACE_NUM_PROCS)(
1159 __kmp_ftn_int64_to_int(KMP_DEREF place_num));
1160}
1161
1162void FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_PLACE_PROC_IDS)(int place_num,
1163 int *ids) {
1164#if defined(KMP_STUB) || !KMP_AFFINITY_SUPPORTED
1165// Nothing.
1166#else
1167 int i, j;
1168 if (!TCR_4(__kmp_init_middle)) {
1169 __kmp_middle_initialize();
1170 }
1171 if (!KMP_AFFINITY_CAPABLE())
1172 return;
1173 if (!__kmp_affinity.flags.reset) {
1174 // only bind root here if its affinity reset is not requested
1175 int gtid = __kmp_entry_gtid();
1176 kmp_info_t *thread = __kmp_threads[gtid];
1177 if (thread->th.th_team->t.t_level == 0) {
1178 __kmp_assign_root_init_mask();
1179 }
1180 }
1181 if (place_num < 0 || place_num >= (int)__kmp_affinity.num_masks)
1182 return;
1183 kmp_affin_mask_t *mask = KMP_CPU_INDEX(__kmp_affinity.masks, place_num);
1184 j = 0;
1185 KMP_CPU_SET_ITERATE(i, mask) {
1186 if ((!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) ||
1187 (!KMP_CPU_ISSET(i, mask))) {
1188 continue;
1189 }
1190 ids[j++] = i;
1191 }
1192#endif
1193}
1194
1195void FTN_STDCALL FTN_GET_PLACE_PROC_IDS_8(kmp_int64 KMP_DEREF place_num,
1196 kmp_int64 *ids) {
1197 int place_num32 = __kmp_ftn_int64_to_int(KMP_DEREF place_num);
1198 int n = KMP_EXPAND_NAME(FTN_GET_PLACE_NUM_PROCS)(place_num32);
1199 int *ids32;
1200 if (n <= 0)
1201 return;
1202 ids32 = (int *)KMP_INTERNAL_MALLOC(sizeof(int) * n);
1203 if (ids32 == NULL)
1204 return;
1205 KMP_EXPAND_NAME(FTN_GET_PLACE_PROC_IDS)(place_num32, ids32);
1206 for (int i = 0; i < n; ++i)
1207 ids[i] = ids32[i];
1208 KMP_INTERNAL_FREE(ids32);
1209}
1210
1211int FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_PLACE_NUM)(void) {
1212#if defined(KMP_STUB) || !KMP_AFFINITY_SUPPORTED
1213 return -1;
1214#else
1215 int gtid;
1216 kmp_info_t *thread;
1217 if (!TCR_4(__kmp_init_middle)) {
1218 __kmp_middle_initialize();
1219 }
1220 if (!KMP_AFFINITY_CAPABLE())
1221 return -1;
1222 gtid = __kmp_entry_gtid();
1223 thread = __kmp_thread_from_gtid(gtid);
1224 if (thread->th.th_team->t.t_level == 0 && !__kmp_affinity.flags.reset) {
1225 __kmp_assign_root_init_mask();
1226 }
1227 if (thread->th.th_current_place < 0)
1228 return -1;
1229 return thread->th.th_current_place;
1230#endif
1231}
1232
1233int FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_PARTITION_NUM_PLACES)(void) {
1234#if defined(KMP_STUB) || !KMP_AFFINITY_SUPPORTED
1235 return 0;
1236#else
1237 int gtid, num_places, first_place, last_place;
1238 kmp_info_t *thread;
1239 if (!TCR_4(__kmp_init_middle)) {
1240 __kmp_middle_initialize();
1241 }
1242 if (!KMP_AFFINITY_CAPABLE())
1243 return 0;
1244 gtid = __kmp_entry_gtid();
1245 thread = __kmp_thread_from_gtid(gtid);
1246 if (thread->th.th_team->t.t_level == 0 && !__kmp_affinity.flags.reset) {
1247 __kmp_assign_root_init_mask();
1248 }
1249 first_place = thread->th.th_first_place;
1250 last_place = thread->th.th_last_place;
1251 if (first_place < 0 || last_place < 0)
1252 return 0;
1253 if (first_place <= last_place)
1254 num_places = last_place - first_place + 1;
1255 else
1256 num_places = __kmp_affinity.num_masks - first_place + last_place + 1;
1257 return num_places;
1258#endif
1259}
1260
1261void FTN_STDCALL
1262KMP_EXPAND_NAME(FTN_GET_PARTITION_PLACE_NUMS)(int *place_nums) {
1263#if defined(KMP_STUB) || !KMP_AFFINITY_SUPPORTED
1264// Nothing.
1265#else
1266 int i, gtid, place_num, first_place, last_place, start, end;
1267 kmp_info_t *thread;
1268 if (!TCR_4(__kmp_init_middle)) {
1269 __kmp_middle_initialize();
1270 }
1271 if (!KMP_AFFINITY_CAPABLE())
1272 return;
1273 gtid = __kmp_entry_gtid();
1274 thread = __kmp_thread_from_gtid(gtid);
1275 if (thread->th.th_team->t.t_level == 0 && !__kmp_affinity.flags.reset) {
1276 __kmp_assign_root_init_mask();
1277 }
1278 first_place = thread->th.th_first_place;
1279 last_place = thread->th.th_last_place;
1280 if (first_place < 0 || last_place < 0)
1281 return;
1282 if (first_place <= last_place) {
1283 start = first_place;
1284 end = last_place;
1285 } else {
1286 start = last_place;
1287 end = first_place;
1288 }
1289 for (i = 0, place_num = start; place_num <= end; ++place_num, ++i) {
1290 place_nums[i] = place_num;
1291 }
1292#endif
1293}
1294
1295void FTN_STDCALL FTN_GET_PARTITION_PLACE_NUMS_8(kmp_int64 *place_nums) {
1296#if defined(KMP_STUB) || !KMP_AFFINITY_SUPPORTED
1297// Nothing.
1298#else
1299 int i, gtid, place_num, first_place, last_place, start, end;
1300 kmp_info_t *thread;
1301 if (!TCR_4(__kmp_init_middle)) {
1302 __kmp_middle_initialize();
1303 }
1304 if (!KMP_AFFINITY_CAPABLE())
1305 return;
1306 gtid = __kmp_entry_gtid();
1307 thread = __kmp_thread_from_gtid(gtid);
1308 if (thread->th.th_team->t.t_level == 0 && !__kmp_affinity.flags.reset) {
1309 __kmp_assign_root_init_mask();
1310 }
1311 first_place = thread->th.th_first_place;
1312 last_place = thread->th.th_last_place;
1313 if (first_place < 0 || last_place < 0)
1314 return;
1315 if (first_place <= last_place) {
1316 start = first_place;
1317 end = last_place;
1318 } else {
1319 start = last_place;
1320 end = first_place;
1321 }
1322 for (i = 0, place_num = start; place_num <= end; ++place_num, ++i) {
1323 place_nums[i] = place_num;
1324 }
1325#endif
1326}
1327
1328int FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_NUM_TEAMS)(void) {
1329#ifdef KMP_STUB
1330 return 1;
1331#else
1332 return __kmp_aux_get_num_teams();
1333#endif
1334}
1335
1336int FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_TEAM_NUM)(void) {
1337#ifdef KMP_STUB
1338 return 0;
1339#else
1340 return __kmp_aux_get_team_num();
1341#endif
1342}
1343
1344int FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_DEFAULT_DEVICE)(void) {
1345#if KMP_MIC || KMP_OS_DARWIN || defined(KMP_STUB)
1346 return 0;
1347#else
1348 return __kmp_entry_thread()->th.th_current_task->td_icvs.default_device;
1349#endif
1350}
1351
1352void FTN_STDCALL KMP_EXPAND_NAME(FTN_SET_DEFAULT_DEVICE)(int KMP_DEREF arg) {
1353#if KMP_MIC || KMP_OS_DARWIN || defined(KMP_STUB)
1354// Nothing.
1355#else
1356 __kmp_entry_thread()->th.th_current_task->td_icvs.default_device =
1357 KMP_DEREF arg;
1358#endif
1359}
1360
1361void FTN_STDCALL FTN_SET_DEFAULT_DEVICE_8(kmp_int64 KMP_DEREF arg) {
1362 int arg32 = __kmp_ftn_int64_to_int(KMP_DEREF arg);
1363 KMP_EXPAND_NAME(FTN_SET_DEFAULT_DEVICE)(KMP_FTN_PASS(arg32));
1364}
1365
1366// Get number of NON-HOST devices.
1367// libomptarget, if loaded, provides this function in api.cpp.
1368int FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_NUM_DEVICES)(void)
1369 KMP_WEAK_ATTRIBUTE_EXTERNAL;
1370int FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_NUM_DEVICES)(void) {
1371#if KMP_MIC || KMP_OS_DARWIN || KMP_OS_WASI || defined(KMP_STUB)
1372 return 0;
1373#else
1374 int (*fptr)();
1375 if ((*(void **)(&fptr) = KMP_DLSYM("__tgt_get_num_devices"))) {
1376 return (*fptr)();
1377 } else if ((*(void **)(&fptr) = KMP_DLSYM_NEXT("omp_get_num_devices"))) {
1378 return (*fptr)();
1379 } else if ((*(void **)(&fptr) = KMP_DLSYM("_Offload_number_of_devices"))) {
1380 return (*fptr)();
1381 } else { // liboffload & libomptarget don't exist
1382 return 0;
1383 }
1384#endif // KMP_MIC || KMP_OS_DARWIN || KMP_OS_WINDOWS || defined(KMP_STUB)
1385}
1386
1387// This function always returns true when called on host device.
1388// Compiler/libomptarget should handle when it is called inside target region.
1389int FTN_STDCALL KMP_EXPAND_NAME(FTN_IS_INITIAL_DEVICE)(void)
1390 KMP_WEAK_ATTRIBUTE_EXTERNAL;
1391int FTN_STDCALL KMP_EXPAND_NAME(FTN_IS_INITIAL_DEVICE)(void) {
1392 return 1; // This is the host
1393}
1394
1395// libomptarget, if loaded, provides this function
1396int FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_INITIAL_DEVICE)(void)
1397 KMP_WEAK_ATTRIBUTE_EXTERNAL;
1398int FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_INITIAL_DEVICE)(void) {
1399 // same as omp_get_num_devices()
1400 return KMP_EXPAND_NAME(FTN_GET_NUM_DEVICES)();
1401}
1402
1403#if defined(KMP_STUB)
1404// Entries for stubs library
1405// As all *target* functions are C-only parameters always passed by value
1406void *FTN_STDCALL FTN_TARGET_ALLOC(size_t size, int device_num) { return 0; }
1407
1408void FTN_STDCALL FTN_TARGET_FREE(void *device_ptr, int device_num) {}
1409
1410int FTN_STDCALL FTN_TARGET_IS_PRESENT(void *ptr, int device_num) { return 0; }
1411
1412int FTN_STDCALL FTN_TARGET_MEMCPY(void *dst, void *src, size_t length,
1413 size_t dst_offset, size_t src_offset,
1414 int dst_device, int src_device) {
1415 return -1;
1416}
1417
1418int FTN_STDCALL FTN_TARGET_MEMCPY_RECT(
1419 void *dst, void *src, size_t element_size, int num_dims,
1420 const size_t *volume, const size_t *dst_offsets, const size_t *src_offsets,
1421 const size_t *dst_dimensions, const size_t *src_dimensions, int dst_device,
1422 int src_device) {
1423 return -1;
1424}
1425
1426int FTN_STDCALL FTN_TARGET_ASSOCIATE_PTR(void *host_ptr, void *device_ptr,
1427 size_t size, size_t device_offset,
1428 int device_num) {
1429 return -1;
1430}
1431
1432int FTN_STDCALL FTN_TARGET_DISASSOCIATE_PTR(void *host_ptr, int device_num) {
1433 return -1;
1434}
1435#endif // defined(KMP_STUB)
1436
1437#ifdef KMP_STUB
1438typedef enum { UNINIT = -1, UNLOCKED, LOCKED } kmp_stub_lock_t;
1439#endif /* KMP_STUB */
1440
1441#if KMP_USE_DYNAMIC_LOCK
1442void FTN_STDCALL FTN_INIT_LOCK_WITH_HINT(void **user_lock,
1443 uintptr_t KMP_DEREF hint) {
1444#ifdef KMP_STUB
1445 *((kmp_stub_lock_t *)user_lock) = UNLOCKED;
1446#else
1447 int gtid = __kmp_entry_gtid();
1448#if OMPT_SUPPORT && OMPT_OPTIONAL
1449 OMPT_STORE_RETURN_ADDRESS(gtid);
1450#endif
1451 __kmpc_init_lock_with_hint(NULL, gtid, user_lock, KMP_DEREF hint);
1452#endif
1453}
1454
1455void FTN_STDCALL FTN_INIT_NEST_LOCK_WITH_HINT(void **user_lock,
1456 uintptr_t KMP_DEREF hint) {
1457#ifdef KMP_STUB
1458 *((kmp_stub_lock_t *)user_lock) = UNLOCKED;
1459#else
1460 int gtid = __kmp_entry_gtid();
1461#if OMPT_SUPPORT && OMPT_OPTIONAL
1462 OMPT_STORE_RETURN_ADDRESS(gtid);
1463#endif
1464 __kmpc_init_nest_lock_with_hint(NULL, gtid, user_lock, KMP_DEREF hint);
1465#endif
1466}
1467#endif
1468
1469/* initialize the lock */
1470void FTN_STDCALL KMP_EXPAND_NAME(FTN_INIT_LOCK)(void **user_lock) {
1471#ifdef KMP_STUB
1472 *((kmp_stub_lock_t *)user_lock) = UNLOCKED;
1473#else
1474 int gtid = __kmp_entry_gtid();
1475#if OMPT_SUPPORT && OMPT_OPTIONAL
1476 OMPT_STORE_RETURN_ADDRESS(gtid);
1477#endif
1478 __kmpc_init_lock(NULL, gtid, user_lock);
1479#endif
1480}
1481
1482/* initialize the lock */
1483void FTN_STDCALL KMP_EXPAND_NAME(FTN_INIT_NEST_LOCK)(void **user_lock) {
1484#ifdef KMP_STUB
1485 *((kmp_stub_lock_t *)user_lock) = UNLOCKED;
1486#else
1487 int gtid = __kmp_entry_gtid();
1488#if OMPT_SUPPORT && OMPT_OPTIONAL
1489 OMPT_STORE_RETURN_ADDRESS(gtid);
1490#endif
1491 __kmpc_init_nest_lock(NULL, gtid, user_lock);
1492#endif
1493}
1494
1495void FTN_STDCALL KMP_EXPAND_NAME(FTN_DESTROY_LOCK)(void **user_lock) {
1496#ifdef KMP_STUB
1497 *((kmp_stub_lock_t *)user_lock) = UNINIT;
1498#else
1499 int gtid = __kmp_entry_gtid();
1500#if OMPT_SUPPORT && OMPT_OPTIONAL
1501 OMPT_STORE_RETURN_ADDRESS(gtid);
1502#endif
1503 __kmpc_destroy_lock(NULL, gtid, user_lock);
1504#endif
1505}
1506
1507void FTN_STDCALL KMP_EXPAND_NAME(FTN_DESTROY_NEST_LOCK)(void **user_lock) {
1508#ifdef KMP_STUB
1509 *((kmp_stub_lock_t *)user_lock) = UNINIT;
1510#else
1511 int gtid = __kmp_entry_gtid();
1512#if OMPT_SUPPORT && OMPT_OPTIONAL
1513 OMPT_STORE_RETURN_ADDRESS(gtid);
1514#endif
1515 __kmpc_destroy_nest_lock(NULL, gtid, user_lock);
1516#endif
1517}
1518
1519void FTN_STDCALL KMP_EXPAND_NAME(FTN_SET_LOCK)(void **user_lock) {
1520#ifdef KMP_STUB
1521 if (*((kmp_stub_lock_t *)user_lock) == UNINIT) {
1522 // TODO: Issue an error.
1523 }
1524 if (*((kmp_stub_lock_t *)user_lock) != UNLOCKED) {
1525 // TODO: Issue an error.
1526 }
1527 *((kmp_stub_lock_t *)user_lock) = LOCKED;
1528#else
1529 int gtid = __kmp_entry_gtid();
1530#if OMPT_SUPPORT && OMPT_OPTIONAL
1531 OMPT_STORE_RETURN_ADDRESS(gtid);
1532#endif
1533 __kmpc_set_lock(NULL, gtid, user_lock);
1534#endif
1535}
1536
1537void FTN_STDCALL KMP_EXPAND_NAME(FTN_SET_NEST_LOCK)(void **user_lock) {
1538#ifdef KMP_STUB
1539 if (*((kmp_stub_lock_t *)user_lock) == UNINIT) {
1540 // TODO: Issue an error.
1541 }
1542 (*((int *)user_lock))++;
1543#else
1544 int gtid = __kmp_entry_gtid();
1545#if OMPT_SUPPORT && OMPT_OPTIONAL
1546 OMPT_STORE_RETURN_ADDRESS(gtid);
1547#endif
1548 __kmpc_set_nest_lock(NULL, gtid, user_lock);
1549#endif
1550}
1551
1552void FTN_STDCALL KMP_EXPAND_NAME(FTN_UNSET_LOCK)(void **user_lock) {
1553#ifdef KMP_STUB
1554 if (*((kmp_stub_lock_t *)user_lock) == UNINIT) {
1555 // TODO: Issue an error.
1556 }
1557 if (*((kmp_stub_lock_t *)user_lock) == UNLOCKED) {
1558 // TODO: Issue an error.
1559 }
1560 *((kmp_stub_lock_t *)user_lock) = UNLOCKED;
1561#else
1562 int gtid = __kmp_entry_gtid();
1563#if OMPT_SUPPORT && OMPT_OPTIONAL
1564 OMPT_STORE_RETURN_ADDRESS(gtid);
1565#endif
1566 __kmpc_unset_lock(NULL, gtid, user_lock);
1567#endif
1568}
1569
1570void FTN_STDCALL KMP_EXPAND_NAME(FTN_UNSET_NEST_LOCK)(void **user_lock) {
1571#ifdef KMP_STUB
1572 if (*((kmp_stub_lock_t *)user_lock) == UNINIT) {
1573 // TODO: Issue an error.
1574 }
1575 if (*((kmp_stub_lock_t *)user_lock) == UNLOCKED) {
1576 // TODO: Issue an error.
1577 }
1578 (*((int *)user_lock))--;
1579#else
1580 int gtid = __kmp_entry_gtid();
1581#if OMPT_SUPPORT && OMPT_OPTIONAL
1582 OMPT_STORE_RETURN_ADDRESS(gtid);
1583#endif
1584 __kmpc_unset_nest_lock(NULL, gtid, user_lock);
1585#endif
1586}
1587
1588int FTN_STDCALL KMP_EXPAND_NAME(FTN_TEST_LOCK)(void **user_lock) {
1589#ifdef KMP_STUB
1590 if (*((kmp_stub_lock_t *)user_lock) == UNINIT) {
1591 // TODO: Issue an error.
1592 }
1593 if (*((kmp_stub_lock_t *)user_lock) == LOCKED) {
1594 return 0;
1595 }
1596 *((kmp_stub_lock_t *)user_lock) = LOCKED;
1597 return 1;
1598#else
1599 int gtid = __kmp_entry_gtid();
1600#if OMPT_SUPPORT && OMPT_OPTIONAL
1601 OMPT_STORE_RETURN_ADDRESS(gtid);
1602#endif
1603 return __kmpc_test_lock(NULL, gtid, user_lock);
1604#endif
1605}
1606
1607int FTN_STDCALL KMP_EXPAND_NAME(FTN_TEST_NEST_LOCK)(void **user_lock) {
1608#ifdef KMP_STUB
1609 if (*((kmp_stub_lock_t *)user_lock) == UNINIT) {
1610 // TODO: Issue an error.
1611 }
1612 return ++(*((int *)user_lock));
1613#else
1614 int gtid = __kmp_entry_gtid();
1615#if OMPT_SUPPORT && OMPT_OPTIONAL
1616 OMPT_STORE_RETURN_ADDRESS(gtid);
1617#endif
1618 return __kmpc_test_nest_lock(NULL, gtid, user_lock);
1619#endif
1620}
1621
1622double FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_WTIME)(void) {
1623#ifdef KMP_STUB
1624 return __kmps_get_wtime();
1625#else
1626 double data;
1627#if !KMP_OS_LINUX
1628 // We don't need library initialization to get the time on Linux* OS. The
1629 // routine can be used to measure library initialization time on Linux* OS now
1630 if (!__kmp_init_serial) {
1631 __kmp_serial_initialize();
1632 }
1633#endif
1634 __kmp_elapsed(&data);
1635 return data;
1636#endif
1637}
1638
1639double FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_WTICK)(void) {
1640#ifdef KMP_STUB
1641 return __kmps_get_wtick();
1642#else
1643 double data;
1644 if (!__kmp_init_serial) {
1645 __kmp_serial_initialize();
1646 }
1647 __kmp_elapsed_tick(&data);
1648 return data;
1649#endif
1650}
1651
1652/* ------------------------------------------------------------------------ */
1653
1654void *FTN_STDCALL FTN_MALLOC(size_t KMP_DEREF size) {
1655 // kmpc_malloc initializes the library if needed
1656 return kmpc_malloc(KMP_DEREF size);
1657}
1658
1659void *FTN_STDCALL FTN_ALIGNED_MALLOC(size_t KMP_DEREF size,
1660 size_t KMP_DEREF alignment) {
1661 // kmpc_aligned_malloc initializes the library if needed
1662 return kmpc_aligned_malloc(KMP_DEREF size, KMP_DEREF alignment);
1663}
1664
1665void *FTN_STDCALL FTN_CALLOC(size_t KMP_DEREF nelem, size_t KMP_DEREF elsize) {
1666 // kmpc_calloc initializes the library if needed
1667 return kmpc_calloc(KMP_DEREF nelem, KMP_DEREF elsize);
1668}
1669
1670void *FTN_STDCALL FTN_REALLOC(void *KMP_DEREF ptr, size_t KMP_DEREF size) {
1671 // kmpc_realloc initializes the library if needed
1672 return kmpc_realloc(KMP_DEREF ptr, KMP_DEREF size);
1673}
1674
1675void FTN_STDCALL FTN_KFREE(void *KMP_DEREF ptr) {
1676 // does nothing if the library is not initialized
1677 kmpc_free(KMP_DEREF ptr);
1678}
1679
1680void FTN_STDCALL FTN_SET_WARNINGS_ON(void) {
1681#ifndef KMP_STUB
1682 __kmp_generate_warnings = kmp_warnings_explicit;
1683#endif
1684}
1685
1686void FTN_STDCALL FTN_SET_WARNINGS_OFF(void) {
1687#ifndef KMP_STUB
1688 __kmp_generate_warnings = FALSE;
1689#endif
1690}
1691
1692void FTN_STDCALL FTN_SET_DEFAULTS(char const *str
1693#ifndef PASS_ARGS_BY_VALUE
1694 ,
1695 int len
1696#endif
1697) {
1698#ifndef KMP_STUB
1699 size_t sz;
1700 char const *defaults = str;
1701
1702#ifdef PASS_ARGS_BY_VALUE
1703 sz = KMP_STRLEN(str);
1704#else
1705 sz = (size_t)len;
1706 ConvertedString cstr(str, sz);
1707 defaults = cstr.get();
1708#endif
1709
1710 __kmp_aux_set_defaults(defaults, sz);
1711#endif
1712}
1713
1714/* ------------------------------------------------------------------------ */
1715
1716/* returns the status of cancellation */
1717int FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_CANCELLATION)(void) {
1718#ifdef KMP_STUB
1719 return 0 /* false */;
1720#else
1721 // initialize the library if needed
1722 if (!__kmp_init_serial) {
1723 __kmp_serial_initialize();
1724 }
1725 return __kmp_omp_cancellation;
1726#endif
1727}
1728
1729int FTN_STDCALL FTN_GET_CANCELLATION_STATUS(int cancel_kind) {
1730#ifdef KMP_STUB
1731 return 0 /* false */;
1732#else
1733 return __kmp_get_cancellation_status(cancel_kind);
1734#endif
1735}
1736
1737/* returns the maximum allowed task priority */
1738int FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_MAX_TASK_PRIORITY)(void) {
1739#ifdef KMP_STUB
1740 return 0;
1741#else
1742 if (!__kmp_init_serial) {
1743 __kmp_serial_initialize();
1744 }
1745 return __kmp_max_task_priority;
1746#endif
1747}
1748
1749// These functions will be defined in libomptarget. When libomptarget is not
1750// loaded, we assume we are on the host.
1751// Compiler/libomptarget will handle this if called inside target.
1752int FTN_STDCALL FTN_GET_DEVICE_NUM(void) KMP_WEAK_ATTRIBUTE_EXTERNAL;
1753int FTN_STDCALL FTN_GET_DEVICE_NUM(void) {
1754 return KMP_EXPAND_NAME(FTN_GET_INITIAL_DEVICE)();
1755}
1756const char *FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_UID_FROM_DEVICE)(int device_num)
1757 KMP_WEAK_ATTRIBUTE_EXTERNAL;
1758const char *FTN_STDCALL
1759KMP_EXPAND_NAME(FTN_GET_UID_FROM_DEVICE)(int device_num) {
1760#if KMP_OS_DARWIN || KMP_OS_WASI || defined(KMP_STUB)
1761 return nullptr;
1762#else
1763 const char *(*fptr)(int);
1764 if ((*(void **)(&fptr) = KMP_DLSYM_NEXT("omp_get_uid_from_device")))
1765 return (*fptr)(device_num);
1766 // Returns the same string as used by libomptarget
1767 return "HOST";
1768#endif
1769}
1770int FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_DEVICE_FROM_UID)(const char *device_uid)
1771 KMP_WEAK_ATTRIBUTE_EXTERNAL;
1772int FTN_STDCALL
1773KMP_EXPAND_NAME(FTN_GET_DEVICE_FROM_UID)(const char *device_uid) {
1774#if KMP_OS_DARWIN || KMP_OS_WASI || defined(KMP_STUB)
1775 return -2; // omp_invalid_device, see definition in omp.h
1776#else
1777 int (*fptr)(const char *);
1778 if ((*(void **)(&fptr) = KMP_DLSYM_NEXT("omp_get_device_from_uid")))
1779 return (*fptr)(device_uid);
1780 return KMP_EXPAND_NAME(FTN_GET_INITIAL_DEVICE)();
1781#endif
1782}
1783
1784// Compiler will ensure that this is only called from host in sequential region
1785int FTN_STDCALL KMP_EXPAND_NAME(FTN_PAUSE_RESOURCE)(kmp_pause_status_t kind,
1786 int device_num) {
1787#ifdef KMP_STUB
1788 return 1; // just fail
1789#else
1790 if (kind == kmp_stop_tool_paused)
1791 return 1; // stop_tool must not be specified
1792 if (device_num == KMP_EXPAND_NAME(FTN_GET_INITIAL_DEVICE)())
1793 return __kmpc_pause_resource(kind);
1794 else {
1795 int (*fptr)(kmp_pause_status_t, int);
1796 if ((*(void **)(&fptr) = KMP_DLSYM("tgt_pause_resource")))
1797 return (*fptr)(kind, device_num);
1798 else
1799 return 1; // just fail if there is no libomptarget
1800 }
1801#endif
1802}
1803
1804int FTN_STDCALL FTN_PAUSE_RESOURCE_8(kmp_pause_status_t kind,
1805 kmp_int64 KMP_DEREF device_num) {
1806 return KMP_EXPAND_NAME(FTN_PAUSE_RESOURCE)(
1807 kind, __kmp_ftn_int64_to_int(KMP_DEREF device_num));
1808}
1809
1810// Compiler will ensure that this is only called from host in sequential region
1811int FTN_STDCALL
1812 KMP_EXPAND_NAME(FTN_PAUSE_RESOURCE_ALL)(kmp_pause_status_t kind) {
1813#ifdef KMP_STUB
1814 return 1; // just fail
1815#else
1816 int fails = 0;
1817 int (*fptr)(kmp_pause_status_t, int);
1818 if ((*(void **)(&fptr) = KMP_DLSYM("tgt_pause_resource")))
1819 fails = (*fptr)(kind, KMP_DEVICE_ALL); // pause devices
1820 fails += __kmpc_pause_resource(kind); // pause host
1821 return fails;
1822#endif
1823}
1824
1825// Returns the maximum number of nesting levels supported by implementation
1826int FTN_STDCALL FTN_GET_SUPPORTED_ACTIVE_LEVELS(void) {
1827#ifdef KMP_STUB
1828 return 1;
1829#else
1830 return KMP_MAX_ACTIVE_LEVELS_LIMIT;
1831#endif
1832}
1833
1834void FTN_STDCALL FTN_FULFILL_EVENT(kmp_event_t *event) {
1835#ifndef KMP_STUB
1836 __kmp_fulfill_event(event);
1837#endif
1838}
1839
1840// nteams-var per-device ICV
1841void FTN_STDCALL FTN_SET_NUM_TEAMS(int KMP_DEREF num_teams) {
1842#ifdef KMP_STUB
1843// Nothing.
1844#else
1845 if (!__kmp_init_serial) {
1846 __kmp_serial_initialize();
1847 }
1848 kmp_info_t *th = __kmp_entry_thread();
1849 // OpenMP 5.1, Section 3.4.3: omp_set_num_teams may not be called from
1850 // within a parallel region other than the implicit parallel region.
1851 // Also guard against calls from within a teams region: nteams-var is a
1852 // device-scoped ICV and concurrent modification from multiple team initial
1853 // threads may race.
1854 // t_level counts both active and serialized parallel levels (0 at the
1855 // implicit top-level parallel region), so this catches all non-implicit
1856 // parallel regions.
1857 if (th->th.th_teams_microtask || th->th.th_team->t.t_level > 0) {
1858 KMP_WARNING(SetNumTeamsInParOrTeamsRegion, "omp_set_num_teams");
1859 return;
1860 }
1861 __kmp_set_num_teams(KMP_DEREF num_teams);
1862#endif
1863}
1864
1865void FTN_STDCALL FTN_SET_NUM_TEAMS_8(kmp_int64 KMP_DEREF num_teams) {
1866 int num_teams32 = __kmp_ftn_int64_to_int(KMP_DEREF num_teams);
1867 FTN_SET_NUM_TEAMS(KMP_FTN_PASS(num_teams32));
1868}
1869
1870int FTN_STDCALL FTN_GET_MAX_TEAMS(void) {
1871#ifdef KMP_STUB
1872 return 1;
1873#else
1874 if (!__kmp_init_serial) {
1875 __kmp_serial_initialize();
1876 }
1877 return __kmp_get_max_teams();
1878#endif
1879}
1880// teams-thread-limit-var per-device ICV
1881void FTN_STDCALL FTN_SET_TEAMS_THREAD_LIMIT(int KMP_DEREF limit) {
1882#ifdef KMP_STUB
1883// Nothing.
1884#else
1885 if (!__kmp_init_serial) {
1886 __kmp_serial_initialize();
1887 }
1888 kmp_info_t *th = __kmp_entry_thread();
1889 // OpenMP 5.1, Section 3.4.5: omp_set_teams_thread_limit may not be called
1890 // from within a parallel region other than the implicit parallel region.
1891 // Also guard against calls from within a teams region:
1892 // teams-thread-limit-var is a device-scoped ICV and concurrent modification
1893 // from multiple team initial threads may race.
1894 // t_level counts both active and serialized parallel levels (0 at the
1895 // implicit top-level parallel region), so this catches all non-implicit
1896 // parallel regions.
1897 if (th->th.th_teams_microtask || th->th.th_team->t.t_level > 0) {
1898 KMP_WARNING(SetTeamsThreadLimitInParOrTeamsRegion,
1899 "omp_set_teams_thread_limit");
1900 return;
1901 }
1902 __kmp_set_teams_thread_limit(KMP_DEREF limit);
1903#endif
1904}
1905
1906void FTN_STDCALL FTN_SET_TEAMS_THREAD_LIMIT_8(kmp_int64 KMP_DEREF limit) {
1907 int limit32 = __kmp_ftn_int64_to_int(KMP_DEREF limit);
1908 FTN_SET_TEAMS_THREAD_LIMIT(KMP_FTN_PASS(limit32));
1909}
1910
1911int FTN_STDCALL FTN_GET_TEAMS_THREAD_LIMIT(void) {
1912#ifdef KMP_STUB
1913 return 1;
1914#else
1915 if (!__kmp_init_serial) {
1916 __kmp_serial_initialize();
1917 }
1918 return __kmp_get_teams_thread_limit();
1919#endif
1920}
1921
1923/* OpenMP 5.1 interop */
1924typedef intptr_t omp_intptr_t;
1925
1926/* 0..omp_get_num_interop_properties()-1 are reserved for implementation-defined
1927 * properties */
1928typedef enum omp_interop_property {
1929 omp_ipr_fr_id = -1,
1930 omp_ipr_fr_name = -2,
1931 omp_ipr_vendor = -3,
1932 omp_ipr_vendor_name = -4,
1933 omp_ipr_device_num = -5,
1934 omp_ipr_platform = -6,
1935 omp_ipr_device = -7,
1936 omp_ipr_device_context = -8,
1937 omp_ipr_targetsync = -9,
1938 omp_ipr_first = -9
1939} omp_interop_property_t;
1940
1941#define omp_interop_none 0
1942
1943typedef enum omp_interop_rc {
1944 omp_irc_no_value = 1,
1945 omp_irc_success = 0,
1946 omp_irc_empty = -1,
1947 omp_irc_out_of_range = -2,
1948 omp_irc_type_int = -3,
1949 omp_irc_type_ptr = -4,
1950 omp_irc_type_str = -5,
1951 omp_irc_other = -6
1952} omp_interop_rc_t;
1953
1954typedef enum omp_interop_fr {
1955 omp_ifr_cuda = 1,
1956 omp_ifr_cuda_driver = 2,
1957 omp_ifr_opencl = 3,
1958 omp_ifr_sycl = 4,
1959 omp_ifr_hip = 5,
1960 omp_ifr_level_zero = 6,
1961 omp_ifr_last = 7
1962} omp_interop_fr_t;
1963
1964typedef void *omp_interop_t;
1965
1966// libomptarget, if loaded, provides this function
1967int FTN_STDCALL FTN_GET_NUM_INTEROP_PROPERTIES(const omp_interop_t interop) {
1968#if KMP_OS_DARWIN || KMP_OS_WASI || defined(KMP_STUB)
1969 return 0;
1970#else
1971 int (*fptr)(const omp_interop_t);
1972 if ((*(void **)(&fptr) = KMP_DLSYM_NEXT("omp_get_num_interop_properties")))
1973 return (*fptr)(interop);
1974 return 0;
1975#endif
1976}
1977
1979// libomptarget, if loaded, provides this function
1980intptr_t FTN_STDCALL FTN_GET_INTEROP_INT(const omp_interop_t interop,
1981 omp_interop_property_t property_id,
1982 int *err) {
1983#if KMP_OS_DARWIN || KMP_OS_WASI || defined(KMP_STUB)
1984 return 0;
1985#else
1986 intptr_t (*fptr)(const omp_interop_t, omp_interop_property_t, int *);
1987 if ((*(void **)(&fptr) = KMP_DLSYM_NEXT("omp_get_interop_int")))
1988 return (*fptr)(interop, property_id, err);
1989 return 0;
1990#endif
1991}
1992
1993// libomptarget, if loaded, provides this function
1994void *FTN_STDCALL FTN_GET_INTEROP_PTR(const omp_interop_t interop,
1995 omp_interop_property_t property_id,
1996 int *err) {
1997#if KMP_OS_DARWIN || KMP_OS_WASI || defined(KMP_STUB)
1998 return nullptr;
1999#else
2000 void *(*fptr)(const omp_interop_t, omp_interop_property_t, int *);
2001 if ((*(void **)(&fptr) = KMP_DLSYM_NEXT("omp_get_interop_ptr")))
2002 return (*fptr)(interop, property_id, err);
2003 return nullptr;
2004#endif
2005}
2006
2007// libomptarget, if loaded, provides this function
2008const char *FTN_STDCALL FTN_GET_INTEROP_STR(const omp_interop_t interop,
2009 omp_interop_property_t property_id,
2010 int *err) {
2011#if KMP_OS_DARWIN || KMP_OS_WASI || defined(KMP_STUB)
2012 return nullptr;
2013#else
2014 const char *(*fptr)(const omp_interop_t, omp_interop_property_t, int *);
2015 if ((*(void **)(&fptr) = KMP_DLSYM_NEXT("omp_get_interop_str")))
2016 return (*fptr)(interop, property_id, err);
2017 return nullptr;
2018#endif
2019}
2020
2021// libomptarget, if loaded, provides this function
2022const char *FTN_STDCALL FTN_GET_INTEROP_NAME(
2023 const omp_interop_t interop, omp_interop_property_t property_id) {
2024#if KMP_OS_DARWIN || KMP_OS_WASI || defined(KMP_STUB)
2025 return nullptr;
2026#else
2027 const char *(*fptr)(const omp_interop_t, omp_interop_property_t);
2028 if ((*(void **)(&fptr) = KMP_DLSYM_NEXT("omp_get_interop_name")))
2029 return (*fptr)(interop, property_id);
2030 return nullptr;
2031#endif
2032}
2033
2034// libomptarget, if loaded, provides this function
2035const char *FTN_STDCALL FTN_GET_INTEROP_TYPE_DESC(
2036 const omp_interop_t interop, omp_interop_property_t property_id) {
2037#if KMP_OS_DARWIN || KMP_OS_WASI || defined(KMP_STUB)
2038 return nullptr;
2039#else
2040 const char *(*fptr)(const omp_interop_t, omp_interop_property_t);
2041 if ((*(void **)(&fptr) = KMP_DLSYM_NEXT("omp_get_interop_type_desc")))
2042 return (*fptr)(interop, property_id);
2043 return nullptr;
2044#endif
2045}
2046
2047// libomptarget, if loaded, provides this function
2048const char *FTN_STDCALL FTN_GET_INTEROP_RC_DESC(
2049 const omp_interop_t interop, omp_interop_property_t property_id) {
2050#if KMP_OS_DARWIN || KMP_OS_WASI || defined(KMP_STUB)
2051 return nullptr;
2052#else
2053 const char *(*fptr)(const omp_interop_t, omp_interop_property_t);
2054 if ((*(void **)(&fptr) = KMP_DLSYM_NEXT("omp_get_interop_rec_desc")))
2055 return (*fptr)(interop, property_id);
2056 return nullptr;
2057#endif
2058}
2059
2060// display environment variables when requested
2061void FTN_STDCALL FTN_DISPLAY_ENV(int verbose) {
2062#ifndef KMP_STUB
2063 __kmp_omp_display_env(verbose);
2064#endif
2065}
2066
2067int FTN_STDCALL FTN_IN_EXPLICIT_TASK(void) {
2068#ifdef KMP_STUB
2069 return 0;
2070#else
2071 int gtid = __kmp_entry_gtid();
2072 return __kmp_thread_from_gtid(gtid)->th.th_current_task->td_flags.tasktype;
2073#endif
2074}
2075
2076// GCC compatibility (versioned symbols)
2077#ifdef KMP_USE_VERSION_SYMBOLS
2078
2079/* These following sections create versioned symbols for the
2080 omp_* routines. The KMP_VERSION_SYMBOL macro expands the API name and
2081 then maps it to a versioned symbol.
2082 libgomp ``versions'' its symbols (OMP_1.0, OMP_2.0, OMP_3.0, ...) while also
2083 retaining the default version which libomp uses: VERSION (defined in
2084 exports_so.txt). If you want to see the versioned symbols for libgomp.so.1
2085 then just type:
2086
2087 objdump -T /path/to/libgomp.so.1 | grep omp_
2088
2089 Example:
2090 Step 1) Create __kmp_api_omp_set_num_threads_10_alias which is alias of
2091 __kmp_api_omp_set_num_threads
2092 Step 2) Set __kmp_api_omp_set_num_threads_10_alias to version:
2093 omp_set_num_threads@OMP_1.0
2094 Step 2B) Set __kmp_api_omp_set_num_threads to default version:
2095 omp_set_num_threads@@VERSION
2096*/
2097
2098// OMP_1.0 versioned symbols
2099KMP_VERSION_SYMBOL(FTN_SET_NUM_THREADS, 10, "OMP_1.0");
2100KMP_VERSION_SYMBOL(FTN_GET_NUM_THREADS, 10, "OMP_1.0");
2101KMP_VERSION_SYMBOL(FTN_GET_MAX_THREADS, 10, "OMP_1.0");
2102KMP_VERSION_SYMBOL(FTN_GET_THREAD_NUM, 10, "OMP_1.0");
2103KMP_VERSION_SYMBOL(FTN_GET_NUM_PROCS, 10, "OMP_1.0");
2104KMP_VERSION_SYMBOL(FTN_IN_PARALLEL, 10, "OMP_1.0");
2105KMP_VERSION_SYMBOL(FTN_SET_DYNAMIC, 10, "OMP_1.0");
2106KMP_VERSION_SYMBOL(FTN_GET_DYNAMIC, 10, "OMP_1.0");
2107KMP_VERSION_SYMBOL(FTN_SET_NESTED, 10, "OMP_1.0");
2108KMP_VERSION_SYMBOL(FTN_GET_NESTED, 10, "OMP_1.0");
2109KMP_VERSION_SYMBOL(FTN_INIT_LOCK, 10, "OMP_1.0");
2110KMP_VERSION_SYMBOL(FTN_INIT_NEST_LOCK, 10, "OMP_1.0");
2111KMP_VERSION_SYMBOL(FTN_DESTROY_LOCK, 10, "OMP_1.0");
2112KMP_VERSION_SYMBOL(FTN_DESTROY_NEST_LOCK, 10, "OMP_1.0");
2113KMP_VERSION_SYMBOL(FTN_SET_LOCK, 10, "OMP_1.0");
2114KMP_VERSION_SYMBOL(FTN_SET_NEST_LOCK, 10, "OMP_1.0");
2115KMP_VERSION_SYMBOL(FTN_UNSET_LOCK, 10, "OMP_1.0");
2116KMP_VERSION_SYMBOL(FTN_UNSET_NEST_LOCK, 10, "OMP_1.0");
2117KMP_VERSION_SYMBOL(FTN_TEST_LOCK, 10, "OMP_1.0");
2118KMP_VERSION_SYMBOL(FTN_TEST_NEST_LOCK, 10, "OMP_1.0");
2119
2120// OMP_2.0 versioned symbols
2121KMP_VERSION_SYMBOL(FTN_GET_WTICK, 20, "OMP_2.0");
2122KMP_VERSION_SYMBOL(FTN_GET_WTIME, 20, "OMP_2.0");
2123
2124// OMP_3.0 versioned symbols
2125KMP_VERSION_SYMBOL(FTN_SET_SCHEDULE, 30, "OMP_3.0");
2126KMP_VERSION_SYMBOL(FTN_GET_SCHEDULE, 30, "OMP_3.0");
2127KMP_VERSION_SYMBOL(FTN_GET_THREAD_LIMIT, 30, "OMP_3.0");
2128KMP_VERSION_SYMBOL(FTN_SET_MAX_ACTIVE_LEVELS, 30, "OMP_3.0");
2129KMP_VERSION_SYMBOL(FTN_GET_MAX_ACTIVE_LEVELS, 30, "OMP_3.0");
2130KMP_VERSION_SYMBOL(FTN_GET_ANCESTOR_THREAD_NUM, 30, "OMP_3.0");
2131KMP_VERSION_SYMBOL(FTN_GET_LEVEL, 30, "OMP_3.0");
2132KMP_VERSION_SYMBOL(FTN_GET_TEAM_SIZE, 30, "OMP_3.0");
2133KMP_VERSION_SYMBOL(FTN_GET_ACTIVE_LEVEL, 30, "OMP_3.0");
2134
2135// the lock routines have a 1.0 and 3.0 version
2136KMP_VERSION_SYMBOL(FTN_INIT_LOCK, 30, "OMP_3.0");
2137KMP_VERSION_SYMBOL(FTN_INIT_NEST_LOCK, 30, "OMP_3.0");
2138KMP_VERSION_SYMBOL(FTN_DESTROY_LOCK, 30, "OMP_3.0");
2139KMP_VERSION_SYMBOL(FTN_DESTROY_NEST_LOCK, 30, "OMP_3.0");
2140KMP_VERSION_SYMBOL(FTN_SET_LOCK, 30, "OMP_3.0");
2141KMP_VERSION_SYMBOL(FTN_SET_NEST_LOCK, 30, "OMP_3.0");
2142KMP_VERSION_SYMBOL(FTN_UNSET_LOCK, 30, "OMP_3.0");
2143KMP_VERSION_SYMBOL(FTN_UNSET_NEST_LOCK, 30, "OMP_3.0");
2144KMP_VERSION_SYMBOL(FTN_TEST_LOCK, 30, "OMP_3.0");
2145KMP_VERSION_SYMBOL(FTN_TEST_NEST_LOCK, 30, "OMP_3.0");
2146
2147// OMP_3.1 versioned symbol
2148KMP_VERSION_SYMBOL(FTN_IN_FINAL, 31, "OMP_3.1");
2149
2150// OMP_4.0 versioned symbols
2151KMP_VERSION_SYMBOL(FTN_GET_PROC_BIND, 40, "OMP_4.0");
2152KMP_VERSION_SYMBOL(FTN_GET_NUM_TEAMS, 40, "OMP_4.0");
2153KMP_VERSION_SYMBOL(FTN_GET_TEAM_NUM, 40, "OMP_4.0");
2154KMP_VERSION_SYMBOL(FTN_GET_CANCELLATION, 40, "OMP_4.0");
2155KMP_VERSION_SYMBOL(FTN_GET_DEFAULT_DEVICE, 40, "OMP_4.0");
2156KMP_VERSION_SYMBOL(FTN_SET_DEFAULT_DEVICE, 40, "OMP_4.0");
2157KMP_VERSION_SYMBOL(FTN_IS_INITIAL_DEVICE, 40, "OMP_4.0");
2158KMP_VERSION_SYMBOL(FTN_GET_NUM_DEVICES, 40, "OMP_4.0");
2159
2160// OMP_4.5 versioned symbols
2161KMP_VERSION_SYMBOL(FTN_GET_MAX_TASK_PRIORITY, 45, "OMP_4.5");
2162KMP_VERSION_SYMBOL(FTN_GET_NUM_PLACES, 45, "OMP_4.5");
2163KMP_VERSION_SYMBOL(FTN_GET_PLACE_NUM_PROCS, 45, "OMP_4.5");
2164KMP_VERSION_SYMBOL(FTN_GET_PLACE_PROC_IDS, 45, "OMP_4.5");
2165KMP_VERSION_SYMBOL(FTN_GET_PLACE_NUM, 45, "OMP_4.5");
2166KMP_VERSION_SYMBOL(FTN_GET_PARTITION_NUM_PLACES, 45, "OMP_4.5");
2167KMP_VERSION_SYMBOL(FTN_GET_PARTITION_PLACE_NUMS, 45, "OMP_4.5");
2168KMP_VERSION_SYMBOL(FTN_GET_INITIAL_DEVICE, 45, "OMP_4.5");
2169
2170// OMP_5.0 versioned symbols
2171// KMP_VERSION_SYMBOL(FTN_GET_DEVICE_NUM, 50, "OMP_5.0");
2172KMP_VERSION_SYMBOL(FTN_PAUSE_RESOURCE, 50, "OMP_5.0");
2173KMP_VERSION_SYMBOL(FTN_PAUSE_RESOURCE_ALL, 50, "OMP_5.0");
2174// The C versions (KMP_FTN_PLAIN) of these symbols are in kmp_csupport.c
2175#if KMP_FTN_ENTRIES == KMP_FTN_APPEND
2176KMP_VERSION_SYMBOL(FTN_CAPTURE_AFFINITY, 50, "OMP_5.0");
2177KMP_VERSION_SYMBOL(FTN_DISPLAY_AFFINITY, 50, "OMP_5.0");
2178KMP_VERSION_SYMBOL(FTN_GET_AFFINITY_FORMAT, 50, "OMP_5.0");
2179KMP_VERSION_SYMBOL(FTN_SET_AFFINITY_FORMAT, 50, "OMP_5.0");
2180#endif
2181// KMP_VERSION_SYMBOL(FTN_GET_SUPPORTED_ACTIVE_LEVELS, 50, "OMP_5.0");
2182// KMP_VERSION_SYMBOL(FTN_FULFILL_EVENT, 50, "OMP_5.0");
2183
2184// OMP_6.0 versioned symbols
2185KMP_VERSION_SYMBOL(FTN_GET_UID_FROM_DEVICE, 60, "OMP_6.0");
2186KMP_VERSION_SYMBOL(FTN_GET_DEVICE_FROM_UID, 60, "OMP_6.0");
2187
2188#endif // KMP_USE_VERSION_SYMBOLS
2189
2190#ifdef __cplusplus
2191} // extern "C"
2192#endif // __cplusplus
2193
2194// end of file //
KMP_EXPORT kmp_int32 __kmpc_bound_num_threads(ident_t *)