source: doc/theses/colby_parsons_MMAth/benchmarks/channels/go/barrier/barrier.go@ bb7422a

ADT ast-experimental
Last change on this file since bb7422a was e2f827f, checked in by caparson <caparson@…>, 2 years ago

added go counterpart to cfa benchmarks

  • Property mode set to 100644
File size: 2.3 KB
Line 
1package main
2
3import (
4 "fmt"
5 "sync"
6 "time"
7 "runtime"
8 "os"
9 "strconv"
10)
11
12var Processors, Tasks, BarrierSize int = 1, 1, 2
13var done bool = false;
14var total_operations uint64 = 0
15var m sync.Mutex
16
17var taskJoin chan int = make(chan int, Tasks + 1)
18
19var barWait chan int = make(chan int, 2 * BarrierSize)
20var entryWait chan int = make(chan int, 2 * BarrierSize)
21
22func initBarrier() {
23 for j := 0; j < BarrierSize; j++ {
24 entryWait <- j
25 }
26}
27
28func 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
54func 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
68func 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
76func 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}
Note: See TracBrowser for help on using the repository browser.