xref: /petsc/src/mat/impls/aij/seq/aijfact.c (revision d5d45c9bad5d66633d6ef030bee5689b038db93c)
1cb512458SBarry Smith #ifndef lint
2*d5d45c9bSBarry Smith static char vcid[] = "$Id: aijfact.c,v 1.44 1995/11/01 23:17:58 bsmith Exp bsmith $";
3cb512458SBarry Smith #endif
4289bc588SBarry Smith 
5289bc588SBarry Smith #include "aij.h"
68c37ef55SBarry Smith #include "inline/spops.h"
7289bc588SBarry Smith /*
8289bc588SBarry Smith     Factorization code for AIJ format.
9289bc588SBarry Smith */
10289bc588SBarry Smith 
11416022c9SBarry Smith int MatLUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,Mat *B)
12289bc588SBarry Smith {
13416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b;
14289bc588SBarry Smith   IS         isicol;
15416022c9SBarry Smith   int        *r,*ic, ierr, i, n = a->m, *ai = a->i, *aj = a->j;
16416022c9SBarry Smith   int        *ainew,*ajnew, jmax,*fill, *ajtmp, nz,shift = a->indexshift;
172fb3ed76SBarry Smith   int        *idnew, idx, row,m,fm, nnz, nzi,len, realloc = 0,nzbd,*im;
18289bc588SBarry Smith 
19416022c9SBarry Smith   if (n != a->n) SETERRQ(1,"MatLUFactorSymbolic_SeqAIJ:Must be square");
20416022c9SBarry Smith   if (!isrow) SETERRQ(1,"MatLUFactorSymbolic_SeqAIJ:Must have row permutation");
21416022c9SBarry Smith   if (!iscol) SETERRQ(1,"MatLUFactorSymbolic_SeqAIJ:Must have column permutation");
22289bc588SBarry Smith 
2378b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr);
24289bc588SBarry Smith   ISGetIndices(isrow,&r); ISGetIndices(isicol,&ic);
25289bc588SBarry Smith 
26289bc588SBarry Smith   /* get new row pointers */
270452661fSBarry Smith   ainew = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew);
28dbb450caSBarry Smith   ainew[0] = -shift;
29289bc588SBarry Smith   /* don't know how many column pointers are needed so estimate */
30dbb450caSBarry Smith   jmax = (int) (f*ai[n]+(!shift));
310452661fSBarry Smith   ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew);
32289bc588SBarry Smith   /* fill is a linked list of nonzeros in active row */
330452661fSBarry Smith   fill = (int *) PetscMalloc( (2*n+1)*sizeof(int)); CHKPTRQ(fill);
342fb3ed76SBarry Smith   im = fill + n + 1;
35289bc588SBarry Smith   /* idnew is location of diagonal in factor */
360452661fSBarry Smith   idnew = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(idnew);
37dbb450caSBarry Smith   idnew[0] = -shift;
38289bc588SBarry Smith 
39289bc588SBarry Smith   for ( i=0; i<n; i++ ) {
40289bc588SBarry Smith     /* first copy previous fill into linked list */
41289bc588SBarry Smith     nnz     = nz    = ai[r[i]+1] - ai[r[i]];
42dbb450caSBarry Smith     ajtmp   = aj + ai[r[i]] + shift;
43289bc588SBarry Smith     fill[n] = n;
44289bc588SBarry Smith     while (nz--) {
45289bc588SBarry Smith       fm  = n;
46dbb450caSBarry Smith       idx = ic[*ajtmp++ + shift];
47289bc588SBarry Smith       do {
48289bc588SBarry Smith         m  = fm;
49289bc588SBarry Smith         fm = fill[m];
50289bc588SBarry Smith       } while (fm < idx);
51289bc588SBarry Smith       fill[m]   = idx;
52289bc588SBarry Smith       fill[idx] = fm;
53289bc588SBarry Smith     }
54289bc588SBarry Smith     row = fill[n];
55289bc588SBarry Smith     while ( row < i ) {
56dbb450caSBarry Smith       ajtmp = ajnew + idnew[row] + (!shift);
572fb3ed76SBarry Smith       nzbd  = 1 + idnew[row] - ainew[row];
582fb3ed76SBarry Smith       nz    = im[row] - nzbd;
59289bc588SBarry Smith       fm    = row;
602fb3ed76SBarry Smith       while (nz-- > 0) {
61dbb450caSBarry Smith         idx = *ajtmp++ + shift;
622fb3ed76SBarry Smith         nzbd++;
632fb3ed76SBarry Smith         if (idx == i) im[row] = nzbd;
64289bc588SBarry Smith         do {
65289bc588SBarry Smith           m  = fm;
66289bc588SBarry Smith           fm = fill[m];
67289bc588SBarry Smith         } while (fm < idx);
68289bc588SBarry Smith         if (fm != idx) {
69289bc588SBarry Smith           fill[m]   = idx;
70289bc588SBarry Smith           fill[idx] = fm;
71289bc588SBarry Smith           fm        = idx;
72289bc588SBarry Smith           nnz++;
73289bc588SBarry Smith         }
74289bc588SBarry Smith       }
75289bc588SBarry Smith       row = fill[row];
76289bc588SBarry Smith     }
77289bc588SBarry Smith     /* copy new filled row into permanent storage */
78289bc588SBarry Smith     ainew[i+1] = ainew[i] + nnz;
79289bc588SBarry Smith     if (ainew[i+1] > jmax+1) {
80289bc588SBarry Smith       /* allocate a longer ajnew */
812fb3ed76SBarry Smith       int maxadd;
82dbb450caSBarry Smith       maxadd = (int) ((f*(ai[n]+(!shift))*(n-i+5))/n);
83bbb6d6a8SBarry Smith       if (maxadd < nnz) maxadd = (n-i)*(nnz+1);
842fb3ed76SBarry Smith       jmax += maxadd;
850452661fSBarry Smith       ajtmp = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(ajtmp);
86416022c9SBarry Smith       PetscMemcpy(ajtmp,ajnew,(ainew[i]+shift)*sizeof(int));
870452661fSBarry Smith       PetscFree(ajnew);
88289bc588SBarry Smith       ajnew = ajtmp;
892fb3ed76SBarry Smith       realloc++; /* count how many times we realloc */
90289bc588SBarry Smith     }
91dbb450caSBarry Smith     ajtmp = ajnew + ainew[i] + shift;
92289bc588SBarry Smith     fm    = fill[n];
93289bc588SBarry Smith     nzi   = 0;
942fb3ed76SBarry Smith     im[i] = nnz;
95289bc588SBarry Smith     while (nnz--) {
96289bc588SBarry Smith       if (fm < i) nzi++;
97dbb450caSBarry Smith       *ajtmp++ = fm - shift;
98289bc588SBarry Smith       fm       = fill[fm];
99289bc588SBarry Smith     }
100289bc588SBarry Smith     idnew[i] = ainew[i] + nzi;
101289bc588SBarry Smith   }
102289bc588SBarry Smith 
103416022c9SBarry Smith   PLogInfo((PetscObject)A,
104ec8511deSBarry Smith     "Info:MatLUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n",
105bbb6d6a8SBarry Smith                              realloc,f,((double)ainew[n])/((double)ai[i]));
1062fb3ed76SBarry Smith 
107898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
108898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
1091987afe7SBarry Smith 
1100452661fSBarry Smith   PetscFree(fill);
111289bc588SBarry Smith 
112289bc588SBarry Smith   /* put together the new matrix */
113416022c9SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,n, n, 0, 0, B); CHKERRQ(ierr);
1141987afe7SBarry Smith   PLogObjectParent(*B,isicol);
1151987afe7SBarry Smith   ierr = ISDestroy(isicol); CHKERRQ(ierr);
116416022c9SBarry Smith   b = (Mat_SeqAIJ *) (*B)->data;
1170452661fSBarry Smith   PetscFree(b->imax);
118416022c9SBarry Smith   b->singlemalloc = 0;
119dbb450caSBarry Smith   len             = (ainew[n] + shift)*sizeof(Scalar);
120e8d4e0b9SBarry Smith   /* the next line frees the default space generated by the Create() */
1210452661fSBarry Smith   PetscFree(b->a); PetscFree(b->ilen);
1220452661fSBarry Smith   b->a          = (Scalar *) PetscMalloc( len ); CHKPTRQ(b->a);
123416022c9SBarry Smith   b->j          = ajnew;
124416022c9SBarry Smith   b->i          = ainew;
125416022c9SBarry Smith   b->diag       = idnew;
126416022c9SBarry Smith   b->ilen       = 0;
127416022c9SBarry Smith   b->imax       = 0;
128416022c9SBarry Smith   b->row        = isrow;
129416022c9SBarry Smith   b->col        = iscol;
1300452661fSBarry Smith   b->solve_work = (Scalar *) PetscMalloc( n*sizeof(Scalar));
131416022c9SBarry Smith   CHKPTRQ(b->solve_work);
132416022c9SBarry Smith   /* In b structure:  Free imax, ilen, old a, old j.
1337fd98bd6SLois Curfman McInnes      Allocate idnew, solve_work, new a, new j */
134416022c9SBarry Smith   PLogObjectMemory(*B,(ainew[n]+shift-n)*(sizeof(int)+sizeof(Scalar)));
135416022c9SBarry Smith   b->maxnz = b->nz = ainew[n] + shift;
1367fd98bd6SLois Curfman McInnes 
137289bc588SBarry Smith   return 0;
138289bc588SBarry Smith }
1390a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
140416022c9SBarry Smith int MatLUFactorNumeric_SeqAIJ(Mat A,Mat *B)
141289bc588SBarry Smith {
142416022c9SBarry Smith   Mat        C = *B;
143416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b = (Mat_SeqAIJ *)C->data;
144416022c9SBarry Smith   IS         iscol = b->col, isrow = b->row, isicol;
145416022c9SBarry Smith   int        *r,*ic, ierr, i, j, n = a->m, *ai = b->i, *aj = b->j;
1461987afe7SBarry Smith   int        *ajtmpold, *ajtmp, nz, row, *ics, shift = a->indexshift;
147*d5d45c9bSBarry Smith   int        *diag_offset = b->diag;
148390f2bbaSBarry Smith   Scalar     *rtmp,*v, *pc, multiplier;
1491987afe7SBarry Smith   /* These declarations are for optimizations.  They reduce the number of
1501987afe7SBarry Smith      memory references that are made by locally storing information; the
1511987afe7SBarry Smith      word "register" used here with pointers can be viewed as "private" or
1521987afe7SBarry Smith      "known only to me"
1531987afe7SBarry Smith    */
154390f2bbaSBarry Smith   register Scalar *pv, *rtmps;
1551987afe7SBarry Smith   register int    *pj;
156289bc588SBarry Smith 
15778b31e54SBarry Smith   ierr  = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr);
158416022c9SBarry Smith   PLogObjectParent(*B,isicol);
15978b31e54SBarry Smith   ierr  = ISGetIndices(isrow,&r); CHKERRQ(ierr);
16078b31e54SBarry Smith   ierr  = ISGetIndices(isicol,&ic); CHKERRQ(ierr);
1610452661fSBarry Smith   rtmp  = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar) ); CHKPTRQ(rtmp);
1620cf60383SBarry Smith   rtmps = rtmp + shift; ics = ic + shift;
163289bc588SBarry Smith 
164289bc588SBarry Smith   for ( i=0; i<n; i++ ) {
165289bc588SBarry Smith     nz    = ai[i+1] - ai[i];
166dbb450caSBarry Smith     ajtmp = aj + ai[i] + shift;
1670cf60383SBarry Smith     for  ( j=0; j<nz; j++ ) rtmps[ajtmp[j]] = 0.0;
168289bc588SBarry Smith 
169289bc588SBarry Smith     /* load in initial (unfactored row) */
170416022c9SBarry Smith     nz       = a->i[r[i]+1] - a->i[r[i]];
171416022c9SBarry Smith     ajtmpold = a->j + a->i[r[i]] + shift;
172416022c9SBarry Smith     v        = a->a + a->i[r[i]] + shift;
1730cf60383SBarry Smith     for ( j=0; j<nz; j++ ) rtmp[ics[ajtmpold[j]]] =  v[j];
174289bc588SBarry Smith 
175dbb450caSBarry Smith     row = *ajtmp++ + shift;
176289bc588SBarry Smith     while (row < i) {
1778c37ef55SBarry Smith       pc = rtmp + row;
1788c37ef55SBarry Smith       if (*pc != 0.0) {
1791987afe7SBarry Smith         pv         = b->a + diag_offset[row] + shift;
1801987afe7SBarry Smith         pj         = b->j + diag_offset[row] + (!shift);
1818c37ef55SBarry Smith         multiplier = *pc * *pv++;
1828c37ef55SBarry Smith         *pc        = multiplier;
1831987afe7SBarry Smith         nz         = ai[row+1] - diag_offset[row] - 1;
184eef2e97bSBarry Smith         PLogFlops(2*nz);
1851987afe7SBarry Smith 	/* The for-loop form can aid the compiler in overlapping
1861987afe7SBarry Smith 	   loads and stores */
1871987afe7SBarry Smith         /*while (nz-->0) rtmps[*pj++] -= multiplier* *pv++;  */
1881987afe7SBarry Smith 	{int __i;
1891987afe7SBarry Smith 	 for (__i=0; __i<nz; __i++) rtmps[pj[__i]] -= multiplier * pv[__i];
1901987afe7SBarry Smith 	}
191289bc588SBarry Smith       }
192dbb450caSBarry Smith       row = *ajtmp++ + shift;
193289bc588SBarry Smith     }
194416022c9SBarry Smith     /* finished row so stick it into b->a */
195416022c9SBarry Smith     pv = b->a + ai[i] + shift;
196416022c9SBarry Smith     pj = b->j + ai[i] + shift;
1978c37ef55SBarry Smith     nz = ai[i+1] - ai[i];
198ec8511deSBarry Smith     if (rtmp[i] == 0.0) {SETERRQ(1,"MatLUFactorNumeric_SeqAIJ:Zero pivot");}
1998c37ef55SBarry Smith     rtmp[i] = 1.0/rtmp[i];
200416022c9SBarry Smith     for ( j=0; j<nz; j++ ) {pv[j] = rtmps[pj[j]];}
2018c37ef55SBarry Smith   }
2020452661fSBarry Smith   PetscFree(rtmp);
20378b31e54SBarry Smith   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
20478b31e54SBarry Smith   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
20578b31e54SBarry Smith   ierr = ISDestroy(isicol); CHKERRQ(ierr);
206416022c9SBarry Smith   C->factor      = FACTOR_LU;
207416022c9SBarry Smith   b->assembled = 1;
208416022c9SBarry Smith   PLogFlops(b->n);
209289bc588SBarry Smith   return 0;
210289bc588SBarry Smith }
2110a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
212416022c9SBarry Smith int MatLUFactor_SeqAIJ(Mat A,IS row,IS col,double f)
213da3a660dSBarry Smith {
214416022c9SBarry Smith   Mat_SeqAIJ *mat = (Mat_SeqAIJ *) A->data;
2156abc6512SBarry Smith   int        ierr;
216416022c9SBarry Smith   Mat        C;
217416022c9SBarry Smith 
218416022c9SBarry Smith   ierr = MatLUFactorSymbolic_SeqAIJ(A,row,col,f,&C); CHKERRQ(ierr);
219416022c9SBarry Smith   ierr = MatLUFactorNumeric_SeqAIJ(A,&C); CHKERRQ(ierr);
220da3a660dSBarry Smith 
221da3a660dSBarry Smith   /* free all the data structures from mat */
2220452661fSBarry Smith   PetscFree(mat->a);
2230452661fSBarry Smith   if (!mat->singlemalloc) {PetscFree(mat->i); PetscFree(mat->j);}
2240452661fSBarry Smith   if (mat->diag) PetscFree(mat->diag);
2250452661fSBarry Smith   if (mat->ilen) PetscFree(mat->ilen);
2260452661fSBarry Smith   if (mat->imax) PetscFree(mat->imax);
2270452661fSBarry Smith   if (mat->solve_work) PetscFree(mat->solve_work);
2280452661fSBarry Smith   PetscFree(mat);
229da3a660dSBarry Smith 
230416022c9SBarry Smith   PetscMemcpy(A,C,sizeof(struct _Mat));
2310452661fSBarry Smith   PetscHeaderDestroy(C);
232da3a660dSBarry Smith   return 0;
233da3a660dSBarry Smith }
2340a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
235416022c9SBarry Smith int MatSolve_SeqAIJ(Mat A,Vec bb, Vec xx)
2368c37ef55SBarry Smith {
237416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
238416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row;
239416022c9SBarry Smith   int        *r,*c, ierr, i,  n = a->m, *vi, *ai = a->i, *aj = a->j;
240416022c9SBarry Smith   int        nz,shift = a->indexshift;
241416022c9SBarry Smith   Scalar     *x,*b,*tmp, *tmps, *aa = a->a, sum, *v;
2428c37ef55SBarry Smith 
243416022c9SBarry Smith   if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolve_SeqAIJ:Not for unfactored matrix");
2449e25ed09SBarry Smith 
24578b31e54SBarry Smith   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
24678b31e54SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
247416022c9SBarry Smith   tmp  = a->solve_work;
2488c37ef55SBarry Smith 
24978b31e54SBarry Smith   ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr);
25078b31e54SBarry Smith   ierr = ISGetIndices(iscol,&c);CHKERRQ(ierr); c = c + (n-1);
2518c37ef55SBarry Smith 
2528c37ef55SBarry Smith   /* forward solve the lower triangular */
2538c37ef55SBarry Smith   tmp[0] = b[*r++];
2540cf60383SBarry Smith   tmps   = tmp + shift;
2558c37ef55SBarry Smith   for ( i=1; i<n; i++ ) {
256dbb450caSBarry Smith     v   = aa + ai[i] + shift;
257dbb450caSBarry Smith     vi  = aj + ai[i] + shift;
258416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
2598c37ef55SBarry Smith     sum = b[*r++];
2600cf60383SBarry Smith     while (nz--) sum -= *v++ * tmps[*vi++];
2618c37ef55SBarry Smith     tmp[i] = sum;
2628c37ef55SBarry Smith   }
2638c37ef55SBarry Smith 
2648c37ef55SBarry Smith   /* backward solve the upper triangular */
2658c37ef55SBarry Smith   for ( i=n-1; i>=0; i-- ){
266416022c9SBarry Smith     v   = aa + a->diag[i] + (!shift);
267416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
268416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
2698c37ef55SBarry Smith     sum = tmp[i];
2700cf60383SBarry Smith     while (nz--) sum -= *v++ * tmps[*vi++];
271416022c9SBarry Smith     x[*c--] = tmp[i] = sum*aa[a->diag[i]+shift];
2728c37ef55SBarry Smith   }
2738c37ef55SBarry Smith 
274898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
275898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(iscol,&c); CHKERRQ(ierr);
276416022c9SBarry Smith   PLogFlops(2*a->nz - a->n);
2778c37ef55SBarry Smith   return 0;
2788c37ef55SBarry Smith }
279416022c9SBarry Smith int MatSolveAdd_SeqAIJ(Mat A,Vec bb, Vec yy, Vec xx)
280da3a660dSBarry Smith {
281416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
282416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row;
283416022c9SBarry Smith   int        *r,*c, ierr, i,  n = a->m, *vi, *ai = a->i, *aj = a->j;
284416022c9SBarry Smith   int        nz, shift = a->indexshift;
285416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, sum, *v;
286da3a660dSBarry Smith 
287416022c9SBarry Smith   if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolveAdd_SeqAIJ:Not for unfactored matrix");
28878b31e54SBarry Smith   if (yy != xx) {ierr = VecCopy(yy,xx); CHKERRQ(ierr);}
289da3a660dSBarry Smith 
29078b31e54SBarry Smith   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
29178b31e54SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
292416022c9SBarry Smith   tmp  = a->solve_work;
293da3a660dSBarry Smith 
29478b31e54SBarry Smith   ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr);
29578b31e54SBarry Smith   ierr = ISGetIndices(iscol,&c); CHKERRQ(ierr); c = c + (n-1);
296da3a660dSBarry Smith 
297da3a660dSBarry Smith   /* forward solve the lower triangular */
298da3a660dSBarry Smith   tmp[0] = b[*r++];
299da3a660dSBarry Smith   for ( i=1; i<n; i++ ) {
300dbb450caSBarry Smith     v   = aa + ai[i] + shift;
301dbb450caSBarry Smith     vi  = aj + ai[i] + shift;
302416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
303da3a660dSBarry Smith     sum = b[*r++];
304dbb450caSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ + shift];
305da3a660dSBarry Smith     tmp[i] = sum;
306da3a660dSBarry Smith   }
307da3a660dSBarry Smith 
308da3a660dSBarry Smith   /* backward solve the upper triangular */
309da3a660dSBarry Smith   for ( i=n-1; i>=0; i-- ){
310416022c9SBarry Smith     v   = aa + a->diag[i] + (!shift);
311416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
312416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
313da3a660dSBarry Smith     sum = tmp[i];
314dbb450caSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ + shift];
315416022c9SBarry Smith     tmp[i] = sum*aa[a->diag[i]+shift];
316da3a660dSBarry Smith     x[*c--] += tmp[i];
317da3a660dSBarry Smith   }
318da3a660dSBarry Smith 
319898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
320898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(iscol,&c); CHKERRQ(ierr);
321416022c9SBarry Smith   PLogFlops(2*a->nz);
322898183ecSLois Curfman McInnes 
323da3a660dSBarry Smith   return 0;
324da3a660dSBarry Smith }
325da3a660dSBarry Smith /* -------------------------------------------------------------------*/
326416022c9SBarry Smith int MatSolveTrans_SeqAIJ(Mat A,Vec bb, Vec xx)
327da3a660dSBarry Smith {
328416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
329416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row, invisrow,inviscol;
330416022c9SBarry Smith   int        *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j;
331416022c9SBarry Smith   int        nz,shift = a->indexshift;
332416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, *v;
333da3a660dSBarry Smith 
334416022c9SBarry Smith   if (A->factor != FACTOR_LU)  SETERRQ(1,"MatSolveTrans_SeqAIJ:Not unfactored matrix");
33578b31e54SBarry Smith   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
33678b31e54SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
337416022c9SBarry Smith   tmp  = a->solve_work;
338da3a660dSBarry Smith 
339da3a660dSBarry Smith   /* invert the permutations */
34078b31e54SBarry Smith   ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr);
34178b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr);
342da3a660dSBarry Smith 
34378b31e54SBarry Smith   ierr = ISGetIndices(invisrow,&r); CHKERRQ(ierr);
34478b31e54SBarry Smith   ierr = ISGetIndices(inviscol,&c); CHKERRQ(ierr);
345da3a660dSBarry Smith 
346da3a660dSBarry Smith   /* copy the b into temp work space according to permutation */
347da3a660dSBarry Smith   for ( i=0; i<n; i++ ) tmp[c[i]] = b[i];
348da3a660dSBarry Smith 
349da3a660dSBarry Smith   /* forward solve the U^T */
350da3a660dSBarry Smith   for ( i=0; i<n; i++ ) {
351416022c9SBarry Smith     v   = aa + a->diag[i] + shift;
352416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
353416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
354da3a660dSBarry Smith     tmp[i] *= *v++;
355da3a660dSBarry Smith     while (nz--) {
356dbb450caSBarry Smith       tmp[*vi++ + shift] -= (*v++)*tmp[i];
357da3a660dSBarry Smith     }
358da3a660dSBarry Smith   }
359da3a660dSBarry Smith 
360da3a660dSBarry Smith   /* backward solve the L^T */
361da3a660dSBarry Smith   for ( i=n-1; i>=0; i-- ){
362416022c9SBarry Smith     v   = aa + a->diag[i] - 1 + shift;
363416022c9SBarry Smith     vi  = aj + a->diag[i] - 1 + shift;
364416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
365da3a660dSBarry Smith     while (nz--) {
366dbb450caSBarry Smith       tmp[*vi-- + shift] -= (*v--)*tmp[i];
367da3a660dSBarry Smith     }
368da3a660dSBarry Smith   }
369da3a660dSBarry Smith 
370da3a660dSBarry Smith   /* copy tmp into x according to permutation */
371da3a660dSBarry Smith   for ( i=0; i<n; i++ ) x[r[i]] = tmp[i];
372da3a660dSBarry Smith 
373898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(invisrow,&r); CHKERRQ(ierr);
374898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(inviscol,&c); CHKERRQ(ierr);
375898183ecSLois Curfman McInnes   ierr = ISDestroy(invisrow); CHKERRQ(ierr);
376898183ecSLois Curfman McInnes   ierr = ISDestroy(inviscol); CHKERRQ(ierr);
377da3a660dSBarry Smith 
378416022c9SBarry Smith   PLogFlops(2*a->nz-a->n);
379da3a660dSBarry Smith   return 0;
380da3a660dSBarry Smith }
381da3a660dSBarry Smith 
382416022c9SBarry Smith int MatSolveTransAdd_SeqAIJ(Mat A,Vec bb, Vec zz,Vec xx)
383da3a660dSBarry Smith {
384416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
385416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row, invisrow,inviscol;
386416022c9SBarry Smith   int        *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j;
387416022c9SBarry Smith   int        nz,shift = a->indexshift;
388416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, *v;
3896abc6512SBarry Smith 
390416022c9SBarry Smith   if (A->factor != FACTOR_LU)SETERRQ(1,"MatSolveTransAdd_SeqAIJ:Not unfactored matrix");
3916abc6512SBarry Smith   if (zz != xx) VecCopy(zz,xx);
3926abc6512SBarry Smith 
39378b31e54SBarry Smith   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
39478b31e54SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
395416022c9SBarry Smith   tmp = a->solve_work;
3966abc6512SBarry Smith 
3976abc6512SBarry Smith   /* invert the permutations */
39878b31e54SBarry Smith   ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr);
39978b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr);
40078b31e54SBarry Smith   ierr = ISGetIndices(invisrow,&r); CHKERRQ(ierr);
40178b31e54SBarry Smith   ierr = ISGetIndices(inviscol,&c); CHKERRQ(ierr);
4026abc6512SBarry Smith 
4036abc6512SBarry Smith   /* copy the b into temp work space according to permutation */
4046abc6512SBarry Smith   for ( i=0; i<n; i++ ) tmp[c[i]] = b[i];
4056abc6512SBarry Smith 
4066abc6512SBarry Smith   /* forward solve the U^T */
4076abc6512SBarry Smith   for ( i=0; i<n; i++ ) {
408416022c9SBarry Smith     v   = aa + a->diag[i] + shift;
409416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
410416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
4116abc6512SBarry Smith     tmp[i] *= *v++;
4126abc6512SBarry Smith     while (nz--) {
413dbb450caSBarry Smith       tmp[*vi++ + shift] -= (*v++)*tmp[i];
4146abc6512SBarry Smith     }
4156abc6512SBarry Smith   }
4166abc6512SBarry Smith 
4176abc6512SBarry Smith   /* backward solve the L^T */
4186abc6512SBarry Smith   for ( i=n-1; i>=0; i-- ){
419416022c9SBarry Smith     v   = aa + a->diag[i] - 1 + shift;
420416022c9SBarry Smith     vi  = aj + a->diag[i] - 1 + shift;
421416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
4226abc6512SBarry Smith     while (nz--) {
423dbb450caSBarry Smith       tmp[*vi-- + shift] -= (*v--)*tmp[i];
4246abc6512SBarry Smith     }
4256abc6512SBarry Smith   }
4266abc6512SBarry Smith 
4276abc6512SBarry Smith   /* copy tmp into x according to permutation */
4286abc6512SBarry Smith   for ( i=0; i<n; i++ ) x[r[i]] += tmp[i];
4296abc6512SBarry Smith 
430898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(invisrow,&r); CHKERRQ(ierr);
431898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(inviscol,&c); CHKERRQ(ierr);
432898183ecSLois Curfman McInnes   ierr = ISDestroy(invisrow); CHKERRQ(ierr);
433898183ecSLois Curfman McInnes   ierr = ISDestroy(inviscol); CHKERRQ(ierr);
4346abc6512SBarry Smith 
435416022c9SBarry Smith   PLogFlops(2*a->nz);
4366abc6512SBarry Smith   return 0;
437da3a660dSBarry Smith }
4389e25ed09SBarry Smith /* ----------------------------------------------------------------*/
43908480c60SBarry Smith 
44008480c60SBarry Smith int MatILUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,int levels,Mat *fact)
4419e25ed09SBarry Smith {
442416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b;
4439e25ed09SBarry Smith   IS         isicol;
444416022c9SBarry Smith   int        *r,*ic, ierr, prow, n = a->m, *ai = a->i, *aj = a->j;
44556beaf04SBarry Smith   int        *ainew,*ajnew, jmax,*fill, *xi, nz, *im,*ajfill,*flev;
44656beaf04SBarry Smith   int        *dloc, idx, row,m,fm, nzf, nzi,len,  realloc = 0;
447416022c9SBarry Smith   int        incrlev,nnz,i,shift = a->indexshift;
4489e25ed09SBarry Smith 
449416022c9SBarry Smith   if (n != a->n) SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:Matrix must be square");
450416022c9SBarry Smith   if (!isrow) SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:Must have row permutation");
451416022c9SBarry Smith   if (!iscol) SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:Must have column permutation");
4529e25ed09SBarry Smith 
45308480c60SBarry Smith   /* special case that simply copies fill pattern */
45408480c60SBarry Smith   if (levels == 0 && ISIsIdentity(isrow) && ISIsIdentity(iscol)) {
45508480c60SBarry Smith     ierr = MatCopyPrivate_SeqAIJ(A,fact,DO_NOT_COPY_VALUES); CHKERRQ(ierr);
45608480c60SBarry Smith     (*fact)->factor = FACTOR_LU;
45708480c60SBarry Smith     b               = (Mat_SeqAIJ *) (*fact)->data;
45808480c60SBarry Smith     if (!b->diag) {
45908480c60SBarry Smith       ierr = MatMarkDiag_SeqAIJ(*fact); CHKERRQ(ierr);
46008480c60SBarry Smith     }
46108480c60SBarry Smith     b->row          = isrow;
46208480c60SBarry Smith     b->col          = iscol;
4630452661fSBarry Smith     b->solve_work = (Scalar *) PetscMalloc((b->m+1)*sizeof(Scalar));CHKPTRQ(b->solve_work);
46408480c60SBarry Smith     return 0;
46508480c60SBarry Smith   }
46608480c60SBarry Smith 
46778b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr);
468898183ecSLois Curfman McInnes   ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr);
469898183ecSLois Curfman McInnes   ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr);
4709e25ed09SBarry Smith 
4719e25ed09SBarry Smith   /* get new row pointers */
4720452661fSBarry Smith   ainew = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew);
473dbb450caSBarry Smith   ainew[0] = -shift;
4749e25ed09SBarry Smith   /* don't know how many column pointers are needed so estimate */
475dbb450caSBarry Smith   jmax = (int) (f*(ai[n]+!shift));
4760452661fSBarry Smith   ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew);
4779e25ed09SBarry Smith   /* ajfill is level of fill for each fill entry */
4780452661fSBarry Smith   ajfill = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajfill);
4799e25ed09SBarry Smith   /* fill is a linked list of nonzeros in active row */
4800452661fSBarry Smith   fill = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(fill);
48156beaf04SBarry Smith   /* im is level for each filled value */
4820452661fSBarry Smith   im = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(im);
48356beaf04SBarry Smith   /* dloc is location of diagonal in factor */
4840452661fSBarry Smith   dloc = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(dloc);
48556beaf04SBarry Smith   dloc[0]  = 0;
4869e25ed09SBarry Smith 
48756beaf04SBarry Smith   for ( prow=0; prow<n; prow++ ) {
4889e25ed09SBarry Smith     /* first copy previous fill into linked list */
48956beaf04SBarry Smith     nzf     = nz  = ai[r[prow]+1] - ai[r[prow]];
490dbb450caSBarry Smith     xi      = aj + ai[r[prow]] + shift;
4919e25ed09SBarry Smith     fill[n] = n;
4929e25ed09SBarry Smith     while (nz--) {
4939e25ed09SBarry Smith       fm  = n;
494dbb450caSBarry Smith       idx = ic[*xi++ + shift];
4959e25ed09SBarry Smith       do {
4969e25ed09SBarry Smith         m  = fm;
4979e25ed09SBarry Smith         fm = fill[m];
4989e25ed09SBarry Smith       } while (fm < idx);
4999e25ed09SBarry Smith       fill[m]   = idx;
5009e25ed09SBarry Smith       fill[idx] = fm;
50156beaf04SBarry Smith       im[idx]   = 0;
5029e25ed09SBarry Smith     }
50356beaf04SBarry Smith     nzi = 0;
5049e25ed09SBarry Smith     row = fill[n];
50556beaf04SBarry Smith     while ( row < prow ) {
50656beaf04SBarry Smith       incrlev = im[row] + 1;
50756beaf04SBarry Smith       nz      = dloc[row];
508dbb450caSBarry Smith       xi      = ajnew  + ainew[row] + shift + nz;
509dbb450caSBarry Smith       flev    = ajfill + ainew[row] + shift + nz + 1;
510416022c9SBarry Smith       nnz     = ainew[row+1] - ainew[row] - nz - 1;
511dbb450caSBarry Smith       if (*xi++ + shift != row) {
512ec8511deSBarry Smith         SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:zero pivot");
51356beaf04SBarry Smith       }
51456beaf04SBarry Smith       fm      = row;
51556beaf04SBarry Smith       while (nnz-- > 0) {
516dbb450caSBarry Smith         idx = *xi++ + shift;
51756beaf04SBarry Smith         if (*flev + incrlev > levels) {
51856beaf04SBarry Smith           flev++;
51956beaf04SBarry Smith           continue;
52056beaf04SBarry Smith         }
52156beaf04SBarry Smith         do {
5229e25ed09SBarry Smith           m  = fm;
5239e25ed09SBarry Smith           fm = fill[m];
52456beaf04SBarry Smith         } while (fm < idx);
5259e25ed09SBarry Smith         if (fm != idx) {
52656beaf04SBarry Smith           im[idx]   = *flev + incrlev;
5279e25ed09SBarry Smith           fill[m]   = idx;
5289e25ed09SBarry Smith           fill[idx] = fm;
5299e25ed09SBarry Smith           fm        = idx;
53056beaf04SBarry Smith           nzf++;
5319e25ed09SBarry Smith         }
53256beaf04SBarry Smith         else {
53356beaf04SBarry Smith           if (im[idx] > *flev + incrlev) im[idx] = *flev+incrlev;
5349e25ed09SBarry Smith         }
53556beaf04SBarry Smith         flev++;
5369e25ed09SBarry Smith       }
5379e25ed09SBarry Smith       row = fill[row];
53856beaf04SBarry Smith       nzi++;
5399e25ed09SBarry Smith     }
5409e25ed09SBarry Smith     /* copy new filled row into permanent storage */
54156beaf04SBarry Smith     ainew[prow+1] = ainew[prow] + nzf;
54256beaf04SBarry Smith     if (ainew[prow+1] > jmax+1) {
5439e25ed09SBarry Smith       /* allocate a longer ajnew */
544bbb6d6a8SBarry Smith       int maxadd;
545dbb450caSBarry Smith       maxadd = (int) ((f*(ai[n]+!shift)*(n-prow+5))/n);
54656beaf04SBarry Smith       if (maxadd < nzf) maxadd = (n-prow)*(nzf+1);
547bbb6d6a8SBarry Smith       jmax += maxadd;
5480452661fSBarry Smith       xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi);
549416022c9SBarry Smith       PetscMemcpy(xi,ajnew,(ainew[prow]+shift)*sizeof(int));
5500452661fSBarry Smith       PetscFree(ajnew);
55156beaf04SBarry Smith       ajnew = xi;
5529e25ed09SBarry Smith       /* allocate a longer ajfill */
5530452661fSBarry Smith       xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi);
554416022c9SBarry Smith       PetscMemcpy(xi,ajfill,(ainew[prow]+shift)*sizeof(int));
5550452661fSBarry Smith       PetscFree(ajfill);
55656beaf04SBarry Smith       ajfill = xi;
557bbb6d6a8SBarry Smith       realloc++;
5589e25ed09SBarry Smith     }
559dbb450caSBarry Smith     xi          = ajnew + ainew[prow] + shift;
560dbb450caSBarry Smith     flev        = ajfill + ainew[prow] + shift;
56156beaf04SBarry Smith     dloc[prow]  = nzi;
5629e25ed09SBarry Smith     fm          = fill[n];
56356beaf04SBarry Smith     while (nzf--) {
564dbb450caSBarry Smith       *xi++   = fm - shift;
56556beaf04SBarry Smith       *flev++ = im[fm];
5669e25ed09SBarry Smith       fm      = fill[fm];
5679e25ed09SBarry Smith     }
5689e25ed09SBarry Smith   }
5690452661fSBarry Smith   PetscFree(ajfill);
570898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
571898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
572898183ecSLois Curfman McInnes   ierr = ISDestroy(isicol); CHKERRQ(ierr);
5730452661fSBarry Smith   PetscFree(fill); PetscFree(im);
5749e25ed09SBarry Smith 
575416022c9SBarry Smith   PLogInfo((PetscObject)A,
576ec8511deSBarry Smith     "Info:MatILUFactorSymbolic_SeqAIJ:Realloc %d Fill ratio:given %g needed %g\n",
57756beaf04SBarry Smith                              realloc,f,((double)ainew[n])/((double)ai[prow]));
578bbb6d6a8SBarry Smith 
5799e25ed09SBarry Smith   /* put together the new matrix */
580416022c9SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,n, n, 0, 0, fact); CHKERRQ(ierr);
581416022c9SBarry Smith   b = (Mat_SeqAIJ *) (*fact)->data;
5820452661fSBarry Smith   PetscFree(b->imax);
583416022c9SBarry Smith   b->singlemalloc = 0;
584dbb450caSBarry Smith   len = (ainew[n] + shift)*sizeof(Scalar);
5859e25ed09SBarry Smith   /* the next line frees the default space generated by the Create() */
5860452661fSBarry Smith   PetscFree(b->a); PetscFree(b->ilen);
5870452661fSBarry Smith   b->a          = (Scalar *) PetscMalloc( len ); CHKPTRQ(b->a);
588416022c9SBarry Smith   b->j          = ajnew;
589416022c9SBarry Smith   b->i          = ainew;
59056beaf04SBarry Smith   for ( i=0; i<n; i++ ) dloc[i] += ainew[i];
591416022c9SBarry Smith   b->diag       = dloc;
592416022c9SBarry Smith   b->ilen       = 0;
593416022c9SBarry Smith   b->imax       = 0;
594416022c9SBarry Smith   b->row        = isrow;
595416022c9SBarry Smith   b->col        = iscol;
5960452661fSBarry Smith   b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar));
597416022c9SBarry Smith   CHKPTRQ(b->solve_work);
598416022c9SBarry Smith   /* In b structure:  Free imax, ilen, old a, old j.
59956beaf04SBarry Smith      Allocate dloc, solve_work, new a, new j */
600dbb450caSBarry Smith   PLogObjectMemory(*fact,(ainew[n]+shift-n) * (sizeof(int)+sizeof(Scalar)));
601416022c9SBarry Smith   b->maxnz          = b->nz = ainew[n] + shift;
6029e25ed09SBarry Smith   (*fact)->factor   = FACTOR_LU;
6039e25ed09SBarry Smith   return 0;
6049e25ed09SBarry Smith }
605*d5d45c9bSBarry Smith 
606*d5d45c9bSBarry Smith 
607*d5d45c9bSBarry Smith 
608*d5d45c9bSBarry Smith 
609