Charades
lp.h
Go to the documentation of this file.
1 
5 #ifndef _LP_H
6 #define _LP_H
7 
8 #include "lp.decl.h"
9 
10 #include "event.h"
11 #include "pending_heap.h"
12 #include "processed_queue.h"
13 #include "random.h"
14 #include "typedefs.h"
15 
16 #include <vector>
17 
18 extern CProxy_LPChare lps;
19 
20 class LPBase;
21 class RemoteEvent;
22 class Scheduler;
23 
24 using std::vector;
25 
30 struct LPToken {
31  private:
33  Time ts;
34  uint32_t index;
35 
36  public:
38  LPToken() : lp(NULL), ts(0), index(0) {}
39 
44  LPToken(LPChare* lp) : lp(lp), ts(0), index(0) {}
45 
46  friend class PEQueue;
47  friend class Scheduler;
48  friend class DistributedScheduler;
49  friend class OptimisticScheduler;
50 };
51 
64 class LPChare : public CBase_LPChare {
65  private:
72  Time current_time;
74 
76  uint64_t next_event_id;
77 
83 
90 
95  vector<LPBase*> lp_structs;
96 
103 
109 
117 //@{
127 
129  public:
135 
137  LPChare();
139  LPChare(CkMigrateMessage* m);
140 
141  // TODO: Clean up current/event and time. Probably store a sentinel event
142  // in processed queue that can be used when rolling back to a point where
143  // we have no history. Also have the initial event be not the abort event,
144  // probably by again having a sentinel in the processed queue.
145  uint64_t get_next_event_id() { return next_event_id++; }
146  Event* get_current_event() const { return current_event; }
147  Time get_current_time() const { return current_time; }
148  void set_current_event(Event* e) {
149  current_event = e;
150  if (current_event == NULL) {
151  current_time = PE_VALUE(g_last_gvt);
152  } else {
154  }
155  }
156 
158  void stop_scheduler();
159 
166  void init();
167  void finalize();
172  int execute_me();
177  void fossil_me(Time gvt);
179 
186 
187  void reconstruct_causality(Event*, Event**, Event**);
193  virtual void pup(PUP::er &p);
195  void load_balance();
197  void ResumeFromSync();
199  void UserSetLBLoad();
201 
207 
211  void recv_remote_event(RemoteEvent* event);
216  void recv_anti_event(RemoteEvent* event);
222  void recv_local_event(Event* e);
224 
230 
234  void rollback_me(Time ts);
239  void rollback_me(Event* event);
244  void cancel_event(Event* event);
249  void delete_pending(Event* event);
254  void add_to_cancel_q(Event* e);
256  void process_cancel_q();
258 };
259 
265 class LPBase {
266  public:
270  unsigned gid;
273 
275  virtual void initialize() {}
277  virtual void forward(Event* e) {}
279  virtual void reverse(Event* e) {}
281  virtual void commit(Event* e) {}
283  virtual void finalize() {}
284 
289  virtual bool compare(const Event* e1, const Event* e2) {
290  if (e1->src_lp != e2->src_lp) {
291  return e1->src_lp < e2->src_lp;
292  } else {
293  return e1->event_id < e2->event_id;
294  }
295  }
296 
297  void set_current_event(Event* e) { owner->set_current_event(e); }
298  Event* get_current_event() const { return owner->get_current_event(); }
299  Time get_current_time() const { return owner->get_current_time(); }
300 
301  virtual void pup(PUP::er& p) {
302  p | gid;
303  p | rng;
304  }
305 };
306 
313 template <typename Derived, typename M, typename... Ms>
314 class LP : public LPBase {
315  private:
316  static bool registered;
317  static vector<DispatcherBase<Derived>*> dispatchers;
318 
319  template <typename M1>
320  static void register_msg_type() {
321  dispatchers[get_msg_id<M1>()] = new Dispatcher<Derived, M1>();
322  }
323 
324  template <typename M1, typename M2, typename... M3>
325  static void register_msg_type() {
326  register_msg_type<M1>();
327  register_msg_type<M2, M3...>();
328  }
329 
330  public:
331  LP() {
332  dispatchers.resize(g_num_msg_types);
333  if (!registered) {
334  register_msg_type<M, Ms...>();
335  registered = true;
336  }
337  }
338 
343  void forward(Event* e) {
344  dispatchers[e->type_id]->forward(static_cast<Derived*>(this), e);
345  }
350  void reverse(Event* e) {
351  dispatchers[e->type_id]->reverse(static_cast<Derived*>(this), e);
352  }
357  void commit(Event* e) {
358  dispatchers[e->type_id]->commit(static_cast<Derived*>(this), e);
359  }
360 };
361 
362 template <typename Derived, typename M, typename... Ms>
363 vector<DispatcherBase<Derived>*> LP<Derived, M, Ms...>::dispatchers;
364 
365 template <typename Derived, typename M, typename... Ms>
366 bool LP<Derived, M, Ms...>::registered = false;
367 
373 void init_lps();
375 
376 #endif
void delete_pending(Event *event)
Cancel an event from the pending queue by simply removing it.
Definition: lp.C:481
void reconstruct_pending_event(Event *)
Correclty rebuild pending events when unpacking.
Definition: lp_pup.C:77
void recv_local_event(Event *e)
Regular method for common processing on locally allocated events.
Definition: lp.C:289
virtual void forward(Event *e)
Overridden by LP.
Definition: lp.h:277
void reverse(Event *e)
Called by the Scheduler to reverse execute e during a rollback.
Definition: lp.h:350
vector< LPBase * > lp_structs
A list of all LPs owned by this chare.
Definition: lp.h:95
void recv_remote_event(RemoteEvent *event)
Entry method for receiving a remote event from another chare.
Definition: lp.C:255
int rolled_back_events
Number of events rolled back since last LB.
Definition: lp.h:115
Base class defining basic scheduler functionality.
Definition: scheduler.h:36
virtual void initialize()
Called to initialize LP state and seed initial events.
Definition: lp.h:275
void load_balance()
Tell this chare that we are going to do load balancing.
Definition: lp.C:104
Definition: event.h:120
virtual void finalize()
Called to perform any final actions before simulation exit.
Definition: lp.h:283
Definition: pe_queue.h:15
A chare that encapsulates a set of LPStructs and their events.
Definition: lp.h:64
void stop_scheduler()
Stops the Charm++ scheduler and returns control to main.
Definition: lp.C:243
unsigned gid
The global id of this LP.
Definition: lp.h:270
Definition: pending_heap.h:34
int committed_events
Number of events committed since last LB.
Definition: lp.h:114
void forward(Event *e)
Called by the Scheduler to forward execute e.
Definition: lp.h:343
Scheduler * scheduler
A direct pointer to the scheduler managing this LP chare.
Definition: lp.h:102
Base class which defines the interface for defining model LPs.
Definition: lp.h:314
void cancel_event(Event *event)
Cancel an event that should have never been received.
Definition: lp.C:449
LPToken next_token
LPToken whose key is the timestamp of the next event this chare will execute.
Definition: lp.h:71
LPChare * lp
Direct pointer to the LP chare this token represents.
Definition: lp.h:32
LPChare()
Default constructor.
Definition: lp.C:62
A token representing a handle to an LP chare inside the scheduler queues.
Definition: lp.h:30
void init_lps()
Calls init on the array of LP chares (only if we are rank 0), and then starts the charm scheduler on ...
Definition: lp.C:48
uint64_t next_event_id
The next available ID for uniquely identifying events from this LP.
Definition: lp.h:76
Time current_time
Time of most recently executed event.
Definition: lp.h:72
Concrete base class for distributed schedulers.
Definition: scheduler.h:109
void UserSetLBLoad()
Called by the runtime system to get the load of this chare.
Definition: lp.C:161
void reconstruct_causality(Event *, Event **, Event **)
Correctly rebuild causality chains when unpacking.
Definition: lp_pup.C:111
Time ts
A timestamp associated with the LP to be used as a key.
Definition: lp.h:33
int execute_me()
Called during execution to execute the next event owned by this LP.
Definition: lp.C:343
void rollback_me(Time ts)
Do a primary rollback to a given virtual timestamp.
Definition: lp.C:374
virtual bool compare(const Event *e1, const Event *e2)
Called by the scheduler to break ties on events with the same timestamp.
Definition: lp.h:289
AvlTree all_events
A pointer to the PE level AVL tree for hashing remote events.
Definition: lp.h:134
virtual void pup(PUP::er &p)
Pack/unpack this LP chare.
Definition: lp_pup.C:17
Definition: avl_tree.h:7
LPToken()
Default constructor.
Definition: lp.h:38
RNG rng
The random number generator for this LP.
Definition: lp.h:272
Time min_cancel_q
Minimum time in this LPs cancel queue.
Definition: lp.h:126
Event * current_event
Most recently executed event.
Definition: lp.h:73
void add_to_cancel_q(Event *e)
Add an event to the queue of events to be cancelled later.
Definition: lp.C:469
uint32_t index
The index of this token within the queue/heap.
Definition: lp.h:34
Definition: optimistic.h:10
void ResumeFromSync()
Called by the runtime system to tell this chare we can resume.
Definition: lp.C:109
bool isOptimistic
Set to true if this LP is an optimistic LP.
Definition: lp.h:108
PendingHeap events
All future events received for any of our LPs.
Definition: lp.h:82
Implementation of a CLCG4 random number generator, based on the reversible implementation from ROSS (...
Definition: clcg4.h:14
void commit(Event *e)
Called by the Scheduler when e is committed.
Definition: lp.h:357
uint32_t g_num_msg_types
number of message types
Definition: globals.C:25
#define PE_VALUE(x)
Macro to simplify global variable access.
Definition: globals.h:111
Definition: processed_queue.h:8
LPToken(LPChare *lp)
Constructor called from the LP chare that sets lp at initialization.
Definition: lp.h:44
CProxy_LPChare lps
A readonly declaration for a global lp proxy.
Definition: lp.C:25
void fossil_me(Time gvt)
Called after GVT computation to commit old events.
Definition: lp.C:434
void init()
Called at the start of a simulation to run LP init handlers.
Definition: lp.C:222
virtual void reverse(Event *e)
Overridden by LP.
Definition: lp.h:279
ProcessedQueue processed_events
A flat queue of all events previously processed by our LPs that haven&#39;t yet been committed or rolled ...
Definition: lp.h:89
Declares most types used within the simulator and by models.
Event * cancel_q
Queue of events this LP needs to cancel.
Definition: lp.h:125
Definition: event.h:282
Definition: event.h:15
void recv_anti_event(RemoteEvent *event)
Entry method for receiving and processing an anti event.
Definition: lp.C:314
Non-templated base class for LP, so that the simulator can deal with a single LP base type...
Definition: lp.h:265
virtual void commit(Event *e)
Overridden by LP.
Definition: lp.h:281
LPChare * owner
The LPChare this LP object lives on.
Definition: lp.h:268
void reconstruct_processed_event(Event *, Event **, Event **)
Correctly rebuild processed events when unpacking.
Definition: lp_pup.C:94
int committed_time
Timestamp of most recent committed event.
Definition: lp.h:116
void process_cancel_q()
Process and cancel each event in the cancel queue.
Definition: lp.C:487