main.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /**
  2. * @file tests/mpi/main.cpp
  3. * @author The ARTIS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * ARTIS - the multimodeling and simulation environment
  8. * This file is a part of the ARTIS environment
  9. *
  10. * Copyright (C) 2013-2019 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 <artis-star/common/RootCoordinator.hpp>
  26. #include <boost/mpi/environment.hpp>
  27. #include <boost/mpi/communicator.hpp>
  28. #include <artis-star/common/time/DoubleTime.hpp>
  29. #include <artis-star/kernel/pdevs/mpi/LogicalProcessor.hpp>
  30. #include <artis-star/kernel/pdevs/multithreading/Coordinator.hpp>
  31. #include <tests/mpi/graph_manager.hpp>
  32. #include <chrono>
  33. using namespace artis::tests::mpi;
  34. using namespace artis::common;
  35. using namespace boost::mpi;
  36. using namespace std::chrono;
  37. void example_simple(int argc, char *argv[])
  38. {
  39. environment env(argc, argv);
  40. communicator world;
  41. std::cout << "START " << world.rank() << std::endl;
  42. if (world.rank() == 0) {
  43. artis::tests::mpi::RootGraphManagerParameters parameters;
  44. parameters.ranks.push_back(1);
  45. parameters.ranks.push_back(2);
  46. parameters.ranks.push_back(3);
  47. // parameters.ranks.push_back(4);
  48. // parameters.ranks.push_back(5);
  49. // parameters.ranks.push_back(6);
  50. // parameters.ranks.push_back(7);
  51. // parameters.ranks.push_back(8);
  52. artis::common::context::Context<artis::common::DoubleTime> context(0, 1000);
  53. artis::common::RootCoordinator<
  54. DoubleTime,
  55. artis::pdevs::Coordinator<
  56. DoubleTime,
  57. artis::tests::mpi::RootGraphManager,
  58. artis::common::NoParameters,
  59. artis::tests::mpi::RootGraphManagerParameters>
  60. > rc(context, "root", artis::common::NoParameters(), parameters);
  61. steady_clock::time_point t1 = steady_clock::now();
  62. rc.run(context);
  63. steady_clock::time_point t2 = steady_clock::now();
  64. duration<double> time_span = duration_cast<duration<double> >(t2 - t1);
  65. std::cout << "CHAIN = " << time_span.count() << std::endl;
  66. } else {
  67. std::stringstream ss;
  68. ss << "S" << world.rank();
  69. if (world.rank() == 1) {
  70. artis::pdevs::mpi::Coordinator<
  71. DoubleTime,
  72. artis::tests::mpi::S1GraphManager> model(ss.str(), artis::common::NoParameters(),
  73. artis::common::NoParameters());
  74. artis::pdevs::mpi::LogicalProcessor<DoubleTime> LP(&model, world.rank(), 0);
  75. model.set_logical_processor(&LP);
  76. LP.loop();
  77. } else {
  78. artis::pdevs::mpi::Coordinator<
  79. DoubleTime,
  80. artis::tests::mpi::S2GraphManager> model(ss.str(), artis::common::NoParameters(),
  81. artis::common::NoParameters());
  82. artis::pdevs::mpi::LogicalProcessor<DoubleTime> LP(&model, world.rank(), 0);
  83. model.set_logical_processor(&LP);
  84. LP.loop();
  85. }
  86. }
  87. }
  88. void example_simple_local()
  89. {
  90. artis::tests::mpi::RootGraphManagerParameters parameters;
  91. parameters.ranks.push_back(1);
  92. parameters.ranks.push_back(2);
  93. parameters.ranks.push_back(3);
  94. // parameters.ranks.push_back(4);
  95. // parameters.ranks.push_back(5);
  96. // parameters.ranks.push_back(6);
  97. // parameters.ranks.push_back(7);
  98. // parameters.ranks.push_back(8);
  99. artis::common::context::Context<artis::common::DoubleTime> context(0, 1000);
  100. artis::common::RootCoordinator<
  101. DoubleTime,
  102. artis::pdevs::Coordinator<
  103. DoubleTime,
  104. artis::tests::mpi::RootLocalGraphManager,
  105. artis::common::NoParameters,
  106. artis::tests::mpi::RootGraphManagerParameters>
  107. > rc(context, "root", artis::common::NoParameters(), parameters);
  108. steady_clock::time_point t1 = steady_clock::now();
  109. rc.run(context);
  110. steady_clock::time_point t2 = steady_clock::now();
  111. duration<double> time_span = duration_cast<duration<double> >(t2 - t1);
  112. std::cout << "CHAIN = " << time_span.count() << std::endl;
  113. }
  114. void example_simple_multithreading()
  115. {
  116. artis::tests::mpi::RootGraphManagerParameters parameters;
  117. parameters.ranks.push_back(1);
  118. parameters.ranks.push_back(2);
  119. parameters.ranks.push_back(3);
  120. // parameters.ranks.push_back(4);
  121. // parameters.ranks.push_back(5);
  122. // parameters.ranks.push_back(6);
  123. // parameters.ranks.push_back(7);
  124. // parameters.ranks.push_back(8);
  125. artis::common::context::Context<artis::common::DoubleTime> context(0, 1000);
  126. artis::common::RootCoordinator<
  127. DoubleTime,
  128. artis::pdevs::multithreading::Coordinator<
  129. DoubleTime,
  130. artis::tests::mpi::RootMultithreadingGraphManager,
  131. artis::common::NoParameters,
  132. artis::tests::mpi::RootGraphManagerParameters>
  133. > rc(context, "root", artis::common::NoParameters(), parameters);
  134. steady_clock::time_point t1 = steady_clock::now();
  135. rc.run(context);
  136. steady_clock::time_point t2 = steady_clock::now();
  137. duration<double> time_span = duration_cast<duration<double> >(t2 - t1);
  138. std::cout << "CHAIN = " << time_span.count() << std::endl;
  139. }
  140. //#define SUB_GRID_SIZE 32
  141. //#define SUB_GRID_NUMBER 8
  142. //
  143. //void example_grid(int argc, char* argv[])
  144. //{
  145. // environment env(argc, argv);
  146. // communicator world;
  147. //
  148. // if (world.rank() == 0) {
  149. // artis::tests::mpi::GridGraphManagerParameters parameters;
  150. //
  151. // parameters.global_size = SUB_GRID_SIZE * SUB_GRID_NUMBER;
  152. // parameters.sub_grid_size = SUB_GRID_SIZE;
  153. //
  154. // artis::common::RootCoordinator<
  155. // DoubleTime,
  156. // artis::pdevs::Coordinator<
  157. // DoubleTime,
  158. // artis::tests::mpi::GridGraphManager,
  159. // artis::common::NoParameters,
  160. // artis::tests::mpi::GridGraphManagerParameters>
  161. // > rc(0, 20, "root", artis::common::NoParameters(), parameters);
  162. //
  163. // rc.run(context);
  164. // } else {
  165. // std::stringstream ss;
  166. // unsigned int x = (world.rank() - 1) % SUB_GRID_NUMBER;
  167. // unsigned int y = (world.rank() - 1) / SUB_GRID_NUMBER;
  168. // artis::tests::mpi::SubGridGraphManagerParameters parameters;
  169. //
  170. // ss << "GC_" << x << "_" << y;
  171. //
  172. // parameters.global_size = SUB_GRID_SIZE * SUB_GRID_NUMBER;
  173. // parameters.begin_column = x * SUB_GRID_SIZE;
  174. // parameters.end_column = (x + 1) * SUB_GRID_SIZE - 1;
  175. // parameters.begin_line = y * SUB_GRID_SIZE;
  176. // parameters.end_line = (y + 1) * SUB_GRID_SIZE - 1;
  177. // artis::pdevs::mpi::Coordinator<
  178. // DoubleTime,
  179. // artis::tests::mpi::SubGridGraphManager,
  180. // artis::common::NoParameters,
  181. // artis::tests::mpi::SubGridGraphManagerParameters
  182. // > model(ss.str(), artis::common::NoParameters(), parameters);
  183. // artis::pdevs::mpi::LogicalProcessor<
  184. // DoubleTime> LP(&model, world.rank(), 0);
  185. //
  186. // model.set_logical_processor(&LP);
  187. // LP.loop();
  188. // }
  189. //}
  190. // void example_grid(int argc, char *argv[])
  191. // {
  192. // environment env(argc, argv);
  193. // communicator world;
  194. // unsigned int side = 100;
  195. // std::vector<std::pair<int,int>> vertex_selection;
  196. // std::pair<int,int> tmp;
  197. // tmp.first = 0;
  198. // tmp.second = 3;
  199. // vertex_selection.push_back(tmp);
  200. // artis::tests::boost_graph::Entiers weight_vertex;
  201. // weight_vertex.push_back(1);
  202. // const char *edge_weight;
  203. // edge_weight = "../../sortie_graphe/tests_grid.txt";
  204. // bool rec = false;
  205. // artis::tests::boost_graph::RandomGridGraphGenerator generator(
  206. // side, vertex_selection, weight_vertex, edge_weight, rec);
  207. // artis::tests::boost_graph::PartitioningGraphBuilder builder(
  208. // 4, "gggp", 20, false, generator);
  209. // artis::tests::mpi::MPIHierarchicalGraphManagerParameters parameters;
  210. // builder.build(parameters.graphs, parameters.input_edges,
  211. // parameters.output_edges, parameters.parent_connections);
  212. // if (world.rank() == 0) {
  213. // artis::common::RootCoordinator <
  214. // DoubleTime, artis::pdevs::mpi::Coordinator <
  215. // DoubleTime,
  216. // artis::tests::mpi::MPIHierarchicalGraphManager,
  217. // artis::common::NoParameters,
  218. // artis::tests::mpi::MPIHierarchicalGraphManagerParameters >
  219. // > rc(0, 10, "root", artis::common::NoParameters(), parameters);
  220. // steady_clock::time_point t1 = steady_clock::now();
  221. // rc.run(context);
  222. // steady_clock::time_point t2 = steady_clock::now();
  223. // duration < double > time_span = duration_cast <
  224. // duration < double > >(t2 - t1);
  225. // std::cout << "MULTI = " << time_span.count() << std::endl;
  226. // } else {
  227. // }
  228. // }
  229. // in output directory:
  230. // $ mpirun -np 4 -hostfile host_file ../cmake-build-debug/src/tests/mpi/pdevs-mpi-main
  231. int main(int argc, char *argv[])
  232. {
  233. std::cout << "== MPI ==" << std::endl;
  234. example_simple(argc, argv);
  235. std::cout << "== Multithreading ==" << std::endl;
  236. example_simple_multithreading();
  237. std::cout << "== Monothreading ==" << std::endl;
  238. example_simple_local();
  239. // example_grid(argc, argv);
  240. return 0;
  241. }