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