Actual source code: aijfact.c

petsc-master 2019-06-15
Report Typos and Errors

  2:  #include <../src/mat/impls/aij/seq/aij.h>
  3:  #include <../src/mat/impls/sbaij/seq/sbaij.h>
  4:  #include <petscbt.h>
  5:  #include <../src/mat/utils/freespace.h>

  7: /*
  8:       Computes an ordering to get most of the large numerical values in the lower triangular part of the matrix

 10:       This code does not work and is not called anywhere. It would be registered with MatOrderingRegisterAll()
 11: */
 12: PetscErrorCode MatGetOrdering_Flow_SeqAIJ(Mat mat,MatOrderingType type,IS *irow,IS *icol)
 13: {
 14:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)mat->data;
 15:   PetscErrorCode    ierr;
 16:   PetscInt          i,j,jj,k, kk,n = mat->rmap->n, current = 0, newcurrent = 0,*order;
 17:   const PetscInt    *ai = a->i, *aj = a->j;
 18:   const PetscScalar *aa = a->a;
 19:   PetscBool         *done;
 20:   PetscReal         best,past = 0,future;

 23:   /* pick initial row */
 24:   best = -1;
 25:   for (i=0; i<n; i++) {
 26:     future = 0.0;
 27:     for (j=ai[i]; j<ai[i+1]; j++) {
 28:       if (aj[j] != i) future += PetscAbsScalar(aa[j]);
 29:       else              past  = PetscAbsScalar(aa[j]);
 30:     }
 31:     if (!future) future = 1.e-10; /* if there is zero in the upper diagonal part want to rank this row high */
 32:     if (past/future > best) {
 33:       best    = past/future;
 34:       current = i;
 35:     }
 36:   }

 38:   PetscMalloc1(n,&done);
 39:   PetscMemzero(done,n*sizeof(PetscBool));
 40:   PetscMalloc1(n,&order);
 41:   order[0] = current;
 42:   for (i=0; i<n-1; i++) {
 43:     done[current] = PETSC_TRUE;
 44:     best          = -1;
 45:     /* loop over all neighbors of current pivot */
 46:     for (j=ai[current]; j<ai[current+1]; j++) {
 47:       jj = aj[j];
 48:       if (done[jj]) continue;
 49:       /* loop over columns of potential next row computing weights for below and above diagonal */
 50:       past = future = 0.0;
 51:       for (k=ai[jj]; k<ai[jj+1]; k++) {
 52:         kk = aj[k];
 53:         if (done[kk]) past += PetscAbsScalar(aa[k]);
 54:         else if (kk != jj) future += PetscAbsScalar(aa[k]);
 55:       }
 56:       if (!future) future = 1.e-10; /* if there is zero in the upper diagonal part want to rank this row high */
 57:       if (past/future > best) {
 58:         best       = past/future;
 59:         newcurrent = jj;
 60:       }
 61:     }
 62:     if (best == -1) { /* no neighbors to select from so select best of all that remain */
 63:       best = -1;
 64:       for (k=0; k<n; k++) {
 65:         if (done[k]) continue;
 66:         future = 0.0;
 67:         past   = 0.0;
 68:         for (j=ai[k]; j<ai[k+1]; j++) {
 69:           kk = aj[j];
 70:           if (done[kk])       past += PetscAbsScalar(aa[j]);
 71:           else if (kk != k) future += PetscAbsScalar(aa[j]);
 72:         }
 73:         if (!future) future = 1.e-10; /* if there is zero in the upper diagonal part want to rank this row high */
 74:         if (past/future > best) {
 75:           best       = past/future;
 76:           newcurrent = k;
 77:         }
 78:       }
 79:     }
 80:     if (current == newcurrent) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"newcurrent cannot be current");
 81:     current    = newcurrent;
 82:     order[i+1] = current;
 83:   }
 84:   ISCreateGeneral(PETSC_COMM_SELF,n,order,PETSC_COPY_VALUES,irow);
 85:   *icol = *irow;
 86:   PetscObjectReference((PetscObject)*irow);
 87:   PetscFree(done);
 88:   PetscFree(order);
 89:   return(0);
 90: }

 92: PETSC_INTERN PetscErrorCode MatGetFactor_seqaij_petsc(Mat A,MatFactorType ftype,Mat *B)
 93: {
 94:   PetscInt       n = A->rmap->n;

 98: #if defined(PETSC_USE_COMPLEX)
 99:   if (A->hermitian && (ftype == MAT_FACTOR_CHOLESKY || ftype == MAT_FACTOR_ICC)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Hermitian Factor is not supported");
100: #endif
101:   MatCreate(PetscObjectComm((PetscObject)A),B);
102:   MatSetSizes(*B,n,n,n,n);
103:   if (ftype == MAT_FACTOR_LU || ftype == MAT_FACTOR_ILU || ftype == MAT_FACTOR_ILUDT) {
104:     MatSetType(*B,MATSEQAIJ);

106:     (*B)->ops->ilufactorsymbolic = MatILUFactorSymbolic_SeqAIJ;
107:     (*B)->ops->lufactorsymbolic  = MatLUFactorSymbolic_SeqAIJ;

109:     MatSetBlockSizesFromMats(*B,A,A);
110:   } else if (ftype == MAT_FACTOR_CHOLESKY || ftype == MAT_FACTOR_ICC) {
111:     MatSetType(*B,MATSEQSBAIJ);
112:     MatSeqSBAIJSetPreallocation(*B,1,MAT_SKIP_ALLOCATION,NULL);

114:     (*B)->ops->iccfactorsymbolic      = MatICCFactorSymbolic_SeqAIJ;
115:     (*B)->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_SeqAIJ;
116:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Factor type not supported");
117:   (*B)->factortype = ftype;

119:   PetscFree((*B)->solvertype);
120:   PetscStrallocpy(MATSOLVERPETSC,&(*B)->solvertype);
121:   return(0);
122: }

124: PetscErrorCode MatLUFactorSymbolic_SeqAIJ_inplace(Mat B,Mat A,IS isrow,IS iscol,const MatFactorInfo *info)
125: {
126:   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data,*b;
127:   IS                 isicol;
128:   PetscErrorCode     ierr;
129:   const PetscInt     *r,*ic;
130:   PetscInt           i,n=A->rmap->n,*ai=a->i,*aj=a->j;
131:   PetscInt           *bi,*bj,*ajtmp;
132:   PetscInt           *bdiag,row,nnz,nzi,reallocs=0,nzbd,*im;
133:   PetscReal          f;
134:   PetscInt           nlnk,*lnk,k,**bi_ptr;
135:   PetscFreeSpaceList free_space=NULL,current_space=NULL;
136:   PetscBT            lnkbt;
137:   PetscBool          missing;

140:   if (A->rmap->N != A->cmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"matrix must be square");
141:   MatMissingDiagonal(A,&missing,&i);
142:   if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",i);

144:   ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);
145:   ISGetIndices(isrow,&r);
146:   ISGetIndices(isicol,&ic);

148:   /* get new row pointers */
149:   PetscMalloc1(n+1,&bi);
150:   bi[0] = 0;

152:   /* bdiag is location of diagonal in factor */
153:   PetscMalloc1(n+1,&bdiag);
154:   bdiag[0] = 0;

156:   /* linked list for storing column indices of the active row */
157:   nlnk = n + 1;
158:   PetscLLCreate(n,n,nlnk,lnk,lnkbt);

160:   PetscMalloc2(n+1,&bi_ptr,n+1,&im);

162:   /* initial FreeSpace size is f*(ai[n]+1) */
163:   f             = info->fill;
164:   PetscFreeSpaceGet(PetscRealIntMultTruncate(f,ai[n]+1),&free_space);
165:   current_space = free_space;

167:   for (i=0; i<n; i++) {
168:     /* copy previous fill into linked list */
169:     nzi = 0;
170:     nnz = ai[r[i]+1] - ai[r[i]];
171:     ajtmp = aj + ai[r[i]];
172:     PetscLLAddPerm(nnz,ajtmp,ic,n,nlnk,lnk,lnkbt);
173:     nzi  += nlnk;

175:     /* add pivot rows into linked list */
176:     row = lnk[n];
177:     while (row < i) {
178:       nzbd  = bdiag[row] - bi[row] + 1;   /* num of entries in the row with column index <= row */
179:       ajtmp = bi_ptr[row] + nzbd;   /* points to the entry next to the diagonal */
180:       PetscLLAddSortedLU(ajtmp,row,nlnk,lnk,lnkbt,i,nzbd,im);
181:       nzi  += nlnk;
182:       row   = lnk[row];
183:     }
184:     bi[i+1] = bi[i] + nzi;
185:     im[i]   = nzi;

187:     /* mark bdiag */
188:     nzbd = 0;
189:     nnz  = nzi;
190:     k    = lnk[n];
191:     while (nnz-- && k < i) {
192:       nzbd++;
193:       k = lnk[k];
194:     }
195:     bdiag[i] = bi[i] + nzbd;

197:     /* if free space is not available, make more free space */
198:     if (current_space->local_remaining<nzi) {
199:       nnz  = PetscIntMultTruncate(n - i,nzi); /* estimated and max additional space needed */
200:       PetscFreeSpaceGet(nnz,&current_space);
201:       reallocs++;
202:     }

204:     /* copy data into free space, then initialize lnk */
205:     PetscLLClean(n,n,nzi,lnk,current_space->array,lnkbt);

207:     bi_ptr[i]                       = current_space->array;
208:     current_space->array           += nzi;
209:     current_space->local_used      += nzi;
210:     current_space->local_remaining -= nzi;
211:   }
212: #if defined(PETSC_USE_INFO)
213:   if (ai[n] != 0) {
214:     PetscReal af = ((PetscReal)bi[n])/((PetscReal)ai[n]);
215:     PetscInfo3(A,"Reallocs %D Fill ratio:given %g needed %g\n",reallocs,(double)f,(double)af);
216:     PetscInfo1(A,"Run with -pc_factor_fill %g or use \n",(double)af);
217:     PetscInfo1(A,"PCFactorSetFill(pc,%g);\n",(double)af);
218:     PetscInfo(A,"for best performance.\n");
219:   } else {
220:     PetscInfo(A,"Empty matrix\n");
221:   }
222: #endif

224:   ISRestoreIndices(isrow,&r);
225:   ISRestoreIndices(isicol,&ic);

227:   /* destroy list of free space and other temporary array(s) */
228:   PetscMalloc1(bi[n]+1,&bj);
229:   PetscFreeSpaceContiguous(&free_space,bj);
230:   PetscLLDestroy(lnk,lnkbt);
231:   PetscFree2(bi_ptr,im);

233:   /* put together the new matrix */
234:   MatSeqAIJSetPreallocation_SeqAIJ(B,MAT_SKIP_ALLOCATION,NULL);
235:   PetscLogObjectParent((PetscObject)B,(PetscObject)isicol);
236:   b    = (Mat_SeqAIJ*)(B)->data;

238:   b->free_a       = PETSC_TRUE;
239:   b->free_ij      = PETSC_TRUE;
240:   b->singlemalloc = PETSC_FALSE;

242:   PetscMalloc1(bi[n]+1,&b->a);
243:   b->j    = bj;
244:   b->i    = bi;
245:   b->diag = bdiag;
246:   b->ilen = 0;
247:   b->imax = 0;
248:   b->row  = isrow;
249:   b->col  = iscol;
250:   PetscObjectReference((PetscObject)isrow);
251:   PetscObjectReference((PetscObject)iscol);
252:   b->icol = isicol;
253:   PetscMalloc1(n+1,&b->solve_work);

255:   /* In b structure:  Free imax, ilen, old a, old j.  Allocate solve_work, new a, new j */
256:   PetscLogObjectMemory((PetscObject)B,(bi[n]-n)*(sizeof(PetscInt)+sizeof(PetscScalar)));
257:   b->maxnz = b->nz = bi[n];

259:   (B)->factortype            = MAT_FACTOR_LU;
260:   (B)->info.factor_mallocs   = reallocs;
261:   (B)->info.fill_ratio_given = f;

263:   if (ai[n]) {
264:     (B)->info.fill_ratio_needed = ((PetscReal)bi[n])/((PetscReal)ai[n]);
265:   } else {
266:     (B)->info.fill_ratio_needed = 0.0;
267:   }
268:   (B)->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJ_inplace;
269:   if (a->inode.size) {
270:     (B)->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJ_Inode_inplace;
271:   }
272:   return(0);
273: }

275: PetscErrorCode MatLUFactorSymbolic_SeqAIJ(Mat B,Mat A,IS isrow,IS iscol,const MatFactorInfo *info)
276: {
277:   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data,*b;
278:   IS                 isicol;
279:   PetscErrorCode     ierr;
280:   const PetscInt     *r,*ic,*ai=a->i,*aj=a->j,*ajtmp;
281:   PetscInt           i,n=A->rmap->n;
282:   PetscInt           *bi,*bj;
283:   PetscInt           *bdiag,row,nnz,nzi,reallocs=0,nzbd,*im;
284:   PetscReal          f;
285:   PetscInt           nlnk,*lnk,k,**bi_ptr;
286:   PetscFreeSpaceList free_space=NULL,current_space=NULL;
287:   PetscBT            lnkbt;
288:   PetscBool          missing;

291:   if (A->rmap->N != A->cmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"matrix must be square");
292:   MatMissingDiagonal(A,&missing,&i);
293:   if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",i);
294: 
295:   ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);
296:   ISGetIndices(isrow,&r);
297:   ISGetIndices(isicol,&ic);

299:   /* get new row and diagonal pointers, must be allocated separately because they will be given to the Mat_SeqAIJ and freed separately */
300:   PetscMalloc1(n+1,&bi);
301:   PetscMalloc1(n+1,&bdiag);
302:   bi[0] = bdiag[0] = 0;

304:   /* linked list for storing column indices of the active row */
305:   nlnk = n + 1;
306:   PetscLLCreate(n,n,nlnk,lnk,lnkbt);

308:   PetscMalloc2(n+1,&bi_ptr,n+1,&im);

310:   /* initial FreeSpace size is f*(ai[n]+1) */
311:   f             = info->fill;
312:   PetscFreeSpaceGet(PetscRealIntMultTruncate(f,ai[n]+1),&free_space);
313:   current_space = free_space;

315:   for (i=0; i<n; i++) {
316:     /* copy previous fill into linked list */
317:     nzi = 0;
318:     nnz = ai[r[i]+1] - ai[r[i]];
319:     ajtmp = aj + ai[r[i]];
320:     PetscLLAddPerm(nnz,ajtmp,ic,n,nlnk,lnk,lnkbt);
321:     nzi  += nlnk;

323:     /* add pivot rows into linked list */
324:     row = lnk[n];
325:     while (row < i) {
326:       nzbd  = bdiag[row] + 1; /* num of entries in the row with column index <= row */
327:       ajtmp = bi_ptr[row] + nzbd; /* points to the entry next to the diagonal */
328:       PetscLLAddSortedLU(ajtmp,row,nlnk,lnk,lnkbt,i,nzbd,im);
329:       nzi  += nlnk;
330:       row   = lnk[row];
331:     }
332:     bi[i+1] = bi[i] + nzi;
333:     im[i]   = nzi;

335:     /* mark bdiag */
336:     nzbd = 0;
337:     nnz  = nzi;
338:     k    = lnk[n];
339:     while (nnz-- && k < i) {
340:       nzbd++;
341:       k = lnk[k];
342:     }
343:     bdiag[i] = nzbd; /* note: bdiag[i] = nnzL as input for PetscFreeSpaceContiguous_LU() */

345:     /* if free space is not available, make more free space */
346:     if (current_space->local_remaining<nzi) {
347:       /* estimated additional space needed */
348:       nnz  = PetscIntMultTruncate(2,PetscIntMultTruncate(n-1,nzi));
349:       PetscFreeSpaceGet(nnz,&current_space);
350:       reallocs++;
351:     }

353:     /* copy data into free space, then initialize lnk */
354:     PetscLLClean(n,n,nzi,lnk,current_space->array,lnkbt);

356:     bi_ptr[i]                       = current_space->array;
357:     current_space->array           += nzi;
358:     current_space->local_used      += nzi;
359:     current_space->local_remaining -= nzi;
360:   }

362:   ISRestoreIndices(isrow,&r);
363:   ISRestoreIndices(isicol,&ic);

365:   /*   copy free_space into bj and free free_space; set bi, bj, bdiag in new datastructure; */
366:   PetscMalloc1(bi[n]+1,&bj);
367:   PetscFreeSpaceContiguous_LU(&free_space,bj,n,bi,bdiag);
368:   PetscLLDestroy(lnk,lnkbt);
369:   PetscFree2(bi_ptr,im);

371:   /* put together the new matrix */
372:   MatSeqAIJSetPreallocation_SeqAIJ(B,MAT_SKIP_ALLOCATION,NULL);
373:   PetscLogObjectParent((PetscObject)B,(PetscObject)isicol);
374:   b    = (Mat_SeqAIJ*)(B)->data;

376:   b->free_a       = PETSC_TRUE;
377:   b->free_ij      = PETSC_TRUE;
378:   b->singlemalloc = PETSC_FALSE;

380:   PetscMalloc1(bdiag[0]+1,&b->a);

382:   b->j    = bj;
383:   b->i    = bi;
384:   b->diag = bdiag;
385:   b->ilen = 0;
386:   b->imax = 0;
387:   b->row  = isrow;
388:   b->col  = iscol;
389:   PetscObjectReference((PetscObject)isrow);
390:   PetscObjectReference((PetscObject)iscol);
391:   b->icol = isicol;
392:   PetscMalloc1(n+1,&b->solve_work);

394:   /* In b structure:  Free imax, ilen, old a, old j.  Allocate solve_work, new a, new j */
395:   PetscLogObjectMemory((PetscObject)B,(bdiag[0]+1)*(sizeof(PetscInt)+sizeof(PetscScalar)));
396:   b->maxnz = b->nz = bdiag[0]+1;

398:   B->factortype            = MAT_FACTOR_LU;
399:   B->info.factor_mallocs   = reallocs;
400:   B->info.fill_ratio_given = f;

402:   if (ai[n]) {
403:     B->info.fill_ratio_needed = ((PetscReal)(bdiag[0]+1))/((PetscReal)ai[n]);
404:   } else {
405:     B->info.fill_ratio_needed = 0.0;
406:   }
407: #if defined(PETSC_USE_INFO)
408:   if (ai[n] != 0) {
409:     PetscReal af = B->info.fill_ratio_needed;
410:     PetscInfo3(A,"Reallocs %D Fill ratio:given %g needed %g\n",reallocs,(double)f,(double)af);
411:     PetscInfo1(A,"Run with -pc_factor_fill %g or use \n",(double)af);
412:     PetscInfo1(A,"PCFactorSetFill(pc,%g);\n",(double)af);
413:     PetscInfo(A,"for best performance.\n");
414:   } else {
415:     PetscInfo(A,"Empty matrix\n");
416:   }
417: #endif
418:   B->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJ;
419:   if (a->inode.size) {
420:     B->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJ_Inode;
421:   }
422:   MatSeqAIJCheckInode_FactorLU(B);
423:   return(0);
424: }

426: /*
427:     Trouble in factorization, should we dump the original matrix?
428: */
429: PetscErrorCode MatFactorDumpMatrix(Mat A)
430: {
432:   PetscBool      flg = PETSC_FALSE;

435:   PetscOptionsGetBool(((PetscObject)A)->options,NULL,"-mat_factor_dump_on_error",&flg,NULL);
436:   if (flg) {
437:     PetscViewer viewer;
438:     char        filename[PETSC_MAX_PATH_LEN];

440:     PetscSNPrintf(filename,PETSC_MAX_PATH_LEN,"matrix_factor_error.%d",PetscGlobalRank);
441:     PetscViewerBinaryOpen(PetscObjectComm((PetscObject)A),filename,FILE_MODE_WRITE,&viewer);
442:     MatView(A,viewer);
443:     PetscViewerDestroy(&viewer);
444:   }
445:   return(0);
446: }

