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