Charades
event.h
Go to the documentation of this file.
1 
5 #ifndef EVENT_H_
6 #define EVENT_H_
7 
8 #include "event.decl.h"
9 
10 #include "globals.h" // Included for g_num_msg_types (TODO should be to move this)
11 #include "typedefs.h"
12 
13 class LPBase;
14 
15 struct RemoteEvent : public CMessage_RemoteEvent {
16  public:
17  RemoteEvent() {
18  clear();
19  }
20 
21  void clear() {
22  ts = 0;
23  event_id = 0;
24  src_lp = 0;
25  dest_lp = 0;
26  }
27 
28  Time ts;
29  uint64_t event_id;
30  uint64_t src_lp;
31  uint64_t dest_lp;
32 
33  // Async GVT variables
34  uint8_t phase;
35  bool anti;
36  uint8_t offset;
37 
38  uint8_t type_id;
39  uint8_t type_size;
40  char* data;
42  virtual void pup(PUP::er& p) {
43  p | ts;
44  p | event_id;
45  p | src_lp;
46  p | dest_lp;
47  p | phase;
48  p | anti;
49  p | offset;
50  p | type_id;
51  p | type_size;
52  p(data, type_size);
53  }
54 };
55 
58  TW_chare_q = 1,
60  TW_sent = 3,
61 };
62 
64  unsigned char owner;
65  unsigned char cancel_q;
66  unsigned char remote;
67  unsigned char avl_tree;
68 };
69 PUPbytes(tw_event_state);
70 
78 struct tw_bf {
79  unsigned int c0:1;
80  unsigned int c1:1;
81  unsigned int c2:1;
82  unsigned int c3:1;
83  unsigned int c4:1;
84  unsigned int c5:1;
85  unsigned int c6:1;
86  unsigned int c7:1;
87  unsigned int c8:1;
88  unsigned int c9:1;
89  unsigned int c10:1;
90  unsigned int c11:1;
91  unsigned int c12:1;
92  unsigned int c13:1;
93  unsigned int c14:1;
94  unsigned int c15:1;
95  unsigned int c16:1;
96  unsigned int c17:1;
97  unsigned int c18:1;
98  unsigned int c19:1;
99  unsigned int c20:1;
100  unsigned int c21:1;
101  unsigned int c22:1;
102  unsigned int c23:1;
103  unsigned int c24:1;
104  unsigned int c25:1;
105  unsigned int c26:1;
106  unsigned int c27:1;
107  unsigned int c28:1;
108  unsigned int c29:1;
109  unsigned int c30:1;
110  unsigned int c31:1;
111 
112  void clear() {
113  memset(this, 0, sizeof(tw_bf));
114  }
115 };
116 PUPbytes(tw_bf);
117 
118 void pup_causality(PUP::er& p, Event* e);
119 
120 class Event {
121 public:
122  // Basic event info, used as a key for unique event identification
123  Time ts;
124  uint64_t event_id;
125  uint64_t src_lp;
126  uint64_t dest_lp;
127 
128  LPBase* owner;
129  RemoteEvent* msg;
130 
131  // Variables for message data types
132  uint8_t type_id;
133  uint8_t type_size;
134 
135  // Fields used to enable time warp mechanism to do rollbacks
136  tw_bf cv; // Bitfield keeps track of execution path
137  tw_event_state state; // State keeps track of who owns the event
138 
139  // Pointers used in data structures storing Events
140  Event* prev; // Prev in processed queue
141  Event* next; // Next in processed queue
142  Event* caused_by_me; // Start of event list caused by this event
143  Event* cause_next; // Next in parent's caused_by_me chain
144  Event* cancel_next; // next in cancel list
145 
146  // Index of the event in the pending heap and the order of event pupping
147  uint8_t index;
148 
149  // Fields for rebuilding causality lists after migration
150  unsigned pending_count;
151  unsigned processed_count;
152  unsigned sent_count;
153  unsigned* pending_indices;
154  unsigned* processed_indices;
155 
156  Event() {
157  clear();
158  }
159  void clear() {
160  ts = 0;
161  event_id = dest_lp = src_lp = 0;
162 
163  owner = NULL;
164  msg = NULL;
165 
166  cv.clear();
167  state.owner = state.remote = state.cancel_q = state.avl_tree = 0;
168 
169  next = prev = caused_by_me = cause_next = cancel_next = NULL;
170 
171  type_id = type_size = index = 0;
172 
173  pending_count = processed_count = sent_count = 0;
174  pending_indices = processed_indices = NULL;
175  }
176  void pup(PUP::er& p) {
177  p | state;
178 
179  if (state.owner == TW_chare_q) {
180  p | index;
181  p | type_size;
182  if (p.isUnpacking()) {
183  msg = new (type_size) RemoteEvent();
184  }
185  msg->pup(p);
186  set_msg(msg);
187  } else if (state.owner == TW_rollback_q) {
188  p | cv;
189  p | index;
190  p | type_size;
191  if (p.isUnpacking()) {
192  msg = new (type_size) RemoteEvent();
193  }
194  msg->pup(p);
195  set_msg(msg);
196  pup_causality(p,this);
197  } else if (state.owner == TW_sent) {
198  p | ts;
199  p | event_id;
200  p | src_lp;
201  p | dest_lp;
202  } else {
203  CkAbort("Bad event state during pupping\n");
204  }
205  }
206 
207  void set_msg(RemoteEvent* m) {
208  msg = m;
209  ts = msg->ts;
210  event_id = msg->event_id;
211  src_lp = msg->src_lp;
212  dest_lp = msg->dest_lp;
213  type_id = msg->type_id;
214  type_size = msg->type_size;
215  }
216  RemoteEvent* get_msg() const {
217  return msg;
218  }
219 
221  template<typename DataType>
222  DataType* get_data() const {
223  return reinterpret_cast<DataType*>(msg->data);
224  }
225 };
226 
228  public:
229  bool operator()(const Event* e1, const Event* e2);
230 };
231 
233  public:
234  bool operator()(const Event* e1, const Event* e2);
235 };
236 
237 // This function is also used during unpacking after migration, which is why
238 // it is included in the header instead of the cpp file.
239 static inline void link_causality(Event* nev, Event* cev) {
240  nev->cause_next = cev->caused_by_me;
241  cev->caused_by_me = nev;
242 }
243 
244 // Public API for models to use for managing events
249 Event* event_alloc(RemoteEvent* event, uint64_t dest_gid, Time offset, LPBase * sender);
250 Event* event_alloc();
252 void tw_event_send(Event* event);
253 void tw_event_free(Event* e, bool commit);
254 void tw_event_rollback(Event* event);
255 
256 // TODO: After unifying events this won't be needed
257 // API for Charm++ specific event usage, to be used by original ROSS code
258 void charm_event_cancel(Event* e);
259 void charm_anti_send(unsigned, Event* e);
260 void charm_event_release(RemoteEvent* e);
261 
262 template <typename MsgType>
263 uint32_t get_msg_id() {
264  static uint32_t msg_id = g_num_msg_types++;
265  return msg_id;
266 }
267 
268 template <typename MsgType>
269 void register_msg_type() {
270  get_msg_id<MsgType>();
271 }
272 
273 template <typename LPType>
275 public:
276  virtual void forward(LPType* lp, Event* e) = 0;
277  virtual void reverse(LPType* lp, Event* e) = 0;
278  virtual void commit(LPType* lp, Event* e) = 0;
279 };
280 
281 template <typename LPType, typename MsgType>
282 class Dispatcher : public DispatcherBase<LPType> {
283 public:
284  void forward(LPType* lp, Event* e) {
285  lp->forward(e->get_data<MsgType>(), &e->cv);
286  }
287  void reverse(LPType* lp, Event* e) {
288  lp->reverse(e->get_data<MsgType>(), &e->cv);
289  }
290  void commit(LPType* lp, Event* e) {
291  lp->commit(e->get_data<MsgType>(), &e->cv);
292  }
293 };
294 
303 template <typename MsgType, typename... Args>
304 Event* tw_event_new(uint64_t dest_gid, Time offset, LPBase* sender, Args&&... args) {
305  RemoteEvent* msg = new (sizeof(MsgType)) RemoteEvent();
306  msg->type_id = get_msg_id<MsgType>();
307  msg->type_size = sizeof(MsgType);
308  new (msg->data) MsgType(std::forward<Args>(args)...);
309  return event_alloc(msg, dest_gid, offset, sender);
310 }
311 
319 Event* tw_event_new(uint64_t dest_gid, Time offset, LPBase* sender, size_t size);
320 
321 #endif
unsigned char owner
Which queue I am in; see tw_event_owner.
Definition: event.h:64
Definition: event.h:120
char * data
Points to memory for user event data.
Definition: event.h:40
unsigned char cancel_q
Actively on a dest_lp->pe&#39;s cancel_q.
Definition: event.h:65
Event * event_alloc(RemoteEvent *event, uint64_t dest_gid, Time offset, LPBase *sender)
Send a previously allocated event.
Definition: event.C:47
Declarations for global variables and associated functions.
Event sent to someone else.
Definition: event.h:60
DataType * get_data() const
Public accessor for the event payload, cast as the specified type.
Definition: event.h:222
Definition: event.h:274
void tw_event_send(Event *event)
Sends an event to its destination LP.
Definition: event.C:95
Event in unknown location.
Definition: event.h:57
In the chare&#39;s rollback queue.
Definition: event.h:59
Definition: event.h:232
tw_bf
Definition: event.h:78
tw_event_owner
Definition: event.h:56
Event * tw_event_new(uint64_t dest_gid, Time offset, LPBase *sender, Args &&... args)
Creates an Event with the appropriate payload type for the desired destination and offset in virtual ...
Definition: event.h:304
unsigned char avl_tree
Indicates that the event is in the AVL tree.
Definition: event.h:67
uint8_t type_id
Used for double-dispatch of event data.
Definition: event.h:38
uint32_t g_num_msg_types
number of message types
Definition: globals.C:25
Definition: event.h:63
Declares most types used within the simulator and by models.
Definition: event.h:282
Definition: event.h:15
unsigned char remote
Indicates union addr is in &#39;remote&#39; storage.
Definition: event.h:66
Non-templated base class for LP, so that the simulator can deal with a single LP base type...
Definition: lp.h:265
In the chare&#39;s pending queue.
Definition: event.h:58
Definition: event.h:227