1cb512458SBarry Smith #ifndef lint 2*c2c36f1cSSatish Balay static char vcid[] = "$Id: aijfact.c,v 1.48 1995/11/22 01:25:25 balay Exp balay $"; 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; 169*c2c36f1cSSatish Balay /*for ( j=0; j<nz; j++ ) rtmps[ajtmp[j]] = 0.0;*/ 170*c2c36f1cSSatish Balay for(j = 0; j < n; ++j) rtmps[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); 2330452661fSBarry Smith PetscFree(mat); 234da3a660dSBarry Smith 235416022c9SBarry Smith PetscMemcpy(A,C,sizeof(struct _Mat)); 2360452661fSBarry Smith PetscHeaderDestroy(C); 237da3a660dSBarry Smith return 0; 238da3a660dSBarry Smith } 2390a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 240416022c9SBarry Smith int MatSolve_SeqAIJ(Mat A,Vec bb, Vec xx) 2418c37ef55SBarry Smith { 242416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 243416022c9SBarry Smith IS iscol = a->col, isrow = a->row; 244416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 245416022c9SBarry Smith int nz,shift = a->indexshift; 246416022c9SBarry Smith Scalar *x,*b,*tmp, *tmps, *aa = a->a, sum, *v; 2478c37ef55SBarry Smith 248416022c9SBarry Smith if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolve_SeqAIJ:Not for unfactored matrix"); 2499e25ed09SBarry Smith 25078b31e54SBarry Smith ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 25178b31e54SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 252416022c9SBarry Smith tmp = a->solve_work; 2538c37ef55SBarry Smith 25478b31e54SBarry Smith ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 25578b31e54SBarry Smith ierr = ISGetIndices(iscol,&c);CHKERRQ(ierr); c = c + (n-1); 2568c37ef55SBarry Smith 2578c37ef55SBarry Smith /* forward solve the lower triangular */ 2588c37ef55SBarry Smith tmp[0] = b[*r++]; 2590cf60383SBarry Smith tmps = tmp + shift; 2608c37ef55SBarry Smith for ( i=1; i<n; i++ ) { 261dbb450caSBarry Smith v = aa + ai[i] + shift; 262dbb450caSBarry Smith vi = aj + ai[i] + shift; 263416022c9SBarry Smith nz = a->diag[i] - ai[i]; 2648c37ef55SBarry Smith sum = b[*r++]; 2650cf60383SBarry Smith while (nz--) sum -= *v++ * tmps[*vi++]; 2668c37ef55SBarry Smith tmp[i] = sum; 2678c37ef55SBarry Smith } 2688c37ef55SBarry Smith 2698c37ef55SBarry Smith /* backward solve the upper triangular */ 2708c37ef55SBarry Smith for ( i=n-1; i>=0; i-- ){ 271416022c9SBarry Smith v = aa + a->diag[i] + (!shift); 272416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 273416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 2748c37ef55SBarry Smith sum = tmp[i]; 2750cf60383SBarry Smith while (nz--) sum -= *v++ * tmps[*vi++]; 276416022c9SBarry Smith x[*c--] = tmp[i] = sum*aa[a->diag[i]+shift]; 2778c37ef55SBarry Smith } 2788c37ef55SBarry Smith 279898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 280898183ecSLois Curfman McInnes ierr = ISRestoreIndices(iscol,&c); CHKERRQ(ierr); 281416022c9SBarry Smith PLogFlops(2*a->nz - a->n); 2828c37ef55SBarry Smith return 0; 2838c37ef55SBarry Smith } 284416022c9SBarry Smith int MatSolveAdd_SeqAIJ(Mat A,Vec bb, Vec yy, Vec xx) 285da3a660dSBarry Smith { 286416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 287416022c9SBarry Smith IS iscol = a->col, isrow = a->row; 288416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 289416022c9SBarry Smith int nz, shift = a->indexshift; 290416022c9SBarry Smith Scalar *x,*b,*tmp, *aa = a->a, sum, *v; 291da3a660dSBarry Smith 292416022c9SBarry Smith if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolveAdd_SeqAIJ:Not for unfactored matrix"); 29378b31e54SBarry Smith if (yy != xx) {ierr = VecCopy(yy,xx); CHKERRQ(ierr);} 294da3a660dSBarry Smith 29578b31e54SBarry Smith ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 29678b31e54SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 297416022c9SBarry Smith tmp = a->solve_work; 298da3a660dSBarry Smith 29978b31e54SBarry Smith ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr); 30078b31e54SBarry Smith ierr = ISGetIndices(iscol,&c); CHKERRQ(ierr); c = c + (n-1); 301da3a660dSBarry Smith 302da3a660dSBarry Smith /* forward solve the lower triangular */ 303da3a660dSBarry Smith tmp[0] = b[*r++]; 304da3a660dSBarry Smith for ( i=1; i<n; i++ ) { 305dbb450caSBarry Smith v = aa + ai[i] + shift; 306dbb450caSBarry Smith vi = aj + ai[i] + shift; 307416022c9SBarry Smith nz = a->diag[i] - ai[i]; 308da3a660dSBarry Smith sum = b[*r++]; 309dbb450caSBarry Smith while (nz--) sum -= *v++ * tmp[*vi++ + shift]; 310da3a660dSBarry Smith tmp[i] = sum; 311da3a660dSBarry Smith } 312da3a660dSBarry Smith 313da3a660dSBarry Smith /* backward solve the upper triangular */ 314da3a660dSBarry Smith for ( i=n-1; i>=0; i-- ){ 315416022c9SBarry Smith v = aa + a->diag[i] + (!shift); 316416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 317416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 318da3a660dSBarry Smith sum = tmp[i]; 319dbb450caSBarry Smith while (nz--) sum -= *v++ * tmp[*vi++ + shift]; 320416022c9SBarry Smith tmp[i] = sum*aa[a->diag[i]+shift]; 321da3a660dSBarry Smith x[*c--] += tmp[i]; 322da3a660dSBarry Smith } 323da3a660dSBarry Smith 324898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 325898183ecSLois Curfman McInnes ierr = ISRestoreIndices(iscol,&c); CHKERRQ(ierr); 326416022c9SBarry Smith PLogFlops(2*a->nz); 327898183ecSLois Curfman McInnes 328da3a660dSBarry Smith return 0; 329da3a660dSBarry Smith } 330da3a660dSBarry Smith /* -------------------------------------------------------------------*/ 331416022c9SBarry Smith int MatSolveTrans_SeqAIJ(Mat A,Vec bb, Vec xx) 332da3a660dSBarry Smith { 333416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 334416022c9SBarry Smith IS iscol = a->col, isrow = a->row, invisrow,inviscol; 335416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 336416022c9SBarry Smith int nz,shift = a->indexshift; 337416022c9SBarry Smith Scalar *x,*b,*tmp, *aa = a->a, *v; 338da3a660dSBarry Smith 339416022c9SBarry Smith if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolveTrans_SeqAIJ:Not unfactored matrix"); 34078b31e54SBarry Smith ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 34178b31e54SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 342416022c9SBarry Smith tmp = a->solve_work; 343da3a660dSBarry Smith 344da3a660dSBarry Smith /* invert the permutations */ 34578b31e54SBarry Smith ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr); 34678b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr); 347da3a660dSBarry Smith 34878b31e54SBarry Smith ierr = ISGetIndices(invisrow,&r); CHKERRQ(ierr); 34978b31e54SBarry Smith ierr = ISGetIndices(inviscol,&c); CHKERRQ(ierr); 350da3a660dSBarry Smith 351da3a660dSBarry Smith /* copy the b into temp work space according to permutation */ 352da3a660dSBarry Smith for ( i=0; i<n; i++ ) tmp[c[i]] = b[i]; 353da3a660dSBarry Smith 354da3a660dSBarry Smith /* forward solve the U^T */ 355da3a660dSBarry Smith for ( i=0; i<n; i++ ) { 356416022c9SBarry Smith v = aa + a->diag[i] + shift; 357416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 358416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 359da3a660dSBarry Smith tmp[i] *= *v++; 360da3a660dSBarry Smith while (nz--) { 361dbb450caSBarry Smith tmp[*vi++ + shift] -= (*v++)*tmp[i]; 362da3a660dSBarry Smith } 363da3a660dSBarry Smith } 364da3a660dSBarry Smith 365da3a660dSBarry Smith /* backward solve the L^T */ 366da3a660dSBarry Smith for ( i=n-1; i>=0; i-- ){ 367416022c9SBarry Smith v = aa + a->diag[i] - 1 + shift; 368416022c9SBarry Smith vi = aj + a->diag[i] - 1 + shift; 369416022c9SBarry Smith nz = a->diag[i] - ai[i]; 370da3a660dSBarry Smith while (nz--) { 371dbb450caSBarry Smith tmp[*vi-- + shift] -= (*v--)*tmp[i]; 372da3a660dSBarry Smith } 373da3a660dSBarry Smith } 374da3a660dSBarry Smith 375da3a660dSBarry Smith /* copy tmp into x according to permutation */ 376da3a660dSBarry Smith for ( i=0; i<n; i++ ) x[r[i]] = tmp[i]; 377da3a660dSBarry Smith 378898183ecSLois Curfman McInnes ierr = ISRestoreIndices(invisrow,&r); CHKERRQ(ierr); 379898183ecSLois Curfman McInnes ierr = ISRestoreIndices(inviscol,&c); CHKERRQ(ierr); 380898183ecSLois Curfman McInnes ierr = ISDestroy(invisrow); CHKERRQ(ierr); 381898183ecSLois Curfman McInnes ierr = ISDestroy(inviscol); CHKERRQ(ierr); 382da3a660dSBarry Smith 383416022c9SBarry Smith PLogFlops(2*a->nz-a->n); 384da3a660dSBarry Smith return 0; 385da3a660dSBarry Smith } 386da3a660dSBarry Smith 387416022c9SBarry Smith int MatSolveTransAdd_SeqAIJ(Mat A,Vec bb, Vec zz,Vec xx) 388da3a660dSBarry Smith { 389416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 390416022c9SBarry Smith IS iscol = a->col, isrow = a->row, invisrow,inviscol; 391416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 392416022c9SBarry Smith int nz,shift = a->indexshift; 393416022c9SBarry Smith Scalar *x,*b,*tmp, *aa = a->a, *v; 3946abc6512SBarry Smith 395416022c9SBarry Smith if (A->factor != FACTOR_LU)SETERRQ(1,"MatSolveTransAdd_SeqAIJ:Not unfactored matrix"); 3966abc6512SBarry Smith if (zz != xx) VecCopy(zz,xx); 3976abc6512SBarry Smith 39878b31e54SBarry Smith ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 39978b31e54SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 400416022c9SBarry Smith tmp = a->solve_work; 4016abc6512SBarry Smith 4026abc6512SBarry Smith /* invert the permutations */ 40378b31e54SBarry Smith ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr); 40478b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr); 40578b31e54SBarry Smith ierr = ISGetIndices(invisrow,&r); CHKERRQ(ierr); 40678b31e54SBarry Smith ierr = ISGetIndices(inviscol,&c); CHKERRQ(ierr); 4076abc6512SBarry Smith 4086abc6512SBarry Smith /* copy the b into temp work space according to permutation */ 4096abc6512SBarry Smith for ( i=0; i<n; i++ ) tmp[c[i]] = b[i]; 4106abc6512SBarry Smith 4116abc6512SBarry Smith /* forward solve the U^T */ 4126abc6512SBarry Smith for ( i=0; i<n; i++ ) { 413416022c9SBarry Smith v = aa + a->diag[i] + shift; 414416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 415416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 4166abc6512SBarry Smith tmp[i] *= *v++; 4176abc6512SBarry Smith while (nz--) { 418dbb450caSBarry Smith tmp[*vi++ + shift] -= (*v++)*tmp[i]; 4196abc6512SBarry Smith } 4206abc6512SBarry Smith } 4216abc6512SBarry Smith 4226abc6512SBarry Smith /* backward solve the L^T */ 4236abc6512SBarry Smith for ( i=n-1; i>=0; i-- ){ 424416022c9SBarry Smith v = aa + a->diag[i] - 1 + shift; 425416022c9SBarry Smith vi = aj + a->diag[i] - 1 + shift; 426416022c9SBarry Smith nz = a->diag[i] - ai[i]; 4276abc6512SBarry Smith while (nz--) { 428dbb450caSBarry Smith tmp[*vi-- + shift] -= (*v--)*tmp[i]; 4296abc6512SBarry Smith } 4306abc6512SBarry Smith } 4316abc6512SBarry Smith 4326abc6512SBarry Smith /* copy tmp into x according to permutation */ 4336abc6512SBarry Smith for ( i=0; i<n; i++ ) x[r[i]] += tmp[i]; 4346abc6512SBarry Smith 435898183ecSLois Curfman McInnes ierr = ISRestoreIndices(invisrow,&r); CHKERRQ(ierr); 436898183ecSLois Curfman McInnes ierr = ISRestoreIndices(inviscol,&c); CHKERRQ(ierr); 437898183ecSLois Curfman McInnes ierr = ISDestroy(invisrow); CHKERRQ(ierr); 438898183ecSLois Curfman McInnes ierr = ISDestroy(inviscol); CHKERRQ(ierr); 4396abc6512SBarry Smith 440416022c9SBarry Smith PLogFlops(2*a->nz); 4416abc6512SBarry Smith return 0; 442da3a660dSBarry Smith } 4439e25ed09SBarry Smith /* ----------------------------------------------------------------*/ 44408480c60SBarry Smith 44508480c60SBarry Smith int MatILUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,int levels,Mat *fact) 4469e25ed09SBarry Smith { 447416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b; 4489e25ed09SBarry Smith IS isicol; 449416022c9SBarry Smith int *r,*ic, ierr, prow, n = a->m, *ai = a->i, *aj = a->j; 45056beaf04SBarry Smith int *ainew,*ajnew, jmax,*fill, *xi, nz, *im,*ajfill,*flev; 45156beaf04SBarry Smith int *dloc, idx, row,m,fm, nzf, nzi,len, realloc = 0; 452416022c9SBarry Smith int incrlev,nnz,i,shift = a->indexshift; 4539e25ed09SBarry Smith 454416022c9SBarry Smith if (n != a->n) SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:Matrix must be square"); 455416022c9SBarry Smith if (!isrow) SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:Must have row permutation"); 456416022c9SBarry Smith if (!iscol) SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:Must have column permutation"); 4579e25ed09SBarry Smith 45808480c60SBarry Smith /* special case that simply copies fill pattern */ 45908480c60SBarry Smith if (levels == 0 && ISIsIdentity(isrow) && ISIsIdentity(iscol)) { 46008480c60SBarry Smith ierr = MatCopyPrivate_SeqAIJ(A,fact,DO_NOT_COPY_VALUES); CHKERRQ(ierr); 46108480c60SBarry Smith (*fact)->factor = FACTOR_LU; 46208480c60SBarry Smith b = (Mat_SeqAIJ *) (*fact)->data; 46308480c60SBarry Smith if (!b->diag) { 46408480c60SBarry Smith ierr = MatMarkDiag_SeqAIJ(*fact); CHKERRQ(ierr); 46508480c60SBarry Smith } 46608480c60SBarry Smith b->row = isrow; 46708480c60SBarry Smith b->col = iscol; 4680452661fSBarry Smith b->solve_work = (Scalar *) PetscMalloc((b->m+1)*sizeof(Scalar));CHKPTRQ(b->solve_work); 46908480c60SBarry Smith return 0; 47008480c60SBarry Smith } 47108480c60SBarry Smith 47278b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr); 473898183ecSLois Curfman McInnes ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr); 474898183ecSLois Curfman McInnes ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr); 4759e25ed09SBarry Smith 4769e25ed09SBarry Smith /* get new row pointers */ 4770452661fSBarry Smith ainew = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew); 478dbb450caSBarry Smith ainew[0] = -shift; 4799e25ed09SBarry Smith /* don't know how many column pointers are needed so estimate */ 480dbb450caSBarry Smith jmax = (int) (f*(ai[n]+!shift)); 4810452661fSBarry Smith ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew); 4829e25ed09SBarry Smith /* ajfill is level of fill for each fill entry */ 4830452661fSBarry Smith ajfill = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajfill); 4849e25ed09SBarry Smith /* fill is a linked list of nonzeros in active row */ 4850452661fSBarry Smith fill = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(fill); 48656beaf04SBarry Smith /* im is level for each filled value */ 4870452661fSBarry Smith im = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(im); 48856beaf04SBarry Smith /* dloc is location of diagonal in factor */ 4890452661fSBarry Smith dloc = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(dloc); 49056beaf04SBarry Smith dloc[0] = 0; 49156beaf04SBarry Smith for ( prow=0; prow<n; prow++ ) { 4929e25ed09SBarry Smith /* first copy previous fill into linked list */ 49356beaf04SBarry Smith nzf = nz = ai[r[prow]+1] - ai[r[prow]]; 494dbb450caSBarry Smith xi = aj + ai[r[prow]] + shift; 4959e25ed09SBarry Smith fill[n] = n; 4969e25ed09SBarry Smith while (nz--) { 4979e25ed09SBarry Smith fm = n; 498dbb450caSBarry Smith idx = ic[*xi++ + shift]; 4999e25ed09SBarry Smith do { 5009e25ed09SBarry Smith m = fm; 5019e25ed09SBarry Smith fm = fill[m]; 5029e25ed09SBarry Smith } while (fm < idx); 5039e25ed09SBarry Smith fill[m] = idx; 5049e25ed09SBarry Smith fill[idx] = fm; 50556beaf04SBarry Smith im[idx] = 0; 5069e25ed09SBarry Smith } 50756beaf04SBarry Smith nzi = 0; 5089e25ed09SBarry Smith row = fill[n]; 50956beaf04SBarry Smith while ( row < prow ) { 51056beaf04SBarry Smith incrlev = im[row] + 1; 51156beaf04SBarry Smith nz = dloc[row]; 512dbb450caSBarry Smith xi = ajnew + ainew[row] + shift + nz; 513dbb450caSBarry Smith flev = ajfill + ainew[row] + shift + nz + 1; 514416022c9SBarry Smith nnz = ainew[row+1] - ainew[row] - nz - 1; 515dbb450caSBarry Smith if (*xi++ + shift != row) { 516ec8511deSBarry Smith SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:zero pivot"); 51756beaf04SBarry Smith } 51856beaf04SBarry Smith fm = row; 51956beaf04SBarry Smith while (nnz-- > 0) { 520dbb450caSBarry Smith idx = *xi++ + shift; 52156beaf04SBarry Smith if (*flev + incrlev > levels) { 52256beaf04SBarry Smith flev++; 52356beaf04SBarry Smith continue; 52456beaf04SBarry Smith } 52556beaf04SBarry Smith do { 5269e25ed09SBarry Smith m = fm; 5279e25ed09SBarry Smith fm = fill[m]; 52856beaf04SBarry Smith } while (fm < idx); 5299e25ed09SBarry Smith if (fm != idx) { 53056beaf04SBarry Smith im[idx] = *flev + incrlev; 5319e25ed09SBarry Smith fill[m] = idx; 5329e25ed09SBarry Smith fill[idx] = fm; 5339e25ed09SBarry Smith fm = idx; 53456beaf04SBarry Smith nzf++; 5359e25ed09SBarry Smith } 53656beaf04SBarry Smith else { 53756beaf04SBarry Smith if (im[idx] > *flev + incrlev) im[idx] = *flev+incrlev; 5389e25ed09SBarry Smith } 53956beaf04SBarry Smith flev++; 5409e25ed09SBarry Smith } 5419e25ed09SBarry Smith row = fill[row]; 54256beaf04SBarry Smith nzi++; 5439e25ed09SBarry Smith } 5449e25ed09SBarry Smith /* copy new filled row into permanent storage */ 54556beaf04SBarry Smith ainew[prow+1] = ainew[prow] + nzf; 546d7e8b826SBarry Smith if (ainew[prow+1] > jmax-shift) { 5479e25ed09SBarry Smith /* allocate a longer ajnew */ 548bbb6d6a8SBarry Smith int maxadd; 549dbb450caSBarry Smith maxadd = (int) ((f*(ai[n]+!shift)*(n-prow+5))/n); 55056beaf04SBarry Smith if (maxadd < nzf) maxadd = (n-prow)*(nzf+1); 551bbb6d6a8SBarry Smith jmax += maxadd; 5520452661fSBarry Smith xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi); 553416022c9SBarry Smith PetscMemcpy(xi,ajnew,(ainew[prow]+shift)*sizeof(int)); 5540452661fSBarry Smith PetscFree(ajnew); 55556beaf04SBarry Smith ajnew = xi; 5569e25ed09SBarry Smith /* allocate a longer ajfill */ 5570452661fSBarry Smith xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi); 558416022c9SBarry Smith PetscMemcpy(xi,ajfill,(ainew[prow]+shift)*sizeof(int)); 5590452661fSBarry Smith PetscFree(ajfill); 56056beaf04SBarry Smith ajfill = xi; 561bbb6d6a8SBarry Smith realloc++; 5629e25ed09SBarry Smith } 563dbb450caSBarry Smith xi = ajnew + ainew[prow] + shift; 564dbb450caSBarry Smith flev = ajfill + ainew[prow] + shift; 56556beaf04SBarry Smith dloc[prow] = nzi; 5669e25ed09SBarry Smith fm = fill[n]; 56756beaf04SBarry Smith while (nzf--) { 568dbb450caSBarry Smith *xi++ = fm - shift; 56956beaf04SBarry Smith *flev++ = im[fm]; 5709e25ed09SBarry Smith fm = fill[fm]; 5719e25ed09SBarry Smith } 5729e25ed09SBarry Smith } 5730452661fSBarry Smith PetscFree(ajfill); 574898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 575898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr); 576898183ecSLois Curfman McInnes ierr = ISDestroy(isicol); CHKERRQ(ierr); 5770452661fSBarry Smith PetscFree(fill); PetscFree(im); 5789e25ed09SBarry Smith 579416022c9SBarry Smith PLogInfo((PetscObject)A, 580ec8511deSBarry Smith "Info:MatILUFactorSymbolic_SeqAIJ:Realloc %d Fill ratio:given %g needed %g\n", 58156beaf04SBarry Smith realloc,f,((double)ainew[n])/((double)ai[prow])); 582bbb6d6a8SBarry Smith 5839e25ed09SBarry Smith /* put together the new matrix */ 584416022c9SBarry Smith ierr = MatCreateSeqAIJ(A->comm,n, n, 0, 0, fact); CHKERRQ(ierr); 585416022c9SBarry Smith b = (Mat_SeqAIJ *) (*fact)->data; 5860452661fSBarry Smith PetscFree(b->imax); 587416022c9SBarry Smith b->singlemalloc = 0; 588dbb450caSBarry Smith len = (ainew[n] + shift)*sizeof(Scalar); 5899e25ed09SBarry Smith /* the next line frees the default space generated by the Create() */ 5900452661fSBarry Smith PetscFree(b->a); PetscFree(b->ilen); 5910452661fSBarry Smith b->a = (Scalar *) PetscMalloc( len ); CHKPTRQ(b->a); 592416022c9SBarry Smith b->j = ajnew; 593416022c9SBarry Smith b->i = ainew; 59456beaf04SBarry Smith for ( i=0; i<n; i++ ) dloc[i] += ainew[i]; 595416022c9SBarry Smith b->diag = dloc; 596416022c9SBarry Smith b->ilen = 0; 597416022c9SBarry Smith b->imax = 0; 598416022c9SBarry Smith b->row = isrow; 599416022c9SBarry Smith b->col = iscol; 6000452661fSBarry Smith b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar)); 601416022c9SBarry Smith CHKPTRQ(b->solve_work); 602416022c9SBarry Smith /* In b structure: Free imax, ilen, old a, old j. 60356beaf04SBarry Smith Allocate dloc, solve_work, new a, new j */ 604dbb450caSBarry Smith PLogObjectMemory(*fact,(ainew[n]+shift-n) * (sizeof(int)+sizeof(Scalar))); 605416022c9SBarry Smith b->maxnz = b->nz = ainew[n] + shift; 6069e25ed09SBarry Smith (*fact)->factor = FACTOR_LU; 6079e25ed09SBarry Smith return 0; 6089e25ed09SBarry Smith } 609d5d45c9bSBarry Smith 610d5d45c9bSBarry Smith 611d5d45c9bSBarry Smith 612d5d45c9bSBarry Smith 613