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;
107#define OMPT_DLCLOSE(Lib) FreeLibrary(Lib)
109static void *ompt_tool_module = NULL;
110#define OMPT_DLCLOSE(Lib) dlclose(Lib)
117static ompt_interface_fn_t ompt_fn_lookup(
const char *s);
119OMPT_API_ROUTINE ompt_data_t *ompt_get_thread_data(
void);
125typedef ompt_start_tool_result_t *(*ompt_start_tool_t)(
unsigned int,
139static ompt_start_tool_result_t *ompt_tool_darwin(
unsigned int omp_version,
140 const char *runtime_version) {
141 ompt_start_tool_result_t *ret = NULL;
143 ompt_start_tool_t start_tool =
144 (ompt_start_tool_t)dlsym(RTLD_DEFAULT,
"ompt_start_tool");
146 ret = start_tool(omp_version, runtime_version);
151#elif OMPT_HAVE_WEAK_ATTRIBUTE
157_OMP_EXTERN OMPT_WEAK_ATTRIBUTE ompt_start_tool_result_t *
158ompt_start_tool(
unsigned int omp_version,
const char *runtime_version) {
159 ompt_start_tool_result_t *ret = NULL;
164 ompt_start_tool_t next_tool =
165 (ompt_start_tool_t)dlsym(RTLD_NEXT,
"ompt_start_tool");
167 ret = next_tool(omp_version, runtime_version);
180#pragma comment(lib, "psapi.lib")
183#define NUM_MODULES 128
185static ompt_start_tool_result_t *
186ompt_tool_windows(
unsigned int omp_version,
const char *runtime_version) {
188 DWORD needed, new_size;
190 HANDLE process = GetCurrentProcess();
191 modules = (HMODULE *)malloc(NUM_MODULES *
sizeof(HMODULE));
192 ompt_start_tool_t ompt_tool_p = NULL;
195 printf(
"ompt_tool_windows(): looking for ompt_start_tool\n");
197 if (!EnumProcessModules(process, modules, NUM_MODULES *
sizeof(HMODULE),
204 new_size = needed /
sizeof(HMODULE);
205 if (new_size > NUM_MODULES) {
207 printf(
"ompt_tool_windows(): resize buffer to %d bytes\n", needed);
209 modules = (HMODULE *)realloc(modules, needed);
211 if (!EnumProcessModules(process, modules, needed, &needed)) {
216 for (i = 0; i < new_size; ++i) {
217 (FARPROC &)ompt_tool_p = GetProcAddress(modules[i],
"ompt_start_tool");
220 TCHAR modName[MAX_PATH];
221 if (GetModuleFileName(modules[i], modName, MAX_PATH))
222 printf(
"ompt_tool_windows(): ompt_start_tool found in module %s\n",
226 return (*ompt_tool_p)(omp_version, runtime_version);
230 TCHAR modName[MAX_PATH];
231 if (GetModuleFileName(modules[i], modName, MAX_PATH))
232 printf(
"ompt_tool_windows(): ompt_start_tool not found in module %s\n",
241#error Activation of OMPT is not supported on this platform.
244static ompt_start_tool_result_t *
245ompt_try_start_tool(
unsigned int omp_version,
const char *runtime_version) {
246 ompt_start_tool_result_t *ret = NULL;
247 ompt_start_tool_t start_tool = NULL;
250 const char *sep =
";";
252 const char *sep =
":";
255 OMPT_VERBOSE_INIT_PRINT(
"----- START LOGGING OF TOOL REGISTRATION -----\n");
256 OMPT_VERBOSE_INIT_PRINT(
"Search for OMP tool in current address space... ");
260 ret = ompt_tool_darwin(omp_version, runtime_version);
261#elif OMPT_HAVE_WEAK_ATTRIBUTE
262 ret = ompt_start_tool(omp_version, runtime_version);
264 ret = ompt_tool_windows(omp_version, runtime_version);
266#error Activation of OMPT is not supported on this platform.
269 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Success.\n");
270 OMPT_VERBOSE_INIT_PRINT(
271 "Tool was started and is using the OMPT interface.\n");
272 OMPT_VERBOSE_INIT_PRINT(
"----- END LOGGING OF TOOL REGISTRATION -----\n");
277 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed.\n");
278 const char *tool_libs = getenv(
"OMP_TOOL_LIBRARIES");
280 OMPT_VERBOSE_INIT_PRINT(
"Searching tool libraries...\n");
281 OMPT_VERBOSE_INIT_PRINT(
"OMP_TOOL_LIBRARIES = %s\n", tool_libs);
282 char *libs = __kmp_str_format(
"%s", tool_libs);
284 char *fname = __kmp_str_token(libs, sep, &buf);
290 OMPT_VERBOSE_INIT_PRINT(
"Opening %s... ", fname);
291 void *h = dlopen(fname, RTLD_LAZY);
293 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed: %s\n", dlerror());
295 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Success. \n");
296 OMPT_VERBOSE_INIT_PRINT(
"Searching for ompt_start_tool in %s... ",
299 start_tool = (ompt_start_tool_t)dlsym(h,
"ompt_start_tool");
301 char *error = dlerror();
303 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed: %s\n", error);
305 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed: %s\n",
306 "ompt_start_tool = NULL");
310 OMPT_VERBOSE_INIT_PRINT(
"Opening %s... ", fname);
311 HMODULE h = LoadLibrary(fname);
313 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed: Error %u\n",
314 (
unsigned)GetLastError());
316 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Success. \n");
317 OMPT_VERBOSE_INIT_PRINT(
"Searching for ompt_start_tool in %s... ",
319 start_tool = (ompt_start_tool_t)GetProcAddress(h,
"ompt_start_tool");
321 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed: Error %u\n",
322 (
unsigned)GetLastError());
325#error Activation of OMPT is not supported on this platform.
328 ret = (*start_tool)(omp_version, runtime_version);
330 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Success.\n");
331 OMPT_VERBOSE_INIT_PRINT(
332 "Tool was started and is using the OMPT interface.\n");
333 ompt_tool_module = h;
336 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
337 "Found but not using the OMPT interface.\n");
338 OMPT_VERBOSE_INIT_PRINT(
"Continuing search...\n");
342 fname = __kmp_str_token(NULL, sep, &buf);
344 __kmp_str_free(&libs);
346 OMPT_VERBOSE_INIT_PRINT(
"No OMP_TOOL_LIBRARIES defined.\n");
351 OMPT_VERBOSE_INIT_PRINT(
"----- END LOGGING OF TOOL REGISTRATION -----\n");
357 const char *fname =
"libarcher.so";
358 OMPT_VERBOSE_INIT_PRINT(
359 "...searching tool libraries failed. Using archer tool.\n");
360 OMPT_VERBOSE_INIT_PRINT(
"Opening %s... ", fname);
361 void *h = dlopen(fname, RTLD_LAZY);
363 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Success.\n");
364 OMPT_VERBOSE_INIT_PRINT(
"Searching for ompt_start_tool in %s... ", fname);
365 start_tool = (ompt_start_tool_t)dlsym(h,
"ompt_start_tool");
367 ret = (*start_tool)(omp_version, runtime_version);
369 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Success.\n");
370 OMPT_VERBOSE_INIT_PRINT(
371 "Tool was started and is using the OMPT interface.\n");
372 OMPT_VERBOSE_INIT_PRINT(
373 "----- END LOGGING OF TOOL REGISTRATION -----\n");
376 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
377 "Found but not using the OMPT interface.\n");
379 OMPT_VERBOSE_INIT_CONTINUED_PRINT(
"Failed: %s\n", dlerror());
384 OMPT_VERBOSE_INIT_PRINT(
"No OMP tool loaded.\n");
385 OMPT_VERBOSE_INIT_PRINT(
"----- END LOGGING OF TOOL REGISTRATION -----\n");
389void ompt_pre_init() {
393 static int ompt_pre_initialized = 0;
395 if (ompt_pre_initialized)
398 ompt_pre_initialized = 1;
403 const char *ompt_env_var = getenv(
"OMP_TOOL");
404 tool_setting_e tool_setting = omp_tool_error;
406 if (!ompt_env_var || !strcmp(ompt_env_var,
""))
407 tool_setting = omp_tool_unset;
408 else if (OMPT_STR_MATCH(ompt_env_var,
"disabled"))
409 tool_setting = omp_tool_disabled;
410 else if (OMPT_STR_MATCH(ompt_env_var,
"enabled"))
411 tool_setting = omp_tool_enabled;
413 const char *ompt_env_verbose_init = getenv(
"OMP_TOOL_VERBOSE_INIT");
416 if (ompt_env_verbose_init && strcmp(ompt_env_verbose_init,
"") &&
417 !OMPT_STR_MATCH(ompt_env_verbose_init,
"disabled")) {
419 if (OMPT_STR_MATCH(ompt_env_verbose_init,
"STDERR"))
420 verbose_file = stderr;
421 else if (OMPT_STR_MATCH(ompt_env_verbose_init,
"STDOUT"))
422 verbose_file = stdout;
424 verbose_file = fopen(ompt_env_verbose_init,
"w");
429 printf(
"ompt_pre_init(): tool_setting = %d\n", tool_setting);
431 switch (tool_setting) {
432 case omp_tool_disabled:
433 OMPT_VERBOSE_INIT_PRINT(
"OMP tool disabled. \n");
437 case omp_tool_enabled:
442 ompt_start_tool_result =
443 ompt_try_start_tool(__kmp_openmp_version, ompt_get_runtime_version());
445 memset(&ompt_enabled, 0,
sizeof(ompt_enabled));
450 "Warning: OMP_TOOL has invalid value \"%s\".\n"
451 " legal values are (NULL,\"\",\"disabled\","
456 if (verbose_init && verbose_file != stderr && verbose_file != stdout)
457 fclose(verbose_file);
459 printf(
"ompt_pre_init(): ompt_enabled = %d\n", ompt_enabled);
463extern "C" int omp_get_initial_device(
void);
465void ompt_post_init() {
469 static int ompt_post_initialized = 0;
471 if (ompt_post_initialized)
474 ompt_post_initialized = 1;
479 if (ompt_start_tool_result) {
480 ompt_enabled.enabled = !!ompt_start_tool_result->initialize(
481 ompt_fn_lookup, omp_get_initial_device(),
482 &(ompt_start_tool_result->tool_data));
484 if (!ompt_enabled.enabled) {
486 memset(&ompt_enabled, 0,
sizeof(ompt_enabled));
490 kmp_info_t *root_thread = ompt_get_thread();
492 ompt_set_thread_state(root_thread, ompt_state_overhead);
494 if (ompt_enabled.ompt_callback_thread_begin) {
495 ompt_callbacks.ompt_callback(ompt_callback_thread_begin)(
496 ompt_thread_initial, __ompt_get_thread_data_internal());
498 ompt_data_t *task_data;
499 ompt_data_t *parallel_data;
500 __ompt_get_task_info_internal(0, NULL, &task_data, NULL, ¶llel_data,
502 if (ompt_enabled.ompt_callback_implicit_task) {
503 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
504 ompt_scope_begin, parallel_data, task_data, 1, 1, ompt_task_initial);
507 ompt_set_thread_state(root_thread, ompt_state_work_serial);
512 if (ompt_enabled.enabled
514 && ompt_start_tool_result && ompt_start_tool_result->finalize
517 ompt_start_tool_result->finalize(&(ompt_start_tool_result->tool_data));
520 if (ompt_tool_module)
521 OMPT_DLCLOSE(ompt_tool_module);
522 memset(&ompt_enabled, 0,
sizeof(ompt_enabled));
533OMPT_API_ROUTINE
int ompt_enumerate_states(
int current_state,
int *next_state,
534 const char **next_state_name) {
535 const static int len =
sizeof(ompt_state_info) /
sizeof(ompt_state_info_t);
538 for (i = 0; i < len - 1; i++) {
539 if (ompt_state_info[i].state_id == current_state) {
540 *next_state = ompt_state_info[i + 1].state_id;
541 *next_state_name = ompt_state_info[i + 1].state_name;
549OMPT_API_ROUTINE
int ompt_enumerate_mutex_impls(
int current_impl,
551 const char **next_impl_name) {
552 const static int len =
553 sizeof(kmp_mutex_impl_info) /
sizeof(kmp_mutex_impl_info_t);
555 for (i = 0; i < len - 1; i++) {
556 if (kmp_mutex_impl_info[i].
id != current_impl)
558 *next_impl = kmp_mutex_impl_info[i + 1].id;
559 *next_impl_name = kmp_mutex_impl_info[i + 1].name;
569OMPT_API_ROUTINE ompt_set_result_t ompt_set_callback(ompt_callbacks_t which,
570 ompt_callback_t callback) {
573#define ompt_event_macro(event_name, callback_type, event_id) \
575 ompt_callbacks.ompt_callback(event_name) = (callback_type)callback; \
576 ompt_enabled.event_name = (callback != 0); \
578 return ompt_event_implementation_status(event_name); \
580 return ompt_set_always;
582 FOREACH_OMPT_EVENT(ompt_event_macro)
584#undef ompt_event_macro
587 return ompt_set_error;
591OMPT_API_ROUTINE
int ompt_get_callback(ompt_callbacks_t which,
592 ompt_callback_t *callback) {
593 if (!ompt_enabled.enabled)
594 return ompt_get_callback_failure;
598#define ompt_event_macro(event_name, callback_type, event_id) \
600 ompt_callback_t mycb = \
601 (ompt_callback_t)ompt_callbacks.ompt_callback(event_name); \
602 if (ompt_enabled.event_name && mycb) { \
604 return ompt_get_callback_success; \
606 return ompt_get_callback_failure; \
609 FOREACH_OMPT_EVENT(ompt_event_macro)
611#undef ompt_event_macro
614 return ompt_get_callback_failure;
622OMPT_API_ROUTINE
int ompt_get_parallel_info(
int ancestor_level,
623 ompt_data_t **parallel_data,
625 if (!ompt_enabled.enabled)
627 return __ompt_get_parallel_info_internal(ancestor_level, parallel_data,
631OMPT_API_ROUTINE
int ompt_get_state(ompt_wait_id_t *wait_id) {
632 if (!ompt_enabled.enabled)
633 return ompt_state_work_serial;
634 int thread_state = __ompt_get_state_internal(wait_id);
636 if (thread_state == ompt_state_undefined) {
637 thread_state = ompt_state_work_serial;
647OMPT_API_ROUTINE ompt_data_t *ompt_get_thread_data(
void) {
648 if (!ompt_enabled.enabled)
650 return __ompt_get_thread_data_internal();
653OMPT_API_ROUTINE
int ompt_get_task_info(
int ancestor_level,
int *type,
654 ompt_data_t **task_data,
655 ompt_frame_t **task_frame,
656 ompt_data_t **parallel_data,
658 if (!ompt_enabled.enabled)
660 return __ompt_get_task_info_internal(ancestor_level, type, task_data,
661 task_frame, parallel_data, thread_num);
664OMPT_API_ROUTINE
int ompt_get_task_memory(
void **addr,
size_t *size,
666 return __ompt_get_task_memory_internal(addr, size, block);
673OMPT_API_ROUTINE
int ompt_get_num_procs(
void) {
676 return __kmp_avail_proc;
683OMPT_API_ROUTINE
int ompt_get_num_places(
void) {
685#if !KMP_AFFINITY_SUPPORTED
688 if (!KMP_AFFINITY_CAPABLE())
690 return __kmp_affinity_num_masks;
694OMPT_API_ROUTINE
int ompt_get_place_proc_ids(
int place_num,
int ids_size,
697#if !KMP_AFFINITY_SUPPORTED
701 int tmp_ids[ids_size];
702 for (
int j = 0; j < ids_size; j++)
704 if (!KMP_AFFINITY_CAPABLE())
706 if (place_num < 0 || place_num >= (
int)__kmp_affinity_num_masks)
710 kmp_affin_mask_t *mask = KMP_CPU_INDEX(__kmp_affinity_masks, place_num);
712 KMP_CPU_SET_ITERATE(i, mask) {
713 if ((!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) ||
714 (!KMP_CPU_ISSET(i, mask))) {
717 if (count < ids_size)
721 if (ids_size >= count) {
722 for (i = 0; i < count; i++) {
730OMPT_API_ROUTINE
int ompt_get_place_num(
void) {
732#if !KMP_AFFINITY_SUPPORTED
735 if (!ompt_enabled.enabled || __kmp_get_gtid() < 0)
740 if (!KMP_AFFINITY_CAPABLE())
742 gtid = __kmp_entry_gtid();
743 thread = __kmp_thread_from_gtid(gtid);
744 if (thread == NULL || thread->th.th_current_place < 0)
746 return thread->th.th_current_place;
750OMPT_API_ROUTINE
int ompt_get_partition_place_nums(
int place_nums_size,
753#if !KMP_AFFINITY_SUPPORTED
756 if (!ompt_enabled.enabled || __kmp_get_gtid() < 0)
759 int i, gtid, place_num, first_place, last_place, start, end;
761 if (!KMP_AFFINITY_CAPABLE())
763 gtid = __kmp_entry_gtid();
764 thread = __kmp_thread_from_gtid(gtid);
767 first_place = thread->th.th_first_place;
768 last_place = thread->th.th_last_place;
769 if (first_place < 0 || last_place < 0)
771 if (first_place <= last_place) {
778 if (end - start <= place_nums_size)
779 for (i = 0, place_num = start; place_num <= end; ++place_num, ++i) {
780 place_nums[i] = place_num;
782 return end - start + 1;
790OMPT_API_ROUTINE
int ompt_get_proc_id(
void) {
791 if (!ompt_enabled.enabled || __kmp_get_gtid() < 0)
793#if KMP_HAVE_SCHED_GETCPU
794 return sched_getcpu();
797 GetCurrentProcessorNumberEx(&pn);
798 return 64 * pn.Group + pn.Number;
821int __kmp_control_tool(uint64_t command, uint64_t modifier,
void *arg) {
823 if (ompt_enabled.enabled) {
824 if (ompt_enabled.ompt_callback_control_tool) {
825 return ompt_callbacks.ompt_callback(ompt_callback_control_tool)(
826 command, modifier, arg, OMPT_LOAD_RETURN_ADDRESS(__kmp_entry_gtid()));
839OMPT_API_ROUTINE uint64_t ompt_get_unique_id(
void) {
840 return __ompt_get_unique_id_internal();
843OMPT_API_ROUTINE
void ompt_finalize_tool(
void) { __kmp_internal_end_atexit(); }
849OMPT_API_ROUTINE
int ompt_get_target_info(uint64_t *device_num,
850 ompt_id_t *target_id,
851 ompt_id_t *host_op_id) {
855OMPT_API_ROUTINE
int ompt_get_num_devices(
void) {
863static ompt_interface_fn_t ompt_fn_lookup(
const char *s) {
865#define ompt_interface_fn(fn) \
866 fn##_t fn##_f = fn; \
867 if (strcmp(s, #fn) == 0) \
868 return (ompt_interface_fn_t)fn##_f;
870 FOREACH_OMPT_INQUIRY_FN(ompt_interface_fn)