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