|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | +package org.elasticsearch.benchmark.vector; |
| 10 | + |
| 11 | +import org.apache.lucene.util.VectorUtil; |
| 12 | +import org.elasticsearch.common.logging.LogConfigurator; |
| 13 | +import org.elasticsearch.common.logging.NodeNamePatternConverter; |
| 14 | +import org.elasticsearch.nativeaccess.NativeAccess; |
| 15 | +import org.elasticsearch.nativeaccess.VectorSimilarityFunctions; |
| 16 | +import org.openjdk.jmh.annotations.Benchmark; |
| 17 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 18 | +import org.openjdk.jmh.annotations.Fork; |
| 19 | +import org.openjdk.jmh.annotations.Level; |
| 20 | +import org.openjdk.jmh.annotations.Measurement; |
| 21 | +import org.openjdk.jmh.annotations.Mode; |
| 22 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 23 | +import org.openjdk.jmh.annotations.Param; |
| 24 | +import org.openjdk.jmh.annotations.Scope; |
| 25 | +import org.openjdk.jmh.annotations.Setup; |
| 26 | +import org.openjdk.jmh.annotations.State; |
| 27 | +import org.openjdk.jmh.annotations.TearDown; |
| 28 | +import org.openjdk.jmh.annotations.Warmup; |
| 29 | + |
| 30 | +import java.lang.foreign.Arena; |
| 31 | +import java.lang.foreign.MemorySegment; |
| 32 | +import java.util.concurrent.ThreadLocalRandom; |
| 33 | +import java.util.concurrent.TimeUnit; |
| 34 | + |
| 35 | +@BenchmarkMode(Mode.AverageTime) |
| 36 | +@OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 37 | +@State(Scope.Benchmark) |
| 38 | +@Warmup(iterations = 3, time = 1) |
| 39 | +@Measurement(iterations = 5, time = 1) |
| 40 | +public class JDKVectorInt7uBenchmark { |
| 41 | + |
| 42 | + static { |
| 43 | + NodeNamePatternConverter.setGlobalNodeName("foo"); |
| 44 | + LogConfigurator.loadLog4jPlugins(); |
| 45 | + LogConfigurator.configureESLogging(); // native access requires logging to be initialized |
| 46 | + } |
| 47 | + |
| 48 | + byte[] byteArrayA; |
| 49 | + byte[] byteArrayB; |
| 50 | + MemorySegment heapSegA, heapSegB; |
| 51 | + MemorySegment nativeSegA, nativeSegB; |
| 52 | + |
| 53 | + Arena arena; |
| 54 | + |
| 55 | + @Param({ "1", "128", "207", "256", "300", "512", "702", "1024" }) |
| 56 | + public int size; |
| 57 | + |
| 58 | + @Setup(Level.Iteration) |
| 59 | + public void init() { |
| 60 | + byteArrayA = new byte[size]; |
| 61 | + byteArrayB = new byte[size]; |
| 62 | + for (int i = 0; i < size; ++i) { |
| 63 | + randomInt7BytesBetween(byteArrayA); |
| 64 | + randomInt7BytesBetween(byteArrayB); |
| 65 | + } |
| 66 | + heapSegA = MemorySegment.ofArray(byteArrayA); |
| 67 | + heapSegB = MemorySegment.ofArray(byteArrayB); |
| 68 | + |
| 69 | + arena = Arena.ofConfined(); |
| 70 | + nativeSegA = arena.allocate((long) byteArrayA.length); |
| 71 | + MemorySegment.copy(MemorySegment.ofArray(byteArrayA), 0L, nativeSegA, 0L, byteArrayA.length); |
| 72 | + nativeSegB = arena.allocate((long) byteArrayB.length); |
| 73 | + MemorySegment.copy(MemorySegment.ofArray(byteArrayB), 0L, nativeSegB, 0L, byteArrayB.length); |
| 74 | + } |
| 75 | + |
| 76 | + @TearDown |
| 77 | + public void teardown() { |
| 78 | + arena.close(); |
| 79 | + } |
| 80 | + |
| 81 | + @Benchmark |
| 82 | + @Fork(value = 3, jvmArgsPrepend = { "--add-modules=jdk.incubator.vector" }) |
| 83 | + public int dotProductLucene() { |
| 84 | + return VectorUtil.dotProduct(byteArrayA, byteArrayB); |
| 85 | + } |
| 86 | + |
| 87 | + @Benchmark |
| 88 | + @Fork(value = 3, jvmArgsPrepend = { "--add-modules=jdk.incubator.vector" }) |
| 89 | + public int dotProductNativeWithNativeSeg() { |
| 90 | + return dotProduct7u(nativeSegA, nativeSegB, size); |
| 91 | + } |
| 92 | + |
| 93 | + @Benchmark |
| 94 | + @Fork(value = 3, jvmArgsPrepend = { "--add-modules=jdk.incubator.vector" }) |
| 95 | + public int dotProductNativeWithHeapSeg() { |
| 96 | + return dotProduct7u(heapSegA, heapSegB, size); |
| 97 | + } |
| 98 | + |
| 99 | + static final VectorSimilarityFunctions vectorSimilarityFunctions = vectorSimilarityFunctions(); |
| 100 | + |
| 101 | + static VectorSimilarityFunctions vectorSimilarityFunctions() { |
| 102 | + return NativeAccess.instance().getVectorSimilarityFunctions().get(); |
| 103 | + } |
| 104 | + |
| 105 | + int dotProduct7u(MemorySegment a, MemorySegment b, int length) { |
| 106 | + try { |
| 107 | + return (int) vectorSimilarityFunctions.dotProductHandle7u().invokeExact(a, b, length); |
| 108 | + } catch (Throwable e) { |
| 109 | + if (e instanceof Error err) { |
| 110 | + throw err; |
| 111 | + } else if (e instanceof RuntimeException re) { |
| 112 | + throw re; |
| 113 | + } else { |
| 114 | + throw new RuntimeException(e); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // Unsigned int7 byte vectors have values in the range of 0 to 127 (inclusive). |
| 120 | + static final byte MIN_INT7_VALUE = 0; |
| 121 | + static final byte MAX_INT7_VALUE = 127; |
| 122 | + |
| 123 | + static void randomInt7BytesBetween(byte[] bytes) { |
| 124 | + var random = ThreadLocalRandom.current(); |
| 125 | + for (int i = 0, len = bytes.length; i < len;) { |
| 126 | + bytes[i++] = (byte) random.nextInt(MIN_INT7_VALUE, MAX_INT7_VALUE + 1); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments