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