aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/dev/morling/onebrc/CalculateAverage_parkertimmins.java
blob: 71412fb78bcee929ce663965be31925e537fd40d (plain)
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
 *  Copyright 2023 The original authors
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package dev.morling.onebrc;

import jdk.incubator.vector.ByteVector;
import jdk.incubator.vector.VectorMask;
import jdk.incubator.vector.VectorOperators;

import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;

import java.lang.foreign.ValueLayout;
import java.lang.reflect.Array;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;
import java.util.zip.CRC32C;

public class CalculateAverage_parkertimmins {
    private static final String FILE = "./measurements.txt";
    // private static final String FILE = "./full_measurements.no_license";

    private static record ResultRow(double min, double mean, double max) {
        public String toString() {
            return round(min) + "/" + round(mean) + "/" + round(max);
        }

        private double round(double value) {
            return Math.round(value * 10.0) / 10.0;
        }
    };

    static class OpenHashTable {
        static class Entry {
            byte[] key;
            short min;
            short max;
            long sum = 0;
            long count = 0;
            int hash;

            void merge(OpenHashTable.Entry other) {
                min = (short) Math.min(min, other.min);
                max = (short) Math.max(max, other.max);
                sum += other.sum;
                count += other.count;
            }
        }

        static final int bits = 14;
        static final int tableSize = 1 << bits; // 16k
        static final int mask = tableSize - 1;
        final Entry[] entries = new Entry[tableSize];

        void add(byte[] buf, int sLen, short val, int hash) {
            int idx = hash & mask;

            while (true) {
                Entry entry = entries[idx];

                // key not present, so add it
                if (entry == null) {
                    entry = entries[idx] = new Entry();
                    entry.key = Arrays.copyOf(buf, sLen);
                    entry.min = entry.max = val;
                    entry.sum += val;
                    entry.count++;
                    entry.hash = hash;
                    break;
                }
                else {
                    if (entry.hash == hash && entry.key.length == sLen && Arrays.equals(entry.key, 0, sLen, buf, 0, sLen)) {
                        entry.min = (short) Math.min(entry.min, val);
                        entry.max = (short) Math.max(entry.max, val);
                        entry.sum += val;
                        entry.count++;
                        break;
                    }
                    else {
                        idx = (idx + 1) & mask;
                    }
                }
            }
        }
    }

    static long findNextEntryStart(MemorySegment ms, long offset) {
        long curr = offset;
        while (ms.get(ValueLayout.JAVA_BYTE, curr) != '\n') {
            curr++;
        }
        curr++;
        return curr;
    }

    static short[] digits10s = { 0, 100, 200, 300, 400, 500, 600, 700, 800, 900 };
    static short[] digits1s = { 0, 10, 20, 30, 40, 50, 60, 70, 80, 90 };

    static void processRangeScalar(MemorySegment ms, long start, long end, final OpenHashTable localAgg) {
        byte[] buf = new byte[128];

        long curr = start;
        long limit = end;

        while (curr < limit) {
            int i = 0;
            byte val = ms.get(ValueLayout.JAVA_BYTE, curr);
            while (val != ';') {
                buf[i++] = val;
                curr++;
                val = ms.get(ValueLayout.JAVA_BYTE, curr);
            }

            int sLen = i;
            int hash = hash(buf, sLen);

            curr++; // skip semicolon

            long tempIdx = curr;
            boolean neg = ms.get(ValueLayout.JAVA_BYTE, tempIdx) == '-';
            boolean twoDig = ms.get(ValueLayout.JAVA_BYTE, tempIdx + 1 + (neg ? 1 : 0)) == '.';
            int len = 3 + (neg ? 1 : 0) + (twoDig ? 0 : 1);
            int d0 = ((char) ms.get(ValueLayout.JAVA_BYTE, tempIdx + len - 1)) - '0';
            int d1 = ((char) ms.get(ValueLayout.JAVA_BYTE, tempIdx + len - 3)) - '0';
            int base = d0 + digits1s[d1] + (twoDig ? 0 : digits10s[((char) ms.get(ValueLayout.JAVA_BYTE, tempIdx + len - 4)) - '0']);
            short temp = (short) (neg ? -base : base);

            localAgg.add(buf, sLen, temp, hash);
            curr = tempIdx + len + 1;
        }
    }

    static int hash(byte[] buf, int sLen) {
        // TODO find a hash that works directly from byte array
        // if shorter than 8 chars, mask out upper bits
        long mask = sLen < 8 ? -(1L << ((8 - sLen) << 3)) : 0xFFFFFFFFL;
        long val = ((buf[0] & 0xffL) << 56) | ((buf[1] & 0xffL) << 48) | ((buf[2] & 0xffL) << 40) | ((buf[3] & 0xffL) << 32) | ((buf[4] & 0xffL) << 24)
                | ((buf[5] & 0xffL) << 16) | ((buf[6] & 0xFFL) << 8) | (buf[7] & 0xffL);
        val &= mask;

        // also worth trying: https://lemire.me/blog/2015/10/22/faster-hashing-without-effort/
        // lemire: https://lemire.me/blog/2023/07/14/recognizing-string-prefixes-with-simd-instructions/
        int hash = (int) (((((val >> 32) ^ val) & 0xffffffffL) * 3523216699L) >> 32);
        return hash;
    }