448: PetscErrorCode MatLUFactorNumeric_SeqAIJ(Mat B,Mat A,const MatFactorInfo *info)
449: {
450:   Mat             C     =B;
451:   Mat_SeqAIJ      *a    =(Mat_SeqAIJ*)A->data,*b=(Mat_SeqAIJ*)C->data;
452:   IS              isrow = b->row,isicol = b->icol;
453:   PetscErrorCode  ierr;
454:   const PetscInt  *r,*ic,*ics;
455:   const PetscInt  n=A->rmap->n,*ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j,*bdiag=b->diag;
456:   PetscInt        i,j,k,nz,nzL,row,*pj;
457:   const PetscInt  *ajtmp,*bjtmp;
458:   MatScalar       *rtmp,*pc,multiplier,*pv;
459:   const MatScalar *aa=a->a,*v;
460:   PetscBool       row_identity,col_identity;
461:   FactorShiftCtx  sctx;
462:   const PetscInt  *ddiag;
463:   PetscReal       rs;
464:   MatScalar       d;

467:   /* MatPivotSetUp(): initialize shift context sctx */
468:   PetscMemzero(&sctx,sizeof(FactorShiftCtx));

470:   if (info->shifttype == (PetscReal) MAT_SHIFT_POSITIVE_DEFINITE) { /* set sctx.shift_top=max{rs} */
471:     ddiag          = a->diag;
472:     sctx.shift_top = info->zeropivot;
473:     for (i=0; i<n; i++) {
474:       /* calculate sum(|aij|)-RealPart(aii), amt of shift needed for this row */
475:       d  = (aa)[ddiag[i]];
476:       rs = -PetscAbsScalar(d) - PetscRealPart(d);
477:       v  = aa+ai[i];
478:       nz = ai[i+1] - ai[i];
479:       for (j=0; j<nz; j++) rs += PetscAbsScalar(v[j]);
480:       if (rs>sctx.shift_top) sctx.shift_top = rs;
481:     }
482:     sctx.shift_top *= 1.1;
483:     sctx.nshift_max = 5;
484:     sctx.shift_lo   = 0.;
485:     sctx.shift_hi   = 1.;
486:   }

488:   ISGetIndices(isrow,&r);
489:   ISGetIndices(isicol,&ic);
490:   PetscMalloc1(n+1,&rtmp);
491:   ics  = ic;

493:   do {
494:     sctx.newshift = PETSC_FALSE;
495:     for (i=0; i<n; i++) {
496:       /* zero rtmp */
497:       /* L part */
498:       nz    = bi[i+1] - bi[i];
499:       bjtmp = bj + bi[i];
500:       for  (j=0; j<nz; j++) rtmp[bjtmp[j]] = 0.0;

502:       /* U part */
503:       nz    = bdiag[i]-bdiag[i+1];
504:       bjtmp = bj + bdiag[i+1]+1;
505:       for  (j=0; j<nz; j++) rtmp[bjtmp[j]] = 0.0;

507:       /* load in initial (unfactored row) */
508:       nz    = ai[r[i]+1] - ai[r[i]];
509:       ajtmp = aj + ai[r[i]];
510:       v     = aa + ai[r[i]];
511:       for (j=0; j<nz; j++) {
512:         rtmp[ics[ajtmp[j]]] = v[j];
513:       }
514:       /* ZeropivotApply() */
515:       rtmp[i] += sctx.shift_amount;  /* shift the diagonal of the matrix */

517:       /* elimination */
518:       bjtmp = bj + bi[i];
519:       row   = *bjtmp++;
520:       nzL   = bi[i+1] - bi[i];
521:       for (k=0; k < nzL; k++) {
522:         pc = rtmp + row;
523:         if (*pc != 0.0) {
524:           pv         = b->a + bdiag[row];
525:           multiplier = *pc * (*pv);
526:           *pc        = multiplier;

528:           pj = b->j + bdiag[row+1]+1; /* beginning of U(row,:) */
529:           pv = b->a + bdiag[row+1]+1;
530:           nz = bdiag[row]-bdiag[row+1]-1; /* num of entries in U(row,:) excluding diag */

532:           for (j=0; j<nz; j++) rtmp[pj[j]] -= multiplier * pv[j];
533:           PetscLogFlops(1+2*nz);
534:         }
535:         row = *bjtmp++;
536:       }

538:       /* finished row so stick it into b->a */
539:       rs = 0.0;
540:       /* L part */
541:       pv = b->a + bi[i];
542:       pj = b->j + bi[i];
543:       nz = bi[i+1] - bi[i];
544:       for (j=0; j<nz; j++) {
545:         pv[j] = rtmp[pj[j]]; rs += PetscAbsScalar(pv[j]);
546:       }

548:       /* U part */
549:       pv = b->a + bdiag[i+1]+1;
550:       pj = b->j + bdiag[i+1]+1;
551:       nz = bdiag[i] - bdiag[i+1]-1;
552:       for (j=0; j<nz; j++) {
553:         pv[j] = rtmp[pj[j]]; rs += PetscAbsScalar(pv[j]);
554:       }

556:       sctx.rs = rs;
557:       sctx.pv = rtmp[i];
558:       MatPivotCheck(B,A,info,&sctx,i);
559:       if (sctx.newshift) break; /* break for-loop */
560:       rtmp[i] = sctx.pv; /* sctx.pv might be updated in the case of MAT_SHIFT_INBLOCKS */

562:       /* Mark diagonal and invert diagonal for simplier triangular solves */
563:       pv  = b->a + bdiag[i];
564:       *pv = 1.0/rtmp[i];

566:     } /* endof for (i=0; i<n; i++) { */

568:     /* MatPivotRefine() */
569:     if (info->shifttype == (PetscReal)MAT_SHIFT_POSITIVE_DEFINITE && !sctx.newshift && sctx.shift_fraction>0 && sctx.nshift<sctx.nshift_max) {
570:       /*
571:        * if no shift in this attempt & shifting & started shifting & can refine,
572:        * then try lower shift
573:        */
574:       sctx.shift_hi       = sctx.shift_fraction;
575:       sctx.shift_fraction = (sctx.shift_hi+sctx.shift_lo)/2.;
576:       sctx.shift_amount   = sctx.shift_fraction * sctx.shift_top;
577:       sctx.newshift       = PETSC_TRUE;
578:       sctx.nshift++;
579:     }
580:   } while (sctx.newshift);

582:   PetscFree(rtmp);
583:   ISRestoreIndices(isicol,&ic);
584:   ISRestoreIndices(isrow,&r);

586:   ISIdentity(isrow,&row_identity);
587:   ISIdentity(isicol,&col_identity);
588:   if (b->inode.size) {
589:     C->ops->solve = MatSolve_SeqAIJ_Inode;
590:   } else if (row_identity && col_identity) {
591:     C->ops->solve = MatSolve_SeqAIJ_NaturalOrdering;
592:   } else {
593:     C->ops->solve = MatSolve_SeqAIJ;
594:   }
595:   C->ops->solveadd          = MatSolveAdd_SeqAIJ;
596:   C->ops->solvetranspose    = MatSolveTranspose_SeqAIJ;
597:   C->ops->solvetransposeadd = MatSolveTransposeAdd_SeqAIJ;
598:   C->ops->matsolve          = MatMatSolve_SeqAIJ;
599:   C->assembled              = PETSC_TRUE;
600:   C->preallocated           = PETSC_TRUE;

602:   PetscLogFlops(C->cmap->n);

604:   /* MatShiftView(A,info,&sctx) */
605:   if (sctx.nshift) {
606:     if (info->shifttype == (PetscReal)MAT_SHIFT_POSITIVE_DEFINITE) {
607:       PetscInfo4(A,"number of shift_pd tries %D, shift_amount %g, diagonal shifted up by %e fraction top_value %e\n",sctx.nshift,(double)sctx.shift_amount,(double)sctx.shift_fraction,(double)sctx.shift_top);
608:     } else if (info->shifttype == (PetscReal)MAT_SHIFT_NONZERO) {
609:       PetscInfo2(A,"number of shift_nz tries %D, shift_amount %g\n",sctx.nshift,(double)sctx.shift_amount);
610:     } else if (info->shifttype == (PetscReal)MAT_SHIFT_INBLOCKS) {
611:       PetscInfo2(A,"number of shift_inblocks applied %D, each shift_amount %g\n",sctx.nshift,(double)info->shiftamount);
612:     }
613:   }
614:   return(0);
615: }

617: PetscErrorCode MatLUFactorNumeric_SeqAIJ_inplace(Mat B,Mat A,const MatFactorInfo *info)
618: {
619:   Mat             C     =B;
620:   Mat_SeqAIJ      *a    =(Mat_SeqAIJ*)A->data,*b=(Mat_SeqAIJ*)C->data;
621:   IS              isrow = b->row,isicol = b->icol;
622:   PetscErrorCode  ierr;
623:   const PetscInt  *r,*ic,*ics;
624:   PetscInt        nz,row,i,j,n=A->rmap->n,diag;
625:   const PetscInt  *ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j;
626:   const PetscInt  *ajtmp,*bjtmp,*diag_offset = b->diag,*pj;
627:   MatScalar       *pv,*rtmp,*pc,multiplier,d;
628:   const MatScalar *v,*aa=a->a;
629:   PetscReal       rs=0.0;
630:   FactorShiftCtx  sctx;
631:   const PetscInt  *ddiag;
632:   PetscBool       row_identity, col_identity;

635:   /* MatPivotSetUp(): initialize shift context sctx */
636:   PetscMemzero(&sctx,sizeof(FactorShiftCtx));

638:   if (info->shifttype == (PetscReal) MAT_SHIFT_POSITIVE_DEFINITE) { /* set sctx.shift_top=max{rs} */
639:     ddiag          = a->diag;
640:     sctx.shift_top = info->zeropivot;
641:     for (i=0; i<n; i++) {
642:       /* calculate sum(|aij|)-RealPart(aii), amt of shift needed for this row */
643:       d  = (aa)[ddiag[i]];
644:       rs = -PetscAbsScalar(d) - PetscRealPart(d);
645:       v  = aa+ai[i];
646:       nz = ai[i+1] - ai[i];
647:       for (j=0; j<nz; j++) rs += PetscAbsScalar(v[j]);
648:       if (rs>sctx.shift_top) sctx.shift_top = rs;
649:     }
650:     sctx.shift_top *= 1.1;
651:     sctx.nshift_max = 5;
652:     sctx.shift_lo   = 0.;
653:     sctx.shift_hi   = 1.;
654:   }

656:   ISGetIndices(isrow,&r);
657:   ISGetIndices(isicol,&ic);
658:   PetscMalloc1(n+1,&rtmp);
659:   ics  = ic;

661:   do {
662:     sctx.newshift = PETSC_FALSE;
663:     for (i=0; i<n; i++) {
664:       nz    = bi[i+1] - bi[i];
665:       bjtmp = bj + bi[i];
666:       for  (j=0; j<nz; j++) rtmp[bjtmp[j]] = 0.0;

668:       /* load in initial (unfactored row) */
669:       nz    = ai[r[i]+1] - ai[r[i]];
670:       ajtmp = aj + ai[r[i]];
671:       v     = aa + ai[r[i]];
672:       for (j=0; j<nz; j++) {
673:         rtmp[ics[ajtmp[j]]] = v[j];
674:       }
675:       rtmp[ics[r[i]]] += sctx.shift_amount; /* shift the diagonal of the matrix */

677:       row = *bjtmp++;
678:       while  (row < i) {
679:         pc = rtmp + row;
680:         if (*pc != 0.0) {
681:           pv         = b->a + diag_offset[row];
682:           pj         = b->j + diag_offset[row] + 1;
683:           multiplier = *pc / *pv++;
684:           *pc        = multiplier;
685:           nz         = bi[row+1] - diag_offset[row] - 1;
686:           for (j=0; j<nz; j++) rtmp[pj[j]] -= multiplier * pv[j];
687:           PetscLogFlops(1+2*nz);
688:         }
689:         row = *bjtmp++;
690:       }
691:       /* finished row so stick it into b->a */
692:       pv   = b->a + bi[i];
693:       pj   = b->j + bi[i];
694:       nz   = bi[i+1] - bi[i];
695:       diag = diag_offset[i] - bi[i];
696:       rs   = 0.0;
697:       for (j=0; j<nz; j++) {
698:         pv[j] = rtmp[pj[j]];
699:         rs   += PetscAbsScalar(pv[j]);
700:       }
701:       rs -= PetscAbsScalar(pv[diag]);

703:       sctx.rs = rs;
704:       sctx.pv = pv[diag];
705:       MatPivotCheck(B,A,info,&sctx,i);
706:       if (sctx.newshift) break;
707:       pv[diag] = sctx.pv;
708:     }

710:     if (info->shifttype == (PetscReal)MAT_SHIFT_POSITIVE_DEFINITE && !sctx.newshift && sctx.shift_fraction>0 && sctx.nshift<sctx.nshift_max) {
711:       /*
712:        * if no shift in this attempt & shifting & started shifting & can refine,
713:        * then try lower shift
714:        */
715:       sctx.shift_hi       = sctx.shift_fraction;
716:       sctx.shift_fraction = (sctx.shift_hi+sctx.shift_lo)/2.;
717:       sctx.shift_amount   = sctx.shift_fraction * sctx.shift_top;
718:       sctx.newshift       = PETSC_TRUE;
719:       sctx.nshift++;
720:     }
721:   } while (sctx.newshift);

723:   /* invert diagonal entries for simplier triangular solves */
724:   for (i=0; i<n; i++) {
725:     b->a[diag_offset[i]] = 1.0/b->a[diag_offset[i]];
726:   }
727:   PetscFree(rtmp);
728:   ISRestoreIndices(isicol,&ic);
729:   ISRestoreIndices(isrow,&r);

731:   ISIdentity(isrow,&row_identity);
732:   ISIdentity(isicol,&col_identity);
733:   if (row_identity && col_identity) {
734:     C->ops->solve = MatSolve_SeqAIJ_NaturalOrdering_inplace;
735:   } else {
736:     C->ops->solve = MatSolve_SeqAIJ_inplace;
737:   }
738:   C->ops->solveadd          = MatSolveAdd_SeqAIJ_inplace;
739:   C->ops->solvetranspose    = MatSolveTranspose_SeqAIJ_inplace;
740:   C->ops->solvetransposeadd = MatSolveTransposeAdd_SeqAIJ_inplace;
741:   C->ops->matsolve          = MatMatSolve_SeqAIJ_inplace;

743:   C->assembled    = PETSC_TRUE;
744:   C->preallocated = PETSC_TRUE;

746:   PetscLogFlops(C->cmap->n);
747:   if (sctx.nshift) {
748:     if (info->shifttype == (PetscReal)MAT_SHIFT_POSITIVE_DEFINITE) {
749:       PetscInfo4(A,"number of shift_pd tries %D, shift_amount %g, diagonal shifted up by %e fraction top_value %e\n",sctx.nshift,(double)sctx.shift_amount,(double)sctx.shift_fraction,(double)sctx.shift_top);
750:     } else if (info->shifttype == (PetscReal)MAT_SHIFT_NONZERO) {
751:       PetscInfo2(A,"number of shift_nz tries %D, shift_amount %g\n",sctx.nshift,(double)sctx.shift_amount);
752:     }
753:   }
754:   (C)->ops->solve          = MatSolve_SeqAIJ_inplace;
755:   (C)->ops->solvetranspose = MatSolveTranspose_SeqAIJ_inplace;

757:   MatSeqAIJCheckInode(C);
758:   return(0);
759: }

761: /*
762:    This routine implements inplace ILU(0) with row or/and column permutations.
763:    Input:
764:      A - original matrix
765:    Output;
766:      A - a->i (rowptr) is same as original rowptr, but factored i-the row is stored in rowperm[i]
767:          a->j (col index) is permuted by the inverse of colperm, then sorted
768:          a->a reordered accordingly with a->j
769:          a->diag (ptr to diagonal elements) is updated.
770: */
771: PetscErrorCode MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(Mat B,Mat A,const MatFactorInfo *info)
772: {
773:   Mat_SeqAIJ      *a    =(Mat_SeqAIJ*)A->data;
774:   IS              isrow = a->row,isicol = a->icol;
775:   PetscErrorCode  ierr;
776:   const PetscInt  *r,*ic,*ics;
777:   PetscInt        i,j,n=A->rmap->n,*ai=a->i,*aj=a->j;
778:   PetscInt        *ajtmp,nz,row;
779:   PetscInt        *diag = a->diag,nbdiag,*pj;
780:   PetscScalar     *rtmp,*pc,multiplier,d;
781:   MatScalar       *pv,*v;
782:   PetscReal       rs;
783:   FactorShiftCtx  sctx;
784:   const MatScalar *aa=a->a,*vtmp;

787:   if (A != B) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"input and output matrix must have same address");

789:   /* MatPivotSetUp(): initialize shift context sctx */
790:   PetscMemzero(&sctx,sizeof(FactorShiftCtx));

792:   if (info->shifttype == (PetscReal) MAT_SHIFT_POSITIVE_DEFINITE) { /* set sctx.shift_top=max{rs} */
793:     const PetscInt *ddiag = a->diag;
794:     sctx.shift_top = info->zeropivot;
795:     for (i=0; i<n; i++) {
796:       /* calculate sum(|aij|)-RealPart(aii), amt of shift needed for this row */
797:       d    = (aa)[ddiag[i]];
798:       rs   = -PetscAbsScalar(d) - PetscRealPart(d);
799:       vtmp = aa+ai[i];
800:       nz   = ai[i+1] - ai[i];
801:       for (j=0; j<nz; j++) rs += PetscAbsScalar(vtmp[j]);
802:       if (rs>sctx.shift_top) sctx.shift_top = rs;
803:     }
804:     sctx.shift_top *= 1.1;
805:     sctx.nshift_max = 5;
806:     sctx.shift_lo   = 0.;
807:     sctx.shift_hi   = 1.;
808:   }

810:   ISGetIndices(isrow,&r);
811:   ISGetIndices(isicol,&ic);
812:   PetscMalloc1(n+1,&rtmp);
813:   PetscMemzero(rtmp,(n+1)*sizeof(PetscScalar));
814:   ics  = ic;

816: #if defined(MV)
817:   sctx.shift_top      = 0.;
818:   sctx.nshift_max     = 0;
819:   sctx.shift_lo       = 0.;
820:   sctx.shift_hi       = 0.;
821:   sctx.shift_fraction = 0.;

823:   if (info->shifttype == (PetscReal)MAT_SHIFT_POSITIVE_DEFINITE) { /* set sctx.shift_top=max{rs} */
824:     sctx.shift_top = 0.;
825:     for (i=0; i<n; i++) {
826:       /* calculate sum(|aij|)-RealPart(aii), amt of shift needed for this row */
827:       d  = (a->a)[diag[i]];
828:       rs = -PetscAbsScalar(d) - PetscRealPart(d);
829:       v  = a->a+ai[i];
830:       nz = ai[i+1] - ai[i];
831:       for (j=0; j<nz; j++) rs += PetscAbsScalar(v[j]);
832:       if (rs>sctx.shift_top) sctx.shift_top = rs;
833:     }
834:     if (sctx.shift_top < info->zeropivot) sctx.shift_top = info->zeropivot;
835:     sctx.shift_top *= 1.1;
836:     sctx.nshift_max = 5;
837:     sctx.shift_lo   = 0.;
838:     sctx.shift_hi   = 1.;
839:   }

841:   sctx.shift_amount = 0.;
842:   sctx.nshift       = 0;
843: #endif

845:   do {
846:     sctx.newshift = PETSC_FALSE;
847:     for (i=0; i<n; i++) {
848:       /* load in initial unfactored row */
849:       nz    = ai[r[i]+1] - ai[r[i]];
850:       ajtmp = aj + ai[r[i]];
851:       v     = a->a + ai[r[i]];
852:       /* sort permuted ajtmp and values v accordingly */
853:       for (j=0; j<nz; j++) ajtmp[j] = ics[ajtmp[j]];
854:       PetscSortIntWithScalarArray(nz,ajtmp,v);

856:       diag[r[i]] = ai[r[i]];
857:       for (j=0; j<nz; j++) {
858:         rtmp[ajtmp[j]] = v[j];
859:         if (ajtmp[j] < i) diag[r[i]]++; /* update a->diag */
860:       }
861:       rtmp[r[i]] += sctx.shift_amount; /* shift the diagonal of the matrix */

863:       row = *ajtmp++;
864:       while  (row < i) {
865:         pc = rtmp + row;
866:         if (*pc != 0.0) {
867:           pv = a->a + diag[r[row]];
868:           pj = aj + diag[r[row]] + 1;

870:           multiplier = *pc / *pv++;
871:           *pc        = multiplier;
872:           nz         = ai[r[row]+1] - diag[r[row]] - 1;
873:           for (j=0; j<nz; j++) rtmp[pj[j]] -= multiplier * pv[j];
874:           PetscLogFlops(1+2*nz);
875:         }
876:         row = *ajtmp++;
877:       }
878:       /* finished row so overwrite it onto a->a */
879:       pv     = a->a + ai[r[i]];
880:       pj     = aj + ai[r[i]];
881:       nz     = ai[r[i]+1] - ai[r[i]];
882:       nbdiag = diag[r[i]] - ai[r[i]]; /* num of entries before the diagonal */

884:       rs = 0.0;
885:       for (j=0; j<nz; j++) {
886:         pv[j] = rtmp[pj[j]];
887:         if (j != nbdiag) rs += PetscAbsScalar(pv[j]);
888:       }

890:       sctx.rs = rs;
891:       sctx.pv = pv[nbdiag];
892:       MatPivotCheck(B,A,info,&sctx,i);
893:       if (sctx.newshift) break;
894:       pv[nbdiag] = sctx.pv;
895:     }

897:     if (info->shifttype == (PetscReal)MAT_SHIFT_POSITIVE_DEFINITE && !sctx.newshift && sctx.shift_fraction>0 && sctx.nshift<sctx.nshift_max) {
898:       /*
899:        * if no shift in this attempt & shifting & started shifting & can refine,
900:        * then try lower shift
901:        */
902:       sctx.shift_hi       = sctx.shift_fraction;
903:       sctx.shift_fraction = (sctx.shift_hi+sctx.shift_lo)/2.;
904:       sctx.shift_amount   = sctx.shift_fraction * sctx.shift_top;
905:       sctx.newshift       = PETSC_TRUE;
906:       sctx.nshift++;
907:     }
908:   } while (sctx.newshift);

910:   /* invert diagonal entries for simplier triangular solves */
911:   for (i=0; i<n; i++) {
912:     a->a[diag[r[i]]] = 1.0/a->a[diag[r[i]]];
913:   }

915:   PetscFree(rtmp);
916:   ISRestoreIndices(isicol,&ic);
917:   ISRestoreIndices(isrow,&r);

