blob: 4051d9e91db874fc8cbc6c0a7fe65c305e8cf0b3 [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
Andrey Churbanov75bc70f2018-03-05 18:01:47 +000018#include "omp.h" // omp_* declarations, must be included before "kmp.h"
Jonathan Peyton30419822017-05-12 18:01:32 +000019#include "kmp.h" // KMP_DEFAULT_STKSIZE
Jim Cownie4cc4bb42014-10-07 16:25:50 +000020#include "kmp_stub.h"
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;
Jonathan Peyton62da5502017-11-29 22:29:38 +0000142 void *res;
143#if KMP_OS_WINDOWS
144 // If succesfull returns a pointer to the memory block, otherwise returns
145 // NULL.
146 // Sets errno to ENOMEM or EINVAL if memory allocation failed or parameter
147 // validation failed.
148 res = _aligned_malloc(size, 1);
149#else
150 res = malloc(size);
151#endif
152 return res;
Jonathan Peyton30419822017-05-12 18:01:32 +0000153}
154void *kmp_aligned_malloc(size_t sz, size_t a) {
155 i;
Jonathan Peyton30419822017-05-12 18:01:32 +0000156 int err;
Jonathan Peyton62da5502017-11-29 22:29:38 +0000157 void *res;
158#if KMP_OS_WINDOWS
159 res = _aligned_malloc(sz, a);
160#else
Jonathan Peyton30419822017-05-12 18:01:32 +0000161 if (err = posix_memalign(&res, a, sz)) {
162 errno = err; // can be EINVAL or ENOMEM
Jonathan Peyton62da5502017-11-29 22:29:38 +0000163 res = NULL;
Jonathan Peyton30419822017-05-12 18:01:32 +0000164 }
Jonathan Peytonf83ae312016-05-12 22:00:37 +0000165#endif
Jonathan Peyton62da5502017-11-29 22:29:38 +0000166 return res;
Jonathan Peytonf83ae312016-05-12 22:00:37 +0000167}
Jonathan Peyton30419822017-05-12 18:01:32 +0000168void *kmp_calloc(size_t nelem, size_t elsize) {
169 i;
Jonathan Peyton62da5502017-11-29 22:29:38 +0000170 void *res;
171#if KMP_OS_WINDOWS
172 res = _aligned_recalloc(NULL, nelem, elsize, 1);
173#else
174 res = calloc(nelem, elsize);
175#endif
176 return res;
Jonathan Peyton30419822017-05-12 18:01:32 +0000177}
178void *kmp_realloc(void *ptr, size_t size) {
179 i;
Jonathan Peyton62da5502017-11-29 22:29:38 +0000180 void *res;
181#if KMP_OS_WINDOWS
182 res = _aligned_realloc(ptr, size, 1);
183#else
184 res = realloc(ptr, size);
185#endif
186 return res;
Jonathan Peyton30419822017-05-12 18:01:32 +0000187}
188void kmp_free(void *ptr) {
189 i;
Jonathan Peyton62da5502017-11-29 22:29:38 +0000190#if KMP_OS_WINDOWS
191 _aligned_free(ptr);
192#else
Jonathan Peyton30419822017-05-12 18:01:32 +0000193 free(ptr);
Jonathan Peyton62da5502017-11-29 22:29:38 +0000194#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000195}
Jim Cownie5e8470a2013-09-27 10:38:44 +0000196
197static int __kmps_blocktime = INT_MAX;
198
Jonathan Peyton30419822017-05-12 18:01:32 +0000199void __kmps_set_blocktime(int arg) {
200 i;
201 __kmps_blocktime = arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000202} // __kmps_set_blocktime
203
Jonathan Peyton30419822017-05-12 18:01:32 +0000204int __kmps_get_blocktime(void) {
205 i;
206 return __kmps_blocktime;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000207} // __kmps_get_blocktime
208
209static int __kmps_dynamic = 0;
210
Jonathan Peyton30419822017-05-12 18:01:32 +0000211void __kmps_set_dynamic(int arg) {
212 i;
213 __kmps_dynamic = arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000214} // __kmps_set_dynamic
215
Jonathan Peyton30419822017-05-12 18:01:32 +0000216int __kmps_get_dynamic(void) {
217 i;
218 return __kmps_dynamic;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000219} // __kmps_get_dynamic
220
221static int __kmps_library = 1000;
222
Jonathan Peyton30419822017-05-12 18:01:32 +0000223void __kmps_set_library(int arg) {
224 i;
225 __kmps_library = arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000226} // __kmps_set_library
227
Jonathan Peyton30419822017-05-12 18:01:32 +0000228int __kmps_get_library(void) {
229 i;
230 return __kmps_library;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000231} // __kmps_get_library
232
233static int __kmps_nested = 0;
234
Jonathan Peyton30419822017-05-12 18:01:32 +0000235void __kmps_set_nested(int arg) {
236 i;
237 __kmps_nested = arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000238} // __kmps_set_nested
239
Jonathan Peyton30419822017-05-12 18:01:32 +0000240int __kmps_get_nested(void) {
241 i;
242 return __kmps_nested;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000243} // __kmps_get_nested
244
245static size_t __kmps_stacksize = KMP_DEFAULT_STKSIZE;
246
Jonathan Peyton30419822017-05-12 18:01:32 +0000247void __kmps_set_stacksize(int arg) {
248 i;
249 __kmps_stacksize = arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000250} // __kmps_set_stacksize
251
Jonathan Peyton30419822017-05-12 18:01:32 +0000252int __kmps_get_stacksize(void) {
253 i;
254 return __kmps_stacksize;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000255} // __kmps_get_stacksize
256
Jonathan Peyton30419822017-05-12 18:01:32 +0000257static kmp_sched_t __kmps_sched_kind = kmp_sched_default;
258static int __kmps_sched_modifier = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000259
Jonathan Peyton30419822017-05-12 18:01:32 +0000260void __kmps_set_schedule(kmp_sched_t kind, int modifier) {
261 i;
262 __kmps_sched_kind = kind;
263 __kmps_sched_modifier = modifier;
264} // __kmps_set_schedule
Jim Cownie5e8470a2013-09-27 10:38:44 +0000265
Jonathan Peyton30419822017-05-12 18:01:32 +0000266void __kmps_get_schedule(kmp_sched_t *kind, int *modifier) {
267 i;
268 *kind = __kmps_sched_kind;
269 *modifier = __kmps_sched_modifier;
270} // __kmps_get_schedule
Jim Cownie5e8470a2013-09-27 10:38:44 +0000271
Jim Cownie5e8470a2013-09-27 10:38:44 +0000272#if OMP_40_ENABLED
273
274static kmp_proc_bind_t __kmps_proc_bind = proc_bind_false;
275
Jonathan Peyton30419822017-05-12 18:01:32 +0000276void __kmps_set_proc_bind(kmp_proc_bind_t arg) {
277 i;
278 __kmps_proc_bind = arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000279} // __kmps_set_proc_bind
280
Jonathan Peyton30419822017-05-12 18:01:32 +0000281kmp_proc_bind_t __kmps_get_proc_bind(void) {
282 i;
283 return __kmps_proc_bind;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000284} // __kmps_get_proc_bind
285
286#endif /* OMP_40_ENABLED */
287
Jonathan Peyton30419822017-05-12 18:01:32 +0000288double __kmps_get_wtime(void) {
289 // Elapsed wall clock time (in second) from "sometime in the past".
290 double wtime = 0.0;
291 i;
292#if KMP_OS_WINDOWS
293 if (frequency > 0.0) {
294 LARGE_INTEGER now;
295 BOOL status = QueryPerformanceCounter(&now);
296 if (status) {
297 wtime = double(now.QuadPart) / frequency;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000298 }
299 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000300#else
301 // gettimeofday() returns seconds and microseconds since the Epoch.
302 struct timeval tval;
303 int rc;
304 rc = gettimeofday(&tval, NULL);
305 if (rc == 0) {
306 wtime = (double)(tval.tv_sec) + 1.0E-06 * (double)(tval.tv_usec);
307 } else {
308 // TODO: Assert or abort here.
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000309 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000310#endif
311 return wtime;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000312} // __kmps_get_wtime
Jim Cownie5e8470a2013-09-27 10:38:44 +0000313
Jonathan Peyton30419822017-05-12 18:01:32 +0000314double __kmps_get_wtick(void) {
315 // Number of seconds between successive clock ticks.
316 double wtick = 0.0;
317 i;
318#if KMP_OS_WINDOWS
319 {
320 DWORD increment;
321 DWORD adjustment;
322 BOOL disabled;
323 BOOL rc;
324 rc = GetSystemTimeAdjustment(&adjustment, &increment, &disabled);
325 if (rc) {
326 wtick = 1.0E-07 * (double)(disabled ? increment : adjustment);
327 } else {
328 // TODO: Assert or abort here.
329 wtick = 1.0E-03;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000330 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000331 }
332#else
333 // TODO: gettimeofday() returns in microseconds, but what the precision?
334 wtick = 1.0E-06;
335#endif
336 return wtick;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000337} // __kmps_get_wtick
Jim Cownie5e8470a2013-09-27 10:38:44 +0000338
Jim Cownie5e8470a2013-09-27 10:38:44 +0000339// end of file //