Skip to content

Commit

Permalink
Add benchmark for comparing vector mode with existing methods
Browse files Browse the repository at this point in the history
  • Loading branch information
vaithak authored and vgvassilev committed Aug 30, 2023
1 parent bba8cb1 commit 59fd672
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
8 changes: 8 additions & 0 deletions benchmark/BenchmarkedFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,11 @@ inline double product(double p[], int n) {
}
return prod;
}

///\returns the weighted sum of the elements in \p
inline double weightedSum(double p[], double w[], int n) {
double sum = 0;
for (int i = 0; i < n; i++)
sum += p[i] * w[i];
return sum;
}
1 change: 1 addition & 0 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CB_ADD_GBENCHMARK(Simple Simple.cpp)
CB_ADD_GBENCHMARK(AlgorithmicComplexity AlgorithmicComplexity.cpp)
CB_ADD_GBENCHMARK(EnzymeCladComparison EnzymeCladComparison.cpp)
CB_ADD_GBENCHMARK(MemoryComplexity MemoryComplexity.cpp)
CB_ADD_GBENCHMARK(VectorModeComparison VectorModeComparison.cpp)

set (CLAD_BENCHMARK_DEPS clad)
get_property(_benchmark_names DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY TESTS)
Expand Down
122 changes: 122 additions & 0 deletions benchmark/VectorModeComparison.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#include "benchmark/benchmark.h"

#include "clad/Differentiator/Differentiator.h"

#include "BenchmarkedFunctions.h"

// Benchmark forward mode for weighted sum.
static void BM_ForwardModeWeightedSum(benchmark::State& state) {
auto dp0 = clad::differentiate(weightedSum, "p[0]");
auto dp1 = clad::differentiate(weightedSum, "p[1]");
auto dp2 = clad::differentiate(weightedSum, "p[2]");
auto dp3 = clad::differentiate(weightedSum, "p[3]");
auto dp4 = clad::differentiate(weightedSum, "p[4]");

auto dw0 = clad::differentiate(weightedSum, "w[0]");
auto dw1 = clad::differentiate(weightedSum, "w[1]");
auto dw2 = clad::differentiate(weightedSum, "w[2]");
auto dw3 = clad::differentiate(weightedSum, "w[3]");
auto dw4 = clad::differentiate(weightedSum, "w[4]");

constexpr int n = 5;
double inputs[n];
double weights[n];
for (int i = 0; i < n; ++i) {
inputs[i] = i + 1;
weights[i] = 1.0 / (double)(i + 1);
}

double sum = 0;
for (auto _ : state) {
benchmark::DoNotOptimize(
sum +=
dp0.execute(inputs, weights, n) + dp1.execute(inputs, weights, n) +
dp2.execute(inputs, weights, n) + dp3.execute(inputs, weights, n) +
dp4.execute(inputs, weights, n) + dw0.execute(inputs, weights, n) +
dw1.execute(inputs, weights, n) + dw2.execute(inputs, weights, n) +
dw3.execute(inputs, weights, n) + dw4.execute(inputs, weights, n));
}
}
BENCHMARK(BM_ForwardModeWeightedSum);

// Benchmark reverse mode for weighted sum.
static void BM_ReverseModeWeightedSum(benchmark::State& state) {
auto grad = clad::gradient(weightedSum, "p, w");
constexpr int n = 5;

double inputs[n];
double weights[n];
for (int i = 0; i < n; ++i) {
inputs[i] = i + 1;
weights[i] = 1.0 / (double)(i + 1);
}

double dinp[n];
double dweights[n];
clad::array_ref<double> dinp_ref(dinp, n);
clad::array_ref<double> dweights_ref(dweights, n);

double sum = 0;
for (auto _ : state) {
grad.execute(inputs, weights, n, dinp_ref, dweights_ref);
for (int i = 0; i < n; ++i)
sum += dinp[i] + dweights[i];
}
}
BENCHMARK(BM_ReverseModeWeightedSum);

// Benchmark enzyme's reverse mode for weighted sum.
static void BM_EnzymeReverseModeWeightedSum(benchmark::State& state) {
auto grad = clad::gradient<clad::opts::use_enzyme>(weightedSum, "p, w");
constexpr int n = 5;

double inputs[n];
double weights[n];
for (int i = 0; i < n; ++i) {
inputs[i] = i + 1;
weights[i] = 1.0 / (double)(i + 1);
}

double dinp[n];
double dweights[n];
clad::array_ref<double> dinp_ref(dinp, n);
clad::array_ref<double> dweights_ref(dweights, n);

double sum = 0;
for (auto _ : state) {
grad.execute(inputs, weights, n, dinp_ref, dweights_ref);
for (int i = 0; i < n; ++i)
sum += dinp[i] + dweights[i];
}
}
BENCHMARK(BM_EnzymeReverseModeWeightedSum);

// Benchmark vector forward mode for weighted sum.
static void BM_VectorForwardModeWeightedSum(benchmark::State& state) {
auto vm_grad =
clad::differentiate<clad::opts::vector_mode>(weightedSum, "p, w");
constexpr int n = 5;

double inputs[n];
double weights[n];
for (int i = 0; i < n; ++i) {
inputs[i] = i + 1;
weights[i] = 1.0 / (double)(i + 1);
}

double dinp[n];
double dweights[n];
clad::array_ref<double> dinp_ref(dinp, n);
clad::array_ref<double> dweights_ref(dweights, n);

double sum = 0;
for (auto _ : state) {
vm_grad.execute(inputs, weights, n, dinp_ref, dweights_ref);
for (int i = 0; i < n; ++i)
sum += dinp[i] + dweights[i];
}
}
BENCHMARK(BM_VectorForwardModeWeightedSum);

// Define our main.
BENCHMARK_MAIN();

0 comments on commit 59fd672

Please sign in to comment.