Skip to content

Commit 67b9938

Browse files
committed
Benchmark Eigen tensor expressions
1 parent f15367d commit 67b9938

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

‎benchmark/benchmark_compare.cpp‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#ifdef XTENSOR_BENCHMARK_USE_EIGEN
2222
#include <Eigen/Core>
23+
#include <unsupported/Eigen/CXX11/Tensor>
2324
#endif
2425

2526
#ifdef XTENSOR_BENCHMARK_USE_ARMADILLO
@@ -188,6 +189,41 @@ namespace xt::compare
188189
benchmark::ClobberMemory();
189190
}
190191
}
192+
193+
void broadcast_dynamic_eigen(benchmark::State& state)
194+
{
195+
using tensor = Eigen::Tensor<double, 3, Eigen::RowMajor>;
196+
tensor x(64, 64, 16), out(64, 64, 16);
197+
Eigen::Tensor<double, 1, Eigen::RowMajor> row(16), column(64);
198+
for (Eigen::Index i = 0; i < x.size(); ++i) x.data()[i] = 0.5 + double(i % 251) * 0.01;
199+
for (Eigen::Index i = 0; i < row.size(); ++i) row(i) = double(i) * 0.25;
200+
for (Eigen::Index i = 0; i < column.size(); ++i) column(i) = double(i) * 0.125;
201+
const Eigen::array<Eigen::Index, 3> row_shape = {1, 1, 16};
202+
const Eigen::array<Eigen::Index, 3> row_broadcast = {64, 64, 1};
203+
const Eigen::array<Eigen::Index, 3> column_shape = {64, 1, 1};
204+
const Eigen::array<Eigen::Index, 3> column_broadcast = {1, 64, 16};
205+
for (auto _ : state)
206+
{
207+
out = (x + row.reshape(row_shape).broadcast(row_broadcast)) * 1.5
208+
- column.reshape(column_shape).broadcast(column_broadcast);
209+
benchmark::DoNotOptimize(out.data());
210+
benchmark::ClobberMemory();
211+
}
212+
}
213+
214+
void transpose_cast_eigen(benchmark::State& state)
215+
{
216+
Eigen::Tensor<unsigned char, 3, Eigen::RowMajor> input(128, 256, 3);
217+
Eigen::Tensor<float, 3, Eigen::RowMajor> out(3, 128, 256);
218+
for (Eigen::Index i = 0; i < input.size(); ++i) input.data()[i] = static_cast<unsigned char>(i % 251);
219+
const Eigen::array<Eigen::Index, 3> permutation = {2, 0, 1};
220+
for (auto _ : state)
221+
{
222+
out = input.shuffle(permutation).cast<float>() / 255.0f;
223+
benchmark::DoNotOptimize(out.data());
224+
benchmark::ClobberMemory();
225+
}
226+
}
191227
#endif
192228

193229
#ifdef XTENSOR_BENCHMARK_USE_ARMADILLO
@@ -226,6 +262,8 @@ namespace xt::compare
226262
#ifdef XTENSOR_BENCHMARK_USE_EIGEN
227263
BENCHMARK(linear_dynamic_eigen);
228264
BENCHMARK(linear_fixed_eigen);
265+
BENCHMARK(broadcast_dynamic_eigen);
266+
BENCHMARK(transpose_cast_eigen);
229267
#endif
230268
#ifdef XTENSOR_BENCHMARK_USE_ARMADILLO
231269
BENCHMARK(linear_dynamic_armadillo);

0 commit comments

Comments
 (0)