1 /*$Id: sbaijov.c,v 1.65 2001/08/06 21:15:42 bsmith Exp $*/ 2 3 /* 4 Routines to compute overlapping regions of a parallel MPI matrix. 5 Used for finding submatrices that were shared across processors. 6 */ 7 #include "src/mat/impls/sbaij/mpi/mpisbaij.h" 8 #include "petscbt.h" 9 10 static int MatIncreaseOverlap_MPISBAIJ_Once(Mat,int,IS *); 11 static int MatIncreaseOverlap_MPISBAIJ_Local(Mat,int *,int **,PetscBT*); 12 static int MatIncreaseOverlap_MPISBAIJ_Receive(Mat,int,int **,int**,int*); 13 14 /* this function is sasme as MatCompressIndicesGeneral_MPIBAIJ -- should be removed! */ 15 #undef __FUNCT__ 16 #define __FUNCT__ "MatCompressIndicesGeneral_MPISBAIJ" 17 static int MatCompressIndicesGeneral_MPISBAIJ(Mat C,int imax,const IS is_in[],IS is_out[]) 18 { 19 Mat_MPISBAIJ *baij = (Mat_MPISBAIJ*)C->data; 20 int ierr,isz,bs = baij->bs,n,i,j,*idx,ival; 21 #if defined (PETSC_USE_CTABLE) 22 PetscTable gid1_lid1; 23 int tt, gid1, *nidx; 24 PetscTablePosition tpos; 25 #else 26 int Nbs,*nidx; 27 PetscBT table; 28 #endif 29 30 PetscFunctionBegin; 31 /* printf(" ...MatCompressIndicesGeneral_MPISBAIJ is called ...\n"); */ 32 #if defined (PETSC_USE_CTABLE) 33 ierr = PetscTableCreate(baij->mbs,&gid1_lid1);CHKERRQ(ierr); 34 #else 35 Nbs = baij->Nbs; 36 ierr = PetscMalloc((Nbs+1)*sizeof(int),&nidx);CHKERRQ(ierr); 37 ierr = PetscBTCreate(Nbs,table);CHKERRQ(ierr); 38 #endif 39 for (i=0; i<imax; i++) { 40 isz = 0; 41 #if defined (PETSC_USE_CTABLE) 42 ierr = PetscTableRemoveAll(gid1_lid1);CHKERRQ(ierr); 43 #else 44 ierr = PetscBTMemzero(Nbs,table);CHKERRQ(ierr); 45 #endif 46 ierr = ISGetIndices(is_in[i],&idx);CHKERRQ(ierr); 47 ierr = ISGetLocalSize(is_in[i],&n);CHKERRQ(ierr); 48 for (j=0; j<n ; j++) { 49 ival = idx[j]/bs; /* convert the indices into block indices */ 50 #if defined (PETSC_USE_CTABLE) 51 ierr = PetscTableFind(gid1_lid1,ival+1,&tt);CHKERRQ(ierr); 52 if (!tt) { 53 ierr = PetscTableAdd(gid1_lid1,ival+1,isz+1);CHKERRQ(ierr); 54 isz++; 55 } 56 #else 57 if (ival>Nbs) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"index greater than mat-dim"); 58 if(!PetscBTLookupSet(table,ival)) { nidx[isz++] = ival;} 59 #endif 60 } 61 ierr = ISRestoreIndices(is_in[i],&idx);CHKERRQ(ierr); 62 #if defined (PETSC_USE_CTABLE) 63 ierr = PetscMalloc((isz+1)*sizeof(int),&nidx);CHKERRQ(ierr); 64 ierr = PetscTableGetHeadPosition(gid1_lid1,&tpos);CHKERRQ(ierr); 65 j = 0; 66 while (tpos) { 67 ierr = PetscTableGetNext(gid1_lid1,&tpos,&gid1,&tt);CHKERRQ(ierr); 68 if (tt-- > isz) { SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"index greater than array-dim"); } 69 nidx[tt] = gid1 - 1; 70 j++; 71 } 72 if (j != isz) { SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"table error: jj != isz"); } 73 ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is_out+i));CHKERRQ(ierr); 74 ierr = PetscFree(nidx);CHKERRQ(ierr); 75 #else 76 ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is_out+i));CHKERRQ(ierr); 77 #endif 78 } 79 #if defined (PETSC_USE_CTABLE) 80 ierr = PetscTableDelete(gid1_lid1);CHKERRQ(ierr); 81 #else 82 ierr = PetscBTDestroy(table);CHKERRQ(ierr); 83 ierr = PetscFree(nidx);CHKERRQ(ierr); 84 #endif 85 PetscFunctionReturn(0); 86 } 87 88 #undef __FUNCT__ 89 #define __FUNCT__ "MatExpandIndices_MPISBAIJ" 90 static int MatExpandIndices_MPISBAIJ(Mat C,int imax,const IS is_in[],IS is_out[]) 91 { 92 Mat_MPISBAIJ *baij = (Mat_MPISBAIJ*)C->data; 93 int ierr,bs = baij->bs,n,i,j,k,*idx,*nidx; 94 #if defined (PETSC_USE_CTABLE) 95 int maxsz; 96 #else 97 int Nbs = baij->Nbs; 98 #endif 99 100 PetscFunctionBegin; 101 /* printf(" ... MatExpandIndices_MPISBAIJ is called ...\n"); */ 102 #if defined (PETSC_USE_CTABLE) 103 /* Now check max size */ 104 for (i=0,maxsz=0; i<imax; i++) { 105 ierr = ISGetIndices(is_in[i],&idx);CHKERRQ(ierr); 106 ierr = ISGetLocalSize(is_in[i],&n);CHKERRQ(ierr); 107 if (n*bs > maxsz) maxsz = n*bs; 108 } 109 ierr = PetscMalloc((maxsz+1)*sizeof(int),&nidx);CHKERRQ(ierr); 110 #else 111 ierr = PetscMalloc((Nbs*bs+1)*sizeof(int),&nidx);CHKERRQ(ierr); 112 #endif 113 114 for (i=0; i<imax; i++) { 115 ierr = ISGetIndices(is_in[i],&idx);CHKERRQ(ierr); 116 ierr = ISGetLocalSize(is_in[i],&n);CHKERRQ(ierr); 117 for (j=0; j<n ; ++j){ 118 for (k=0; k<bs; k++) 119 nidx[j*bs+k] = idx[j]*bs+k; 120 } 121 ierr = ISRestoreIndices(is_in[i],&idx);CHKERRQ(ierr); 122 ierr = ISCreateGeneral(PETSC_COMM_SELF,n*bs,nidx,is_out+i);CHKERRQ(ierr); 123 } 124 ierr = PetscFree(nidx);CHKERRQ(ierr); 125 PetscFunctionReturn(0); 126 } 127 128 #undef __FUNCT__ 129 #define __FUNCT__ "MatIncreaseOverlap_MPISBAIJ" 130 int MatIncreaseOverlap_MPISBAIJ(Mat C,int is_max,IS is[],int ov) 131 { 132 int i,ierr; 133 IS *is_new; 134 135 PetscFunctionBegin; 136 ierr = PetscMalloc(is_max*sizeof(IS),&is_new);CHKERRQ(ierr); 137 /* Convert the indices into block format */ 138 ierr = MatCompressIndicesGeneral_MPISBAIJ(C,is_max,is,is_new);CHKERRQ(ierr); 139 if (ov < 0){ SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Negative overlap specified\n");} 140 for (i=0; i<ov; ++i) { 141 ierr = MatIncreaseOverlap_MPISBAIJ_Once(C,is_max,is_new);CHKERRQ(ierr); 142 } 143 for (i=0; i<is_max; i++) {ierr = ISDestroy(is[i]);CHKERRQ(ierr);} 144 ierr = MatExpandIndices_MPISBAIJ(C,is_max,is_new,is);CHKERRQ(ierr); 145 for (i=0; i<is_max; i++) {ierr = ISDestroy(is_new[i]);CHKERRQ(ierr);} 146 ierr = PetscFree(is_new);CHKERRQ(ierr); 147 PetscFunctionReturn(0); 148 } 149 150 /* All data are packed in the format: 151 data[0] = is_max, no of is 152 data[1] = size of is[0] 153 ... 154 data[is_max] = size of is[is_max-1] 155 data[is_max + 1] = data(is[0]) 156 ... 157 data[is_max+1+sum(size of is[k]), k=0,...,i-1] = data(is[i]) 158 ... 159 */ 160 #undef __FUNCT__ 161 #define __FUNCT__ "MatIncreaseOverlap_MPISBAIJ_Once" 162 static int MatIncreaseOverlap_MPISBAIJ_Once(Mat C,int is_max,IS is[]) 163 { 164 Mat_MPISBAIJ *c = (Mat_MPISBAIJ*)C->data; 165 int len,*idx_i,*nidx_i,isz,col,*n; 166 int size,rank,Mbs,i,j,k,ierr,nrqs,*data1,*data2,*data3,*odata1,*odata2; 167 int *mydata,*mydata_i; 168 int tag1,tag2,flag,proc_id,**odata2_ptr; 169 char *t_p,*ot_p; 170 MPI_Comm comm; 171 MPI_Request *s_waits1,*s_waits2,r_req; 172 MPI_Status *s_status,r_status; 173 PetscBT *table; 174 PetscBT table_i; 175 PetscBT *otable; 176 177 PetscFunctionBegin; 178 179 comm = C->comm; 180 size = c->size; 181 rank = c->rank; 182 Mbs = c->Mbs; 183 184 ierr = PetscObjectGetNewTag((PetscObject)C,&tag1);CHKERRQ(ierr); 185 ierr = PetscObjectGetNewTag((PetscObject)C,&tag2);CHKERRQ(ierr); 186 187 /* create tables for marking indices */ 188 len = is_max*sizeof(PetscBT) + (Mbs/PETSC_BITS_PER_BYTE+1)*is_max*sizeof(char) + 1; 189 ierr = PetscMalloc(2*len,&table);CHKERRQ(ierr); 190 otable = (PetscBT *)(table + len); 191 t_p = (char *)(table + is_max); 192 ot_p = (char *)(otable + is_max); 193 for (i=0; i<is_max; i++) { 194 j = (Mbs/PETSC_BITS_PER_BYTE+1)*i; 195 table[i] = t_p + j; 196 otable[i] = ot_p + j; 197 } 198 199 /* 1. Send is[] in this processor to all other processors */ 200 /*--------------------------------------------------------*/ 201 /* Allocate Memory for outgoing messages */ 202 len = is_max*sizeof(int); 203 ierr = PetscMalloc(len,&n);CHKERRQ(ierr); 204 205 len = 1 + is_max; 206 for (i=0; i<is_max; i++) { 207 ierr = ISGetLocalSize(is[i],&n[i]);CHKERRQ(ierr); 208 len += n[i]; 209 } 210 ierr = PetscMalloc(len*sizeof(int),&data1);CHKERRQ(ierr); 211 212 /* Form the outgoing messages */ 213 data1[0] = is_max; 214 k = is_max + 1; 215 for (i=0; i<is_max; i++) { 216 data1[1+i] = n[i]; 217 ierr = ISGetIndices(is[i],&idx_i);CHKERRQ(ierr); 218 for (j=0; j<data1[i+1]; j++){ 219 data1[k++] = idx_i[j]; 220 } 221 ierr = ISRestoreIndices(is[i],&idx_i);CHKERRQ(ierr); 222 ierr = ISDestroy(is[i]);CHKERRQ(ierr); 223 } 224 if (k != len) SETERRQ2(1,"Error on forming the outgoing messages: k %d != len %d",k,len); 225 226 /* Now post the sends */ 227 ierr = PetscMalloc(size*sizeof(MPI_Request),&s_waits1);CHKERRQ(ierr); 228 229 k = 0; 230 for (proc_id=0; proc_id<size; ++proc_id) { /* send data1 to processor [proc_id] */ 231 if (proc_id != rank){ 232 ierr = MPI_Isend(data1,len,MPI_INT,proc_id,tag1,comm,s_waits1+k);CHKERRQ(ierr); 233 /* printf(" [%d] send %d msg to [%d] \n",rank,len,proc_id); */ 234 k++; 235 } 236 } 237 238 /* 2. Do local work on this processor's is[] */ 239 /*-------------------------------------------*/ 240 ierr = MatIncreaseOverlap_MPISBAIJ_Local(C,data1,&data2,table);CHKERRQ(ierr); 241 242 /* 3. Receive other's is[] and process. Then send back */ 243 /*----------------------------------------------------*/ 244 /* Send is done */ 245 nrqs = size-1; 246 ierr = PetscMalloc(size*sizeof(MPI_Status),&s_status);CHKERRQ(ierr); 247 ierr = MPI_Waitall(nrqs,s_waits1,s_status);CHKERRQ(ierr); 248 249 ierr = PetscFree(data1);CHKERRQ(ierr); /* keep - move after merge !!! */ 250 ierr = PetscMalloc(size*sizeof(MPI_Request),&s_waits2);CHKERRQ(ierr); 251 252 ierr = PetscMalloc(size*sizeof(int**),&odata2_ptr); 253 k = 0; 254 do { 255 /* Receive messages */ 256 ierr = MPI_Iprobe(MPI_ANY_SOURCE,tag1,comm,&flag,&r_status); 257 if (flag){ 258 ierr = MPI_Get_count(&r_status,MPI_INT,&len); 259 proc_id = r_status.MPI_SOURCE; 260 ierr = PetscMalloc(len*sizeof(int),&odata1);CHKERRQ(ierr); 261 ierr = MPI_Irecv(odata1,len,MPI_INT,proc_id,r_status.MPI_TAG,comm,&r_req); 262 /* printf(" [%d] recv %d msg from [%d]\n",rank,len,proc_id); */ 263 264 /* Process messages */ 265 ierr = MatIncreaseOverlap_MPISBAIJ_Local(C,odata1,&odata2_ptr[k],otable);CHKERRQ(ierr); 266 odata2 = odata2_ptr[k]; 267 len = 1 + odata2[0]; 268 for (i=0; i<odata2[0]; i++){ 269 len += odata2[1 + i]; 270 } 271 272 /* Send messages back */ 273 /* printf(" [%d] send %d msg back to [%d] \n",rank,len,proc_id); */ 274 ierr = MPI_Isend(odata2,len,MPI_INT,proc_id,tag2,comm,&s_waits2[k]);CHKERRQ(ierr); 275 276 ierr = PetscFree(odata1);CHKERRQ(ierr); 277 k++; 278 } 279 } while (k < nrqs); 280 281 /* 4. Receive work done on other processors, then merge */ 282 /*--------------------------------------------------------*/ 283 ierr = MPI_Waitall(nrqs,s_waits2,s_status);CHKERRQ(ierr); 284 for (k=0; k<nrqs; k++){ 285 ierr = PetscFree(odata2_ptr[k]);CHKERRQ(ierr); 286 } 287 ierr = PetscFree(odata2_ptr);CHKERRQ(ierr); 288 289 /* allocate memory for merged data */ 290 ierr = PetscMalloc((1+is_max*(Mbs+1))*sizeof(int),&mydata);CHKERRQ(ierr); 291 292 /* copy data2 into mydata */ 293 k = is_max + 1; 294 for (i=0; i<is_max; i++){ 295 mydata[1+i] = data2[1+i]; /* size of is[i] before merge */ 296 mydata_i = mydata + 1 + is_max + Mbs*i; 297 for (j=0; j<data2[1+i]; j++){ 298 mydata_i[j] = data2[k++]; 299 } 300 } 301 302 k = 0; 303 do { 304 /* Receive messages */ 305 ierr = MPI_Iprobe(MPI_ANY_SOURCE,tag2,comm,&flag,&r_status); 306 if (flag){ 307 ierr = MPI_Get_count(&r_status,MPI_INT,&len); 308 proc_id = r_status.MPI_SOURCE; 309 ierr = ierr = PetscMalloc(len*sizeof(int),&data3);CHKERRQ(ierr); 310 ierr = MPI_Irecv(data3,len,MPI_INT,proc_id,r_status.MPI_TAG,comm,&r_req); 311 /* printf(" [%d] recv %d msg from [%d]\n",rank,len,proc_id); */ 312 313 /* merge data3 into mydata */ 314 nidx_i = data3 + 1 + is_max; 315 for (i=0; i<is_max; i++){ 316 table_i = table[i]; 317 mydata_i = mydata + 1 + is_max + Mbs*i; 318 isz = mydata[1+i]; /* size of is[i] from data2 */ 319 320 for (j=0; j<data3[1+i]; j++){ 321 col = nidx_i[j]; 322 if (!PetscBTLookupSet(table_i,col)) {mydata_i[isz++] = col;} 323 } 324 mydata[1+i] = isz; 325 if (i < is_max - 1){ 326 nidx_i += data3[1+i]; /* ptr to is[i+1] array from data3 */ 327 } 328 } /* for (i=0; i<is_max; i++) */ 329 330 k++; 331 ierr = PetscFree(data3);CHKERRQ(ierr); 332 } 333 } while (k < nrqs); 334 335 /* 5. Create new is[] */ 336 /*--------------------*/ 337 for (i=0; i<is_max; i++) { 338 mydata_i = mydata + 1 + is_max + Mbs*i; 339 ierr = ISCreateGeneral(PETSC_COMM_SELF,mydata[1+i],mydata_i,is+i);CHKERRQ(ierr); 340 } 341 342 ierr = PetscFree(mydata);CHKERRQ(ierr); 343 ierr = PetscFree(data2);CHKERRQ(ierr); 344 ierr = PetscFree(n);CHKERRQ(ierr); 345 ierr = PetscFree(s_waits1);CHKERRQ(ierr); 346 ierr = PetscFree(s_waits2);CHKERRQ(ierr); 347 ierr = PetscFree(s_status);CHKERRQ(ierr); 348 ierr = PetscFree(table);CHKERRQ(ierr); 349 PetscFunctionReturn(0); 350 } 351 352 #undef __FUNCT__ 353 #define __FUNCT__ "MatIncreaseOverlap_MPISBAIJ_Local" 354 /* 355 MatIncreaseOverlap_MPISBAIJ_Local - Called by MatIncreaseOverlap, to do 356 the work on the local processor. 357 358 Inputs: 359 C - MAT_MPISBAIJ; 360 data - holds is[] in the format: 361 data[0] = is_max, no of is 362 data[1] = size of is[0] 363 ... 364 data[is_max] = size of is[is_max-1] 365 data[is_max + 1] = is[0] array 366 ... 367 data[is_max+1+sum(n[k]), k=0,...,i-1] = is[i] array 368 ... 369 370 Output: 371 data_new - holds new is[] in the same format as data 372 table - table[i]: mark the indices of is[i], i=0,...,is_max. 373 */ 374 static int MatIncreaseOverlap_MPISBAIJ_Local(Mat C,int *data,int **data_new,PetscBT *table) 375 { 376 Mat_MPISBAIJ *c = (Mat_MPISBAIJ*)C->data; 377 Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)(c->A)->data; 378 Mat_SeqBAIJ *b = (Mat_SeqBAIJ*)(c->B)->data; 379 int ierr,row,mbs,Mbs,*nidx,*nidx_i,col,isz,isz0,*ai,*aj,bs,*bi,*bj,*garray,rstart,l; 380 int a_start,a_end,b_start,b_end,i,j,k,is_max,*idx_i,n; 381 PetscBT table0; 382 PetscBT table_i; /* poits to i-th table */ 383 384 PetscFunctionBegin; 385 Mbs = c->Mbs; mbs = a->mbs; bs = a->bs; 386 ai = a->i; aj = a->j; 387 bi = b->i; bj = b->j; 388 garray = c->garray; 389 rstart = c->rstart; 390 is_max = data[0]; 391 392 ierr = PetscBTCreate(Mbs,table0);CHKERRQ(ierr); 393 394 ierr = PetscMalloc((1+is_max*(Mbs+1))*sizeof(int),&nidx);CHKERRQ(ierr); 395 nidx[0] = is_max; 396 397 idx_i = data + is_max + 1; /* ptr to input is[0] array */ 398 nidx_i = nidx + is_max + 1; /* ptr to active is[0] array */ 399 for (i=0; i<is_max; i++) { /* for each is */ 400 isz = 0; 401 table_i = table[i]; 402 ierr = PetscBTMemzero(Mbs,table_i);CHKERRQ(ierr); 403 n = data[1+i]; /* size of input is[i] */ 404 405 if (n > 0) { 406 407 /* Enter input is[i] into active is[i] */ 408 for (j=0; j<n; j++){ 409 col = idx_i[j]; 410 if (col >= Mbs) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"index col %d >= Mbs %d",col,Mbs); 411 if(!PetscBTLookupSet(table_i,col)) { nidx_i[isz++] = col;} 412 } 413 414 /* set table0 for lookup */ 415 ierr = PetscBTMemzero(Mbs,table0);CHKERRQ(ierr); 416 for (l=0; l<isz; l++) PetscBTSet(table0,nidx_i[l]); 417 418 isz0 = isz; /* size of input is[i] after removing repeated indices */ 419 k = 0; /* no. of indices from input is[i] that have been examined */ 420 for (row=0; row<mbs; row++){ 421 a_start = ai[row]; a_end = ai[row+1]; 422 b_start = bi[row]; b_end = bi[row+1]; 423 if (PetscBTLookup(table0,row+rstart)){ /* row is on nidx_i - row search: collect all col in this row */ 424 for (l = a_start; l<a_end ; l++){ /* Amat */ 425 col = aj[l] + rstart; 426 if (!PetscBTLookupSet(table_i,col)) {nidx_i[isz++] = col;} 427 } 428 for (l = b_start; l<b_end ; l++){ /* Bmat */ 429 col = garray[bj[l]]; 430 if (!PetscBTLookupSet(table_i,col)) {nidx_i[isz++] = col;} 431 } 432 k++; 433 if (k >= isz0) break; /* for (row=0; row<mbs; row++) */ 434 } else { /* row is not on nidx_i - col serach: add row onto nidx_i if there is a col in nidx_i */ 435 for (l = a_start; l<a_end ; l++){ /* Amat */ 436 col = aj[l] + rstart; 437 if (PetscBTLookup(table0,col)){ 438 if (!PetscBTLookupSet(table_i,row+rstart)) {nidx_i[isz++] = row+rstart;} 439 break; /* for l = start; l<end ; l++) */ 440 } 441 } 442 for (l = b_start; l<b_end ; l++){ /* Bmat */ 443 col = garray[bj[l]]; 444 if (PetscBTLookup(table0,col)){ 445 if (!PetscBTLookupSet(table_i,row+rstart)) {nidx_i[isz++] = row+rstart;} 446 break; /* for l = start; l<end ; l++) */ 447 } 448 } 449 } 450 } /* for (row=0; row<mbs; row++) */ 451 } /* if (n > 0) */ 452 453 if (i < is_max - 1){ 454 idx_i += n; /* ptr to input is[i+1] array */ 455 nidx_i += isz; /* ptr to active is[i+1] array */ 456 } 457 nidx[1+i] = isz; /* size of new is[i] */ 458 } /* /* for each is */ 459 *data_new = nidx; 460 ierr = PetscBTDestroy(table0);CHKERRQ(ierr); 461 462 PetscFunctionReturn(0); 463 } 464 465 466