nomalloc.cpp File Reference
#include "main.h"
#include <Eigen/Cholesky>
#include <Eigen/Eigenvalues>
#include <Eigen/LU>
#include <Eigen/QR>
#include <Eigen/SVD>

Macros

#define EIGEN_STACK_ALLOCATION_LIMIT   0
 
#define EIGEN_RUNTIME_NO_MALLOC
 

Functions

template<typename MatrixType >
void nomalloc (const MatrixType &m)
 
template<typename Scalar >
void ctms_decompositions ()
 
void test_zerosized ()
 
template<typename MatrixType >
void test_reference (const MatrixType &m)
 
 EIGEN_DECLARE_TEST (nomalloc)
 

Macro Definition Documentation

◆ EIGEN_RUNTIME_NO_MALLOC

#define EIGEN_RUNTIME_NO_MALLOC

◆ EIGEN_STACK_ALLOCATION_LIMIT

#define EIGEN_STACK_ALLOCATION_LIMIT   0

Function Documentation

◆ ctms_decompositions()

template<typename Scalar >
void ctms_decompositions ( )
89  {
90  const int maxSize = 16;
91  const int size = 12;
92 
94 
96 
97  typedef Eigen::Matrix<std::complex<Scalar>, Eigen::Dynamic, Eigen::Dynamic, 0, maxSize, maxSize> ComplexMatrix;
98 
99  const Matrix A(Matrix::Random(size, size)), B(Matrix::Random(size, size));
100  Matrix X(size, size);
101  const ComplexMatrix complexA(ComplexMatrix::Random(size, size));
102  const Matrix saA = A.adjoint() * A;
103  const Vector b(Vector::Random(size));
104  Vector x(size);
105 
106  // Cholesky module
108  LLT.compute(A);
109  X = LLT.solve(B);
110  x = LLT.solve(b);
112  LDLT.compute(A);
113  X = LDLT.solve(B);
114  x = LDLT.solve(b);
115 
116  // Eigenvalues module
118  hessDecomp.compute(complexA);
120  cSchur.compute(complexA);
122  cEigSolver.compute(complexA);
123  Eigen::EigenSolver<Matrix> eigSolver;
124  eigSolver.compute(A);
126  saEigSolver.compute(saA);
128  tridiag.compute(saA);
129 
130  // LU module
132  ppLU.compute(A);
133  X = ppLU.solve(B);
134  x = ppLU.solve(b);
136  fpLU.compute(A);
137  X = fpLU.solve(B);
138  x = fpLU.solve(b);
139 
140  // QR module
142  hQR.compute(A);
143  X = hQR.solve(B);
144  x = hQR.solve(b);
146  cpQR.compute(A);
147  X = cpQR.solve(B);
148  x = cpQR.solve(b);
150  fpQR.compute(A);
151  // FIXME X = fpQR.solve(B);
152  x = fpQR.solve(b);
153 
154  // SVD module
156  jSVD.compute(A);
157 }
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
Scalar * b
Definition: benchVecAdd.cpp:17
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:47
Matrix< SCALARB, Dynamic, Dynamic, opt_B > B
Definition: bench_gemm.cpp:48
Householder rank-revealing QR decomposition of a matrix with column-pivoting.
Definition: ColPivHouseholderQR.h:54
ColPivHouseholderQR & compute(const EigenBase< InputType > &matrix)
Computes eigenvalues and eigenvectors of general complex matrices.
Definition: ComplexEigenSolver.h:49
ComplexEigenSolver & compute(const EigenBase< InputType > &matrix, bool computeEigenvectors=true)
Computes eigendecomposition of given matrix.
Performs a complex Schur decomposition of a real or complex square matrix.
Definition: ComplexSchur.h:56
Computes eigenvalues and eigenvectors of general matrices.
Definition: EigenSolver.h:68
EigenSolver & compute(const EigenBase< InputType > &matrix, bool computeEigenvectors=true)
Computes eigendecomposition of given matrix.
Householder rank-revealing QR decomposition of a matrix with full pivoting.
Definition: FullPivHouseholderQR.h:63
FullPivHouseholderQR & compute(const EigenBase< InputType > &matrix)
LU decomposition of a matrix with complete pivoting, and related features.
Definition: FullPivLU.h:63
FullPivLU & compute(const EigenBase< InputType > &matrix)
Definition: FullPivLU.h:123
Reduces a square matrix to Hessenberg form by an orthogonal similarity transformation.
Definition: HessenbergDecomposition.h:61
HessenbergDecomposition & compute(const EigenBase< InputType > &matrix)
Computes Hessenberg decomposition of given matrix.
Definition: HessenbergDecomposition.h:147
Householder QR decomposition of a matrix.
Definition: HouseholderQR.h:59
HouseholderQR & compute(const EigenBase< InputType > &matrix)
Definition: HouseholderQR.h:174
Two-sided Jacobi SVD decomposition of a rectangular matrix.
Definition: JacobiSVD.h:500
JacobiSVD & compute(const MatrixType &matrix)
Method performing the decomposition of given matrix. Computes Thin/Full unitaries U/V if specified us...
Definition: JacobiSVD.h:591
Robust Cholesky decomposition of a matrix with pivoting.
Definition: LDLT.h:63
LDLT & compute(const EigenBase< InputType > &matrix)
Standard Cholesky decomposition (LL^T) of a matrix and associated features.
Definition: LLT.h:70
LLT & compute(const EigenBase< InputType > &matrix)
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
LU decomposition of a matrix with partial pivoting, and related features.
Definition: PartialPivLU.h:77
PartialPivLU & compute(const EigenBase< InputType > &matrix)
Definition: PartialPivLU.h:130
Computes eigenvalues and eigenvectors of selfadjoint matrices.
Definition: SelfAdjointEigenSolver.h:82
const Solve< Derived, Rhs > solve(const MatrixBase< Rhs > &b) const
Definition: SolverBase.h:106
Tridiagonal decomposition of a selfadjoint matrix.
Definition: Tridiagonalization.h:66
Tridiagonalization & compute(const EigenBase< InputType > &matrix)
Computes tridiagonal decomposition of given matrix.
Definition: Tridiagonalization.h:154
Definition: matrices.h:74
Matrix< Type, Size, 1 > Vector
\cpp11 SizeƗ1 vector of type Type.
Definition: Eigen/Eigen/src/Core/Matrix.h:515
#define X
Definition: icosphere.cpp:20
const int Dynamic
Definition: Constants.h:25
list x
Definition: plotDoE.py:28

References b, Eigen::LDLT< MatrixType_, UpLo_ >::compute(), Eigen::LLT< MatrixType_, UpLo_ >::compute(), Eigen::HessenbergDecomposition< MatrixType_ >::compute(), Eigen::Tridiagonalization< MatrixType_ >::compute(), Eigen::FullPivLU< MatrixType_, PermutationIndex_ >::compute(), Eigen::PartialPivLU< MatrixType_, PermutationIndex_ >::compute(), Eigen::ColPivHouseholderQR< MatrixType_, PermutationIndex_ >::compute(), Eigen::FullPivHouseholderQR< MatrixType_, PermutationIndex_ >::compute(), Eigen::HouseholderQR< MatrixType_ >::compute(), Eigen::ComplexEigenSolver< MatrixType_ >::compute(), Eigen::EigenSolver< MatrixType_ >::compute(), Eigen::ComplexSchur< MatrixType_ >::compute(), Eigen::SelfAdjointEigenSolver< MatrixType_ >::compute(), Eigen::JacobiSVD< MatrixType_, Options_ >::compute(), Eigen::Dynamic, size, Eigen::SolverBase< Derived >::solve(), plotDoE::x, and X.

◆ EIGEN_DECLARE_TEST()

EIGEN_DECLARE_TEST ( nomalloc  )
206  {
207  // create some dynamic objects
208  Eigen::MatrixXd M1 = MatrixXd::Random(3, 3);
209  Ref<const MatrixXd> R1 = 2.0 * M1; // Ref requires temporary
210 
211  // from here on prohibit malloc:
212  Eigen::internal::set_is_malloc_allowed(false);
213 
214  // check that our operator new is indeed called:
215  VERIFY_RAISES_ASSERT(MatrixXd dummy(MatrixXd::Random(3, 3)));
217  CALL_SUBTEST_2(nomalloc(Matrix4d()));
219 
220  // Check decomposition modules with dynamic matrices that have a known compile-time max size (ctms)
221  CALL_SUBTEST_4(ctms_decompositions<float>());
222 
224 
227  CALL_SUBTEST_8(Ref<MatrixXd> R2 = M1.topRows<2>(); test_reference(R2));
228 
229  // freeing is now possible
230  Eigen::internal::set_is_malloc_allowed(true);
231 }
MatrixXf M1
Definition: Tutorial_SlicingCol.cpp:1
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:264
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:329
void test_zerosized()
Definition: nomalloc.cpp:159
void nomalloc(const MatrixType &m)
Definition: nomalloc.cpp:24
void test_reference(const MatrixType &m)
Definition: nomalloc.cpp:173
#define CALL_SUBTEST_6(FUNC)
Definition: split_test_helper.h:34
#define CALL_SUBTEST_3(FUNC)
Definition: split_test_helper.h:16
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
#define CALL_SUBTEST_8(FUNC)
Definition: split_test_helper.h:46
#define CALL_SUBTEST_5(FUNC)
Definition: split_test_helper.h:28
#define CALL_SUBTEST_2(FUNC)
Definition: split_test_helper.h:10
#define CALL_SUBTEST_7(FUNC)
Definition: split_test_helper.h:40
#define CALL_SUBTEST_4(FUNC)
Definition: split_test_helper.h:22

