changes.
[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 = 2;
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         float bspan = (this.blen/group);
111         int[] ps = this.pixels;
112         int length = ps.length;
113         int id = this.id;
114         int group = this.group;
115         float startb = bmin + bspan * id;
116         float endb;
117         if ((id+1)==group)
118             endb=bmax;
119         else
120             endb=bmin+bspan*(id+1);
121         int count=0;
122         for(a=amin;a<amax;a+=adelta) {
123             for(b=startb;b<endb;b+=bdelta) {
124                 x=(float)0.0;
125                 y=(float)0.0;
126                 int iteration=0;
127                 float x2 = (float)0.0;
128                 float y2 = (float)0.0;
129                 float xy = (float)0.0;
130                 boolean finish = true; //(x2 + y2 <= 4.0) & (iteration != times);
131                 while(finish) {
132                     float tmpy = (float)2.0*xy;
133                     x = x2 - y2 + a;
134                     y = tmpy + b;
135                     x2 = x*x;
136                     y2 = y*y;
137                     xy = x*y;
138                     iteration++;
139                     boolean tmpf = (x2 + y2 <= 4.0);
140                     finish = tmpf & (iteration != times);
141                 }
142                 count++;
143                 if(iteration<=times & iteration>0) {
144                     scaleda=(int)((a - amin)*appletWidth/(amax - amin));
145                     scaledb=(int)((b - bmin)*appletHeight/(bmax - bmin));
146                     int index = (scaledb * appletWidth + scaleda - id) / group;
147                     if(index < length) {
148                         ps[index] = alpha<<24 | red<<16 | iteration<<8 | blue;
149                     }
150                 }
151             }
152         }
153         System.printString(count+"\n");
154     }
155 }
156
157 public class Image {
158     flag finish;
159     
160     private int group;
161     private int counter;
162     private int[][] pixels;
163     private String outputfile;
164     
165     public Image(int g) {
166         this.group = g;
167         this.counter = 0;
168         this.pixels = new int[g][];
169         this.outputfile = new String("/scratch/fractal/image.dat");
170     }
171     
172     public boolean outputImage(int[] pixels, int index) {
173         this.counter++;
174
175         this.pixels[index] = pixels;
176
177         boolean isFinish = (this.group == this.counter);
178         if(isFinish) {
179             // output the image
180             /*FileOutputStream oStream = new FileOutputStream(outputfile, true);
181             //System.printString(new String(ps, 0, ps.length) + "\n");
182             oStream.write(ps, 0, ps.length);
183             oStream.close();*/
184         }
185         return isFinish;
186     }
187 }