orca-sim
Simulator.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 <signal.h>
27 
28 #include <chrono>
29 
30 #include "Simulator.hpp"
31 #include "Event.hpp"
32 
33 #define ORCA_EPOCH_LENGTH 100000
34 #define ORCA_EPOCHS_TO_SIM 10000
35 
36 
40 
41 static void orcasim::modeling::sig_handler(int _) {
42  (void)_;
43 
44  switch (Simulator::GetInterruptionStatus()) {
45  case SimulatorInterruptionStatus::RUNNING:
46  Simulator::SetInterruptionStatus(
47  SimulatorInterruptionStatus::INTERRUPTED);
48  std::cout << std::endl
49  << "Simulation interrupted. Wait for the current epoch to finish "
50  << "or press CTRL+C again to force quit." << std::endl;
51  break;
52  case SimulatorInterruptionStatus::INTERRUPTED:
53  Simulator::SetInterruptionStatus(SimulatorInterruptionStatus::ABORTED);
54  exit(0);
55  break;
56  }
57 }
58 
59 volatile SimulatorInterruptionStatus Simulator::_interruption_status
60  = SimulatorInterruptionStatus::RUNNING;
61 
62 void Simulator::SetInterruptionStatus(SimulatorInterruptionStatus status) {
64 }
65 
68 }
69 
70 std::string Simulator::GetParam(int index) {
71  return std::string(_params[index]);
72 }
73 
74 Simulator::Simulator(int argc, char** argv) {
75 
76  std::cout << "param list:" << std::endl;
77  for(int i = 0; i < argc; ++i){
78  std::cout << i << "\t" << argv[i] << std::endl;
79  }
80 
81  _exit_status = 0; // if not overwritten, exist status is zero (EXIT_OK)
83  signal(SIGINT, sig_handler); // register interruption handler
84 
85  // parse params
86  if (argc > 0) {
87  _params = std::vector<std::string>();
88  for (int i = 0; i < argc; i++) {
89  char* param = argv[i];
90  _params.push_back(std::string(param));
91  }
92  }
93 }
94 
96  _engine.Schedule(Event(time, model));
97 }
98 
100  Simulator::Register(model, 1);
101 }
102 
104 
105  Startup();
106  Schedule();
107 
109  t1 = std::chrono::high_resolution_clock::now();
110 
112 
113  t2 = std::chrono::high_resolution_clock::now();
114 
115  auto duration =
116  std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1)
117  .count();
118  _engine.NextEpoch();
119 
120  // converts mili to seconds before calculating the frequency
121  double hertz = static_cast<double>(ORCA_EPOCH_LENGTH) /
122  (static_cast<double>(duration) / 1000.0);
123 
124  // divide frequency by 1k (Hz -> KHz)
125  std::cout << "notice: epoch #" << _engine.GetEpochs() << " took ~"
126  << duration << "ms (running @ " << (hertz / 1000000.0)
127  << " MHz)" << std::endl;
128 
129  #ifdef ORCA_EPOCHS_TO_SIM
130  // simulate until reach the limit of pulses
132  break;
133  #endif
134  }
135 
136  Report();
137 }
138 
140  return _exit_status;
141 }
142 
143 void Simulator::SetExitStatus(int status) {
144  _exit_status = status;
145 }
This class models a TimedModel.
Definition: TimedModel.hpp:42
SimulationTime Run(SimulationTime time=100000)
Executes the simulator until the internal clock reaches <time> cycles.
Definition: Engine.cpp:46
#define ORCA_EPOCHS_TO_SIM
Definition: Simulator.cpp:34
#define ORCA_EPOCH_LENGTH
Definition: Simulator.cpp:33
static SimulatorInterruptionStatus GetInterruptionStatus()
Definition: Simulator.cpp:66
This class models a discrete event.
Definition: Event.hpp:39
static void sig_handler(int _)
std::chrono::high_resolution_clock::time_point t2
Definition: Simulator.hpp:57
SimulationTime NextEpoch()
Resets the simulation clock and advance simulation to the next epoch.
Definition: Engine.cpp:81
uint32_t SimulationTime
std::chrono::high_resolution_clock::time_point t1
Definition: Simulator.hpp:57
void Schedule(const Event &e)
Adds an event to the simulation queue.
Definition: Engine.cpp:122
SimulationTime GetEpochs()
Gets the number of cycles to simulate before reseting the simulation clock.
Definition: Engine.cpp:115
void SetExitStatus(int status)
Definition: Simulator.cpp:143
std::string GetParam(int index)
Definition: Simulator.cpp:70
std::vector< std::string > _params
Definition: Simulator.hpp:58
static volatile SimulatorInterruptionStatus _interruption_status
Definition: Simulator.hpp:64
void Register(TimedModel *m)
Definition: Simulator.cpp:99