Intel® OpenMP* Runtime Library
kmp_stub.c
1 /*
2  * kmp_stub.c -- stub versions of user-callable OpenMP RT functions.
3  */
4 
5 /* <copyright>
6  Copyright (c) 1997-2015 Intel Corporation. All Rights Reserved.
7 
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions
10  are met:
11 
12  * Redistributions of source code must retain the above copyright
13  notice, this list of conditions and the following disclaimer.
14  * Redistributions in binary form must reproduce the above copyright
15  notice, this list of conditions and the following disclaimer in the
16  documentation and/or other materials provided with the distribution.
17  * Neither the name of Intel Corporation nor the names of its
18  contributors may be used to endorse or promote products derived
19  from this software without specific prior written permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33 </copyright> */
34 
35 #include <stdlib.h>
36 #include <limits.h>
37 #include <errno.h>
38 
39 #include "omp.h" // Function renamings.
40 #include "kmp.h" // KMP_DEFAULT_STKSIZE
41 #include "kmp_stub.h"
42 
43 #if KMP_OS_WINDOWS
44  #include <windows.h>
45 #else
46  #include <sys/time.h>
47 #endif
48 
49 // Moved from omp.h
50 #define omp_set_max_active_levels ompc_set_max_active_levels
51 #define omp_set_schedule ompc_set_schedule
52 #define omp_get_ancestor_thread_num ompc_get_ancestor_thread_num
53 #define omp_get_team_size ompc_get_team_size
54 
55 #define omp_set_num_threads ompc_set_num_threads
56 #define omp_set_dynamic ompc_set_dynamic
57 #define omp_set_nested ompc_set_nested
58 #define kmp_set_stacksize kmpc_set_stacksize
59 #define kmp_set_stacksize_s kmpc_set_stacksize_s
60 #define kmp_set_blocktime kmpc_set_blocktime
61 #define kmp_set_library kmpc_set_library
62 #define kmp_set_defaults kmpc_set_defaults
63 #define kmp_malloc kmpc_malloc
64 #define kmp_calloc kmpc_calloc
65 #define kmp_realloc kmpc_realloc
66 #define kmp_free kmpc_free
67 
68 static double frequency = 0.0;
69 
70 // Helper functions.
71 static size_t __kmps_init() {
72  static int initialized = 0;
73  static size_t dummy = 0;
74  if ( ! initialized ) {
75 
76  // TODO: Analyze KMP_VERSION environment variable, print __kmp_version_copyright and
77  // __kmp_version_build_time.
78  // WARNING: Do not use "fprintf( stderr, ... )" because it will cause unresolved "__iob"
79  // symbol (see C70080). We need to extract __kmp_printf() stuff from kmp_runtime.c and use
80  // it.
81 
82  // Trick with dummy variable forces linker to keep __kmp_version_copyright and
83  // __kmp_version_build_time strings in executable file (in case of static linkage).
84  // When KMP_VERSION analyze is implemented, dummy variable should be deleted, function
85  // should return void.
86  dummy = __kmp_version_copyright - __kmp_version_build_time;
87 
88  #if KMP_OS_WINDOWS
89  LARGE_INTEGER freq;
90  BOOL status = QueryPerformanceFrequency( & freq );
91  if ( status ) {
92  frequency = double( freq.QuadPart );
93  }; // if
94  #endif
95 
96  initialized = 1;
97  }; // if
98  return dummy;
99 }; // __kmps_init
100 
101 #define i __kmps_init();
102 
103 /* set API functions */
104 void omp_set_num_threads( omp_int_t num_threads ) { i; }
105 void omp_set_dynamic( omp_int_t dynamic ) { i; __kmps_set_dynamic( dynamic ); }
106 void omp_set_nested( omp_int_t nested ) { i; __kmps_set_nested( nested ); }
107 void omp_set_max_active_levels( omp_int_t max_active_levels ) { i; }
108 void omp_set_schedule( omp_sched_t kind, omp_int_t modifier ) { i; __kmps_set_schedule( (kmp_sched_t)kind, modifier ); }
109 int omp_get_ancestor_thread_num( omp_int_t level ) { i; return ( level ) ? ( -1 ) : ( 0 ); }
110 int omp_get_team_size( omp_int_t level ) { i; return ( level ) ? ( -1 ) : ( 1 ); }
111 int kmpc_set_affinity_mask_proc( int proc, void **mask ) { i; return -1; }
112 int kmpc_unset_affinity_mask_proc( int proc, void **mask ) { i; return -1; }
113 int kmpc_get_affinity_mask_proc( int proc, void **mask ) { i; return -1; }
114 
115 /* kmp API functions */
116 void kmp_set_stacksize( omp_int_t arg ) { i; __kmps_set_stacksize( arg ); }
117 void kmp_set_stacksize_s( size_t arg ) { i; __kmps_set_stacksize( arg ); }
118 void kmp_set_blocktime( omp_int_t arg ) { i; __kmps_set_blocktime( arg ); }
119 void kmp_set_library( omp_int_t arg ) { i; __kmps_set_library( arg ); }
120 void kmp_set_defaults( char const * str ) { i; }
121 
122 /* KMP memory management functions. */
123 void * kmp_malloc( size_t size ) { i; return malloc( size ); }
124 void * kmp_calloc( size_t nelem, size_t elsize ) { i; return calloc( nelem, elsize ); }
125 void * kmp_realloc( void *ptr, size_t size ) { i; return realloc( ptr, size ); }
126 void kmp_free( void * ptr ) { i; free( ptr ); }
127 
128 static int __kmps_blocktime = INT_MAX;
129 
130 void __kmps_set_blocktime( int arg ) {
131  i;
132  __kmps_blocktime = arg;
133 } // __kmps_set_blocktime
134 
135 int __kmps_get_blocktime( void ) {
136  i;
137  return __kmps_blocktime;
138 } // __kmps_get_blocktime
139 
140 static int __kmps_dynamic = 0;
141 
142 void __kmps_set_dynamic( int arg ) {
143  i;
144  __kmps_dynamic = arg;
145 } // __kmps_set_dynamic
146 
147 int __kmps_get_dynamic( void ) {
148  i;
149  return __kmps_dynamic;
150 } // __kmps_get_dynamic
151 
152 static int __kmps_library = 1000;
153 
154 void __kmps_set_library( int arg ) {
155  i;
156  __kmps_library = arg;
157 } // __kmps_set_library
158 
159 int __kmps_get_library( void ) {
160  i;
161  return __kmps_library;
162 } // __kmps_get_library
163 
164 static int __kmps_nested = 0;
165 
166 void __kmps_set_nested( int arg ) {
167  i;
168  __kmps_nested = arg;
169 } // __kmps_set_nested
170 
171 int __kmps_get_nested( void ) {
172  i;
173  return __kmps_nested;
174 } // __kmps_get_nested
175 
176 static size_t __kmps_stacksize = KMP_DEFAULT_STKSIZE;
177 
178 void __kmps_set_stacksize( int arg ) {
179  i;
180  __kmps_stacksize = arg;
181 } // __kmps_set_stacksize
182 
183 int __kmps_get_stacksize( void ) {
184  i;
185  return __kmps_stacksize;
186 } // __kmps_get_stacksize
187 
188 static kmp_sched_t __kmps_sched_kind = kmp_sched_default;
189 static int __kmps_sched_modifier = 0;
190 
191  void __kmps_set_schedule( kmp_sched_t kind, int modifier ) {
192  i;
193  __kmps_sched_kind = kind;
194  __kmps_sched_modifier = modifier;
195  } // __kmps_set_schedule
196 
197  void __kmps_get_schedule( kmp_sched_t *kind, int *modifier ) {
198  i;
199  *kind = __kmps_sched_kind;
200  *modifier = __kmps_sched_modifier;
201  } // __kmps_get_schedule
202 
203 #if OMP_40_ENABLED
204 
205 static kmp_proc_bind_t __kmps_proc_bind = proc_bind_false;
206 
207 void __kmps_set_proc_bind( kmp_proc_bind_t arg ) {
208  i;
209  __kmps_proc_bind = arg;
210 } // __kmps_set_proc_bind
211 
212 kmp_proc_bind_t __kmps_get_proc_bind( void ) {
213  i;
214  return __kmps_proc_bind;
215 } // __kmps_get_proc_bind
216 
217 #endif /* OMP_40_ENABLED */
218 
219 double __kmps_get_wtime( void ) {
220  // Elapsed wall clock time (in second) from "sometime in the past".
221  double wtime = 0.0;
222  i;
223  #if KMP_OS_WINDOWS
224  if ( frequency > 0.0 ) {
225  LARGE_INTEGER now;
226  BOOL status = QueryPerformanceCounter( & now );
227  if ( status ) {
228  wtime = double( now.QuadPart ) / frequency;
229  }; // if
230  }; // if
231  #else
232  // gettimeofday() returns seconds and microseconds since the Epoch.
233  struct timeval tval;
234  int rc;
235  rc = gettimeofday( & tval, NULL );
236  if ( rc == 0 ) {
237  wtime = (double)( tval.tv_sec ) + 1.0E-06 * (double)( tval.tv_usec );
238  } else {
239  // TODO: Assert or abort here.
240  }; // if
241  #endif
242  return wtime;
243 }; // __kmps_get_wtime
244 
245 double __kmps_get_wtick( void ) {
246  // Number of seconds between successive clock ticks.
247  double wtick = 0.0;
248  i;
249  #if KMP_OS_WINDOWS
250  {
251  DWORD increment;
252  DWORD adjustment;
253  BOOL disabled;
254  BOOL rc;
255  rc = GetSystemTimeAdjustment( & adjustment, & increment, & disabled );
256  if ( rc ) {
257  wtick = 1.0E-07 * (double)( disabled ? increment : adjustment );
258  } else {
259  // TODO: Assert or abort here.
260  wtick = 1.0E-03;
261  }; // if
262  }
263  #else
264  // TODO: gettimeofday() returns in microseconds, but what the precision?
265  wtick = 1.0E-06;
266  #endif
267  return wtick;
268 }; // __kmps_get_wtick
269 
270 // end of file //
271