1 |
package appl.parallel.test; |
2 |
|
3 |
import java.awt.Rectangle; |
4 |
|
5 |
import appl.parallel.spmd.AbstractSPMDTask; |
6 |
import appl.parallel.spmd.SPMDServerInterface; |
7 |
import appl.parallel.spmd.split.DataPartition; |
8 |
import appl.util.RasterUtil; |
9 |
import de.schmitzm.geotools.data.WritableGrid; |
10 |
|
11 |
/** |
12 |
* @author Dominik Appl |
13 |
*/ |
14 |
public class AverageNeighborhoodTestTask_MultiGrid extends AbstractSPMDTask { |
15 |
|
16 |
/** |
17 |
* |
18 |
*/ |
19 |
private static final long serialVersionUID = 13L; |
20 |
|
21 |
/* (non-Javadoc) |
22 |
* @see appl.parallel.spmd.SPMDTask#run() |
23 |
*/ |
24 |
public Object run(Object...parameters) { |
25 |
SPMDServerInterface controller = getSPMDServerController(); |
26 |
// WritableGrid gridInput = (WritableGrid) controller |
27 |
// .getPartition("inputGrid"); |
28 |
// WritableGrid gridOutput = (WritableGrid) controller |
29 |
// .getPartition("outputGrid"); |
30 |
|
31 |
|
32 |
// WritableGrid gridInput = (WritableGrid) controller |
33 |
// .getMultiPartition("gridData",0); |
34 |
// WritableGrid gridOutput = (WritableGrid) controller |
35 |
// .getMultiPartition("gridData",1); |
36 |
// |
37 |
DataPartition[] gridData = (DataPartition[]) controller.getMultiPartition("gridData"); |
38 |
WritableGrid gridInput = (WritableGrid) gridData[0]; |
39 |
WritableGrid gridOutput = (WritableGrid) gridData[1]; |
40 |
|
41 |
int neighborhoodRange = (Integer) parameters[0]; |
42 |
/********************** START REMOTE ALGORITHM ********************* |
43 |
* start the calculation (notice that only the outer loop variables have changed) |
44 |
* *****************************************************************/ |
45 |
float overallSum = 0; |
46 |
Rectangle partition = controller.getLocalBounds(); |
47 |
//System.out.println("290,64: " + gridInput.getRasterSampleAsFloat(controller.getLocalCalcMinX()+289,64)); |
48 |
for (int y = controller.getLocalCalcMinY(); y < controller |
49 |
.getLocalCalcMaxY() + 1; y++) |
50 |
for (int x = controller.getLocalCalcMinX(); x < controller |
51 |
.getLocalCalcMaxX() + 1; x++) { |
52 |
float tmp = gridInput.getRasterSampleAsFloat(x,y); |
53 |
if(!Float.isNaN(tmp)) |
54 |
overallSum += tmp; |
55 |
//the local sum is simply the sum over all elements in the neighborhood |
56 |
float localSum = 0; |
57 |
//number of cells over which the sum is calculated |
58 |
int noOfCells = 0; |
59 |
//for each cell: calculate the sum of all neighbors |
60 |
for (int y2 = y - neighborhoodRange; y2 <= y |
61 |
+ neighborhoodRange; y2++) |
62 |
for (int x2 = x - neighborhoodRange; x2 <= x |
63 |
+ neighborhoodRange; x2++) |
64 |
//check if the coordinates are valid (inside the grid and not NaN) |
65 |
if (partition.contains(x2, y2)) |
66 |
if (!Float.isNaN(gridInput |
67 |
.getRasterSampleAsFloat(x2, y2))) { |
68 |
localSum += gridInput.getRasterSampleAsFloat( |
69 |
x2, y2); |
70 |
noOfCells++; |
71 |
} |
72 |
gridOutput.setRasterSample(localSum / (float) (noOfCells), x,y); |
73 |
|
74 |
} |
75 |
//copy values into results from output to input (for next step) |
76 |
RasterUtil.copyInto(gridOutput, gridInput); |
77 |
return overallSum; |
78 |
} |
79 |
|
80 |
/* (non-Javadoc) |
81 |
* @see appl.parallel.spmd.AbstractSPMDTask#init() |
82 |
*/ |
83 |
@Override |
84 |
public void init() { |
85 |
// TODO Auto-generated method stub |
86 |
|
87 |
} |
88 |
|
89 |
} |