31#include "ompt-specific.cpp"
37#define ompt_get_callback_success 1
38#define ompt_get_callback_failure 0
40#define no_tool_present 0
42#define OMPT_API_ROUTINE static
45#define OMPT_STR_MATCH(haystack, needle) (!strcasecmp(haystack, needle))
52#define OMPT_VERBOSE_INIT_PRINT(...) \
54 fprintf(verbose_file, __VA_ARGS__)
55#define OMPT_VERBOSE_INIT_CONTINUED_PRINT(...) \
57 fprintf(verbose_file, __VA_ARGS__)
59static FILE *verbose_file;
60static int verbose_init;
67 const char *state_name;
68 ompt_state_t state_id;
74} kmp_mutex_impl_info_t;
87ompt_callbacks_active_t ompt_enabled;
89ompt_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
95kmp_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
101ompt_callbacks_internal_t ompt_callbacks;
103static ompt_start_tool_result_t *ompt_start_tool_result = NULL;
106static HMODULE ompt_tool_module = NULL;
107static HMODULE ompt_archer_module = NULL;
108#define OMPT_DLCLOSE(Lib) FreeLibrary(Lib)
110static void *ompt_tool_module = NULL;
111static void *ompt_archer_module = NULL;
112#define OMPT_DLCLOSE(Lib) dlclose(Lib)
116static ompt_start_tool_result_t *libomptarget_ompt_result = NULL;
122static ompt_interface_fn_t ompt_fn_lookup(
const char *s);
124OMPT_API_ROUTINE ompt_data_t *ompt_get_thread_data(
void);
130typedef ompt_start_tool_result_t *(*ompt_start_tool_t)(
unsigned int,
144static ompt_start_tool_result_t *ompt_tool_darwin(
unsigned int omp_version,
145 const char *runtime_version) {
146 ompt_start_tool_result_t *ret = NULL;
148 ompt_start_tool_t start_tool =
149 (ompt_start_tool_t)dlsym(RTLD_DEFAULT,
"ompt_start_tool");
151 ret = start_tool(omp_version, runtime_version);
156#elif OMPT_HAVE_WEAK_ATTRIBUTE
162_OMP_EXTERN OMPT_WEAK_ATTRIBUTE ompt_start_tool_result_t *
163ompt_start_tool(
unsigned int omp_version,
const char *runtime_version) {
164 ompt_start_tool_result_t *ret = NULL;
169 ompt_start_tool_t next_tool =
170 (ompt_start_tool_t)dlsym(RTLD_NEXT,
"ompt_start_tool");
172 ret = next_tool(omp_version, runtime_version);
185#pragma comment(lib, "psapi.lib")
188#define NUM_MODULES 128
190static ompt_start_tool_result_t *
191ompt_tool_windows(
unsigned int omp_version,
const char *runtime_version) {
193 DWORD needed, new_size;
195 HANDLE process = GetCurrentProcess();
196 modules = (HMODULE *)malloc(NUM_MODULES *
sizeof(HMODULE));
197 ompt_start_tool_t ompt_tool_p = NULL;
200 printf(
"ompt_tool_windows(): looking for ompt_start_tool\n");
202 if (!EnumProcessModules(process, modules, NUM_MODULES *
sizeof(HMODULE),
209 new_size = needed /
sizeof(HMODULE);
210 if (new_size > NUM_MODULES) {
212 printf(
"ompt_tool_windows(): resize buffer to %d bytes\n", needed);
214 modules = (HMODULE *)realloc(modules, needed);
216 if (!EnumProcessModules(process, modules, needed, &needed)) {
221 for (i = 0; i < new_size; ++i) {
222 (FARPROC &)ompt_tool_p = GetProcAddress(modules[i],
"ompt_start_tool");
225 TCHAR modName[MAX_PATH];
226 if (GetModuleFileName(modules[i], modName, MAX_PATH))
227 printf(
"ompt_tool_windows(): ompt_start_tool found in module %s\n",
231 return (*ompt_tool_p)(omp_version, runtime_version);
235 TCHAR modName[MAX_PATH];
236 if (GetModuleFileName(modules[i], modName, MAX_PATH))
237 printf(
"ompt_tool_windows(): ompt_start_tool not found in module %s\n",
246#error Activation of OMPT is not supported on this platform.
249static ompt_start_tool_result_t *
250ompt_try_start_tool(
unsigned int omp_version,
const char *runtime_version) {
251 ompt_start_tool_result_t *ret = NULL;
252 ompt_start_tool_t start_tool = NULL;
255 const char *sep =
";";
257 const char *sep =
":";
260 OMPT_VERBOSE_INIT_PRINT(
"----- START LOGGING OF TOOL REGISTRATION -----\n");
261 OMPT_VERBOSE_INIT_PRINT(
"Search for OMP tool in current address space... ");
265 ret = ompt_tool_darwin(omp_version, runtime_version);
266#elif OMPT_HAVE_WEAK_ATTRIBUTE
267 ret = ompt_start_tool(omp_version, runtime_version);
269 ret = ompt_tool_windows(omp_version, runtime_version);
271#error Activation of OMPT is not supported on this platform.
274 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Success.\n");
275 OMPT_VERBOSE_INIT_PRINT(
276 "Tool was started and is using the OMPT interface.\n");
277 OMPT_VERBOSE_INIT_PRINT(
"----- END LOGGING OF TOOL REGISTRATION -----\n");
282 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed.\n");
283 const char *tool_libs = getenv(
"OMP_TOOL_LIBRARIES");
285 OMPT_VERBOSE_INIT_PRINT(
"Searching tool libraries...\n");
286 OMPT_VERBOSE_INIT_PRINT(
"OMP_TOOL_LIBRARIES = %s\n", tool_libs);
287 char *libs = __kmp_str_format(
"%s", tool_libs);
289 char *fname = __kmp_str_token(libs, sep, &buf);
295 OMPT_VERBOSE_INIT_PRINT(
"Opening %s... ", fname);
296 void *h = dlopen(fname, RTLD_LAZY);
298 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed: %s\n", dlerror());
300 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Success. \n");
301 OMPT_VERBOSE_INIT_PRINT(
"Searching for ompt_start_tool in %s... ",
304 start_tool = (ompt_start_tool_t)dlsym(h,
"ompt_start_tool");
306 char *error = dlerror();
308 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed: %s\n", error);
310 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed: %s\n",
311 "ompt_start_tool = NULL");
315 OMPT_VERBOSE_INIT_PRINT(
"Opening %s... ", fname);
316 HMODULE h = LoadLibrary(fname);
318 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed: Error %u\n",
319 (
unsigned)GetLastError());
321 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Success. \n");
322 OMPT_VERBOSE_INIT_PRINT(
"Searching for ompt_start_tool in %s... ",
324 start_tool = (ompt_start_tool_t)GetProcAddress(h,
"ompt_start_tool");
326 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed: Error %u\n",
327 (
unsigned)GetLastError());
330#error Activation of OMPT is not supported on this platform.
333 ret = (*start_tool)(omp_version, runtime_version);
335 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Success.\n");
336 OMPT_VERBOSE_INIT_PRINT(
337 "Tool was started and is using the OMPT interface.\n");
338 ompt_tool_module = h;
341 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
342 "Found but not using the OMPT interface.\n");
343 OMPT_VERBOSE_INIT_PRINT(
"Continuing search...\n");
347 fname = __kmp_str_token(NULL, sep, &buf);
349 __kmp_str_free(&libs);
351 OMPT_VERBOSE_INIT_PRINT(
"No OMP_TOOL_LIBRARIES defined.\n");
356 OMPT_VERBOSE_INIT_PRINT(
"----- END LOGGING OF TOOL REGISTRATION -----\n");
362 const char *fname =
"libarcher.so";
363 OMPT_VERBOSE_INIT_PRINT(
364 "...searching tool libraries failed. Using archer tool.\n");
365 OMPT_VERBOSE_INIT_PRINT(
"Opening %s... ", fname);
366 void *h = dlopen(fname, RTLD_LAZY);
368 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Success.\n");
369 OMPT_VERBOSE_INIT_PRINT(
"Searching for ompt_start_tool in %s... ", fname);
370 start_tool = (ompt_start_tool_t)dlsym(h,
"ompt_start_tool");
372 ret = (*start_tool)(omp_version, runtime_version);
374 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Success.\n");
375 OMPT_VERBOSE_INIT_PRINT(
376 "Tool was started and is using the OMPT interface.\n");
377 OMPT_VERBOSE_INIT_PRINT(
378 "----- END LOGGING OF TOOL REGISTRATION -----\n");
379 ompt_archer_module = h;
382 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
383 "Found but not using the OMPT interface.\n");
385 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed: %s\n", dlerror());
391 OMPT_VERBOSE_INIT_PRINT(
"No OMP tool loaded.\n");
392 OMPT_VERBOSE_INIT_PRINT(
"----- END LOGGING OF TOOL REGISTRATION -----\n");
396void ompt_pre_init() {
400 static int ompt_pre_initialized = 0;
402 if (ompt_pre_initialized)
405 ompt_pre_initialized = 1;
410 const char *ompt_env_var = getenv(
"OMP_TOOL");
411 tool_setting_e tool_setting = omp_tool_error;
413 if (!ompt_env_var || !strcmp(ompt_env_var,
""))
414 tool_setting = omp_tool_unset;
415 else if (OMPT_STR_MATCH(ompt_env_var,
"disabled"))
416 tool_setting = omp_tool_disabled;
417 else if (OMPT_STR_MATCH(ompt_env_var,
"enabled"))
418 tool_setting = omp_tool_enabled;
420 const char *ompt_env_verbose_init = getenv(
"OMP_TOOL_VERBOSE_INIT");
423 if (ompt_env_verbose_init && strcmp(ompt_env_verbose_init,
"") &&
424 !OMPT_STR_MATCH(ompt_env_verbose_init,
"disabled")) {
426 if (OMPT_STR_MATCH(ompt_env_verbose_init,
"STDERR"))
427 verbose_file = stderr;
428 else if (OMPT_STR_MATCH(ompt_env_verbose_init,
"STDOUT"))
429 verbose_file = stdout;
431 verbose_file = fopen(ompt_env_verbose_init,
"w");
436 printf(
"ompt_pre_init(): tool_setting = %d\n", tool_setting);
438 switch (tool_setting) {
439 case omp_tool_disabled:
440 OMPT_VERBOSE_INIT_PRINT(
"OMP tool disabled. \n");
444 case omp_tool_enabled:
449 ompt_start_tool_result =
450 ompt_try_start_tool(__kmp_openmp_version, ompt_get_runtime_version());
452 memset(&ompt_enabled, 0,
sizeof(ompt_enabled));
457 "Warning: OMP_TOOL has invalid value \"%s\".\n"
458 " legal values are (NULL,\"\",\"disabled\","
463 if (verbose_init && verbose_file != stderr && verbose_file != stdout)
464 fclose(verbose_file);
466 printf(
"ompt_pre_init(): ompt_enabled = %d\n", ompt_enabled.enabled);
470extern "C" int omp_get_initial_device(
void);
472void ompt_post_init() {
476 static int ompt_post_initialized = 0;
478 if (ompt_post_initialized)
481 ompt_post_initialized = 1;
486 if (ompt_start_tool_result) {
487 ompt_enabled.enabled = !!ompt_start_tool_result->initialize(
488 ompt_fn_lookup, omp_get_initial_device(),
489 &(ompt_start_tool_result->tool_data));
491 if (!ompt_enabled.enabled) {
493 memset(&ompt_enabled, 0,
sizeof(ompt_enabled));
497 kmp_info_t *root_thread = ompt_get_thread();
499 ompt_set_thread_state(root_thread, ompt_state_overhead);
501 if (ompt_enabled.ompt_callback_thread_begin) {
502 ompt_callbacks.ompt_callback(ompt_callback_thread_begin)(
503 ompt_thread_initial, __ompt_get_thread_data_internal());
505 ompt_data_t *task_data =
nullptr;
506 ompt_data_t *parallel_data =
nullptr;
507 __ompt_get_task_info_internal(0, NULL, &task_data, NULL, ¶llel_data,
509 if (ompt_enabled.ompt_callback_implicit_task) {
510 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
511 ompt_scope_begin, parallel_data, task_data, 1, 1, ompt_task_initial);
514 ompt_set_thread_state(root_thread, ompt_state_work_serial);
519 if (ompt_enabled.enabled) {
520 if (ompt_start_tool_result && ompt_start_tool_result->finalize) {
521 ompt_start_tool_result->finalize(&(ompt_start_tool_result->tool_data));
523 if (libomptarget_ompt_result && libomptarget_ompt_result->finalize) {
524 libomptarget_ompt_result->finalize(NULL);
528 if (ompt_archer_module)
529 OMPT_DLCLOSE(ompt_archer_module);
530 if (ompt_tool_module)
531 OMPT_DLCLOSE(ompt_tool_module);
532 memset(&ompt_enabled, 0,
sizeof(ompt_enabled));
543OMPT_API_ROUTINE
int ompt_enumerate_states(
int current_state,
int *next_state,
544 const char **next_state_name) {
545 const static int len =
sizeof(ompt_state_info) /
sizeof(ompt_state_info_t);
548 for (i = 0; i < len - 1; i++) {
549 if (ompt_state_info[i].state_id == current_state) {
550 *next_state = ompt_state_info[i + 1].state_id;
551 *next_state_name = ompt_state_info[i + 1].state_name;
559OMPT_API_ROUTINE
int ompt_enumerate_mutex_impls(
int current_impl,
561 const char **next_impl_name) {
562 const static int len =
563 sizeof(kmp_mutex_impl_info) /
sizeof(kmp_mutex_impl_info_t);
565 for (i = 0; i < len - 1; i++) {
566 if (kmp_mutex_impl_info[i].
id != current_impl)
568 *next_impl = kmp_mutex_impl_info[i + 1].id;
569 *next_impl_name = kmp_mutex_impl_info[i + 1].name;
579OMPT_API_ROUTINE ompt_set_result_t ompt_set_callback(ompt_callbacks_t which,
580 ompt_callback_t callback) {
583#define ompt_event_macro(event_name, callback_type, event_id) \
585 ompt_callbacks.ompt_callback(event_name) = (callback_type)callback; \
586 ompt_enabled.event_name = (callback != 0); \
588 return ompt_event_implementation_status(event_name); \
590 return ompt_set_always;
592 FOREACH_OMPT_EVENT(ompt_event_macro)
594#undef ompt_event_macro
597 return ompt_set_error;
601OMPT_API_ROUTINE
int ompt_get_callback(ompt_callbacks_t which,
602 ompt_callback_t *callback) {
603 if (!ompt_enabled.enabled)
604 return ompt_get_callback_failure;
608#define ompt_event_macro(event_name, callback_type, event_id) \
610 ompt_callback_t mycb = \
611 (ompt_callback_t)ompt_callbacks.ompt_callback(event_name); \
612 if (ompt_enabled.event_name && mycb) { \
614 return ompt_get_callback_success; \
616 return ompt_get_callback_failure; \
619 FOREACH_OMPT_EVENT(ompt_event_macro)
621#undef ompt_event_macro
624 return ompt_get_callback_failure;
632OMPT_API_ROUTINE
int ompt_get_parallel_info(
int ancestor_level,
633 ompt_data_t **parallel_data,
635 if (!ompt_enabled.enabled)
637 return __ompt_get_parallel_info_internal(ancestor_level, parallel_data,
641OMPT_API_ROUTINE
int ompt_get_state(ompt_wait_id_t *wait_id) {
642 if (!ompt_enabled.enabled)
643 return ompt_state_work_serial;
644 int thread_state = __ompt_get_state_internal(wait_id);
646 if (thread_state == ompt_state_undefined) {
647 thread_state = ompt_state_work_serial;
657OMPT_API_ROUTINE ompt_data_t *ompt_get_thread_data(
void) {
658 if (!ompt_enabled.enabled)
660 return __ompt_get_thread_data_internal();
663OMPT_API_ROUTINE
int ompt_get_task_info(
int ancestor_level,
int *type,
664 ompt_data_t **task_data,
665 ompt_frame_t **task_frame,
666 ompt_data_t **parallel_data,
668 if (!ompt_enabled.enabled)
670 return __ompt_get_task_info_internal(ancestor_level, type, task_data,
671 task_frame, parallel_data, thread_num);
674OMPT_API_ROUTINE
int ompt_get_task_memory(
void **addr,
size_t *size,
676 return __ompt_get_task_memory_internal(addr, size, block);
683OMPT_API_ROUTINE
int ompt_get_num_procs(
void) {
686 return __kmp_avail_proc;
693OMPT_API_ROUTINE
int ompt_get_num_places(
void) {
695#if !KMP_AFFINITY_SUPPORTED
698 if (!KMP_AFFINITY_CAPABLE())
700 return __kmp_affinity.num_masks;
704OMPT_API_ROUTINE
int ompt_get_place_proc_ids(
int place_num,
int ids_size,
707#if !KMP_AFFINITY_SUPPORTED
711 int tmp_ids[ids_size];
712 for (
int j = 0; j < ids_size; j++)
714 if (!KMP_AFFINITY_CAPABLE())
716 if (place_num < 0 || place_num >= (
int)__kmp_affinity.num_masks)
720 kmp_affin_mask_t *mask = KMP_CPU_INDEX(__kmp_affinity.masks, place_num);
722 KMP_CPU_SET_ITERATE(i, mask) {
723 if ((!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) ||
724 (!KMP_CPU_ISSET(i, mask))) {
727 if (count < ids_size)
731 if (ids_size >= count) {
732 for (i = 0; i < count; i++) {
740OMPT_API_ROUTINE
int ompt_get_place_num(
void) {
742#if !KMP_AFFINITY_SUPPORTED
745 if (!ompt_enabled.enabled || __kmp_get_gtid() < 0)
750 if (!KMP_AFFINITY_CAPABLE())
752 gtid = __kmp_entry_gtid();
753 thread = __kmp_thread_from_gtid(gtid);
754 if (thread == NULL || thread->th.th_current_place < 0)
756 return thread->th.th_current_place;
760OMPT_API_ROUTINE
int ompt_get_partition_place_nums(
int place_nums_size,
763#if !KMP_AFFINITY_SUPPORTED
766 if (!ompt_enabled.enabled || __kmp_get_gtid() < 0)
769 int i, gtid, place_num, first_place, last_place, start, end;
771 if (!KMP_AFFINITY_CAPABLE())
773 gtid = __kmp_entry_gtid();
774 thread = __kmp_thread_from_gtid(gtid);
777 first_place = thread->th.th_first_place;
778 last_place = thread->th.th_last_place;
779 if (first_place < 0 || last_place < 0)
781 if (first_place <= last_place) {
788 if (end - start <= place_nums_size)
789 for (i = 0, place_num = start; place_num <= end; ++place_num, ++i) {
790 place_nums[i] = place_num;
792 return end - start + 1;
800OMPT_API_ROUTINE
int ompt_get_proc_id(
void) {
801 if (!ompt_enabled.enabled || __kmp_get_gtid() < 0)
803#if KMP_HAVE_SCHED_GETCPU
804 return sched_getcpu();
807 GetCurrentProcessorNumberEx(&pn);
808 return 64 * pn.Group + pn.Number;
831int __kmp_control_tool(uint64_t command, uint64_t modifier,
void *arg) {
833 if (ompt_enabled.enabled) {
834 if (ompt_enabled.ompt_callback_control_tool) {
835 return ompt_callbacks.ompt_callback(ompt_callback_control_tool)(
836 command, modifier, arg, OMPT_LOAD_RETURN_ADDRESS(__kmp_entry_gtid()));
849OMPT_API_ROUTINE uint64_t ompt_get_unique_id(
void) {
850 return __ompt_get_unique_id_internal();
853OMPT_API_ROUTINE
void ompt_finalize_tool(
void) { __kmp_internal_end_atexit(); }
859OMPT_API_ROUTINE
int ompt_get_target_info(uint64_t *device_num,
860 ompt_id_t *target_id,
861 ompt_id_t *host_op_id) {
865OMPT_API_ROUTINE
int ompt_get_num_devices(
void) {
873static ompt_interface_fn_t ompt_fn_lookup(
const char *s) {
875#define ompt_interface_fn(fn) \
876 fn##_t fn##_f = fn; \
877 if (strcmp(s, #fn) == 0) \
878 return (ompt_interface_fn_t)fn##_f;
880 FOREACH_OMPT_INQUIRY_FN(ompt_interface_fn)
882#undef ompt_interface_fn
887static ompt_data_t *ompt_get_task_data() {
return __ompt_get_task_data(); }
889static ompt_data_t *ompt_get_target_task_data() {
890 return __ompt_get_target_task_data();
894static ompt_interface_fn_t ompt_libomp_target_fn_lookup(
const char *s) {
895#define provide_fn(fn) \
896 if (strcmp(s, #fn) == 0) \
897 return (ompt_interface_fn_t)fn;
899 provide_fn(ompt_get_callback);
900 provide_fn(ompt_get_task_data);
901 provide_fn(ompt_get_target_task_data);
904#define ompt_interface_fn(fn, type, code) \
905 if (strcmp(s, #fn) == 0) \
906 return (ompt_interface_fn_t)ompt_callbacks.ompt_callback(fn);
908 FOREACH_OMPT_DEVICE_EVENT(ompt_interface_fn)
909 FOREACH_OMPT_EMI_EVENT(ompt_interface_fn)
910 FOREACH_OMPT_NOEMI_EVENT(ompt_interface_fn)
911#undef ompt_interface_fn
913 return (ompt_interface_fn_t)0;
918_OMP_EXTERN
void ompt_libomp_connect(ompt_start_tool_result_t *result) {
919 OMPT_VERBOSE_INIT_PRINT(
"libomp --> OMPT: Enter ompt_libomp_connect\n");
922 __ompt_force_initialization();
924 if (ompt_enabled.enabled && result) {
925 OMPT_VERBOSE_INIT_PRINT(
"libomp --> OMPT: Connecting with libomptarget\n");
929 result->initialize(ompt_libomp_target_fn_lookup,
933 libomptarget_ompt_result = result;
935 OMPT_VERBOSE_INIT_PRINT(
"libomp --> OMPT: Exit ompt_libomp_connect\n");