graph_manager.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. add_link(coordinator, ss.str(), models[it->second], "in");
  71. }
  72. for (std::vector < std::pair < int, int > >::const_iterator it =
  73. parameters.outputs.begin(); it != parameters.outputs.end(); ++it) {
  74. std::stringstream ss;
  75. ss << "out_" << it->first;
  76. if (not coordinator->exist_out_port(ss.str())) {
  77. coordinator->add_out_port(ss.str());
  78. }
  79. add_link(models[it->first], "out", coordinator, ss.str());
  80. }
  81. for (std::vector < std::pair < int, int > >::const_iterator it =
  82. parameters.internals.begin(); it != parameters.internals.end();
  83. ++it) {
  84. add_link(models[it->first], "out", models[it->second], "in");
  85. }
  86. }
  87. void init()
  88. { }
  89. void start(common::DoubleTime::type /* t */)
  90. { }
  91. void transition(const common::Models < common::DoubleTime >& /* receivers */,
  92. common::DoubleTime::type /* t */)
  93. { }
  94. virtual ~SubGraphManager()
  95. {
  96. for (Simulators::iterator it = models.begin(); it != models.end(); ++it) {
  97. delete it->second;
  98. }
  99. }
  100. private:
  101. typedef paradevs::pdevs::Simulator < common::DoubleTime,
  102. ThreeStateModel > Simulator;
  103. typedef std::map < int, Simulator* > Simulators;
  104. Simulators models;
  105. };
  106. struct RootGraphManagerParameters
  107. {
  108. std::vector <
  109. std::vector <
  110. std::pair <
  111. std::pair < int, int >,
  112. std::pair < int, int > > > > parents;
  113. };
  114. class RootGraphManager :
  115. public paradevs::pdevs::GraphManager < common::DoubleTime,
  116. RootGraphManagerParameters >
  117. {
  118. public:
  119. RootGraphManager(
  120. common::Coordinator < common::DoubleTime >* coordinator,
  121. const RootGraphManagerParameters& parameters) :
  122. paradevs::pdevs::GraphManager < common::DoubleTime,
  123. RootGraphManagerParameters >(
  124. coordinator, parameters)
  125. {
  126. int index = 0;
  127. for (std::vector < std::vector <
  128. std::pair < std::pair < int, int >,
  129. std::pair < int, int > > > >::const_iterator it =
  130. parameters.parents.begin(); it != parameters.parents.end();
  131. ++it) {
  132. std::stringstream ss;
  133. ModelProxy* model = 0;
  134. ss << "S_" << index;
  135. model = new ModelProxy(ss.str(), index + 1, false);
  136. models.push_back(model);
  137. add_child(model);
  138. ++index;
  139. }
  140. for (std::vector < std::vector <
  141. std::pair < std::pair < int, int >,
  142. std::pair < int, int > > > >::const_iterator it =
  143. parameters.parents.begin(); it != parameters.parents.end();
  144. ++it) {
  145. for (std::vector <
  146. std::pair < std::pair < int, int >,
  147. std::pair < int, int > > >::const_iterator it2 =
  148. it->begin(); it2 != it->end(); ++it2) {
  149. std::stringstream out_ss;
  150. std::stringstream in_ss;
  151. out_ss << "out_" << it2->first.second;
  152. if (not models[it2->first.first]->exist_out_port(out_ss.str())) {
  153. models[it2->first.first]->add_out_port(out_ss.str());
  154. }
  155. in_ss << "in_" << it2->second.second;
  156. if (not models[it2->second.first]->exist_in_port(in_ss.str())) {
  157. models[it2->second.first]->add_in_port(in_ss.str());
  158. }
  159. add_link(models[it2->first.first], out_ss.str(),
  160. models[it2->second.first], in_ss.str());
  161. }
  162. }
  163. }
  164. virtual ~RootGraphManager()
  165. {
  166. std::for_each(models.begin(), models.end(),
  167. std::default_delete < ModelProxy >());
  168. }
  169. private:
  170. typedef paradevs::pdevs::mpi::ModelProxy < common::DoubleTime > ModelProxy;
  171. typedef std::vector < ModelProxy* > ModelProxies;
  172. ModelProxies models;
  173. };
  174. } } } } // namespace paradevs tests mpi cluster
  175. #endif