xref: /petsc/src/mat/impls/aij/seq/aijfact.c (revision 48e945598e149d53bd4656ff0f6eb4668f7aa5e6)
1cb512458SBarry Smith #ifndef lint
2*48e94559SBarry Smith static char vcid[] = "$Id: aijfact.c,v 1.53 1995/12/23 04:53:32 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 */
113b4fd4287SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,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 /* ----------------------------------------------------------- */
14041c01911SSatish Balay int Mat_AIJ_CheckInode(Mat);
14141c01911SSatish Balay 
142416022c9SBarry Smith int MatLUFactorNumeric_SeqAIJ(Mat A,Mat *B)
143289bc588SBarry Smith {
144416022c9SBarry Smith   Mat        C = *B;
145416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b = (Mat_SeqAIJ *)C->data;
146416022c9SBarry Smith   IS         iscol = b->col, isrow = b->row, isicol;
147416022c9SBarry Smith   int        *r,*ic, ierr, i, j, n = a->m, *ai = b->i, *aj = b->j;
1481987afe7SBarry Smith   int        *ajtmpold, *ajtmp, nz, row, *ics, shift = a->indexshift;
149d5d45c9bSBarry Smith   int        *diag_offset = b->diag;
150390f2bbaSBarry Smith   Scalar     *rtmp,*v, *pc, multiplier;
1511987afe7SBarry Smith   /* These declarations are for optimizations.  They reduce the number of
1521987afe7SBarry Smith      memory references that are made by locally storing information; the
1531987afe7SBarry Smith      word "register" used here with pointers can be viewed as "private" or
1541987afe7SBarry Smith      "known only to me"
1551987afe7SBarry Smith    */
156390f2bbaSBarry Smith   register Scalar *pv, *rtmps;
1571987afe7SBarry Smith   register int    *pj;
158289bc588SBarry Smith 
15978b31e54SBarry Smith   ierr  = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr);
160416022c9SBarry Smith   PLogObjectParent(*B,isicol);
16178b31e54SBarry Smith   ierr  = ISGetIndices(isrow,&r); CHKERRQ(ierr);
16278b31e54SBarry Smith   ierr  = ISGetIndices(isicol,&ic); CHKERRQ(ierr);
1630452661fSBarry Smith   rtmp  = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar) ); CHKPTRQ(rtmp);
1640cf60383SBarry Smith   rtmps = rtmp + shift; ics = ic + shift;
165289bc588SBarry Smith 
166289bc588SBarry Smith   for ( i=0; i<n; i++ ) {
167289bc588SBarry Smith     nz    = ai[i+1] - ai[i];
168dbb450caSBarry Smith     ajtmp = aj + ai[i] + shift;
169*48e94559SBarry Smith     for  ( j=0; j<nz; j++ ) rtmps[ajtmp[j]] = 0.0;
170*48e94559SBarry Smith     /* for(j = 0; j < n; ++j) rtmp[j] =0.0; */
171289bc588SBarry Smith 
172289bc588SBarry Smith     /* load in initial (unfactored row) */
173416022c9SBarry Smith     nz       = a->i[r[i]+1] - a->i[r[i]];
174416022c9SBarry Smith     ajtmpold = a->j + a->i[r[i]] + shift;
175416022c9SBarry Smith     v        = a->a + a->i[r[i]] + shift;
1760cf60383SBarry Smith     for ( j=0; j<nz; j++ ) rtmp[ics[ajtmpold[j]]] =  v[j];
177289bc588SBarry Smith 
178dbb450caSBarry Smith     row = *ajtmp++ + shift;
179289bc588SBarry Smith     while (row < i) {
1808c37ef55SBarry Smith       pc = rtmp + row;
1818c37ef55SBarry Smith       if (*pc != 0.0) {
1821987afe7SBarry Smith         pv         = b->a + diag_offset[row] + shift;
1831987afe7SBarry Smith         pj         = b->j + diag_offset[row] + (!shift);
1848c37ef55SBarry Smith         multiplier = *pc * *pv++;
1858c37ef55SBarry Smith         *pc        = multiplier;
1861987afe7SBarry Smith         nz         = ai[row+1] - diag_offset[row] - 1;
187eef2e97bSBarry Smith         PLogFlops(2*nz);
1881987afe7SBarry Smith 	/* The for-loop form can aid the compiler in overlapping
1891987afe7SBarry Smith 	   loads and stores */
1901987afe7SBarry Smith         /*while (nz-->0) rtmps[*pj++] -= multiplier* *pv++;  */
1911987afe7SBarry Smith 	{int __i;
1921987afe7SBarry Smith 	 for (__i=0; __i<nz; __i++) rtmps[pj[__i]] -= multiplier * pv[__i];
1931987afe7SBarry Smith 	}
194289bc588SBarry Smith       }
195dbb450caSBarry Smith       row = *ajtmp++ + shift;
196289bc588SBarry Smith     }
197416022c9SBarry Smith     /* finished row so stick it into b->a */
198416022c9SBarry Smith     pv = b->a + ai[i] + shift;
199416022c9SBarry Smith     pj = b->j + ai[i] + shift;
2008c37ef55SBarry Smith     nz = ai[i+1] - ai[i];
201ec8511deSBarry Smith     if (rtmp[i] == 0.0) {SETERRQ(1,"MatLUFactorNumeric_SeqAIJ:Zero pivot");}
2028c37ef55SBarry Smith     rtmp[i] = 1.0/rtmp[i];
203416022c9SBarry Smith     for ( j=0; j<nz; j++ ) {pv[j] = rtmps[pj[j]];}
2048c37ef55SBarry Smith   }
2050f11f9aeSSatish Balay 
2060452661fSBarry Smith   PetscFree(rtmp);
20778b31e54SBarry Smith   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
20878b31e54SBarry Smith   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
20978b31e54SBarry Smith   ierr = ISDestroy(isicol); CHKERRQ(ierr);
210416022c9SBarry Smith   C->factor = FACTOR_LU;
21141c01911SSatish Balay   ierr = Mat_AIJ_CheckInode(C); CHKERRQ(ierr);
212416022c9SBarry Smith   b->assembled = 1;
213416022c9SBarry Smith   PLogFlops(b->n);
214289bc588SBarry Smith   return 0;
215289bc588SBarry Smith }
2160a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
217416022c9SBarry Smith int MatLUFactor_SeqAIJ(Mat A,IS row,IS col,double f)
218da3a660dSBarry Smith {
219416022c9SBarry Smith   Mat_SeqAIJ *mat = (Mat_SeqAIJ *) A->data;
2206abc6512SBarry Smith   int        ierr;
221416022c9SBarry Smith   Mat        C;
222416022c9SBarry Smith 
223416022c9SBarry Smith   ierr = MatLUFactorSymbolic_SeqAIJ(A,row,col,f,&C); CHKERRQ(ierr);
224416022c9SBarry Smith   ierr = MatLUFactorNumeric_SeqAIJ(A,&C); CHKERRQ(ierr);
225da3a660dSBarry Smith 
226da3a660dSBarry Smith   /* free all the data structures from mat */
2270452661fSBarry Smith   PetscFree(mat->a);
2280452661fSBarry Smith   if (!mat->singlemalloc) {PetscFree(mat->i); PetscFree(mat->j);}
2290452661fSBarry Smith   if (mat->diag) PetscFree(mat->diag);
2300452661fSBarry Smith   if (mat->ilen) PetscFree(mat->ilen);
2310452661fSBarry Smith   if (mat->imax) PetscFree(mat->imax);
2320452661fSBarry Smith   if (mat->solve_work) PetscFree(mat->solve_work);
233a7a49059SBarry Smith   if (mat->inode.size) PetscFree(mat->inode.size);
2340452661fSBarry Smith   PetscFree(mat);
235da3a660dSBarry Smith 
236416022c9SBarry Smith   PetscMemcpy(A,C,sizeof(struct _Mat));
2370452661fSBarry Smith   PetscHeaderDestroy(C);
238da3a660dSBarry Smith   return 0;
239da3a660dSBarry Smith }
2400a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */
241416022c9SBarry Smith int MatSolve_SeqAIJ(Mat A,Vec bb, Vec xx)
2428c37ef55SBarry Smith {
243416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
244416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row;
245416022c9SBarry Smith   int        *r,*c, ierr, i,  n = a->m, *vi, *ai = a->i, *aj = a->j;
246416022c9SBarry Smith   int        nz,shift = a->indexshift;
247416022c9SBarry Smith   Scalar     *x,*b,*tmp, *tmps, *aa = a->a, sum, *v;
2488c37ef55SBarry Smith 
249416022c9SBarry Smith   if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolve_SeqAIJ:Not for unfactored matrix");
2509e25ed09SBarry Smith 
25178b31e54SBarry Smith   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
25278b31e54SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
253416022c9SBarry Smith   tmp  = a->solve_work;
2548c37ef55SBarry Smith 
25578b31e54SBarry Smith   ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr);
25678b31e54SBarry Smith   ierr = ISGetIndices(iscol,&c);CHKERRQ(ierr); c = c + (n-1);
2578c37ef55SBarry Smith 
2588c37ef55SBarry Smith   /* forward solve the lower triangular */
2598c37ef55SBarry Smith   tmp[0] = b[*r++];
2600cf60383SBarry Smith   tmps   = tmp + shift;
2618c37ef55SBarry Smith   for ( i=1; i<n; i++ ) {
262dbb450caSBarry Smith     v   = aa + ai[i] + shift;
263dbb450caSBarry Smith     vi  = aj + ai[i] + shift;
264416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
2658c37ef55SBarry Smith     sum = b[*r++];
2660cf60383SBarry Smith     while (nz--) sum -= *v++ * tmps[*vi++];
2678c37ef55SBarry Smith     tmp[i] = sum;
2688c37ef55SBarry Smith   }
2698c37ef55SBarry Smith 
2708c37ef55SBarry Smith   /* backward solve the upper triangular */
2718c37ef55SBarry Smith   for ( i=n-1; i>=0; i-- ){
272416022c9SBarry Smith     v   = aa + a->diag[i] + (!shift);
273416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
274416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
2758c37ef55SBarry Smith     sum = tmp[i];
2760cf60383SBarry Smith     while (nz--) sum -= *v++ * tmps[*vi++];
277416022c9SBarry Smith     x[*c--] = tmp[i] = sum*aa[a->diag[i]+shift];
2788c37ef55SBarry Smith   }
2798c37ef55SBarry Smith 
280898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
281898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(iscol,&c); CHKERRQ(ierr);
282416022c9SBarry Smith   PLogFlops(2*a->nz - a->n);
2838c37ef55SBarry Smith   return 0;
2848c37ef55SBarry Smith }
285416022c9SBarry Smith int MatSolveAdd_SeqAIJ(Mat A,Vec bb, Vec yy, Vec xx)
286da3a660dSBarry Smith {
287416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
288416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row;
289416022c9SBarry Smith   int        *r,*c, ierr, i,  n = a->m, *vi, *ai = a->i, *aj = a->j;
290416022c9SBarry Smith   int        nz, shift = a->indexshift;
291416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, sum, *v;
292da3a660dSBarry Smith 
293416022c9SBarry Smith   if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolveAdd_SeqAIJ:Not for unfactored matrix");
29478b31e54SBarry Smith   if (yy != xx) {ierr = VecCopy(yy,xx); CHKERRQ(ierr);}
295da3a660dSBarry Smith 
29678b31e54SBarry Smith   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
29778b31e54SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
298416022c9SBarry Smith   tmp  = a->solve_work;
299da3a660dSBarry Smith 
30078b31e54SBarry Smith   ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr);
30178b31e54SBarry Smith   ierr = ISGetIndices(iscol,&c); CHKERRQ(ierr); c = c + (n-1);
302da3a660dSBarry Smith 
303da3a660dSBarry Smith   /* forward solve the lower triangular */
304da3a660dSBarry Smith   tmp[0] = b[*r++];
305da3a660dSBarry Smith   for ( i=1; i<n; i++ ) {
306dbb450caSBarry Smith     v   = aa + ai[i] + shift;
307dbb450caSBarry Smith     vi  = aj + ai[i] + shift;
308416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
309da3a660dSBarry Smith     sum = b[*r++];
310dbb450caSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ + shift];
311da3a660dSBarry Smith     tmp[i] = sum;
312da3a660dSBarry Smith   }
313da3a660dSBarry Smith 
314da3a660dSBarry Smith   /* backward solve the upper triangular */
315da3a660dSBarry Smith   for ( i=n-1; i>=0; i-- ){
316416022c9SBarry Smith     v   = aa + a->diag[i] + (!shift);
317416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
318416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
319da3a660dSBarry Smith     sum = tmp[i];
320dbb450caSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ + shift];
321416022c9SBarry Smith     tmp[i] = sum*aa[a->diag[i]+shift];
322da3a660dSBarry Smith     x[*c--] += tmp[i];
323da3a660dSBarry Smith   }
324da3a660dSBarry Smith 
325898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
326898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(iscol,&c); CHKERRQ(ierr);
327416022c9SBarry Smith   PLogFlops(2*a->nz);
328898183ecSLois Curfman McInnes 
329da3a660dSBarry Smith   return 0;
330da3a660dSBarry Smith }
331da3a660dSBarry Smith /* -------------------------------------------------------------------*/
332416022c9SBarry Smith int MatSolveTrans_SeqAIJ(Mat A,Vec bb, Vec xx)
333da3a660dSBarry Smith {
334416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
335416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row, invisrow,inviscol;
336416022c9SBarry Smith   int        *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j;
337416022c9SBarry Smith   int        nz,shift = a->indexshift;
338416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, *v;
339da3a660dSBarry Smith 
340416022c9SBarry Smith   if (A->factor != FACTOR_LU)  SETERRQ(1,"MatSolveTrans_SeqAIJ:Not unfactored matrix");
34178b31e54SBarry Smith   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
34278b31e54SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
343416022c9SBarry Smith   tmp  = a->solve_work;
344da3a660dSBarry Smith 
345da3a660dSBarry Smith   /* invert the permutations */
34678b31e54SBarry Smith   ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr);
34778b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr);
348da3a660dSBarry Smith 
34978b31e54SBarry Smith   ierr = ISGetIndices(invisrow,&r); CHKERRQ(ierr);
35078b31e54SBarry Smith   ierr = ISGetIndices(inviscol,&c); CHKERRQ(ierr);
351da3a660dSBarry Smith 
352da3a660dSBarry Smith   /* copy the b into temp work space according to permutation */
353da3a660dSBarry Smith   for ( i=0; i<n; i++ ) tmp[c[i]] = b[i];
354da3a660dSBarry Smith 
355da3a660dSBarry Smith   /* forward solve the U^T */
356da3a660dSBarry Smith   for ( i=0; i<n; i++ ) {
357416022c9SBarry Smith     v   = aa + a->diag[i] + shift;
358416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
359416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
360da3a660dSBarry Smith     tmp[i] *= *v++;
361da3a660dSBarry Smith     while (nz--) {
362dbb450caSBarry Smith       tmp[*vi++ + shift] -= (*v++)*tmp[i];
363da3a660dSBarry Smith     }
364da3a660dSBarry Smith   }
365da3a660dSBarry Smith 
366da3a660dSBarry Smith   /* backward solve the L^T */
367da3a660dSBarry Smith   for ( i=n-1; i>=0; i-- ){
368416022c9SBarry Smith     v   = aa + a->diag[i] - 1 + shift;
369416022c9SBarry Smith     vi  = aj + a->diag[i] - 1 + shift;
370416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
371da3a660dSBarry Smith     while (nz--) {
372dbb450caSBarry Smith       tmp[*vi-- + shift] -= (*v--)*tmp[i];
373da3a660dSBarry Smith     }
374da3a660dSBarry Smith   }
375da3a660dSBarry Smith 
376da3a660dSBarry Smith   /* copy tmp into x according to permutation */
377da3a660dSBarry Smith   for ( i=0; i<n; i++ ) x[r[i]] = tmp[i];
378da3a660dSBarry Smith 
379898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(invisrow,&r); CHKERRQ(ierr);
380898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(inviscol,&c); CHKERRQ(ierr);
381898183ecSLois Curfman McInnes   ierr = ISDestroy(invisrow); CHKERRQ(ierr);
382898183ecSLois Curfman McInnes   ierr = ISDestroy(inviscol); CHKERRQ(ierr);
383da3a660dSBarry Smith 
384416022c9SBarry Smith   PLogFlops(2*a->nz-a->n);
385da3a660dSBarry Smith   return 0;
386da3a660dSBarry Smith }
387da3a660dSBarry Smith 
388416022c9SBarry Smith int MatSolveTransAdd_SeqAIJ(Mat A,Vec bb, Vec zz,Vec xx)
389da3a660dSBarry Smith {
390416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
391416022c9SBarry Smith   IS         iscol = a->col, isrow = a->row, invisrow,inviscol;
392416022c9SBarry Smith   int        *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j;
393416022c9SBarry Smith   int        nz,shift = a->indexshift;
394416022c9SBarry Smith   Scalar     *x,*b,*tmp, *aa = a->a, *v;
3956abc6512SBarry Smith 
396416022c9SBarry Smith   if (A->factor != FACTOR_LU)SETERRQ(1,"MatSolveTransAdd_SeqAIJ:Not unfactored matrix");
3976abc6512SBarry Smith   if (zz != xx) VecCopy(zz,xx);
3986abc6512SBarry Smith 
39978b31e54SBarry Smith   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
40078b31e54SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
401416022c9SBarry Smith   tmp = a->solve_work;
4026abc6512SBarry Smith 
4036abc6512SBarry Smith   /* invert the permutations */
40478b31e54SBarry Smith   ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr);
40578b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr);
40678b31e54SBarry Smith   ierr = ISGetIndices(invisrow,&r); CHKERRQ(ierr);
40778b31e54SBarry Smith   ierr = ISGetIndices(inviscol,&c); CHKERRQ(ierr);
4086abc6512SBarry Smith 
4096abc6512SBarry Smith   /* copy the b into temp work space according to permutation */
4106abc6512SBarry Smith   for ( i=0; i<n; i++ ) tmp[c[i]] = b[i];
4116abc6512SBarry Smith 
4126abc6512SBarry Smith   /* forward solve the U^T */
4136abc6512SBarry Smith   for ( i=0; i<n; i++ ) {
414416022c9SBarry Smith     v   = aa + a->diag[i] + shift;
415416022c9SBarry Smith     vi  = aj + a->diag[i] + (!shift);
416416022c9SBarry Smith     nz  = ai[i+1] - a->diag[i] - 1;
4176abc6512SBarry Smith     tmp[i] *= *v++;
4186abc6512SBarry Smith     while (nz--) {
419dbb450caSBarry Smith       tmp[*vi++ + shift] -= (*v++)*tmp[i];
4206abc6512SBarry Smith     }
4216abc6512SBarry Smith   }
4226abc6512SBarry Smith 
4236abc6512SBarry Smith   /* backward solve the L^T */
4246abc6512SBarry Smith   for ( i=n-1; i>=0; i-- ){
425416022c9SBarry Smith     v   = aa + a->diag[i] - 1 + shift;
426416022c9SBarry Smith     vi  = aj + a->diag[i] - 1 + shift;
427416022c9SBarry Smith     nz  = a->diag[i] - ai[i];
4286abc6512SBarry Smith     while (nz--) {
429dbb450caSBarry Smith       tmp[*vi-- + shift] -= (*v--)*tmp[i];
4306abc6512SBarry Smith     }
4316abc6512SBarry Smith   }
4326abc6512SBarry Smith 
4336abc6512SBarry Smith   /* copy tmp into x according to permutation */
4346abc6512SBarry Smith   for ( i=0; i<n; i++ ) x[r[i]] += tmp[i];
4356abc6512SBarry Smith 
436898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(invisrow,&r); CHKERRQ(ierr);
437898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(inviscol,&c); CHKERRQ(ierr);
438898183ecSLois Curfman McInnes   ierr = ISDestroy(invisrow); CHKERRQ(ierr);
439898183ecSLois Curfman McInnes   ierr = ISDestroy(inviscol); CHKERRQ(ierr);
4406abc6512SBarry Smith 
441416022c9SBarry Smith   PLogFlops(2*a->nz);
4426abc6512SBarry Smith   return 0;
443da3a660dSBarry Smith }
4449e25ed09SBarry Smith /* ----------------------------------------------------------------*/
44508480c60SBarry Smith 
44608480c60SBarry Smith int MatILUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,int levels,Mat *fact)
4479e25ed09SBarry Smith {
448416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b;
4499e25ed09SBarry Smith   IS         isicol;
450416022c9SBarry Smith   int        *r,*ic, ierr, prow, n = a->m, *ai = a->i, *aj = a->j;
45156beaf04SBarry Smith   int        *ainew,*ajnew, jmax,*fill, *xi, nz, *im,*ajfill,*flev;
45256beaf04SBarry Smith   int        *dloc, idx, row,m,fm, nzf, nzi,len,  realloc = 0;
453416022c9SBarry Smith   int        incrlev,nnz,i,shift = a->indexshift;
4549e25ed09SBarry Smith 
455416022c9SBarry Smith   if (n != a->n) SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:Matrix must be square");
456416022c9SBarry Smith   if (!isrow) SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:Must have row permutation");
457416022c9SBarry Smith   if (!iscol) SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:Must have column permutation");
4589e25ed09SBarry Smith 
45908480c60SBarry Smith   /* special case that simply copies fill pattern */
46008480c60SBarry Smith   if (levels == 0 && ISIsIdentity(isrow) && ISIsIdentity(iscol)) {
4613d1612f7SBarry Smith     ierr = MatConvertSameType_SeqAIJ(A,fact,DO_NOT_COPY_VALUES); CHKERRQ(ierr);
46208480c60SBarry Smith     (*fact)->factor = FACTOR_LU;
46308480c60SBarry Smith     b               = (Mat_SeqAIJ *) (*fact)->data;
46408480c60SBarry Smith     if (!b->diag) {
46508480c60SBarry Smith       ierr = MatMarkDiag_SeqAIJ(*fact); CHKERRQ(ierr);
46608480c60SBarry Smith     }
46708480c60SBarry Smith     b->row          = isrow;
46808480c60SBarry Smith     b->col          = iscol;
4690452661fSBarry Smith     b->solve_work = (Scalar *) PetscMalloc((b->m+1)*sizeof(Scalar));CHKPTRQ(b->solve_work);
47008480c60SBarry Smith     return 0;
47108480c60SBarry Smith   }
47208480c60SBarry Smith 
47378b31e54SBarry Smith   ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr);
474898183ecSLois Curfman McInnes   ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr);
475898183ecSLois Curfman McInnes   ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr);
4769e25ed09SBarry Smith 
4779e25ed09SBarry Smith   /* get new row pointers */
4780452661fSBarry Smith   ainew = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew);
479dbb450caSBarry Smith   ainew[0] = -shift;
4809e25ed09SBarry Smith   /* don't know how many column pointers are needed so estimate */
481dbb450caSBarry Smith   jmax = (int) (f*(ai[n]+!shift));
4820452661fSBarry Smith   ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew);
4839e25ed09SBarry Smith   /* ajfill is level of fill for each fill entry */
4840452661fSBarry Smith   ajfill = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajfill);
4859e25ed09SBarry Smith   /* fill is a linked list of nonzeros in active row */
4860452661fSBarry Smith   fill = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(fill);
48756beaf04SBarry Smith   /* im is level for each filled value */
4880452661fSBarry Smith   im = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(im);
48956beaf04SBarry Smith   /* dloc is location of diagonal in factor */
4900452661fSBarry Smith   dloc = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(dloc);
49156beaf04SBarry Smith   dloc[0]  = 0;
49256beaf04SBarry Smith   for ( prow=0; prow<n; prow++ ) {
4939e25ed09SBarry Smith     /* first copy previous fill into linked list */
49456beaf04SBarry Smith     nzf     = nz  = ai[r[prow]+1] - ai[r[prow]];
495dbb450caSBarry Smith     xi      = aj + ai[r[prow]] + shift;
4969e25ed09SBarry Smith     fill[n] = n;
4979e25ed09SBarry Smith     while (nz--) {
4989e25ed09SBarry Smith       fm  = n;
499dbb450caSBarry Smith       idx = ic[*xi++ + shift];
5009e25ed09SBarry Smith       do {
5019e25ed09SBarry Smith         m  = fm;
5029e25ed09SBarry Smith         fm = fill[m];
5039e25ed09SBarry Smith       } while (fm < idx);
5049e25ed09SBarry Smith       fill[m]   = idx;
5059e25ed09SBarry Smith       fill[idx] = fm;
50656beaf04SBarry Smith       im[idx]   = 0;
5079e25ed09SBarry Smith     }
50856beaf04SBarry Smith     nzi = 0;
5099e25ed09SBarry Smith     row = fill[n];
51056beaf04SBarry Smith     while ( row < prow ) {
51156beaf04SBarry Smith       incrlev = im[row] + 1;
51256beaf04SBarry Smith       nz      = dloc[row];
513dbb450caSBarry Smith       xi      = ajnew  + ainew[row] + shift + nz;
514dbb450caSBarry Smith       flev    = ajfill + ainew[row] + shift + nz + 1;
515416022c9SBarry Smith       nnz     = ainew[row+1] - ainew[row] - nz - 1;
516dbb450caSBarry Smith       if (*xi++ + shift != row) {
517ec8511deSBarry Smith         SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:zero pivot");
51856beaf04SBarry Smith       }
51956beaf04SBarry Smith       fm      = row;
52056beaf04SBarry Smith       while (nnz-- > 0) {
521dbb450caSBarry Smith         idx = *xi++ + shift;
52256beaf04SBarry Smith         if (*flev + incrlev > levels) {
52356beaf04SBarry Smith           flev++;
52456beaf04SBarry Smith           continue;
52556beaf04SBarry Smith         }
52656beaf04SBarry Smith         do {
5279e25ed09SBarry Smith           m  = fm;
5289e25ed09SBarry Smith           fm = fill[m];
52956beaf04SBarry Smith         } while (fm < idx);
5309e25ed09SBarry Smith         if (fm != idx) {
53156beaf04SBarry Smith           im[idx]   = *flev + incrlev;
5329e25ed09SBarry Smith           fill[m]   = idx;
5339e25ed09SBarry Smith           fill[idx] = fm;
5349e25ed09SBarry Smith           fm        = idx;
53556beaf04SBarry Smith           nzf++;
5369e25ed09SBarry Smith         }
53756beaf04SBarry Smith         else {
53856beaf04SBarry Smith           if (im[idx] > *flev + incrlev) im[idx] = *flev+incrlev;
5399e25ed09SBarry Smith         }
54056beaf04SBarry Smith         flev++;
5419e25ed09SBarry Smith       }
5429e25ed09SBarry Smith       row = fill[row];
54356beaf04SBarry Smith       nzi++;
5449e25ed09SBarry Smith     }
5459e25ed09SBarry Smith     /* copy new filled row into permanent storage */
54656beaf04SBarry Smith     ainew[prow+1] = ainew[prow] + nzf;
547d7e8b826SBarry Smith     if (ainew[prow+1] > jmax-shift) {
5489e25ed09SBarry Smith       /* allocate a longer ajnew */
549bbb6d6a8SBarry Smith       int maxadd;
550dbb450caSBarry Smith       maxadd = (int) ((f*(ai[n]+!shift)*(n-prow+5))/n);
55156beaf04SBarry Smith       if (maxadd < nzf) maxadd = (n-prow)*(nzf+1);
552bbb6d6a8SBarry Smith       jmax += maxadd;
5530452661fSBarry Smith       xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi);
554416022c9SBarry Smith       PetscMemcpy(xi,ajnew,(ainew[prow]+shift)*sizeof(int));
5550452661fSBarry Smith       PetscFree(ajnew);
55656beaf04SBarry Smith       ajnew = xi;
5579e25ed09SBarry Smith       /* allocate a longer ajfill */
5580452661fSBarry Smith       xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi);
559416022c9SBarry Smith       PetscMemcpy(xi,ajfill,(ainew[prow]+shift)*sizeof(int));
5600452661fSBarry Smith       PetscFree(ajfill);
56156beaf04SBarry Smith       ajfill = xi;
562bbb6d6a8SBarry Smith       realloc++;
5639e25ed09SBarry Smith     }
564dbb450caSBarry Smith     xi          = ajnew + ainew[prow] + shift;
565dbb450caSBarry Smith     flev        = ajfill + ainew[prow] + shift;
56656beaf04SBarry Smith     dloc[prow]  = nzi;
5679e25ed09SBarry Smith     fm          = fill[n];
56856beaf04SBarry Smith     while (nzf--) {
569dbb450caSBarry Smith       *xi++   = fm - shift;
57056beaf04SBarry Smith       *flev++ = im[fm];
5719e25ed09SBarry Smith       fm      = fill[fm];
5729e25ed09SBarry Smith     }
5739e25ed09SBarry Smith   }
5740452661fSBarry Smith   PetscFree(ajfill);
575898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr);
576898183ecSLois Curfman McInnes   ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr);
577898183ecSLois Curfman McInnes   ierr = ISDestroy(isicol); CHKERRQ(ierr);
5780452661fSBarry Smith   PetscFree(fill); PetscFree(im);
5799e25ed09SBarry Smith 
580416022c9SBarry Smith   PLogInfo((PetscObject)A,
581ec8511deSBarry Smith     "Info:MatILUFactorSymbolic_SeqAIJ:Realloc %d Fill ratio:given %g needed %g\n",
58256beaf04SBarry Smith                              realloc,f,((double)ainew[n])/((double)ai[prow]));
583bbb6d6a8SBarry Smith 
5849e25ed09SBarry Smith   /* put together the new matrix */
585b4fd4287SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,fact); CHKERRQ(ierr);
586416022c9SBarry Smith   b = (Mat_SeqAIJ *) (*fact)->data;
5870452661fSBarry Smith   PetscFree(b->imax);
588416022c9SBarry Smith   b->singlemalloc = 0;
589dbb450caSBarry Smith   len = (ainew[n] + shift)*sizeof(Scalar);
5909e25ed09SBarry Smith   /* the next line frees the default space generated by the Create() */
5910452661fSBarry Smith   PetscFree(b->a); PetscFree(b->ilen);
5920452661fSBarry Smith   b->a          = (Scalar *) PetscMalloc( len ); CHKPTRQ(b->a);
593416022c9SBarry Smith   b->j          = ajnew;
594416022c9SBarry Smith   b->i          = ainew;
59556beaf04SBarry Smith   for ( i=0; i<n; i++ ) dloc[i] += ainew[i];
596416022c9SBarry Smith   b->diag       = dloc;
597416022c9SBarry Smith   b->ilen       = 0;
598416022c9SBarry Smith   b->imax       = 0;
599416022c9SBarry Smith   b->row        = isrow;
600416022c9SBarry Smith   b->col        = iscol;
6010452661fSBarry Smith   b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar));
602416022c9SBarry Smith   CHKPTRQ(b->solve_work);
603416022c9SBarry Smith   /* In b structure:  Free imax, ilen, old a, old j.
60456beaf04SBarry Smith      Allocate dloc, solve_work, new a, new j */
605dbb450caSBarry Smith   PLogObjectMemory(*fact,(ainew[n]+shift-n) * (sizeof(int)+sizeof(Scalar)));
606416022c9SBarry Smith   b->maxnz          = b->nz = ainew[n] + shift;
6079e25ed09SBarry Smith   (*fact)->factor   = FACTOR_LU;
6089e25ed09SBarry Smith   return 0;
6099e25ed09SBarry Smith }
610d5d45c9bSBarry Smith 
611d5d45c9bSBarry Smith 
612d5d45c9bSBarry Smith 
613d5d45c9bSBarry Smith 
614