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 (); |
|
57 umfpack_di_defaults (control); |
|
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 |
|
87 umfpack_di_report_control (control); |
|
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 |
|
93 umfpack_di_report_matrix (nr, nc, Ap, Ai, Ax, 1, control); |
|
94 |
|
95 void *Symbolic; |
|
96 Matrix Info (1, UMFPACK_INFO); |
|
97 double *info = Info.fortran_vec (); |
|
98 int status = umfpack_di_qsymbolic (nr, nc, Ap, Ai, Ax, NULL, |
|
99 &Symbolic, control, info); |
|
100 |
|
101 if (status < 0) |
|
102 { |
|
103 (*current_liboctave_error_handler) |
|
104 ("SparseLU::SparseLU symbolic factorization failed"); |
|
105 |
|
106 umfpack_di_report_status (control, status); |
|
107 umfpack_di_report_info (control, info); |
|
108 |
|
109 umfpack_di_free_symbolic (&Symbolic) ; |
|
110 } |
|
111 else |
|
112 { |
|
113 umfpack_di_report_symbolic (Symbolic, control); |
|
114 |
|
115 void *Numeric; |
|
116 status = umfpack_di_numeric (Ap, Ai, Ax, Symbolic, &Numeric, |
|
117 control, info) ; |
|
118 umfpack_di_free_symbolic (&Symbolic) ; |
|
119 |
|
120 cond = Info (UMFPACK_RCOND); |
|
121 |
|
122 if (status < 0) |
|
123 { |
|
124 (*current_liboctave_error_handler) |
|
125 ("SparseLU::SparseLU numeric factorization failed"); |
|
126 |
|
127 umfpack_di_report_status (control, status); |
|
128 umfpack_di_report_info (control, info); |
|
129 |
|
130 umfpack_di_free_numeric (&Numeric); |
|
131 } |
|
132 else |
|
133 { |
|
134 umfpack_di_report_numeric (Numeric, control); |
|
135 |
|
136 int lnz, unz, ignore1, ignore2, ignore3; |
|
137 status = umfpack_di_get_lunz (&lnz, &unz, &ignore1, &ignore2, |
|
138 &ignore3, Numeric) ; |
|
139 |
|
140 if (status < 0) |
|
141 { |
|
142 (*current_liboctave_error_handler) |
|
143 ("SparseLU::SparseLU extracting LU factors failed"); |
|
144 |
|
145 umfpack_di_report_status (control, status); |
|
146 umfpack_di_report_info (control, info); |
|
147 |
|
148 umfpack_di_free_numeric (&Numeric); |
|
149 } |
|
150 else |
|
151 { |
|
152 int n_inner = (nr < nc ? nr : nc); |
|
153 |
|
154 if (lnz < 1) |
5275
|
155 Lfact = SparseMatrix (static_cast<octave_idx_type> (n_inner), nr, |
|
156 static_cast<octave_idx_type> (1)); |
5164
|
157 else |
5275
|
158 Lfact = SparseMatrix (static_cast<octave_idx_type> (n_inner), nr, |
|
159 static_cast<octave_idx_type> (lnz)); |
5164
|
160 |
5275
|
161 octave_idx_type *Ltp = Lfact.cidx (); |
|
162 octave_idx_type *Ltj = Lfact.ridx (); |
5164
|
163 double *Ltx = Lfact.data (); |
|
164 |
|
165 if (unz < 1) |
5275
|
166 Ufact = SparseMatrix (static_cast<octave_idx_type> (n_inner), nc, |
|
167 static_cast<octave_idx_type> (1)); |
5164
|
168 else |
5275
|
169 Ufact = SparseMatrix (static_cast<octave_idx_type> (n_inner), nc, |
|
170 static_cast<octave_idx_type> (unz)); |
5164
|
171 |
5275
|
172 octave_idx_type *Up = Ufact.cidx (); |
|
173 octave_idx_type *Uj = Ufact.ridx (); |
5164
|
174 double *Ux = Ufact.data (); |
|
175 |
|
176 P.resize (nr); |
|
177 int *p = P.fortran_vec (); |
|
178 |
|
179 Q.resize (nc); |
|
180 int *q = Q.fortran_vec (); |
|
181 |
|
182 int do_recip; |
|
183 status = umfpack_di_get_numeric (Ltp, Ltj, Ltx, Up, Uj, |
|
184 Ux, p, q, (double *) NULL, |
|
185 &do_recip, (double *) NULL, |
|
186 Numeric) ; |
|
187 |
|
188 umfpack_di_free_numeric (&Numeric) ; |
|
189 |
|
190 if (status < 0 || do_recip) |
|
191 { |
|
192 (*current_liboctave_error_handler) |
|
193 ("SparseLU::SparseLU extracting LU factors failed"); |
|
194 |
|
195 umfpack_di_report_status (control, status); |
|
196 } |
|
197 else |
|
198 { |
|
199 Lfact = Lfact.transpose (); |
|
200 |
|
201 umfpack_di_report_matrix (nr, n_inner, Lfact.cidx (), |
|
202 Lfact.ridx (), Lfact.data (), |
|
203 1, control); |
|
204 umfpack_di_report_matrix (n_inner, nc, Ufact.cidx (), |
|
205 Ufact.ridx (), Ufact.data (), |
|
206 1, control); |
|
207 umfpack_di_report_perm (nr, p, control); |
|
208 umfpack_di_report_perm (nc, q, control); |
|
209 } |
|
210 |
|
211 umfpack_di_report_info (control, info); |
|
212 } |
|
213 } |
|
214 } |
5203
|
215 #else |
|
216 (*current_liboctave_error_handler) ("UMFPACK not installed"); |
|
217 #endif |
5164
|
218 } |
|
219 |
|
220 SparseLU::SparseLU (const SparseMatrix& a, const ColumnVector& Qinit, |
5282
|
221 double piv_thres, bool FixedQ, double droptol, |
|
222 bool milu, bool udiag) |
5164
|
223 { |
5203
|
224 #ifdef HAVE_UMFPACK |
5282
|
225 if (milu) |
|
226 (*current_liboctave_error_handler) |
|
227 ("Modified incomplete LU not implemented"); |
5164
|
228 else |
|
229 { |
5282
|
230 octave_idx_type nr = a.rows (); |
|
231 octave_idx_type nc = a.cols (); |
5164
|
232 |
5282
|
233 // Setup the control parameters |
|
234 Matrix Control (UMFPACK_CONTROL, 1); |
|
235 double *control = Control.fortran_vec (); |
|
236 umfpack_di_defaults (control); |
5164
|
237 |
5282
|
238 double tmp = Voctave_sparse_controls.get_key ("spumoni"); |
|
239 if (!xisnan (tmp)) |
|
240 Control (UMFPACK_PRL) = tmp; |
|
241 if (piv_thres >= 0.) |
|
242 { |
|
243 piv_thres = (piv_thres > 1. ? 1. : piv_thres); |
|
244 Control (UMFPACK_SYM_PIVOT_TOLERANCE) = piv_thres; |
|
245 Control (UMFPACK_PIVOT_TOLERANCE) = piv_thres; |
|
246 } |
|
247 else |
|
248 { |
|
249 tmp = Voctave_sparse_controls.get_key ("piv_tol"); |
|
250 if (!xisnan (tmp)) |
|
251 { |
|
252 Control (UMFPACK_SYM_PIVOT_TOLERANCE) = tmp; |
|
253 Control (UMFPACK_PIVOT_TOLERANCE) = tmp; |
|
254 } |
|
255 } |
5164
|
256 |
5282
|
257 if (droptol >= 0.) |
|
258 Control (UMFPACK_DROPTOL) = droptol; |
5164
|
259 |
|
260 |
5282
|
261 // Set whether we are allowed to modify Q or not |
|
262 if (FixedQ) |
|
263 Control (UMFPACK_FIXQ) = 1.0; |
|
264 else |
|
265 { |
|
266 tmp = Voctave_sparse_controls.get_key ("autoamd"); |
|
267 if (!xisnan (tmp)) |
|
268 Control (UMFPACK_FIXQ) = tmp; |
|
269 } |
5164
|
270 |
5282
|
271 // Turn-off UMFPACK scaling for LU |
|
272 Control (UMFPACK_SCALE) = UMFPACK_SCALE_NONE; |
5164
|
273 |
5282
|
274 umfpack_di_report_control (control); |
5164
|
275 |
5282
|
276 const octave_idx_type *Ap = a.cidx (); |
|
277 const octave_idx_type *Ai = a.ridx (); |
|
278 const double *Ax = a.data (); |
|
279 |
|
280 umfpack_di_report_matrix (nr, nc, Ap, Ai, Ax, 1, control); |
|
281 |
|
282 void *Symbolic; |
|
283 Matrix Info (1, UMFPACK_INFO); |
|
284 double *info = Info.fortran_vec (); |
|
285 int status; |
5164
|
286 |
5282
|
287 // Null loop so that qinit is imediately deallocated when not needed |
|
288 do { |
|
289 OCTAVE_LOCAL_BUFFER (int, qinit, nc); |
5164
|
290 |
5282
|
291 for (int i = 0; i < nc; i++) |
|
292 qinit [i] = static_cast<int> (Qinit (i)); |
5164
|
293 |
5282
|
294 status = umfpack_di_qsymbolic (nr, nc, Ap, Ai, Ax, qinit, |
|
295 &Symbolic, control, info); |
|
296 } while (0); |
5164
|
297 |
|
298 if (status < 0) |
|
299 { |
|
300 (*current_liboctave_error_handler) |
5282
|
301 ("SparseLU::SparseLU symbolic factorization failed"); |
5164
|
302 |
|
303 umfpack_di_report_status (control, status); |
|
304 umfpack_di_report_info (control, info); |
|
305 |
5282
|
306 umfpack_di_free_symbolic (&Symbolic) ; |
5164
|
307 } |
|
308 else |
|
309 { |
5282
|
310 umfpack_di_report_symbolic (Symbolic, control); |
5164
|
311 |
5282
|
312 void *Numeric; |
|
313 status = umfpack_di_numeric (Ap, Ai, Ax, Symbolic, &Numeric, |
|
314 control, info) ; |
|
315 umfpack_di_free_symbolic (&Symbolic) ; |
|
316 |
|
317 cond = Info (UMFPACK_RCOND); |
|
318 |
5164
|
319 if (status < 0) |
|
320 { |
|
321 (*current_liboctave_error_handler) |
5282
|
322 ("SparseLU::SparseLU numeric factorization failed"); |
5164
|
323 |
|
324 umfpack_di_report_status (control, status); |
|
325 umfpack_di_report_info (control, info); |
|
326 |
|
327 umfpack_di_free_numeric (&Numeric); |
|
328 } |
|
329 else |
|
330 { |
5282
|
331 umfpack_di_report_numeric (Numeric, control); |
5164
|
332 |
5282
|
333 int lnz, unz, ignore1, ignore2, ignore3; |
|
334 status = umfpack_di_get_lunz (&lnz, &unz, &ignore1, &ignore2, |
|
335 &ignore3, Numeric) ; |
|
336 |
|
337 if (status < 0) |
5164
|
338 { |
|
339 (*current_liboctave_error_handler) |
|
340 ("SparseLU::SparseLU extracting LU factors failed"); |
|
341 |
|
342 umfpack_di_report_status (control, status); |
5282
|
343 umfpack_di_report_info (control, info); |
|
344 |
|
345 umfpack_di_free_numeric (&Numeric); |
5164
|
346 } |
|
347 else |
|
348 { |
5282
|
349 int n_inner = (nr < nc ? nr : nc); |
|
350 |
|
351 if (lnz < 1) |
|
352 Lfact = SparseMatrix |
|
353 (static_cast<octave_idx_type> (n_inner), nr, |
|
354 static_cast<octave_idx_type> (1)); |
|
355 else |
|
356 Lfact = SparseMatrix |
|
357 (static_cast<octave_idx_type> (n_inner), nr, |
|
358 static_cast<octave_idx_type> (lnz)); |
|
359 |
|
360 octave_idx_type *Ltp = Lfact.cidx (); |
|
361 octave_idx_type *Ltj = Lfact.ridx (); |
|
362 double *Ltx = Lfact.data (); |
|
363 |
|
364 if (unz < 1) |
|
365 Ufact = SparseMatrix |
|
366 (static_cast<octave_idx_type> (n_inner), nc, |
|
367 static_cast<octave_idx_type> (1)); |
|
368 else |
|
369 Ufact = SparseMatrix |
|
370 (static_cast<octave_idx_type> (n_inner), nc, |
|
371 static_cast<octave_idx_type> (unz)); |
|
372 |
|
373 octave_idx_type *Up = Ufact.cidx (); |
|
374 octave_idx_type *Uj = Ufact.ridx (); |
|
375 double *Ux = Ufact.data (); |
|
376 |
|
377 P.resize (nr); |
|
378 int *p = P.fortran_vec (); |
|
379 |
|
380 Q.resize (nc); |
|
381 int *q = Q.fortran_vec (); |
|
382 |
|
383 int do_recip; |
|
384 status = umfpack_di_get_numeric (Ltp, Ltj, Ltx, Up, Uj, |
|
385 Ux, p, q, |
|
386 (double *) NULL, |
|
387 &do_recip, |
|
388 (double *) NULL, |
|
389 Numeric) ; |
|
390 |
|
391 umfpack_di_free_numeric (&Numeric) ; |
|
392 |
|
393 if (status < 0 || do_recip) |
|
394 { |
|
395 (*current_liboctave_error_handler) |
|
396 ("SparseLU::SparseLU extracting LU factors failed"); |
|
397 |
|
398 umfpack_di_report_status (control, status); |
|
399 } |
|
400 else |
|
401 { |
|
402 Lfact = Lfact.transpose (); |
|
403 umfpack_di_report_matrix (nr, n_inner, |
|
404 Lfact.cidx (), |
|
405 Lfact.ridx (), |
|
406 Lfact.data (), |
|
407 1, control); |
|
408 umfpack_di_report_matrix (n_inner, nc, |
|
409 Ufact.cidx (), |
|
410 Ufact.ridx (), |
|
411 Ufact.data (), |
|
412 1, control); |
|
413 umfpack_di_report_perm (nr, p, control); |
|
414 umfpack_di_report_perm (nc, q, control); |
|
415 } |
|
416 |
|
417 umfpack_di_report_info (control, info); |
5164
|
418 } |
|
419 } |
|
420 } |
5282
|
421 |
|
422 if (udiag) |
|
423 (*current_liboctave_error_handler) |
|
424 ("Option udiag of incomplete LU not implemented"); |
5164
|
425 } |
5203
|
426 #else |
|
427 (*current_liboctave_error_handler) ("UMFPACK not installed"); |
|
428 #endif |
5164
|
429 } |
|
430 |
|
431 /* |
|
432 ;;; Local Variables: *** |
|
433 ;;; mode: C++ *** |
|
434 ;;; End: *** |
|
435 */ |
|
436 |