-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmandel.c
88 lines (72 loc) · 1.66 KB
/
mandel.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
/* ---------------------------- */
/* David Gago 41710 */
/* Nuno Mendonça 41623 */
/* ---------------------------- */
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "ppm.h"
#include <cilk/cilk.h>
#define XCENTER -2
#define YCENTER -1.5
#define SCALE 0.003
#define COLORMAP "mca.map"
unsigned char *matrix;
int main (int argc, char **argv)
{
int imgsize, n;
double scale;
/* help */
if (argc!=4)
{
printf ("%s <ntasks> <imgsize> <file.ppm>\n\n", argv[0]);
exit(0);
}
/* setup */
imgsize = atoi (argv[2]);
assert (imgsize >= 1024);
scale = SCALE * 1024 / imgsize;
matrix = (unsigned char*) calloc (imgsize*imgsize, sizeof (unsigned char));
assert (matrix != NULL);
/* mandelbrot */
void fillMap(int start, int range)
{
int x,y,lp;
double ax,ay;
double a1,b1,a2,b2;
unsigned long int index;
for (x=start; x<start+range; x++)
for (y=0; y<imgsize-1; y++)
{
ax=XCENTER+x*scale;
ay=YCENTER+y*scale;
a1=ax; b1=ay; lp=0;
while ((lp<=255) && (a1*a1+b1*b1<=4))
{
lp++;
a2=a1*a1-b1*b1+ax;
b2=2*a1*b1+ay;
a1=a2;
b1=b2;
}
if (lp>255)
lp=0;
index = x+(y*imgsize);
matrix[index]=lp;
}
}
/*
for (x=0; x<imgsize-1; x++)
for (y=start; y<start+range; y++)
for (x=start; x<start+range; x++)
for (y=0; y<imgsize-1; y++)
*/
int cores = atoi(argv[1]);
for (n = 0; n < cores; n++)
cilk_spawn fillMap(imgsize*n/cores,imgsize/cores);
cilk_sync;
/* output to file */
output_ppm (argv[3], COLORMAP, matrix, imgsize, imgsize, 255);
free (matrix);
return 1;
}