13 #ifndef PARALLEL_CELLULAR_AUTOMATA_PARALLEL_AUTOMATON_HPP
14 #define PARALLEL_CELLULAR_AUTOMATA_PARALLEL_AUTOMATON_HPP
16 #ifndef PARALLEL_CELLULAR_AUTOMATA_CELLULAR_AUTOMATA_HPP
50 CellularAutomaton(
ca::Grid<T> &grid, std::function<T(T, T, T, T, T, T, T, T, T)> update_function,
52 : grid{grid}, generation(0), update_function(update_function), pool(workers)
55 this->nw = pool.get_number_workers();
66 grid = std::move(other.grid);
67 generation = other.generation;
68 update_function = other.update_function;
70 pool = std::move(other.pool);
100 auto step_advancement_fun = [&]() {
107 auto work = [&,
this](
unsigned start,
unsigned end) {
110 for (
unsigned r{start}; r < end; ++r)
112 for (
unsigned c{0}; c < grid.
columns(); ++c)
114 auto cell = std::make_tuple(grid(r, c));
116 std::apply(this->update_function, std::tuple_cat(cell, get_neighborhood(r, c)));
120 step_advancement_fun);
124 std::vector<std::future<void>> results;
125 unsigned delta{
static_cast<unsigned>(grid.
rows()) / nw};
127 for (
unsigned i{0}; i < nw; i++)
129 unsigned start = i * delta;
130 unsigned end = (i != (nw - 1) ? (i + 1) * delta : grid.
rows());
132 results.push_back(pool.submit(work, start, end));
135 for (
auto &r : results)
162 return os <<
ca.grid;
208 unsigned rows = grid.
rows();
209 unsigned columns = grid.
columns();
210 T top_left, top, top_right, left, right, bottom_left, bottom, bottom_right;
211 top_left = grid((row - 1 + rows) % rows, (col - 1 + columns) % columns);
212 top = grid((row - 1 + rows) % rows, col);
213 top_right = grid((row - 1 + rows) % rows, (col + 1) % columns);
214 left = grid(row, (col - 1 + columns) % columns);
215 right = grid(row, (col + 1) % columns);
216 bottom_left = grid((row + 1) % rows, (col - 1 + columns) % columns);
217 bottom = grid((row + 1) % rows, col);
218 bottom_right = grid((row + 1) % rows, (col + 1) % columns);
219 return std::make_tuple(top_left, top, top_right, left, right, bottom_left, bottom, bottom_right);
This file contains the definition of a barrier for thread synchronization.
This header imports all the other headers of the framework.
Self-resetting synchronization barrier for threads.
Definition: barrier.hpp:27
void wait()
wait for the other threads to reach the barrier.
Definition: barrier.cpp:23
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
Work-stealing threadpool.
Definition: threadpool.hpp:51
Definition: parallel_automaton.hpp:38
CellularAutomaton(const CellularAutomaton &other)=delete
Deleted copy constructor.
virtual size_t get_generation() const
Get the generation of the simulation.
Definition: parallel_automaton.hpp:146
virtual void simulate(unsigned steps=1)
Run the simulation for a given number of steps.
Definition: parallel_automaton.hpp:91
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: parallel_automaton.hpp:187
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: parallel_automaton.hpp:206
CellularAutomaton(CellularAutomaton &&other)
Construct a new Cellular Automaton object from another one using move semantic.
Definition: parallel_automaton.hpp:63
unsigned nw
Number of worker threads.
Definition: parallel_automaton.hpp:220
Grid< T > & grid
Grid of the C.A.
Definition: parallel_automaton.hpp:170
Threadpool pool
The threadpool that will run the tasks.
Definition: parallel_automaton.hpp:231
size_t generation
Current generation of the grid.
Definition: parallel_automaton.hpp:176
friend std::ostream & operator<<(std::ostream &os, const CellularAutomaton &ca)
Overload of the << operator.
Definition: parallel_automaton.hpp:159
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: parallel_automaton.hpp:50
Definition of the grid of the automaton.
Namespace of the framework.
Definition: barrier.hpp:20
definition of a threadpool.