| 1 | package main
|
|---|
| 2 |
|
|---|
| 3 | import (
|
|---|
| 4 | "context"
|
|---|
| 5 | "flag"
|
|---|
| 6 | "fmt"
|
|---|
| 7 | "math/rand"
|
|---|
| 8 | "os"
|
|---|
| 9 | "sync/atomic"
|
|---|
| 10 | "time"
|
|---|
| 11 | "unsafe"
|
|---|
| 12 | "golang.org/x/sync/semaphore"
|
|---|
| 13 | "golang.org/x/text/language"
|
|---|
| 14 | "golang.org/x/text/message"
|
|---|
| 15 | )
|
|---|
| 16 |
|
|---|
| 17 | type GoCtx struct {
|
|---|
| 18 | s * semaphore.Weighted
|
|---|
| 19 | d * [] uint64
|
|---|
| 20 | c context.Context
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | type Spot struct {
|
|---|
| 24 | ptr uintptr
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | // Main handshake of the code
|
|---|
| 28 | // Single seat, first thread arriving waits
|
|---|
| 29 | // Next threads unblocks current one and blocks in its place
|
|---|
| 30 | // if share == true, exchange data in the process
|
|---|
| 31 | func (this * Spot) put( ctx * GoCtx, data * [] uint64, share bool) (* [] uint64) {
|
|---|
| 32 | new := uintptr(unsafe.Pointer(ctx))
|
|---|
| 33 | // fmt.Printf("Enter with %p\n", data)
|
|---|
| 34 | var raw uintptr
|
|---|
| 35 | for true {
|
|---|
| 36 | raw = this.ptr
|
|---|
| 37 | if raw == uintptr(1) {
|
|---|
| 38 | return nil
|
|---|
| 39 | }
|
|---|
| 40 | if atomic.CompareAndSwapUintptr(&this.ptr, raw, new) {
|
|---|
| 41 | break
|
|---|
| 42 | }
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | if raw != uintptr(0) {
|
|---|
| 46 | var val *GoCtx
|
|---|
| 47 | val = (*GoCtx)(unsafe.Pointer(raw))
|
|---|
| 48 | if share {
|
|---|
| 49 | val.d = data
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | val.s.Release(1)
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | ctx.s.Acquire(ctx.c, 1)
|
|---|
| 56 | // fmt.Printf("Leave with %p (was %p)\n", ctx.d, data)
|
|---|
| 57 | return ctx.d
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | func (this * Spot) release() {
|
|---|
| 61 | val := (*GoCtx)(unsafe.Pointer(atomic.SwapUintptr(&this.ptr, uintptr(1))))
|
|---|
| 62 | if val == nil {
|
|---|
| 63 | return
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | val.s.Release(1)
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | func __xorshift64( state * uint64 ) (uint64) {
|
|---|
| 70 | x := *state
|
|---|
| 71 | x ^= x << 13
|
|---|
| 72 | x ^= x >> 7
|
|---|
| 73 | x ^= x << 17
|
|---|
| 74 | *state = x
|
|---|
| 75 | return x
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | func local(result chan uint64, start chan struct{}, size uint64, cnt uint64, channels [] Spot, share bool) {
|
|---|
| 79 | state := rand.Uint64()
|
|---|
| 80 | var my_data [] uint64
|
|---|
| 81 | my_data = make([]uint64, size)
|
|---|
| 82 | for i := uint64(0); i < size; i++ {
|
|---|
| 83 | my_data[i] = 0
|
|---|
| 84 | }
|
|---|
| 85 | data := &my_data
|
|---|
| 86 |
|
|---|
| 87 | sem := semaphore.NewWeighted(1)
|
|---|
| 88 | sem.Acquire(context.Background(), 1)
|
|---|
| 89 | ctx := GoCtx{sem, data, context.Background()}
|
|---|
| 90 |
|
|---|
| 91 | count := uint64(0)
|
|---|
| 92 | <- start
|
|---|
| 93 | for true {
|
|---|
| 94 | for i := uint64(0); i < cnt; i++ {
|
|---|
| 95 | (*data)[__xorshift64(&state) % size] += 1
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | i := __xorshift64(&state) % uint64(len(channels))
|
|---|
| 99 | // data = channels[i].put(sem, data, share)
|
|---|
| 100 | data = channels[i].put(&ctx, data, share)
|
|---|
| 101 | count += 1
|
|---|
| 102 |
|
|---|
| 103 | if clock_mode && atomic.LoadInt32(&stop) == 1 { break }
|
|---|
| 104 | if !clock_mode && count >= stop_count { break }
|
|---|
| 105 | if uint64(len(*data)) != size {
|
|---|
| 106 | panic("Data has weird size")
|
|---|
| 107 | }
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | atomic.AddInt64(&threads_left, -1);
|
|---|
| 111 | result <- count
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | func main() {
|
|---|
| 115 | work_sizeOpt := flag.Uint64("w", 2 , "Number of words (uint64) per threads")
|
|---|
| 116 | countOpt := flag.Uint64("c", 2 , "Number of words (uint64) to touch")
|
|---|
| 117 | shareOpt := flag.Bool ("s", false, "Pass the work data to the next thread when blocking")
|
|---|
| 118 |
|
|---|
| 119 | defer bench_init()()
|
|---|
| 120 |
|
|---|
| 121 | size := *work_sizeOpt
|
|---|
| 122 | cnt := *countOpt
|
|---|
| 123 | share := *shareOpt
|
|---|
| 124 |
|
|---|
| 125 | if ! (nthreads > nprocs) {
|
|---|
| 126 | fmt.Fprintf(os.Stderr, "Must have more threads than procs\n")
|
|---|
| 127 | os.Exit(1)
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | barrierStart := make(chan struct{})
|
|---|
| 131 | threads_left = int64(nthreads)
|
|---|
| 132 | result := make(chan uint64)
|
|---|
| 133 | channels := make([]Spot, nthreads - nprocs)
|
|---|
| 134 | for i := range channels {
|
|---|
| 135 | channels[i] = Spot{uintptr(0)}
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | for i := 0; i < nthreads; i++ {
|
|---|
| 139 | go local(result, barrierStart, size, cnt, channels, share)
|
|---|
| 140 | }
|
|---|
| 141 | fmt.Printf("Starting\n");
|
|---|
| 142 |
|
|---|
| 143 | atomic.StoreInt32(&stop, 0)
|
|---|
| 144 | start := time.Now()
|
|---|
| 145 | close(barrierStart)
|
|---|
| 146 |
|
|---|
| 147 | wait(start, true);
|
|---|
| 148 |
|
|---|
| 149 | atomic.StoreInt32(&stop, 1)
|
|---|
| 150 | end := time.Now()
|
|---|
| 151 | delta := end.Sub(start)
|
|---|
| 152 |
|
|---|
| 153 | fmt.Printf("\nDone\n")
|
|---|
| 154 |
|
|---|
| 155 | for i := range channels {
|
|---|
| 156 | channels[i].release()
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | global_counter := uint64(0)
|
|---|
| 160 | for i := 0; i < nthreads; i++ {
|
|---|
| 161 | r := <- result
|
|---|
| 162 | global_counter += r
|
|---|
| 163 | fmt.Printf("%d\n", r)
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | p := message.NewPrinter(language.English)
|
|---|
| 167 | p.Printf("Duration (ms) : %f\n", delta.Seconds());
|
|---|
| 168 | p.Printf("Number of processors : %d\n", nprocs);
|
|---|
| 169 | p.Printf("Number of threads : %d\n", nthreads);
|
|---|
| 170 | p.Printf("Work size (64bit words): %d\n", size);
|
|---|
| 171 | p.Printf("Total Operations(ops) : %15d\n", global_counter)
|
|---|
| 172 | p.Printf("Ops per second : %18.2f\n", float64(global_counter) / delta.Seconds())
|
|---|
| 173 | p.Printf("ns per ops : %18.2f\n", float64(delta.Nanoseconds()) / float64(global_counter))
|
|---|
| 174 | p.Printf("Ops per threads : %15d\n", global_counter / uint64(nthreads))
|
|---|
| 175 | p.Printf("Ops per procs : %15d\n", global_counter / uint64(nprocs))
|
|---|
| 176 | p.Printf("Ops/sec/procs : %18.2f\n", (float64(global_counter) / float64(nprocs)) / delta.Seconds())
|
|---|
| 177 | p.Printf("ns per ops/procs : %18.2f\n", float64(delta.Nanoseconds()) / (float64(global_counter) / float64(nprocs)))
|
|---|
| 178 | }
|
|---|