graph_manager.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /**
  2. * @file tests/mpi/cluster/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_PDEVS_MPI_CLUSTER_GRAPH_MANAGER_HPP
  26. #define TESTS_PDEVS_MPI_CLUSTER_GRAPH_MANAGER_HPP 1
  27. #include <tests/pdevs/graph_manager.hpp>
  28. #include <tests/mpi/cluster/models.hpp>
  29. #include <paradevs/kernel/pdevs/mpi/Coordinator.hpp>
  30. #include <paradevs/kernel/pdevs/mpi/ModelProxy.hpp>
  31. #include <paradevs/kernel/pdevs/mpi/GraphManager.hpp>
  32. #include <paradevs/kernel/pdevs/Simulator.hpp>
  33. #include <sstream>
  34. namespace paradevs { namespace tests { namespace mpi { namespace cluster {
  35. struct SubGraphManagerParameters
  36. {
  37. std::vector < int > indexes;
  38. std::vector < std::pair < int, int > > internals;
  39. std::vector < std::pair < int, int > > inputs;
  40. std::vector < std::pair < int, int > > outputs;
  41. };
  42. class SubGraphManager :
  43. public paradevs::pdevs::mpi::GraphManager < common::DoubleTime,
  44. SubGraphManagerParameters >
  45. {
  46. public:
  47. SubGraphManager(common::Coordinator < common::DoubleTime >* coordinator,
  48. const SubGraphManagerParameters& parameters) :
  49. paradevs::pdevs::mpi::GraphManager <
  50. common::DoubleTime,
  51. SubGraphManagerParameters >(coordinator, parameters)
  52. {
  53. for (std::vector < int >::const_iterator it = parameters.indexes.begin();
  54. it != parameters.indexes.end(); ++it) {
  55. std::stringstream ss;
  56. ss << "m_" << *it;
  57. Simulator* m = new Simulator(ss.str(), paradevs::common::NoParameters());
  58. models[*it] = m;
  59. add_child(m);
  60. m->add_in_port("in");
  61. m->add_out_port("out");
  62. }
  63. for (std::vector < std::pair < int, int > >::const_iterator it =
  64. parameters.inputs.begin(); it != parameters.inputs.end(); ++it) {
  65. std::stringstream ss;
  66. ss << "in_" << it->first;
  67. if (not coordinator->exist_in_port(ss.str())) {
  68. coordinator->add_in_port(ss.str());
  69. }
  70. if (not exist_link(coordinator, ss.str(), models[it->second], "in")) {
  71. add_link(coordinator, ss.str(), models[it->second], "in");
  72. }
  73. }
  74. for (std::vector < std::pair < int, int > >::const_iterator it =
  75. parameters.outputs.begin(); it != parameters.outputs.end(); ++it) {
  76. std::stringstream ss;
  77. ss << "out_" << it->first;
  78. if (not coordinator->exist_out_port(ss.str())) {
  79. coordinator->add_out_port(ss.str());
  80. }
  81. if (not exist_link(models[it->first], "out", coordinator, ss.str())) {
  82. add_link(models[it->first], "out", coordinator, ss.str());
  83. }
  84. }
  85. for (std::vector < std::pair < int, int > >::const_iterator it =
  86. parameters.internals.begin(); it != parameters.internals.end();
  87. ++it) {
  88. if (not exist_link(models[it->first], "out", models[it->second], "in")) {
  89. add_link(models[it->first], "out", models[it->second], "in");
  90. }
  91. }
  92. }
  93. void init()
  94. { }
  95. void start(common::DoubleTime::type /* t */)
  96. { }
  97. void transition(const common::Models < common::DoubleTime >& /* receivers */,
  98. common::DoubleTime::type /* t */)
  99. { }
  100. virtual ~SubGraphManager()
  101. {
  102. for (Simulators::iterator it = models.begin(); it != models.end(); ++it) {
  103. delete it->second;
  104. }
  105. }
  106. private:
  107. typedef paradevs::pdevs::Simulator < common::DoubleTime,
  108. ThreeStateModel > Simulator;
  109. typedef std::map < int, Simulator* > Simulators;
  110. Simulators models;
  111. };
  112. struct RootGraphManagerParameters
  113. {
  114. std::vector <
  115. std::vector <
  116. std::pair <
  117. std::pair < int, int >,
  118. std::pair < int, int > > > > parents;
  119. };
  120. class LevelGraphManager :
  121. public paradevs::pdevs::GraphManager < common::DoubleTime,
  122. RootGraphManagerParameters >
  123. {
  124. public:
  125. LevelGraphManager(
  126. common::Coordinator < common::DoubleTime >* coordinator,
  127. const RootGraphManagerParameters& parameters) :
  128. paradevs::pdevs::GraphManager < common::DoubleTime,
  129. RootGraphManagerParameters >(
  130. coordinator, parameters)
  131. {
  132. int index = 0;
  133. for (std::vector < std::vector <
  134. std::pair < std::pair < int, int >,
  135. std::pair < int, int > > > >::const_iterator it =
  136. parameters.parents.begin(); it != parameters.parents.end();
  137. ++it) {
  138. std::stringstream ss;
  139. ModelProxy* model = 0;
  140. ss << "S_" << index;
  141. model = new ModelProxy(ss.str(), index + 1, false);
  142. models.push_back(model);
  143. add_child(model);
  144. ++index;
  145. }
  146. for (std::vector < std::vector <
  147. std::pair < std::pair < int, int >,
  148. std::pair < int, int > > > >::const_iterator it =
  149. parameters.parents.begin(); it != parameters.parents.end();
  150. ++it) {
  151. for (std::vector <
  152. std::pair < std::pair < int, int >,
  153. std::pair < int, int > > >::const_iterator it2 =
  154. it->begin(); it2 != it->end(); ++it2) {
  155. std::stringstream out_ss;
  156. std::stringstream in_ss;
  157. out_ss << "out_" << it2->first.second;
  158. if (not models[it2->first.first]->exist_out_port(out_ss.str())) {
  159. models[it2->first.first]->add_out_port(out_ss.str());
  160. }
  161. in_ss << "in_" << it2->first.second;
  162. if (not models[it2->second.first]->exist_in_port(in_ss.str())) {
  163. models[it2->second.first]->add_in_port(in_ss.str());
  164. }
  165. if (not exist_link(models[it2->first.first], out_ss.str(),
  166. models[it2->second.first], in_ss.str())) {
  167. add_link(models[it2->first.first], out_ss.str(),
  168. models[it2->second.first], in_ss.str());
  169. }
  170. }
  171. }
  172. }
  173. void init()
  174. { }
  175. void start(common::DoubleTime::type /* t */)
  176. { }
  177. void transition(
  178. const common::Models < common::DoubleTime >& /* receivers */,
  179. common::DoubleTime::type /* t */)
  180. { }
  181. virtual ~LevelGraphManager()
  182. {
  183. std::for_each(models.begin(), models.end(),
  184. std::default_delete < ModelProxy >());
  185. }
  186. private:
  187. typedef paradevs::pdevs::mpi::ModelProxy < common::DoubleTime > ModelProxy;
  188. typedef std::vector < ModelProxy* > ModelProxies;
  189. ModelProxies models;
  190. };
  191. class RootGraphManager :
  192. public paradevs::pdevs::GraphManager < common::DoubleTime,
  193. RootGraphManagerParameters >
  194. {
  195. public:
  196. RootGraphManager(
  197. common::Coordinator < common::DoubleTime >* coordinator,
  198. const RootGraphManagerParameters& parameters) :
  199. paradevs::pdevs::GraphManager < common::DoubleTime,
  200. RootGraphManagerParameters >(
  201. coordinator, parameters),
  202. S("S", paradevs::common::NoParameters(), parameters)
  203. {
  204. add_child(&S);
  205. }
  206. void init()
  207. {
  208. S.set_sender(
  209. dynamic_cast < paradevs::pdevs::multithreading::Coordinator <
  210. common::DoubleTime,
  211. paradevs::tests::mpi::cluster::RootGraphManager,
  212. paradevs::common::NoParameters,
  213. paradevs::tests::mpi::cluster::RootGraphManagerParameters >*
  214. >(get_coordinator())->get_sender());
  215. }
  216. void start(common::DoubleTime::type t)
  217. {
  218. S.get_sender().send(
  219. paradevs::pdevs::multithreading::start_message <
  220. common::DoubleTime >(t));
  221. }
  222. void transition(const common::Models < common::DoubleTime >& receivers,
  223. common::DoubleTime::type t)
  224. {
  225. S.get_sender().send(
  226. paradevs::pdevs::multithreading::transition_message <
  227. common::DoubleTime >(t));
  228. }
  229. virtual ~RootGraphManager()
  230. { }
  231. private:
  232. paradevs::pdevs::Coordinator <
  233. common::DoubleTime, LevelGraphManager, paradevs::common::NoParameters, RootGraphManagerParameters > S;
  234. };
  235. } } } } // namespace paradevs tests mpi cluster
  236. #endif