xref: /petsc/src/mat/impls/baij/mpi/baijov.c (revision a42ab0d6646d38f20ae0e8ebf287f7b074405f5f)
1 
2 /*
3    Routines to compute overlapping regions of a parallel MPI matrix
4   and to find submatrices that were shared across processors.
5 */
6 #include <../src/mat/impls/baij/mpi/mpibaij.h>
7 #include <petscbt.h>
8 
9 static PetscErrorCode MatIncreaseOverlap_MPIBAIJ_Local(Mat,PetscInt,char**,PetscInt*,PetscInt**);
10 static PetscErrorCode MatIncreaseOverlap_MPIBAIJ_Receive(Mat,PetscInt,PetscInt**,PetscInt**,PetscInt*);
11 extern PetscErrorCode MatGetRow_MPIBAIJ(Mat,PetscInt,PetscInt*,PetscInt**,PetscScalar**);
12 extern PetscErrorCode MatRestoreRow_MPIBAIJ(Mat,PetscInt,PetscInt*,PetscInt**,PetscScalar**);
13 
14 PetscErrorCode MatIncreaseOverlap_MPIBAIJ(Mat C,PetscInt imax,IS is[],PetscInt ov)
15 {
16   PetscErrorCode ierr;
17   PetscInt       i,N=C->cmap->N, bs=C->rmap->bs;
18   IS             *is_new;
19 
20   PetscFunctionBegin;
21   ierr = PetscMalloc1(imax,&is_new);CHKERRQ(ierr);
22   /* Convert the indices into block format */
23   ierr = ISCompressIndicesGeneral(N,C->rmap->n,bs,imax,is,is_new);CHKERRQ(ierr);
24   if (ov < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative overlap specified\n");
25   for (i=0; i<ov; ++i) {
26     ierr = MatIncreaseOverlap_MPIBAIJ_Once(C,imax,is_new);CHKERRQ(ierr);
27   }
28   for (i=0; i<imax; i++) {ierr = ISDestroy(&is[i]);CHKERRQ(ierr);}
29   ierr = ISExpandIndicesGeneral(N,N,bs,imax,is_new,is);CHKERRQ(ierr);
30   for (i=0; i<imax; i++) {ierr = ISDestroy(&is_new[i]);CHKERRQ(ierr);}
31   ierr = PetscFree(is_new);CHKERRQ(ierr);
32   PetscFunctionReturn(0);
33 }
34 
35 /*
36   Sample message format:
37   If a processor A wants processor B to process some elements corresponding
38   to index sets is[1], is[5]
39   mesg [0] = 2   (no of index sets in the mesg)
40   -----------
41   mesg [1] = 1 => is[1]
42   mesg [2] = sizeof(is[1]);
43   -----------
44   mesg [5] = 5  => is[5]
45   mesg [6] = sizeof(is[5]);
46   -----------
47   mesg [7]
48   mesg [n]  data(is[1])
49   -----------
50   mesg[n+1]
51   mesg[m]  data(is[5])
52   -----------
53 
54   Notes:
55   nrqs - no of requests sent (or to be sent out)
56   nrqr - no of requests recieved (which have to be or which have been processed
57 */
58 PetscErrorCode MatIncreaseOverlap_MPIBAIJ_Once(Mat C,PetscInt imax,IS is[])
59 {
60   Mat_MPIBAIJ    *c = (Mat_MPIBAIJ*)C->data;
61   const PetscInt **idx,*idx_i;
62   PetscInt       *n,*w3,*w4,**data,len;
63   PetscErrorCode ierr;
64   PetscMPIInt    size,rank,tag1,tag2,*w2,*w1,nrqr;
65   PetscInt       Mbs,i,j,k,**rbuf,row,proc=-1,nrqs,msz,**outdat,**ptr;
66   PetscInt       *ctr,*pa,*tmp,*isz,*isz1,**xdata,**rbuf2,*d_p;
67   PetscMPIInt    *onodes1,*olengths1,*onodes2,*olengths2;
68   PetscBT        *table;
69   MPI_Comm       comm;
70   MPI_Request    *s_waits1,*r_waits1,*s_waits2,*r_waits2;
71   MPI_Status     *s_status,*recv_status;
72   char           *t_p;
73 
74   PetscFunctionBegin;
75   ierr = PetscObjectGetComm((PetscObject)C,&comm);CHKERRQ(ierr);
76   size = c->size;
77   rank = c->rank;
78   Mbs  = c->Mbs;
79 
80   ierr = PetscObjectGetNewTag((PetscObject)C,&tag1);CHKERRQ(ierr);
81   ierr = PetscObjectGetNewTag((PetscObject)C,&tag2);CHKERRQ(ierr);
82 
83   ierr = PetscMalloc2(imax+1,&idx,imax,&n);CHKERRQ(ierr);
84 
85   for (i=0; i<imax; i++) {
86     ierr = ISGetIndices(is[i],&idx[i]);CHKERRQ(ierr);
87     ierr = ISGetLocalSize(is[i],&n[i]);CHKERRQ(ierr);
88   }
89 
90   /* evaluate communication - mesg to who,length of mesg, and buffer space
91      required. Based on this, buffers are allocated, and data copied into them*/
92   ierr = PetscCalloc4(size,&w1,size,&w2,size,&w3,size,&w4);CHKERRQ(ierr);
93   for (i=0; i<imax; i++) {
94     ierr  = PetscMemzero(w4,size*sizeof(PetscInt));CHKERRQ(ierr); /* initialise work vector*/
95     idx_i = idx[i];
96     len   = n[i];
97     for (j=0; j<len; j++) {
98       row = idx_i[j];
99       if (row < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Index set cannot have negative entries");
100       ierr = PetscLayoutFindOwner(C->rmap,row*C->rmap->bs,&proc);CHKERRQ(ierr);
101       w4[proc]++;
102     }
103     for (j=0; j<size; j++) {
104       if (w4[j]) { w1[j] += w4[j]; w3[j]++;}
105     }
106   }
107 
108   nrqs     = 0;              /* no of outgoing messages */
109   msz      = 0;              /* total mesg length (for all proc */
110   w1[rank] = 0;              /* no mesg sent to itself */
111   w3[rank] = 0;
112   for (i=0; i<size; i++) {
113     if (w1[i])  {w2[i] = 1; nrqs++;} /* there exists a message to proc i */
114   }
115   /* pa - is list of processors to communicate with */
116   ierr = PetscMalloc1(nrqs+1,&pa);CHKERRQ(ierr);
117   for (i=0,j=0; i<size; i++) {
118     if (w1[i]) {pa[j] = i; j++;}
119   }
120 
121   /* Each message would have a header = 1 + 2*(no of IS) + data */
122   for (i=0; i<nrqs; i++) {
123     j      = pa[i];
124     w1[j] += w2[j] + 2*w3[j];
125     msz   += w1[j];
126   }
127 
128   /* Determine the number of messages to expect, their lengths, from from-ids */
129   ierr = PetscGatherNumberOfMessages(comm,w2,w1,&nrqr);CHKERRQ(ierr);
130   ierr = PetscGatherMessageLengths(comm,nrqs,nrqr,w1,&onodes1,&olengths1);CHKERRQ(ierr);
131 
132   /* Now post the Irecvs corresponding to these messages */
133   ierr = PetscPostIrecvInt(comm,tag1,nrqr,onodes1,olengths1,&rbuf,&r_waits1);CHKERRQ(ierr);
134 
135   /* Allocate Memory for outgoing messages */
136   ierr = PetscMalloc4(size,&outdat,size,&ptr,msz,&tmp,size,&ctr);CHKERRQ(ierr);
137   ierr = PetscMemzero(outdat,size*sizeof(PetscInt*));CHKERRQ(ierr);
138   ierr = PetscMemzero(ptr,size*sizeof(PetscInt*));CHKERRQ(ierr);
139   {
140     PetscInt *iptr = tmp,ict  = 0;
141     for (i=0; i<nrqs; i++) {
142       j         = pa[i];
143       iptr     +=  ict;
144       outdat[j] = iptr;
145       ict       = w1[j];
146     }
147   }
148 
149   /* Form the outgoing messages */
150   /*plug in the headers*/
151   for (i=0; i<nrqs; i++) {
152     j            = pa[i];
153     outdat[j][0] = 0;
154     ierr         = PetscMemzero(outdat[j]+1,2*w3[j]*sizeof(PetscInt));CHKERRQ(ierr);
155     ptr[j]       = outdat[j] + 2*w3[j] + 1;
156   }
157 
158   /* Memory for doing local proc's work*/
159   {
160     ierr = PetscCalloc5(imax,&table, imax,&data, imax,&isz, Mbs*imax,&d_p, (Mbs/PETSC_BITS_PER_BYTE+1)*imax,&t_p);CHKERRQ(ierr);
161 
162     for (i=0; i<imax; i++) {
163       table[i] = t_p + (Mbs/PETSC_BITS_PER_BYTE+1)*i;
164       data[i]  = d_p + (Mbs)*i;
165     }
166   }
167 
168   /* Parse the IS and update local tables and the outgoing buf with the data*/
169   {
170     PetscInt n_i,*data_i,isz_i,*outdat_j,ctr_j;
171     PetscBT  table_i;
172 
173     for (i=0; i<imax; i++) {
174       ierr    = PetscMemzero(ctr,size*sizeof(PetscInt));CHKERRQ(ierr);
175       n_i     = n[i];
176       table_i = table[i];
177       idx_i   = idx[i];
178       data_i  = data[i];
179       isz_i   = isz[i];
180       for (j=0; j<n_i; j++) {   /* parse the indices of each IS */
181         row  = idx_i[j];
182         ierr = PetscLayoutFindOwner(C->rmap,row*C->rmap->bs,&proc);CHKERRQ(ierr);
183         if (proc != rank) { /* copy to the outgoing buffer */
184           ctr[proc]++;
185           *ptr[proc] = row;
186           ptr[proc]++;
187         } else { /* Update the local table */
188           if (!PetscBTLookupSet(table_i,row)) data_i[isz_i++] = row;
189         }
190       }
191       /* Update the headers for the current IS */
192       for (j=0; j<size; j++) { /* Can Optimise this loop by using pa[] */
193         if ((ctr_j = ctr[j])) {
194           outdat_j        = outdat[j];
195           k               = ++outdat_j[0];
196           outdat_j[2*k]   = ctr_j;
197           outdat_j[2*k-1] = i;
198         }
199       }
200       isz[i] = isz_i;
201     }
202   }
203 
204   /*  Now  post the sends */
205   ierr = PetscMalloc1(nrqs+1,&s_waits1);CHKERRQ(ierr);
206   for (i=0; i<nrqs; ++i) {
207     j    = pa[i];
208     ierr = MPI_Isend(outdat[j],w1[j],MPIU_INT,j,tag1,comm,s_waits1+i);CHKERRQ(ierr);
209   }
210 
211   /* No longer need the original indices*/
212   for (i=0; i<imax; ++i) {
213     ierr = ISRestoreIndices(is[i],idx+i);CHKERRQ(ierr);
214   }
215   ierr = PetscFree2(idx,n);CHKERRQ(ierr);
216 
217   for (i=0; i<imax; ++i) {
218     ierr = ISDestroy(&is[i]);CHKERRQ(ierr);
219   }
220 
221   /* Do Local work*/
222   ierr = MatIncreaseOverlap_MPIBAIJ_Local(C,imax,table,isz,data);CHKERRQ(ierr);
223 
224   /* Receive messages*/
225   ierr = PetscMalloc1(nrqr+1,&recv_status);CHKERRQ(ierr);
226   if (nrqr) {ierr = MPI_Waitall(nrqr,r_waits1,recv_status);CHKERRQ(ierr);}
227 
228   ierr = PetscMalloc1(nrqs+1,&s_status);CHKERRQ(ierr);
229   if (nrqs) {ierr = MPI_Waitall(nrqs,s_waits1,s_status);CHKERRQ(ierr);}
230 
231   /* Phase 1 sends are complete - deallocate buffers */
232   ierr = PetscFree4(outdat,ptr,tmp,ctr);CHKERRQ(ierr);
233   ierr = PetscFree4(w1,w2,w3,w4);CHKERRQ(ierr);
234 
235   ierr = PetscMalloc1(nrqr+1,&xdata);CHKERRQ(ierr);
236   ierr = PetscMalloc1(nrqr+1,&isz1);CHKERRQ(ierr);
237   ierr = MatIncreaseOverlap_MPIBAIJ_Receive(C,nrqr,rbuf,xdata,isz1);CHKERRQ(ierr);
238   ierr = PetscFree(rbuf[0]);CHKERRQ(ierr);
239   ierr = PetscFree(rbuf);CHKERRQ(ierr);
240 
241   /* Send the data back*/
242   /* Do a global reduction to know the buffer space req for incoming messages*/
243   {
244     PetscMPIInt *rw1;
245 
246     ierr = PetscCalloc1(size,&rw1);CHKERRQ(ierr);
247 
248     for (i=0; i<nrqr; ++i) {
249       proc = recv_status[i].MPI_SOURCE;
250       if (proc != onodes1[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"MPI_SOURCE mismatch");
251       rw1[proc] = isz1[i];
252     }
253 
254     ierr = PetscFree(onodes1);CHKERRQ(ierr);
255     ierr = PetscFree(olengths1);CHKERRQ(ierr);
256 
257     /* Determine the number of messages to expect, their lengths, from from-ids */
258     ierr = PetscGatherMessageLengths(comm,nrqr,nrqs,rw1,&onodes2,&olengths2);CHKERRQ(ierr);
259     ierr = PetscFree(rw1);CHKERRQ(ierr);
260   }
261   /* Now post the Irecvs corresponding to these messages */
262   ierr = PetscPostIrecvInt(comm,tag2,nrqs,onodes2,olengths2,&rbuf2,&r_waits2);CHKERRQ(ierr);
263 
264   /*  Now  post the sends */
265   ierr = PetscMalloc1(nrqr+1,&s_waits2);CHKERRQ(ierr);
266   for (i=0; i<nrqr; ++i) {
267     j    = recv_status[i].MPI_SOURCE;
268     ierr = MPI_Isend(xdata[i],isz1[i],MPIU_INT,j,tag2,comm,s_waits2+i);CHKERRQ(ierr);
269   }
270 
271   /* receive work done on other processors*/
272   {
273     PetscMPIInt idex;
274     PetscInt    is_no,ct1,max,*rbuf2_i,isz_i,*data_i,jmax;
275     PetscBT     table_i;
276     MPI_Status  *status2;
277 
278     ierr = PetscMalloc1(PetscMax(nrqr,nrqs)+1,&status2);CHKERRQ(ierr);
279     for (i=0; i<nrqs; ++i) {
280       ierr = MPI_Waitany(nrqs,r_waits2,&idex,status2+i);CHKERRQ(ierr);
281       /* Process the message*/
282       rbuf2_i = rbuf2[idex];
283       ct1     = 2*rbuf2_i[0]+1;
284       jmax    = rbuf2[idex][0];
285       for (j=1; j<=jmax; j++) {
286         max     = rbuf2_i[2*j];
287         is_no   = rbuf2_i[2*j-1];
288         isz_i   = isz[is_no];
289         data_i  = data[is_no];
290         table_i = table[is_no];
291         for (k=0; k<max; k++,ct1++) {
292           row = rbuf2_i[ct1];
293           if (!PetscBTLookupSet(table_i,row)) data_i[isz_i++] = row;
294         }
295         isz[is_no] = isz_i;
296       }
297     }
298     if (nrqr) {ierr = MPI_Waitall(nrqr,s_waits2,status2);CHKERRQ(ierr);}
299     ierr = PetscFree(status2);CHKERRQ(ierr);
300   }
301 
302   for (i=0; i<imax; ++i) {
303     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz[i],data[i],PETSC_COPY_VALUES,is+i);CHKERRQ(ierr);
304   }
305 
306 
307   ierr = PetscFree(onodes2);CHKERRQ(ierr);
308   ierr = PetscFree(olengths2);CHKERRQ(ierr);
309 
310   ierr = PetscFree(pa);CHKERRQ(ierr);
311   ierr = PetscFree(rbuf2[0]);CHKERRQ(ierr);
312   ierr = PetscFree(rbuf2);CHKERRQ(ierr);
313   ierr = PetscFree(s_waits1);CHKERRQ(ierr);
314   ierr = PetscFree(r_waits1);CHKERRQ(ierr);
315   ierr = PetscFree(s_waits2);CHKERRQ(ierr);
316   ierr = PetscFree(r_waits2);CHKERRQ(ierr);
317   ierr = PetscFree5(table,data,isz,d_p,t_p);CHKERRQ(ierr);
318   ierr = PetscFree(s_status);CHKERRQ(ierr);
319   ierr = PetscFree(recv_status);CHKERRQ(ierr);
320   ierr = PetscFree(xdata[0]);CHKERRQ(ierr);
321   ierr = PetscFree(xdata);CHKERRQ(ierr);
322   ierr = PetscFree(isz1);CHKERRQ(ierr);
323   PetscFunctionReturn(0);
324 }
325 
326 /*
327    MatIncreaseOverlap_MPIBAIJ_Local - Called by MatincreaseOverlap, to do
328        the work on the local processor.
329 
330      Inputs:
331       C      - MAT_MPIBAIJ;
332       imax - total no of index sets processed at a time;
333       table  - an array of char - size = Mbs bits.
334 
335      Output:
336       isz    - array containing the count of the solution elements corresponding
337                to each index set;
338       data   - pointer to the solutions
339 */
340 static PetscErrorCode MatIncreaseOverlap_MPIBAIJ_Local(Mat C,PetscInt imax,PetscBT *table,PetscInt *isz,PetscInt **data)
341 {
342   Mat_MPIBAIJ *c = (Mat_MPIBAIJ*)C->data;
343   Mat         A  = c->A,B = c->B;
344   Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data,*b = (Mat_SeqBAIJ*)B->data;
345   PetscInt    start,end,val,max,rstart,cstart,*ai,*aj;
346   PetscInt    *bi,*bj,*garray,i,j,k,row,*data_i,isz_i;
347   PetscBT     table_i;
348 
349   PetscFunctionBegin;
350   rstart = c->rstartbs;
351   cstart = c->cstartbs;
352   ai     = a->i;
353   aj     = a->j;
354   bi     = b->i;
355   bj     = b->j;
356   garray = c->garray;
357 
358 
359   for (i=0; i<imax; i++) {
360     data_i  = data[i];
361     table_i = table[i];
362     isz_i   = isz[i];
363     for (j=0,max=isz[i]; j<max; j++) {
364       row   = data_i[j] - rstart;
365       start = ai[row];
366       end   = ai[row+1];
367       for (k=start; k<end; k++) { /* Amat */
368         val = aj[k] + cstart;
369         if (!PetscBTLookupSet(table_i,val)) data_i[isz_i++] = val;
370       }
371       start = bi[row];
372       end   = bi[row+1];
373       for (k=start; k<end; k++) { /* Bmat */
374         val = garray[bj[k]];
375         if (!PetscBTLookupSet(table_i,val)) data_i[isz_i++] = val;
376       }
377     }
378     isz[i] = isz_i;
379   }
380   PetscFunctionReturn(0);
381 }
382 /*
383       MatIncreaseOverlap_MPIBAIJ_Receive - Process the recieved messages,
384          and return the output
385 
386          Input:
387            C    - the matrix
388            nrqr - no of messages being processed.
389            rbuf - an array of pointers to the recieved requests
390 
391          Output:
392            xdata - array of messages to be sent back
393            isz1  - size of each message
394 
395   For better efficiency perhaps we should malloc separately each xdata[i],
396 then if a remalloc is required we need only copy the data for that one row
397 rather than all previous rows as it is now where a single large chunck of
398 memory is used.
399 
400 */
401 static PetscErrorCode MatIncreaseOverlap_MPIBAIJ_Receive(Mat C,PetscInt nrqr,PetscInt **rbuf,PetscInt **xdata,PetscInt * isz1)
402 {
403   Mat_MPIBAIJ    *c = (Mat_MPIBAIJ*)C->data;
404   Mat            A  = c->A,B = c->B;
405   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data,*b = (Mat_SeqBAIJ*)B->data;
406   PetscErrorCode ierr;
407   PetscInt       rstart,cstart,*ai,*aj,*bi,*bj,*garray,i,j,k;
408   PetscInt       row,total_sz,ct,ct1,ct2,ct3,mem_estimate,oct2,l,start,end;
409   PetscInt       val,max1,max2,Mbs,no_malloc =0,*tmp,new_estimate,ctr;
410   PetscInt       *rbuf_i,kmax,rbuf_0;
411   PetscBT        xtable;
412 
413   PetscFunctionBegin;
414   Mbs    = c->Mbs;
415   rstart = c->rstartbs;
416   cstart = c->cstartbs;
417   ai     = a->i;
418   aj     = a->j;
419   bi     = b->i;
420   bj     = b->j;
421   garray = c->garray;
422 
423 
424   for (i=0,ct=0,total_sz=0; i<nrqr; ++i) {
425     rbuf_i =  rbuf[i];
426     rbuf_0 =  rbuf_i[0];
427     ct    += rbuf_0;
428     for (j=1; j<=rbuf_0; j++) total_sz += rbuf_i[2*j];
429   }
430 
431   if (c->Mbs) max1 = ct*(a->nz +b->nz)/c->Mbs;
432   else        max1 = 1;
433   mem_estimate = 3*((total_sz > max1 ? total_sz : max1)+1);
434   ierr         = PetscMalloc1(mem_estimate,&xdata[0]);CHKERRQ(ierr);
435   ++no_malloc;
436   ierr = PetscBTCreate(Mbs,&xtable);CHKERRQ(ierr);
437   ierr = PetscMemzero(isz1,nrqr*sizeof(PetscInt));CHKERRQ(ierr);
438 
439   ct3 = 0;
440   for (i=0; i<nrqr; i++) { /* for easch mesg from proc i */
441     rbuf_i =  rbuf[i];
442     rbuf_0 =  rbuf_i[0];
443     ct1    =  2*rbuf_0+1;
444     ct2    =  ct1;
445     ct3   += ct1;
446     for (j=1; j<=rbuf_0; j++) { /* for each IS from proc i*/
447       ierr = PetscBTMemzero(Mbs,xtable);CHKERRQ(ierr);
448       oct2 = ct2;
449       kmax = rbuf_i[2*j];
450       for (k=0; k<kmax; k++,ct1++) {
451         row = rbuf_i[ct1];
452         if (!PetscBTLookupSet(xtable,row)) {
453           if (!(ct3 < mem_estimate)) {
454             new_estimate = (PetscInt)(1.5*mem_estimate)+1;
455             ierr         = PetscMalloc1(new_estimate,&tmp);CHKERRQ(ierr);
456             ierr         = PetscMemcpy(tmp,xdata[0],mem_estimate*sizeof(PetscInt));CHKERRQ(ierr);
457             ierr         = PetscFree(xdata[0]);CHKERRQ(ierr);
458             xdata[0]     = tmp;
459             mem_estimate = new_estimate; ++no_malloc;
460             for (ctr=1; ctr<=i; ctr++) xdata[ctr] = xdata[ctr-1] + isz1[ctr-1];
461           }
462           xdata[i][ct2++] = row;
463           ct3++;
464         }
465       }
466       for (k=oct2,max2=ct2; k<max2; k++)  {
467         row   = xdata[i][k] - rstart;
468         start = ai[row];
469         end   = ai[row+1];
470         for (l=start; l<end; l++) {
471           val = aj[l] + cstart;
472           if (!PetscBTLookupSet(xtable,val)) {
473             if (!(ct3 < mem_estimate)) {
474               new_estimate = (PetscInt)(1.5*mem_estimate)+1;
475               ierr         = PetscMalloc1(new_estimate,&tmp);CHKERRQ(ierr);
476               ierr         = PetscMemcpy(tmp,xdata[0],mem_estimate*sizeof(PetscInt));CHKERRQ(ierr);
477               ierr         = PetscFree(xdata[0]);CHKERRQ(ierr);
478               xdata[0]     = tmp;
479               mem_estimate = new_estimate; ++no_malloc;
480               for (ctr=1; ctr<=i; ctr++) xdata[ctr] = xdata[ctr-1] + isz1[ctr-1];
481             }
482             xdata[i][ct2++] = val;
483             ct3++;
484           }
485         }
486         start = bi[row];
487         end   = bi[row+1];
488         for (l=start; l<end; l++) {
489           val = garray[bj[l]];
490           if (!PetscBTLookupSet(xtable,val)) {
491             if (!(ct3 < mem_estimate)) {
492               new_estimate = (PetscInt)(1.5*mem_estimate)+1;
493               ierr         = PetscMalloc1(new_estimate,&tmp);CHKERRQ(ierr);
494               ierr         = PetscMemcpy(tmp,xdata[0],mem_estimate*sizeof(PetscInt));CHKERRQ(ierr);
495               ierr         = PetscFree(xdata[0]);CHKERRQ(ierr);
496               xdata[0]     = tmp;
497               mem_estimate = new_estimate; ++no_malloc;
498               for (ctr =1; ctr <=i; ctr++) xdata[ctr] = xdata[ctr-1] + isz1[ctr-1];
499             }
500             xdata[i][ct2++] = val;
501             ct3++;
502           }
503         }
504       }
505       /* Update the header*/
506       xdata[i][2*j]   = ct2 - oct2; /* Undo the vector isz1 and use only a var*/
507       xdata[i][2*j-1] = rbuf_i[2*j-1];
508     }
509     xdata[i][0] = rbuf_0;
510     xdata[i+1]  = xdata[i] + ct2;
511     isz1[i]     = ct2; /* size of each message */
512   }
513   ierr = PetscBTDestroy(&xtable);CHKERRQ(ierr);
514   ierr = PetscInfo3(C,"Allocated %D bytes, required %D, no of mallocs = %D\n",mem_estimate,ct3,no_malloc);CHKERRQ(ierr);
515   PetscFunctionReturn(0);
516 }
517 
518 PetscErrorCode MatGetSubMatrices_MPIBAIJ(Mat C,PetscInt ismax,const IS isrow[],const IS iscol[],MatReuse scall,Mat *submat[])
519 {
520   IS             *isrow_block,*iscol_block;
521   Mat_MPIBAIJ    *c = (Mat_MPIBAIJ*)C->data;
522   PetscErrorCode ierr;
523   PetscInt       nmax,nstages_local,nstages,i,pos,max_no,ncol,nrow,N=C->cmap->N,bs=C->rmap->bs;
524   PetscBool      colflag,*allcolumns,*allrows;
525 
526   PetscFunctionBegin;
527   //printf("MatGetSubMatrices_MPIBAIJ scall %d, bs %d\n",scall,bs);
528   /* The compression and expansion should be avoided. Doesn't point
529      out errors, might change the indices, hence buggey */
530   ierr = PetscMalloc2(ismax+1,&isrow_block,ismax+1,&iscol_block);CHKERRQ(ierr);
531   ierr = ISCompressIndicesGeneral(N,C->rmap->n,bs,ismax,isrow,isrow_block);CHKERRQ(ierr);
532   ierr = ISCompressIndicesGeneral(N,C->cmap->n,bs,ismax,iscol,iscol_block);CHKERRQ(ierr);
533 
534   /* Check for special case: each processor gets entire matrix columns */
535   ierr = PetscMalloc2(ismax+1,&allcolumns,ismax+1,&allrows);CHKERRQ(ierr);
536   for (i=0; i<ismax; i++) {
537     ierr = ISIdentity(iscol[i],&colflag);CHKERRQ(ierr);
538     ierr = ISGetLocalSize(iscol[i],&ncol);CHKERRQ(ierr);
539     if (colflag && ncol == C->cmap->N) allcolumns[i] = PETSC_TRUE;
540     else allcolumns[i] = PETSC_FALSE;
541 
542     ierr = ISIdentity(isrow[i],&colflag);CHKERRQ(ierr);
543     ierr = ISGetLocalSize(isrow[i],&nrow);CHKERRQ(ierr);
544     if (colflag && nrow == C->rmap->N) allrows[i] = PETSC_TRUE;
545     else allrows[i] = PETSC_FALSE;
546   }
547 
548   /* Allocate memory to hold all the submatrices */
549   if (scall == MAT_INITIAL_MATRIX) {
550     ierr = PetscMalloc1(ismax+1,submat);CHKERRQ(ierr);
551   }
552   /* Determine the number of stages through which submatrices are done */
553   nmax = 20*1000000 / (c->Nbs * sizeof(PetscInt));
554   if (!nmax) nmax = 1;
555   nstages_local = ismax/nmax + ((ismax % nmax) ? 1 : 0);
556 
557   /* Make sure every processor loops through the nstages */
558   ierr = MPIU_Allreduce(&nstages_local,&nstages,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)C));CHKERRQ(ierr);
559   for (i=0,pos=0; i<nstages; i++) {
560     if (pos+nmax <= ismax) max_no = nmax;
561     else if (pos == ismax) max_no = 0;
562     else                   max_no = ismax-pos;
563 
564     PetscBool isbaij;
565     ierr = PetscObjectTypeCompare((PetscObject)C,MATMPIBAIJ,&isbaij);CHKERRQ(ierr);
566     if (isbaij) {
567       ierr = MatGetSubMatrices_MPIBAIJ_local_new(C,max_no,isrow_block+pos,iscol_block+pos,scall,allrows+pos,allcolumns+pos,*submat+pos);CHKERRQ(ierr);
568     } else {
569       ierr = MatGetSubMatrices_MPIBAIJ_local(C,max_no,isrow_block+pos,iscol_block+pos,scall,allrows+pos,allcolumns+pos,*submat+pos);CHKERRQ(ierr);
570     }
571     pos += max_no;
572   }
573 
574   for (i=0; i<ismax; i++) {
575     ierr = ISDestroy(&isrow_block[i]);CHKERRQ(ierr);
576     ierr = ISDestroy(&iscol_block[i]);CHKERRQ(ierr);
577   }
578   ierr = PetscFree2(isrow_block,iscol_block);CHKERRQ(ierr);
579   ierr = PetscFree2(allcolumns,allrows);CHKERRQ(ierr);
580   PetscFunctionReturn(0);
581 }
582 
583 #if defined(PETSC_USE_CTABLE)
584 PetscErrorCode PetscGetProc(const PetscInt row, const PetscMPIInt size, const PetscInt proc_gnode[], PetscMPIInt *rank)
585 {
586   PetscInt       nGlobalNd = proc_gnode[size];
587   PetscMPIInt    fproc;
588   PetscErrorCode ierr;
589 
590   PetscFunctionBegin;
591   ierr = PetscMPIIntCast((PetscInt)(((float)row * (float)size / (float)nGlobalNd + 0.5)),&fproc);CHKERRQ(ierr);
592   if (fproc > size) fproc = size;
593   while (row < proc_gnode[fproc] || row >= proc_gnode[fproc+1]) {
594     if (row < proc_gnode[fproc]) fproc--;
595     else                         fproc++;
596   }
597   *rank = fproc;
598   PetscFunctionReturn(0);
599 }
600 #endif
601 
602 /* -------------------------------------------------------------------------*/
603 /* This code is used for BAIJ and SBAIJ matrices (unfortunate dependency) */
604 
605 PetscErrorCode MatDestroy_MPIBAIJ_MatGetSubmatrices(Mat C)
606 {
607   PetscErrorCode ierr;
608   Mat_SeqBAIJ    *c = (Mat_SeqBAIJ*)C->data;
609   Mat_SubMat     *submatj = c->submatis1;
610   PetscInt       i;
611 
612   PetscFunctionBegin;
613   if (!submatj->id) { /* delete data that are linked only to submats[id=0] */
614     ierr = PetscFree4(submatj->sbuf1,submatj->ptr,submatj->tmp,submatj->ctr);CHKERRQ(ierr);
615 
616     for (i=0; i<submatj->nrqr; ++i) {
617       ierr = PetscFree(submatj->sbuf2[i]);CHKERRQ(ierr);
618     }
619     ierr = PetscFree3(submatj->sbuf2,submatj->req_size,submatj->req_source1);CHKERRQ(ierr);
620 
621     if (submatj->rbuf1) {
622       ierr = PetscFree(submatj->rbuf1[0]);CHKERRQ(ierr);
623       ierr = PetscFree(submatj->rbuf1);CHKERRQ(ierr);
624     }
625 
626     for (i=0; i<submatj->nrqs; ++i) {
627       ierr = PetscFree(submatj->rbuf3[i]);CHKERRQ(ierr);
628     }
629     ierr = PetscFree3(submatj->req_source2,submatj->rbuf2,submatj->rbuf3);CHKERRQ(ierr);
630     ierr = PetscFree(submatj->pa);CHKERRQ(ierr);
631   }
632 
633 #if defined(PETSC_USE_CTABLE)
634   ierr = PetscTableDestroy((PetscTable*)&submatj->rmap);CHKERRQ(ierr);
635   if (submatj->cmap_loc) {ierr = PetscFree(submatj->cmap_loc);CHKERRQ(ierr);}
636   ierr = PetscFree(submatj->rmap_loc);CHKERRQ(ierr);
637 #else
638   ierr = PetscFree(submatj->rmap);CHKERRQ(ierr);
639 #endif
640 
641   if (!submatj->allcolumns) {
642 #if defined(PETSC_USE_CTABLE)
643     ierr = PetscTableDestroy((PetscTable*)&submatj->cmap);CHKERRQ(ierr);
644 #else
645     ierr = PetscFree(submatj->cmap);CHKERRQ(ierr);
646 #endif
647   }
648   ierr = submatj->destroy(C);CHKERRQ(ierr);
649   ierr = PetscFree(submatj->row2proc);CHKERRQ(ierr);
650 
651   ierr = PetscFree(submatj);CHKERRQ(ierr);
652   PetscFunctionReturn(0);
653 }
654 
655 PetscErrorCode MatGetSubMatrices_MPIBAIJ_local_new(Mat C,PetscInt ismax,const IS isrow[],const IS iscol[],MatReuse scall,PetscBool *allrows,PetscBool *allcolumns,Mat *submats)
656 {
657   Mat_MPIBAIJ    *c = (Mat_MPIBAIJ*)C->data;
658   Mat            A  = c->A;
659   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data,*b = (Mat_SeqBAIJ*)c->B->data,*subc;
660   const PetscInt **icol,**irow;
661   PetscInt       *nrow,*ncol,start;
662   PetscErrorCode ierr;
663   PetscMPIInt    rank,size,tag0,tag2,tag3,tag4,*w1,*w2,*w3,*w4,nrqr;
664   PetscInt       **sbuf1,**sbuf2,i,j,k,l,ct1,ct2,**rbuf1,row,proc=-1;
665   PetscInt       nrqs=0,msz,**ptr=NULL,*req_size=NULL,*ctr=NULL,*pa,*tmp=NULL,tcol;
666   PetscInt       **rbuf3=NULL,*req_source1=NULL,*req_source2,**sbuf_aj,**rbuf2=NULL,max1,max2;
667   PetscInt       **lens,is_no,ncols,*cols,mat_i,*mat_j,tmp2,jmax;
668 #if defined(PETSC_USE_CTABLE)
669   PetscTable     *cmap,cmap_i=NULL,*rmap,rmap_i;
670 #else
671   PetscInt       **cmap,*cmap_i=NULL,**rmap,*rmap_i;
672 #endif
673   const PetscInt *irow_i;
674   PetscInt       ctr_j,*sbuf1_j,*sbuf_aj_i,*rbuf1_i,kmax,*lens_i;
675   MPI_Request    *s_waits1,*r_waits1,*s_waits2,*r_waits2,*r_waits3;
676   MPI_Request    *r_waits4,*s_waits3,*s_waits4;
677   MPI_Status     *r_status1,*r_status2,*s_status1,*s_status3,*s_status2;
678   MPI_Status     *r_status3,*r_status4,*s_status4;
679   MPI_Comm       comm;
680   PetscScalar    **rbuf4,*rbuf4_i,**sbuf_aa,*vals,*mat_a,*imat_a,*sbuf_aa_i;
681   PetscMPIInt    *onodes1,*olengths1,end;
682   PetscInt       **row2proc,*row2proc_i,ilen_row,*imat_ilen,*imat_j,*imat_i,old_row;
683   Mat_SubMat     **smats,*smat_i;
684   PetscBool      *issorted,colflag,iscsorted=PETSC_TRUE;
685   PetscInt       *sbuf1_i,*rbuf2_i,*rbuf3_i,ilen;
686 
687   PetscInt       bs=C->rmap->bs,bs2=c->bs2,rstart = c->rstartbs;
688   PetscBool      ijonly=c->ijonly; /* private flag indicates only matrix data structures are requested */
689   PetscInt       nzA,nzB,*a_i=a->i,*b_i=b->i,*a_j = a->j,*b_j = b->j,ctmp,imark,*cworkA,*cworkB;
690   PetscScalar    *vworkA,*vworkB,*a_a = a->a,*b_a = b->a;
691   PetscInt       cstart = c->cstartbs,*bmap = c->garray;
692 
693   PetscFunctionBegin;
694   ierr = PetscObjectGetComm((PetscObject)C,&comm);CHKERRQ(ierr);
695   size = c->size;
696   rank = c->rank;
697   if (!rank) {
698     printf("MatGetSubMatrices_MPIBAIJ_new scall %d, bs %d\n",scall,bs);
699   }
700 
701   ierr = PetscMalloc5(ismax,&smats,ismax,&row2proc,ismax,&cmap,ismax,&rmap,ismax,&allcolumns);CHKERRQ(ierr);
702   ierr = PetscMalloc5(ismax,&irow,ismax,&icol,ismax,&nrow,ismax,&ncol,ismax,&issorted);CHKERRQ(ierr);
703 
704   for (i=0; i<ismax; i++) {
705     ierr = ISSorted(iscol[i],&issorted[i]);CHKERRQ(ierr);
706     if (!issorted[i]) iscsorted = issorted[i];
707 
708     ierr = ISSorted(isrow[i],&issorted[i]);CHKERRQ(ierr);
709 
710     ierr = ISGetIndices(isrow[i],&irow[i]);CHKERRQ(ierr);
711     ierr = ISGetLocalSize(isrow[i],&nrow[i]);CHKERRQ(ierr);
712 
713     /* Check for special case: allcolumn */
714     ierr = ISIdentity(iscol[i],&colflag);CHKERRQ(ierr);
715     ierr = ISGetLocalSize(iscol[i],&ncol[i]);CHKERRQ(ierr);
716     if (colflag && ncol[i] == C->cmap->N) {
717       allcolumns[i] = PETSC_TRUE;
718       icol[i] = NULL;
719     } else {
720       allcolumns[i] = PETSC_FALSE;
721       ierr = ISGetIndices(iscol[i],&icol[i]);CHKERRQ(ierr);
722     }
723   }
724 
725   if (scall == MAT_REUSE_MATRIX) {
726     /* Assumes new rows are same length as the old rows */
727     for (i=0; i<ismax; i++) {
728       subc = (Mat_SeqBAIJ*)(submats[i]->data);
729       if ((submats[i]->rmap->n != nrow[i]) || (submats[i]->cmap->n != ncol[i])) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
730 
731       /* Initial matrix as if empty */
732       ierr = PetscMemzero(subc->ilen,submats[i]->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
733 
734       /* Initial matrix as if empty */
735       submats[i]->factortype = C->factortype;
736 
737       smat_i   = subc->submatis1;
738       smats[i] = smat_i;
739 
740       nrqs        = smat_i->nrqs;
741       nrqr        = smat_i->nrqr;
742       rbuf1       = smat_i->rbuf1;
743       rbuf2       = smat_i->rbuf2;
744       rbuf3       = smat_i->rbuf3;
745       req_source2 = smat_i->req_source2;
746 
747       sbuf1     = smat_i->sbuf1;
748       sbuf2     = smat_i->sbuf2;
749       ptr       = smat_i->ptr;
750       tmp       = smat_i->tmp;
751       ctr       = smat_i->ctr;
752 
753       pa          = smat_i->pa;
754       req_size    = smat_i->req_size;
755       req_source1 = smat_i->req_source1;
756 
757       allcolumns[i] = smat_i->allcolumns;
758       row2proc[i]   = smat_i->row2proc;
759       rmap[i]       = smat_i->rmap;
760       cmap[i]       = smat_i->cmap;
761     }
762   } else { /* scall == MAT_INITIAL_MATRIX */
763     /* Get some new tags to keep the communication clean */
764     ierr = PetscObjectGetNewTag((PetscObject)C,&tag2);CHKERRQ(ierr);
765     ierr = PetscObjectGetNewTag((PetscObject)C,&tag3);CHKERRQ(ierr);
766 
767     /* evaluate communication - mesg to who, length of mesg, and buffer space
768      required. Based on this, buffers are allocated, and data copied into them*/
769     ierr = PetscCalloc4(size,&w1,size,&w2,size,&w3,size,&w4);CHKERRQ(ierr);   /* mesg size, initialize work vectors */
770 
771     for (i=0; i<ismax; i++) {
772       jmax   = nrow[i];
773       irow_i = irow[i];
774 
775       ierr   = PetscMalloc1(jmax,&row2proc_i);CHKERRQ(ierr);
776       row2proc[i] = row2proc_i;
777 
778       if (issorted[i]) proc = 0;
779       for (j=0; j<jmax; j++) {
780         if (!issorted[i]) proc = 0;
781         row = irow_i[j];
782         //while (row >= C->rmap->range[proc+1]) proc++;
783         while (row >= c->rangebs[proc+1]) proc++;
784         w4[proc]++;
785         row2proc_i[j] = proc; /* map row index to proc */
786       }
787       for (j=0; j<size; j++) {
788         if (w4[j]) { w1[j] += w4[j];  w3[j]++; w4[j] = 0;}
789       }
790     }
791 
792     nrqs     = 0;              /* no of outgoing messages */
793     msz      = 0;              /* total mesg length (for all procs) */
794     w1[rank] = 0;              /* no mesg sent to self */
795     w3[rank] = 0;
796     for (i=0; i<size; i++) {
797       if (w1[i])  { w2[i] = 1; nrqs++;} /* there exists a message to proc i */
798     }
799     ierr = PetscMalloc1(nrqs+1,&pa);CHKERRQ(ierr); /*(proc -array)*/
800     for (i=0,j=0; i<size; i++) {
801       if (w1[i]) { pa[j] = i; j++; }
802     }
803 
804     /* Each message would have a header = 1 + 2*(no of IS) + data */
805     for (i=0; i<nrqs; i++) {
806       j      = pa[i];
807       w1[j] += w2[j] + 2* w3[j];
808       msz   += w1[j];
809     }
810     ierr = PetscInfo2(0,"Number of outgoing messages %D Total message length %D\n",nrqs,msz);CHKERRQ(ierr);
811 
812     /* Determine the number of messages to expect, their lengths, from from-ids */
813     ierr = PetscGatherNumberOfMessages(comm,w2,w1,&nrqr);CHKERRQ(ierr);
814     ierr = PetscGatherMessageLengths(comm,nrqs,nrqr,w1,&onodes1,&olengths1);CHKERRQ(ierr);
815     printf("[%d] nrqs %d, nrqr %d\n",rank,nrqs,nrqr);
816 
817     /* Now post the Irecvs corresponding to these messages */
818     tag0 = ((PetscObject)C)->tag;
819     ierr = PetscPostIrecvInt(comm,tag0,nrqr,onodes1,olengths1,&rbuf1,&r_waits1);CHKERRQ(ierr);
820     if (rank == 1) {
821       for (i=0; i<nrqr; i++) printf(" onodes1 [%d] olengths1 %d\n",onodes1[i],olengths1[i]);
822     }
823 
824     ierr = PetscFree(onodes1);CHKERRQ(ierr);
825     ierr = PetscFree(olengths1);CHKERRQ(ierr);
826 
827     /* Allocate Memory for outgoing messages */
828     ierr = PetscMalloc4(size,&sbuf1,size,&ptr,2*msz,&tmp,size,&ctr);CHKERRQ(ierr);
829     ierr = PetscMemzero(sbuf1,size*sizeof(PetscInt*));CHKERRQ(ierr);
830     ierr = PetscMemzero(ptr,size*sizeof(PetscInt*));CHKERRQ(ierr);
831 
832     {
833       PetscInt *iptr = tmp;
834       k    = 0;
835       for (i=0; i<nrqs; i++) {
836         j        = pa[i];
837         iptr    += k;
838         sbuf1[j] = iptr;
839         k        = w1[j];
840       }
841     }
842 
843     /* Form the outgoing messages. Initialize the header space */
844     for (i=0; i<nrqs; i++) {
845       j           = pa[i];
846       sbuf1[j][0] = 0;
847       ierr        = PetscMemzero(sbuf1[j]+1,2*w3[j]*sizeof(PetscInt));CHKERRQ(ierr);
848       ptr[j]      = sbuf1[j] + 2*w3[j] + 1;
849     }
850 
851     /* Parse the isrow and copy data into outbuf */
852     for (i=0; i<ismax; i++) {
853       row2proc_i = row2proc[i];
854       ierr   = PetscMemzero(ctr,size*sizeof(PetscInt));CHKERRQ(ierr);
855       irow_i = irow[i];
856       jmax   = nrow[i];
857       for (j=0; j<jmax; j++) {  /* parse the indices of each IS */
858         proc = row2proc_i[j];
859         if (proc != rank) { /* copy to the outgoing buf*/
860           ctr[proc]++;
861           *ptr[proc] = irow_i[j];
862           ptr[proc]++;
863         }
864       }
865       /* Update the headers for the current IS */
866       for (j=0; j<size; j++) { /* Can Optimise this loop too */
867         if ((ctr_j = ctr[j])) {
868           sbuf1_j        = sbuf1[j];
869           k              = ++sbuf1_j[0];
870           sbuf1_j[2*k]   = ctr_j;
871           sbuf1_j[2*k-1] = i;
872         }
873       }
874     }
875 
876     /*  Now  post the sends */
877     ierr = PetscMalloc1(nrqs+1,&s_waits1);CHKERRQ(ierr);
878     for (i=0; i<nrqs; ++i) {
879       j    = pa[i];
880       printf("[%d] sends sbuf1 %d to [%d]\n",rank,w1[j],j);
881       ierr = MPI_Isend(sbuf1[j],w1[j],MPIU_INT,j,tag0,comm,s_waits1+i);CHKERRQ(ierr);
882     }
883 
884     /* Post Receives to capture the buffer size */
885     ierr = PetscMalloc1(nrqs+1,&r_waits2);CHKERRQ(ierr);
886     ierr = PetscMalloc3(nrqs+1,&req_source2,nrqs+1,&rbuf2,nrqs+1,&rbuf3);CHKERRQ(ierr);
887     rbuf2[0] = tmp + msz;
888     for (i=1; i<nrqs; ++i) {
889       rbuf2[i] = rbuf2[i-1]+w1[pa[i-1]];
890     }
891     for (i=0; i<nrqs; ++i) {
892       j    = pa[i];
893       ierr = MPI_Irecv(rbuf2[i],w1[j],MPIU_INT,j,tag2,comm,r_waits2+i);CHKERRQ(ierr);
894     }
895 
896     /* Send to other procs the buf size they should allocate */
897     /* Receive messages*/
898     ierr = PetscMalloc1(nrqr+1,&s_waits2);CHKERRQ(ierr);
899     ierr = PetscMalloc1(nrqr+1,&r_status1);CHKERRQ(ierr);
900     ierr = PetscMalloc3(nrqr,&sbuf2,nrqr,&req_size,nrqr,&req_source1);CHKERRQ(ierr);
901     {
902       PetscInt   *sAi = a->i,*sBi = b->i,id;
903       PetscInt   *sbuf2_i;
904 
905       ierr = MPI_Waitall(nrqr,r_waits1,r_status1);CHKERRQ(ierr);
906       for (i=0; i<nrqr; ++i) {
907         req_size[i] = 0;
908         rbuf1_i        = rbuf1[i];
909         start          = 2*rbuf1_i[0] + 1;
910         ierr           = MPI_Get_count(r_status1+i,MPIU_INT,&end);CHKERRQ(ierr);
911         ierr           = PetscMalloc1(end+1,&sbuf2[i]);CHKERRQ(ierr);
912         sbuf2_i        = sbuf2[i];
913         for (j=start; j<end; j++) {
914           id              = rbuf1_i[j] - rstart;
915           ncols           = sAi[id+1] - sAi[id] + sBi[id+1] - sBi[id];
916           sbuf2_i[j]      = ncols;
917           req_size[i] += ncols;
918         }
919         req_source1[i] = r_status1[i].MPI_SOURCE;
920         /* form the header */
921         sbuf2_i[0] = req_size[i];
922         for (j=1; j<start; j++) sbuf2_i[j] = rbuf1_i[j];
923 
924         ierr = MPI_Isend(sbuf2_i,end,MPIU_INT,req_source1[i],tag2,comm,s_waits2+i);CHKERRQ(ierr);
925       }
926     }
927     ierr = PetscFree(r_status1);CHKERRQ(ierr);
928     ierr = PetscFree(r_waits1);CHKERRQ(ierr);
929     ierr = PetscFree4(w1,w2,w3,w4);CHKERRQ(ierr);
930 
931     /* Receive messages*/
932     ierr = PetscMalloc1(nrqs+1,&r_waits3);CHKERRQ(ierr);
933     ierr = PetscMalloc1(nrqs+1,&r_status2);CHKERRQ(ierr);
934 
935     ierr = MPI_Waitall(nrqs,r_waits2,r_status2);CHKERRQ(ierr);
936     for (i=0; i<nrqs; ++i) {
937       ierr = PetscMalloc1(rbuf2[i][0]+1,&rbuf3[i]);CHKERRQ(ierr);
938       req_source2[i] = r_status2[i].MPI_SOURCE;
939       ierr = MPI_Irecv(rbuf3[i],rbuf2[i][0],MPIU_INT,req_source2[i],tag3,comm,r_waits3+i);CHKERRQ(ierr);
940     }
941     ierr = PetscFree(r_status2);CHKERRQ(ierr);
942     ierr = PetscFree(r_waits2);CHKERRQ(ierr);
943 
944     /* Wait on sends1 and sends2 */
945     ierr = PetscMalloc1(nrqs+1,&s_status1);CHKERRQ(ierr);
946     ierr = PetscMalloc1(nrqr+1,&s_status2);CHKERRQ(ierr);
947 
948     if (nrqs) {ierr = MPI_Waitall(nrqs,s_waits1,s_status1);CHKERRQ(ierr);}
949     if (nrqr) {ierr = MPI_Waitall(nrqr,s_waits2,s_status2);CHKERRQ(ierr);}
950     ierr = PetscFree(s_status1);CHKERRQ(ierr);
951     ierr = PetscFree(s_status2);CHKERRQ(ierr);
952     ierr = PetscFree(s_waits1);CHKERRQ(ierr);
953     ierr = PetscFree(s_waits2);CHKERRQ(ierr);
954 
955     /* Now allocate sending buffers for a->j, and send them off */
956     ierr = PetscMalloc1(nrqr+1,&sbuf_aj);CHKERRQ(ierr);
957     for (i=0,j=0; i<nrqr; i++) j += req_size[i];
958     ierr = PetscMalloc1(j+1,&sbuf_aj[0]);CHKERRQ(ierr);
959     for (i=1; i<nrqr; i++) sbuf_aj[i] = sbuf_aj[i-1] + req_size[i-1];
960 
961     ierr = PetscMalloc1(nrqr+1,&s_waits3);CHKERRQ(ierr);
962     {
963       //PetscInt *a_i = a->i,*b_i = b->i;
964       //PetscInt cstart = c->cstartbs,*bmap = c->garray;
965       //PetscInt cend = c->cendbs;
966       //PetscInt *a_j = a->j,*b_j = b->j,ctmp,imark;
967 
968       for (i=0; i<nrqr; i++) {
969         rbuf1_i   = rbuf1[i];
970         sbuf_aj_i = sbuf_aj[i];
971         ct1       = 2*rbuf1_i[0] + 1;
972         ct2       = 0;
973         for (j=1,max1=rbuf1_i[0]; j<=max1; j++) {
974           kmax = rbuf1[i][2*j];
975           for (k=0; k<kmax; k++,ct1++) {
976             row    = rbuf1_i[ct1] - rstart;
977             nzA    = a_i[row+1] - a_i[row]; nzB = b_i[row+1] - b_i[row];
978             ncols  = nzA + nzB;
979             cworkA = a_j + a_i[row]; cworkB = b_j + b_i[row];
980 
981             /* load the column indices for this row into cols */
982             cols = sbuf_aj_i + ct2;
983             for (l=0; l<nzB; l++) {
984               if ((ctmp = bmap[cworkB[l]]) < cstart) cols[l] = ctmp;
985               else break;
986             }
987             imark = l;
988             for (l=0; l<nzA; l++) {cols[imark+l] = cstart + cworkA[l];}
989             for (l=imark; l<nzB; l++) cols[nzA+l] = bmap[cworkB[l]];
990             ct2 += ncols;
991           }
992         }
993         ierr = MPI_Isend(sbuf_aj_i,req_size[i],MPIU_INT,req_source1[i],tag3,comm,s_waits3+i);CHKERRQ(ierr);
994       }
995     }
996     ierr = PetscMalloc2(nrqs+1,&r_status3,nrqr+1,&s_status3);CHKERRQ(ierr);
997 
998     /* create col map: global col of C -> local col of submatrices */
999     {
1000       const PetscInt *icol_i;
1001 #if defined(PETSC_USE_CTABLE)
1002       for (i=0; i<ismax; i++) {
1003         if (!allcolumns[i]) {
1004           //ierr = PetscTableCreate(ncol[i]+1,C->cmap->N+1,&cmap[i]);CHKERRQ(ierr);
1005           ierr = PetscTableCreate(ncol[i]+1,c->Nbs+1,&cmap[i]);CHKERRQ(ierr);
1006 
1007           jmax   = ncol[i];
1008           icol_i = icol[i];
1009           cmap_i = cmap[i];
1010           for (j=0; j<jmax; j++) {
1011             ierr = PetscTableAdd(cmap[i],icol_i[j]+1,j+1,INSERT_VALUES);CHKERRQ(ierr);
1012           }
1013         } else cmap[i] = NULL;
1014       }
1015 #else
1016       for (i=0; i<ismax; i++) {
1017         if (!allcolumns[i]) {
1018           ierr   = PetscCalloc1(c->Nbs,&cmap[i]);CHKERRQ(ierr);
1019           jmax   = ncol[i];
1020           icol_i = icol[i];
1021           cmap_i = cmap[i];
1022           for (j=0; j<jmax; j++) cmap_i[icol_i[j]] = j+1;
1023         } else cmap[i] = NULL;
1024       }
1025 #endif
1026     }
1027 
1028     /* Create lens which is required for MatCreate... */
1029     for (i=0,j=0; i<ismax; i++) j += nrow[i];
1030     ierr = PetscMalloc1(ismax,&lens);CHKERRQ(ierr);
1031 
1032     if (ismax) {
1033       ierr = PetscCalloc1(j,&lens[0]);CHKERRQ(ierr);
1034     }
1035     for (i=1; i<ismax; i++) lens[i] = lens[i-1] + nrow[i-1];
1036 
1037     /* Update lens from local data */
1038     for (i=0; i<ismax; i++) {
1039       row2proc_i = row2proc[i];
1040       jmax = nrow[i];
1041       if (!allcolumns[i]) cmap_i = cmap[i];
1042       irow_i = irow[i];
1043       lens_i = lens[i];
1044       for (j=0; j<jmax; j++) {
1045         row = irow_i[j]; /* global blocked row of C */
1046         proc = row2proc_i[j];
1047         //if (!rank) printf("j %d, proc %d\n",j,proc);
1048         if (proc == rank) {
1049 
1050                   /* Get indices from matA and then from matB */
1051           PetscInt nzA,nzB,*cworkA,*cworkB,cstart = c->cstartbs,*bmap  = c->garray;
1052           PetscInt *a_i = a->i,*b_i=b->i,*a_j = a->j,*b_j = b->j;
1053  #if defined(PETSC_USE_CTABLE)
1054           PetscInt   tt;
1055 #endif
1056         row    = row - rstart;
1057         nzA    = a_i[row+1] - a_i[row];
1058         nzB    = b_i[row+1] - b_i[row];
1059         cworkA =  a_j + a_i[row];
1060         cworkB = b_j + b_i[row];
1061 
1062         //printf("[%d] row_loc %d\n",rank,row);
1063         if (!allcolumns[i]) {
1064 #if defined(PETSC_USE_CTABLE)
1065           for (k=0; k<nzA; k++) {
1066             ierr = PetscTableFind(cmap_i,cstart+cworkA[k]+1,&tt);CHKERRQ(ierr);
1067             if (tt) lens_i[j]++;
1068           }
1069           for (k=0; k<nzB; k++) {
1070             ierr = PetscTableFind(cmap_i,bmap[cworkB[k]]+1,&tt);CHKERRQ(ierr);
1071             if (tt) lens_i[j]++;
1072           }
1073 
1074 #else
1075           for (k=0; k<nzA; k++) {
1076             if (cmap_i[cstart + cworkA[k]]) lens_i[j]++;
1077           }
1078           for (k=0; k<nzB; k++) {
1079             if (cmap_i[bmap[cworkB[k]]]) lens_i[j]++;
1080           }
1081 #endif
1082         } else { /* allcolumns */
1083           lens_i[j] = nzA + nzB;
1084         }
1085 
1086 #if 0
1087         //------------------------------------------
1088           /* Get indices from matA and then from matB */
1089           PetscInt *a_i = a->i,*b_i=b->i;
1090           PetscInt row_loc = row - rstart; /* local row of matA and matB */
1091           PetscInt nzA    = a_i[row_loc+1] - a_i[row_loc];
1092           PetscInt nzB    = b_i[row_loc+1] - b_i[row_loc];
1093 
1094           printf("[%d] row %d, row_loc %d\n",rank,row,row_loc);
1095           ierr = MatGetRow_MPIBAIJ(C,row*bs,&ncols,&cols,0);CHKERRQ(ierr);
1096           if (nzA + nzB != ncols/bs) SETERRQ3(PETSC_COMM_SELF,0,"nzA %d + nzB %d != ncols/bs %d",nzA,nzB,ncols/bs);
1097 
1098           if (!allcolumns[i]) {
1099             //for (k=0; k<ncols; k++) {
1100             for (k=0; k<ncols; k+=bs) {
1101 #if defined(PETSC_USE_CTABLE)
1102               ierr = PetscTableFind(cmap_i,cols[k]/bs+1,&tcol);CHKERRQ(ierr);
1103 #else
1104               tcol = cmap_i[cols[k]];
1105 #endif
1106               if (tcol) lens_i[j]++;
1107             }
1108           } else { /* allcolumns */
1109             lens_i[j] = ncols;
1110           }
1111           ierr = MatRestoreRow_MPIBAIJ(C,row*bs,&ncols,&cols,0);CHKERRQ(ierr);
1112 #endif //------------------------------------------------------
1113         }
1114       }
1115     }
1116 
1117     /* Create row map: global row of C -> local row of submatrices */
1118 #if defined(PETSC_USE_CTABLE)
1119     for (i=0; i<ismax; i++) {
1120       //ierr   = PetscTableCreate(nrow[i]+1,C->rmap->N+1,&rmap[i]);CHKERRQ(ierr);
1121       ierr   = PetscTableCreate(nrow[i]+1,c->Mbs+1,&rmap[i]);CHKERRQ(ierr);
1122       irow_i = irow[i];
1123       jmax   = nrow[i];
1124       for (j=0; j<jmax; j++) {
1125       ierr = PetscTableAdd(rmap[i],irow_i[j]+1,j+1,INSERT_VALUES);CHKERRQ(ierr);
1126       }
1127     }
1128 #else
1129     for (i=0; i<ismax; i++) {
1130       ierr   = PetscCalloc1(c->Mbs,&rmap[i]);CHKERRQ(ierr);
1131       rmap_i = rmap[i];
1132       irow_i = irow[i];
1133       jmax   = nrow[i];
1134       for (j=0; j<jmax; j++) {
1135         rmap_i[irow_i[j]] = j;
1136       }
1137     }
1138 #endif
1139 
1140     /* Update lens from offproc data */
1141     {
1142       PetscInt *rbuf2_i,*rbuf3_i,*sbuf1_i;
1143 
1144       ierr    = MPI_Waitall(nrqs,r_waits3,r_status3);CHKERRQ(ierr);
1145       for (tmp2=0; tmp2<nrqs; tmp2++) {
1146         sbuf1_i = sbuf1[pa[tmp2]];
1147         jmax    = sbuf1_i[0];
1148         ct1     = 2*jmax+1;
1149         ct2     = 0;
1150         rbuf2_i = rbuf2[tmp2];
1151         rbuf3_i = rbuf3[tmp2];
1152         for (j=1; j<=jmax; j++) {
1153           is_no  = sbuf1_i[2*j-1];
1154           max1   = sbuf1_i[2*j];
1155           lens_i = lens[is_no];
1156           if (!allcolumns[is_no]) cmap_i = cmap[is_no];
1157           rmap_i = rmap[is_no];
1158           for (k=0; k<max1; k++,ct1++) {
1159 #if defined(PETSC_USE_CTABLE)
1160             ierr = PetscTableFind(rmap_i,sbuf1_i[ct1]+1,&row);CHKERRQ(ierr);
1161             row--;
1162             if (row < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"row not found in table");
1163 #else
1164             row = rmap_i[sbuf1_i[ct1]]; /* the val in the new matrix to be */
1165 #endif
1166             max2 = rbuf2_i[ct1];
1167             for (l=0; l<max2; l++,ct2++) {
1168               if (!allcolumns[is_no]) {
1169 #if defined(PETSC_USE_CTABLE)
1170                 ierr = PetscTableFind(cmap_i,rbuf3_i[ct2]+1,&tcol);CHKERRQ(ierr);
1171 #else
1172                 tcol = cmap_i[rbuf3_i[ct2]];
1173 #endif
1174                 if (tcol) lens_i[row]++;
1175               } else { /* allcolumns */
1176                 lens_i[row]++; /* lens_i[row] += max2 ? */
1177               }
1178             }
1179           }
1180         }
1181       }
1182     }
1183     ierr = PetscFree(r_waits3);CHKERRQ(ierr);
1184     if (nrqr) {ierr = MPI_Waitall(nrqr,s_waits3,s_status3);CHKERRQ(ierr);}
1185     ierr = PetscFree2(r_status3,s_status3);CHKERRQ(ierr);
1186     ierr = PetscFree(s_waits3);CHKERRQ(ierr);
1187 
1188     /* Create the submatrices */
1189     for (i=0; i<ismax; i++) {
1190       PetscInt bs_tmp;
1191       if (ijonly) bs_tmp = 1;
1192       else        bs_tmp = bs;
1193 
1194       ierr = MatCreate(PETSC_COMM_SELF,submats+i);CHKERRQ(ierr);
1195       ierr = MatSetSizes(submats[i],nrow[i]*bs_tmp,ncol[i]*bs_tmp,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
1196 
1197       ierr = MatSetType(submats[i],((PetscObject)A)->type_name);CHKERRQ(ierr);
1198       ierr = MatSeqBAIJSetPreallocation(submats[i],bs_tmp,0,lens[i]);CHKERRQ(ierr);
1199       ierr = MatSeqSBAIJSetPreallocation(submats[i],bs_tmp,0,lens[i]);CHKERRQ(ierr); /* this subroutine is used by SBAIJ routines */
1200 
1201       /* create struct Mat_SubMat and attached it to submat */
1202       ierr = PetscNew(&smat_i);CHKERRQ(ierr);
1203       subc = (Mat_SeqBAIJ*)submats[i]->data;
1204       subc->submatis1 = smat_i;
1205       smats[i]        = smat_i;
1206 
1207       smat_i->destroy          = submats[i]->ops->destroy;
1208       submats[i]->ops->destroy = MatDestroy_MPIBAIJ_MatGetSubmatrices;
1209       submats[i]->factortype   = C->factortype;
1210 
1211       smat_i->id          = i;
1212       smat_i->nrqs        = nrqs;
1213       smat_i->nrqr        = nrqr;
1214       smat_i->rbuf1       = rbuf1;
1215       smat_i->rbuf2       = rbuf2;
1216       smat_i->rbuf3       = rbuf3;
1217       smat_i->sbuf2       = sbuf2;
1218       smat_i->req_source2 = req_source2;
1219 
1220       smat_i->sbuf1       = sbuf1;
1221       smat_i->ptr         = ptr;
1222       smat_i->tmp         = tmp;
1223       smat_i->ctr         = ctr;
1224 
1225       smat_i->pa           = pa;
1226       smat_i->req_size     = req_size;
1227       smat_i->req_source1  = req_source1;
1228 
1229       smat_i->allcolumns  = allcolumns[i];
1230       smat_i->singleis    = PETSC_FALSE;
1231       smat_i->row2proc    = row2proc[i];
1232       smat_i->rmap        = rmap[i];
1233       smat_i->cmap        = cmap[i];
1234     }
1235 
1236     if (ismax) {ierr = PetscFree(lens[0]);CHKERRQ(ierr);}
1237     ierr = PetscFree(lens);CHKERRQ(ierr);
1238     ierr = PetscFree(sbuf_aj[0]);CHKERRQ(ierr);
1239     ierr = PetscFree(sbuf_aj);CHKERRQ(ierr);
1240 
1241   } /* endof scall == MAT_INITIAL_MATRIX */
1242 
1243   /* Post recv matrix values */
1244   ierr = PetscObjectGetNewTag((PetscObject)C,&tag4);CHKERRQ(ierr);
1245   ierr = PetscMalloc1(nrqs+1,&rbuf4);CHKERRQ(ierr);
1246   ierr = PetscMalloc1(nrqs+1,&r_waits4);CHKERRQ(ierr);
1247   ierr = PetscMalloc1(nrqs+1,&r_status4);CHKERRQ(ierr);
1248   ierr = PetscMalloc1(nrqr+1,&s_status4);CHKERRQ(ierr);
1249   for (i=0; i<nrqs; ++i) {
1250     ierr = PetscMalloc1(rbuf2[i][0]*bs2,&rbuf4[i]);CHKERRQ(ierr);
1251     ierr = MPI_Irecv(rbuf4[i],rbuf2[i][0]*bs2,MPIU_SCALAR,req_source2[i],tag4,comm,r_waits4+i);CHKERRQ(ierr);
1252   }
1253 
1254   /* Allocate sending buffers for a->a, and send them off */
1255   ierr = PetscMalloc1(nrqr+1,&sbuf_aa);CHKERRQ(ierr);
1256   for (i=0,j=0; i<nrqr; i++) j += req_size[i];
1257   //ierr = PetscMalloc1(j+1,&sbuf_aa[0]);CHKERRQ(ierr);
1258   //for (i=1; i<nrqr; i++) sbuf_aa[i] = sbuf_aa[i-1] + req_size[i-1];
1259   ierr = PetscMalloc1((j+1)*bs2,&sbuf_aa[0]);CHKERRQ(ierr);
1260   for (i=1; i<nrqr; i++) sbuf_aa[i] = sbuf_aa[i-1] + req_size[i-1]*bs2;
1261 
1262   ierr = PetscMalloc1(nrqr+1,&s_waits4);CHKERRQ(ierr);
1263   {
1264     //PetscInt    nzA,nzB,*a_i = a->i,*b_i = b->i, *cworkB;
1265     PetscInt    cstart = c->cstartbs,*bmap = c->garray;
1266     PetscInt    *b_j   = b->j;
1267     //PetscScalar *vworkA,*vworkB,*a_a = a->a,*b_a = b->a;
1268     PetscInt    imark;
1269 
1270     for (i=0; i<nrqr; i++) {
1271       rbuf1_i   = rbuf1[i];
1272       sbuf_aa_i = sbuf_aa[i];
1273       ct1       = 2*rbuf1_i[0]+1;
1274       ct2       = 0;
1275       for (j=1,max1=rbuf1_i[0]; j<=max1; j++) {
1276         kmax = rbuf1_i[2*j];
1277         for (k=0; k<kmax; k++,ct1++) {
1278           row    = rbuf1_i[ct1] - rstart;
1279           nzA    = a_i[row+1] - a_i[row];
1280           nzB    = b_i[row+1] - b_i[row];
1281           ncols  = nzA + nzB;
1282           cworkB = b_j + b_i[row];
1283           vworkA = a_a + a_i[row]*bs2;
1284           vworkB = b_a + b_i[row]*bs2;
1285           //if (rank==1) printf("i %d, j %d, k %d, ncols %d\n",i,j,k,ncols);
1286 
1287           /* load the column values for this row into vals*/
1288           vals = sbuf_aa_i+ct2*bs2;
1289           for (l=0; l<nzB; l++) {
1290             if ((bmap[cworkB[l]]) < cstart) {
1291               ierr = PetscMemcpy(vals+l*bs2,vworkB+l*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr);
1292             } else break;
1293           }
1294           imark = l;
1295           for (l=0; l<nzA; l++) {
1296             ierr = PetscMemcpy(vals+(imark+l)*bs2,vworkA+l*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr);//error in proc[1]???
1297           }
1298           for (l=imark; l<nzB; l++) {
1299             ierr = PetscMemcpy(vals+(nzA+l)*bs2,vworkB+l*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr);
1300           }
1301 #if 0
1302           vals = sbuf_aa_i+ct2;
1303 
1304           lwrite = 0;
1305           for (l=0; l<nzB; l++) {
1306             if ((bmap[cworkB[l]]) < cstart) vals[lwrite++] = vworkB[l];
1307           }
1308           for (l=0; l<nzA; l++) vals[lwrite++] = vworkA[l];
1309           for (l=0; l<nzB; l++) {
1310             if ((bmap[cworkB[l]]) >= cend) vals[lwrite++] = vworkB[l];
1311           }
1312 #endif
1313           ct2 += ncols;
1314         }
1315       }
1316       printf("[%d] send %d aa to [%d] \n",rank,req_size[i]*bs2,req_source1[i]);
1317       ierr = MPI_Isend(sbuf_aa_i,req_size[i]*bs2,MPIU_SCALAR,req_source1[i],tag4,comm,s_waits4+i);CHKERRQ(ierr);
1318     }
1319   }
1320   ierr = MPI_Barrier(comm);CHKERRQ(ierr);
1321 
1322   if (!ismax) {
1323     ierr = PetscFree(rbuf1[0]);CHKERRQ(ierr);
1324     ierr = PetscFree(rbuf1);CHKERRQ(ierr);
1325   }
1326 
1327   /* Assemble the matrices */
1328   /* First assemble the local rows */
1329   for (i=0; i<ismax; i++) {
1330     row2proc_i = row2proc[i];
1331     subc      = (Mat_SeqBAIJ*)submats[i]->data;
1332     imat_ilen = subc->ilen;
1333     imat_j    = subc->j;
1334     imat_i    = subc->i;
1335     imat_a    = subc->a;
1336 
1337     if (!allcolumns[i]) cmap_i = cmap[i];
1338     rmap_i = rmap[i];
1339     irow_i = irow[i];
1340     jmax   = nrow[i];
1341     for (j=0; j<jmax; j++) {
1342       row  = irow_i[j];
1343       proc = row2proc_i[j];
1344 
1345       if (proc == rank) {
1346 
1347         row    = row - rstart;
1348         nzA    = a_i[row+1] - a_i[row];
1349         nzB    = b_i[row+1] - b_i[row];
1350         cworkA = a_j + a_i[row];
1351         cworkB = b_j + b_i[row];
1352         if (!ijonly) {
1353           vworkA = a_a + a_i[row]*bs2;
1354           vworkB = b_a + b_i[row]*bs2;
1355         }
1356 #if defined(PETSC_USE_CTABLE)
1357         ierr = PetscTableFind(rmap_i,row+rstart+1,&row);CHKERRQ(ierr);
1358         row--;
1359         if (row < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"row not found in table");
1360 #else
1361         row = rmap_i[row + rstart];
1362 #endif
1363         mat_i = imat_i[row];
1364         if (!ijonly) mat_a = imat_a + mat_i*bs2;
1365         mat_j    = imat_j + mat_i;
1366         ilen_row = imat_ilen[row];
1367 
1368 #if 1
1369         /* load the column indices for this row into cols*/
1370         if (!allcolumns[i]) {
1371           for (l=0; l<nzB; l++) {
1372             if ((ctmp = bmap[cworkB[l]]) < cstart) {
1373 #if defined(PETSC_USE_CTABLE)
1374               ierr = PetscTableFind(cmap_i,ctmp+1,&tcol);CHKERRQ(ierr);
1375               if (tcol) {
1376 #else
1377               if ((tcol = cmap_i[ctmp])) {
1378 #endif
1379                 *mat_j++ = tcol - 1;
1380                 ierr     = PetscMemcpy(mat_a,vworkB+l*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr);
1381                 mat_a   += bs2;
1382                 ilen_row++;
1383               }
1384             } else break;
1385           }
1386           imark = l;
1387           for (l=0; l<nzA; l++) {
1388 #if defined(PETSC_USE_CTABLE)
1389             ierr = PetscTableFind(cmap_i,cstart+cworkA[l]+1,&tcol);CHKERRQ(ierr);
1390             if (tcol) {
1391 #else
1392             if ((tcol = cmap_i[cstart + cworkA[l]])) {
1393 #endif
1394               *mat_j++ = tcol - 1;
1395               if (!ijonly) {
1396                 ierr   = PetscMemcpy(mat_a,vworkA+l*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr);
1397                 mat_a += bs2;
1398               }
1399               ilen_row++;
1400             }
1401           }
1402           for (l=imark; l<nzB; l++) {
1403 #if defined(PETSC_USE_CTABLE)
1404             ierr = PetscTableFind(cmap_i,bmap[cworkB[l]]+1,&tcol);CHKERRQ(ierr);
1405             if (tcol) {
1406 #else
1407             if ((tcol = cmap_i[bmap[cworkB[l]]])) {
1408 #endif
1409               *mat_j++ = tcol - 1;
1410               if (!ijonly) {
1411                 ierr   = PetscMemcpy(mat_a,vworkB+l*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr);
1412                 mat_a += bs2;
1413               }
1414               ilen_row++;
1415             }
1416           }
1417         } else { /* allcolumns */
1418           for (l=0; l<nzB; l++) {
1419             if ((ctmp = bmap[cworkB[l]]) < cstart) {
1420               *mat_j++ = ctmp;
1421               ierr     = PetscMemcpy(mat_a,vworkB+l*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr);
1422               mat_a   += bs2;
1423               ilen_row++;
1424             } else break;
1425           }
1426           imark = l;
1427           for (l=0; l<nzA; l++) {
1428             *mat_j++ = cstart+cworkA[l];
1429             if (!ijonly) {
1430               ierr   = PetscMemcpy(mat_a,vworkA+l*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr);
1431               mat_a += bs2;
1432             }
1433             ilen_row++;
1434           }
1435           for (l=imark; l<nzB; l++) {
1436             *mat_j++ = bmap[cworkB[l]];
1437             if (!ijonly) {
1438               ierr   = PetscMemcpy(mat_a,vworkB+l*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr);
1439               mat_a += bs2;
1440             }
1441             ilen_row++;
1442           }
1443         }
1444             //imat_ilen[row] = ilen_row;
1445             //}
1446 #endif
1447 
1448 #if 0 // ------- old
1449         old_row = row;
1450 #if defined(PETSC_USE_CTABLE)
1451         ierr = PetscTableFind(rmap_i,row+1,&row);CHKERRQ(ierr);
1452         row--;
1453 #else
1454         row = rmap_i[row];
1455 #endif
1456         ilen_row = imat_ilen[row];
1457         ierr     = MatGetRow_MPIBAIJ(C,old_row,&ncols,&cols,&vals);CHKERRQ(ierr);
1458         mat_i    = imat_i[row];
1459         mat_a    = imat_a + mat_i;
1460         mat_j    = imat_j + mat_i;
1461         if (!allcolumns[i]) {
1462           for (k=0; k<ncols; k++) {
1463 #if defined(PETSC_USE_CTABLE)
1464             ierr = PetscTableFind(cmap_i,cols[k]+1,&tcol);CHKERRQ(ierr);
1465 #else
1466             tcol = cmap_i[cols[k]];
1467 #endif
1468             if (tcol) {
1469               *mat_j++ = tcol - 1;
1470               *mat_a++ = vals[k];
1471               ilen_row++;
1472             }
1473           }
1474         } else { /* allcolumns */
1475           for (k=0; k<ncols; k++) {
1476             *mat_j++ = cols[k];  /* global col index! */
1477             *mat_a++ = vals[k];
1478             ilen_row++;
1479           }
1480         }
1481         ierr = MatRestoreRow_MPIBAIJ(C,old_row,&ncols,&cols,&vals);CHKERRQ(ierr);
1482 #endif //------ end of old
1483         imat_ilen[row] = ilen_row;
1484       }
1485     }
1486   }
1487 
1488   /* Now assemble the off proc rows */
1489   ierr    = MPI_Waitall(nrqs,r_waits4,r_status4);CHKERRQ(ierr);
1490   for (tmp2=0; tmp2<nrqs; tmp2++) {
1491     sbuf1_i = sbuf1[pa[tmp2]];
1492     jmax    = sbuf1_i[0];
1493     ct1     = 2*jmax + 1;
1494     ct2     = 0;
1495     rbuf2_i = rbuf2[tmp2];
1496     rbuf3_i = rbuf3[tmp2];
1497     rbuf4_i = rbuf4[tmp2];
1498     for (j=1; j<=jmax; j++) {
1499       is_no     = sbuf1_i[2*j-1];
1500       rmap_i    = rmap[is_no];
1501       if (!allcolumns[is_no]) cmap_i = cmap[is_no];
1502       subc      = (Mat_SeqBAIJ*)submats[is_no]->data;
1503       imat_ilen = subc->ilen;
1504       imat_j    = subc->j;
1505       imat_i    = subc->i;
1506       imat_a    = subc->a;
1507       max1      = sbuf1_i[2*j];
1508       for (k=0; k<max1; k++,ct1++) {
1509         row = sbuf1_i[ct1];
1510 #if defined(PETSC_USE_CTABLE)
1511         ierr = PetscTableFind(rmap_i,row+1,&row);CHKERRQ(ierr);
1512         row--;
1513 #else
1514         row = rmap_i[row];
1515 #endif
1516         ilen  = imat_ilen[row];
1517         mat_i = imat_i[row];
1518         mat_a = imat_a + mat_i;
1519         mat_j = imat_j + mat_i;
1520         max2  = rbuf2_i[ct1];
1521         if (!allcolumns[is_no]) {
1522           for (l=0; l<max2; l++,ct2++) {
1523 #if defined(PETSC_USE_CTABLE)
1524             ierr = PetscTableFind(cmap_i,rbuf3_i[ct2]+1,&tcol);CHKERRQ(ierr);
1525 #else
1526             tcol = cmap_i[rbuf3_i[ct2]];
1527 #endif
1528             if (tcol) {
1529               *mat_j++ = tcol - 1;
1530               *mat_a++ = rbuf4_i[ct2];
1531               ilen++;
1532             }
1533           }
1534         } else { /* allcolumns */
1535           for (l=0; l<max2; l++,ct2++) {
1536             *mat_j++ = rbuf3_i[ct2]; /* same global column index of C */
1537             *mat_a++ = rbuf4_i[ct2];
1538             ilen++;
1539           }
1540         }
1541         imat_ilen[row] = ilen;
1542       }
1543     }
1544   }
1545 
1546   if (!iscsorted) { /* sort column indices of the rows */
1547     for (i=0; i<ismax; i++) {
1548       subc       = (Mat_SeqBAIJ*)submats[i]->data;
1549       imat_j    = subc->j;
1550       imat_i    = subc->i;
1551       imat_a    = subc->a;
1552       imat_ilen = subc->ilen;
1553 
1554       if (allcolumns[i]) continue;
1555       jmax = nrow[i];
1556       for (j=0; j<jmax; j++) {
1557         PetscInt ilen;
1558 
1559         mat_i = imat_i[j];
1560         mat_a = imat_a + mat_i;
1561         mat_j = imat_j + mat_i;
1562         ilen  = imat_ilen[j];
1563         ierr  = PetscSortIntWithScalarArray(ilen,mat_j,mat_a);CHKERRQ(ierr);
1564       }
1565     }
1566   }
1567 
1568   ierr = PetscFree(r_status4);CHKERRQ(ierr);
1569   ierr = PetscFree(r_waits4);CHKERRQ(ierr);
1570   if (nrqr) {ierr = MPI_Waitall(nrqr,s_waits4,s_status4);CHKERRQ(ierr);}
1571   ierr = PetscFree(s_waits4);CHKERRQ(ierr);
1572   ierr = PetscFree(s_status4);CHKERRQ(ierr);
1573 
1574   /* Restore the indices */
1575   for (i=0; i<ismax; i++) {
1576     ierr = ISRestoreIndices(isrow[i],irow+i);CHKERRQ(ierr);
1577     if (!allcolumns[i]) {
1578       ierr = ISRestoreIndices(iscol[i],icol+i);CHKERRQ(ierr);
1579     }
1580   }
1581 
1582   for (i=0; i<ismax; i++) {
1583     ierr = MatAssemblyBegin(submats[i],MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1584     ierr = MatAssemblyEnd(submats[i],MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1585   }
1586 
1587   /* Destroy allocated memory */
1588   if (!ismax) {
1589     ierr = PetscFree(pa);CHKERRQ(ierr);
1590 
1591     ierr = PetscFree4(sbuf1,ptr,tmp,ctr);CHKERRQ(ierr);
1592     for (i=0; i<nrqr; ++i) {
1593       ierr = PetscFree(sbuf2[i]);CHKERRQ(ierr);
1594     }
1595     for (i=0; i<nrqs; ++i) {
1596       ierr = PetscFree(rbuf3[i]);CHKERRQ(ierr);
1597     }
1598 
1599     ierr = PetscFree3(sbuf2,req_size,req_source1);CHKERRQ(ierr);
1600     ierr = PetscFree3(req_source2,rbuf2,rbuf3);CHKERRQ(ierr);
1601   }
1602 
1603   ierr = PetscFree(sbuf_aa[0]);CHKERRQ(ierr);
1604   ierr = PetscFree(sbuf_aa);CHKERRQ(ierr);
1605   ierr = PetscFree5(irow,icol,nrow,ncol,issorted);CHKERRQ(ierr);
1606 
1607   for (i=0; i<nrqs; ++i) {
1608     ierr = PetscFree(rbuf4[i]);CHKERRQ(ierr);
1609   }
1610   ierr = PetscFree(rbuf4);CHKERRQ(ierr);
1611 
1612   ierr = PetscFree5(smats,row2proc,cmap,rmap,allcolumns);CHKERRQ(ierr);
1613   PetscFunctionReturn(0);
1614 }
1615 
1616 //------------ endof new -------------
1617 
1618 PetscErrorCode MatGetSubMatrices_MPIBAIJ_local(Mat C,PetscInt ismax,const IS isrow[],const IS iscol[],MatReuse scall,PetscBool *allrows,PetscBool *allcolumns,Mat *submats)
1619 {
1620   Mat_MPIBAIJ    *c = (Mat_MPIBAIJ*)C->data;
1621   Mat            A  = c->A;
1622   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data,*b = (Mat_SeqBAIJ*)c->B->data,*mat;
1623   const PetscInt **irow,**icol,*irow_i;
1624   PetscInt       *nrow,*ncol,*w3,*w4,start;
1625   PetscErrorCode ierr;
1626   PetscMPIInt    size,tag0,tag1,tag2,tag3,*w1,*w2,nrqr,idex,end,proc;
1627   PetscInt       **sbuf1,**sbuf2,rank,i,j,k,l,ct1,ct2,**rbuf1,row;
1628   PetscInt       nrqs,msz,**ptr,*req_size,*ctr,*pa,*tmp,tcol;
1629   PetscInt       **rbuf3,*req_source,**sbuf_aj,**rbuf2,max1,max2;
1630   PetscInt       **lens,is_no,ncols,*cols,mat_i,*mat_j,tmp2,jmax;
1631   PetscInt       ctr_j,*sbuf1_j,*sbuf_aj_i,*rbuf1_i,kmax,*lens_i;
1632   PetscInt       bs     =C->rmap->bs,bs2=c->bs2,*a_j=a->j,*b_j=b->j,*cworkA,*cworkB;
1633   PetscInt       cstart = c->cstartbs,nzA,nzB,*a_i=a->i,*b_i=b->i,imark;
1634   PetscInt       *bmap  = c->garray,ctmp,rstart=c->rstartbs;
1635   MPI_Request    *s_waits1,*r_waits1,*s_waits2,*r_waits2,*r_waits3,*s_waits3;
1636   MPI_Status     *r_status1,*r_status2,*s_status1,*s_status3,*s_status2,*r_status3;
1637   MPI_Comm       comm;
1638   PetscBool      flag;
1639   PetscMPIInt    *onodes1,*olengths1;
1640   PetscBool      ijonly=c->ijonly; /* private flag indicates only matrix data structures are requested */
1641 
1642   /* variables below are used for the matrix numerical values - case of !ijonly */
1643   MPI_Request *r_waits4,*s_waits4;
1644   MPI_Status  *r_status4,*s_status4;
1645   MatScalar   **rbuf4,**sbuf_aa,*vals,*mat_a = NULL,*sbuf_aa_i,*vworkA = NULL,*vworkB = NULL;
1646   MatScalar   *a_a=a->a,*b_a=b->a;
1647 
1648 #if defined(PETSC_USE_CTABLE)
1649   PetscInt   tt;
1650   PetscTable *rmap,*cmap,rmap_i,cmap_i=NULL;
1651 #else
1652   PetscInt **cmap,*cmap_i=NULL,*rtable,*rmap_i,**rmap, Mbs = c->Mbs;
1653 #endif
1654 
1655   PetscFunctionBegin;
1656   ierr = PetscObjectGetComm((PetscObject)C,&comm);CHKERRQ(ierr);
1657   tag0 = ((PetscObject)C)->tag;
1658   size = c->size;
1659   rank = c->rank;
1660   if (!rank) printf("MatGetSubMatrices_MPIBAIJ scall %d, bs %d\n",scall,bs);
1661 
1662   /* Get some new tags to keep the communication clean */
1663   ierr = PetscObjectGetNewTag((PetscObject)C,&tag1);CHKERRQ(ierr);
1664   ierr = PetscObjectGetNewTag((PetscObject)C,&tag2);CHKERRQ(ierr);
1665   ierr = PetscObjectGetNewTag((PetscObject)C,&tag3);CHKERRQ(ierr);
1666 
1667 #if defined(PETSC_USE_CTABLE)
1668   ierr = PetscMalloc4(ismax,&irow,ismax,&icol,ismax,&nrow,ismax,&ncol);CHKERRQ(ierr);
1669 #else
1670   ierr = PetscMalloc5(ismax,&irow,ismax,&icol,ismax,&nrow,ismax,&ncol,Mbs+1,&rtable);CHKERRQ(ierr);
1671   /* Create hash table for the mapping :row -> proc*/
1672   for (i=0,j=0; i<size; i++) {
1673     jmax = C->rmap->range[i+1]/bs;
1674     for (; j<jmax; j++) rtable[j] = i;
1675   }
1676 #endif
1677 
1678   for (i=0; i<ismax; i++) {
1679     if (allrows[i]) {
1680       irow[i] = NULL;
1681       nrow[i] = C->rmap->N/bs;
1682     } else {
1683       ierr = ISGetIndices(isrow[i],&irow[i]);CHKERRQ(ierr);
1684       ierr = ISGetLocalSize(isrow[i],&nrow[i]);CHKERRQ(ierr);
1685     }
1686 
1687     if (allcolumns[i]) {
1688       icol[i] = NULL;
1689       ncol[i] = C->cmap->N/bs;
1690     } else {
1691       ierr = ISGetIndices(iscol[i],&icol[i]);CHKERRQ(ierr);
1692       ierr = ISGetLocalSize(iscol[i],&ncol[i]);CHKERRQ(ierr);
1693     }
1694   }
1695 
1696   /* evaluate communication - mesg to who,length of mesg,and buffer space
1697      required. Based on this, buffers are allocated, and data copied into them*/
1698   ierr = PetscCalloc4(size,&w1,size,&w2,size,&w3,size,&w4);CHKERRQ(ierr);
1699   for (i=0; i<ismax; i++) {
1700     ierr   = PetscMemzero(w4,size*sizeof(PetscInt));CHKERRQ(ierr); /* initialise work vector*/
1701     jmax   = nrow[i];
1702     irow_i = irow[i];
1703     for (j=0; j<jmax; j++) {
1704       if (allrows[i]) row = j;
1705       else row = irow_i[j];
1706 
1707 #if defined(PETSC_USE_CTABLE)
1708       ierr = PetscGetProc(row,size,c->rangebs,&proc);CHKERRQ(ierr);
1709 #else
1710       proc = rtable[row];
1711 #endif
1712       w4[proc]++;
1713     }
1714     for (j=0; j<size; j++) {
1715       if (w4[j]) { w1[j] += w4[j];  w3[j]++;}
1716     }
1717   }
1718 
1719   nrqs     = 0;              /* no of outgoing messages */
1720   msz      = 0;              /* total mesg length for all proc */
1721   w1[rank] = 0;              /* no mesg sent to intself */
1722   w3[rank] = 0;
1723   for (i=0; i<size; i++) {
1724     if (w1[i])  { w2[i] = 1; nrqs++;} /* there exists a message to proc i */
1725   }
1726   ierr = PetscMalloc1(nrqs+1,&pa);CHKERRQ(ierr); /*(proc -array)*/
1727   for (i=0,j=0; i<size; i++) {
1728     if (w1[i]) { pa[j] = i; j++; }
1729   }
1730 
1731   /* Each message would have a header = 1 + 2*(no of IS) + data */
1732   for (i=0; i<nrqs; i++) {
1733     j     = pa[i];
1734     w1[j] += w2[j] + 2* w3[j];
1735     msz   += w1[j];
1736   }
1737 
1738   /* Determine the number of messages to expect, their lengths, from from-ids */
1739   ierr = PetscGatherNumberOfMessages(comm,w2,w1,&nrqr);CHKERRQ(ierr);
1740   ierr = PetscGatherMessageLengths(comm,nrqs,nrqr,w1,&onodes1,&olengths1);CHKERRQ(ierr);
1741 
1742   /* Now post the Irecvs corresponding to these messages */
1743   ierr = PetscPostIrecvInt(comm,tag0,nrqr,onodes1,olengths1,&rbuf1,&r_waits1);CHKERRQ(ierr);
1744 
1745   ierr = PetscFree(onodes1);CHKERRQ(ierr);
1746   ierr = PetscFree(olengths1);CHKERRQ(ierr);
1747 
1748   /* Allocate Memory for outgoing messages */
1749   ierr = PetscMalloc4(size,&sbuf1,size,&ptr,2*msz,&tmp,size,&ctr);CHKERRQ(ierr);
1750   ierr = PetscMemzero(sbuf1,size*sizeof(PetscInt*));CHKERRQ(ierr);
1751   ierr = PetscMemzero(ptr,size*sizeof(PetscInt*));CHKERRQ(ierr);
1752   {
1753     PetscInt *iptr = tmp,ict = 0;
1754     for (i=0; i<nrqs; i++) {
1755       j        = pa[i];
1756       iptr    += ict;
1757       sbuf1[j] = iptr;
1758       ict      = w1[j];
1759     }
1760   }
1761 
1762   /* Form the outgoing messages */
1763   /* Initialise the header space */
1764   for (i=0; i<nrqs; i++) {
1765     j           = pa[i];
1766     sbuf1[j][0] = 0;
1767     ierr        = PetscMemzero(sbuf1[j]+1,2*w3[j]*sizeof(PetscInt));CHKERRQ(ierr);
1768     ptr[j]      = sbuf1[j] + 2*w3[j] + 1;
1769   }
1770 
1771   /* Parse the isrow and copy data into outbuf */
1772   for (i=0; i<ismax; i++) {
1773     ierr   = PetscMemzero(ctr,size*sizeof(PetscInt));CHKERRQ(ierr);
1774     irow_i = irow[i];
1775     jmax   = nrow[i];
1776     for (j=0; j<jmax; j++) {  /* parse the indices of each IS */
1777       if (allrows[i]) row = j;
1778       else row = irow_i[j];
1779 
1780 #if defined(PETSC_USE_CTABLE)
1781       ierr = PetscGetProc(row,size,c->rangebs,&proc);CHKERRQ(ierr);
1782 #else
1783       proc = rtable[row];
1784 #endif
1785       if (proc != rank) { /* copy to the outgoing buf*/
1786         ctr[proc]++;
1787         *ptr[proc] = row;
1788         ptr[proc]++;
1789       }
1790     }
1791     /* Update the headers for the current IS */
1792     for (j=0; j<size; j++) { /* Can Optimise this loop too */
1793       if ((ctr_j = ctr[j])) {
1794         sbuf1_j        = sbuf1[j];
1795         k              = ++sbuf1_j[0];
1796         sbuf1_j[2*k]   = ctr_j;
1797         sbuf1_j[2*k-1] = i;
1798       }
1799     }
1800   }
1801 
1802   /*  Now  post the sends */
1803   ierr = PetscMalloc1(nrqs+1,&s_waits1);CHKERRQ(ierr);
1804   for (i=0; i<nrqs; ++i) {
1805     j    = pa[i];
1806     ierr = MPI_Isend(sbuf1[j],w1[j],MPIU_INT,j,tag0,comm,s_waits1+i);CHKERRQ(ierr);
1807   }
1808 
1809   /* Post Recieves to capture the buffer size */
1810   ierr     = PetscMalloc1(nrqs+1,&r_waits2);CHKERRQ(ierr);
1811   ierr     = PetscMalloc1(nrqs+1,&rbuf2);CHKERRQ(ierr);
1812   rbuf2[0] = tmp + msz;
1813   for (i=1; i<nrqs; ++i) {
1814     rbuf2[i] = rbuf2[i-1]+w1[pa[i-1]];
1815   }
1816   for (i=0; i<nrqs; ++i) {
1817     j        = pa[i];
1818     ierr     = MPI_Irecv(rbuf2[i],w1[j],MPIU_INT,j,tag1,comm,r_waits2+i);CHKERRQ(ierr);
1819   }
1820 
1821   /* Send to other procs the buf size they should allocate */
1822 
1823   /* Receive messages*/
1824   ierr = PetscMalloc1(nrqr+1,&s_waits2);CHKERRQ(ierr);
1825   ierr = PetscMalloc1(nrqr+1,&r_status1);CHKERRQ(ierr);
1826   ierr = PetscMalloc3(nrqr+1,&sbuf2,nrqr,&req_size,nrqr,&req_source);CHKERRQ(ierr);
1827   {
1828     Mat_SeqBAIJ *sA  = (Mat_SeqBAIJ*)c->A->data,*sB = (Mat_SeqBAIJ*)c->B->data;
1829     PetscInt    *sAi = sA->i,*sBi = sB->i,id,*sbuf2_i;
1830 
1831     for (i=0; i<nrqr; ++i) {
1832       ierr = MPI_Waitany(nrqr,r_waits1,&idex,r_status1+i);CHKERRQ(ierr);
1833 
1834       req_size[idex] = 0;
1835       rbuf1_i        = rbuf1[idex];
1836       start          = 2*rbuf1_i[0] + 1;
1837       ierr           = MPI_Get_count(r_status1+i,MPIU_INT,&end);CHKERRQ(ierr);
1838       ierr           = PetscMalloc1(end,&sbuf2[idex]);CHKERRQ(ierr);
1839       sbuf2_i        = sbuf2[idex];
1840       for (j=start; j<end; j++) {
1841         id              = rbuf1_i[j] - rstart;
1842         ncols           = sAi[id+1] - sAi[id] + sBi[id+1] - sBi[id];
1843         sbuf2_i[j]      = ncols;
1844         req_size[idex] += ncols;
1845       }
1846       req_source[idex] = r_status1[i].MPI_SOURCE;
1847       /* form the header */
1848       sbuf2_i[0] = req_size[idex];
1849       for (j=1; j<start; j++) sbuf2_i[j] = rbuf1_i[j];
1850       ierr = MPI_Isend(sbuf2_i,end,MPIU_INT,req_source[idex],tag1,comm,s_waits2+i);CHKERRQ(ierr);
1851     }
1852   }
1853   ierr = PetscFree(r_status1);CHKERRQ(ierr);
1854   ierr = PetscFree(r_waits1);CHKERRQ(ierr);
1855 
1856   /*  recv buffer sizes */
1857   /* Receive messages*/
1858   ierr = PetscMalloc1(nrqs+1,&rbuf3);CHKERRQ(ierr);
1859   ierr = PetscMalloc1(nrqs+1,&r_waits3);CHKERRQ(ierr);
1860   ierr = PetscMalloc1(nrqs+1,&r_status2);CHKERRQ(ierr);
1861   if (!ijonly) {
1862     ierr = PetscMalloc1(nrqs+1,&rbuf4);CHKERRQ(ierr);
1863     ierr = PetscMalloc1(nrqs+1,&r_waits4);CHKERRQ(ierr);
1864   }
1865 
1866   for (i=0; i<nrqs; ++i) {
1867     ierr = MPI_Waitany(nrqs,r_waits2,&idex,r_status2+i);CHKERRQ(ierr);
1868     ierr = PetscMalloc1(rbuf2[idex][0],&rbuf3[idex]);CHKERRQ(ierr);
1869     ierr = MPI_Irecv(rbuf3[idex],rbuf2[idex][0],MPIU_INT,r_status2[i].MPI_SOURCE,tag2,comm,r_waits3+idex);CHKERRQ(ierr);
1870     if (!ijonly) {
1871       ierr = PetscMalloc1(rbuf2[idex][0]*bs2,&rbuf4[idex]);CHKERRQ(ierr);
1872       ierr = MPI_Irecv(rbuf4[idex],rbuf2[idex][0]*bs2,MPIU_MATSCALAR,r_status2[i].MPI_SOURCE,tag3,comm,r_waits4+idex);CHKERRQ(ierr);
1873     }
1874   }
1875   ierr = PetscFree(r_status2);CHKERRQ(ierr);
1876   ierr = PetscFree(r_waits2);CHKERRQ(ierr);
1877 
1878   /* Wait on sends1 and sends2 */
1879   ierr = PetscMalloc1(nrqs+1,&s_status1);CHKERRQ(ierr);
1880   ierr = PetscMalloc1(nrqr+1,&s_status2);CHKERRQ(ierr);
1881 
1882   if (nrqs) {ierr = MPI_Waitall(nrqs,s_waits1,s_status1);CHKERRQ(ierr);}
1883   if (nrqr) {ierr = MPI_Waitall(nrqr,s_waits2,s_status2);CHKERRQ(ierr);}
1884   ierr = PetscFree(s_status1);CHKERRQ(ierr);
1885   ierr = PetscFree(s_status2);CHKERRQ(ierr);
1886   ierr = PetscFree(s_waits1);CHKERRQ(ierr);
1887   ierr = PetscFree(s_waits2);CHKERRQ(ierr);
1888 
1889   /* Now allocate buffers for a->j, and send them off */
1890   ierr = PetscMalloc1(nrqr+1,&sbuf_aj);CHKERRQ(ierr);
1891   for (i=0,j=0; i<nrqr; i++) j += req_size[i];
1892   ierr = PetscMalloc1(j+1,&sbuf_aj[0]);CHKERRQ(ierr);
1893   for (i=1; i<nrqr; i++) sbuf_aj[i] = sbuf_aj[i-1] + req_size[i-1];
1894 
1895   ierr = PetscMalloc1(nrqr+1,&s_waits3);CHKERRQ(ierr);
1896   {
1897     for (i=0; i<nrqr; i++) {
1898       rbuf1_i   = rbuf1[i];
1899       sbuf_aj_i = sbuf_aj[i];
1900       ct1       = 2*rbuf1_i[0] + 1;
1901       ct2       = 0;
1902       for (j=1,max1=rbuf1_i[0]; j<=max1; j++) {
1903         kmax = rbuf1[i][2*j];
1904         for (k=0; k<kmax; k++,ct1++) {
1905           row    = rbuf1_i[ct1] - rstart;
1906           nzA    = a_i[row+1] - a_i[row];
1907           nzB    = b_i[row+1] - b_i[row];
1908           ncols  = nzA + nzB;
1909           cworkA = a_j + a_i[row];
1910           cworkB = b_j + b_i[row];
1911 
1912           /* load the column indices for this row into cols*/
1913           cols = sbuf_aj_i + ct2;
1914           for (l=0; l<nzB; l++) {
1915             if ((ctmp = bmap[cworkB[l]]) < cstart) cols[l] = ctmp;
1916             else break;
1917           }
1918           imark = l;
1919           for (l=0; l<nzA; l++)   cols[imark+l] = cstart + cworkA[l];
1920           for (l=imark; l<nzB; l++) cols[nzA+l] = bmap[cworkB[l]];
1921           ct2 += ncols;
1922         }
1923       }
1924       ierr = MPI_Isend(sbuf_aj_i,req_size[i],MPIU_INT,req_source[i],tag2,comm,s_waits3+i);CHKERRQ(ierr);
1925     }
1926   }
1927   ierr = PetscMalloc1(nrqs+1,&r_status3);CHKERRQ(ierr);
1928   ierr = PetscMalloc1(nrqr+1,&s_status3);CHKERRQ(ierr);
1929 
1930   /* Allocate buffers for a->a, and send them off */
1931   if (!ijonly) {
1932     ierr = PetscMalloc1(nrqr+1,&sbuf_aa);CHKERRQ(ierr);
1933     for (i=0,j=0; i<nrqr; i++) j += req_size[i];
1934     ierr = PetscMalloc1((j+1)*bs2,&sbuf_aa[0]);CHKERRQ(ierr);
1935     for (i=1; i<nrqr; i++) sbuf_aa[i] = sbuf_aa[i-1] + req_size[i-1]*bs2;
1936 
1937     ierr = PetscMalloc1(nrqr+1,&s_waits4);CHKERRQ(ierr);
1938     {
1939       for (i=0; i<nrqr; i++) {
1940         rbuf1_i   = rbuf1[i];
1941         sbuf_aa_i = sbuf_aa[i];
1942         ct1       = 2*rbuf1_i[0]+1;
1943         ct2       = 0;
1944         for (j=1,max1=rbuf1_i[0]; j<=max1; j++) {
1945           kmax = rbuf1_i[2*j];
1946           for (k=0; k<kmax; k++,ct1++) {
1947             row    = rbuf1_i[ct1] - rstart;
1948             nzA    = a_i[row+1] - a_i[row];
1949             nzB    = b_i[row+1] - b_i[row];
1950             ncols  = nzA + nzB;
1951             cworkB = b_j + b_i[row];
1952             vworkA = a_a + a_i[row]*bs2;
1953             vworkB = b_a + b_i[row]*bs2;
1954 
1955             /* load the column values for this row into vals*/
1956             vals = sbuf_aa_i+ct2*bs2;
1957             for (l=0; l<nzB; l++) {
1958               if ((bmap[cworkB[l]]) < cstart) {
1959                 ierr = PetscMemcpy(vals+l*bs2,vworkB+l*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr);
1960               } else break;
1961             }
1962             imark = l;
1963             for (l=0; l<nzA; l++) {
1964               ierr = PetscMemcpy(vals+(imark+l)*bs2,vworkA+l*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr);
1965             }
1966             for (l=imark; l<nzB; l++) {
1967               ierr = PetscMemcpy(vals+(nzA+l)*bs2,vworkB+l*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr);
1968             }
1969             ct2 += ncols;
1970           }
1971         }
1972         ierr = MPI_Isend(sbuf_aa_i,req_size[i]*bs2,MPIU_MATSCALAR,req_source[i],tag3,comm,s_waits4+i);CHKERRQ(ierr);
1973       }
1974     }
1975     ierr = PetscMalloc1(nrqs+1,&r_status4);CHKERRQ(ierr);
1976     ierr = PetscMalloc1(nrqr+1,&s_status4);CHKERRQ(ierr);
1977   }
1978   ierr = PetscFree(rbuf1[0]);CHKERRQ(ierr);
1979   ierr = PetscFree(rbuf1);CHKERRQ(ierr);
1980 
1981   /* Form the matrix */
1982   /* create col map: global col of C -> local col of submatrices */
1983   {
1984     const PetscInt *icol_i;
1985 #if defined(PETSC_USE_CTABLE)
1986     ierr = PetscMalloc1(1+ismax,&cmap);CHKERRQ(ierr);
1987     for (i=0; i<ismax; i++) {
1988       if (!allcolumns[i]) {
1989         ierr   = PetscTableCreate(ncol[i]+1,c->Nbs+1,&cmap[i]);CHKERRQ(ierr);
1990         jmax   = ncol[i];
1991         icol_i = icol[i];
1992         cmap_i = cmap[i];
1993         for (j=0; j<jmax; j++) {
1994           ierr = PetscTableAdd(cmap_i,icol_i[j]+1,j+1,INSERT_VALUES);CHKERRQ(ierr);
1995         }
1996       } else {
1997         cmap[i] = NULL;
1998       }
1999     }
2000 #else
2001     ierr = PetscMalloc1(ismax,&cmap);CHKERRQ(ierr);
2002     for (i=0; i<ismax; i++) {
2003       if (!allcolumns[i]) {
2004         ierr   = PetscCalloc1(c->Nbs,&cmap[i]);CHKERRQ(ierr);
2005         jmax   = ncol[i];
2006         icol_i = icol[i];
2007         cmap_i = cmap[i];
2008         for (j=0; j<jmax; j++) {
2009           cmap_i[icol_i[j]] = j+1;
2010         }
2011       } else { /* allcolumns[i] */
2012         cmap[i] = NULL;
2013       }
2014     }
2015 #endif
2016   }
2017 
2018   /* Create lens which is required for MatCreate... */
2019   for (i=0,j=0; i<ismax; i++) j += nrow[i];
2020   ierr    = PetscMalloc((1+ismax)*sizeof(PetscInt*)+ j*sizeof(PetscInt),&lens);CHKERRQ(ierr);
2021   lens[0] = (PetscInt*)(lens + ismax);
2022   ierr    = PetscMemzero(lens[0],j*sizeof(PetscInt));CHKERRQ(ierr);
2023   for (i=1; i<ismax; i++) lens[i] = lens[i-1] + nrow[i-1];
2024 
2025   /* Update lens from local data */
2026   for (i=0; i<ismax; i++) {
2027     jmax = nrow[i];
2028     if (!allcolumns[i]) cmap_i = cmap[i];
2029     irow_i = irow[i];
2030     lens_i = lens[i];
2031     for (j=0; j<jmax; j++) {
2032       if (allrows[i]) row = j;
2033       else row = irow_i[j];
2034 
2035 #if defined(PETSC_USE_CTABLE)
2036       ierr = PetscGetProc(row,size,c->rangebs,&proc);CHKERRQ(ierr);
2037 #else
2038       proc = rtable[row];
2039 #endif
2040       if (proc == rank) {
2041         /* Get indices from matA and then from matB */
2042         row    = row - rstart;
2043         nzA    = a_i[row+1] - a_i[row];
2044         nzB    = b_i[row+1] - b_i[row];
2045         cworkA =  a_j + a_i[row];
2046         cworkB = b_j + b_i[row];
2047         if (!allcolumns[i]) {
2048 #if defined(PETSC_USE_CTABLE)
2049           for (k=0; k<nzA; k++) {
2050             ierr = PetscTableFind(cmap_i,cstart+cworkA[k]+1,&tt);CHKERRQ(ierr);
2051             if (tt) lens_i[j]++;
2052           }
2053           for (k=0; k<nzB; k++) {
2054             ierr = PetscTableFind(cmap_i,bmap[cworkB[k]]+1,&tt);CHKERRQ(ierr);
2055             if (tt) lens_i[j]++;
2056           }
2057 
2058 #else
2059           for (k=0; k<nzA; k++) {
2060             if (cmap_i[cstart + cworkA[k]]) lens_i[j]++;
2061           }
2062           for (k=0; k<nzB; k++) {
2063             if (cmap_i[bmap[cworkB[k]]]) lens_i[j]++;
2064           }
2065 #endif
2066         } else { /* allcolumns */
2067           lens_i[j] = nzA + nzB;
2068         }
2069       }
2070     }
2071   }
2072 #if defined(PETSC_USE_CTABLE)
2073   /* Create row map*/
2074   ierr = PetscMalloc1(1+ismax,&rmap);CHKERRQ(ierr);
2075   for (i=0; i<ismax; i++) {
2076     ierr = PetscTableCreate(nrow[i]+1,c->Mbs+1,&rmap[i]);CHKERRQ(ierr);
2077   }
2078 #else
2079   /* Create row map*/
2080   ierr    = PetscMalloc((1+ismax)*sizeof(PetscInt*)+ ismax*Mbs*sizeof(PetscInt),&rmap);CHKERRQ(ierr);
2081   rmap[0] = (PetscInt*)(rmap + ismax);
2082   ierr    = PetscMemzero(rmap[0],ismax*Mbs*sizeof(PetscInt));CHKERRQ(ierr);
2083   for (i=1; i<ismax; i++) rmap[i] = rmap[i-1] + Mbs;
2084 #endif
2085   for (i=0; i<ismax; i++) {
2086     irow_i = irow[i];
2087     jmax   = nrow[i];
2088 #if defined(PETSC_USE_CTABLE)
2089     rmap_i = rmap[i];
2090     for (j=0; j<jmax; j++) {
2091       if (allrows[i]) {
2092         ierr = PetscTableAdd(rmap_i,j+1,j+1,INSERT_VALUES);CHKERRQ(ierr);
2093       } else {
2094         ierr = PetscTableAdd(rmap_i,irow_i[j]+1,j+1,INSERT_VALUES);CHKERRQ(ierr);
2095       }
2096     }
2097 #else
2098     rmap_i = rmap[i];
2099     for (j=0; j<jmax; j++) {
2100       if (allrows[i]) rmap_i[j] = j;
2101       else rmap_i[irow_i[j]] = j;
2102     }
2103 #endif
2104   }
2105 
2106   /* Update lens from offproc data */
2107   {
2108     PetscInt    *rbuf2_i,*rbuf3_i,*sbuf1_i;
2109     PetscMPIInt ii;
2110 
2111     for (tmp2=0; tmp2<nrqs; tmp2++) {
2112       ierr    = MPI_Waitany(nrqs,r_waits3,&ii,r_status3+tmp2);CHKERRQ(ierr);
2113       idex    = pa[ii];
2114       sbuf1_i = sbuf1[idex];
2115       jmax    = sbuf1_i[0];
2116       ct1     = 2*jmax+1;
2117       ct2     = 0;
2118       rbuf2_i = rbuf2[ii];
2119       rbuf3_i = rbuf3[ii];
2120       for (j=1; j<=jmax; j++) {
2121         is_no  = sbuf1_i[2*j-1];
2122         max1   = sbuf1_i[2*j];
2123         lens_i = lens[is_no];
2124         if (!allcolumns[is_no]) cmap_i = cmap[is_no];
2125         rmap_i = rmap[is_no];
2126         for (k=0; k<max1; k++,ct1++) {
2127 #if defined(PETSC_USE_CTABLE)
2128           ierr = PetscTableFind(rmap_i,sbuf1_i[ct1]+1,&row);CHKERRQ(ierr);
2129           row--;
2130           if (row < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"row not found in table");
2131 #else
2132           row = rmap_i[sbuf1_i[ct1]];  /* the val in the new matrix to be */
2133 #endif
2134           max2 = rbuf2_i[ct1];
2135           for (l=0; l<max2; l++,ct2++) {
2136             if (!allcolumns[is_no]) {
2137 #if defined(PETSC_USE_CTABLE)
2138               ierr = PetscTableFind(cmap_i,rbuf3_i[ct2]+1,&tt);CHKERRQ(ierr);
2139               if (tt) lens_i[row]++;
2140 #else
2141               if (cmap_i[rbuf3_i[ct2]]) lens_i[row]++;
2142 #endif
2143             } else { /* allcolumns */
2144               lens_i[row]++;
2145             }
2146           }
2147         }
2148       }
2149     }
2150   }
2151   ierr = PetscFree(r_status3);CHKERRQ(ierr);
2152   ierr = PetscFree(r_waits3);CHKERRQ(ierr);
2153   if (nrqr) {ierr = MPI_Waitall(nrqr,s_waits3,s_status3);CHKERRQ(ierr);}
2154   ierr = PetscFree(s_status3);CHKERRQ(ierr);
2155   ierr = PetscFree(s_waits3);CHKERRQ(ierr);
2156 
2157   /* Create the submatrices */
2158   if (scall == MAT_REUSE_MATRIX) {
2159     if (ijonly) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP," MAT_REUSE_MATRIX and ijonly is not supported yet");
2160     /*
2161         Assumes new rows are same length as the old rows, hence bug!
2162     */
2163     for (i=0; i<ismax; i++) {
2164       mat = (Mat_SeqBAIJ*)(submats[i]->data);
2165       if ((mat->mbs != nrow[i]) || (mat->nbs != ncol[i] || C->rmap->bs != bs)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
2166       ierr = PetscMemcmp(mat->ilen,lens[i],mat->mbs *sizeof(PetscInt),&flag);CHKERRQ(ierr);
2167       if (!flag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Cannot reuse matrix. wrong no of nonzeros");
2168       /* Initial matrix as if empty */
2169       ierr = PetscMemzero(mat->ilen,mat->mbs*sizeof(PetscInt));CHKERRQ(ierr);
2170 
2171       submats[i]->factortype = C->factortype;
2172     }
2173   } else {
2174     PetscInt bs_tmp;
2175     if (ijonly) bs_tmp = 1;
2176     else        bs_tmp = bs;
2177     for (i=0; i<ismax; i++) {
2178       ierr = MatCreate(PETSC_COMM_SELF,submats+i);CHKERRQ(ierr);
2179       ierr = MatSetSizes(submats[i],nrow[i]*bs_tmp,ncol[i]*bs_tmp,nrow[i]*bs_tmp,ncol[i]*bs_tmp);CHKERRQ(ierr);
2180       ierr = MatSetType(submats[i],((PetscObject)A)->type_name);CHKERRQ(ierr);
2181       ierr = MatSeqBAIJSetPreallocation(submats[i],bs_tmp,0,lens[i]);CHKERRQ(ierr);
2182       ierr = MatSeqSBAIJSetPreallocation(submats[i],bs_tmp,0,lens[i]);CHKERRQ(ierr); /* this subroutine is used by SBAIJ routines */
2183     }
2184   }
2185 
2186   /* Assemble the matrices */
2187   /* First assemble the local rows */
2188   {
2189     PetscInt  ilen_row,*imat_ilen,*imat_j,*imat_i;
2190     MatScalar *imat_a = NULL;
2191 
2192     for (i=0; i<ismax; i++) {
2193       mat       = (Mat_SeqBAIJ*)submats[i]->data;
2194       imat_ilen = mat->ilen;
2195       imat_j    = mat->j;
2196       imat_i    = mat->i;
2197       if (!ijonly) imat_a = mat->a;
2198       if (!allcolumns[i]) cmap_i = cmap[i];
2199       rmap_i = rmap[i];
2200       irow_i = irow[i];
2201       jmax   = nrow[i];
2202       for (j=0; j<jmax; j++) {
2203         if (allrows[i]) row = j;
2204         else row = irow_i[j];
2205 
2206 #if defined(PETSC_USE_CTABLE)
2207         ierr = PetscGetProc(row,size,c->rangebs,&proc);CHKERRQ(ierr);
2208 #else
2209         proc = rtable[row];
2210 #endif
2211         if (proc == rank) {
2212           row    = row - rstart;
2213           nzA    = a_i[row+1] - a_i[row];
2214           nzB    = b_i[row+1] - b_i[row];
2215           cworkA = a_j + a_i[row];
2216           cworkB = b_j + b_i[row];
2217           if (!ijonly) {
2218             vworkA = a_a + a_i[row]*bs2;
2219             vworkB = b_a + b_i[row]*bs2;
2220           }
2221 #if defined(PETSC_USE_CTABLE)
2222           ierr = PetscTableFind(rmap_i,row+rstart+1,&row);CHKERRQ(ierr);
2223           row--;
2224           if (row < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"row not found in table");
2225 #else
2226           row = rmap_i[row + rstart];
2227 #endif
2228           mat_i = imat_i[row];
2229           if (!ijonly) mat_a = imat_a + mat_i*bs2;
2230           mat_j    = imat_j + mat_i;
2231           ilen_row = imat_ilen[row];
2232 
2233           /* load the column indices for this row into cols*/
2234           if (!allcolumns[i]) {
2235             for (l=0; l<nzB; l++) {
2236               if ((ctmp = bmap[cworkB[l]]) < cstart) {
2237 #if defined(PETSC_USE_CTABLE)
2238                 ierr = PetscTableFind(cmap_i,ctmp+1,&tcol);CHKERRQ(ierr);
2239                 if (tcol) {
2240 #else
2241                 if ((tcol = cmap_i[ctmp])) {
2242 #endif
2243                   *mat_j++ = tcol - 1;
2244                   ierr     = PetscMemcpy(mat_a,vworkB+l*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr);
2245                   mat_a   += bs2;
2246                   ilen_row++;
2247                 }
2248               } else break;
2249             }
2250             imark = l;
2251             for (l=0; l<nzA; l++) {
2252 #if defined(PETSC_USE_CTABLE)
2253               ierr = PetscTableFind(cmap_i,cstart+cworkA[l]+1,&tcol);CHKERRQ(ierr);
2254               if (tcol) {
2255 #else
2256               if ((tcol = cmap_i[cstart + cworkA[l]])) {
2257 #endif
2258                 *mat_j++ = tcol - 1;
2259                 if (!ijonly) {
2260                   ierr   = PetscMemcpy(mat_a,vworkA+l*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr);
2261                   mat_a += bs2;
2262                 }
2263                 ilen_row++;
2264               }
2265             }
2266             for (l=imark; l<nzB; l++) {
2267 #if defined(PETSC_USE_CTABLE)
2268               ierr = PetscTableFind(cmap_i,bmap[cworkB[l]]+1,&tcol);CHKERRQ(ierr);
2269               if (tcol) {
2270 #else
2271               if ((tcol = cmap_i[bmap[cworkB[l]]])) {
2272 #endif
2273                 *mat_j++ = tcol - 1;
2274                 if (!ijonly) {
2275                   ierr   = PetscMemcpy(mat_a,vworkB+l*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr);
2276                   mat_a += bs2;
2277                 }
2278                 ilen_row++;
2279               }
2280             }
2281           } else { /* allcolumns */
2282             for (l=0; l<nzB; l++) {
2283               if ((ctmp = bmap[cworkB[l]]) < cstart) {
2284                 *mat_j++ = ctmp;
2285                 ierr     = PetscMemcpy(mat_a,vworkB+l*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr);
2286                 mat_a   += bs2;
2287                 ilen_row++;
2288               } else break;
2289             }
2290             imark = l;
2291             for (l=0; l<nzA; l++) {
2292               *mat_j++ = cstart+cworkA[l];
2293               if (!ijonly) {
2294                 ierr   = PetscMemcpy(mat_a,vworkA+l*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr);
2295                 mat_a += bs2;
2296               }
2297               ilen_row++;
2298             }
2299             for (l=imark; l<nzB; l++) {
2300               *mat_j++ = bmap[cworkB[l]];
2301               if (!ijonly) {
2302                 ierr   = PetscMemcpy(mat_a,vworkB+l*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr);
2303                 mat_a += bs2;
2304               }
2305               ilen_row++;
2306             }
2307           }
2308           imat_ilen[row] = ilen_row;
2309         }
2310       }
2311     }
2312   }
2313 
2314   /*   Now assemble the off proc rows*/
2315   {
2316     PetscInt    *sbuf1_i,*rbuf2_i,*rbuf3_i,*imat_ilen,ilen;
2317     PetscInt    *imat_j,*imat_i;
2318     MatScalar   *imat_a = NULL,*rbuf4_i = NULL;
2319     PetscMPIInt ii;
2320 
2321     for (tmp2=0; tmp2<nrqs; tmp2++) {
2322       if (ijonly) ii = tmp2;
2323       else {
2324         ierr = MPI_Waitany(nrqs,r_waits4,&ii,r_status4+tmp2);CHKERRQ(ierr);
2325       }
2326       idex    = pa[ii];
2327       sbuf1_i = sbuf1[idex];
2328       jmax    = sbuf1_i[0];
2329       ct1     = 2*jmax + 1;
2330       ct2     = 0;
2331       rbuf2_i = rbuf2[ii];
2332       rbuf3_i = rbuf3[ii];
2333       if (!ijonly) rbuf4_i = rbuf4[ii];
2334       for (j=1; j<=jmax; j++) {
2335         is_no = sbuf1_i[2*j-1];
2336         if (!allcolumns[is_no]) cmap_i = cmap[is_no];
2337         rmap_i    = rmap[is_no];
2338         mat       = (Mat_SeqBAIJ*)submats[is_no]->data;
2339         imat_ilen = mat->ilen;
2340         imat_j    = mat->j;
2341         imat_i    = mat->i;
2342         if (!ijonly) imat_a = mat->a;
2343         max1 = sbuf1_i[2*j];
2344         for (k=0; k<max1; k++,ct1++) {
2345           row = sbuf1_i[ct1];
2346 #if defined(PETSC_USE_CTABLE)
2347           ierr = PetscTableFind(rmap_i,row+1,&row);CHKERRQ(ierr);
2348           row--;
2349           if (row < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"row not found in table");
2350 #else
2351           row = rmap_i[row];
2352 #endif
2353           ilen  = imat_ilen[row];
2354           mat_i = imat_i[row];
2355           if (!ijonly) mat_a = imat_a + mat_i*bs2;
2356           mat_j = imat_j + mat_i;
2357           max2  = rbuf2_i[ct1];
2358 
2359           if (!allcolumns[is_no]) {
2360             for (l=0; l<max2; l++,ct2++) {
2361 #if defined(PETSC_USE_CTABLE)
2362               ierr = PetscTableFind(cmap_i,rbuf3_i[ct2]+1,&tcol);CHKERRQ(ierr);
2363               if (tcol) {
2364 #else
2365               if ((tcol = cmap_i[rbuf3_i[ct2]])) {
2366 #endif
2367                 *mat_j++ = tcol - 1;
2368                 if (!ijonly) {
2369                   ierr   = PetscMemcpy(mat_a,rbuf4_i+ct2*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr);
2370                   mat_a += bs2;
2371                 }
2372                 ilen++;
2373               }
2374             }
2375           } else { /* allcolumns */
2376             for (l=0; l<max2; l++,ct2++) {
2377               *mat_j++ = rbuf3_i[ct2];
2378               if (!ijonly) {
2379                 ierr   = PetscMemcpy(mat_a,rbuf4_i+ct2*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr);
2380                 mat_a += bs2;
2381               }
2382               ilen++;
2383             }
2384           }
2385           imat_ilen[row] = ilen;
2386         }
2387       }
2388     }
2389   }
2390   /* sort the rows */
2391   {
2392     PetscInt  ilen_row,*imat_ilen,*imat_j,*imat_i;
2393     MatScalar *imat_a = NULL;
2394     MatScalar *work;
2395 
2396     ierr = PetscMalloc1(bs2,&work);CHKERRQ(ierr);
2397     for (i=0; i<ismax; i++) {
2398       mat       = (Mat_SeqBAIJ*)submats[i]->data;
2399       imat_ilen = mat->ilen;
2400       imat_j    = mat->j;
2401       imat_i    = mat->i;
2402       if (!ijonly) imat_a = mat->a;
2403       if (allcolumns[i]) continue;
2404       jmax   = nrow[i];
2405       for (j=0; j<jmax; j++) {
2406         mat_i = imat_i[j];
2407         if (!ijonly) mat_a = imat_a + mat_i*bs2;
2408         mat_j    = imat_j + mat_i;
2409         ilen_row = imat_ilen[j];
2410         if (!ijonly) {ierr = PetscSortIntWithDataArray(ilen_row,mat_j,mat_a,bs2*sizeof(MatScalar),work);CHKERRQ(ierr);}
2411         else {ierr = PetscSortInt(ilen_row,mat_j);CHKERRQ(ierr);}
2412       }
2413     }
2414     ierr = PetscFree(work);CHKERRQ(ierr);
2415   }
2416   if (!ijonly) {
2417     ierr = PetscFree(r_status4);CHKERRQ(ierr);
2418     ierr = PetscFree(r_waits4);CHKERRQ(ierr);
2419     if (nrqr) {ierr = MPI_Waitall(nrqr,s_waits4,s_status4);CHKERRQ(ierr);}
2420     ierr = PetscFree(s_waits4);CHKERRQ(ierr);
2421     ierr = PetscFree(s_status4);CHKERRQ(ierr);
2422   }
2423 
2424   /* Restore the indices */
2425   for (i=0; i<ismax; i++) {
2426     if (!allrows[i]) {
2427       ierr = ISRestoreIndices(isrow[i],irow+i);CHKERRQ(ierr);
2428     }
2429     if (!allcolumns[i]) {
2430       ierr = ISRestoreIndices(iscol[i],icol+i);CHKERRQ(ierr);
2431     }
2432   }
2433 
2434   /* Destroy allocated memory */
2435 #if defined(PETSC_USE_CTABLE)
2436   ierr = PetscFree4(irow,icol,nrow,ncol);CHKERRQ(ierr);
2437 #else
2438   ierr = PetscFree5(irow,icol,nrow,ncol,rtable);CHKERRQ(ierr);
2439 #endif
2440   ierr = PetscFree4(w1,w2,w3,w4);CHKERRQ(ierr);
2441   ierr = PetscFree(pa);CHKERRQ(ierr);
2442 
2443   ierr = PetscFree4(sbuf1,ptr,tmp,ctr);CHKERRQ(ierr);
2444   ierr = PetscFree(sbuf1);CHKERRQ(ierr);
2445   ierr = PetscFree(rbuf2);CHKERRQ(ierr);
2446   for (i=0; i<nrqr; ++i) {
2447     ierr = PetscFree(sbuf2[i]);CHKERRQ(ierr);
2448   }
2449   for (i=0; i<nrqs; ++i) {
2450     ierr = PetscFree(rbuf3[i]);CHKERRQ(ierr);
2451   }
2452   ierr = PetscFree3(sbuf2,req_size,req_source);CHKERRQ(ierr);
2453   ierr = PetscFree(rbuf3);CHKERRQ(ierr);
2454   ierr = PetscFree(sbuf_aj[0]);CHKERRQ(ierr);
2455   ierr = PetscFree(sbuf_aj);CHKERRQ(ierr);
2456   if (!ijonly) {
2457     for (i=0; i<nrqs; ++i) {ierr = PetscFree(rbuf4[i]);CHKERRQ(ierr);}
2458     ierr = PetscFree(rbuf4);CHKERRQ(ierr);
2459     ierr = PetscFree(sbuf_aa[0]);CHKERRQ(ierr);
2460     ierr = PetscFree(sbuf_aa);CHKERRQ(ierr);
2461   }
2462 
2463 #if defined(PETSC_USE_CTABLE)
2464   for (i=0; i<ismax; i++) {
2465     ierr = PetscTableDestroy((PetscTable*)&rmap[i]);CHKERRQ(ierr);
2466   }
2467 #endif
2468   ierr = PetscFree(rmap);CHKERRQ(ierr);
2469 
2470   for (i=0; i<ismax; i++) {
2471     if (!allcolumns[i]) {
2472 #if defined(PETSC_USE_CTABLE)
2473       ierr = PetscTableDestroy((PetscTable*)&cmap[i]);CHKERRQ(ierr);
2474 #else
2475       ierr = PetscFree(cmap[i]);CHKERRQ(ierr);
2476 #endif
2477     }
2478   }
2479   ierr = PetscFree(cmap);CHKERRQ(ierr);
2480   ierr = PetscFree(lens);CHKERRQ(ierr);
2481 
2482   for (i=0; i<ismax; i++) {
2483     ierr = MatAssemblyBegin(submats[i],MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2484     ierr = MatAssemblyEnd(submats[i],MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2485   }
2486 
2487   c->ijonly = PETSC_FALSE; /* set back to the default */
2488   PetscFunctionReturn(0);
2489 }
2490 
2491