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