forked from dgraph-io/ristretto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseline.go
127 lines (118 loc) · 2.11 KB
/
baseline.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package simd
import (
"fmt"
"runtime"
"sort"
"sync"
)
// Search finds the key using the naive way
func Naive(xs []uint64, k uint64) int16 {
var i int
for i = 0; i < len(xs); i += 2 {
x := xs[i]
if x >= k {
return int16(i / 2)
}
}
return int16(i / 2)
}
func Clever(xs []uint64, k uint64) int16 {
if len(xs) < 8 {
return Naive(xs, k)
}
var twos, pk [4]uint64
pk[0] = k
pk[1] = k
pk[2] = k
pk[3] = k
for i := 0; i < len(xs); i += 8 {
twos[0] = xs[i]
twos[1] = xs[i+2]
twos[2] = xs[i+4]
twos[3] = xs[i+6]
if twos[0] >= pk[0] {
return int16(i / 2)
}
if twos[1] >= pk[1] {
return int16((i + 2) / 2)
}
if twos[2] >= pk[2] {
return int16((i + 4) / 2)
}
if twos[3] >= pk[3] {
return int16((i + 6) / 2)
}
}
return int16(len(xs) / 2)
}
func Parallel(xs []uint64, k uint64) int16 {
cpus := runtime.NumCPU()
if cpus%2 != 0 {
panic(fmt.Sprintf("odd number of CPUs %v", cpus))
}
sz := len(xs)/cpus + 1
var wg sync.WaitGroup
retChan := make(chan int16, cpus)
for i := 0; i < len(xs); i += sz {
end := i + sz
if end >= len(xs) {
end = len(xs)
}
chunk := xs[i:end]
wg.Add(1)
go func(hd int16, xs []uint64, k uint64, wg *sync.WaitGroup, ch chan int16) {
for i := 0; i < len(xs); i += 2 {
if xs[i] >= k {
ch <- (int16(i) + hd) / 2
break
}
}
wg.Done()
}(int16(i), chunk, k, &wg, retChan)
}
wg.Wait()
close(retChan)
var min int16 = (1 << 15) - 1
for i := range retChan {
if i < min {
min = i
}
}
if min == (1<<15)-1 {
return int16(len(xs) / 2)
}
return min
}
func Binary(keys []uint64, key uint64) int16 {
return int16(sort.Search(len(keys), func(i int) bool {
if i*2 >= len(keys) {
return true
}
return keys[i*2] >= key
}))
}
func cmp2_native(twos, pk [2]uint64) int16 {
if twos[0] == pk[0] {
return 0
}
if twos[1] == pk[1] {
return 1
}
return 2
}
func cmp4_native(fours, pk [4]uint64) int16 {
for i := range fours {
if fours[i] >= pk[i] {
return int16(i)
}
}
return 4
}
func cmp8_native(a [8]uint64, pk [4]uint64) int16 {
for i := range a {
if a[i] >= pk[0] {
return int16(i)
}
}
return 8
}