f885354ba08a348e4e9ef32b278225f2c03eaaf6
[IRC.git] / Robust / src / Benchmarks / Performance / Fractal.java
1 /*  Draw a Mandelbrot set, maximum magnification 10000000 times;
2  */
3 task t1(StartupObject s{initialstate}) {
4     //System.printString("task t1\n");
5     
6     int width = 3200; 
7     int height = 3200;
8     int group = 640;
9
10     int h = height / group;
11     for(int i = 0; i < group; i++) {
12         Fractal fratal = new Fractal(i,
13                                      group,
14                                      width,
15                                      height){run};
16     }
17     Image image = new Image(group){!finish};
18     
19     taskexit(s{!initialstate});
20 }
21
22 task t2(Fractal fractal{run}) {
23     //System.printString("task t2\n");
24     
25     //  Now do the computation.
26     fractal.run();
27     
28     taskexit(fractal{!run, output});
29 }
30
31 //task t3(Image image{!finish}, Fractal fractal{output}) {
32     //System.printString("task t3\n");
33
34 //    if(image.outputImage(fractal.pixels, fractal.id)) {
35         //System.printString("Finish!\n");
36 //      taskexit(image{finish}, fractal{!output});
37 //    } else {
38 //      taskexit(fractal{!output});
39 //    }
40 //}
41
42 public class Fractal {
43     flag run;
44     flag output;
45     
46     public int id;
47     public int group;
48     public int AppletWidth;
49     private int AppletHeight;
50     private float amin;
51     private float amax;
52     private float bmin;
53     private float bmax;
54     private float alen, blen;
55     public int[] pixels;
56     int alpha;
57     int red;
58     int green;
59     int blue;
60     int times;
61     
62     public Fractal(int index,
63                    int group,
64                    int width, 
65                    int height) {
66         this.id = index;
67         this.group = group;
68         this.AppletWidth = width;
69         this.AppletHeight = height;
70         this.amin = (float)-2.0;
71         this.amax =  (float)1.0;
72         this.bmin = (float)-1.5;
73         this.bmax =  (float)1.5;
74         this.alen = (float)3.0;//this.amax - this.amin;
75         this.blen = (float)3.0;//this.bmax - this.bmin;
76         this.alpha = 0xff;
77         this.red = 0xff;
78         this.green = 0xff;
79         this.blue = 0xff;
80         this.times = 255;
81         int length = this.AppletWidth * this.AppletHeight / this.group;
82         this.pixels = new int[length];
83         int[] ps = this.pixels;
84         int incr=0;
85         while (incr < length) {
86             ps[incr++] = this.alpha<<24 | 0x00<<16 | 0x00<<8 | 0xff;
87         }
88         Random rnd = new Random();
89         int maxint = (1<<32) - 1;
90         red   = (int)(((float)rnd.nextInt()/maxint)*255);
91         green = (int)(((float)rnd.nextInt()/maxint)*255);
92         blue  = (int)(((float)rnd.nextInt()/maxint)*255);
93     }
94
95     public void run () {
96         float amin = this.amin;
97         float amax = this.amax;
98         float bmin = this.bmin;
99         float bmax = this.bmax;
100         int appletWidth = this.AppletWidth;
101         int appletHeight = this.AppletHeight;
102         int times = this.times;
103         int alpha = this.alpha;
104         int red = this.red;
105         int blue = this.blue;
106         float a,b,x,y; //a--width, b--height
107         int scaleda,scaledb;
108         float adelta = (this.alen/appletWidth);
109         float bdelta = (this.blen/appletHeight);
110         int[] ps = this.pixels;
111         int length = ps.length;
112         int id = this.id;
113         int group = this.group;
114         float startb = bmin + bdelta * id;
115         float endb = bmax - bdelta * (group - id);
116         float bspan = bdelta * group;
117         for(a=amin;a<amax;a+=adelta) {
118             for(b=startb;b<endb;b+=bspan) {
119                 x=(float)0.0;
120                 y=(float)0.0;
121                 int iteration=0;
122                 float x2 = (float)0.0;
123                 float y2 = (float)0.0;
124                 float xy = (float)0.0;
125                 boolean finish = true; //(x2 + y2 <= 4.0) & (iteration != times);
126                 while(finish) {
127                     float tmpy = (float)2.0*xy;
128                     x = x2 - y2 + a;
129                     y = tmpy + b;
130                     x2 = x*x;
131                     y2 = y*y;
132                     xy = x*y;
133                     iteration++;
134                     boolean tmpf = (x2 + y2 <= 4.0);
135                     finish = tmpf & (iteration != times);
136                 }
137                 if(iteration<=times & iteration>0) {
138                     scaleda=(int)((a - amin)*appletWidth/(amax - amin));
139                     scaledb=(int)((b - bmin)*appletHeight/(bmax - bmin));
140                     int index = (scaledb * appletWidth + scaleda - id) / group;
141                     if(index < length) {
142                         ps[index] = alpha<<24 | red<<16 | iteration<<8 | blue;
143                     }
144                 }
145             }
146         }
147     }
148 }
149
150 public class Image {
151     flag finish;
152     
153     private int group;
154     private int counter;
155     private int[][] pixels;
156     private String outputfile;
157     
158     public Image(int g) {
159         this.group = g;
160         this.counter = 0;
161         this.pixels = new int[g][];
162         this.outputfile = new String("/scratch/fractal/image.dat");
163     }
164     
165     public boolean outputImage(int[] pixels, int index) {
166         this.counter++;
167
168         this.pixels[index] = pixels;
169
170         boolean isFinish = (this.group == this.counter);
171         if(isFinish) {
172             // output the image
173             /*FileOutputStream oStream = new FileOutputStream(outputfile, true);
174             //System.printString(new String(ps, 0, ps.length) + "\n");
175             oStream.write(ps, 0, ps.length);
176             oStream.close();*/
177         }
178         return isFinish;
179     }
180 }