xref: /petsc/src/mat/impls/dense/mpi/mmdense.c (revision ef66eb6987ddfdf4e414d6b820cbc8d8d7d17bc2)
1 /*$Id: mmdense.c,v 1.39 2001/08/07 03:02:46 balay Exp bsmith $*/
2 
3 /*
4    Support for the parallel dense matrix vector multiply
5 */
6 #include "src/mat/impls/dense/mpi/mpidense.h"
7 #include "src/vec/vecimpl.h"
8 
9 #undef __FUNCT__
10 #define __FUNCT__ "MatSetUpMultiply_MPIDense"
11 int MatSetUpMultiply_MPIDense(Mat mat)
12 {
13   Mat_MPIDense *mdn = (Mat_MPIDense*)mat->data;
14   int          ierr;
15   IS           from,to;
16   Vec          gvec;
17 
18   PetscFunctionBegin;
19   /* Create local vector that is used to scatter into */
20   ierr = VecCreateSeq(PETSC_COMM_SELF,mat->N,&mdn->lvec);CHKERRQ(ierr);
21 
22   /* Create temporary index set for building scatter gather */
23   ierr = ISCreateStride(mat->comm,mat->N,0,1,&from);CHKERRQ(ierr);
24   ierr = ISCreateStride(PETSC_COMM_SELF,mat->N,0,1,&to);CHKERRQ(ierr);
25 
26   /* Create temporary global vector to generate scatter context */
27   /* n    = mdn->cowners[mdn->rank+1] - mdn->cowners[mdn->rank]; */
28 
29   ierr = VecCreateMPI(mat->comm,mdn->nvec,mat->N,&gvec);CHKERRQ(ierr);
30 
31   /* Generate the scatter context */
32   ierr = VecScatterCreate(gvec,from,mdn->lvec,to,&mdn->Mvctx);CHKERRQ(ierr);
33   PetscLogObjectParent(mat,mdn->Mvctx);
34   PetscLogObjectParent(mat,mdn->lvec);
35   PetscLogObjectParent(mat,from);
36   PetscLogObjectParent(mat,to);
37   PetscLogObjectParent(mat,gvec);
38 
39   ierr = ISDestroy(to);CHKERRQ(ierr);
40   ierr = ISDestroy(from);CHKERRQ(ierr);
41   ierr = VecDestroy(gvec);CHKERRQ(ierr);
42   PetscFunctionReturn(0);
43 }
44 
45 EXTERN int MatGetSubMatrices_MPIDense_Local(Mat,int,IS*,IS*,MatReuse,Mat*);
46 #undef __FUNCT__
47 #define __FUNCT__ "MatGetSubMatrices_MPIDense"
48 int MatGetSubMatrices_MPIDense(Mat C,int ismax,IS *isrow,IS *iscol,MatReuse scall,Mat **submat)
49 {
50   int           nmax,nstages_local,nstages,i,pos,max_no,ierr;
51 
52   PetscFunctionBegin;
53   /* Allocate memory to hold all the submatrices */
54   if (scall != MAT_REUSE_MATRIX) {
55     ierr = PetscMalloc((ismax+1)*sizeof(Mat),submat);CHKERRQ(ierr);
56   }
57   /* Determine the number of stages through which submatrices are done */
58   nmax          = 20*1000000 / (C->N * sizeof(int));
59   if (!nmax) nmax = 1;
60   nstages_local = ismax/nmax + ((ismax % nmax)?1:0);
61 
62   /* Make sure every processor loops through the nstages */
63   ierr = MPI_Allreduce(&nstages_local,&nstages,1,MPI_INT,MPI_MAX,C->comm);CHKERRQ(ierr);
64 
65 
66   for (i=0,pos=0; i<nstages; i++) {
67     if (pos+nmax <= ismax) max_no = nmax;
68     else if (pos == ismax) max_no = 0;
69     else                   max_no = ismax-pos;
70     ierr = MatGetSubMatrices_MPIDense_Local(C,max_no,isrow+pos,iscol+pos,scall,*submat+pos);CHKERRQ(ierr);
71     pos += max_no;
72   }
73   PetscFunctionReturn(0);
74 }
75 /* -------------------------------------------------------------------------*/
76 #undef __FUNCT__
77 #define __FUNCT__ "MatGetSubMatrices_MPIDense_Local"
78 int MatGetSubMatrices_MPIDense_Local(Mat C,int ismax,IS *isrow,IS *iscol,MatReuse scall,Mat *submats)
79 {
80   Mat_MPIDense  *c = (Mat_MPIDense*)C->data;
81   Mat           A = c->A;
82   Mat_SeqDense  *a = (Mat_SeqDense*)A->data,*mat;
83   int           N = C->N,rstart = c->rstart,count;
84   int           **irow,**icol,*nrow,*ncol,*w1,*w2,*w3,*w4,*rtable,start,end,size;
85   int           **sbuf1,rank,m,i,j,k,l,ct1,ierr,**rbuf1,row,proc;
86   int           nrqs,msz,**ptr,index,*ctr,*pa,*tmp,bsz,nrqr;
87   int           is_no,jmax,*irow_i,**rmap,*rmap_i;
88   int           len,ctr_j,*sbuf1_j,*rbuf1_i;
89   int           tag0,tag1;
90   MPI_Request   *s_waits1,*r_waits1,*s_waits2,*r_waits2;
91   MPI_Status    *r_status1,*r_status2,*s_status1,*s_status2;
92   MPI_Comm      comm;
93   PetscScalar   **rbuf2,**sbuf2;
94 
95   PetscFunctionBegin;
96   comm   = C->comm;
97   tag0   = C->tag;
98   size   = c->size;
99   rank   = c->rank;
100   m      = C->M;
101 
102   /* Get some new tags to keep the communication clean */
103   ierr = PetscObjectGetNewTag((PetscObject)C,&tag1);CHKERRQ(ierr);
104 
105     /* Check if the col indices are sorted */
106   for (i=0; i<ismax; i++) {
107     ierr = ISSorted(isrow[i],(PetscTruth*)&j);CHKERRQ(ierr);
108     if (!j) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
109     ierr = ISSorted(iscol[i],(PetscTruth*)&j);CHKERRQ(ierr);
110     if (!j) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
111   }
112 
113   len    =  2*ismax*(sizeof(int *)+sizeof(int)) + (m+1)*sizeof(int);
114   ierr   = PetscMalloc(len,&irow);CHKERRQ(ierr);
115   icol   = irow + ismax;
116   nrow   = (int*)(icol + ismax);
117   ncol   = nrow + ismax;
118   rtable = ncol + ismax;
119 
120   for (i=0; i<ismax; i++) {
121     ierr = ISGetIndices(isrow[i],&irow[i]);CHKERRQ(ierr);
122     ierr = ISGetIndices(iscol[i],&icol[i]);CHKERRQ(ierr);
123     ierr = ISGetLocalSize(isrow[i],&nrow[i]);CHKERRQ(ierr);
124     ierr = ISGetLocalSize(iscol[i],&ncol[i]);CHKERRQ(ierr);
125   }
126 
127   /* Create hash table for the mapping :row -> proc*/
128   for (i=0,j=0; i<size; i++) {
129     jmax = c->rowners[i+1];
130     for (; j<jmax; j++) {
131       rtable[j] = i;
132     }
133   }
134 
135   /* evaluate communication - mesg to who,length of mesg, and buffer space
136      required. Based on this, buffers are allocated, and data copied into them*/
137   ierr   = PetscMalloc(size*4*sizeof(int),&w1);CHKERRQ(ierr); /* mesg size */
138   w2     = w1 + size;      /* if w2[i] marked, then a message to proc i*/
139   w3     = w2 + size;      /* no of IS that needs to be sent to proc i */
140   w4     = w3 + size;      /* temp work space used in determining w1, w2, w3 */
141   ierr = PetscMemzero(w1,size*3*sizeof(int));CHKERRQ(ierr); /* initialize work vector*/
142   for (i=0; i<ismax; i++) {
143     ierr   = PetscMemzero(w4,size*sizeof(int));CHKERRQ(ierr); /* initialize work vector*/
144     jmax   = nrow[i];
145     irow_i = irow[i];
146     for (j=0; j<jmax; j++) {
147       row  = irow_i[j];
148       proc = rtable[row];
149       w4[proc]++;
150     }
151     for (j=0; j<size; j++) {
152       if (w4[j]) { w1[j] += w4[j];  w3[j]++;}
153     }
154   }
155 
156   nrqs     = 0;              /* no of outgoing messages */
157   msz      = 0;              /* total mesg length (for all procs) */
158   w1[rank] = 0;              /* no mesg sent to self */
159   w3[rank] = 0;
160   for (i=0; i<size; i++) {
161     if (w1[i])  { w2[i] = 1; nrqs++;} /* there exists a message to proc i */
162   }
163   ierr = PetscMalloc((nrqs+1)*sizeof(int),&pa);CHKERRQ(ierr); /*(proc -array)*/
164   for (i=0,j=0; i<size; i++) {
165     if (w1[i]) { pa[j] = i; j++; }
166   }
167 
168   /* Each message would have a header = 1 + 2*(no of IS) + data */
169   for (i=0; i<nrqs; i++) {
170     j     = pa[i];
171     w1[j] += w2[j] + 2* w3[j];
172     msz   += w1[j];
173   }
174   /* Do a global reduction to determine how many messages to expect*/
175   {
176     int *rw1;
177     ierr  = PetscMalloc(2*size*sizeof(int),&rw1);CHKERRQ(ierr);
178     ierr  = MPI_Allreduce(w1,rw1,2*size,MPI_INT,PetscMaxSum_Op,comm);CHKERRQ(ierr);
179     bsz   = rw1[rank];
180     nrqr  = rw1[size+rank];
181     ierr  = PetscFree(rw1);CHKERRQ(ierr);
182   }
183 
184   /* Allocate memory for recv buffers . Prob none if nrqr = 0 ???? */
185   len      = (nrqr+1)*sizeof(int*) + nrqr*bsz*sizeof(int);
186   ierr     = PetscMalloc(len,&rbuf1);CHKERRQ(ierr);
187   rbuf1[0] = (int*)(rbuf1 + nrqr);
188   for (i=1; i<nrqr; ++i) rbuf1[i] = rbuf1[i-1] + bsz;
189 
190   /* Post the receives */
191   ierr = PetscMalloc((nrqr+1)*sizeof(MPI_Request),&r_waits1);CHKERRQ(ierr);
192   for (i=0; i<nrqr; ++i) {
193     ierr = MPI_Irecv(rbuf1[i],bsz,MPI_INT,MPI_ANY_SOURCE,tag0,comm,r_waits1+i);CHKERRQ(ierr);
194   }
195 
196   /* Allocate Memory for outgoing messages */
197   len   = 2*size*sizeof(int*) + 2*msz*sizeof(int)+ size*sizeof(int);
198   ierr  = PetscMalloc(len,&sbuf1);CHKERRQ(ierr);
199   ptr   = sbuf1 + size;   /* Pointers to the data in outgoing buffers */
200   ierr  = PetscMemzero(sbuf1,2*size*sizeof(int*));CHKERRQ(ierr);
201   /* allocate memory for outgoing data + buf to receive the first reply */
202   tmp   = (int*)(ptr + size);
203   ctr   = tmp + 2*msz;
204 
205   {
206     int *iptr = tmp,ict = 0;
207     for (i=0; i<nrqs; i++) {
208       j         = pa[i];
209       iptr     += ict;
210       sbuf1[j]  = iptr;
211       ict       = w1[j];
212     }
213   }
214 
215   /* Form the outgoing messages */
216   /* Initialize the header space */
217   for (i=0; i<nrqs; i++) {
218     j           = pa[i];
219     sbuf1[j][0] = 0;
220     ierr        = PetscMemzero(sbuf1[j]+1,2*w3[j]*sizeof(int));CHKERRQ(ierr);
221     ptr[j]      = sbuf1[j] + 2*w3[j] + 1;
222   }
223 
224   /* Parse the isrow and copy data into outbuf */
225   for (i=0; i<ismax; i++) {
226     ierr = PetscMemzero(ctr,size*sizeof(int));CHKERRQ(ierr);
227     irow_i = irow[i];
228     jmax   = nrow[i];
229     for (j=0; j<jmax; j++) {  /* parse the indices of each IS */
230       row  = irow_i[j];
231       proc = rtable[row];
232       if (proc != rank) { /* copy to the outgoing buf*/
233         ctr[proc]++;
234         *ptr[proc] = row;
235         ptr[proc]++;
236       }
237     }
238     /* Update the headers for the current IS */
239     for (j=0; j<size; j++) { /* Can Optimise this loop too */
240       if ((ctr_j = ctr[j])) {
241         sbuf1_j        = sbuf1[j];
242         k              = ++sbuf1_j[0];
243         sbuf1_j[2*k]   = ctr_j;
244         sbuf1_j[2*k-1] = i;
245       }
246     }
247   }
248 
249   /*  Now  post the sends */
250   ierr = PetscMalloc((nrqs+1)*sizeof(MPI_Request),&s_waits1);CHKERRQ(ierr);
251   for (i=0; i<nrqs; ++i) {
252     j = pa[i];
253     ierr = MPI_Isend(sbuf1[j],w1[j],MPI_INT,j,tag0,comm,s_waits1+i);CHKERRQ(ierr);
254   }
255 
256   /* Post recieves to capture the row_data from other procs */
257   ierr  = PetscMalloc((nrqs+1)*sizeof(MPI_Request),&r_waits2);CHKERRQ(ierr);
258   ierr  = PetscMalloc((nrqs+1)*sizeof(PetscScalar*),&rbuf2);CHKERRQ(ierr);
259   for (i=0; i<nrqs; i++) {
260     j        = pa[i];
261     count    = (w1[j] - (2*sbuf1[j][0] + 1))*N;
262     ierr     = PetscMalloc((count+1)*sizeof(PetscScalar),&rbuf2[i]);CHKERRQ(ierr);
263     ierr     = MPI_Irecv(rbuf2[i],count,MPIU_SCALAR,j,tag1,comm,r_waits2+i);CHKERRQ(ierr);
264   }
265 
266   /* Receive messages(row_nos) and then, pack and send off the rowvalues
267      to the correct processors */
268 
269   ierr = PetscMalloc((nrqr+1)*sizeof(MPI_Request),&s_waits2);CHKERRQ(ierr);
270   ierr = PetscMalloc((nrqr+1)*sizeof(MPI_Status),&r_status1);CHKERRQ(ierr);
271   ierr = PetscMalloc((nrqr+1)*sizeof(PetscScalar*),&sbuf2);CHKERRQ(ierr);
272 
273   {
274     PetscScalar *sbuf2_i,*v_start;
275     int         s_proc;
276     for (i=0; i<nrqr; ++i) {
277       ierr = MPI_Waitany(nrqr,r_waits1,&index,r_status1+i);CHKERRQ(ierr);
278       s_proc          = r_status1[i].MPI_SOURCE; /* send processor */
279       rbuf1_i         = rbuf1[index]; /* Actual message from s_proc */
280       /* no of rows = end - start; since start is array index[], 0index, whel end
281          is length of the buffer - which is 1index */
282       start           = 2*rbuf1_i[0] + 1;
283       ierr            = MPI_Get_count(r_status1+i,MPI_INT,&end);CHKERRQ(ierr);
284       /* allocate memory sufficinet to hold all the row values */
285       ierr = PetscMalloc((end-start)*N*sizeof(PetscScalar),&sbuf2[index]);CHKERRQ(ierr);
286       sbuf2_i      = sbuf2[index];
287       /* Now pack the data */
288       for (j=start; j<end; j++) {
289         row = rbuf1_i[j] - rstart;
290         v_start = a->v + row;
291         for (k=0; k<N; k++) {
292           sbuf2_i[0] = v_start[0];
293           sbuf2_i++; v_start += C->m;
294         }
295       }
296       /* Now send off the data */
297       ierr = MPI_Isend(sbuf2[index],(end-start)*N,MPIU_SCALAR,s_proc,tag1,comm,s_waits2+i);CHKERRQ(ierr);
298     }
299   }
300   /* End Send-Recv of IS + row_numbers */
301   ierr = PetscFree(r_status1);CHKERRQ(ierr);
302   ierr = PetscFree(r_waits1);CHKERRQ(ierr);
303   ierr = PetscMalloc((nrqs+1)*sizeof(MPI_Status),&s_status1);CHKERRQ(ierr);
304   ierr      = MPI_Waitall(nrqs,s_waits1,s_status1);CHKERRQ(ierr);
305   ierr = PetscFree(s_status1);CHKERRQ(ierr);
306   ierr = PetscFree(s_waits1);CHKERRQ(ierr);
307 
308   /* Create the submatrices */
309   if (scall == MAT_REUSE_MATRIX) {
310     for (i=0; i<ismax; i++) {
311       mat = (Mat_SeqDense *)(submats[i]->data);
312       if ((submats[i]->m != nrow[i]) || (submats[i]->n != ncol[i])) {
313         SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
314       }
315       ierr = PetscMemzero(mat->v,submats[i]->m*submats[i]->n*sizeof(PetscScalar));CHKERRQ(ierr);
316       submats[i]->factor = C->factor;
317     }
318   } else {
319     for (i=0; i<ismax; i++) {
320       ierr = MatCreateSeqDense(PETSC_COMM_SELF,nrow[i],ncol[i],PETSC_NULL,submats+i);CHKERRQ(ierr);
321     }
322   }
323 
324   /* Assemble the matrices */
325   {
326     int         col;
327     PetscScalar *imat_v,*mat_v,*imat_vi,*mat_vi;
328 
329     for (i=0; i<ismax; i++) {
330       mat       = (Mat_SeqDense*)submats[i]->data;
331       mat_v     = a->v;
332       imat_v    = mat->v;
333       irow_i    = irow[i];
334       m         = nrow[i];
335       for (j=0; j<m; j++) {
336         row      = irow_i[j] ;
337         proc     = rtable[row];
338         if (proc == rank) {
339           row      = row - rstart;
340           mat_vi   = mat_v + row;
341           imat_vi  = imat_v + j;
342           for (k=0; k<ncol[i]; k++) {
343             col = icol[i][k];
344             imat_vi[k*m] = mat_vi[col*C->m];
345           }
346         }
347       }
348     }
349   }
350 
351   /* Create row map. This maps c->row to submat->row for each submat*/
352   /* this is a very expensive operation wrt memory usage */
353   len     = (1+ismax)*sizeof(int*)+ ismax*C->M*sizeof(int);
354   ierr    = PetscMalloc(len,&rmap);CHKERRQ(ierr);
355   rmap[0] = (int *)(rmap + ismax);
356   ierr    = PetscMemzero(rmap[0],ismax*C->M*sizeof(int));CHKERRQ(ierr);
357   for (i=1; i<ismax; i++) { rmap[i] = rmap[i-1] + C->M;}
358   for (i=0; i<ismax; i++) {
359     rmap_i = rmap[i];
360     irow_i = irow[i];
361     jmax   = nrow[i];
362     for (j=0; j<jmax; j++) {
363       rmap_i[irow_i[j]] = j;
364     }
365   }
366 
367   /* Now Receive the row_values and assemble the rest of the matrix */
368   ierr = PetscMalloc((nrqs+1)*sizeof(MPI_Status),&r_status2);CHKERRQ(ierr);
369 
370   {
371     int         is_max,tmp1,col,*sbuf1_i,is_sz;
372     PetscScalar *rbuf2_i,*imat_v,*imat_vi;
373 
374     for (tmp1=0; tmp1<nrqs; tmp1++) { /* For each message */
375       ierr = MPI_Waitany(nrqs,r_waits2,&i,r_status2+tmp1);CHKERRQ(ierr);
376       /* Now dig out the corresponding sbuf1, which contains the IS data_structure */
377       sbuf1_i = sbuf1[pa[i]];
378       is_max  = sbuf1_i[0];
379       ct1     = 2*is_max+1;
380       rbuf2_i = rbuf2[i];
381       for (j=1; j<=is_max; j++) { /* For each IS belonging to the message */
382         is_no     = sbuf1_i[2*j-1];
383         is_sz     = sbuf1_i[2*j];
384         mat       = (Mat_SeqDense*)submats[is_no]->data;
385         imat_v    = mat->v;
386         rmap_i    = rmap[is_no];
387         m         = nrow[is_no];
388         for (k=0; k<is_sz; k++,rbuf2_i+=N) {  /* For each row */
389           row      = sbuf1_i[ct1]; ct1++;
390           row      = rmap_i[row];
391           imat_vi  = imat_v + row;
392           for (l=0; l<ncol[is_no]; l++) { /* For each col */
393             col = icol[is_no][l];
394             imat_vi[l*m] = rbuf2_i[col];
395           }
396         }
397       }
398     }
399   }
400   /* End Send-Recv of row_values */
401   ierr = PetscFree(r_status2);CHKERRQ(ierr);
402   ierr = PetscFree(r_waits2);CHKERRQ(ierr);
403   ierr = PetscMalloc((nrqr+1)*sizeof(MPI_Status),&s_status2);CHKERRQ(ierr);
404   ierr = MPI_Waitall(nrqr,s_waits2,s_status2);CHKERRQ(ierr);
405   ierr = PetscFree(s_status2);CHKERRQ(ierr);
406   ierr = PetscFree(s_waits2);CHKERRQ(ierr);
407 
408   /* Restore the indices */
409   for (i=0; i<ismax; i++) {
410     ierr = ISRestoreIndices(isrow[i],irow+i);CHKERRQ(ierr);
411     ierr = ISRestoreIndices(iscol[i],icol+i);CHKERRQ(ierr);
412   }
413 
414   /* Destroy allocated memory */
415   ierr = PetscFree(irow);CHKERRQ(ierr);
416   ierr = PetscFree(w1);CHKERRQ(ierr);
417   ierr = PetscFree(pa);CHKERRQ(ierr);
418 
419 
420   for (i=0; i<nrqs; ++i) {
421     ierr = PetscFree(rbuf2[i]);CHKERRQ(ierr);
422   }
423   ierr = PetscFree(rbuf2);CHKERRQ(ierr);
424   ierr = PetscFree(sbuf1);CHKERRQ(ierr);
425   ierr = PetscFree(rbuf1);CHKERRQ(ierr);
426 
427   for (i=0; i<nrqr; ++i) {
428     ierr = PetscFree(sbuf2[i]);CHKERRQ(ierr);
429   }
430 
431   ierr = PetscFree(sbuf2);CHKERRQ(ierr);
432   ierr = PetscFree(rmap);CHKERRQ(ierr);
433 
434   for (i=0; i<ismax; i++) {
435     ierr = MatAssemblyBegin(submats[i],MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
436     ierr = MatAssemblyEnd(submats[i],MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
437   }
438 
439   PetscFunctionReturn(0);
440 }
441 
442