-
Notifications
You must be signed in to change notification settings - Fork 299
/
Matrix_test.cpp
635 lines (529 loc) · 24.3 KB
/
Matrix_test.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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 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 Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
/** Sparse matrix test suite.
*
* The same suite is instanciated using different parameters: entry types
* (float/Real) and BlockMN size in CompressedRowSparse.
*/
#include <sofa/linearalgebra/EigenSparseMatrix.h>
#include <sofa/linearalgebra/SparseMatrix.h>
#include <sofa/linearalgebra/CompressedRowSparseMatrix.h>
#include <sofa/linearalgebra/FullMatrix.h>
#include <sofa/linearalgebra/FullVector.h>
#include <sofa/type/Mat.h>
#include <sofa/type/Vec.h>
#include <sofa/defaulttype/VecTypes.h>
#include <sofa/testing/NumericTest.h>
using sofa::testing::NumericTest;
#define BENCHMARK_MATRIX_PRODUCT 0
#if BENCHMARK_MATRIX_PRODUCT
#include <ctime>
using sofa::helper::system::thread::CTime;
Real get_time() {
CTime * timer;
return (Real) timer->getTime();
}
#endif
namespace sofa
{
template<class TReal, sofa::Index TNbRows, sofa::Index TNbCols, sofa::Index TBlockRows, sofa::Index TBlockCols >
struct TestSparseMatricesTraits
{
static constexpr sofa::Index NbRows = TNbRows;
static constexpr sofa::Index NbCols = TNbCols;
static constexpr sofa::Index BlockRows = TBlockRows;
static constexpr sofa::Index BlockCols = TBlockCols;
using Real = TReal;
};
/** Sparse matrix test suite.
Creates matrices of different types, sets their entries and checks that all the matrices are the same.
Perform matrix-vector products and compare the results.
*/
template <class T>
struct TestSparseMatrices : public NumericTest<typename T::Real>
{
using Inherit = NumericTest<typename T::Real>;
// Scalar type and dimensions of the matrices to test
typedef typename T::Real Real;
static const unsigned NROWS=T::NbRows; // matrix size
static const unsigned NCOLS=T::NbCols;
static const unsigned BROWS=T::BlockRows; // block size used for matrices with block-wise storage
static const unsigned BCOLS=T::BlockCols;
// Dense implementation
typedef sofa::linearalgebra::FullMatrix<Real> FullMatrix;
typedef sofa::linearalgebra::FullVector<Real> FullVector;
// Simple sparse matrix implemented using map< map< > >
typedef sofa::linearalgebra::SparseMatrix<Real> MapMatrix;
// Blockwise Compressed Sparse Row format
typedef sofa::linearalgebra::CompressedRowSparseMatrix<Real> CRSMatrixScalar;
typedef sofa::type::Mat<BROWS,BCOLS,Real> BlockMN;
typedef sofa::linearalgebra::CompressedRowSparseMatrix<BlockMN> CRSMatrixMN;
typedef sofa::type::Mat<BCOLS,BROWS,Real> BlockNM;
typedef sofa::linearalgebra::CompressedRowSparseMatrix<BlockNM> CRSMatrixNM;
typedef sofa::type::Mat<BROWS,BROWS,Real> BlockMM;
typedef sofa::linearalgebra::CompressedRowSparseMatrix<BlockMM> CRSMatrixMM;
typedef sofa::type::Mat<BCOLS,BCOLS,Real> BlockNN;
typedef sofa::linearalgebra::CompressedRowSparseMatrix<BlockNN> CRSMatrixNN;
// Implementation based on Eigen
typedef sofa::defaulttype::StdVectorTypes< sofa::type::Vec<BCOLS,Real>, sofa::type::Vec<BCOLS,Real> > InTypes;
typedef sofa::defaulttype::StdVectorTypes< sofa::type::Vec<BROWS,Real>, sofa::type::Vec<BROWS,Real> > OutTypes;
typedef sofa::linearalgebra::EigenSparseMatrix<InTypes,OutTypes> EigenBlockSparseMatrix;
typedef sofa::linearalgebra::EigenBaseSparseMatrix<Real> EigenBaseSparseMatrix;
typedef Eigen::Matrix<Real,Eigen::Dynamic,Eigen::Dynamic> EigenDenseMatrix;
typedef Eigen::Matrix<Real,Eigen::Dynamic,1> EigenDenseVec;
// Regular Mat implementation
typedef sofa::type::Mat<NROWS,NCOLS,Real> MatMN;
typedef sofa::type::Mat<NCOLS,NROWS,Real> MatNM;
typedef sofa::type::Mat<NROWS,NROWS,Real> MatMM;
typedef sofa::type::Mat<NCOLS,NCOLS,Real> MatNN;
typedef sofa::type::Vec<NROWS,Real> VecM;
typedef sofa::type::Vec<NCOLS,Real> VecN;
// The matrices used in the tests
CRSMatrixMN crs1,crs2;
CRSMatrixScalar crsScalar;
FullMatrix fullMat;
MapMatrix mapMat;
EigenBlockSparseMatrix eiBlock1,eiBlock2,eiBlock3;
EigenBaseSparseMatrix eiBase;
// matrices for multiplication test
CRSMatrixScalar crsScalarMultiplier;
CRSMatrixScalar crsScalarMultiplication;
CRSMatrixScalar crsScalarTransposeMultiplication;
CRSMatrixNM crsMultiplier;
CRSMatrixMM crsMultiplication;
CRSMatrixNN crsTransposeMultiplication;
FullMatrix fullMultiplier;
FullMatrix fullMultiplication;
FullMatrix fullTransposeMultiplication;
MatMN mat;
MatNM matMultiplier;
MatMM matMultiplication;
MatNN matTransposeMultiplication;
EigenBaseSparseMatrix eiBaseMultiplier;
EigenBaseSparseMatrix eiBaseMultiplication;
EigenDenseMatrix eiDenseMultiplier;
EigenDenseMatrix eiDenseMultiplication;
// The vectors used in the tests
FullVector fullVec_ncols;
FullVector fullVec_nrows_reference,fullVec_nrows_result;
VecM vecM;
VecN vecN;
EigenDenseVec eiVecM, eiVecN;
/// generating a random Mat
/// if sparse=0 a lot a null entries will be created
template<Size nbrows,Size nbcols>
static void generateRandomMat( type::Mat<nbrows,nbcols,Real>& mat, bool sparse=false )
{
for( Size j=0; j<mat.nbCols; j++)
{
for( Size i=0; i<mat.nbLines; i++)
{
Real random = Real(helper::drand(1));
if( sparse && random > -0.5 && random < 0.5 ) random = 0;
mat(i,j)=random;
}
}
}
/// filling a BaseMatrix from a given Mat
template<Size nbrows,Size nbcols>
static void copyFromMat( linearalgebra::BaseMatrix& baseMat, const type::Mat<nbrows,nbcols,Real>& mat )
{
baseMat.resize(mat.nbLines,mat.nbCols);
for( Size j=0; j<mat.nbCols; j++)
{
for( Size i=0; i<mat.nbLines; i++)
{
if( !baseMat.isSparse() || mat(i,j)!=0 ) baseMat.add( i, j, mat(i,j) );
}
}
baseMat.compress();
}
/// Create the context for the matrix tests.
TestSparseMatrices()
{
//std::cout<<"Matrix_test "<<NumRows<<" "<<NumCols<<" "<<BlockRows<<" "<<BlockCols<<std::endl << "seed number = " << BaseSofa_test::seed<<std::endl;
// resize and fill the matrices
generateRandomMat( mat, true );
copyFromMat( crs1, mat );
copyFromMat( crs2, mat );
copyFromMat(crsScalar, mat);
copyFromMat( fullMat, mat );
copyFromMat( mapMat, mat );
copyFromMat( eiBlock1, mat );
copyFromMat( eiBlock2, mat );
copyFromMat( eiBase, mat );
eiBlock3.copyFrom(crs1);
// resize and fill the vectors
fullVec_ncols.resize(NCOLS);
fullVec_nrows_reference.resize(NROWS);
fullVec_nrows_result.resize(NROWS);
eiVecM.resize(NROWS);
eiVecN.resize(NCOLS);
for( unsigned i=0; i<NCOLS; i++)
{
fullVec_ncols[i] = (Real)i;
vecN[i] = (Real)i;
eiVecN[i] = (Real)i;
}
fullMat.mul(fullVec_nrows_reference,fullVec_ncols); // cerr<<"MatrixTest: vref = " << vref << endl;
vecM = mat * vecN;
// matrix multiplication
generateRandomMat( matMultiplier, true );
copyFromMat(crsScalarMultiplier, matMultiplier);
copyFromMat( crsMultiplier, matMultiplier );
copyFromMat( fullMultiplier, matMultiplier );
copyFromMat( eiBaseMultiplier, matMultiplier );
eiDenseMultiplier = Eigen::Map< EigenDenseMatrix >( &(matMultiplier.transposed())[0][0], NCOLS, NROWS ); // need to transpose because EigenDenseMatrix is ColMajor
matMultiplication = mat * matMultiplier;
crsScalar.mul(crsScalarMultiplication, crsScalarMultiplier);
crs1.mul( crsMultiplication, crsMultiplier );
fullMat.mul( fullMultiplication, fullMultiplier );
eiBase.mul_MT( eiBaseMultiplication, eiBaseMultiplier ); // sparse x sparse
eiBase.mul_MT( eiDenseMultiplication, eiDenseMultiplier ); // sparse x dense
matTransposeMultiplication = mat.multTranspose( mat );
crsScalar.mulTranspose(crsScalarTransposeMultiplication, crsScalar);
crs1.mulTranspose( crsTransposeMultiplication, crs1 );
fullMat.mulT( fullTransposeMultiplication, fullMat );
}
/** Check that EigenMatrix update works as well as direct init. Return true if the test succeeds.*/
bool checkEigenMatrixUpdate()
{
// fill two matrices with the same values, one directly (in order), one in two passes, then compare their values
EigenBlockSparseMatrix a,b;
a.resize(NROWS,NCOLS);
b.resize(NROWS,NCOLS);
for( unsigned i=0; i<NROWS; i++)
{
a.beginRow(i);
for( unsigned j=0; j<NCOLS; j++)
{
Real valij = i*NCOLS+j;
a.insertBack(i,j,valij);
if( i==j )
b.add(i,j,valij);
}
}
a.compress();
b.compress();
// second pass for b. Some values are set with the right value, some with the wrong value, some are not set
for( unsigned j=0; j<NCOLS; j++)
{
for( unsigned i=0; i<NROWS; i++)
{
Real valij = i*NCOLS+j;
if( i!=j )
b.add(i,j,valij);
}
}
b.compress();
return Inherit::matrixMaxDiff(a,b) < 100 * Inherit::epsilon();
}
/** Check the filling of EigenMatrix per rows of blocks. */
void checkEigenMatrixBlockRowFilling()
{
EigenBlockSparseMatrix mb;
FullMatrix ma;
unsigned br=3, bc=3;
ma.resize(br*BROWS,bc*BCOLS);
// building with unordered blocks
mb.resizeBlocks(br,bc);
for( unsigned i=0; i<br; i++ )
{
mb.beginBlockRow(i);
if( i%2==0 ) // leave some rows empty
{
for( int j=bc-1; j>=0; j--) // set the blocs in reverse order, for fun.
{
// create a block and give it some value
BlockMN b;
for( unsigned k=0; k<BROWS && k<BCOLS; k++ ){
b[k][k] = (Real)i+j;
ma.set(i*BROWS+k, j*BCOLS+k, i+j);
}
// insert the block in the matrix
mb.createBlock(j,b);
}
}
mb.endBlockRow();
}
mb.compress();
ASSERT_LT( Inherit::matrixMaxDiff(ma,mb), 100*Inherit::epsilon() );
// building with ordered blocks
mb.resizeBlocks(br,bc);
for( unsigned i=0; i<br; i++ )
{
mb.beginBlockRow(i);
if( i%2==0 ) // leave some rows empty
{
for( unsigned j=0 ; j<bc; ++j ) // set the blocs in column order
{
// create a block and give it some value
BlockMN b;
for( unsigned k=0; k<BROWS && k<BCOLS; k++ ){
b[k][k] = (Real)i+j;
}
// insert the block in the matrix
mb.createBlock(j,b);
}
}
mb.endSortedBlockRow();
}
mb.compress();
ASSERT_LT( Inherit::matrixMaxDiff(ma,mb), 100*Inherit::epsilon() );
// building with scheduled block additions
mb.resizeBlocks(br,bc);
for( unsigned i=0; i<br; i++ )
{
if( i%2==0 ) // leave some rows empty
{
for( int j=bc-1; j>=0; j--) // set the blocs in reverse order, for fun.
{
// create a block and give it some value
BlockMN b;
for( unsigned k=0; k<BROWS && k<BCOLS; k++ ){
b[k][k] = (Real)i+j;
}
// insert the block in the matrix
mb.addBlock(i,j,b);
}
}
}
mb.compress();
ASSERT_LT( Inherit::matrixMaxDiff(ma,mb), 100*Inherit::epsilon() );
// building with one block per row
ma.clear();
mb.resizeBlocks(br,bc);
for( unsigned i=0; i<br; i++ )
{
if( i%2==0 ) // leave some rows empty
{
// create a block and give it some value
BlockMN b;
for( unsigned k=0; k<BROWS && k<BCOLS; k++ ){
b[k][k] = (Real)i;
ma.set(i*BROWS+k, i*BCOLS+k, i);
}
// insert the block in the matrix
mb.insertBackBlock(i,i,b);
}
else
{
// empty lines
mb.beginBlockRow(i);
mb.endSortedBlockRow();
}
}
mb.compress();
ASSERT_LT( Inherit::matrixMaxDiff(ma,mb), 100*Inherit::epsilon() );
}
bool checkEigenMatrixBlockFromCompressedRowSparseMatrix()
{
return Inherit::matrixMaxDiff(crs1,eiBlock3) < 100*Inherit::epsilon();
}
bool checkEigenDenseMatrix()
{
if( matMultiplier.nbCols != eiDenseMultiplier.cols() || matMultiplier.nbLines != eiDenseMultiplier.rows() ) return false;
for( sofa::Index j=0; j<matMultiplier.nbCols; j++)
{
for( sofa::Index i=0; i<matMultiplier.nbLines; i++)
{
if( matMultiplier(i,j) != eiDenseMultiplier(i,j) ) return false;
}
}
return true;
}
};
using TestSparseMatricesImplementations = ::testing::Types<
TestSparseMatricesTraits<SReal, 4, 8, 4, 8>,
TestSparseMatricesTraits<SReal, 4, 8, 4, 2>,
TestSparseMatricesTraits<SReal, 4, 8, 1, 8>,
TestSparseMatricesTraits<SReal, 4, 8, 2, 8>,
TestSparseMatricesTraits<SReal, 4, 8, 2, 4>,
TestSparseMatricesTraits<SReal, 24, 24, 3, 3>
>;
TYPED_TEST_SUITE(TestSparseMatrices, TestSparseMatricesImplementations);
// ==============================
// Set/get value tests
TYPED_TEST(TestSparseMatrices, set_fullMat ) { ASSERT_LT( this->matrixMaxDiff(this->mat,this->fullMat), 100*this->epsilon() ); }
TYPED_TEST(TestSparseMatrices, set_crs_scalar ) { ASSERT_LT( this->matrixMaxDiff(this->fullMat,this->crsScalar ), 100*this->epsilon() ); }
TYPED_TEST(TestSparseMatrices, set_crs1 ) { ASSERT_LT( this->matrixMaxDiff(this->fullMat,this->crs1), 100*this->epsilon() ); }
TYPED_TEST(TestSparseMatrices, set_crs2 ) { ASSERT_LT( this->matrixMaxDiff(this->fullMat,this->crs2), 100*this->epsilon() ); }
TYPED_TEST(TestSparseMatrices, set_mapMat ) { ASSERT_LT( this->matrixMaxDiff(this->fullMat,this->mapMat), 100*this->epsilon() ); }
TYPED_TEST(TestSparseMatrices, set_eiBlock1 ) { ASSERT_LT( this->matrixMaxDiff(this->fullMat,this->eiBlock1), 100*this->epsilon() ); }
TYPED_TEST(TestSparseMatrices, set_eiBlock2 ) { ASSERT_LT( this->matrixMaxDiff(this->fullMat,this->eiBlock2), 100*this->epsilon() ); }
TYPED_TEST(TestSparseMatrices, set_eiBase ) { ASSERT_LT( this->matrixMaxDiff(this->fullMat,this->eiBase), 100*this->epsilon() ); }
TYPED_TEST(TestSparseMatrices, eigenMatrix_update ) { ASSERT_TRUE( this->checkEigenMatrixUpdate() ); }
TYPED_TEST(TestSparseMatrices, eigenMatrix_block_row_filling ) { this->checkEigenMatrixBlockRowFilling(); }
TYPED_TEST(TestSparseMatrices, eigenMatrixBlockFromCompressedRowSparseMatrix ) { ASSERT_TRUE( this->checkEigenMatrixBlockFromCompressedRowSparseMatrix() ); }
TYPED_TEST(TestSparseMatrices, eigenMapToDenseMatrix ) { ASSERT_TRUE( this->checkEigenDenseMatrix() ); }
// ==============================
// Matrix-Vector product tests
TYPED_TEST(TestSparseMatrices, set_fullVec_nrows_reference )
{
ASSERT_LT(this->vectorMaxDiff(this->vecM,this->fullVec_nrows_reference), this->epsilon() );
}
TYPED_TEST(TestSparseMatrices, fullMat_vector_product )
{
// fullMat.opMulV(&fullVec_nrows_result,&fullVec_ncols);
this->fullVec_nrows_result = this->fullMat * this->fullVec_ncols;
ASSERT_LT(this->vectorMaxDiff(this->fullVec_nrows_reference,this->fullVec_nrows_result), this->epsilon() );
}
TYPED_TEST(TestSparseMatrices, mapMat_vector_product )
{
// mapMat.opMulV(&fullVec_nrows_result,&fullVec_ncols);
this->fullVec_nrows_result = this->mapMat * this->fullVec_ncols;
ASSERT_LT(this->vectorMaxDiff(this->fullVec_nrows_reference,this->fullVec_nrows_result), this->epsilon() );
}
TYPED_TEST(TestSparseMatrices, eiBlock1_vector_product )
{
// eiBlock1.opMulV(&fullVec_nrows_result,&fullVec_ncols);
// eiBlock1.multVector(fullVec_nrows_result,fullVec_ncols);
this->fullVec_nrows_result = this->eiBlock1 * this->fullVec_ncols;
ASSERT_LT(this->vectorMaxDiff(this->fullVec_nrows_reference,this->fullVec_nrows_result), this->epsilon() );
}
TYPED_TEST(TestSparseMatrices, crs1_vector_product )
{
// EXPECT_TRUE(NROWS%BROWS==0 && NCOLS%BCOLS==0) << "Error: CompressedRowSparseMatrix * Vector crashes when the size of the matrix is not a multiple of the size of the matrix blocks. Aborting this test, and reporting a failure."; // otherwise the product crashes
// crs1.opMulV(&fullVec_nrows_result,&fullVec_ncols);
this->fullVec_nrows_result = this->crs1 * this->fullVec_ncols;
ASSERT_LT(this->vectorMaxDiff(this->fullVec_nrows_reference,this->fullVec_nrows_result), this->epsilon() );
}
// ==============================
// Matrix product tests
TYPED_TEST(TestSparseMatrices, full_matrix_product ) { ASSERT_LT( this->matrixMaxDiff(this->matMultiplication,this->fullMultiplication), 100*this->epsilon() ); }
TYPED_TEST(TestSparseMatrices, crs_scalar_matrix_product ) { ASSERT_LT( this->matrixMaxDiff(this->fullMultiplication,this->crsScalarMultiplication), 100*this->epsilon() ); }
TYPED_TEST(TestSparseMatrices, crs_matrix_product ) { ASSERT_LT( this->matrixMaxDiff(this->fullMultiplication,this->crsMultiplication), 100*this->epsilon() ); }
TYPED_TEST(TestSparseMatrices, EigenBase_matrix_product ) { ASSERT_LT( this->matrixMaxDiff(this->fullMultiplication,this->eiBaseMultiplication), 100*this->epsilon() ); }
//TYPED_TEST(TestSparseMatrices, EigenSparseDense_matrix_product ) { ASSERT_TRUE( EigenDenseMatrix(this->eiBaseMultiplication.compressedMatrix) == this->eiDenseMultiplication ); }
TYPED_TEST(TestSparseMatrices, full_matrix_transposeproduct ) { ASSERT_LT( this->matrixMaxDiff(this->matTransposeMultiplication,this->fullTransposeMultiplication), 100*this->epsilon() ); }
TYPED_TEST(TestSparseMatrices, crs_scalar_matrix_transposeproduct ) { ASSERT_LT( this->matrixMaxDiff(this->fullTransposeMultiplication,this->crsScalarTransposeMultiplication), 100*this->epsilon() ); }
TYPED_TEST(TestSparseMatrices, crs_matrix_transposeproduct ) { ASSERT_LT( this->matrixMaxDiff(this->fullTransposeMultiplication,this->crsTransposeMultiplication), 100*this->epsilon() ); }
// Matrix addition
TYPED_TEST(TestSparseMatrices, crs_matrix_addition )
{
this->crs2 = this->crs1 + this->crs1;
ASSERT_LT( this->matrixMaxDiff(this->mat*2,this->crs2), 100*this->epsilon() );
this->crs2 += this->crs1;
ASSERT_LT( this->matrixMaxDiff(this->mat*3,this->crs2), 100*this->epsilon() );
this->crs2 -= this->crs1;
ASSERT_LT( this->matrixMaxDiff(this->mat*2,this->crs2), 100*this->epsilon() );
}
#if BENCHMARK_MATRIX_PRODUCT
///// product timing
typedef TestSparseMatrices<Real,360,300,3,3> TsProductTimings;
TEST_F(TsProductTimings, benchmark )
{
msg_info()<<"=== Matrix-Matrix Products:"<<std::endl;
Real start, stop;
matMultiplication.clear();
start = get_time();
matMultiplication = mat * matMultiplier;
stop = get_time();
msg_info()<<"Mat:\t\t"<<stop-start<<" (ms)"<<std::endl;
fullMultiplication.clear();
start = get_time();
fullMat.mul( fullMultiplication, fullMultiplier );
stop = get_time();
msg_info()<<"Full:\t\t"<<stop-start<<" (ms)"<<std::endl;
crsMultiplication.clear();
start = get_time();
crs1.mul( crsMultiplication, crsMultiplier );
stop = get_time();
msg_info()<<"CRS:\t\t"<<stop-start<<" (ms)"<<std::endl;
eiBaseMultiplication.clear();
start = get_time();
eiBase.mul( eiBaseMultiplication, eiBaseMultiplier );
stop = get_time();
msg_info()<<"Eigen Base ST:\t\t"<<stop-start<<" (ms)"<<std::endl;
#ifdef _OPENMP
eiBaseMultiplication.clear();
start = get_time();
eiBase.mul_MT( eiBaseMultiplication, eiBaseMultiplier );
stop = get_time();
msg_info()<<"Eigen Base MT:\t\t"<<stop-start<<" (ms)"<<std::endl;
#endif
start = get_time();
eiDenseMultiplication = eiBase.compressedMatrix * eiDenseMultiplier;
stop = get_time();
msg_info()<<"Eigen Sparse*Dense:\t\t"<<stop-start<<" (ms)"<<std::endl;
#ifdef _OPENMP
start = get_time();
eiDenseMultiplication.noalias() = component::linearsolver::mul_EigenSparseDenseMatrix_MT( eiBase.compressedMatrix, eiDenseMultiplier, omp_get_max_threads()/2 );
stop = get_time();
msg_info()<<"Eigen Sparse*Dense MT:\t\t"<<stop-start<<" (ms)"<<std::endl;
#endif
msg_info()<<"=== Eigen Matrix-Vector Products:"<<std::endl;
unsigned nbrows = 100, nbcols;
msg_info()<<"=== nb rows:"<<nbrows<<std::endl;
for( int j=1; j<300 ; j+=30 )
{
nbcols = 100 * j;
msg_info()<<"=== nb cols:"<<nbcols<<std::endl;
Eigen::SparseMatrix<SReal,Eigen::RowMajor> A;
A.resize(nbrows,nbcols);
#define NBCOLSRHS 1
Eigen::Matrix<SReal, Eigen::Dynamic, NBCOLSRHS> res, rhs;
rhs.resize(nbcols,NBCOLSRHS);
res.resize(nbrows,NBCOLSRHS);
for( unsigned j=0; j<nbcols; j++)
{
Real random = Real(helper::drand(1));
for( unsigned i=0; i<NBCOLSRHS; i++)
rhs.coeffRef(j,i) = random;
for( unsigned i=0; i<nbrows; i++)
{
if( random > -0.5 && random < 0.5 ) A.coeffRef(i,j)=random;
}
}
Real min=std::numeric_limits<Real>::max(), max=0, sum=0;
for( int i=0; i<100 ; ++i )
{
start = get_time();
res.noalias() = A * rhs;
stop = get_time();
Real current = stop-start;
sum+=current;
if( current<min ) min=current;
if( current>max ) max=current;
}
msg_info()<<"ST: "<<sum/100.0<<" "<<min<<" "<<max<<std::endl;
#ifdef _OPENMP
min=std::numeric_limits<Real>::max(), max=0, sum=0;
for( int i=0; i<100 ; ++i )
{
start = get_time();
// res.noalias() = typename Eigen::SparseDenseProductReturnType_MT<Eigen::SparseMatrix<SReal,Eigen::RowMajor>,Eigen::Matrix<SReal, Eigen::Dynamic, 1> >::Type( A.derived(), rhs.derived() );
// component::linearsolver::mul_EigenSparseDenseMatrix_MT( res, A, rhs );
res.noalias() = component::linearsolver::mul_EigenSparseDenseMatrix_MT( A, rhs );
stop = get_time();
Real current = stop-start;
sum+=current;
if( current<min ) min=current;
if( current>max ) max=current;
}
msg_info()<<"MT: "<<sum/100.0<<" "<<min<<" "<<max<<std::endl;
#endif
}
ASSERT_TRUE( true );
}
#endif
}// namespace sofa