-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cu
288 lines (210 loc) · 8.17 KB
/
main.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include <Shader.hpp>
#include <GLFW/glfw3.h>
#include <display.hpp>
#include <data.hpp>
#include <cpu.hpp>
#include <gpu.cuh>
#include <getopt.h>
#include <chrono>
#define WIDTH 1920
#define HEIGHT 1080
#define SEED 271828
#define TIME_ITERATIONS 100
#define GPU_FAST_RENDER
#ifdef GPU_FAST_RENDER
#define setDataGPU setDataGPUFast
#else
#define setDataGPU setDataGPUSlow
#endif
void runCPU();
void runGPU();
void timeMeasureCPU(int iterations);
void timeMeasureGPU(int iterations);
int main(int argc, char** argv)
{
int option = 0;
while ((option = getopt(argc, argv, "cgtT")) != -1)
{
switch (option)
{
case 'c':
runCPU();
case 'g':
runGPU();
return 0;
case 't':
timeMeasureCPU(TIME_ITERATIONS);
return 0;
case 'T':
timeMeasureGPU(TIME_ITERATIONS);
return 0;
default:
std::cerr << "Nieznana opcja: " << option << "\n";
return 1;
}
}
std::cerr << "Proszę podać jedną z opcji: -c (CPU), -g (GPU), -t (pomiar czasu dla CPU), -T (pomiar czasu dla GPU))\n";
return 1;
}
GLFWwindow* window;
void framebufferSizeCallback(GLFWwindow* window, int width, int height) { glViewport(0, 0, width, height); }
void initializeGLFWAndGlad() {
glfwInit();
window = glfwCreateWindow(WIDTH, HEIGHT, "GOL & CUDA | Maja Nagarnowicz & Agnieszka Stefankowska", NULL, NULL);
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebufferSizeCallback);
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
}
void initializeShaders() {
Shader shader("../shader.vs", "../shader.fs");
shader.use();
}
void runCPU() {
initializeGLFWAndGlad();
initializeShaders();
Grid<Location::Host> gridA {WIDTH, HEIGHT};
Grid<Location::Host> gridB {WIDTH, HEIGHT};
gridA.randomInit(SEED);
auto *current = &gridA;
auto *next = &gridB;
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT);
TempTexture texture {WIDTH, HEIGHT};
texture.setDataCPU(current->color);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 6);
updateCPU(current, next, WIDTH, HEIGHT);
std::swap(current->color, next->color);
std::swap(current->alive, next->alive);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
}
void runGPU() {
initializeGLFWAndGlad();
initializeShaders();
Grid<Location::Host> gridA {WIDTH, HEIGHT};
Grid<Location::Host> gridB {WIDTH, HEIGHT};
gridA.randomInit(SEED);
auto *current = &gridA;
auto *next = &gridB;
Grid<Location::Device> currentGPU {WIDTH, HEIGHT};
Grid<Location::Device> nextGPU {WIDTH, HEIGHT};
CHECK_CUDA(cudaMemset(nextGPU.color, 0, sizeof(Color) * WIDTH * HEIGHT));
CHECK_CUDA(cudaMemset(nextGPU.alive, 0, sizeof(bool) * WIDTH * HEIGHT));
CHECK_CUDA(cudaMemcpy(currentGPU.color, current->color, sizeof(Color) * WIDTH * HEIGHT, cudaMemcpyDefault));
CHECK_CUDA(cudaMemcpy(currentGPU.alive, current->alive, sizeof(bool) * WIDTH * HEIGHT, cudaMemcpyDefault));
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT);
TempTexture texture {WIDTH, HEIGHT};
texture.setDataGPU(currentGPU.color);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 6);
updateGPU(currentGPU.color, currentGPU.alive, nextGPU.color, nextGPU.alive, WIDTH, HEIGHT);
std::swap(currentGPU.color, nextGPU.color);
std::swap(currentGPU.alive, nextGPU.alive);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
}
void timeMeasureGPU(int numIterations){
initializeGLFWAndGlad();
initializeShaders();
Grid<Location::Host> gridA {WIDTH, HEIGHT};
Grid<Location::Host> gridB {WIDTH, HEIGHT};
gridA.randomInit(SEED);
auto *current = &gridA;
auto *next = &gridB;
Grid<Location::Device> currentGPU {WIDTH, HEIGHT};
Grid<Location::Device> nextGPU {WIDTH, HEIGHT};
CHECK_CUDA(cudaMemset(nextGPU.color, 0, sizeof(Color) * WIDTH * HEIGHT));
CHECK_CUDA(cudaMemset(nextGPU.alive, 0, sizeof(bool) * WIDTH * HEIGHT));
CHECK_CUDA(cudaMemcpy(currentGPU.color, current->color, sizeof(Color) * WIDTH * HEIGHT, cudaMemcpyDefault));
CHECK_CUDA(cudaMemcpy(currentGPU.alive, current->alive, sizeof(bool) * WIDTH * HEIGHT, cudaMemcpyDefault));
float avg_time_gpu = 0.0;
float totalTimeGpu = 0.0;
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
totalTimeGpu = 0;
for (int i = 0; i < numIterations; ++i) {
// Measure the calculation time
cudaEventRecord(start);
updateGPU(currentGPU.color, currentGPU.alive, nextGPU.color, nextGPU.alive, WIDTH, HEIGHT);
cudaEventRecord(stop);
cudaEventSynchronize(stop);
float milliseconds = 0;
cudaEventElapsedTime(&milliseconds, start, stop);
totalTimeGpu += milliseconds;
std::swap(currentGPU.color, nextGPU.color);
std::swap(currentGPU.alive, nextGPU.alive);
glfwSwapBuffers(window);
glfwPollEvents();
}
avg_time_gpu = totalTimeGpu / numIterations;
std::cout << "GPU: średni czas obliczeń dla " << numIterations << " iteracji: " << avg_time_gpu << " ms" << std::endl;
totalTimeGpu = 0;
for(int i = 0; i < numIterations; ++i) {
glClear(GL_COLOR_BUFFER_BIT);
// Measure the rendering time
cudaEventRecord(start);
TempTexture texture {WIDTH, HEIGHT};
texture.setDataGPU(currentGPU.color);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 6);
cudaEventRecord(stop);
cudaEventSynchronize(stop);
float milliseconds = 0;
cudaEventElapsedTime(&milliseconds, start, stop);
totalTimeGpu += milliseconds;
updateGPU(currentGPU.color, currentGPU.alive, nextGPU.color, nextGPU.alive, WIDTH, HEIGHT);
std::swap(currentGPU.color, nextGPU.color);
std::swap(currentGPU.alive, nextGPU.alive);
glfwSwapBuffers(window);
glfwPollEvents();
}
avg_time_gpu = totalTimeGpu / numIterations;
std::cout << "GPU: średni czas renderowania dla " << numIterations << " iteracji: " << avg_time_gpu << " ms" << std::endl;
cudaEventDestroy(start);
cudaEventDestroy(stop);
}
void timeMeasureCPU(int numIterations){
initializeGLFWAndGlad();
initializeShaders();
Grid<Location::Host> gridA {WIDTH, HEIGHT};
Grid<Location::Host> gridB {WIDTH, HEIGHT};
gridA.randomInit(SEED);
auto *current = &gridA;
auto *next = &gridB;
auto totalTime = 0;
for (int i = 0; i < numIterations; ++i) {
// Measure the calculation time
auto start_time = std::chrono::high_resolution_clock::now();
updateCPU(current, next, WIDTH, HEIGHT);
auto end_time = std::chrono::high_resolution_clock::now();
auto time = end_time - start_time;
totalTime += time/std::chrono::milliseconds(1);
std::swap(current->color, next->color);
std::swap(current->alive, next->alive);
}
auto avg_time = totalTime / numIterations;
std::cout << "CPU: średni czas obliczeń dla " << numIterations << " iteracji: " << avg_time << " ms" << std::endl;
for (int i = 0; i < numIterations; ++i) {
glClear(GL_COLOR_BUFFER_BIT);
// Measure the rendering time
auto start_time = std::chrono::high_resolution_clock::now();
TempTexture texture {WIDTH, HEIGHT};
texture.setDataCPU(current->color);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 6);
auto end_time = std::chrono::high_resolution_clock::now();
auto time = end_time - start_time;
totalTime += time/std::chrono::milliseconds(1);
updateCPU(current, next, WIDTH, HEIGHT);
std::swap(current->color, next->color);
std::swap(current->alive, next->alive);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
avg_time = totalTime / numIterations;
std::cout << "CPU: średni czas renderowania dla " << numIterations << " iteracji: " << avg_time << " ms" << std::endl;
}