Intel® OpenMP* Runtime Library
 All Classes Functions Variables Typedefs Enumerations Enumerator Modules Pages
kmp_stub.c
1 /*
2  * kmp_stub.c -- stub versions of user-callable OpenMP RT functions.
3  * $Revision: 42951 $
4  * $Date: 2014-01-21 14:41:41 -0600 (Tue, 21 Jan 2014) $
5  */
6 
7 /* <copyright>
8  Copyright (c) 1997-2014 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 <stdlib.h>
38 #include <limits.h>
39 #include <errno.h>
40 
41 #include "omp.h" // Function renamings.
42 #include "kmp.h" // KMP_DEFAULT_STKSIZE
43 #include "kmp_stub.h"
44 
45 #if KMP_OS_WINDOWS
46  #include <windows.h>
47 #else
48  #include <sys/time.h>
49 #endif
50 
51 // Moved from omp.h
52 #define omp_set_max_active_levels ompc_set_max_active_levels
53 #define omp_set_schedule ompc_set_schedule
54 #define omp_get_ancestor_thread_num ompc_get_ancestor_thread_num
55 #define omp_get_team_size ompc_get_team_size
56 
57 #define omp_set_num_threads ompc_set_num_threads
58 #define omp_set_dynamic ompc_set_dynamic
59 #define omp_set_nested ompc_set_nested
60 #define kmp_set_stacksize kmpc_set_stacksize
61 #define kmp_set_stacksize_s kmpc_set_stacksize_s
62 #define kmp_set_blocktime kmpc_set_blocktime
63 #define kmp_set_library kmpc_set_library
64 #define kmp_set_defaults kmpc_set_defaults
65 #define kmp_malloc kmpc_malloc
66 #define kmp_calloc kmpc_calloc
67 #define kmp_realloc kmpc_realloc
68 #define kmp_free kmpc_free
69 
70 static double frequency = 0.0;
71 
72 // Helper functions.
73 static size_t __kmps_init() {
74  static int initialized = 0;
75  static size_t dummy = 0;
76  if ( ! initialized ) {
77 
78  // TODO: Analyze KMP_VERSION environment variable, print __kmp_version_copyright and
79  // __kmp_version_build_time.
80  // WARNING: Do not use "fprintf( stderr, ... )" because it will cause unresolved "__iob"
81  // symbol (see C70080). We need to extract __kmp_printf() stuff from kmp_runtime.c and use
82  // it.
83 
84  // Trick with dummy variable forces linker to keep __kmp_version_copyright and
85  // __kmp_version_build_time strings in executable file (in case of static linkage).
86  // When KMP_VERSION analyze is implemented, dummy variable should be deleted, function
87  // should return void.
88  dummy = __kmp_version_copyright - __kmp_version_build_time;
89 
90  #if KMP_OS_WINDOWS
91  LARGE_INTEGER freq;
92  BOOL status = QueryPerformanceFrequency( & freq );
93  if ( status ) {
94  frequency = double( freq.QuadPart );
95  }; // if
96  #endif
97 
98  initialized = 1;
99  }; // if
100  return dummy;
101 }; // __kmps_init
102 
103 #define i __kmps_init();
104 
105 /* set API functions */
106 void omp_set_num_threads( omp_int_t num_threads ) { i; }
107 void omp_set_dynamic( omp_int_t dynamic ) { i; __kmps_set_dynamic( dynamic ); }
108 void omp_set_nested( omp_int_t nested ) { i; __kmps_set_nested( nested ); }
109 void omp_set_max_active_levels( omp_int_t max_active_levels ) { i; }
110 void omp_set_schedule( omp_sched_t kind, omp_int_t modifier ) { i; __kmps_set_schedule( (kmp_sched_t)kind, modifier ); }
111 int omp_get_ancestor_thread_num( omp_int_t level ) { i; return ( level ) ? ( -1 ) : ( 0 ); }
112 int omp_get_team_size( omp_int_t level ) { i; return ( level ) ? ( -1 ) : ( 1 ); }
113 int kmpc_set_affinity_mask_proc( int proc, void **mask ) { i; return -1; }
114 int kmpc_unset_affinity_mask_proc( int proc, void **mask ) { i; return -1; }
115 int kmpc_get_affinity_mask_proc( int proc, void **mask ) { i; return -1; }
116 
117 /* kmp API functions */
118 void kmp_set_stacksize( omp_int_t arg ) { i; __kmps_set_stacksize( arg ); }
119 void kmp_set_stacksize_s( size_t arg ) { i; __kmps_set_stacksize( arg ); }
120 void kmp_set_blocktime( omp_int_t arg ) { i; __kmps_set_blocktime( arg ); }
121 void kmp_set_library( omp_int_t arg ) { i; __kmps_set_library( arg ); }
122 void kmp_set_defaults( char const * str ) { i; }
123 
124 /* KMP memory management functions. */
125 void * kmp_malloc( size_t size ) { i; return malloc( size ); }
126 void * kmp_calloc( size_t nelem, size_t elsize ) { i; return calloc( nelem, elsize ); }
127 void * kmp_realloc( void *ptr, size_t size ) { i; return realloc( ptr, size ); }
128 void kmp_free( void * ptr ) { i; free( ptr ); }
129 
130 static int __kmps_blocktime = INT_MAX;
131 
132 void __kmps_set_blocktime( int arg ) {
133  i;
134  __kmps_blocktime = arg;
135 } // __kmps_set_blocktime
136 
137 int __kmps_get_blocktime( void ) {
138  i;
139  return __kmps_blocktime;
140 } // __kmps_get_blocktime
141 
142 static int __kmps_dynamic = 0;
143 
144 void __kmps_set_dynamic( int arg ) {
145  i;
146  __kmps_dynamic = arg;
147 } // __kmps_set_dynamic
148 
149 int __kmps_get_dynamic( void ) {
150  i;
151  return __kmps_dynamic;
152 } // __kmps_get_dynamic
153 
154 static int __kmps_library = 1000;
155 
156 void __kmps_set_library( int arg ) {
157  i;
158  __kmps_library = arg;
159 } // __kmps_set_library
160 
161 int __kmps_get_library( void ) {
162  i;
163  return __kmps_library;
164 } // __kmps_get_library
165 
166 static int __kmps_nested = 0;
167 
168 void __kmps_set_nested( int arg ) {
169  i;
170  __kmps_nested = arg;
171 } // __kmps_set_nested
172 
173 int __kmps_get_nested( void ) {
174  i;
175  return __kmps_nested;
176 } // __kmps_get_nested
177 
178 static size_t __kmps_stacksize = KMP_DEFAULT_STKSIZE;
179 
180 void __kmps_set_stacksize( int arg ) {
181  i;
182  __kmps_stacksize = arg;
183 } // __kmps_set_stacksize
184 
185 int __kmps_get_stacksize( void ) {
186  i;
187  return __kmps_stacksize;
188 } // __kmps_get_stacksize
189 
190 static kmp_sched_t __kmps_sched_kind = kmp_sched_default;
191 static int __kmps_sched_modifier = 0;
192 
193  void __kmps_set_schedule( kmp_sched_t kind, int modifier ) {
194  i;
195  __kmps_sched_kind = kind;
196  __kmps_sched_modifier = modifier;
197  } // __kmps_set_schedule
198 
199  void __kmps_get_schedule( kmp_sched_t *kind, int *modifier ) {
200  i;
201  *kind = __kmps_sched_kind;
202  *modifier = __kmps_sched_modifier;
203  } // __kmps_get_schedule
204 
205 #if OMP_40_ENABLED
206 
207 static kmp_proc_bind_t __kmps_proc_bind = proc_bind_false;
208 
209 void __kmps_set_proc_bind( kmp_proc_bind_t arg ) {
210  i;
211  __kmps_proc_bind = arg;
212 } // __kmps_set_proc_bind
213 
214 kmp_proc_bind_t __kmps_get_proc_bind( void ) {
215  i;
216  return __kmps_proc_bind;
217 } // __kmps_get_proc_bind
218 
219 #endif /* OMP_40_ENABLED */
220 
221 double __kmps_get_wtime( void ) {
222  // Elapsed wall clock time (in second) from "sometime in the past".
223  double wtime = 0.0;
224  i;
225  #if KMP_OS_WINDOWS
226  if ( frequency > 0.0 ) {
227  LARGE_INTEGER now;
228  BOOL status = QueryPerformanceCounter( & now );
229  if ( status ) {
230  wtime = double( now.QuadPart ) / frequency;
231  }; // if
232  }; // if
233  #else
234  // gettimeofday() returns seconds and microseconds sinse the Epoch.
235  struct timeval tval;
236  int rc;
237  rc = gettimeofday( & tval, NULL );
238  if ( rc == 0 ) {
239  wtime = (double)( tval.tv_sec ) + 1.0E-06 * (double)( tval.tv_usec );
240  } else {
241  // TODO: Assert or abort here.
242  }; // if
243  #endif
244  return wtime;
245 }; // __kmps_get_wtime
246 
247 double __kmps_get_wtick( void ) {
248  // Number of seconds between successive clock ticks.
249  double wtick = 0.0;
250  i;
251  #if KMP_OS_WINDOWS
252  {
253  DWORD increment;
254  DWORD adjustment;
255  BOOL disabled;
256  BOOL rc;
257  rc = GetSystemTimeAdjustment( & adjustment, & increment, & disabled );
258  if ( rc ) {
259  wtick = 1.0E-07 * (double)( disabled ? increment : adjustment );
260  } else {
261  // TODO: Assert or abort here.
262  wtick = 1.0E-03;
263  }; // if
264  }
265  #else
266  // TODO: gettimeofday() returns in microseconds, but what the precision?
267  wtick = 1.0E-06;
268  #endif
269  return wtick;
270 }; // __kmps_get_wtick
271 
272 // end of file //
273