eigen2_nomalloc.cpp
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra. Eigen itself is part of the KDE project.
00003 //
00004 // Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
00005 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
00006 //
00007 // Eigen is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public
00009 // License as published by the Free Software Foundation; either
00010 // version 3 of the License, or (at your option) any later version.
00011 //
00012 // Alternatively, you can redistribute it and/or
00013 // modify it under the terms of the GNU General Public License as
00014 // published by the Free Software Foundation; either version 2 of
00015 // the License, or (at your option) any later version.
00016 //
00017 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00018 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00019 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00020 // GNU General Public License for more details.
00021 //
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License and a copy of the GNU General Public License along with
00024 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00025 
00026 // this hack is needed to make this file compiles with -pedantic (gcc)
00027 #ifdef __GNUC__
00028 #define throw(X)
00029 #endif
00030 // discard stack allocation as that too bypasses malloc
00031 #define EIGEN_STACK_ALLOCATION_LIMIT 0
00032 // any heap allocation will raise an assert
00033 #define EIGEN_NO_MALLOC
00034 
00035 #include "main.h"
00036 
00037 template<typename MatrixType> void nomalloc(const MatrixType& m)
00038 {
00039   /* this test check no dynamic memory allocation are issued with fixed-size matrices
00040   */
00041 
00042   typedef typename MatrixType::Scalar Scalar;
00043   typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
00044 
00045   int rows = m.rows();
00046   int cols = m.cols();
00047 
00048   MatrixType m1 = MatrixType::Random(rows, cols),
00049              m2 = MatrixType::Random(rows, cols),
00050              m3(rows, cols),
00051              mzero = MatrixType::Zero(rows, cols),
00052              identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
00053                               ::Identity(rows, rows),
00054              square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
00055                               ::Random(rows, rows);
00056   VectorType v1 = VectorType::Random(rows),
00057              v2 = VectorType::Random(rows),
00058              vzero = VectorType::Zero(rows);
00059 
00060   Scalar s1 = ei_random<Scalar>();
00061 
00062   int r = ei_random<int>(0, rows-1),
00063       c = ei_random<int>(0, cols-1);
00064 
00065   VERIFY_IS_APPROX((m1+m2)*s1,              s1*m1+s1*m2);
00066   VERIFY_IS_APPROX((m1+m2)(r,c), (m1(r,c))+(m2(r,c)));
00067   VERIFY_IS_APPROX(m1.cwise() * m1.block(0,0,rows,cols), m1.cwise() * m1);
00068   VERIFY_IS_APPROX((m1*m1.transpose())*m2,  m1*(m1.transpose()*m2));
00069 }
00070 
00071 void test_eigen2_nomalloc()
00072 {
00073   // check that our operator new is indeed called:
00074   VERIFY_RAISES_ASSERT(MatrixXd dummy = MatrixXd::Random(3,3));
00075   CALL_SUBTEST_1( nomalloc(Matrix<float, 1, 1>()) );
00076   CALL_SUBTEST_2( nomalloc(Matrix4d()) );
00077   CALL_SUBTEST_3( nomalloc(Matrix<float,32,32>()) );
00078 }


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:31:04