Skip to content

Commit

Permalink
Some internal cleanup from previous PR (#498)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexklibisz authored Feb 27, 2023
1 parent 5aac5b3 commit c6a396c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ class VectorOpsJmhBenchmark {
@Fork(value = 1)
@Warmup(time = 5, iterations = 6)
@Measurement(time = 5, iterations = 6)
def dotProductPanama(state: BenchmarkState): Unit =
state.panama.dotProduct(state.v1, state.v2)
def dotProductDefault(state: BenchmarkState): Double =
state.default.dotProduct(state.v1, state.v2)

@Benchmark
@BenchmarkMode(Array(Mode.Throughput))
@Fork(value = 1)
@Warmup(time = 5, iterations = 6)
@Measurement(time = 5, iterations = 6)
def dotProductDefault(state: BenchmarkState): Double =
state.default.cosineSimilarity(state.v1, state.v2)
def dotProductPanama(state: BenchmarkState): Unit =
state.panama.dotProduct(state.v1, state.v2)

@Benchmark
@BenchmarkMode(Array(Mode.Throughput))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,21 @@ public double cosineSimilarity(float[] v1, float[] v2) {
else return -1;
}

public double dotProduct(float[] v1, float[] v2) {
float dotProd = 0f;
for (int i = 0; i < v1.length; i++) dotProd += v1[i] * v2[i];
return dotProd;
}

public double l1Distance(float[] v1, float[] v2) {
double sumAbsDiff = 0.0;
for (int i = 0; i < v1.length; i++) {
sumAbsDiff += Math.abs(v1[i] - v2[i]);
}
for (int i = 0; i < v1.length; i++) sumAbsDiff += Math.abs(v1[i] - v2[i]);
return sumAbsDiff;
}

public double dotProduct(float[] v1, float[] v2) {
float dp = 0f;
for (int i = 0; i < v1.length; i++) dp += v1[i] * v2[i];
return dp;
}

public double l2Distance(float[] v1, float[] v2) {
double sumSqrDiff = 0.0;
for (int i = 0; i < v1.length; i++) {
sumSqrDiff += Math.pow(v1[i] - v2[i], 2);
}
for (int i = 0; i < v1.length; i++) sumSqrDiff += Math.pow(v1[i] - v2[i], 2);
return Math.sqrt(sumSqrDiff);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

public interface FloatVectorOps {

double dotProduct(final float[] v1, final float[] v2);
double dotProduct(float[] v1, float[] v2);

double l2Distance(final float[] v1, final float[] v2);
double l2Distance(float[] v1, float[] v2);

double l1Distance(float[] v1, float[] v2);

public double cosineSimilarity(float[] v1, float[] v2);
double cosineSimilarity(float[] v1, float[] v2);
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ public double cosineSimilarity(float[] v1, float[] v2) {
}

public double dotProduct(float[] v1, float[] v2) {
double dp = 0f;
double dotProd = 0f;
int i = 0;
int bound = species.loopBound(v1.length);
FloatVector pv1, pv2;
for (; i < bound; i += species.length()) {
pv1 = FloatVector.fromArray(species, v1, i);
pv2 = FloatVector.fromArray(species, v2, i);
dp += pv1.mul(pv2).reduceLanes(VectorOperators.ADD);
dotProd += pv1.mul(pv2).reduceLanes(VectorOperators.ADD);
}
for (; i < v1.length; i++) {
dp = Math.fma(v1[i], v2[i], dp);
dotProd = Math.fma(v1[i], v2[i], dotProd);
}
return dp;
return dotProd;
}


Expand Down

0 comments on commit c6a396c

Please sign in to comment.