ompt-general.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. /*
  2. * ompt-general.cpp -- OMPT implementation of interface functions
  3. */
  4. //===----------------------------------------------------------------------===//
  5. //
  6. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  7. // See https://llvm.org/LICENSE.txt for license information.
  8. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  9. //
  10. //===----------------------------------------------------------------------===//
  11. /*****************************************************************************
  12. * system include files
  13. ****************************************************************************/
  14. #include <assert.h>
  15. #include <stdint.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #if KMP_OS_UNIX
  20. #include <dlfcn.h>
  21. #endif
  22. /*****************************************************************************
  23. * ompt include files
  24. ****************************************************************************/
  25. #include "ompt-specific.cpp"
  26. /*****************************************************************************
  27. * macros
  28. ****************************************************************************/
  29. #define ompt_get_callback_success 1
  30. #define ompt_get_callback_failure 0
  31. #define no_tool_present 0
  32. #define OMPT_API_ROUTINE static
  33. #ifndef OMPT_STR_MATCH
  34. #define OMPT_STR_MATCH(haystack, needle) (!strcasecmp(haystack, needle))
  35. #endif
  36. // prints for an enabled OMP_TOOL_VERBOSE_INIT.
  37. // In the future a prefix could be added in the first define, the second define
  38. // omits the prefix to allow for continued lines. Example: "PREFIX: Start
  39. // tool... Success." instead of "PREFIX: Start tool... PREFIX: Success."
  40. #define OMPT_VERBOSE_INIT_PRINT(...) \
  41. if (verbose_init) \
  42. fprintf(verbose_file, __VA_ARGS__)
  43. #define OMPT_VERBOSE_INIT_CONTINUED_PRINT(...) \
  44. if (verbose_init) \
  45. fprintf(verbose_file, __VA_ARGS__)
  46. static FILE *verbose_file;
  47. static int verbose_init;
  48. /*****************************************************************************
  49. * types
  50. ****************************************************************************/
  51. typedef struct {
  52. const char *state_name;
  53. ompt_state_t state_id;
  54. } ompt_state_info_t;
  55. typedef struct {
  56. const char *name;
  57. kmp_mutex_impl_t id;
  58. } kmp_mutex_impl_info_t;
  59. enum tool_setting_e {
  60. omp_tool_error,
  61. omp_tool_unset,
  62. omp_tool_disabled,
  63. omp_tool_enabled
  64. };
  65. /*****************************************************************************
  66. * global variables
  67. ****************************************************************************/
  68. ompt_callbacks_active_t ompt_enabled;
  69. ompt_state_info_t ompt_state_info[] = {
  70. #define ompt_state_macro(state, code) {#state, state},
  71. FOREACH_OMPT_STATE(ompt_state_macro)
  72. #undef ompt_state_macro
  73. };
  74. kmp_mutex_impl_info_t kmp_mutex_impl_info[] = {
  75. #define kmp_mutex_impl_macro(name, id) {#name, name},
  76. FOREACH_KMP_MUTEX_IMPL(kmp_mutex_impl_macro)
  77. #undef kmp_mutex_impl_macro
  78. };
  79. ompt_callbacks_internal_t ompt_callbacks;
  80. static ompt_start_tool_result_t *ompt_start_tool_result = NULL;
  81. #if KMP_OS_WINDOWS
  82. static HMODULE ompt_tool_module = NULL;
  83. #define OMPT_DLCLOSE(Lib) FreeLibrary(Lib)
  84. #else
  85. static void *ompt_tool_module = NULL;
  86. #define OMPT_DLCLOSE(Lib) dlclose(Lib)
  87. #endif
  88. /*****************************************************************************
  89. * forward declarations
  90. ****************************************************************************/
  91. static ompt_interface_fn_t ompt_fn_lookup(const char *s);
  92. OMPT_API_ROUTINE ompt_data_t *ompt_get_thread_data(void);
  93. /*****************************************************************************
  94. * initialization and finalization (private operations)
  95. ****************************************************************************/
  96. typedef ompt_start_tool_result_t *(*ompt_start_tool_t)(unsigned int,
  97. const char *);
  98. #if KMP_OS_DARWIN
  99. // While Darwin supports weak symbols, the library that wishes to provide a new
  100. // implementation has to link against this runtime which defeats the purpose
  101. // of having tools that are agnostic of the underlying runtime implementation.
  102. //
  103. // Fortunately, the linker includes all symbols of an executable in the global
  104. // symbol table by default so dlsym() even finds static implementations of
  105. // ompt_start_tool. For this to work on Linux, -Wl,--export-dynamic needs to be
  106. // passed when building the application which we don't want to rely on.
  107. static ompt_start_tool_result_t *ompt_tool_darwin(unsigned int omp_version,
  108. const char *runtime_version) {
  109. ompt_start_tool_result_t *ret = NULL;
  110. // Search symbol in the current address space.
  111. ompt_start_tool_t start_tool =
  112. (ompt_start_tool_t)dlsym(RTLD_DEFAULT, "ompt_start_tool");
  113. if (start_tool) {
  114. ret = start_tool(omp_version, runtime_version);
  115. }
  116. return ret;
  117. }
  118. #elif OMPT_HAVE_WEAK_ATTRIBUTE
  119. // On Unix-like systems that support weak symbols the following implementation
  120. // of ompt_start_tool() will be used in case no tool-supplied implementation of
  121. // this function is present in the address space of a process.
  122. _OMP_EXTERN OMPT_WEAK_ATTRIBUTE ompt_start_tool_result_t *
  123. ompt_start_tool(unsigned int omp_version, const char *runtime_version) {
  124. ompt_start_tool_result_t *ret = NULL;
  125. // Search next symbol in the current address space. This can happen if the
  126. // runtime library is linked before the tool. Since glibc 2.2 strong symbols
  127. // don't override weak symbols that have been found before unless the user
  128. // sets the environment variable LD_DYNAMIC_WEAK.
  129. ompt_start_tool_t next_tool =
  130. (ompt_start_tool_t)dlsym(RTLD_NEXT, "ompt_start_tool");
  131. if (next_tool) {
  132. ret = next_tool(omp_version, runtime_version);
  133. }
  134. return ret;
  135. }
  136. #elif OMPT_HAVE_PSAPI
  137. // On Windows, the ompt_tool_windows function is used to find the
  138. // ompt_start_tool symbol across all modules loaded by a process. If
  139. // ompt_start_tool is found, ompt_start_tool's return value is used to
  140. // initialize the tool. Otherwise, NULL is returned and OMPT won't be enabled.
  141. #include <psapi.h>
  142. #pragma comment(lib, "psapi.lib")
  143. // The number of loaded modules to start enumeration with EnumProcessModules()
  144. #define NUM_MODULES 128
  145. static ompt_start_tool_result_t *
  146. ompt_tool_windows(unsigned int omp_version, const char *runtime_version) {
  147. int i;
  148. DWORD needed, new_size;
  149. HMODULE *modules;
  150. HANDLE process = GetCurrentProcess();
  151. modules = (HMODULE *)malloc(NUM_MODULES * sizeof(HMODULE));
  152. ompt_start_tool_t ompt_tool_p = NULL;
  153. #if OMPT_DEBUG
  154. printf("ompt_tool_windows(): looking for ompt_start_tool\n");
  155. #endif
  156. if (!EnumProcessModules(process, modules, NUM_MODULES * sizeof(HMODULE),
  157. &needed)) {
  158. // Regardless of the error reason use the stub initialization function
  159. free(modules);
  160. return NULL;
  161. }
  162. // Check if NUM_MODULES is enough to list all modules
  163. new_size = needed / sizeof(HMODULE);
  164. if (new_size > NUM_MODULES) {
  165. #if OMPT_DEBUG
  166. printf("ompt_tool_windows(): resize buffer to %d bytes\n", needed);
  167. #endif
  168. modules = (HMODULE *)realloc(modules, needed);
  169. // If resizing failed use the stub function.
  170. if (!EnumProcessModules(process, modules, needed, &needed)) {
  171. free(modules);
  172. return NULL;
  173. }
  174. }
  175. for (i = 0; i < new_size; ++i) {
  176. (FARPROC &)ompt_tool_p = GetProcAddress(modules[i], "ompt_start_tool");
  177. if (ompt_tool_p) {
  178. #if OMPT_DEBUG
  179. TCHAR modName[MAX_PATH];
  180. if (GetModuleFileName(modules[i], modName, MAX_PATH))
  181. printf("ompt_tool_windows(): ompt_start_tool found in module %s\n",
  182. modName);
  183. #endif
  184. free(modules);
  185. return (*ompt_tool_p)(omp_version, runtime_version);
  186. }
  187. #if OMPT_DEBUG
  188. else {
  189. TCHAR modName[MAX_PATH];
  190. if (GetModuleFileName(modules[i], modName, MAX_PATH))
  191. printf("ompt_tool_windows(): ompt_start_tool not found in module %s\n",
  192. modName);
  193. }
  194. #endif
  195. }
  196. free(modules);
  197. return NULL;
  198. }
  199. #else
  200. #error Activation of OMPT is not supported on this platform.
  201. #endif
  202. static ompt_start_tool_result_t *
  203. ompt_try_start_tool(unsigned int omp_version, const char *runtime_version) {
  204. ompt_start_tool_result_t *ret = NULL;
  205. ompt_start_tool_t start_tool = NULL;
  206. #if KMP_OS_WINDOWS
  207. // Cannot use colon to describe a list of absolute paths on Windows
  208. const char *sep = ";";
  209. #else
  210. const char *sep = ":";
  211. #endif
  212. OMPT_VERBOSE_INIT_PRINT("----- START LOGGING OF TOOL REGISTRATION -----\n");
  213. OMPT_VERBOSE_INIT_PRINT("Search for OMP tool in current address space... ");
  214. #if KMP_OS_DARWIN
  215. // Try in the current address space
  216. ret = ompt_tool_darwin(omp_version, runtime_version);
  217. #elif OMPT_HAVE_WEAK_ATTRIBUTE
  218. ret = ompt_start_tool(omp_version, runtime_version);
  219. #elif OMPT_HAVE_PSAPI
  220. ret = ompt_tool_windows(omp_version, runtime_version);
  221. #else
  222. #error Activation of OMPT is not supported on this platform.
  223. #endif
  224. if (ret) {
  225. OMPT_VERBOSE_INIT_CONTINUED_PRINT("Success.\n");
  226. OMPT_VERBOSE_INIT_PRINT(
  227. "Tool was started and is using the OMPT interface.\n");
  228. OMPT_VERBOSE_INIT_PRINT("----- END LOGGING OF TOOL REGISTRATION -----\n");
  229. return ret;
  230. }
  231. // Try tool-libraries-var ICV
  232. OMPT_VERBOSE_INIT_CONTINUED_PRINT("Failed.\n");
  233. const char *tool_libs = getenv("OMP_TOOL_LIBRARIES");
  234. if (tool_libs) {
  235. OMPT_VERBOSE_INIT_PRINT("Searching tool libraries...\n");
  236. OMPT_VERBOSE_INIT_PRINT("OMP_TOOL_LIBRARIES = %s\n", tool_libs);
  237. char *libs = __kmp_str_format("%s", tool_libs);
  238. char *buf;
  239. char *fname = __kmp_str_token(libs, sep, &buf);
  240. // Reset dl-error
  241. dlerror();
  242. while (fname) {
  243. #if KMP_OS_UNIX
  244. OMPT_VERBOSE_INIT_PRINT("Opening %s... ", fname);
  245. void *h = dlopen(fname, RTLD_LAZY);
  246. if (!h) {
  247. OMPT_VERBOSE_INIT_CONTINUED_PRINT("Failed: %s\n", dlerror());
  248. } else {
  249. OMPT_VERBOSE_INIT_CONTINUED_PRINT("Success. \n");
  250. OMPT_VERBOSE_INIT_PRINT("Searching for ompt_start_tool in %s... ",
  251. fname);
  252. dlerror(); // Clear any existing error
  253. start_tool = (ompt_start_tool_t)dlsym(h, "ompt_start_tool");
  254. if (!start_tool) {
  255. char *error = dlerror();
  256. if (error != NULL) {
  257. OMPT_VERBOSE_INIT_CONTINUED_PRINT("Failed: %s\n", error);
  258. } else {
  259. OMPT_VERBOSE_INIT_CONTINUED_PRINT("Failed: %s\n",
  260. "ompt_start_tool = NULL");
  261. }
  262. } else
  263. #elif KMP_OS_WINDOWS
  264. OMPT_VERBOSE_INIT_PRINT("Opening %s... ", fname);
  265. HMODULE h = LoadLibrary(fname);
  266. if (!h) {
  267. OMPT_VERBOSE_INIT_CONTINUED_PRINT("Failed: Error %u\n",
  268. (unsigned)GetLastError());
  269. } else {
  270. OMPT_VERBOSE_INIT_CONTINUED_PRINT("Success. \n");
  271. OMPT_VERBOSE_INIT_PRINT("Searching for ompt_start_tool in %s... ",
  272. fname);
  273. start_tool = (ompt_start_tool_t)GetProcAddress(h, "ompt_start_tool");
  274. if (!start_tool) {
  275. OMPT_VERBOSE_INIT_CONTINUED_PRINT("Failed: Error %u\n",
  276. (unsigned)GetLastError());
  277. } else
  278. #else
  279. #error Activation of OMPT is not supported on this platform.
  280. #endif
  281. { // if (start_tool)
  282. ret = (*start_tool)(omp_version, runtime_version);
  283. if (ret) {
  284. OMPT_VERBOSE_INIT_CONTINUED_PRINT("Success.\n");
  285. OMPT_VERBOSE_INIT_PRINT(
  286. "Tool was started and is using the OMPT interface.\n");
  287. ompt_tool_module = h;
  288. break;
  289. }
  290. OMPT_VERBOSE_INIT_CONTINUED_PRINT(
  291. "Found but not using the OMPT interface.\n");
  292. OMPT_VERBOSE_INIT_PRINT("Continuing search...\n");
  293. }
  294. OMPT_DLCLOSE(h);
  295. }
  296. fname = __kmp_str_token(NULL, sep, &buf);
  297. }
  298. __kmp_str_free(&libs);
  299. } else {
  300. OMPT_VERBOSE_INIT_PRINT("No OMP_TOOL_LIBRARIES defined.\n");
  301. }
  302. // usable tool found in tool-libraries
  303. if (ret) {
  304. OMPT_VERBOSE_INIT_PRINT("----- END LOGGING OF TOOL REGISTRATION -----\n");
  305. return ret;
  306. }
  307. #if KMP_OS_UNIX
  308. { // Non-standard: load archer tool if application is built with TSan
  309. const char *fname = "libarcher.so";
  310. OMPT_VERBOSE_INIT_PRINT(
  311. "...searching tool libraries failed. Using archer tool.\n");
  312. OMPT_VERBOSE_INIT_PRINT("Opening %s... ", fname);
  313. void *h = dlopen(fname, RTLD_LAZY);
  314. if (h) {
  315. OMPT_VERBOSE_INIT_CONTINUED_PRINT("Success.\n");
  316. OMPT_VERBOSE_INIT_PRINT("Searching for ompt_start_tool in %s... ", fname);
  317. start_tool = (ompt_start_tool_t)dlsym(h, "ompt_start_tool");
  318. if (start_tool) {
  319. ret = (*start_tool)(omp_version, runtime_version);
  320. if (ret) {
  321. OMPT_VERBOSE_INIT_CONTINUED_PRINT("Success.\n");
  322. OMPT_VERBOSE_INIT_PRINT(
  323. "Tool was started and is using the OMPT interface.\n");
  324. OMPT_VERBOSE_INIT_PRINT(
  325. "----- END LOGGING OF TOOL REGISTRATION -----\n");
  326. return ret;
  327. }
  328. OMPT_VERBOSE_INIT_CONTINUED_PRINT(
  329. "Found but not using the OMPT interface.\n");
  330. } else {
  331. OMPT_VERBOSE_INIT_CONTINUED_PRINT("Failed: %s\n", dlerror());
  332. }
  333. }
  334. }
  335. #endif
  336. OMPT_VERBOSE_INIT_PRINT("No OMP tool loaded.\n");
  337. OMPT_VERBOSE_INIT_PRINT("----- END LOGGING OF TOOL REGISTRATION -----\n");
  338. return ret;
  339. }
  340. void ompt_pre_init() {
  341. //--------------------------------------------------
  342. // Execute the pre-initialization logic only once.
  343. //--------------------------------------------------
  344. static int ompt_pre_initialized = 0;
  345. if (ompt_pre_initialized)
  346. return;
  347. ompt_pre_initialized = 1;
  348. //--------------------------------------------------
  349. // Use a tool iff a tool is enabled and available.
  350. //--------------------------------------------------
  351. const char *ompt_env_var = getenv("OMP_TOOL");
  352. tool_setting_e tool_setting = omp_tool_error;
  353. if (!ompt_env_var || !strcmp(ompt_env_var, ""))
  354. tool_setting = omp_tool_unset;
  355. else if (OMPT_STR_MATCH(ompt_env_var, "disabled"))
  356. tool_setting = omp_tool_disabled;
  357. else if (OMPT_STR_MATCH(ompt_env_var, "enabled"))
  358. tool_setting = omp_tool_enabled;
  359. const char *ompt_env_verbose_init = getenv("OMP_TOOL_VERBOSE_INIT");
  360. // possible options: disabled | stdout | stderr | <filename>
  361. // if set, not empty and not disabled -> prepare for logging
  362. if (ompt_env_verbose_init && strcmp(ompt_env_verbose_init, "") &&
  363. !OMPT_STR_MATCH(ompt_env_verbose_init, "disabled")) {
  364. verbose_init = 1;
  365. if (OMPT_STR_MATCH(ompt_env_verbose_init, "STDERR"))
  366. verbose_file = stderr;
  367. else if (OMPT_STR_MATCH(ompt_env_verbose_init, "STDOUT"))
  368. verbose_file = stdout;
  369. else
  370. verbose_file = fopen(ompt_env_verbose_init, "w");
  371. } else
  372. verbose_init = 0;
  373. #if OMPT_DEBUG
  374. printf("ompt_pre_init(): tool_setting = %d\n", tool_setting);
  375. #endif
  376. switch (tool_setting) {
  377. case omp_tool_disabled:
  378. OMPT_VERBOSE_INIT_PRINT("OMP tool disabled. \n");
  379. break;
  380. case omp_tool_unset:
  381. case omp_tool_enabled:
  382. //--------------------------------------------------
  383. // Load tool iff specified in environment variable
  384. //--------------------------------------------------
  385. ompt_start_tool_result =
  386. ompt_try_start_tool(__kmp_openmp_version, ompt_get_runtime_version());
  387. memset(&ompt_enabled, 0, sizeof(ompt_enabled));
  388. break;
  389. case omp_tool_error:
  390. fprintf(stderr,
  391. "Warning: OMP_TOOL has invalid value \"%s\".\n"
  392. " legal values are (NULL,\"\",\"disabled\","
  393. "\"enabled\").\n",
  394. ompt_env_var);
  395. break;
  396. }
  397. if (verbose_init && verbose_file != stderr && verbose_file != stdout)
  398. fclose(verbose_file);
  399. #if OMPT_DEBUG
  400. printf("ompt_pre_init(): ompt_enabled = %d\n", ompt_enabled);
  401. #endif
  402. }
  403. extern "C" int omp_get_initial_device(void);
  404. void ompt_post_init() {
  405. //--------------------------------------------------
  406. // Execute the post-initialization logic only once.
  407. //--------------------------------------------------
  408. static int ompt_post_initialized = 0;
  409. if (ompt_post_initialized)
  410. return;
  411. ompt_post_initialized = 1;
  412. //--------------------------------------------------
  413. // Initialize the tool if so indicated.
  414. //--------------------------------------------------
  415. if (ompt_start_tool_result) {
  416. ompt_enabled.enabled = !!ompt_start_tool_result->initialize(
  417. ompt_fn_lookup, omp_get_initial_device(),
  418. &(ompt_start_tool_result->tool_data));
  419. if (!ompt_enabled.enabled) {
  420. // tool not enabled, zero out the bitmap, and done
  421. memset(&ompt_enabled, 0, sizeof(ompt_enabled));
  422. return;
  423. }
  424. kmp_info_t *root_thread = ompt_get_thread();
  425. ompt_set_thread_state(root_thread, ompt_state_overhead);
  426. if (ompt_enabled.ompt_callback_thread_begin) {
  427. ompt_callbacks.ompt_callback(ompt_callback_thread_begin)(
  428. ompt_thread_initial, __ompt_get_thread_data_internal());
  429. }
  430. ompt_data_t *task_data;
  431. ompt_data_t *parallel_data;
  432. __ompt_get_task_info_internal(0, NULL, &task_data, NULL, &parallel_data,
  433. NULL);
  434. if (ompt_enabled.ompt_callback_implicit_task) {
  435. ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
  436. ompt_scope_begin, parallel_data, task_data, 1, 1, ompt_task_initial);
  437. }
  438. ompt_set_thread_state(root_thread, ompt_state_work_serial);
  439. }
  440. }
  441. void ompt_fini() {
  442. if (ompt_enabled.enabled
  443. #if OMPD_SUPPORT
  444. && ompt_start_tool_result && ompt_start_tool_result->finalize
  445. #endif
  446. ) {
  447. ompt_start_tool_result->finalize(&(ompt_start_tool_result->tool_data));
  448. }
  449. if (ompt_tool_module)
  450. OMPT_DLCLOSE(ompt_tool_module);
  451. memset(&ompt_enabled, 0, sizeof(ompt_enabled));
  452. }
  453. /*****************************************************************************
  454. * interface operations
  455. ****************************************************************************/
  456. /*****************************************************************************
  457. * state
  458. ****************************************************************************/
  459. OMPT_API_ROUTINE int ompt_enumerate_states(int current_state, int *next_state,
  460. const char **next_state_name) {
  461. const static int len = sizeof(ompt_state_info) / sizeof(ompt_state_info_t);
  462. int i = 0;
  463. for (i = 0; i < len - 1; i++) {
  464. if (ompt_state_info[i].state_id == current_state) {
  465. *next_state = ompt_state_info[i + 1].state_id;
  466. *next_state_name = ompt_state_info[i + 1].state_name;
  467. return 1;
  468. }
  469. }
  470. return 0;
  471. }
  472. OMPT_API_ROUTINE int ompt_enumerate_mutex_impls(int current_impl,
  473. int *next_impl,
  474. const char **next_impl_name) {
  475. const static int len =
  476. sizeof(kmp_mutex_impl_info) / sizeof(kmp_mutex_impl_info_t);
  477. int i = 0;
  478. for (i = 0; i < len - 1; i++) {
  479. if (kmp_mutex_impl_info[i].id != current_impl)
  480. continue;
  481. *next_impl = kmp_mutex_impl_info[i + 1].id;
  482. *next_impl_name = kmp_mutex_impl_info[i + 1].name;
  483. return 1;
  484. }
  485. return 0;
  486. }
  487. /*****************************************************************************
  488. * callbacks
  489. ****************************************************************************/
  490. OMPT_API_ROUTINE ompt_set_result_t ompt_set_callback(ompt_callbacks_t which,
  491. ompt_callback_t callback) {
  492. switch (which) {
  493. #define ompt_event_macro(event_name, callback_type, event_id) \
  494. case event_name: \
  495. ompt_callbacks.ompt_callback(event_name) = (callback_type)callback; \
  496. ompt_enabled.event_name = (callback != 0); \
  497. if (callback) \
  498. return ompt_event_implementation_status(event_name); \
  499. else \
  500. return ompt_set_always;
  501. FOREACH_OMPT_EVENT(ompt_event_macro)
  502. #undef ompt_event_macro
  503. default:
  504. return ompt_set_error;
  505. }
  506. }
  507. OMPT_API_ROUTINE int ompt_get_callback(ompt_callbacks_t which,
  508. ompt_callback_t *callback) {
  509. if (!ompt_enabled.enabled)
  510. return ompt_get_callback_failure;
  511. switch (which) {
  512. #define ompt_event_macro(event_name, callback_type, event_id) \
  513. case event_name: { \
  514. ompt_callback_t mycb = \
  515. (ompt_callback_t)ompt_callbacks.ompt_callback(event_name); \
  516. if (ompt_enabled.event_name && mycb) { \
  517. *callback = mycb; \
  518. return ompt_get_callback_success; \
  519. } \
  520. return ompt_get_callback_failure; \
  521. }
  522. FOREACH_OMPT_EVENT(ompt_event_macro)
  523. #undef ompt_event_macro
  524. default:
  525. return ompt_get_callback_failure;
  526. }
  527. }
  528. /*****************************************************************************
  529. * parallel regions
  530. ****************************************************************************/
  531. OMPT_API_ROUTINE int ompt_get_parallel_info(int ancestor_level,
  532. ompt_data_t **parallel_data,
  533. int *team_size) {
  534. if (!ompt_enabled.enabled)
  535. return 0;
  536. return __ompt_get_parallel_info_internal(ancestor_level, parallel_data,
  537. team_size);
  538. }
  539. OMPT_API_ROUTINE int ompt_get_state(ompt_wait_id_t *wait_id) {
  540. if (!ompt_enabled.enabled)
  541. return ompt_state_work_serial;
  542. int thread_state = __ompt_get_state_internal(wait_id);
  543. if (thread_state == ompt_state_undefined) {
  544. thread_state = ompt_state_work_serial;
  545. }
  546. return thread_state;
  547. }
  548. /*****************************************************************************
  549. * tasks
  550. ****************************************************************************/
  551. OMPT_API_ROUTINE ompt_data_t *ompt_get_thread_data(void) {
  552. if (!ompt_enabled.enabled)
  553. return NULL;
  554. return __ompt_get_thread_data_internal();
  555. }
  556. OMPT_API_ROUTINE int ompt_get_task_info(int ancestor_level, int *type,
  557. ompt_data_t **task_data,
  558. ompt_frame_t **task_frame,
  559. ompt_data_t **parallel_data,
  560. int *thread_num) {
  561. if (!ompt_enabled.enabled)
  562. return 0;
  563. return __ompt_get_task_info_internal(ancestor_level, type, task_data,
  564. task_frame, parallel_data, thread_num);
  565. }
  566. OMPT_API_ROUTINE int ompt_get_task_memory(void **addr, size_t *size,
  567. int block) {
  568. return __ompt_get_task_memory_internal(addr, size, block);
  569. }
  570. /*****************************************************************************
  571. * num_procs
  572. ****************************************************************************/
  573. OMPT_API_ROUTINE int ompt_get_num_procs(void) {
  574. // copied from kmp_ftn_entry.h (but modified: OMPT can only be called when
  575. // runtime is initialized)
  576. return __kmp_avail_proc;
  577. }
  578. /*****************************************************************************
  579. * places
  580. ****************************************************************************/
  581. OMPT_API_ROUTINE int ompt_get_num_places(void) {
  582. // copied from kmp_ftn_entry.h (but modified)
  583. #if !KMP_AFFINITY_SUPPORTED
  584. return 0;
  585. #else
  586. if (!KMP_AFFINITY_CAPABLE())
  587. return 0;
  588. return __kmp_affinity_num_masks;
  589. #endif
  590. }
  591. OMPT_API_ROUTINE int ompt_get_place_proc_ids(int place_num, int ids_size,
  592. int *ids) {
  593. // copied from kmp_ftn_entry.h (but modified)
  594. #if !KMP_AFFINITY_SUPPORTED
  595. return 0;
  596. #else
  597. int i, count;
  598. int tmp_ids[ids_size];
  599. for (int j = 0; j < ids_size; j++)
  600. tmp_ids[j] = 0;
  601. if (!KMP_AFFINITY_CAPABLE())
  602. return 0;
  603. if (place_num < 0 || place_num >= (int)__kmp_affinity_num_masks)
  604. return 0;
  605. /* TODO: Is this safe for asynchronous call from signal handler during runtime
  606. * shutdown? */
  607. kmp_affin_mask_t *mask = KMP_CPU_INDEX(__kmp_affinity_masks, place_num);
  608. count = 0;
  609. KMP_CPU_SET_ITERATE(i, mask) {
  610. if ((!KMP_CPU_ISSET(i, __kmp_affin_fullMask)) ||
  611. (!KMP_CPU_ISSET(i, mask))) {
  612. continue;
  613. }
  614. if (count < ids_size)
  615. tmp_ids[count] = i;
  616. count++;
  617. }
  618. if (ids_size >= count) {
  619. for (i = 0; i < count; i++) {
  620. ids[i] = tmp_ids[i];
  621. }
  622. }
  623. return count;
  624. #endif
  625. }
  626. OMPT_API_ROUTINE int ompt_get_place_num(void) {
  627. // copied from kmp_ftn_entry.h (but modified)
  628. #if !KMP_AFFINITY_SUPPORTED
  629. return -1;
  630. #else
  631. if (!ompt_enabled.enabled || __kmp_get_gtid() < 0)
  632. return -1;
  633. int gtid;
  634. kmp_info_t *thread;
  635. if (!KMP_AFFINITY_CAPABLE())
  636. return -1;
  637. gtid = __kmp_entry_gtid();
  638. thread = __kmp_thread_from_gtid(gtid);
  639. if (thread == NULL || thread->th.th_current_place < 0)
  640. return -1;
  641. return thread->th.th_current_place;
  642. #endif
  643. }
  644. OMPT_API_ROUTINE int ompt_get_partition_place_nums(int place_nums_size,
  645. int *place_nums) {
  646. // copied from kmp_ftn_entry.h (but modified)
  647. #if !KMP_AFFINITY_SUPPORTED
  648. return 0;
  649. #else
  650. if (!ompt_enabled.enabled || __kmp_get_gtid() < 0)
  651. return 0;
  652. int i, gtid, place_num, first_place, last_place, start, end;
  653. kmp_info_t *thread;
  654. if (!KMP_AFFINITY_CAPABLE())
  655. return 0;
  656. gtid = __kmp_entry_gtid();
  657. thread = __kmp_thread_from_gtid(gtid);
  658. if (thread == NULL)
  659. return 0;
  660. first_place = thread->th.th_first_place;
  661. last_place = thread->th.th_last_place;
  662. if (first_place < 0 || last_place < 0)
  663. return 0;
  664. if (first_place <= last_place) {
  665. start = first_place;
  666. end = last_place;
  667. } else {
  668. start = last_place;
  669. end = first_place;
  670. }
  671. if (end - start <= place_nums_size)
  672. for (i = 0, place_num = start; place_num <= end; ++place_num, ++i) {
  673. place_nums[i] = place_num;
  674. }
  675. return end - start + 1;
  676. #endif
  677. }
  678. /*****************************************************************************
  679. * places
  680. ****************************************************************************/
  681. OMPT_API_ROUTINE int ompt_get_proc_id(void) {
  682. if (!ompt_enabled.enabled || __kmp_get_gtid() < 0)
  683. return -1;
  684. #if KMP_HAVE_SCHED_GETCPU
  685. return sched_getcpu();
  686. #elif KMP_OS_WINDOWS
  687. PROCESSOR_NUMBER pn;
  688. GetCurrentProcessorNumberEx(&pn);
  689. return 64 * pn.Group + pn.Number;
  690. #else
  691. return -1;
  692. #endif
  693. }
  694. /*****************************************************************************
  695. * compatability
  696. ****************************************************************************/
  697. /*
  698. * Currently unused function
  699. OMPT_API_ROUTINE int ompt_get_ompt_version() { return OMPT_VERSION; }
  700. */
  701. /*****************************************************************************
  702. * application-facing API
  703. ****************************************************************************/
  704. /*----------------------------------------------------------------------------
  705. | control
  706. ---------------------------------------------------------------------------*/
  707. int __kmp_control_tool(uint64_t command, uint64_t modifier, void *arg) {
  708. if (ompt_enabled.enabled) {
  709. if (ompt_enabled.ompt_callback_control_tool) {
  710. return ompt_callbacks.ompt_callback(ompt_callback_control_tool)(
  711. command, modifier, arg, OMPT_LOAD_RETURN_ADDRESS(__kmp_entry_gtid()));
  712. } else {
  713. return -1;
  714. }
  715. } else {
  716. return -2;
  717. }
  718. }
  719. /*****************************************************************************
  720. * misc
  721. ****************************************************************************/
  722. OMPT_API_ROUTINE uint64_t ompt_get_unique_id(void) {
  723. return __ompt_get_unique_id_internal();
  724. }
  725. OMPT_API_ROUTINE void ompt_finalize_tool(void) { __kmp_internal_end_atexit(); }
  726. /*****************************************************************************
  727. * Target
  728. ****************************************************************************/
  729. OMPT_API_ROUTINE int ompt_get_target_info(uint64_t *device_num,
  730. ompt_id_t *target_id,
  731. ompt_id_t *host_op_id) {
  732. return 0; // thread is not in a target region
  733. }
  734. OMPT_API_ROUTINE int ompt_get_num_devices(void) {
  735. return 1; // only one device (the current device) is available
  736. }
  737. /*****************************************************************************
  738. * API inquiry for tool
  739. ****************************************************************************/
  740. static ompt_interface_fn_t ompt_fn_lookup(const char *s) {
  741. #define ompt_interface_fn(fn) \
  742. fn##_t fn##_f = fn; \
  743. if (strcmp(s, #fn) == 0) \
  744. return (ompt_interface_fn_t)fn##_f;
  745. FOREACH_OMPT_INQUIRY_FN(ompt_interface_fn)
  746. return NULL;
  747. }