source: doc/theses/colby_parsons_MMAth/benchmarks/waituntil/go/contend/contend.go

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