39 #ifndef GMM_PRECOND_ILUTP_H
40 #define GMM_PRECOND_ILUTP_H
55 template <
typename Matrix>
58 typedef typename linalg_traits<Matrix>::value_type value_type;
61 typedef row_matrix<_rsvector> LU_Matrix;
62 typedef col_matrix<_wsvector> CLU_Matrix;
66 gmm::unsorted_sub_index indperm;
67 gmm::unsorted_sub_index indperminv;
68 mutable std::vector<value_type> temporary;
74 template<
typename M>
void do_ilutp(
const M&, row_major);
75 void do_ilutp(
const Matrix&, col_major);
78 void build_with(
const Matrix& A,
int k_ = -1,
double eps_ =
double(-1)) {
80 if (eps_ >=
double(0)) eps = eps_;
84 do_ilutp(A,
typename principal_orientation_type<
typename
85 linalg_traits<Matrix>::sub_orientation>::potype());
88 : L(mat_nrows(A), mat_ncols(A)), U(mat_nrows(A), mat_ncols(A)),
89 K(k_), eps(eps_) { build_with(A); }
92 size_type memsize()
const {
93 return sizeof(*this) + (
nnz(U)+
nnz(L))*
sizeof(value_type);
98 template<
typename Matrix>
template<
typename M>
100 typedef value_type T;
101 typedef typename number_traits<T>::magnitude_type R;
103 size_type n = mat_nrows(A);
106 std::vector<T> indiag(n);
108 std::vector<size_type> ipvt(n), ipvtinv(n);
109 for (size_type i = 0; i < n; ++i) ipvt[i] = ipvtinv[i] = i;
110 indperm = unsorted_sub_index(ipvt);
111 indperminv = unsorted_sub_index(ipvtinv);
112 _wsvector w(mat_ncols(A));
113 _rsvector ww(mat_ncols(A));
117 R prec = default_tol(R());
118 R max_pivot = gmm::abs(A(0,0)) * prec;
120 for (size_type i = 0; i < n; ++i) {
122 copy(sub_vector(mat_const_row(A, i), indperm), w);
125 typename _wsvector::iterator wkold = w.end();
126 for (
typename _wsvector::iterator wk = w.begin();
127 wk != w.end() && wk->first < i; ) {
128 size_type k = wk->first;
129 tmp = (wk->second) * indiag[k];
130 if (gmm::abs(tmp) < eps * norm_row) w.erase(k);
131 else { wk->second += tmp;
gmm::add(scaled(mat_row(U, k), -tmp), w); }
132 if (wkold == w.end()) wk = w.begin();
else { wk = wkold; ++wk; }
133 if (wk != w.end() && wk->first == k)
134 {
if (wkold == w.end()) wkold = w.begin();
else ++wkold; ++wk; }
140 std::sort(ww.begin(), ww.end(), elt_rsvector_value_less_<T>());
141 typename _rsvector::const_iterator wit = ww.begin(), wite = ww.end();
144 for (; wit != wite; ++wit)
145 if (wit->c >= i) { ip = wit->c; tmp = wit->e;
break; }
146 if (ip ==
size_type(-1) || gmm::abs(tmp) <= max_pivot)
147 { GMM_WARNING2(
"pivot " << i <<
" too small"); ip=i; ww[i]=tmp=T(1); }
148 max_pivot = std::max(max_pivot, std::min(gmm::abs(tmp) * prec, R(1)));
149 indiag[i] = T(1) / tmp;
153 L[i].base_resize(K); U[i].base_resize(K+1);
154 typename _rsvector::iterator witL = L[i].begin(), witU = U[i].begin();
155 for (; wit != wite; ++wit) {
156 if (wit->c < i) {
if (nnl < K) { *witL++ = *wit; ++nnl; } }
157 else if (nnu < K || wit->c == i)
158 { CU(i, wit->c) = wit->e; *witU++ = *wit; ++nnu; }
160 L[i].base_resize(nnl); U[i].base_resize(nnu);
161 std::sort(L[i].begin(), L[i].end());
162 std::sort(U[i].begin(), U[i].end());
165 typename _wsvector::const_iterator iti = CU.col(i).begin();
166 typename _wsvector::const_iterator itie = CU.col(i).end();
167 typename _wsvector::const_iterator itp = CU.col(ip).begin();
168 typename _wsvector::const_iterator itpe = CU.col(ip).end();
170 while (iti != itie && itp != itpe) {
171 if (iti->first < itp->first)
172 { U.row(iti->first).swap_indices(i, ip); ++iti; }
173 else if (iti->first > itp->first)
174 { U.row(itp->first).swap_indices(i,ip);++itp; }
176 { U.row(iti->first).swap_indices(i, ip); ++iti; ++itp; }
179 for( ; iti != itie; ++iti) U.row(iti->first).swap_indices(i, ip);
180 for( ; itp != itpe; ++itp) U.row(itp->first).swap_indices(i, ip);
185 indperminv.swap(ipvt[i], ipvt[ip]);
186 std::swap(ipvtinv[ipvt[i]], ipvtinv[ipvt[ip]]);
187 std::swap(ipvt[i], ipvt[ip]);
192 template<
typename Matrix>
193 void ilutp_precond<Matrix>::do_ilutp(
const Matrix& A, col_major) {
194 do_ilutp(gmm::transposed(A), row_major());
198 template <
typename Matrix,
typename V1,
typename V2>
inline
199 void mult(
const ilutp_precond<Matrix>& P,
const V1 &v1, V2 &v2) {
201 gmm::copy(gmm::sub_vector(v1, P.indperm), v2);
202 gmm::lower_tri_solve(gmm::transposed(P.U), v2,
false);
203 gmm::upper_tri_solve(gmm::transposed(P.L), v2,
true);
207 gmm::lower_tri_solve(P.L, P.temporary,
true);
208 gmm::upper_tri_solve(P.U, P.temporary,
false);
209 gmm::copy(gmm::sub_vector(P.temporary, P.indperminv), v2);
213 template <
typename Matrix,
typename V1,
typename V2>
inline
214 void transposed_mult(
const ilutp_precond<Matrix>& P,
const V1 &v1,V2 &v2) {
217 gmm::lower_tri_solve(P.L, P.temporary,
true);
218 gmm::upper_tri_solve(P.U, P.temporary,
false);
219 gmm::copy(gmm::sub_vector(P.temporary, P.indperminv), v2);
222 gmm::copy(gmm::sub_vector(v1, P.indperm), v2);
223 gmm::lower_tri_solve(gmm::transposed(P.U), v2,
false);
224 gmm::upper_tri_solve(gmm::transposed(P.L), v2,
true);
228 template <
typename Matrix,
typename V1,
typename V2>
inline
229 void left_mult(
const ilutp_precond<Matrix>& P,
const V1 &v1, V2 &v2) {
231 gmm::copy(gmm::sub_vector(v1, P.indperm), v2);
232 gmm::lower_tri_solve(gmm::transposed(P.U), v2,
false);
236 gmm::lower_tri_solve(P.L, v2,
true);
240 template <
typename Matrix,
typename V1,
typename V2>
inline
241 void right_mult(
const ilutp_precond<Matrix>& P,
const V1 &v1, V2 &v2) {
244 gmm::upper_tri_solve(gmm::transposed(P.L), v2,
true);
247 copy(v1, P.temporary);
248 gmm::upper_tri_solve(P.U, P.temporary,
false);
249 gmm::copy(gmm::sub_vector(P.temporary, P.indperminv), v2);
253 template <
typename Matrix,
typename V1,
typename V2>
inline
254 void transposed_left_mult(
const ilutp_precond<Matrix>& P,
const V1 &v1,
257 copy(v1, P.temporary);
258 gmm::upper_tri_solve(P.U, P.temporary,
false);
259 gmm::copy(gmm::sub_vector(P.temporary, P.indperminv), v2);
263 gmm::upper_tri_solve(gmm::transposed(P.L), v2,
true);
267 template <
typename Matrix,
typename V1,
typename V2>
inline
268 void transposed_right_mult(
const ilutp_precond<Matrix>& P,
const V1 &v1,
272 gmm::lower_tri_solve(P.L, v2,
true);
275 gmm::copy(gmm::sub_vector(v1, P.indperm), v2);
276 gmm::lower_tri_solve(gmm::transposed(P.U), v2,
false);
ILUTP: Incomplete LU with threshold and K fill-in Preconditioner and column pivoting.
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)
*/
ILUT: Incomplete LU with threshold and K fill-in Preconditioner.
size_t size_type
used as the common size type in the library