1cb512458SBarry Smith #ifndef lint 2*08480c60SBarry Smith static char vcid[] = "$Id: aijfact.c,v 1.42 1995/10/12 15:01:41 bsmith Exp bsmith $"; 3cb512458SBarry Smith #endif 4289bc588SBarry Smith 5289bc588SBarry Smith 6289bc588SBarry Smith #include "aij.h" 78c37ef55SBarry Smith #include "inline/spops.h" 8289bc588SBarry Smith /* 9289bc588SBarry Smith Factorization code for AIJ format. 10289bc588SBarry Smith */ 11289bc588SBarry Smith 12416022c9SBarry Smith int MatLUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,Mat *B) 13289bc588SBarry Smith { 14416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b; 15289bc588SBarry Smith IS isicol; 16416022c9SBarry Smith int *r,*ic, ierr, i, n = a->m, *ai = a->i, *aj = a->j; 17416022c9SBarry Smith int *ainew,*ajnew, jmax,*fill, *ajtmp, nz,shift = a->indexshift; 182fb3ed76SBarry Smith int *idnew, idx, row,m,fm, nnz, nzi,len, realloc = 0,nzbd,*im; 19289bc588SBarry Smith 20416022c9SBarry Smith if (n != a->n) SETERRQ(1,"MatLUFactorSymbolic_SeqAIJ:Must be square"); 21416022c9SBarry Smith if (!isrow) SETERRQ(1,"MatLUFactorSymbolic_SeqAIJ:Must have row permutation"); 22416022c9SBarry Smith if (!iscol) SETERRQ(1,"MatLUFactorSymbolic_SeqAIJ:Must have column permutation"); 23289bc588SBarry Smith 2478b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr); 25289bc588SBarry Smith ISGetIndices(isrow,&r); ISGetIndices(isicol,&ic); 26289bc588SBarry Smith 27289bc588SBarry Smith /* get new row pointers */ 2878b31e54SBarry Smith ainew = (int *) PETSCMALLOC( (n+1)*sizeof(int) ); CHKPTRQ(ainew); 29dbb450caSBarry Smith ainew[0] = -shift; 30289bc588SBarry Smith /* don't know how many column pointers are needed so estimate */ 31dbb450caSBarry Smith jmax = (int) (f*ai[n]+(!shift)); 3278b31e54SBarry Smith ajnew = (int *) PETSCMALLOC( (jmax)*sizeof(int) ); CHKPTRQ(ajnew); 33289bc588SBarry Smith /* fill is a linked list of nonzeros in active row */ 342fb3ed76SBarry Smith fill = (int *) PETSCMALLOC( (2*n+1)*sizeof(int)); CHKPTRQ(fill); 352fb3ed76SBarry Smith im = fill + n + 1; 36289bc588SBarry Smith /* idnew is location of diagonal in factor */ 3778b31e54SBarry Smith idnew = (int *) PETSCMALLOC( (n+1)*sizeof(int)); CHKPTRQ(idnew); 38dbb450caSBarry Smith idnew[0] = -shift; 39289bc588SBarry Smith 40289bc588SBarry Smith for ( i=0; i<n; i++ ) { 41289bc588SBarry Smith /* first copy previous fill into linked list */ 42289bc588SBarry Smith nnz = nz = ai[r[i]+1] - ai[r[i]]; 43dbb450caSBarry Smith ajtmp = aj + ai[r[i]] + shift; 44289bc588SBarry Smith fill[n] = n; 45289bc588SBarry Smith while (nz--) { 46289bc588SBarry Smith fm = n; 47dbb450caSBarry Smith idx = ic[*ajtmp++ + shift]; 48289bc588SBarry Smith do { 49289bc588SBarry Smith m = fm; 50289bc588SBarry Smith fm = fill[m]; 51289bc588SBarry Smith } while (fm < idx); 52289bc588SBarry Smith fill[m] = idx; 53289bc588SBarry Smith fill[idx] = fm; 54289bc588SBarry Smith } 55289bc588SBarry Smith row = fill[n]; 56289bc588SBarry Smith while ( row < i ) { 57dbb450caSBarry Smith ajtmp = ajnew + idnew[row] + (!shift); 582fb3ed76SBarry Smith nzbd = 1 + idnew[row] - ainew[row]; 592fb3ed76SBarry Smith nz = im[row] - nzbd; 60289bc588SBarry Smith fm = row; 612fb3ed76SBarry Smith while (nz-- > 0) { 62dbb450caSBarry Smith idx = *ajtmp++ + shift; 632fb3ed76SBarry Smith nzbd++; 642fb3ed76SBarry Smith if (idx == i) im[row] = nzbd; 65289bc588SBarry Smith do { 66289bc588SBarry Smith m = fm; 67289bc588SBarry Smith fm = fill[m]; 68289bc588SBarry Smith } while (fm < idx); 69289bc588SBarry Smith if (fm != idx) { 70289bc588SBarry Smith fill[m] = idx; 71289bc588SBarry Smith fill[idx] = fm; 72289bc588SBarry Smith fm = idx; 73289bc588SBarry Smith nnz++; 74289bc588SBarry Smith } 75289bc588SBarry Smith } 76289bc588SBarry Smith row = fill[row]; 77289bc588SBarry Smith } 78289bc588SBarry Smith /* copy new filled row into permanent storage */ 79289bc588SBarry Smith ainew[i+1] = ainew[i] + nnz; 80289bc588SBarry Smith if (ainew[i+1] > jmax+1) { 81289bc588SBarry Smith /* allocate a longer ajnew */ 822fb3ed76SBarry Smith int maxadd; 83dbb450caSBarry Smith maxadd = (int) ((f*(ai[n]+(!shift))*(n-i+5))/n); 84bbb6d6a8SBarry Smith if (maxadd < nnz) maxadd = (n-i)*(nnz+1); 852fb3ed76SBarry Smith jmax += maxadd; 8678b31e54SBarry Smith ajtmp = (int *) PETSCMALLOC( jmax*sizeof(int) );CHKPTRQ(ajtmp); 87416022c9SBarry Smith PetscMemcpy(ajtmp,ajnew,(ainew[i]+shift)*sizeof(int)); 8878b31e54SBarry Smith PETSCFREE(ajnew); 89289bc588SBarry Smith ajnew = ajtmp; 902fb3ed76SBarry Smith realloc++; /* count how many times we realloc */ 91289bc588SBarry Smith } 92dbb450caSBarry Smith ajtmp = ajnew + ainew[i] + shift; 93289bc588SBarry Smith fm = fill[n]; 94289bc588SBarry Smith nzi = 0; 952fb3ed76SBarry Smith im[i] = nnz; 96289bc588SBarry Smith while (nnz--) { 97289bc588SBarry Smith if (fm < i) nzi++; 98dbb450caSBarry Smith *ajtmp++ = fm - shift; 99289bc588SBarry Smith fm = fill[fm]; 100289bc588SBarry Smith } 101289bc588SBarry Smith idnew[i] = ainew[i] + nzi; 102289bc588SBarry Smith } 103289bc588SBarry Smith 104416022c9SBarry Smith PLogInfo((PetscObject)A, 105ec8511deSBarry Smith "Info:MatLUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n", 106bbb6d6a8SBarry Smith realloc,f,((double)ainew[n])/((double)ai[i])); 1072fb3ed76SBarry Smith 108898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 109898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr); 1101987afe7SBarry Smith 111898183ecSLois Curfman McInnes PETSCFREE(fill); 112289bc588SBarry Smith 113289bc588SBarry Smith /* put together the new matrix */ 114416022c9SBarry Smith ierr = MatCreateSeqAIJ(A->comm,n, n, 0, 0, B); CHKERRQ(ierr); 1151987afe7SBarry Smith PLogObjectParent(*B,isicol); 1161987afe7SBarry Smith ierr = ISDestroy(isicol); CHKERRQ(ierr); 117416022c9SBarry Smith b = (Mat_SeqAIJ *) (*B)->data; 118416022c9SBarry Smith PETSCFREE(b->imax); 119416022c9SBarry Smith b->singlemalloc = 0; 120dbb450caSBarry Smith len = (ainew[n] + shift)*sizeof(Scalar); 121e8d4e0b9SBarry Smith /* the next line frees the default space generated by the Create() */ 122416022c9SBarry Smith PETSCFREE(b->a); PETSCFREE(b->ilen); 123416022c9SBarry Smith b->a = (Scalar *) PETSCMALLOC( len ); CHKPTRQ(b->a); 124416022c9SBarry Smith b->j = ajnew; 125416022c9SBarry Smith b->i = ainew; 126416022c9SBarry Smith b->diag = idnew; 127416022c9SBarry Smith b->ilen = 0; 128416022c9SBarry Smith b->imax = 0; 129416022c9SBarry Smith b->row = isrow; 130416022c9SBarry Smith b->col = iscol; 131416022c9SBarry Smith b->solve_work = (Scalar *) PETSCMALLOC( n*sizeof(Scalar)); 132416022c9SBarry Smith CHKPTRQ(b->solve_work); 133416022c9SBarry Smith /* In b structure: Free imax, ilen, old a, old j. 1347fd98bd6SLois Curfman McInnes Allocate idnew, solve_work, new a, new j */ 135416022c9SBarry Smith PLogObjectMemory(*B,(ainew[n]+shift-n)*(sizeof(int)+sizeof(Scalar))); 136416022c9SBarry Smith b->maxnz = b->nz = ainew[n] + shift; 1377fd98bd6SLois Curfman McInnes 138289bc588SBarry Smith return 0; 139289bc588SBarry Smith } 1400a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 141416022c9SBarry Smith int MatLUFactorNumeric_SeqAIJ(Mat A,Mat *B) 142289bc588SBarry Smith { 143416022c9SBarry Smith Mat C = *B; 144416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b = (Mat_SeqAIJ *)C->data; 145416022c9SBarry Smith IS iscol = b->col, isrow = b->row, isicol; 146416022c9SBarry Smith int *r,*ic, ierr, i, j, n = a->m, *ai = b->i, *aj = b->j; 1471987afe7SBarry Smith int *ajtmpold, *ajtmp, nz, row, *ics, shift = a->indexshift; 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 */ 1541987afe7SBarry Smith int *diag_offset = b->diag; 155390f2bbaSBarry Smith register Scalar *pv, *rtmps; 1561987afe7SBarry Smith register int *pj; 157289bc588SBarry Smith 15878b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr); 159416022c9SBarry Smith PLogObjectParent(*B,isicol); 16078b31e54SBarry Smith ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr); 16178b31e54SBarry Smith ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr); 16278b31e54SBarry Smith rtmp = (Scalar *) PETSCMALLOC( (n+1)*sizeof(Scalar) ); CHKPTRQ(rtmp); 1630cf60383SBarry Smith rtmps = rtmp + shift; ics = ic + shift; 164289bc588SBarry Smith 165289bc588SBarry Smith for ( i=0; i<n; i++ ) { 166289bc588SBarry Smith nz = ai[i+1] - ai[i]; 167dbb450caSBarry Smith ajtmp = aj + ai[i] + shift; 1680cf60383SBarry Smith for ( j=0; j<nz; j++ ) rtmps[ajtmp[j]] = 0.0; 169289bc588SBarry Smith 170289bc588SBarry Smith /* load in initial (unfactored row) */ 171416022c9SBarry Smith nz = a->i[r[i]+1] - a->i[r[i]]; 172416022c9SBarry Smith ajtmpold = a->j + a->i[r[i]] + shift; 173416022c9SBarry Smith v = a->a + a->i[r[i]] + shift; 1740cf60383SBarry Smith for ( j=0; j<nz; j++ ) rtmp[ics[ajtmpold[j]]] = v[j]; 175289bc588SBarry Smith 176dbb450caSBarry Smith row = *ajtmp++ + shift; 177289bc588SBarry Smith while (row < i) { 1788c37ef55SBarry Smith pc = rtmp + row; 1798c37ef55SBarry Smith if (*pc != 0.0) { 1801987afe7SBarry Smith pv = b->a + diag_offset[row] + shift; 1811987afe7SBarry Smith pj = b->j + diag_offset[row] + (!shift); 1828c37ef55SBarry Smith multiplier = *pc * *pv++; 1838c37ef55SBarry Smith *pc = multiplier; 1841987afe7SBarry Smith nz = ai[row+1] - diag_offset[row] - 1; 185eef2e97bSBarry Smith PLogFlops(2*nz); 1861987afe7SBarry Smith /* The for-loop form can aid the compiler in overlapping 1871987afe7SBarry Smith loads and stores */ 1881987afe7SBarry Smith /*while (nz-->0) rtmps[*pj++] -= multiplier* *pv++; */ 1891987afe7SBarry Smith {int __i; 1901987afe7SBarry Smith for (__i=0; __i<nz; __i++) rtmps[pj[__i]] -= multiplier * pv[__i]; 1911987afe7SBarry Smith } 192289bc588SBarry Smith } 193dbb450caSBarry Smith row = *ajtmp++ + shift; 194289bc588SBarry Smith } 195416022c9SBarry Smith /* finished row so stick it into b->a */ 196416022c9SBarry Smith pv = b->a + ai[i] + shift; 197416022c9SBarry Smith pj = b->j + ai[i] + shift; 1988c37ef55SBarry Smith nz = ai[i+1] - ai[i]; 199ec8511deSBarry Smith if (rtmp[i] == 0.0) {SETERRQ(1,"MatLUFactorNumeric_SeqAIJ:Zero pivot");} 2008c37ef55SBarry Smith rtmp[i] = 1.0/rtmp[i]; 201416022c9SBarry Smith for ( j=0; j<nz; j++ ) {pv[j] = rtmps[pj[j]];} 2028c37ef55SBarry Smith } 20378b31e54SBarry Smith PETSCFREE(rtmp); 20478b31e54SBarry Smith ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr); 20578b31e54SBarry Smith ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 20678b31e54SBarry Smith ierr = ISDestroy(isicol); CHKERRQ(ierr); 207416022c9SBarry Smith C->factor = FACTOR_LU; 208416022c9SBarry Smith b->assembled = 1; 209416022c9SBarry Smith PLogFlops(b->n); 210289bc588SBarry Smith return 0; 211289bc588SBarry Smith } 2120a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 213416022c9SBarry Smith int MatLUFactor_SeqAIJ(Mat A,IS row,IS col,double f) 214da3a660dSBarry Smith { 215416022c9SBarry Smith Mat_SeqAIJ *mat = (Mat_SeqAIJ *) A->data; 2166abc6512SBarry Smith int ierr; 217416022c9SBarry Smith Mat C; 218416022c9SBarry Smith 219416022c9SBarry Smith ierr = MatLUFactorSymbolic_SeqAIJ(A,row,col,f,&C); CHKERRQ(ierr); 220416022c9SBarry Smith ierr = MatLUFactorNumeric_SeqAIJ(A,&C); CHKERRQ(ierr); 221da3a660dSBarry Smith 222da3a660dSBarry Smith /* free all the data structures from mat */ 22378b31e54SBarry Smith PETSCFREE(mat->a); 22478b31e54SBarry Smith if (!mat->singlemalloc) {PETSCFREE(mat->i); PETSCFREE(mat->j);} 22578b31e54SBarry Smith if (mat->diag) PETSCFREE(mat->diag); 22678b31e54SBarry Smith if (mat->ilen) PETSCFREE(mat->ilen); 22778b31e54SBarry Smith if (mat->imax) PETSCFREE(mat->imax); 22894473badSLois Curfman McInnes if (mat->solve_work) PETSCFREE(mat->solve_work); 22978b31e54SBarry Smith PETSCFREE(mat); 230da3a660dSBarry Smith 231416022c9SBarry Smith PetscMemcpy(A,C,sizeof(struct _Mat)); 232416022c9SBarry Smith PETSCHEADERDESTROY(C); 233da3a660dSBarry Smith return 0; 234da3a660dSBarry Smith } 2350a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 236416022c9SBarry Smith int MatSolve_SeqAIJ(Mat A,Vec bb, Vec xx) 2378c37ef55SBarry Smith { 238416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 239416022c9SBarry Smith IS iscol = a->col, isrow = a->row; 240416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 241416022c9SBarry Smith int nz,shift = a->indexshift; 242416022c9SBarry Smith Scalar *x,*b,*tmp, *tmps, *aa = a->a, sum, *v; 2438c37ef55SBarry Smith 244416022c9SBarry Smith if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolve_SeqAIJ:Not for unfactored matrix"); 2459e25ed09SBarry Smith 24678b31e54SBarry Smith ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 24778b31e54SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 248416022c9SBarry Smith tmp = a->solve_work; 2498c37ef55SBarry Smith 25078b31e54SBarry Smith ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 25178b31e54SBarry Smith ierr = ISGetIndices(iscol,&c);CHKERRQ(ierr); c = c + (n-1); 2528c37ef55SBarry Smith 2538c37ef55SBarry Smith /* forward solve the lower triangular */ 2548c37ef55SBarry Smith tmp[0] = b[*r++]; 2550cf60383SBarry Smith tmps = tmp + shift; 2568c37ef55SBarry Smith for ( i=1; i<n; i++ ) { 257dbb450caSBarry Smith v = aa + ai[i] + shift; 258dbb450caSBarry Smith vi = aj + ai[i] + shift; 259416022c9SBarry Smith nz = a->diag[i] - ai[i]; 2608c37ef55SBarry Smith sum = b[*r++]; 2610cf60383SBarry Smith while (nz--) sum -= *v++ * tmps[*vi++]; 2628c37ef55SBarry Smith tmp[i] = sum; 2638c37ef55SBarry Smith } 2648c37ef55SBarry Smith 2658c37ef55SBarry Smith /* backward solve the upper triangular */ 2668c37ef55SBarry Smith for ( i=n-1; i>=0; i-- ){ 267416022c9SBarry Smith v = aa + a->diag[i] + (!shift); 268416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 269416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 2708c37ef55SBarry Smith sum = tmp[i]; 2710cf60383SBarry Smith while (nz--) sum -= *v++ * tmps[*vi++]; 272416022c9SBarry Smith x[*c--] = tmp[i] = sum*aa[a->diag[i]+shift]; 2738c37ef55SBarry Smith } 2748c37ef55SBarry Smith 275898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 276898183ecSLois Curfman McInnes ierr = ISRestoreIndices(iscol,&c); CHKERRQ(ierr); 277416022c9SBarry Smith PLogFlops(2*a->nz - a->n); 2788c37ef55SBarry Smith return 0; 2798c37ef55SBarry Smith } 280416022c9SBarry Smith int MatSolveAdd_SeqAIJ(Mat A,Vec bb, Vec yy, Vec xx) 281da3a660dSBarry Smith { 282416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 283416022c9SBarry Smith IS iscol = a->col, isrow = a->row; 284416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 285416022c9SBarry Smith int nz, shift = a->indexshift; 286416022c9SBarry Smith Scalar *x,*b,*tmp, *aa = a->a, sum, *v; 287da3a660dSBarry Smith 288416022c9SBarry Smith if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolveAdd_SeqAIJ:Not for unfactored matrix"); 28978b31e54SBarry Smith if (yy != xx) {ierr = VecCopy(yy,xx); CHKERRQ(ierr);} 290da3a660dSBarry Smith 29178b31e54SBarry Smith ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 29278b31e54SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 293416022c9SBarry Smith tmp = a->solve_work; 294da3a660dSBarry Smith 29578b31e54SBarry Smith ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr); 29678b31e54SBarry Smith ierr = ISGetIndices(iscol,&c); CHKERRQ(ierr); c = c + (n-1); 297da3a660dSBarry Smith 298da3a660dSBarry Smith /* forward solve the lower triangular */ 299da3a660dSBarry Smith tmp[0] = b[*r++]; 300da3a660dSBarry Smith for ( i=1; i<n; i++ ) { 301dbb450caSBarry Smith v = aa + ai[i] + shift; 302dbb450caSBarry Smith vi = aj + ai[i] + shift; 303416022c9SBarry Smith nz = a->diag[i] - ai[i]; 304da3a660dSBarry Smith sum = b[*r++]; 305dbb450caSBarry Smith while (nz--) sum -= *v++ * tmp[*vi++ + shift]; 306da3a660dSBarry Smith tmp[i] = sum; 307da3a660dSBarry Smith } 308da3a660dSBarry Smith 309da3a660dSBarry Smith /* backward solve the upper triangular */ 310da3a660dSBarry Smith for ( i=n-1; i>=0; i-- ){ 311416022c9SBarry Smith v = aa + a->diag[i] + (!shift); 312416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 313416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 314da3a660dSBarry Smith sum = tmp[i]; 315dbb450caSBarry Smith while (nz--) sum -= *v++ * tmp[*vi++ + shift]; 316416022c9SBarry Smith tmp[i] = sum*aa[a->diag[i]+shift]; 317da3a660dSBarry Smith x[*c--] += tmp[i]; 318da3a660dSBarry Smith } 319da3a660dSBarry Smith 320898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 321898183ecSLois Curfman McInnes ierr = ISRestoreIndices(iscol,&c); CHKERRQ(ierr); 322416022c9SBarry Smith PLogFlops(2*a->nz); 323898183ecSLois Curfman McInnes 324da3a660dSBarry Smith return 0; 325da3a660dSBarry Smith } 326da3a660dSBarry Smith /* -------------------------------------------------------------------*/ 327416022c9SBarry Smith int MatSolveTrans_SeqAIJ(Mat A,Vec bb, Vec xx) 328da3a660dSBarry Smith { 329416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 330416022c9SBarry Smith IS iscol = a->col, isrow = a->row, invisrow,inviscol; 331416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 332416022c9SBarry Smith int nz,shift = a->indexshift; 333416022c9SBarry Smith Scalar *x,*b,*tmp, *aa = a->a, *v; 334da3a660dSBarry Smith 335416022c9SBarry Smith if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolveTrans_SeqAIJ:Not unfactored matrix"); 33678b31e54SBarry Smith ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 33778b31e54SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 338416022c9SBarry Smith tmp = a->solve_work; 339da3a660dSBarry Smith 340da3a660dSBarry Smith /* invert the permutations */ 34178b31e54SBarry Smith ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr); 34278b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr); 343da3a660dSBarry Smith 34478b31e54SBarry Smith ierr = ISGetIndices(invisrow,&r); CHKERRQ(ierr); 34578b31e54SBarry Smith ierr = ISGetIndices(inviscol,&c); CHKERRQ(ierr); 346da3a660dSBarry Smith 347da3a660dSBarry Smith /* copy the b into temp work space according to permutation */ 348da3a660dSBarry Smith for ( i=0; i<n; i++ ) tmp[c[i]] = b[i]; 349da3a660dSBarry Smith 350da3a660dSBarry Smith /* forward solve the U^T */ 351da3a660dSBarry Smith for ( i=0; i<n; i++ ) { 352416022c9SBarry Smith v = aa + a->diag[i] + shift; 353416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 354416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 355da3a660dSBarry Smith tmp[i] *= *v++; 356da3a660dSBarry Smith while (nz--) { 357dbb450caSBarry Smith tmp[*vi++ + shift] -= (*v++)*tmp[i]; 358da3a660dSBarry Smith } 359da3a660dSBarry Smith } 360da3a660dSBarry Smith 361da3a660dSBarry Smith /* backward solve the L^T */ 362da3a660dSBarry Smith for ( i=n-1; i>=0; i-- ){ 363416022c9SBarry Smith v = aa + a->diag[i] - 1 + shift; 364416022c9SBarry Smith vi = aj + a->diag[i] - 1 + shift; 365416022c9SBarry Smith nz = a->diag[i] - ai[i]; 366da3a660dSBarry Smith while (nz--) { 367dbb450caSBarry Smith tmp[*vi-- + shift] -= (*v--)*tmp[i]; 368da3a660dSBarry Smith } 369da3a660dSBarry Smith } 370da3a660dSBarry Smith 371da3a660dSBarry Smith /* copy tmp into x according to permutation */ 372da3a660dSBarry Smith for ( i=0; i<n; i++ ) x[r[i]] = tmp[i]; 373da3a660dSBarry Smith 374898183ecSLois Curfman McInnes ierr = ISRestoreIndices(invisrow,&r); CHKERRQ(ierr); 375898183ecSLois Curfman McInnes ierr = ISRestoreIndices(inviscol,&c); CHKERRQ(ierr); 376898183ecSLois Curfman McInnes ierr = ISDestroy(invisrow); CHKERRQ(ierr); 377898183ecSLois Curfman McInnes ierr = ISDestroy(inviscol); CHKERRQ(ierr); 378da3a660dSBarry Smith 379416022c9SBarry Smith PLogFlops(2*a->nz-a->n); 380da3a660dSBarry Smith return 0; 381da3a660dSBarry Smith } 382da3a660dSBarry Smith 383416022c9SBarry Smith int MatSolveTransAdd_SeqAIJ(Mat A,Vec bb, Vec zz,Vec xx) 384da3a660dSBarry Smith { 385416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 386416022c9SBarry Smith IS iscol = a->col, isrow = a->row, invisrow,inviscol; 387416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 388416022c9SBarry Smith int nz,shift = a->indexshift; 389416022c9SBarry Smith Scalar *x,*b,*tmp, *aa = a->a, *v; 3906abc6512SBarry Smith 391416022c9SBarry Smith if (A->factor != FACTOR_LU)SETERRQ(1,"MatSolveTransAdd_SeqAIJ:Not unfactored matrix"); 3926abc6512SBarry Smith if (zz != xx) VecCopy(zz,xx); 3936abc6512SBarry Smith 39478b31e54SBarry Smith ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 39578b31e54SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 396416022c9SBarry Smith tmp = a->solve_work; 3976abc6512SBarry Smith 3986abc6512SBarry Smith /* invert the permutations */ 39978b31e54SBarry Smith ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr); 40078b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr); 40178b31e54SBarry Smith ierr = ISGetIndices(invisrow,&r); CHKERRQ(ierr); 40278b31e54SBarry Smith ierr = ISGetIndices(inviscol,&c); CHKERRQ(ierr); 4036abc6512SBarry Smith 4046abc6512SBarry Smith /* copy the b into temp work space according to permutation */ 4056abc6512SBarry Smith for ( i=0; i<n; i++ ) tmp[c[i]] = b[i]; 4066abc6512SBarry Smith 4076abc6512SBarry Smith /* forward solve the U^T */ 4086abc6512SBarry Smith for ( i=0; i<n; i++ ) { 409416022c9SBarry Smith v = aa + a->diag[i] + shift; 410416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 411416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 4126abc6512SBarry Smith tmp[i] *= *v++; 4136abc6512SBarry Smith while (nz--) { 414dbb450caSBarry Smith tmp[*vi++ + shift] -= (*v++)*tmp[i]; 4156abc6512SBarry Smith } 4166abc6512SBarry Smith } 4176abc6512SBarry Smith 4186abc6512SBarry Smith /* backward solve the L^T */ 4196abc6512SBarry Smith for ( i=n-1; i>=0; i-- ){ 420416022c9SBarry Smith v = aa + a->diag[i] - 1 + shift; 421416022c9SBarry Smith vi = aj + a->diag[i] - 1 + shift; 422416022c9SBarry Smith nz = a->diag[i] - ai[i]; 4236abc6512SBarry Smith while (nz--) { 424dbb450caSBarry Smith tmp[*vi-- + shift] -= (*v--)*tmp[i]; 4256abc6512SBarry Smith } 4266abc6512SBarry Smith } 4276abc6512SBarry Smith 4286abc6512SBarry Smith /* copy tmp into x according to permutation */ 4296abc6512SBarry Smith for ( i=0; i<n; i++ ) x[r[i]] += tmp[i]; 4306abc6512SBarry Smith 431898183ecSLois Curfman McInnes ierr = ISRestoreIndices(invisrow,&r); CHKERRQ(ierr); 432898183ecSLois Curfman McInnes ierr = ISRestoreIndices(inviscol,&c); CHKERRQ(ierr); 433898183ecSLois Curfman McInnes ierr = ISDestroy(invisrow); CHKERRQ(ierr); 434898183ecSLois Curfman McInnes ierr = ISDestroy(inviscol); CHKERRQ(ierr); 4356abc6512SBarry Smith 436416022c9SBarry Smith PLogFlops(2*a->nz); 4376abc6512SBarry Smith return 0; 438da3a660dSBarry Smith } 4399e25ed09SBarry Smith /* ----------------------------------------------------------------*/ 440*08480c60SBarry Smith 441*08480c60SBarry Smith int MatILUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,int levels,Mat *fact) 4429e25ed09SBarry Smith { 443416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b; 4449e25ed09SBarry Smith IS isicol; 445416022c9SBarry Smith int *r,*ic, ierr, prow, n = a->m, *ai = a->i, *aj = a->j; 44656beaf04SBarry Smith int *ainew,*ajnew, jmax,*fill, *xi, nz, *im,*ajfill,*flev; 44756beaf04SBarry Smith int *dloc, idx, row,m,fm, nzf, nzi,len, realloc = 0; 448416022c9SBarry Smith int incrlev,nnz,i,shift = a->indexshift; 4499e25ed09SBarry Smith 450416022c9SBarry Smith if (n != a->n) SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:Matrix must be square"); 451416022c9SBarry Smith if (!isrow) SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:Must have row permutation"); 452416022c9SBarry Smith if (!iscol) SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:Must have column permutation"); 4539e25ed09SBarry Smith 454*08480c60SBarry Smith /* special case that simply copies fill pattern */ 455*08480c60SBarry Smith if (levels == 0 && ISIsIdentity(isrow) && ISIsIdentity(iscol)) { 456*08480c60SBarry Smith ierr = MatCopyPrivate_SeqAIJ(A,fact,DO_NOT_COPY_VALUES); CHKERRQ(ierr); 457*08480c60SBarry Smith (*fact)->factor = FACTOR_LU; 458*08480c60SBarry Smith b = (Mat_SeqAIJ *) (*fact)->data; 459*08480c60SBarry Smith if (!b->diag) { 460*08480c60SBarry Smith ierr = MatMarkDiag_SeqAIJ(*fact); CHKERRQ(ierr); 461*08480c60SBarry Smith } 462*08480c60SBarry Smith b->row = isrow; 463*08480c60SBarry Smith b->col = iscol; 464*08480c60SBarry Smith b->solve_work = (Scalar *) PETSCMALLOC((b->m+1)*sizeof(Scalar));CHKPTRQ(b->solve_work); 465*08480c60SBarry Smith return 0; 466*08480c60SBarry Smith } 467*08480c60SBarry Smith 46878b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr); 469898183ecSLois Curfman McInnes ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr); 470898183ecSLois Curfman McInnes ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr); 4719e25ed09SBarry Smith 4729e25ed09SBarry Smith /* get new row pointers */ 47378b31e54SBarry Smith ainew = (int *) PETSCMALLOC( (n+1)*sizeof(int) ); CHKPTRQ(ainew); 474dbb450caSBarry Smith ainew[0] = -shift; 4759e25ed09SBarry Smith /* don't know how many column pointers are needed so estimate */ 476dbb450caSBarry Smith jmax = (int) (f*(ai[n]+!shift)); 47778b31e54SBarry Smith ajnew = (int *) PETSCMALLOC( (jmax)*sizeof(int) ); CHKPTRQ(ajnew); 4789e25ed09SBarry Smith /* ajfill is level of fill for each fill entry */ 47978b31e54SBarry Smith ajfill = (int *) PETSCMALLOC( (jmax)*sizeof(int) ); CHKPTRQ(ajfill); 4809e25ed09SBarry Smith /* fill is a linked list of nonzeros in active row */ 48156beaf04SBarry Smith fill = (int *) PETSCMALLOC( (n+1)*sizeof(int)); CHKPTRQ(fill); 48256beaf04SBarry Smith /* im is level for each filled value */ 48356beaf04SBarry Smith im = (int *) PETSCMALLOC( (n+1)*sizeof(int)); CHKPTRQ(im); 48456beaf04SBarry Smith /* dloc is location of diagonal in factor */ 48556beaf04SBarry Smith dloc = (int *) PETSCMALLOC( (n+1)*sizeof(int)); CHKPTRQ(dloc); 48656beaf04SBarry Smith dloc[0] = 0; 4879e25ed09SBarry Smith 48856beaf04SBarry Smith for ( prow=0; prow<n; prow++ ) { 4899e25ed09SBarry Smith /* first copy previous fill into linked list */ 49056beaf04SBarry Smith nzf = nz = ai[r[prow]+1] - ai[r[prow]]; 491dbb450caSBarry Smith xi = aj + ai[r[prow]] + shift; 4929e25ed09SBarry Smith fill[n] = n; 4939e25ed09SBarry Smith while (nz--) { 4949e25ed09SBarry Smith fm = n; 495dbb450caSBarry Smith idx = ic[*xi++ + shift]; 4969e25ed09SBarry Smith do { 4979e25ed09SBarry Smith m = fm; 4989e25ed09SBarry Smith fm = fill[m]; 4999e25ed09SBarry Smith } while (fm < idx); 5009e25ed09SBarry Smith fill[m] = idx; 5019e25ed09SBarry Smith fill[idx] = fm; 50256beaf04SBarry Smith im[idx] = 0; 5039e25ed09SBarry Smith } 50456beaf04SBarry Smith nzi = 0; 5059e25ed09SBarry Smith row = fill[n]; 50656beaf04SBarry Smith while ( row < prow ) { 50756beaf04SBarry Smith incrlev = im[row] + 1; 50856beaf04SBarry Smith nz = dloc[row]; 509dbb450caSBarry Smith xi = ajnew + ainew[row] + shift + nz; 510dbb450caSBarry Smith flev = ajfill + ainew[row] + shift + nz + 1; 511416022c9SBarry Smith nnz = ainew[row+1] - ainew[row] - nz - 1; 512dbb450caSBarry Smith if (*xi++ + shift != row) { 513ec8511deSBarry Smith SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:zero pivot"); 51456beaf04SBarry Smith } 51556beaf04SBarry Smith fm = row; 51656beaf04SBarry Smith while (nnz-- > 0) { 517dbb450caSBarry Smith idx = *xi++ + shift; 51856beaf04SBarry Smith if (*flev + incrlev > levels) { 51956beaf04SBarry Smith flev++; 52056beaf04SBarry Smith continue; 52156beaf04SBarry Smith } 52256beaf04SBarry Smith do { 5239e25ed09SBarry Smith m = fm; 5249e25ed09SBarry Smith fm = fill[m]; 52556beaf04SBarry Smith } while (fm < idx); 5269e25ed09SBarry Smith if (fm != idx) { 52756beaf04SBarry Smith im[idx] = *flev + incrlev; 5289e25ed09SBarry Smith fill[m] = idx; 5299e25ed09SBarry Smith fill[idx] = fm; 5309e25ed09SBarry Smith fm = idx; 53156beaf04SBarry Smith nzf++; 5329e25ed09SBarry Smith } 53356beaf04SBarry Smith else { 53456beaf04SBarry Smith if (im[idx] > *flev + incrlev) im[idx] = *flev+incrlev; 5359e25ed09SBarry Smith } 53656beaf04SBarry Smith flev++; 5379e25ed09SBarry Smith } 5389e25ed09SBarry Smith row = fill[row]; 53956beaf04SBarry Smith nzi++; 5409e25ed09SBarry Smith } 5419e25ed09SBarry Smith /* copy new filled row into permanent storage */ 54256beaf04SBarry Smith ainew[prow+1] = ainew[prow] + nzf; 54356beaf04SBarry Smith if (ainew[prow+1] > jmax+1) { 5449e25ed09SBarry Smith /* allocate a longer ajnew */ 545bbb6d6a8SBarry Smith int maxadd; 546dbb450caSBarry Smith maxadd = (int) ((f*(ai[n]+!shift)*(n-prow+5))/n); 54756beaf04SBarry Smith if (maxadd < nzf) maxadd = (n-prow)*(nzf+1); 548bbb6d6a8SBarry Smith jmax += maxadd; 54956beaf04SBarry Smith xi = (int *) PETSCMALLOC( jmax*sizeof(int) );CHKPTRQ(xi); 550416022c9SBarry Smith PetscMemcpy(xi,ajnew,(ainew[prow]+shift)*sizeof(int)); 55178b31e54SBarry Smith PETSCFREE(ajnew); 55256beaf04SBarry Smith ajnew = xi; 5539e25ed09SBarry Smith /* allocate a longer ajfill */ 55456beaf04SBarry Smith xi = (int *) PETSCMALLOC( jmax*sizeof(int) );CHKPTRQ(xi); 555416022c9SBarry Smith PetscMemcpy(xi,ajfill,(ainew[prow]+shift)*sizeof(int)); 55678b31e54SBarry Smith PETSCFREE(ajfill); 55756beaf04SBarry Smith ajfill = xi; 558bbb6d6a8SBarry Smith realloc++; 5599e25ed09SBarry Smith } 560dbb450caSBarry Smith xi = ajnew + ainew[prow] + shift; 561dbb450caSBarry Smith flev = ajfill + ainew[prow] + shift; 56256beaf04SBarry Smith dloc[prow] = nzi; 5639e25ed09SBarry Smith fm = fill[n]; 56456beaf04SBarry Smith while (nzf--) { 565dbb450caSBarry Smith *xi++ = fm - shift; 56656beaf04SBarry Smith *flev++ = im[fm]; 5679e25ed09SBarry Smith fm = fill[fm]; 5689e25ed09SBarry Smith } 5699e25ed09SBarry Smith } 57078b31e54SBarry Smith PETSCFREE(ajfill); 571898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 572898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr); 573898183ecSLois Curfman McInnes ierr = ISDestroy(isicol); CHKERRQ(ierr); 574898183ecSLois Curfman McInnes PETSCFREE(fill); PETSCFREE(im); 5759e25ed09SBarry Smith 576416022c9SBarry Smith PLogInfo((PetscObject)A, 577ec8511deSBarry Smith "Info:MatILUFactorSymbolic_SeqAIJ:Realloc %d Fill ratio:given %g needed %g\n", 57856beaf04SBarry Smith realloc,f,((double)ainew[n])/((double)ai[prow])); 579bbb6d6a8SBarry Smith 5809e25ed09SBarry Smith /* put together the new matrix */ 581416022c9SBarry Smith ierr = MatCreateSeqAIJ(A->comm,n, n, 0, 0, fact); CHKERRQ(ierr); 582416022c9SBarry Smith b = (Mat_SeqAIJ *) (*fact)->data; 583416022c9SBarry Smith PETSCFREE(b->imax); 584416022c9SBarry Smith b->singlemalloc = 0; 585dbb450caSBarry Smith len = (ainew[n] + shift)*sizeof(Scalar); 5869e25ed09SBarry Smith /* the next line frees the default space generated by the Create() */ 587416022c9SBarry Smith PETSCFREE(b->a); PETSCFREE(b->ilen); 588416022c9SBarry Smith b->a = (Scalar *) PETSCMALLOC( len ); CHKPTRQ(b->a); 589416022c9SBarry Smith b->j = ajnew; 590416022c9SBarry Smith b->i = ainew; 59156beaf04SBarry Smith for ( i=0; i<n; i++ ) dloc[i] += ainew[i]; 592416022c9SBarry Smith b->diag = dloc; 593416022c9SBarry Smith b->ilen = 0; 594416022c9SBarry Smith b->imax = 0; 595416022c9SBarry Smith b->row = isrow; 596416022c9SBarry Smith b->col = iscol; 597416022c9SBarry Smith b->solve_work = (Scalar *) PETSCMALLOC( (n+1)*sizeof(Scalar)); 598416022c9SBarry Smith CHKPTRQ(b->solve_work); 599416022c9SBarry Smith /* In b structure: Free imax, ilen, old a, old j. 60056beaf04SBarry Smith Allocate dloc, solve_work, new a, new j */ 601dbb450caSBarry Smith PLogObjectMemory(*fact,(ainew[n]+shift-n) * (sizeof(int)+sizeof(Scalar))); 602416022c9SBarry Smith b->maxnz = b->nz = ainew[n] + shift; 6039e25ed09SBarry Smith (*fact)->factor = FACTOR_LU; 6049e25ed09SBarry Smith return 0; 6059e25ed09SBarry Smith } 606