CellularAutomata
Loading...
Searching...
No Matches
common.hpp
1#pragma once
2
3// macro for registering functions to CUDA (if used)
4#ifdef __CUDACC__
5#define cudaFn __device__ __host__
6#else
7#define cudaFn
8#endif
9
10template <typename T>
18using stateFunc = void (*)(T*, const unsigned int, const unsigned int);
19
20template <typename T>
27using kernelFunc = void (*)(T*, const unsigned int);
28
33{
38 enum Device
39 {
48 };
49
54 template <typename T>
55 class State
56 {
57 public:
61 T* curr;
62
66 T* next;
67
71 const unsigned int height;
72
76 const unsigned int width;
77
82
92 State(const unsigned int height, const unsigned int width, stateFunc<T> fn, Device device);
93
97 ~State();
98
104 void copyTo(State<T>* to);
105
109 void swap();
110
114 void print();
115
116 private:
117 void initCPU(stateFunc<T> fn);
118 void initCUDA(stateFunc<T> fn);
119 void freeCPU();
120 void freeCUDA();
121 };
122
128 template <typename T>
129 class Kernel
130 {
131 public:
139 const unsigned int size;
144
153 Kernel(const unsigned int size, kernelFunc<T> fn, Device device);
154
158 ~Kernel();
159
165 void copyTo(Kernel<T>* to);
166
170 void print();
171
172 private:
173 void initCPU(kernelFunc<T> fn);
174 void initCUDA(kernelFunc<T> fn);
175 void freeCPU();
176 void freeCUDA();
177 };
178
179}
180
181#include "common.inl"
2D convolution kernel
Definition common.hpp:130
const Device device
Computational device.
Definition common.hpp:143
~Kernel()
Frees dynamically allocated memory.
Definition common.inl:212
void print()
Prints the kernel array.
Definition common.inl:258
T * kernel
2D Matrix
Definition common.hpp:135
void copyTo(Kernel< T > *to)
Copy the own kernel array to another kernel array across devices.
Definition common.inl:239
const unsigned int size
Side length of matrix.
Definition common.hpp:139
2D buffered matrices
Definition common.hpp:56
const unsigned int width
Width of 2D Matrix.
Definition common.hpp:76
T * curr
Current 2D Matrix buffer.
Definition common.hpp:61
void swap()
Swaps the current- and next state buffer.
Definition common.inl:139
void copyTo(State< T > *to)
Copy the own current state buffer to another current state buffer across devices.
Definition common.inl:120
void print()
Prints the current state buffer.
Definition common.inl:147
const unsigned int height
Height of 2D Matrix.
Definition common.hpp:71
const Device device
Computational device.
Definition common.hpp:81
T * next
Next 2D Matrix buffer.
Definition common.hpp:66
~State()
Frees dynamically allocated memory.
Definition common.inl:88
Main namespace.
Definition common.hpp:33
Device
Computational device.
Definition common.hpp:39
@ CPU
Allocation & computation on CPU/RAM.
Definition common.hpp:43
@ CUDA
Allocation & computation on NVIDIA GPU via CUDA.
Definition common.hpp:47