Actual source code: matptap.c

petsc-3.14.6 2021-03-30
Report Typos and Errors

  2: /*
  3:   Defines projective product routines where A is a SeqAIJ matrix
  4:           C = P^T * A * P
  5: */

  7: #include <../src/mat/impls/aij/seq/aij.h>
  8: #include <../src/mat/utils/freespace.h>
  9: #include <petscbt.h>
 10: #include <petsctime.h>

 12: #if defined(PETSC_HAVE_HYPRE)
 13: PETSC_INTERN PetscErrorCode MatPtAPSymbolic_AIJ_AIJ_wHYPRE(Mat,Mat,PetscReal,Mat);
 14: #endif

 16: PetscErrorCode MatProductSymbolic_PtAP_SeqAIJ_SeqAIJ(Mat C)
 17: {
 18:   PetscErrorCode      ierr;
 19:   Mat_Product         *product = C->product;
 20:   Mat                 A=product->A,P=product->B;
 21:   MatProductAlgorithm alg=product->alg;
 22:   PetscReal           fill=product->fill;
 23:   PetscBool           flg;
 24:   Mat                 Pt;

 27:   /* "scalable" */
 28:   PetscStrcmp(alg,"scalable",&flg);
 29:   if (flg) {
 30:     MatPtAPSymbolic_SeqAIJ_SeqAIJ_SparseAxpy(A,P,fill,C);
 31:     C->ops->productnumeric = MatProductNumeric_PtAP;
 32:     return(0);
 33:   }

 35:   /* "rap" */
 36:   PetscStrcmp(alg,"rap",&flg);
 37:   if (flg) {
 38:     Mat_MatTransMatMult *atb;

 40:     PetscNew(&atb);
 41:     MatTranspose_SeqAIJ(P,MAT_INITIAL_MATRIX,&Pt);
 42:     MatMatMatMultSymbolic_SeqAIJ_SeqAIJ_SeqAIJ(Pt,A,P,fill,C);

 44:     atb->At                = Pt;
 45:     atb->data              = C->product->data;
 46:     atb->destroy           = C->product->destroy;
 47:     C->product->data       = atb;
 48:     C->product->destroy    = MatDestroy_SeqAIJ_MatTransMatMult;
 49:     C->ops->ptapnumeric    = MatPtAPNumeric_SeqAIJ_SeqAIJ;
 50:     C->ops->productnumeric = MatProductNumeric_PtAP;
 51:     return(0);
 52:   }

 54:   /* hypre */
 55: #if defined(PETSC_HAVE_HYPRE)
 56:   PetscStrcmp(alg,"hypre",&flg);
 57:   if (flg) {
 58:     MatPtAPSymbolic_AIJ_AIJ_wHYPRE(A,P,fill,C);
 59:     return(0);
 60:   }
 61: #endif

 63:   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatProductType is not supported");
 64: }

 66: PetscErrorCode MatPtAPSymbolic_SeqAIJ_SeqAIJ_SparseAxpy(Mat A,Mat P,PetscReal fill,Mat C)
 67: {
 68:   PetscErrorCode     ierr;
 69:   PetscFreeSpaceList free_space=NULL,current_space=NULL;
 70:   Mat_SeqAIJ         *a        = (Mat_SeqAIJ*)A->data,*p = (Mat_SeqAIJ*)P->data,*c;
 71:   PetscInt           *pti,*ptj,*ptJ,*ai=a->i,*aj=a->j,*ajj,*pi=p->i,*pj=p->j,*pjj;
 72:   PetscInt           *ci,*cj,*ptadenserow,*ptasparserow,*ptaj,nspacedouble=0;
 73:   PetscInt           an=A->cmap->N,am=A->rmap->N,pn=P->cmap->N,pm=P->rmap->N;
 74:   PetscInt           i,j,k,ptnzi,arow,anzj,ptanzi,prow,pnzj,cnzi,nlnk,*lnk;
 75:   MatScalar          *ca;
 76:   PetscBT            lnkbt;
 77:   PetscReal          afill;

 80:   /* Get ij structure of P^T */
 81:   MatGetSymbolicTranspose_SeqAIJ(P,&pti,&ptj);
 82:   ptJ  = ptj;

 84:   /* Allocate ci array, arrays for fill computation and */
 85:   /* free space for accumulating nonzero column info */
 86:   PetscMalloc1(pn+1,&ci);
 87:   ci[0] = 0;

 89:   PetscCalloc1(2*an+1,&ptadenserow);
 90:   ptasparserow = ptadenserow  + an;

 92:   /* create and initialize a linked list */
 93:   nlnk = pn+1;
 94:   PetscLLCreate(pn,pn,nlnk,lnk,lnkbt);

 96:   /* Set initial free space to be fill*(nnz(A)+ nnz(P)) */
 97:   PetscFreeSpaceGet(PetscRealIntMultTruncate(fill,PetscIntSumTruncate(ai[am],pi[pm])),&free_space);
 98:   current_space = free_space;

100:   /* Determine symbolic info for each row of C: */
101:   for (i=0; i<pn; i++) {
102:     ptnzi  = pti[i+1] - pti[i];
103:     ptanzi = 0;
104:     /* Determine symbolic row of PtA: */
105:     for (j=0; j<ptnzi; j++) {
106:       arow = *ptJ++;
107:       anzj = ai[arow+1] - ai[arow];
108:       ajj  = aj + ai[arow];
109:       for (k=0; k<anzj; k++) {
110:         if (!ptadenserow[ajj[k]]) {
111:           ptadenserow[ajj[k]]    = -1;
112:           ptasparserow[ptanzi++] = ajj[k];
113:         }
114:       }
115:     }
116:     /* Using symbolic info for row of PtA, determine symbolic info for row of C: */
117:     ptaj = ptasparserow;
118:     cnzi = 0;
119:     for (j=0; j<ptanzi; j++) {
120:       prow = *ptaj++;
121:       pnzj = pi[prow+1] - pi[prow];
122:       pjj  = pj + pi[prow];
123:       /* add non-zero cols of P into the sorted linked list lnk */
124:       PetscLLAddSorted(pnzj,pjj,pn,nlnk,lnk,lnkbt);
125:       cnzi += nlnk;
126:     }

128:     /* If free space is not available, make more free space */
129:     /* Double the amount of total space in the list */
130:     if (current_space->local_remaining<cnzi) {
131:       PetscFreeSpaceGet(PetscIntSumTruncate(cnzi,current_space->total_array_size),&current_space);
132:       nspacedouble++;
133:     }

135:     /* Copy data into free space, and zero out denserows */
136:     PetscLLClean(pn,pn,cnzi,lnk,current_space->array,lnkbt);

138:     current_space->array           += cnzi;
139:     current_space->local_used      += cnzi;
140:     current_space->local_remaining -= cnzi;

142:     for (j=0; j<ptanzi; j++) ptadenserow[ptasparserow[j]] = 0;

144:     /* Aside: Perhaps we should save the pta info for the numerical factorization. */
145:     /*        For now, we will recompute what is needed. */
146:     ci[i+1] = ci[i] + cnzi;
147:   }
148:   /* nnz is now stored in ci[ptm], column indices are in the list of free space */
149:   /* Allocate space for cj, initialize cj, and */
150:   /* destroy list of free space and other temporary array(s) */
151:   PetscMalloc1(ci[pn]+1,&cj);
152:   PetscFreeSpaceContiguous(&free_space,cj);
153:   PetscFree(ptadenserow);
154:   PetscLLDestroy(lnk,lnkbt);

156:   PetscCalloc1(ci[pn]+1,&ca);

158:   /* put together the new matrix */
159:   MatSetSeqAIJWithArrays_private(PetscObjectComm((PetscObject)A),pn,pn,ci,cj,ca,((PetscObject)A)->type_name,C);
160:   MatSetBlockSizes(C,PetscAbs(P->cmap->bs),PetscAbs(P->cmap->bs));

162:   /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
163:   /* Since these are PETSc arrays, change flags to free them as necessary. */
164:   c          = (Mat_SeqAIJ*)((C)->data);
165:   c->free_a  = PETSC_TRUE;
166:   c->free_ij = PETSC_TRUE;
167:   c->nonew   = 0;

169:   C->ops->ptapnumeric = MatPtAPNumeric_SeqAIJ_SeqAIJ_SparseAxpy;

171:   /* set MatInfo */
172:   afill = (PetscReal)ci[pn]/(ai[am]+pi[pm] + 1.e-5);
173:   if (afill < 1.0) afill = 1.0;
174:   c->maxnz                     = ci[pn];
175:   c->nz                        = ci[pn];
176:   C->info.mallocs           = nspacedouble;
177:   C->info.fill_ratio_given  = fill;
178:   C->info.fill_ratio_needed = afill;

180:   /* Clean up. */
181:   MatRestoreSymbolicTranspose_SeqAIJ(P,&pti,&ptj);
182: #if defined(PETSC_USE_INFO)
183:   if (ci[pn] != 0) {
184:     PetscInfo3(C,"Reallocs %D; Fill ratio: given %g needed %g.\n",nspacedouble,(double)fill,(double)afill);
185:     PetscInfo1(C,"Use MatPtAP(A,P,MatReuse,%g,&C) for best performance.\n",(double)afill);
186:   } else {
187:     PetscInfo(C,"Empty matrix product\n");
188:   }
189: #endif
190:   return(0);
191: }

