xref: /petsc/src/mat/impls/aij/mpi/mmaij.c (revision 1eb62cbb64d64b61ce4c1a3ec8cb7777b3ff11c2)
1 
2 /*
3    Support for the parallel AIJ matrix vector multiply
4 */
5 #include "mpiaij.h"
6 #include "vec/vecimpl.h"
7 #include "../seq/aij.h"
8 
9 int MPIAIJSetUpMultiply(Mat mat)
10 {
11   Matimpiaij *aij = (Matimpiaij *) mat->data;
12   Matiaij    *B = (Matiaij *) (aij->B->data);
13   int        N = aij->N,i,j,*indices,n = B->i[B->m] - 1,*aj = B->j;
14   int        ierr,ec = 0,*garray;
15   IS         from,to;
16   Vec        gvec;
17 
18   /* For the first stab we make an array as long as the number of columns */
19   /* mark those columns that are in aij->B */
20   indices = (int *) MALLOC( N*sizeof(int) ); CHKPTR(indices);
21   MEMSET(indices,0,N*sizeof(int));
22   for ( i=0; i<n; i++ ) {
23     if (aj[i]) { if (!indices[aj[i]-1]) ec++; indices[aj[i]-1] = 1;}
24   }
25 
26   /* form array of columns we need */
27   garray = (int *) MALLOC( ec*sizeof(int) ); CHKPTR(garray);
28   ec = 0;
29   for ( i=0; i<N; i++ ) {
30     if (indices[i]) garray[ec++] = i;
31   }
32 
33   /* make indices now point into garray */
34   for ( i=0; i<ec; i++ ) {
35     indices[garray[i]] = i+1;
36   }
37 
38   /* compact out the extra columns in B */
39   for ( i=0; i<n; i++ ) {
40     if (aj[i]) aj[i] = indices[aj[i]-1];
41   }
42   FREE(indices);
43 
44   /* create local vector that is used to scatter into */
45   ierr = VecCreateSequential(ec,&aij->lvec); CHKERR(ierr);
46 
47   /* create two temporary Index sets fot build scatter gather */
48   ierr = ISCreateSequential(ec,garray,&from); CHKERR(ierr);
49   ierr = ISCreateStrideSequential(ec,0,1,&to); CHKERR(ierr);
50 
51   /* create temporary global vector to generate scatter context */
52   /* this is inefficient, but otherwise we must do either
53      1) save garray until the first actual scatter when the vector is known or
54      2) have another way of generating a scatter context without a vector.*/
55   ierr = VecCreateMPI(aij->comm,aij->n,aij->N,&gvec); CHKERR(ierr);
56 
57   /* gnerate the scatter context */
58   ierr = VecScatterCtxCreate(gvec,from,aij->lvec,to,&aij->Mvctx); CHKERR(ierr);
59 
60   FREE(garray);
61   ierr = ISDestroy(from); CHKERR(ierr);
62   ierr = ISDestroy(to); CHKERR(ierr);
63   ierr = VecDestroy(gvec);
64   return 0;
65 }
66