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