blob: c1f3bf38755c6af0db5ac1bfffc3af63de08bf2c [file] [log] [blame]
Jim Cownie5e8470a2013-09-27 10:38:44 +00001/*
Jonathan Peytonde4749b2016-12-14 23:01:24 +00002 * kmp_stub.cpp -- stub versions of user-callable OpenMP RT functions.
Jim Cownie5e8470a2013-09-27 10:38:44 +00003 */
4
Jim Cownie5e8470a2013-09-27 10:38:44 +00005//===----------------------------------------------------------------------===//
6//
7// The LLVM Compiler Infrastructure
8//
9// This file is dual licensed under the MIT and the University of Illinois Open
10// Source Licenses. See LICENSE.txt for details.
11//
12//===----------------------------------------------------------------------===//
13
Jim Cownie5e8470a2013-09-27 10:38:44 +000014#include <errno.h>
Jonathan Peyton30419822017-05-12 18:01:32 +000015#include <limits.h>
16#include <stdlib.h>
Jim Cownie5e8470a2013-09-27 10:38:44 +000017
Jonathan Peyton92ca6182018-09-07 18:25:49 +000018#define __KMP_IMP
Andrey Churbanov75bc70f2018-03-05 18:01:47 +000019#include "omp.h" // omp_* declarations, must be included before "kmp.h"
Jonathan Peyton30419822017-05-12 18:01:32 +000020#include "kmp.h" // KMP_DEFAULT_STKSIZE
Jim Cownie4cc4bb42014-10-07 16:25:50 +000021#include "kmp_stub.h"
Jim Cownie5e8470a2013-09-27 10:38:44 +000022
23#if KMP_OS_WINDOWS
Jonathan Peyton30419822017-05-12 18:01:32 +000024#include <windows.h>
Jim Cownie5e8470a2013-09-27 10:38:44 +000025#else
Jonathan Peyton30419822017-05-12 18:01:32 +000026#include <sys/time.h>
Jim Cownie5e8470a2013-09-27 10:38:44 +000027#endif
28
Jim Cownie181b4bb2013-12-23 17:28:57 +000029// Moved from omp.h
Jonathan Peyton30419822017-05-12 18:01:32 +000030#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
Jim Cownie181b4bb2013-12-23 17:28:57 +000034
Jonathan Peyton30419822017-05-12 18:01:32 +000035#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
Jonathan Peyton6d88e042018-12-13 23:14:24 +000038#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
Jonathan Peyton30419822017-05-12 18:01:32 +000042#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
Jim Cownie5e8470a2013-09-27 10:38:44 +000053
Jonathan Peytonbaad3f62018-08-09 22:04:30 +000054#if KMP_OS_WINDOWS
Jim Cownie5e8470a2013-09-27 10:38:44 +000055static double frequency = 0.0;
Jonathan Peytonbaad3f62018-08-09 22:04:30 +000056#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +000057
58// Helper functions.
59static size_t __kmps_init() {
Jonathan Peyton30419822017-05-12 18:01:32 +000060 static int initialized = 0;
61 static size_t dummy = 0;
62 if (!initialized) {
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.
Jim Cownie5e8470a2013-09-27 10:38:44 +000068
Jonathan Peyton30419822017-05-12 18:01:32 +000069 // 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;
Jim Cownie5e8470a2013-09-27 10:38:44 +000074
Jonathan Peyton30419822017-05-12 18:01:32 +000075#if KMP_OS_WINDOWS
76 LARGE_INTEGER freq;
77 BOOL status = QueryPerformanceFrequency(&freq);
78 if (status) {
79 frequency = double(freq.QuadPart);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +000080 }
Jonathan Peyton30419822017-05-12 18:01:32 +000081#endif
82
83 initialized = 1;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +000084 }
Jonathan Peyton30419822017-05-12 18:01:32 +000085 return dummy;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +000086} // __kmps_init
Jim Cownie5e8470a2013-09-27 10:38:44 +000087
88#define i __kmps_init();
89
90/* set API functions */
Jonathan Peyton30419822017-05-12 18:01:32 +000091void omp_set_num_threads(omp_int_t num_threads) { i; }
92void omp_set_dynamic(omp_int_t dynamic) {
93 i;
94 __kmps_set_dynamic(dynamic);
95}
96void omp_set_nested(omp_int_t nested) {
97 i;
98 __kmps_set_nested(nested);
99}
100void omp_set_max_active_levels(omp_int_t max_active_levels) { i; }
101void omp_set_schedule(omp_sched_t kind, omp_int_t modifier) {
102 i;
103 __kmps_set_schedule((kmp_sched_t)kind, modifier);
104}
105int omp_get_ancestor_thread_num(omp_int_t level) {
106 i;
107 return (level) ? (-1) : (0);
108}
109int omp_get_team_size(omp_int_t level) {
110 i;
111 return (level) ? (-1) : (1);
112}
113int kmpc_set_affinity_mask_proc(int proc, void **mask) {
114 i;
115 return -1;
116}
117int kmpc_unset_affinity_mask_proc(int proc, void **mask) {
118 i;
119 return -1;
120}
121int kmpc_get_affinity_mask_proc(int proc, void **mask) {
122 i;
123 return -1;
124}
Jim Cownie5e8470a2013-09-27 10:38:44 +0000125
126/* kmp API functions */
Jonathan Peyton30419822017-05-12 18:01:32 +0000127void kmp_set_stacksize(omp_int_t arg) {
128 i;
129 __kmps_set_stacksize(arg);
130}
131void kmp_set_stacksize_s(size_t arg) {
132 i;
133 __kmps_set_stacksize(arg);
134}
135void kmp_set_blocktime(omp_int_t arg) {
136 i;
137 __kmps_set_blocktime(arg);
138}
139void kmp_set_library(omp_int_t arg) {
140 i;
141 __kmps_set_library(arg);
142}
143void kmp_set_defaults(char const *str) { i; }
144void kmp_set_disp_num_buffers(omp_int_t arg) { i; }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000145
146/* KMP memory management functions. */
Jonathan Peyton30419822017-05-12 18:01:32 +0000147void *kmp_malloc(size_t size) {
148 i;
Jonathan Peyton62da5502017-11-29 22:29:38 +0000149 void *res;
150#if KMP_OS_WINDOWS
151 // If succesfull returns a pointer to the memory block, otherwise returns
152 // NULL.
153 // Sets errno to ENOMEM or EINVAL if memory allocation failed or parameter
154 // validation failed.
155 res = _aligned_malloc(size, 1);
156#else
157 res = malloc(size);
158#endif
159 return res;
Jonathan Peyton30419822017-05-12 18:01:32 +0000160}
161void *kmp_aligned_malloc(size_t sz, size_t a) {
162 i;
Jonathan Peyton30419822017-05-12 18:01:32 +0000163 int err;
Jonathan Peyton62da5502017-11-29 22:29:38 +0000164 void *res;
165#if KMP_OS_WINDOWS
166 res = _aligned_malloc(sz, a);
167#else
Jonathan Peyton30419822017-05-12 18:01:32 +0000168 if (err = posix_memalign(&res, a, sz)) {
169 errno = err; // can be EINVAL or ENOMEM
Jonathan Peyton62da5502017-11-29 22:29:38 +0000170 res = NULL;
Jonathan Peyton30419822017-05-12 18:01:32 +0000171 }
Jonathan Peytonf83ae312016-05-12 22:00:37 +0000172#endif
Jonathan Peyton62da5502017-11-29 22:29:38 +0000173 return res;
Jonathan Peytonf83ae312016-05-12 22:00:37 +0000174}
Jonathan Peyton30419822017-05-12 18:01:32 +0000175void *kmp_calloc(size_t nelem, size_t elsize) {
176 i;
Jonathan Peyton62da5502017-11-29 22:29:38 +0000177 void *res;
178#if KMP_OS_WINDOWS
179 res = _aligned_recalloc(NULL, nelem, elsize, 1);
180#else
181 res = calloc(nelem, elsize);
182#endif
183 return res;
Jonathan Peyton30419822017-05-12 18:01:32 +0000184}
185void *kmp_realloc(void *ptr, size_t size) {
186 i;
Jonathan Peyton62da5502017-11-29 22:29:38 +0000187 void *res;
188#if KMP_OS_WINDOWS
189 res = _aligned_realloc(ptr, size, 1);
190#else
191 res = realloc(ptr, size);
192#endif
193 return res;
Jonathan Peyton30419822017-05-12 18:01:32 +0000194}
195void kmp_free(void *ptr) {
196 i;
Jonathan Peyton62da5502017-11-29 22:29:38 +0000197#if KMP_OS_WINDOWS
198 _aligned_free(ptr);
199#else
Jonathan Peyton30419822017-05-12 18:01:32 +0000200 free(ptr);
Jonathan Peyton62da5502017-11-29 22:29:38 +0000201#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000202}
Jim Cownie5e8470a2013-09-27 10:38:44 +0000203
204static int __kmps_blocktime = INT_MAX;
205
Jonathan Peyton30419822017-05-12 18:01:32 +0000206void __kmps_set_blocktime(int arg) {
207 i;
208 __kmps_blocktime = arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000209} // __kmps_set_blocktime
210
Jonathan Peyton30419822017-05-12 18:01:32 +0000211int __kmps_get_blocktime(void) {
212 i;
213 return __kmps_blocktime;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000214} // __kmps_get_blocktime
215
216static int __kmps_dynamic = 0;
217
Jonathan Peyton30419822017-05-12 18:01:32 +0000218void __kmps_set_dynamic(int arg) {
219 i;
220 __kmps_dynamic = arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000221} // __kmps_set_dynamic
222
Jonathan Peyton30419822017-05-12 18:01:32 +0000223int __kmps_get_dynamic(void) {
224 i;
225 return __kmps_dynamic;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000226} // __kmps_get_dynamic
227
228static int __kmps_library = 1000;
229
Jonathan Peyton30419822017-05-12 18:01:32 +0000230void __kmps_set_library(int arg) {
231 i;
232 __kmps_library = arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000233} // __kmps_set_library
234
Jonathan Peyton30419822017-05-12 18:01:32 +0000235int __kmps_get_library(void) {
236 i;
237 return __kmps_library;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000238} // __kmps_get_library
239
240static int __kmps_nested = 0;
241
Jonathan Peyton30419822017-05-12 18:01:32 +0000242void __kmps_set_nested(int arg) {
243 i;
244 __kmps_nested = arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000245} // __kmps_set_nested
246
Jonathan Peyton30419822017-05-12 18:01:32 +0000247int __kmps_get_nested(void) {
248 i;
249 return __kmps_nested;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000250} // __kmps_get_nested
251
252static size_t __kmps_stacksize = KMP_DEFAULT_STKSIZE;
253
Jonathan Peyton30419822017-05-12 18:01:32 +0000254void __kmps_set_stacksize(int arg) {
255 i;
256 __kmps_stacksize = arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000257} // __kmps_set_stacksize
258
Jonathan Peyton30419822017-05-12 18:01:32 +0000259int __kmps_get_stacksize(void) {
260 i;
261 return __kmps_stacksize;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000262} // __kmps_get_stacksize
263
Jonathan Peyton30419822017-05-12 18:01:32 +0000264static kmp_sched_t __kmps_sched_kind = kmp_sched_default;
265static int __kmps_sched_modifier = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000266
Jonathan Peyton30419822017-05-12 18:01:32 +0000267void __kmps_set_schedule(kmp_sched_t kind, int modifier) {
268 i;
269 __kmps_sched_kind = kind;
270 __kmps_sched_modifier = modifier;
271} // __kmps_set_schedule
Jim Cownie5e8470a2013-09-27 10:38:44 +0000272
Jonathan Peyton30419822017-05-12 18:01:32 +0000273void __kmps_get_schedule(kmp_sched_t *kind, int *modifier) {
274 i;
275 *kind = __kmps_sched_kind;
276 *modifier = __kmps_sched_modifier;
277} // __kmps_get_schedule
Jim Cownie5e8470a2013-09-27 10:38:44 +0000278
Jim Cownie5e8470a2013-09-27 10:38:44 +0000279#if OMP_40_ENABLED
280
281static kmp_proc_bind_t __kmps_proc_bind = proc_bind_false;
282
Jonathan Peyton30419822017-05-12 18:01:32 +0000283void __kmps_set_proc_bind(kmp_proc_bind_t arg) {
284 i;
285 __kmps_proc_bind = arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000286} // __kmps_set_proc_bind
287
Jonathan Peyton30419822017-05-12 18:01:32 +0000288kmp_proc_bind_t __kmps_get_proc_bind(void) {
289 i;
290 return __kmps_proc_bind;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000291} // __kmps_get_proc_bind
292
293#endif /* OMP_40_ENABLED */
294
Jonathan Peyton30419822017-05-12 18:01:32 +0000295double __kmps_get_wtime(void) {
296 // Elapsed wall clock time (in second) from "sometime in the past".
297 double wtime = 0.0;
298 i;
299#if KMP_OS_WINDOWS
300 if (frequency > 0.0) {
301 LARGE_INTEGER now;
302 BOOL status = QueryPerformanceCounter(&now);
303 if (status) {
304 wtime = double(now.QuadPart) / frequency;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000305 }
306 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000307#else
308 // gettimeofday() returns seconds and microseconds since the Epoch.
309 struct timeval tval;
310 int rc;
311 rc = gettimeofday(&tval, NULL);
312 if (rc == 0) {
313 wtime = (double)(tval.tv_sec) + 1.0E-06 * (double)(tval.tv_usec);
314 } else {
315 // TODO: Assert or abort here.
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000316 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000317#endif
318 return wtime;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000319} // __kmps_get_wtime
Jim Cownie5e8470a2013-09-27 10:38:44 +0000320
Jonathan Peyton30419822017-05-12 18:01:32 +0000321double __kmps_get_wtick(void) {
322 // Number of seconds between successive clock ticks.
323 double wtick = 0.0;
324 i;
325#if KMP_OS_WINDOWS
326 {
327 DWORD increment;
328 DWORD adjustment;
329 BOOL disabled;
330 BOOL rc;
331 rc = GetSystemTimeAdjustment(&adjustment, &increment, &disabled);
332 if (rc) {
333 wtick = 1.0E-07 * (double)(disabled ? increment : adjustment);
334 } else {
335 // TODO: Assert or abort here.
336 wtick = 1.0E-03;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000337 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000338 }
339#else
340 // TODO: gettimeofday() returns in microseconds, but what the precision?
341 wtick = 1.0E-06;
342#endif
343 return wtick;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000344} // __kmps_get_wtick
Jim Cownie5e8470a2013-09-27 10:38:44 +0000345
Jonathan Peyton92ca6182018-09-07 18:25:49 +0000346#if OMP_50_ENABLED
347/* OpenMP 5.0 Memory Management */
348const omp_allocator_t *OMP_NULL_ALLOCATOR = NULL;
349const omp_allocator_t *omp_default_mem_alloc = (const omp_allocator_t *)1;
350const omp_allocator_t *omp_large_cap_mem_alloc = (const omp_allocator_t *)2;
351const omp_allocator_t *omp_const_mem_alloc = (const omp_allocator_t *)3;
352const omp_allocator_t *omp_high_bw_mem_alloc = (const omp_allocator_t *)4;
353const omp_allocator_t *omp_low_lat_mem_alloc = (const omp_allocator_t *)5;
354const omp_allocator_t *omp_cgroup_mem_alloc = (const omp_allocator_t *)6;
355const omp_allocator_t *omp_pteam_mem_alloc = (const omp_allocator_t *)7;
356const omp_allocator_t *omp_thread_mem_alloc = (const omp_allocator_t *)8;
Jonathan Peyton6d88e042018-12-13 23:14:24 +0000357/* OpenMP 5.0 Affinity Format */
358void omp_set_affinity_format(char const *format) { i; }
359size_t omp_get_affinity_format(char *buffer, size_t size) {
360 i;
361 return 0;
362}
363void omp_display_affinity(char const *format) { i; }
364size_t omp_capture_affinity(char *buffer, size_t buf_size, char const *format) {
365 i;
366 return 0;
367}
Jonathan Peyton92ca6182018-09-07 18:25:49 +0000368#endif /* OMP_50_ENABLED */
369
Jim Cownie5e8470a2013-09-27 10:38:44 +0000370// end of file //