#include "src/mat/impls/baij/seq/baij.h" #include "src/mat/impls/sbaij/seq/sbaij.h" #include "src/inline/ilu.h" #include "include/petscis.h" #if !defined(PETSC_USE_COMPLEX) /* input: F -- numeric factor output: nneg, nzero, npos: matrix inertia */ #undef __FUNCT__ #define __FUNCT__ "MatGetInertia_SeqSBAIJ" PetscErrorCode MatGetInertia_SeqSBAIJ(Mat F,int *nneig,int *nzero,int *npos) { Mat_SeqSBAIJ *fact_ptr = (Mat_SeqSBAIJ*)F->data; PetscScalar *dd = fact_ptr->a; int mbs=fact_ptr->mbs,bs=fact_ptr->bs,i,nneig_tmp,npos_tmp, *fi = fact_ptr->i; PetscFunctionBegin; if (bs != 1) SETERRQ1(PETSC_ERR_SUP,"No support for bs: %d >1 yet",bs); nneig_tmp = 0; npos_tmp = 0; for (i=0; i 0.0){ npos_tmp++; } else if (PetscRealPart(dd[*fi]) < 0.0){ nneig_tmp++; } fi++; } if (nneig) *nneig = nneig_tmp; if (npos) *npos = npos_tmp; if (nzero) *nzero = mbs - nneig_tmp - npos_tmp; PetscFunctionReturn(0); } #endif /* !defined(PETSC_USE_COMPLEX) */ /* Using Modified Sparse Row (MSR) storage. See page 85, "Iterative Methods ..." by Saad. */ /* Symbolic U^T*D*U factorization for SBAIJ format. Modified from SSF of YSMP. */ /* Use Modified Sparse Row storage for u and ju, see Saad pp.85 */ #undef __FUNCT__ #define __FUNCT__ "MatCholeskyFactorSymbolic_SeqSBAIJ" PetscErrorCode MatCholeskyFactorSymbolic_SeqSBAIJ(Mat A,IS perm,MatFactorInfo *info,Mat *B) { Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data,*b; PetscErrorCode ierr; int *rip,i,mbs = a->mbs,*ai,*aj; int *jutmp,bs = a->bs,bs2=a->bs2; int m,realloc = 0,prow; int *jl,*q,jmin,jmax,juidx,nzk,qm,*iu,*ju,k,j,vj,umax,maxadd; int *il,ili,nextprow; PetscReal f = info->fill; PetscTruth perm_identity; PetscFunctionBegin; /* check whether perm is the identity mapping */ ierr = ISIdentity(perm,&perm_identity);CHKERRQ(ierr); /* -- inplace factorization, i.e., use sbaij for *B -- */ if (perm_identity && bs==1 ){ if (!perm_identity) a->permute = PETSC_TRUE; ierr = ISGetIndices(perm,&rip);CHKERRQ(ierr); if (perm_identity){ /* without permutation */ ai = a->i; aj = a->j; } else { /* non-trivial permutation */ ierr = MatReorderingSeqSBAIJ(A,perm);CHKERRQ(ierr); ai = a->inew; aj = a->jnew; } /* initialization */ ierr = PetscMalloc((mbs+1)*sizeof(int),&iu);CHKERRQ(ierr); umax = (int)(f*ai[mbs] + 1); ierr = PetscMalloc(umax*sizeof(int),&ju);CHKERRQ(ierr); iu[0] = 0; juidx = 0; /* index for ju */ ierr = PetscMalloc((3*mbs+1)*sizeof(int),&jl);CHKERRQ(ierr); /* linked list for getting pivot row */ q = jl + mbs; /* linked list for col index of active row */ il = q + mbs; for (i=0; i k){ qm = k; do { m = qm; qm = q[m]; } while(qm < vj); if (qm == vj) { SETERRQ(1," error: duplicate entry in A\n"); } nzk++; q[m] = vj; q[vj] = qm; } /* if(vj > k) */ } /* for (j=jmin; j 0){ i = q[k]; /* col value of the first nonzero element in U(k, k+1:mbs-1) */ jl[k] = jl[i]; jl[i] = k; il[k] = iu[k] + 1; } iu[k+1] = iu[k] + nzk + 1; /* include diag[k] */ /* allocate more space to ju if needed */ if (iu[k+1] > umax) { /* estimate how much additional space we will need */ /* use the strategy suggested by David Hysom */ /* just double the memory each time */ maxadd = umax; if (maxadd < nzk) maxadd = (mbs-k)*(nzk+1)/2; umax += maxadd; /* allocate a longer ju */ ierr = PetscMalloc(umax*sizeof(int),&jutmp);CHKERRQ(ierr); ierr = PetscMemcpy(jutmp,ju,iu[k]*sizeof(int));CHKERRQ(ierr); ierr = PetscFree(ju);CHKERRQ(ierr); ju = jutmp; realloc++; /* count how many times we realloc */ } /* save nonzero structure of k-th row in ju */ ju[juidx++] = k; /* diag[k] */ i = k; while (nzk --) { i = q[i]; ju[juidx++] = i; } } if (ai[mbs] != 0) { PetscReal af = ((PetscReal)iu[mbs])/((PetscReal)ai[mbs]); PetscLogInfo(A,"MatCholeskyFactorSymbolic_SeqSBAIJ:Reallocs %d Fill ratio:given %g needed %g\n",realloc,f,af); PetscLogInfo(A,"MatCholeskyFactorSymbolic_SeqSBAIJ:Run with -pc_cholesky_fill %g or use \n",af); PetscLogInfo(A,"MatCholeskyFactorSymbolic_SeqSBAIJ:PCCholeskySetFill(pc,%g);\n",af); PetscLogInfo(A,"MatCholeskyFactorSymbolic_SeqSBAIJ:for best performance.\n"); } else { PetscLogInfo(A,"MatCholeskyFactorSymbolic_SeqSBAIJ:Empty matrix.\n"); } ierr = ISRestoreIndices(perm,&rip);CHKERRQ(ierr); /* ierr = PetscFree(q);CHKERRQ(ierr); */ ierr = PetscFree(jl);CHKERRQ(ierr); /* put together the new matrix */ ierr = MatCreate(A->comm,bs*mbs,bs*mbs,bs*mbs,bs*mbs,B);CHKERRQ(ierr); ierr = MatSetType(*B,A->type_name);CHKERRQ(ierr); ierr = MatSeqSBAIJSetPreallocation(*B,bs,0,PETSC_NULL);CHKERRQ(ierr); /* PetscLogObjectParent(*B,iperm); */ b = (Mat_SeqSBAIJ*)(*B)->data; ierr = PetscFree(b->imax);CHKERRQ(ierr); b->singlemalloc = PETSC_FALSE; /* the next line frees the default space generated by the Create() */ ierr = PetscFree(b->a);CHKERRQ(ierr); ierr = PetscFree(b->ilen);CHKERRQ(ierr); ierr = PetscMalloc((iu[mbs]+1)*sizeof(MatScalar)*bs2,&b->a);CHKERRQ(ierr); b->j = ju; b->i = iu; b->diag = 0; b->ilen = 0; b->imax = 0; b->row = perm; b->pivotinblocks = PETSC_FALSE; /* need to get from MatFactorInfo */ ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); b->icol = perm; ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); ierr = PetscMalloc((bs*mbs+bs)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); /* In b structure: Free imax, ilen, old a, old j. Allocate idnew, solve_work, new a, new j */ PetscLogObjectMemory(*B,(iu[mbs]-mbs)*(sizeof(int)+sizeof(MatScalar))); b->maxnz = b->nz = iu[mbs]; (*B)->factor = FACTOR_CHOLESKY; (*B)->info.factor_mallocs = realloc; (*B)->info.fill_ratio_given = f; if (ai[mbs] != 0) { (*B)->info.fill_ratio_needed = ((PetscReal)iu[mbs])/((PetscReal)ai[mbs]); } else { (*B)->info.fill_ratio_needed = 0.0; } (*B)->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_1_NaturalOrdering; (*B)->ops->solve = MatSolve_SeqSBAIJ_1_NaturalOrdering; PetscLogInfo(A,"MatICCFactorSymbolic_SeqSBAIJ:Using special in-place natural ordering factor and solve BS=1\n"); PetscFunctionReturn(0); } /* ----------- end of new code --------------------*/ if (!perm_identity) a->permute = PETSC_TRUE; ierr = ISGetIndices(perm,&rip);CHKERRQ(ierr); if (perm_identity){ /* without permutation */ ai = a->i; aj = a->j; } else { /* non-trivial permutation */ ierr = MatReorderingSeqSBAIJ(A,perm);CHKERRQ(ierr); ai = a->inew; aj = a->jnew; } /* initialization */ ierr = PetscMalloc((mbs+1)*sizeof(int),&iu);CHKERRQ(ierr); umax = (int)(f*ai[mbs] + 1); umax += mbs + 1; ierr = PetscMalloc(umax*sizeof(int),&ju);CHKERRQ(ierr); iu[0] = mbs+1; juidx = mbs + 1; /* index for ju */ ierr = PetscMalloc(2*mbs*sizeof(int),&jl);CHKERRQ(ierr); /* linked list for pivot row */ q = jl + mbs; /* linked list for col index */ for (i=0; i k){ qm = k; do { m = qm; qm = q[m]; } while(qm < vj); if (qm == vj) { SETERRQ(1," error: duplicate entry in A\n"); } nzk++; q[m] = vj; q[vj] = qm; } /* if(vj > k) */ } /* for (j=jmin; j 0){ i = q[k]; /* col value of first nonzero element in U(k, k+1:mbs-1) */ jl[k] = jl[i]; jl[i] = k; } iu[k+1] = iu[k] + nzk; /* allocate more space to ju if needed */ if (iu[k+1] > umax) { /* estimate how much additional space we will need */ /* use the strategy suggested by David Hysom */ /* just double the memory each time */ maxadd = umax; if (maxadd < nzk) maxadd = (mbs-k)*(nzk+1)/2; umax += maxadd; /* allocate a longer ju */ ierr = PetscMalloc(umax*sizeof(int),&jutmp);CHKERRQ(ierr); ierr = PetscMemcpy(jutmp,ju,iu[k]*sizeof(int));CHKERRQ(ierr); ierr = PetscFree(ju);CHKERRQ(ierr); ju = jutmp; realloc++; /* count how many times we realloc */ } /* save nonzero structure of k-th row in ju */ i=k; while (nzk --) { i = q[i]; ju[juidx++] = i; } } if (ai[mbs] != 0) { PetscReal af = ((PetscReal)iu[mbs])/((PetscReal)ai[mbs]); PetscLogInfo(A,"MatCholeskyFactorSymbolic_SeqSBAIJ:Reallocs %d Fill ratio:given %g needed %g\n",realloc,f,af); PetscLogInfo(A,"MatCholeskyFactorSymbolic_SeqSBAIJ:Run with -pc_cholesky_fill %g or use \n",af); PetscLogInfo(A,"MatCholeskyFactorSymbolic_SeqSBAIJ:PCCholeskySetFill(pc,%g);\n",af); PetscLogInfo(A,"MatCholeskyFactorSymbolic_SeqSBAIJ:for best performance.\n"); } else { PetscLogInfo(A,"MatCholeskyFactorSymbolic_SeqSBAIJ:Empty matrix.\n"); } ierr = ISRestoreIndices(perm,&rip);CHKERRQ(ierr); /* ierr = PetscFree(q);CHKERRQ(ierr); */ ierr = PetscFree(jl);CHKERRQ(ierr); /* put together the new matrix */ ierr = MatCreate(A->comm,bs*mbs,bs*mbs,bs*mbs,bs*mbs,B);CHKERRQ(ierr); ierr = MatSetType(*B,A->type_name);CHKERRQ(ierr); ierr = MatSeqSBAIJSetPreallocation(*B,bs,0,PETSC_NULL);CHKERRQ(ierr); /* PetscLogObjectParent(*B,iperm); */ b = (Mat_SeqSBAIJ*)(*B)->data; ierr = PetscFree(b->imax);CHKERRQ(ierr); b->singlemalloc = PETSC_FALSE; /* the next line frees the default space generated by the Create() */ ierr = PetscFree(b->a);CHKERRQ(ierr); ierr = PetscFree(b->ilen);CHKERRQ(ierr); ierr = PetscMalloc((iu[mbs]+1)*sizeof(MatScalar)*bs2,&b->a);CHKERRQ(ierr); b->j = ju; b->i = iu; b->diag = 0; b->ilen = 0; b->imax = 0; b->row = perm; b->pivotinblocks = PETSC_FALSE; /* need to get from MatFactorInfo */ ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); b->icol = perm; ierr = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr); ierr = PetscMalloc((bs*mbs+bs)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr); /* In b structure: Free imax, ilen, old a, old j. Allocate idnew, solve_work, new a, new j */ PetscLogObjectMemory(*B,(iu[mbs]-mbs)*(sizeof(int)+sizeof(MatScalar))); b->maxnz = b->nz = iu[mbs]; (*B)->factor = FACTOR_CHOLESKY; (*B)->info.factor_mallocs = realloc; (*B)->info.fill_ratio_given = f; if (ai[mbs] != 0) { (*B)->info.fill_ratio_needed = ((PetscReal)iu[mbs])/((PetscReal)ai[mbs]); } else { (*B)->info.fill_ratio_needed = 0.0; } if (perm_identity){ switch (bs) { case 1: (*B)->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_1_NaturalOrdering; (*B)->ops->solve = MatSolve_SeqSBAIJ_1_NaturalOrdering; PetscLogInfo(A,"MatICCFactorSymbolic_SeqSBAIJ:Using special in-place natural ordering factor and solve BS=1\n"); break; case 2: (*B)->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_2_NaturalOrdering; (*B)->ops->solve = MatSolve_SeqSBAIJ_2_NaturalOrdering; PetscLogInfo(A,"MatICCFactorSymbolic_SeqSBAIJ:Using special in-place natural ordering factor and solve BS=2\n"); break; case 3: (*B)->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_3_NaturalOrdering; (*B)->ops->solve = MatSolve_SeqSBAIJ_3_NaturalOrdering; PetscLogInfo(A,"MatICCFactorSymbolic_SeqSBAIJ:sing special in-place natural ordering factor and solve BS=3\n"); break; case 4: (*B)->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_4_NaturalOrdering; (*B)->ops->solve = MatSolve_SeqSBAIJ_4_NaturalOrdering; PetscLogInfo(A,"MatICCFactorSymbolic_SeqSBAIJ:Using special in-place natural ordering factor and solve BS=4\n"); break; case 5: (*B)->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_5_NaturalOrdering; (*B)->ops->solve = MatSolve_SeqSBAIJ_5_NaturalOrdering; PetscLogInfo(A,"MatICCFactorSymbolic_SeqSBAIJ:Using special in-place natural ordering factor and solve BS=5\n"); break; case 6: (*B)->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_6_NaturalOrdering; (*B)->ops->solve = MatSolve_SeqSBAIJ_6_NaturalOrdering; PetscLogInfo(A,"MatICCFactorSymbolic_SeqSBAIJ:Using special in-place natural ordering factor and solve BS=6\n"); break; case 7: (*B)->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_7_NaturalOrdering; (*B)->ops->solve = MatSolve_SeqSBAIJ_7_NaturalOrdering; PetscLogInfo(A,"MatICCFactorSymbolic_SeqSBAIJ:Using special in-place natural ordering factor and solve BS=7\n"); break; default: (*B)->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_N_NaturalOrdering; (*B)->ops->solve = MatSolve_SeqSBAIJ_N_NaturalOrdering; PetscLogInfo(A,"MatICCFactorSymbolic_SeqSBAIJ:Using special in-place natural ordering factor and solve BS>7\n"); break; } } PetscFunctionReturn(0); } #undef __FUNCT__ #define __FUNCT__ "MatCholeskyFactorNumeric_SeqSBAIJ_N" PetscErrorCode MatCholeskyFactorNumeric_SeqSBAIJ_N(Mat A,Mat *B) { Mat C = *B; Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data,*b = (Mat_SeqSBAIJ *)C->data; IS perm = b->row; PetscErrorCode ierr; int *perm_ptr,i,j,mbs=a->mbs,*bi=b->i,*bj=b->j; int *ai,*aj,*a2anew,k,k1,jmin,jmax,*jl,*il,vj,nexti,ili; int bs=a->bs,bs2 = a->bs2; MatScalar *ba = b->a,*aa,*ap,*dk,*uik; MatScalar *u,*diag,*rtmp,*rtmp_ptr; MatScalar *work; int *pivots; PetscFunctionBegin; /* initialization */ ierr = PetscMalloc(bs2*mbs*sizeof(MatScalar),&rtmp);CHKERRQ(ierr); ierr = PetscMemzero(rtmp,bs2*mbs*sizeof(MatScalar));CHKERRQ(ierr); ierr = PetscMalloc(2*mbs*sizeof(int),&il);CHKERRQ(ierr); jl = il + mbs; for (i=0; ipermute){ ai = a->i; aj = a->j; aa = a->a; } else { ai = a->inew; aj = a->jnew; ierr = PetscMalloc(bs2*ai[mbs]*sizeof(MatScalar),&aa);CHKERRQ(ierr); ierr = PetscMemcpy(aa,a->a,bs2*ai[mbs]*sizeof(MatScalar));CHKERRQ(ierr); ierr = PetscMalloc(ai[mbs]*sizeof(int),&a2anew);CHKERRQ(ierr); ierr = PetscMemcpy(a2anew,a->a2anew,(ai[mbs])*sizeof(int));CHKERRQ(ierr); for (i=0; i aj[j]){ /* printf("change orientation, row: %d, col: %d\n",i,aj[j]); */ ap = aa + j*bs2; /* ptr to the beginning of j-th block of aa */ for (k=0; kpermute){ ierr = PetscFree(aa);CHKERRQ(ierr); } ierr = ISRestoreIndices(perm,&perm_ptr);CHKERRQ(ierr); C->factor = FACTOR_CHOLESKY; C->assembled = PETSC_TRUE; C->preallocated = PETSC_TRUE; PetscLogFlops(1.3333*bs*bs2*b->mbs); /* from inverting diagonal blocks */ PetscFunctionReturn(0); } #undef __FUNCT__ #define __FUNCT__ "MatCholeskyFactorNumeric_SeqSBAIJ_N_NaturalOrdering" PetscErrorCode MatCholeskyFactorNumeric_SeqSBAIJ_N_NaturalOrdering(Mat A,Mat *B) { Mat C = *B; Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data,*b = (Mat_SeqSBAIJ *)C->data; PetscErrorCode ierr; int i,j,mbs=a->mbs,*bi=b->i,*bj=b->j; int *ai,*aj,k,k1,jmin,jmax,*jl,*il,vj,nexti,ili; int bs=a->bs,bs2 = a->bs2; MatScalar *ba = b->a,*aa,*ap,*dk,*uik; MatScalar *u,*diag,*rtmp,*rtmp_ptr; MatScalar *work; int *pivots; PetscFunctionBegin; /* initialization */ ierr = PetscMalloc(bs2*mbs*sizeof(MatScalar),&rtmp);CHKERRQ(ierr); ierr = PetscMemzero(rtmp,bs2*mbs*sizeof(MatScalar));CHKERRQ(ierr); ierr = PetscMalloc(2*mbs*sizeof(int),&il);CHKERRQ(ierr); jl = il + mbs; for (i=0; ii; aj = a->j; aa = a->a; /* for each row k */ for (k = 0; kfactor = FACTOR_CHOLESKY; C->assembled = PETSC_TRUE; C->preallocated = PETSC_TRUE; PetscLogFlops(1.3333*bs*bs2*b->mbs); /* from inverting diagonal blocks */ PetscFunctionReturn(0); } /* Numeric U^T*D*U factorization for SBAIJ format. Modified from SNF of YSMP. Version for blocks 2 by 2. */ #undef __FUNCT__ #define __FUNCT__ "MatCholeskyFactorNumeric_SeqSBAIJ_2" PetscErrorCode MatCholeskyFactorNumeric_SeqSBAIJ_2(Mat A,Mat *B) { Mat C = *B; Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data,*b = (Mat_SeqSBAIJ *)C->data; IS perm = b->row; PetscErrorCode ierr; int *perm_ptr,i,j,mbs=a->mbs,*bi=b->i,*bj=b->j; int *ai,*aj,*a2anew,k,k1,jmin,jmax,*jl,*il,vj,nexti,ili; MatScalar *ba = b->a,*aa,*ap,*dk,*uik; MatScalar *u,*diag,*rtmp,*rtmp_ptr; PetscFunctionBegin; /* initialization */ /* il and jl record the first nonzero element in each row of the accessing window U(0:k, k:mbs-1). jl: list of rows to be added to uneliminated rows i>= k: jl(i) is the first row to be added to row i i< k: jl(i) is the row following row i in some list of rows jl(i) = mbs indicates the end of a list il(i): points to the first nonzero element in columns k,...,mbs-1 of row i of U */ ierr = PetscMalloc(4*mbs*sizeof(MatScalar),&rtmp);CHKERRQ(ierr); ierr = PetscMemzero(rtmp,4*mbs*sizeof(MatScalar));CHKERRQ(ierr); ierr = PetscMalloc(2*mbs*sizeof(int),&il);CHKERRQ(ierr); jl = il + mbs; for (i=0; ipermute){ ai = a->i; aj = a->j; aa = a->a; } else { ai = a->inew; aj = a->jnew; ierr = PetscMalloc(4*ai[mbs]*sizeof(MatScalar),&aa);CHKERRQ(ierr); ierr = PetscMemcpy(aa,a->a,4*ai[mbs]*sizeof(MatScalar));CHKERRQ(ierr); ierr = PetscMalloc(ai[mbs]*sizeof(int),&a2anew);CHKERRQ(ierr); ierr = PetscMemcpy(a2anew,a->a2anew,(ai[mbs])*sizeof(int));CHKERRQ(ierr); for (i=0; i aj[j]){ /* printf("change orientation, row: %d, col: %d\n",i,aj[j]); */ ap = aa + j*4; /* ptr to the beginning of the block */ dk[1] = ap[1]; /* swap ap[1] and ap[2] */ ap[1] = ap[2]; ap[2] = dk[1]; } } } ierr = PetscFree(a2anew);CHKERRQ(ierr); } /* for each row k */ for (k = 0; kpermute) { ierr = PetscFree(aa);CHKERRQ(ierr); } ierr = ISRestoreIndices(perm,&perm_ptr);CHKERRQ(ierr); C->factor = FACTOR_CHOLESKY; C->assembled = PETSC_TRUE; C->preallocated = PETSC_TRUE; PetscLogFlops(1.3333*8*b->mbs); /* from inverting diagonal blocks */ PetscFunctionReturn(0); } /* Version for when blocks are 2 by 2 Using natural ordering */ #undef __FUNCT__ #define __FUNCT__ "MatCholeskyFactorNumeric_SeqSBAIJ_2_NaturalOrdering" PetscErrorCode MatCholeskyFactorNumeric_SeqSBAIJ_2_NaturalOrdering(Mat A,Mat *B) { Mat C = *B; Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data,*b = (Mat_SeqSBAIJ *)C->data; PetscErrorCode ierr; int i,j,mbs=a->mbs,*bi=b->i,*bj=b->j; int *ai,*aj,k,k1,jmin,jmax,*jl,*il,vj,nexti,ili; MatScalar *ba = b->a,*aa,*ap,*dk,*uik; MatScalar *u,*diag,*rtmp,*rtmp_ptr; PetscFunctionBegin; /* initialization */ /* il and jl record the first nonzero element in each row of the accessing window U(0:k, k:mbs-1). jl: list of rows to be added to uneliminated rows i>= k: jl(i) is the first row to be added to row i i< k: jl(i) is the row following row i in some list of rows jl(i) = mbs indicates the end of a list il(i): points to the first nonzero element in columns k,...,mbs-1 of row i of U */ ierr = PetscMalloc(4*mbs*sizeof(MatScalar),&rtmp);CHKERRQ(ierr); ierr = PetscMemzero(rtmp,4*mbs*sizeof(MatScalar));CHKERRQ(ierr); ierr = PetscMalloc(2*mbs*sizeof(int),&il);CHKERRQ(ierr); jl = il + mbs; for (i=0; ii; aj = a->j; aa = a->a; /* for each row k */ for (k = 0; kfactor = FACTOR_CHOLESKY; C->assembled = PETSC_TRUE; C->preallocated = PETSC_TRUE; PetscLogFlops(1.3333*8*b->mbs); /* from inverting diagonal blocks */ PetscFunctionReturn(0); } /* Numeric U^T*D*U factorization for SBAIJ format. Modified from SNF of YSMP. Version for blocks are 1 by 1. */ #undef __FUNCT__ #define __FUNCT__ "MatCholeskyFactorNumeric_SeqSBAIJ_1" PetscErrorCode MatCholeskyFactorNumeric_SeqSBAIJ_1(Mat A,Mat *B) { Mat C = *B; Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data,*b = (Mat_SeqSBAIJ *)C->data; IS ip = b->row; PetscErrorCode ierr; int *rip,i,j,mbs = a->mbs,*bi = b->i,*bj = b->j; int *ai,*aj,*r; int k,jmin,jmax,*jl,*il,vj,nexti,ili; MatScalar *rtmp; MatScalar *ba = b->a,*aa,ak; MatScalar dk,uikdi; PetscFunctionBegin; ierr = ISGetIndices(ip,&rip);CHKERRQ(ierr); if (!a->permute){ ai = a->i; aj = a->j; aa = a->a; } else { ai = a->inew; aj = a->jnew; ierr = PetscMalloc(ai[mbs]*sizeof(MatScalar),&aa);CHKERRQ(ierr); ierr = PetscMemcpy(aa,a->a,ai[mbs]*sizeof(MatScalar));CHKERRQ(ierr); ierr = PetscMalloc(ai[mbs]*sizeof(int),&r);CHKERRQ(ierr); ierr= PetscMemcpy(r,a->a2anew,(ai[mbs])*sizeof(int));CHKERRQ(ierr); jmin = ai[0]; jmax = ai[mbs]; for (j=jmin; j= k: jl(i) is the first row to be added to row i i< k: jl(i) is the row following row i in some list of rows jl(i) = mbs indicates the end of a list il(i): points to the first nonzero element in columns k,...,mbs-1 of row i of U */ ierr = PetscMalloc(mbs*sizeof(MatScalar),&rtmp);CHKERRQ(ierr); ierr = PetscMalloc(2*mbs*sizeof(int),&il);CHKERRQ(ierr); jl = il + mbs; for (i=0; ipermute){ ierr = PetscFree(aa);CHKERRQ(ierr); } ierr = ISRestoreIndices(ip,&rip);CHKERRQ(ierr); C->factor = FACTOR_CHOLESKY; C->assembled = PETSC_TRUE; C->preallocated = PETSC_TRUE; PetscLogFlops(b->mbs); PetscFunctionReturn(0); } /* Version for when blocks are 1 by 1 Using natural ordering */ #undef __FUNCT__ #define __FUNCT__ "MatCholeskyFactorNumeric_SeqSBAIJ_1_NaturalOrdering" PetscErrorCode MatCholeskyFactorNumeric_SeqSBAIJ_1_NaturalOrdering(Mat A,Mat *B) { Mat C = *B; Mat_SeqSBAIJ *a=(Mat_SeqSBAIJ*)A->data,*b=(Mat_SeqSBAIJ *)C->data; PetscErrorCode ierr; int i,j,mbs = a->mbs; int *ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j; int k,jmin,*jl,*il,nexti,ili,*acol,*bcol,nz,ndamp = 0; MatScalar *rtmp,*ba=b->a,*aa=a->a,dk,uikdi,*aval,*bval; PetscReal damping=b->factor_damping, zeropivot=b->factor_zeropivot,shift_amount; PetscTruth damp,chshift; int nshift=0; PetscFunctionBegin; /* initialization */ /* il and jl record the first nonzero element in each row of the accessing window U(0:k, k:mbs-1). jl: list of rows to be added to uneliminated rows i>= k: jl(i) is the first row to be added to row i i< k: jl(i) is the row following row i in some list of rows jl(i) = mbs indicates the end of a list il(i): points to the first nonzero element in U(i,k:mbs-1) */ ierr = PetscMalloc(mbs*sizeof(MatScalar),&rtmp);CHKERRQ(ierr); ierr = PetscMalloc(2*mbs*sizeof(int),&il);CHKERRQ(ierr); jl = il + mbs; shift_amount = 0; do { damp = PETSC_FALSE; chshift = PETSC_FALSE; for (i=0; i 0){ bcol = bj + jmin; bval = ba + jmin; while (nz --) rtmp[*bcol++] += uikdi*(*bval++); /* ... add i to row list for next nonzero entry */ il[i] = jmin; /* update il(i) in column k+1, ... mbs-1 */ j = bj[jmin]; jl[i] = jl[j]; jl[j] = i; /* update jl */ } i = nexti; } if (PetscRealPart(dk) < zeropivot && b->factor_shift){ /* calculate a shift that would make this row diagonally dominant */ PetscReal rs = PetscAbs(PetscRealPart(dk)); jmin = bi[k]+1; nz = bi[k+1] - jmin; if (nz){ bcol = bj + jmin; bval = ba + jmin; while (nz--){ rs += PetscAbsScalar(rtmp[*bcol++]); } } /* if this shift is less than the previous, just up the previous one by a bit */ shift_amount = PetscMax(rs,1.1*shift_amount); chshift = PETSC_TRUE; /* Unlike in the ILU case there is no exit condition on nshift: we increase the shift until it converges. There is no guarantee that this algorithm converges faster or slower, or is better or worse than the ILU algorithm. */ nshift++; break; } if (PetscRealPart(dk) < zeropivot){ if (damping == (PetscReal) PETSC_DECIDE) damping = -PetscRealPart(dk)/(k+1); if (damping > 0.0) { if (ndamp) damping *= 2.0; damp = PETSC_TRUE; ndamp++; break; } else if (PetscAbsScalar(dk) < zeropivot){ SETERRQ3(PETSC_ERR_MAT_LU_ZRPVT,"Zero pivot row %d value %g tolerance %g",k,PetscRealPart(dk),zeropivot); } else { PetscLogInfo((PetscObject)A,"Negative pivot %g in row %d of Cholesky factorization\n",PetscRealPart(dk),k); } } /* save nonzero entries in k-th row of U ... */ /* printf("%d, dk: %g, 1/dk: %g\n",k,dk,1/dk); */ ba[bi[k]] = 1.0/dk; jmin = bi[k]+1; nz = bi[k+1] - jmin; if (nz){ bcol = bj + jmin; bval = ba + jmin; while (nz--){ *bval++ = rtmp[*bcol]; rtmp[*bcol++] = 0.0; } /* ... add k to row list for first nonzero entry in k-th row */ il[k] = jmin; i = bj[jmin]; jl[k] = jl[i]; jl[i] = k; } } /* end of for (k = 0; kfactor = FACTOR_CHOLESKY; C->assembled = PETSC_TRUE; C->preallocated = PETSC_TRUE; PetscLogFlops(b->mbs); if (ndamp) { PetscLogInfo(0,"MatCholeskyFactorNumerical_SeqSBAIJ_1_NaturalOrdering: number of damping tries %d damping value %g\n",ndamp,damping); } if (nshift) { PetscLogInfo(0,"MatCholeskyFactorNumeric_SeqSBAIJ_1_NaturalOrdering diagonal shifted %d shifts\n",nshift); } PetscFunctionReturn(0); } #undef __FUNCT__ #define __FUNCT__ "MatCholeskyFactor_SeqSBAIJ" PetscErrorCode MatCholeskyFactor_SeqSBAIJ(Mat A,IS perm,MatFactorInfo *info) { PetscErrorCode ierr; Mat C; PetscFunctionBegin; ierr = MatCholeskyFactorSymbolic(A,perm,info,&C);CHKERRQ(ierr); ierr = MatCholeskyFactorNumeric(A,&C);CHKERRQ(ierr); ierr = MatHeaderCopy(A,C);CHKERRQ(ierr); PetscFunctionReturn(0); }