xref: /petsc/src/mat/impls/sbaij/mpi/sbaijov.c (revision e8527bf2a27ef244d8e8778cd1179cf7343669d8)
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 *);
11bfc6803cSHong 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,
62*e8527bf2SHong Zhang               size,rank,Mbs,i,j,k,ierr,nrqs,nrqr,*odata1,*odata2,
63*e8527bf2SHong Zhang               tag1,tag2,flag,proc_id,**odata2_ptr,*ctable=0,*btable,len_b;
64*e8527bf2SHong Zhang   int         *id_r1,*len_r1,proc_end=0,*iwork,*len_s;
65bfc6803cSHong Zhang   char        *t_p;
66632d0f97SHong Zhang   MPI_Comm    comm;
67*e8527bf2SHong Zhang   MPI_Request *s_waits1,*s_waits2,r_req;
68632d0f97SHong Zhang   MPI_Status  *s_status,r_status;
695483b11dSHong Zhang   PetscBT     *table=0;  /* mark indices of this processor's is[] */
70fc70d14eSHong Zhang   PetscBT     table_i;
71bfc6803cSHong Zhang   PetscBT     otable; /* mark indices of other processors' is[] */
72*e8527bf2SHong Zhang   int         bs=c->bs,Bn = c->B->n,Bnbs = Bn/bs,*Bowners;
73*e8527bf2SHong Zhang   IS          is_local,is_gl;
745483b11dSHong Zhang 
75632d0f97SHong Zhang   PetscFunctionBegin;
76632d0f97SHong Zhang 
77632d0f97SHong Zhang   comm = C->comm;
78632d0f97SHong Zhang   size = c->size;
79632d0f97SHong Zhang   rank = c->rank;
80632d0f97SHong Zhang   Mbs  = c->Mbs;
81632d0f97SHong Zhang 
82c910923dSHong Zhang   ierr = PetscObjectGetNewTag((PetscObject)C,&tag1);CHKERRQ(ierr);
83c910923dSHong Zhang   ierr = PetscObjectGetNewTag((PetscObject)C,&tag2);CHKERRQ(ierr);
84c910923dSHong Zhang 
855483b11dSHong Zhang   /* 1. Send this processor's is[] to other processors */
865483b11dSHong Zhang   /*---------------------------------------------------*/
87*e8527bf2SHong Zhang   /* allocate spaces */
885483b11dSHong Zhang   ierr = PetscMalloc(is_max*sizeof(int),&n);CHKERRQ(ierr);
895483b11dSHong Zhang   len = 0;
90c910923dSHong Zhang   for (i=0; i<is_max; i++) {
91632d0f97SHong Zhang     ierr = ISGetLocalSize(is[i],&n[i]);CHKERRQ(ierr);
92632d0f97SHong Zhang     len += n[i];
93632d0f97SHong Zhang   }
945483b11dSHong Zhang   if (len == 0) {
955483b11dSHong Zhang     is_max = 0;
965483b11dSHong Zhang   } else {
975483b11dSHong Zhang     len += 1 + is_max; /* max length of data1 for one processor */
985483b11dSHong Zhang   }
995483b11dSHong Zhang 
1005483b11dSHong Zhang   ierr = PetscMalloc((size*len+1)*sizeof(int),&data1);CHKERRQ(ierr);
1015483b11dSHong Zhang   ierr = PetscMalloc(size*sizeof(int*),&data1_start);CHKERRQ(ierr);
1025483b11dSHong Zhang   for (i=0; i<size; i++) data1_start[i] = data1 + i*len;
1035483b11dSHong Zhang 
104*e8527bf2SHong Zhang   ierr   = PetscMalloc(size*3*sizeof(int),&len_s);CHKERRQ(ierr);
105*e8527bf2SHong Zhang   btable = len_s + size;
106*e8527bf2SHong Zhang   iwork  = btable + size;
107*e8527bf2SHong Zhang 
1085483b11dSHong Zhang   if (is_max){
1095483b11dSHong Zhang     /* create hash table ctable which maps c->row to proc_id) */
1105483b11dSHong Zhang     ierr = PetscMalloc(Mbs*sizeof(int),&ctable);CHKERRQ(ierr);
1115483b11dSHong Zhang     for (proc_id=0,j=0; proc_id<size; proc_id++) {
1125483b11dSHong Zhang       for (; j<c->rowners[proc_id+1]; j++) {
1135483b11dSHong Zhang         ctable[j] = proc_id;
1145483b11dSHong Zhang       }
1155483b11dSHong Zhang     }
1165483b11dSHong Zhang 
1175483b11dSHong Zhang     /* create tables for marking indices */
1185483b11dSHong Zhang     len_b = is_max*sizeof(PetscBT) + (Mbs/PETSC_BITS_PER_BYTE+1)*is_max*sizeof(char) + 1;
1195483b11dSHong Zhang     ierr = PetscMalloc(len_b,&table);CHKERRQ(ierr);
1205483b11dSHong Zhang     t_p  = (char *)(table + is_max);
121a2a9f22aSHong Zhang     for (i=0; i<is_max; i++) {
1225483b11dSHong Zhang       table[i]  = t_p  + (Mbs/PETSC_BITS_PER_BYTE+1)*i;
1235483b11dSHong Zhang     }
1245483b11dSHong Zhang 
1255483b11dSHong Zhang     ierr = ISCreateGeneral(comm,Bnbs,c->garray,&is_local);CHKERRQ(ierr);
1265483b11dSHong Zhang     ierr = ISAllGather(is_local, &is_gl);CHKERRQ(ierr);
127*e8527bf2SHong Zhang     ierr = ISDestroy(is_local);CHKERRQ(ierr);
1285483b11dSHong Zhang 
1295483b11dSHong Zhang     ierr = PetscMalloc((size+1)*sizeof(int),&Bowners);CHKERRQ(ierr);
130*e8527bf2SHong Zhang     ierr = MPI_Allgather(&Bnbs,1,MPI_INT,Bowners+1,1,MPI_INT,comm);CHKERRQ(ierr);
131*e8527bf2SHong Zhang     Bowners[0] = 0;
132*e8527bf2SHong Zhang     for (i=0; i<size; i++) Bowners[i+1] += Bowners[i];
1335483b11dSHong Zhang 
134*e8527bf2SHong Zhang     /* hash table table_i[idx] = 1 if idx is on is[] array
1355483b11dSHong Zhang                                = 0 otherwise */
1365483b11dSHong Zhang     table_i = table[0];
1375483b11dSHong Zhang     ierr    = PetscBTMemzero(Mbs,table_i);CHKERRQ(ierr);
1385483b11dSHong Zhang     for (i=0; i<is_max; i++){
139a2a9f22aSHong Zhang       ierr = ISGetIndices(is[i],&idx_i);CHKERRQ(ierr);
1405483b11dSHong Zhang       for (j=0; j<n[i]; j++){
1415483b11dSHong Zhang         idx = idx_i[j];
1425483b11dSHong Zhang         ierr = PetscBTSet(table_i,idx);CHKERRQ(ierr);
143632d0f97SHong Zhang       }
144a2a9f22aSHong Zhang       ierr = ISRestoreIndices(is[i],&idx_i);CHKERRQ(ierr);
1455483b11dSHong Zhang     }
1465483b11dSHong Zhang 
147*e8527bf2SHong Zhang     /* hash table btable[id_proc] = 1 if a col index of B-matrix in [id_proc] is on is[] array
148*e8527bf2SHong Zhang                                   = 0 otherwise */
149*e8527bf2SHong Zhang     ierr = ISGetIndices(is_gl,&idx_i);
1505483b11dSHong Zhang     for (i=0; i<size; i++){
1515483b11dSHong Zhang       btable[i] = 0;
1525483b11dSHong Zhang       for (j = Bowners[i]; j<Bowners[i+1]; j++){ /* go through B cols */
153*e8527bf2SHong Zhang         idx = idx_i[j];
1545483b11dSHong Zhang         if(PetscBTLookup(table_i,idx)){
1555483b11dSHong Zhang           btable[i] = 1;
1565483b11dSHong Zhang           break;
1575483b11dSHong Zhang         }
1585483b11dSHong Zhang       }
1595483b11dSHong Zhang     }
160*e8527bf2SHong Zhang     ierr = ISRestoreIndices(is_gl,&idx_i);CHKERRQ(ierr);
1615483b11dSHong Zhang     ierr = PetscFree(Bowners);CHKERRQ(ierr);
162*e8527bf2SHong Zhang     ierr = ISDestroy(is_gl);CHKERRQ(ierr);
1635483b11dSHong Zhang   } /* if (is_max) */
1645483b11dSHong Zhang 
1655483b11dSHong Zhang   /* evaluate communication - mesg to who, length, and buffer space */
166*e8527bf2SHong Zhang   for (i=0; i<size; i++) len_s[i] = 0;
1675483b11dSHong Zhang 
1685483b11dSHong Zhang   /* header of data1 */
1695483b11dSHong Zhang   for (proc_id=0; proc_id<size; proc_id++){
1705483b11dSHong Zhang     iwork[proc_id] = 0;
1715483b11dSHong Zhang     *data1_start[proc_id] = is_max;
1725483b11dSHong Zhang     data1_start[proc_id]++;
1735483b11dSHong Zhang     for (j=0; j<is_max; j++) {
1745483b11dSHong Zhang       if (proc_id == rank){
1755483b11dSHong Zhang         *data1_start[proc_id] = n[j];
1765483b11dSHong Zhang       } else {
1775483b11dSHong Zhang         *data1_start[proc_id] = 0;
1785483b11dSHong Zhang       }
1795483b11dSHong Zhang       data1_start[proc_id]++;
1805483b11dSHong Zhang     }
1815483b11dSHong Zhang   }
1825483b11dSHong Zhang 
1835483b11dSHong Zhang   for (i=0; i<is_max; i++) {
1845483b11dSHong Zhang     ierr = ISGetIndices(is[i],&idx_i);CHKERRQ(ierr);
1855483b11dSHong Zhang     ierr = PetscSortInt(n[i],idx_i);CHKERRQ(ierr);
1865483b11dSHong Zhang     for (j=0; j<n[i]; j++){
1875483b11dSHong Zhang       idx = idx_i[j];
1885483b11dSHong Zhang       *data1_start[rank] = idx; data1_start[rank]++; /* for local proccessing */
1895483b11dSHong Zhang       proc_end = ctable[idx];
1905483b11dSHong Zhang       for (proc_id=0;  proc_id<=proc_end; proc_id++){ /* for others to process */
191*e8527bf2SHong Zhang         if (proc_id == rank ) continue; /* done before this loop */
192*e8527bf2SHong Zhang         if (proc_id < proc_end && !btable[proc_id]) continue;   /* no need for sending idx to [proc_id] */
1935483b11dSHong Zhang         *data1_start[proc_id] = idx; data1_start[proc_id]++;
1945483b11dSHong Zhang         len_s[proc_id]++;
1955483b11dSHong Zhang       }
1965483b11dSHong Zhang     }
1975483b11dSHong Zhang     /* update header data */
1985483b11dSHong Zhang     for (proc_id=0; proc_id<=proc_end; proc_id++){
1995483b11dSHong Zhang       if (proc_id== rank) continue;
2005483b11dSHong Zhang       *(data1 + proc_id*len + 1 + i) = len_s[proc_id] - iwork[proc_id];
2015483b11dSHong Zhang       iwork[proc_id] = len_s[proc_id] ;
2025483b11dSHong Zhang     }
2035483b11dSHong Zhang     ierr = ISRestoreIndices(is[i],&idx_i);CHKERRQ(ierr);
204*e8527bf2SHong Zhang   }
2055483b11dSHong Zhang 
2065483b11dSHong Zhang   nrqs = 0; nrqr = 0;
2075483b11dSHong Zhang   for (i=0; i<size; i++){
2085483b11dSHong Zhang     data1_start[i] = data1 + i*len;
2095483b11dSHong Zhang     if (len_s[i]){
2105483b11dSHong Zhang       nrqs++;
2115483b11dSHong Zhang       len_s[i] += 1 + is_max; /* add no. of header msg */
2125483b11dSHong Zhang     }
2135483b11dSHong Zhang   }
2145483b11dSHong Zhang 
2155483b11dSHong Zhang   for (i=0; i<is_max; i++) {
216a2a9f22aSHong Zhang     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
217632d0f97SHong Zhang   }
218bfc6803cSHong Zhang   ierr = PetscFree(n);CHKERRQ(ierr);
2195483b11dSHong Zhang   if (ctable){ierr = PetscFree(ctable);CHKERRQ(ierr);}
2205483b11dSHong Zhang 
2215483b11dSHong Zhang   /* Determine the number of messages to expect, their lengths, from from-ids */
2225483b11dSHong Zhang   ierr = PetscGatherNumberOfMessages(comm,PETSC_NULL,len_s,&nrqr);CHKERRQ(ierr);
2235483b11dSHong Zhang   ierr = PetscGatherMessageLengths(comm,nrqs,nrqr,len_s,&id_r1,&len_r1);CHKERRQ(ierr);
2245483b11dSHong Zhang   /* ierr = PetscPrintf(PETSC_COMM_SELF, "[%d] nrqs: %d, nrqr: %d\n",rank,nrqs,nrqr); */
225632d0f97SHong Zhang 
226632d0f97SHong Zhang   /*  Now  post the sends */
2275483b11dSHong Zhang   ierr = PetscMalloc(2*size*sizeof(MPI_Request),&s_waits1);CHKERRQ(ierr);
2285483b11dSHong Zhang   s_waits2 = s_waits1 + size;
229632d0f97SHong Zhang   k = 0;
2305483b11dSHong Zhang   for (proc_id=0; proc_id<size; proc_id++){  /* send data1 to processor [proc_id] */
2315483b11dSHong Zhang     if (len_s[proc_id]){
2325483b11dSHong Zhang       ierr = MPI_Isend(data1_start[proc_id],len_s[proc_id],MPI_INT,proc_id,tag1,comm,s_waits1+k);CHKERRQ(ierr);
233632d0f97SHong Zhang       k++;
234632d0f97SHong Zhang     }
235632d0f97SHong Zhang   }
236632d0f97SHong Zhang 
237dc008846SHong Zhang   /* 2. Do local work on this processor's is[] */
238dc008846SHong Zhang   /*-------------------------------------------*/
2395483b11dSHong Zhang   ierr = PetscBTCreate(Mbs,otable);CHKERRQ(ierr);
240*e8527bf2SHong Zhang   ierr = MatIncreaseOverlap_MPISBAIJ_Local(C,data1_start[rank],MINE,&data,table);CHKERRQ(ierr);
241*e8527bf2SHong Zhang   ierr = PetscFree(data1_start);CHKERRQ(ierr);
242632d0f97SHong Zhang 
243632d0f97SHong Zhang   /* 3. Receive other's is[] and process. Then send back */
244bfc6803cSHong Zhang   /*-----------------------------------------------------*/
2455483b11dSHong Zhang   len = 0;
2465483b11dSHong Zhang   for (i=0; i<nrqr; i++){
2475483b11dSHong Zhang     if (len_r1[i] > len)len = len_r1[i];
2485483b11dSHong Zhang     /* ierr = PetscPrintf(PETSC_COMM_SELF, "[%d] expect to recv len=%d from [%d]\n",rank,len_r1[i],id_r1[i]); */
2495483b11dSHong Zhang   }
2505483b11dSHong Zhang   ierr = PetscMalloc((len+1)*sizeof(int),&odata1);CHKERRQ(ierr);
251bfc6803cSHong Zhang   ierr = PetscMalloc(size*sizeof(int**),&odata2_ptr);CHKERRQ(ierr);
252632d0f97SHong Zhang   k = 0;
2535483b11dSHong Zhang   while (k < nrqr){
254632d0f97SHong Zhang     /* Receive messages */
255bfc6803cSHong Zhang     ierr = MPI_Iprobe(MPI_ANY_SOURCE,tag1,comm,&flag,&r_status);CHKERRQ(ierr);
256632d0f97SHong Zhang     if (flag){
257bfc6803cSHong Zhang       ierr = MPI_Get_count(&r_status,MPI_INT,&len);CHKERRQ(ierr);
258632d0f97SHong Zhang       proc_id = r_status.MPI_SOURCE;
259bfc6803cSHong Zhang       ierr = MPI_Irecv(odata1,len,MPI_INT,proc_id,r_status.MPI_TAG,comm,&r_req);CHKERRQ(ierr);
2605483b11dSHong Zhang       ierr = MPI_Wait(&r_req,&r_status);CHKERRQ(ierr);
2615483b11dSHong Zhang       /* ierr = PetscPrintf(PETSC_COMM_SELF, " [%d] recv %d from [%d]\n",rank,len,proc_id); */
262632d0f97SHong Zhang 
263fc70d14eSHong Zhang       /*  Process messages */
264bfc6803cSHong Zhang       ierr = MatIncreaseOverlap_MPISBAIJ_Local(C,odata1,OTHER,&odata2_ptr[k],&otable);CHKERRQ(ierr);
265a2a9f22aSHong Zhang       odata2 = odata2_ptr[k];
266a2a9f22aSHong Zhang       len = 1 + odata2[0];
267a2a9f22aSHong Zhang       for (i=0; i<odata2[0]; i++){
268a2a9f22aSHong Zhang         len += odata2[1 + i];
269fc70d14eSHong Zhang       }
270632d0f97SHong Zhang 
271632d0f97SHong Zhang       /* Send messages back */
2725483b11dSHong Zhang       ierr = MPI_Isend(odata2,len,MPI_INT,proc_id,tag2,comm,s_waits2+k);CHKERRQ(ierr);
2735483b11dSHong Zhang       /* ierr = PetscPrintf(PETSC_COMM_SELF," [%d] send %d back to [%d] \n",rank,len,proc_id); */
274fc70d14eSHong Zhang       k++;
275632d0f97SHong Zhang     }
2765483b11dSHong Zhang   }
2775483b11dSHong Zhang   ierr = PetscFree(odata1);CHKERRQ(ierr);
278632d0f97SHong Zhang 
279fc70d14eSHong Zhang   /* 4. Receive work done on other processors, then merge */
280632d0f97SHong Zhang   /*--------------------------------------------------------*/
2810472cc68SHong Zhang   /* Allocate memory for incoming data */
282bfc6803cSHong Zhang   len = (1+is_max*(Mbs+1));
2830472cc68SHong Zhang   ierr = PetscMalloc(len*sizeof(int),&data2);CHKERRQ(ierr);
284bfc6803cSHong Zhang 
285632d0f97SHong Zhang   k = 0;
2865483b11dSHong Zhang   while (k < nrqs){
287632d0f97SHong Zhang     /* Receive messages */
288c910923dSHong Zhang     ierr = MPI_Iprobe(MPI_ANY_SOURCE,tag2,comm,&flag,&r_status);
289632d0f97SHong Zhang     if (flag){
290bfc6803cSHong Zhang       ierr = MPI_Get_count(&r_status,MPI_INT,&len);CHKERRQ(ierr);
291632d0f97SHong Zhang       proc_id = r_status.MPI_SOURCE;
2920472cc68SHong Zhang       ierr = MPI_Irecv(data2,len,MPI_INT,proc_id,r_status.MPI_TAG,comm,&r_req);CHKERRQ(ierr);
2935483b11dSHong Zhang       ierr = MPI_Wait(&r_req,&r_status);CHKERRQ(ierr);
2945483b11dSHong Zhang       /* ierr = PetscPrintf(PETSC_COMM_SELF," [%d] recv %d from [%d], data2:\n",rank,len,proc_id); */
2955483b11dSHong Zhang       if (len > 1+is_max){ /* Add data2 into data */
2960472cc68SHong Zhang         data2_i = data2 + 1 + is_max;
297fc70d14eSHong Zhang         for (i=0; i<is_max; i++){
298fc70d14eSHong Zhang           table_i = table[i];
299bfc6803cSHong Zhang           data_i  = data + 1 + is_max + Mbs*i;
300bfc6803cSHong Zhang           isz     = data[1+i];
3010472cc68SHong Zhang           for (j=0; j<data2[1+i]; j++){
3020472cc68SHong Zhang             col = data2_i[j];
303bfc6803cSHong Zhang             if (!PetscBTLookupSet(table_i,col)) {data_i[isz++] = col;}
304fc70d14eSHong Zhang           }
305bfc6803cSHong Zhang           data[1+i] = isz;
3060472cc68SHong Zhang           if (i < is_max - 1) data2_i += data2[1+i];
307fc70d14eSHong Zhang         }
3085483b11dSHong Zhang       }
309632d0f97SHong Zhang       k++;
310632d0f97SHong Zhang     }
3115483b11dSHong Zhang   }
3125483b11dSHong Zhang 
3135483b11dSHong Zhang   /* phase 1 sends are complete */
3145483b11dSHong Zhang   ierr = PetscMalloc(size*sizeof(MPI_Status),&s_status);CHKERRQ(ierr);
3155483b11dSHong Zhang   if (nrqs){
3165483b11dSHong Zhang     ierr = MPI_Waitall(nrqs,s_waits1,s_status);CHKERRQ(ierr);
3175483b11dSHong Zhang   }
3185483b11dSHong Zhang   ierr = PetscFree(data1);CHKERRQ(ierr);
3195483b11dSHong Zhang 
3205483b11dSHong Zhang   /* phase 3 sends are complete */
3215483b11dSHong Zhang   if (nrqr){
3225483b11dSHong Zhang     ierr = MPI_Waitall(nrqr,s_waits2,s_status);CHKERRQ(ierr);
3235483b11dSHong Zhang   }
3245483b11dSHong Zhang   for (k=0; k<nrqr; k++){
3255483b11dSHong Zhang     ierr = PetscFree(odata2_ptr[k]);CHKERRQ(ierr);
3265483b11dSHong Zhang   }
3275483b11dSHong Zhang   ierr = PetscFree(odata2_ptr);CHKERRQ(ierr);
328632d0f97SHong Zhang 
329c910923dSHong Zhang   /* 5. Create new is[] */
330c910923dSHong Zhang   /*--------------------*/
331c910923dSHong Zhang   for (i=0; i<is_max; i++) {
332bfc6803cSHong Zhang     data_i = data + 1 + is_max + Mbs*i;
333bfc6803cSHong Zhang     ierr = ISCreateGeneral(PETSC_COMM_SELF,data[1+i],data_i,is+i);CHKERRQ(ierr);
334632d0f97SHong Zhang   }
3355483b11dSHong Zhang 
336a2a9f22aSHong Zhang   ierr = PetscFree(data2);CHKERRQ(ierr);
3370472cc68SHong Zhang   ierr = PetscFree(data);CHKERRQ(ierr);
3385483b11dSHong Zhang   ierr = PetscFree(s_waits1);CHKERRQ(ierr);
339632d0f97SHong Zhang   ierr = PetscFree(s_status);CHKERRQ(ierr);
3405483b11dSHong Zhang   if (table) {ierr = PetscFree(table);CHKERRQ(ierr);}
341bfc6803cSHong Zhang   ierr = PetscBTDestroy(otable);CHKERRQ(ierr);
3425483b11dSHong Zhang 
3435483b11dSHong Zhang   ierr = PetscFree(len_s);CHKERRQ(ierr);
3445483b11dSHong Zhang   ierr = PetscFree(id_r1);CHKERRQ(ierr);
3455483b11dSHong Zhang   ierr = PetscFree(len_r1);CHKERRQ(ierr);
3465483b11dSHong Zhang 
347632d0f97SHong Zhang   PetscFunctionReturn(0);
348632d0f97SHong Zhang }
349632d0f97SHong Zhang 
350632d0f97SHong Zhang #undef __FUNCT__
351632d0f97SHong Zhang #define __FUNCT__ "MatIncreaseOverlap_MPISBAIJ_Local"
352632d0f97SHong Zhang /*
353dc008846SHong Zhang    MatIncreaseOverlap_MPISBAIJ_Local - Called by MatIncreaseOverlap, to do
354632d0f97SHong Zhang        the work on the local processor.
355632d0f97SHong Zhang 
356632d0f97SHong Zhang      Inputs:
357632d0f97SHong Zhang       C      - MAT_MPISBAIJ;
358bfc6803cSHong Zhang       data   - holds is[]. See MatIncreaseOverlap_MPISBAIJ_Once() for the format.
359bfc6803cSHong Zhang       whose  - whose is[] to be processed,
360bfc6803cSHong Zhang                MINE:  this processor's is[]
361bfc6803cSHong Zhang                OTHER: other processor's is[]
362632d0f97SHong Zhang      Output:
3630472cc68SHong Zhang        data_new  - whose = MINE:
3640472cc68SHong Zhang                      holds input and newly found indices in the same format as data
3650472cc68SHong Zhang                    whose = OTHER:
3660472cc68SHong Zhang                      only holds the newly found indices
3670472cc68SHong Zhang        table     - table[i]: mark the indices of is[i], i=0,...,is_max. Used only in the case 'whose=MINE'.
368632d0f97SHong Zhang */
369bfc6803cSHong Zhang static int MatIncreaseOverlap_MPISBAIJ_Local(Mat C,int *data,int whose,int **data_new,PetscBT *table)
370632d0f97SHong Zhang {
371632d0f97SHong Zhang   Mat_MPISBAIJ *c = (Mat_MPISBAIJ*)C->data;
372dc008846SHong Zhang   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)(c->A)->data;
373dc008846SHong Zhang   Mat_SeqBAIJ  *b = (Mat_SeqBAIJ*)(c->B)->data;
37431f99336SHong Zhang   int          ierr,row,mbs,Mbs,*nidx,*nidx_i,col,isz,isz0,*ai,*aj,*bi,*bj,*garray,rstart,l;
375dc008846SHong Zhang   int          a_start,a_end,b_start,b_end,i,j,k,is_max,*idx_i,n;
376bfc6803cSHong Zhang   PetscBT      table0;  /* mark the indices of input is[] for look up */
377bfc6803cSHong Zhang   PetscBT      table_i; /* poits to i-th table. When whose=OTHER, a single table is used for all is[] */
378632d0f97SHong Zhang 
379632d0f97SHong Zhang   PetscFunctionBegin;
38031f99336SHong Zhang   Mbs = c->Mbs; mbs = a->mbs;
381dc008846SHong Zhang   ai = a->i; aj = a->j;
382dc008846SHong Zhang   bi = b->i; bj = b->j;
383632d0f97SHong Zhang   garray = c->garray;
384dc008846SHong Zhang   rstart = c->rstart;
385dc008846SHong Zhang   is_max = data[0];
386632d0f97SHong Zhang 
387fc70d14eSHong Zhang   ierr = PetscBTCreate(Mbs,table0);CHKERRQ(ierr);
388fc70d14eSHong Zhang 
389fc70d14eSHong Zhang   ierr = PetscMalloc((1+is_max*(Mbs+1))*sizeof(int),&nidx);CHKERRQ(ierr);
390fc70d14eSHong Zhang   nidx[0] = is_max;
391fc70d14eSHong Zhang 
392fc70d14eSHong Zhang   idx_i  = data + is_max + 1; /* ptr to input is[0] array */
393bfc6803cSHong Zhang   nidx_i = nidx + is_max + 1; /* ptr to output is[0] array */
394dc008846SHong Zhang   for (i=0; i<is_max; i++) { /* for each is */
395dc008846SHong Zhang     isz  = 0;
396fc70d14eSHong Zhang     n = data[1+i]; /* size of input is[i] */
397dc008846SHong Zhang 
398bfc6803cSHong Zhang     /* initialize table_i, set table0 */
399bfc6803cSHong Zhang     if (whose == MINE){ /* process this processor's is[] */
400bfc6803cSHong Zhang       table_i = table[i];
4010472cc68SHong Zhang       nidx_i  = nidx + 1+ is_max + Mbs*i;
402bfc6803cSHong Zhang     } else {            /* process other processor's is[] - only use one temp table */
403bfc6803cSHong Zhang       table_i = *table;
404bfc6803cSHong Zhang     }
405bfc6803cSHong Zhang     ierr = PetscBTMemzero(Mbs,table_i);CHKERRQ(ierr);
406bfc6803cSHong Zhang     ierr = PetscBTMemzero(Mbs,table0);CHKERRQ(ierr);
407fc70d14eSHong Zhang     if (n > 0) {
408bfc6803cSHong Zhang       isz0 = 0;
409dc008846SHong Zhang       for (j=0; j<n; j++){
410dc008846SHong Zhang         col = idx_i[j];
411dc008846SHong Zhang         if (col >= Mbs) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"index col %d >= Mbs %d",col,Mbs);
412bfc6803cSHong Zhang         if(!PetscBTLookupSet(table_i,col)) {
413bfc6803cSHong Zhang           ierr = PetscBTSet(table0,col);CHKERRQ(ierr);
4140472cc68SHong Zhang           if (whose == MINE) {nidx_i[isz0] = col;}
415bfc6803cSHong Zhang           isz0++;
416bfc6803cSHong Zhang         }
417632d0f97SHong Zhang       }
418dc008846SHong Zhang 
4190472cc68SHong Zhang       if (whose == MINE) {isz = isz0;}
420fc70d14eSHong Zhang       k = 0;  /* no. of indices from input is[i] that have been examined */
421dc008846SHong Zhang       for (row=0; row<mbs; row++){
422dc008846SHong Zhang         a_start = ai[row]; a_end = ai[row+1];
423dc008846SHong Zhang         b_start = bi[row]; b_end = bi[row+1];
4240472cc68SHong Zhang         if (PetscBTLookup(table0,row+rstart)){ /* row is on input is[i]:
4250472cc68SHong Zhang            do row search: collect all col in this row */
426dc008846SHong Zhang           for (l = a_start; l<a_end ; l++){ /* Amat */
427dc008846SHong Zhang             col = aj[l] + rstart;
428fc70d14eSHong Zhang             if (!PetscBTLookupSet(table_i,col)) {nidx_i[isz++] = col;}
429dc008846SHong Zhang           }
430dc008846SHong Zhang           for (l = b_start; l<b_end ; l++){ /* Bmat */
431dc008846SHong Zhang             col = garray[bj[l]];
432fc70d14eSHong Zhang             if (!PetscBTLookupSet(table_i,col)) {nidx_i[isz++] = col;}
433dc008846SHong Zhang           }
434dc008846SHong Zhang           k++;
435dc008846SHong Zhang           if (k >= isz0) break; /* for (row=0; row<mbs; row++) */
4360472cc68SHong Zhang         } else { /* row is not on input is[i]:
4370472cc68SHong Zhang           do col serach: add row onto nidx_i if there is a col in nidx_i */
438dc008846SHong Zhang           for (l = a_start; l<a_end ; l++){ /* Amat */
439dc008846SHong Zhang             col = aj[l] + rstart;
440dc008846SHong Zhang             if (PetscBTLookup(table0,col)){
441fc70d14eSHong Zhang               if (!PetscBTLookupSet(table_i,row+rstart)) {nidx_i[isz++] = row+rstart;}
442dc008846SHong Zhang               break; /* for l = start; l<end ; l++) */
443632d0f97SHong Zhang             }
444632d0f97SHong Zhang           }
445dc008846SHong Zhang           for (l = b_start; l<b_end ; l++){ /* Bmat */
446dc008846SHong Zhang             col = garray[bj[l]];
447dc008846SHong Zhang             if (PetscBTLookup(table0,col)){
448fc70d14eSHong Zhang               if (!PetscBTLookupSet(table_i,row+rstart)) {nidx_i[isz++] = row+rstart;}
449dc008846SHong Zhang               break; /* for l = start; l<end ; l++) */
450632d0f97SHong Zhang             }
451dc008846SHong Zhang           }
452dc008846SHong Zhang         }
453bfc6803cSHong Zhang       }
454fc70d14eSHong Zhang     } /* if (n > 0) */
455dc008846SHong Zhang 
456dc008846SHong Zhang     if (i < is_max - 1){
457fc70d14eSHong Zhang       idx_i  += n;   /* ptr to input is[i+1] array */
458bfc6803cSHong Zhang       nidx_i += isz; /* ptr to output is[i+1] array */
459dc008846SHong Zhang     }
460fc70d14eSHong Zhang     nidx[1+i] = isz; /* size of new is[i] */
4611ab97a2aSSatish Balay   } /* for each is */
462dc008846SHong Zhang   *data_new = nidx;
463dc008846SHong Zhang   ierr = PetscBTDestroy(table0);CHKERRQ(ierr);
464632d0f97SHong Zhang 
465632d0f97SHong Zhang   PetscFunctionReturn(0);
466632d0f97SHong Zhang }
467632d0f97SHong Zhang 
468632d0f97SHong Zhang 
469