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