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