11 #ifndef PARALLEL_CELLULAR_AUTOMATA_PARALLEL_AUTOMATON_BW_HPP
12 #define PARALLEL_CELLULAR_AUTOMATA_PARALLEL_AUTOMATON_BW_HPP
14 #ifndef PARALLEL_CELLULAR_AUTOMATA_CELLULAR_AUTOMATA_HPP
55 CellularAutomaton(
ca::Grid<T> &grid, std::function<T(T, T, T, T, T, T, T, T, T)> update_function,
57 : grid{grid}, generation(0), update_function(update_function), pool(workers)
60 this->nw = pool.get_number_workers();
71 grid = std::move(other.grid);
72 generation = other.generation;
73 update_function = other.update_function;
75 pool = std::move(other.pool);
105 auto step_advancement_fun = [&]() {
112 auto work = [&,
this](
unsigned start,
unsigned end) {
115 for (
unsigned r{start}; r < end; ++r)
117 for (
unsigned c{0}; c < grid.
columns(); ++c)
119 auto cell = std::make_tuple(grid(r, c));
121 std::apply(this->update_function, std::tuple_cat(cell, get_neighborhood(r, c)));
125 step_advancement_fun);
129 std::vector<std::future<void>> results;
130 unsigned delta{
static_cast<unsigned>(grid.
rows()) / nw};
132 for (
unsigned i{0}; i < nw; i++)
134 unsigned start = i * delta;
135 unsigned end = (i != (nw - 1) ? (i + 1) * delta : grid.
rows());
137 results.push_back(pool.submit(work, start, end));
140 for (
auto &r : results)
167 return os <<
ca.grid;
213 unsigned rows = grid.
rows();
214 unsigned columns = grid.
columns();
215 T top_left, top, top_right, left, right, bottom_left, bottom, bottom_right;
216 top_left = grid((row - 1 + rows) % rows, (col - 1 + columns) % columns);
217 top = grid((row - 1 + rows) % rows, col);
218 top_right = grid((row - 1 + rows) % rows, (col + 1) % columns);
219 left = grid(row, (col - 1 + columns) % columns);
220 right = grid(row, (col + 1) % columns);
221 bottom_left = grid((row + 1) % rows, (col - 1 + columns) % columns);
222 bottom = grid((row + 1) % rows, col);
223 bottom_right = grid((row + 1) % rows, (col + 1) % columns);
224 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 busy_wait()
busy wait for other threads to reach the barrier.
Definition: barrier.cpp:101
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_busyw.hpp:43
Grid< T > & grid
Grid of the C.A.
Definition: parallel_automaton_busyw.hpp:175
Threadpool pool
The threadpool that will run the tasks.
Definition: parallel_automaton_busyw.hpp:236
virtual size_t get_generation() const
Get the generation of the simulation.
Definition: parallel_automaton_busyw.hpp:151
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_busyw.hpp:211
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: parallel_automaton_busyw.hpp:192
size_t generation
Current generation of the grid.
Definition: parallel_automaton_busyw.hpp:181
virtual void simulate(unsigned steps=1)
Run the simulation for a given number of steps.
Definition: parallel_automaton_busyw.hpp:96
unsigned nw
Number of worker threads.
Definition: parallel_automaton_busyw.hpp:225
CellularAutomaton(CellularAutomaton &&other)
Construct a new Cellular Automaton object from another one using move semantic.
Definition: parallel_automaton_busyw.hpp:68
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_busyw.hpp:55
friend std::ostream & operator<<(std::ostream &os, const CellularAutomaton &ca)
Overload of the << operator.
Definition: parallel_automaton_busyw.hpp:164
Definition of the grid of the automaton.
Namespace of the framework.
Definition: barrier.hpp:20
definition of a threadpool.