Actual source code: lusol.c

  1: /*$Id: lusol.c,v 1.11 2001/08/06 21:15:14 bsmith Exp $*/
  2: /* 
  3:         Provides an interface to the LUSOL package of ....

  5: */
 6:  #include src/mat/impls/aij/seq/aij.h

  8: EXTERN int MatDestroy_SeqAIJ(Mat);

 10: #if defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
 11: #define LU1FAC   lu1fac_
 12: #define LU6SOL   lu6sol_
 13: #define M1PAGE   m1page_
 14: #define M5SETX   m5setx_
 15: #define M6RDEL   m6rdel_
 16: #elif !defined(PETSC_HAVE_FORTRAN_CAPS)
 17: #define LU1FAC   lu1fac
 18: #define LU6SOL   lu6sol
 19: #define M1PAGE   m1page
 20: #define M5SETX   m5setx
 21: #define M6RDEL   m6rdel
 22: #endif

 24: EXTERN_C_BEGIN
 25: /*
 26:     Dummy symbols that the MINOS files mi25bfac.f and mi15blas.f may require
 27: */
 28: void PETSC_STDCALL M1PAGE() {
 29:   ;
 30: }
 31: void PETSC_STDCALL M5SETX() {
 32:   ;
 33: }

 35: void PETSC_STDCALL M6RDEL() {
 36:   ;
 37: }
 38: EXTERN_C_END

 40: #if defined(PETSC_HAVE_LUSOL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)

 42: EXTERN_C_BEGIN
 43: extern void PETSC_STDCALL LU1FAC (int *m, int *n, int *nnz, int *size, int *luparm,
 44:                         double *parmlu, double *data, int *indc, int *indr,
 45:                         int *rowperm, int *colperm, int *collen, int *rowlen,
 46:                         int *colstart, int *rowstart, int *rploc, int *cploc,
 47:                         int *rpinv, int *cpinv, double *w, int *inform);

 49: extern void PETSC_STDCALL LU6SOL (int *mode, int *m, int *n, double *rhs, double *x,
 50:                         int *size, int *luparm, double *parmlu, double *data,
 51:                         int *indc, int *indr, int *rowperm, int *colperm,
 52:                         int *collen, int *rowlen, int *colstart, int *rowstart,
 53:                         int *inform);

 55: typedef struct
 56: {
 57:      double *data;
 58:      int *indc;
 59:      int *indr;

 61:      int *ip;
 62:      int *iq;
 63:      int *lenc;
 64:      int *lenr;
 65:      int *locc;
 66:      int *locr;
 67:      int *iploc;
 68:      int *iqloc;
 69:      int *ipinv;
 70:      int *iqinv;
 71:      double *mnsw;
 72:      double *mnsv;

 74:      double elbowroom;
 75:      double luroom;                /* Extra space allocated when factor fails   */
 76:      double parmlu[30];                /* Input/output to LUSOL                     */

 78:      int n;                        /* Number of rows/columns in matrix          */
 79:      int nz;                        /* Number of nonzeros                        */
 80:      int nnz;                        /* Number of nonzeros allocated for factors  */
 81:      int luparm[30];                /* Input/output to LUSOL                     */

 83: } Mat_SeqAIJ_LUSOL;

 85: /*  LUSOL input/Output Parameters (Description uses C-style indexes
 86:  *
 87:  *  Input parameters                                        Typical value
 88:  *
 89:  *  luparm(0) = nout     File number for printed messages.         6
 90:  *  luparm(1) = lprint   Print level.                              0
 91:  *                    < 0 suppresses output.
 92:  *                    = 0 gives error messages.
 93:  *                    = 1 gives debug output from some of the
 94:  *                        other routines in LUSOL.
 95:  *                   >= 2 gives the pivot row and column and the
 96:  *                        no. of rows and columns involved at
 97:  *                        each elimination step in lu1fac.
 98:  *  luparm(2) = maxcol   lu1fac: maximum number of columns         5
 99:  *                        searched allowed in a Markowitz-type
100:  *                        search for the next pivot element.
101:  *                        For some of the factorization, the
102:  *                        number of rows searched is
103:  *                        maxrow = maxcol - 1.
104:  *
105:  *
106:  *  Output parameters
107:  *
108:  *  luparm(9) = inform   Return code from last call to any LU routine.
109:  *  luparm(10) = nsing    No. of singularities marked in the
110:  *                        output array w(*).
111:  *  luparm(11) = jsing    Column index of last singularity.
112:  *  luparm(12) = minlen   Minimum recommended value for  lena.
113:  *  luparm(13) = maxlen   ?
114:  *  luparm(14) = nupdat   No. of updates performed by the lu8 routines.
115:  *  luparm(15) = nrank    No. of nonempty rows of U.
116:  *  luparm(16) = ndens1   No. of columns remaining when the density of
117:  *                        the matrix being factorized reached dens1.
118:  *  luparm(17) = ndens2   No. of columns remaining when the density of
119:  *                        the matrix being factorized reached dens2.
120:  *  luparm(18) = jumin    The column index associated with dumin.
121:  *  luparm(19) = numl0    No. of columns in initial  L.
122:  *  luparm(20) = lenl0    Size of initial  L  (no. of nonzeros).
123:  *  luparm(21) = lenu0    Size of initial  U.
124:  *  luparm(22) = lenl     Size of current  L.
125:  *  luparm(23) = lenu     Size of current  U.
126:  *  luparm(24) = lrow     Length of row file.
127:  *  luparm(25) = ncp      No. of compressions of LU data structures.
128:  *  luparm(26) = mersum   lu1fac: sum of Markowitz merit counts.
129:  *  luparm(27) = nutri    lu1fac: triangular rows in U.
130:  *  luparm(28) = nltri    lu1fac: triangular rows in L.
131:  *  luparm(29) =
132:  *
133:  *
134:  *  Input parameters                                        Typical value
135:  *
136:  *  parmlu(0) = elmax1   Max multiplier allowed in  L           10.0
137:  *                        during factor.
138:  *  parmlu(1) = elmax2   Max multiplier allowed in  L           10.0
139:  *                        during updates.
140:  *  parmlu(2) = small    Absolute tolerance for             eps**0.8
141:  *                        treating reals as zero.     IBM double: 3.0d-13
142:  *  parmlu(3) = utol1    Absolute tol for flagging          eps**0.66667
143:  *                        small diagonals of U.       IBM double: 3.7d-11
144:  *  parmlu(4) = utol2    Relative tol for flagging          eps**0.66667
145:  *                        small diagonals of U.       IBM double: 3.7d-11
146:  *  parmlu(5) = uspace   Factor limiting waste space in  U.      3.0
147:  *                        In lu1fac, the row or column lists
148:  *                        are compressed if their length
149:  *                        exceeds uspace times the length of
150:  *                        either file after the last compression.
151:  *  parmlu(6) = dens1    The density at which the Markowitz      0.3
152:  *                        strategy should search maxcol columns
153:  *                        and no rows.
154:  *  parmlu(7) = dens2    the density at which the Markowitz      0.6
155:  *                        strategy should search only 1 column
156:  *                        or (preferably) use a dense LU for
157:  *                        all the remaining rows and columns.
158:  *
159:  *
160:  *  Output parameters
161:  *
162:  *  parmlu(9) = amax     Maximum element in  A.
163:  *  parmlu(10) = elmax    Maximum multiplier in current  L.
164:  *  parmlu(11) = umax     Maximum element in current  U.
165:  *  parmlu(12) = dumax    Maximum diagonal in  U.
166:  *  parmlu(13) = dumin    Minimum diagonal in  U.
167:  *  parmlu(14) =
168:  *  parmlu(15) =
169:  *  parmlu(16) =
170:  *  parmlu(17) =
171:  *  parmlu(18) =
172:  *  parmlu(19) = resid    lu6sol: residual after solve with U or U'.
173:  *  ...
174:  *  parmlu(29) =
175:  */

