Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 1 | /***************************************************************************** |
| 2 | * system include files |
| 3 | ****************************************************************************/ |
| 4 | |
| 5 | #include <assert.h> |
| 6 | |
| 7 | #include <stdint.h> |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 12 | /***************************************************************************** |
| 13 | * ompt include files |
| 14 | ****************************************************************************/ |
| 15 | |
Jonathan Peyton | 7cc577a | 2016-12-14 22:39:11 +0000 | [diff] [blame] | 16 | #include "ompt-specific.cpp" |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 17 | |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 18 | /***************************************************************************** |
| 19 | * macros |
| 20 | ****************************************************************************/ |
| 21 | |
| 22 | #define ompt_get_callback_success 1 |
| 23 | #define ompt_get_callback_failure 0 |
| 24 | |
| 25 | #define no_tool_present 0 |
| 26 | |
| 27 | #define OMPT_API_ROUTINE static |
| 28 | |
Jonathan Peyton | 69e596a | 2015-10-29 20:56:24 +0000 | [diff] [blame] | 29 | #ifndef OMPT_STR_MATCH |
| 30 | #define OMPT_STR_MATCH(haystack, needle) (!strcasecmp(haystack, needle)) |
| 31 | #endif |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 32 | |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 33 | /***************************************************************************** |
| 34 | * types |
| 35 | ****************************************************************************/ |
| 36 | |
| 37 | typedef struct { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 38 | const char *state_name; |
| 39 | ompt_state_t state_id; |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 40 | } ompt_state_info_t; |
| 41 | |
Jonathan Peyton | 82a13bf | 2015-09-21 18:01:02 +0000 | [diff] [blame] | 42 | enum tool_setting_e { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 43 | omp_tool_error, |
| 44 | omp_tool_unset, |
| 45 | omp_tool_disabled, |
| 46 | omp_tool_enabled |
Jonathan Peyton | 82a13bf | 2015-09-21 18:01:02 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 49 | typedef void (*ompt_initialize_t)(ompt_function_lookup_t ompt_fn_lookup, |
| 50 | const char *version, |
| 51 | unsigned int ompt_version); |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 52 | |
| 53 | /***************************************************************************** |
| 54 | * global variables |
| 55 | ****************************************************************************/ |
| 56 | |
Jonathan Peyton | b68a85d | 2015-09-21 18:11:22 +0000 | [diff] [blame] | 57 | int ompt_enabled = 0; |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 58 | |
| 59 | ompt_state_info_t ompt_state_info[] = { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 60 | #define ompt_state_macro(state, code) {#state, state}, |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 61 | FOREACH_OMPT_STATE(ompt_state_macro) |
| 62 | #undef ompt_state_macro |
| 63 | }; |
| 64 | |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 65 | ompt_callbacks_t ompt_callbacks; |
| 66 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 67 | static ompt_initialize_t ompt_initialize_fn = NULL; |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 68 | |
| 69 | /***************************************************************************** |
| 70 | * forward declarations |
| 71 | ****************************************************************************/ |
| 72 | |
| 73 | static ompt_interface_fn_t ompt_fn_lookup(const char *s); |
| 74 | |
Jonathan Peyton | 82a13bf | 2015-09-21 18:01:02 +0000 | [diff] [blame] | 75 | OMPT_API_ROUTINE ompt_thread_id_t ompt_get_thread_id(void); |
| 76 | |
Jonathan Peyton | 82a13bf | 2015-09-21 18:01:02 +0000 | [diff] [blame] | 77 | /***************************************************************************** |
| 78 | * initialization and finalization (private operations) |
| 79 | ****************************************************************************/ |
| 80 | |
Jonathan Peyton | 69e596a | 2015-10-29 20:56:24 +0000 | [diff] [blame] | 81 | /* On Unix-like systems that support weak symbols the following implementation |
| 82 | * of ompt_tool() will be used in case no tool-supplied implementation of |
| 83 | * this function is present in the address space of a process. |
| 84 | * |
| 85 | * On Windows, the ompt_tool_windows function is used to find the |
| 86 | * ompt_tool symbol across all modules loaded by a process. If ompt_tool is |
| 87 | * found, ompt_tool's return value is used to initialize the tool. Otherwise, |
| 88 | * NULL is returned and OMPT won't be enabled */ |
| 89 | #if OMPT_HAVE_WEAK_ATTRIBUTE |
Jonathan Peyton | 6111849 | 2016-05-20 19:03:38 +0000 | [diff] [blame] | 90 | _OMP_EXTERN |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 91 | __attribute__((weak)) ompt_initialize_t ompt_tool() { |
Jonathan Peyton | 69e596a | 2015-10-29 20:56:24 +0000 | [diff] [blame] | 92 | #if OMPT_DEBUG |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 93 | printf("ompt_tool() is called from the RTL\n"); |
Jonathan Peyton | 69e596a | 2015-10-29 20:56:24 +0000 | [diff] [blame] | 94 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 95 | return NULL; |
Jonathan Peyton | 82a13bf | 2015-09-21 18:01:02 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Jonathan Peyton | 69e596a | 2015-10-29 20:56:24 +0000 | [diff] [blame] | 98 | #elif OMPT_HAVE_PSAPI |
| 99 | |
| 100 | #include <psapi.h> |
| 101 | #pragma comment(lib, "psapi.lib") |
| 102 | #define ompt_tool ompt_tool_windows |
| 103 | |
| 104 | // The number of loaded modules to start enumeration with EnumProcessModules() |
| 105 | #define NUM_MODULES 128 |
| 106 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 107 | static ompt_initialize_t ompt_tool_windows() { |
| 108 | int i; |
| 109 | DWORD needed, new_size; |
| 110 | HMODULE *modules; |
| 111 | HANDLE process = GetCurrentProcess(); |
| 112 | modules = (HMODULE *)malloc(NUM_MODULES * sizeof(HMODULE)); |
| 113 | ompt_initialize_t (*ompt_tool_p)() = NULL; |
Jonathan Peyton | 69e596a | 2015-10-29 20:56:24 +0000 | [diff] [blame] | 114 | |
| 115 | #if OMPT_DEBUG |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 116 | printf("ompt_tool_windows(): looking for ompt_tool\n"); |
Jonathan Peyton | 69e596a | 2015-10-29 20:56:24 +0000 | [diff] [blame] | 117 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 118 | if (!EnumProcessModules(process, modules, NUM_MODULES * sizeof(HMODULE), |
| 119 | &needed)) { |
| 120 | // Regardless of the error reason use the stub initialization function |
| 121 | free(modules); |
| 122 | return NULL; |
| 123 | } |
| 124 | // Check if NUM_MODULES is enough to list all modules |
| 125 | new_size = needed / sizeof(HMODULE); |
| 126 | if (new_size > NUM_MODULES) { |
Jonathan Peyton | 69e596a | 2015-10-29 20:56:24 +0000 | [diff] [blame] | 127 | #if OMPT_DEBUG |
| 128 | printf("ompt_tool_windows(): resize buffer to %d bytes\n", needed); |
| 129 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 130 | modules = (HMODULE *)realloc(modules, needed); |
| 131 | // If resizing failed use the stub function. |
| 132 | if (!EnumProcessModules(process, modules, needed, &needed)) { |
| 133 | free(modules); |
| 134 | return NULL; |
Jonathan Peyton | 69e596a | 2015-10-29 20:56:24 +0000 | [diff] [blame] | 135 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 136 | } |
| 137 | for (i = 0; i < new_size; ++i) { |
| 138 | (FARPROC &)ompt_tool_p = GetProcAddress(modules[i], "ompt_tool"); |
| 139 | if (ompt_tool_p) { |
Jonathan Peyton | 69e596a | 2015-10-29 20:56:24 +0000 | [diff] [blame] | 140 | #if OMPT_DEBUG |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 141 | TCHAR modName[MAX_PATH]; |
| 142 | if (GetModuleFileName(modules[i], modName, MAX_PATH)) |
| 143 | printf("ompt_tool_windows(): ompt_tool found in module %s\n", modName); |
Jonathan Peyton | 69e596a | 2015-10-29 20:56:24 +0000 | [diff] [blame] | 144 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 145 | free(modules); |
| 146 | return ompt_tool_p(); |
Jonathan Peyton | 69e596a | 2015-10-29 20:56:24 +0000 | [diff] [blame] | 147 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 148 | #if OMPT_DEBUG |
| 149 | else { |
| 150 | TCHAR modName[MAX_PATH]; |
| 151 | if (GetModuleFileName(modules[i], modName, MAX_PATH)) |
| 152 | printf("ompt_tool_windows(): ompt_tool not found in module %s\n", |
| 153 | modName); |
| 154 | } |
| 155 | #endif |
| 156 | } |
| 157 | free(modules); |
| 158 | return NULL; |
Jonathan Peyton | 69e596a | 2015-10-29 20:56:24 +0000 | [diff] [blame] | 159 | } |
| 160 | #else |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 161 | #error Either __attribute__((weak)) or psapi.dll are required for OMPT support |
Jonathan Peyton | 69e596a | 2015-10-29 20:56:24 +0000 | [diff] [blame] | 162 | #endif // OMPT_HAVE_WEAK_ATTRIBUTE |
Jonathan Peyton | 82a13bf | 2015-09-21 18:01:02 +0000 | [diff] [blame] | 163 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 164 | void ompt_pre_init() { |
| 165 | //-------------------------------------------------- |
| 166 | // Execute the pre-initialization logic only once. |
| 167 | //-------------------------------------------------- |
| 168 | static int ompt_pre_initialized = 0; |
Jonathan Peyton | 82a13bf | 2015-09-21 18:01:02 +0000 | [diff] [blame] | 169 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 170 | if (ompt_pre_initialized) |
| 171 | return; |
Jonathan Peyton | 82a13bf | 2015-09-21 18:01:02 +0000 | [diff] [blame] | 172 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 173 | ompt_pre_initialized = 1; |
Jonathan Peyton | 82a13bf | 2015-09-21 18:01:02 +0000 | [diff] [blame] | 174 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 175 | //-------------------------------------------------- |
| 176 | // Use a tool iff a tool is enabled and available. |
| 177 | //-------------------------------------------------- |
| 178 | const char *ompt_env_var = getenv("OMP_TOOL"); |
| 179 | tool_setting_e tool_setting = omp_tool_error; |
Jonathan Peyton | 82a13bf | 2015-09-21 18:01:02 +0000 | [diff] [blame] | 180 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 181 | if (!ompt_env_var || !strcmp(ompt_env_var, "")) |
| 182 | tool_setting = omp_tool_unset; |
| 183 | else if (OMPT_STR_MATCH(ompt_env_var, "disabled")) |
| 184 | tool_setting = omp_tool_disabled; |
| 185 | else if (OMPT_STR_MATCH(ompt_env_var, "enabled")) |
| 186 | tool_setting = omp_tool_enabled; |
Jonathan Peyton | 82a13bf | 2015-09-21 18:01:02 +0000 | [diff] [blame] | 187 | |
Jonathan Peyton | 69e596a | 2015-10-29 20:56:24 +0000 | [diff] [blame] | 188 | #if OMPT_DEBUG |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 189 | printf("ompt_pre_init(): tool_setting = %d\n", tool_setting); |
Jonathan Peyton | 69e596a | 2015-10-29 20:56:24 +0000 | [diff] [blame] | 190 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 191 | switch (tool_setting) { |
| 192 | case omp_tool_disabled: |
| 193 | break; |
Jonathan Peyton | 82a13bf | 2015-09-21 18:01:02 +0000 | [diff] [blame] | 194 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 195 | case omp_tool_unset: |
| 196 | case omp_tool_enabled: |
| 197 | ompt_initialize_fn = ompt_tool(); |
| 198 | if (ompt_initialize_fn) { |
| 199 | ompt_enabled = 1; |
Jonathan Peyton | 82a13bf | 2015-09-21 18:01:02 +0000 | [diff] [blame] | 200 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 201 | break; |
| 202 | |
| 203 | case omp_tool_error: |
| 204 | fprintf(stderr, "Warning: OMP_TOOL has invalid value \"%s\".\n" |
| 205 | " legal values are (NULL,\"\",\"disabled\"," |
| 206 | "\"enabled\").\n", |
| 207 | ompt_env_var); |
| 208 | break; |
| 209 | } |
Jonathan Peyton | 69e596a | 2015-10-29 20:56:24 +0000 | [diff] [blame] | 210 | #if OMPT_DEBUG |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 211 | printf("ompt_pre_init(): ompt_enabled = %d\n", ompt_enabled); |
Jonathan Peyton | 69e596a | 2015-10-29 20:56:24 +0000 | [diff] [blame] | 212 | #endif |
Jonathan Peyton | 82a13bf | 2015-09-21 18:01:02 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 215 | void ompt_post_init() { |
| 216 | //-------------------------------------------------- |
| 217 | // Execute the post-initialization logic only once. |
| 218 | //-------------------------------------------------- |
| 219 | static int ompt_post_initialized = 0; |
Jonathan Peyton | 82a13bf | 2015-09-21 18:01:02 +0000 | [diff] [blame] | 220 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 221 | if (ompt_post_initialized) |
| 222 | return; |
Jonathan Peyton | 82a13bf | 2015-09-21 18:01:02 +0000 | [diff] [blame] | 223 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 224 | ompt_post_initialized = 1; |
Jonathan Peyton | 82a13bf | 2015-09-21 18:01:02 +0000 | [diff] [blame] | 225 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 226 | //-------------------------------------------------- |
| 227 | // Initialize the tool if so indicated. |
| 228 | //-------------------------------------------------- |
| 229 | if (ompt_enabled) { |
| 230 | ompt_initialize_fn(ompt_fn_lookup, ompt_get_runtime_version(), |
| 231 | OMPT_VERSION); |
Jonathan Peyton | 82a13bf | 2015-09-21 18:01:02 +0000 | [diff] [blame] | 232 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 233 | ompt_thread_t *root_thread = ompt_get_thread(); |
Jonathan Peyton | 82a13bf | 2015-09-21 18:01:02 +0000 | [diff] [blame] | 234 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 235 | ompt_set_thread_state(root_thread, ompt_state_overhead); |
Jonathan Peyton | 82a13bf | 2015-09-21 18:01:02 +0000 | [diff] [blame] | 236 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 237 | if (ompt_callbacks.ompt_callback(ompt_event_thread_begin)) { |
| 238 | ompt_callbacks.ompt_callback(ompt_event_thread_begin)( |
| 239 | ompt_thread_initial, ompt_get_thread_id()); |
Jonathan Peyton | 82a13bf | 2015-09-21 18:01:02 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 242 | ompt_set_thread_state(root_thread, ompt_state_work_serial); |
| 243 | } |
Jonathan Peyton | 82a13bf | 2015-09-21 18:01:02 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 246 | void ompt_fini() { |
| 247 | if (ompt_enabled) { |
| 248 | if (ompt_callbacks.ompt_callback(ompt_event_runtime_shutdown)) { |
| 249 | ompt_callbacks.ompt_callback(ompt_event_runtime_shutdown)(); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | ompt_enabled = 0; |
| 254 | } |
Jonathan Peyton | 82a13bf | 2015-09-21 18:01:02 +0000 | [diff] [blame] | 255 | |
| 256 | /***************************************************************************** |
| 257 | * interface operations |
| 258 | ****************************************************************************/ |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 259 | |
| 260 | /***************************************************************************** |
| 261 | * state |
| 262 | ****************************************************************************/ |
| 263 | |
| 264 | OMPT_API_ROUTINE int ompt_enumerate_state(int current_state, int *next_state, |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 265 | const char **next_state_name) { |
| 266 | const static int len = sizeof(ompt_state_info) / sizeof(ompt_state_info_t); |
| 267 | int i = 0; |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 268 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 269 | for (i = 0; i < len - 1; i++) { |
| 270 | if (ompt_state_info[i].state_id == current_state) { |
| 271 | *next_state = ompt_state_info[i + 1].state_id; |
| 272 | *next_state_name = ompt_state_info[i + 1].state_name; |
| 273 | return 1; |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 274 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 275 | } |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 276 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 277 | return 0; |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 280 | /***************************************************************************** |
| 281 | * callbacks |
| 282 | ****************************************************************************/ |
| 283 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 284 | OMPT_API_ROUTINE int ompt_set_callback(ompt_event_t evid, ompt_callback_t cb) { |
| 285 | switch (evid) { |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 286 | |
| 287 | #define ompt_event_macro(event_name, callback_type, event_id) \ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 288 | case event_name: \ |
| 289 | if (ompt_event_implementation_status(event_name)) { \ |
| 290 | ompt_callbacks.ompt_callback(event_name) = (callback_type)cb; \ |
| 291 | } \ |
| 292 | return ompt_event_implementation_status(event_name); |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 293 | |
| 294 | FOREACH_OMPT_EVENT(ompt_event_macro) |
| 295 | |
| 296 | #undef ompt_event_macro |
| 297 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 298 | default: |
| 299 | return ompt_set_result_registration_error; |
| 300 | } |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 303 | OMPT_API_ROUTINE int ompt_get_callback(ompt_event_t evid, ompt_callback_t *cb) { |
| 304 | switch (evid) { |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 305 | |
| 306 | #define ompt_event_macro(event_name, callback_type, event_id) \ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 307 | case event_name: \ |
| 308 | if (ompt_event_implementation_status(event_name)) { \ |
| 309 | ompt_callback_t mycb = \ |
| 310 | (ompt_callback_t)ompt_callbacks.ompt_callback(event_name); \ |
| 311 | if (mycb) { \ |
| 312 | *cb = mycb; \ |
| 313 | return ompt_get_callback_success; \ |
| 314 | } \ |
| 315 | } \ |
| 316 | return ompt_get_callback_failure; |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 317 | |
| 318 | FOREACH_OMPT_EVENT(ompt_event_macro) |
| 319 | |
| 320 | #undef ompt_event_macro |
| 321 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 322 | default: |
| 323 | return ompt_get_callback_failure; |
| 324 | } |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 327 | /***************************************************************************** |
| 328 | * parallel regions |
| 329 | ****************************************************************************/ |
| 330 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 331 | OMPT_API_ROUTINE ompt_parallel_id_t ompt_get_parallel_id(int ancestor_level) { |
| 332 | return __ompt_get_parallel_id_internal(ancestor_level); |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 335 | OMPT_API_ROUTINE int ompt_get_parallel_team_size(int ancestor_level) { |
| 336 | return __ompt_get_parallel_team_size_internal(ancestor_level); |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 339 | OMPT_API_ROUTINE void *ompt_get_parallel_function(int ancestor_level) { |
| 340 | return __ompt_get_parallel_function_internal(ancestor_level); |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 343 | OMPT_API_ROUTINE ompt_state_t ompt_get_state(ompt_wait_id_t *ompt_wait_id) { |
| 344 | ompt_state_t thread_state = __ompt_get_state_internal(ompt_wait_id); |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 345 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 346 | if (thread_state == ompt_state_undefined) { |
| 347 | thread_state = ompt_state_work_serial; |
| 348 | } |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 349 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 350 | return thread_state; |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 353 | /***************************************************************************** |
| 354 | * threads |
| 355 | ****************************************************************************/ |
| 356 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 357 | OMPT_API_ROUTINE void *ompt_get_idle_frame() { |
| 358 | return __ompt_get_idle_frame_internal(); |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 361 | /***************************************************************************** |
| 362 | * tasks |
| 363 | ****************************************************************************/ |
| 364 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 365 | OMPT_API_ROUTINE ompt_thread_id_t ompt_get_thread_id(void) { |
| 366 | return __ompt_get_thread_id_internal(); |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 369 | OMPT_API_ROUTINE ompt_task_id_t ompt_get_task_id(int depth) { |
| 370 | return __ompt_get_task_id_internal(depth); |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 373 | OMPT_API_ROUTINE ompt_frame_t *ompt_get_task_frame(int depth) { |
| 374 | return __ompt_get_task_frame_internal(depth); |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 377 | OMPT_API_ROUTINE void *ompt_get_task_function(int depth) { |
| 378 | return __ompt_get_task_function_internal(depth); |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 381 | /***************************************************************************** |
| 382 | * placeholders |
| 383 | ****************************************************************************/ |
| 384 | |
Jonathan Peyton | 122dd76 | 2015-07-13 18:55:45 +0000 | [diff] [blame] | 385 | // Don't define this as static. The loader may choose to eliminate the symbol |
Jonathan Peyton | 6111849 | 2016-05-20 19:03:38 +0000 | [diff] [blame] | 386 | // even though it is needed by tools. |
| 387 | #define OMPT_API_PLACEHOLDER |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 388 | |
Jonathan Peyton | 122dd76 | 2015-07-13 18:55:45 +0000 | [diff] [blame] | 389 | // Ensure that placeholders don't have mangled names in the symbol table. |
| 390 | #ifdef __cplusplus |
| 391 | extern "C" { |
| 392 | #endif |
| 393 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 394 | OMPT_API_PLACEHOLDER void ompt_idle(void) { |
| 395 | // This function is a placeholder used to represent the calling context of |
| 396 | // idle OpenMP worker threads. It is not meant to be invoked. |
| 397 | assert(0); |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 400 | OMPT_API_PLACEHOLDER void ompt_overhead(void) { |
| 401 | // This function is a placeholder used to represent the OpenMP context of |
| 402 | // threads working in the OpenMP runtime. It is not meant to be invoked. |
| 403 | assert(0); |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 406 | OMPT_API_PLACEHOLDER void ompt_barrier_wait(void) { |
| 407 | // This function is a placeholder used to represent the OpenMP context of |
| 408 | // threads waiting for a barrier in the OpenMP runtime. It is not meant |
| 409 | // to be invoked. |
| 410 | assert(0); |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 413 | OMPT_API_PLACEHOLDER void ompt_task_wait(void) { |
| 414 | // This function is a placeholder used to represent the OpenMP context of |
| 415 | // threads waiting for a task in the OpenMP runtime. It is not meant |
| 416 | // to be invoked. |
| 417 | assert(0); |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 420 | OMPT_API_PLACEHOLDER void ompt_mutex_wait(void) { |
| 421 | // This function is a placeholder used to represent the OpenMP context of |
| 422 | // threads waiting for a mutex in the OpenMP runtime. It is not meant |
| 423 | // to be invoked. |
| 424 | assert(0); |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Jonathan Peyton | 122dd76 | 2015-07-13 18:55:45 +0000 | [diff] [blame] | 427 | #ifdef __cplusplus |
| 428 | }; |
| 429 | #endif |
| 430 | |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 431 | /***************************************************************************** |
| 432 | * compatability |
| 433 | ****************************************************************************/ |
| 434 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 435 | OMPT_API_ROUTINE int ompt_get_ompt_version() { return OMPT_VERSION; } |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 436 | |
| 437 | /***************************************************************************** |
| 438 | * application-facing API |
| 439 | ****************************************************************************/ |
| 440 | |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 441 | /*---------------------------------------------------------------------------- |
| 442 | | control |
| 443 | ---------------------------------------------------------------------------*/ |
| 444 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 445 | _OMP_EXTERN void ompt_control(uint64_t command, uint64_t modifier) { |
| 446 | if (ompt_enabled && ompt_callbacks.ompt_callback(ompt_event_control)) { |
| 447 | ompt_callbacks.ompt_callback(ompt_event_control)(command, modifier); |
| 448 | } |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 451 | /***************************************************************************** |
| 452 | * API inquiry for tool |
| 453 | ****************************************************************************/ |
| 454 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 455 | static ompt_interface_fn_t ompt_fn_lookup(const char *s) { |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 456 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 457 | #define ompt_interface_fn(fn) \ |
| 458 | if (strcmp(s, #fn) == 0) \ |
| 459 | return (ompt_interface_fn_t)fn; |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 460 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 461 | FOREACH_OMPT_INQUIRY_FN(ompt_interface_fn) |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 462 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 463 | FOREACH_OMPT_PLACEHOLDER_FN(ompt_interface_fn) |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 464 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 465 | return (ompt_interface_fn_t)0; |
Andrey Churbanov | e5f4492 | 2015-04-29 16:22:07 +0000 | [diff] [blame] | 466 | } |