1 2 /* 3 Routines to compute overlapping regions of a parallel MPI matrix 4 and to find submatrices that were shared across processors. 5 */ 6 #include <../src/mat/impls/baij/mpi/mpibaij.h> 7 #include <petscbt.h> 8 9 static PetscErrorCode MatIncreaseOverlap_MPIBAIJ_Local(Mat,PetscInt,char**,PetscInt*,PetscInt**); 10 static PetscErrorCode MatIncreaseOverlap_MPIBAIJ_Receive(Mat,PetscInt,PetscInt**,PetscInt**,PetscInt*); 11 extern PetscErrorCode MatGetRow_MPIBAIJ(Mat,PetscInt,PetscInt*,PetscInt**,PetscScalar**); 12 extern PetscErrorCode MatRestoreRow_MPIBAIJ(Mat,PetscInt,PetscInt*,PetscInt**,PetscScalar**); 13 14 PetscErrorCode MatIncreaseOverlap_MPIBAIJ(Mat C,PetscInt imax,IS is[],PetscInt ov) 15 { 16 PetscErrorCode ierr; 17 PetscInt i,N=C->cmap->N, bs=C->rmap->bs; 18 IS *is_new; 19 20 PetscFunctionBegin; 21 ierr = PetscMalloc1(imax,&is_new);CHKERRQ(ierr); 22 /* Convert the indices into block format */ 23 ierr = ISCompressIndicesGeneral(N,C->rmap->n,bs,imax,is,is_new);CHKERRQ(ierr); 24 if (ov < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative overlap specified\n"); 25 for (i=0; i<ov; ++i) { 26 ierr = MatIncreaseOverlap_MPIBAIJ_Once(C,imax,is_new);CHKERRQ(ierr); 27 } 28 for (i=0; i<imax; i++) {ierr = ISDestroy(&is[i]);CHKERRQ(ierr);} 29 ierr = ISExpandIndicesGeneral(N,N,bs,imax,is_new,is);CHKERRQ(ierr); 30 for (i=0; i<imax; i++) {ierr = ISDestroy(&is_new[i]);CHKERRQ(ierr);} 31 ierr = PetscFree(is_new);CHKERRQ(ierr); 32 PetscFunctionReturn(0); 33 } 34 35 /* 36 Sample message format: 37 If a processor A wants processor B to process some elements corresponding 38 to index sets is[1], is[5] 39 mesg [0] = 2 (no of index sets in the mesg) 40 ----------- 41 mesg [1] = 1 => is[1] 42 mesg [2] = sizeof(is[1]); 43 ----------- 44 mesg [5] = 5 => is[5] 45 mesg [6] = sizeof(is[5]); 46 ----------- 47 mesg [7] 48 mesg [n] data(is[1]) 49 ----------- 50 mesg[n+1] 51 mesg[m] data(is[5]) 52 ----------- 53 54 Notes: 55 nrqs - no of requests sent (or to be sent out) 56 nrqr - no of requests recieved (which have to be or which have been processed 57 */ 58 PetscErrorCode MatIncreaseOverlap_MPIBAIJ_Once(Mat C,PetscInt imax,IS is[]) 59 { 60 Mat_MPIBAIJ *c = (Mat_MPIBAIJ*)C->data; 61 const PetscInt **idx,*idx_i; 62 PetscInt *n,*w3,*w4,**data,len; 63 PetscErrorCode ierr; 64 PetscMPIInt size,rank,tag1,tag2,*w2,*w1,nrqr; 65 PetscInt Mbs,i,j,k,**rbuf,row,proc=-1,nrqs,msz,**outdat,**ptr; 66 PetscInt *ctr,*pa,*tmp,*isz,*isz1,**xdata,**rbuf2,*d_p; 67 PetscMPIInt *onodes1,*olengths1,*onodes2,*olengths2; 68 PetscBT *table; 69 MPI_Comm comm; 70 MPI_Request *s_waits1,*r_waits1,*s_waits2,*r_waits2; 71 MPI_Status *s_status,*recv_status; 72 char *t_p; 73 74 PetscFunctionBegin; 75 ierr = PetscObjectGetComm((PetscObject)C,&comm);CHKERRQ(ierr); 76 size = c->size; 77 rank = c->rank; 78 Mbs = c->Mbs; 79 80 ierr = PetscObjectGetNewTag((PetscObject)C,&tag1);CHKERRQ(ierr); 81 ierr = PetscObjectGetNewTag((PetscObject)C,&tag2);CHKERRQ(ierr); 82 83 ierr = PetscMalloc2(imax+1,&idx,imax,&n);CHKERRQ(ierr); 84 85 for (i=0; i<imax; i++) { 86 ierr = ISGetIndices(is[i],&idx[i]);CHKERRQ(ierr); 87 ierr = ISGetLocalSize(is[i],&n[i]);CHKERRQ(ierr); 88 } 89 90 /* evaluate communication - mesg to who,length of mesg, and buffer space 91 required. Based on this, buffers are allocated, and data copied into them*/ 92 ierr = PetscCalloc4(size,&w1,size,&w2,size,&w3,size,&w4);CHKERRQ(ierr); 93 for (i=0; i<imax; i++) { 94 ierr = PetscMemzero(w4,size*sizeof(PetscInt));CHKERRQ(ierr); /* initialise work vector*/ 95 idx_i = idx[i]; 96 len = n[i]; 97 for (j=0; j<len; j++) { 98 row = idx_i[j]; 99 if (row < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Index set cannot have negative entries"); 100 ierr = PetscLayoutFindOwner(C->rmap,row*C->rmap->bs,&proc);CHKERRQ(ierr); 101 w4[proc]++; 102 } 103 for (j=0; j<size; j++) { 104 if (w4[j]) { w1[j] += w4[j]; w3[j]++;} 105 } 106 } 107 108 nrqs = 0; /* no of outgoing messages */ 109 msz = 0; /* total mesg length (for all proc */ 110 w1[rank] = 0; /* no mesg sent to itself */ 111 w3[rank] = 0; 112 for (i=0; i<size; i++) { 113 if (w1[i]) {w2[i] = 1; nrqs++;} /* there exists a message to proc i */ 114 } 115 /* pa - is list of processors to communicate with */ 116 ierr = PetscMalloc1(nrqs+1,&pa);CHKERRQ(ierr); 117 for (i=0,j=0; i<size; i++) { 118 if (w1[i]) {pa[j] = i; j++;} 119 } 120 121 /* Each message would have a header = 1 + 2*(no of IS) + data */ 122 for (i=0; i<nrqs; i++) { 123 j = pa[i]; 124 w1[j] += w2[j] + 2*w3[j]; 125 msz += w1[j]; 126 } 127 128 /* Determine the number of messages to expect, their lengths, from from-ids */ 129 ierr = PetscGatherNumberOfMessages(comm,w2,w1,&nrqr);CHKERRQ(ierr); 130 ierr = PetscGatherMessageLengths(comm,nrqs,nrqr,w1,&onodes1,&olengths1);CHKERRQ(ierr); 131 132 /* Now post the Irecvs corresponding to these messages */ 133 ierr = PetscPostIrecvInt(comm,tag1,nrqr,onodes1,olengths1,&rbuf,&r_waits1);CHKERRQ(ierr); 134 135 /* Allocate Memory for outgoing messages */ 136 ierr = PetscMalloc4(size,&outdat,size,&ptr,msz,&tmp,size,&ctr);CHKERRQ(ierr); 137 ierr = PetscMemzero(outdat,size*sizeof(PetscInt*));CHKERRQ(ierr); 138 ierr = PetscMemzero(ptr,size*sizeof(PetscInt*));CHKERRQ(ierr); 139 { 140 PetscInt *iptr = tmp,ict = 0; 141 for (i=0; i<nrqs; i++) { 142 j = pa[i]; 143 iptr += ict; 144 outdat[j] = iptr; 145 ict = w1[j]; 146 } 147 } 148 149 /* Form the outgoing messages */ 150 /*plug in the headers*/ 151 for (i=0; i<nrqs; i++) { 152 j = pa[i]; 153 outdat[j][0] = 0; 154 ierr = PetscMemzero(outdat[j]+1,2*w3[j]*sizeof(PetscInt));CHKERRQ(ierr); 155 ptr[j] = outdat[j] + 2*w3[j] + 1; 156 } 157 158 /* Memory for doing local proc's work*/ 159 { 160 ierr = PetscCalloc5(imax,&table, imax,&data, imax,&isz, Mbs*imax,&d_p, (Mbs/PETSC_BITS_PER_BYTE+1)*imax,&t_p);CHKERRQ(ierr); 161 162 for (i=0; i<imax; i++) { 163 table[i] = t_p + (Mbs/PETSC_BITS_PER_BYTE+1)*i; 164 data[i] = d_p + (Mbs)*i; 165 } 166 } 167 168 /* Parse the IS and update local tables and the outgoing buf with the data*/ 169 { 170 PetscInt n_i,*data_i,isz_i,*outdat_j,ctr_j; 171 PetscBT table_i; 172 173 for (i=0; i<imax; i++) { 174 ierr = PetscMemzero(ctr,size*sizeof(PetscInt));CHKERRQ(ierr); 175 n_i = n[i]; 176 table_i = table[i]; 177 idx_i = idx[i]; 178 data_i = data[i]; 179 isz_i = isz[i]; 180 for (j=0; j<n_i; j++) { /* parse the indices of each IS */ 181 row = idx_i[j]; 182 ierr = PetscLayoutFindOwner(C->rmap,row*C->rmap->bs,&proc);CHKERRQ(ierr); 183 if (proc != rank) { /* copy to the outgoing buffer */ 184 ctr[proc]++; 185 *ptr[proc] = row; 186 ptr[proc]++; 187 } else { /* Update the local table */ 188 if (!PetscBTLookupSet(table_i,row)) data_i[isz_i++] = row; 189 } 190 } 191 /* Update the headers for the current IS */ 192 for (j=0; j<size; j++) { /* Can Optimise this loop by using pa[] */ 193 if ((ctr_j = ctr[j])) { 194 outdat_j = outdat[j]; 195 k = ++outdat_j[0]; 196 outdat_j[2*k] = ctr_j; 197 outdat_j[2*k-1] = i; 198 } 199 } 200 isz[i] = isz_i; 201 } 202 } 203 204 /* Now post the sends */ 205 ierr = PetscMalloc1(nrqs+1,&s_waits1);CHKERRQ(ierr); 206 for (i=0; i<nrqs; ++i) { 207 j = pa[i]; 208 ierr = MPI_Isend(outdat[j],w1[j],MPIU_INT,j,tag1,comm,s_waits1+i);CHKERRQ(ierr); 209 } 210 211 /* No longer need the original indices*/ 212 for (i=0; i<imax; ++i) { 213 ierr = ISRestoreIndices(is[i],idx+i);CHKERRQ(ierr); 214 } 215 ierr = PetscFree2(idx,n);CHKERRQ(ierr); 216 217 for (i=0; i<imax; ++i) { 218 ierr = ISDestroy(&is[i]);CHKERRQ(ierr); 219 } 220 221 /* Do Local work*/ 222 ierr = MatIncreaseOverlap_MPIBAIJ_Local(C,imax,table,isz,data);CHKERRQ(ierr); 223 224 /* Receive messages*/ 225 ierr = PetscMalloc1(nrqr+1,&recv_status);CHKERRQ(ierr); 226 if (nrqr) {ierr = MPI_Waitall(nrqr,r_waits1,recv_status);CHKERRQ(ierr);} 227 228 ierr = PetscMalloc1(nrqs+1,&s_status);CHKERRQ(ierr); 229 if (nrqs) {ierr = MPI_Waitall(nrqs,s_waits1,s_status);CHKERRQ(ierr);} 230 231 /* Phase 1 sends are complete - deallocate buffers */ 232 ierr = PetscFree4(outdat,ptr,tmp,ctr);CHKERRQ(ierr); 233 ierr = PetscFree4(w1,w2,w3,w4);CHKERRQ(ierr); 234 235 ierr = PetscMalloc1(nrqr+1,&xdata);CHKERRQ(ierr); 236 ierr = PetscMalloc1(nrqr+1,&isz1);CHKERRQ(ierr); 237 ierr = MatIncreaseOverlap_MPIBAIJ_Receive(C,nrqr,rbuf,xdata,isz1);CHKERRQ(ierr); 238 ierr = PetscFree(rbuf[0]);CHKERRQ(ierr); 239 ierr = PetscFree(rbuf);CHKERRQ(ierr); 240 241 /* Send the data back*/ 242 /* Do a global reduction to know the buffer space req for incoming messages*/ 243 { 244 PetscMPIInt *rw1; 245 246 ierr = PetscCalloc1(size,&rw1);CHKERRQ(ierr); 247 248 for (i=0; i<nrqr; ++i) { 249 proc = recv_status[i].MPI_SOURCE; 250 if (proc != onodes1[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"MPI_SOURCE mismatch"); 251 rw1[proc] = isz1[i]; 252 } 253 254 ierr = PetscFree(onodes1);CHKERRQ(ierr); 255 ierr = PetscFree(olengths1);CHKERRQ(ierr); 256 257 /* Determine the number of messages to expect, their lengths, from from-ids */ 258 ierr = PetscGatherMessageLengths(comm,nrqr,nrqs,rw1,&onodes2,&olengths2);CHKERRQ(ierr); 259 ierr = PetscFree(rw1);CHKERRQ(ierr); 260 } 261 /* Now post the Irecvs corresponding to these messages */ 262 ierr = PetscPostIrecvInt(comm,tag2,nrqs,onodes2,olengths2,&rbuf2,&r_waits2);CHKERRQ(ierr); 263 264 /* Now post the sends */ 265 ierr = PetscMalloc1(nrqr+1,&s_waits2);CHKERRQ(ierr); 266 for (i=0; i<nrqr; ++i) { 267 j = recv_status[i].MPI_SOURCE; 268 ierr = MPI_Isend(xdata[i],isz1[i],MPIU_INT,j,tag2,comm,s_waits2+i);CHKERRQ(ierr); 269 } 270 271 /* receive work done on other processors*/ 272 { 273 PetscMPIInt idex; 274 PetscInt is_no,ct1,max,*rbuf2_i,isz_i,*data_i,jmax; 275 PetscBT table_i; 276 MPI_Status *status2; 277 278 ierr = PetscMalloc1(PetscMax(nrqr,nrqs)+1,&status2);CHKERRQ(ierr); 279 for (i=0; i<nrqs; ++i) { 280 ierr = MPI_Waitany(nrqs,r_waits2,&idex,status2+i);CHKERRQ(ierr); 281 /* Process the message*/ 282 rbuf2_i = rbuf2[idex]; 283 ct1 = 2*rbuf2_i[0]+1; 284 jmax = rbuf2[idex][0]; 285 for (j=1; j<=jmax; j++) { 286 max = rbuf2_i[2*j]; 287 is_no = rbuf2_i[2*j-1]; 288 isz_i = isz[is_no]; 289 data_i = data[is_no]; 290 table_i = table[is_no]; 291 for (k=0; k<max; k++,ct1++) { 292 row = rbuf2_i[ct1]; 293 if (!PetscBTLookupSet(table_i,row)) data_i[isz_i++] = row; 294 } 295 isz[is_no] = isz_i; 296 } 297 } 298 if (nrqr) {ierr = MPI_Waitall(nrqr,s_waits2,status2);CHKERRQ(ierr);} 299 ierr = PetscFree(status2);CHKERRQ(ierr); 300 } 301 302 for (i=0; i<imax; ++i) { 303 ierr = ISCreateGeneral(PETSC_COMM_SELF,isz[i],data[i],PETSC_COPY_VALUES,is+i);CHKERRQ(ierr); 304 } 305 306 307 ierr = PetscFree(onodes2);CHKERRQ(ierr); 308 ierr = PetscFree(olengths2);CHKERRQ(ierr); 309 310 ierr = PetscFree(pa);CHKERRQ(ierr); 311 ierr = PetscFree(rbuf2[0]);CHKERRQ(ierr); 312 ierr = PetscFree(rbuf2);CHKERRQ(ierr); 313 ierr = PetscFree(s_waits1);CHKERRQ(ierr); 314 ierr = PetscFree(r_waits1);CHKERRQ(ierr); 315 ierr = PetscFree(s_waits2);CHKERRQ(ierr); 316 ierr = PetscFree(r_waits2);CHKERRQ(ierr); 317 ierr = PetscFree5(table,data,isz,d_p,t_p);CHKERRQ(ierr); 318 ierr = PetscFree(s_status);CHKERRQ(ierr); 319 ierr = PetscFree(recv_status);CHKERRQ(ierr); 320 ierr = PetscFree(xdata[0]);CHKERRQ(ierr); 321 ierr = PetscFree(xdata);CHKERRQ(ierr); 322 ierr = PetscFree(isz1);CHKERRQ(ierr); 323 PetscFunctionReturn(0); 324 } 325 326 /* 327 MatIncreaseOverlap_MPIBAIJ_Local - Called by MatincreaseOverlap, to do 328 the work on the local processor. 329 330 Inputs: 331 C - MAT_MPIBAIJ; 332 imax - total no of index sets processed at a time; 333 table - an array of char - size = Mbs bits. 334 335 Output: 336 isz - array containing the count of the solution elements corresponding 337 to each index set; 338 data - pointer to the solutions 339 */ 340 static PetscErrorCode MatIncreaseOverlap_MPIBAIJ_Local(Mat C,PetscInt imax,PetscBT *table,PetscInt *isz,PetscInt **data) 341 { 342 Mat_MPIBAIJ *c = (Mat_MPIBAIJ*)C->data; 343 Mat A = c->A,B = c->B; 344 Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data,*b = (Mat_SeqBAIJ*)B->data; 345 PetscInt start,end,val,max,rstart,cstart,*ai,*aj; 346 PetscInt *bi,*bj,*garray,i,j,k,row,*data_i,isz_i; 347 PetscBT table_i; 348 349 PetscFunctionBegin; 350 rstart = c->rstartbs; 351 cstart = c->cstartbs; 352 ai = a->i; 353 aj = a->j; 354 bi = b->i; 355 bj = b->j; 356 garray = c->garray; 357 358 359 for (i=0; i<imax; i++) { 360 data_i = data[i]; 361 table_i = table[i]; 362 isz_i = isz[i]; 363 for (j=0,max=isz[i]; j<max; j++) { 364 row = data_i[j] - rstart; 365 start = ai[row]; 366 end = ai[row+1]; 367 for (k=start; k<end; k++) { /* Amat */ 368 val = aj[k] + cstart; 369 if (!PetscBTLookupSet(table_i,val)) data_i[isz_i++] = val; 370 } 371 start = bi[row]; 372 end = bi[row+1]; 373 for (k=start; k<end; k++) { /* Bmat */ 374 val = garray[bj[k]]; 375 if (!PetscBTLookupSet(table_i,val)) data_i[isz_i++] = val; 376 } 377 } 378 isz[i] = isz_i; 379 } 380 PetscFunctionReturn(0); 381 } 382 /* 383 MatIncreaseOverlap_MPIBAIJ_Receive - Process the recieved messages, 384 and return the output 385 386 Input: 387 C - the matrix 388 nrqr - no of messages being processed. 389 rbuf - an array of pointers to the recieved requests 390 391 Output: 392 xdata - array of messages to be sent back 393 isz1 - size of each message 394 395 For better efficiency perhaps we should malloc separately each xdata[i], 396 then if a remalloc is required we need only copy the data for that one row 397 rather than all previous rows as it is now where a single large chunck of 398 memory is used. 399 400 */ 401 static PetscErrorCode MatIncreaseOverlap_MPIBAIJ_Receive(Mat C,PetscInt nrqr,PetscInt **rbuf,PetscInt **xdata,PetscInt * isz1) 402 { 403 Mat_MPIBAIJ *c = (Mat_MPIBAIJ*)C->data; 404 Mat A = c->A,B = c->B; 405 Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data,*b = (Mat_SeqBAIJ*)B->data; 406 PetscErrorCode ierr; 407 PetscInt rstart,cstart,*ai,*aj,*bi,*bj,*garray,i,j,k; 408 PetscInt row,total_sz,ct,ct1,ct2,ct3,mem_estimate,oct2,l,start,end; 409 PetscInt val,max1,max2,Mbs,no_malloc =0,*tmp,new_estimate,ctr; 410 PetscInt *rbuf_i,kmax,rbuf_0; 411 PetscBT xtable; 412 413 PetscFunctionBegin; 414 Mbs = c->Mbs; 415 rstart = c->rstartbs; 416 cstart = c->cstartbs; 417 ai = a->i; 418 aj = a->j; 419 bi = b->i; 420 bj = b->j; 421 garray = c->garray; 422 423 424 for (i=0,ct=0,total_sz=0; i<nrqr; ++i) { 425 rbuf_i = rbuf[i]; 426 rbuf_0 = rbuf_i[0]; 427 ct += rbuf_0; 428 for (j=1; j<=rbuf_0; j++) total_sz += rbuf_i[2*j]; 429 } 430 431 if (c->Mbs) max1 = ct*(a->nz +b->nz)/c->Mbs; 432 else max1 = 1; 433 mem_estimate = 3*((total_sz > max1 ? total_sz : max1)+1); 434 ierr = PetscMalloc1(mem_estimate,&xdata[0]);CHKERRQ(ierr); 435 ++no_malloc; 436 ierr = PetscBTCreate(Mbs,&xtable);CHKERRQ(ierr); 437 ierr = PetscMemzero(isz1,nrqr*sizeof(PetscInt));CHKERRQ(ierr); 438 439 ct3 = 0; 440 for (i=0; i<nrqr; i++) { /* for easch mesg from proc i */ 441 rbuf_i = rbuf[i]; 442 rbuf_0 = rbuf_i[0]; 443 ct1 = 2*rbuf_0+1; 444 ct2 = ct1; 445 ct3 += ct1; 446 for (j=1; j<=rbuf_0; j++) { /* for each IS from proc i*/ 447 ierr = PetscBTMemzero(Mbs,xtable);CHKERRQ(ierr); 448 oct2 = ct2; 449 kmax = rbuf_i[2*j]; 450 for (k=0; k<kmax; k++,ct1++) { 451 row = rbuf_i[ct1]; 452 if (!PetscBTLookupSet(xtable,row)) { 453 if (!(ct3 < mem_estimate)) { 454 new_estimate = (PetscInt)(1.5*mem_estimate)+1; 455 ierr = PetscMalloc1(new_estimate,&tmp);CHKERRQ(ierr); 456 ierr = PetscMemcpy(tmp,xdata[0],mem_estimate*sizeof(PetscInt));CHKERRQ(ierr); 457 ierr = PetscFree(xdata[0]);CHKERRQ(ierr); 458 xdata[0] = tmp; 459 mem_estimate = new_estimate; ++no_malloc; 460 for (ctr=1; ctr<=i; ctr++) xdata[ctr] = xdata[ctr-1] + isz1[ctr-1]; 461 } 462 xdata[i][ct2++] = row; 463 ct3++; 464 } 465 } 466 for (k=oct2,max2=ct2; k<max2; k++) { 467 row = xdata[i][k] - rstart; 468 start = ai[row]; 469 end = ai[row+1]; 470 for (l=start; l<end; l++) { 471 val = aj[l] + cstart; 472 if (!PetscBTLookupSet(xtable,val)) { 473 if (!(ct3 < mem_estimate)) { 474 new_estimate = (PetscInt)(1.5*mem_estimate)+1; 475 ierr = PetscMalloc1(new_estimate,&tmp);CHKERRQ(ierr); 476 ierr = PetscMemcpy(tmp,xdata[0],mem_estimate*sizeof(PetscInt));CHKERRQ(ierr); 477 ierr = PetscFree(xdata[0]);CHKERRQ(ierr); 478 xdata[0] = tmp; 479 mem_estimate = new_estimate; ++no_malloc; 480 for (ctr=1; ctr<=i; ctr++) xdata[ctr] = xdata[ctr-1] + isz1[ctr-1]; 481 } 482 xdata[i][ct2++] = val; 483 ct3++; 484 } 485 } 486 start = bi[row]; 487 end = bi[row+1]; 488 for (l=start; l<end; l++) { 489 val = garray[bj[l]]; 490 if (!PetscBTLookupSet(xtable,val)) { 491 if (!(ct3 < mem_estimate)) { 492 new_estimate = (PetscInt)(1.5*mem_estimate)+1; 493 ierr = PetscMalloc1(new_estimate,&tmp);CHKERRQ(ierr); 494 ierr = PetscMemcpy(tmp,xdata[0],mem_estimate*sizeof(PetscInt));CHKERRQ(ierr); 495 ierr = PetscFree(xdata[0]);CHKERRQ(ierr); 496 xdata[0] = tmp; 497 mem_estimate = new_estimate; ++no_malloc; 498 for (ctr =1; ctr <=i; ctr++) xdata[ctr] = xdata[ctr-1] + isz1[ctr-1]; 499 } 500 xdata[i][ct2++] = val; 501 ct3++; 502 } 503 } 504 } 505 /* Update the header*/ 506 xdata[i][2*j] = ct2 - oct2; /* Undo the vector isz1 and use only a var*/ 507 xdata[i][2*j-1] = rbuf_i[2*j-1]; 508 } 509 xdata[i][0] = rbuf_0; 510 xdata[i+1] = xdata[i] + ct2; 511 isz1[i] = ct2; /* size of each message */ 512 } 513 ierr = PetscBTDestroy(&xtable);CHKERRQ(ierr); 514 ierr = PetscInfo3(C,"Allocated %D bytes, required %D, no of mallocs = %D\n",mem_estimate,ct3,no_malloc);CHKERRQ(ierr); 515 PetscFunctionReturn(0); 516 } 517 518 PetscErrorCode MatGetSubMatrices_MPIBAIJ(Mat C,PetscInt ismax,const IS isrow[],const IS iscol[],MatReuse scall,Mat *submat[]) 519 { 520 IS *isrow_block,*iscol_block; 521 Mat_MPIBAIJ *c = (Mat_MPIBAIJ*)C->data; 522 PetscErrorCode ierr; 523 PetscInt nmax,nstages_local,nstages,i,pos,max_no,ncol,nrow,N=C->cmap->N,bs=C->rmap->bs; 524 PetscBool colflag,*allcolumns,*allrows; 525 526 PetscFunctionBegin; 527 //printf("MatGetSubMatrices_MPIBAIJ scall %d, bs %d\n",scall,bs); 528 /* The compression and expansion should be avoided. Doesn't point 529 out errors, might change the indices, hence buggey */ 530 ierr = PetscMalloc2(ismax+1,&isrow_block,ismax+1,&iscol_block);CHKERRQ(ierr); 531 ierr = ISCompressIndicesGeneral(N,C->rmap->n,bs,ismax,isrow,isrow_block);CHKERRQ(ierr); 532 ierr = ISCompressIndicesGeneral(N,C->cmap->n,bs,ismax,iscol,iscol_block);CHKERRQ(ierr); 533 534 /* Check for special case: each processor gets entire matrix columns */ 535 ierr = PetscMalloc2(ismax+1,&allcolumns,ismax+1,&allrows);CHKERRQ(ierr); 536 for (i=0; i<ismax; i++) { 537 ierr = ISIdentity(iscol[i],&colflag);CHKERRQ(ierr); 538 ierr = ISGetLocalSize(iscol[i],&ncol);CHKERRQ(ierr); 539 if (colflag && ncol == C->cmap->N) allcolumns[i] = PETSC_TRUE; 540 else allcolumns[i] = PETSC_FALSE; 541 542 ierr = ISIdentity(isrow[i],&colflag);CHKERRQ(ierr); 543 ierr = ISGetLocalSize(isrow[i],&nrow);CHKERRQ(ierr); 544 if (colflag && nrow == C->rmap->N) allrows[i] = PETSC_TRUE; 545 else allrows[i] = PETSC_FALSE; 546 } 547 548 /* Allocate memory to hold all the submatrices */ 549 if (scall == MAT_INITIAL_MATRIX) { 550 ierr = PetscMalloc1(ismax+1,submat);CHKERRQ(ierr); 551 } 552 /* Determine the number of stages through which submatrices are done */ 553 nmax = 20*1000000 / (c->Nbs * sizeof(PetscInt)); 554 if (!nmax) nmax = 1; 555 nstages_local = ismax/nmax + ((ismax % nmax) ? 1 : 0); 556 557 /* Make sure every processor loops through the nstages */ 558 ierr = MPIU_Allreduce(&nstages_local,&nstages,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)C));CHKERRQ(ierr); 559 for (i=0,pos=0; i<nstages; i++) { 560 if (pos+nmax <= ismax) max_no = nmax; 561 else if (pos == ismax) max_no = 0; 562 else max_no = ismax-pos; 563 ierr = MatGetSubMatrices_MPIBAIJ_local_new(C,max_no,isrow_block+pos,iscol_block+pos,scall,allrows+pos,allcolumns+pos,*submat+pos);CHKERRQ(ierr); 564 pos += max_no; 565 } 566 567 for (i=0; i<ismax; i++) { 568 ierr = ISDestroy(&isrow_block[i]);CHKERRQ(ierr); 569 ierr = ISDestroy(&iscol_block[i]);CHKERRQ(ierr); 570 } 571 ierr = PetscFree2(isrow_block,iscol_block);CHKERRQ(ierr); 572 ierr = PetscFree2(allcolumns,allrows);CHKERRQ(ierr); 573 PetscFunctionReturn(0); 574 } 575 576 #if defined(PETSC_USE_CTABLE) 577 PetscErrorCode PetscGetProc(const PetscInt row, const PetscMPIInt size, const PetscInt proc_gnode[], PetscMPIInt *rank) 578 { 579 PetscInt nGlobalNd = proc_gnode[size]; 580 PetscMPIInt fproc; 581 PetscErrorCode ierr; 582 583 PetscFunctionBegin; 584 ierr = PetscMPIIntCast((PetscInt)(((float)row * (float)size / (float)nGlobalNd + 0.5)),&fproc);CHKERRQ(ierr); 585 if (fproc > size) fproc = size; 586 while (row < proc_gnode[fproc] || row >= proc_gnode[fproc+1]) { 587 if (row < proc_gnode[fproc]) fproc--; 588 else fproc++; 589 } 590 *rank = fproc; 591 PetscFunctionReturn(0); 592 } 593 #endif 594 595 /* -------------------------------------------------------------------------*/ 596 /* This code is used for BAIJ and SBAIJ matrices (unfortunate dependency) */ 597 598 PetscErrorCode MatDestroy_MPIBAIJ_MatGetSubmatrices(Mat C) 599 { 600 PetscErrorCode ierr; 601 Mat_SeqBAIJ *c = (Mat_SeqBAIJ*)C->data; 602 Mat_SubMat *submatj = c->submatis1; 603 PetscInt i; 604 605 PetscFunctionBegin; 606 if (!submatj->id) { /* delete data that are linked only to submats[id=0] */ 607 ierr = PetscFree4(submatj->sbuf1,submatj->ptr,submatj->tmp,submatj->ctr);CHKERRQ(ierr); 608 609 for (i=0; i<submatj->nrqr; ++i) { 610 ierr = PetscFree(submatj->sbuf2[i]);CHKERRQ(ierr); 611 } 612 ierr = PetscFree3(submatj->sbuf2,submatj->req_size,submatj->req_source1);CHKERRQ(ierr); 613 614 if (submatj->rbuf1) { 615 ierr = PetscFree(submatj->rbuf1[0]);CHKERRQ(ierr); 616 ierr = PetscFree(submatj->rbuf1);CHKERRQ(ierr); 617 } 618 619 for (i=0; i<submatj->nrqs; ++i) { 620 ierr = PetscFree(submatj->rbuf3[i]);CHKERRQ(ierr); 621 } 622 ierr = PetscFree3(submatj->req_source2,submatj->rbuf2,submatj->rbuf3);CHKERRQ(ierr); 623 ierr = PetscFree(submatj->pa);CHKERRQ(ierr); 624 } 625 626 #if defined(PETSC_USE_CTABLE) 627 ierr = PetscTableDestroy((PetscTable*)&submatj->rmap);CHKERRQ(ierr); 628 if (submatj->cmap_loc) {ierr = PetscFree(submatj->cmap_loc);CHKERRQ(ierr);} 629 ierr = PetscFree(submatj->rmap_loc);CHKERRQ(ierr); 630 #else 631 ierr = PetscFree(submatj->rmap);CHKERRQ(ierr); 632 #endif 633 634 if (!submatj->allcolumns) { 635 #if defined(PETSC_USE_CTABLE) 636 ierr = PetscTableDestroy((PetscTable*)&submatj->cmap);CHKERRQ(ierr); 637 #else 638 ierr = PetscFree(submatj->cmap);CHKERRQ(ierr); 639 #endif 640 } 641 ierr = submatj->destroy(C);CHKERRQ(ierr); 642 ierr = PetscFree(submatj->row2proc);CHKERRQ(ierr); 643 644 ierr = PetscFree(submatj);CHKERRQ(ierr); 645 PetscFunctionReturn(0); 646 } 647 648 #if 1 649 PetscErrorCode MatGetSubMatrices_MPIBAIJ_local_new(Mat C,PetscInt ismax,const IS isrow[],const IS iscol[],MatReuse scall,PetscBool *allrows,PetscBool *allcolumns,Mat *submats) 650 { 651 Mat_MPIBAIJ *c = (Mat_MPIBAIJ*)C->data; 652 Mat A = c->A; 653 Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data,*b = (Mat_SeqBAIJ*)c->B->data,*subc; 654 const PetscInt **icol,**irow; 655 PetscInt *nrow,*ncol,start; 656 PetscErrorCode ierr; 657 PetscMPIInt rank,size,tag0,tag2,tag3,tag4,*w1,*w2,*w3,*w4,nrqr; 658 PetscInt **sbuf1,**sbuf2,i,j,k,l,ct1,ct2,**rbuf1,row,proc=-1; 659 PetscInt nrqs=0,msz,**ptr=NULL,*req_size=NULL,*ctr=NULL,*pa,*tmp=NULL,tcol; 660 PetscInt **rbuf3=NULL,*req_source1=NULL,*req_source2,**sbuf_aj,**rbuf2=NULL,max1,max2; 661 PetscInt **lens,is_no,ncols,*cols,mat_i,*mat_j,tmp2,jmax; 662 #if defined(PETSC_USE_CTABLE) 663 PetscTable *cmap,cmap_i=NULL,*rmap,rmap_i; 664 #else 665 PetscInt **cmap,*cmap_i=NULL,**rmap,*rmap_i; 666 #endif 667 const PetscInt *irow_i; 668 PetscInt ctr_j,*sbuf1_j,*sbuf_aj_i,*rbuf1_i,kmax,*lens_i; 669 MPI_Request *s_waits1,*r_waits1,*s_waits2,*r_waits2,*r_waits3; 670 MPI_Request *r_waits4,*s_waits3,*s_waits4; 671 MPI_Status *r_status1,*r_status2,*s_status1,*s_status3,*s_status2; 672 MPI_Status *r_status3,*r_status4,*s_status4; 673 MPI_Comm comm; 674 PetscScalar **rbuf4,*rbuf4_i,**sbuf_aa,*vals,*mat_a,*imat_a,*sbuf_aa_i; 675 PetscMPIInt *onodes1,*olengths1,end; 676 PetscInt **row2proc,*row2proc_i,ilen_row,*imat_ilen,*imat_j,*imat_i,old_row; 677 Mat_SubMat **smats,*smat_i; 678 PetscBool *issorted,colflag,iscsorted=PETSC_TRUE; 679 PetscInt *sbuf1_i,*rbuf2_i,*rbuf3_i,ilen; 680 681 PetscFunctionBegin; 682 ierr = PetscObjectGetComm((PetscObject)C,&comm);CHKERRQ(ierr); 683 size = c->size; 684 rank = c->rank; 685 if (!rank) printf("MatGetSubMatrices_MPIBAIJ_new scall %d\n",scall); 686 687 ierr = PetscMalloc5(ismax,&smats,ismax,&row2proc,ismax,&cmap,ismax,&rmap,ismax,&allcolumns);CHKERRQ(ierr); 688 ierr = PetscMalloc5(ismax,&irow,ismax,&icol,ismax,&nrow,ismax,&ncol,ismax,&issorted);CHKERRQ(ierr); 689 690 for (i=0; i<ismax; i++) { 691 ierr = ISSorted(iscol[i],&issorted[i]);CHKERRQ(ierr); 692 if (!issorted[i]) iscsorted = issorted[i]; 693 694 ierr = ISSorted(isrow[i],&issorted[i]);CHKERRQ(ierr); 695 696 ierr = ISGetIndices(isrow[i],&irow[i]);CHKERRQ(ierr); 697 ierr = ISGetLocalSize(isrow[i],&nrow[i]);CHKERRQ(ierr); 698 699 /* Check for special case: allcolumn */ 700 ierr = ISIdentity(iscol[i],&colflag);CHKERRQ(ierr); 701 ierr = ISGetLocalSize(iscol[i],&ncol[i]);CHKERRQ(ierr); 702 if (colflag && ncol[i] == C->cmap->N) { 703 allcolumns[i] = PETSC_TRUE; 704 icol[i] = NULL; 705 } else { 706 allcolumns[i] = PETSC_FALSE; 707 ierr = ISGetIndices(iscol[i],&icol[i]);CHKERRQ(ierr); 708 } 709 } 710 711 if (scall == MAT_REUSE_MATRIX) { 712 /* Assumes new rows are same length as the old rows */ 713 for (i=0; i<ismax; i++) { 714 subc = (Mat_SeqBAIJ*)(submats[i]->data); 715 if ((submats[i]->rmap->n != nrow[i]) || (submats[i]->cmap->n != ncol[i])) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size"); 716 717 /* Initial matrix as if empty */ 718 ierr = PetscMemzero(subc->ilen,submats[i]->rmap->n*sizeof(PetscInt));CHKERRQ(ierr); 719 720 /* Initial matrix as if empty */ 721 submats[i]->factortype = C->factortype; 722 723 smat_i = subc->submatis1; 724 smats[i] = smat_i; 725 726 nrqs = smat_i->nrqs; 727 nrqr = smat_i->nrqr; 728 rbuf1 = smat_i->rbuf1; 729 rbuf2 = smat_i->rbuf2; 730 rbuf3 = smat_i->rbuf3; 731 req_source2 = smat_i->req_source2; 732 733 sbuf1 = smat_i->sbuf1; 734 sbuf2 = smat_i->sbuf2; 735 ptr = smat_i->ptr; 736 tmp = smat_i->tmp; 737 ctr = smat_i->ctr; 738 739 pa = smat_i->pa; 740 req_size = smat_i->req_size; 741 req_source1 = smat_i->req_source1; 742 743 allcolumns[i] = smat_i->allcolumns; 744 row2proc[i] = smat_i->row2proc; 745 rmap[i] = smat_i->rmap; 746 cmap[i] = smat_i->cmap; 747 } 748 } else { /* scall == MAT_INITIAL_MATRIX */ 749 /* Get some new tags to keep the communication clean */ 750 ierr = PetscObjectGetNewTag((PetscObject)C,&tag2);CHKERRQ(ierr); 751 ierr = PetscObjectGetNewTag((PetscObject)C,&tag3);CHKERRQ(ierr); 752 753 /* evaluate communication - mesg to who, length of mesg, and buffer space 754 required. Based on this, buffers are allocated, and data copied into them*/ 755 ierr = PetscCalloc4(size,&w1,size,&w2,size,&w3,size,&w4);CHKERRQ(ierr); /* mesg size, initialize work vectors */ 756 757 for (i=0; i<ismax; i++) { 758 jmax = nrow[i]; 759 irow_i = irow[i]; 760 761 ierr = PetscMalloc1(jmax,&row2proc_i);CHKERRQ(ierr); 762 row2proc[i] = row2proc_i; 763 764 if (issorted[i]) proc = 0; 765 for (j=0; j<jmax; j++) { 766 if (!issorted[i]) proc = 0; 767 row = irow_i[j]; 768 while (row >= C->rmap->range[proc+1]) proc++; 769 w4[proc]++; 770 row2proc_i[j] = proc; /* map row index to proc */ 771 } 772 for (j=0; j<size; j++) { 773 if (w4[j]) { w1[j] += w4[j]; w3[j]++; w4[j] = 0;} 774 } 775 } 776 777 nrqs = 0; /* no of outgoing messages */ 778 msz = 0; /* total mesg length (for all procs) */ 779 w1[rank] = 0; /* no mesg sent to self */ 780 w3[rank] = 0; 781 for (i=0; i<size; i++) { 782 if (w1[i]) { w2[i] = 1; nrqs++;} /* there exists a message to proc i */ 783 } 784 ierr = PetscMalloc1(nrqs+1,&pa);CHKERRQ(ierr); /*(proc -array)*/ 785 for (i=0,j=0; i<size; i++) { 786 if (w1[i]) { pa[j] = i; j++; } 787 } 788 789 /* Each message would have a header = 1 + 2*(no of IS) + data */ 790 for (i=0; i<nrqs; i++) { 791 j = pa[i]; 792 w1[j] += w2[j] + 2* w3[j]; 793 msz += w1[j]; 794 } 795 ierr = PetscInfo2(0,"Number of outgoing messages %D Total message length %D\n",nrqs,msz);CHKERRQ(ierr); 796 797 /* Determine the number of messages to expect, their lengths, from from-ids */ 798 ierr = PetscGatherNumberOfMessages(comm,w2,w1,&nrqr);CHKERRQ(ierr); 799 ierr = PetscGatherMessageLengths(comm,nrqs,nrqr,w1,&onodes1,&olengths1);CHKERRQ(ierr); 800 801 /* Now post the Irecvs corresponding to these messages */ 802 tag0 = ((PetscObject)C)->tag; 803 ierr = PetscPostIrecvInt(comm,tag0,nrqr,onodes1,olengths1,&rbuf1,&r_waits1);CHKERRQ(ierr); 804 805 ierr = PetscFree(onodes1);CHKERRQ(ierr); 806 ierr = PetscFree(olengths1);CHKERRQ(ierr); 807 808 /* Allocate Memory for outgoing messages */ 809 ierr = PetscMalloc4(size,&sbuf1,size,&ptr,2*msz,&tmp,size,&ctr);CHKERRQ(ierr); 810 ierr = PetscMemzero(sbuf1,size*sizeof(PetscInt*));CHKERRQ(ierr); 811 ierr = PetscMemzero(ptr,size*sizeof(PetscInt*));CHKERRQ(ierr); 812 813 { 814 PetscInt *iptr = tmp; 815 k = 0; 816 for (i=0; i<nrqs; i++) { 817 j = pa[i]; 818 iptr += k; 819 sbuf1[j] = iptr; 820 k = w1[j]; 821 } 822 } 823 824 /* Form the outgoing messages. Initialize the header space */ 825 for (i=0; i<nrqs; i++) { 826 j = pa[i]; 827 sbuf1[j][0] = 0; 828 ierr = PetscMemzero(sbuf1[j]+1,2*w3[j]*sizeof(PetscInt));CHKERRQ(ierr); 829 ptr[j] = sbuf1[j] + 2*w3[j] + 1; 830 } 831 832 /* Parse the isrow and copy data into outbuf */ 833 for (i=0; i<ismax; i++) { 834 row2proc_i = row2proc[i]; 835 ierr = PetscMemzero(ctr,size*sizeof(PetscInt));CHKERRQ(ierr); 836 irow_i = irow[i]; 837 jmax = nrow[i]; 838 for (j=0; j<jmax; j++) { /* parse the indices of each IS */ 839 proc = row2proc_i[j]; 840 if (proc != rank) { /* copy to the outgoing buf*/ 841 ctr[proc]++; 842 *ptr[proc] = irow_i[j]; 843 ptr[proc]++; 844 } 845 } 846 /* Update the headers for the current IS */ 847 for (j=0; j<size; j++) { /* Can Optimise this loop too */ 848 if ((ctr_j = ctr[j])) { 849 sbuf1_j = sbuf1[j]; 850 k = ++sbuf1_j[0]; 851 sbuf1_j[2*k] = ctr_j; 852 sbuf1_j[2*k-1] = i; 853 } 854 } 855 } 856 857 /* Now post the sends */ 858 ierr = PetscMalloc1(nrqs+1,&s_waits1);CHKERRQ(ierr); 859 for (i=0; i<nrqs; ++i) { 860 j = pa[i]; 861 ierr = MPI_Isend(sbuf1[j],w1[j],MPIU_INT,j,tag0,comm,s_waits1+i);CHKERRQ(ierr); 862 } 863 864 /* Post Receives to capture the buffer size */ 865 ierr = PetscMalloc1(nrqs+1,&r_waits2);CHKERRQ(ierr); 866 ierr = PetscMalloc3(nrqs+1,&req_source2,nrqs+1,&rbuf2,nrqs+1,&rbuf3);CHKERRQ(ierr); 867 rbuf2[0] = tmp + msz; 868 for (i=1; i<nrqs; ++i) { 869 rbuf2[i] = rbuf2[i-1]+w1[pa[i-1]]; 870 } 871 for (i=0; i<nrqs; ++i) { 872 j = pa[i]; 873 ierr = MPI_Irecv(rbuf2[i],w1[j],MPIU_INT,j,tag2,comm,r_waits2+i);CHKERRQ(ierr); 874 } 875 876 /* Send to other procs the buf size they should allocate */ 877 /* Receive messages*/ 878 ierr = PetscMalloc1(nrqr+1,&s_waits2);CHKERRQ(ierr); 879 ierr = PetscMalloc1(nrqr+1,&r_status1);CHKERRQ(ierr); 880 ierr = PetscMalloc3(nrqr,&sbuf2,nrqr,&req_size,nrqr,&req_source1);CHKERRQ(ierr); 881 { 882 PetscInt *sAi = a->i,*sBi = b->i,id,rstart = C->rmap->rstart; 883 PetscInt *sbuf2_i; 884 885 ierr = MPI_Waitall(nrqr,r_waits1,r_status1);CHKERRQ(ierr); 886 for (i=0; i<nrqr; ++i) { 887 req_size[i] = 0; 888 rbuf1_i = rbuf1[i]; 889 start = 2*rbuf1_i[0] + 1; 890 ierr = MPI_Get_count(r_status1+i,MPIU_INT,&end);CHKERRQ(ierr); 891 ierr = PetscMalloc1(end+1,&sbuf2[i]);CHKERRQ(ierr); 892 sbuf2_i = sbuf2[i]; 893 for (j=start; j<end; j++) { 894 id = rbuf1_i[j] - rstart; 895 ncols = sAi[id+1] - sAi[id] + sBi[id+1] - sBi[id]; 896 sbuf2_i[j] = ncols; 897 req_size[i] += ncols; 898 } 899 req_source1[i] = r_status1[i].MPI_SOURCE; 900 /* form the header */ 901 sbuf2_i[0] = req_size[i]; 902 for (j=1; j<start; j++) sbuf2_i[j] = rbuf1_i[j]; 903 904 ierr = MPI_Isend(sbuf2_i,end,MPIU_INT,req_source1[i],tag2,comm,s_waits2+i);CHKERRQ(ierr); 905 } 906 } 907 ierr = PetscFree(r_status1);CHKERRQ(ierr); 908 ierr = PetscFree(r_waits1);CHKERRQ(ierr); 909 ierr = PetscFree4(w1,w2,w3,w4);CHKERRQ(ierr); 910 911 /* Receive messages*/ 912 ierr = PetscMalloc1(nrqs+1,&r_waits3);CHKERRQ(ierr); 913 ierr = PetscMalloc1(nrqs+1,&r_status2);CHKERRQ(ierr); 914 915 ierr = MPI_Waitall(nrqs,r_waits2,r_status2);CHKERRQ(ierr); 916 for (i=0; i<nrqs; ++i) { 917 ierr = PetscMalloc1(rbuf2[i][0]+1,&rbuf3[i]);CHKERRQ(ierr); 918 req_source2[i] = r_status2[i].MPI_SOURCE; 919 ierr = MPI_Irecv(rbuf3[i],rbuf2[i][0],MPIU_INT,req_source2[i],tag3,comm,r_waits3+i);CHKERRQ(ierr); 920 } 921 ierr = PetscFree(r_status2);CHKERRQ(ierr); 922 ierr = PetscFree(r_waits2);CHKERRQ(ierr); 923 924 /* Wait on sends1 and sends2 */ 925 ierr = PetscMalloc1(nrqs+1,&s_status1);CHKERRQ(ierr); 926 ierr = PetscMalloc1(nrqr+1,&s_status2);CHKERRQ(ierr); 927 928 if (nrqs) {ierr = MPI_Waitall(nrqs,s_waits1,s_status1);CHKERRQ(ierr);} 929 if (nrqr) {ierr = MPI_Waitall(nrqr,s_waits2,s_status2);CHKERRQ(ierr);} 930 ierr = PetscFree(s_status1);CHKERRQ(ierr); 931 ierr = PetscFree(s_status2);CHKERRQ(ierr); 932 ierr = PetscFree(s_waits1);CHKERRQ(ierr); 933 ierr = PetscFree(s_waits2);CHKERRQ(ierr); 934 935 /* Now allocate sending buffers for a->j, and send them off */ 936 ierr = PetscMalloc1(nrqr+1,&sbuf_aj);CHKERRQ(ierr); 937 for (i=0,j=0; i<nrqr; i++) j += req_size[i]; 938 ierr = PetscMalloc1(j+1,&sbuf_aj[0]);CHKERRQ(ierr); 939 for (i=1; i<nrqr; i++) sbuf_aj[i] = sbuf_aj[i-1] + req_size[i-1]; 940 941 ierr = PetscMalloc1(nrqr+1,&s_waits3);CHKERRQ(ierr); 942 { 943 PetscInt nzA,nzB,*a_i = a->i,*b_i = b->i,lwrite; 944 PetscInt *cworkA,*cworkB,cstart = C->cmap->rstart,rstart = C->rmap->rstart,*bmap = c->garray; 945 PetscInt cend = C->cmap->rend; 946 PetscInt *a_j = a->j,*b_j = b->j,ctmp; 947 948 for (i=0; i<nrqr; i++) { 949 rbuf1_i = rbuf1[i]; 950 sbuf_aj_i = sbuf_aj[i]; 951 ct1 = 2*rbuf1_i[0] + 1; 952 ct2 = 0; 953 for (j=1,max1=rbuf1_i[0]; j<=max1; j++) { 954 kmax = rbuf1[i][2*j]; 955 for (k=0; k<kmax; k++,ct1++) { 956 row = rbuf1_i[ct1] - rstart; 957 nzA = a_i[row+1] - a_i[row]; nzB = b_i[row+1] - b_i[row]; 958 ncols = nzA + nzB; 959 cworkA = a_j + a_i[row]; cworkB = b_j + b_i[row]; 960 961 /* load the column indices for this row into cols */ 962 cols = sbuf_aj_i + ct2; 963 964 lwrite = 0; 965 for (l=0; l<nzB; l++) { 966 if ((ctmp = bmap[cworkB[l]]) < cstart) cols[lwrite++] = ctmp; 967 } 968 for (l=0; l<nzA; l++) cols[lwrite++] = cstart + cworkA[l]; 969 for (l=0; l<nzB; l++) { 970 if ((ctmp = bmap[cworkB[l]]) >= cend) cols[lwrite++] = ctmp; 971 } 972 973 ct2 += ncols; 974 } 975 } 976 ierr = MPI_Isend(sbuf_aj_i,req_size[i],MPIU_INT,req_source1[i],tag3,comm,s_waits3+i);CHKERRQ(ierr); 977 } 978 } 979 ierr = PetscMalloc2(nrqs+1,&r_status3,nrqr+1,&s_status3);CHKERRQ(ierr); 980 981 /* create col map: global col of C -> local col of submatrices */ 982 { 983 const PetscInt *icol_i; 984 #if defined(PETSC_USE_CTABLE) 985 for (i=0; i<ismax; i++) { 986 if (!allcolumns[i]) { 987 ierr = PetscTableCreate(ncol[i]+1,C->cmap->N+1,&cmap[i]);CHKERRQ(ierr); 988 989 jmax = ncol[i]; 990 icol_i = icol[i]; 991 cmap_i = cmap[i]; 992 for (j=0; j<jmax; j++) { 993 ierr = PetscTableAdd(cmap[i],icol_i[j]+1,j+1,INSERT_VALUES);CHKERRQ(ierr); 994 } 995 } else cmap[i] = NULL; 996 } 997 #else 998 for (i=0; i<ismax; i++) { 999 if (!allcolumns[i]) { 1000 ierr = PetscCalloc1(C->cmap->N,&cmap[i]);CHKERRQ(ierr); 1001 jmax = ncol[i]; 1002 icol_i = icol[i]; 1003 cmap_i = cmap[i]; 1004 for (j=0; j<jmax; j++) { 1005 cmap_i[icol_i[j]] = j+1; 1006 } 1007 } else cmap[i] = NULL; 1008 } 1009 #endif 1010 } 1011 1012 /* Create lens which is required for MatCreate... */ 1013 for (i=0,j=0; i<ismax; i++) j += nrow[i]; 1014 ierr = PetscMalloc1(ismax,&lens);CHKERRQ(ierr); 1015 1016 if (ismax) { 1017 ierr = PetscCalloc1(j,&lens[0]);CHKERRQ(ierr); 1018 } 1019 for (i=1; i<ismax; i++) lens[i] = lens[i-1] + nrow[i-1]; 1020 1021 /* Update lens from local data */ 1022 for (i=0; i<ismax; i++) { 1023 row2proc_i = row2proc[i]; 1024 jmax = nrow[i]; 1025 if (!allcolumns[i]) cmap_i = cmap[i]; 1026 irow_i = irow[i]; 1027 lens_i = lens[i]; 1028 for (j=0; j<jmax; j++) { 1029 row = irow_i[j]; 1030 proc = row2proc_i[j]; 1031 if (proc == rank) { 1032 ierr = MatGetRow_MPIBAIJ(C,row,&ncols,&cols,0);CHKERRQ(ierr); 1033 if (!allcolumns[i]) { 1034 for (k=0; k<ncols; k++) { 1035 #if defined(PETSC_USE_CTABLE) 1036 ierr = PetscTableFind(cmap_i,cols[k]+1,&tcol);CHKERRQ(ierr); 1037 #else 1038 tcol = cmap_i[cols[k]]; 1039 #endif 1040 if (tcol) lens_i[j]++; 1041 } 1042 } else { /* allcolumns */ 1043 lens_i[j] = ncols; 1044 } 1045 ierr = MatRestoreRow_MPIBAIJ(C,row,&ncols,&cols,0);CHKERRQ(ierr); 1046 } 1047 } 1048 } 1049 1050 /* Create row map: global row of C -> local row of submatrices */ 1051 #if defined(PETSC_USE_CTABLE) 1052 for (i=0; i<ismax; i++) { 1053 ierr = PetscTableCreate(nrow[i]+1,C->rmap->N+1,&rmap[i]);CHKERRQ(ierr); 1054 irow_i = irow[i]; 1055 jmax = nrow[i]; 1056 for (j=0; j<jmax; j++) { 1057 ierr = PetscTableAdd(rmap[i],irow_i[j]+1,j+1,INSERT_VALUES);CHKERRQ(ierr); 1058 } 1059 } 1060 #else 1061 for (i=0; i<ismax; i++) { 1062 ierr = PetscCalloc1(C->rmap->N,&rmap[i]);CHKERRQ(ierr); 1063 rmap_i = rmap[i]; 1064 irow_i = irow[i]; 1065 jmax = nrow[i]; 1066 for (j=0; j<jmax; j++) { 1067 rmap_i[irow_i[j]] = j; 1068 } 1069 } 1070 #endif 1071 1072 /* Update lens from offproc data */ 1073 { 1074 PetscInt *rbuf2_i,*rbuf3_i,*sbuf1_i; 1075 1076 ierr = MPI_Waitall(nrqs,r_waits3,r_status3);CHKERRQ(ierr); 1077 for (tmp2=0; tmp2<nrqs; tmp2++) { 1078 sbuf1_i = sbuf1[pa[tmp2]]; 1079 jmax = sbuf1_i[0]; 1080 ct1 = 2*jmax+1; 1081 ct2 = 0; 1082 rbuf2_i = rbuf2[tmp2]; 1083 rbuf3_i = rbuf3[tmp2]; 1084 for (j=1; j<=jmax; j++) { 1085 is_no = sbuf1_i[2*j-1]; 1086 max1 = sbuf1_i[2*j]; 1087 lens_i = lens[is_no]; 1088 if (!allcolumns[is_no]) cmap_i = cmap[is_no]; 1089 rmap_i = rmap[is_no]; 1090 for (k=0; k<max1; k++,ct1++) { 1091 #if defined(PETSC_USE_CTABLE) 1092 ierr = PetscTableFind(rmap_i,sbuf1_i[ct1]+1,&row);CHKERRQ(ierr); 1093 row--; 1094 if (row < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"row not found in table"); 1095 #else 1096 row = rmap_i[sbuf1_i[ct1]]; /* the val in the new matrix to be */ 1097 #endif 1098 max2 = rbuf2_i[ct1]; 1099 for (l=0; l<max2; l++,ct2++) { 1100 if (!allcolumns[is_no]) { 1101 #if defined(PETSC_USE_CTABLE) 1102 ierr = PetscTableFind(cmap_i,rbuf3_i[ct2]+1,&tcol);CHKERRQ(ierr); 1103 #else 1104 tcol = cmap_i[rbuf3_i[ct2]]; 1105 #endif 1106 if (tcol) lens_i[row]++; 1107 } else { /* allcolumns */ 1108 lens_i[row]++; /* lens_i[row] += max2 ? */ 1109 } 1110 } 1111 } 1112 } 1113 } 1114 } 1115 ierr = PetscFree(r_waits3);CHKERRQ(ierr); 1116 if (nrqr) {ierr = MPI_Waitall(nrqr,s_waits3,s_status3);CHKERRQ(ierr);} 1117 ierr = PetscFree2(r_status3,s_status3);CHKERRQ(ierr); 1118 ierr = PetscFree(s_waits3);CHKERRQ(ierr); 1119 1120 /* Create the submatrices */ 1121 for (i=0; i<ismax; i++) { 1122 PetscInt rbs,cbs; 1123 1124 ierr = ISGetBlockSize(isrow[i],&rbs);CHKERRQ(ierr); 1125 ierr = ISGetBlockSize(iscol[i],&cbs);CHKERRQ(ierr); 1126 1127 ierr = MatCreate(PETSC_COMM_SELF,submats+i);CHKERRQ(ierr); 1128 ierr = MatSetSizes(submats[i],nrow[i],ncol[i],PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 1129 1130 ierr = MatSetBlockSizes(submats[i],rbs,cbs);CHKERRQ(ierr); 1131 ierr = MatSetType(submats[i],((PetscObject)A)->type_name);CHKERRQ(ierr); 1132 ierr = MatSeqBAIJSetPreallocation(submats[i],1,0,lens[i]);CHKERRQ(ierr); //bs=1! 1133 ierr = MatSeqSBAIJSetPreallocation(submats[i],1,0,lens[i]);CHKERRQ(ierr); /* this subroutine is used by SBAIJ routines */ 1134 1135 /* create struct Mat_SubMat and attached it to submat */ 1136 ierr = PetscNew(&smat_i);CHKERRQ(ierr); 1137 subc = (Mat_SeqBAIJ*)submats[i]->data; 1138 subc->submatis1 = smat_i; 1139 smats[i] = smat_i; 1140 1141 smat_i->destroy = submats[i]->ops->destroy; 1142 submats[i]->ops->destroy = MatDestroy_MPIBAIJ_MatGetSubmatrices; 1143 submats[i]->factortype = C->factortype; 1144 1145 smat_i->id = i; 1146 smat_i->nrqs = nrqs; 1147 smat_i->nrqr = nrqr; 1148 smat_i->rbuf1 = rbuf1; 1149 smat_i->rbuf2 = rbuf2; 1150 smat_i->rbuf3 = rbuf3; 1151 smat_i->sbuf2 = sbuf2; 1152 smat_i->req_source2 = req_source2; 1153 1154 smat_i->sbuf1 = sbuf1; 1155 smat_i->ptr = ptr; 1156 smat_i->tmp = tmp; 1157 smat_i->ctr = ctr; 1158 1159 smat_i->pa = pa; 1160 smat_i->req_size = req_size; 1161 smat_i->req_source1 = req_source1; 1162 1163 smat_i->allcolumns = allcolumns[i]; 1164 smat_i->singleis = PETSC_FALSE; 1165 smat_i->row2proc = row2proc[i]; 1166 smat_i->rmap = rmap[i]; 1167 smat_i->cmap = cmap[i]; 1168 } 1169 1170 if (ismax) {ierr = PetscFree(lens[0]);CHKERRQ(ierr);} 1171 ierr = PetscFree(lens);CHKERRQ(ierr); 1172 ierr = PetscFree(sbuf_aj[0]);CHKERRQ(ierr); 1173 ierr = PetscFree(sbuf_aj);CHKERRQ(ierr); 1174 1175 } /* endof scall == MAT_INITIAL_MATRIX */ 1176 1177 /* Post recv matrix values */ 1178 ierr = PetscObjectGetNewTag((PetscObject)C,&tag4);CHKERRQ(ierr); 1179 ierr = PetscMalloc1(nrqs+1,&rbuf4);CHKERRQ(ierr); 1180 ierr = PetscMalloc1(nrqs+1,&r_waits4);CHKERRQ(ierr); 1181 ierr = PetscMalloc1(nrqs+1,&r_status4);CHKERRQ(ierr); 1182 ierr = PetscMalloc1(nrqr+1,&s_status4);CHKERRQ(ierr); 1183 for (i=0; i<nrqs; ++i) { 1184 ierr = PetscMalloc1(rbuf2[i][0]+1,&rbuf4[i]);CHKERRQ(ierr); 1185 ierr = MPI_Irecv(rbuf4[i],rbuf2[i][0],MPIU_SCALAR,req_source2[i],tag4,comm,r_waits4+i);CHKERRQ(ierr); 1186 } 1187 1188 /* Allocate sending buffers for a->a, and send them off */ 1189 ierr = PetscMalloc1(nrqr+1,&sbuf_aa);CHKERRQ(ierr); 1190 for (i=0,j=0; i<nrqr; i++) j += req_size[i]; 1191 ierr = PetscMalloc1(j+1,&sbuf_aa[0]);CHKERRQ(ierr); 1192 for (i=1; i<nrqr; i++) sbuf_aa[i] = sbuf_aa[i-1] + req_size[i-1]; 1193 1194 ierr = PetscMalloc1(nrqr+1,&s_waits4);CHKERRQ(ierr); 1195 { 1196 PetscInt nzA,nzB,*a_i = a->i,*b_i = b->i, *cworkB,lwrite; 1197 PetscInt cstart = C->cmap->rstart,rstart = C->rmap->rstart,*bmap = c->garray; 1198 PetscInt cend = C->cmap->rend; 1199 PetscInt *b_j = b->j; 1200 PetscScalar *vworkA,*vworkB,*a_a = a->a,*b_a = b->a; 1201 1202 for (i=0; i<nrqr; i++) { 1203 rbuf1_i = rbuf1[i]; 1204 sbuf_aa_i = sbuf_aa[i]; 1205 ct1 = 2*rbuf1_i[0]+1; 1206 ct2 = 0; 1207 for (j=1,max1=rbuf1_i[0]; j<=max1; j++) { 1208 kmax = rbuf1_i[2*j]; 1209 for (k=0; k<kmax; k++,ct1++) { 1210 row = rbuf1_i[ct1] - rstart; 1211 nzA = a_i[row+1] - a_i[row]; nzB = b_i[row+1] - b_i[row]; 1212 ncols = nzA + nzB; 1213 cworkB = b_j + b_i[row]; 1214 vworkA = a_a + a_i[row]; 1215 vworkB = b_a + b_i[row]; 1216 1217 /* load the column values for this row into vals*/ 1218 vals = sbuf_aa_i+ct2; 1219 1220 lwrite = 0; 1221 for (l=0; l<nzB; l++) { 1222 if ((bmap[cworkB[l]]) < cstart) vals[lwrite++] = vworkB[l]; 1223 } 1224 for (l=0; l<nzA; l++) vals[lwrite++] = vworkA[l]; 1225 for (l=0; l<nzB; l++) { 1226 if ((bmap[cworkB[l]]) >= cend) vals[lwrite++] = vworkB[l]; 1227 } 1228 1229 ct2 += ncols; 1230 } 1231 } 1232 ierr = MPI_Isend(sbuf_aa_i,req_size[i],MPIU_SCALAR,req_source1[i],tag4,comm,s_waits4+i);CHKERRQ(ierr); 1233 } 1234 } 1235 1236 if (!ismax) { 1237 ierr = PetscFree(rbuf1[0]);CHKERRQ(ierr); 1238 ierr = PetscFree(rbuf1);CHKERRQ(ierr); 1239 } 1240 1241 /* Assemble the matrices */ 1242 /* First assemble the local rows */ 1243 for (i=0; i<ismax; i++) { 1244 row2proc_i = row2proc[i]; 1245 subc = (Mat_SeqBAIJ*)submats[i]->data; 1246 imat_ilen = subc->ilen; 1247 imat_j = subc->j; 1248 imat_i = subc->i; 1249 imat_a = subc->a; 1250 1251 if (!allcolumns[i]) cmap_i = cmap[i]; 1252 rmap_i = rmap[i]; 1253 irow_i = irow[i]; 1254 jmax = nrow[i]; 1255 for (j=0; j<jmax; j++) { 1256 row = irow_i[j]; 1257 proc = row2proc_i[j]; 1258 if (proc == rank) { 1259 old_row = row; 1260 #if defined(PETSC_USE_CTABLE) 1261 ierr = PetscTableFind(rmap_i,row+1,&row);CHKERRQ(ierr); 1262 row--; 1263 #else 1264 row = rmap_i[row]; 1265 #endif 1266 ilen_row = imat_ilen[row]; 1267 ierr = MatGetRow_MPIBAIJ(C,old_row,&ncols,&cols,&vals);CHKERRQ(ierr); 1268 mat_i = imat_i[row]; 1269 mat_a = imat_a + mat_i; 1270 mat_j = imat_j + mat_i; 1271 if (!allcolumns[i]) { 1272 for (k=0; k<ncols; k++) { 1273 #if defined(PETSC_USE_CTABLE) 1274 ierr = PetscTableFind(cmap_i,cols[k]+1,&tcol);CHKERRQ(ierr); 1275 #else 1276 tcol = cmap_i[cols[k]]; 1277 #endif 1278 if (tcol) { 1279 *mat_j++ = tcol - 1; 1280 *mat_a++ = vals[k]; 1281 ilen_row++; 1282 } 1283 } 1284 } else { /* allcolumns */ 1285 for (k=0; k<ncols; k++) { 1286 *mat_j++ = cols[k]; /* global col index! */ 1287 *mat_a++ = vals[k]; 1288 ilen_row++; 1289 } 1290 } 1291 ierr = MatRestoreRow_MPIBAIJ(C,old_row,&ncols,&cols,&vals);CHKERRQ(ierr); 1292 1293 imat_ilen[row] = ilen_row; 1294 } 1295 } 1296 } 1297 1298 /* Now assemble the off proc rows */ 1299 ierr = MPI_Waitall(nrqs,r_waits4,r_status4);CHKERRQ(ierr); 1300 for (tmp2=0; tmp2<nrqs; tmp2++) { 1301 sbuf1_i = sbuf1[pa[tmp2]]; 1302 jmax = sbuf1_i[0]; 1303 ct1 = 2*jmax + 1; 1304 ct2 = 0; 1305 rbuf2_i = rbuf2[tmp2]; 1306 rbuf3_i = rbuf3[tmp2]; 1307 rbuf4_i = rbuf4[tmp2]; 1308 for (j=1; j<=jmax; j++) { 1309 is_no = sbuf1_i[2*j-1]; 1310 rmap_i = rmap[is_no]; 1311 if (!allcolumns[is_no]) cmap_i = cmap[is_no]; 1312 subc = (Mat_SeqBAIJ*)submats[is_no]->data; 1313 imat_ilen = subc->ilen; 1314 imat_j = subc->j; 1315 imat_i = subc->i; 1316 imat_a = subc->a; 1317 max1 = sbuf1_i[2*j]; 1318 for (k=0; k<max1; k++,ct1++) { 1319 row = sbuf1_i[ct1]; 1320 #if defined(PETSC_USE_CTABLE) 1321 ierr = PetscTableFind(rmap_i,row+1,&row);CHKERRQ(ierr); 1322 row--; 1323 #else 1324 row = rmap_i[row]; 1325 #endif 1326 ilen = imat_ilen[row]; 1327 mat_i = imat_i[row]; 1328 mat_a = imat_a + mat_i; 1329 mat_j = imat_j + mat_i; 1330 max2 = rbuf2_i[ct1]; 1331 if (!allcolumns[is_no]) { 1332 for (l=0; l<max2; l++,ct2++) { 1333 #if defined(PETSC_USE_CTABLE) 1334 ierr = PetscTableFind(cmap_i,rbuf3_i[ct2]+1,&tcol);CHKERRQ(ierr); 1335 #else 1336 tcol = cmap_i[rbuf3_i[ct2]]; 1337 #endif 1338 if (tcol) { 1339 *mat_j++ = tcol - 1; 1340 *mat_a++ = rbuf4_i[ct2]; 1341 ilen++; 1342 } 1343 } 1344 } else { /* allcolumns */ 1345 for (l=0; l<max2; l++,ct2++) { 1346 *mat_j++ = rbuf3_i[ct2]; /* same global column index of C */ 1347 *mat_a++ = rbuf4_i[ct2]; 1348 ilen++; 1349 } 1350 } 1351 imat_ilen[row] = ilen; 1352 } 1353 } 1354 } 1355 1356 if (!iscsorted) { /* sort column indices of the rows */ 1357 for (i=0; i<ismax; i++) { 1358 subc = (Mat_SeqBAIJ*)submats[i]->data; 1359 imat_j = subc->j; 1360 imat_i = subc->i; 1361 imat_a = subc->a; 1362 imat_ilen = subc->ilen; 1363 1364 if (allcolumns[i]) continue; 1365 jmax = nrow[i]; 1366 for (j=0; j<jmax; j++) { 1367 PetscInt ilen; 1368 1369 mat_i = imat_i[j]; 1370 mat_a = imat_a + mat_i; 1371 mat_j = imat_j + mat_i; 1372 ilen = imat_ilen[j]; 1373 ierr = PetscSortIntWithScalarArray(ilen,mat_j,mat_a);CHKERRQ(ierr); 1374 } 1375 } 1376 } 1377 1378 ierr = PetscFree(r_status4);CHKERRQ(ierr); 1379 ierr = PetscFree(r_waits4);CHKERRQ(ierr); 1380 if (nrqr) {ierr = MPI_Waitall(nrqr,s_waits4,s_status4);CHKERRQ(ierr);} 1381 ierr = PetscFree(s_waits4);CHKERRQ(ierr); 1382 ierr = PetscFree(s_status4);CHKERRQ(ierr); 1383 1384 /* Restore the indices */ 1385 for (i=0; i<ismax; i++) { 1386 ierr = ISRestoreIndices(isrow[i],irow+i);CHKERRQ(ierr); 1387 if (!allcolumns[i]) { 1388 ierr = ISRestoreIndices(iscol[i],icol+i);CHKERRQ(ierr); 1389 } 1390 } 1391 1392 for (i=0; i<ismax; i++) { 1393 ierr = MatAssemblyBegin(submats[i],MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1394 ierr = MatAssemblyEnd(submats[i],MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1395 } 1396 1397 /* Destroy allocated memory */ 1398 if (!ismax) { 1399 ierr = PetscFree(pa);CHKERRQ(ierr); 1400 1401 ierr = PetscFree4(sbuf1,ptr,tmp,ctr);CHKERRQ(ierr); 1402 for (i=0; i<nrqr; ++i) { 1403 ierr = PetscFree(sbuf2[i]);CHKERRQ(ierr); 1404 } 1405 for (i=0; i<nrqs; ++i) { 1406 ierr = PetscFree(rbuf3[i]);CHKERRQ(ierr); 1407 } 1408 1409 ierr = PetscFree3(sbuf2,req_size,req_source1);CHKERRQ(ierr); 1410 ierr = PetscFree3(req_source2,rbuf2,rbuf3);CHKERRQ(ierr); 1411 } 1412 1413 ierr = PetscFree(sbuf_aa[0]);CHKERRQ(ierr); 1414 ierr = PetscFree(sbuf_aa);CHKERRQ(ierr); 1415 ierr = PetscFree5(irow,icol,nrow,ncol,issorted);CHKERRQ(ierr); 1416 1417 for (i=0; i<nrqs; ++i) { 1418 ierr = PetscFree(rbuf4[i]);CHKERRQ(ierr); 1419 } 1420 ierr = PetscFree(rbuf4);CHKERRQ(ierr); 1421 1422 ierr = PetscFree5(smats,row2proc,cmap,rmap,allcolumns);CHKERRQ(ierr); 1423 PetscFunctionReturn(0); 1424 } 1425 #endif 1426 //------------ endof new ------------- 1427 1428 PetscErrorCode MatGetSubMatrices_MPIBAIJ_local(Mat C,PetscInt ismax,const IS isrow[],const IS iscol[],MatReuse scall,PetscBool *allrows,PetscBool *allcolumns,Mat *submats) 1429 { 1430 Mat_MPIBAIJ *c = (Mat_MPIBAIJ*)C->data; 1431 Mat A = c->A; 1432 Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data,*b = (Mat_SeqBAIJ*)c->B->data,*mat; 1433 const PetscInt **irow,**icol,*irow_i; 1434 PetscInt *nrow,*ncol,*w3,*w4,start; 1435 PetscErrorCode ierr; 1436 PetscMPIInt size,tag0,tag1,tag2,tag3,*w1,*w2,nrqr,idex,end,proc; 1437 PetscInt **sbuf1,**sbuf2,rank,i,j,k,l,ct1,ct2,**rbuf1,row; 1438 PetscInt nrqs,msz,**ptr,*req_size,*ctr,*pa,*tmp,tcol; 1439 PetscInt **rbuf3,*req_source,**sbuf_aj,**rbuf2,max1,max2; 1440 PetscInt **lens,is_no,ncols,*cols,mat_i,*mat_j,tmp2,jmax; 1441 PetscInt ctr_j,*sbuf1_j,*sbuf_aj_i,*rbuf1_i,kmax,*lens_i; 1442 PetscInt bs =C->rmap->bs,bs2=c->bs2,*a_j=a->j,*b_j=b->j,*cworkA,*cworkB; 1443 PetscInt cstart = c->cstartbs,nzA,nzB,*a_i=a->i,*b_i=b->i,imark; 1444 PetscInt *bmap = c->garray,ctmp,rstart=c->rstartbs; 1445 MPI_Request *s_waits1,*r_waits1,*s_waits2,*r_waits2,*r_waits3,*s_waits3; 1446 MPI_Status *r_status1,*r_status2,*s_status1,*s_status3,*s_status2,*r_status3; 1447 MPI_Comm comm; 1448 PetscBool flag; 1449 PetscMPIInt *onodes1,*olengths1; 1450 PetscBool ijonly=c->ijonly; /* private flag indicates only matrix data structures are requested */ 1451 1452 /* variables below are used for the matrix numerical values - case of !ijonly */ 1453 MPI_Request *r_waits4,*s_waits4; 1454 MPI_Status *r_status4,*s_status4; 1455 MatScalar **rbuf4,**sbuf_aa,*vals,*mat_a = NULL,*sbuf_aa_i,*vworkA = NULL,*vworkB = NULL; 1456 MatScalar *a_a=a->a,*b_a=b->a; 1457 1458 #if defined(PETSC_USE_CTABLE) 1459 PetscInt tt; 1460 PetscTable *rmap,*cmap,rmap_i,cmap_i=NULL; 1461 #else 1462 PetscInt **cmap,*cmap_i=NULL,*rtable,*rmap_i,**rmap, Mbs = c->Mbs; 1463 #endif 1464 1465 PetscFunctionBegin; 1466 ierr = PetscObjectGetComm((PetscObject)C,&comm);CHKERRQ(ierr); 1467 tag0 = ((PetscObject)C)->tag; 1468 size = c->size; 1469 rank = c->rank; 1470 if (!rank) printf("MatGetSubMatrices_MPIBAIJ scall %d, bs %d\n",scall,bs); 1471 1472 /* Get some new tags to keep the communication clean */ 1473 ierr = PetscObjectGetNewTag((PetscObject)C,&tag1);CHKERRQ(ierr); 1474 ierr = PetscObjectGetNewTag((PetscObject)C,&tag2);CHKERRQ(ierr); 1475 ierr = PetscObjectGetNewTag((PetscObject)C,&tag3);CHKERRQ(ierr); 1476 1477 #if defined(PETSC_USE_CTABLE) 1478 ierr = PetscMalloc4(ismax,&irow,ismax,&icol,ismax,&nrow,ismax,&ncol);CHKERRQ(ierr); 1479 #else 1480 ierr = PetscMalloc5(ismax,&irow,ismax,&icol,ismax,&nrow,ismax,&ncol,Mbs+1,&rtable);CHKERRQ(ierr); 1481 /* Create hash table for the mapping :row -> proc*/ 1482 for (i=0,j=0; i<size; i++) { 1483 jmax = C->rmap->range[i+1]/bs; 1484 for (; j<jmax; j++) rtable[j] = i; 1485 } 1486 #endif 1487 1488 for (i=0; i<ismax; i++) { 1489 if (allrows[i]) { 1490 irow[i] = NULL; 1491 nrow[i] = C->rmap->N/bs; 1492 } else { 1493 ierr = ISGetIndices(isrow[i],&irow[i]);CHKERRQ(ierr); 1494 ierr = ISGetLocalSize(isrow[i],&nrow[i]);CHKERRQ(ierr); 1495 } 1496 1497 if (allcolumns[i]) { 1498 icol[i] = NULL; 1499 ncol[i] = C->cmap->N/bs; 1500 } else { 1501 ierr = ISGetIndices(iscol[i],&icol[i]);CHKERRQ(ierr); 1502 ierr = ISGetLocalSize(iscol[i],&ncol[i]);CHKERRQ(ierr); 1503 } 1504 } 1505 1506 /* evaluate communication - mesg to who,length of mesg,and buffer space 1507 required. Based on this, buffers are allocated, and data copied into them*/ 1508 ierr = PetscCalloc4(size,&w1,size,&w2,size,&w3,size,&w4);CHKERRQ(ierr); 1509 for (i=0; i<ismax; i++) { 1510 ierr = PetscMemzero(w4,size*sizeof(PetscInt));CHKERRQ(ierr); /* initialise work vector*/ 1511 jmax = nrow[i]; 1512 irow_i = irow[i]; 1513 for (j=0; j<jmax; j++) { 1514 if (allrows[i]) row = j; 1515 else row = irow_i[j]; 1516 1517 #if defined(PETSC_USE_CTABLE) 1518 ierr = PetscGetProc(row,size,c->rangebs,&proc);CHKERRQ(ierr); 1519 #else 1520 proc = rtable[row]; 1521 #endif 1522 w4[proc]++; 1523 } 1524 for (j=0; j<size; j++) { 1525 if (w4[j]) { w1[j] += w4[j]; w3[j]++;} 1526 } 1527 } 1528 1529 nrqs = 0; /* no of outgoing messages */ 1530 msz = 0; /* total mesg length for all proc */ 1531 w1[rank] = 0; /* no mesg sent to intself */ 1532 w3[rank] = 0; 1533 for (i=0; i<size; i++) { 1534 if (w1[i]) { w2[i] = 1; nrqs++;} /* there exists a message to proc i */ 1535 } 1536 ierr = PetscMalloc1(nrqs+1,&pa);CHKERRQ(ierr); /*(proc -array)*/ 1537 for (i=0,j=0; i<size; i++) { 1538 if (w1[i]) { pa[j] = i; j++; } 1539 } 1540 1541 /* Each message would have a header = 1 + 2*(no of IS) + data */ 1542 for (i=0; i<nrqs; i++) { 1543 j = pa[i]; 1544 w1[j] += w2[j] + 2* w3[j]; 1545 msz += w1[j]; 1546 } 1547 1548 /* Determine the number of messages to expect, their lengths, from from-ids */ 1549 ierr = PetscGatherNumberOfMessages(comm,w2,w1,&nrqr);CHKERRQ(ierr); 1550 ierr = PetscGatherMessageLengths(comm,nrqs,nrqr,w1,&onodes1,&olengths1);CHKERRQ(ierr); 1551 1552 /* Now post the Irecvs corresponding to these messages */ 1553 ierr = PetscPostIrecvInt(comm,tag0,nrqr,onodes1,olengths1,&rbuf1,&r_waits1);CHKERRQ(ierr); 1554 1555 ierr = PetscFree(onodes1);CHKERRQ(ierr); 1556 ierr = PetscFree(olengths1);CHKERRQ(ierr); 1557 1558 /* Allocate Memory for outgoing messages */ 1559 ierr = PetscMalloc4(size,&sbuf1,size,&ptr,2*msz,&tmp,size,&ctr);CHKERRQ(ierr); 1560 ierr = PetscMemzero(sbuf1,size*sizeof(PetscInt*));CHKERRQ(ierr); 1561 ierr = PetscMemzero(ptr,size*sizeof(PetscInt*));CHKERRQ(ierr); 1562 { 1563 PetscInt *iptr = tmp,ict = 0; 1564 for (i=0; i<nrqs; i++) { 1565 j = pa[i]; 1566 iptr += ict; 1567 sbuf1[j] = iptr; 1568 ict = w1[j]; 1569 } 1570 } 1571 1572 /* Form the outgoing messages */ 1573 /* Initialise the header space */ 1574 for (i=0; i<nrqs; i++) { 1575 j = pa[i]; 1576 sbuf1[j][0] = 0; 1577 ierr = PetscMemzero(sbuf1[j]+1,2*w3[j]*sizeof(PetscInt));CHKERRQ(ierr); 1578 ptr[j] = sbuf1[j] + 2*w3[j] + 1; 1579 } 1580 1581 /* Parse the isrow and copy data into outbuf */ 1582 for (i=0; i<ismax; i++) { 1583 ierr = PetscMemzero(ctr,size*sizeof(PetscInt));CHKERRQ(ierr); 1584 irow_i = irow[i]; 1585 jmax = nrow[i]; 1586 for (j=0; j<jmax; j++) { /* parse the indices of each IS */ 1587 if (allrows[i]) row = j; 1588 else row = irow_i[j]; 1589 1590 #if defined(PETSC_USE_CTABLE) 1591 ierr = PetscGetProc(row,size,c->rangebs,&proc);CHKERRQ(ierr); 1592 #else 1593 proc = rtable[row]; 1594 #endif 1595 if (proc != rank) { /* copy to the outgoing buf*/ 1596 ctr[proc]++; 1597 *ptr[proc] = row; 1598 ptr[proc]++; 1599 } 1600 } 1601 /* Update the headers for the current IS */ 1602 for (j=0; j<size; j++) { /* Can Optimise this loop too */ 1603 if ((ctr_j = ctr[j])) { 1604 sbuf1_j = sbuf1[j]; 1605 k = ++sbuf1_j[0]; 1606 sbuf1_j[2*k] = ctr_j; 1607 sbuf1_j[2*k-1] = i; 1608 } 1609 } 1610 } 1611 1612 /* Now post the sends */ 1613 ierr = PetscMalloc1(nrqs+1,&s_waits1);CHKERRQ(ierr); 1614 for (i=0; i<nrqs; ++i) { 1615 j = pa[i]; 1616 ierr = MPI_Isend(sbuf1[j],w1[j],MPIU_INT,j,tag0,comm,s_waits1+i);CHKERRQ(ierr); 1617 } 1618 1619 /* Post Recieves to capture the buffer size */ 1620 ierr = PetscMalloc1(nrqs+1,&r_waits2);CHKERRQ(ierr); 1621 ierr = PetscMalloc1(nrqs+1,&rbuf2);CHKERRQ(ierr); 1622 rbuf2[0] = tmp + msz; 1623 for (i=1; i<nrqs; ++i) { 1624 rbuf2[i] = rbuf2[i-1]+w1[pa[i-1]]; 1625 } 1626 for (i=0; i<nrqs; ++i) { 1627 j = pa[i]; 1628 ierr = MPI_Irecv(rbuf2[i],w1[j],MPIU_INT,j,tag1,comm,r_waits2+i);CHKERRQ(ierr); 1629 } 1630 1631 /* Send to other procs the buf size they should allocate */ 1632 1633 /* Receive messages*/ 1634 ierr = PetscMalloc1(nrqr+1,&s_waits2);CHKERRQ(ierr); 1635 ierr = PetscMalloc1(nrqr+1,&r_status1);CHKERRQ(ierr); 1636 ierr = PetscMalloc3(nrqr+1,&sbuf2,nrqr,&req_size,nrqr,&req_source);CHKERRQ(ierr); 1637 { 1638 Mat_SeqBAIJ *sA = (Mat_SeqBAIJ*)c->A->data,*sB = (Mat_SeqBAIJ*)c->B->data; 1639 PetscInt *sAi = sA->i,*sBi = sB->i,id,*sbuf2_i; 1640 1641 for (i=0; i<nrqr; ++i) { 1642 ierr = MPI_Waitany(nrqr,r_waits1,&idex,r_status1+i);CHKERRQ(ierr); 1643 1644 req_size[idex] = 0; 1645 rbuf1_i = rbuf1[idex]; 1646 start = 2*rbuf1_i[0] + 1; 1647 ierr = MPI_Get_count(r_status1+i,MPIU_INT,&end);CHKERRQ(ierr); 1648 ierr = PetscMalloc1(end,&sbuf2[idex]);CHKERRQ(ierr); 1649 sbuf2_i = sbuf2[idex]; 1650 for (j=start; j<end; j++) { 1651 id = rbuf1_i[j] - rstart; 1652 ncols = sAi[id+1] - sAi[id] + sBi[id+1] - sBi[id]; 1653 sbuf2_i[j] = ncols; 1654 req_size[idex] += ncols; 1655 } 1656 req_source[idex] = r_status1[i].MPI_SOURCE; 1657 /* form the header */ 1658 sbuf2_i[0] = req_size[idex]; 1659 for (j=1; j<start; j++) sbuf2_i[j] = rbuf1_i[j]; 1660 ierr = MPI_Isend(sbuf2_i,end,MPIU_INT,req_source[idex],tag1,comm,s_waits2+i);CHKERRQ(ierr); 1661 } 1662 } 1663 ierr = PetscFree(r_status1);CHKERRQ(ierr); 1664 ierr = PetscFree(r_waits1);CHKERRQ(ierr); 1665 1666 /* recv buffer sizes */ 1667 /* Receive messages*/ 1668 ierr = PetscMalloc1(nrqs+1,&rbuf3);CHKERRQ(ierr); 1669 ierr = PetscMalloc1(nrqs+1,&r_waits3);CHKERRQ(ierr); 1670 ierr = PetscMalloc1(nrqs+1,&r_status2);CHKERRQ(ierr); 1671 if (!ijonly) { 1672 ierr = PetscMalloc1(nrqs+1,&rbuf4);CHKERRQ(ierr); 1673 ierr = PetscMalloc1(nrqs+1,&r_waits4);CHKERRQ(ierr); 1674 } 1675 1676 for (i=0; i<nrqs; ++i) { 1677 ierr = MPI_Waitany(nrqs,r_waits2,&idex,r_status2+i);CHKERRQ(ierr); 1678 ierr = PetscMalloc1(rbuf2[idex][0],&rbuf3[idex]);CHKERRQ(ierr); 1679 ierr = MPI_Irecv(rbuf3[idex],rbuf2[idex][0],MPIU_INT,r_status2[i].MPI_SOURCE,tag2,comm,r_waits3+idex);CHKERRQ(ierr); 1680 if (!ijonly) { 1681 ierr = PetscMalloc1(rbuf2[idex][0]*bs2,&rbuf4[idex]);CHKERRQ(ierr); 1682 ierr = MPI_Irecv(rbuf4[idex],rbuf2[idex][0]*bs2,MPIU_MATSCALAR,r_status2[i].MPI_SOURCE,tag3,comm,r_waits4+idex);CHKERRQ(ierr); 1683 } 1684 } 1685 ierr = PetscFree(r_status2);CHKERRQ(ierr); 1686 ierr = PetscFree(r_waits2);CHKERRQ(ierr); 1687 1688 /* Wait on sends1 and sends2 */ 1689 ierr = PetscMalloc1(nrqs+1,&s_status1);CHKERRQ(ierr); 1690 ierr = PetscMalloc1(nrqr+1,&s_status2);CHKERRQ(ierr); 1691 1692 if (nrqs) {ierr = MPI_Waitall(nrqs,s_waits1,s_status1);CHKERRQ(ierr);} 1693 if (nrqr) {ierr = MPI_Waitall(nrqr,s_waits2,s_status2);CHKERRQ(ierr);} 1694 ierr = PetscFree(s_status1);CHKERRQ(ierr); 1695 ierr = PetscFree(s_status2);CHKERRQ(ierr); 1696 ierr = PetscFree(s_waits1);CHKERRQ(ierr); 1697 ierr = PetscFree(s_waits2);CHKERRQ(ierr); 1698 1699 /* Now allocate buffers for a->j, and send them off */ 1700 ierr = PetscMalloc1(nrqr+1,&sbuf_aj);CHKERRQ(ierr); 1701 for (i=0,j=0; i<nrqr; i++) j += req_size[i]; 1702 ierr = PetscMalloc1(j+1,&sbuf_aj[0]);CHKERRQ(ierr); 1703 for (i=1; i<nrqr; i++) sbuf_aj[i] = sbuf_aj[i-1] + req_size[i-1]; 1704 1705 ierr = PetscMalloc1(nrqr+1,&s_waits3);CHKERRQ(ierr); 1706 { 1707 for (i=0; i<nrqr; i++) { 1708 rbuf1_i = rbuf1[i]; 1709 sbuf_aj_i = sbuf_aj[i]; 1710 ct1 = 2*rbuf1_i[0] + 1; 1711 ct2 = 0; 1712 for (j=1,max1=rbuf1_i[0]; j<=max1; j++) { 1713 kmax = rbuf1[i][2*j]; 1714 for (k=0; k<kmax; k++,ct1++) { 1715 row = rbuf1_i[ct1] - rstart; 1716 nzA = a_i[row+1] - a_i[row]; 1717 nzB = b_i[row+1] - b_i[row]; 1718 ncols = nzA + nzB; 1719 cworkA = a_j + a_i[row]; 1720 cworkB = b_j + b_i[row]; 1721 1722 /* load the column indices for this row into cols*/ 1723 cols = sbuf_aj_i + ct2; 1724 for (l=0; l<nzB; l++) { 1725 if ((ctmp = bmap[cworkB[l]]) < cstart) cols[l] = ctmp; 1726 else break; 1727 } 1728 imark = l; 1729 for (l=0; l<nzA; l++) cols[imark+l] = cstart + cworkA[l]; 1730 for (l=imark; l<nzB; l++) cols[nzA+l] = bmap[cworkB[l]]; 1731 ct2 += ncols; 1732 } 1733 } 1734 ierr = MPI_Isend(sbuf_aj_i,req_size[i],MPIU_INT,req_source[i],tag2,comm,s_waits3+i);CHKERRQ(ierr); 1735 } 1736 } 1737 ierr = PetscMalloc1(nrqs+1,&r_status3);CHKERRQ(ierr); 1738 ierr = PetscMalloc1(nrqr+1,&s_status3);CHKERRQ(ierr); 1739 1740 /* Allocate buffers for a->a, and send them off */ 1741 if (!ijonly) { 1742 ierr = PetscMalloc1(nrqr+1,&sbuf_aa);CHKERRQ(ierr); 1743 for (i=0,j=0; i<nrqr; i++) j += req_size[i]; 1744 ierr = PetscMalloc1((j+1)*bs2,&sbuf_aa[0]);CHKERRQ(ierr); 1745 for (i=1; i<nrqr; i++) sbuf_aa[i] = sbuf_aa[i-1] + req_size[i-1]*bs2; 1746 1747 ierr = PetscMalloc1(nrqr+1,&s_waits4);CHKERRQ(ierr); 1748 { 1749 for (i=0; i<nrqr; i++) { 1750 rbuf1_i = rbuf1[i]; 1751 sbuf_aa_i = sbuf_aa[i]; 1752 ct1 = 2*rbuf1_i[0]+1; 1753 ct2 = 0; 1754 for (j=1,max1=rbuf1_i[0]; j<=max1; j++) { 1755 kmax = rbuf1_i[2*j]; 1756 for (k=0; k<kmax; k++,ct1++) { 1757 row = rbuf1_i[ct1] - rstart; 1758 nzA = a_i[row+1] - a_i[row]; 1759 nzB = b_i[row+1] - b_i[row]; 1760 ncols = nzA + nzB; 1761 cworkB = b_j + b_i[row]; 1762 vworkA = a_a + a_i[row]*bs2; 1763 vworkB = b_a + b_i[row]*bs2; 1764 1765 /* load the column values for this row into vals*/ 1766 vals = sbuf_aa_i+ct2*bs2; 1767 for (l=0; l<nzB; l++) { 1768 if ((bmap[cworkB[l]]) < cstart) { 1769 ierr = PetscMemcpy(vals+l*bs2,vworkB+l*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr); 1770 } else break; 1771 } 1772 imark = l; 1773 for (l=0; l<nzA; l++) { 1774 ierr = PetscMemcpy(vals+(imark+l)*bs2,vworkA+l*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr); 1775 } 1776 for (l=imark; l<nzB; l++) { 1777 ierr = PetscMemcpy(vals+(nzA+l)*bs2,vworkB+l*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr); 1778 } 1779 ct2 += ncols; 1780 } 1781 } 1782 ierr = MPI_Isend(sbuf_aa_i,req_size[i]*bs2,MPIU_MATSCALAR,req_source[i],tag3,comm,s_waits4+i);CHKERRQ(ierr); 1783 } 1784 } 1785 ierr = PetscMalloc1(nrqs+1,&r_status4);CHKERRQ(ierr); 1786 ierr = PetscMalloc1(nrqr+1,&s_status4);CHKERRQ(ierr); 1787 } 1788 ierr = PetscFree(rbuf1[0]);CHKERRQ(ierr); 1789 ierr = PetscFree(rbuf1);CHKERRQ(ierr); 1790 1791 /* Form the matrix */ 1792 /* create col map: global col of C -> local col of submatrices */ 1793 { 1794 const PetscInt *icol_i; 1795 #if defined(PETSC_USE_CTABLE) 1796 ierr = PetscMalloc1(1+ismax,&cmap);CHKERRQ(ierr); 1797 for (i=0; i<ismax; i++) { 1798 if (!allcolumns[i]) { 1799 ierr = PetscTableCreate(ncol[i]+1,c->Nbs+1,&cmap[i]);CHKERRQ(ierr); 1800 jmax = ncol[i]; 1801 icol_i = icol[i]; 1802 cmap_i = cmap[i]; 1803 for (j=0; j<jmax; j++) { 1804 ierr = PetscTableAdd(cmap_i,icol_i[j]+1,j+1,INSERT_VALUES);CHKERRQ(ierr); 1805 } 1806 } else { 1807 cmap[i] = NULL; 1808 } 1809 } 1810 #else 1811 ierr = PetscMalloc1(ismax,&cmap);CHKERRQ(ierr); 1812 for (i=0; i<ismax; i++) { 1813 if (!allcolumns[i]) { 1814 ierr = PetscCalloc1(c->Nbs,&cmap[i]);CHKERRQ(ierr); 1815 jmax = ncol[i]; 1816 icol_i = icol[i]; 1817 cmap_i = cmap[i]; 1818 for (j=0; j<jmax; j++) { 1819 cmap_i[icol_i[j]] = j+1; 1820 } 1821 } else { /* allcolumns[i] */ 1822 cmap[i] = NULL; 1823 } 1824 } 1825 #endif 1826 } 1827 1828 /* Create lens which is required for MatCreate... */ 1829 for (i=0,j=0; i<ismax; i++) j += nrow[i]; 1830 ierr = PetscMalloc((1+ismax)*sizeof(PetscInt*)+ j*sizeof(PetscInt),&lens);CHKERRQ(ierr); 1831 lens[0] = (PetscInt*)(lens + ismax); 1832 ierr = PetscMemzero(lens[0],j*sizeof(PetscInt));CHKERRQ(ierr); 1833 for (i=1; i<ismax; i++) lens[i] = lens[i-1] + nrow[i-1]; 1834 1835 /* Update lens from local data */ 1836 for (i=0; i<ismax; i++) { 1837 jmax = nrow[i]; 1838 if (!allcolumns[i]) cmap_i = cmap[i]; 1839 irow_i = irow[i]; 1840 lens_i = lens[i]; 1841 for (j=0; j<jmax; j++) { 1842 if (allrows[i]) row = j; 1843 else row = irow_i[j]; 1844 1845 #if defined(PETSC_USE_CTABLE) 1846 ierr = PetscGetProc(row,size,c->rangebs,&proc);CHKERRQ(ierr); 1847 #else 1848 proc = rtable[row]; 1849 #endif 1850 if (proc == rank) { 1851 /* Get indices from matA and then from matB */ 1852 row = row - rstart; 1853 nzA = a_i[row+1] - a_i[row]; 1854 nzB = b_i[row+1] - b_i[row]; 1855 cworkA = a_j + a_i[row]; 1856 cworkB = b_j + b_i[row]; 1857 if (!allcolumns[i]) { 1858 #if defined(PETSC_USE_CTABLE) 1859 for (k=0; k<nzA; k++) { 1860 ierr = PetscTableFind(cmap_i,cstart+cworkA[k]+1,&tt);CHKERRQ(ierr); 1861 if (tt) lens_i[j]++; 1862 } 1863 for (k=0; k<nzB; k++) { 1864 ierr = PetscTableFind(cmap_i,bmap[cworkB[k]]+1,&tt);CHKERRQ(ierr); 1865 if (tt) lens_i[j]++; 1866 } 1867 1868 #else 1869 for (k=0; k<nzA; k++) { 1870 if (cmap_i[cstart + cworkA[k]]) lens_i[j]++; 1871 } 1872 for (k=0; k<nzB; k++) { 1873 if (cmap_i[bmap[cworkB[k]]]) lens_i[j]++; 1874 } 1875 #endif 1876 } else { /* allcolumns */ 1877 lens_i[j] = nzA + nzB; 1878 } 1879 } 1880 } 1881 } 1882 #if defined(PETSC_USE_CTABLE) 1883 /* Create row map*/ 1884 ierr = PetscMalloc1(1+ismax,&rmap);CHKERRQ(ierr); 1885 for (i=0; i<ismax; i++) { 1886 ierr = PetscTableCreate(nrow[i]+1,c->Mbs+1,&rmap[i]);CHKERRQ(ierr); 1887 } 1888 #else 1889 /* Create row map*/ 1890 ierr = PetscMalloc((1+ismax)*sizeof(PetscInt*)+ ismax*Mbs*sizeof(PetscInt),&rmap);CHKERRQ(ierr); 1891 rmap[0] = (PetscInt*)(rmap + ismax); 1892 ierr = PetscMemzero(rmap[0],ismax*Mbs*sizeof(PetscInt));CHKERRQ(ierr); 1893 for (i=1; i<ismax; i++) rmap[i] = rmap[i-1] + Mbs; 1894 #endif 1895 for (i=0; i<ismax; i++) { 1896 irow_i = irow[i]; 1897 jmax = nrow[i]; 1898 #if defined(PETSC_USE_CTABLE) 1899 rmap_i = rmap[i]; 1900 for (j=0; j<jmax; j++) { 1901 if (allrows[i]) { 1902 ierr = PetscTableAdd(rmap_i,j+1,j+1,INSERT_VALUES);CHKERRQ(ierr); 1903 } else { 1904 ierr = PetscTableAdd(rmap_i,irow_i[j]+1,j+1,INSERT_VALUES);CHKERRQ(ierr); 1905 } 1906 } 1907 #else 1908 rmap_i = rmap[i]; 1909 for (j=0; j<jmax; j++) { 1910 if (allrows[i]) rmap_i[j] = j; 1911 else rmap_i[irow_i[j]] = j; 1912 } 1913 #endif 1914 } 1915 1916 /* Update lens from offproc data */ 1917 { 1918 PetscInt *rbuf2_i,*rbuf3_i,*sbuf1_i; 1919 PetscMPIInt ii; 1920 1921 for (tmp2=0; tmp2<nrqs; tmp2++) { 1922 ierr = MPI_Waitany(nrqs,r_waits3,&ii,r_status3+tmp2);CHKERRQ(ierr); 1923 idex = pa[ii]; 1924 sbuf1_i = sbuf1[idex]; 1925 jmax = sbuf1_i[0]; 1926 ct1 = 2*jmax+1; 1927 ct2 = 0; 1928 rbuf2_i = rbuf2[ii]; 1929 rbuf3_i = rbuf3[ii]; 1930 for (j=1; j<=jmax; j++) { 1931 is_no = sbuf1_i[2*j-1]; 1932 max1 = sbuf1_i[2*j]; 1933 lens_i = lens[is_no]; 1934 if (!allcolumns[is_no]) cmap_i = cmap[is_no]; 1935 rmap_i = rmap[is_no]; 1936 for (k=0; k<max1; k++,ct1++) { 1937 #if defined(PETSC_USE_CTABLE) 1938 ierr = PetscTableFind(rmap_i,sbuf1_i[ct1]+1,&row);CHKERRQ(ierr); 1939 row--; 1940 if (row < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"row not found in table"); 1941 #else 1942 row = rmap_i[sbuf1_i[ct1]]; /* the val in the new matrix to be */ 1943 #endif 1944 max2 = rbuf2_i[ct1]; 1945 for (l=0; l<max2; l++,ct2++) { 1946 if (!allcolumns[is_no]) { 1947 #if defined(PETSC_USE_CTABLE) 1948 ierr = PetscTableFind(cmap_i,rbuf3_i[ct2]+1,&tt);CHKERRQ(ierr); 1949 if (tt) lens_i[row]++; 1950 #else 1951 if (cmap_i[rbuf3_i[ct2]]) lens_i[row]++; 1952 #endif 1953 } else { /* allcolumns */ 1954 lens_i[row]++; 1955 } 1956 } 1957 } 1958 } 1959 } 1960 } 1961 ierr = PetscFree(r_status3);CHKERRQ(ierr); 1962 ierr = PetscFree(r_waits3);CHKERRQ(ierr); 1963 if (nrqr) {ierr = MPI_Waitall(nrqr,s_waits3,s_status3);CHKERRQ(ierr);} 1964 ierr = PetscFree(s_status3);CHKERRQ(ierr); 1965 ierr = PetscFree(s_waits3);CHKERRQ(ierr); 1966 1967 /* Create the submatrices */ 1968 if (scall == MAT_REUSE_MATRIX) { 1969 if (ijonly) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP," MAT_REUSE_MATRIX and ijonly is not supported yet"); 1970 /* 1971 Assumes new rows are same length as the old rows, hence bug! 1972 */ 1973 for (i=0; i<ismax; i++) { 1974 mat = (Mat_SeqBAIJ*)(submats[i]->data); 1975 if ((mat->mbs != nrow[i]) || (mat->nbs != ncol[i] || C->rmap->bs != bs)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size"); 1976 ierr = PetscMemcmp(mat->ilen,lens[i],mat->mbs *sizeof(PetscInt),&flag);CHKERRQ(ierr); 1977 if (!flag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Cannot reuse matrix. wrong no of nonzeros"); 1978 /* Initial matrix as if empty */ 1979 ierr = PetscMemzero(mat->ilen,mat->mbs*sizeof(PetscInt));CHKERRQ(ierr); 1980 1981 submats[i]->factortype = C->factortype; 1982 } 1983 } else { 1984 PetscInt bs_tmp; 1985 if (ijonly) bs_tmp = 1; 1986 else bs_tmp = bs; 1987 for (i=0; i<ismax; i++) { 1988 ierr = MatCreate(PETSC_COMM_SELF,submats+i);CHKERRQ(ierr); 1989 ierr = MatSetSizes(submats[i],nrow[i]*bs_tmp,ncol[i]*bs_tmp,nrow[i]*bs_tmp,ncol[i]*bs_tmp);CHKERRQ(ierr); 1990 ierr = MatSetType(submats[i],((PetscObject)A)->type_name);CHKERRQ(ierr); 1991 ierr = MatSeqBAIJSetPreallocation(submats[i],bs_tmp,0,lens[i]);CHKERRQ(ierr); 1992 ierr = MatSeqSBAIJSetPreallocation(submats[i],bs_tmp,0,lens[i]);CHKERRQ(ierr); /* this subroutine is used by SBAIJ routines */ 1993 } 1994 } 1995 1996 /* Assemble the matrices */ 1997 /* First assemble the local rows */ 1998 { 1999 PetscInt ilen_row,*imat_ilen,*imat_j,*imat_i; 2000 MatScalar *imat_a = NULL; 2001 2002 for (i=0; i<ismax; i++) { 2003 mat = (Mat_SeqBAIJ*)submats[i]->data; 2004 imat_ilen = mat->ilen; 2005 imat_j = mat->j; 2006 imat_i = mat->i; 2007 if (!ijonly) imat_a = mat->a; 2008 if (!allcolumns[i]) cmap_i = cmap[i]; 2009 rmap_i = rmap[i]; 2010 irow_i = irow[i]; 2011 jmax = nrow[i]; 2012 for (j=0; j<jmax; j++) { 2013 if (allrows[i]) row = j; 2014 else row = irow_i[j]; 2015 2016 #if defined(PETSC_USE_CTABLE) 2017 ierr = PetscGetProc(row,size,c->rangebs,&proc);CHKERRQ(ierr); 2018 #else 2019 proc = rtable[row]; 2020 #endif 2021 if (proc == rank) { 2022 row = row - rstart; 2023 nzA = a_i[row+1] - a_i[row]; 2024 nzB = b_i[row+1] - b_i[row]; 2025 cworkA = a_j + a_i[row]; 2026 cworkB = b_j + b_i[row]; 2027 if (!ijonly) { 2028 vworkA = a_a + a_i[row]*bs2; 2029 vworkB = b_a + b_i[row]*bs2; 2030 } 2031 #if defined(PETSC_USE_CTABLE) 2032 ierr = PetscTableFind(rmap_i,row+rstart+1,&row);CHKERRQ(ierr); 2033 row--; 2034 if (row < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"row not found in table"); 2035 #else 2036 row = rmap_i[row + rstart]; 2037 #endif 2038 mat_i = imat_i[row]; 2039 if (!ijonly) mat_a = imat_a + mat_i*bs2; 2040 mat_j = imat_j + mat_i; 2041 ilen_row = imat_ilen[row]; 2042 2043 /* load the column indices for this row into cols*/ 2044 if (!allcolumns[i]) { 2045 for (l=0; l<nzB; l++) { 2046 if ((ctmp = bmap[cworkB[l]]) < cstart) { 2047 #if defined(PETSC_USE_CTABLE) 2048 ierr = PetscTableFind(cmap_i,ctmp+1,&tcol);CHKERRQ(ierr); 2049 if (tcol) { 2050 #else 2051 if ((tcol = cmap_i[ctmp])) { 2052 #endif 2053 *mat_j++ = tcol - 1; 2054 ierr = PetscMemcpy(mat_a,vworkB+l*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr); 2055 mat_a += bs2; 2056 ilen_row++; 2057 } 2058 } else break; 2059 } 2060 imark = l; 2061 for (l=0; l<nzA; l++) { 2062 #if defined(PETSC_USE_CTABLE) 2063 ierr = PetscTableFind(cmap_i,cstart+cworkA[l]+1,&tcol);CHKERRQ(ierr); 2064 if (tcol) { 2065 #else 2066 if ((tcol = cmap_i[cstart + cworkA[l]])) { 2067 #endif 2068 *mat_j++ = tcol - 1; 2069 if (!ijonly) { 2070 ierr = PetscMemcpy(mat_a,vworkA+l*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr); 2071 mat_a += bs2; 2072 } 2073 ilen_row++; 2074 } 2075 } 2076 for (l=imark; l<nzB; l++) { 2077 #if defined(PETSC_USE_CTABLE) 2078 ierr = PetscTableFind(cmap_i,bmap[cworkB[l]]+1,&tcol);CHKERRQ(ierr); 2079 if (tcol) { 2080 #else 2081 if ((tcol = cmap_i[bmap[cworkB[l]]])) { 2082 #endif 2083 *mat_j++ = tcol - 1; 2084 if (!ijonly) { 2085 ierr = PetscMemcpy(mat_a,vworkB+l*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr); 2086 mat_a += bs2; 2087 } 2088 ilen_row++; 2089 } 2090 } 2091 } else { /* allcolumns */ 2092 for (l=0; l<nzB; l++) { 2093 if ((ctmp = bmap[cworkB[l]]) < cstart) { 2094 *mat_j++ = ctmp; 2095 ierr = PetscMemcpy(mat_a,vworkB+l*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr); 2096 mat_a += bs2; 2097 ilen_row++; 2098 } else break; 2099 } 2100 imark = l; 2101 for (l=0; l<nzA; l++) { 2102 *mat_j++ = cstart+cworkA[l]; 2103 if (!ijonly) { 2104 ierr = PetscMemcpy(mat_a,vworkA+l*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr); 2105 mat_a += bs2; 2106 } 2107 ilen_row++; 2108 } 2109 for (l=imark; l<nzB; l++) { 2110 *mat_j++ = bmap[cworkB[l]]; 2111 if (!ijonly) { 2112 ierr = PetscMemcpy(mat_a,vworkB+l*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr); 2113 mat_a += bs2; 2114 } 2115 ilen_row++; 2116 } 2117 } 2118 imat_ilen[row] = ilen_row; 2119 } 2120 } 2121 } 2122 } 2123 2124 /* Now assemble the off proc rows*/ 2125 { 2126 PetscInt *sbuf1_i,*rbuf2_i,*rbuf3_i,*imat_ilen,ilen; 2127 PetscInt *imat_j,*imat_i; 2128 MatScalar *imat_a = NULL,*rbuf4_i = NULL; 2129 PetscMPIInt ii; 2130 2131 for (tmp2=0; tmp2<nrqs; tmp2++) { 2132 if (ijonly) ii = tmp2; 2133 else { 2134 ierr = MPI_Waitany(nrqs,r_waits4,&ii,r_status4+tmp2);CHKERRQ(ierr); 2135 } 2136 idex = pa[ii]; 2137 sbuf1_i = sbuf1[idex]; 2138 jmax = sbuf1_i[0]; 2139 ct1 = 2*jmax + 1; 2140 ct2 = 0; 2141 rbuf2_i = rbuf2[ii]; 2142 rbuf3_i = rbuf3[ii]; 2143 if (!ijonly) rbuf4_i = rbuf4[ii]; 2144 for (j=1; j<=jmax; j++) { 2145 is_no = sbuf1_i[2*j-1]; 2146 if (!allcolumns[is_no]) cmap_i = cmap[is_no]; 2147 rmap_i = rmap[is_no]; 2148 mat = (Mat_SeqBAIJ*)submats[is_no]->data; 2149 imat_ilen = mat->ilen; 2150 imat_j = mat->j; 2151 imat_i = mat->i; 2152 if (!ijonly) imat_a = mat->a; 2153 max1 = sbuf1_i[2*j]; 2154 for (k=0; k<max1; k++,ct1++) { 2155 row = sbuf1_i[ct1]; 2156 #if defined(PETSC_USE_CTABLE) 2157 ierr = PetscTableFind(rmap_i,row+1,&row);CHKERRQ(ierr); 2158 row--; 2159 if (row < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"row not found in table"); 2160 #else 2161 row = rmap_i[row]; 2162 #endif 2163 ilen = imat_ilen[row]; 2164 mat_i = imat_i[row]; 2165 if (!ijonly) mat_a = imat_a + mat_i*bs2; 2166 mat_j = imat_j + mat_i; 2167 max2 = rbuf2_i[ct1]; 2168 2169 if (!allcolumns[is_no]) { 2170 for (l=0; l<max2; l++,ct2++) { 2171 #if defined(PETSC_USE_CTABLE) 2172 ierr = PetscTableFind(cmap_i,rbuf3_i[ct2]+1,&tcol);CHKERRQ(ierr); 2173 if (tcol) { 2174 #else 2175 if ((tcol = cmap_i[rbuf3_i[ct2]])) { 2176 #endif 2177 *mat_j++ = tcol - 1; 2178 if (!ijonly) { 2179 ierr = PetscMemcpy(mat_a,rbuf4_i+ct2*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr); 2180 mat_a += bs2; 2181 } 2182 ilen++; 2183 } 2184 } 2185 } else { /* allcolumns */ 2186 for (l=0; l<max2; l++,ct2++) { 2187 *mat_j++ = rbuf3_i[ct2]; 2188 if (!ijonly) { 2189 ierr = PetscMemcpy(mat_a,rbuf4_i+ct2*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr); 2190 mat_a += bs2; 2191 } 2192 ilen++; 2193 } 2194 } 2195 imat_ilen[row] = ilen; 2196 } 2197 } 2198 } 2199 } 2200 /* sort the rows */ 2201 { 2202 PetscInt ilen_row,*imat_ilen,*imat_j,*imat_i; 2203 MatScalar *imat_a = NULL; 2204 MatScalar *work; 2205 2206 ierr = PetscMalloc1(bs2,&work);CHKERRQ(ierr); 2207 for (i=0; i<ismax; i++) { 2208 mat = (Mat_SeqBAIJ*)submats[i]->data; 2209 imat_ilen = mat->ilen; 2210 imat_j = mat->j; 2211 imat_i = mat->i; 2212 if (!ijonly) imat_a = mat->a; 2213 if (allcolumns[i]) continue; 2214 jmax = nrow[i]; 2215 for (j=0; j<jmax; j++) { 2216 mat_i = imat_i[j]; 2217 if (!ijonly) mat_a = imat_a + mat_i*bs2; 2218 mat_j = imat_j + mat_i; 2219 ilen_row = imat_ilen[j]; 2220 if (!ijonly) {ierr = PetscSortIntWithDataArray(ilen_row,mat_j,mat_a,bs2*sizeof(MatScalar),work);CHKERRQ(ierr);} 2221 else {ierr = PetscSortInt(ilen_row,mat_j);CHKERRQ(ierr);} 2222 } 2223 } 2224 ierr = PetscFree(work);CHKERRQ(ierr); 2225 } 2226 if (!ijonly) { 2227 ierr = PetscFree(r_status4);CHKERRQ(ierr); 2228 ierr = PetscFree(r_waits4);CHKERRQ(ierr); 2229 if (nrqr) {ierr = MPI_Waitall(nrqr,s_waits4,s_status4);CHKERRQ(ierr);} 2230 ierr = PetscFree(s_waits4);CHKERRQ(ierr); 2231 ierr = PetscFree(s_status4);CHKERRQ(ierr); 2232 } 2233 2234 /* Restore the indices */ 2235 for (i=0; i<ismax; i++) { 2236 if (!allrows[i]) { 2237 ierr = ISRestoreIndices(isrow[i],irow+i);CHKERRQ(ierr); 2238 } 2239 if (!allcolumns[i]) { 2240 ierr = ISRestoreIndices(iscol[i],icol+i);CHKERRQ(ierr); 2241 } 2242 } 2243 2244 /* Destroy allocated memory */ 2245 #if defined(PETSC_USE_CTABLE) 2246 ierr = PetscFree4(irow,icol,nrow,ncol);CHKERRQ(ierr); 2247 #else 2248 ierr = PetscFree5(irow,icol,nrow,ncol,rtable);CHKERRQ(ierr); 2249 #endif 2250 ierr = PetscFree4(w1,w2,w3,w4);CHKERRQ(ierr); 2251 ierr = PetscFree(pa);CHKERRQ(ierr); 2252 2253 ierr = PetscFree4(sbuf1,ptr,tmp,ctr);CHKERRQ(ierr); 2254 ierr = PetscFree(sbuf1);CHKERRQ(ierr); 2255 ierr = PetscFree(rbuf2);CHKERRQ(ierr); 2256 for (i=0; i<nrqr; ++i) { 2257 ierr = PetscFree(sbuf2[i]);CHKERRQ(ierr); 2258 } 2259 for (i=0; i<nrqs; ++i) { 2260 ierr = PetscFree(rbuf3[i]);CHKERRQ(ierr); 2261 } 2262 ierr = PetscFree3(sbuf2,req_size,req_source);CHKERRQ(ierr); 2263 ierr = PetscFree(rbuf3);CHKERRQ(ierr); 2264 ierr = PetscFree(sbuf_aj[0]);CHKERRQ(ierr); 2265 ierr = PetscFree(sbuf_aj);CHKERRQ(ierr); 2266 if (!ijonly) { 2267 for (i=0; i<nrqs; ++i) {ierr = PetscFree(rbuf4[i]);CHKERRQ(ierr);} 2268 ierr = PetscFree(rbuf4);CHKERRQ(ierr); 2269 ierr = PetscFree(sbuf_aa[0]);CHKERRQ(ierr); 2270 ierr = PetscFree(sbuf_aa);CHKERRQ(ierr); 2271 } 2272 2273 #if defined(PETSC_USE_CTABLE) 2274 for (i=0; i<ismax; i++) { 2275 ierr = PetscTableDestroy((PetscTable*)&rmap[i]);CHKERRQ(ierr); 2276 } 2277 #endif 2278 ierr = PetscFree(rmap);CHKERRQ(ierr); 2279 2280 for (i=0; i<ismax; i++) { 2281 if (!allcolumns[i]) { 2282 #if defined(PETSC_USE_CTABLE) 2283 ierr = PetscTableDestroy((PetscTable*)&cmap[i]);CHKERRQ(ierr); 2284 #else 2285 ierr = PetscFree(cmap[i]);CHKERRQ(ierr); 2286 #endif 2287 } 2288 } 2289 ierr = PetscFree(cmap);CHKERRQ(ierr); 2290 ierr = PetscFree(lens);CHKERRQ(ierr); 2291 2292 for (i=0; i<ismax; i++) { 2293 ierr = MatAssemblyBegin(submats[i],MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2294 ierr = MatAssemblyEnd(submats[i],MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2295 } 2296 2297 c->ijonly = PETSC_FALSE; /* set back to the default */ 2298 PetscFunctionReturn(0); 2299 } 2300 2301