blob: 6f922bad3845b6e41a2b694d96f1f4a5890e1fbf [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
5
6//===----------------------------------------------------------------------===//
7//
8// The LLVM Compiler Infrastructure
9//
10// This file is dual licensed under the MIT and the University of Illinois Open
11// Source Licenses. See LICENSE.txt for details.
12//
13//===----------------------------------------------------------------------===//
14
15
Jim Cownie5e8470a2013-09-27 10:38:44 +000016#include <errno.h>
Jonathan Peyton30419822017-05-12 18:01:32 +000017#include <limits.h>
18#include <stdlib.h>
Jim Cownie5e8470a2013-09-27 10:38:44 +000019
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"
Jonathan Peyton30419822017-05-12 18:01:32 +000022#include "omp.h" // Function renamings.
Jim Cownie5e8470a2013-09-27 10:38:44 +000023
24#if KMP_OS_WINDOWS
Jonathan Peyton30419822017-05-12 18:01:32 +000025#include <windows.h>
Jim Cownie5e8470a2013-09-27 10:38:44 +000026#else
Jonathan Peyton30419822017-05-12 18:01:32 +000027#include <sys/time.h>
Jim Cownie5e8470a2013-09-27 10:38:44 +000028#endif
29
Jim Cownie181b4bb2013-12-23 17:28:57 +000030// Moved from omp.h
Jonathan Peyton30419822017-05-12 18:01:32 +000031#define omp_set_max_active_levels ompc_set_max_active_levels
32#define omp_set_schedule ompc_set_schedule
33#define omp_get_ancestor_thread_num ompc_get_ancestor_thread_num
34#define omp_get_team_size ompc_get_team_size
Jim Cownie181b4bb2013-12-23 17:28:57 +000035
Jonathan Peyton30419822017-05-12 18:01:32 +000036#define omp_set_num_threads ompc_set_num_threads
37#define omp_set_dynamic ompc_set_dynamic
38#define omp_set_nested ompc_set_nested
39#define kmp_set_stacksize kmpc_set_stacksize
40#define kmp_set_stacksize_s kmpc_set_stacksize_s
41#define kmp_set_blocktime kmpc_set_blocktime
42#define kmp_set_library kmpc_set_library
43#define kmp_set_defaults kmpc_set_defaults
44#define kmp_set_disp_num_buffers kmpc_set_disp_num_buffers
45#define kmp_malloc kmpc_malloc
46#define kmp_aligned_malloc kmpc_aligned_malloc
47#define kmp_calloc kmpc_calloc
48#define kmp_realloc kmpc_realloc
49#define kmp_free kmpc_free
Jim Cownie5e8470a2013-09-27 10:38:44 +000050
51static double frequency = 0.0;
52
53// Helper functions.
54static size_t __kmps_init() {
Jonathan Peyton30419822017-05-12 18:01:32 +000055 static int initialized = 0;
56 static size_t dummy = 0;
57 if (!initialized) {
58 // TODO: Analyze KMP_VERSION environment variable, print
59 // __kmp_version_copyright and __kmp_version_build_time.
60 // WARNING: Do not use "fprintf(stderr, ...)" because it will cause
61 // unresolved "__iob" symbol (see C70080). We need to extract __kmp_printf()
62 // stuff from kmp_runtime.cpp and use it.
Jim Cownie5e8470a2013-09-27 10:38:44 +000063
Jonathan Peyton30419822017-05-12 18:01:32 +000064 // Trick with dummy variable forces linker to keep __kmp_version_copyright
65 // and __kmp_version_build_time strings in executable file (in case of
66 // static linkage). When KMP_VERSION analysis is implemented, dummy
67 // variable should be deleted, function should return void.
68 dummy = __kmp_version_copyright - __kmp_version_build_time;
Jim Cownie5e8470a2013-09-27 10:38:44 +000069
Jonathan Peyton30419822017-05-12 18:01:32 +000070#if KMP_OS_WINDOWS
71 LARGE_INTEGER freq;
72 BOOL status = QueryPerformanceFrequency(&freq);
73 if (status) {
74 frequency = double(freq.QuadPart);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +000075 }
Jonathan Peyton30419822017-05-12 18:01:32 +000076#endif
77
78 initialized = 1;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +000079 }
Jonathan Peyton30419822017-05-12 18:01:32 +000080 return dummy;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +000081} // __kmps_init
Jim Cownie5e8470a2013-09-27 10:38:44 +000082
83#define i __kmps_init();
84
85/* set API functions */
Jonathan Peyton30419822017-05-12 18:01:32 +000086void omp_set_num_threads(omp_int_t num_threads) { i; }
87void omp_set_dynamic(omp_int_t dynamic) {
88 i;
89 __kmps_set_dynamic(dynamic);
90}
91void omp_set_nested(omp_int_t nested) {
92 i;
93 __kmps_set_nested(nested);
94}
95void omp_set_max_active_levels(omp_int_t max_active_levels) { i; }
96void omp_set_schedule(omp_sched_t kind, omp_int_t modifier) {
97 i;
98 __kmps_set_schedule((kmp_sched_t)kind, modifier);
99}
100int omp_get_ancestor_thread_num(omp_int_t level) {
101 i;
102 return (level) ? (-1) : (0);
103}
104int omp_get_team_size(omp_int_t level) {
105 i;
106 return (level) ? (-1) : (1);
107}
108int kmpc_set_affinity_mask_proc(int proc, void **mask) {
109 i;
110 return -1;
111}
112int kmpc_unset_affinity_mask_proc(int proc, void **mask) {
113 i;
114 return -1;
115}
116int kmpc_get_affinity_mask_proc(int proc, void **mask) {
117 i;
118 return -1;
119}
Jim Cownie5e8470a2013-09-27 10:38:44 +0000120
121/* kmp API functions */
Jonathan Peyton30419822017-05-12 18:01:32 +0000122void kmp_set_stacksize(omp_int_t arg) {
123 i;
124 __kmps_set_stacksize(arg);
125}
126void kmp_set_stacksize_s(size_t arg) {
127 i;
128 __kmps_set_stacksize(arg);
129}
130void kmp_set_blocktime(omp_int_t arg) {
131 i;
132 __kmps_set_blocktime(arg);
133}
134void kmp_set_library(omp_int_t arg) {
135 i;
136 __kmps_set_library(arg);
137}
138void kmp_set_defaults(char const *str) { i; }
139void kmp_set_disp_num_buffers(omp_int_t arg) { i; }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000140
141/* KMP memory management functions. */
Jonathan Peyton30419822017-05-12 18:01:32 +0000142void *kmp_malloc(size_t size) {
143 i;
144 return malloc(size);
145}
146void *kmp_aligned_malloc(size_t sz, size_t a) {
147 i;
Jonathan Peytonf83ae312016-05-12 22:00:37 +0000148#if KMP_OS_WINDOWS
Jonathan Peyton30419822017-05-12 18:01:32 +0000149 errno = ENOSYS; // not supported
150 return NULL; // no standard aligned allocator on Windows (pre - C11)
Jonathan Peytonf83ae312016-05-12 22:00:37 +0000151#else
Jonathan Peyton30419822017-05-12 18:01:32 +0000152 void *res;
153 int err;
154 if (err = posix_memalign(&res, a, sz)) {
155 errno = err; // can be EINVAL or ENOMEM
156 return NULL;
157 }
158 return res;
Jonathan Peytonf83ae312016-05-12 22:00:37 +0000159#endif
160}
Jonathan Peyton30419822017-05-12 18:01:32 +0000161void *kmp_calloc(size_t nelem, size_t elsize) {
162 i;
163 return calloc(nelem, elsize);
164}
165void *kmp_realloc(void *ptr, size_t size) {
166 i;
167 return realloc(ptr, size);
168}
169void kmp_free(void *ptr) {
170 i;
171 free(ptr);
172}
Jim Cownie5e8470a2013-09-27 10:38:44 +0000173
174static int __kmps_blocktime = INT_MAX;
175
Jonathan Peyton30419822017-05-12 18:01:32 +0000176void __kmps_set_blocktime(int arg) {
177 i;
178 __kmps_blocktime = arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000179} // __kmps_set_blocktime
180
Jonathan Peyton30419822017-05-12 18:01:32 +0000181int __kmps_get_blocktime(void) {
182 i;
183 return __kmps_blocktime;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000184} // __kmps_get_blocktime
185
186static int __kmps_dynamic = 0;
187
Jonathan Peyton30419822017-05-12 18:01:32 +0000188void __kmps_set_dynamic(int arg) {
189 i;
190 __kmps_dynamic = arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000191} // __kmps_set_dynamic
192
Jonathan Peyton30419822017-05-12 18:01:32 +0000193int __kmps_get_dynamic(void) {
194 i;
195 return __kmps_dynamic;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000196} // __kmps_get_dynamic
197
198static int __kmps_library = 1000;
199
Jonathan Peyton30419822017-05-12 18:01:32 +0000200void __kmps_set_library(int arg) {
201 i;
202 __kmps_library = arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000203} // __kmps_set_library
204
Jonathan Peyton30419822017-05-12 18:01:32 +0000205int __kmps_get_library(void) {
206 i;
207 return __kmps_library;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000208} // __kmps_get_library
209
210static int __kmps_nested = 0;
211
Jonathan Peyton30419822017-05-12 18:01:32 +0000212void __kmps_set_nested(int arg) {
213 i;
214 __kmps_nested = arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000215} // __kmps_set_nested
216
Jonathan Peyton30419822017-05-12 18:01:32 +0000217int __kmps_get_nested(void) {
218 i;
219 return __kmps_nested;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000220} // __kmps_get_nested
221
222static size_t __kmps_stacksize = KMP_DEFAULT_STKSIZE;
223
Jonathan Peyton30419822017-05-12 18:01:32 +0000224void __kmps_set_stacksize(int arg) {
225 i;
226 __kmps_stacksize = arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000227} // __kmps_set_stacksize
228
Jonathan Peyton30419822017-05-12 18:01:32 +0000229int __kmps_get_stacksize(void) {
230 i;
231 return __kmps_stacksize;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000232} // __kmps_get_stacksize
233
Jonathan Peyton30419822017-05-12 18:01:32 +0000234static kmp_sched_t __kmps_sched_kind = kmp_sched_default;
235static int __kmps_sched_modifier = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000236
Jonathan Peyton30419822017-05-12 18:01:32 +0000237void __kmps_set_schedule(kmp_sched_t kind, int modifier) {
238 i;
239 __kmps_sched_kind = kind;
240 __kmps_sched_modifier = modifier;
241} // __kmps_set_schedule
Jim Cownie5e8470a2013-09-27 10:38:44 +0000242
Jonathan Peyton30419822017-05-12 18:01:32 +0000243void __kmps_get_schedule(kmp_sched_t *kind, int *modifier) {
244 i;
245 *kind = __kmps_sched_kind;
246 *modifier = __kmps_sched_modifier;
247} // __kmps_get_schedule
Jim Cownie5e8470a2013-09-27 10:38:44 +0000248
Jim Cownie5e8470a2013-09-27 10:38:44 +0000249#if OMP_40_ENABLED
250
251static kmp_proc_bind_t __kmps_proc_bind = proc_bind_false;
252
Jonathan Peyton30419822017-05-12 18:01:32 +0000253void __kmps_set_proc_bind(kmp_proc_bind_t arg) {
254 i;
255 __kmps_proc_bind = arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000256} // __kmps_set_proc_bind
257
Jonathan Peyton30419822017-05-12 18:01:32 +0000258kmp_proc_bind_t __kmps_get_proc_bind(void) {
259 i;
260 return __kmps_proc_bind;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000261} // __kmps_get_proc_bind
262
263#endif /* OMP_40_ENABLED */
264
Jonathan Peyton30419822017-05-12 18:01:32 +0000265double __kmps_get_wtime(void) {
266 // Elapsed wall clock time (in second) from "sometime in the past".
267 double wtime = 0.0;
268 i;
269#if KMP_OS_WINDOWS
270 if (frequency > 0.0) {
271 LARGE_INTEGER now;
272 BOOL status = QueryPerformanceCounter(&now);
273 if (status) {
274 wtime = double(now.QuadPart) / frequency;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000275 }
276 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000277#else
278 // gettimeofday() returns seconds and microseconds since the Epoch.
279 struct timeval tval;
280 int rc;
281 rc = gettimeofday(&tval, NULL);
282 if (rc == 0) {
283 wtime = (double)(tval.tv_sec) + 1.0E-06 * (double)(tval.tv_usec);
284 } else {
285 // TODO: Assert or abort here.
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000286 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000287#endif
288 return wtime;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000289} // __kmps_get_wtime
Jim Cownie5e8470a2013-09-27 10:38:44 +0000290
Jonathan Peyton30419822017-05-12 18:01:32 +0000291double __kmps_get_wtick(void) {
292 // Number of seconds between successive clock ticks.
293 double wtick = 0.0;
294 i;
295#if KMP_OS_WINDOWS
296 {
297 DWORD increment;
298 DWORD adjustment;
299 BOOL disabled;
300 BOOL rc;
301 rc = GetSystemTimeAdjustment(&adjustment, &increment, &disabled);
302 if (rc) {
303 wtick = 1.0E-07 * (double)(disabled ? increment : adjustment);
304 } else {
305 // TODO: Assert or abort here.
306 wtick = 1.0E-03;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000307 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000308 }
309#else
310 // TODO: gettimeofday() returns in microseconds, but what the precision?
311 wtick = 1.0E-06;
312#endif
313 return wtick;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000314} // __kmps_get_wtick
Jim Cownie5e8470a2013-09-27 10:38:44 +0000315
Jim Cownie5e8470a2013-09-27 10:38:44 +0000316// end of file //