main.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**
  2. * @file tests/multithreading/lifegame/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 <tests/multithreading/lifegame/graph_manager.hpp>
  27. #include <tests/multithreading/lifegame/models.hpp>
  28. #include <chrono>
  29. using namespace artis::common;
  30. using namespace std::chrono;
  31. using namespace artis::tests::multithreading::lifegame;
  32. class View : public artis::observer::View<artis::common::DoubleTime> {
  33. public:
  34. View()
  35. {
  36. selector("Cell:state", {FlatGraphManager::CELL, ALL, Cell::STATE});
  37. }
  38. };
  39. void show_state(const GridGraphManagerParameters& graph_parameters,
  40. const artis::observer::View<artis::common::DoubleTime>& view)
  41. {
  42. std::vector<std::vector<std::vector<bool> > > states;
  43. for (unsigned int t = 0; t <= 11; ++t) {
  44. states.emplace_back(std::vector<std::vector<bool> >());
  45. for (unsigned int i = 0; i < graph_parameters.column_number; ++i) {
  46. states[t].emplace_back(std::vector<bool>());
  47. for (unsigned int j = 0; j < graph_parameters.line_number; ++j) {
  48. states[t][i].emplace_back(false);
  49. }
  50. }
  51. }
  52. for (unsigned int i = 0; i < graph_parameters.column_number; ++i) {
  53. for (unsigned int j = 0; j < graph_parameters.line_number; ++j) {
  54. std::ostringstream ss;
  55. ss << ":root:C_" << (i + 1) << "_" << (j + 1) << ":state";
  56. const ::View::Values& values = view.get("Cell:state", ss.str());
  57. for (const auto& value: values) {
  58. bool state;
  59. value.second(state);
  60. states[(int)value.first][i][j] = state;
  61. }
  62. }
  63. }
  64. for (unsigned int t = 0; t <= 10; ++t) {
  65. for (unsigned int i = 0; i < graph_parameters.column_number; ++i) {
  66. for (unsigned int j = 0; j < graph_parameters.line_number; ++j) {
  67. std::cout << states[t][i][j] << " ";
  68. }
  69. std::cout << std::endl;
  70. }
  71. std::cout << std::endl;
  72. }
  73. }
  74. double lifegame_monothreading()
  75. {
  76. CellParameters parameters({
  77. {
  78. {"C_1_1", 2},
  79. {"C_1_2", 3},
  80. {"C_1_3", 3},
  81. {"C_1_4", 3},
  82. {"C_1_5", 2},
  83. {"C_2_1", 3},
  84. {"C_2_2", 4},
  85. {"C_2_3", 4},
  86. {"C_2_4", 4},
  87. {"C_2_5", 3},
  88. {"C_3_1", 3},
  89. {"C_3_2", 4},
  90. {"C_3_3", 4},
  91. {"C_3_4", 4},
  92. {"C_3_5", 3},
  93. {"C_4_1", 3},
  94. {"C_4_2", 4},
  95. {"C_4_3", 4},
  96. {"C_4_4", 4},
  97. {"C_4_5", 3},
  98. {"C_5_1", 2},
  99. {"C_5_2", 3},
  100. {"C_5_3", 3},
  101. {"C_5_4", 3},
  102. {"C_5_5", 2},
  103. },
  104. {
  105. {"C_1_1", false},
  106. {"C_1_2", false},
  107. {"C_1_3", false},
  108. {"C_1_4", false},
  109. {"C_1_5", false},
  110. {"C_2_1", false},
  111. {"C_2_2", false},
  112. {"C_2_3", true},
  113. {"C_2_4", false},
  114. {"C_2_5", false},
  115. {"C_3_1", false},
  116. {"C_3_2", false},
  117. {"C_3_3", true},
  118. {"C_3_4", false},
  119. {"C_3_5", false},
  120. {"C_4_1", false},
  121. {"C_4_2", false},
  122. {"C_4_3", true},
  123. {"C_4_4", false},
  124. {"C_4_5", false},
  125. {"C_5_1", false},
  126. {"C_5_2", false},
  127. {"C_5_3", false},
  128. {"C_5_4", false},
  129. {"C_5_5", false},
  130. }
  131. });
  132. GridGraphManagerParameters graph_parameters({5, 5});
  133. artis::common::RootCoordinator<
  134. DoubleTime, artis::pdevs::Coordinator<
  135. DoubleTime,
  136. FlatGraphManager,
  137. CellParameters,
  138. GridGraphManagerParameters>
  139. > rc(0, 10, "root", parameters, graph_parameters);
  140. rc.attachView("Matrix", new ::View());
  141. steady_clock::time_point t1 = steady_clock::now();
  142. rc.run();
  143. steady_clock::time_point t2 = steady_clock::now();
  144. duration<double> time_span = duration_cast<duration<double> >(t2 - t1);
  145. show_state(graph_parameters, rc.observer().view("Matrix"));
  146. return time_span.count();
  147. }
  148. double lifegame_multithreading()
  149. {
  150. // artis::common::RootCoordinator<
  151. // DoubleTime, artis::pdevs::multithreading::Coordinator<
  152. // DoubleTime,
  153. // ParallelHierarchicalGraphManager,
  154. // artis::common::NoParameters,
  155. // GraphManagerParameters>
  156. // > rc(0, 10, "root", artis::common::NoParameters(), GraphManagerParameters(cluster_number));
  157. steady_clock::time_point t1 = steady_clock::now();
  158. // rc.run();
  159. steady_clock::time_point t2 = steady_clock::now();
  160. duration<double> time_span = duration_cast<duration<double> >(t2 - t1);
  161. return time_span.count();
  162. }
  163. int main()
  164. {
  165. std::cout << lifegame_monothreading() << std::endl;
  166. std::cout << lifegame_multithreading() << std::endl;
  167. return 0;
  168. }