xref: /freebsd/contrib/llvm-project/openmp/runtime/src/ompt-general.cpp (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1 /*
2  * ompt-general.cpp -- OMPT implementation of interface functions
3  */
4 
5 //===----------------------------------------------------------------------===//
6 //
7 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
8 // See https://llvm.org/LICENSE.txt for license information.
9 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
10 //
11 //===----------------------------------------------------------------------===//
12 
13 /*****************************************************************************
14  * system include files
15  ****************************************************************************/
16 
17 #include <assert.h>
18 
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #if KMP_OS_UNIX
24 #include <dlfcn.h>
25 #endif
26 
27 /*****************************************************************************
28  * ompt include files
29  ****************************************************************************/
30 
31 #include "ompt-specific.cpp"
32 
33 /*****************************************************************************
34  * macros
35  ****************************************************************************/
36 
37 #define ompt_get_callback_success 1
38 #define ompt_get_callback_failure 0
39 
40 #define no_tool_present 0
41 
42 #define OMPT_API_ROUTINE static
43 
44 #ifndef OMPT_STR_MATCH
45 #define OMPT_STR_MATCH(haystack, needle) (!strcasecmp(haystack, needle))
46 #endif
47 
48 // prints for an enabled OMP_TOOL_VERBOSE_INIT.
49 // In the future a prefix could be added in the first define, the second define
50 // omits the prefix to allow for continued lines. Example: "PREFIX: Start
51 // tool... Success." instead of "PREFIX: Start tool... PREFIX: Success."
52 #define OMPT_VERBOSE_INIT_PRINT(...)                                           \
53   if (verbose_init)                                                            \
54   fprintf(verbose_file, __VA_ARGS__)
55 #define OMPT_VERBOSE_INIT_CONTINUED_PRINT(...)                                 \
56   if (verbose_init)                                                            \
57   fprintf(verbose_file, __VA_ARGS__)
58 
59 static FILE *verbose_file;
60 static int verbose_init;
61 
62 /*****************************************************************************
63  * types
64  ****************************************************************************/
65 
66 typedef struct {
67   const char *state_name;
68   ompt_state_t state_id;
69 } ompt_state_info_t;
70 
71 typedef struct {
72   const char *name;
73   kmp_mutex_impl_t id;
74 } kmp_mutex_impl_info_t;
75 
76 enum tool_setting_e {
77   omp_tool_error,
78   omp_tool_unset,
79   omp_tool_disabled,
80   omp_tool_enabled
81 };
82 
83 /*****************************************************************************
84  * global variables
85  ****************************************************************************/
86 
87 ompt_callbacks_active_t ompt_enabled;
88 
89 ompt_state_info_t ompt_state_info[] = {
90 #define ompt_state_macro(state, code) {#state, state},
91     FOREACH_OMPT_STATE(ompt_state_macro)
92 #undef ompt_state_macro
93 };
94 
95 kmp_mutex_impl_info_t kmp_mutex_impl_info[] = {
96 #define kmp_mutex_impl_macro(name, id) {#name, name},
97     FOREACH_KMP_MUTEX_IMPL(kmp_mutex_impl_macro)
98 #undef kmp_mutex_impl_macro
99 };
100 
101 ompt_callbacks_internal_t ompt_callbacks;
102 
103 static ompt_start_tool_result_t *ompt_start_tool_result = NULL;
104 
105 #if KMP_OS_WINDOWS
106 static HMODULE ompt_tool_module = NULL;
107 #define OMPT_DLCLOSE(Lib) FreeLibrary(Lib)
108 #else
109 static void *ompt_tool_module = NULL;
110 #define OMPT_DLCLOSE(Lib) dlclose(Lib)
111 #endif
112 
113 /*****************************************************************************
114  * forward declarations
115  ****************************************************************************/
116 
117 static ompt_interface_fn_t ompt_fn_lookup(const char *s);
118 
119 OMPT_API_ROUTINE ompt_data_t *ompt_get_thread_data(void);
120 
121 /*****************************************************************************
122  * initialization and finalization (private operations)
123  ****************************************************************************/
124 
125 typedef ompt_start_tool_result_t *(*ompt_start_tool_t)(unsigned int,
126                                                        const char *);
127 
128 #if KMP_OS_DARWIN
129 
130 // While Darwin supports weak symbols, the library that wishes to provide a new
131 // implementation has to link against this runtime which defeats the purpose
132 // of having tools that are agnostic of the underlying runtime implementation.
133 //
134 // Fortunately, the linker includes all symbols of an executable in the global
135 // symbol table by default so dlsym() even finds static implementations of
136 // ompt_start_tool. For this to work on Linux, -Wl,--export-dynamic needs to be
137 // passed when building the application which we don't want to rely on.
138 
139 static ompt_start_tool_result_t *ompt_tool_darwin(unsigned int omp_version,
140                                                   const char *runtime_version) {
141   ompt_start_tool_result_t *ret = NULL;
142   // Search symbol in the current address space.
143   ompt_start_tool_t start_tool =
144       (ompt_start_tool_t)dlsym(RTLD_DEFAULT, "ompt_start_tool");
145   if (start_tool) {
146     ret = start_tool(omp_version, runtime_version);
147   }
148   return ret;
149 }
150 
151 #elif OMPT_HAVE_WEAK_ATTRIBUTE
152 
153 // On Unix-like systems that support weak symbols the following implementation
154 // of ompt_start_tool() will be used in case no tool-supplied implementation of
155 // this function is present in the address space of a process.
156 
157 _OMP_EXTERN OMPT_WEAK_ATTRIBUTE ompt_start_tool_result_t *
158 ompt_start_tool(unsigned int omp_version, const char *runtime_version) {
159   ompt_start_tool_result_t *ret = NULL;
160   // Search next symbol in the current address space. This can happen if the
161   // runtime library is linked before the tool. Since glibc 2.2 strong symbols
162   // don't override weak symbols that have been found before unless the user
163   // sets the environment variable LD_DYNAMIC_WEAK.
164   ompt_start_tool_t next_tool =
165       (ompt_start_tool_t)dlsym(RTLD_NEXT, "ompt_start_tool");
166   if (next_tool) {
167     ret = next_tool(omp_version, runtime_version);
168   }
169   return ret;
170 }
171 
172 #elif OMPT_HAVE_PSAPI
173 
174 // On Windows, the ompt_tool_windows function is used to find the
175 // ompt_start_tool symbol across all modules loaded by a process. If
176 // ompt_start_tool is found, ompt_start_tool's return value is used to
177 // initialize the tool. Otherwise, NULL is returned and OMPT won't be enabled.
178 
179 #include <psapi.h>
180 #pragma comment(lib, "psapi.lib")
181 
182 // The number of loaded modules to start enumeration with EnumProcessModules()
183 #define NUM_MODULES 128
184 
185 static ompt_start_tool_result_t *
186 ompt_tool_windows(unsigned int omp_version, const char *runtime_version) {
187   int i;
188   DWORD needed, new_size;
189   HMODULE *modules;
190   HANDLE process = GetCurrentProcess();
191   modules = (HMODULE *)malloc(NUM_MODULES * sizeof(HMODULE));
192   ompt_start_tool_t ompt_tool_p = NULL;
193 
194 #if OMPT_DEBUG
195   printf("ompt_tool_windows(): looking for ompt_start_tool\n");
196 #endif
197   if (!EnumProcessModules(process, modules, NUM_MODULES * sizeof(HMODULE),
198                           &needed)) {
199     // Regardless of the error reason use the stub initialization function
200     free(modules);
201     return NULL;
202   }
203   // Check if NUM_MODULES is enough to list all modules
204   new_size = needed / sizeof(HMODULE);
205   if (new_size > NUM_MODULES) {
206 #if OMPT_DEBUG
207     printf("ompt_tool_windows(): resize buffer to %d bytes\n", needed);
208 #endif
209     modules = (HMODULE *)realloc(modules, needed);
210     // If resizing failed use the stub function.
211     if (!EnumProcessModules(process, modules, needed, &needed)) {
212       free(modules);
213       return NULL;
214     }
215   }
216   for (i = 0; i < new_size; ++i) {
217     (FARPROC &)ompt_tool_p = GetProcAddress(modules[i], "ompt_start_tool");
218     if (ompt_tool_p) {
219 #if OMPT_DEBUG
220       TCHAR modName[MAX_PATH];
221       if (GetModuleFileName(modules[i], modName, MAX_PATH))
222         printf("ompt_tool_windows(): ompt_start_tool found in module %s\n",
223                modName);
224 #endif
225       free(modules);
226       return (*ompt_tool_p)(omp_version, runtime_version);
227     }
228 #if OMPT_DEBUG
229     else {
230       TCHAR modName[MAX_PATH];
231       if (GetModuleFileName(modules[i], modName, MAX_PATH))
232         printf("ompt_tool_windows(): ompt_start_tool not found in module %s\n",
233                modName);
234     }
235 #endif
236   }
237   free(modules);
238   return NULL;
239 }
240 #else
241 #error Activation of OMPT is not supported on this platform.
242 #endif
243 
244 static ompt_start_tool_result_t *
245 ompt_try_start_tool(unsigned int omp_version, const char *runtime_version) {
246   ompt_start_tool_result_t *ret = NULL;
247   ompt_start_tool_t start_tool = NULL;
248 #if KMP_OS_WINDOWS
249   // Cannot use colon to describe a list of absolute paths on Windows
250   const char *sep = ";";
251 #else
252   const char *sep = ":";
253 #endif
254 
255   OMPT_VERBOSE_INIT_PRINT("----- START LOGGING OF TOOL REGISTRATION -----\n");
256   OMPT_VERBOSE_INIT_PRINT("Search for OMP tool in current address space... ");
257 
258 #if KMP_OS_DARWIN
259   // Try in the current address space
260   ret = ompt_tool_darwin(omp_version, runtime_version);
261 #elif OMPT_HAVE_WEAK_ATTRIBUTE
262   ret = ompt_start_tool(omp_version, runtime_version);
263 #elif OMPT_HAVE_PSAPI
264   ret = ompt_tool_windows(omp_version, runtime_version);
265 #else
266 #error Activation of OMPT is not supported on this platform.
267 #endif
268   if (ret) {
269     OMPT_VERBOSE_INIT_CONTINUED_PRINT("Success.\n");
270     OMPT_VERBOSE_INIT_PRINT(
271         "Tool was started and is using the OMPT interface.\n");
272     OMPT_VERBOSE_INIT_PRINT("----- END LOGGING OF TOOL REGISTRATION -----\n");
273     return ret;
274   }
275 
276   // Try tool-libraries-var ICV
277   OMPT_VERBOSE_INIT_CONTINUED_PRINT("Failed.\n");
278   const char *tool_libs = getenv("OMP_TOOL_LIBRARIES");
279   if (tool_libs) {
280     OMPT_VERBOSE_INIT_PRINT("Searching tool libraries...\n");
281     OMPT_VERBOSE_INIT_PRINT("OMP_TOOL_LIBRARIES = %s\n", tool_libs);
282     char *libs = __kmp_str_format("%s", tool_libs);
283     char *buf;
284     char *fname = __kmp_str_token(libs, sep, &buf);
285     // Reset dl-error
286     dlerror();
287 
288     while (fname) {
289 #if KMP_OS_UNIX
290       OMPT_VERBOSE_INIT_PRINT("Opening %s... ", fname);
291       void *h = dlopen(fname, RTLD_LAZY);
292       if (!h) {
293         OMPT_VERBOSE_INIT_CONTINUED_PRINT("Failed: %s\n", dlerror());
294       } else {
295         OMPT_VERBOSE_INIT_CONTINUED_PRINT("Success. \n");
296         OMPT_VERBOSE_INIT_PRINT("Searching for ompt_start_tool in %s... ",
297                                 fname);
298         dlerror(); // Clear any existing error
299         start_tool = (ompt_start_tool_t)dlsym(h, "ompt_start_tool");
300         if (!start_tool) {
301           char *error = dlerror();
302           if (error != NULL) {
303             OMPT_VERBOSE_INIT_CONTINUED_PRINT("Failed: %s\n", error);
304           } else {
305             OMPT_VERBOSE_INIT_CONTINUED_PRINT("Failed: %s\n",
306                                               "ompt_start_tool = NULL");
307           }
308         } else
309 #elif KMP_OS_WINDOWS
310       OMPT_VERBOSE_INIT_PRINT("Opening %s... ", fname);
311       HMODULE h = LoadLibrary(fname);
312       if (!h) {
313         OMPT_VERBOSE_INIT_CONTINUED_PRINT("Failed: Error %u\n", GetLastError());
314       } else {
315         OMPT_VERBOSE_INIT_CONTINUED_PRINT("Success. \n");
316         OMPT_VERBOSE_INIT_PRINT("Searching for ompt_start_tool in %s... ",
317                                 fname);
318         start_tool = (ompt_start_tool_t)GetProcAddress(h, "ompt_start_tool");
319         if (!start_tool) {
320           OMPT_VERBOSE_INIT_CONTINUED_PRINT("Failed: Error %u\n",
321                                             GetLastError());
322         } else
323 #else
324 #error Activation of OMPT is not supported on this platform.
325 #endif
326         { // if (start_tool)
327           ret = (*start_tool)(omp_version, runtime_version);
328           if (ret) {
329             OMPT_VERBOSE_INIT_CONTINUED_PRINT("Success.\n");
330             OMPT_VERBOSE_INIT_PRINT(
331                 "Tool was started and is using the OMPT interface.\n");
332             ompt_tool_module = h;
333             break;
334           }
335           OMPT_VERBOSE_INIT_CONTINUED_PRINT(
336               "Found but not using the OMPT interface.\n");
337           OMPT_VERBOSE_INIT_PRINT("Continuing search...\n");
338         }
339         OMPT_DLCLOSE(h);
340       }
341       fname = __kmp_str_token(NULL, sep, &buf);
342     }
343     __kmp_str_free(&libs);
344   } else {
345     OMPT_VERBOSE_INIT_PRINT("No OMP_TOOL_LIBRARIES defined.\n");
346   }
347 
348   // usable tool found in tool-libraries
349   if (ret) {
350     OMPT_VERBOSE_INIT_PRINT("----- END LOGGING OF TOOL REGISTRATION -----\n");
351     return ret;
352   }
353 
354 #if KMP_OS_UNIX
355   { // Non-standard: load archer tool if application is built with TSan
356     const char *fname = "libarcher.so";
357     OMPT_VERBOSE_INIT_PRINT(
358         "...searching tool libraries failed. Using archer tool.\n");
359     OMPT_VERBOSE_INIT_PRINT("Opening %s... ", fname);
360     void *h = dlopen(fname, RTLD_LAZY);
361     if (h) {
362       OMPT_VERBOSE_INIT_CONTINUED_PRINT("Success.\n");
363       OMPT_VERBOSE_INIT_PRINT("Searching for ompt_start_tool in %s... ", fname);
364       start_tool = (ompt_start_tool_t)dlsym(h, "ompt_start_tool");
365       if (start_tool) {
366         ret = (*start_tool)(omp_version, runtime_version);
367         if (ret) {
368           OMPT_VERBOSE_INIT_CONTINUED_PRINT("Success.\n");
369           OMPT_VERBOSE_INIT_PRINT(
370               "Tool was started and is using the OMPT interface.\n");
371           OMPT_VERBOSE_INIT_PRINT(
372               "----- END LOGGING OF TOOL REGISTRATION -----\n");
373           return ret;
374         }
375         OMPT_VERBOSE_INIT_CONTINUED_PRINT(
376             "Found but not using the OMPT interface.\n");
377       } else {
378         OMPT_VERBOSE_INIT_CONTINUED_PRINT("Failed: %s\n", dlerror());
379       }
380     }
381   }
382 #endif
383   OMPT_VERBOSE_INIT_PRINT("No OMP tool loaded.\n");
384   OMPT_VERBOSE_INIT_PRINT("----- END LOGGING OF TOOL REGISTRATION -----\n");
385   return ret;
386 }
387 
388 void ompt_pre_init() {
389   //--------------------------------------------------
390   // Execute the pre-initialization logic only once.
391   //--------------------------------------------------
392   static int ompt_pre_initialized = 0;
393 
394   if (ompt_pre_initialized)
395     return;
396 
397   ompt_pre_initialized = 1;
398 
399   //--------------------------------------------------
400   // Use a tool iff a tool is enabled and available.
401   //--------------------------------------------------
402   const char *ompt_env_var = getenv("OMP_TOOL");
403   tool_setting_e tool_setting = omp_tool_error;
404 
405   if (!ompt_env_var || !strcmp(ompt_env_var, ""))
406     tool_setting = omp_tool_unset;
407   else if (OMPT_STR_MATCH(ompt_env_var, "disabled"))
408     tool_setting = omp_tool_disabled;
409   else if (OMPT_STR_MATCH(ompt_env_var, "enabled"))
410     tool_setting = omp_tool_enabled;
411 
412   const char *ompt_env_verbose_init = getenv("OMP_TOOL_VERBOSE_INIT");
413   // possible options: disabled | stdout | stderr | <filename>
414   // if set, not empty and not disabled -> prepare for logging
415   if (ompt_env_verbose_init && strcmp(ompt_env_verbose_init, "") &&
416       !OMPT_STR_MATCH(ompt_env_verbose_init, "disabled")) {
417     verbose_init = 1;
418     if (OMPT_STR_MATCH(ompt_env_verbose_init, "STDERR"))
419       verbose_file = stderr;
420     else if (OMPT_STR_MATCH(ompt_env_verbose_init, "STDOUT"))
421       verbose_file = stdout;
422     else
423       verbose_file = fopen(ompt_env_verbose_init, "w");
424   } else
425     verbose_init = 0;
426 
427 #if OMPT_DEBUG
428   printf("ompt_pre_init(): tool_setting = %d\n", tool_setting);
429 #endif
430   switch (tool_setting) {
431   case omp_tool_disabled:
432     OMPT_VERBOSE_INIT_PRINT("OMP tool disabled. \n");
433     break;
434 
435   case omp_tool_unset:
436   case omp_tool_enabled:
437 
438     //--------------------------------------------------
439     // Load tool iff specified in environment variable
440     //--------------------------------------------------
441     ompt_start_tool_result =
442         ompt_try_start_tool(__kmp_openmp_version, ompt_get_runtime_version());
443 
444     memset(&ompt_enabled, 0, sizeof(ompt_enabled));
445     break;
446 
447   case omp_tool_error:
448     fprintf(stderr,
449             "Warning: OMP_TOOL has invalid value \"%s\".\n"
450             "  legal values are (NULL,\"\",\"disabled\","
451             "\"enabled\").\n",
452             ompt_env_var);
453     break;
454   }
455   if (verbose_init && verbose_file != stderr && verbose_file != stdout)
456     fclose(verbose_file);
457 #if OMPT_DEBUG
458   printf("ompt_pre_init(): ompt_enabled = %d\n", ompt_enabled);
459 #endif
460 }
461 
462 extern "C" int omp_get_initial_device(void);
463 
464 void ompt_post_init() {
465   //--------------------------------------------------
466   // Execute the post-initialization logic only once.
467   //--------------------------------------------------
468   static int ompt_post_initialized = 0;
469 
470   if (ompt_post_initialized)
471     return;
472 
473   ompt_post_initialized = 1;
474 
475   //--------------------------------------------------
476   // Initialize the tool if so indicated.
477   //--------------------------------------------------
478   if (ompt_start_tool_result) {
479     ompt_enabled.enabled = !!ompt_start_tool_result->initialize(
480         ompt_fn_lookup, omp_get_initial_device(),
481         &(ompt_start_tool_result->tool_data));
482 
483     if (!ompt_enabled.enabled) {
484       // tool not enabled, zero out the bitmap, and done
485       memset(&ompt_enabled, 0, sizeof(ompt_enabled));
486       return;
487     }
488 
489     kmp_info_t *root_thread = ompt_get_thread();
490 
491     ompt_set_thread_state(root_thread, ompt_state_overhead);
492 
493     if (ompt_enabled.ompt_callback_thread_begin) {
494       ompt_callbacks.ompt_callback(ompt_callback_thread_begin)(
495           ompt_thread_initial, __ompt_get_thread_data_internal());
496     }
497     ompt_data_t *task_data;
498     ompt_data_t *parallel_data;
499     __ompt_get_task_info_internal(0, NULL, &task_data, NULL, &parallel_data,
500                                   NULL);
501     if (ompt_enabled.ompt_callback_implicit_task) {
502       ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
503           ompt_scope_begin, parallel_data, task_data, 1, 1, ompt_task_initial);
504     }
505 
506     ompt_set_thread_state(root_thread, ompt_state_work_serial);
507   }
508 }
509 
510 void ompt_fini() {
511   if (ompt_enabled.enabled
512 #if OMPD_SUPPORT
513       && ompt_start_tool_result && ompt_start_tool_result->finalize
514 #endif
515   ) {
516     ompt_start_tool_result->finalize(&(ompt_start_tool_result->tool_data));
517   }
518 
519   if (ompt_tool_module)
520     OMPT_DLCLOSE(ompt_tool_module);
521   memset(&ompt_enabled, 0, sizeof(ompt_enabled));
522 }
523 
524 /*****************************************************************************
525  * interface operations
526  ****************************************************************************/
527 
528 /*****************************************************************************
529  * state
530  ****************************************************************************/
531 
532 OMPT_API_ROUTINE int ompt_enumerate_states(int current_state, int *next_state,
533                                            const char **next_state_name) {
534   const static int len = sizeof(ompt_state_info) / sizeof(ompt_state_info_t);
535   int i = 0;
536 
537   for (i = 0; i < len - 1; i++) {
538     if (ompt_state_info[i].state_id == current_state) {
539       *next_state = ompt_state_info[i + 1].state_id;
540       *next_state_name = ompt_state_info[i + 1].state_name;
541       return 1;
542     }
543   }
544 
545   return 0;
546 }
547 
548 OMPT_API_ROUTINE int ompt_enumerate_mutex_impls(int current_impl,
549                                                 int *next_impl,
550                                                 const char **next_impl_name) {
551   const static int len =
552       sizeof(kmp_mutex_impl_info) / sizeof(kmp_mutex_impl_info_t);
553   int i = 0;
554   for (i = 0; i < len - 1; i++) {
555     if (kmp_mutex_impl_info[i].id != current_impl)
556       continue;
557     *next_impl = kmp_mutex_impl_info[i + 1].id;
558     *next_impl_name = kmp_mutex_impl_info[i + 1].name;
559     return 1;
560   }
561   return 0;
562 }
563 
564 /*****************************************************************************
565  * callbacks
566  ****************************************************************************/
567 
568 OMPT_API_ROUTINE ompt_set_result_t ompt_set_callback(ompt_callbacks_t which,
569                                                      ompt_callback_t callback) {
570   switch (which) {
571 
572 #define ompt_event_macro(event_name, callback_type, event_id)                  \
573   case event_name:                                                             \
574     ompt_callbacks.ompt_callback(event_name) = (callback_type)callback;        \
575     ompt_enabled.event_name = (callback != 0);                                 \
576     if (callback)                                                              \
577       return ompt_event_implementation_status(event_name);                     \
578     else                                                                       \
579       return ompt_set_always;
580 
581     FOREACH_OMPT_EVENT(ompt_event_macro)
582 
583 #undef ompt_event_macro
584 
585   default:
586     return ompt_set_error;
587   }
588 }
589 
590 OMPT_API_ROUTINE int ompt_get_callback(ompt_callbacks_t which,
591                                        ompt_callback_t *callback) {
592   if (!ompt_enabled.enabled)
593     return ompt_get_callback_failure;
594 
595   switch (which) {
596 
597 #define ompt_event_macro(event_name, callback_type, event_id)                  \
598   case event_name: {                                                           \
599     ompt_callback_t mycb =                                                     \
600         (ompt_callback_t)ompt_callbacks.ompt_callback(event_name);             \
601     if (ompt_enabled.event_name && mycb) {                                     \
602       *callback = mycb;                                                        \
603       return ompt_get_callback_success;                                        \
604     }                                                                          \
605     return ompt_get_callback_failure;                                          \
606   }
607 
608     FOREACH_OMPT_EVENT(ompt_event_macro)
609 
610 #undef ompt_event_macro
611 
612   default:
613     return ompt_get_callback_failure;
614   }
615 }
616 
617 /*****************************************************************************
618  * parallel regions
619  ****************************************************************************/
620 
621 OMPT_API_ROUTINE int ompt_get_parallel_info(int ancestor_level,
622                                             ompt_data_t **parallel_data,
623                                             int *team_size) {
624   if (!ompt_enabled.enabled)
625     return 0;
626   return __ompt_get_parallel_info_internal(ancestor_level, parallel_data,
627                                            team_size);
628 }
629 
630 OMPT_API_ROUTINE int ompt_get_state(ompt_wait_id_t *wait_id) {
631   if (!ompt_enabled.enabled)
632     return ompt_state_work_serial;
633   int thread_state = __ompt_get_state_internal(wait_id);
634 
635   if (thread_state == ompt_state_undefined) {
636     thread_state = ompt_state_work_serial;
637   }
638 
639   return thread_state;
640 }
641 
642 /*****************************************************************************
643  * tasks
644  ****************************************************************************/
645 
646 OMPT_API_ROUTINE ompt_data_t *ompt_get_thread_data(void) {
647   if (!ompt_enabled.enabled)
648     return NULL;
649   return __ompt_get_thread_data_internal();
650 }
651 
652 OMPT_API_ROUTINE int ompt_get_task_info(int ancestor_level, int *type,
653                                         ompt_data_t **task_data,
654                                         ompt_frame_t **task_frame,
655                                         ompt_data_t **parallel_data,
656                                         int *thread_num) {
657   if (!ompt_enabled.enabled)
658     return 0;
659   return __ompt_get_task_info_internal(ancestor_level, type, task_data,
660                                        task_frame, parallel_data, thread_num);
661 }
662 
663 OMPT_API_ROUTINE int ompt_get_task_memory(void **addr, size_t *size,
664                                           int block) {
665   return __ompt_get_task_memory_internal(addr, size, block);
666 }
667 
668 /*****************************************************************************
669  * num_procs
670  ****************************************************************************/
671 
672 OMPT_API_ROUTINE int ompt_get_num_procs(void) {
673   // copied from kmp_ftn_entry.h (but modified: OMPT can only be called when
674   // runtime is initialized)
675   return __kmp_avail_proc;
676 }
677 
678 /*****************************************************************************
679  * places
680  ****************************************************************************/
681 
682 OMPT_API_ROUTINE int ompt_get_num_places(void) {
683 // copied from kmp_ftn_entry.h (but modified)
684 #if !KMP_AFFINITY_SUPPORTED
685   return 0;
686 #else
687   if (!KMP_AFFINITY_CAPABLE())
688     return 0;
689   return __kmp_affinity_num_masks;
690 #endif
691 }
692 
693 OMPT_API_ROUTINE int ompt_get_place_proc_ids(int place_num, int ids_size,
694                                              int *ids) {
695 // copied from kmp_ftn_entry.h (but modified)
696 #if !KMP_AFFINITY_SUPPORTED
697   return 0;
698 #else
699   int i, count;
700   int tmp_ids[ids_size];
701   for (int j = 0; j < ids_size; j++)
702     tmp_ids[j] = 0;
703   if (!KMP_AFFINITY_CAPABLE())
704     return 0;
705   if (place_num < 0 || place_num >= (int)__kmp_affinity_num_masks)
706     return 0;
707   /* TODO: Is this safe for asynchronous call from signal handler during runtime
708    * shutdown? */
709   kmp_affin_mask_t *mask = KMP_CPU_INDEX(__kmp_affinity_masks, place_num);
710   count = 0;
711   KMP_CPU_SET_ITERATE(i, mask) {
712     if ((!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) ||
713         (!KMP_CPU_ISSET(i, mask))) {
714       continue;
715     }
716     if (count < ids_size)
717       tmp_ids[count] = i;
718     count++;
719   }
720   if (ids_size >= count) {
721     for (i = 0; i < count; i++) {
722       ids[i] = tmp_ids[i];
723     }
724   }
725   return count;
726 #endif
727 }
728 
729 OMPT_API_ROUTINE int ompt_get_place_num(void) {
730 // copied from kmp_ftn_entry.h (but modified)
731 #if !KMP_AFFINITY_SUPPORTED
732   return -1;
733 #else
734   if (!ompt_enabled.enabled || __kmp_get_gtid() < 0)
735     return -1;
736 
737   int gtid;
738   kmp_info_t *thread;
739   if (!KMP_AFFINITY_CAPABLE())
740     return -1;
741   gtid = __kmp_entry_gtid();
742   thread = __kmp_thread_from_gtid(gtid);
743   if (thread == NULL || thread->th.th_current_place < 0)
744     return -1;
745   return thread->th.th_current_place;
746 #endif
747 }
748 
749 OMPT_API_ROUTINE int ompt_get_partition_place_nums(int place_nums_size,
750                                                    int *place_nums) {
751 // copied from kmp_ftn_entry.h (but modified)
752 #if !KMP_AFFINITY_SUPPORTED
753   return 0;
754 #else
755   if (!ompt_enabled.enabled || __kmp_get_gtid() < 0)
756     return 0;
757 
758   int i, gtid, place_num, first_place, last_place, start, end;
759   kmp_info_t *thread;
760   if (!KMP_AFFINITY_CAPABLE())
761     return 0;
762   gtid = __kmp_entry_gtid();
763   thread = __kmp_thread_from_gtid(gtid);
764   if (thread == NULL)
765     return 0;
766   first_place = thread->th.th_first_place;
767   last_place = thread->th.th_last_place;
768   if (first_place < 0 || last_place < 0)
769     return 0;
770   if (first_place <= last_place) {
771     start = first_place;
772     end = last_place;
773   } else {
774     start = last_place;
775     end = first_place;
776   }
777   if (end - start <= place_nums_size)
778     for (i = 0, place_num = start; place_num <= end; ++place_num, ++i) {
779       place_nums[i] = place_num;
780     }
781   return end - start + 1;
782 #endif
783 }
784 
785 /*****************************************************************************
786  * places
787  ****************************************************************************/
788 
789 OMPT_API_ROUTINE int ompt_get_proc_id(void) {
790   if (!ompt_enabled.enabled || __kmp_get_gtid() < 0)
791     return -1;
792 #if KMP_OS_LINUX
793   return sched_getcpu();
794 #elif KMP_OS_WINDOWS
795   PROCESSOR_NUMBER pn;
796   GetCurrentProcessorNumberEx(&pn);
797   return 64 * pn.Group + pn.Number;
798 #else
799   return -1;
800 #endif
801 }
802 
803 /*****************************************************************************
804  * compatability
805  ****************************************************************************/
806 
807 /*
808  * Currently unused function
809 OMPT_API_ROUTINE int ompt_get_ompt_version() { return OMPT_VERSION; }
810 */
811 
812 /*****************************************************************************
813  * application-facing API
814  ****************************************************************************/
815 
816 /*----------------------------------------------------------------------------
817  | control
818  ---------------------------------------------------------------------------*/
819 
820 int __kmp_control_tool(uint64_t command, uint64_t modifier, void *arg) {
821 
822   if (ompt_enabled.enabled) {
823     if (ompt_enabled.ompt_callback_control_tool) {
824       return ompt_callbacks.ompt_callback(ompt_callback_control_tool)(
825           command, modifier, arg, OMPT_LOAD_RETURN_ADDRESS(__kmp_entry_gtid()));
826     } else {
827       return -1;
828     }
829   } else {
830     return -2;
831   }
832 }
833 
834 /*****************************************************************************
835  * misc
836  ****************************************************************************/
837 
838 OMPT_API_ROUTINE uint64_t ompt_get_unique_id(void) {
839   return __ompt_get_unique_id_internal();
840 }
841 
842 OMPT_API_ROUTINE void ompt_finalize_tool(void) { __kmp_internal_end_atexit(); }
843 
844 /*****************************************************************************
845  * Target
846  ****************************************************************************/
847 
848 OMPT_API_ROUTINE int ompt_get_target_info(uint64_t *device_num,
849                                           ompt_id_t *target_id,
850                                           ompt_id_t *host_op_id) {
851   return 0; // thread is not in a target region
852 }
853 
854 OMPT_API_ROUTINE int ompt_get_num_devices(void) {
855   return 1; // only one device (the current device) is available
856 }
857 
858 /*****************************************************************************
859  * API inquiry for tool
860  ****************************************************************************/
861 
862 static ompt_interface_fn_t ompt_fn_lookup(const char *s) {
863 
864 #define ompt_interface_fn(fn)                                                  \
865   fn##_t fn##_f = fn;                                                          \
866   if (strcmp(s, #fn) == 0)                                                     \
867     return (ompt_interface_fn_t)fn##_f;
868 
869   FOREACH_OMPT_INQUIRY_FN(ompt_interface_fn)
870 
871   return NULL;
872 }
873