You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
4.5 KiB

  1. /*
  2. Copyright (c) 2011, Intel Corporation. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without modification,
  4. are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright notice, this
  6. list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. * Neither the name of Intel Corporation nor the names of its contributors may
  11. be used to endorse or promote products derived from this software without
  12. specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  14. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  17. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  18. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  19. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  20. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. ********************************************************************************
  24. * Content : Eigen bindings to Intel(R) MKL
  25. * Singular Value Decomposition - SVD.
  26. ********************************************************************************
  27. */
  28. #ifndef EIGEN_JACOBISVD_MKL_H
  29. #define EIGEN_JACOBISVD_MKL_H
  30. #include "Eigen/src/Core/util/MKL_support.h"
  31. namespace Eigen {
  32. /** \internal Specialization for the data types supported by MKL */
  33. #define EIGEN_MKL_SVD(EIGTYPE, MKLTYPE, MKLRTYPE, MKLPREFIX, EIGCOLROW, MKLCOLROW) \
  34. template<> inline \
  35. JacobiSVD<Matrix<EIGTYPE, Dynamic, Dynamic, EIGCOLROW, Dynamic, Dynamic>, ColPivHouseholderQRPreconditioner>& \
  36. JacobiSVD<Matrix<EIGTYPE, Dynamic, Dynamic, EIGCOLROW, Dynamic, Dynamic>, ColPivHouseholderQRPreconditioner>::compute(const Matrix<EIGTYPE, Dynamic, Dynamic, EIGCOLROW, Dynamic, Dynamic>& matrix, unsigned int computationOptions) \
  37. { \
  38. typedef Matrix<EIGTYPE, Dynamic, Dynamic, EIGCOLROW, Dynamic, Dynamic> MatrixType; \
  39. typedef MatrixType::Scalar Scalar; \
  40. typedef MatrixType::RealScalar RealScalar; \
  41. allocate(matrix.rows(), matrix.cols(), computationOptions); \
  42. \
  43. /*const RealScalar precision = RealScalar(2) * NumTraits<Scalar>::epsilon();*/ \
  44. m_nonzeroSingularValues = m_diagSize; \
  45. \
  46. lapack_int lda = matrix.outerStride(), ldu, ldvt; \
  47. lapack_int matrix_order = MKLCOLROW; \
  48. char jobu, jobvt; \
  49. MKLTYPE *u, *vt, dummy; \
  50. jobu = (m_computeFullU) ? 'A' : (m_computeThinU) ? 'S' : 'N'; \
  51. jobvt = (m_computeFullV) ? 'A' : (m_computeThinV) ? 'S' : 'N'; \
  52. if (computeU()) { \
  53. ldu = m_matrixU.outerStride(); \
  54. u = (MKLTYPE*)m_matrixU.data(); \
  55. } else { ldu=1; u=&dummy; }\
  56. MatrixType localV; \
  57. ldvt = (m_computeFullV) ? m_cols : (m_computeThinV) ? m_diagSize : 1; \
  58. if (computeV()) { \
  59. localV.resize(ldvt, m_cols); \
  60. vt = (MKLTYPE*)localV.data(); \
  61. } else { ldvt=1; vt=&dummy; }\
  62. Matrix<MKLRTYPE, Dynamic, Dynamic> superb; superb.resize(m_diagSize, 1); \
  63. MatrixType m_temp; m_temp = matrix; \
  64. LAPACKE_##MKLPREFIX##gesvd( matrix_order, jobu, jobvt, m_rows, m_cols, (MKLTYPE*)m_temp.data(), lda, (MKLRTYPE*)m_singularValues.data(), u, ldu, vt, ldvt, superb.data()); \
  65. if (computeV()) m_matrixV = localV.adjoint(); \
  66. /* for(int i=0;i<m_diagSize;i++) if (m_singularValues.coeffRef(i) < precision) { m_nonzeroSingularValues--; m_singularValues.coeffRef(i)=RealScalar(0);}*/ \
  67. m_isInitialized = true; \
  68. return *this; \
  69. }
  70. EIGEN_MKL_SVD(double, double, double, d, ColMajor, LAPACK_COL_MAJOR)
  71. EIGEN_MKL_SVD(float, float, float , s, ColMajor, LAPACK_COL_MAJOR)
  72. EIGEN_MKL_SVD(dcomplex, MKL_Complex16, double, z, ColMajor, LAPACK_COL_MAJOR)
  73. EIGEN_MKL_SVD(scomplex, MKL_Complex8, float , c, ColMajor, LAPACK_COL_MAJOR)
  74. EIGEN_MKL_SVD(double, double, double, d, RowMajor, LAPACK_ROW_MAJOR)
  75. EIGEN_MKL_SVD(float, float, float , s, RowMajor, LAPACK_ROW_MAJOR)
  76. EIGEN_MKL_SVD(dcomplex, MKL_Complex16, double, z, RowMajor, LAPACK_ROW_MAJOR)
  77. EIGEN_MKL_SVD(scomplex, MKL_Complex8, float , c, RowMajor, LAPACK_ROW_MAJOR)
  78. } // end namespace Eigen
  79. #endif // EIGEN_JACOBISVD_MKL_H