CellularAutomata
Loading...
Searching...
No Matches
cpu.inl
1#include "cpu.hpp"
2
3namespace CellularAutomata
4{
5 namespace cpu
6 {
7 template <typename T, typename Activation>
8 void epoch(const T* input, const T* kernel, T* output, Activation fn,
9 const unsigned int h, const unsigned int w, const unsigned int s, const bool r)
10 {
11 int kernel_radius = s / 2;
12
13 #pragma omp parallel for
14 // iterate over state array
15 for (int row = 0; row < h; row++)
16 {
17 // definitons inside first loop for parallelization
18 int array_y = 0;
19 int array_x = 0;
20 float sum = 0.0f;
21
22 for (int col = 0; col < w; col++)
23 {
24
25 // iterate over kernel
26 for (int y = 0; y < s; y++)
27 {
28 for (int x = 0; x < s; x++)
29 {
30 // calculate state array positions
31 array_y = row + (y - kernel_radius);
32 array_x = col + (x - kernel_radius);
33
34 if (r)
35 {
36 // overflow y
37 if (array_y < 0)
38 array_y += h;
39 else if (array_y >= h)
40 array_y -= h;
41 // overflow x
42 if (array_x < 0)
43 array_x += w;
44 else if (array_x >= w)
45 array_x -= w;
46 }
47 else
48 {
49 // overflow on y or x
50 if (array_y < 0 || array_y >= h || array_x < 0 || array_x >= w)
51 continue; // add nothing
52 }
53
54 // State x Kernel
55 sum += input[array_y * w + array_x] * kernel[y * s + x];
56 }
57 }
58
59 // Set new value
60 output[row * w + col] = fn(sum);
61 sum = 0.0f;
62 }
63 }
64 }
65 } // namespace CPU
66} // namespace CellularAutomata
Main namespace.
Definition common.hpp:33