11 #ifndef PARALLEL_CELLULAR_AUTOMATA_OMPL_AUTOMATON_HPP
12 #define PARALLEL_CELLULAR_AUTOMATA_OMPL_AUTOMATON_HPP
47 CellularAutomaton(
ca::Grid<T> &
grid, std::function<T(T, T, T, T, T, T, T, T, T)>
update_function,
51 nw = (workers) ? workers : std::thread::hardware_concurrency();
63 grid = std::move(other.grid);
93 #pragma omp parallel for collapse(2) num_threads(nw)
94 for (
size_t r = 0; r <
grid.rows(); ++r)
96 for (
size_t c = 0; c <
grid.columns(); ++c)
98 auto cell = std::make_tuple(
grid(r, c));
130 return os <<
ca.grid;
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);
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