-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
511 lines (450 loc) · 18.5 KB
/
main.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/* An comparison of the Eigen and PETSc libraries for matrix multiplication,
matrix vector multiplication, and matrix inverses.
Copyright (C) 2016 Michael Nucci (michael.nucci@gmail.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
// aither includes
#include <matrix.hpp>
#include <genArray.hpp>
// eigen includes
#include <Eigen/Dense>
#include <iostream>
#include <iomanip>
#include <numeric>
#include <chrono>
#include <array>
Eigen::MatrixXd FillEigenMatrix(const int &size, double start,
const double &diagFactor);
Eigen::Matrix2d FillEigenMatrix2d(double start, const double &diagFactor);
Eigen::VectorXd FillEigenVector(const int &size, double start);
Eigen::Vector2d FillEigenVector2d(double start);
squareMatrix FillAitherMatrix(const int &size, double start,
const double &diagFactor);
genArray FillAitherVector(const int &size, double start);
genArray FillAitherVector2d(double start);
int main(int argc, char *argv[]) {
// determine if eigen is vectorized
#ifdef EIGEN_VECTORIZE
std::cout << "Eigen is vectorized" << std::endl;
#endif
// put data into eigen matrices and vectors
// eigen contains special fixed sized matrices and vectors which allows
// allocation on stack -- will test static and dynamic allocation versions
auto eFlowMat = FillEigenMatrix(5, -6.0, 10.0);
auto eTurbMat = FillEigenMatrix2d(1.0, 10.0);
auto eXTurbMat = FillEigenMatrix(2, 1.0, 10.0);
auto eFlowVec = FillEigenVector(5, -9.0);
auto eTurbVec = FillEigenVector2d(3.0);
auto eXTurbVec = FillEigenVector(2, 3.0);
std::cout << "***** Populating Eigen Data *****\n" << std::endl;
std::cout << "Eigen flow matrix:\n" << eFlowMat << std::endl;
std::cout << "\nEigen turbulence matrix (static):\n" << eTurbMat << std::endl;
std::cout << "\nEigen flow vector:\n" << eFlowVec << std::endl;
std::cout << "\nEigen turbulence vector (static):\n" << eTurbVec << std::endl;
std::cout << "\nEigen turbulence matrix (dynamic):\n" << eXTurbMat
<< std::endl;
std::cout << "\nEigen turbulence vector (dynamic):\n" << eXTurbVec
<< std::endl;
std::cout << "\n********************\n" << std::endl;
// put data into aither matrices and vectors
// matrices in aither are variable size and dynamically allocated
// vectors in aither are fixed size (max equations) and statically allocated
auto aFlowMat = FillAitherMatrix(5, -6.0, 10.0);
auto aTurbMat = FillAitherMatrix(2, 1.0, 10.0);
auto aFlowVec = FillAitherVector(5, -9.0);
auto aTurbVec = FillAitherVector2d(3.0);
std::cout << "***** Populating Aither Data *****\n" << std::endl;
std::cout << "Aither flow matrix:\n" << aFlowMat << std::endl;
std::cout << "\nAither turbulence matrix:\n" << aTurbMat << std::endl;
std::cout << "\nAither flow vector:\n" << aFlowVec << std::endl;
std::cout << "\nAither turbulence vector:\n" << aTurbVec << std::endl;
std::cout << "\n********************\n" << std::endl;
constexpr auto samples = 10000000;
// array to hold eigen times
std::array<double, 5> eTimeFlow;
std::array<double, 5> eTimeTurb;
std::array<double, 5> eXTimeTurb;
// array to hold aither times
std::array<double, 5> aTimeFlow;
std::array<double, 5> aTimeTurb;
// test matrix matrix multiplication ---------------------------------------
// eigen
Eigen::MatrixXd eFlowProd;
Eigen::Matrix2d eTurbProd;
Eigen::MatrixXd eXTurbProd;
auto start = std::chrono::high_resolution_clock::now();
for (auto ii = 0; ii < samples; ++ii) {
eFlowProd = eFlowMat * eFlowMat;
}
auto stop = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> duration = stop - start;
std::cout << "Eigen flow matrix matrix multiplication: " << duration.count()
<< " seconds" << std::endl;
std::cout << eFlowProd << "\n" << std::endl;
eTimeFlow[0] = duration.count();
start = std::chrono::high_resolution_clock::now();
for (auto ii = 0; ii < samples; ++ii) {
eTurbProd = eTurbMat * eTurbMat;
}
stop = std::chrono::high_resolution_clock::now();
duration = stop - start;
std::cout << "Eigen (static) turbulence matrix matrix multiplication: "
<< duration.count() << " seconds" << std::endl;
std::cout << eTurbProd << "\n" << std::endl;
eTimeTurb[0] = duration.count();
start = std::chrono::high_resolution_clock::now();
for (auto ii = 0; ii < samples; ++ii) {
eXTurbProd = eXTurbMat * eXTurbMat;
}
stop = std::chrono::high_resolution_clock::now();
duration = stop - start;
std::cout << "Eigen (dynamic) turbulence matrix matrix multiplication: "
<< duration.count() << " seconds" << std::endl;
std::cout << eXTurbProd << "\n" << std::endl;
eXTimeTurb[0] = duration.count();
// aither
squareMatrix aFlowProd;
squareMatrix aTurbProd;
start = std::chrono::high_resolution_clock::now();
for (auto ii = 0; ii < samples; ++ii) {
aFlowProd = aFlowMat.MatMult(aFlowMat);
}
stop = std::chrono::high_resolution_clock::now();
duration = stop - start;
std::cout << "Aither flow matrix matrix multiplication: " << duration.count()
<< " seconds" << std::endl;
std::cout << aFlowProd << "\n" << std::endl;
aTimeFlow[0] = duration.count();
start = std::chrono::high_resolution_clock::now();
for (auto ii = 0; ii < samples; ++ii) {
aTurbProd = aTurbMat.MatMult(aTurbMat);
}
stop = std::chrono::high_resolution_clock::now();
duration = stop - start;
std::cout << "Aither turbulence matrix matrix multiplication: "
<< duration.count() << " seconds" << std::endl;
std::cout << aTurbProd << "\n" << std::endl;
aTimeTurb[0] = duration.count();
// test matrix vector multiplication --------------------------------------
// eigen
Eigen::VectorXd eFlowProdV;
Eigen::Vector2d eTurbProdV;
Eigen::VectorXd eXTurbProdV;
start = std::chrono::high_resolution_clock::now();
for (auto ii = 0; ii < samples; ++ii) {
eFlowProdV = eFlowMat * eFlowVec;
}
stop = std::chrono::high_resolution_clock::now();
duration = stop - start;
std::cout << "Eigen flow matrix vector multiplication: " << duration.count()
<< " seconds" << std::endl;
std::cout << eFlowProdV << "\n" << std::endl;
eTimeFlow[1] = duration.count();
start = std::chrono::high_resolution_clock::now();
for (auto ii = 0; ii < samples; ++ii) {
eTurbProdV = eTurbMat * eTurbVec;
}
stop = std::chrono::high_resolution_clock::now();
duration = stop - start;
std::cout << "Eigen (static) turbulence matrix vector multiplication: "
<< duration.count() << " seconds" << std::endl;
std::cout << eTurbProdV << "\n" << std::endl;
eTimeTurb[1] = duration.count();
start = std::chrono::high_resolution_clock::now();
for (auto ii = 0; ii < samples; ++ii) {
eXTurbProdV = eXTurbMat * eXTurbVec;
}
stop = std::chrono::high_resolution_clock::now();
duration = stop - start;
std::cout << "Eigen (dynamic) turbulence matrix vector multiplication: "
<< duration.count() << " seconds" << std::endl;
std::cout << eXTurbProdV << "\n" << std::endl;
eXTimeTurb[1] = duration.count();
// aither
genArray aFlowProdV;
genArray aTurbProdV;
start = std::chrono::high_resolution_clock::now();
for (auto ii = 0; ii < samples; ++ii) {
aFlowProdV = aFlowMat.ArrayMult(aFlowVec);
}
stop = std::chrono::high_resolution_clock::now();
duration = stop - start;
std::cout << "Eigen flow matrix vector multiplication: " << duration.count()
<< " seconds" << std::endl;
std::cout << aFlowProdV << "\n" << std::endl;
aTimeFlow[1] = duration.count();
start = std::chrono::high_resolution_clock::now();
for (auto ii = 0; ii < samples; ++ii) {
aTurbProdV = aTurbMat.ArrayMult(aTurbVec, 5);
}
stop = std::chrono::high_resolution_clock::now();
duration = stop - start;
std::cout << "Eigen turbulence matrix vector multiplication: "
<< duration.count() << " seconds" << std::endl;
std::cout << aTurbProdV << "\n" << std::endl;
aTimeTurb[1] = duration.count();
// test matrix multiplication with scalar and addition --------------------
// eigen
Eigen::MatrixXd eFlowAdd;
Eigen::Matrix2d eTurbAdd;
Eigen::MatrixXd eXTurbAdd;
start = std::chrono::high_resolution_clock::now();
for (auto ii = 0; ii < samples; ++ii) {
eFlowAdd = 1.5 * eFlowMat + eFlowMat;
}
stop = std::chrono::high_resolution_clock::now();
duration = stop - start;
std::cout << "Eigen flow matrix matrix addition: " << duration.count()
<< " seconds" << std::endl;
std::cout << eFlowAdd << "\n" << std::endl;
eTimeFlow[2] = duration.count();
start = std::chrono::high_resolution_clock::now();
for (auto ii = 0; ii < samples; ++ii) {
eTurbAdd = 1.5 * eTurbMat + eTurbMat;
}
stop = std::chrono::high_resolution_clock::now();
duration = stop - start;
std::cout << "Eigen (static) turbulence matrix matrix addition: "
<< duration.count() << " seconds" << std::endl;
std::cout << eTurbAdd << "\n" << std::endl;
eTimeTurb[2] = duration.count();
start = std::chrono::high_resolution_clock::now();
for (auto ii = 0; ii < samples; ++ii) {
eXTurbAdd = 1.5 * eXTurbMat + eXTurbMat;
}
stop = std::chrono::high_resolution_clock::now();
duration = stop - start;
std::cout << "Eigen (dynamic) turbulence matrix matrix addition: "
<< duration.count() << " seconds" << std::endl;
std::cout << eXTurbAdd << "\n" << std::endl;
eXTimeTurb[2] = duration.count();
// aither
squareMatrix aFlowAdd;
squareMatrix aTurbAdd;
start = std::chrono::high_resolution_clock::now();
for (auto ii = 0; ii < samples; ++ii) {
aFlowAdd = 1.5 * aFlowMat + aFlowMat;
}
stop = std::chrono::high_resolution_clock::now();
duration = stop - start;
std::cout << "Aither flow matrix matrix addition: " << duration.count()
<< " seconds" << std::endl;
std::cout << aFlowAdd << "\n" << std::endl;
aTimeFlow[2] = duration.count();
start = std::chrono::high_resolution_clock::now();
for (auto ii = 0; ii < samples; ++ii) {
aTurbAdd = 1.5 * aTurbMat + aTurbMat;
}
stop = std::chrono::high_resolution_clock::now();
duration = stop - start;
std::cout << "Aither turbulence matrix matrix addition: "
<< duration.count() << " seconds" << std::endl;
std::cout << aTurbAdd << "\n" << std::endl;
aTimeTurb[2] = duration.count();
// test vector multiplication with scalar and addition --------------------
// eigen
Eigen::VectorXd eFlowAddV;
Eigen::Vector2d eTurbAddV;
Eigen::VectorXd eXTurbAddV;
start = std::chrono::high_resolution_clock::now();
for (auto ii = 0; ii < samples; ++ii) {
eFlowAddV = 1.5 * eFlowVec + eFlowVec;
}
stop = std::chrono::high_resolution_clock::now();
duration = stop - start;
std::cout << "Eigen flow vector vector addition: " << duration.count()
<< " seconds" << std::endl;
std::cout << eFlowAddV << "\n" << std::endl;
eTimeFlow[3] = duration.count();
start = std::chrono::high_resolution_clock::now();
for (auto ii = 0; ii < samples; ++ii) {
eTurbAddV = 1.5 * eTurbVec + eTurbVec;
}
stop = std::chrono::high_resolution_clock::now();
duration = stop - start;
std::cout << "Eigen (static) turbulence vector vector addition: "
<< duration.count() << " seconds" << std::endl;
std::cout << eTurbAddV << "\n" << std::endl;
eTimeTurb[3] = duration.count();
start = std::chrono::high_resolution_clock::now();
for (auto ii = 0; ii < samples; ++ii) {
eXTurbAddV = 1.5 * eXTurbVec + eXTurbVec;
}
stop = std::chrono::high_resolution_clock::now();
duration = stop - start;
std::cout << "Eigen (dynamic) turbulence vector vector addition: "
<< duration.count() << " seconds" << std::endl;
std::cout << eXTurbAddV << "\n" << std::endl;
eXTimeTurb[3] = duration.count();
// aither
genArray aFlowAddV;
start = std::chrono::high_resolution_clock::now();
for (auto ii = 0; ii < samples; ++ii) {
aFlowAddV = 1.5 * aFlowVec + aFlowVec;
}
stop = std::chrono::high_resolution_clock::now();
duration = stop - start;
std::cout << "Aither flow vector vector addition: " << duration.count()
<< " seconds" << std::endl;
std::cout << aFlowAddV << "\n" << std::endl;
aTimeFlow[3] = duration.count();
// in aither vectors are of size 7, so no comparison for turbulence
// test matrix inverse ---------------------------------------------------
// eigen
Eigen::MatrixXd eFlowInv;
Eigen::Matrix2d eTurbInv;
Eigen::MatrixXd eXTurbInv;
start = std::chrono::high_resolution_clock::now();
for (auto ii = 0; ii < samples; ++ii) {
eFlowInv = eFlowMat.inverse();
}
stop = std::chrono::high_resolution_clock::now();
duration = stop - start;
std::cout << "Eigen flow matrix inversion: " << duration.count()
<< " seconds" << std::endl;
std::cout << eFlowInv << "\n" << std::endl;
eTimeFlow[4] = duration.count();
start = std::chrono::high_resolution_clock::now();
for (auto ii = 0; ii < samples; ++ii) {
eTurbInv = eTurbMat.inverse();
}
stop = std::chrono::high_resolution_clock::now();
duration = stop - start;
std::cout << "Eigen (static) turbulence matrix inversion: "
<< duration.count() << " seconds" << std::endl;
std::cout << eTurbInv << "\n" << std::endl;
eTimeTurb[4] = duration.count();
start = std::chrono::high_resolution_clock::now();
for (auto ii = 0; ii < samples; ++ii) {
eXTurbInv = eXTurbMat.inverse();
}
stop = std::chrono::high_resolution_clock::now();
duration = stop - start;
std::cout << "Eigen (dynamic) turbulence matrix inversion: "
<< duration.count() << " seconds" << std::endl;
std::cout << eXTurbInv << "\n" << std::endl;
eXTimeTurb[4] = duration.count();
// aither
squareMatrix aFlowInv;
squareMatrix aTurbInv;
start = std::chrono::high_resolution_clock::now();
for (auto ii = 0; ii < samples; ++ii) {
aFlowInv = aFlowMat;
aFlowInv.Inverse();
}
stop = std::chrono::high_resolution_clock::now();
duration = stop - start;
std::cout << "Aither flow matrix inversion: " << duration.count()
<< " seconds" << std::endl;
std::cout << aFlowInv << "\n" << std::endl;
aTimeFlow[4] = duration.count();
start = std::chrono::high_resolution_clock::now();
for (auto ii = 0; ii < samples; ++ii) {
aTurbInv = aTurbMat;
aTurbInv.Inverse();
}
stop = std::chrono::high_resolution_clock::now();
duration = stop - start;
std::cout << "Aither turbulence matrix inversion: "
<< duration.count() << " seconds" << std::endl;
std::cout << aTurbInv << "\n" << std::endl;
aTimeTurb[4] = duration.count();
// print timing summary --------------------------------------------------
std::cout << "Timing Summary" << std::endl;
std::cout << std::scientific << std::setprecision(5);
std::cout << std::setw(25) << "Matrix Type";
std::cout << std::setw(15) << "MatMult" << std::setw(15) << "MatVec"
<< std::setw(15) << "MatAdd" << std::setw(15) << "VecAdd"
<< std::setw(15) << "Inverse" << std::endl;
std::cout << std::setw(25) << "Eigen 5x5 Dynamic";
std::cout << std::setw(15) << eTimeFlow[0] << std::setw(15) << eTimeFlow[1]
<< std::setw(15) << eTimeFlow[2] << std::setw(15) << eTimeFlow[3]
<< std::setw(15) << eTimeFlow[4] << std::endl;
std::cout << std::setw(25) << "Eigen 2x2 Static";
std::cout << std::setw(15) << eTimeTurb[0] << std::setw(15) << eTimeTurb[1]
<< std::setw(15) << eTimeTurb[2] << std::setw(15) << eTimeTurb[3]
<< std::setw(15) << eTimeTurb[4] << std::endl;
std::cout << std::setw(25) << "Eigen 2x2 Dynamic";
std::cout << std::setw(15) << eXTimeTurb[0] << std::setw(15) << eXTimeTurb[1]
<< std::setw(15) << eXTimeTurb[2] << std::setw(15) << eXTimeTurb[3]
<< std::setw(15) << eXTimeTurb[4] << std::endl;
std::cout << std::setw(25) << "Aither 5x5 Dynamic";
std::cout << std::setw(15) << aTimeFlow[0] << std::setw(15) << aTimeFlow[1]
<< std::setw(15) << aTimeFlow[2] << std::setw(15) << aTimeFlow[3]
<< std::setw(15) << aTimeFlow[4] << std::endl;
std::cout << std::setw(25) << "Aither 2x2 Dynamic";
std::cout << std::setw(15) << aTimeTurb[0] << std::setw(15) << aTimeTurb[1]
<< std::setw(15) << aTimeTurb[2] << std::setw(15) << aTimeTurb[3]
<< std::setw(15) << aTimeTurb[4] << std::endl;
return 0;
}
// -------------------------------------------------------------------------
// function definitions ----------------------------------------------------
Eigen::MatrixXd FillEigenMatrix(const int &size, double start,
const double &diagFactor) {
Eigen::MatrixXd matrix(size, size);
for (auto rr = 0; rr < size; ++rr) {
for (auto cc = 0; cc < size; ++cc) {
auto fac = (rr == cc) ? diagFactor : 1.0;
matrix(rr, cc) = ++start * fac;
}
}
return matrix;
}
Eigen::Matrix2d FillEigenMatrix2d(double start, const double &diagFactor) {
Eigen::Matrix2d matrix;
for (auto rr = 0; rr < 2; ++rr) {
for (auto cc = 0; cc < 2; ++cc) {
auto fac = (rr == cc) ? diagFactor : 1.0;
matrix(rr, cc) = ++start * fac;
}
}
return matrix;
}
Eigen::VectorXd FillEigenVector(const int &size, double start) {
Eigen::VectorXd vector(size);
for (auto rr = 0; rr < size; ++rr) {
vector(rr) = ++start;
}
return vector;
}
Eigen::Vector2d FillEigenVector2d(double start) {
Eigen::Vector2d vector;
for (auto rr = 0; rr < 2; ++rr) {
vector(rr) = ++start;
}
return vector;
}
squareMatrix FillAitherMatrix(const int &size, double start,
const double &diagFactor) {
squareMatrix matrix(size);
for (auto rr = 0; rr < size; ++rr) {
for (auto cc = 0; cc < size; ++cc) {
auto fac = (rr == cc) ? diagFactor : 1.0;
matrix(rr, cc) = ++start * fac;
}
}
return matrix;
}
genArray FillAitherVector(const int &size, double start) {
genArray vector(0.0);
for (auto rr = 0; rr < size; ++rr) {
vector[rr] = ++start;
}
return vector;
}
genArray FillAitherVector2d(double start) {
genArray vector(0.0);
for (auto rr = 5; rr < 7; ++rr) {
vector[rr] = ++start;
}
return vector;
}