main.cpp 9.6 KB

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