-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.hpp
69 lines (59 loc) · 1.39 KB
/
data.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#pragma once
#include <cuda_runtime.h>
#include <utils.hpp>
struct Color {
float r;
float g;
float b;
float a = {1.0f};
};
enum Location
{
Host,
Device
};
template<Location location>
struct Grid
{
bool* alive;
Color* color;
int width, height;
Grid(int width, int height) {
this->width = width;
this->height = height;
if (location == Location::Device) {
CHECK_CUDA(cudaMalloc(&color, sizeof(Color) * width * height));
CHECK_CUDA(cudaMalloc(&alive, sizeof(bool) * width * height));
} else {
color = new Color[width * height];
alive = new bool[width * height];
}
}
~Grid() {
if (location == Location::Device) {
CHECK_CUDA(cudaFree(color));
CHECK_CUDA(cudaFree(alive));
} else {
delete[] color;
delete[] alive;
}
}
void randomInit(int seed){
srand(seed);
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
alive[y * width + x] = rand() % 2;
color[y * width + x] = {
(rand() % 255) / 255.0f,
(rand() % 255) / 255.0f,
(rand() % 255) / 255.0f
};
}
}
}
};
template <Location location>
struct GridPointers {
Grid<location>* current;
Grid<location>* next;
};