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