xref: /petsc/src/mat/impls/sbaij/mpi/sbaijov.c (revision dbe03f88ef651362992d50b70cdfc3144f10fda2)
1632d0f97SHong Zhang /*$Id: sbaijov.c,v 1.65 2001/08/06 21:15:42 bsmith Exp $*/
2632d0f97SHong Zhang 
3632d0f97SHong Zhang /*
4632d0f97SHong Zhang    Routines to compute overlapping regions of a parallel MPI matrix.
5632d0f97SHong Zhang    Used for finding submatrices that were shared across processors.
6632d0f97SHong Zhang */
7632d0f97SHong Zhang #include "src/mat/impls/sbaij/mpi/mpisbaij.h"
8632d0f97SHong Zhang #include "petscbt.h"
9632d0f97SHong Zhang 
10632d0f97SHong Zhang static int MatIncreaseOverlap_MPISBAIJ_Once(Mat,int,IS*);
1110eea884SHong Zhang static int MatIncreaseOverlap_MPISBAIJ_Local(Mat,int*,int,int*,PetscBT*);
12632d0f97SHong Zhang 
13632d0f97SHong Zhang #undef __FUNCT__
14632d0f97SHong Zhang #define __FUNCT__ "MatIncreaseOverlap_MPISBAIJ"
15c910923dSHong Zhang int MatIncreaseOverlap_MPISBAIJ(Mat C,int is_max,IS is[],int ov)
16632d0f97SHong Zhang {
17d9489beaSHong Zhang   Mat_MPISBAIJ  *c = (Mat_MPISBAIJ*)C->data;
18d9489beaSHong Zhang   int           i,ierr,N=C->N, bs=c->bs;
19632d0f97SHong Zhang   IS            *is_new;
20632d0f97SHong Zhang 
21632d0f97SHong Zhang   PetscFunctionBegin;
22c910923dSHong Zhang   ierr = PetscMalloc(is_max*sizeof(IS),&is_new);CHKERRQ(ierr);
23632d0f97SHong Zhang   /* Convert the indices into block format */
2427f478b1SHong Zhang   ierr = ISCompressIndicesGeneral(N,bs,is_max,is,is_new);CHKERRQ(ierr);
25632d0f97SHong Zhang   if (ov < 0){ SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Negative overlap specified\n");}
26632d0f97SHong Zhang   for (i=0; i<ov; ++i) {
27c910923dSHong Zhang     ierr = MatIncreaseOverlap_MPISBAIJ_Once(C,is_max,is_new);CHKERRQ(ierr);
28632d0f97SHong Zhang   }
29c910923dSHong Zhang   for (i=0; i<is_max; i++) {ierr = ISDestroy(is[i]);CHKERRQ(ierr);}
3027f478b1SHong Zhang   ierr = ISExpandIndicesGeneral(N,bs,is_max,is_new,is);CHKERRQ(ierr);
31c910923dSHong Zhang   for (i=0; i<is_max; i++) {ierr = ISDestroy(is_new[i]);CHKERRQ(ierr);}
32632d0f97SHong Zhang   ierr = PetscFree(is_new);CHKERRQ(ierr);
33632d0f97SHong Zhang   PetscFunctionReturn(0);
34632d0f97SHong Zhang }
35632d0f97SHong Zhang 
364a69c536SBarry Smith typedef enum {MINE,OTHER} WhoseOwner;
370472cc68SHong Zhang /*  data1, odata1 and odata2 are packed in the format (for communication):
38a2a9f22aSHong Zhang        data[0]          = is_max, no of is
39a2a9f22aSHong Zhang        data[1]          = size of is[0]
40a2a9f22aSHong Zhang         ...
41a2a9f22aSHong Zhang        data[is_max]     = size of is[is_max-1]
42a2a9f22aSHong Zhang        data[is_max + 1] = data(is[0])
43a2a9f22aSHong Zhang         ...
44a2a9f22aSHong Zhang        data[is_max+1+sum(size of is[k]), k=0,...,i-1] = data(is[i])
45a2a9f22aSHong Zhang         ...
460472cc68SHong Zhang    data2 is packed in the format (for creating output is[]):
470472cc68SHong Zhang        data[0]          = is_max, no of is
480472cc68SHong Zhang        data[1]          = size of is[0]
490472cc68SHong Zhang         ...
500472cc68SHong Zhang        data[is_max]     = size of is[is_max-1]
510472cc68SHong Zhang        data[is_max + 1] = data(is[0])
520472cc68SHong Zhang         ...
530472cc68SHong Zhang        data[is_max + 1 + Mbs*i) = data(is[i])
540472cc68SHong Zhang         ...
55a2a9f22aSHong Zhang */
56632d0f97SHong Zhang #undef __FUNCT__
57632d0f97SHong Zhang #define __FUNCT__ "MatIncreaseOverlap_MPISBAIJ_Once"
58c910923dSHong Zhang static int MatIncreaseOverlap_MPISBAIJ_Once(Mat C,int is_max,IS is[])
59632d0f97SHong Zhang {
60632d0f97SHong Zhang   Mat_MPISBAIJ  *c = (Mat_MPISBAIJ*)C->data;
615483b11dSHong Zhang   int         len,idx,*idx_i,isz,col,*n,*data1,**data1_start,*data2,*data2_i,*data,*data_i,
62e8527bf2SHong Zhang               size,rank,Mbs,i,j,k,ierr,nrqs,nrqr,*odata1,*odata2,
6310eea884SHong Zhang               tag1,tag2,flag,proc_id,**odata2_ptr,*ctable=0,*btable,len_max,len_est;
6410eea884SHong Zhang   int         *id_r1,*len_r1,proc_end=0,*iwork,*len_s,len_unused,nodata2;
6510eea884SHong Zhang   int         ois_max; /* max no of is[] in each of processor */
66bfc6803cSHong Zhang   char        *t_p;
67632d0f97SHong Zhang   MPI_Comm    comm;
68e8527bf2SHong Zhang   MPI_Request *s_waits1,*s_waits2,r_req;
69632d0f97SHong Zhang   MPI_Status  *s_status,r_status;
705483b11dSHong Zhang   PetscBT     *table=0;  /* mark indices of this processor's is[] */
71fc70d14eSHong Zhang   PetscBT     table_i;
72bfc6803cSHong Zhang   PetscBT     otable; /* mark indices of other processors' is[] */
73e8527bf2SHong Zhang   int         bs=c->bs,Bn = c->B->n,Bnbs = Bn/bs,*Bowners;
7410eea884SHong Zhang   IS          garray_local,garray_gl;
755483b11dSHong Zhang 
76632d0f97SHong Zhang   PetscFunctionBegin;
77632d0f97SHong Zhang 
78632d0f97SHong Zhang   comm = C->comm;
79632d0f97SHong Zhang   size = c->size;
80632d0f97SHong Zhang   rank = c->rank;
81632d0f97SHong Zhang   Mbs  = c->Mbs;
82632d0f97SHong Zhang 
83c910923dSHong Zhang   ierr = PetscObjectGetNewTag((PetscObject)C,&tag1);CHKERRQ(ierr);
84c910923dSHong Zhang   ierr = PetscObjectGetNewTag((PetscObject)C,&tag2);CHKERRQ(ierr);
85c910923dSHong Zhang 
8610eea884SHong Zhang   ierr = MPI_Allreduce(&is_max,&ois_max,1,MPI_INT,MPI_MAX,comm);CHKERRQ(ierr);
8710eea884SHong Zhang 
885483b11dSHong Zhang   /* 1. Send this processor's is[] to other processors */
895483b11dSHong Zhang   /*---------------------------------------------------*/
90e8527bf2SHong Zhang   /* allocate spaces */
915483b11dSHong Zhang   ierr = PetscMalloc(is_max*sizeof(int),&n);CHKERRQ(ierr);
925483b11dSHong Zhang   len = 0;
93c910923dSHong Zhang   for (i=0; i<is_max; i++) {
94632d0f97SHong Zhang     ierr = ISGetLocalSize(is[i],&n[i]);CHKERRQ(ierr);
95632d0f97SHong Zhang     len += n[i];
96632d0f97SHong Zhang   }
975483b11dSHong Zhang   if (len == 0) {
985483b11dSHong Zhang     is_max = 0;
995483b11dSHong Zhang   } else {
1005483b11dSHong Zhang     len += 1 + is_max; /* max length of data1 for one processor */
1015483b11dSHong Zhang   }
1025483b11dSHong Zhang 
1035483b11dSHong Zhang   ierr = PetscMalloc((size*len+1)*sizeof(int),&data1);CHKERRQ(ierr);
1045483b11dSHong Zhang   ierr = PetscMalloc(size*sizeof(int*),&data1_start);CHKERRQ(ierr);
1055483b11dSHong Zhang   for (i=0; i<size; i++) data1_start[i] = data1 + i*len;
1065483b11dSHong Zhang 
10710eea884SHong Zhang   ierr = PetscMalloc((size*4+1)*sizeof(int),&len_s);CHKERRQ(ierr);
108e8527bf2SHong Zhang   btable  = len_s + size;
109e8527bf2SHong Zhang   iwork   = btable + size;
11010eea884SHong Zhang   Bowners = iwork + size;
111e8527bf2SHong Zhang 
11276f244e2SHong Zhang   /* gather c->garray from all processors */
11376f244e2SHong Zhang   ierr = ISCreateGeneral(comm,Bnbs,c->garray,&garray_local);CHKERRQ(ierr);
11476f244e2SHong Zhang   ierr = ISAllGather(garray_local, &garray_gl);CHKERRQ(ierr);
11576f244e2SHong Zhang   ierr = ISDestroy(garray_local);CHKERRQ(ierr);
11676f244e2SHong Zhang   ierr = MPI_Allgather(&Bnbs,1,MPI_INT,Bowners+1,1,MPI_INT,comm);CHKERRQ(ierr);
11776f244e2SHong Zhang   Bowners[0] = 0;
11876f244e2SHong Zhang   for (i=0; i<size; i++) Bowners[i+1] += Bowners[i];
11976f244e2SHong Zhang 
1205483b11dSHong Zhang   if (is_max){
1215483b11dSHong Zhang     /* create hash table ctable which maps c->row to proc_id) */
1225483b11dSHong Zhang     ierr = PetscMalloc(Mbs*sizeof(int),&ctable);CHKERRQ(ierr);
1235483b11dSHong Zhang     for (proc_id=0,j=0; proc_id<size; proc_id++) {
1245483b11dSHong Zhang       for (; j<c->rowners[proc_id+1]; j++) {
1255483b11dSHong Zhang         ctable[j] = proc_id;
1265483b11dSHong Zhang       }
1275483b11dSHong Zhang     }
1285483b11dSHong Zhang 
1295483b11dSHong Zhang     /* create tables for marking indices */
13010eea884SHong Zhang     len_max = is_max*sizeof(PetscBT) + (Mbs/PETSC_BITS_PER_BYTE+1)*is_max*sizeof(char) + 1;
13110eea884SHong Zhang     ierr = PetscMalloc(len_max,&table);CHKERRQ(ierr);
1325483b11dSHong Zhang     t_p  = (char *)(table + is_max);
133a2a9f22aSHong Zhang     for (i=0; i<is_max; i++) {
1345483b11dSHong Zhang       table[i]  = t_p  + (Mbs/PETSC_BITS_PER_BYTE+1)*i;
1355483b11dSHong Zhang     }
1365483b11dSHong Zhang 
137e8527bf2SHong Zhang     /* hash table table_i[idx] = 1 if idx is on is[] array
1385483b11dSHong Zhang                                = 0 otherwise */
1395483b11dSHong Zhang     table_i = table[0];
1405483b11dSHong Zhang     ierr    = PetscBTMemzero(Mbs,table_i);CHKERRQ(ierr);
1415483b11dSHong Zhang     for (i=0; i<is_max; i++){
142a2a9f22aSHong Zhang       ierr = ISGetIndices(is[i],&idx_i);CHKERRQ(ierr);
1435483b11dSHong Zhang       for (j=0; j<n[i]; j++){
1445483b11dSHong Zhang         idx = idx_i[j];
1455483b11dSHong Zhang         ierr = PetscBTSet(table_i,idx);CHKERRQ(ierr);
146632d0f97SHong Zhang       }
147a2a9f22aSHong Zhang       ierr = ISRestoreIndices(is[i],&idx_i);CHKERRQ(ierr);
1485483b11dSHong Zhang     }
1495483b11dSHong Zhang 
150e8527bf2SHong Zhang     /* hash table btable[id_proc] = 1 if a col index of B-matrix in [id_proc] is on is[] array
151e8527bf2SHong Zhang                                   = 0 otherwise */
15210eea884SHong Zhang     ierr = ISGetIndices(garray_gl,&idx_i);
1535483b11dSHong Zhang     for (i=0; i<size; i++){
1545483b11dSHong Zhang       btable[i] = 0;
1555483b11dSHong Zhang       for (j = Bowners[i]; j<Bowners[i+1]; j++){ /* go through B cols */
156e8527bf2SHong Zhang         idx = idx_i[j];
1575483b11dSHong Zhang         if(PetscBTLookup(table_i,idx)){
1585483b11dSHong Zhang           btable[i] = 1;
1595483b11dSHong Zhang           break;
1605483b11dSHong Zhang         }
1615483b11dSHong Zhang       }
1625483b11dSHong Zhang     }
16310eea884SHong Zhang     ierr = ISRestoreIndices(garray_gl,&idx_i);CHKERRQ(ierr);
1645483b11dSHong Zhang   }  /* if (is_max) */
16576f244e2SHong Zhang   ierr = ISDestroy(garray_gl);CHKERRQ(ierr);
1665483b11dSHong Zhang 
1675483b11dSHong Zhang   /* evaluate communication - mesg to who, length, and buffer space */
168e8527bf2SHong Zhang   for (i=0; i<size; i++) len_s[i] = 0;
1695483b11dSHong Zhang 
1705483b11dSHong Zhang   /* header of data1 */
1715483b11dSHong Zhang   for (proc_id=0; proc_id<size; proc_id++){
1725483b11dSHong Zhang     iwork[proc_id] = 0;
1735483b11dSHong Zhang     *data1_start[proc_id] = is_max;
1745483b11dSHong Zhang     data1_start[proc_id]++;
1755483b11dSHong Zhang     for (j=0; j<is_max; j++) {
1765483b11dSHong Zhang       if (proc_id == rank){
1775483b11dSHong Zhang         *data1_start[proc_id] = n[j];
1785483b11dSHong Zhang       } else {
1795483b11dSHong Zhang         *data1_start[proc_id] = 0;
1805483b11dSHong Zhang       }
1815483b11dSHong Zhang       data1_start[proc_id]++;
1825483b11dSHong Zhang     }
1835483b11dSHong Zhang   }
1845483b11dSHong Zhang 
1855483b11dSHong Zhang   for (i=0; i<is_max; i++) {
1865483b11dSHong Zhang     ierr = ISGetIndices(is[i],&idx_i);CHKERRQ(ierr);
1875483b11dSHong Zhang     for (j=0; j<n[i]; j++){
1885483b11dSHong Zhang       idx = idx_i[j];
1895483b11dSHong Zhang       *data1_start[rank] = idx; data1_start[rank]++; /* for local proccessing */
1905483b11dSHong Zhang       proc_end = ctable[idx];
1915483b11dSHong Zhang       for (proc_id=0;  proc_id<=proc_end; proc_id++){ /* for others to process */
192e8527bf2SHong Zhang         if (proc_id == rank ) continue; /* done before this loop */
193e8527bf2SHong Zhang         if (proc_id < proc_end && !btable[proc_id]) continue;   /* no need for sending idx to [proc_id] */
1945483b11dSHong Zhang         *data1_start[proc_id] = idx; data1_start[proc_id]++;
1955483b11dSHong Zhang         len_s[proc_id]++;
1965483b11dSHong Zhang       }
1975483b11dSHong Zhang     }
1985483b11dSHong Zhang     /* update header data */
1992cfbe0a4SHong Zhang     for (proc_id=0; proc_id<size; proc_id++){
2005483b11dSHong Zhang       if (proc_id== rank) continue;
2015483b11dSHong Zhang       *(data1 + proc_id*len + 1 + i) = len_s[proc_id] - iwork[proc_id];
2025483b11dSHong Zhang       iwork[proc_id] = len_s[proc_id] ;
2035483b11dSHong Zhang     }
2045483b11dSHong Zhang     ierr = ISRestoreIndices(is[i],&idx_i);CHKERRQ(ierr);
205e8527bf2SHong Zhang   }
2065483b11dSHong Zhang 
2075483b11dSHong Zhang   nrqs = 0; nrqr = 0;
2085483b11dSHong Zhang   for (i=0; i<size; i++){
2095483b11dSHong Zhang     data1_start[i] = data1 + i*len;
2105483b11dSHong Zhang     if (len_s[i]){
2115483b11dSHong Zhang       nrqs++;
2125483b11dSHong Zhang       len_s[i] += 1 + is_max; /* add no. of header msg */
2135483b11dSHong Zhang     }
2145483b11dSHong Zhang   }
2155483b11dSHong Zhang 
2165483b11dSHong Zhang   for (i=0; i<is_max; i++) {
217a2a9f22aSHong Zhang     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
218632d0f97SHong Zhang   }
219bfc6803cSHong Zhang   ierr = PetscFree(n);CHKERRQ(ierr);
2205483b11dSHong Zhang   if (ctable){ierr = PetscFree(ctable);CHKERRQ(ierr);}
2215483b11dSHong Zhang 
2225483b11dSHong Zhang   /* Determine the number of messages to expect, their lengths, from from-ids */
2235483b11dSHong Zhang   ierr = PetscGatherNumberOfMessages(comm,PETSC_NULL,len_s,&nrqr);CHKERRQ(ierr);
2245483b11dSHong Zhang   ierr = PetscGatherMessageLengths(comm,nrqs,nrqr,len_s,&id_r1,&len_r1);CHKERRQ(ierr);
2255483b11dSHong Zhang   /* ierr = PetscPrintf(PETSC_COMM_SELF, "[%d] nrqs: %d, nrqr: %d\n",rank,nrqs,nrqr); */
226632d0f97SHong Zhang 
227632d0f97SHong Zhang   /*  Now  post the sends */
2285483b11dSHong Zhang   ierr = PetscMalloc(2*size*sizeof(MPI_Request),&s_waits1);CHKERRQ(ierr);
2295483b11dSHong Zhang   s_waits2 = s_waits1 + size;
230632d0f97SHong Zhang   k = 0;
2315483b11dSHong Zhang   for (proc_id=0; proc_id<size; proc_id++){  /* send data1 to processor [proc_id] */
2325483b11dSHong Zhang     if (len_s[proc_id]){
2335483b11dSHong Zhang       ierr = MPI_Isend(data1_start[proc_id],len_s[proc_id],MPI_INT,proc_id,tag1,comm,s_waits1+k);CHKERRQ(ierr);
234632d0f97SHong Zhang       k++;
235632d0f97SHong Zhang     }
236632d0f97SHong Zhang   }
237632d0f97SHong Zhang 
238dc008846SHong Zhang   /* 2. Do local work on this processor's is[] */
239dc008846SHong Zhang   /*-------------------------------------------*/
24010eea884SHong Zhang   len_max = is_max*(Mbs+1); /* max space storing all is[] for this processor */
24110eea884SHong Zhang   ierr = PetscMalloc((len_max+1)*sizeof(int),&data);CHKERRQ(ierr);
24210eea884SHong Zhang   ierr = MatIncreaseOverlap_MPISBAIJ_Local(C,data1_start[rank],MINE,data,table);CHKERRQ(ierr);
243e8527bf2SHong Zhang   ierr = PetscFree(data1_start);CHKERRQ(ierr);
244632d0f97SHong Zhang 
245632d0f97SHong Zhang   /* 3. Receive other's is[] and process. Then send back */
246bfc6803cSHong Zhang   /*-----------------------------------------------------*/
2475483b11dSHong Zhang   len = 0;
2485483b11dSHong Zhang   for (i=0; i<nrqr; i++){
2495483b11dSHong Zhang     if (len_r1[i] > len)len = len_r1[i];
2505483b11dSHong Zhang     /* ierr = PetscPrintf(PETSC_COMM_SELF, "[%d] expect to recv len=%d from [%d]\n",rank,len_r1[i],id_r1[i]); */
2515483b11dSHong Zhang   }
2525483b11dSHong Zhang   ierr = PetscMalloc((len+1)*sizeof(int),&odata1);CHKERRQ(ierr);
25310eea884SHong Zhang   ierr = PetscMalloc((size+1)*sizeof(int**),&odata2_ptr);CHKERRQ(ierr);
25476f244e2SHong Zhang   ierr = PetscBTCreate(Mbs,otable);CHKERRQ(ierr);
25510eea884SHong Zhang 
25610eea884SHong Zhang   len_max = ois_max*(Mbs+1);  /* max space storing all is[] for each receive */
25710eea884SHong Zhang   len_est = 2*len_max; /* estimated space of storing is[] for all receiving messages */
25810eea884SHong Zhang   nodata2 = 0;       /* nodata2+1: num of PetscMalloc(,&odata2_ptr[]) called */
25910eea884SHong Zhang   ierr = PetscMalloc((len_est+1)*sizeof(int),&odata2_ptr[nodata2]);CHKERRQ(ierr);
26010eea884SHong Zhang   odata2     = odata2_ptr[nodata2];
26110eea884SHong Zhang   len_unused = len_est; /* unused space in the array odata2_ptr[nodata2]-- needs to be >= len_max  */
26210eea884SHong Zhang 
263632d0f97SHong Zhang   k = 0;
2645483b11dSHong Zhang   while (k < nrqr){
265632d0f97SHong Zhang     /* Receive messages */
266bfc6803cSHong Zhang     ierr = MPI_Iprobe(MPI_ANY_SOURCE,tag1,comm,&flag,&r_status);CHKERRQ(ierr);
267632d0f97SHong Zhang     if (flag){
268bfc6803cSHong Zhang       ierr = MPI_Get_count(&r_status,MPI_INT,&len);CHKERRQ(ierr);
269632d0f97SHong Zhang       proc_id = r_status.MPI_SOURCE;
270bfc6803cSHong Zhang       ierr = MPI_Irecv(odata1,len,MPI_INT,proc_id,r_status.MPI_TAG,comm,&r_req);CHKERRQ(ierr);
2715483b11dSHong Zhang       ierr = MPI_Wait(&r_req,&r_status);CHKERRQ(ierr);
2725483b11dSHong Zhang       /* ierr = PetscPrintf(PETSC_COMM_SELF, " [%d] recv %d from [%d]\n",rank,len,proc_id); */
273632d0f97SHong Zhang 
274fc70d14eSHong Zhang       /*  Process messages */
27510eea884SHong Zhang       /*  check if there is enough unused space in odata2 array */
27610eea884SHong Zhang       if (len_unused < len_max){ /* allocate more space for odata2 */
27710eea884SHong Zhang         ierr = PetscMalloc((len_est+1)*sizeof(int),&odata2_ptr[++nodata2]);CHKERRQ(ierr);
27810eea884SHong Zhang         odata2 = odata2_ptr[nodata2];
27910eea884SHong Zhang         len_unused = len_est;
28010eea884SHong Zhang         /* ierr = PetscPrintf(PETSC_COMM_SELF, " [%d] Malloc odata2, nodata2: %d\n",rank,nodata2); */
28110eea884SHong Zhang       }
28210eea884SHong Zhang 
28310eea884SHong Zhang       ierr = MatIncreaseOverlap_MPISBAIJ_Local(C,odata1,OTHER,odata2,&otable);CHKERRQ(ierr);
284a2a9f22aSHong Zhang       len = 1 + odata2[0];
285a2a9f22aSHong Zhang       for (i=0; i<odata2[0]; i++){
286a2a9f22aSHong Zhang         len += odata2[1 + i];
287fc70d14eSHong Zhang       }
288632d0f97SHong Zhang 
289632d0f97SHong Zhang       /* Send messages back */
2905483b11dSHong Zhang       ierr = MPI_Isend(odata2,len,MPI_INT,proc_id,tag2,comm,s_waits2+k);CHKERRQ(ierr);
2915483b11dSHong Zhang       /* ierr = PetscPrintf(PETSC_COMM_SELF," [%d] send %d back to [%d] \n",rank,len,proc_id); */
292fc70d14eSHong Zhang       k++;
29310eea884SHong Zhang       odata2     += len;
29410eea884SHong Zhang       len_unused -= len;
295632d0f97SHong Zhang     }
2965483b11dSHong Zhang   }
2975483b11dSHong Zhang   ierr = PetscFree(odata1);CHKERRQ(ierr);
298632d0f97SHong Zhang 
299fc70d14eSHong Zhang   /* 4. Receive work done on other processors, then merge */
30010eea884SHong Zhang   /*------------------------------------------------------*/
30110eea884SHong Zhang   data2 = odata2;
30210eea884SHong Zhang   /* check if there is enough unused space in odata2(=data2) array */
30310eea884SHong Zhang   if (len_unused < len_max){ /* allocate more space for odata2 */
30410eea884SHong Zhang     ierr = PetscMalloc((len_est+1)*sizeof(int),&odata2_ptr[++nodata2]);CHKERRQ(ierr);
30510eea884SHong Zhang     data2 = odata2_ptr[nodata2];
30610eea884SHong Zhang     len_unused = len_est;
30710eea884SHong Zhang     /* ierr = PetscPrintf(PETSC_COMM_SELF, " [%d] Malloc data2, nodata2: %d\n",rank,nodata2); */
30810eea884SHong Zhang   }
309bfc6803cSHong Zhang 
310632d0f97SHong Zhang   k = 0;
3115483b11dSHong Zhang   while (k < nrqs){
312632d0f97SHong Zhang     /* Receive messages */
313c910923dSHong Zhang     ierr = MPI_Iprobe(MPI_ANY_SOURCE,tag2,comm,&flag,&r_status);
314632d0f97SHong Zhang     if (flag){
315bfc6803cSHong Zhang       ierr = MPI_Get_count(&r_status,MPI_INT,&len);CHKERRQ(ierr);
316632d0f97SHong Zhang       proc_id = r_status.MPI_SOURCE;
3170472cc68SHong Zhang       ierr = MPI_Irecv(data2,len,MPI_INT,proc_id,r_status.MPI_TAG,comm,&r_req);CHKERRQ(ierr);
3185483b11dSHong Zhang       ierr = MPI_Wait(&r_req,&r_status);CHKERRQ(ierr);
3195483b11dSHong Zhang       /* ierr = PetscPrintf(PETSC_COMM_SELF," [%d] recv %d from [%d], data2:\n",rank,len,proc_id); */
3205483b11dSHong Zhang       if (len > 1+is_max){ /* Add data2 into data */
3210472cc68SHong Zhang         data2_i = data2 + 1 + is_max;
322fc70d14eSHong Zhang         for (i=0; i<is_max; i++){
323fc70d14eSHong Zhang           table_i = table[i];
324bfc6803cSHong Zhang           data_i  = data + 1 + is_max + Mbs*i;
325bfc6803cSHong Zhang           isz     = data[1+i];
3260472cc68SHong Zhang           for (j=0; j<data2[1+i]; j++){
3270472cc68SHong Zhang             col = data2_i[j];
328bfc6803cSHong Zhang             if (!PetscBTLookupSet(table_i,col)) {data_i[isz++] = col;}
329fc70d14eSHong Zhang           }
330bfc6803cSHong Zhang           data[1+i] = isz;
3310472cc68SHong Zhang           if (i < is_max - 1) data2_i += data2[1+i];
332fc70d14eSHong Zhang         }
3335483b11dSHong Zhang       }
334632d0f97SHong Zhang       k++;
335632d0f97SHong Zhang     }
3365483b11dSHong Zhang   }
3375483b11dSHong Zhang 
3385483b11dSHong Zhang   /* phase 1 sends are complete */
3395483b11dSHong Zhang   ierr = PetscMalloc(size*sizeof(MPI_Status),&s_status);CHKERRQ(ierr);
3405483b11dSHong Zhang   if (nrqs){
3415483b11dSHong Zhang     ierr = MPI_Waitall(nrqs,s_waits1,s_status);CHKERRQ(ierr);
3425483b11dSHong Zhang   }
3435483b11dSHong Zhang   ierr = PetscFree(data1);CHKERRQ(ierr);
3445483b11dSHong Zhang 
3455483b11dSHong Zhang   /* phase 3 sends are complete */
3465483b11dSHong Zhang   if (nrqr){
3475483b11dSHong Zhang     ierr = MPI_Waitall(nrqr,s_waits2,s_status);CHKERRQ(ierr);
3485483b11dSHong Zhang   }
34910eea884SHong Zhang   for (k=0; k<=nodata2; k++){
3505483b11dSHong Zhang     ierr = PetscFree(odata2_ptr[k]);CHKERRQ(ierr);
3515483b11dSHong Zhang   }
3525483b11dSHong Zhang   ierr = PetscFree(odata2_ptr);CHKERRQ(ierr);
353632d0f97SHong Zhang 
354c910923dSHong Zhang   /* 5. Create new is[] */
355c910923dSHong Zhang   /*--------------------*/
356c910923dSHong Zhang   for (i=0; i<is_max; i++) {
357bfc6803cSHong Zhang     data_i = data + 1 + is_max + Mbs*i;
358bfc6803cSHong Zhang     ierr = ISCreateGeneral(PETSC_COMM_SELF,data[1+i],data_i,is+i);CHKERRQ(ierr);
359632d0f97SHong Zhang   }
3605483b11dSHong Zhang 
3610472cc68SHong Zhang   ierr = PetscFree(data);CHKERRQ(ierr);
3625483b11dSHong Zhang   ierr = PetscFree(s_waits1);CHKERRQ(ierr);
363632d0f97SHong Zhang   ierr = PetscFree(s_status);CHKERRQ(ierr);
3645483b11dSHong Zhang   if (table) {ierr = PetscFree(table);CHKERRQ(ierr);}
365bfc6803cSHong Zhang   ierr = PetscBTDestroy(otable);CHKERRQ(ierr);
3665483b11dSHong Zhang 
3675483b11dSHong Zhang   ierr = PetscFree(len_s);CHKERRQ(ierr);
3685483b11dSHong Zhang   ierr = PetscFree(id_r1);CHKERRQ(ierr);
3695483b11dSHong Zhang   ierr = PetscFree(len_r1);CHKERRQ(ierr);
3705483b11dSHong Zhang 
371632d0f97SHong Zhang   PetscFunctionReturn(0);
372632d0f97SHong Zhang }
373632d0f97SHong Zhang 
374632d0f97SHong Zhang #undef __FUNCT__
375632d0f97SHong Zhang #define __FUNCT__ "MatIncreaseOverlap_MPISBAIJ_Local"
376632d0f97SHong Zhang /*
377dc008846SHong Zhang    MatIncreaseOverlap_MPISBAIJ_Local - Called by MatIncreaseOverlap, to do
378632d0f97SHong Zhang        the work on the local processor.
379632d0f97SHong Zhang 
380632d0f97SHong Zhang      Inputs:
381632d0f97SHong Zhang       C      - MAT_MPISBAIJ;
382bfc6803cSHong Zhang       data   - holds is[]. See MatIncreaseOverlap_MPISBAIJ_Once() for the format.
383bfc6803cSHong Zhang       whose  - whose is[] to be processed,
384bfc6803cSHong Zhang                MINE:  this processor's is[]
385bfc6803cSHong Zhang                OTHER: other processor's is[]
386632d0f97SHong Zhang      Output:
38710eea884SHong Zhang        nidx  - whose = MINE:
3880472cc68SHong Zhang                      holds input and newly found indices in the same format as data
3890472cc68SHong Zhang                whose = OTHER:
3900472cc68SHong Zhang                      only holds the newly found indices
3910472cc68SHong Zhang        table - table[i]: mark the indices of is[i], i=0,...,is_max. Used only in the case 'whose=MINE'.
392632d0f97SHong Zhang */
39376f244e2SHong Zhang /* Would computation be reduced by swapping the loop 'for each is' and 'for each row'? */
39410eea884SHong Zhang static int MatIncreaseOverlap_MPISBAIJ_Local(Mat C,int *data,int whose,int *nidx,PetscBT *table)
395632d0f97SHong Zhang {
396632d0f97SHong Zhang   Mat_MPISBAIJ *c = (Mat_MPISBAIJ*)C->data;
397dc008846SHong Zhang   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)(c->A)->data;
398dc008846SHong Zhang   Mat_SeqBAIJ  *b = (Mat_SeqBAIJ*)(c->B)->data;
39976f244e2SHong Zhang   int          ierr,row,mbs,Mbs,*nidx_i,col,col_max,isz,isz0,*ai,*aj,*bi,*bj,*garray,rstart,l;
400dc008846SHong Zhang   int          a_start,a_end,b_start,b_end,i,j,k,is_max,*idx_i,n;
401bfc6803cSHong Zhang   PetscBT      table0;  /* mark the indices of input is[] for look up */
402bfc6803cSHong Zhang   PetscBT      table_i; /* poits to i-th table. When whose=OTHER, a single table is used for all is[] */
403632d0f97SHong Zhang 
404632d0f97SHong Zhang   PetscFunctionBegin;
40531f99336SHong Zhang   Mbs = c->Mbs; mbs = a->mbs;
406dc008846SHong Zhang   ai = a->i; aj = a->j;
407dc008846SHong Zhang   bi = b->i; bj = b->j;
408632d0f97SHong Zhang   garray = c->garray;
409dc008846SHong Zhang   rstart = c->rstart;
410dc008846SHong Zhang   is_max = data[0];
411632d0f97SHong Zhang 
412fc70d14eSHong Zhang   ierr = PetscBTCreate(Mbs,table0);CHKERRQ(ierr);
413fc70d14eSHong Zhang 
414fc70d14eSHong Zhang   nidx[0] = is_max;
415fc70d14eSHong Zhang   idx_i   = data + is_max + 1; /* ptr to input is[0] array */
416bfc6803cSHong Zhang   nidx_i  = nidx + is_max + 1; /* ptr to output is[0] array */
417dc008846SHong Zhang   for (i=0; i<is_max; i++) { /* for each is */
418dc008846SHong Zhang     isz  = 0;
419fc70d14eSHong Zhang     n = data[1+i]; /* size of input is[i] */
420dc008846SHong Zhang 
42176f244e2SHong Zhang     /* initialize and set table_i(mark idx and nidx) and table0(only mark idx) */
422bfc6803cSHong Zhang     if (whose == MINE){ /* process this processor's is[] */
423bfc6803cSHong Zhang       table_i = table[i];
4240472cc68SHong Zhang       nidx_i  = nidx + 1+ is_max + Mbs*i;
425bfc6803cSHong Zhang     } else {            /* process other processor's is[] - only use one temp table */
426bfc6803cSHong Zhang       table_i = *table;
427bfc6803cSHong Zhang     }
428bfc6803cSHong Zhang     ierr = PetscBTMemzero(Mbs,table_i);CHKERRQ(ierr);
429bfc6803cSHong Zhang     ierr = PetscBTMemzero(Mbs,table0);CHKERRQ(ierr);
43076f244e2SHong Zhang     if (n==0) {
43176f244e2SHong Zhang        nidx[1+i] = 0; /* size of new is[i] */
43276f244e2SHong Zhang        continue;
43376f244e2SHong Zhang     }
43476f244e2SHong Zhang 
43576f244e2SHong Zhang     isz0 = 0; col_max = 0;
436dc008846SHong Zhang     for (j=0; j<n; j++){
437dc008846SHong Zhang       col = idx_i[j];
438dc008846SHong Zhang       if (col >= Mbs) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"index col %d >= Mbs %d",col,Mbs);
439bfc6803cSHong Zhang       if(!PetscBTLookupSet(table_i,col)) {
440bfc6803cSHong Zhang         ierr = PetscBTSet(table0,col);CHKERRQ(ierr);
4410472cc68SHong Zhang         if (whose == MINE) {nidx_i[isz0] = col;}
442*dbe03f88SHong Zhang         if (col_max < col) col_max = col;
443bfc6803cSHong Zhang         isz0++;
444bfc6803cSHong Zhang       }
445632d0f97SHong Zhang     }
446dc008846SHong Zhang 
4470472cc68SHong Zhang     if (whose == MINE) {isz = isz0;}
448fc70d14eSHong Zhang     k = 0;  /* no. of indices from input is[i] that have been examined */
449dc008846SHong Zhang     for (row=0; row<mbs; row++){
450dc008846SHong Zhang       a_start = ai[row]; a_end = ai[row+1];
451dc008846SHong Zhang       b_start = bi[row]; b_end = bi[row+1];
4520472cc68SHong Zhang       if (PetscBTLookup(table0,row+rstart)){ /* row is on input is[i]:
4530472cc68SHong Zhang                                                 do row search: collect all col in this row */
454dc008846SHong Zhang         for (l = a_start; l<a_end ; l++){ /* Amat */
455dc008846SHong Zhang           col = aj[l] + rstart;
456fc70d14eSHong Zhang           if (!PetscBTLookupSet(table_i,col)) {nidx_i[isz++] = col;}
457dc008846SHong Zhang         }
458dc008846SHong Zhang         for (l = b_start; l<b_end ; l++){ /* Bmat */
459dc008846SHong Zhang           col = garray[bj[l]];
460fc70d14eSHong Zhang           if (!PetscBTLookupSet(table_i,col)) {nidx_i[isz++] = col;}
461dc008846SHong Zhang         }
462dc008846SHong Zhang         k++;
463dc008846SHong Zhang         if (k >= isz0) break; /* for (row=0; row<mbs; row++) */
4640472cc68SHong Zhang       } else { /* row is not on input is[i]:
4650472cc68SHong Zhang                   do col serach: add row onto nidx_i if there is a col in nidx_i */
466dc008846SHong Zhang         for (l = a_start; l<a_end ; l++){ /* Amat */
467dc008846SHong Zhang           col = aj[l] + rstart;
46876f244e2SHong Zhang           if (col > col_max) break;
469dc008846SHong Zhang           if (PetscBTLookup(table0,col)){
470fc70d14eSHong Zhang             if (!PetscBTLookupSet(table_i,row+rstart)) {nidx_i[isz++] = row+rstart;}
471dc008846SHong Zhang             break; /* for l = start; l<end ; l++) */
472632d0f97SHong Zhang           }
473632d0f97SHong Zhang         }
474dc008846SHong Zhang         for (l = b_start; l<b_end ; l++){ /* Bmat */
475dc008846SHong Zhang           col = garray[bj[l]];
47676f244e2SHong Zhang           if (col > col_max) break;
477dc008846SHong Zhang           if (PetscBTLookup(table0,col)){
478fc70d14eSHong Zhang             if (!PetscBTLookupSet(table_i,row+rstart)) {nidx_i[isz++] = row+rstart;}
479dc008846SHong Zhang             break; /* for l = start; l<end ; l++) */
480632d0f97SHong Zhang           }
481dc008846SHong Zhang         }
482dc008846SHong Zhang       }
483bfc6803cSHong Zhang     }
484dc008846SHong Zhang 
485dc008846SHong Zhang     if (i < is_max - 1){
486fc70d14eSHong Zhang       idx_i  += n;   /* ptr to input is[i+1] array */
487bfc6803cSHong Zhang       nidx_i += isz; /* ptr to output is[i+1] array */
488dc008846SHong Zhang     }
489fc70d14eSHong Zhang     nidx[1+i] = isz; /* size of new is[i] */
4901ab97a2aSSatish Balay   } /* for each is */
491dc008846SHong Zhang   ierr = PetscBTDestroy(table0);CHKERRQ(ierr);
492632d0f97SHong Zhang 
493632d0f97SHong Zhang   PetscFunctionReturn(0);
494632d0f97SHong Zhang }
495632d0f97SHong Zhang 
496632d0f97SHong Zhang 
497