References CALL_SUBTEST_1, CALL_SUBTEST_2, CALL_SUBTEST_3, CALL_SUBTEST_4, CALL_SUBTEST_5, CALL_SUBTEST_6, CALL_SUBTEST_7, CALL_SUBTEST_8, M1, nomalloc(), test_reference(), test_zerosized(), and VERIFY_RAISES_ASSERT.

◆ nomalloc()

template<typename MatrixType >
void nomalloc ( const MatrixType m)
24  {
25  /* this test check no dynamic memory allocation are issued with fixed-size matrices
26  */
27  typedef typename MatrixType::Scalar Scalar;
28 
29  Index rows = m.rows();
30  Index cols = m.cols();
31 
32  MatrixType m1 = MatrixType::Random(rows, cols), m2 = MatrixType::Random(rows, cols), m3(rows, cols);
33 
34  Scalar s1 = internal::random<Scalar>();
35 
36  Index r = internal::random<Index>(0, rows - 1), c = internal::random<Index>(0, cols - 1);
37 
38  VERIFY_IS_APPROX((m1 + m2) * s1, s1 * m1 + s1 * m2);
39  VERIFY_IS_APPROX((m1 + m2)(r, c), (m1(r, c)) + (m2(r, c)));
40  VERIFY_IS_APPROX(m1.cwiseProduct(m1.block(0, 0, rows, cols)), (m1.array() * m1.array()).matrix());
41  VERIFY_IS_APPROX((m1 * m1.transpose()) * m2, m1 * (m1.transpose() * m2));
42 
43  m2.col(0).noalias() = m1 * m1.col(0);
44  m2.col(0).noalias() -= m1.adjoint() * m1.col(0);
45  m2.col(0).noalias() -= m1 * m1.row(0).adjoint();
46  m2.col(0).noalias() -= m1.adjoint() * m1.row(0).adjoint();
47 
48  m2.row(0).noalias() = m1.row(0) * m1;
49  m2.row(0).noalias() -= m1.row(0) * m1.adjoint();
50  m2.row(0).noalias() -= m1.col(0).adjoint() * m1;
51  m2.row(0).noalias() -= m1.col(0).adjoint() * m1.adjoint();
53 
54  m2.col(0).noalias() = m1.template triangularView<Upper>() * m1.col(0);
55  m2.col(0).noalias() -= m1.adjoint().template triangularView<Upper>() * m1.col(0);
56  m2.col(0).noalias() -= m1.template triangularView<Upper>() * m1.row(0).adjoint();
57  m2.col(0).noalias() -= m1.adjoint().template triangularView<Upper>() * m1.row(0).adjoint();
58 
59  m2.row(0).noalias() = m1.row(0) * m1.template triangularView<Upper>();
60  m2.row(0).noalias() -= m1.row(0) * m1.adjoint().template triangularView<Upper>();
61  m2.row(0).noalias() -= m1.col(0).adjoint() * m1.template triangularView<Upper>();
62  m2.row(0).noalias() -= m1.col(0).adjoint() * m1.adjoint().template triangularView<Upper>();
64 
65  m2.col(0).noalias() = m1.template selfadjointView<Upper>() * m1.col(0);
66  m2.col(0).noalias() -= m1.adjoint().template selfadjointView<Upper>() * m1.col(0);
67  m2.col(0).noalias() -= m1.template selfadjointView<Upper>() * m1.row(0).adjoint();
68  m2.col(0).noalias() -= m1.adjoint().template selfadjointView<Upper>() * m1.row(0).adjoint();
69 
70  m2.row(0).noalias() = m1.row(0) * m1.template selfadjointView<Upper>();
71  m2.row(0).noalias() -= m1.row(0) * m1.adjoint().template selfadjointView<Upper>();
72  m2.row(0).noalias() -= m1.col(0).adjoint() * m1.template selfadjointView<Upper>();
73  m2.row(0).noalias() -= m1.col(0).adjoint() * m1.adjoint().template selfadjointView<Upper>();
75 
76  m2.template selfadjointView<Lower>().rankUpdate(m1.col(0), -1);
77  m2.template selfadjointView<Upper>().rankUpdate(m1.row(0), -1);
78  m2.template selfadjointView<Lower>().rankUpdate(m1.col(0), m1.col(0)); // rank-2
79 
80  // The following fancy matrix-matrix products are not safe yet regarding static allocation
81  m2.template selfadjointView<Lower>().rankUpdate(m1);
82  m2 += m2.template triangularView<Upper>() * m1;
83  m2.template triangularView<Upper>() = m2 * m2;
84  m1 += m1.template selfadjointView<Lower>() * m2;
86 }
Matrix3d m1
Definition: IOFormat.cpp:2
MatrixType m2(n_dims)
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
SCALAR Scalar
Definition: bench_gemm.cpp:45
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:13
int * m
Definition: level2_cplx_impl.h:294
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
r
Definition: UniformPSDSelfTest.py:20
int c
Definition: calibrate.py:100

