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