919:   A->ops->solve             = MatSolve_SeqAIJ_InplaceWithPerm;
920:   A->ops->solveadd          = MatSolveAdd_SeqAIJ_inplace;
921:   A->ops->solvetranspose    = MatSolveTranspose_SeqAIJ_inplace;
922:   A->ops->solvetransposeadd = MatSolveTransposeAdd_SeqAIJ_inplace;

924:   A->assembled    = PETSC_TRUE;
925:   A->preallocated = PETSC_TRUE;

927:   PetscLogFlops(A->cmap->n);
928:   if (sctx.nshift) {
929:     if (info->shifttype == (PetscReal)MAT_SHIFT_POSITIVE_DEFINITE) {
930:       PetscInfo4(A,"number of shift_pd tries %D, shift_amount %g, diagonal shifted up by %e fraction top_value %e\n",sctx.nshift,(double)sctx.shift_amount,(double)sctx.shift_fraction,(double)sctx.shift_top);
931:     } else if (info->shifttype == (PetscReal)MAT_SHIFT_NONZERO) {
932:       PetscInfo2(A,"number of shift_nz tries %D, shift_amount %g\n",sctx.nshift,(double)sctx.shift_amount);
933:     }
934:   }
935:   return(0);
936: }

938: /* ----------------------------------------------------------- */
939: PetscErrorCode MatLUFactor_SeqAIJ(Mat A,IS row,IS col,const MatFactorInfo *info)
940: {
942:   Mat            C;

945:   MatGetFactor(A,MATSOLVERPETSC,MAT_FACTOR_LU,&C);
946:   MatLUFactorSymbolic(C,A,row,col,info);
947:   MatLUFactorNumeric(C,A,info);

949:   A->ops->solve          = C->ops->solve;
950:   A->ops->solvetranspose = C->ops->solvetranspose;

952:   MatHeaderMerge(A,&C);
953:   PetscLogObjectParent((PetscObject)A,(PetscObject)((Mat_SeqAIJ*)(A->data))->icol);
954:   return(0);
955: }
956: /* ----------------------------------------------------------- */


959: PetscErrorCode MatSolve_SeqAIJ_inplace(Mat A,Vec bb,Vec xx)
960: {
961:   Mat_SeqAIJ        *a    = (Mat_SeqAIJ*)A->data;
962:   IS                iscol = a->col,isrow = a->row;
963:   PetscErrorCode    ierr;
964:   PetscInt          i, n = A->rmap->n,*vi,*ai = a->i,*aj = a->j;
965:   PetscInt          nz;
966:   const PetscInt    *rout,*cout,*r,*c;
967:   PetscScalar       *x,*tmp,*tmps,sum;
968:   const PetscScalar *b;
969:   const MatScalar   *aa = a->a,*v;

972:   if (!n) return(0);

974:   VecGetArrayRead(bb,&b);
975:   VecGetArray(xx,&x);
976:   tmp  = a->solve_work;

978:   ISGetIndices(isrow,&rout); r = rout;
979:   ISGetIndices(iscol,&cout); c = cout + (n-1);

981:   /* forward solve the lower triangular */
982:   tmp[0] = b[*r++];
983:   tmps   = tmp;
984:   for (i=1; i<n; i++) {
985:     v   = aa + ai[i];
986:     vi  = aj + ai[i];
987:     nz  = a->diag[i] - ai[i];
988:     sum = b[*r++];
989:     PetscSparseDenseMinusDot(sum,tmps,v,vi,nz);
990:     tmp[i] = sum;
991:   }

993:   /* backward solve the upper triangular */
994:   for (i=n-1; i>=0; i--) {
995:     v   = aa + a->diag[i] + 1;
996:     vi  = aj + a->diag[i] + 1;
997:     nz  = ai[i+1] - a->diag[i] - 1;
998:     sum = tmp[i];
999:     PetscSparseDenseMinusDot(sum,tmps,v,vi,nz);
1000:     x[*c--] = tmp[i] = sum*aa[a->diag[i]];
1001:   }

1003:   ISRestoreIndices(isrow,&rout);
1004:   ISRestoreIndices(iscol,&cout);
1005:   VecRestoreArrayRead(bb,&b);
1006:   VecRestoreArray(xx,&x);
1007:   PetscLogFlops(2.0*a->nz - A->cmap->n);
1008:   return(0);
1009: }

1011: PetscErrorCode MatMatSolve_SeqAIJ_inplace(Mat A,Mat B,Mat X)
1012: {
1013:   Mat_SeqAIJ        *a    = (Mat_SeqAIJ*)A->data;
1014:   IS                iscol = a->col,isrow = a->row;
1015:   PetscErrorCode    ierr;
1016:   PetscInt          i, n = A->rmap->n,*vi,*ai = a->i,*aj = a->j;
1017:   PetscInt          nz,neq;
1018:   const PetscInt    *rout,*cout,*r,*c;
1019:   PetscScalar       *x,*tmp,*tmps,sum;
1020:   const PetscScalar *aa = a->a,*v;
1021:   const PetscScalar *b;
1022:   PetscBool         bisdense,xisdense;

1025:   if (!n) return(0);

1027:   PetscObjectTypeCompare((PetscObject)B,MATSEQDENSE,&bisdense);
1028:   if (!bisdense) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"B matrix must be a SeqDense matrix");
1029:   PetscObjectTypeCompare((PetscObject)X,MATSEQDENSE,&xisdense);
1030:   if (!xisdense) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"X matrix must be a SeqDense matrix");

1032:   MatDenseGetArrayRead(B,&b);
1033:   MatDenseGetArray(X,&x);

1035:   tmp  = a->solve_work;
1036:   ISGetIndices(isrow,&rout); r = rout;
1037:   ISGetIndices(iscol,&cout); c = cout;

1039:   for (neq=0; neq<B->cmap->n; neq++) {
1040:     /* forward solve the lower triangular */
1041:     tmp[0] = b[r[0]];
1042:     tmps   = tmp;
1043:     for (i=1; i<n; i++) {
1044:       v   = aa + ai[i];
1045:       vi  = aj + ai[i];
1046:       nz  = a->diag[i] - ai[i];
1047:       sum = b[r[i]];
1048:       PetscSparseDenseMinusDot(sum,tmps,v,vi,nz);
1049:       tmp[i] = sum;
1050:     }
1051:     /* backward solve the upper triangular */
1052:     for (i=n-1; i>=0; i--) {
1053:       v   = aa + a->diag[i] + 1;
1054:       vi  = aj + a->diag[i] + 1;
1055:       nz  = ai[i+1] - a->diag[i] - 1;
1056:       sum = tmp[i];
1057:       PetscSparseDenseMinusDot(sum,tmps,v,vi,nz);
1058:       x[c[i]] = tmp[i] = sum*aa[a->diag[i]];
1059:     }

1061:     b += n;
1062:     x += n;
1063:   }
1064:   ISRestoreIndices(isrow,&rout);
1065:   ISRestoreIndices(iscol,&cout);
1066:   MatDenseRestoreArrayRead(B,&b);
1067:   MatDenseRestoreArray(X,&x);
1068:   PetscLogFlops(B->cmap->n*(2.0*a->nz - n));
1069:   return(0);
1070: }

1072: PetscErrorCode MatMatSolve_SeqAIJ(Mat A,Mat B,Mat X)
1073: {
1074:   Mat_SeqAIJ        *a    = (Mat_SeqAIJ*)A->data;
1075:   IS                iscol = a->col,isrow = a->row;
1076:   PetscErrorCode    ierr;
1077:   PetscInt          i, n = A->rmap->n,*vi,*ai = a->i,*aj = a->j,*adiag = a->diag;
1078:   PetscInt          nz,neq;
1079:   const PetscInt    *rout,*cout,*r,*c;
1080:   PetscScalar       *x,*tmp,sum;
1081:   const PetscScalar *b;
1082:   const PetscScalar *aa = a->a,*v;
1083:   PetscBool         bisdense,xisdense;

1086:   if (!n) return(0);

1088:   PetscObjectTypeCompare((PetscObject)B,MATSEQDENSE,&bisdense);
1089:   if (!bisdense) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"B matrix must be a SeqDense matrix");
1090:   PetscObjectTypeCompare((PetscObject)X,MATSEQDENSE,&xisdense);
1091:   if (!xisdense) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"X matrix must be a SeqDense matrix");

1093:   MatDenseGetArrayRead(B,&b);
1094:   MatDenseGetArray(X,&x);

1096:   tmp  = a->solve_work;
1097:   ISGetIndices(isrow,&rout); r = rout;
1098:   ISGetIndices(iscol,&cout); c = cout;

1100:   for (neq=0; neq<B->cmap->n; neq++) {
1101:     /* forward solve the lower triangular */
1102:     tmp[0] = b[r[0]];
1103:     v      = aa;
1104:     vi     = aj;
1105:     for (i=1; i<n; i++) {
1106:       nz  = ai[i+1] - ai[i];
1107:       sum = b[r[i]];
1108:       PetscSparseDenseMinusDot(sum,tmp,v,vi,nz);
1109:       tmp[i] = sum;
1110:       v     += nz; vi += nz;
1111:     }

1113:     /* backward solve the upper triangular */
1114:     for (i=n-1; i>=0; i--) {
1115:       v   = aa + adiag[i+1]+1;
1116:       vi  = aj + adiag[i+1]+1;
1117:       nz  = adiag[i]-adiag[i+1]-1;
1118:       sum = tmp[i];
1119:       PetscSparseDenseMinusDot(sum,tmp,v,vi,nz);
1120:       x[c[i]] = tmp[i] = sum*v[nz]; /* v[nz] = aa[adiag[i]] */
1121:     }

1123:     b += n;
1124:     x += n;
1125:   }
1126:   ISRestoreIndices(isrow,&rout);
1127:   ISRestoreIndices(iscol,&cout);
1128:   MatDenseRestoreArrayRead(B,&b);
1129:   MatDenseRestoreArray(X,&x);
1130:   PetscLogFlops(B->cmap->n*(2.0*a->nz - n));
1131:   return(0);
1132: }

1134: PetscErrorCode MatSolve_SeqAIJ_InplaceWithPerm(Mat A,Vec bb,Vec xx)
1135: {
1136:   Mat_SeqAIJ      *a    = (Mat_SeqAIJ*)A->data;
1137:   IS              iscol = a->col,isrow = a->row;
1138:   PetscErrorCode  ierr;
1139:   const PetscInt  *r,*c,*rout,*cout;
1140:   PetscInt        i, n = A->rmap->n,*vi,*ai = a->i,*aj = a->j;
1141:   PetscInt        nz,row;
1142:   PetscScalar     *x,*b,*tmp,*tmps,sum;
1143:   const MatScalar *aa = a->a,*v;

1146:   if (!n) return(0);

1148:   VecGetArray(bb,&b);
1149:   VecGetArray(xx,&x);
1150:   tmp  = a->solve_work;

1152:   ISGetIndices(isrow,&rout); r = rout;
1153:   ISGetIndices(iscol,&cout); c = cout + (n-1);

1155:   /* forward solve the lower triangular */
1156:   tmp[0] = b[*r++];
1157:   tmps   = tmp;
1158:   for (row=1; row<n; row++) {
1159:     i   = rout[row]; /* permuted row */
1160:     v   = aa + ai[i];
1161:     vi  = aj + ai[i];
1162:     nz  = a->diag[i] - ai[i];
1163:     sum = b[*r++];
1164:     PetscSparseDenseMinusDot(sum,tmps,v,vi,nz);
1165:     tmp[row] = sum;
1166:   }

1168:   /* backward solve the upper triangular */
1169:   for (row=n-1; row>=0; row--) {
1170:     i   = rout[row]; /* permuted row */
1171:     v   = aa + a->diag[i] + 1;
1172:     vi  = aj + a->diag[i] + 1;
1173:     nz  = ai[i+1] - a->diag[i] - 1;
1174:     sum = tmp[row];
1175:     PetscSparseDenseMinusDot(sum,tmps,v,vi,nz);
1176:     x[*c--] = tmp[row] = sum*aa[a->diag[i]];
1177:   }

1179:   ISRestoreIndices(isrow,&rout);
1180:   ISRestoreIndices(iscol,&cout);
1181:   VecRestoreArray(bb,&b);
1182:   VecRestoreArray(xx,&x);
1183:   PetscLogFlops(2.0*a->nz - A->cmap->n);
1184:   return(0);
1185: }

1187: /* ----------------------------------------------------------- */
1188: #include <../src/mat/impls/aij/seq/ftn-kernels/fsolve.h>
1189: PetscErrorCode MatSolve_SeqAIJ_NaturalOrdering_inplace(Mat A,Vec bb,Vec xx)
1190: {
1191:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1192:   PetscErrorCode    ierr;
1193:   PetscInt          n   = A->rmap->n;
1194:   const PetscInt    *ai = a->i,*aj = a->j,*adiag = a->diag;
1195:   PetscScalar       *x;
1196:   const PetscScalar *b;
1197:   const MatScalar   *aa = a->a;
1198: #if !defined(PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ)
1199:   PetscInt        adiag_i,i,nz,ai_i;
1200:   const PetscInt  *vi;
1201:   const MatScalar *v;
1202:   PetscScalar     sum;
1203: #endif

1206:   if (!n) return(0);

1208:   VecGetArrayRead(bb,&b);
1209:   VecGetArray(xx,&x);

1211: #if defined(PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ)
1212:   fortransolveaij_(&n,x,ai,aj,adiag,aa,b);
1213: #else
1214:   /* forward solve the lower triangular */
1215:   x[0] = b[0];
1216:   for (i=1; i<n; i++) {
1217:     ai_i = ai[i];
1218:     v    = aa + ai_i;
1219:     vi   = aj + ai_i;
1220:     nz   = adiag[i] - ai_i;
1221:     sum  = b[i];
1222:     PetscSparseDenseMinusDot(sum,x,v,vi,nz);
1223:     x[i] = sum;
1224:   }

1226:   /* backward solve the upper triangular */
1227:   for (i=n-1; i>=0; i--) {
1228:     adiag_i = adiag[i];
1229:     v       = aa + adiag_i + 1;
1230:     vi      = aj + adiag_i + 1;
1231:     nz      = ai[i+1] - adiag_i - 1;
1232:     sum     = x[i];
1233:     PetscSparseDenseMinusDot(sum,x,v,vi,nz);
1234:     x[i] = sum*aa[adiag_i];
1235:   }
1236: #endif
1237:   PetscLogFlops(2.0*a->nz - A->cmap->n);
1238:   VecRestoreArrayRead(bb,&b);
1239:   VecRestoreArray(xx,&x);
1240:   return(0);
1241: }

1243: PetscErrorCode MatSolveAdd_SeqAIJ_inplace(Mat A,Vec bb,Vec yy,Vec xx)
1244: {
1245:   Mat_SeqAIJ        *a    = (Mat_SeqAIJ*)A->data;
1246:   IS                iscol = a->col,isrow = a->row;
1247:   PetscErrorCode    ierr;
1248:   PetscInt          i, n = A->rmap->n,j;
1249:   PetscInt          nz;
1250:   const PetscInt    *rout,*cout,*r,*c,*vi,*ai = a->i,*aj = a->j;
1251:   PetscScalar       *x,*tmp,sum;
1252:   const PetscScalar *b;
1253:   const MatScalar   *aa = a->a,*v;

1256:   if (yy != xx) {VecCopy(yy,xx);}

1258:   VecGetArrayRead(bb,&b);
1259:   VecGetArray(xx,&x);
1260:   tmp  = a->solve_work;

1262:   ISGetIndices(isrow,&rout); r = rout;
1263:   ISGetIndices(iscol,&cout); c = cout + (n-1);

1265:   /* forward solve the lower triangular */
1266:   tmp[0] = b[*r++];
1267:   for (i=1; i<n; i++) {
1268:     v   = aa + ai[i];
1269:     vi  = aj + ai[i];
1270:     nz  = a->diag[i] - ai[i];
1271:     sum = b[*r++];
1272:     for (j=0; j<nz; j++) sum -= v[j]*tmp[vi[j]];
1273:     tmp[i] = sum;
1274:   }

1276:   /* backward solve the upper triangular */
1277:   for (i=n-1; i>=0; i--) {
1278:     v   = aa + a->diag[i] + 1;
1279:     vi  = aj + a->diag[i] + 1;
1280:     nz  = ai[i+1] - a->diag[i] - 1;
1281:     sum = tmp[i];
1282:     for (j=0; j<nz; j++) sum -= v[j]*tmp[vi[j]];
1283:     tmp[i]   = sum*aa[a->diag[i]];
1284:     x[*c--] += tmp[i];
1285:   }

1287:   ISRestoreIndices(isrow,&rout);
1288:   ISRestoreIndices(iscol,&cout);
1289:   VecRestoreArrayRead(bb,&b);
1290:   VecRestoreArray(xx,&x);
1291:   PetscLogFlops(2.0*a->nz);
1292:   return(0);
1293: }

1295: PetscErrorCode MatSolveAdd_SeqAIJ(Mat A,Vec bb,Vec yy,Vec xx)
1296: {
1297:   Mat_SeqAIJ        *a    = (Mat_SeqAIJ*)A->data;
1298:   IS                iscol = a->col,isrow = a->row;
1299:   PetscErrorCode    ierr;
1300:   PetscInt          i, n = A->rmap->n,j;
1301:   PetscInt          nz;
1302:   const PetscInt    *rout,*cout,*r,*c,*vi,*ai = a->i,*aj = a->j,*adiag = a->diag;
1303:   PetscScalar       *x,*tmp,sum;
1304:   const PetscScalar *b;
1305:   const MatScalar   *aa = a->a,*v;

1308:   if (yy != xx) {VecCopy(yy,xx);}

1310:   VecGetArrayRead(bb,&b);
1311:   VecGetArray(xx,&x);
1312:   tmp  = a->solve_work;

1314:   ISGetIndices(isrow,&rout); r = rout;
1315:   ISGetIndices(iscol,&cout); c = cout;

1317:   /* forward solve the lower triangular */
1318:   tmp[0] = b[r[0]];
1319:   v      = aa;
1320:   vi     = aj;
1321:   for (i=1; i<n; i++) {
1322:     nz  = ai[i+1] - ai[i];
1323:     sum = b[r[i]];
1324:     for (j=0; j<nz; j++) sum -= v[j]*tmp[vi[j]];
1325:     tmp[i] = sum;
1326:     v     += nz;
1327:     vi    += nz;
1328:   }

1330:   /* backward solve the upper triangular */
1331:   v  = aa + adiag[n-1];
1332:   vi = aj + adiag[n-1];
1333:   for (i=n-1; i>=0; i--) {
1334:     nz  = adiag[i] - adiag[i+1] - 1;
1335:     sum = tmp[i];
1336:     for (j=0; j<nz; j++) sum -= v[j]*tmp[vi[j]];
1337:     tmp[i]   = sum*v[nz];
1338:     x[c[i]] += tmp[i];
1339:     v       += nz+1; vi += nz+1;
1340:   }

1342:   ISRestoreIndices(isrow,&rout);
1343:   ISRestoreIndices(iscol,&cout);
1344:   VecRestoreArrayRead(bb,&b);
1345:   VecRestoreArray(xx,&x);
1346:   PetscLogFlops(2.0*a->nz);
1347:   return(0);
1348: }

1350: PetscErrorCode MatSolveTranspose_SeqAIJ_inplace(Mat A,Vec bb,Vec xx)
1351: {
1352:   Mat_SeqAIJ        *a    = (Mat_SeqAIJ*)A->data;
1353:   IS                iscol = a->col,isrow = a->row;
1354:   PetscErrorCode    ierr;
1355:   const PetscInt    *rout,*cout,*r,*c,*diag = a->diag,*ai = a->i,*aj = a->j,*vi;
1356:   PetscInt          i,n = A->rmap->n,j;
1357:   PetscInt          nz;
1358:   PetscScalar       *x,*tmp,s1;
1359:   const MatScalar   *aa = a->a,*v;
1360:   const PetscScalar *b;

1363:   VecGetArrayRead(bb,&b);
1364:   VecGetArray(xx,&x);
1365:   tmp  = a->solve_work;

1367:   ISGetIndices(isrow,&rout); r = rout;
1368:   ISGetIndices(iscol,&cout); c = cout;

1370:   /* copy the b into temp work space according to permutation */
1371:   for (i=0; i<n; i++) tmp[i] = b[c[i]];

1373:   /* forward solve the U^T */
1374:   for (i=0; i<n; i++) {
1375:     v   = aa + diag[i];
1376:     vi  = aj + diag[i] + 1;
1377:     nz  = ai[i+1] - diag[i] - 1;
1378:     s1  = tmp[i];
1379:     s1 *= (*v++);  /* multiply by inverse of diagonal entry */
1380:     for (j=0; j<nz; j++) tmp[vi[j]] -= s1*v[j];
1381:     tmp[i] = s1;
1382:   }

1384:   /* backward solve the L^T */
1385:   for (i=n-1; i>=0; i--) {
1386:     v  = aa + diag[i] - 1;
1387:     vi = aj + diag[i] - 1;
1388:     nz = diag[i] - ai[i];
1389:     s1 = tmp[i];
1390:     for (j=0; j>-nz; j--) tmp[vi[j]] -= s1*v[j];
1391:   }

1393:   /* copy tmp into x according to permutation */
1394:   for (i=0; i<n; i++) x[r[i]] = tmp[i];

1396:   ISRestoreIndices(isrow,&rout);
1397:   ISRestoreIndices(iscol,&cout);
1398:   VecRestoreArrayRead(bb,&b);
1399:   VecRestoreArray(xx,&x);

1401:   PetscLogFlops(2.0*a->nz-A->cmap->n);
1402:   return(0);
1403: }

