SparseLU_panel_dfs.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 
10 /*
11 
12  * NOTE: This file is the modified version of [s,d,c,z]panel_dfs.c file in SuperLU
13 
14  * -- SuperLU routine (version 2.0) --
15  * Univ. of California Berkeley, Xerox Palo Alto Research Center,
16  * and Lawrence Berkeley National Lab.
17  * November 15, 1997
18  *
19  * Copyright (c) 1994 by Xerox Corporation. All rights reserved.
20  *
21  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY
22  * EXPRESSED OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
23  *
24  * Permission is hereby granted to use or copy this program for any
25  * purpose, provided the above notices are retained on all copies.
26  * Permission to modify the code and to distribute modified code is
27  * granted, provided the above notices are retained, and a notice that
28  * the code was modified is included with the above copyright notice.
29  */
30 #ifndef SPARSELU_PANEL_DFS_H
31 #define SPARSELU_PANEL_DFS_H
32 
33 #include "./InternalHeaderCheck.h"
34 
35 namespace Eigen {
36 
37 namespace internal {
38 
39 template<typename IndexVector>
40 struct panel_dfs_traits
41 {
42  typedef typename IndexVector::Scalar StorageIndex;
43  panel_dfs_traits(Index jcol, StorageIndex* marker)
44  : m_jcol(jcol), m_marker(marker)
45  {}
46  bool update_segrep(Index krep, StorageIndex jj)
47  {
48  if(m_marker[krep]<m_jcol)
49  {
50  m_marker[krep] = jj;
51  return true;
52  }
53  return false;
54  }
55  void mem_expand(IndexVector& /*glu.lsub*/, Index /*nextl*/, Index /*chmark*/) {}
56  enum { ExpandMem = false };
57  Index m_jcol;
58  StorageIndex* m_marker;
59 };
60 
61 
62 template <typename Scalar, typename StorageIndex>
63 template <typename Traits>
64 void SparseLUImpl<Scalar,StorageIndex>::dfs_kernel(const StorageIndex jj, IndexVector& perm_r,
65  Index& nseg, IndexVector& panel_lsub, IndexVector& segrep,
66  Ref<IndexVector> repfnz_col, IndexVector& xprune, Ref<IndexVector> marker, IndexVector& parent,
67  IndexVector& xplore, GlobalLU_t& glu,
68  Index& nextl_col, Index krow, Traits& traits
69  )
70 {
71 
72  StorageIndex kmark = marker(krow);
73 
74  // For each unmarked krow of jj
75  marker(krow) = jj;
76  StorageIndex kperm = perm_r(krow);
77  if (kperm == emptyIdxLU ) {
78  // krow is in L : place it in structure of L(*, jj)
79  panel_lsub(nextl_col++) = StorageIndex(krow); // krow is indexed into A
80 
81  traits.mem_expand(panel_lsub, nextl_col, kmark);
82  }
83  else
84  {
85  // krow is in U : if its supernode-representative krep
86  // has been explored, update repfnz(*)
87  // krep = supernode representative of the current row
88  StorageIndex krep = glu.xsup(glu.supno(kperm)+1) - 1;
89  // First nonzero element in the current column:
90  StorageIndex myfnz = repfnz_col(krep);
91 
92  if (myfnz != emptyIdxLU )
93  {
94  // Representative visited before
95  if (myfnz > kperm ) repfnz_col(krep) = kperm;
96 
97  }
98  else
99  {
100  // Otherwise, perform dfs starting at krep
101  StorageIndex oldrep = emptyIdxLU;
102  parent(krep) = oldrep;
103  repfnz_col(krep) = kperm;
104  StorageIndex xdfs = glu.xlsub(krep);
105  Index maxdfs = xprune(krep);
106 
107  StorageIndex kpar;
108  do
109  {
110  // For each unmarked kchild of krep
111  while (xdfs < maxdfs)
112  {
113  StorageIndex kchild = glu.lsub(xdfs);
114  xdfs++;
115  StorageIndex chmark = marker(kchild);
116 
117  if (chmark != jj )
118  {
119  marker(kchild) = jj;
120  StorageIndex chperm = perm_r(kchild);
121 
122  if (chperm == emptyIdxLU)
123  {
124  // case kchild is in L: place it in L(*, j)
125  panel_lsub(nextl_col++) = kchild;
126  traits.mem_expand(panel_lsub, nextl_col, chmark);
127  }
128  else
129  {
130  // case kchild is in U :
131  // chrep = its supernode-rep. If its rep has been explored,
132  // update its repfnz(*)
133  StorageIndex chrep = glu.xsup(glu.supno(chperm)+1) - 1;
134  myfnz = repfnz_col(chrep);
135 
136  if (myfnz != emptyIdxLU)
137  { // Visited before
138  if (myfnz > chperm)
139  repfnz_col(chrep) = chperm;
140  }
141  else
142  { // Cont. dfs at snode-rep of kchild
143  xplore(krep) = xdfs;
144  oldrep = krep;
145  krep = chrep; // Go deeper down G(L)
146  parent(krep) = oldrep;
147  repfnz_col(krep) = chperm;
148  xdfs = glu.xlsub(krep);
149  maxdfs = xprune(krep);
150 
151  } // end if myfnz != -1
152  } // end if chperm == -1
153 
154  } // end if chmark !=jj
155  } // end while xdfs < maxdfs
156 
157  // krow has no more unexplored nbrs :
158  // Place snode-rep krep in postorder DFS, if this
159  // segment is seen for the first time. (Note that
160  // "repfnz(krep)" may change later.)
161  // Baktrack dfs to its parent
162  if(traits.update_segrep(krep,jj))
163  //if (marker1(krep) < jcol )
164  {
165  segrep(nseg) = krep;
166  ++nseg;
167  //marker1(krep) = jj;
168  }
169 
170  kpar = parent(krep); // Pop recursion, mimic recursion
171  if (kpar == emptyIdxLU)
172  break; // dfs done
173  krep = kpar;
174  xdfs = xplore(krep);
175  maxdfs = xprune(krep);
176 
177  } while (kpar != emptyIdxLU); // Do until empty stack
178 
179  } // end if (myfnz = -1)
180 
181  } // end if (kperm == -1)
182 }
183 
220 template <typename Scalar, typename StorageIndex>
221 void SparseLUImpl<Scalar,StorageIndex>::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)
222 {
223  Index nextl_col; // Next available position in panel_lsub[*,jj]
224 
225  // Initialize pointers
226  VectorBlock<IndexVector> marker1(marker, m, m);
227  nseg = 0;
228 
229  panel_dfs_traits<IndexVector> traits(jcol, marker1.data());
230 
231  // For each column in the panel
232  for (StorageIndex jj = StorageIndex(jcol); jj < jcol + w; jj++)
233  {
234  nextl_col = (jj - jcol) * m;
235 
236  VectorBlock<IndexVector> repfnz_col(repfnz, nextl_col, m); // First nonzero location in each row
237  VectorBlock<ScalarVector> dense_col(dense,nextl_col, m); // Accumulate a column vector here
238 
239 
240  // For each nnz in A[*, jj] do depth first search
241  for (typename MatrixType::InnerIterator it(A, jj); it; ++it)
242  {
243  Index krow = it.row();
244  dense_col(krow) = it.value();
245 
246  StorageIndex kmark = marker(krow);
247  if (kmark == jj)
248  continue; // krow visited before, go to the next nonzero
249 
250  dfs_kernel(jj, perm_r, nseg, panel_lsub, segrep, repfnz_col, xprune, marker, parent,
251  xplore, glu, nextl_col, krow, traits);
252  }// end for nonzeros in column jj
253 
254  } // end for column jj
255 }
256 
257 } // end namespace internal
258 } // end namespace Eigen
259 
260 #endif // SPARSELU_PANEL_DFS_H
Matrix3f m
MatrixXcf A
RowVector3d w
Matrix< float, 1, Dynamic > MatrixType
: InteropHeaders
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82