-
Notifications
You must be signed in to change notification settings - Fork 0
/
gol.go
311 lines (280 loc) · 8.01 KB
/
gol.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
package main
import (
"fmt"
"strconv"
"strings"
)
type worker struct {
upperSend chan<- byte
upperGet <-chan byte
lowerSend chan<- byte
lowerGet <-chan byte
//signal chan bool
}
func allocateSlice(height int, width int) [][]byte {
slice := make([][]byte, height)
for i := range slice {
slice[i] = make([]byte, width)
}
return slice
}
func calculateThreadHeight(p golParams) []int {
heightSlice := make([]int, p.threads)
leftover := p.imageHeight % p.threads
for i := range heightSlice {
heightSlice[i] = p.imageHeight / p.threads
}
if leftover != 0 {
for j := 0; leftover > 0 && j < p.threads; leftover-- {
heightSlice[j]++
j = (j + 1) % p.threads
}
}
return heightSlice
}
func golLogic(start [][]byte) [][]byte {
height := len(start)
width := len(start[0])
//init result
result := allocateSlice(height, width)
for y := 1; y < height-1; y++ {
for x := 0; x < width; x++ {
count := 0
//counts neighbors
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
if start[y-1+i][(x-1+j+width)%width] == 0xFF {
count++
}
}
}
//calculating alive or dead
if start[y][x] == 0xFF { //if the cell is alive
count-- //excludes itself
if count < 2 || count > 3 {
result[y][x] = 0x00
} else {
result[y][x] = 0xFF
}
} else { //if the cell is dead
if count == 3 {
result[y][x] = 0xFF
} else {
result[y][x] = 0x00
}
}
}
}
return result
}
func outputBoard(p golParams, d distributorChans, world [][]byte, turn int) {
d.io.command <- ioOutput
d.io.filename <- strings.Join([]string{strconv.Itoa(p.imageWidth), strconv.Itoa(p.imageHeight), strconv.Itoa(turn)}, "x")
for y := 0; y < p.imageHeight; y++ {
for x := 0; x < p.imageWidth; x++ {
d.io.outputVal <- world[y][x]
}
}
}
func countAlive(p golParams, world [][]byte) []cell {
// Create an empty slice to store coordinates of cells that are still alive after p.turns are done.
var finalAlive []cell
// Go through the world and append the cells that are still alive.
for y := 0; y < p.imageHeight; y++ {
for x := 0; x < p.imageWidth; x++ {
if world[y][x] != 0 {
finalAlive = append(finalAlive, cell{x: x, y: y})
}
}
}
return finalAlive
}
func golWorker(p golParams, cellChan <-chan byte, out chan<- [][]byte, heightInfo int) {
height := heightInfo + 2
width := p.imageWidth
//Makes thread with incoming cells
start := allocateSlice(height, width)
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
cell := <-cellChan
start[y][x] = cell
}
}
out <- golLogic(start)
}
func removeHalo(input [][]byte) [][]byte {
height := len(input) - 2
width := len(input[0])
output := allocateSlice(height, width)
for y := 1; y < len(input)-1; y++ {
for x := 0; x < width; x++ {
output[y-1][x] = input[y][x]
}
}
return output
}
// distributor divides the work between workers and interacts with other goroutines.
func distributor(p golParams, d distributorChans, alive chan []cell) {
// Create the 2D slice to store the world.
// Create new world here
world := make([][]byte, p.imageHeight)
newWorld := make([][]byte, p.imageHeight)
for i := range world {
world[i] = make([]byte, p.imageWidth)
newWorld[i] = make([]byte, p.imageWidth)
}
// Request the io goroutine to read in the image with the given filename.
d.io.command <- ioInput
d.io.filename <- strings.Join([]string{strconv.Itoa(p.imageWidth), strconv.Itoa(p.imageHeight)}, "x")
// The io goroutine sends the requested image byte by byte, in rows.
for y := 0; y < p.imageHeight; y++ {
for x := 0; x < p.imageWidth; x++ {
val := <-d.io.inputVal
if val != 0 {
fmt.Println("Alive cell at", x, y)
world[y][x] = val
newWorld[y][x] = val
}
}
}
//Initialise halo channels
golWorkerHaloExchanges := make([]chan byte, 2*p.threads)
for i := range golWorkerHaloExchanges {
golWorkerHaloExchanges[i] = make(chan byte)
}
workers := make([]worker, p.threads)
for i := 0; i < p.threads; i++ {
//worker[0] to worker[3]: Senders
//worker[4] to worker[7]: Getters
//w(i).upperGet = w(i - 1).lowerSend
//w(i).lowerGet = w(i + 1).upperSend
workers[i] = worker{
upperSend: golWorkerHaloExchanges[i],
upperGet: golWorkerHaloExchanges[i+p.threads],
lowerSend: golWorkerHaloExchanges[i+1],
lowerGet: golWorkerHaloExchanges[i-1+p.threads]}
}
//Calculating thread height
golThreadHeights := calculateThreadHeight(p)
for a := range golThreadHeights {
fmt.Printf("Thread %d has height %d\n", a, golThreadHeights[a])
}
golCumulativeThreadHeights := make([]int, p.threads)
golCumulativeThreadHeights[0] = golThreadHeights[0]
fmt.Printf("Thread %d has cumulative height %d\n", 0, golCumulativeThreadHeights[0])
for i := 1; i < len(golCumulativeThreadHeights); i++ {
golCumulativeThreadHeights[i] = golCumulativeThreadHeights[i-1] + golThreadHeights[i]
fmt.Printf("Thread %d has cumulative height %d\n", i, golCumulativeThreadHeights[i])
}
//Init slices of channels and slices of threads
//Slice of threads after gol logic with halo
golHalos := make([][][]byte, p.threads)
//Slice of threads after removing halo
golNonHalos := make([][][]byte, p.threads)
//Slice of channel of workers before gol logic
golWorkerChans := make([]chan byte, p.threads)
//Slice of channel of workers after gol logic
golResultChans := make([]chan [][]byte, p.threads)
for i := range golResultChans {
golResultChans[i] = make(chan [][]byte, golThreadHeights[i]+2)
}
for i := range golWorkerChans {
golWorkerChans[i] = make(chan byte)
}
// Calculate the new state of Game of Life after the given number of turns.
for turns := 0; turns < p.turns; turns++ {
//Go routine starts here
for i := range golWorkerChans {
go golWorker(p, golWorkerChans[i], golResultChans[i], golThreadHeights[i])
}
//Upper halo
for x := 0; x < p.imageWidth; x++ {
for i := range golWorkerChans {
golWorkerChans[i] <- world[golCumulativeThreadHeights[((i-1)+p.threads)%p.threads]-1][x]
}
}
//mid
for y := 0; y < golCumulativeThreadHeights[0]; y++ {
for x := 0; x < p.imageWidth; x++ {
golWorkerChans[0] <- world[y][x]
}
}
for i := 1; i < p.threads; i++ {
for y := golCumulativeThreadHeights[i-1]; y < golCumulativeThreadHeights[i]; y++ {
for x := 0; x < p.imageWidth; x++ {
golWorkerChans[i] <- world[y][x]
}
}
}
//Lower halo
for x := 0; x < p.imageWidth; x++ {
for i := range golWorkerChans {
golWorkerChans[i] <- world[golCumulativeThreadHeights[i]%p.imageHeight][x]
}
}
//Remove halo
for i := range golResultChans {
golHalos[i] = <-golResultChans[i]
golNonHalos[i] = removeHalo(golHalos[i])
//Passing threads without halo to new world
if i == 0 {
for y := 0; y < golThreadHeights[0]; y++ {
for x := 0; x < p.imageWidth; x++ {
newWorld[y][x] = golNonHalos[i][y][x]
}
}
} else {
h := 0
for y := golCumulativeThreadHeights[i-1]; y < golCumulativeThreadHeights[i]; y++ {
for x := 0; x < p.imageWidth; x++ {
newWorld[y][x] = golNonHalos[i][h][x]
}
h++
}
}
}
//Updating the world with new world
for y := 0; y < p.imageHeight; y++ {
for x := 0; x < p.imageWidth; x++ {
world[y][x] = newWorld[y][x]
}
}
//Keyboard input section
//r: input signal from rune channel
//t: 2 seconds time signal from bool channel
select {
case r := <-d.io.keyChan:
if r == 's' {
outputBoard(p, d, world, turns)
}
if r == 'p' {
fmt.Println("Execution Paused")
for x := true; x == true; {
select {
case pauseInput := <-d.io.keyChan:
if pauseInput == 'p' {
x = false
fmt.Println("Continuing")
}
}
}
}
if r == 'q' {
p.turns = turns
}
case t := <-d.io.timeChan:
if t {
fmt.Println(len(countAlive(p, world)))
}
default:
}
//Keyboard input section end
}
finalAlive := countAlive(p, world)
outputBoard(p, d, world, p.turns)
// Make sure that the Io has finished any output before exiting.
d.io.command <- ioCheckIdle
<-d.io.idle
// Return the coordinates of cells that are still alive.
alive <- finalAlive
}