mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-06-04 18:54:00 +08:00
Prevent heap allocation in diagonal product
This commit is contained in:
parent
a0fc640c18
commit
4b0926f99b
@ -134,7 +134,7 @@ struct traits<DiagonalMatrix<Scalar_,SizeAtCompileTime,MaxSizeAtCompileTime> >
|
|||||||
typedef Matrix<Scalar_,SizeAtCompileTime,1,0,MaxSizeAtCompileTime,1> DiagonalVectorType;
|
typedef Matrix<Scalar_,SizeAtCompileTime,1,0,MaxSizeAtCompileTime,1> DiagonalVectorType;
|
||||||
typedef DiagonalShape StorageKind;
|
typedef DiagonalShape StorageKind;
|
||||||
enum {
|
enum {
|
||||||
Flags = LvalueBit | NoPreferredStorageOrderBit
|
Flags = LvalueBit | NoPreferredStorageOrderBit | NestByRefBit
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,12 @@
|
|||||||
// Public License v. 2.0. If a copy of the MPL was not distributed
|
// Public License v. 2.0. If a copy of the MPL was not distributed
|
||||||
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
|
||||||
|
// discard stack allocation as that too bypasses malloc
|
||||||
|
#define EIGEN_STACK_ALLOCATION_LIMIT 0
|
||||||
|
// heap allocation will raise an assert if enabled at runtime
|
||||||
|
#define EIGEN_RUNTIME_NO_MALLOC
|
||||||
|
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
template<typename MatrixType> void diagonalmatrices(const MatrixType& m)
|
template<typename MatrixType> void diagonalmatrices(const MatrixType& m)
|
||||||
@ -56,6 +62,7 @@ template<typename MatrixType> void diagonalmatrices(const MatrixType& m)
|
|||||||
Index i = internal::random<Index>(0, rows-1);
|
Index i = internal::random<Index>(0, rows-1);
|
||||||
Index j = internal::random<Index>(0, cols-1);
|
Index j = internal::random<Index>(0, cols-1);
|
||||||
|
|
||||||
|
internal::set_is_malloc_allowed(false);
|
||||||
VERIFY_IS_APPROX( ((ldm1 * m1)(i,j)) , ldm1.diagonal()(i) * m1(i,j) );
|
VERIFY_IS_APPROX( ((ldm1 * m1)(i,j)) , ldm1.diagonal()(i) * m1(i,j) );
|
||||||
VERIFY_IS_APPROX( ((ldm1 * (m1+m2))(i,j)) , ldm1.diagonal()(i) * (m1+m2)(i,j) );
|
VERIFY_IS_APPROX( ((ldm1 * (m1+m2))(i,j)) , ldm1.diagonal()(i) * (m1+m2)(i,j) );
|
||||||
VERIFY_IS_APPROX( ((m1 * rdm1)(i,j)) , rdm1.diagonal()(j) * m1(i,j) );
|
VERIFY_IS_APPROX( ((m1 * rdm1)(i,j)) , rdm1.diagonal()(j) * m1(i,j) );
|
||||||
@ -65,6 +72,7 @@ template<typename MatrixType> void diagonalmatrices(const MatrixType& m)
|
|||||||
VERIFY_IS_APPROX( (((v1+v2).asDiagonal() * (m1+m2))(i,j)) , (v1+v2)(i) * (m1+m2)(i,j) );
|
VERIFY_IS_APPROX( (((v1+v2).asDiagonal() * (m1+m2))(i,j)) , (v1+v2)(i) * (m1+m2)(i,j) );
|
||||||
VERIFY_IS_APPROX( ((m1 * (rv1+rv2).asDiagonal())(i,j)) , (rv1+rv2)(j) * m1(i,j) );
|
VERIFY_IS_APPROX( ((m1 * (rv1+rv2).asDiagonal())(i,j)) , (rv1+rv2)(j) * m1(i,j) );
|
||||||
VERIFY_IS_APPROX( (((m1+m2) * (rv1+rv2).asDiagonal())(i,j)) , (rv1+rv2)(j) * (m1+m2)(i,j) );
|
VERIFY_IS_APPROX( (((m1+m2) * (rv1+rv2).asDiagonal())(i,j)) , (rv1+rv2)(j) * (m1+m2)(i,j) );
|
||||||
|
internal::set_is_malloc_allowed(true);
|
||||||
|
|
||||||
if(rows>1)
|
if(rows>1)
|
||||||
{
|
{
|
||||||
@ -85,6 +93,13 @@ template<typename MatrixType> void diagonalmatrices(const MatrixType& m)
|
|||||||
big.block(i,j,rows,cols) = big.block(i,j,rows,cols) * rv1.asDiagonal();
|
big.block(i,j,rows,cols) = big.block(i,j,rows,cols) * rv1.asDiagonal();
|
||||||
VERIFY_IS_APPROX((big.block(i,j,rows,cols)) , m1 * rv1.asDiagonal() );
|
VERIFY_IS_APPROX((big.block(i,j,rows,cols)) , m1 * rv1.asDiagonal() );
|
||||||
|
|
||||||
|
// products do not allocate memory
|
||||||
|
MatrixType res(rows, cols);
|
||||||
|
internal::set_is_malloc_allowed(false);
|
||||||
|
res.noalias() = ldm1 * m;
|
||||||
|
res.noalias() = m * rdm1;
|
||||||
|
res.noalias() = ldm1 * m * rdm1;
|
||||||
|
internal::set_is_malloc_allowed(true);
|
||||||
|
|
||||||
// scalar multiple
|
// scalar multiple
|
||||||
VERIFY_IS_APPROX(LeftDiagonalMatrix(ldm1*s1).diagonal(), ldm1.diagonal() * s1);
|
VERIFY_IS_APPROX(LeftDiagonalMatrix(ldm1*s1).diagonal(), ldm1.diagonal() * s1);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user