1 /* 2 Routines to compute overlapping regions of a parallel MPI matrix 3 and to find submatrices that were shared across processors. 4 */ 5 #include "src/mat/impls/aij/mpi/mpiaij.h" 6 #include "petscbt.h" 7 8 static PetscErrorCode MatIncreaseOverlap_MPIAIJ_Once(Mat,PetscInt,IS *); 9 static PetscErrorCode MatIncreaseOverlap_MPIAIJ_Local(Mat,PetscInt,char **,PetscInt*,PetscInt**); 10 static PetscErrorCode MatIncreaseOverlap_MPIAIJ_Receive(Mat,PetscInt,PetscInt **,PetscInt**,PetscInt*); 11 EXTERN PetscErrorCode MatGetRow_MPIAIJ(Mat,PetscInt,PetscInt*,PetscInt**,PetscScalar**); 12 EXTERN PetscErrorCode MatRestoreRow_MPIAIJ(Mat,PetscInt,PetscInt*,PetscInt**,PetscScalar**); 13 14 #undef __FUNCT__ 15 #define __FUNCT__ "MatIncreaseOverlap_MPIAIJ" 16 PetscErrorCode MatIncreaseOverlap_MPIAIJ(Mat C,PetscInt imax,IS is[],PetscInt ov) 17 { 18 PetscErrorCode ierr; 19 PetscInt i; 20 21 PetscFunctionBegin; 22 if (ov < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Negative overlap specified"); 23 for (i=0; i<ov; ++i) { 24 ierr = MatIncreaseOverlap_MPIAIJ_Once(C,imax,is);CHKERRQ(ierr); 25 } 26 PetscFunctionReturn(0); 27 } 28 29 /* 30 Sample message format: 31 If a processor A wants processor B to process some elements corresponding 32 to index sets is[1],is[5] 33 mesg [0] = 2 (no of index sets in the mesg) 34 ----------- 35 mesg [1] = 1 => is[1] 36 mesg [2] = sizeof(is[1]); 37 ----------- 38 mesg [3] = 5 => is[5] 39 mesg [4] = sizeof(is[5]); 40 ----------- 41 mesg [5] 42 mesg [n] datas[1] 43 ----------- 44 mesg[n+1] 45 mesg[m] data(is[5]) 46 ----------- 47 48 Notes: 49 nrqs - no of requests sent (or to be sent out) 50 nrqr - no of requests recieved (which have to be or which have been processed 51 */ 52 #undef __FUNCT__ 53 #define __FUNCT__ "MatIncreaseOverlap_MPIAIJ_Once" 54 static PetscErrorCode MatIncreaseOverlap_MPIAIJ_Once(Mat C,PetscInt imax,IS is[]) 55 { 56 Mat_MPIAIJ *c = (Mat_MPIAIJ*)C->data; 57 PetscMPIInt *w1,*w2,nrqr,*w3,*w4,*onodes1,*olengths1,*onodes2,*olengths2; 58 PetscInt **idx,*n,*rtable,**data,len,*idx_i; 59 PetscErrorCode ierr; 60 PetscMPIInt size,rank,tag1,tag2; 61 PetscInt m,i,j,k,**rbuf,row,proc,nrqs,msz,**outdat,**ptr; 62 PetscInt *ctr,*pa,*tmp,*isz,*isz1,**xdata,**rbuf2; 63 PetscBT *table; 64 MPI_Comm comm; 65 MPI_Request *s_waits1,*r_waits1,*s_waits2,*r_waits2; 66 MPI_Status *s_status,*recv_status; 67 68 PetscFunctionBegin; 69 comm = C->comm; 70 size = c->size; 71 rank = c->rank; 72 m = C->M; 73 74 ierr = PetscObjectGetNewTag((PetscObject)C,&tag1);CHKERRQ(ierr); 75 ierr = PetscObjectGetNewTag((PetscObject)C,&tag2);CHKERRQ(ierr); 76 77 len = (imax+1)*sizeof(PetscInt*)+ (imax + m)*sizeof(PetscInt); 78 ierr = PetscMalloc(len,&idx);CHKERRQ(ierr); 79 n = (PetscInt*)(idx + imax); 80 rtable = n + imax; 81 82 for (i=0; i<imax; i++) { 83 ierr = ISGetIndices(is[i],&idx[i]);CHKERRQ(ierr); 84 ierr = ISGetLocalSize(is[i],&n[i]);CHKERRQ(ierr); 85 } 86 87 /* Create hash table for the mapping :row -> proc*/ 88 for (i=0,j=0; i<size; i++) { 89 len = c->rowners[i+1]; 90 for (; j<len; j++) { 91 rtable[j] = i; 92 } 93 } 94 95 /* evaluate communication - mesg to who,length of mesg, and buffer space 96 required. Based on this, buffers are allocated, and data copied into them*/ 97 ierr = PetscMalloc(size*4*sizeof(PetscMPIInt),&w1);CHKERRQ(ierr);/* mesg size */ 98 w2 = w1 + size; /* if w2[i] marked, then a message to proc i*/ 99 w3 = w2 + size; /* no of IS that needs to be sent to proc i */ 100 w4 = w3 + size; /* temp work space used in determining w1, w2, w3 */ 101 ierr = PetscMemzero(w1,size*3*sizeof(PetscMPIInt));CHKERRQ(ierr); /* initialise work vector*/ 102 for (i=0; i<imax; i++) { 103 ierr = PetscMemzero(w4,size*sizeof(PetscMPIInt));CHKERRQ(ierr); /* initialise work vector*/ 104 idx_i = idx[i]; 105 len = n[i]; 106 for (j=0; j<len; j++) { 107 row = idx_i[j]; 108 if (row < 0) { 109 SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Index set cannot have negative entries"); 110 } 111 proc = rtable[row]; 112 w4[proc]++; 113 } 114 for (j=0; j<size; j++){ 115 if (w4[j]) { w1[j] += w4[j]; w3[j]++;} 116 } 117 } 118 119 nrqs = 0; /* no of outgoing messages */ 120 msz = 0; /* total mesg length (for all proc */ 121 w1[rank] = 0; /* no mesg sent to intself */ 122 w3[rank] = 0; 123 for (i=0; i<size; i++) { 124 if (w1[i]) {w2[i] = 1; nrqs++;} /* there exists a message to proc i */ 125 } 126 /* pa - is list of processors to communicate with */ 127 ierr = PetscMalloc((nrqs+1)*sizeof(PetscInt),&pa);CHKERRQ(ierr); 128 for (i=0,j=0; i<size; i++) { 129 if (w1[i]) {pa[j] = i; j++;} 130 } 131 132 /* Each message would have a header = 1 + 2*(no of IS) + data */ 133 for (i=0; i<nrqs; i++) { 134 j = pa[i]; 135 w1[j] += w2[j] + 2*w3[j]; 136 msz += w1[j]; 137 } 138 139 /* Determine the number of messages to expect, their lengths, from from-ids */ 140 ierr = PetscGatherNumberOfMessages(comm,w2,w1,&nrqr);CHKERRQ(ierr); 141 ierr = PetscGatherMessageLengths(comm,nrqs,nrqr,w1,&onodes1,&olengths1);CHKERRQ(ierr); 142 143 /* Now post the Irecvs corresponding to these messages */ 144 ierr = PetscPostIrecvInt(comm,tag1,nrqr,onodes1,olengths1,&rbuf,&r_waits1);CHKERRQ(ierr); 145 146 /* Allocate Memory for outgoing messages */ 147 len = 2*size*sizeof(PetscInt*) + (size+msz)*sizeof(PetscInt); 148 ierr = PetscMalloc(len,&outdat);CHKERRQ(ierr); 149 ptr = outdat + size; /* Pointers to the data in outgoing buffers */ 150 ierr = PetscMemzero(outdat,2*size*sizeof(PetscInt*));CHKERRQ(ierr); 151 tmp = (PetscInt*)(outdat + 2*size); 152 ctr = tmp + msz; 153 154 { 155 PetscInt *iptr = tmp,ict = 0; 156 for (i=0; i<nrqs; i++) { 157 j = pa[i]; 158 iptr += ict; 159 outdat[j] = iptr; 160 ict = w1[j]; 161 } 162 } 163 164 /* Form the outgoing messages */ 165 /*plug in the headers*/ 166 for (i=0; i<nrqs; i++) { 167 j = pa[i]; 168 outdat[j][0] = 0; 169 ierr = PetscMemzero(outdat[j]+1,2*w3[j]*sizeof(PetscInt));CHKERRQ(ierr); 170 ptr[j] = outdat[j] + 2*w3[j] + 1; 171 } 172 173 /* Memory for doing local proc's work*/ 174 { 175 PetscInt *d_p; 176 char *t_p; 177 178 len = (imax)*(sizeof(PetscBT) + sizeof(PetscInt*)+ sizeof(PetscInt)) + 179 (m)*imax*sizeof(PetscInt) + (m/PETSC_BITS_PER_BYTE+1)*imax*sizeof(char) + 1; 180 ierr = PetscMalloc(len,&table);CHKERRQ(ierr); 181 ierr = PetscMemzero(table,len);CHKERRQ(ierr); 182 data = (PetscInt **)(table + imax); 183 isz = (PetscInt *)(data + imax); 184 d_p = (PetscInt *)(isz + imax); 185 t_p = (char *)(d_p + m*imax); 186 for (i=0; i<imax; i++) { 187 table[i] = t_p + (m/PETSC_BITS_PER_BYTE+1)*i; 188 data[i] = d_p + (m)*i; 189 } 190 } 191 192 /* Parse the IS and update local tables and the outgoing buf with the data*/ 193 { 194 PetscInt n_i,*data_i,isz_i,*outdat_j,ctr_j; 195 PetscBT table_i; 196 197 for (i=0; i<imax; i++) { 198 ierr = PetscMemzero(ctr,size*sizeof(PetscInt));CHKERRQ(ierr); 199 n_i = n[i]; 200 table_i = table[i]; 201 idx_i = idx[i]; 202 data_i = data[i]; 203 isz_i = isz[i]; 204 for (j=0; j<n_i; j++) { /* parse the indices of each IS */ 205 row = idx_i[j]; 206 proc = rtable[row]; 207 if (proc != rank) { /* copy to the outgoing buffer */ 208 ctr[proc]++; 209 *ptr[proc] = row; 210 ptr[proc]++; 211 } else { /* Update the local table */ 212 if (!PetscBTLookupSet(table_i,row)) { data_i[isz_i++] = row;} 213 } 214 } 215 /* Update the headers for the current IS */ 216 for (j=0; j<size; j++) { /* Can Optimise this loop by using pa[] */ 217 if ((ctr_j = ctr[j])) { 218 outdat_j = outdat[j]; 219 k = ++outdat_j[0]; 220 outdat_j[2*k] = ctr_j; 221 outdat_j[2*k-1] = i; 222 } 223 } 224 isz[i] = isz_i; 225 } 226 } 227 228 229 230 /* Now post the sends */ 231 ierr = PetscMalloc((nrqs+1)*sizeof(MPI_Request),&s_waits1);CHKERRQ(ierr); 232 for (i=0; i<nrqs; ++i) { 233 j = pa[i]; 234 ierr = MPI_Isend(outdat[j],w1[j],MPIU_INT,j,tag1,comm,s_waits1+i);CHKERRQ(ierr); 235 } 236 237 /* No longer need the original indices*/ 238 for (i=0; i<imax; ++i) { 239 ierr = ISRestoreIndices(is[i],idx+i);CHKERRQ(ierr); 240 } 241 ierr = PetscFree(idx);CHKERRQ(ierr); 242 243 for (i=0; i<imax; ++i) { 244 ierr = ISDestroy(is[i]);CHKERRQ(ierr); 245 } 246 247 /* Do Local work*/ 248 ierr = MatIncreaseOverlap_MPIAIJ_Local(C,imax,table,isz,data);CHKERRQ(ierr); 249 250 /* Receive messages*/ 251 ierr = PetscMalloc((nrqr+1)*sizeof(MPI_Status),&recv_status);CHKERRQ(ierr); 252 ierr = MPI_Waitall(nrqr,r_waits1,recv_status);CHKERRQ(ierr); 253 254 ierr = PetscMalloc((nrqs+1)*sizeof(MPI_Status),&s_status);CHKERRQ(ierr); 255 ierr = MPI_Waitall(nrqs,s_waits1,s_status);CHKERRQ(ierr); 256 257 /* Phase 1 sends are complete - deallocate buffers */ 258 ierr = PetscFree(outdat);CHKERRQ(ierr); 259 ierr = PetscFree(w1);CHKERRQ(ierr); 260 261 ierr = PetscMalloc((nrqr+1)*sizeof(PetscInt*),&xdata);CHKERRQ(ierr); 262 ierr = PetscMalloc((nrqr+1)*sizeof(PetscInt),&isz1);CHKERRQ(ierr); 263 ierr = MatIncreaseOverlap_MPIAIJ_Receive(C,nrqr,rbuf,xdata,isz1);CHKERRQ(ierr); 264 ierr = PetscFree(rbuf);CHKERRQ(ierr); 265 266 267 /* Send the data back*/ 268 /* Do a global reduction to know the buffer space req for incoming messages*/ 269 { 270 PetscMPIInt *rw1; 271 272 ierr = PetscMalloc(size*sizeof(PetscMPIInt),&rw1);CHKERRQ(ierr); 273 ierr = PetscMemzero(rw1,size*sizeof(PetscMPIInt));CHKERRQ(ierr); 274 275 for (i=0; i<nrqr; ++i) { 276 proc = recv_status[i].MPI_SOURCE; 277 if (proc != onodes1[i]) SETERRQ(PETSC_ERR_PLIB,"MPI_SOURCE mismatch"); 278 rw1[proc] = isz1[i]; 279 } 280 ierr = PetscFree(onodes1);CHKERRQ(ierr); 281 ierr = PetscFree(olengths1);CHKERRQ(ierr); 282 283 /* Determine the number of messages to expect, their lengths, from from-ids */ 284 ierr = PetscGatherMessageLengths(comm,nrqr,nrqs,rw1,&onodes2,&olengths2);CHKERRQ(ierr); 285 PetscFree(rw1); 286 } 287 /* Now post the Irecvs corresponding to these messages */ 288 ierr = PetscPostIrecvInt(comm,tag2,nrqs,onodes2,olengths2,&rbuf2,&r_waits2);CHKERRQ(ierr); 289 290 /* Now post the sends */ 291 ierr = PetscMalloc((nrqr+1)*sizeof(MPI_Request),&s_waits2);CHKERRQ(ierr); 292 for (i=0; i<nrqr; ++i) { 293 j = recv_status[i].MPI_SOURCE; 294 ierr = MPI_Isend(xdata[i],isz1[i],MPIU_INT,j,tag2,comm,s_waits2+i);CHKERRQ(ierr); 295 } 296 297 /* receive work done on other processors*/ 298 { 299 PetscInt is_no,ct1,max,*rbuf2_i,isz_i,*data_i,jmax; 300 PetscMPIInt idex; 301 PetscBT table_i; 302 MPI_Status *status2; 303 304 ierr = PetscMalloc((PetscMax(nrqr,nrqs)+1)*sizeof(MPI_Status),&status2);CHKERRQ(ierr); 305 for (i=0; i<nrqs; ++i) { 306 ierr = MPI_Waitany(nrqs,r_waits2,&idex,status2+i);CHKERRQ(ierr); 307 /* Process the message*/ 308 rbuf2_i = rbuf2[idex]; 309 ct1 = 2*rbuf2_i[0]+1; 310 jmax = rbuf2[idex][0]; 311 for (j=1; j<=jmax; j++) { 312 max = rbuf2_i[2*j]; 313 is_no = rbuf2_i[2*j-1]; 314 isz_i = isz[is_no]; 315 data_i = data[is_no]; 316 table_i = table[is_no]; 317 for (k=0; k<max; k++,ct1++) { 318 row = rbuf2_i[ct1]; 319 if (!PetscBTLookupSet(table_i,row)) { data_i[isz_i++] = row;} 320 } 321 isz[is_no] = isz_i; 322 } 323 } 324 325 ierr = MPI_Waitall(nrqr,s_waits2,status2);CHKERRQ(ierr); 326 ierr = PetscFree(status2);CHKERRQ(ierr); 327 } 328 329 for (i=0; i<imax; ++i) { 330 ierr = ISCreateGeneral(PETSC_COMM_SELF,isz[i],data[i],is+i);CHKERRQ(ierr); 331 } 332 333 ierr = PetscFree(onodes2);CHKERRQ(ierr); 334 ierr = PetscFree(olengths2);CHKERRQ(ierr); 335 336 ierr = PetscFree(pa);CHKERRQ(ierr); 337 ierr = PetscFree(rbuf2);CHKERRQ(ierr); 338 ierr = PetscFree(s_waits1);CHKERRQ(ierr); 339 ierr = PetscFree(r_waits1);CHKERRQ(ierr); 340 ierr = PetscFree(s_waits2);CHKERRQ(ierr); 341 ierr = PetscFree(r_waits2);CHKERRQ(ierr); 342 ierr = PetscFree(table);CHKERRQ(ierr); 343 ierr = PetscFree(s_status);CHKERRQ(ierr); 344 ierr = PetscFree(recv_status);CHKERRQ(ierr); 345 ierr = PetscFree(xdata[0]);CHKERRQ(ierr); 346 ierr = PetscFree(xdata);CHKERRQ(ierr); 347 ierr = PetscFree(isz1);CHKERRQ(ierr); 348 PetscFunctionReturn(0); 349 } 350 351 #undef __FUNCT__ 352 #define __FUNCT__ "MatIncreaseOverlap_MPIAIJ_Local" 353 /* 354 MatIncreaseOverlap_MPIAIJ_Local - Called by MatincreaseOverlap, to do 355 the work on the local processor. 356 357 Inputs: 358 C - MAT_MPIAIJ; 359 imax - total no of index sets processed at a time; 360 table - an array of char - size = m bits. 361 362 Output: 363 isz - array containing the count of the solution elements correspondign 364 to each index set; 365 data - pointer to the solutions 366 */ 367 static PetscErrorCode MatIncreaseOverlap_MPIAIJ_Local(Mat C,PetscInt imax,PetscBT *table,PetscInt *isz,PetscInt **data) 368 { 369 Mat_MPIAIJ *c = (Mat_MPIAIJ*)C->data; 370 Mat A = c->A,B = c->B; 371 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*b = (Mat_SeqAIJ*)B->data; 372 PetscInt start,end,val,max,rstart,cstart,*ai,*aj; 373 PetscInt *bi,*bj,*garray,i,j,k,row,*data_i,isz_i; 374 PetscBT table_i; 375 376 PetscFunctionBegin; 377 rstart = c->rstart; 378 cstart = c->cstart; 379 ai = a->i; 380 aj = a->j; 381 bi = b->i; 382 bj = b->j; 383 garray = c->garray; 384 385 386 for (i=0; i<imax; i++) { 387 data_i = data[i]; 388 table_i = table[i]; 389 isz_i = isz[i]; 390 for (j=0,max=isz[i]; j<max; j++) { 391 row = data_i[j] - rstart; 392 start = ai[row]; 393 end = ai[row+1]; 394 for (k=start; k<end; k++) { /* Amat */ 395 val = aj[k] + cstart; 396 if (!PetscBTLookupSet(table_i,val)) { data_i[isz_i++] = val;} 397 } 398 start = bi[row]; 399 end = bi[row+1]; 400 for (k=start; k<end; k++) { /* Bmat */ 401 val = garray[bj[k]]; 402 if (!PetscBTLookupSet(table_i,val)) { data_i[isz_i++] = val;} 403 } 404 } 405 isz[i] = isz_i; 406 } 407 PetscFunctionReturn(0); 408 } 409 410 #undef __FUNCT__ 411 #define __FUNCT__ "MatIncreaseOverlap_MPIAIJ_Receive" 412 /* 413 MatIncreaseOverlap_MPIAIJ_Receive - Process the recieved messages, 414 and return the output 415 416 Input: 417 C - the matrix 418 nrqr - no of messages being processed. 419 rbuf - an array of pointers to the recieved requests 420 421 Output: 422 xdata - array of messages to be sent back 423 isz1 - size of each message 424 425 For better efficiency perhaps we should malloc seperately each xdata[i], 426 then if a remalloc is required we need only copy the data for that one row 427 rather then all previous rows as it is now where a single large chunck of 428 memory is used. 429 430 */ 431 static PetscErrorCode MatIncreaseOverlap_MPIAIJ_Receive(Mat C,PetscInt nrqr,PetscInt **rbuf,PetscInt **xdata,PetscInt * isz1) 432 { 433 Mat_MPIAIJ *c = (Mat_MPIAIJ*)C->data; 434 Mat A = c->A,B = c->B; 435 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*b = (Mat_SeqAIJ*)B->data; 436 PetscErrorCode ierr; 437 PetscMPIInt rank; 438 PetscInt rstart,cstart,*ai,*aj,*bi,*bj,*garray,i,j,k; 439 PetscInt row,total_sz,ct,ct1,ct2,ct3,mem_estimate,oct2,l,start,end; 440 PetscInt val,max1,max2,m,no_malloc =0,*tmp,new_estimate,ctr; 441 PetscInt *rbuf_i,kmax,rbuf_0; 442 PetscBT xtable; 443 444 PetscFunctionBegin; 445 rank = c->rank; 446 m = C->M; 447 rstart = c->rstart; 448 cstart = c->cstart; 449 ai = a->i; 450 aj = a->j; 451 bi = b->i; 452 bj = b->j; 453 garray = c->garray; 454 455 456 for (i=0,ct=0,total_sz=0; i<nrqr; ++i) { 457 rbuf_i = rbuf[i]; 458 rbuf_0 = rbuf_i[0]; 459 ct += rbuf_0; 460 for (j=1; j<=rbuf_0; j++) { total_sz += rbuf_i[2*j]; } 461 } 462 463 if (C->m) max1 = ct*(a->nz + b->nz)/C->m; 464 else max1 = 1; 465 mem_estimate = 3*((total_sz > max1 ? total_sz : max1)+1); 466 ierr = PetscMalloc(mem_estimate*sizeof(PetscInt),&xdata[0]);CHKERRQ(ierr); 467 ++no_malloc; 468 ierr = PetscBTCreate(m,xtable);CHKERRQ(ierr); 469 ierr = PetscMemzero(isz1,nrqr*sizeof(PetscInt));CHKERRQ(ierr); 470 471 ct3 = 0; 472 for (i=0; i<nrqr; i++) { /* for easch mesg from proc i */ 473 rbuf_i = rbuf[i]; 474 rbuf_0 = rbuf_i[0]; 475 ct1 = 2*rbuf_0+1; 476 ct2 = ct1; 477 ct3 += ct1; 478 for (j=1; j<=rbuf_0; j++) { /* for each IS from proc i*/ 479 ierr = PetscBTMemzero(m,xtable);CHKERRQ(ierr); 480 oct2 = ct2; 481 kmax = rbuf_i[2*j]; 482 for (k=0; k<kmax; k++,ct1++) { 483 row = rbuf_i[ct1]; 484 if (!PetscBTLookupSet(xtable,row)) { 485 if (!(ct3 < mem_estimate)) { 486 new_estimate = (PetscInt)(1.5*mem_estimate)+1; 487 ierr = PetscMalloc(new_estimate*sizeof(PetscInt),&tmp);CHKERRQ(ierr); 488 ierr = PetscMemcpy(tmp,xdata[0],mem_estimate*sizeof(PetscInt));CHKERRQ(ierr); 489 ierr = PetscFree(xdata[0]);CHKERRQ(ierr); 490 xdata[0] = tmp; 491 mem_estimate = new_estimate; ++no_malloc; 492 for (ctr=1; ctr<=i; ctr++) { xdata[ctr] = xdata[ctr-1] + isz1[ctr-1];} 493 } 494 xdata[i][ct2++] = row; 495 ct3++; 496 } 497 } 498 for (k=oct2,max2=ct2; k<max2; k++) { 499 row = xdata[i][k] - rstart; 500 start = ai[row]; 501 end = ai[row+1]; 502 for (l=start; l<end; l++) { 503 val = aj[l] + cstart; 504 if (!PetscBTLookupSet(xtable,val)) { 505 if (!(ct3 < mem_estimate)) { 506 new_estimate = (PetscInt)(1.5*mem_estimate)+1; 507 ierr = PetscMalloc(new_estimate*sizeof(PetscInt),&tmp);CHKERRQ(ierr); 508 ierr = PetscMemcpy(tmp,xdata[0],mem_estimate*sizeof(PetscInt));CHKERRQ(ierr); 509 ierr = PetscFree(xdata[0]);CHKERRQ(ierr); 510 xdata[0] = tmp; 511 mem_estimate = new_estimate; ++no_malloc; 512 for (ctr=1; ctr<=i; ctr++) { xdata[ctr] = xdata[ctr-1] + isz1[ctr-1];} 513 } 514 xdata[i][ct2++] = val; 515 ct3++; 516 } 517 } 518 start = bi[row]; 519 end = bi[row+1]; 520 for (l=start; l<end; l++) { 521 val = garray[bj[l]]; 522 if (!PetscBTLookupSet(xtable,val)) { 523 if (!(ct3 < mem_estimate)) { 524 new_estimate = (PetscInt)(1.5*mem_estimate)+1; 525 ierr = PetscMalloc(new_estimate*sizeof(PetscInt),&tmp);CHKERRQ(ierr); 526 ierr = PetscMemcpy(tmp,xdata[0],mem_estimate*sizeof(PetscInt));CHKERRQ(ierr); 527 ierr = PetscFree(xdata[0]);CHKERRQ(ierr); 528 xdata[0] = tmp; 529 mem_estimate = new_estimate; ++no_malloc; 530 for (ctr =1; ctr <=i; ctr++) { xdata[ctr] = xdata[ctr-1] + isz1[ctr-1];} 531 } 532 xdata[i][ct2++] = val; 533 ct3++; 534 } 535 } 536 } 537 /* Update the header*/ 538 xdata[i][2*j] = ct2 - oct2; /* Undo the vector isz1 and use only a var*/ 539 xdata[i][2*j-1] = rbuf_i[2*j-1]; 540 } 541 xdata[i][0] = rbuf_0; 542 xdata[i+1] = xdata[i] + ct2; 543 isz1[i] = ct2; /* size of each message */ 544 } 545 ierr = PetscBTDestroy(xtable);CHKERRQ(ierr); 546 PetscLogInfo(0,"MatIncreaseOverlap_MPIAIJ:[%d] Allocated %D bytes, required %D bytes, no of mallocs = %D\n",rank,mem_estimate, ct3,no_malloc); 547 PetscFunctionReturn(0); 548 } 549 /* -------------------------------------------------------------------------*/ 550 EXTERN PetscErrorCode MatGetSubMatrices_MPIAIJ_Local(Mat,PetscInt,const IS[],const IS[],MatReuse,Mat*); 551 EXTERN PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat,MatAssemblyType); 552 /* 553 Every processor gets the entire matrix 554 */ 555 #undef __FUNCT__ 556 #define __FUNCT__ "MatGetSubMatrix_MPIAIJ_All" 557 PetscErrorCode MatGetSubMatrix_MPIAIJ_All(Mat A,MatReuse scall,Mat *Bin[]) 558 { 559 Mat B; 560 Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data; 561 Mat_SeqAIJ *b,*ad = (Mat_SeqAIJ*)a->A->data,*bd = (Mat_SeqAIJ*)a->B->data; 562 PetscErrorCode ierr; 563 PetscMPIInt size,rank,*recvcounts = 0,*displs = 0; 564 PetscInt sendcount,i,*rstarts = a->rowners,n,cnt,j; 565 PetscInt m,*b_sendj,*garray = a->garray,*lens,*jsendbuf,*a_jsendbuf,*b_jsendbuf; 566 PetscScalar *sendbuf,*recvbuf,*a_sendbuf,*b_sendbuf; 567 568 PetscFunctionBegin; 569 ierr = MPI_Comm_size(A->comm,&size);CHKERRQ(ierr); 570 ierr = MPI_Comm_rank(A->comm,&rank);CHKERRQ(ierr); 571 572 if (scall == MAT_INITIAL_MATRIX) { 573 /* ---------------------------------------------------------------- 574 Tell every processor the number of nonzeros per row 575 */ 576 ierr = PetscMalloc(A->M*sizeof(PetscInt),&lens);CHKERRQ(ierr); 577 for (i=a->rstart; i<a->rend; i++) { 578 lens[i] = ad->i[i-a->rstart+1] - ad->i[i-a->rstart] + bd->i[i-a->rstart+1] - bd->i[i-a->rstart]; 579 } 580 sendcount = a->rend - a->rstart; 581 ierr = PetscMalloc(2*size*sizeof(PetscMPIInt),&recvcounts);CHKERRQ(ierr); 582 displs = recvcounts + size; 583 for (i=0; i<size; i++) { 584 recvcounts[i] = a->rowners[i+1] - a->rowners[i]; 585 displs[i] = a->rowners[i]; 586 } 587 ierr = MPI_Allgatherv(lens+a->rstart,sendcount,MPIU_INT,lens,recvcounts,displs,MPIU_INT,A->comm);CHKERRQ(ierr); 588 589 /* --------------------------------------------------------------- 590 Create the sequential matrix of the same type as the local block diagonal 591 */ 592 ierr = MatCreate(PETSC_COMM_SELF,A->M,A->N,PETSC_DETERMINE,PETSC_DETERMINE,&B);CHKERRQ(ierr); 593 ierr = MatSetType(B,a->A->type_name);CHKERRQ(ierr); 594 ierr = MatSeqAIJSetPreallocation(B,0,lens);CHKERRQ(ierr); 595 ierr = PetscMalloc(sizeof(Mat),Bin);CHKERRQ(ierr); 596 **Bin = B; 597 b = (Mat_SeqAIJ *)B->data; 598 599 /*-------------------------------------------------------------------- 600 Copy my part of matrix column indices over 601 */ 602 sendcount = ad->nz + bd->nz; 603 jsendbuf = b->j + b->i[rstarts[rank]]; 604 a_jsendbuf = ad->j; 605 b_jsendbuf = bd->j; 606 n = a->rend - a->rstart; 607 cnt = 0; 608 for (i=0; i<n; i++) { 609 610 /* put in lower diagonal portion */ 611 m = bd->i[i+1] - bd->i[i]; 612 while (m > 0) { 613 /* is it above diagonal (in bd (compressed) numbering) */ 614 if (garray[*b_jsendbuf] > a->rstart + i) break; 615 jsendbuf[cnt++] = garray[*b_jsendbuf++]; 616 m--; 617 } 618 619 /* put in diagonal portion */ 620 for (j=ad->i[i]; j<ad->i[i+1]; j++) { 621 jsendbuf[cnt++] = a->rstart + *a_jsendbuf++; 622 } 623 624 /* put in upper diagonal portion */ 625 while (m-- > 0) { 626 jsendbuf[cnt++] = garray[*b_jsendbuf++]; 627 } 628 } 629 if (cnt != sendcount) SETERRQ2(PETSC_ERR_PLIB,"Corrupted PETSc matrix: nz given %D actual nz %D",sendcount,cnt); 630 631 /*-------------------------------------------------------------------- 632 Gather all column indices to all processors 633 */ 634 for (i=0; i<size; i++) { 635 recvcounts[i] = 0; 636 for (j=a->rowners[i]; j<a->rowners[i+1]; j++) { 637 recvcounts[i] += lens[j]; 638 } 639 } 640 displs[0] = 0; 641 for (i=1; i<size; i++) { 642 displs[i] = displs[i-1] + recvcounts[i-1]; 643 } 644 ierr = MPI_Allgatherv(jsendbuf,sendcount,MPIU_INT,b->j,recvcounts,displs,MPIU_INT,A->comm);CHKERRQ(ierr); 645 646 /*-------------------------------------------------------------------- 647 Assemble the matrix into useable form (note numerical values not yet set) 648 */ 649 /* set the b->ilen (length of each row) values */ 650 ierr = PetscMemcpy(b->ilen,lens,A->M*sizeof(PetscInt));CHKERRQ(ierr); 651 /* set the b->i indices */ 652 b->i[0] = 0; 653 for (i=1; i<=A->M; i++) { 654 b->i[i] = b->i[i-1] + lens[i-1]; 655 } 656 ierr = PetscFree(lens);CHKERRQ(ierr); 657 ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 658 ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 659 660 } else { 661 B = **Bin; 662 b = (Mat_SeqAIJ *)B->data; 663 } 664 665 /*-------------------------------------------------------------------- 666 Copy my part of matrix numerical values into the values location 667 */ 668 sendcount = ad->nz + bd->nz; 669 sendbuf = b->a + b->i[rstarts[rank]]; 670 a_sendbuf = ad->a; 671 b_sendbuf = bd->a; 672 b_sendj = bd->j; 673 n = a->rend - a->rstart; 674 cnt = 0; 675 for (i=0; i<n; i++) { 676 677 /* put in lower diagonal portion */ 678 m = bd->i[i+1] - bd->i[i]; 679 while (m > 0) { 680 /* is it above diagonal (in bd (compressed) numbering) */ 681 if (garray[*b_sendj] > a->rstart + i) break; 682 sendbuf[cnt++] = *b_sendbuf++; 683 m--; 684 b_sendj++; 685 } 686 687 /* put in diagonal portion */ 688 for (j=ad->i[i]; j<ad->i[i+1]; j++) { 689 sendbuf[cnt++] = *a_sendbuf++; 690 } 691 692 /* put in upper diagonal portion */ 693 while (m-- > 0) { 694 sendbuf[cnt++] = *b_sendbuf++; 695 b_sendj++; 696 } 697 } 698 if (cnt != sendcount) SETERRQ2(PETSC_ERR_PLIB,"Corrupted PETSc matrix: nz given %D actual nz %D",sendcount,cnt); 699 700 /* ----------------------------------------------------------------- 701 Gather all numerical values to all processors 702 */ 703 if (!recvcounts) { 704 ierr = PetscMalloc(2*size*sizeof(PetscInt),&recvcounts);CHKERRQ(ierr); 705 displs = recvcounts + size; 706 } 707 for (i=0; i<size; i++) { 708 recvcounts[i] = b->i[rstarts[i+1]] - b->i[rstarts[i]]; 709 } 710 displs[0] = 0; 711 for (i=1; i<size; i++) { 712 displs[i] = displs[i-1] + recvcounts[i-1]; 713 } 714 recvbuf = b->a; 715 ierr = MPI_Allgatherv(sendbuf,sendcount,MPIU_SCALAR,recvbuf,recvcounts,displs,MPIU_SCALAR,A->comm);CHKERRQ(ierr); 716 ierr = PetscFree(recvcounts);CHKERRQ(ierr); 717 if (A->symmetric){ 718 ierr = MatSetOption(B,MAT_SYMMETRIC);CHKERRQ(ierr); 719 } else if (A->hermitian) { 720 ierr = MatSetOption(B,MAT_HERMITIAN);CHKERRQ(ierr); 721 } else if (A->structurally_symmetric) { 722 ierr = MatSetOption(B,MAT_STRUCTURALLY_SYMMETRIC);CHKERRQ(ierr); 723 } 724 725 PetscFunctionReturn(0); 726 } 727 728 #undef __FUNCT__ 729 #define __FUNCT__ "MatGetSubMatrices_MPIAIJ" 730 PetscErrorCode MatGetSubMatrices_MPIAIJ(Mat C,PetscInt ismax,const IS isrow[],const IS iscol[],MatReuse scall,Mat *submat[]) 731 { 732 PetscErrorCode ierr; 733 PetscInt nmax,nstages_local,nstages,i,pos,max_no,nrow,ncol; 734 PetscTruth rowflag,colflag,wantallmatrix = PETSC_FALSE,twantallmatrix; 735 736 PetscFunctionBegin; 737 /* 738 Check for special case each processor gets entire matrix 739 */ 740 if (ismax == 1 && C->M == C->N) { 741 ierr = ISIdentity(*isrow,&rowflag);CHKERRQ(ierr); 742 ierr = ISIdentity(*iscol,&colflag);CHKERRQ(ierr); 743 ierr = ISGetLocalSize(*isrow,&nrow);CHKERRQ(ierr); 744 ierr = ISGetLocalSize(*iscol,&ncol);CHKERRQ(ierr); 745 if (rowflag && colflag && nrow == C->M && ncol == C->N) { 746 wantallmatrix = PETSC_TRUE; 747 ierr = PetscOptionsGetLogical(C->prefix,"-use_fast_submatrix",&wantallmatrix,PETSC_NULL);CHKERRQ(ierr); 748 } 749 } 750 ierr = MPI_Allreduce(&wantallmatrix,&twantallmatrix,1,MPI_INT,MPI_MIN,C->comm);CHKERRQ(ierr); 751 if (twantallmatrix) { 752 ierr = MatGetSubMatrix_MPIAIJ_All(C,scall,submat);CHKERRQ(ierr); 753 PetscFunctionReturn(0); 754 } 755 756 /* Allocate memory to hold all the submatrices */ 757 if (scall != MAT_REUSE_MATRIX) { 758 ierr = PetscMalloc((ismax+1)*sizeof(Mat),submat);CHKERRQ(ierr); 759 } 760 /* Determine the number of stages through which submatrices are done */ 761 nmax = 20*1000000 / (C->N * sizeof(PetscInt)); 762 if (!nmax) nmax = 1; 763 nstages_local = ismax/nmax + ((ismax % nmax)?1:0); 764 765 /* Make sure every processor loops through the nstages */ 766 ierr = MPI_Allreduce(&nstages_local,&nstages,1,MPIU_INT,MPI_MAX,C->comm);CHKERRQ(ierr); 767 768 for (i=0,pos=0; i<nstages; i++) { 769 if (pos+nmax <= ismax) max_no = nmax; 770 else if (pos == ismax) max_no = 0; 771 else max_no = ismax-pos; 772 ierr = MatGetSubMatrices_MPIAIJ_Local(C,max_no,isrow+pos,iscol+pos,scall,*submat+pos);CHKERRQ(ierr); 773 pos += max_no; 774 } 775 PetscFunctionReturn(0); 776 } 777 /* -------------------------------------------------------------------------*/ 778 #undef __FUNCT__ 779 #define __FUNCT__ "MatGetSubMatrices_MPIAIJ_Local" 780 PetscErrorCode MatGetSubMatrices_MPIAIJ_Local(Mat C,PetscInt ismax,const IS isrow[],const IS iscol[],MatReuse scall,Mat *submats) 781 { 782 Mat_MPIAIJ *c = (Mat_MPIAIJ*)C->data; 783 Mat A = c->A; 784 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*b = (Mat_SeqAIJ*)c->B->data,*mat; 785 PetscInt **irow,**icol,*nrow,*ncol,start; 786 PetscErrorCode ierr; 787 PetscMPIInt rank,size,tag0,tag1,tag2,tag3,*w1,*w2,*w3,*w4,nrqr; 788 PetscInt **sbuf1,**sbuf2,i,j,k,l,ct1,ct2,**rbuf1,row,proc; 789 PetscInt nrqs,msz,**ptr,*req_size,*ctr,*pa,*tmp,tcol; 790 PetscInt **rbuf3,*req_source,**sbuf_aj,**rbuf2,max1,max2,**rmap; 791 PetscInt **cmap,**lens,is_no,ncols,*cols,mat_i,*mat_j,tmp2,jmax,*irow_i; 792 PetscInt len,ctr_j,*sbuf1_j,*sbuf_aj_i,*rbuf1_i,kmax,*cmap_i,*lens_i; 793 PetscInt *rmap_i; 794 MPI_Request *s_waits1,*r_waits1,*s_waits2,*r_waits2,*r_waits3; 795 MPI_Request *r_waits4,*s_waits3,*s_waits4; 796 MPI_Status *r_status1,*r_status2,*s_status1,*s_status3,*s_status2; 797 MPI_Status *r_status3,*r_status4,*s_status4; 798 MPI_Comm comm; 799 PetscScalar **rbuf4,**sbuf_aa,*vals,*mat_a,*sbuf_aa_i; 800 PetscTruth sorted; 801 PetscMPIInt *onodes1,*olengths1; 802 PetscMPIInt idex,idex2,end; 803 804 PetscFunctionBegin; 805 comm = C->comm; 806 tag0 = C->tag; 807 size = c->size; 808 rank = c->rank; 809 810 /* Get some new tags to keep the communication clean */ 811 ierr = PetscObjectGetNewTag((PetscObject)C,&tag1);CHKERRQ(ierr); 812 ierr = PetscObjectGetNewTag((PetscObject)C,&tag2);CHKERRQ(ierr); 813 ierr = PetscObjectGetNewTag((PetscObject)C,&tag3);CHKERRQ(ierr); 814 815 /* Check if the col indices are sorted */ 816 for (i=0; i<ismax; i++) { 817 ierr = ISSorted(isrow[i],&sorted);CHKERRQ(ierr); 818 if (!sorted) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted"); 819 ierr = ISSorted(iscol[i],&sorted);CHKERRQ(ierr); 820 /* if (!sorted) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted"); */ 821 } 822 823 len = (2*ismax+1)*(sizeof(PetscInt*)+ sizeof(PetscInt)); 824 ierr = PetscMalloc(len,&irow);CHKERRQ(ierr); 825 icol = irow + ismax; 826 nrow = (PetscInt*)(icol + ismax); 827 ncol = nrow + ismax; 828 829 for (i=0; i<ismax; i++) { 830 ierr = ISGetIndices(isrow[i],&irow[i]);CHKERRQ(ierr); 831 ierr = ISGetIndices(iscol[i],&icol[i]);CHKERRQ(ierr); 832 ierr = ISGetLocalSize(isrow[i],&nrow[i]);CHKERRQ(ierr); 833 ierr = ISGetLocalSize(iscol[i],&ncol[i]);CHKERRQ(ierr); 834 } 835 836 /* evaluate communication - mesg to who, length of mesg, and buffer space 837 required. Based on this, buffers are allocated, and data copied into them*/ 838 ierr = PetscMalloc(size*4*sizeof(PetscMPIInt),&w1);CHKERRQ(ierr); /* mesg size */ 839 w2 = w1 + size; /* if w2[i] marked, then a message to proc i*/ 840 w3 = w2 + size; /* no of IS that needs to be sent to proc i */ 841 w4 = w3 + size; /* temp work space used in determining w1, w2, w3 */ 842 ierr = PetscMemzero(w1,size*3*sizeof(PetscMPIInt));CHKERRQ(ierr); /* initialize work vector*/ 843 for (i=0; i<ismax; i++) { 844 ierr = PetscMemzero(w4,size*sizeof(PetscMPIInt));CHKERRQ(ierr); /* initialize work vector*/ 845 jmax = nrow[i]; 846 irow_i = irow[i]; 847 for (l=0,j=0; j<jmax; j++) { 848 row = irow_i[j]; 849 while (row >= c->rowners[l+1]) l++; 850 proc = l; 851 w4[proc]++; 852 } 853 for (j=0; j<size; j++) { 854 if (w4[j]) { w1[j] += w4[j]; w3[j]++;} 855 } 856 } 857 858 nrqs = 0; /* no of outgoing messages */ 859 msz = 0; /* total mesg length (for all procs) */ 860 w1[rank] = 0; /* no mesg sent to self */ 861 w3[rank] = 0; 862 for (i=0; i<size; i++) { 863 if (w1[i]) { w2[i] = 1; nrqs++;} /* there exists a message to proc i */ 864 } 865 ierr = PetscMalloc((nrqs+1)*sizeof(PetscInt),&pa);CHKERRQ(ierr); /*(proc -array)*/ 866 for (i=0,j=0; i<size; i++) { 867 if (w1[i]) { pa[j] = i; j++; } 868 } 869 870 /* Each message would have a header = 1 + 2*(no of IS) + data */ 871 for (i=0; i<nrqs; i++) { 872 j = pa[i]; 873 w1[j] += w2[j] + 2* w3[j]; 874 msz += w1[j]; 875 } 876 877 /* Determine the number of messages to expect, their lengths, from from-ids */ 878 ierr = PetscGatherNumberOfMessages(comm,w2,w1,&nrqr);CHKERRQ(ierr); 879 ierr = PetscGatherMessageLengths(comm,nrqs,nrqr,w1,&onodes1,&olengths1);CHKERRQ(ierr); 880 881 /* Now post the Irecvs corresponding to these messages */ 882 ierr = PetscPostIrecvInt(comm,tag0,nrqr,onodes1,olengths1,&rbuf1,&r_waits1);CHKERRQ(ierr); 883 884 ierr = PetscFree(onodes1);CHKERRQ(ierr); 885 ierr = PetscFree(olengths1);CHKERRQ(ierr); 886 887 /* Allocate Memory for outgoing messages */ 888 len = 2*size*sizeof(PetscInt*) + 2*msz*sizeof(PetscInt) + size*sizeof(PetscInt); 889 ierr = PetscMalloc(len,&sbuf1);CHKERRQ(ierr); 890 ptr = sbuf1 + size; /* Pointers to the data in outgoing buffers */ 891 ierr = PetscMemzero(sbuf1,2*size*sizeof(PetscInt*));CHKERRQ(ierr); 892 /* allocate memory for outgoing data + buf to receive the first reply */ 893 tmp = (PetscInt*)(ptr + size); 894 ctr = tmp + 2*msz; 895 896 { 897 PetscInt *iptr = tmp,ict = 0; 898 for (i=0; i<nrqs; i++) { 899 j = pa[i]; 900 iptr += ict; 901 sbuf1[j] = iptr; 902 ict = w1[j]; 903 } 904 } 905 906 /* Form the outgoing messages */ 907 /* Initialize the header space */ 908 for (i=0; i<nrqs; i++) { 909 j = pa[i]; 910 sbuf1[j][0] = 0; 911 ierr = PetscMemzero(sbuf1[j]+1,2*w3[j]*sizeof(PetscInt));CHKERRQ(ierr); 912 ptr[j] = sbuf1[j] + 2*w3[j] + 1; 913 } 914 915 /* Parse the isrow and copy data into outbuf */ 916 for (i=0; i<ismax; i++) { 917 ierr = PetscMemzero(ctr,size*sizeof(PetscInt));CHKERRQ(ierr); 918 irow_i = irow[i]; 919 jmax = nrow[i]; 920 for (l=0,j=0; j<jmax; j++) { /* parse the indices of each IS */ 921 row = irow_i[j]; 922 while (row >= c->rowners[l+1]) l++; 923 proc = l; 924 if (proc != rank) { /* copy to the outgoing buf*/ 925 ctr[proc]++; 926 *ptr[proc] = row; 927 ptr[proc]++; 928 } 929 } 930 /* Update the headers for the current IS */ 931 for (j=0; j<size; j++) { /* Can Optimise this loop too */ 932 if ((ctr_j = ctr[j])) { 933 sbuf1_j = sbuf1[j]; 934 k = ++sbuf1_j[0]; 935 sbuf1_j[2*k] = ctr_j; 936 sbuf1_j[2*k-1] = i; 937 } 938 } 939 } 940 941 /* Now post the sends */ 942 ierr = PetscMalloc((nrqs+1)*sizeof(MPI_Request),&s_waits1);CHKERRQ(ierr); 943 for (i=0; i<nrqs; ++i) { 944 j = pa[i]; 945 ierr = MPI_Isend(sbuf1[j],w1[j],MPIU_INT,j,tag0,comm,s_waits1+i);CHKERRQ(ierr); 946 } 947 948 /* Post Receives to capture the buffer size */ 949 ierr = PetscMalloc((nrqs+1)*sizeof(MPI_Request),&r_waits2);CHKERRQ(ierr); 950 ierr = PetscMalloc((nrqs+1)*sizeof(PetscInt*),&rbuf2);CHKERRQ(ierr); 951 rbuf2[0] = tmp + msz; 952 for (i=1; i<nrqs; ++i) { 953 rbuf2[i] = rbuf2[i-1]+w1[pa[i-1]]; 954 } 955 for (i=0; i<nrqs; ++i) { 956 j = pa[i]; 957 ierr = MPI_Irecv(rbuf2[i],w1[j],MPIU_INT,j,tag1,comm,r_waits2+i);CHKERRQ(ierr); 958 } 959 960 /* Send to other procs the buf size they should allocate */ 961 962 963 /* Receive messages*/ 964 ierr = PetscMalloc((nrqr+1)*sizeof(MPI_Request),&s_waits2);CHKERRQ(ierr); 965 ierr = PetscMalloc((nrqr+1)*sizeof(MPI_Status),&r_status1);CHKERRQ(ierr); 966 len = 2*nrqr*sizeof(PetscInt) + (nrqr+1)*sizeof(PetscInt*); 967 ierr = PetscMalloc(len,&sbuf2);CHKERRQ(ierr); 968 req_size = (PetscInt*)(sbuf2 + nrqr); 969 req_source = req_size + nrqr; 970 971 { 972 Mat_SeqAIJ *sA = (Mat_SeqAIJ*)c->A->data,*sB = (Mat_SeqAIJ*)c->B->data; 973 PetscInt *sAi = sA->i,*sBi = sB->i,id,rstart = c->rstart; 974 PetscInt *sbuf2_i; 975 976 for (i=0; i<nrqr; ++i) { 977 ierr = MPI_Waitany(nrqr,r_waits1,&idex,r_status1+i);CHKERRQ(ierr); 978 req_size[idex] = 0; 979 rbuf1_i = rbuf1[idex]; 980 start = 2*rbuf1_i[0] + 1; 981 ierr = MPI_Get_count(r_status1+i,MPIU_INT,&end);CHKERRQ(ierr); 982 ierr = PetscMalloc((end+1)*sizeof(PetscInt),&sbuf2[idex]);CHKERRQ(ierr); 983 sbuf2_i = sbuf2[idex]; 984 for (j=start; j<end; j++) { 985 id = rbuf1_i[j] - rstart; 986 ncols = sAi[id+1] - sAi[id] + sBi[id+1] - sBi[id]; 987 sbuf2_i[j] = ncols; 988 req_size[idex] += ncols; 989 } 990 req_source[idex] = r_status1[i].MPI_SOURCE; 991 /* form the header */ 992 sbuf2_i[0] = req_size[idex]; 993 for (j=1; j<start; j++) { sbuf2_i[j] = rbuf1_i[j]; } 994 ierr = MPI_Isend(sbuf2_i,end,MPIU_INT,req_source[idex],tag1,comm,s_waits2+i);CHKERRQ(ierr); 995 } 996 } 997 ierr = PetscFree(r_status1);CHKERRQ(ierr); 998 ierr = PetscFree(r_waits1);CHKERRQ(ierr); 999 1000 /* recv buffer sizes */ 1001 /* Receive messages*/ 1002 1003 ierr = PetscMalloc((nrqs+1)*sizeof(PetscInt*),&rbuf3);CHKERRQ(ierr); 1004 ierr = PetscMalloc((nrqs+1)*sizeof(PetscScalar*),&rbuf4);CHKERRQ(ierr); 1005 ierr = PetscMalloc((nrqs+1)*sizeof(MPI_Request),&r_waits3);CHKERRQ(ierr); 1006 ierr = PetscMalloc((nrqs+1)*sizeof(MPI_Request),&r_waits4);CHKERRQ(ierr); 1007 ierr = PetscMalloc((nrqs+1)*sizeof(MPI_Status),&r_status2);CHKERRQ(ierr); 1008 1009 for (i=0; i<nrqs; ++i) { 1010 ierr = MPI_Waitany(nrqs,r_waits2,&idex,r_status2+i);CHKERRQ(ierr); 1011 ierr = PetscMalloc((rbuf2[idex][0]+1)*sizeof(PetscInt),&rbuf3[idex]);CHKERRQ(ierr); 1012 ierr = PetscMalloc((rbuf2[idex][0]+1)*sizeof(PetscScalar),&rbuf4[idex]);CHKERRQ(ierr); 1013 ierr = MPI_Irecv(rbuf3[idex],rbuf2[idex][0],MPIU_INT,r_status2[i].MPI_SOURCE,tag2,comm,r_waits3+idex);CHKERRQ(ierr); 1014 ierr = MPI_Irecv(rbuf4[idex],rbuf2[idex][0],MPIU_SCALAR,r_status2[i].MPI_SOURCE,tag3,comm,r_waits4+idex);CHKERRQ(ierr); 1015 } 1016 ierr = PetscFree(r_status2);CHKERRQ(ierr); 1017 ierr = PetscFree(r_waits2);CHKERRQ(ierr); 1018 1019 /* Wait on sends1 and sends2 */ 1020 ierr = PetscMalloc((nrqs+1)*sizeof(MPI_Status),&s_status1);CHKERRQ(ierr); 1021 ierr = PetscMalloc((nrqr+1)*sizeof(MPI_Status),&s_status2);CHKERRQ(ierr); 1022 1023 ierr = MPI_Waitall(nrqs,s_waits1,s_status1);CHKERRQ(ierr); 1024 ierr = MPI_Waitall(nrqr,s_waits2,s_status2);CHKERRQ(ierr); 1025 ierr = PetscFree(s_status1);CHKERRQ(ierr); 1026 ierr = PetscFree(s_status2);CHKERRQ(ierr); 1027 ierr = PetscFree(s_waits1);CHKERRQ(ierr); 1028 ierr = PetscFree(s_waits2);CHKERRQ(ierr); 1029 1030 /* Now allocate buffers for a->j, and send them off */ 1031 ierr = PetscMalloc((nrqr+1)*sizeof(PetscInt*),&sbuf_aj);CHKERRQ(ierr); 1032 for (i=0,j=0; i<nrqr; i++) j += req_size[i]; 1033 ierr = PetscMalloc((j+1)*sizeof(PetscInt),&sbuf_aj[0]);CHKERRQ(ierr); 1034 for (i=1; i<nrqr; i++) sbuf_aj[i] = sbuf_aj[i-1] + req_size[i-1]; 1035 1036 ierr = PetscMalloc((nrqr+1)*sizeof(MPI_Request),&s_waits3);CHKERRQ(ierr); 1037 { 1038 PetscInt nzA,nzB,*a_i = a->i,*b_i = b->i,imark; 1039 PetscInt *cworkA,*cworkB,cstart = c->cstart,rstart = c->rstart,*bmap = c->garray; 1040 PetscInt *a_j = a->j,*b_j = b->j,ctmp; 1041 1042 for (i=0; i<nrqr; i++) { 1043 rbuf1_i = rbuf1[i]; 1044 sbuf_aj_i = sbuf_aj[i]; 1045 ct1 = 2*rbuf1_i[0] + 1; 1046 ct2 = 0; 1047 for (j=1,max1=rbuf1_i[0]; j<=max1; j++) { 1048 kmax = rbuf1[i][2*j]; 1049 for (k=0; k<kmax; k++,ct1++) { 1050 row = rbuf1_i[ct1] - rstart; 1051 nzA = a_i[row+1] - a_i[row]; nzB = b_i[row+1] - b_i[row]; 1052 ncols = nzA + nzB; 1053 cworkA = a_j + a_i[row]; cworkB = b_j + b_i[row]; 1054 1055 /* load the column indices for this row into cols*/ 1056 cols = sbuf_aj_i + ct2; 1057 1058 for (l=0; l<nzB; l++) { 1059 if ((ctmp = bmap[cworkB[l]]) < cstart) cols[l] = ctmp; 1060 else break; 1061 } 1062 imark = l; 1063 for (l=0; l<nzA; l++) cols[imark+l] = cstart + cworkA[l]; 1064 for (l=imark; l<nzB; l++) cols[nzA+l] = bmap[cworkB[l]]; 1065 1066 ct2 += ncols; 1067 } 1068 } 1069 ierr = MPI_Isend(sbuf_aj_i,req_size[i],MPIU_INT,req_source[i],tag2,comm,s_waits3+i);CHKERRQ(ierr); 1070 } 1071 } 1072 ierr = PetscMalloc((nrqs+1)*sizeof(MPI_Status),&r_status3);CHKERRQ(ierr); 1073 ierr = PetscMalloc((nrqr+1)*sizeof(MPI_Status),&s_status3);CHKERRQ(ierr); 1074 1075 /* Allocate buffers for a->a, and send them off */ 1076 ierr = PetscMalloc((nrqr+1)*sizeof(PetscScalar*),&sbuf_aa);CHKERRQ(ierr); 1077 for (i=0,j=0; i<nrqr; i++) j += req_size[i]; 1078 ierr = PetscMalloc((j+1)*sizeof(PetscScalar),&sbuf_aa[0]);CHKERRQ(ierr); 1079 for (i=1; i<nrqr; i++) sbuf_aa[i] = sbuf_aa[i-1] + req_size[i-1]; 1080 1081 ierr = PetscMalloc((nrqr+1)*sizeof(MPI_Request),&s_waits4);CHKERRQ(ierr); 1082 { 1083 PetscInt nzA,nzB,*a_i = a->i,*b_i = b->i, *cworkB,imark; 1084 PetscInt cstart = c->cstart,rstart = c->rstart,*bmap = c->garray; 1085 PetscInt *b_j = b->j; 1086 PetscScalar *vworkA,*vworkB,*a_a = a->a,*b_a = b->a; 1087 1088 for (i=0; i<nrqr; i++) { 1089 rbuf1_i = rbuf1[i]; 1090 sbuf_aa_i = sbuf_aa[i]; 1091 ct1 = 2*rbuf1_i[0]+1; 1092 ct2 = 0; 1093 for (j=1,max1=rbuf1_i[0]; j<=max1; j++) { 1094 kmax = rbuf1_i[2*j]; 1095 for (k=0; k<kmax; k++,ct1++) { 1096 row = rbuf1_i[ct1] - rstart; 1097 nzA = a_i[row+1] - a_i[row]; nzB = b_i[row+1] - b_i[row]; 1098 ncols = nzA + nzB; 1099 cworkB = b_j + b_i[row]; 1100 vworkA = a_a + a_i[row]; 1101 vworkB = b_a + b_i[row]; 1102 1103 /* load the column values for this row into vals*/ 1104 vals = sbuf_aa_i+ct2; 1105 1106 for (l=0; l<nzB; l++) { 1107 if ((bmap[cworkB[l]]) < cstart) vals[l] = vworkB[l]; 1108 else break; 1109 } 1110 imark = l; 1111 for (l=0; l<nzA; l++) vals[imark+l] = vworkA[l]; 1112 for (l=imark; l<nzB; l++) vals[nzA+l] = vworkB[l]; 1113 1114 ct2 += ncols; 1115 } 1116 } 1117 ierr = MPI_Isend(sbuf_aa_i,req_size[i],MPIU_SCALAR,req_source[i],tag3,comm,s_waits4+i);CHKERRQ(ierr); 1118 } 1119 } 1120 ierr = PetscMalloc((nrqs+1)*sizeof(MPI_Status),&r_status4);CHKERRQ(ierr); 1121 ierr = PetscMalloc((nrqr+1)*sizeof(MPI_Status),&s_status4);CHKERRQ(ierr); 1122 ierr = PetscFree(rbuf1);CHKERRQ(ierr); 1123 1124 /* Form the matrix */ 1125 /* create col map */ 1126 { 1127 PetscInt *icol_i; 1128 1129 len = (1+ismax)*sizeof(PetscInt*)+ (1+ismax*C->N)*sizeof(PetscInt); 1130 ierr = PetscMalloc(len,&cmap);CHKERRQ(ierr); 1131 cmap[0] = (PetscInt*)(cmap + ismax); 1132 ierr = PetscMemzero(cmap[0],(1+ismax*C->N)*sizeof(PetscInt));CHKERRQ(ierr); 1133 for (i=1; i<ismax; i++) { cmap[i] = cmap[i-1] + C->N; } 1134 for (i=0; i<ismax; i++) { 1135 jmax = ncol[i]; 1136 icol_i = icol[i]; 1137 cmap_i = cmap[i]; 1138 for (j=0; j<jmax; j++) { 1139 cmap_i[icol_i[j]] = j+1; 1140 } 1141 } 1142 } 1143 1144 /* Create lens which is required for MatCreate... */ 1145 for (i=0,j=0; i<ismax; i++) { j += nrow[i]; } 1146 len = (1+ismax)*sizeof(PetscInt*)+ j*sizeof(PetscInt); 1147 ierr = PetscMalloc(len,&lens);CHKERRQ(ierr); 1148 lens[0] = (PetscInt*)(lens + ismax); 1149 ierr = PetscMemzero(lens[0],j*sizeof(PetscInt));CHKERRQ(ierr); 1150 for (i=1; i<ismax; i++) { lens[i] = lens[i-1] + nrow[i-1]; } 1151 1152 /* Update lens from local data */ 1153 for (i=0; i<ismax; i++) { 1154 jmax = nrow[i]; 1155 cmap_i = cmap[i]; 1156 irow_i = irow[i]; 1157 lens_i = lens[i]; 1158 for (l=0,j=0; j<jmax; j++) { 1159 row = irow_i[j]; 1160 while (row >= c->rowners[l+1]) l++; 1161 proc = l; 1162 if (proc == rank) { 1163 ierr = MatGetRow_MPIAIJ(C,row,&ncols,&cols,0);CHKERRQ(ierr); 1164 for (k=0; k<ncols; k++) { 1165 if (cmap_i[cols[k]]) { lens_i[j]++;} 1166 } 1167 ierr = MatRestoreRow_MPIAIJ(C,row,&ncols,&cols,0);CHKERRQ(ierr); 1168 } 1169 } 1170 } 1171 1172 /* Create row map*/ 1173 len = (1+ismax)*sizeof(PetscInt*)+ ismax*C->M*sizeof(PetscInt); 1174 ierr = PetscMalloc(len,&rmap);CHKERRQ(ierr); 1175 rmap[0] = (PetscInt*)(rmap + ismax); 1176 ierr = PetscMemzero(rmap[0],ismax*C->M*sizeof(PetscInt));CHKERRQ(ierr); 1177 for (i=1; i<ismax; i++) { rmap[i] = rmap[i-1] + C->M;} 1178 for (i=0; i<ismax; i++) { 1179 rmap_i = rmap[i]; 1180 irow_i = irow[i]; 1181 jmax = nrow[i]; 1182 for (j=0; j<jmax; j++) { 1183 rmap_i[irow_i[j]] = j; 1184 } 1185 } 1186 1187 /* Update lens from offproc data */ 1188 { 1189 PetscInt *rbuf2_i,*rbuf3_i,*sbuf1_i; 1190 1191 for (tmp2=0; tmp2<nrqs; tmp2++) { 1192 ierr = MPI_Waitany(nrqs,r_waits3,&idex2,r_status3+tmp2);CHKERRQ(ierr); 1193 idex = pa[idex2]; 1194 sbuf1_i = sbuf1[idex]; 1195 jmax = sbuf1_i[0]; 1196 ct1 = 2*jmax+1; 1197 ct2 = 0; 1198 rbuf2_i = rbuf2[idex2]; 1199 rbuf3_i = rbuf3[idex2]; 1200 for (j=1; j<=jmax; j++) { 1201 is_no = sbuf1_i[2*j-1]; 1202 max1 = sbuf1_i[2*j]; 1203 lens_i = lens[is_no]; 1204 cmap_i = cmap[is_no]; 1205 rmap_i = rmap[is_no]; 1206 for (k=0; k<max1; k++,ct1++) { 1207 row = rmap_i[sbuf1_i[ct1]]; /* the val in the new matrix to be */ 1208 max2 = rbuf2_i[ct1]; 1209 for (l=0; l<max2; l++,ct2++) { 1210 if (cmap_i[rbuf3_i[ct2]]) { 1211 lens_i[row]++; 1212 } 1213 } 1214 } 1215 } 1216 } 1217 } 1218 ierr = PetscFree(r_status3);CHKERRQ(ierr); 1219 ierr = PetscFree(r_waits3);CHKERRQ(ierr); 1220 ierr = MPI_Waitall(nrqr,s_waits3,s_status3);CHKERRQ(ierr); 1221 ierr = PetscFree(s_status3);CHKERRQ(ierr); 1222 ierr = PetscFree(s_waits3);CHKERRQ(ierr); 1223 1224 /* Create the submatrices */ 1225 if (scall == MAT_REUSE_MATRIX) { 1226 PetscTruth flag; 1227 1228 /* 1229 Assumes new rows are same length as the old rows,hence bug! 1230 */ 1231 for (i=0; i<ismax; i++) { 1232 mat = (Mat_SeqAIJ *)(submats[i]->data); 1233 if ((submats[i]->m != nrow[i]) || (submats[i]->n != ncol[i])) { 1234 SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size"); 1235 } 1236 ierr = PetscMemcmp(mat->ilen,lens[i],submats[i]->m*sizeof(PetscInt),&flag);CHKERRQ(ierr); 1237 if (!flag) { 1238 SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros"); 1239 } 1240 /* Initial matrix as if empty */ 1241 ierr = PetscMemzero(mat->ilen,submats[i]->m*sizeof(PetscInt));CHKERRQ(ierr); 1242 submats[i]->factor = C->factor; 1243 } 1244 } else { 1245 for (i=0; i<ismax; i++) { 1246 ierr = MatCreate(PETSC_COMM_SELF,nrow[i],ncol[i],PETSC_DETERMINE,PETSC_DETERMINE,submats+i);CHKERRQ(ierr); 1247 ierr = MatSetType(submats[i],A->type_name);CHKERRQ(ierr); 1248 ierr = MatSeqAIJSetPreallocation(submats[i],0,lens[i]);CHKERRQ(ierr); 1249 } 1250 } 1251 1252 /* Assemble the matrices */ 1253 /* First assemble the local rows */ 1254 { 1255 PetscInt ilen_row,*imat_ilen,*imat_j,*imat_i,old_row; 1256 PetscScalar *imat_a; 1257 1258 for (i=0; i<ismax; i++) { 1259 mat = (Mat_SeqAIJ*)submats[i]->data; 1260 imat_ilen = mat->ilen; 1261 imat_j = mat->j; 1262 imat_i = mat->i; 1263 imat_a = mat->a; 1264 cmap_i = cmap[i]; 1265 rmap_i = rmap[i]; 1266 irow_i = irow[i]; 1267 jmax = nrow[i]; 1268 for (l=0,j=0; j<jmax; j++) { 1269 row = irow_i[j]; 1270 while (row >= c->rowners[l+1]) l++; 1271 proc = l; 1272 if (proc == rank) { 1273 old_row = row; 1274 row = rmap_i[row]; 1275 ilen_row = imat_ilen[row]; 1276 ierr = MatGetRow_MPIAIJ(C,old_row,&ncols,&cols,&vals);CHKERRQ(ierr); 1277 mat_i = imat_i[row] ; 1278 mat_a = imat_a + mat_i; 1279 mat_j = imat_j + mat_i; 1280 for (k=0; k<ncols; k++) { 1281 if ((tcol = cmap_i[cols[k]])) { 1282 *mat_j++ = tcol - 1; 1283 *mat_a++ = vals[k]; 1284 ilen_row++; 1285 } 1286 } 1287 ierr = MatRestoreRow_MPIAIJ(C,old_row,&ncols,&cols,&vals);CHKERRQ(ierr); 1288 imat_ilen[row] = ilen_row; 1289 } 1290 } 1291 } 1292 } 1293 1294 /* Now assemble the off proc rows*/ 1295 { 1296 PetscInt *sbuf1_i,*rbuf2_i,*rbuf3_i,*imat_ilen,ilen; 1297 PetscInt *imat_j,*imat_i; 1298 PetscScalar *imat_a,*rbuf4_i; 1299 1300 for (tmp2=0; tmp2<nrqs; tmp2++) { 1301 ierr = MPI_Waitany(nrqs,r_waits4,&idex2,r_status4+tmp2);CHKERRQ(ierr); 1302 idex = pa[idex2]; 1303 sbuf1_i = sbuf1[idex]; 1304 jmax = sbuf1_i[0]; 1305 ct1 = 2*jmax + 1; 1306 ct2 = 0; 1307 rbuf2_i = rbuf2[idex2]; 1308 rbuf3_i = rbuf3[idex2]; 1309 rbuf4_i = rbuf4[idex2]; 1310 for (j=1; j<=jmax; j++) { 1311 is_no = sbuf1_i[2*j-1]; 1312 rmap_i = rmap[is_no]; 1313 cmap_i = cmap[is_no]; 1314 mat = (Mat_SeqAIJ*)submats[is_no]->data; 1315 imat_ilen = mat->ilen; 1316 imat_j = mat->j; 1317 imat_i = mat->i; 1318 imat_a = mat->a; 1319 max1 = sbuf1_i[2*j]; 1320 for (k=0; k<max1; k++,ct1++) { 1321 row = sbuf1_i[ct1]; 1322 row = rmap_i[row]; 1323 ilen = imat_ilen[row]; 1324 mat_i = imat_i[row] ; 1325 mat_a = imat_a + mat_i; 1326 mat_j = imat_j + mat_i; 1327 max2 = rbuf2_i[ct1]; 1328 for (l=0; l<max2; l++,ct2++) { 1329 if ((tcol = cmap_i[rbuf3_i[ct2]])) { 1330 *mat_j++ = tcol - 1; 1331 *mat_a++ = rbuf4_i[ct2]; 1332 ilen++; 1333 } 1334 } 1335 imat_ilen[row] = ilen; 1336 } 1337 } 1338 } 1339 } 1340 ierr = PetscFree(r_status4);CHKERRQ(ierr); 1341 ierr = PetscFree(r_waits4);CHKERRQ(ierr); 1342 ierr = MPI_Waitall(nrqr,s_waits4,s_status4);CHKERRQ(ierr); 1343 ierr = PetscFree(s_waits4);CHKERRQ(ierr); 1344 ierr = PetscFree(s_status4);CHKERRQ(ierr); 1345 1346 /* Restore the indices */ 1347 for (i=0; i<ismax; i++) { 1348 ierr = ISRestoreIndices(isrow[i],irow+i);CHKERRQ(ierr); 1349 ierr = ISRestoreIndices(iscol[i],icol+i);CHKERRQ(ierr); 1350 } 1351 1352 /* Destroy allocated memory */ 1353 ierr = PetscFree(irow);CHKERRQ(ierr); 1354 ierr = PetscFree(w1);CHKERRQ(ierr); 1355 ierr = PetscFree(pa);CHKERRQ(ierr); 1356 1357 ierr = PetscFree(sbuf1);CHKERRQ(ierr); 1358 ierr = PetscFree(rbuf2);CHKERRQ(ierr); 1359 for (i=0; i<nrqr; ++i) { 1360 ierr = PetscFree(sbuf2[i]);CHKERRQ(ierr); 1361 } 1362 for (i=0; i<nrqs; ++i) { 1363 ierr = PetscFree(rbuf3[i]);CHKERRQ(ierr); 1364 ierr = PetscFree(rbuf4[i]);CHKERRQ(ierr); 1365 } 1366 1367 ierr = PetscFree(sbuf2);CHKERRQ(ierr); 1368 ierr = PetscFree(rbuf3);CHKERRQ(ierr); 1369 ierr = PetscFree(rbuf4);CHKERRQ(ierr); 1370 ierr = PetscFree(sbuf_aj[0]);CHKERRQ(ierr); 1371 ierr = PetscFree(sbuf_aj);CHKERRQ(ierr); 1372 ierr = PetscFree(sbuf_aa[0]);CHKERRQ(ierr); 1373 ierr = PetscFree(sbuf_aa);CHKERRQ(ierr); 1374 1375 ierr = PetscFree(cmap);CHKERRQ(ierr); 1376 ierr = PetscFree(rmap);CHKERRQ(ierr); 1377 ierr = PetscFree(lens);CHKERRQ(ierr); 1378 1379 for (i=0; i<ismax; i++) { 1380 ierr = MatAssemblyBegin(submats[i],MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1381 ierr = MatAssemblyEnd(submats[i],MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1382 } 1383 PetscFunctionReturn(0); 1384 } 1385 1386