main.cpp 7.4 KB

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