main.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**
  2. * @file tests/mpi/cluster/main.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. #include <paradevs/common/RootCoordinator.hpp>
  26. #include <boost/mpi/environment.hpp>
  27. #include <boost/mpi/communicator.hpp>
  28. #include <boost/algorithm/string.hpp>
  29. #include <boost/lexical_cast.hpp>
  30. #include <paradevs/common/time/DoubleTime.hpp>
  31. #include <paradevs/kernel/pdevs/mpi/LogicalProcessor.hpp>
  32. #include <tests/mpi/cluster/graph_manager.hpp>
  33. #include <chrono>
  34. #include <fstream>
  35. using namespace paradevs::tests::mpi::cluster;
  36. using namespace paradevs::common;
  37. using namespace boost::mpi;
  38. using namespace std::chrono;
  39. std::vector < std::vector < int > > clusters;
  40. std::vector < std::vector < std::pair < int, int > > > graphs;
  41. std::vector < std::vector < std::pair < int, int > > > inputs;
  42. std::vector < std::vector < std::pair < int, int > > > outputs;
  43. std::vector < std::vector < std::pair <
  44. std::pair < int, int >,
  45. std::pair < int, int > > > > parents;
  46. void read_graphs(const std::string& path)
  47. {
  48. for (int index = 0; index < clusters.size(); ++index) {
  49. std::stringstream ss;
  50. ss << path << "graphe/graphe_" << index << ".txt";
  51. std::ifstream graphFile(ss.str());
  52. graphs.push_back(std::vector < std::pair < int, int > >());
  53. while (not graphFile.eof()) {
  54. std::string str;
  55. std::vector < std::string > strs;
  56. std::getline(graphFile, str);
  57. if (str.size() > 0) {
  58. boost::split(strs, str, boost::is_any_of(" "));
  59. graphs[graphs.size() - 1].
  60. push_back(std::make_pair(boost::lexical_cast < int >(strs[0]),
  61. boost::lexical_cast < int >(strs[1])));
  62. }
  63. }
  64. graphFile.close();
  65. }
  66. }
  67. void read_inputs(const std::string& path)
  68. {
  69. for (int index = 0; index < clusters.size(); ++index) {
  70. std::stringstream ss;
  71. ss << path << "input_edges/input_edges_" << index << ".txt";
  72. std::ifstream inputsFile(ss.str());
  73. inputs.push_back(std::vector < std::pair < int, int > >());
  74. while (not inputsFile.eof()) {
  75. std::string str;
  76. std::vector < std::string > strs;
  77. std::getline(inputsFile, str);
  78. if (str.size() > 0) {
  79. boost::split(strs, str, boost::is_any_of(" "));
  80. inputs[inputs.size() - 1].
  81. push_back(std::make_pair(boost::lexical_cast < int >(strs[0]),
  82. boost::lexical_cast < int >(strs[1])));
  83. }
  84. }
  85. inputsFile.close();
  86. }
  87. }
  88. void read_outputs(const std::string& path)
  89. {
  90. for (int index = 0; index < clusters.size(); ++index) {
  91. std::stringstream ss;
  92. ss << path << "output_edges/output_edges_" << index << ".txt";
  93. std::ifstream outputsFile(ss.str());
  94. outputs.push_back(std::vector < std::pair < int, int > >());
  95. while (not outputsFile.eof()) {
  96. std::string str;
  97. std::vector < std::string > strs;
  98. std::getline(outputsFile, str);
  99. if (str.size() > 0) {
  100. boost::split(strs, str, boost::is_any_of(" "));
  101. outputs[outputs.size() - 1].
  102. push_back(std::make_pair(boost::lexical_cast < int >(strs[0]),
  103. boost::lexical_cast < int >(strs[1])));
  104. }
  105. }
  106. outputsFile.close();
  107. }
  108. }
  109. void read_parents(const std::string& path)
  110. {
  111. for (int index = 0; index < clusters.size(); ++index) {
  112. std::stringstream ss;
  113. ss << path << "parent_connection/parent_connection_" << index
  114. << ".txt";
  115. std::ifstream parentsFile(ss.str());
  116. parents.push_back(std::vector < std::pair < std::pair < int, int >,
  117. std::pair < int, int > > >());
  118. while (not parentsFile.eof()) {
  119. std::string str;
  120. std::vector < std::string > strs;
  121. std::getline(parentsFile, str);
  122. if (str.size() > 0) {
  123. boost::split(strs, str, boost::is_any_of(" "));
  124. parents[parents.size() - 1].
  125. push_back(std::make_pair(
  126. std::make_pair(boost::lexical_cast < int >(strs[0]),
  127. boost::lexical_cast < int >(strs[1])),
  128. std::make_pair(boost::lexical_cast < int >(strs[2]),
  129. boost::lexical_cast < int >(strs[3]))));
  130. }
  131. }
  132. parentsFile.close();
  133. }
  134. }
  135. void read(const std::string& path)
  136. {
  137. std::stringstream ss;
  138. ss << path << "partition.txt";
  139. std::ifstream clusterFile(ss.str());
  140. while (not clusterFile.eof()) {
  141. std::string str;
  142. std::vector < std::string > strs;
  143. std::getline(clusterFile, str);
  144. boost::split(strs, str, boost::is_any_of(" "));
  145. if (str.size() > 0) {
  146. clusters.push_back(std::vector < int >());
  147. for (std::vector < std::string >::const_iterator it = strs.begin();
  148. it != strs.end(); ++it) {
  149. if (it->size() > 0) {
  150. clusters[clusters.size() - 1].
  151. push_back(boost::lexical_cast < int >(*it));
  152. }
  153. }
  154. }
  155. }
  156. clusterFile.close();
  157. read_graphs(path);
  158. read_inputs(path);
  159. read_outputs(path);
  160. read_parents(path);
  161. }
  162. void example_simple(int argc, char *argv[])
  163. {
  164. environment env(argc, argv);
  165. communicator world;
  166. if (world.rank() == 0) {
  167. paradevs::tests::mpi::cluster::RootGraphManagerParameters parameters;
  168. parameters.parents = parents;
  169. paradevs::common::RootCoordinator <
  170. DoubleTime,
  171. paradevs::pdevs::Coordinator <
  172. DoubleTime,
  173. paradevs::tests::mpi::cluster::RootGraphManager,
  174. paradevs::common::NoParameters,
  175. paradevs::tests::mpi::cluster::RootGraphManagerParameters >
  176. > rc(0, 100, "root", paradevs::common::NoParameters(), parameters);
  177. steady_clock::time_point t1 = steady_clock::now();
  178. std::cout << "START" << std::endl;
  179. rc.run();
  180. steady_clock::time_point t2 = steady_clock::now();
  181. duration < double > time_span = duration_cast <
  182. duration < double > >(t2 - t1);
  183. std::cout << "time = " << time_span.count() << std::endl;
  184. } else {
  185. paradevs::tests::mpi::cluster::SubGraphManagerParameters parameters;
  186. std::stringstream ss;
  187. parameters.indexes = clusters[world.rank() - 1];
  188. parameters.inputs = inputs[world.rank() - 1];
  189. parameters.outputs = outputs[world.rank() - 1];
  190. parameters.internals = graphs[world.rank() - 1];
  191. ss << "S" << world.rank();
  192. paradevs::pdevs::mpi::Coordinator <
  193. DoubleTime,
  194. paradevs::tests::mpi::cluster::SubGraphManager,
  195. paradevs::common::NoParameters,
  196. paradevs::tests::mpi::cluster::SubGraphManagerParameters >
  197. model(ss.str(), paradevs::common::NoParameters(), parameters);
  198. paradevs::pdevs::mpi::LogicalProcessor <
  199. DoubleTime > LP(&model, world.rank(), 0);
  200. model.set_logical_processor(&LP);
  201. LP.loop();
  202. }
  203. }
  204. int main(int argc, char *argv[])
  205. {
  206. if (argc == 2) {
  207. read(argv[1]);
  208. example_simple(argc, argv);
  209. return 0;
  210. } else {
  211. return -1;
  212. }
  213. }