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