-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector3D.cpp
More file actions
233 lines (200 loc) · 7.49 KB
/
Vector3D.cpp
File metadata and controls
233 lines (200 loc) · 7.49 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
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
228
229
230
231
232
233
/**
Copyright 2020 Andrey Kudryavtsev (andrewkoudr@hotmail.com)
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted, provided
that the above copyright notice appears in all copies and that both the
copyright notice and this permission notice appear in supporting
documentation, and that the same name not be used in advertising or
publicity pertaining to distribution of the software without specific,
written prior permission.
We make no representations about the suitability of this software for any
purpose. It is provided "as is" without any expressed or implied warranty.
*/
#include <iostream>
#include <vector>
#include <chrono>
#include <random>
#include "Vector.h"
/**
Some tests for 3D vectors, without (TVector<T>) and with SIMD (Vector4).
NDEBUG removed in Release from C/C++ > Preprocessor > Preprocessor Definitions
to enable assert().
*/
int main()
{
using namespace std;
using namespace std::chrono;
high_resolution_clock::time_point t1,t2,t3,t4;
#define NUM_VECTORS 1000000
{
#define T float
//#define NO_AUTOVECTORISATION
cout << " Test 1 : Simple ariphmetic of 3D vectors" << endl;
random_device rd;
mt19937 mt(rd());
uniform_real_distribution<T> dist(1.0, 10.0);
// init vectors
vector<TVector<T>> v1(NUM_VECTORS);
vector<TVector<T>> v2(NUM_VECTORS);
vector<Vector4> v3(NUM_VECTORS);
vector<Vector4> v4(NUM_VECTORS);
for (size_t i = 0; i < NUM_VECTORS; i++)
{
for (size_t j = 0; j < 3; j++)
{
v1[i].XYZ[j] = v3[i].XYZ[j] = dist(mt);
v2[i].XYZ[j] = v4[i].XYZ[j] = dist(mt);
}
}
// without SIMD
t1 = high_resolution_clock::now();
#ifdef NO_AUTOVECTORISATION
#pragma loop(no_vector)
#endif
for (size_t i = 0; i < v1.size(); i++)
{
v1[i] = v1[i] + v2[i] + (v2[i] - v1[i]) * static_cast<T>(0.333);
}
t2 = high_resolution_clock::now();
// with SIMD
t3 = high_resolution_clock::now();
#ifdef NO_AUTOVECTORISATION
#pragma loop(no_vector)
#endif
for (size_t i = 0; i < v3.size(); i++)
{
v3[i] = v3[i] + v4[i]+ (v4[i] - v3[i]) * static_cast<float>(0.333);
}
t4 = high_resolution_clock::now();
// test for correctness : v1 must be same as v3
for (size_t i = 0; i < NUM_VECTORS; i++)
{
T dx = std::abs(v1[i].X - v3[i].X);
T dy = std::abs(v1[i].Y - v3[i].Y);
T dz = std::abs(v1[i].Z - v3[i].Z);
T dw = std::abs(v1[i].W - v3[i].W);
assert(dx < TOLERANCE(T) && dy < TOLERANCE(T) && dz < TOLERANCE(T) && dw < TOLERANCE(T));
}
duration<double> time_span1 = duration_cast<duration<double>>(t2 - t1);
duration<double> time_span2 = duration_cast<duration<double>>(t4 - t3);
cout << "time without SIMD " << time_span1.count() << ", with SIMD " << time_span2.count() <<
" sec, speedup " << time_span1.count() / time_span2.count() << endl;
#undef NO_AUTOVECTORISATION
#undef T
}
{
#define T float
//#define NO_AUTOVECTORISATION
cout << " Test 2 : Ariphmetic, length and normalisation of 3D vectors" << endl;
random_device rd;
mt19937 mt(rd());
uniform_real_distribution<T> dist(1.0, 10.0);
// init vectors
vector<TVector<T>> v1(NUM_VECTORS);
vector<TVector<T>> v2(NUM_VECTORS);
vector<Vector4> v3(NUM_VECTORS);
vector<Vector4> v4(NUM_VECTORS);
for (size_t i = 0; i < NUM_VECTORS; i++)
{
for (size_t j = 0; j < 3; j++)
{
v1[i].XYZ[j] = v3[i].XYZ[j] = dist(mt);
v2[i].XYZ[j] = v4[i].XYZ[j] = dist(mt);
}
}
// without SIMD
t1 = high_resolution_clock::now();
#ifdef NO_AUTOVECTORISATION
#pragma loop(no_vector)
#endif
for (size_t i = 0; i < v1.size(); i++)
{
v1[i] = 6 * (v1[i] + v2[i]) / (!(v1[i] + v2[i])) + (+(v2[i] - v1[i])) * static_cast<T>(0.333);
}
t2 = high_resolution_clock::now();
// with SIMD
t3 = high_resolution_clock::now();
#ifdef NO_AUTOVECTORISATION
#pragma loop(no_vector)
#endif
for (size_t i = 0; i < v3.size(); i++)
{
v3[i] = 6 * (v3[i] + v4[i]) / (!(v3[i] + v4[i])) + (+(v4[i] - v3[i])) * static_cast<float>(0.333);
}
t4 = high_resolution_clock::now();
// test for correctness : v1 must be same as v3
for (size_t i = 0; i < NUM_VECTORS; i++)
{
T dx = std::abs(v1[i].X - v3[i].X);
T dy = std::abs(v1[i].Y - v3[i].Y);
T dz = std::abs(v1[i].Z - v3[i].Z);
T dw = std::abs(v1[i].W - v3[i].W);
assert(dx < TOLERANCE(T) && dy < TOLERANCE(T) && dz < TOLERANCE(T) && dw < TOLERANCE(T));
}
duration<double> time_span1 = duration_cast<duration<double>>(t2 - t1);
duration<double> time_span2 = duration_cast<duration<double>>(t4 - t3);
cout << "time without SIMD " << time_span1.count() << ", with SIMD " << time_span2.count() <<
" sec, speedup " << time_span1.count() / time_span2.count() << endl;
#undef NO_AUTOVECTORISATION
#undef T
}
{
#define T float
//#define NO_AUTOVECTORISATION
cout << " Test 3 : Ariphmetic, dot and cross products of 3D vectors" << endl;
random_device rd;
mt19937 mt(rd());
uniform_real_distribution<T> dist(1.0, 10.0);
// init vectors
vector<TVector<T>> v1(NUM_VECTORS);
vector<TVector<T>> v2(NUM_VECTORS);
vector<Vector4> v3(NUM_VECTORS);
vector<Vector4> v4(NUM_VECTORS);
for (size_t i = 0; i < NUM_VECTORS; i++)
{
for (size_t j = 0; j < 3; j++)
{
v1[i].XYZ[j] = v3[i].XYZ[j] = dist(mt);
v2[i].XYZ[j] = v4[i].XYZ[j] = dist(mt);
}
}
// without SIMD
t1 = high_resolution_clock::now();
#ifdef NO_AUTOVECTORISATION
#pragma loop(no_vector)
#endif
for (size_t i = 0; i < v1.size(); i++)
{
v1[i] = ((v2[i] + v1[i]) ^ (v2[i] - v1[i])) * (v2[i] * v1[i]);
}
t2 = high_resolution_clock::now();
// with SIMD
t3 = high_resolution_clock::now();
#ifdef NO_AUTOVECTORISATION
#pragma loop(no_vector)
#endif
for (size_t i = 0; i < v3.size(); i++)
{
v3[i] = ((v4[i] + v3[i]) ^ (v4[i] - v3[i])) * (v4[i] * v3[i]);
}
t4 = high_resolution_clock::now();
// test for correctness : v1 must be same as v3
for (size_t i = 0; i < NUM_VECTORS; i++)
{
T dx = std::abs(v1[i].X - v3[i].X);
T dy = std::abs(v1[i].Y - v3[i].Y);
T dz = std::abs(v1[i].Z - v3[i].Z);
T dw = std::abs(v1[i].W - v3[i].W);
assert(dx < TOLERANCE(T) && dy < TOLERANCE(T) && dz < TOLERANCE(T) && dw < TOLERANCE(T));
}
duration<double> time_span1 = duration_cast<duration<double>>(t2 - t1);
duration<double> time_span2 = duration_cast<duration<double>>(t4 - t3);
cout << "time without SIMD " << time_span1.count() << ", with SIMD " << time_span2.count() <<
" sec, speedup " << time_span1.count() / time_span2.count() << endl;
#undef NO_AUTOVECTORISATION
#undef T
}
#undef NUM_VECTORS
cout << endl << "Press [ENTER]" << endl;
getchar();
}