goankur commented on code in PR #13572:
URL: https://github.com/apache/lucene/pull/13572#discussion_r1819967252


##########
lucene/native/src/c/dotProduct.c:
##########
@@ -0,0 +1,209 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include "dotProduct.h"
+
+#ifdef __ARM_ACLE
+#include <arm_acle.h>
+#endif
+
+#if (defined(__ARM_FEATURE_SVE))
+#include <arm_sve.h>
+
+/**
+  - ARM intrinsics guide - 
https://developer.arm.com/architectures/instruction-sets/intrinsics/#q=svptrue
+  - SVE Programming examples - 
https://developer.arm.com/documentation/dai0548/latest/
+*/
+void dump(int8_t vec[], int N) {
+  printf("[");
+  for (int i = 0; i < N ; i++) {
+      printf("%d,",vec[i]);
+  }
+  printf("]\n");
+}
+/*
+ * Unrolled and vectorized int8 dotProduct implementation using SVE 
instructions
+ * NOTE: Clang 15.0 compiler on Apple M3 Max compiles the code below 
successfully
+ * with '-march=native+sve' option but throws "Illegal Hardware Instruction" 
error
+ * Looks like Apple M3 does not implement SVE and Apple's official 
documentation
+ * is not explicit about this or at least I could not find it.
+ * 
+ */
+int32_t vdot8s_sve(int8_t vec1[], int8_t vec2[], int32_t limit) {

Review Comment:
   > Was the gcc path less performant?
   
   Yes. The compiler auto-vectorized dot-product implementation shows `~15%` 
**LOWER** throughput compared to this manually unrolled and vectorized 
implementation which uses SVE intrinsics. Here is the JMH benchmark output from 
the next revision executed on `AWS Graviton 3` instance. C implementation 
compiled with `GCC 10` and compiler flags `-O3 -march=native -funroll-loops`
   
   ```
   Benchmark                        (size)   Mode  Cnt   Score   Error   Units
   VectorUtilBenchmark.dot8s           768  thrpt   15  43.233 ± 0.639  ops/us
   VectorUtilBenchmark.simpleDot8s     768  thrpt   15  36.437 ± 0.572  ops/us
   ```
   
   Command to reproduce these results with the next revision
   
   ```
       java --enable-native-access=ALL-UNNAMED \
               --enable-preview \
               
-Djava.library.path="./lucene/native/build/libs/dotProduct/shared" \
               -jar 
lucene/benchmark-jmh/build/benchmarks/lucene-benchmark-jmh-11.0.0-SNAPSHOT.jar 
regexp \
                "(.*)?(ot8s)" \
                -p "size=768"
   ```
   
   A benefit of manually vectorized implementation is that our implementation 
is less likely to suffer from performance regressions due to auto-vectorization 
variations across different ARM compilers.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to