graph_manager.hpp 13 KB

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