1 |
package appl.parallel.test; |
2 |
|
3 |
import java.awt.Rectangle; |
4 |
import java.io.File; |
5 |
import java.io.Serializable; |
6 |
import java.util.Vector; |
7 |
|
8 |
import junit.framework.Test; |
9 |
import junit.framework.TestCase; |
10 |
import junit.framework.TestSuite; |
11 |
|
12 |
import org.apache.log4j.LogManager; |
13 |
import org.apache.log4j.Logger; |
14 |
import org.apache.log4j.PropertyConfigurator; |
15 |
|
16 |
import appl.parallel.ComputingResourceContainer; |
17 |
import appl.parallel.client.ClientDataServer; |
18 |
import appl.parallel.client.RemoteEventHandler; |
19 |
import appl.parallel.data.splittable.SplittableLLProxyGrid; |
20 |
import appl.parallel.services.GlobalDiscoveryService; |
21 |
import appl.parallel.spmd.SPMDClientController; |
22 |
import appl.parallel.spmd.split.SplittableGrid; |
23 |
import appl.util.RasterUtil; |
24 |
import de.appl.util.RasterMetaData; |
25 |
import de.schmitzm.geotools.data.WritableGrid; |
26 |
import de.schmitzm.geotools.data.WritableGridRaster; |
27 |
import de.schmitzm.geotools.io.GeoImportUtil; |
28 |
import edu.bonn.xulu.plugin.io.grid.array.WritableGridArrayFactory; |
29 |
|
30 |
/** |
31 |
* This is a minor modification of {@link SPMDTest}. It is only used to test the |
32 |
* Multi-Splittable functionality of the parallelization. For the test of all |
33 |
* other features and more detailed information see {@link SPMDTest}. |
34 |
* |
35 |
* @author Dominik Appl |
36 |
*/ |
37 |
public class SPMDTest_MultiGrid extends TestCase implements Serializable { |
38 |
|
39 |
transient private final Logger LOG = LogManager.getLogger(this.getClass() |
40 |
.getName()); |
41 |
|
42 |
transient private ClientDataServer client; |
43 |
transient private Vector<ComputingResourceContainer> computingResources; |
44 |
transient private SPMDClientController clientController; |
45 |
|
46 |
transient SplittableGrid gridInput; |
47 |
transient SplittableGrid remoteInput; |
48 |
transient SplittableGrid localOutput; |
49 |
transient SplittableGrid remoteOutput; |
50 |
|
51 |
/** |
52 |
* Constructor for SPMDTest. |
53 |
* |
54 |
* @param name |
55 |
*/ |
56 |
public SPMDTest_MultiGrid(String name) { |
57 |
super(name); |
58 |
} |
59 |
|
60 |
/** |
61 |
* Returns the JUnit test suite that implements the <b>SPMDTest</b> |
62 |
* definition. |
63 |
*/ |
64 |
public static Test suite() { |
65 |
TestSuite sPMDTest = new TestSuite("SPMDTest"); |
66 |
// sPMDTest.setArbiter(DefaultTestArbiter.INSTANCE).setId( |
67 |
// "E873D66A70977ED2A98AAC10B54511DB"); |
68 |
// |
69 |
// sPMDTest.addTest(new SPMDTest("testSPMD").setId( |
70 |
// "E873D66A70977ED2E5E8ED20B54511DB").setTestInvocationId( |
71 |
// "E873D66A70977ED2EF7E3C00B54511DB")); |
72 |
return sPMDTest; |
73 |
} |
74 |
|
75 |
/** |
76 |
* @see junit.framework.TestCase#setUp() |
77 |
*/ |
78 |
protected void setUp() throws Exception { |
79 |
PropertyConfigurator.configure("xululog4j.cfg"); |
80 |
computingResources = new GlobalDiscoveryService() |
81 |
.getRemoteResources(); |
82 |
|
83 |
for (ComputingResourceContainer computingRes : computingResources) { |
84 |
System.out.println("DiscoveredRessource " + computingRes); |
85 |
} |
86 |
LOG.info("Discovery finished..."); |
87 |
RemoteEventHandler handler = new RemoteEventHandler(); |
88 |
client = new ClientDataServer(handler); |
89 |
clientController = new SPMDClientController(computingResources,null, client,handler); |
90 |
// ////////// Initialise data \\\\\\\\\\\\\ |
91 |
WritableGridRaster baseGrid = GeoImportUtil |
92 |
.readGridRasterFromArcInfoASCII(new File( |
93 |
"../Xulu-Data/minigrid2.arc")); // small data |
94 |
// "../Xulu-Data/large_clue_data/sc1gr0.0")); //large data |
95 |
gridInput = new SplittableLLProxyGrid(new WritableGridArrayFactory(), |
96 |
new RasterMetaData(baseGrid)); |
97 |
localOutput = new SplittableLLProxyGrid(new WritableGridArrayFactory(), |
98 |
new RasterMetaData(baseGrid)); |
99 |
remoteInput = new SplittableLLProxyGrid(new WritableGridArrayFactory(), |
100 |
new RasterMetaData(baseGrid)); |
101 |
remoteOutput = new SplittableLLProxyGrid( |
102 |
new WritableGridArrayFactory(), new RasterMetaData(baseGrid)); |
103 |
RasterUtil.copyInto(baseGrid, gridInput); |
104 |
RasterUtil.copyInto(baseGrid, remoteInput); |
105 |
//RasterUtil.copyInto(baseGrid, localOutput); |
106 |
//RasterUtil.copyInto(baseGrid, remoteOutput); |
107 |
} |
108 |
|
109 |
/** |
110 |
* @see junit.framework.TestCase#tearDown() |
111 |
*/ |
112 |
protected void tearDown() throws Exception { |
113 |
client.close(); |
114 |
clientController.close(); |
115 |
// GlobalDiscoveryService.getInstance().stopServices(); |
116 |
} |
117 |
|
118 |
/** |
119 |
* TestSPMD |
120 |
* |
121 |
* @throws Exception |
122 |
*/ |
123 |
public void testSPMD() throws Exception { |
124 |
int steps = 4; |
125 |
int neighborhoodRange = 3; |
126 |
System.out.println("Starting step 1: "); |
127 |
|
128 |
/*********************************************************************** |
129 |
* START LOCAL ALGORITHM ******************** The simple Algorithm will |
130 |
* calculate the average of the neighborhood und write the output to the |
131 |
* coresponing cell in the output cell |
132 |
**********************************************************************/ |
133 |
|
134 |
Rectangle partition = new Rectangle(0, 0, gridInput.getWidth(), |
135 |
gridInput.getHeight()); |
136 |
if (gridInput.getWidth() < 20) |
137 |
RasterUtil.printGrid(gridInput, 5, 1, "Input"); |
138 |
|
139 |
for (int step = 0; step < steps; step++) { |
140 |
// the overallsum is simply the sum over all elements |
141 |
float overallSum = 0f; |
142 |
long localStartTime = System.currentTimeMillis(); |
143 |
for (int y = 0; y < gridInput.getHeight(); y++) |
144 |
for (int x = 0; x < gridInput.getWidth(); x++) { |
145 |
float tmp = gridInput.getRasterSampleAsFloat(x, y); |
146 |
if (!Float.isNaN(tmp)) |
147 |
overallSum += tmp; |
148 |
// the local sum is simply the sum over all elements in the |
149 |
// neighborhood |
150 |
float localSum = 0; |
151 |
// number of cells over which the sum is calculated |
152 |
int noOfCells = 0; |
153 |
// for each cell: calculate the sum of all neighbors |
154 |
for (int y2 = y - neighborhoodRange; y2 <= y |
155 |
+ neighborhoodRange; y2++) |
156 |
for (int x2 = x - neighborhoodRange; x2 <= x |
157 |
+ neighborhoodRange; x2++) |
158 |
// check if the coordinates are valid (inside the |
159 |
// grid and not NaN) |
160 |
if (partition.contains(x2, y2)) |
161 |
if (!Float.isNaN(gridInput |
162 |
.getRasterSampleAsFloat(x2, y2))) { |
163 |
localSum += gridInput |
164 |
.getRasterSampleAsFloat(x2, y2); |
165 |
noOfCells++; |
166 |
} |
167 |
|
168 |
localOutput.setRasterSample(localSum / (float) (noOfCells), |
169 |
x, y); |
170 |
|
171 |
} |
172 |
|
173 |
System.out.print("Finished step " + (step+1) + " in " |
174 |
+ ((System.currentTimeMillis() - localStartTime)) + " ms"); |
175 |
localStartTime = System.currentTimeMillis(); |
176 |
System.out.println(" with sum (local calculated): " + overallSum); |
177 |
//copy values into results from output to input (for next step) |
178 |
RasterUtil.copyInto(localOutput, gridInput); |
179 |
} |
180 |
if(gridInput.getWidth()<20) |
181 |
RasterUtil.printGrid(localOutput, 5, 1, "Local Output"); |
182 |
|
183 |
|
184 |
|
185 |
|
186 |
/*********************************************************************** |
187 |
* START REMOTE ALGORITHM |
188 |
* ******************** All the same..now remotely********************/ |
189 |
|
190 |
System.out |
191 |
.println("******************* starting remote execution *****************"); |
192 |
long remoteStartTime = System.currentTimeMillis(); |
193 |
clientController.setNeighborhoodRange(neighborhoodRange); |
194 |
// initialize as MulitSplittable |
195 |
WritableGrid[] testSplittable = {remoteInput,remoteOutput}; |
196 |
clientController.addToMultiDataSplitControl(testSplittable, "gridData"); |
197 |
|
198 |
// clientController.addToSplitControl(remoteInput, "inputGrid"); |
199 |
// clientController.addToSplitControl(remoteOutput, "outputGrid"); |
200 |
System.out.println("After " |
201 |
+ ((System.currentTimeMillis() - remoteStartTime)) |
202 |
+ "ms: finished with adding to splitcontroll"); |
203 |
|
204 |
//********************** START STEP CONTROL ********************* |
205 |
for (int step = 0; step < steps; step++) { |
206 |
if(step > 0){ |
207 |
long updateStartTime = System.currentTimeMillis(); |
208 |
clientController.updateNeighborhood(remoteInput); |
209 |
System.out.println("Update took:" + (System.currentTimeMillis()-updateStartTime)); |
210 |
} |
211 |
Object[] sum; |
212 |
try { |
213 |
sum = clientController.runSPMDModelTask( |
214 |
new AverageNeighborhoodTestTask_MultiGrid(), neighborhoodRange); |
215 |
//add results |
216 |
float overallSum = 0; |
217 |
for (int i = 0; i < sum.length; i++) |
218 |
overallSum += (Float) sum[i]; |
219 |
System.out.println("finished step " |
220 |
+ (step+1) + " in " |
221 |
+ ((System.currentTimeMillis() - remoteStartTime)) |
222 |
+ "ms " + " with sum "+ |
223 |
+ overallSum); |
224 |
remoteStartTime = System.currentTimeMillis(); |
225 |
|
226 |
} catch (Throwable e) { |
227 |
// TODO Auto-generated catch block |
228 |
e.printStackTrace(); |
229 |
} |
230 |
} |
231 |
//***************** END STEP CONTROL ***************************** |
232 |
|
233 |
//After execution: |
234 |
// merge partitions and check if the local and remotely calculated |
235 |
// versions are equal |
236 |
remoteStartTime = System.currentTimeMillis(); |
237 |
clientController.mergePartition(remoteOutput); |
238 |
System.out.println("Merging took another " |
239 |
+ ((System.currentTimeMillis() - remoteStartTime)) |
240 |
+ "ms"); |
241 |
// Thread.currentThread().sleep(10000); |
242 |
if(remoteInput.getWidth()<20) |
243 |
RasterUtil.printGrid(remoteOutput, 5, 1, "Remote Output"); |
244 |
assertTrue(RasterUtil.checkEqual(remoteOutput, localOutput, true)); |
245 |
} |
246 |
} |