/
mandel_classic.c
549 lines (461 loc) · 14.5 KB
/
mandel_classic.c
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <math.h>
#include <time.h>
#include <SDL.h>
#include <SDL_ttf.h>
#define MAX_SOURCE_SIZE (0x100000)
#ifdef CACHE
int** cached_points;
int** cached_x;
int** cached_y;
#endif
int *iteration_pixels;
typedef struct point_args point_args;
struct point_args
{
int res_x;
int res_y;
int image_x;
int image_y;
float zoom;
int max_iteration;
int thread_number;
};
typedef struct piece_args piece_args;
struct piece_args
{
int res_x;
int res_y;
float zoom;
int max_iteration;
int total_threads;
int thread_number;
int julia_mode;
};
int get_x (int linear_point, int width)
{
return linear_point % width;
}
int get_y (int linear_point, int height)
{
return floor(linear_point / height);
}
float map_x_mandelbrot(int x, int width, float zoom)
{
return (((float)x / (float)width) * (3.5 * zoom)) - (2.5 - (1.0 - zoom));
return (((float)x / (float)width) * (3.5 * zoom)) - (1.75 - (1.0 - zoom));
}
float map_x_julia(int x, int width, float zoom)
{
return (((float)x / (float)width) * (3.5 * zoom)) - (1.75 - (1.0 - zoom));
}
float map_y(int y, int height, float zoom)
{
return (((float)y / (float)height) * (2.0 * zoom)) - (1.00001 - (1.0 - zoom));
}
#ifdef CACHE
int cached_iteration(float pos_x, float pos_y)
{
float centered_x = pos_x + 2.5;
float centered_y = pos_y + 1.0;
float temp_x = floor(centered_x * 1000.0);
float temp_y = floor(centered_y * 1000.0);
int trs_pos_x = (int)temp_x;
int trs_pos_y = (int)temp_y;
return cached_points[trs_pos_x][trs_pos_y];
}
float get_cached_x(float pos_x, float pos_y)
{
float centered_x = pos_x + 2.5;
float centered_y = pos_y + 1.0;
float temp_x = floor(centered_x * 1000.0);
float temp_y = floor(centered_y * 1000.0);
int trs_pos_x = (int)temp_x;
int trs_pos_y = (int)temp_y;
return cached_x[trs_pos_x][trs_pos_y];
}
float get_cached_y(float pos_x, float pos_y)
{
float centered_x = pos_x + 2.5;
float centered_y = pos_y + 1.0;
float temp_x = floor(centered_x * 1000.0);
float temp_y = floor(centered_y * 1000.0);
int trs_pos_x = (int)temp_x;
int trs_pos_y = (int)temp_y;
return cached_y[trs_pos_x][trs_pos_y];
}
void store_iteration(float pos_x, float pos_y, int iteration, float x, float y)
{
float centered_x = pos_x + 2.5;
float centered_y = pos_y + 1.0;
float temp_x = floor(centered_x * 1000.0);
float temp_y = floor(centered_y * 1000.0);
int trs_pos_x = (int)temp_x;
int trs_pos_y = (int)temp_y;
cached_points[trs_pos_x][trs_pos_y] = iteration;
cached_x[trs_pos_x][trs_pos_y] = x;
cached_y[trs_pos_x][trs_pos_y] = y;
}
#endif
int mandelbrot_point(int res_x, int res_y, int image_x, int image_y, float zoom, int max_iteration)
{
// Get the index of the current element
float pos_x = map_x_mandelbrot(image_x, res_x, zoom);
float pos_y = map_y(image_y, res_y, zoom);
float x = 0.0;
float y = 0.0;
float q, x_term, pos_y2;
float xtemp, xx, yy, xplusy;
#ifdef CACHE
int storeable = 1;
#endif
int iteration = 0;
// Period-2 bulb check
x_term = pos_x + 1.0;
pos_y2 = pos_y * pos_y;
if ((x_term * x_term + pos_y2) < 0.0625) return 0;
// Cardioid check
x_term = pos_x - 0.25;
q = x_term * x_term + pos_y2;
q = q * (q + x_term);
if (q < (0.25 * pos_y2)) return 0;
#ifdef CACHE
// Look up our cache
iteration = cached_iteration(pos_x, pos_y);
if (iteration > 0)
{
x = get_cached_x(pos_x, pos_y);
y = get_cached_y(pos_x, pos_y);
yy = y * y;
}
if (iteration < 0) storeable = 0;
#endif
while (iteration < max_iteration)
{
xx = x * x;
yy = y * y;
xplusy = x + y;
if ((xx) + (yy) > (4.0)) break;
y = xplusy * xplusy - xx - yy;
y = y + pos_y;
xtemp = xx - yy + pos_x;
x = xtemp;
iteration++;
}
if (iteration >= max_iteration)
{
return 0;
}
else
{
#ifdef CACHE
if (storeable == 1)
{
store_iteration(pos_x, pos_y, iteration, x, y);
}
#endif
return iteration;
}
}
int julia_point(int res_x, int res_y, int image_x, int image_y, float zoom, int max_iteration)
{
// Get the index of the current element
float pos_x = map_x_julia(image_x, res_x, 1.0);
float pos_y = map_y(image_y, res_y, 1.0);
float x = pos_x;
float y = pos_y;
float xtemp, xx, yy;
#ifdef CACHE
int storeable = 1;
#endif
int iteration = 0;
#ifdef CACHE
// Look up our cache
iteration = cached_iteration(pos_x, pos_y);
if (iteration > 0)
{
x = get_cached_x(pos_x, pos_y);
y = get_cached_y(pos_x, pos_y);
yy = y * y;
}
if (iteration < 0) storeable = 0;
#endif
while (iteration < max_iteration)
{
xx = x * x;
yy = y * y;
if ((xx) + (yy) > (4.0)) break;
y = pow((x + y), 2) - xx - yy;
y = y + 0.288;
xtemp = xx - yy + 0.353 + zoom;
x = xtemp;
iteration++;
}
if (iteration >= max_iteration)
{
return 0;
}
else
{
#ifdef CACHE
if (storeable == 1)
{
store_iteration(pos_x, pos_y, iteration, x, y);
}
#endif
return iteration;
}
}
// Splits the image in pieces and calls the corresponding algorithm
void *thread_launcher(void *arguments)
{
piece_args *args;
args = (piece_args *) arguments;
int x,y, small_res_x, small_res_y, init_x, init_y, limit_x, limit_y;
int split, piece_x, piece_y;
if(args->total_threads > 2)
{
split = sqrt(args->total_threads);
}
else if (args->total_threads == 2)
{
split = 2;
}
else
{
split = 1;
}
if (args->thread_number > 0)
{
piece_x = args->thread_number % split;
piece_y = floor((float)args->thread_number / (float)split);
}
else
{
piece_x = 0;
piece_y = 0;
}
small_res_x = floor((float)args->res_x / (float)split);
small_res_y = floor((float)args->res_y / (float)split);
init_x = small_res_x * piece_x;
init_y = small_res_y * piece_y;
limit_x = init_x + small_res_x;
limit_y = init_y + small_res_y;
for (y = init_y; y < limit_y; y++)
{
for (x = init_x; x < limit_x; x++)
{
if(args->julia_mode == 0)
iteration_pixels[x + (y * args->res_x)] = mandelbrot_point(args->res_x, args->res_y, x, y, args->zoom, args->max_iteration);
else
iteration_pixels[x + (y * args->res_x)] = julia_point(args->res_x, args->res_y, x, y, args->zoom, args->max_iteration);
}
}
}
int get_cpus()
{
int number_of_cores = 0;
number_of_cores = sysconf(_SC_NPROCESSORS_ONLN);
return number_of_cores;
}
int main(int argn, char **argv)
{
// Init SDL
if(SDL_Init(SDL_INIT_VIDEO) != 0)
fprintf(stderr, "Could not initialize SDL2: %s\n", SDL_GetError());
printf("SDL Initialized\n");
// Create screen texture
SDL_Surface *screen, *message;
int res_x = 800;
int res_y = 600;
int julia_mode = 0;
if(argn == 1)
{
julia_mode = 0;
}
else if ((argn == 2) && (strcmp(argv[1], "-julia") == 0))
{
julia_mode = 1;
printf("Julia mode activated.\n");
}
int number_cores = get_cpus();
int number_threads = number_cores * number_cores;
printf("Number of CPUs/cores autodetected: %d\n", number_cores);
#ifdef CACHE
// Init our cached points
cached_points = malloc(res_y * 1000 * sizeof(int *));
cached_x = malloc(res_y * 1000 * sizeof(float *));
cached_y = malloc(res_y * 1000 * sizeof(float *));
if (cached_points == NULL)
{
fprintf(stderr, "Bad luck, out of memory\n");
return 2;
}
int count;
for (count = 0; count < res_y * 1000; count++)
{
cached_points[count] = malloc(res_x * 1000 * sizeof(int));
if(cached_points[count] == NULL)
{
fprintf(stderr, "Bad luck, out of memory\n");
return 2;
}
cached_x[count] = malloc(res_x * 1000 * sizeof(float));
cached_y[count] = malloc(res_x * 1000 * sizeof(float));
/*for (count2 = 0; count2 < res_x * 100; count2++)
{
cached_points[count][count2] = -1;
}*/
}
printf("Cache ready\n");
#endif
// screen = SDL_SetVideoMode(res_x, res_y, 0, SDL_HWSURFACE|SDL_DOUBLEBUF);
// screen = SDL_SetVideoMode(res_x, res_y, 0, SDL_DOUBLEBUF);
SDL_Window *window = SDL_CreateWindow("MandelClassic",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
res_x, res_y, 0);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
if ((!window) || (!renderer))
fprintf(stderr,"Could not set video mode: %s\n",SDL_GetError());
// Blank the window
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
SDL_Texture *texture_screen = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
res_x, res_y);
screen = SDL_CreateRGBSurface(0, res_x, res_y , 32, 0, 0, 0, 0);
//Initialize SDL_ttf
if( TTF_Init() == -1 )
{
printf("Error setting up TTF module.\n");
return 1;
}
// Load a font
TTF_Font *font;
font = TTF_OpenFont("font.ttf", 24);
if (font == NULL)
{
printf("TTF_OpenFont() Failed: %s", TTF_GetError());
SDL_Quit();
return 1;
}
//The color of the font
SDL_Color textColor = { 255, 255, 255 };
// Prepare the resolution and sizes and colors, threads...
iteration_pixels = malloc(res_x * res_y * sizeof(int));
pthread_t threads[number_threads];
piece_args arguments[number_threads];
printf("Rendering...\n");
float zoom = 1.0;
float stop_point;
if (julia_mode == 0)
stop_point = 0.00001;
else
stop_point = -2.5;
// We measure the time to do the zooming
clock_t start = clock();
while(zoom > stop_point)
{
int iteration, max_iteration, x, y, res;
if((zoom < -0.02) && (zoom > -1.0))
{
max_iteration = 100;
}
else
{
max_iteration = 170;
}
int thread_count;
for(thread_count = 0; thread_count < number_threads; thread_count++)
{
arguments[thread_count].res_x = res_x;
arguments[thread_count].res_y = res_y;
arguments[thread_count].zoom = zoom;
arguments[thread_count].max_iteration = max_iteration;
arguments[thread_count].total_threads = number_threads;
arguments[thread_count].thread_number = thread_count;
arguments[thread_count].julia_mode = julia_mode;
pthread_create( &threads[thread_count], NULL, thread_launcher, (void*) &arguments[thread_count]);
}
for(thread_count = 0; thread_count < number_threads; thread_count++)
{
res = pthread_join(threads[thread_count], NULL);
if (res != 0)
{
printf("Error in %d thread\n", thread_count);
}
}
int rank;
Uint32 *pixel;
rank = screen->pitch/sizeof(Uint32);
pixel = (Uint32*)screen->pixels;
for(y = 0; y < res_y ; y++)
{
for(x = 0; x < res_x; x++)
{
iteration = iteration_pixels[x + y * res_x];
if ((iteration < 128) && (iteration > 0)) {
pixel[x + y * rank] = SDL_MapRGBA(screen->format,
0,
20 + iteration,
0,
255);
}
else if ((iteration >= 128) && (iteration < max_iteration))
{
pixel[x + y * rank] = SDL_MapRGBA(screen->format,
iteration,
148,
iteration,
255);
}
else
{
pixel[x + y * rank] = SDL_MapRGBA(screen->format,
0,
0,
0,
255);
}
}
}
if(julia_mode == 0)
zoom = zoom * 0.99;
else
zoom -= 0.01;
// Draw message on a corner...
char* msg = (char *)malloc(100 * sizeof(char));
sprintf(msg, "Zoom level: %0.3f", zoom * 100.0);
message = TTF_RenderText_Solid( font, msg, textColor );
free(msg);
if (message != NULL)
SDL_BlitSurface(message, NULL, screen, NULL);
free(message);
SDL_UpdateTexture(texture_screen, NULL, (Uint32*)screen->pixels, 800 * sizeof (Uint32));
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture_screen, NULL, NULL);
SDL_RenderPresent(renderer);
// SDL_Flip(screen);
}
printf("Time elapsed %0.5f seconds\n", ((double)clock() - start) / CLOCKS_PER_SEC);
SDL_Event ev;
int active;
active = 1;
while(active)
{
/* Handle events */
while(SDL_PollEvent(&ev))
{
if(ev.type == SDL_QUIT)
active = 0; /* End */
}
}
SDL_Quit();
return 0;
}