source: doc/theses/colby_parsons_MMAth/benchmarks/waituntil/go/spin2/spin.go @ c141c09

Last change on this file since c141c09 was 382467f, checked in by caparsons <caparson@…>, 15 months ago

added go and cfa waituntil channel benchmarks

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