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