-
Notifications
You must be signed in to change notification settings - Fork 915
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add templated benchmark with fixture (#9838)
BENCHMARK_TEMPLATE_F is for non-templated benchmark with templated fixture. BENCHMARK_F is for non-templated benchmark with non-templated fixture. Google benchmark does not have support for templated benchmark function with non-templated fixture. Often, BENCHMARK_TEMPLATE_F is used as a proxy for templated benchmark. But templated fixture is not really required here. It also has limitation of specifying different name for each template. So, this PR extends google benchmark to support templated benchmark with non-templated fixture. - [x] Use TEMPLATED_BENCHMARK_F in compiled binary op. - [x] Use in other relevant benchmarks as well. Usage: `TEMPLATED_BENCHMARK_F(FixtureClass, TemplateFunction, ...);` `...` are template arguments Example: ``` class FixtureClass : public cudf::benchmark { }; template<typename T, typename U> void TemplateFunction(benchmark::State& state) { for (auto _ : state) { // benchmark stuff } } TEMPLATED_BENCHMARK_F(FixtureClass, TemplateFunction, int, double)->Range(128, 512); ``` Authors: - Karthikeyan (https://github.com/karthikeyann) Approvers: - Devavret Makkar (https://github.com/devavret) - Mark Harris (https://github.com/harrism) URL: #9838
- Loading branch information
1 parent
4579d23
commit e6b0661
Showing
5 changed files
with
171 additions
and
135 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright (c) 2021, NVIDIA CORPORATION. | ||
* | ||
* Licensed 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. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <benchmark/benchmark.h> | ||
|
||
namespace cudf { | ||
/** | ||
* @brief Templated Google Benchmark with fixture | ||
* | ||
* Extends Google benchmarks to support templated Benchmarks with non-templated fixture class. | ||
* | ||
* The SetUp and TearDown methods is called before each templated benchmark function is run. | ||
* These methods are called automatically by Google Benchmark | ||
* | ||
* Example: | ||
* | ||
* @code | ||
* template <class T, class U> | ||
* void my_benchmark(::benchmark::State& state) { | ||
* std::vector<T> v1(state.range(0)); | ||
* std::vector<U> v2(state.range(0)); | ||
* for (auto _ : state) { | ||
* // benchmark stuff | ||
* } | ||
* } | ||
* | ||
* TEMPLATED_BENCHMARK_F(cudf::benchmark, my_benchmark, int, double)->Range(128, 512); | ||
* @endcode | ||
*/ | ||
template <class Fixture> | ||
class FunctionTemplateBenchmark : public Fixture { | ||
public: | ||
FunctionTemplateBenchmark(const char* name, ::benchmark::internal::Function* func) | ||
: Fixture(), func_(func) | ||
{ | ||
this->SetName(name); | ||
} | ||
|
||
virtual void Run(::benchmark::State& st) | ||
{ | ||
this->SetUp(st); | ||
this->BenchmarkCase(st); | ||
this->TearDown(st); | ||
} | ||
|
||
private: | ||
::benchmark::internal::Function* func_; | ||
|
||
protected: | ||
virtual void BenchmarkCase(::benchmark::State& st) { func_(st); } | ||
}; | ||
|
||
#define TEMPLATED_BENCHMARK_F(BaseClass, n, ...) \ | ||
BENCHMARK_PRIVATE_DECLARE(n) = (::benchmark::internal::RegisterBenchmarkInternal( \ | ||
new cudf::FunctionTemplateBenchmark<BaseClass>(#BaseClass "/" #n "<" #__VA_ARGS__ ">", \ | ||
n<__VA_ARGS__>))) | ||
|
||
} // namespace cudf |
Oops, something went wrong.