Pārlūkot izejas kodu

Increase neighbourhood of Life game example

Eric Ramat 5 gadi atpakaļ
vecāks
revīzija
df6a1911cf

+ 22 - 2
src/tests/multithreading/lifegame/graph_manager.hpp

@@ -73,12 +73,12 @@ namespace artis {
                             for (int j = 0; j < (int)graph_parameters.line_number; ++j) {
                                 int index = i * graph_parameters.line_number + j;
 
-                                // south
+                                // north
                                 if (j - 1 >= 0) {
                                     out({_cells[index], Cell::OUT})
                                             >> in({_cells[i * graph_parameters.line_number + j - 1], Cell::IN});
                                 }
-                                // north
+                                // south
                                 if (j + 1 < (int)graph_parameters.line_number) {
                                     out({_cells[index], Cell::OUT})
                                             >> in({_cells[i * graph_parameters.line_number + j + 1], Cell::IN});
@@ -93,6 +93,26 @@ namespace artis {
                                     out({_cells[index], Cell::OUT})
                                             >> in({_cells[(i + 1) * graph_parameters.line_number + j], Cell::IN});
                                 }
+                                // north west
+                                if (j - 1 >= 0 and i - 1 >= 0) {
+                                    out({_cells[index], Cell::OUT})
+                                            >> in({_cells[(i - 1) * graph_parameters.line_number + j - 1], Cell::IN});
+                                }
+                                // south west
+                                if (j + 1 < (int)graph_parameters.line_number and i - 1 >= 0) {
+                                    out({_cells[index], Cell::OUT})
+                                            >> in({_cells[(i - 1) * graph_parameters.line_number + j + 1], Cell::IN});
+                                }
+                                // north east
+                                if (j - 1 >= 0 and i + 1 < (int)graph_parameters.column_number) {
+                                    out({_cells[index], Cell::OUT})
+                                            >> in({_cells[(i + 1) * graph_parameters.line_number + j - 1], Cell::IN});
+                                }
+                                // south east
+                                if (j + 1 < (int)graph_parameters.line_number and i + 1 < (int)graph_parameters.column_number) {
+                                    out({_cells[index], Cell::OUT})
+                                            >> in({_cells[(i + 1) * graph_parameters.line_number + j + 1], Cell::IN});
+                                }
                             }
                         }
                     }

+ 182 - 32
src/tests/multithreading/lifegame/main.cpp

@@ -70,7 +70,7 @@ void show_state(const GridGraphManagerParameters& graph_parameters,
                 bool state;
 
                 value.second(state);
-                states[(int)value.first][i][j] = state;
+                states[(int) value.first][i][j] = state;
             }
         }
     }
