1 2 /* 3 Defines projective product routines where A is a SeqAIJ matrix 4 C = P^T * A * P 5 */ 6 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 11 #undef __FUNCT__ 12 #define __FUNCT__ "MatPtAPSymbolic_SeqAIJ" 13 PetscErrorCode MatPtAPSymbolic_SeqAIJ(Mat A,Mat P,PetscReal fill,Mat *C) 14 { 15 PetscErrorCode ierr; 16 17 PetscFunctionBegin; 18 if (!P->ops->ptapsymbolic_seqaij) SETERRQ2(((PetscObject)A)->comm,PETSC_ERR_SUP,"Not implemented for A=%s and P=%s",((PetscObject)A)->type_name,((PetscObject)P)->type_name); 19 ierr = (*P->ops->ptapsymbolic_seqaij)(A,P,fill,C);CHKERRQ(ierr); 20 PetscFunctionReturn(0); 21 } 22 23 #undef __FUNCT__ 24 #define __FUNCT__ "MatPtAPNumeric_SeqAIJ" 25 PetscErrorCode MatPtAPNumeric_SeqAIJ(Mat A,Mat P,Mat C) 26 { 27 PetscErrorCode ierr; 28 29 PetscFunctionBegin; 30 if (!P->ops->ptapnumeric_seqaij) SETERRQ2(((PetscObject)A)->comm,PETSC_ERR_SUP,"Not implemented for A=%s and P=%s",((PetscObject)A)->type_name,((PetscObject)P)->type_name); 31 ierr = (*P->ops->ptapnumeric_seqaij)(A,P,C);CHKERRQ(ierr); 32 PetscFunctionReturn(0); 33 } 34 35 #undef __FUNCT__ 36 #define __FUNCT__ "PetscContainerDestroy_Mat_PtAP" 37 PetscErrorCode PetscContainerDestroy_Mat_PtAP(void *ptr) 38 { 39 PetscErrorCode ierr; 40 Mat_PtAP *ptap=(Mat_PtAP*)ptr; 41 42 PetscFunctionBegin; 43 ierr = PetscFree(ptap->apa);CHKERRQ(ierr); 44 ierr = PetscFree(ptap->api);CHKERRQ(ierr); 45 ierr = PetscFree(ptap->apj);CHKERRQ(ierr); 46 ierr = PetscFree(ptap);CHKERRQ(ierr); 47 PetscFunctionReturn(0); 48 } 49 50 #undef __FUNCT__ 51 #define __FUNCT__ "MatDestroy_SeqAIJ_PtAP" 52 PetscErrorCode MatDestroy_SeqAIJ_PtAP(Mat A) 53 { 54 PetscErrorCode ierr; 55 PetscContainer container; 56 Mat_PtAP *ptap=PETSC_NULL; 57 58 PetscFunctionBegin; 59 ierr = PetscObjectQuery((PetscObject)A,"Mat_PtAP",(PetscObject *)&container);CHKERRQ(ierr); 60 if (!container) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Container does not exit"); 61 ierr = PetscContainerGetPointer(container,(void **)&ptap);CHKERRQ(ierr); 62 A->ops->destroy = ptap->destroy; 63 if (A->ops->destroy) { 64 ierr = (*A->ops->destroy)(A);CHKERRQ(ierr); 65 } 66 ierr = PetscObjectCompose((PetscObject)A,"Mat_PtAP",0);CHKERRQ(ierr); 67 PetscFunctionReturn(0); 68 } 69 70 #undef __FUNCT__ 71 #define __FUNCT__ "MatPtAPSymbolic_SeqAIJ_SeqAIJ" 72 PetscErrorCode MatPtAPSymbolic_SeqAIJ_SeqAIJ(Mat A,Mat P,PetscReal fill,Mat *C) 73 { 74 PetscErrorCode ierr; 75 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*p = (Mat_SeqAIJ*)P->data,*c; 76 PetscInt *pti,*ptj,*ptJ,*ai=a->i,*aj=a->j,*pi=p->i,*pj=p->j,*api,*apj; 77 PetscInt *ci,*cj,ndouble_ap,ndouble_ptap; 78 PetscInt an=A->cmap->N,am=A->rmap->N,pn=P->cmap->N; 79 MatScalar *ca; 80 Mat_PtAP *ptap; 81 PetscContainer container; 82 83 PetscFunctionBegin; 84 /* Get ij structure of Pt = P^T */ 85 ierr = MatGetSymbolicTranspose_SeqAIJ(P,&pti,&ptj);CHKERRQ(ierr); 86 ptJ=ptj; 87 88 /* Get structure of AP = A*P */ 89 ierr = MatGetSymbolicMatMatMult_SeqAIJ_SeqAIJ(am,ai,aj,an,pn,pi,pj,fill,&api,&apj,&ndouble_ap);CHKERRQ(ierr); 90 91 /* Get structure of C = Pt*AP */ 92 ierr = MatGetSymbolicMatMatMult_SeqAIJ_SeqAIJ(pn,pti,ptj,am,pn,api,apj,fill,&ci,&cj,&ndouble_ptap);CHKERRQ(ierr); 93 #if defined(MV) 94 /* Allocate ci array, arrays for fill computation and */ 95 /* free space for accumulating nonzero column info */ 96 ierr = PetscMalloc((pn+1)*sizeof(PetscInt),&ci);CHKERRQ(ierr); 97 ci[0] = 0; 98 99 ierr = PetscMalloc((2*an+1)*sizeof(PetscInt),&ptadenserow);CHKERRQ(ierr); 100 ierr = PetscMemzero(ptadenserow,(2*an+1)*sizeof(PetscInt));CHKERRQ(ierr); 101 ptasparserow = ptadenserow + an; 102 103 /* create and initialize a linked list */ 104 nlnk = pn+1; 105 ierr = PetscLLCreate(pn,pn,nlnk,lnk,lnkbt);CHKERRQ(ierr); 106 107 /* Set initial free space to be fill*nnz(A). */ 108 /* This should be reasonable if sparsity of PtAP is similar to that of A. */ 109 ierr = PetscFreeSpaceGet((PetscInt)(fill*ai[am]),&free_space); 110 current_space = free_space; 111 112 /* Determine symbolic info for each row of C: */ 113 for (i=0;i<pn;i++) { 114 ptnzi = pti[i+1] - pti[i]; 115 ptanzi = 0; 116 /* Determine symbolic row of PtA: */ 117 for (j=0;j<ptnzi;j++) { 118 arow = *ptJ++; 119 anzj = ai[arow+1] - ai[arow]; 120 ajj = aj + ai[arow]; 121 for (k=0;k<anzj;k++) { 122 if (!ptadenserow[ajj[k]]) { 123 ptadenserow[ajj[k]] = -1; 124 ptasparserow[ptanzi++] = ajj[k]; 125 } 126 } 127 } 128 /* Using symbolic info for row of PtA, determine symbolic info for row of C: */ 129 ptaj = ptasparserow; 130 cnzi = 0; 131 for (j=0;j<ptanzi;j++) { 132 prow = *ptaj++; 133 pnzj = pi[prow+1] - pi[prow]; 134 pjj = pj + pi[prow]; 135 /* add non-zero cols of P into the sorted linked list lnk */ 136 ierr = PetscLLAdd(pnzj,pjj,pn,nlnk,lnk,lnkbt);CHKERRQ(ierr); 137 cnzi += nlnk; 138 } 139 140 /* If free space is not available, make more free space */ 141 /* Double the amount of total space in the list */ 142 if (current_space->local_remaining<cnzi) { 143 ierr = PetscFreeSpaceGet(cnzi+current_space->total_array_size,¤t_space);CHKERRQ(ierr); 144 nspacedouble++; 145 } 146 147 /* Copy data into free space, and zero out denserows */ 148 ierr = PetscLLClean(pn,pn,cnzi,lnk,current_space->array,lnkbt);CHKERRQ(ierr); 149 current_space->array += cnzi; 150 current_space->local_used += cnzi; 151 current_space->local_remaining -= cnzi; 152 153 for (j=0;j<ptanzi;j++) { 154 ptadenserow[ptasparserow[j]] = 0; 155 } 156 /* Aside: Perhaps we should save the pta info for the numerical factorization. */ 157 /* For now, we will recompute what is needed. */ 158 ci[i+1] = ci[i] + cnzi; 159 } 160 /* nnz is now stored in ci[ptm], column indices are in the list of free space */ 161 /* Allocate space for cj, initialize cj, and */ 162 /* destroy list of free space and other temporary array(s) */ 163 ierr = PetscMalloc((ci[pn]+1)*sizeof(PetscInt),&cj);CHKERRQ(ierr); 164 ierr = PetscFreeSpaceContiguous(&free_space,cj);CHKERRQ(ierr); 165 ierr = PetscFree(ptadenserow);CHKERRQ(ierr); 166 ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 167 168 #endif 169 170 /* Allocate space for ca */ 171 ierr = PetscMalloc((ci[pn]+1)*sizeof(MatScalar),&ca);CHKERRQ(ierr); 172 ierr = PetscMemzero(ca,(ci[pn]+1)*sizeof(MatScalar));CHKERRQ(ierr); 173 174 /* put together the new matrix */ 175 ierr = MatCreateSeqAIJWithArrays(((PetscObject)A)->comm,pn,pn,ci,cj,ca,C);CHKERRQ(ierr); 176 177 /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */ 178 /* Since these are PETSc arrays, change flags to free them as necessary. */ 179 c = (Mat_SeqAIJ *)(*C)->data; 180 c->free_a = PETSC_TRUE; 181 c->free_ij = PETSC_TRUE; 182 c->nonew = 0; 183 184 /* create a supporting struct for reuse by MatPtAPNumeric(), attach it to *C */ 185 ierr = PetscNew(Mat_PtAP,&ptap);CHKERRQ(ierr); 186 187 /* attach the supporting struct to C */ 188 ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr); 189 ierr = PetscContainerSetPointer(container,ptap);CHKERRQ(ierr); 190 ierr = PetscContainerSetUserDestroy(container,PetscContainerDestroy_Mat_PtAP);CHKERRQ(ierr); 191 ierr = PetscObjectCompose((PetscObject)(*C),"Mat_PtAP",(PetscObject)container);CHKERRQ(ierr); 192 ierr = PetscContainerDestroy(&container);CHKERRQ(ierr); 193 194 ptap->destroy = (*C)->ops->destroy; 195 (*C)->ops->destroy = MatDestroy_SeqAIJ_PtAP; 196 197 /* Allocate temporary array for storage of one row of A*P */ 198 ierr = PetscMalloc((pn+1)*sizeof(PetscScalar),&ptap->apa);CHKERRQ(ierr); 199 ierr = PetscMemzero(ptap->apa,(pn+1)*sizeof(MatScalar));CHKERRQ(ierr); 200 ptap->api = api; 201 ptap->apj = apj; 202 203 /* Clean up. */ 204 ierr = MatRestoreSymbolicTranspose_SeqAIJ(P,&pti,&ptj);CHKERRQ(ierr); 205 #if defined(PETSC_USE_INFO) 206 if (ci[pn] != 0) { 207 PetscReal apfill,ptapfill; 208 apfill = ((PetscReal)api[am])/(ai[am]+pi[an]); 209 if (apfill < 1.0) apfill = 1.0; 210 ierr = PetscInfo3((*C),"A*P: Reallocs %D; Fill ratio: given %G needed %G.\n",ndouble_ap,fill,apfill);CHKERRQ(ierr); 211 ptapfill = ((PetscReal)ci[pn])/(pi[an]+api[am]); 212 if (ptapfill < 1.0) ptapfill = 1.0; 213 ierr = PetscInfo3((*C),"Pt*AP: Reallocs %D; Fill ratio: given %G needed %G.\n",ndouble_ptap,fill,ptapfill);CHKERRQ(ierr); 214 215 ierr = PetscInfo1((*C),"Use MatPtAP(A,P,MatReuse,%G,&C) for best performance.\n",PetscMax(apfill,ptapfill));CHKERRQ(ierr); 216 ierr = PetscInfo4((*C),"nonzeros: A %D, P %D, A*P %D, C=PtAP %D\n",ai[am],pi[an],api[am],ci[pn]);CHKERRQ(ierr); 217 } else { 218 ierr = PetscInfo((*C),"Empty matrix product\n");CHKERRQ(ierr); 219 } 220 #endif 221 PetscFunctionReturn(0); 222 } 223 224 #undef __FUNCT__ 225 #define __FUNCT__ "MatPtAPNumeric_SeqAIJ_SeqAIJ" 226 PetscErrorCode MatPtAPNumeric_SeqAIJ_SeqAIJ(Mat A,Mat P,Mat C) 227 { 228 PetscErrorCode ierr; 229 Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 230 Mat_SeqAIJ *p = (Mat_SeqAIJ *) P->data; 231 Mat_SeqAIJ *c = (Mat_SeqAIJ *) C->data; 232 PetscInt *ai=a->i,*aj=a->j,*pi=p->i,*pj=p->j,*ci=c->i,*cj=c->j; 233 PetscScalar *aa=a->a,*pa=p->a; 234 PetscInt *apj,*pcol,*cjj,cnz; 235 PetscInt am=A->rmap->N,cm=C->rmap->N; 236 PetscInt i,j,k,anz,apnz,pnz,prow,crow,apcol,nextap; 237 PetscScalar *apa,*pval,*ca=c->a,*caj; 238 PetscBool sparse_axpy=PETSC_FALSE; 239 Mat_PtAP *ptap; 240 PetscContainer container; 241 242 PetscFunctionBegin; 243 ierr = PetscOptionsGetBool(PETSC_NULL,"-matptap_spaxpy",&sparse_axpy,PETSC_NULL);CHKERRQ(ierr); 244 ierr = PetscObjectQuery((PetscObject)C,"Mat_PtAP",(PetscObject *)&container);CHKERRQ(ierr); 245 if (!container) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Container does not exit"); 246 ierr = PetscContainerGetPointer(container,(void **)&ptap);CHKERRQ(ierr); 247 248 /* Get temporary array for storage of one row of A*P */ 249 apa = ptap->apa; 250 251 /* Clear old values in C */ 252 ierr = PetscMemzero(ca,ci[cm]*sizeof(MatScalar));CHKERRQ(ierr); 253 254 for (i=0;i<am;i++) { 255 /* Form sparse row of AP[i,:] = A[i,:]*P */ 256 anz = ai[i+1] - ai[i]; 257 apnz = 0; 258 for (j=0; j<anz; j++) { 259 prow = aj[j]; 260 pnz = pi[prow+1] - pi[prow]; 261 pcol = pj + pi[prow]; 262 pval = pa + pi[prow]; 263 for (k=0; k<pnz; k++) { 264 apa[pcol[k]] += aa[j]*pval[k]; 265 } 266 ierr = PetscLogFlops(2.0*pnz);CHKERRQ(ierr); 267 } 268 aj += anz; aa += anz; 269 270 /* Compute P^T*A*P using outer product P[i,:]^T*AP[i,:]. */ 271 apj = ptap->apj + ptap->api[i]; 272 apnz = ptap->api[i+1] - ptap->api[i]; 273 pnz = pi[i+1] - pi[i]; 274 pcol = pj + pi[i]; 275 pval = pa + pi[i]; 276 277 if (sparse_axpy){ /* Perform sparse axpy */ 278 for (j=0; j<pnz; j++) { 279 crow = pcol[j]; 280 cjj = cj + ci[crow]; 281 caj = ca + ci[crow]; 282 nextap = 0; 283 apcol = apj[nextap]; 284 for (k=0; nextap<apnz; k++) { 285 if (cjj[k] == apcol) { 286 caj[k] += pval[j]*apa[apcol]; 287 apcol = apj[++nextap]; 288 } 289 } 290 ierr = PetscLogFlops(2.0*apnz);CHKERRQ(ierr); 291 } 292 } else { /* Perform dense axpy */ 293 for (j=0; j<pnz; j++) { 294 crow = pcol[j]; 295 cjj = cj + ci[crow]; 296 caj = ca + ci[crow]; 297 cnz = ci[crow+1] - ci[crow]; 298 for (k=0; k<cnz; k++){ 299 caj[k] += pval[j]*apa[cjj[k]]; 300 } 301 ierr = PetscLogFlops(2.0*cnz);CHKERRQ(ierr); 302 } 303 } 304 305 /* Zero the current row info for A*P */ 306 for (j=0; j<apnz; j++) { 307 apcol = apj[j]; 308 apa[apcol] = 0.; 309 } 310 } 311 312 /* Assemble the final matrix and clean up */ 313 ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 314 ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 315 PetscFunctionReturn(0); 316 } 317