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