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 45 #ifndef OMPT_STR_MATCH 46 #define OMPT_STR_MATCH(haystack, needle) (!strcasecmp(haystack, needle)) 54 const char *state_name;
61 } kmp_mutex_impl_info_t;
74 ompt_callbacks_active_t ompt_enabled;
76 omp_state_info_t omp_state_info[] = {
77 #define omp_state_macro(state, code) {#state, state}, 78 FOREACH_OMP_STATE(omp_state_macro)
79 #undef omp_state_macro 82 kmp_mutex_impl_info_t kmp_mutex_impl_info[] = {
83 #define kmp_mutex_impl_macro(name, id) {#name, name}, 84 FOREACH_KMP_MUTEX_IMPL(kmp_mutex_impl_macro)
85 #undef kmp_mutex_impl_macro 88 ompt_callbacks_internal_t ompt_callbacks;
90 static ompt_start_tool_result_t *ompt_start_tool_result = NULL;
96 static ompt_interface_fn_t ompt_fn_lookup(
const char *s);
98 OMPT_API_ROUTINE ompt_data_t *ompt_get_thread_data(
void);
104 typedef ompt_start_tool_result_t *(*ompt_start_tool_t)(
unsigned int,
118 static ompt_start_tool_result_t *ompt_tool_darwin(
unsigned int omp_version,
119 const char *runtime_version) {
120 ompt_start_tool_result_t *ret = NULL;
122 ompt_start_tool_t start_tool =
123 (ompt_start_tool_t)dlsym(RTLD_DEFAULT,
"ompt_start_tool");
125 ret = start_tool(omp_version, runtime_version);
130 #elif OMPT_HAVE_WEAK_ATTRIBUTE 136 _OMP_EXTERN OMPT_WEAK_ATTRIBUTE ompt_start_tool_result_t *
137 ompt_start_tool(
unsigned int omp_version,
const char *runtime_version) {
138 ompt_start_tool_result_t *ret = NULL;
143 ompt_start_tool_t next_tool =
144 (ompt_start_tool_t)dlsym(RTLD_NEXT,
"ompt_start_tool");
146 ret = next_tool(omp_version, runtime_version);
151 #elif OMPT_HAVE_PSAPI 159 #pragma comment(lib, "psapi.lib") 162 #define NUM_MODULES 128 164 static ompt_start_tool_result_t *
165 ompt_tool_windows(
unsigned int omp_version,
const char *runtime_version) {
167 DWORD needed, new_size;
169 HANDLE process = GetCurrentProcess();
170 modules = (HMODULE *)malloc(NUM_MODULES *
sizeof(HMODULE));
171 ompt_start_tool_t ompt_tool_p = NULL;
174 printf(
"ompt_tool_windows(): looking for ompt_start_tool\n");
176 if (!EnumProcessModules(process, modules, NUM_MODULES *
sizeof(HMODULE),
183 new_size = needed /
sizeof(HMODULE);
184 if (new_size > NUM_MODULES) {
186 printf(
"ompt_tool_windows(): resize buffer to %d bytes\n", needed);
188 modules = (HMODULE *)realloc(modules, needed);
190 if (!EnumProcessModules(process, modules, needed, &needed)) {
195 for (i = 0; i < new_size; ++i) {
196 (FARPROC &)ompt_tool_p = GetProcAddress(modules[i],
"ompt_start_tool");
199 TCHAR modName[MAX_PATH];
200 if (GetModuleFileName(modules[i], modName, MAX_PATH))
201 printf(
"ompt_tool_windows(): ompt_start_tool found in module %s\n",
205 return (*ompt_tool_p)(omp_version, runtime_version);
209 TCHAR modName[MAX_PATH];
210 if (GetModuleFileName(modules[i], modName, MAX_PATH))
211 printf(
"ompt_tool_windows(): ompt_start_tool not found in module %s\n",
220 #error Activation of OMPT is not supported on this platform. 223 static ompt_start_tool_result_t *
224 ompt_try_start_tool(
unsigned int omp_version,
const char *runtime_version) {
225 ompt_start_tool_result_t *ret = NULL;
226 ompt_start_tool_t start_tool = NULL;
229 const char *sep =
";";
231 const char *sep =
":";
236 ret = ompt_tool_darwin(omp_version, runtime_version);
237 #elif OMPT_HAVE_WEAK_ATTRIBUTE 238 ret = ompt_start_tool(omp_version, runtime_version);
239 #elif OMPT_HAVE_PSAPI 240 ret = ompt_tool_windows(omp_version, runtime_version);
242 #error Activation of OMPT is not supported on this platform. 248 const char *tool_libs = getenv(
"OMP_TOOL_LIBRARIES");
250 char *libs = __kmp_str_format(
"%s", tool_libs);
252 char *fname = __kmp_str_token(libs, sep, &buf);
255 void *h = dlopen(fname, RTLD_LAZY);
257 start_tool = (ompt_start_tool_t)dlsym(h,
"ompt_start_tool");
259 HMODULE h = LoadLibrary(fname);
261 start_tool = (ompt_start_tool_t)GetProcAddress(h,
"ompt_start_tool");
263 #error Activation of OMPT is not supported on this platform. 265 if (start_tool && (ret = (*start_tool)(omp_version, runtime_version)))
268 fname = __kmp_str_token(NULL, sep, &buf);
270 __kmp_str_free(&libs);
275 void ompt_pre_init() {
279 static int ompt_pre_initialized = 0;
281 if (ompt_pre_initialized)
284 ompt_pre_initialized = 1;
289 const char *ompt_env_var = getenv(
"OMP_TOOL");
290 tool_setting_e tool_setting = omp_tool_error;
292 if (!ompt_env_var || !strcmp(ompt_env_var,
""))
293 tool_setting = omp_tool_unset;
294 else if (OMPT_STR_MATCH(ompt_env_var,
"disabled"))
295 tool_setting = omp_tool_disabled;
296 else if (OMPT_STR_MATCH(ompt_env_var,
"enabled"))
297 tool_setting = omp_tool_enabled;
300 printf(
"ompt_pre_init(): tool_setting = %d\n", tool_setting);
302 switch (tool_setting) {
303 case omp_tool_disabled:
307 case omp_tool_enabled:
312 ompt_start_tool_result =
313 ompt_try_start_tool(__kmp_openmp_version, ompt_get_runtime_version());
315 memset(&ompt_enabled, 0,
sizeof(ompt_enabled));
319 fprintf(stderr,
"Warning: OMP_TOOL has invalid value \"%s\".\n" 320 " legal values are (NULL,\"\",\"disabled\"," 326 printf(
"ompt_pre_init(): ompt_enabled = %d\n", ompt_enabled);
330 void ompt_post_init() {
334 static int ompt_post_initialized = 0;
336 if (ompt_post_initialized)
339 ompt_post_initialized = 1;
344 if (ompt_start_tool_result) {
345 ompt_enabled.enabled = !!ompt_start_tool_result->initialize(
346 ompt_fn_lookup, &(ompt_start_tool_result->tool_data));
348 if (!ompt_enabled.enabled) {
350 memset(&ompt_enabled, 0,
sizeof(ompt_enabled));
354 ompt_thread_t *root_thread = ompt_get_thread();
356 ompt_set_thread_state(root_thread, omp_state_overhead);
358 if (ompt_enabled.ompt_callback_thread_begin) {
359 ompt_callbacks.ompt_callback(ompt_callback_thread_begin)(
360 ompt_thread_initial, __ompt_get_thread_data_internal());
362 ompt_data_t *task_data;
363 __ompt_get_task_info_internal(0, NULL, &task_data, NULL, NULL, NULL);
364 if (ompt_enabled.ompt_callback_task_create) {
365 ompt_callbacks.ompt_callback(ompt_callback_task_create)(
366 NULL, NULL, task_data, ompt_task_initial, 0, NULL);
369 ompt_set_thread_state(root_thread, omp_state_work_serial);
374 if (ompt_enabled.enabled) {
375 ompt_start_tool_result->finalize(&(ompt_start_tool_result->tool_data));
378 memset(&ompt_enabled, 0,
sizeof(ompt_enabled));
389 OMPT_API_ROUTINE
int ompt_enumerate_states(
int current_state,
int *next_state,
390 const char **next_state_name) {
391 const static int len =
sizeof(omp_state_info) /
sizeof(omp_state_info_t);
394 for (i = 0; i < len - 1; i++) {
395 if (omp_state_info[i].state_id == current_state) {
396 *next_state = omp_state_info[i + 1].state_id;
397 *next_state_name = omp_state_info[i + 1].state_name;
405 OMPT_API_ROUTINE
int ompt_enumerate_mutex_impls(
int current_impl,
407 const char **next_impl_name) {
408 const static int len =
409 sizeof(kmp_mutex_impl_info) /
sizeof(kmp_mutex_impl_info_t);
411 for (i = 0; i < len - 1; i++) {
412 if (kmp_mutex_impl_info[i].
id != current_impl)
414 *next_impl = kmp_mutex_impl_info[i + 1].id;
415 *next_impl_name = kmp_mutex_impl_info[i + 1].name;
425 OMPT_API_ROUTINE
int ompt_set_callback(ompt_callbacks_t which,
426 ompt_callback_t callback) {
429 #define ompt_event_macro(event_name, callback_type, event_id) \ 431 if (ompt_event_implementation_status(event_name)) { \ 432 ompt_callbacks.ompt_callback(event_name) = (callback_type)callback; \ 433 ompt_enabled.event_name = (callback != 0); \ 436 return ompt_event_implementation_status(event_name); \ 438 return ompt_set_always; 440 FOREACH_OMPT_EVENT(ompt_event_macro)
442 #undef ompt_event_macro 445 return ompt_set_error;
449 OMPT_API_ROUTINE
int ompt_get_callback(ompt_callbacks_t which,
450 ompt_callback_t *callback) {
453 #define ompt_event_macro(event_name, callback_type, event_id) \ 455 if (ompt_event_implementation_status(event_name)) { \ 456 ompt_callback_t mycb = \ 457 (ompt_callback_t)ompt_callbacks.ompt_callback(event_name); \ 460 return ompt_get_callback_success; \ 463 return ompt_get_callback_failure; 465 FOREACH_OMPT_EVENT(ompt_event_macro)
467 #undef ompt_event_macro 470 return ompt_get_callback_failure;
478 OMPT_API_ROUTINE
int ompt_get_parallel_info(
int ancestor_level,
479 ompt_data_t **parallel_data,
481 return __ompt_get_parallel_info_internal(ancestor_level, parallel_data,
485 OMPT_API_ROUTINE omp_state_t ompt_get_state(omp_wait_id_t *wait_id) {
486 omp_state_t thread_state = __ompt_get_state_internal(wait_id);
488 if (thread_state == omp_state_undefined) {
489 thread_state = omp_state_work_serial;
499 OMPT_API_ROUTINE ompt_data_t *ompt_get_thread_data(
void) {
500 return __ompt_get_thread_data_internal();
503 OMPT_API_ROUTINE
int ompt_get_task_info(
int ancestor_level,
int *type,
504 ompt_data_t **task_data,
505 omp_frame_t **task_frame,
506 ompt_data_t **parallel_data,
508 return __ompt_get_task_info_internal(ancestor_level, type, task_data,
509 task_frame, parallel_data, thread_num);
516 OMPT_API_ROUTINE
int ompt_get_num_procs(
void) {
519 return __kmp_avail_proc;
526 OMPT_API_ROUTINE
int ompt_get_num_places(
void) {
528 #if !KMP_AFFINITY_SUPPORTED 531 if (!KMP_AFFINITY_CAPABLE())
533 return __kmp_affinity_num_masks;
537 OMPT_API_ROUTINE
int ompt_get_place_proc_ids(
int place_num,
int ids_size,
540 #if !KMP_AFFINITY_SUPPORTED 544 int tmp_ids[ids_size];
545 if (!KMP_AFFINITY_CAPABLE())
547 if (place_num < 0 || place_num >= (
int)__kmp_affinity_num_masks)
551 kmp_affin_mask_t *mask = KMP_CPU_INDEX(__kmp_affinity_masks, place_num);
553 KMP_CPU_SET_ITERATE(i, mask) {
554 if ((!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) ||
555 (!KMP_CPU_ISSET(i, mask))) {
558 if (count < ids_size)
562 if (ids_size >= count) {
563 for (i = 0; i < count; i++) {
571 OMPT_API_ROUTINE
int ompt_get_place_num(
void) {
573 #if !KMP_AFFINITY_SUPPORTED 576 if (__kmp_get_gtid() < 0)
581 if (!KMP_AFFINITY_CAPABLE())
583 gtid = __kmp_entry_gtid();
584 thread = __kmp_thread_from_gtid(gtid);
585 if (thread == NULL || thread->th.th_current_place < 0)
587 return thread->th.th_current_place;
591 OMPT_API_ROUTINE
int ompt_get_partition_place_nums(
int place_nums_size,
594 #if !KMP_AFFINITY_SUPPORTED 597 if (__kmp_get_gtid() < 0)
600 int i, gtid, place_num, first_place, last_place, start, end;
602 if (!KMP_AFFINITY_CAPABLE())
604 gtid = __kmp_entry_gtid();
605 thread = __kmp_thread_from_gtid(gtid);
608 first_place = thread->th.th_first_place;
609 last_place = thread->th.th_last_place;
610 if (first_place < 0 || last_place < 0)
612 if (first_place <= last_place) {
619 if (end - start <= place_nums_size)
620 for (i = 0, place_num = start; place_num <= end; ++place_num, ++i) {
621 place_nums[i] = place_num;
623 return end - start + 1;
631 OMPT_API_ROUTINE
int ompt_get_proc_id(
void) {
633 if (__kmp_get_gtid() < 0)
636 return sched_getcpu();
646 OMPT_API_ROUTINE
int ompt_get_ompt_version() {
return OMPT_VERSION; }
656 int __kmp_control_tool(uint64_t command, uint64_t modifier,
void *arg) {
658 if (ompt_enabled.enabled) {
659 if (ompt_enabled.ompt_callback_control_tool) {
660 return ompt_callbacks.ompt_callback(ompt_callback_control_tool)(
661 command, modifier, arg, OMPT_LOAD_RETURN_ADDRESS(__kmp_entry_gtid()));
674 OMPT_API_ROUTINE uint64_t ompt_get_unique_id(
void) {
675 return __ompt_get_unique_id_internal();
682 OMPT_API_ROUTINE
int ompt_get_target_info(uint64_t *device_num,
683 ompt_id_t *target_id,
684 ompt_id_t *host_op_id) {
688 OMPT_API_ROUTINE
int ompt_get_num_devices(
void) {
696 static ompt_interface_fn_t ompt_fn_lookup(
const char *s) {
698 #define ompt_interface_fn(fn) \ 699 fn##_t fn##_f = fn; \ 700 if (strcmp(s, #fn) == 0) \ 701 return (ompt_interface_fn_t)fn##_f; 703 FOREACH_OMPT_INQUIRY_FN(ompt_interface_fn)
705 return (ompt_interface_fn_t)0;