-
Notifications
You must be signed in to change notification settings - Fork 617
Expand file tree
/
Copy pathHEFTPlanningAlgorithm.java
More file actions
390 lines (331 loc) · 11.9 KB
/
HEFTPlanningAlgorithm.java
File metadata and controls
390 lines (331 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/**
* Copyright 2012-2013 University Of Southern California
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.workflowsim.planning;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.cloudbus.cloudsim.Consts;
import org.cloudbus.cloudsim.File;
import org.cloudbus.cloudsim.Log;
import org.workflowsim.CondorVM;
import org.workflowsim.Task;
import org.workflowsim.utils.Parameters;
/**
* The HEFT planning algorithm.
*
* @author Pedro Paulo Vezzá Campos
* @date Oct 12, 2013
*/
public class HEFTPlanningAlgorithm extends BasePlanningAlgorithm {
private Map<Task, Map<CondorVM, Double>> computationCosts;
private Map<Task, Map<Task, Double>> transferCosts;
private Map<Task, Double> rank;
private Map<CondorVM, List<Event>> schedules;
private Map<Task, Double> earliestFinishTimes;
private double averageBandwidth;
private class Event {
public double start;
public double finish;
public Event(double start, double finish) {
this.start = start;
this.finish = finish;
}
}
private class TaskRank implements Comparable<TaskRank> {
public Task task;
public Double rank;
public TaskRank(Task task, Double rank) {
this.task = task;
this.rank = rank;
}
@Override
public int compareTo(TaskRank o) {
return o.rank.compareTo(rank);
}
}
public HEFTPlanningAlgorithm() {
computationCosts = new HashMap<Task, Map<CondorVM, Double>>();
transferCosts = new HashMap<Task, Map<Task, Double>>();
rank = new HashMap<Task, Double>();
earliestFinishTimes = new HashMap<Task, Double>();
schedules = new HashMap<CondorVM, List<Event>>();
}
/**
* The main function
*/
@Override
public void run() {
Log.printLine("HEFT planner running with " + getTaskList().size()
+ " tasks.");
averageBandwidth = calculateAverageBandwidth();
for (Object vmObject : getVmList()) {
CondorVM vm = (CondorVM) vmObject;
schedules.put(vm, new ArrayList<Event>());
}
// Prioritization phase
calculateComputationCosts();
calculateTransferCosts();
calculateRanks();
// Selection phase
allocateTasks();
}
/**
* Calculates the average available bandwidth among all VMs in Mbit/s
*
* @return Average available bandwidth in Mbit/s
*/
private double calculateAverageBandwidth() {
double avg = 0.0;
for (Object vmObject : getVmList()) {
CondorVM vm = (CondorVM) vmObject;
avg += vm.getBw();
}
return avg / getVmList().size();
}
/**
* Populates the computationCosts field with the time in seconds to compute
* a task in a vm.
*/
private void calculateComputationCosts() {
for (Object taskObject : getTaskList()) {
Task task = (Task) taskObject;
Map<CondorVM, Double> costsVm = new HashMap<CondorVM, Double>();
for (Object vmObject : getVmList()) {
CondorVM vm = (CondorVM) vmObject;
if (vm.getNumberOfPes() < task.getNumberOfPes()) {
costsVm.put(vm, Double.MAX_VALUE);
} else {
costsVm.put(vm,
task.getCloudletTotalLength() / vm.getMips());
}
}
computationCosts.put(task, costsVm);
}
}
/**
* Populates the transferCosts map with the time in seconds to transfer all
* files from each parent to each child
*/
private void calculateTransferCosts() {
// Initializing the matrix
for (Object taskObject1 : getTaskList()) {
Task task1 = (Task) taskObject1;
Map<Task, Double> taskTransferCosts = new HashMap<Task, Double>();
for (Object taskObject2 : getTaskList()) {
Task task2 = (Task) taskObject2;
taskTransferCosts.put(task2, 0.0);
}
transferCosts.put(task1, taskTransferCosts);
}
// Calculating the actual values
for (Object parentObject : getTaskList()) {
Task parent = (Task) parentObject;
for (Task child : parent.getChildList()) {
transferCosts.get(parent).put(child,
calculateTransferCost(parent, child));
}
}
}
/**
* Accounts the time in seconds necessary to transfer all files described
* between parent and child
*
* @param parent
* @param child
* @return Transfer cost in seconds
*/
private double calculateTransferCost(Task parent, Task child) {
List<File> parentFiles = (List<File>) parent.getFileList();
List<File> childFiles = (List<File>) child.getFileList();
double acc = 0.0;
for (File parentFile : parentFiles) {
if (parentFile.getType() != Parameters.FileType.OUTPUT.value) {
continue;
}
for (File childFile : childFiles) {
if (childFile.getType() == Parameters.FileType.INPUT.value
&& childFile.getName().equals(parentFile.getName())) {
acc += childFile.getSize();
break;
}
}
}
//file Size is in Bytes, acc in MB
acc = acc / Consts.MILLION;
// acc in MB, averageBandwidth in Mb/s
return acc * 8 / averageBandwidth;
}
/**
* Invokes calculateRank for each task to be scheduled
*/
private void calculateRanks() {
for (Object taskObject : getTaskList()) {
Task task = (Task) taskObject;
calculateRank(task);
}
}
/**
* Populates rank.get(task) with the rank of task as defined in the HEFT
* paper.
*
* @param task The task have the rank calculates
* @return The rank
*/
private double calculateRank(Task task) {
if (rank.containsKey(task)) {
return rank.get(task);
}
double averageComputationCost = 0.0;
for (Double cost : computationCosts.get(task).values()) {
averageComputationCost += cost;
}
averageComputationCost /= computationCosts.get(task).size();
double max = 0.0;
for (Task child : task.getChildList()) {
double childCost = transferCosts.get(task).get(child)
+ calculateRank(child);
max = Math.max(max, childCost);
}
rank.put(task, averageComputationCost + max);
return rank.get(task);
}
/**
* Allocates all tasks to be scheduled in non-ascending order of schedule.
*/
private void allocateTasks() {
List<TaskRank> taskRank = new ArrayList<TaskRank>();
for (Task task : rank.keySet()) {
taskRank.add(new TaskRank(task, rank.get(task)));
}
// Sorting in non-ascending order of rank
Collections.sort(taskRank);
for (TaskRank tr : taskRank) {
allocateTask(tr.task);
}
}
/**
* Schedules the task given in one of the VMs minimizing the earliest finish
* time
*
* @param task The task to be scheduled
* @pre All parent tasks are already scheduled
*/
private void allocateTask(Task task) {
CondorVM chosenVM = null;
double earliestFinishTime = Double.MAX_VALUE;
double bestReadyTime = 0.0;
double finishTime;
for (Object vmObject : getVmList()) {
CondorVM vm = (CondorVM) vmObject;
double minReadyTime = 0.0;
for (Task parent : task.getParentList()) {
double readyTime = earliestFinishTimes.get(parent);
if (parent.getVmId() != vm.getId()) {
readyTime += transferCosts.get(parent).get(task);
}
minReadyTime = Math.max(minReadyTime, readyTime);
}
finishTime = findFinishTime(task, vm, minReadyTime, false);
if (finishTime < earliestFinishTime) {
bestReadyTime = minReadyTime;
earliestFinishTime = finishTime;
chosenVM = vm;
}
}
findFinishTime(task, chosenVM, bestReadyTime, true);
earliestFinishTimes.put(task, earliestFinishTime);
task.setVmId(chosenVM.getId());
}
/**
* Finds the best time slot available to minimize the finish time of the
* given task in the vm with the constraint of not scheduling it before
* readyTime. If occupySlot is true, reserves the time slot in the schedule.
*
* @param task The task to have the time slot reserved
* @param vm The vm that will execute the task
* @param readyTime The first moment that the task is available to be
* scheduled
* @param occupySlot If true, reserves the time slot in the schedule.
* @return The minimal finish time of the task in the vmn
*/
private double findFinishTime(Task task, CondorVM vm, double readyTime,
boolean occupySlot) {
List<Event> sched = schedules.get(vm);
double computationCost = computationCosts.get(task).get(vm);
double start, finish;
int pos;
if (sched.size() == 0) {
if (occupySlot) {
sched.add(new Event(readyTime, readyTime + computationCost));
}
return readyTime + computationCost;
}
if (sched.size() == 1) {
if (readyTime >= sched.get(0).finish) {
pos = 1;
start = readyTime;
} else if (readyTime + computationCost <= sched.get(0).start) {
pos = 0;
start = readyTime;
} else {
pos = 1;
start = sched.get(0).finish;
}
if (occupySlot) {
sched.add(pos, new Event(start, start + computationCost));
}
return start + computationCost;
}
// Trivial case: Start after the latest task scheduled
start = Math.max(readyTime, sched.get(sched.size() - 1).finish);
finish = start + computationCost;
int i = sched.size() - 1;
int j = sched.size() - 2;
pos = i + 1;
while (j >= 0) {
Event current = sched.get(i);
Event previous = sched.get(j);
if (readyTime > previous.finish) {
if (readyTime + computationCost <= current.start) {
start = readyTime;
finish = readyTime + computationCost;
}
break;
}
if (previous.finish + computationCost <= current.start) {
start = previous.finish;
finish = previous.finish + computationCost;
pos = i;
}
i--;
j--;
}
if (readyTime + computationCost <= sched.get(0).start) {
pos = 0;
start = readyTime;
if (occupySlot) {
sched.add(pos, new Event(start, start + computationCost));
}
return start + computationCost;
}
if (occupySlot) {
sched.add(pos, new Event(start, finish));
}
return finish;
}
}