-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschedule.go
More file actions
58 lines (45 loc) · 1.1 KB
/
schedule.go
File metadata and controls
58 lines (45 loc) · 1.1 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
package mr
import (
"fmt"
"sync"
)
// schedule starts and waits for all tasks in the given phase (MapFunc or ReduceFunc).
func (m *MrMaster) schedule(phase jobPhase) {
var nTasks int
var nInputOrOutput int // number of inputs (for reduce) or outputs (for map)
switch phase {
case mapPhase:
nTasks = len(m.files)
nInputOrOutput = m.nReduce
case reducePhase:
nTasks = m.nReduce
nInputOrOutput = len(m.files)
}
fmt.Printf("Task(%s): %v, %v\n", phase, nTasks, nInputOrOutput)
var wg sync.WaitGroup
for i := 0 ; i < nTasks; i++ {
wg.Add(1)
go func(phase jobPhase, iTask int, numOtherPhase int) {
var worker MrMasterWorker
for {
worker = <-m.registerChannel
var args DoTaskArgs
args.JobName = m.jobName
args.Phase = phase
args.TaskNumber = iTask
args.NumberOtherPhase = numOtherPhase
if phase == mapPhase {
args.File = m.files[iTask]
}
reply := struct{}{}
ok := call("MrWorker.DoTask", worker.WorkerAddr, &args, &reply)
if ok {
wg.Done()
m.registerChannel <- worker
return
}
}
} (phase, i, nInputOrOutput)
}
wg.Wait()
}