xref: /petsc/src/mat/impls/sbaij/mpi/sbaijov.c (revision c6db04a5321582041def2b1e244c75985478b3ef)
1632d0f97SHong Zhang 
2632d0f97SHong Zhang /*
3632d0f97SHong Zhang    Routines to compute overlapping regions of a parallel MPI matrix.
4632d0f97SHong Zhang    Used for finding submatrices that were shared across processors.
5632d0f97SHong Zhang */
6*c6db04a5SJed Brown #include <../src/mat/impls/sbaij/mpi/mpisbaij.h>
7*c6db04a5SJed Brown #include <petscbt.h>
8632d0f97SHong Zhang 
913f74950SBarry Smith static PetscErrorCode MatIncreaseOverlap_MPISBAIJ_Once(Mat,PetscInt,IS*);
1013f74950SBarry Smith static PetscErrorCode MatIncreaseOverlap_MPISBAIJ_Local(Mat,PetscInt*,PetscInt,PetscInt*,PetscBT*);
11632d0f97SHong Zhang 
12632d0f97SHong Zhang #undef __FUNCT__
13632d0f97SHong Zhang #define __FUNCT__ "MatIncreaseOverlap_MPISBAIJ"
1413f74950SBarry Smith PetscErrorCode MatIncreaseOverlap_MPISBAIJ(Mat C,PetscInt is_max,IS is[],PetscInt ov)
15632d0f97SHong Zhang {
166849ba73SBarry Smith   PetscErrorCode ierr;
17d0f46423SBarry Smith   PetscInt       i,N=C->cmap->N, bs=C->rmap->bs;
18632d0f97SHong Zhang   IS             *is_new;
19632d0f97SHong Zhang 
20632d0f97SHong Zhang   PetscFunctionBegin;
21c910923dSHong Zhang   ierr = PetscMalloc(is_max*sizeof(IS),&is_new);CHKERRQ(ierr);
22632d0f97SHong Zhang   /* Convert the indices into block format */
2327f478b1SHong Zhang   ierr = ISCompressIndicesGeneral(N,bs,is_max,is,is_new);CHKERRQ(ierr);
24e32f2f54SBarry Smith   if (ov < 0){ SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative overlap specified\n");}
25632d0f97SHong Zhang   for (i=0; i<ov; ++i) {
26c910923dSHong Zhang     ierr = MatIncreaseOverlap_MPISBAIJ_Once(C,is_max,is_new);CHKERRQ(ierr);
27632d0f97SHong Zhang   }
28c910923dSHong Zhang   for (i=0; i<is_max; i++) {ierr = ISDestroy(is[i]);CHKERRQ(ierr);}
2927f478b1SHong Zhang   ierr = ISExpandIndicesGeneral(N,bs,is_max,is_new,is);CHKERRQ(ierr);
30c910923dSHong Zhang   for (i=0; i<is_max; i++) {ierr = ISDestroy(is_new[i]);CHKERRQ(ierr);}
31632d0f97SHong Zhang   ierr = PetscFree(is_new);CHKERRQ(ierr);
32632d0f97SHong Zhang   PetscFunctionReturn(0);
33632d0f97SHong Zhang }
34632d0f97SHong Zhang 
354a69c536SBarry Smith typedef enum {MINE,OTHER} WhoseOwner;
360472cc68SHong Zhang /*  data1, odata1 and odata2 are packed in the format (for communication):
37a2a9f22aSHong Zhang        data[0]          = is_max, no of is
38a2a9f22aSHong Zhang        data[1]          = size of is[0]
39a2a9f22aSHong Zhang         ...
40a2a9f22aSHong Zhang        data[is_max]     = size of is[is_max-1]
41a2a9f22aSHong Zhang        data[is_max + 1] = data(is[0])
42a2a9f22aSHong Zhang         ...
43a2a9f22aSHong Zhang        data[is_max+1+sum(size of is[k]), k=0,...,i-1] = data(is[i])
44a2a9f22aSHong Zhang         ...
450472cc68SHong Zhang    data2 is packed in the format (for creating output is[]):
460472cc68SHong Zhang        data[0]          = is_max, no of is
470472cc68SHong Zhang        data[1]          = size of is[0]
480472cc68SHong Zhang         ...
490472cc68SHong Zhang        data[is_max]     = size of is[is_max-1]
500472cc68SHong Zhang        data[is_max + 1] = data(is[0])
510472cc68SHong Zhang         ...
520472cc68SHong Zhang        data[is_max + 1 + Mbs*i) = data(is[i])
530472cc68SHong Zhang         ...
54a2a9f22aSHong Zhang */
55632d0f97SHong Zhang #undef __FUNCT__
56632d0f97SHong Zhang #define __FUNCT__ "MatIncreaseOverlap_MPISBAIJ_Once"
5713f74950SBarry Smith static PetscErrorCode MatIncreaseOverlap_MPISBAIJ_Once(Mat C,PetscInt is_max,IS is[])
58632d0f97SHong Zhang {
59632d0f97SHong Zhang   Mat_MPISBAIJ  *c = (Mat_MPISBAIJ*)C->data;
606849ba73SBarry Smith   PetscErrorCode ierr;
6113f74950SBarry Smith   PetscMPIInt    size,rank,tag1,tag2,*len_s,nrqr,nrqs,*id_r1,*len_r1,flag,len;
625d0c19d7SBarry Smith   const PetscInt *idx_i;
635d0c19d7SBarry Smith   PetscInt       idx,isz,col,*n,*data1,**data1_start,*data2,*data2_i,*data,*data_i,
6413f74950SBarry Smith                  Mbs,i,j,k,*odata1,*odata2,
6513f74950SBarry Smith                  proc_id,**odata2_ptr,*ctable=0,*btable,len_max,len_est;
6613f74950SBarry Smith   PetscInt       proc_end=0,*iwork,len_unused,nodata2;
6713f74950SBarry Smith   PetscInt       ois_max; /* max no of is[] in each of processor */
68bfc6803cSHong Zhang   char           *t_p;
69632d0f97SHong Zhang   MPI_Comm       comm;
70e8527bf2SHong Zhang   MPI_Request    *s_waits1,*s_waits2,r_req;
71632d0f97SHong Zhang   MPI_Status     *s_status,r_status;
7245f43ab7SHong Zhang   PetscBT        *table;  /* mark indices of this processor's is[] */
73fc70d14eSHong Zhang   PetscBT        table_i;
74bfc6803cSHong Zhang   PetscBT        otable; /* mark indices of other processors' is[] */
75d0f46423SBarry Smith   PetscInt       bs=C->rmap->bs,Bn = c->B->cmap->n,Bnbs = Bn/bs,*Bowners;
7610eea884SHong Zhang   IS             garray_local,garray_gl;
775483b11dSHong Zhang 
78632d0f97SHong Zhang   PetscFunctionBegin;
797adad957SLisandro Dalcin   comm = ((PetscObject)C)->comm;
80632d0f97SHong Zhang   size = c->size;
81632d0f97SHong Zhang   rank = c->rank;
82632d0f97SHong Zhang   Mbs  = c->Mbs;
83632d0f97SHong Zhang 
84c910923dSHong Zhang   ierr = PetscObjectGetNewTag((PetscObject)C,&tag1);CHKERRQ(ierr);
85c910923dSHong Zhang   ierr = PetscObjectGetNewTag((PetscObject)C,&tag2);CHKERRQ(ierr);
86c910923dSHong Zhang 
87430a0127SHong Zhang   /* create tables used in
88430a0127SHong Zhang      step 1: table[i] - mark c->garray of proc [i]
8945f43ab7SHong Zhang      step 3: table[i] - mark indices of is[i] when whose=MINE
90430a0127SHong Zhang              table[0] - mark incideces of is[] when whose=OTHER */
91430a0127SHong Zhang   len = PetscMax(is_max, size);CHKERRQ(ierr);
9274ed9c26SBarry Smith   ierr = PetscMalloc2(len,PetscBT,&table,(Mbs/PETSC_BITS_PER_BYTE+1)*len,char,&t_p);CHKERRQ(ierr);
93430a0127SHong Zhang   for (i=0; i<len; i++) {
94430a0127SHong Zhang     table[i]  = t_p  + (Mbs/PETSC_BITS_PER_BYTE+1)*i;
95430a0127SHong Zhang   }
96430a0127SHong Zhang 
9713f74950SBarry Smith   ierr = MPI_Allreduce(&is_max,&ois_max,1,MPIU_INT,MPI_MAX,comm);CHKERRQ(ierr);
9810eea884SHong Zhang 
995483b11dSHong Zhang   /* 1. Send this processor's is[] to other processors */
1005483b11dSHong Zhang   /*---------------------------------------------------*/
101e8527bf2SHong Zhang   /* allocate spaces */
10213f74950SBarry Smith   ierr = PetscMalloc(is_max*sizeof(PetscInt),&n);CHKERRQ(ierr);
1035483b11dSHong Zhang   len = 0;
104c910923dSHong Zhang   for (i=0; i<is_max; i++) {
105632d0f97SHong Zhang     ierr = ISGetLocalSize(is[i],&n[i]);CHKERRQ(ierr);
106632d0f97SHong Zhang     len += n[i];
107632d0f97SHong Zhang   }
108958c9bccSBarry Smith   if (!len) {
1095483b11dSHong Zhang     is_max = 0;
1105483b11dSHong Zhang   } else {
1115483b11dSHong Zhang     len += 1 + is_max; /* max length of data1 for one processor */
1125483b11dSHong Zhang   }
1135483b11dSHong Zhang 
114430a0127SHong Zhang 
11513f74950SBarry Smith   ierr = PetscMalloc((size*len+1)*sizeof(PetscInt),&data1);CHKERRQ(ierr);
11613f74950SBarry Smith   ierr = PetscMalloc(size*sizeof(PetscInt*),&data1_start);CHKERRQ(ierr);
1175483b11dSHong Zhang   for (i=0; i<size; i++) data1_start[i] = data1 + i*len;
1185483b11dSHong Zhang 
11940cb64c9SJed Brown   ierr = PetscMalloc4(size,PetscInt,&len_s,size,PetscInt,&btable,size,PetscInt,&iwork,size+1,PetscInt,&Bowners);CHKERRQ(ierr);
120e8527bf2SHong Zhang 
12176f244e2SHong Zhang   /* gather c->garray from all processors */
12270b3c8c7SBarry Smith   ierr = ISCreateGeneral(comm,Bnbs,c->garray,PETSC_COPY_VALUES,&garray_local);CHKERRQ(ierr);
12376f244e2SHong Zhang   ierr = ISAllGather(garray_local, &garray_gl);CHKERRQ(ierr);
12476f244e2SHong Zhang   ierr = ISDestroy(garray_local);CHKERRQ(ierr);
125a7cc72afSBarry Smith   ierr = MPI_Allgather(&Bnbs,1,MPIU_INT,Bowners+1,1,MPIU_INT,comm);CHKERRQ(ierr);
12676f244e2SHong Zhang   Bowners[0] = 0;
12776f244e2SHong Zhang   for (i=0; i<size; i++) Bowners[i+1] += Bowners[i];
12876f244e2SHong Zhang 
1295483b11dSHong Zhang   if (is_max){
130430a0127SHong Zhang     /* hash table ctable which maps c->row to proc_id) */
13113f74950SBarry Smith     ierr = PetscMalloc(Mbs*sizeof(PetscInt),&ctable);CHKERRQ(ierr);
1325483b11dSHong Zhang     for (proc_id=0,j=0; proc_id<size; proc_id++) {
133288a2dc7SJed Brown       for (; j<C->rmap->range[proc_id+1]/bs; j++) {
1345483b11dSHong Zhang         ctable[j] = proc_id;
1355483b11dSHong Zhang       }
1365483b11dSHong Zhang     }
1375483b11dSHong Zhang 
138430a0127SHong Zhang     /* hash tables marking c->garray */
13910eea884SHong Zhang     ierr = ISGetIndices(garray_gl,&idx_i);
1405483b11dSHong Zhang     for (i=0; i<size; i++){
141430a0127SHong Zhang       table_i = table[i];
142430a0127SHong Zhang       ierr    = PetscBTMemzero(Mbs,table_i);CHKERRQ(ierr);
143430a0127SHong Zhang       for (j = Bowners[i]; j<Bowners[i+1]; j++){ /* go through B cols of proc[i]*/
144430a0127SHong Zhang         ierr = PetscBTSet(table_i,idx_i[j]);CHKERRQ(ierr);
1455483b11dSHong Zhang       }
1465483b11dSHong Zhang     }
14710eea884SHong Zhang     ierr = ISRestoreIndices(garray_gl,&idx_i);CHKERRQ(ierr);
1485483b11dSHong Zhang   }  /* if (is_max) */
14976f244e2SHong Zhang   ierr = ISDestroy(garray_gl);CHKERRQ(ierr);
1505483b11dSHong Zhang 
1515483b11dSHong Zhang   /* evaluate communication - mesg to who, length, and buffer space */
152e8527bf2SHong Zhang   for (i=0; i<size; i++) len_s[i] = 0;
1535483b11dSHong Zhang 
1545483b11dSHong Zhang   /* header of data1 */
1555483b11dSHong Zhang   for (proc_id=0; proc_id<size; proc_id++){
1565483b11dSHong Zhang     iwork[proc_id] = 0;
1575483b11dSHong Zhang     *data1_start[proc_id] = is_max;
1585483b11dSHong Zhang     data1_start[proc_id]++;
1595483b11dSHong Zhang     for (j=0; j<is_max; j++) {
1605483b11dSHong Zhang       if (proc_id == rank){
1615483b11dSHong Zhang         *data1_start[proc_id] = n[j];
1625483b11dSHong Zhang       } else {
1635483b11dSHong Zhang         *data1_start[proc_id] = 0;
1645483b11dSHong Zhang       }
1655483b11dSHong Zhang       data1_start[proc_id]++;
1665483b11dSHong Zhang     }
1675483b11dSHong Zhang   }
1685483b11dSHong Zhang 
1695483b11dSHong Zhang   for (i=0; i<is_max; i++) {
1705483b11dSHong Zhang     ierr = ISGetIndices(is[i],&idx_i);CHKERRQ(ierr);
1715483b11dSHong Zhang     for (j=0; j<n[i]; j++){
1725483b11dSHong Zhang       idx = idx_i[j];
1735483b11dSHong Zhang       *data1_start[rank] = idx; data1_start[rank]++; /* for local proccessing */
1745483b11dSHong Zhang       proc_end = ctable[idx];
1755483b11dSHong Zhang       for (proc_id=0;  proc_id<=proc_end; proc_id++){ /* for others to process */
176e8527bf2SHong Zhang         if (proc_id == rank ) continue; /* done before this loop */
177430a0127SHong Zhang         if (proc_id < proc_end && !PetscBTLookup(table[proc_id],idx))
178430a0127SHong Zhang           continue;   /* no need for sending idx to [proc_id] */
1795483b11dSHong Zhang         *data1_start[proc_id] = idx; data1_start[proc_id]++;
1805483b11dSHong Zhang         len_s[proc_id]++;
1815483b11dSHong Zhang       }
1825483b11dSHong Zhang     }
1835483b11dSHong Zhang     /* update header data */
1842cfbe0a4SHong Zhang     for (proc_id=0; proc_id<size; proc_id++){
1855483b11dSHong Zhang       if (proc_id== rank) continue;
1865483b11dSHong Zhang       *(data1 + proc_id*len + 1 + i) = len_s[proc_id] - iwork[proc_id];
1875483b11dSHong Zhang       iwork[proc_id] = len_s[proc_id] ;
1885483b11dSHong Zhang     }
1895483b11dSHong Zhang     ierr = ISRestoreIndices(is[i],&idx_i);CHKERRQ(ierr);
190e8527bf2SHong Zhang   }
1915483b11dSHong Zhang 
1925483b11dSHong Zhang   nrqs = 0; nrqr = 0;
1935483b11dSHong Zhang   for (i=0; i<size; i++){
1945483b11dSHong Zhang     data1_start[i] = data1 + i*len;
1955483b11dSHong Zhang     if (len_s[i]){
1965483b11dSHong Zhang       nrqs++;
1975483b11dSHong Zhang       len_s[i] += 1 + is_max; /* add no. of header msg */
1985483b11dSHong Zhang     }
1995483b11dSHong Zhang   }
2005483b11dSHong Zhang 
2015483b11dSHong Zhang   for (i=0; i<is_max; i++) {
202a2a9f22aSHong Zhang     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
203632d0f97SHong Zhang   }
204bfc6803cSHong Zhang   ierr = PetscFree(n);CHKERRQ(ierr);
20505b42c5fSBarry Smith   ierr = PetscFree(ctable);CHKERRQ(ierr);
2065483b11dSHong Zhang 
2075483b11dSHong Zhang   /* Determine the number of messages to expect, their lengths, from from-ids */
2085483b11dSHong Zhang   ierr = PetscGatherNumberOfMessages(comm,PETSC_NULL,len_s,&nrqr);CHKERRQ(ierr);
2095483b11dSHong Zhang   ierr = PetscGatherMessageLengths(comm,nrqs,nrqr,len_s,&id_r1,&len_r1);CHKERRQ(ierr);
210632d0f97SHong Zhang 
211632d0f97SHong Zhang   /*  Now  post the sends */
21274ed9c26SBarry Smith   ierr = PetscMalloc2(size,MPI_Request,&s_waits1,size,MPI_Request,&s_waits2);CHKERRQ(ierr);
213632d0f97SHong Zhang   k = 0;
2145483b11dSHong Zhang   for (proc_id=0; proc_id<size; proc_id++){  /* send data1 to processor [proc_id] */
2155483b11dSHong Zhang     if (len_s[proc_id]){
21613f74950SBarry Smith       ierr = MPI_Isend(data1_start[proc_id],len_s[proc_id],MPIU_INT,proc_id,tag1,comm,s_waits1+k);CHKERRQ(ierr);
217632d0f97SHong Zhang       k++;
218632d0f97SHong Zhang     }
219632d0f97SHong Zhang   }
220632d0f97SHong Zhang 
22145f43ab7SHong Zhang   /* 2. Receive other's is[] and process. Then send back */
222bfc6803cSHong Zhang   /*-----------------------------------------------------*/
2235483b11dSHong Zhang   len = 0;
2245483b11dSHong Zhang   for (i=0; i<nrqr; i++){
2255483b11dSHong Zhang     if (len_r1[i] > len)len = len_r1[i];
2265483b11dSHong Zhang   }
22745f43ab7SHong Zhang   ierr = PetscFree(len_r1);CHKERRQ(ierr);
22845f43ab7SHong Zhang   ierr = PetscFree(id_r1);CHKERRQ(ierr);
22945f43ab7SHong Zhang 
23045f43ab7SHong Zhang   for (proc_id=0; proc_id<size; proc_id++)
23145f43ab7SHong Zhang     len_s[proc_id] = iwork[proc_id] = 0;
23245f43ab7SHong Zhang 
23313f74950SBarry Smith   ierr = PetscMalloc((len+1)*sizeof(PetscInt),&odata1);CHKERRQ(ierr);
23413f74950SBarry Smith   ierr = PetscMalloc(size*sizeof(PetscInt**),&odata2_ptr);CHKERRQ(ierr);
23576f244e2SHong Zhang   ierr = PetscBTCreate(Mbs,otable);CHKERRQ(ierr);
23610eea884SHong Zhang 
23710eea884SHong Zhang   len_max = ois_max*(Mbs+1);  /* max space storing all is[] for each receive */
238240e5896SHong Zhang   len_est = 2*len_max; /* estimated space of storing is[] for all receiving messages */
23913f74950SBarry Smith   ierr = PetscMalloc((len_est+1)*sizeof(PetscInt),&odata2);CHKERRQ(ierr);
24010eea884SHong Zhang   nodata2 = 0;       /* nodata2+1: num of PetscMalloc(,&odata2_ptr[]) called */
241240e5896SHong Zhang   odata2_ptr[nodata2] = odata2;
24210eea884SHong Zhang   len_unused = len_est; /* unused space in the array odata2_ptr[nodata2]-- needs to be >= len_max  */
24310eea884SHong Zhang 
244632d0f97SHong Zhang   k = 0;
2455483b11dSHong Zhang   while (k < nrqr){
246632d0f97SHong Zhang     /* Receive messages */
247bfc6803cSHong Zhang     ierr = MPI_Iprobe(MPI_ANY_SOURCE,tag1,comm,&flag,&r_status);CHKERRQ(ierr);
248632d0f97SHong Zhang     if (flag){
24913f74950SBarry Smith       ierr = MPI_Get_count(&r_status,MPIU_INT,&len);CHKERRQ(ierr);
250632d0f97SHong Zhang       proc_id = r_status.MPI_SOURCE;
25113f74950SBarry Smith       ierr = MPI_Irecv(odata1,len,MPIU_INT,proc_id,r_status.MPI_TAG,comm,&r_req);CHKERRQ(ierr);
2525483b11dSHong Zhang       ierr = MPI_Wait(&r_req,&r_status);CHKERRQ(ierr);
253632d0f97SHong Zhang 
254fc70d14eSHong Zhang       /*  Process messages */
255240e5896SHong Zhang       /*  make sure there is enough unused space in odata2 array */
25610eea884SHong Zhang       if (len_unused < len_max){ /* allocate more space for odata2 */
25713f74950SBarry Smith         ierr = PetscMalloc((len_est+1)*sizeof(PetscInt),&odata2);CHKERRQ(ierr);
258240e5896SHong Zhang         odata2_ptr[++nodata2] = odata2;
25910eea884SHong Zhang         len_unused = len_est;
26010eea884SHong Zhang       }
26110eea884SHong Zhang 
26210eea884SHong Zhang       ierr = MatIncreaseOverlap_MPISBAIJ_Local(C,odata1,OTHER,odata2,&otable);CHKERRQ(ierr);
263a2a9f22aSHong Zhang       len = 1 + odata2[0];
264a2a9f22aSHong Zhang       for (i=0; i<odata2[0]; i++){
265a2a9f22aSHong Zhang         len += odata2[1 + i];
266fc70d14eSHong Zhang       }
267632d0f97SHong Zhang 
268632d0f97SHong Zhang       /* Send messages back */
26913f74950SBarry Smith       ierr = MPI_Isend(odata2,len,MPIU_INT,proc_id,tag2,comm,s_waits2+k);CHKERRQ(ierr);
270fc70d14eSHong Zhang       k++;
27110eea884SHong Zhang       odata2     += len;
27210eea884SHong Zhang       len_unused -= len;
27345f43ab7SHong Zhang       len_s[proc_id] = len; /* num of messages sending back to [proc_id] by this proc */
274632d0f97SHong Zhang     }
2755483b11dSHong Zhang   }
2765483b11dSHong Zhang   ierr = PetscFree(odata1);CHKERRQ(ierr);
27745f43ab7SHong Zhang   ierr = PetscBTDestroy(otable);CHKERRQ(ierr);
278632d0f97SHong Zhang 
27945f43ab7SHong Zhang   /* 3. Do local work on this processor's is[] */
28045f43ab7SHong Zhang   /*-------------------------------------------*/
28145f43ab7SHong Zhang   /* make sure there is enough unused space in odata2(=data) array */
28245f43ab7SHong Zhang   len_max = is_max*(Mbs+1); /* max space storing all is[] for this processor */
28310eea884SHong Zhang   if (len_unused < len_max){ /* allocate more space for odata2 */
28413f74950SBarry Smith     ierr = PetscMalloc((len_est+1)*sizeof(PetscInt),&odata2);CHKERRQ(ierr);
285240e5896SHong Zhang     odata2_ptr[++nodata2] = odata2;
28610eea884SHong Zhang     len_unused = len_est;
28710eea884SHong Zhang   }
288bfc6803cSHong Zhang 
289240e5896SHong Zhang   data = odata2;
29045f43ab7SHong Zhang   ierr = MatIncreaseOverlap_MPISBAIJ_Local(C,data1_start[rank],MINE,data,table);CHKERRQ(ierr);
29145f43ab7SHong Zhang   ierr = PetscFree(data1_start);CHKERRQ(ierr);
29245f43ab7SHong Zhang 
29345f43ab7SHong Zhang   /* 4. Receive work done on other processors, then merge */
29445f43ab7SHong Zhang   /*------------------------------------------------------*/
29545f43ab7SHong Zhang   /* get max number of messages that this processor expects to recv */
29613f74950SBarry Smith   ierr = MPI_Allreduce(len_s,iwork,size,MPIU_INT,MPI_MAX,comm);CHKERRQ(ierr);
29713f74950SBarry Smith   ierr = PetscMalloc((iwork[rank]+1)*sizeof(PetscInt),&data2);CHKERRQ(ierr);
29874ed9c26SBarry Smith   ierr = PetscFree4(len_s,btable,iwork,Bowners);CHKERRQ(ierr);
29945f43ab7SHong Zhang 
300632d0f97SHong Zhang   k = 0;
3015483b11dSHong Zhang   while (k < nrqs){
302632d0f97SHong Zhang     /* Receive messages */
303c910923dSHong Zhang     ierr = MPI_Iprobe(MPI_ANY_SOURCE,tag2,comm,&flag,&r_status);
304632d0f97SHong Zhang     if (flag){
30513f74950SBarry Smith       ierr = MPI_Get_count(&r_status,MPIU_INT,&len);CHKERRQ(ierr);
306632d0f97SHong Zhang       proc_id = r_status.MPI_SOURCE;
30713f74950SBarry Smith       ierr = MPI_Irecv(data2,len,MPIU_INT,proc_id,r_status.MPI_TAG,comm,&r_req);CHKERRQ(ierr);
3085483b11dSHong Zhang       ierr = MPI_Wait(&r_req,&r_status);CHKERRQ(ierr);
3095483b11dSHong Zhang       if (len > 1+is_max){ /* Add data2 into data */
3100472cc68SHong Zhang         data2_i = data2 + 1 + is_max;
311fc70d14eSHong Zhang         for (i=0; i<is_max; i++){
312fc70d14eSHong Zhang           table_i = table[i];
313bfc6803cSHong Zhang           data_i  = data + 1 + is_max + Mbs*i;
314bfc6803cSHong Zhang           isz     = data[1+i];
3150472cc68SHong Zhang           for (j=0; j<data2[1+i]; j++){
3160472cc68SHong Zhang             col = data2_i[j];
317bfc6803cSHong Zhang             if (!PetscBTLookupSet(table_i,col)) {data_i[isz++] = col;}
318fc70d14eSHong Zhang           }
319bfc6803cSHong Zhang           data[1+i] = isz;
3200472cc68SHong Zhang           if (i < is_max - 1) data2_i += data2[1+i];
321fc70d14eSHong Zhang         }
3225483b11dSHong Zhang       }
323632d0f97SHong Zhang       k++;
324632d0f97SHong Zhang     }
3255483b11dSHong Zhang   }
32645f43ab7SHong Zhang   ierr = PetscFree(data2);CHKERRQ(ierr);
32774ed9c26SBarry Smith   ierr = PetscFree2(table,t_p);CHKERRQ(ierr);
3285483b11dSHong Zhang 
3295483b11dSHong Zhang   /* phase 1 sends are complete */
3305483b11dSHong Zhang   ierr = PetscMalloc(size*sizeof(MPI_Status),&s_status);CHKERRQ(ierr);
3310c468ba9SBarry Smith   if (nrqs) {ierr = MPI_Waitall(nrqs,s_waits1,s_status);CHKERRQ(ierr);}
3325483b11dSHong Zhang   ierr = PetscFree(data1);CHKERRQ(ierr);
3335483b11dSHong Zhang 
334240e5896SHong Zhang   /* phase 2 sends are complete */
3350c468ba9SBarry Smith   if (nrqr){ierr = MPI_Waitall(nrqr,s_waits2,s_status);CHKERRQ(ierr);}
33674ed9c26SBarry Smith   ierr = PetscFree2(s_waits1,s_waits2);CHKERRQ(ierr);
33745f43ab7SHong Zhang   ierr = PetscFree(s_status);CHKERRQ(ierr);
338632d0f97SHong Zhang 
339c910923dSHong Zhang   /* 5. Create new is[] */
340c910923dSHong Zhang   /*--------------------*/
341c910923dSHong Zhang   for (i=0; i<is_max; i++) {
342bfc6803cSHong Zhang     data_i = data + 1 + is_max + Mbs*i;
34370b3c8c7SBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,data[1+i],data_i,PETSC_COPY_VALUES,is+i);CHKERRQ(ierr);
344632d0f97SHong Zhang   }
34545f43ab7SHong Zhang   for (k=0; k<=nodata2; k++){
34645f43ab7SHong Zhang     ierr = PetscFree(odata2_ptr[k]);CHKERRQ(ierr);
34745f43ab7SHong Zhang   }
34845f43ab7SHong Zhang   ierr = PetscFree(odata2_ptr);CHKERRQ(ierr);
3495483b11dSHong Zhang 
350632d0f97SHong Zhang   PetscFunctionReturn(0);
351632d0f97SHong Zhang }
352632d0f97SHong Zhang 
353632d0f97SHong Zhang #undef __FUNCT__
354632d0f97SHong Zhang #define __FUNCT__ "MatIncreaseOverlap_MPISBAIJ_Local"
355632d0f97SHong Zhang /*
356dc008846SHong Zhang    MatIncreaseOverlap_MPISBAIJ_Local - Called by MatIncreaseOverlap, to do
357632d0f97SHong Zhang        the work on the local processor.
358632d0f97SHong Zhang 
359632d0f97SHong Zhang      Inputs:
360632d0f97SHong Zhang       C      - MAT_MPISBAIJ;
361bfc6803cSHong Zhang       data   - holds is[]. See MatIncreaseOverlap_MPISBAIJ_Once() for the format.
362bfc6803cSHong Zhang       whose  - whose is[] to be processed,
363bfc6803cSHong Zhang                MINE:  this processor's is[]
364bfc6803cSHong Zhang                OTHER: other processor's is[]
365632d0f97SHong Zhang      Output:
36610eea884SHong Zhang        nidx  - whose = MINE:
3670472cc68SHong Zhang                      holds input and newly found indices in the same format as data
3680472cc68SHong Zhang                whose = OTHER:
3690472cc68SHong Zhang                      only holds the newly found indices
3700472cc68SHong Zhang        table - table[i]: mark the indices of is[i], i=0,...,is_max. Used only in the case 'whose=MINE'.
371632d0f97SHong Zhang */
37276f244e2SHong Zhang /* Would computation be reduced by swapping the loop 'for each is' and 'for each row'? */
37313f74950SBarry Smith static PetscErrorCode MatIncreaseOverlap_MPISBAIJ_Local(Mat C,PetscInt *data,PetscInt whose,PetscInt *nidx,PetscBT *table)
374632d0f97SHong Zhang {
375632d0f97SHong Zhang   Mat_MPISBAIJ   *c = (Mat_MPISBAIJ*)C->data;
376dc008846SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)(c->A)->data;
377dc008846SHong Zhang   Mat_SeqBAIJ    *b = (Mat_SeqBAIJ*)(c->B)->data;
378dfbe8321SBarry Smith   PetscErrorCode ierr;
37913f74950SBarry Smith   PetscInt       row,mbs,Mbs,*nidx_i,col,col_max,isz,isz0,*ai,*aj,*bi,*bj,*garray,rstart,l;
38013f74950SBarry Smith   PetscInt       a_start,a_end,b_start,b_end,i,j,k,is_max,*idx_i,n;
381bfc6803cSHong Zhang   PetscBT        table0;  /* mark the indices of input is[] for look up */
382bfc6803cSHong Zhang   PetscBT        table_i; /* poits to i-th table. When whose=OTHER, a single table is used for all is[] */
383632d0f97SHong Zhang 
384632d0f97SHong Zhang   PetscFunctionBegin;
38531f99336SHong Zhang   Mbs = c->Mbs; mbs = a->mbs;
386dc008846SHong Zhang   ai = a->i; aj = a->j;
387dc008846SHong Zhang   bi = b->i; bj = b->j;
388632d0f97SHong Zhang   garray = c->garray;
389899cda47SBarry Smith   rstart = c->rstartbs;
390dc008846SHong Zhang   is_max = data[0];
391632d0f97SHong Zhang 
392fc70d14eSHong Zhang   ierr = PetscBTCreate(Mbs,table0);CHKERRQ(ierr);
393fc70d14eSHong Zhang 
394fc70d14eSHong Zhang   nidx[0] = is_max;
395fc70d14eSHong Zhang   idx_i   = data + is_max + 1; /* ptr to input is[0] array */
396bfc6803cSHong Zhang   nidx_i  = nidx + is_max + 1; /* ptr to output is[0] array */
397dc008846SHong Zhang   for (i=0; i<is_max; i++) { /* for each is */
398dc008846SHong Zhang     isz  = 0;
399fc70d14eSHong Zhang     n = data[1+i]; /* size of input is[i] */
400dc008846SHong Zhang 
40176f244e2SHong Zhang     /* initialize and set table_i(mark idx and nidx) and table0(only mark idx) */
402bfc6803cSHong Zhang     if (whose == MINE){ /* process this processor's is[] */
403bfc6803cSHong Zhang       table_i = table[i];
4040472cc68SHong Zhang       nidx_i  = nidx + 1+ is_max + Mbs*i;
405bfc6803cSHong Zhang     } else {            /* process other processor's is[] - only use one temp table */
406430a0127SHong Zhang       table_i = table[0];
407bfc6803cSHong Zhang     }
408bfc6803cSHong Zhang     ierr = PetscBTMemzero(Mbs,table_i);CHKERRQ(ierr);
409bfc6803cSHong Zhang     ierr = PetscBTMemzero(Mbs,table0);CHKERRQ(ierr);
41076f244e2SHong Zhang     if (n==0) {
41176f244e2SHong Zhang        nidx[1+i] = 0; /* size of new is[i] */
41276f244e2SHong Zhang        continue;
41376f244e2SHong Zhang     }
41476f244e2SHong Zhang 
41576f244e2SHong Zhang     isz0 = 0; col_max = 0;
416dc008846SHong Zhang     for (j=0; j<n; j++){
417dc008846SHong Zhang       col = idx_i[j];
418e32f2f54SBarry Smith       if (col >= Mbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"index col %D >= Mbs %D",col,Mbs);
419bfc6803cSHong Zhang       if(!PetscBTLookupSet(table_i,col)) {
420bfc6803cSHong Zhang         ierr = PetscBTSet(table0,col);CHKERRQ(ierr);
4210472cc68SHong Zhang         if (whose == MINE) {nidx_i[isz0] = col;}
422dbe03f88SHong Zhang         if (col_max < col) col_max = col;
423bfc6803cSHong Zhang         isz0++;
424bfc6803cSHong Zhang       }
425632d0f97SHong Zhang     }
426dc008846SHong Zhang 
4270472cc68SHong Zhang     if (whose == MINE) {isz = isz0;}
428fc70d14eSHong Zhang     k = 0;  /* no. of indices from input is[i] that have been examined */
429dc008846SHong Zhang     for (row=0; row<mbs; row++){
430dc008846SHong Zhang       a_start = ai[row]; a_end = ai[row+1];
431dc008846SHong Zhang       b_start = bi[row]; b_end = bi[row+1];
4320472cc68SHong Zhang       if (PetscBTLookup(table0,row+rstart)){ /* row is on input is[i]:
4330472cc68SHong Zhang                                                 do row search: collect all col in this row */
434dc008846SHong Zhang         for (l = a_start; l<a_end ; l++){ /* Amat */
435dc008846SHong Zhang           col = aj[l] + rstart;
436fc70d14eSHong Zhang           if (!PetscBTLookupSet(table_i,col)) {nidx_i[isz++] = col;}
437dc008846SHong Zhang         }
438dc008846SHong Zhang         for (l = b_start; l<b_end ; l++){ /* Bmat */
439dc008846SHong Zhang           col = garray[bj[l]];
440fc70d14eSHong Zhang           if (!PetscBTLookupSet(table_i,col)) {nidx_i[isz++] = col;}
441dc008846SHong Zhang         }
442dc008846SHong Zhang         k++;
443dc008846SHong Zhang         if (k >= isz0) break; /* for (row=0; row<mbs; row++) */
4440472cc68SHong Zhang       } else { /* row is not on input is[i]:
4450472cc68SHong Zhang                   do col serach: add row onto nidx_i if there is a col in nidx_i */
446dc008846SHong Zhang         for (l = a_start; l<a_end ; l++){ /* Amat */
447dc008846SHong Zhang           col = aj[l] + rstart;
44876f244e2SHong Zhang           if (col > col_max) break;
449dc008846SHong Zhang           if (PetscBTLookup(table0,col)){
450fc70d14eSHong Zhang             if (!PetscBTLookupSet(table_i,row+rstart)) {nidx_i[isz++] = row+rstart;}
451dc008846SHong Zhang             break; /* for l = start; l<end ; l++) */
452632d0f97SHong Zhang           }
453632d0f97SHong Zhang         }
454dc008846SHong Zhang         for (l = b_start; l<b_end ; l++){ /* Bmat */
455dc008846SHong Zhang           col = garray[bj[l]];
45676f244e2SHong Zhang           if (col > col_max) break;
457dc008846SHong Zhang           if (PetscBTLookup(table0,col)){
458fc70d14eSHong Zhang             if (!PetscBTLookupSet(table_i,row+rstart)) {nidx_i[isz++] = row+rstart;}
459dc008846SHong Zhang             break; /* for l = start; l<end ; l++) */
460632d0f97SHong Zhang           }
461dc008846SHong Zhang         }
462dc008846SHong Zhang       }
463bfc6803cSHong Zhang     }
464dc008846SHong Zhang 
465dc008846SHong Zhang     if (i < is_max - 1){
466fc70d14eSHong Zhang       idx_i  += n;   /* ptr to input is[i+1] array */
467bfc6803cSHong Zhang       nidx_i += isz; /* ptr to output is[i+1] array */
468dc008846SHong Zhang     }
469fc70d14eSHong Zhang     nidx[1+i] = isz; /* size of new is[i] */
4701ab97a2aSSatish Balay   } /* for each is */
471dc008846SHong Zhang   ierr = PetscBTDestroy(table0);CHKERRQ(ierr);
472632d0f97SHong Zhang 
473632d0f97SHong Zhang   PetscFunctionReturn(0);
474632d0f97SHong Zhang }
475632d0f97SHong Zhang 
476632d0f97SHong Zhang 
477