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