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