1405: PetscErrorCode MatSolveTranspose_SeqAIJ(Mat A,Vec bb,Vec xx)
1406: {
1407:   Mat_SeqAIJ        *a    = (Mat_SeqAIJ*)A->data;
1408:   IS                iscol = a->col,isrow = a->row;
1409:   PetscErrorCode    ierr;
1410:   const PetscInt    *rout,*cout,*r,*c,*adiag = a->diag,*ai = a->i,*aj = a->j,*vi;
1411:   PetscInt          i,n = A->rmap->n,j;
1412:   PetscInt          nz;
1413:   PetscScalar       *x,*tmp,s1;
1414:   const MatScalar   *aa = a->a,*v;
1415:   const PetscScalar *b;

1418:   VecGetArrayRead(bb,&b);
1419:   VecGetArray(xx,&x);
1420:   tmp  = a->solve_work;

1422:   ISGetIndices(isrow,&rout); r = rout;
1423:   ISGetIndices(iscol,&cout); c = cout;

1425:   /* copy the b into temp work space according to permutation */
1426:   for (i=0; i<n; i++) tmp[i] = b[c[i]];

1428:   /* forward solve the U^T */
1429:   for (i=0; i<n; i++) {
1430:     v   = aa + adiag[i+1] + 1;
1431:     vi  = aj + adiag[i+1] + 1;
1432:     nz  = adiag[i] - adiag[i+1] - 1;
1433:     s1  = tmp[i];
1434:     s1 *= v[nz];  /* multiply by inverse of diagonal entry */
1435:     for (j=0; j<nz; j++) tmp[vi[j]] -= s1*v[j];
1436:     tmp[i] = s1;
1437:   }

1439:   /* backward solve the L^T */
1440:   for (i=n-1; i>=0; i--) {
1441:     v  = aa + ai[i];
1442:     vi = aj + ai[i];
1443:     nz = ai[i+1] - ai[i];
1444:     s1 = tmp[i];
1445:     for (j=0; j<nz; j++) tmp[vi[j]] -= s1*v[j];
1446:   }

1448:   /* copy tmp into x according to permutation */
1449:   for (i=0; i<n; i++) x[r[i]] = tmp[i];

1451:   ISRestoreIndices(isrow,&rout);
1452:   ISRestoreIndices(iscol,&cout);
1453:   VecRestoreArrayRead(bb,&b);
1454:   VecRestoreArray(xx,&x);

1456:   PetscLogFlops(2.0*a->nz-A->cmap->n);
1457:   return(0);
1458: }

1460: PetscErrorCode MatSolveTransposeAdd_SeqAIJ_inplace(Mat A,Vec bb,Vec zz,Vec xx)
1461: {
1462:   Mat_SeqAIJ        *a    = (Mat_SeqAIJ*)A->data;
1463:   IS                iscol = a->col,isrow = a->row;
1464:   PetscErrorCode    ierr;
1465:   const PetscInt    *rout,*cout,*r,*c,*diag = a->diag,*ai = a->i,*aj = a->j,*vi;
1466:   PetscInt          i,n = A->rmap->n,j;
1467:   PetscInt          nz;
1468:   PetscScalar       *x,*tmp,s1;
1469:   const MatScalar   *aa = a->a,*v;
1470:   const PetscScalar *b;

1473:   if (zz != xx) {VecCopy(zz,xx);}
1474:   VecGetArrayRead(bb,&b);
1475:   VecGetArray(xx,&x);
1476:   tmp  = a->solve_work;

1478:   ISGetIndices(isrow,&rout); r = rout;
1479:   ISGetIndices(iscol,&cout); c = cout;

1481:   /* copy the b into temp work space according to permutation */
1482:   for (i=0; i<n; i++) tmp[i] = b[c[i]];

1484:   /* forward solve the U^T */
1485:   for (i=0; i<n; i++) {
1486:     v   = aa + diag[i];
1487:     vi  = aj + diag[i] + 1;
1488:     nz  = ai[i+1] - diag[i] - 1;
1489:     s1  = tmp[i];
1490:     s1 *= (*v++);  /* multiply by inverse of diagonal entry */
1491:     for (j=0; j<nz; j++) tmp[vi[j]] -= s1*v[j];
1492:     tmp[i] = s1;
1493:   }

1495:   /* backward solve the L^T */
1496:   for (i=n-1; i>=0; i--) {
1497:     v  = aa + diag[i] - 1;
1498:     vi = aj + diag[i] - 1;
1499:     nz = diag[i] - ai[i];
1500:     s1 = tmp[i];
1501:     for (j=0; j>-nz; j--) tmp[vi[j]] -= s1*v[j];
1502:   }

1504:   /* copy tmp into x according to permutation */
1505:   for (i=0; i<n; i++) x[r[i]] += tmp[i];

1507:   ISRestoreIndices(isrow,&rout);
1508:   ISRestoreIndices(iscol,&cout);
1509:   VecRestoreArrayRead(bb,&b);
1510:   VecRestoreArray(xx,&x);

1512:   PetscLogFlops(2.0*a->nz-A->cmap->n);
1513:   return(0);
1514: }

1516: PetscErrorCode MatSolveTransposeAdd_SeqAIJ(Mat A,Vec bb,Vec zz,Vec xx)
1517: {
1518:   Mat_SeqAIJ        *a    = (Mat_SeqAIJ*)A->data;
1519:   IS                iscol = a->col,isrow = a->row;
1520:   PetscErrorCode    ierr;
1521:   const PetscInt    *rout,*cout,*r,*c,*adiag = a->diag,*ai = a->i,*aj = a->j,*vi;
1522:   PetscInt          i,n = A->rmap->n,j;
1523:   PetscInt          nz;
1524:   PetscScalar       *x,*tmp,s1;
1525:   const MatScalar   *aa = a->a,*v;
1526:   const PetscScalar *b;

1529:   if (zz != xx) {VecCopy(zz,xx);}
1530:   VecGetArrayRead(bb,&b);
1531:   VecGetArray(xx,&x);
1532:   tmp  = a->solve_work;

1534:   ISGetIndices(isrow,&rout); r = rout;
1535:   ISGetIndices(iscol,&cout); c = cout;

1537:   /* copy the b into temp work space according to permutation */
1538:   for (i=0; i<n; i++) tmp[i] = b[c[i]];

1540:   /* forward solve the U^T */
1541:   for (i=0; i<n; i++) {
1542:     v   = aa + adiag[i+1] + 1;
1543:     vi  = aj + adiag[i+1] + 1;
1544:     nz  = adiag[i] - adiag[i+1] - 1;
1545:     s1  = tmp[i];
1546:     s1 *= v[nz];  /* multiply by inverse of diagonal entry */
1547:     for (j=0; j<nz; j++) tmp[vi[j]] -= s1*v[j];
1548:     tmp[i] = s1;
1549:   }


1552:   /* backward solve the L^T */
1553:   for (i=n-1; i>=0; i--) {
1554:     v  = aa + ai[i];
1555:     vi = aj + ai[i];
1556:     nz = ai[i+1] - ai[i];
1557:     s1 = tmp[i];
1558:     for (j=0; j<nz; j++) tmp[vi[j]] -= s1*v[j];
1559:   }

1561:   /* copy tmp into x according to permutation */
1562:   for (i=0; i<n; i++) x[r[i]] += tmp[i];

1564:   ISRestoreIndices(isrow,&rout);
1565:   ISRestoreIndices(iscol,&cout);
1566:   VecRestoreArrayRead(bb,&b);
1567:   VecRestoreArray(xx,&x);

1569:   PetscLogFlops(2.0*a->nz-A->cmap->n);
1570:   return(0);
1571: }

1573: /* ----------------------------------------------------------------*/

1575: /*
1576:    ilu() under revised new data structure.
1577:    Factored arrays bj and ba are stored as
1578:      L(0,:), L(1,:), ...,L(n-1,:),  U(n-1,:),...,U(i,:),U(i-1,:),...,U(0,:)

1580:    bi=fact->i is an array of size n+1, in which
1581:    bi+
1582:      bi[i]:  points to 1st entry of L(i,:),i=0,...,n-1
1583:      bi[n]:  points to L(n-1,n-1)+1

1585:   bdiag=fact->diag is an array of size n+1,in which
1586:      bdiag[i]: points to diagonal of U(i,:), i=0,...,n-1
1587:      bdiag[n]: points to entry of U(n-1,0)-1

1589:    U(i,:) contains bdiag[i] as its last entry, i.e.,
1590:     U(i,:) = (u[i,i+1],...,u[i,n-1],diag[i])
1591: */
1592: PetscErrorCode MatILUFactorSymbolic_SeqAIJ_ilu0(Mat fact,Mat A,IS isrow,IS iscol,const MatFactorInfo *info)
1593: {
1594:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*b;
1596:   const PetscInt n=A->rmap->n,*ai=a->i,*aj,*adiag=a->diag;
1597:   PetscInt       i,j,k=0,nz,*bi,*bj,*bdiag;
1598:   IS             isicol;

1601:   ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);
1602:   MatDuplicateNoCreate_SeqAIJ(fact,A,MAT_DO_NOT_COPY_VALUES,PETSC_FALSE);
1603:   b    = (Mat_SeqAIJ*)(fact)->data;

1605:   /* allocate matrix arrays for new data structure */
1606:   PetscMalloc3(ai[n]+1,&b->a,ai[n]+1,&b->j,n+1,&b->i);
1607:   PetscLogObjectMemory((PetscObject)fact,ai[n]*(sizeof(PetscScalar)+sizeof(PetscInt))+(n+1)*sizeof(PetscInt));

1609:   b->singlemalloc = PETSC_TRUE;
1610:   if (!b->diag) {
1611:     PetscMalloc1(n+1,&b->diag);
1612:     PetscLogObjectMemory((PetscObject)fact,(n+1)*sizeof(PetscInt));
1613:   }
1614:   bdiag = b->diag;

1616:   if (n > 0) {
1617:     PetscMemzero(b->a,(ai[n])*sizeof(MatScalar));
1618:   }

1620:   /* set bi and bj with new data structure */
1621:   bi = b->i;
1622:   bj = b->j;

1624:   /* L part */
1625:   bi[0] = 0;
1626:   for (i=0; i<n; i++) {
1627:     nz      = adiag[i] - ai[i];
1628:     bi[i+1] = bi[i] + nz;
1629:     aj      = a->j + ai[i];
1630:     for (j=0; j<nz; j++) {
1631:       /*   *bj = aj[j]; bj++; */
1632:       bj[k++] = aj[j];
1633:     }
1634:   }

1636:   /* U part */
1637:   bdiag[n] = bi[n]-1;
1638:   for (i=n-1; i>=0; i--) {
1639:     nz = ai[i+1] - adiag[i] - 1;
1640:     aj = a->j + adiag[i] + 1;
1641:     for (j=0; j<nz; j++) {
1642:       /*      *bj = aj[j]; bj++; */
1643:       bj[k++] = aj[j];
1644:     }
1645:     /* diag[i] */
1646:     /*    *bj = i; bj++; */
1647:     bj[k++]  = i;
1648:     bdiag[i] = bdiag[i+1] + nz + 1;
1649:   }

1651:   fact->factortype             = MAT_FACTOR_ILU;
1652:   fact->info.factor_mallocs    = 0;
1653:   fact->info.fill_ratio_given  = info->fill;
1654:   fact->info.fill_ratio_needed = 1.0;
1655:   fact->ops->lufactornumeric   = MatLUFactorNumeric_SeqAIJ;
1656:   MatSeqAIJCheckInode_FactorLU(fact);

1658:   b       = (Mat_SeqAIJ*)(fact)->data;
1659:   b->row  = isrow;
1660:   b->col  = iscol;
1661:   b->icol = isicol;
1662:   PetscMalloc1(fact->rmap->n+1,&b->solve_work);
1663:   PetscObjectReference((PetscObject)isrow);
1664:   PetscObjectReference((PetscObject)iscol);
1665:   return(0);
1666: }

1668: PetscErrorCode MatILUFactorSymbolic_SeqAIJ(Mat fact,Mat A,IS isrow,IS iscol,const MatFactorInfo *info)
1669: {
1670:   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data,*b;
1671:   IS                 isicol;
1672:   PetscErrorCode     ierr;
1673:   const PetscInt     *r,*ic;
1674:   PetscInt           n=A->rmap->n,*ai=a->i,*aj=a->j;
1675:   PetscInt           *bi,*cols,nnz,*cols_lvl;
1676:   PetscInt           *bdiag,prow,fm,nzbd,reallocs=0,dcount=0;
1677:   PetscInt           i,levels,diagonal_fill;
1678:   PetscBool          col_identity,row_identity,missing;
1679:   PetscReal          f;
1680:   PetscInt           nlnk,*lnk,*lnk_lvl=NULL;
1681:   PetscBT            lnkbt;
1682:   PetscInt           nzi,*bj,**bj_ptr,**bjlvl_ptr;
1683:   PetscFreeSpaceList free_space    =NULL,current_space=NULL;
1684:   PetscFreeSpaceList free_space_lvl=NULL,current_space_lvl=NULL;

1687:   if (A->rmap->n != A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Must be square matrix, rows %D columns %D",A->rmap->n,A->cmap->n);
1688:   MatMissingDiagonal(A,&missing,&i);
1689:   if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",i);
1690: 
1691:   levels = (PetscInt)info->levels;
1692:   ISIdentity(isrow,&row_identity);
1693:   ISIdentity(iscol,&col_identity);
1694:   if (!levels && row_identity && col_identity) {
1695:     /* special case: ilu(0) with natural ordering */
1696:     MatILUFactorSymbolic_SeqAIJ_ilu0(fact,A,isrow,iscol,info);
1697:     if (a->inode.size) {
1698:       fact->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJ_Inode;
1699:     }
1700:     return(0);
1701:   }

1703:   ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);
1704:   ISGetIndices(isrow,&r);
1705:   ISGetIndices(isicol,&ic);

1707:   /* get new row and diagonal pointers, must be allocated separately because they will be given to the Mat_SeqAIJ and freed separately */
1708:   PetscMalloc1(n+1,&bi);
1709:   PetscMalloc1(n+1,&bdiag);
1710:   bi[0] = bdiag[0] = 0;
1711:   PetscMalloc2(n,&bj_ptr,n,&bjlvl_ptr);

1713:   /* create a linked list for storing column indices of the active row */
1714:   nlnk = n + 1;
1715:   PetscIncompleteLLCreate(n,n,nlnk,lnk,lnk_lvl,lnkbt);

1717:   /* initial FreeSpace size is f*(ai[n]+1) */
1718:   f                 = info->fill;
1719:   diagonal_fill     = (PetscInt)info->diagonal_fill;
1720:   PetscFreeSpaceGet(PetscRealIntMultTruncate(f,ai[n]+1),&free_space);
1721:   current_space     = free_space;
1722:   PetscFreeSpaceGet(PetscRealIntMultTruncate(f,ai[n]+1),&free_space_lvl);
1723:   current_space_lvl = free_space_lvl;
1724:   for (i=0; i<n; i++) {
1725:     nzi = 0;
1726:     /* copy current row into linked list */
1727:     nnz = ai[r[i]+1] - ai[r[i]];
1728:     if (!nnz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_MAT_LU_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",r[i],i);
1729:     cols   = aj + ai[r[i]];
1730:     lnk[i] = -1; /* marker to indicate if diagonal exists */
1731:     PetscIncompleteLLInit(nnz,cols,n,ic,nlnk,lnk,lnk_lvl,lnkbt);
1732:     nzi   += nlnk;

1734:     /* make sure diagonal entry is included */
1735:     if (diagonal_fill && lnk[i] == -1) {
1736:       fm = n;
1737:       while (lnk[fm] < i) fm = lnk[fm];
1738:       lnk[i]     = lnk[fm]; /* insert diagonal into linked list */
1739:       lnk[fm]    = i;
1740:       lnk_lvl[i] = 0;
1741:       nzi++; dcount++;
1742:     }

1744:     /* add pivot rows into the active row */
1745:     nzbd = 0;
1746:     prow = lnk[n];
1747:     while (prow < i) {
1748:       nnz      = bdiag[prow];
1749:       cols     = bj_ptr[prow] + nnz + 1;
1750:       cols_lvl = bjlvl_ptr[prow] + nnz + 1;
1751:       nnz      = bi[prow+1] - bi[prow] - nnz - 1;
1752:       PetscILULLAddSorted(nnz,cols,levels,cols_lvl,prow,nlnk,lnk,lnk_lvl,lnkbt,prow);
1753:       nzi     += nlnk;
1754:       prow     = lnk[prow];
1755:       nzbd++;
1756:     }
1757:     bdiag[i] = nzbd;
1758:     bi[i+1]  = bi[i] + nzi;
1759:     /* if free space is not available, make more free space */
1760:     if (current_space->local_remaining<nzi) {
1761:       nnz  = PetscIntMultTruncate(2,PetscIntMultTruncate(nzi,n - i)); /* estimated and max additional space needed */
1762:       PetscFreeSpaceGet(nnz,&current_space);
1763:       PetscFreeSpaceGet(nnz,&current_space_lvl);
1764:       reallocs++;
1765:     }

1767:     /* copy data into free_space and free_space_lvl, then initialize lnk */
1768:     PetscIncompleteLLClean(n,n,nzi,lnk,lnk_lvl,current_space->array,current_space_lvl->array,lnkbt);
1769:     bj_ptr[i]    = current_space->array;
1770:     bjlvl_ptr[i] = current_space_lvl->array;

1772:     /* make sure the active row i has diagonal entry */
1773:     if (*(bj_ptr[i]+bdiag[i]) != i) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_MAT_LU_ZRPVT,"Row %D has missing diagonal in factored matrix\ntry running with -pc_factor_nonzeros_along_diagonal or -pc_factor_diagonal_fill",i);

1775:     current_space->array               += nzi;
1776:     current_space->local_used          += nzi;
1777:     current_space->local_remaining     -= nzi;
1778:     current_space_lvl->array           += nzi;
1779:     current_space_lvl->local_used      += nzi;
1780:     current_space_lvl->local_remaining -= nzi;
1781:   }

1783:   ISRestoreIndices(isrow,&r);
1784:   ISRestoreIndices(isicol,&ic);
1785:   /* copy free_space into bj and free free_space; set bi, bj, bdiag in new datastructure; */
1786:   PetscMalloc1(bi[n]+1,&bj);
1787:   PetscFreeSpaceContiguous_LU(&free_space,bj,n,bi,bdiag);

1789:   PetscIncompleteLLDestroy(lnk,lnkbt);
1790:   PetscFreeSpaceDestroy(free_space_lvl);
1791:   PetscFree2(bj_ptr,bjlvl_ptr);

1793: #if defined(PETSC_USE_INFO)
1794:   {
1795:     PetscReal af = ((PetscReal)(bdiag[0]+1))/((PetscReal)ai[n]);
1796:     PetscInfo3(A,"Reallocs %D Fill ratio:given %g needed %g\n",reallocs,(double)f,(double)af);
1797:     PetscInfo1(A,"Run with -[sub_]pc_factor_fill %g or use \n",(double)af);
1798:     PetscInfo1(A,"PCFactorSetFill([sub]pc,%g);\n",(double)af);
1799:     PetscInfo(A,"for best performance.\n");
1800:     if (diagonal_fill) {
1801:       PetscInfo1(A,"Detected and replaced %D missing diagonals\n",dcount);
1802:     }
1803:   }
1804: #endif
1805:   /* put together the new matrix */
1806:   MatSeqAIJSetPreallocation_SeqAIJ(fact,MAT_SKIP_ALLOCATION,NULL);
1807:   PetscLogObjectParent((PetscObject)fact,(PetscObject)isicol);
1808:   b    = (Mat_SeqAIJ*)(fact)->data;

1810:   b->free_a       = PETSC_TRUE;
1811:   b->free_ij      = PETSC_TRUE;
1812:   b->singlemalloc = PETSC_FALSE;

1814:   PetscMalloc1(bdiag[0]+1,&b->a);

1816:   b->j    = bj;
1817:   b->i    = bi;
1818:   b->diag = bdiag;
1819:   b->ilen = 0;
1820:   b->imax = 0;
1821:   b->row  = isrow;
1822:   b->col  = iscol;
1823:   PetscObjectReference((PetscObject)isrow);
1824:   PetscObjectReference((PetscObject)iscol);
1825:   b->icol = isicol;

1827:   PetscMalloc1(n+1,&b->solve_work);
1828:   /* In b structure:  Free imax, ilen, old a, old j.
1829:      Allocate bdiag, solve_work, new a, new j */
1830:   PetscLogObjectMemory((PetscObject)fact,(bdiag[0]+1)*(sizeof(PetscInt)+sizeof(PetscScalar)));
1831:   b->maxnz = b->nz = bdiag[0]+1;

1833:   (fact)->info.factor_mallocs    = reallocs;
1834:   (fact)->info.fill_ratio_given  = f;
1835:   (fact)->info.fill_ratio_needed = ((PetscReal)(bdiag[0]+1))/((PetscReal)ai[n]);
1836:   (fact)->ops->lufactornumeric   = MatLUFactorNumeric_SeqAIJ;
1837:   if (a->inode.size) {
1838:     (fact)->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJ_Inode;
1839:   }
1840:   MatSeqAIJCheckInode_FactorLU(fact);
1841:   return(0);
1842: }

