Skip to content

Commit 4c17f7c

Browse files
committed
Merge remote-tracking branch 'origin/development' into development
2 parents 3ace8f2 + 2499dfc commit 4c17f7c

File tree

8 files changed

+927
-0
lines changed

8 files changed

+927
-0
lines changed

source-code/performance/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ These are some resources about general performance considerations.
88
1. `number_puzzle.ipynb`: jupyter notebbook solving a number puzzle
99
in a variety of ways, shwoing some aspects of performance
1010
optimization.
11+
1. `cellular_automata`: some illustrations of profiling and performance
12+
optimizations.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CXX=g++
2+
CXXFLAGS=-O3 -std=c++17 -Wall -Wextra -Wpedantic
3+
4+
all: cellular_automaton.exe
5+
6+
cellular_automaton.exe: automaton_runner.o main.o
7+
g++ $(CXXFLAGS) -o $@ $^
8+
9+
clean:
10+
$(RM) $(wildcard *.o) $(wildcard *.exe)
11+
$(RM) core
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Cellular automata
2+
3+
Performance of various implemenations of cellular automata.
4+
5+
## What is it?
6+
7+
1. `cellular_automaton.py`: Python script to run from the command line.
8+
2. C++ implementation
9+
a. `automaton_runner.h`: automaton runner class definition.
10+
a. `automaton_runner.cpp`: automaton runner method definitions.
11+
a. `main.cpp`: C++ application to run cellular automata.
12+
a. `Makefile`: make file to build the C++ application.
13+
3. `cellular_automaton.ipynb`: Jupyter notebook comparing various
14+
implementations.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "automaton_runner.h"
2+
3+
AutomatonRunner::AutomatonRunner(uint8_t rule_nr) {
4+
for (uint8_t i = 0; i < rules.size(); ++i) {
5+
rules[i] = rule_nr % 2;
6+
rule_nr /= 2;
7+
}
8+
}
9+
10+
Automaton AutomatonRunner::next_generation(const Automaton& automaton) {
11+
uint8_t idx;
12+
size_t nr_cells {automaton.size()};
13+
Automaton ng_automaton(nr_cells);
14+
// first cell
15+
idx = automaton[nr_cells - 1] << 2 | automaton[0] << 1 | automaton[1];
16+
ng_automaton[0] = rules[idx];
17+
// second to one before last cell
18+
for (size_t cell_nr = 1; cell_nr < nr_cells - 1; ++cell_nr) {
19+
idx = automaton[cell_nr - 1] << 2 | automaton[cell_nr] << 1 | automaton[cell_nr + 1];
20+
ng_automaton[cell_nr] = rules[idx];
21+
}
22+
// last cell
23+
idx = automaton[nr_cells - 2] << 2 | automaton[nr_cells - 1] << 1 | automaton[0];
24+
ng_automaton[nr_cells - 1] = rules[idx];
25+
return ng_automaton;
26+
}
27+
28+
void AutomatonRunner::evolve(Automaton& automaton, const size_t nr_generations,
29+
GenerationHandler handler) {
30+
if (!handler(automaton)) return;
31+
for (size_t gen_nr = 0; gen_nr < nr_generations; ++gen_nr) {
32+
automaton = next_generation(automaton);
33+
if (!handler(automaton)) return;
34+
}
35+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef AUTOMATON_RUNNER_HDR
2+
#define AUTOMATON_RUNNER_HDR
3+
4+
#include <array>
5+
#include <valarray>
6+
#include <functional>
7+
8+
using Rules = std::array<uint8_t, 8>;
9+
using Automaton = std::valarray<uint8_t>;
10+
using GenerationHandler = std::function<bool(const Automaton&)>;
11+
12+
class AutomatonRunner {
13+
private:
14+
Rules rules;
15+
Automaton next_generation(const Automaton& automaton);
16+
public:
17+
AutomatonRunner(uint8_t rule_nr);
18+
void evolve(Automaton& automaton, const size_t nr_generations,
19+
GenerationHandler handler);
20+
};
21+
22+
#endif

0 commit comments

Comments
 (0)