-
Notifications
You must be signed in to change notification settings - Fork 0
/
mandelbrot_gpu.cu
319 lines (268 loc) · 8.24 KB
/
mandelbrot_gpu.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include <stdio.h>
#include <time.h>
#include <SDL2/SDL.h>
#define SIZE 1024
unsigned char pixels[SIZE * SIZE * 3];
unsigned char getColorR(int t){
int t_i = (t/60) % 6;
float f = t/60.0 - t_i;
float s = 1.0;
float v = 1-t/255;
int8_t l = v * (1-s) * 255;
int8_t m = v * (1-f*s) * 255;
int8_t n = v * (1-(1-f)*s) * 255;
switch (t_i){
case 0:
return v*255;
case 1:
return m;
case 2:
return l;
case 3:
return l;
case 4:
return n;
case 5:
return v*255;
}
return 0;
}
unsigned char getColorG(int t){
int t_i = (t/60) % 6;
float f = t/60.0 - t_i;
float s = 1.0;
float v = 1-t/255;
int8_t l = v * (1-s) * 255;
int8_t m = v * (1-f*s) * 255;
int8_t n = v * (1-(1-f)*s) * 255;
switch (t_i){
case 0:
return n;
case 1:
return v*255;
case 2:
return v*255;
case 3:
return m;
case 4:
return l;
case 5:
return l;
}
return 0;
}
unsigned char getColorB(int t){
int t_i = (t/60) % 6;
float f = t/60.0 - t_i;
float s = 1.0;
float v = 1-t/255;
int8_t l = v * (1-s) * 255;
int8_t m = v * (1-f*s) * 255;
int8_t n = v * (1-(1-f)*s) * 255;
switch (t_i){
case 0:
return l;
case 1:
return l;
case 2:
return n;
case 3:
return v*255;
case 4:
return v*255;
case 5:
return m;
}
return 0;
}
void render(SDL_Renderer* pRenderer, int *T_ALL){
for (int i=0; i < SIZE; i++){
for (int j=0; j < SIZE; j++){
pixels[(j*SIZE+i)*3] = getColorR(T_ALL[(j*SIZE+i)]);
pixels[(j*SIZE+i)*3 + 1] = getColorG(T_ALL[(j*SIZE+i)]);
pixels[(j*SIZE+i)*3 + 2] = getColorB(T_ALL[(j*SIZE+i)]);
if (T_ALL[(j*SIZE+i)] < 0){
pixels[(j*SIZE+i)*3] = getColorR(255);
pixels[(j*SIZE+i)*3 + 1] = getColorG(255);
pixels[(j*SIZE+i)*3 + 2] = getColorB(255);
}
}
}
SDL_Rect texture_rect;
texture_rect.x=0; texture_rect.y=0; texture_rect.w=SIZE; texture_rect.h=SIZE;
SDL_Surface *surface = SDL_CreateRGBSurfaceFrom((void*)pixels,
SIZE,
SIZE,
3 * 8, // bits per pixel = 24
SIZE * 3, // pitch
0x0000FF, // red mask
0x00FF00, // green mask
0xFF0000, // blue mask
0); // alpha mask (none)
SDL_Texture *texture = SDL_CreateTextureFromSurface(pRenderer, surface);
SDL_RenderClear(pRenderer);
SDL_RenderCopy(pRenderer, texture, NULL, &texture_rect);
SDL_RenderPresent(pRenderer);
}
__device__ double R_F(int POWER, double a, double r, double c){
switch (POWER){
case 2:
return a + r*r - c*c;
case 3:
return a + r*r*r - 3*r*c*c;
case 4:
return a + r*r*r*r - 6*r*r*c*c + c*c*c*c;
default:
return 0;
}
}
__device__ double I_F(int POWER, double b, double r, double c){
switch (POWER){
case 2:
return b + 2*r*c;
case 3:
return b + 3*r*r*c - c*c*c + b;
case 4:
return b - 4*r*c*c*c + 4*r*r*r*c;
default:
return 0;
}
}
__global__ void JULIA_GPU(int *T, double size, double centerX, double centerY, int POWER){
int i = blockIdx.x*blockDim.x+threadIdx.x;
int j = blockIdx.y*blockDim.y+threadIdx.y;
double c_r = centerX;
double c_c = centerY;
double z_r = size*(i*1.0/SIZE - 0.5);
double z_c = size*(j*1.0/SIZE - 0.5);
double tmp = 0.0;
int n = 0;
while (n < 255 && (z_r*z_r + z_c*z_c) < 4){
tmp = R_F(POWER, c_r, z_r, z_c);
z_c = I_F(POWER, c_c, z_r, z_c);
z_r = tmp;
n++;
}
T[j*SIZE+i] = n;
}
__global__ void MANDELBROT_GPU(int *T, double size, double centerX, double centerY, int POWER){
int i = blockIdx.x*blockDim.x+threadIdx.x;
int j = blockIdx.y*blockDim.y+threadIdx.y;
double c_r = size*(i*1.0/SIZE - 0.5) + centerX;
double c_c = size*(j*1.0/SIZE - 0.5) + centerY;
double z_r = 0.0, tmp = 0.0, z_c = 0.0;
int n = 0;
while (n < 255 && (z_r*z_r + z_c*z_c) < 4){
tmp = R_F(POWER, c_r, z_r, z_c);
z_c = I_F(POWER, c_c, z_r, z_c);
z_r = tmp;
n++;
}
T[j*SIZE+i] = n;
}
int main(int argc, char** argv){
int *T, *d_T;
int POWER = 2;
if (argc == 2){
int p = argv[1][0] - '0';
if (p >= 2 && p <= 4){
POWER = p;
}
}
dim3 numBlocks(32, 32);
dim3 threadsPerBlock(32, 32);
clock_t t1, t2, t;
T = (int*)malloc(SIZE*SIZE*sizeof(int));
cudaMalloc(&d_T, SIZE*SIZE*sizeof(int));
for (int i = 0; i < SIZE*SIZE; i++) {
T[i] = 1;
}
cudaMemcpy(d_T, T, SIZE*SIZE*sizeof(int), cudaMemcpyHostToDevice);
MANDELBROT_GPU<<<numBlocks, threadsPerBlock>>>(d_T, 3.0, 0.0, 0.0, POWER);
cudaMemcpy(T, d_T, SIZE*SIZE*sizeof(int), cudaMemcpyDeviceToHost);
SDL_Window* pWindow = NULL;
SDL_Renderer* pRenderer = NULL;
if(SDL_Init(SDL_INIT_VIDEO) < 0){
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "[debug] %s", SDL_GetError());
return 0;
}
pWindow = SDL_CreateWindow("SDL Programme", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SIZE, SIZE, SDL_WINDOW_SHOWN);
if (pWindow == NULL){
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "[DEBUG] > %s", SDL_GetError());
SDL_Quit();
return 0;
}
pRenderer = SDL_CreateRenderer(pWindow, -1, SDL_RENDERER_ACCELERATED);
if (pRenderer == NULL){
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "[DEBUG] > %s", SDL_GetError());
SDL_Quit();
return 0;
}
SDL_Event events;
int xMouse, yMouse;
double cx, cy, size=3.0, old_cx=0.0, old_cy=0.0;
render(pRenderer, T);
unsigned hold=0, isOpen=1, isMandelbrot=1;
while (isOpen){
if (hold && !isMandelbrot){
SDL_GetMouseState(&xMouse,&yMouse);
cx = size*(xMouse*1.0/SIZE - 0.5) + old_cx;
cy = size*(yMouse*1.0/SIZE - 0.5) + old_cy;
JULIA_GPU<<<numBlocks, threadsPerBlock>>>(d_T, 3., cx, cy, POWER);
cudaMemcpy(T, d_T, SIZE*SIZE*sizeof(int), cudaMemcpyDeviceToHost);
render(pRenderer, T);
}
while (SDL_PollEvent(&events)) {
switch (events.type) {
case SDL_QUIT:
isOpen = 0;
break;
case SDL_KEYDOWN:
isMandelbrot = 1-isMandelbrot;
break;
case SDL_MOUSEBUTTONDOWN:
if (isMandelbrot){
SDL_GetMouseState(&xMouse,&yMouse);
size /= 2;
cx = size*(xMouse*1.0/SIZE - 0.5)+old_cx;
cy = size*(yMouse*1.0/SIZE - 0.5)+old_cy;
if (events.button.button == SDL_BUTTON_RIGHT){
size = 3.;
cx = 0.;
cy = 0.;
}
t1 = clock();
MANDELBROT_GPU<<<numBlocks, threadsPerBlock>>>(d_T, size, cx, cy, POWER);
cudaMemcpy(T, d_T, SIZE*SIZE*sizeof(int), cudaMemcpyDeviceToHost);
t2 = clock();
t = t2-t1;
printf("Temps Mandelbrot MPI : %i ms\n", (int) (t * 1000 / CLOCKS_PER_SEC));
render(pRenderer, T);
old_cx = cx;
old_cy = cy;
}
else{
hold = 1;
}
break;
case SDL_MOUSEBUTTONUP:
if (!isMandelbrot){
t1 = clock();
MANDELBROT_GPU<<<numBlocks, threadsPerBlock>>>(d_T, size, old_cx, old_cy, POWER);
cudaMemcpy(T, d_T, SIZE*SIZE*sizeof(int), cudaMemcpyDeviceToHost);
t2 = clock();
t = t2-t1;
printf("Temps Mandelbrot MPI : %i ms\n", (int) (t * 1000 / CLOCKS_PER_SEC));
render(pRenderer, T);
hold = 0;
}
break;
}
}
}
SDL_DestroyRenderer(pRenderer);
SDL_DestroyWindow(pWindow);
SDL_Quit();
cudaFree(d_T);
free(T);
}