-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpu.cuh
52 lines (45 loc) · 1.84 KB
/
gpu.cuh
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
#pragma once
#include <cuda_runtime.h>
#include <data.hpp>
__global__ void updateGPU_kernel(Color* currentColor, bool* currentAlive, Color* nextColor, bool* nextAlive, int width, int height)
{
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width && y < height) {
float r = 0.0f;
float g = 0.0f;
float b = 0.0f;
int aliveNeighbours = 0;
for (int dx = -1; dx <= 1; ++dx) {
for (int dy = -1; dy <= 1; ++dy) {
if (dx == 0 && dy == 0) {
continue;
}
int newX = (x + dx + width) % width;
int newY = (y + dy + height) % height;
if (currentAlive[newY * width + newX]) {
r += currentColor[newY * width + newX].r;
g += currentColor[newY * width + newX].g;
b += currentColor[newY * width + newX].b;
++aliveNeighbours;
}
}
}
bool alive = currentAlive[y * width + x]
? (aliveNeighbours == 2 || aliveNeighbours == 3)
: (aliveNeighbours == 3);
nextAlive[y * width + x] = alive;
if (alive) {
nextColor[y * width + x] = {r / aliveNeighbours, g / aliveNeighbours, b / aliveNeighbours};
}
else {
nextColor[y * width + x] = {0, 0, 0};
}
}
}
void updateGPU(Color* currentColor, bool* currentAlive, Color* nextColor, bool* nextAlive, int width, int height) {
dim3 blockSize(16, 16);
dim3 gridSize((width + blockSize.x - 1) / blockSize.x, (height + blockSize.y - 1) / blockSize.y);
updateGPU_kernel<<<gridSize, blockSize>>>(currentColor, currentAlive, nextColor, nextAlive, width, height);
CHECK_CUDA(cudaDeviceSynchronize());
}