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