Intel® OpenMP* Runtime Library
 All Classes Functions Variables Typedefs Enumerations Enumerator Modules Pages
ompt-general.c
1 /*
2  * ompt-general.c -- OMPT general file.
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 /*****************************************************************************
36  * system include files
37  ****************************************************************************/
38 
39 #include <assert.h>
40 
41 #include <stdint.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 
46 
47 
48 /*****************************************************************************
49  * ompt include files
50  ****************************************************************************/
51 
52 #include "ompt-internal.h"
53 #include "ompt-specific.c"
54 
55 
56 
57 /*****************************************************************************
58  * macros
59  ****************************************************************************/
60 
61 #define ompt_get_callback_success 1
62 #define ompt_get_callback_failure 0
63 
64 #define no_tool_present 0
65 
66 #define OMPT_API_ROUTINE static
67 
68 
69 
70 /*****************************************************************************
71  * types
72  ****************************************************************************/
73 
74 typedef struct {
75  const char *state_name;
76  ompt_state_t state_id;
77 } ompt_state_info_t;
78 
79 
80 
81 /*****************************************************************************
82  * global variables
83  ****************************************************************************/
84 
85 ompt_status_t ompt_status = ompt_status_ready;
86 
87 
88 ompt_state_info_t ompt_state_info[] = {
89 #define ompt_state_macro(state, code) { # state, state },
90  FOREACH_OMPT_STATE(ompt_state_macro)
91 #undef ompt_state_macro
92 };
93 
94 
95 ompt_callbacks_t ompt_callbacks;
96 
97 
98 
99 /*****************************************************************************
100  * forward declarations
101  ****************************************************************************/
102 
103 static ompt_interface_fn_t ompt_fn_lookup(const char *s);
104 
105 
106 /*****************************************************************************
107  * state
108  ****************************************************************************/
109 
110 OMPT_API_ROUTINE int ompt_enumerate_state(int current_state, int *next_state,
111  const char **next_state_name)
112 {
113  const static int len = sizeof(ompt_state_info) / sizeof(ompt_state_info_t);
114  int i = 0;
115 
116  for (i = 0; i < len - 1; i++) {
117  if (ompt_state_info[i].state_id == current_state) {
118  *next_state = ompt_state_info[i+1].state_id;
119  *next_state_name = ompt_state_info[i+1].state_name;
120  return 1;
121  }
122  }
123 
124  return 0;
125 }
126 
127 
128 
129 /*****************************************************************************
130  * callbacks
131  ****************************************************************************/
132 
133 OMPT_API_ROUTINE int ompt_set_callback(ompt_event_t evid, ompt_callback_t cb)
134 {
135  switch (evid) {
136 
137 #define ompt_event_macro(event_name, callback_type, event_id) \
138  case event_name: \
139  if (ompt_event_implementation_status(event_name)) { \
140  ompt_callbacks.ompt_callback(event_name) = (callback_type) cb; \
141  } \
142  return ompt_event_implementation_status(event_name);
143 
144  FOREACH_OMPT_EVENT(ompt_event_macro)
145 
146 #undef ompt_event_macro
147 
148  default: return ompt_set_result_registration_error;
149  }
150 }
151 
152 
153 OMPT_API_ROUTINE int ompt_get_callback(ompt_event_t evid, ompt_callback_t *cb)
154 {
155  switch (evid) {
156 
157 #define ompt_event_macro(event_name, callback_type, event_id) \
158  case event_name: \
159  if (ompt_event_implementation_status(event_name)) { \
160  ompt_callback_t mycb = \
161  (ompt_callback_t) ompt_callbacks.ompt_callback(event_name); \
162  if (mycb) { \
163  *cb = mycb; \
164  return ompt_get_callback_success; \
165  } \
166  } \
167  return ompt_get_callback_failure;
168 
169  FOREACH_OMPT_EVENT(ompt_event_macro)
170 
171 #undef ompt_event_macro
172 
173  default: return ompt_get_callback_failure;
174  }
175 }
176 
177 
178 
179 /*****************************************************************************
180  * intialization/finalization
181  ****************************************************************************/
182 
183 _OMP_EXTERN
184 #if KMP_OS_UNIX
185 __attribute__ (( weak ))
186 #endif
187 int ompt_initialize(ompt_function_lookup_t ompt_fn_lookup, const char *version,
188  unsigned int ompt_version)
189 {
190  return no_tool_present;
191 }
192 
193 enum tool_setting_e {
194  omp_tool_error,
195  omp_tool_unset,
196  omp_tool_disabled,
197  omp_tool_enabled
198 };
199 
200 void ompt_init()
201 {
202  static int ompt_initialized = 0;
203 
204  if (ompt_initialized) return;
205 
206  const char *ompt_env_var = getenv("OMP_TOOL");
207  tool_setting_e tool_setting = omp_tool_error;
208 
209  if (!ompt_env_var || !strcmp(ompt_env_var, ""))
210  tool_setting = omp_tool_unset;
211  else if (!strcmp(ompt_env_var, "disabled"))
212  tool_setting = omp_tool_disabled;
213  else if (!strcmp(ompt_env_var, "enabled"))
214  tool_setting = omp_tool_enabled;
215 
216  switch(tool_setting) {
217  case omp_tool_disabled:
218  ompt_status = ompt_status_disabled;
219  break;
220 
221  case omp_tool_unset:
222  case omp_tool_enabled:
223  {
224  const char *runtime_version = __ompt_get_runtime_version_internal();
225  int ompt_init_val =
226  ompt_initialize(ompt_fn_lookup, runtime_version, OMPT_VERSION);
227 
228  if (ompt_init_val) {
229  ompt_status = ompt_status_track_callback;
230  __ompt_init_internal();
231  }
232  break;
233  }
234 
235  case omp_tool_error:
236  fprintf(stderr,
237  "Warning: OMP_TOOL has invalid value \"%s\".\n"
238  " legal values are (NULL,\"\",\"disabled\","
239  "\"enabled\").\n", ompt_env_var);
240  break;
241  }
242 
243  ompt_initialized = 1;
244 }
245 
246 
247 void ompt_fini()
248 {
249  if (ompt_status == ompt_status_track_callback) {
250  if (ompt_callbacks.ompt_callback(ompt_event_runtime_shutdown)) {
251  ompt_callbacks.ompt_callback(ompt_event_runtime_shutdown)();
252  }
253  }
254 
255  ompt_status = ompt_status_disabled;
256 }
257 
258 
259 
260 /*****************************************************************************
261  * parallel regions
262  ****************************************************************************/
263 
264 OMPT_API_ROUTINE ompt_parallel_id_t ompt_get_parallel_id(int ancestor_level)
265 {
266  return __ompt_get_parallel_id_internal(ancestor_level);
267 }
268 
269 
270 OMPT_API_ROUTINE int ompt_get_parallel_team_size(int ancestor_level)
271 {
272  return __ompt_get_parallel_team_size_internal(ancestor_level);
273 }
274 
275 
276 OMPT_API_ROUTINE void *ompt_get_parallel_function(int ancestor_level)
277 {
278  return __ompt_get_parallel_function_internal(ancestor_level);
279 }
280 
281 
282 OMPT_API_ROUTINE ompt_state_t ompt_get_state(ompt_wait_id_t *ompt_wait_id)
283 {
284  ompt_state_t thread_state = __ompt_get_state_internal(ompt_wait_id);
285 
286  if (thread_state == ompt_state_undefined) {
287  thread_state = ompt_state_work_serial;
288  }
289 
290  return thread_state;
291 }
292 
293 
294 
295 /*****************************************************************************
296  * threads
297  ****************************************************************************/
298 
299 
300 OMPT_API_ROUTINE void *ompt_get_idle_frame()
301 {
302  return __ompt_get_idle_frame_internal();
303 }
304 
305 
306 
307 /*****************************************************************************
308  * tasks
309  ****************************************************************************/
310 
311 
312 OMPT_API_ROUTINE ompt_thread_id_t ompt_get_thread_id(void)
313 {
314  return __ompt_get_thread_id_internal();
315 }
316 
317 OMPT_API_ROUTINE ompt_task_id_t ompt_get_task_id(int depth)
318 {
319  return __ompt_get_task_id_internal(depth);
320 }
321 
322 
323 OMPT_API_ROUTINE ompt_frame_t *ompt_get_task_frame(int depth)
324 {
325  return __ompt_get_task_frame_internal(depth);
326 }
327 
328 
329 OMPT_API_ROUTINE void *ompt_get_task_function(int depth)
330 {
331  return __ompt_get_task_function_internal(depth);
332 }
333 
334 
335 /*****************************************************************************
336  * placeholders
337  ****************************************************************************/
338 
339 
340 OMPT_API_ROUTINE void omp_idle(void)
341 {
342  // this function is a placeholder used to represent the calling context of
343  // idle OpenMP worker threads. It is not meant to be invoked.
344  assert(0);
345 }
346 
347 
348 OMPT_API_ROUTINE void omp_overhead(void)
349 {
350  // this function is a placeholder used to represent the OpenMP context of
351  // threads working in the OpenMP runtime. It is not meant to be invoked.
352  assert(0);
353 }
354 
355 
356 OMPT_API_ROUTINE void omp_barrier_wait(void)
357 {
358  // this function is a placeholder used to represent the OpenMP context of
359  // threads waiting for a barrier in the OpenMP runtime. It is not meant
360  // to be invoked.
361  assert(0);
362 }
363 
364 
365 OMPT_API_ROUTINE void omp_task_wait(void)
366 {
367  // this function is a placeholder used to represent the OpenMP context of
368  // threads waiting for a task in the OpenMP runtime. It is not meant
369  // to be invoked.
370  assert(0);
371 }
372 
373 
374 OMPT_API_ROUTINE void omp_mutex_wait(void)
375 {
376  // this function is a placeholder used to represent the OpenMP context of
377  // threads waiting for a mutex in the OpenMP runtime. It is not meant
378  // to be invoked.
379  assert(0);
380 }
381 
382 
383 /*****************************************************************************
384  * compatability
385  ****************************************************************************/
386 
387 OMPT_API_ROUTINE int ompt_get_ompt_version()
388 {
389  return OMPT_VERSION;
390 }
391 
392 
393 
394 /*****************************************************************************
395  * application-facing API
396  ****************************************************************************/
397 
398 
399 /*----------------------------------------------------------------------------
400  | control
401  ---------------------------------------------------------------------------*/
402 
403 _OMP_EXTERN void ompt_control(uint64_t command, uint64_t modifier)
404 {
405  if (ompt_status == ompt_status_track_callback &&
406  ompt_callbacks.ompt_callback(ompt_event_control)) {
407  ompt_callbacks.ompt_callback(ompt_event_control)(command, modifier);
408  }
409 }
410 
411 
412 
413 /*****************************************************************************
414  * API inquiry for tool
415  ****************************************************************************/
416 
417 static ompt_interface_fn_t ompt_fn_lookup(const char *s)
418 {
419 
420 #define ompt_interface_fn(fn) \
421  if (strcmp(s, #fn) == 0) return (ompt_interface_fn_t) fn;
422 
423  FOREACH_OMPT_INQUIRY_FN(ompt_interface_fn)
424 
425  FOREACH_OMPT_PLACEHOLDER_FN(ompt_interface_fn)
426 
427  return (ompt_interface_fn_t) 0;
428 }