MetisSupport.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2012 Désiré Nuentsa-Wakam <desire.nuentsa_wakam@inria.fr>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla
  7. // Public License v. 2.0. If a copy of the MPL was not distributed
  8. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. #ifndef METIS_SUPPORT_H
  10. #define METIS_SUPPORT_H
  11. namespace Eigen {
  12. /**
  13. * Get the fill-reducing ordering from the METIS package
  14. *
  15. * If A is the original matrix and Ap is the permuted matrix,
  16. * the fill-reducing permutation is defined as follows :
  17. * Row (column) i of A is the matperm(i) row (column) of Ap.
  18. * WARNING: As computed by METIS, this corresponds to the vector iperm (instead of perm)
  19. */
  20. template <typename StorageIndex>
  21. class MetisOrdering
  22. {
  23. public:
  24. typedef PermutationMatrix<Dynamic,Dynamic,StorageIndex> PermutationType;
  25. typedef Matrix<StorageIndex,Dynamic,1> IndexVector;
  26. template <typename MatrixType>
  27. void get_symmetrized_graph(const MatrixType& A)
  28. {
  29. Index m = A.cols();
  30. eigen_assert((A.rows() == A.cols()) && "ONLY FOR SQUARED MATRICES");
  31. // Get the transpose of the input matrix
  32. MatrixType At = A.transpose();
  33. // Get the number of nonzeros elements in each row/col of At+A
  34. Index TotNz = 0;
  35. IndexVector visited(m);
  36. visited.setConstant(-1);
  37. for (StorageIndex j = 0; j < m; j++)
  38. {
  39. // Compute the union structure of of A(j,:) and At(j,:)
  40. visited(j) = j; // Do not include the diagonal element
  41. // Get the nonzeros in row/column j of A
  42. for (typename MatrixType::InnerIterator it(A, j); it; ++it)
  43. {
  44. Index idx = it.index(); // Get the row index (for column major) or column index (for row major)
  45. if (visited(idx) != j )
  46. {
  47. visited(idx) = j;
  48. ++TotNz;
  49. }
  50. }
  51. //Get the nonzeros in row/column j of At
  52. for (typename MatrixType::InnerIterator it(At, j); it; ++it)
  53. {
  54. Index idx = it.index();
  55. if(visited(idx) != j)
  56. {
  57. visited(idx) = j;
  58. ++TotNz;
  59. }
  60. }
  61. }
  62. // Reserve place for A + At
  63. m_indexPtr.resize(m+1);
  64. m_innerIndices.resize(TotNz);
  65. // Now compute the real adjacency list of each column/row
  66. visited.setConstant(-1);
  67. StorageIndex CurNz = 0;
  68. for (StorageIndex j = 0; j < m; j++)
  69. {
  70. m_indexPtr(j) = CurNz;
  71. visited(j) = j; // Do not include the diagonal element
  72. // Add the pattern of row/column j of A to A+At
  73. for (typename MatrixType::InnerIterator it(A,j); it; ++it)
  74. {
  75. StorageIndex idx = it.index(); // Get the row index (for column major) or column index (for row major)
  76. if (visited(idx) != j )
  77. {
  78. visited(idx) = j;
  79. m_innerIndices(CurNz) = idx;
  80. CurNz++;
  81. }
  82. }
  83. //Add the pattern of row/column j of At to A+At
  84. for (typename MatrixType::InnerIterator it(At, j); it; ++it)
  85. {
  86. StorageIndex idx = it.index();
  87. if(visited(idx) != j)
  88. {
  89. visited(idx) = j;
  90. m_innerIndices(CurNz) = idx;
  91. ++CurNz;
  92. }
  93. }
  94. }
  95. m_indexPtr(m) = CurNz;
  96. }
  97. template <typename MatrixType>
  98. void operator() (const MatrixType& A, PermutationType& matperm)
  99. {
  100. StorageIndex m = internal::convert_index<StorageIndex>(A.cols()); // must be StorageIndex, because it is passed by address to METIS
  101. IndexVector perm(m),iperm(m);
  102. // First, symmetrize the matrix graph.
  103. get_symmetrized_graph(A);
  104. int output_error;
  105. // Call the fill-reducing routine from METIS
  106. output_error = METIS_NodeND(&m, m_indexPtr.data(), m_innerIndices.data(), NULL, NULL, perm.data(), iperm.data());
  107. if(output_error != METIS_OK)
  108. {
  109. //FIXME The ordering interface should define a class of possible errors
  110. std::cerr << "ERROR WHILE CALLING THE METIS PACKAGE \n";
  111. return;
  112. }
  113. // Get the fill-reducing permutation
  114. //NOTE: If Ap is the permuted matrix then perm and iperm vectors are defined as follows
  115. // Row (column) i of Ap is the perm(i) row(column) of A, and row (column) i of A is the iperm(i) row(column) of Ap
  116. matperm.resize(m);
  117. for (int j = 0; j < m; j++)
  118. matperm.indices()(iperm(j)) = j;
  119. }
  120. protected:
  121. IndexVector m_indexPtr; // Pointer to the adjacenccy list of each row/column
  122. IndexVector m_innerIndices; // Adjacency list
  123. };
  124. }// end namespace eigen
  125. #endif