1 #define PETSCMAT_DLL 2 3 /* 4 Defines projective product routines where A is a MPIAIJ matrix 5 C = P^T * A * P 6 */ 7 8 #include "src/mat/impls/aij/seq/aij.h" /*I "petscmat.h" I*/ 9 #include "src/mat/utils/freespace.h" 10 #include "src/mat/impls/aij/mpi/mpiaij.h" 11 #include "petscbt.h" 12 13 #undef __FUNCT__ 14 #define __FUNCT__ "MatPtAPSymbolic_MPIAIJ" 15 PetscErrorCode MatPtAPSymbolic_MPIAIJ(Mat A,Mat P,PetscReal fill,Mat *C) 16 { 17 PetscErrorCode ierr; 18 19 PetscFunctionBegin; 20 if (!P->ops->ptapsymbolic_mpiaij) { 21 SETERRQ2(PETSC_ERR_SUP,"Not implemented for A=%s and P=%s",A->type_name,P->type_name); 22 } 23 ierr = (*P->ops->ptapsymbolic_mpiaij)(A,P,fill,C);CHKERRQ(ierr); 24 PetscFunctionReturn(0); 25 } 26 27 #undef __FUNCT__ 28 #define __FUNCT__ "MatPtAPNumeric_MPIAIJ" 29 PetscErrorCode MatPtAPNumeric_MPIAIJ(Mat A,Mat P,Mat C) 30 { 31 PetscErrorCode ierr; 32 33 PetscFunctionBegin; 34 if (!P->ops->ptapnumeric_mpiaij) { 35 SETERRQ2(PETSC_ERR_SUP,"Not implemented for A=%s and P=%s",A->type_name,P->type_name); 36 } 37 ierr = (*P->ops->ptapnumeric_mpiaij)(A,P,C);CHKERRQ(ierr); 38 PetscFunctionReturn(0); 39 } 40 41 #undef __FUNCT__ 42 #define __FUNCT__ "MatPtAPSymbolic_MPIAIJ_MPIAIJ" 43 PetscErrorCode MatPtAPSymbolic_MPIAIJ_MPIAIJ(Mat A,Mat P,PetscReal fill,Mat *C) 44 { 45 PetscErrorCode ierr; 46 Mat B_mpi; 47 Mat_MatMatMultMPI *ap; 48 PetscObjectContainer container; 49 FreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL; 50 Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data,*p=(Mat_MPIAIJ*)P->data; 51 Mat_SeqAIJ *ad=(Mat_SeqAIJ*)(a->A)->data,*ao=(Mat_SeqAIJ*)(a->B)->data; 52 Mat_SeqAIJ *p_loc,*p_oth; 53 PetscInt *pi_loc,*pj_loc,*pi_oth,*pj_oth,*pdti,*pdtj,*poti,*potj,*ptJ; 54 PetscInt *adi=ad->i,*adj=ad->j,*aoi=ao->i,*aoj=ao->j,nnz; 55 PetscInt nlnk,*lnk,*owners_co,*coi,*coj,i,k,pnz,row; 56 PetscInt am=A->m,pN=P->N,pn=P->n; 57 PetscBT lnkbt; 58 MPI_Comm comm=A->comm; 59 PetscMPIInt size,rank,tag,*len_si,*len_s,*len_ri; 60 PetscInt **buf_rj,**buf_ri,**buf_ri_k; 61 PetscInt len,proc,*dnz,*onz,*owners; 62 PetscInt nzi,*bi,*bj; 63 PetscInt nrows,*buf_s,*buf_si,*buf_si_i,**nextrow,**nextci; 64 MPI_Request *swaits,*rwaits; 65 MPI_Status *sstatus,rstatus; 66 Mat_Merge_SeqsToMPI *merge; 67 PetscInt *api,*apj,*Jptr,apnz,*prmap=p->garray,pon; 68 PetscMPIInt j; 69 70 PetscFunctionBegin; 71 ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 72 ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 73 74 /* destroy the container 'Mat_MatMatMultMPI' in case that P is attached to it */ 75 ierr = PetscObjectQuery((PetscObject)P,"Mat_MatMatMultMPI",(PetscObject *)&container);CHKERRQ(ierr); 76 if (container) { 77 ierr = PetscObjectContainerDestroy(container);CHKERRQ(ierr); 78 ierr = PetscObjectCompose((PetscObject)P,"Mat_MatMatMultMPI",0);CHKERRQ(ierr); 79 } 80 81 /* create the container 'Mat_MatMatMultMPI' and attach it to P */ 82 ierr = PetscNew(Mat_MatMatMultMPI,&ap);CHKERRQ(ierr); 83 ap->abi=PETSC_NULL; ap->abj=PETSC_NULL; 84 ap->abnz_max = 0; 85 86 ierr = PetscObjectContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr); 87 ierr = PetscObjectContainerSetPointer(container,ap);CHKERRQ(ierr); 88 ierr = PetscObjectCompose((PetscObject)P,"Mat_MatMatMultMPI",(PetscObject)container);CHKERRQ(ierr); 89 ap->MatDestroy = P->ops->destroy; 90 P->ops->destroy = MatDestroy_MPIAIJ_MatMatMult; 91 ap->reuse = MAT_INITIAL_MATRIX; 92 ierr = PetscObjectContainerSetUserDestroy(container,PetscObjectContainerDestroy_Mat_MatMatMultMPI);CHKERRQ(ierr); 93 94 /* get P_oth by taking rows of P (= non-zero cols of local A) from other processors */ 95 ierr = MatGetBrowsOfAoCols(A,P,MAT_INITIAL_MATRIX,&ap->startsj,&ap->bufa,&ap->B_oth);CHKERRQ(ierr); 96 /* get P_loc by taking all local rows of P */ 97 ierr = MatGetLocalMat(P,MAT_INITIAL_MATRIX,&ap->B_loc);CHKERRQ(ierr); 98 99 p_loc = (Mat_SeqAIJ*)(ap->B_loc)->data; 100 p_oth = (Mat_SeqAIJ*)(ap->B_oth)->data; 101 pi_loc = p_loc->i; pj_loc = p_loc->j; 102 pi_oth = p_oth->i; pj_oth = p_oth->j; 103 104 /* first, compute symbolic AP = A_loc*P */ 105 /*--------------------------------------*/ 106 ierr = PetscMalloc((am+2)*sizeof(PetscInt),&api);CHKERRQ(ierr); 107 ap->abi = api; 108 api[0] = 0; 109 110 /* create and initialize a linked list */ 111 nlnk = pN+1; 112 ierr = PetscLLCreate(pN,pN,nlnk,lnk,lnkbt);CHKERRQ(ierr); 113 114 /* Initial FreeSpace size is fill*nnz(A) */ 115 ierr = GetMoreSpace((PetscInt)(fill*(adi[am]+aoi[am])),&free_space);CHKERRQ(ierr); 116 current_space = free_space; 117 118 for (i=0;i<am;i++) { 119 apnz = 0; 120 /* diagonal portion of A */ 121 nzi = adi[i+1] - adi[i]; 122 for (j=0; j<nzi; j++){ 123 row = *adj++; 124 pnz = pi_loc[row+1] - pi_loc[row]; 125 Jptr = pj_loc + pi_loc[row]; 126 /* add non-zero cols of P into the sorted linked list lnk */ 127 ierr = PetscLLAdd(pnz,Jptr,pN,nlnk,lnk,lnkbt);CHKERRQ(ierr); 128 apnz += nlnk; 129 } 130 /* off-diagonal portion of A */ 131 nzi = aoi[i+1] - aoi[i]; 132 for (j=0; j<nzi; j++){ 133 row = *aoj++; 134 pnz = pi_oth[row+1] - pi_oth[row]; 135 Jptr = pj_oth + pi_oth[row]; 136 ierr = PetscLLAdd(pnz,Jptr,pN,nlnk,lnk,lnkbt);CHKERRQ(ierr); 137 apnz += nlnk; 138 } 139 140 api[i+1] = api[i] + apnz; 141 if (ap->abnz_max < apnz) ap->abnz_max = apnz; 142 143 /* if free space is not available, double the total space in the list */ 144 if (current_space->local_remaining<apnz) { 145 ierr = GetMoreSpace(current_space->total_array_size,¤t_space);CHKERRQ(ierr); 146 } 147 148 /* Copy data into free space, then initialize lnk */ 149 ierr = PetscLLClean(pN,pN,apnz,lnk,current_space->array,lnkbt);CHKERRQ(ierr); 150 current_space->array += apnz; 151 current_space->local_used += apnz; 152 current_space->local_remaining -= apnz; 153 } 154 /* Allocate space for apj, initialize apj, and */ 155 /* destroy list of free space and other temporary array(s) */ 156 ierr = PetscMalloc((api[am]+1)*sizeof(PetscInt),&ap->abj);CHKERRQ(ierr); 157 apj = ap->abj; 158 ierr = MakeSpaceContiguous(&free_space,ap->abj);CHKERRQ(ierr); 159 160 /* determine symbolic Co=(p->B)^T*AP - send to others */ 161 /*----------------------------------------------------*/ 162 ierr = MatGetSymbolicTranspose_SeqAIJ(p->B,&poti,&potj);CHKERRQ(ierr); 163 164 /* then, compute symbolic Co = (p->B)^T*AP */ 165 pon = (p->B)->n; /* total num of rows to be sent to other processors 166 >= (num of nonzero rows of C_seq) - pn */ 167 ierr = PetscMalloc((pon+1)*sizeof(PetscInt),&coi);CHKERRQ(ierr); 168 coi[0] = 0; 169 170 /* set initial free space to be 3*pon*max( nnz(AP) per row) */ 171 nnz = 3*pon*ap->abnz_max + 1; 172 ierr = GetMoreSpace(nnz,&free_space); 173 current_space = free_space; 174 175 for (i=0; i<pon; i++) { 176 nnz = 0; 177 pnz = poti[i+1] - poti[i]; 178 j = pnz; 179 ptJ = potj + poti[i+1]; 180 while (j){/* assume cols are almost in increasing order, starting from its end saves computation */ 181 j--; ptJ--; 182 row = *ptJ; /* row of AP == col of Pot */ 183 apnz = api[row+1] - api[row]; 184 Jptr = apj + api[row]; 185 /* add non-zero cols of AP into the sorted linked list lnk */ 186 ierr = PetscLLAdd(apnz,Jptr,pN,nlnk,lnk,lnkbt);CHKERRQ(ierr); 187 nnz += nlnk; 188 } 189 190 /* If free space is not available, double the total space in the list */ 191 if (current_space->local_remaining<nnz) { 192 ierr = GetMoreSpace(current_space->total_array_size,¤t_space);CHKERRQ(ierr); 193 } 194 195 /* Copy data into free space, and zero out denserows */ 196 ierr = PetscLLClean(pN,pN,nnz,lnk,current_space->array,lnkbt);CHKERRQ(ierr); 197 current_space->array += nnz; 198 current_space->local_used += nnz; 199 current_space->local_remaining -= nnz; 200 coi[i+1] = coi[i] + nnz; 201 } 202 ierr = PetscMalloc((coi[pon]+1)*sizeof(PetscInt),&coj);CHKERRQ(ierr); 203 ierr = MakeSpaceContiguous(&free_space,coj);CHKERRQ(ierr); 204 ierr = MatRestoreSymbolicTranspose_SeqAIJ(p->B,&poti,&potj);CHKERRQ(ierr); 205 206 /* send j-array (coj) of Co to other processors */ 207 /*----------------------------------------------*/ 208 /* determine row ownership */ 209 ierr = PetscNew(Mat_Merge_SeqsToMPI,&merge);CHKERRQ(ierr); 210 ierr = PetscMapCreate(comm,&merge->rowmap);CHKERRQ(ierr); 211 ierr = PetscMapSetLocalSize(merge->rowmap,pn);CHKERRQ(ierr); 212 ierr = PetscMapSetType(merge->rowmap,MAP_MPI);CHKERRQ(ierr); 213 ierr = PetscMapGetGlobalRange(merge->rowmap,&owners);CHKERRQ(ierr); 214 215 /* determine the number of messages to send, their lengths */ 216 ierr = PetscMalloc(size*sizeof(PetscMPIInt),&len_si);CHKERRQ(ierr); 217 ierr = PetscMemzero(len_si,size*sizeof(PetscMPIInt));CHKERRQ(ierr); 218 ierr = PetscMalloc(size*sizeof(PetscMPIInt),&merge->len_s);CHKERRQ(ierr); 219 len_s = merge->len_s; 220 merge->nsend = 0; 221 222 ierr = PetscMalloc((size+2)*sizeof(PetscInt),&owners_co);CHKERRQ(ierr); 223 ierr = PetscMemzero(len_s,size*sizeof(PetscMPIInt));CHKERRQ(ierr); 224 225 proc = 0; 226 for (i=0; i<pon; i++){ 227 while (prmap[i] >= owners[proc+1]) proc++; 228 len_si[proc]++; /* num of rows in Co to be sent to [proc] */ 229 len_s[proc] += coi[i+1] - coi[i]; 230 } 231 232 len = 0; /* max length of buf_si[] */ 233 owners_co[0] = 0; 234 for (proc=0; proc<size; proc++){ 235 owners_co[proc+1] = owners_co[proc] + len_si[proc]; 236 if (len_si[proc]){ 237 merge->nsend++; 238 len_si[proc] = 2*(len_si[proc] + 1); 239 len += len_si[proc]; 240 } 241 } 242 243 /* determine the number and length of messages to receive for coi and coj */ 244 ierr = PetscGatherNumberOfMessages(comm,PETSC_NULL,len_s,&merge->nrecv);CHKERRQ(ierr); 245 ierr = PetscGatherMessageLengths2(comm,merge->nsend,merge->nrecv,len_s,len_si,&merge->id_r,&merge->len_r,&len_ri);CHKERRQ(ierr); 246 247 /* post the Irecv and Isend of coj */ 248 ierr = PetscObjectGetNewTag((PetscObject)merge->rowmap,&tag);CHKERRQ(ierr); 249 ierr = PetscPostIrecvInt(comm,tag,merge->nrecv,merge->id_r,merge->len_r,&buf_rj,&rwaits);CHKERRQ(ierr); 250 251 ierr = PetscMalloc((merge->nsend+1)*sizeof(MPI_Request),&swaits);CHKERRQ(ierr); 252 253 for (proc=0, k=0; proc<size; proc++){ 254 if (!len_s[proc]) continue; 255 i = owners_co[proc]; 256 ierr = MPI_Isend(coj+coi[i],len_s[proc],MPIU_INT,proc,tag,comm,swaits+k);CHKERRQ(ierr); 257 k++; 258 } 259 260 /* receives and sends of coj are complete */ 261 ierr = PetscMalloc(size*sizeof(MPI_Status),&sstatus);CHKERRQ(ierr); 262 i = merge->nrecv; 263 while (i--) { 264 ierr = MPI_Waitany(merge->nrecv,rwaits,&j,&rstatus);CHKERRQ(ierr); 265 } 266 ierr = PetscFree(rwaits);CHKERRQ(ierr); 267 if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,swaits,sstatus);CHKERRQ(ierr);} 268 269 /* send and recv coi */ 270 /*-------------------*/ 271 ierr = PetscPostIrecvInt(comm,tag,merge->nrecv,merge->id_r,len_ri,&buf_ri,&rwaits);CHKERRQ(ierr); 272 273 ierr = PetscMalloc((len+1)*sizeof(PetscInt),&buf_s);CHKERRQ(ierr); 274 buf_si = buf_s; /* points to the beginning of k-th msg to be sent */ 275 for (proc=0,k=0; proc<size; proc++){ 276 if (!len_s[proc]) continue; 277 /* form outgoing message for i-structure: 278 buf_si[0]: nrows to be sent 279 [1:nrows]: row index (global) 280 [nrows+1:2*nrows+1]: i-structure index 281 */ 282 /*-------------------------------------------*/ 283 nrows = len_si[proc]/2 - 1; 284 buf_si_i = buf_si + nrows+1; 285 buf_si[0] = nrows; 286 buf_si_i[0] = 0; 287 nrows = 0; 288 for (i=owners_co[proc]; i<owners_co[proc+1]; i++){ 289 nzi = coi[i+1] - coi[i]; 290 buf_si_i[nrows+1] = buf_si_i[nrows] + nzi; /* i-structure */ 291 buf_si[nrows+1] =prmap[i] -owners[proc]; /* local row index */ 292 nrows++; 293 } 294 ierr = MPI_Isend(buf_si,len_si[proc],MPIU_INT,proc,tag,comm,swaits+k);CHKERRQ(ierr); 295 k++; 296 buf_si += len_si[proc]; 297 } 298 i = merge->nrecv; 299 while (i--) { 300 ierr = MPI_Waitany(merge->nrecv,rwaits,&j,&rstatus);CHKERRQ(ierr); 301 } 302 ierr = PetscFree(rwaits);CHKERRQ(ierr); 303 if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,swaits,sstatus);CHKERRQ(ierr);} 304 305 ierr = PetscLogInfo(((PetscObject)A,"MatMerge_SeqsToMPI: nsend: %d, nrecv: %d\n",merge->nsend,merge->nrecv));CHKERRQ(ierr); 306 for (i=0; i<merge->nrecv; i++){ 307 ierr = PetscLogInfo(((PetscObject)A,"MatMerge_SeqsToMPI: recv len_ri=%d, len_rj=%d from [%d]\n",len_ri[i],merge->len_r[i],merge->id_r[i]));CHKERRQ(ierr); 308 } 309 310 ierr = PetscFree(len_si);CHKERRQ(ierr); 311 ierr = PetscFree(len_ri);CHKERRQ(ierr); 312 ierr = PetscFree(swaits);CHKERRQ(ierr); 313 ierr = PetscFree(sstatus);CHKERRQ(ierr); 314 ierr = PetscFree(buf_s);CHKERRQ(ierr); 315 316 /* compute the local portion of C (mpi mat) */ 317 /*------------------------------------------*/ 318 ierr = MatGetSymbolicTranspose_SeqAIJ(p->A,&pdti,&pdtj);CHKERRQ(ierr); 319 320 /* allocate bi array and free space for accumulating nonzero column info */ 321 ierr = PetscMalloc((pn+1)*sizeof(PetscInt),&bi);CHKERRQ(ierr); 322 bi[0] = 0; 323 324 /* set initial free space to be 3*pn*max( nnz(AP) per row) */ 325 nnz = 3*pn*ap->abnz_max + 1; 326 ierr = GetMoreSpace(nnz,&free_space); 327 current_space = free_space; 328 329 ierr = PetscMalloc((3*merge->nrecv+1)*sizeof(PetscInt**),&buf_ri_k);CHKERRQ(ierr); 330 nextrow = buf_ri_k + merge->nrecv; 331 nextci = nextrow + merge->nrecv; 332 for (k=0; k<merge->nrecv; k++){ 333 buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */ 334 nrows = *buf_ri_k[k]; 335 nextrow[k] = buf_ri_k[k] + 1; /* next row number of k-th recved i-structure */ 336 nextci[k] = buf_ri_k[k] + (nrows + 1);/* poins to the next i-structure of k-th recved i-structure */ 337 } 338 ierr = MatPreallocateInitialize(comm,pn,pn,dnz,onz);CHKERRQ(ierr); 339 for (i=0; i<pn; i++) { 340 /* add pdt[i,:]*AP into lnk */ 341 nnz = 0; 342 pnz = pdti[i+1] - pdti[i]; 343 j = pnz; 344 ptJ = pdtj + pdti[i+1]; 345 while (j){/* assume cols are almost in increasing order, starting from its end saves computation */ 346 j--; ptJ--; 347 row = *ptJ; /* row of AP == col of Pt */ 348 apnz = api[row+1] - api[row]; 349 Jptr = apj + api[row]; 350 /* add non-zero cols of AP into the sorted linked list lnk */ 351 ierr = PetscLLAdd(apnz,Jptr,pN,nlnk,lnk,lnkbt);CHKERRQ(ierr); 352 nnz += nlnk; 353 } 354 /* add received col data into lnk */ 355 for (k=0; k<merge->nrecv; k++){ /* k-th received message */ 356 if (i == *nextrow[k]) { /* i-th row */ 357 nzi = *(nextci[k]+1) - *nextci[k]; 358 Jptr = buf_rj[k] + *nextci[k]; 359 ierr = PetscLLAdd(nzi,Jptr,pN,nlnk,lnk,lnkbt);CHKERRQ(ierr); 360 nnz += nlnk; 361 nextrow[k]++; nextci[k]++; 362 } 363 } 364 365 /* if free space is not available, make more free space */ 366 if (current_space->local_remaining<nnz) { 367 ierr = GetMoreSpace(current_space->total_array_size,¤t_space);CHKERRQ(ierr); 368 } 369 /* copy data into free space, then initialize lnk */ 370 ierr = PetscLLClean(pN,pN,nnz,lnk,current_space->array,lnkbt);CHKERRQ(ierr); 371 ierr = MatPreallocateSet(i+owners[rank],nnz,current_space->array,dnz,onz);CHKERRQ(ierr); 372 current_space->array += nnz; 373 current_space->local_used += nnz; 374 current_space->local_remaining -= nnz; 375 bi[i+1] = bi[i] + nnz; 376 } 377 ierr = MatRestoreSymbolicTranspose_SeqAIJ(p->A,&pdti,&pdtj);CHKERRQ(ierr); 378 ierr = PetscFree(buf_ri_k);CHKERRQ(ierr); 379 380 ierr = PetscMalloc((bi[pn]+1)*sizeof(PetscInt),&bj);CHKERRQ(ierr); 381 ierr = MakeSpaceContiguous(&free_space,bj);CHKERRQ(ierr); 382 ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 383 384 /* create symbolic parallel matrix B_mpi */ 385 /*---------------------------------------*/ 386 ierr = MatCreate(comm,pn,pn,PETSC_DETERMINE,PETSC_DETERMINE,&B_mpi);CHKERRQ(ierr); 387 ierr = MatSetType(B_mpi,MATMPIAIJ);CHKERRQ(ierr); 388 ierr = MatMPIAIJSetPreallocation(B_mpi,0,dnz,0,onz);CHKERRQ(ierr); 389 ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr); 390 391 /* B_mpi is not ready for use - assembly will be done by MatPtAPNumeric() */ 392 B_mpi->assembled = PETSC_FALSE; 393 B_mpi->ops->destroy = MatDestroy_MPIAIJ_SeqsToMPI; 394 merge->bi = bi; 395 merge->bj = bj; 396 merge->coi = coi; 397 merge->coj = coj; 398 merge->buf_ri = buf_ri; 399 merge->buf_rj = buf_rj; 400 merge->owners_co = owners_co; 401 402 /* attach the supporting struct to B_mpi for reuse */ 403 ierr = PetscObjectContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr); 404 ierr = PetscObjectContainerSetPointer(container,merge);CHKERRQ(ierr); 405 ierr = PetscObjectCompose((PetscObject)B_mpi,"MatMergeSeqsToMPI",(PetscObject)container);CHKERRQ(ierr); 406 *C = B_mpi; 407 PetscFunctionReturn(0); 408 } 409 410 #undef __FUNCT__ 411 #define __FUNCT__ "MatPtAPNumeric_MPIAIJ_MPIAIJ" 412 PetscErrorCode MatPtAPNumeric_MPIAIJ_MPIAIJ(Mat A,Mat P,Mat C) 413 { 414 PetscErrorCode ierr; 415 Mat_Merge_SeqsToMPI *merge; 416 Mat_MatMatMultMPI *ap; 417 PetscObjectContainer cont_merge,cont_ptap; 418 Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data,*p=(Mat_MPIAIJ*)P->data; 419 Mat_SeqAIJ *ad=(Mat_SeqAIJ*)(a->A)->data,*ao=(Mat_SeqAIJ*)(a->B)->data; 420 Mat_SeqAIJ *pd=(Mat_SeqAIJ*)(p->A)->data,*po=(Mat_SeqAIJ*)(p->B)->data; 421 Mat_SeqAIJ *p_loc,*p_oth; 422 PetscInt *adi=ad->i,*aoi=ao->i,*adj=ad->j,*aoj=ao->j,*apJ,nextp,flops=0; 423 PetscInt *pi_loc,*pj_loc,*pi_oth,*pj_oth,*pJ,*pj; 424 PetscInt i,j,k,anz,pnz,apnz,nextap,row,*cj; 425 MatScalar *ada=ad->a,*aoa=ao->a,*apa,*pa,*ca,*pa_loc,*pa_oth; 426 PetscInt am=A->m,cm=C->m,pon=(p->B)->n; 427 MPI_Comm comm=C->comm; 428 PetscMPIInt size,rank,taga,*len_s; 429 PetscInt *owners,proc,nrows,**buf_ri_k,**nextrow,**nextci; 430 PetscInt **buf_ri,**buf_rj; 431 PetscInt cnz=0,*bj_i,*bi,*bj,bnz,nextcj; /* bi,bj,ba: local array of C(mpi mat) */ 432 MPI_Request *s_waits,*r_waits; 433 MPI_Status *status; 434 MatScalar **abuf_r,*ba_i,*pA,*coa,*ba; 435 PetscInt *api,*apj,*coi,*coj; 436 PetscInt *poJ=po->j,*pdJ=pd->j,pcstart=p->cstart,pcend=p->cend; 437 438 PetscFunctionBegin; 439 ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 440 ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 441 442 ierr = PetscObjectQuery((PetscObject)C,"MatMergeSeqsToMPI",(PetscObject *)&cont_merge);CHKERRQ(ierr); 443 if (cont_merge) { 444 ierr = PetscObjectContainerGetPointer(cont_merge,(void **)&merge);CHKERRQ(ierr); 445 } else { 446 SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Matrix C does not posses an object container"); 447 } 448 449 ierr = PetscObjectQuery((PetscObject)P,"Mat_MatMatMultMPI",(PetscObject *)&cont_ptap);CHKERRQ(ierr); 450 if (cont_ptap) { 451 ierr = PetscObjectContainerGetPointer(cont_ptap,(void **)&ap);CHKERRQ(ierr); 452 if (ap->reuse == MAT_INITIAL_MATRIX){ 453 ap->reuse = MAT_REUSE_MATRIX; 454 } else { /* update numerical values of P_oth and P_loc */ 455 ierr = MatGetBrowsOfAoCols(A,P,MAT_REUSE_MATRIX,&ap->startsj,&ap->bufa,&ap->B_oth);CHKERRQ(ierr); 456 ierr = MatGetLocalMat(P,MAT_REUSE_MATRIX,&ap->B_loc);CHKERRQ(ierr); 457 } 458 } else { 459 SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Matrix P does not posses an object container"); 460 } 461 462 /* get data from symbolic products */ 463 p_loc = (Mat_SeqAIJ*)(ap->B_loc)->data; 464 p_oth = (Mat_SeqAIJ*)(ap->B_oth)->data; 465 pi_loc=p_loc->i; pj_loc=p_loc->j; pJ=pj_loc; pa_loc=p_loc->a,pA=pa_loc; 466 pi_oth=p_oth->i; pj_oth=p_oth->j; pa_oth=p_oth->a; 467 468 coi = merge->coi; coj = merge->coj; 469 ierr = PetscMalloc((coi[pon]+1)*sizeof(MatScalar),&coa);CHKERRQ(ierr); 470 ierr = PetscMemzero(coa,coi[pon]*sizeof(MatScalar));CHKERRQ(ierr); 471 472 bi = merge->bi; bj = merge->bj; 473 ierr = PetscMapGetGlobalRange(merge->rowmap,&owners);CHKERRQ(ierr); 474 ierr = PetscMalloc((bi[cm]+1)*sizeof(MatScalar),&ba);CHKERRQ(ierr); 475 ierr = PetscMemzero(ba,bi[cm]*sizeof(MatScalar));CHKERRQ(ierr); 476 477 /* get data from symbolic A*P */ 478 ierr = PetscMalloc((ap->abnz_max+1)*sizeof(MatScalar),&apa);CHKERRQ(ierr); 479 ierr = PetscMemzero(apa,ap->abnz_max*sizeof(MatScalar));CHKERRQ(ierr); 480 481 /* compute numeric C_seq=P_loc^T*A_loc*P */ 482 api = ap->abi; apj = ap->abj; 483 for (i=0;i<am;i++) { 484 /* form i-th sparse row of A*P */ 485 apnz = api[i+1] - api[i]; 486 apJ = apj + api[i]; 487 /* diagonal portion of A */ 488 anz = adi[i+1] - adi[i]; 489 for (j=0;j<anz;j++) { 490 row = *adj++; 491 pnz = pi_loc[row+1] - pi_loc[row]; 492 pj = pj_loc + pi_loc[row]; 493 pa = pa_loc + pi_loc[row]; 494 nextp = 0; 495 for (k=0; nextp<pnz; k++) { 496 if (apJ[k] == pj[nextp]) { /* col of AP == col of P */ 497 apa[k] += (*ada)*pa[nextp++]; 498 } 499 } 500 flops += 2*pnz; 501 ada++; 502 } 503 /* off-diagonal portion of A */ 504 anz = aoi[i+1] - aoi[i]; 505 for (j=0; j<anz; j++) { 506 row = *aoj++; 507 pnz = pi_oth[row+1] - pi_oth[row]; 508 pj = pj_oth + pi_oth[row]; 509 pa = pa_oth + pi_oth[row]; 510 nextp = 0; 511 for (k=0; nextp<pnz; k++) { 512 if (apJ[k] == pj[nextp]) { /* col of AP == col of P */ 513 apa[k] += (*aoa)*pa[nextp++]; 514 } 515 } 516 flops += 2*pnz; 517 aoa++; 518 } 519 520 /* Compute P_loc[i,:]^T*AP[i,:] using outer product */ 521 pnz = pi_loc[i+1] - pi_loc[i]; 522 for (j=0; j<pnz; j++) { 523 nextap = 0; 524 row = *pJ++; /* global index */ 525 if (row < pcstart || row >=pcend) { /* put the value into Co */ 526 cj = coj + coi[*poJ]; 527 ca = coa + coi[*poJ++]; 528 } else { /* put the value into Cd */ 529 cj = bj + bi[*pdJ]; 530 ca = ba + bi[*pdJ++]; 531 } 532 for (k=0; nextap<apnz; k++) { 533 if (cj[k]==apJ[nextap]) ca[k] += (*pA)*apa[nextap++]; 534 } 535 flops += 2*apnz; 536 pA++; 537 } 538 539 /* zero the current row info for A*P */ 540 ierr = PetscMemzero(apa,apnz*sizeof(MatScalar));CHKERRQ(ierr); 541 } 542 ierr = PetscFree(apa);CHKERRQ(ierr); 543 544 /* send and recv matrix values */ 545 /*-----------------------------*/ 546 buf_ri = merge->buf_ri; 547 buf_rj = merge->buf_rj; 548 len_s = merge->len_s; 549 ierr = PetscObjectGetNewTag((PetscObject)merge->rowmap,&taga);CHKERRQ(ierr); 550 ierr = PetscPostIrecvScalar(comm,taga,merge->nrecv,merge->id_r,merge->len_r,&abuf_r,&r_waits);CHKERRQ(ierr); 551 552 ierr = PetscMalloc((merge->nsend+1)*sizeof(MPI_Request),&s_waits);CHKERRQ(ierr); 553 for (proc=0,k=0; proc<size; proc++){ 554 if (!len_s[proc]) continue; 555 i = merge->owners_co[proc]; 556 ierr = MPI_Isend(coa+coi[i],len_s[proc],MPIU_MATSCALAR,proc,taga,comm,s_waits+k);CHKERRQ(ierr); 557 k++; 558 } 559 ierr = PetscMalloc(size*sizeof(MPI_Status),&status);CHKERRQ(ierr); 560 if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,r_waits,status);CHKERRQ(ierr);} 561 if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,s_waits,status);CHKERRQ(ierr);} 562 ierr = PetscFree(status);CHKERRQ(ierr); 563 564 ierr = PetscFree(s_waits);CHKERRQ(ierr); 565 ierr = PetscFree(r_waits);CHKERRQ(ierr); 566 ierr = PetscFree(coa);CHKERRQ(ierr); 567 568 /* insert local and received values into C */ 569 /*-----------------------------------------*/ 570 ierr = PetscMalloc((3*merge->nrecv+1)*sizeof(PetscInt**),&buf_ri_k);CHKERRQ(ierr); 571 nextrow = buf_ri_k + merge->nrecv; 572 nextci = nextrow + merge->nrecv; 573 574 for (k=0; k<merge->nrecv; k++){ 575 buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */ 576 nrows = *(buf_ri_k[k]); 577 nextrow[k] = buf_ri_k[k]+1; /* next row number of k-th recved i-structure */ 578 nextci[k] = buf_ri_k[k] + (nrows + 1);/* poins to the next i-structure of k-th recved i-structure */ 579 } 580 581 for (i=0; i<cm; i++) { 582 row = owners[rank] + i; /* global row index of C_seq */ 583 bj_i = bj + bi[i]; /* col indices of the i-th row of C */ 584 ba_i = ba + bi[i]; 585 bnz = bi[i+1] - bi[i]; 586 /* add received vals into ba */ 587 for (k=0; k<merge->nrecv; k++){ /* k-th received message */ 588 /* i-th row */ 589 if (i == *nextrow[k]) { 590 cnz = *(nextci[k]+1) - *nextci[k]; 591 cj = buf_rj[k] + *(nextci[k]); 592 ca = abuf_r[k] + *(nextci[k]); 593 nextcj = 0; 594 for (j=0; nextcj<cnz; j++){ 595 if (bj_i[j] == cj[nextcj]){ /* bcol == ccol */ 596 ba_i[j] += ca[nextcj++]; 597 } 598 } 599 nextrow[k]++; nextci[k]++; 600 } 601 } 602 ierr = MatSetValues(C,1,&row,bnz,bj_i,ba_i,INSERT_VALUES);CHKERRQ(ierr); 603 flops += 2*cnz; 604 } 605 ierr = MatSetOption(C,MAT_COLUMNS_SORTED);CHKERRQ(ierr); /* -cause delay? */ 606 ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 607 ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 608 609 ierr = PetscFree(ba);CHKERRQ(ierr); 610 ierr = PetscFree(abuf_r);CHKERRQ(ierr); 611 ierr = PetscFree(buf_ri_k);CHKERRQ(ierr); 612 ierr = PetscLogFlops(flops);CHKERRQ(ierr); 613 PetscFunctionReturn(0); 614 } 615