1
+ /* Copyright (C) 2018 Povilas Kanapickas <[email protected] >
2
+ Copyright (C) 2018 Thomas Retornaz <[email protected] >
3
+
4
+ Distributed under the Boost Software License, Version 1.0.
5
+ (See accompanying file LICENSE_1_0.txt or copy at
6
+ http://www.boost.org/LICENSE_1_0.txt)
7
+ */
8
+
9
+ #include " benchmark/benchmark.h"
10
+ #include < vector>
11
+ #include < algorithm>
12
+ #include < iterator>
13
+ #include < simdpp/simd.h>
14
+ // algorithm
15
+ #include < simdpp/algorithm/transform.h>
16
+
17
+
18
+ namespace {
19
+
20
+ template < typename T>
21
+ struct BinaryOpAdd
22
+ {
23
+ public:
24
+ BinaryOpAdd () {}
25
+ SIMDPP_INL T operator ()(T const &a0, T const &a1) const SIMDPP_NOEXCEPT
26
+ {
27
+ return a0 + a1;
28
+ }
29
+
30
+ template <typename U>
31
+ SIMDPP_INL U operator ()(U const &a0, U const &a1) const SIMDPP_NOEXCEPT
32
+ {
33
+ using namespace simdpp ;
34
+ return a0 + a1;
35
+ }
36
+ };
37
+
38
+
39
+ template <typename T>
40
+ struct GeneratorConstant
41
+ {
42
+ GeneratorConstant (T constant) { m_constant = constant; }
43
+ T operator ()() { return m_constant; }
44
+ T m_constant;
45
+ };
46
+
47
+
48
+ template <typename T, class Generator >
49
+ std::vector<T, simdpp::aligned_allocator<T, simdpp::simd_traits<T>::alignment>> DataGenerator (std::size_t size, Generator gen)
50
+ {
51
+
52
+ using vector_aligned_t = std::vector<T, simdpp::aligned_allocator<T, simdpp::simd_traits<T>::alignment>>;
53
+ vector_aligned_t input (size);
54
+ std::generate (input.begin (), input.end (), gen);
55
+ return input;
56
+ }
57
+
58
+ /* ********************Binary****************************/
59
+
60
+ template <typename T>
61
+ class TransformBinaryFixture : public ::benchmark::Fixture {
62
+ public:
63
+ void SetUp (const ::benchmark::State& st)
64
+ {
65
+ m_inputvect = DataGenerator<T, GeneratorConstant<T>>((size_t )st.range (0 ), GeneratorConstant<T>(42 ));
66
+ m_inputvect2 = DataGenerator<T, GeneratorConstant<T>>((size_t )st.range (0 ), GeneratorConstant<T>(42 ));
67
+ m_outputvect.resize ((size_t )st.range (0 ));
68
+ }
69
+ void TearDown (const ::benchmark::State&)
70
+ {
71
+ m_inputvect.clear ();
72
+ m_inputvect2.clear ();
73
+ m_outputvect.clear ();
74
+ }
75
+ using vector_aligned_t = std::vector<T, simdpp::aligned_allocator<T, simdpp::simd_traits<T>::alignment>>;
76
+ vector_aligned_t m_inputvect;
77
+ vector_aligned_t m_inputvect2;
78
+ vector_aligned_t m_outputvect;
79
+ BinaryOpAdd<T> opPlus= BinaryOpAdd<T>();
80
+ };
81
+
82
+ // UINT8_T
83
+ BENCHMARK_TEMPLATE_DEFINE_F (TransformBinaryFixture, BinaryUNINT8_SIMD_Test, uint8_t )(benchmark::State& st)
84
+ {
85
+ const auto size= (size_t )st.range (0 );
86
+ while (st.KeepRunning ())
87
+ {
88
+ benchmark::DoNotOptimize (simdpp::transform (m_inputvect.data (), m_inputvect.data () + m_inputvect.size (),m_inputvect2.data (),m_outputvect.data (), opPlus));
89
+ }
90
+ }
91
+ BENCHMARK_REGISTER_F (TransformBinaryFixture, BinaryUNINT8_SIMD_Test)->Arg (1 )->Arg(10 )->Arg(32 )->Arg(100 )->Arg(1000 )->Arg(10000 );
92
+
93
+ BENCHMARK_TEMPLATE_DEFINE_F (TransformBinaryFixture, BinaryUNINT8_STD_Test, uint8_t )(benchmark::State& st)
94
+ {
95
+ const auto size = (size_t )st.range (0 );
96
+ while (st.KeepRunning ())
97
+ {
98
+ benchmark::DoNotOptimize (std::transform (m_inputvect.begin (), m_inputvect.end (), m_inputvect.begin (),m_outputvect.begin (), opPlus));
99
+ }
100
+ }
101
+ BENCHMARK_REGISTER_F (TransformBinaryFixture, BinaryUNINT8_STD_Test)->Arg (1 )->Arg(10 )->Arg(32 )->Arg(100 )->Arg(1000 )->Arg(10000 );
102
+
103
+ // UINT16_T
104
+ BENCHMARK_TEMPLATE_DEFINE_F (TransformBinaryFixture, BinaryUNINT16_SIMD_Test, uint16_t )(benchmark::State& st)
105
+ {
106
+ const auto size= (size_t )st.range (0 );
107
+ while (st.KeepRunning ())
108
+ {
109
+ benchmark::DoNotOptimize (simdpp::transform (m_inputvect.data (), m_inputvect.data () + m_inputvect.size (), m_inputvect2.data (), m_outputvect.data (), opPlus));
110
+ }
111
+ }
112
+ BENCHMARK_REGISTER_F (TransformBinaryFixture, BinaryUNINT16_SIMD_Test)->Arg (1 )->Arg(10 )->Arg(32 )->Arg(100 )->Arg(1000 )->Arg(10000 );
113
+
114
+ BENCHMARK_TEMPLATE_DEFINE_F (TransformBinaryFixture, BinaryUNINT16_STD_Test, uint16_t )(benchmark::State& st)
115
+ {
116
+ const auto size = (size_t )st.range (0 );
117
+ while (st.KeepRunning ())
118
+ {
119
+ benchmark::DoNotOptimize (std::transform (m_inputvect.begin (), m_inputvect.end (), m_inputvect.begin (), m_outputvect.begin (), opPlus));
120
+ }
121
+ }
122
+ BENCHMARK_REGISTER_F (TransformBinaryFixture, BinaryUNINT16_STD_Test)->Arg (1 )->Arg(10 )->Arg(32 )->Arg(100 )->Arg(1000 )->Arg(10000 );
123
+
124
+ // UINT32_T
125
+ BENCHMARK_TEMPLATE_DEFINE_F (TransformBinaryFixture, BinaryUNINT32_SIMD_Test, uint32_t )(benchmark::State& st)
126
+ {
127
+ const auto size= (size_t )st.range (0 );
128
+ while (st.KeepRunning ())
129
+ {
130
+ benchmark::DoNotOptimize (simdpp::transform (m_inputvect.data (), m_inputvect.data () + m_inputvect.size (), m_inputvect2.data (), m_outputvect.data (), opPlus));
131
+ }
132
+ }
133
+ BENCHMARK_REGISTER_F (TransformBinaryFixture, BinaryUNINT32_SIMD_Test)->Arg (1 )->Arg(10 )->Arg(32 )->Arg(100 )->Arg(1000 )->Arg(10000 );
134
+
135
+ BENCHMARK_TEMPLATE_DEFINE_F (TransformBinaryFixture, BinaryUNINT32_STD_Test, uint32_t )(benchmark::State& st)
136
+ {
137
+ const auto size = (size_t )st.range (0 );
138
+ while (st.KeepRunning ())
139
+ {
140
+ benchmark::DoNotOptimize (std::transform (m_inputvect.begin (), m_inputvect.end (), m_inputvect.begin (), m_outputvect.begin (), opPlus));
141
+ }
142
+ }
143
+ BENCHMARK_REGISTER_F (TransformBinaryFixture, BinaryUNINT32_STD_Test)->Arg (1 )->Arg(10 )->Arg(31 )->Arg(100 )->Arg(1000 )->Arg(10000 );
144
+
145
+ // UINT64_T
146
+ BENCHMARK_TEMPLATE_DEFINE_F (TransformBinaryFixture, BinaryUNINT64_SIMD_Test, uint64_t )(benchmark::State& st)
147
+ {
148
+ const auto size= (size_t )st.range (0 );
149
+ while (st.KeepRunning ())
150
+ {
151
+ benchmark::DoNotOptimize (simdpp::transform (m_inputvect.data (), m_inputvect.data () + m_inputvect.size (), m_inputvect2.data (), m_outputvect.data (), opPlus));
152
+ }
153
+ }
154
+ BENCHMARK_REGISTER_F (TransformBinaryFixture, BinaryUNINT64_SIMD_Test)->Arg (1 )->Arg(10 )->Arg(32 )->Arg(100 )->Arg(1000 )->Arg(10000 );
155
+
156
+ BENCHMARK_TEMPLATE_DEFINE_F (TransformBinaryFixture, BinaryUNINT64_STD_Test, uint64_t )(benchmark::State& st)
157
+ {
158
+ const auto size = (size_t )st.range (0 );
159
+ while (st.KeepRunning ())
160
+ {
161
+ benchmark::DoNotOptimize (std::transform (m_inputvect.begin (), m_inputvect.end (), m_inputvect.begin (), m_outputvect.begin (), opPlus));
162
+ }
163
+ }
164
+ BENCHMARK_REGISTER_F (TransformBinaryFixture, BinaryUNINT64_STD_Test)->Arg (1 )->Arg(10 )->Arg(32 )->Arg(100 )->Arg(1000 )->Arg(10000 );
165
+
166
+ // FLOAT
167
+ BENCHMARK_TEMPLATE_DEFINE_F (TransformBinaryFixture, BinaryFloat_SIMD_Test, float )(benchmark::State& st)
168
+ {
169
+ const auto size = (size_t )st.range (0 );
170
+ while (st.KeepRunning ())
171
+ {
172
+ benchmark::DoNotOptimize (simdpp::transform (m_inputvect.data (), m_inputvect.data () + m_inputvect.size (), m_inputvect2.data (), m_outputvect.data (), opPlus));
173
+ }
174
+ }
175
+ BENCHMARK_REGISTER_F (TransformBinaryFixture, BinaryFloat_SIMD_Test)->Arg (1 )->Arg(10 )->Arg(32 )->Arg(100 )->Arg(1000 )->Arg(10000 );
176
+
177
+
178
+ BENCHMARK_TEMPLATE_DEFINE_F (TransformBinaryFixture, BinaryFloat_STD_Test, float )(benchmark::State& st)
179
+ {
180
+ const auto size = (size_t )st.range (0 );
181
+ while (st.KeepRunning ())
182
+ {
183
+ benchmark::DoNotOptimize (std::transform (m_inputvect.begin (), m_inputvect.end (), m_inputvect.begin (), m_outputvect.begin (), opPlus));
184
+ }
185
+ }
186
+ BENCHMARK_REGISTER_F (TransformBinaryFixture, BinaryFloat_STD_Test)->Arg (1 )->Arg(10 )->Arg(32 )->Arg(100 )->Arg(1000 )->Arg(10000 );
187
+
188
+ // DOUBLE
189
+ BENCHMARK_TEMPLATE_DEFINE_F (TransformBinaryFixture, BinaryDouble_SIMD_Test, double )(benchmark::State& st)
190
+ {
191
+ const auto size= (size_t )st.range (0 );
192
+ while (st.KeepRunning ())
193
+ {
194
+ benchmark::DoNotOptimize (simdpp::transform (m_inputvect.data (), m_inputvect.data () + m_inputvect.size (), m_inputvect2.data (), m_outputvect.data (), opPlus));
195
+ }
196
+ }
197
+ BENCHMARK_REGISTER_F (TransformBinaryFixture, BinaryDouble_SIMD_Test)->Arg (1 )->Arg(10 )->Arg(32 )->Arg(100 )->Arg(1000 )->Arg(10000 );
198
+
199
+ BENCHMARK_TEMPLATE_DEFINE_F (TransformBinaryFixture, BinaryDouble_STD_Test, double )(benchmark::State& st)
200
+ {
201
+ const auto size = (size_t )st.range (0 );
202
+ while (st.KeepRunning ())
203
+ {
204
+ benchmark::DoNotOptimize (std::transform (m_inputvect.begin (), m_inputvect.end (), m_inputvect.begin (), m_outputvect.begin (), opPlus));
205
+ }
206
+ }
207
+ BENCHMARK_REGISTER_F (TransformBinaryFixture, BinaryDouble_STD_Test)->Arg (1 )->Arg(10 )->Arg(32 )->Arg(100 )->Arg(1000 )->Arg(10000 );
208
+
209
+ } // namespace
0 commit comments