63 #ifndef GMM_PRECOND_ILUT_H
64 #define GMM_PRECOND_ILUT_H
86 template<
typename T>
struct elt_rsvector_value_less_ {
87 inline bool operator()(
const elt_rsvector_<T>& a,
88 const elt_rsvector_<T>& b)
const
89 {
return (gmm::abs(a.e) > gmm::abs(b.e)); }
100 template <
typename Matrix>
103 typedef typename linalg_traits<Matrix>::value_type value_type;
106 typedef row_matrix<_rsvector> LU_Matrix;
115 template<
typename M>
void do_ilut(
const M&, row_major);
116 void do_ilut(
const Matrix&, col_major);
119 void build_with(
const Matrix& A,
int k_ = -1,
double eps_ =
double(-1)) {
121 if (eps_ >=
double(0)) eps = eps_;
125 do_ilut(A,
typename principal_orientation_type<
typename
126 linalg_traits<Matrix>::sub_orientation>::potype());
129 : L(mat_nrows(A), mat_ncols(A)), U(mat_nrows(A), mat_ncols(A)),
130 K(k_), eps(eps_) { build_with(A); }
131 ilut_precond(size_type k_,
double eps_) : K(k_), eps(eps_) {}
133 size_type memsize()
const {
134 return sizeof(*this) + (
nnz(U)+
nnz(L))*
sizeof(value_type);
138 template<
typename Matrix>
template<
typename M>
140 typedef value_type T;
141 typedef typename number_traits<T>::magnitude_type R;
143 size_type n = mat_nrows(A);
145 std::vector<T> indiag(n);
146 _wsvector w(mat_ncols(A));
147 _rsvector ww(mat_ncols(A)), wL(mat_ncols(A)), wU(mat_ncols(A));
150 R prec = default_tol(R());
151 R max_pivot = gmm::abs(A(0,0)) * prec;
153 for (size_type i = 0; i < n; ++i) {
157 typename _wsvector::iterator wkold = w.end();
158 for (
typename _wsvector::iterator wk = w.begin();
159 wk != w.end() && wk->first < i; ) {
160 size_type k = wk->first;
161 tmp = (wk->second) * indiag[k];
162 if (gmm::abs(tmp) < eps * norm_row) w.erase(k);
163 else { wk->second += tmp;
gmm::add(scaled(mat_row(U, k), -tmp), w); }
164 if (wkold == w.end()) wk = w.begin();
else { wk = wkold; ++wk; }
165 if (wk != w.end() && wk->first == k)
166 {
if (wkold == w.end()) wkold = w.begin();
else ++wkold; ++wk; }
170 if (gmm::abs(tmp) <= max_pivot) {
171 GMM_WARNING2(
"pivot " << i <<
" too small. try with ilutp ?");
175 max_pivot = std::max(max_pivot, std::min(gmm::abs(tmp) * prec, R(1)));
176 indiag[i] = T(1) / tmp;
179 std::sort(ww.begin(), ww.end(), elt_rsvector_value_less_<T>());
180 typename _rsvector::const_iterator wit = ww.begin(), wite = ww.end();
183 wL.base_resize(K); wU.base_resize(K+1);
184 typename _rsvector::iterator witL = wL.begin(), witU = wU.begin();
185 for (; wit != wite; ++wit)
186 if (wit->c < i) {
if (nnl < K) { *witL++ = *wit; ++nnl; } }
187 else {
if (nnu < K || wit->c == i) { *witU++ = *wit; ++nnu; } }
188 wL.base_resize(nnl); wU.base_resize(nnu);
189 std::sort(wL.begin(), wL.end());
190 std::sort(wU.begin(), wU.end());
197 template<
typename Matrix>
198 void ilut_precond<Matrix>::do_ilut(
const Matrix& A, col_major) {
199 do_ilut(gmm::transposed(A), row_major());
203 template <
typename Matrix,
typename V1,
typename V2>
inline
204 void mult(
const ilut_precond<Matrix>& P,
const V1 &v1, V2 &v2) {
207 gmm::lower_tri_solve(gmm::transposed(P.U), v2,
false);
208 gmm::upper_tri_solve(gmm::transposed(P.L), v2,
true);
211 gmm::lower_tri_solve(P.L, v2,
true);
212 gmm::upper_tri_solve(P.U, v2,
false);
216 template <
typename Matrix,
typename V1,
typename V2>
inline
217 void transposed_mult(
const ilut_precond<Matrix>& P,
const V1 &v1,V2 &v2) {
220 gmm::lower_tri_solve(P.L, v2,
true);
221 gmm::upper_tri_solve(P.U, v2,
false);
224 gmm::lower_tri_solve(gmm::transposed(P.U), v2,
false);
225 gmm::upper_tri_solve(gmm::transposed(P.L), v2,
true);
229 template <
typename Matrix,
typename V1,
typename V2>
inline
230 void left_mult(
const ilut_precond<Matrix>& P,
const V1 &v1, V2 &v2) {
232 if (P.invert) gmm::lower_tri_solve(gmm::transposed(P.U), v2,
false);
233 else gmm::lower_tri_solve(P.L, v2,
true);
236 template <
typename Matrix,
typename V1,
typename V2>
inline
237 void right_mult(
const ilut_precond<Matrix>& P,
const V1 &v1, V2 &v2) {
239 if (P.invert) gmm::upper_tri_solve(gmm::transposed(P.L), v2,
true);
240 else gmm::upper_tri_solve(P.U, v2,
false);
243 template <
typename Matrix,
typename V1,
typename V2>
inline
244 void transposed_left_mult(
const ilut_precond<Matrix>& P,
const V1 &v1,
247 if (P.invert) gmm::upper_tri_solve(P.U, v2,
false);
248 else gmm::upper_tri_solve(gmm::transposed(P.L), v2,
true);
251 template <
typename Matrix,
typename V1,
typename V2>
inline
252 void transposed_right_mult(
const ilut_precond<Matrix>& P,
const V1 &v1,
255 if (P.invert) gmm::lower_tri_solve(P.L, v2,
true);
256 else gmm::lower_tri_solve(gmm::transposed(P.U), v2,
false);
Incomplete LU with threshold and K fill-in Preconditioner.
sparse vector built upon std::vector.
sparse vector built upon std::map.
size_type nnz(const L &l)
count the number of non-zero entries of a vector or matrix.
void copy(const L1 &l1, L2 &l2)
*/
number_traits< typename linalg_traits< V >::value_type >::magnitude_type vect_norm2(const V &v)
Euclidean norm of a vector.
void clear(L &l)
clear (fill with zeros) a vector or matrix.
void resize(V &v, size_type n)
*/
void clean(L &l, double threshold)
Clean a vector or matrix (replace near-zero entries with zeroes).
void mult(const L1 &l1, const L2 &l2, L3 &l3)
*/
void add(const L1 &l1, L2 &l2)
*/
size_t size_type
used as the common size type in the library