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