xref: /petsc/src/mat/impls/baij/mpi/baijov.c (revision ac2a4f0d24b3b6a4ee93edbcad41f4bb9e923944)
1 /*$Id: baijov.c,v 1.38 1999/10/13 20:37:30 bsmith Exp bsmith $*/
2 
3 /*
4    Routines to compute overlapping regions of a parallel MPI matrix
5   and to find submatrices that were shared across processors.
6 */
7 #include "src/mat/impls/baij/mpi/mpibaij.h"
8 #include "bitarray.h"
9 
10 static int MatIncreaseOverlap_MPIBAIJ_Once(Mat, int, IS *);
11 static int MatIncreaseOverlap_MPIBAIJ_Local(Mat , int , char **,int*, int**);
12 static int MatIncreaseOverlap_MPIBAIJ_Receive(Mat , int, int **, int**, int* );
13 extern int MatGetRow_MPIBAIJ(Mat,int,int*,int**,Scalar**);
14 extern int MatRestoreRow_MPIBAIJ(Mat,int,int*,int**,Scalar**);
15 
16 #undef __FUNC__
17 #define __FUNC__ "MatCompressIndicesGeneral_MPIBAIJ"
18 static int MatCompressIndicesGeneral_MPIBAIJ(Mat C, int imax, IS *is_in, IS *is_out)
19 {
20   Mat_MPIBAIJ  *baij = (Mat_MPIBAIJ *) C->data;
21   int          ierr,isz,bs = baij->bs,Nbs,n,i,j,*idx,*nidx,ival;
22   BTPetsc      table;
23 
24   PetscFunctionBegin;
25   Nbs   = baij->Nbs;
26   nidx  = (int *) PetscMalloc((Nbs+1)*sizeof(int));CHKPTRQ(nidx);
27   ierr  = PetscBTCreate(Nbs,table);CHKERRQ(ierr);
28 
29   for (i=0; i<imax; i++) {
30     isz  = 0;
31     ierr = PetscBTMemzero(Nbs,table);CHKERRQ(ierr);
32     ierr = ISGetIndices(is_in[i],&idx);CHKERRQ(ierr);
33     ierr = ISGetSize(is_in[i],&n);CHKERRQ(ierr);
34     for (j=0; j<n ; j++) {
35       ival = idx[j]/bs; /* convert the indices into block indices */
36       if (ival>Nbs) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"index greater than mat-dim");
37       if(!PetscBTLoopupSet(table, ival)) { nidx[isz++] = ival;}
38     }
39     ierr = ISRestoreIndices(is_in[i],&idx);CHKERRQ(ierr);
40     ierr = ISCreateGeneral(PETSC_COMM_SELF, isz, nidx, (is_out+i));CHKERRQ(ierr);
41   }
42   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
43   ierr = PetscFree(nidx);CHKERRQ(ierr);
44   PetscFunctionReturn(0);
45 }
46 
47 #undef __FUNC__
48 #define __FUNC__ "MatCompressIndicesSorted_MPIBAIJ"
49 static int MatCompressIndicesSorted_MPIBAIJ(Mat C, int imax, IS *is_in, IS *is_out)
50 {
51   Mat_MPIBAIJ  *baij = (Mat_MPIBAIJ *) C->data;
52   int          ierr,bs=baij->bs,i,j,k,val,n,*idx,*nidx,Nbs=baij->Nbs,*idx_local;
53   PetscTruth   flg;
54 
55   PetscFunctionBegin;
56   for (i=0; i<imax; i++) {
57     ierr = ISSorted(is_in[i],&flg);CHKERRQ(ierr);
58     if (!flg) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Indices are not sorted");
59   }
60   nidx  = (int *) PetscMalloc((Nbs+1)*sizeof(int));CHKPTRQ(nidx);
61   /* Now check if the indices are in block order */
62   for (i=0; i<imax; i++) {
63     ierr = ISGetIndices(is_in[i],&idx);CHKERRQ(ierr);
64     ierr = ISGetSize(is_in[i],&n);CHKERRQ(ierr);
65     if ( n%bs !=0 ) SETERRA(1,0,"Indices are not block ordered");
66 
67     n = n/bs; /* The reduced index size */
68     idx_local = idx;
69     for (j=0; j<n ; j++) {
70       val = idx_local[0];
71       if (val%bs != 0) SETERRA(1,0,"Indices are not block ordered");
72       for (k=0; k<bs; k++) {
73         if ( val+k != idx_local[k]) SETERRA(1,0,"Indices are not block ordered");
74       }
75       nidx[j] = val/bs;
76       idx_local +=bs;
77     }
78     ierr = ISRestoreIndices(is_in[i],&idx);CHKERRQ(ierr);
79     ierr = ISCreateGeneral(PETSC_COMM_SELF,n,nidx,(is_out+i));CHKERRQ(ierr);
80   }
81   ierr = PetscFree(nidx);CHKERRQ(ierr);
82   PetscFunctionReturn(0);
83 }
84 
85 #undef __FUNC__
86 #define __FUNC__ "MatExpandIndices_MPIBAIJ"
87 static int MatExpandIndices_MPIBAIJ(Mat C, int imax, IS *is_in, IS *is_out)
88 {
89   Mat_MPIBAIJ  *baij = (Mat_MPIBAIJ *) C->data;
90   int          ierr,bs = baij->bs,Nbs,n,i,j,k,*idx,*nidx;
91 
92   PetscFunctionBegin;
93   Nbs   = baij->Nbs;
94 
95   nidx  = (int *) PetscMalloc((Nbs*bs+1)*sizeof(int));CHKPTRQ(nidx);
96 
97   for ( i=0; i<imax; i++ ) {
98     ierr = ISGetIndices(is_in[i],&idx);CHKERRQ(ierr);
99     ierr = ISGetSize(is_in[i],&n);CHKERRQ(ierr);
100     for (j=0; j<n ; ++j){
101       for (k=0; k<bs; k++)
102         nidx[j*bs+k] = idx[j]*bs+k;
103     }
104     ierr = ISRestoreIndices(is_in[i],&idx);CHKERRQ(ierr);
105     ierr = ISCreateGeneral(PETSC_COMM_SELF, n*bs, nidx, (is_out+i));CHKERRQ(ierr);
106   }
107   ierr = PetscFree(nidx);CHKERRQ(ierr);
108   PetscFunctionReturn(0);
109 }
110 
111 
112 #undef __FUNC__
113 #define __FUNC__ "MatIncreaseOverlap_MPIBAIJ"
114 int MatIncreaseOverlap_MPIBAIJ(Mat C, int imax, IS *is, int ov)
115 {
116   int i, ierr;
117   IS  *is_new;
118 
119   PetscFunctionBegin;
120   is_new = (IS *)PetscMalloc(imax*sizeof(IS));CHKPTRQ(is_new);
121   /* Convert the indices into block format */
122   ierr = MatCompressIndicesGeneral_MPIBAIJ(C, imax, is,is_new);CHKERRQ(ierr);
123   if (ov < 0){ SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Negative overlap specified\n");}
124   for (i=0; i<ov; ++i) {
125     ierr = MatIncreaseOverlap_MPIBAIJ_Once(C, imax, is_new);CHKERRQ(ierr);
126   }
127   for (i=0; i<imax; i++) {ierr = ISDestroy(is[i]);CHKERRQ(ierr);}
128   ierr = MatExpandIndices_MPIBAIJ(C, imax, is_new,is);CHKERRQ(ierr);
129   for (i=0; i<imax; i++) {ierr = ISDestroy(is_new[i]);CHKERRQ(ierr);}
130   ierr = PetscFree(is_new);CHKERRQ(ierr);
131   PetscFunctionReturn(0);
132 }
133 
134 /*
135   Sample message format:
136   If a processor A wants processor B to process some elements corresponding
137   to index sets 1s[1], is[5]
138   mesg [0] = 2   ( no of index sets in the mesg)
139   -----------
140   mesg [1] = 1 => is[1]
141   mesg [2] = sizeof(is[1]);
142   -----------
143   mesg [5] = 5  => is[5]
144   mesg [6] = sizeof(is[5]);
145   -----------
146   mesg [7]
147   mesg [n]  datas[1]
148   -----------
149   mesg[n+1]
150   mesg[m]  data(is[5])
151   -----------
152 
153   Notes:
154   nrqs - no of requests sent (or to be sent out)
155   nrqr - no of requests recieved (which have to be or which have been processed
156 */
157 #undef __FUNC__
158 #define __FUNC__ "MatIncreaseOverlap_MPIBAIJ_Once"
159 static int MatIncreaseOverlap_MPIBAIJ_Once(Mat C, int imax, IS *is)
160 {
161   Mat_MPIBAIJ  *c = (Mat_MPIBAIJ *) C->data;
162   int         **idx, *n, *w1, *w2, *w3, *w4, *rtable,**data,len,*idx_i;
163   int         size,rank,Mbs,i,j,k,ierr,**rbuf,row,proc,nrqs,msz,**outdat,**ptr;
164   int         *ctr,*pa,tag,*tmp,bsz,nrqr,*isz,*isz1,**xdata,bsz1,**rbuf2;
165   BTPetsc     *table;
166   MPI_Comm    comm;
167   MPI_Request *s_waits1,*r_waits1,*s_waits2,*r_waits2;
168   MPI_Status  *s_status,*recv_status;
169 
170   PetscFunctionBegin;
171   comm   = C->comm;
172   tag    = C->tag;
173   size   = c->size;
174   rank   = c->rank;
175   Mbs      = c->Mbs;
176 
177   len    = (imax+1)*sizeof(int *) + (imax + Mbs)*sizeof(int);
178   idx    = (int **) PetscMalloc(len);CHKPTRQ(idx);
179   n      = (int *) (idx + imax);
180   rtable = n + imax;
181 
182   for (i=0; i<imax; i++) {
183     ierr = ISGetIndices(is[i],&idx[i]);CHKERRQ(ierr);
184     ierr = ISGetSize(is[i],&n[i]);CHKERRQ(ierr);
185   }
186 
187   /* Create hash table for the mapping :row -> proc*/
188   for (i=0,j=0; i<size; i++) {
189     len = c->rowners[i+1];
190     for (; j<len; j++) {
191       rtable[j] = i;
192     }
193   }
194 
195   /* evaluate communication - mesg to who, length of mesg, and buffer space
196      required. Based on this, buffers are allocated, and data copied into them*/
197   w1   = (int *)PetscMalloc(size*4*sizeof(int));CHKPTRQ(w1);/*  mesg size */
198   w2   = w1 + size;       /* if w2[i] marked, then a message to proc i*/
199   w3   = w2 + size;       /* no of IS that needs to be sent to proc i */
200   w4   = w3 + size;       /* temp work space used in determining w1, w2, w3 */
201   ierr = PetscMemzero(w1,size*3*sizeof(int));CHKERRQ(ierr); /* initialise work vector*/
202   for (i=0; i<imax; i++) {
203     ierr  = PetscMemzero(w4,size*sizeof(int));CHKERRQ(ierr); /* initialise work vector*/
204     idx_i = idx[i];
205     len   = n[i];
206     for (j=0; j<len; j++) {
207       row  = idx_i[j];
208       if (row < 0) {
209         SETERRQ(1,1,"Index set cannot have negative entries");
210       }
211       proc = rtable[row];
212       w4[proc]++;
213     }
214     for (j=0; j<size; j++){
215       if (w4[j]) { w1[j] += w4[j]; w3[j]++;}
216     }
217   }
218 
219   nrqs     = 0;              /* no of outgoing messages */
220   msz      = 0;              /* total mesg length (for all proc */
221   w1[rank] = 0;              /* no mesg sent to intself */
222   w3[rank] = 0;
223   for ( i=0; i<size; i++) {
224     if (w1[i])  {w2[i] = 1; nrqs++;} /* there exists a message to proc i */
225   }
226   /* pa - is list of processors to communicate with */
227   pa = (int *)PetscMalloc((nrqs+1)*sizeof(int));CHKPTRQ(pa);
228   for (i=0,j=0; i<size; i++) {
229     if (w1[i]) {pa[j] = i; j++;}
230   }
231 
232   /* Each message would have a header = 1 + 2*(no of IS) + data */
233   for (i=0; i<nrqs; i++) {
234     j      = pa[i];
235     w1[j] += w2[j] + 2*w3[j];
236     msz   += w1[j];
237   }
238 
239 
240   /* Do a global reduction to determine how many messages to expect*/
241   {
242     int *rw1;
243     rw1   = (int *) PetscMalloc(2*size*sizeof(int));CHKPTRQ(rw1);
244     ierr  = MPI_Allreduce(w1, rw1, 2*size, MPI_INT, PetscMaxSum_Op, comm);CHKERRQ(ierr);
245     bsz   = rw1[rank];
246     nrqr  = rw1[size+rank];
247     ierr  = PetscFree(rw1);CHKERRQ(ierr);
248   }
249 
250   /* Allocate memory for recv buffers . Prob none if nrqr = 0 ???? */
251   len     = (nrqr+1)*sizeof(int*) + nrqr*bsz*sizeof(int);
252   rbuf    = (int**) PetscMalloc(len);CHKPTRQ(rbuf);
253   rbuf[0] = (int *) (rbuf + nrqr);
254   for (i=1; i<nrqr; ++i) rbuf[i] = rbuf[i-1] + bsz;
255 
256   /* Post the receives */
257   r_waits1 = (MPI_Request *) PetscMalloc((nrqr+1)*sizeof(MPI_Request));CHKPTRQ(r_waits1);
258   for (i=0; i<nrqr; ++i) {
259     ierr = MPI_Irecv(rbuf[i],bsz,MPI_INT,MPI_ANY_SOURCE,tag,comm,r_waits1+i);CHKERRQ(ierr);
260   }
261 
262   /* Allocate Memory for outgoing messages */
263   len    = 2*size*sizeof(int*) + (size+msz)*sizeof(int);
264   outdat = (int **)PetscMalloc(len);CHKPTRQ(outdat);
265   ptr    = outdat + size;     /* Pointers to the data in outgoing buffers */
266   ierr   = PetscMemzero(outdat,2*size*sizeof(int*));CHKERRQ(ierr);
267   tmp    = (int *) (outdat + 2*size);
268   ctr    = tmp + msz;
269 
270   {
271     int *iptr = tmp,ict  = 0;
272     for (i=0; i<nrqs; i++) {
273       j         = pa[i];
274       iptr     +=  ict;
275       outdat[j] = iptr;
276       ict       = w1[j];
277     }
278   }
279 
280   /* Form the outgoing messages */
281   /*plug in the headers*/
282   for (i=0; i<nrqs; i++) {
283     j            = pa[i];
284     outdat[j][0] = 0;
285     ierr = PetscMemzero(outdat[j]+1,2*w3[j]*sizeof(int));CHKERRQ(ierr);
286     ptr[j]       = outdat[j] + 2*w3[j] + 1;
287   }
288 
289   /* Memory for doing local proc's work*/
290   {
291     int  *d_p;
292     char *t_p;
293 
294     len   = (imax)*(sizeof(BTPetsc) + sizeof(int *) + sizeof(int)) +
295       (Mbs)*imax*sizeof(int)  + (Mbs/BITSPERBYTE+1)*imax*sizeof(char) + 1;
296     table = (BTPetsc *)PetscMalloc(len);CHKPTRQ(table);
297     ierr  = PetscMemzero(table,len);CHKERRQ(ierr);
298     data  = (int **)(table + imax);
299     isz   = (int  *)(data  + imax);
300     d_p   = (int  *)(isz   + imax);
301     t_p   = (char *)(d_p   + Mbs*imax);
302     for (i=0; i<imax; i++) {
303       table[i] = t_p + (Mbs/BITSPERBYTE+1)*i;
304       data[i]  = d_p + (Mbs)*i;
305     }
306   }
307 
308   /* Parse the IS and update local tables and the outgoing buf with the data*/
309   {
310     int     n_i,*data_i,isz_i,*outdat_j,ctr_j;
311     BTPetsc table_i;
312 
313     for (i=0; i<imax; i++) {
314       ierr    = PetscMemzero(ctr,size*sizeof(int));CHKERRQ(ierr);
315       n_i     = n[i];
316       table_i = table[i];
317       idx_i   = idx[i];
318       data_i  = data[i];
319       isz_i   = isz[i];
320       for (j=0;  j<n_i; j++) {  /* parse the indices of each IS */
321         row  = idx_i[j];
322         proc = rtable[row];
323         if (proc != rank) { /* copy to the outgoing buffer */
324           ctr[proc]++;
325           *ptr[proc] = row;
326           ptr[proc]++;
327         }
328         else { /* Update the local table */
329           if (!PetscBTLoopupSet(table_i,row)) { data_i[isz_i++] = row;}
330         }
331       }
332       /* Update the headers for the current IS */
333       for (j=0; j<size; j++) { /* Can Optimise this loop by using pa[] */
334         if ((ctr_j = ctr[j])) {
335           outdat_j        = outdat[j];
336           k               = ++outdat_j[0];
337           outdat_j[2*k]   = ctr_j;
338           outdat_j[2*k-1] = i;
339         }
340       }
341       isz[i] = isz_i;
342     }
343   }
344 
345 
346 
347   /*  Now  post the sends */
348   s_waits1 = (MPI_Request *) PetscMalloc((nrqs+1)*sizeof(MPI_Request));CHKPTRQ(s_waits1);
349   for (i=0; i<nrqs; ++i) {
350     j    = pa[i];
351     ierr = MPI_Isend(outdat[j], w1[j], MPI_INT, j, tag, comm, s_waits1+i);CHKERRQ(ierr);
352   }
353 
354   /* No longer need the original indices*/
355   for (i=0; i<imax; ++i) {
356     ierr = ISRestoreIndices(is[i], idx+i);CHKERRQ(ierr);
357   }
358   ierr = PetscFree(idx);CHKERRQ(ierr);
359 
360   for (i=0; i<imax; ++i) {
361     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
362   }
363 
364   /* Do Local work*/
365   ierr = MatIncreaseOverlap_MPIBAIJ_Local(C,imax,table,isz,data);CHKERRQ(ierr);
366 
367   /* Receive messages*/
368   {
369     int        index;
370 
371     recv_status = (MPI_Status *) PetscMalloc((nrqr+1)*sizeof(MPI_Status));CHKPTRQ(recv_status);
372     for (i=0; i<nrqr; ++i) {
373       ierr = MPI_Waitany(nrqr, r_waits1, &index, recv_status+i);CHKERRQ(ierr);
374     }
375 
376     s_status = (MPI_Status *) PetscMalloc((nrqs+1)*sizeof(MPI_Status));CHKPTRQ(s_status);
377     ierr     = MPI_Waitall(nrqs,s_waits1,s_status);CHKERRQ(ierr);
378   }
379 
380   /* Phase 1 sends are complete - deallocate buffers */
381   ierr = PetscFree(outdat);CHKERRQ(ierr);
382   ierr = PetscFree(w1);CHKERRQ(ierr);
383 
384   xdata = (int **)PetscMalloc((nrqr+1)*sizeof(int *));CHKPTRQ(xdata);
385   isz1  = (int *)PetscMalloc((nrqr+1)*sizeof(int));CHKPTRQ(isz1);
386   ierr  = MatIncreaseOverlap_MPIBAIJ_Receive(C,nrqr,rbuf,xdata,isz1);CHKERRQ(ierr);
387   ierr = PetscFree(rbuf);CHKERRQ(ierr);
388 
389   /* Send the data back*/
390   /* Do a global reduction to know the buffer space req for incoming messages*/
391   {
392     int *rw1, *rw2;
393 
394     rw1  = (int *)PetscMalloc(2*size*sizeof(int));CHKPTRQ(rw1);
395     ierr = PetscMemzero(rw1,2*size*sizeof(int));CHKERRQ(ierr);
396     rw2  = rw1+size;
397     for (i=0; i<nrqr; ++i) {
398       proc      = recv_status[i].MPI_SOURCE;
399       rw1[proc] = isz1[i];
400     }
401 
402     ierr   = MPI_Allreduce(rw1, rw2, size, MPI_INT, MPI_MAX, comm);CHKERRQ(ierr);
403     bsz1   = rw2[rank];
404     ierr = PetscFree(rw1);CHKERRQ(ierr);
405   }
406 
407   /* Allocate buffers*/
408 
409   /* Allocate memory for recv buffers. Prob none if nrqr = 0 ???? */
410   len      = (nrqs+1)*sizeof(int*) + nrqs*bsz1*sizeof(int);
411   rbuf2    = (int**) PetscMalloc(len);CHKPTRQ(rbuf2);
412   rbuf2[0] = (int *) (rbuf2 + nrqs);
413   for (i=1; i<nrqs; ++i) rbuf2[i] = rbuf2[i-1] + bsz1;
414 
415   /* Post the receives */
416   r_waits2 = (MPI_Request *)PetscMalloc((nrqs+1)*sizeof(MPI_Request));CHKPTRQ(r_waits2);
417   for (i=0; i<nrqs; ++i) {
418     ierr = MPI_Irecv(rbuf2[i], bsz1, MPI_INT, MPI_ANY_SOURCE, tag, comm, r_waits2+i);CHKERRQ(ierr);
419   }
420 
421   /*  Now  post the sends */
422   s_waits2 = (MPI_Request *) PetscMalloc((nrqr+1)*sizeof(MPI_Request));CHKPTRQ(s_waits2);
423   for (i=0; i<nrqr; ++i) {
424     j    = recv_status[i].MPI_SOURCE;
425     ierr = MPI_Isend( xdata[i], isz1[i], MPI_INT, j, tag, comm, s_waits2+i);CHKERRQ(ierr);
426   }
427 
428   /* receive work done on other processors*/
429   {
430     int         index, is_no, ct1, max,*rbuf2_i,isz_i,*data_i,jmax;
431     BTPetsc     table_i;
432     MPI_Status  *status2;
433 
434     status2 = (MPI_Status *) PetscMalloc((nrqs+1)*sizeof(MPI_Status));CHKPTRQ(status2);
435 
436     for (i=0; i<nrqs; ++i) {
437       ierr = MPI_Waitany(nrqs, r_waits2, &index, status2+i);CHKERRQ(ierr);
438       /* Process the message*/
439       rbuf2_i = rbuf2[index];
440       ct1     = 2*rbuf2_i[0]+1;
441       jmax    = rbuf2[index][0];
442       for (j=1; j<=jmax; j++) {
443         max     = rbuf2_i[2*j];
444         is_no   = rbuf2_i[2*j-1];
445         isz_i   = isz[is_no];
446         data_i  = data[is_no];
447         table_i = table[is_no];
448         for (k=0; k<max; k++,ct1++) {
449           row = rbuf2_i[ct1];
450           if (!PetscBTLoopupSet(table_i,row)) { data_i[isz_i++] = row;}
451         }
452         isz[is_no] = isz_i;
453       }
454     }
455     ierr = MPI_Waitall(nrqr,s_waits2,status2);CHKERRQ(ierr);
456     ierr = PetscFree(status2); CHKERRQ(ierr);
457   }
458 
459   for (i=0; i<imax; ++i) {
460     ierr = ISCreateGeneral(PETSC_COMM_SELF, isz[i], data[i], is+i);CHKERRQ(ierr);
461   }
462 
463   ierr = PetscFree(pa);CHKERRQ(ierr);
464   ierr = PetscFree(rbuf2);CHKERRQ(ierr);
465   ierr = PetscFree(s_waits1);CHKERRQ(ierr);
466   ierr = PetscFree(r_waits1);CHKERRQ(ierr);
467   ierr = PetscFree(s_waits2);CHKERRQ(ierr);
468   ierr = PetscFree(r_waits2);CHKERRQ(ierr);
469   ierr = PetscFree(table);CHKERRQ(ierr);
470   ierr = PetscFree(s_status);CHKERRQ(ierr);
471   ierr = PetscFree(recv_status);CHKERRQ(ierr);
472   ierr = PetscFree(xdata[0]);CHKERRQ(ierr);
473   ierr = PetscFree(xdata);CHKERRQ(ierr);
474   ierr = PetscFree(isz1);CHKERRQ(ierr);
475   PetscFunctionReturn(0);
476 }
477 
478 #undef __FUNC__
479 #define __FUNC__ "MatIncreaseOverlap_MPIBAIJ_Local"
480 /*
481    MatIncreaseOverlap_MPIBAIJ_Local - Called by MatincreaseOverlap, to do
482        the work on the local processor.
483 
484      Inputs:
485       C      - MAT_MPIBAIJ;
486       imax - total no of index sets processed at a time;
487       table  - an array of char - size = Mbs bits.
488 
489      Output:
490       isz    - array containing the count of the solution elements correspondign
491                to each index set;
492       data   - pointer to the solutions
493 */
494 static int MatIncreaseOverlap_MPIBAIJ_Local(Mat C,int imax,BTPetsc *table,int *isz,int **data)
495 {
496   Mat_MPIBAIJ *c = (Mat_MPIBAIJ *) C->data;
497   Mat         A = c->A, B = c->B;
498   Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data,*b = (Mat_SeqBAIJ*)B->data;
499   int         start, end, val, max, rstart,cstart,*ai, *aj;
500   int         *bi, *bj, *garray, i, j, k, row,*data_i,isz_i;
501   BTPetsc     table_i;
502 
503   PetscFunctionBegin;
504   rstart = c->rstart;
505   cstart = c->cstart;
506   ai     = a->i;
507   aj     = a->j;
508   bi     = b->i;
509   bj     = b->j;
510   garray = c->garray;
511 
512 
513   for (i=0; i<imax; i++) {
514     data_i  = data[i];
515     table_i = table[i];
516     isz_i   = isz[i];
517     for (j=0, max=isz[i]; j<max; j++) {
518       row   = data_i[j] - rstart;
519       start = ai[row];
520       end   = ai[row+1];
521       for (k=start; k<end; k++) { /* Amat */
522         val = aj[k] + cstart;
523         if (!PetscBTLoopupSet(table_i,val)) { data_i[isz_i++] = val;}
524       }
525       start = bi[row];
526       end   = bi[row+1];
527       for (k=start; k<end; k++) { /* Bmat */
528         val = garray[bj[k]];
529         if (!PetscBTLoopupSet(table_i,val)) { data_i[isz_i++] = val;}
530       }
531     }
532     isz[i] = isz_i;
533   }
534   PetscFunctionReturn(0);
535 }
536 #undef __FUNC__
537 #define __FUNC__ "MatIncreaseOverlap_MPIBAIJ_Receive"
538 /*
539       MatIncreaseOverlap_MPIBAIJ_Receive - Process the recieved messages,
540          and return the output
541 
542          Input:
543            C    - the matrix
544            nrqr - no of messages being processed.
545            rbuf - an array of pointers to the recieved requests
546 
547          Output:
548            xdata - array of messages to be sent back
549            isz1  - size of each message
550 
551   For better efficiency perhaps we should malloc seperately each xdata[i],
552 then if a remalloc is required we need only copy the data for that one row
553 rather then all previous rows as it is now where a single large chunck of
554 memory is used.
555 
556 */
557 static int MatIncreaseOverlap_MPIBAIJ_Receive(Mat C,int nrqr,int **rbuf,int **xdata, int * isz1)
558 {
559   Mat_MPIBAIJ *c = (Mat_MPIBAIJ *) C->data;
560   Mat         A = c->A, B = c->B;
561   Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data,*b = (Mat_SeqBAIJ*)B->data;
562   int         rstart,cstart,*ai, *aj, *bi, *bj, *garray, i, j, k;
563   int         row,total_sz,ct, ct1, ct2, ct3,mem_estimate, oct2, l, start, end;
564   int         val, max1, max2, rank, Mbs, no_malloc =0, *tmp, new_estimate, ctr;
565   int         *rbuf_i,kmax,rbuf_0,ierr;
566   BTPetsc     xtable;
567 
568   PetscFunctionBegin;
569   rank   = c->rank;
570   Mbs    = c->Mbs;
571   rstart = c->rstart;
572   cstart = c->cstart;
573   ai     = a->i;
574   aj     = a->j;
575   bi     = b->i;
576   bj     = b->j;
577   garray = c->garray;
578 
579 
580   for (i=0,ct=0,total_sz=0; i<nrqr; ++i) {
581     rbuf_i  =  rbuf[i];
582     rbuf_0  =  rbuf_i[0];
583     ct     += rbuf_0;
584     for (j=1; j<=rbuf_0; j++) { total_sz += rbuf_i[2*j]; }
585   }
586 
587   if (c->Mbs) max1 = ct*(a->nz +b->nz)/c->Mbs;
588   else        max1 = 1;
589   mem_estimate = 3*((total_sz > max1 ? total_sz : max1)+1);
590   xdata[0]     = (int *) PetscMalloc(mem_estimate*sizeof(int));CHKPTRQ(xdata[0]);
591   ++no_malloc;
592   ierr         = PetscBTCreate(Mbs,xtable);CHKERRQ(ierr);
593   ierr         = PetscMemzero(isz1,nrqr*sizeof(int));CHKERRQ(ierr);
594 
595   ct3 = 0;
596   for (i=0; i<nrqr; i++) { /* for easch mesg from proc i */
597     rbuf_i =  rbuf[i];
598     rbuf_0 =  rbuf_i[0];
599     ct1    =  2*rbuf_0+1;
600     ct2    =  ct1;
601     ct3    += ct1;
602     for (j=1; j<=rbuf_0; j++) { /* for each IS from proc i*/
603       ierr = PetscBTMemzero(Mbs,xtable);CHKERRQ(ierr);
604       oct2 = ct2;
605       kmax = rbuf_i[2*j];
606       for (k=0; k<kmax; k++, ct1++) {
607         row = rbuf_i[ct1];
608         if (!PetscBTLoopupSet(xtable,row)) {
609           if (!(ct3 < mem_estimate)) {
610             new_estimate = (int)(1.5*mem_estimate)+1;
611             tmp          = (int*) PetscMalloc(new_estimate * sizeof(int));CHKPTRQ(tmp);
612             ierr = PetscMemcpy(tmp,xdata[0],mem_estimate*sizeof(int));CHKERRQ(ierr);
613             ierr = PetscFree(xdata[0]);CHKERRQ(ierr);
614             xdata[0]     = tmp;
615             mem_estimate = new_estimate; ++no_malloc;
616             for (ctr=1; ctr<=i; ctr++) { xdata[ctr] = xdata[ctr-1] + isz1[ctr-1];}
617           }
618           xdata[i][ct2++] = row;
619           ct3++;
620         }
621       }
622       for (k=oct2,max2=ct2; k<max2; k++)  {
623         row   = xdata[i][k] - rstart;
624         start = ai[row];
625         end   = ai[row+1];
626         for (l=start; l<end; l++) {
627           val = aj[l] + cstart;
628           if (!PetscBTLoopupSet(xtable,val)) {
629             if (!(ct3 < mem_estimate)) {
630               new_estimate = (int)(1.5*mem_estimate)+1;
631               tmp          = (int*) PetscMalloc(new_estimate * sizeof(int));CHKPTRQ(tmp);
632               ierr         = PetscMemcpy(tmp,xdata[0],mem_estimate*sizeof(int));CHKERRQ(ierr);
633               ierr = PetscFree(xdata[0]);CHKERRQ(ierr);
634               xdata[0]     = tmp;
635               mem_estimate = new_estimate; ++no_malloc;
636               for (ctr=1; ctr<=i; ctr++) { xdata[ctr] = xdata[ctr-1] + isz1[ctr-1];}
637             }
638             xdata[i][ct2++] = val;
639             ct3++;
640           }
641         }
642         start = bi[row];
643         end   = bi[row+1];
644         for (l=start; l<end; l++) {
645           val = garray[bj[l]];
646           if (!PetscBTLoopupSet(xtable,val)) {
647             if (!(ct3 < mem_estimate)) {
648               new_estimate = (int)(1.5*mem_estimate)+1;
649               tmp          = (int*) PetscMalloc(new_estimate * sizeof(int));CHKPTRQ(tmp);
650               ierr         = PetscMemcpy(tmp,xdata[0],mem_estimate*sizeof(int));CHKERRQ(ierr);
651               ierr = PetscFree(xdata[0]);CHKERRQ(ierr);
652               xdata[0]     = tmp;
653               mem_estimate = new_estimate; ++no_malloc;
654               for (ctr =1; ctr <=i; ctr++) { xdata[ctr] = xdata[ctr-1] + isz1[ctr-1];}
655             }
656             xdata[i][ct2++] = val;
657             ct3++;
658           }
659         }
660       }
661       /* Update the header*/
662       xdata[i][2*j]   = ct2 - oct2; /* Undo the vector isz1 and use only a var*/
663       xdata[i][2*j-1] = rbuf_i[2*j-1];
664     }
665     xdata[i][0] = rbuf_0;
666     xdata[i+1]  = xdata[i] + ct2;
667     isz1[i]     = ct2; /* size of each message */
668   }
669   ierr = PetscBTDestroy(xtable);CHKERRQ(ierr);
670   PLogInfo(0,"MatIncreaseOverlap_MPIBAIJ:[%d] Allocated %d bytes, required %d, no of mallocs = %d\n",rank,mem_estimate,ct3,no_malloc);
671   PetscFunctionReturn(0);
672 }
673 
674 static int MatGetSubMatrices_MPIBAIJ_local(Mat,int,IS *,IS *,MatReuse,Mat *);
675 
676 #undef __FUNC__
677 #define __FUNC__ "MatGetSubMatrices_MPIBAIJ"
678 int MatGetSubMatrices_MPIBAIJ(Mat C,int ismax,IS *isrow,IS *iscol,
679                              MatReuse scall,Mat **submat)
680 {
681   IS          *isrow_new,*iscol_new;
682   Mat_MPIBAIJ *c = (Mat_MPIBAIJ *) C->data;
683   int         nmax,nstages_local,nstages,i,pos,max_no,ierr;
684 
685   PetscFunctionBegin;
686   /* The compression and expansion should be avoided. Does'nt point
687      out errors might change the indices hence buggey */
688 
689   isrow_new = (IS *)PetscMalloc(2*(ismax+1)*sizeof(IS));CHKPTRQ(isrow_new);
690   iscol_new = isrow_new + ismax;
691   ierr = MatCompressIndicesSorted_MPIBAIJ(C, ismax, isrow,isrow_new);CHKERRQ(ierr);
692   ierr = MatCompressIndicesSorted_MPIBAIJ(C, ismax, iscol,iscol_new);CHKERRQ(ierr);
693 
694   /* Allocate memory to hold all the submatrices */
695   if (scall != MAT_REUSE_MATRIX) {
696     *submat = (Mat *)PetscMalloc((ismax+1)*sizeof(Mat));CHKPTRQ(*submat);
697   }
698   /* Determine the number of stages through which submatrices are done */
699   nmax          = 20*1000000 / (c->Nbs * sizeof(int));
700   if (nmax == 0) nmax = 1;
701   nstages_local = ismax/nmax + ((ismax % nmax)?1:0);
702 
703   /* Make sure every porcessor loops through the nstages */
704   ierr = MPI_Allreduce(&nstages_local,&nstages,1,MPI_INT,MPI_MAX,C->comm);CHKERRQ(ierr);
705 
706   for ( i=0,pos=0; i<nstages; i++ ) {
707     if (pos+nmax <= ismax) max_no = nmax;
708     else if (pos == ismax) max_no = 0;
709     else                   max_no = ismax-pos;
710     ierr = MatGetSubMatrices_MPIBAIJ_local(C,max_no,isrow_new+pos,iscol_new+pos,scall,*submat+pos);CHKERRQ(ierr);
711     pos += max_no;
712   }
713 
714   for (i=0; i<ismax; i++) {
715     ierr = ISDestroy(isrow_new[i]);CHKERRQ(ierr);
716     ierr = ISDestroy(iscol_new[i]);CHKERRQ(ierr);
717   }
718   ierr = PetscFree(isrow_new);CHKERRQ(ierr);
719   PetscFunctionReturn(0);
720 }
721 
722 /* -------------------------------------------------------------------------*/
723 #undef __FUNC__
724 #define __FUNC__ "MatGetSubMatrices_MPIBAIJ_local"
725 static int MatGetSubMatrices_MPIBAIJ_local(Mat C,int ismax,IS *isrow,IS *iscol,
726                              MatReuse scall,Mat *submats)
727 {
728   Mat_MPIBAIJ *c = (Mat_MPIBAIJ *) C->data;
729   Mat         A = c->A;
730   Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data, *b = (Mat_SeqBAIJ*)c->B->data, *mat;
731   int         **irow,**icol,*nrow,*ncol,*w1,*w2,*w3,*w4,*rtable,start,end,size;
732   int         **sbuf1,**sbuf2, rank, Mbs,i,j,k,l,ct1,ct2,ierr, **rbuf1,row,proc;
733   int         nrqs, msz, **ptr,index,*req_size,*ctr,*pa,*tmp,tcol,bsz,nrqr;
734   int         **rbuf3,*req_source,**sbuf_aj, **rbuf2, max1,max2,**rmap;
735   int         **cmap,**lens,is_no,ncols,*cols,mat_i,*mat_j,tmp2,jmax,*irow_i;
736   int         len,ctr_j,*sbuf1_j,*sbuf_aj_i,*rbuf1_i,kmax,*cmap_i,*lens_i;
737   int         *rmap_i,bs=c->bs,bs2=c->bs2,*a_j=a->j,*b_j=b->j,*cworkA, *cworkB;
738   int         cstart = c->cstart,nzA,nzB,*a_i=a->i,*b_i=b->i,imark;
739   int         *bmap = c->garray,ctmp,rstart=c->rstart,tag0,tag1,tag2,tag3;
740   MPI_Request *s_waits1,*r_waits1,*s_waits2,*r_waits2,*r_waits3;
741   MPI_Request *r_waits4,*s_waits3,*s_waits4;
742   MPI_Status  *r_status1,*r_status2,*s_status1,*s_status3,*s_status2;
743   MPI_Status  *r_status3,*r_status4,*s_status4;
744   MPI_Comm    comm;
745   Scalar      **rbuf4,**sbuf_aa,*vals,*mat_a,*sbuf_aa_i,*vworkA,*vworkB;
746   Scalar      *a_a=a->a,*b_a=b->a;
747   PetscTruth  flag;
748 
749   PetscFunctionBegin;
750   comm   = C->comm;
751   tag0    = C->tag;
752   size   = c->size;
753   rank   = c->rank;
754   Mbs      = c->Mbs;
755 
756   /* Get some new tags to keep the communication clean */
757   ierr = PetscObjectGetNewTag((PetscObject)C,&tag1);CHKERRQ(ierr);
758   ierr = PetscObjectGetNewTag((PetscObject)C,&tag2);CHKERRQ(ierr);
759   ierr = PetscObjectGetNewTag((PetscObject)C,&tag3);CHKERRQ(ierr);
760 
761   /* Check if the col indices are sorted */
762   for (i=0; i<ismax; i++) {
763     ierr = ISSorted(iscol[i],(PetscTruth*)&j);CHKERRQ(ierr);
764     if (!j) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"IS is not sorted");
765   }
766 
767   len    = (2*ismax+1)*(sizeof(int *) + sizeof(int)) + (Mbs+1)*sizeof(int);
768   irow   = (int **)PetscMalloc(len);CHKPTRQ(irow);
769   icol   = irow + ismax;
770   nrow   = (int *) (icol + ismax);
771   ncol   = nrow + ismax;
772   rtable = ncol + ismax;
773 
774   for (i=0; i<ismax; i++) {
775     ierr = ISGetIndices(isrow[i],&irow[i]);CHKERRQ(ierr);
776     ierr = ISGetIndices(iscol[i],&icol[i]);CHKERRQ(ierr);
777     ierr = ISGetSize(isrow[i],&nrow[i]);CHKERRQ(ierr);
778     ierr = ISGetSize(iscol[i],&ncol[i]);CHKERRQ(ierr);
779   }
780 
781   /* Create hash table for the mapping :row -> proc*/
782   for (i=0,j=0; i<size; i++) {
783     jmax = c->rowners[i+1];
784     for (; j<jmax; j++) {
785       rtable[j] = i;
786     }
787   }
788 
789   /* evaluate communication - mesg to who, length of mesg, and buffer space
790      required. Based on this, buffers are allocated, and data copied into them*/
791   w1     = (int *)PetscMalloc(size*4*sizeof(int));CHKPTRQ(w1); /* mesg size */
792   w2     = w1 + size;      /* if w2[i] marked, then a message to proc i*/
793   w3     = w2 + size;      /* no of IS that needs to be sent to proc i */
794   w4     = w3 + size;      /* temp work space used in determining w1, w2, w3 */
795   ierr   = PetscMemzero(w1,size*3*sizeof(int));CHKERRQ(ierr); /* initialise work vector*/
796   for (i=0; i<ismax; i++) {
797     ierr   = PetscMemzero(w4,size*sizeof(int));CHKERRQ(ierr); /* initialise work vector*/
798     jmax   = nrow[i];
799     irow_i = irow[i];
800     for (j=0; j<jmax; j++) {
801       row  = irow_i[j];
802       proc = rtable[row];
803       w4[proc]++;
804     }
805     for (j=0; j<size; j++) {
806       if (w4[j]) { w1[j] += w4[j];  w3[j]++;}
807     }
808   }
809 
810   nrqs     = 0;              /* no of outgoing messages */
811   msz      = 0;              /* total mesg length for all proc */
812   w1[rank] = 0;              /* no mesg sent to intself */
813   w3[rank] = 0;
814   for (i=0; i<size; i++) {
815     if (w1[i])  { w2[i] = 1; nrqs++;} /* there exists a message to proc i */
816   }
817   pa = (int *)PetscMalloc((nrqs+1)*sizeof(int));CHKPTRQ(pa); /*(proc -array)*/
818   for (i=0, j=0; i<size; i++) {
819     if (w1[i]) { pa[j] = i; j++; }
820   }
821 
822   /* Each message would have a header = 1 + 2*(no of IS) + data */
823   for (i=0; i<nrqs; i++) {
824     j     = pa[i];
825     w1[j] += w2[j] + 2* w3[j];
826     msz   += w1[j];
827   }
828   /* Do a global reduction to determine how many messages to expect*/
829   {
830     int *rw1;
831     rw1  = (int *)PetscMalloc(2*size*sizeof(int));CHKPTRQ(rw1);
832     ierr = MPI_Allreduce(w1, rw1, 2*size, MPI_INT, PetscMaxSum_Op, comm);CHKERRQ(ierr);
833     bsz  = rw1[rank];
834     nrqr = rw1[size+rank];
835     ierr = PetscFree(rw1);CHKERRQ(ierr);
836   }
837 
838   /* Allocate memory for recv buffers . Prob none if nrqr = 0 ???? */
839   len      = (nrqr+1)*sizeof(int*) + nrqr*bsz*sizeof(int);
840   rbuf1    = (int**) PetscMalloc(len);CHKPTRQ(rbuf1);
841   rbuf1[0] = (int *) (rbuf1 + nrqr);
842   for (i=1; i<nrqr; ++i) rbuf1[i] = rbuf1[i-1] + bsz;
843 
844   /* Post the receives */
845   r_waits1 = (MPI_Request *) PetscMalloc((nrqr+1)*sizeof(MPI_Request));CHKPTRQ(r_waits1);
846   for (i=0; i<nrqr; ++i) {
847     ierr = MPI_Irecv(rbuf1[i],bsz,MPI_INT,MPI_ANY_SOURCE,tag0,comm,r_waits1+i);CHKERRQ(ierr);
848   }
849 
850   /* Allocate Memory for outgoing messages */
851   len      = 2*size*sizeof(int*) + 2*msz*sizeof(int) + size*sizeof(int);
852   sbuf1    = (int **)PetscMalloc(len);CHKPTRQ(sbuf1);
853   ptr      = sbuf1 + size;   /* Pointers to the data in outgoing buffers */
854   ierr     = PetscMemzero(sbuf1,2*size*sizeof(int*));CHKERRQ(ierr);
855   /* allocate memory for outgoing data + buf to receive the first reply */
856   tmp      = (int *) (ptr + size);
857   ctr      = tmp + 2*msz;
858 
859   {
860     int *iptr = tmp,ict = 0;
861     for (i=0; i<nrqs; i++) {
862       j         = pa[i];
863       iptr     += ict;
864       sbuf1[j]  = iptr;
865       ict       = w1[j];
866     }
867   }
868 
869   /* Form the outgoing messages */
870   /* Initialise the header space */
871   for (i=0; i<nrqs; i++) {
872     j           = pa[i];
873     sbuf1[j][0] = 0;
874     ierr        = PetscMemzero(sbuf1[j]+1, 2*w3[j]*sizeof(int));CHKERRQ(ierr);
875     ptr[j]      = sbuf1[j] + 2*w3[j] + 1;
876   }
877 
878   /* Parse the isrow and copy data into outbuf */
879   for (i=0; i<ismax; i++) {
880     ierr   = PetscMemzero(ctr,size*sizeof(int));CHKERRQ(ierr);
881     irow_i = irow[i];
882     jmax   = nrow[i];
883     for (j=0; j<jmax; j++) {  /* parse the indices of each IS */
884       row  = irow_i[j];
885       proc = rtable[row];
886       if (proc != rank) { /* copy to the outgoing buf*/
887         ctr[proc]++;
888         *ptr[proc] = row;
889         ptr[proc]++;
890       }
891     }
892     /* Update the headers for the current IS */
893     for (j=0; j<size; j++) { /* Can Optimise this loop too */
894       if ((ctr_j = ctr[j])) {
895         sbuf1_j        = sbuf1[j];
896         k              = ++sbuf1_j[0];
897         sbuf1_j[2*k]   = ctr_j;
898         sbuf1_j[2*k-1] = i;
899       }
900     }
901   }
902 
903   /*  Now  post the sends */
904   s_waits1 = (MPI_Request *) PetscMalloc((nrqs+1)*sizeof(MPI_Request));CHKPTRQ(s_waits1);
905   for (i=0; i<nrqs; ++i) {
906     j = pa[i];
907     ierr = MPI_Isend( sbuf1[j], w1[j], MPI_INT, j, tag0, comm, s_waits1+i);CHKERRQ(ierr);
908   }
909 
910   /* Post Recieves to capture the buffer size */
911   r_waits2 = (MPI_Request *) PetscMalloc((nrqs+1)*sizeof(MPI_Request));CHKPTRQ(r_waits2);
912   rbuf2    = (int**)PetscMalloc((nrqs+1)*sizeof(int *));CHKPTRQ(rbuf2);
913   rbuf2[0] = tmp + msz;
914   for (i=1; i<nrqs; ++i) {
915     j        = pa[i];
916     rbuf2[i] = rbuf2[i-1]+w1[pa[i-1]];
917   }
918   for (i=0; i<nrqs; ++i) {
919     j    = pa[i];
920     ierr = MPI_Irecv( rbuf2[i], w1[j], MPI_INT, j, tag1, comm, r_waits2+i);CHKERRQ(ierr);
921   }
922 
923   /* Send to other procs the buf size they should allocate */
924 
925 
926   /* Receive messages*/
927   s_waits2   = (MPI_Request *) PetscMalloc((nrqr+1)*sizeof(MPI_Request));CHKPTRQ(s_waits2);
928   r_status1  = (MPI_Status *) PetscMalloc((nrqr+1)*sizeof(MPI_Status));CHKPTRQ(r_status1);
929   len        = 2*nrqr*sizeof(int) + (nrqr+1)*sizeof(int*);
930   sbuf2      = (int**) PetscMalloc(len);CHKPTRQ(sbuf2);
931   req_size   = (int *) (sbuf2 + nrqr);
932   req_source = req_size + nrqr;
933 
934   {
935     Mat_SeqBAIJ *sA = (Mat_SeqBAIJ*) c->A->data, *sB = (Mat_SeqBAIJ*) c->B->data;
936     int        *sAi = sA->i, *sBi = sB->i, id, *sbuf2_i;
937 
938     for (i=0; i<nrqr; ++i) {
939       ierr = MPI_Waitany(nrqr, r_waits1, &index, r_status1+i);CHKERRQ(ierr);
940       req_size[index] = 0;
941       rbuf1_i         = rbuf1[index];
942       start           = 2*rbuf1_i[0] + 1;
943       ierr = MPI_Get_count(r_status1+i,MPI_INT, &end);CHKERRQ(ierr);
944       sbuf2[index] = (int *)PetscMalloc(end*sizeof(int));CHKPTRQ(sbuf2[index]);
945       sbuf2_i      = sbuf2[index];
946       for (j=start; j<end; j++) {
947         id               = rbuf1_i[j] - rstart;
948         ncols            = sAi[id+1] - sAi[id] + sBi[id+1] - sBi[id];
949         sbuf2_i[j]       = ncols;
950         req_size[index] += ncols;
951       }
952       req_source[index] = r_status1[i].MPI_SOURCE;
953       /* form the header */
954       sbuf2_i[0]   = req_size[index];
955       for (j=1; j<start; j++) { sbuf2_i[j] = rbuf1_i[j]; }
956       ierr = MPI_Isend(sbuf2_i,end,MPI_INT,req_source[index],tag1,comm,s_waits2+i);CHKERRQ(ierr);
957     }
958   }
959   ierr = PetscFree(r_status1);CHKERRQ(ierr);
960   ierr = PetscFree(r_waits1);CHKERRQ(ierr);
961 
962   /*  recv buffer sizes */
963   /* Receive messages*/
964 
965   rbuf3     = (int**)PetscMalloc((nrqs+1)*sizeof(int*));CHKPTRQ(rbuf3);
966   rbuf4     = (Scalar**)PetscMalloc((nrqs+1)*sizeof(Scalar*));CHKPTRQ(rbuf4);
967   r_waits3  = (MPI_Request *) PetscMalloc((nrqs+1)*sizeof(MPI_Request));CHKPTRQ(r_waits3);
968   r_waits4  = (MPI_Request *) PetscMalloc((nrqs+1)*sizeof(MPI_Request));CHKPTRQ(r_waits4);
969   r_status2 = (MPI_Status *) PetscMalloc((nrqs+1)*sizeof(MPI_Status));CHKPTRQ(r_status2);
970 
971   for (i=0; i<nrqs; ++i) {
972     ierr = MPI_Waitany(nrqs, r_waits2, &index, r_status2+i);CHKERRQ(ierr);
973     rbuf3[index] = (int *)PetscMalloc(rbuf2[index][0]*sizeof(int));CHKPTRQ(rbuf3[index]);
974     rbuf4[index] = (Scalar *)PetscMalloc(rbuf2[index][0]*bs2*sizeof(Scalar));CHKPTRQ(rbuf4[index]);
975     ierr = MPI_Irecv(rbuf3[index],rbuf2[index][0], MPI_INT,
976                      r_status2[i].MPI_SOURCE, tag2, comm, r_waits3+index);CHKERRQ(ierr);
977     ierr = MPI_Irecv(rbuf4[index],rbuf2[index][0]*bs2, MPIU_SCALAR,
978                      r_status2[i].MPI_SOURCE, tag3, comm, r_waits4+index);CHKERRQ(ierr);
979   }
980   ierr = PetscFree(r_status2);CHKERRQ(ierr);
981   ierr = PetscFree(r_waits2);CHKERRQ(ierr);
982 
983   /* Wait on sends1 and sends2 */
984   s_status1 = (MPI_Status *) PetscMalloc((nrqs+1)*sizeof(MPI_Status));CHKPTRQ(s_status1);
985   s_status2 = (MPI_Status *) PetscMalloc((nrqr+1)*sizeof(MPI_Status));CHKPTRQ(s_status2);
986 
987   ierr = MPI_Waitall(nrqs,s_waits1,s_status1);CHKERRQ(ierr);
988   ierr = MPI_Waitall(nrqr,s_waits2,s_status2);CHKERRQ(ierr);
989   ierr = PetscFree(s_status1);CHKERRQ(ierr);
990   ierr = PetscFree(s_status2);CHKERRQ(ierr);
991   ierr = PetscFree(s_waits1);CHKERRQ(ierr);
992   ierr = PetscFree(s_waits2);CHKERRQ(ierr);
993 
994   /* Now allocate buffers for a->j, and send them off */
995   sbuf_aj = (int **)PetscMalloc((nrqr+1)*sizeof(int *));CHKPTRQ(sbuf_aj);
996   for (i=0,j=0; i<nrqr; i++) j += req_size[i];
997   sbuf_aj[0] = (int*) PetscMalloc((j+1)*sizeof(int));CHKPTRQ(sbuf_aj[0]);
998   for (i=1; i<nrqr; i++)  sbuf_aj[i] = sbuf_aj[i-1] + req_size[i-1];
999 
1000   s_waits3 = (MPI_Request *) PetscMalloc((nrqr+1)*sizeof(MPI_Request));CHKPTRQ(s_waits3);
1001   {
1002      for (i=0; i<nrqr; i++) {
1003       rbuf1_i   = rbuf1[i];
1004       sbuf_aj_i = sbuf_aj[i];
1005       ct1       = 2*rbuf1_i[0] + 1;
1006       ct2       = 0;
1007       for (j=1,max1=rbuf1_i[0]; j<=max1; j++) {
1008         kmax = rbuf1[i][2*j];
1009         for (k=0; k<kmax; k++,ct1++) {
1010           row    = rbuf1_i[ct1] - rstart;
1011           nzA    = a_i[row+1] - a_i[row];     nzB = b_i[row+1] - b_i[row];
1012           ncols  = nzA + nzB;
1013           cworkA = a_j + a_i[row]; cworkB = b_j + b_i[row];
1014 
1015           /* load the column indices for this row into cols*/
1016           cols  = sbuf_aj_i + ct2;
1017           for (l=0; l<nzB; l++) {
1018             if ((ctmp = bmap[cworkB[l]]) < cstart)  cols[l] = ctmp;
1019             else break;
1020           }
1021           imark = l;
1022           for (l=0; l<nzA; l++)   cols[imark+l] = cstart + cworkA[l];
1023           for (l=imark; l<nzB; l++) cols[nzA+l] = bmap[cworkB[l]];
1024           ct2 += ncols;
1025         }
1026       }
1027       ierr = MPI_Isend(sbuf_aj_i,req_size[i],MPI_INT,req_source[i],tag2,comm,s_waits3+i);CHKERRQ(ierr);
1028     }
1029   }
1030   r_status3 = (MPI_Status *) PetscMalloc((nrqs+1)*sizeof(MPI_Status));CHKPTRQ(r_status3);
1031   s_status3 = (MPI_Status *) PetscMalloc((nrqr+1)*sizeof(MPI_Status));CHKPTRQ(s_status3);
1032 
1033   /* Allocate buffers for a->a, and send them off */
1034   sbuf_aa = (Scalar **)PetscMalloc((nrqr+1)*sizeof(Scalar *));CHKPTRQ(sbuf_aa);
1035   for (i=0,j=0; i<nrqr; i++) j += req_size[i];
1036   sbuf_aa[0] = (Scalar*) PetscMalloc((j+1)*bs2*sizeof(Scalar));CHKPTRQ(sbuf_aa[0]);
1037   for (i=1; i<nrqr; i++)  sbuf_aa[i] = sbuf_aa[i-1] + req_size[i-1]*bs2;
1038 
1039   s_waits4 = (MPI_Request *) PetscMalloc((nrqr+1)*sizeof(MPI_Request));CHKPTRQ(s_waits4);
1040   {
1041     for (i=0; i<nrqr; i++) {
1042       rbuf1_i   = rbuf1[i];
1043       sbuf_aa_i = sbuf_aa[i];
1044       ct1       = 2*rbuf1_i[0]+1;
1045       ct2       = 0;
1046       for (j=1,max1=rbuf1_i[0]; j<=max1; j++) {
1047         kmax = rbuf1_i[2*j];
1048         for (k=0; k<kmax; k++,ct1++) {
1049           row    = rbuf1_i[ct1] - rstart;
1050           nzA    = a_i[row+1] - a_i[row];     nzB = b_i[row+1] - b_i[row];
1051           ncols  = nzA + nzB;
1052           cworkA = a_j + a_i[row];     cworkB = b_j + b_i[row];
1053           vworkA = a_a + a_i[row]*bs2; vworkB = b_a + b_i[row]*bs2;
1054 
1055           /* load the column values for this row into vals*/
1056           vals  = sbuf_aa_i+ct2*bs2;
1057           for (l=0; l<nzB; l++) {
1058             if ((bmap[cworkB[l]]) < cstart) {
1059               ierr = PetscMemcpy(vals+l*bs2,vworkB+l*bs2,bs2*sizeof(Scalar));CHKERRQ(ierr);
1060             }
1061             else break;
1062           }
1063           imark = l;
1064           for (l=0; l<nzA; l++) {
1065             ierr = PetscMemcpy(vals+(imark+l)*bs2,vworkA+l*bs2,bs2*sizeof(Scalar));CHKERRQ(ierr);
1066           }
1067           for (l=imark; l<nzB; l++) {
1068             ierr = PetscMemcpy(vals+(nzA+l)*bs2,vworkB+l*bs2,bs2*sizeof(Scalar));CHKERRQ(ierr);
1069           }
1070           ct2 += ncols;
1071         }
1072       }
1073       ierr = MPI_Isend(sbuf_aa_i,req_size[i]*bs2,MPIU_SCALAR,req_source[i],tag3,comm,s_waits4+i);CHKERRQ(ierr);
1074     }
1075   }
1076   r_status4 = (MPI_Status *) PetscMalloc((nrqs+1)*sizeof(MPI_Status));CHKPTRQ(r_status4);
1077   s_status4 = (MPI_Status *) PetscMalloc((nrqr+1)*sizeof(MPI_Status));CHKPTRQ(s_status4);
1078   ierr = PetscFree(rbuf1);CHKERRQ(ierr);
1079 
1080   /* Form the matrix */
1081   /* create col map */
1082   {
1083     int *icol_i;
1084 
1085     len     = (1+ismax)*sizeof(int *) + ismax*c->Nbs*sizeof(int);
1086     cmap    = (int **)PetscMalloc(len);CHKPTRQ(cmap);
1087     cmap[0] = (int *)(cmap + ismax);
1088     ierr    = PetscMemzero(cmap[0],(1+ismax*c->Nbs)*sizeof(int));CHKERRQ(ierr);
1089     for (i=1; i<ismax; i++) { cmap[i] = cmap[i-1] + c->Nbs; }
1090     for (i=0; i<ismax; i++) {
1091       jmax   = ncol[i];
1092       icol_i = icol[i];
1093       cmap_i = cmap[i];
1094       for (j=0; j<jmax; j++) {
1095         cmap_i[icol_i[j]] = j+1;
1096       }
1097     }
1098   }
1099 
1100 
1101   /* Create lens which is required for MatCreate... */
1102   for (i=0,j=0; i<ismax; i++) { j += nrow[i]; }
1103   len     = (1+ismax)*sizeof(int *) + j*sizeof(int);
1104   lens    = (int **)PetscMalloc(len);CHKPTRQ(lens);
1105   lens[0] = (int *)(lens + ismax);
1106   ierr    = PetscMemzero(lens[0], j*sizeof(int));CHKERRQ(ierr);
1107   for (i=1; i<ismax; i++) { lens[i] = lens[i-1] + nrow[i-1]; }
1108 
1109   /* Update lens from local data */
1110   for (i=0; i<ismax; i++) {
1111     jmax   = nrow[i];
1112     cmap_i = cmap[i];
1113     irow_i = irow[i];
1114     lens_i = lens[i];
1115     for (j=0; j<jmax; j++) {
1116       row  = irow_i[j];
1117       proc = rtable[row];
1118       if (proc == rank) {
1119         /* Get indices from matA and then from matB */
1120         row    = row - rstart;
1121         nzA    = a_i[row+1] - a_i[row];     nzB = b_i[row+1] - b_i[row];
1122         cworkA =  a_j + a_i[row]; cworkB = b_j + b_i[row];
1123         for (k=0; k<nzA; k++) {
1124           if (cmap_i[cstart + cworkA[k]]) { lens_i[j]++;}
1125         }
1126         for (k=0; k<nzB; k++) {
1127           if (cmap_i[bmap[cworkB[k]]]) { lens_i[j]++;}
1128         }
1129       }
1130     }
1131   }
1132 
1133   /* Create row map*/
1134   len     = (1+ismax)*sizeof(int *) + ismax*c->Mbs*sizeof(int);
1135   rmap    = (int **)PetscMalloc(len);CHKPTRQ(rmap);
1136   rmap[0] = (int *)(rmap + ismax);
1137   ierr    = PetscMemzero(rmap[0],ismax*c->Mbs*sizeof(int));CHKERRQ(ierr);
1138   for (i=1; i<ismax; i++) { rmap[i] = rmap[i-1] + c->Mbs;}
1139   for (i=0; i<ismax; i++) {
1140     rmap_i = rmap[i];
1141     irow_i = irow[i];
1142     jmax   = nrow[i];
1143     for (j=0; j<jmax; j++) {
1144       rmap_i[irow_i[j]] = j;
1145     }
1146   }
1147 
1148   /* Update lens from offproc data */
1149   {
1150     int *rbuf2_i, *rbuf3_i, *sbuf1_i;
1151 
1152     for (tmp2=0; tmp2<nrqs; tmp2++) {
1153       ierr    = MPI_Waitany(nrqs, r_waits3, &i, r_status3+tmp2);CHKERRQ(ierr);
1154       index   = pa[i];
1155       sbuf1_i = sbuf1[index];
1156       jmax    = sbuf1_i[0];
1157       ct1     = 2*jmax+1;
1158       ct2     = 0;
1159       rbuf2_i = rbuf2[i];
1160       rbuf3_i = rbuf3[i];
1161       for (j=1; j<=jmax; j++) {
1162         is_no   = sbuf1_i[2*j-1];
1163         max1    = sbuf1_i[2*j];
1164         lens_i  = lens[is_no];
1165         cmap_i  = cmap[is_no];
1166         rmap_i  = rmap[is_no];
1167         for (k=0; k<max1; k++,ct1++) {
1168           row  = rmap_i[sbuf1_i[ct1]]; /* the val in the new matrix to be */
1169           max2 = rbuf2_i[ct1];
1170           for (l=0; l<max2; l++,ct2++) {
1171             if (cmap_i[rbuf3_i[ct2]]) {
1172               lens_i[row]++;
1173             }
1174           }
1175         }
1176       }
1177     }
1178   }
1179   ierr = PetscFree(r_status3);CHKERRQ(ierr);
1180   ierr = PetscFree(r_waits3);CHKERRQ(ierr);
1181   ierr = MPI_Waitall(nrqr,s_waits3,s_status3);CHKERRQ(ierr);
1182   ierr = PetscFree(s_status3);CHKERRQ(ierr);
1183   ierr = PetscFree(s_waits3);CHKERRQ(ierr);
1184 
1185   /* Create the submatrices */
1186   if (scall == MAT_REUSE_MATRIX) {
1187     /*
1188         Assumes new rows are same length as the old rows, hence bug!
1189     */
1190     for (i=0; i<ismax; i++) {
1191       mat = (Mat_SeqBAIJ *)(submats[i]->data);
1192       if ((mat->mbs != nrow[i]) || (mat->nbs != ncol[i] || mat->bs != bs)) {
1193         SETERRQ(PETSC_ERR_ARG_SIZ,0,"Cannot reuse matrix. wrong size");
1194       }
1195       ierr = PetscMemcmp(mat->ilen,lens[i], mat->mbs *sizeof(int),&flag);CHKERRQ(ierr);
1196       if (flag == PETSC_FALSE) {
1197         SETERRQ(PETSC_ERR_ARG_INCOMP,0,"Cannot reuse matrix. wrong no of nonzeros");
1198       }
1199       /* Initial matrix as if empty */
1200       ierr = PetscMemzero(mat->ilen,mat->mbs*sizeof(int));CHKERRQ(ierr);
1201       submats[i]->factor = C->factor;
1202     }
1203   } else {
1204     for (i=0; i<ismax; i++) {
1205       ierr = MatCreateSeqBAIJ(PETSC_COMM_SELF,a->bs,nrow[i]*bs,ncol[i]*bs,0,lens[i],submats+i);CHKERRQ(ierr);
1206     }
1207   }
1208 
1209   /* Assemble the matrices */
1210   /* First assemble the local rows */
1211   {
1212     int    ilen_row,*imat_ilen, *imat_j, *imat_i;
1213     Scalar *imat_a;
1214 
1215     for (i=0; i<ismax; i++) {
1216       mat       = (Mat_SeqBAIJ *) submats[i]->data;
1217       imat_ilen = mat->ilen;
1218       imat_j    = mat->j;
1219       imat_i    = mat->i;
1220       imat_a    = mat->a;
1221       cmap_i    = cmap[i];
1222       rmap_i    = rmap[i];
1223       irow_i    = irow[i];
1224       jmax      = nrow[i];
1225       for (j=0; j<jmax; j++) {
1226         row      = irow_i[j];
1227         proc     = rtable[row];
1228         if (proc == rank) {
1229           row      = row - rstart;
1230           nzA      = a_i[row+1] - a_i[row];
1231           nzB      = b_i[row+1] - b_i[row];
1232           cworkA   = a_j + a_i[row];
1233           cworkB   = b_j + b_i[row];
1234           vworkA   = a_a + a_i[row]*bs2;
1235           vworkB   = b_a + b_i[row]*bs2;
1236 
1237           row      = rmap_i[row + rstart];
1238           mat_i    = imat_i[row];
1239           mat_a    = imat_a + mat_i*bs2;
1240           mat_j    = imat_j + mat_i;
1241           ilen_row = imat_ilen[row];
1242 
1243           /* load the column indices for this row into cols*/
1244           for (l=0; l<nzB; l++) {
1245             if ((ctmp = bmap[cworkB[l]]) < cstart) {
1246               if ((tcol = cmap_i[ctmp])) {
1247                 *mat_j++ = tcol - 1;
1248                 ierr     = PetscMemcpy(mat_a,vworkB+l*bs2,bs2*sizeof(Scalar));CHKERRQ(ierr);
1249                 mat_a   += bs2;
1250                 ilen_row++;
1251               }
1252             } else break;
1253           }
1254           imark = l;
1255           for (l=0; l<nzA; l++) {
1256             if ((tcol = cmap_i[cstart + cworkA[l]])) {
1257               *mat_j++ = tcol - 1;
1258               ierr     = PetscMemcpy(mat_a,vworkA+l*bs2,bs2*sizeof(Scalar));CHKERRQ(ierr);
1259               mat_a   += bs2;
1260               ilen_row++;
1261             }
1262           }
1263           for (l=imark; l<nzB; l++) {
1264             if ((tcol = cmap_i[bmap[cworkB[l]]])) {
1265               *mat_j++ = tcol - 1;
1266               ierr     = PetscMemcpy(mat_a,vworkB+l*bs2,bs2*sizeof(Scalar));CHKERRQ(ierr);
1267               mat_a   += bs2;
1268               ilen_row++;
1269             }
1270           }
1271           imat_ilen[row] = ilen_row;
1272         }
1273       }
1274 
1275     }
1276   }
1277 
1278   /*   Now assemble the off proc rows*/
1279   {
1280     int    *sbuf1_i,*rbuf2_i,*rbuf3_i,*imat_ilen,ilen;
1281     int    *imat_j,*imat_i;
1282     Scalar *imat_a,*rbuf4_i;
1283 
1284     for (tmp2=0; tmp2<nrqs; tmp2++) {
1285       ierr    = MPI_Waitany(nrqs, r_waits4, &i, r_status4+tmp2);CHKERRQ(ierr);
1286       index   = pa[i];
1287       sbuf1_i = sbuf1[index];
1288       jmax    = sbuf1_i[0];
1289       ct1     = 2*jmax + 1;
1290       ct2     = 0;
1291       rbuf2_i = rbuf2[i];
1292       rbuf3_i = rbuf3[i];
1293       rbuf4_i = rbuf4[i];
1294       for (j=1; j<=jmax; j++) {
1295         is_no     = sbuf1_i[2*j-1];
1296         rmap_i    = rmap[is_no];
1297         cmap_i    = cmap[is_no];
1298         mat       = (Mat_SeqBAIJ *) submats[is_no]->data;
1299         imat_ilen = mat->ilen;
1300         imat_j    = mat->j;
1301         imat_i    = mat->i;
1302         imat_a    = mat->a;
1303         max1      = sbuf1_i[2*j];
1304         for (k=0; k<max1; k++, ct1++) {
1305           row   = sbuf1_i[ct1];
1306           row   = rmap_i[row];
1307           ilen  = imat_ilen[row];
1308           mat_i = imat_i[row];
1309           mat_a = imat_a + mat_i*bs2;
1310           mat_j = imat_j + mat_i;
1311           max2 = rbuf2_i[ct1];
1312           for (l=0; l<max2; l++,ct2++) {
1313             if ((tcol = cmap_i[rbuf3_i[ct2]])) {
1314               *mat_j++    = tcol - 1;
1315               /* *mat_a++= rbuf4_i[ct2]; */
1316               ierr        = PetscMemcpy(mat_a,rbuf4_i+ct2*bs2,bs2*sizeof(Scalar));CHKERRQ(ierr);
1317               mat_a      += bs2;
1318               ilen++;
1319             }
1320           }
1321           imat_ilen[row] = ilen;
1322         }
1323       }
1324     }
1325   }
1326   ierr = PetscFree(r_status4);CHKERRQ(ierr);
1327   ierr = PetscFree(r_waits4);CHKERRQ(ierr);
1328   ierr = MPI_Waitall(nrqr,s_waits4,s_status4);CHKERRQ(ierr);
1329   ierr = PetscFree(s_waits4);CHKERRQ(ierr);
1330   ierr = PetscFree(s_status4);CHKERRQ(ierr);
1331 
1332   /* Restore the indices */
1333   for (i=0; i<ismax; i++) {
1334     ierr = ISRestoreIndices(isrow[i], irow+i);CHKERRQ(ierr);
1335     ierr = ISRestoreIndices(iscol[i], icol+i);CHKERRQ(ierr);
1336   }
1337 
1338   /* Destroy allocated memory */
1339   ierr = PetscFree(irow);CHKERRQ(ierr);
1340   ierr = PetscFree(w1);CHKERRQ(ierr);
1341   ierr = PetscFree(pa);CHKERRQ(ierr);
1342 
1343   ierr = PetscFree(sbuf1);CHKERRQ(ierr);
1344   ierr = PetscFree(rbuf2);CHKERRQ(ierr);
1345   for (i=0; i<nrqr; ++i) {
1346     ierr = PetscFree(sbuf2[i]);CHKERRQ(ierr);
1347   }
1348   for (i=0; i<nrqs; ++i) {
1349     ierr = PetscFree(rbuf3[i]);CHKERRQ(ierr);
1350     ierr = PetscFree(rbuf4[i]);CHKERRQ(ierr);
1351   }
1352 
1353   ierr = PetscFree(sbuf2);CHKERRQ(ierr);
1354   ierr = PetscFree(rbuf3);CHKERRQ(ierr);
1355   ierr = PetscFree(rbuf4 );CHKERRQ(ierr);
1356   ierr = PetscFree(sbuf_aj[0]);CHKERRQ(ierr);
1357   ierr = PetscFree(sbuf_aj);CHKERRQ(ierr);
1358   ierr = PetscFree(sbuf_aa[0]);CHKERRQ(ierr);
1359   ierr = PetscFree(sbuf_aa);CHKERRQ(ierr);
1360 
1361   ierr = PetscFree(cmap);CHKERRQ(ierr);
1362   ierr = PetscFree(rmap);CHKERRQ(ierr);
1363   ierr = PetscFree(lens);CHKERRQ(ierr);
1364 
1365   for (i=0; i<ismax; i++) {
1366     ierr = MatAssemblyBegin(submats[i], MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1367     ierr = MatAssemblyEnd(submats[i], MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1368   }
1369 
1370   ierr = PetscObjectRestoreNewTag((PetscObject)C,&tag3);CHKERRQ(ierr);
1371   ierr = PetscObjectRestoreNewTag((PetscObject)C,&tag2);CHKERRQ(ierr);
1372   ierr = PetscObjectRestoreNewTag((PetscObject)C,&tag1);CHKERRQ(ierr);
1373 
1374   PetscFunctionReturn(0);
1375 }
1376 
1377 
1378