orca-sim
Engine.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * This file is part of project ORCA. More information on the project
3  * can be found at the following repositories at GitHub's website.
4  *
5  * http://https://github.com/andersondomingues/orca-sim
6  * http://https://github.com/andersondomingues/orca-software
7  * http://https://github.com/andersondomingues/orca-mpsoc
8  * http://https://github.com/andersondomingues/orca-tools
9  *
10  * Copyright (C) 2018-2020 Anderson Domingues, <ti.andersondomingues@gmail.com>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along
23  * with this program; if not, write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 ******************************************************************************/
26 #include "Engine.hpp"
27 
30 
34 Engine::Engine() {
35  _queue = std::priority_queue<Event>();
36  _globalTime = 0;
37  _epochs = 0;
38  _timeout = 1;
39 }
40 
47  _globalTime = 0;
48  _timeout = time;
49 
50  while (_globalTime <= _timeout) {
51  #ifdef ORCA_BASE_CHECK_FOR_EMPTY_QUEUE
52  if (_queue.size() <= 0) {
53  throw std::runtime_error("Simulation queue is empty");
54  }
55  #endif
56 
57  // get next event to be processed. Since we use a priority
58  // queue to store events, the event nealy in time will be
59  // popped first
60  Event e = _queue.top();
61  _queue.pop();
62 
63  // update global time
64  _globalTime = e.time;
65 
66  // schedule current event to be executed after a certain number
67  // cycles, defined in the correspondent Run method
68  e.time += e.timedModel->Run();
69 
70  // push event back to the queue
71  _queue.push(e);
72  }
73 
74  // return point in time in which the last event was executed
75  return _globalTime;
76 }
77 
82  // get the number of elements scheduled
83  int queue_size = static_cast<int>(_queue.size());
84 
85  // time that amount of time
86  SimulationTime discount = _globalTime - 1;
87 
88  // create a new queue and reschedule events
89  Event tmp_queue[queue_size];
90 
91  // store events in an array so that we can update
92  // them without messing up with the priority queue
93  for (int i = 0; i < queue_size; i++) {
94  tmp_queue[i] = _queue.top();
95  tmp_queue[i].time -= discount;
96 
97  _queue.pop();
98  }
99 
100  // put update events back in simulator's queue
101  for (int i = 0; i < queue_size; i++)
102  _queue.push(tmp_queue[i]);
103 
104  // update epochs counters
105  _epochs++;
106 
107  return _globalTime;
108 }
109 
111  return _globalTime;
112 }
113 
114 
116  return _epochs;
117 }
118 
122 void Engine::Schedule(const Event& e) {
123  #ifdef URSA_ZERO_TIME_CHECKING
124  if (e.time == 0) {
125  throw std::runtime_error("Simulator: unable to schedule "
126  + e.timedModel->GetName() + " to run in the past. " +
127  "Events must be scheduled to run with time > 0.");
128  }
129  #endif
130  _queue.push(e);
131 }
132 
136  // nothing to do
137 }
SimulationTime Run(SimulationTime time=100000)
Executes the simulator until the internal clock reaches <time> cycles.
Definition: Engine.cpp:46
SimulationTime GetGlobalTime()
Gets the current global time.
Definition: Engine.cpp:110
This class models a discrete event.
Definition: Event.hpp:39
std::priority_queue< Event > _queue
queue that stores all events
Definition: Engine.hpp:52
SimulationTime _timeout
max time the simulation can reach
Definition: Engine.hpp:58
SimulationTime NextEpoch()
Resets the simulation clock and advance simulation to the next epoch.
Definition: Engine.cpp:81
SimulationTime _globalTime
The global clock, stores current simulation time.
Definition: Engine.hpp:55
uint32_t SimulationTime
~Engine()
Destructor.
Definition: Engine.cpp:135
std::string GetName()
Getter method for the <_name> field.
Definition: Model.cpp:34
SimulationTime _epochs
number of cycles to simulate before reseting the queue
Definition: Engine.hpp:49
This class implements an event queue to schedule and execute hardware modules.
Definition: Engine.hpp:46
virtual SimulationTime Run()=0
Method which is called by the simulator when during the execution of the TimedModel.
void Schedule(const Event &e)
Adds an event to the simulation queue.
Definition: Engine.cpp:122
SimulationTime time
Point in time when the event will trigger.
Definition: Event.hpp:44
SimulationTime GetEpochs()
Gets the number of cycles to simulate before reseting the simulation clock.
Definition: Engine.cpp:115
TimedModel * timedModel
Model whose activation function will be called once the event triggers.
Definition: Event.hpp:50