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