-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathca.cu
188 lines (159 loc) · 6.1 KB
/
ca.cu
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <iostream>
#include <stdlib.h>
#include <thread>
#include <time.h>
#define BLOCK_SIZE 16
// wrap function for device code
__device__ int wrap_d(int N, int idx) {
if (idx <= 0) {
return N - 1;
} else if (idx >= N - 1) {
return 0;
}
return idx;
}
__global__ void updateUniverseKernel(const int width, const int height,
const int *const oldUniverse,
int *const newUniverse) {
int i = (blockIdx.y * blockDim.y) + threadIdx.y;
int j = (blockIdx.x * blockDim.x) + threadIdx.x;
int numNeighbors =
oldUniverse[wrap_d(height, i - 1) * width + wrap_d(width, j - 1)] +
oldUniverse[wrap_d(height, i - 1) * width + wrap_d(width, j)] +
oldUniverse[wrap_d(height, i - 1) * width + wrap_d(width, j + 1)] +
oldUniverse[wrap_d(height, i) * width + wrap_d(width, j - 1)] +
oldUniverse[wrap_d(height, i) * width + wrap_d(width, j + 1)] +
oldUniverse[wrap_d(height, i + 1) * width + wrap_d(width, j - 1)] +
oldUniverse[wrap_d(height, i + 1) * width + wrap_d(width, j)] +
oldUniverse[wrap_d(height, i + 1) * width + wrap_d(width, j + 1)];
if (oldUniverse[i * width + j] == 1 &&
(numNeighbors <= 1 || numNeighbors >= 4)) { // on
newUniverse[i * width + j] = 0;
} else if (numNeighbors == 3 && oldUniverse[i * width + j] == 0) { // off
newUniverse[i * width + j] = 1;
} else {
newUniverse[i * width + j] = oldUniverse[i * width + j];
}
}
// N is assumed to be a multiple of BLOCK_SIZE
void updateUniverseGPU(const int width, const int height,
const int *const oldUniverse, int *const newUniverse) {
dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE);
dim3 dimGrid(width / dimBlock.x, height / dimBlock.y);
updateUniverseKernel<<<dimGrid, dimBlock>>>(width, height, oldUniverse,
newUniverse);
cudaDeviceSynchronize();
}
// wrap function for cpu code
int wrap(int N, int idx) {
if (idx <= 0) {
return N - 1;
} else if (idx >= N - 1) {
return 0;
}
return idx;
}
void updateUniverseCPU(const int width, const int height,
const int *const oldUniverse, int *const newUniverse) {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int numNeighbors =
oldUniverse[wrap(height, i - 1) * width + wrap(width, j - 1)] +
oldUniverse[wrap(height, i - 1) * width + wrap(width, j)] +
oldUniverse[wrap(height, i - 1) * width + wrap(width, j + 1)] +
oldUniverse[wrap(height, i) * width + wrap(width, j - 1)] +
oldUniverse[wrap(height, i) * width + wrap(width, j + 1)] +
oldUniverse[wrap(height, i + 1) * width + wrap(width, j - 1)] +
oldUniverse[wrap(height, i + 1) * width + wrap(width, j)] +
oldUniverse[wrap(height, i + 1) * width + wrap(width, j + 1)];
if (oldUniverse[i * width + j] == 1 &&
(numNeighbors <= 1 || numNeighbors >= 4)) { // on
newUniverse[i * width + j] = 0;
} else if (numNeighbors == 3 && oldUniverse[i * width + j] == 0) { // off
newUniverse[i * width + j] = 1;
} else {
newUniverse[i * width + j] = oldUniverse[i * width + j];
}
}
}
}
// Randomly add some cells that are living at the beginning
void populateUniverse(int width, int height, int *universe) {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if ((rand() % 3) == 0) {
universe[i * width + j] = 1;
} else {
universe[i * width + j] = 0;
}
}
}
}
void printUniverse(const int width, const int height, int *universe) {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
std::cout << (universe[i * width + j] == 1 ? '#' : ' ');
}
std::cout << std::endl;
}
std::cout << std::endl;
}
void profileRuntimeCPUvsGPU(const int width, const int height, int *&universe,
int *&universeTemp, const int numIterations) {
clock_t t;
t = clock();
for (int i = 0; i < numIterations; i++) {
updateUniverseCPU(width, height, universeTemp, universe);
std::swap(universe, universeTemp);
}
t = clock() - t;
float CPU_runtime_ms = (((float)t) / CLOCKS_PER_SEC) * 1000;
t = clock();
for (int i = 0; i < numIterations; i++) {
updateUniverseGPU(width, height, universeTemp, universe);
std::swap(universe, universeTemp);
}
t = clock() - t;
float GPU_runtime_ms = (((float)t) / CLOCKS_PER_SEC) * 1000;
std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
std::cout << "CPU Runtime: " << CPU_runtime_ms << " ms" << std::endl;
std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
std::cout << "GPU Runtime: " << GPU_runtime_ms << " ms" << std::endl;
std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
}
void runWithOutput(const int width, const int height, int *&universe,
int *&universeTemp, const int numIterations) {
for (int i = 0; i < numIterations; i++) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
system("clear");
printUniverse(width, height, universeTemp);
updateUniverseGPU(width, height, universeTemp, universe);
std::swap(universe, universeTemp);
}
}
int main() {
int height = BLOCK_SIZE * 8;
int width = BLOCK_SIZE * 32;
int *universe;
int *universeTemp;
int numIterations = 0;
std::string choice = "";
srand(time(NULL));
cudaMallocManaged(&universe, width * height * sizeof(int));
cudaMallocManaged(&universeTemp, width * height * sizeof(int));
populateUniverse(width, height, universeTemp);
std::cout << "Enter P for profiling mode and O for output mode(O/P): ";
std::cin >> choice;
std::cout << "Enter number of iterations: ";
std::cin >> numIterations;
std::cout << "Starting..." << std::endl;
if (choice == "P" || choice == "p") {
profileRuntimeCPUvsGPU(width, height, universe, universeTemp,
numIterations);
} else {
runWithOutput(width, height, universe, universeTemp, numIterations);
}
cudaFree(universe);
cudaFree(universeTemp);
return 0;
}