xref: /petsc/src/mat/impls/aij/mpi/mmaij.c (revision 2066d6f7fcf6e0c577de7fa8ad8ff6c6226a123c)
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*2066d6f7SSatish Balay static char vcid[] = "$Id: mmaij.c,v 1.38 1997/10/19 03:25:26 bsmith Exp balay $";
3cb512458SBarry Smith #endif
48c79f6d3SBarry Smith 
5d6dfbf8fSBarry Smith 
68c79f6d3SBarry Smith /*
78c79f6d3SBarry Smith    Support for the parallel AIJ matrix vector multiply
88c79f6d3SBarry Smith */
970f55243SBarry Smith #include "src/mat/impls/aij/mpi/mpiaij.h"
10f5eb4b81SSatish Balay #include "src/vec/vecimpl.h"
118c79f6d3SBarry Smith 
125615d1e5SSatish Balay #undef __FUNC__
135615d1e5SSatish Balay #define __FUNC__ "MatSetUpMultiply_MPIAIJ"
1444a69424SLois Curfman McInnes int MatSetUpMultiply_MPIAIJ(Mat mat)
158c79f6d3SBarry Smith {
1644a69424SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data;
17ec8511deSBarry Smith   Mat_SeqAIJ *B = (Mat_SeqAIJ *) (aij->B->data);
18416022c9SBarry Smith   int        N = aij->N,i,j,*indices,*aj = B->j,ierr,ec = 0,*garray;
19416022c9SBarry Smith   int        shift = B->indexshift;
201eb62cbbSBarry Smith   IS         from,to;
211eb62cbbSBarry Smith   Vec        gvec;
228c79f6d3SBarry Smith 
23*2066d6f7SSatish Balay #if defined (USE_CTABLE)
24*2066d6f7SSatish Balay     Table gid1_lid1;
25*2066d6f7SSatish Balay     CTablePos tpos;
26*2066d6f7SSatish Balay     int gid, lid;
27*2066d6f7SSatish Balay #endif
28*2066d6f7SSatish Balay 
293a40ed3dSBarry Smith   PetscFunctionBegin;
30*2066d6f7SSatish Balay 
31*2066d6f7SSatish Balay #if defined (USE_TABLE)
32*2066d6f7SSatish Balay   /* use a table - Mark Adams (this has not been tested with "shift") */
33*2066d6f7SSatish Balay   TableCreate( &gid1_lid1, B->m );
34*2066d6f7SSatish Balay   for ( i=0; i<B->m; i++ ) {
35*2066d6f7SSatish Balay     for ( j=0; j<B->ilen[i]; j++ ) {
36*2066d6f7SSatish Balay       int gid1 = aj[B->i[i] + shift + j] + 1;
37*2066d6f7SSatish Balay       if ( !TableFind( gid1_lid1, gid1 ) ){
38*2066d6f7SSatish Balay         /* one based table */
39*2066d6f7SSatish Balay         ierr = TableAdd( gid1_lid1, gid1, ++ec ); CHKERRQ(ierr);
40*2066d6f7SSatish Balay       }
41*2066d6f7SSatish Balay     }
42*2066d6f7SSatish Balay   }
43*2066d6f7SSatish Balay   /* form array of columns we need */
44*2066d6f7SSatish Balay   garray = (int *) PetscMalloc( (ec+1)*sizeof(int) ); CHKPTRQ(garray);
45*2066d6f7SSatish Balay   ierr = TableGetHeadPosition( gid1_lid1, &tpos ); CHKERRQ(ierr);
46*2066d6f7SSatish Balay   while( tpos ) {
47*2066d6f7SSatish Balay     ierr = TableGetNext( gid1_lid1, &tpos, &gid, &lid ); CHKERRQ(ierr);
48*2066d6f7SSatish Balay     gid--; lid--;
49*2066d6f7SSatish Balay     garray[lid] = gid;
50*2066d6f7SSatish Balay   }
51*2066d6f7SSatish Balay   qsort( garray, ec, sizeof(int), intcomparc ); /* sort, and rebuild */
52*2066d6f7SSatish Balay   TableRemoveAll( gid1_lid1 );
53*2066d6f7SSatish Balay   for ( i=0; i<ec; i++ ) {
54*2066d6f7SSatish Balay     ierr = TableAdd( gid1_lid1, garray[i] + 1, i + 1 ); CHKERRQ(ierr);
55*2066d6f7SSatish Balay   }
56*2066d6f7SSatish Balay   /* compact out the extra columns in B */
57*2066d6f7SSatish Balay   for ( i=0; i<B->m; i++ ) {
58*2066d6f7SSatish Balay     for ( j=0; j<B->ilen[i]; j++ ) {
59*2066d6f7SSatish Balay       int gid1 = aj[B->i[i] + shift + j] + 1;
60*2066d6f7SSatish Balay       lid = TableFind( gid1_lid1, gid1 ) - 1;
61*2066d6f7SSatish Balay       aj[B->i[i] + shift + j] = lid;
62*2066d6f7SSatish Balay     }
63*2066d6f7SSatish Balay   }
64*2066d6f7SSatish Balay   B->n = aij->B->n = aij->B->N = ec;
65*2066d6f7SSatish Balay   TableDelete(gid1_lid1);
66*2066d6f7SSatish Balay   /* Mark Adams */
67*2066d6f7SSatish Balay #else
688c79f6d3SBarry Smith   /* For the first stab we make an array as long as the number of columns */
691eb62cbbSBarry Smith   /* mark those columns that are in aij->B */
70ac03b0baSBarry Smith   indices = (int *) PetscMalloc( (N+1)*sizeof(int) ); CHKPTRQ(indices);
71cddf8d76SBarry Smith   PetscMemzero(indices,N*sizeof(int));
72d6dfbf8fSBarry Smith   for ( i=0; i<B->m; i++ ) {
73d6dfbf8fSBarry Smith     for ( j=0; j<B->ilen[i]; j++ ) {
74dbb450caSBarry Smith       if (!indices[aj[B->i[i] +shift + j] + shift]) ec++;
75416022c9SBarry Smith       indices[aj[B->i[i] + shift + j] + shift] = 1;
76416022c9SBarry Smith     }
771eb62cbbSBarry Smith   }
788c79f6d3SBarry Smith 
791eb62cbbSBarry Smith   /* form array of columns we need */
800452661fSBarry Smith   garray = (int *) PetscMalloc( (ec+1)*sizeof(int) ); CHKPTRQ(garray);
811eb62cbbSBarry Smith   ec = 0;
821eb62cbbSBarry Smith   for ( i=0; i<N; i++ ) {
831eb62cbbSBarry Smith     if (indices[i]) garray[ec++] = i;
841eb62cbbSBarry Smith   }
851eb62cbbSBarry Smith 
861eb62cbbSBarry Smith   /* make indices now point into garray */
871eb62cbbSBarry Smith   for ( i=0; i<ec; i++ ) {
88dbb450caSBarry Smith     indices[garray[i]] = i-shift;
891eb62cbbSBarry Smith   }
901eb62cbbSBarry Smith 
911eb62cbbSBarry Smith   /* compact out the extra columns in B */
92d6dfbf8fSBarry Smith   for ( i=0; i<B->m; i++ ) {
93d6dfbf8fSBarry Smith     for ( j=0; j<B->ilen[i]; j++ ) {
94dbb450caSBarry Smith       aj[B->i[i] + shift + j] = indices[aj[B->i[i] + shift + j]+shift];
951eb62cbbSBarry Smith     }
96d6dfbf8fSBarry Smith   }
97639f9d9dSBarry Smith   B->n = aij->B->n = aij->B->N = ec;
980452661fSBarry Smith   PetscFree(indices);
99*2066d6f7SSatish Balay #endif
1001eb62cbbSBarry Smith   /* create local vector that is used to scatter into */
101029af93fSBarry Smith   ierr = VecCreateSeq(PETSC_COMM_SELF,ec,&aij->lvec); CHKERRQ(ierr);
1021eb62cbbSBarry Smith 
103d6dfbf8fSBarry Smith   /* create two temporary Index sets for build scatter gather */
104029af93fSBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,ec,garray,&from); CHKERRQ(ierr);
105029af93fSBarry Smith   ierr = ISCreateStride(PETSC_COMM_SELF,ec,0,1,&to); CHKERRQ(ierr);
1061eb62cbbSBarry Smith 
1071eb62cbbSBarry Smith   /* create temporary global vector to generate scatter context */
1081eb62cbbSBarry Smith   /* this is inefficient, but otherwise we must do either
1091eb62cbbSBarry Smith      1) save garray until the first actual scatter when the vector is known or
1101eb62cbbSBarry Smith      2) have another way of generating a scatter context without a vector.*/
11178b31e54SBarry Smith   ierr = VecCreateMPI(mat->comm,aij->n,aij->N,&gvec); CHKERRQ(ierr);
1121eb62cbbSBarry Smith 
1132d336d48SLois Curfman McInnes   /* generate the scatter context */
11408480c60SBarry Smith   ierr = VecScatterCreate(gvec,from,aij->lvec,to,&aij->Mvctx); CHKERRQ(ierr);
115a5a9c739SBarry Smith   PLogObjectParent(mat,aij->Mvctx);
116a5a9c739SBarry Smith   PLogObjectParent(mat,aij->lvec);
117464493b3SBarry Smith   PLogObjectParent(mat,from);
118464493b3SBarry Smith   PLogObjectParent(mat,to);
1199e25ed09SBarry Smith   aij->garray = garray;
120464493b3SBarry Smith   PLogObjectMemory(mat,(ec+1)*sizeof(int));
12178b31e54SBarry Smith   ierr = ISDestroy(from); CHKERRQ(ierr);
12278b31e54SBarry Smith   ierr = ISDestroy(to); CHKERRQ(ierr);
1231eb62cbbSBarry Smith   ierr = VecDestroy(gvec);
1243a40ed3dSBarry Smith   PetscFunctionReturn(0);
1258c79f6d3SBarry Smith }
1269e25ed09SBarry Smith 
1279e25ed09SBarry Smith 
1285615d1e5SSatish Balay #undef __FUNC__
1295615d1e5SSatish Balay #define __FUNC__ "DisAssemble_MPIAIJ"
1302493cbb0SBarry Smith /*
1312493cbb0SBarry Smith      Takes the local part of an already assembled MPIAIJ matrix
1322493cbb0SBarry Smith    and disassembles it. This is to allow new nonzeros into the matrix
1332493cbb0SBarry Smith    that require more communication in the matrix vector multiply.
1342493cbb0SBarry Smith    Thus certain data-structures must be rebuilt.
1352493cbb0SBarry Smith 
1362493cbb0SBarry Smith    Kind of slow! But that's what application programmers get when
1372493cbb0SBarry Smith    they are sloppy.
1382493cbb0SBarry Smith */
1392493cbb0SBarry Smith int DisAssemble_MPIAIJ(Mat A)
1402493cbb0SBarry Smith {
1412493cbb0SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ *) A->data;
1422493cbb0SBarry Smith   Mat        B = aij->B,Bnew;
143ec8511deSBarry Smith   Mat_SeqAIJ *Baij = (Mat_SeqAIJ*)B->data;
1442493cbb0SBarry Smith   int        ierr,i,j,m = Baij->m,n = aij->N,col,ct = 0,*garray = aij->garray;
145416022c9SBarry Smith   int        *nz,ec,shift = Baij->indexshift;
1462493cbb0SBarry Smith   Scalar     v;
1472493cbb0SBarry Smith 
1483a40ed3dSBarry Smith   PetscFunctionBegin;
1492493cbb0SBarry Smith   /* free stuff related to matrix-vec multiply */
150464493b3SBarry Smith   ierr = VecGetSize(aij->lvec,&ec); /* needed for PLogObjectMemory below */
1512493cbb0SBarry Smith   ierr = VecDestroy(aij->lvec); CHKERRQ(ierr); aij->lvec = 0;
15208480c60SBarry Smith   ierr = VecScatterDestroy(aij->Mvctx); CHKERRQ(ierr); aij->Mvctx = 0;
153464493b3SBarry Smith   if (aij->colmap) {
154*2066d6f7SSatish Balay #if defined (USE_CTABLE)
155*2066d6f7SSatish Balay     TableDelete(aij->colmap); aij->colmap = 0;
156*2066d6f7SSatish Balay #else
1570452661fSBarry Smith     PetscFree(aij->colmap); aij->colmap = 0;
158464493b3SBarry Smith     PLogObjectMemory(A,-Baij->n*sizeof(int));
159*2066d6f7SSatish Balay #endif
160464493b3SBarry Smith   }
1612493cbb0SBarry Smith 
1622493cbb0SBarry Smith   /* make sure that B is assembled so we can access its values */
1636d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
164fe2f2677SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
1652493cbb0SBarry Smith 
1662493cbb0SBarry Smith   /* invent new B and copy stuff over */
167639f9d9dSBarry Smith   nz = (int *) PetscMalloc( (m+1)*sizeof(int) ); CHKPTRQ(nz);
16848b35521SBarry Smith   for ( i=0; i<m; i++ ) {
16948b35521SBarry Smith     nz[i] = Baij->i[i+1] - Baij->i[i];
17048b35521SBarry Smith   }
171029af93fSBarry Smith   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,m,n,0,nz,&Bnew); CHKERRQ(ierr);
1720452661fSBarry Smith   PetscFree(nz);
1732493cbb0SBarry Smith   for ( i=0; i<m; i++ ) {
174dbb450caSBarry Smith     for ( j=Baij->i[i]+shift; j<Baij->i[i+1]+shift; j++ ) {
175dbb450caSBarry Smith       col = garray[Baij->j[ct]+shift];
1762493cbb0SBarry Smith       v = Baij->a[ct++];
177dbb450caSBarry Smith       ierr = MatSetValues(Bnew,1,&i,1,&col,&v,INSERT_VALUES); CHKERRQ(ierr);
1782493cbb0SBarry Smith     }
1792493cbb0SBarry Smith   }
1800452661fSBarry Smith   PetscFree(aij->garray); aij->garray = 0;
181464493b3SBarry Smith   PLogObjectMemory(A,-ec*sizeof(int));
1822493cbb0SBarry Smith   ierr = MatDestroy(B); CHKERRQ(ierr);
183464493b3SBarry Smith   PLogObjectParent(A,Bnew);
1842493cbb0SBarry Smith   aij->B = Bnew;
185227d817aSBarry Smith   A->was_assembled = PETSC_FALSE;
1863a40ed3dSBarry Smith   PetscFunctionReturn(0);
1872493cbb0SBarry Smith }
1882493cbb0SBarry Smith 
18948b35521SBarry Smith 
190