-
Notifications
You must be signed in to change notification settings - Fork 436
Expand file tree
/
Copy pathblas1_comparison.cpp
More file actions
177 lines (140 loc) · 5.58 KB
/
blas1_comparison.cpp
File metadata and controls
177 lines (140 loc) · 5.58 KB
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
/***************************************************************************
* Comparison benchmarks: BLAS1 operations
* Comparing raw C++ vs xtensor performance
****************************************************************************/
#include <cstddef>
#include <vector>
#include <benchmark/benchmark.h>
#include "xtensor/containers/xarray.hpp"
#include "xtensor/containers/xtensor.hpp"
#include "xtensor/core/xnoalias.hpp"
namespace xt::comparison
{
//========================================================================
// Helpers
//========================================================================
// Benchmark range configuration
constexpr std::size_t min_size = 8;
constexpr std::size_t max_size = 16384;
constexpr std::size_t multiplier = 4;
// Helper to create xtensor vectors
inline auto make_xtensor(std::size_t size, double val)
{
return xt::xtensor<double, 1>::from_shape({size}) * val;
}
inline auto make_xtensor_zeros(std::size_t size)
{
auto c = xt::xtensor<double, 1>::from_shape({size});
c.fill(0);
return c;
}
// Helper to create xarray
inline auto make_xarray(std::size_t size, double val)
{
return xt::xarray<double>::from_shape({size}) * val;
}
// Macro for benchmark loop (reduces boilerplate)
#define BENCHMARK_LOOP(state, container, ...) \
for (auto _ : state) \
{ \
__VA_ARGS__; \
benchmark::DoNotOptimize(container.data()); \
}
// Macro for registering benchmarks with standard sizes
#define REGISTER_BENCHMARK(func) BENCHMARK(func)->Range(min_size, max_size)->RangeMultiplier(multiplier)
//========================================================================
// Vector Addition: z = x + y
//========================================================================
static void add_vector_std(benchmark::State& state)
{
const std::size_t size = state.range(0);
std::vector<double> x(size, 1.0);
std::vector<double> y(size, 2.0);
std::vector<double> z(size);
BENCHMARK_LOOP(state, z, for (std::size_t i = 0; i < size; ++i) z[i] = x[i] + y[i];);
}
static void add_vector_xarray(benchmark::State& state)
{
const std::size_t size = state.range(0);
auto x = make_xarray(size, 1.0);
auto y = make_xarray(size, 2.0);
xt::xarray<double> z;
BENCHMARK_LOOP(state, z, z = x + y;);
}
static void add_vector_xtensor(benchmark::State& state)
{
const std::size_t size = state.range(0);
auto x = make_xtensor(size, 1.0);
auto y = make_xtensor(size, 2.0);
xt::xtensor<double, 1> z;
BENCHMARK_LOOP(state, z, z = x + y;);
}
static void add_vector_noalias(benchmark::State& state)
{
const std::size_t size = state.range(0);
auto x = make_xtensor(size, 1.0);
auto y = make_xtensor(size, 2.0);
auto z = make_xtensor_zeros(size);
BENCHMARK_LOOP(state, z, xt::noalias(z) = x + y;);
}
//========================================================================
// Scalar Addition: z = x + a
//========================================================================
static void add_scalar_std(benchmark::State& state)
{
const std::size_t size = state.range(0);
constexpr double a = 5.0;
std::vector<double> x(size, 1.0);
std::vector<double> z(size);
BENCHMARK_LOOP(state, z, for (std::size_t i = 0; i < size; ++i) z[i] = x[i] + a;);
}
static void add_scalar_xtensor(benchmark::State& state)
{
const std::size_t size = state.range(0);
constexpr double a = 5.0;
auto x = make_xtensor(size, 1.0);
xt::xtensor<double, 1> z;
BENCHMARK_LOOP(state, z, z = x + a;);
}
static void add_scalar_noalias(benchmark::State& state)
{
const std::size_t size = state.range(0);
constexpr double a = 5.0;
auto x = make_xtensor(size, 1.0);
auto z = make_xtensor_zeros(size);
BENCHMARK_LOOP(state, z, xt::noalias(z) = x + a;);
}
//========================================================================
// Scalar Multiplication: y = a * x
//========================================================================
static void mul_scalar_std(benchmark::State& state)
{
const std::size_t size = state.range(0);
constexpr double a = 2.5;
std::vector<double> x(size, 1.0);
std::vector<double> y(size);
BENCHMARK_LOOP(state, y, for (std::size_t i = 0; i < size; ++i) y[i] = a * x[i];);
}
static void mul_scalar_xtensor(benchmark::State& state)
{
const std::size_t size = state.range(0);
constexpr double a = 2.5;
auto x = make_xtensor(size, 1.0);
xt::xtensor<double, 1> y;
BENCHMARK_LOOP(state, y, y = a * x;);
}
//========================================================================
// Register benchmarks
//========================================================================
// Vector + Vector
REGISTER_BENCHMARK(add_vector_std);
REGISTER_BENCHMARK(add_vector_xarray);
REGISTER_BENCHMARK(add_vector_xtensor);
REGISTER_BENCHMARK(add_vector_noalias);
// Scalar operations
REGISTER_BENCHMARK(add_scalar_std);
REGISTER_BENCHMARK(add_scalar_xtensor);
REGISTER_BENCHMARK(add_scalar_noalias);
REGISTER_BENCHMARK(mul_scalar_std);
REGISTER_BENCHMARK(mul_scalar_xtensor);
}