@@ -89,31 +89,106 @@ double lifegame_monothreading()
 {
     CellParameters parameters({
             {
-                    {"C_1_1", 2},
-                    {"C_1_2", 3},
-                    {"C_1_3", 3},
-                    {"C_1_4", 3},
-                    {"C_1_5", 2},
-                    {"C_2_1", 3},
-                    {"C_2_2", 4},
-                    {"C_2_3", 4},
-                    {"C_2_4", 4},
-                    {"C_2_5", 3},
-                    {"C_3_1", 3},
-                    {"C_3_2", 4},
-                    {"C_3_3", 4},
-                    {"C_3_4", 4},
-                    {"C_3_5", 3},
-                    {"C_4_1", 3},
-                    {"C_4_2", 4},
-                    {"C_4_3", 4},
-                    {"C_4_4", 4},
-                    {"C_4_5", 3},
-                    {"C_5_1", 2},
-                    {"C_5_2", 3},
-                    {"C_5_3", 3},
-                    {"C_5_4", 3},
-                    {"C_5_5", 2},
+                    {"C_1_1", 3},
+                    {"C_1_2", 5},
+                    {"C_1_3", 5},
+                    {"C_1_4", 5},
+                    {"C_1_5", 5},
+                    {"C_1_6", 5},
+                    {"C_1_7", 5},
+                    {"C_1_8", 5},
+                    {"C_1_9", 5},
+                    {"C_1_10", 3},
+                    {"C_2_1", 5},
+                    {"C_2_2", 8},
+                    {"C_2_3", 8},
+                    {"C_2_4", 8},
+                    {"C_2_5", 8},
+                    {"C_2_6", 8},
+                    {"C_2_7", 8},
+                    {"C_2_8", 8},
+                    {"C_2_9", 8},
+                    {"C_2_10", 5},
+                    {"C_3_1", 5},
+                    {"C_3_2", 8},
+                    {"C_3_3", 8},
+                    {"C_3_4", 8},
+                    {"C_3_5", 8},
+                    {"C_3_6", 8},
+                    {"C_3_7", 8},
+                    {"C_3_8", 8},
+                    {"C_3_9", 8},
+                    {"C_3_10", 5},
+                    {"C_4_1", 5},
+                    {"C_4_2", 8},
+                    {"C_4_3", 8},
+                    {"C_4_4", 8},
+                    {"C_4_5", 8},
+                    {"C_4_6", 8},
+                    {"C_4_7", 8},
+                    {"C_4_8", 8},
+                    {"C_4_9", 8},
+                    {"C_4_10", 5},
+                    {"C_5_1", 5},
+                    {"C_5_2", 8},
+                    {"C_5_3", 8},
+                    {"C_5_4", 8},
+                    {"C_5_5", 8},
+                    {"C_5_6", 8},
+                    {"C_5_7", 8},
+                    {"C_5_8", 8},
+                    {"C_5_9", 8},
+                    {"C_5_10", 5},
+                    {"C_6_1", 5},
+                    {"C_6_2", 8},
+                    {"C_6_3", 8},
+                    {"C_6_4", 8},
+                    {"C_6_5", 8},
+                    {"C_6_6", 8},
+                    {"C_6_7", 8},
+                    {"C_6_8", 8},
+                    {"C_6_9", 8},
+                    {"C_6_10", 5},
+                    {"C_7_1", 5},
+                    {"C_7_2", 8},
+                    {"C_7_3", 8},
+                    {"C_7_4", 8},
+                    {"C_7_5", 8},
+                    {"C_7_6", 8},
+                    {"C_7_7", 8},
+                    {"C_7_8", 8},
+                    {"C_7_9", 8},
+                    {"C_7_10", 5},
+                    {"C_8_1", 5},
+                    {"C_8_2", 8},
+                    {"C_8_3", 8},
+                    {"C_8_4", 8},
+                    {"C_8_5", 8},
+                    {"C_8_6", 8},
+                    {"C_8_7", 8},
+                    {"C_8_8", 8},
+                    {"C_8_9", 8},
+                    {"C_8_10", 5},
+                    {"C_9_1", 5},
+                    {"C_9_2", 8},
+                    {"C_9_3", 8},
+                    {"C_9_4", 8},
+                    {"C_9_5", 8},
+                    {"C_9_6", 8},
+                    {"C_9_7", 8},
+                    {"C_9_8", 8},
+                    {"C_9_9", 8},
+                    {"C_9_10", 5},
+                    {"C_10_1", 3},
+                    {"C_10_2", 5},
+                    {"C_10_3", 5},
+                    {"C_10_4", 5},
+                    {"C_10_5", 5},
+                    {"C_10_6", 5},
+                    {"C_10_7", 5},
+                    {"C_10_8", 5},
+                    {"C_10_9", 5},
+                    {"C_10_10", 3}
             },
             {
                     {"C_1_1", false},
@@ -121,29 +196,104 @@ double lifegame_monothreading()
                     {"C_1_3", false},
                     {"C_1_4", false},
                     {"C_1_5", false},
+                    {"C_1_6", false},
+                    {"C_1_7", false},
+                    {"C_1_8", false},
+                    {"C_1_9", false},
+                    {"C_1_10", false},
                     {"C_2_1", false},
                     {"C_2_2", false},
-                    {"C_2_3", true},
+                    {"C_2_3", false},
                     {"C_2_4", false},
-                    {"C_2_5", false},
+                    {"C_2_5", true},
+                    {"C_2_6", true},
+                    {"C_2_7", false},
+                    {"C_2_8", false},
+                    {"C_2_9", false},
+                    {"C_2_10", false},
                     {"C_3_1", false},
                     {"C_3_2", false},
-                    {"C_3_3", true},
-                    {"C_3_4", false},
+                    {"C_3_3", false},
+                    {"C_3_4", true},
                     {"C_3_5", false},
+                    {"C_3_6", false},
+                    {"C_3_7", true},
+                    {"C_3_8", false},
+                    {"C_3_9", false},
+                    {"C_3_10", false},
                     {"C_4_1", false},
                     {"C_4_2", false},
                     {"C_4_3", true},
                     {"C_4_4", false},
                     {"C_4_5", false},
+                    {"C_4_6", false},
+                    {"C_4_7", false},
+                    {"C_4_8", true},
+                    {"C_4_9", false},
+                    {"C_4_10", false},
                     {"C_5_1", false},
-                    {"C_5_2", false},
+                    {"C_5_2", true},
                     {"C_5_3", false},
                     {"C_5_4", false},
                     {"C_5_5", false},
+                    {"C_5_6", false},
+                    {"C_5_7", false},
+                    {"C_5_8", false},
+                    {"C_5_9", true},
+                    {"C_5_10", false},
+                    {"C_6_1", false},
+                    {"C_6_2", true},
+                    {"C_6_3", false},
+                    {"C_6_4", false},
+                    {"C_6_5", false},
+                    {"C_6_6", false},
+                    {"C_6_7", false},
+                    {"C_6_8", false},
+                    {"C_6_9", true},
+                    {"C_6_10", false},
+                    {"C_7_1", false},
+                    {"C_7_2", false},
+                    {"C_7_3", true},
+                    {"C_7_4", false},
+                    {"C_7_5", false},
+                    {"C_7_6", false},
+                    {"C_7_7", false},
+                    {"C_7_8", true},
+                    {"C_7_9", false},
+                    {"C_7_10", false},
+                    {"C_8_1", false},
+                    {"C_8_2", false},
+                    {"C_8_3", false},
+                    {"C_8_4", true},
+                    {"C_8_5", false},
+                    {"C_8_6", false},
+                    {"C_8_7", true},
+                    {"C_8_8", false},
+                    {"C_8_9", false},
+                    {"C_8_10", false},
+                    {"C_9_1", false},
+                    {"C_9_2", false},
+                    {"C_9_3", false},
+                    {"C_9_4", false},
+                    {"C_9_5", true},
+                    {"C_9_6", true},
+                    {"C_9_7", false},
+                    {"C_9_8", false},
+                    {"C_9_9", false},
+                    {"C_9_10", false},
+                    {"C_10_1",  false},
+                    {"C_10_2",  false},
+                    {"C_10_3",  false},
+                    {"C_10_4",  false},
+                    {"C_10_5",  false},
+                    {"C_10_6",  false},
+                    {"C_10_7",  false},
+                    {"C_10_8",  false},
+                    {"C_10_9",  false},
+                    {"C_10_10",  false}
             }
     });
-    GridGraphManagerParameters graph_parameters({5, 5});
+    GridGraphManagerParameters graph_parameters({10, 10});
 
     artis::common::context::Context<artis::common::DoubleTime> context(0, 10);
     artis::common::RootCoordinator<

+ 23 - 13
src/tests/multithreading/lifegame/models.hpp

@@ -41,7 +41,8 @@ namespace artis {
                     std::map<std::string, bool> initial_states;
                 };
 
-                class Cell : public artis::pdevs::Dynamics<common::DoubleTime, Cell, CellParameters> {
+                class Cell
+                        : public artis::pdevs::Dynamics<common::DoubleTime, Cell, CellParameters> {
                 public:
                     enum inputs {
                         IN
@@ -56,8 +57,10 @@ namespace artis {
                     Cell(const std::string& name,
                             const artis::pdevs::Context<common::DoubleTime, Cell, CellParameters>& context)
                             :
-                            artis::pdevs::Dynamics<common::DoubleTime, Cell, CellParameters>(name, context),
-                            _neighbour_number(context.parameters().neighbour_numbers.find(name)->second),
+                            artis::pdevs::Dynamics<common::DoubleTime, Cell, CellParameters>(name,
+                                    context),
+                            _neighbour_number(
+                                    context.parameters().neighbour_numbers.find(name)->second),
                             _initial_state(context.parameters().initial_states.find(name)->second)
                     {
                         input_ports({{IN, "in"}});
@@ -72,8 +75,9 @@ namespace artis {
                         if (_phase == SEND) {
                             _phase = WAIT;
                             _sigma = common::DoubleTime::infinity;
-                        } else if (_phase == NEWSTATE) {
-                            if (_state and (_true_neighbour_number < 2 or _true_neighbour_number > 3)) {
+                        } else if (_phase == NEW_STATE) {
+                            if (_state and (_true_neighbour_number < 2
+                                    or _true_neighbour_number > 3)) {
                                 _state = false;
                             } else if (not _state and (_true_neighbour_number == 3)) {
                                 _state = true;
@@ -85,7 +89,8 @@ namespace artis {
                         }
                     }
 
-                    void dext(typename common::DoubleTime::type /* t */, typename common::DoubleTime::type /* e */,
+                    void dext(typename common::DoubleTime::type /* t */,
+                            typename common::DoubleTime::type /* e */,
                             const common::Bag<common::DoubleTime>& bag) override
                     {
                         std::for_each(bag.begin(), bag.end(),
@@ -101,7 +106,7 @@ namespace artis {
                                     }
                                 });
                         if (_received == _neighbour_number) {
-                            _phase = NEWSTATE;
+                            _phase = NEW_STATE;
                             _sigma = 0;
                         } else {
                             _phase = WAIT;
@@ -109,7 +114,8 @@ namespace artis {
                         }
                     }
 
-                    void dconf(typename common::DoubleTime::type t, typename common::DoubleTime::type e,
+                    void
+                    dconf(typename common::DoubleTime::type t, typename common::DoubleTime::type e,
                             const common::Bag<common::DoubleTime>& bag) override
                     {
                         dext(t, e, bag);
@@ -126,22 +132,26 @@ namespace artis {
                         _received = 0;
                     }
 
-                    typename common::DoubleTime::type ta(typename common::DoubleTime::type /* t */) const override
+                    typename common::DoubleTime::type
+                    ta(typename common::DoubleTime::type /* t */) const override
                     {
                         return _sigma;
                     }
 
-                    common::Bag<common::DoubleTime> lambda(typename common::DoubleTime::type /* t */) const override
+                    common::Bag<common::DoubleTime>
+                    lambda(typename common::DoubleTime::type /* t */) const override
                     {
                         common::Bag<common::DoubleTime> bag;
 
                         if (_phase == SEND) {
-                            bag.push_back(artis::common::ExternalEvent<common::DoubleTime>(OUT, _state));
+                            bag.push_back(
+                                    artis::common::ExternalEvent<common::DoubleTime>(OUT, _state));
                         }
                         return bag;
                     }
 
-                    common::Value observe(const common::DoubleTime::type& /* t */, unsigned int index) const override
+                    common::Value observe(const common::DoubleTime::type& /* t */,
+                            unsigned int index) const override
                     {
                         if (index == STATE) {
                             return _state;
@@ -151,7 +161,7 @@ namespace artis {
 
                 private:
                     enum Phase {
-                        SEND, WAIT, NEWSTATE
+                        SEND, WAIT, NEW_STATE
                     };
 
                     // parameters