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: #undef __FUNCT__  
184: int MatDestroy_SeqAIJ_LUSOL(Mat A)
185: {
186:      Mat_SeqAIJ_LUSOL *lusol;
187:      int             ierr;

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

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

205:      PetscFree(lusol->indc);
206:      PetscFree(lusol);

208:      MatDestroy_SeqAIJ(A);
209:      return(0);
210: }

212: #undef __FUNCT__  
214: int MatSolve_SeqAIJ_LUSOL(Mat A,Vec b,Vec x)
215: {
216:      Mat_SeqAIJ_LUSOL *lusol = (Mat_SeqAIJ_LUSOL *)A->spptr;
217:      double *bb, *xx;
218:      int mode = 5;
219:      int i, m, n, nnz, status, ierr;

222:      VecGetArray(x, &xx);
223:      VecGetArray(b, &bb);

225:      m = n = lusol->n;
226:      nnz = lusol->nnz;

228:      for (i = 0; i < m; i++)
229:      {
230:           lusol->mnsv[i] = bb[i];
231:      }

233:      LU6SOL(&mode, &m, &n, lusol->mnsv, xx, &nnz,
234:             lusol->luparm, lusol->parmlu, lusol->data,
235:             lusol->indc, lusol->indr, lusol->ip, lusol->iq,
236:             lusol->lenc, lusol->lenr, lusol->locc, lusol->locr, &status);

238:      if (status != 0)
239:      {
240:           SETERRQ(PETSC_ERR_ARG_SIZ,"solve failed");
241:      }

243:      VecRestoreArray(x, &xx);
244:      VecRestoreArray(b, &bb);
245:      return(0);
246: }

248: #undef __FUNCT__  
250: int MatLUFactorNumeric_SeqAIJ_LUSOL(Mat A, Mat *F)
251: {
252:      Mat_SeqAIJ       *a;
253:      Mat_SeqAIJ_LUSOL *lusol = (Mat_SeqAIJ_LUSOL *)(*F)->spptr;
254:      int              m, n, nz, nnz, status;
255:      int              i, rs, re,ierr;
256:      int              factorizations;

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

262:      if (m != lusol->n) {
263:        SETERRQ(PETSC_ERR_ARG_SIZ,"factorization struct inconsistent");
264:      }

266:      factorizations = 0;
267:      do
268:      {
269:           /*******************************************************************/
270:           /* Check the workspace allocation.                                 */
271:           /*******************************************************************/

273:           nz = a->nz;
274:           nnz = PetscMax(lusol->nnz, (int)(lusol->elbowroom*nz));
275:           nnz = PetscMax(nnz, 5*n);

277:           if (nnz < lusol->luparm[12]){
278:                nnz = (int)(lusol->luroom * lusol->luparm[12]);
279:           } else if ((factorizations > 0) && (lusol->luroom < 6)){
280:                lusol->luroom += 0.1;
281:           }

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

285:           if (nnz > lusol->nnz){
286:                PetscFree(lusol->indc);
287:                ierr        = PetscMalloc((sizeof(double)+2*sizeof(int))*nnz,&lusol->indc);
288:                lusol->indr = lusol->indc + nnz;
289:                lusol->data = (double *)(lusol->indr + nnz);
290:                lusol->nnz  = nnz;
291:           }

293:           /*******************************************************************/
294:           /* Fill in the data for the problem.                               */
295:           /*******************************************************************/

297:           nz = 0;
298:           if (a->indexshift)
299:           {
300:                for (i = 0; i < n; i++)
301:                {
302:                     rs = a->i[i] - 1;
303:                     re = a->i[i+1] - 1;

305:                     while (rs < re)
306:                     {
307:                              if (a->a[rs] != 0.0)
308:                          {
309:                              lusol->indc[nz] = i + 1;
310:                              lusol->indr[nz] = a->j[rs];
311:                              lusol->data[nz] = a->a[rs];
312:                              nz++;
313:                          }
314:                          rs++;
315:                     }
316:                }
317:           } else
318:           {
319:                for (i = 0; i < n; i++)
320:                {
321:                     rs = a->i[i];
322:                     re = a->i[i+1];

324:                     while (rs < re)
325:                     {
326:                              if (a->a[rs] != 0.0)
327:                          {
328:                              lusol->indc[nz] = i + 1;
329:                              lusol->indr[nz] = a->j[rs] + 1;
330:                              lusol->data[nz] = a->a[rs];
331:                              nz++;
332:                          }
333:                          rs++;
334:                     }
335:                }
336:           }

338:           /*******************************************************************/
339:           /* Do the factorization.                                           */
340:           /*******************************************************************/

342:           LU1FAC(&m, &n, &nz, &nnz,
343:                  lusol->luparm, lusol->parmlu, lusol->data,
344:                  lusol->indc, lusol->indr, lusol->ip, lusol->iq,
345:                  lusol->lenc, lusol->lenr, lusol->locc, lusol->locr,
346:                  lusol->iploc, lusol->iqloc, lusol->ipinv,
347:                  lusol->iqinv, lusol->mnsw, &status);
348: 
349:           switch(status)
350:           {
351:           case 0:                /* factored */
352:                break;

354:           case 7:                /* insufficient memory */
355:                break;

357:           case 1:
358:           case -1:                /* singular */
359:                SETERRQ(1,"Singular matrix");

361:           case 3:
362:           case 4:                /* error conditions */
363:                SETERRQ(1,"matrix error");

365:           default:                /* unknown condition */
366:                SETERRQ(1,"matrix unknown return code");
367:           }

369:           factorizations++;
370:      } while (status == 7);
371:      return(0);
372: }

