main.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. #include <iostream>
  30. using namespace artis::common;
  31. using namespace std::chrono;
  32. using namespace artis::tests::multithreading::lifegame;
  33. CellParameters parameters({
  34. {
  35. {"C_1_1", 3},
  36. {"C_1_2", 5},
  37. {"C_1_3", 5},
  38. {"C_1_4", 5},
  39. {"C_1_5", 5},
  40. {"C_1_6", 5},
  41. {"C_1_7", 5},
  42. {"C_1_8", 5},
  43. {"C_1_9", 5},
  44. {"C_1_10", 3},
  45. {"C_2_1", 5},
  46. {"C_2_2", 8},
  47. {"C_2_3", 8},
  48. {"C_2_4", 8},
  49. {"C_2_5", 8},
  50. {"C_2_6", 8},
  51. {"C_2_7", 8},
  52. {"C_2_8", 8},
  53. {"C_2_9", 8},
  54. {"C_2_10", 5},
  55. {"C_3_1", 5},
  56. {"C_3_2", 8},
  57. {"C_3_3", 8},
  58. {"C_3_4", 8},
  59. {"C_3_5", 8},
  60. {"C_3_6", 8},
  61. {"C_3_7", 8},
  62. {"C_3_8", 8},
  63. {"C_3_9", 8},
  64. {"C_3_10", 5},
  65. {"C_4_1", 5},
  66. {"C_4_2", 8},
  67. {"C_4_3", 8},
  68. {"C_4_4", 8},
  69. {"C_4_5", 8},
  70. {"C_4_6", 8},
  71. {"C_4_7", 8},
  72. {"C_4_8", 8},
  73. {"C_4_9", 8},
  74. {"C_4_10", 5},
  75. {"C_5_1", 5},
  76. {"C_5_2", 8},
  77. {"C_5_3", 8},
  78. {"C_5_4", 8},
  79. {"C_5_5", 8},
  80. {"C_5_6", 8},
  81. {"C_5_7", 8},
  82. {"C_5_8", 8},
  83. {"C_5_9", 8},
  84. {"C_5_10", 5},
  85. {"C_6_1", 5},
  86. {"C_6_2", 8},
  87. {"C_6_3", 8},
  88. {"C_6_4", 8},
  89. {"C_6_5", 8},
  90. {"C_6_6", 8},
  91. {"C_6_7", 8},
  92. {"C_6_8", 8},
  93. {"C_6_9", 8},
  94. {"C_6_10", 5},
  95. {"C_7_1", 5},
  96. {"C_7_2", 8},
  97. {"C_7_3", 8},
  98. {"C_7_4", 8},
  99. {"C_7_5", 8},
  100. {"C_7_6", 8},
  101. {"C_7_7", 8},
  102. {"C_7_8", 8},
  103. {"C_7_9", 8},
  104. {"C_7_10", 5},
  105. {"C_8_1", 5},
  106. {"C_8_2", 8},
  107. {"C_8_3", 8},
  108. {"C_8_4", 8},
  109. {"C_8_5", 8},
  110. {"C_8_6", 8},
  111. {"C_8_7", 8},
  112. {"C_8_8", 8},
  113. {"C_8_9", 8},
  114. {"C_8_10", 5},
  115. {"C_9_1", 5},
  116. {"C_9_2", 8},
  117. {"C_9_3", 8},
  118. {"C_9_4", 8},
  119. {"C_9_5", 8},
  120. {"C_9_6", 8},
  121. {"C_9_7", 8},
  122. {"C_9_8", 8},
  123. {"C_9_9", 8},
  124. {"C_9_10", 5},
  125. {"C_10_1", 3},
  126. {"C_10_2", 5},
  127. {"C_10_3", 5},
  128. {"C_10_4", 5},
  129. {"C_10_5", 5},
  130. {"C_10_6", 5},
  131. {"C_10_7", 5},
  132. {"C_10_8", 5},
  133. {"C_10_9", 5},
  134. {"C_10_10", 3}
  135. },
  136. {
  137. {"C_1_1", false},
  138. {"C_1_2", false},
  139. {"C_1_3", false},
  140. {"C_1_4", false},
  141. {"C_1_5", false},
  142. {"C_1_6", false},
  143. {"C_1_7", false},
  144. {"C_1_8", false},
  145. {"C_1_9", false},
  146. {"C_1_10", false},
  147. {"C_2_1", false},
  148. {"C_2_2", false},
  149. {"C_2_3", false},
  150. {"C_2_4", false},
  151. {"C_2_5", true},
  152. {"C_2_6", true},
  153. {"C_2_7", false},
  154. {"C_2_8", false},
  155. {"C_2_9", false},
  156. {"C_2_10", false},
  157. {"C_3_1", false},
  158. {"C_3_2", false},
  159. {"C_3_3", false},
  160. {"C_3_4", true},
  161. {"C_3_5", false},
  162. {"C_3_6", false},
  163. {"C_3_7", true},
  164. {"C_3_8", false},
  165. {"C_3_9", false},
  166. {"C_3_10", false},
  167. {"C_4_1", false},
  168. {"C_4_2", false},
  169. {"C_4_3", true},
  170. {"C_4_4", false},
  171. {"C_4_5", false},
  172. {"C_4_6", false},
  173. {"C_4_7", false},
  174. {"C_4_8", true},
  175. {"C_4_9", false},
  176. {"C_4_10", false},
  177. {"C_5_1", false},
  178. {"C_5_2", true},
  179. {"C_5_3", false},
  180. {"C_5_4", false},
  181. {"C_5_5", false},
  182. {"C_5_6", false},
  183. {"C_5_7", false},
  184. {"C_5_8", false},
  185. {"C_5_9", true},
  186. {"C_5_10", false},
  187. {"C_6_1", false},
  188. {"C_6_2", true},
  189. {"C_6_3", false},
  190. {"C_6_4", false},
  191. {"C_6_5", false},
  192. {"C_6_6", false},
  193. {"C_6_7", false},
  194. {"C_6_8", false},
  195. {"C_6_9", true},
  196. {"C_6_10", false},
  197. {"C_7_1", false},
  198. {"C_7_2", false},
  199. {"C_7_3", true},
  200. {"C_7_4", false},
  201. {"C_7_5", false},
  202. {"C_7_6", false},
  203. {"C_7_7", false},
  204. {"C_7_8", true},
  205. {"C_7_9", false},
  206. {"C_7_10", false},
  207. {"C_8_1", false},
  208. {"C_8_2", false},
  209. {"C_8_3", false},
  210. {"C_8_4", true},
  211. {"C_8_5", false},
  212. {"C_8_6", false},
  213. {"C_8_7", true},
  214. {"C_8_8", false},
  215. {"C_8_9", false},
  216. {"C_8_10", false},
  217. {"C_9_1", false},
  218. {"C_9_2", false},
  219. {"C_9_3", false},
  220. {"C_9_4", false},
  221. {"C_9_5", true},
  222. {"C_9_6", true},
  223. {"C_9_7", false},
  224. {"C_9_8", false},
  225. {"C_9_9", false},
  226. {"C_9_10", false},
  227. {"C_10_1", false},
  228. {"C_10_2", false},
  229. {"C_10_3", false},
  230. {"C_10_4", false},
  231. {"C_10_5", false},
  232. {"C_10_6", false},
  233. {"C_10_7", false},
  234. {"C_10_8", false},
  235. {"C_10_9", false},
  236. {"C_10_10", false}
  237. }
  238. });
  239. GridGraphManagerParameters graph_parameters({1, 10, 1, 10, 1, 10, 1, 10});
  240. class View : public artis::observer::View<artis::common::DoubleTime>
  241. {
  242. public:
  243. View()
  244. {
  245. selector("Cell:state", {FlatGraphManager::CELL, ALL, Cell::STATE});
  246. }
  247. };
  248. void show_state(const artis::observer::View<artis::common::DoubleTime> &view)
  249. {
  250. std::vector<std::vector<std::vector<bool> > > states;
  251. for (unsigned int t = 0; t <= 11; ++t) {
  252. states.emplace_back(std::vector<std::vector<bool> >());
  253. for (unsigned int i = graph_parameters.begin_column - 1; i < graph_parameters.end_column; ++i) {
  254. states[t].emplace_back(std::vector<bool>());
  255. for (unsigned int j = graph_parameters.begin_line - 1; j < graph_parameters.end_line; ++j) {
  256. states[t][i].emplace_back(false);
  257. }
  258. }
  259. }
  260. for (unsigned int i = graph_parameters.begin_column - 1; i < graph_parameters.end_column; ++i) {
  261. for (unsigned int j = graph_parameters.begin_line - 1; j < graph_parameters.end_line; ++j) {
  262. std::ostringstream ss;
  263. ss << ":root:C_" << (i + 1) << "_" << (j + 1) << ":state";
  264. const ::View::Values &values = view.get("Cell:state", ss.str());
  265. for (const auto &value: values) {
  266. bool state;
  267. value.second(state);
  268. if (value.first < 11) {
  269. states[(int) value.first][i][j] = state;
  270. }
  271. }
  272. }
  273. }
  274. for (unsigned int t = 0; t <= 10; ++t) {
  275. for (unsigned int i = graph_parameters.begin_column - 1; i < graph_parameters.end_column; ++i) {
  276. for (unsigned int j = graph_parameters.begin_line - 1; j < graph_parameters.end_line; ++j) {
  277. std::cout << states[t][i][j] << " ";
  278. }
  279. std::cout << std::endl;
  280. }
  281. std::cout << std::endl;
  282. }
  283. }
  284. double lifegame_monothreading()
  285. {
  286. artis::common::context::Context<artis::common::DoubleTime> context(0, 10);
  287. artis::common::RootCoordinator<
  288. DoubleTime, artis::pdevs::Coordinator<
  289. DoubleTime,
  290. FlatGraphManager,
  291. CellParameters,
  292. GridGraphManagerParameters>
  293. > rc(context, "root", parameters, graph_parameters);
  294. rc.attachView("Matrix", new ::View());
  295. steady_clock::time_point t1 = steady_clock::now();
  296. rc.run(context);
  297. steady_clock::time_point t2 = steady_clock::now();
  298. duration<double> time_span = duration_cast<duration<double> >(t2 - t1);
  299. show_state(rc.observer().view("Matrix"));
  300. return time_span.count();
  301. }
  302. double lifegame_multithreading()
  303. {
  304. artis::common::context::Context<artis::common::DoubleTime> context(0, 10);
  305. artis::common::RootCoordinator<
  306. DoubleTime, artis::pdevs::multithreading::Coordinator<
  307. DoubleTime,
  308. ParallelHierarchicalGraphManager,
  309. CellParameters,
  310. GridGraphManagerParameters>
  311. > rc(context, "root", parameters, graph_parameters);
  312. steady_clock::time_point t1 = steady_clock::now();
  313. rc.run(context);
  314. steady_clock::time_point t2 = steady_clock::now();
  315. duration<double> time_span = duration_cast<duration<double> >(t2 - t1);
  316. return time_span.count();
  317. }
  318. int main()
  319. {
  320. // std::cout << lifegame_monothreading() << std::endl;
  321. std::cout << lifegame_multithreading() << std::endl;
  322. return 0;
  323. }