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