forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apacheGH-15200: [C++] Created benchmarks for round kernels. (apache#1…
…5201) The four existing kernel functions Ceil, Floor, Round, and Trunc gain benchmarks with this change. * Closes: apache#15200 Lead-authored-by: David Sisson <EpsilonPrime@users.noreply.github.com> Co-authored-by: Will Jones <willjones127@gmail.com> Signed-off-by: Yibo Cai <yibo.cai@arm.com>
- Loading branch information
1 parent
33d677c
commit 85b167c
Showing
3 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
cpp/src/arrow/compute/kernels/scalar_round_benchmark.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |