Advertisement
Guest User

main.go

a guest
Nov 13th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 4.39 KB | None | 0 0
  1. package main
  2.  
  3. import "flag"
  4.  
  5. // golParams provides the details of how to run the Game of Life and which image to load.
  6. type golParams struct {
  7.     turns       int
  8.     threads     int
  9.     imageWidth  int
  10.     imageHeight int
  11.  
  12. }
  13.  
  14. // ioCommand allows requesting behaviour from the io (pgm) goroutine.
  15. type ioCommand uint8
  16.  
  17. // This is a way of creating enums in Go.
  18. // It will evaluate to:
  19. //      ioOutput    = 0
  20. //      ioInput     = 1
  21. //      ioCheckIdle = 2
  22. const (
  23.     ioOutput ioCommand = iota
  24.     ioInput
  25.     ioCheckIdle
  26. )
  27.  
  28. // cell is used as the return type for the testing framework.
  29. type cell struct {
  30.     x, y int
  31. }
  32.  
  33. // distributorToIo defines all chans that the distributor goroutine will have to communicate with the io goroutine.
  34. // Note the restrictions on chans being send-only or receive-only to prevent bugs.
  35. // here you receive the input values from io and then send the output values to io
  36. type distributorToIo struct {
  37.     command chan<- ioCommand
  38.     idle    <-chan bool
  39.  
  40.     filename  chan<- string
  41.     inputVal  <-chan uint8 // left arrow means receive , right arrow means send
  42.  
  43.  
  44.     // added code here . channel to output image
  45.     outputVal chan<-  uint8
  46. }
  47.  
  48. // ioToDistributor defines all chans that the io goroutine will have to communicate with the distributor goroutine.
  49. // Note the restrictions on chans being send-only or receive-only to prevent bugs.
  50. // here you send the input values to io and receive the output values from distributor
  51. type ioToDistributor struct {
  52.     command <-chan ioCommand
  53.     idle    chan<- bool
  54.  
  55.     filename  <-chan string
  56.     inputVal  chan<- uint8
  57.     // added code here . channel to output image
  58.     outputVal <-chan uint8
  59. }
  60.  
  61. // distributorChans stores all the chans that the distributor goroutine will use.
  62. type distributorChans struct {
  63.     io distributorToIo
  64. }
  65.  
  66. // ioChans stores all the chans that the io goroutine will use.
  67. type ioChans struct {
  68.     distributor ioToDistributor
  69. }
  70.  
  71. // gameOfLife is the function called by the testing framework.
  72. // It makes some channels and starts relevant goroutines.
  73. // It places the created channels in the relevant structs.
  74. // It returns an array of alive cells returned by the distributor.
  75. func gameOfLife(p golParams, keyChan <-chan rune) []cell { //gets called when tests are called
  76.     var dChans distributorChans
  77.     var ioChans ioChans
  78.  
  79.     ioCommand := make(chan ioCommand)
  80.     dChans.io.command = ioCommand
  81.     ioChans.distributor.command = ioCommand //tell io what to do
  82.  
  83.     ioIdle := make(chan bool)
  84.     dChans.io.idle = ioIdle
  85.     ioChans.distributor.idle = ioIdle
  86.  
  87.     ioFilename := make(chan string)
  88.     dChans.io.filename = ioFilename
  89.     ioChans.distributor.filename = ioFilename
  90.  
  91.     inputVal := make(chan uint8)
  92.     dChans.io.inputVal = inputVal
  93.     ioChans.distributor.inputVal = inputVal //input values recieved on io
  94.  
  95.     // channel to output the image of the board
  96.     outputVal := make(chan uint8)
  97.     dChans.io.outputVal = outputVal
  98.     ioChans.distributor.outputVal = outputVal
  99.  
  100.     aliveCells := make(chan []cell)
  101.  
  102.  
  103.     // Array of channel to be passed to workers and used by workers to pass the world back to
  104.     // distributor
  105.     var wChans = make([] chan[][] byte,p.threads)
  106.     for i:=0; i<p.threads; i++ {
  107.         wChans[i] = make(chan [][] byte)
  108.     }
  109.  
  110.  
  111.     //output  channel in workers which sends worker slices back to distributor
  112.     // distributor receives slices from this channel and reconstruct the world
  113.     var workerchan = make ([]chan [][] byte,p.threads)
  114.     for i:=0; i<p.threads; i++ {
  115.         workerchan[i] = make(chan [][] byte)
  116.     }
  117.  
  118.  
  119.     go distributor(p, dChans, aliveCells,wChans)
  120.     worldSlice := wChans
  121.     go pgmIo(p, ioChans)
  122.  
  123.     // Worker threads here
  124.     for i:=0; i<p.threads; i++ {
  125.             go worker(p, aliveCells, dChans,worldSlice[i],workerchan[i])
  126.         }
  127.  
  128.  
  129. // for number of turns :
  130. // for i:=0 to h/number of workers ++i
  131. // sends to worker[i]
  132.     alive := <-aliveCells
  133.     return alive
  134. }
  135.  
  136. // main is the function called when starting Game of Life with 'make gol'
  137. // Do not edit until Stage 2.
  138. func main() {
  139.     var params golParams
  140.  
  141.     flag.IntVar(
  142.         &params.threads,
  143.         "t",
  144.         8,
  145.         "Specify the number of worker threads to use. Defaults to 8.")
  146.  
  147.     flag.IntVar(
  148.         &params.imageWidth,
  149.         "w",
  150.         512,
  151.         "Specify the width of the image. Defaults to 512.")
  152.  
  153.     flag.IntVar(
  154.         &params.imageHeight,
  155.         "h",
  156.         512,
  157.         "Specify the height of the image. Defaults to 512.")
  158.  
  159.     flag.Parse()
  160.  
  161.     params.turns = 10000000000
  162.  
  163.     startControlServer(params)
  164.     gameOfLife(params, nil)
  165.     StopControlServer()
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement