2#include "exceptions.hpp"
5#include "../cuda/cuda.cuh"
11#define stateSize height * width * sizeof(T)
12#define kernelSize size * size * sizeof(T)
15inline void print2D(T* array,
const unsigned int h,
const unsigned int w)
17 for (
int y = 0; y < h; y++)
19 for (
int x = 0; x < w; x++)
21 std::cout << array[y * w + x] <<
" ";
23 std::cout << std::endl;
30 State<T>::State(
const unsigned int height,
const unsigned int width, stateFunc<T> fn,
Device device) : height(height), width(width), device(device)
45 curr =
new T[height * width]();
46 next =
new T[height * width]();
49 fn(curr, height, width);
53 void State<T>::initCUDA(stateFunc<T> fn)
55 size_t bytes = stateSize;
61 CellularAutomata::cuda::allocateCUDA(
reinterpret_cast<void**
>(&curr), bytes);
62 CellularAutomata::cuda::allocateCUDA(
reinterpret_cast<void**
>(&next), bytes);
66 T* temp =
new T[height * width]();
68 fn(temp, height, width);
83 fn(curr, height, width);
104 template <
typename T>
105 void State<T>::freeCUDA()
109 CellularAutomata::cuda::freeCUDA(curr);
110 CellularAutomata::cuda::freeCUDA(next);
119 template <
typename T>
128 size_t bytes = stateSize;
129 CellularAutomata::cuda::copyCUDA(curr, device, to->
curr, to->
device, bytes);
134 std::copy(curr, curr + height * width, to->
curr);
138 template <
typename T>
146 template <
typename T>
152 size_t bytes = stateSize;
153 T* temp =
new T[height * width]();
154 CellularAutomata::cuda::copyCUDA(curr, device, temp,
Device::CPU, bytes);
156 print2D(temp, height, width);
162 print2D(curr, height, width);
167 template <
typename T>
184 template <
typename T>
187 kernel =
new T[size * size]();
193 template <
typename T>
194 void Kernel<T>::initCUDA(kernelFunc<T> fn)
196 size_t bytes = kernelSize;
198 CellularAutomata::cuda::allocateCUDA(
reinterpret_cast<void**
>(&kernel), bytes);
202 T* temp =
new T[size * size]();
211 template <
typename T>
226 template <
typename T>
232 template <
typename T>
233 void Kernel<T>::freeCUDA()
235 CellularAutomata::cuda::freeCUDA(kernel);
238 template <
typename T>
241 if (size != to->
size)
247 size_t bytes = kernelSize;
248 CellularAutomata::cuda::copyCUDA(kernel, device, to->
kernel, to->
device, bytes);
253 std::copy(kernel, kernel + size * size, to->
kernel);
257 template <
typename T>
262 size_t bytes = kernelSize;
263 T* temp =
new T[size * size]();
264 CellularAutomata::cuda::copyCUDA(kernel, device, temp,
Device::CPU, bytes);
266 print2D(temp, size, size);
272 print2D(kernel, size, size);
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
Kernel(const unsigned int size, kernelFunc< T > fn, Device device)
Initalizes a kernel.
Definition common.inl:168
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
State(const unsigned int height, const unsigned int width, stateFunc< T > fn, Device device)
Initalizes a state.
Definition common.inl:30
~State()
Frees dynamically allocated memory.
Definition common.inl:88
Requested device not available.
Definition exceptions.hpp:31
Shape of two matrices are not equal.
Definition exceptions.hpp:41
void allocateHost(void **arrayPtr, size_t bytes)
Allocated memory on the host device (CPU)
Definition cuda.inl:150
void freeHost(void *array)
Frees memory on the host device (CPU)
Definition cuda.inl:160
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