graph_manager.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /**
  2. * @file tests/boost_graph/graph_manager.hpp
  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 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_BOOST_GRAPH_GRAPH_MANAGER_HPP
  26. #define __TESTS_BOOST_GRAPH_GRAPH_MANAGER_HPP 1
  27. #include <common/scheduler/HeapScheduler.hpp>
  28. #include <common/scheduler/VectorScheduler.hpp>
  29. #include <kernel/pdevs/Coordinator.hpp>
  30. #include <kernel/pdevs/GraphManager.hpp>
  31. #include <kernel/pdevs/Simulator.hpp>
  32. #include <tests/boost_graph/models.hpp>
  33. namespace paradevs { namespace tests { namespace boost_graph {
  34. struct SchedulerHandle;
  35. typedef typename paradevs::common::scheduler::HeapScheduler <
  36. common::DoubleTime, SchedulerHandle >::type SchedulerType;
  37. struct SchedulerHandle
  38. {
  39. SchedulerHandle()
  40. { }
  41. SchedulerHandle(const SchedulerType::handle_type& handle)
  42. : _handle(handle)
  43. { }
  44. const SchedulerHandle& handle() const
  45. { return *this; }
  46. void handle(const SchedulerHandle& handle)
  47. { _handle = handle._handle; }
  48. SchedulerType::handle_type _handle;
  49. };
  50. struct GraphParameters
  51. {
  52. OrientedGraph _graph;
  53. InputEdges _input_edges;
  54. OutputEdges _output_edges;
  55. GraphParameters(const OrientedGraph& graph,
  56. const InputEdges& input_edges,
  57. const OutputEdges& output_edges) :
  58. _graph(graph), _input_edges(input_edges), _output_edges(output_edges)
  59. { }
  60. };
  61. template < class SchedulerHandle, class Parameters >
  62. class FlatGraphManager :
  63. public paradevs::pdevs::GraphManager < common::DoubleTime,
  64. SchedulerHandle,
  65. Parameters >
  66. {
  67. public:
  68. FlatGraphManager(common::Coordinator < common::DoubleTime,
  69. SchedulerHandle >* coordinator,
  70. const Parameters& parameters) :
  71. paradevs::pdevs::GraphManager < common::DoubleTime, SchedulerHandle,
  72. Parameters >(
  73. coordinator, parameters)
  74. { }
  75. virtual ~FlatGraphManager()
  76. {
  77. for (typename TopSimulators::const_iterator it =
  78. _top_simulators.begin(); it != _top_simulators.end();
  79. ++it) {
  80. delete it->second;
  81. }
  82. for (typename NormalSimulators::const_iterator it =
  83. _normal_simulators.begin(); it != _normal_simulators.end();
  84. ++it) {
  85. delete it->second;
  86. }
  87. }
  88. void build_flat_graph(const OrientedGraph& g, const InputEdges& inputs)
  89. {
  90. OrientedGraph::vertex_iterator vertexIt, vertexEnd;
  91. boost::tie(vertexIt, vertexEnd) = boost::vertices(g);
  92. for (; vertexIt != vertexEnd; ++vertexIt)
  93. {
  94. std::ostringstream ss;
  95. ss << "a" << g[*vertexIt]._index;
  96. switch (g[*vertexIt]._type) {
  97. case TOP_PIXEL:
  98. _top_simulators[g[*vertexIt]._index] =
  99. new pdevs::Simulator <
  100. common::DoubleTime, TopPixel < SchedulerHandle >,
  101. SchedulerHandle,
  102. TopPixelParameters >(ss.str(), TopPixelParameters());
  103. _top_simulators[g[*vertexIt]._index]->add_out_port("out");
  104. FlatGraphManager < SchedulerHandle, Parameters >::add_child(
  105. _top_simulators[g[*vertexIt]._index]);
  106. break;
  107. case NORMAL_PIXEL:
  108. unsigned int n = 0;
  109. OrientedGraph::adjacency_iterator neighbourIt, neighbourEnd;
  110. boost::tie(neighbourIt, neighbourEnd) =
  111. boost::adjacent_vertices(*vertexIt, g);
  112. for (; neighbourIt != neighbourEnd; ++neighbourIt) {
  113. ++n;
  114. }
  115. for (InputEdges::const_iterator it = inputs.begin();
  116. it != inputs.end(); ++it) {
  117. if (g[*vertexIt]._index == it->second) {
  118. ++n;
  119. }
  120. }
  121. _normal_simulators[g[*vertexIt]._index] =
  122. new pdevs::Simulator <
  123. common::DoubleTime, NormalPixel < SchedulerHandle >,
  124. SchedulerHandle, NormalPixelParameters >(
  125. ss.str(), NormalPixelParameters(n));
  126. _normal_simulators[g[*vertexIt]._index]->add_in_port("in");
  127. _normal_simulators[g[*vertexIt]._index]->add_out_port("out");
  128. FlatGraphManager < SchedulerHandle, Parameters >::add_child(
  129. _normal_simulators[g[*vertexIt]._index]);
  130. break;
  131. };
  132. }
  133. std::cout << FlatGraphManager <
  134. SchedulerHandle,
  135. Parameters >::get_coordinator()->get_name() << ":" << std::endl;
  136. boost::tie(vertexIt, vertexEnd) = boost::vertices(g);
  137. for (; vertexIt != vertexEnd; ++vertexIt)
  138. {
  139. OrientedGraph::adjacency_iterator neighbourIt, neighbourEnd;
  140. boost::tie(neighbourIt, neighbourEnd) =
  141. boost::adjacent_vertices(*vertexIt, g);
  142. for (; neighbourIt != neighbourEnd; ++neighbourIt) {
  143. paradevs::common::Model < common::DoubleTime,
  144. SchedulerHandle >* a = 0;
  145. paradevs::common::Model < common::DoubleTime,
  146. SchedulerHandle >* b = 0;
  147. if (g[*vertexIt]._type == TOP_PIXEL) {
  148. a = _top_simulators[g[*vertexIt]._index];
  149. } else {
  150. a = _normal_simulators[g[*vertexIt]._index];
  151. }
  152. if (g[*neighbourIt]._type == TOP_PIXEL) {
  153. b = _top_simulators[g[*neighbourIt]._index];
  154. } else {
  155. b = _normal_simulators[g[*neighbourIt]._index];
  156. }
  157. FlatGraphManager < SchedulerHandle,
  158. Parameters >::add_link(a, "out",
  159. b, "in");
  160. std::cout << " " << a->get_name() << "::out -> "
  161. << b->get_name() << "::in" << std::endl;
  162. }
  163. }
  164. }
  165. protected:
  166. typedef std::map < int, pdevs::Simulator <
  167. common::DoubleTime,
  168. TopPixel < SchedulerHandle >,
  169. SchedulerHandle,
  170. TopPixelParameters >* > TopSimulators;
  171. typedef std::map < int, pdevs::Simulator <
  172. common::DoubleTime,
  173. NormalPixel < SchedulerHandle >,
  174. SchedulerHandle,
  175. NormalPixelParameters >* > NormalSimulators;
  176. TopSimulators _top_simulators;
  177. NormalSimulators _normal_simulators;
  178. };
  179. template < class SchedulerHandle >
  180. class BuiltFlatGraphManager :
  181. public FlatGraphManager < SchedulerHandle, GraphParameters >
  182. {
  183. public:
  184. BuiltFlatGraphManager(
  185. common::Coordinator < common::DoubleTime,
  186. SchedulerHandle >* coordinator,
  187. const GraphParameters& parameters) :
  188. FlatGraphManager < SchedulerHandle, GraphParameters >(
  189. coordinator, parameters)
  190. {
  191. BuiltFlatGraphManager < SchedulerHandle >::build_flat_graph(
  192. parameters._graph, parameters._input_edges);
  193. // input
  194. for (Edges::const_iterator it = parameters._input_edges.begin();
  195. it != parameters._input_edges.end(); ++it) {
  196. std::ostringstream ss_in;
  197. ss_in << "in_" << it->first;
  198. if (not coordinator->exist_in_port(ss_in.str())) {
  199. coordinator->add_in_port(ss_in.str());
  200. }
  201. BuiltFlatGraphManager < SchedulerHandle>::add_link(
  202. coordinator, ss_in.str(),
  203. BuiltFlatGraphManager <
  204. SchedulerHandle >::_normal_simulators[it->second], "in");
  205. std::cout << coordinator->get_name() << "::" << ss_in.str()
  206. << " -> "
  207. << BuiltFlatGraphManager <
  208. SchedulerHandle >::_normal_simulators[
  209. it->second]->get_name() << "::in" << std::endl;
  210. }
  211. // output
  212. for (Edges::const_iterator it = parameters._output_edges.begin();
  213. it != parameters._output_edges.end(); ++it) {
  214. std::ostringstream ss_out;
  215. ss_out << "out_" << it->first;
  216. if (not coordinator->exist_out_port(ss_out.str())) {
  217. coordinator->add_out_port(ss_out.str());
  218. }
  219. BuiltFlatGraphManager < SchedulerHandle>::add_link(
  220. BuiltFlatGraphManager <
  221. SchedulerHandle >::_normal_simulators[it->first], "out",
  222. coordinator, ss_out.str());
  223. std::cout << BuiltFlatGraphManager <
  224. SchedulerHandle >::_normal_simulators[it->first]->get_name()
  225. << "::out -> " << coordinator->get_name()
  226. << "::" << ss_out.str() << std::endl;
  227. }
  228. }
  229. virtual ~BuiltFlatGraphManager()
  230. { }
  231. };
  232. template < class SchedulerHandle, class GraphBuilder >
  233. class InBuildFlatGraphManager :
  234. public FlatGraphManager < SchedulerHandle,
  235. paradevs::common::NoParameters >
  236. {
  237. public:
  238. InBuildFlatGraphManager(
  239. common::Coordinator < common::DoubleTime,
  240. SchedulerHandle >* coordinator,
  241. const paradevs::common::NoParameters& parameters) :
  242. FlatGraphManager < SchedulerHandle, paradevs::common::NoParameters >(
  243. coordinator, parameters)
  244. {
  245. GraphBuilder builder;
  246. OrientedGraphs graphs;
  247. InputEdgeList input_edges;
  248. OutputEdgeList output_edges;
  249. Connections parent_connections;
  250. builder.build(graphs, input_edges, output_edges, parent_connections);
  251. InBuildFlatGraphManager < SchedulerHandle,
  252. GraphBuilder >::build_flat_graph(
  253. graphs.front(), InputEdges());
  254. }
  255. virtual ~InBuildFlatGraphManager()
  256. { }
  257. };
  258. template < class SchedulerHandle, class GraphBuilder >
  259. class HierarchicalGraphManager :
  260. public paradevs::pdevs::GraphManager < common::DoubleTime,
  261. SchedulerHandle,
  262. paradevs::common::NoParameters >
  263. {
  264. public:
  265. HierarchicalGraphManager(
  266. common::Coordinator < common::DoubleTime,
  267. SchedulerHandle >* coordinator,
  268. const paradevs::common::NoParameters& parameters) :
  269. paradevs::pdevs::GraphManager < common::DoubleTime, SchedulerHandle,
  270. paradevs::common::NoParameters >(
  271. coordinator, parameters)
  272. {
  273. GraphBuilder graph_builder;
  274. OrientedGraphs graphs;
  275. InputEdgeList input_edges;
  276. OutputEdgeList output_edges;
  277. Connections parent_connections;
  278. graph_builder.build(graphs, input_edges, output_edges,
  279. parent_connections);
  280. // build coordinators (graphs)
  281. for (unsigned int i = 0; i < graphs.size(); ++i) {
  282. Coordinator* coordinator = 0;
  283. std::ostringstream ss;
  284. ss << "S" << (i + 1);
  285. coordinator =
  286. new Coordinator(ss.str(), paradevs::common::NoParameters(),
  287. GraphParameters(graphs[i], input_edges[i],
  288. output_edges[i]));
  289. _coordinators.push_back(coordinator);
  290. HierarchicalGraphManager < SchedulerHandle,
  291. GraphBuilder >::add_child(coordinator);
  292. }
  293. // builds internal connections (edges)
  294. std::cout << "parent connections:" << std::endl;
  295. for (unsigned int i = 0; i < parent_connections.size(); i++) {
  296. std::cout << " (" << parent_connections.at(i).first.first << ","
  297. << parent_connections.at(i).first.second << ") -> ("
  298. << parent_connections.at(i).second.first << ","
  299. << parent_connections.at(i).second.second << ")"
  300. << std::endl;
  301. }
  302. for (Connections::const_iterator it = parent_connections.begin();
  303. it != parent_connections.end(); ++it) {
  304. const Connection& connection = *it;
  305. std::ostringstream ss_out;
  306. std::ostringstream ss_in;
  307. ss_out << "out_" << connection.first.second;
  308. ss_in << "in_" << connection.first.second;
  309. HierarchicalGraphManager <
  310. SchedulerHandle, GraphBuilder >::add_link(
  311. _coordinators[connection.first.first - 1], ss_out.str(),
  312. _coordinators[connection.second.first - 1], ss_in.str());
  313. std::cout << _coordinators[connection.first.first - 1]->get_name()
  314. << "::" << ss_out.str() << " -> "
  315. << _coordinators[connection.second.first - 1]->get_name()
  316. << "::" << ss_in.str() << std::endl;
  317. }
  318. }
  319. virtual ~HierarchicalGraphManager()
  320. {
  321. for (typename Coordinators::const_iterator it = _coordinators.begin();
  322. it != _coordinators.end(); ++it) {
  323. delete *it;
  324. }
  325. }
  326. private:
  327. typedef paradevs::pdevs::Coordinator <
  328. common::DoubleTime,
  329. SchedulerType,
  330. SchedulerHandle,
  331. BuiltFlatGraphManager < SchedulerHandle >,
  332. common::NoParameters,
  333. GraphParameters > Coordinator;
  334. typedef std::vector < Coordinator* > Coordinators;
  335. Coordinators _coordinators;
  336. };
  337. } } } // namespace paradevs tests boost_graph
  338. #endif