1*bf2c3715SXin Li/* 2*bf2c3715SXin Li Copyright (c) 2011, Intel Corporation. All rights reserved. 3*bf2c3715SXin Li Copyright (C) 2011-2016 Gael Guennebaud <[email protected]> 4*bf2c3715SXin Li 5*bf2c3715SXin Li Redistribution and use in source and binary forms, with or without modification, 6*bf2c3715SXin Li are permitted provided that the following conditions are met: 7*bf2c3715SXin Li 8*bf2c3715SXin Li * Redistributions of source code must retain the above copyright notice, this 9*bf2c3715SXin Li list of conditions and the following disclaimer. 10*bf2c3715SXin Li * Redistributions in binary form must reproduce the above copyright notice, 11*bf2c3715SXin Li this list of conditions and the following disclaimer in the documentation 12*bf2c3715SXin Li and/or other materials provided with the distribution. 13*bf2c3715SXin Li * Neither the name of Intel Corporation nor the names of its contributors may 14*bf2c3715SXin Li be used to endorse or promote products derived from this software without 15*bf2c3715SXin Li specific prior written permission. 16*bf2c3715SXin Li 17*bf2c3715SXin Li THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 18*bf2c3715SXin Li ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19*bf2c3715SXin Li WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20*bf2c3715SXin Li DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 21*bf2c3715SXin Li ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22*bf2c3715SXin Li (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23*bf2c3715SXin Li LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 24*bf2c3715SXin Li ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25*bf2c3715SXin Li (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26*bf2c3715SXin Li SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27*bf2c3715SXin Li 28*bf2c3715SXin Li ******************************************************************************** 29*bf2c3715SXin Li * Content : Documentation on the use of BLAS/LAPACK libraries through Eigen 30*bf2c3715SXin Li ******************************************************************************** 31*bf2c3715SXin Li*/ 32*bf2c3715SXin Li 33*bf2c3715SXin Linamespace Eigen { 34*bf2c3715SXin Li 35*bf2c3715SXin Li/** \page TopicUsingBlasLapack Using BLAS/LAPACK from %Eigen 36*bf2c3715SXin Li 37*bf2c3715SXin Li 38*bf2c3715SXin LiSince %Eigen version 3.3 and later, any F77 compatible BLAS or LAPACK libraries can be used as backends for dense matrix products and dense matrix decompositions. 39*bf2c3715SXin LiFor instance, one can use <a href="http://eigen.tuxfamily.org/Counter/redirect_to_mkl.php">Intel® MKL</a>, Apple's Accelerate framework on OSX, <a href="http://www.openblas.net/">OpenBLAS</a>, <a href="http://www.netlib.org/lapack">Netlib LAPACK</a>, etc. 40*bf2c3715SXin Li 41*bf2c3715SXin LiDo not miss this \link TopicUsingIntelMKL page \endlink for further discussions on the specific use of Intel® MKL (also includes VML, PARDISO, etc.) 42*bf2c3715SXin Li 43*bf2c3715SXin LiIn order to use an external BLAS and/or LAPACK library, you must link you own application to the respective libraries and their dependencies. 44*bf2c3715SXin LiFor LAPACK, you must also link to the standard <a href="http://www.netlib.org/lapack/lapacke.html">Lapacke</a> library, which is used as a convenient think layer between %Eigen's C++ code and LAPACK F77 interface. Then you must activate their usage by defining one or multiple of the following macros (\b before including any %Eigen's header): 45*bf2c3715SXin Li 46*bf2c3715SXin Li\note For Mac users, in order to use the lapack version shipped with the Accelerate framework, you also need the lapacke library. 47*bf2c3715SXin LiUsing <a href="https://www.macports.org/">MacPorts</a>, this is as easy as: 48*bf2c3715SXin Li\code 49*bf2c3715SXin Lisudo port install lapack 50*bf2c3715SXin Li\endcode 51*bf2c3715SXin Liand then use the following link flags: \c -framework \c Accelerate \c /opt/local/lib/lapack/liblapacke.dylib 52*bf2c3715SXin Li 53*bf2c3715SXin Li<table class="manual"> 54*bf2c3715SXin Li<tr><td>\c EIGEN_USE_BLAS </td><td>Enables the use of external BLAS level 2 and 3 routines (compatible with any F77 BLAS interface)</td></tr> 55*bf2c3715SXin Li<tr class="alt"><td>\c EIGEN_USE_LAPACKE </td><td>Enables the use of external Lapack routines via the <a href="http://www.netlib.org/lapack/lapacke.html">Lapacke</a> C interface to Lapack (compatible with any F77 LAPACK interface)</td></tr> 56*bf2c3715SXin Li<tr><td>\c EIGEN_USE_LAPACKE_STRICT </td><td>Same as \c EIGEN_USE_LAPACKE but algorithms of lower numerical robustness are disabled. \n This currently concerns only JacobiSVD which otherwise would be replaced by \c gesvd that is less robust than Jacobi rotations.</td></tr> 57*bf2c3715SXin Li</table> 58*bf2c3715SXin Li 59*bf2c3715SXin LiWhen doing so, a number of %Eigen's algorithms are silently substituted with calls to BLAS or LAPACK routines. 60*bf2c3715SXin LiThese substitutions apply only for \b Dynamic \b or \b large enough objects with one of the following four standard scalar types: \c float, \c double, \c complex<float>, and \c complex<double>. 61*bf2c3715SXin LiOperations on other scalar types or mixing reals and complexes will continue to use the built-in algorithms. 62*bf2c3715SXin Li 63*bf2c3715SXin LiThe breadth of %Eigen functionality that can be substituted is listed in the table below. 64*bf2c3715SXin Li<table class="manual"> 65*bf2c3715SXin Li<tr><th>Functional domain</th><th>Code example</th><th>BLAS/LAPACK routines</th></tr> 66*bf2c3715SXin Li<tr><td>Matrix-matrix operations \n \c EIGEN_USE_BLAS </td><td>\code 67*bf2c3715SXin Lim1*m2.transpose(); 68*bf2c3715SXin Lim1.selfadjointView<Lower>()*m2; 69*bf2c3715SXin Lim1*m2.triangularView<Upper>(); 70*bf2c3715SXin Lim1.selfadjointView<Lower>().rankUpdate(m2,1.0); 71*bf2c3715SXin Li\endcode</td><td>\code 72*bf2c3715SXin Li?gemm 73*bf2c3715SXin Li?symm/?hemm 74*bf2c3715SXin Li?trmm 75*bf2c3715SXin Lidsyrk/ssyrk 76*bf2c3715SXin Li\endcode</td></tr> 77*bf2c3715SXin Li<tr class="alt"><td>Matrix-vector operations \n \c EIGEN_USE_BLAS </td><td>\code 78*bf2c3715SXin Lim1.adjoint()*b; 79*bf2c3715SXin Lim1.selfadjointView<Lower>()*b; 80*bf2c3715SXin Lim1.triangularView<Upper>()*b; 81*bf2c3715SXin Li\endcode</td><td>\code 82*bf2c3715SXin Li?gemv 83*bf2c3715SXin Li?symv/?hemv 84*bf2c3715SXin Li?trmv 85*bf2c3715SXin Li\endcode</td></tr> 86*bf2c3715SXin Li<tr><td>LU decomposition \n \c EIGEN_USE_LAPACKE \n \c EIGEN_USE_LAPACKE_STRICT </td><td>\code 87*bf2c3715SXin Liv1 = m1.lu().solve(v2); 88*bf2c3715SXin Li\endcode</td><td>\code 89*bf2c3715SXin Li?getrf 90*bf2c3715SXin Li\endcode</td></tr> 91*bf2c3715SXin Li<tr class="alt"><td>Cholesky decomposition \n \c EIGEN_USE_LAPACKE \n \c EIGEN_USE_LAPACKE_STRICT </td><td>\code 92*bf2c3715SXin Liv1 = m2.selfadjointView<Upper>().llt().solve(v2); 93*bf2c3715SXin Li\endcode</td><td>\code 94*bf2c3715SXin Li?potrf 95*bf2c3715SXin Li\endcode</td></tr> 96*bf2c3715SXin Li<tr><td>QR decomposition \n \c EIGEN_USE_LAPACKE \n \c EIGEN_USE_LAPACKE_STRICT </td><td>\code 97*bf2c3715SXin Lim1.householderQr(); 98*bf2c3715SXin Lim1.colPivHouseholderQr(); 99*bf2c3715SXin Li\endcode</td><td>\code 100*bf2c3715SXin Li?geqrf 101*bf2c3715SXin Li?geqp3 102*bf2c3715SXin Li\endcode</td></tr> 103*bf2c3715SXin Li<tr class="alt"><td>Singular value decomposition \n \c EIGEN_USE_LAPACKE </td><td>\code 104*bf2c3715SXin LiJacobiSVD<MatrixXd> svd; 105*bf2c3715SXin Lisvd.compute(m1, ComputeThinV); 106*bf2c3715SXin Li\endcode</td><td>\code 107*bf2c3715SXin Li?gesvd 108*bf2c3715SXin Li\endcode</td></tr> 109*bf2c3715SXin Li<tr><td>Eigen-value decompositions \n \c EIGEN_USE_LAPACKE \n \c EIGEN_USE_LAPACKE_STRICT </td><td>\code 110*bf2c3715SXin LiEigenSolver<MatrixXd> es(m1); 111*bf2c3715SXin LiComplexEigenSolver<MatrixXcd> ces(m1); 112*bf2c3715SXin LiSelfAdjointEigenSolver<MatrixXd> saes(m1+m1.transpose()); 113*bf2c3715SXin LiGeneralizedSelfAdjointEigenSolver<MatrixXd> 114*bf2c3715SXin Li gsaes(m1+m1.transpose(),m2+m2.transpose()); 115*bf2c3715SXin Li\endcode</td><td>\code 116*bf2c3715SXin Li?gees 117*bf2c3715SXin Li?gees 118*bf2c3715SXin Li?syev/?heev 119*bf2c3715SXin Li?syev/?heev, 120*bf2c3715SXin Li?potrf 121*bf2c3715SXin Li\endcode</td></tr> 122*bf2c3715SXin Li<tr class="alt"><td>Schur decomposition \n \c EIGEN_USE_LAPACKE \n \c EIGEN_USE_LAPACKE_STRICT </td><td>\code 123*bf2c3715SXin LiRealSchur<MatrixXd> schurR(m1); 124*bf2c3715SXin LiComplexSchur<MatrixXcd> schurC(m1); 125*bf2c3715SXin Li\endcode</td><td>\code 126*bf2c3715SXin Li?gees 127*bf2c3715SXin Li\endcode</td></tr> 128*bf2c3715SXin Li</table> 129*bf2c3715SXin LiIn the examples, m1 and m2 are dense matrices and v1 and v2 are dense vectors. 130*bf2c3715SXin Li 131*bf2c3715SXin Li*/ 132*bf2c3715SXin Li 133*bf2c3715SXin Li} 134