Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- )
- // 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.
- // here you receive the input values from io and then send the output values to io
- type distributorToIo struct {
- command chan<- ioCommand
- idle <-chan bool
- filename chan<- string
- inputVal <-chan uint8 // left arrow means receive , right arrow means send
- // added code here . channel to output image
- outputVal 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.
- // here you send the input values to io and receive the output values from distributor
- type ioToDistributor struct {
- command <-chan ioCommand
- idle chan<- bool
- filename <-chan string
- inputVal chan<- uint8
- // added code here . channel to output image
- outputVal <-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
- }
- // 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, keyChan <-chan rune) []cell { //gets called when tests are called
- var dChans distributorChans
- var ioChans ioChans
- ioCommand := make(chan ioCommand)
- dChans.io.command = ioCommand
- ioChans.distributor.command = ioCommand //tell io what to do
- 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 //input values recieved on io
- // channel to output the image of the board
- outputVal := make(chan uint8)
- dChans.io.outputVal = outputVal
- ioChans.distributor.outputVal = outputVal
- aliveCells := make(chan []cell)
- // Array of channel to be passed to workers and used by workers to pass the world back to
- // distributor
- var wChans = make([] chan[][] byte,p.threads)
- for i:=0; i<p.threads; i++ {
- wChans[i] = make(chan [][] byte)
- }
- //output channel in workers which sends worker slices back to distributor
- // distributor receives slices from this channel and reconstruct the world
- var workerchan = make ([]chan [][] byte,p.threads)
- for i:=0; i<p.threads; i++ {
- workerchan[i] = make(chan [][] byte)
- }
- go distributor(p, dChans, aliveCells,wChans)
- worldSlice := wChans
- go pgmIo(p, ioChans)
- // Worker threads here
- for i:=0; i<p.threads; i++ {
- go worker(p, aliveCells, dChans,worldSlice[i],workerchan[i])
- }
- // for number of turns :
- // for i:=0 to h/number of workers ++i
- // sends to worker[i]
- 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 = 10000000000
- startControlServer(params)
- gameOfLife(params, nil)
- StopControlServer()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement