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