MetisSupport.h
Go to the documentation of this file.
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 
12 #include "./InternalHeaderCheck.h"
13 
14 namespace Eigen {
23 template <typename StorageIndex>
25 {
26 public:
29 
30  template <typename MatrixType>
32  {
33  Index m = A.cols();
34  eigen_assert((A.rows() == A.cols()) && "ONLY FOR SQUARED MATRICES");
35  // Get the transpose of the input matrix
36  MatrixType At = A.transpose();
37  // Get the number of nonzeros elements in each row/col of At+A
38  Index TotNz = 0;
39  IndexVector visited(m);
40  visited.setConstant(-1);
41  for (StorageIndex j = 0; j < m; j++)
42  {
43  // Compute the union structure of of A(j,:) and At(j,:)
44  visited(j) = j; // Do not include the diagonal element
45  // Get the nonzeros in row/column j of A
46  for (typename MatrixType::InnerIterator it(A, j); it; ++it)
47  {
48  Index idx = it.index(); // Get the row index (for column major) or column index (for row major)
49  if (visited(idx) != j )
50  {
51  visited(idx) = j;
52  ++TotNz;
53  }
54  }
55  //Get the nonzeros in row/column j of At
56  for (typename MatrixType::InnerIterator it(At, j); it; ++it)
57  {
58  Index idx = it.index();
59  if(visited(idx) != j)
60  {
61  visited(idx) = j;
62  ++TotNz;
63  }
64  }
65  }
66  // Reserve place for A + At
67  m_indexPtr.resize(m+1);
68  m_innerIndices.resize(TotNz);
69 
70  // Now compute the real adjacency list of each column/row
71  visited.setConstant(-1);
72  StorageIndex CurNz = 0;
73  for (StorageIndex j = 0; j < m; j++)
74  {
75  m_indexPtr(j) = CurNz;
76 
77  visited(j) = j; // Do not include the diagonal element
78  // Add the pattern of row/column j of A to A+At
79  for (typename MatrixType::InnerIterator it(A,j); it; ++it)
80  {
81  StorageIndex idx = it.index(); // Get the row index (for column major) or column index (for row major)
82  if (visited(idx) != j )
83  {
84  visited(idx) = j;
85  m_innerIndices(CurNz) = idx;
86  CurNz++;
87  }
88  }
89  //Add the pattern of row/column j of At to A+At
90  for (typename MatrixType::InnerIterator it(At, j); it; ++it)
91  {
92  StorageIndex idx = it.index();
93  if(visited(idx) != j)
94  {
95  visited(idx) = j;
96  m_innerIndices(CurNz) = idx;
97  ++CurNz;
98  }
99  }
100  }
101  m_indexPtr(m) = CurNz;
102  }
103 
104  template <typename MatrixType>
105  void operator() (const MatrixType& A, PermutationType& matperm)
106  {
107  StorageIndex m = internal::convert_index<StorageIndex>(A.cols()); // must be StorageIndex, because it is passed by address to METIS
108  IndexVector perm(m),iperm(m);
109  // First, symmetrize the matrix graph.
111  int output_error;
112 
113  // Call the fill-reducing routine from METIS
114  output_error = METIS_NodeND(&m, m_indexPtr.data(), m_innerIndices.data(), NULL, NULL, perm.data(), iperm.data());
115 
116  if(output_error != METIS_OK)
117  {
118  //FIXME The ordering interface should define a class of possible errors
119  std::cerr << "ERROR WHILE CALLING THE METIS PACKAGE \n";
120  return;
121  }
122 
123  // Get the fill-reducing permutation
124  //NOTE: If Ap is the permuted matrix then perm and iperm vectors are defined as follows
125  // 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
126 
127  matperm.resize(m);
128  for (int j = 0; j < m; j++)
129  matperm.indices()(iperm(j)) = j;
130 
131  }
132 
133  protected:
134  IndexVector m_indexPtr; // Pointer to the adjacenccy list of each row/column
135  IndexVector m_innerIndices; // Adjacency list
136 };
137 
138 }// end namespace eigen
139 #endif
Matrix3f m
MatrixXcf A
#define eigen_assert(x)
Definition: Macros.h:902
PermutationMatrix< Dynamic, Dynamic, StorageIndex > PermutationType
Definition: MetisSupport.h:27
void operator()(const MatrixType &A, PermutationType &matperm)
Definition: MetisSupport.h:105
IndexVector m_indexPtr
Definition: MetisSupport.h:134
void get_symmetrized_graph(const MatrixType &A)
Definition: MetisSupport.h:31
IndexVector m_innerIndices
Definition: MetisSupport.h:135
Matrix< StorageIndex, Dynamic, 1 > IndexVector
Definition: MetisSupport.h:28
void resize(Index newSize)
const IndicesType & indices() const
Derived & setConstant(Index size, const Scalar &val)
const Scalar * data() const
constexpr void resize(Index rows, Index cols)
: InteropHeaders
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
std::ptrdiff_t j