-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.go
297 lines (245 loc) · 7.24 KB
/
main.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package main
import (
"bytes"
"flag"
"fmt"
"log"
"math"
"os"
"reflect"
"runtime"
"runtime/debug"
"runtime/pprof"
"runtime/trace"
"sort"
"strings"
"sync"
"syscall"
"time"
"unsafe"
)
type Timer struct {
start int64
}
func NewTimer() Timer {
return Timer{
start: time.Now().UnixNano(),
}
}
func (t *Timer) Elapsed() time.Duration {
return time.Duration(time.Now().UnixNano() - t.start)
}
func (t *Timer) ElapsedAsSeconds() float64 {
return t.Elapsed().Seconds()
}
func (t *Timer) ElapsedAsMilliseconds() float64 {
return t.Elapsed().Seconds() * 1000
}
const (
PROT_READ = syscall.PROT_READ
PROT_WRITE = syscall.PROT_WRITE
MAP_SHARED = syscall.MAP_SHARED
MAP_PRIVATE = syscall.MAP_PRIVATE
)
func Map(fd uintptr, offset int, len int, prot int, flags int) ([]byte, error) {
return syscall.Mmap(int(fd), int64(offset), len, prot, flags)
}
func UnMap(b []byte) error {
return syscall.Munmap(b)
}
const OUTPUT_PATH = "outputs.txt"
const MEASUREMENTS_PATH = "measurements.txt"
var threads = runtime.NumCPU() * 3 / 2
var pagesize = syscall.Getpagesize()
const hash_space = 1 << 8
const hash_mask = hash_space - 1
type hashtable struct {
buckets [hash_space]bucket
}
type bucket struct {
Key [31]byte // 31
Len byte // 32
Value stats // 64
}
type stats struct {
min, max, sum int64
count int64
}
func fastString(b []byte) string { return *(*string)(unsafe.Pointer(&b)) }
func ht_index(b unsafe.Pointer, _len int64) uint64 {
if _len > 8 {
_len = 8
}
var x0 uint64 = *(*uint64)(b) << ((8 - _len) * 8)
x0 += 7048676786887195564
x0 = (x0 ^ (x0 >> 30)) * 0xbf58476d1ce4e5b9
x0 = (x0 ^ (x0 >> 27)) * 0x94d049bb133111eb
return (x0 ^ (x0 >> 31)) & hash_mask
}
func chunk(view []byte, n int64) (bounds []int64) {
bounds = make([]int64, n+1)
bounds[n] = int64(len(view))
block_size := int64(len(view)) / int64(n)
var offset int64
// Assume the view is large enough not to cause an index out of bounds error.
for i := int64(1); i < n; i++ {
offset = block_size * i
newline_index := bytes.IndexByte(view[offset:], '\n')
if newline_index == -1 {
panic("Invalid View")
}
offset += int64(newline_index + 1)
bounds[i] = offset
}
return bounds
}
const msk0 uint64 = 0b0011000000110000001100000011000000110000001100000011000000110000
const msk1 uint64 = 0b0000000000000000000000001111111100000000000000000000000011111111
const exp0 uint64 = 0b0000000000000000001001110001000000000000000000000000000000000001
const exp1 uint64 = 0b0000000000001111010000100100000000000000000000000000000001100100
//go:nosplit
func processChunk(ht *hashtable, view []byte, wg *sync.WaitGroup) {
for i := range ht.buckets {
ht.buckets[i].Value.max = 0
ht.buckets[i].Value.min = math.MaxInt64
}
index := int64(0)
for {
city_index := bytes.IndexByte(view[index:], ';')
if city_index == -1 {
wg.Done()
return
}
city_bytes := view[index : index+int64(city_index)]
idx := ht_index(unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&city_bytes)).Data), int64(city_index))
temp_index := bytes.IndexByte(view[index+int64(city_index)+1:], '\n')
temp_bytes := view[index+int64(city_index)+1 : index+int64(city_index)+1+int64(temp_index)]
var val uint64 = *(*uint64)(unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&temp_bytes)).Data))
val ^= msk0
val = (val << ((8 - len(temp_bytes)) * 8))
val = (val * 10) + (val >> 8)
val = (((val & msk1) * exp1) + (((val >> 16) & msk1) * exp0)) >> 32
temp := int64(val)
if ht.buckets[idx].Key[0] == 0 {
ht.buckets[idx].Len = byte(city_index)
copy(ht.buckets[idx].Key[:], city_bytes)
}
ht.buckets[idx].Value.min = ht.buckets[idx].Value.min + ((temp - ht.buckets[idx].Value.min) & ((temp - ht.buckets[idx].Value.min) >> 63))
ht.buckets[idx].Value.max = ht.buckets[idx].Value.max - ((ht.buckets[idx].Value.max - temp) & ((ht.buckets[idx].Value.max - temp) >> 63))
ht.buckets[idx].Value.sum += temp
ht.buckets[idx].Value.count++
index += int64(city_index) + 1 + int64(temp_index) + 1
}
}
func solution(inputPath string) []byte {
buffer := make([]byte, 0, pagesize)
// Open File
finfo, err := os.Stat(inputPath)
if err != nil {
panic(err)
}
fd, err := syscall.Open(inputPath, syscall.O_RDONLY, 0)
if err != nil {
panic(err)
}
view, err := Map(uintptr(fd), 0, int(finfo.Size()), PROT_READ, MAP_SHARED)
if err != nil {
panic(err)
}
var wg sync.WaitGroup
var hashtables []hashtable = make([]hashtable, int64(threads))
wg.Add(threads)
bounds := chunk(view, int64(threads))
for i := int64(0); i < int64(threads); i++ {
chunk := view[bounds[i]:bounds[i+1]]
go processChunk(&hashtables[i], chunk, &wg)
}
wg.Wait()
stationStats := make(map[string]*stats)
stations := make([]string, 0, 128)
for i := range hashtables {
for j := range hashtables[i].buckets {
if hashtables[i].buckets[j].Len > 0 {
key := fastString(hashtables[i].buckets[j].Key[:hashtables[i].buckets[j].Len])
stats := stationStats[key]
if stats == nil {
stationStats[key] = &hashtables[i].buckets[j].Value
stations = append(stations, key)
continue
}
if hashtables[i].buckets[j].Value.min < stats.min {
stats.min = hashtables[i].buckets[j].Value.min
}
if hashtables[i].buckets[j].Value.max > stats.max {
stats.max = hashtables[i].buckets[j].Value.max
}
stats.sum += hashtables[i].buckets[j].Value.sum
stats.count += hashtables[i].buckets[j].Value.count
}
}
}
sort.Strings(stations)
for _, station := range stations {
s := stationStats[station]
mean := s.sum / s.count
buffer = append(buffer, fmt.Sprintf("%s=%d;%d;%d(%d/%d)\n", station, s.min, s.max, mean, s.sum, s.count)...)
}
runtime.KeepAlive(&hashtables) // Memory Lifetime
return buffer
}
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`")
var traceprofile = flag.String("traceprofile", "", "write trace profile to `file`")
var memprofile = flag.String("memprofile", "", "write memory profile to `file`")
func main() {
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal("could not create CPU profile: ", err)
}
defer f.Close()
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
defer pprof.StopCPUProfile()
}
if *traceprofile != "" {
f, err := os.Create(*traceprofile)
if err != nil {
log.Fatal("could not create Trace profile: ", err)
}
defer f.Close()
if err := trace.Start(f); err != nil {
log.Fatal("could not start Trace profile: ", err)
}
defer trace.Stop()
}
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
log.Fatal("could not create Memory profile: ", err)
}
defer f.Close()
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not write Memory profile: ", err)
}
}
expect_output, err := os.ReadFile(OUTPUT_PATH)
if err != nil {
panic(err)
}
runtime.GOMAXPROCS(threads)
debug.SetGCPercent(-1)
timer := NewTimer()
got := solution(MEASUREMENTS_PATH)
fmt.Printf("Elapsed: %fms\n", timer.ElapsedAsMilliseconds())
if strings.TrimSpace(string(expect_output)) != strings.TrimSpace(string(got)) {
fmt.Println("Expect:")
os.Stdout.Write(expect_output)
fmt.Println("Got:")
os.Stdout.Write(got)
panic("Not matched")
} else {
fmt.Println("Matched")
}
}