-
Notifications
You must be signed in to change notification settings - Fork 1
/
buddhabrot.c
96 lines (79 loc) · 2.62 KB
/
buddhabrot.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <mpi.h>
#include "util.h"
int main(int argc, char** argv)
{
int i;
int count, countnew, tally, tallynew, max;
int world_rank, world_size;
double cr, ci, crnew, cinew;
struct Screen s;
s = getScreen(argc, argv);
int samples = 10000000;
int sampleFreq = 10;
int maxIter = s.iterations;
MPI_Init(NULL, NULL);
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
srand(time(NULL)*world_rank);
double *zrArray = (double *)calloc(maxIter, sizeof(double));
double *ziArray = (double *)calloc(maxIter, sizeof(double));
int *hist = (int *)calloc(s.screensize, sizeof(int));
int *global_hist = (int *)calloc(s.screensize, sizeof(int));
tally = 0;
tallynew = 0;
cr = random(s.minx, s.maxx);
ci = random(s.miny, s.maxy);
double *zrArraynew = (double *)calloc(maxIter, sizeof(double));
double *ziArraynew = (double *)calloc(maxIter, sizeof(double));
double *tempz;
for (i=0; i < samples; i++) {
if ((world_rank == 0) && (i % 10000 == 0)) {
printf("%d\n", i);
}
if (random(0.0, 1.0) > .8) {
crnew = random(s.minx, s.maxx);
cinew = random(s.miny, s.maxy);
} else {
mutate(cr, ci, &crnew, &cinew, s.zoom);
}
countnew = getMandelbrotPath(zrArraynew, ziArraynew, crnew, cinew, maxIter);
if (countnew == -1 || countnew == maxIter) continue;
tallynew = tallyPath(zrArraynew, ziArraynew, countnew, NULL, s);
if ((tallynew >= tally) || (random(0.0, 1.0) < ((double)tallynew/(double)tally))) {
tally = tallynew;
cr = crnew;
ci = cinew;
tempz = zrArray;
zrArray = zrArraynew;
zrArraynew = tempz;
tempz = ziArray;
ziArray = ziArraynew;
ziArraynew = tempz;
count = countnew;
}
if ((i % sampleFreq) != 0) continue;
tallyPath(zrArray, ziArray, count, hist, s);
}
MPI_Reduce(hist, global_hist, s.screensize, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
if (world_rank == 0) {
for (i=0; i < s.screensize; i++) {
if (global_hist[i] > max) {
max = global_hist[i];
}
}
writePPM("buddhabrot.ppm", global_hist, s.width, s.height, max);
}
free(zrArray);
free(ziArray);
free(zrArraynew);
free(ziArraynew);
free(global_hist);
free(hist);
MPI_Barrier(MPI_COMM_WORLD);
MPI_Finalize();
return 0;
}