| [0b84b15] | 1 | package main
 | 
|---|
 | 2 | 
 | 
|---|
 | 3 | import (
 | 
|---|
 | 4 |         "flag"
 | 
|---|
 | 5 |         "fmt"
 | 
|---|
 | 6 |         "sync/atomic"
 | 
|---|
 | 7 |         "time"
 | 
|---|
 | 8 |         "golang.org/x/text/language"
 | 
|---|
 | 9 |         "golang.org/x/text/message"
 | 
|---|
 | 10 | )
 | 
|---|
 | 11 | 
 | 
|---|
 | 12 | func partner(result chan uint64, mine chan int, next chan int) {
 | 
|---|
 | 13 |         count := uint64(0)
 | 
|---|
 | 14 |         for true {
 | 
|---|
 | 15 |                 <- mine
 | 
|---|
| [7f8fbe3] | 16 |                 select {
 | 
|---|
 | 17 |                 case next <- 0:
 | 
|---|
 | 18 |                 default:
 | 
|---|
 | 19 |                 }
 | 
|---|
| [0b84b15] | 20 |                 count += 1
 | 
|---|
 | 21 |                 if  clock_mode && atomic.LoadInt32(&stop) == 1 { break }
 | 
|---|
 | 22 |                 if !clock_mode && count >= stop_count { break }
 | 
|---|
 | 23 |         }
 | 
|---|
 | 24 | 
 | 
|---|
 | 25 |         atomic.AddInt64(&threads_left, -1);
 | 
|---|
 | 26 |         result <- count
 | 
|---|
 | 27 | }
 | 
|---|
 | 28 | 
 | 
|---|
 | 29 | func main() {
 | 
|---|
 | 30 |         var ring_size int
 | 
|---|
 | 31 | 
 | 
|---|
 | 32 |         ring_sizeOpt := flag.Int("r", 2, "The number of threads per cycles")
 | 
|---|
 | 33 | 
 | 
|---|
| [2c7eee0] | 34 |         bench_init()
 | 
|---|
| [0b84b15] | 35 | 
 | 
|---|
 | 36 |         ring_size = *ring_sizeOpt
 | 
|---|
 | 37 | 
 | 
|---|
 | 38 |         tthreads := nthreads * ring_size
 | 
|---|
 | 39 |         threads_left = int64(tthreads)
 | 
|---|
 | 40 | 
 | 
|---|
 | 41 |         result := make(chan uint64)
 | 
|---|
 | 42 |         channels := make([]chan int, tthreads)
 | 
|---|
 | 43 |         for i := range channels {
 | 
|---|
 | 44 |                 channels[i] = make(chan int, 1)
 | 
|---|
 | 45 |         }
 | 
|---|
 | 46 | 
 | 
|---|
 | 47 |         for i := 0; i < tthreads; i++ {
 | 
|---|
 | 48 |                 pi := (i + nthreads) % tthreads
 | 
|---|
 | 49 |                 go partner(result, channels[i], channels[pi])
 | 
|---|
 | 50 |         }
 | 
|---|
 | 51 |         fmt.Printf("Starting\n");
 | 
|---|
 | 52 | 
 | 
|---|
 | 53 |         atomic.StoreInt32(&stop, 0)
 | 
|---|
 | 54 |         start := time.Now()
 | 
|---|
 | 55 |         for i := 0; i < nthreads; i++ {
 | 
|---|
 | 56 |                 channels[i] <- 0
 | 
|---|
 | 57 |         }
 | 
|---|
 | 58 |         wait(start, true);
 | 
|---|
 | 59 | 
 | 
|---|
 | 60 |         atomic.StoreInt32(&stop, 1)
 | 
|---|
 | 61 |         end := time.Now()
 | 
|---|
| [d0b9247] | 62 |         duration := end.Sub(start)
 | 
|---|
| [0b84b15] | 63 | 
 | 
|---|
 | 64 |         fmt.Printf("\nDone\n")
 | 
|---|
 | 65 | 
 | 
|---|
 | 66 |         global_counter := uint64(0)
 | 
|---|
 | 67 |         for i := 0; i < tthreads; i++ {
 | 
|---|
| [69d1748] | 68 |                 select {
 | 
|---|
 | 69 |                 case channels[i] <- 0:
 | 
|---|
 | 70 |                 default:
 | 
|---|
 | 71 |                 }
 | 
|---|
| [0b84b15] | 72 |                 global_counter += <- result
 | 
|---|
 | 73 |         }
 | 
|---|
 | 74 | 
 | 
|---|
 | 75 |         p := message.NewPrinter(language.English)
 | 
|---|
| [d0b9247] | 76 |         p.Printf("Duration (ms)        : %d\n", duration.Milliseconds())
 | 
|---|
| [2c7eee0] | 77 |         p.Printf("Number of processors : %d\n", nprocs);
 | 
|---|
 | 78 |         p.Printf("Number of threads    : %d\n", tthreads);
 | 
|---|
 | 79 |         p.Printf("Cycle size (# thrds) : %d\n", ring_size);
 | 
|---|
 | 80 |         p.Printf("Total Operations(ops): %15d\n", global_counter)
 | 
|---|
| [d0b9247] | 81 |         p.Printf("Ops per second       : %18.2f\n", float64(global_counter) / duration.Seconds())
 | 
|---|
 | 82 |         p.Printf("ns per ops           : %18.2f\n", float64(duration.Nanoseconds()) / float64(global_counter))
 | 
|---|
| [2c7eee0] | 83 |         p.Printf("Ops per threads      : %15d\n", global_counter / uint64(tthreads))
 | 
|---|
 | 84 |         p.Printf("Ops per procs        : %15d\n", global_counter / uint64(nprocs))
 | 
|---|
| [d0b9247] | 85 |         p.Printf("Ops/sec/procs        : %18.2f\n", (float64(global_counter) / float64(nprocs)) / duration.Seconds())
 | 
|---|
 | 86 |         p.Printf("ns per ops/procs     : %18.2f\n", float64(duration.Nanoseconds()) / (float64(global_counter) / float64(nprocs)))
 | 
|---|
| [0b84b15] | 87 | 
 | 
|---|
| [7f8fbe3] | 88 | }
 | 
|---|