HIP: Heterogenous-computing Interface for Portability
hip_prof_api.h
1 // automatically generated sources
2 #ifndef _HIP_PROF_API_H
3 #define _HIP_PROF_API_H
4 
5 #include <atomic>
6 #include <iostream>
7 #include <mutex>
8 
9 #include "hip/hcc_detail/hip_prof_str.h"
10 
11 template <typename Record, typename Fun, typename Act>
13  public:
14  typedef std::recursive_mutex mutex_t;
15 
16  typedef Record record_t;
17  typedef Fun fun_t;
18  typedef Act act_t;
19 
20  // HIP API callbacks table
22  volatile std::atomic<bool> sync;
23  volatile std::atomic<uint32_t> sem;
24  act_t act;
25  void* a_arg;
26  fun_t fun;
27  void* arg;
28  };
29 
30  struct hip_cb_table_t {
31  hip_cb_table_entry_t arr[HIP_API_ID_NUMBER];
32  };
33 
35  memset(&callbacks_table_, 0, sizeof(callbacks_table_));
36  }
37 
38  bool set_activity(uint32_t id, act_t fun, void* arg) {
39  std::lock_guard<mutex_t> lock(mutex_);
40  bool ret = true;
41  if (id < HIP_API_ID_NUMBER) {
42  cb_sync(id);
43  callbacks_table_.arr[id].act = fun;
44  callbacks_table_.arr[id].a_arg = arg;
45  cb_release(id);
46  } else {
47  ret = false;
48  }
49  return ret;
50  }
51 
52  bool set_callback(uint32_t id, fun_t fun, void* arg) {
53  std::lock_guard<mutex_t> lock(mutex_);
54  bool ret = true;
55  if (id < HIP_API_ID_NUMBER) {
56  cb_sync(id);
57  callbacks_table_.arr[id].fun = fun;
58  callbacks_table_.arr[id].arg = arg;
59  cb_release(id);
60  } else {
61  ret = false;
62  }
63  return ret;
64  }
65 
66  inline hip_cb_table_entry_t& entry(const uint32_t& id) {
67  return callbacks_table_.arr[id];
68  }
69 
70  inline void sem_sync(const uint32_t& id) {
71  sem_increment(id);
72  if (entry(id).sync.load() == true) sync_wait(id);
73  }
74 
75  inline void sem_release(const uint32_t& id) {
76  sem_decrement(id);
77  }
78 
79  private:
80  inline void cb_sync(const uint32_t& id) {
81  entry(id).sync.store(true);
82  while (entry(id).sem.load() != 0) {}
83  }
84 
85  inline void cb_release(const uint32_t& id) {
86  entry(id).sync.store(false);
87  }
88 
89  inline void sem_increment(const uint32_t& id) {
90  const uint32_t prev = entry(id).sem.fetch_add(1);
91  if (prev == UINT32_MAX) {
92  std::cerr << "sem overflow id = " << id << std::endl << std::flush;
93  abort();
94  }
95  }
96 
97  inline void sem_decrement(const uint32_t& id) {
98  const uint32_t prev = entry(id).sem.fetch_sub(1);
99  if (prev == 0) {
100  std::cerr << "sem corrupted id = " << id << std::endl << std::flush;
101  abort();
102  }
103  }
104 
105  void sync_wait(const uint32_t& id) {
106  sem_decrement(id);
107  while (entry(id).sync.load() == true) {}
108  sem_increment(id);
109  }
110 
111  mutex_t mutex_;
112  hip_cb_table_t callbacks_table_;
113 };
114 
115 
116 #if USE_PROF_API
117 #include <prof_protocol.h>
118 
119 static const uint32_t HIP_DOMAIN_ID = ACTIVITY_DOMAIN_HIP_API;
120 typedef activity_record_t hip_api_record_t;
121 typedef activity_rtapi_callback_t hip_api_callback_t;
122 typedef activity_sync_callback_t hip_act_callback_t;
123 
124 // HIP API callbacks spawner object macro
125 #define HIP_CB_SPAWNER_OBJECT(CB_ID) \
126  hip_api_data_t api_data{}; \
127  INIT_CB_ARGS_DATA(CB_ID, api_data); \
128  api_callbacks_spawner_t<HIP_API_ID_##CB_ID> __api_tracer(HIP_API_ID_##CB_ID, api_data);
129 
130 typedef api_callbacks_table_templ<hip_api_record_t,
131  hip_api_callback_t,
132  hip_act_callback_t> api_callbacks_table_t;
133 extern api_callbacks_table_t callbacks_table;
134 
135 template <int cid_>
136 class api_callbacks_spawner_t {
137  public:
138  api_callbacks_spawner_t(const hip_api_id_t& cid, hip_api_data_t& api_data) :
139  api_data_(api_data),
140  record_({})
141  {
142  if (cid_ >= HIP_API_ID_NUMBER) {
143  fprintf(stderr, "HIP %s bad id %d\n", __FUNCTION__, cid_);
144  abort();
145  }
146  callbacks_table.sem_sync(cid_);
147 
148  act = entry(cid_).act;
149  a_arg = entry(cid_).a_arg;
150  fun = entry(cid_).fun;
151  arg = entry(cid_).arg;
152 
153  api_data_.phase = 0;
154  if (act != NULL) act(cid_, &record_, &api_data_, a_arg);
155  if (fun != NULL) fun(HIP_DOMAIN_ID, cid_, &api_data_, arg);
156  }
157 
158  ~api_callbacks_spawner_t() {
159  api_data_.phase = 1;
160  if (act != NULL) act(cid_, &record_, &api_data_, a_arg);
161  if (fun != NULL) fun(HIP_DOMAIN_ID, cid_, &api_data_, arg);
162 
163  callbacks_table.sem_release(cid_);
164  }
165 
166  private:
167  inline api_callbacks_table_t::hip_cb_table_entry_t& entry(const uint32_t& id) {
168  return callbacks_table.entry(id);
169  }
170 
171  hip_api_data_t& api_data_;
172  hip_api_record_t record_;
173 
174  hip_act_callback_t act;
175  void* a_arg;
176  hip_api_callback_t fun;
177  void* arg;
178 };
179 
180 template <>
181 class api_callbacks_spawner_t<HIP_API_ID_NUMBER> {
182  public:
183  api_callbacks_spawner_t(const hip_api_id_t& cid, hip_api_data_t& api_data) {}
184 };
185 
186 #else
187 
188 #define HIP_CB_SPAWNER_OBJECT(x) do {} while(0)
189 
191  public:
192  typedef void* act_t;
193  typedef void* fun_t;
194  bool set_activity(uint32_t id, act_t fun, void* arg) { return false; }
195  bool set_callback(uint32_t id, fun_t fun, void* arg) { return false; }
196 };
197 
198 #endif
199 
200 #endif // _HIP_PROF_API_H
Definition: hip_prof_api.h:21
Definition: hip_prof_api.h:190
Definition: hip_prof_api.h:30
Definition: hip_prof_api.h:12
Definition: hip_prof_str.h:407