1 /* 2 Defines projective product routines where A is a SeqAIJ matrix 3 C = P^T * A * P 4 */ 5 6 #include "src/mat/impls/aij/seq/aij.h" /*I "petscmat.h" I*/ 7 #include "src/mat/utils/freespace.h" 8 #include "petscbt.h" 9 10 11 #undef __FUNCT__ 12 #define __FUNCT__ "MatPtAP_SeqAIJ_SeqAIJ" 13 PetscErrorCode MatPtAP_SeqAIJ_SeqAIJ(Mat A,Mat P,MatReuse scall,PetscReal fill,Mat *C) 14 { 15 PetscErrorCode ierr; 16 17 PetscFunctionBegin; 18 if (scall == MAT_INITIAL_MATRIX){ 19 ierr = PetscLogEventBegin(MAT_PtAPSymbolic,A,P,0,0);CHKERRQ(ierr); 20 ierr = MatPtAPSymbolic_SeqAIJ_SeqAIJ(A,P,fill,C);CHKERRQ(ierr); 21 ierr = PetscLogEventEnd(MAT_PtAPSymbolic,A,P,0,0);CHKERRQ(ierr); 22 } 23 ierr = PetscLogEventBegin(MAT_PtAPNumeric,A,P,0,0);CHKERRQ(ierr); 24 ierr = MatPtAPNumeric_SeqAIJ_SeqAIJ(A,P,*C);CHKERRQ(ierr); 25 ierr = PetscLogEventEnd(MAT_PtAPNumeric,A,P,0,0);CHKERRQ(ierr); 26 PetscFunctionReturn(0); 27 } 28 29 #undef __FUNCT__ 30 #define __FUNCT__ "MatPtAPSymbolic_SeqAIJ_SeqAIJ" 31 PetscErrorCode MatPtAPSymbolic_SeqAIJ_SeqAIJ(Mat A,Mat P,PetscReal fill,Mat *C) 32 { 33 PetscErrorCode ierr; 34 FreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL; 35 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*p = (Mat_SeqAIJ*)P->data,*c; 36 PetscInt *pti,*ptj,*ptJ,*ai=a->i,*aj=a->j,*ajj,*pi=p->i,*pj=p->j,*pjj; 37 PetscInt *ci,*cj,*ptadenserow,*ptasparserow,*ptaj; 38 PetscInt an=A->N,am=A->M,pn=P->N,pm=P->M; 39 PetscInt i,j,k,ptnzi,arow,anzj,ptanzi,prow,pnzj,cnzi,nlnk,*lnk; 40 MatScalar *ca; 41 PetscBT lnkbt; 42 43 PetscFunctionBegin; 44 /* Get ij structure of P^T */ 45 ierr = MatGetSymbolicTranspose_SeqAIJ(P,&pti,&ptj);CHKERRQ(ierr); 46 ptJ=ptj; 47 48 /* Allocate ci array, arrays for fill computation and */ 49 /* free space for accumulating nonzero column info */ 50 ierr = PetscMalloc((pn+1)*sizeof(PetscInt),&ci);CHKERRQ(ierr); 51 ci[0] = 0; 52 53 ierr = PetscMalloc((2*an+1)*sizeof(PetscInt),&ptadenserow);CHKERRQ(ierr); 54 ierr = PetscMemzero(ptadenserow,(2*an+1)*sizeof(PetscInt));CHKERRQ(ierr); 55 ptasparserow = ptadenserow + an; 56 57 /* create and initialize a linked list */ 58 nlnk = pn+1; 59 ierr = PetscLLCreate(pn,pn,nlnk,lnk,lnkbt);CHKERRQ(ierr); 60 61 /* Set initial free space to be nnz(A) scaled by aspect ratio of P. */ 62 /* This should be reasonable if sparsity of PtAP is similar to that of A. */ 63 ierr = GetMoreSpace((ai[am]/pm)*pn,&free_space); 64 current_space = free_space; 65 66 /* Determine symbolic info for each row of C: */ 67 for (i=0;i<pn;i++) { 68 ptnzi = pti[i+1] - pti[i]; 69 ptanzi = 0; 70 /* Determine symbolic row of PtA: */ 71 for (j=0;j<ptnzi;j++) { 72 arow = *ptJ++; 73 anzj = ai[arow+1] - ai[arow]; 74 ajj = aj + ai[arow]; 75 for (k=0;k<anzj;k++) { 76 if (!ptadenserow[ajj[k]]) { 77 ptadenserow[ajj[k]] = -1; 78 ptasparserow[ptanzi++] = ajj[k]; 79 } 80 } 81 } 82 /* Using symbolic info for row of PtA, determine symbolic info for row of C: */ 83 ptaj = ptasparserow; 84 cnzi = 0; 85 for (j=0;j<ptanzi;j++) { 86 prow = *ptaj++; 87 pnzj = pi[prow+1] - pi[prow]; 88 pjj = pj + pi[prow]; 89 /* add non-zero cols of P into the sorted linked list lnk */ 90 ierr = PetscLLAdd(pnzj,pjj,pn,nlnk,lnk,lnkbt);CHKERRQ(ierr); 91 cnzi += nlnk; 92 } 93 94 /* If free space is not available, make more free space */ 95 /* Double the amount of total space in the list */ 96 if (current_space->local_remaining<cnzi) { 97 ierr = GetMoreSpace(current_space->total_array_size,¤t_space);CHKERRQ(ierr); 98 } 99 100 /* Copy data into free space, and zero out denserows */ 101 ierr = PetscLLClean(pn,pn,cnzi,lnk,current_space->array,lnkbt);CHKERRQ(ierr); 102 current_space->array += cnzi; 103 current_space->local_used += cnzi; 104 current_space->local_remaining -= cnzi; 105 106 for (j=0;j<ptanzi;j++) { 107 ptadenserow[ptasparserow[j]] = 0; 108 } 109 /* Aside: Perhaps we should save the pta info for the numerical factorization. */ 110 /* For now, we will recompute what is needed. */ 111 ci[i+1] = ci[i] + cnzi; 112 } 113 /* nnz is now stored in ci[ptm], column indices are in the list of free space */ 114 /* Allocate space for cj, initialize cj, and */ 115 /* destroy list of free space and other temporary array(s) */ 116 ierr = PetscMalloc((ci[pn]+1)*sizeof(PetscInt),&cj);CHKERRQ(ierr); 117 ierr = MakeSpaceContiguous(&free_space,cj);CHKERRQ(ierr); 118 ierr = PetscFree(ptadenserow);CHKERRQ(ierr); 119 ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 120 121 /* Allocate space for ca */ 122 ierr = PetscMalloc((ci[pn]+1)*sizeof(MatScalar),&ca);CHKERRQ(ierr); 123 ierr = PetscMemzero(ca,(ci[pn]+1)*sizeof(MatScalar));CHKERRQ(ierr); 124 125 /* put together the new matrix */ 126 ierr = MatCreateSeqAIJWithArrays(A->comm,pn,pn,ci,cj,ca,C);CHKERRQ(ierr); 127 128 /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */ 129 /* Since these are PETSc arrays, change flags to free them as necessary. */ 130 c = (Mat_SeqAIJ *)((*C)->data); 131 c->freedata = PETSC_TRUE; 132 c->nonew = 0; 133 134 /* Clean up. */ 135 ierr = MatRestoreSymbolicTranspose_SeqAIJ(P,&pti,&ptj);CHKERRQ(ierr); 136 137 PetscFunctionReturn(0); 138 } 139 140 #include "src/mat/impls/maij/maij.h" 141 #undef __FUNCT__ 142 #define __FUNCT__ "MatPtAPSymbolic_SeqAIJ_SeqMAIJ" 143 PetscErrorCode MatPtAPSymbolic_SeqAIJ_SeqMAIJ(Mat A,Mat PP,PetscReal fill,Mat *C) 144 { 145 /* This routine requires testing -- but it's getting better. */ 146 PetscErrorCode ierr; 147 FreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL; 148 Mat_SeqMAIJ *pp=(Mat_SeqMAIJ*)PP->data; 149 Mat P=pp->AIJ; 150 Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data,*p=(Mat_SeqAIJ*)P->data,*c; 151 PetscInt *pti,*ptj,*ptJ,*ai=a->i,*aj=a->j,*ajj,*pi=p->i,*pj=p->j,*pjj; 152 PetscInt *ci,*cj,*ptadenserow,*ptasparserow,*denserow,*sparserow,*ptaj; 153 PetscInt an=A->N,am=A->M,pn=P->N,pm=P->M,ppdof=pp->dof,cn; 154 PetscInt i,j,k,dof,pshift,ptnzi,arow,anzj,ptanzi,prow,pnzj,cnzi; 155 MatScalar *ca; 156 157 PetscFunctionBegin; 158 /* Start timer */ 159 ierr = PetscLogEventBegin(MAT_PtAPSymbolic,A,PP,0,0);CHKERRQ(ierr); 160 161 /* Get ij structure of P^T */ 162 ierr = MatGetSymbolicTranspose_SeqAIJ(P,&pti,&ptj);CHKERRQ(ierr); 163 164 cn = pn*ppdof; 165 /* Allocate ci array, arrays for fill computation and */ 166 /* free space for accumulating nonzero column info */ 167 ierr = PetscMalloc((cn+1)*sizeof(PetscInt),&ci);CHKERRQ(ierr); 168 ci[0] = 0; 169 170 /* Work arrays for rows of P^T*A */ 171 ierr = PetscMalloc((2*cn+2*an+1)*sizeof(PetscInt),&ptadenserow);CHKERRQ(ierr); 172 ierr = PetscMemzero(ptadenserow,(2*cn+2*an+1)*sizeof(PetscInt));CHKERRQ(ierr); 173 ptasparserow = ptadenserow + an; 174 denserow = ptasparserow + an; 175 sparserow = denserow + cn; 176 177 /* Set initial free space to be nnz(A) scaled by aspect ratio of P. */ 178 /* This should be reasonable if sparsity of PtAP is similar to that of A. */ 179 /* Note, aspect ratio of P is the same as the aspect ratio of SeqAIJ inside P */ 180 ierr = GetMoreSpace((ai[am]/pm)*pn,&free_space); 181 current_space = free_space; 182 183 /* Determine symbolic info for each row of C: */ 184 for (i=0;i<pn;i++) { 185 ptnzi = pti[i+1] - pti[i]; 186 ptJ = ptj + pti[i]; 187 for (dof=0;dof<ppdof;dof++) { 188 ptanzi = 0; 189 /* Determine symbolic row of PtA: */ 190 for (j=0;j<ptnzi;j++) { 191 /* Expand ptJ[j] by block size and shift by dof to get the right row of A */ 192 arow = ptJ[j]*ppdof + dof; 193 /* Nonzeros of P^T*A will be in same locations as any element of A in that row */ 194 anzj = ai[arow+1] - ai[arow]; 195 ajj = aj + ai[arow]; 196 for (k=0;k<anzj;k++) { 197 if (!ptadenserow[ajj[k]]) { 198 ptadenserow[ajj[k]] = -1; 199 ptasparserow[ptanzi++] = ajj[k]; 200 } 201 } 202 } 203 /* Using symbolic info for row of PtA, determine symbolic info for row of C: */ 204 ptaj = ptasparserow; 205 cnzi = 0; 206 for (j=0;j<ptanzi;j++) { 207 /* Get offset within block of P */ 208 pshift = *ptaj%ppdof; 209 /* Get block row of P */ 210 prow = (*ptaj++)/ppdof; /* integer division */ 211 /* P has same number of nonzeros per row as the compressed form */ 212 pnzj = pi[prow+1] - pi[prow]; 213 pjj = pj + pi[prow]; 214 for (k=0;k<pnzj;k++) { 215 /* Locations in C are shifted by the offset within the block */ 216 /* Note: we cannot use PetscLLAdd here because of the additional offset for the write location */ 217 if (!denserow[pjj[k]*ppdof+pshift]) { 218 denserow[pjj[k]*ppdof+pshift] = -1; 219 sparserow[cnzi++] = pjj[k]*ppdof+pshift; 220 } 221 } 222 } 223 224 /* sort sparserow */ 225 ierr = PetscSortInt(cnzi,sparserow);CHKERRQ(ierr); 226 227 /* If free space is not available, make more free space */ 228 /* Double the amount of total space in the list */ 229 if (current_space->local_remaining<cnzi) { 230 ierr = GetMoreSpace(current_space->total_array_size,¤t_space);CHKERRQ(ierr); 231 } 232 233 /* Copy data into free space, and zero out denserows */ 234 ierr = PetscMemcpy(current_space->array,sparserow,cnzi*sizeof(PetscInt));CHKERRQ(ierr); 235 current_space->array += cnzi; 236 current_space->local_used += cnzi; 237 current_space->local_remaining -= cnzi; 238 239 for (j=0;j<ptanzi;j++) { 240 ptadenserow[ptasparserow[j]] = 0; 241 } 242 for (j=0;j<cnzi;j++) { 243 denserow[sparserow[j]] = 0; 244 } 245 /* Aside: Perhaps we should save the pta info for the numerical factorization. */ 246 /* For now, we will recompute what is needed. */ 247 ci[i*ppdof+1+dof] = ci[i*ppdof+dof] + cnzi; 248 } 249 } 250 /* nnz is now stored in ci[ptm], column indices are in the list of free space */ 251 /* Allocate space for cj, initialize cj, and */ 252 /* destroy list of free space and other temporary array(s) */ 253 ierr = PetscMalloc((ci[cn]+1)*sizeof(PetscInt),&cj);CHKERRQ(ierr); 254 ierr = MakeSpaceContiguous(&free_space,cj);CHKERRQ(ierr); 255 ierr = PetscFree(ptadenserow);CHKERRQ(ierr); 256 257 /* Allocate space for ca */ 258 ierr = PetscMalloc((ci[cn]+1)*sizeof(MatScalar),&ca);CHKERRQ(ierr); 259 ierr = PetscMemzero(ca,(ci[cn]+1)*sizeof(MatScalar));CHKERRQ(ierr); 260 261 /* put together the new matrix */ 262 ierr = MatCreateSeqAIJWithArrays(A->comm,cn,cn,ci,cj,ca,C);CHKERRQ(ierr); 263 264 /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */ 265 /* Since these are PETSc arrays, change flags to free them as necessary. */ 266 c = (Mat_SeqAIJ *)((*C)->data); 267 c->freedata = PETSC_TRUE; 268 c->nonew = 0; 269 270 /* Clean up. */ 271 ierr = MatRestoreSymbolicTranspose_SeqAIJ(P,&pti,&ptj);CHKERRQ(ierr); 272 273 ierr = PetscLogEventEnd(MAT_PtAPSymbolic,A,PP,0,0);CHKERRQ(ierr); 274 PetscFunctionReturn(0); 275 } 276 277 #undef __FUNCT__ 278 #define __FUNCT__ "MatPtAPNumeric_SeqAIJ_SeqAIJ" 279 PetscErrorCode MatPtAPNumeric_SeqAIJ_SeqAIJ(Mat A,Mat P,Mat C) 280 { 281 PetscErrorCode ierr; 282 PetscInt flops=0; 283 Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 284 Mat_SeqAIJ *p = (Mat_SeqAIJ *) P->data; 285 Mat_SeqAIJ *c = (Mat_SeqAIJ *) C->data; 286 PetscInt *ai=a->i,*aj=a->j,*apj,*apjdense,*pi=p->i,*pj=p->j,*pJ=p->j,*pjj; 287 PetscInt *ci=c->i,*cj=c->j,*cjj; 288 PetscInt am=A->M,cn=C->N,cm=C->M; 289 PetscInt i,j,k,anzi,pnzi,apnzj,nextap,pnzj,prow,crow; 290 MatScalar *aa=a->a,*apa,*pa=p->a,*pA=p->a,*paj,*ca=c->a,*caj; 291 292 PetscFunctionBegin; 293 /* Allocate temporary array for storage of one row of A*P */ 294 ierr = PetscMalloc(cn*(sizeof(MatScalar)+2*sizeof(PetscInt)),&apa);CHKERRQ(ierr); 295 ierr = PetscMemzero(apa,cn*(sizeof(MatScalar)+2*sizeof(PetscInt)));CHKERRQ(ierr); 296 297 apj = (PetscInt *)(apa + cn); 298 apjdense = apj + cn; 299 300 /* Clear old values in C */ 301 ierr = PetscMemzero(ca,ci[cm]*sizeof(MatScalar));CHKERRQ(ierr); 302 303 for (i=0;i<am;i++) { 304 /* Form sparse row of A*P */ 305 anzi = ai[i+1] - ai[i]; 306 apnzj = 0; 307 for (j=0;j<anzi;j++) { 308 prow = *aj++; 309 pnzj = pi[prow+1] - pi[prow]; 310 pjj = pj + pi[prow]; 311 paj = pa + pi[prow]; 312 for (k=0;k<pnzj;k++) { 313 if (!apjdense[pjj[k]]) { 314 apjdense[pjj[k]] = -1; 315 apj[apnzj++] = pjj[k]; 316 } 317 apa[pjj[k]] += (*aa)*paj[k]; 318 } 319 flops += 2*pnzj; 320 aa++; 321 } 322 323 /* Sort the j index array for quick sparse axpy. */ 324 /* Note: a array does not need sorting as it is in dense storage locations. */ 325 ierr = PetscSortInt(apnzj,apj);CHKERRQ(ierr); 326 327 /* Compute P^T*A*P using outer product (P^T)[:,j]*(A*P)[j,:]. */ 328 pnzi = pi[i+1] - pi[i]; 329 for (j=0;j<pnzi;j++) { 330 nextap = 0; 331 crow = *pJ++; 332 cjj = cj + ci[crow]; 333 caj = ca + ci[crow]; 334 /* Perform sparse axpy operation. Note cjj includes apj. */ 335 for (k=0;nextap<apnzj;k++) { 336 if (cjj[k]==apj[nextap]) { 337 caj[k] += (*pA)*apa[apj[nextap++]]; 338 } 339 } 340 flops += 2*apnzj; 341 pA++; 342 } 343 344 /* Zero the current row info for A*P */ 345 for (j=0;j<apnzj;j++) { 346 apa[apj[j]] = 0.; 347 apjdense[apj[j]] = 0; 348 } 349 } 350 351 /* Assemble the final matrix and clean up */ 352 ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 353 ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 354 ierr = PetscFree(apa);CHKERRQ(ierr); 355 ierr = PetscLogFlops(flops);CHKERRQ(ierr); 356 357 PetscFunctionReturn(0); 358 } 359 360 #undef __FUNCT__ 361 #define __FUNCT__ "MatPtAPNumeric_SeqAIJ_SeqMAIJ" 362 PetscErrorCode MatPtAPNumeric_SeqAIJ_SeqMAIJ(Mat A,Mat PP,Mat C) 363 { 364 /* This routine requires testing -- first draft only */ 365 PetscErrorCode ierr; 366 PetscInt flops=0; 367 Mat_SeqMAIJ *pp=(Mat_SeqMAIJ*)PP->data; 368 Mat P=pp->AIJ; 369 Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 370 Mat_SeqAIJ *p = (Mat_SeqAIJ *) P->data; 371 Mat_SeqAIJ *c = (Mat_SeqAIJ *) C->data; 372 PetscInt *ai=a->i,*aj=a->j,*apj,*apjdense,*pi=p->i,*pj=p->j,*pJ=p->j,*pjj; 373 PetscInt *ci=c->i,*cj=c->j,*cjj; 374 PetscInt am=A->M,cn=C->N,cm=C->M,ppdof=pp->dof; 375 PetscInt i,j,k,pshift,poffset,anzi,pnzi,apnzj,nextap,pnzj,prow,crow; 376 MatScalar *aa=a->a,*apa,*pa=p->a,*pA=p->a,*paj,*ca=c->a,*caj; 377 378 PetscFunctionBegin; 379 /* Allocate temporary array for storage of one row of A*P */ 380 ierr = PetscMalloc(cn*(sizeof(MatScalar)+2*sizeof(PetscInt)),&apa);CHKERRQ(ierr); 381 ierr = PetscMemzero(apa,cn*(sizeof(MatScalar)+2*sizeof(PetscInt)));CHKERRQ(ierr); 382 383 apj = (PetscInt *)(apa + cn); 384 apjdense = apj + cn; 385 386 /* Clear old values in C */ 387 ierr = PetscMemzero(ca,ci[cm]*sizeof(MatScalar));CHKERRQ(ierr); 388 389 for (i=0;i<am;i++) { 390 /* Form sparse row of A*P */ 391 anzi = ai[i+1] - ai[i]; 392 apnzj = 0; 393 for (j=0;j<anzi;j++) { 394 /* Get offset within block of P */ 395 pshift = *aj%ppdof; 396 /* Get block row of P */ 397 prow = *aj++/ppdof; /* integer division */ 398 pnzj = pi[prow+1] - pi[prow]; 399 pjj = pj + pi[prow]; 400 paj = pa + pi[prow]; 401 for (k=0;k<pnzj;k++) { 402 poffset = pjj[k]*ppdof+pshift; 403 if (!apjdense[poffset]) { 404 apjdense[poffset] = -1; 405 apj[apnzj++] = poffset; 406 } 407 apa[poffset] += (*aa)*paj[k]; 408 } 409 flops += 2*pnzj; 410 aa++; 411 } 412 413 /* Sort the j index array for quick sparse axpy. */ 414 /* Note: a array does not need sorting as it is in dense storage locations. */ 415 ierr = PetscSortInt(apnzj,apj);CHKERRQ(ierr); 416 417 /* Compute P^T*A*P using outer product (P^T)[:,j]*(A*P)[j,:]. */ 418 prow = i/ppdof; /* integer division */ 419 pshift = i%ppdof; 420 poffset = pi[prow]; 421 pnzi = pi[prow+1] - poffset; 422 /* Reset pJ and pA so we can traverse the same row of P 'dof' times. */ 423 pJ = pj+poffset; 424 pA = pa+poffset; 425 for (j=0;j<pnzi;j++) { 426 crow = (*pJ)*ppdof+pshift; 427 cjj = cj + ci[crow]; 428 caj = ca + ci[crow]; 429 pJ++; 430 /* Perform sparse axpy operation. Note cjj includes apj. */ 431 for (k=0,nextap=0;nextap<apnzj;k++) { 432 if (cjj[k]==apj[nextap]) { 433 caj[k] += (*pA)*apa[apj[nextap++]]; 434 } 435 } 436 flops += 2*apnzj; 437 pA++; 438 } 439 440 /* Zero the current row info for A*P */ 441 for (j=0;j<apnzj;j++) { 442 apa[apj[j]] = 0.; 443 apjdense[apj[j]] = 0; 444 } 445 } 446 447 /* Assemble the final matrix and clean up */ 448 ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 449 ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 450 ierr = PetscFree(apa);CHKERRQ(ierr); 451 ierr = PetscLogFlops(flops);CHKERRQ(ierr); 452 453 PetscFunctionReturn(0); 454 } 455