source: doc/theses/colby_parsons_MMAth/benchmarks/channels/go/churn/churn.go @ 1571e4d

Last change on this file since 1571e4d was e2f827f, checked in by caparson <caparson@…>, 16 months ago

added go counterpart to cfa benchmarks

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