Improved ArrayExpr (#378)
Build on RHEL9 / build (push) Successful in 3m9s
Build on RHEL8 / build (push) Successful in 3m34s
Run tests using data on local RHEL8 / build (push) Successful in 4m25s
Build on local RHEL8 / build (push) Successful in 3m5s

- removes code repetition
- avoid temporaries for scalar operations
This commit is contained in:
Erik Fröjdh
2026-09-25 10:43:58 +02:00
committed by GitHub
parent 4677e1003e
commit 8579a140f7
6 changed files with 290 additions and 167 deletions
+76
View File
@@ -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();