193: PetscErrorCode MatPtAPNumeric_SeqAIJ_SeqAIJ_SparseAxpy(Mat A,Mat P,Mat C)
194: {
196:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
197:   Mat_SeqAIJ     *p = (Mat_SeqAIJ*) P->data;
198:   Mat_SeqAIJ     *c = (Mat_SeqAIJ*) C->data;
199:   PetscInt       *ai=a->i,*aj=a->j,*apj,*apjdense,*pi=p->i,*pj=p->j,*pJ=p->j,*pjj;
200:   PetscInt       *ci=c->i,*cj=c->j,*cjj;
201:   PetscInt       am =A->rmap->N,cn=C->cmap->N,cm=C->rmap->N;
202:   PetscInt       i,j,k,anzi,pnzi,apnzj,nextap,pnzj,prow,crow;
203:   MatScalar      *aa=a->a,*apa,*pa=p->a,*pA=p->a,*paj,*ca=c->a,*caj;

206:   /* Allocate temporary array for storage of one row of A*P (cn: non-scalable) */
207:   PetscCalloc2(cn,&apa,cn,&apjdense);
208:   PetscMalloc1(cn,&apj);

210:   /* Clear old values in C */
211:   PetscArrayzero(ca,ci[cm]);

213:   for (i=0; i<am; i++) {
214:     /* Form sparse row of A*P */
215:     anzi  = ai[i+1] - ai[i];
216:     apnzj = 0;
217:     for (j=0; j<anzi; j++) {
218:       prow = *aj++;
219:       pnzj = pi[prow+1] - pi[prow];
220:       pjj  = pj + pi[prow];
221:       paj  = pa + pi[prow];
222:       for (k=0; k<pnzj; k++) {
223:         if (!apjdense[pjj[k]]) {
224:           apjdense[pjj[k]] = -1;
225:           apj[apnzj++]     = pjj[k];
226:         }
227:         apa[pjj[k]] += (*aa)*paj[k];
228:       }
229:       PetscLogFlops(2.0*pnzj);
230:       aa++;
231:     }

233:     /* Sort the j index array for quick sparse axpy. */
234:     /* Note: a array does not need sorting as it is in dense storage locations. */
235:     PetscSortInt(apnzj,apj);

237:     /* Compute P^T*A*P using outer product (P^T)[:,j]*(A*P)[j,:]. */
238:     pnzi = pi[i+1] - pi[i];
239:     for (j=0; j<pnzi; j++) {
240:       nextap = 0;
241:       crow   = *pJ++;
242:       cjj    = cj + ci[crow];
243:       caj    = ca + ci[crow];
244:       /* Perform sparse axpy operation.  Note cjj includes apj. */
245:       for (k=0; nextap<apnzj; k++) {
246:         if (PetscUnlikelyDebug(k >= ci[crow+1] - ci[crow])) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"k too large k %d, crow %d",k,crow);
247:         if (cjj[k]==apj[nextap]) {
248:           caj[k] += (*pA)*apa[apj[nextap++]];
249:         }
250:       }
251:       PetscLogFlops(2.0*apnzj);
252:       pA++;
253:     }

255:     /* Zero the current row info for A*P */
256:     for (j=0; j<apnzj; j++) {
257:       apa[apj[j]]      = 0.;
258:       apjdense[apj[j]] = 0;
259:     }
260:   }

262:   /* Assemble the final matrix and clean up */
263:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
264:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

266:   PetscFree2(apa,apjdense);
267:   PetscFree(apj);
268:   return(0);
269: }

271: PetscErrorCode MatPtAPNumeric_SeqAIJ_SeqAIJ(Mat A,Mat P,Mat C)
272: {
273:   PetscErrorCode      ierr;
274:   Mat_MatTransMatMult *atb;

277:   MatCheckProduct(C,3);
278:   atb  = (Mat_MatTransMatMult*)C->product->data;
279:   if (!atb) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_PLIB,"Missing data structure");
280:   MatTranspose_SeqAIJ(P,MAT_REUSE_MATRIX,&atb->At);
281:   if (!C->ops->matmultnumeric) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_PLIB,"Missing numeric operation");
282:   /* when using rap, MatMatMatMultSymbolic used a different data */
283:   if (atb->data) C->product->data = atb->data;
284:   (*C->ops->matmatmultnumeric)(atb->At,A,P,C);
285:   C->product->data = atb;
286:   return(0);
287: }