source: doc/theses/colby_parsons_MMAth/benchmarks/channels/go/contend.go@ e10714a

ADT ast-experimental
Last change on this file since e10714a was 2f6a9391, checked in by caparsons <caparson@…>, 3 years ago

added first channel bench and copied over scripts that will need to be edited for channel usage. intermediate commit so that benchmarks can be pulled and run on another server

  • Property mode set to 100644
File size: 2.9 KB
Line 
1package main
2
3import (
4 "fmt"
5 "sync"
6 "time"
7 "runtime"
8 "os"
9 "strconv"
10)
11
12var Processors, Channels, ProdsPerChan, ConsPerChan, ChannelSize int = 1, 1, 1, 1, 128
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, ProdsPerChan * Channels + 1)
18var consJoin chan int = make(chan int, ConsPerChan * Channels + 1)
19
20func consumer( channel chan uint64 ) {
21 var count uint64 = 0
22 var checksum uint64 = 0
23 for {
24 if cons_done { break }
25 j := <- channel
26 checksum = checksum ^ j
27 if ! prod_done { count++ }
28 }
29 m.Lock()
30 total_operations += count
31 cons_check = cons_check ^ checksum
32 // fmt.Print("C: ",count)
33 m.Unlock()
34 consJoin <- 0
35}
36
37func producer( channel chan uint64 ) {
38 var count uint64 = 0
39 var checksum uint64 = 0
40 for {
41 if prod_done { break }
42 checksum = checksum ^ count
43 channel <- count
44 count++
45 }
46 m.Lock()
47 total_operations += count
48 prod_check = prod_check ^ checksum
49 // fmt.Print("P: ",count, " ")
50 m.Unlock()
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[1] )
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 ProdsPerChan = Processors
81 ConsPerChan = Processors
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 i := range chans {
90 for j := 0; j < ProdsPerChan; j++ {
91 go producer( chans[i] )
92 }
93
94 for j := 0; j < ConsPerChan; j++ {
95 go consumer( chans[i] )
96 }
97 }
98
99
100 // wait 10 seconds
101 time.Sleep(time.Second * 10)
102 // fmt.Println("prod done\n")
103 prod_done = true
104 for j := 0; j < ProdsPerChan * Channels ; j++ {
105 <-prodJoin
106 }
107 // fmt.Println("cons done\n")
108 cons_done = true
109 for i := range chans {
110 J: for j := 0; j < ConsPerChan; j++ {
111 select {
112 case chans[i] <- 0:
113
114 default:
115 break J
116 }
117 }
118 }
119 for j := 0; j < ConsPerChan * Channels; j++{
120 <-consJoin
121 }
122
123 // for i := range chans {
124 // L: for {
125 // select {
126 // case k := <-chans[i]:
127 // cons_check = cons_check ^ k
128 // default:
129 // break L
130 // }
131 // }
132 // }
133 if cons_check != prod_check {
134 fmt.Println("\nChecksum mismatch: Cons: %d, Prods: %d", cons_check, prod_check)
135 }
136 for i := range chans {
137 close(chans[i])
138 }
139 fmt.Println(total_operations)
140}
Note: See TracBrowser for help on using the repository browser.