main.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 <paradevs/kernel/pdevs/multithreading/Coordinator.hpp>
  33. #include <tests/mpi/cluster/graph_manager.hpp>
  34. #include <chrono>
  35. #include <fstream>
  36. using namespace paradevs::tests::mpi::cluster;
  37. using namespace paradevs::common;
  38. using namespace boost::mpi;
  39. using namespace std::chrono;
  40. std::vector < std::vector < int > > clusters;
  41. std::vector < std::vector < std::pair < int, int > > > graphs;
  42. std::vector < std::vector < std::pair < int, int > > > inputs;
  43. std::vector < std::vector < std::pair < int, int > > > outputs;
  44. std::vector < std::vector < std::pair <
  45. std::pair < int, int >,
  46. std::pair < int, int > > > > parents;
  47. void read_graphs(const std::string& path)
  48. {
  49. for (int index = 0; index < clusters.size(); ++index) {
  50. std::stringstream ss;
  51. ss << path << "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(const std::string& path)
  69. {
  70. for (int index = 0; index < clusters.size(); ++index) {
  71. std::stringstream ss;
  72. ss << path << "input_edges/input_edges_" << index << ".txt";
  73. std::ifstream inputsFile(ss.str());
  74. inputs.push_back(std::vector < std::pair < int, int > >());
  75. while (not inputsFile.eof()) {
  76. std::string str;
  77. std::vector < std::string > strs;
  78. std::getline(inputsFile, str);
  79. if (str.size() > 0) {
  80. boost::split(strs, str, boost::is_any_of(" "));
  81. inputs[inputs.size() - 1].
  82. push_back(std::make_pair(boost::lexical_cast < int >(strs[0]),
  83. boost::lexical_cast < int >(strs[1])));
  84. }
  85. }
  86. inputsFile.close();
  87. }
  88. }
  89. void read_outputs(const std::string& path)
  90. {
  91. for (int index = 0; index < clusters.size(); ++index) {
  92. std::stringstream ss;
  93. ss << path << "output_edges/output_edges_" << index << ".txt";
  94. std::ifstream outputsFile(ss.str());
  95. outputs.push_back(std::vector < std::pair < int, int > >());
  96. while (not outputsFile.eof()) {
  97. std::string str;
  98. std::vector < std::string > strs;
  99. std::getline(outputsFile, str);
  100. if (str.size() > 0) {
  101. boost::split(strs, str, boost::is_any_of(" "));
  102. outputs[outputs.size() - 1].
  103. push_back(std::make_pair(boost::lexical_cast < int >(strs[0]),
  104. boost::lexical_cast < int >(strs[1])));
  105. }
  106. }
  107. outputsFile.close();
  108. }
  109. }
  110. void read_parents(const std::string& path)
  111. {
  112. for (int index = 0; index < clusters.size(); ++index) {
  113. std::stringstream ss;
  114. ss << path << "parent_connection/parent_connection_" << index
  115. << ".txt";
  116. std::ifstream parentsFile(ss.str());
  117. parents.push_back(std::vector < std::pair < std::pair < int, int >,
  118. std::pair < int, int > > >());
  119. while (not parentsFile.eof()) {
  120. std::string str;
  121. std::vector < std::string > strs;
  122. std::getline(parentsFile, str);
  123. if (str.size() > 0) {
  124. boost::split(strs, str, boost::is_any_of(" "));
  125. parents[parents.size() - 1].
  126. push_back(std::make_pair(
  127. std::make_pair(boost::lexical_cast < int >(strs[0]),
  128. boost::lexical_cast < int >(strs[1])),
  129. std::make_pair(boost::lexical_cast < int >(strs[2]),
  130. boost::lexical_cast < int >(strs[3]))));
  131. }
  132. }
  133. parentsFile.close();
  134. }
  135. }
  136. void read(const std::string& path)
  137. {
  138. std::stringstream ss;
  139. ss << path << "partition.txt";
  140. std::ifstream clusterFile(ss.str());
  141. while (not clusterFile.eof()) {
  142. std::string str;
  143. std::vector < std::string > strs;
  144. std::getline(clusterFile, str);
  145. boost::split(strs, str, boost::is_any_of(" "));
  146. if (str.size() > 0) {
  147. clusters.push_back(std::vector < int >());
  148. for (std::vector < std::string >::const_iterator it = strs.begin();
  149. it != strs.end(); ++it) {
  150. if (it->size() > 0) {
  151. clusters[clusters.size() - 1].
  152. push_back(boost::lexical_cast < int >(*it));
  153. }
  154. }
  155. }
  156. }
  157. clusterFile.close();
  158. read_graphs(path);
  159. read_inputs(path);
  160. read_outputs(path);
  161. read_parents(path);
  162. }
  163. void example_simple(int argc, char *argv[])
  164. {
  165. environment env(argc, argv);
  166. communicator world;
  167. if (world.rank() == 0) {
  168. paradevs::tests::mpi::cluster::RootGraphManagerParameters parameters;
  169. parameters.parents = parents;
  170. paradevs::common::RootCoordinator <
  171. DoubleTime,
  172. paradevs::pdevs::multithreading::Coordinator <
  173. DoubleTime,
  174. paradevs::tests::mpi::cluster::RootGraphManager,
  175. paradevs::common::NoParameters,
  176. paradevs::tests::mpi::cluster::RootGraphManagerParameters >
  177. > rc(0, 100, "root", paradevs::common::NoParameters(), parameters);
  178. steady_clock::time_point t1 = steady_clock::now();
  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. }