Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Cacheline And False Sharing
package main
import (
"sync"
"sync/atomic"
"testing"
)
const THREADS int = 8
const LOOP int = 100000
func BenchmarkCachelineMutex(b *testing.B) {
for run := 0; run < b.N; run++ {
total := 0
var wg sync.WaitGroup
wg.Add(THREADS)
var mu sync.Mutex
for i := 0; i < THREADS; i++ {
go func() {
for j := 0; j < LOOP; j++ {
mu.Lock()
total++
mu.Unlock()
}
wg.Done()
}()
}
wg.Wait()
}
}
func BenchmarkCachelineAtomic(b *testing.B) {
for run := 0; run < b.N; run++ {
var total = new(int32)
var wg sync.WaitGroup
wg.Add(THREADS)
for i := 0; i < THREADS; i++ {
go func() {
for j := 0; j < LOOP; j++ {
atomic.AddInt32(total, 1)
}
wg.Done()
}()
}
wg.Wait()
}
}
func BenchmarkCachelineArray1(b *testing.B) {
for run := 0; run < b.N; run++ {
var total [2048]int
var wg sync.WaitGroup
wg.Add(THREADS)
for i := 0; i < THREADS; i++ {
go func(idx int) {
for j := 0; j < LOOP; j++ {
total[idx]++
}
wg.Done()
}(i * 1)
}
wg.Wait()
}
}
func BenchmarkCachelineArray2(b *testing.B) {
for run := 0; run < b.N; run++ {
var total [2048]int
var wg sync.WaitGroup
wg.Add(THREADS)
for i := 0; i < THREADS; i++ {
go func(idx int) {
for j := 0; j < LOOP; j++ {
total[idx]++
}
wg.Done()
}(i * 2)
}
wg.Wait()
}
}
func BenchmarkCachelineArray3(b *testing.B) {
for run := 0; run < b.N; run++ {
var total [2048]int
var wg sync.WaitGroup
wg.Add(THREADS)
for i := 0; i < THREADS; i++ {
go func(idx int) {
for j := 0; j < LOOP; j++ {
total[idx]++
}
wg.Done()
}(i * 3)
}
wg.Wait()
}
}
func BenchmarkCachelineArray4(b *testing.B) {
for run := 0; run < b.N; run++ {
var total [2048]int
var wg sync.WaitGroup
wg.Add(THREADS)
for i := 0; i < THREADS; i++ {
go func(idx int) {
for j := 0; j < LOOP; j++ {
total[idx]++
}
wg.Done()
}(i * 4)
}
wg.Wait()
}
}
func BenchmarkCachelineArray5(b *testing.B) {
for run := 0; run < b.N; run++ {
var total [2048]int
var wg sync.WaitGroup
wg.Add(THREADS)
for i := 0; i < THREADS; i++ {
go func(idx int) {
for j := 0; j < LOOP; j++ {
total[idx]++
}
wg.Done()
}(i * 5)
}
wg.Wait()
}
}
func BenchmarkCachelineArray6(b *testing.B) {
for run := 0; run < b.N; run++ {
var total [2048]int
var wg sync.WaitGroup
wg.Add(THREADS)
for i := 0; i < THREADS; i++ {
go func(idx int) {
for j := 0; j < LOOP; j++ {
total[idx]++
}
wg.Done()
}(i * 6)
}
wg.Wait()
}
}
func BenchmarkCachelineArray7(b *testing.B) {
for run := 0; run < b.N; run++ {
var total [2048]int
var wg sync.WaitGroup
wg.Add(THREADS)
for i := 0; i < THREADS; i++ {
go func(idx int) {
for j := 0; j < LOOP; j++ {
total[idx]++
}
wg.Done()
}(i * 7)
}
wg.Wait()
}
}
func BenchmarkCachelineArray8(b *testing.B) {
for run := 0; run < b.N; run++ {
var total [2048]int
var wg sync.WaitGroup
wg.Add(THREADS)
for i := 0; i < THREADS; i++ {
go func(idx int) {
for j := 0; j < LOOP; j++ {
total[idx]++
}
wg.Done()
}(i * 8)
}
wg.Wait()
}
}
func BenchmarkCachelineArray9(b *testing.B) {
for run := 0; run < b.N; run++ {
var total [2048]int
var wg sync.WaitGroup
wg.Add(THREADS)
for i := 0; i < THREADS; i++ {
go func(idx int) {
for j := 0; j < LOOP; j++ {
total[idx]++
}
wg.Done()
}(i * 9)
}
wg.Wait()
}
}
func BenchmarkCachelineArray10(b *testing.B) {
for run := 0; run < b.N; run++ {
var total [2048]int
var wg sync.WaitGroup
wg.Add(THREADS)
for i := 0; i < THREADS; i++ {
go func(idx int) {
for j := 0; j < LOOP; j++ {
total[idx]++
}
wg.Done()
}(i * 10)
}
wg.Wait()
}
}
func BenchmarkCachelineArray11(b *testing.B) {
for run := 0; run < b.N; run++ {
var total [2048]int
var wg sync.WaitGroup
wg.Add(THREADS)
for i := 0; i < THREADS; i++ {
go func(idx int) {
for j := 0; j < LOOP; j++ {
total[idx]++
}
wg.Done()
}(i * 11)
}
wg.Wait()
}
}
func BenchmarkCachelineArray12(b *testing.B) {
for run := 0; run < b.N; run++ {
var total [2048]int
var wg sync.WaitGroup
wg.Add(THREADS)
for i := 0; i < THREADS; i++ {
go func(idx int) {
for j := 0; j < LOOP; j++ {
total[idx]++
}
wg.Done()
}(i * 12)
}
wg.Wait()
}
}
func BenchmarkCachelineArray13(b *testing.B) {
for run := 0; run < b.N; run++ {
var total [2048]int
var wg sync.WaitGroup
wg.Add(THREADS)
for i := 0; i < THREADS; i++ {
go func(idx int) {
for j := 0; j < LOOP; j++ {
total[idx]++
}
wg.Done()
}(i * 13)
}
wg.Wait()
}
}
func BenchmarkCachelineArray14(b *testing.B) {
for run := 0; run < b.N; run++ {
var total [2048]int
var wg sync.WaitGroup
wg.Add(THREADS)
for i := 0; i < THREADS; i++ {
go func(idx int) {
for j := 0; j < LOOP; j++ {
total[idx]++
}
wg.Done()
}(i * 14)
}
wg.Wait()
}
}
func BenchmarkCachelineArray15(b *testing.B) {
for run := 0; run < b.N; run++ {
var total [2048]int
var wg sync.WaitGroup
wg.Add(THREADS)
for i := 0; i < THREADS; i++ {
go func(idx int) {
for j := 0; j < LOOP; j++ {
total[idx]++
}
wg.Done()
}(i * 15)
}
wg.Wait()
}
}
func BenchmarkCachelineArray16(b *testing.B) {
for run := 0; run < b.N; run++ {
var total [2048]int
var wg sync.WaitGroup
wg.Add(THREADS)
for i := 0; i < THREADS; i++ {
go func(idx int) {
for j := 0; j < LOOP; j++ {
total[idx]++
}
wg.Done()
}(i * 16)
}
wg.Wait()
}
}
func BenchmarkCachelineArray17(b *testing.B) {
for run := 0; run < b.N; run++ {
var total [2048]int
var wg sync.WaitGroup
wg.Add(THREADS)
for i := 0; i < THREADS; i++ {
go func(idx int) {
for j := 0; j < LOOP; j++ {
total[idx]++
}
wg.Done()
}(i * 17)
}
wg.Wait()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment