18 #include "ompt-specific.cpp" 26 #define ompt_get_callback_success 1 27 #define ompt_get_callback_failure 0 29 #define no_tool_present 0 31 #define OMPT_API_ROUTINE static 33 #ifndef OMPT_STR_MATCH 34 #define OMPT_STR_MATCH(haystack, needle) (!strcasecmp(haystack, needle)) 43 const char *state_name;
44 ompt_state_t state_id;
56 typedef void (*ompt_initialize_t) (
57 ompt_function_lookup_t ompt_fn_lookup,
59 unsigned int ompt_version
70 ompt_state_info_t ompt_state_info[] = {
71 #define ompt_state_macro(state, code) { # state, state }, 72 FOREACH_OMPT_STATE(ompt_state_macro)
73 #undef ompt_state_macro 76 ompt_callbacks_t ompt_callbacks;
78 static ompt_initialize_t ompt_initialize_fn = NULL;
86 static ompt_interface_fn_t ompt_fn_lookup(
const char *s);
88 OMPT_API_ROUTINE ompt_thread_id_t ompt_get_thread_id(
void);
103 #if OMPT_HAVE_WEAK_ATTRIBUTE 105 __attribute__ (( weak ))
106 ompt_initialize_t ompt_tool()
109 printf(
"ompt_tool() is called from the RTL\n");
114 #elif OMPT_HAVE_PSAPI 117 #pragma comment(lib, "psapi.lib") 118 #define ompt_tool ompt_tool_windows 121 #define NUM_MODULES 128 124 ompt_initialize_t ompt_tool_windows()
127 DWORD needed, new_size;
129 HANDLE process = GetCurrentProcess();
130 modules = (HMODULE*)malloc( NUM_MODULES *
sizeof(HMODULE) );
131 ompt_initialize_t (*ompt_tool_p)() = NULL;
134 printf(
"ompt_tool_windows(): looking for ompt_tool\n");
136 if (!EnumProcessModules( process, modules, NUM_MODULES *
sizeof(HMODULE),
143 new_size = needed /
sizeof(HMODULE);
144 if (new_size > NUM_MODULES) {
146 printf(
"ompt_tool_windows(): resize buffer to %d bytes\n", needed);
148 modules = (HMODULE*)realloc( modules, needed );
150 if (!EnumProcessModules(process, modules, needed, &needed)) {
155 for (i = 0; i < new_size; ++i) {
156 (FARPROC &)ompt_tool_p = GetProcAddress(modules[i],
"ompt_tool");
159 TCHAR modName[MAX_PATH];
160 if (GetModuleFileName(modules[i], modName, MAX_PATH))
161 printf(
"ompt_tool_windows(): ompt_tool found in module %s\n",
165 return ompt_tool_p();
169 TCHAR modName[MAX_PATH];
170 if (GetModuleFileName(modules[i], modName, MAX_PATH))
171 printf(
"ompt_tool_windows(): ompt_tool not found in module %s\n",
180 # error Either __attribute__((weak)) or psapi.dll are required for OMPT support 181 #endif // OMPT_HAVE_WEAK_ATTRIBUTE 188 static int ompt_pre_initialized = 0;
190 if (ompt_pre_initialized)
return;
192 ompt_pre_initialized = 1;
197 const char *ompt_env_var = getenv(
"OMP_TOOL");
198 tool_setting_e tool_setting = omp_tool_error;
200 if (!ompt_env_var || !strcmp(ompt_env_var,
""))
201 tool_setting = omp_tool_unset;
202 else if (OMPT_STR_MATCH(ompt_env_var,
"disabled"))
203 tool_setting = omp_tool_disabled;
204 else if (OMPT_STR_MATCH(ompt_env_var,
"enabled"))
205 tool_setting = omp_tool_enabled;
208 printf(
"ompt_pre_init(): tool_setting = %d\n", tool_setting);
210 switch(tool_setting) {
211 case omp_tool_disabled:
215 case omp_tool_enabled:
216 ompt_initialize_fn = ompt_tool();
217 if (ompt_initialize_fn) {
224 "Warning: OMP_TOOL has invalid value \"%s\".\n" 225 " legal values are (NULL,\"\",\"disabled\"," 226 "\"enabled\").\n", ompt_env_var);
230 printf(
"ompt_pre_init(): ompt_enabled = %d\n", ompt_enabled);
235 void ompt_post_init()
240 static int ompt_post_initialized = 0;
242 if (ompt_post_initialized)
return;
244 ompt_post_initialized = 1;
250 ompt_initialize_fn(ompt_fn_lookup, ompt_get_runtime_version(),
253 ompt_thread_t *root_thread = ompt_get_thread();
255 ompt_set_thread_state(root_thread, ompt_state_overhead);
257 if (ompt_callbacks.ompt_callback(ompt_event_thread_begin)) {
258 ompt_callbacks.ompt_callback(ompt_event_thread_begin)
259 (ompt_thread_initial, ompt_get_thread_id());
262 ompt_set_thread_state(root_thread, ompt_state_work_serial);
270 if (ompt_callbacks.ompt_callback(ompt_event_runtime_shutdown)) {
271 ompt_callbacks.ompt_callback(ompt_event_runtime_shutdown)();
287 OMPT_API_ROUTINE
int ompt_enumerate_state(
int current_state,
int *next_state,
288 const char **next_state_name)
290 const static int len =
sizeof(ompt_state_info) /
sizeof(ompt_state_info_t);
293 for (i = 0; i < len - 1; i++) {
294 if (ompt_state_info[i].state_id == current_state) {
295 *next_state = ompt_state_info[i+1].state_id;
296 *next_state_name = ompt_state_info[i+1].state_name;
310 OMPT_API_ROUTINE
int ompt_set_callback(ompt_event_t evid, ompt_callback_t cb)
314 #define ompt_event_macro(event_name, callback_type, event_id) \ 316 if (ompt_event_implementation_status(event_name)) { \ 317 ompt_callbacks.ompt_callback(event_name) = (callback_type) cb; \ 319 return ompt_event_implementation_status(event_name); 321 FOREACH_OMPT_EVENT(ompt_event_macro)
323 #undef ompt_event_macro 325 default:
return ompt_set_result_registration_error;
330 OMPT_API_ROUTINE
int ompt_get_callback(ompt_event_t evid, ompt_callback_t *cb)
334 #define ompt_event_macro(event_name, callback_type, event_id) \ 336 if (ompt_event_implementation_status(event_name)) { \ 337 ompt_callback_t mycb = \ 338 (ompt_callback_t) ompt_callbacks.ompt_callback(event_name); \ 341 return ompt_get_callback_success; \ 344 return ompt_get_callback_failure; 346 FOREACH_OMPT_EVENT(ompt_event_macro)
348 #undef ompt_event_macro 350 default:
return ompt_get_callback_failure;
359 OMPT_API_ROUTINE ompt_parallel_id_t ompt_get_parallel_id(
int ancestor_level)
361 return __ompt_get_parallel_id_internal(ancestor_level);
365 OMPT_API_ROUTINE
int ompt_get_parallel_team_size(
int ancestor_level)
367 return __ompt_get_parallel_team_size_internal(ancestor_level);
371 OMPT_API_ROUTINE
void *ompt_get_parallel_function(
int ancestor_level)
373 return __ompt_get_parallel_function_internal(ancestor_level);
377 OMPT_API_ROUTINE ompt_state_t ompt_get_state(ompt_wait_id_t *ompt_wait_id)
379 ompt_state_t thread_state = __ompt_get_state_internal(ompt_wait_id);
381 if (thread_state == ompt_state_undefined) {
382 thread_state = ompt_state_work_serial;
395 OMPT_API_ROUTINE
void *ompt_get_idle_frame()
397 return __ompt_get_idle_frame_internal();
407 OMPT_API_ROUTINE ompt_thread_id_t ompt_get_thread_id(
void)
409 return __ompt_get_thread_id_internal();
412 OMPT_API_ROUTINE ompt_task_id_t ompt_get_task_id(
int depth)
414 return __ompt_get_task_id_internal(depth);
418 OMPT_API_ROUTINE ompt_frame_t *ompt_get_task_frame(
int depth)
420 return __ompt_get_task_frame_internal(depth);
424 OMPT_API_ROUTINE
void *ompt_get_task_function(
int depth)
426 return __ompt_get_task_function_internal(depth);
436 #define OMPT_API_PLACEHOLDER 444 OMPT_API_PLACEHOLDER
void ompt_idle(
void)
452 OMPT_API_PLACEHOLDER
void ompt_overhead(
void)
460 OMPT_API_PLACEHOLDER
void ompt_barrier_wait(
void)
469 OMPT_API_PLACEHOLDER
void ompt_task_wait(
void)
478 OMPT_API_PLACEHOLDER
void ompt_mutex_wait(
void)
495 OMPT_API_ROUTINE
int ompt_get_ompt_version()
511 _OMP_EXTERN
void ompt_control(uint64_t command, uint64_t modifier)
513 if (ompt_enabled && ompt_callbacks.ompt_callback(ompt_event_control)) {
514 ompt_callbacks.ompt_callback(ompt_event_control)(command, modifier);
524 static ompt_interface_fn_t ompt_fn_lookup(
const char *s)
527 #define ompt_interface_fn(fn) \ 528 if (strcmp(s, #fn) == 0) return (ompt_interface_fn_t) fn; 530 FOREACH_OMPT_INQUIRY_FN(ompt_interface_fn)
532 FOREACH_OMPT_PLACEHOLDER_FN(ompt_interface_fn)
534 return (ompt_interface_fn_t) 0;