Actual source code: matptap.c

      1: #define PETSCMAT_DLL

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

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

     14: PetscErrorCode MatPtAPSymbolic_SeqAIJ(Mat A,Mat P,PetscReal fill,Mat *C)
     15: {

     19:   if (!P->ops->ptapsymbolic_seqaij) {
     20:     SETERRQ2(PETSC_ERR_SUP,"Not implemented for A=%s and P=%s",A->type_name,P->type_name);
     21:   }
     22:   (*P->ops->ptapsymbolic_seqaij)(A,P,fill,C);
     23:   return(0);
     24: }

     28: PetscErrorCode MatPtAPNumeric_SeqAIJ(Mat A,Mat P,Mat C)
     29: {

     33:   if (!P->ops->ptapnumeric_seqaij) {
     34:     SETERRQ2(PETSC_ERR_SUP,"Not implemented for A=%s and P=%s",A->type_name,P->type_name);
     35:   }
     36:   (*P->ops->ptapnumeric_seqaij)(A,P,C);
     37:   return(0);
     38: }

     42: PetscErrorCode MatPtAPSymbolic_SeqAIJ_SeqAIJ(Mat A,Mat P,PetscReal fill,Mat *C)
     43: {
     45:   FreeSpaceList  free_space=PETSC_NULL,current_space=PETSC_NULL;
     46:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*p = (Mat_SeqAIJ*)P->data,*c;
     47:   PetscInt       *pti,*ptj,*ptJ,*ai=a->i,*aj=a->j,*ajj,*pi=p->i,*pj=p->j,*pjj;
     48:   PetscInt       *ci,*cj,*ptadenserow,*ptasparserow,*ptaj;
     49:   PetscInt       an=A->N,am=A->M,pn=P->N,pm=P->M;
     50:   PetscInt       i,j,k,ptnzi,arow,anzj,ptanzi,prow,pnzj,cnzi,nlnk,*lnk;
     51:   MatScalar      *ca;
     52:   PetscBT        lnkbt;

     55:   /* Get ij structure of P^T */
     56:   MatGetSymbolicTranspose_SeqAIJ(P,&pti,&ptj);
     57:   ptJ=ptj;

     59:   /* Allocate ci array, arrays for fill computation and */
     60:   /* free space for accumulating nonzero column info */
     61:   PetscMalloc((pn+1)*sizeof(PetscInt),&ci);
     62:   ci[0] = 0;

     64:   PetscMalloc((2*an+1)*sizeof(PetscInt),&ptadenserow);
     65:   PetscMemzero(ptadenserow,(2*an+1)*sizeof(PetscInt));
     66:   ptasparserow = ptadenserow  + an;

     68:   /* create and initialize a linked list */
     69:   nlnk = pn+1;
     70:   PetscLLCreate(pn,pn,nlnk,lnk,lnkbt);

     72:   /* Set initial free space to be nnz(A) scaled by aspect ratio of P. */
     73:   /* This should be reasonable if sparsity of PtAP is similar to that of A. */
     74:   GetMoreSpace((ai[am]/pm)*pn,&free_space);
     75:   current_space = free_space;

     77:   /* Determine symbolic info for each row of C: */
     78:   for (i=0;i<pn;i++) {
     79:     ptnzi  = pti[i+1] - pti[i];
     80:     ptanzi = 0;
     81:     /* Determine symbolic row of PtA: */
     82:     for (j=0;j<ptnzi;j++) {
     83:       arow = *ptJ++;
     84:       anzj = ai[arow+1] - ai[arow];
     85:       ajj  = aj + ai[arow];
     86:       for (k=0;k<anzj;k++) {
     87:         if (!ptadenserow[ajj[k]]) {
     88:           ptadenserow[ajj[k]]    = -1;
     89:           ptasparserow[ptanzi++] = ajj[k];
     90:         }
     91:       }
     92:     }
     93:       /* Using symbolic info for row of PtA, determine symbolic info for row of C: */
     94:     ptaj = ptasparserow;
     95:     cnzi   = 0;
     96:     for (j=0;j<ptanzi;j++) {
     97:       prow = *ptaj++;
     98:       pnzj = pi[prow+1] - pi[prow];
     99:       pjj  = pj + pi[prow];
    100:       /* add non-zero cols of P into the sorted linked list lnk */
    101:       PetscLLAdd(pnzj,pjj,pn,nlnk,lnk,lnkbt);
    102:       cnzi += nlnk;
    103:     }
    104:
    105:     /* If free space is not available, make more free space */
    106:     /* Double the amount of total space in the list */
    107:     if (current_space->local_remaining<cnzi) {
    108:       GetMoreSpace(current_space->total_array_size,&current_space);
    109:     }

    111:     /* Copy data into free space, and zero out denserows */
    112:     PetscLLClean(pn,pn,cnzi,lnk,current_space->array,lnkbt);
    113:     current_space->array           += cnzi;
    114:     current_space->local_used      += cnzi;
    115:     current_space->local_remaining -= cnzi;
    116:
    117:     for (j=0;j<ptanzi;j++) {
    118:       ptadenserow[ptasparserow[j]] = 0;
    119:     }
    120:     /* Aside: Perhaps we should save the pta info for the numerical factorization. */
    121:     /*        For now, we will recompute what is needed. */
    122:     ci[i+1] = ci[i] + cnzi;
    123:   }
    124:   /* nnz is now stored in ci[ptm], column indices are in the list of free space */
    125:   /* Allocate space for cj, initialize cj, and */
    126:   /* destroy list of free space and other temporary array(s) */
    127:   PetscMalloc((ci[pn]+1)*sizeof(PetscInt),&cj);
    128:   MakeSpaceContiguous(&free_space,cj);
    129:   PetscFree(ptadenserow);
    130:   PetscLLDestroy(lnk,lnkbt);
    131:
    132:   /* Allocate space for ca */
    133:   PetscMalloc((ci[pn]+1)*sizeof(MatScalar),&ca);
    134:   PetscMemzero(ca,(ci[pn]+1)*sizeof(MatScalar));
    135:
    136:   /* put together the new matrix */
    137:   MatCreateSeqAIJWithArrays(A->comm,pn,pn,ci,cj,ca,C);

    139:   /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
    140:   /* Since these are PETSc arrays, change flags to free them as necessary. */
    141:   c = (Mat_SeqAIJ *)((*C)->data);
    142:   c->freedata = PETSC_TRUE;
    143:   c->nonew    = 0;

    145:   /* Clean up. */
    146:   MatRestoreSymbolicTranspose_SeqAIJ(P,&pti,&ptj);

    148:   return(0);
    149: }

    153: PetscErrorCode MatPtAPNumeric_SeqAIJ_SeqAIJ(Mat A,Mat P,Mat C)
    154: {
    156:   PetscInt       flops=0;
    157:   Mat_SeqAIJ     *a  = (Mat_SeqAIJ *) A->data;
    158:   Mat_SeqAIJ     *p  = (Mat_SeqAIJ *) P->data;
    159:   Mat_SeqAIJ     *c  = (Mat_SeqAIJ *) C->data;
    160:   PetscInt       *ai=a->i,*aj=a->j,*apj,*apjdense,*pi=p->i,*pj=p->j,*pJ=p->j,*pjj;
    161:   PetscInt       *ci=c->i,*cj=c->j,*cjj;
    162:   PetscInt       am=A->M,cn=C->N,cm=C->M;
    163:   PetscInt       i,j,k,anzi,pnzi,apnzj,nextap,pnzj,prow,crow;
    164:   MatScalar      *aa=a->a,*apa,*pa=p->a,*pA=p->a,*paj,*ca=c->a,*caj;

    167:   /* Allocate temporary array for storage of one row of A*P */
    168:   PetscMalloc(cn*(sizeof(MatScalar)+2*sizeof(PetscInt)),&apa);
    169:   PetscMemzero(apa,cn*(sizeof(MatScalar)+2*sizeof(PetscInt)));

    171:   apj      = (PetscInt *)(apa + cn);
    172:   apjdense = apj + cn;

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

    177:   for (i=0;i<am;i++) {
    178:     /* Form sparse row of A*P */
    179:     anzi  = ai[i+1] - ai[i];
    180:     apnzj = 0;
    181:     for (j=0;j<anzi;j++) {
    182:       prow = *aj++;
    183:       pnzj = pi[prow+1] - pi[prow];
    184:       pjj  = pj + pi[prow];
    185:       paj  = pa + pi[prow];
    186:       for (k=0;k<pnzj;k++) {
    187:         if (!apjdense[pjj[k]]) {
    188:           apjdense[pjj[k]] = -1;
    189:           apj[apnzj++]     = pjj[k];
    190:         }
    191:         apa[pjj[k]] += (*aa)*paj[k];
    192:       }
    193:       flops += 2*pnzj;
    194:       aa++;
    195:     }

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

    201:     /* Compute P^T*A*P using outer product (P^T)[:,j]*(A*P)[j,:]. */
    202:     pnzi = pi[i+1] - pi[i];
    203:     for (j=0;j<pnzi;j++) {
    204:       nextap = 0;
    205:       crow   = *pJ++;
    206:       cjj    = cj + ci[crow];
    207:       caj    = ca + ci[crow];
    208:       /* Perform sparse axpy operation.  Note cjj includes apj. */
    209:       for (k=0;nextap<apnzj;k++) {
    210:         if (cjj[k]==apj[nextap]) {
    211:           caj[k] += (*pA)*apa[apj[nextap++]];
    212:         }
    213:       }
    214:       flops += 2*apnzj;
    215:       pA++;
    216:     }

    218:     /* Zero the current row info for A*P */
    219:     for (j=0;j<apnzj;j++) {
    220:       apa[apj[j]]      = 0.;
    221:       apjdense[apj[j]] = 0;
    222:     }
    223:   }

    225:   /* Assemble the final matrix and clean up */
    226:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
    227:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
    228:   PetscFree(apa);
    229:   PetscLogFlops(flops);

    231:   return(0);
    232: }