14 #ifndef PARALLEL_CELLULAR_AUTOMATA_FASTFLOW_AUTOMATON_HPP
15 #define PARALLEL_CELLULAR_AUTOMATA_FASTFLOW_AUTOMATON_HPP
17 #include <ff/parallel_for.hpp>
44 CellularAutomaton(
ca::Grid<T> &grid, std::function<T(T, T, T, T, T, T, T, T, T)> update_function,
46 : grid(grid), generation(0), update_function(update_function),
47 pf((workers) ? workers : std::thread::hardware_concurrency())
50 this->nw = (workers) ? workers : std::thread::hardware_concurrency();
61 grid = std::move(other.grid);
62 generation = other.generation;
63 update_function = other.update_function;
65 pf = std::move(other.pf);
97 for (size_t j = 0; j < grid.columns(); ++j)
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)));
131 return os <<
ca.grid;
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);
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