1844: PetscErrorCode MatILUFactorSymbolic_SeqAIJ_inplace(Mat fact,Mat A,IS isrow,IS iscol,const MatFactorInfo *info)
1845: {
1846:   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data,*b;
1847:   IS                 isicol;
1848:   PetscErrorCode     ierr;
1849:   const PetscInt     *r,*ic;
1850:   PetscInt           n=A->rmap->n,*ai=a->i,*aj=a->j;
1851:   PetscInt           *bi,*cols,nnz,*cols_lvl;
1852:   PetscInt           *bdiag,prow,fm,nzbd,reallocs=0,dcount=0;
1853:   PetscInt           i,levels,diagonal_fill;
1854:   PetscBool          col_identity,row_identity;
1855:   PetscReal          f;
1856:   PetscInt           nlnk,*lnk,*lnk_lvl=NULL;
1857:   PetscBT            lnkbt;
1858:   PetscInt           nzi,*bj,**bj_ptr,**bjlvl_ptr;
1859:   PetscFreeSpaceList free_space    =NULL,current_space=NULL;
1860:   PetscFreeSpaceList free_space_lvl=NULL,current_space_lvl=NULL;
1861:   PetscBool          missing;

1864:   if (A->rmap->n != A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Must be square matrix, rows %D columns %D",A->rmap->n,A->cmap->n);
1865:   MatMissingDiagonal(A,&missing,&i);
1866:   if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",i);

1868:   f             = info->fill;
1869:   levels        = (PetscInt)info->levels;
1870:   diagonal_fill = (PetscInt)info->diagonal_fill;

1872:   ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);

1874:   ISIdentity(isrow,&row_identity);
1875:   ISIdentity(iscol,&col_identity);
1876:   if (!levels && row_identity && col_identity) { /* special case: ilu(0) with natural ordering */
1877:     MatDuplicateNoCreate_SeqAIJ(fact,A,MAT_DO_NOT_COPY_VALUES,PETSC_TRUE);

1879:     (fact)->ops->lufactornumeric =  MatLUFactorNumeric_SeqAIJ_inplace;
1880:     if (a->inode.size) {
1881:       (fact)->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJ_Inode_inplace;
1882:     }
1883:     fact->factortype               = MAT_FACTOR_ILU;
1884:     (fact)->info.factor_mallocs    = 0;
1885:     (fact)->info.fill_ratio_given  = info->fill;
1886:     (fact)->info.fill_ratio_needed = 1.0;

1888:     b    = (Mat_SeqAIJ*)(fact)->data;
1889:     b->row  = isrow;
1890:     b->col  = iscol;
1891:     b->icol = isicol;
1892:     PetscMalloc1((fact)->rmap->n+1,&b->solve_work);
1893:     PetscObjectReference((PetscObject)isrow);
1894:     PetscObjectReference((PetscObject)iscol);
1895:     return(0);
1896:   }

1898:   ISGetIndices(isrow,&r);
1899:   ISGetIndices(isicol,&ic);

1901:   /* get new row and diagonal pointers, must be allocated separately because they will be given to the Mat_SeqAIJ and freed separately */
1902:   PetscMalloc1(n+1,&bi);
1903:   PetscMalloc1(n+1,&bdiag);
1904:   bi[0] = bdiag[0] = 0;

1906:   PetscMalloc2(n,&bj_ptr,n,&bjlvl_ptr);

1908:   /* create a linked list for storing column indices of the active row */
1909:   nlnk = n + 1;
1910:   PetscIncompleteLLCreate(n,n,nlnk,lnk,lnk_lvl,lnkbt);

1912:   /* initial FreeSpace size is f*(ai[n]+1) */
1913:   PetscFreeSpaceGet(PetscRealIntMultTruncate(f,ai[n]+1),&free_space);
1914:   current_space     = free_space;
1915:   PetscFreeSpaceGet(PetscRealIntMultTruncate(f,ai[n]+1),&free_space_lvl);
1916:   current_space_lvl = free_space_lvl;

1918:   for (i=0; i<n; i++) {
1919:     nzi = 0;
1920:     /* copy current row into linked list */
1921:     nnz = ai[r[i]+1] - ai[r[i]];
1922:     if (!nnz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_MAT_LU_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",r[i],i);
1923:     cols   = aj + ai[r[i]];
1924:     lnk[i] = -1; /* marker to indicate if diagonal exists */
1925:     PetscIncompleteLLInit(nnz,cols,n,ic,nlnk,lnk,lnk_lvl,lnkbt);
1926:     nzi   += nlnk;

1928:     /* make sure diagonal entry is included */
1929:     if (diagonal_fill && lnk[i] == -1) {
1930:       fm = n;
1931:       while (lnk[fm] < i) fm = lnk[fm];
1932:       lnk[i]     = lnk[fm]; /* insert diagonal into linked list */
1933:       lnk[fm]    = i;
1934:       lnk_lvl[i] = 0;
1935:       nzi++; dcount++;
1936:     }

1938:     /* add pivot rows into the active row */
1939:     nzbd = 0;
1940:     prow = lnk[n];
1941:     while (prow < i) {
1942:       nnz      = bdiag[prow];
1943:       cols     = bj_ptr[prow] + nnz + 1;
1944:       cols_lvl = bjlvl_ptr[prow] + nnz + 1;
1945:       nnz      = bi[prow+1] - bi[prow] - nnz - 1;
1946:       PetscILULLAddSorted(nnz,cols,levels,cols_lvl,prow,nlnk,lnk,lnk_lvl,lnkbt,prow);
1947:       nzi     += nlnk;
1948:       prow     = lnk[prow];
1949:       nzbd++;
1950:     }
1951:     bdiag[i] = nzbd;
1952:     bi[i+1]  = bi[i] + nzi;

1954:     /* if free space is not available, make more free space */
1955:     if (current_space->local_remaining<nzi) {
1956:       nnz  = PetscIntMultTruncate(nzi,n - i); /* estimated and max additional space needed */
1957:       PetscFreeSpaceGet(nnz,&current_space);
1958:       PetscFreeSpaceGet(nnz,&current_space_lvl);
1959:       reallocs++;
1960:     }

1962:     /* copy data into free_space and free_space_lvl, then initialize lnk */
1963:     PetscIncompleteLLClean(n,n,nzi,lnk,lnk_lvl,current_space->array,current_space_lvl->array,lnkbt);
1964:     bj_ptr[i]    = current_space->array;
1965:     bjlvl_ptr[i] = current_space_lvl->array;

1967:     /* make sure the active row i has diagonal entry */
1968:     if (*(bj_ptr[i]+bdiag[i]) != i) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_MAT_LU_ZRPVT,"Row %D has missing diagonal in factored matrix\ntry running with -pc_factor_nonzeros_along_diagonal or -pc_factor_diagonal_fill",i);

1970:     current_space->array               += nzi;
1971:     current_space->local_used          += nzi;
1972:     current_space->local_remaining     -= nzi;
1973:     current_space_lvl->array           += nzi;
1974:     current_space_lvl->local_used      += nzi;
1975:     current_space_lvl->local_remaining -= nzi;
1976:   }

1978:   ISRestoreIndices(isrow,&r);
1979:   ISRestoreIndices(isicol,&ic);

1981:   /* destroy list of free space and other temporary arrays */
1982:   PetscMalloc1(bi[n]+1,&bj);
1983:   PetscFreeSpaceContiguous(&free_space,bj); /* copy free_space -> bj */
1984:   PetscIncompleteLLDestroy(lnk,lnkbt);
1985:   PetscFreeSpaceDestroy(free_space_lvl);
1986:   PetscFree2(bj_ptr,bjlvl_ptr);

1988: #if defined(PETSC_USE_INFO)
1989:   {
1990:     PetscReal af = ((PetscReal)bi[n])/((PetscReal)ai[n]);
1991:     PetscInfo3(A,"Reallocs %D Fill ratio:given %g needed %g\n",reallocs,(double)f,(double)af);
1992:     PetscInfo1(A,"Run with -[sub_]pc_factor_fill %g or use \n",(double)af);
1993:     PetscInfo1(A,"PCFactorSetFill([sub]pc,%g);\n",(double)af);
1994:     PetscInfo(A,"for best performance.\n");
1995:     if (diagonal_fill) {
1996:       PetscInfo1(A,"Detected and replaced %D missing diagonals\n",dcount);
1997:     }
1998:   }
1999: #endif

2001:   /* put together the new matrix */
2002:   MatSeqAIJSetPreallocation_SeqAIJ(fact,MAT_SKIP_ALLOCATION,NULL);
2003:   PetscLogObjectParent((PetscObject)fact,(PetscObject)isicol);
2004:   b    = (Mat_SeqAIJ*)(fact)->data;

2006:   b->free_a       = PETSC_TRUE;
2007:   b->free_ij      = PETSC_TRUE;
2008:   b->singlemalloc = PETSC_FALSE;

2010:   PetscMalloc1(bi[n],&b->a);
2011:   b->j = bj;
2012:   b->i = bi;
2013:   for (i=0; i<n; i++) bdiag[i] += bi[i];
2014:   b->diag = bdiag;
2015:   b->ilen = 0;
2016:   b->imax = 0;
2017:   b->row  = isrow;
2018:   b->col  = iscol;
2019:   PetscObjectReference((PetscObject)isrow);
2020:   PetscObjectReference((PetscObject)iscol);
2021:   b->icol = isicol;
2022:   PetscMalloc1(n+1,&b->solve_work);
2023:   /* In b structure:  Free imax, ilen, old a, old j.
2024:      Allocate bdiag, solve_work, new a, new j */
2025:   PetscLogObjectMemory((PetscObject)fact,(bi[n]-n) * (sizeof(PetscInt)+sizeof(PetscScalar)));
2026:   b->maxnz = b->nz = bi[n];

2028:   (fact)->info.factor_mallocs    = reallocs;
2029:   (fact)->info.fill_ratio_given  = f;
2030:   (fact)->info.fill_ratio_needed = ((PetscReal)bi[n])/((PetscReal)ai[n]);
2031:   (fact)->ops->lufactornumeric   =  MatLUFactorNumeric_SeqAIJ_inplace;
2032:   if (a->inode.size) {
2033:     (fact)->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJ_Inode_inplace;
2034:   }
2035:   return(0);
2036: }

2038: PetscErrorCode MatCholeskyFactorNumeric_SeqAIJ(Mat B,Mat A,const MatFactorInfo *info)
2039: {
2040:   Mat            C = B;
2041:   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data;
2042:   Mat_SeqSBAIJ   *b=(Mat_SeqSBAIJ*)C->data;
2043:   IS             ip=b->row,iip = b->icol;
2045:   const PetscInt *rip,*riip;
2046:   PetscInt       i,j,mbs=A->rmap->n,*bi=b->i,*bj=b->j,*bdiag=b->diag,*bjtmp;
2047:   PetscInt       *ai=a->i,*aj=a->j;
2048:   PetscInt       k,jmin,jmax,*c2r,*il,col,nexti,ili,nz;
2049:   MatScalar      *rtmp,*ba=b->a,*bval,*aa=a->a,dk,uikdi;
2050:   PetscBool      perm_identity;
2051:   FactorShiftCtx sctx;
2052:   PetscReal      rs;
2053:   MatScalar      d,*v;

2056:   /* MatPivotSetUp(): initialize shift context sctx */
2057:   PetscMemzero(&sctx,sizeof(FactorShiftCtx));

2059:   if (info->shifttype == (PetscReal)MAT_SHIFT_POSITIVE_DEFINITE) { /* set sctx.shift_top=max{rs} */
2060:     sctx.shift_top = info->zeropivot;
2061:     for (i=0; i<mbs; i++) {
2062:       /* calculate sum(|aij|)-RealPart(aii), amt of shift needed for this row */
2063:       d  = (aa)[a->diag[i]];
2064:       rs = -PetscAbsScalar(d) - PetscRealPart(d);
2065:       v  = aa+ai[i];
2066:       nz = ai[i+1] - ai[i];
2067:       for (j=0; j<nz; j++) rs += PetscAbsScalar(v[j]);
2068:       if (rs>sctx.shift_top) sctx.shift_top = rs;
2069:     }
2070:     sctx.shift_top *= 1.1;
2071:     sctx.nshift_max = 5;
2072:     sctx.shift_lo   = 0.;
2073:     sctx.shift_hi   = 1.;
2074:   }

2076:   ISGetIndices(ip,&rip);
2077:   ISGetIndices(iip,&riip);

2079:   /* allocate working arrays
2080:      c2r: linked list, keep track of pivot rows for a given column. c2r[col]: head of the list for a given col
2081:      il:  for active k row, il[i] gives the index of the 1st nonzero entry in U[i,k:n-1] in bj and ba arrays
2082:   */
2083:   PetscMalloc3(mbs,&rtmp,mbs,&il,mbs,&c2r);

2085:   do {
2086:     sctx.newshift = PETSC_FALSE;

2088:     for (i=0; i<mbs; i++) c2r[i] = mbs;
2089:     if (mbs) il[0] = 0;

2091:     for (k = 0; k<mbs; k++) {
2092:       /* zero rtmp */
2093:       nz    = bi[k+1] - bi[k];
2094:       bjtmp = bj + bi[k];
2095:       for (j=0; j<nz; j++) rtmp[bjtmp[j]] = 0.0;

2097:       /* load in initial unfactored row */
2098:       bval = ba + bi[k];
2099:       jmin = ai[rip[k]]; jmax = ai[rip[k]+1];
2100:       for (j = jmin; j < jmax; j++) {
2101:         col = riip[aj[j]];
2102:         if (col >= k) { /* only take upper triangular entry */
2103:           rtmp[col] = aa[j];
2104:           *bval++   = 0.0; /* for in-place factorization */
2105:         }
2106:       }
2107:       /* shift the diagonal of the matrix: ZeropivotApply() */
2108:       rtmp[k] += sctx.shift_amount;  /* shift the diagonal of the matrix */

2110:       /* modify k-th row by adding in those rows i with U(i,k)!=0 */
2111:       dk = rtmp[k];
2112:       i  = c2r[k]; /* first row to be added to k_th row  */

2114:       while (i < k) {
2115:         nexti = c2r[i]; /* next row to be added to k_th row */

2117:         /* compute multiplier, update diag(k) and U(i,k) */
2118:         ili     = il[i]; /* index of first nonzero element in U(i,k:bms-1) */
2119:         uikdi   = -ba[ili]*ba[bdiag[i]]; /* diagonal(k) */
2120:         dk     += uikdi*ba[ili]; /* update diag[k] */
2121:         ba[ili] = uikdi; /* -U(i,k) */

2123:         /* add multiple of row i to k-th row */
2124:         jmin = ili + 1; jmax = bi[i+1];
2125:         if (jmin < jmax) {
2126:           for (j=jmin; j<jmax; j++) rtmp[bj[j]] += uikdi*ba[j];
2127:           /* update il and c2r for row i */
2128:           il[i] = jmin;
2129:           j     = bj[jmin]; c2r[i] = c2r[j]; c2r[j] = i;
2130:         }
2131:         i = nexti;
2132:       }

2134:       /* copy data into U(k,:) */
2135:       rs   = 0.0;
2136:       jmin = bi[k]; jmax = bi[k+1]-1;
2137:       if (jmin < jmax) {
2138:         for (j=jmin; j<jmax; j++) {
2139:           col = bj[j]; ba[j] = rtmp[col]; rs += PetscAbsScalar(ba[j]);
2140:         }
2141:         /* add the k-th row into il and c2r */
2142:         il[k] = jmin;
2143:         i     = bj[jmin]; c2r[k] = c2r[i]; c2r[i] = k;
2144:       }

2146:       /* MatPivotCheck() */
2147:       sctx.rs = rs;
2148:       sctx.pv = dk;
2149:       MatPivotCheck(B,A,info,&sctx,i);
2150:       if (sctx.newshift) break;
2151:       dk = sctx.pv;

2153:       ba[bdiag[k]] = 1.0/dk; /* U(k,k) */
2154:     }
2155:   } while (sctx.newshift);

2157:   PetscFree3(rtmp,il,c2r);
2158:   ISRestoreIndices(ip,&rip);
2159:   ISRestoreIndices(iip,&riip);

2161:   ISIdentity(ip,&perm_identity);
2162:   if (perm_identity) {
2163:     B->ops->solve          = MatSolve_SeqSBAIJ_1_NaturalOrdering;
2164:     B->ops->solvetranspose = MatSolve_SeqSBAIJ_1_NaturalOrdering;
2165:     B->ops->forwardsolve   = MatForwardSolve_SeqSBAIJ_1_NaturalOrdering;
2166:     B->ops->backwardsolve  = MatBackwardSolve_SeqSBAIJ_1_NaturalOrdering;
2167:   } else {
2168:     B->ops->solve          = MatSolve_SeqSBAIJ_1;
2169:     B->ops->solvetranspose = MatSolve_SeqSBAIJ_1;
2170:     B->ops->forwardsolve   = MatForwardSolve_SeqSBAIJ_1;
2171:     B->ops->backwardsolve  = MatBackwardSolve_SeqSBAIJ_1;
2172:   }

2174:   C->assembled    = PETSC_TRUE;
2175:   C->preallocated = PETSC_TRUE;

2177:   PetscLogFlops(C->rmap->n);

2179:   /* MatPivotView() */
2180:   if (sctx.nshift) {
2181:     if (info->shifttype == (PetscReal)MAT_SHIFT_POSITIVE_DEFINITE) {
2182:       PetscInfo4(A,"number of shift_pd tries %D, shift_amount %g, diagonal shifted up by %e fraction top_value %e\n",sctx.nshift,(double)sctx.shift_amount,(double)sctx.shift_fraction,(double)sctx.shift_top);
2183:     } else if (info->shifttype == (PetscReal)MAT_SHIFT_NONZERO) {
2184:       PetscInfo2(A,"number of shift_nz tries %D, shift_amount %g\n",sctx.nshift,(double)sctx.shift_amount);
2185:     } else if (info->shifttype == (PetscReal)MAT_SHIFT_INBLOCKS) {
2186:       PetscInfo2(A,"number of shift_inblocks applied %D, each shift_amount %g\n",sctx.nshift,(double)info->shiftamount);
2187:     }
2188:   }
2189:   return(0);
2190: }

