You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by yi...@apache.org on 2023/01/10 04:59:11 UTC

[arrow] branch master updated: GH-15200: [C++] Created benchmarks for round kernels. (#15201)

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

yibocai 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 85b167c05c GH-15200: [C++] Created benchmarks for round kernels. (#15201)
85b167c05c is described below

commit 85b167c05c2f93a95b23e8ac4fd4da576ea5b899
Author: David Sisson <Ep...@users.noreply.github.com>
AuthorDate: Mon Jan 9 20:59:02 2023 -0800

    GH-15200: [C++] Created benchmarks for round kernels. (#15201)
    
    The four existing kernel functions Ceil, Floor, Round, and Trunc gain benchmarks with this change.
    * Closes: #15200
    
    Lead-authored-by: David Sisson <Ep...@users.noreply.github.com>
    Co-authored-by: Will Jones <wi...@gmail.com>
    Signed-off-by: Yibo Cai <yi...@arm.com>
---
 cpp/src/arrow/compute/kernels/CMakeLists.txt       |   1 +
 .../compute/kernels/scalar_arithmetic_benchmark.cc |   2 +
 .../compute/kernels/scalar_round_benchmark.cc      | 120 +++++++++++++++++++++
 3 files changed, 123 insertions(+)

diff --git a/cpp/src/arrow/compute/kernels/CMakeLists.txt b/cpp/src/arrow/compute/kernels/CMakeLists.txt
index e2f869750d..5eadf5d0ea 100644
--- a/cpp/src/arrow/compute/kernels/CMakeLists.txt
+++ b/cpp/src/arrow/compute/kernels/CMakeLists.txt
@@ -40,6 +40,7 @@ add_arrow_benchmark(scalar_cast_benchmark PREFIX "arrow-compute")
 add_arrow_benchmark(scalar_compare_benchmark PREFIX "arrow-compute")
 add_arrow_benchmark(scalar_if_else_benchmark PREFIX "arrow-compute")
 add_arrow_benchmark(scalar_random_benchmark PREFIX "arrow-compute")
+add_arrow_benchmark(scalar_round_benchmark PREFIX "arrow-compute")
 add_arrow_benchmark(scalar_set_lookup_benchmark PREFIX "arrow-compute")
 add_arrow_benchmark(scalar_string_benchmark PREFIX "arrow-compute")
 add_arrow_benchmark(scalar_temporal_benchmark PREFIX "arrow-compute")
diff --git a/cpp/src/arrow/compute/kernels/scalar_arithmetic_benchmark.cc b/cpp/src/arrow/compute/kernels/scalar_arithmetic_benchmark.cc
index 01d9ec944e..4b678da5f1 100644
--- a/cpp/src/arrow/compute/kernels/scalar_arithmetic_benchmark.cc
+++ b/cpp/src/arrow/compute/kernels/scalar_arithmetic_benchmark.cc
@@ -107,6 +107,8 @@ static void ArrayArrayKernel(benchmark::State& state) {
 }
 
 void SetArgs(benchmark::internal::Benchmark* bench) {
+  bench->ArgNames({"size", "inverse_null_proportion"});
+
   for (const auto inverse_null_proportion : std::vector<ArgsType>({100, 0})) {
     bench->Args({static_cast<ArgsType>(kL2Size), inverse_null_proportion});
   }
diff --git a/cpp/src/arrow/compute/kernels/scalar_round_benchmark.cc b/cpp/src/arrow/compute/kernels/scalar_round_benchmark.cc
new file mode 100644
index 0000000000..dd9ba04a0e
--- /dev/null
+++ b/cpp/src/arrow/compute/kernels/scalar_round_benchmark.cc
@@ -0,0 +1,120 @@
+// 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/kernels/test_util.h"
+#include "arrow/testing/gtest_util.h"
+#include "arrow/testing/random.h"
+#include "arrow/util/benchmark_util.h"
+
+namespace arrow {
+namespace compute {
+
+// Use a fixed hash to ensure consistent results from run to run.
+constexpr auto kSeed = 0x94378165;
+
+template <typename ArrowType, RoundMode Mode, typename CType = typename ArrowType::c_type>
+static void RoundArrayBenchmark(benchmark::State& state, const std::string& func_name) {
+  RegressionArgs args(state);
+
+  const int64_t array_size = args.size / sizeof(CType);
+  auto rand = random::RandomArrayGenerator(kSeed);
+
+  // Choose values so as to avoid overflow on all ops and types.
+  auto min = static_cast<CType>(6);
+  auto max = static_cast<CType>(min + 15);
+  auto val = std::static_pointer_cast<NumericArray<ArrowType>>(
+      rand.Numeric<ArrowType>(array_size, min, max, args.null_proportion));
+  RoundOptions options;
+  options.round_mode = static_cast<RoundMode>(Mode);
+
+  for (auto _ : state) {
+    ABORT_NOT_OK(CallFunction(func_name, {val}, &options));
+  }
+  state.SetItemsProcessed(state.iterations() * array_size);
+}
+
+void SetRoundArgs(benchmark::internal::Benchmark* bench) {
+  bench->ArgNames({"size", "inverse_null_proportion"});
+
+  for (const auto inverse_null_proportion : std::vector<ArgsType>({100, 0})) {
+    bench->Args({static_cast<ArgsType>(kL2Size), inverse_null_proportion});
+  }
+}
+
+template <typename ArrowType, RoundMode Mode>
+static void Ceil(benchmark::State& state) {
+  RoundArrayBenchmark<ArrowType, Mode>(state, "ceil");
+}
+
+template <typename ArrowType, RoundMode Mode>
+static void Floor(benchmark::State& state) {
+  RoundArrayBenchmark<ArrowType, Mode>(state, "floor");
+}
+
+template <typename ArrowType, RoundMode Mode>
+static void Round(benchmark::State& state) {
+  RoundArrayBenchmark<ArrowType, Mode>(state, "round");
+}
+
+template <typename ArrowType, RoundMode Mode>
+static void Trunc(benchmark::State& state) {
+  RoundArrayBenchmark<ArrowType, Mode>(state, "trunc");
+}
+
+#ifdef ALL_ROUND_BENCHMARKS
+#define DECLARE_ROUND_BENCHMARKS_WITH_ROUNDMODE(OP, TYPE)                              \
+  BENCHMARK_TEMPLATE(OP, TYPE, RoundMode::DOWN)->Apply(SetRoundArgs);                  \
+  BENCHMARK_TEMPLATE(OP, TYPE, RoundMode::UP)->Apply(SetRoundArgs);                    \
+  BENCHMARK_TEMPLATE(OP, TYPE, RoundMode::TOWARDS_ZERO)->Apply(SetRoundArgs);          \
+  BENCHMARK_TEMPLATE(OP, TYPE, RoundMode::TOWARDS_INFINITY)->Apply(SetRoundArgs);      \
+  BENCHMARK_TEMPLATE(OP, TYPE, RoundMode::HALF_DOWN)->Apply(SetRoundArgs);             \
+  BENCHMARK_TEMPLATE(OP, TYPE, RoundMode::HALF_UP)->Apply(SetRoundArgs);               \
+  BENCHMARK_TEMPLATE(OP, TYPE, RoundMode::HALF_TOWARDS_ZERO)->Apply(SetRoundArgs);     \
+  BENCHMARK_TEMPLATE(OP, TYPE, RoundMode::HALF_TOWARDS_INFINITY)->Apply(SetRoundArgs); \
+  BENCHMARK_TEMPLATE(OP, TYPE, RoundMode::HALF_TO_EVEN)->Apply(SetRoundArgs);          \
+  BENCHMARK_TEMPLATE(OP, TYPE, RoundMode::HALF_TO_ODD)->Apply(SetRoundArgs)
+#else
+#define DECLARE_ROUND_BENCHMARKS_WITH_ROUNDMODE(OP, TYPE)                          \
+  BENCHMARK_TEMPLATE(OP, TYPE, RoundMode::DOWN)->Apply(SetRoundArgs);              \
+  BENCHMARK_TEMPLATE(OP, TYPE, RoundMode::HALF_TOWARDS_ZERO)->Apply(SetRoundArgs); \
+  BENCHMARK_TEMPLATE(OP, TYPE, RoundMode::HALF_TO_ODD)->Apply(SetRoundArgs)
+#endif
+
+#define DECLARE_ROUND_BENCHMARKS(OP)                       \
+  DECLARE_ROUND_BENCHMARKS_WITH_ROUNDMODE(OP, Int64Type);  \
+  DECLARE_ROUND_BENCHMARKS_WITH_ROUNDMODE(OP, Int32Type);  \
+  DECLARE_ROUND_BENCHMARKS_WITH_ROUNDMODE(OP, Int16Type);  \
+  DECLARE_ROUND_BENCHMARKS_WITH_ROUNDMODE(OP, Int8Type);   \
+  DECLARE_ROUND_BENCHMARKS_WITH_ROUNDMODE(OP, UInt64Type); \
+  DECLARE_ROUND_BENCHMARKS_WITH_ROUNDMODE(OP, UInt32Type); \
+  DECLARE_ROUND_BENCHMARKS_WITH_ROUNDMODE(OP, UInt16Type); \
+  DECLARE_ROUND_BENCHMARKS_WITH_ROUNDMODE(OP, UInt8Type);  \
+  DECLARE_ROUND_BENCHMARKS_WITH_ROUNDMODE(OP, FloatType);  \
+  DECLARE_ROUND_BENCHMARKS_WITH_ROUNDMODE(OP, DoubleType);
+
+DECLARE_ROUND_BENCHMARKS(Ceil);
+DECLARE_ROUND_BENCHMARKS(Floor);
+DECLARE_ROUND_BENCHMARKS(Round);
+DECLARE_ROUND_BENCHMARKS(Trunc);
+
+}  // namespace compute
+}  // namespace arrow