374: #undef __FUNCT__  
376: int MatLUFactorSymbolic_SeqAIJ_LUSOL(Mat A, IS r, IS c,MatLUInfo *info, Mat *F)
377: {
378:      /************************************************************************/
379:      /* Input                                                                */
380:      /*     A  - matrix to factor                                            */
381:      /*     r  - row permutation (ignored)                                   */
382:      /*     c  - column permutation (ignored)                                */
383:      /*                                                                      */
384:      /* Output                                                               */
385:      /*     F  - matrix storing the factorization;                           */
386:      /************************************************************************/

388:      Mat_SeqAIJ_LUSOL *lusol;
389:      int              ierr,i, m, n, nz, nnz;

392: 
393:      /************************************************************************/
394:      /* Check the arguments.                                                 */
395:      /************************************************************************/

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

400:      /************************************************************************/
401:      /* Create the factorization.                                            */
402:      /************************************************************************/

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

406:      (*F)->ops->destroy = MatDestroy_SeqAIJ_LUSOL;
407:      (*F)->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJ_LUSOL;
408:      (*F)->ops->solve = MatSolve_SeqAIJ_LUSOL;
409:      (*F)->factor = FACTOR_LU;

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

414:      /************************************************************************/
415:      /* Initialize parameters                                                */
416:      /************************************************************************/

418:      for (i = 0; i < 30; i++)
419:      {
420:           lusol->luparm[i] = 0;
421:           lusol->parmlu[i] = 0;
422:      }

424:      lusol->luparm[1] = -1;
425:      lusol->luparm[2] = 5;
426:      lusol->luparm[7] = 1;

428:      lusol->parmlu[0] = 1 / Factorization_Tolerance;
429:      lusol->parmlu[1] = 1 / Factorization_Tolerance;
430:      lusol->parmlu[2] = Factorization_Small_Tolerance;
431:      lusol->parmlu[3] = Factorization_Pivot_Tolerance;
432:      lusol->parmlu[4] = Factorization_Pivot_Tolerance;
433:      lusol->parmlu[5] = 3.0;
434:      lusol->parmlu[6] = 0.3;
435:      lusol->parmlu[7] = 0.6;

437:      /************************************************************************/
438:      /* Allocate the workspace needed by LUSOL.                              */
439:      /************************************************************************/

441:      lusol->elbowroom = PetscMax(lusol->elbowroom, info->fill);
442:      nnz = PetscMax((int)(lusol->elbowroom*nz), 5*n);
443: 
444:      lusol->n = n;
445:      lusol->nz = nz;
446:      lusol->nnz = nnz;
447:      lusol->luroom = 1.75;

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

462:      ierr        = PetscMalloc((sizeof(double)+2*sizeof(int))*nnz,&lusol->indc);
463:      lusol->indr = lusol->indc + nnz;
464:      lusol->data = (double *)(lusol->indr + nnz);
465:      return(0);
466: }
467: EXTERN_C_END

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

492: #else

494: #undef __FUNCT__  
496: int MatUseLUSOL_SeqAIJ(Mat A)
497: {
499:      return(0);
500: }

502: #endif