2 * kmp_stub.cpp -- stub versions of user-callable OpenMP RT functions.
5 //===----------------------------------------------------------------------===//
7 // The LLVM Compiler Infrastructure
9 // This file is dual licensed under the MIT and the University of Illinois Open
10 // Source Licenses. See LICENSE.txt for details.
12 //===----------------------------------------------------------------------===//
19 #include "omp.h" // omp_* declarations, must be included before "kmp.h"
20 #include "kmp.h" // KMP_DEFAULT_STKSIZE
30 #define omp_set_max_active_levels ompc_set_max_active_levels
31 #define omp_set_schedule ompc_set_schedule
32 #define omp_get_ancestor_thread_num ompc_get_ancestor_thread_num
33 #define omp_get_team_size ompc_get_team_size
35 #define omp_set_num_threads ompc_set_num_threads
36 #define omp_set_dynamic ompc_set_dynamic
37 #define omp_set_nested ompc_set_nested
38 #define omp_set_affinity_format ompc_set_affinity_format
39 #define omp_get_affinity_format ompc_get_affinity_format
40 #define omp_display_affinity ompc_display_affinity
41 #define omp_capture_affinity ompc_capture_affinity
42 #define kmp_set_stacksize kmpc_set_stacksize
43 #define kmp_set_stacksize_s kmpc_set_stacksize_s
44 #define kmp_set_blocktime kmpc_set_blocktime
45 #define kmp_set_library kmpc_set_library
46 #define kmp_set_defaults kmpc_set_defaults
47 #define kmp_set_disp_num_buffers kmpc_set_disp_num_buffers
48 #define kmp_malloc kmpc_malloc
49 #define kmp_aligned_malloc kmpc_aligned_malloc
50 #define kmp_calloc kmpc_calloc
51 #define kmp_realloc kmpc_realloc
52 #define kmp_free kmpc_free
55 static double frequency = 0.0;
59 static size_t __kmps_init() {
60 static int initialized = 0;
61 static size_t dummy = 0;
63 // TODO: Analyze KMP_VERSION environment variable, print
64 // __kmp_version_copyright and __kmp_version_build_time.
65 // WARNING: Do not use "fprintf(stderr, ...)" because it will cause
66 // unresolved "__iob" symbol (see C70080). We need to extract __kmp_printf()
67 // stuff from kmp_runtime.cpp and use it.
69 // Trick with dummy variable forces linker to keep __kmp_version_copyright
70 // and __kmp_version_build_time strings in executable file (in case of
71 // static linkage). When KMP_VERSION analysis is implemented, dummy
72 // variable should be deleted, function should return void.
73 dummy = __kmp_version_copyright - __kmp_version_build_time;
77 BOOL status = QueryPerformanceFrequency(&freq);
79 frequency = double(freq.QuadPart);
88 #define i __kmps_init();
90 /* set API functions */
91 void omp_set_num_threads(omp_int_t num_threads) { i; }
92 void omp_set_dynamic(omp_int_t dynamic) {
94 __kmps_set_dynamic(dynamic);
96 void omp_set_nested(omp_int_t nested) {
98 __kmps_set_nested(nested);
100 void omp_set_max_active_levels(omp_int_t max_active_levels) { i; }
101 void omp_set_schedule(omp_sched_t kind, omp_int_t modifier) {
103 __kmps_set_schedule((kmp_sched_t)kind, modifier);
105 int omp_get_ancestor_thread_num(omp_int_t level) {
107 return (level) ? (-1) : (0);
109 int omp_get_team_size(omp_int_t level) {
111 return (level) ? (-1) : (1);
113 int kmpc_set_affinity_mask_proc(int proc, void **mask) {
117 int kmpc_unset_affinity_mask_proc(int proc, void **mask) {
121 int kmpc_get_affinity_mask_proc(int proc, void **mask) {
126 /* kmp API functions */
127 void kmp_set_stacksize(omp_int_t arg) {
129 __kmps_set_stacksize(arg);
131 void kmp_set_stacksize_s(size_t arg) {
133 __kmps_set_stacksize(arg);
135 void kmp_set_blocktime(omp_int_t arg) {
137 __kmps_set_blocktime(arg);
139 void kmp_set_library(omp_int_t arg) {
141 __kmps_set_library(arg);
143 void kmp_set_defaults(char const *str) { i; }
144 void kmp_set_disp_num_buffers(omp_int_t arg) { i; }
146 /* KMP memory management functions. */
147 void *kmp_malloc(size_t size) {
151 // If succesfull returns a pointer to the memory block, otherwise returns
153 // Sets errno to ENOMEM or EINVAL if memory allocation failed or parameter
154 // validation failed.
155 res = _aligned_malloc(size, 1);
161 void *kmp_aligned_malloc(size_t sz, size_t a) {
166 res = _aligned_malloc(sz, a);
168 if (err = posix_memalign(&res, a, sz)) {
169 errno = err; // can be EINVAL or ENOMEM
175 void *kmp_calloc(size_t nelem, size_t elsize) {
179 res = _aligned_recalloc(NULL, nelem, elsize, 1);
181 res = calloc(nelem, elsize);
185 void *kmp_realloc(void *ptr, size_t size) {
189 res = _aligned_realloc(ptr, size, 1);
191 res = realloc(ptr, size);
195 void kmp_free(void *ptr) {
204 static int __kmps_blocktime = INT_MAX;
206 void __kmps_set_blocktime(int arg) {
208 __kmps_blocktime = arg;
209 } // __kmps_set_blocktime
211 int __kmps_get_blocktime(void) {
213 return __kmps_blocktime;
214 } // __kmps_get_blocktime
216 static int __kmps_dynamic = 0;
218 void __kmps_set_dynamic(int arg) {
220 __kmps_dynamic = arg;
221 } // __kmps_set_dynamic
223 int __kmps_get_dynamic(void) {
225 return __kmps_dynamic;
226 } // __kmps_get_dynamic
228 static int __kmps_library = 1000;
230 void __kmps_set_library(int arg) {
232 __kmps_library = arg;
233 } // __kmps_set_library
235 int __kmps_get_library(void) {
237 return __kmps_library;
238 } // __kmps_get_library
240 static int __kmps_nested = 0;
242 void __kmps_set_nested(int arg) {
245 } // __kmps_set_nested
247 int __kmps_get_nested(void) {
249 return __kmps_nested;
250 } // __kmps_get_nested
252 static size_t __kmps_stacksize = KMP_DEFAULT_STKSIZE;
254 void __kmps_set_stacksize(int arg) {
256 __kmps_stacksize = arg;
257 } // __kmps_set_stacksize
259 int __kmps_get_stacksize(void) {
261 return __kmps_stacksize;
262 } // __kmps_get_stacksize
264 static kmp_sched_t __kmps_sched_kind = kmp_sched_default;
265 static int __kmps_sched_modifier = 0;
267 void __kmps_set_schedule(kmp_sched_t kind, int modifier) {
269 __kmps_sched_kind = kind;
270 __kmps_sched_modifier = modifier;
271 } // __kmps_set_schedule
273 void __kmps_get_schedule(kmp_sched_t *kind, int *modifier) {
275 *kind = __kmps_sched_kind;
276 *modifier = __kmps_sched_modifier;
277 } // __kmps_get_schedule
281 static kmp_proc_bind_t __kmps_proc_bind = proc_bind_false;
283 void __kmps_set_proc_bind(kmp_proc_bind_t arg) {
285 __kmps_proc_bind = arg;
286 } // __kmps_set_proc_bind
288 kmp_proc_bind_t __kmps_get_proc_bind(void) {
290 return __kmps_proc_bind;
291 } // __kmps_get_proc_bind
293 #endif /* OMP_40_ENABLED */
295 double __kmps_get_wtime(void) {
296 // Elapsed wall clock time (in second) from "sometime in the past".
300 if (frequency > 0.0) {
302 BOOL status = QueryPerformanceCounter(&now);
304 wtime = double(now.QuadPart) / frequency;
308 // gettimeofday() returns seconds and microseconds since the Epoch.
311 rc = gettimeofday(&tval, NULL);
313 wtime = (double)(tval.tv_sec) + 1.0E-06 * (double)(tval.tv_usec);
315 // TODO: Assert or abort here.
319 } // __kmps_get_wtime
321 double __kmps_get_wtick(void) {
322 // Number of seconds between successive clock ticks.
331 rc = GetSystemTimeAdjustment(&adjustment, &increment, &disabled);
333 wtick = 1.0E-07 * (double)(disabled ? increment : adjustment);
335 // TODO: Assert or abort here.
340 // TODO: gettimeofday() returns in microseconds, but what the precision?
344 } // __kmps_get_wtick
347 /* OpenMP 5.0 Memory Management */
348 const omp_allocator_t *OMP_NULL_ALLOCATOR = NULL;
349 const omp_allocator_t *omp_default_mem_alloc = (const omp_allocator_t *)1;
350 const omp_allocator_t *omp_large_cap_mem_alloc = (const omp_allocator_t *)2;
351 const omp_allocator_t *omp_const_mem_alloc = (const omp_allocator_t *)3;
352 const omp_allocator_t *omp_high_bw_mem_alloc = (const omp_allocator_t *)4;
353 const omp_allocator_t *omp_low_lat_mem_alloc = (const omp_allocator_t *)5;
354 const omp_allocator_t *omp_cgroup_mem_alloc = (const omp_allocator_t *)6;
355 const omp_allocator_t *omp_pteam_mem_alloc = (const omp_allocator_t *)7;
356 const omp_allocator_t *omp_thread_mem_alloc = (const omp_allocator_t *)8;
357 /* OpenMP 5.0 Affinity Format */
358 void omp_set_affinity_format(char const *format) { i; }
359 size_t omp_get_affinity_format(char *buffer, size_t size) {
363 void omp_display_affinity(char const *format) { i; }
364 size_t omp_capture_affinity(char *buffer, size_t buf_size, char const *format) {
368 #endif /* OMP_50_ENABLED */