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