Developer Documentation

VectorT.cpp
1 /*
2 * VectorT.cpp
3 *
4 * Created on: Nov 19, 2015
5 * Author: ebke
6 */
7
8 #ifndef BMPOSTFIX
9 #error "Do not compile directly."
10 #endif
11
12 #include <type_traits>
13
14 #define ASSEMBLE_(POSTFIX, PREFIX) PREFIX ## POSTFIX
15 #define ASSEMBLE(POSTFIX, PREFIX) ASSEMBLE_(POSTFIX, PREFIX)
16 #define MYBENCHMARK(NAME) BENCHMARK(NAME)
17 #define MYBENCHMARK_TEMPLATE(NAME, TYPE) BENCHMARK_TEMPLATE(NAME, TYPE)
18
19 template<class Vec>
20 static inline
21 typename std::enable_if<Vec::size_ == 3, Vec>::type
22 testVec() {
23 return Vec(1.1, 1.2, 1.3);
24 }
25
26 template<class Vec>
27 static inline
28 typename std::enable_if<Vec::size_ == 4, Vec>::type
29 testVec() {
30 return Vec(1.1, 1.2, 1.3, 1.4);
31 }
32
33 template<class Vec>
34 static void ASSEMBLE(BMPOSTFIX, Vec_add_compare)(benchmark::State& state) {
35 Vec v1(0.0);
36 Vec v2(1000.0);
37 while (state.KeepRunning()) {
38 v1 += testVec<Vec>();
39 v2 -= testVec<Vec>();
40 if (v1 == v2) {
41 v1 -= v2;
42 v2 += v1;
43 }
44 }
45 // Just so nothing gets optimized away.
46 static double dummy;
47 dummy = v1.norm() + v2.norm();
48 static_cast<void>(dummy);
49 }
50
51 MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_add_compare), OpenMesh::Vec3d);
52 MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_add_compare), OpenMesh::Vec3f);
53 MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_add_compare), OpenMesh::Vec4d);
54 MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_add_compare), OpenMesh::Vec4f);
55
56 template<class Vec>
57 static void ASSEMBLE(BMPOSTFIX, Vec_cross_product)(benchmark::State& state) {
58 Vec v1(0.0);
59 Vec v2(1000.0);
60 while (state.KeepRunning()) {
61 v1 += testVec<Vec>();
62 v2 -= testVec<Vec>();
63 v1 = (v1 % v2);
64 }
65 // Just so nothing gets optimized away.
66 static double dummy;
67 dummy = v1.norm() + v2.norm();
68 static_cast<void>(dummy);
69 }
70
71 MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_cross_product), OpenMesh::Vec3d);
72 MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_cross_product), OpenMesh::Vec3f);
73
74 template<class Vec>
75 static void ASSEMBLE(BMPOSTFIX, Vec_scalar_product)(benchmark::State& state) {
76 Vec v1(0.0);
77 Vec v2(1000.0);
78 double acc = 0;
79 while (state.KeepRunning()) {
80 v1 += testVec<Vec>();
81 v2 -= testVec<Vec>();
82 acc += (v1 | v2);
83 }
84 // Otherwise GCC will optimize everything away.
85 static double dummy;
86 dummy = acc;
87 static_cast<void>(dummy);
88 }
89
90 MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_scalar_product), OpenMesh::Vec3d);
91 MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_scalar_product), OpenMesh::Vec3f);
92 MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_scalar_product), OpenMesh::Vec4d);
93 MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_scalar_product), OpenMesh::Vec4f);
94
95 template<class Vec>
96 static void ASSEMBLE(BMPOSTFIX, Vec_norm)(benchmark::State& state) {
97 Vec v1(0.0);
98 double acc = 0;
99 while (state.KeepRunning()) {
100 v1 += testVec<Vec>();
101 acc += v1.norm();
102 }
103 // Otherwise GCC will optimize everything away.
104 static double dummy;
105 dummy = acc;
106 static_cast<void>(dummy);
107 }
108
109 MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_norm), OpenMesh::Vec3d);
110 MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_norm), OpenMesh::Vec3f);
111 MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_norm), OpenMesh::Vec4d);
112 MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_norm), OpenMesh::Vec4f);
113
114 template<class Vec>
115 static void ASSEMBLE(BMPOSTFIX, Vec_times_scalar)(benchmark::State& state) {
116 Vec v1(0.0);
117 while (state.KeepRunning()) {
118 v1 += testVec<Vec>();
119 v1 *= static_cast<decltype(v1.norm())>(1.0)/v1[0];
120 v1 *= v1[1];
121 }
122 // Otherwise GCC will optimize everything away.
123 static double dummy;
124 dummy = v1.norm();
125 static_cast<void>(dummy);
126 }
127
128 MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_times_scalar), OpenMesh::Vec3d);
129 MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_times_scalar), OpenMesh::Vec3f);
130 MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_times_scalar), OpenMesh::Vec4d);
131 MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_times_scalar), OpenMesh::Vec4f);
132
133 #include "VectorT_dummy_data.hpp"
134
135 template<class Vec>
136 static void ASSEMBLE(BMPOSTFIX, Vec_add_in_place)(benchmark::State& state) {
137 auto v = make_dummy_vector<Vec>();
138 while (state.KeepRunning()) {
139 v += v;
140 }
141 // Otherwise GCC will optimize everything away.
142 static double dummy = observe_dummy_vector(v);
143 static_cast<void>(dummy);
144 }
145
146 template<class Vec>
147 static void ASSEMBLE(BMPOSTFIX, Vec_add_temporary)(benchmark::State& state) {
148 auto v = make_dummy_vector<Vec>();
149 while (state.KeepRunning()) {
150 v = v + v;
151 }
152 // Otherwise GCC will optimize everything away.
153 static double dummy = observe_dummy_vector(v);
154 static_cast<void>(dummy);
155 }
156
157 typedef OpenMesh::VectorT<std::valarray<double>, 3> Vec3VAd;
158 typedef OpenMesh::VectorT<std::complex<double>, 3> Vec3Cd;
159
160 MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_add_in_place), Vec3VAd);
161 MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_add_temporary), Vec3VAd);
162 MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_add_in_place), Vec3Cd);
163 MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_add_temporary), Vec3Cd);
OpenMesh::VectorT::norm
Scalar norm(const VectorT< Scalar, DIM > &_v)
Definition: MeshNode2T_impl.hh:275
OpenMesh::VectorT
Definition: Vector11T.hh:83