2192: PetscErrorCode MatCholeskyFactorNumeric_SeqAIJ_inplace(Mat B,Mat A,const MatFactorInfo *info)
2193: {
2194:   Mat            C = B;
2195:   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data;
2196:   Mat_SeqSBAIJ   *b=(Mat_SeqSBAIJ*)C->data;
2197:   IS             ip=b->row,iip = b->icol;
2199:   const PetscInt *rip,*riip;
2200:   PetscInt       i,j,mbs=A->rmap->n,*bi=b->i,*bj=b->j,*bcol,*bjtmp;
2201:   PetscInt       *ai=a->i,*aj=a->j;
2202:   PetscInt       k,jmin,jmax,*jl,*il,col,nexti,ili,nz;
2203:   MatScalar      *rtmp,*ba=b->a,*bval,*aa=a->a,dk,uikdi;
2204:   PetscBool      perm_identity;
2205:   FactorShiftCtx sctx;
2206:   PetscReal      rs;
2207:   MatScalar      d,*v;

2210:   /* MatPivotSetUp(): initialize shift context sctx */
2211:   PetscMemzero(&sctx,sizeof(FactorShiftCtx));

2213:   if (info->shifttype == (PetscReal)MAT_SHIFT_POSITIVE_DEFINITE) { /* set sctx.shift_top=max{rs} */
2214:     sctx.shift_top = info->zeropivot;
2215:     for (i=0; i<mbs; i++) {
2216:       /* calculate sum(|aij|)-RealPart(aii), amt of shift needed for this row */
2217:       d  = (aa)[a->diag[i]];
2218:       rs = -PetscAbsScalar(d) - PetscRealPart(d);
2219:       v  = aa+ai[i];
2220:       nz = ai[i+1] - ai[i];
2221:       for (j=0; j<nz; j++) rs += PetscAbsScalar(v[j]);
2222:       if (rs>sctx.shift_top) sctx.shift_top = rs;
2223:     }
2224:     sctx.shift_top *= 1.1;
2225:     sctx.nshift_max = 5;
2226:     sctx.shift_lo   = 0.;
2227:     sctx.shift_hi   = 1.;
2228:   }

2230:   ISGetIndices(ip,&rip);
2231:   ISGetIndices(iip,&riip);

2233:   /* initialization */
2234:   PetscMalloc3(mbs,&rtmp,mbs,&il,mbs,&jl);

2236:   do {
2237:     sctx.newshift = PETSC_FALSE;

2239:     for (i=0; i<mbs; i++) jl[i] = mbs;
2240:     il[0] = 0;

2242:     for (k = 0; k<mbs; k++) {
2243:       /* zero rtmp */
2244:       nz    = bi[k+1] - bi[k];
2245:       bjtmp = bj + bi[k];
2246:       for (j=0; j<nz; j++) rtmp[bjtmp[j]] = 0.0;

2248:       bval = ba + bi[k];
2249:       /* initialize k-th row by the perm[k]-th row of A */
2250:       jmin = ai[rip[k]]; jmax = ai[rip[k]+1];
2251:       for (j = jmin; j < jmax; j++) {
2252:         col = riip[aj[j]];
2253:         if (col >= k) { /* only take upper triangular entry */
2254:           rtmp[col] = aa[j];
2255:           *bval++   = 0.0; /* for in-place factorization */
2256:         }
2257:       }
2258:       /* shift the diagonal of the matrix */
2259:       if (sctx.nshift) rtmp[k] += sctx.shift_amount;

2261:       /* modify k-th row by adding in those rows i with U(i,k)!=0 */
2262:       dk = rtmp[k];
2263:       i  = jl[k]; /* first row to be added to k_th row  */

2265:       while (i < k) {
2266:         nexti = jl[i]; /* next row to be added to k_th row */

2268:         /* compute multiplier, update diag(k) and U(i,k) */
2269:         ili     = il[i]; /* index of first nonzero element in U(i,k:bms-1) */
2270:         uikdi   = -ba[ili]*ba[bi[i]]; /* diagonal(k) */
2271:         dk     += uikdi*ba[ili];
2272:         ba[ili] = uikdi; /* -U(i,k) */

2274:         /* add multiple of row i to k-th row */
2275:         jmin = ili + 1; jmax = bi[i+1];
2276:         if (jmin < jmax) {
2277:           for (j=jmin; j<jmax; j++) rtmp[bj[j]] += uikdi*ba[j];
2278:           /* update il and jl for row i */
2279:           il[i] = jmin;
2280:           j     = bj[jmin]; jl[i] = jl[j]; jl[j] = i;
2281:         }
2282:         i = nexti;
2283:       }

2285:       /* shift the diagonals when zero pivot is detected */
2286:       /* compute rs=sum of abs(off-diagonal) */
2287:       rs   = 0.0;
2288:       jmin = bi[k]+1;
2289:       nz   = bi[k+1] - jmin;
2290:       bcol = bj + jmin;
2291:       for (j=0; j<nz; j++) {
2292:         rs += PetscAbsScalar(rtmp[bcol[j]]);
2293:       }

2295:       sctx.rs = rs;
2296:       sctx.pv = dk;
2297:       MatPivotCheck(B,A,info,&sctx,k);
2298:       if (sctx.newshift) break;
2299:       dk = sctx.pv;

2301:       /* copy data into U(k,:) */
2302:       ba[bi[k]] = 1.0/dk; /* U(k,k) */
2303:       jmin      = bi[k]+1; jmax = bi[k+1];
2304:       if (jmin < jmax) {
2305:         for (j=jmin; j<jmax; j++) {
2306:           col = bj[j]; ba[j] = rtmp[col];
2307:         }
2308:         /* add the k-th row into il and jl */
2309:         il[k] = jmin;
2310:         i     = bj[jmin]; jl[k] = jl[i]; jl[i] = k;
2311:       }
2312:     }
2313:   } while (sctx.newshift);

2315:   PetscFree3(rtmp,il,jl);
2316:   ISRestoreIndices(ip,&rip);
2317:   ISRestoreIndices(iip,&riip);

2319:   ISIdentity(ip,&perm_identity);
2320:   if (perm_identity) {
2321:     B->ops->solve          = MatSolve_SeqSBAIJ_1_NaturalOrdering_inplace;
2322:     B->ops->solvetranspose = MatSolve_SeqSBAIJ_1_NaturalOrdering_inplace;
2323:     B->ops->forwardsolve   = MatForwardSolve_SeqSBAIJ_1_NaturalOrdering_inplace;
2324:     B->ops->backwardsolve  = MatBackwardSolve_SeqSBAIJ_1_NaturalOrdering_inplace;
2325:   } else {
2326:     B->ops->solve          = MatSolve_SeqSBAIJ_1_inplace;
2327:     B->ops->solvetranspose = MatSolve_SeqSBAIJ_1_inplace;
2328:     B->ops->forwardsolve   = MatForwardSolve_SeqSBAIJ_1_inplace;
2329:     B->ops->backwardsolve  = MatBackwardSolve_SeqSBAIJ_1_inplace;
2330:   }

2332:   C->assembled    = PETSC_TRUE;
2333:   C->preallocated = PETSC_TRUE;

2335:   PetscLogFlops(C->rmap->n);
2336:   if (sctx.nshift) {
2337:     if (info->shifttype == (PetscReal)MAT_SHIFT_NONZERO) {
2338:       PetscInfo2(A,"number of shiftnz tries %D, shift_amount %g\n",sctx.nshift,(double)sctx.shift_amount);
2339:     } else if (info->shifttype == (PetscReal)MAT_SHIFT_POSITIVE_DEFINITE) {
2340:       PetscInfo2(A,"number of shiftpd tries %D, shift_amount %g\n",sctx.nshift,(double)sctx.shift_amount);
2341:     }
2342:   }
2343:   return(0);
2344: }

2346: /*
2347:    icc() under revised new data structure.
2348:    Factored arrays bj and ba are stored as
2349:      U(0,:),...,U(i,:),U(n-1,:)

2351:    ui=fact->i is an array of size n+1, in which
2352:    ui+
2353:      ui[i]:  points to 1st entry of U(i,:),i=0,...,n-1
2354:      ui[n]:  points to U(n-1,n-1)+1

2356:   udiag=fact->diag is an array of size n,in which
2357:      udiag[i]: points to diagonal of U(i,:), i=0,...,n-1

2359:    U(i,:) contains udiag[i] as its last entry, i.e.,
2360:     U(i,:) = (u[i,i+1],...,u[i,n-1],diag[i])
2361: */

