xref: /petsc/src/mat/impls/sbaij/mpi/sbaijov.c (revision fc70d14ed97db9ec06644699396778d7af6324d4)
1 /*$Id: sbaijov.c,v 1.65 2001/08/06 21:15:42 bsmith Exp $*/
2 
3 /*
4    Routines to compute overlapping regions of a parallel MPI matrix.
5    Used for finding submatrices that were shared across processors.
6 */
7 #include "src/mat/impls/sbaij/mpi/mpisbaij.h"
8 #include "petscbt.h"
9 
10 static int MatIncreaseOverlap_MPISBAIJ_Once(Mat,int,IS *);
11 static int MatIncreaseOverlap_MPISBAIJ_Local(Mat,int *,int **,PetscBT*);
12 static int MatIncreaseOverlap_MPISBAIJ_Receive(Mat,int,int **,int**,int*);
13 
14 /* this function is sasme as MatCompressIndicesGeneral_MPIBAIJ -- should be removed! */
15 #undef __FUNCT__
16 #define __FUNCT__ "MatCompressIndicesGeneral_MPISBAIJ"
17 static int MatCompressIndicesGeneral_MPISBAIJ(Mat C,int imax,const IS is_in[],IS is_out[])
18 {
19   Mat_MPISBAIJ        *baij = (Mat_MPISBAIJ*)C->data;
20   int                ierr,isz,bs = baij->bs,n,i,j,*idx,ival;
21 #if defined (PETSC_USE_CTABLE)
22   PetscTable         gid1_lid1;
23   int                tt, gid1, *nidx;
24   PetscTablePosition tpos;
25 #else
26   int                Nbs,*nidx;
27   PetscBT            table;
28 #endif
29 
30   PetscFunctionBegin;
31   /* printf(" ...MatCompressIndicesGeneral_MPISBAIJ is called ...\n"); */
32 #if defined (PETSC_USE_CTABLE)
33   ierr = PetscTableCreate(baij->mbs,&gid1_lid1);CHKERRQ(ierr);
34 #else
35   Nbs  = baij->Nbs;
36   ierr = PetscMalloc((Nbs+1)*sizeof(int),&nidx);CHKERRQ(ierr);
37   ierr = PetscBTCreate(Nbs,table);CHKERRQ(ierr);
38 #endif
39   for (i=0; i<imax; i++) {
40     isz  = 0;
41 #if defined (PETSC_USE_CTABLE)
42     ierr = PetscTableRemoveAll(gid1_lid1);CHKERRQ(ierr);
43 #else
44     ierr = PetscBTMemzero(Nbs,table);CHKERRQ(ierr);
45 #endif
46     ierr = ISGetIndices(is_in[i],&idx);CHKERRQ(ierr);
47     ierr = ISGetLocalSize(is_in[i],&n);CHKERRQ(ierr);
48     for (j=0; j<n ; j++) {
49       ival = idx[j]/bs; /* convert the indices into block indices */
50 #if defined (PETSC_USE_CTABLE)
51       ierr = PetscTableFind(gid1_lid1,ival+1,&tt);CHKERRQ(ierr);
52       if (!tt) {
53 	ierr = PetscTableAdd(gid1_lid1,ival+1,isz+1);CHKERRQ(ierr);
54         isz++;
55       }
56 #else
57       if (ival>Nbs) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"index greater than mat-dim");
58       if(!PetscBTLookupSet(table,ival)) { nidx[isz++] = ival;}
59 #endif
60     }
61     ierr = ISRestoreIndices(is_in[i],&idx);CHKERRQ(ierr);
62 #if defined (PETSC_USE_CTABLE)
63     ierr = PetscMalloc((isz+1)*sizeof(int),&nidx);CHKERRQ(ierr);
64     ierr = PetscTableGetHeadPosition(gid1_lid1,&tpos);CHKERRQ(ierr);
65     j = 0;
66     while (tpos) {
67       ierr = PetscTableGetNext(gid1_lid1,&tpos,&gid1,&tt);CHKERRQ(ierr);
68       if (tt-- > isz) { SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"index greater than array-dim"); }
69       nidx[tt] = gid1 - 1;
70       j++;
71     }
72     if (j != isz) { SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"table error: jj != isz"); }
73     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is_out+i));CHKERRQ(ierr);
74     ierr = PetscFree(nidx);CHKERRQ(ierr);
75 #else
76     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is_out+i));CHKERRQ(ierr);
77 #endif
78   }
79 #if defined (PETSC_USE_CTABLE)
80   ierr = PetscTableDelete(gid1_lid1);CHKERRQ(ierr);
81 #else
82   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
83   ierr = PetscFree(nidx);CHKERRQ(ierr);
84 #endif
85   PetscFunctionReturn(0);
86 }
87 
88 #undef __FUNCT__
89 #define __FUNCT__ "MatCompressIndicesSorted_MPISBAIJ"
90 static int MatCompressIndicesSorted_MPISBAIJ(Mat C,int imax,const IS is_in[],IS is_out[])
91 {
92   Mat_MPISBAIJ  *baij = (Mat_MPISBAIJ*)C->data;
93   int          ierr,bs=baij->bs,i,j,k,val,n,*idx,*nidx,*idx_local;
94   PetscTruth   flg;
95 #if defined (PETSC_USE_CTABLE)
96   int maxsz;
97 #else
98   int Nbs=baij->Nbs;
99 #endif
100   PetscFunctionBegin;
101   printf(" ... MatCompressIndicesSorted_MPISBAIJ is called ...\n");
102   for (i=0; i<imax; i++) {
103     ierr = ISSorted(is_in[i],&flg);CHKERRQ(ierr);
104     if (!flg) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Indices are not sorted");
105   }
106 #if defined (PETSC_USE_CTABLE)
107   /* Now check max size */
108   for (i=0,maxsz=0; i<imax; i++) {
109     ierr = ISGetIndices(is_in[i],&idx);CHKERRQ(ierr);
110     ierr = ISGetLocalSize(is_in[i],&n);CHKERRQ(ierr);
111     if (n%bs !=0) SETERRQ(1,"Indices are not block ordered");
112     n = n/bs; /* The reduced index size */
113     if (n > maxsz) maxsz = n;
114   }
115   ierr = PetscMalloc((maxsz+1)*sizeof(int),&nidx);CHKERRQ(ierr);
116 #else
117   ierr = PetscMalloc((Nbs+1)*sizeof(int),&nidx);CHKERRQ(ierr);
118 #endif
119   /* Now check if the indices are in block order */
120   for (i=0; i<imax; i++) {
121     ierr = ISGetIndices(is_in[i],&idx);CHKERRQ(ierr);
122     ierr = ISGetLocalSize(is_in[i],&n);CHKERRQ(ierr);
123     if (n%bs !=0) SETERRQ(1,"Indices are not block ordered");
124 
125     n = n/bs; /* The reduced index size */
126     idx_local = idx;
127     for (j=0; j<n ; j++) {
128       val = idx_local[0];
129       if (val%bs != 0) SETERRQ(1,"Indices are not block ordered");
130       for (k=0; k<bs; k++) {
131         if (val+k != idx_local[k]) SETERRQ(1,"Indices are not block ordered");
132       }
133       nidx[j] = val/bs;
134       idx_local +=bs;
135     }
136     ierr = ISRestoreIndices(is_in[i],&idx);CHKERRQ(ierr);
137     ierr = ISCreateGeneral(PETSC_COMM_SELF,n,nidx,(is_out+i));CHKERRQ(ierr);
138   }
139   ierr = PetscFree(nidx);CHKERRQ(ierr);
140 
141   PetscFunctionReturn(0);
142 }
143 
144 #undef __FUNCT__
145 #define __FUNCT__ "MatExpandIndices_MPISBAIJ"
146 static int MatExpandIndices_MPISBAIJ(Mat C,int imax,const IS is_in[],IS is_out[])
147 {
148   Mat_MPISBAIJ  *baij = (Mat_MPISBAIJ*)C->data;
149   int          ierr,bs = baij->bs,n,i,j,k,*idx,*nidx;
150 #if defined (PETSC_USE_CTABLE)
151   int          maxsz;
152 #else
153   int          Nbs = baij->Nbs;
154 #endif
155 
156   PetscFunctionBegin;
157   /* printf(" ... MatExpandIndices_MPISBAIJ is called ...\n"); */
158 #if defined (PETSC_USE_CTABLE)
159   /* Now check max size */
160   for (i=0,maxsz=0; i<imax; i++) {
161     ierr = ISGetIndices(is_in[i],&idx);CHKERRQ(ierr);
162     ierr = ISGetLocalSize(is_in[i],&n);CHKERRQ(ierr);
163     if (n*bs > maxsz) maxsz = n*bs;
164   }
165   ierr = PetscMalloc((maxsz+1)*sizeof(int),&nidx);CHKERRQ(ierr);
166 #else
167   ierr = PetscMalloc((Nbs*bs+1)*sizeof(int),&nidx);CHKERRQ(ierr);
168 #endif
169 
170   for (i=0; i<imax; i++) {
171     ierr = ISGetIndices(is_in[i],&idx);CHKERRQ(ierr);
172     ierr = ISGetLocalSize(is_in[i],&n);CHKERRQ(ierr);
173     for (j=0; j<n ; ++j){
174       for (k=0; k<bs; k++)
175         nidx[j*bs+k] = idx[j]*bs+k;
176     }
177     ierr = ISRestoreIndices(is_in[i],&idx);CHKERRQ(ierr);
178     ierr = ISCreateGeneral(PETSC_COMM_SELF,n*bs,nidx,is_out+i);CHKERRQ(ierr);
179   }
180   ierr = PetscFree(nidx);CHKERRQ(ierr);
181   PetscFunctionReturn(0);
182 }
183 
184 
185 #undef __FUNCT__
186 #define __FUNCT__ "MatIncreaseOverlap_MPISBAIJ"
187 int MatIncreaseOverlap_MPISBAIJ(Mat C,int is_max,IS is[],int ov)
188 {
189   int i,ierr;
190   IS  *is_new;
191 
192   PetscFunctionBegin;
193   ierr = PetscMalloc(is_max*sizeof(IS),&is_new);CHKERRQ(ierr);
194   /* Convert the indices into block format */
195   ierr = MatCompressIndicesGeneral_MPISBAIJ(C,is_max,is,is_new);CHKERRQ(ierr);
196   if (ov < 0){ SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Negative overlap specified\n");}
197   for (i=0; i<ov; ++i) {
198     ierr = MatIncreaseOverlap_MPISBAIJ_Once(C,is_max,is_new);CHKERRQ(ierr);
199   }
200   for (i=0; i<is_max; i++) {ierr = ISDestroy(is[i]);CHKERRQ(ierr);}
201   ierr = MatExpandIndices_MPISBAIJ(C,is_max,is_new,is);CHKERRQ(ierr);
202   for (i=0; i<is_max; i++) {ierr = ISDestroy(is_new[i]);CHKERRQ(ierr);}
203   ierr = PetscFree(is_new);CHKERRQ(ierr);
204   PetscFunctionReturn(0);
205 }
206 
207 #undef __FUNCT__
208 #define __FUNCT__ "MatIncreaseOverlap_MPISBAIJ_Once"
209 static int MatIncreaseOverlap_MPISBAIJ_Once(Mat C,int is_max,IS is[])
210 {
211   Mat_MPISBAIJ  *c = (Mat_MPISBAIJ*)C->data;
212   int         **idx,*n,len,*idx_i,*nidx,*nidx_i,isz,col;
213   int         size,rank,Mbs,i,j,k,ierr,nrqs,msz,*outdat,*indat;
214   int         tag1,tag2,flag,proc_id;
215   MPI_Comm    comm;
216   MPI_Request *s_waits1,*s_waits2,r_req;
217   MPI_Status  *s_status,r_status;
218   PetscBT     *table;
219   PetscBT     table_i;
220   PetscBT     *table_tmp;
221 
222   PetscFunctionBegin;
223 
224   comm = C->comm;
225   size = c->size;
226   rank = c->rank;
227   Mbs  = c->Mbs;
228 
229   /* int prid=0; */
230 
231   len  = is_max*sizeof(PetscBT) + (Mbs/PETSC_BITS_PER_BYTE+1)*is_max*sizeof(char) + 1;
232   ierr = PetscMalloc(len,&table);CHKERRQ(ierr);
233   char *t_p;
234   t_p  = (char *)(table + is_max);
235   for (i=0; i<is_max; i++) {
236       table[i] = t_p + (Mbs/PETSC_BITS_PER_BYTE+1)*i;
237   }
238 
239   ierr = PetscMalloc(len,&table_tmp);CHKERRQ(ierr);
240   t_p  = (char *)(table_tmp + is_max);
241   for (i=0; i<is_max; i++) {
242       table_tmp[i] = t_p + (Mbs/PETSC_BITS_PER_BYTE+1)*i;
243   }
244 
245   /* 1. Send is[] to all other processors */
246   /*--------------------------------------*/
247   /* This processor sends its is[] to all other processors in the format:
248        outdat[0]          = is_max, no of is in this processor
249        outdat[1]          = n[0], size of is[0]
250         ...
251        outdat[is_max]     = n[is_max-1], size of is[is_max-1]
252        outdat[is_max + 1] = data(is[0])
253         ...
254        outdat[is_max+1+sum(n[k]), k=0,...,i-1] = data(is[i])
255         ...
256   */
257   ierr = PetscObjectGetNewTag((PetscObject)C,&tag1);CHKERRQ(ierr);
258   ierr = PetscObjectGetNewTag((PetscObject)C,&tag2);CHKERRQ(ierr);
259 
260   len  = (is_max+1)*sizeof(int*)+ (is_max)*sizeof(int);
261   ierr = PetscMalloc(len,&idx);CHKERRQ(ierr);
262   n    = (int*)(idx + is_max);
263 
264   /* Allocate Memory for outgoing messages */
265   len = 1 + is_max;
266   for (i=0; i<is_max; i++) {
267     ierr = ISGetIndices(is[i],&idx[i]);CHKERRQ(ierr);
268     ierr = ISGetLocalSize(is[i],&n[i]);CHKERRQ(ierr);
269     len += n[i];
270   }
271   ierr = PetscMalloc(len*sizeof(int),&outdat);CHKERRQ(ierr);
272 
273   /* Form the outgoing messages */
274   outdat[0] = is_max;
275   for (i=0; i<is_max; i++) {
276     outdat[i+1] = n[i];
277   }
278   k = is_max + 1;
279   for (i=0; i<is_max; i++) { /* for is[i] */
280     idx_i = idx[i];
281     for (j=0; j<n[i]; j++){
282       outdat[k] = *(idx_i);
283       k++; idx_i++;
284     }
285   }
286   if (k != len) SETERRQ2(1,"Error on forming the outgoing messages: k %d != len %d",k,len);
287 
288   /*  Now  post the sends */
289   ierr = PetscMalloc(size*sizeof(MPI_Request),&s_waits1);CHKERRQ(ierr);
290 
291   k = 0;
292   for (proc_id=0; proc_id<size; ++proc_id) { /* send outdat to processor [proc_id] */
293     if (proc_id != rank){
294       ierr = MPI_Isend(outdat,len,MPI_INT,proc_id,tag1,comm,s_waits1+k);CHKERRQ(ierr);
295       /* printf(" [%d] send %d msg to [%d] \n",rank,len,proc_id); */
296       k++;
297     }
298   }
299 
300   /* 2. Do local work on this processor's is[] */
301   /*-------------------------------------------*/
302   ierr = MatIncreaseOverlap_MPISBAIJ_Local(C,outdat,&nidx,table);CHKERRQ(ierr);
303 
304   for (i=0; i<is_max; i++){
305     ierr = ISRestoreIndices(is[i],idx+i);CHKERRQ(ierr);
306     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
307   }
308 
309   /* 3. Receive other's is[] and process. Then send back */
310   /*----------------------------------------------------*/
311   /* Send is done */
312   nrqs = size-1;
313   ierr = PetscMalloc(size*sizeof(MPI_Status),&s_status);CHKERRQ(ierr);
314   ierr = MPI_Waitall(nrqs,s_waits1,s_status);CHKERRQ(ierr);
315 
316   /* save n[i] */
317   for (i=0; i<is_max; i++){
318     n[i] = outdat[1+i];
319   }
320   ierr = PetscFree(outdat);CHKERRQ(ierr);
321   ierr = PetscMalloc(size*sizeof(MPI_Request),&s_waits2);CHKERRQ(ierr);
322   int **outdat_ptr;
323   ierr = PetscMalloc(size*sizeof(int**),&outdat_ptr);
324   k = 0;
325   do {
326     /* Receive messages */
327     ierr = MPI_Iprobe(MPI_ANY_SOURCE,tag1,comm,&flag,&r_status);
328     if (flag){
329       ierr = MPI_Get_count(&r_status,MPI_INT,&len);
330       proc_id = r_status.MPI_SOURCE;
331       ierr = PetscMalloc(len*sizeof(int),&indat);CHKERRQ(ierr);
332       ierr = MPI_Irecv(indat,len,MPI_INT,proc_id,r_status.MPI_TAG,comm,&r_req);
333       /* printf(" [%d] recv %d msg from [%d]\n",rank,len,proc_id); */
334 
335       /*  Process messages */
336       ierr = MatIncreaseOverlap_MPISBAIJ_Local(C,indat,&outdat_ptr[k],table_tmp);CHKERRQ(ierr);
337       outdat = outdat_ptr[k];
338       len = 1 + outdat[0];
339       for (i=0; i<outdat[0]; i++){
340         len += outdat[1 + i];
341       }
342 
343       /* Send messages back */
344       /* printf(" [%d] send %d msg back to [%d] \n",rank,len,proc_id); */
345       ierr = MPI_Isend(outdat,len,MPI_INT,proc_id,tag2,comm,&s_waits2[k]);CHKERRQ(ierr);
346 
347       ierr = PetscFree(indat);CHKERRQ(ierr);
348       k++;
349     }
350   } while (k < nrqs);
351 
352   /* 4. Receive work done on other processors, then merge */
353   /*--------------------------------------------------------*/
354   ierr = MPI_Waitall(nrqs,s_waits2,s_status);CHKERRQ(ierr);
355   for (k=0; k<nrqs; k++){
356     ierr = PetscFree(outdat_ptr[k]);CHKERRQ(ierr);
357   }
358   ierr = PetscFree(outdat_ptr);CHKERRQ(ierr);
359 
360   /* allocate memory for merged data */
361   int *mydata,*mydata_i;
362   ierr = PetscMalloc((1+is_max*(Mbs+1))*sizeof(int),&mydata);CHKERRQ(ierr);
363 
364   /* copy nidx into mydata */
365   k = is_max + 1;
366   for (i=0; i<is_max; i++){
367     mydata[1+i] = nidx[1+i]; /* size of is[i] before merge */
368     mydata_i = mydata + 1 + is_max + Mbs*i;
369     for (j=0; j<nidx[1+i]; j++){
370       mydata_i[j] = nidx[k++];
371     }
372   }
373 
374   k = 0;
375   do {
376     /* Receive messages */
377     ierr = MPI_Iprobe(MPI_ANY_SOURCE,tag2,comm,&flag,&r_status);
378     if (flag){
379       ierr = MPI_Get_count(&r_status,MPI_INT,&len);
380       proc_id = r_status.MPI_SOURCE;
381       ierr = ierr = PetscMalloc(len*sizeof(int),&indat);CHKERRQ(ierr);
382       ierr = MPI_Irecv(indat,len,MPI_INT,proc_id,r_status.MPI_TAG,comm,&r_req);
383       /* printf(" [%d] recv %d msg from [%d]\n",rank,len,proc_id); */
384 
385       /*  merge indat into mydata */
386       nidx_i   = indat + 1 + is_max;
387       for (i=0; i<is_max; i++){
388         table_i  = table[i];
389         mydata_i = mydata + 1 + is_max + Mbs*i;
390         isz = mydata[1+i]; /* size of is[i] from nidx */
391 
392         for (j=0; j<indat[1+i]; j++){
393           col = nidx_i[j];
394           if (!PetscBTLookupSet(table_i,col)) {mydata_i[isz++] = col;}
395         }
396         mydata[1+i] = isz;
397         if (i < is_max - 1){
398           nidx_i += indat[1+i]; /* ptr to is[i+1] array from indat */
399         }
400       } /* for (i=0; i<is_max; i++) */
401 
402       k++;
403       ierr = PetscFree(indat);CHKERRQ(ierr);
404     }
405   } while (k < nrqs);
406 
407   /* 5. Create new is[] */
408   /*--------------------*/
409   for (i=0; i<is_max; i++) {
410     mydata_i = mydata + 1 + is_max + Mbs*i;
411     ierr = ISCreateGeneral(PETSC_COMM_SELF,mydata[1+i],mydata_i,is+i);CHKERRQ(ierr);
412   }
413 
414   ierr = PetscFree(mydata);CHKERRQ(ierr);
415   ierr = PetscFree(nidx);CHKERRQ(ierr);
416   ierr = PetscFree(idx);CHKERRQ(ierr);
417   ierr = PetscFree(s_waits1);CHKERRQ(ierr);
418   ierr = PetscFree(s_waits2);CHKERRQ(ierr);
419   ierr = PetscFree(s_status);CHKERRQ(ierr);
420   ierr = PetscFree(table);CHKERRQ(ierr);
421   ierr = PetscFree(table_tmp);CHKERRQ(ierr);
422   PetscFunctionReturn(0);
423 }
424 
425 #undef __FUNCT__
426 #define __FUNCT__ "MatIncreaseOverlap_MPISBAIJ_Local"
427 /*
428    MatIncreaseOverlap_MPISBAIJ_Local - Called by MatIncreaseOverlap, to do
429        the work on the local processor.
430 
431      Inputs:
432       C      - MAT_MPISBAIJ;
433       data   - holds is[] in the format:
434         data[0]          = is_max, no of is
435         data[1]          = size of is[0]
436         ...
437         data[is_max]     = size of is[is_max-1]
438         data[is_max + 1] = is[0] array
439         ...
440         data[is_max+1+sum(n[k]), k=0,...,i-1] = is[i] array
441         ...
442 
443      Output:
444        data_new   - holds new is[] in the same format as data
445        table      - table[i]: mark the indices of is[i], i=0,...,is_max.
446 */
447 static int MatIncreaseOverlap_MPISBAIJ_Local(Mat C,int *data,int **data_new,PetscBT *table)
448 {
449   Mat_MPISBAIJ *c = (Mat_MPISBAIJ*)C->data;
450   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)(c->A)->data;
451   Mat_SeqBAIJ  *b = (Mat_SeqBAIJ*)(c->B)->data;
452   int          ierr,row,mbs,Mbs,*nidx,*nidx_i,col,isz,isz0,*ai,*aj,bs,*bi,*bj,*garray,rstart,l;
453   int          a_start,a_end,b_start,b_end,i,j,k,is_max,*idx_i,n;
454   PetscBT      table0;
455   PetscBT      table_i; /* poits to i-th table */
456 
457   PetscFunctionBegin;
458   Mbs = c->Mbs; mbs = a->mbs; bs = a->bs;
459   ai = a->i; aj = a->j;
460   bi = b->i; bj = b->j;
461   garray = c->garray;
462   rstart = c->rstart;
463   is_max = data[0];
464 
465   /* int rank=c->rank,prid=0; */ /* for debugging */
466 
467   ierr = PetscBTCreate(Mbs,table0);CHKERRQ(ierr);
468 
469   ierr = PetscMalloc((1+is_max*(Mbs+1))*sizeof(int),&nidx);CHKERRQ(ierr);
470   nidx[0] = is_max;
471 
472   idx_i  = data + is_max + 1; /* ptr to input is[0] array */
473   nidx_i = nidx + is_max + 1; /* ptr to active is[0] array */
474   for (i=0; i<is_max; i++) { /* for each is */
475     isz  = 0;
476     table_i = table[i];
477     ierr = PetscBTMemzero(Mbs,table_i);CHKERRQ(ierr);
478     n = data[1+i]; /* size of input is[i] */
479 
480     if (n > 0) {
481 
482       /* Enter input is[i] into active is[i] */
483       for (j=0; j<n; j++){
484         col = idx_i[j];
485         if (col >= Mbs) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"index col %d >= Mbs %d",col,Mbs);
486         if(!PetscBTLookupSet(table_i,col)) { nidx_i[isz++] = col;}
487       }
488 
489       /* set table0 for lookup */
490       ierr = PetscBTMemzero(Mbs,table0);CHKERRQ(ierr);
491       for (l=0; l<isz; l++) PetscBTSet(table0,nidx_i[l]);
492 
493       isz0 = isz; /* size of input is[i] after removing repeated indices */
494       k = 0;  /* no. of indices from input is[i] that have been examined */
495       for (row=0; row<mbs; row++){
496         a_start = ai[row]; a_end = ai[row+1];
497         b_start = bi[row]; b_end = bi[row+1];
498         if (PetscBTLookup(table0,row+rstart)){ /* row is on nidx_i - row search: collect all col in this row */
499           for (l = a_start; l<a_end ; l++){ /* Amat */
500             col = aj[l] + rstart;
501             if (!PetscBTLookupSet(table_i,col)) {nidx_i[isz++] = col;}
502           }
503           for (l = b_start; l<b_end ; l++){ /* Bmat */
504             col = garray[bj[l]];
505             if (!PetscBTLookupSet(table_i,col)) {nidx_i[isz++] = col;}
506           }
507           k++;
508           if (k >= isz0) break; /* for (row=0; row<mbs; row++) */
509         } else { /* row is not on nidx_i - col serach: add row onto nidx_i if there is a col in nidx_i */
510           for (l = a_start; l<a_end ; l++){ /* Amat */
511             col = aj[l] + rstart;
512             if (PetscBTLookup(table0,col)){
513               if (!PetscBTLookupSet(table_i,row+rstart)) {nidx_i[isz++] = row+rstart;}
514               break; /* for l = start; l<end ; l++) */
515             }
516           }
517           for (l = b_start; l<b_end ; l++){ /* Bmat */
518             col = garray[bj[l]];
519             if (PetscBTLookup(table0,col)){
520               if (!PetscBTLookupSet(table_i,row+rstart)) {nidx_i[isz++] = row+rstart;}
521               break; /* for l = start; l<end ; l++) */
522             }
523           }
524         }
525       } /* for (row=0; row<mbs; row++) */
526     } /* if (n > 0) */
527 
528     if (i < is_max - 1){
529       idx_i  += n;   /* ptr to input is[i+1] array */
530       nidx_i += isz; /* ptr to active is[i+1] array */
531     }
532     nidx[1+i] = isz; /* size of new is[i] */
533   } /* /* for each is */
534   *data_new = nidx;
535   ierr = PetscBTDestroy(table0);CHKERRQ(ierr);
536 
537   PetscFunctionReturn(0);
538 }
539 
540 
541