You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by we...@apache.org on 2020/06/15 02:15:03 UTC

[arrow] branch master updated: ARROW-9079: [C++] Write benchmark for arithmetic kernels

This is an automated email from the ASF dual-hosted git repository.

wesm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/master by this push:
     new 96279ee  ARROW-9079: [C++] Write benchmark for arithmetic kernels
96279ee is described below

commit 96279ee1d072d6dba069821176da015e71bd3788
Author: Krisztián Szűcs <sz...@gmail.com>
AuthorDate: Sun Jun 14 21:14:37 2020 -0500

    ARROW-9079: [C++] Write benchmark for arithmetic kernels
    
    Quickly wanted to add a benchmark for the `Add` function to verify that no significant regressions were introduced by https://github.com/apache/arrow/pull/7341
    
    Before:
    ```
    ---------------------------------------------------------------------------------------
    Benchmark                                Time           CPU Iterations UserCounters...
    ---------------------------------------------------------------------------------------
    AddArrayArrayKernel/32768/10000         18 us         18 us      35892 null_percent=0.01 size=32.768k   1.67854GB/s
    AddArrayArrayKernel/32768/100           19 us         19 us      37540 null_percent=1 size=32.768k   1.61941GB/s
    AddArrayArrayKernel/32768/10            20 us         20 us      37049 null_percent=10 size=32.768k   1.55599GB/s
    AddArrayArrayKernel/32768/2             20 us         20 us      35394 null_percent=50 size=32.768k   1.54512GB/s
    AddArrayArrayKernel/32768/1             19 us         19 us      37901 null_percent=100 size=32.768k   1.63153GB/s
    ```
    
    After:
    ```
    ---------------------------------------------------------------------------------------
    Benchmark                                Time           CPU Iterations UserCounters...
    ---------------------------------------------------------------------------------------
    AddArrayArrayKernel/32768/10000         19 us         19 us      36704 null_percent=0.01 size=32.768k   1.64619GB/s
    AddArrayArrayKernel/32768/100           18 us         18 us      37194 null_percent=1 size=32.768k   1.67588GB/s
    AddArrayArrayKernel/32768/10            18 us         18 us      36341 null_percent=10 size=32.768k   1.65205GB/s
    AddArrayArrayKernel/32768/2             18 us         18 us      37502 null_percent=50 size=32.768k     1.662GB/s
    AddArrayArrayKernel/32768/1             18 us         18 us      38622 null_percent=100 size=32.768k   1.66593GB/s
    ```
    
    cc @wesm
    
    Closes #7417 from kszucs/ARROW-9079
    
    Lead-authored-by: Krisztián Szűcs <sz...@gmail.com>
    Co-authored-by: Benjamin Kietzman <be...@gmail.com>
    Co-authored-by: Wes McKinney <we...@apache.org>
    Signed-off-by: Wes McKinney <we...@apache.org>
---
 cpp/src/arrow/compute/kernels/CMakeLists.txt       |   1 +
 cpp/src/arrow/compute/kernels/scalar_arithmetic.cc |   2 -
 .../compute/kernels/scalar_arithmetic_benchmark.cc | 102 +++++++++++++++++++++
 3 files changed, 103 insertions(+), 2 deletions(-)

diff --git a/cpp/src/arrow/compute/kernels/CMakeLists.txt b/cpp/src/arrow/compute/kernels/CMakeLists.txt
index 93b03e7..e3fa987 100644
--- a/cpp/src/arrow/compute/kernels/CMakeLists.txt
+++ b/cpp/src/arrow/compute/kernels/CMakeLists.txt
@@ -28,6 +28,7 @@ add_arrow_compute_test(scalar_test
                        scalar_string_test.cc
                        test_util.cc)
 
+add_arrow_benchmark(scalar_arithmetic_benchmark PREFIX "arrow-compute")
 add_arrow_benchmark(scalar_compare_benchmark PREFIX "arrow-compute")
 add_arrow_benchmark(scalar_string_benchmark PREFIX "arrow-compute")
 
