-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
206 lines (166 loc) · 4.45 KB
/
main.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
package main
import (
"flag"
)
// golParams provides the details of how to run the Game of Life and which image to load.
type golParams struct {
turns int
threads int
imageWidth int
imageHeight int
}
// ioCommand allows requesting behaviour from the io (pgm) goroutine.
type ioCommand uint8
// This is a way of creating enums in Go.
// It will evaluate to:
// ioOutput = 0
// ioInput = 1
// ioCheckIdle = 2
const (
ioOutput ioCommand = iota
ioInput
ioCheckIdle
)
type progState uint8
// Program state enum
const (
STOP progState = iota
PAUSE
CONTINUE
)
// cell is used as the return type for the testing framework.
type cell struct {
x, y int
}
// distributorToIo defines all chans that the distributor goroutine will have to communicate with the io goroutine.
// Note the restrictions on chans being send-only or receive-only to prevent bugs.
type distributorToIo struct {
command chan<- ioCommand
idle <-chan bool
outputVal chan<- uint8
filename chan<- string
inputVal <-chan uint8
}
// ioToDistributor defines all chans that the io goroutine will have to communicate with the distributor goroutine.
// Note the restrictions on chans being send-only or receive-only to prevent bugs.
type ioToDistributor struct {
command <-chan ioCommand
idle chan<- bool
outputVal <-chan uint8
filename <-chan string
inputVal chan<- uint8
}
// distributorChans stores all the chans that the distributor goroutine will use.
type distributorChans struct {
io distributorToIo
}
// ioChans stores all the chans that the io goroutine will use.
type ioChans struct {
distributor ioToDistributor
}
type outChans struct {
tChan chan<- byte
bChan chan<- byte
}
type inChans struct {
tChan <-chan byte
bChan <-chan byte
}
type chanType uint8
// WORLD: contains the world the worker is allocated
// TOPROW: contains the top halo
// BOTTOMROW: contains the bottom halo
const (
WORLD chanType = iota
TOPROW
BOTTOMROW
)
type workerComs uint8
// It will evaluate to:
// OUTPUT = 0
const (
OUTPUT workerComs = iota
INPUT
WORK
IDLE
)
// gameOfLife is the function called by the testing framework.
// It makes some channels and starts relevant goroutines.
// It places the created channels in the relevant structs.
// It returns an array of alive cells returned by the distributor.
func gameOfLife(p golParams, key chan rune) []cell {
var dChans distributorChans
var ioChans ioChans
ioCommand := make(chan ioCommand)
dChans.io.command = ioCommand
ioChans.distributor.command = ioCommand
ioIdle := make(chan bool)
dChans.io.idle = ioIdle
ioChans.distributor.idle = ioIdle
ioFilename := make(chan string)
dChans.io.filename = ioFilename
ioChans.distributor.filename = ioFilename
inputVal := make(chan uint8)
dChans.io.inputVal = inputVal
ioChans.distributor.inputVal = inputVal
outputVal := make(chan uint8)
dChans.io.outputVal = outputVal
ioChans.distributor.outputVal = outputVal
aliveCells := make(chan []cell)
workerChans := make([][]chan byte, p.threads)
comChans := make([]chan workerComs, p.threads)
for i := 0; i < p.threads; i++ {
workerChans[i] = make([]chan byte, 3)
for j := 0; j < 3; j++ {
workerChans[i][j] = make(chan byte, 1) // Made buffered
}
comChans[i] = make(chan workerComs)
}
remainder := p.imageHeight % p.threads
for i := 0; i < p.threads; i++ {
var in inChans
var out outChans
in.tChan = workerChans[(i-1+p.threads)%p.threads][BOTTOMROW]
in.bChan = workerChans[(i+1)%p.threads][TOPROW]
out.tChan = workerChans[i][TOPROW]
out.bChan = workerChans[i][BOTTOMROW]
var offset int
if remainder > i {
offset = 3
} else {
offset = 2
}
go worker(in, out, workerChans[i][WORLD], (p.imageHeight/p.threads + offset), p.imageWidth, comChans[i])
}
go distributor(p, dChans, aliveCells, workerChans, key, comChans)
go pgmIo(p, ioChans)
alive := <-aliveCells
return alive
}
// main is the function called when starting Game of Life with 'make gol'
// Do not edit until Stage 2.
func main() {
var params golParams
flag.IntVar(
¶ms.threads,
"t",
8,
"Specify the number of worker threads to use. Defaults to 8.")
flag.IntVar(
¶ms.imageWidth,
"w",
512,
"Specify the width of the image. Defaults to 512.")
flag.IntVar(
¶ms.imageHeight,
"h",
512,
"Specify the height of the image. Defaults to 512.")
flag.Parse()
params.turns = 1000000000
key := make(chan rune)
startControlServer(params)
go getKeyboardCommand(key)
gameOfLife(params, key)
StopControlServer()
}