xref: /petsc/src/mat/impls/sbaij/mpi/sbaijov.c (revision 7a868f3e1c0c9a88f5780a22d07fe2a0d8cb7b47)
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 */
6c6db04a5SJed Brown #include <../src/mat/impls/sbaij/mpi/mpisbaij.h>
7c6db04a5SJed 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");}
25db41cccfSHong Zhang 
26db41cccfSHong Zhang   //------- new ----------
27db41cccfSHong Zhang   PetscBool flg=PETSC_FALSE;
28*7a868f3eSHong Zhang   ierr = PetscOptionsHasName(PETSC_NULL, "-IncreaseOverlap_old", &flg);CHKERRQ(ierr);
29*7a868f3eSHong Zhang   if (flg){ /* previous non-scalable implementation */
30*7a868f3eSHong Zhang     printf("use previous non-scalable implementation...\n");
31632d0f97SHong Zhang     for (i=0; i<ov; ++i) {
32c910923dSHong Zhang       ierr = MatIncreaseOverlap_MPISBAIJ_Once(C,is_max,is_new);CHKERRQ(ierr);
33632d0f97SHong Zhang     }
34db41cccfSHong Zhang   } else { /* scalable implementation using modified BAIJ routines */
35db41cccfSHong Zhang   Mat           *submats;
36db41cccfSHong Zhang   IS            *is_row;
37db41cccfSHong Zhang   PetscInt      M=C->rmap->N,Mbs=M/bs,*nidx,isz,iov;
38db41cccfSHong Zhang   Mat_MPISBAIJ  *c = (Mat_MPISBAIJ*)C->data;
39db41cccfSHong Zhang   PetscMPIInt   rank=c->rank;
40db41cccfSHong Zhang 
41db41cccfSHong Zhang   ierr = PetscMalloc((Mbs+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr);
42db41cccfSHong Zhang 
43db41cccfSHong Zhang   /* Create is_row */
44db41cccfSHong Zhang   ierr = PetscMalloc(is_max*sizeof(IS **),&is_row);CHKERRQ(ierr);
45487daaacSHong Zhang   ierr = ISCreateStride(PETSC_COMM_SELF,Mbs,0,1,&is_row[0]);CHKERRQ(ierr);
46487daaacSHong Zhang   for (i=1; i<is_max; i++) is_row[i] = is_row[0]; /* reuse is_row[0] */
47db41cccfSHong Zhang 
48db41cccfSHong Zhang   for (iov=0; iov<ov; ++iov) {
49487daaacSHong Zhang     /* Get submats for column search - Modified from MatGetSubMatrices_MPIBAIJ() */
50487daaacSHong Zhang 
51db41cccfSHong Zhang     /* Allocate memory to hold all the submatrices */
52db41cccfSHong Zhang     ierr = PetscMalloc((is_max+1)*sizeof(Mat),&submats);CHKERRQ(ierr);
53db41cccfSHong Zhang     /* Determine the number of stages through which submatrices are done */
54db41cccfSHong Zhang     PetscInt nmax,nstages_local,nstages,max_no,pos;
55db41cccfSHong Zhang     nmax          = 20*1000000 / (c->Nbs * sizeof(PetscInt));
56db41cccfSHong Zhang     if (!nmax) nmax = 1;
57db41cccfSHong Zhang     nstages_local = is_max/nmax + ((is_max % nmax)?1:0);
58db41cccfSHong Zhang 
59db41cccfSHong Zhang     /* Make sure every processor loops through the nstages */
60db41cccfSHong Zhang     ierr = MPI_Allreduce(&nstages_local,&nstages,1,MPIU_INT,MPI_MAX,((PetscObject)C)->comm);CHKERRQ(ierr);
61db41cccfSHong Zhang     for (i=0,pos=0; i<nstages; i++) {
62db41cccfSHong Zhang       if (pos+nmax <= is_max) max_no = nmax;
63db41cccfSHong Zhang       else if (pos == is_max) max_no = 0;
64db41cccfSHong Zhang       else                   max_no = is_max-pos;
65db41cccfSHong Zhang 
66*7a868f3eSHong Zhang       c->ijonly = PETSC_TRUE;
67*7a868f3eSHong Zhang       ierr = MatGetSubMatrices_MPIBAIJ_local(C,max_no,is_row+pos,is_new+pos,MAT_INITIAL_MATRIX,submats+pos);CHKERRQ(ierr);
68*7a868f3eSHong Zhang 
69db41cccfSHong Zhang       if (rank == 10 && !i){
70db41cccfSHong Zhang         printf("submats[0]:\n");
71db41cccfSHong Zhang         ierr = MatView(submats[0],PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
72db41cccfSHong Zhang       }
73db41cccfSHong Zhang       pos += max_no;
74db41cccfSHong Zhang     }
75db41cccfSHong Zhang 
76db41cccfSHong Zhang     /* Row search */
77db41cccfSHong Zhang     ierr = MatIncreaseOverlap_MPIBAIJ_Once(C,is_max,is_new);CHKERRQ(ierr);
78db41cccfSHong Zhang 
79db41cccfSHong Zhang     /* Column search */
80db41cccfSHong Zhang     PetscBT        table;
81db41cccfSHong Zhang     Mat_SeqSBAIJ   *asub_i;
82db41cccfSHong Zhang     PetscInt       *ai,brow,nz,nis,l;
83db41cccfSHong Zhang     const PetscInt *idx;
84db41cccfSHong Zhang 
85db41cccfSHong Zhang     ierr = PetscBTCreate(Mbs,table);CHKERRQ(ierr);
86db41cccfSHong Zhang     for (i=0; i<is_max; i++){
87db41cccfSHong Zhang       asub_i = (Mat_SeqSBAIJ*)submats[i]->data;
88db41cccfSHong Zhang       ai=asub_i->i;;
89db41cccfSHong Zhang 
90db41cccfSHong Zhang       /* put is_new obtained from MatIncreaseOverlap_MPIBAIJ() to table */
91db41cccfSHong Zhang       ierr = PetscBTMemzero(Mbs,table);CHKERRQ(ierr);
92db41cccfSHong Zhang 
93db41cccfSHong Zhang       ierr = ISGetIndices(is_new[i],&idx);CHKERRQ(ierr);
94db41cccfSHong Zhang       ierr = ISGetLocalSize(is_new[i],&nis);CHKERRQ(ierr);
95db41cccfSHong Zhang       for (l=0; l<nis; l++) {
96db41cccfSHong Zhang         ierr = PetscBTSet(table,idx[l]);CHKERRQ(ierr);
97db41cccfSHong Zhang         nidx[l] = idx[l];
98db41cccfSHong Zhang       }
99db41cccfSHong Zhang       isz = nis;
100db41cccfSHong Zhang 
101db41cccfSHong Zhang       for (brow=0; brow<Mbs; brow++){
102db41cccfSHong Zhang         nz = ai[brow+1] - ai[brow];
103db41cccfSHong Zhang         if (nz) {
104db41cccfSHong Zhang           if (!PetscBTLookupSet(table,brow)) nidx[isz++] = brow;
105db41cccfSHong Zhang         }
106db41cccfSHong Zhang       }
107db41cccfSHong Zhang       ierr = ISRestoreIndices(is_new[i],&idx);CHKERRQ(ierr);
108db41cccfSHong Zhang       ierr = ISDestroy(is_new[i]);CHKERRQ(ierr);
109db41cccfSHong Zhang 
110db41cccfSHong Zhang       /* create updated is_new */
111db41cccfSHong Zhang       ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,is_new+i);CHKERRQ(ierr);
112db41cccfSHong Zhang     }
113db41cccfSHong Zhang 
114db41cccfSHong Zhang     /* Free tmp spaces */
115db41cccfSHong Zhang     ierr = PetscBTDestroy(table);CHKERRQ(ierr);
116db41cccfSHong Zhang     for (i=0; i<is_max; i++){
117db41cccfSHong Zhang       ierr = MatDestroy(submats[i]);CHKERRQ(ierr);
118db41cccfSHong Zhang     }
119db41cccfSHong Zhang     ierr = PetscFree(submats);CHKERRQ(ierr);
120db41cccfSHong Zhang   }
121487daaacSHong Zhang   ierr = ISDestroy(is_row[0]);CHKERRQ(ierr);
122db41cccfSHong Zhang   ierr = PetscFree(is_row);CHKERRQ(ierr);
123db41cccfSHong Zhang   ierr = PetscFree(nidx);CHKERRQ(ierr);
124db41cccfSHong Zhang   }
125db41cccfSHong Zhang   //--------end of new----------
126db41cccfSHong Zhang 
127c910923dSHong Zhang   for (i=0; i<is_max; i++) {ierr = ISDestroy(is[i]);CHKERRQ(ierr);}
12827f478b1SHong Zhang   ierr = ISExpandIndicesGeneral(N,bs,is_max,is_new,is);CHKERRQ(ierr);
129db41cccfSHong Zhang 
130c910923dSHong Zhang   for (i=0; i<is_max; i++) {ierr = ISDestroy(is_new[i]);CHKERRQ(ierr);}
131632d0f97SHong Zhang   ierr = PetscFree(is_new);CHKERRQ(ierr);
132632d0f97SHong Zhang   PetscFunctionReturn(0);
133632d0f97SHong Zhang }
134632d0f97SHong Zhang 
1354a69c536SBarry Smith typedef enum {MINE,OTHER} WhoseOwner;
1360472cc68SHong Zhang /*  data1, odata1 and odata2 are packed in the format (for communication):
137a2a9f22aSHong Zhang        data[0]          = is_max, no of is
138a2a9f22aSHong Zhang        data[1]          = size of is[0]
139a2a9f22aSHong Zhang         ...
140a2a9f22aSHong Zhang        data[is_max]     = size of is[is_max-1]
141a2a9f22aSHong Zhang        data[is_max + 1] = data(is[0])
142a2a9f22aSHong Zhang         ...
143a2a9f22aSHong Zhang        data[is_max+1+sum(size of is[k]), k=0,...,i-1] = data(is[i])
144a2a9f22aSHong Zhang         ...
1450472cc68SHong Zhang    data2 is packed in the format (for creating output is[]):
1460472cc68SHong Zhang        data[0]          = is_max, no of is
1470472cc68SHong Zhang        data[1]          = size of is[0]
1480472cc68SHong Zhang         ...
1490472cc68SHong Zhang        data[is_max]     = size of is[is_max-1]
1500472cc68SHong Zhang        data[is_max + 1] = data(is[0])
1510472cc68SHong Zhang         ...
1520472cc68SHong Zhang        data[is_max + 1 + Mbs*i) = data(is[i])
1530472cc68SHong Zhang         ...
154a2a9f22aSHong Zhang */
155632d0f97SHong Zhang #undef __FUNCT__
156632d0f97SHong Zhang #define __FUNCT__ "MatIncreaseOverlap_MPISBAIJ_Once"
15713f74950SBarry Smith static PetscErrorCode MatIncreaseOverlap_MPISBAIJ_Once(Mat C,PetscInt is_max,IS is[])
158632d0f97SHong Zhang {
159632d0f97SHong Zhang   Mat_MPISBAIJ  *c = (Mat_MPISBAIJ*)C->data;
1606849ba73SBarry Smith   PetscErrorCode ierr;
16113f74950SBarry Smith   PetscMPIInt    size,rank,tag1,tag2,*len_s,nrqr,nrqs,*id_r1,*len_r1,flag,len;
1625d0c19d7SBarry Smith   const PetscInt *idx_i;
1635d0c19d7SBarry Smith   PetscInt       idx,isz,col,*n,*data1,**data1_start,*data2,*data2_i,*data,*data_i,
16413f74950SBarry Smith                  Mbs,i,j,k,*odata1,*odata2,
16513f74950SBarry Smith                  proc_id,**odata2_ptr,*ctable=0,*btable,len_max,len_est;
16613f74950SBarry Smith   PetscInt       proc_end=0,*iwork,len_unused,nodata2;
16713f74950SBarry Smith   PetscInt       ois_max; /* max no of is[] in each of processor */
168bfc6803cSHong Zhang   char           *t_p;
169632d0f97SHong Zhang   MPI_Comm       comm;
170e8527bf2SHong Zhang   MPI_Request    *s_waits1,*s_waits2,r_req;
171632d0f97SHong Zhang   MPI_Status     *s_status,r_status;
17245f43ab7SHong Zhang   PetscBT        *table;  /* mark indices of this processor's is[] */
173fc70d14eSHong Zhang   PetscBT        table_i;
174bfc6803cSHong Zhang   PetscBT        otable; /* mark indices of other processors' is[] */
175d0f46423SBarry Smith   PetscInt       bs=C->rmap->bs,Bn = c->B->cmap->n,Bnbs = Bn/bs,*Bowners;
17610eea884SHong Zhang   IS             garray_local,garray_gl;
1775483b11dSHong Zhang 
178632d0f97SHong Zhang   PetscFunctionBegin;
1797adad957SLisandro Dalcin   comm = ((PetscObject)C)->comm;
180632d0f97SHong Zhang   size = c->size;
181632d0f97SHong Zhang   rank = c->rank;
182632d0f97SHong Zhang   Mbs  = c->Mbs;
183632d0f97SHong Zhang 
184c910923dSHong Zhang   ierr = PetscObjectGetNewTag((PetscObject)C,&tag1);CHKERRQ(ierr);
185c910923dSHong Zhang   ierr = PetscObjectGetNewTag((PetscObject)C,&tag2);CHKERRQ(ierr);
186c910923dSHong Zhang 
187430a0127SHong Zhang   /* create tables used in
188430a0127SHong Zhang      step 1: table[i] - mark c->garray of proc [i]
18945f43ab7SHong Zhang      step 3: table[i] - mark indices of is[i] when whose=MINE
190430a0127SHong Zhang              table[0] - mark incideces of is[] when whose=OTHER */
191430a0127SHong Zhang   len = PetscMax(is_max, size);CHKERRQ(ierr);
19274ed9c26SBarry Smith   ierr = PetscMalloc2(len,PetscBT,&table,(Mbs/PETSC_BITS_PER_BYTE+1)*len,char,&t_p);CHKERRQ(ierr);
193430a0127SHong Zhang   for (i=0; i<len; i++) {
194430a0127SHong Zhang     table[i]  = t_p  + (Mbs/PETSC_BITS_PER_BYTE+1)*i;
195430a0127SHong Zhang   }
196430a0127SHong Zhang 
19713f74950SBarry Smith   ierr = MPI_Allreduce(&is_max,&ois_max,1,MPIU_INT,MPI_MAX,comm);CHKERRQ(ierr);
19810eea884SHong Zhang 
1995483b11dSHong Zhang   /* 1. Send this processor's is[] to other processors */
2005483b11dSHong Zhang   /*---------------------------------------------------*/
201e8527bf2SHong Zhang   /* allocate spaces */
20213f74950SBarry Smith   ierr = PetscMalloc(is_max*sizeof(PetscInt),&n);CHKERRQ(ierr);
2035483b11dSHong Zhang   len = 0;
204c910923dSHong Zhang   for (i=0; i<is_max; i++) {
205632d0f97SHong Zhang     ierr = ISGetLocalSize(is[i],&n[i]);CHKERRQ(ierr);
206632d0f97SHong Zhang     len += n[i];
207632d0f97SHong Zhang   }
208958c9bccSBarry Smith   if (!len) {
2095483b11dSHong Zhang     is_max = 0;
2105483b11dSHong Zhang   } else {
2115483b11dSHong Zhang     len += 1 + is_max; /* max length of data1 for one processor */
2125483b11dSHong Zhang   }
2135483b11dSHong Zhang 
214430a0127SHong Zhang 
21513f74950SBarry Smith   ierr = PetscMalloc((size*len+1)*sizeof(PetscInt),&data1);CHKERRQ(ierr);
21613f74950SBarry Smith   ierr = PetscMalloc(size*sizeof(PetscInt*),&data1_start);CHKERRQ(ierr);
2175483b11dSHong Zhang   for (i=0; i<size; i++) data1_start[i] = data1 + i*len;
2185483b11dSHong Zhang 
21940cb64c9SJed Brown   ierr = PetscMalloc4(size,PetscInt,&len_s,size,PetscInt,&btable,size,PetscInt,&iwork,size+1,PetscInt,&Bowners);CHKERRQ(ierr);
220e8527bf2SHong Zhang 
22176f244e2SHong Zhang   /* gather c->garray from all processors */
22270b3c8c7SBarry Smith   ierr = ISCreateGeneral(comm,Bnbs,c->garray,PETSC_COPY_VALUES,&garray_local);CHKERRQ(ierr);
22376f244e2SHong Zhang   ierr = ISAllGather(garray_local, &garray_gl);CHKERRQ(ierr);
22476f244e2SHong Zhang   ierr = ISDestroy(garray_local);CHKERRQ(ierr);
225a7cc72afSBarry Smith   ierr = MPI_Allgather(&Bnbs,1,MPIU_INT,Bowners+1,1,MPIU_INT,comm);CHKERRQ(ierr);
22676f244e2SHong Zhang   Bowners[0] = 0;
22776f244e2SHong Zhang   for (i=0; i<size; i++) Bowners[i+1] += Bowners[i];
22876f244e2SHong Zhang 
2295483b11dSHong Zhang   if (is_max){
230430a0127SHong Zhang     /* hash table ctable which maps c->row to proc_id) */
23113f74950SBarry Smith     ierr = PetscMalloc(Mbs*sizeof(PetscInt),&ctable);CHKERRQ(ierr);
2325483b11dSHong Zhang     for (proc_id=0,j=0; proc_id<size; proc_id++) {
233288a2dc7SJed Brown       for (; j<C->rmap->range[proc_id+1]/bs; j++) {
2345483b11dSHong Zhang         ctable[j] = proc_id;
2355483b11dSHong Zhang       }
2365483b11dSHong Zhang     }
2375483b11dSHong Zhang 
238430a0127SHong Zhang     /* hash tables marking c->garray */
23910eea884SHong Zhang     ierr = ISGetIndices(garray_gl,&idx_i);
2405483b11dSHong Zhang     for (i=0; i<size; i++){
241430a0127SHong Zhang       table_i = table[i];
242430a0127SHong Zhang       ierr    = PetscBTMemzero(Mbs,table_i);CHKERRQ(ierr);
243430a0127SHong Zhang       for (j = Bowners[i]; j<Bowners[i+1]; j++){ /* go through B cols of proc[i]*/
244430a0127SHong Zhang         ierr = PetscBTSet(table_i,idx_i[j]);CHKERRQ(ierr);
2455483b11dSHong Zhang       }
2465483b11dSHong Zhang     }
24710eea884SHong Zhang     ierr = ISRestoreIndices(garray_gl,&idx_i);CHKERRQ(ierr);
2485483b11dSHong Zhang   }  /* if (is_max) */
24976f244e2SHong Zhang   ierr = ISDestroy(garray_gl);CHKERRQ(ierr);
2505483b11dSHong Zhang 
2515483b11dSHong Zhang   /* evaluate communication - mesg to who, length, and buffer space */
252e8527bf2SHong Zhang   for (i=0; i<size; i++) len_s[i] = 0;
2535483b11dSHong Zhang 
2545483b11dSHong Zhang   /* header of data1 */
2555483b11dSHong Zhang   for (proc_id=0; proc_id<size; proc_id++){
2565483b11dSHong Zhang     iwork[proc_id] = 0;
2575483b11dSHong Zhang     *data1_start[proc_id] = is_max;
2585483b11dSHong Zhang     data1_start[proc_id]++;
2595483b11dSHong Zhang     for (j=0; j<is_max; j++) {
2605483b11dSHong Zhang       if (proc_id == rank){
2615483b11dSHong Zhang         *data1_start[proc_id] = n[j];
2625483b11dSHong Zhang       } else {
2635483b11dSHong Zhang         *data1_start[proc_id] = 0;
2645483b11dSHong Zhang       }
2655483b11dSHong Zhang       data1_start[proc_id]++;
2665483b11dSHong Zhang     }
2675483b11dSHong Zhang   }
2685483b11dSHong Zhang 
2695483b11dSHong Zhang   for (i=0; i<is_max; i++) {
2705483b11dSHong Zhang     ierr = ISGetIndices(is[i],&idx_i);CHKERRQ(ierr);
2715483b11dSHong Zhang     for (j=0; j<n[i]; j++){
2725483b11dSHong Zhang       idx = idx_i[j];
2735483b11dSHong Zhang       *data1_start[rank] = idx; data1_start[rank]++; /* for local proccessing */
2745483b11dSHong Zhang       proc_end = ctable[idx];
2755483b11dSHong Zhang       for (proc_id=0;  proc_id<=proc_end; proc_id++){ /* for others to process */
276e8527bf2SHong Zhang         if (proc_id == rank ) continue; /* done before this loop */
277430a0127SHong Zhang         if (proc_id < proc_end && !PetscBTLookup(table[proc_id],idx))
278430a0127SHong Zhang           continue;   /* no need for sending idx to [proc_id] */
2795483b11dSHong Zhang         *data1_start[proc_id] = idx; data1_start[proc_id]++;
2805483b11dSHong Zhang         len_s[proc_id]++;
2815483b11dSHong Zhang       }
2825483b11dSHong Zhang     }
2835483b11dSHong Zhang     /* update header data */
2842cfbe0a4SHong Zhang     for (proc_id=0; proc_id<size; proc_id++){
2855483b11dSHong Zhang       if (proc_id== rank) continue;
2865483b11dSHong Zhang       *(data1 + proc_id*len + 1 + i) = len_s[proc_id] - iwork[proc_id];
2875483b11dSHong Zhang       iwork[proc_id] = len_s[proc_id] ;
2885483b11dSHong Zhang     }
2895483b11dSHong Zhang     ierr = ISRestoreIndices(is[i],&idx_i);CHKERRQ(ierr);
290e8527bf2SHong Zhang   }
2915483b11dSHong Zhang 
2925483b11dSHong Zhang   nrqs = 0; nrqr = 0;
2935483b11dSHong Zhang   for (i=0; i<size; i++){
2945483b11dSHong Zhang     data1_start[i] = data1 + i*len;
2955483b11dSHong Zhang     if (len_s[i]){
2965483b11dSHong Zhang       nrqs++;
2975483b11dSHong Zhang       len_s[i] += 1 + is_max; /* add no. of header msg */
2985483b11dSHong Zhang     }
2995483b11dSHong Zhang   }
3005483b11dSHong Zhang 
3015483b11dSHong Zhang   for (i=0; i<is_max; i++) {
302a2a9f22aSHong Zhang     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
303632d0f97SHong Zhang   }
304bfc6803cSHong Zhang   ierr = PetscFree(n);CHKERRQ(ierr);
30505b42c5fSBarry Smith   ierr = PetscFree(ctable);CHKERRQ(ierr);
3065483b11dSHong Zhang 
3075483b11dSHong Zhang   /* Determine the number of messages to expect, their lengths, from from-ids */
3085483b11dSHong Zhang   ierr = PetscGatherNumberOfMessages(comm,PETSC_NULL,len_s,&nrqr);CHKERRQ(ierr);
3095483b11dSHong Zhang   ierr = PetscGatherMessageLengths(comm,nrqs,nrqr,len_s,&id_r1,&len_r1);CHKERRQ(ierr);
310632d0f97SHong Zhang 
311632d0f97SHong Zhang   /*  Now  post the sends */
31274ed9c26SBarry Smith   ierr = PetscMalloc2(size,MPI_Request,&s_waits1,size,MPI_Request,&s_waits2);CHKERRQ(ierr);
313632d0f97SHong Zhang   k = 0;
3145483b11dSHong Zhang   for (proc_id=0; proc_id<size; proc_id++){  /* send data1 to processor [proc_id] */
3155483b11dSHong Zhang     if (len_s[proc_id]){
31613f74950SBarry Smith       ierr = MPI_Isend(data1_start[proc_id],len_s[proc_id],MPIU_INT,proc_id,tag1,comm,s_waits1+k);CHKERRQ(ierr);
317632d0f97SHong Zhang       k++;
318632d0f97SHong Zhang     }
319632d0f97SHong Zhang   }
320632d0f97SHong Zhang 
32145f43ab7SHong Zhang   /* 2. Receive other's is[] and process. Then send back */
322bfc6803cSHong Zhang   /*-----------------------------------------------------*/
3235483b11dSHong Zhang   len = 0;
3245483b11dSHong Zhang   for (i=0; i<nrqr; i++){
3255483b11dSHong Zhang     if (len_r1[i] > len)len = len_r1[i];
3265483b11dSHong Zhang   }
32745f43ab7SHong Zhang   ierr = PetscFree(len_r1);CHKERRQ(ierr);
32845f43ab7SHong Zhang   ierr = PetscFree(id_r1);CHKERRQ(ierr);
32945f43ab7SHong Zhang 
33045f43ab7SHong Zhang   for (proc_id=0; proc_id<size; proc_id++)
33145f43ab7SHong Zhang     len_s[proc_id] = iwork[proc_id] = 0;
33245f43ab7SHong Zhang 
33313f74950SBarry Smith   ierr = PetscMalloc((len+1)*sizeof(PetscInt),&odata1);CHKERRQ(ierr);
33413f74950SBarry Smith   ierr = PetscMalloc(size*sizeof(PetscInt**),&odata2_ptr);CHKERRQ(ierr);
33576f244e2SHong Zhang   ierr = PetscBTCreate(Mbs,otable);CHKERRQ(ierr);
33610eea884SHong Zhang 
33710eea884SHong Zhang   len_max = ois_max*(Mbs+1);  /* max space storing all is[] for each receive */
338240e5896SHong Zhang   len_est = 2*len_max; /* estimated space of storing is[] for all receiving messages */
33913f74950SBarry Smith   ierr = PetscMalloc((len_est+1)*sizeof(PetscInt),&odata2);CHKERRQ(ierr);
34010eea884SHong Zhang   nodata2 = 0;       /* nodata2+1: num of PetscMalloc(,&odata2_ptr[]) called */
341240e5896SHong Zhang   odata2_ptr[nodata2] = odata2;
34210eea884SHong Zhang   len_unused = len_est; /* unused space in the array odata2_ptr[nodata2]-- needs to be >= len_max  */
34310eea884SHong Zhang 
344632d0f97SHong Zhang   k = 0;
3455483b11dSHong Zhang   while (k < nrqr){
346632d0f97SHong Zhang     /* Receive messages */
347bfc6803cSHong Zhang     ierr = MPI_Iprobe(MPI_ANY_SOURCE,tag1,comm,&flag,&r_status);CHKERRQ(ierr);
348632d0f97SHong Zhang     if (flag){
34913f74950SBarry Smith       ierr = MPI_Get_count(&r_status,MPIU_INT,&len);CHKERRQ(ierr);
350632d0f97SHong Zhang       proc_id = r_status.MPI_SOURCE;
35113f74950SBarry Smith       ierr = MPI_Irecv(odata1,len,MPIU_INT,proc_id,r_status.MPI_TAG,comm,&r_req);CHKERRQ(ierr);
3525483b11dSHong Zhang       ierr = MPI_Wait(&r_req,&r_status);CHKERRQ(ierr);
353632d0f97SHong Zhang 
354fc70d14eSHong Zhang       /*  Process messages */
355240e5896SHong Zhang       /*  make sure there is enough unused space in odata2 array */
35610eea884SHong Zhang       if (len_unused < len_max){ /* allocate more space for odata2 */
35713f74950SBarry Smith         ierr = PetscMalloc((len_est+1)*sizeof(PetscInt),&odata2);CHKERRQ(ierr);
358240e5896SHong Zhang         odata2_ptr[++nodata2] = odata2;
35910eea884SHong Zhang         len_unused = len_est;
36010eea884SHong Zhang       }
36110eea884SHong Zhang 
36210eea884SHong Zhang       ierr = MatIncreaseOverlap_MPISBAIJ_Local(C,odata1,OTHER,odata2,&otable);CHKERRQ(ierr);
363a2a9f22aSHong Zhang       len = 1 + odata2[0];
364a2a9f22aSHong Zhang       for (i=0; i<odata2[0]; i++){
365a2a9f22aSHong Zhang         len += odata2[1 + i];
366fc70d14eSHong Zhang       }
367632d0f97SHong Zhang 
368632d0f97SHong Zhang       /* Send messages back */
36913f74950SBarry Smith       ierr = MPI_Isend(odata2,len,MPIU_INT,proc_id,tag2,comm,s_waits2+k);CHKERRQ(ierr);
370fc70d14eSHong Zhang       k++;
37110eea884SHong Zhang       odata2     += len;
37210eea884SHong Zhang       len_unused -= len;
37345f43ab7SHong Zhang       len_s[proc_id] = len; /* num of messages sending back to [proc_id] by this proc */
374632d0f97SHong Zhang     }
3755483b11dSHong Zhang   }
3765483b11dSHong Zhang   ierr = PetscFree(odata1);CHKERRQ(ierr);
37745f43ab7SHong Zhang   ierr = PetscBTDestroy(otable);CHKERRQ(ierr);
378632d0f97SHong Zhang 
37945f43ab7SHong Zhang   /* 3. Do local work on this processor's is[] */
38045f43ab7SHong Zhang   /*-------------------------------------------*/
38145f43ab7SHong Zhang   /* make sure there is enough unused space in odata2(=data) array */
38245f43ab7SHong Zhang   len_max = is_max*(Mbs+1); /* max space storing all is[] for this processor */
38310eea884SHong Zhang   if (len_unused < len_max){ /* allocate more space for odata2 */
38413f74950SBarry Smith     ierr = PetscMalloc((len_est+1)*sizeof(PetscInt),&odata2);CHKERRQ(ierr);
385240e5896SHong Zhang     odata2_ptr[++nodata2] = odata2;
38610eea884SHong Zhang     len_unused = len_est;
38710eea884SHong Zhang   }
388bfc6803cSHong Zhang 
389240e5896SHong Zhang   data = odata2;
39045f43ab7SHong Zhang   ierr = MatIncreaseOverlap_MPISBAIJ_Local(C,data1_start[rank],MINE,data,table);CHKERRQ(ierr);
39145f43ab7SHong Zhang   ierr = PetscFree(data1_start);CHKERRQ(ierr);
39245f43ab7SHong Zhang 
39345f43ab7SHong Zhang   /* 4. Receive work done on other processors, then merge */
39445f43ab7SHong Zhang   /*------------------------------------------------------*/
39545f43ab7SHong Zhang   /* get max number of messages that this processor expects to recv */
39613f74950SBarry Smith   ierr = MPI_Allreduce(len_s,iwork,size,MPIU_INT,MPI_MAX,comm);CHKERRQ(ierr);
39713f74950SBarry Smith   ierr = PetscMalloc((iwork[rank]+1)*sizeof(PetscInt),&data2);CHKERRQ(ierr);
39874ed9c26SBarry Smith   ierr = PetscFree4(len_s,btable,iwork,Bowners);CHKERRQ(ierr);
39945f43ab7SHong Zhang 
400632d0f97SHong Zhang   k = 0;
4015483b11dSHong Zhang   while (k < nrqs){
402632d0f97SHong Zhang     /* Receive messages */
403c910923dSHong Zhang     ierr = MPI_Iprobe(MPI_ANY_SOURCE,tag2,comm,&flag,&r_status);
404632d0f97SHong Zhang     if (flag){
40513f74950SBarry Smith       ierr = MPI_Get_count(&r_status,MPIU_INT,&len);CHKERRQ(ierr);
406632d0f97SHong Zhang       proc_id = r_status.MPI_SOURCE;
40713f74950SBarry Smith       ierr = MPI_Irecv(data2,len,MPIU_INT,proc_id,r_status.MPI_TAG,comm,&r_req);CHKERRQ(ierr);
4085483b11dSHong Zhang       ierr = MPI_Wait(&r_req,&r_status);CHKERRQ(ierr);
4095483b11dSHong Zhang       if (len > 1+is_max){ /* Add data2 into data */
4100472cc68SHong Zhang         data2_i = data2 + 1 + is_max;
411fc70d14eSHong Zhang         for (i=0; i<is_max; i++){
412fc70d14eSHong Zhang           table_i = table[i];
413bfc6803cSHong Zhang           data_i  = data + 1 + is_max + Mbs*i;
414bfc6803cSHong Zhang           isz     = data[1+i];
4150472cc68SHong Zhang           for (j=0; j<data2[1+i]; j++){
4160472cc68SHong Zhang             col = data2_i[j];
417bfc6803cSHong Zhang             if (!PetscBTLookupSet(table_i,col)) {data_i[isz++] = col;}
418fc70d14eSHong Zhang           }
419bfc6803cSHong Zhang           data[1+i] = isz;
4200472cc68SHong Zhang           if (i < is_max - 1) data2_i += data2[1+i];
421fc70d14eSHong Zhang         }
4225483b11dSHong Zhang       }
423632d0f97SHong Zhang       k++;
424632d0f97SHong Zhang     }
4255483b11dSHong Zhang   }
42645f43ab7SHong Zhang   ierr = PetscFree(data2);CHKERRQ(ierr);
42774ed9c26SBarry Smith   ierr = PetscFree2(table,t_p);CHKERRQ(ierr);
4285483b11dSHong Zhang 
4295483b11dSHong Zhang   /* phase 1 sends are complete */
4305483b11dSHong Zhang   ierr = PetscMalloc(size*sizeof(MPI_Status),&s_status);CHKERRQ(ierr);
4310c468ba9SBarry Smith   if (nrqs) {ierr = MPI_Waitall(nrqs,s_waits1,s_status);CHKERRQ(ierr);}
4325483b11dSHong Zhang   ierr = PetscFree(data1);CHKERRQ(ierr);
4335483b11dSHong Zhang 
434240e5896SHong Zhang   /* phase 2 sends are complete */
4350c468ba9SBarry Smith   if (nrqr){ierr = MPI_Waitall(nrqr,s_waits2,s_status);CHKERRQ(ierr);}
43674ed9c26SBarry Smith   ierr = PetscFree2(s_waits1,s_waits2);CHKERRQ(ierr);
43745f43ab7SHong Zhang   ierr = PetscFree(s_status);CHKERRQ(ierr);
438632d0f97SHong Zhang 
439c910923dSHong Zhang   /* 5. Create new is[] */
440c910923dSHong Zhang   /*--------------------*/
441c910923dSHong Zhang   for (i=0; i<is_max; i++) {
442bfc6803cSHong Zhang     data_i = data + 1 + is_max + Mbs*i;
44370b3c8c7SBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,data[1+i],data_i,PETSC_COPY_VALUES,is+i);CHKERRQ(ierr);
444632d0f97SHong Zhang   }
44545f43ab7SHong Zhang   for (k=0; k<=nodata2; k++){
44645f43ab7SHong Zhang     ierr = PetscFree(odata2_ptr[k]);CHKERRQ(ierr);
44745f43ab7SHong Zhang   }
44845f43ab7SHong Zhang   ierr = PetscFree(odata2_ptr);CHKERRQ(ierr);
4495483b11dSHong Zhang 
450632d0f97SHong Zhang   PetscFunctionReturn(0);
451632d0f97SHong Zhang }
452632d0f97SHong Zhang 
453632d0f97SHong Zhang #undef __FUNCT__
454632d0f97SHong Zhang #define __FUNCT__ "MatIncreaseOverlap_MPISBAIJ_Local"
455632d0f97SHong Zhang /*
456dc008846SHong Zhang    MatIncreaseOverlap_MPISBAIJ_Local - Called by MatIncreaseOverlap, to do
457632d0f97SHong Zhang        the work on the local processor.
458632d0f97SHong Zhang 
459632d0f97SHong Zhang      Inputs:
460632d0f97SHong Zhang       C      - MAT_MPISBAIJ;
461bfc6803cSHong Zhang       data   - holds is[]. See MatIncreaseOverlap_MPISBAIJ_Once() for the format.
462bfc6803cSHong Zhang       whose  - whose is[] to be processed,
463bfc6803cSHong Zhang                MINE:  this processor's is[]
464bfc6803cSHong Zhang                OTHER: other processor's is[]
465632d0f97SHong Zhang      Output:
46610eea884SHong Zhang        nidx  - whose = MINE:
4670472cc68SHong Zhang                      holds input and newly found indices in the same format as data
4680472cc68SHong Zhang                whose = OTHER:
4690472cc68SHong Zhang                      only holds the newly found indices
4700472cc68SHong Zhang        table - table[i]: mark the indices of is[i], i=0,...,is_max. Used only in the case 'whose=MINE'.
471632d0f97SHong Zhang */
47276f244e2SHong Zhang /* Would computation be reduced by swapping the loop 'for each is' and 'for each row'? */
47313f74950SBarry Smith static PetscErrorCode MatIncreaseOverlap_MPISBAIJ_Local(Mat C,PetscInt *data,PetscInt whose,PetscInt *nidx,PetscBT *table)
474632d0f97SHong Zhang {
475632d0f97SHong Zhang   Mat_MPISBAIJ   *c = (Mat_MPISBAIJ*)C->data;
476dc008846SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)(c->A)->data;
477dc008846SHong Zhang   Mat_SeqBAIJ    *b = (Mat_SeqBAIJ*)(c->B)->data;
478dfbe8321SBarry Smith   PetscErrorCode ierr;
47913f74950SBarry Smith   PetscInt       row,mbs,Mbs,*nidx_i,col,col_max,isz,isz0,*ai,*aj,*bi,*bj,*garray,rstart,l;
48013f74950SBarry Smith   PetscInt       a_start,a_end,b_start,b_end,i,j,k,is_max,*idx_i,n;
481bfc6803cSHong Zhang   PetscBT        table0;  /* mark the indices of input is[] for look up */
482bfc6803cSHong Zhang   PetscBT        table_i; /* poits to i-th table. When whose=OTHER, a single table is used for all is[] */
483632d0f97SHong Zhang 
484632d0f97SHong Zhang   PetscFunctionBegin;
48531f99336SHong Zhang   Mbs = c->Mbs; mbs = a->mbs;
486dc008846SHong Zhang   ai = a->i; aj = a->j;
487dc008846SHong Zhang   bi = b->i; bj = b->j;
488632d0f97SHong Zhang   garray = c->garray;
489899cda47SBarry Smith   rstart = c->rstartbs;
490dc008846SHong Zhang   is_max = data[0];
491632d0f97SHong Zhang 
492fc70d14eSHong Zhang   ierr = PetscBTCreate(Mbs,table0);CHKERRQ(ierr);
493fc70d14eSHong Zhang 
494fc70d14eSHong Zhang   nidx[0] = is_max;
495fc70d14eSHong Zhang   idx_i   = data + is_max + 1; /* ptr to input is[0] array */
496bfc6803cSHong Zhang   nidx_i  = nidx + is_max + 1; /* ptr to output is[0] array */
497dc008846SHong Zhang   for (i=0; i<is_max; i++) { /* for each is */
498dc008846SHong Zhang     isz  = 0;
499fc70d14eSHong Zhang     n = data[1+i]; /* size of input is[i] */
500dc008846SHong Zhang 
50176f244e2SHong Zhang     /* initialize and set table_i(mark idx and nidx) and table0(only mark idx) */
502bfc6803cSHong Zhang     if (whose == MINE){ /* process this processor's is[] */
503bfc6803cSHong Zhang       table_i = table[i];
5040472cc68SHong Zhang       nidx_i  = nidx + 1+ is_max + Mbs*i;
505bfc6803cSHong Zhang     } else {            /* process other processor's is[] - only use one temp table */
506430a0127SHong Zhang       table_i = table[0];
507bfc6803cSHong Zhang     }
508bfc6803cSHong Zhang     ierr = PetscBTMemzero(Mbs,table_i);CHKERRQ(ierr);
509bfc6803cSHong Zhang     ierr = PetscBTMemzero(Mbs,table0);CHKERRQ(ierr);
51076f244e2SHong Zhang     if (n==0) {
51176f244e2SHong Zhang        nidx[1+i] = 0; /* size of new is[i] */
51276f244e2SHong Zhang        continue;
51376f244e2SHong Zhang     }
51476f244e2SHong Zhang 
51576f244e2SHong Zhang     isz0 = 0; col_max = 0;
516dc008846SHong Zhang     for (j=0; j<n; j++){
517dc008846SHong Zhang       col = idx_i[j];
518e32f2f54SBarry Smith       if (col >= Mbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"index col %D >= Mbs %D",col,Mbs);
519bfc6803cSHong Zhang       if(!PetscBTLookupSet(table_i,col)) {
520bfc6803cSHong Zhang         ierr = PetscBTSet(table0,col);CHKERRQ(ierr);
5210472cc68SHong Zhang         if (whose == MINE) {nidx_i[isz0] = col;}
522dbe03f88SHong Zhang         if (col_max < col) col_max = col;
523bfc6803cSHong Zhang         isz0++;
524bfc6803cSHong Zhang       }
525632d0f97SHong Zhang     }
526dc008846SHong Zhang 
5270472cc68SHong Zhang     if (whose == MINE) {isz = isz0;}
528fc70d14eSHong Zhang     k = 0;  /* no. of indices from input is[i] that have been examined */
529dc008846SHong Zhang     for (row=0; row<mbs; row++){
530dc008846SHong Zhang       a_start = ai[row]; a_end = ai[row+1];
531dc008846SHong Zhang       b_start = bi[row]; b_end = bi[row+1];
5320472cc68SHong Zhang       if (PetscBTLookup(table0,row+rstart)){ /* row is on input is[i]:
5330472cc68SHong Zhang                                                 do row search: collect all col in this row */
534dc008846SHong Zhang         for (l = a_start; l<a_end ; l++){ /* Amat */
535dc008846SHong Zhang           col = aj[l] + rstart;
536fc70d14eSHong Zhang           if (!PetscBTLookupSet(table_i,col)) {nidx_i[isz++] = col;}
537dc008846SHong Zhang         }
538dc008846SHong Zhang         for (l = b_start; l<b_end ; l++){ /* Bmat */
539dc008846SHong Zhang           col = garray[bj[l]];
540fc70d14eSHong Zhang           if (!PetscBTLookupSet(table_i,col)) {nidx_i[isz++] = col;}
541dc008846SHong Zhang         }
542dc008846SHong Zhang         k++;
543dc008846SHong Zhang         if (k >= isz0) break; /* for (row=0; row<mbs; row++) */
5440472cc68SHong Zhang       } else { /* row is not on input is[i]:
5450472cc68SHong Zhang                   do col serach: add row onto nidx_i if there is a col in nidx_i */
546dc008846SHong Zhang         for (l = a_start; l<a_end ; l++){ /* Amat */
547dc008846SHong Zhang           col = aj[l] + rstart;
54876f244e2SHong Zhang           if (col > col_max) break;
549dc008846SHong Zhang           if (PetscBTLookup(table0,col)){
550fc70d14eSHong Zhang             if (!PetscBTLookupSet(table_i,row+rstart)) {nidx_i[isz++] = row+rstart;}
551dc008846SHong Zhang             break; /* for l = start; l<end ; l++) */
552632d0f97SHong Zhang           }
553632d0f97SHong Zhang         }
554dc008846SHong Zhang         for (l = b_start; l<b_end ; l++){ /* Bmat */
555dc008846SHong Zhang           col = garray[bj[l]];
55676f244e2SHong Zhang           if (col > col_max) break;
557dc008846SHong Zhang           if (PetscBTLookup(table0,col)){
558fc70d14eSHong Zhang             if (!PetscBTLookupSet(table_i,row+rstart)) {nidx_i[isz++] = row+rstart;}
559dc008846SHong Zhang             break; /* for l = start; l<end ; l++) */
560632d0f97SHong Zhang           }
561dc008846SHong Zhang         }
562dc008846SHong Zhang       }
563bfc6803cSHong Zhang     }
564dc008846SHong Zhang 
565dc008846SHong Zhang     if (i < is_max - 1){
566fc70d14eSHong Zhang       idx_i  += n;   /* ptr to input is[i+1] array */
567bfc6803cSHong Zhang       nidx_i += isz; /* ptr to output is[i+1] array */
568dc008846SHong Zhang     }
569fc70d14eSHong Zhang     nidx[1+i] = isz; /* size of new is[i] */
5701ab97a2aSSatish Balay   } /* for each is */
571dc008846SHong Zhang   ierr = PetscBTDestroy(table0);CHKERRQ(ierr);
572632d0f97SHong Zhang 
573632d0f97SHong Zhang   PetscFunctionReturn(0);
574632d0f97SHong Zhang }
575632d0f97SHong Zhang 
576632d0f97SHong Zhang 
577