1cb512458SBarry Smith #ifndef lint 2*416022c9SBarry Smith static char vcid[] = "$Id: aijfact.c,v 1.39 1995/09/22 20:17:46 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 12*416022c9SBarry Smith int MatLUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,Mat *B) 13289bc588SBarry Smith { 14*416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b; 15289bc588SBarry Smith IS isicol; 16*416022c9SBarry Smith int *r,*ic, ierr, i, n = a->m, *ai = a->i, *aj = a->j; 17*416022c9SBarry 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 20*416022c9SBarry Smith if (n != a->n) SETERRQ(1,"MatLUFactorSymbolic_SeqAIJ:Must be square"); 21*416022c9SBarry Smith if (!isrow) SETERRQ(1,"MatLUFactorSymbolic_SeqAIJ:Must have row permutation"); 22*416022c9SBarry 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); 87*416022c9SBarry 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 104*416022c9SBarry 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); 110898183ecSLois Curfman McInnes ierr = ISDestroy(isicol); CHKERRQ(ierr); 111898183ecSLois Curfman McInnes PETSCFREE(fill); 112289bc588SBarry Smith 113289bc588SBarry Smith /* put together the new matrix */ 114*416022c9SBarry Smith ierr = MatCreateSeqAIJ(A->comm,n, n, 0, 0, B); CHKERRQ(ierr); 115*416022c9SBarry Smith b = (Mat_SeqAIJ *) (*B)->data; 116*416022c9SBarry Smith PETSCFREE(b->imax); 117*416022c9SBarry Smith b->singlemalloc = 0; 118dbb450caSBarry Smith len = (ainew[n] + shift)*sizeof(Scalar); 119e8d4e0b9SBarry Smith /* the next line frees the default space generated by the Create() */ 120*416022c9SBarry Smith PETSCFREE(b->a); PETSCFREE(b->ilen); 121*416022c9SBarry Smith b->a = (Scalar *) PETSCMALLOC( len ); CHKPTRQ(b->a); 122*416022c9SBarry Smith b->j = ajnew; 123*416022c9SBarry Smith b->i = ainew; 124*416022c9SBarry Smith b->diag = idnew; 125*416022c9SBarry Smith b->ilen = 0; 126*416022c9SBarry Smith b->imax = 0; 127*416022c9SBarry Smith b->row = isrow; 128*416022c9SBarry Smith b->col = iscol; 129*416022c9SBarry Smith b->solve_work = (Scalar *) PETSCMALLOC( n*sizeof(Scalar)); 130*416022c9SBarry Smith CHKPTRQ(b->solve_work); 131*416022c9SBarry Smith /* In b structure: Free imax, ilen, old a, old j. 1327fd98bd6SLois Curfman McInnes Allocate idnew, solve_work, new a, new j */ 133*416022c9SBarry Smith PLogObjectMemory(*B,(ainew[n]+shift-n)*(sizeof(int)+sizeof(Scalar))); 134*416022c9SBarry Smith b->maxnz = b->nz = ainew[n] + shift; 1357fd98bd6SLois Curfman McInnes 1365e42470aSBarry Smith /* Cannot do this here because child is destroyed before parent created 137*416022c9SBarry Smith PLogObjectParent(*B,isicol); */ 138289bc588SBarry Smith return 0; 139289bc588SBarry Smith } 1400a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 141*416022c9SBarry Smith int MatLUFactorNumeric_SeqAIJ(Mat A,Mat *B) 142289bc588SBarry Smith { 143*416022c9SBarry Smith Mat C = *B; 144*416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b = (Mat_SeqAIJ *)C->data; 145*416022c9SBarry Smith IS iscol = b->col, isrow = b->row, isicol; 146*416022c9SBarry Smith int *r,*ic, ierr, i, j, n = a->m, *ai = b->i, *aj = b->j; 147*416022c9SBarry Smith int *ajtmpold, *ajtmp, nz, row,*pj, *ics, shift = a->indexshift; 1480cf60383SBarry Smith Scalar *rtmp,*v, *pv, *pc, multiplier, *rtmps; 149289bc588SBarry Smith 15078b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr); 151*416022c9SBarry Smith PLogObjectParent(*B,isicol); 15278b31e54SBarry Smith ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr); 15378b31e54SBarry Smith ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr); 15478b31e54SBarry Smith rtmp = (Scalar *) PETSCMALLOC( (n+1)*sizeof(Scalar) ); CHKPTRQ(rtmp); 1550cf60383SBarry Smith rtmps = rtmp + shift; ics = ic + shift; 156289bc588SBarry Smith 157289bc588SBarry Smith for ( i=0; i<n; i++ ) { 158289bc588SBarry Smith nz = ai[i+1] - ai[i]; 159dbb450caSBarry Smith ajtmp = aj + ai[i] + shift; 1600cf60383SBarry Smith for ( j=0; j<nz; j++ ) rtmps[ajtmp[j]] = 0.0; 161289bc588SBarry Smith 162289bc588SBarry Smith /* load in initial (unfactored row) */ 163*416022c9SBarry Smith nz = a->i[r[i]+1] - a->i[r[i]]; 164*416022c9SBarry Smith ajtmpold = a->j + a->i[r[i]] + shift; 165*416022c9SBarry Smith v = a->a + a->i[r[i]] + shift; 1660cf60383SBarry Smith for ( j=0; j<nz; j++ ) rtmp[ics[ajtmpold[j]]] = v[j]; 167289bc588SBarry Smith 168dbb450caSBarry Smith row = *ajtmp++ + shift; 169289bc588SBarry Smith while (row < i) { 1708c37ef55SBarry Smith pc = rtmp + row; 1718c37ef55SBarry Smith if (*pc != 0.0) { 172*416022c9SBarry Smith pv = b->a + b->diag[row] + shift; 173*416022c9SBarry Smith pj = b->j + b->diag[row] + (!shift); 1748c37ef55SBarry Smith multiplier = *pc * *pv++; 1758c37ef55SBarry Smith *pc = multiplier; 176*416022c9SBarry Smith nz = ai[row+1] - b->diag[row] - 1; 177eef2e97bSBarry Smith PLogFlops(2*nz); 1780cf60383SBarry Smith while (nz-->0) rtmps[*pj++] -= multiplier* *pv++; 179289bc588SBarry Smith } 180dbb450caSBarry Smith row = *ajtmp++ + shift; 181289bc588SBarry Smith } 182*416022c9SBarry Smith /* finished row so stick it into b->a */ 183*416022c9SBarry Smith pv = b->a + ai[i] + shift; 184*416022c9SBarry Smith pj = b->j + ai[i] + shift; 1858c37ef55SBarry Smith nz = ai[i+1] - ai[i]; 186ec8511deSBarry Smith if (rtmp[i] == 0.0) {SETERRQ(1,"MatLUFactorNumeric_SeqAIJ:Zero pivot");} 1878c37ef55SBarry Smith rtmp[i] = 1.0/rtmp[i]; 188*416022c9SBarry Smith for ( j=0; j<nz; j++ ) {pv[j] = rtmps[pj[j]];} 1898c37ef55SBarry Smith } 19078b31e54SBarry Smith PETSCFREE(rtmp); 19178b31e54SBarry Smith ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr); 19278b31e54SBarry Smith ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 19378b31e54SBarry Smith ierr = ISDestroy(isicol); CHKERRQ(ierr); 194*416022c9SBarry Smith C->factor = FACTOR_LU; 195*416022c9SBarry Smith b->assembled = 1; 196*416022c9SBarry Smith PLogFlops(b->n); 197289bc588SBarry Smith return 0; 198289bc588SBarry Smith } 1990a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 200*416022c9SBarry Smith int MatLUFactor_SeqAIJ(Mat A,IS row,IS col,double f) 201da3a660dSBarry Smith { 202*416022c9SBarry Smith Mat_SeqAIJ *mat = (Mat_SeqAIJ *) A->data; 2036abc6512SBarry Smith int ierr; 204*416022c9SBarry Smith Mat C; 205*416022c9SBarry Smith 206*416022c9SBarry Smith ierr = MatLUFactorSymbolic_SeqAIJ(A,row,col,f,&C); CHKERRQ(ierr); 207*416022c9SBarry Smith ierr = MatLUFactorNumeric_SeqAIJ(A,&C); CHKERRQ(ierr); 208da3a660dSBarry Smith 209da3a660dSBarry Smith /* free all the data structures from mat */ 21078b31e54SBarry Smith PETSCFREE(mat->a); 21178b31e54SBarry Smith if (!mat->singlemalloc) {PETSCFREE(mat->i); PETSCFREE(mat->j);} 21278b31e54SBarry Smith if (mat->diag) PETSCFREE(mat->diag); 21378b31e54SBarry Smith if (mat->ilen) PETSCFREE(mat->ilen); 21478b31e54SBarry Smith if (mat->imax) PETSCFREE(mat->imax); 21594473badSLois Curfman McInnes if (mat->solve_work) PETSCFREE(mat->solve_work); 21678b31e54SBarry Smith PETSCFREE(mat); 217da3a660dSBarry Smith 218*416022c9SBarry Smith PetscMemcpy(A,C,sizeof(struct _Mat)); 219*416022c9SBarry Smith PETSCHEADERDESTROY(C); 220da3a660dSBarry Smith return 0; 221da3a660dSBarry Smith } 2220a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 223*416022c9SBarry Smith int MatSolve_SeqAIJ(Mat A,Vec bb, Vec xx) 2248c37ef55SBarry Smith { 225*416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 226*416022c9SBarry Smith IS iscol = a->col, isrow = a->row; 227*416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 228*416022c9SBarry Smith int nz,shift = a->indexshift; 229*416022c9SBarry Smith Scalar *x,*b,*tmp, *tmps, *aa = a->a, sum, *v; 2308c37ef55SBarry Smith 231*416022c9SBarry Smith if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolve_SeqAIJ:Not for unfactored matrix"); 2329e25ed09SBarry Smith 23378b31e54SBarry Smith ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 23478b31e54SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 235*416022c9SBarry Smith tmp = a->solve_work; 2368c37ef55SBarry Smith 23778b31e54SBarry Smith ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 23878b31e54SBarry Smith ierr = ISGetIndices(iscol,&c);CHKERRQ(ierr); c = c + (n-1); 2398c37ef55SBarry Smith 2408c37ef55SBarry Smith /* forward solve the lower triangular */ 2418c37ef55SBarry Smith tmp[0] = b[*r++]; 2420cf60383SBarry Smith tmps = tmp + shift; 2438c37ef55SBarry Smith for ( i=1; i<n; i++ ) { 244dbb450caSBarry Smith v = aa + ai[i] + shift; 245dbb450caSBarry Smith vi = aj + ai[i] + shift; 246*416022c9SBarry Smith nz = a->diag[i] - ai[i]; 2478c37ef55SBarry Smith sum = b[*r++]; 2480cf60383SBarry Smith while (nz--) sum -= *v++ * tmps[*vi++]; 2498c37ef55SBarry Smith tmp[i] = sum; 2508c37ef55SBarry Smith } 2518c37ef55SBarry Smith 2528c37ef55SBarry Smith /* backward solve the upper triangular */ 2538c37ef55SBarry Smith for ( i=n-1; i>=0; i-- ){ 254*416022c9SBarry Smith v = aa + a->diag[i] + (!shift); 255*416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 256*416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 2578c37ef55SBarry Smith sum = tmp[i]; 2580cf60383SBarry Smith while (nz--) sum -= *v++ * tmps[*vi++]; 259*416022c9SBarry Smith x[*c--] = tmp[i] = sum*aa[a->diag[i]+shift]; 2608c37ef55SBarry Smith } 2618c37ef55SBarry Smith 262898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 263898183ecSLois Curfman McInnes ierr = ISRestoreIndices(iscol,&c); CHKERRQ(ierr); 264*416022c9SBarry Smith PLogFlops(2*a->nz - a->n); 2658c37ef55SBarry Smith return 0; 2668c37ef55SBarry Smith } 267*416022c9SBarry Smith int MatSolveAdd_SeqAIJ(Mat A,Vec bb, Vec yy, Vec xx) 268da3a660dSBarry Smith { 269*416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 270*416022c9SBarry Smith IS iscol = a->col, isrow = a->row; 271*416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 272*416022c9SBarry Smith int nz, shift = a->indexshift; 273*416022c9SBarry Smith Scalar *x,*b,*tmp, *aa = a->a, sum, *v; 274da3a660dSBarry Smith 275*416022c9SBarry Smith if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolveAdd_SeqAIJ:Not for unfactored matrix"); 27678b31e54SBarry Smith if (yy != xx) {ierr = VecCopy(yy,xx); CHKERRQ(ierr);} 277da3a660dSBarry Smith 27878b31e54SBarry Smith ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 27978b31e54SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 280*416022c9SBarry Smith tmp = a->solve_work; 281da3a660dSBarry Smith 28278b31e54SBarry Smith ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr); 28378b31e54SBarry Smith ierr = ISGetIndices(iscol,&c); CHKERRQ(ierr); c = c + (n-1); 284da3a660dSBarry Smith 285da3a660dSBarry Smith /* forward solve the lower triangular */ 286da3a660dSBarry Smith tmp[0] = b[*r++]; 287da3a660dSBarry Smith for ( i=1; i<n; i++ ) { 288dbb450caSBarry Smith v = aa + ai[i] + shift; 289dbb450caSBarry Smith vi = aj + ai[i] + shift; 290*416022c9SBarry Smith nz = a->diag[i] - ai[i]; 291da3a660dSBarry Smith sum = b[*r++]; 292dbb450caSBarry Smith while (nz--) sum -= *v++ * tmp[*vi++ + shift]; 293da3a660dSBarry Smith tmp[i] = sum; 294da3a660dSBarry Smith } 295da3a660dSBarry Smith 296da3a660dSBarry Smith /* backward solve the upper triangular */ 297da3a660dSBarry Smith for ( i=n-1; i>=0; i-- ){ 298*416022c9SBarry Smith v = aa + a->diag[i] + (!shift); 299*416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 300*416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 301da3a660dSBarry Smith sum = tmp[i]; 302dbb450caSBarry Smith while (nz--) sum -= *v++ * tmp[*vi++ + shift]; 303*416022c9SBarry Smith tmp[i] = sum*aa[a->diag[i]+shift]; 304da3a660dSBarry Smith x[*c--] += tmp[i]; 305da3a660dSBarry Smith } 306da3a660dSBarry Smith 307898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 308898183ecSLois Curfman McInnes ierr = ISRestoreIndices(iscol,&c); CHKERRQ(ierr); 309*416022c9SBarry Smith PLogFlops(2*a->nz); 310898183ecSLois Curfman McInnes 311da3a660dSBarry Smith return 0; 312da3a660dSBarry Smith } 313da3a660dSBarry Smith /* -------------------------------------------------------------------*/ 314*416022c9SBarry Smith int MatSolveTrans_SeqAIJ(Mat A,Vec bb, Vec xx) 315da3a660dSBarry Smith { 316*416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 317*416022c9SBarry Smith IS iscol = a->col, isrow = a->row, invisrow,inviscol; 318*416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 319*416022c9SBarry Smith int nz,shift = a->indexshift; 320*416022c9SBarry Smith Scalar *x,*b,*tmp, *aa = a->a, *v; 321da3a660dSBarry Smith 322*416022c9SBarry Smith if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolveTrans_SeqAIJ:Not unfactored matrix"); 32378b31e54SBarry Smith ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 32478b31e54SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 325*416022c9SBarry Smith tmp = a->solve_work; 326da3a660dSBarry Smith 327da3a660dSBarry Smith /* invert the permutations */ 32878b31e54SBarry Smith ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr); 32978b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr); 330da3a660dSBarry Smith 33178b31e54SBarry Smith ierr = ISGetIndices(invisrow,&r); CHKERRQ(ierr); 33278b31e54SBarry Smith ierr = ISGetIndices(inviscol,&c); CHKERRQ(ierr); 333da3a660dSBarry Smith 334da3a660dSBarry Smith /* copy the b into temp work space according to permutation */ 335da3a660dSBarry Smith for ( i=0; i<n; i++ ) tmp[c[i]] = b[i]; 336da3a660dSBarry Smith 337da3a660dSBarry Smith /* forward solve the U^T */ 338da3a660dSBarry Smith for ( i=0; i<n; i++ ) { 339*416022c9SBarry Smith v = aa + a->diag[i] + shift; 340*416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 341*416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 342da3a660dSBarry Smith tmp[i] *= *v++; 343da3a660dSBarry Smith while (nz--) { 344dbb450caSBarry Smith tmp[*vi++ + shift] -= (*v++)*tmp[i]; 345da3a660dSBarry Smith } 346da3a660dSBarry Smith } 347da3a660dSBarry Smith 348da3a660dSBarry Smith /* backward solve the L^T */ 349da3a660dSBarry Smith for ( i=n-1; i>=0; i-- ){ 350*416022c9SBarry Smith v = aa + a->diag[i] - 1 + shift; 351*416022c9SBarry Smith vi = aj + a->diag[i] - 1 + shift; 352*416022c9SBarry Smith nz = a->diag[i] - ai[i]; 353da3a660dSBarry Smith while (nz--) { 354dbb450caSBarry Smith tmp[*vi-- + shift] -= (*v--)*tmp[i]; 355da3a660dSBarry Smith } 356da3a660dSBarry Smith } 357da3a660dSBarry Smith 358da3a660dSBarry Smith /* copy tmp into x according to permutation */ 359da3a660dSBarry Smith for ( i=0; i<n; i++ ) x[r[i]] = tmp[i]; 360da3a660dSBarry Smith 361898183ecSLois Curfman McInnes ierr = ISRestoreIndices(invisrow,&r); CHKERRQ(ierr); 362898183ecSLois Curfman McInnes ierr = ISRestoreIndices(inviscol,&c); CHKERRQ(ierr); 363898183ecSLois Curfman McInnes ierr = ISDestroy(invisrow); CHKERRQ(ierr); 364898183ecSLois Curfman McInnes ierr = ISDestroy(inviscol); CHKERRQ(ierr); 365da3a660dSBarry Smith 366*416022c9SBarry Smith PLogFlops(2*a->nz-a->n); 367da3a660dSBarry Smith return 0; 368da3a660dSBarry Smith } 369da3a660dSBarry Smith 370*416022c9SBarry Smith int MatSolveTransAdd_SeqAIJ(Mat A,Vec bb, Vec zz,Vec xx) 371da3a660dSBarry Smith { 372*416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 373*416022c9SBarry Smith IS iscol = a->col, isrow = a->row, invisrow,inviscol; 374*416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 375*416022c9SBarry Smith int nz,shift = a->indexshift; 376*416022c9SBarry Smith Scalar *x,*b,*tmp, *aa = a->a, *v; 3776abc6512SBarry Smith 378*416022c9SBarry Smith if (A->factor != FACTOR_LU)SETERRQ(1,"MatSolveTransAdd_SeqAIJ:Not unfactored matrix"); 3796abc6512SBarry Smith if (zz != xx) VecCopy(zz,xx); 3806abc6512SBarry Smith 38178b31e54SBarry Smith ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 38278b31e54SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 383*416022c9SBarry Smith tmp = a->solve_work; 3846abc6512SBarry Smith 3856abc6512SBarry Smith /* invert the permutations */ 38678b31e54SBarry Smith ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr); 38778b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr); 38878b31e54SBarry Smith ierr = ISGetIndices(invisrow,&r); CHKERRQ(ierr); 38978b31e54SBarry Smith ierr = ISGetIndices(inviscol,&c); CHKERRQ(ierr); 3906abc6512SBarry Smith 3916abc6512SBarry Smith /* copy the b into temp work space according to permutation */ 3926abc6512SBarry Smith for ( i=0; i<n; i++ ) tmp[c[i]] = b[i]; 3936abc6512SBarry Smith 3946abc6512SBarry Smith /* forward solve the U^T */ 3956abc6512SBarry Smith for ( i=0; i<n; i++ ) { 396*416022c9SBarry Smith v = aa + a->diag[i] + shift; 397*416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 398*416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 3996abc6512SBarry Smith tmp[i] *= *v++; 4006abc6512SBarry Smith while (nz--) { 401dbb450caSBarry Smith tmp[*vi++ + shift] -= (*v++)*tmp[i]; 4026abc6512SBarry Smith } 4036abc6512SBarry Smith } 4046abc6512SBarry Smith 4056abc6512SBarry Smith /* backward solve the L^T */ 4066abc6512SBarry Smith for ( i=n-1; i>=0; i-- ){ 407*416022c9SBarry Smith v = aa + a->diag[i] - 1 + shift; 408*416022c9SBarry Smith vi = aj + a->diag[i] - 1 + shift; 409*416022c9SBarry Smith nz = a->diag[i] - ai[i]; 4106abc6512SBarry Smith while (nz--) { 411dbb450caSBarry Smith tmp[*vi-- + shift] -= (*v--)*tmp[i]; 4126abc6512SBarry Smith } 4136abc6512SBarry Smith } 4146abc6512SBarry Smith 4156abc6512SBarry Smith /* copy tmp into x according to permutation */ 4166abc6512SBarry Smith for ( i=0; i<n; i++ ) x[r[i]] += tmp[i]; 4176abc6512SBarry Smith 418898183ecSLois Curfman McInnes ierr = ISRestoreIndices(invisrow,&r); CHKERRQ(ierr); 419898183ecSLois Curfman McInnes ierr = ISRestoreIndices(inviscol,&c); CHKERRQ(ierr); 420898183ecSLois Curfman McInnes ierr = ISDestroy(invisrow); CHKERRQ(ierr); 421898183ecSLois Curfman McInnes ierr = ISDestroy(inviscol); CHKERRQ(ierr); 4226abc6512SBarry Smith 423*416022c9SBarry Smith PLogFlops(2*a->nz); 4246abc6512SBarry Smith return 0; 425da3a660dSBarry Smith } 4269e25ed09SBarry Smith /* ----------------------------------------------------------------*/ 427*416022c9SBarry Smith int MatILUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f, 42849d8b64dSBarry Smith int levels,Mat *fact) 4299e25ed09SBarry Smith { 430*416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b; 4319e25ed09SBarry Smith IS isicol; 432*416022c9SBarry Smith int *r,*ic, ierr, prow, n = a->m, *ai = a->i, *aj = a->j; 43356beaf04SBarry Smith int *ainew,*ajnew, jmax,*fill, *xi, nz, *im,*ajfill,*flev; 43456beaf04SBarry Smith int *dloc, idx, row,m,fm, nzf, nzi,len, realloc = 0; 435*416022c9SBarry Smith int incrlev,nnz,i,shift = a->indexshift; 4369e25ed09SBarry Smith 437*416022c9SBarry Smith if (n != a->n) SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:Matrix must be square"); 438*416022c9SBarry Smith if (!isrow) SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:Must have row permutation"); 439*416022c9SBarry Smith if (!iscol) SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:Must have column permutation"); 4409e25ed09SBarry Smith 44178b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr); 442898183ecSLois Curfman McInnes ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr); 443898183ecSLois Curfman McInnes ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr); 4449e25ed09SBarry Smith 4459e25ed09SBarry Smith /* get new row pointers */ 44678b31e54SBarry Smith ainew = (int *) PETSCMALLOC( (n+1)*sizeof(int) ); CHKPTRQ(ainew); 447dbb450caSBarry Smith ainew[0] = -shift; 4489e25ed09SBarry Smith /* don't know how many column pointers are needed so estimate */ 449dbb450caSBarry Smith jmax = (int) (f*(ai[n]+!shift)); 45078b31e54SBarry Smith ajnew = (int *) PETSCMALLOC( (jmax)*sizeof(int) ); CHKPTRQ(ajnew); 4519e25ed09SBarry Smith /* ajfill is level of fill for each fill entry */ 45278b31e54SBarry Smith ajfill = (int *) PETSCMALLOC( (jmax)*sizeof(int) ); CHKPTRQ(ajfill); 4539e25ed09SBarry Smith /* fill is a linked list of nonzeros in active row */ 45456beaf04SBarry Smith fill = (int *) PETSCMALLOC( (n+1)*sizeof(int)); CHKPTRQ(fill); 45556beaf04SBarry Smith /* im is level for each filled value */ 45656beaf04SBarry Smith im = (int *) PETSCMALLOC( (n+1)*sizeof(int)); CHKPTRQ(im); 45756beaf04SBarry Smith /* dloc is location of diagonal in factor */ 45856beaf04SBarry Smith dloc = (int *) PETSCMALLOC( (n+1)*sizeof(int)); CHKPTRQ(dloc); 45956beaf04SBarry Smith dloc[0] = 0; 4609e25ed09SBarry Smith 46156beaf04SBarry Smith for ( prow=0; prow<n; prow++ ) { 4629e25ed09SBarry Smith /* first copy previous fill into linked list */ 46356beaf04SBarry Smith nzf = nz = ai[r[prow]+1] - ai[r[prow]]; 464dbb450caSBarry Smith xi = aj + ai[r[prow]] + shift; 4659e25ed09SBarry Smith fill[n] = n; 4669e25ed09SBarry Smith while (nz--) { 4679e25ed09SBarry Smith fm = n; 468dbb450caSBarry Smith idx = ic[*xi++ + shift]; 4699e25ed09SBarry Smith do { 4709e25ed09SBarry Smith m = fm; 4719e25ed09SBarry Smith fm = fill[m]; 4729e25ed09SBarry Smith } while (fm < idx); 4739e25ed09SBarry Smith fill[m] = idx; 4749e25ed09SBarry Smith fill[idx] = fm; 47556beaf04SBarry Smith im[idx] = 0; 4769e25ed09SBarry Smith } 47756beaf04SBarry Smith nzi = 0; 4789e25ed09SBarry Smith row = fill[n]; 47956beaf04SBarry Smith while ( row < prow ) { 48056beaf04SBarry Smith incrlev = im[row] + 1; 48156beaf04SBarry Smith nz = dloc[row]; 482dbb450caSBarry Smith xi = ajnew + ainew[row] + shift + nz; 483dbb450caSBarry Smith flev = ajfill + ainew[row] + shift + nz + 1; 484*416022c9SBarry Smith nnz = ainew[row+1] - ainew[row] - nz - 1; 485dbb450caSBarry Smith if (*xi++ + shift != row) { 486ec8511deSBarry Smith SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:zero pivot"); 48756beaf04SBarry Smith } 48856beaf04SBarry Smith fm = row; 48956beaf04SBarry Smith while (nnz-- > 0) { 490dbb450caSBarry Smith idx = *xi++ + shift; 49156beaf04SBarry Smith if (*flev + incrlev > levels) { 49256beaf04SBarry Smith flev++; 49356beaf04SBarry Smith continue; 49456beaf04SBarry Smith } 49556beaf04SBarry Smith do { 4969e25ed09SBarry Smith m = fm; 4979e25ed09SBarry Smith fm = fill[m]; 49856beaf04SBarry Smith } while (fm < idx); 4999e25ed09SBarry Smith if (fm != idx) { 50056beaf04SBarry Smith im[idx] = *flev + incrlev; 5019e25ed09SBarry Smith fill[m] = idx; 5029e25ed09SBarry Smith fill[idx] = fm; 5039e25ed09SBarry Smith fm = idx; 50456beaf04SBarry Smith nzf++; 5059e25ed09SBarry Smith } 50656beaf04SBarry Smith else { 50756beaf04SBarry Smith if (im[idx] > *flev + incrlev) im[idx] = *flev+incrlev; 5089e25ed09SBarry Smith } 50956beaf04SBarry Smith flev++; 5109e25ed09SBarry Smith } 5119e25ed09SBarry Smith row = fill[row]; 51256beaf04SBarry Smith nzi++; 5139e25ed09SBarry Smith } 5149e25ed09SBarry Smith /* copy new filled row into permanent storage */ 51556beaf04SBarry Smith ainew[prow+1] = ainew[prow] + nzf; 51656beaf04SBarry Smith if (ainew[prow+1] > jmax+1) { 5179e25ed09SBarry Smith /* allocate a longer ajnew */ 518bbb6d6a8SBarry Smith int maxadd; 519dbb450caSBarry Smith maxadd = (int) ((f*(ai[n]+!shift)*(n-prow+5))/n); 52056beaf04SBarry Smith if (maxadd < nzf) maxadd = (n-prow)*(nzf+1); 521bbb6d6a8SBarry Smith jmax += maxadd; 52256beaf04SBarry Smith xi = (int *) PETSCMALLOC( jmax*sizeof(int) );CHKPTRQ(xi); 523*416022c9SBarry Smith PetscMemcpy(xi,ajnew,(ainew[prow]+shift)*sizeof(int)); 52478b31e54SBarry Smith PETSCFREE(ajnew); 52556beaf04SBarry Smith ajnew = xi; 5269e25ed09SBarry Smith /* allocate a longer ajfill */ 52756beaf04SBarry Smith xi = (int *) PETSCMALLOC( jmax*sizeof(int) );CHKPTRQ(xi); 528*416022c9SBarry Smith PetscMemcpy(xi,ajfill,(ainew[prow]+shift)*sizeof(int)); 52978b31e54SBarry Smith PETSCFREE(ajfill); 53056beaf04SBarry Smith ajfill = xi; 531bbb6d6a8SBarry Smith realloc++; 5329e25ed09SBarry Smith } 533dbb450caSBarry Smith xi = ajnew + ainew[prow] + shift; 534dbb450caSBarry Smith flev = ajfill + ainew[prow] + shift; 53556beaf04SBarry Smith dloc[prow] = nzi; 5369e25ed09SBarry Smith fm = fill[n]; 53756beaf04SBarry Smith while (nzf--) { 538dbb450caSBarry Smith *xi++ = fm - shift; 53956beaf04SBarry Smith *flev++ = im[fm]; 5409e25ed09SBarry Smith fm = fill[fm]; 5419e25ed09SBarry Smith } 5429e25ed09SBarry Smith } 54378b31e54SBarry Smith PETSCFREE(ajfill); 544898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 545898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr); 546898183ecSLois Curfman McInnes ierr = ISDestroy(isicol); CHKERRQ(ierr); 547898183ecSLois Curfman McInnes PETSCFREE(fill); PETSCFREE(im); 5489e25ed09SBarry Smith 549*416022c9SBarry Smith PLogInfo((PetscObject)A, 550ec8511deSBarry Smith "Info:MatILUFactorSymbolic_SeqAIJ:Realloc %d Fill ratio:given %g needed %g\n", 55156beaf04SBarry Smith realloc,f,((double)ainew[n])/((double)ai[prow])); 552bbb6d6a8SBarry Smith 5539e25ed09SBarry Smith /* put together the new matrix */ 554*416022c9SBarry Smith ierr = MatCreateSeqAIJ(A->comm,n, n, 0, 0, fact); CHKERRQ(ierr); 555*416022c9SBarry Smith b = (Mat_SeqAIJ *) (*fact)->data; 556*416022c9SBarry Smith PETSCFREE(b->imax); 557*416022c9SBarry Smith b->singlemalloc = 0; 558dbb450caSBarry Smith len = (ainew[n] + shift)*sizeof(Scalar); 5599e25ed09SBarry Smith /* the next line frees the default space generated by the Create() */ 560*416022c9SBarry Smith PETSCFREE(b->a); PETSCFREE(b->ilen); 561*416022c9SBarry Smith b->a = (Scalar *) PETSCMALLOC( len ); CHKPTRQ(b->a); 562*416022c9SBarry Smith b->j = ajnew; 563*416022c9SBarry Smith b->i = ainew; 56456beaf04SBarry Smith for ( i=0; i<n; i++ ) dloc[i] += ainew[i]; 565*416022c9SBarry Smith b->diag = dloc; 566*416022c9SBarry Smith b->ilen = 0; 567*416022c9SBarry Smith b->imax = 0; 568*416022c9SBarry Smith b->row = isrow; 569*416022c9SBarry Smith b->col = iscol; 570*416022c9SBarry Smith b->solve_work = (Scalar *) PETSCMALLOC( (n+1)*sizeof(Scalar)); 571*416022c9SBarry Smith CHKPTRQ(b->solve_work); 572*416022c9SBarry Smith /* In b structure: Free imax, ilen, old a, old j. 57356beaf04SBarry Smith Allocate dloc, solve_work, new a, new j */ 574dbb450caSBarry Smith PLogObjectMemory(*fact,(ainew[n]+shift-n) * (sizeof(int)+sizeof(Scalar))); 575*416022c9SBarry Smith b->maxnz = b->nz = ainew[n] + shift; 5769e25ed09SBarry Smith (*fact)->factor = FACTOR_LU; 5779e25ed09SBarry Smith return 0; 5789e25ed09SBarry Smith } 579