| 1 | package main
 | 
|---|
| 2 | 
 | 
|---|
| 3 | import (
 | 
|---|
| 4 |         "fmt"
 | 
|---|
| 5 |         "runtime"
 | 
|---|
| 6 |         "sync"
 | 
|---|
| 7 |         "sync/atomic"
 | 
|---|
| 8 |         "time"
 | 
|---|
| 9 |         "golang.org/x/text/language"
 | 
|---|
| 10 |         "golang.org/x/text/message"
 | 
|---|
| 11 | )
 | 
|---|
| 12 | 
 | 
|---|
| 13 | func yielder(result chan uint64, start *sync.WaitGroup) {
 | 
|---|
| 14 |         count := uint64(0)
 | 
|---|
| 15 |         start.Wait()
 | 
|---|
| 16 |         for true {
 | 
|---|
| 17 |                 runtime.Gosched()
 | 
|---|
| 18 |                 count += 1
 | 
|---|
| 19 |                 if  clock_mode && atomic.LoadInt32(&stop) == 1 { break }
 | 
|---|
| 20 |                 if !clock_mode && count >= stop_count { break }
 | 
|---|
| 21 |         }
 | 
|---|
| 22 | 
 | 
|---|
| 23 |         atomic.AddInt64(&threads_left, -1);
 | 
|---|
| 24 |         result <- count
 | 
|---|
| 25 | }
 | 
|---|
| 26 | 
 | 
|---|
| 27 | func main() {
 | 
|---|
| 28 |         bench_init()
 | 
|---|
| 29 | 
 | 
|---|
| 30 |         threads_left = int64(nthreads)
 | 
|---|
| 31 | 
 | 
|---|
| 32 |         result := make(chan uint64)
 | 
|---|
| 33 |         var wg sync.WaitGroup
 | 
|---|
| 34 |         wg.Add(1)
 | 
|---|
| 35 | 
 | 
|---|
| 36 |         for i := 0; i < nthreads; i++ {
 | 
|---|
| 37 |                 go yielder(result, &wg)
 | 
|---|
| 38 |         }
 | 
|---|
| 39 |         fmt.Printf("Starting\n");
 | 
|---|
| 40 |         atomic.StoreInt32(&stop, 0)
 | 
|---|
| 41 |         start := time.Now()
 | 
|---|
| 42 |         wg.Done();
 | 
|---|
| 43 |         wait(start, true);
 | 
|---|
| 44 | 
 | 
|---|
| 45 |         atomic.StoreInt32(&stop, 1)
 | 
|---|
| 46 |         end := time.Now()
 | 
|---|
| 47 |         duration := end.Sub(start)
 | 
|---|
| 48 | 
 | 
|---|
| 49 |         fmt.Printf("\nDone\n")
 | 
|---|
| 50 | 
 | 
|---|
| 51 |         global_counter := uint64(0)
 | 
|---|
| 52 |         for i := 0; i < nthreads; i++ {
 | 
|---|
| 53 |                 global_counter += <- result
 | 
|---|
| 54 |         }
 | 
|---|
| 55 | 
 | 
|---|
| 56 |         p := message.NewPrinter(language.English)
 | 
|---|
| 57 |         p.Printf("Duration (ms)        : %d\n", duration.Milliseconds())
 | 
|---|
| 58 |         p.Printf("Number of processors : %d\n", nprocs)
 | 
|---|
| 59 |         p.Printf("Number of threads    : %d\n", nthreads)
 | 
|---|
| 60 |         p.Printf("Total Operations(ops): %15d\n", global_counter)
 | 
|---|
| 61 |         p.Printf("Ops per second       : %18.2f\n", float64(global_counter) / duration.Seconds())
 | 
|---|
| 62 |         p.Printf("ns per ops           : %18.2f\n", float64(duration.Nanoseconds()) / float64(global_counter))
 | 
|---|
| 63 |         p.Printf("Ops per threads      : %15d\n", global_counter / uint64(nthreads))
 | 
|---|
| 64 |         p.Printf("Ops per procs        : %15d\n", global_counter / uint64(nprocs))
 | 
|---|
| 65 |         p.Printf("Ops/sec/procs        : %18.2f\n", (float64(global_counter) / float64(nprocs)) / duration.Seconds())
 | 
|---|
| 66 |         p.Printf("ns per ops/procs     : %18.2f\n", float64(duration.Nanoseconds()) / (float64(global_counter) / float64(nprocs)))
 | 
|---|
| 67 | }
 | 
|---|