|
| 1 | +#include "Benchmark.h" |
| 2 | + |
| 3 | +// Forward-declare the functions to benchmark |
| 4 | +extern "C" { |
| 5 | +double transform_and_sum(const int n, const double *array, const int option); |
| 6 | +double transform_and_sum_improved(const int n, const double *array, |
| 7 | + const int option); |
| 8 | +double transform_and_sum_f(const int n, const double *array, const int option); |
| 9 | +double transform_and_sum_improved_f(const int n, const double *array, |
| 10 | + const int option); |
| 11 | +} |
| 12 | + |
| 13 | +// Size adjusted to fit execution on micro-seconds |
| 14 | +constexpr int N = 1024 * 1024; |
| 15 | + |
| 16 | +#if OCB_ENABLE_C |
| 17 | + |
| 18 | +static void CExampleBench(benchmark::State &state) { |
| 19 | + auto array = OpenCatalog::CreateRandomVector<double>(N); |
| 20 | + |
| 21 | + for (auto _ : state) { |
| 22 | + double result = transform_and_sum(N, array.data(), /*option=*/-1); |
| 23 | + benchmark::DoNotOptimize(result); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +static void CImprovedBench(benchmark::State &state) { |
| 28 | + auto array = OpenCatalog::CreateRandomVector<double>(N); |
| 29 | + |
| 30 | + for (auto _ : state) { |
| 31 | + double result = transform_and_sum_improved(N, array.data(), /*option=*/-1); |
| 32 | + benchmark::DoNotOptimize(result); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// The goal of these benchmarks is to demonstrate that the suggested check does |
| 37 | +// not incur any performance penalty |
| 38 | +OC_BENCHMARK("PWR080 C Example", CExampleBench); |
| 39 | +OC_BENCHMARK("PWR080 C Improved", CImprovedBench); |
| 40 | + |
| 41 | +#endif |
| 42 | + |
| 43 | +#if OCB_ENABLE_Fortran |
| 44 | + |
| 45 | +static void FortranExampleBench(benchmark::State &state) { |
| 46 | + auto array = OpenCatalog::CreateRandomVector<double>(N); |
| 47 | + |
| 48 | + for (auto _ : state) { |
| 49 | + double result = transform_and_sum_f(N, array.data(), /*option=*/-1); |
| 50 | + benchmark::DoNotOptimize(result); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +static void FortranImprovedBench(benchmark::State &state) { |
| 55 | + auto array = OpenCatalog::CreateRandomVector<double>(N); |
| 56 | + |
| 57 | + for (auto _ : state) { |
| 58 | + double result = |
| 59 | + transform_and_sum_improved_f(N, array.data(), /*option=*/-1); |
| 60 | + benchmark::DoNotOptimize(result); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// The goal of these benchmarks is to demonstrate that the suggested check does |
| 65 | +// not incur any performance penalty |
| 66 | +OC_BENCHMARK("PWR080 Fortran Example", FortranExampleBench); |
| 67 | +OC_BENCHMARK("PWR080 Fortran Improved", FortranImprovedBench); |
| 68 | + |
| 69 | +#endif |
0 commit comments