177: #define Factorization_Tolerance       1e-1
178: #define Factorization_Pivot_Tolerance pow(2.2204460492503131E-16, 2.0 / 3.0) 
179: #define Factorization_Small_Tolerance 1e-15 /* pow(DBL_EPSILON, 0.8) */


182: int MatDestroy_SeqAIJ_LUSOL(Mat A)
183: {
184:      Mat_SeqAIJ_LUSOL *lusol;
185:      int             ierr;

188:      lusol = (Mat_SeqAIJ_LUSOL *)A->spptr;

190:      PetscFree(lusol->ip);
191:      PetscFree(lusol->iq);
192:      PetscFree(lusol->lenc);
193:      PetscFree(lusol->lenr);
194:      PetscFree(lusol->locc);
195:      PetscFree(lusol->locr);
196:      PetscFree(lusol->iploc);
197:      PetscFree(lusol->iqloc);
198:      PetscFree(lusol->ipinv);
199:      PetscFree(lusol->iqinv);
200:      PetscFree(lusol->mnsw);
201:      PetscFree(lusol->mnsv);

203:      PetscFree(lusol->indc);
204:      PetscFree(lusol);

206:      MatDestroy_SeqAIJ(A);
207:      return(0);
208: }

210: int MatSolve_SeqAIJ_LUSOL(Mat A,Vec b,Vec x)
211: {
212:      Mat_SeqAIJ_LUSOL *lusol = (Mat_SeqAIJ_LUSOL *)A->spptr;
213:      double *bb, *xx;
214:      int mode = 5;
215:      int i, m, n, nnz, status, ierr;

218:      VecGetArray(x, &xx);
219:      VecGetArray(b, &bb);

221:      m = n = lusol->n;
222:      nnz = lusol->nnz;

224:      for (i = 0; i < m; i++)
225:      {
226:           lusol->mnsv[i] = bb[i];
227:      }

229:      LU6SOL(&mode, &m, &n, lusol->mnsv, xx, &nnz,
230:             lusol->luparm, lusol->parmlu, lusol->data,
231:             lusol->indc, lusol->indr, lusol->ip, lusol->iq,
232:             lusol->lenc, lusol->lenr, lusol->locc, lusol->locr, &status);

234:      if (status != 0)
235:      {
236:           SETERRQ(PETSC_ERR_ARG_SIZ,"solve failed");
237:      }

239:      VecRestoreArray(x, &xx);
240:      VecRestoreArray(b, &bb);
241:      return(0);
242: }

