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