xref: /petsc/src/mat/impls/sbaij/mpi/sbaijov.c (revision d2fd7bfc6f0fd2e1d083decbb7cc7d77e16824f0)
1 
2 /*
3    Routines to compute overlapping regions of a parallel MPI matrix.
4    Used for finding submatrices that were shared across processors.
5 */
6 #include <../src/mat/impls/sbaij/mpi/mpisbaij.h>
7 #include <petscbt.h>
8 
9 static PetscErrorCode MatIncreaseOverlap_MPISBAIJ_Once(Mat,PetscInt,IS*);
10 static PetscErrorCode MatIncreaseOverlap_MPISBAIJ_Local(Mat,PetscInt*,PetscInt,PetscInt*,PetscBT*);
11 
12 PetscErrorCode MatIncreaseOverlap_MPISBAIJ(Mat C,PetscInt is_max,IS is[],PetscInt ov)
13 {
14   PetscErrorCode ierr;
15   PetscInt       i,N=C->cmap->N, bs=C->rmap->bs,M=C->rmap->N,Mbs=M/bs,*nidx,isz,iov;
16   IS             *is_new,*is_row;
17   Mat            *submats;
18   Mat_MPISBAIJ   *c=(Mat_MPISBAIJ*)C->data;
19   Mat_SeqSBAIJ   *asub_i;
20   PetscBT        table;
21   PetscInt       *ai,brow,nz,nis,l,nmax,nstages_local,nstages,max_no,pos;
22   const PetscInt *idx;
23   PetscBool      flg;
24 
25   PetscFunctionBegin;
26   ierr = PetscMalloc1(is_max,&is_new);CHKERRQ(ierr);
27   /* Convert the indices into block format */
28   ierr = ISCompressIndicesGeneral(N,C->rmap->n,bs,is_max,is,is_new);CHKERRQ(ierr);
29   if (ov < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative overlap specified\n");
30 
31   /* ----- previous non-scalable implementation ----- */
32   flg  = PETSC_FALSE;
33   ierr = PetscOptionsHasName(NULL,NULL, "-IncreaseOverlap_old", &flg);CHKERRQ(ierr);
34   if (flg) { /* previous non-scalable implementation */
35     printf("use previous non-scalable implementation...\n");
36     for (i=0; i<ov; ++i) {
37       ierr = MatIncreaseOverlap_MPISBAIJ_Once(C,is_max,is_new);CHKERRQ(ierr);
38     }
39   } else { /* implementation using modified BAIJ routines */
40 
41     ierr = PetscMalloc1(Mbs+1,&nidx);CHKERRQ(ierr);
42     ierr = PetscBTCreate(Mbs,&table);CHKERRQ(ierr); /* for column search */
43 
44     /* Create is_row */
45     ierr = PetscMalloc1(is_max,&is_row);CHKERRQ(ierr);
46     ierr = ISCreateStride(PETSC_COMM_SELF,Mbs,0,1,&is_row[0]);CHKERRQ(ierr);
47 
48     for (i=1; i<is_max; i++) {
49       is_row[i]  = is_row[0]; /* reuse is_row[0] */
50     }
51 
52     /* Allocate memory to hold all the submatrices - Modified from MatCreateSubMatrices_MPIBAIJ() */
53     ierr = PetscMalloc1(is_max+1,&submats);CHKERRQ(ierr);
54 
55     /* Determine the number of stages through which submatrices are done */
56     nmax = 20*1000000 / (c->Nbs * sizeof(PetscInt));
57     if (!nmax) nmax = 1;
58     nstages_local = is_max/nmax + ((is_max % nmax) ? 1 : 0);
59 
60     /* Make sure every processor loops through the nstages */
61     ierr = MPIU_Allreduce(&nstages_local,&nstages,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)C));CHKERRQ(ierr);
62 
63     for (iov=0; iov<ov; ++iov) {
64       /* 1) Get submats for column search */
65       for (i=0,pos=0; i<nstages; i++) {
66         if (pos+nmax <= is_max) max_no = nmax;
67         else if (pos == is_max) max_no = 0;
68         else                    max_no = is_max-pos;
69         c->ijonly = PETSC_TRUE; /* only matrix data structures are requested */
70         /* The resulting submatrices should be BAIJ, not SBAIJ, hence we change this value to trigger that */
71         ierr      = PetscStrcpy(((PetscObject)c->A)->type_name,MATSEQBAIJ);CHKERRQ(ierr);
72         ierr      = MatCreateSubMatrices_MPIBAIJ_local(C,max_no,is_row+pos,is_new+pos,MAT_INITIAL_MATRIX,submats+pos);CHKERRQ(ierr);
73         ierr      = PetscStrcpy(((PetscObject)c->A)->type_name,MATSEQSBAIJ);CHKERRQ(ierr);
74         pos      += max_no;
75       }
76 
77       /* 2) Row search */
78       ierr = MatIncreaseOverlap_MPIBAIJ_Once(C,is_max,is_new);CHKERRQ(ierr);
79 
80       /* 3) Column search */
81       for (i=0; i<is_max; i++) {
82         asub_i = (Mat_SeqSBAIJ*)submats[i]->data;
83         ai     = asub_i->i;
84 
85         /* put is_new obtained from MatIncreaseOverlap_MPIBAIJ() to table */
86         ierr = PetscBTMemzero(Mbs,table);CHKERRQ(ierr);
87 
88         ierr = ISGetIndices(is_new[i],&idx);CHKERRQ(ierr);
89         ierr = ISGetLocalSize(is_new[i],&nis);CHKERRQ(ierr);
90         for (l=0; l<nis; l++) {
91           ierr    = PetscBTSet(table,idx[l]);CHKERRQ(ierr);
92           nidx[l] = idx[l];
93         }
94         isz = nis;
95 
96         /* add column entries to table */
97         for (brow=0; brow<Mbs; brow++) {
98           nz = ai[brow+1] - ai[brow];
99           if (nz) {
100             if (!PetscBTLookupSet(table,brow)) nidx[isz++] = brow;
101           }
102         }
103         ierr = ISRestoreIndices(is_new[i],&idx);CHKERRQ(ierr);
104         ierr = ISDestroy(&is_new[i]);CHKERRQ(ierr);
105 
106         /* create updated is_new */
107         ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,is_new+i);CHKERRQ(ierr);
108       }
109 
110       /* Free tmp spaces */
111       for (i=0; i<is_max; i++) {
112         ierr = MatDestroy(&submats[i]);CHKERRQ(ierr);
113       }
114     }
115 
116     ierr = PetscBTDestroy(&table);CHKERRQ(ierr);
117     ierr = PetscFree(submats);CHKERRQ(ierr);
118     ierr = ISDestroy(&is_row[0]);CHKERRQ(ierr);
119     ierr = PetscFree(is_row);CHKERRQ(ierr);
120     ierr = PetscFree(nidx);CHKERRQ(ierr);
121 
122   }
123 
124   for (i=0; i<is_max; i++) {ierr = ISDestroy(&is[i]);CHKERRQ(ierr);}
125   ierr = ISExpandIndicesGeneral(N,N,bs,is_max,is_new,is);CHKERRQ(ierr);
126 
127   for (i=0; i<is_max; i++) {ierr = ISDestroy(&is_new[i]);CHKERRQ(ierr);}
128   ierr = PetscFree(is_new);CHKERRQ(ierr);
129   PetscFunctionReturn(0);
130 }
131 
132 typedef enum {MINE,OTHER} WhoseOwner;
133 /*  data1, odata1 and odata2 are packed in the format (for communication):
134        data[0]          = is_max, no of is
135        data[1]          = size of is[0]
136         ...
137        data[is_max]     = size of is[is_max-1]
138        data[is_max + 1] = data(is[0])
139         ...
140        data[is_max+1+sum(size of is[k]), k=0,...,i-1] = data(is[i])
141         ...
142    data2 is packed in the format (for creating output is[]):
143        data[0]          = is_max, no of is
144        data[1]          = size of is[0]
145         ...
146        data[is_max]     = size of is[is_max-1]
147        data[is_max + 1] = data(is[0])
148         ...
149        data[is_max + 1 + Mbs*i) = data(is[i])
150         ...
151 */
152 static PetscErrorCode MatIncreaseOverlap_MPISBAIJ_Once(Mat C,PetscInt is_max,IS is[])
153 {
154   Mat_MPISBAIJ   *c = (Mat_MPISBAIJ*)C->data;
155   PetscErrorCode ierr;
156   PetscMPIInt    size,rank,tag1,tag2,*len_s,nrqr,nrqs,*id_r1,*len_r1,flag,len,*iwork;
157   const PetscInt *idx_i;
158   PetscInt       idx,isz,col,*n,*data1,**data1_start,*data2,*data2_i,*data,*data_i;
159   PetscInt       Mbs,i,j,k,*odata1,*odata2;
160   PetscInt       proc_id,**odata2_ptr,*ctable=0,*btable,len_max,len_est;
161   PetscInt       proc_end=0,len_unused,nodata2;
162   PetscInt       ois_max; /* max no of is[] in each of processor */
163   char           *t_p;
164   MPI_Comm       comm;
165   MPI_Request    *s_waits1,*s_waits2,r_req;
166   MPI_Status     *s_status,r_status;
167   PetscBT        *table;  /* mark indices of this processor's is[] */
168   PetscBT        table_i;
169   PetscBT        otable; /* mark indices of other processors' is[] */
170   PetscInt       bs=C->rmap->bs,Bn = c->B->cmap->n,Bnbs = Bn/bs,*Bowners;
171   IS             garray_local,garray_gl;
172 
173   PetscFunctionBegin;
174   ierr = PetscObjectGetComm((PetscObject)C,&comm);CHKERRQ(ierr);
175   size = c->size;
176   rank = c->rank;
177   Mbs  = c->Mbs;
178 
179   ierr = PetscObjectGetNewTag((PetscObject)C,&tag1);CHKERRQ(ierr);
180   ierr = PetscObjectGetNewTag((PetscObject)C,&tag2);CHKERRQ(ierr);
181 
182   /* create tables used in
183      step 1: table[i] - mark c->garray of proc [i]
184      step 3: table[i] - mark indices of is[i] when whose=MINE
185              table[0] - mark incideces of is[] when whose=OTHER */
186   len  = PetscMax(is_max, size);CHKERRQ(ierr);
187   ierr = PetscMalloc2(len,&table,(Mbs/PETSC_BITS_PER_BYTE+1)*len,&t_p);CHKERRQ(ierr);
188   for (i=0; i<len; i++) {
189     table[i] = t_p  + (Mbs/PETSC_BITS_PER_BYTE+1)*i;
190   }
191 
192   ierr = MPIU_Allreduce(&is_max,&ois_max,1,MPIU_INT,MPI_MAX,comm);CHKERRQ(ierr);
193 
194   /* 1. Send this processor's is[] to other processors */
195   /*---------------------------------------------------*/
196   /* allocate spaces */
197   ierr = PetscMalloc1(is_max,&n);CHKERRQ(ierr);
198   len  = 0;
199   for (i=0; i<is_max; i++) {
200     ierr = ISGetLocalSize(is[i],&n[i]);CHKERRQ(ierr);
201     len += n[i];
202   }
203   if (!len) {
204     is_max = 0;
205   } else {
206     len += 1 + is_max; /* max length of data1 for one processor */
207   }
208 
209 
210   ierr = PetscMalloc1(size*len+1,&data1);CHKERRQ(ierr);
211   ierr = PetscMalloc1(size,&data1_start);CHKERRQ(ierr);
212   for (i=0; i<size; i++) data1_start[i] = data1 + i*len;
213 
214   ierr = PetscMalloc4(size,&len_s,size,&btable,size,&iwork,size+1,&Bowners);CHKERRQ(ierr);
215 
216   /* gather c->garray from all processors */
217   ierr = ISCreateGeneral(comm,Bnbs,c->garray,PETSC_COPY_VALUES,&garray_local);CHKERRQ(ierr);
218   ierr = ISAllGather(garray_local, &garray_gl);CHKERRQ(ierr);
219   ierr = ISDestroy(&garray_local);CHKERRQ(ierr);
220   ierr = MPI_Allgather(&Bnbs,1,MPIU_INT,Bowners+1,1,MPIU_INT,comm);CHKERRQ(ierr);
221 
222   Bowners[0] = 0;
223   for (i=0; i<size; i++) Bowners[i+1] += Bowners[i];
224 
225   if (is_max) {
226     /* hash table ctable which maps c->row to proc_id) */
227     ierr = PetscMalloc1(Mbs,&ctable);CHKERRQ(ierr);
228     for (proc_id=0,j=0; proc_id<size; proc_id++) {
229       for (; j<C->rmap->range[proc_id+1]/bs; j++) ctable[j] = proc_id;
230     }
231 
232     /* hash tables marking c->garray */
233     ierr = ISGetIndices(garray_gl,&idx_i);CHKERRQ(ierr);
234     for (i=0; i<size; i++) {
235       table_i = table[i];
236       ierr    = PetscBTMemzero(Mbs,table_i);CHKERRQ(ierr);
237       for (j = Bowners[i]; j<Bowners[i+1]; j++) { /* go through B cols of proc[i]*/
238         ierr = PetscBTSet(table_i,idx_i[j]);CHKERRQ(ierr);
239       }
240     }
241     ierr = ISRestoreIndices(garray_gl,&idx_i);CHKERRQ(ierr);
242   }  /* if (is_max) */
243   ierr = ISDestroy(&garray_gl);CHKERRQ(ierr);
244 
245   /* evaluate communication - mesg to who, length, and buffer space */
246   for (i=0; i<size; i++) len_s[i] = 0;
247 
248   /* header of data1 */
249   for (proc_id=0; proc_id<size; proc_id++) {
250     iwork[proc_id]        = 0;
251     *data1_start[proc_id] = is_max;
252     data1_start[proc_id]++;
253     for (j=0; j<is_max; j++) {
254       if (proc_id == rank) {
255         *data1_start[proc_id] = n[j];
256       } else {
257         *data1_start[proc_id] = 0;
258       }
259       data1_start[proc_id]++;
260     }
261   }
262 
263   for (i=0; i<is_max; i++) {
264     ierr = ISGetIndices(is[i],&idx_i);CHKERRQ(ierr);
265     for (j=0; j<n[i]; j++) {
266       idx                = idx_i[j];
267       *data1_start[rank] = idx; data1_start[rank]++; /* for local proccessing */
268       proc_end           = ctable[idx];
269       for (proc_id=0; proc_id<=proc_end; proc_id++) {  /* for others to process */
270         if (proc_id == rank) continue; /* done before this loop */
271         if (proc_id < proc_end && !PetscBTLookup(table[proc_id],idx)) continue; /* no need for sending idx to [proc_id] */
272         *data1_start[proc_id] = idx; data1_start[proc_id]++;
273         len_s[proc_id]++;
274       }
275     }
276     /* update header data */
277     for (proc_id=0; proc_id<size; proc_id++) {
278       if (proc_id== rank) continue;
279       *(data1 + proc_id*len + 1 + i) = len_s[proc_id] - iwork[proc_id];
280       iwork[proc_id]                 = len_s[proc_id];
281     }
282     ierr = ISRestoreIndices(is[i],&idx_i);CHKERRQ(ierr);
283   }
284 
285   nrqs = 0; nrqr = 0;
286   for (i=0; i<size; i++) {
287     data1_start[i] = data1 + i*len;
288     if (len_s[i]) {
289       nrqs++;
290       len_s[i] += 1 + is_max; /* add no. of header msg */
291     }
292   }
293 
294   for (i=0; i<is_max; i++) {
295     ierr = ISDestroy(&is[i]);CHKERRQ(ierr);
296   }
297   ierr = PetscFree(n);CHKERRQ(ierr);
298   ierr = PetscFree(ctable);CHKERRQ(ierr);
299 
300   /* Determine the number of messages to expect, their lengths, from from-ids */
301   ierr = PetscGatherNumberOfMessages(comm,NULL,len_s,&nrqr);CHKERRQ(ierr);
302   ierr = PetscGatherMessageLengths(comm,nrqs,nrqr,len_s,&id_r1,&len_r1);CHKERRQ(ierr);
303 
304   /*  Now  post the sends */
305   ierr = PetscMalloc2(size,&s_waits1,size,&s_waits2);CHKERRQ(ierr);
306   k    = 0;
307   for (proc_id=0; proc_id<size; proc_id++) {  /* send data1 to processor [proc_id] */
308     if (len_s[proc_id]) {
309       ierr = MPI_Isend(data1_start[proc_id],len_s[proc_id],MPIU_INT,proc_id,tag1,comm,s_waits1+k);CHKERRQ(ierr);
310       k++;
311     }
312   }
313 
314   /* 2. Receive other's is[] and process. Then send back */
315   /*-----------------------------------------------------*/
316   len = 0;
317   for (i=0; i<nrqr; i++) {
318     if (len_r1[i] > len) len = len_r1[i];
319   }
320   ierr = PetscFree(len_r1);CHKERRQ(ierr);
321   ierr = PetscFree(id_r1);CHKERRQ(ierr);
322 
323   for (proc_id=0; proc_id<size; proc_id++) len_s[proc_id] = iwork[proc_id] = 0;
324 
325   ierr = PetscMalloc1(len+1,&odata1);CHKERRQ(ierr);
326   ierr = PetscMalloc1(size,&odata2_ptr);CHKERRQ(ierr);
327   ierr = PetscBTCreate(Mbs,&otable);CHKERRQ(ierr);
328 
329   len_max = ois_max*(Mbs+1); /* max space storing all is[] for each receive */
330   len_est = 2*len_max;       /* estimated space of storing is[] for all receiving messages */
331   ierr    = PetscMalloc1(len_est+1,&odata2);CHKERRQ(ierr);
332   nodata2 = 0;               /* nodata2+1: num of PetscMalloc(,&odata2_ptr[]) called */
333 
334   odata2_ptr[nodata2] = odata2;
335 
336   len_unused = len_est; /* unused space in the array odata2_ptr[nodata2]-- needs to be >= len_max  */
337 
338   k = 0;
339   while (k < nrqr) {
340     /* Receive messages */
341     ierr = MPI_Iprobe(MPI_ANY_SOURCE,tag1,comm,&flag,&r_status);CHKERRQ(ierr);
342     if (flag) {
343       ierr    = MPI_Get_count(&r_status,MPIU_INT,&len);CHKERRQ(ierr);
344       proc_id = r_status.MPI_SOURCE;
345       ierr    = MPI_Irecv(odata1,len,MPIU_INT,proc_id,r_status.MPI_TAG,comm,&r_req);CHKERRQ(ierr);
346       ierr    = MPI_Wait(&r_req,&r_status);CHKERRQ(ierr);
347 
348       /*  Process messages */
349       /*  make sure there is enough unused space in odata2 array */
350       if (len_unused < len_max) { /* allocate more space for odata2 */
351         ierr = PetscMalloc1(len_est+1,&odata2);CHKERRQ(ierr);
352 
353         odata2_ptr[++nodata2] = odata2;
354 
355         len_unused = len_est;
356       }
357 
358       ierr = MatIncreaseOverlap_MPISBAIJ_Local(C,odata1,OTHER,odata2,&otable);CHKERRQ(ierr);
359       len  = 1 + odata2[0];
360       for (i=0; i<odata2[0]; i++) len += odata2[1 + i];
361 
362       /* Send messages back */
363       ierr = MPI_Isend(odata2,len,MPIU_INT,proc_id,tag2,comm,s_waits2+k);CHKERRQ(ierr);
364       k++;
365       odata2        += len;
366       len_unused    -= len;
367       len_s[proc_id] = len; /* num of messages sending back to [proc_id] by this proc */
368     }
369   }
370   ierr = PetscFree(odata1);CHKERRQ(ierr);
371   ierr = PetscBTDestroy(&otable);CHKERRQ(ierr);
372 
373   /* 3. Do local work on this processor's is[] */
374   /*-------------------------------------------*/
375   /* make sure there is enough unused space in odata2(=data) array */
376   len_max = is_max*(Mbs+1); /* max space storing all is[] for this processor */
377   if (len_unused < len_max) { /* allocate more space for odata2 */
378     ierr = PetscMalloc1(len_est+1,&odata2);CHKERRQ(ierr);
379 
380     odata2_ptr[++nodata2] = odata2;
381   }
382 
383   data = odata2;
384   ierr = MatIncreaseOverlap_MPISBAIJ_Local(C,data1_start[rank],MINE,data,table);CHKERRQ(ierr);
385   ierr = PetscFree(data1_start);CHKERRQ(ierr);
386 
387   /* 4. Receive work done on other processors, then merge */
388   /*------------------------------------------------------*/
389   /* get max number of messages that this processor expects to recv */
390   ierr = MPIU_Allreduce(len_s,iwork,size,MPI_INT,MPI_MAX,comm);CHKERRQ(ierr);
391   ierr = PetscMalloc1(iwork[rank]+1,&data2);CHKERRQ(ierr);
392   ierr = PetscFree4(len_s,btable,iwork,Bowners);CHKERRQ(ierr);
393 
394   k = 0;
395   while (k < nrqs) {
396     /* Receive messages */
397     ierr = MPI_Iprobe(MPI_ANY_SOURCE,tag2,comm,&flag,&r_status);CHKERRQ(ierr);
398     if (flag) {
399       ierr = MPI_Get_count(&r_status,MPIU_INT,&len);CHKERRQ(ierr);
400 
401       proc_id = r_status.MPI_SOURCE;
402 
403       ierr = MPI_Irecv(data2,len,MPIU_INT,proc_id,r_status.MPI_TAG,comm,&r_req);CHKERRQ(ierr);
404       ierr = MPI_Wait(&r_req,&r_status);CHKERRQ(ierr);
405       if (len > 1+is_max) { /* Add data2 into data */
406         data2_i = data2 + 1 + is_max;
407         for (i=0; i<is_max; i++) {
408           table_i = table[i];
409           data_i  = data + 1 + is_max + Mbs*i;
410           isz     = data[1+i];
411           for (j=0; j<data2[1+i]; j++) {
412             col = data2_i[j];
413             if (!PetscBTLookupSet(table_i,col)) data_i[isz++] = col;
414           }
415           data[1+i] = isz;
416           if (i < is_max - 1) data2_i += data2[1+i];
417         }
418       }
419       k++;
420     }
421   }
422   ierr = PetscFree(data2);CHKERRQ(ierr);
423   ierr = PetscFree2(table,t_p);CHKERRQ(ierr);
424 
425   /* phase 1 sends are complete */
426   ierr = PetscMalloc1(size,&s_status);CHKERRQ(ierr);
427   if (nrqs) {ierr = MPI_Waitall(nrqs,s_waits1,s_status);CHKERRQ(ierr);}
428   ierr = PetscFree(data1);CHKERRQ(ierr);
429 
430   /* phase 2 sends are complete */
431   if (nrqr) {ierr = MPI_Waitall(nrqr,s_waits2,s_status);CHKERRQ(ierr);}
432   ierr = PetscFree2(s_waits1,s_waits2);CHKERRQ(ierr);
433   ierr = PetscFree(s_status);CHKERRQ(ierr);
434 
435   /* 5. Create new is[] */
436   /*--------------------*/
437   for (i=0; i<is_max; i++) {
438     data_i = data + 1 + is_max + Mbs*i;
439     ierr   = ISCreateGeneral(PETSC_COMM_SELF,data[1+i],data_i,PETSC_COPY_VALUES,is+i);CHKERRQ(ierr);
440   }
441   for (k=0; k<=nodata2; k++) {
442     ierr = PetscFree(odata2_ptr[k]);CHKERRQ(ierr);
443   }
444   ierr = PetscFree(odata2_ptr);CHKERRQ(ierr);
445   PetscFunctionReturn(0);
446 }
447 
448 /*
449    MatIncreaseOverlap_MPISBAIJ_Local - Called by MatIncreaseOverlap, to do
450        the work on the local processor.
451 
452      Inputs:
453       C      - MAT_MPISBAIJ;
454       data   - holds is[]. See MatIncreaseOverlap_MPISBAIJ_Once() for the format.
455       whose  - whose is[] to be processed,
456                MINE:  this processor's is[]
457                OTHER: other processor's is[]
458      Output:
459        nidx  - whose = MINE:
460                      holds input and newly found indices in the same format as data
461                whose = OTHER:
462                      only holds the newly found indices
463        table - table[i]: mark the indices of is[i], i=0,...,is_max. Used only in the case 'whose=MINE'.
464 */
465 /* Would computation be reduced by swapping the loop 'for each is' and 'for each row'? */
466 static PetscErrorCode MatIncreaseOverlap_MPISBAIJ_Local(Mat C,PetscInt *data,PetscInt whose,PetscInt *nidx,PetscBT *table)
467 {
468   Mat_MPISBAIJ   *c = (Mat_MPISBAIJ*)C->data;
469   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)(c->A)->data;
470   Mat_SeqBAIJ    *b = (Mat_SeqBAIJ*)(c->B)->data;
471   PetscErrorCode ierr;
472   PetscInt       row,mbs,Mbs,*nidx_i,col,col_max,isz,isz0,*ai,*aj,*bi,*bj,*garray,rstart,l;
473   PetscInt       a_start,a_end,b_start,b_end,i,j,k,is_max,*idx_i,n;
474   PetscBT        table0;  /* mark the indices of input is[] for look up */
475   PetscBT        table_i; /* poits to i-th table. When whose=OTHER, a single table is used for all is[] */
476 
477   PetscFunctionBegin;
478   Mbs    = c->Mbs; mbs = a->mbs;
479   ai     = a->i; aj = a->j;
480   bi     = b->i; bj = b->j;
481   garray = c->garray;
482   rstart = c->rstartbs;
483   is_max = data[0];
484 
485   ierr = PetscBTCreate(Mbs,&table0);CHKERRQ(ierr);
486 
487   nidx[0] = is_max;
488   idx_i   = data + is_max + 1; /* ptr to input is[0] array */
489   nidx_i  = nidx + is_max + 1; /* ptr to output is[0] array */
490   for (i=0; i<is_max; i++) { /* for each is */
491     isz = 0;
492     n   = data[1+i]; /* size of input is[i] */
493 
494     /* initialize and set table_i(mark idx and nidx) and table0(only mark idx) */
495     if (whose == MINE) { /* process this processor's is[] */
496       table_i = table[i];
497       nidx_i  = nidx + 1+ is_max + Mbs*i;
498     } else {            /* process other processor's is[] - only use one temp table */
499       table_i = table[0];
500     }
501     ierr = PetscBTMemzero(Mbs,table_i);CHKERRQ(ierr);
502     ierr = PetscBTMemzero(Mbs,table0);CHKERRQ(ierr);
503     if (n==0) {
504       nidx[1+i] = 0;  /* size of new is[i] */
505       continue;
506     }
507 
508     isz0 = 0; col_max = 0;
509     for (j=0; j<n; j++) {
510       col = idx_i[j];
511       if (col >= Mbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"index col %D >= Mbs %D",col,Mbs);
512       if (!PetscBTLookupSet(table_i,col)) {
513         ierr = PetscBTSet(table0,col);CHKERRQ(ierr);
514         if (whose == MINE) nidx_i[isz0] = col;
515         if (col_max < col) col_max = col;
516         isz0++;
517       }
518     }
519 
520     if (whose == MINE) isz = isz0;
521     k = 0;  /* no. of indices from input is[i] that have been examined */
522     for (row=0; row<mbs; row++) {
523       a_start = ai[row]; a_end = ai[row+1];
524       b_start = bi[row]; b_end = bi[row+1];
525       if (PetscBTLookup(table0,row+rstart)) { /* row is on input is[i]:
526                                                 do row search: collect all col in this row */
527         for (l = a_start; l<a_end ; l++) { /* Amat */
528           col = aj[l] + rstart;
529           if (!PetscBTLookupSet(table_i,col)) nidx_i[isz++] = col;
530         }
531         for (l = b_start; l<b_end ; l++) { /* Bmat */
532           col = garray[bj[l]];
533           if (!PetscBTLookupSet(table_i,col)) nidx_i[isz++] = col;
534         }
535         k++;
536         if (k >= isz0) break; /* for (row=0; row<mbs; row++) */
537       } else { /* row is not on input is[i]:
538                   do col serach: add row onto nidx_i if there is a col in nidx_i */
539         for (l = a_start; l<a_end; l++) {  /* Amat */
540           col = aj[l] + rstart;
541           if (col > col_max) break;
542           if (PetscBTLookup(table0,col)) {
543             if (!PetscBTLookupSet(table_i,row+rstart)) nidx_i[isz++] = row+rstart;
544             break; /* for l = start; l<end ; l++) */
545           }
546         }
547         for (l = b_start; l<b_end; l++) {  /* Bmat */
548           col = garray[bj[l]];
549           if (col > col_max) break;
550           if (PetscBTLookup(table0,col)) {
551             if (!PetscBTLookupSet(table_i,row+rstart)) nidx_i[isz++] = row+rstart;
552             break; /* for l = start; l<end ; l++) */
553           }
554         }
555       }
556     }
557 
558     if (i < is_max - 1) {
559       idx_i  += n;   /* ptr to input is[i+1] array */
560       nidx_i += isz; /* ptr to output is[i+1] array */
561     }
562     nidx[1+i] = isz; /* size of new is[i] */
563   } /* for each is */
564   ierr = PetscBTDestroy(&table0);CHKERRQ(ierr);
565   PetscFunctionReturn(0);
566 }
567 
568 
569