source: doc/theses/colby_parsons_MMAth/benchmarks/waituntil/go/contend4/contend.go @ c44705c

Last change on this file since c44705c 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.3 KB
RevLine 
[382467f]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, 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                }
31                if ! prod_done { count++ }
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                        case chans[2] <- count:
47                        case chans[3] <- count:
48                }
49                count++
50        }
51        prodJoin <- 0
52}
53
54func usage() {
55        fmt.Printf( "Usage: %v " +
56                "[ processors (> 0) | 'd' (default %v) ] " +
57                "[ ChannelSize (> 0) | 'd' (default %v) ]\n",
58                os.Args[0], Processors, ChannelSize );
59        os.Exit( 1 );
60}
61
62func main() {
63        switch len( os.Args ) {
64                case 3:
65                        if os.Args[2] != "d" {                                                  // default ?
66                                ChannelSize, _ = strconv.Atoi( os.Args[2] )
67                                        if ChannelSize < 0 { usage(); }
68                        } // if
69                fallthrough
70                case 2:
71                        if os.Args[1] != "d" {                                                  // default ?
72                                Processors, _ = strconv.Atoi( os.Args[1] )
73                                if Processors < 1 { usage(); }
74                        } // if
75                case 1:                                                                                 // use defaults
76                default:
77                usage();
78        } // switch
79        runtime.GOMAXPROCS( Processors );
80        Prods = Processors /2
81        Cons = Processors / 2
82
83        // fmt.Println("Processors: ",Processors," Channels: ",Channels," ProdsPerChan: ",ProdsPerChan," ConsPerChan: ",ConsPerChan," Channel Size: ",ChannelSize)
84       
85        chans := make( [] chan uint64, Channels )
86        for i := range chans {
87                chans[i] = make(chan uint64, ChannelSize)
88        }
89        for j := 0; j < Prods; j++ {
90                go producer( chans )
91        }
92
93        for j := 0; j < Cons; j++ {
94                go consumer( chans )
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.