-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathILBA.go
More file actions
185 lines (149 loc) · 4.71 KB
/
ILBA.go
File metadata and controls
185 lines (149 loc) · 4.71 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
import (
"math/rand"
"sort"
)
//This implementation can be optimized by using pointers and list instead of vectors.
type Jobs struct {
timeToExecute float32
initTime float32
id int // We use ID to know from which Organization this job belongs
}
type Processor struct{
jobs []Jobs
num int
localMakeSpan float32
}
type Organization struct{
num int
jobs []Jobs
p []Processor
totalMakeSpan float32
}
type ByMakeSpan []Organization
func (a ByMakeSpan) Len() int { return len(a) }
func (a ByMakeSpan) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByMakeSpan) Less(i, j int) bool { return a[i].totalMakeSpan < a[j].totalMakeSpan }
func MakeJobs(n int, id int) []Jobs {
jobs := make([]Jobs, n)
for i := 0; i < n; i++ {
jobTime := rand.Intn(1000) + 1
jobs[i].timeToExecute = float32(jobTime)
jobs[i].id = id
}
return jobs
}
func CalculateMakeSpan(O Organization) float32 {
for i:=0; i < len(O.p); i++ {
if O.totalMakeSpan < O.p[i].localMakeSpan {
O.totalMakeSpan = O.p[i].localMakeSpan
}
}
return O.totalMakeSpan
}
/*func RecalculateMakeSpan(O []Organization) float32{
var globalMakeSpan float32 = 0.0
for N:=0; N < len(O); N++ {
var currentMakeSpan float32 = 0.0
for k:=0; k < len(O); k++{
for p:=0; p < len(O[k].p); p++{
for j:=0; j < len(O[k].p[p].jobs); j++ {
if O[k].p[p].jobs[j].id == O[N].num {
if currentMakeSpan < (O[k].p[p].jobs[j].timeToExecute + O[k].p[p].jobs[j].initTime){
currentMakeSpan = O[k].p[p].jobs[j].timeToExecute + O[k].p[p].jobs[j].initTime
}
}
}
}
}
O[N].totalMakeSpan = currentMakeSpan
if globalMakeSpan < O[N].totalMakeSpan {
globalMakeSpan = O[N].totalMakeSpan
}
}
return globalMakeSpan
}*/
func LocalSchedule(O []Organization, N int) float32 {
var globalMakeSpan float32 = 0.0
for k:=0; k < N; k++ {
O[k] = Schedule(O[k])
if globalMakeSpan < O[k].totalMakeSpan {
globalMakeSpan = O[k].totalMakeSpan
}
}
return globalMakeSpan
}
func Schedule(O Organization) Organization{
j := 0
//Schedule Jobs into J Processors
for i := 0; i < len(O.jobs); i++ {
if len(O.p[j].jobs) == 0 {
O.p[j].localMakeSpan = 0
}
O.jobs[i].initTime = O.p[j].localMakeSpan
O.p[j].localMakeSpan += O.jobs[i].timeToExecute
O.p[j].jobs = append(O.p[j].jobs, O.jobs[i])
j++
if len(O.p) == j {
j = 0
}
}
//Calculate TotalMakeSpan
O.totalMakeSpan = CalculateMakeSpan(O)
return O
}
func CreateOrganizations(k int) []Organization{
O := make([]Organization,k)
for i:= 0; i < k; i++{
O[i].num = i
n:= rand.Intn(900) + 100//number of Jobs
numProcessors := rand.Intn(10) + 1
O[i].p = make([]Processor,numProcessors)
O[i].jobs = MakeJobs(n,i)
O[i].totalMakeSpan = 0
}
return O
}
func ILBA(){
var N int = 5//inicial number of Organizations
//Create N Organizations
O := CreateOrganizations(N)
//Local Scheduling
LocalSchedule(O,N)
//Sort Orgs by MakeSpan
sort.Sort(ByMakeSpan(O))
//Global Scheduling ILBA
/*How ILBA works: For k = 2 until N (organizations), jobs from O(k) are rescheduled sequentially
* and assigned to the less loaded organizations O(1) .... O(k).
* Each job is rescheduled by ILBA either earlier or at the same time that the job was scheduled
* before the migration.
* In other words, no job is delayed by ILBA, which guarantees that the local constraint is respected
* for MOSP(Cmax) and MOSP(Sum of Ci)*/
for k:=1; k < N; k++ { //for each k organization
for i:=0; i < k; i++ {
for pi:=0; pi < len(O[i].p); pi++{ //Search if any job can be scheduled into Processor P(i)
for p := 0; p < len(O[k].p); p++ { //Jobs from Processor P(k)
for j := len(O[k].p[p].jobs)-1; j > -1; j-- { //Start from the job with the greatest initTime
//If check jobs that can be rescheduled
if O[k].p[p].jobs[j].initTime >= O[i].p[pi].localMakeSpan {
O[k].p[p].jobs[j].initTime = O[i].p[pi].localMakeSpan //update job initTime
//assign the job to the processor Pi from Organization O(i)
O[i].p[pi].jobs = append(O[i].p[pi].jobs, O[k].p[p].jobs[j])
time := O[k].p[p].jobs[j].timeToExecute
O[i].p[pi].localMakeSpan += time //update makeSpan of Processor(pi)
//Remove job rescheduled from processor p[k] of O[k]
copy(O[k].p[p].jobs[j:], O[k].p[p].jobs[j+1:])
O[k].p[p].jobs[len(O[k].p[p].jobs)-1].timeToExecute = 0.0
O[k].p[p].jobs[len(O[k].p[p].jobs)-1].initTime = 0.0
O[k].p[p].jobs = O[k].p[p].jobs[:len(O[k].p[p].jobs)-1]
O[k].p[p].localMakeSpan = O[k].p[p].localMakeSpan - time
//Finish removing
} else {
break; //No more Jobs from Processor p of O[k] can be rescheduled
}
}
}
}
}
}
//Finish ILBA. You can recalculate each local makespan by calling RecalculateMakeSpan
}