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