Skip to content

Commit

Permalink
PWR037: Add benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrogd committed Dec 19, 2024
1 parent 28e4f4a commit f021e8c
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions Benchmark/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
add_benchmark(PWR003)
add_benchmark(PWR022)
add_benchmark(PWR032)
add_benchmark(PWR037)
add_benchmark(PWR039)
add_benchmark(PWR043)
add_benchmark(PWR062)
Expand Down
54 changes: 54 additions & 0 deletions Benchmark/src/PWR037.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "Benchmark.h"

// Forward-declare the functions to benchmark
extern "C" {
void compute_damped_sinusoid(const int n, const double *timesteps,
const double amplitude,
const double angularFrequency,
const double decayRate, const double phaseShift,
double *results);
void compute_damped_sinusoid_improved(const int n, const double *timesteps,
const double amplitude,
const double angularFrequency,
const double decayRate,
const double phaseShift, double *results);
}

// Size adjusted to fit execution on micro-seconds
constexpr int N = 1024 * 1024;
constexpr double AMPLITUDE = 1.0;
constexpr double ANGULAR_FREQUENCY = 2.0;
constexpr double DECAY_RATE = 0.5;
constexpr double PHASE_SHIFT = 3.141592653589793 / 4;

#if OCB_ENABLE_C

static void CExampleBench(benchmark::State &state) {
const auto timesteps = OpenCatalog::CreateRandomVector<double>(N, 0.0, 10.0);
auto results = OpenCatalog::CreateUninitializedVector<double>(N);

for (auto _ : state) {
compute_damped_sinusoid(N, timesteps.data(), AMPLITUDE, ANGULAR_FREQUENCY,
DECAY_RATE, PHASE_SHIFT, results.data());
benchmark::DoNotOptimize(results);
}
}

static void CImprovedBench(benchmark::State &state) {
const auto timesteps = OpenCatalog::CreateRandomVector<double>(N, 0.0, 10.0);
auto results = OpenCatalog::CreateUninitializedVector<double>(N);

for (auto _ : state) {
compute_damped_sinusoid_improved(N, timesteps.data(), AMPLITUDE,
ANGULAR_FREQUENCY, DECAY_RATE, PHASE_SHIFT,
results.data());
benchmark::DoNotOptimize(results);
}
}

// A performance impact is expected when performing higher-precision
// calculations
OC_BENCHMARK("PWR037 C Example", CExampleBench);
OC_BENCHMARK("PWR037 C Fixed", CImprovedBench);

#endif
17 changes: 17 additions & 0 deletions Checks/PWR037/benchmark/example.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// PWR037: Potential precision loss in call to mathematical function

#include <math.h>

// https://en.wikipedia.org/wiki/Damping#Damped_sine_wave
void compute_damped_sinusoid(const int n, const double *timesteps,
const double amplitude,
const double angularFrequency,
const double decayRate, const double phaseShift,
double *results) {

for (int i = 0; i < n; ++i) {
double exponentialDecay = expf(-decayRate * timesteps[i]);
double angle = angularFrequency * timesteps[i] + phaseShift;
results[i] = amplitude * exponentialDecay * cosf(angle);
}
}
18 changes: 18 additions & 0 deletions Checks/PWR037/benchmark/solution.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// PWR037: Potential precision loss in call to mathematical function

#include <math.h>

// https://en.wikipedia.org/wiki/Damping#Damped_sine_wave
void compute_damped_sinusoid_improved(const int n, const double *timesteps,
const double amplitude,
const double angularFrequency,
const double decayRate,
const double phaseShift,
double *results) {

for (int i = 0; i < n; ++i) {
double exponentialDecay = exp(-decayRate * timesteps[i]);
double angle = angularFrequency * timesteps[i] + phaseShift;
results[i] = amplitude * exponentialDecay * cos(angle);
}
}

0 comments on commit f021e8c

Please sign in to comment.