parallel-cellular-automata
Framework for building parallel cellular automata.
omp_automaton.hpp
Go to the documentation of this file.
1 
11 #ifndef PARALLEL_CELLULAR_AUTOMATA_OMPL_AUTOMATON_HPP
12 #define PARALLEL_CELLULAR_AUTOMATA_OMPL_AUTOMATON_HPP
13 #include <cstddef>
14 #include <functional>
15 #include <grid.hpp>
16 #include <iostream>
17 #include <tuple>
18 
19 namespace ca
20 {
25 namespace omp
26 {
35 template <typename T>
37 {
38  public:
47  CellularAutomaton(ca::Grid<T> &grid, std::function<T(T, T, T, T, T, T, T, T, T)> update_function,
48  unsigned workers = 0)
50  {
51  nw = (workers) ? workers : std::thread::hardware_concurrency();
52  };
53 
61  {
62  // move what's movable and copying numeric types.
63  grid = std::move(other.grid);
64  update_function = std::move(other.update_function);
65  }
66 
74  CellularAutomaton(const CellularAutomaton &other) = delete;
75 
83  virtual void simulate(unsigned steps = 1)
84  {
85  if (steps == 0)
86  return;
87  // allocate new grid
89 
90  // compute state and put values on the new grid.
91  while (steps > 0)
92  {
93 #pragma omp parallel for collapse(2) num_threads(nw)
94  for (size_t r = 0; r < grid.rows(); ++r)
95  {
96  for (size_t c = 0; c < grid.columns(); ++c)
97  {
98  auto cell = std::make_tuple(grid(r, c));
99  new_grid(r, c) = std::apply(update_function, std::tuple_cat(cell, get_neighborhood(r, c)));
100  }
101  }
102  // swap grids so grid contains the updated value.
103  grid.swap(new_grid);
104  ++generation;
105  --steps;
106  }
107  }
108 
114  size_t get_generation() const
115  {
116  return generation;
117  }
127  friend std::ostream &operator<<(std::ostream &os, const CellularAutomaton &ca)
128  {
129 
130  return os << ca.grid;
131  }
132 
133  protected:
139 
144  size_t generation;
155  std::function<T(T, T, T, T, T, T, T, T, T)> update_function;
156 
161  unsigned nw;
162 
181  virtual std::tuple<T, T, T, T, T, T, T, T> get_neighborhood(int row, int col) const
182 
183  {
184  unsigned rows = grid.rows();
185  unsigned columns = grid.columns();
186  T top_left, top, top_right, left, right, bottom_left, bottom, bottom_right;
187  top_left = grid((row - 1 + rows) % rows, (col - 1 + columns) % columns);
188  top = grid((row - 1 + rows) % rows, col);
189  top_right = grid((row - 1 + rows) % rows, (col + 1) % columns);
190  left = grid(row, (col - 1 + columns) % columns);
191  right = grid(row, (col + 1) % columns);
192  bottom_left = grid((row + 1) % rows, (col - 1 + columns) % columns);
193  bottom = grid((row + 1) % rows, col);
194  bottom_right = grid((row + 1) % rows, (col + 1) % columns);
195  return std::make_tuple(top_left, top, top_right, left, right, bottom_left, bottom, bottom_right);
196  };
197 };
198 } // namespace omp
199 } // namespace ca
200 
201 #endif
Grid of the cellular automaton.
Definition: grid.hpp:31
static Grid newWithSameSize(const Grid &other)
Return a grid of the same dimension of the grid passed as argument.
Definition: grid.hpp:95
Parallel Cellular Automaton using OpenMP.
Definition: omp_automaton.hpp:37
CellularAutomaton(CellularAutomaton &&other)
Construct a new Cellular Automaton object from another one using move semantic.
Definition: omp_automaton.hpp:60
size_t get_generation() const
Get the generation of the simulation.
Definition: omp_automaton.hpp:114
size_t generation
Current generation of the grid.
Definition: omp_automaton.hpp:144
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: omp_automaton.hpp:181
virtual void simulate(unsigned steps=1)
Run the simulation for a given number of steps.
Definition: omp_automaton.hpp:83
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: omp_automaton.hpp:47
Grid< T > & grid
Grid of the C.A.
Definition: omp_automaton.hpp:138
friend std::ostream & operator<<(std::ostream &os, const CellularAutomaton &ca)
Overload of the << operator.
Definition: omp_automaton.hpp:127
unsigned nw
Number of worker threads.
Definition: omp_automaton.hpp:161
CellularAutomaton(const CellularAutomaton &other)=delete
Deleted copy constructor.
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: omp_automaton.hpp:155
Definition of the grid of the automaton.
Namespace of the framework.
Definition: barrier.hpp:20