1cb512458SBarry Smith #ifndef lint 2*d7e8b826SBarry Smith static char vcid[] = "$Id: aijfact.c,v 1.46 1995/11/08 00:10:48 balay 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 /* ----------------------------------------------------------- */ 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; 1690cf60383SBarry Smith for ( j=0; j<nz; j++ ) rtmps[ajtmp[j]] = 0.0; 170289bc588SBarry Smith 171289bc588SBarry Smith /* load in initial (unfactored row) */ 172416022c9SBarry Smith nz = a->i[r[i]+1] - a->i[r[i]]; 173416022c9SBarry Smith ajtmpold = a->j + a->i[r[i]] + shift; 174416022c9SBarry Smith v = a->a + a->i[r[i]] + shift; 1750cf60383SBarry Smith for ( j=0; j<nz; j++ ) rtmp[ics[ajtmpold[j]]] = v[j]; 176289bc588SBarry Smith 177dbb450caSBarry Smith row = *ajtmp++ + shift; 178289bc588SBarry Smith while (row < i) { 1798c37ef55SBarry Smith pc = rtmp + row; 1808c37ef55SBarry Smith if (*pc != 0.0) { 1811987afe7SBarry Smith pv = b->a + diag_offset[row] + shift; 1821987afe7SBarry Smith pj = b->j + diag_offset[row] + (!shift); 1838c37ef55SBarry Smith multiplier = *pc * *pv++; 1848c37ef55SBarry Smith *pc = multiplier; 1851987afe7SBarry Smith nz = ai[row+1] - diag_offset[row] - 1; 186eef2e97bSBarry Smith PLogFlops(2*nz); 1871987afe7SBarry Smith /* The for-loop form can aid the compiler in overlapping 1881987afe7SBarry Smith loads and stores */ 1891987afe7SBarry Smith /*while (nz-->0) rtmps[*pj++] -= multiplier* *pv++; */ 1901987afe7SBarry Smith {int __i; 1911987afe7SBarry Smith for (__i=0; __i<nz; __i++) rtmps[pj[__i]] -= multiplier * pv[__i]; 1921987afe7SBarry Smith } 193289bc588SBarry Smith } 194dbb450caSBarry Smith row = *ajtmp++ + shift; 195289bc588SBarry Smith } 196416022c9SBarry Smith /* finished row so stick it into b->a */ 197416022c9SBarry Smith pv = b->a + ai[i] + shift; 198416022c9SBarry Smith pj = b->j + ai[i] + shift; 1998c37ef55SBarry Smith nz = ai[i+1] - ai[i]; 200ec8511deSBarry Smith if (rtmp[i] == 0.0) {SETERRQ(1,"MatLUFactorNumeric_SeqAIJ:Zero pivot");} 2018c37ef55SBarry Smith rtmp[i] = 1.0/rtmp[i]; 202416022c9SBarry Smith for ( j=0; j<nz; j++ ) {pv[j] = rtmps[pj[j]];} 2038c37ef55SBarry Smith } 2040452661fSBarry Smith PetscFree(rtmp); 20578b31e54SBarry Smith ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr); 20678b31e54SBarry Smith ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 20778b31e54SBarry Smith ierr = ISDestroy(isicol); CHKERRQ(ierr); 208416022c9SBarry Smith C->factor = FACTOR_LU; 20941c01911SSatish Balay ierr = Mat_AIJ_CheckInode(C); CHKERRQ(ierr); 210416022c9SBarry Smith b->assembled = 1; 211416022c9SBarry Smith PLogFlops(b->n); 212289bc588SBarry Smith return 0; 213289bc588SBarry Smith } 2140a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 215416022c9SBarry Smith int MatLUFactor_SeqAIJ(Mat A,IS row,IS col,double f) 216da3a660dSBarry Smith { 217416022c9SBarry Smith Mat_SeqAIJ *mat = (Mat_SeqAIJ *) A->data; 2186abc6512SBarry Smith int ierr; 219416022c9SBarry Smith Mat C; 220416022c9SBarry Smith 221416022c9SBarry Smith ierr = MatLUFactorSymbolic_SeqAIJ(A,row,col,f,&C); CHKERRQ(ierr); 222416022c9SBarry Smith ierr = MatLUFactorNumeric_SeqAIJ(A,&C); CHKERRQ(ierr); 223da3a660dSBarry Smith 224da3a660dSBarry Smith /* free all the data structures from mat */ 2250452661fSBarry Smith PetscFree(mat->a); 2260452661fSBarry Smith if (!mat->singlemalloc) {PetscFree(mat->i); PetscFree(mat->j);} 2270452661fSBarry Smith if (mat->diag) PetscFree(mat->diag); 2280452661fSBarry Smith if (mat->ilen) PetscFree(mat->ilen); 2290452661fSBarry Smith if (mat->imax) PetscFree(mat->imax); 2300452661fSBarry Smith if (mat->solve_work) PetscFree(mat->solve_work); 2310452661fSBarry Smith PetscFree(mat); 232da3a660dSBarry Smith 233416022c9SBarry Smith PetscMemcpy(A,C,sizeof(struct _Mat)); 2340452661fSBarry Smith PetscHeaderDestroy(C); 235da3a660dSBarry Smith return 0; 236da3a660dSBarry Smith } 2370a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 238416022c9SBarry Smith int MatSolve_SeqAIJ(Mat A,Vec bb, Vec xx) 2398c37ef55SBarry Smith { 240416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 241416022c9SBarry Smith IS iscol = a->col, isrow = a->row; 242416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 243416022c9SBarry Smith int nz,shift = a->indexshift; 244416022c9SBarry Smith Scalar *x,*b,*tmp, *tmps, *aa = a->a, sum, *v; 2458c37ef55SBarry Smith 246416022c9SBarry Smith if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolve_SeqAIJ:Not for unfactored matrix"); 2479e25ed09SBarry Smith 24878b31e54SBarry Smith ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 24978b31e54SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 250416022c9SBarry Smith tmp = a->solve_work; 2518c37ef55SBarry Smith 25278b31e54SBarry Smith ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 25378b31e54SBarry Smith ierr = ISGetIndices(iscol,&c);CHKERRQ(ierr); c = c + (n-1); 2548c37ef55SBarry Smith 2558c37ef55SBarry Smith /* forward solve the lower triangular */ 2568c37ef55SBarry Smith tmp[0] = b[*r++]; 2570cf60383SBarry Smith tmps = tmp + shift; 2588c37ef55SBarry Smith for ( i=1; i<n; i++ ) { 259dbb450caSBarry Smith v = aa + ai[i] + shift; 260dbb450caSBarry Smith vi = aj + ai[i] + shift; 261416022c9SBarry Smith nz = a->diag[i] - ai[i]; 2628c37ef55SBarry Smith sum = b[*r++]; 2630cf60383SBarry Smith while (nz--) sum -= *v++ * tmps[*vi++]; 2648c37ef55SBarry Smith tmp[i] = sum; 2658c37ef55SBarry Smith } 2668c37ef55SBarry Smith 2678c37ef55SBarry Smith /* backward solve the upper triangular */ 2688c37ef55SBarry Smith for ( i=n-1; i>=0; i-- ){ 269416022c9SBarry Smith v = aa + a->diag[i] + (!shift); 270416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 271416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 2728c37ef55SBarry Smith sum = tmp[i]; 2730cf60383SBarry Smith while (nz--) sum -= *v++ * tmps[*vi++]; 274416022c9SBarry Smith x[*c--] = tmp[i] = sum*aa[a->diag[i]+shift]; 2758c37ef55SBarry Smith } 2768c37ef55SBarry Smith 277898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 278898183ecSLois Curfman McInnes ierr = ISRestoreIndices(iscol,&c); CHKERRQ(ierr); 279416022c9SBarry Smith PLogFlops(2*a->nz - a->n); 2808c37ef55SBarry Smith return 0; 2818c37ef55SBarry Smith } 282416022c9SBarry Smith int MatSolveAdd_SeqAIJ(Mat A,Vec bb, Vec yy, Vec xx) 283da3a660dSBarry Smith { 284416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 285416022c9SBarry Smith IS iscol = a->col, isrow = a->row; 286416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 287416022c9SBarry Smith int nz, shift = a->indexshift; 288416022c9SBarry Smith Scalar *x,*b,*tmp, *aa = a->a, sum, *v; 289da3a660dSBarry Smith 290416022c9SBarry Smith if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolveAdd_SeqAIJ:Not for unfactored matrix"); 29178b31e54SBarry Smith if (yy != xx) {ierr = VecCopy(yy,xx); CHKERRQ(ierr);} 292da3a660dSBarry Smith 29378b31e54SBarry Smith ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 29478b31e54SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 295416022c9SBarry Smith tmp = a->solve_work; 296da3a660dSBarry Smith 29778b31e54SBarry Smith ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr); 29878b31e54SBarry Smith ierr = ISGetIndices(iscol,&c); CHKERRQ(ierr); c = c + (n-1); 299da3a660dSBarry Smith 300da3a660dSBarry Smith /* forward solve the lower triangular */ 301da3a660dSBarry Smith tmp[0] = b[*r++]; 302da3a660dSBarry Smith for ( i=1; i<n; i++ ) { 303dbb450caSBarry Smith v = aa + ai[i] + shift; 304dbb450caSBarry Smith vi = aj + ai[i] + shift; 305416022c9SBarry Smith nz = a->diag[i] - ai[i]; 306da3a660dSBarry Smith sum = b[*r++]; 307dbb450caSBarry Smith while (nz--) sum -= *v++ * tmp[*vi++ + shift]; 308da3a660dSBarry Smith tmp[i] = sum; 309da3a660dSBarry Smith } 310da3a660dSBarry Smith 311da3a660dSBarry Smith /* backward solve the upper triangular */ 312da3a660dSBarry Smith for ( i=n-1; i>=0; i-- ){ 313416022c9SBarry Smith v = aa + a->diag[i] + (!shift); 314416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 315416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 316da3a660dSBarry Smith sum = tmp[i]; 317dbb450caSBarry Smith while (nz--) sum -= *v++ * tmp[*vi++ + shift]; 318416022c9SBarry Smith tmp[i] = sum*aa[a->diag[i]+shift]; 319da3a660dSBarry Smith x[*c--] += tmp[i]; 320da3a660dSBarry Smith } 321da3a660dSBarry Smith 322898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 323898183ecSLois Curfman McInnes ierr = ISRestoreIndices(iscol,&c); CHKERRQ(ierr); 324416022c9SBarry Smith PLogFlops(2*a->nz); 325898183ecSLois Curfman McInnes 326da3a660dSBarry Smith return 0; 327da3a660dSBarry Smith } 328da3a660dSBarry Smith /* -------------------------------------------------------------------*/ 329416022c9SBarry Smith int MatSolveTrans_SeqAIJ(Mat A,Vec bb, Vec xx) 330da3a660dSBarry Smith { 331416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 332416022c9SBarry Smith IS iscol = a->col, isrow = a->row, invisrow,inviscol; 333416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 334416022c9SBarry Smith int nz,shift = a->indexshift; 335416022c9SBarry Smith Scalar *x,*b,*tmp, *aa = a->a, *v; 336da3a660dSBarry Smith 337416022c9SBarry Smith if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolveTrans_SeqAIJ:Not unfactored matrix"); 33878b31e54SBarry Smith ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 33978b31e54SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 340416022c9SBarry Smith tmp = a->solve_work; 341da3a660dSBarry Smith 342da3a660dSBarry Smith /* invert the permutations */ 34378b31e54SBarry Smith ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr); 34478b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr); 345da3a660dSBarry Smith 34678b31e54SBarry Smith ierr = ISGetIndices(invisrow,&r); CHKERRQ(ierr); 34778b31e54SBarry Smith ierr = ISGetIndices(inviscol,&c); CHKERRQ(ierr); 348da3a660dSBarry Smith 349da3a660dSBarry Smith /* copy the b into temp work space according to permutation */ 350da3a660dSBarry Smith for ( i=0; i<n; i++ ) tmp[c[i]] = b[i]; 351da3a660dSBarry Smith 352da3a660dSBarry Smith /* forward solve the U^T */ 353da3a660dSBarry Smith for ( i=0; i<n; i++ ) { 354416022c9SBarry Smith v = aa + a->diag[i] + shift; 355416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 356416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 357da3a660dSBarry Smith tmp[i] *= *v++; 358da3a660dSBarry Smith while (nz--) { 359dbb450caSBarry Smith tmp[*vi++ + shift] -= (*v++)*tmp[i]; 360da3a660dSBarry Smith } 361da3a660dSBarry Smith } 362da3a660dSBarry Smith 363da3a660dSBarry Smith /* backward solve the L^T */ 364da3a660dSBarry Smith for ( i=n-1; i>=0; i-- ){ 365416022c9SBarry Smith v = aa + a->diag[i] - 1 + shift; 366416022c9SBarry Smith vi = aj + a->diag[i] - 1 + shift; 367416022c9SBarry Smith nz = a->diag[i] - ai[i]; 368da3a660dSBarry Smith while (nz--) { 369dbb450caSBarry Smith tmp[*vi-- + shift] -= (*v--)*tmp[i]; 370da3a660dSBarry Smith } 371da3a660dSBarry Smith } 372da3a660dSBarry Smith 373da3a660dSBarry Smith /* copy tmp into x according to permutation */ 374da3a660dSBarry Smith for ( i=0; i<n; i++ ) x[r[i]] = tmp[i]; 375da3a660dSBarry Smith 376898183ecSLois Curfman McInnes ierr = ISRestoreIndices(invisrow,&r); CHKERRQ(ierr); 377898183ecSLois Curfman McInnes ierr = ISRestoreIndices(inviscol,&c); CHKERRQ(ierr); 378898183ecSLois Curfman McInnes ierr = ISDestroy(invisrow); CHKERRQ(ierr); 379898183ecSLois Curfman McInnes ierr = ISDestroy(inviscol); CHKERRQ(ierr); 380da3a660dSBarry Smith 381416022c9SBarry Smith PLogFlops(2*a->nz-a->n); 382da3a660dSBarry Smith return 0; 383da3a660dSBarry Smith } 384da3a660dSBarry Smith 385416022c9SBarry Smith int MatSolveTransAdd_SeqAIJ(Mat A,Vec bb, Vec zz,Vec xx) 386da3a660dSBarry Smith { 387416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 388416022c9SBarry Smith IS iscol = a->col, isrow = a->row, invisrow,inviscol; 389416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 390416022c9SBarry Smith int nz,shift = a->indexshift; 391416022c9SBarry Smith Scalar *x,*b,*tmp, *aa = a->a, *v; 3926abc6512SBarry Smith 393416022c9SBarry Smith if (A->factor != FACTOR_LU)SETERRQ(1,"MatSolveTransAdd_SeqAIJ:Not unfactored matrix"); 3946abc6512SBarry Smith if (zz != xx) VecCopy(zz,xx); 3956abc6512SBarry Smith 39678b31e54SBarry Smith ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 39778b31e54SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 398416022c9SBarry Smith tmp = a->solve_work; 3996abc6512SBarry Smith 4006abc6512SBarry Smith /* invert the permutations */ 40178b31e54SBarry Smith ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr); 40278b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr); 40378b31e54SBarry Smith ierr = ISGetIndices(invisrow,&r); CHKERRQ(ierr); 40478b31e54SBarry Smith ierr = ISGetIndices(inviscol,&c); CHKERRQ(ierr); 4056abc6512SBarry Smith 4066abc6512SBarry Smith /* copy the b into temp work space according to permutation */ 4076abc6512SBarry Smith for ( i=0; i<n; i++ ) tmp[c[i]] = b[i]; 4086abc6512SBarry Smith 4096abc6512SBarry Smith /* forward solve the U^T */ 4106abc6512SBarry Smith for ( i=0; i<n; i++ ) { 411416022c9SBarry Smith v = aa + a->diag[i] + shift; 412416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 413416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 4146abc6512SBarry Smith tmp[i] *= *v++; 4156abc6512SBarry Smith while (nz--) { 416dbb450caSBarry Smith tmp[*vi++ + shift] -= (*v++)*tmp[i]; 4176abc6512SBarry Smith } 4186abc6512SBarry Smith } 4196abc6512SBarry Smith 4206abc6512SBarry Smith /* backward solve the L^T */ 4216abc6512SBarry Smith for ( i=n-1; i>=0; i-- ){ 422416022c9SBarry Smith v = aa + a->diag[i] - 1 + shift; 423416022c9SBarry Smith vi = aj + a->diag[i] - 1 + shift; 424416022c9SBarry Smith nz = a->diag[i] - ai[i]; 4256abc6512SBarry Smith while (nz--) { 426dbb450caSBarry Smith tmp[*vi-- + shift] -= (*v--)*tmp[i]; 4276abc6512SBarry Smith } 4286abc6512SBarry Smith } 4296abc6512SBarry Smith 4306abc6512SBarry Smith /* copy tmp into x according to permutation */ 4316abc6512SBarry Smith for ( i=0; i<n; i++ ) x[r[i]] += tmp[i]; 4326abc6512SBarry Smith 433898183ecSLois Curfman McInnes ierr = ISRestoreIndices(invisrow,&r); CHKERRQ(ierr); 434898183ecSLois Curfman McInnes ierr = ISRestoreIndices(inviscol,&c); CHKERRQ(ierr); 435898183ecSLois Curfman McInnes ierr = ISDestroy(invisrow); CHKERRQ(ierr); 436898183ecSLois Curfman McInnes ierr = ISDestroy(inviscol); CHKERRQ(ierr); 4376abc6512SBarry Smith 438416022c9SBarry Smith PLogFlops(2*a->nz); 4396abc6512SBarry Smith return 0; 440da3a660dSBarry Smith } 4419e25ed09SBarry Smith /* ----------------------------------------------------------------*/ 44208480c60SBarry Smith 44308480c60SBarry Smith int MatILUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,int levels,Mat *fact) 4449e25ed09SBarry Smith { 445416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b; 4469e25ed09SBarry Smith IS isicol; 447416022c9SBarry Smith int *r,*ic, ierr, prow, n = a->m, *ai = a->i, *aj = a->j; 44856beaf04SBarry Smith int *ainew,*ajnew, jmax,*fill, *xi, nz, *im,*ajfill,*flev; 44956beaf04SBarry Smith int *dloc, idx, row,m,fm, nzf, nzi,len, realloc = 0; 450416022c9SBarry Smith int incrlev,nnz,i,shift = a->indexshift; 4519e25ed09SBarry Smith 452416022c9SBarry Smith if (n != a->n) SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:Matrix must be square"); 453416022c9SBarry Smith if (!isrow) SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:Must have row permutation"); 454416022c9SBarry Smith if (!iscol) SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:Must have column permutation"); 4559e25ed09SBarry Smith 45608480c60SBarry Smith /* special case that simply copies fill pattern */ 45708480c60SBarry Smith if (levels == 0 && ISIsIdentity(isrow) && ISIsIdentity(iscol)) { 45808480c60SBarry Smith ierr = MatCopyPrivate_SeqAIJ(A,fact,DO_NOT_COPY_VALUES); CHKERRQ(ierr); 45908480c60SBarry Smith (*fact)->factor = FACTOR_LU; 46008480c60SBarry Smith b = (Mat_SeqAIJ *) (*fact)->data; 46108480c60SBarry Smith if (!b->diag) { 46208480c60SBarry Smith ierr = MatMarkDiag_SeqAIJ(*fact); CHKERRQ(ierr); 46308480c60SBarry Smith } 46408480c60SBarry Smith b->row = isrow; 46508480c60SBarry Smith b->col = iscol; 4660452661fSBarry Smith b->solve_work = (Scalar *) PetscMalloc((b->m+1)*sizeof(Scalar));CHKPTRQ(b->solve_work); 46708480c60SBarry Smith return 0; 46808480c60SBarry Smith } 46908480c60SBarry Smith 47078b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr); 471898183ecSLois Curfman McInnes ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr); 472898183ecSLois Curfman McInnes ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr); 4739e25ed09SBarry Smith 4749e25ed09SBarry Smith /* get new row pointers */ 4750452661fSBarry Smith ainew = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew); 476dbb450caSBarry Smith ainew[0] = -shift; 4779e25ed09SBarry Smith /* don't know how many column pointers are needed so estimate */ 478dbb450caSBarry Smith jmax = (int) (f*(ai[n]+!shift)); 4790452661fSBarry Smith ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew); 4809e25ed09SBarry Smith /* ajfill is level of fill for each fill entry */ 4810452661fSBarry Smith ajfill = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajfill); 4829e25ed09SBarry Smith /* fill is a linked list of nonzeros in active row */ 4830452661fSBarry Smith fill = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(fill); 48456beaf04SBarry Smith /* im is level for each filled value */ 4850452661fSBarry Smith im = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(im); 48656beaf04SBarry Smith /* dloc is location of diagonal in factor */ 4870452661fSBarry Smith dloc = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(dloc); 48856beaf04SBarry Smith dloc[0] = 0; 48956beaf04SBarry Smith for ( prow=0; prow<n; prow++ ) { 4909e25ed09SBarry Smith /* first copy previous fill into linked list */ 49156beaf04SBarry Smith nzf = nz = ai[r[prow]+1] - ai[r[prow]]; 492dbb450caSBarry Smith xi = aj + ai[r[prow]] + shift; 4939e25ed09SBarry Smith fill[n] = n; 4949e25ed09SBarry Smith while (nz--) { 4959e25ed09SBarry Smith fm = n; 496dbb450caSBarry Smith idx = ic[*xi++ + shift]; 4979e25ed09SBarry Smith do { 4989e25ed09SBarry Smith m = fm; 4999e25ed09SBarry Smith fm = fill[m]; 5009e25ed09SBarry Smith } while (fm < idx); 5019e25ed09SBarry Smith fill[m] = idx; 5029e25ed09SBarry Smith fill[idx] = fm; 50356beaf04SBarry Smith im[idx] = 0; 5049e25ed09SBarry Smith } 50556beaf04SBarry Smith nzi = 0; 5069e25ed09SBarry Smith row = fill[n]; 50756beaf04SBarry Smith while ( row < prow ) { 50856beaf04SBarry Smith incrlev = im[row] + 1; 50956beaf04SBarry Smith nz = dloc[row]; 510dbb450caSBarry Smith xi = ajnew + ainew[row] + shift + nz; 511dbb450caSBarry Smith flev = ajfill + ainew[row] + shift + nz + 1; 512416022c9SBarry Smith nnz = ainew[row+1] - ainew[row] - nz - 1; 513dbb450caSBarry Smith if (*xi++ + shift != row) { 514ec8511deSBarry Smith SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:zero pivot"); 51556beaf04SBarry Smith } 51656beaf04SBarry Smith fm = row; 51756beaf04SBarry Smith while (nnz-- > 0) { 518dbb450caSBarry Smith idx = *xi++ + shift; 51956beaf04SBarry Smith if (*flev + incrlev > levels) { 52056beaf04SBarry Smith flev++; 52156beaf04SBarry Smith continue; 52256beaf04SBarry Smith } 52356beaf04SBarry Smith do { 5249e25ed09SBarry Smith m = fm; 5259e25ed09SBarry Smith fm = fill[m]; 52656beaf04SBarry Smith } while (fm < idx); 5279e25ed09SBarry Smith if (fm != idx) { 52856beaf04SBarry Smith im[idx] = *flev + incrlev; 5299e25ed09SBarry Smith fill[m] = idx; 5309e25ed09SBarry Smith fill[idx] = fm; 5319e25ed09SBarry Smith fm = idx; 53256beaf04SBarry Smith nzf++; 5339e25ed09SBarry Smith } 53456beaf04SBarry Smith else { 53556beaf04SBarry Smith if (im[idx] > *flev + incrlev) im[idx] = *flev+incrlev; 5369e25ed09SBarry Smith } 53756beaf04SBarry Smith flev++; 5389e25ed09SBarry Smith } 5399e25ed09SBarry Smith row = fill[row]; 54056beaf04SBarry Smith nzi++; 5419e25ed09SBarry Smith } 5429e25ed09SBarry Smith /* copy new filled row into permanent storage */ 54356beaf04SBarry Smith ainew[prow+1] = ainew[prow] + nzf; 544*d7e8b826SBarry Smith if (ainew[prow+1] > jmax-shift) { 5459e25ed09SBarry Smith /* allocate a longer ajnew */ 546bbb6d6a8SBarry Smith int maxadd; 547dbb450caSBarry Smith maxadd = (int) ((f*(ai[n]+!shift)*(n-prow+5))/n); 54856beaf04SBarry Smith if (maxadd < nzf) maxadd = (n-prow)*(nzf+1); 549bbb6d6a8SBarry Smith jmax += maxadd; 5500452661fSBarry Smith xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi); 551416022c9SBarry Smith PetscMemcpy(xi,ajnew,(ainew[prow]+shift)*sizeof(int)); 5520452661fSBarry Smith PetscFree(ajnew); 55356beaf04SBarry Smith ajnew = xi; 5549e25ed09SBarry Smith /* allocate a longer ajfill */ 5550452661fSBarry Smith xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi); 556416022c9SBarry Smith PetscMemcpy(xi,ajfill,(ainew[prow]+shift)*sizeof(int)); 5570452661fSBarry Smith PetscFree(ajfill); 55856beaf04SBarry Smith ajfill = xi; 559bbb6d6a8SBarry Smith realloc++; 5609e25ed09SBarry Smith } 561dbb450caSBarry Smith xi = ajnew + ainew[prow] + shift; 562dbb450caSBarry Smith flev = ajfill + ainew[prow] + shift; 56356beaf04SBarry Smith dloc[prow] = nzi; 5649e25ed09SBarry Smith fm = fill[n]; 56556beaf04SBarry Smith while (nzf--) { 566dbb450caSBarry Smith *xi++ = fm - shift; 56756beaf04SBarry Smith *flev++ = im[fm]; 5689e25ed09SBarry Smith fm = fill[fm]; 5699e25ed09SBarry Smith } 5709e25ed09SBarry Smith } 5710452661fSBarry Smith PetscFree(ajfill); 572898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 573898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr); 574898183ecSLois Curfman McInnes ierr = ISDestroy(isicol); CHKERRQ(ierr); 5750452661fSBarry Smith PetscFree(fill); PetscFree(im); 5769e25ed09SBarry Smith 577416022c9SBarry Smith PLogInfo((PetscObject)A, 578ec8511deSBarry Smith "Info:MatILUFactorSymbolic_SeqAIJ:Realloc %d Fill ratio:given %g needed %g\n", 57956beaf04SBarry Smith realloc,f,((double)ainew[n])/((double)ai[prow])); 580bbb6d6a8SBarry Smith 5819e25ed09SBarry Smith /* put together the new matrix */ 582416022c9SBarry Smith ierr = MatCreateSeqAIJ(A->comm,n, n, 0, 0, fact); CHKERRQ(ierr); 583416022c9SBarry Smith b = (Mat_SeqAIJ *) (*fact)->data; 5840452661fSBarry Smith PetscFree(b->imax); 585416022c9SBarry Smith b->singlemalloc = 0; 586dbb450caSBarry Smith len = (ainew[n] + shift)*sizeof(Scalar); 5879e25ed09SBarry Smith /* the next line frees the default space generated by the Create() */ 5880452661fSBarry Smith PetscFree(b->a); PetscFree(b->ilen); 5890452661fSBarry Smith b->a = (Scalar *) PetscMalloc( len ); CHKPTRQ(b->a); 590416022c9SBarry Smith b->j = ajnew; 591416022c9SBarry Smith b->i = ainew; 59256beaf04SBarry Smith for ( i=0; i<n; i++ ) dloc[i] += ainew[i]; 593416022c9SBarry Smith b->diag = dloc; 594416022c9SBarry Smith b->ilen = 0; 595416022c9SBarry Smith b->imax = 0; 596416022c9SBarry Smith b->row = isrow; 597416022c9SBarry Smith b->col = iscol; 5980452661fSBarry Smith b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar)); 599416022c9SBarry Smith CHKPTRQ(b->solve_work); 600416022c9SBarry Smith /* In b structure: Free imax, ilen, old a, old j. 60156beaf04SBarry Smith Allocate dloc, solve_work, new a, new j */ 602dbb450caSBarry Smith PLogObjectMemory(*fact,(ainew[n]+shift-n) * (sizeof(int)+sizeof(Scalar))); 603416022c9SBarry Smith b->maxnz = b->nz = ainew[n] + shift; 6049e25ed09SBarry Smith (*fact)->factor = FACTOR_LU; 6059e25ed09SBarry Smith return 0; 6069e25ed09SBarry Smith } 607d5d45c9bSBarry Smith 608d5d45c9bSBarry Smith 609d5d45c9bSBarry Smith 610d5d45c9bSBarry Smith 611