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