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