graph_manager.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * @file tests/multithreading/lifegame/graph_manager.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. #ifndef TESTS_MULTITHREADING_LIFEGAME_GRAPH_MANAGER_HPP
  26. #define TESTS_MULTITHREADING_LIFEGAME_GRAPH_MANAGER_HPP 1
  27. #include <tests/multithreading/lifegame/models.hpp>
  28. #include <artis-star/kernel/pdevs/multithreading/Coordinator.hpp>
  29. #include <artis-star/kernel/pdevs/GraphManager.hpp>
  30. #include <artis-star/kernel/pdevs/Simulator.hpp>
  31. namespace artis {
  32. namespace tests {
  33. namespace multithreading {
  34. namespace lifegame {
  35. struct GridGraphManagerParameters {
  36. unsigned int column_number;
  37. unsigned int line_number;
  38. };
  39. class FlatGraphManager
  40. : public artis::pdevs::GraphManager<common::DoubleTime, CellParameters, GridGraphManagerParameters> {
  41. public:
  42. enum submodels {
  43. CELL
  44. };
  45. FlatGraphManager(common::Coordinator<common::DoubleTime>* coordinator,
  46. const CellParameters& parameters,
  47. const GridGraphManagerParameters& graph_parameters)
  48. :
  49. artis::pdevs::GraphManager<common::DoubleTime, CellParameters, GridGraphManagerParameters>(
  50. coordinator, parameters, graph_parameters)
  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 << "C_" << (i + 1) << "_" << (j + 1);
  56. Simulator* cell = new Simulator(ss.str(), parameters);
  57. _cells.push_back(cell);
  58. add_children(CELL, cell);
  59. }
  60. }
  61. for (int i = 0; i < (int)graph_parameters.column_number; ++i) {
  62. for (int j = 0; j < (int)graph_parameters.line_number; ++j) {
  63. int index = i * graph_parameters.line_number + j;
  64. // south
  65. if (j - 1 >= 0) {
  66. out({_cells[index], Cell::OUT})
  67. >> in({_cells[i * graph_parameters.line_number + j - 1], Cell::IN});
  68. }
  69. // north
  70. if (j + 1 < (int)graph_parameters.line_number) {
  71. out({_cells[index], Cell::OUT})
  72. >> in({_cells[i * graph_parameters.line_number + j + 1], Cell::IN});
  73. }
  74. // west
  75. if (i - 1 >= 0) {
  76. out({_cells[index], Cell::OUT})
  77. >> in({_cells[(i - 1) * graph_parameters.line_number + j], Cell::IN});
  78. }
  79. // east
  80. if (i + 1 < (int)graph_parameters.column_number) {
  81. out({_cells[index], Cell::OUT})
  82. >> in({_cells[(i + 1) * graph_parameters.line_number + j], Cell::IN});
  83. }
  84. }
  85. }
  86. }
  87. ~FlatGraphManager() override
  88. {
  89. std::for_each(_cells.begin(), _cells.end(), std::default_delete<Simulator>());
  90. }
  91. private:
  92. typedef pdevs::Simulator<common::DoubleTime, Cell, CellParameters> Simulator;
  93. typedef std::vector<Simulator*> Simulators;
  94. Simulators _cells;
  95. };
  96. }
  97. }
  98. }
  99. } // namespace artis tests multithreading lifegame
  100. #endif