RepastHPC  2.3.1
Schedule.h
1 /*
2  * Repast for High Performance Computing (Repast HPC)
3  *
4  * Copyright (c) 2010 Argonne National Laboratory
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with
8  * or without modification, are permitted provided that the following
9  * conditions are met:
10  *
11  * Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  *
14  * Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  *
18  * Neither the name of the Argonne National Laboratory nor the names of its
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TRUSTEES OR
26  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
32  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  *
35  * Schedule.h
36  *
37  * Created on: Jan 5, 2009
38  * Author: nick
39  */
40 
41 #ifndef SCHEDULE_H_
42 #define SCHEDULE_H_
43 
44 #include <vector>
45 #include <queue>
46 #include <boost/mpi/communicator.hpp>
47 #include <boost/shared_ptr.hpp>
48 #include <boost/noncopyable.hpp>
49 
50 namespace repast {
51 
55 class Functor {
56 public:
57  virtual ~Functor() = 0;
58  virtual void operator()() = 0;
59 };
60 
68 template<typename T>
69 class MethodFunctor: public Functor {
70 private:
71  void (T::*fptr)();
72  T *obj;
73 public:
74  MethodFunctor(T *_obj, void(T::*_fptr)()) :
75  fptr(_fptr), obj(_obj) {
76  }
77  ;
78  ~MethodFunctor() {
79  }
80  ;
81  void operator()() {
82  (obj->*fptr)();
83  }
84 };
85 
89 class RepastEvent {
90 
91 public:
92  double tick;
93  boost::shared_ptr<Functor> func_ptr;
94 
95  virtual ~RepastEvent();
96 
97 };
98 
99 class EventCompare;
100 
105 
106 protected:
107  RepastEvent *event;
108  double start;
109 
110 public:
111  friend class EventCompare;
112  ScheduledEvent(double, RepastEvent *);
113  virtual ~ScheduledEvent();
117  virtual bool reschedule(std::priority_queue<ScheduledEvent *, std::vector<ScheduledEvent*>, EventCompare>&) = 0;
118 
123  return event;
124  }
125  ;
126 };
127 
132 
133 public:
134  OneTimeEvent(double, RepastEvent*);
135  ~OneTimeEvent();
139  virtual bool reschedule(std::priority_queue<ScheduledEvent *, std::vector<ScheduledEvent*>, EventCompare>&);
140 };
141 
147 
148 private:
149  double interval;
150 
151 public:
152  RepeatingEvent(double start, double _interval, RepastEvent*);
153  ~RepeatingEvent();
154  virtual bool reschedule(std::priority_queue<ScheduledEvent *, std::vector<ScheduledEvent*>, EventCompare>&);
155 };
156 
161 public:
162  int operator()(const ScheduledEvent* one, const ScheduledEvent* two) {
163  double tick1 = one->event->tick;
164  double tick2 = two->event->tick;
165  return tick1 > tick2 ? 1 : 0;
166  }
167 };
168 
173 class Schedule {
174 private:
175  typedef std::priority_queue<ScheduledEvent *, std::vector<ScheduledEvent*>, EventCompare> QueueType;
176  QueueType queue;
177  double currentTick;
178 
179 public:
183  typedef boost::shared_ptr<Functor> FunctorPtr;
184  virtual ~Schedule();
185  Schedule();
186 
195  ScheduledEvent* schedule_event(double at, FunctorPtr functor);
196 
207  ScheduledEvent* schedule_event(double start, double interval, FunctorPtr func);
208  void execute();
209 
215  double getCurrentTick() const {
216  return currentTick;
217  }
218  ;
219 
225  double getNextTick() const {
226  if (queue.empty())
227  return -1;
228  return queue.top()->get_event()->tick;
229  }
230  ;
231 };
232 
239 class ScheduleRunner: public boost::noncopyable {
240 
241 private:
242 
243  bool go;
244  Schedule schedule_;
245  double globalNextTick, localNextTick;
246  void nextTick();
247  boost::mpi::communicator* comm;
248  std::vector<boost::shared_ptr<Functor> > endEvents;
249 
250 public:
251  ScheduleRunner(boost::mpi::communicator* communicator);
252  ~ScheduleRunner();
253 
263 
274  ScheduledEvent* scheduleEvent(double start, double interval, Schedule::FunctorPtr func);
275 
282 
288  void scheduleStop(double at);
289 
293  void run();
294 
300  double currentTick() {
301  return schedule_.getCurrentTick();
302  }
303 
307  void stop();
308 
314  const Schedule& schedule() {
315  return schedule_;
316  }
317 };
318 
319 }
320 
321 #endif /* SCHEDULE_H_ */
repast::OneTimeEvent::reschedule
virtual bool reschedule(std::priority_queue< ScheduledEvent *, std::vector< ScheduledEvent * >, EventCompare > &)
Always returns false, as it does not reschedule itself.
Definition: Schedule.cpp:74
repast::RepeatingEvent
ScheduledEvent that executes repeatedly.
Definition: Schedule.h:146
repast::OneTimeEvent
ScheduledEvent that will only execute only once.
Definition: Schedule.h:131
repast::Functor
Functor interface.
Definition: Schedule.h:55
repast::ScheduledEvent::reschedule
virtual bool reschedule(std::priority_queue< ScheduledEvent *, std::vector< ScheduledEvent * >, EventCompare > &)=0
Returns true if this event is rescheduled on the specified queue, otherwise false.
repast::Schedule
The simulation schedule queue.
Definition: Schedule.h:173
repast::RepastEvent
General class linking a function pointer to a specific tick.
Definition: Schedule.h:89
repast::ScheduleRunner::schedule
const Schedule & schedule()
Gets the schedule executed by this simulation runner.
Definition: Schedule.h:314
repast::ScheduleRunner::scheduleEvent
ScheduledEvent * scheduleEvent(double at, Schedule::FunctorPtr func)
Schedules the Functor to execute at the specified tick.
Definition: Schedule.cpp:162
repast::ScheduleRunner::currentTick
double currentTick()
Gets the current tick.
Definition: Schedule.h:300
repast::ScheduleRunner::stop
void stop()
Stops the simulation.
Definition: Schedule.cpp:174
repast::Schedule::getNextTick
double getNextTick() const
Gets the next tick at which the next events will be executed.
Definition: Schedule.h:225
repast::EventCompare
Compares ScheduledEvents based on their tick times.
Definition: Schedule.h:160
repast::Schedule::FunctorPtr
boost::shared_ptr< Functor > FunctorPtr
Typedef of for the functors that get scheduled.
Definition: Schedule.h:183
repast::ScheduleRunner::run
void run()
Starts and runs the simulation schedule.
Definition: Schedule.cpp:182
repast::Schedule::schedule_event
ScheduledEvent * schedule_event(double at, FunctorPtr functor)
Schedule the specified functor to execute once at the specified tick.
Definition: Schedule.cpp:105
repast::ScheduledEvent
The object that is placed (scheduled) in the priority queue for execution.
Definition: Schedule.h:104
repast::ScheduledEvent::get_event
RepastEvent * get_event()
Gets the RepastEvent that this ScheduleEvent wraps.
Definition: Schedule.h:122
repast::MethodFunctor
Adapts a no-arg method call on an object instance to a Functor interface.
Definition: Schedule.h:69
repast::Schedule::getCurrentTick
double getCurrentTick() const
Gets the current simulation tick.
Definition: Schedule.h:215
repast::ScheduleRunner::scheduleStop
void scheduleStop(double at)
Schedules the simulation to stop at the specified tick.
Definition: Schedule.cpp:156
repast::ScheduleRunner
Runs the Schedule by popping events off of the Schedule and executing them; also provides methods for...
Definition: Schedule.h:239
repast::ScheduleRunner::scheduleEndEvent
void scheduleEndEvent(Schedule::FunctorPtr func)
Schedules the specified functor to execute when the simulation ends.
Definition: Schedule.cpp:178
repast::RepeatingEvent::reschedule
virtual bool reschedule(std::priority_queue< ScheduledEvent *, std::vector< ScheduledEvent * >, EventCompare > &)
Returns true if this event is rescheduled on the specified queue, otherwise false.
Definition: Schedule.cpp:85