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