5164
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 2004, 2005, 2006, 2007 David Bateman |
7016
|
4 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 Andy Adler |
|
5 |
|
6 This file is part of Octave. |
5164
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
5164
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
5164
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <vector> |
|
29 |
|
30 #include "lo-error.h" |
|
31 |
|
32 #include "SparsedbleLU.h" |
|
33 #include "oct-spparms.h" |
|
34 |
|
35 // Instantiate the base LU class for the types we need. |
|
36 |
|
37 #include "sparse-base-lu.h" |
|
38 #include "sparse-base-lu.cc" |
|
39 |
|
40 template class sparse_base_lu <SparseMatrix, double, SparseMatrix, double>; |
|
41 |
5451
|
42 #include "oct-sparse.h" |
5164
|
43 |
|
44 SparseLU::SparseLU (const SparseMatrix& a, double piv_thres) |
|
45 { |
5203
|
46 #ifdef HAVE_UMFPACK |
5275
|
47 octave_idx_type nr = a.rows (); |
|
48 octave_idx_type nc = a.cols (); |
5164
|
49 |
|
50 // Setup the control parameters |
|
51 Matrix Control (UMFPACK_CONTROL, 1); |
|
52 double *control = Control.fortran_vec (); |
5322
|
53 UMFPACK_DNAME (defaults) (control); |
5164
|
54 |
5893
|
55 double tmp = octave_sparse_params::get_key ("spumoni"); |
5164
|
56 if (!xisnan (tmp)) |
|
57 Control (UMFPACK_PRL) = tmp; |
|
58 |
|
59 if (piv_thres >= 0.) |
|
60 { |
|
61 piv_thres = (piv_thres > 1. ? 1. : piv_thres); |
|
62 Control (UMFPACK_SYM_PIVOT_TOLERANCE) = piv_thres; |
|
63 Control (UMFPACK_PIVOT_TOLERANCE) = piv_thres; |
|
64 } |
|
65 else |
|
66 { |
5893
|
67 tmp = octave_sparse_params::get_key ("piv_tol"); |
5164
|
68 if (!xisnan (tmp)) |
|
69 { |
|
70 Control (UMFPACK_SYM_PIVOT_TOLERANCE) = tmp; |
|
71 Control (UMFPACK_PIVOT_TOLERANCE) = tmp; |
|
72 } |
|
73 } |
|
74 |
|
75 // Set whether we are allowed to modify Q or not |
5893
|
76 tmp = octave_sparse_params::get_key ("autoamd"); |
5164
|
77 if (!xisnan (tmp)) |
|
78 Control (UMFPACK_FIXQ) = tmp; |
|
79 |
|
80 // Turn-off UMFPACK scaling for LU |
|
81 Control (UMFPACK_SCALE) = UMFPACK_SCALE_NONE; |
|
82 |
5322
|
83 UMFPACK_DNAME (report_control) (control); |
5164
|
84 |
5275
|
85 const octave_idx_type *Ap = a.cidx (); |
|
86 const octave_idx_type *Ai = a.ridx (); |
5164
|
87 const double *Ax = a.data (); |
|
88 |
5322
|
89 UMFPACK_DNAME (report_matrix) (nr, nc, Ap, Ai, Ax, 1, control); |
5164
|
90 |
|
91 void *Symbolic; |
|
92 Matrix Info (1, UMFPACK_INFO); |
|
93 double *info = Info.fortran_vec (); |
5322
|
94 int status = UMFPACK_DNAME (qsymbolic) (nr, nc, Ap, Ai, Ax, NULL, |
5164
|
95 &Symbolic, control, info); |
|
96 |
|
97 if (status < 0) |
|
98 { |
|
99 (*current_liboctave_error_handler) |
|
100 ("SparseLU::SparseLU symbolic factorization failed"); |
|
101 |
5322
|
102 UMFPACK_DNAME (report_status) (control, status); |
|
103 UMFPACK_DNAME (report_info) (control, info); |
5164
|
104 |
5322
|
105 UMFPACK_DNAME (free_symbolic) (&Symbolic) ; |
5164
|
106 } |
|
107 else |
|
108 { |
5322
|
109 UMFPACK_DNAME (report_symbolic) (Symbolic, control); |
5164
|
110 |
|
111 void *Numeric; |
5322
|
112 status = UMFPACK_DNAME (numeric) (Ap, Ai, Ax, Symbolic, |
|
113 &Numeric, control, info) ; |
|
114 UMFPACK_DNAME (free_symbolic) (&Symbolic) ; |
5164
|
115 |
|
116 cond = Info (UMFPACK_RCOND); |
|
117 |
|
118 if (status < 0) |
|
119 { |
|
120 (*current_liboctave_error_handler) |
|
121 ("SparseLU::SparseLU numeric factorization failed"); |
|
122 |
5322
|
123 UMFPACK_DNAME (report_status) (control, status); |
|
124 UMFPACK_DNAME (report_info) (control, info); |
5164
|
125 |
5322
|
126 UMFPACK_DNAME (free_numeric) (&Numeric); |
5164
|
127 } |
|
128 else |
|
129 { |
5322
|
130 UMFPACK_DNAME (report_numeric) (Numeric, control); |
5164
|
131 |
5322
|
132 octave_idx_type lnz, unz, ignore1, ignore2, ignore3; |
|
133 status = UMFPACK_DNAME (get_lunz) (&lnz, &unz, &ignore1, |
|
134 &ignore2, &ignore3, Numeric) ; |
5164
|
135 |
|
136 if (status < 0) |
|
137 { |
|
138 (*current_liboctave_error_handler) |
|
139 ("SparseLU::SparseLU extracting LU factors failed"); |
|
140 |
5322
|
141 UMFPACK_DNAME (report_status) (control, status); |
|
142 UMFPACK_DNAME (report_info) (control, info); |
5164
|
143 |
5322
|
144 UMFPACK_DNAME (free_numeric) (&Numeric); |
5164
|
145 } |
|
146 else |
|
147 { |
5322
|
148 octave_idx_type n_inner = (nr < nc ? nr : nc); |
5164
|
149 |
|
150 if (lnz < 1) |
5322
|
151 Lfact = SparseMatrix (n_inner, nr, |
5275
|
152 static_cast<octave_idx_type> (1)); |
5164
|
153 else |
5322
|
154 Lfact = SparseMatrix (n_inner, nr, lnz); |
5164
|
155 |
5275
|
156 octave_idx_type *Ltp = Lfact.cidx (); |
|
157 octave_idx_type *Ltj = Lfact.ridx (); |
5164
|
158 double *Ltx = Lfact.data (); |
|
159 |
|
160 if (unz < 1) |
5322
|
161 Ufact = SparseMatrix (n_inner, nc, |
5275
|
162 static_cast<octave_idx_type> (1)); |
5164
|
163 else |
5322
|
164 Ufact = SparseMatrix (n_inner, nc, unz); |
5164
|
165 |
5275
|
166 octave_idx_type *Up = Ufact.cidx (); |
|
167 octave_idx_type *Uj = Ufact.ridx (); |
5164
|
168 double *Ux = Ufact.data (); |
|
169 |
|
170 P.resize (nr); |
5322
|
171 octave_idx_type *p = P.fortran_vec (); |
5164
|
172 |
|
173 Q.resize (nc); |
5322
|
174 octave_idx_type *q = Q.fortran_vec (); |
5164
|
175 |
5322
|
176 octave_idx_type do_recip; |
|
177 status = UMFPACK_DNAME (get_numeric) (Ltp, Ltj, Ltx, |
5760
|
178 Up, Uj, Ux, p, q, NULL, |
|
179 &do_recip, NULL, |
5164
|
180 Numeric) ; |
|
181 |
5322
|
182 UMFPACK_DNAME (free_numeric) (&Numeric) ; |
5164
|
183 |
|
184 if (status < 0 || do_recip) |
|
185 { |
|
186 (*current_liboctave_error_handler) |
|
187 ("SparseLU::SparseLU extracting LU factors failed"); |
|
188 |
5322
|
189 UMFPACK_DNAME (report_status) (control, status); |
5164
|
190 } |
|
191 else |
|
192 { |
|
193 Lfact = Lfact.transpose (); |
|
194 |
5322
|
195 UMFPACK_DNAME (report_matrix) (nr, n_inner, |
|
196 Lfact.cidx (), Lfact.ridx (), |
|
197 Lfact.data (), 1, control); |
|
198 UMFPACK_DNAME (report_matrix) (n_inner, nc, |
|
199 Ufact.cidx (), Ufact.ridx (), |
|
200 Ufact.data (), 1, control); |
|
201 UMFPACK_DNAME (report_perm) (nr, p, control); |
|
202 UMFPACK_DNAME (report_perm) (nc, q, control); |
5164
|
203 } |
|
204 |
5322
|
205 UMFPACK_DNAME (report_info) (control, info); |
5164
|
206 } |
|
207 } |
|
208 } |
5203
|
209 #else |
|
210 (*current_liboctave_error_handler) ("UMFPACK not installed"); |
|
211 #endif |
5164
|
212 } |
|
213 |
|
214 SparseLU::SparseLU (const SparseMatrix& a, const ColumnVector& Qinit, |
5282
|
215 double piv_thres, bool FixedQ, double droptol, |
|
216 bool milu, bool udiag) |
5164
|
217 { |
5203
|
218 #ifdef HAVE_UMFPACK |
5282
|
219 if (milu) |
|
220 (*current_liboctave_error_handler) |
|
221 ("Modified incomplete LU not implemented"); |
5164
|
222 else |
|
223 { |
5282
|
224 octave_idx_type nr = a.rows (); |
|
225 octave_idx_type nc = a.cols (); |
5164
|
226 |
5282
|
227 // Setup the control parameters |
|
228 Matrix Control (UMFPACK_CONTROL, 1); |
|
229 double *control = Control.fortran_vec (); |
5322
|
230 UMFPACK_DNAME (defaults) (control); |
5164
|
231 |
5893
|
232 double tmp = octave_sparse_params::get_key ("spumoni"); |
5282
|
233 if (!xisnan (tmp)) |
|
234 Control (UMFPACK_PRL) = tmp; |
|
235 if (piv_thres >= 0.) |
|
236 { |
|
237 piv_thres = (piv_thres > 1. ? 1. : piv_thres); |
|
238 Control (UMFPACK_SYM_PIVOT_TOLERANCE) = piv_thres; |
|
239 Control (UMFPACK_PIVOT_TOLERANCE) = piv_thres; |
|
240 } |
|
241 else |
|
242 { |
5893
|
243 tmp = octave_sparse_params::get_key ("piv_tol"); |
5282
|
244 if (!xisnan (tmp)) |
|
245 { |
|
246 Control (UMFPACK_SYM_PIVOT_TOLERANCE) = tmp; |
|
247 Control (UMFPACK_PIVOT_TOLERANCE) = tmp; |
|
248 } |
|
249 } |
5164
|
250 |
5282
|
251 if (droptol >= 0.) |
|
252 Control (UMFPACK_DROPTOL) = droptol; |
5164
|
253 |
|
254 |
5282
|
255 // Set whether we are allowed to modify Q or not |
|
256 if (FixedQ) |
|
257 Control (UMFPACK_FIXQ) = 1.0; |
|
258 else |
|
259 { |
5893
|
260 tmp = octave_sparse_params::get_key ("autoamd"); |
5282
|
261 if (!xisnan (tmp)) |
|
262 Control (UMFPACK_FIXQ) = tmp; |
|
263 } |
5164
|
264 |
5282
|
265 // Turn-off UMFPACK scaling for LU |
|
266 Control (UMFPACK_SCALE) = UMFPACK_SCALE_NONE; |
5164
|
267 |
5322
|
268 UMFPACK_DNAME (report_control) (control); |
5164
|
269 |
5282
|
270 const octave_idx_type *Ap = a.cidx (); |
|
271 const octave_idx_type *Ai = a.ridx (); |
|
272 const double *Ax = a.data (); |
|
273 |
5322
|
274 UMFPACK_DNAME (report_matrix) (nr, nc, Ap, Ai, Ax, 1, |
|
275 control); |
5282
|
276 |
|
277 void *Symbolic; |
|
278 Matrix Info (1, UMFPACK_INFO); |
|
279 double *info = Info.fortran_vec (); |
|
280 int status; |
5164
|
281 |
5282
|
282 // Null loop so that qinit is imediately deallocated when not needed |
|
283 do { |
5322
|
284 OCTAVE_LOCAL_BUFFER (octave_idx_type, qinit, nc); |
5164
|
285 |
5322
|
286 for (octave_idx_type i = 0; i < nc; i++) |
|
287 qinit [i] = static_cast<octave_idx_type> (Qinit (i)); |
5164
|
288 |
5322
|
289 status = UMFPACK_DNAME (qsymbolic) (nr, nc, Ap, Ai, Ax, |
|
290 qinit, &Symbolic, control, info); |
5282
|
291 } while (0); |
5164
|
292 |
|
293 if (status < 0) |
|
294 { |
|
295 (*current_liboctave_error_handler) |
5282
|
296 ("SparseLU::SparseLU symbolic factorization failed"); |
5164
|
297 |
5322
|
298 UMFPACK_DNAME (report_status) (control, status); |
|
299 UMFPACK_DNAME (report_info) (control, info); |
5164
|
300 |
5322
|
301 UMFPACK_DNAME (free_symbolic) (&Symbolic) ; |
5164
|
302 } |
|
303 else |
|
304 { |
5322
|
305 UMFPACK_DNAME (report_symbolic) (Symbolic, control); |
5164
|
306 |
5282
|
307 void *Numeric; |
5322
|
308 status = UMFPACK_DNAME (numeric) (Ap, Ai, Ax, Symbolic, |
|
309 &Numeric, control, info) ; |
|
310 UMFPACK_DNAME (free_symbolic) (&Symbolic) ; |
5282
|
311 |
|
312 cond = Info (UMFPACK_RCOND); |
|
313 |
5164
|
314 if (status < 0) |
|
315 { |
|
316 (*current_liboctave_error_handler) |
5282
|
317 ("SparseLU::SparseLU numeric factorization failed"); |
5164
|
318 |
5322
|
319 UMFPACK_DNAME (report_status) (control, status); |
|
320 UMFPACK_DNAME (report_info) (control, info); |
5164
|
321 |
5322
|
322 UMFPACK_DNAME (free_numeric) (&Numeric); |
5164
|
323 } |
|
324 else |
|
325 { |
5322
|
326 UMFPACK_DNAME (report_numeric) (Numeric, control); |
5164
|
327 |
5322
|
328 octave_idx_type lnz, unz, ignore1, ignore2, ignore3; |
|
329 status = UMFPACK_DNAME (get_lunz) (&lnz, &unz, &ignore1, &ignore2, |
|
330 &ignore3, Numeric) ; |
5282
|
331 |
|
332 if (status < 0) |
5164
|
333 { |
|
334 (*current_liboctave_error_handler) |
|
335 ("SparseLU::SparseLU extracting LU factors failed"); |
|
336 |
5322
|
337 UMFPACK_DNAME (report_status) (control, status); |
|
338 UMFPACK_DNAME (report_info) (control, info); |
5282
|
339 |
5322
|
340 UMFPACK_DNAME (free_numeric) (&Numeric); |
5164
|
341 } |
|
342 else |
|
343 { |
5322
|
344 octave_idx_type n_inner = (nr < nc ? nr : nc); |
5282
|
345 |
|
346 if (lnz < 1) |
5322
|
347 Lfact = SparseMatrix (n_inner, nr, |
|
348 static_cast<octave_idx_type> (1)); |
5282
|
349 else |
5322
|
350 Lfact = SparseMatrix (n_inner, nr, lnz); |
5282
|
351 |
|
352 octave_idx_type *Ltp = Lfact.cidx (); |
|
353 octave_idx_type *Ltj = Lfact.ridx (); |
|
354 double *Ltx = Lfact.data (); |
|
355 |
|
356 if (unz < 1) |
5322
|
357 Ufact = SparseMatrix (n_inner, nc, |
|
358 static_cast<octave_idx_type> (1)); |
5282
|
359 else |
5322
|
360 Ufact = SparseMatrix (n_inner, nc, unz); |
5282
|
361 |
|
362 octave_idx_type *Up = Ufact.cidx (); |
|
363 octave_idx_type *Uj = Ufact.ridx (); |
|
364 double *Ux = Ufact.data (); |
|
365 |
|
366 P.resize (nr); |
5322
|
367 octave_idx_type *p = P.fortran_vec (); |
5282
|
368 |
|
369 Q.resize (nc); |
5322
|
370 octave_idx_type *q = Q.fortran_vec (); |
5282
|
371 |
5322
|
372 octave_idx_type do_recip; |
|
373 status = UMFPACK_DNAME (get_numeric) (Ltp, Ltj, |
|
374 Ltx, Up, Uj, Ux, p, q, |
5760
|
375 NULL, &do_recip, |
|
376 NULL, Numeric) ; |
5282
|
377 |
5322
|
378 UMFPACK_DNAME (free_numeric) (&Numeric) ; |
5282
|
379 |
|
380 if (status < 0 || do_recip) |
|
381 { |
|
382 (*current_liboctave_error_handler) |
|
383 ("SparseLU::SparseLU extracting LU factors failed"); |
|
384 |
5322
|
385 UMFPACK_DNAME (report_status) (control, status); |
5282
|
386 } |
|
387 else |
|
388 { |
|
389 Lfact = Lfact.transpose (); |
5322
|
390 UMFPACK_DNAME (report_matrix) (nr, n_inner, |
5282
|
391 Lfact.cidx (), |
|
392 Lfact.ridx (), |
|
393 Lfact.data (), |
|
394 1, control); |
5322
|
395 UMFPACK_DNAME (report_matrix) (n_inner, nc, |
5282
|
396 Ufact.cidx (), |
|
397 Ufact.ridx (), |
|
398 Ufact.data (), |
|
399 1, control); |
5322
|
400 UMFPACK_DNAME (report_perm) (nr, p, control); |
|
401 UMFPACK_DNAME (report_perm) (nc, q, control); |
5282
|
402 } |
|
403 |
5322
|
404 UMFPACK_DNAME (report_info) (control, info); |
5164
|
405 } |
|
406 } |
|
407 } |
5282
|
408 |
|
409 if (udiag) |
|
410 (*current_liboctave_error_handler) |
|
411 ("Option udiag of incomplete LU not implemented"); |
5164
|
412 } |
5203
|
413 #else |
|
414 (*current_liboctave_error_handler) ("UMFPACK not installed"); |
|
415 #endif |
5164
|
416 } |
|
417 |
|
418 /* |
|
419 ;;; Local Variables: *** |
|
420 ;;; mode: C++ *** |
|
421 ;;; End: *** |
|
422 */ |
|
423 |