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