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_HOST_EVENT(ompt_event_macro)
601 FOREACH_OMPT_DEVICE_EVENT(ompt_event_macro)
603#undef ompt_event_macro
613#define ompt_target_event_macro(event_name, comparison_name, callback_type, \
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); \
621 return ompt_event_implementation_status(event_name); \
623 return ompt_set_always;
625 FOREACH_MAPPED_OMPT_TARGET_EVENT(ompt_target_event_macro);
627#undef ompt_target_event_macro
630 return ompt_set_error;
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;
641#define ompt_event_macro(event_name, callback_type, event_id) \
643 ompt_callback_t mycb = \
644 (ompt_callback_t)ompt_callbacks.ompt_callback(event_name); \
645 if (ompt_enabled.event_name && mycb) { \
647 return ompt_get_callback_success; \
649 return ompt_get_callback_failure; \
652 FOREACH_OMPT_EVENT(ompt_event_macro)
654#undef ompt_event_macro
657 return ompt_get_callback_failure;
665OMPT_API_ROUTINE
int ompt_get_parallel_info(
int ancestor_level,
666 ompt_data_t **parallel_data,
668 if (!ompt_enabled.enabled)
670 return __ompt_get_parallel_info_internal(ancestor_level, parallel_data,
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);
679 if (thread_state == ompt_state_undefined) {
680 thread_state = ompt_state_work_serial;
690OMPT_API_ROUTINE ompt_data_t *ompt_get_thread_data(
void) {
691 if (!ompt_enabled.enabled)
693 return __ompt_get_thread_data_internal();
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,
701 if (!ompt_enabled.enabled)
703 return __ompt_get_task_info_internal(ancestor_level, type, task_data,
704 task_frame, parallel_data, thread_num);
707OMPT_API_ROUTINE
int ompt_get_task_memory(
void **addr,
size_t *size,
709 return __ompt_get_task_memory_internal(addr, size, block);
716OMPT_API_ROUTINE
int ompt_get_num_procs(
void) {
719 return __kmp_avail_proc;
726OMPT_API_ROUTINE
int ompt_get_num_places(
void) {
728#if !KMP_AFFINITY_SUPPORTED
731 if (!KMP_AFFINITY_CAPABLE())
733 return __kmp_affinity.num_masks;
737OMPT_API_ROUTINE
int ompt_get_place_proc_ids(
int place_num,
int ids_size,
740#if !KMP_AFFINITY_SUPPORTED
745 for (
int j = 0; j < ids_size; j++)
747 if (!KMP_AFFINITY_CAPABLE())
749 if (place_num < 0 || place_num >= (
int)__kmp_affinity.num_masks)
753 kmp_affin_mask_t *mask = KMP_CPU_INDEX(__kmp_affinity.masks, place_num);
755 KMP_CPU_SET_ITERATE(i, mask) {
756 if ((!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) ||
757 (!KMP_CPU_ISSET(i, mask))) {
760 if (count < ids_size)
764 if (ids_size >= count) {
765 for (i = 0; i < count; i++) {
773OMPT_API_ROUTINE
int ompt_get_place_num(
void) {
775#if !KMP_AFFINITY_SUPPORTED
778 if (!ompt_enabled.enabled || __kmp_get_gtid() < 0)
783 if (!KMP_AFFINITY_CAPABLE())
785 gtid = __kmp_entry_gtid();
786 thread = __kmp_thread_from_gtid(gtid);
787 if (thread == NULL || thread->th.th_current_place < 0)
789 return thread->th.th_current_place;
793OMPT_API_ROUTINE
int ompt_get_partition_place_nums(
int place_nums_size,
796#if !KMP_AFFINITY_SUPPORTED
799 if (!ompt_enabled.enabled || __kmp_get_gtid() < 0)
802 int i, gtid, place_num, first_place, last_place, start, end;
804 if (!KMP_AFFINITY_CAPABLE())
806 gtid = __kmp_entry_gtid();
807 thread = __kmp_thread_from_gtid(gtid);
810 first_place = thread->th.th_first_place;
811 last_place = thread->th.th_last_place;
812 if (first_place < 0 || last_place < 0)
814 if (first_place <= last_place) {
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;
825 return end - start + 1;
833OMPT_API_ROUTINE
int ompt_get_proc_id(
void) {
834 if (!ompt_enabled.enabled || __kmp_get_gtid() < 0)
836#if KMP_HAVE_SCHED_GETCPU
837 return sched_getcpu();
840 GetCurrentProcessorNumberEx(&pn);
841 return 64 * pn.Group + pn.Number;
864int __kmp_control_tool(uint64_t command, uint64_t modifier,
void *arg) {
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()));
882OMPT_API_ROUTINE uint64_t ompt_get_unique_id(
void) {
883 return __ompt_get_unique_id_internal();
886OMPT_API_ROUTINE
void ompt_finalize_tool(
void) { __kmp_internal_end_atexit(); }
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) {
898extern "C" int omp_get_num_devices(
void);
900OMPT_API_ROUTINE
int ompt_get_num_devices(
void) {
901 return omp_get_num_devices();
908static ompt_interface_fn_t ompt_fn_lookup(
const char *s) {
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;
915 FOREACH_OMPT_INQUIRY_FN(ompt_interface_fn)
917#undef ompt_interface_fn
922static ompt_data_t *ompt_get_task_data() {
return __ompt_get_task_data(); }
924static ompt_data_t *ompt_get_target_task_data() {
925 return __ompt_get_target_task_data();
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;
934 provide_fn(ompt_get_callback);
935 provide_fn(ompt_get_task_data);
936 provide_fn(ompt_get_target_task_data);
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);
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
948 return (ompt_interface_fn_t)0;
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");
957 __ompt_force_initialization();
959 if (ompt_enabled.enabled && result) {
960 OMPT_VERBOSE_INIT_PRINT(
"libomp --> OMPT: Connecting with libomptarget\n");
964 result->initialize(ompt_libomp_target_fn_lookup,
969 libomptarget_ompt_result = result;
971 OMPT_VERBOSE_INIT_PRINT(
"libomp --> OMPT: Exit ompt_libomp_connect\n");