Skip to content

Commit 2a98cbe

Browse files
committed
add benchmark
1 parent a156056 commit 2a98cbe

File tree

3 files changed

+92
-10
lines changed

3 files changed

+92
-10
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
10+
package org.elasticsearch.benchmark.vector.scorer;
11+
12+
import org.apache.lucene.util.quantization.OptimizedScalarQuantizer;
13+
import org.elasticsearch.common.logging.LogConfigurator;
14+
import org.elasticsearch.simdvec.ESVectorUtil;
15+
import org.openjdk.jmh.annotations.Benchmark;
16+
import org.openjdk.jmh.annotations.BenchmarkMode;
17+
import org.openjdk.jmh.annotations.Fork;
18+
import org.openjdk.jmh.annotations.Measurement;
19+
import org.openjdk.jmh.annotations.Mode;
20+
import org.openjdk.jmh.annotations.OutputTimeUnit;
21+
import org.openjdk.jmh.annotations.Param;
22+
import org.openjdk.jmh.annotations.Scope;
23+
import org.openjdk.jmh.annotations.Setup;
24+
import org.openjdk.jmh.annotations.State;
25+
import org.openjdk.jmh.annotations.Warmup;
26+
import org.openjdk.jmh.infra.Blackhole;
27+
28+
import java.io.IOException;
29+
import java.util.Random;
30+
import java.util.concurrent.TimeUnit;
31+
32+
@BenchmarkMode(Mode.Throughput)
33+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
34+
@State(Scope.Benchmark)
35+
// first iteration is complete garbage, so make sure we really warmup
36+
@Warmup(iterations = 4, time = 1)
37+
// real iterations. not useful to spend tons of time here, better to fork more
38+
@Measurement(iterations = 5, time = 1)
39+
// engage some noise reduction
40+
@Fork(value = 1)
41+
public class VectorUtilsBenchmark {
42+
43+
static {
44+
LogConfigurator.configureESLogging(); // native access requires logging to be initialized
45+
}
46+
47+
@Param({ "384", "782", "1024" })
48+
int dims;
49+
50+
int numVectors = 4; // * 100;
51+
float[][] vectors;
52+
53+
@Setup
54+
public void setup() throws IOException {
55+
Random random = new Random(123);
56+
57+
vectors = new float[numVectors][dims];
58+
for (float[] vector : vectors) {
59+
for (int i = 0; i < dims; i++) {
60+
vector[i] = random.nextFloat();
61+
}
62+
}
63+
}
64+
65+
@Benchmark
66+
@Fork(jvmArgsPrepend = { "--add-modules=jdk.incubator.vector" })
67+
public void vectorAcc(Blackhole bh) {
68+
float[] acc = new float[dims];
69+
for (int i = 0; i < numVectors; i++) {
70+
ESVectorUtil.vectorAccumulateAdd(acc, vectors[i]);
71+
}
72+
}
73+
74+
@Benchmark
75+
public void defaultVectorAcc(Blackhole bh) {
76+
float[] acc = new float[dims];
77+
for (int i = 0; i < numVectors; i++) {
78+
float[] vector = vectors[i];
79+
for (int j = 0; j < vector.length; j++) {
80+
acc[j] += vector[j];
81+
}
82+
}
83+
}
84+
}

libs/simdvec/src/main21/java/org/elasticsearch/simdvec/internal/vectorization/PanamaESVectorUtilSupport.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,12 +1143,11 @@ public int indexOf(final byte[] bytes, final int offset, final int length, final
11431143

11441144
@Override
11451145
public void vectorAccumulateAdd(float[] a, float[] b) {
1146-
final VectorSpecies<Float> SPECIES = FloatVector.SPECIES_PREFERRED;
1147-
11481146
int i = 0;
1149-
for (; i < SPECIES.loopBound(a.length); i += SPECIES.length()) {
1150-
FloatVector va = FloatVector.fromArray(SPECIES, a, i);
1151-
FloatVector vb = FloatVector.fromArray(SPECIES, b, i);
1147+
int limit = FLOAT_SPECIES.loopBound(a.length);
1148+
for (; i < limit; i += FLOAT_SPECIES.length()) {
1149+
FloatVector va = FloatVector.fromArray(FLOAT_SPECIES, a, i);
1150+
FloatVector vb = FloatVector.fromArray(FLOAT_SPECIES, b, i);
11521151
FloatVector vc = va.add(vb);
11531152
vc.intoArray(a, i);
11541153
}
@@ -1160,11 +1159,9 @@ public void vectorAccumulateAdd(float[] a, float[] b) {
11601159

11611160
@Override
11621161
public void vectorScalerDivide(float[] a, float b) {
1163-
final VectorSpecies<Float> SPECIES = FloatVector.SPECIES_PREFERRED;
1164-
11651162
int i = 0;
1166-
for (; i < SPECIES.loopBound(a.length); i += SPECIES.length()) {
1167-
FloatVector va = FloatVector.fromArray(SPECIES, a, i);
1163+
for (; i < FLOAT_SPECIES.loopBound(a.length); i += FLOAT_SPECIES.length()) {
1164+
FloatVector va = FloatVector.fromArray(FLOAT_SPECIES, a, i);
11681165
FloatVector vc = va.div(b);
11691166
vc.intoArray(a, i);
11701167
}

server/src/main/java/org/elasticsearch/index/codec/vectors/cluster/KMeansLocal.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,10 @@ private static boolean stepLloyd(
107107
final int assignment = assignments[translateOrd.apply(idx)];
108108
if (centroidChanged.get(assignment)) {
109109
if (centroidCounts[assignment]++ == 0) {
110-
centroids[assignment] = vectors.vectorValue(idx);
110+
System.arraycopy(vectors.vectorValue(idx), 0, centroids[assignment], 0, vectors.vectorValue(idx).length);
111111
continue;
112112
}
113+
113114
ESVectorUtil.vectorAccumulateAdd(centroids[assignment], vectors.vectorValue(idx));
114115
}
115116
}

0 commit comments

Comments
 (0)