parallel-cellular-automata
Framework for building parallel cellular automata.
Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
ca::omp::CellularAutomaton< T > Class Template Reference

Parallel Cellular Automaton using OpenMP. More...

#include <omp_automaton.hpp>

Public Member Functions

 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. More...
 
 CellularAutomaton (CellularAutomaton &&other)
 Construct a new Cellular Automaton object from another one using move semantic. More...
 
 CellularAutomaton (const CellularAutomaton &other)=delete
 Deleted copy constructor. More...
 
virtual void simulate (unsigned steps=1)
 Run the simulation for a given number of steps. More...
 
size_t get_generation () const
 Get the generation of the simulation. More...
 

Protected Member Functions

virtual std::tuple< T, T, T, T, T, T, T, T > get_neighborhood (int row, int col) const
 Get the neighborhood of a cell. More...
 

Protected Attributes

Grid< T > & grid
 Grid of the C.A.
 
size_t generation
 Current generation of the grid.
 
std::function< T(T, T, T, T, T, T, T, T, T)> update_function
 Function used to compute the next state of the cell. More...
 
unsigned nw
 Number of worker threads.
 

Friends

std::ostream & operator<< (std::ostream &os, const CellularAutomaton &ca)
 Overload of the << operator. More...
 

Detailed Description

template<typename T>
class ca::omp::CellularAutomaton< T >

Parallel Cellular Automaton using OpenMP.

This class implements a toroidal Cellular Automaton parallelized using OpenMP.

Template Parameters
Ttype of the cells.

Constructor & Destructor Documentation

◆ CellularAutomaton() [1/3]

template<typename T >
ca::omp::CellularAutomaton< T >::CellularAutomaton ( ca::Grid< T > &  grid,
std::function< T(T, T, T, T, T, T, T, T, T)>  update_function,
unsigned  workers = 0 
)
inline

Construct a new Cellular Automaton object.

Parameters
gridGrid of the simulation.
update_functionFunction used to update the state of the grid.
workersNumber of threads. If 0 the default value is chosen by OpenMP.

◆ CellularAutomaton() [2/3]

template<typename T >
ca::omp::CellularAutomaton< T >::CellularAutomaton ( CellularAutomaton< T > &&  other)
inline

Construct a new Cellular Automaton object from another one using move semantic.

Note
The old object will be left in a valid but unspecified state.
Parameters
otherCellularAutomaton to move.

◆ CellularAutomaton() [3/3]

template<typename T >
ca::omp::CellularAutomaton< T >::CellularAutomaton ( const CellularAutomaton< T > &  other)
delete

Deleted copy constructor.

Note
The copy constructor has been deleted since the class uses a pointer to a user defined grid and the freeing of the grid is up to them.

Member Function Documentation

◆ get_generation()

template<typename T >
size_t ca::omp::CellularAutomaton< T >::get_generation ( ) const
inline

Get the generation of the simulation.

Returns
size_t value of the generation member variable.

◆ get_neighborhood()

template<typename T >
virtual std::tuple<T, T, T, T, T, T, T, T> ca::omp::CellularAutomaton< T >::get_neighborhood ( int  row,
int  col 
) const
inlineprotectedvirtual

Get the neighborhood of a cell.

+----+-----------+----+
| TL | T | TR |
+----+-----------+----+
| L | (row,col) | R |
+----+-----------+----+
| BL | B | BR |
+----+-----------+----+
Precondition
0 <= row < rows.
0 <= col < columns.
Parameters
rowrow of the center cell.
colcolumn of the center cell.
Returns
std::tuple<T, T, T, T, T, T, T, T> containing <TL, T, TR, L, R, BL, B, BR>

◆ simulate()

template<typename T >
virtual void ca::omp::CellularAutomaton< T >::simulate ( unsigned  steps = 1)
inlinevirtual

Run the simulation for a given number of steps.

Parameters
stepsnumber of simulation steps to run (default 1).
Postcondition
grid contains the result of the simulation and generation = generation + steps.

Friends And Related Function Documentation

◆ operator<<

template<typename T >
std::ostream& operator<< ( std::ostream &  os,
const CellularAutomaton< T > &  ca 
)
friend

Overload of the << operator.

Print the grid on a output stream.

Parameters
osoutput stream
caCellularAutomaton
Returns
std::ostream& reference to the output stream

Member Data Documentation

◆ update_function

template<typename T >
std::function<T(T, T, T, T, T, T, T, T, T)> ca::omp::CellularAutomaton< T >::update_function
protected

Function used to compute the next state of the cell.

The function takes nine arguments. It will be called by passing the central cell as first argument and its eight neighbors as the remaining arguments. Their order is the one returned by the get_neighborhood function.

See also
get_neighborhood

The documentation for this class was generated from the following file: