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