1 | package de.uka.ipd.sdq.measurement.strategies.activeresource.cpu; |
2 | |
3 | import de.uka.ipd.sdq.measurement.strategies.activeresource.AbstractDemandStrategy; |
4 | import de.uka.ipd.sdq.measurement.strategies.activeresource.ResourceTypeEnum; |
5 | |
6 | public class MandelbrotDemand extends AbstractDemandStrategy { |
7 | |
8 | private int BAILOUT = 16; |
9 | private int MAX_ITERATIONS = 1000; |
10 | |
11 | public MandelbrotDemand() { |
12 | super(-2,0,2,40,20); |
13 | } |
14 | |
15 | private int iterate(float x, float y) { |
16 | float cr = y - 0.5f; |
17 | float ci = x; |
18 | float zi = 0.0f; |
19 | float zr = 0.0f; |
20 | int i = 0; |
21 | while (true) { |
22 | i++; |
23 | float temp = zr * zi; |
24 | float zr2 = zr * zr; |
25 | float zi2 = zi * zi; |
26 | zr = zr2 - zi2 + cr; |
27 | zi = temp + temp + ci; |
28 | if (zi2 + zr2 > BAILOUT) |
29 | return i; |
30 | if (i > MAX_ITERATIONS) |
31 | return 0; |
32 | } |
33 | } |
34 | |
35 | private void drawMandelbrot(long init) { |
36 | // Date d1 = new Date(); |
37 | int n = (int) init; |
38 | float m = (float) n; |
39 | int x, y; |
40 | for (y = -n; y < n; y++) { |
41 | // System.out.print("\n"); |
42 | for (x = -n; x < n; x++) { |
43 | if (iterate(x / m, y / m) == 0) { |
44 | // System.out.print("*"); |
45 | } else { |
46 | // System.out.print(" "); |
47 | } |
48 | |
49 | } |
50 | } |
51 | // Date d2 = new Date(); |
52 | // long diff = d2.getTime() - d1.getTime(); |
53 | // System.out.println("\nJava Elapsed " + diff / 1000.0f); |
54 | } |
55 | |
56 | @Override |
57 | public void run(long initial) { |
58 | drawMandelbrot(initial); |
59 | } |
60 | |
61 | @Override |
62 | public ResourceTypeEnum getStrategysResource() { |
63 | return ResourceTypeEnum.CPU; |
64 | } |
65 | |
66 | @Override |
67 | public String getName() { |
68 | return "Mandelbrot"; |
69 | } |
70 | |
71 | @Override |
72 | public void cleanup() { |
73 | // Do nothing. |
74 | } |
75 | } |