[2f6a9391] | 1 | package main |
---|
| 2 | |
---|
| 3 | import ( |
---|
| 4 | "fmt" |
---|
| 5 | "sync" |
---|
| 6 | "time" |
---|
| 7 | "runtime" |
---|
| 8 | "os" |
---|
| 9 | "strconv" |
---|
| 10 | ) |
---|
| 11 | |
---|
| 12 | var Processors, Channels, ProdsPerChan, ConsPerChan, ChannelSize int = 1, 1, 1, 1, 128 |
---|
| 13 | var cons_done, prod_done bool = false, false; |
---|
| 14 | var total_operations, cons_check, prod_check uint64 = 0, 0, 0 |
---|
| 15 | var m sync.Mutex |
---|
| 16 | |
---|
| 17 | var prodJoin chan int = make(chan int, ProdsPerChan * Channels + 1) |
---|
| 18 | var consJoin chan int = make(chan int, ConsPerChan * Channels + 1) |
---|
| 19 | |
---|
| 20 | func consumer( channel chan uint64 ) { |
---|
| 21 | var count uint64 = 0 |
---|
| 22 | var checksum uint64 = 0 |
---|
| 23 | for { |
---|
| 24 | if cons_done { break } |
---|
| 25 | j := <- channel |
---|
| 26 | checksum = checksum ^ j |
---|
| 27 | if ! prod_done { count++ } |
---|
| 28 | } |
---|
| 29 | m.Lock() |
---|
| 30 | total_operations += count |
---|
| 31 | cons_check = cons_check ^ checksum |
---|
| 32 | // fmt.Print("C: ",count) |
---|
| 33 | m.Unlock() |
---|
| 34 | consJoin <- 0 |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | func producer( channel chan uint64 ) { |
---|
| 38 | var count uint64 = 0 |
---|
| 39 | var checksum uint64 = 0 |
---|
| 40 | for { |
---|
| 41 | if prod_done { break } |
---|
| 42 | checksum = checksum ^ count |
---|
| 43 | channel <- count |
---|
| 44 | count++ |
---|
| 45 | } |
---|
| 46 | m.Lock() |
---|
| 47 | total_operations += count |
---|
| 48 | prod_check = prod_check ^ checksum |
---|
| 49 | // fmt.Print("P: ",count, " ") |
---|
| 50 | m.Unlock() |
---|
| 51 | prodJoin <- 0 |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | func usage() { |
---|
| 55 | fmt.Printf( "Usage: %v " + |
---|
| 56 | "[ processors (> 0) | 'd' (default %v) ] " + |
---|
| 57 | "[ ChannelSize (> 0) | 'd' (default %v) ]\n", |
---|
| 58 | os.Args[0], Processors, ChannelSize ); |
---|
| 59 | os.Exit( 1 ); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | func main() { |
---|
| 63 | switch len( os.Args ) { |
---|
| 64 | case 3: |
---|
| 65 | if os.Args[2] != "d" { // default ? |
---|
[e2f827f] | 66 | ChannelSize, _ = strconv.Atoi( os.Args[2] ) |
---|
[2f6a9391] | 67 | if ChannelSize < 0 { usage(); } |
---|
| 68 | } // if |
---|
| 69 | fallthrough |
---|
| 70 | case 2: |
---|
| 71 | if os.Args[1] != "d" { // default ? |
---|
| 72 | Processors, _ = strconv.Atoi( os.Args[1] ) |
---|
| 73 | if Processors < 1 { usage(); } |
---|
| 74 | } // if |
---|
| 75 | case 1: // use defaults |
---|
| 76 | default: |
---|
| 77 | usage(); |
---|
| 78 | } // switch |
---|
| 79 | runtime.GOMAXPROCS( Processors ); |
---|
[e2f827f] | 80 | ProdsPerChan = Processors /2 |
---|
| 81 | ConsPerChan = Processors / 2 |
---|
[2f6a9391] | 82 | |
---|
| 83 | // fmt.Println("Processors: ",Processors," Channels: ",Channels," ProdsPerChan: ",ProdsPerChan," ConsPerChan: ",ConsPerChan," Channel Size: ",ChannelSize) |
---|
| 84 | |
---|
| 85 | chans := make( [] chan uint64, Channels ) |
---|
| 86 | for i := range chans { |
---|
| 87 | chans[i] = make(chan uint64, ChannelSize) |
---|
| 88 | } |
---|
| 89 | for i := range chans { |
---|
| 90 | for j := 0; j < ProdsPerChan; j++ { |
---|
| 91 | go producer( chans[i] ) |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | for j := 0; j < ConsPerChan; j++ { |
---|
| 95 | go consumer( chans[i] ) |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | // wait 10 seconds |
---|
| 101 | time.Sleep(time.Second * 10) |
---|
| 102 | // fmt.Println("prod done\n") |
---|
| 103 | prod_done = true |
---|
| 104 | for j := 0; j < ProdsPerChan * Channels ; j++ { |
---|
| 105 | <-prodJoin |
---|
| 106 | } |
---|
| 107 | // fmt.Println("cons done\n") |
---|
| 108 | cons_done = true |
---|
| 109 | for i := range chans { |
---|
[e2f827f] | 110 | L: for { |
---|
[2f6a9391] | 111 | select { |
---|
[e2f827f] | 112 | case k := <-chans[i]: |
---|
| 113 | cons_check = cons_check ^ k |
---|
[2f6a9391] | 114 | default: |
---|
[e2f827f] | 115 | break L |
---|
[2f6a9391] | 116 | } |
---|
| 117 | } |
---|
| 118 | } |
---|
[e2f827f] | 119 | for i := range chans { |
---|
| 120 | close(chans[i]) |
---|
| 121 | } |
---|
| 122 | |
---|
[2f6a9391] | 123 | for j := 0; j < ConsPerChan * Channels; j++{ |
---|
| 124 | <-consJoin |
---|
| 125 | } |
---|
| 126 | |
---|
[e2f827f] | 127 | |
---|
[2f6a9391] | 128 | if cons_check != prod_check { |
---|
| 129 | fmt.Println("\nChecksum mismatch: Cons: %d, Prods: %d", cons_check, prod_check) |
---|
| 130 | } |
---|
| 131 | fmt.Println(total_operations) |
---|
| 132 | } |
---|