ompt-specific.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * ompt-specific.h - header of OMPT internal functions implementation
  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. #ifndef OMPT_SPECIFIC_H
  12. #define OMPT_SPECIFIC_H
  13. #include "kmp.h"
  14. #if OMPT_SUPPORT
  15. /*****************************************************************************
  16. * forward declarations
  17. ****************************************************************************/
  18. void __ompt_team_assign_id(kmp_team_t *team, ompt_data_t ompt_pid);
  19. void __ompt_thread_assign_wait_id(void *variable);
  20. void __ompt_lw_taskteam_init(ompt_lw_taskteam_t *lwt, kmp_info_t *thr, int gtid,
  21. ompt_data_t *ompt_pid, void *codeptr);
  22. void __ompt_lw_taskteam_link(ompt_lw_taskteam_t *lwt, kmp_info_t *thr,
  23. int on_heap, bool always = false);
  24. void __ompt_lw_taskteam_unlink(kmp_info_t *thr);
  25. ompt_team_info_t *__ompt_get_teaminfo(int depth, int *size);
  26. ompt_task_info_t *__ompt_get_task_info_object(int depth);
  27. int __ompt_get_parallel_info_internal(int ancestor_level,
  28. ompt_data_t **parallel_data,
  29. int *team_size);
  30. int __ompt_get_task_info_internal(int ancestor_level, int *type,
  31. ompt_data_t **task_data,
  32. ompt_frame_t **task_frame,
  33. ompt_data_t **parallel_data, int *thread_num);
  34. ompt_data_t *__ompt_get_thread_data_internal();
  35. /*
  36. * Unused currently
  37. static uint64_t __ompt_get_get_unique_id_internal();
  38. */
  39. ompt_sync_region_t __ompt_get_barrier_kind(enum barrier_type, kmp_info_t *);
  40. /*****************************************************************************
  41. * macros
  42. ****************************************************************************/
  43. #define OMPT_CUR_TASK_INFO(thr) (&(thr->th.th_current_task->ompt_task_info))
  44. #define OMPT_CUR_TASK_DATA(thr) \
  45. (&(thr->th.th_current_task->ompt_task_info.task_data))
  46. #define OMPT_CUR_TEAM_INFO(thr) (&(thr->th.th_team->t.ompt_team_info))
  47. #define OMPT_CUR_TEAM_DATA(thr) \
  48. (&(thr->th.th_team->t.ompt_team_info.parallel_data))
  49. #define OMPT_HAVE_WEAK_ATTRIBUTE KMP_HAVE_WEAK_ATTRIBUTE
  50. #define OMPT_HAVE_PSAPI KMP_HAVE_PSAPI
  51. #define OMPT_STR_MATCH(haystack, needle) __kmp_str_match(haystack, 0, needle)
  52. inline void *__ompt_load_return_address(int gtid) {
  53. kmp_info_t *thr = __kmp_threads[gtid];
  54. void *return_address = thr->th.ompt_thread_info.return_address;
  55. thr->th.ompt_thread_info.return_address = NULL;
  56. return return_address;
  57. }
  58. /*#define OMPT_STORE_RETURN_ADDRESS(gtid) \
  59. if (ompt_enabled.enabled && gtid >= 0 && __kmp_threads[gtid] && \
  60. !__kmp_threads[gtid]->th.ompt_thread_info.return_address) \
  61. __kmp_threads[gtid]->th.ompt_thread_info.return_address = \
  62. __builtin_return_address(0)*/
  63. #define OMPT_STORE_RETURN_ADDRESS(gtid) \
  64. OmptReturnAddressGuard ReturnAddressGuard{gtid, __builtin_return_address(0)};
  65. #define OMPT_LOAD_RETURN_ADDRESS(gtid) __ompt_load_return_address(gtid)
  66. #define OMPT_LOAD_OR_GET_RETURN_ADDRESS(gtid) \
  67. ((ompt_enabled.enabled && gtid >= 0 && __kmp_threads[gtid] && \
  68. __kmp_threads[gtid]->th.ompt_thread_info.return_address) \
  69. ? __ompt_load_return_address(gtid) \
  70. : __builtin_return_address(0))
  71. #define OMPT_GET_DISPATCH_CHUNK(chunk, lb, ub, incr) \
  72. do { \
  73. if (incr > 0) { \
  74. chunk.start = static_cast<uint64_t>(lb); \
  75. chunk.iterations = static_cast<uint64_t>(((ub) - (lb)) / (incr) + 1); \
  76. } else { \
  77. chunk.start = static_cast<uint64_t>(ub); \
  78. chunk.iterations = static_cast<uint64_t>(((lb) - (ub)) / -(incr) + 1); \
  79. } \
  80. } while (0)
  81. //******************************************************************************
  82. // inline functions
  83. //******************************************************************************
  84. inline kmp_info_t *ompt_get_thread_gtid(int gtid) {
  85. return (gtid >= 0) ? __kmp_thread_from_gtid(gtid) : NULL;
  86. }
  87. inline kmp_info_t *ompt_get_thread() {
  88. int gtid = __kmp_get_gtid();
  89. return ompt_get_thread_gtid(gtid);
  90. }
  91. inline void ompt_set_thread_state(kmp_info_t *thread, ompt_state_t state) {
  92. if (thread)
  93. thread->th.ompt_thread_info.state = state;
  94. }
  95. inline const char *ompt_get_runtime_version() {
  96. return &__kmp_version_lib_ver[KMP_VERSION_MAGIC_LEN];
  97. }
  98. class OmptReturnAddressGuard {
  99. private:
  100. bool SetAddress{false};
  101. int Gtid;
  102. public:
  103. OmptReturnAddressGuard(int Gtid, void *ReturnAddress) : Gtid(Gtid) {
  104. if (ompt_enabled.enabled && Gtid >= 0 && __kmp_threads[Gtid] &&
  105. !__kmp_threads[Gtid]->th.ompt_thread_info.return_address) {
  106. SetAddress = true;
  107. __kmp_threads[Gtid]->th.ompt_thread_info.return_address = ReturnAddress;
  108. }
  109. }
  110. ~OmptReturnAddressGuard() {
  111. if (SetAddress)
  112. __kmp_threads[Gtid]->th.ompt_thread_info.return_address = NULL;
  113. }
  114. };
  115. #endif // OMPT_SUPPORT
  116. // macros providing the OMPT callbacks for reduction clause
  117. #if OMPT_SUPPORT && OMPT_OPTIONAL
  118. #define OMPT_REDUCTION_DECL(this_thr, gtid) \
  119. ompt_data_t *my_task_data = OMPT_CUR_TASK_DATA(this_thr); \
  120. ompt_data_t *my_parallel_data = OMPT_CUR_TEAM_DATA(this_thr); \
  121. void *return_address = OMPT_LOAD_RETURN_ADDRESS(gtid);
  122. #define OMPT_REDUCTION_BEGIN \
  123. if (ompt_enabled.enabled && ompt_enabled.ompt_callback_reduction) { \
  124. ompt_callbacks.ompt_callback(ompt_callback_reduction)( \
  125. ompt_sync_region_reduction, ompt_scope_begin, my_parallel_data, \
  126. my_task_data, return_address); \
  127. }
  128. #define OMPT_REDUCTION_END \
  129. if (ompt_enabled.enabled && ompt_enabled.ompt_callback_reduction) { \
  130. ompt_callbacks.ompt_callback(ompt_callback_reduction)( \
  131. ompt_sync_region_reduction, ompt_scope_end, my_parallel_data, \
  132. my_task_data, return_address); \
  133. }
  134. #else // OMPT_SUPPORT && OMPT_OPTIONAL
  135. #define OMPT_REDUCTION_DECL(this_thr, gtid)
  136. #define OMPT_REDUCTION_BEGIN
  137. #define OMPT_REDUCTION_END
  138. #endif // ! OMPT_SUPPORT && OMPT_OPTIONAL
  139. #endif