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