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 |
|
17 along with this program; see the file COPYING. If not, write to the Free |
|
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
19 |
|
20 */ |
|
21 |
|
22 #ifdef HAVE_CONFIG_H |
|
23 #include <config.h> |
|
24 #endif |
|
25 |
|
26 #include <vector> |
|
27 |
|
28 #include "lo-error.h" |
|
29 |
|
30 #include "SparsedbleLU.h" |
|
31 #include "oct-spparms.h" |
|
32 |
|
33 // Instantiate the base LU class for the types we need. |
|
34 |
|
35 #include "sparse-base-lu.h" |
|
36 #include "sparse-base-lu.cc" |
|
37 |
|
38 template class sparse_base_lu <SparseMatrix, double, SparseMatrix, double>; |
|
39 |
5203
|
40 #ifdef HAVE_UMFPACK |
5164
|
41 // Include the UMFPACK functions |
|
42 extern "C" { |
5203
|
43 #include <umfpack/umfpack.h> |
5164
|
44 } |
5203
|
45 #endif |
5164
|
46 |
|
47 SparseLU::SparseLU (const SparseMatrix& a, double piv_thres) |
|
48 { |
5203
|
49 #ifdef HAVE_UMFPACK |
5275
|
50 octave_idx_type nr = a.rows (); |
|
51 octave_idx_type nc = a.cols (); |
5164
|
52 |
|
53 // Setup the control parameters |
|
54 Matrix Control (UMFPACK_CONTROL, 1); |
|
55 double *control = Control.fortran_vec (); |
|
56 umfpack_di_defaults (control); |
|
57 |
|
58 double tmp = Voctave_sparse_controls.get_key ("spumoni"); |
|
59 if (!xisnan (tmp)) |
|
60 Control (UMFPACK_PRL) = tmp; |
|
61 |
|
62 if (piv_thres >= 0.) |
|
63 { |
|
64 piv_thres = (piv_thres > 1. ? 1. : piv_thres); |
|
65 Control (UMFPACK_SYM_PIVOT_TOLERANCE) = piv_thres; |
|
66 Control (UMFPACK_PIVOT_TOLERANCE) = piv_thres; |
|
67 } |
|
68 else |
|
69 { |
|
70 tmp = Voctave_sparse_controls.get_key ("piv_tol"); |
|
71 if (!xisnan (tmp)) |
|
72 { |
|
73 Control (UMFPACK_SYM_PIVOT_TOLERANCE) = tmp; |
|
74 Control (UMFPACK_PIVOT_TOLERANCE) = tmp; |
|
75 } |
|
76 } |
|
77 |
|
78 // Set whether we are allowed to modify Q or not |
|
79 tmp = Voctave_sparse_controls.get_key ("autoamd"); |
|
80 if (!xisnan (tmp)) |
|
81 Control (UMFPACK_FIXQ) = tmp; |
|
82 |
|
83 // Turn-off UMFPACK scaling for LU |
|
84 Control (UMFPACK_SCALE) = UMFPACK_SCALE_NONE; |
|
85 |
|
86 umfpack_di_report_control (control); |
|
87 |
5275
|
88 const octave_idx_type *Ap = a.cidx (); |
|
89 const octave_idx_type *Ai = a.ridx (); |
5164
|
90 const double *Ax = a.data (); |
|
91 |
|
92 umfpack_di_report_matrix (nr, nc, Ap, Ai, Ax, 1, control); |
|
93 |
|
94 void *Symbolic; |
|
95 Matrix Info (1, UMFPACK_INFO); |
|
96 double *info = Info.fortran_vec (); |
|
97 int status = umfpack_di_qsymbolic (nr, nc, Ap, Ai, Ax, NULL, |
|
98 &Symbolic, control, info); |
|
99 |
|
100 if (status < 0) |
|
101 { |
|
102 (*current_liboctave_error_handler) |
|
103 ("SparseLU::SparseLU symbolic factorization failed"); |
|
104 |
|
105 umfpack_di_report_status (control, status); |
|
106 umfpack_di_report_info (control, info); |
|
107 |
|
108 umfpack_di_free_symbolic (&Symbolic) ; |
|
109 } |
|
110 else |
|
111 { |
|
112 umfpack_di_report_symbolic (Symbolic, control); |
|
113 |
|
114 void *Numeric; |
|
115 status = umfpack_di_numeric (Ap, Ai, Ax, Symbolic, &Numeric, |
|
116 control, info) ; |
|
117 umfpack_di_free_symbolic (&Symbolic) ; |
|
118 |
|
119 cond = Info (UMFPACK_RCOND); |
|
120 |
|
121 if (status < 0) |
|
122 { |
|
123 (*current_liboctave_error_handler) |
|
124 ("SparseLU::SparseLU numeric factorization failed"); |
|
125 |
|
126 umfpack_di_report_status (control, status); |
|
127 umfpack_di_report_info (control, info); |
|
128 |
|
129 umfpack_di_free_numeric (&Numeric); |
|
130 } |
|
131 else |
|
132 { |
|
133 umfpack_di_report_numeric (Numeric, control); |
|
134 |
|
135 int lnz, unz, ignore1, ignore2, ignore3; |
|
136 status = umfpack_di_get_lunz (&lnz, &unz, &ignore1, &ignore2, |
|
137 &ignore3, Numeric) ; |
|
138 |
|
139 if (status < 0) |
|
140 { |
|
141 (*current_liboctave_error_handler) |
|
142 ("SparseLU::SparseLU extracting LU factors failed"); |
|
143 |
|
144 umfpack_di_report_status (control, status); |
|
145 umfpack_di_report_info (control, info); |
|
146 |
|
147 umfpack_di_free_numeric (&Numeric); |
|
148 } |
|
149 else |
|
150 { |
|
151 int n_inner = (nr < nc ? nr : nc); |
|
152 |
|
153 if (lnz < 1) |
5275
|
154 Lfact = SparseMatrix (static_cast<octave_idx_type> (n_inner), nr, |
|
155 static_cast<octave_idx_type> (1)); |
5164
|
156 else |
5275
|
157 Lfact = SparseMatrix (static_cast<octave_idx_type> (n_inner), nr, |
|
158 static_cast<octave_idx_type> (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) |
5275
|
165 Ufact = SparseMatrix (static_cast<octave_idx_type> (n_inner), nc, |
|
166 static_cast<octave_idx_type> (1)); |
5164
|
167 else |
5275
|
168 Ufact = SparseMatrix (static_cast<octave_idx_type> (n_inner), nc, |
|
169 static_cast<octave_idx_type> (unz)); |
5164
|
170 |
5275
|
171 octave_idx_type *Up = Ufact.cidx (); |
|
172 octave_idx_type *Uj = Ufact.ridx (); |
5164
|
173 double *Ux = Ufact.data (); |
|
174 |
|
175 P.resize (nr); |
|
176 int *p = P.fortran_vec (); |
|
177 |
|
178 Q.resize (nc); |
|
179 int *q = Q.fortran_vec (); |
|
180 |
|
181 int do_recip; |
|
182 status = umfpack_di_get_numeric (Ltp, Ltj, Ltx, Up, Uj, |
|
183 Ux, p, q, (double *) NULL, |
|
184 &do_recip, (double *) NULL, |
|
185 Numeric) ; |
|
186 |
|
187 umfpack_di_free_numeric (&Numeric) ; |
|
188 |
|
189 if (status < 0 || do_recip) |
|
190 { |
|
191 (*current_liboctave_error_handler) |
|
192 ("SparseLU::SparseLU extracting LU factors failed"); |
|
193 |
|
194 umfpack_di_report_status (control, status); |
|
195 } |
|
196 else |
|
197 { |
|
198 Lfact = Lfact.transpose (); |
|
199 |
|
200 umfpack_di_report_matrix (nr, n_inner, Lfact.cidx (), |
|
201 Lfact.ridx (), Lfact.data (), |
|
202 1, control); |
|
203 umfpack_di_report_matrix (n_inner, nc, Ufact.cidx (), |
|
204 Ufact.ridx (), Ufact.data (), |
|
205 1, control); |
|
206 umfpack_di_report_perm (nr, p, control); |
|
207 umfpack_di_report_perm (nc, q, control); |
|
208 } |
|
209 |
|
210 umfpack_di_report_info (control, info); |
|
211 } |
|
212 } |
|
213 } |
5203
|
214 #else |
|
215 (*current_liboctave_error_handler) ("UMFPACK not installed"); |
|
216 #endif |
5164
|
217 } |
|
218 |
|
219 SparseLU::SparseLU (const SparseMatrix& a, const ColumnVector& Qinit, |
5282
|
220 double piv_thres, bool FixedQ, double droptol, |
|
221 bool milu, bool udiag) |
5164
|
222 { |
5203
|
223 #ifdef HAVE_UMFPACK |
5282
|
224 if (milu) |
|
225 (*current_liboctave_error_handler) |
|
226 ("Modified incomplete LU not implemented"); |
5164
|
227 else |
|
228 { |
5282
|
229 octave_idx_type nr = a.rows (); |
|
230 octave_idx_type nc = a.cols (); |
5164
|
231 |
5282
|
232 // Setup the control parameters |
|
233 Matrix Control (UMFPACK_CONTROL, 1); |
|
234 double *control = Control.fortran_vec (); |
|
235 umfpack_di_defaults (control); |
5164
|
236 |
5282
|
237 double tmp = Voctave_sparse_controls.get_key ("spumoni"); |
|
238 if (!xisnan (tmp)) |
|
239 Control (UMFPACK_PRL) = tmp; |
|
240 if (piv_thres >= 0.) |
|
241 { |
|
242 piv_thres = (piv_thres > 1. ? 1. : piv_thres); |
|
243 Control (UMFPACK_SYM_PIVOT_TOLERANCE) = piv_thres; |
|
244 Control (UMFPACK_PIVOT_TOLERANCE) = piv_thres; |
|
245 } |
|
246 else |
|
247 { |
|
248 tmp = Voctave_sparse_controls.get_key ("piv_tol"); |
|
249 if (!xisnan (tmp)) |
|
250 { |
|
251 Control (UMFPACK_SYM_PIVOT_TOLERANCE) = tmp; |
|
252 Control (UMFPACK_PIVOT_TOLERANCE) = tmp; |
|
253 } |
|
254 } |
5164
|
255 |
5282
|
256 if (droptol >= 0.) |
|
257 Control (UMFPACK_DROPTOL) = droptol; |
5164
|
258 |
|
259 |
5282
|
260 // Set whether we are allowed to modify Q or not |
|
261 if (FixedQ) |
|
262 Control (UMFPACK_FIXQ) = 1.0; |
|
263 else |
|
264 { |
|
265 tmp = Voctave_sparse_controls.get_key ("autoamd"); |
|
266 if (!xisnan (tmp)) |
|
267 Control (UMFPACK_FIXQ) = tmp; |
|
268 } |
5164
|
269 |
5282
|
270 // Turn-off UMFPACK scaling for LU |
|
271 Control (UMFPACK_SCALE) = UMFPACK_SCALE_NONE; |
5164
|
272 |
5282
|
273 umfpack_di_report_control (control); |
5164
|
274 |
5282
|
275 const octave_idx_type *Ap = a.cidx (); |
|
276 const octave_idx_type *Ai = a.ridx (); |
|
277 const double *Ax = a.data (); |
|
278 |
|
279 umfpack_di_report_matrix (nr, nc, Ap, Ai, Ax, 1, control); |
|
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 { |
|
288 OCTAVE_LOCAL_BUFFER (int, qinit, nc); |
5164
|
289 |
5282
|
290 for (int i = 0; i < nc; i++) |
|
291 qinit [i] = static_cast<int> (Qinit (i)); |
5164
|
292 |
5282
|
293 status = umfpack_di_qsymbolic (nr, nc, Ap, Ai, Ax, qinit, |
|
294 &Symbolic, control, info); |
|
295 } while (0); |
5164
|
296 |
|
297 if (status < 0) |
|
298 { |
|
299 (*current_liboctave_error_handler) |
5282
|
300 ("SparseLU::SparseLU symbolic factorization failed"); |
5164
|
301 |
|
302 umfpack_di_report_status (control, status); |
|
303 umfpack_di_report_info (control, info); |
|
304 |
5282
|
305 umfpack_di_free_symbolic (&Symbolic) ; |
5164
|
306 } |
|
307 else |
|
308 { |
5282
|
309 umfpack_di_report_symbolic (Symbolic, control); |
5164
|
310 |
5282
|
311 void *Numeric; |
|
312 status = umfpack_di_numeric (Ap, Ai, Ax, Symbolic, &Numeric, |
|
313 control, info) ; |
|
314 umfpack_di_free_symbolic (&Symbolic) ; |
|
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 |
|
323 umfpack_di_report_status (control, status); |
|
324 umfpack_di_report_info (control, info); |
|
325 |
|
326 umfpack_di_free_numeric (&Numeric); |
|
327 } |
|
328 else |
|
329 { |
5282
|
330 umfpack_di_report_numeric (Numeric, control); |
5164
|
331 |
5282
|
332 int lnz, unz, ignore1, ignore2, ignore3; |
|
333 status = umfpack_di_get_lunz (&lnz, &unz, &ignore1, &ignore2, |
|
334 &ignore3, Numeric) ; |
|
335 |
|
336 if (status < 0) |
5164
|
337 { |
|
338 (*current_liboctave_error_handler) |
|
339 ("SparseLU::SparseLU extracting LU factors failed"); |
|
340 |
|
341 umfpack_di_report_status (control, status); |
5282
|
342 umfpack_di_report_info (control, info); |
|
343 |
|
344 umfpack_di_free_numeric (&Numeric); |
5164
|
345 } |
|
346 else |
|
347 { |
5282
|
348 int n_inner = (nr < nc ? nr : nc); |
|
349 |
|
350 if (lnz < 1) |
|
351 Lfact = SparseMatrix |
|
352 (static_cast<octave_idx_type> (n_inner), nr, |
|
353 static_cast<octave_idx_type> (1)); |
|
354 else |
|
355 Lfact = SparseMatrix |
|
356 (static_cast<octave_idx_type> (n_inner), nr, |
|
357 static_cast<octave_idx_type> (lnz)); |
|
358 |
|
359 octave_idx_type *Ltp = Lfact.cidx (); |
|
360 octave_idx_type *Ltj = Lfact.ridx (); |
|
361 double *Ltx = Lfact.data (); |
|
362 |
|
363 if (unz < 1) |
|
364 Ufact = SparseMatrix |
|
365 (static_cast<octave_idx_type> (n_inner), nc, |
|
366 static_cast<octave_idx_type> (1)); |
|
367 else |
|
368 Ufact = SparseMatrix |
|
369 (static_cast<octave_idx_type> (n_inner), nc, |
|
370 static_cast<octave_idx_type> (unz)); |
|
371 |
|
372 octave_idx_type *Up = Ufact.cidx (); |
|
373 octave_idx_type *Uj = Ufact.ridx (); |
|
374 double *Ux = Ufact.data (); |
|
375 |
|
376 P.resize (nr); |
|
377 int *p = P.fortran_vec (); |
|
378 |
|
379 Q.resize (nc); |
|
380 int *q = Q.fortran_vec (); |
|
381 |
|
382 int do_recip; |
|
383 status = umfpack_di_get_numeric (Ltp, Ltj, Ltx, Up, Uj, |
|
384 Ux, p, q, |
|
385 (double *) NULL, |
|
386 &do_recip, |
|
387 (double *) NULL, |
|
388 Numeric) ; |
|
389 |
|
390 umfpack_di_free_numeric (&Numeric) ; |
|
391 |
|
392 if (status < 0 || do_recip) |
|
393 { |
|
394 (*current_liboctave_error_handler) |
|
395 ("SparseLU::SparseLU extracting LU factors failed"); |
|
396 |
|
397 umfpack_di_report_status (control, status); |
|
398 } |
|
399 else |
|
400 { |
|
401 Lfact = Lfact.transpose (); |
|
402 umfpack_di_report_matrix (nr, n_inner, |
|
403 Lfact.cidx (), |
|
404 Lfact.ridx (), |
|
405 Lfact.data (), |
|
406 1, control); |
|
407 umfpack_di_report_matrix (n_inner, nc, |
|
408 Ufact.cidx (), |
|
409 Ufact.ridx (), |
|
410 Ufact.data (), |
|
411 1, control); |
|
412 umfpack_di_report_perm (nr, p, control); |
|
413 umfpack_di_report_perm (nc, q, control); |
|
414 } |
|
415 |
|
416 umfpack_di_report_info (control, info); |
5164
|
417 } |
|
418 } |
|
419 } |
5282
|
420 |
|
421 if (udiag) |
|
422 (*current_liboctave_error_handler) |
|
423 ("Option udiag of incomplete LU not implemented"); |
5164
|
424 } |
5203
|
425 #else |
|
426 (*current_liboctave_error_handler) ("UMFPACK not installed"); |
|
427 #endif |
5164
|
428 } |
|
429 |
|
430 /* |
|
431 ;;; Local Variables: *** |
|
432 ;;; mode: C++ *** |
|
433 ;;; End: *** |
|
434 */ |
|
435 |