cdboost-devstone.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /**
  2. * Copyright (c) 2013-2014, Carleton University, Universite de Nice-Sophia Antipolis
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * 1. Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright notice,
  10. * this list of conditions and the following disclaimer in the documentation
  11. * and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  17. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. * POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include <iostream>
  26. #include <chrono>
  27. #include <algorithm>
  28. #include <fstream>
  29. #include <boost/program_options.hpp>
  30. #include <boost/simulation.hpp>
  31. #include "cdboost-devstone-atomic.hpp"
  32. #include "cdboost-devstone-atomic-pair.hpp"
  33. #include "cdboost-devstone-filter.hpp"
  34. #include "cdboost-devstone-input-stream-pair.hpp"
  35. using namespace std;
  36. using namespace cdpp;
  37. namespace po=boost::program_options;
  38. using hclock=chrono::high_resolution_clock;
  39. using Time=double;
  40. //const float infinity = std::numeric_limits<double>::infinity();
  41. inline bool is_infinity(double& f ){ return isinf(f); }
  42. using msg_type_int=int;
  43. using msg_type_pair=std::pair<int, int>;
  44. shared_ptr<boost::simulation::pdevs::coupled<Time, msg_type_int>> LI_coupling(int& counted_atomic_models, int& counted_coupled_models,
  45. int width, int depth, string event_list, int ext_cycles, int int_cycles, int time_advance)
  46. {
  47. shared_ptr<boost::simulation::pdevs::coupled<Time, msg_type_int>> cm;
  48. auto first_pdevstone = boost::simulation::make_atomic_ptr<PDEVStoneAtomic<Time, msg_type_int>, int, int, Time>(int_cycles, ext_cycles, Time(time_advance));
  49. counted_atomic_models++;
  50. cm.reset(new boost::simulation::pdevs::coupled<Time, msg_type_int>{{first_pdevstone}, {first_pdevstone}, {}, {first_pdevstone}});
  51. counted_coupled_models++;
  52. //connect higher level models
  53. for (int i=1; i < depth; i++){
  54. vector<std::shared_ptr<boost::simulation::model<Time>>> vpdt;
  55. vector<std::shared_ptr<boost::simulation::model<Time>>> eoc_cm;
  56. vector<std::shared_ptr<boost::simulation::model<Time>>> eic_cm;
  57. vpdt.clear();
  58. eoc_cm.clear();
  59. eic_cm.clear();
  60. eoc_cm.push_back(cm);
  61. for (int j=0; j < width-1; j++){
  62. auto current = (std::shared_ptr<boost::simulation::model<Time>>) std::make_shared<cdpp::PDEVStoneAtomic<Time, msg_type_int>>(int_cycles, ext_cycles, Time(time_advance));
  63. vpdt.push_back(current);
  64. counted_atomic_models++;
  65. eic_cm.push_back(current);
  66. }
  67. vpdt.push_back(cm);
  68. eic_cm.push_back(cm);
  69. shared_ptr<boost::simulation::pdevs::coupled<Time, msg_type_int>> cm_int ( new boost::simulation::pdevs::coupled<Time, msg_type_int>{vpdt, eic_cm, {}, eoc_cm});
  70. counted_coupled_models++;
  71. cm=cm_int;
  72. }
  73. //Plug the input events
  74. shared_ptr<istream> piss{ new ifstream{event_list} };
  75. auto pf = boost::simulation::make_atomic_ptr<boost::simulation::pdevs::basic_models::input_stream<Time, msg_type_int, int, int>, shared_ptr<istream>, Time>(piss, Time{0});
  76. counted_atomic_models++;
  77. auto root = std::make_shared<boost::simulation::pdevs::coupled<Time, msg_type_int>>(boost::simulation::pdevs::coupled<Time, msg_type_int>({pf, cm}, {}, {{pf, cm}}, {cm}));
  78. counted_coupled_models++;
  79. return root;
  80. }
  81. shared_ptr<boost::simulation::pdevs::coupled<Time, msg_type_int>> HI_coupling(int& counted_atomic_models, int& counted_coupled_models,
  82. int width, int depth, string event_list, int ext_cycles, int int_cycles, int time_advance)
  83. {
  84. shared_ptr<boost::simulation::pdevs::coupled<Time, msg_type_int>> cm;
  85. auto first_pdevstone = boost::simulation::make_atomic_ptr<PDEVStoneAtomic<Time, msg_type_int>, int, int, Time>(int_cycles, ext_cycles, Time(time_advance));
  86. counted_atomic_models++;
  87. cm.reset(new boost::simulation::pdevs::coupled<Time, msg_type_int>{{first_pdevstone}, {first_pdevstone}, {}, {first_pdevstone}});
  88. counted_coupled_models++;
  89. //connect higher level models
  90. for (int i=1; i < depth; i++){
  91. vector<std::shared_ptr<boost::simulation::model<Time>>> vpdt;
  92. vector<std::shared_ptr<boost::simulation::model<Time>>> eoc_cm;
  93. vector<std::shared_ptr<boost::simulation::model<Time>>> eic_cm;
  94. vector<pair<std::shared_ptr<boost::simulation::model<Time>>, std::shared_ptr<boost::simulation::model<Time>>>> ic_cm;
  95. vpdt.clear();
  96. eoc_cm.clear();
  97. eic_cm.clear();
  98. ic_cm.clear();
  99. eoc_cm.push_back(cm);
  100. for (int j=0; j < width-1; j++){
  101. std::shared_ptr<boost::simulation::model<Time>> current = std::shared_ptr<boost::simulation::model<Time>>(make_shared<PDEVStoneAtomic<Time, msg_type_int>>(int_cycles, ext_cycles, Time(time_advance)));
  102. if (j > 0) ic_cm.push_back({vpdt.back(), current});
  103. vpdt.push_back(current);
  104. counted_atomic_models++;
  105. eic_cm.push_back(current);
  106. }
  107. vpdt.push_back(cm);
  108. eic_cm.push_back(cm);
  109. shared_ptr<boost::simulation::pdevs::coupled<Time, msg_type_int>> cm_int ( new boost::simulation::pdevs::coupled<Time, msg_type_int>{vpdt, eic_cm, ic_cm, eoc_cm});
  110. counted_coupled_models++;
  111. cm=cm_int;
  112. }
  113. //Plug the input events
  114. shared_ptr<istream> piss{ new ifstream{event_list} };
  115. auto pf = boost::simulation::make_atomic_ptr<boost::simulation::pdevs::basic_models::input_stream<Time, msg_type_int, int, int>, shared_ptr<istream>, Time>(piss, Time{0});
  116. counted_atomic_models++;
  117. auto root = std::make_shared<boost::simulation::pdevs::coupled<Time, msg_type_int>>(boost::simulation::pdevs::coupled<Time, msg_type_int>({pf, cm}, {}, {{pf, cm}}, {cm}));
  118. counted_coupled_models++;
  119. return root;
  120. }
  121. shared_ptr<boost::simulation::pdevs::coupled<Time, msg_type_pair>> HO_coupling(int& counted_atomic_models, int& counted_coupled_models,
  122. int width, int depth, string event_list, int ext_cycles, int int_cycles, int time_advance)
  123. {
  124. shared_ptr<boost::simulation::pdevs::coupled<Time, msg_type_pair>> cm;
  125. auto first_pdevstone = boost::simulation::make_atomic_ptr<PDEVStoneAtomicPair<Time, msg_type_pair>, int, int, Time>(int_cycles, ext_cycles, Time(time_advance));
  126. counted_atomic_models++;
  127. cm.reset(new boost::simulation::pdevs::coupled<Time, msg_type_pair>{{first_pdevstone}, {first_pdevstone}, {}, {first_pdevstone}});
  128. counted_coupled_models++;
  129. //connect higher level models
  130. for (int i=1; i < depth; i++){
  131. auto filter0 = boost::simulation::make_atomic_ptr<PDEVStoneFilter<Time, msg_type_pair>, int, Time>(0, Time(time_advance)); // in
  132. auto filter1 = boost::simulation::make_atomic_ptr<PDEVStoneFilter<Time, msg_type_pair>, int, Time>(1, Time(time_advance)); // in2
  133. counted_atomic_models+=2;
  134. vector<std::shared_ptr<boost::simulation::model<Time>>> vpdt;
  135. vector<std::shared_ptr<boost::simulation::model<Time>>> eoc_cm;
  136. vector<std::shared_ptr<boost::simulation::model<Time>>> eic_cm;
  137. vector<pair<std::shared_ptr<boost::simulation::model<Time>>, std::shared_ptr<boost::simulation::model<Time>>>> ic_cm;
  138. vpdt.clear();
  139. eoc_cm.clear();
  140. eic_cm.clear();
  141. ic_cm.clear();
  142. eoc_cm.push_back(cm);
  143. eic_cm.push_back(filter0);
  144. eic_cm.push_back(filter1);
  145. for (int j=0; j < width-1; j++){
  146. std::shared_ptr<boost::simulation::model<Time>> current = std::shared_ptr<boost::simulation::model<Time>>(make_shared<PDEVStoneAtomicPair<Time, msg_type_pair>>(int_cycles, ext_cycles, Time(time_advance)));
  147. if (j > 0) ic_cm.push_back({vpdt.back(), current});
  148. vpdt.push_back(current);
  149. counted_atomic_models++;
  150. //eic_cm.push_back(current);
  151. ic_cm.push_back({filter1, current});
  152. eoc_cm.push_back(current);
  153. }
  154. vpdt.push_back(cm);
  155. vpdt.push_back(filter0);
  156. vpdt.push_back(filter1);
  157. //eic_cm.push_back(cm);
  158. ic_cm.push_back({filter0, cm});
  159. shared_ptr<boost::simulation::pdevs::coupled<Time, msg_type_pair>> cm_int ( new boost::simulation::pdevs::coupled<Time, msg_type_pair>{vpdt, eic_cm, ic_cm, eoc_cm});
  160. counted_coupled_models++;
  161. cm=cm_int;
  162. }
  163. //Plug the input events
  164. auto gen = boost::simulation::make_atomic_ptr<devstone_input_stream_pair<Time, msg_type_pair>, Time>(Time(time_advance));
  165. counted_atomic_models++;
  166. auto root = std::make_shared<boost::simulation::pdevs::coupled<Time, msg_type_pair>>(boost::simulation::pdevs::coupled<Time, msg_type_pair>({gen, cm}, {}, {{gen, cm}}, {cm}));
  167. counted_coupled_models++;
  168. return root;
  169. }
  170. int main(int argc, char* argv[]){
  171. auto start = hclock::now();
  172. // Declare the supported options.
  173. po::options_description desc("Allowed options");
  174. desc.add_options()
  175. ("help", "produce help message")
  176. ("kind", po::value<string>()->required(), "set kind of devstone: LI, HI or HO")
  177. ("width", po::value<int>()->required(), "set width of the DEVStone: integer value")
  178. ("depth", po::value<int>()->required(), "set depth of the DEVStone: integer value")
  179. ("int-cycles", po::value<int>()->required(), "set the Dhrystone cycles to expend in internal transtions: integer value")
  180. ("ext-cycles", po::value<int>()->required(), "set the Dhrystone cycles to expend in external transtions: integer value")
  181. ("event-list", po::value<string>()->required(), "set the file to read the events. The format is 2 ints per line meaning time->msg")
  182. ("time-advance", po::value<int>()->default_value(1), "set the time expend in external transtions by the Dhrystone in miliseconds: integer value")
  183. ;
  184. po::variables_map vm;
  185. try {
  186. po::store(po::parse_command_line(argc, argv, desc), vm);
  187. po::notify(vm);
  188. } catch ( boost::program_options::required_option be ){
  189. if (vm.count("help")) {
  190. cout << desc << "\n";
  191. return 0;
  192. } else {
  193. cout << be.what() << endl;
  194. cout << endl;
  195. cout << "for mode information run: " << argv[0] << " --help" << endl;
  196. return 1;
  197. }
  198. }
  199. string kind = vm["kind"].as<string>();
  200. if (kind.compare("LI") != 0 && kind.compare("HI") != 0 && kind.compare("HO") != 0) {
  201. cout << "The kind needs to be LI, HI or HO and received value was: " << kind;
  202. cout << endl;
  203. cout << "for mode information run: " << argv[0] << " --help" << endl;
  204. return 1;
  205. }
  206. {
  207. std::ifstream f(vm["event-list"].as<string>().c_str());
  208. if(!f.is_open()){
  209. cout << "File for events: " << vm["event-list"].as<string>() << " is not accesible." << endl;
  210. cout << endl;
  211. cout << "for mode information run: " << argv[0] << " --help" << endl;
  212. return 1;
  213. }
  214. }
  215. int width = vm["width"].as<int>();
  216. int depth = vm["depth"].as<int>();
  217. int int_cycles = vm["int-cycles"].as<int>();
  218. int ext_cycles = vm["ext-cycles"].as<int>();
  219. int time_advance = vm["time-advance"].as<int>();
  220. string event_list = vm["event-list"].as<string>();
  221. //finished processing input
  222. //create models for LI kind
  223. int models_quantity = (width - 1) * (depth - 1) + 1;
  224. int counted_atomic_models=0;
  225. int counted_coupled_models=0;
  226. auto processed_parameters = hclock::now();
  227. shared_ptr<boost::simulation::pdevs::coupled<Time, msg_type_int>> root_int;
  228. shared_ptr<boost::simulation::pdevs::coupled<Time, msg_type_pair>> root_pair;
  229. if (kind.compare("LI") == 0){
  230. root_int = LI_coupling(counted_atomic_models, counted_coupled_models, width, depth, event_list, ext_cycles, int_cycles, time_advance);
  231. } else if (kind.compare("HI") == 0){
  232. root_int = HI_coupling(counted_atomic_models, counted_coupled_models, width, depth, event_list, ext_cycles, int_cycles, time_advance);
  233. } else if (kind.compare("HO") == 0){
  234. root_pair = HO_coupling(counted_atomic_models, counted_coupled_models, width, depth, event_list, ext_cycles, int_cycles, time_advance);
  235. } else {
  236. abort();
  237. }
  238. auto model_built = hclock::now();
  239. std::chrono::_V2::system_clock::time_point model_init;
  240. //run the model
  241. if (kind.compare("LI") == 0 || kind.compare("HI") == 0) {
  242. boost::simulation::pdevs::runner<Time, msg_type_int> r(root_int, Time{0});
  243. model_init = hclock::now();
  244. r.runUntilPassivate();
  245. } else { // HO or HOmod
  246. boost::simulation::pdevs::runner<Time, msg_type_pair> r(root_pair, Time{0});
  247. model_init = hclock::now();
  248. r.runUntilPassivate();
  249. }
  250. auto finished_simulation = hclock::now();
  251. cout << "Simulation with params: ";
  252. for (const auto& it : vm) {
  253. cout << it.first.c_str() << ": ";
  254. auto& value = it.second.value();
  255. if (auto v = boost::any_cast<int>(&value))
  256. std::cout << *v;
  257. else if (auto v = boost::any_cast<std::string>(&value))
  258. std::cout << *v;
  259. else
  260. std::cout << "error";
  261. cout << " ";
  262. }
  263. cout << endl;
  264. cout << "Model creation time: " << chrono::duration_cast<chrono::duration<double, ratio<1>>>( model_built - processed_parameters).count() << endl;
  265. cout << "Engine setup time: " << chrono::duration_cast<chrono::duration<double, ratio<1>>>( model_init - model_built).count() << endl;
  266. cout << "Simulation time: " << chrono::duration_cast<chrono::duration<double, ratio<1>>>( finished_simulation - model_init).count() << endl;
  267. }