You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2020/06/10 01:09:56 UTC

[GitHub] [arrow] jianxind commented on a change in pull request #7314: ARROW-8996: [C++] SSE runtime support for aggregate sum dense kernel

jianxind commented on a change in pull request #7314:
URL: https://github.com/apache/arrow/pull/7314#discussion_r437804878



##########
File path: cpp/src/arrow/compute/kernels/aggregate_basic_sse.cc
##########
@@ -0,0 +1,355 @@
+// 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 "arrow/compute/api_aggregate.h"
+#include "arrow/compute/kernels/aggregate_basic_internal.h"
+#include "arrow/compute/kernels/aggregate_internal.h"
+#include "arrow/compute/kernels/common.h"
+#include "arrow/util/align_util.h"
+#include "arrow/util/simd.h"
+
+TARGET_CODE_START_SSE4_2
+namespace arrow {
+namespace compute {
+namespace aggregate {
+
+// ----------------------------------------------------------------------
+// Sum implementation for SSE
+
+// Each m128 stream handle 2 double/int64 accumulator type, one batch has 4 streams.
+static constexpr int kSseBatchStreams = 4;
+static constexpr int kSseStreamSize = sizeof(__m128d) / sizeof(double);
+static constexpr int kSseBatchSize = kSseBatchStreams * kSseStreamSize;
+
+// Default scalar version
+template <typename T, typename SumT>
+inline SumResult<SumT> SumDenseBatchSse(const T* values, int64_t num_batch) {
+  SumResult<SumT> sum_result;
+  SumT sum_streams[kSseBatchSize] = {0};
+
+  // Add the results by streams
+  for (int64_t batch = 0; batch < num_batch; batch++) {
+    for (int i = 0; i < kSseBatchSize; i++) {
+      sum_streams[i] += values[(batch * kSseBatchSize) + i];
+    }
+  }
+
+  // Aggregate the result streams
+  for (int i = 0; i < kSseBatchSize; i++) {
+    sum_result.sum += sum_streams[i];
+  }
+  sum_result.count = num_batch * kSseBatchSize;
+  return sum_result;
+}
+
+// Dense helper for accumulator type is same to data type
+#define SUM_DENSE_BATCH_SSE_DIRECT(Type, SumSimdType, SimdZeroFn, SimdLoadFn, SimdAddFn) \
+  template <>                                                                            \
+  inline SumResult<Type> SumDenseBatchSse(const Type* values, int64_t num_batch) {       \
+    SumResult<Type> sum_result;                                                          \
+    SumSimdType results_simd[kSseBatchStreams];                                          \
+    for (int i = 0; i < kSseBatchStreams; i++) {                                         \
+      results_simd[i] = SimdZeroFn();                                                    \
+    }                                                                                    \
+                                                                                         \
+    /* Add the values to result streams */                                               \
+    for (int64_t batch = 0; batch < num_batch; batch++) {                                \
+      for (int i = 0; i < kSseBatchStreams; i++) {                                       \
+        const auto src_simd =                                                            \
+            SimdLoadFn(&values[batch * kSseBatchSize + kSseStreamSize * i]);             \
+        results_simd[i] = SimdAddFn(src_simd, results_simd[i]);                          \
+      }                                                                                  \
+    }                                                                                    \

Review comment:
       Thanks, the vectorize code by clang is really good. But seems the GCC part only use zmm0 which may limit the performance. This PR only aims on dense part, the final target is expanding the support to sparse part also, do you know if sparse can applied similar approach also? Example scalar code like below
   
     inline T MaskedValue(bool valid, T value) const { return valid ? value : 0; }
         for (size_t i = 0; i < 8; i++) {
           local.sum += MaskedValue(bits & (1U << i), values[i]);
         }
         local.count += BitUtil::kBytePopcount[bits];




----------------------------------------------------------------
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.

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