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