11#include " magic_enum.hpp"
22#include " ompt.h.include"
33#include " xprof_utils.hpp"
4+ #include < algorithm>
5+ #include < iostream>
46#include < metababel/metababel.h>
5- #include < optional >
7+ #include < sstream >
68#include < string>
79#include < unordered_map>
810
911using hpt_function_name_omp_t = std::tuple<hostname_t , process_id_t , thread_id_t , std::string>;
1012
1113struct data_s {
1214 std::unordered_map<hpt_function_name_omp_t , uint64_t > host_start;
15+ std::unordered_map<std::tuple<std::string, std::string>, std::string> cached_build_name;
1316};
1417typedef struct data_s data_t ;
1518
@@ -19,7 +22,7 @@ static void btx_finalize_component(void *usr_data) { delete static_cast<data_t *
1922
2023// Get the start of the "ompt_scope" or return empty optional
2124static std::optional<int64_t > set_or_get_start (void *usr_data, hpt_function_name_omp_t key,
22- ompt_scope_endpoint_t endpoint, int64_t ts) {
25+ ompt_scope_endpoint_t endpoint, int64_t ts) {
2326 auto state = static_cast <data_t *>(usr_data);
2427 switch (endpoint) {
2528 case ompt_scope_begin:
@@ -34,96 +37,181 @@ static std::optional<int64_t> set_or_get_start(void *usr_data, hpt_function_name
3437 }
3538}
3639
37- static void host_op_callback (void *btx_handle, void *usr_data, int64_t ts, const char *hostname,
38- int64_t vpid, uint64_t vtid, ompt_scope_endpoint_t endpoint,
39- std::string const &op_name) {
40+ // Split on `delimiter`, return unique tokens in sorted order.
41+ static auto split (const std::string &str, char delimiter, size_t offset = 0 ) {
42+ std::vector<std::string> tokens;
43+ std::stringstream ss (str);
44+ ss.seekg (offset);
45+ std::string token;
46+ while (std::getline (ss, token, delimiter)) {
47+ if (!token.empty ())
48+ tokens.push_back (token);
49+ }
50+ return tokens;
51+ }
52+
53+ // Join a vector of strings with `delimiter` in between.
54+ static std::string join (const std::vector<std::string> &v, const std::string &delimiter) {
55+ std::stringstream ss;
56+ bool first = true ;
57+ for (const auto &s : v) {
58+ if (!first)
59+ ss << delimiter;
60+ first = false ;
61+ ss << s;
62+ }
63+ return ss.str ();
64+ }
65+
66+ inline std::string build_name (void *usr_data, const char *event_class_name, void *) {
67+ // Just strip and return
68+ return strip_event_class_name (event_class_name);
69+ }
70+
71+ // Remove element in `kind` who are in the `event_class_name`
72+ template <typename E, typename = std::enable_if_t <std::is_enum_v<E>>>
73+ static std::string build_name (void *usr_data, const char *event_class_name, E kind) {
74+ // ompt_callback_target_emi, ompt_target_enter_data -> ompt_callback_target_emi:enter_data
75+ // ompt_callback_target_emi, ompt_target -> ompt_callback_target_emi:target
76+ auto state = static_cast <data_t *>(usr_data);
77+
78+ // 1) Base name
79+ std::string base = strip_event_class_name (event_class_name);
80+ auto kindName = std::string{magic_enum::enum_name (kind)};
81+
82+ // 2) Cache
83+ const auto key = std::make_tuple (base, kindName);
84+ auto it = state->cached_build_name .find (key);
85+ if (it != state->cached_build_name .end ()) {
86+ return it->second ;
87+ }
88+
89+ // 3) Tokenize
90+ auto kindTokens = split (kindName, ' _' , strlen (" ompt_" ));
91+ auto baseTokens = split (base, ' _' , strlen (" ompt_" ));
92+
93+ // 4) set_difference
94+ std::vector<std::string> diffTokens;
95+ std::set_difference (kindTokens.begin (), kindTokens.end (), baseTokens.begin (), baseTokens.end (),
96+ std::back_inserter (diffTokens));
97+
98+ // 5) glue it back together
99+ std::string kindNameProcessed;
100+ if (diffTokens.empty ()) {
101+ kindNameProcessed = join (kindTokens, " _" );
102+ } else {
103+ kindNameProcessed = join (diffTokens, " _" );
104+ }
105+
106+ // 6) Update Cache and Return
107+ it = state->cached_build_name .insert (it, {key, base + " :" + kindNameProcessed});
108+ return it->second ;
109+ }
110+
111+ template <typename EnumType = void *>
112+ static void host_op_callback (void *btx_handle, void *usr_data, int64_t ts,
113+ const char *event_class_name, const char *hostname, int64_t vpid,
114+ uint64_t vtid, ompt_scope_endpoint_t endpoint,
115+ EnumType kind = EnumType()) {
116+
117+ std::string op_name = build_name (usr_data, event_class_name, kind);
118+
40119 if (auto start_ts = set_or_get_start (usr_data, {hostname, vpid, vtid, op_name}, endpoint, ts)) {
41120 const bool err = false ;
42121 btx_push_message_lttng_host (btx_handle, hostname, vpid, vtid, start_ts.value (), BACKEND_OMP,
43122 op_name.c_str (), (ts - start_ts.value ()), err);
44123 }
45124}
46125
47- static void host_and_traffic_op_callback (void *btx_handle, void *usr_data, int64_t ts,
48- const char *hostname, int64_t vpid, uint64_t vtid,
49- ompt_scope_endpoint_t endpoint, size_t bytes,
50- std::string const &op_name) {
126+ static void traffic_op_callback (void *btx_handle, void *usr_data, int64_t ts,
127+ const char *event_class_name, const char *hostname, int64_t vpid,
128+ uint64_t vtid, ompt_scope_endpoint_t endpoint, size_t bytes,
129+ ompt_target_data_op_t kind) {
130+
131+ std::string op_name = build_name (usr_data, event_class_name, kind);
132+
51133 if (auto start_ts = set_or_get_start (usr_data, {hostname, vpid, vtid, op_name}, endpoint, ts)) {
52- const bool err = false ;
53- btx_push_message_lttng_host (btx_handle, hostname, vpid, vtid, start_ts.value (), BACKEND_OMP,
54- op_name.c_str (), (ts - start_ts.value ()), err);
55134 btx_push_message_lttng_traffic (btx_handle, hostname, vpid, vtid, start_ts.value (), BACKEND_OMP,
56135 op_name.c_str (), bytes, " " );
57136 }
58137}
59138
60- static void ompt_callback_target_callback (void *btx_handle, void *usr_data, int64_t ts,
61- const char *hostname, int64_t vpid, uint64_t vtid,
62- ompt_target_t kind, ompt_scope_endpoint_t endpoint,
63- int device_num, ompt_data_t *task_data,
64- ompt_id_t target_id, void *codeptr_ra) {
65- std::string op_name (magic_enum::enum_name (kind));
66- host_op_callback (btx_handle, usr_data, ts, hostname, vpid, vtid, endpoint, op_name);
139+ // _
140+ // / _. | | |_ _. _ | _
141+ // \_ (_| | | |_) (_| (_ |< _>
142+ //
143+
144+ // Host: We support function with 'ompt_sync_region_t'.
145+ // We specialize with:
146+ // - ompt_sync_region_t
147+ // - ompt_target_t
148+ // - ompt_target_data_op_t
149+ static void btx_host_sync_region_callback (void *btx_handle, void *usr_data, int64_t ts,
150+ const char *event_class_name, const char *hostname,
151+ int64_t vpid, uint64_t vtid, ompt_sync_region_t kind,
152+ ompt_scope_endpoint_t endpoint) {
153+
154+ host_op_callback (btx_handle, usr_data, ts, event_class_name, hostname, vpid, vtid, endpoint,
155+ kind);
67156}
68157
69- static void ompt_callback_target_emi_callback (void *btx_handle, void *usr_data, int64_t ts,
70- const char *hostname, int64_t vpid, uint64_t vtid,
71- ompt_target_t kind, ompt_scope_endpoint_t endpoint,
72- int device_num, ompt_data_t *task_data,
73- ompt_data_t *target_task_data,
74- ompt_data_t *target_data, void *codeptr_ra) {
75- std::string op_name (magic_enum::enum_name (kind));
76- host_op_callback (btx_handle, usr_data, ts, hostname, vpid, vtid, endpoint, op_name);
77- }
158+ static void btx_host_target_callback (void *btx_handle, void *usr_data, int64_t ts,
159+ const char *event_class_name, const char *hostname,
160+ int64_t vpid, uint64_t vtid, ompt_target_t kind,
161+ ompt_scope_endpoint_t endpoint) {
78162
79- static void ompt_callback_target_data_op_callback (
80- void *btx_handle, void *usr_data, int64_t ts, const char *hostname, int64_t vpid, uint64_t vtid,
81- ompt_id_t target_id, ompt_id_t host_op_id, ompt_target_data_op_t optype, void *src_addr,
82- int src_device_num, void *dest_addr, int dest_device_num, size_t bytes, void *codeptr_ra) {
83- std::string op_name (magic_enum::enum_name (optype));
84- host_and_traffic_op_callback (btx_handle, usr_data, ts, hostname, vpid, vtid, ompt_scope_beginend,
85- bytes, op_name);
163+ host_op_callback (btx_handle, usr_data, ts, event_class_name, hostname, vpid, vtid, endpoint,
164+ kind);
86165}
87166
88- static void ompt_callback_target_data_op_emi_callback (
89- void *btx_handle, void *usr_data, int64_t ts, const char *hostname, int64_t vpid, uint64_t vtid ,
90- ompt_scope_endpoint_t endpoint, ompt_data_t *target_task_data, ompt_data_t *target_data ,
91- ompt_id_t *host_op_id, ompt_target_data_op_t optype, void *src_addr, int src_device_num ,
92- void *dest_addr, int dest_device_num, size_t bytes, void *codeptr_ra ) {
93- std::string op_name ( magic_enum::enum_name (optype));
94- host_and_traffic_op_callback (btx_handle, usr_data, ts, hostname, vpid, vtid, endpoint, bytes ,
95- op_name );
167+ static void btx_host_target_data_callback ( void *btx_handle, void *usr_data, int64_t ts,
168+ const char *event_class_name, const char *hostname ,
169+ int64_t vpid, uint64_t vtid ,
170+ ompt_scope_endpoint_t endpoint ,
171+ ompt_target_data_op_t optype ) {
172+
173+ host_op_callback (btx_handle, usr_data, ts, event_class_name, hostname, vpid, vtid, endpoint,
174+ optype );
96175}
97176
98- static void ompt_callback_target_submit_callback (void *btx_handle, void *usr_data, int64_t ts,
99- const char *hostname, int64_t vpid, uint64_t vtid ,
100- ompt_id_t target_id, ompt_id_t host_op_id,
101- unsigned int requested_num_teams) {
102- // Nothing we can do with them, but add a callback to they are removed from the trace
177+ static void btx_host_callback (void *btx_handle, void *usr_data, int64_t ts,
178+ const char *event_class_name, const char *hostname, int64_t vpid,
179+ uint64_t vtid, ompt_scope_endpoint_t endpoint) {
180+
181+ host_op_callback (btx_handle, usr_data, ts, event_class_name, hostname, vpid, vtid, endpoint);
103182}
104183
105- static void ompt_callback_target_submit_emi_callback (void *btx_handle, void *usr_data, int64_t ts,
106- const char *hostname, int64_t vpid,
107- uint64_t vtid, ompt_scope_endpoint_t endpoint,
108- ompt_data_t *target_data,
109- ompt_id_t *host_op_id,
110- unsigned int requested_num_teams) {
111- host_op_callback (btx_handle, usr_data, ts, hostname, vpid, vtid, endpoint,
112- " ompt_target_submit_emi" );
184+ // Traffic
185+
186+ static void btx_traffic_target_data_callback (void *btx_handle, void *usr_data, int64_t ts,
187+ const char *event_class_name, const char *hostname,
188+ int64_t vpid, uint64_t vtid,
189+ ompt_scope_endpoint_t endpoint,
190+ ompt_target_data_op_t optype, size_t bytes) {
191+
192+ traffic_op_callback (btx_handle, usr_data, ts, event_class_name, hostname, vpid, vtid, endpoint,
193+ bytes, optype);
113194}
114195
196+ // _ _
197+ // |_) _ _ o _ _|_ _ ._ / _. | | |_ _. _ | _
198+ // | \ (/_ (_| | _> |_ (/_ | \_ (_| | | |_) (_| (_ |< _>
199+ // _|
200+
115201#define REGISTER_ASSOCIATED_CALLBACK (base_name ) \
116- btx_register_callbacks_lttng_ust_ompt_ ##base_name(btx_handle, &base_name##_callback);
202+ btx_register_callbacks_ ##base_name(btx_handle, &base_name##_callback);
117203
118204void btx_register_usr_callbacks (void *btx_handle) {
205+
119206 btx_register_callbacks_initialize_component (btx_handle, &btx_initialize_component);
120207 btx_register_callbacks_finalize_component (btx_handle, &btx_finalize_component);
121- REGISTER_ASSOCIATED_CALLBACK (ompt_callback_target);
122- REGISTER_ASSOCIATED_CALLBACK (ompt_callback_target_emi);
123- REGISTER_ASSOCIATED_CALLBACK (ompt_callback_target_data_op);
124- REGISTER_ASSOCIATED_CALLBACK (ompt_callback_target_data_op_emi);
125- REGISTER_ASSOCIATED_CALLBACK (ompt_callback_target_submit);
126- REGISTER_ASSOCIATED_CALLBACK (ompt_callback_target_submit_emi);
208+
209+ REGISTER_ASSOCIATED_CALLBACK (btx_host_sync_region);
210+ REGISTER_ASSOCIATED_CALLBACK (btx_host_target);
211+ REGISTER_ASSOCIATED_CALLBACK (btx_host_target_data);
212+ REGISTER_ASSOCIATED_CALLBACK (btx_host);
213+
214+ REGISTER_ASSOCIATED_CALLBACK (btx_traffic_target_data);
127215}
128216
129217#undef REGISTER_ASSOCIATED_CALLBACK
0 commit comments