source: doc/theses/colby_parsons_MMAth/benchmarks/waituntil/go/spin4/spin.go @ 382467f

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

added go and cfa waituntil channel 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, Channels, Prods, Cons, ChannelSize int = 2, 4, 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                        default:
34                }
35        }
36        m.Lock()
37        total_operations += count
38        m.Unlock()
39        consJoin <- 0
40}
41
42func producer( chans [] chan uint64 ) {
43        var count uint64 = 0
44        for {
45                if prod_done { break }
46                select {
47                        case chans[0] <- count:
48                        case chans[1] <- count:
49                        case chans[2] <- count:
50                        case chans[3] <- count:
51                        default:
52                }
53                count++
54        }
55        prodJoin <- 0
56}
57
58func usage() {
59        fmt.Printf( "Usage: %v " +
60                "[ processors (> 0) | 'd' (default %v) ] " +
61                "[ ChannelSize (> 0) | 'd' (default %v) ]\n",
62                os.Args[0], Processors, ChannelSize );
63        os.Exit( 1 );
64}
65
66func main() {
67        switch len( os.Args ) {
68                case 3:
69                        if os.Args[2] != "d" {                                                  // default ?
70                                ChannelSize, _ = strconv.Atoi( os.Args[2] )
71                                        if ChannelSize < 0 { usage(); }
72                        } // if
73                fallthrough
74                case 2:
75                        if os.Args[1] != "d" {                                                  // default ?
76                                Processors, _ = strconv.Atoi( os.Args[1] )
77                                if Processors < 1 { usage(); }
78                        } // if
79                case 1:                                                                                 // use defaults
80                default:
81                usage();
82        } // switch
83        runtime.GOMAXPROCS( Processors );
84        Prods = Processors /2
85        Cons = Processors / 2
86
87        // fmt.Println("Processors: ",Processors," Channels: ",Channels," ProdsPerChan: ",ProdsPerChan," ConsPerChan: ",ConsPerChan," Channel Size: ",ChannelSize)
88       
89        chans := make( [] chan uint64, Channels )
90        for i := range chans {
91                chans[i] = make(chan uint64, ChannelSize)
92        }
93        for j := 0; j < Prods; j++ {
94                go producer( chans )
95        }
96
97        for j := 0; j < Cons; j++ {
98                go consumer( chans )
99        }
100               
101
102        // wait 10 seconds
103        time.Sleep(time.Second * 10)
104        // fmt.Println("prod done\n")
105        prod_done = true
106        for j := 0; j < Prods; j++ {
107                <-prodJoin
108        }
109        // fmt.Println("cons done\n")
110        cons_done = true
111        for i := range chans {
112                close(chans[i])
113        }
114       
115        for j := 0; j < Cons; j++{
116                <-consJoin
117        }
118
119    fmt.Println(total_operations)
120}
Note: See TracBrowser for help on using the repository browser.