xref: /petsc/src/mat/impls/aij/seq/aijfact.c (revision 703deb492105f35ecf8a6e6c03ef9f487e6ac355)
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*703deb49SSatish Balay static char vcid[] = "$Id: aijfact.c,v 1.110 1998/10/09 19:22:05 bsmith Exp balay $";
3cb512458SBarry Smith #endif
4289bc588SBarry Smith 
570f55243SBarry Smith #include "src/mat/impls/aij/seq/aij.h"
690f02eecSBarry Smith #include "src/vec/vecimpl.h"
71c4feecaSSatish Balay #include "src/inline/dot.h"
83b2fbd54SBarry Smith 
95615d1e5SSatish Balay #undef __FUNC__
10d4bb536fSBarry Smith #define __FUNC__ "MatOrder_Flow_SeqAIJ"
11ca161407SBarry Smith int MatOrder_Flow_SeqAIJ(Mat mat,MatReorderingType type,IS *irow,IS *icol)
123b2fbd54SBarry Smith {
133a40ed3dSBarry Smith   PetscFunctionBegin;
143a40ed3dSBarry Smith 
15e3372554SBarry Smith   SETERRQ(PETSC_ERR_SUP,0,"Code not written");
16d64ed03dSBarry Smith #if !defined(USE_PETSC_DEBUG)
17d64ed03dSBarry Smith   PetscFunctionReturn(0);
18d64ed03dSBarry Smith #endif
193b2fbd54SBarry Smith }
203b2fbd54SBarry Smith 
21289bc588SBarry Smith /*
22289bc588SBarry Smith     Factorization code for AIJ format.
23289bc588SBarry Smith */
245615d1e5SSatish Balay #undef __FUNC__
255615d1e5SSatish Balay #define __FUNC__ "MatLUFactorSymbolic_SeqAIJ"
26416022c9SBarry Smith int MatLUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,Mat *B)
27289bc588SBarry Smith {
28416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b;
29289bc588SBarry Smith   IS         isicol;
30416022c9SBarry Smith   int        *r,*ic, ierr, i, n = a->m, *ai = a->i, *aj = a->j;
31416022c9SBarry Smith   int        *ainew,*ajnew, jmax,*fill, *ajtmp, nz,shift = a->indexshift;
3291bf9adeSBarry Smith   int        *idnew, idx, row,m,fm, nnz, nzi, realloc = 0,nzbd,*im;
33289bc588SBarry Smith 
343a40ed3dSBarry Smith   PetscFunctionBegin;
35d3cbd50cSLois Curfman McInnes   PetscValidHeaderSpecific(isrow,IS_COOKIE);
36d3cbd50cSLois Curfman McInnes   PetscValidHeaderSpecific(iscol,IS_COOKIE);
373b2fbd54SBarry Smith 
3878b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr);
39ac121b3dSBarry Smith   ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr);
40ac121b3dSBarry Smith   ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr);
41289bc588SBarry Smith 
42289bc588SBarry Smith   /* get new row pointers */
430452661fSBarry Smith   ainew    = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew);
44dbb450caSBarry Smith   ainew[0] = -shift;
45289bc588SBarry Smith   /* don't know how many column pointers are needed so estimate */
46dbb450caSBarry Smith   jmax  = (int) (f*ai[n]+(!shift));
470452661fSBarry Smith   ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew);
48289bc588SBarry Smith   /* fill is a linked list of nonzeros in active row */
490452661fSBarry Smith   fill = (int *) PetscMalloc( (2*n+1)*sizeof(int)); CHKPTRQ(fill);
502fb3ed76SBarry Smith   im   = fill + n + 1;
51289bc588SBarry Smith   /* idnew is location of diagonal in factor */
520452661fSBarry Smith   idnew    = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(idnew);
53dbb450caSBarry Smith   idnew[0] = -shift;
54289bc588SBarry Smith 
55289bc588SBarry Smith   for ( i=0; i<n; i++ ) {
56289bc588SBarry Smith     /* first copy previous fill into linked list */
57289bc588SBarry Smith     nnz     = nz    = ai[r[i]+1] - ai[r[i]];
586b96ef82SSatish Balay     if (!nz) SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,1,"Empty row in matrix");
59dbb450caSBarry Smith     ajtmp   = aj + ai[r[i]] + shift;
60289bc588SBarry Smith     fill[n] = n;
61289bc588SBarry Smith     while (nz--) {
62289bc588SBarry Smith       fm  = n;
63dbb450caSBarry Smith       idx = ic[*ajtmp++ + shift];
64289bc588SBarry Smith       do {
65289bc588SBarry Smith         m  = fm;
66289bc588SBarry Smith         fm = fill[m];
67289bc588SBarry Smith       } while (fm < idx);
68289bc588SBarry Smith       fill[m]   = idx;
69289bc588SBarry Smith       fill[idx] = fm;
70289bc588SBarry Smith     }
71289bc588SBarry Smith     row = fill[n];
72289bc588SBarry Smith     while ( row < i ) {
73dbb450caSBarry Smith       ajtmp = ajnew + idnew[row] + (!shift);
742fb3ed76SBarry Smith       nzbd  = 1 + idnew[row] - ainew[row];
752fb3ed76SBarry Smith       nz    = im[row] - nzbd;
76289bc588SBarry Smith       fm    = row;
772fb3ed76SBarry Smith       while (nz-- > 0) {
78dbb450caSBarry Smith         idx = *ajtmp++ + shift;
792fb3ed76SBarry Smith         nzbd++;
802fb3ed76SBarry Smith         if (idx == i) im[row] = nzbd;
81289bc588SBarry Smith         do {
82289bc588SBarry Smith           m  = fm;
83289bc588SBarry Smith           fm = fill[m];
84289bc588SBarry Smith         } while (fm < idx);
85289bc588SBarry Smith         if (fm != idx) {
86289bc588SBarry Smith           fill[m]   = idx;
87289bc588SBarry Smith           fill[idx] = fm;
88289bc588SBarry Smith           fm        = idx;
89289bc588SBarry Smith           nnz++;
90289bc588SBarry Smith         }
91289bc588SBarry Smith       }
92289bc588SBarry Smith       row = fill[row];
93289bc588SBarry Smith     }
94289bc588SBarry Smith     /* copy new filled row into permanent storage */
95289bc588SBarry Smith     ainew[i+1] = ainew[i] + nnz;
96496e697eSBarry Smith     if (ainew[i+1] > jmax) {
97ecf371e4SBarry Smith 
98ecf371e4SBarry Smith       /* estimate how much additional space we will need */
99ecf371e4SBarry Smith       /* use the strategy suggested by David Hysom <hysom@perch-t.icase.edu> */
100ecf371e4SBarry Smith       /* just double the memory each time */
101ecf371e4SBarry Smith       int maxadd = jmax;
102ecf371e4SBarry Smith       /* maxadd = (int) ((f*(ai[n]+(!shift))*(n-i+5))/n); */
103bbb6d6a8SBarry Smith       if (maxadd < nnz) maxadd = (n-i)*(nnz+1);
1042fb3ed76SBarry Smith       jmax += maxadd;
105ecf371e4SBarry Smith 
106ecf371e4SBarry Smith       /* allocate a longer ajnew */
1070452661fSBarry Smith       ajtmp = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(ajtmp);
108416022c9SBarry Smith       PetscMemcpy(ajtmp,ajnew,(ainew[i]+shift)*sizeof(int));
1090452661fSBarry Smith       PetscFree(ajnew);
110289bc588SBarry Smith       ajnew = ajtmp;
1112fb3ed76SBarry Smith       realloc++; /* count how many times we realloc */
112289bc588SBarry Smith     }
113dbb450caSBarry Smith     ajtmp = ajnew + ainew[i] + shift;
114289bc588SBarry Smith     fm    = fill[n];
115289bc588SBarry Smith     nzi   = 0;
1162fb3ed76SBarry Smith     im[i] = nnz;
117289bc588SBarry Smith     while (nnz--) {
118289bc588SBarry Smith       if (fm < i) nzi++;
119dbb450caSBarry Smith       *ajtmp++ = fm - shift;
120289bc588SBarry Smith       fm       = fill[fm];
121289bc588SBarry Smith     }
122289bc588SBarry Smith     idnew[i] = ainew[i] + nzi;
123289bc588SBarry Smith   }
124464e8d44SSatish Balay   if (ai[n] != 0) {
125464e8d44SSatish Balay     double af = ((double)ainew[n])/((double)ai[n]);
126981c4779SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n",
127f64065bbSBarry Smith              realloc,f,af);
128981c4779SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:Run with -pc_lu_fill %g or use \n",af);
129981c4779SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:PCLUSetFill(pc,%g);\n",af);
130981c4779SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ:for best performance.\n");
13105bf559cSSatish Balay   } else {
132981c4779SBarry Smith     PLogInfo(A,"MatLUFactorSymbolic_SeqAIJ: Empty matrix\n");
13305bf559cSSatish Balay   }
1342fb3ed76SBarry Smith 
135898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
136898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
1371987afe7SBarry Smith 
1380452661fSBarry Smith   PetscFree(fill);
139289bc588SBarry Smith 
140289bc588SBarry Smith   /* put together the new matrix */
141b4fd4287SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,B); CHKERRQ(ierr);
1421987afe7SBarry Smith   PLogObjectParent(*B,isicol);
143416022c9SBarry Smith   b = (Mat_SeqAIJ *) (*B)->data;
1440452661fSBarry Smith   PetscFree(b->imax);
145416022c9SBarry Smith   b->singlemalloc = 0;
146e8d4e0b9SBarry Smith   /* the next line frees the default space generated by the Create() */
1470452661fSBarry Smith   PetscFree(b->a); PetscFree(b->ilen);
14891bf9adeSBarry Smith   b->a          = (Scalar *) PetscMalloc((ainew[n]+shift+1)*sizeof(Scalar));CHKPTRQ(b->a);
149416022c9SBarry Smith   b->j          = ajnew;
150416022c9SBarry Smith   b->i          = ainew;
151416022c9SBarry Smith   b->diag       = idnew;
152416022c9SBarry Smith   b->ilen       = 0;
153416022c9SBarry Smith   b->imax       = 0;
154416022c9SBarry Smith   b->row        = isrow;
155416022c9SBarry Smith   b->col        = iscol;
15682bf6240SBarry Smith   b->icol       = isicol;
15791bf9adeSBarry Smith   b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar));CHKPTRQ(b->solve_work);
158416022c9SBarry Smith   /* In b structure:  Free imax, ilen, old a, old j.
1597fd98bd6SLois Curfman McInnes      Allocate idnew, solve_work, new a, new j */
160416022c9SBarry Smith   PLogObjectMemory(*B,(ainew[n]+shift-n)*(sizeof(int)+sizeof(Scalar)));
161416022c9SBarry Smith   b->maxnz = b->nz = ainew[n] + shift;
1627fd98bd6SLois Curfman McInnes 
163e93ccc4dSBarry Smith   (*B)->factor                 =  FACTOR_LU;;
164ae068f58SLois Curfman McInnes   (*B)->info.factor_mallocs    = realloc;
165ae068f58SLois Curfman McInnes   (*B)->info.fill_ratio_given  = f;
166*703deb49SSatish Balay   (*B)->ops->lufactornumeric   =  A->ops->lufactornumeric; /* Use Inode variant if A has inodes */
167*703deb49SSatish Balay 
168e93ccc4dSBarry Smith   if (ai[n] != 0) {
169e93ccc4dSBarry Smith     (*B)->info.fill_ratio_needed = ((double)ainew[n])/((double)ai[n]);
170eea2913aSSatish Balay   } else {
171eea2913aSSatish Balay     (*B)->info.fill_ratio_needed = 0.0;
172eea2913aSSatish Balay   }
1733a40ed3dSBarry Smith   PetscFunctionReturn(0);
174289bc588SBarry Smith }
1750a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
17641c01911SSatish Balay int Mat_AIJ_CheckInode(Mat);
17741c01911SSatish Balay 
1785615d1e5SSatish Balay #undef __FUNC__
1795615d1e5SSatish Balay #define __FUNC__ "MatLUFactorNumeric_SeqAIJ"
180416022c9SBarry Smith int MatLUFactorNumeric_SeqAIJ(Mat A,Mat *B)
181289bc588SBarry Smith {
182416022c9SBarry Smith   Mat        C = *B;
183416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b = (Mat_SeqAIJ *)C->data;
18482bf6240SBarry Smith   IS         isrow = b->row, isicol = b->icol;
185416022c9SBarry Smith   int        *r,*ic, ierr, i, j, n = a->m, *ai = b->i, *aj = b->j;
1861987afe7SBarry Smith   int        *ajtmpold, *ajtmp, nz, row, *ics, shift = a->indexshift;
187f2582111SSatish Balay   int        *diag_offset = b->diag,diag,k;
18835aab85fSBarry Smith   int        preserve_row_sums = (int) a->ilu_preserve_row_sums;
1893a40ed3dSBarry Smith   register   int    *pj;
1908ecf7165SBarry Smith   Scalar     *rtmp,*v, *pc, multiplier,sum,inner_sum,*rowsums = 0;
19135aab85fSBarry Smith   double     ssum;
19235aab85fSBarry Smith   register   Scalar *pv, *rtmps,*u_values;
193289bc588SBarry Smith 
1943a40ed3dSBarry Smith   PetscFunctionBegin;
19582bf6240SBarry Smith 
19678b31e54SBarry Smith   ierr  = ISGetIndices(isrow,&r); CHKERRQ(ierr);
19778b31e54SBarry Smith   ierr  = ISGetIndices(isicol,&ic); CHKERRQ(ierr);
1980452661fSBarry Smith   rtmp  = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar) ); CHKPTRQ(rtmp);
19913ae1565SBarry Smith   PetscMemzero(rtmp,(n+1)*sizeof(Scalar));
2000cf60383SBarry Smith   rtmps = rtmp + shift; ics = ic + shift;
201289bc588SBarry Smith 
20235aab85fSBarry Smith   /* precalcuate row sums */
20335aab85fSBarry Smith   if (preserve_row_sums) {
20435aab85fSBarry Smith     rowsums = (Scalar *) PetscMalloc( n*sizeof(Scalar) ); CHKPTRQ(rowsums);
20535aab85fSBarry Smith     for ( i=0; i<n; i++ ) {
20635aab85fSBarry Smith       nz  = a->i[r[i]+1] - a->i[r[i]];
20735aab85fSBarry Smith       v   = a->a + a->i[r[i]] + shift;
20835aab85fSBarry Smith       sum = 0.0;
20935aab85fSBarry Smith       for ( j=0; j<nz; j++ ) sum += v[j];
21035aab85fSBarry Smith       rowsums[i] = sum;
21135aab85fSBarry Smith     }
21235aab85fSBarry Smith   }
21335aab85fSBarry Smith 
214289bc588SBarry Smith   for ( i=0; i<n; i++ ) {
215289bc588SBarry Smith     nz    = ai[i+1] - ai[i];
216dbb450caSBarry Smith     ajtmp = aj + ai[i] + shift;
21748e94559SBarry Smith     for  ( j=0; j<nz; j++ ) rtmps[ajtmp[j]] = 0.0;
218289bc588SBarry Smith 
219289bc588SBarry Smith     /* load in initial (unfactored row) */
220416022c9SBarry Smith     nz       = a->i[r[i]+1] - a->i[r[i]];
221416022c9SBarry Smith     ajtmpold = a->j + a->i[r[i]] + shift;
222416022c9SBarry Smith     v        = a->a + a->i[r[i]] + shift;
2230cf60383SBarry Smith     for ( j=0; j<nz; j++ ) rtmp[ics[ajtmpold[j]]] =  v[j];
224289bc588SBarry Smith 
225dbb450caSBarry Smith     row = *ajtmp++ + shift;
226f2582111SSatish Balay       while  (row < i ) {
2278c37ef55SBarry Smith       pc = rtmp + row;
2288c37ef55SBarry Smith       if (*pc != 0.0) {
2291987afe7SBarry Smith         pv         = b->a + diag_offset[row] + shift;
2301987afe7SBarry Smith         pj         = b->j + diag_offset[row] + (!shift);
23135aab85fSBarry Smith         multiplier = *pc / *pv++;
2328c37ef55SBarry Smith         *pc        = multiplier;
233f2582111SSatish Balay         nz         = ai[row+1] - diag_offset[row] - 1;
234f2582111SSatish Balay         for (j=0; j<nz; j++) rtmps[pj[j]] -= multiplier * pv[j];
235f2582111SSatish Balay         PLogFlops(2*nz);
236289bc588SBarry Smith       }
237dbb450caSBarry Smith       row = *ajtmp++ + shift;
238289bc588SBarry Smith     }
239416022c9SBarry Smith     /* finished row so stick it into b->a */
240416022c9SBarry Smith     pv = b->a + ai[i] + shift;
241416022c9SBarry Smith     pj = b->j + ai[i] + shift;
2428c37ef55SBarry Smith     nz = ai[i+1] - ai[i];
243416022c9SBarry Smith     for ( j=0; j<nz; j++ ) {pv[j] = rtmps[pj[j]];}
24435aab85fSBarry Smith     diag = diag_offset[i] - ai[i];
24535aab85fSBarry Smith     /*
24635aab85fSBarry Smith           Possibly adjust diagonal entry on current row to force
24735aab85fSBarry Smith         LU matrix to have same row sum as initial matrix.
24835aab85fSBarry Smith     */
249d708749eSBarry Smith     if (pv[diag] == 0.0) {
250d708749eSBarry Smith       SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,0,"Zero pivot");
251d708749eSBarry Smith     }
25235aab85fSBarry Smith     if (preserve_row_sums) {
25335aab85fSBarry Smith       pj  = b->j + ai[i] + shift;
25435aab85fSBarry Smith       sum = rowsums[i];
25535aab85fSBarry Smith       for ( j=0; j<diag; j++ ) {
25635aab85fSBarry Smith         u_values  = b->a + diag_offset[pj[j]] + shift;
25735aab85fSBarry Smith         nz        = ai[pj[j]+1] - diag_offset[pj[j]];
25835aab85fSBarry Smith         inner_sum = 0.0;
25935aab85fSBarry Smith         for ( k=0; k<nz; k++ ) {
26035aab85fSBarry Smith           inner_sum += u_values[k];
26135aab85fSBarry Smith         }
26235aab85fSBarry Smith         sum -= pv[j]*inner_sum;
26335aab85fSBarry Smith 
26435aab85fSBarry Smith       }
26535aab85fSBarry Smith       nz       = ai[i+1] - diag_offset[i] - 1;
26635aab85fSBarry Smith       u_values = b->a + diag_offset[i] + 1 + shift;
26735aab85fSBarry Smith       for ( k=0; k<nz; k++ ) {
26835aab85fSBarry Smith         sum -= u_values[k];
26935aab85fSBarry Smith       }
27035aab85fSBarry Smith       ssum = PetscAbsScalar(sum/pv[diag]);
27135aab85fSBarry Smith       if (ssum < 1000. && ssum > .001) pv[diag] = sum;
27235aab85fSBarry Smith     }
27335aab85fSBarry Smith     /* check pivot entry for current row */
2748c37ef55SBarry Smith   }
2750f11f9aeSSatish Balay 
27635aab85fSBarry Smith   /* invert diagonal entries for simplier triangular solves */
27735aab85fSBarry Smith   for ( i=0; i<n; i++ ) {
27835aab85fSBarry Smith     b->a[diag_offset[i]+shift] = 1.0/b->a[diag_offset[i]+shift];
27935aab85fSBarry Smith   }
28035aab85fSBarry Smith 
28135aab85fSBarry Smith   if (preserve_row_sums) PetscFree(rowsums);
2820452661fSBarry Smith   PetscFree(rtmp);
28378b31e54SBarry Smith   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
28478b31e54SBarry Smith   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
285416022c9SBarry Smith   C->factor = FACTOR_LU;
28641c01911SSatish Balay   ierr = Mat_AIJ_CheckInode(C); CHKERRQ(ierr);
287c456f294SBarry Smith   C->assembled = PETSC_TRUE;
288416022c9SBarry Smith   PLogFlops(b->n);
2893a40ed3dSBarry Smith   PetscFunctionReturn(0);
290289bc588SBarry Smith }
2910a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
2925615d1e5SSatish Balay #undef __FUNC__
2935615d1e5SSatish Balay #define __FUNC__ "MatLUFactor_SeqAIJ"
294416022c9SBarry Smith int MatLUFactor_SeqAIJ(Mat A,IS row,IS col,double f)
295da3a660dSBarry Smith {
296416022c9SBarry Smith   Mat_SeqAIJ     *mat = (Mat_SeqAIJ *) A->data;
2976abc6512SBarry Smith   int            ierr;
298416022c9SBarry Smith   Mat            C;
299f830108cSBarry Smith   PetscOps       *Abops;
3000a6ffc59SBarry Smith   MatOps         Aops;
301416022c9SBarry Smith 
3023a40ed3dSBarry Smith   PetscFunctionBegin;
303f2582111SSatish Balay   ierr = MatLUFactorSymbolic(A,row,col,f,&C); CHKERRQ(ierr);
304f2582111SSatish Balay   ierr = MatLUFactorNumeric(A,&C); CHKERRQ(ierr);
305da3a660dSBarry Smith 
306da3a660dSBarry Smith   /* free all the data structures from mat */
3070452661fSBarry Smith   PetscFree(mat->a);
3080452661fSBarry Smith   if (!mat->singlemalloc) {PetscFree(mat->i); PetscFree(mat->j);}
3090452661fSBarry Smith   if (mat->diag) PetscFree(mat->diag);
3100452661fSBarry Smith   if (mat->ilen) PetscFree(mat->ilen);
3110452661fSBarry Smith   if (mat->imax) PetscFree(mat->imax);
3120452661fSBarry Smith   if (mat->solve_work) PetscFree(mat->solve_work);
313a7a49059SBarry Smith   if (mat->inode.size) PetscFree(mat->inode.size);
3140f0aacb9SBarry Smith   if (mat->icol) {ierr = ISDestroy(mat->icol);CHKERRQ(ierr);}
3150452661fSBarry Smith   PetscFree(mat);
316da3a660dSBarry Smith 
31717642b18SBarry Smith   ierr = MapDestroy(A->rmap);CHKERRQ(ierr);
31817642b18SBarry Smith   ierr = MapDestroy(A->cmap);CHKERRQ(ierr);
31917642b18SBarry Smith 
320f830108cSBarry Smith   /*
321f830108cSBarry Smith        This is horrible, horrible code. We need to keep the
322f830108cSBarry Smith     A pointers for the bops and ops but copy everything
323f830108cSBarry Smith     else from C.
324f830108cSBarry Smith   */
325f830108cSBarry Smith   Abops = A->bops;
326f830108cSBarry Smith   Aops  = A->ops;
327f09e8eb9SSatish Balay   PetscMemcpy(A,C,sizeof(struct _p_Mat));
328f830108cSBarry Smith   A->bops  = Abops;
329f830108cSBarry Smith   A->ops   = Aops;
330bef8e0ddSBarry Smith   A->qlist = 0;
331f830108cSBarry Smith 
3320452661fSBarry Smith   PetscHeaderDestroy(C);
3333a40ed3dSBarry Smith   PetscFunctionReturn(0);
334da3a660dSBarry Smith }
3350a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
3365615d1e5SSatish Balay #undef __FUNC__
3375615d1e5SSatish Balay #define __FUNC__ "MatSolve_SeqAIJ"
338416022c9SBarry Smith int MatSolve_SeqAIJ(Mat A,Vec bb, Vec xx)
3398c37ef55SBarry Smith {
340416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
341416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row;
342416022c9SBarry Smith   int        *r,*c, ierr, i,  n = a->m, *vi, *ai = a->i, *aj = a->j;
3433b2fbd54SBarry Smith   int        nz,shift = a->indexshift,*rout,*cout;
344416022c9SBarry Smith   Scalar     *x,*b,*tmp, *tmps, *aa = a->a, sum, *v;
3458c37ef55SBarry Smith 
3463a40ed3dSBarry Smith   PetscFunctionBegin;
3473a40ed3dSBarry Smith   if (!n) PetscFunctionReturn(0);
34891bf9adeSBarry Smith 
349e1311b90SBarry Smith   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
350e1311b90SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
351416022c9SBarry Smith   tmp  = a->solve_work;
3528c37ef55SBarry Smith 
3533b2fbd54SBarry Smith   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
3543b2fbd54SBarry Smith   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1);
3558c37ef55SBarry Smith 
3568c37ef55SBarry Smith   /* forward solve the lower triangular */
3578c37ef55SBarry Smith   tmp[0] = b[*r++];
3580cf60383SBarry Smith   tmps   = tmp + shift;
3598c37ef55SBarry Smith   for ( i=1; i<n; i++ ) {
360dbb450caSBarry Smith     v   = aa + ai[i] + shift;
361dbb450caSBarry Smith     vi  = aj + ai[i] + shift;
362416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
3638c37ef55SBarry Smith     sum = b[*r++];
3640cf60383SBarry Smith     while (nz--) sum -= *v++ * tmps[*vi++];
3658c37ef55SBarry Smith     tmp[i] = sum;
3668c37ef55SBarry Smith   }
3678c37ef55SBarry Smith 
3688c37ef55SBarry Smith   /* backward solve the upper triangular */
3698c37ef55SBarry Smith   for ( i=n-1; i>=0; i-- ){
370416022c9SBarry Smith     v   = aa + a->diag[i] + (!shift);
371416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
372416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
3738c37ef55SBarry Smith     sum = tmp[i];
3740cf60383SBarry Smith     while (nz--) sum -= *v++ * tmps[*vi++];
375416022c9SBarry Smith     x[*c--] = tmp[i] = sum*aa[a->diag[i]+shift];
3768c37ef55SBarry Smith   }
3778c37ef55SBarry Smith 
3783b2fbd54SBarry Smith   ierr = ISRestoreIndices(isrow,&rout); CHKERRQ(ierr);
3793b2fbd54SBarry Smith   ierr = ISRestoreIndices(iscol,&cout); CHKERRQ(ierr);
380cb5b572fSBarry Smith   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
381cb5b572fSBarry Smith   ierr = VecRestoreArray(xx,&x); CHKERRQ(ierr);
382416022c9SBarry Smith   PLogFlops(2*a->nz - a->n);
3833a40ed3dSBarry Smith   PetscFunctionReturn(0);
3848c37ef55SBarry Smith }
385026e39d0SSatish Balay 
386930ae53cSBarry Smith /* ----------------------------------------------------------- */
387930ae53cSBarry Smith #undef __FUNC__
388930ae53cSBarry Smith #define __FUNC__ "MatSolve_SeqAIJ_NaturalOrdering"
389930ae53cSBarry Smith int MatSolve_SeqAIJ_NaturalOrdering(Mat A,Vec bb, Vec xx)
390930ae53cSBarry Smith {
391930ae53cSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
392d85afc42SSatish Balay   int        n = a->m, *ai = a->i, *aj = a->j, *adiag = a->diag,ierr;
393d85afc42SSatish Balay   Scalar     *x,*b, *aa = a->a, sum;
3942e40c62eSSatish Balay #if !defined(USE_FORTRAN_KERNEL_SOLVEAIJ)
395d85afc42SSatish Balay   int        adiag_i,i,*vi,nz,ai_i;
396d85afc42SSatish Balay   Scalar     *v;
397d85afc42SSatish Balay #endif
398930ae53cSBarry Smith 
3993a40ed3dSBarry Smith   PetscFunctionBegin;
4003a40ed3dSBarry Smith   if (!n) PetscFunctionReturn(0);
401930ae53cSBarry Smith   if (a->indexshift) {
4023a40ed3dSBarry Smith      ierr = MatSolve_SeqAIJ(A,bb,xx);CHKERRQ(ierr);
4033a40ed3dSBarry Smith      PetscFunctionReturn(0);
404930ae53cSBarry Smith   }
405930ae53cSBarry Smith 
406e1311b90SBarry Smith   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
407e1311b90SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
408930ae53cSBarry Smith 
4092e40c62eSSatish Balay #if defined(USE_FORTRAN_KERNEL_SOLVEAIJ)
4101c4feecaSSatish Balay   fortransolveaij_(&n,x,ai,aj,adiag,aa,b);
4111c4feecaSSatish Balay #else
412930ae53cSBarry Smith   /* forward solve the lower triangular */
413930ae53cSBarry Smith   x[0] = b[0];
414930ae53cSBarry Smith   for ( i=1; i<n; i++ ) {
415930ae53cSBarry Smith     ai_i = ai[i];
416930ae53cSBarry Smith     v    = aa + ai_i;
417930ae53cSBarry Smith     vi   = aj + ai_i;
418930ae53cSBarry Smith     nz   = adiag[i] - ai_i;
419930ae53cSBarry Smith     sum  = b[i];
420930ae53cSBarry Smith     while (nz--) sum -= *v++ * x[*vi++];
421930ae53cSBarry Smith     x[i] = sum;
422930ae53cSBarry Smith   }
423930ae53cSBarry Smith 
424930ae53cSBarry Smith   /* backward solve the upper triangular */
425930ae53cSBarry Smith   for ( i=n-1; i>=0; i-- ){
426930ae53cSBarry Smith     adiag_i = adiag[i];
427d708749eSBarry Smith     v       = aa + adiag_i + 1;
428d708749eSBarry Smith     vi      = aj + adiag_i + 1;
429930ae53cSBarry Smith     nz      = ai[i+1] - adiag_i - 1;
430930ae53cSBarry Smith     sum     = x[i];
431930ae53cSBarry Smith     while (nz--) sum -= *v++ * x[*vi++];
432930ae53cSBarry Smith     x[i]    = sum*aa[adiag_i];
433930ae53cSBarry Smith   }
4341c4feecaSSatish Balay #endif
435930ae53cSBarry Smith   PLogFlops(2*a->nz - a->n);
436cb5b572fSBarry Smith   ierr = VecRestoreArray(bb,&b); CHKERRQ(ierr);
437cb5b572fSBarry Smith   ierr = VecRestoreArray(xx,&x); CHKERRQ(ierr);
4383a40ed3dSBarry Smith   PetscFunctionReturn(0);
439930ae53cSBarry Smith }
440930ae53cSBarry Smith 
4415615d1e5SSatish Balay #undef __FUNC__
4425615d1e5SSatish Balay #define __FUNC__ "MatSolveAdd_SeqAIJ"
443416022c9SBarry Smith int MatSolveAdd_SeqAIJ(Mat A,Vec bb, Vec yy, Vec xx)
444da3a660dSBarry Smith {
445416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
446416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row;
447416022c9SBarry Smith   int        *r,*c, ierr, i,  n = a->m, *vi, *ai = a->i, *aj = a->j;
4483b2fbd54SBarry Smith   int        nz, shift = a->indexshift,*rout,*cout;
449416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, sum, *v;
450da3a660dSBarry Smith 
4513a40ed3dSBarry Smith   PetscFunctionBegin;
45278b31e54SBarry Smith   if (yy != xx) {ierr = VecCopy(yy,xx); CHKERRQ(ierr);}
453da3a660dSBarry Smith 
454e1311b90SBarry Smith   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
455e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
456416022c9SBarry Smith   tmp  = a->solve_work;
457da3a660dSBarry Smith 
4583b2fbd54SBarry Smith   ierr = ISGetIndices(isrow,&rout); CHKERRQ(ierr); r = rout;
4593b2fbd54SBarry Smith   ierr = ISGetIndices(iscol,&cout); CHKERRQ(ierr); c = cout + (n-1);
460da3a660dSBarry Smith 
461da3a660dSBarry Smith   /* forward solve the lower triangular */
462da3a660dSBarry Smith   tmp[0] = b[*r++];
463da3a660dSBarry Smith   for ( i=1; i<n; i++ ) {
464dbb450caSBarry Smith     v   = aa + ai[i] + shift;
465dbb450caSBarry Smith     vi  = aj + ai[i] + shift;
466416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
467da3a660dSBarry Smith     sum = b[*r++];
468dbb450caSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ + shift];
469da3a660dSBarry Smith     tmp[i] = sum;
470da3a660dSBarry Smith   }
471da3a660dSBarry Smith 
472da3a660dSBarry Smith   /* backward solve the upper triangular */
473da3a660dSBarry Smith   for ( i=n-1; i>=0; i-- ){
474416022c9SBarry Smith     v   = aa + a->diag[i] + (!shift);
475416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
476416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
477da3a660dSBarry Smith     sum = tmp[i];
478dbb450caSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ + shift];
479416022c9SBarry Smith     tmp[i] = sum*aa[a->diag[i]+shift];
480da3a660dSBarry Smith     x[*c--] += tmp[i];
481da3a660dSBarry Smith   }
482da3a660dSBarry Smith 
4833b2fbd54SBarry Smith   ierr = ISRestoreIndices(isrow,&rout); CHKERRQ(ierr);
4843b2fbd54SBarry Smith   ierr = ISRestoreIndices(iscol,&cout); CHKERRQ(ierr);
485cb5b572fSBarry Smith   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
486cb5b572fSBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
487416022c9SBarry Smith   PLogFlops(2*a->nz);
488898183ecSLois Curfman McInnes 
4893a40ed3dSBarry Smith   PetscFunctionReturn(0);
490da3a660dSBarry Smith }
491da3a660dSBarry Smith /* -------------------------------------------------------------------*/
4925615d1e5SSatish Balay #undef __FUNC__
4935615d1e5SSatish Balay #define __FUNC__ "MatSolveTrans_SeqAIJ"
494416022c9SBarry Smith int MatSolveTrans_SeqAIJ(Mat A,Vec bb, Vec xx)
495da3a660dSBarry Smith {
496416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
4972235e667SBarry Smith   IS         iscol = a->col, isrow = a->row;
498416022c9SBarry Smith   int        *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j;
4993b2fbd54SBarry Smith   int        nz,shift = a->indexshift,*rout,*cout;
500416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, *v;
501da3a660dSBarry Smith 
5023a40ed3dSBarry Smith   PetscFunctionBegin;
503e1311b90SBarry Smith   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
504e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
505416022c9SBarry Smith   tmp  = a->solve_work;
506da3a660dSBarry Smith 
5072235e667SBarry Smith   ierr = ISGetIndices(isrow,&rout); CHKERRQ(ierr); r = rout;
5082235e667SBarry Smith   ierr = ISGetIndices(iscol,&cout); CHKERRQ(ierr); c = cout;
509da3a660dSBarry Smith 
510da3a660dSBarry Smith   /* copy the b into temp work space according to permutation */
5112235e667SBarry Smith   for ( i=0; i<n; i++ ) tmp[i] = b[c[i]];
512da3a660dSBarry Smith 
513da3a660dSBarry Smith   /* forward solve the U^T */
514da3a660dSBarry Smith   for ( i=0; i<n; i++ ) {
515416022c9SBarry Smith     v   = aa + a->diag[i] + shift;
516416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
517416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
518da3a660dSBarry Smith     tmp[i] *= *v++;
519da3a660dSBarry Smith     while (nz--) {
520dbb450caSBarry Smith       tmp[*vi++ + shift] -= (*v++)*tmp[i];
521da3a660dSBarry Smith     }
522da3a660dSBarry Smith   }
523da3a660dSBarry Smith 
524da3a660dSBarry Smith   /* backward solve the L^T */
525da3a660dSBarry Smith   for ( i=n-1; i>=0; i-- ){
526416022c9SBarry Smith     v   = aa + a->diag[i] - 1 + shift;
527416022c9SBarry Smith     vi  = aj + a->diag[i] - 1 + shift;
528416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
529da3a660dSBarry Smith     while (nz--) {
530dbb450caSBarry Smith       tmp[*vi-- + shift] -= (*v--)*tmp[i];
531da3a660dSBarry Smith     }
532da3a660dSBarry Smith   }
533da3a660dSBarry Smith 
534da3a660dSBarry Smith   /* copy tmp into x according to permutation */
535da3a660dSBarry Smith   for ( i=0; i<n; i++ ) x[r[i]] = tmp[i];
536da3a660dSBarry Smith 
5372235e667SBarry Smith   ierr = ISRestoreIndices(isrow,&rout); CHKERRQ(ierr);
5382235e667SBarry Smith   ierr = ISRestoreIndices(iscol,&cout); CHKERRQ(ierr);
539cb5b572fSBarry Smith   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
540cb5b572fSBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
541da3a660dSBarry Smith 
542416022c9SBarry Smith   PLogFlops(2*a->nz-a->n);
5433a40ed3dSBarry Smith   PetscFunctionReturn(0);
544da3a660dSBarry Smith }
545da3a660dSBarry Smith 
5465615d1e5SSatish Balay #undef __FUNC__
5475615d1e5SSatish Balay #define __FUNC__ "MatSolveTransAdd_SeqAIJ"
548416022c9SBarry Smith int MatSolveTransAdd_SeqAIJ(Mat A,Vec bb, Vec zz,Vec xx)
549da3a660dSBarry Smith {
550416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
5512235e667SBarry Smith   IS         iscol = a->col, isrow = a->row;
552416022c9SBarry Smith   int        *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j;
5533b2fbd54SBarry Smith   int        nz,shift = a->indexshift, *rout, *cout;
554416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, *v;
5556abc6512SBarry Smith 
5563a40ed3dSBarry Smith   PetscFunctionBegin;
5576abc6512SBarry Smith   if (zz != xx) VecCopy(zz,xx);
5586abc6512SBarry Smith 
559e1311b90SBarry Smith   ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
560e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
561416022c9SBarry Smith   tmp = a->solve_work;
5626abc6512SBarry Smith 
5632235e667SBarry Smith   ierr = ISGetIndices(isrow,&rout); CHKERRQ(ierr); r = rout;
5642235e667SBarry Smith   ierr = ISGetIndices(iscol,&cout); CHKERRQ(ierr); c = cout;
5656abc6512SBarry Smith 
5666abc6512SBarry Smith   /* copy the b into temp work space according to permutation */
5672235e667SBarry Smith   for ( i=0; i<n; i++ ) tmp[i] = b[c[i]];
5686abc6512SBarry Smith 
5696abc6512SBarry Smith   /* forward solve the U^T */
5706abc6512SBarry Smith   for ( i=0; i<n; i++ ) {
571416022c9SBarry Smith     v   = aa + a->diag[i] + shift;
572416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
573416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
5746abc6512SBarry Smith     tmp[i] *= *v++;
5756abc6512SBarry Smith     while (nz--) {
576dbb450caSBarry Smith       tmp[*vi++ + shift] -= (*v++)*tmp[i];
5776abc6512SBarry Smith     }
5786abc6512SBarry Smith   }
5796abc6512SBarry Smith 
5806abc6512SBarry Smith   /* backward solve the L^T */
5816abc6512SBarry Smith   for ( i=n-1; i>=0; i-- ){
582416022c9SBarry Smith     v   = aa + a->diag[i] - 1 + shift;
583416022c9SBarry Smith     vi  = aj + a->diag[i] - 1 + shift;
584416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
5856abc6512SBarry Smith     while (nz--) {
586dbb450caSBarry Smith       tmp[*vi-- + shift] -= (*v--)*tmp[i];
5876abc6512SBarry Smith     }
5886abc6512SBarry Smith   }
5896abc6512SBarry Smith 
5906abc6512SBarry Smith   /* copy tmp into x according to permutation */
5916abc6512SBarry Smith   for ( i=0; i<n; i++ ) x[r[i]] += tmp[i];
5926abc6512SBarry Smith 
5932235e667SBarry Smith   ierr = ISRestoreIndices(isrow,&rout); CHKERRQ(ierr);
5942235e667SBarry Smith   ierr = ISRestoreIndices(iscol,&cout); CHKERRQ(ierr);
595cb5b572fSBarry Smith   ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);
596cb5b572fSBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
5976abc6512SBarry Smith 
598416022c9SBarry Smith   PLogFlops(2*a->nz);
5993a40ed3dSBarry Smith   PetscFunctionReturn(0);
600da3a660dSBarry Smith }
6019e25ed09SBarry Smith /* ----------------------------------------------------------------*/
60208480c60SBarry Smith 
6035615d1e5SSatish Balay #undef __FUNC__
6045615d1e5SSatish Balay #define __FUNC__ "MatILUFactorSymbolic_SeqAIJ"
60508480c60SBarry Smith int MatILUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,int levels,Mat *fact)
6069e25ed09SBarry Smith {
607416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b;
6089e25ed09SBarry Smith   IS         isicol;
609416022c9SBarry Smith   int        *r,*ic, ierr, prow, n = a->m, *ai = a->i, *aj = a->j;
61056beaf04SBarry Smith   int        *ainew,*ajnew, jmax,*fill, *xi, nz, *im,*ajfill,*flev;
61156beaf04SBarry Smith   int        *dloc, idx, row,m,fm, nzf, nzi,len,  realloc = 0;
612416022c9SBarry Smith   int        incrlev,nnz,i,shift = a->indexshift;
61377c4ece6SBarry Smith   PetscTruth col_identity, row_identity;
6149e25ed09SBarry Smith 
6153a40ed3dSBarry Smith   PetscFunctionBegin;
61682bf6240SBarry Smith   ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr);
61782bf6240SBarry Smith 
61808480c60SBarry Smith   /* special case that simply copies fill pattern */
61977c4ece6SBarry Smith   ISIdentity(isrow,&row_identity); ISIdentity(iscol,&col_identity);
62077c4ece6SBarry Smith   if (levels == 0 && row_identity && col_identity) {
6212e8a6d31SBarry Smith     ierr = MatDuplicate_SeqAIJ(A,MAT_DO_NOT_COPY_VALUES,fact); CHKERRQ(ierr);
62208480c60SBarry Smith     (*fact)->factor = FACTOR_LU;
62308480c60SBarry Smith     b               = (Mat_SeqAIJ *) (*fact)->data;
62408480c60SBarry Smith     if (!b->diag) {
62508480c60SBarry Smith       ierr = MatMarkDiag_SeqAIJ(*fact); CHKERRQ(ierr);
62608480c60SBarry Smith     }
62708480c60SBarry Smith     b->row             = isrow;
62808480c60SBarry Smith     b->col             = iscol;
62982bf6240SBarry Smith     b->icol            = isicol;
6300452661fSBarry Smith     b->solve_work      = (Scalar *) PetscMalloc((b->m+1)*sizeof(Scalar));CHKPTRQ(b->solve_work);
631f830108cSBarry Smith     (*fact)->ops->solve = MatSolve_SeqAIJ_NaturalOrdering;
6323a40ed3dSBarry Smith     PetscFunctionReturn(0);
63308480c60SBarry Smith   }
63408480c60SBarry Smith 
635898183ecSLois Curfman McInnes   ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr);
636898183ecSLois Curfman McInnes   ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr);
6379e25ed09SBarry Smith 
6389e25ed09SBarry Smith   /* get new row pointers */
6390452661fSBarry Smith   ainew = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew);
640dbb450caSBarry Smith   ainew[0] = -shift;
6419e25ed09SBarry Smith   /* don't know how many column pointers are needed so estimate */
642dbb450caSBarry Smith   jmax = (int) (f*(ai[n]+!shift));
6430452661fSBarry Smith   ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew);
6449e25ed09SBarry Smith   /* ajfill is level of fill for each fill entry */
6450452661fSBarry Smith   ajfill = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajfill);
6469e25ed09SBarry Smith   /* fill is a linked list of nonzeros in active row */
6470452661fSBarry Smith   fill = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(fill);
64856beaf04SBarry Smith   /* im is level for each filled value */
6490452661fSBarry Smith   im = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(im);
65056beaf04SBarry Smith   /* dloc is location of diagonal in factor */
6510452661fSBarry Smith   dloc = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(dloc);
65256beaf04SBarry Smith   dloc[0]  = 0;
65356beaf04SBarry Smith   for ( prow=0; prow<n; prow++ ) {
6549e25ed09SBarry Smith     /* first copy previous fill into linked list */
65556beaf04SBarry Smith     nzf     = nz  = ai[r[prow]+1] - ai[r[prow]];
656385f7a74SSatish Balay     if (!nz) SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,1,"Empty row in matrix");
657dbb450caSBarry Smith     xi      = aj + ai[r[prow]] + shift;
6589e25ed09SBarry Smith     fill[n] = n;
6599e25ed09SBarry Smith     while (nz--) {
6609e25ed09SBarry Smith       fm  = n;
661dbb450caSBarry Smith       idx = ic[*xi++ + shift];
6629e25ed09SBarry Smith       do {
6639e25ed09SBarry Smith         m  = fm;
6649e25ed09SBarry Smith         fm = fill[m];
6659e25ed09SBarry Smith       } while (fm < idx);
6669e25ed09SBarry Smith       fill[m]   = idx;
6679e25ed09SBarry Smith       fill[idx] = fm;
66856beaf04SBarry Smith       im[idx]   = 0;
6699e25ed09SBarry Smith     }
67056beaf04SBarry Smith     nzi = 0;
6719e25ed09SBarry Smith     row = fill[n];
67256beaf04SBarry Smith     while ( row < prow ) {
67356beaf04SBarry Smith       incrlev = im[row] + 1;
67456beaf04SBarry Smith       nz      = dloc[row];
675dbb450caSBarry Smith       xi      = ajnew  + ainew[row] + shift + nz;
676dbb450caSBarry Smith       flev    = ajfill + ainew[row] + shift + nz + 1;
677416022c9SBarry Smith       nnz     = ainew[row+1] - ainew[row] - nz - 1;
678dbb450caSBarry Smith       if (*xi++ + shift != row) {
679965fcd8bSBarry Smith         SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,0,"Zero pivot: try running with -pc_ilu_nonzeros_along_diagonal");
68056beaf04SBarry Smith       }
68156beaf04SBarry Smith       fm      = row;
68256beaf04SBarry Smith       while (nnz-- > 0) {
683dbb450caSBarry Smith         idx = *xi++ + shift;
68456beaf04SBarry Smith         if (*flev + incrlev > levels) {
68556beaf04SBarry Smith           flev++;
68656beaf04SBarry Smith           continue;
68756beaf04SBarry Smith         }
68856beaf04SBarry Smith         do {
6899e25ed09SBarry Smith           m  = fm;
6909e25ed09SBarry Smith           fm = fill[m];
69156beaf04SBarry Smith         } while (fm < idx);
6929e25ed09SBarry Smith         if (fm != idx) {
69356beaf04SBarry Smith           im[idx]   = *flev + incrlev;
6949e25ed09SBarry Smith           fill[m]   = idx;
6959e25ed09SBarry Smith           fill[idx] = fm;
6969e25ed09SBarry Smith           fm        = idx;
69756beaf04SBarry Smith           nzf++;
698ecf371e4SBarry Smith         } else {
69956beaf04SBarry Smith           if (im[idx] > *flev + incrlev) im[idx] = *flev+incrlev;
7009e25ed09SBarry Smith         }
70156beaf04SBarry Smith         flev++;
7029e25ed09SBarry Smith       }
7039e25ed09SBarry Smith       row = fill[row];
70456beaf04SBarry Smith       nzi++;
7059e25ed09SBarry Smith     }
7069e25ed09SBarry Smith     /* copy new filled row into permanent storage */
70756beaf04SBarry Smith     ainew[prow+1] = ainew[prow] + nzf;
708d7e8b826SBarry Smith     if (ainew[prow+1] > jmax-shift) {
709ecf371e4SBarry Smith 
710ecf371e4SBarry Smith       /* estimate how much additional space we will need */
711ecf371e4SBarry Smith       /* use the strategy suggested by David Hysom <hysom@perch-t.icase.edu> */
712ecf371e4SBarry Smith       /* just double the memory each time */
713ecf371e4SBarry Smith       /*  maxadd = (int) ((f*(ai[n]+!shift)*(n-prow+5))/n); */
714ecf371e4SBarry Smith       int maxadd = jmax;
71556beaf04SBarry Smith       if (maxadd < nzf) maxadd = (n-prow)*(nzf+1);
716bbb6d6a8SBarry Smith       jmax += maxadd;
717ecf371e4SBarry Smith 
718ecf371e4SBarry Smith       /* allocate a longer ajnew and ajfill */
7190452661fSBarry Smith       xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi);
720416022c9SBarry Smith       PetscMemcpy(xi,ajnew,(ainew[prow]+shift)*sizeof(int));
7210452661fSBarry Smith       PetscFree(ajnew);
72256beaf04SBarry Smith       ajnew = xi;
7230452661fSBarry Smith       xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi);
724416022c9SBarry Smith       PetscMemcpy(xi,ajfill,(ainew[prow]+shift)*sizeof(int));
7250452661fSBarry Smith       PetscFree(ajfill);
72656beaf04SBarry Smith       ajfill = xi;
727ecf371e4SBarry Smith       realloc++; /* count how many times we realloc */
7289e25ed09SBarry Smith     }
729dbb450caSBarry Smith     xi          = ajnew + ainew[prow] + shift;
730dbb450caSBarry Smith     flev        = ajfill + ainew[prow] + shift;
73156beaf04SBarry Smith     dloc[prow]  = nzi;
7329e25ed09SBarry Smith     fm          = fill[n];
73356beaf04SBarry Smith     while (nzf--) {
734dbb450caSBarry Smith       *xi++   = fm - shift;
73556beaf04SBarry Smith       *flev++ = im[fm];
7369e25ed09SBarry Smith       fm      = fill[fm];
7379e25ed09SBarry Smith     }
7389e25ed09SBarry Smith   }
7390452661fSBarry Smith   PetscFree(ajfill);
740898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
741898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
7420452661fSBarry Smith   PetscFree(fill); PetscFree(im);
7439e25ed09SBarry Smith 
744f64065bbSBarry Smith   {
745464e8d44SSatish Balay     double af = ((double)ainew[n])/((double)ai[n]);
746981c4779SBarry Smith     PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n",
747f64065bbSBarry Smith              realloc,f,af);
748981c4779SBarry Smith     PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:Run with -pc_ilu_fill %g or use \n",af);
749981c4779SBarry Smith     PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:PCILUSetFill(pc,%g);\n",af);
750981c4779SBarry Smith     PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:for best performance.\n");
751f64065bbSBarry Smith   }
752bbb6d6a8SBarry Smith 
7539e25ed09SBarry Smith   /* put together the new matrix */
754b4fd4287SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,fact); CHKERRQ(ierr);
755fa6007c9SSatish Balay   PLogObjectParent(*fact,isicol);
756416022c9SBarry Smith   b = (Mat_SeqAIJ *) (*fact)->data;
7570452661fSBarry Smith   PetscFree(b->imax);
758416022c9SBarry Smith   b->singlemalloc = 0;
759dbb450caSBarry Smith   len = (ainew[n] + shift)*sizeof(Scalar);
7609e25ed09SBarry Smith   /* the next line frees the default space generated by the Create() */
7610452661fSBarry Smith   PetscFree(b->a); PetscFree(b->ilen);
7626b96ef82SSatish Balay   b->a          = (Scalar *) PetscMalloc( len+1 ); CHKPTRQ(b->a);
763416022c9SBarry Smith   b->j          = ajnew;
764416022c9SBarry Smith   b->i          = ainew;
76556beaf04SBarry Smith   for ( i=0; i<n; i++ ) dloc[i] += ainew[i];
766416022c9SBarry Smith   b->diag       = dloc;
767416022c9SBarry Smith   b->ilen       = 0;
768416022c9SBarry Smith   b->imax       = 0;
769416022c9SBarry Smith   b->row        = isrow;
770416022c9SBarry Smith   b->col        = iscol;
77182bf6240SBarry Smith   b->icol       = isicol;
77282bf6240SBarry Smith   b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar)); CHKPTRQ(b->solve_work);
773416022c9SBarry Smith   /* In b structure:  Free imax, ilen, old a, old j.
77456beaf04SBarry Smith      Allocate dloc, solve_work, new a, new j */
775dbb450caSBarry Smith   PLogObjectMemory(*fact,(ainew[n]+shift-n) * (sizeof(int)+sizeof(Scalar)));
776416022c9SBarry Smith   b->maxnz          = b->nz = ainew[n] + shift;
7779e25ed09SBarry Smith   (*fact)->factor   = FACTOR_LU;
778ae068f58SLois Curfman McInnes 
779ae068f58SLois Curfman McInnes   (*fact)->info.factor_mallocs    = realloc;
780ae068f58SLois Curfman McInnes   (*fact)->info.fill_ratio_given  = f;
781ae068f58SLois Curfman McInnes   (*fact)->info.fill_ratio_needed = ((double)ainew[n])/((double)ai[prow]);
782e93ccc4dSBarry Smith   (*fact)->factor                 =  FACTOR_LU;;
783ae068f58SLois Curfman McInnes 
7843a40ed3dSBarry Smith   PetscFunctionReturn(0);
7859e25ed09SBarry Smith }
786d5d45c9bSBarry Smith 
787d5d45c9bSBarry Smith 
788d5d45c9bSBarry Smith 
789d5d45c9bSBarry Smith 
790