xref: /petsc/src/mat/impls/aij/seq/aijfact.c (revision b2a402fc331fe3a03362e2dcf4ac2d81e3edb218)
1 #define PETSCMAT_DLL
2 
3 
4 #include "../src/mat/impls/aij/seq/aij.h"
5 #include "../src/mat/impls/sbaij/seq/sbaij.h"
6 #include "petscbt.h"
7 #include "../src/mat/utils/freespace.h"
8 
9 EXTERN_C_BEGIN
10 #undef __FUNCT__
11 #define __FUNCT__ "MatOrdering_Flow_SeqAIJ"
12 /*
13       Computes an ordering to get most of the large numerical values in the lower triangular part of the matrix
14 */
15 PetscErrorCode MatOrdering_Flow_SeqAIJ(Mat mat,const MatOrderingType type,IS *irow,IS *icol)
16 {
17   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)mat->data;
18   PetscErrorCode    ierr;
19   PetscInt          i,j,jj,k, kk,n = mat->rmap->n, current = 0, newcurrent = 0,*order;
20   const PetscInt    *ai = a->i, *aj = a->j;
21   const PetscScalar *aa = a->a;
22   PetscTruth        *done;
23   PetscReal         best,past = 0,future;
24 
25   PetscFunctionBegin;
26   /* pick initial row */
27   best = -1;
28   for (i=0; i<n; i++) {
29     future = 0.0;
30     for (j=ai[i]; j<ai[i+1]; j++) {
31       if (aj[j] != i) future  += PetscAbsScalar(aa[j]); else past = PetscAbsScalar(aa[j]);
32     }
33     if (!future) future = 1.e-10; /* if there is zero in the upper diagonal part want to rank this row high */
34     if (past/future > best) {
35       best = past/future;
36       current = i;
37     }
38   }
39 
40   ierr = PetscMalloc(n*sizeof(PetscTruth),&done);CHKERRQ(ierr);
41   ierr = PetscMemzero(done,n*sizeof(PetscTruth));CHKERRQ(ierr);
42   ierr = PetscMalloc(n*sizeof(PetscInt),&order);CHKERRQ(ierr);
43   order[0] = current;
44   for (i=0; i<n-1; i++) {
45     done[current] = PETSC_TRUE;
46     best          = -1;
47     /* loop over all neighbors of current pivot */
48     for (j=ai[current]; j<ai[current+1]; j++) {
49       jj = aj[j];
50       if (done[jj]) continue;
51       /* loop over columns of potential next row computing weights for below and above diagonal */
52       past = future = 0.0;
53       for (k=ai[jj]; k<ai[jj+1]; k++) {
54         kk = aj[k];
55         if (done[kk]) past += PetscAbsScalar(aa[k]);
56         else if (kk != jj) future  += PetscAbsScalar(aa[k]);
57       }
58       if (!future) future = 1.e-10; /* if there is zero in the upper diagonal part want to rank this row high */
59       if (past/future > best) {
60         best = past/future;
61         newcurrent = jj;
62       }
63     }
64     if (best == -1) { /* no neighbors to select from so select best of all that remain */
65       best = -1;
66       for (k=0; k<n; k++) {
67         if (done[k]) continue;
68         future = 0.0;
69         past   = 0.0;
70         for (j=ai[k]; j<ai[k+1]; j++) {
71           kk = aj[j];
72           if (done[kk]) past += PetscAbsScalar(aa[j]);
73           else if (kk != k) future  += PetscAbsScalar(aa[j]);
74         }
75         if (!future) future = 1.e-10; /* if there is zero in the upper diagonal part want to rank this row high */
76         if (past/future > best) {
77           best = past/future;
78           newcurrent = k;
79         }
80       }
81     }
82     if (current == newcurrent) SETERRQ(PETSC_ERR_PLIB,"newcurrent cannot be current");
83     current = newcurrent;
84     order[i+1] = current;
85   }
86   ierr = ISCreateGeneral(PETSC_COMM_SELF,n,order,irow);CHKERRQ(ierr);
87   *icol = *irow;
88   ierr = PetscObjectReference((PetscObject)*irow);CHKERRQ(ierr);
89   ierr = PetscFree(done);CHKERRQ(ierr);
90   ierr = PetscFree(order);CHKERRQ(ierr);
91   PetscFunctionReturn(0);
92 }
93 EXTERN_C_END
94 
95 EXTERN_C_BEGIN
96 #undef __FUNCT__
97 #define __FUNCT__ "MatGetFactorAvailable_seqaij_petsc"
98 PetscErrorCode MatGetFactorAvailable_seqaij_petsc(Mat A,MatFactorType ftype,PetscTruth *flg)
99 {
100   PetscFunctionBegin;
101   *flg = PETSC_TRUE;
102   PetscFunctionReturn(0);
103 }
104 EXTERN_C_END
105 
106 EXTERN_C_BEGIN
107 #undef __FUNCT__
108 #define __FUNCT__ "MatGetFactor_seqaij_petsc"
109 PetscErrorCode MatGetFactor_seqaij_petsc(Mat A,MatFactorType ftype,Mat *B)
110 {
111   PetscInt           n = A->rmap->n;
112   PetscErrorCode     ierr;
113 
114   PetscFunctionBegin;
115   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
116   ierr = MatSetSizes(*B,n,n,n,n);CHKERRQ(ierr);
117   if (ftype == MAT_FACTOR_LU || ftype == MAT_FACTOR_ILU || ftype == MAT_FACTOR_ILUDT){
118     ierr = MatSetType(*B,MATSEQAIJ);CHKERRQ(ierr);
119     (*B)->ops->ilufactorsymbolic = MatILUFactorSymbolic_SeqAIJ;
120     (*B)->ops->lufactorsymbolic  = MatLUFactorSymbolic_SeqAIJ;
121   } else if (ftype == MAT_FACTOR_CHOLESKY || ftype == MAT_FACTOR_ICC) {
122     ierr = MatSetType(*B,MATSEQSBAIJ);CHKERRQ(ierr);
123     ierr = MatSeqSBAIJSetPreallocation(*B,1,MAT_SKIP_ALLOCATION,PETSC_NULL);CHKERRQ(ierr);
124     (*B)->ops->iccfactorsymbolic      = MatICCFactorSymbolic_SeqAIJ;
125     (*B)->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_SeqAIJ;
126   } else SETERRQ(PETSC_ERR_SUP,"Factor type not supported");
127   (*B)->factor = ftype;
128   PetscFunctionReturn(0);
129 }
130 EXTERN_C_END
131 
132 #undef __FUNCT__
133 #define __FUNCT__ "MatLUFactorSymbolic_SeqAIJ_inplace"
134 PetscErrorCode MatLUFactorSymbolic_SeqAIJ_inplace(Mat B,Mat A,IS isrow,IS iscol,const MatFactorInfo *info)
135 {
136   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data,*b;
137   IS                 isicol;
138   PetscErrorCode     ierr;
139   const PetscInt     *r,*ic;
140   PetscInt           i,n=A->rmap->n,*ai=a->i,*aj=a->j;
141   PetscInt           *bi,*bj,*ajtmp;
142   PetscInt           *bdiag,row,nnz,nzi,reallocs=0,nzbd,*im;
143   PetscReal          f;
144   PetscInt           nlnk,*lnk,k,**bi_ptr;
145   PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL;
146   PetscBT            lnkbt;
147 
148   PetscFunctionBegin;
149   if (A->rmap->N != A->cmap->N) SETERRQ(PETSC_ERR_ARG_WRONG,"matrix must be square");
150   ierr = ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);CHKERRQ(ierr);
151   ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr);
152   ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr);
153 
154   /* get new row pointers */
155   ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bi);CHKERRQ(ierr);
156   bi[0] = 0;
157 
158   /* bdiag is location of diagonal in factor */
159   ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bdiag);CHKERRQ(ierr);
160   bdiag[0] = 0;
161 
162   /* linked list for storing column indices of the active row */
163   nlnk = n + 1;
164   ierr = PetscLLCreate(n,n,nlnk,lnk,lnkbt);CHKERRQ(ierr);
165 
166   ierr = PetscMalloc2(n+1,PetscInt**,&bi_ptr,n+1,PetscInt,&im);CHKERRQ(ierr);
167 
168   /* initial FreeSpace size is f*(ai[n]+1) */
169   f = info->fill;
170   ierr = PetscFreeSpaceGet((PetscInt)(f*(ai[n]+1)),&free_space);CHKERRQ(ierr);
171   current_space = free_space;
172 
173   for (i=0; i<n; i++) {
174     /* copy previous fill into linked list */
175     nzi = 0;
176     nnz = ai[r[i]+1] - ai[r[i]];
177     if (!nnz) SETERRQ2(PETSC_ERR_MAT_LU_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",r[i],i);
178     ajtmp = aj + ai[r[i]];
179     ierr = PetscLLAddPerm(nnz,ajtmp,ic,n,nlnk,lnk,lnkbt);CHKERRQ(ierr);
180     nzi += nlnk;
181 
182     /* add pivot rows into linked list */
183     row = lnk[n];
184     while (row < i) {
185       nzbd    = bdiag[row] - bi[row] + 1; /* num of entries in the row with column index <= row */
186       ajtmp   = bi_ptr[row] + nzbd; /* points to the entry next to the diagonal */
187       ierr = PetscLLAddSortedLU(ajtmp,row,nlnk,lnk,lnkbt,i,nzbd,im);CHKERRQ(ierr);
188       nzi += nlnk;
189       row  = lnk[row];
190     }
191     bi[i+1] = bi[i] + nzi;
192     im[i]   = nzi;
193 
194     /* mark bdiag */
195     nzbd = 0;
196     nnz  = nzi;
197     k    = lnk[n];
198     while (nnz-- && k < i){
199       nzbd++;
200       k = lnk[k];
201     }
202     bdiag[i] = bi[i] + nzbd;
203 
204     /* if free space is not available, make more free space */
205     if (current_space->local_remaining<nzi) {
206       nnz = (n - i)*nzi; /* estimated and max additional space needed */
207       ierr = PetscFreeSpaceGet(nnz,&current_space);CHKERRQ(ierr);
208       reallocs++;
209     }
210 
211     /* copy data into free space, then initialize lnk */
212     ierr = PetscLLClean(n,n,nzi,lnk,current_space->array,lnkbt);CHKERRQ(ierr);
213     bi_ptr[i] = current_space->array;
214     current_space->array           += nzi;
215     current_space->local_used      += nzi;
216     current_space->local_remaining -= nzi;
217   }
218 #if defined(PETSC_USE_INFO)
219   if (ai[n] != 0) {
220     PetscReal af = ((PetscReal)bi[n])/((PetscReal)ai[n]);
221     ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocs,f,af);CHKERRQ(ierr);
222     ierr = PetscInfo1(A,"Run with -pc_factor_fill %G or use \n",af);CHKERRQ(ierr);
223     ierr = PetscInfo1(A,"PCFactorSetFill(pc,%G);\n",af);CHKERRQ(ierr);
224     ierr = PetscInfo(A,"for best performance.\n");CHKERRQ(ierr);
225   } else {
226     ierr = PetscInfo(A,"Empty matrix\n");CHKERRQ(ierr);
227   }
228 #endif
229 
230   ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr);
231   ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr);
232 
233   /* destroy list of free space and other temporary array(s) */
234   ierr = PetscMalloc((bi[n]+1)*sizeof(PetscInt),&bj);CHKERRQ(ierr);
235   ierr = PetscFreeSpaceContiguous(&free_space,bj);CHKERRQ(ierr);
236   ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr);
237   ierr = PetscFree2(bi_ptr,im);CHKERRQ(ierr);
238 
239   /* put together the new matrix */
240   ierr = MatSeqAIJSetPreallocation_SeqAIJ(B,MAT_SKIP_ALLOCATION,PETSC_NULL);CHKERRQ(ierr);
241   ierr = PetscLogObjectParent(B,isicol);CHKERRQ(ierr);
242   b    = (Mat_SeqAIJ*)(B)->data;
243   b->free_a       = PETSC_TRUE;
244   b->free_ij      = PETSC_TRUE;
245   b->singlemalloc = PETSC_FALSE;
246   ierr          = PetscMalloc((bi[n]+1)*sizeof(PetscScalar),&b->a);CHKERRQ(ierr);
247   b->j          = bj;
248   b->i          = bi;
249   b->diag       = bdiag;
250   b->ilen       = 0;
251   b->imax       = 0;
252   b->row        = isrow;
253   b->col        = iscol;
254   ierr          = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr);
255   ierr          = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr);
256   b->icol       = isicol;
257   ierr          = PetscMalloc((n+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr);
258 
259   /* In b structure:  Free imax, ilen, old a, old j.  Allocate solve_work, new a, new j */
260   ierr = PetscLogObjectMemory(B,(bi[n]-n)*(sizeof(PetscInt)+sizeof(PetscScalar)));CHKERRQ(ierr);
261   b->maxnz = b->nz = bi[n] ;
262 
263   (B)->factor                = MAT_FACTOR_LU;
264   (B)->info.factor_mallocs   = reallocs;
265   (B)->info.fill_ratio_given = f;
266 
267   if (ai[n]) {
268     (B)->info.fill_ratio_needed = ((PetscReal)bi[n])/((PetscReal)ai[n]);
269   } else {
270     (B)->info.fill_ratio_needed = 0.0;
271   }
272   (B)->ops->lufactornumeric  = MatLUFactorNumeric_SeqAIJ_inplace;
273   (B)->ops->solve            = MatSolve_SeqAIJ_inplace;
274   (B)->ops->solvetranspose   = MatSolveTranspose_SeqAIJ_inplace;
275   /* switch to inodes if appropriate */
276   ierr = MatLUFactorSymbolic_SeqAIJ_Inode(B,A,isrow,iscol,info);CHKERRQ(ierr);
277   PetscFunctionReturn(0);
278 }
279 
280 #undef __FUNCT__
281 #define __FUNCT__ "MatLUFactorSymbolic_SeqAIJ"
282 PetscErrorCode MatLUFactorSymbolic_SeqAIJ(Mat B,Mat A,IS isrow,IS iscol,const MatFactorInfo *info)
283 {
284   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data,*b;
285   IS                 isicol;
286   PetscErrorCode     ierr;
287   const PetscInt     *r,*ic;
288   PetscInt           i,n=A->rmap->n,*ai=a->i,*aj=a->j;
289   PetscInt           *bi,*bj,*ajtmp;
290   PetscInt           *bdiag,row,nnz,nzi,reallocs=0,nzbd,*im;
291   PetscReal          f;
292   PetscInt           nlnk,*lnk,k,**bi_ptr;
293   PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL;
294   PetscBT            lnkbt;
295 
296   PetscFunctionBegin;
297   if (A->rmap->N != A->cmap->N) SETERRQ(PETSC_ERR_ARG_WRONG,"matrix must be square");
298   ierr = ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);CHKERRQ(ierr);
299   ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr);
300   ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr);
301 
302   /* get new row and diagonal pointers, must be allocated separately because they will be given to the Mat_SeqAIJ and freed separately */
303   ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bi);CHKERRQ(ierr);
304   ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bdiag);CHKERRQ(ierr);
305   bi[0] = bdiag[0] = 0;
306 
307   /* linked list for storing column indices of the active row */
308   nlnk = n + 1;
309   ierr = PetscLLCreate(n,n,nlnk,lnk,lnkbt);CHKERRQ(ierr);
310 
311   ierr = PetscMalloc2(n+1,PetscInt**,&bi_ptr,n+1,PetscInt,&im);CHKERRQ(ierr);
312 
313   /* initial FreeSpace size is f*(ai[n]+1) */
314   f = info->fill;
315   ierr = PetscFreeSpaceGet((PetscInt)(f*(ai[n]+1)),&free_space);CHKERRQ(ierr);
316   current_space = free_space;
317 
318   for (i=0; i<n; i++) {
319     /* copy previous fill into linked list */
320     nzi = 0;
321     nnz = ai[r[i]+1] - ai[r[i]];
322     if (!nnz) SETERRQ2(PETSC_ERR_MAT_LU_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",r[i],i);
323     ajtmp = aj + ai[r[i]];
324     ierr = PetscLLAddPerm(nnz,ajtmp,ic,n,nlnk,lnk,lnkbt);CHKERRQ(ierr);
325     nzi += nlnk;
326 
327     /* add pivot rows into linked list */
328     row = lnk[n];
329     while (row < i){
330       nzbd  = bdiag[row] + 1; /* num of entries in the row with column index <= row */
331       ajtmp = bi_ptr[row] + nzbd; /* points to the entry next to the diagonal */
332       ierr  = PetscLLAddSortedLU(ajtmp,row,nlnk,lnk,lnkbt,i,nzbd,im);CHKERRQ(ierr);
333       nzi  += nlnk;
334       row   = lnk[row];
335     }
336     bi[i+1] = bi[i] + nzi;
337     im[i]   = nzi;
338 
339     /* mark bdiag */
340     nzbd = 0;
341     nnz  = nzi;
342     k    = lnk[n];
343     while (nnz-- && k < i){
344       nzbd++;
345       k = lnk[k];
346     }
347     bdiag[i] = nzbd; /* note: bdiag[i] = nnzL as input for PetscFreeSpaceContiguous_LU() */
348 
349     /* if free space is not available, make more free space */
350     if (current_space->local_remaining<nzi) {
351       nnz = 2*(n - i)*nzi; /* estimated and max additional space needed */
352       ierr = PetscFreeSpaceGet(nnz,&current_space);CHKERRQ(ierr);
353       reallocs++;
354     }
355 
356     /* copy data into free space, then initialize lnk */
357     ierr = PetscLLClean(n,n,nzi,lnk,current_space->array,lnkbt);CHKERRQ(ierr);
358     bi_ptr[i] = current_space->array;
359     current_space->array           += nzi;
360     current_space->local_used      += nzi;
361     current_space->local_remaining -= nzi;
362   }
363 #if defined(PETSC_USE_INFO)
364   if (ai[n] != 0) {
365     PetscReal af = ((PetscReal)bi[n])/((PetscReal)ai[n]);
366     ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocs,f,af);CHKERRQ(ierr);
367     ierr = PetscInfo1(A,"Run with -pc_factor_fill %G or use \n",af);CHKERRQ(ierr);
368     ierr = PetscInfo1(A,"PCFactorSetFill(pc,%G);\n",af);CHKERRQ(ierr);
369     ierr = PetscInfo(A,"for best performance.\n");CHKERRQ(ierr);
370   } else {
371     ierr = PetscInfo(A,"Empty matrix\n");CHKERRQ(ierr);
372   }
373 #endif
374 
375   ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr);
376   ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr);
377 
378   /* destroy list of free space and other temporary array(s) */
379   ierr = PetscMalloc((bi[n]+1)*sizeof(PetscInt),&bj);CHKERRQ(ierr);
380   ierr = PetscFreeSpaceContiguous_LU(&free_space,bj,n,bi,bdiag);CHKERRQ(ierr);
381   ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr);
382   ierr = PetscFree2(bi_ptr,im);CHKERRQ(ierr);
383 
384   /* put together the new matrix */
385   ierr = MatSeqAIJSetPreallocation_SeqAIJ(B,MAT_SKIP_ALLOCATION,PETSC_NULL);CHKERRQ(ierr);
386   ierr = PetscLogObjectParent(B,isicol);CHKERRQ(ierr);
387   b    = (Mat_SeqAIJ*)(B)->data;
388   b->free_a       = PETSC_TRUE;
389   b->free_ij      = PETSC_TRUE;
390   b->singlemalloc = PETSC_FALSE;
391   ierr = PetscMalloc((bdiag[0]+1)*sizeof(PetscScalar),&b->a);CHKERRQ(ierr);
392   b->j          = bj;
393   b->i          = bi;
394   b->diag       = bdiag;
395   b->ilen       = 0;
396   b->imax       = 0;
397   b->row        = isrow;
398   b->col        = iscol;
399   ierr          = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr);
400   ierr          = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr);
401   b->icol       = isicol;
402   ierr          = PetscMalloc((n+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr);
403 
404   /* In b structure:  Free imax, ilen, old a, old j.  Allocate solve_work, new a, new j */
405   ierr = PetscLogObjectMemory(B,(bdiag[0]+1)*(sizeof(PetscInt)+sizeof(PetscScalar)));CHKERRQ(ierr);
406   b->maxnz = b->nz = bdiag[0]+1;
407   B->factor                = MAT_FACTOR_LU;
408   B->info.factor_mallocs   = reallocs;
409   B->info.fill_ratio_given = f;
410 
411   if (ai[n]) {
412     B->info.fill_ratio_needed = ((PetscReal)(bdiag[0]+1))/((PetscReal)ai[n]);
413   } else {
414     B->info.fill_ratio_needed = 0.0;
415   }
416   B->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJ;
417   /* switch to inodes if appropriate */
418   ierr = Mat_CheckInode_FactorLU(B,PETSC_FALSE);CHKERRQ(ierr);
419   PetscFunctionReturn(0);
420 }
421 
422 /*
423     Trouble in factorization, should we dump the original matrix?
424 */
425 #undef __FUNCT__
426 #define __FUNCT__ "MatFactorDumpMatrix"
427 PetscErrorCode MatFactorDumpMatrix(Mat A)
428 {
429   PetscErrorCode ierr;
430   PetscTruth     flg = PETSC_FALSE;
431 
432   PetscFunctionBegin;
433   ierr = PetscOptionsGetTruth(PETSC_NULL,"-mat_factor_dump_on_error",&flg,PETSC_NULL);CHKERRQ(ierr);
434   if (flg) {
435     PetscViewer viewer;
436     char        filename[PETSC_MAX_PATH_LEN];
437 
438     ierr = PetscSNPrintf(filename,PETSC_MAX_PATH_LEN,"matrix_factor_error.%d",PetscGlobalRank);CHKERRQ(ierr);
439     ierr = PetscViewerBinaryOpen(((PetscObject)A)->comm,filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
440     ierr = MatView(A,viewer);CHKERRQ(ierr);
441     ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr);
442   }
443   PetscFunctionReturn(0);
444 }
445 
446 #undef __FUNCT__
447 #define __FUNCT__ "MatLUFactorNumeric_SeqAIJ"
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   PetscTruth       row_identity,col_identity;
461 
462   FactorShiftCtx sctx;
463   PetscInt       *ddiag;
464   PetscReal      rs;
465   MatScalar      d;
466 
467   PetscFunctionBegin;
468   /* MatPivotSetUp(): initialize shift context sctx */
469   ierr = PetscMemzero(&sctx,sizeof(FactorShiftCtx));CHKERRQ(ierr);
470 
471   if (info->shifttype == MAT_SHIFT_POSITIVE_DEFINITE) { /* set sctx.shift_top=max{rs} */
472     ddiag          = a->diag;
473     sctx.shift_top = info->zeropivot;
474     for (i=0; i<n; i++) {
475       /* calculate sum(|aij|)-RealPart(aii), amt of shift needed for this row */
476       d  = (aa)[ddiag[i]];
477       rs = -PetscAbsScalar(d) - PetscRealPart(d);
478       v  = aa+ai[i];
479       nz = ai[i+1] - ai[i];
480       for (j=0; j<nz; j++)
481 	rs += PetscAbsScalar(v[j]);
482       if (rs>sctx.shift_top) sctx.shift_top = rs;
483     }
484     sctx.shift_top   *= 1.1;
485     sctx.nshift_max   = 5;
486     sctx.shift_lo     = 0.;
487     sctx.shift_hi     = 1.;
488   }
489 
490   ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr);
491   ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr);
492   ierr = PetscMalloc((n+1)*sizeof(MatScalar),&rtmp);CHKERRQ(ierr);
493   ics  = ic;
494 
495   do {
496     sctx.useshift = PETSC_FALSE;
497     for (i=0; i<n; i++){
498       /* zero rtmp */
499       /* L part */
500       nz    = bi[i+1] - bi[i];
501       bjtmp = bj + bi[i];
502       for  (j=0; j<nz; j++) rtmp[bjtmp[j]] = 0.0;
503 
504       /* U part */
505       nz = bdiag[i]-bdiag[i+1];
506       bjtmp = bj + bdiag[i+1]+1;
507       for  (j=0; j<nz; j++) rtmp[bjtmp[j]] = 0.0;
508 
509       /* load in initial (unfactored row) */
510       nz    = ai[r[i]+1] - ai[r[i]];
511       ajtmp = aj + ai[r[i]];
512       v     = aa + ai[r[i]];
513       for (j=0; j<nz; j++) {
514         rtmp[ics[ajtmp[j]]] = v[j];
515       }
516       /* ZeropivotApply() */
517       rtmp[i] += sctx.shift_amount;  /* shift the diagonal of the matrix */
518 
519       /* elimination */
520       bjtmp = bj + bi[i];
521       row   = *bjtmp++;
522       nzL   = bi[i+1] - bi[i];
523       for(k=0; k < nzL;k++) {
524         pc = rtmp + row;
525         if (*pc != 0.0) {
526           pv         = b->a + bdiag[row];
527           multiplier = *pc * (*pv);
528           *pc        = multiplier;
529           pj = b->j + bdiag[row+1]+1; /* beginning of U(row,:) */
530 	  pv = b->a + bdiag[row+1]+1;
531 	  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           ierr = PetscLogFlops(2.0*nz);CHKERRQ(ierr);
534         }
535         row = *bjtmp++;
536       }
537 
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       }
547 
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       }
555 
556       /* MatPivotCheck() */
557       sctx.rs  = rs;
558       sctx.pv  = rtmp[i];
559       if (info->shifttype == MAT_SHIFT_NONZERO){
560         ierr = MatPivotCheck_nz(info,sctx,i);CHKERRQ(ierr);
561       } else if (info->shifttype == MAT_SHIFT_POSITIVE_DEFINITE){
562         ierr = MatPivotCheck_pd(info,sctx,i);CHKERRQ(ierr);
563       } else if (info->shifttype == MAT_SHIFT_INBLOCKS){
564         ierr = MatPivotCheck_inblocks(info,sctx,i);CHKERRQ(ierr);
565       } else {
566         ierr = MatPivotCheck_none(info,sctx,i);CHKERRQ(ierr);
567       }
568       rtmp[i] = sctx.pv;
569 
570       /* Mark diagonal and invert diagonal for simplier triangular solves */
571       pv  = b->a + bdiag[i];
572       *pv = 1.0/rtmp[i];
573 
574     } /* endof for (i=0; i<n; i++){ */
575 
576     /* MatPivotRefine() */
577     if (info->shifttype == MAT_SHIFT_POSITIVE_DEFINITE && !sctx.useshift && sctx.shift_fraction>0 && sctx.nshift<sctx.nshift_max){
578       /*
579        * if no shift in this attempt & shifting & started shifting & can refine,
580        * then try lower shift
581        */
582       sctx.shift_hi       = sctx.shift_fraction;
583       sctx.shift_fraction = (sctx.shift_hi+sctx.shift_lo)/2.;
584       sctx.shift_amount   = sctx.shift_fraction * sctx.shift_top;
585       sctx.useshift        = PETSC_TRUE;
586       sctx.nshift++;
587     }
588   } while (sctx.useshift);
589 
590   ierr = PetscFree(rtmp);CHKERRQ(ierr);
591   ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr);
592   ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr);
593   if (b->inode.use){
594     C->ops->solve   = MatSolve_SeqAIJ_Inode;
595   } else {
596     ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr);
597     ierr = ISIdentity(isicol,&col_identity);CHKERRQ(ierr);
598     if (row_identity && col_identity) {
599       C->ops->solve = MatSolve_SeqAIJ_NaturalOrdering;
600     } else {
601       C->ops->solve = MatSolve_SeqAIJ;
602     }
603   }
604   C->ops->solveadd           = MatSolveAdd_SeqAIJ;
605   C->ops->solvetranspose     = MatSolveTranspose_SeqAIJ;
606   C->ops->solvetransposeadd  = MatSolveTransposeAdd_SeqAIJ;
607   C->ops->matsolve           = MatMatSolve_SeqAIJ;
608   C->assembled    = PETSC_TRUE;
609   C->preallocated = PETSC_TRUE;
610   ierr = PetscLogFlops(C->cmap->n);CHKERRQ(ierr);
611 
612   /* MatPivotView() */
613   if (sctx.nshift){
614     if (info->shifttype == MAT_SHIFT_POSITIVE_DEFINITE) {
615       ierr = PetscInfo4(A,"number of shift_pd tries %D, shift_amount %G, diagonal shifted up by %e fraction top_value %e\n",sctx.nshift,sctx.shift_amount,sctx.shift_fraction,sctx.shift_top);CHKERRQ(ierr);
616     } else if (info->shifttype == MAT_SHIFT_NONZERO) {
617       ierr = PetscInfo2(A,"number of shift_nz tries %D, shift_amount %G\n",sctx.nshift,sctx.shift_amount);CHKERRQ(ierr);
618     } else if (info->shifttype == MAT_SHIFT_INBLOCKS){
619       ierr = PetscInfo2(A,"number of shift_inblocks applied %D, each shift_amount %G\n",sctx.nshift,info->shiftamount);CHKERRQ(ierr);
620     }
621   }
622   PetscFunctionReturn(0);
623 }
624 
625 #undef __FUNCT__
626 #define __FUNCT__ "MatLUFactorNumeric_SeqAIJ_inplace"
627 PetscErrorCode MatLUFactorNumeric_SeqAIJ_inplace(Mat B,Mat A,const MatFactorInfo *info)
628 {
629   Mat             C=B;
630   Mat_SeqAIJ      *a=(Mat_SeqAIJ*)A->data,*b=(Mat_SeqAIJ *)C->data;
631   IS              isrow = b->row,isicol = b->icol;
632   PetscErrorCode  ierr;
633   const PetscInt   *r,*ic,*ics;
634   PetscInt        nz,row,i,j,n=A->rmap->n,diag;
635   const PetscInt  *ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j;
636   const PetscInt  *ajtmp,*bjtmp,*diag_offset = b->diag,*pj;
637   MatScalar       *pv,*rtmp,*pc,multiplier,d;
638   const MatScalar *v,*aa=a->a;
639   PetscReal       rs=0.0;
640   FactorShiftCtx  sctx;
641   PetscInt        newshift,*ddiag;
642 
643   PetscFunctionBegin;
644   ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr);
645   ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr);
646   ierr = PetscMalloc((n+1)*sizeof(MatScalar),&rtmp);CHKERRQ(ierr);
647   ics  = ic;
648 
649   /* initialize shift context sctx */
650   sctx.nshift         = 0;
651   sctx.nshift_max     = 0;
652   sctx.shift_top      = 0.0;
653   sctx.shift_lo       = 0.0;
654   sctx.shift_hi       = 0.0;
655   sctx.shift_fraction = 0.0;
656   sctx.shift_amount   = 0.0;
657 
658   if (info->shifttype == MAT_SHIFT_POSITIVE_DEFINITE) { /* set sctx.shift_top=max{rs} */
659     ddiag          = a->diag;
660     sctx.shift_top = info->zeropivot;
661     for (i=0; i<n; i++) {
662       /* calculate sum(|aij|)-RealPart(aii), amt of shift needed for this row */
663       d  = (aa)[ddiag[i]];
664       rs = -PetscAbsScalar(d) - PetscRealPart(d);
665       v  = aa+ai[i];
666       nz = ai[i+1] - ai[i];
667       for (j=0; j<nz; j++)
668 	rs += PetscAbsScalar(v[j]);
669       if (rs>sctx.shift_top) sctx.shift_top = rs;
670     }
671     sctx.shift_top   *= 1.1;
672     sctx.nshift_max   = 5;
673     sctx.shift_lo     = 0.;
674     sctx.shift_hi     = 1.;
675   }
676 
677   do {
678     sctx.useshift = PETSC_FALSE;
679     for (i=0; i<n; i++){
680       nz    = bi[i+1] - bi[i];
681       bjtmp = bj + bi[i];
682       for  (j=0; j<nz; j++) rtmp[bjtmp[j]] = 0.0;
683 
684       /* load in initial (unfactored row) */
685       nz    = ai[r[i]+1] - ai[r[i]];
686       ajtmp = aj + ai[r[i]];
687       v     = aa + ai[r[i]];
688       for (j=0; j<nz; j++) {
689         rtmp[ics[ajtmp[j]]] = v[j];
690       }
691       rtmp[ics[r[i]]] += sctx.shift_amount; /* shift the diagonal of the matrix */
692       /* if (sctx.shift_amount > 0.0) printf("row %d, shift %g\n",i,sctx.shift_amount); */
693 
694       row = *bjtmp++;
695       while  (row < i) {
696         pc = rtmp + row;
697         if (*pc != 0.0) {
698           pv         = b->a + diag_offset[row];
699           pj         = b->j + diag_offset[row] + 1;
700           multiplier = *pc / *pv++;
701           *pc        = multiplier;
702           nz         = bi[row+1] - diag_offset[row] - 1;
703           for (j=0; j<nz; j++) rtmp[pj[j]] -= multiplier * pv[j];
704           ierr = PetscLogFlops(2.0*nz);CHKERRQ(ierr);
705         }
706         row = *bjtmp++;
707       }
708       /* finished row so stick it into b->a */
709       pv   = b->a + bi[i] ;
710       pj   = b->j + bi[i] ;
711       nz   = bi[i+1] - bi[i];
712       diag = diag_offset[i] - bi[i];
713       rs   = 0.0;
714       for (j=0; j<nz; j++) {
715         pv[j] = rtmp[pj[j]];
716         rs   += PetscAbsScalar(pv[j]);
717       }
718       rs   -= PetscAbsScalar(pv[diag]);
719 
720       /* 9/13/02 Victor Eijkhout suggested scaling zeropivot by rs for matrices with funny scalings */
721       sctx.rs  = rs;
722       sctx.pv  = pv[diag];
723       ierr = MatLUCheckShift_inline(info,sctx,i,newshift);CHKERRQ(ierr);
724       if (newshift == 1) break;
725     }
726 
727     if (info->shifttype == MAT_SHIFT_POSITIVE_DEFINITE && !sctx.useshift && sctx.shift_fraction>0 && sctx.nshift<sctx.nshift_max) {
728       /*
729        * if no shift in this attempt & shifting & started shifting & can refine,
730        * then try lower shift
731        */
732       sctx.shift_hi       = sctx.shift_fraction;
733       sctx.shift_fraction = (sctx.shift_hi+sctx.shift_lo)/2.;
734       sctx.shift_amount   = sctx.shift_fraction * sctx.shift_top;
735       sctx.useshift        = PETSC_TRUE;
736       sctx.nshift++;
737     }
738   } while (sctx.useshift);
739 
740   /* invert diagonal entries for simplier triangular solves */
741   for (i=0; i<n; i++) {
742     b->a[diag_offset[i]] = 1.0/b->a[diag_offset[i]];
743   }
744   ierr = PetscFree(rtmp);CHKERRQ(ierr);
745   ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr);
746   ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr);
747   if (b->inode.use) {
748     C->ops->solve   = MatSolve_SeqAIJ_Inode_inplace;
749   } else {
750     PetscTruth row_identity, col_identity;
751     ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr);
752     ierr = ISIdentity(isicol,&col_identity);CHKERRQ(ierr);
753     if (row_identity && col_identity) {
754       C->ops->solve   = MatSolve_SeqAIJ_NaturalOrdering_inplace;
755     } else {
756       C->ops->solve   = MatSolve_SeqAIJ_inplace;
757     }
758   }
759   C->ops->solveadd           = MatSolveAdd_SeqAIJ_inplace;
760   C->ops->solvetranspose     = MatSolveTranspose_SeqAIJ_inplace;
761   C->ops->solvetransposeadd  = MatSolveTransposeAdd_SeqAIJ_inplace;
762   C->ops->matsolve           = MatMatSolve_SeqAIJ_inplace;
763   C->assembled    = PETSC_TRUE;
764   C->preallocated = PETSC_TRUE;
765   ierr = PetscLogFlops(C->cmap->n);CHKERRQ(ierr);
766   if (sctx.nshift){
767      if (info->shifttype == MAT_SHIFT_POSITIVE_DEFINITE) {
768       ierr = PetscInfo4(A,"number of shift_pd tries %D, shift_amount %G, diagonal shifted up by %e fraction top_value %e\n",sctx.nshift,sctx.shift_amount,sctx.shift_fraction,sctx.shift_top);CHKERRQ(ierr);
769     } else if (info->shifttype == MAT_SHIFT_NONZERO) {
770       ierr = PetscInfo2(A,"number of shift_nz tries %D, shift_amount %G\n",sctx.nshift,sctx.shift_amount);CHKERRQ(ierr);
771     }
772   }
773   PetscFunctionReturn(0);
774 }
775 
776 /*
777    This routine implements inplace ILU(0) with row or/and column permutations.
778    Input:
779      A - original matrix
780    Output;
781      A - a->i (rowptr) is same as original rowptr, but factored i-the row is stored in rowperm[i]
782          a->j (col index) is permuted by the inverse of colperm, then sorted
783          a->a reordered accordingly with a->j
784          a->diag (ptr to diagonal elements) is updated.
785 */
786 #undef __FUNCT__
787 #define __FUNCT__ "MatLUFactorNumeric_SeqAIJ_InplaceWithPerm"
788 PetscErrorCode MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(Mat B,Mat A,const MatFactorInfo *info)
789 {
790   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data;
791   IS             isrow = a->row,isicol = a->icol;
792   PetscErrorCode ierr;
793   const PetscInt *r,*ic,*ics;
794   PetscInt       i,j,n=A->rmap->n,*ai=a->i,*aj=a->j;
795   PetscInt       *ajtmp,nz,row;
796   PetscInt       *diag = a->diag,nbdiag,*pj;
797   PetscScalar    *rtmp,*pc,multiplier,d;
798   MatScalar      *v,*pv;
799   PetscReal      rs;
800   FactorShiftCtx sctx;
801   PetscInt       newshift;
802 
803   PetscFunctionBegin;
804   if (A != B) SETERRQ(PETSC_ERR_ARG_INCOMP,"input and output matrix must have same address");
805   ierr  = ISGetIndices(isrow,&r);CHKERRQ(ierr);
806   ierr  = ISGetIndices(isicol,&ic);CHKERRQ(ierr);
807   ierr  = PetscMalloc((n+1)*sizeof(PetscScalar),&rtmp);CHKERRQ(ierr);
808   ierr  = PetscMemzero(rtmp,(n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
809   ics = ic;
810 
811   sctx.shift_top      = 0.;
812   sctx.nshift_max     = 0;
813   sctx.shift_lo       = 0.;
814   sctx.shift_hi       = 0.;
815   sctx.shift_fraction = 0.;
816 
817   if (info->shifttype == MAT_SHIFT_POSITIVE_DEFINITE) { /* set sctx.shift_top=max{rs} */
818     sctx.shift_top = 0.;
819     for (i=0; i<n; i++) {
820       /* calculate sum(|aij|)-RealPart(aii), amt of shift needed for this row */
821       d  = (a->a)[diag[i]];
822       rs = -PetscAbsScalar(d) - PetscRealPart(d);
823       v  = a->a+ai[i];
824       nz = ai[i+1] - ai[i];
825       for (j=0; j<nz; j++)
826 	rs += PetscAbsScalar(v[j]);
827       if (rs>sctx.shift_top) sctx.shift_top = rs;
828     }
829     if (sctx.shift_top < info->zeropivot) sctx.shift_top = info->zeropivot;
830     sctx.shift_top    *= 1.1;
831     sctx.nshift_max   = 5;
832     sctx.shift_lo     = 0.;
833     sctx.shift_hi     = 1.;
834   }
835 
836   sctx.shift_amount = 0.;
837   sctx.nshift       = 0;
838   do {
839     sctx.useshift = PETSC_FALSE;
840     for (i=0; i<n; i++){
841       /* load in initial unfactored row */
842       nz    = ai[r[i]+1] - ai[r[i]];
843       ajtmp = aj + ai[r[i]];
844       v     = a->a + ai[r[i]];
845       /* sort permuted ajtmp and values v accordingly */
846       for (j=0; j<nz; j++) ajtmp[j] = ics[ajtmp[j]];
847       ierr = PetscSortIntWithScalarArray(nz,ajtmp,v);CHKERRQ(ierr);
848 
849       diag[r[i]] = ai[r[i]];
850       for (j=0; j<nz; j++) {
851         rtmp[ajtmp[j]] = v[j];
852         if (ajtmp[j] < i) diag[r[i]]++; /* update a->diag */
853       }
854       rtmp[r[i]] += sctx.shift_amount; /* shift the diagonal of the matrix */
855 
856       row = *ajtmp++;
857       while  (row < i) {
858         pc = rtmp + row;
859         if (*pc != 0.0) {
860           pv         = a->a + diag[r[row]];
861           pj         = aj + diag[r[row]] + 1;
862 
863           multiplier = *pc / *pv++;
864           *pc        = multiplier;
865           nz         = ai[r[row]+1] - diag[r[row]] - 1;
866           for (j=0; j<nz; j++) rtmp[pj[j]] -= multiplier * pv[j];
867           ierr = PetscLogFlops(2.0*nz);CHKERRQ(ierr);
868         }
869         row = *ajtmp++;
870       }
871       /* finished row so overwrite it onto a->a */
872       pv   = a->a + ai[r[i]] ;
873       pj   = aj + ai[r[i]] ;
874       nz   = ai[r[i]+1] - ai[r[i]];
875       nbdiag = diag[r[i]] - ai[r[i]]; /* num of entries before the diagonal */
876 
877       rs   = 0.0;
878       for (j=0; j<nz; j++) {
879         pv[j] = rtmp[pj[j]];
880         if (j != nbdiag) rs += PetscAbsScalar(pv[j]);
881       }
882 
883       /* 9/13/02 Victor Eijkhout suggested scaling zeropivot by rs for matrices with funny scalings */
884       sctx.rs  = rs;
885       sctx.pv  = pv[nbdiag];
886       ierr = MatLUCheckShift_inline(info,sctx,i,newshift);CHKERRQ(ierr);
887       if (newshift == 1) break;
888     }
889 
890     if (info->shifttype == MAT_SHIFT_POSITIVE_DEFINITE && !sctx.useshift && sctx.shift_fraction>0 && sctx.nshift<sctx.nshift_max) {
891       /*
892        * if no shift in this attempt & shifting & started shifting & can refine,
893        * then try lower shift
894        */
895       sctx.shift_hi        = sctx.shift_fraction;
896       sctx.shift_fraction = (sctx.shift_hi+sctx.shift_lo)/2.;
897       sctx.shift_amount    = sctx.shift_fraction * sctx.shift_top;
898       sctx.useshift         = PETSC_TRUE;
899       sctx.nshift++;
900     }
901   } while (sctx.useshift);
902 
903   /* invert diagonal entries for simplier triangular solves */
904   for (i=0; i<n; i++) {
905     a->a[diag[r[i]]] = 1.0/a->a[diag[r[i]]];
906   }
907 
908   ierr = PetscFree(rtmp);CHKERRQ(ierr);
909   ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr);
910   ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr);
911   A->ops->solve             = MatSolve_SeqAIJ_InplaceWithPerm;
912   A->ops->solveadd          = MatSolveAdd_SeqAIJ_inplace;
913   A->ops->solvetranspose    = MatSolveTranspose_SeqAIJ_inplace;
914   A->ops->solvetransposeadd = MatSolveTransposeAdd_SeqAIJ_inplace;
915   A->assembled = PETSC_TRUE;
916   A->preallocated = PETSC_TRUE;
917   ierr = PetscLogFlops(A->cmap->n);CHKERRQ(ierr);
918   if (sctx.nshift){
919     if (info->shifttype == MAT_SHIFT_POSITIVE_DEFINITE) {
920       ierr = PetscInfo4(A,"number of shift_pd tries %D, shift_amount %G, diagonal shifted up by %e fraction top_value %e\n",sctx.nshift,sctx.shift_amount,sctx.shift_fraction,sctx.shift_top);CHKERRQ(ierr);
921     } else if (info->shifttype == MAT_SHIFT_NONZERO) {
922       ierr = PetscInfo2(A,"number of shift_nz tries %D, shift_amount %G\n",sctx.nshift,sctx.shift_amount);CHKERRQ(ierr);
923     }
924   }
925   PetscFunctionReturn(0);
926 }
927 
928 /* ----------------------------------------------------------- */
929 #undef __FUNCT__
930 #define __FUNCT__ "MatLUFactor_SeqAIJ"
931 PetscErrorCode MatLUFactor_SeqAIJ(Mat A,IS row,IS col,const MatFactorInfo *info)
932 {
933   PetscErrorCode ierr;
934   Mat            C;
935 
936   PetscFunctionBegin;
937   ierr = MatGetFactor(A,MAT_SOLVER_PETSC,MAT_FACTOR_LU,&C);CHKERRQ(ierr);
938   ierr = MatLUFactorSymbolic(C,A,row,col,info);CHKERRQ(ierr);
939   ierr = MatLUFactorNumeric(C,A,info);CHKERRQ(ierr);
940   A->ops->solve            = C->ops->solve;
941   A->ops->solvetranspose   = C->ops->solvetranspose;
942   ierr = MatHeaderCopy(A,C);CHKERRQ(ierr);
943   ierr = PetscLogObjectParent(A,((Mat_SeqAIJ*)(A->data))->icol);CHKERRQ(ierr);
944   PetscFunctionReturn(0);
945 }
946 /* ----------------------------------------------------------- */
947 
948 
949 #undef __FUNCT__
950 #define __FUNCT__ "MatSolve_SeqAIJ_inplace"
951 PetscErrorCode MatSolve_SeqAIJ_inplace(Mat A,Vec bb,Vec xx)
952 {
953   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
954   IS                iscol = a->col,isrow = a->row;
955   PetscErrorCode    ierr;
956   PetscInt          i, n = A->rmap->n,*vi,*ai = a->i,*aj = a->j;
957   PetscInt          nz;
958   const PetscInt    *rout,*cout,*r,*c;
959   PetscScalar       *x,*tmp,*tmps,sum;
960   const PetscScalar *b;
961   const MatScalar   *aa = a->a,*v;
962 
963   PetscFunctionBegin;
964   if (!n) PetscFunctionReturn(0);
965 
966   ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
967   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
968   tmp  = a->solve_work;
969 
970   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
971   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1);
972 
973   /* forward solve the lower triangular */
974   tmp[0] = b[*r++];
975   tmps   = tmp;
976   for (i=1; i<n; i++) {
977     v   = aa + ai[i] ;
978     vi  = aj + ai[i] ;
979     nz  = a->diag[i] - ai[i];
980     sum = b[*r++];
981     PetscSparseDenseMinusDot(sum,tmps,v,vi,nz);
982     tmp[i] = sum;
983   }
984 
985   /* backward solve the upper triangular */
986   for (i=n-1; i>=0; i--){
987     v   = aa + a->diag[i] + 1;
988     vi  = aj + a->diag[i] + 1;
989     nz  = ai[i+1] - a->diag[i] - 1;
990     sum = tmp[i];
991     PetscSparseDenseMinusDot(sum,tmps,v,vi,nz);
992     x[*c--] = tmp[i] = sum*aa[a->diag[i]];
993   }
994 
995   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
996   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
997   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
998   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
999   ierr = PetscLogFlops(2.0*a->nz - A->cmap->n);CHKERRQ(ierr);
1000   PetscFunctionReturn(0);
1001 }
1002 
1003 #undef __FUNCT__
1004 #define __FUNCT__ "MatMatSolve_SeqAIJ_inplace"
1005 PetscErrorCode MatMatSolve_SeqAIJ_inplace(Mat A,Mat B,Mat X)
1006 {
1007   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
1008   IS              iscol = a->col,isrow = a->row;
1009   PetscErrorCode  ierr;
1010   PetscInt        i, n = A->rmap->n,*vi,*ai = a->i,*aj = a->j;
1011   PetscInt        nz,neq;
1012   const PetscInt  *rout,*cout,*r,*c;
1013   PetscScalar     *x,*b,*tmp,*tmps,sum;
1014   const MatScalar *aa = a->a,*v;
1015   PetscTruth      bisdense,xisdense;
1016 
1017   PetscFunctionBegin;
1018   if (!n) PetscFunctionReturn(0);
1019 
1020   ierr = PetscTypeCompare((PetscObject)B,MATSEQDENSE,&bisdense);CHKERRQ(ierr);
1021   if (!bisdense) SETERRQ(PETSC_ERR_ARG_INCOMP,"B matrix must be a SeqDense matrix");
1022   ierr = PetscTypeCompare((PetscObject)X,MATSEQDENSE,&xisdense);CHKERRQ(ierr);
1023   if (!xisdense) SETERRQ(PETSC_ERR_ARG_INCOMP,"X matrix must be a SeqDense matrix");
1024 
1025   ierr = MatGetArray(B,&b);CHKERRQ(ierr);
1026   ierr = MatGetArray(X,&x);CHKERRQ(ierr);
1027 
1028   tmp  = a->solve_work;
1029   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
1030   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout;
1031 
1032   for (neq=0; neq<B->cmap->n; neq++){
1033     /* forward solve the lower triangular */
1034     tmp[0] = b[r[0]];
1035     tmps   = tmp;
1036     for (i=1; i<n; i++) {
1037       v   = aa + ai[i] ;
1038       vi  = aj + ai[i] ;
1039       nz  = a->diag[i] - ai[i];
1040       sum = b[r[i]];
1041       PetscSparseDenseMinusDot(sum,tmps,v,vi,nz);
1042       tmp[i] = sum;
1043     }
1044     /* backward solve the upper triangular */
1045     for (i=n-1; i>=0; i--){
1046       v   = aa + a->diag[i] + 1;
1047       vi  = aj + a->diag[i] + 1;
1048       nz  = ai[i+1] - a->diag[i] - 1;
1049       sum = tmp[i];
1050       PetscSparseDenseMinusDot(sum,tmps,v,vi,nz);
1051       x[c[i]] = tmp[i] = sum*aa[a->diag[i]];
1052     }
1053 
1054     b += n;
1055     x += n;
1056   }
1057   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
1058   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
1059   ierr = MatRestoreArray(B,&b);CHKERRQ(ierr);
1060   ierr = MatRestoreArray(X,&x);CHKERRQ(ierr);
1061   ierr = PetscLogFlops(B->cmap->n*(2.0*a->nz - n));CHKERRQ(ierr);
1062   PetscFunctionReturn(0);
1063 }
1064 
1065 #undef __FUNCT__
1066 #define __FUNCT__ "MatMatSolve_SeqAIJ"
1067 PetscErrorCode MatMatSolve_SeqAIJ(Mat A,Mat B,Mat X)
1068 {
1069   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
1070   IS              iscol = a->col,isrow = a->row;
1071   PetscErrorCode  ierr;
1072   PetscInt        i, n = A->rmap->n,*vi,*ai = a->i,*aj = a->j,*adiag = a->diag;
1073   PetscInt        nz,neq;
1074   const PetscInt  *rout,*cout,*r,*c;
1075   PetscScalar     *x,*b,*tmp,sum;
1076   const MatScalar *aa = a->a,*v;
1077   PetscTruth      bisdense,xisdense;
1078 
1079   PetscFunctionBegin;
1080   if (!n) PetscFunctionReturn(0);
1081 
1082   ierr = PetscTypeCompare((PetscObject)B,MATSEQDENSE,&bisdense);CHKERRQ(ierr);
1083   if (!bisdense) SETERRQ(PETSC_ERR_ARG_INCOMP,"B matrix must be a SeqDense matrix");
1084   ierr = PetscTypeCompare((PetscObject)X,MATSEQDENSE,&xisdense);CHKERRQ(ierr);
1085   if (!xisdense) SETERRQ(PETSC_ERR_ARG_INCOMP,"X matrix must be a SeqDense matrix");
1086 
1087   ierr = MatGetArray(B,&b);CHKERRQ(ierr);
1088   ierr = MatGetArray(X,&x);CHKERRQ(ierr);
1089 
1090   tmp  = a->solve_work;
1091   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
1092   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout;
1093 
1094   for (neq=0; neq<B->cmap->n; neq++){
1095     /* forward solve the lower triangular */
1096     tmp[0] = b[r[0]];
1097     v      = aa;
1098     vi     = aj;
1099     for (i=1; i<n; i++) {
1100       nz  = ai[i+1] - ai[i];
1101       sum = b[r[i]];
1102       PetscSparseDenseMinusDot(sum,tmp,v,vi,nz);
1103       tmp[i] = sum;
1104       v += nz; vi += nz;
1105     }
1106 
1107     /* backward solve the upper triangular */
1108     for (i=n-1; i>=0; i--){
1109       v   = aa + adiag[i+1]+1;
1110       vi  = aj + adiag[i+1]+1;
1111       nz  = adiag[i]-adiag[i+1]-1;
1112       sum = tmp[i];
1113       PetscSparseDenseMinusDot(sum,tmp,v,vi,nz);
1114       x[c[i]] = tmp[i] = sum*v[nz]; /* v[nz] = aa[adiag[i]] */
1115     }
1116 
1117     b += n;
1118     x += n;
1119   }
1120   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
1121   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
1122   ierr = MatRestoreArray(B,&b);CHKERRQ(ierr);
1123   ierr = MatRestoreArray(X,&x);CHKERRQ(ierr);
1124   ierr = PetscLogFlops(B->cmap->n*(2.0*a->nz - n));CHKERRQ(ierr);
1125   PetscFunctionReturn(0);
1126 }
1127 
1128 #undef __FUNCT__
1129 #define __FUNCT__ "MatSolve_SeqAIJ_InplaceWithPerm"
1130 PetscErrorCode MatSolve_SeqAIJ_InplaceWithPerm(Mat A,Vec bb,Vec xx)
1131 {
1132   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
1133   IS              iscol = a->col,isrow = a->row;
1134   PetscErrorCode  ierr;
1135   const PetscInt  *r,*c,*rout,*cout;
1136   PetscInt        i, n = A->rmap->n,*vi,*ai = a->i,*aj = a->j;
1137   PetscInt        nz,row;
1138   PetscScalar     *x,*b,*tmp,*tmps,sum;
1139   const MatScalar *aa = a->a,*v;
1140 
1141   PetscFunctionBegin;
1142   if (!n) PetscFunctionReturn(0);
1143 
1144   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
1145   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1146   tmp  = a->solve_work;
1147 
1148   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
1149   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1);
1150 
1151   /* forward solve the lower triangular */
1152   tmp[0] = b[*r++];
1153   tmps   = tmp;
1154   for (row=1; row<n; row++) {
1155     i   = rout[row]; /* permuted row */
1156     v   = aa + ai[i] ;
1157     vi  = aj + ai[i] ;
1158     nz  = a->diag[i] - ai[i];
1159     sum = b[*r++];
1160     PetscSparseDenseMinusDot(sum,tmps,v,vi,nz);
1161     tmp[row] = sum;
1162   }
1163 
1164   /* backward solve the upper triangular */
1165   for (row=n-1; row>=0; row--){
1166     i   = rout[row]; /* permuted row */
1167     v   = aa + a->diag[i] + 1;
1168     vi  = aj + a->diag[i] + 1;
1169     nz  = ai[i+1] - a->diag[i] - 1;
1170     sum = tmp[row];
1171     PetscSparseDenseMinusDot(sum,tmps,v,vi,nz);
1172     x[*c--] = tmp[row] = sum*aa[a->diag[i]];
1173   }
1174 
1175   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
1176   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
1177   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
1178   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1179   ierr = PetscLogFlops(2.0*a->nz - A->cmap->n);CHKERRQ(ierr);
1180   PetscFunctionReturn(0);
1181 }
1182 
1183 /* ----------------------------------------------------------- */
1184 #include "../src/mat/impls/aij/seq/ftn-kernels/fsolve.h"
1185 #undef __FUNCT__
1186 #define __FUNCT__ "MatSolve_SeqAIJ_NaturalOrdering_inplace"
1187 PetscErrorCode MatSolve_SeqAIJ_NaturalOrdering_inplace(Mat A,Vec bb,Vec xx)
1188 {
1189   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1190   PetscErrorCode    ierr;
1191   PetscInt          n = A->rmap->n;
1192   const PetscInt    *ai = a->i,*aj = a->j,*adiag = a->diag;
1193   PetscScalar       *x;
1194   const PetscScalar *b;
1195   const MatScalar   *aa = a->a;
1196 #if !defined(PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ)
1197   PetscInt          adiag_i,i,nz,ai_i;
1198   const PetscInt    *vi;
1199   const MatScalar   *v;
1200   PetscScalar       sum;
1201 #endif
1202 
1203   PetscFunctionBegin;
1204   if (!n) PetscFunctionReturn(0);
1205 
1206   ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1207   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1208 
1209 #if defined(PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ)
1210   fortransolveaij_(&n,x,ai,aj,adiag,aa,b);
1211 #else
1212   /* forward solve the lower triangular */
1213   x[0] = b[0];
1214   for (i=1; i<n; i++) {
1215     ai_i = ai[i];
1216     v    = aa + ai_i;
1217     vi   = aj + ai_i;
1218     nz   = adiag[i] - ai_i;
1219     sum  = b[i];
1220     PetscSparseDenseMinusDot(sum,x,v,vi,nz);
1221     x[i] = sum;
1222   }
1223 
1224   /* backward solve the upper triangular */
1225   for (i=n-1; i>=0; i--){
1226     adiag_i = adiag[i];
1227     v       = aa + adiag_i + 1;
1228     vi      = aj + adiag_i + 1;
1229     nz      = ai[i+1] - adiag_i - 1;
1230     sum     = x[i];
1231     PetscSparseDenseMinusDot(sum,x,v,vi,nz);
1232     x[i]    = sum*aa[adiag_i];
1233   }
1234 #endif
1235   ierr = PetscLogFlops(2.0*a->nz - A->cmap->n);CHKERRQ(ierr);
1236   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1237   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1238   PetscFunctionReturn(0);
1239 }
1240 
1241 #undef __FUNCT__
1242 #define __FUNCT__ "MatSolveAdd_SeqAIJ_inplace"
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;
1254 
1255   PetscFunctionBegin;
1256   if (yy != xx) {ierr = VecCopy(yy,xx);CHKERRQ(ierr);}
1257 
1258   ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1259   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1260   tmp  = a->solve_work;
1261 
1262   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
1263   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1);
1264 
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   }
1275 
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   }
1286 
1287   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
1288   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
1289   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1290   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1291   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1292 
1293   PetscFunctionReturn(0);
1294 }
1295 
1296 #undef __FUNCT__
1297 #define __FUNCT__ "MatSolveAdd_SeqAIJ"
1298 PetscErrorCode MatSolveAdd_SeqAIJ(Mat A,Vec bb,Vec yy,Vec xx)
1299 {
1300   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1301   IS                iscol = a->col,isrow = a->row;
1302   PetscErrorCode    ierr;
1303   PetscInt          i, n = A->rmap->n,j;
1304   PetscInt          nz;
1305   const PetscInt    *rout,*cout,*r,*c,*vi,*ai = a->i,*aj = a->j,*adiag = a->diag;
1306   PetscScalar       *x,*tmp,sum;
1307   const PetscScalar *b;
1308   const MatScalar   *aa = a->a,*v;
1309 
1310   PetscFunctionBegin;
1311   if (yy != xx) {ierr = VecCopy(yy,xx);CHKERRQ(ierr);}
1312 
1313   ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1314   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1315   tmp  = a->solve_work;
1316 
1317   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
1318   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout;
1319 
1320   /* forward solve the lower triangular */
1321   tmp[0] = b[r[0]];
1322   v      = aa;
1323   vi     = aj;
1324   for (i=1; i<n; i++) {
1325     nz  = ai[i+1] - ai[i];
1326     sum = b[r[i]];
1327     for (j=0; j<nz; j++) sum -= v[j]*tmp[vi[j]];
1328     tmp[i] = sum;
1329     v += nz; vi += nz;
1330   }
1331 
1332   /* backward solve the upper triangular */
1333   v  = aa + adiag[n-1];
1334   vi = aj + adiag[n-1];
1335   for (i=n-1; i>=0; i--){
1336     nz  = adiag[i] - adiag[i+1] - 1;
1337     sum = tmp[i];
1338     for (j=0; j<nz; j++) sum -= v[j]*tmp[vi[j]];
1339     tmp[i] = sum*v[nz];
1340     x[c[i]] += tmp[i];
1341     v += nz+1; vi += nz+1;
1342   }
1343 
1344   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
1345   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
1346   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1347   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1348   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1349 
1350   PetscFunctionReturn(0);
1351 }
1352 
1353 #undef __FUNCT__
1354 #define __FUNCT__ "MatSolveTranspose_SeqAIJ_inplace"
1355 PetscErrorCode MatSolveTranspose_SeqAIJ_inplace(Mat A,Vec bb,Vec xx)
1356 {
1357   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1358   IS                iscol = a->col,isrow = a->row;
1359   PetscErrorCode    ierr;
1360   const PetscInt    *rout,*cout,*r,*c,*diag = a->diag,*ai = a->i,*aj = a->j,*vi;
1361   PetscInt          i,n = A->rmap->n,j;
1362   PetscInt          nz;
1363   PetscScalar       *x,*tmp,s1;
1364   const MatScalar   *aa = a->a,*v;
1365   const PetscScalar *b;
1366 
1367   PetscFunctionBegin;
1368   ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1369   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1370   tmp  = a->solve_work;
1371 
1372   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
1373   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout;
1374 
1375   /* copy the b into temp work space according to permutation */
1376   for (i=0; i<n; i++) tmp[i] = b[c[i]];
1377 
1378   /* forward solve the U^T */
1379   for (i=0; i<n; i++) {
1380     v   = aa + diag[i] ;
1381     vi  = aj + diag[i] + 1;
1382     nz  = ai[i+1] - diag[i] - 1;
1383     s1  = tmp[i];
1384     s1 *= (*v++);  /* multiply by inverse of diagonal entry */
1385     for (j=0; j<nz; j++) tmp[vi[j]] -= s1*v[j];
1386     tmp[i] = s1;
1387   }
1388 
1389   /* backward solve the L^T */
1390   for (i=n-1; i>=0; i--){
1391     v   = aa + diag[i] - 1 ;
1392     vi  = aj + diag[i] - 1 ;
1393     nz  = diag[i] - ai[i];
1394     s1  = tmp[i];
1395     for (j=0; j>-nz; j--) tmp[vi[j]] -= s1*v[j];
1396   }
1397 
1398   /* copy tmp into x according to permutation */
1399   for (i=0; i<n; i++) x[r[i]] = tmp[i];
1400 
1401   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
1402   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
1403   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1404   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1405 
1406   ierr = PetscLogFlops(2.0*a->nz-A->cmap->n);CHKERRQ(ierr);
1407   PetscFunctionReturn(0);
1408 }
1409 
1410 #undef __FUNCT__
1411 #define __FUNCT__ "MatSolveTranspose_SeqAIJ"
1412 PetscErrorCode MatSolveTranspose_SeqAIJ(Mat A,Vec bb,Vec xx)
1413 {
1414   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1415   IS                iscol = a->col,isrow = a->row;
1416   PetscErrorCode    ierr;
1417   const PetscInt    *rout,*cout,*r,*c,*adiag = a->diag,*ai = a->i,*aj = a->j,*vi;
1418   PetscInt          i,n = A->rmap->n,j;
1419   PetscInt          nz;
1420   PetscScalar       *x,*tmp,s1;
1421   const MatScalar   *aa = a->a,*v;
1422   const PetscScalar *b;
1423 
1424   PetscFunctionBegin;
1425   ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1426   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1427   tmp  = a->solve_work;
1428 
1429   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
1430   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout;
1431 
1432   /* copy the b into temp work space according to permutation */
1433   for (i=0; i<n; i++) tmp[i] = b[c[i]];
1434 
1435   /* forward solve the U^T */
1436   for (i=0; i<n; i++) {
1437     v   = aa + adiag[i+1] + 1;
1438     vi  = aj + adiag[i+1] + 1;
1439     nz  = adiag[i] - adiag[i+1] - 1;
1440     s1  = tmp[i];
1441     s1 *= v[nz];  /* multiply by inverse of diagonal entry */
1442     for (j=0; j<nz; j++) tmp[vi[j]] -= s1*v[j];
1443     tmp[i] = s1;
1444   }
1445 
1446   /* backward solve the L^T */
1447   for (i=n-1; i>=0; i--){
1448     v   = aa + ai[i];
1449     vi  = aj + ai[i];
1450     nz  = ai[i+1] - ai[i];
1451     s1  = tmp[i];
1452     for (j=0; j<nz; j++) tmp[vi[j]] -= s1*v[j];
1453   }
1454 
1455   /* copy tmp into x according to permutation */
1456   for (i=0; i<n; i++) x[r[i]] = tmp[i];
1457 
1458   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
1459   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
1460   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1461   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1462 
1463   ierr = PetscLogFlops(2.0*a->nz-A->cmap->n);CHKERRQ(ierr);
1464   PetscFunctionReturn(0);
1465 }
1466 
1467 #undef __FUNCT__
1468 #define __FUNCT__ "MatSolveTransposeAdd_SeqAIJ_inplace"
1469 PetscErrorCode MatSolveTransposeAdd_SeqAIJ_inplace(Mat A,Vec bb,Vec zz,Vec xx)
1470 {
1471   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1472   IS                iscol = a->col,isrow = a->row;
1473   PetscErrorCode    ierr;
1474   const PetscInt    *rout,*cout,*r,*c,*diag = a->diag,*ai = a->i,*aj = a->j,*vi;
1475   PetscInt          i,n = A->rmap->n,j;
1476   PetscInt          nz;
1477   PetscScalar       *x,*tmp,s1;
1478   const MatScalar   *aa = a->a,*v;
1479   const PetscScalar *b;
1480 
1481   PetscFunctionBegin;
1482   if (zz != xx) {ierr = VecCopy(zz,xx);CHKERRQ(ierr);}
1483   ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1484   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1485   tmp  = a->solve_work;
1486 
1487   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
1488   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout;
1489 
1490   /* copy the b into temp work space according to permutation */
1491   for (i=0; i<n; i++) tmp[i] = b[c[i]];
1492 
1493   /* forward solve the U^T */
1494   for (i=0; i<n; i++) {
1495     v   = aa + diag[i] ;
1496     vi  = aj + diag[i] + 1;
1497     nz  = ai[i+1] - diag[i] - 1;
1498     s1  = tmp[i];
1499     s1 *= (*v++);  /* multiply by inverse of diagonal entry */
1500     for (j=0; j<nz; j++) tmp[vi[j]] -= s1*v[j];
1501     tmp[i] = s1;
1502   }
1503 
1504   /* backward solve the L^T */
1505   for (i=n-1; i>=0; i--){
1506     v   = aa + diag[i] - 1 ;
1507     vi  = aj + diag[i] - 1 ;
1508     nz  = diag[i] - ai[i];
1509     s1  = tmp[i];
1510     for (j=0; j>-nz; j--) tmp[vi[j]] -= s1*v[j];
1511   }
1512 
1513   /* copy tmp into x according to permutation */
1514   for (i=0; i<n; i++) x[r[i]] += tmp[i];
1515 
1516   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
1517   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
1518   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1519   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1520 
1521   ierr = PetscLogFlops(2.0*a->nz-A->cmap->n);CHKERRQ(ierr);
1522   PetscFunctionReturn(0);
1523 }
1524 
1525 #undef __FUNCT__
1526 #define __FUNCT__ "MatSolveTransposeAdd_SeqAIJ"
1527 PetscErrorCode MatSolveTransposeAdd_SeqAIJ(Mat A,Vec bb,Vec zz,Vec xx)
1528 {
1529   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1530   IS                iscol = a->col,isrow = a->row;
1531   PetscErrorCode    ierr;
1532   const PetscInt    *rout,*cout,*r,*c,*adiag = a->diag,*ai = a->i,*aj = a->j,*vi;
1533   PetscInt          i,n = A->rmap->n,j;
1534   PetscInt          nz;
1535   PetscScalar       *x,*tmp,s1;
1536   const MatScalar   *aa = a->a,*v;
1537   const PetscScalar *b;
1538 
1539   PetscFunctionBegin;
1540   if (zz != xx) {ierr = VecCopy(zz,xx);CHKERRQ(ierr);}
1541   ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1542   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1543   tmp  = a->solve_work;
1544 
1545   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
1546   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout;
1547 
1548   /* copy the b into temp work space according to permutation */
1549   for (i=0; i<n; i++) tmp[i] = b[c[i]];
1550 
1551   /* forward solve the U^T */
1552   for (i=0; i<n; i++) {
1553     v   = aa + adiag[i+1] + 1;
1554     vi  = aj + adiag[i+1] + 1;
1555     nz  = adiag[i] - adiag[i+1] - 1;
1556     s1  = tmp[i];
1557     s1 *= v[nz];  /* multiply by inverse of diagonal entry */
1558     for (j=0; j<nz; j++) tmp[vi[j]] -= s1*v[j];
1559     tmp[i] = s1;
1560   }
1561 
1562 
1563   /* backward solve the L^T */
1564   for (i=n-1; i>=0; i--){
1565     v   = aa + ai[i] ;
1566     vi  = aj + ai[i];
1567     nz  = ai[i+1] - ai[i];
1568     s1  = tmp[i];
1569     for (j=0; j<nz; j++) tmp[vi[j]] -= s1*v[j];
1570   }
1571 
1572   /* copy tmp into x according to permutation */
1573   for (i=0; i<n; i++) x[r[i]] += tmp[i];
1574 
1575   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
1576   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
1577   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1578   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1579 
1580   ierr = PetscLogFlops(2.0*a->nz-A->cmap->n);CHKERRQ(ierr);
1581   PetscFunctionReturn(0);
1582 }
1583 
1584 /* ----------------------------------------------------------------*/
1585 
1586 EXTERN PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat,Mat,MatDuplicateOption,PetscTruth);
1587 
1588 /*
1589    ilu() under revised new data structure.
1590    Factored arrays bj and ba are stored as
1591      L(0,:), L(1,:), ...,L(n-1,:),  U(n-1,:),...,U(i,:),U(i-1,:),...,U(0,:)
1592 
1593    bi=fact->i is an array of size n+1, in which
1594    bi+
1595      bi[i]:  points to 1st entry of L(i,:),i=0,...,n-1
1596      bi[n]:  points to L(n-1,n-1)+1
1597 
1598   bdiag=fact->diag is an array of size n+1,in which
1599      bdiag[i]: points to diagonal of U(i,:), i=0,...,n-1
1600      bdiag[n]: points to entry of U(n-1,0)-1
1601 
1602    U(i,:) contains bdiag[i] as its last entry, i.e.,
1603     U(i,:) = (u[i,i+1],...,u[i,n-1],diag[i])
1604 */
1605 #undef __FUNCT__
1606 #define __FUNCT__ "MatILUFactorSymbolic_SeqAIJ_ilu0"
1607 PetscErrorCode MatILUFactorSymbolic_SeqAIJ_ilu0(Mat fact,Mat A,IS isrow,IS iscol,const MatFactorInfo *info)
1608 {
1609 
1610   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data,*b;
1611   PetscErrorCode     ierr;
1612   const PetscInt     n=A->rmap->n,*ai=a->i,*aj,*adiag=a->diag;
1613   PetscInt           i,j,k=0,nz,*bi,*bj,*bdiag;
1614   PetscTruth         missing;
1615   IS                 isicol;
1616 
1617   PetscFunctionBegin;
1618   if (A->rmap->n != A->cmap->n) SETERRQ2(PETSC_ERR_ARG_WRONG,"Must be square matrix, rows %D columns %D",A->rmap->n,A->cmap->n);
1619   ierr = MatMissingDiagonal(A,&missing,&i);CHKERRQ(ierr);
1620   if (missing) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",i);
1621   ierr = ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);CHKERRQ(ierr);
1622 
1623   ierr = MatDuplicateNoCreate_SeqAIJ(fact,A,MAT_DO_NOT_COPY_VALUES,PETSC_FALSE);CHKERRQ(ierr);
1624   b    = (Mat_SeqAIJ*)(fact)->data;
1625 
1626   /* allocate matrix arrays for new data structure */
1627   ierr = PetscMalloc3(ai[n]+1,PetscScalar,&b->a,ai[n]+1,PetscInt,&b->j,n+1,PetscInt,&b->i);CHKERRQ(ierr);
1628   ierr = PetscLogObjectMemory(fact,ai[n]*(sizeof(PetscScalar)+sizeof(PetscInt))+(n+1)*sizeof(PetscInt));CHKERRQ(ierr);
1629   b->singlemalloc = PETSC_TRUE;
1630   if (!b->diag){
1631     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&b->diag);CHKERRQ(ierr);
1632     ierr = PetscLogObjectMemory(fact,(n+1)*sizeof(PetscInt));CHKERRQ(ierr);
1633   }
1634   bdiag = b->diag;
1635 
1636   if (n > 0) {
1637     ierr = PetscMemzero(b->a,(ai[n])*sizeof(MatScalar));CHKERRQ(ierr);
1638   }
1639 
1640   /* set bi and bj with new data structure */
1641   bi = b->i;
1642   bj = b->j;
1643 
1644   /* L part */
1645   bi[0] = 0;
1646   for (i=0; i<n; i++){
1647     nz = adiag[i] - ai[i];
1648     bi[i+1] = bi[i] + nz;
1649     aj = a->j + ai[i];
1650     for (j=0; j<nz; j++){
1651       /*   *bj = aj[j]; bj++; */
1652       bj[k++] = aj[j];
1653     }
1654   }
1655 
1656   /* U part */
1657   bdiag[n] = bi[n]-1;
1658   for (i=n-1; i>=0; i--){
1659     nz = ai[i+1] - adiag[i] - 1;
1660     aj = a->j + adiag[i] + 1;
1661     for (j=0; j<nz; j++){
1662       /*      *bj = aj[j]; bj++; */
1663       bj[k++] = aj[j];
1664     }
1665     /* diag[i] */
1666     /*    *bj = i; bj++; */
1667     bj[k++] = i;
1668     bdiag[i] = bdiag[i+1] + nz + 1;
1669   }
1670 
1671   fact->factor                 = MAT_FACTOR_ILU;
1672   fact->info.factor_mallocs    = 0;
1673   fact->info.fill_ratio_given  = info->fill;
1674   fact->info.fill_ratio_needed = 1.0;
1675   fact->ops->lufactornumeric   = MatLUFactorNumeric_SeqAIJ;
1676 
1677   b       = (Mat_SeqAIJ*)(fact)->data;
1678   b->row  = isrow;
1679   b->col  = iscol;
1680   b->icol = isicol;
1681   ierr    = PetscMalloc((fact->rmap->n+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr);
1682   ierr    = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr);
1683   ierr    = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr);
1684   PetscFunctionReturn(0);
1685 }
1686 
1687 #undef __FUNCT__
1688 #define __FUNCT__ "MatILUFactorSymbolic_SeqAIJ"
1689 PetscErrorCode MatILUFactorSymbolic_SeqAIJ(Mat fact,Mat A,IS isrow,IS iscol,const MatFactorInfo *info)
1690 {
1691   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data,*b;
1692   IS                 isicol;
1693   PetscErrorCode     ierr;
1694   const PetscInt     *r,*ic;
1695   PetscInt           n=A->rmap->n,*ai=a->i,*aj=a->j;
1696   PetscInt           *bi,*cols,nnz,*cols_lvl;
1697   PetscInt           *bdiag,prow,fm,nzbd,reallocs=0,dcount=0;
1698   PetscInt           i,levels,diagonal_fill;
1699   PetscTruth         col_identity,row_identity;
1700   PetscReal          f;
1701   PetscInt           nlnk,*lnk,*lnk_lvl=PETSC_NULL;
1702   PetscBT            lnkbt;
1703   PetscInt           nzi,*bj,**bj_ptr,**bjlvl_ptr;
1704   PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL;
1705   PetscFreeSpaceList free_space_lvl=PETSC_NULL,current_space_lvl=PETSC_NULL;
1706 
1707   PetscFunctionBegin;
1708   /* // Testing new data structure for MatSolve()
1709   PetscTruth         olddatastruct=PETSC_FALSE
1710   ierr = PetscOptionsGetTruth(PETSC_NULL,"-ilu_old",&olddatastruct,PETSC_NULL);CHKERRQ(ierr);
1711   if(olddatastruct){
1712     ierr = MatILUFactorSymbolic_SeqAIJ_inplace(fact,A,isrow,iscol,info);CHKERRQ(ierr);
1713     PetscFunctionReturn(0);
1714   }
1715   */
1716 
1717   levels = (PetscInt)info->levels;
1718   ierr   = ISIdentity(isrow,&row_identity);CHKERRQ(ierr);
1719   ierr   = ISIdentity(iscol,&col_identity);CHKERRQ(ierr);
1720 
1721   if (!levels && row_identity && col_identity) {
1722     /* special case: ilu(0) with natural ordering */
1723     ierr = MatILUFactorSymbolic_SeqAIJ_ilu0(fact,A,isrow,iscol,info);CHKERRQ(ierr);
1724     ierr = Mat_CheckInode_FactorLU(fact,PETSC_FALSE);CHKERRQ(ierr);
1725     PetscFunctionReturn(0);
1726   }
1727 
1728   if (A->rmap->n != A->cmap->n) SETERRQ2(PETSC_ERR_ARG_WRONG,"Must be square matrix, rows %D columns %D",A->rmap->n,A->cmap->n);
1729   ierr = ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);CHKERRQ(ierr);
1730   ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr);
1731   ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr);
1732 
1733   /* get new row and diagonal pointers, must be allocated separately because they will be given to the Mat_SeqAIJ and freed separately */
1734   ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bi);CHKERRQ(ierr);
1735   ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bdiag);CHKERRQ(ierr);
1736   bi[0] = bdiag[0] = 0;
1737 
1738   ierr = PetscMalloc2(n,PetscInt*,&bj_ptr,n,PetscInt*,&bjlvl_ptr);CHKERRQ(ierr);
1739 
1740   /* create a linked list for storing column indices of the active row */
1741   nlnk = n + 1;
1742   ierr = PetscIncompleteLLCreate(n,n,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr);
1743 
1744   /* initial FreeSpace size is f*(ai[n]+1) */
1745   f             = info->fill;
1746   diagonal_fill = (PetscInt)info->diagonal_fill;
1747   ierr = PetscFreeSpaceGet((PetscInt)(f*(ai[n]+1)),&free_space);CHKERRQ(ierr);
1748   current_space = free_space;
1749   ierr = PetscFreeSpaceGet((PetscInt)(f*(ai[n]+1)),&free_space_lvl);CHKERRQ(ierr);
1750   current_space_lvl = free_space_lvl;
1751 
1752   for (i=0; i<n; i++) {
1753     nzi = 0;
1754     /* copy current row into linked list */
1755     nnz  = ai[r[i]+1] - ai[r[i]];
1756     if (!nnz) SETERRQ2(PETSC_ERR_MAT_LU_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",r[i],i);
1757     cols = aj + ai[r[i]];
1758     lnk[i] = -1; /* marker to indicate if diagonal exists */
1759     ierr = PetscIncompleteLLInit(nnz,cols,n,ic,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr);
1760     nzi += nlnk;
1761 
1762     /* make sure diagonal entry is included */
1763     if (diagonal_fill && lnk[i] == -1) {
1764       fm = n;
1765       while (lnk[fm] < i) fm = lnk[fm];
1766       lnk[i]     = lnk[fm]; /* insert diagonal into linked list */
1767       lnk[fm]    = i;
1768       lnk_lvl[i] = 0;
1769       nzi++; dcount++;
1770     }
1771 
1772     /* add pivot rows into the active row */
1773     nzbd = 0;
1774     prow = lnk[n];
1775     while (prow < i) {
1776       nnz      = bdiag[prow];
1777       cols     = bj_ptr[prow] + nnz + 1;
1778       cols_lvl = bjlvl_ptr[prow] + nnz + 1;
1779       nnz      = bi[prow+1] - bi[prow] - nnz - 1;
1780       ierr = PetscILULLAddSorted(nnz,cols,levels,cols_lvl,prow,nlnk,lnk,lnk_lvl,lnkbt,prow);CHKERRQ(ierr);
1781       nzi += nlnk;
1782       prow = lnk[prow];
1783       nzbd++;
1784     }
1785     bdiag[i] = nzbd;
1786     bi[i+1]  = bi[i] + nzi;
1787 
1788     /* if free space is not available, make more free space */
1789     if (current_space->local_remaining<nzi) {
1790       nnz = 2*nzi*(n - i); /* estimated and max additional space needed */
1791       ierr = PetscFreeSpaceGet(nnz,&current_space);CHKERRQ(ierr);
1792       ierr = PetscFreeSpaceGet(nnz,&current_space_lvl);CHKERRQ(ierr);
1793       reallocs++;
1794     }
1795 
1796     /* copy data into free_space and free_space_lvl, then initialize lnk */
1797     ierr = PetscIncompleteLLClean(n,n,nzi,lnk,lnk_lvl,current_space->array,current_space_lvl->array,lnkbt);CHKERRQ(ierr);
1798     bj_ptr[i]    = current_space->array;
1799     bjlvl_ptr[i] = current_space_lvl->array;
1800 
1801     /* make sure the active row i has diagonal entry */
1802     if (*(bj_ptr[i]+bdiag[i]) != i) {
1803       SETERRQ1(PETSC_ERR_MAT_LU_ZRPVT,"Row %D has missing diagonal in factored matrix\n\
1804     try running with -pc_factor_nonzeros_along_diagonal or -pc_factor_diagonal_fill",i);
1805     }
1806 
1807     current_space->array           += nzi;
1808     current_space->local_used      += nzi;
1809     current_space->local_remaining -= nzi;
1810     current_space_lvl->array           += nzi;
1811     current_space_lvl->local_used      += nzi;
1812     current_space_lvl->local_remaining -= nzi;
1813   }
1814 
1815   ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr);
1816   ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr);
1817 
1818   /* destroy list of free space and other temporary arrays */
1819   ierr = PetscMalloc((bi[n]+1)*sizeof(PetscInt),&bj);CHKERRQ(ierr);
1820 
1821   /* copy free_space into bj and free free_space; set bi, bj, bdiag in new datastructure; */
1822   ierr = PetscFreeSpaceContiguous_LU(&free_space,bj,n,bi,bdiag);CHKERRQ(ierr);
1823 
1824   ierr = PetscIncompleteLLDestroy(lnk,lnkbt);CHKERRQ(ierr);
1825   ierr = PetscFreeSpaceDestroy(free_space_lvl);CHKERRQ(ierr);
1826   ierr = PetscFree2(bj_ptr,bjlvl_ptr);CHKERRQ(ierr);
1827 
1828 #if defined(PETSC_USE_INFO)
1829   {
1830     PetscReal af = ((PetscReal)bi[n])/((PetscReal)ai[n]);
1831     ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocs,f,af);CHKERRQ(ierr);
1832     ierr = PetscInfo1(A,"Run with -[sub_]pc_factor_fill %G or use \n",af);CHKERRQ(ierr);
1833     ierr = PetscInfo1(A,"PCFactorSetFill([sub]pc,%G);\n",af);CHKERRQ(ierr);
1834     ierr = PetscInfo(A,"for best performance.\n");CHKERRQ(ierr);
1835     if (diagonal_fill) {
1836       ierr = PetscInfo1(A,"Detected and replaced %D missing diagonals",dcount);CHKERRQ(ierr);
1837     }
1838   }
1839 #endif
1840 
1841   /* put together the new matrix */
1842   ierr = MatSeqAIJSetPreallocation_SeqAIJ(fact,MAT_SKIP_ALLOCATION,PETSC_NULL);CHKERRQ(ierr);
1843   ierr = PetscLogObjectParent(fact,isicol);CHKERRQ(ierr);
1844   b = (Mat_SeqAIJ*)(fact)->data;
1845   b->free_a       = PETSC_TRUE;
1846   b->free_ij      = PETSC_TRUE;
1847   b->singlemalloc = PETSC_FALSE;
1848   ierr = PetscMalloc((bdiag[0]+1)*sizeof(PetscScalar),&b->a);CHKERRQ(ierr);
1849   b->j          = bj;
1850   b->i          = bi;
1851   b->diag       = bdiag;
1852   b->ilen       = 0;
1853   b->imax       = 0;
1854   b->row        = isrow;
1855   b->col        = iscol;
1856   ierr          = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr);
1857   ierr          = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr);
1858   b->icol       = isicol;
1859   ierr = PetscMalloc((n+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr);
1860   /* In b structure:  Free imax, ilen, old a, old j.
1861      Allocate bdiag, solve_work, new a, new j */
1862   ierr = PetscLogObjectMemory(fact,(bdiag[0]+1)*(sizeof(PetscInt)+sizeof(PetscScalar)));CHKERRQ(ierr);
1863   b->maxnz = b->nz = bdiag[0]+1;
1864   (fact)->info.factor_mallocs    = reallocs;
1865   (fact)->info.fill_ratio_given  = f;
1866   (fact)->info.fill_ratio_needed = ((PetscReal)(bdiag[0]+1))/((PetscReal)ai[n]);
1867   (fact)->ops->lufactornumeric = MatLUFactorNumeric_SeqAIJ;
1868   ierr = Mat_CheckInode_FactorLU(fact,PETSC_FALSE);CHKERRQ(ierr);
1869   PetscFunctionReturn(0);
1870 }
1871 
1872 #undef __FUNCT__
1873 #define __FUNCT__ "MatILUFactorSymbolic_SeqAIJ_inplace"
1874 PetscErrorCode MatILUFactorSymbolic_SeqAIJ_inplace(Mat fact,Mat A,IS isrow,IS iscol,const MatFactorInfo *info)
1875 {
1876   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data,*b;
1877   IS                 isicol;
1878   PetscErrorCode     ierr;
1879   const PetscInt     *r,*ic;
1880   PetscInt           n=A->rmap->n,*ai=a->i,*aj=a->j,d;
1881   PetscInt           *bi,*cols,nnz,*cols_lvl;
1882   PetscInt           *bdiag,prow,fm,nzbd,reallocs=0,dcount=0;
1883   PetscInt           i,levels,diagonal_fill;
1884   PetscTruth         col_identity,row_identity;
1885   PetscReal          f;
1886   PetscInt           nlnk,*lnk,*lnk_lvl=PETSC_NULL;
1887   PetscBT            lnkbt;
1888   PetscInt           nzi,*bj,**bj_ptr,**bjlvl_ptr;
1889   PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL;
1890   PetscFreeSpaceList free_space_lvl=PETSC_NULL,current_space_lvl=PETSC_NULL;
1891   PetscTruth         missing;
1892 
1893   PetscFunctionBegin;
1894   if (A->rmap->n != A->cmap->n) SETERRQ2(PETSC_ERR_ARG_WRONG,"Must be square matrix, rows %D columns %D",A->rmap->n,A->cmap->n);
1895   f             = info->fill;
1896   levels        = (PetscInt)info->levels;
1897   diagonal_fill = (PetscInt)info->diagonal_fill;
1898   ierr = ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);CHKERRQ(ierr);
1899 
1900   ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr);
1901   ierr = ISIdentity(iscol,&col_identity);CHKERRQ(ierr);
1902   if (!levels && row_identity && col_identity) { /* special case: ilu(0) with natural ordering */
1903     ierr = MatDuplicateNoCreate_SeqAIJ(fact,A,MAT_DO_NOT_COPY_VALUES,PETSC_TRUE);CHKERRQ(ierr);
1904     (fact)->ops->lufactornumeric =  MatLUFactorNumeric_SeqAIJ_inplace;
1905 
1906     fact->factor = MAT_FACTOR_ILU;
1907     (fact)->info.factor_mallocs    = 0;
1908     (fact)->info.fill_ratio_given  = info->fill;
1909     (fact)->info.fill_ratio_needed = 1.0;
1910     b               = (Mat_SeqAIJ*)(fact)->data;
1911     ierr = MatMissingDiagonal(A,&missing,&d);CHKERRQ(ierr);
1912     if (missing) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",d);
1913     b->row              = isrow;
1914     b->col              = iscol;
1915     b->icol             = isicol;
1916     ierr                = PetscMalloc(((fact)->rmap->n+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr);
1917     ierr                = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr);
1918     ierr                = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr);
1919     ierr = MatILUFactorSymbolic_SeqAIJ_Inode(fact,A,isrow,iscol,info);CHKERRQ(ierr);
1920     PetscFunctionReturn(0);
1921   }
1922 
1923   ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr);
1924   ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr);
1925 
1926   /* get new row and diagonal pointers, must be allocated separately because they will be given to the Mat_SeqAIJ and freed separately */
1927   ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bi);CHKERRQ(ierr);
1928   ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bdiag);CHKERRQ(ierr);
1929   bi[0] = bdiag[0] = 0;
1930 
1931   ierr = PetscMalloc2(n,PetscInt*,&bj_ptr,n,PetscInt*,&bjlvl_ptr);CHKERRQ(ierr);
1932 
1933   /* create a linked list for storing column indices of the active row */
1934   nlnk = n + 1;
1935   ierr = PetscIncompleteLLCreate(n,n,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr);
1936 
1937   /* initial FreeSpace size is f*(ai[n]+1) */
1938   ierr = PetscFreeSpaceGet((PetscInt)(f*(ai[n]+1)),&free_space);CHKERRQ(ierr);
1939   current_space = free_space;
1940   ierr = PetscFreeSpaceGet((PetscInt)(f*(ai[n]+1)),&free_space_lvl);CHKERRQ(ierr);
1941   current_space_lvl = free_space_lvl;
1942 
1943   for (i=0; i<n; i++) {
1944     nzi = 0;
1945     /* copy current row into linked list */
1946     nnz  = ai[r[i]+1] - ai[r[i]];
1947     if (!nnz) SETERRQ2(PETSC_ERR_MAT_LU_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",r[i],i);
1948     cols = aj + ai[r[i]];
1949     lnk[i] = -1; /* marker to indicate if diagonal exists */
1950     ierr = PetscIncompleteLLInit(nnz,cols,n,ic,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr);
1951     nzi += nlnk;
1952 
1953     /* make sure diagonal entry is included */
1954     if (diagonal_fill && lnk[i] == -1) {
1955       fm = n;
1956       while (lnk[fm] < i) fm = lnk[fm];
1957       lnk[i]     = lnk[fm]; /* insert diagonal into linked list */
1958       lnk[fm]    = i;
1959       lnk_lvl[i] = 0;
1960       nzi++; dcount++;
1961     }
1962 
1963     /* add pivot rows into the active row */
1964     nzbd = 0;
1965     prow = lnk[n];
1966     while (prow < i) {
1967       nnz      = bdiag[prow];
1968       cols     = bj_ptr[prow] + nnz + 1;
1969       cols_lvl = bjlvl_ptr[prow] + nnz + 1;
1970       nnz      = bi[prow+1] - bi[prow] - nnz - 1;
1971       ierr = PetscILULLAddSorted(nnz,cols,levels,cols_lvl,prow,nlnk,lnk,lnk_lvl,lnkbt,prow);CHKERRQ(ierr);
1972       nzi += nlnk;
1973       prow = lnk[prow];
1974       nzbd++;
1975     }
1976     bdiag[i] = nzbd;
1977     bi[i+1]  = bi[i] + nzi;
1978 
1979     /* if free space is not available, make more free space */
1980     if (current_space->local_remaining<nzi) {
1981       nnz = nzi*(n - i); /* estimated and max additional space needed */
1982       ierr = PetscFreeSpaceGet(nnz,&current_space);CHKERRQ(ierr);
1983       ierr = PetscFreeSpaceGet(nnz,&current_space_lvl);CHKERRQ(ierr);
1984       reallocs++;
1985     }
1986 
1987     /* copy data into free_space and free_space_lvl, then initialize lnk */
1988     ierr = PetscIncompleteLLClean(n,n,nzi,lnk,lnk_lvl,current_space->array,current_space_lvl->array,lnkbt);CHKERRQ(ierr);
1989     bj_ptr[i]    = current_space->array;
1990     bjlvl_ptr[i] = current_space_lvl->array;
1991 
1992     /* make sure the active row i has diagonal entry */
1993     if (*(bj_ptr[i]+bdiag[i]) != i) {
1994       SETERRQ1(PETSC_ERR_MAT_LU_ZRPVT,"Row %D has missing diagonal in factored matrix\n\
1995     try running with -pc_factor_nonzeros_along_diagonal or -pc_factor_diagonal_fill",i);
1996     }
1997 
1998     current_space->array           += nzi;
1999     current_space->local_used      += nzi;
2000     current_space->local_remaining -= nzi;
2001     current_space_lvl->array           += nzi;
2002     current_space_lvl->local_used      += nzi;
2003     current_space_lvl->local_remaining -= nzi;
2004   }
2005 
2006   ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr);
2007   ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr);
2008 
2009   /* destroy list of free space and other temporary arrays */
2010   ierr = PetscMalloc((bi[n]+1)*sizeof(PetscInt),&bj);CHKERRQ(ierr);
2011   ierr = PetscFreeSpaceContiguous(&free_space,bj);CHKERRQ(ierr); /* copy free_space -> bj */
2012   ierr = PetscIncompleteLLDestroy(lnk,lnkbt);CHKERRQ(ierr);
2013   ierr = PetscFreeSpaceDestroy(free_space_lvl);CHKERRQ(ierr);
2014   ierr = PetscFree2(bj_ptr,bjlvl_ptr);CHKERRQ(ierr);
2015 
2016 #if defined(PETSC_USE_INFO)
2017   {
2018     PetscReal af = ((PetscReal)bi[n])/((PetscReal)ai[n]);
2019     ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocs,f,af);CHKERRQ(ierr);
2020     ierr = PetscInfo1(A,"Run with -[sub_]pc_factor_fill %G or use \n",af);CHKERRQ(ierr);
2021     ierr = PetscInfo1(A,"PCFactorSetFill([sub]pc,%G);\n",af);CHKERRQ(ierr);
2022     ierr = PetscInfo(A,"for best performance.\n");CHKERRQ(ierr);
2023     if (diagonal_fill) {
2024       ierr = PetscInfo1(A,"Detected and replaced %D missing diagonals",dcount);CHKERRQ(ierr);
2025     }
2026   }
2027 #endif
2028 
2029   /* put together the new matrix */
2030   ierr = MatSeqAIJSetPreallocation_SeqAIJ(fact,MAT_SKIP_ALLOCATION,PETSC_NULL);CHKERRQ(ierr);
2031   ierr = PetscLogObjectParent(fact,isicol);CHKERRQ(ierr);
2032   b = (Mat_SeqAIJ*)(fact)->data;
2033   b->free_a       = PETSC_TRUE;
2034   b->free_ij      = PETSC_TRUE;
2035   b->singlemalloc = PETSC_FALSE;
2036   ierr = PetscMalloc(bi[n]*sizeof(PetscScalar),&b->a);CHKERRQ(ierr);
2037   b->j          = bj;
2038   b->i          = bi;
2039   for (i=0; i<n; i++) bdiag[i] += bi[i];
2040   b->diag       = bdiag;
2041   b->ilen       = 0;
2042   b->imax       = 0;
2043   b->row        = isrow;
2044   b->col        = iscol;
2045   ierr          = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr);
2046   ierr          = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr);
2047   b->icol       = isicol;
2048   ierr = PetscMalloc((n+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr);
2049   /* In b structure:  Free imax, ilen, old a, old j.
2050      Allocate bdiag, solve_work, new a, new j */
2051   ierr = PetscLogObjectMemory(fact,(bi[n]-n) * (sizeof(PetscInt)+sizeof(PetscScalar)));CHKERRQ(ierr);
2052   b->maxnz             = b->nz = bi[n] ;
2053   (fact)->info.factor_mallocs    = reallocs;
2054   (fact)->info.fill_ratio_given  = f;
2055   (fact)->info.fill_ratio_needed = ((PetscReal)bi[n])/((PetscReal)ai[n]);
2056   (fact)->ops->lufactornumeric =  MatLUFactorNumeric_SeqAIJ_inplace;
2057   ierr = MatILUFactorSymbolic_SeqAIJ_Inode(fact,A,isrow,iscol,info);CHKERRQ(ierr);
2058   PetscFunctionReturn(0);
2059 }
2060 
2061 #undef __FUNCT__
2062 #define __FUNCT__ "MatCholeskyFactorNumeric_SeqAIJ"
2063 PetscErrorCode MatCholeskyFactorNumeric_SeqAIJ(Mat B,Mat A,const MatFactorInfo *info)
2064 {
2065   Mat            C = B;
2066   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data;
2067   Mat_SeqSBAIJ   *b=(Mat_SeqSBAIJ*)C->data;
2068   IS             ip=b->row,iip = b->icol;
2069   PetscErrorCode ierr;
2070   const PetscInt *rip,*riip;
2071   PetscInt       i,j,mbs=A->rmap->n,*bi=b->i,*bj=b->j,*bdiag=b->diag,*bjtmp;
2072   PetscInt       *ai=a->i,*aj=a->j;
2073   PetscInt       k,jmin,jmax,*c2r,*il,col,nexti,ili,nz;
2074   MatScalar      *rtmp,*ba=b->a,*bval,*aa=a->a,dk,uikdi;
2075   PetscTruth     perm_identity;
2076 
2077   FactorShiftCtx sctx;
2078   PetscReal      rs;
2079   MatScalar      d,*v;
2080 
2081   PetscFunctionBegin;
2082   /* MatPivotSetUp(): initialize shift context sctx */
2083   ierr = PetscMemzero(&sctx,sizeof(FactorShiftCtx));CHKERRQ(ierr);
2084 
2085   if (info->shifttype == MAT_SHIFT_POSITIVE_DEFINITE) { /* set sctx.shift_top=max{rs} */
2086     sctx.shift_top = info->zeropivot;
2087     for (i=0; i<mbs; i++) {
2088       /* calculate sum(|aij|)-RealPart(aii), amt of shift needed for this row */
2089       d  = (aa)[a->diag[i]];
2090       rs = -PetscAbsScalar(d) - PetscRealPart(d);
2091       v  = aa+ai[i];
2092       nz = ai[i+1] - ai[i];
2093       for (j=0; j<nz; j++)
2094 	rs += PetscAbsScalar(v[j]);
2095       if (rs>sctx.shift_top) sctx.shift_top = rs;
2096     }
2097     sctx.shift_top   *= 1.1;
2098     sctx.nshift_max   = 5;
2099     sctx.shift_lo     = 0.;
2100     sctx.shift_hi     = 1.;
2101   }
2102 
2103   ierr  = ISGetIndices(ip,&rip);CHKERRQ(ierr);
2104   ierr  = ISGetIndices(iip,&riip);CHKERRQ(ierr);
2105 
2106   /* allocate working arrays
2107      c2r: linked list, keep track of pivot rows for a given column. c2r[col]: head of the list for a given col
2108      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
2109   */
2110   ierr = PetscMalloc3(mbs,MatScalar,&rtmp,mbs,PetscInt,&il,mbs,PetscInt,&c2r);CHKERRQ(ierr);
2111 
2112   do {
2113     sctx.useshift = PETSC_FALSE;
2114 
2115     for (i=0; i<mbs; i++) c2r[i] = mbs;
2116     il[0] = 0;
2117 
2118     for (k = 0; k<mbs; k++){
2119       /* zero rtmp */
2120       nz = bi[k+1] - bi[k];
2121       bjtmp = bj + bi[k];
2122       for (j=0; j<nz; j++) rtmp[bjtmp[j]] = 0.0;
2123 
2124       /* load in initial unfactored row */
2125       bval = ba + bi[k];
2126       jmin = ai[rip[k]]; jmax = ai[rip[k]+1];
2127       for (j = jmin; j < jmax; j++){
2128         col = riip[aj[j]];
2129         if (col >= k){ /* only take upper triangular entry */
2130           rtmp[col] = aa[j];
2131           *bval++   = 0.0; /* for in-place factorization */
2132         }
2133       }
2134       /* shift the diagonal of the matrix: ZeropivotApply() */
2135       rtmp[k] += sctx.shift_amount;  /* shift the diagonal of the matrix */
2136 
2137       /* modify k-th row by adding in those rows i with U(i,k)!=0 */
2138       dk = rtmp[k];
2139       i  = c2r[k]; /* first row to be added to k_th row  */
2140 
2141       while (i < k){
2142         nexti = c2r[i]; /* next row to be added to k_th row */
2143 
2144         /* compute multiplier, update diag(k) and U(i,k) */
2145         ili   = il[i];  /* index of first nonzero element in U(i,k:bms-1) */
2146         uikdi = - ba[ili]*ba[bdiag[i]];  /* diagonal(k) */
2147         dk   += uikdi*ba[ili]; /* update diag[k] */
2148         ba[ili] = uikdi; /* -U(i,k) */
2149 
2150         /* add multiple of row i to k-th row */
2151         jmin = ili + 1; jmax = bi[i+1];
2152         if (jmin < jmax){
2153           for (j=jmin; j<jmax; j++) rtmp[bj[j]] += uikdi*ba[j];
2154           /* update il and c2r for row i */
2155           il[i] = jmin;
2156           j = bj[jmin]; c2r[i] = c2r[j]; c2r[j] = i;
2157         }
2158         i = nexti;
2159       }
2160 
2161       /* copy data into U(k,:) */
2162       rs   = 0.0;
2163       jmin = bi[k]; jmax = bi[k+1]-1;
2164       if (jmin < jmax) {
2165         for (j=jmin; j<jmax; j++){
2166           col = bj[j]; ba[j] = rtmp[col]; rs += PetscAbsScalar(ba[j]);
2167         }
2168         /* add the k-th row into il and c2r */
2169         il[k] = jmin;
2170         i = bj[jmin]; c2r[k] = c2r[i]; c2r[i] = k;
2171       }
2172 
2173       /* MatPivotCheck() */
2174       sctx.rs  = rs;
2175       sctx.pv  = dk;
2176       if (info->shifttype == MAT_SHIFT_NONZERO){
2177         ierr = MatPivotCheck_nz(info,sctx,k);CHKERRQ(ierr);
2178       } else if (info->shifttype == MAT_SHIFT_POSITIVE_DEFINITE){
2179         ierr = MatPivotCheck_pd(info,sctx,k);CHKERRQ(ierr);
2180       } else if (info->shifttype == MAT_SHIFT_INBLOCKS){
2181         ierr = MatPivotCheck_inblocks(info,sctx,k);CHKERRQ(ierr);
2182       } else {
2183         ierr = MatPivotCheck_none(info,sctx,k);CHKERRQ(ierr);
2184       }
2185       dk = sctx.pv;
2186 
2187       ba[bdiag[k]] = 1.0/dk; /* U(k,k) */
2188     }
2189   } while (sctx.useshift);
2190 
2191   ierr = PetscFree3(rtmp,il,c2r);CHKERRQ(ierr);
2192   ierr = ISRestoreIndices(ip,&rip);CHKERRQ(ierr);
2193   ierr = ISRestoreIndices(iip,&riip);CHKERRQ(ierr);
2194 
2195   ierr = ISIdentity(ip,&perm_identity);CHKERRQ(ierr);
2196   if (perm_identity){
2197     B->ops->solve           = MatSolve_SeqSBAIJ_1_NaturalOrdering;
2198     B->ops->solvetranspose  = MatSolve_SeqSBAIJ_1_NaturalOrdering;
2199     B->ops->forwardsolve    = MatForwardSolve_SeqSBAIJ_1_NaturalOrdering;
2200     B->ops->backwardsolve   = MatBackwardSolve_SeqSBAIJ_1_NaturalOrdering;
2201   } else {
2202     B->ops->solve           = MatSolve_SeqSBAIJ_1;
2203     B->ops->solvetranspose  = MatSolve_SeqSBAIJ_1;
2204     B->ops->forwardsolve    = MatForwardSolve_SeqSBAIJ_1;
2205     B->ops->backwardsolve   = MatBackwardSolve_SeqSBAIJ_1;
2206   }
2207 
2208   C->assembled    = PETSC_TRUE;
2209   C->preallocated = PETSC_TRUE;
2210   ierr = PetscLogFlops(C->rmap->n);CHKERRQ(ierr);
2211 
2212   /* MatPivotView() */
2213   if (sctx.nshift){
2214     if (info->shifttype == MAT_SHIFT_POSITIVE_DEFINITE) {
2215       ierr = PetscInfo4(A,"number of shift_pd tries %D, shift_amount %G, diagonal shifted up by %e fraction top_value %e\n",sctx.nshift,sctx.shift_amount,sctx.shift_fraction,sctx.shift_top);CHKERRQ(ierr);
2216     } else if (info->shifttype == MAT_SHIFT_NONZERO) {
2217       ierr = PetscInfo2(A,"number of shift_nz tries %D, shift_amount %G\n",sctx.nshift,sctx.shift_amount);CHKERRQ(ierr);
2218     } else if (info->shifttype == MAT_SHIFT_INBLOCKS){
2219       ierr = PetscInfo2(A,"number of shift_inblocks applied %D, each shift_amount %G\n",sctx.nshift,info->shiftamount);CHKERRQ(ierr);
2220     }
2221   }
2222   PetscFunctionReturn(0);
2223 }
2224 
2225 #undef __FUNCT__
2226 #define __FUNCT__ "MatCholeskyFactorNumeric_SeqAIJ_inplace"
2227 PetscErrorCode MatCholeskyFactorNumeric_SeqAIJ_inplace(Mat B,Mat A,const MatFactorInfo *info)
2228 {
2229   Mat            C = B;
2230   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data;
2231   Mat_SeqSBAIJ   *b=(Mat_SeqSBAIJ*)C->data;
2232   IS             ip=b->row,iip = b->icol;
2233   PetscErrorCode ierr;
2234   const PetscInt *rip,*riip;
2235   PetscInt       i,j,mbs=A->rmap->n,*bi=b->i,*bj=b->j,*bcol,*bjtmp;
2236   PetscInt       *ai=a->i,*aj=a->j;
2237   PetscInt       k,jmin,jmax,*jl,*il,col,nexti,ili,nz;
2238   MatScalar      *rtmp,*ba=b->a,*bval,*aa=a->a,dk,uikdi;
2239   PetscReal      zeropivot,rs,shiftnz;
2240   PetscReal      shiftpd;
2241   ChShift_Ctx    sctx;
2242   PetscInt       newshift;
2243   PetscTruth     perm_identity;
2244 
2245   PetscFunctionBegin;
2246   zeropivot = info->zeropivot;
2247 
2248   ierr  = ISGetIndices(ip,&rip);CHKERRQ(ierr);
2249   ierr  = ISGetIndices(iip,&riip);CHKERRQ(ierr);
2250 
2251   /* initialization */
2252   ierr = PetscMalloc3(mbs,MatScalar,&rtmp,mbs,PetscInt,&il,mbs,PetscInt,&jl);CHKERRQ(ierr);
2253   sctx.shift_amount = 0;
2254   sctx.nshift       = 0;
2255   do {
2256     sctx.chshift = PETSC_FALSE;
2257     for (i=0; i<mbs; i++) jl[i] = mbs;
2258     il[0] = 0;
2259 
2260     for (k = 0; k<mbs; k++){
2261       /* zero rtmp */
2262       nz = bi[k+1] - bi[k];
2263       bjtmp = bj + bi[k];
2264       for (j=0; j<nz; j++) rtmp[bjtmp[j]] = 0.0;
2265 
2266       bval = ba + bi[k];
2267       /* initialize k-th row by the perm[k]-th row of A */
2268       jmin = ai[rip[k]]; jmax = ai[rip[k]+1];
2269       for (j = jmin; j < jmax; j++){
2270         col = riip[aj[j]];
2271         if (col >= k){ /* only take upper triangular entry */
2272           rtmp[col] = aa[j];
2273           *bval++  = 0.0; /* for in-place factorization */
2274         }
2275       }
2276       /* shift the diagonal of the matrix */
2277       if (sctx.nshift) rtmp[k] += sctx.shift_amount;
2278 
2279       /* modify k-th row by adding in those rows i with U(i,k)!=0 */
2280       dk = rtmp[k];
2281       i = jl[k]; /* first row to be added to k_th row  */
2282 
2283       while (i < k){
2284         nexti = jl[i]; /* next row to be added to k_th row */
2285 
2286         /* compute multiplier, update diag(k) and U(i,k) */
2287         ili = il[i];  /* index of first nonzero element in U(i,k:bms-1) */
2288         uikdi = - ba[ili]*ba[bi[i]];  /* diagonal(k) */
2289         dk += uikdi*ba[ili];
2290         ba[ili] = uikdi; /* -U(i,k) */
2291 
2292         /* add multiple of row i to k-th row */
2293         jmin = ili + 1; jmax = bi[i+1];
2294         if (jmin < jmax){
2295           for (j=jmin; j<jmax; j++) rtmp[bj[j]] += uikdi*ba[j];
2296           /* update il and jl for row i */
2297           il[i] = jmin;
2298           j = bj[jmin]; jl[i] = jl[j]; jl[j] = i;
2299         }
2300         i = nexti;
2301       }
2302 
2303       /* shift the diagonals when zero pivot is detected */
2304       /* compute rs=sum of abs(off-diagonal) */
2305       rs   = 0.0;
2306       jmin = bi[k]+1;
2307       nz   = bi[k+1] - jmin;
2308       bcol = bj + jmin;
2309       for (j=0; j<nz; j++) {
2310         rs += PetscAbsScalar(rtmp[bcol[j]]);
2311       }
2312 
2313       sctx.rs = rs;
2314       sctx.pv = dk;
2315       ierr = MatCholeskyCheckShift_inline(info,sctx,k,newshift);CHKERRQ(ierr);
2316 
2317       if (newshift == 1) {
2318         if (!sctx.shift_amount) {
2319           sctx.shift_amount = 1e-5;
2320         }
2321         break;
2322       }
2323 
2324       /* copy data into U(k,:) */
2325       ba[bi[k]] = 1.0/dk; /* U(k,k) */
2326       jmin = bi[k]+1; jmax = bi[k+1];
2327       if (jmin < jmax) {
2328         for (j=jmin; j<jmax; j++){
2329           col = bj[j]; ba[j] = rtmp[col];
2330         }
2331         /* add the k-th row into il and jl */
2332         il[k] = jmin;
2333         i = bj[jmin]; jl[k] = jl[i]; jl[i] = k;
2334       }
2335     }
2336   } while (sctx.chshift);
2337   ierr = PetscFree3(rtmp,il,jl);CHKERRQ(ierr);
2338   ierr = ISRestoreIndices(ip,&rip);CHKERRQ(ierr);
2339   ierr = ISRestoreIndices(iip,&riip);CHKERRQ(ierr);
2340 
2341   ierr = ISIdentity(ip,&perm_identity);CHKERRQ(ierr);
2342   if (perm_identity){
2343     B->ops->solve           = MatSolve_SeqSBAIJ_1_NaturalOrdering_inplace;
2344     B->ops->solvetranspose  = MatSolve_SeqSBAIJ_1_NaturalOrdering_inplace;
2345     B->ops->forwardsolve    = MatForwardSolve_SeqSBAIJ_1_NaturalOrdering_inplace;
2346     B->ops->backwardsolve   = MatBackwardSolve_SeqSBAIJ_1_NaturalOrdering_inplace;
2347   } else {
2348     B->ops->solve           = MatSolve_SeqSBAIJ_1_inplace;
2349     B->ops->solvetranspose  = MatSolve_SeqSBAIJ_1_inplace;
2350     B->ops->forwardsolve    = MatForwardSolve_SeqSBAIJ_1_inplace;
2351     B->ops->backwardsolve   = MatBackwardSolve_SeqSBAIJ_1_inplace;
2352   }
2353 
2354   C->assembled    = PETSC_TRUE;
2355   C->preallocated = PETSC_TRUE;
2356   ierr = PetscLogFlops(C->rmap->n);CHKERRQ(ierr);
2357   if (sctx.nshift){
2358     if (shiftnz) {
2359       ierr = PetscInfo2(A,"number of shiftnz tries %D, shift_amount %G\n",sctx.nshift,sctx.shift_amount);CHKERRQ(ierr);
2360     } else if (shiftpd) {
2361       ierr = PetscInfo2(A,"number of shiftpd tries %D, shift_amount %G\n",sctx.nshift,sctx.shift_amount);CHKERRQ(ierr);
2362     }
2363   }
2364   PetscFunctionReturn(0);
2365 }
2366 
2367 /*
2368    icc() under revised new data structure.
2369    Factored arrays bj and ba are stored as
2370      U(0,:),...,U(i,:),U(n-1,:)
2371 
2372    ui=fact->i is an array of size n+1, in which
2373    ui+
2374      ui[i]:  points to 1st entry of U(i,:),i=0,...,n-1
2375      ui[n]:  points to U(n-1,n-1)+1
2376 
2377   udiag=fact->diag is an array of size n,in which
2378      udiag[i]: points to diagonal of U(i,:), i=0,...,n-1
2379 
2380    U(i,:) contains udiag[i] as its last entry, i.e.,
2381     U(i,:) = (u[i,i+1],...,u[i,n-1],diag[i])
2382 */
2383 
2384 #undef __FUNCT__
2385 #define __FUNCT__ "MatICCFactorSymbolic_SeqAIJ"
2386 PetscErrorCode MatICCFactorSymbolic_SeqAIJ(Mat fact,Mat A,IS perm,const MatFactorInfo *info)
2387 {
2388   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
2389   Mat_SeqSBAIJ       *b;
2390   PetscErrorCode     ierr;
2391   PetscTruth         perm_identity,missing;
2392   PetscInt           reallocs=0,i,*ai=a->i,*aj=a->j,am=A->rmap->n,*ui,*udiag;
2393   const PetscInt     *rip,*riip;
2394   PetscInt           jmin,jmax,nzk,k,j,*jl,prow,*il,nextprow;
2395   PetscInt           nlnk,*lnk,*lnk_lvl=PETSC_NULL,d;
2396   PetscInt           ncols,ncols_upper,*cols,*ajtmp,*uj,**uj_ptr,**uj_lvl_ptr;
2397   PetscReal          fill=info->fill,levels=info->levels;
2398   PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL;
2399   PetscFreeSpaceList free_space_lvl=PETSC_NULL,current_space_lvl=PETSC_NULL;
2400   PetscBT            lnkbt;
2401   IS                 iperm;
2402 
2403   PetscFunctionBegin;
2404   if (A->rmap->n != A->cmap->n) SETERRQ2(PETSC_ERR_ARG_WRONG,"Must be square matrix, rows %D columns %D",A->rmap->n,A->cmap->n);
2405   ierr = MatMissingDiagonal(A,&missing,&d);CHKERRQ(ierr);
2406   if (missing) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",d);
2407   ierr = ISIdentity(perm,&perm_identity);CHKERRQ(ierr);
2408   ierr = ISInvertPermutation(perm,PETSC_DECIDE,&iperm);CHKERRQ(ierr);
2409 
2410   ierr = PetscMalloc((am+1)*sizeof(PetscInt),&ui);CHKERRQ(ierr);
2411   ierr = PetscMalloc((am+1)*sizeof(PetscInt),&udiag);CHKERRQ(ierr);
2412   ui[0] = 0;
2413 
2414   /* ICC(0) without matrix ordering: simply rearrange column indices */
2415   if (!levels && perm_identity) {
2416     for (i=0; i<am; i++) {
2417       ncols    = ai[i+1] - a->diag[i];
2418       ui[i+1]  = ui[i] + ncols;
2419       udiag[i] = ui[i+1] - 1; /* points to the last entry of U(i,:) */
2420     }
2421     ierr = PetscMalloc((ui[am]+1)*sizeof(PetscInt),&uj);CHKERRQ(ierr);
2422     cols = uj;
2423     for (i=0; i<am; i++) {
2424       aj    = a->j + a->diag[i] + 1; /* 1st entry of U(i,:) without diagonal */
2425       ncols = ai[i+1] - a->diag[i] -1;
2426       for (j=0; j<ncols; j++) *cols++ = aj[j];
2427       *cols++ = i; /* diagoanl is located as the last entry of U(i,:) */
2428     }
2429   } else { /* case: levels>0 || (levels=0 && !perm_identity) */
2430     ierr = ISGetIndices(iperm,&riip);CHKERRQ(ierr);
2431     ierr = ISGetIndices(perm,&rip);CHKERRQ(ierr);
2432 
2433     /* initialization */
2434     ierr  = PetscMalloc((am+1)*sizeof(PetscInt),&ajtmp);CHKERRQ(ierr);
2435 
2436     /* jl: linked list for storing indices of the pivot rows
2437        il: il[i] points to the 1st nonzero entry of U(i,k:am-1) */
2438     ierr = PetscMalloc4(am,PetscInt*,&uj_ptr,am,PetscInt*,&uj_lvl_ptr,am,PetscInt,&jl,am,PetscInt,&il);CHKERRQ(ierr);
2439     for (i=0; i<am; i++){
2440       jl[i] = am; il[i] = 0;
2441     }
2442 
2443     /* create and initialize a linked list for storing column indices of the active row k */
2444     nlnk = am + 1;
2445     ierr = PetscIncompleteLLCreate(am,am,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr);
2446 
2447     /* initial FreeSpace size is fill*(ai[am]+1) */
2448     ierr = PetscFreeSpaceGet((PetscInt)(fill*(ai[am]+1)),&free_space);CHKERRQ(ierr);
2449     current_space = free_space;
2450     ierr = PetscFreeSpaceGet((PetscInt)(fill*(ai[am]+1)),&free_space_lvl);CHKERRQ(ierr);
2451     current_space_lvl = free_space_lvl;
2452 
2453     for (k=0; k<am; k++){  /* for each active row k */
2454       /* initialize lnk by the column indices of row rip[k] of A */
2455       nzk   = 0;
2456       ncols = ai[rip[k]+1] - ai[rip[k]];
2457       if (!ncols) SETERRQ2(PETSC_ERR_MAT_CH_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",rip[k],k);
2458       ncols_upper = 0;
2459       for (j=0; j<ncols; j++){
2460         i = *(aj + ai[rip[k]] + j); /* unpermuted column index */
2461         if (riip[i] >= k){ /* only take upper triangular entry */
2462           ajtmp[ncols_upper] = i;
2463           ncols_upper++;
2464         }
2465       }
2466       ierr = PetscIncompleteLLInit(ncols_upper,ajtmp,am,riip,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr);
2467       nzk += nlnk;
2468 
2469       /* update lnk by computing fill-in for each pivot row to be merged in */
2470       prow = jl[k]; /* 1st pivot row */
2471 
2472       while (prow < k){
2473         nextprow = jl[prow];
2474 
2475         /* merge prow into k-th row */
2476         jmin = il[prow] + 1;  /* index of the 2nd nzero entry in U(prow,k:am-1) */
2477         jmax = ui[prow+1];
2478         ncols = jmax-jmin;
2479         i     = jmin - ui[prow];
2480         cols  = uj_ptr[prow] + i; /* points to the 2nd nzero entry in U(prow,k:am-1) */
2481         uj    = uj_lvl_ptr[prow] + i; /* levels of cols */
2482         j     = *(uj - 1);
2483         ierr = PetscICCLLAddSorted(ncols,cols,levels,uj,am,nlnk,lnk,lnk_lvl,lnkbt,j);CHKERRQ(ierr);
2484         nzk += nlnk;
2485 
2486         /* update il and jl for prow */
2487         if (jmin < jmax){
2488           il[prow] = jmin;
2489           j = *cols; jl[prow] = jl[j]; jl[j] = prow;
2490         }
2491         prow = nextprow;
2492       }
2493 
2494       /* if free space is not available, make more free space */
2495       if (current_space->local_remaining<nzk) {
2496         i  = am - k + 1; /* num of unfactored rows */
2497         i *= PetscMin(nzk, i-1); /* i*nzk, i*(i-1): estimated and max additional space needed */
2498         ierr = PetscFreeSpaceGet(i,&current_space);CHKERRQ(ierr);
2499         ierr = PetscFreeSpaceGet(i,&current_space_lvl);CHKERRQ(ierr);
2500         reallocs++;
2501       }
2502 
2503       /* copy data into free_space and free_space_lvl, then initialize lnk */
2504       if (nzk == 0) SETERRQ1(PETSC_ERR_ARG_WRONG,"Empty row %D in ICC matrix factor",k);
2505       ierr = PetscIncompleteLLClean(am,am,nzk,lnk,lnk_lvl,current_space->array,current_space_lvl->array,lnkbt);CHKERRQ(ierr);
2506 
2507       /* add the k-th row into il and jl */
2508       if (nzk > 1){
2509         i = current_space->array[1]; /* col value of the first nonzero element in U(k, k+1:am-1) */
2510         jl[k] = jl[i]; jl[i] = k;
2511         il[k] = ui[k] + 1;
2512       }
2513       uj_ptr[k]     = current_space->array;
2514       uj_lvl_ptr[k] = current_space_lvl->array;
2515 
2516       current_space->array           += nzk;
2517       current_space->local_used      += nzk;
2518       current_space->local_remaining -= nzk;
2519 
2520       current_space_lvl->array           += nzk;
2521       current_space_lvl->local_used      += nzk;
2522       current_space_lvl->local_remaining -= nzk;
2523 
2524       ui[k+1] = ui[k] + nzk;
2525     }
2526 
2527 #if defined(PETSC_USE_INFO)
2528     if (ai[am] != 0) {
2529       PetscReal af = (PetscReal)ui[am]/((PetscReal)ai[am]);
2530       ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocs,fill,af);CHKERRQ(ierr);
2531       ierr = PetscInfo1(A,"Run with -pc_factor_fill %G or use \n",af);CHKERRQ(ierr);
2532       ierr = PetscInfo1(A,"PCFactorSetFill(pc,%G) for best performance.\n",af);CHKERRQ(ierr);
2533     } else {
2534       ierr = PetscInfo(A,"Empty matrix.\n");CHKERRQ(ierr);
2535     }
2536 #endif
2537 
2538     ierr = ISRestoreIndices(perm,&rip);CHKERRQ(ierr);
2539     ierr = ISRestoreIndices(iperm,&riip);CHKERRQ(ierr);
2540     ierr = PetscFree4(uj_ptr,uj_lvl_ptr,jl,il);CHKERRQ(ierr);
2541     ierr = PetscFree(ajtmp);CHKERRQ(ierr);
2542 
2543     /* destroy list of free space and other temporary array(s) */
2544     ierr = PetscMalloc((ui[am]+1)*sizeof(PetscInt),&uj);CHKERRQ(ierr);
2545     ierr = PetscFreeSpaceContiguous_Cholesky(&free_space,uj,am,ui,udiag);CHKERRQ(ierr); /* store matrix factor  */
2546     ierr = PetscIncompleteLLDestroy(lnk,lnkbt);CHKERRQ(ierr);
2547     ierr = PetscFreeSpaceDestroy(free_space_lvl);CHKERRQ(ierr);
2548 
2549   } /* end of case: levels>0 || (levels=0 && !perm_identity) */
2550 
2551   /* put together the new matrix in MATSEQSBAIJ format */
2552   b    = (Mat_SeqSBAIJ*)(fact)->data;
2553   b->singlemalloc = PETSC_FALSE;
2554   ierr = PetscMalloc((ui[am]+1)*sizeof(MatScalar),&b->a);CHKERRQ(ierr);
2555   b->j    = uj;
2556   b->i    = ui;
2557   b->diag = udiag;
2558   b->free_diag = PETSC_TRUE;
2559   b->ilen = 0;
2560   b->imax = 0;
2561   b->row  = perm;
2562   b->col  = perm;
2563   ierr    = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr);
2564   ierr    = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr);
2565   b->icol = iperm;
2566   b->pivotinblocks = PETSC_FALSE; /* need to get from MatFactorInfo */
2567   ierr = PetscMalloc((am+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr);
2568   ierr = PetscLogObjectMemory(fact,ui[am]*(sizeof(PetscInt)+sizeof(MatScalar)));CHKERRQ(ierr);
2569   b->maxnz   = b->nz = ui[am];
2570   b->free_a  = PETSC_TRUE;
2571   b->free_ij = PETSC_TRUE;
2572 
2573   fact->info.factor_mallocs    = reallocs;
2574   fact->info.fill_ratio_given  = fill;
2575   if (ai[am] != 0) {
2576     fact->info.fill_ratio_needed = ((PetscReal)ui[am])/((PetscReal)ai[am]);
2577   } else {
2578     fact->info.fill_ratio_needed = 0.0;
2579   }
2580   fact->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJ;
2581   PetscFunctionReturn(0);
2582 }
2583 
2584 #undef __FUNCT__
2585 #define __FUNCT__ "MatICCFactorSymbolic_SeqAIJ_inplace"
2586 PetscErrorCode MatICCFactorSymbolic_SeqAIJ_inplace(Mat fact,Mat A,IS perm,const MatFactorInfo *info)
2587 {
2588   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
2589   Mat_SeqSBAIJ       *b;
2590   PetscErrorCode     ierr;
2591   PetscTruth         perm_identity,missing;
2592   PetscInt           reallocs=0,i,*ai=a->i,*aj=a->j,am=A->rmap->n,*ui,*udiag;
2593   const PetscInt     *rip,*riip;
2594   PetscInt           jmin,jmax,nzk,k,j,*jl,prow,*il,nextprow;
2595   PetscInt           nlnk,*lnk,*lnk_lvl=PETSC_NULL,d;
2596   PetscInt           ncols,ncols_upper,*cols,*ajtmp,*uj,**uj_ptr,**uj_lvl_ptr;
2597   PetscReal          fill=info->fill,levels=info->levels;
2598   PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL;
2599   PetscFreeSpaceList free_space_lvl=PETSC_NULL,current_space_lvl=PETSC_NULL;
2600   PetscBT            lnkbt;
2601   IS                 iperm;
2602 
2603   PetscFunctionBegin;
2604   if (A->rmap->n != A->cmap->n) SETERRQ2(PETSC_ERR_ARG_WRONG,"Must be square matrix, rows %D columns %D",A->rmap->n,A->cmap->n);
2605   ierr = MatMissingDiagonal(A,&missing,&d);CHKERRQ(ierr);
2606   if (missing) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",d);
2607   ierr = ISIdentity(perm,&perm_identity);CHKERRQ(ierr);
2608   ierr = ISInvertPermutation(perm,PETSC_DECIDE,&iperm);CHKERRQ(ierr);
2609 
2610   ierr = PetscMalloc((am+1)*sizeof(PetscInt),&ui);CHKERRQ(ierr);
2611   ierr = PetscMalloc((am+1)*sizeof(PetscInt),&udiag);CHKERRQ(ierr);
2612   ui[0] = 0;
2613 
2614   /* ICC(0) without matrix ordering: simply copies fill pattern */
2615   if (!levels && perm_identity) {
2616 
2617     for (i=0; i<am; i++) {
2618       ui[i+1]  = ui[i] + ai[i+1] - a->diag[i];
2619       udiag[i] = ui[i];
2620     }
2621     ierr = PetscMalloc((ui[am]+1)*sizeof(PetscInt),&uj);CHKERRQ(ierr);
2622     cols = uj;
2623     for (i=0; i<am; i++) {
2624       aj    = a->j + a->diag[i];
2625       ncols = ui[i+1] - ui[i];
2626       for (j=0; j<ncols; j++) *cols++ = *aj++;
2627     }
2628   } else { /* case: levels>0 || (levels=0 && !perm_identity) */
2629     ierr = ISGetIndices(iperm,&riip);CHKERRQ(ierr);
2630     ierr = ISGetIndices(perm,&rip);CHKERRQ(ierr);
2631 
2632     /* initialization */
2633     ierr  = PetscMalloc((am+1)*sizeof(PetscInt),&ajtmp);CHKERRQ(ierr);
2634 
2635     /* jl: linked list for storing indices of the pivot rows
2636        il: il[i] points to the 1st nonzero entry of U(i,k:am-1) */
2637     ierr = PetscMalloc4(am,PetscInt*,&uj_ptr,am,PetscInt*,&uj_lvl_ptr,am,PetscInt,&jl,am,PetscInt,&il);CHKERRQ(ierr);
2638     for (i=0; i<am; i++){
2639       jl[i] = am; il[i] = 0;
2640     }
2641 
2642     /* create and initialize a linked list for storing column indices of the active row k */
2643     nlnk = am + 1;
2644     ierr = PetscIncompleteLLCreate(am,am,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr);
2645 
2646     /* initial FreeSpace size is fill*(ai[am]+1) */
2647     ierr = PetscFreeSpaceGet((PetscInt)(fill*(ai[am]+1)),&free_space);CHKERRQ(ierr);
2648     current_space = free_space;
2649     ierr = PetscFreeSpaceGet((PetscInt)(fill*(ai[am]+1)),&free_space_lvl);CHKERRQ(ierr);
2650     current_space_lvl = free_space_lvl;
2651 
2652     for (k=0; k<am; k++){  /* for each active row k */
2653       /* initialize lnk by the column indices of row rip[k] of A */
2654       nzk   = 0;
2655       ncols = ai[rip[k]+1] - ai[rip[k]];
2656       if (!ncols) SETERRQ2(PETSC_ERR_MAT_CH_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",rip[k],k);
2657       ncols_upper = 0;
2658       for (j=0; j<ncols; j++){
2659         i = *(aj + ai[rip[k]] + j); /* unpermuted column index */
2660         if (riip[i] >= k){ /* only take upper triangular entry */
2661           ajtmp[ncols_upper] = i;
2662           ncols_upper++;
2663         }
2664       }
2665       ierr = PetscIncompleteLLInit(ncols_upper,ajtmp,am,riip,nlnk,lnk,lnk_lvl,lnkbt);CHKERRQ(ierr);
2666       nzk += nlnk;
2667 
2668       /* update lnk by computing fill-in for each pivot row to be merged in */
2669       prow = jl[k]; /* 1st pivot row */
2670 
2671       while (prow < k){
2672         nextprow = jl[prow];
2673 
2674         /* merge prow into k-th row */
2675         jmin = il[prow] + 1;  /* index of the 2nd nzero entry in U(prow,k:am-1) */
2676         jmax = ui[prow+1];
2677         ncols = jmax-jmin;
2678         i     = jmin - ui[prow];
2679         cols  = uj_ptr[prow] + i; /* points to the 2nd nzero entry in U(prow,k:am-1) */
2680         uj    = uj_lvl_ptr[prow] + i; /* levels of cols */
2681         j     = *(uj - 1);
2682         ierr = PetscICCLLAddSorted(ncols,cols,levels,uj,am,nlnk,lnk,lnk_lvl,lnkbt,j);CHKERRQ(ierr);
2683         nzk += nlnk;
2684 
2685         /* update il and jl for prow */
2686         if (jmin < jmax){
2687           il[prow] = jmin;
2688           j = *cols; jl[prow] = jl[j]; jl[j] = prow;
2689         }
2690         prow = nextprow;
2691       }
2692 
2693       /* if free space is not available, make more free space */
2694       if (current_space->local_remaining<nzk) {
2695         i = am - k + 1; /* num of unfactored rows */
2696         i *= PetscMin(nzk, (i-1)); /* i*nzk, i*(i-1): estimated and max additional space needed */
2697         ierr = PetscFreeSpaceGet(i,&current_space);CHKERRQ(ierr);
2698         ierr = PetscFreeSpaceGet(i,&current_space_lvl);CHKERRQ(ierr);
2699         reallocs++;
2700       }
2701 
2702       /* copy data into free_space and free_space_lvl, then initialize lnk */
2703       if (nzk == 0) SETERRQ1(PETSC_ERR_ARG_WRONG,"Empty row %D in ICC matrix factor",k);
2704       ierr = PetscIncompleteLLClean(am,am,nzk,lnk,lnk_lvl,current_space->array,current_space_lvl->array,lnkbt);CHKERRQ(ierr);
2705 
2706       /* add the k-th row into il and jl */
2707       if (nzk > 1){
2708         i = current_space->array[1]; /* col value of the first nonzero element in U(k, k+1:am-1) */
2709         jl[k] = jl[i]; jl[i] = k;
2710         il[k] = ui[k] + 1;
2711       }
2712       uj_ptr[k]     = current_space->array;
2713       uj_lvl_ptr[k] = current_space_lvl->array;
2714 
2715       current_space->array           += nzk;
2716       current_space->local_used      += nzk;
2717       current_space->local_remaining -= nzk;
2718 
2719       current_space_lvl->array           += nzk;
2720       current_space_lvl->local_used      += nzk;
2721       current_space_lvl->local_remaining -= nzk;
2722 
2723       ui[k+1] = ui[k] + nzk;
2724     }
2725 
2726 #if defined(PETSC_USE_INFO)
2727     if (ai[am] != 0) {
2728       PetscReal af = (PetscReal)ui[am]/((PetscReal)ai[am]);
2729       ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocs,fill,af);CHKERRQ(ierr);
2730       ierr = PetscInfo1(A,"Run with -pc_factor_fill %G or use \n",af);CHKERRQ(ierr);
2731       ierr = PetscInfo1(A,"PCFactorSetFill(pc,%G) for best performance.\n",af);CHKERRQ(ierr);
2732     } else {
2733       ierr = PetscInfo(A,"Empty matrix.\n");CHKERRQ(ierr);
2734     }
2735 #endif
2736 
2737     ierr = ISRestoreIndices(perm,&rip);CHKERRQ(ierr);
2738     ierr = ISRestoreIndices(iperm,&riip);CHKERRQ(ierr);
2739     ierr = PetscFree4(uj_ptr,uj_lvl_ptr,jl,il);CHKERRQ(ierr);
2740     ierr = PetscFree(ajtmp);CHKERRQ(ierr);
2741 
2742     /* destroy list of free space and other temporary array(s) */
2743     ierr = PetscMalloc((ui[am]+1)*sizeof(PetscInt),&uj);CHKERRQ(ierr);
2744     ierr = PetscFreeSpaceContiguous(&free_space,uj);CHKERRQ(ierr);
2745     ierr = PetscIncompleteLLDestroy(lnk,lnkbt);CHKERRQ(ierr);
2746     ierr = PetscFreeSpaceDestroy(free_space_lvl);CHKERRQ(ierr);
2747 
2748   } /* end of case: levels>0 || (levels=0 && !perm_identity) */
2749 
2750   /* put together the new matrix in MATSEQSBAIJ format */
2751 
2752   b    = (Mat_SeqSBAIJ*)fact->data;
2753   b->singlemalloc = PETSC_FALSE;
2754   ierr = PetscMalloc((ui[am]+1)*sizeof(MatScalar),&b->a);CHKERRQ(ierr);
2755   b->j    = uj;
2756   b->i    = ui;
2757   b->diag = udiag;
2758   b->free_diag = PETSC_TRUE;
2759   b->ilen = 0;
2760   b->imax = 0;
2761   b->row  = perm;
2762   b->col  = perm;
2763   ierr    = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr);
2764   ierr    = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr);
2765   b->icol = iperm;
2766   b->pivotinblocks = PETSC_FALSE; /* need to get from MatFactorInfo */
2767   ierr    = PetscMalloc((am+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr);
2768   ierr = PetscLogObjectMemory(fact,(ui[am]-am)*(sizeof(PetscInt)+sizeof(MatScalar)));CHKERRQ(ierr);
2769   b->maxnz   = b->nz = ui[am];
2770   b->free_a  = PETSC_TRUE;
2771   b->free_ij = PETSC_TRUE;
2772 
2773   fact->info.factor_mallocs    = reallocs;
2774   fact->info.fill_ratio_given  = fill;
2775   if (ai[am] != 0) {
2776     fact->info.fill_ratio_needed = ((PetscReal)ui[am])/((PetscReal)ai[am]);
2777   } else {
2778     fact->info.fill_ratio_needed = 0.0;
2779   }
2780   fact->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJ_inplace;
2781   PetscFunctionReturn(0);
2782 }
2783 
2784 PetscErrorCode MatCholeskyFactorSymbolic_SeqAIJ(Mat fact,Mat A,IS perm,const MatFactorInfo *info)
2785 {
2786   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
2787   Mat_SeqSBAIJ       *b;
2788   PetscErrorCode     ierr;
2789   PetscTruth         perm_identity;
2790   PetscReal          fill = info->fill;
2791   const PetscInt     *rip,*riip;
2792   PetscInt           i,am=A->rmap->n,*ai=a->i,*aj=a->j,reallocs=0,prow;
2793   PetscInt           *jl,jmin,jmax,nzk,*ui,k,j,*il,nextprow;
2794   PetscInt           nlnk,*lnk,ncols,ncols_upper,*cols,*uj,**ui_ptr,*uj_ptr,*udiag;
2795   PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL;
2796   PetscBT            lnkbt;
2797   IS                 iperm;
2798 
2799   PetscFunctionBegin;
2800   if (A->rmap->n != A->cmap->n) SETERRQ2(PETSC_ERR_ARG_WRONG,"Must be square matrix, rows %D columns %D",A->rmap->n,A->cmap->n);
2801   /* check whether perm is the identity mapping */
2802   ierr = ISIdentity(perm,&perm_identity);CHKERRQ(ierr);
2803   ierr = ISInvertPermutation(perm,PETSC_DECIDE,&iperm);CHKERRQ(ierr);
2804   ierr = ISGetIndices(iperm,&riip);CHKERRQ(ierr);
2805   ierr = ISGetIndices(perm,&rip);CHKERRQ(ierr);
2806 
2807   /* initialization */
2808   ierr  = PetscMalloc((am+1)*sizeof(PetscInt),&ui);CHKERRQ(ierr);
2809   ierr  = PetscMalloc((am+1)*sizeof(PetscInt),&udiag);CHKERRQ(ierr);
2810   ui[0] = 0;
2811 
2812   /* jl: linked list for storing indices of the pivot rows
2813      il: il[i] points to the 1st nonzero entry of U(i,k:am-1) */
2814   ierr = PetscMalloc4(am,PetscInt*,&ui_ptr,am,PetscInt,&jl,am,PetscInt,&il,am,PetscInt,&cols);CHKERRQ(ierr);
2815   for (i=0; i<am; i++){
2816     jl[i] = am; il[i] = 0;
2817   }
2818 
2819   /* create and initialize a linked list for storing column indices of the active row k */
2820   nlnk = am + 1;
2821   ierr = PetscLLCreate(am,am,nlnk,lnk,lnkbt);CHKERRQ(ierr);
2822 
2823   /* initial FreeSpace size is fill*(ai[am]+1) */
2824   ierr = PetscFreeSpaceGet((PetscInt)(fill*(ai[am]+1)),&free_space);CHKERRQ(ierr);
2825   current_space = free_space;
2826 
2827   for (k=0; k<am; k++){  /* for each active row k */
2828     /* initialize lnk by the column indices of row rip[k] of A */
2829     nzk   = 0;
2830     ncols = ai[rip[k]+1] - ai[rip[k]];
2831     if (!ncols) SETERRQ2(PETSC_ERR_MAT_CH_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",rip[k],k);
2832     ncols_upper = 0;
2833     for (j=0; j<ncols; j++){
2834       i = riip[*(aj + ai[rip[k]] + j)];
2835       if (i >= k){ /* only take upper triangular entry */
2836         cols[ncols_upper] = i;
2837         ncols_upper++;
2838       }
2839     }
2840     ierr = PetscLLAdd(ncols_upper,cols,am,nlnk,lnk,lnkbt);CHKERRQ(ierr);
2841     nzk += nlnk;
2842 
2843     /* update lnk by computing fill-in for each pivot row to be merged in */
2844     prow = jl[k]; /* 1st pivot row */
2845 
2846     while (prow < k){
2847       nextprow = jl[prow];
2848       /* merge prow into k-th row */
2849       jmin = il[prow] + 1;  /* index of the 2nd nzero entry in U(prow,k:am-1) */
2850       jmax = ui[prow+1];
2851       ncols = jmax-jmin;
2852       uj_ptr = ui_ptr[prow] + jmin - ui[prow]; /* points to the 2nd nzero entry in U(prow,k:am-1) */
2853       ierr = PetscLLAddSorted(ncols,uj_ptr,am,nlnk,lnk,lnkbt);CHKERRQ(ierr);
2854       nzk += nlnk;
2855 
2856       /* update il and jl for prow */
2857       if (jmin < jmax){
2858         il[prow] = jmin;
2859         j = *uj_ptr; jl[prow] = jl[j]; jl[j] = prow;
2860       }
2861       prow = nextprow;
2862     }
2863 
2864     /* if free space is not available, make more free space */
2865     if (current_space->local_remaining<nzk) {
2866       i  = am - k + 1; /* num of unfactored rows */
2867       i *= PetscMin(nzk,i-1); /* i*nzk, i*(i-1): estimated and max additional space needed */
2868       ierr = PetscFreeSpaceGet(i,&current_space);CHKERRQ(ierr);
2869       reallocs++;
2870     }
2871 
2872     /* copy data into free space, then initialize lnk */
2873     ierr = PetscLLClean(am,am,nzk,lnk,current_space->array,lnkbt);CHKERRQ(ierr);
2874 
2875     /* add the k-th row into il and jl */
2876     if (nzk > 1){
2877       i = current_space->array[1]; /* col value of the first nonzero element in U(k, k+1:am-1) */
2878       jl[k] = jl[i]; jl[i] = k;
2879       il[k] = ui[k] + 1;
2880     }
2881     ui_ptr[k] = current_space->array;
2882     current_space->array           += nzk;
2883     current_space->local_used      += nzk;
2884     current_space->local_remaining -= nzk;
2885 
2886     ui[k+1] = ui[k] + nzk;
2887   }
2888 
2889 #if defined(PETSC_USE_INFO)
2890   if (ai[am] != 0) {
2891     PetscReal af = (PetscReal)(ui[am])/((PetscReal)ai[am]);
2892     ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocs,fill,af);CHKERRQ(ierr);
2893     ierr = PetscInfo1(A,"Run with -pc_factor_fill %G or use \n",af);CHKERRQ(ierr);
2894     ierr = PetscInfo1(A,"PCFactorSetFill(pc,%G) for best performance.\n",af);CHKERRQ(ierr);
2895   } else {
2896      ierr = PetscInfo(A,"Empty matrix.\n");CHKERRQ(ierr);
2897   }
2898 #endif
2899 
2900   ierr = ISRestoreIndices(perm,&rip);CHKERRQ(ierr);
2901   ierr = ISRestoreIndices(iperm,&riip);CHKERRQ(ierr);
2902   ierr = PetscFree4(ui_ptr,jl,il,cols);CHKERRQ(ierr);
2903 
2904   /* destroy list of free space and other temporary array(s) */
2905   ierr = PetscMalloc((ui[am]+1)*sizeof(PetscInt),&uj);CHKERRQ(ierr);
2906   ierr = PetscFreeSpaceContiguous_Cholesky(&free_space,uj,am,ui,udiag);CHKERRQ(ierr); /* store matrix factor */
2907   ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr);
2908 
2909   /* put together the new matrix in MATSEQSBAIJ format */
2910 
2911   b = (Mat_SeqSBAIJ*)fact->data;
2912   b->singlemalloc = PETSC_FALSE;
2913   b->free_a       = PETSC_TRUE;
2914   b->free_ij      = PETSC_TRUE;
2915   ierr = PetscMalloc((ui[am]+1)*sizeof(MatScalar),&b->a);CHKERRQ(ierr);
2916   b->j    = uj;
2917   b->i    = ui;
2918   b->diag = udiag;
2919   b->free_diag = PETSC_TRUE;
2920   b->ilen = 0;
2921   b->imax = 0;
2922   b->row  = perm;
2923   b->col  = perm;
2924   ierr    = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr);
2925   ierr    = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr);
2926   b->icol = iperm;
2927   b->pivotinblocks = PETSC_FALSE; /* need to get from MatFactorInfo */
2928   ierr    = PetscMalloc((am+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr);
2929   ierr    = PetscLogObjectMemory(fact,ui[am]*(sizeof(PetscInt)+sizeof(MatScalar)));CHKERRQ(ierr);
2930   b->maxnz = b->nz = ui[am];
2931 
2932   fact->info.factor_mallocs    = reallocs;
2933   fact->info.fill_ratio_given  = fill;
2934   if (ai[am] != 0) {
2935     fact->info.fill_ratio_needed = ((PetscReal)ui[am])/((PetscReal)ai[am]);
2936   } else {
2937     fact->info.fill_ratio_needed = 0.0;
2938   }
2939   fact->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJ;
2940   PetscFunctionReturn(0);
2941 }
2942 
2943 #undef __FUNCT__
2944 #define __FUNCT__ "MatCholeskyFactorSymbolic_SeqAIJ_inplace"
2945 PetscErrorCode MatCholeskyFactorSymbolic_SeqAIJ_inplace(Mat fact,Mat A,IS perm,const MatFactorInfo *info)
2946 {
2947   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
2948   Mat_SeqSBAIJ       *b;
2949   PetscErrorCode     ierr;
2950   PetscTruth         perm_identity;
2951   PetscReal          fill = info->fill;
2952   const PetscInt     *rip,*riip;
2953   PetscInt           i,am=A->rmap->n,*ai=a->i,*aj=a->j,reallocs=0,prow;
2954   PetscInt           *jl,jmin,jmax,nzk,*ui,k,j,*il,nextprow;
2955   PetscInt           nlnk,*lnk,ncols,ncols_upper,*cols,*uj,**ui_ptr,*uj_ptr;
2956   PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL;
2957   PetscBT            lnkbt;
2958   IS                 iperm;
2959 
2960   PetscFunctionBegin;
2961   if (A->rmap->n != A->cmap->n) SETERRQ2(PETSC_ERR_ARG_WRONG,"Must be square matrix, rows %D columns %D",A->rmap->n,A->cmap->n);
2962   /* check whether perm is the identity mapping */
2963   ierr = ISIdentity(perm,&perm_identity);CHKERRQ(ierr);
2964   ierr = ISInvertPermutation(perm,PETSC_DECIDE,&iperm);CHKERRQ(ierr);
2965   ierr = ISGetIndices(iperm,&riip);CHKERRQ(ierr);
2966   ierr = ISGetIndices(perm,&rip);CHKERRQ(ierr);
2967 
2968   /* initialization */
2969   ierr  = PetscMalloc((am+1)*sizeof(PetscInt),&ui);CHKERRQ(ierr);
2970   ui[0] = 0;
2971 
2972   /* jl: linked list for storing indices of the pivot rows
2973      il: il[i] points to the 1st nonzero entry of U(i,k:am-1) */
2974   ierr = PetscMalloc4(am,PetscInt*,&ui_ptr,am,PetscInt,&jl,am,PetscInt,&il,am,PetscInt,&cols);CHKERRQ(ierr);
2975   for (i=0; i<am; i++){
2976     jl[i] = am; il[i] = 0;
2977   }
2978 
2979   /* create and initialize a linked list for storing column indices of the active row k */
2980   nlnk = am + 1;
2981   ierr = PetscLLCreate(am,am,nlnk,lnk,lnkbt);CHKERRQ(ierr);
2982 
2983   /* initial FreeSpace size is fill*(ai[am]+1) */
2984   ierr = PetscFreeSpaceGet((PetscInt)(fill*(ai[am]+1)),&free_space);CHKERRQ(ierr);
2985   current_space = free_space;
2986 
2987   for (k=0; k<am; k++){  /* for each active row k */
2988     /* initialize lnk by the column indices of row rip[k] of A */
2989     nzk   = 0;
2990     ncols = ai[rip[k]+1] - ai[rip[k]];
2991     if (!ncols) SETERRQ2(PETSC_ERR_MAT_CH_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",rip[k],k);
2992     ncols_upper = 0;
2993     for (j=0; j<ncols; j++){
2994       i = riip[*(aj + ai[rip[k]] + j)];
2995       if (i >= k){ /* only take upper triangular entry */
2996         cols[ncols_upper] = i;
2997         ncols_upper++;
2998       }
2999     }
3000     ierr = PetscLLAdd(ncols_upper,cols,am,nlnk,lnk,lnkbt);CHKERRQ(ierr);
3001     nzk += nlnk;
3002 
3003     /* update lnk by computing fill-in for each pivot row to be merged in */
3004     prow = jl[k]; /* 1st pivot row */
3005 
3006     while (prow < k){
3007       nextprow = jl[prow];
3008       /* merge prow into k-th row */
3009       jmin = il[prow] + 1;  /* index of the 2nd nzero entry in U(prow,k:am-1) */
3010       jmax = ui[prow+1];
3011       ncols = jmax-jmin;
3012       uj_ptr = ui_ptr[prow] + jmin - ui[prow]; /* points to the 2nd nzero entry in U(prow,k:am-1) */
3013       ierr = PetscLLAddSorted(ncols,uj_ptr,am,nlnk,lnk,lnkbt);CHKERRQ(ierr);
3014       nzk += nlnk;
3015 
3016       /* update il and jl for prow */
3017       if (jmin < jmax){
3018         il[prow] = jmin;
3019         j = *uj_ptr; jl[prow] = jl[j]; jl[j] = prow;
3020       }
3021       prow = nextprow;
3022     }
3023 
3024     /* if free space is not available, make more free space */
3025     if (current_space->local_remaining<nzk) {
3026       i = am - k + 1; /* num of unfactored rows */
3027       i = PetscMin(i*nzk, i*(i-1)); /* i*nzk, i*(i-1): estimated and max additional space needed */
3028       ierr = PetscFreeSpaceGet(i,&current_space);CHKERRQ(ierr);
3029       reallocs++;
3030     }
3031 
3032     /* copy data into free space, then initialize lnk */
3033     ierr = PetscLLClean(am,am,nzk,lnk,current_space->array,lnkbt);CHKERRQ(ierr);
3034 
3035     /* add the k-th row into il and jl */
3036     if (nzk-1 > 0){
3037       i = current_space->array[1]; /* col value of the first nonzero element in U(k, k+1:am-1) */
3038       jl[k] = jl[i]; jl[i] = k;
3039       il[k] = ui[k] + 1;
3040     }
3041     ui_ptr[k] = current_space->array;
3042     current_space->array           += nzk;
3043     current_space->local_used      += nzk;
3044     current_space->local_remaining -= nzk;
3045 
3046     ui[k+1] = ui[k] + nzk;
3047   }
3048 
3049 #if defined(PETSC_USE_INFO)
3050   if (ai[am] != 0) {
3051     PetscReal af = (PetscReal)(ui[am])/((PetscReal)ai[am]);
3052     ierr = PetscInfo3(A,"Reallocs %D Fill ratio:given %G needed %G\n",reallocs,fill,af);CHKERRQ(ierr);
3053     ierr = PetscInfo1(A,"Run with -pc_factor_fill %G or use \n",af);CHKERRQ(ierr);
3054     ierr = PetscInfo1(A,"PCFactorSetFill(pc,%G) for best performance.\n",af);CHKERRQ(ierr);
3055   } else {
3056      ierr = PetscInfo(A,"Empty matrix.\n");CHKERRQ(ierr);
3057   }
3058 #endif
3059 
3060   ierr = ISRestoreIndices(perm,&rip);CHKERRQ(ierr);
3061   ierr = ISRestoreIndices(iperm,&riip);CHKERRQ(ierr);
3062   ierr = PetscFree4(ui_ptr,jl,il,cols);CHKERRQ(ierr);
3063 
3064   /* destroy list of free space and other temporary array(s) */
3065   ierr = PetscMalloc((ui[am]+1)*sizeof(PetscInt),&uj);CHKERRQ(ierr);
3066   ierr = PetscFreeSpaceContiguous(&free_space,uj);CHKERRQ(ierr);
3067   ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr);
3068 
3069   /* put together the new matrix in MATSEQSBAIJ format */
3070 
3071   b = (Mat_SeqSBAIJ*)fact->data;
3072   b->singlemalloc = PETSC_FALSE;
3073   b->free_a       = PETSC_TRUE;
3074   b->free_ij      = PETSC_TRUE;
3075   ierr = PetscMalloc((ui[am]+1)*sizeof(MatScalar),&b->a);CHKERRQ(ierr);
3076   b->j    = uj;
3077   b->i    = ui;
3078   b->diag = 0;
3079   b->ilen = 0;
3080   b->imax = 0;
3081   b->row  = perm;
3082   b->col  = perm;
3083   ierr    = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr);
3084   ierr    = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr);
3085   b->icol = iperm;
3086   b->pivotinblocks = PETSC_FALSE; /* need to get from MatFactorInfo */
3087   ierr    = PetscMalloc((am+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr);
3088   ierr    = PetscLogObjectMemory(fact,(ui[am]-am)*(sizeof(PetscInt)+sizeof(MatScalar)));CHKERRQ(ierr);
3089   b->maxnz = b->nz = ui[am];
3090 
3091   fact->info.factor_mallocs    = reallocs;
3092   fact->info.fill_ratio_given  = fill;
3093   if (ai[am] != 0) {
3094     fact->info.fill_ratio_needed = ((PetscReal)ui[am])/((PetscReal)ai[am]);
3095   } else {
3096     fact->info.fill_ratio_needed = 0.0;
3097   }
3098   fact->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJ_inplace;
3099   PetscFunctionReturn(0);
3100 }
3101 
3102 #undef __FUNCT__
3103 #define __FUNCT__ "MatSolve_SeqAIJ_NaturalOrdering"
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;
3114 
3115   PetscFunctionBegin;
3116   if (!n) PetscFunctionReturn(0);
3117 
3118   ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
3119   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
3120 
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   }
3133 
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   }
3143 
3144   ierr = PetscLogFlops(2.0*a->nz - A->cmap->n);CHKERRQ(ierr);
3145   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
3146   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
3147   PetscFunctionReturn(0);
3148 }
3149 
3150 #undef __FUNCT__
3151 #define __FUNCT__ "MatSolve_SeqAIJ"
3152 PetscErrorCode MatSolve_SeqAIJ(Mat A,Vec bb,Vec xx)
3153 {
3154   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
3155   IS                iscol = a->col,isrow = a->row;
3156   PetscErrorCode    ierr;
3157   PetscInt          i,n=A->rmap->n,*vi,*ai=a->i,*aj=a->j,*adiag = a->diag,nz;
3158   const PetscInt    *rout,*cout,*r,*c;
3159   PetscScalar       *x,*tmp,sum;
3160   const PetscScalar *b;
3161   const MatScalar   *aa = a->a,*v;
3162 
3163   PetscFunctionBegin;
3164   if (!n) PetscFunctionReturn(0);
3165 
3166   ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
3167   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
3168   tmp  = a->solve_work;
3169 
3170   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
3171   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout;
3172 
3173   /* forward solve the lower triangular */
3174   tmp[0] = b[r[0]];
3175   v      = aa;
3176   vi     = aj;
3177   for (i=1; i<n; i++) {
3178     nz  = ai[i+1] - ai[i];
3179     sum = b[r[i]];
3180     PetscSparseDenseMinusDot(sum,tmp,v,vi,nz);
3181     tmp[i] = sum;
3182     v += nz; vi += nz;
3183   }
3184 
3185   /* backward solve the upper triangular */
3186   for (i=n-1; i>=0; i--){
3187     v   = aa + adiag[i+1]+1;
3188     vi  = aj + adiag[i+1]+1;
3189     nz  = adiag[i]-adiag[i+1]-1;
3190     sum = tmp[i];
3191     PetscSparseDenseMinusDot(sum,tmp,v,vi,nz);
3192     x[c[i]] = tmp[i] = sum*v[nz]; /* v[nz] = aa[adiag[i]] */
3193   }
3194 
3195   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
3196   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
3197   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
3198   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
3199   ierr = PetscLogFlops(2*a->nz - A->cmap->n);CHKERRQ(ierr);
3200   PetscFunctionReturn(0);
3201 }
3202 
3203 #undef __FUNCT__
3204 #define __FUNCT__ "MatILUDTFactor_SeqAIJ"
3205 /*
3206     This will get a new name and become a varient of MatILUFactor_SeqAIJ() there is no longer seperate functions in the matrix function table for dt factors
3207 */
3208 PetscErrorCode MatILUDTFactor_SeqAIJ(Mat A,IS isrow,IS iscol,const MatFactorInfo *info,Mat *fact)
3209 {
3210   Mat                B = *fact;
3211   Mat_SeqAIJ         *a=(Mat_SeqAIJ*)A->data,*b;
3212   IS                 isicol;
3213   PetscErrorCode     ierr;
3214   const PetscInt     *r,*ic;
3215   PetscInt           i,n=A->rmap->n,*ai=a->i,*aj=a->j,*ajtmp,*adiag;
3216   PetscInt           *bi,*bj,*bdiag,*bdiag_rev;
3217   PetscInt           row,nzi,nzi_bl,nzi_bu,*im,nzi_al,nzi_au;
3218   PetscInt           nlnk,*lnk;
3219   PetscBT            lnkbt;
3220   PetscTruth         row_identity,icol_identity,both_identity;
3221   MatScalar          *aatmp,*pv,*batmp,*ba,*rtmp,*pc,multiplier,*vtmp,diag_tmp;
3222   const PetscInt     *ics;
3223   PetscInt           j,nz,*pj,*bjtmp,k,ncut,*jtmp;
3224   PetscReal          dt=info->dt,dtcol=info->dtcol,shift=info->shiftamount;
3225   PetscInt           dtcount=(PetscInt)info->dtcount,nnz_max;
3226   PetscTruth         missing;
3227 
3228   PetscFunctionBegin;
3229 
3230   if (dt      == PETSC_DEFAULT) dt      = 0.005;
3231   if (dtcol   == PETSC_DEFAULT) dtcol   = 0.01; /* XXX unused! */
3232   if (dtcount == PETSC_DEFAULT) dtcount = (PetscInt)(1.5*a->rmax);
3233 
3234   /* ------- symbolic factorization, can be reused ---------*/
3235   ierr = MatMissingDiagonal(A,&missing,&i);CHKERRQ(ierr);
3236   if (missing) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",i);
3237   adiag=a->diag;
3238 
3239   ierr = ISInvertPermutation(iscol,PETSC_DECIDE,&isicol);CHKERRQ(ierr);
3240 
3241   /* bdiag is location of diagonal in factor */
3242   ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bdiag);CHKERRQ(ierr);     /* becomes b->diag */
3243   ierr = PetscMalloc((n+1)*sizeof(PetscInt),&bdiag_rev);CHKERRQ(ierr); /* temporary */
3244 
3245   /* allocate row pointers bi */
3246   ierr = PetscMalloc((2*n+2)*sizeof(PetscInt),&bi);CHKERRQ(ierr);
3247 
3248   /* allocate bj and ba; max num of nonzero entries is (ai[n]+2*n*dtcount+2) */
3249   if (dtcount > n-1) dtcount = n-1; /* diagonal is excluded */
3250   nnz_max  = ai[n]+2*n*dtcount+2;
3251 
3252   ierr = PetscMalloc((nnz_max+1)*sizeof(PetscInt),&bj);CHKERRQ(ierr);
3253   ierr = PetscMalloc((nnz_max+1)*sizeof(MatScalar),&ba);CHKERRQ(ierr);
3254 
3255   /* put together the new matrix */
3256   ierr = MatSeqAIJSetPreallocation_SeqAIJ(B,MAT_SKIP_ALLOCATION,PETSC_NULL);CHKERRQ(ierr);
3257   ierr = PetscLogObjectParent(B,isicol);CHKERRQ(ierr);
3258   b    = (Mat_SeqAIJ*)B->data;
3259   b->free_a       = PETSC_TRUE;
3260   b->free_ij      = PETSC_TRUE;
3261   b->singlemalloc = PETSC_FALSE;
3262   b->a          = ba;
3263   b->j          = bj;
3264   b->i          = bi;
3265   b->diag       = bdiag;
3266   b->ilen       = 0;
3267   b->imax       = 0;
3268   b->row        = isrow;
3269   b->col        = iscol;
3270   ierr          = PetscObjectReference((PetscObject)isrow);CHKERRQ(ierr);
3271   ierr          = PetscObjectReference((PetscObject)iscol);CHKERRQ(ierr);
3272   b->icol       = isicol;
3273   ierr = PetscMalloc((n+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr);
3274 
3275   ierr = PetscLogObjectMemory(B,nnz_max*(sizeof(PetscInt)+sizeof(MatScalar)));CHKERRQ(ierr);
3276   b->maxnz = nnz_max;
3277 
3278   B->factor                = MAT_FACTOR_ILUDT;
3279   B->info.factor_mallocs   = 0;
3280   B->info.fill_ratio_given = ((PetscReal)nnz_max)/((PetscReal)ai[n]);
3281   CHKMEMQ;
3282   /* ------- end of symbolic factorization ---------*/
3283 
3284   ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr);
3285   ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr);
3286   ics  = ic;
3287 
3288   /* linked list for storing column indices of the active row */
3289   nlnk = n + 1;
3290   ierr = PetscLLCreate(n,n,nlnk,lnk,lnkbt);CHKERRQ(ierr);
3291 
3292   /* im: used by PetscLLAddSortedLU(); jtmp: working array for column indices of active row */
3293   ierr = PetscMalloc2(n,PetscInt,&im,n,PetscInt,&jtmp);CHKERRQ(ierr);
3294   /* rtmp, vtmp: working arrays for sparse and contiguous row entries of active row */
3295   ierr = PetscMalloc2(n,MatScalar,&rtmp,n,MatScalar,&vtmp);CHKERRQ(ierr);
3296   ierr = PetscMemzero(rtmp,n*sizeof(MatScalar));CHKERRQ(ierr);
3297 
3298   bi[0]    = 0;
3299   bdiag[0] = nnz_max-1; /* location of diag[0] in factor B */
3300   bdiag_rev[n] = bdiag[0];
3301   bi[2*n+1] = bdiag[0]+1; /* endof bj and ba array */
3302   for (i=0; i<n; i++) {
3303     /* copy initial fill into linked list */
3304     nzi = 0; /* nonzeros for active row i */
3305     nzi = ai[r[i]+1] - ai[r[i]];
3306     if (!nzi) SETERRQ2(PETSC_ERR_MAT_LU_ZRPVT,"Empty row in matrix: row in original ordering %D in permuted ordering %D",r[i],i);
3307     nzi_al = adiag[r[i]] - ai[r[i]];
3308     nzi_au = ai[r[i]+1] - adiag[r[i]] -1;
3309     ajtmp = aj + ai[r[i]];
3310     ierr = PetscLLAddPerm(nzi,ajtmp,ic,n,nlnk,lnk,lnkbt);CHKERRQ(ierr);
3311 
3312     /* load in initial (unfactored row) */
3313     aatmp = a->a + ai[r[i]];
3314     for (j=0; j<nzi; j++) {
3315       rtmp[ics[*ajtmp++]] = *aatmp++;
3316     }
3317 
3318     /* add pivot rows into linked list */
3319     row = lnk[n];
3320     while (row < i ) {
3321       nzi_bl = bi[row+1] - bi[row] + 1;
3322       bjtmp = bj + bdiag[row+1]+1; /* points to 1st column next to the diagonal in U */
3323       ierr  = PetscLLAddSortedLU(bjtmp,row,nlnk,lnk,lnkbt,i,nzi_bl,im);CHKERRQ(ierr);
3324       nzi  += nlnk;
3325       row   = lnk[row];
3326     }
3327 
3328     /* copy data from lnk into jtmp, then initialize lnk */
3329     ierr = PetscLLClean(n,n,nzi,lnk,jtmp,lnkbt);CHKERRQ(ierr);
3330 
3331     /* numerical factorization */
3332     bjtmp = jtmp;
3333     row   = *bjtmp++; /* 1st pivot row */
3334     while  ( row < i ) {
3335       pc         = rtmp + row;
3336       pv         = ba + bdiag[row]; /* 1./(diag of the pivot row) */
3337       multiplier = (*pc) * (*pv);
3338       *pc        = multiplier;
3339       if (PetscAbsScalar(*pc) > dt){ /* apply tolerance dropping rule */
3340         pj         = bj + bdiag[row+1] + 1; /* point to 1st entry of U(row,:) */
3341         pv         = ba + bdiag[row+1] + 1;
3342         /* if (multiplier < -1.0 or multiplier >1.0) printf("row/prow %d, %d, multiplier %g\n",i,row,multiplier); */
3343         nz         = bdiag[row] - bdiag[row+1] - 1; /* num of entries in U(row,:), excluding diagonal */
3344         for (j=0; j<nz; j++) rtmp[*pj++] -= multiplier * (*pv++);
3345         ierr = PetscLogFlops(2.0*nz);CHKERRQ(ierr);
3346       }
3347       row = *bjtmp++;
3348     }
3349 
3350     /* copy sparse rtmp into contiguous vtmp; separate L and U part */
3351     diag_tmp = rtmp[i];  /* save diagonal value - may not needed?? */
3352     nzi_bl = 0; j = 0;
3353     while (jtmp[j] < i){ /* Note: jtmp is sorted */
3354       vtmp[j] = rtmp[jtmp[j]]; rtmp[jtmp[j]]=0.0;
3355       nzi_bl++; j++;
3356     }
3357     nzi_bu = nzi - nzi_bl -1;
3358     while (j < nzi){
3359       vtmp[j] = rtmp[jtmp[j]]; rtmp[jtmp[j]]=0.0;
3360       j++;
3361     }
3362 
3363     bjtmp = bj + bi[i];
3364     batmp = ba + bi[i];
3365     /* apply level dropping rule to L part */
3366     ncut = nzi_al + dtcount;
3367     if (ncut < nzi_bl){
3368       ierr = PetscSortSplit(ncut,nzi_bl,vtmp,jtmp);CHKERRQ(ierr);
3369       ierr = PetscSortIntWithScalarArray(ncut,jtmp,vtmp);CHKERRQ(ierr);
3370     } else {
3371       ncut = nzi_bl;
3372     }
3373     for (j=0; j<ncut; j++){
3374       bjtmp[j] = jtmp[j];
3375       batmp[j] = vtmp[j];
3376       /* printf(" (%d,%g),",bjtmp[j],batmp[j]); */
3377     }
3378     bi[i+1] = bi[i] + ncut;
3379     nzi = ncut + 1;
3380 
3381     /* apply level dropping rule to U part */
3382     ncut = nzi_au + dtcount;
3383     if (ncut < nzi_bu){
3384       ierr = PetscSortSplit(ncut,nzi_bu,vtmp+nzi_bl+1,jtmp+nzi_bl+1);CHKERRQ(ierr);
3385       ierr = PetscSortIntWithScalarArray(ncut,jtmp+nzi_bl+1,vtmp+nzi_bl+1);CHKERRQ(ierr);
3386     } else {
3387       ncut = nzi_bu;
3388     }
3389     nzi += ncut;
3390 
3391     /* mark bdiagonal */
3392     bdiag[i+1]       = bdiag[i] - (ncut + 1);
3393     bdiag_rev[n-i-1] = bdiag[i+1];
3394     bi[2*n - i]      = bi[2*n - i +1] - (ncut + 1);
3395     bjtmp = bj + bdiag[i];
3396     batmp = ba + bdiag[i];
3397     *bjtmp = i;
3398     *batmp = diag_tmp; /* rtmp[i]; */
3399     if (*batmp == 0.0) {
3400       *batmp = dt+shift;
3401       /* printf(" row %d add shift %g\n",i,shift); */
3402     }
3403     *batmp = 1.0/(*batmp); /* invert diagonal entries for simplier triangular solves */
3404     /* printf(" (%d,%g),",*bjtmp,*batmp); */
3405 
3406     bjtmp = bj + bdiag[i+1]+1;
3407     batmp = ba + bdiag[i+1]+1;
3408     for (k=0; k<ncut; k++){
3409       bjtmp[k] = jtmp[nzi_bl+1+k];
3410       batmp[k] = vtmp[nzi_bl+1+k];
3411       /* printf(" (%d,%g),",bjtmp[k],batmp[k]); */
3412     }
3413     /* printf("\n"); */
3414 
3415     im[i]   = nzi; /* used by PetscLLAddSortedLU() */
3416     /*
3417     printf("row %d: bi %d, bdiag %d\n",i,bi[i],bdiag[i]);
3418     printf(" ----------------------------\n");
3419     */
3420   } /* for (i=0; i<n; i++) */
3421   /* printf("end of L %d, beginning of U %d\n",bi[n],bdiag[n]); */
3422   if (bi[n] >= bdiag[n]) SETERRQ2(PETSC_ERR_ARG_SIZ,"end of L array %d cannot >= the beginning of U array %d",bi[n],bdiag[n]);
3423 
3424   ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr);
3425   ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr);
3426 
3427   ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr);
3428   ierr = PetscFree2(im,jtmp);CHKERRQ(ierr);
3429   ierr = PetscFree2(rtmp,vtmp);CHKERRQ(ierr);
3430   ierr = PetscFree(bdiag_rev);CHKERRQ(ierr);
3431 
3432   ierr = PetscLogFlops(B->cmap->n);CHKERRQ(ierr);
3433   b->maxnz = b->nz = bi[n] + bdiag[0] - bdiag[n];
3434 
3435   ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr);
3436   ierr = ISIdentity(isicol,&icol_identity);CHKERRQ(ierr);
3437   both_identity = (PetscTruth) (row_identity && icol_identity);
3438   if (row_identity && icol_identity) {
3439     B->ops->solve = MatSolve_SeqAIJ_NaturalOrdering;
3440   } else {
3441     B->ops->solve = MatSolve_SeqAIJ;
3442   }
3443 
3444   B->ops->solveadd          = 0;
3445   B->ops->solvetranspose    = 0;
3446   B->ops->solvetransposeadd = 0;
3447   B->ops->matsolve          = 0;
3448   B->assembled              = PETSC_TRUE;
3449   B->preallocated           = PETSC_TRUE;
3450   PetscFunctionReturn(0);
3451 }
3452 
3453 /* a wraper of MatILUDTFactor_SeqAIJ() */
3454 #undef __FUNCT__
3455 #define __FUNCT__ "MatILUDTFactorSymbolic_SeqAIJ"
3456 /*
3457     This will get a new name and become a varient of MatILUFactor_SeqAIJ() there is no longer seperate functions in the matrix function table for dt factors
3458 */
3459 
3460 PetscErrorCode PETSCMAT_DLLEXPORT MatILUDTFactorSymbolic_SeqAIJ(Mat fact,Mat A,IS row,IS col,const MatFactorInfo *info)
3461 {
3462   PetscErrorCode     ierr;
3463 
3464   PetscFunctionBegin;
3465   ierr = MatILUDTFactor_SeqAIJ(A,row,col,info,&fact);CHKERRQ(ierr);
3466   PetscFunctionReturn(0);
3467 }
3468 
3469 /*
3470    same as MatLUFactorNumeric_SeqAIJ(), except using contiguous array matrix factors
3471    - intend to replace existing MatLUFactorNumeric_SeqAIJ()
3472 */
3473 #undef __FUNCT__
3474 #define __FUNCT__ "MatILUDTFactorNumeric_SeqAIJ"
3475 /*
3476     This will get a new name and become a varient of MatILUFactor_SeqAIJ() there is no longer seperate functions in the matrix function table for dt factors
3477 */
3478 
3479 PetscErrorCode PETSCMAT_DLLEXPORT MatILUDTFactorNumeric_SeqAIJ(Mat fact,Mat A,const MatFactorInfo *info)
3480 {
3481   Mat            C=fact;
3482   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data,*b=(Mat_SeqAIJ *)C->data;
3483   IS             isrow = b->row,isicol = b->icol;
3484   PetscErrorCode ierr;
3485   const PetscInt *r,*ic,*ics;
3486   PetscInt       i,j,k,n=A->rmap->n,*ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j;
3487   PetscInt       *ajtmp,*bjtmp,nz,nzl,nzu,row,*bdiag = b->diag,*pj;
3488   MatScalar      *rtmp,*pc,multiplier,*v,*pv,*aa=a->a;
3489   PetscReal      dt=info->dt,shift=info->shiftamount;
3490   PetscTruth     row_identity, col_identity;
3491 
3492   PetscFunctionBegin;
3493   ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr);
3494   ierr = ISGetIndices(isicol,&ic);CHKERRQ(ierr);
3495   ierr = PetscMalloc((n+1)*sizeof(MatScalar),&rtmp);CHKERRQ(ierr);
3496   ics  = ic;
3497 
3498   for (i=0; i<n; i++){
3499     /* initialize rtmp array */
3500     nzl   = bi[i+1] - bi[i];       /* num of nozeros in L(i,:) */
3501     bjtmp = bj + bi[i];
3502     for  (j=0; j<nzl; j++) rtmp[*bjtmp++] = 0.0;
3503     rtmp[i] = 0.0;
3504     nzu   = bdiag[i] - bdiag[i+1]; /* num of nozeros in U(i,:) */
3505     bjtmp = bj + bdiag[i+1] + 1;
3506     for  (j=0; j<nzu; j++) rtmp[*bjtmp++] = 0.0;
3507 
3508     /* load in initial unfactored row of A */
3509     /* printf("row %d\n",i); */
3510     nz    = ai[r[i]+1] - ai[r[i]];
3511     ajtmp = aj + ai[r[i]];
3512     v     = aa + ai[r[i]];
3513     for (j=0; j<nz; j++) {
3514       rtmp[ics[*ajtmp++]] = v[j];
3515       /* printf(" (%d,%g),",ics[ajtmp[j]],rtmp[ics[ajtmp[j]]]); */
3516     }
3517     /* printf("\n"); */
3518 
3519     /* numerical factorization */
3520     bjtmp = bj + bi[i]; /* point to 1st entry of L(i,:) */
3521     nzl   = bi[i+1] - bi[i]; /* num of entries in L(i,:) */
3522     k = 0;
3523     while (k < nzl){
3524       row   = *bjtmp++;
3525       /* printf("  prow %d\n",row); */
3526       pc         = rtmp + row;
3527       pv         = b->a + bdiag[row]; /* 1./(diag of the pivot row) */
3528       multiplier = (*pc) * (*pv);
3529       *pc        = multiplier;
3530       if (PetscAbsScalar(multiplier) > dt){
3531         pj         = bj + bdiag[row+1] + 1; /* point to 1st entry of U(row,:) */
3532         pv         = b->a + bdiag[row+1] + 1;
3533         nz         = bdiag[row] - bdiag[row+1] - 1; /* num of entries in U(row,:), excluding diagonal */
3534         for (j=0; j<nz; j++) rtmp[*pj++] -= multiplier * (*pv++);
3535         /* ierr = PetscLogFlops(2.0*nz);CHKERRQ(ierr); */
3536       }
3537       k++;
3538     }
3539 
3540     /* finished row so stick it into b->a */
3541     /* L-part */
3542     pv = b->a + bi[i] ;
3543     pj = bj + bi[i] ;
3544     nzl = bi[i+1] - bi[i];
3545     for (j=0; j<nzl; j++) {
3546       pv[j] = rtmp[pj[j]];
3547       /* printf(" (%d,%g),",pj[j],pv[j]); */
3548     }
3549 
3550     /* diagonal: invert diagonal entries for simplier triangular solves */
3551     if (rtmp[i] == 0.0) rtmp[i] = dt+shift;
3552     b->a[bdiag[i]] = 1.0/rtmp[i];
3553     /* printf(" (%d,%g),",i,b->a[bdiag[i]]); */
3554 
3555     /* U-part */
3556     pv = b->a + bdiag[i+1] + 1;
3557     pj = bj + bdiag[i+1] + 1;
3558     nzu = bdiag[i] - bdiag[i+1] - 1;
3559     for (j=0; j<nzu; j++) {
3560       pv[j] = rtmp[pj[j]];
3561       /* printf(" (%d,%g),",pj[j],pv[j]); */
3562     }
3563     /* printf("\n"); */
3564   }
3565 
3566   ierr = PetscFree(rtmp);CHKERRQ(ierr);
3567   ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr);
3568   ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr);
3569 
3570   ierr = ISIdentity(isrow,&row_identity);CHKERRQ(ierr);
3571   ierr = ISIdentity(isicol,&col_identity);CHKERRQ(ierr);
3572   if (row_identity && col_identity) {
3573     C->ops->solve   = MatSolve_SeqAIJ_NaturalOrdering;
3574   } else {
3575     C->ops->solve   = MatSolve_SeqAIJ;
3576   }
3577   C->ops->solveadd           = 0;
3578   C->ops->solvetranspose     = 0;
3579   C->ops->solvetransposeadd  = 0;
3580   C->ops->matsolve           = 0;
3581   C->assembled    = PETSC_TRUE;
3582   C->preallocated = PETSC_TRUE;
3583   ierr = PetscLogFlops(C->cmap->n);CHKERRQ(ierr);
3584   PetscFunctionReturn(0);
3585 }
3586