Eigen  5.0.1
 
Loading...
Searching...
No Matches
SparseLU_panel_dfs.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
11/*
12
13 * NOTE: This file is the modified version of [s,d,c,z]panel_dfs.c file in SuperLU
14
15 * -- SuperLU routine (version 2.0) --
16 * Univ. of California Berkeley, Xerox Palo Alto Research Center,
17 * and Lawrence Berkeley National Lab.
18 * November 15, 1997
19 *
20 * Copyright (c) 1994 by Xerox Corporation. All rights reserved.
21 *
22 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY
23 * EXPRESSED OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
24 *
25 * Permission is hereby granted to use or copy this program for any
26 * purpose, provided the above notices are retained on all copies.
27 * Permission to modify the code and to distribute modified code is
28 * granted, provided the above notices are retained, and a notice that
29 * the code was modified is included with the above copyright notice.
30 */
31#ifndef SPARSELU_PANEL_DFS_H
32#define SPARSELU_PANEL_DFS_H
33
34// IWYU pragma: private
35#include "./InternalHeaderCheck.h"
36
37namespace Eigen {
38
39namespace internal {
40
41template <typename IndexVector>
42struct panel_dfs_traits {
43 using StorageIndex = typename IndexVector::Scalar;
44 panel_dfs_traits(Index jcol, StorageIndex* marker) : m_jcol(jcol), m_marker(marker) {}
45 bool update_segrep(Index krep, StorageIndex jj) {
46 if (m_marker[krep] < m_jcol) {
47 m_marker[krep] = jj;
48 return true;
49 }
50 return false;
51 }
52 void mem_expand(IndexVector& /*glu.lsub*/, Index /*nextl*/, Index /*chmark*/) {}
53 enum { ExpandMem = false };
54 Index m_jcol;
55 StorageIndex* m_marker;
56};
57
58template <typename Scalar, typename StorageIndex>
59template <typename Traits>
60void SparseLUImpl<Scalar, StorageIndex>::dfs_kernel(const StorageIndex jj, IndexVector& perm_r, Index& nseg,
61 IndexVector& panel_lsub, IndexVector& segrep,
62 Ref<IndexVector> repfnz_col, IndexVector& xprune,
63 Ref<IndexVector> marker, IndexVector& parent, IndexVector& xplore,
64 GlobalLU_t& glu, Index& nextl_col, Index krow, Traits& traits) {
65 StorageIndex kmark = marker(krow);
66
67 // For each unmarked krow of jj
68 marker(krow) = jj;
69 StorageIndex kperm = perm_r(krow);
70 if (kperm == emptyIdxLU) {
71 // krow is in L : place it in structure of L(*, jj)
72 panel_lsub(nextl_col++) = StorageIndex(krow); // krow is indexed into A
73
74 traits.mem_expand(panel_lsub, nextl_col, kmark);
75 } else {
76 // krow is in U : if its supernode-representative krep
77 // has been explored, update repfnz(*)
78 // krep = supernode representative of the current row
79 StorageIndex krep = glu.xsup(glu.supno(kperm) + 1) - 1;
80 // First nonzero element in the current column:
81 StorageIndex myfnz = repfnz_col(krep);
82
83 if (myfnz != emptyIdxLU) {
84 // Representative visited before
85 if (myfnz > kperm) repfnz_col(krep) = kperm;
86
87 } else {
88 // Otherwise, perform dfs starting at krep
89 StorageIndex oldrep = emptyIdxLU;
90 parent(krep) = oldrep;
91 repfnz_col(krep) = kperm;
92 StorageIndex xdfs = glu.xlsub(krep);
93 Index maxdfs = xprune(krep);
94
95 StorageIndex kpar;
96 do {
97 // For each unmarked kchild of krep
98 while (xdfs < maxdfs) {
99 StorageIndex kchild = glu.lsub(xdfs);
100 xdfs++;
101 StorageIndex chmark = marker(kchild);
102
103 if (chmark != jj) {
104 marker(kchild) = jj;
105 StorageIndex chperm = perm_r(kchild);
106
107 if (chperm == emptyIdxLU) {
108 // case kchild is in L: place it in L(*, j)
109 panel_lsub(nextl_col++) = kchild;
110 traits.mem_expand(panel_lsub, nextl_col, chmark);
111 } else {
112 // case kchild is in U :
113 // chrep = its supernode-rep. If its rep has been explored,
114 // update its repfnz(*)
115 StorageIndex chrep = glu.xsup(glu.supno(chperm) + 1) - 1;
116 myfnz = repfnz_col(chrep);
117
118 if (myfnz != emptyIdxLU) { // Visited before
119 if (myfnz > chperm) repfnz_col(chrep) = chperm;
120 } else { // Cont. dfs at snode-rep of kchild
121 xplore(krep) = xdfs;
122 oldrep = krep;
123 krep = chrep; // Go deeper down G(L)
124 parent(krep) = oldrep;
125 repfnz_col(krep) = chperm;
126 xdfs = glu.xlsub(krep);
127 maxdfs = xprune(krep);
128
129 } // end if myfnz != -1
130 } // end if chperm == -1
131
132 } // end if chmark !=jj
133 } // end while xdfs < maxdfs
134
135 // krow has no more unexplored nbrs :
136 // Place snode-rep krep in postorder DFS, if this
137 // segment is seen for the first time. (Note that
138 // "repfnz(krep)" may change later.)
139 // Backtrack dfs to its parent
140 if (traits.update_segrep(krep, jj)) {
141 segrep(nseg) = krep;
142 ++nseg;
143 }
144
145 kpar = parent(krep); // Pop recursion, mimic recursion
146 if (kpar == emptyIdxLU) break; // dfs done
147 krep = kpar;
148 xdfs = xplore(krep);
149 maxdfs = xprune(krep);
150
151 } while (kpar != emptyIdxLU); // Do until empty stack
152
153 } // end if (myfnz = -1)
154
155 } // end if (kperm == -1)
156}
157
193
194template <typename Scalar, typename StorageIndex>
195void SparseLUImpl<Scalar, StorageIndex>::panel_dfs(const Index m, const Index w, const Index jcol, MatrixType& A,
196 IndexVector& perm_r, Index& nseg, ScalarVector& dense,
197 IndexVector& panel_lsub, IndexVector& segrep, IndexVector& repfnz,
198 IndexVector& xprune, IndexVector& marker, IndexVector& parent,
199 IndexVector& xplore, GlobalLU_t& glu) {
200 Index nextl_col; // Next available position in panel_lsub[*,jj]
201
202 // Initialize pointers
203 VectorBlock<IndexVector> marker1(marker, m, m);
204 nseg = 0;
205
206 panel_dfs_traits<IndexVector> traits(jcol, marker1.data());
207
208 // For each column in the panel
209 for (StorageIndex jj = StorageIndex(jcol); jj < jcol + w; jj++) {
210 nextl_col = (jj - jcol) * m;
211
212 VectorBlock<IndexVector> repfnz_col(repfnz, nextl_col, m); // First nonzero location in each row
213 VectorBlock<ScalarVector> dense_col(dense, nextl_col, m); // Accumulate a column vector here
214
215 // For each nnz in A[*, jj] do depth first search
216 for (typename MatrixType::InnerIterator it(A, jj); it; ++it) {
217 Index krow = it.row();
218 dense_col(krow) = it.value();
219
220 StorageIndex kmark = marker(krow);
221 if (kmark == jj) continue; // krow visited before, go to the next nonzero
222
223 dfs_kernel(jj, perm_r, nseg, panel_lsub, segrep, repfnz_col, xprune, marker, parent, xplore, glu, nextl_col, krow,
224 traits);
225 } // end for nonzeros in column jj
226
227 } // end for column jj
228}
229
230} // end namespace internal
231} // end namespace Eigen
232
233#endif // SPARSELU_PANEL_DFS_H
A matrix or vector expression mapping an existing expression.
Definition Ref.h:262
Expression of a fixed-size or dynamic-size sub-vector.
Definition VectorBlock.h:59
void panel_dfs(const Index m, const Index w, const Index jcol, MatrixType &A, IndexVector &perm_r, Index &nseg, ScalarVector &dense, IndexVector &panel_lsub, IndexVector &segrep, IndexVector &repfnz, IndexVector &xprune, IndexVector &marker, IndexVector &parent, IndexVector &xplore, GlobalLU_t &glu)
Performs a symbolic factorization on a panel of columns [jcol, jcol+w)
Definition SparseLU_panel_dfs.h:195