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