File indexing completed on 2025-04-19 09:06:43
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031 #ifndef EIGEN_SPARSELU_MEMORY
0032 #define EIGEN_SPARSELU_MEMORY
0033
0034 namespace RivetEigen {
0035 namespace internal {
0036
0037 enum { LUNoMarker = 3 };
0038 enum {emptyIdxLU = -1};
0039 inline Index LUnumTempV(Index& m, Index& w, Index& t, Index& b)
0040 {
0041 return (std::max)(m, (t+b)*w);
0042 }
0043
0044 template< typename Scalar>
0045 inline Index LUTempSpace(Index&m, Index& w)
0046 {
0047 return (2*w + 4 + LUNoMarker) * m * sizeof(Index) + (w + 1) * m * sizeof(Scalar);
0048 }
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061 template <typename Scalar, typename StorageIndex>
0062 template <typename VectorType>
0063 Index SparseLUImpl<Scalar,StorageIndex>::expand(VectorType& vec, Index& length, Index nbElts, Index keep_prev, Index& num_expansions)
0064 {
0065
0066 float alpha = 1.5;
0067 Index new_len;
0068
0069 if(num_expansions == 0 || keep_prev)
0070 new_len = length ;
0071 else
0072 new_len = (std::max)(length+1,Index(alpha * length));
0073
0074 VectorType old_vec;
0075 if (nbElts > 0 )
0076 old_vec = vec.segment(0,nbElts);
0077
0078
0079 #ifdef EIGEN_EXCEPTIONS
0080 try
0081 #endif
0082 {
0083 vec.resize(new_len);
0084 }
0085 #ifdef EIGEN_EXCEPTIONS
0086 catch(std::bad_alloc& )
0087 #else
0088 if(!vec.size())
0089 #endif
0090 {
0091 if (!num_expansions)
0092 {
0093
0094
0095 return -1;
0096 }
0097 if (keep_prev)
0098 {
0099
0100 return new_len;
0101 }
0102 else
0103 {
0104
0105 Index tries = 0;
0106 do
0107 {
0108 alpha = (alpha + 1)/2;
0109 new_len = (std::max)(length+1,Index(alpha * length));
0110 #ifdef EIGEN_EXCEPTIONS
0111 try
0112 #endif
0113 {
0114 vec.resize(new_len);
0115 }
0116 #ifdef EIGEN_EXCEPTIONS
0117 catch(std::bad_alloc& )
0118 #else
0119 if (!vec.size())
0120 #endif
0121 {
0122 tries += 1;
0123 if ( tries > 10) return new_len;
0124 }
0125 } while (!vec.size());
0126 }
0127 }
0128
0129 if (nbElts > 0)
0130 vec.segment(0, nbElts) = old_vec;
0131
0132
0133 length = new_len;
0134 if(num_expansions) ++num_expansions;
0135 return 0;
0136 }
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150 template <typename Scalar, typename StorageIndex>
0151 Index SparseLUImpl<Scalar,StorageIndex>::memInit(Index m, Index n, Index annz, Index lwork, Index fillratio, Index panel_size, GlobalLU_t& glu)
0152 {
0153 Index& num_expansions = glu.num_expansions;
0154 num_expansions = 0;
0155 glu.nzumax = glu.nzlumax = (std::min)(fillratio * (annz+1) / n, m) * n;
0156 glu.nzlmax = (std::max)(Index(4), fillratio) * (annz+1) / 4;
0157
0158 Index tempSpace;
0159 tempSpace = (2*panel_size + 4 + LUNoMarker) * m * sizeof(Index) + (panel_size + 1) * m * sizeof(Scalar);
0160 if (lwork == emptyIdxLU)
0161 {
0162 Index estimated_size;
0163 estimated_size = (5 * n + 5) * sizeof(Index) + tempSpace
0164 + (glu.nzlmax + glu.nzumax) * sizeof(Index) + (glu.nzlumax+glu.nzumax) * sizeof(Scalar) + n;
0165 return estimated_size;
0166 }
0167
0168
0169
0170
0171 glu.xsup.resize(n+1);
0172 glu.supno.resize(n+1);
0173 glu.xlsub.resize(n+1);
0174 glu.xlusup.resize(n+1);
0175 glu.xusub.resize(n+1);
0176
0177
0178 do
0179 {
0180 if( (expand<ScalarVector>(glu.lusup, glu.nzlumax, 0, 0, num_expansions)<0)
0181 || (expand<ScalarVector>(glu.ucol, glu.nzumax, 0, 0, num_expansions)<0)
0182 || (expand<IndexVector> (glu.lsub, glu.nzlmax, 0, 0, num_expansions)<0)
0183 || (expand<IndexVector> (glu.usub, glu.nzumax, 0, 1, num_expansions)<0) )
0184 {
0185
0186 glu.nzlumax /= 2;
0187 glu.nzumax /= 2;
0188 glu.nzlmax /= 2;
0189 if (glu.nzlumax < annz ) return glu.nzlumax;
0190 }
0191 } while (!glu.lusup.size() || !glu.ucol.size() || !glu.lsub.size() || !glu.usub.size());
0192
0193 ++num_expansions;
0194 return 0;
0195
0196 }
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207 template <typename Scalar, typename StorageIndex>
0208 template <typename VectorType>
0209 Index SparseLUImpl<Scalar,StorageIndex>::memXpand(VectorType& vec, Index& maxlen, Index nbElts, MemType memtype, Index& num_expansions)
0210 {
0211 Index failed_size;
0212 if (memtype == USUB)
0213 failed_size = this->expand<VectorType>(vec, maxlen, nbElts, 1, num_expansions);
0214 else
0215 failed_size = this->expand<VectorType>(vec, maxlen, nbElts, 0, num_expansions);
0216
0217 if (failed_size)
0218 return failed_size;
0219
0220 return 0 ;
0221 }
0222
0223 }
0224
0225 }
0226 #endif