1
1
/* ========================================================================== */
2
/* === kludemo ============================================================== */
2
/* === KLU DEMO ============================================================= */
3
3
/* ========================================================================== */
5
/* Demo program for KLU. Reads in a column-form 0-based matrix in tmp/Ap,
6
* tmp/Ai, and tmp/Ax, whose size and # of nonzeros are in the file tmp/Asize.
7
* Then calls KLU to analyze, factor, and solve the system.
13
* The right-hand-side can be provided in the optional tmp/b file. The solution
14
* is written to tmp/x.
5
/* Read in a Matrix Market matrix (using CHOLMOD) and solve a linear system. */
22
#include "klu_cholmod.h"
24
#define ABS(x) ((x) >= 0 ? (x) : -(x))
11
/* for handling complex matrices */
12
#define REAL(X,i) (X [2*(i)])
13
#define IMAG(X,i) (X [2*(i)+1])
14
#define CABS(X,i) (sqrt (REAL (X,i) * REAL (X,i) + IMAG (X,i) * IMAG (X,i)))
25
16
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
26
#define XTRUE(i,n) (1.0 + ((double) i) / ((double) n))
27
#define ISNAN(x) ((x) != (x))
43
#define CPUTIME (((double) clock ( )) / CLOCKS_PER_SEC)
45
/* -------------------------------------------------------------------------- */
46
/* err: compute the relative error, ||x-xtrue||/||xtrue|| */
47
/* -------------------------------------------------------------------------- */
56
double enorm, e, abse, absxtrue, xnorm ;
60
for (i = 0 ; i < n ; i++)
67
e = x [i] - XTRUE (i,n) ;
69
enorm = MAX (enorm, abse) ;
72
for (i = 0 ; i < n ; i++)
74
/* XTRUE is positive, but do this in case XTRUE is redefined */
75
absxtrue = ABS (XTRUE (i,n)) ;
76
xnorm = MAX (xnorm, absxtrue) ;
83
return (enorm / xnorm) ;
87
/* -------------------------------------------------------------------------- */
88
/* resid: compute the relative residual, ||Ax-b||/||b|| or ||A'x-b||/||b|| */
89
/* -------------------------------------------------------------------------- */
104
double rnorm, absr, absb, bnorm ;
105
for (i = 0 ; i < n ; i++)
112
for (j = 0 ; j < n ; j++)
114
for (p = Ap [j] ; p < Ap [j+1] ; p++)
117
r [j] += Ax [p] * x [i] ;
123
for (j = 0 ; j < n ; j++)
125
for (p = Ap [j] ; p < Ap [j+1] ; p++)
128
r [i] += Ax [p] * x [j] ;
133
for (i = 0 ; i < n ; i++)
139
for (i = 0 ; i < n ; i++)
147
rnorm = MAX (rnorm, absr) ;
149
for (i = 0 ; i < n ; i++)
157
bnorm = MAX (bnorm, absb) ;
163
return (rnorm / bnorm) ;
167
/* -------------------------------------------------------------------------- */
168
/* Atimesx: compute y = A*x or A'*x, where x (i) = 1 + i/n */
169
/* -------------------------------------------------------------------------- */
182
for (i = 0 ; i < n ; i++)
188
for (j = 0 ; j < n ; j++)
190
for (p = Ap [j] ; p < Ap [j+1] ; p++)
193
y [j] += Ax [p] * XTRUE (i,n) ;
199
for (j = 0 ; j < n ; j++)
201
for (p = Ap [j] ; p < Ap [j+1] ; p++)
204
y [i] += Ax [p] * XTRUE (j,n) ;
210
/* -------------------------------------------------------------------------- */
212
/* -------------------------------------------------------------------------- */
18
/* ========================================================================== */
19
/* === klu_backslash ======================================================== */
20
/* ========================================================================== */
22
static int klu_backslash /* return 1 if successful, 0 otherwise */
25
int n, /* A is n-by-n */
26
int *Ap, /* size n+1, column pointers */
27
int *Ai, /* size nz = Ap [n], row indices */
28
double *Ax, /* size nz, numerical values */
29
int isreal, /* nonzero if A is real, 0 otherwise */
30
double *B, /* size n, right-hand-side */
33
double *X, /* size n, solution to Ax=b */
34
double *R, /* size n, residual r = b-A*x */
36
/* --- scalar output --- */
37
int *lunz, /* nnz (L+U+F) */
38
double *rnorm, /* norm (b-A*x,1) / norm (A,1) */
42
klu_common *Common /* default parameters and statistics */
45
double anorm = 0, asum ;
46
klu_symbolic *Symbolic ;
47
klu_numeric *Numeric ;
50
if (!Ap || !Ai || !Ax || !B || !X || !B) return (0) ;
52
/* ---------------------------------------------------------------------- */
53
/* symbolic ordering and analysis */
54
/* ---------------------------------------------------------------------- */
56
Symbolic = klu_analyze (n, Ap, Ai, Common) ;
57
if (!Symbolic) return (0) ;
62
/* ------------------------------------------------------------------ */
64
/* ------------------------------------------------------------------ */
66
Numeric = klu_factor (Ap, Ai, Ax, Symbolic, Common) ;
69
klu_free_symbolic (&Symbolic, Common) ;
73
/* ------------------------------------------------------------------ */
74
/* statistics (not required to solve Ax=b) */
75
/* ------------------------------------------------------------------ */
77
klu_rgrowth (Ap, Ai, Ax, Symbolic, Numeric, Common) ;
78
klu_condest (Ap, Ax, Symbolic, Numeric, Common) ;
79
klu_rcond (Symbolic, Numeric, Common) ;
80
klu_flops (Symbolic, Numeric, Common) ;
81
*lunz = Numeric->lnz + Numeric->unz - n +
82
((Numeric->Offp) ? (Numeric->Offp [n]) : 0) ;
84
/* ------------------------------------------------------------------ */
86
/* ------------------------------------------------------------------ */
88
for (i = 0 ; i < n ; i++)
92
klu_solve (Symbolic, Numeric, n, 1, X, Common) ;
94
/* ------------------------------------------------------------------ */
95
/* compute residual, rnorm = norm(b-Ax,1) / norm(A,1) */
96
/* ------------------------------------------------------------------ */
98
for (i = 0 ; i < n ; i++)
102
for (j = 0 ; j < n ; j++)
105
for (p = Ap [j] ; p < Ap [j+1] ; p++)
107
/* R (i) -= A (i,j) * X (j) */
108
R [Ai [p]] -= Ax [p] * X [j] ;
109
asum += fabs (Ax [p]) ;
111
anorm = MAX (anorm, asum) ;
114
for (i = 0 ; i < n ; i++)
116
*rnorm = MAX (*rnorm, fabs (R [i])) ;
119
/* ------------------------------------------------------------------ */
120
/* free numeric factorization */
121
/* ------------------------------------------------------------------ */
123
klu_free_numeric (&Numeric, Common) ;
129
/* ------------------------------------------------------------------ */
130
/* statistics (not required to solve Ax=b) */
131
/* ------------------------------------------------------------------ */
133
Numeric = klu_z_factor (Ap, Ai, Ax, Symbolic, Common) ;
136
klu_free_symbolic (&Symbolic, Common) ;
140
/* ------------------------------------------------------------------ */
142
/* ------------------------------------------------------------------ */
144
klu_z_rgrowth (Ap, Ai, Ax, Symbolic, Numeric, Common) ;
145
klu_z_condest (Ap, Ax, Symbolic, Numeric, Common) ;
146
klu_z_rcond (Symbolic, Numeric, Common) ;
147
klu_z_flops (Symbolic, Numeric, Common) ;
148
*lunz = Numeric->lnz + Numeric->unz - n +
149
((Numeric->Offp) ? (Numeric->Offp [n]) : 0) ;
151
/* ------------------------------------------------------------------ */
153
/* ------------------------------------------------------------------ */
155
for (i = 0 ; i < 2*n ; i++)
159
klu_z_solve (Symbolic, Numeric, n, 1, X, Common) ;
161
/* ------------------------------------------------------------------ */
162
/* compute residual, rnorm = norm(b-Ax,1) / norm(A,1) */
163
/* ------------------------------------------------------------------ */
165
for (i = 0 ; i < 2*n ; i++)
169
for (j = 0 ; j < n ; j++)
172
for (p = Ap [j] ; p < Ap [j+1] ; p++)
174
/* R (i) -= A (i,j) * X (j) */
176
REAL (R,i) -= REAL(Ax,p) * REAL(X,j) - IMAG(Ax,p) * IMAG(X,j) ;
177
IMAG (R,i) -= IMAG(Ax,p) * REAL(X,j) + REAL(Ax,p) * IMAG(X,j) ;
178
asum += CABS (Ax, p) ;
180
anorm = MAX (anorm, asum) ;
183
for (i = 0 ; i < n ; i++)
185
*rnorm = MAX (*rnorm, CABS (R, i)) ;
188
/* ------------------------------------------------------------------ */
189
/* free numeric factorization */
190
/* ------------------------------------------------------------------ */
192
klu_z_free_numeric (&Numeric, Common) ;
195
/* ---------------------------------------------------------------------- */
196
/* free symbolic analysis, and residual */
197
/* ---------------------------------------------------------------------- */
199
klu_free_symbolic (&Symbolic, Common) ;
204
/* ========================================================================== */
205
/* === klu_demo ============================================================= */
206
/* ========================================================================== */
208
/* Given a sparse matrix A, set up a right-hand-side and solve X = A\b */
210
static void klu_demo (int n, int *Ap, int *Ai, double *Ax, int isreal)
217
printf ("KLU: %s, version: %d.%d.%d\n", KLU_DATE, KLU_MAIN_VERSION,
218
KLU_SUB_VERSION, KLU_SUBSUB_VERSION) ;
220
/* ---------------------------------------------------------------------- */
222
/* ---------------------------------------------------------------------- */
224
klu_defaults (&Common) ;
226
/* ---------------------------------------------------------------------- */
227
/* create a right-hand-side */
228
/* ---------------------------------------------------------------------- */
232
/* B = 1 + (1:n)/n */
233
B = klu_malloc (n, sizeof (double), &Common) ;
234
X = klu_malloc (n, sizeof (double), &Common) ;
235
R = klu_malloc (n, sizeof (double), &Common) ;
238
for (i = 0 ; i < n ; i++)
240
B [i] = 1 + ((double) i+1) / ((double) n) ;
246
/* real (B) = 1 + (1:n)/n, imag(B) = (n:-1:1)/n */
247
B = klu_malloc (n, 2 * sizeof (double), &Common) ;
248
X = klu_malloc (n, 2 * sizeof (double), &Common) ;
249
R = klu_malloc (n, 2 * sizeof (double), &Common) ;
252
for (i = 0 ; i < n ; i++)
254
REAL (B, i) = 1 + ((double) i+1) / ((double) n) ;
255
IMAG (B, i) = ((double) n-i) / ((double) n) ;
260
/* ---------------------------------------------------------------------- */
261
/* X = A\b using KLU and print statistics */
262
/* ---------------------------------------------------------------------- */
264
if (!klu_backslash (n, Ap, Ai, Ax, isreal, B, X, R, &lunz, &rnorm, &Common))
266
printf ("KLU failed\n") ;
270
printf ("n %d nnz(A) %d nnz(L+U+F) %d resid %g\n"
271
"recip growth %g condest %g rcond %g flops %g\n",
272
n, Ap [n], lunz, rnorm, Common.rgrowth, Common.condest,
273
Common.rcond, Common.flops) ;
276
/* ---------------------------------------------------------------------- */
277
/* free the problem */
278
/* ---------------------------------------------------------------------- */
282
klu_free (B, n, sizeof (double), &Common) ;
283
klu_free (X, n, sizeof (double), &Common) ;
284
klu_free (R, n, sizeof (double), &Common) ;
288
klu_free (B, 2*n, sizeof (double), &Common) ;
289
klu_free (X, 2*n, sizeof (double), &Common) ;
290
klu_free (R, 2*n, sizeof (double), &Common) ;
292
printf ("peak memory usage: %g bytes\n\n", (double) (Common.mempeak)) ;
296
/* ========================================================================== */
297
/* === main ================================================================= */
298
/* ========================================================================== */
300
/* Read in a sparse matrix in Matrix Market format using CHOLMOD, and then
301
* solve Ax=b with KLU. Note that CHOLMOD is only used to read the matrix. */
216
double *Ax, *b, *x, *r, *Y ;
217
double stats [2], condest, growth, t, c, rcond ;
218
/* atime [2], ftime [2], rtime [2], stime [2], stime2 [2], */
219
int p, i, j, n, nz, *Ap, *Ai, nrow, ncol, rhs, trial, s, k, nrhs, do_sort,
220
ldim, ordering, do_btf, scale, rescale, lnz, unz, halt_if_singular ;
221
klu_symbolic *Symbolic ;
222
klu_numeric *Numeric ;
223
klu_common Kommon, *km ;
226
printf ("\nKLU stand-alone demo:\n") ;
230
/* ---------------------------------------------------------------------- */
232
/* ---------------------------------------------------------------------- */
234
printf ("File: tmp/Asize\n") ;
235
f = fopen ("tmp/Asize", "r") ;
238
printf ("Unable to open file\n") ;
241
fscanf (f, "%d %d %d\n", &nrow, &ncol, &nz) ;
243
n = MAX (1, MAX (nrow, ncol)) ;
248
/* ---------------------------------------------------------------------- */
249
/* allocate space for the matrix A, and for the vectors b, r, and x */
250
/* ---------------------------------------------------------------------- */
252
Ap = (int *) malloc ((n+1) * sizeof (int)) ;
253
Ai = (int *) malloc (nz * sizeof (int)) ;
254
Ax = (double *) malloc (nz * sizeof (double)) ;
255
b = (double *) malloc (ldim * NRHS * sizeof (double)) ;
256
r = (double *) malloc (n * sizeof (double)) ;
257
x = (double *) malloc (ldim * NRHS * sizeof (double)) ;
259
if (!Ap || !Ai || !Ax || !b || !r || !x)
261
printf ("out of memory for input matrix\n") ;
265
/* ---------------------------------------------------------------------- */
266
/* get the matrix (tmp/Ap, tmp/Ai, and tmp/Ax) */
267
/* ---------------------------------------------------------------------- */
269
printf ("File: tmp/Ap\n") ;
270
f = fopen ("tmp/Ap", "r") ;
273
printf ("Unable to open file\n") ;
276
for (j = 0 ; j <= n ; j++)
278
fscanf (f, "%d\n", &Ap [j]) ;
282
printf ("File: tmp/Ai\n") ;
283
f = fopen ("tmp/Ai", "r") ;
286
printf ("Unable to open file\n") ;
289
for (p = 0 ; p < nz ; p++)
291
fscanf (f, "%d\n", &Ai [p]) ;
295
printf ("File: tmp/Ax\n") ;
296
f = fopen ("tmp/Ax", "r") ;
299
printf ("Unable to open file\n") ;
302
for (p = 0 ; p < nz ; p++)
304
fscanf (f, "%lg\n", &Ax [p]) ;
308
printf ("n %d nrow %d ncol %d nz %d\n", n, nrow, ncol, nz) ;
310
/* ---------------------------------------------------------------------- */
311
/* b = A * xtrue, or read b from a file */
312
/* ---------------------------------------------------------------------- */
314
for (i = 0 ; i < ldim * NRHS ; i++) b [i] = 0 ;
317
f = fopen ("tmp/b", "r") ;
318
if (f != (FILE *) NULL)
320
printf ("Reading tmp/b\n") ;
322
for (i = 0 ; i < n ; i++)
324
fscanf (f, "%lg\n", &b [i]) ;
330
Atimesx (n, Ap, Ai, Ax, b, FALSE) ;
333
/* create b (:,2:NRHS) */
335
for (s = 1 ; s < NRHS ; s++)
337
for (k = 0 ; k < n ; k++)
339
Y [k] = s + ((double) n) / 1000 ;
362
halt_if_singular = TRUE ;
366
for (do_btf = 0 ; do_btf <= 1 ; do_btf++)
368
for (scale = -1 ; scale <= 2 ; scale++)
370
for (halt_if_singular = TRUE ; halt_if_singular >= FALSE ; halt_if_singular--)
374
for (ordering = 0 ; ordering <= 3 ; ordering++)
376
if (ordering == 2 || ordering == 1) continue ;
379
for (do_sort = FALSE ; do_sort <= TRUE ; do_sort++)
384
km->ordering = ordering ; /* 0: amd, 1: colamd, 2: given, 3: user */
385
km->btf = do_btf ; /* 1: BTF , 0: no BTF */
386
km->scale = scale ; /* -1: none (and no error check)
387
0: none, 1: row, 2: max */
388
km->user_order = klu_cholmod ;
389
km->halt_if_singular = halt_if_singular ;
391
printf ("\nKLUDEMO: ordering: %d BTF: %d scale: %d halt_if_singular: %d\n",
392
ordering, do_btf, scale, halt_if_singular) ;
394
/* ---------------------------------------------------------------------- */
395
/* symbolic factorization */
396
/* ---------------------------------------------------------------------- */
398
for (trial = 0 ; trial < NTRIAL ; trial++)
400
printf ("---------------- trial %d\n", trial) ;
404
Symbolic = klu_analyze (n, Ap, Ai, km) ;
405
stats [0] = dsecnd_ ( ) - t ;
406
stats [1] = CPUTIME - c ;
408
if (Symbolic == NULL)
410
printf ("klu_analyze failed\n") ;
414
printf ("\nKLU Symbolic analysis:\n"
415
"dimension of matrix: %d\n"
416
"nz in off diagonal blocks: %d\n"
417
"# of off diagonal blocks: %d\n"
418
"largest block dimension: %d\n"
419
"estimated nz in L: %g\n"
420
"estimated nz in U: %g\n"
421
"symbolic analysis time: %10.6f (wall) %10.6f (cpu)\n",
422
n, Symbolic->nzoff, Symbolic->nblocks,
423
Symbolic->maxblock, Symbolic->lnz, Symbolic->unz,
424
stats [0], stats [1]) ;
426
/* ---------------------------------------------------------------------- */
427
/* numeric factorization */
428
/* ---------------------------------------------------------------------- */
432
Numeric = klu_factor (Ap, Ai, Ax, Symbolic, km) ;
433
stats [0] = dsecnd_ ( ) - t ;
434
stats [1] = CPUTIME - c ;
438
printf ("klu_factor failed\n") ;
441
lnz = (Numeric != NULL) ? Numeric->lnz : 0 ;
442
unz = (Numeric != NULL) ? Numeric->unz : 0 ;
444
printf ("\nKLU Numeric factorization:\n"
445
"actual nz in L: %d\n"
446
"actual nz in U: %d\n"
447
"actual nz in L+U: %d\n"
448
"# of offdiagonal pivots: %d\n"
449
"numeric factorization time: %10.6f (wall) %10.6f (cpu)\n",
450
lnz, unz, lnz + unz - n, km->noffdiag, stats [0], stats [1]) ;
454
klu_flops (Symbolic, Numeric, km) ;
455
t = dsecnd_ ( ) - t ;
457
printf ("flop count %g time %10.6f %10.6f\n", km->flops, t,c) ;
461
klu_condest (Ap, Ax, Symbolic, Numeric, &condest, km) ;
462
t = dsecnd_ ( ) - t ;
465
printf ("condition number estimate %g time %10.6f %10.6f\n", condest,t,c) ;
469
klu_growth (Ap, Ai, Ax, Symbolic, Numeric, &growth, km) ;
470
t = dsecnd_ ( ) - t ;
473
printf ("reciprocal pivot growth: %g time %10.6f %10.6f\n", growth,t,c) ;
477
klu_rcond (Symbolic, Numeric, &rcond, km) ;
478
t = dsecnd_ ( ) - t ;
481
printf ("cheap reciprocal cond: %g time %10.6f %10.6f\n", rcond,t,c) ;
487
klu_sort (Symbolic, Numeric, km) ;
488
t = dsecnd_ ( ) - t ;
490
printf (" -------------------- sort time: %10.6f (wall) %10.6f (cpu)\n",
494
/* ---------------------------------------------------------------------- */
496
/* ---------------------------------------------------------------------- */
498
for (i = 0 ; i < n ; i++) x [i] = b [i] ;
502
klu_solve (Symbolic, Numeric, n, 1, x, km) ; /* does x = A\x */
503
stats [0] = dsecnd_ ( ) - t ;
504
stats [1] = CPUTIME - c ;
506
printf ("\nKLU solve:\n"
507
"solve time: %10.6f (wall) %10.6f (cpu)\n",
508
stats [0], stats [1]) ;
510
printf ("\nrelative maxnorm of residual, ||Ax-b||/||b||: %8.2e\n",
511
resid (n, Ap, Ai, Ax, x, r, b, FALSE)) ;
514
printf ("relative maxnorm of error, ||x-xtrue||/||xtrue||: %8.2e\n\n",
520
printf ("Writing solution to file: tmp/x\n") ;
521
f = fopen ("tmp/x", "w") ;
524
printf ("Unable to open file\n") ;
527
for (i = 0 ; i < n ; i++)
529
fprintf (f, "%24.16e\n", x [i]) ;
534
/* ---------------------------------------------------------------------- */
536
/* ---------------------------------------------------------------------- */
538
for (i = 0 ; i < n ; i++) x [i] = b [i] ;
542
klu_tsolve (Symbolic, Numeric, n, 1, x, km) ;
543
stats [0] = dsecnd_ ( ) - t ;
544
stats [1] = CPUTIME - c ;
546
printf ("\nKLU tsolve:\n"
547
"solve time: %10.6f (wall) %10.6f (cpu)\n",
548
stats [0], stats [1]) ;
550
printf ("\nrelative maxnorm of residual, ||A'x-b||/||b||: %8.2e\n",
551
resid (n, Ap, Ai, Ax, x, r, b, TRUE)) ;
553
/* ---------------------------------------------------------------------- */
555
/* ---------------------------------------------------------------------- */
559
for (i = 0 ; i < ldim*NRHS ; i++) x [i] = b [i] ;
563
klu_solve (Symbolic, Numeric, ldim, NRHS, x, km) ; /* does x = A\x */
564
stats [0] = dsecnd_ ( ) - t ;
565
stats [1] = CPUTIME - c ;
567
printf ("\nKLU solve (nrhs = %d:\n"
568
"solve time: %10.6f (wall) %10.6f (cpu)\n",
569
NRHS, stats [0], stats [1]) ;
571
for (s = 0 ; s < NRHS ; s++)
573
printf ("relative maxnorm of residual, ||Ax-b||/||b||: %8.2e\n",
574
resid (n, Ap, Ai, Ax, x + s*ldim, r, b + s*ldim, FALSE)) ;
577
for (i = 0 ; i < ldim*NRHS ; i++) x [i] = b [i] ;
579
for (s = 0 ; s < NRHS ; s++)
581
klu_solve (Symbolic, Numeric, n, 1, x+s*ldim, km) ; /* does x = A\x */
583
for (s = 0 ; s < NRHS ; s++)
585
printf ("relative maxnorm of residual, ||Ax-b||/||b||: %8.2e\n",
586
resid (n, Ap, Ai, Ax, x + s*ldim, r, b + s*ldim, FALSE)) ;
589
for (nrhs = 1 ; nrhs <= NRHS ; nrhs++)
591
for (i = 0 ; i < ldim*NRHS ; i++) x [i] = b [i] ;
593
klu_solve (Symbolic, Numeric, ldim, nrhs, x, km) ; /* does x = A\x */
595
printf ("\nKLU solve (nrhs = %2d : ", nrhs) ;
597
for (s = 0 ; s < nrhs ; s++)
599
printf ("relative maxnorm of residual, ||Ax-b||/||b||: %8.2e\n",
600
resid (n, Ap, Ai, Ax, x + s*ldim, r, b + s*ldim, FALSE)) ;
604
/* ---------------------------------------------------------------------- */
606
/* ---------------------------------------------------------------------- */
608
for (i = 0 ; i < ldim*NRHS ; i++) x [i] = b [i] ;
612
klu_tsolve (Symbolic, Numeric, ldim, NRHS, x, km) ; /* does x = A'\x */
613
stats [0] = dsecnd_ ( ) - t ;
614
stats [1] = CPUTIME - c ;
616
printf ("\nKLU tsolve (nrhs = %d:\n"
617
"solve time: %10.6f (wall) %10.6f (cpu)\n",
618
NRHS, stats [0], stats [1]) ;
620
for (s = 0 ; s < NRHS ; s++)
622
printf ("relative maxnorm of residual, ||A'x-b||/||b||: %8.2e\n",
623
resid (n, Ap, Ai, Ax, x + s*ldim, r, b + s*ldim, TRUE)) ;
626
for (i = 0 ; i < ldim*NRHS ; i++) x [i] = b [i] ;
628
for (s = 0 ; s < NRHS ; s++)
630
klu_tsolve (Symbolic, Numeric, n, 1, x+s*ldim, 0, km) ;
632
for (s = 0 ; s < NRHS ; s++)
634
printf ("relative maxnorm of residual, ||A'x-b||/||b||: %8.2e\n",
635
resid (n, Ap, Ai, Ax, x + s*ldim, r, b + s*ldim, TRUE)) ;
638
for (nrhs = 1 ; nrhs <= NRHS ; nrhs++)
640
for (i = 0 ; i < ldim*NRHS ; i++) x [i] = b [i] ;
642
klu_tsolve (Symbolic, Numeric, ldim, nrhs, x, 0, km) ;
644
printf ("\nKLU tsolve (nrhs = %2d : ", nrhs) ;
646
for (s = 0 ; s < nrhs ; s++)
648
printf ("relative maxnorm of residual, ||A'x-b||/||b||: %8.2e\n",
649
resid (n, Ap, Ai, Ax, x + s*ldim, r, b + s*ldim, TRUE)) ;
655
/* ---------------------------------------------------------------------- */
656
/* numeric refactorization, no pivot (just to test the code) */
657
/* ---------------------------------------------------------------------- */
659
for (rescale = -1 ; rescale <= 2 ; rescale++)
661
km->scale = rescale ;
665
if (!klu_refactor (Ap, Ai, Ax, Symbolic, Numeric, km))
667
printf ("klu_refactor failed\n") ;
669
stats [0] = dsecnd_ ( ) - t ;
670
stats [1] = CPUTIME - c ;
672
printf ("\nKLU Numeric refactorization: scale %d\n"
673
"numeric refactorization time: %10.6f (wall) %10.6f (cpu)\n",
674
km->scale, stats [0], stats [1]) ;
676
klu_condest (Ap, Ax, Symbolic, Numeric, &condest, km) ;
677
printf ("condition number estimate %g\n", condest) ;
678
klu_growth (Ap, Ai, Ax, Symbolic, Numeric, &growth, km) ;
679
printf ("reciprocal pivot growth: %g\n", growth) ;
680
klu_rcond (Symbolic, Numeric, &rcond, km) ;
681
printf ("cheap reciprocal cond: %g\n", rcond) ;
683
/* ---------------------------------------------------------------------- */
684
/* solve Ax=b again */
685
/* ---------------------------------------------------------------------- */
687
for (i = 0 ; i < n ; i++) x [i] = b [i] ;
691
klu_solve (Symbolic, Numeric, n, 1, x, km) ;
692
stats [0] = dsecnd_ ( ) - t ;
693
stats [1] = CPUTIME - c ;
695
printf ("\nKLU solve:\n"
696
"solve time: %10.6f (wall) %10.6f (cpu)\n",
697
stats [0], stats [1]) ;
699
printf ("\nrelative maxnorm of residual, ||Ax-b||/||b||: %8.2e\n",
700
resid (n, Ap, Ai, Ax, x, r, b, FALSE)) ;
703
printf ("relative maxnorm of error, ||x-xtrue||/||xtrue||: %8.2e\n\n",
708
/* restore scale parameter for next trial */
711
/* ---------------------------------------------------------------------- */
712
/* free the Symbolic and Numeric factorization, and all workspace */
713
/* ---------------------------------------------------------------------- */
715
klu_free_symbolic (&Symbolic, km) ;
716
klu_free_numeric (&Numeric, km) ;
720
printf ("analysis: %12.4f %12.4f\n", atime[0] / NTRIAL, atime [1] / NTRIAL) ;
721
printf ("factor: %12.4f %12.4f\n", ftime[0] / NTRIAL, ftime [1] / NTRIAL) ;
722
printf ("refactor: %12.4f %12.4f\n", rtime[0] / NTRIAL, rtime [1] / NTRIAL) ;
723
printf ("solve: %12.4f %12.4f\n", stime[0] / (4*NTRIAL), stime [1] / (4*NTRIAL)) ;
724
printf ("solve2: %12.4f %12.4f (per rhs)\n", stime2[0] / (NTRIAL*NRHS), stime2 [1] / (NTRIAL*NRHS)) ;
743
printf ("kludemo done\n") ;
309
cholmod_start (&ch) ;
310
A = cholmod_read_sparse (stdin, &ch) ;
313
if (A->nrow != A->ncol || A->stype != 0
314
|| (!(A->xtype == CHOLMOD_REAL || A->xtype == CHOLMOD_COMPLEX)))
316
printf ("invalid matrix\n") ;
320
klu_demo (A->nrow, A->p, A->i, A->x, A->xtype == CHOLMOD_REAL) ;
322
cholmod_free_sparse (&A, &ch) ;
324
cholmod_finish (&ch) ;