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_EVENT(ompt_event_macro)
601
602#undef ompt_event_macro
603
604 default:
605 return ompt_set_error;
606 }
607}
608
609OMPT_API_ROUTINE int ompt_get_callback(ompt_callbacks_t which,
610 ompt_callback_t *callback) {
611 if (!ompt_enabled.enabled)
612 return ompt_get_callback_failure;
613
614 switch (which) {
615
616#define ompt_event_macro(event_name, callback_type, event_id) \
617 case event_name: { \
618 ompt_callback_t mycb = \
619 (ompt_callback_t)ompt_callbacks.ompt_callback(event_name); \
620 if (ompt_enabled.event_name && mycb) { \
621 *callback = mycb; \
622 return ompt_get_callback_success; \
623 } \
624 return ompt_get_callback_failure; \
625 }
626
627 FOREACH_OMPT_EVENT(ompt_event_macro)
628
629#undef ompt_event_macro
630
631 default:
632 return ompt_get_callback_failure;
633 }
634}
635
636/*****************************************************************************
637 * parallel regions
638 ****************************************************************************/
639
640OMPT_API_ROUTINE int ompt_get_parallel_info(int ancestor_level,
641 ompt_data_t **parallel_data,
642 int *team_size) {
643 if (!ompt_enabled.enabled)
644 return 0;
645 return __ompt_get_parallel_info_internal(ancestor_level, parallel_data,
646 team_size);
647}
648
649OMPT_API_ROUTINE int ompt_get_state(ompt_wait_id_t *wait_id) {
650 if (!ompt_enabled.enabled)
651 return ompt_state_work_serial;
652 int thread_state = __ompt_get_state_internal(wait_id);
653
654 if (thread_state == ompt_state_undefined) {
655 thread_state = ompt_state_work_serial;
656 }
657
658 return thread_state;
659}
660
661/*****************************************************************************
662 * tasks
663 ****************************************************************************/
664
665OMPT_API_ROUTINE ompt_data_t *ompt_get_thread_data(void) {
666 if (!ompt_enabled.enabled)
667 return NULL;
668 return __ompt_get_thread_data_internal();
669}
670
671OMPT_API_ROUTINE int ompt_get_task_info(int ancestor_level, int *type,
672 ompt_data_t **task_data,
673 ompt_frame_t **task_frame,
674 ompt_data_t **parallel_data,
675 int *thread_num) {
676 if (!ompt_enabled.enabled)
677 return 0;
678 return __ompt_get_task_info_internal(ancestor_level, type, task_data,
679 task_frame, parallel_data, thread_num);
680}
681
682OMPT_API_ROUTINE int ompt_get_task_memory(void **addr, size_t *size,
683 int block) {
684 return __ompt_get_task_memory_internal(addr, size, block);
685}
686
687/*****************************************************************************
688 * num_procs
689 ****************************************************************************/
690
691OMPT_API_ROUTINE int ompt_get_num_procs(void) {
692 // copied from kmp_ftn_entry.h (but modified: OMPT can only be called when
693 // runtime is initialized)
694 return __kmp_avail_proc;
695}
696
697/*****************************************************************************
698 * places
699 ****************************************************************************/
700
701OMPT_API_ROUTINE int ompt_get_num_places(void) {
702// copied from kmp_ftn_entry.h (but modified)
703#if !KMP_AFFINITY_SUPPORTED
704 return 0;
705#else
706 if (!KMP_AFFINITY_CAPABLE())
707 return 0;
708 return __kmp_affinity.num_masks;
709#endif
710}
711
712OMPT_API_ROUTINE int ompt_get_place_proc_ids(int place_num, int ids_size,
713 int *ids) {
714// copied from kmp_ftn_entry.h (but modified)
715#if !KMP_AFFINITY_SUPPORTED
716 return 0;
717#else
718 int i, count;
719 SimpleVLA<int> tmp_ids(ids_size);
720 for (int j = 0; j < ids_size; j++)
721 tmp_ids[j] = 0;
722 if (!KMP_AFFINITY_CAPABLE())
723 return 0;
724 if (place_num < 0 || place_num >= (int)__kmp_affinity.num_masks)
725 return 0;
726 /* TODO: Is this safe for asynchronous call from signal handler during runtime
727 * shutdown? */
728 kmp_affin_mask_t *mask = KMP_CPU_INDEX(__kmp_affinity.masks, place_num);
729 count = 0;
730 KMP_CPU_SET_ITERATE(i, mask) {
731 if ((!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) ||
732 (!KMP_CPU_ISSET(i, mask))) {
733 continue;
734 }
735 if (count < ids_size)
736 tmp_ids[count] = i;
737 count++;
738 }
739 if (ids_size >= count) {
740 for (i = 0; i < count; i++) {
741 ids[i] = tmp_ids[i];
742 }
743 }
744 return count;
745#endif
746}
747
748OMPT_API_ROUTINE int ompt_get_place_num(void) {
749// copied from kmp_ftn_entry.h (but modified)
750#if !KMP_AFFINITY_SUPPORTED
751 return -1;
752#else
753 if (!ompt_enabled.enabled || __kmp_get_gtid() < 0)
754 return -1;
755
756 int gtid;
757 kmp_info_t *thread;
758 if (!KMP_AFFINITY_CAPABLE())
759 return -1;
760 gtid = __kmp_entry_gtid();
761 thread = __kmp_thread_from_gtid(gtid);
762 if (thread == NULL || thread->th.th_current_place < 0)
763 return -1;
764 return thread->th.th_current_place;
765#endif
766}
767
768OMPT_API_ROUTINE int ompt_get_partition_place_nums(int place_nums_size,
769 int *place_nums) {
770// copied from kmp_ftn_entry.h (but modified)
771#if !KMP_AFFINITY_SUPPORTED
772 return 0;
773#else
774 if (!ompt_enabled.enabled || __kmp_get_gtid() < 0)
775 return 0;
776
777 int i, gtid, place_num, first_place, last_place, start, end;
778 kmp_info_t *thread;
779 if (!KMP_AFFINITY_CAPABLE())
780 return 0;
781 gtid = __kmp_entry_gtid();
782 thread = __kmp_thread_from_gtid(gtid);
783 if (thread == NULL)
784 return 0;
785 first_place = thread->th.th_first_place;
786 last_place = thread->th.th_last_place;
787 if (first_place < 0 || last_place < 0)
788 return 0;
789 if (first_place <= last_place) {
790 start = first_place;
791 end = last_place;
792 } else {
793 start = last_place;
794 end = first_place;
795 }
796 if (end - start <= place_nums_size)
797 for (i = 0, place_num = start; place_num <= end; ++place_num, ++i) {
798 place_nums[i] = place_num;
799 }
800 return end - start + 1;
801#endif
802}
803
804/*****************************************************************************
805 * places
806 ****************************************************************************/
807
808OMPT_API_ROUTINE int ompt_get_proc_id(void) {
809 if (!ompt_enabled.enabled || __kmp_get_gtid() < 0)
810 return -1;
811#if KMP_HAVE_SCHED_GETCPU
812 return sched_getcpu();
813#elif KMP_OS_WINDOWS
814 PROCESSOR_NUMBER pn;
815 GetCurrentProcessorNumberEx(&pn);
816 return 64 * pn.Group + pn.Number;
817#else
818 return -1;
819#endif
820}
821
822/*****************************************************************************
823 * compatability
824 ****************************************************************************/
825
826/*
827 * Currently unused function
828OMPT_API_ROUTINE int ompt_get_ompt_version() { return OMPT_VERSION; }
829*/
830
831/*****************************************************************************
832 * application-facing API
833 ****************************************************************************/
834
835/*----------------------------------------------------------------------------
836 | control
837 ---------------------------------------------------------------------------*/
838
839int __kmp_control_tool(uint64_t command, uint64_t modifier, void *arg) {
840
841 if (ompt_enabled.enabled) {
842 if (ompt_enabled.ompt_callback_control_tool) {
843 return ompt_callbacks.ompt_callback(ompt_callback_control_tool)(
844 command, modifier, arg, OMPT_LOAD_RETURN_ADDRESS(__kmp_entry_gtid()));
845 } else {
846 return -1;
847 }
848 } else {
849 return -2;
850 }
851}
852
853/*****************************************************************************
854 * misc
855 ****************************************************************************/
856
857OMPT_API_ROUTINE uint64_t ompt_get_unique_id(void) {
858 return __ompt_get_unique_id_internal();
859}
860
861OMPT_API_ROUTINE void ompt_finalize_tool(void) { __kmp_internal_end_atexit(); }
862
863/*****************************************************************************
864 * Target
865 ****************************************************************************/
866
867OMPT_API_ROUTINE int ompt_get_target_info(uint64_t *device_num,
868 ompt_id_t *target_id,
869 ompt_id_t *host_op_id) {
870 return 0; // thread is not in a target region
871}
872
873extern "C" int omp_get_num_devices(void);
874
875OMPT_API_ROUTINE int ompt_get_num_devices(void) {
876 return omp_get_num_devices();
877}
878
879/*****************************************************************************
880 * API inquiry for tool
881 ****************************************************************************/
882
883static ompt_interface_fn_t ompt_fn_lookup(const char *s) {
884
885#define ompt_interface_fn(fn) \
886 fn##_t fn##_f = fn; \
887 if (strcmp(s, #fn) == 0) \
888 return (ompt_interface_fn_t)fn##_f;
889
890 FOREACH_OMPT_INQUIRY_FN(ompt_interface_fn)
891
892#undef ompt_interface_fn
893
894 return NULL;
895}
896
897static ompt_data_t *ompt_get_task_data() { return __ompt_get_task_data(); }
898
899static ompt_data_t *ompt_get_target_task_data() {
900 return __ompt_get_target_task_data();
901}
902
904static ompt_interface_fn_t ompt_libomp_target_fn_lookup(const char *s) {
905#define provide_fn(fn) \
906 if (strcmp(s, #fn) == 0) \
907 return (ompt_interface_fn_t)fn;
908
909 provide_fn(ompt_get_callback);
910 provide_fn(ompt_get_task_data);
911 provide_fn(ompt_get_target_task_data);
912#undef provide_fn
913
914#define ompt_interface_fn(fn, type, code) \
915 if (strcmp(s, #fn) == 0) \
916 return (ompt_interface_fn_t)ompt_callbacks.ompt_callback(fn);
917
918 FOREACH_OMPT_DEVICE_EVENT(ompt_interface_fn)
919 FOREACH_OMPT_EMI_EVENT(ompt_interface_fn)
920 FOREACH_OMPT_NOEMI_EVENT(ompt_interface_fn)
921#undef ompt_interface_fn
922
923 return (ompt_interface_fn_t)0;
924}
925
928_OMP_EXTERN void ompt_libomp_connect(ompt_start_tool_result_t *result) {
929 OMPT_VERBOSE_INIT_PRINT("libomp --> OMPT: Enter ompt_libomp_connect\n");
930
931 // Ensure libomp callbacks have been added if not already
932 __ompt_force_initialization();
933
934 if (ompt_enabled.enabled && result) {
935 OMPT_VERBOSE_INIT_PRINT("libomp --> OMPT: Connecting with libomptarget\n");
936 // Pass in the libomp lookup function so that the already registered
937 // functions can be extracted and assigned to the callbacks in
938 // libomptarget
939 result->initialize(ompt_libomp_target_fn_lookup,
940 /* initial_device_num */ omp_initial_device,
941 /* tool_data */ nullptr);
942 // Track the object provided by libomptarget so that the finalizer can be
943 // called during OMPT finalization
944 libomptarget_ompt_result = result;
945 }
946 OMPT_VERBOSE_INIT_PRINT("libomp --> OMPT: Exit ompt_libomp_connect\n");
947}