graph_manager.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /**
  2. * @file tests/plot/graph_manager.cpp
  3. * @author The PARADEVS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * PARADEVS - the multimodeling and simulation environment
  8. * This file is a part of the PARADEVS environment
  9. *
  10. * Copyright (C) 2013-2015 ULCO http://www.univ-litoral.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_PLOT_GRAPH_MANAGER_HPP
  26. #define TESTS_PLOT_GRAPH_MANAGER_HPP 1
  27. #include <tests/plot/graph_builder.hpp>
  28. #include <tests/plot/models.hpp>
  29. #include <paradevs/kernel/pdevs/multithreading/Coordinator.hpp>
  30. #include <paradevs/kernel/pdevs/GraphManager.hpp>
  31. #include <paradevs/kernel/pdevs/Simulator.hpp>
  32. using namespace paradevs::tests::boost_graph;
  33. namespace paradevs { namespace tests { namespace plot {
  34. struct GraphParameters
  35. {
  36. OrientedGraph _graph;
  37. InputEdges _input_edges;
  38. OutputEdges _output_edges;
  39. GraphParameters(const OrientedGraph& graph,
  40. const InputEdges& input_edges,
  41. const OutputEdges& output_edges) :
  42. _graph(graph), _input_edges(input_edges), _output_edges(output_edges)
  43. { }
  44. };
  45. template < class Parameters >
  46. class FlatGraphManager :
  47. public paradevs::pdevs::GraphManager < common::DoubleTime,
  48. Parameters >
  49. {
  50. public:
  51. FlatGraphManager(common::Coordinator < common::DoubleTime >* coordinator,
  52. const Parameters& parameters) :
  53. paradevs::pdevs::GraphManager < common::DoubleTime, Parameters >(
  54. coordinator, parameters)
  55. { }
  56. virtual ~FlatGraphManager()
  57. {
  58. for (typename Simulators::const_iterator it =
  59. _simulators.begin(); it != _simulators.end();
  60. ++it) {
  61. delete it->second;
  62. }
  63. }
  64. void build_flat_graph(const OrientedGraph& g, const InputEdges& inputs)
  65. {
  66. OrientedGraph::vertex_iterator vertexIt, vertexEnd;
  67. boost::tie(vertexIt, vertexEnd) = boost::vertices(g);
  68. for (; vertexIt != vertexEnd; ++vertexIt) {
  69. std::ostringstream ss;
  70. PlotParameters parameters(g[*vertexIt]._index,
  71. g[*vertexIt]._points,
  72. g[*vertexIt]._neighbour_number);
  73. ss << "" << g[*vertexIt]._index;
  74. // std::cout << "CREATE SIMULATOR: " << g[*vertexIt]._index
  75. // << std::endl;
  76. _simulators[g[*vertexIt]._index] = new Simulator(ss.str(),
  77. parameters);
  78. _simulators[g[*vertexIt]._index]->add_out_port("out");
  79. _simulators[g[*vertexIt]._index]->add_in_port("in");
  80. FlatGraphManager < Parameters >::add_child(
  81. _simulators[g[*vertexIt]._index]);
  82. }
  83. boost::tie(vertexIt, vertexEnd) = boost::vertices(g);
  84. for (; vertexIt != vertexEnd; ++vertexIt)
  85. {
  86. OrientedGraph::adjacency_iterator neighbourIt, neighbourEnd;
  87. boost::tie(neighbourIt, neighbourEnd) =
  88. boost::adjacent_vertices(*vertexIt, g);
  89. for (; neighbourIt != neighbourEnd; ++neighbourIt) {
  90. paradevs::common::Model < common::DoubleTime >* a = 0;
  91. paradevs::common::Model < common::DoubleTime >* b = 0;
  92. a = _simulators[g[*vertexIt]._index];
  93. b = _simulators[g[*neighbourIt]._index];
  94. FlatGraphManager < Parameters >::add_link(a, "out",
  95. b, "in");
  96. }
  97. }
  98. }
  99. protected:
  100. typedef pdevs::Simulator < common::DoubleTime,
  101. Plot,
  102. PlotParameters > Simulator;
  103. typedef std::map < int, Simulator* > Simulators;
  104. Simulators _simulators;
  105. };
  106. class BuiltFlatGraphManager :
  107. public FlatGraphManager < GraphParameters >
  108. {
  109. public:
  110. BuiltFlatGraphManager(
  111. common::Coordinator < common::DoubleTime >* coordinator,
  112. const GraphParameters& parameters) :
  113. FlatGraphManager < GraphParameters >(
  114. coordinator, parameters)
  115. {
  116. BuiltFlatGraphManager::build_flat_graph(
  117. parameters._graph, parameters._input_edges);
  118. // input
  119. for (Edges::const_iterator it = parameters._input_edges.begin();
  120. it != parameters._input_edges.end(); ++it) {
  121. std::ostringstream ss_in;
  122. ss_in << "in_" << it->first;
  123. if (not coordinator->exist_in_port(ss_in.str())) {
  124. coordinator->add_in_port(ss_in.str());
  125. }
  126. // std::cout << "CREATE LINK TO => " << it->second << " "
  127. // << std::endl;
  128. BuiltFlatGraphManager::add_link(
  129. coordinator, ss_in.str(),
  130. BuiltFlatGraphManager::_simulators[it->second], "in");
  131. // BuiltFlatGraphManager::_simulators[
  132. // parameters._graph[it->second]._index], "in");
  133. }
  134. // output
  135. for (Edges::const_iterator it = parameters._output_edges.begin();
  136. it != parameters._output_edges.end(); ++it) {
  137. std::ostringstream ss_out;
  138. ss_out << "out_" << it->first;
  139. if (not coordinator->exist_out_port(ss_out.str())) {
  140. coordinator->add_out_port(ss_out.str());
  141. }
  142. if (not BuiltFlatGraphManager::exist_link(
  143. BuiltFlatGraphManager::_simulators[it->first],
  144. "out", coordinator, ss_out.str())) {
  145. BuiltFlatGraphManager::add_link(
  146. BuiltFlatGraphManager::_simulators[it->first],
  147. "out", coordinator, ss_out.str());
  148. }
  149. }
  150. }
  151. virtual ~BuiltFlatGraphManager()
  152. { }
  153. };
  154. class ParallelBuiltFlatGraphManager :
  155. public FlatGraphManager < GraphParameters >
  156. {
  157. public:
  158. ParallelBuiltFlatGraphManager(
  159. common::Coordinator < common::DoubleTime >* coordinator,
  160. const GraphParameters& parameters) :
  161. FlatGraphManager < GraphParameters >(
  162. coordinator, parameters)
  163. {
  164. ParallelBuiltFlatGraphManager::build_flat_graph(
  165. parameters._graph, parameters._input_edges);
  166. // input
  167. for (Edges::const_iterator it = parameters._input_edges.begin();
  168. it != parameters._input_edges.end(); ++it) {
  169. std::ostringstream ss_in;
  170. ss_in << "in_" << it->first;
  171. if (not coordinator->exist_in_port(ss_in.str())) {
  172. coordinator->add_in_port(ss_in.str());
  173. }
  174. ParallelBuiltFlatGraphManager::add_link(
  175. coordinator, ss_in.str(),
  176. ParallelBuiltFlatGraphManager::_simulators[it->second],
  177. "in");
  178. }
  179. // output
  180. for (Edges::const_iterator it = parameters._output_edges.begin();
  181. it != parameters._output_edges.end(); ++it) {
  182. std::ostringstream ss_out;
  183. ss_out << "out_" << it->first;
  184. if (not coordinator->exist_out_port(ss_out.str())) {
  185. coordinator->add_out_port(ss_out.str());
  186. }
  187. if (not ParallelBuiltFlatGraphManager::exist_link(
  188. ParallelBuiltFlatGraphManager::_simulators[it->first],
  189. "out", coordinator, ss_out.str())) {
  190. ParallelBuiltFlatGraphManager::add_link(
  191. ParallelBuiltFlatGraphManager::_simulators[it->first],
  192. "out", coordinator, ss_out.str());
  193. }
  194. }
  195. }
  196. void init()
  197. { }
  198. void start(common::DoubleTime::type /* t */)
  199. { }
  200. void transition(
  201. const common::Models < common::DoubleTime >& /* receivers */,
  202. common::DoubleTime::type /* t */)
  203. { }
  204. virtual ~ParallelBuiltFlatGraphManager()
  205. { }
  206. };
  207. struct GraphManagerParameters
  208. {
  209. GraphManagerParameters(const std::string& file_name,
  210. int cluster_number) :
  211. cluster_number(cluster_number), file_name(file_name)
  212. { }
  213. int cluster_number;
  214. std::string file_name;
  215. };
  216. class HierarchicalGraphManager :
  217. public paradevs::pdevs::GraphManager < common::DoubleTime,
  218. GraphManagerParameters >
  219. {
  220. public:
  221. HierarchicalGraphManager(
  222. common::Coordinator < common::DoubleTime >* coordinator,
  223. const GraphManagerParameters& parameters) :
  224. paradevs::pdevs::GraphManager < common::DoubleTime,
  225. GraphManagerParameters >(
  226. coordinator, parameters)
  227. {
  228. GraphBuilder graph_builder(parameters.file_name,
  229. parameters.cluster_number);
  230. OrientedGraphs graphs;
  231. InputEdgeList input_edges;
  232. OutputEdgeList output_edges;
  233. Connections parent_connections;
  234. graph_builder.build(graphs, input_edges, output_edges,
  235. parent_connections);
  236. // build coordinators (graphs)
  237. for (unsigned int i = 0; i < graphs.size(); ++i) {
  238. Coordinator* coordinator = 0;
  239. std::ostringstream ss;
  240. ss << "S" << i;
  241. coordinator =
  242. new Coordinator(ss.str(), paradevs::common::NoParameters(),
  243. GraphParameters(graphs[i],
  244. input_edges[i],
  245. output_edges[i]));
  246. _coordinators.push_back(coordinator);
  247. add_child(coordinator);
  248. }
  249. // builds internal connections (edges)
  250. for (Connections::const_iterator it = parent_connections.begin();
  251. it != parent_connections.end(); ++it) {
  252. const Connection& connection = *it;
  253. std::ostringstream ss_out;
  254. std::ostringstream ss_in;
  255. ss_out << "out_" << connection.first.second;
  256. ss_in << "in_" << connection.first.second;
  257. if (not exist_link(
  258. _coordinators[connection.first.first - 1],
  259. ss_out.str(),
  260. _coordinators[connection.second.first - 1],
  261. ss_in.str())) {
  262. add_link(
  263. _coordinators[connection.first.first - 1],
  264. ss_out.str(),
  265. _coordinators[connection.second.first - 1],
  266. ss_in.str());
  267. }
  268. }
  269. }
  270. virtual ~HierarchicalGraphManager()
  271. {
  272. for (typename Coordinators::const_iterator it = _coordinators.begin();
  273. it != _coordinators.end(); ++it) {
  274. delete *it;
  275. }
  276. }
  277. private:
  278. typedef paradevs::pdevs::Coordinator <
  279. common::DoubleTime,
  280. BuiltFlatGraphManager,
  281. common::NoParameters,
  282. GraphParameters
  283. > Coordinator;
  284. typedef std::vector < Coordinator* > Coordinators;
  285. Coordinators _coordinators;
  286. };
  287. class ParallelHierarchicalGraphManager :
  288. public paradevs::pdevs::GraphManager < common::DoubleTime,
  289. GraphManagerParameters >
  290. {
  291. public:
  292. ParallelHierarchicalGraphManager(
  293. common::Coordinator < common::DoubleTime >* coordinator,
  294. const GraphManagerParameters& parameters) :
  295. paradevs::pdevs::GraphManager < common::DoubleTime,
  296. GraphManagerParameters >(
  297. coordinator, parameters)
  298. {
  299. GraphBuilder graph_builder(parameters.file_name,
  300. parameters.cluster_number);
  301. OrientedGraphs graphs;
  302. InputEdgeList input_edges;
  303. OutputEdgeList output_edges;
  304. Connections parent_connections;
  305. graph_builder.build(graphs, input_edges, output_edges,
  306. parent_connections);
  307. // build coordinators (graphs)
  308. for (unsigned int i = 0; i < graphs.size(); ++i) {
  309. ParallelCoordinator* coordinator = 0;
  310. std::ostringstream ss;
  311. ss << "S" << i;
  312. coordinator =
  313. new ParallelCoordinator(ss.str(),
  314. paradevs::common::NoParameters(),
  315. GraphParameters(graphs[i],
  316. input_edges[i],
  317. output_edges[i]));
  318. _coordinators.push_back(coordinator);
  319. add_child(coordinator);
  320. }
  321. // builds internal connections (edges)
  322. for (Connections::const_iterator it = parent_connections.begin();
  323. it != parent_connections.end(); ++it) {
  324. const Connection& connection = *it;
  325. std::ostringstream ss_out;
  326. std::ostringstream ss_in;
  327. ss_out << "out_" << connection.first.second;
  328. ss_in << "in_" << connection.first.second;
  329. if (not exist_link(
  330. _coordinators[connection.first.first - 1],
  331. ss_out.str(),
  332. _coordinators[connection.second.first - 1],
  333. ss_in.str())) {
  334. add_link(
  335. _coordinators[connection.first.first - 1],
  336. ss_out.str(),
  337. _coordinators[connection.second.first - 1],
  338. ss_in.str());
  339. }
  340. }
  341. }
  342. virtual ~ParallelHierarchicalGraphManager()
  343. {
  344. for (typename Coordinators::const_iterator it = _coordinators.begin();
  345. it != _coordinators.end(); ++it) {
  346. delete *it;
  347. }
  348. }
  349. void init()
  350. {
  351. for (typename Coordinators::const_iterator it = _coordinators.begin();
  352. it != _coordinators.end(); ++it) {
  353. (*it)->set_sender(
  354. dynamic_cast < paradevs::pdevs::multithreading::Coordinator <
  355. common::DoubleTime,
  356. ParallelHierarchicalGraphManager,
  357. paradevs::common::NoParameters,
  358. GraphManagerParameters >*
  359. >(get_coordinator())->get_sender());
  360. }
  361. }
  362. void start(common::DoubleTime::type t)
  363. {
  364. for (typename Coordinators::const_iterator it = _coordinators.begin();
  365. it != _coordinators.end(); ++it) {
  366. (*it)->get_sender().send(
  367. paradevs::pdevs::multithreading::start_message <
  368. paradevs::common::DoubleTime >(t));
  369. }
  370. }
  371. void transition(const common::Models < common::DoubleTime >& receivers,
  372. paradevs::common::DoubleTime::type t)
  373. {
  374. common::Models < common::DoubleTime >::const_iterator it =
  375. receivers.begin();
  376. while (it != receivers.end()) {
  377. if (not (*it)->is_atomic()) {
  378. typename Coordinators::const_iterator itc =
  379. std::find(_coordinators.begin(), _coordinators.end(), *it);
  380. (*itc)->get_sender().send(
  381. paradevs::pdevs::multithreading::transition_message <
  382. paradevs::common::DoubleTime >(t));
  383. }
  384. ++it;
  385. }
  386. }
  387. private:
  388. typedef paradevs::pdevs::multithreading::Coordinator <
  389. common::DoubleTime,
  390. ParallelBuiltFlatGraphManager,
  391. common::NoParameters,
  392. GraphParameters
  393. > ParallelCoordinator;
  394. typedef std::vector < ParallelCoordinator* > Coordinators;
  395. Coordinators _coordinators;
  396. };
  397. } } } // namespace paradevs tests plot
  398. #endif