244: int MatLUFactorNumeric_SeqAIJ_LUSOL(Mat A, Mat *F)
245: {
246:      Mat_SeqAIJ       *a;
247:      Mat_SeqAIJ_LUSOL *lusol = (Mat_SeqAIJ_LUSOL *)(*F)->spptr;
248:      int              m, n, nz, nnz, status;
249:      int              i, rs, re,ierr;
250:      int              factorizations;

253:      MatGetSize(A,&m,&n);
254:      a = (Mat_SeqAIJ *)A->data;

256:      if (m != lusol->n) {
257:        SETERRQ(PETSC_ERR_ARG_SIZ,"factorization struct inconsistent");
258:      }

260:      factorizations = 0;
261:      do
262:      {
263:           /*******************************************************************/
264:           /* Check the workspace allocation.                                 */
265:           /*******************************************************************/

267:           nz = a->nz;
268:           nnz = PetscMax(lusol->nnz, (int)(lusol->elbowroom*nz));
269:           nnz = PetscMax(nnz, 5*n);

271:           if (nnz < lusol->luparm[12]){
272:                nnz = (int)(lusol->luroom * lusol->luparm[12]);
273:           } else if ((factorizations > 0) && (lusol->luroom < 6)){
274:                lusol->luroom += 0.1;
275:           }

277:           nnz = PetscMax(nnz, (int)(lusol->luroom*(lusol->luparm[22] + lusol->luparm[23])));

279:           if (nnz > lusol->nnz){
280:                PetscFree(lusol->indc);
281:                ierr        = PetscMalloc((sizeof(double)+2*sizeof(int))*nnz,&lusol->indc);
282:                lusol->indr = lusol->indc + nnz;
283:                lusol->data = (double *)(lusol->indr + nnz);
284:                lusol->nnz  = nnz;
285:           }

287:           /*******************************************************************/
288:           /* Fill in the data for the problem.                               */
289:           /*******************************************************************/

291:           nz = 0;
292:           if (a->indexshift)
293:           {
294:                for (i = 0; i < n; i++)
295:                {
296:                     rs = a->i[i] - 1;
297:                     re = a->i[i+1] - 1;

299:                     while (rs < re)
300:                     {
301:                              if (a->a[rs] != 0.0)
302:                          {
303:                              lusol->indc[nz] = i + 1;
304:                              lusol->indr[nz] = a->j[rs];
305:                              lusol->data[nz] = a->a[rs];
306:                              nz++;
307:                          }
308:                          rs++;
309:                     }
310:                }
311:           } else
312:           {
313:                for (i = 0; i < n; i++)
314:                {
315:                     rs = a->i[i];
316:                     re = a->i[i+1];

318:                     while (rs < re)
319:                     {
320:                              if (a->a[rs] != 0.0)
321:                          {
322:                              lusol->indc[nz] = i + 1;
323:                              lusol->indr[nz] = a->j[rs] + 1;
324:                              lusol->data[nz] = a->a[rs];
325:                              nz++;
326:                          }
327:                          rs++;
328:                     }
329:                }
330:           }

332:           /*******************************************************************/
333:           /* Do the factorization.                                           */
334:           /*******************************************************************/

336:           LU1FAC(&m, &n, &nz, &nnz,
337:                  lusol->luparm, lusol->parmlu, lusol->data,
338:                  lusol->indc, lusol->indr, lusol->ip, lusol->iq,
339:                  lusol->lenc, lusol->lenr, lusol->locc, lusol->locr,
340:                  lusol->iploc, lusol->iqloc, lusol->ipinv,
341:                  lusol->iqinv, lusol->mnsw, &status);
342: 
343:           switch(status)
344:           {
345:           case 0:                /* factored */
346:                break;

348:           case 7:                /* insufficient memory */
349:                break;

351:           case 1:
352:           case -1:                /* singular */
353:                SETERRQ(PETSC_ERR_ARG_SIZ,"singular matrix");

355:           case 3:
356:           case 4:                /* error conditions */
357:                SETERRQ(PETSC_ERR_ARG_SIZ,"matrix error");

359:           default:                /* unknown condition */
360:                SETERRQ(PETSC_ERR_ARG_SIZ,"matrix unknown return code");
361:           }

363:           factorizations++;
364:      } while (status == 7);
365:      return(0);
366: }

