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