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