source: doc/theses/colby_parsons_MMAth/benchmarks/waituntil/go/spin/spin.go @ 2cb8bf71

ast-experimental
Last change on this file since 2cb8bf71 was 382467f, checked in by caparsons <caparson@…>, 14 months ago

added go and cfa waituntil channel benchmarks

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