1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
|
/*
Copyright (c) 2003, The Regents of the University of California, through
Lawrence Berkeley National Laboratory (subject to receipt of any required
approvals from U.S. Dept. of Energy)
All rights reserved.
The source code is distributed under BSD license, see the file License.txt
at the top-level directory.
*/
/*
* -- SuperLU routine (version 5.0) --
* Lawrence Berkeley National Laboratory
* November, 2010
* August, 2011
*/
/*! \file
* \brief Example #2 showing how to use ILU to precondition GMRES
*
* This example shows that ILU is computed from the equilibrated matrix,
* but the preconditioned GMRES is applied to the original system.
* The driver routine DGSISX is called twice to perform factorization
* and apply preconditioner separately.
*
* Note that DGSISX performs the following factorization:
* Pr*Dr*A*Dc*Pc^T ~= LU
* with Pr being obtained from MC64 statically then partial pivoting
* dynamically. On return, A is overwritten as A1 = Dr*A*Dc.
*
* We need to save a copy of the original matrix A, then solve
* the original system, A*x = B, using FGMRES.
* Each GMRES step requires requires 2 procedures:
* 1) Apply preconditioner M^{-1} = Dc*Pc^T*U^{-1}*L^{-1}*Pr*Dr
* 2) Matrix-vector multiplication: w = A*v
*
* \ingroup Example
*/
#include "slu_ddefs.h"
char *GLOBAL_EQUED;
superlu_options_t *GLOBAL_OPTIONS;
double *GLOBAL_R, *GLOBAL_C;
int *GLOBAL_PERM_C, *GLOBAL_PERM_R;
SuperMatrix *GLOBAL_A, *GLOBAL_A_ORIG, *GLOBAL_L, *GLOBAL_U;
SuperLUStat_t *GLOBAL_STAT;
mem_usage_t *GLOBAL_MEM_USAGE;
/*!
* \brief Performs DGSISX with original matrix A.
*
* See documentation of dgsisx for more details.
*
* \param [in] n Dimension of matrices
* \param [out] x Solution
* \param [in,out] y Right-hand side
*/
void dpsolve(int n, double x[], double y[])
{
SuperMatrix *A = GLOBAL_A, *L = GLOBAL_L, *U = GLOBAL_U;
SuperLUStat_t *stat = GLOBAL_STAT;
int *perm_c = GLOBAL_PERM_C, *perm_r = GLOBAL_PERM_R;
char *equed = GLOBAL_EQUED;
double *R = GLOBAL_R, *C = GLOBAL_C;
superlu_options_t *options = GLOBAL_OPTIONS;
mem_usage_t *mem_usage = GLOBAL_MEM_USAGE;
int_t info;
static DNformat X, Y;
static SuperMatrix XX = {SLU_DN, SLU_D, SLU_GE, 1, 1, &X};
static SuperMatrix YY = {SLU_DN, SLU_D, SLU_GE, 1, 1, &Y};
double rpg, rcond;
XX.nrow = YY.nrow = n;
X.lda = Y.lda = n;
X.nzval = x;
Y.nzval = y;
#if 0
dcopy_(&n, y, &i_1, x, &i_1);
dgstrs(NOTRANS, L, U, perm_c, perm_r, &XX, stat, &info);
#else
dgsisx(options, A, perm_c, perm_r, NULL, equed, R, C,
L, U, NULL, 0, &YY, &XX, &rpg, &rcond, NULL,
mem_usage, stat, &info);
#endif
}
/*!
* \brief Performs matrix-vector multiplication sp_dgemv with original matrix A.
*
* The operations is y := alpha*A*x + beta*y. See documentation of sp_dgemv
* for further details.
*
* \param [in] alpha Scalar factor for A*x
* \param [in] x Vector to multiply with A
* \param [in] beta Scalar factor for y
* \param [in,out] y Vector to add to to matrix-vector multiplication and
* storage for result.
*/
void dmatvec_mult(double alpha, double x[], double beta, double y[])
{
SuperMatrix *A = GLOBAL_A_ORIG;
sp_dgemv("N", alpha, A, x, 1, beta, y, 1);
}
int main(int argc, char *argv[])
{
void dmatvec_mult(double alpha, double x[], double beta, double y[]);
void dpsolve(int n, double x[], double y[]);
extern int dfgmr( int n,
void (*matvec_mult)(double, double [], double, double []),
void (*psolve)(int n, double [], double[]),
double *rhs, double *sol, double tol, int restrt, int *itmax,
FILE *fits);
extern int dfill_diag(int n, NCformat *Astore);
char equed[1] = {'B'};
trans_t trans;
SuperMatrix A, AA, L, U;
SuperMatrix B, X;
NCformat *Astore;
NCformat *Ustore;
SCformat *Lstore;
GlobalLU_t Glu; /* facilitate multiple factorizations with
SamePattern_SameRowPerm */
double *a, *a_orig;
int_t *asub, *xa, *asub_orig, *xa_orig;
int *etree;
int *perm_c; /* column permutation vector */
int *perm_r; /* row permutations from partial pivoting */
int nrhs, ldx, m, n;
int_t lwork, info, nnz;
double *rhsb, *rhsx, *xact;
double *work = NULL;
double *R, *C;
double rpg, rcond;
double zero = 0.0;
mem_usage_t mem_usage;
superlu_options_t options;
SuperLUStat_t stat;
FILE *fp = stdin;
double *x, *b;
#ifdef DEBUG
extern int num_drop_L, num_drop_U;
#endif
#if ( DEBUGlevel>=1 )
CHECK_MALLOC("Enter main()");
#endif
/* Defaults */
lwork = 0;
nrhs = 1;
trans = NOTRANS;
/* Set the default input options:
options.Fact = DOFACT;
options.Equil = YES;
options.ColPerm = COLAMD;
options.DiagPivotThresh = 0.1; //different from complete LU
options.Trans = NOTRANS;
options.IterRefine = NOREFINE;
options.SymmetricMode = NO;
options.PivotGrowth = NO;
options.ConditionNumber = NO;
options.PrintStat = YES;
options.RowPerm = LargeDiag_MC64;
options.ILU_DropTol = 1e-4;
options.ILU_FillTol = 1e-2;
options.ILU_FillFactor = 10.0;
options.ILU_DropRule = DROP_BASIC | DROP_AREA;
options.ILU_Norm = INF_NORM;
options.ILU_MILU = SILU;
*/
ilu_set_default_options(&options);
/* Modify the defaults. */
options.PivotGrowth = YES; /* Compute reciprocal pivot growth */
options.ConditionNumber = YES;/* Compute reciprocal condition number */
if ( lwork > 0 ) {
work = SUPERLU_MALLOC(lwork);
if ( !work ) ABORT("Malloc fails for work[].");
}
/* Read matrix A from a file in Harwell-Boeing format.*/
if (argc < 2)
{
printf("Usage:\n%s [OPTION] < [INPUT] > [OUTPUT]\nOPTION:\n"
"-h -hb:\n\t[INPUT] is a Harwell-Boeing format matrix.\n"
"-r -rb:\n\t[INPUT] is a Rutherford-Boeing format matrix.\n"
"-t -triplet:\n\t[INPUT] is a triplet format matrix.\n",
argv[0]);
return EXIT_FAILURE;
}
else
{
switch (argv[1][1])
{
case 'H':
case 'h':
printf("Input a Harwell-Boeing format matrix:\n");
dreadhb(fp, &m, &n, &nnz, &a, &asub, &xa);
break;
case 'R':
case 'r':
printf("Input a Rutherford-Boeing format matrix:\n");
dreadrb(&m, &n, &nnz, &a, &asub, &xa);
break;
case 'T':
case 't':
printf("Input a triplet format matrix:\n");
dreadtriple(&m, &n, &nnz, &a, &asub, &xa);
break;
default:
printf("Unrecognized format.\n");
return EXIT_FAILURE;
}
}
dCreate_CompCol_Matrix(&A, m, n, nnz, a, asub, xa,
SLU_NC, SLU_D, SLU_GE);
Astore = A.Store;
dfill_diag(n, Astore);
printf("Dimension %dx%d; # nonzeros %d\n", (int)A.nrow, (int)A.ncol, (int)Astore->nnz);
fflush(stdout);
/* Make a copy of the original matrix. */
nnz = Astore->nnz;
a_orig = doubleMalloc(nnz);
asub_orig = intMalloc(nnz);
xa_orig = intMalloc(n+1);
for (int i = 0; i < nnz; ++i) {
a_orig[i] = ((double *)Astore->nzval)[i];
asub_orig[i] = Astore->rowind[i];
}
for (int i = 0; i <= n; ++i) xa_orig[i] = Astore->colptr[i];
dCreate_CompCol_Matrix(&AA, m, n, nnz, a_orig, asub_orig, xa_orig,
SLU_NC, SLU_D, SLU_GE);
/* Generate the right-hand side */
if ( !(rhsb = doubleMalloc(m * nrhs)) ) ABORT("Malloc fails for rhsb[].");
if ( !(rhsx = doubleMalloc(m * nrhs)) ) ABORT("Malloc fails for rhsx[].");
dCreate_Dense_Matrix(&B, m, nrhs, rhsb, m, SLU_DN, SLU_D, SLU_GE);
dCreate_Dense_Matrix(&X, m, nrhs, rhsx, m, SLU_DN, SLU_D, SLU_GE);
xact = doubleMalloc(n * nrhs);
ldx = n;
dGenXtrue(n, nrhs, xact, ldx);
dFillRHS(trans, nrhs, xact, ldx, &A, &B);
if ( !(etree = int32Malloc(n)) ) ABORT("Malloc fails for etree[].");
if ( !(perm_r = int32Malloc(m)) ) ABORT("Malloc fails for perm_r[].");
if ( !(perm_c = int32Malloc(n)) ) ABORT("Malloc fails for perm_c[].");
if ( !(R = (double *) SUPERLU_MALLOC(A.nrow * sizeof(double))) )
ABORT("SUPERLU_MALLOC fails for R[].");
if ( !(C = (double *) SUPERLU_MALLOC(A.ncol * sizeof(double))) )
ABORT("SUPERLU_MALLOC fails for C[].");
info = 0;
#ifdef DEBUG
num_drop_L = 0;
num_drop_U = 0;
#endif
/* Initialize the statistics variables. */
StatInit(&stat);
/* Compute the incomplete factorization and compute the condition number
and pivot growth using dgsisx. */
B.ncol = 0; /* not to perform triangular solution */
dgsisx(&options, &A, perm_c, perm_r, etree, equed, R, C, &L, &U, work,
lwork, &B, &X, &rpg, &rcond, &Glu, &mem_usage, &stat, &info);
/* Set RHS for GMRES. */
if (!(b = doubleMalloc(m))) ABORT("Malloc fails for b[].");
for (int i = 0; i < m; i++) b[i] = rhsb[i];
printf("dgsisx(): info %lld, equed %c\n", (long long)info, equed[0]);
if (info > 0 || rcond < 1e-8 || rpg > 1e8)
printf("WARNING: This preconditioner might be unstable.\n");
if ( info == 0 || info == n+1 ) {
if ( options.PivotGrowth == YES )
printf("Recip. pivot growth = %e\n", rpg);
if ( options.ConditionNumber == YES )
printf("Recip. condition number = %e\n", rcond);
} else if ( info > 0 && lwork == -1 ) {
printf("** Estimated memory: %lld bytes\n", (long long)info - n);
}
Lstore = (SCformat *) L.Store;
Ustore = (NCformat *) U.Store;
printf("n(A) = %d, nnz(A) = %lld\n", n, (long long)Astore->nnz);
printf("No of nonzeros in factor L = %lld\n", (long long)Lstore->nnz);
printf("No of nonzeros in factor U = %lld\n", (long long)Ustore->nnz);
printf("No of nonzeros in L+U = %lld\n", (long long)Lstore->nnz + Ustore->nnz - n);
printf("Fill ratio: nnz(F)/nnz(A) = %.1f\n",
((double)(Lstore->nnz) + (double)(Ustore->nnz) - (double)n)
/ (double)Astore->nnz);
printf("L\\U MB %.3f\ttotal MB needed %.3f\n",
mem_usage.for_lu/1e6, mem_usage.total_needed/1e6);
fflush(stdout);
/* Set the global variables. */
GLOBAL_A = &A;
GLOBAL_A_ORIG = &AA;
GLOBAL_L = &L;
GLOBAL_U = &U;
GLOBAL_STAT = &stat;
GLOBAL_PERM_C = perm_c;
GLOBAL_PERM_R = perm_r;
GLOBAL_OPTIONS = &options;
GLOBAL_EQUED = equed;
GLOBAL_R = R;
GLOBAL_C = C;
GLOBAL_MEM_USAGE = &mem_usage;
/* Set the options to do solve-only. */
options.Fact = FACTORED;
options.PivotGrowth = NO;
options.ConditionNumber = NO;
/* Set the variables used by GMRES. */
int restrt = SUPERLU_MIN(n / 3 + 1, 50);
int maxit = 1000;
int iter = maxit;
double resid = 1e-8;
if (!(x = doubleMalloc(n))) ABORT("Malloc fails for x[].");
if (info <= n + 1)
{
int i_1 = 1;
double maxferr = 0.0, nrmA, nrmB, res, t;
extern double dnrm2_(int *, double [], int *);
extern void daxpy_(int *, double *, double [], int *, double [], int *);
/* Initial guess */
for (int i = 0; i < n; i++) x[i] = zero;
t = SuperLU_timer_();
/* Call GMRES */
dfgmr(n, dmatvec_mult, dpsolve, b, x, resid, restrt, &iter, stdout);
t = SuperLU_timer_() - t;
/* Output the result. */
int nnz32 = Astore->nnz;
nrmA = dnrm2_(&nnz32, (double *)((NCformat *)A.Store)->nzval,
&i_1);
nrmB = dnrm2_(&m, b, &i_1);
sp_dgemv("N", -1.0, &A, x, 1, 1.0, b, 1);
res = dnrm2_(&m, b, &i_1);
resid = res / nrmB;
printf("||A||_F = %.1e, ||B||_2 = %.1e, ||B-A*X||_2 = %.1e, "
"relres = %.1e\n", nrmA, nrmB, res, resid);
if (iter >= maxit)
{
if (resid >= 1.0) iter = -180;
else if (resid > 1e-8) iter = -111;
}
printf("iteration: %d\nresidual: %.1e\nGMRES time: %.2f seconds.\n",
iter, resid, t);
for (int i = 0; i < m; i++) {
maxferr = SUPERLU_MAX(maxferr, fabs(x[i] - xact[i]));
}
printf("||X-X_true||_oo = %.1e\n", maxferr);
}
#ifdef DEBUG
printf("%d entries in L and %d entries in U dropped.\n",
num_drop_L, num_drop_U);
#endif
fflush(stdout);
if ( options.PrintStat ) StatPrint(&stat);
StatFree(&stat);
SUPERLU_FREE (rhsb);
SUPERLU_FREE (rhsx);
SUPERLU_FREE (xact);
SUPERLU_FREE (etree);
SUPERLU_FREE (perm_r);
SUPERLU_FREE (perm_c);
SUPERLU_FREE (R);
SUPERLU_FREE (C);
Destroy_CompCol_Matrix(&A);
Destroy_CompCol_Matrix(&AA);
Destroy_SuperMatrix_Store(&B);
Destroy_SuperMatrix_Store(&X);
if ( lwork >= 0 ) {
Destroy_SuperNode_Matrix(&L);
Destroy_CompCol_Matrix(&U);
}
SUPERLU_FREE(b);
SUPERLU_FREE(x);
#if ( DEBUGlevel>=1 )
CHECK_MALLOC("Exit main()");
#endif
return EXIT_SUCCESS;
}
|