#ifndef lint static char vcid[] = "$Id: aijfact.c,v 1.17 1995/05/21 14:04:20 curfman Exp curfman $"; #endif #include "aij.h" #include "inline/spops.h" /* Factorization code for AIJ format. */ int MatLUFactorSymbolic_AIJ(Mat mat,IS isrow,IS iscol,Mat *fact) { Mat_AIJ *aij = (Mat_AIJ *) 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");} ierr = ISInvertPermutation(iscol,&isicol); CHKERR(ierr); 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(mat->comm,n, n, 0, 0, fact); CHKERR(ierr); aijnew = (Mat_AIJ *) (*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; aijnew->row = isrow; aijnew->col = iscol; /* aijnew->mem += something */ (*fact)->factor = FACTOR_LU; aijnew->solve_work = (Scalar *) MALLOC( n*sizeof(Scalar)); CHKPTR(aijnew->solve_work); /* Cannot do this here because child is destroyed before parent created PLogObjectParent(*fact,isicol); */ return 0; } int MatLUFactorNumeric_AIJ(Mat mat,Mat *infact) { Mat fact = *infact; Mat_AIJ *aij = (Mat_AIJ *) mat->data, *aijnew = (Mat_AIJ *)fact->data; IS iscol = aijnew->col, isrow = aijnew->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; ierr = ISInvertPermutation(iscol,&isicol); CHKERR(ierr); PLogObjectParent(*infact,isicol); 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; PLogFlops(2*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; aijnew->nz = ai[aijnew->n]; PLogFlops(aijnew->n); return 0; } int MatLUFactor_AIJ(Mat matin,IS row,IS col) { Mat_AIJ *mat = (Mat_AIJ *) matin->data; int ierr; Mat fact; ierr = MatLUFactorSymbolic_AIJ(matin,row,col,&fact); CHKERR(ierr); ierr = MatLUFactorNumeric_AIJ(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 (mat->row && mat->col && mat->row != mat->col) { ISDestroy(mat->row); } if (mat->col) ISDestroy(mat->col); FREE(mat); MEMCPY(matin,fact,sizeof(struct _Mat)); FREE(fact); return 0; } int MatSolve_AIJ(Mat mat,Vec bb, Vec xx) { Mat_AIJ *aij = (Mat_AIJ *) mat->data; IS iscol = aij->col, isrow = aij->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"); ierr = VecGetArray(bb,&b); CHKERR(ierr); ierr = VecGetArray(xx,&x); CHKERR(ierr); tmp = aij->solve_work; ierr = ISGetIndices(isrow,&r);CHKERR(ierr); ierr = ISGetIndices(iscol,&c);CHKERR(ierr); 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]; } PLogFlops(2*aij->nz - aij->n); return 0; } int MatSolveAdd_AIJ(Mat mat,Vec bb, Vec yy, Vec xx) { Mat_AIJ *aij = (Mat_AIJ *) mat->data; IS iscol = aij->col, isrow = aij->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);} ierr = VecGetArray(bb,&b); CHKERR(ierr); ierr = VecGetArray(xx,&x); CHKERR(ierr); tmp = aij->solve_work; ierr = ISGetIndices(isrow,&r); CHKERR(ierr); ierr = ISGetIndices(iscol,&c); CHKERR(ierr); 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]; } PLogFlops(2*aij->nz); return 0; } /* -------------------------------------------------------------------*/ int MatSolveTrans_AIJ(Mat mat,Vec bb, Vec xx) { Mat_AIJ *aij = (Mat_AIJ *) mat->data; IS iscol = aij->col, isrow = aij->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"); ierr = VecGetArray(bb,&b); CHKERR(ierr); ierr = VecGetArray(xx,&x); CHKERR(ierr); tmp = aij->solve_work; /* invert the permutations */ ierr = ISInvertPermutation(isrow,&invisrow); CHKERR(ierr); ierr = ISInvertPermutation(iscol,&inviscol); CHKERR(ierr); ierr = ISGetIndices(invisrow,&r); CHKERR(ierr); ierr = ISGetIndices(inviscol,&c); CHKERR(ierr); /* 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; inz-aij->n); return 0; } int MatSolveTransAdd_AIJ(Mat mat,Vec bb, Vec zz,Vec xx) { Mat_AIJ *aij = (Mat_AIJ *) mat->data; IS iscol = aij->col, isrow = aij->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); ierr = VecGetArray(bb,&b); CHKERR(ierr); ierr = VecGetArray(xx,&x); CHKERR(ierr); tmp = aij->solve_work; /* invert the permutations */ ierr = ISInvertPermutation(isrow,&invisrow); CHKERR(ierr); ierr = ISInvertPermutation(iscol,&inviscol); CHKERR(ierr); ierr = ISGetIndices(invisrow,&r); CHKERR(ierr); ierr = ISGetIndices(inviscol,&c); CHKERR(ierr); /* 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; inz); return 0; } /* ----------------------------------------------------------------*/ int MatILUFactorSymbolic_AIJ(Mat mat,IS isrow,IS iscol,int levels,Mat *fact) { Mat_AIJ *aij = (Mat_AIJ *) 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, *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");} ierr = ISInvertPermutation(iscol,&isicol); CHKERR(ierr); 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(mat->comm,n, n, 0, 0, fact); CHKERR(ierr); aijnew = (Mat_AIJ *) (*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; aijnew->row = isrow; aijnew->col = iscol; aijnew->solve_work = (Scalar *) MALLOC( n*sizeof(Scalar)); CHKPTR(aijnew->solve_work); (*fact)->factor = FACTOR_LU; return 0; }