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