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))
53 const char *state_name;
54 ompt_state_t state_id;
60 } kmp_mutex_impl_info_t;
73 ompt_callbacks_active_t ompt_enabled;
75 ompt_state_info_t ompt_state_info[] = {
76 #define ompt_state_macro(state, code) {#state, state},
77 FOREACH_OMPT_STATE(ompt_state_macro)
78 #undef ompt_state_macro
81 kmp_mutex_impl_info_t kmp_mutex_impl_info[] = {
82 #define kmp_mutex_impl_macro(name, id) {#name, name},
83 FOREACH_KMP_MUTEX_IMPL(kmp_mutex_impl_macro)
84 #undef kmp_mutex_impl_macro
87 ompt_callbacks_internal_t ompt_callbacks;
89 static ompt_start_tool_result_t *ompt_start_tool_result = NULL;
95 static ompt_interface_fn_t ompt_fn_lookup(
const char *s);
97 OMPT_API_ROUTINE ompt_data_t *ompt_get_thread_data(
void);
103 typedef ompt_start_tool_result_t *(*ompt_start_tool_t)(
unsigned int,
117 static ompt_start_tool_result_t *ompt_tool_darwin(
unsigned int omp_version,
118 const char *runtime_version) {
119 ompt_start_tool_result_t *ret = NULL;
121 ompt_start_tool_t start_tool =
122 (ompt_start_tool_t)dlsym(RTLD_DEFAULT,
"ompt_start_tool");
124 ret = start_tool(omp_version, runtime_version);
129 #elif OMPT_HAVE_WEAK_ATTRIBUTE
135 _OMP_EXTERN OMPT_WEAK_ATTRIBUTE ompt_start_tool_result_t *
136 ompt_start_tool(
unsigned int omp_version,
const char *runtime_version) {
137 ompt_start_tool_result_t *ret = NULL;
142 ompt_start_tool_t next_tool =
143 (ompt_start_tool_t)dlsym(RTLD_NEXT,
"ompt_start_tool");
145 ret = next_tool(omp_version, runtime_version);
150 #elif OMPT_HAVE_PSAPI
158 #pragma comment(lib, "psapi.lib")
161 #define NUM_MODULES 128
163 static ompt_start_tool_result_t *
164 ompt_tool_windows(
unsigned int omp_version,
const char *runtime_version) {
166 DWORD needed, new_size;
168 HANDLE process = GetCurrentProcess();
169 modules = (HMODULE *)malloc(NUM_MODULES *
sizeof(HMODULE));
170 ompt_start_tool_t ompt_tool_p = NULL;
173 printf(
"ompt_tool_windows(): looking for ompt_start_tool\n");
175 if (!EnumProcessModules(process, modules, NUM_MODULES *
sizeof(HMODULE),
182 new_size = needed /
sizeof(HMODULE);
183 if (new_size > NUM_MODULES) {
185 printf(
"ompt_tool_windows(): resize buffer to %d bytes\n", needed);
187 modules = (HMODULE *)realloc(modules, needed);
189 if (!EnumProcessModules(process, modules, needed, &needed)) {
194 for (i = 0; i < new_size; ++i) {
195 (FARPROC &)ompt_tool_p = GetProcAddress(modules[i],
"ompt_start_tool");
198 TCHAR modName[MAX_PATH];
199 if (GetModuleFileName(modules[i], modName, MAX_PATH))
200 printf(
"ompt_tool_windows(): ompt_start_tool found in module %s\n",
204 return (*ompt_tool_p)(omp_version, runtime_version);
208 TCHAR modName[MAX_PATH];
209 if (GetModuleFileName(modules[i], modName, MAX_PATH))
210 printf(
"ompt_tool_windows(): ompt_start_tool not found in module %s\n",
219 #error Activation of OMPT is not supported on this platform.
222 static ompt_start_tool_result_t *
223 ompt_try_start_tool(
unsigned int omp_version,
const char *runtime_version) {
224 ompt_start_tool_result_t *ret = NULL;
225 ompt_start_tool_t start_tool = NULL;
228 const char *sep =
";";
230 const char *sep =
":";
235 ret = ompt_tool_darwin(omp_version, runtime_version);
236 #elif OMPT_HAVE_WEAK_ATTRIBUTE
237 ret = ompt_start_tool(omp_version, runtime_version);
238 #elif OMPT_HAVE_PSAPI
239 ret = ompt_tool_windows(omp_version, runtime_version);
241 #error Activation of OMPT is not supported on this platform.
247 const char *tool_libs = getenv(
"OMP_TOOL_LIBRARIES");
249 char *libs = __kmp_str_format(
"%s", tool_libs);
251 char *fname = __kmp_str_token(libs, sep, &buf);
254 void *h = dlopen(fname, RTLD_LAZY);
256 start_tool = (ompt_start_tool_t)dlsym(h,
"ompt_start_tool");
258 HMODULE h = LoadLibrary(fname);
260 start_tool = (ompt_start_tool_t)GetProcAddress(h,
"ompt_start_tool");
262 #error Activation of OMPT is not supported on this platform.
264 if (start_tool && (ret = (*start_tool)(omp_version, runtime_version)))
267 fname = __kmp_str_token(NULL, sep, &buf);
269 __kmp_str_free(&libs);
276 const char *fname =
"libarcher.so";
277 void *h = dlopen(fname, RTLD_LAZY);
279 start_tool = (ompt_start_tool_t)dlsym(h,
"ompt_start_tool");
281 ret = (*start_tool)(omp_version, runtime_version);
290 void ompt_pre_init() {
294 static int ompt_pre_initialized = 0;
296 if (ompt_pre_initialized)
299 ompt_pre_initialized = 1;
304 const char *ompt_env_var = getenv(
"OMP_TOOL");
305 tool_setting_e tool_setting = omp_tool_error;
307 if (!ompt_env_var || !strcmp(ompt_env_var,
""))
308 tool_setting = omp_tool_unset;
309 else if (OMPT_STR_MATCH(ompt_env_var,
"disabled"))
310 tool_setting = omp_tool_disabled;
311 else if (OMPT_STR_MATCH(ompt_env_var,
"enabled"))
312 tool_setting = omp_tool_enabled;
315 printf(
"ompt_pre_init(): tool_setting = %d\n", tool_setting);
317 switch (tool_setting) {
318 case omp_tool_disabled:
322 case omp_tool_enabled:
327 ompt_start_tool_result =
328 ompt_try_start_tool(__kmp_openmp_version, ompt_get_runtime_version());
330 memset(&ompt_enabled, 0,
sizeof(ompt_enabled));
334 fprintf(stderr,
"Warning: OMP_TOOL has invalid value \"%s\".\n"
335 " legal values are (NULL,\"\",\"disabled\","
341 printf(
"ompt_pre_init(): ompt_enabled = %d\n", ompt_enabled);
345 extern "C" int omp_get_initial_device(
void);
347 void ompt_post_init() {
351 static int ompt_post_initialized = 0;
353 if (ompt_post_initialized)
356 ompt_post_initialized = 1;
361 if (ompt_start_tool_result) {
362 ompt_enabled.enabled = !!ompt_start_tool_result->initialize(
363 ompt_fn_lookup, omp_get_initial_device(), &(ompt_start_tool_result->tool_data));
365 if (!ompt_enabled.enabled) {
367 memset(&ompt_enabled, 0,
sizeof(ompt_enabled));
371 kmp_info_t *root_thread = ompt_get_thread();
373 ompt_set_thread_state(root_thread, ompt_state_overhead);
375 if (ompt_enabled.ompt_callback_thread_begin) {
376 ompt_callbacks.ompt_callback(ompt_callback_thread_begin)(
377 ompt_thread_initial, __ompt_get_thread_data_internal());
379 ompt_data_t *task_data;
380 ompt_data_t *parallel_data;
381 __ompt_get_task_info_internal(0, NULL, &task_data, NULL, ¶llel_data, NULL);
382 if (ompt_enabled.ompt_callback_implicit_task) {
383 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
384 ompt_scope_begin, parallel_data, task_data, 1, 1, ompt_task_initial);
387 ompt_set_thread_state(root_thread, ompt_state_work_serial);
392 if (ompt_enabled.enabled) {
393 ompt_start_tool_result->finalize(&(ompt_start_tool_result->tool_data));
396 memset(&ompt_enabled, 0,
sizeof(ompt_enabled));
407 OMPT_API_ROUTINE
int ompt_enumerate_states(
int current_state,
int *next_state,
408 const char **next_state_name) {
409 const static int len =
sizeof(ompt_state_info) /
sizeof(ompt_state_info_t);
412 for (i = 0; i < len - 1; i++) {
413 if (ompt_state_info[i].state_id == current_state) {
414 *next_state = ompt_state_info[i + 1].state_id;
415 *next_state_name = ompt_state_info[i + 1].state_name;
423 OMPT_API_ROUTINE
int ompt_enumerate_mutex_impls(
int current_impl,
425 const char **next_impl_name) {
426 const static int len =
427 sizeof(kmp_mutex_impl_info) /
sizeof(kmp_mutex_impl_info_t);
429 for (i = 0; i < len - 1; i++) {
430 if (kmp_mutex_impl_info[i].
id != current_impl)
432 *next_impl = kmp_mutex_impl_info[i + 1].id;
433 *next_impl_name = kmp_mutex_impl_info[i + 1].name;
443 OMPT_API_ROUTINE ompt_set_result_t ompt_set_callback(ompt_callbacks_t which,
444 ompt_callback_t callback) {
447 #define ompt_event_macro(event_name, callback_type, event_id) \
449 ompt_callbacks.ompt_callback(event_name) = (callback_type)callback; \
450 ompt_enabled.event_name = (callback != 0); \
452 return ompt_event_implementation_status(event_name); \
454 return ompt_set_always;
456 FOREACH_OMPT_EVENT(ompt_event_macro)
458 #undef ompt_event_macro
461 return ompt_set_error;
465 OMPT_API_ROUTINE
int ompt_get_callback(ompt_callbacks_t which,
466 ompt_callback_t *callback) {
467 if (!ompt_enabled.enabled)
468 return ompt_get_callback_failure;
472 #define ompt_event_macro(event_name, callback_type, event_id) \
474 ompt_callback_t mycb = \
475 (ompt_callback_t)ompt_callbacks.ompt_callback(event_name); \
476 if (ompt_enabled.event_name && mycb) { \
478 return ompt_get_callback_success; \
480 return ompt_get_callback_failure; \
483 FOREACH_OMPT_EVENT(ompt_event_macro)
485 #undef ompt_event_macro
488 return ompt_get_callback_failure;
496 OMPT_API_ROUTINE
int ompt_get_parallel_info(
int ancestor_level,
497 ompt_data_t **parallel_data,
499 if (!ompt_enabled.enabled)
501 return __ompt_get_parallel_info_internal(ancestor_level, parallel_data,
505 OMPT_API_ROUTINE
int ompt_get_state(ompt_wait_id_t *wait_id) {
506 if (!ompt_enabled.enabled)
507 return ompt_state_work_serial;
508 int thread_state = __ompt_get_state_internal(wait_id);
510 if (thread_state == ompt_state_undefined) {
511 thread_state = ompt_state_work_serial;
521 OMPT_API_ROUTINE ompt_data_t *ompt_get_thread_data(
void) {
522 if (!ompt_enabled.enabled)
524 return __ompt_get_thread_data_internal();
527 OMPT_API_ROUTINE
int ompt_get_task_info(
int ancestor_level,
int *type,
528 ompt_data_t **task_data,
529 ompt_frame_t **task_frame,
530 ompt_data_t **parallel_data,
532 if (!ompt_enabled.enabled)
534 return __ompt_get_task_info_internal(ancestor_level, type, task_data,
535 task_frame, parallel_data, thread_num);
538 OMPT_API_ROUTINE
int ompt_get_task_memory(
void **addr,
size_t *size,
540 return __ompt_get_task_memory_internal(addr, size, block);
547 OMPT_API_ROUTINE
int ompt_get_num_procs(
void) {
550 return __kmp_avail_proc;
557 OMPT_API_ROUTINE
int ompt_get_num_places(
void) {
559 #if !KMP_AFFINITY_SUPPORTED
562 if (!KMP_AFFINITY_CAPABLE())
564 return __kmp_affinity_num_masks;
568 OMPT_API_ROUTINE
int ompt_get_place_proc_ids(
int place_num,
int ids_size,
571 #if !KMP_AFFINITY_SUPPORTED
575 int tmp_ids[ids_size];
576 if (!KMP_AFFINITY_CAPABLE())
578 if (place_num < 0 || place_num >= (
int)__kmp_affinity_num_masks)
582 kmp_affin_mask_t *mask = KMP_CPU_INDEX(__kmp_affinity_masks, place_num);
584 KMP_CPU_SET_ITERATE(i, mask) {
585 if ((!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) ||
586 (!KMP_CPU_ISSET(i, mask))) {
589 if (count < ids_size)
593 if (ids_size >= count) {
594 for (i = 0; i < count; i++) {
602 OMPT_API_ROUTINE
int ompt_get_place_num(
void) {
604 #if !KMP_AFFINITY_SUPPORTED
607 if (!ompt_enabled.enabled || __kmp_get_gtid() < 0)
612 if (!KMP_AFFINITY_CAPABLE())
614 gtid = __kmp_entry_gtid();
615 thread = __kmp_thread_from_gtid(gtid);
616 if (thread == NULL || thread->th.th_current_place < 0)
618 return thread->th.th_current_place;
622 OMPT_API_ROUTINE
int ompt_get_partition_place_nums(
int place_nums_size,
625 #if !KMP_AFFINITY_SUPPORTED
628 if (!ompt_enabled.enabled || __kmp_get_gtid() < 0)
631 int i, gtid, place_num, first_place, last_place, start, end;
633 if (!KMP_AFFINITY_CAPABLE())
635 gtid = __kmp_entry_gtid();
636 thread = __kmp_thread_from_gtid(gtid);
639 first_place = thread->th.th_first_place;
640 last_place = thread->th.th_last_place;
641 if (first_place < 0 || last_place < 0)
643 if (first_place <= last_place) {
650 if (end - start <= place_nums_size)
651 for (i = 0, place_num = start; place_num <= end; ++place_num, ++i) {
652 place_nums[i] = place_num;
654 return end - start + 1;
662 OMPT_API_ROUTINE
int ompt_get_proc_id(
void) {
663 if (!ompt_enabled.enabled || __kmp_get_gtid() < 0)
666 return sched_getcpu();
669 GetCurrentProcessorNumberEx(&pn);
670 return 64 * pn.Group + pn.Number;
693 int __kmp_control_tool(uint64_t command, uint64_t modifier,
void *arg) {
695 if (ompt_enabled.enabled) {
696 if (ompt_enabled.ompt_callback_control_tool) {
697 return ompt_callbacks.ompt_callback(ompt_callback_control_tool)(
698 command, modifier, arg, OMPT_LOAD_RETURN_ADDRESS(__kmp_entry_gtid()));
711 OMPT_API_ROUTINE uint64_t ompt_get_unique_id(
void) {
712 return __ompt_get_unique_id_internal();
715 OMPT_API_ROUTINE
void ompt_finalize_tool(
void) { __kmp_internal_end_atexit(); }
721 OMPT_API_ROUTINE
int ompt_get_target_info(uint64_t *device_num,
722 ompt_id_t *target_id,
723 ompt_id_t *host_op_id) {
727 OMPT_API_ROUTINE
int ompt_get_num_devices(
void) {
735 static ompt_interface_fn_t ompt_fn_lookup(
const char *s) {
737 #define ompt_interface_fn(fn) \
738 fn##_t fn##_f = fn; \
739 if (strcmp(s, #fn) == 0) \
740 return (ompt_interface_fn_t)fn##_f;
742 FOREACH_OMPT_INQUIRY_FN(ompt_interface_fn)
744 return (ompt_interface_fn_t)0;