Changeset aa1d13c
- Timestamp:
- Dec 17, 2020, 3:50:18 PM (4 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- c5a98f3
- Parents:
- 94d93510
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
benchmark/readyQ/locality.go
r94d93510 raa1d13c 7 7 "math/rand" 8 8 "os" 9 "syscall" 9 10 "sync/atomic" 10 11 "time" … … 15 16 ) 16 17 17 type GoCtx struct { 18 // ================================================== 19 type MyData struct { 20 ttid int 21 id int 22 data [] uint64 23 } 24 25 func NewData(id int, size uint64) (*MyData) { 26 var data [] uint64 27 data = make([]uint64, size) 28 for i := uint64(0); i < size; i++ { 29 data[i] = 0 30 } 31 return &MyData{syscall.Gettid(), id, data} 32 } 33 34 func (this * MyData) moved( ttid int ) (uint64) { 35 if this.ttid == ttid { 36 return 0 37 } 38 this.ttid = ttid 39 return 1 40 } 41 42 func (this * MyData) access( idx uint64 ) { 43 this.data[idx % uint64(len(this.data))] += 1 44 } 45 46 // ================================================== 47 type MyCtx struct { 18 48 s * semaphore.Weighted 19 49 d unsafe.Pointer 20 50 c context.Context 51 ttid int 21 52 id int 22 53 } 23 54 55 func NewCtx( sem * semaphore.Weighted, data * MyData, id int ) (MyCtx) { 56 return MyCtx{sem, unsafe.Pointer(data), context.Background(), syscall.Gettid(), id} 57 } 58 59 func (this * MyCtx) moved( ttid int ) (uint64) { 60 if this.ttid == ttid { 61 return 0 62 } 63 this.ttid = ttid 64 return 1 65 } 66 67 // ================================================== 24 68 type Spot struct { 25 69 ptr uintptr … … 31 75 // Next threads unblocks current one and blocks in its place 32 76 // if share == true, exchange data in the process 33 func (this * Spot) put( ctx * GoCtx, data * [] uint64, share bool) (* [] uint64, bool) {77 func (this * Spot) put( ctx * MyCtx, data * MyData, share bool) (* MyData, bool) { 34 78 new := uintptr(unsafe.Pointer(ctx)) 35 79 // old_d := ctx.d … … 46 90 47 91 if raw != uintptr(0) { 48 var val * GoCtx49 val = (* GoCtx)(unsafe.Pointer(raw))92 var val *MyCtx 93 val = (*MyCtx)(unsafe.Pointer(raw)) 50 94 if share { 51 95 // fmt.Printf("[%d] - %d update %d: %p -> %p\n", this.id, ctx.id, val.id, val.d, data) … … 59 103 // fmt.Printf("[%d] - %d enter\n", this.id, ctx.id) 60 104 ctx.s.Acquire(ctx.c, 1) 61 ret := (* [] uint64)(atomic.LoadPointer(&ctx.d))105 ret := (* MyData)(atomic.LoadPointer(&ctx.d)) 62 106 // fmt.Printf("[%d] - %d leave: %p -> %p\n", this.id, ctx.id, ret, old_d) 63 107 … … 66 110 67 111 func (this * Spot) release() { 68 val := (* GoCtx)(unsafe.Pointer(atomic.SwapUintptr(&this.ptr, uintptr(1))))112 val := (*MyCtx)(unsafe.Pointer(atomic.SwapUintptr(&this.ptr, uintptr(1)))) 69 113 if val == nil { 70 114 return … … 74 118 } 75 119 120 // ================================================== 121 type Result struct { 122 count uint64 123 gmigs uint64 124 dmigs uint64 125 } 126 127 func NewResult() (Result) { 128 return Result{0, 0, 0} 129 } 130 131 // ================================================== 76 132 func __xorshift64( state * uint64 ) (uint64) { 77 133 x := *state … … 83 139 } 84 140 85 func work(data * [] uint64, size uint64, cnt uint64, state * uint64) {141 func work(data * MyData, cnt uint64, state * uint64) { 86 142 for i := uint64(0); i < cnt; i++ { 87 (*data)[__xorshift64(state) % size] += 188 } 89 } 90 91 func local(result chan uint64, start chan struct{}, size uint64, cnt uint64, channels [] Spot, share bool, id int) {143 data.access(__xorshift64(state)) 144 } 145 } 146 147 func local(result chan Result, start chan struct{}, size uint64, cnt uint64, channels [] Spot, share bool, id int) { 92 148 state := rand.Uint64() 93 var my_data [] uint64 94 my_data = make([]uint64, size) 95 for i := uint64(0); i < size; i++ { 96 my_data[i] = 0 97 } 98 data := &my_data 99 149 150 data := NewData(id, size) 100 151 sem := semaphore.NewWeighted(1) 101 152 sem.Acquire(context.Background(), 1) 102 ctx := GoCtx{sem, unsafe.Pointer(data), context.Background(), id}103 104 count := uint64(0)153 ctx := NewCtx(sem, data, id) 154 155 r := NewResult() 105 156 <- start 106 157 for true { 107 work(data, size,cnt, &state)158 work(data, cnt, &state) 108 159 109 160 i := __xorshift64(&state) % uint64(len(channels)) 110 161 var closed bool 111 162 data, closed = channels[i].put(&ctx, data, share) 112 count += 1113 163 114 164 if closed { break } 115 165 if clock_mode && atomic.LoadInt32(&stop) == 1 { break } 116 if !clock_mode && count >= stop_count { break }117 if uint64(len( *data)) != size {166 if !clock_mode && r.count >= stop_count { break } 167 if uint64(len(data.data)) != size { 118 168 panic("Data has weird size") 119 169 } 170 171 ttid := syscall.Gettid() 172 r.count += 1 173 r.gmigs += ctx .moved(ttid) 174 r.dmigs += data.moved(ttid) 120 175 } 121 176 122 177 atomic.AddInt64(&threads_left, -1); 123 result <- count178 result <- r 124 179 } 125 180 … … 142 197 barrierStart := make(chan struct{}) 143 198 threads_left = int64(nprocs) 144 result := make(chan uint64)199 result := make(chan Result) 145 200 channels := make([]Spot, nthreads - nprocs) 146 201 for i := range channels { … … 169 224 } 170 225 171 global_ counter := uint64(0)226 global_result := NewResult() 172 227 for i := 0; i < nthreads; i++ { 173 228 r := <- result 174 global_counter += r 175 fmt.Printf("%d\n", r) 229 global_result.count += r.count 230 global_result.gmigs += r.gmigs 231 global_result.dmigs += r.dmigs 176 232 } 177 233 … … 181 237 p.Printf("Number of threads : %d\n", nthreads); 182 238 p.Printf("Work size (64bit words): %d\n", size); 183 p.Printf("Total Operations(ops) : %15d\n", global_counter) 184 p.Printf("Ops per second : %18.2f\n", float64(global_counter) / delta.Seconds()) 185 p.Printf("ns per ops : %18.2f\n", float64(delta.Nanoseconds()) / float64(global_counter)) 186 p.Printf("Ops per threads : %15d\n", global_counter / uint64(nthreads)) 187 p.Printf("Ops per procs : %15d\n", global_counter / uint64(nprocs)) 188 p.Printf("Ops/sec/procs : %18.2f\n", (float64(global_counter) / float64(nprocs)) / delta.Seconds()) 189 p.Printf("ns per ops/procs : %18.2f\n", float64(delta.Nanoseconds()) / (float64(global_counter) / float64(nprocs))) 190 } 239 p.Printf("Total Operations(ops) : %15d\n", global_result.count) 240 p.Printf("Total G Migrations : %15d\n", global_result.gmigs) 241 p.Printf("Total D Migrations : %15d\n", global_result.dmigs) 242 p.Printf("Ops per second : %18.2f\n", float64(global_result.count) / delta.Seconds()) 243 p.Printf("ns per ops : %18.2f\n", float64(delta.Nanoseconds()) / float64(global_result.count)) 244 p.Printf("Ops per threads : %15d\n", global_result.count / uint64(nthreads)) 245 p.Printf("Ops per procs : %15d\n", global_result.count / uint64(nprocs)) 246 p.Printf("Ops/sec/procs : %18.2f\n", (float64(global_result.count) / float64(nprocs)) / delta.Seconds()) 247 p.Printf("ns per ops/procs : %18.2f\n", float64(delta.Nanoseconds()) / (float64(global_result.count) / float64(nprocs))) 248 }
Note: See TracChangeset
for help on using the changeset viewer.