#ifndef lint static char vcid[] = "$Id: aijfact.c,v 1.9 1995/03/06 04:02:49 bsmith Exp bsmith $"; #endif #include "aij.h" #include "inline/spops.h" /* Factorization code for AIJ format. */ int MatiAIJLUFactorSymbolic(Mat mat,IS isrow,IS iscol,Mat *fact) { Matiaij *aij = (Matiaij *) mat->data, *aijnew; IS isicol; int *r,*ic, ierr, i, n = aij->m, *ai = aij->i, *aj = aij->j; int *ainew,*ajnew, jmax,*fill, *ajtmp, nz; int *idnew, idx, row,m,fm, nnz, nzi,len; if (n != aij->n) SETERR(1,"Mat must be square"); if (!isrow) {SETERR(1,"Must have row permutation");} if (!iscol) {SETERR(1,"Must have column permutation");} if ((ierr = ISInvertPermutation(iscol,&isicol))) SETERR(ierr,0); ISGetIndices(isrow,&r); ISGetIndices(isicol,&ic); /* get new row pointers */ ainew = (int *) MALLOC( (n+1)*sizeof(int) ); CHKPTR(ainew); ainew[0] = 1; /* don't know how many column pointers are needed so estimate */ jmax = 2*ai[n]; ajnew = (int *) MALLOC( (jmax)*sizeof(int) ); CHKPTR(ajnew); /* fill is a linked list of nonzeros in active row */ fill = (int *) MALLOC( (n+1)*sizeof(int)); CHKPTR(fill); /* idnew is location of diagonal in factor */ idnew = (int *) MALLOC( (n+1)*sizeof(int)); CHKPTR(idnew); idnew[0] = 1; for ( i=0; i jmax+1) { /* allocate a longer ajnew */ jmax += nnz*(n-i); ajtmp = (int *) MALLOC( jmax*sizeof(int) );CHKPTR(ajtmp); MEMCPY(ajtmp,ajnew,(ainew[i]-1)*sizeof(int)); FREE(ajnew); ajnew = ajtmp; } ajtmp = ajnew + ainew[i] - 1; fm = fill[n]; nzi = 0; while (nnz--) { if (fm < i) nzi++; *ajtmp++ = fm + 1; fm = fill[fm]; } idnew[i] = ainew[i] + nzi; } ISDestroy(isicol); FREE(fill); /* put together the new matrix */ ierr = MatCreateSequentialAIJ(n, n, 0, 0, fact); CHKERR(ierr); aijnew = (Matiaij *) (*fact)->data; FREE(aijnew->imax); aijnew->singlemalloc = 0; len = (ainew[n] - 1)*sizeof(Scalar); /* the next line frees the default space generated by the Create() */ FREE(aijnew->a); FREE(aijnew->ilen); aijnew->a = (Scalar *) MALLOC( len ); CHKPTR(aijnew->a); aijnew->j = ajnew; aijnew->i = ainew; aijnew->diag = idnew; aijnew->ilen = 0; aijnew->imax = 0; (*fact)->row = isrow; (*fact)->col = iscol; (*fact)->factor = FACTOR_LU; return 0; } int MatiAIJLUFactorNumeric(Mat mat,Mat *infact) { Mat fact = *infact; Matiaij *aij = (Matiaij *) mat->data, *aijnew = (Matiaij *)fact->data; IS iscol = fact->col, isrow = fact->row, isicol; int *r,*ic, ierr, i, j, n = aij->m, *ai = aijnew->i, *aj = aijnew->j; int *ajtmpold, *ajtmp, nz, row,*pj; Scalar *rtmp,*v, *pv, *pc, multiplier; if ((ierr = ISInvertPermutation(iscol,&isicol))) SETERR(ierr,0); ierr = ISGetIndices(isrow,&r); CHKERR(ierr); ierr = ISGetIndices(isicol,&ic); CHKERR(ierr); rtmp = (Scalar *) MALLOC( (n+1)*sizeof(Scalar) ); CHKPTR(rtmp); for ( i=0; ii[r[i]+1] - aij->i[r[i]]; ajtmpold = aij->j + aij->i[r[i]] - 1; v = aij->a + aij->i[r[i]] - 1; for ( j=0; jdiag[row] - ai[row]; pv = aijnew->a + aijnew->diag[row] - 1; pj = aijnew->j + aijnew->diag[row]; multiplier = *pc * *pv++; *pc = multiplier; nz = ai[row+1] - ai[row] - 1 - nz; while (nz-->0) rtmp[*pj++ - 1] -= multiplier* *pv++; } row = *ajtmp++ - 1; } /* finished row so stick it into aijnew->a */ pv = aijnew->a + ai[i] - 1; pj = aijnew->j + ai[i] - 1; nz = ai[i+1] - ai[i]; rtmp[i] = 1.0/rtmp[i]; for ( j=0; jfactor = FACTOR_LU; aijnew->assembled = 1; return 0; } int MatiAIJLUFactor(Mat matin,IS row,IS col) { Matiaij *mat = (Matiaij *) matin->data; int ierr; Mat fact; ierr = MatiAIJLUFactorSymbolic(matin,row,col,&fact); CHKERR(ierr); ierr = MatiAIJLUFactorNumeric(matin,&fact); CHKERR(ierr); /* free all the data structures from mat */ FREE(mat->a); if (!mat->singlemalloc) {FREE(mat->i); FREE(mat->j);} if (mat->diag) FREE(mat->diag); if (mat->ilen) FREE(mat->ilen); if (mat->imax) FREE(mat->imax); if (matin->row && matin->col && matin->row != matin->col) { ISDestroy(matin->row); } if (matin->col) ISDestroy(matin->col); FREE(mat); MEMCPY(matin,fact,sizeof(struct _Mat)); FREE(fact); return 0; } int MatiAIJSolve(Mat mat,Vec bb, Vec xx) { Matiaij *aij = (Matiaij *) mat->data; IS iscol = mat->col, isrow = mat->row; int *r,*c, ierr, i, n = aij->m, *vi, *ai = aij->i, *aj = aij->j; int nz; Scalar *x,*b,*tmp, *aa = aij->a, sum, *v; if (mat->factor != FACTOR_LU) SETERR(1,"Cannot solve with factor"); if ((ierr = VecGetArray(bb,&b))) SETERR(ierr,0); if ((ierr = VecGetArray(xx,&x))) SETERR(ierr,0); tmp = (Scalar *) MALLOC(n*sizeof(Scalar)); CHKPTR(tmp); if ((ierr = ISGetIndices(isrow,&r))) SETERR(ierr,0); if ((ierr = ISGetIndices(iscol,&c))) SETERR(ierr,0); c = c + (n-1); /* forward solve the lower triangular */ tmp[0] = b[*r++]; for ( i=1; idiag[i] - ai[i]; sum = b[*r++]; while (nz--) sum -= *v++ * tmp[*vi++ - 1]; tmp[i] = sum; } /* backward solve the upper triangular */ for ( i=n-1; i>=0; i-- ){ v = aa + aij->diag[i]; vi = aj + aij->diag[i]; nz = ai[i+1] - aij->diag[i] - 1; sum = tmp[i]; while (nz--) sum -= *v++ * tmp[*vi++ - 1]; x[*c--] = tmp[i] = sum*aa[aij->diag[i]-1]; } FREE(tmp); return 0; } int MatiAIJSolveAdd(Mat mat,Vec bb, Vec yy, Vec xx) { Matiaij *aij = (Matiaij *) mat->data; IS iscol = mat->col, isrow = mat->row; int *r,*c, ierr, i, n = aij->m, *vi, *ai = aij->i, *aj = aij->j; int nz; Scalar *x,*b,*tmp, *aa = aij->a, sum, *v; if (mat->factor != FACTOR_LU) SETERR(1,"Cannot solve with factor"); if (yy != xx) {ierr = VecCopy(yy,xx); CHKERR(ierr);} if ((ierr = VecGetArray(bb,&b))) SETERR(ierr,0); if ((ierr = VecGetArray(xx,&x))) SETERR(ierr,0); tmp = (Scalar *) MALLOC(n*sizeof(Scalar)); CHKPTR(tmp); if ((ierr = ISGetIndices(isrow,&r))) SETERR(ierr,0); if ((ierr = ISGetIndices(iscol,&c))) SETERR(ierr,0); c = c + (n-1); /* forward solve the lower triangular */ tmp[0] = b[*r++]; for ( i=1; idiag[i] - ai[i]; sum = b[*r++]; while (nz--) sum -= *v++ * tmp[*vi++ - 1]; tmp[i] = sum; } /* backward solve the upper triangular */ for ( i=n-1; i>=0; i-- ){ v = aa + aij->diag[i]; vi = aj + aij->diag[i]; nz = ai[i+1] - aij->diag[i] - 1; sum = tmp[i]; while (nz--) sum -= *v++ * tmp[*vi++ - 1]; tmp[i] = sum*aa[aij->diag[i]-1]; x[*c--] += tmp[i]; } FREE(tmp); return 0; } /* -------------------------------------------------------------------*/ int MatiAIJSolveTrans(Mat mat,Vec bb, Vec xx) { Matiaij *aij = (Matiaij *) mat->data; IS iscol = mat->col, isrow = mat->row, invisrow,inviscol; int *r,*c, ierr, i, n = aij->m, *vi, *ai = aij->i, *aj = aij->j; int nz; Scalar *x,*b,*tmp, *aa = aij->a, *v; if (mat->factor != FACTOR_LU) SETERR(1,"Cannot solve with factor"); if ((ierr = VecGetArray(bb,&b))) SETERR(ierr,0); if ((ierr = VecGetArray(xx,&x))) SETERR(ierr,0); tmp = (Scalar *) MALLOC(n*sizeof(Scalar)); CHKPTR(tmp); /* invert the permutations */ ierr = ISInvertPermutation(isrow,&invisrow); CHKERR(ierr); ierr = ISInvertPermutation(iscol,&inviscol); CHKERR(ierr); if ((ierr = ISGetIndices(invisrow,&r))) SETERR(ierr,0); if ((ierr = ISGetIndices(inviscol,&c))) SETERR(ierr,0); /* copy the b into temp work space according to permutation */ for ( i=0; idiag[i] - 1; vi = aj + aij->diag[i]; nz = ai[i+1] - aij->diag[i] - 1; tmp[i] *= *v++; while (nz--) { tmp[*vi++ - 1] -= (*v++)*tmp[i]; } } /* backward solve the L^T */ for ( i=n-1; i>=0; i-- ){ v = aa + aij->diag[i] - 2; vi = aj + aij->diag[i] - 2; nz = aij->diag[i] - ai[i]; while (nz--) { tmp[*vi-- - 1] -= (*v--)*tmp[i]; } } /* copy tmp into x according to permutation */ for ( i=0; idata; IS iscol = mat->col, isrow = mat->row, invisrow,inviscol; int *r,*c, ierr, i, n = aij->m, *vi, *ai = aij->i, *aj = aij->j; int nz; Scalar *x,*b,*tmp, *aa = aij->a, *v; if (mat->factor != FACTOR_LU) SETERR(1,"Cannot solve with factor"); if (zz != xx) VecCopy(zz,xx); if ((ierr = VecGetArray(bb,&b))) SETERR(ierr,0); if ((ierr = VecGetArray(xx,&x))) SETERR(ierr,0); tmp = (Scalar *) MALLOC(n*sizeof(Scalar)); CHKPTR(tmp); /* invert the permutations */ ierr = ISInvertPermutation(isrow,&invisrow); CHKERR(ierr); ierr = ISInvertPermutation(iscol,&inviscol); CHKERR(ierr); if ((ierr = ISGetIndices(invisrow,&r))) SETERR(ierr,0); if ((ierr = ISGetIndices(inviscol,&c))) SETERR(ierr,0); /* copy the b into temp work space according to permutation */ for ( i=0; idiag[i] - 1; vi = aj + aij->diag[i]; nz = ai[i+1] - aij->diag[i] - 1; tmp[i] *= *v++; while (nz--) { tmp[*vi++ - 1] -= (*v++)*tmp[i]; } } /* backward solve the L^T */ for ( i=n-1; i>=0; i-- ){ v = aa + aij->diag[i] - 2; vi = aj + aij->diag[i] - 2; nz = aij->diag[i] - ai[i]; while (nz--) { tmp[*vi-- - 1] -= (*v--)*tmp[i]; } } /* copy tmp into x according to permutation */ for ( i=0; idata, *aijnew; IS isicol; int *r,*ic, ierr, i, n = aij->m, *ai = aij->i, *aj = aij->j; int *ainew,*ajnew, jmax,*fill, *ajtmp, nz, *lfill,*ajfill,*ajtmpf; int *idnew, idx, row,m,fm, nnz, nzi,len; if (n != aij->n) SETERR(1,"Mat must be square"); if (!isrow) {SETERR(1,"Must have row permutation");} if (!iscol) {SETERR(1,"Must have column permutation");} if ((ierr = ISInvertPermutation(iscol,&isicol))) SETERR(ierr,0); ISGetIndices(isrow,&r); ISGetIndices(isicol,&ic); /* get new row pointers */ ainew = (int *) MALLOC( (n+1)*sizeof(int) ); CHKPTR(ainew); ainew[0] = 1; /* don't know how many column pointers are needed so estimate */ jmax = 2*ai[n]; ajnew = (int *) MALLOC( (jmax)*sizeof(int) ); CHKPTR(ajnew); /* ajfill is level of fill for each fill entry */ ajfill = (int *) MALLOC( (jmax)*sizeof(int) ); CHKPTR(ajfill); /* fill is a linked list of nonzeros in active row */ fill = (int *) MALLOC( (n+1)*sizeof(int)); CHKPTR(fill); /* lfill is level for each filled value */ lfill = (int *) MALLOC( (n+1)*sizeof(int)); CHKPTR(lfill); /* idnew is location of diagonal in factor */ idnew = (int *) MALLOC( (n+1)*sizeof(int)); CHKPTR(idnew); idnew[0] = 1; for ( i=0; i jmax+1) { /* allocate a longer ajnew */ jmax += nnz*(n-i); ajtmp = (int *) MALLOC( jmax*sizeof(int) );CHKPTR(ajtmp); MEMCPY(ajtmp,ajnew,(ainew[i]-1)*sizeof(int)); FREE(ajnew); ajnew = ajtmp; /* allocate a longer ajfill */ ajtmp = (int *) MALLOC( jmax*sizeof(int) );CHKPTR(ajtmp); MEMCPY(ajtmp,ajfill,(ainew[i]-1)*sizeof(int)); FREE(ajfill); ajfill = ajtmp; } ajtmp = ajnew + ainew[i] - 1; ajtmpf = ajfill + ainew[i] - 1; fm = fill[n]; nzi = 0; while (nnz--) { if (fm < i) nzi++; *ajtmp++ = fm + 1; *ajtmpf++ = lfill[fm]; fm = fill[fm]; } idnew[i] = ainew[i] + nzi; } FREE(ajfill); ISDestroy(isicol); FREE(fill); FREE(lfill); /* put together the new matrix */ ierr = MatCreateSequentialAIJ(n, n, 0, 0, fact); CHKERR(ierr); aijnew = (Matiaij *) (*fact)->data; FREE(aijnew->imax); aijnew->singlemalloc = 0; len = (ainew[n] - 1)*sizeof(Scalar); /* the next line frees the default space generated by the Create() */ FREE(aijnew->a); FREE(aijnew->ilen); aijnew->a = (Scalar *) MALLOC( len ); CHKPTR(aijnew->a); aijnew->j = ajnew; aijnew->i = ainew; aijnew->diag = idnew; aijnew->ilen = 0; aijnew->imax = 0; (*fact)->row = isrow; (*fact)->col = iscol; (*fact)->factor = FACTOR_LU; return 0; }