forked from xtensor-stack/xtensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_assign.cpp
227 lines (199 loc) · 7.98 KB
/
benchmark_assign.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/***************************************************************************
* Copyright (c) 2016, Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#ifndef BENCHMARK_ASSIGN_HPP
#define BENCHMARK_ASSIGN_HPP
#include <benchmark/benchmark.h>
#include "xtensor/xarray.hpp"
#include "xtensor/xnoalias.hpp"
#include "xtensor/xtensor.hpp"
namespace xt
{
namespace assign
{
/****************************
* Benchmark initialization *
****************************/
template <class V>
inline void init_benchmark_data(V& lhs, V& rhs, std::size_t size0, std::size_t size1)
{
using T = typename V::value_type;
for (std::size_t i = 0; i < size0; ++i)
{
for (std::size_t j = 0; j < size1; ++j)
{
lhs(i, j) = T(0.5) * T(j) / T(j + 1) + std::sqrt(T(i)) * T(9.) / T(size1);
rhs(i, j) = T(10.2) / T(i + 2) + T(0.25) * T(j);
}
}
}
template <class V>
inline void init_xtensor_benchmark(V& lhs, V& rhs, V& res, std::size_t size0, size_t size1)
{
lhs.resize({size0, size1});
rhs.resize({size0, size1});
res.resize({size0, size1});
init_benchmark_data(lhs, rhs, size0, size1);
}
template <class V>
inline void init_dl_xtensor_benchmark(V& lhs, V& rhs, V& res, std::size_t size0, size_t size1)
{
using strides_type = typename V::strides_type;
strides_type str = {size1, 1};
lhs.resize({size0, size1}, str);
rhs.resize({size0, size1}, str);
res.resize({size0, size1}, str);
init_benchmark_data(lhs, rhs, size0, size1);
}
template <class E>
inline auto assign_c_assign(benchmark::State& state)
{
using size_type = typename E::size_type;
E x, y, res;
init_xtensor_benchmark(x, y, res, state.range(0), state.range(0));
for (auto _ : state)
{
size_type csize = x.size();
for (size_type i = 0; i < csize; ++i)
{
res.data()[i] = 3.0 * x.data()[i] - 2.0 * y.data()[i];
}
benchmark::DoNotOptimize(res.data());
}
}
template <class E>
inline auto assign_x_assign(benchmark::State& state)
{
E x, y, res;
init_xtensor_benchmark(x, y, res, state.range(0), state.range(0));
for (auto _ : state)
{
xt::noalias(res) = 3.0 * x - 2.0 * y;
benchmark::DoNotOptimize(res.data());
}
}
template <class E>
inline auto assign_c_assign_ii(benchmark::State& state)
{
using size_type = typename E::size_type;
E x, y, res;
init_xtensor_benchmark(x, y, res, state.range(0), state.range(0));
for (auto _ : state)
{
size_type csize = x.size();
for (size_type i = 0; i < csize; ++i)
{
res.data()[i] = 3.0 * x.data()[i];
}
benchmark::DoNotOptimize(res.data());
}
}
template <class E>
inline auto assign_x_assign_ii(benchmark::State& state)
{
E x, y, res;
init_xtensor_benchmark(x, y, res, state.range(0), state.range(0));
for (auto _ : state)
{
xt::noalias(res) = 3.0 * x;
benchmark::DoNotOptimize(res.data());
}
}
template <class E>
inline auto assign_x_assign_iii(benchmark::State& state)
{
E x, y, res;
init_xtensor_benchmark(x, y, res, state.range(0), state.range(0));
for (auto _ : state)
{
xt::noalias(res) = y * x;
benchmark::DoNotOptimize(res.data());
}
}
template <class E>
inline auto assign_c_assign_iii(benchmark::State& state)
{
using size_type = typename E::size_type;
E x, y, res;
init_xtensor_benchmark(x, y, res, state.range(0), state.range(0));
for (auto _ : state)
{
size_type csize = x.size();
for (size_type i = 0; i < csize; ++i)
{
res.data()[i] = x.data()[i] * y.data()[i];
}
benchmark::DoNotOptimize(res.data());
}
}
template <class E>
inline auto assign_xstorageiter_copy(benchmark::State& state)
{
E x, y, res;
init_xtensor_benchmark(x, y, res, state.range(0), state.range(0));
for (auto _ : state)
{
auto fun = 3.0 * x - 2.0 * y;
std::copy(fun.linear_cbegin(), fun.linear_cend(), res.linear_begin());
benchmark::DoNotOptimize(res.data());
}
}
template <class E>
inline auto assign_xiter_copy(benchmark::State& state)
{
E x, y, res;
init_xtensor_benchmark(x, y, res, state.range(0), state.range(0));
for (auto _ : state)
{
auto fun = 3.0 * x - 2.0 * y;
std::copy(fun.cbegin(), fun.cend(), res.begin());
benchmark::DoNotOptimize(res.data());
}
}
template <class E>
inline auto assign_c_scalar_computed(benchmark::State& state)
{
using size_type = typename E::size_type;
E x, y, res;
init_xtensor_benchmark(x, y, res, state.range(0), state.range(0));
for (auto _ : state)
{
size_type csize = res.size();
for (size_type i = 0; i < csize; ++i)
{
res.storage()[i] += 3.123;
}
benchmark::DoNotOptimize(res.data());
}
}
template <class E>
inline auto assign_x_scalar_computed(benchmark::State& state)
{
E x, y, res;
init_xtensor_benchmark(x, y, res, state.range(0), state.range(0));
for (auto _ : state)
{
res += 3.123;
benchmark::DoNotOptimize(res.data());
}
}
BENCHMARK_TEMPLATE(assign_c_assign, xt::xtensor<double, 2>)->Range(32, 32 << 3);
BENCHMARK_TEMPLATE(assign_x_assign, xt::xtensor<double, 2>)->Range(32, 32 << 3);
BENCHMARK_TEMPLATE(assign_xiter_copy, xt::xtensor<double, 2>)->Range(32, 32 << 3);
BENCHMARK_TEMPLATE(assign_xstorageiter_copy, xt::xtensor<double, 2>)->Range(32, 32 << 3);
BENCHMARK_TEMPLATE(assign_c_assign_ii, xt::xtensor<double, 2>)->Range(32, 32 << 3);
BENCHMARK_TEMPLATE(assign_x_assign_ii, xt::xtensor<double, 2>)->Range(32, 32 << 3);
BENCHMARK_TEMPLATE(assign_x_assign_iii, xt::xtensor<double, 2>)->Range(32, 32 << 3);
BENCHMARK_TEMPLATE(assign_c_assign_iii, xt::xtensor<double, 2>)->Range(32, 32 << 3);
BENCHMARK_TEMPLATE(assign_x_assign, xt::xarray<double>)->Range(32, 32 << 3);
BENCHMARK_TEMPLATE(assign_x_assign, xt::xarray<double, layout_type::dynamic>)->Range(32, 32 << 3);
BENCHMARK_TEMPLATE(assign_x_assign, xt::xtensor<double, 2, layout_type::dynamic>)->Range(32, 32 << 3);
BENCHMARK_TEMPLATE(assign_c_scalar_computed, xt::xtensor<double, 2>)->Range(32, 32 << 3);
BENCHMARK_TEMPLATE(assign_x_scalar_computed, xt::xtensor<double, 2>)->Range(32, 32 << 3);
}
}
#endif