graph_manager.hpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /**
  2. * @file tests/multithreading/simple/graph_manager.cpp
  3. * @author The ARTIS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * ARTIS - the multimodeling and simulation environment
  8. * This file is a part of the ARTIS environment
  9. *
  10. * Copyright (C) 2013-2019 ULCO http://www.univ-littoral.fr
  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 3 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
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. #ifndef TESTS_MULTITHREADING_SIMPLE_GRAPH_MANAGER_HPP
  26. #define TESTS_MULTITHREADING_SIMPLE_GRAPH_MANAGER_HPP
  27. #include <tests/multithreading/simple/models.hpp>
  28. #include <artis-star/kernel/pdevs/multithreading/Coordinator.hpp>
  29. #include <artis-star/kernel/pdevs/GraphManager.hpp>
  30. #include <artis-star/kernel/pdevs/Simulator.hpp>
  31. namespace artis {
  32. namespace tests {
  33. namespace multithreading {
  34. namespace simple {
  35. class LinksGraphManager :
  36. public artis::pdevs::GraphManager<artis::common::DoubleTime, GeneratorParameters>
  37. {
  38. public:
  39. enum submodels
  40. {
  41. LINK_1, LINK_2
  42. };
  43. struct inputs
  44. {
  45. enum values
  46. {
  47. IN_1, IN_2
  48. };
  49. };
  50. struct outputs
  51. {
  52. enum values
  53. {
  54. OUT_1, OUT_2
  55. };
  56. };
  57. LinksGraphManager(
  58. artis::common::Coordinator<artis::common::DoubleTime> *coordinator,
  59. const GeneratorParameters &parameters,
  60. const artis::common::NoParameters &graph_parameters)
  61. :
  62. artis::pdevs::GraphManager<artis::common::DoubleTime, GeneratorParameters>(
  63. coordinator, parameters, graph_parameters),
  64. _link_1("link_1", artis::common::NoParameters()),
  65. _link_2("link_2", artis::common::NoParameters())
  66. {
  67. add_child(LINK_1, &_link_1);
  68. add_child(LINK_2, &_link_2);
  69. coordinator->input_ports({{inputs::IN_1, "in_1"},
  70. {inputs::IN_2, "in_2"}});
  71. coordinator->output_ports({{outputs::OUT_1, "out_1"},
  72. {outputs::OUT_2, "out_2"}});
  73. in({coordinator, inputs::IN_1})
  74. >> out({&_link_1, Link::inputs::IN});
  75. in({coordinator, inputs::IN_2})
  76. >> out({&_link_2, Link::inputs::IN});
  77. out({&_link_1, Link::outputs::OUT})
  78. >> out({coordinator, outputs::OUT_1});
  79. out({&_link_2, Link::outputs::OUT})
  80. >> out({coordinator, outputs::OUT_2});
  81. }
  82. ~LinksGraphManager() override = default;
  83. common::DoubleTime::type
  84. lookahead(const common::DoubleTime::type &t) const override
  85. {
  86. std::vector<double> lookaheads = {_link_1.lookahead(t),
  87. _link_2.lookahead(t)};
  88. return *std::min(lookaheads.begin(), lookaheads.end());
  89. }
  90. void init()
  91. {}
  92. private:
  93. artis::pdevs::Simulator<artis::common::DoubleTime, Link> _link_1;
  94. artis::pdevs::Simulator<artis::common::DoubleTime, Link> _link_2;
  95. };
  96. class SimpleGraphManager :
  97. public artis::pdevs::GraphManager<artis::common::DoubleTime, GeneratorParameters>
  98. {
  99. public:
  100. enum submodels
  101. {
  102. GENERATOR, LINK_1_2, LINK_3_4, LINK_5_6, LINK_7_8, COUNTER
  103. };
  104. typedef artis::pdevs::multithreading::Coordinator<
  105. artis::common::DoubleTime,
  106. SimpleGraphManager,
  107. GeneratorParameters,
  108. artis::common::NoParameters> coordinator_type;
  109. SimpleGraphManager(
  110. artis::common::Coordinator<artis::common::DoubleTime> *coordinator,
  111. const GeneratorParameters &parameters,
  112. const artis::common::NoParameters &graph_parameters)
  113. :
  114. artis::pdevs::GraphManager<artis::common::DoubleTime, GeneratorParameters>(
  115. coordinator, parameters, graph_parameters),
  116. _generator("generator", parameters),
  117. _link_1_2("link_1_2", parameters, artis::common::NoParameters()),
  118. _link_3_4("link_3_4", parameters, artis::common::NoParameters()),
  119. _link_5_6("link_5_6", parameters, artis::common::NoParameters()),
  120. _link_7_8("link_7_8", parameters, artis::common::NoParameters()),
  121. _counter("counter", artis::common::NoParameters())
  122. {
  123. add_child(GENERATOR, &_generator);
  124. add_child(LINK_1_2, &_link_1_2);
  125. add_child(LINK_3_4, &_link_3_4);
  126. add_child(LINK_5_6, &_link_5_6);
  127. add_child(LINK_7_8, &_link_7_8);
  128. add_child(COUNTER, &_counter);
  129. out({&_generator, Generator::outputs::OUT})
  130. >> in({&_link_1_2, LinksGraphManager::inputs::IN_1});
  131. out({&_generator, Generator::outputs::OUT + 1})
  132. >> in({&_link_1_2, LinksGraphManager::inputs::IN_2});
  133. out({&_generator, Generator::outputs::OUT + 2})
  134. >> in({&_link_3_4, LinksGraphManager::inputs::IN_1});
  135. out({&_generator, Generator::outputs::OUT + 3})
  136. >> in({&_link_3_4, LinksGraphManager::inputs::IN_2});
  137. out({&_generator, Generator::outputs::OUT + 4})
  138. >> in({&_link_5_6, LinksGraphManager::inputs::IN_1});
  139. out({&_generator, Generator::outputs::OUT + 5})
  140. >> in({&_link_5_6, LinksGraphManager::inputs::IN_2});
  141. out({&_generator, Generator::outputs::OUT + 6})
  142. >> in({&_link_7_8, LinksGraphManager::inputs::IN_1});
  143. out({&_generator, Generator::outputs::OUT + 7})
  144. >> in({&_link_7_8, LinksGraphManager::inputs::IN_2});
  145. out({&_link_1_2, LinksGraphManager::outputs::OUT_1})
  146. >> in({&_counter, Counter::inputs::IN});
  147. out({&_link_1_2, LinksGraphManager::outputs::OUT_2})
  148. >> in({&_counter, Counter::inputs::IN});
  149. out({&_link_3_4, LinksGraphManager::outputs::OUT_1})
  150. >> in({&_counter, Counter::inputs::IN});
  151. out({&_link_3_4, LinksGraphManager::outputs::OUT_2})
  152. >> in({&_counter, Counter::inputs::IN});
  153. out({&_link_5_6, LinksGraphManager::outputs::OUT_1})
  154. >> in({&_counter, Counter::inputs::IN});
  155. out({&_link_5_6, LinksGraphManager::outputs::OUT_2})
  156. >> in({&_counter, Counter::inputs::IN});
  157. out({&_link_7_8, LinksGraphManager::outputs::OUT_1})
  158. >> in({&_counter, Counter::inputs::IN});
  159. out({&_link_7_8, LinksGraphManager::outputs::OUT_2})
  160. >> in({&_counter, Counter::inputs::IN});
  161. }
  162. ~SimpleGraphManager() override = default;
  163. common::DoubleTime::type
  164. lookahead(const common::DoubleTime::type &t) const override
  165. {
  166. std::vector<double> lookaheads = {_generator.lookahead(t),
  167. _link_1_2.lookahead(t),
  168. _link_3_4.lookahead(t),
  169. _link_5_6.lookahead(t),
  170. _link_7_8.lookahead(t),
  171. _counter.lookahead(t)};
  172. return *std::min(lookaheads.begin(), lookaheads.end());
  173. }
  174. // void init()
  175. // {
  176. // dynamic_cast< coordinator_type *>(this->coordinator())
  177. // ->attach_child(&_link_1_2, _link_1_2.get_queue());
  178. // dynamic_cast< coordinator_type *>(this->coordinator())
  179. // ->attach_child(&_link_3_4, _link_3_4.get_queue());
  180. // dynamic_cast< coordinator_type *>(this->coordinator())
  181. // ->attach_child(&_link_5_6, _link_5_6.get_queue());
  182. // dynamic_cast< coordinator_type *>(this->coordinator())
  183. // ->attach_child(&_link_7_8, _link_7_8.get_queue());
  184. //
  185. // _link_1_2.attach_parent(dynamic_cast< coordinator_type *>(this->coordinator())->get_queue());
  186. // _link_3_4.attach_parent(dynamic_cast< coordinator_type *>(this->coordinator())->get_queue());
  187. // _link_5_6.attach_parent(dynamic_cast< coordinator_type *>(this->coordinator())->get_queue());
  188. // _link_7_8.attach_parent(dynamic_cast< coordinator_type *>(this->coordinator())->get_queue());
  189. // }
  190. private:
  191. artis::pdevs::Simulator<artis::common::DoubleTime, Generator, GeneratorParameters> _generator;
  192. artis::pdevs::Coordinator<artis::common::DoubleTime,
  193. LinksGraphManager,
  194. GeneratorParameters,
  195. artis::common::NoParameters> _link_1_2;
  196. artis::pdevs::Coordinator<artis::common::DoubleTime,
  197. LinksGraphManager,
  198. GeneratorParameters,
  199. artis::common::NoParameters> _link_3_4;
  200. artis::pdevs::Coordinator<artis::common::DoubleTime,
  201. LinksGraphManager,
  202. GeneratorParameters,
  203. artis::common::NoParameters> _link_5_6;
  204. artis::pdevs::Coordinator<artis::common::DoubleTime,
  205. LinksGraphManager,
  206. GeneratorParameters,
  207. artis::common::NoParameters> _link_7_8;
  208. // artis::pdevs::multithreading::Coordinator<artis::common::DoubleTime,
  209. // LinksGraphManager,
  210. // GeneratorParameters,
  211. // artis::common::NoParameters> _link_1_2;
  212. // artis::pdevs::multithreading::Coordinator<artis::common::DoubleTime,
  213. // LinksGraphManager,
  214. // GeneratorParameters,
  215. // artis::common::NoParameters> _link_3_4;
  216. // artis::pdevs::multithreading::Coordinator<artis::common::DoubleTime,
  217. // LinksGraphManager,
  218. // GeneratorParameters,
  219. // artis::common::NoParameters> _link_5_6;
  220. // artis::pdevs::multithreading::Coordinator<artis::common::DoubleTime,
  221. // LinksGraphManager,
  222. // GeneratorParameters,
  223. // artis::common::NoParameters> _link_7_8;
  224. artis::pdevs::Simulator<artis::common::DoubleTime, Counter> _counter;
  225. };
  226. }
  227. }
  228. }
  229. } // namespace artis tests multithreading simple
  230. #endif