| 1 | package main
|
|---|
| 2 |
|
|---|
| 3 | import (
|
|---|
| 4 | "flag"
|
|---|
| 5 | "fmt"
|
|---|
| 6 | "math/rand"
|
|---|
| 7 | "os"
|
|---|
| 8 | "sync/atomic"
|
|---|
| 9 | "time"
|
|---|
| 10 | "golang.org/x/text/language"
|
|---|
| 11 | "golang.org/x/text/message"
|
|---|
| 12 | )
|
|---|
| 13 |
|
|---|
| 14 | func handshake(stop chan struct {}, c chan [] uint64, data [] uint64, share bool) (bool, [] uint64) {
|
|---|
| 15 | var s [] uint64 = data
|
|---|
| 16 | if !share {
|
|---|
| 17 | s = nil
|
|---|
| 18 | }
|
|---|
| 19 |
|
|---|
| 20 | // send the data
|
|---|
| 21 | select {
|
|---|
| 22 | case <- stop:
|
|---|
| 23 | return true, nil
|
|---|
| 24 | case c <- s:
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | // get the new data chunk
|
|---|
| 28 | select {
|
|---|
| 29 | case <- stop:
|
|---|
| 30 | return true, nil
|
|---|
| 31 | case n := <- c:
|
|---|
| 32 | if share {
|
|---|
| 33 | return false, n
|
|---|
| 34 | }
|
|---|
| 35 | return false, data
|
|---|
| 36 | }
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | func local(result chan uint64, start chan struct{}, stop chan struct{}, size uint64, cnt uint64, channels []chan [] uint64, chan_cnt uint64, share bool) {
|
|---|
| 40 | var data [] uint64
|
|---|
| 41 | data = make([]uint64, size)
|
|---|
| 42 | for i := uint64(0); i < size; i++ {
|
|---|
| 43 | data[i] = 0
|
|---|
| 44 | }
|
|---|
| 45 | count := uint64(0)
|
|---|
| 46 | <- start
|
|---|
| 47 | for true {
|
|---|
| 48 | for i := uint64(0); i < cnt; i++ {
|
|---|
| 49 | data[rand.Uint64() % size] += 1
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | i := rand.Uint64() % chan_cnt
|
|---|
| 53 | var closed bool
|
|---|
| 54 | closed, data = handshake(stop, channels[i], data, share)
|
|---|
| 55 | count += 1
|
|---|
| 56 |
|
|---|
| 57 | if closed { break }
|
|---|
| 58 | if !clock_mode && count >= stop_count { break }
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | atomic.AddInt64(&threads_left, -1);
|
|---|
| 62 | result <- count
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | func main() {
|
|---|
| 66 |
|
|---|
| 67 | work_sizeOpt := flag.Uint64("w", 2 , "Number of words (uint64) per threads")
|
|---|
| 68 | countOpt := flag.Uint64("c", 2 , "Number of words (uint64) to touch")
|
|---|
| 69 | shareOpt := flag.Bool ("s", false, "Pass the work data to the next thread when blocking")
|
|---|
| 70 |
|
|---|
| 71 | bench_init()
|
|---|
| 72 |
|
|---|
| 73 | size := *work_sizeOpt
|
|---|
| 74 | cnt := *countOpt
|
|---|
| 75 | share := *shareOpt
|
|---|
| 76 |
|
|---|
| 77 | if ! (nthreads > nprocs) {
|
|---|
| 78 | fmt.Fprintf(os.Stderr, "Must have more threads than procs\n")
|
|---|
| 79 | os.Exit(1)
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | barrierStart := make(chan struct{})
|
|---|
| 83 | barrierStop := make(chan struct{})
|
|---|
| 84 | threads_left = int64(nthreads)
|
|---|
| 85 | result := make(chan uint64)
|
|---|
| 86 | channels := make([]chan [] uint64, nthreads - nprocs)
|
|---|
| 87 | for i := range channels {
|
|---|
| 88 | channels[i] = make(chan [] uint64, 1)
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | for i := 0; i < nthreads; i++ {
|
|---|
| 92 | go local(result, barrierStart, barrierStop, size, cnt, channels, uint64(nthreads - nprocs), share)
|
|---|
| 93 | }
|
|---|
| 94 | fmt.Printf("Starting\n");
|
|---|
| 95 |
|
|---|
| 96 | start := time.Now()
|
|---|
| 97 | close(barrierStart)
|
|---|
| 98 |
|
|---|
| 99 | wait(start, true);
|
|---|
| 100 |
|
|---|
| 101 | close(barrierStop)
|
|---|
| 102 | end := time.Now()
|
|---|
| 103 | delta := end.Sub(start)
|
|---|
| 104 |
|
|---|
| 105 | fmt.Printf("\nDone\n")
|
|---|
| 106 |
|
|---|
| 107 | global_counter := uint64(0)
|
|---|
| 108 | for i := 0; i < nthreads; i++ {
|
|---|
| 109 | global_counter += <- result
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | p := message.NewPrinter(language.English)
|
|---|
| 113 | p.Printf("Duration (ms) : %f\n", delta.Seconds());
|
|---|
| 114 | p.Printf("Number of processors : %d\n", nprocs);
|
|---|
| 115 | p.Printf("Number of threads : %d\n", nthreads);
|
|---|
| 116 | p.Printf("Work size (64bit words): %d\n", size);
|
|---|
| 117 | p.Printf("Total Operations(ops) : %15d\n", global_counter)
|
|---|
| 118 | p.Printf("Ops per second : %18.2f\n", float64(global_counter) / delta.Seconds())
|
|---|
| 119 | p.Printf("ns per ops : %18.2f\n", float64(delta.Nanoseconds()) / float64(global_counter))
|
|---|
| 120 | p.Printf("Ops per threads : %15d\n", global_counter / uint64(nthreads))
|
|---|
| 121 | p.Printf("Ops per procs : %15d\n", global_counter / uint64(nprocs))
|
|---|
| 122 | p.Printf("Ops/sec/procs : %18.2f\n", (float64(global_counter) / float64(nprocs)) / delta.Seconds())
|
|---|
| 123 | p.Printf("ns per ops/procs : %18.2f\n", float64(delta.Nanoseconds()) / (float64(global_counter) / float64(nprocs)))
|
|---|
| 124 | }
|
|---|