diff --git a/cpp/src/arrow/compute/kernels/scalar_arithmetic.cc b/cpp/src/arrow/compute/kernels/scalar_arithmetic.cc
index de05900..4459e4f 100644
--- a/cpp/src/arrow/compute/kernels/scalar_arithmetic.cc
+++ b/cpp/src/arrow/compute/kernels/scalar_arithmetic.cc
@@ -83,9 +83,7 @@ struct Multiply {
   static_assert(std::is_same<decltype(int16_t() * int16_t()), int32_t>::value, "");
   static_assert(std::is_same<decltype(uint16_t() * uint16_t()), int32_t>::value, "");
   static_assert(std::is_same<decltype(int32_t() * int32_t()), int32_t>::value, "");
-
   static_assert(std::is_same<decltype(uint32_t() * uint32_t()), uint32_t>::value, "");
-
   static_assert(std::is_same<decltype(int64_t() * int64_t()), int64_t>::value, "");
   static_assert(std::is_same<decltype(uint64_t() * uint64_t()), uint64_t>::value, "");
 
diff --git a/cpp/src/arrow/compute/kernels/scalar_arithmetic_benchmark.cc b/cpp/src/arrow/compute/kernels/scalar_arithmetic_benchmark.cc
new file mode 100644
index 0000000..34991b9
--- /dev/null
+++ b/cpp/src/arrow/compute/kernels/scalar_arithmetic_benchmark.cc
@@ -0,0 +1,102 @@
+// 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 "benchmark/benchmark.h"
+
+#include <vector>
+
+#include "arrow/compute/api_scalar.h"
+#include "arrow/compute/benchmark_util.h"
+#include "arrow/compute/kernels/test_util.h"
+#include "arrow/testing/gtest_util.h"
+#include "arrow/testing/random.h"
+
+namespace arrow {
+namespace compute {
+
+constexpr auto kSeed = 0x94378165;
+
+using BinaryOp = Result<Datum>(const Datum&, const Datum&, ExecContext*);
+
+template <BinaryOp& Op, typename ArrowType, typename CType = typename ArrowType::c_type>
+static void ArrayScalarKernel(benchmark::State& state) {
+  RegressionArgs args(state);
+
+  const int64_t array_size = args.size / sizeof(CType);
+  auto min = std::numeric_limits<CType>::lowest();
+  auto max = std::numeric_limits<CType>::max();
+
+  auto rand = random::RandomArrayGenerator(kSeed);
+  auto lhs = std::static_pointer_cast<NumericArray<ArrowType>>(
+      rand.Numeric<ArrowType>(array_size, min, max, args.null_proportion));
+
+  Datum fifteen(CType(15));
+  for (auto _ : state) {
+    ABORT_NOT_OK(Op(lhs, fifteen, nullptr).status());
+  }
+  state.SetItemsProcessed(state.iterations() * array_size);
+}
+
+template <BinaryOp& Op, typename ArrowType, typename CType = typename ArrowType::c_type>
+static void ArrayArrayKernel(benchmark::State& state) {
+  RegressionArgs args(state);
+
+  const int64_t array_size = args.size / sizeof(CType);
+  auto min = std::numeric_limits<CType>::lowest();
+  auto max = std::numeric_limits<CType>::max();
+
+  auto rand = random::RandomArrayGenerator(kSeed);
+  auto lhs = std::static_pointer_cast<NumericArray<ArrowType>>(
+      rand.Numeric<ArrowType>(array_size, min, max, args.null_proportion));
+  auto rhs = std::static_pointer_cast<NumericArray<ArrowType>>(
+      rand.Numeric<ArrowType>(array_size, min, max, args.null_proportion));
+
+  for (auto _ : state) {
+    ABORT_NOT_OK(Op(lhs, rhs, nullptr).status());
+  }
+  state.SetItemsProcessed(state.iterations() * array_size);
+}
+
+void SetArgs(benchmark::internal::Benchmark* bench) {
+  for (const auto size : {kL1Size, kL2Size}) {
+    for (const auto inverse_null_proportion : std::vector<ArgsType>({100, 0})) {
+      bench->Args({static_cast<ArgsType>(size), inverse_null_proportion});
+    }
+  }
+}
+
+#define DECLARE_ARITHMETIC_BENCHMARKS(BENCHMARK, OP)             \
+  BENCHMARK_TEMPLATE(BENCHMARK, OP, Int64Type)->Apply(SetArgs);  \
+  BENCHMARK_TEMPLATE(BENCHMARK, OP, Int32Type)->Apply(SetArgs);  \
+  BENCHMARK_TEMPLATE(BENCHMARK, OP, Int16Type)->Apply(SetArgs);  \
+  BENCHMARK_TEMPLATE(BENCHMARK, OP, Int8Type)->Apply(SetArgs);   \
+  BENCHMARK_TEMPLATE(BENCHMARK, OP, UInt64Type)->Apply(SetArgs); \
+  BENCHMARK_TEMPLATE(BENCHMARK, OP, UInt32Type)->Apply(SetArgs); \
+  BENCHMARK_TEMPLATE(BENCHMARK, OP, UInt16Type)->Apply(SetArgs); \
+  BENCHMARK_TEMPLATE(BENCHMARK, OP, UInt8Type)->Apply(SetArgs);  \
+  BENCHMARK_TEMPLATE(BENCHMARK, OP, FloatType)->Apply(SetArgs);  \
+  BENCHMARK_TEMPLATE(BENCHMARK, OP, DoubleType)->Apply(SetArgs)
+
+DECLARE_ARITHMETIC_BENCHMARKS(ArrayArrayKernel, Add);
+DECLARE_ARITHMETIC_BENCHMARKS(ArrayScalarKernel, Add);
+DECLARE_ARITHMETIC_BENCHMARKS(ArrayArrayKernel, Subtract);
+DECLARE_ARITHMETIC_BENCHMARKS(ArrayScalarKernel, Subtract);
+DECLARE_ARITHMETIC_BENCHMARKS(ArrayArrayKernel, Multiply);
+DECLARE_ARITHMETIC_BENCHMARKS(ArrayScalarKernel, Multiply);
+
+}  // namespace compute
+}  // namespace arrow