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