blob: c704e61dd0181b14d9f9344fd8dc8cc76893d9b9 [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 Peyton30419822017-05-12 18:01:32 +000018#include "kmp.h" // KMP_DEFAULT_STKSIZE
Jim Cownie4cc4bb42014-10-07 16:25:50 +000019#include "kmp_stub.h"
Jonathan Peyton30419822017-05-12 18:01:32 +000020#include "omp.h" // Function renamings.
Jim Cownie5e8470a2013-09-27 10:38:44 +000021
22#if KMP_OS_WINDOWS
Jonathan Peyton30419822017-05-12 18:01:32 +000023#include <windows.h>
Jim Cownie5e8470a2013-09-27 10:38:44 +000024#else
Jonathan Peyton30419822017-05-12 18:01:32 +000025#include <sys/time.h>
Jim Cownie5e8470a2013-09-27 10:38:44 +000026#endif
27
Jim Cownie181b4bb2013-12-23 17:28:57 +000028// Moved from omp.h
Jonathan Peyton30419822017-05-12 18:01:32 +000029#define omp_set_max_active_levels ompc_set_max_active_levels
30#define omp_set_schedule ompc_set_schedule
31#define omp_get_ancestor_thread_num ompc_get_ancestor_thread_num
32#define omp_get_team_size ompc_get_team_size
Jim Cownie181b4bb2013-12-23 17:28:57 +000033
Jonathan Peyton30419822017-05-12 18:01:32 +000034#define omp_set_num_threads ompc_set_num_threads
35#define omp_set_dynamic ompc_set_dynamic
36#define omp_set_nested ompc_set_nested
37#define kmp_set_stacksize kmpc_set_stacksize
38#define kmp_set_stacksize_s kmpc_set_stacksize_s
39#define kmp_set_blocktime kmpc_set_blocktime
40#define kmp_set_library kmpc_set_library
41#define kmp_set_defaults kmpc_set_defaults
42#define kmp_set_disp_num_buffers kmpc_set_disp_num_buffers
43#define kmp_malloc kmpc_malloc
44#define kmp_aligned_malloc kmpc_aligned_malloc
45#define kmp_calloc kmpc_calloc
46#define kmp_realloc kmpc_realloc
47#define kmp_free kmpc_free
Jim Cownie5e8470a2013-09-27 10:38:44 +000048
49static double frequency = 0.0;
50
51// Helper functions.
52static size_t __kmps_init() {
Jonathan Peyton30419822017-05-12 18:01:32 +000053 static int initialized = 0;
54 static size_t dummy = 0;
55 if (!initialized) {
56 // TODO: Analyze KMP_VERSION environment variable, print
57 // __kmp_version_copyright and __kmp_version_build_time.
58 // WARNING: Do not use "fprintf(stderr, ...)" because it will cause
59 // unresolved "__iob" symbol (see C70080). We need to extract __kmp_printf()
60 // stuff from kmp_runtime.cpp and use it.
Jim Cownie5e8470a2013-09-27 10:38:44 +000061
Jonathan Peyton30419822017-05-12 18:01:32 +000062 // Trick with dummy variable forces linker to keep __kmp_version_copyright
63 // and __kmp_version_build_time strings in executable file (in case of
64 // static linkage). When KMP_VERSION analysis is implemented, dummy
65 // variable should be deleted, function should return void.
66 dummy = __kmp_version_copyright - __kmp_version_build_time;
Jim Cownie5e8470a2013-09-27 10:38:44 +000067
Jonathan Peyton30419822017-05-12 18:01:32 +000068#if KMP_OS_WINDOWS
69 LARGE_INTEGER freq;
70 BOOL status = QueryPerformanceFrequency(&freq);
71 if (status) {
72 frequency = double(freq.QuadPart);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +000073 }
Jonathan Peyton30419822017-05-12 18:01:32 +000074#endif
75
76 initialized = 1;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +000077 }
Jonathan Peyton30419822017-05-12 18:01:32 +000078 return dummy;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +000079} // __kmps_init
Jim Cownie5e8470a2013-09-27 10:38:44 +000080
81#define i __kmps_init();
82
83/* set API functions */
Jonathan Peyton30419822017-05-12 18:01:32 +000084void omp_set_num_threads(omp_int_t num_threads) { i; }
85void omp_set_dynamic(omp_int_t dynamic) {
86 i;
87 __kmps_set_dynamic(dynamic);
88}
89void omp_set_nested(omp_int_t nested) {
90 i;
91 __kmps_set_nested(nested);
92}
93void omp_set_max_active_levels(omp_int_t max_active_levels) { i; }
94void omp_set_schedule(omp_sched_t kind, omp_int_t modifier) {
95 i;
96 __kmps_set_schedule((kmp_sched_t)kind, modifier);
97}
98int omp_get_ancestor_thread_num(omp_int_t level) {
99 i;
100 return (level) ? (-1) : (0);
101}
102int omp_get_team_size(omp_int_t level) {
103 i;
104 return (level) ? (-1) : (1);
105}
106int kmpc_set_affinity_mask_proc(int proc, void **mask) {
107 i;
108 return -1;
109}
110int kmpc_unset_affinity_mask_proc(int proc, void **mask) {
111 i;
112 return -1;
113}
114int kmpc_get_affinity_mask_proc(int proc, void **mask) {
115 i;
116 return -1;
117}
Jim Cownie5e8470a2013-09-27 10:38:44 +0000118
119/* kmp API functions */
Jonathan Peyton30419822017-05-12 18:01:32 +0000120void kmp_set_stacksize(omp_int_t arg) {
121 i;
122 __kmps_set_stacksize(arg);
123}
124void kmp_set_stacksize_s(size_t arg) {
125 i;
126 __kmps_set_stacksize(arg);
127}
128void kmp_set_blocktime(omp_int_t arg) {
129 i;
130 __kmps_set_blocktime(arg);
131}
132void kmp_set_library(omp_int_t arg) {
133 i;
134 __kmps_set_library(arg);
135}
136void kmp_set_defaults(char const *str) { i; }
137void kmp_set_disp_num_buffers(omp_int_t arg) { i; }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000138
139/* KMP memory management functions. */
Jonathan Peyton30419822017-05-12 18:01:32 +0000140void *kmp_malloc(size_t size) {
141 i;
142 return malloc(size);
143}
144void *kmp_aligned_malloc(size_t sz, size_t a) {
145 i;
Jonathan Peytonf83ae312016-05-12 22:00:37 +0000146#if KMP_OS_WINDOWS
Jonathan Peyton30419822017-05-12 18:01:32 +0000147 errno = ENOSYS; // not supported
148 return NULL; // no standard aligned allocator on Windows (pre - C11)
Jonathan Peytonf83ae312016-05-12 22:00:37 +0000149#else
Jonathan Peyton30419822017-05-12 18:01:32 +0000150 void *res;
151 int err;
152 if (err = posix_memalign(&res, a, sz)) {
153 errno = err; // can be EINVAL or ENOMEM
154 return NULL;
155 }
156 return res;
Jonathan Peytonf83ae312016-05-12 22:00:37 +0000157#endif
158}
Jonathan Peyton30419822017-05-12 18:01:32 +0000159void *kmp_calloc(size_t nelem, size_t elsize) {
160 i;
161 return calloc(nelem, elsize);
162}
163void *kmp_realloc(void *ptr, size_t size) {
164 i;
165 return realloc(ptr, size);
166}
167void kmp_free(void *ptr) {
168 i;
169 free(ptr);
170}
Jim Cownie5e8470a2013-09-27 10:38:44 +0000171
172static int __kmps_blocktime = INT_MAX;
173
Jonathan Peyton30419822017-05-12 18:01:32 +0000174void __kmps_set_blocktime(int arg) {
175 i;
176 __kmps_blocktime = arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000177} // __kmps_set_blocktime
178
Jonathan Peyton30419822017-05-12 18:01:32 +0000179int __kmps_get_blocktime(void) {
180 i;
181 return __kmps_blocktime;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000182} // __kmps_get_blocktime
183
184static int __kmps_dynamic = 0;
185
Jonathan Peyton30419822017-05-12 18:01:32 +0000186void __kmps_set_dynamic(int arg) {
187 i;
188 __kmps_dynamic = arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000189} // __kmps_set_dynamic
190
Jonathan Peyton30419822017-05-12 18:01:32 +0000191int __kmps_get_dynamic(void) {
192 i;
193 return __kmps_dynamic;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000194} // __kmps_get_dynamic
195
196static int __kmps_library = 1000;
197
Jonathan Peyton30419822017-05-12 18:01:32 +0000198void __kmps_set_library(int arg) {
199 i;
200 __kmps_library = arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000201} // __kmps_set_library
202
Jonathan Peyton30419822017-05-12 18:01:32 +0000203int __kmps_get_library(void) {
204 i;
205 return __kmps_library;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000206} // __kmps_get_library
207
208static int __kmps_nested = 0;
209
Jonathan Peyton30419822017-05-12 18:01:32 +0000210void __kmps_set_nested(int arg) {
211 i;
212 __kmps_nested = arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000213} // __kmps_set_nested
214
Jonathan Peyton30419822017-05-12 18:01:32 +0000215int __kmps_get_nested(void) {
216 i;
217 return __kmps_nested;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000218} // __kmps_get_nested
219
220static size_t __kmps_stacksize = KMP_DEFAULT_STKSIZE;
221
Jonathan Peyton30419822017-05-12 18:01:32 +0000222void __kmps_set_stacksize(int arg) {
223 i;
224 __kmps_stacksize = arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000225} // __kmps_set_stacksize
226
Jonathan Peyton30419822017-05-12 18:01:32 +0000227int __kmps_get_stacksize(void) {
228 i;
229 return __kmps_stacksize;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000230} // __kmps_get_stacksize
231
Jonathan Peyton30419822017-05-12 18:01:32 +0000232static kmp_sched_t __kmps_sched_kind = kmp_sched_default;
233static int __kmps_sched_modifier = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000234
Jonathan Peyton30419822017-05-12 18:01:32 +0000235void __kmps_set_schedule(kmp_sched_t kind, int modifier) {
236 i;
237 __kmps_sched_kind = kind;
238 __kmps_sched_modifier = modifier;
239} // __kmps_set_schedule
Jim Cownie5e8470a2013-09-27 10:38:44 +0000240
Jonathan Peyton30419822017-05-12 18:01:32 +0000241void __kmps_get_schedule(kmp_sched_t *kind, int *modifier) {
242 i;
243 *kind = __kmps_sched_kind;
244 *modifier = __kmps_sched_modifier;
245} // __kmps_get_schedule
Jim Cownie5e8470a2013-09-27 10:38:44 +0000246
Jim Cownie5e8470a2013-09-27 10:38:44 +0000247#if OMP_40_ENABLED
248
249static kmp_proc_bind_t __kmps_proc_bind = proc_bind_false;
250
Jonathan Peyton30419822017-05-12 18:01:32 +0000251void __kmps_set_proc_bind(kmp_proc_bind_t arg) {
252 i;
253 __kmps_proc_bind = arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000254} // __kmps_set_proc_bind
255
Jonathan Peyton30419822017-05-12 18:01:32 +0000256kmp_proc_bind_t __kmps_get_proc_bind(void) {
257 i;
258 return __kmps_proc_bind;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000259} // __kmps_get_proc_bind
260
261#endif /* OMP_40_ENABLED */
262
Jonathan Peyton30419822017-05-12 18:01:32 +0000263double __kmps_get_wtime(void) {
264 // Elapsed wall clock time (in second) from "sometime in the past".
265 double wtime = 0.0;
266 i;
267#if KMP_OS_WINDOWS
268 if (frequency > 0.0) {
269 LARGE_INTEGER now;
270 BOOL status = QueryPerformanceCounter(&now);
271 if (status) {
272 wtime = double(now.QuadPart) / frequency;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000273 }
274 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000275#else
276 // gettimeofday() returns seconds and microseconds since the Epoch.
277 struct timeval tval;
278 int rc;
279 rc = gettimeofday(&tval, NULL);
280 if (rc == 0) {
281 wtime = (double)(tval.tv_sec) + 1.0E-06 * (double)(tval.tv_usec);
282 } else {
283 // TODO: Assert or abort here.
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000284 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000285#endif
286 return wtime;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000287} // __kmps_get_wtime
Jim Cownie5e8470a2013-09-27 10:38:44 +0000288
Jonathan Peyton30419822017-05-12 18:01:32 +0000289double __kmps_get_wtick(void) {
290 // Number of seconds between successive clock ticks.
291 double wtick = 0.0;
292 i;
293#if KMP_OS_WINDOWS
294 {
295 DWORD increment;
296 DWORD adjustment;
297 BOOL disabled;
298 BOOL rc;
299 rc = GetSystemTimeAdjustment(&adjustment, &increment, &disabled);
300 if (rc) {
301 wtick = 1.0E-07 * (double)(disabled ? increment : adjustment);
302 } else {
303 // TODO: Assert or abort here.
304 wtick = 1.0E-03;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000305 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000306 }
307#else
308 // TODO: gettimeofday() returns in microseconds, but what the precision?
309 wtick = 1.0E-06;
310#endif
311 return wtick;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000312} // __kmps_get_wtick
Jim Cownie5e8470a2013-09-27 10:38:44 +0000313
Jim Cownie5e8470a2013-09-27 10:38:44 +0000314// end of file //