mirror of
https://github.com/slsdetectorgroup/aare.git
synced 2026-10-18 04:35:12 +02:00
Improved ArrayExpr (#378)
- removes code repetition - avoid temporaries for scalar operations
This commit is contained in:
@@ -130,4 +130,80 @@ BENCHMARK_F(TwoArrays, MultiplyAddDivideWithIndex)(benchmark::State &st) {
|
||||
}
|
||||
}
|
||||
|
||||
BENCHMARK_F(TwoArrays, ScalarMultiplyAddWithOperator)(benchmark::State &st) {
|
||||
for (auto _ : st) {
|
||||
// This code gets timed
|
||||
NDArray<int, 2> res = a * 2 + b;
|
||||
benchmark::DoNotOptimize(res);
|
||||
}
|
||||
}
|
||||
BENCHMARK_F(TwoArrays, ScalarMultiplyAddWithIndex)(benchmark::State &st) {
|
||||
for (auto _ : st) {
|
||||
// This code gets timed
|
||||
NDArray<int, 2> res(a.shape());
|
||||
for (uint32_t i = 0; i < a.size(); i++) {
|
||||
res(i) = a(i) * 2 + b(i);
|
||||
}
|
||||
benchmark::DoNotOptimize(res);
|
||||
}
|
||||
}
|
||||
|
||||
BENCHMARK_F(TwoArrays, AddToExistingWithOperator)(benchmark::State &st) {
|
||||
NDArray<int, 2> res(a.shape());
|
||||
for (auto _ : st) {
|
||||
// This code gets timed
|
||||
res = a + b;
|
||||
benchmark::DoNotOptimize(res);
|
||||
}
|
||||
}
|
||||
BENCHMARK_F(TwoArrays, AddToExistingWithIndex)(benchmark::State &st) {
|
||||
NDArray<int, 2> res(a.shape());
|
||||
for (auto _ : st) {
|
||||
// This code gets timed
|
||||
for (uint32_t i = 0; i < a.size(); i++) {
|
||||
res(i) = a(i) + b(i);
|
||||
}
|
||||
benchmark::DoNotOptimize(res);
|
||||
}
|
||||
}
|
||||
|
||||
// A store to uint8_t or a 64 bit integer can alias the members of NDArray.
|
||||
// Compare with a loop over raw pointers to check that this does not block
|
||||
// vectorization.
|
||||
template <typename T> void InPlaceAddWithOperator(benchmark::State &st) {
|
||||
NDArray<T, 2> a({size, size}, 1);
|
||||
NDArray<T, 2> b({size, size}, 2);
|
||||
for (auto _ : st) {
|
||||
a += b;
|
||||
benchmark::DoNotOptimize(a);
|
||||
}
|
||||
}
|
||||
template <typename T> void InPlaceAddWithPointer(benchmark::State &st) {
|
||||
NDArray<T, 2> a({size, size}, 1);
|
||||
NDArray<T, 2> b({size, size}, 2);
|
||||
for (auto _ : st) {
|
||||
T *dst = a.data();
|
||||
const T *src = b.data();
|
||||
for (ssize_t i = 0, n = a.size(); i < n; ++i) {
|
||||
dst[i] += src[i];
|
||||
}
|
||||
benchmark::DoNotOptimize(a);
|
||||
}
|
||||
}
|
||||
template <typename T> void AddToExistingWithOperator(benchmark::State &st) {
|
||||
NDArray<T, 2> a({size, size}, 1);
|
||||
NDArray<T, 2> b({size, size}, 2);
|
||||
NDArray<T, 2> res({size, size}, 0);
|
||||
for (auto _ : st) {
|
||||
res = a + b;
|
||||
benchmark::DoNotOptimize(res);
|
||||
}
|
||||
}
|
||||
BENCHMARK_TEMPLATE(InPlaceAddWithOperator, uint8_t);
|
||||
BENCHMARK_TEMPLATE(InPlaceAddWithPointer, uint8_t);
|
||||
BENCHMARK_TEMPLATE(AddToExistingWithOperator, uint8_t);
|
||||
BENCHMARK_TEMPLATE(InPlaceAddWithOperator, int64_t);
|
||||
BENCHMARK_TEMPLATE(InPlaceAddWithPointer, int64_t);
|
||||
BENCHMARK_TEMPLATE(AddToExistingWithOperator, int64_t);
|
||||
|
||||
BENCHMARK_MAIN();
|
||||
Reference in New Issue
Block a user