Actual source code: matptap.c

petsc-dev 2014-02-02
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>   /*I "petscmat.h" I*/
  8: #include <../src/mat/utils/freespace.h>
  9: #include <petscbt.h>
 10: #include <petsctime.h>

 14: PetscErrorCode MatPtAP_SeqAIJ_SeqAIJ(Mat A,Mat P,MatReuse scall,PetscReal fill,Mat *C)
 15: {
 17:   const char     *algTypes[2] = {"scalable","nonscalable"};
 18:   PetscInt       alg=0; /* set default algorithm */

 21:   if (scall == MAT_INITIAL_MATRIX) {
 22:     /* 
 23:      Alg 'scalable' determines which implementations to be used:
 24:        "nonscalable": do dense axpy in MatPtAPNumeric() - fastest, but requires storage of struct A*P;
 25:        "scalable":    do two sparse axpy in MatPtAPNumeric() - might slow, does not store structure of A*P. 
 26:      */
 27:     PetscObjectOptionsBegin((PetscObject)A);
 28:     PetscOptionsEList("-matptap_via","Algorithmic approach","MatPtAP",algTypes,2,algTypes[0],&alg,NULL);
 29:     PetscOptionsEnd();
 30:     PetscLogEventBegin(MAT_PtAPSymbolic,A,P,0,0);
 31:     switch (alg) {
 32:     case 1:
 33:       MatPtAPSymbolic_SeqAIJ_SeqAIJ_DenseAxpy(A,P,fill,C);
 34:       break;
 35:     default:
 36:       MatPtAPSymbolic_SeqAIJ_SeqAIJ_SparseAxpy(A,P,fill,C);
 37:       break;
 38:     }
 39:     PetscLogEventEnd(MAT_PtAPSymbolic,A,P,0,0);
 40:   }
 41:   PetscLogEventBegin(MAT_PtAPNumeric,A,P,0,0);
 42:   (*(*C)->ops->ptapnumeric)(A,P,*C);
 43:   PetscLogEventEnd(MAT_PtAPNumeric,A,P,0,0);
 44:   return(0);
 45: }

 49: PetscErrorCode MatDestroy_SeqAIJ_PtAP(Mat A)
 50: {
 52:   Mat_SeqAIJ     *a    = (Mat_SeqAIJ*)A->data;
 53:   Mat_PtAP       *ptap = a->ptap;

 56:   PetscFree(ptap->apa);
 57:   PetscFree(ptap->api);
 58:   PetscFree(ptap->apj);
 59:   (ptap->destroy)(A);
 60:   PetscFree(ptap);
 61:   return(0);
 62: }

 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((PetscInt)(fill*(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(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:   MatCreateSeqAIJWithArrays(PetscObjectComm((PetscObject)A),pn,pn,ci,cj,ca,C);

161:   (*C)->rmap->bs = P->cmap->bs;
162:   (*C)->cmap->bs = P->cmap->bs;

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

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

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

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

209:   /* Allocate temporary array for storage of one row of A*P (cn: non-scalable) */
210:   PetscMalloc3(cn,&apa,cn,&apjdense,c->rmax,&apj);
211:   PetscMemzero(apa,cn*sizeof(MatScalar));
212:   PetscMemzero(apjdense,cn*sizeof(PetscInt));

214:   /* Clear old values in C */
215:   PetscMemzero(ca,ci[cm]*sizeof(MatScalar));

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

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

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

261:     /* Zero the current row info for A*P */
262:     for (j=0; j<apnzj; j++) {
263:       apa[apj[j]]      = 0.;
264:       apjdense[apj[j]] = 0;
265:     }
266:   }

268:   /* Assemble the final matrix and clean up */
269:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
270:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

272:   PetscFree3(apa,apjdense,apj);
273:   return(0);
274: }

278: PetscErrorCode MatPtAPSymbolic_SeqAIJ_SeqAIJ_DenseAxpy(Mat A,Mat P,PetscReal fill,Mat *C)
279: {
281:   Mat_SeqAIJ     *ap,*c;
282:   PetscInt       *api,*apj,*ci,pn=P->cmap->N;
283:   MatScalar      *ca;
284:   Mat_PtAP       *ptap;
285:   Mat            Pt,AP;

288:   /* Get symbolic Pt = P^T */
289:   MatTransposeSymbolic_SeqAIJ(P,&Pt);

291:   /* Get symbolic AP = A*P */
292:   MatMatMultSymbolic_SeqAIJ_SeqAIJ(A,P,fill,&AP);

294:   ap          = (Mat_SeqAIJ*)AP->data;
295:   api         = ap->i;
296:   apj         = ap->j;
297:   ap->free_ij = PETSC_FALSE; /* api and apj are kept in struct ptap, cannot be destroyed with AP */

299:   /* Get C = Pt*AP */
300:   MatMatMultSymbolic_SeqAIJ_SeqAIJ(Pt,AP,fill,C);

302:   c         = (Mat_SeqAIJ*)(*C)->data;
303:   ci        = c->i;
304:   PetscCalloc1(ci[pn]+1,&ca);
305:   c->a      = ca;
306:   c->free_a = PETSC_TRUE;

308:   /* Create a supporting struct for reuse by MatPtAPNumeric() */
309:   PetscNew(&ptap);

311:   c->ptap            = ptap;
312:   ptap->destroy      = (*C)->ops->destroy;
313:   (*C)->ops->destroy = MatDestroy_SeqAIJ_PtAP;

315:   /* Allocate temporary array for storage of one row of A*P */
316:   PetscCalloc1(pn+1,&ptap->apa);

318:   (*C)->ops->ptapnumeric = MatPtAPNumeric_SeqAIJ_SeqAIJ;

320:   ptap->api = api;
321:   ptap->apj = apj;

323:   /* Clean up. */
324:   MatDestroy(&Pt);
325:   MatDestroy(&AP);
326: #if defined(PETSC_USE_INFO)
327:   PetscInfo1((*C),"given fill %g\n",(double)fill);
328: #endif
329:   return(0);
330: }

332: /* #define PROFILE_MatPtAPNumeric */
335: PetscErrorCode MatPtAPNumeric_SeqAIJ_SeqAIJ(Mat A,Mat P,Mat C)
336: {
337:   PetscErrorCode    ierr;
338:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*) A->data;
339:   Mat_SeqAIJ        *p = (Mat_SeqAIJ*) P->data;
340:   Mat_SeqAIJ        *c = (Mat_SeqAIJ*) C->data;
341:   const PetscInt    *ai=a->i,*aj=a->j,*pi=p->i,*pj=p->j,*ci=c->i,*cj=c->j;
342:   const PetscScalar *aa=a->a,*pa=p->a,*pval;
343:   const PetscInt    *apj,*pcol,*cjj;
344:   const PetscInt    am=A->rmap->N,cm=C->rmap->N;
345:   PetscInt          i,j,k,anz,apnz,pnz,prow,crow,cnz;
346:   PetscScalar       *apa,*ca=c->a,*caj,pvalj;
347:   Mat_PtAP          *ptap = c->ptap;
348: #if defined(PROFILE_MatPtAPNumeric)
349:   PetscLogDouble t0,tf,time_Cseq0=0.0,time_Cseq1=0.0;
350:   PetscInt       flops0=0,flops1=0;
351: #endif

354:   /* Get temporary array for storage of one row of A*P */
355:   apa = ptap->apa;

357:   /* Clear old values in C */
358:   PetscMemzero(ca,ci[cm]*sizeof(MatScalar));

360:   for (i=0; i<am; i++) {
361:     /* Form sparse row of AP[i,:] = A[i,:]*P */
362: #if defined(PROFILE_MatPtAPNumeric)
363:     PetscTime(&t0);
364: #endif
365:     anz  = ai[i+1] - ai[i];
366:     apnz = 0;
367:     for (j=0; j<anz; j++) {
368:       prow = aj[j];
369:       pnz  = pi[prow+1] - pi[prow];
370:       pcol = pj + pi[prow];
371:       pval = pa + pi[prow];
372:       for (k=0; k<pnz; k++) {
373:         apa[pcol[k]] += aa[j]*pval[k];
374:       }
375:       PetscLogFlops(2.0*pnz);
376: #if defined(PROFILE_MatPtAPNumeric)
377:       flops0 += 2.0*pnz;
378: #endif
379:     }
380:     aj += anz; aa += anz;
381: #if defined(PROFILE_MatPtAPNumeric)
382:     PetscTime(&tf);

384:     time_Cseq0 += tf - t0;
385: #endif

387:     /* Compute P^T*A*P using outer product P[i,:]^T*AP[i,:]. */
388: #if defined(PROFILE_MatPtAPNumeric)
389:     PetscTime(&t0);
390: #endif
391:     apj  = ptap->apj + ptap->api[i];
392:     apnz = ptap->api[i+1] - ptap->api[i];
393:     pnz  = pi[i+1] - pi[i];
394:     pcol = pj + pi[i];
395:     pval = pa + pi[i];

397:     /* Perform dense axpy */
398:     for (j=0; j<pnz; j++) {
399:       crow  = pcol[j];
400:       cjj   = cj + ci[crow];
401:       caj   = ca + ci[crow];
402:       pvalj = pval[j];
403:       cnz   = ci[crow+1] - ci[crow];
404:       for (k=0; k<cnz; k++) caj[k] += pvalj*apa[cjj[k]];
405:       PetscLogFlops(2.0*cnz);
406: #if defined(PROFILE_MatPtAPNumeric)
407:       flops1 += 2.0*cnz;
408: #endif
409:     }
410: #if defined(PROFILE_MatPtAPNumeric)
411:     PetscTime(&tf);
412:     time_Cseq1 += tf - t0;
413: #endif

415:     /* Zero the current row info for A*P */
416:     for (j=0; j<apnz; j++) apa[apj[j]] = 0.0;
417:   }

419:   /* Assemble the final matrix and clean up */
420:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
421:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
422: #if defined(PROFILE_MatPtAPNumeric)
423:   printf("PtAPNumeric_SeqAIJ time %g + %g, flops %d %d\n",time_Cseq0,time_Cseq1,flops0,flops1);
424: #endif
425:   return(0);
426: }