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