Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 1 | // This file is part of Eigen, a lightweight C++ template library |
Benoit Jacob | 6347b1d | 2009-05-22 20:25:33 +0200 | [diff] [blame] | 2 | // for linear algebra. |
Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 3 | // |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 4 | // Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr> |
Benoit Jacob | 00f89a8 | 2008-11-24 13:40:43 +0000 | [diff] [blame] | 5 | // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com> |
Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 6 | // |
| 7 | // Eigen is free software; you can redistribute it and/or |
| 8 | // modify it under the terms of the GNU Lesser General Public |
| 9 | // License as published by the Free Software Foundation; either |
| 10 | // version 3 of the License, or (at your option) any later version. |
| 11 | // |
| 12 | // Alternatively, you can redistribute it and/or |
| 13 | // modify it under the terms of the GNU General Public License as |
| 14 | // published by the Free Software Foundation; either version 2 of |
| 15 | // the License, or (at your option) any later version. |
| 16 | // |
| 17 | // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY |
| 18 | // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 | // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the |
| 20 | // GNU General Public License for more details. |
| 21 | // |
| 22 | // You should have received a copy of the GNU Lesser General Public |
| 23 | // License and a copy of the GNU General Public License along with |
| 24 | // Eigen. If not, see <http://www.gnu.org/licenses/>. |
| 25 | |
Gael Guennebaud | e3fac69 | 2008-06-08 15:03:23 +0000 | [diff] [blame] | 26 | // this hack is needed to make this file compiles with -pedantic (gcc) |
Benoit Jacob | 9e3c731 | 2009-01-22 00:09:34 +0000 | [diff] [blame] | 27 | #ifdef __GNUC__ |
Gael Guennebaud | 60726f9 | 2008-06-04 10:15:48 +0000 | [diff] [blame] | 28 | #define throw(X) |
Benoit Jacob | 9e3c731 | 2009-01-22 00:09:34 +0000 | [diff] [blame] | 29 | #endif |
Benoit Jacob | 1d52bd4 | 2009-01-08 15:20:21 +0000 | [diff] [blame] | 30 | // discard stack allocation as that too bypasses malloc |
Benoit Jacob | d671205 | 2008-12-31 00:25:31 +0000 | [diff] [blame] | 31 | #define EIGEN_STACK_ALLOCATION_LIMIT 0 |
Benoit Jacob | 1d52bd4 | 2009-01-08 15:20:21 +0000 | [diff] [blame] | 32 | // any heap allocation will raise an assert |
| 33 | #define EIGEN_NO_MALLOC |
Benoit Jacob | d671205 | 2008-12-31 00:25:31 +0000 | [diff] [blame] | 34 | |
Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 35 | #include "main.h" |
| 36 | |
Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 37 | template<typename MatrixType> void nomalloc(const MatrixType& m) |
| 38 | { |
| 39 | /* this test check no dynamic memory allocation are issued with fixed-size matrices |
| 40 | */ |
| 41 | |
| 42 | typedef typename MatrixType::Scalar Scalar; |
| 43 | typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType; |
| 44 | |
| 45 | int rows = m.rows(); |
| 46 | int cols = m.cols(); |
| 47 | |
Gael Guennebaud | c10f069 | 2008-07-21 00:34:46 +0000 | [diff] [blame] | 48 | MatrixType m1 = MatrixType::Random(rows, cols), |
| 49 | m2 = MatrixType::Random(rows, cols), |
Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 50 | m3(rows, cols), |
Gael Guennebaud | c10f069 | 2008-07-21 00:34:46 +0000 | [diff] [blame] | 51 | mzero = MatrixType::Zero(rows, cols), |
Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 52 | identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> |
Gael Guennebaud | c10f069 | 2008-07-21 00:34:46 +0000 | [diff] [blame] | 53 | ::Identity(rows, rows), |
Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 54 | square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> |
Gael Guennebaud | c10f069 | 2008-07-21 00:34:46 +0000 | [diff] [blame] | 55 | ::Random(rows, rows); |
| 56 | VectorType v1 = VectorType::Random(rows), |
| 57 | v2 = VectorType::Random(rows), |
| 58 | vzero = VectorType::Zero(rows); |
Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 59 | |
| 60 | Scalar s1 = ei_random<Scalar>(); |
| 61 | |
| 62 | int r = ei_random<int>(0, rows-1), |
| 63 | c = ei_random<int>(0, cols-1); |
| 64 | |
| 65 | VERIFY_IS_APPROX((m1+m2)*s1, s1*m1+s1*m2); |
| 66 | VERIFY_IS_APPROX((m1+m2)(r,c), (m1(r,c))+(m2(r,c))); |
Gael Guennebaud | 71a171c | 2010-01-04 19:13:08 +0100 | [diff] [blame] | 67 | VERIFY_IS_APPROX(m1.cwiseProduct(m1.block(0,0,rows,cols)), (m1.array()*m1.array()).matrix()); |
Gael Guennebaud | 56d0077 | 2009-08-06 12:20:02 +0200 | [diff] [blame] | 68 | if (MatrixType::RowsAtCompileTime<EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD) { |
| 69 | // If the matrices are too large, we have better to use the optimized GEMM |
| 70 | // routines which allocates temporaries. However, on some platforms |
| 71 | // these temporaries are allocated on the stack using alloca. |
| 72 | VERIFY_IS_APPROX((m1*m1.transpose())*m2, m1*(m1.transpose()*m2)); |
| 73 | } |
Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void test_nomalloc() |
| 77 | { |
| 78 | // check that our operator new is indeed called: |
Gael Guennebaud | c10f069 | 2008-07-21 00:34:46 +0000 | [diff] [blame] | 79 | VERIFY_RAISES_ASSERT(MatrixXd dummy = MatrixXd::Random(3,3)); |
Benoit Jacob | 2840ac7 | 2009-10-28 18:19:29 -0400 | [diff] [blame] | 80 | CALL_SUBTEST(nomalloc(Matrix<float, 1, 1>()) ); |
| 81 | CALL_SUBTEST(nomalloc(Matrix4d()) ); |
| 82 | CALL_SUBTEST(nomalloc(Matrix<float,32,32>()) ); |
Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 83 | } |