parallel-cellular-automata
Framework for building parallel cellular automata.
ff_automaton.hpp
Go to the documentation of this file.
1 
14 #ifndef PARALLEL_CELLULAR_AUTOMATA_FASTFLOW_AUTOMATON_HPP
15 #define PARALLEL_CELLULAR_AUTOMATA_FASTFLOW_AUTOMATON_HPP
16 #include <ff/ff.hpp>
17 #include <ff/parallel_for.hpp>
18 #include <iostream>
19 #include <thread>
20 using namespace std;
21 
22 namespace ca
23 {
28 namespace ffl
29 {
30 template <typename T>
32 {
33  public:
44  CellularAutomaton(ca::Grid<T> &grid, std::function<T(T, T, T, T, T, T, T, T, T)> update_function,
45  unsigned workers = 0)
46  : grid(grid), generation(0), update_function(update_function),
47  pf((workers) ? workers : std::thread::hardware_concurrency())
48  {
49 
50  this->nw = (workers) ? workers : std::thread::hardware_concurrency();
51  };
59  {
60  // move what's movable and copying numeric types.
61  grid = std::move(other.grid);
62  generation = other.generation;
63  update_function = other.update_function;
64  nw = other.nw;
65  pf = std::move(other.pf);
66  // set the old object in a valid state
67  other.generation = 0;
68  }
69 
77  CellularAutomaton(const CellularAutomaton &other) = delete;
78 
86  virtual void simulate(unsigned steps = 1)
87  {
88  if (steps == 0)
89  return;
90  Grid<T> new_grid = Grid<T>::newWithSameSize(grid);
91 
92  while (steps > 0)
93  {
94  pf.parallel_for(
95  0, grid.rows(),
96  [&](const long i) {
97  for (size_t j = 0; j < grid.columns(); ++j)
98  {
99  auto cell = std::make_tuple(grid(i, j));
100  new_grid(i, j) = std::apply(update_function, std::tuple_cat(cell, get_neighborhood(i, j)));
101  }
102  },
103  nw);
104  grid.swap(new_grid);
105  ++generation;
106  --steps;
107  }
108  }
109 
115  virtual size_t get_generation() const
116  {
117  return generation;
118  }
128  friend std::ostream &operator<<(std::ostream &os, const CellularAutomaton &ca)
129  {
130 
131  return os << ca.grid;
132  }
133 
134  protected:
140 
145  size_t generation;
156  std::function<T(T, T, T, T, T, T, T, T, T)> update_function;
175  virtual std::tuple<T, T, T, T, T, T, T, T> get_neighborhood(int row, int col) const
176  {
177  unsigned rows = grid.rows();
178  unsigned columns = grid.columns();
179  T top_left, top, top_right, left, right, bottom_left, bottom, bottom_right;
180  top_left = grid((row - 1 + rows) % rows, (col - 1 + columns) % columns);
181  top = grid((row - 1 + rows) % rows, col);
182  top_right = grid((row - 1 + rows) % rows, (col + 1) % columns);
183  left = grid(row, (col - 1 + columns) % columns);
184  right = grid(row, (col + 1) % columns);
185  bottom_left = grid((row + 1) % rows, (col - 1 + columns) % columns);
186  bottom = grid((row + 1) % rows, col);
187  bottom_right = grid((row + 1) % rows, (col + 1) % columns);
188  return std::make_tuple(top_left, top, top_right, left, right, bottom_left, bottom, bottom_right);
189  };
194  unsigned nw;
199  ff::ParallelFor pf;
200 };
201 } // namespace ffl
202 } // namespace ca
203 
204 #endif
Grid of the cellular automaton.
Definition: grid.hpp:31
size_t columns() const
Returns the number of columns of the grid.
Definition: grid.hpp:200
void swap(Grid &other)
Swap the content of the grid with the one of another grid.
Definition: grid.hpp:243
size_t rows() const
Returns the number of rows of the grid.
Definition: grid.hpp:191
Definition: ff_automaton.hpp:32
std::function< T(T, T, T, T, T, T, T, T, T)> update_function
Function used to compute the next state of the cell.
Definition: ff_automaton.hpp:156
ff::ParallelFor pf
parallel for pattern run-time suppor
Definition: ff_automaton.hpp:199
virtual size_t get_generation() const
Get the generation of the simulation.
Definition: ff_automaton.hpp:115
size_t generation
Current generation of the grid.
Definition: ff_automaton.hpp:145
CellularAutomaton(const CellularAutomaton &other)=delete
Deleted copy constructor.
virtual std::tuple< T, T, T, T, T, T, T, T > get_neighborhood(int row, int col) const
Get the neighborhood of a cell.
Definition: ff_automaton.hpp:175
CellularAutomaton(ca::Grid< T > &grid, std::function< T(T, T, T, T, T, T, T, T, T)> update_function, unsigned workers=0)
Construct a new Cellular Automaton object.
Definition: ff_automaton.hpp:44
CellularAutomaton(CellularAutomaton &&other)
Construct a new Cellular Automaton object from another one using move semantic.
Definition: ff_automaton.hpp:58
virtual void simulate(unsigned steps=1)
Run the simulation for a given number of steps.
Definition: ff_automaton.hpp:86
unsigned nw
Number of worker threads.
Definition: ff_automaton.hpp:189
Grid< T > & grid
Grid of the C.A.
Definition: ff_automaton.hpp:139
friend std::ostream & operator<<(std::ostream &os, const CellularAutomaton &ca)
Overload of the << operator.
Definition: ff_automaton.hpp:128
Namespace of the framework.
Definition: barrier.hpp:20