1 /*
2  * ompt-specific.h - header of OMPT internal functions implementation
3  */
4 
5 //===----------------------------------------------------------------------===//
6 //
7 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
8 // See https://llvm.org/LICENSE.txt for license information.
9 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef OMPT_SPECIFIC_H
14 #define OMPT_SPECIFIC_H
15 
16 #include "kmp.h"
17 
18 #if OMPT_SUPPORT
19 /*****************************************************************************
20  * forward declarations
21  ****************************************************************************/
22 
23 /// Entrypoint used by libomptarget to register callbacks in libomp, if not
24 /// done already
25 void __ompt_force_initialization();
26 
27 void __ompt_team_assign_id(kmp_team_t *team, ompt_data_t ompt_pid);
28 void __ompt_thread_assign_wait_id(void *variable);
29 
30 void __ompt_lw_taskteam_init(ompt_lw_taskteam_t *lwt, kmp_info_t *thr, int gtid,
31                              ompt_data_t *ompt_pid, void *codeptr);
32 
33 void __ompt_lw_taskteam_link(ompt_lw_taskteam_t *lwt, kmp_info_t *thr,
34                              int on_heap, bool always = false);
35 
36 void __ompt_lw_taskteam_unlink(kmp_info_t *thr);
37 
38 ompt_team_info_t *__ompt_get_teaminfo(int depth, int *size);
39 
40 ompt_task_info_t *__ompt_get_task_info_object(int depth);
41 
42 int __ompt_get_parallel_info_internal(int ancestor_level,
43                                       ompt_data_t **parallel_data,
44                                       int *team_size);
45 
46 int __ompt_get_task_info_internal(int ancestor_level, int *type,
47                                   ompt_data_t **task_data,
48                                   ompt_frame_t **task_frame,
49                                   ompt_data_t **parallel_data, int *thread_num);
50 
51 ompt_data_t *__ompt_get_thread_data_internal();
52 
53 /*
54  * Unused currently
55 static uint64_t __ompt_get_get_unique_id_internal();
56 */
57 
58 ompt_sync_region_t __ompt_get_barrier_kind(enum barrier_type, kmp_info_t *);
59 
60 /*****************************************************************************
61  * macros
62  ****************************************************************************/
63 
64 #define OMPT_CUR_TASK_INFO(thr) (&(thr->th.th_current_task->ompt_task_info))
65 #define OMPT_CUR_TASK_DATA(thr)                                                \
66   (&(thr->th.th_current_task->ompt_task_info.task_data))
67 #define OMPT_CUR_TEAM_INFO(thr) (&(thr->th.th_team->t.ompt_team_info))
68 #define OMPT_CUR_TEAM_DATA(thr)                                                \
69   (&(thr->th.th_team->t.ompt_team_info.parallel_data))
70 
71 #define OMPT_HAVE_WEAK_ATTRIBUTE KMP_HAVE_WEAK_ATTRIBUTE
72 #define OMPT_HAVE_PSAPI KMP_HAVE_PSAPI
73 #define OMPT_STR_MATCH(haystack, needle) __kmp_str_match(haystack, 0, needle)
74 
75 inline void *__ompt_load_return_address(int gtid) {
76   kmp_info_t *thr = __kmp_threads[gtid];
77   void *return_address = thr->th.ompt_thread_info.return_address;
78   thr->th.ompt_thread_info.return_address = NULL;
79   return return_address;
80 }
81 
82 /*#define OMPT_STORE_RETURN_ADDRESS(gtid) \
83   if (ompt_enabled.enabled && gtid >= 0 && __kmp_threads[gtid] &&              \
84       !__kmp_threads[gtid]->th.ompt_thread_info.return_address)                \
85   __kmp_threads[gtid]->th.ompt_thread_info.return_address =                    \
86       __builtin_return_address(0)*/
87 #define OMPT_STORE_RETURN_ADDRESS(gtid)                                        \
88   OmptReturnAddressGuard ReturnAddressGuard{gtid, __builtin_return_address(0)};
89 #define OMPT_LOAD_RETURN_ADDRESS(gtid) __ompt_load_return_address(gtid)
90 #define OMPT_LOAD_OR_GET_RETURN_ADDRESS(gtid)                                  \
91   ((ompt_enabled.enabled && gtid >= 0 && __kmp_threads[gtid] &&                \
92     __kmp_threads[gtid]->th.ompt_thread_info.return_address)                   \
93        ? __ompt_load_return_address(gtid)                                      \
94        : __builtin_return_address(0))
95 
96 #define OMPT_GET_DISPATCH_CHUNK(chunk, lb, ub, incr)                           \
97   do {                                                                         \
98     if (incr > 0) {                                                            \
99       chunk.start = static_cast<uint64_t>(lb);                                 \
100       chunk.iterations = static_cast<uint64_t>(((ub) - (lb)) / (incr) + 1);    \
101     } else {                                                                   \
102       chunk.start = static_cast<uint64_t>(ub);                                 \
103       chunk.iterations = static_cast<uint64_t>(((lb) - (ub)) / -(incr) + 1);   \
104     }                                                                          \
105   } while (0)
106 
107 //******************************************************************************
108 // inline functions
109 //******************************************************************************
110 
111 inline kmp_info_t *ompt_get_thread_gtid(int gtid) {
112   return (gtid >= 0) ? __kmp_thread_from_gtid(gtid) : NULL;
113 }
114 
115 inline kmp_info_t *ompt_get_thread() {
116   int gtid = __kmp_get_gtid();
117   return ompt_get_thread_gtid(gtid);
118 }
119 
120 inline void ompt_set_thread_state(kmp_info_t *thread, ompt_state_t state) {
121   if (thread)
122     thread->th.ompt_thread_info.state = state;
123 }
124 
125 inline const char *ompt_get_runtime_version() {
126   return &__kmp_version_lib_ver[KMP_VERSION_MAGIC_LEN];
127 }
128 
129 class OmptReturnAddressGuard {
130 private:
131   bool SetAddress{false};
132   int Gtid;
133 
134 public:
135   OmptReturnAddressGuard(int Gtid, void *ReturnAddress) : Gtid(Gtid) {
136     if (ompt_enabled.enabled && Gtid >= 0 && __kmp_threads[Gtid] &&
137         !__kmp_threads[Gtid]->th.ompt_thread_info.return_address) {
138       SetAddress = true;
139       __kmp_threads[Gtid]->th.ompt_thread_info.return_address = ReturnAddress;
140     }
141   }
142   ~OmptReturnAddressGuard() {
143     if (SetAddress)
144       __kmp_threads[Gtid]->th.ompt_thread_info.return_address = NULL;
145   }
146 };
147 
148 #endif // OMPT_SUPPORT
149 
150 // macros providing the OMPT callbacks for reduction clause
151 #if OMPT_SUPPORT && OMPT_OPTIONAL
152 #define OMPT_REDUCTION_DECL(this_thr, gtid)                                    \
153   ompt_data_t *my_task_data = OMPT_CUR_TASK_DATA(this_thr);                    \
154   ompt_data_t *my_parallel_data = OMPT_CUR_TEAM_DATA(this_thr);                \
155   void *return_address = OMPT_LOAD_RETURN_ADDRESS(gtid);
156 #define OMPT_REDUCTION_BEGIN                                                   \
157   if (ompt_enabled.enabled && ompt_enabled.ompt_callback_reduction) {          \
158     ompt_callbacks.ompt_callback(ompt_callback_reduction)(                     \
159         ompt_sync_region_reduction, ompt_scope_begin, my_parallel_data,        \
160         my_task_data, return_address);                                         \
161   }
162 #define OMPT_REDUCTION_END                                                     \
163   if (ompt_enabled.enabled && ompt_enabled.ompt_callback_reduction) {          \
164     ompt_callbacks.ompt_callback(ompt_callback_reduction)(                     \
165         ompt_sync_region_reduction, ompt_scope_end, my_parallel_data,          \
166         my_task_data, return_address);                                         \
167   }
168 #else // OMPT_SUPPORT && OMPT_OPTIONAL
169 #define OMPT_REDUCTION_DECL(this_thr, gtid)
170 #define OMPT_REDUCTION_BEGIN
171 #define OMPT_REDUCTION_END
172 #endif // ! OMPT_SUPPORT && OMPT_OPTIONAL
173 
174 #endif
175