• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 xpivotL.c file in SuperLU
13 
14  * -- SuperLU routine (version 3.0) --
15  * Univ. of California Berkeley, Xerox Palo Alto Research Center,
16  * and Lawrence Berkeley National Lab.
17  * October 15, 2003
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_PIVOTL_H
31 #define SPARSELU_PIVOTL_H
32 
33 namespace Eigen {
34 namespace internal {
35 
36 /**
37  * \brief Performs the numerical pivotin on the current column of L, and the CDIV operation.
38  *
39  * Pivot policy :
40  * (1) Compute thresh = u * max_(i>=j) abs(A_ij);
41  * (2) IF user specifies pivot row k and abs(A_kj) >= thresh THEN
42  *           pivot row = k;
43  *       ELSE IF abs(A_jj) >= thresh THEN
44  *           pivot row = j;
45  *       ELSE
46  *           pivot row = m;
47  *
48  *   Note: If you absolutely want to use a given pivot order, then set u=0.0.
49  *
50  * \param jcol The current column of L
51  * \param diagpivotthresh diagonal pivoting threshold
52  * \param[in,out] perm_r Row permutation (threshold pivoting)
53  * \param[in] iperm_c column permutation - used to finf diagonal of Pc*A*Pc'
54  * \param[out] pivrow  The pivot row
55  * \param glu Global LU data
56  * \return 0 if success, i > 0 if U(i,i) is exactly zero
57  *
58  */
59 template <typename Scalar, typename StorageIndex>
pivotL(const Index jcol,const RealScalar & diagpivotthresh,IndexVector & perm_r,IndexVector & iperm_c,Index & pivrow,GlobalLU_t & glu)60 Index SparseLUImpl<Scalar,StorageIndex>::pivotL(const Index jcol, const RealScalar& diagpivotthresh, IndexVector& perm_r, IndexVector& iperm_c, Index& pivrow, GlobalLU_t& glu)
61 {
62 
63   Index fsupc = (glu.xsup)((glu.supno)(jcol)); // First column in the supernode containing the column jcol
64   Index nsupc = jcol - fsupc; // Number of columns in the supernode portion, excluding jcol; nsupc >=0
65   Index lptr = glu.xlsub(fsupc); // pointer to the starting location of the row subscripts for this supernode portion
66   Index nsupr = glu.xlsub(fsupc+1) - lptr; // Number of rows in the supernode
67   Index lda = glu.xlusup(fsupc+1) - glu.xlusup(fsupc); // leading dimension
68   Scalar* lu_sup_ptr = &(glu.lusup.data()[glu.xlusup(fsupc)]); // Start of the current supernode
69   Scalar* lu_col_ptr = &(glu.lusup.data()[glu.xlusup(jcol)]); // Start of jcol in the supernode
70   StorageIndex* lsub_ptr = &(glu.lsub.data()[lptr]); // Start of row indices of the supernode
71 
72   // Determine the largest abs numerical value for partial pivoting
73   Index diagind = iperm_c(jcol); // diagonal index
74   RealScalar pivmax(-1.0);
75   Index pivptr = nsupc;
76   Index diag = emptyIdxLU;
77   RealScalar rtemp;
78   Index isub, icol, itemp, k;
79   for (isub = nsupc; isub < nsupr; ++isub) {
80     using std::abs;
81     rtemp = abs(lu_col_ptr[isub]);
82     if (rtemp > pivmax) {
83       pivmax = rtemp;
84       pivptr = isub;
85     }
86     if (lsub_ptr[isub] == diagind) diag = isub;
87   }
88 
89   // Test for singularity
90   if ( pivmax <= RealScalar(0.0) ) {
91     // if pivmax == -1, the column is structurally empty, otherwise it is only numerically zero
92     pivrow = pivmax < RealScalar(0.0) ? diagind : lsub_ptr[pivptr];
93     perm_r(pivrow) = StorageIndex(jcol);
94     return (jcol+1);
95   }
96 
97   RealScalar thresh = diagpivotthresh * pivmax;
98 
99   // Choose appropriate pivotal element
100 
101   {
102     // Test if the diagonal element can be used as a pivot (given the threshold value)
103     if (diag >= 0 )
104     {
105       // Diagonal element exists
106       using std::abs;
107       rtemp = abs(lu_col_ptr[diag]);
108       if (rtemp != RealScalar(0.0) && rtemp >= thresh) pivptr = diag;
109     }
110     pivrow = lsub_ptr[pivptr];
111   }
112 
113   // Record pivot row
114   perm_r(pivrow) = StorageIndex(jcol);
115   // Interchange row subscripts
116   if (pivptr != nsupc )
117   {
118     std::swap( lsub_ptr[pivptr], lsub_ptr[nsupc] );
119     // Interchange numerical values as well, for the two rows in the whole snode
120     // such that L is indexed the same way as A
121     for (icol = 0; icol <= nsupc; icol++)
122     {
123       itemp = pivptr + icol * lda;
124       std::swap(lu_sup_ptr[itemp], lu_sup_ptr[nsupc + icol * lda]);
125     }
126   }
127   // cdiv operations
128   Scalar temp = Scalar(1.0) / lu_col_ptr[nsupc];
129   for (k = nsupc+1; k < nsupr; k++)
130     lu_col_ptr[k] *= temp;
131   return 0;
132 }
133 
134 } // end namespace internal
135 } // end namespace Eigen
136 
137 #endif // SPARSELU_PIVOTL_H
138