-
Notifications
You must be signed in to change notification settings - Fork 123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add benchmark for expression templates #634
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
#include "benchmark/benchmark.h" | ||
|
||
#include "clad/Differentiator/Differentiator.h" | ||
|
||
// Benchmark the expression x*y + y*z + z*x between clad arrays, | ||
// this is to compare the performance of expression templates. | ||
// We will evaluate the expression on using four different methods: | ||
// 1. Using operations on clad arrays - this will use expression templates. | ||
// 2. Using clad arrays but creating temporaries manually. | ||
// 3. Using loops on clad arrays. | ||
// 4. Using loops on native arrays. | ||
|
||
// Benchmark expression templates. | ||
static void BM_ExpressionTemplates(benchmark::State& state) { | ||
constexpr int n = 1000; | ||
clad::array<double> x(n); | ||
clad::array<double> y(n); | ||
clad::array<double> z(n); | ||
for (int i = 0; i < n; ++i) { | ||
x[i] = i + 1; | ||
y[i] = i + 2; | ||
z[i] = i + 3; | ||
} | ||
|
||
clad::array<double> res(n); | ||
for (auto _ : state) | ||
benchmark::DoNotOptimize(res = x * y + y * z + z * x); | ||
} | ||
BENCHMARK(BM_ExpressionTemplates); | ||
|
||
// Benchmark manually creating temporaries. | ||
static void BM_ManualTemporaries(benchmark::State& state) { | ||
constexpr int n = 1000; | ||
clad::array<double> x(n); | ||
clad::array<double> y(n); | ||
clad::array<double> z(n); | ||
for (int i = 0; i < n; ++i) { | ||
x[i] = i + 1; | ||
y[i] = i + 2; | ||
z[i] = i + 3; | ||
} | ||
|
||
clad::array<double> res(n); | ||
for (auto _ : state) { | ||
clad::array<double> temp1 = x * y; | ||
clad::array<double> temp2 = y * z; | ||
clad::array<double> temp3 = z * x; | ||
clad::array<double> temp4 = temp1 + temp2; | ||
benchmark::DoNotOptimize(res = temp4 + temp3); | ||
} | ||
} | ||
BENCHMARK(BM_ManualTemporaries); | ||
|
||
// Benchmark loops on clad arrays. | ||
static void BM_LoopsOnCladArrays(benchmark::State& state) { | ||
constexpr int n = 1000; | ||
clad::array<double> x(n); | ||
clad::array<double> y(n); | ||
clad::array<double> z(n); | ||
for (int i = 0; i < n; ++i) { | ||
x[i] = i + 1; | ||
y[i] = i + 2; | ||
z[i] = i + 3; | ||
} | ||
|
||
clad::array<double> res(n); | ||
for (auto _ : state) { | ||
for (int i = 0; i < n; ++i) { | ||
benchmark::DoNotOptimize(res[i] = | ||
x[i] * y[i] + y[i] * z[i] + z[i] * x[i]); | ||
} | ||
} | ||
} | ||
BENCHMARK(BM_LoopsOnCladArrays); | ||
|
||
// Benchmark loops on native arrays. | ||
static void BM_LoopsOnNativeArrays(benchmark::State& state) { | ||
constexpr int n = 1000; | ||
double* x = new double[n]; | ||
double* y = new double[n]; | ||
double* z = new double[n]; | ||
for (int i = 0; i < n; ++i) { | ||
x[i] = i + 1; | ||
y[i] = i + 2; | ||
z[i] = i + 3; | ||
} | ||
|
||
double* res = new double[n]; | ||
for (auto _ : state) { | ||
for (int i = 0; i < n; ++i) { | ||
benchmark::DoNotOptimize(res[i] = | ||
x[i] * y[i] + y[i] * z[i] + z[i] * x[i]); | ||
} | ||
} | ||
|
||
delete[] x; | ||
delete[] y; | ||
delete[] z; | ||
delete[] res; | ||
} | ||
BENCHMARK(BM_LoopsOnNativeArrays); | ||
|
||
// Define our main. | ||
BENCHMARK_MAIN(); |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We spell const in clad this way. Can you update the rest of the occurrences?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed and also added to .clang-format
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice!