main.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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-2016 ULCO http://www.univ-littoral.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 < std::pair < int, 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 (unsigned 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(
  62. boost::lexical_cast < int >(strs[0]),
  63. boost::lexical_cast < int >(strs[1])));
  64. }
  65. }
  66. graphFile.close();
  67. }
  68. }
  69. void read_inputs(const std::string& path)
  70. {
  71. for (unsigned int index = 0; index < clusters.size(); ++index) {
  72. std::stringstream ss;
  73. ss << path << "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(
  84. boost::lexical_cast < int >(strs[0]),
  85. boost::lexical_cast < int >(strs[1])));
  86. }
  87. }
  88. inputsFile.close();
  89. }
  90. }
  91. void read_outputs(const std::string& path)
  92. {
  93. for (unsigned int index = 0; index < clusters.size(); ++index) {
  94. std::stringstream ss;
  95. ss << path << "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(
  106. boost::lexical_cast < int >(strs[0]),
  107. boost::lexical_cast < int >(strs[1])));
  108. }
  109. }
  110. outputsFile.close();
  111. }
  112. }
  113. void read_parents(const std::string& path)
  114. {
  115. for (unsigned int index = 0; index < clusters.size(); ++index) {
  116. std::stringstream ss;
  117. ss << path << "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(
  130. std::make_pair(
  131. std::make_pair(
  132. boost::lexical_cast < int >(strs[0]),
  133. boost::lexical_cast < int >(strs[1])),
  134. std::make_pair(
  135. boost::lexical_cast < int >(strs[2]),
  136. boost::lexical_cast < int >(strs[3]))));
  137. }
  138. }
  139. parentsFile.close();
  140. }
  141. }
  142. void read(const std::string& path)
  143. {
  144. std::stringstream ss;
  145. ss << path << "partition.txt";
  146. std::ifstream clusterFile(ss.str());
  147. while (not clusterFile.eof()) {
  148. std::string str;
  149. std::vector < std::string > strs;
  150. std::getline(clusterFile, str);
  151. boost::split(strs, str, boost::is_any_of(" "));
  152. if (str.size() > 0) {
  153. clusters.push_back(std::vector < std::pair < int, int > >());
  154. for (std::vector < std::string >::const_iterator it = strs.begin();
  155. it != strs.end();) {
  156. if (it->size() > 0) {
  157. int index = boost::lexical_cast < int >(*it);
  158. ++it;
  159. clusters[clusters.size() - 1].
  160. push_back(std::make_pair(
  161. index, boost::lexical_cast < int >(*it)));
  162. }
  163. ++it;
  164. }
  165. }
  166. }
  167. clusterFile.close();
  168. read_graphs(path);
  169. read_inputs(path);
  170. read_outputs(path);
  171. read_parents(path);
  172. }
  173. void example_simple(int argc, char *argv[])
  174. {
  175. environment env(argc, argv);
  176. communicator world;
  177. if (world.rank() == 0) {
  178. paradevs::tests::mpi::cluster::RootGraphManagerParameters parameters;
  179. parameters.parents = parents;
  180. paradevs::common::RootCoordinator <
  181. DoubleTime,
  182. paradevs::pdevs::multithreading::Coordinator <
  183. DoubleTime,
  184. paradevs::tests::mpi::cluster::RootGraphManager,
  185. paradevs::common::NoParameters,
  186. paradevs::tests::mpi::cluster::RootGraphManagerParameters >
  187. > rc(0, 100, "root", paradevs::common::NoParameters(), parameters);
  188. steady_clock::time_point t1 = steady_clock::now();
  189. rc.run();
  190. steady_clock::time_point t2 = steady_clock::now();
  191. duration < double > time_span = duration_cast <
  192. duration < double > >(t2 - t1);
  193. std::cout << "time = " << time_span.count() << std::endl;
  194. } else {
  195. paradevs::tests::mpi::cluster::SubGraphManagerParameters parameters;
  196. std::stringstream ss;
  197. parameters.indexes = clusters[world.rank() - 1];
  198. parameters.inputs = inputs[world.rank() - 1];
  199. parameters.outputs = outputs[world.rank() - 1];
  200. parameters.internals = graphs[world.rank() - 1];
  201. ss << "S" << world.rank();
  202. paradevs::pdevs::mpi::Coordinator <
  203. DoubleTime,
  204. paradevs::tests::mpi::cluster::SubGraphManager,
  205. paradevs::common::NoParameters,
  206. paradevs::tests::mpi::cluster::SubGraphManagerParameters >
  207. model(ss.str(), paradevs::common::NoParameters(), parameters);
  208. paradevs::pdevs::mpi::LogicalProcessor <
  209. DoubleTime > LP(&model, world.rank(), 0);
  210. model.set_logical_processor(&LP);
  211. LP.loop();
  212. }
  213. }
  214. int main(int argc, char *argv[])
  215. {
  216. if (argc == 2) {
  217. read(argv[1]);
  218. example_simple(argc, argv);
  219. return 0;
  220. } else {
  221. return -1;
  222. }
  223. }