2019-01-31 12:38:48 +00:00
|
|
|
import multiprocessing
|
|
|
|
|
2018-12-01 19:37:18 +00:00
|
|
|
from cellular_automaton.ca_grid import Grid
|
|
|
|
from cellular_automaton.ca_rule import Rule
|
2018-12-01 14:14:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CellularAutomaton:
|
2019-01-05 16:38:21 +00:00
|
|
|
def __init__(self, grid: Grid, evolution_rule: Rule):
|
|
|
|
self.grid = grid
|
2018-12-08 21:33:13 +00:00
|
|
|
self.evolution_rule = evolution_rule
|
2019-01-05 16:38:21 +00:00
|
|
|
self.evolution_iteration_index = 0
|
2018-12-01 14:14:22 +00:00
|
|
|
|
2018-12-01 19:37:18 +00:00
|
|
|
|
2019-01-06 10:21:46 +00:00
|
|
|
class CellularAutomatonProcessor:
|
2019-01-31 12:38:48 +00:00
|
|
|
def __init__(self, cellular_automaton, process_count: int = 1):
|
|
|
|
self.active = multiprocessing.Value('i', 1)
|
|
|
|
cells = list(cellular_automaton.grid.get_cells().values())
|
|
|
|
chunk_size = int(len(cells) / process_count)
|
|
|
|
self._processes = [multiprocessing.Process(target=_process_routine,
|
|
|
|
name=str(i),
|
|
|
|
args=(cells[i*chunk_size:i*chunk_size + chunk_size],
|
|
|
|
cellular_automaton.evolution_rule,
|
|
|
|
self.active))
|
|
|
|
for i in range(process_count)]
|
|
|
|
for p in self._processes:
|
|
|
|
p.start()
|
2019-01-05 16:38:21 +00:00
|
|
|
self.__cellular_automaton = None
|
2018-12-02 16:45:08 +00:00
|
|
|
|
2019-01-31 12:38:48 +00:00
|
|
|
def stop(self):
|
|
|
|
self.active.value = 0
|
|
|
|
for p in self._processes:
|
|
|
|
p.join()
|
2019-01-06 10:21:46 +00:00
|
|
|
|
|
|
|
|
2019-01-31 12:38:48 +00:00
|
|
|
def _process_routine(cells, rule, active):
|
|
|
|
while active.value == 1:
|
|
|
|
for cell in cells:
|
2019-02-02 08:11:48 +00:00
|
|
|
cell.evolve_if_ready(rule.evolve_cell)
|
2019-01-06 10:21:46 +00:00
|
|
|
|