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