1 |
package appl.parallel.spmd.split; |
2 |
|
3 |
import java.awt.Rectangle; |
4 |
|
5 |
/** |
6 |
* This class can be used for the implementation of {@link SplitMap}s. It already |
7 |
* provides most of the functions. Normally subclasses only have to implement the methods |
8 |
* {@link SplitMap#makeMap()} and {@link SplitMap#getNeighborsForPosition(int)}. |
9 |
* |
10 |
* @see SplitMap |
11 |
* |
12 |
* @author Dominik Appl |
13 |
*/ |
14 |
public abstract class AbstractSplitMap implements SplitMap { |
15 |
|
16 |
public enum NeighborhoodBoxingMode { |
17 |
inBoxing, outBoxing |
18 |
} |
19 |
|
20 |
protected NeighborhoodBoxingMode boxingMode; |
21 |
protected int noOfPartitions; |
22 |
protected int globalHeight; |
23 |
protected int globalWidth; |
24 |
|
25 |
/** |
26 |
* a value of weights determines how big the share of the |
27 |
* partition with this position is. The sum of all values |
28 |
* should be 1 or near to 1. |
29 |
*/ |
30 |
protected double[] weights; |
31 |
protected int neighborhoodRange; |
32 |
|
33 |
/** the Rectangle of the whole Grid **/ |
34 |
protected Rectangle globalBounds; |
35 |
/** the calculation area of each partition */ |
36 |
protected Rectangle[] partitionCalculationBounds; |
37 |
/** the bounds of the neighborhood data of the partition */ |
38 |
protected Rectangle[] partitionNeighborhoodBounds; |
39 |
protected String[] IPs; |
40 |
|
41 |
public AbstractSplitMap(SplittableResource splittable, int neighborhoodRange, |
42 |
int noOfPartitions, |
43 |
NeighborhoodBoxingMode boxingMode) { |
44 |
this(splittable.getSplitWidth(),splittable.getSplitHeight(),neighborhoodRange, noOfPartitions, boxingMode); |
45 |
|
46 |
} |
47 |
|
48 |
public AbstractSplitMap() { |
49 |
this.globalWidth = 1; |
50 |
this.globalHeight = 1; |
51 |
this.neighborhoodRange = 0; |
52 |
this.noOfPartitions = 1; |
53 |
this.boxingMode = NeighborhoodBoxingMode.inBoxing; |
54 |
init(); |
55 |
makeMap(); |
56 |
} |
57 |
|
58 |
/** |
59 |
* @param width |
60 |
* @param height |
61 |
* @param neighborhoodRange |
62 |
* @param noOfPartitions |
63 |
* @param boxingMode |
64 |
*/ |
65 |
public AbstractSplitMap(int width, int height, int neighborhoodRange, int noOfPartitions, NeighborhoodBoxingMode boxingMode) { |
66 |
this.globalWidth = width; |
67 |
this.globalHeight = height; |
68 |
this.neighborhoodRange = neighborhoodRange; |
69 |
this.noOfPartitions = noOfPartitions; |
70 |
this.boxingMode = boxingMode; |
71 |
init(); |
72 |
makeMap(); |
73 |
} |
74 |
|
75 |
/* non java-doc |
76 |
* @see appl.parallel.spmd.split.SplitMap#setParameters(SplittableResource, int, int, appl.parallel.spmd.split.AbstractSplitMap.NeighborhoodBoxingMode) |
77 |
*/ |
78 |
public void setParameters(SplittableResource splittable, int neighborhoodRange, int noOfPartitions, NeighborhoodBoxingMode boxingMode) { |
79 |
this.globalWidth = splittable.getSplitWidth(); |
80 |
this.globalHeight = splittable.getSplitHeight(); |
81 |
this.neighborhoodRange = neighborhoodRange; |
82 |
this.noOfPartitions = noOfPartitions; |
83 |
this.boxingMode = boxingMode; |
84 |
init(); |
85 |
} |
86 |
|
87 |
/** |
88 |
* Initializes the bounds of the partitions |
89 |
*/ |
90 |
protected void init() { |
91 |
if (noOfPartitions < 1) |
92 |
throw new UnsupportedOperationException( |
93 |
"Number of Partions must be > 0"); |
94 |
|
95 |
//Initialize the weight for equal split (should not matter if not excactly 100%) |
96 |
weights = new double[noOfPartitions]; |
97 |
for (int i = 0; i < noOfPartitions; i++) |
98 |
weights[i] = (1.0 / noOfPartitions); |
99 |
globalBounds = new Rectangle(0, 0, globalWidth, globalHeight); |
100 |
partitionCalculationBounds = new Rectangle[noOfPartitions]; |
101 |
partitionNeighborhoodBounds = new Rectangle[noOfPartitions]; |
102 |
|
103 |
} |
104 |
|
105 |
|
106 |
|
107 |
public void setWeights(double... weights) { |
108 |
if (weights.length != noOfPartitions) |
109 |
throw new UnsupportedOperationException("The number of weights (" |
110 |
+ weights.length |
111 |
+ ") does not match the number of partitions (" |
112 |
+ noOfPartitions + ")"); |
113 |
//check if the weights are valid |
114 |
for (double weight : weights) { |
115 |
if(weight <=0 || weight > 1) |
116 |
throw new UnsupportedOperationException("A weight was '" + weight + "' but must be between 0 and 1"); |
117 |
} |
118 |
this.weights = weights; |
119 |
} |
120 |
|
121 |
/* (non-Javadoc) |
122 |
* @see appl.parallel.spmd.split.SplitMap#getDescription() |
123 |
*/ |
124 |
public abstract String getDescription(); |
125 |
|
126 |
/* (non-Javadoc) |
127 |
* @see appl.parallel.spmd.split.SplitMap#getNeighborsForPosition(int) |
128 |
*/ |
129 |
public abstract int[] getNeighborsForPosition(int pos); |
130 |
|
131 |
/* (non-Javadoc) |
132 |
* @see appl.parallel.spmd.split.SplitMap#makeMap() |
133 |
*/ |
134 |
public abstract void makeMap(); |
135 |
|
136 |
|
137 |
/* (non-Javadoc) |
138 |
* @see appl.parallel.spmd.split.SplitMap#getGlobalCalculationBounds() |
139 |
*/ |
140 |
public Rectangle getGlobalCalculationBounds() { |
141 |
return globalBounds; |
142 |
} |
143 |
|
144 |
|
145 |
|
146 |
/* (non-Javadoc) |
147 |
* @see appl.parallel.spmd.split.SplitMap#getLocalCalculationArea(int) |
148 |
*/ |
149 |
public Rectangle getLocalCalculationBounds(int pos){ |
150 |
Rectangle localBounds = (Rectangle) partitionCalculationBounds[pos].clone(); |
151 |
//move relative to the partition bounds |
152 |
double xdiff=partitionCalculationBounds[pos].getX()-partitionNeighborhoodBounds[pos].getX(); |
153 |
double ydiff=partitionCalculationBounds[pos].getY()-partitionNeighborhoodBounds[pos].getY(); |
154 |
//set to the local coordinates |
155 |
localBounds.setLocation((int) xdiff, (int) ydiff); |
156 |
return localBounds; |
157 |
} |
158 |
|
159 |
|
160 |
/* (non-Javadoc) |
161 |
* @see appl.parallel.spmd.split.SplitMap#getLocalNeighborhoodArea(int) |
162 |
*/ |
163 |
public Rectangle getLocalNeighborhoodBounds(int pos) { |
164 |
Rectangle localBounds = (Rectangle) partitionNeighborhoodBounds[pos] |
165 |
.clone(); |
166 |
//move to (0,0) |
167 |
localBounds.setLocation(0, 0); |
168 |
return localBounds; |
169 |
} |
170 |
|
171 |
/* (non-Javadoc) |
172 |
* @see appl.parallel.spmd.split.SplitMap#getPartitionCalculationArea(int) |
173 |
*/ |
174 |
public Rectangle getPartitionCalculationBounds(int pos) { |
175 |
return partitionCalculationBounds[pos]; |
176 |
} |
177 |
|
178 |
public Rectangle getPartitionNeighborhoodBounds(int pos) { |
179 |
return partitionNeighborhoodBounds[pos]; |
180 |
} |
181 |
|
182 |
|
183 |
/* (non-Javadoc) |
184 |
* @see appl.parallel.spmd.split.SplitMap#getGlobalBounds() |
185 |
*/ |
186 |
public Rectangle getGlobalBounds() { |
187 |
return globalBounds; |
188 |
} |
189 |
|
190 |
/* (non-Javadoc) |
191 |
* @see appl.parallel.spmd.split.SplitMap#getPartitionBounds(int) |
192 |
*/ |
193 |
public Rectangle getPartitionBounds(int pos) { |
194 |
return partitionNeighborhoodBounds[pos]; |
195 |
} |
196 |
|
197 |
public Rectangle getLocalBounds(int pos) |
198 |
{ |
199 |
return getLocalNeighborhoodBounds(pos); |
200 |
} |
201 |
|
202 |
|
203 |
/* (non-Javadoc) |
204 |
* @see appl.parallel.spmd.split.SplitMap#getIP(int) |
205 |
*/ |
206 |
public String getIP(int pos) { |
207 |
if(IPs==null) |
208 |
return null; |
209 |
if(IPs.length!=noOfPartitions) |
210 |
return null; |
211 |
return IPs[pos]; |
212 |
} |
213 |
|
214 |
/* (non-Javadoc) |
215 |
* @see appl.parallel.spmd.split.SplitMap#setIPs(java.lang.String[]) |
216 |
*/ |
217 |
public void setIPs(String[] IPs) { |
218 |
this.IPs = IPs; |
219 |
} |
220 |
|
221 |
/** |
222 |
* @return the neighborhoodRange |
223 |
*/ |
224 |
public int getNeighborhoodRange() { |
225 |
return neighborhoodRange; |
226 |
} |
227 |
|
228 |
public int getCount(){ |
229 |
return noOfPartitions; |
230 |
} |
231 |
} |