xref: /petsc/src/mat/impls/aij/seq/bas/basfactor.c (revision b2f1ab58077089250fe93bab20362d49e466d970)
1*b2f1ab58SBarry Smith 
2*b2f1ab58SBarry Smith #include <petsc.h>
3*b2f1ab58SBarry Smith #include <petscksp.h>
4*b2f1ab58SBarry Smith #include "private/kspimpl.h"
5*b2f1ab58SBarry Smith #include "petscpc.h"
6*b2f1ab58SBarry Smith #include "../src/mat/impls/aij/seq/aij.h"
7*b2f1ab58SBarry Smith #include "../src/mat/impls/sbaij/seq/sbaij.h"
8*b2f1ab58SBarry Smith #include "../src/mat/impls/aij/seq/bas/spbas.h"
9*b2f1ab58SBarry Smith 
10*b2f1ab58SBarry Smith #undef __FUNCT__
11*b2f1ab58SBarry Smith #define __FUNCT__ "MatICCFactorSymbolic_SeqAIJ_Bas"
12*b2f1ab58SBarry Smith PetscErrorCode MatICCFactorSymbolic_SeqAIJ_Bas(Mat fact,Mat A,IS perm,const MatFactorInfo *info)
13*b2f1ab58SBarry Smith {
14*b2f1ab58SBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
15*b2f1ab58SBarry Smith   Mat_SeqSBAIJ       *b;
16*b2f1ab58SBarry Smith   PetscErrorCode     ierr;
17*b2f1ab58SBarry Smith   PetscTruth         perm_identity,missing;
18*b2f1ab58SBarry Smith   PetscInt           reallocs=0,i,*ai=a->i,*aj=a->j,am=A->rmap->n,*ui;
19*b2f1ab58SBarry Smith   const PetscInt     *rip,*riip;
20*b2f1ab58SBarry Smith   PetscInt           j;
21*b2f1ab58SBarry Smith   PetscInt           d;
22*b2f1ab58SBarry Smith   PetscInt           ncols,*cols,*uj;
23*b2f1ab58SBarry Smith   PetscReal          fill=info->fill,levels=info->levels;
24*b2f1ab58SBarry Smith   IS                 iperm;
25*b2f1ab58SBarry Smith   spbas_matrix       Pattern_0, Pattern_P;
26*b2f1ab58SBarry Smith 
27*b2f1ab58SBarry Smith   PetscFunctionBegin;
28*b2f1ab58SBarry Smith   if (A->rmap->n != A->cmap->n) SETERRQ2(PETSC_ERR_ARG_WRONG,"Must be square matrix, rows %D columns %D",A->rmap->n,A->cmap->n);
29*b2f1ab58SBarry Smith   ierr = MatMissingDiagonal(A,&missing,&d);CHKERRQ(ierr);
30*b2f1ab58SBarry Smith   if (missing) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry %D",d);
31*b2f1ab58SBarry Smith   ierr = ISIdentity(perm,&perm_identity);CHKERRQ(ierr);
32*b2f1ab58SBarry Smith   ierr = ISInvertPermutation(perm,PETSC_DECIDE,&iperm);CHKERRQ(ierr);
33*b2f1ab58SBarry Smith 
34*b2f1ab58SBarry Smith 
35*b2f1ab58SBarry Smith   /* ICC(0) without matrix ordering: simply copies fill pattern */
36*b2f1ab58SBarry Smith   if (!levels && perm_identity) {
37*b2f1ab58SBarry Smith     ierr = PetscMalloc((am+1)*sizeof(PetscInt),&ui);CHKERRQ(ierr);
38*b2f1ab58SBarry Smith     ui[0] = 0;
39*b2f1ab58SBarry Smith 
40*b2f1ab58SBarry Smith     for (i=0; i<am; i++) {
41*b2f1ab58SBarry Smith       ui[i+1] = ui[i] + ai[i+1] - a->diag[i];
42*b2f1ab58SBarry Smith     }
43*b2f1ab58SBarry Smith     ierr = PetscMalloc((ui[am]+1)*sizeof(PetscInt),&uj);CHKERRQ(ierr);
44*b2f1ab58SBarry Smith     cols = uj;
45*b2f1ab58SBarry Smith     for (i=0; i<am; i++) {
46*b2f1ab58SBarry Smith       aj    = a->j + a->diag[i];
47*b2f1ab58SBarry Smith       ncols = ui[i+1] - ui[i];
48*b2f1ab58SBarry Smith       for (j=0; j<ncols; j++) *cols++ = *aj++;
49*b2f1ab58SBarry Smith     }
50*b2f1ab58SBarry Smith   } else { /* case: levels>0 || (levels=0 && !perm_identity) */
51*b2f1ab58SBarry Smith     ierr = ISGetIndices(iperm,&riip);CHKERRQ(ierr);
52*b2f1ab58SBarry Smith     ierr = ISGetIndices(perm,&rip);CHKERRQ(ierr);
53*b2f1ab58SBarry Smith 
54*b2f1ab58SBarry Smith     // Create spbas_matrix for pattern
55*b2f1ab58SBarry Smith     ierr = spbas_pattern_only(am, am, ai, aj, &Pattern_0); CHKERRQ(ierr);
56*b2f1ab58SBarry Smith 
57*b2f1ab58SBarry Smith     // Apply the permutation
58*b2f1ab58SBarry Smith     ierr = spbas_apply_reordering( &Pattern_0, rip, riip); CHKERRQ(ierr);
59*b2f1ab58SBarry Smith 
60*b2f1ab58SBarry Smith     // Raise the power
61*b2f1ab58SBarry Smith     ierr = spbas_power( Pattern_0, (int) levels+1, &Pattern_P);
62*b2f1ab58SBarry Smith     CHKERRQ(ierr);
63*b2f1ab58SBarry Smith     ierr = spbas_delete( Pattern_0 ); CHKERRQ(ierr);
64*b2f1ab58SBarry Smith 
65*b2f1ab58SBarry Smith     // Keep only upper triangle of pattern
66*b2f1ab58SBarry Smith     ierr = spbas_keep_upper( &Pattern_P );
67*b2f1ab58SBarry Smith 
68*b2f1ab58SBarry Smith     // Convert to Sparse Row Storage
69*b2f1ab58SBarry Smith     ierr = spbas_matrix_to_crs(Pattern_P, NULL, &ui, &uj); CHKERRQ(ierr);
70*b2f1ab58SBarry Smith     ierr = spbas_delete(Pattern_P);CHKERRQ(ierr);
71*b2f1ab58SBarry Smith   } /* end of case: levels>0 || (levels=0 && !perm_identity) */
72*b2f1ab58SBarry Smith 
73*b2f1ab58SBarry Smith   /* put together the new matrix in MATSEQSBAIJ format */
74*b2f1ab58SBarry Smith 
75*b2f1ab58SBarry Smith   b    = (Mat_SeqSBAIJ*)(fact)->data;
76*b2f1ab58SBarry Smith   b->singlemalloc = PETSC_FALSE;
77*b2f1ab58SBarry Smith   ierr = PetscMalloc((ui[am]+1)*sizeof(MatScalar),&b->a);CHKERRQ(ierr);
78*b2f1ab58SBarry Smith   b->j    = uj;
79*b2f1ab58SBarry Smith   b->i    = ui;
80*b2f1ab58SBarry Smith   b->diag = 0;
81*b2f1ab58SBarry Smith   b->ilen = 0;
82*b2f1ab58SBarry Smith   b->imax = 0;
83*b2f1ab58SBarry Smith   b->row  = perm;
84*b2f1ab58SBarry Smith   b->col  = perm;
85*b2f1ab58SBarry Smith   ierr    = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr);
86*b2f1ab58SBarry Smith   ierr    = PetscObjectReference((PetscObject)perm);CHKERRQ(ierr);
87*b2f1ab58SBarry Smith   b->icol = iperm;
88*b2f1ab58SBarry Smith   b->pivotinblocks = PETSC_FALSE; /* need to get from MatFactorInfo */
89*b2f1ab58SBarry Smith   ierr    = PetscMalloc((am+1)*sizeof(PetscScalar),&b->solve_work);CHKERRQ(ierr);
90*b2f1ab58SBarry Smith   ierr = PetscLogObjectMemory((fact),(ui[am]-am)*(sizeof(PetscInt)+sizeof(MatScalar)));CHKERRQ(ierr);
91*b2f1ab58SBarry Smith   b->maxnz   = b->nz = ui[am];
92*b2f1ab58SBarry Smith   b->free_a  = PETSC_TRUE;
93*b2f1ab58SBarry Smith   b->free_ij = PETSC_TRUE;
94*b2f1ab58SBarry Smith 
95*b2f1ab58SBarry Smith   (fact)->info.factor_mallocs    = reallocs;
96*b2f1ab58SBarry Smith   (fact)->info.fill_ratio_given  = fill;
97*b2f1ab58SBarry Smith   if (ai[am] != 0) {
98*b2f1ab58SBarry Smith     (fact)->info.fill_ratio_needed = ((PetscReal)ui[am])/((PetscReal)ai[am]);
99*b2f1ab58SBarry Smith   } else {
100*b2f1ab58SBarry Smith     (fact)->info.fill_ratio_needed = 0.0;
101*b2f1ab58SBarry Smith   }
102*b2f1ab58SBarry Smith   /*  (fact)->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJ_inplace; */
103*b2f1ab58SBarry Smith   PetscFunctionReturn(0);
104*b2f1ab58SBarry Smith }
105*b2f1ab58SBarry Smith 
106*b2f1ab58SBarry Smith 
107*b2f1ab58SBarry Smith #undef __FUNCT__
108*b2f1ab58SBarry Smith #define __FUNCT__ "MatCholeskyFactorNumeric_SeqAIJ_Bas"
109*b2f1ab58SBarry Smith PetscErrorCode MatCholeskyFactorNumeric_SeqAIJ_Bas(Mat B,Mat A,const MatFactorInfo *info)
110*b2f1ab58SBarry Smith {
111*b2f1ab58SBarry Smith   Mat            C = B;
112*b2f1ab58SBarry Smith   Mat_SeqSBAIJ   *b=(Mat_SeqSBAIJ*)C->data;
113*b2f1ab58SBarry Smith   IS             ip=b->row,iip = b->icol;
114*b2f1ab58SBarry Smith   PetscErrorCode ierr;
115*b2f1ab58SBarry Smith   const PetscInt *rip,*riip;
116*b2f1ab58SBarry Smith   PetscInt       mbs=A->rmap->n,*bi=b->i,*bj=b->j;
117*b2f1ab58SBarry Smith 
118*b2f1ab58SBarry Smith   MatScalar      *ba=b->a;
119*b2f1ab58SBarry Smith   PetscReal      shiftnz = info->shiftnz;
120*b2f1ab58SBarry Smith   PetscScalar    droptol = -1;
121*b2f1ab58SBarry Smith   PetscTruth     perm_identity;
122*b2f1ab58SBarry Smith   spbas_matrix   Pattern, matrix_L,matrix_LT;
123*b2f1ab58SBarry Smith   PetscScalar    mem_reduction;
124*b2f1ab58SBarry Smith 
125*b2f1ab58SBarry Smith   PetscFunctionBegin;
126*b2f1ab58SBarry Smith   // Reduce memory requirements:
127*b2f1ab58SBarry Smith   //   erase values of B-matrix
128*b2f1ab58SBarry Smith   ierr = PetscFree(ba); CHKERRQ(ierr);
129*b2f1ab58SBarry Smith   //   Compress (maximum) sparseness pattern of B-matrix
130*b2f1ab58SBarry Smith   ierr = spbas_compress_pattern(bi, bj, mbs, mbs, SPBAS_DIAGONAL_OFFSETS,
131*b2f1ab58SBarry Smith 				&Pattern, &mem_reduction);CHKERRQ(ierr);
132*b2f1ab58SBarry Smith   ierr = PetscFree(bi); CHKERRQ(ierr);
133*b2f1ab58SBarry Smith   ierr = PetscFree(bj); CHKERRQ(ierr);
134*b2f1ab58SBarry Smith 
135*b2f1ab58SBarry Smith   printf("Results from spbas_compress_pattern:\n");
136*b2f1ab58SBarry Smith   printf("    compression rate %6.2f %%\n",mem_reduction);
137*b2f1ab58SBarry Smith   ierr=7;
138*b2f1ab58SBarry Smith 
139*b2f1ab58SBarry Smith   // Make Cholesky decompositions with larger Manteuffel shifts until no more
140*b2f1ab58SBarry Smith   // negative diagonals are found.
141*b2f1ab58SBarry Smith   ierr  = ISGetIndices(ip,&rip);CHKERRQ(ierr);
142*b2f1ab58SBarry Smith   ierr  = ISGetIndices(iip,&riip);CHKERRQ(ierr);
143*b2f1ab58SBarry Smith 
144*b2f1ab58SBarry Smith   if (info->usedt) {
145*b2f1ab58SBarry Smith     droptol = info->dt;
146*b2f1ab58SBarry Smith   }
147*b2f1ab58SBarry Smith   for (ierr = NEGATIVE_DIAGONAL; ierr == NEGATIVE_DIAGONAL; )
148*b2f1ab58SBarry Smith   {
149*b2f1ab58SBarry Smith      ierr  = spbas_incomplete_cholesky( A, rip, riip, Pattern, droptol, shiftnz,
150*b2f1ab58SBarry Smith                                         &matrix_LT);
151*b2f1ab58SBarry Smith      if (ierr == NEGATIVE_DIAGONAL)
152*b2f1ab58SBarry Smith      {
153*b2f1ab58SBarry Smith         shiftnz *= 1.5;
154*b2f1ab58SBarry Smith         printf("spbas_incomplete_cholesky found a negative diagonal.\n");
155*b2f1ab58SBarry Smith         printf("   Trying again with Manteuffel shift=%e\n",shiftnz);
156*b2f1ab58SBarry Smith      }
157*b2f1ab58SBarry Smith   }
158*b2f1ab58SBarry Smith   CHKERRQ(ierr);
159*b2f1ab58SBarry Smith   ierr = spbas_delete(Pattern); CHKERRQ(ierr);
160*b2f1ab58SBarry Smith 
161*b2f1ab58SBarry Smith   printf("Results from spbas_incomplete_cholesky:\n");
162*b2f1ab58SBarry Smith   printf("    memory_usage:    %6.2f bytes per row\n",
163*b2f1ab58SBarry Smith               (PetscScalar) spbas_memory_requirement( matrix_LT)/ (PetscScalar) mbs);
164*b2f1ab58SBarry Smith 
165*b2f1ab58SBarry Smith   ierr = ISRestoreIndices(ip,&rip);CHKERRQ(ierr);
166*b2f1ab58SBarry Smith   ierr = ISRestoreIndices(iip,&riip);CHKERRQ(ierr);
167*b2f1ab58SBarry Smith 
168*b2f1ab58SBarry Smith   // Convert spbas_matrix to compressed row storage
169*b2f1ab58SBarry Smith   ierr = spbas_transpose(matrix_LT, &matrix_L); CHKERRQ(ierr);
170*b2f1ab58SBarry Smith   ierr = spbas_delete(matrix_LT); CHKERRQ(ierr);
171*b2f1ab58SBarry Smith #if defined(foo)
172*b2f1ab58SBarry Smith   { ierr = spbas_dump("factorL",matrix_L); CHKERRQ(ierr);}
173*b2f1ab58SBarry Smith #endif
174*b2f1ab58SBarry Smith   ierr = spbas_matrix_to_crs(matrix_L, &ba, &bi, &bj); CHKERRQ(ierr);
175*b2f1ab58SBarry Smith   b->i=bi; b->j=bj; b->a=ba;
176*b2f1ab58SBarry Smith   ierr = spbas_delete(matrix_L); CHKERRQ(ierr);
177*b2f1ab58SBarry Smith 
178*b2f1ab58SBarry Smith   // Set the appropriate solution functions
179*b2f1ab58SBarry Smith   ierr = ISIdentity(ip,&perm_identity);CHKERRQ(ierr);
180*b2f1ab58SBarry Smith   if (perm_identity){
181*b2f1ab58SBarry Smith     (B)->ops->solve           = MatSolve_SeqSBAIJ_1_NaturalOrdering_inplace;
182*b2f1ab58SBarry Smith     (B)->ops->solvetranspose  = MatSolve_SeqSBAIJ_1_NaturalOrdering_inplace;
183*b2f1ab58SBarry Smith     (B)->ops->forwardsolve    = MatForwardSolve_SeqSBAIJ_1_NaturalOrdering_inplace;
184*b2f1ab58SBarry Smith     (B)->ops->backwardsolve   = MatBackwardSolve_SeqSBAIJ_1_NaturalOrdering_inplace;
185*b2f1ab58SBarry Smith   } else {
186*b2f1ab58SBarry Smith     (B)->ops->solve           = MatSolve_SeqSBAIJ_1_inplace;
187*b2f1ab58SBarry Smith     (B)->ops->solvetranspose  = MatSolve_SeqSBAIJ_1_inplace;
188*b2f1ab58SBarry Smith     (B)->ops->forwardsolve    = MatForwardSolve_SeqSBAIJ_1_inplace;
189*b2f1ab58SBarry Smith     (B)->ops->backwardsolve   = MatBackwardSolve_SeqSBAIJ_1_inplace;
190*b2f1ab58SBarry Smith   }
191*b2f1ab58SBarry Smith 
192*b2f1ab58SBarry Smith   C->assembled    = PETSC_TRUE;
193*b2f1ab58SBarry Smith   C->preallocated = PETSC_TRUE;
194*b2f1ab58SBarry Smith   ierr = PetscLogFlops(C->rmap->n);CHKERRQ(ierr);
195*b2f1ab58SBarry Smith 
196*b2f1ab58SBarry Smith   // Optionally, print the factor matrix to file
197*b2f1ab58SBarry Smith #if defined(foo)
198*b2f1ab58SBarry Smith   {
199*b2f1ab58SBarry Smith      FILE * factfile = fopen("factorL","w");
200*b2f1ab58SBarry Smith      if (!factfile) CHKERRQ((PetscErrorCode) 10);
201*b2f1ab58SBarry Smith      for (i=0; i<mbs; i++)
202*b2f1ab58SBarry Smith      {
203*b2f1ab58SBarry Smith         for (j=bi[i]; j<bi[i+1]; j++)
204*b2f1ab58SBarry Smith         {
205*b2f1ab58SBarry Smith             fprintf(factfile,"%d %d %e\n",i,bj[j],ba[j]);
206*b2f1ab58SBarry Smith         }
207*b2f1ab58SBarry Smith      }
208*b2f1ab58SBarry Smith      fclose(factfile);
209*b2f1ab58SBarry Smith   }
210*b2f1ab58SBarry Smith #endif
211*b2f1ab58SBarry Smith   PetscFunctionReturn(0);
212*b2f1ab58SBarry Smith }
213*b2f1ab58SBarry Smith 
214*b2f1ab58SBarry Smith #undef __FUNCT__
215*b2f1ab58SBarry Smith #define __FUNCT__ "MatGetFactor_seqaij_bas"
216*b2f1ab58SBarry Smith PetscErrorCode MatGetFactor_seqaij_bas(Mat A,MatFactorType ftype,Mat *B)
217*b2f1ab58SBarry Smith {
218*b2f1ab58SBarry Smith   PetscInt           n = A->rmap->n;
219*b2f1ab58SBarry Smith   PetscErrorCode     ierr;
220*b2f1ab58SBarry Smith 
221*b2f1ab58SBarry Smith   PetscFunctionBegin;
222*b2f1ab58SBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
223*b2f1ab58SBarry Smith   ierr = MatSetSizes(*B,n,n,n,n);CHKERRQ(ierr);
224*b2f1ab58SBarry Smith   if (ftype == MAT_FACTOR_ICC) {
225*b2f1ab58SBarry Smith     ierr = MatSetType(*B,MATSEQSBAIJ);CHKERRQ(ierr);
226*b2f1ab58SBarry Smith     ierr = MatSeqSBAIJSetPreallocation(*B,1,MAT_SKIP_ALLOCATION,PETSC_NULL);CHKERRQ(ierr);
227*b2f1ab58SBarry Smith     (*B)->ops->iccfactorsymbolic     = MatICCFactorSymbolic_SeqAIJ_Bas;
228*b2f1ab58SBarry Smith     (*B)->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqAIJ_Bas;
229*b2f1ab58SBarry Smith   } else SETERRQ(PETSC_ERR_SUP,"Factor type not supported");
230*b2f1ab58SBarry Smith   (*B)->factor = ftype;
231*b2f1ab58SBarry Smith   PetscFunctionReturn(0);
232*b2f1ab58SBarry Smith }
233*b2f1ab58SBarry Smith 
234*b2f1ab58SBarry Smith 
235*b2f1ab58SBarry Smith 
236