/
dagsched.go
635 lines (531 loc) · 16.9 KB
/
dagsched.go
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/***************************************************************************
* DAG Scheduler simulator, Mike and Rick UofO CIS-410 Distributed Scheduling
*
***************************************************************************/
package main
import fmt "fmt" // Package implementing formatted I/O.
import flag "flag" // Command line parsing
import parser "./parser" // structure and code for DAGs
import sorter "./sorter" // topological sort
import vec "container/vector"
import gt "./getTimes" // get time info from dags
func main() {
// Command line flags
var numcores *int = flag.Int("n", 2,
"number of cores to use in the simulation [-n Int value]")
var infname *string = flag.String("f", "infile.dag",
"filename to load the .dag from [-f filename.dag]")
var algtype *string = flag.String("a", "tl",
"algorithm type to use tl=t-level, bl=b-level, or ??")
flag.Parse()
//fmt.Printf("simulating using %d cores\n", *numcores)
//fmt.Printf("loading DAG from %s\n", *infname)
//fmt.Printf("using %s scheduling algorithm\n", *algtype)
// Read in the dag we want to schedule
var dag = parser.ParseFile(*infname)
switch *algtype {
case "tl":
// Schedule using t-level
ScheduleTlevel(dag, *numcores)
case "bl":
// schedule using b-level
ScheduleBlevel(dag, *numcores)
case "c":
ScheduleC(dag, *numcores)
}
//fmt.Printf("fwd: %v\n", sorter.TSort (dag, 'f'))
//fmt.Printf("rev: %v\n", sorter.TSort (dag, 'r'))
//fmt.Printf("SeqTime %v\n", gt.SeqTime(dag))
//parser.PrintDAG(dag)
//fmt.Printf("CPTime %v\n", gt.CPTime(dag))
//parser.PrintDAG(dag)
}
type Event struct {
id int
start int64
end int64
}
// schedule using t-level Earliest Start time 1st
func ScheduleTlevel(indag vec.Vector, ncpus int) (){
var el int64
var nt int
var iccost bool
// produce a topographically sorted DAG to work with
var dag = sorter.TopSort(indag, 't')
cpu:=make([]int, len(dag)) // cpu is a slice as long as the dag
for i:=0; i < len(dag); i++ {
cpu[i]= -1
}
// Create a schedule as a vector of Event Vectors
Schedule:= make([]vec.Vector, ncpus)
//update the t-level of the root node
dag.At(0).(*parser.Node).Lev=0
//recursively update the t-levels of it's children
tlUpdateChildren(dag,0)
startTime := make([]int64, ncpus) // holds current start times
// initialize all start times to 0
for i:=0; i <ncpus; i++ {
startTime[i]=0
}
//iterate over the dag, till we've done every node.
for i:=0; i < len(dag); i++ {
var coreChosen = -1
var earliestAvail int64
var earliestFeasable int64
var efc int
esT := make([]int64, ncpus)
pet :=new(vec.Vector)
pCpu :=new(vec.Vector)
iccost=false
el=9223372036854775807 // largest signed integer
earliestAvail=9223372036854775807
earliestFeasable=9223372036854775807
nt=0
// Select the next task to Schedule
// look through the dag for the task with the lowest t-level
for j:=0; j < len(dag); j++ {
if (cpu[j] < 0) && (dag.At(j).(*parser.Node).Lev < el) {
nt=j
el=dag.At(j).(*parser.Node).Lev
}
}
// select a cpu to schedule it on
for c:=ncpus-1; c >= 0; c-- {
if startTime[c] <= el {
coreChosen = c
}
}
// if none is obvious then we'll record some info and default to 0
if coreChosen == -1 {
for c:=ncpus-1; c >= 0; c-- {
if startTime[c] <= earliestAvail {
earliestAvail = startTime[c]
coreChosen = c
}
}
}
//see if we have to account for communications time
//check each parent to see what cpu it was scheduled on
//eA=0
for j:=0; j < len(dag.At(nt).(*parser.Node).Pl); j++ {
// set up scratch variables, otherwise the statement to capture
// what we want is too long and complex.
cParentId:=dag.At(nt).(*parser.Node).Pl.At(j).(*parser.Rel).Id
comC := dag.At(nt).(*parser.Node).Pl.At(j).(*parser.Rel).Cc
cParent:=parser.GetIndexById(dag, cParentId)
cpCore := cpu[cParent]
cpIndex := findInSchedule(Schedule[cpCore], cParentId)
// Here's what we want
pet.Push(Schedule[cpCore].At(cpIndex).(*Event).end + comC)
pCpu.Push(cpCore)
// check and see if we need to account for comm costs.
if cpu[cParent]!= coreChosen {
iccost=true
}
}
//find the core where the parentend+comm cost is least
efc=0
for c:=0; c < ncpus; c++ {
esT[c]=startTime[c]
for p:=0; p < pCpu.Len(); p++ {
if pCpu.At(p) != c {
if pet.At(p).(int64) > esT[c] {
esT[c] = pet.At(p).(int64)
}
}
}
if esT[c] < earliestFeasable {
earliestFeasable = esT[c]
efc=c
}
}
el=startTime[coreChosen]
// if we have to account for comm overhead chose this core
if iccost {
el = earliestFeasable
coreChosen = efc
}
// prepare the event
ccEvent:=new(Event)
ccEvent.id=dag.At(nt).(*parser.Node).Id
ccEvent.start=el
ccEvent.end=el+dag.At(nt).(*parser.Node).Ex
// update the available startTime for the core we chose
startTime[coreChosen] = ccEvent.end
// schedule the task on a core
Schedule[coreChosen].Push(ccEvent)
cpu[nt]=coreChosen
if dag.At(nt).(*parser.Node).Lev!=el {
//update the t-level of the current node
dag.At(nt).(*parser.Node).Lev=el
//update the t-levels of any children
tlUpdateChildren(dag,nt)
}
}
// PrintSchedule(Schedule)
makespan:=findScheduleEnd(Schedule)
fmt.Printf("%d,", (len(dag)-2)) // don't count the 0 time start and end tasks
fmt.Printf("%d,", gt.SeqTime(dag))
fmt.Printf("%d,", makespan)
fmt.Printf("%d,", gt.CPTime(dag))
fmt.Printf("%v\n", float64(SumExTime(Schedule)) / float64(makespan*int64(ncpus)))
}
func ScheduleBlevel(indag vec.Vector, ncpus int) (){
var el int64
var nt int
var iccost bool
// produce a topographically sorted DAG to work with
var dag = sorter.TopSort(indag, 'b')
//update the b-level of the end node
dag.At(0).(*parser.Node).Lev=0
//recursively update the b-levels of it's parents
blUpdateChildren(dag,0)
//parser.PrintDAG(dag)
cpu:=make([]int, len(dag)) // cpu is a slice as long as the dag
for i:=0; i < len(dag); i++ {
cpu[i]= -1
}
// Create a schedule as a vector of Event Vectors
Schedule:= make([]vec.Vector, ncpus)
startTime := make([]int64, ncpus) // holds current start times
// initialize all start times to 0
for i:=0; i <ncpus; i++ {
startTime[i]=0
}
// Iterate over the dag, each time selecting the node with the highest b-level
for i:=0; i < len(dag); i++ {
var coreChosen = -1
var earliestAvail int64
var earliestFeasable int64
var efc int
esT := make([]int64, ncpus)
pet :=new(vec.Vector)
pCpu :=new(vec.Vector)
iccost=false
el=0 // largest signed integer
earliestAvail=9223372036854775807
earliestFeasable=9223372036854775807
nt=0
// Select the next task to Schedule
// look through the dag for the task with the highest b-level
for j:=0; j < len(dag); j++ {
if (cpu[j] < 0) && (dag.At(j).(*parser.Node).Lev >= el) {
nt=j
el=dag.At(j).(*parser.Node).Lev
}
}
// if none is obvious then we'll record some info and default to 0
//if coreChosen == -1 {
for c:=ncpus-1; c >= 0; c-- {
if startTime[c] <= earliestAvail {
earliestAvail = startTime[c]
coreChosen = c
}
}
//}
//see if we have to account for communications time
//check each parent to see what cpu it was scheduled on
if coreChosen == -1 {
coreChosen=0
}
for j:=0; j < len(dag.At(nt).(*parser.Node).Pl); j++ {
// set up scratch variables, otherwise the statement to capture
// what we want is too long and complex.
cParentId:=dag.At(nt).(*parser.Node).Pl.At(j).(*parser.Rel).Id
comC := dag.At(nt).(*parser.Node).Pl.At(j).(*parser.Rel).Cc
cParent:=parser.GetIndexById(dag, cParentId)
cpCore := cpu[cParent]
cpIndex := findInSchedule(Schedule[cpCore], cParentId)
// Here's what we want
pet.Push(Schedule[cpCore].At(cpIndex).(*Event).end + comC)
pCpu.Push(cpCore)
// check and see if we need to account for comm costs.
if cpu[cParent]!= coreChosen {
iccost=true
}
}
//find the core where the parentend+comm cost is least
efc=0
for c:=0; c < ncpus; c++ {
esT[c]=startTime[c]
for p:=0; p < pCpu.Len(); p++ {
if pCpu.At(p) != c {
if pet.At(p).(int64) > esT[c] {
esT[c] = pet.At(p).(int64)
}
}
}
if esT[c] < earliestFeasable {
earliestFeasable = esT[c]
efc=c
}
}
el=startTime[coreChosen]
// if we have to account for comm overhead chose this core
if iccost {
el = earliestFeasable
coreChosen = efc
}
// prepare the event
ccEvent:=new(Event)
ccEvent.id=dag.At(nt).(*parser.Node).Id
ccEvent.start=el
ccEvent.end=el+dag.At(nt).(*parser.Node).Ex
//fmt.Printf("Core %d got Event: %v\n", coreChosen, ccEvent)
// update the available startTime for the core we chose
startTime[coreChosen] = ccEvent.end
// schedule the task on a core
Schedule[coreChosen].Push(ccEvent)
cpu[nt]=coreChosen
}
//PrintSchedule(Schedule)
// Print the output
makespan:=findScheduleEnd(Schedule)
fmt.Printf("%d,", (len(dag)-2)) // don't count the 0 time start and end tasks
fmt.Printf("%d,", gt.SeqTime(dag))
fmt.Printf("%d,", makespan)
fmt.Printf("%d,", gt.CPTime(dag))
fmt.Printf("%v\n", float64(SumExTime(Schedule)) / float64(makespan*int64(ncpus)))
}
// Given the id of a task that has been scheduled on a processor
// return the index of that Task's Event entry
func findInSchedule(v vec.Vector, id int) (int){
for i:=0; i<len(v); i++ {
if v.At(i).(*Event).id == id {
return i
}
}
return -1
}
// returns a sum of the time
func SumExTime (S []vec.Vector) (int64){
var tsum int64
for i:=0; i < len(S); i++ {
for j:=0; j < S[i].Len(); j++ {
tsum += S[i].At(j).(*Event).end - S[i].At(j).(*Event).start
}
// other variant
/*if S[i].Len() > 0 {
tsum += S[i].Last().(*Event).end - S[i].At(0).(*Event).start
}*/
}
return tsum
}
// Returns the end time of the last task scheduled
func findScheduleEnd(S []vec.Vector) (int64){
var en int64
en=0
for i:=0; i < len(S); i++ {
if en < S[i].Last().(*Event).end {
en = S[i].Last().(*Event).end
}
}
return en
}
// Print out a schedule
// needed to verify that we're getting real schedules out of this
func PrintSchedule(Schedule []vec.Vector) () {
for i:=0; i< len(Schedule); i++ {
fmt.Printf("Schedule for core%d: %d tasks\n", i,len(Schedule[i]) )
for j:=0; j < len(Schedule[i]); j++ {
fmt.Printf("task %d, from %d to %d\n", Schedule[i].At(j).(*Event).id,
Schedule[i].At(j).(*Event).start, Schedule[i].At(j).(*Event).end)
}
}
}
// given a dag and a node within that dag update the t-levels of all children
// of that node
func tlUpdateChildren(dag vec.Vector, nt int) {
visited:=make([]bool, len(dag))
for ii:=0; ii < len(dag); ii++ {
visited[ii]=false
}
//recurse
for jj:=0; jj<len((dag.At(nt).(*parser.Node)).Cl); jj++ {
if !(visited[parser.GetIndexById(dag,
(dag.At(nt).(*parser.Node)).Cl.At(jj).(*parser.Rel).Id)]) {
tlUpdate(dag, visited, parser.GetIndexById(dag,
(dag.At(nt).(*parser.Node)).Cl.At(jj).(*parser.Rel).Id))
}
}
}
// given a dag and a node within that dag update the b-levels of all children
// of that node
func blUpdateChildren(dag vec.Vector, nt int) {
var max int64
var cCost int64
var linkW int64
// for each node
for jj:=0; jj<len(dag); jj++ {
max=0
// for each child node
for kk:=0; kk<len((dag.At(jj).(*parser.Node)).Cl); kk++ {
cIndex:=parser.GetIndexById(dag,
(dag.At(jj).(*parser.Node)).Cl.At(kk).(*parser.Rel).Id)
cLevel:=(dag.At(cIndex).(*parser.Node)).Lev
linkW= (dag.At(jj).(*parser.Node)).Cl.At(kk).(*parser.Rel).Cc
cCost=(dag.At(cIndex).(*parser.Node)).Ex
if cCost + cLevel > max {
max= cCost +cLevel
}
}
//actually set the level
dag.At(jj).(*parser.Node).Lev = max +linkW
}
}
// The recursive helper function for tlUpdateChildren
func tlUpdate(dag vec.Vector, visited []bool, nt int) {
var max int64
var pCost int64
max=0
if visited[nt] {
return
}
for j:=0; j < len((dag.At(nt).(*parser.Node)).Pl); j++ {
pIndex:=parser.GetIndexById(dag,
(dag.At(nt).(*parser.Node)).Pl.At(j).(*parser.Rel).Id)
pLevel:=(dag.At(pIndex).(*parser.Node)).Lev
linkW:= (dag.At(nt).(*parser.Node)).Pl.At(j).(*parser.Rel).Cc
pCost=(dag.At(pIndex).(*parser.Node)).Ex
if ( pLevel + linkW + pCost) > max {
max = pLevel + linkW + pCost
}
}
//actually set the level
dag.At(nt).(*parser.Node).Lev = max
visited[nt]=true
//recurse
for jj:=0; jj<len((dag.At(nt).(*parser.Node)).Cl); jj++ {
tlUpdate(dag, visited, parser.GetIndexById(dag,
(dag.At(nt).(*parser.Node)).Cl.At(jj).(*parser.Rel).Id))
}
}
// The recursive helper function for blUpdateChildren
func blUpdate(dag vec.Vector, visited []bool, nt int) {
var max int64
var cCost int64
var linkW int64
max=0
if visited[nt] {
return
}
for j:=0; j < len((dag.At(nt).(*parser.Node)).Cl); j++ {
cIndex:=parser.GetIndexById(dag,
(dag.At(nt).(*parser.Node)).Cl.At(j).(*parser.Rel).Id)
cLevel:=(dag.At(cIndex).(*parser.Node)).Lev
linkW= (dag.At(nt).(*parser.Node)).Cl.At(j).(*parser.Rel).Cc
cCost=(dag.At(cIndex).(*parser.Node)).Ex
if ( cLevel + cCost) > max {
max = cLevel + cCost
}
}
//actually set the level
dag.At(nt).(*parser.Node).Lev = max + linkW
visited[nt]=true
//recurse
for jj:=0; jj<len((dag.At(nt).(*parser.Node)).Pl); jj++ {
blUpdate(dag, visited, parser.GetIndexById(dag,
(dag.At(nt).(*parser.Node)).Pl.At(jj).(*parser.Rel).Id))
}
}
// chain following scheduler
// this will attempt to follow a chain from parent to child on one core,
// then move to the next available core and continue
// it assumes all communication costs are NULL and only depends on parent
// completion
func ScheduleC(dag vec.Vector, ncpus int) (){
startTime := make([]int64, ncpus) // holds current start times
// initialize all start times to 0
for i:=0; i <ncpus; i++ {
startTime[i]=0
}
// create a slice to hold the number of parents that must complete before we
// can schedule the task
pReq:=make([]int, len(dag))
for i:=0; i < len(dag); i++ {
pReq[i] = dag.At(i).(*parser.Node).Pl.Len()
}
//parser.PrintDAG(dag)
st:=make([]int64, len(dag))
for i:=0; i < len(dag); i++ {
st[i] = 0
}
// Create a schedule as a vector of Event Vectors
Schedule:= make([]vec.Vector, ncpus)
cpu:=make([]int, len(dag)) // cpu is a slice as long as the dag
for i:=0; i < len(dag); i++ {
cpu[i]= -1
}
for i:=0; i < len(dag); i++ {
if (pReq[i] == 0) && (cpu[i] < 0) {
ScheduleCrec(dag, ncpus, i, startTime, 0, pReq, Schedule, cpu, st)
}
//fmt.Printf("Starting Again\n")
}
//PrintSchedule(Schedule)
makespan:=findScheduleEnd(Schedule)
fmt.Printf("%d,", (len(dag)-2)) // don't count the 0 time start and end tasks
fmt.Printf("%d,", gt.SeqTime(dag))
fmt.Printf("%d,", makespan)
fmt.Printf("%d,", gt.CPTime(dag))
fmt.Printf("%v\n", float64(SumExTime(Schedule)) / float64(makespan*int64(ncpus)))
}
func ScheduleCrec(dag vec.Vector, ncpus int, current int, startTime []int64, lsTime int64, pReq []int, Schedule []vec.Vector, cpu []int, st []int64) {
//fmt.Printf("Task %d, can start at %d, parents required %v\n", dag.At(current).(*parser.Node).Id, lsTime, pReq)
//var el int64
//var nt int
//var iccost bool
// produce a topographically sorted DAG to work with
// it's better to start at the beginning
//var dag = sorter.TopSort(indag, 't')
var cChosen = -1
var eAvail int64
eAvail = 9223372036854775807
var eAvailC = 0
//find the first available node where all parents are ready
//Schedule task
// find the first available core
for j:=0; j < ncpus; j++ {
// if we find a core with a start time before our's chose it
if startTime[j] < st[current] {
cChosen = j
}
// but record the earliest available start time
if startTime[j] < eAvail {
eAvail = startTime[j]
eAvailC = j
}
}
if cChosen == -1 {
cChosen = eAvailC
st[current] = eAvail
}
//Schedule the event on this core
// prepare the event
ccEvent:=new(Event)
ccEvent.id=dag.At(current).(*parser.Node).Id
ccEvent.start=st[current]
ccEvent.end=st[current]+dag.At(current).(*parser.Node).Ex
// update the available startTime for the core we chose
startTime[cChosen] = ccEvent.end
// schedule the task on a core
Schedule[cChosen].Push(ccEvent)
cpu[current]=cChosen
//update children's pReq entries
// create a slice to contain the dag entries of the children
cIndexes:= make([]int, dag.At(current).(*parser.Node).Cl.Len())
for j:=0; j < dag.At(current).(*parser.Node).Cl.Len(); j++ {
cChildId:=dag.At(current).(*parser.Node).Cl.At(j).(*parser.Rel).Id
cIndexes[j]= parser.GetIndexById(dag, cChildId)
pReq[cIndexes[j]]--
if st[cIndexes[j]] < ccEvent.end {
st[cIndexes[j]] = ccEvent.end
}
}
//recurse
for jj:=0; jj < len(cIndexes); jj++ {
if (cpu[cIndexes[jj]] < 0) && (pReq[cIndexes[jj]] == 0 ) {
ScheduleCrec(dag, ncpus, cIndexes[jj], startTime, ccEvent.end, pReq, Schedule, cpu, st)
}
}
}