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



##########
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:
       You can use GCC pragmas to force more unrolling, but AVX512 
vectorization gets you a ways despite the adder instruction latency. (My 
experience has been that GCC does an excellent job with intrinsics, often 
better than ICC, and tends to favor smaller code at comparable optimization 
levels. Clang tends to be more aggressive than GCC at unrolling, though I've 
often seen that not pay off.)
   
   The masked case is harder and I'm not aware of an idiomatic way to get GCC 
to vectorize it. We get some masked vectorized code with clang, though it isn't 
as clean as I'd hope for.
   ```c
   double sum1(int64_t n, const unsigned char *mask, const double *values) {
       double sum = 0;
       #pragma omp simd reduction(+:sum) collapse(2)
       for (int64_t i = 0; i < n/8; i++) {
           for (int64_t j = 0; j < 8; j++) {
               sum += mask[i] & (1 << j) ? values[i*8+j] : 0;
           }
       }
       return sum;
   }
   ```
   
   https://gcc.godbolt.org/z/84anmr




----------------------------------------------------------------
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:
us...@infra.apache.org


Reply via email to