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
44 #ifndef OMPT_STR_MATCH
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__)
59 static FILE *verbose_file;
60 static int verbose_init;
67 const char *state_name;
68 ompt_state_t state_id;
74 } kmp_mutex_impl_info_t;
87 ompt_callbacks_active_t ompt_enabled;
89 ompt_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
95 kmp_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
101 ompt_callbacks_internal_t ompt_callbacks;
103 static ompt_start_tool_result_t *ompt_start_tool_result = NULL;
109 static ompt_interface_fn_t ompt_fn_lookup(
const char *s);
111 OMPT_API_ROUTINE ompt_data_t *ompt_get_thread_data(
void);
117 typedef ompt_start_tool_result_t *(*ompt_start_tool_t)(
unsigned int,
131 static ompt_start_tool_result_t *ompt_tool_darwin(
unsigned int omp_version,
132 const char *runtime_version) {
133 ompt_start_tool_result_t *ret = NULL;
135 ompt_start_tool_t start_tool =
136 (ompt_start_tool_t)dlsym(RTLD_DEFAULT,
"ompt_start_tool");
138 ret = start_tool(omp_version, runtime_version);
143 #elif OMPT_HAVE_WEAK_ATTRIBUTE
149 _OMP_EXTERN OMPT_WEAK_ATTRIBUTE ompt_start_tool_result_t *
150 ompt_start_tool(
unsigned int omp_version,
const char *runtime_version) {
151 ompt_start_tool_result_t *ret = NULL;
156 ompt_start_tool_t next_tool =
157 (ompt_start_tool_t)dlsym(RTLD_NEXT,
"ompt_start_tool");
159 ret = next_tool(omp_version, runtime_version);
164 #elif OMPT_HAVE_PSAPI
172 #pragma comment(lib, "psapi.lib")
175 #define NUM_MODULES 128
177 static ompt_start_tool_result_t *
178 ompt_tool_windows(
unsigned int omp_version,
const char *runtime_version) {
180 DWORD needed, new_size;
182 HANDLE process = GetCurrentProcess();
183 modules = (HMODULE *)malloc(NUM_MODULES *
sizeof(HMODULE));
184 ompt_start_tool_t ompt_tool_p = NULL;
187 printf(
"ompt_tool_windows(): looking for ompt_start_tool\n");
189 if (!EnumProcessModules(process, modules, NUM_MODULES *
sizeof(HMODULE),
196 new_size = needed /
sizeof(HMODULE);
197 if (new_size > NUM_MODULES) {
199 printf(
"ompt_tool_windows(): resize buffer to %d bytes\n", needed);
201 modules = (HMODULE *)realloc(modules, needed);
203 if (!EnumProcessModules(process, modules, needed, &needed)) {
208 for (i = 0; i < new_size; ++i) {
209 (FARPROC &)ompt_tool_p = GetProcAddress(modules[i],
"ompt_start_tool");
212 TCHAR modName[MAX_PATH];
213 if (GetModuleFileName(modules[i], modName, MAX_PATH))
214 printf(
"ompt_tool_windows(): ompt_start_tool found in module %s\n",
218 return (*ompt_tool_p)(omp_version, runtime_version);
222 TCHAR modName[MAX_PATH];
223 if (GetModuleFileName(modules[i], modName, MAX_PATH))
224 printf(
"ompt_tool_windows(): ompt_start_tool not found in module %s\n",
233 #error Activation of OMPT is not supported on this platform.
236 static ompt_start_tool_result_t *
237 ompt_try_start_tool(
unsigned int omp_version,
const char *runtime_version) {
238 ompt_start_tool_result_t *ret = NULL;
239 ompt_start_tool_t start_tool = NULL;
242 const char *sep =
";";
244 const char *sep =
":";
247 OMPT_VERBOSE_INIT_PRINT(
"----- START LOGGING OF TOOL REGISTRATION -----\n");
248 OMPT_VERBOSE_INIT_PRINT(
"Search for OMP tool in current address space... ");
252 ret = ompt_tool_darwin(omp_version, runtime_version);
253 #elif OMPT_HAVE_WEAK_ATTRIBUTE
254 ret = ompt_start_tool(omp_version, runtime_version);
255 #elif OMPT_HAVE_PSAPI
256 ret = ompt_tool_windows(omp_version, runtime_version);
258 #error Activation of OMPT is not supported on this platform.
261 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Sucess.\n");
262 OMPT_VERBOSE_INIT_PRINT(
263 "Tool was started and is using the OMPT interface.\n");
264 OMPT_VERBOSE_INIT_PRINT(
"----- END LOGGING OF TOOL REGISTRATION -----\n");
269 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed.\n");
270 const char *tool_libs = getenv(
"OMP_TOOL_LIBRARIES");
272 OMPT_VERBOSE_INIT_PRINT(
"Searching tool libraries...\n");
273 OMPT_VERBOSE_INIT_PRINT(
"OMP_TOOL_LIBRARIES = %s\n", tool_libs);
274 char *libs = __kmp_str_format(
"%s", tool_libs);
276 char *fname = __kmp_str_token(libs, sep, &buf);
282 OMPT_VERBOSE_INIT_PRINT(
"Opening %s... ", fname);
283 void *h = dlopen(fname, RTLD_LAZY);
285 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed: %s\n", dlerror());
287 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Success. \n");
288 OMPT_VERBOSE_INIT_PRINT(
"Searching for ompt_start_tool in %s... ",
290 start_tool = (ompt_start_tool_t)dlsym(h,
"ompt_start_tool");
292 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed: %s\n", dlerror());
295 OMPT_VERBOSE_INIT_PRINT(
"Opening %s... ", fname);
296 HMODULE h = LoadLibrary(fname);
298 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed: Error %u\n", GetLastError());
300 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Success. \n");
301 OMPT_VERBOSE_INIT_PRINT(
"Searching for ompt_start_tool in %s... ",
303 start_tool = (ompt_start_tool_t)GetProcAddress(h,
"ompt_start_tool");
305 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed: Error %s\n",
309 #error Activation of OMPT is not supported on this platform.
312 ret = (*start_tool)(omp_version, runtime_version);
314 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Success.\n");
315 OMPT_VERBOSE_INIT_PRINT(
316 "Tool was started and is using the OMPT interface.\n");
319 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
320 "Found but not using the OMPT interface.\n");
321 OMPT_VERBOSE_INIT_PRINT(
"Continuing search...\n");
324 fname = __kmp_str_token(NULL, sep, &buf);
326 __kmp_str_free(&libs);
328 OMPT_VERBOSE_INIT_PRINT(
"No OMP_TOOL_LIBRARIES defined.\n");
333 OMPT_VERBOSE_INIT_PRINT(
"----- END LOGGING OF TOOL REGISTRATION -----\n");
339 const char *fname =
"libarcher.so";
340 OMPT_VERBOSE_INIT_PRINT(
341 "...searching tool libraries failed. Using archer tool.\n");
342 OMPT_VERBOSE_INIT_PRINT(
"Opening %s... ", fname);
343 void *h = dlopen(fname, RTLD_LAZY);
345 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Success.\n");
346 OMPT_VERBOSE_INIT_PRINT(
"Searching for ompt_start_tool in %s... ", fname);
347 start_tool = (ompt_start_tool_t)dlsym(h,
"ompt_start_tool");
349 ret = (*start_tool)(omp_version, runtime_version);
351 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Success.\n");
352 OMPT_VERBOSE_INIT_PRINT(
353 "Tool was started and is using the OMPT interface.\n");
354 OMPT_VERBOSE_INIT_PRINT(
355 "----- END LOGGING OF TOOL REGISTRATION -----\n");
358 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
359 "Found but not using the OMPT interface.\n");
361 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed: %s\n", dlerror());
366 OMPT_VERBOSE_INIT_PRINT(
"No OMP tool loaded.\n");
367 OMPT_VERBOSE_INIT_PRINT(
"----- END LOGGING OF TOOL REGISTRATION -----\n");
371 void ompt_pre_init() {
375 static int ompt_pre_initialized = 0;
377 if (ompt_pre_initialized)
380 ompt_pre_initialized = 1;
385 const char *ompt_env_var = getenv(
"OMP_TOOL");
386 tool_setting_e tool_setting = omp_tool_error;
388 if (!ompt_env_var || !strcmp(ompt_env_var,
""))
389 tool_setting = omp_tool_unset;
390 else if (OMPT_STR_MATCH(ompt_env_var,
"disabled"))
391 tool_setting = omp_tool_disabled;
392 else if (OMPT_STR_MATCH(ompt_env_var,
"enabled"))
393 tool_setting = omp_tool_enabled;
395 const char *ompt_env_verbose_init = getenv(
"OMP_TOOL_VERBOSE_INIT");
398 if (ompt_env_verbose_init && strcmp(ompt_env_verbose_init,
"") &&
399 !OMPT_STR_MATCH(ompt_env_verbose_init,
"disabled")) {
401 if (OMPT_STR_MATCH(ompt_env_verbose_init,
"STDERR"))
402 verbose_file = stderr;
403 else if (OMPT_STR_MATCH(ompt_env_verbose_init,
"STDOUT"))
404 verbose_file = stdout;
406 verbose_file = fopen(ompt_env_verbose_init,
"w");
411 printf(
"ompt_pre_init(): tool_setting = %d\n", tool_setting);
413 switch (tool_setting) {
414 case omp_tool_disabled:
415 OMPT_VERBOSE_INIT_PRINT(
"OMP tool disabled. \n");
419 case omp_tool_enabled:
424 ompt_start_tool_result =
425 ompt_try_start_tool(__kmp_openmp_version, ompt_get_runtime_version());
427 memset(&ompt_enabled, 0,
sizeof(ompt_enabled));
431 fprintf(stderr,
"Warning: OMP_TOOL has invalid value \"%s\".\n"
432 " legal values are (NULL,\"\",\"disabled\","
437 if (verbose_init && verbose_file != stderr && verbose_file != stdout)
438 fclose(verbose_file);
440 printf(
"ompt_pre_init(): ompt_enabled = %d\n", ompt_enabled);
444 extern "C" int omp_get_initial_device(
void);
446 void ompt_post_init() {
450 static int ompt_post_initialized = 0;
452 if (ompt_post_initialized)
455 ompt_post_initialized = 1;
460 if (ompt_start_tool_result) {
461 ompt_enabled.enabled = !!ompt_start_tool_result->initialize(
462 ompt_fn_lookup, omp_get_initial_device(), &(ompt_start_tool_result->tool_data));
464 if (!ompt_enabled.enabled) {
466 memset(&ompt_enabled, 0,
sizeof(ompt_enabled));
470 kmp_info_t *root_thread = ompt_get_thread();
472 ompt_set_thread_state(root_thread, ompt_state_overhead);
474 if (ompt_enabled.ompt_callback_thread_begin) {
475 ompt_callbacks.ompt_callback(ompt_callback_thread_begin)(
476 ompt_thread_initial, __ompt_get_thread_data_internal());
478 ompt_data_t *task_data;
479 ompt_data_t *parallel_data;
480 __ompt_get_task_info_internal(0, NULL, &task_data, NULL, ¶llel_data, NULL);
481 if (ompt_enabled.ompt_callback_implicit_task) {
482 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
483 ompt_scope_begin, parallel_data, task_data, 1, 1, ompt_task_initial);
486 ompt_set_thread_state(root_thread, ompt_state_work_serial);
491 if (ompt_enabled.enabled) {
492 ompt_start_tool_result->finalize(&(ompt_start_tool_result->tool_data));
495 memset(&ompt_enabled, 0,
sizeof(ompt_enabled));
506 OMPT_API_ROUTINE
int ompt_enumerate_states(
int current_state,
int *next_state,
507 const char **next_state_name) {
508 const static int len =
sizeof(ompt_state_info) /
sizeof(ompt_state_info_t);
511 for (i = 0; i < len - 1; i++) {
512 if (ompt_state_info[i].state_id == current_state) {
513 *next_state = ompt_state_info[i + 1].state_id;
514 *next_state_name = ompt_state_info[i + 1].state_name;
522 OMPT_API_ROUTINE
int ompt_enumerate_mutex_impls(
int current_impl,
524 const char **next_impl_name) {
525 const static int len =
526 sizeof(kmp_mutex_impl_info) /
sizeof(kmp_mutex_impl_info_t);
528 for (i = 0; i < len - 1; i++) {
529 if (kmp_mutex_impl_info[i].
id != current_impl)
531 *next_impl = kmp_mutex_impl_info[i + 1].id;
532 *next_impl_name = kmp_mutex_impl_info[i + 1].name;
542 OMPT_API_ROUTINE ompt_set_result_t ompt_set_callback(ompt_callbacks_t which,
543 ompt_callback_t callback) {
546 #define ompt_event_macro(event_name, callback_type, event_id) \
548 ompt_callbacks.ompt_callback(event_name) = (callback_type)callback; \
549 ompt_enabled.event_name = (callback != 0); \
551 return ompt_event_implementation_status(event_name); \
553 return ompt_set_always;
555 FOREACH_OMPT_EVENT(ompt_event_macro)
557 #undef ompt_event_macro
560 return ompt_set_error;
564 OMPT_API_ROUTINE
int ompt_get_callback(ompt_callbacks_t which,
565 ompt_callback_t *callback) {
566 if (!ompt_enabled.enabled)
567 return ompt_get_callback_failure;
571 #define ompt_event_macro(event_name, callback_type, event_id) \
573 ompt_callback_t mycb = \
574 (ompt_callback_t)ompt_callbacks.ompt_callback(event_name); \
575 if (ompt_enabled.event_name && mycb) { \
577 return ompt_get_callback_success; \
579 return ompt_get_callback_failure; \
582 FOREACH_OMPT_EVENT(ompt_event_macro)
584 #undef ompt_event_macro
587 return ompt_get_callback_failure;
595 OMPT_API_ROUTINE
int ompt_get_parallel_info(
int ancestor_level,
596 ompt_data_t **parallel_data,
598 if (!ompt_enabled.enabled)
600 return __ompt_get_parallel_info_internal(ancestor_level, parallel_data,
604 OMPT_API_ROUTINE
int ompt_get_state(ompt_wait_id_t *wait_id) {
605 if (!ompt_enabled.enabled)
606 return ompt_state_work_serial;
607 int thread_state = __ompt_get_state_internal(wait_id);
609 if (thread_state == ompt_state_undefined) {
610 thread_state = ompt_state_work_serial;
620 OMPT_API_ROUTINE ompt_data_t *ompt_get_thread_data(
void) {
621 if (!ompt_enabled.enabled)
623 return __ompt_get_thread_data_internal();
626 OMPT_API_ROUTINE
int ompt_get_task_info(
int ancestor_level,
int *type,
627 ompt_data_t **task_data,
628 ompt_frame_t **task_frame,
629 ompt_data_t **parallel_data,
631 if (!ompt_enabled.enabled)
633 return __ompt_get_task_info_internal(ancestor_level, type, task_data,
634 task_frame, parallel_data, thread_num);
637 OMPT_API_ROUTINE
int ompt_get_task_memory(
void **addr,
size_t *size,
639 return __ompt_get_task_memory_internal(addr, size, block);
646 OMPT_API_ROUTINE
int ompt_get_num_procs(
void) {
649 return __kmp_avail_proc;
656 OMPT_API_ROUTINE
int ompt_get_num_places(
void) {
658 #if !KMP_AFFINITY_SUPPORTED
661 if (!KMP_AFFINITY_CAPABLE())
663 return __kmp_affinity_num_masks;
667 OMPT_API_ROUTINE
int ompt_get_place_proc_ids(
int place_num,
int ids_size,
670 #if !KMP_AFFINITY_SUPPORTED
674 int tmp_ids[ids_size];
675 if (!KMP_AFFINITY_CAPABLE())
677 if (place_num < 0 || place_num >= (
int)__kmp_affinity_num_masks)
681 kmp_affin_mask_t *mask = KMP_CPU_INDEX(__kmp_affinity_masks, place_num);
683 KMP_CPU_SET_ITERATE(i, mask) {
684 if ((!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) ||
685 (!KMP_CPU_ISSET(i, mask))) {
688 if (count < ids_size)
692 if (ids_size >= count) {
693 for (i = 0; i < count; i++) {
701 OMPT_API_ROUTINE
int ompt_get_place_num(
void) {
703 #if !KMP_AFFINITY_SUPPORTED
706 if (!ompt_enabled.enabled || __kmp_get_gtid() < 0)
711 if (!KMP_AFFINITY_CAPABLE())
713 gtid = __kmp_entry_gtid();
714 thread = __kmp_thread_from_gtid(gtid);
715 if (thread == NULL || thread->th.th_current_place < 0)
717 return thread->th.th_current_place;
721 OMPT_API_ROUTINE
int ompt_get_partition_place_nums(
int place_nums_size,
724 #if !KMP_AFFINITY_SUPPORTED
727 if (!ompt_enabled.enabled || __kmp_get_gtid() < 0)
730 int i, gtid, place_num, first_place, last_place, start, end;
732 if (!KMP_AFFINITY_CAPABLE())
734 gtid = __kmp_entry_gtid();
735 thread = __kmp_thread_from_gtid(gtid);
738 first_place = thread->th.th_first_place;
739 last_place = thread->th.th_last_place;
740 if (first_place < 0 || last_place < 0)
742 if (first_place <= last_place) {
749 if (end - start <= place_nums_size)
750 for (i = 0, place_num = start; place_num <= end; ++place_num, ++i) {
751 place_nums[i] = place_num;
753 return end - start + 1;
761 OMPT_API_ROUTINE
int ompt_get_proc_id(
void) {
762 if (!ompt_enabled.enabled || __kmp_get_gtid() < 0)
765 return sched_getcpu();
768 GetCurrentProcessorNumberEx(&pn);
769 return 64 * pn.Group + pn.Number;
792 int __kmp_control_tool(uint64_t command, uint64_t modifier,
void *arg) {
794 if (ompt_enabled.enabled) {
795 if (ompt_enabled.ompt_callback_control_tool) {
796 return ompt_callbacks.ompt_callback(ompt_callback_control_tool)(
797 command, modifier, arg, OMPT_LOAD_RETURN_ADDRESS(__kmp_entry_gtid()));
810 OMPT_API_ROUTINE uint64_t ompt_get_unique_id(
void) {
811 return __ompt_get_unique_id_internal();
814 OMPT_API_ROUTINE
void ompt_finalize_tool(
void) { __kmp_internal_end_atexit(); }
820 OMPT_API_ROUTINE
int ompt_get_target_info(uint64_t *device_num,
821 ompt_id_t *target_id,
822 ompt_id_t *host_op_id) {
826 OMPT_API_ROUTINE
int ompt_get_num_devices(
void) {
834 static ompt_interface_fn_t ompt_fn_lookup(
const char *s) {
836 #define ompt_interface_fn(fn) \
837 fn##_t fn##_f = fn; \
838 if (strcmp(s, #fn) == 0) \
839 return (ompt_interface_fn_t)fn##_f;
841 FOREACH_OMPT_INQUIRY_FN(ompt_interface_fn)