References calibrate::c, cols, m, m1, m2(), UniformPSDSelfTest::r, rows, and VERIFY_IS_APPROX.

Referenced by EIGEN_DECLARE_TEST().

◆ test_reference()

template<typename MatrixType >
void test_reference ( const MatrixType m)
173  {
174  typedef typename MatrixType::Scalar Scalar;
175  enum { Flag = MatrixType::IsRowMajor ? Eigen::RowMajor : Eigen::ColMajor };
176  enum { TransposeFlag = !MatrixType::IsRowMajor ? Eigen::RowMajor : Eigen::ColMajor };
177  Index rows = m.rows(), cols = m.cols();
180  // Dynamic reference:
182  typedef Eigen::Ref<const MatrixXT> RefT;
183 
184  Ref r1(m);
185  Ref r2(m.block(rows / 3, cols / 4, rows / 2, cols / 2));
186  RefT r3(m.transpose());
187  RefT r4(m.topLeftCorner(rows / 2, cols / 2).transpose());
188 
189  VERIFY_RAISES_ASSERT(RefT r5(m));
190  VERIFY_RAISES_ASSERT(Ref r6(m.transpose()));
191  VERIFY_RAISES_ASSERT(Ref r7(Scalar(2) * m));
192 
193  // Copy constructors shall also never malloc
194  Ref r8 = r1;
195  RefT r9 = r3;
196 
197  // Initializing from a compatible Ref shall also never malloc
199 
200  // Initializing from an incompatible Ref will malloc:
201  typedef Eigen::Ref<const MatrixX, Aligned> RefAligned;
202  VERIFY_RAISES_ASSERT(RefAligned r12 = r10);
203  VERIFY_RAISES_ASSERT(Ref r13 = r10); // r10 has more dynamic strides
204 }
@ ColMajor
Definition: Constants.h:318
@ RowMajor
Definition: Constants.h:320
unsigned Flag
Flag for solution.
Definition: navier_stokes_preconditioners.cc:42

References Eigen::ColMajor, cols, oomph::PressureAdvectionDiffusionValidation::Flag, m, Eigen::RowMajor, rows, and VERIFY_RAISES_ASSERT.

Referenced by EIGEN_DECLARE_TEST().

◆ test_zerosized()

void test_zerosized ( )
159  {
160  // default constructors:
161  Eigen::MatrixXd A;
162  Eigen::VectorXd v;
163  // explicit zero-sized:
164  Eigen::ArrayXXd A0(0, 0);
165  Eigen::ArrayXd v0(0);
166 
167  // assigning empty objects to each other:
168  A = A0;
169  v = v0;
170 }
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1

References v.

Referenced by EIGEN_DECLARE_TEST().