xref: /petsc/src/mat/impls/aij/mpi/mmaij.c (revision 0064e2bb8828235e9094277f38fd4abc2f5e8197)
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*0064e2bbSSatish Balay static char vcid[] = "$Id: mmaij.c,v 1.39 1999/01/08 21:11:21 balay 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 
232066d6f7SSatish Balay #if defined (USE_CTABLE)
242066d6f7SSatish Balay     Table gid1_lid1;
252066d6f7SSatish Balay     CTablePos tpos;
262066d6f7SSatish Balay     int gid, lid;
272066d6f7SSatish Balay #endif
282066d6f7SSatish Balay 
293a40ed3dSBarry Smith   PetscFunctionBegin;
302066d6f7SSatish Balay 
312066d6f7SSatish Balay #if defined (USE_TABLE)
322066d6f7SSatish Balay   /* use a table - Mark Adams (this has not been tested with "shift") */
332066d6f7SSatish Balay   TableCreate( &gid1_lid1, B->m );
342066d6f7SSatish Balay   for ( i=0; i<B->m; i++ ) {
352066d6f7SSatish Balay     for ( j=0; j<B->ilen[i]; j++ ) {
362066d6f7SSatish Balay       int gid1 = aj[B->i[i] + shift + j] + 1;
372066d6f7SSatish Balay       if ( !TableFind( gid1_lid1, gid1 ) ){
382066d6f7SSatish Balay         /* one based table */
392066d6f7SSatish Balay         ierr = TableAdd( gid1_lid1, gid1, ++ec ); CHKERRQ(ierr);
402066d6f7SSatish Balay       }
412066d6f7SSatish Balay     }
422066d6f7SSatish Balay   }
432066d6f7SSatish Balay   /* form array of columns we need */
442066d6f7SSatish Balay   garray = (int *) PetscMalloc( (ec+1)*sizeof(int) ); CHKPTRQ(garray);
452066d6f7SSatish Balay   ierr = TableGetHeadPosition( gid1_lid1, &tpos ); CHKERRQ(ierr);
462066d6f7SSatish Balay   while( tpos ) {
472066d6f7SSatish Balay     ierr = TableGetNext( gid1_lid1, &tpos, &gid, &lid ); CHKERRQ(ierr);
482066d6f7SSatish Balay     gid--; lid--;
492066d6f7SSatish Balay     garray[lid] = gid;
502066d6f7SSatish Balay   }
51*0064e2bbSSatish Balay   ierr = PetscSortInt(ec,garray); CHKERRQ(ierr); /* sort, and rebuild */
52*0064e2bbSSatish Balay   /* qsort( garray, ec, sizeof(int), intcomparc ); */
532066d6f7SSatish Balay   TableRemoveAll( gid1_lid1 );
542066d6f7SSatish Balay   for ( i=0; i<ec; i++ ) {
552066d6f7SSatish Balay     ierr = TableAdd( gid1_lid1, garray[i] + 1, i + 1 ); CHKERRQ(ierr);
562066d6f7SSatish Balay   }
572066d6f7SSatish Balay   /* compact out the extra columns in B */
582066d6f7SSatish Balay   for ( i=0; i<B->m; i++ ) {
592066d6f7SSatish Balay     for ( j=0; j<B->ilen[i]; j++ ) {
602066d6f7SSatish Balay       int gid1 = aj[B->i[i] + shift + j] + 1;
612066d6f7SSatish Balay       lid = TableFind( gid1_lid1, gid1 ) - 1;
622066d6f7SSatish Balay       aj[B->i[i] + shift + j] = lid;
632066d6f7SSatish Balay     }
642066d6f7SSatish Balay   }
652066d6f7SSatish Balay   B->n = aij->B->n = aij->B->N = ec;
662066d6f7SSatish Balay   TableDelete(gid1_lid1);
672066d6f7SSatish Balay   /* Mark Adams */
682066d6f7SSatish Balay #else
698c79f6d3SBarry Smith   /* For the first stab we make an array as long as the number of columns */
701eb62cbbSBarry Smith   /* mark those columns that are in aij->B */
71ac03b0baSBarry Smith   indices = (int *) PetscMalloc( (N+1)*sizeof(int) ); CHKPTRQ(indices);
72cddf8d76SBarry Smith   PetscMemzero(indices,N*sizeof(int));
73d6dfbf8fSBarry Smith   for ( i=0; i<B->m; i++ ) {
74d6dfbf8fSBarry Smith     for ( j=0; j<B->ilen[i]; j++ ) {
75dbb450caSBarry Smith       if (!indices[aj[B->i[i] +shift + j] + shift]) ec++;
76416022c9SBarry Smith       indices[aj[B->i[i] + shift + j] + shift] = 1;
77416022c9SBarry Smith     }
781eb62cbbSBarry Smith   }
798c79f6d3SBarry Smith 
801eb62cbbSBarry Smith   /* form array of columns we need */
810452661fSBarry Smith   garray = (int *) PetscMalloc( (ec+1)*sizeof(int) ); CHKPTRQ(garray);
821eb62cbbSBarry Smith   ec = 0;
831eb62cbbSBarry Smith   for ( i=0; i<N; i++ ) {
841eb62cbbSBarry Smith     if (indices[i]) garray[ec++] = i;
851eb62cbbSBarry Smith   }
861eb62cbbSBarry Smith 
871eb62cbbSBarry Smith   /* make indices now point into garray */
881eb62cbbSBarry Smith   for ( i=0; i<ec; i++ ) {
89dbb450caSBarry Smith     indices[garray[i]] = i-shift;
901eb62cbbSBarry Smith   }
911eb62cbbSBarry Smith 
921eb62cbbSBarry Smith   /* compact out the extra columns in B */
93d6dfbf8fSBarry Smith   for ( i=0; i<B->m; i++ ) {
94d6dfbf8fSBarry Smith     for ( j=0; j<B->ilen[i]; j++ ) {
95dbb450caSBarry Smith       aj[B->i[i] + shift + j] = indices[aj[B->i[i] + shift + j]+shift];
961eb62cbbSBarry Smith     }
97d6dfbf8fSBarry Smith   }
98639f9d9dSBarry Smith   B->n = aij->B->n = aij->B->N = ec;
990452661fSBarry Smith   PetscFree(indices);
1002066d6f7SSatish Balay #endif
1011eb62cbbSBarry Smith   /* create local vector that is used to scatter into */
102029af93fSBarry Smith   ierr = VecCreateSeq(PETSC_COMM_SELF,ec,&aij->lvec); CHKERRQ(ierr);
1031eb62cbbSBarry Smith 
104d6dfbf8fSBarry Smith   /* create two temporary Index sets for build scatter gather */
105029af93fSBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,ec,garray,&from); CHKERRQ(ierr);
106029af93fSBarry Smith   ierr = ISCreateStride(PETSC_COMM_SELF,ec,0,1,&to); CHKERRQ(ierr);
1071eb62cbbSBarry Smith 
1081eb62cbbSBarry Smith   /* create temporary global vector to generate scatter context */
1091eb62cbbSBarry Smith   /* this is inefficient, but otherwise we must do either
1101eb62cbbSBarry Smith      1) save garray until the first actual scatter when the vector is known or
1111eb62cbbSBarry Smith      2) have another way of generating a scatter context without a vector.*/
11278b31e54SBarry Smith   ierr = VecCreateMPI(mat->comm,aij->n,aij->N,&gvec); CHKERRQ(ierr);
1131eb62cbbSBarry Smith 
1142d336d48SLois Curfman McInnes   /* generate the scatter context */
11508480c60SBarry Smith   ierr = VecScatterCreate(gvec,from,aij->lvec,to,&aij->Mvctx); CHKERRQ(ierr);
116a5a9c739SBarry Smith   PLogObjectParent(mat,aij->Mvctx);
117a5a9c739SBarry Smith   PLogObjectParent(mat,aij->lvec);
118464493b3SBarry Smith   PLogObjectParent(mat,from);
119464493b3SBarry Smith   PLogObjectParent(mat,to);
1209e25ed09SBarry Smith   aij->garray = garray;
121464493b3SBarry Smith   PLogObjectMemory(mat,(ec+1)*sizeof(int));
12278b31e54SBarry Smith   ierr = ISDestroy(from); CHKERRQ(ierr);
12378b31e54SBarry Smith   ierr = ISDestroy(to); CHKERRQ(ierr);
1241eb62cbbSBarry Smith   ierr = VecDestroy(gvec);
1253a40ed3dSBarry Smith   PetscFunctionReturn(0);
1268c79f6d3SBarry Smith }
1279e25ed09SBarry Smith 
1289e25ed09SBarry Smith 
1295615d1e5SSatish Balay #undef __FUNC__
1305615d1e5SSatish Balay #define __FUNC__ "DisAssemble_MPIAIJ"
1312493cbb0SBarry Smith /*
1322493cbb0SBarry Smith      Takes the local part of an already assembled MPIAIJ matrix
1332493cbb0SBarry Smith    and disassembles it. This is to allow new nonzeros into the matrix
1342493cbb0SBarry Smith    that require more communication in the matrix vector multiply.
1352493cbb0SBarry Smith    Thus certain data-structures must be rebuilt.
1362493cbb0SBarry Smith 
1372493cbb0SBarry Smith    Kind of slow! But that's what application programmers get when
1382493cbb0SBarry Smith    they are sloppy.
1392493cbb0SBarry Smith */
1402493cbb0SBarry Smith int DisAssemble_MPIAIJ(Mat A)
1412493cbb0SBarry Smith {
1422493cbb0SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ *) A->data;
1432493cbb0SBarry Smith   Mat        B = aij->B,Bnew;
144ec8511deSBarry Smith   Mat_SeqAIJ *Baij = (Mat_SeqAIJ*)B->data;
1452493cbb0SBarry Smith   int        ierr,i,j,m = Baij->m,n = aij->N,col,ct = 0,*garray = aij->garray;
146416022c9SBarry Smith   int        *nz,ec,shift = Baij->indexshift;
1472493cbb0SBarry Smith   Scalar     v;
1482493cbb0SBarry Smith 
1493a40ed3dSBarry Smith   PetscFunctionBegin;
1502493cbb0SBarry Smith   /* free stuff related to matrix-vec multiply */
151464493b3SBarry Smith   ierr = VecGetSize(aij->lvec,&ec); /* needed for PLogObjectMemory below */
1522493cbb0SBarry Smith   ierr = VecDestroy(aij->lvec); CHKERRQ(ierr); aij->lvec = 0;
15308480c60SBarry Smith   ierr = VecScatterDestroy(aij->Mvctx); CHKERRQ(ierr); aij->Mvctx = 0;
154464493b3SBarry Smith   if (aij->colmap) {
1552066d6f7SSatish Balay #if defined (USE_CTABLE)
1562066d6f7SSatish Balay     TableDelete(aij->colmap); aij->colmap = 0;
1572066d6f7SSatish Balay #else
1580452661fSBarry Smith     PetscFree(aij->colmap); aij->colmap = 0;
159464493b3SBarry Smith     PLogObjectMemory(A,-Baij->n*sizeof(int));
1602066d6f7SSatish Balay #endif
161464493b3SBarry Smith   }
1622493cbb0SBarry Smith 
1632493cbb0SBarry Smith   /* make sure that B is assembled so we can access its values */
1646d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
165fe2f2677SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
1662493cbb0SBarry Smith 
1672493cbb0SBarry Smith   /* invent new B and copy stuff over */
168639f9d9dSBarry Smith   nz = (int *) PetscMalloc( (m+1)*sizeof(int) ); CHKPTRQ(nz);
16948b35521SBarry Smith   for ( i=0; i<m; i++ ) {
17048b35521SBarry Smith     nz[i] = Baij->i[i+1] - Baij->i[i];
17148b35521SBarry Smith   }
172029af93fSBarry Smith   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,m,n,0,nz,&Bnew); CHKERRQ(ierr);
1730452661fSBarry Smith   PetscFree(nz);
1742493cbb0SBarry Smith   for ( i=0; i<m; i++ ) {
175dbb450caSBarry Smith     for ( j=Baij->i[i]+shift; j<Baij->i[i+1]+shift; j++ ) {
176dbb450caSBarry Smith       col = garray[Baij->j[ct]+shift];
1772493cbb0SBarry Smith       v = Baij->a[ct++];
178dbb450caSBarry Smith       ierr = MatSetValues(Bnew,1,&i,1,&col,&v,INSERT_VALUES); CHKERRQ(ierr);
1792493cbb0SBarry Smith     }
1802493cbb0SBarry Smith   }
1810452661fSBarry Smith   PetscFree(aij->garray); aij->garray = 0;
182464493b3SBarry Smith   PLogObjectMemory(A,-ec*sizeof(int));
1832493cbb0SBarry Smith   ierr = MatDestroy(B); CHKERRQ(ierr);
184464493b3SBarry Smith   PLogObjectParent(A,Bnew);
1852493cbb0SBarry Smith   aij->B = Bnew;
186227d817aSBarry Smith   A->was_assembled = PETSC_FALSE;
1873a40ed3dSBarry Smith   PetscFunctionReturn(0);
1882493cbb0SBarry Smith }
1892493cbb0SBarry Smith 
19048b35521SBarry Smith 
191