2363: PetscErrorCode MatICCFactorSymbolic_SeqAIJ(Mat fact,Mat A,IS perm,const MatFactorInfo *info)
2364: {
2365:   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
2366:   Mat_SeqSBAIJ       *b;
2367:   PetscErrorCode     ierr;
2368:   PetscBool          perm_identity,missing;
2369:   PetscInt           reallocs=0,i,*ai=a->i,*aj=a->j,am=A->rmap->n,*ui,*udiag;
2370:   const PetscInt     *rip,*riip;
2371:   PetscInt           jmin,jmax,nzk,k,j,*jl,prow,*il,nextprow;
2372:   PetscInt           nlnk,*lnk,*lnk_lvl=NULL,d;
2373:   PetscInt           ncols,ncols_upper,*cols,*ajtmp,*uj,**uj_ptr,**uj_lvl_ptr;
2374:   PetscReal          fill          =info->fill,levels=info->levels;
2375:   PetscFreeSpaceList free_space    =NULL,current_space=NULL;
2376:   PetscFreeSpaceList free_space_lvl=NULL,current_space_lvl=NULL;
2377:   PetscBT            lnkbt;
2378:   IS                 iperm;

2381:   if (A->rmap->n != A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Must be square matrix, rows %D columns %D",A->rmap->n,A->cmap->n);
2382:   MatMissingDiagonal(A,&missing,&d);
2383:   if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",d);
2384:   ISIdentity(perm,&perm_identity);
2385:   ISInvertPermutation(perm,PETSC_DECIDE,&iperm);

2387:   PetscMalloc1(am+1,&ui);
2388:   PetscMalloc1(am+1,&udiag);
2389:   ui[0] = 0;

2391:   /* ICC(0) without matrix ordering: simply rearrange column indices */
2392:   if (!levels && perm_identity) {
2393:     for (i=0; i<am; i++) {
2394:       ncols    = ai[i+1] - a->diag[i];
2395:       ui[i+1]  = ui[i] + ncols;
2396:       udiag[i] = ui[i+1] - 1; /* points to the last entry of U(i,:) */
2397:     }
2398:     PetscMalloc1(ui[am]+1,&uj);
2399:     cols = uj;
2400:     for (i=0; i<am; i++) {
2401:       aj    = a->j + a->diag[i] + 1; /* 1st entry of U(i,:) without diagonal */
2402:       ncols = ai[i+1] - a->diag[i] -1;
2403:       for (j=0; j<ncols; j++) *cols++ = aj[j];
2404:       *cols++ = i; /* diagoanl is located as the last entry of U(i,:) */
2405:     }
2406:   } else { /* case: levels>0 || (levels=0 && !perm_identity) */
2407:     ISGetIndices(iperm,&riip);
2408:     ISGetIndices(perm,&rip);

2410:     /* initialization */
2411:     PetscMalloc1(am+1,&ajtmp);

2413:     /* jl: linked list for storing indices of the pivot rows
2414:        il: il[i] points to the 1st nonzero entry of U(i,k:am-1) */
2415:     PetscMalloc4(am,&uj_ptr,am,&uj_lvl_ptr,am,&jl,am,&il);
2416:     for (i=0; i<am; i++) {
2417:       jl[i] = am; il[i] = 0;
2418:     }

2420:     /* create and initialize a linked list for storing column indices of the active row k */
2421:     nlnk = am + 1;
2422:     PetscIncompleteLLCreate(am,am,nlnk,lnk,lnk_lvl,lnkbt);

2424:     /* initial FreeSpace size is fill*(ai[am]+am)/2 */
2425:     PetscFreeSpaceGet(PetscRealIntMultTruncate(fill,(ai[am]+am)/2),&free_space);
2426:     current_space     = free_space;
2427:     PetscFreeSpaceGet(PetscRealIntMultTruncate(fill,(ai[am]+am)/2),&free_space_lvl);
2428:     current_space_lvl = free_space_lvl;

2430:     for (k=0; k<am; k++) {  /* for each active row k */
2431:       /* initialize lnk by the column indices of row rip[k] of A */
2432:       nzk   = 0;
2433:       ncols = ai[rip[k]+1] - ai[rip[k]];
2434:       if (!ncols) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_MAT_CH_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",rip[k],k);
2435:       ncols_upper = 0;
2436:       for (j=0; j<ncols; j++) {
2437:         i = *(aj + ai[rip[k]] + j); /* unpermuted column index */
2438:         if (riip[i] >= k) { /* only take upper triangular entry */
2439:           ajtmp[ncols_upper] = i;
2440:           ncols_upper++;
2441:         }
2442:       }
2443:       PetscIncompleteLLInit(ncols_upper,ajtmp,am,riip,nlnk,lnk,lnk_lvl,lnkbt);
2444:       nzk += nlnk;

2446:       /* update lnk by computing fill-in for each pivot row to be merged in */
2447:       prow = jl[k]; /* 1st pivot row */

2449:       while (prow < k) {
2450:         nextprow = jl[prow];

2452:         /* merge prow into k-th row */
2453:         jmin  = il[prow] + 1; /* index of the 2nd nzero entry in U(prow,k:am-1) */
2454:         jmax  = ui[prow+1];
2455:         ncols = jmax-jmin;
2456:         i     = jmin - ui[prow];
2457:         cols  = uj_ptr[prow] + i; /* points to the 2nd nzero entry in U(prow,k:am-1) */
2458:         uj    = uj_lvl_ptr[prow] + i; /* levels of cols */
2459:         j     = *(uj - 1);
2460:         PetscICCLLAddSorted(ncols,cols,levels,uj,am,nlnk,lnk,lnk_lvl,lnkbt,j);
2461:         nzk  += nlnk;

2463:         /* update il and jl for prow */
2464:         if (jmin < jmax) {
2465:           il[prow] = jmin;
2466:           j        = *cols; jl[prow] = jl[j]; jl[j] = prow;
2467:         }
2468:         prow = nextprow;
2469:       }

2471:       /* if free space is not available, make more free space */
2472:       if (current_space->local_remaining<nzk) {
2473:         i    = am - k + 1; /* num of unfactored rows */
2474:         i    = PetscIntMultTruncate(i,PetscMin(nzk, i-1)); /* i*nzk, i*(i-1): estimated and max additional space needed */
2475:         PetscFreeSpaceGet(i,&current_space);
2476:         PetscFreeSpaceGet(i,&current_space_lvl);
2477:         reallocs++;
2478:       }

2480:       /* copy data into free_space and free_space_lvl, then initialize lnk */
2481:       if (nzk == 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Empty row %D in ICC matrix factor",k);
2482:       PetscIncompleteLLClean(am,am,nzk,lnk,lnk_lvl,current_space->array,current_space_lvl->array,lnkbt);

2484:       /* add the k-th row into il and jl */
2485:       if (nzk > 1) {
2486:         i     = current_space->array[1]; /* col value of the first nonzero element in U(k, k+1:am-1) */
2487:         jl[k] = jl[i]; jl[i] = k;
2488:         il[k] = ui[k] + 1;
2489:       }
2490:       uj_ptr[k]     = current_space->array;
2491:       uj_lvl_ptr[k] = current_space_lvl->array;

2493:       current_space->array           += nzk;
2494:       current_space->local_used      += nzk;
2495:       current_space->local_remaining -= nzk;

2497:       current_space_lvl->array           += nzk;
2498:       current_space_lvl->local_used      += nzk;
2499:       current_space_lvl->local_remaining -= nzk;

2501:       ui[k+1] = ui[k] + nzk;
2502:     }

2504:     ISRestoreIndices(perm,&rip);
2505:     ISRestoreIndices(iperm,&riip);
2506:     PetscFree4(uj_ptr,uj_lvl_ptr,jl,il);
2507:     PetscFree(ajtmp);

2509:     /* copy free_space into uj and free free_space; set ui, uj, udiag in new datastructure; */
2510:     PetscMalloc1(ui[am]+1,&uj);
2511:     PetscFreeSpaceContiguous_Cholesky(&free_space,uj,am,ui,udiag); /* store matrix factor  */
2512:     PetscIncompleteLLDestroy(lnk,lnkbt);
2513:     PetscFreeSpaceDestroy(free_space_lvl);

2515:   } /* end of case: levels>0 || (levels=0 && !perm_identity) */

2517:   /* put together the new matrix in MATSEQSBAIJ format */
2518:   b               = (Mat_SeqSBAIJ*)(fact)->data;
2519:   b->singlemalloc = PETSC_FALSE;

2521:   PetscMalloc1(ui[am]+1,&b->a);

2523:   b->j             = uj;
2524:   b->i             = ui;
2525:   b->diag          = udiag;
2526:   b->free_diag     = PETSC_TRUE;
2527:   b->ilen          = 0;
2528:   b->imax          = 0;
2529:   b->row           = perm;
2530:   b->col           = perm;
2531:   PetscObjectReference((PetscObject)perm);
2532:   PetscObjectReference((PetscObject)perm);
2533:   b->icol          = iperm;
2534:   b->pivotinblocks = PETSC_FALSE; /* need to get from MatFactorInfo */

2536:   PetscMalloc1(am+1,&b->solve_work);
2537:   PetscLogObjectMemory((PetscObject)fact,ui[am]*(sizeof(PetscInt)+sizeof(MatScalar)));

2539:   b->maxnz   = b->nz = ui[am];
2540:   b->free_a  = PETSC_TRUE;
2541:   b->free_ij = PETSC_TRUE;

2543:   fact->info.factor_mallocs   = reallocs;
2544:   fact->info.fill_ratio_given = fill;
2545:   if (ai[am] != 0) {
2546:     /* nonzeros in lower triangular part of A (including diagonals) = (ai[am]+am)/2 */
2547:     fact->info.fill_ratio_needed = ((PetscReal)2*ui[am])/(ai[am]+am);
2548:   } else {
2549:     fact->info.fill_ratio_needed = 0.0;
2550:   }
2551: #if defined(PETSC_USE_INFO)
2552:   if (ai[am] != 0) {
2553:     PetscReal af = fact->info.fill_ratio_needed;
2554:     PetscInfo3(A,"Reallocs %D Fill ratio:given %g needed %g\n",reallocs,(double)fill,(double)af);
2555:     PetscInfo1(A,"Run with -pc_factor_fill %g or use \n",(double)af);
2556:     PetscInfo1(A,"PCFactorSetFill(pc,%g) for best performance.\n",(double)af);
2557:   } else {
2558:     PetscInfo(A,"Empty matrix.\n");
2559:   }
2560: #endif
2561:   fact->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJ;
2562:   return(0);
2563: }

2565: PetscErrorCode MatICCFactorSymbolic_SeqAIJ_inplace(Mat fact,Mat A,IS perm,const MatFactorInfo *info)
2566: {
2567:   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
2568:   Mat_SeqSBAIJ       *b;
2569:   PetscErrorCode     ierr;
2570:   PetscBool          perm_identity,missing;
2571:   PetscInt           reallocs=0,i,*ai=a->i,*aj=a->j,am=A->rmap->n,*ui,*udiag;
2572:   const PetscInt     *rip,*riip;
2573:   PetscInt           jmin,jmax,nzk,k,j,*jl,prow,*il,nextprow;
2574:   PetscInt           nlnk,*lnk,*lnk_lvl=NULL,d;
2575:   PetscInt           ncols,ncols_upper,*cols,*ajtmp,*uj,**uj_ptr,**uj_lvl_ptr;
2576:   PetscReal          fill          =info->fill,levels=info->levels;
2577:   PetscFreeSpaceList free_space    =NULL,current_space=NULL;
2578:   PetscFreeSpaceList free_space_lvl=NULL,current_space_lvl=NULL;
2579:   PetscBT            lnkbt;
2580:   IS                 iperm;

2583:   if (A->rmap->n != A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Must be square matrix, rows %D columns %D",A->rmap->n,A->cmap->n);
2584:   MatMissingDiagonal(A,&missing,&d);
2585:   if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",d);
2586:   ISIdentity(perm,&perm_identity);
2587:   ISInvertPermutation(perm,PETSC_DECIDE,&iperm);

2589:   PetscMalloc1(am+1,&ui);
2590:   PetscMalloc1(am+1,&udiag);
2591:   ui[0] = 0;

2593:   /* ICC(0) without matrix ordering: simply copies fill pattern */
2594:   if (!levels && perm_identity) {

2596:     for (i=0; i<am; i++) {
2597:       ui[i+1]  = ui[i] + ai[i+1] - a->diag[i];
2598:       udiag[i] = ui[i];
2599:     }
2600:     PetscMalloc1(ui[am]+1,&uj);
2601:     cols = uj;
2602:     for (i=0; i<am; i++) {
2603:       aj    = a->j + a->diag[i];
2604:       ncols = ui[i+1] - ui[i];
2605:       for (j=0; j<ncols; j++) *cols++ = *aj++;
2606:     }
2607:   } else { /* case: levels>0 || (levels=0 && !perm_identity) */
2608:     ISGetIndices(iperm,&riip);
2609:     ISGetIndices(perm,&rip);

2611:     /* initialization */
2612:     PetscMalloc1(am+1,&ajtmp);

2614:     /* jl: linked list for storing indices of the pivot rows
2615:        il: il[i] points to the 1st nonzero entry of U(i,k:am-1) */
2616:     PetscMalloc4(am,&uj_ptr,am,&uj_lvl_ptr,am,&jl,am,&il);
2617:     for (i=0; i<am; i++) {
2618:       jl[i] = am; il[i] = 0;
2619:     }

2621:     /* create and initialize a linked list for storing column indices of the active row k */
2622:     nlnk = am + 1;
2623:     PetscIncompleteLLCreate(am,am,nlnk,lnk,lnk_lvl,lnkbt);

2625:     /* initial FreeSpace size is fill*(ai[am]+1) */
2626:     PetscFreeSpaceGet(PetscRealIntMultTruncate(fill,ai[am]+1),&free_space);
2627:     current_space     = free_space;
2628:     PetscFreeSpaceGet(PetscRealIntMultTruncate(fill,ai[am]+1),&free_space_lvl);
2629:     current_space_lvl = free_space_lvl;

2631:     for (k=0; k<am; k++) {  /* for each active row k */
2632:       /* initialize lnk by the column indices of row rip[k] of A */
2633:       nzk   = 0;
2634:       ncols = ai[rip[k]+1] - ai[rip[k]];
2635:       if (!ncols) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_MAT_CH_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",rip[k],k);
2636:       ncols_upper = 0;
2637:       for (j=0; j<ncols; j++) {
2638:         i = *(aj + ai[rip[k]] + j); /* unpermuted column index */
2639:         if (riip[i] >= k) { /* only take upper triangular entry */
2640:           ajtmp[ncols_upper] = i;
2641:           ncols_upper++;
2642:         }
2643:       }
2644:       PetscIncompleteLLInit(ncols_upper,ajtmp,am,riip,nlnk,lnk,lnk_lvl,lnkbt);
2645:       nzk += nlnk;

2647:       /* update lnk by computing fill-in for each pivot row to be merged in */
2648:       prow = jl[k]; /* 1st pivot row */

2650:       while (prow < k) {
2651:         nextprow = jl[prow];

2653:         /* merge prow into k-th row */
2654:         jmin  = il[prow] + 1; /* index of the 2nd nzero entry in U(prow,k:am-1) */
2655:         jmax  = ui[prow+1];
2656:         ncols = jmax-jmin;
2657:         i     = jmin - ui[prow];
2658:         cols  = uj_ptr[prow] + i; /* points to the 2nd nzero entry in U(prow,k:am-1) */
2659:         uj    = uj_lvl_ptr[prow] + i; /* levels of cols */
2660:         j     = *(uj - 1);
2661:         PetscICCLLAddSorted(ncols,cols,levels,uj,am,nlnk,lnk,lnk_lvl,lnkbt,j);
2662:         nzk  += nlnk;

2664:         /* update il and jl for prow */
2665:         if (jmin < jmax) {
2666:           il[prow] = jmin;
2667:           j        = *cols; jl[prow] = jl[j]; jl[j] = prow;
2668:         }
2669:         prow = nextprow;
2670:       }

2672:       /* if free space is not available, make more free space */
2673:       if (current_space->local_remaining<nzk) {
2674:         i    = am - k + 1; /* num of unfactored rows */
2675:         i    = PetscIntMultTruncate(i,PetscMin(nzk, i-1)); /* i*nzk, i*(i-1): estimated and max additional space needed */
2676:         PetscFreeSpaceGet(i,&current_space);
2677:         PetscFreeSpaceGet(i,&current_space_lvl);
2678:         reallocs++;
2679:       }

2681:       /* copy data into free_space and free_space_lvl, then initialize lnk */
2682:       if (!nzk) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Empty row %D in ICC matrix factor",k);
2683:       PetscIncompleteLLClean(am,am,nzk,lnk,lnk_lvl,current_space->array,current_space_lvl->array,lnkbt);

2685:       /* add the k-th row into il and jl */
2686:       if (nzk > 1) {
2687:         i     = current_space->array[1]; /* col value of the first nonzero element in U(k, k+1:am-1) */
2688:         jl[k] = jl[i]; jl[i] = k;
2689:         il[k] = ui[k] + 1;
2690:       }
2691:       uj_ptr[k]     = current_space->array;
2692:       uj_lvl_ptr[k] = current_space_lvl->array;

2694:       current_space->array           += nzk;
2695:       current_space->local_used      += nzk;
2696:       current_space->local_remaining -= nzk;

2698:       current_space_lvl->array           += nzk;
2699:       current_space_lvl->local_used      += nzk;
2700:       current_space_lvl->local_remaining -= nzk;

2702:       ui[k+1] = ui[k] + nzk;
2703:     }

2705: #if defined(PETSC_USE_INFO)
2706:     if (ai[am] != 0) {
2707:       PetscReal af = (PetscReal)ui[am]/((PetscReal)ai[am]);
2708:       PetscInfo3(A,"Reallocs %D Fill ratio:given %g needed %g\n",reallocs,(double)fill,(double)af);
2709:       PetscInfo1(A,"Run with -pc_factor_fill %g or use \n",(double)af);
2710:       PetscInfo1(A,"PCFactorSetFill(pc,%g) for best performance.\n",(double)af);
2711:     } else {
2712:       PetscInfo(A,"Empty matrix.\n");
2713:     }
2714: #endif

2716:     ISRestoreIndices(perm,&rip);
2717:     ISRestoreIndices(iperm,&riip);
2718:     PetscFree4(uj_ptr,uj_lvl_ptr,jl,il);
2719:     PetscFree(ajtmp);

2721:     /* destroy list of free space and other temporary array(s) */
2722:     PetscMalloc1(ui[am]+1,&uj);
2723:     PetscFreeSpaceContiguous(&free_space,uj);
2724:     PetscIncompleteLLDestroy(lnk,lnkbt);
2725:     PetscFreeSpaceDestroy(free_space_lvl);

2727:   } /* end of case: levels>0 || (levels=0 && !perm_identity) */

2729:   /* put together the new matrix in MATSEQSBAIJ format */

2731:   b               = (Mat_SeqSBAIJ*)fact->data;
2732:   b->singlemalloc = PETSC_FALSE;

2734:   PetscMalloc1(ui[am]+1,&b->a);

2736:   b->j         = uj;
2737:   b->i         = ui;
2738:   b->diag      = udiag;
2739:   b->free_diag = PETSC_TRUE;
2740:   b->ilen      = 0;
2741:   b->imax      = 0;
2742:   b->row       = perm;
2743:   b->col       = perm;

2745:   PetscObjectReference((PetscObject)perm);
2746:   PetscObjectReference((PetscObject)perm);

2748:   b->icol          = iperm;
2749:   b->pivotinblocks = PETSC_FALSE; /* need to get from MatFactorInfo */
2750:   PetscMalloc1(am+1,&b->solve_work);
2751:   PetscLogObjectMemory((PetscObject)fact,(ui[am]-am)*(sizeof(PetscInt)+sizeof(MatScalar)));
2752:   b->maxnz         = b->nz = ui[am];
2753:   b->free_a        = PETSC_TRUE;
2754:   b->free_ij       = PETSC_TRUE;

2756:   fact->info.factor_mallocs   = reallocs;
2757:   fact->info.fill_ratio_given = fill;
2758:   if (ai[am] != 0) {
2759:     fact->info.fill_ratio_needed = ((PetscReal)ui[am])/((PetscReal)ai[am]);
2760:   } else {
2761:     fact->info.fill_ratio_needed = 0.0;
2762:   }
2763:   fact->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJ_inplace;
2764:   return(0);
2765: }

2767: PetscErrorCode MatCholeskyFactorSymbolic_SeqAIJ(Mat fact,Mat A,IS perm,const MatFactorInfo *info)
2768: {
2769:   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
2770:   Mat_SeqSBAIJ       *b;
2771:   PetscErrorCode     ierr;
2772:   PetscBool          perm_identity,missing;
2773:   PetscReal          fill = info->fill;
2774:   const PetscInt     *rip,*riip;
2775:   PetscInt           i,am=A->rmap->n,*ai=a->i,*aj=a->j,reallocs=0,prow;
2776:   PetscInt           *jl,jmin,jmax,nzk,*ui,k,j,*il,nextprow;
2777:   PetscInt           nlnk,*lnk,ncols,ncols_upper,*cols,*uj,**ui_ptr,*uj_ptr,*udiag;
2778:   PetscFreeSpaceList free_space=NULL,current_space=NULL;
2779:   PetscBT            lnkbt;
2780:   IS                 iperm;

2783:   if (A->rmap->n != A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Must be square matrix, rows %D columns %D",A->rmap->n,A->cmap->n);
2784:   MatMissingDiagonal(A,&missing,&i);
2785:   if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",i);

2787:   /* check whether perm is the identity mapping */
2788:   ISIdentity(perm,&perm_identity);
2789:   ISInvertPermutation(perm,PETSC_DECIDE,&iperm);
2790:   ISGetIndices(iperm,&riip);
2791:   ISGetIndices(perm,&rip);

2793:   /* initialization */
2794:   PetscMalloc1(am+1,&ui);
2795:   PetscMalloc1(am+1,&udiag);
2796:   ui[0] = 0;

2798:   /* jl: linked list for storing indices of the pivot rows
2799:      il: il[i] points to the 1st nonzero entry of U(i,k:am-1) */
2800:   PetscMalloc4(am,&ui_ptr,am,&jl,am,&il,am,&cols);
2801:   for (i=0; i<am; i++) {
2802:     jl[i] = am; il[i] = 0;
2803:   }

2805:   /* create and initialize a linked list for storing column indices of the active row k */
2806:   nlnk = am + 1;
2807:   PetscLLCreate(am,am,nlnk,lnk,lnkbt);

2809:   /* initial FreeSpace size is fill*(ai[am]+am)/2 */
2810:   PetscFreeSpaceGet(PetscRealIntMultTruncate(fill,(ai[am]+am)/2),&free_space);
2811:   current_space = free_space;

2813:   for (k=0; k<am; k++) {  /* for each active row k */
2814:     /* initialize lnk by the column indices of row rip[k] of A */
2815:     nzk   = 0;
2816:     ncols = ai[rip[k]+1] - ai[rip[k]];
2817:     if (!ncols) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_MAT_CH_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",rip[k],k);
2818:     ncols_upper = 0;
2819:     for (j=0; j<ncols; j++) {
2820:       i = riip[*(aj + ai[rip[k]] + j)];
2821:       if (i >= k) { /* only take upper triangular entry */
2822:         cols[ncols_upper] = i;
2823:         ncols_upper++;
2824:       }
2825:     }
2826:     PetscLLAdd(ncols_upper,cols,am,nlnk,lnk,lnkbt);
2827:     nzk += nlnk;

2829:     /* update lnk by computing fill-in for each pivot row to be merged in */
2830:     prow = jl[k]; /* 1st pivot row */

2832:     while (prow < k) {
2833:       nextprow = jl[prow];
2834:       /* merge prow into k-th row */
2835:       jmin   = il[prow] + 1; /* index of the 2nd nzero entry in U(prow,k:am-1) */
2836:       jmax   = ui[prow+1];
2837:       ncols  = jmax-jmin;
2838:       uj_ptr = ui_ptr[prow] + jmin - ui[prow]; /* points to the 2nd nzero entry in U(prow,k:am-1) */
2839:       PetscLLAddSorted(ncols,uj_ptr,am,nlnk,lnk,lnkbt);
2840:       nzk   += nlnk;

2842:       /* update il and jl for prow */
2843:       if (jmin < jmax) {
2844:         il[prow] = jmin;
2845:         j        = *uj_ptr;
2846:         jl[prow] = jl[j];
2847:         jl[j]    = prow;
2848:       }
2849:       prow = nextprow;
2850:     }

2852:     /* if free space is not available, make more free space */
2853:     if (current_space->local_remaining<nzk) {
2854:       i    = am - k + 1; /* num of unfactored rows */
2855:       i    = PetscIntMultTruncate(i,PetscMin(nzk,i-1)); /* i*nzk, i*(i-1): estimated and max additional space needed */
2856:       PetscFreeSpaceGet(i,&current_space);
2857:       reallocs++;
2858:     }

2860:     /* copy data into free space, then initialize lnk */
2861:     PetscLLClean(am,am,nzk,lnk,current_space->array,lnkbt);

2863:     /* add the k-th row into il and jl */
2864:     if (nzk > 1) {
2865:       i     = current_space->array[1]; /* col value of the first nonzero element in U(k, k+1:am-1) */
2866:       jl[k] = jl[i]; jl[i] = k;
2867:       il[k] = ui[k] + 1;
2868:     }
2869:     ui_ptr[k] = current_space->array;

2871:     current_space->array           += nzk;
2872:     current_space->local_used      += nzk;
2873:     current_space->local_remaining -= nzk;

2875:     ui[k+1] = ui[k] + nzk;
2876:   }

2878:   ISRestoreIndices(perm,&rip);
2879:   ISRestoreIndices(iperm,&riip);
2880:   PetscFree4(ui_ptr,jl,il,cols);

2882:   /* copy free_space into uj and free free_space; set ui, uj, udiag in new datastructure; */
2883:   PetscMalloc1(ui[am]+1,&uj);
2884:   PetscFreeSpaceContiguous_Cholesky(&free_space,uj,am,ui,udiag); /* store matrix factor */
2885:   PetscLLDestroy(lnk,lnkbt);

2887:   /* put together the new matrix in MATSEQSBAIJ format */

2889:   b               = (Mat_SeqSBAIJ*)fact->data;
2890:   b->singlemalloc = PETSC_FALSE;
2891:   b->free_a       = PETSC_TRUE;
2892:   b->free_ij      = PETSC_TRUE;

2894:   PetscMalloc1(ui[am]+1,&b->a);

2896:   b->j         = uj;
2897:   b->i         = ui;
2898:   b->diag      = udiag;
2899:   b->free_diag = PETSC_TRUE;
2900:   b->ilen      = 0;
2901:   b->imax      = 0;
2902:   b->row       = perm;
2903:   b->col       = perm;

2905:   PetscObjectReference((PetscObject)perm);
2906:   PetscObjectReference((PetscObject)perm);

2908:   b->icol          = iperm;
2909:   b->pivotinblocks = PETSC_FALSE; /* need to get from MatFactorInfo */

2911:   PetscMalloc1(am+1,&b->solve_work);
2912:   PetscLogObjectMemory((PetscObject)fact,ui[am]*(sizeof(PetscInt)+sizeof(MatScalar)));

2914:   b->maxnz = b->nz = ui[am];

2916:   fact->info.factor_mallocs   = reallocs;
2917:   fact->info.fill_ratio_given = fill;
2918:   if (ai[am] != 0) {
2919:     /* nonzeros in lower triangular part of A (including diagonals) = (ai[am]+am)/2 */
2920:     fact->info.fill_ratio_needed = ((PetscReal)2*ui[am])/(ai[am]+am);
2921:   } else {
2922:     fact->info.fill_ratio_needed = 0.0;
2923:   }
2924: #if defined(PETSC_USE_INFO)
2925:   if (ai[am] != 0) {
2926:     PetscReal af = fact->info.fill_ratio_needed;
2927:     PetscInfo3(A,"Reallocs %D Fill ratio:given %g needed %g\n",reallocs,(double)fill,(double)af);
2928:     PetscInfo1(A,"Run with -pc_factor_fill %g or use \n",(double)af);
2929:     PetscInfo1(A,"PCFactorSetFill(pc,%g) for best performance.\n",(double)af);
2930:   } else {
2931:     PetscInfo(A,"Empty matrix.\n");
2932:   }
2933: #endif
2934:   fact->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJ;
2935:   return(0);
2936: }

2938: PetscErrorCode MatCholeskyFactorSymbolic_SeqAIJ_inplace(Mat fact,Mat A,IS perm,const MatFactorInfo *info)
2939: {
2940:   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
2941:   Mat_SeqSBAIJ       *b;
2942:   PetscErrorCode     ierr;
2943:   PetscBool          perm_identity,missing;
2944:   PetscReal          fill = info->fill;
2945:   const PetscInt     *rip,*riip;
2946:   PetscInt           i,am=A->rmap->n,*ai=a->i,*aj=a->j,reallocs=0,prow;
2947:   PetscInt           *jl,jmin,jmax,nzk,*ui,k,j,*il,nextprow;
2948:   PetscInt           nlnk,*lnk,ncols,ncols_upper,*cols,*uj,**ui_ptr,*uj_ptr;
2949:   PetscFreeSpaceList free_space=NULL,current_space=NULL;
2950:   PetscBT            lnkbt;
2951:   IS                 iperm;

2954:   if (A->rmap->n != A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Must be square matrix, rows %D columns %D",A->rmap->n,A->cmap->n);
2955:   MatMissingDiagonal(A,&missing,&i);
2956:   if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",i);

2958:   /* check whether perm is the identity mapping */
2959:   ISIdentity(perm,&perm_identity);
2960:   ISInvertPermutation(perm,PETSC_DECIDE,&iperm);
2961:   ISGetIndices(iperm,&riip);
2962:   ISGetIndices(perm,&rip);

2964:   /* initialization */
2965:   PetscMalloc1(am+1,&ui);
2966:   ui[0] = 0;

2968:   /* jl: linked list for storing indices of the pivot rows
2969:      il: il[i] points to the 1st nonzero entry of U(i,k:am-1) */
2970:   PetscMalloc4(am,&ui_ptr,am,&jl,am,&il,am,&cols);
2971:   for (i=0; i<am; i++) {
2972:     jl[i] = am; il[i] = 0;
2973:   }

2975:   /* create and initialize a linked list for storing column indices of the active row k */
2976:   nlnk = am + 1;
2977:   PetscLLCreate(am,am,nlnk,lnk,lnkbt);

2979:   /* initial FreeSpace size is fill*(ai[am]+1) */
2980:   PetscFreeSpaceGet(PetscRealIntMultTruncate(fill,ai[am]+1),&free_space);
2981:   current_space = free_space;

2983:   for (k=0; k<am; k++) {  /* for each active row k */
2984:     /* initialize lnk by the column indices of row rip[k] of A */
2985:     nzk   = 0;
2986:     ncols = ai[rip[k]+1] - ai[rip[k]];
2987:     if (!ncols) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_MAT_CH_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",rip[k],k);
2988:     ncols_upper = 0;
2989:     for (j=0; j<ncols; j++) {
2990:       i = riip[*(aj + ai[rip[k]] + j)];
2991:       if (i >= k) { /* only take upper triangular entry */
2992:         cols[ncols_upper] = i;
2993:         ncols_upper++;
2994:       }
2995:     }
2996:     PetscLLAdd(ncols_upper,cols,am,nlnk,lnk,lnkbt);
2997:     nzk += nlnk;

2999:     /* update lnk by computing fill-in for each pivot row to be merged in */
3000:     prow = jl[k]; /* 1st pivot row */

3002:     while (prow < k) {
3003:       nextprow = jl[prow];
3004:       /* merge prow into k-th row */
3005:       jmin   = il[prow] + 1; /* index of the 2nd nzero entry in U(prow,k:am-1) */
3006:       jmax   = ui[prow+1];
3007:       ncols  = jmax-jmin;
3008:       uj_ptr = ui_ptr[prow] + jmin - ui[prow]; /* points to the 2nd nzero entry in U(prow,k:am-1) */
3009:       PetscLLAddSorted(ncols,uj_ptr,am,nlnk,lnk,lnkbt);
3010:       nzk   += nlnk;

3012:       /* update il and jl for prow */
3013:       if (jmin < jmax) {
3014:         il[prow] = jmin;
3015:         j        = *uj_ptr; jl[prow] = jl[j]; jl[j] = prow;
3016:       }
3017:       prow = nextprow;
3018:     }

3020:     /* if free space is not available, make more free space */
3021:     if (current_space->local_remaining<nzk) {
3022:       i    = am - k + 1; /* num of unfactored rows */
3023:       i    = PetscMin(i*nzk, i*(i-1)); /* i*nzk, i*(i-1): estimated and max additional space needed */
3024:       PetscFreeSpaceGet(i,&current_space);
3025:       reallocs++;
3026:     }

3028:     /* copy data into free space, then initialize lnk */
3029:     PetscLLClean(am,am,nzk,lnk,current_space->array,lnkbt);

3031:     /* add the k-th row into il and jl */
3032:     if (nzk-1 > 0) {
3033:       i     = current_space->array[1]; /* col value of the first nonzero element in U(k, k+1:am-1) */
3034:       jl[k] = jl[i]; jl[i] = k;
3035:       il[k] = ui[k] + 1;
3036:     }
3037:     ui_ptr[k] = current_space->array;

3039:     current_space->array           += nzk;
3040:     current_space->local_used      += nzk;
3041:     current_space->local_remaining -= nzk;

3043:     ui[k+1] = ui[k] + nzk;
3044:   }

3046: #if defined(PETSC_USE_INFO)
3047:   if (ai[am] != 0) {
3048:     PetscReal af = (PetscReal)(ui[am])/((PetscReal)ai[am]);
3049:     PetscInfo3(A,"Reallocs %D Fill ratio:given %g needed %g\n",reallocs,(double)fill,(double)af);
3050:     PetscInfo1(A,"Run with -pc_factor_fill %g or use \n",(double)af);
3051:     PetscInfo1(A,"PCFactorSetFill(pc,%g) for best performance.\n",(double)af);
3052:   } else {
3053:     PetscInfo(A,"Empty matrix.\n");
3054:   }
3055: #endif

3057:   ISRestoreIndices(perm,&rip);
3058:   ISRestoreIndices(iperm,&riip);
3059:   PetscFree4(ui_ptr,jl,il,cols);

3061:   /* destroy list of free space and other temporary array(s) */
3062:   PetscMalloc1(ui[am]+1,&uj);
3063:   PetscFreeSpaceContiguous(&free_space,uj);
3064:   PetscLLDestroy(lnk,lnkbt);

3066:   /* put together the new matrix in MATSEQSBAIJ format */

3068:   b               = (Mat_SeqSBAIJ*)fact->data;
3069:   b->singlemalloc = PETSC_FALSE;
3070:   b->free_a       = PETSC_TRUE;
3071:   b->free_ij      = PETSC_TRUE;

3073:   PetscMalloc1(ui[am]+1,&b->a);

3075:   b->j    = uj;
3076:   b->i    = ui;
3077:   b->diag = 0;
3078:   b->ilen = 0;
3079:   b->imax = 0;
3080:   b->row  = perm;
3081:   b->col  = perm;

3083:   PetscObjectReference((PetscObject)perm);
3084:   PetscObjectReference((PetscObject)perm);

3086:   b->icol          = iperm;
3087:   b->pivotinblocks = PETSC_FALSE; /* need to get from MatFactorInfo */

3089:   PetscMalloc1(am+1,&b->solve_work);
3090:   PetscLogObjectMemory((PetscObject)fact,(ui[am]-am)*(sizeof(PetscInt)+sizeof(MatScalar)));
3091:   b->maxnz = b->nz = ui[am];

3093:   fact->info.factor_mallocs   = reallocs;
3094:   fact->info.fill_ratio_given = fill;
3095:   if (ai[am] != 0) {
3096:     fact->info.fill_ratio_needed = ((PetscReal)ui[am])/((PetscReal)ai[am]);
3097:   } else {
3098:     fact->info.fill_ratio_needed = 0.0;
3099:   }
3100:   fact->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJ_inplace;
3101:   return(0);
3102: }

3104: PetscErrorCode MatSolve_SeqAIJ_NaturalOrdering(Mat A,Vec bb,Vec xx)
3105: {
3106:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
3107:   PetscErrorCode    ierr;
3108:   PetscInt          n   = A->rmap->n;
3109:   const PetscInt    *ai = a->i,*aj = a->j,*adiag = a->diag,*vi;
3110:   PetscScalar       *x,sum;
3111:   const PetscScalar *b;
3112:   const MatScalar   *aa = a->a,*v;
3113:   PetscInt          i,nz;

3116:   if (!n) return(0);

3118:   VecGetArrayRead(bb,&b);
3119:   VecGetArray(xx,&x);

3121:   /* forward solve the lower triangular */
3122:   x[0] = b[0];
3123:   v    = aa;
3124:   vi   = aj;
3125:   for (i=1; i<n; i++) {
3126:     nz  = ai[i+1] - ai[i];
3127:     sum = b[i];
3128:     PetscSparseDenseMinusDot(sum,x,v,vi,nz);
3129:     v   += nz;
3130:     vi  += nz;
3131:     x[i] = sum;
3132:   }

3134:   /* backward solve the upper triangular */
3135:   for (i=n-1; i>=0; i--) {
3136:     v   = aa + adiag[i+1] + 1;
3137:     vi  = aj + adiag[i+1] + 1;
3138:     nz  = adiag[i] - adiag[i+1]-1;
3139:     sum = x[i];
3140:     PetscSparseDenseMinusDot(sum,x,v,vi,nz);
3141:     x[i] = sum*v[nz]; /* x[i]=aa[adiag[i]]*sum; v++; */
3142:   }

3144:   PetscLogFlops(2.0*a->nz - A->cmap->n);
3145:   VecRestoreArrayRead(bb,&b);
3146:   VecRestoreArray(xx,&x);
3147:   return(0);
3148: }

3150: PetscErrorCode MatSolve_SeqAIJ(Mat A,Vec bb,Vec xx)
3151: {
3152:   Mat_SeqAIJ        *a    = (Mat_SeqAIJ*)A->data;
3153:   IS                iscol = a->col,isrow = a->row;
3154:   PetscErrorCode    ierr;
3155:   PetscInt          i,n=A->rmap->n,*vi,*ai=a->i,*aj=a->j,*adiag = a->diag,nz;
3156:   const PetscInt    *rout,*cout,*r,*c;
3157:   PetscScalar       *x,*tmp,sum;
3158:   const PetscScalar *b;
3159:   const MatScalar   *aa = a->a,*v;

3162:   if (!n) return(0);

3164:   VecGetArrayRead(bb,&b);
3165:   VecGetArray(xx,&x);
3166:   tmp  = a->solve_work;

3168:   ISGetIndices(isrow,&rout); r = rout;
3169:   ISGetIndices(iscol,&cout); c = cout;

3171:   /* forward solve the lower triangular */
3172:   tmp[0] = b[r[0]];
3173:   v      = aa;
3174:   vi     = aj;
3175:   for (i=1; i<n; i++) {
3176:     nz  = ai[i+1] - ai[i];
3177:     sum = b[r[i]];
3178:     PetscSparseDenseMinusDot(sum,tmp,v,vi,nz);
3179:     tmp[i] = sum;
3180:     v     += nz; vi += nz;
3181:   }

3183:   /* backward solve the upper triangular */
3184:   for (i=n-1; i>=0; i--) {
3185:     v   = aa + adiag[i+1]+1;
3186:     vi  = aj + adiag[i+1]+1;
3187:     nz  = adiag[i]-adiag[i+1]-1;
3188:     sum = tmp[i];
3189:     PetscSparseDenseMinusDot(sum,tmp,v,vi,nz);
3190:     x[c[i]] = tmp[i] = sum*v[nz]; /* v[nz] = aa[adiag[i]] */
3191:   }

3193:   ISRestoreIndices(isrow,&rout);
3194:   ISRestoreIndices(iscol,&cout);
3195:   VecRestoreArrayRead(bb,&b);
3196:   VecRestoreArray(xx,&x);
3197:   PetscLogFlops(2*a->nz - A->cmap->n);
3198:   return(0);
3199: }

3201: /*
3202:     This will get a new name and become a varient of MatILUFactor_SeqAIJ() there is no longer separate functions in the matrix function table for dt factors
3203: */
3204: PetscErrorCode MatILUDTFactor_SeqAIJ(Mat A,IS isrow,IS iscol,const MatFactorInfo *info,Mat *fact)
3205: {
3206:   Mat            B = *fact;
3207:   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data,*b;
3208:   IS             isicol;
3210:   const PetscInt *r,*ic;
3211:   PetscInt       i,n=A->rmap->n,*ai=a->i,*aj=a->j,*ajtmp,*adiag;
3212:   PetscInt       *bi,*bj,*bdiag,*bdiag_rev;
3213:   PetscInt       row,nzi,nzi_bl,nzi_bu,*im,nzi_al,nzi_au;
3214:   PetscInt       nlnk,*lnk;
3215:   PetscBT        lnkbt;
3216:   PetscBool      row_identity,icol_identity;
3217:   MatScalar      *aatmp,*pv,*batmp,*ba,*rtmp,*pc,multiplier,*vtmp,diag_tmp;
3218:   const PetscInt *ics;
3219:   PetscInt       j,nz,*pj,*bjtmp,k,ncut,*jtmp;
3220:   PetscReal      dt     =info->dt,shift=info->shiftamount;
3221:   PetscInt       dtcount=(PetscInt)info->dtcount,nnz_max;
3222:   PetscBool      missing;

3225:   if (dt      == PETSC_DEFAULT) dt = 0.005;
3226:   if (dtcount == PETSC_DEFAULT) dtcount = (PetscInt)(1.5*a->rmax);

3228:   /* ------- symbolic factorization, can be reused ---------*/
3229:   MatMissingDiagonal(A,&missing,&i);
3230:   if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",i);
3231:   adiag=a->diag;

3233:   ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);

3235:   /* bdiag is location of diagonal in factor */
3236:   PetscMalloc1(n+1,&bdiag);     /* becomes b->diag */
3237:   PetscMalloc1(n+1,&bdiag_rev); /* temporary */

3239:   /* allocate row pointers bi */
3240:   PetscMalloc1(2*n+2,&bi);

3242:   /* allocate bj and ba; max num of nonzero entries is (ai[n]+2*n*dtcount+2) */
3243:   if (dtcount > n-1) dtcount = n-1; /* diagonal is excluded */
3244:   nnz_max = ai[n]+2*n*dtcount+2;

3246:   PetscMalloc1(nnz_max+1,&bj);
3247:   PetscMalloc1(nnz_max+1,&ba);

3249:   /* put together the new matrix */
3250:   MatSeqAIJSetPreallocation_SeqAIJ(B,MAT_SKIP_ALLOCATION,NULL);
3251:   PetscLogObjectParent((PetscObject)B,(PetscObject)isicol);
3252:   b    = (Mat_SeqAIJ*)B->data;

3254:   b->free_a       = PETSC_TRUE;
3255:   b->free_ij      = PETSC_TRUE;
3256:   b->singlemalloc = PETSC_FALSE;

3258:   b->a    = ba;
3259:   b->j    = bj;
3260:   b->i    = bi;
3261:   b->diag = bdiag;
3262:   b->ilen = 0;
3263:   b->imax = 0;
3264:   b->row  = isrow;
3265:   b->col  = iscol;
3266:   PetscObjectReference((PetscObject)isrow);
3267:   PetscObjectReference((PetscObject)iscol);
3268:   b->icol = isicol;

3270:   PetscMalloc1(n+1,&b->solve_work);
3271:   PetscLogObjectMemory((PetscObject)B,nnz_max*(sizeof(PetscInt)+sizeof(MatScalar)));
3272:   b->maxnz = nnz_max;

3274:   B->factortype            = MAT_FACTOR_ILUDT;
3275:   B->info.factor_mallocs   = 0;
3276:   B->info.fill_ratio_given = ((PetscReal)nnz_max)/((PetscReal)ai[n]);
3277:   /* ------- end of symbolic factorization ---------*/

3279:   ISGetIndices(isrow,&r);
3280:   ISGetIndices(isicol,&ic);
3281:   ics  = ic;

3283:   /* linked list for storing column indices of the active row */
3284:   nlnk = n + 1;
3285:   PetscLLCreate(n,n,nlnk,lnk,lnkbt);

3287:   /* im: used by PetscLLAddSortedLU(); jtmp: working array for column indices of active row */
3288:   PetscMalloc2(n,&im,n,&jtmp);
3289:   /* rtmp, vtmp: working arrays for sparse and contiguous row entries of active row */
3290:   PetscMalloc2(n,&rtmp,n,&vtmp);
3291:   PetscMemzero(rtmp,n*sizeof(MatScalar));

3293:   bi[0]        = 0;
3294:   bdiag[0]     = nnz_max-1; /* location of diag[0] in factor B */
3295:   bdiag_rev[n] = bdiag[0];
3296:   bi[2*n+1]    = bdiag[0]+1; /* endof bj and ba array */
3297:   for (i=0; i<n; i++) {
3298:     /* copy initial fill into linked list */
3299:     nzi = ai[r[i]+1] - ai[r[i]];
3300:     if (!nzi) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_MAT_LU_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",r[i],i);
3301:     nzi_al = adiag[r[i]] - ai[r[i]];
3302:     nzi_au = ai[r[i]+1] - adiag[r[i]] -1;
3303:     ajtmp  = aj + ai[r[i]];
3304:     PetscLLAddPerm(nzi,ajtmp,ic,n,nlnk,lnk,lnkbt);

3306:     /* load in initial (unfactored row) */
3307:     aatmp = a->a + ai[r[i]];
3308:     for (j=0; j<nzi; j++) {
3309:       rtmp[ics[*ajtmp++]] = *aatmp++;
3310:     }

3312:     /* add pivot rows into linked list */
3313:     row = lnk[n];
3314:     while (row < i) {
3315:       nzi_bl = bi[row+1] - bi[row] + 1;
3316:       bjtmp  = bj + bdiag[row+1]+1; /* points to 1st column next to the diagonal in U */
3317:       PetscLLAddSortedLU(bjtmp,row,nlnk,lnk,lnkbt,i,nzi_bl,im);
3318:       nzi   += nlnk;
3319:       row    = lnk[row];
3320:     }

3322:     /* copy data from lnk into jtmp, then initialize lnk */
3323:     PetscLLClean(n,n,nzi,lnk,jtmp,lnkbt);

3325:     /* numerical factorization */
3326:     bjtmp = jtmp;
3327:     row   = *bjtmp++; /* 1st pivot row */
3328:     while (row < i) {
3329:       pc         = rtmp + row;
3330:       pv         = ba + bdiag[row]; /* 1./(diag of the pivot row) */
3331:       multiplier = (*pc) * (*pv);
3332:       *pc        = multiplier;
3333:       if (PetscAbsScalar(*pc) > dt) { /* apply tolerance dropping rule */
3334:         pj = bj + bdiag[row+1] + 1;         /* point to 1st entry of U(row,:) */
3335:         pv = ba + bdiag[row+1] + 1;
3336:         /* if (multiplier < -1.0 or multiplier >1.0) printf("row/prow %d, %d, multiplier %g\n",i,row,multiplier); */
3337:         nz = bdiag[row] - bdiag[row+1] - 1;         /* num of entries in U(row,:), excluding diagonal */
3338:         for (j=0; j<nz; j++) rtmp[*pj++] -= multiplier * (*pv++);
3339:         PetscLogFlops(1+2*nz);
3340:       }
3341:       row = *bjtmp++;
3342:     }

3344:     /* copy sparse rtmp into contiguous vtmp; separate L and U part */
3345:     diag_tmp = rtmp[i];  /* save diagonal value - may not needed?? */
3346:     nzi_bl   = 0; j = 0;
3347:     while (jtmp[j] < i) { /* Note: jtmp is sorted */
3348:       vtmp[j] = rtmp[jtmp[j]]; rtmp[jtmp[j]]=0.0;
3349:       nzi_bl++; j++;
3350:     }
3351:     nzi_bu = nzi - nzi_bl -1;
3352:     while (j < nzi) {
3353:       vtmp[j] = rtmp[jtmp[j]]; rtmp[jtmp[j]]=0.0;
3354:       j++;
3355:     }

3357:     bjtmp = bj + bi[i];
3358:     batmp = ba + bi[i];
3359:     /* apply level dropping rule to L part */
3360:     ncut = nzi_al + dtcount;
3361:     if (ncut < nzi_bl) {
3362:       PetscSortSplit(ncut,nzi_bl,vtmp,jtmp);
3363:       PetscSortIntWithScalarArray(ncut,jtmp,vtmp);
3364:     } else {
3365:       ncut = nzi_bl;
3366:     }
3367:     for (j=0; j<ncut; j++) {
3368:       bjtmp[j] = jtmp[j];
3369:       batmp[j] = vtmp[j];
3370:       /* printf(" (%d,%g),",bjtmp[j],batmp[j]); */
3371:     }
3372:     bi[i+1] = bi[i] + ncut;
3373:     nzi     = ncut + 1;

3375:     /* apply level dropping rule to U part */
3376:     ncut = nzi_au + dtcount;
3377:     if (ncut < nzi_bu) {
3378:       PetscSortSplit(ncut,nzi_bu,vtmp+nzi_bl+1,jtmp+nzi_bl+1);
3379:       PetscSortIntWithScalarArray(ncut,jtmp+nzi_bl+1,vtmp+nzi_bl+1);
3380:     } else {
3381:       ncut = nzi_bu;
3382:     }
3383:     nzi += ncut;

3385:     /* mark bdiagonal */
3386:     bdiag[i+1]       = bdiag[i] - (ncut + 1);
3387:     bdiag_rev[n-i-1] = bdiag[i+1];
3388:     bi[2*n - i]      = bi[2*n - i +1] - (ncut + 1);
3389:     bjtmp            = bj + bdiag[i];
3390:     batmp            = ba + bdiag[i];
3391:     *bjtmp           = i;
3392:     *batmp           = diag_tmp; /* rtmp[i]; */
3393:     if (*batmp == 0.0) {
3394:       *batmp = dt+shift;
3395:       /* printf(" row %d add shift %g\n",i,shift); */
3396:     }
3397:     *batmp = 1.0/(*batmp); /* invert diagonal entries for simplier triangular solves */
3398:     /* printf(" (%d,%g),",*bjtmp,*batmp); */

3400:     bjtmp = bj + bdiag[i+1]+1;
3401:     batmp = ba + bdiag[i+1]+1;
3402:     for (k=0; k<ncut; k++) {
3403:       bjtmp[k] = jtmp[nzi_bl+1+k];
3404:       batmp[k] = vtmp[nzi_bl+1+k];
3405:       /* printf(" (%d,%g),",bjtmp[k],batmp[k]); */
3406:     }
3407:     /* printf("\n"); */

3409:     im[i] = nzi;   /* used by PetscLLAddSortedLU() */
3410:     /*
3411:     printf("row %d: bi %d, bdiag %d\n",i,bi[i],bdiag[i]);
3412:     printf(" ----------------------------\n");
3413:     */
3414:   } /* for (i=0; i<n; i++) */
3415:     /* printf("end of L %d, beginning of U %d\n",bi[n],bdiag[n]); */
3416:   if (bi[n] >= bdiag[n]) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"end of L array %d cannot >= the beginning of U array %d",bi[n],bdiag[n]);

3418:   ISRestoreIndices(isrow,&r);
3419:   ISRestoreIndices(isicol,&ic);

3421:   PetscLLDestroy(lnk,lnkbt);
3422:   PetscFree2(im,jtmp);
3423:   PetscFree2(rtmp,vtmp);
3424:   PetscFree(bdiag_rev);

3426:   PetscLogFlops(B->cmap->n);
3427:   b->maxnz = b->nz = bi[n] + bdiag[0] - bdiag[n];

3429:   ISIdentity(isrow,&row_identity);
3430:   ISIdentity(isicol,&icol_identity);
3431:   if (row_identity && icol_identity) {
3432:     B->ops->solve = MatSolve_SeqAIJ_NaturalOrdering;
3433:   } else {
3434:     B->ops->solve = MatSolve_SeqAIJ;
3435:   }

3437:   B->ops->solveadd          = 0;
3438:   B->ops->solvetranspose    = 0;
3439:   B->ops->solvetransposeadd = 0;
3440:   B->ops->matsolve          = 0;
3441:   B->assembled              = PETSC_TRUE;
3442:   B->preallocated           = PETSC_TRUE;
3443:   return(0);
3444: }

3446: /* a wraper of MatILUDTFactor_SeqAIJ() */
3447: /*
3448:     This will get a new name and become a varient of MatILUFactor_SeqAIJ() there is no longer separate functions in the matrix function table for dt factors
3449: */

3451: PetscErrorCode  MatILUDTFactorSymbolic_SeqAIJ(Mat fact,Mat A,IS row,IS col,const MatFactorInfo *info)
3452: {

3456:   MatILUDTFactor_SeqAIJ(A,row,col,info,&fact);
3457:   return(0);
3458: }

3460: /*
3461:    same as MatLUFactorNumeric_SeqAIJ(), except using contiguous array matrix factors
3462:    - intend to replace existing MatLUFactorNumeric_SeqAIJ()
3463: */
3464: /*
3465:     This will get a new name and become a varient of MatILUFactor_SeqAIJ() there is no longer separate functions in the matrix function table for dt factors
3466: */

3468: PetscErrorCode  MatILUDTFactorNumeric_SeqAIJ(Mat fact,Mat A,const MatFactorInfo *info)
3469: {
3470:   Mat            C     =fact;
3471:   Mat_SeqAIJ     *a    =(Mat_SeqAIJ*)A->data,*b=(Mat_SeqAIJ*)C->data;
3472:   IS             isrow = b->row,isicol = b->icol;
3474:   const PetscInt *r,*ic,*ics;
3475:   PetscInt       i,j,k,n=A->rmap->n,*ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j;
3476:   PetscInt       *ajtmp,*bjtmp,nz,nzl,nzu,row,*bdiag = b->diag,*pj;
3477:   MatScalar      *rtmp,*pc,multiplier,*v,*pv,*aa=a->a;
3478:   PetscReal      dt=info->dt,shift=info->shiftamount;
3479:   PetscBool      row_identity, col_identity;

3482:   ISGetIndices(isrow,&r);
3483:   ISGetIndices(isicol,&ic);
3484:   PetscMalloc1(n+1,&rtmp);
3485:   ics  = ic;

3487:   for (i=0; i<n; i++) {
3488:     /* initialize rtmp array */
3489:     nzl   = bi[i+1] - bi[i];       /* num of nozeros in L(i,:) */
3490:     bjtmp = bj + bi[i];
3491:     for  (j=0; j<nzl; j++) rtmp[*bjtmp++] = 0.0;
3492:     rtmp[i] = 0.0;
3493:     nzu     = bdiag[i] - bdiag[i+1]; /* num of nozeros in U(i,:) */
3494:     bjtmp   = bj + bdiag[i+1] + 1;
3495:     for  (j=0; j<nzu; j++) rtmp[*bjtmp++] = 0.0;

3497:     /* load in initial unfactored row of A */
3498:     /* printf("row %d\n",i); */
3499:     nz    = ai[r[i]+1] - ai[r[i]];
3500:     ajtmp = aj + ai[r[i]];
3501:     v     = aa + ai[r[i]];
3502:     for (j=0; j<nz; j++) {
3503:       rtmp[ics[*ajtmp++]] = v[j];
3504:       /* printf(" (%d,%g),",ics[ajtmp[j]],rtmp[ics[ajtmp[j]]]); */
3505:     }
3506:     /* printf("\n"); */

3508:     /* numerical factorization */
3509:     bjtmp = bj + bi[i]; /* point to 1st entry of L(i,:) */
3510:     nzl   = bi[i+1] - bi[i]; /* num of entries in L(i,:) */
3511:     k     = 0;
3512:     while (k < nzl) {
3513:       row = *bjtmp++;
3514:       /* printf("  prow %d\n",row); */
3515:       pc         = rtmp + row;
3516:       pv         = b->a + bdiag[row]; /* 1./(diag of the pivot row) */
3517:       multiplier = (*pc) * (*pv);
3518:       *pc        = multiplier;
3519:       if (PetscAbsScalar(multiplier) > dt) {
3520:         pj = bj + bdiag[row+1] + 1;         /* point to 1st entry of U(row,:) */
3521:         pv = b->a + bdiag[row+1] + 1;
3522:         nz = bdiag[row] - bdiag[row+1] - 1;         /* num of entries in U(row,:), excluding diagonal */
3523:         for (j=0; j<nz; j++) rtmp[*pj++] -= multiplier * (*pv++);
3524:         PetscLogFlops(1+2*nz);
3525:       }
3526:       k++;
3527:     }

3529:     /* finished row so stick it into b->a */
3530:     /* L-part */
3531:     pv  = b->a + bi[i];
3532:     pj  = bj + bi[i];
3533:     nzl = bi[i+1] - bi[i];
3534:     for (j=0; j<nzl; j++) {
3535:       pv[j] = rtmp[pj[j]];
3536:       /* printf(" (%d,%g),",pj[j],pv[j]); */
3537:     }

3539:     /* diagonal: invert diagonal entries for simplier triangular solves */
3540:     if (rtmp[i] == 0.0) rtmp[i] = dt+shift;
3541:     b->a[bdiag[i]] = 1.0/rtmp[i];
3542:     /* printf(" (%d,%g),",i,b->a[bdiag[i]]); */

3544:     /* U-part */
3545:     pv  = b->a + bdiag[i+1] + 1;
3546:     pj  = bj + bdiag[i+1] + 1;
3547:     nzu = bdiag[i] - bdiag[i+1] - 1;
3548:     for (j=0; j<nzu; j++) {
3549:       pv[j] = rtmp[pj[j]];
3550:       /* printf(" (%d,%g),",pj[j],pv[j]); */
3551:     }
3552:     /* printf("\n"); */
3553:   }

3555:   PetscFree(rtmp);
3556:   ISRestoreIndices(isicol,&ic);
3557:   ISRestoreIndices(isrow,&r);

3559:   ISIdentity(isrow,&row_identity);
3560:   ISIdentity(isicol,&col_identity);
3561:   if (row_identity && col_identity) {
3562:     C->ops->solve = MatSolve_SeqAIJ_NaturalOrdering;
3563:   } else {
3564:     C->ops->solve = MatSolve_SeqAIJ;
3565:   }
3566:   C->ops->solveadd          = 0;
3567:   C->ops->solvetranspose    = 0;
3568:   C->ops->solvetransposeadd = 0;
3569:   C->ops->matsolve          = 0;
3570:   C->assembled              = PETSC_TRUE;
3571:   C->preallocated           = PETSC_TRUE;

3573:   PetscLogFlops(C->cmap->n);
3574:   return(0);
3575: }