32#include "ompt-specific.cpp"
38#define ompt_get_callback_success 1
39#define ompt_get_callback_failure 0
41#define no_tool_present 0
43#define OMPT_API_ROUTINE static
46#define OMPT_STR_MATCH(haystack, needle) (!strcasecmp(haystack, needle))
53#define OMPT_VERBOSE_INIT_PRINT(...) \
55 fprintf(verbose_file, __VA_ARGS__)
56#define OMPT_VERBOSE_INIT_CONTINUED_PRINT(...) \
58 fprintf(verbose_file, __VA_ARGS__)
60static FILE *verbose_file;
61static int verbose_init;
64#define OMPT_GETENV secure_getenv
66#define OMPT_GETENV getenv
74 const char *state_name;
75 ompt_state_t state_id;
81} kmp_mutex_impl_info_t;
94ompt_callbacks_active_t ompt_enabled;
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
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
108ompt_callbacks_internal_t ompt_callbacks;
110static ompt_start_tool_result_t *ompt_start_tool_result = NULL;
113static HMODULE ompt_tool_module = NULL;
114static HMODULE ompt_archer_module = NULL;
115#define OMPT_DLCLOSE(Lib) FreeLibrary(Lib)
117static void *ompt_tool_module = NULL;
118static void *ompt_archer_module = NULL;
119#define OMPT_DLCLOSE(Lib) dlclose(Lib)
123static ompt_start_tool_result_t *libomptarget_ompt_result = NULL;
129static ompt_interface_fn_t ompt_fn_lookup(
const char *s);
131OMPT_API_ROUTINE ompt_data_t *ompt_get_thread_data(
void);
137typedef ompt_start_tool_result_t *(*ompt_start_tool_t)(
unsigned int,
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;
155 ompt_start_tool_t start_tool =
156 (ompt_start_tool_t)dlsym(RTLD_DEFAULT,
"ompt_start_tool");
158 ret = start_tool(omp_version, runtime_version);
163#elif OMPT_HAVE_WEAK_ATTRIBUTE
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;
176 ompt_start_tool_t next_tool =
177 (ompt_start_tool_t)dlsym(RTLD_NEXT,
"ompt_start_tool");
179 ret = next_tool(omp_version, runtime_version);
192#pragma comment(lib, "psapi.lib")
195#define NUM_MODULES 128
197static ompt_start_tool_result_t *
198ompt_tool_windows(
unsigned int omp_version,
const char *runtime_version) {
200 DWORD needed, new_size;
202 HANDLE process = GetCurrentProcess();
203 modules = (HMODULE *)malloc(NUM_MODULES *
sizeof(HMODULE));
204 ompt_start_tool_t ompt_tool_p = NULL;
207 printf(
"ompt_tool_windows(): looking for ompt_start_tool\n");
209 if (!EnumProcessModules(process, modules, NUM_MODULES *
sizeof(HMODULE),
216 new_size = needed /
sizeof(HMODULE);
217 if (new_size > NUM_MODULES) {
219 printf(
"ompt_tool_windows(): resize buffer to %d bytes\n", needed);
221 modules = (HMODULE *)realloc(modules, needed);
223 if (!EnumProcessModules(process, modules, needed, &needed)) {
228 for (i = 0; i < new_size; ++i) {
229 (FARPROC &)ompt_tool_p = GetProcAddress(modules[i],
"ompt_start_tool");
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",
238 return (*ompt_tool_p)(omp_version, runtime_version);
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",
253#error Activation of OMPT is not supported on this platform.
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;
262 const char *sep =
";";
264 const char *sep =
":";
267 OMPT_VERBOSE_INIT_PRINT(
"----- START LOGGING OF TOOL REGISTRATION -----\n");
268 OMPT_VERBOSE_INIT_PRINT(
"Search for OMP tool in 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);
276 ret = ompt_tool_windows(omp_version, runtime_version);
278#error Activation of OMPT is not supported on this platform.
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");
289 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed.\n");
290 const char *tool_libs = OMPT_GETENV(
"OMP_TOOL_LIBRARIES");
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);
296 char *fname = __kmp_str_token(libs, sep, &buf);
302 OMPT_VERBOSE_INIT_PRINT(
"Opening %s... ", fname);
303 void *h = dlopen(fname, RTLD_LAZY);
305 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed: %s\n", dlerror());
307 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Success. \n");
308 OMPT_VERBOSE_INIT_PRINT(
"Searching for ompt_start_tool in %s... ",
311 start_tool = (ompt_start_tool_t)dlsym(h,
"ompt_start_tool");
313 char *error = dlerror();
315 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed: %s\n", error);
317 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed: %s\n",
318 "ompt_start_tool = NULL");
322 OMPT_VERBOSE_INIT_PRINT(
"Opening %s... ", fname);
323 HMODULE h = LoadLibrary(fname);
325 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed: Error %u\n",
326 (
unsigned)GetLastError());
328 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Success. \n");
329 OMPT_VERBOSE_INIT_PRINT(
"Searching for ompt_start_tool in %s... ",
331 start_tool = (ompt_start_tool_t)GetProcAddress(h,
"ompt_start_tool");
333 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed: Error %u\n",
334 (
unsigned)GetLastError());
337#error Activation of OMPT is not supported on this platform.
340 ret = (*start_tool)(omp_version, runtime_version);
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;
348 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
349 "Found but not using the OMPT interface.\n");
350 OMPT_VERBOSE_INIT_PRINT(
"Continuing search...\n");
354 fname = __kmp_str_token(NULL, sep, &buf);
356 __kmp_str_free(&libs);
358 OMPT_VERBOSE_INIT_PRINT(
"No OMP_TOOL_LIBRARIES defined.\n");
363 OMPT_VERBOSE_INIT_PRINT(
"----- END LOGGING OF TOOL REGISTRATION -----\n");
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);
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");
379 ret = (*start_tool)(omp_version, runtime_version);
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;
389 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
390 "Found but not using the OMPT interface.\n");
392 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed: %s\n", dlerror());
398 OMPT_VERBOSE_INIT_PRINT(
"No OMP tool loaded.\n");
399 OMPT_VERBOSE_INIT_PRINT(
"----- END LOGGING OF TOOL REGISTRATION -----\n");
403void ompt_pre_init() {
407 static int ompt_pre_initialized = 0;
409 if (ompt_pre_initialized)
412 ompt_pre_initialized = 1;
417 const char *ompt_env_var = getenv(
"OMP_TOOL");
418 tool_setting_e tool_setting = omp_tool_error;
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;
427 const char *ompt_env_verbose_init = getenv(
"OMP_TOOL_VERBOSE_INIT");
430 if (ompt_env_verbose_init && strcmp(ompt_env_verbose_init,
"") &&
431 !OMPT_STR_MATCH(ompt_env_verbose_init,
"disabled")) {
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;
438 verbose_file = fopen(ompt_env_verbose_init,
"w");
443 printf(
"ompt_pre_init(): tool_setting = %d\n", tool_setting);
445 switch (tool_setting) {
446 case omp_tool_disabled:
447 OMPT_VERBOSE_INIT_PRINT(
"OMP tool disabled. \n");
451 case omp_tool_enabled:
456 ompt_start_tool_result =
457 ompt_try_start_tool(__kmp_openmp_version, ompt_get_runtime_version());
459 memset(&ompt_enabled, 0,
sizeof(ompt_enabled));
464 "Warning: OMP_TOOL has invalid value \"%s\".\n"
465 " legal values are (NULL,\"\",\"disabled\","
470 if (verbose_init && verbose_file != stderr && verbose_file != stdout)
471 fclose(verbose_file);
473 printf(
"ompt_pre_init(): ompt_enabled = %d\n", ompt_enabled.enabled);
477#define omp_initial_device -1
479void ompt_post_init() {
483 static int ompt_post_initialized = 0;
485 if (ompt_post_initialized)
488 ompt_post_initialized = 1;
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));
498 if (!ompt_enabled.enabled) {
500 memset(&ompt_enabled, 0,
sizeof(ompt_enabled));
504 kmp_info_t *root_thread = ompt_get_thread();
506 ompt_set_thread_state(root_thread, ompt_state_overhead);
507 __ompt_task_init(root_thread->th.th_current_task, 0);
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());
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, ¶llel_data,
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);
522 ompt_set_thread_state(root_thread, ompt_state_work_serial);
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));
531 if (libomptarget_ompt_result && libomptarget_ompt_result->finalize) {
532 libomptarget_ompt_result->finalize(NULL);
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));
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);
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;
567OMPT_API_ROUTINE
int ompt_enumerate_mutex_impls(
int current_impl,
569 const char **next_impl_name) {
570 const static int len =
571 sizeof(kmp_mutex_impl_info) /
sizeof(kmp_mutex_impl_info_t);
573 for (i = 0; i < len - 1; i++) {
574 if (kmp_mutex_impl_info[i].
id != current_impl)
576 *next_impl = kmp_mutex_impl_info[i + 1].id;
577 *next_impl_name = kmp_mutex_impl_info[i + 1].name;
587OMPT_API_ROUTINE ompt_set_result_t ompt_set_callback(ompt_callbacks_t which,
588 ompt_callback_t callback) {
591#define ompt_event_macro(event_name, callback_type, event_id) \
593 ompt_callbacks.ompt_callback(event_name) = (callback_type)callback; \
594 ompt_enabled.event_name = (callback != 0); \
596 return ompt_event_implementation_status(event_name); \
598 return ompt_set_always;
600 FOREACH_OMPT_EVENT(ompt_event_macro)
602#undef ompt_event_macro
605 return ompt_set_error;
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;
616#define ompt_event_macro(event_name, callback_type, event_id) \
618 ompt_callback_t mycb = \
619 (ompt_callback_t)ompt_callbacks.ompt_callback(event_name); \
620 if (ompt_enabled.event_name && mycb) { \
622 return ompt_get_callback_success; \
624 return ompt_get_callback_failure; \
627 FOREACH_OMPT_EVENT(ompt_event_macro)
629#undef ompt_event_macro
632 return ompt_get_callback_failure;
640OMPT_API_ROUTINE
int ompt_get_parallel_info(
int ancestor_level,
641 ompt_data_t **parallel_data,
643 if (!ompt_enabled.enabled)
645 return __ompt_get_parallel_info_internal(ancestor_level, parallel_data,
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);
654 if (thread_state == ompt_state_undefined) {
655 thread_state = ompt_state_work_serial;
665OMPT_API_ROUTINE ompt_data_t *ompt_get_thread_data(
void) {
666 if (!ompt_enabled.enabled)
668 return __ompt_get_thread_data_internal();
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,
676 if (!ompt_enabled.enabled)
678 return __ompt_get_task_info_internal(ancestor_level, type, task_data,
679 task_frame, parallel_data, thread_num);
682OMPT_API_ROUTINE
int ompt_get_task_memory(
void **addr,
size_t *size,
684 return __ompt_get_task_memory_internal(addr, size, block);
691OMPT_API_ROUTINE
int ompt_get_num_procs(
void) {
694 return __kmp_avail_proc;
701OMPT_API_ROUTINE
int ompt_get_num_places(
void) {
703#if !KMP_AFFINITY_SUPPORTED
706 if (!KMP_AFFINITY_CAPABLE())
708 return __kmp_affinity.num_masks;
712OMPT_API_ROUTINE
int ompt_get_place_proc_ids(
int place_num,
int ids_size,
715#if !KMP_AFFINITY_SUPPORTED
720 for (
int j = 0; j < ids_size; j++)
722 if (!KMP_AFFINITY_CAPABLE())
724 if (place_num < 0 || place_num >= (
int)__kmp_affinity.num_masks)
728 kmp_affin_mask_t *mask = KMP_CPU_INDEX(__kmp_affinity.masks, place_num);
730 KMP_CPU_SET_ITERATE(i, mask) {
731 if ((!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) ||
732 (!KMP_CPU_ISSET(i, mask))) {
735 if (count < ids_size)
739 if (ids_size >= count) {
740 for (i = 0; i < count; i++) {
748OMPT_API_ROUTINE
int ompt_get_place_num(
void) {
750#if !KMP_AFFINITY_SUPPORTED
753 if (!ompt_enabled.enabled || __kmp_get_gtid() < 0)
758 if (!KMP_AFFINITY_CAPABLE())
760 gtid = __kmp_entry_gtid();
761 thread = __kmp_thread_from_gtid(gtid);
762 if (thread == NULL || thread->th.th_current_place < 0)
764 return thread->th.th_current_place;
768OMPT_API_ROUTINE
int ompt_get_partition_place_nums(
int place_nums_size,
771#if !KMP_AFFINITY_SUPPORTED
774 if (!ompt_enabled.enabled || __kmp_get_gtid() < 0)
777 int i, gtid, place_num, first_place, last_place, start, end;
779 if (!KMP_AFFINITY_CAPABLE())
781 gtid = __kmp_entry_gtid();
782 thread = __kmp_thread_from_gtid(gtid);
785 first_place = thread->th.th_first_place;
786 last_place = thread->th.th_last_place;
787 if (first_place < 0 || last_place < 0)
789 if (first_place <= last_place) {
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;
800 return end - start + 1;
808OMPT_API_ROUTINE
int ompt_get_proc_id(
void) {
809 if (!ompt_enabled.enabled || __kmp_get_gtid() < 0)
811#if KMP_HAVE_SCHED_GETCPU
812 return sched_getcpu();
815 GetCurrentProcessorNumberEx(&pn);
816 return 64 * pn.Group + pn.Number;
839int __kmp_control_tool(uint64_t command, uint64_t modifier,
void *arg) {
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()));
857OMPT_API_ROUTINE uint64_t ompt_get_unique_id(
void) {
858 return __ompt_get_unique_id_internal();
861OMPT_API_ROUTINE
void ompt_finalize_tool(
void) { __kmp_internal_end_atexit(); }
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) {
873extern "C" int omp_get_num_devices(
void);
875OMPT_API_ROUTINE
int ompt_get_num_devices(
void) {
876 return omp_get_num_devices();
883static ompt_interface_fn_t ompt_fn_lookup(
const char *s) {
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;
890 FOREACH_OMPT_INQUIRY_FN(ompt_interface_fn)
892#undef ompt_interface_fn
897static ompt_data_t *ompt_get_task_data() {
return __ompt_get_task_data(); }
899static ompt_data_t *ompt_get_target_task_data() {
900 return __ompt_get_target_task_data();
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;
909 provide_fn(ompt_get_callback);
910 provide_fn(ompt_get_task_data);
911 provide_fn(ompt_get_target_task_data);
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);
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
923 return (ompt_interface_fn_t)0;
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");
932 __ompt_force_initialization();
934 if (ompt_enabled.enabled && result) {
935 OMPT_VERBOSE_INIT_PRINT(
"libomp --> OMPT: Connecting with libomptarget\n");
939 result->initialize(ompt_libomp_target_fn_lookup,
944 libomptarget_ompt_result = result;
946 OMPT_VERBOSE_INIT_PRINT(
"libomp --> OMPT: Exit ompt_libomp_connect\n");