    static void processRangeSIMD(MemorySegment ms, boolean frontPad, boolean backPad, long start, long end, final OpenHashTable localAgg) {
        byte[] buf = new byte[128];

        long curr = frontPad ? findNextEntryStart(ms, start) : start;
        long limit = end - padding;

        var needle = ByteVector.broadcast(ByteVector.SPECIES_256, ';');
        while (curr < limit) {

            int segStart = 0;
            int sLen;

            while (true) {
                var section = ByteVector.fromMemorySegment(ByteVector.SPECIES_256, ms, curr + segStart, ByteOrder.LITTLE_ENDIAN);
                section.intoArray(buf, segStart);
                VectorMask<Byte> matches = section.compare(VectorOperators.EQ, needle);
                int idx = matches.firstTrue();
                if (idx != 32) {
                    sLen = segStart + idx;
                    break;
                }
                segStart += 32;
            }

            int hash = hash(buf, sLen);

            curr += sLen;
            curr++; // semicolon

            long tempIdx = curr;
            boolean neg = ms.get(ValueLayout.JAVA_BYTE, tempIdx) == '-';
            boolean twoDig = ms.get(ValueLayout.JAVA_BYTE, tempIdx + 1 + (neg ? 1 : 0)) == '.';
            int len = 3 + (neg ? 1 : 0) + (twoDig ? 0 : 1);
            int d0 = ((char) ms.get(ValueLayout.JAVA_BYTE, tempIdx + len - 1)) - '0';
            int d1 = ((char) ms.get(ValueLayout.JAVA_BYTE, tempIdx + len - 3)) - '0';
            int base = d0 + digits1s[d1] + (twoDig ? 0 : digits10s[((char) ms.get(ValueLayout.JAVA_BYTE, tempIdx + len - 4)) - '0']);
            short temp = (short) (neg ? -base : base);

            localAgg.add(buf, sLen, temp, hash);
            curr = tempIdx + len + 1;
        }

        // last batch is near end of file, process without SIMD to avoid out-of-bounds
        if (!backPad) {
            processRangeScalar(ms, curr, end, localAgg);
        }
    }

    /**
     *  For debugging issues with hash function
      */
    static void checkHashDistributionQuality(ArrayList<OpenHashTable> localAggs) {
        HashSet<Integer> uniquesHashValues = new HashSet<Integer>();
        HashSet<String> uniqueCities = new HashSet<String>();
        HashMap<String, HashSet<Integer>> cityToHash = new HashMap<>();

        for (var agg : localAggs) {
            for (OpenHashTable.Entry entry : agg.entries) {
                if (entry == null) {
                    continue;
                }
                uniquesHashValues.add(entry.hash);
                String station = new String(entry.key, StandardCharsets.UTF_8); // for UTF-8 encoding
                uniqueCities.add(station);

                if (!cityToHash.containsKey(station)) {
                    cityToHash.put(station, new HashSet<>());
                }
                cityToHash.get(station).add(entry.hash);
            }
        }

        for (var pair : cityToHash.entrySet()) {
            if (pair.getValue().size() > 1) {
                System.err.println("multiple hashes: " + pair.getKey() + " " + pair.getValue());
            }
        }

        System.err.println("Unique stations: " + uniqueCities.size() + ", unique hash values: " + uniquesHashValues.size());
    }

    /**
     * Combine thread local values
     */
    static HashMap<String, OpenHashTable.Entry> mergeAggregations(ArrayList<OpenHashTable> localAggs) {
        HashMap<String, OpenHashTable.Entry> global = new HashMap<>();
        for (var agg : localAggs) {
            for (OpenHashTable.Entry entry : agg.entries) {
                if (entry == null) {
                    continue;
                }
                String station = new String(entry.key, StandardCharsets.UTF_8); // for UTF-8 encoding
                var currentVal = global.get(station);
                if (currentVal != null) {
                    currentVal.merge(entry);
                }
                else {
                    global.put(station, entry);
                }
            }
        }
        return global;
    }

    static final long batchSize = 10_000_000;

    static final int padding = 200; // max entry size is 107ish == 100 (station) + 1 (semicolon) + 5 (temp, eg -99.9) + 1 (newline)

    public static void main(String[] args) throws IOException, InterruptedException {
        RandomAccessFile file = new RandomAccessFile(FILE, "r");
        FileChannel channel = file.getChannel();

        int numThreads = Runtime.getRuntime().availableProcessors();

        final long fileSize = channel.size();
        final MemorySegment ms = channel.map(FileChannel.MapMode.READ_ONLY, 0, fileSize, Arena.global());
        final ArrayList<OpenHashTable> localAggs = new ArrayList<>(numThreads);
        Thread[] threads = new Thread[numThreads];
        final AtomicLong progress = new AtomicLong(0);

        class Task implements Runnable {
            final int threadId;

            Task(int threadId) {
                this.threadId = threadId;
            }

            @Override
            public void run() {
                var localAgg = localAggs.get(threadId);
                while (true) {
                    final long startBatch = progress.getAndAdd(batchSize);
                    if (startBatch >= fileSize) {
                        break;
                    }
                    final long endBatch = Math.min(startBatch + batchSize, fileSize);
                    final boolean first = startBatch == 0;
                    final boolean frontPad = !first;
                    final boolean last = endBatch == fileSize;
                    final boolean backPad = !last;
                    processRangeSIMD(ms, frontPad, backPad, startBatch, endBatch, localAgg);
                }
            }
        }

        for (int t = 0; t < numThreads; t++) {
            localAggs.add(new OpenHashTable());
            threads[t] = new Thread(new Task(t), "Thread-" + t);
            threads[t].start();
        }

        for (var thread : threads) {
            thread.join();
        }

        var globalAggs = mergeAggregations(localAggs);

        Map<String, ResultRow> res = new TreeMap<>();
        for (Map.Entry<String, OpenHashTable.Entry> entry : globalAggs.entrySet()) {
            final var ma = entry.getValue();
            res.put(entry.getKey(), new ResultRow(ma.min / 10.0, (ma.sum / 10.0) / ma.count, ma.max / 10.0));
        }
        System.out.println(res);
    }
}