Actual source code: basfactor.c

petsc-3.6.1 2015-08-06
Report Typos and Errors
  2: #include <../src/mat/impls/aij/seq/aij.h>
  3: #include <../src/mat/impls/sbaij/seq/sbaij.h>
  4: #include <../src/mat/impls/aij/seq/bas/spbas.h>

  8: PetscErrorCode MatICCFactorSymbolic_SeqAIJ_Bas(Mat fact,Mat A,IS perm,const MatFactorInfo *info)
  9: {
 10:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
 11:   Mat_SeqSBAIJ   *b;
 13:   PetscBool      perm_identity,missing;
 14:   PetscInt       reallocs=0,i,*ai=a->i,*aj=a->j,am=A->rmap->n,*ui;
 15:   const PetscInt *rip,*riip;
 16:   PetscInt       j;
 17:   PetscInt       d;
 18:   PetscInt       ncols,*cols,*uj;
 19:   PetscReal      fill=info->fill,levels=info->levels;
 20:   IS             iperm;
 21:   spbas_matrix   Pattern_0, Pattern_P;

 24:   if (A->rmap->n != A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Must be square matrix, rows %D columns %D",A->rmap->n,A->cmap->n);
 25:   MatMissingDiagonal(A,&missing,&d);
 26:   if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",d);
 27:   ISIdentity(perm,&perm_identity);
 28:   ISInvertPermutation(perm,PETSC_DECIDE,&iperm);

 30:   /* ICC(0) without matrix ordering: simply copies fill pattern */
 31:   if (!levels && perm_identity) {
 32:     PetscMalloc1(am+1,&ui);
 33:     ui[0] = 0;

 35:     for (i=0; i<am; i++) {
 36:       ui[i+1] = ui[i] + ai[i+1] - a->diag[i];
 37:     }
 38:     PetscMalloc1(ui[am]+1,&uj);
 39:     cols = uj;
 40:     for (i=0; i<am; i++) {
 41:       aj    = a->j + a->diag[i];
 42:       ncols = ui[i+1] - ui[i];
 43:       for (j=0; j<ncols; j++) *cols++ = *aj++;
 44:     }
 45:   } else { /* case: levels>0 || (levels=0 && !perm_identity) */
 46:     ISGetIndices(iperm,&riip);
 47:     ISGetIndices(perm,&rip);

 49:     /* Create spbas_matrix for pattern */
 50:     spbas_pattern_only(am, am, ai, aj, &Pattern_0);

 52:     /* Apply the permutation */
 53:     spbas_apply_reordering(&Pattern_0, rip, riip);

 55:     /* Raise the power */
 56:     spbas_power(Pattern_0, (int) levels+1, &Pattern_P);
 57:     spbas_delete(Pattern_0);

 59:     /* Keep only upper triangle of pattern */
 60:     spbas_keep_upper(&Pattern_P);

 62:     /* Convert to Sparse Row Storage  */
 63:     spbas_matrix_to_crs(Pattern_P, NULL, &ui, &uj);
 64:     spbas_delete(Pattern_P);
 65:   } /* end of case: levels>0 || (levels=0 && !perm_identity) */

 67:   /* put together the new matrix in MATSEQSBAIJ format */

 69:   b               = (Mat_SeqSBAIJ*)(fact)->data;
 70:   b->singlemalloc = PETSC_FALSE;

 72:   PetscMalloc1(ui[am]+1,&b->a);

 74:   b->j    = uj;
 75:   b->i    = ui;
 76:   b->diag = 0;
 77:   b->ilen = 0;
 78:   b->imax = 0;
 79:   b->row  = perm;
 80:   b->col  = perm;

 82:   PetscObjectReference((PetscObject)perm);
 83:   PetscObjectReference((PetscObject)perm);

 85:   b->icol          = iperm;
 86:   b->pivotinblocks = PETSC_FALSE; /* need to get from MatFactorInfo */
 87:   PetscMalloc1(am+1,&b->solve_work);
 88:   PetscLogObjectMemory((PetscObject)(fact),(ui[am]-am)*(sizeof(PetscInt)+sizeof(MatScalar)));
 89:   b->maxnz         = b->nz = ui[am];
 90:   b->free_a        = PETSC_TRUE;
 91:   b->free_ij       = PETSC_TRUE;

 93:   (fact)->info.factor_mallocs   = reallocs;
 94:   (fact)->info.fill_ratio_given = fill;
 95:   if (ai[am] != 0) {
 96:     (fact)->info.fill_ratio_needed = ((PetscReal)ui[am])/((PetscReal)ai[am]);
 97:   } else {
 98:     (fact)->info.fill_ratio_needed = 0.0;
 99:   }
100:   /*  (fact)->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJ_inplace; */
101:   return(0);
102: }


107: PetscErrorCode MatCholeskyFactorNumeric_SeqAIJ_Bas(Mat B,Mat A,const MatFactorInfo *info)
108: {
109:   Mat            C = B;
110:   Mat_SeqSBAIJ   *b=(Mat_SeqSBAIJ*)C->data;
111:   IS             ip=b->row,iip = b->icol;
113:   const PetscInt *rip,*riip;
114:   PetscInt       mbs=A->rmap->n,*bi=b->i,*bj=b->j;

116:   MatScalar    *ba     = b->a;
117:   PetscReal    shiftnz = info->shiftamount;
118:   PetscReal    droptol = -1;
119:   PetscBool    perm_identity;
120:   spbas_matrix Pattern, matrix_L,matrix_LT;
121:   PetscReal    mem_reduction;

124:   /* Reduce memory requirements:   erase values of B-matrix */
125:   PetscFree(ba);
126:   /*   Compress (maximum) sparseness pattern of B-matrix */
127:   spbas_compress_pattern(bi, bj, mbs, mbs, SPBAS_DIAGONAL_OFFSETS,&Pattern, &mem_reduction);
128:   PetscFree(bi);
129:   PetscFree(bj);

131:   PetscInfo1(NULL,"    compression rate for spbas_compress_pattern %g \n",(double)mem_reduction);

133:   /* Make Cholesky decompositions with larger Manteuffel shifts until no more    negative diagonals are found. */
134:   ISGetIndices(ip,&rip);
135:   ISGetIndices(iip,&riip);

137:   if (info->usedt) {
138:     droptol = info->dt;
139:   }
140:   for (NEGATIVE_DIAGONAL; ierr == NEGATIVE_DIAGONAL;)
141:   {
142:     spbas_incomplete_cholesky(A, rip, riip, Pattern, droptol, shiftnz,&matrix_LT);
143:     if (ierr == NEGATIVE_DIAGONAL) {
144:       shiftnz *= 1.5;
145:       if (shiftnz < 1e-5) shiftnz=1e-5;
146:       PetscInfo1(NULL,"spbas_incomplete_cholesky found a negative diagonal. Trying again with Manteuffel shift=%g\n",(double)shiftnz);
147:     }
148:   }
149:   spbas_delete(Pattern);

151:   PetscInfo1(NULL,"    memory_usage for  spbas_incomplete_cholesky  %g bytes per row\n", (double)(PetscReal) (spbas_memory_requirement(matrix_LT)/ (PetscReal) mbs));

153:   ISRestoreIndices(ip,&rip);
154:   ISRestoreIndices(iip,&riip);

156:   /* Convert spbas_matrix to compressed row storage */
157:   spbas_transpose(matrix_LT, &matrix_L);
158:   spbas_delete(matrix_LT);
159:   spbas_matrix_to_crs(matrix_L, &ba, &bi, &bj);
160:   b->i =bi; b->j=bj; b->a=ba;
161:   spbas_delete(matrix_L);

163:   /* Set the appropriate solution functions */
164:   ISIdentity(ip,&perm_identity);
165:   if (perm_identity) {
166:     (B)->ops->solve          = MatSolve_SeqSBAIJ_1_NaturalOrdering_inplace;
167:     (B)->ops->solvetranspose = MatSolve_SeqSBAIJ_1_NaturalOrdering_inplace;
168:     (B)->ops->forwardsolve   = MatForwardSolve_SeqSBAIJ_1_NaturalOrdering_inplace;
169:     (B)->ops->backwardsolve  = MatBackwardSolve_SeqSBAIJ_1_NaturalOrdering_inplace;
170:   } else {
171:     (B)->ops->solve          = MatSolve_SeqSBAIJ_1_inplace;
172:     (B)->ops->solvetranspose = MatSolve_SeqSBAIJ_1_inplace;
173:     (B)->ops->forwardsolve   = MatForwardSolve_SeqSBAIJ_1_inplace;
174:     (B)->ops->backwardsolve  = MatBackwardSolve_SeqSBAIJ_1_inplace;
175:   }

177:   C->assembled    = PETSC_TRUE;
178:   C->preallocated = PETSC_TRUE;

180:   PetscLogFlops(C->rmap->n);
181:   return(0);
182: }

186: PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_bas(Mat A,MatFactorType ftype,Mat *B)
187: {
188:   PetscInt       n = A->rmap->n;

192:   MatCreate(PetscObjectComm((PetscObject)A),B);
193:   MatSetSizes(*B,n,n,n,n);
194:   if (ftype == MAT_FACTOR_ICC) {
195:     MatSetType(*B,MATSEQSBAIJ);
196:     MatSeqSBAIJSetPreallocation(*B,1,MAT_SKIP_ALLOCATION,NULL);

198:     (*B)->ops->iccfactorsymbolic     = MatICCFactorSymbolic_SeqAIJ_Bas;
199:     (*B)->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJ_Bas;
200:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Factor type not supported");
201:   (*B)->factortype = ftype;
202:   return(0);
203: }