source: doc/theses/colby_parsons_MMAth/benchmarks/channels/go/hot_potato/hot_potato.go @ d24b1985

ADTast-experimental
Last change on this file since d24b1985 was e2f827f, checked in by caparson <caparson@…>, 16 months ago

added go counterpart to cfa benchmarks

  • Property mode set to 100644
File size: 1.2 KB
RevLine 
[e2f827f]1package main
2
3import (
4        "fmt"
5        "time"
6        "runtime"
7        "sync"
8        "os"
9        "strconv"
10)
11
12var done bool = false;
13var total_operations uint64 = 0
14var Processors int = 1
15var m sync.Mutex
16
17var taskJoin chan int = make(chan int, 2)
18
19func Task( chans [] chan uint64, id int ) {
20        var count uint64 = 0
21        right := (id + 1) % Processors
22        for {
23                if done { break }
24                <-chans[id]
25                chans[right]<-0
26                count++;
27        }
28        m.Lock()
29        total_operations += count
30        m.Unlock()
31        taskJoin <- 0
32}
33
34func main() {
35        switch len( os.Args ) {
36                case 2:
37                        if os.Args[1] != "d" {                                                  // default ?
38                                Processors, _ = strconv.Atoi( os.Args[1] )
39                        } // if
40                case 1:                                                                                 // use defaults
41                default:
42                        fmt.Printf( "Invalid args" );
43                        os.Exit( 1 );
44        } // switch
45        runtime.GOMAXPROCS( Processors );
46
47        taskJoin = make(chan int, Processors)
48        chans := make( [] chan uint64, Processors )
49        for i := range chans {
50                chans[i] = make(chan uint64, 3)
51        }
52
53        chans[0] <- 0
54
55        for i := 0; i < Processors; i++ {
56                go Task( chans, i )
57        }
58               
59        // wait 10 seconds
60        time.Sleep(time.Second * 10)
61        // fmt.Println("prod done\n")
62        done = true
63
64        for i := 0; i < Processors; i++ {
65                chans[i] <- 0
66        }
67        for i := 0; i < Processors; i++ {
68                <-taskJoin
69        }
70
71    fmt.Println(total_operations)
72}
Note: See TracBrowser for help on using the repository browser.