1 |
mojays |
2 |
package appl.util.benchmark; |
2 |
|
|
|
3 |
|
|
import java.awt.Rectangle; |
4 |
|
|
import java.awt.image.DataBuffer; |
5 |
|
|
import java.util.Random; |
6 |
|
|
|
7 |
|
|
import appl.data.LoadingException; |
8 |
|
|
import appl.ext.XuluConfig; |
9 |
|
|
import appl.parallel.data.splittable.SplittableLLProxyGrid; |
10 |
|
|
import appl.util.RasterMetaData; |
11 |
|
|
import edu.bonn.xulu.plugin.io.grid.array.WritableGridArrayFactory; |
12 |
|
|
|
13 |
|
|
/** |
14 |
|
|
* The algorithm simply iterates over a grid. The with and height can be |
15 |
|
|
* specified by the {@link XuluConfig} Property |
16 |
|
|
* <code>Benchmark.SimpleBench.gridwidth</code>. For each cell the average |
17 |
|
|
* over a neighborhood region is calculated. The neighborhoodRange can be |
18 |
|
|
* specified by <code>Benchmark.SimpleBench.neighborhood</code>. If the |
19 |
|
|
* algorithm reaches the end of the raster it starts again at (0,0). The rating |
20 |
|
|
* depends on the number of cells visited in the time (ms) specified by |
21 |
|
|
* <code>Benchmark.SimpleBench.time<code>. The number of cells is divided by |
22 |
|
|
* <code>Benchmark.SimpleBench.calibrator</code> and then given back as rating. |
23 |
|
|
* |
24 |
|
|
* @author Dominik Appl |
25 |
|
|
*/ |
26 |
|
|
public class SimpleBenchmark implements Benchmark { |
27 |
|
|
private SplittableLLProxyGrid inputGrid; |
28 |
|
|
|
29 |
|
|
int runningTime = 10000; |
30 |
|
|
|
31 |
|
|
private int gridwidth = 1000; |
32 |
|
|
|
33 |
|
|
private int neighborhoodRange; |
34 |
|
|
|
35 |
|
|
private double calibrator = 1; |
36 |
|
|
|
37 |
|
|
public int bench() { |
38 |
|
|
setUp(); |
39 |
|
|
|
40 |
|
|
int cellCount = 0; |
41 |
|
|
long startTime = System.currentTimeMillis(); |
42 |
|
|
Rectangle partition = new Rectangle(0, 0, inputGrid.getWidth(), |
43 |
|
|
inputGrid.getHeight()); |
44 |
|
|
boolean exit = false; |
45 |
|
|
for (int step = 0; exit == false; step++) { |
46 |
|
|
// the overallsum is simply the sum over all elements |
47 |
|
|
float overallSum = 0f; |
48 |
|
|
for (int y = 0; y < inputGrid.getHeight(); y++) |
49 |
|
|
for (int x = 0; x < inputGrid.getWidth(); x++) { |
50 |
|
|
if (System.currentTimeMillis() - startTime > runningTime) |
51 |
|
|
exit = true; |
52 |
|
|
else |
53 |
|
|
cellCount++; |
54 |
|
|
float tmp = inputGrid.getRasterSampleAsFloat(x, y); |
55 |
|
|
if (!Float.isNaN(tmp)) |
56 |
|
|
overallSum += tmp; |
57 |
|
|
// the local sum is simply the sum over all elements in the |
58 |
|
|
// neighborhood |
59 |
|
|
float localSum = 0; |
60 |
|
|
// number of cells over which the sum is calculated |
61 |
|
|
int noOfCells = 0; |
62 |
|
|
// for each cell: calculate the sum of all neighbors |
63 |
|
|
for (int y2 = y - neighborhoodRange; y2 <= y |
64 |
|
|
+ neighborhoodRange; y2++) |
65 |
|
|
for (int x2 = x - neighborhoodRange; x2 <= x |
66 |
|
|
+ neighborhoodRange; x2++) |
67 |
|
|
// check if the coordinates are valid (inside the |
68 |
|
|
// grid and not NaN) |
69 |
|
|
if (partition.contains(x2, y2)) |
70 |
|
|
if (!Float.isNaN(inputGrid |
71 |
|
|
.getRasterSampleAsFloat(x2, y2))) { |
72 |
|
|
localSum += inputGrid |
73 |
|
|
.getRasterSampleAsFloat(x2, y2); |
74 |
|
|
noOfCells++; |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
} |
78 |
|
|
} |
79 |
|
|
return (int) (cellCount / calibrator); |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
/** |
83 |
|
|
* The method is used to load grids and to initialize the benchmarks |
84 |
|
|
* configuration. |
85 |
|
|
*/ |
86 |
|
|
private void setUp() { |
87 |
|
|
// read config |
88 |
|
|
int width = XuluConfig.getXuluConfig().getIntProperty( |
89 |
|
|
"Benchmark.SimpleBench.gridwidth"); |
90 |
|
|
if (width != 0) |
91 |
|
|
this.gridwidth = width; |
92 |
|
|
// init running time |
93 |
|
|
int time = XuluConfig.getXuluConfig().getIntProperty( |
94 |
|
|
"Benchmark.SimpleBench.runningtime"); |
95 |
|
|
if (time != 0) |
96 |
|
|
this.runningTime = time; |
97 |
|
|
// init calibrator |
98 |
|
|
double calibrator = XuluConfig.getXuluConfig().getDoubleProperty( |
99 |
|
|
"Benchmark.SimpleBench.calibrator"); |
100 |
|
|
if (calibrator != 0) |
101 |
|
|
this.calibrator = calibrator; |
102 |
|
|
// init neighborhoodRange |
103 |
|
|
neighborhoodRange = XuluConfig.getXuluConfig().getIntProperty( |
104 |
|
|
"Benchmark.SimpleBench.neighborhood"); |
105 |
|
|
// create input Grid |
106 |
|
|
inputGrid = new SplittableLLProxyGrid(new WritableGridArrayFactory(), |
107 |
|
|
new RasterMetaData(DataBuffer.TYPE_DOUBLE, gridwidth, |
108 |
|
|
gridwidth, 0, 0, 0, 0, 50, null)); |
109 |
|
|
// load grid (so late loading has no influence on the calculation) |
110 |
|
|
try { |
111 |
|
|
inputGrid.loadData(); |
112 |
|
|
} catch (LoadingException e) { |
113 |
|
|
// TODO Auto-generated catch block |
114 |
|
|
e.printStackTrace(); |
115 |
|
|
} |
116 |
|
|
// fill Grid with random Data |
117 |
|
|
Random rand = new Random(); |
118 |
|
|
for (int y = 0; y < inputGrid.getHeight(); y++) |
119 |
|
|
for (int x = 0; x < inputGrid.getWidth(); x++) |
120 |
|
|
inputGrid.setGridSample(rand.nextDouble(), x, y); |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
public static void main(String[] args) { |
124 |
|
|
System.out.println("Current rating: " + new SimpleBenchmark().bench()); |
125 |
|
|
} |
126 |
|
|
} |