368: int MatLUFactorSymbolic_SeqAIJ_LUSOL(Mat A, IS r, IS c,MatLUInfo *info, Mat *F)
369: {
370:      /************************************************************************/
371:      /* Input                                                                */
372:      /*     A  - matrix to factor                                            */
373:      /*     r  - row permutation (ignored)                                   */
374:      /*     c  - column permutation (ignored)                                */
375:      /*                                                                      */
376:      /* Output                                                               */
377:      /*     F  - matrix storing the factorization;                           */
378:      /************************************************************************/

380:      Mat_SeqAIJ_LUSOL *lusol;
381:      int              ierr,i, m, n, nz, nnz;

384: 
385:      /************************************************************************/
386:      /* Check the arguments.                                                 */
387:      /************************************************************************/

389:      MatGetSize(A, &m, &n);
390:      nz = ((Mat_SeqAIJ *)A->data)->nz;

392:      /************************************************************************/
393:      /* Create the factorization.                                            */
394:      /************************************************************************/

396:      MatCreateSeqAIJ(A->comm, m, n, 0, PETSC_NULL, F);

398:      (*F)->ops->destroy = MatDestroy_SeqAIJ_LUSOL;
399:      (*F)->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJ_LUSOL;
400:      (*F)->ops->solve = MatSolve_SeqAIJ_LUSOL;
401:      (*F)->factor = FACTOR_LU;

403:      PetscNew(Mat_SeqAIJ_LUSOL,&lusol);
404:      (*F)->spptr = (void *)lusol;

406:      /************************************************************************/
407:      /* Initialize parameters                                                */
408:      /************************************************************************/

410:      for (i = 0; i < 30; i++)
411:      {
412:           lusol->luparm[i] = 0;
413:           lusol->parmlu[i] = 0;
414:      }

416:      lusol->luparm[1] = -1;
417:      lusol->luparm[2] = 5;
418:      lusol->luparm[7] = 1;

420:      lusol->parmlu[0] = 1 / Factorization_Tolerance;
421:      lusol->parmlu[1] = 1 / Factorization_Tolerance;
422:      lusol->parmlu[2] = Factorization_Small_Tolerance;
423:      lusol->parmlu[3] = Factorization_Pivot_Tolerance;
424:      lusol->parmlu[4] = Factorization_Pivot_Tolerance;
425:      lusol->parmlu[5] = 3.0;
426:      lusol->parmlu[6] = 0.3;
427:      lusol->parmlu[7] = 0.6;

429:      /************************************************************************/
430:      /* Allocate the workspace needed by LUSOL.                              */
431:      /************************************************************************/

433:      lusol->elbowroom = 2.5;        /* Assume at least some extra memory alloced */
434:      if (info)
435:      {
436:           lusol->elbowroom = PetscMax(lusol->elbowroom, info->fill);
437:      }
438:      nnz = PetscMax((int)(lusol->elbowroom*nz), 5*n);
439: 
440:      lusol->n = n;
441:      lusol->nz = nz;
442:      lusol->nnz = nnz;
443:      lusol->luroom = 1.75;

445:      PetscMalloc(sizeof(int)*n,&lusol->ip);
446:      PetscMalloc(sizeof(int)*n,&lusol->iq);
447:      PetscMalloc(sizeof(int)*n,&lusol->lenc);
448:      PetscMalloc(sizeof(int)*n,&lusol->lenr);
449:      PetscMalloc(sizeof(int)*n,&lusol->locc);
450:      PetscMalloc(sizeof(int)*n,&lusol->locr);
451:      PetscMalloc(sizeof(int)*n,&lusol->iploc);
452:      PetscMalloc(sizeof(int)*n,&lusol->iqloc);
453:      PetscMalloc(sizeof(int)*n,&lusol->ipinv);
454:      PetscMalloc(sizeof(int)*n,&lusol->iqinv);
455:      PetscMalloc(sizeof(double)*n,&lusol->mnsw);
456:      PetscMalloc(sizeof(double)*n,&lusol->mnsv);

458:      ierr        = PetscMalloc((sizeof(double)+2*sizeof(int))*nnz,&lusol->indc);
459:      lusol->indr = lusol->indc + nnz;
460:      lusol->data = (double *)(lusol->indr + nnz);
461:      return(0);
462: }
463: EXTERN_C_END

465: int MatUseLUSOL_SeqAIJ(Mat A)
466: {
467:   int        ierr, m, n;
468:   PetscTruth match;
469: 
471:   MatGetSize(A, &m, &n);
472:   if (m != n) {
473:     SETERRQ(PETSC_ERR_ARG_SIZ,"matrix must be square");
474:   }
475: 
476:   PetscTypeCompare((PetscObject)A,MATSEQAIJ,&match);
477:   if (!match) {
478:     SETERRQ(PETSC_ERR_ARG_SIZ,"matrix must be Seq_AIJ");
479:   }
480: 
481:   A->ops->lufactorsymbolic = MatLUFactorSymbolic_SeqAIJ_LUSOL;
482:   PetscLogInfo(0,"Using LUSOL for SeqAIJ LU factorization and solves.");
483:   return(0);
484: }

486: #else

488: int MatUseLUSOL_SeqAIJ(Mat A)
489: {
491:      return(0);
492: }

494: #endif