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