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