Eigen  5.0.1
 
Loading...
Searching...
No Matches
MetisSupport.h
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// SPDX-License-Identifier: MPL-2.0
10#ifndef METIS_SUPPORT_H
11#define METIS_SUPPORT_H
12
13// IWYU pragma: private
14#include "./InternalHeaderCheck.h"
15
16namespace Eigen {
25template <typename StorageIndex>
27 public:
29 typedef Matrix<StorageIndex, Dynamic, 1> IndexVector;
30
31 template <typename MatrixType>
32 void get_symmetrized_graph(const MatrixType& A) {
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 // Compute the union structure of A(j,:) and At(j,:)
43 visited(j) = j; // Do not include the diagonal element
44 // Get the nonzeros in row/column j of A
45 for (typename MatrixType::InnerIterator it(A, j); it; ++it) {
46 Index idx = it.index(); // Get the row index (for column major) or column index (for row major)
47 if (visited(idx) != j) {
48 visited(idx) = j;
49 ++TotNz;
50 }
51 }
52 // Get the nonzeros in row/column j of At
53 for (typename MatrixType::InnerIterator it(At, j); it; ++it) {
54 Index idx = it.index();
55 if (visited(idx) != j) {
56 visited(idx) = j;
57 ++TotNz;
58 }
59 }
60 }
61 // Reserve place for A + At
62 m_indexPtr.resize(m + 1);
63 m_innerIndices.resize(TotNz);
64
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 m_indexPtr(j) = CurNz;
70
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 StorageIndex idx = it.index(); // Get the row index (for column major) or column index (for row major)
75 if (visited(idx) != j) {
76 visited(idx) = j;
77 m_innerIndices(CurNz) = idx;
78 CurNz++;
79 }
80 }
81 // Add the pattern of row/column j of At to A+At
82 for (typename MatrixType::InnerIterator it(At, j); it; ++it) {
83 StorageIndex idx = it.index();
84 if (visited(idx) != j) {
85 visited(idx) = j;
86 m_innerIndices(CurNz) = idx;
87 ++CurNz;
88 }
89 }
90 }
91 m_indexPtr(m) = CurNz;
92 }
93
94 template <typename MatrixType>
95 void operator()(const MatrixType& A, PermutationType& matperm) {
96 StorageIndex m = internal::convert_index<StorageIndex>(
97 A.cols()); // must be StorageIndex, because it is passed by address to METIS
98 IndexVector perm(m), iperm(m);
99 // First, symmetrize the matrix graph.
100 get_symmetrized_graph(A);
101 int output_error;
102
103 // Call the fill-reducing routine from METIS
104 output_error =
105 METIS_NodeND(&m, m_indexPtr.data(), m_innerIndices.data(), nullptr, nullptr, perm.data(), iperm.data());
106
107 if (output_error != METIS_OK) {
108 // FIXME The ordering interface should define a class of possible errors
109 std::cerr << "ERROR WHILE CALLING THE METIS PACKAGE \n";
110 return;
111 }
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
117 matperm.resize(m);
118 for (int j = 0; j < m; j++) matperm.indices()(iperm(j)) = j;
119 }
120
121 protected:
122 IndexVector m_indexPtr; // Pointer to the adjacency list of each row/column
123 IndexVector m_innerIndices; // Adjacency list
124};
125
126} // namespace Eigen
127#endif
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:188
Definition MetisSupport.h:26
void resize(Index newSize)
Definition PermutationMatrix.h:165
Permutation matrix.
Definition PermutationMatrix.h:346
constexpr const IndicesType & indices() const
Definition PermutationMatrix.h:400
Derived & setConstant(Index size, const Scalar &val)
Definition CwiseNullaryOp.h:349
constexpr const Scalar * data() const
Definition PlainObjectBase.h:261