xref: /petsc/src/mat/impls/aij/mpi/mpiaij.c (revision fa46199cd6736f835a9dfd5fb06fcbb3763bab4e)
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*fa46199cSSatish Balay static char vcid[] = "$Id: mpiaij.c,v 1.282 1999/02/17 19:29:50 balay Exp balay $";
3cb512458SBarry Smith #endif
48a729477SBarry Smith 
570f55243SBarry Smith #include "src/mat/impls/aij/mpi/mpiaij.h"
6f5eb4b81SSatish Balay #include "src/vec/vecimpl.h"
7d9942c19SSatish Balay #include "src/inline/spops.h"
88a729477SBarry Smith 
99e25ed09SBarry Smith /* local utility routine that creates a mapping from the global column
109e25ed09SBarry Smith number to the local number in the off-diagonal part of the local
119e25ed09SBarry Smith storage of the matrix.  This is done in a non scable way since the
129e25ed09SBarry Smith length of colmap equals the global matrix length.
139e25ed09SBarry Smith */
145615d1e5SSatish Balay #undef __FUNC__
15d4bb536fSBarry Smith #define __FUNC__ "CreateColmap_MPIAIJ_Private"
160a198c4cSBarry Smith int CreateColmap_MPIAIJ_Private(Mat mat)
179e25ed09SBarry Smith {
1844a69424SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data;
19ec8511deSBarry Smith   Mat_SeqAIJ *B = (Mat_SeqAIJ*) aij->B->data;
206dee3f7eSBarry Smith   int        n = B->n,i;
216dee3f7eSBarry Smith #if defined (USE_CTABLE)
226dee3f7eSBarry Smith   int        ierr;
236dee3f7eSBarry Smith #endif
24dbb450caSBarry Smith 
253a40ed3dSBarry Smith   PetscFunctionBegin;
26b1fc9764SSatish Balay #if defined (USE_CTABLE)
27*fa46199cSSatish Balay   ierr = TableCreate(aij->n/5,&aij->colmap); CHKERRQ(ierr);
28b1fc9764SSatish Balay   for ( i=0; i<n; i++ ){
29b1fc9764SSatish Balay     ierr = TableAdd(aij->colmap,aij->garray[i]+1,i+1);CHKERRQ(ierr);
30b1fc9764SSatish Balay   }
31b1fc9764SSatish Balay #else
32758f045eSSatish Balay   aij->colmap = (int *) PetscMalloc((aij->N+1)*sizeof(int));CHKPTRQ(aij->colmap);
33464493b3SBarry Smith   PLogObjectMemory(mat,aij->N*sizeof(int));
34cddf8d76SBarry Smith   PetscMemzero(aij->colmap,aij->N*sizeof(int));
35905e6a2fSBarry Smith   for ( i=0; i<n; i++ ) aij->colmap[aij->garray[i]] = i+1;
36b1fc9764SSatish Balay #endif
373a40ed3dSBarry Smith   PetscFunctionReturn(0);
389e25ed09SBarry Smith }
399e25ed09SBarry Smith 
402493cbb0SBarry Smith extern int DisAssemble_MPIAIJ(Mat);
412493cbb0SBarry Smith 
420520107fSSatish Balay #define CHUNKSIZE   15
4330770e4dSSatish Balay #define MatSetValues_SeqAIJ_A_Private(row,col,value,addv) \
440520107fSSatish Balay { \
450520107fSSatish Balay  \
460520107fSSatish Balay     rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift; \
4730770e4dSSatish Balay     rmax = aimax[row]; nrow = ailen[row];  \
48f5e9677aSSatish Balay     col1 = col - shift; \
49f5e9677aSSatish Balay      \
50ba4e3ef2SSatish Balay     low = 0; high = nrow; \
51ba4e3ef2SSatish Balay     while (high-low > 5) { \
52ba4e3ef2SSatish Balay       t = (low+high)/2; \
53ba4e3ef2SSatish Balay       if (rp[t] > col) high = t; \
54ba4e3ef2SSatish Balay       else             low  = t; \
55ba4e3ef2SSatish Balay     } \
560520107fSSatish Balay       for ( _i=0; _i<nrow; _i++ ) { \
57f5e9677aSSatish Balay         if (rp[_i] > col1) break; \
58f5e9677aSSatish Balay         if (rp[_i] == col1) { \
590520107fSSatish Balay           if (addv == ADD_VALUES) ap[_i] += value;   \
600520107fSSatish Balay           else                  ap[_i] = value; \
6130770e4dSSatish Balay           goto a_noinsert; \
620520107fSSatish Balay         } \
630520107fSSatish Balay       }  \
6489280ab3SLois Curfman McInnes       if (nonew == 1) goto a_noinsert; \
65a8c6a408SBarry Smith       else if (nonew == -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Inserting a new nonzero into matrix"); \
660520107fSSatish Balay       if (nrow >= rmax) { \
670520107fSSatish Balay         /* there is no extra room in row, therefore enlarge */ \
680520107fSSatish Balay         int    new_nz = ai[a->m] + CHUNKSIZE,len,*new_i,*new_j; \
690520107fSSatish Balay         Scalar *new_a; \
700520107fSSatish Balay  \
71a8c6a408SBarry Smith         if (nonew == -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Inserting a new nonzero in the matrix"); \
7289280ab3SLois Curfman McInnes  \
730520107fSSatish Balay         /* malloc new storage space */ \
740520107fSSatish Balay         len     = new_nz*(sizeof(int)+sizeof(Scalar))+(a->m+1)*sizeof(int); \
750520107fSSatish Balay         new_a   = (Scalar *) PetscMalloc( len ); CHKPTRQ(new_a); \
760520107fSSatish Balay         new_j   = (int *) (new_a + new_nz); \
770520107fSSatish Balay         new_i   = new_j + new_nz; \
780520107fSSatish Balay  \
790520107fSSatish Balay         /* copy over old data into new slots */ \
800520107fSSatish Balay         for ( ii=0; ii<row+1; ii++ ) {new_i[ii] = ai[ii];} \
810520107fSSatish Balay         for ( ii=row+1; ii<a->m+1; ii++ ) {new_i[ii] = ai[ii]+CHUNKSIZE;} \
820520107fSSatish Balay         PetscMemcpy(new_j,aj,(ai[row]+nrow+shift)*sizeof(int)); \
830520107fSSatish Balay         len = (new_nz - CHUNKSIZE - ai[row] - nrow - shift); \
840520107fSSatish Balay         PetscMemcpy(new_j+ai[row]+shift+nrow+CHUNKSIZE,aj+ai[row]+shift+nrow, \
850520107fSSatish Balay                                                            len*sizeof(int)); \
860520107fSSatish Balay         PetscMemcpy(new_a,aa,(ai[row]+nrow+shift)*sizeof(Scalar)); \
870520107fSSatish Balay         PetscMemcpy(new_a+ai[row]+shift+nrow+CHUNKSIZE,aa+ai[row]+shift+nrow, \
880520107fSSatish Balay                                                            len*sizeof(Scalar));  \
890520107fSSatish Balay         /* free up old matrix storage */ \
90f5e9677aSSatish Balay  \
910520107fSSatish Balay         PetscFree(a->a);  \
920520107fSSatish Balay         if (!a->singlemalloc) {PetscFree(a->i);PetscFree(a->j);} \
930520107fSSatish Balay         aa = a->a = new_a; ai = a->i = new_i; aj = a->j = new_j;  \
940520107fSSatish Balay         a->singlemalloc = 1; \
950520107fSSatish Balay  \
960520107fSSatish Balay         rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift; \
9730770e4dSSatish Balay         rmax = aimax[row] = aimax[row] + CHUNKSIZE; \
980520107fSSatish Balay         PLogObjectMemory(A,CHUNKSIZE*(sizeof(int) + sizeof(Scalar))); \
990520107fSSatish Balay         a->maxnz += CHUNKSIZE; \
1000520107fSSatish Balay         a->reallocs++; \
1010520107fSSatish Balay       } \
1020520107fSSatish Balay       N = nrow++ - 1; a->nz++; \
1030520107fSSatish Balay       /* shift up all the later entries in this row */ \
1040520107fSSatish Balay       for ( ii=N; ii>=_i; ii-- ) { \
1050520107fSSatish Balay         rp[ii+1] = rp[ii]; \
1060520107fSSatish Balay         ap[ii+1] = ap[ii]; \
1070520107fSSatish Balay       } \
108f5e9677aSSatish Balay       rp[_i] = col1;  \
1090520107fSSatish Balay       ap[_i] = value;  \
11030770e4dSSatish Balay       a_noinsert: ; \
1110520107fSSatish Balay       ailen[row] = nrow; \
1120520107fSSatish Balay }
1130a198c4cSBarry Smith 
11430770e4dSSatish Balay #define MatSetValues_SeqAIJ_B_Private(row,col,value,addv) \
11530770e4dSSatish Balay { \
11630770e4dSSatish Balay  \
11730770e4dSSatish Balay     rp   = bj + bi[row] + shift; ap = ba + bi[row] + shift; \
11830770e4dSSatish Balay     rmax = bimax[row]; nrow = bilen[row];  \
11930770e4dSSatish Balay     col1 = col - shift; \
12030770e4dSSatish Balay      \
121ba4e3ef2SSatish Balay     low = 0; high = nrow; \
122ba4e3ef2SSatish Balay     while (high-low > 5) { \
123ba4e3ef2SSatish Balay       t = (low+high)/2; \
124ba4e3ef2SSatish Balay       if (rp[t] > col) high = t; \
125ba4e3ef2SSatish Balay       else             low  = t; \
126ba4e3ef2SSatish Balay     } \
12730770e4dSSatish Balay        for ( _i=0; _i<nrow; _i++ ) { \
12830770e4dSSatish Balay         if (rp[_i] > col1) break; \
12930770e4dSSatish Balay         if (rp[_i] == col1) { \
13030770e4dSSatish Balay           if (addv == ADD_VALUES) ap[_i] += value;   \
13130770e4dSSatish Balay           else                  ap[_i] = value; \
13230770e4dSSatish Balay           goto b_noinsert; \
13330770e4dSSatish Balay         } \
13430770e4dSSatish Balay       }  \
13589280ab3SLois Curfman McInnes       if (nonew == 1) goto b_noinsert; \
136a8c6a408SBarry Smith       else if (nonew == -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Inserting a new nonzero into matrix"); \
13730770e4dSSatish Balay       if (nrow >= rmax) { \
13830770e4dSSatish Balay         /* there is no extra room in row, therefore enlarge */ \
13974c639caSSatish Balay         int    new_nz = bi[b->m] + CHUNKSIZE,len,*new_i,*new_j; \
14030770e4dSSatish Balay         Scalar *new_a; \
14130770e4dSSatish Balay  \
142a8c6a408SBarry Smith         if (nonew == -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Inserting a new nonzero in the matrix"); \
14389280ab3SLois Curfman McInnes  \
14430770e4dSSatish Balay         /* malloc new storage space */ \
14574c639caSSatish Balay         len     = new_nz*(sizeof(int)+sizeof(Scalar))+(b->m+1)*sizeof(int); \
14630770e4dSSatish Balay         new_a   = (Scalar *) PetscMalloc( len ); CHKPTRQ(new_a); \
14730770e4dSSatish Balay         new_j   = (int *) (new_a + new_nz); \
14830770e4dSSatish Balay         new_i   = new_j + new_nz; \
14930770e4dSSatish Balay  \
15030770e4dSSatish Balay         /* copy over old data into new slots */ \
15130770e4dSSatish Balay         for ( ii=0; ii<row+1; ii++ ) {new_i[ii] = bi[ii];} \
15274c639caSSatish Balay         for ( ii=row+1; ii<b->m+1; ii++ ) {new_i[ii] = bi[ii]+CHUNKSIZE;} \
15330770e4dSSatish Balay         PetscMemcpy(new_j,bj,(bi[row]+nrow+shift)*sizeof(int)); \
15430770e4dSSatish Balay         len = (new_nz - CHUNKSIZE - bi[row] - nrow - shift); \
15530770e4dSSatish Balay         PetscMemcpy(new_j+bi[row]+shift+nrow+CHUNKSIZE,bj+bi[row]+shift+nrow, \
15630770e4dSSatish Balay                                                            len*sizeof(int)); \
15730770e4dSSatish Balay         PetscMemcpy(new_a,ba,(bi[row]+nrow+shift)*sizeof(Scalar)); \
15830770e4dSSatish Balay         PetscMemcpy(new_a+bi[row]+shift+nrow+CHUNKSIZE,ba+bi[row]+shift+nrow, \
15930770e4dSSatish Balay                                                            len*sizeof(Scalar));  \
16030770e4dSSatish Balay         /* free up old matrix storage */ \
16130770e4dSSatish Balay  \
16274c639caSSatish Balay         PetscFree(b->a);  \
16374c639caSSatish Balay         if (!b->singlemalloc) {PetscFree(b->i);PetscFree(b->j);} \
16474c639caSSatish Balay         ba = b->a = new_a; bi = b->i = new_i; bj = b->j = new_j;  \
16574c639caSSatish Balay         b->singlemalloc = 1; \
16630770e4dSSatish Balay  \
16730770e4dSSatish Balay         rp   = bj + bi[row] + shift; ap = ba + bi[row] + shift; \
16830770e4dSSatish Balay         rmax = bimax[row] = bimax[row] + CHUNKSIZE; \
16974c639caSSatish Balay         PLogObjectMemory(B,CHUNKSIZE*(sizeof(int) + sizeof(Scalar))); \
17074c639caSSatish Balay         b->maxnz += CHUNKSIZE; \
17174c639caSSatish Balay         b->reallocs++; \
17230770e4dSSatish Balay       } \
17374c639caSSatish Balay       N = nrow++ - 1; b->nz++; \
17430770e4dSSatish Balay       /* shift up all the later entries in this row */ \
17530770e4dSSatish Balay       for ( ii=N; ii>=_i; ii-- ) { \
17630770e4dSSatish Balay         rp[ii+1] = rp[ii]; \
17730770e4dSSatish Balay         ap[ii+1] = ap[ii]; \
17830770e4dSSatish Balay       } \
17930770e4dSSatish Balay       rp[_i] = col1;  \
18030770e4dSSatish Balay       ap[_i] = value;  \
18130770e4dSSatish Balay       b_noinsert: ; \
18230770e4dSSatish Balay       bilen[row] = nrow; \
18330770e4dSSatish Balay }
18430770e4dSSatish Balay 
1850520107fSSatish Balay extern int MatSetValues_SeqAIJ(Mat,int,int*,int,int*,Scalar*,InsertMode);
1865615d1e5SSatish Balay #undef __FUNC__
1875615d1e5SSatish Balay #define __FUNC__ "MatSetValues_MPIAIJ"
1888f6be9afSLois Curfman McInnes int MatSetValues_MPIAIJ(Mat mat,int m,int *im,int n,int *in,Scalar *v,InsertMode addv)
1898a729477SBarry Smith {
19044a69424SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data;
1914b0e389bSBarry Smith   Scalar     value;
1921eb62cbbSBarry Smith   int        ierr,i,j, rstart = aij->rstart, rend = aij->rend;
1931eb62cbbSBarry Smith   int        cstart = aij->cstart, cend = aij->cend,row,col;
194905e6a2fSBarry Smith   int        roworiented = aij->roworiented;
1958a729477SBarry Smith 
1960520107fSSatish Balay   /* Some Variables required in the macro */
1974ee7247eSSatish Balay   Mat        A = aij->A;
1984ee7247eSSatish Balay   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
19930770e4dSSatish Balay   int        *aimax = a->imax, *ai = a->i, *ailen = a->ilen,*aj = a->j;
20030770e4dSSatish Balay   Scalar     *aa = a->a;
20130770e4dSSatish Balay 
20230770e4dSSatish Balay   Mat        B = aij->B;
20330770e4dSSatish Balay   Mat_SeqAIJ *b = (Mat_SeqAIJ *) B->data;
20430770e4dSSatish Balay   int        *bimax = b->imax, *bi = b->i, *bilen = b->ilen,*bj = b->j;
20530770e4dSSatish Balay   Scalar     *ba = b->a;
20630770e4dSSatish Balay 
207ba4e3ef2SSatish Balay   int        *rp,ii,nrow,_i,rmax, N, col1,low,high,t;
20830770e4dSSatish Balay   int        nonew = a->nonew,shift = a->indexshift;
20930770e4dSSatish Balay   Scalar     *ap;
2104ee7247eSSatish Balay 
2113a40ed3dSBarry Smith   PetscFunctionBegin;
2128a729477SBarry Smith   for ( i=0; i<m; i++ ) {
2135ef9f2a5SBarry Smith     if (im[i] < 0) continue;
2143a40ed3dSBarry Smith #if defined(USE_PETSC_BOPT_g)
215a8c6a408SBarry Smith     if (im[i] >= aij->M) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Row too large");
2160a198c4cSBarry Smith #endif
2174b0e389bSBarry Smith     if (im[i] >= rstart && im[i] < rend) {
2184b0e389bSBarry Smith       row = im[i] - rstart;
2191eb62cbbSBarry Smith       for ( j=0; j<n; j++ ) {
2204b0e389bSBarry Smith         if (in[j] >= cstart && in[j] < cend){
2214b0e389bSBarry Smith           col = in[j] - cstart;
2224b0e389bSBarry Smith           if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
22330770e4dSSatish Balay           MatSetValues_SeqAIJ_A_Private(row,col,value,addv);
2240520107fSSatish Balay           /* ierr = MatSetValues_SeqAIJ(aij->A,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */
2251eb62cbbSBarry Smith         }
2265ef9f2a5SBarry Smith         else if (in[j] < 0) continue;
2273a40ed3dSBarry Smith #if defined(USE_PETSC_BOPT_g)
228a8c6a408SBarry Smith         else if (in[j] >= aij->N) {SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Column too large");}
2290a198c4cSBarry Smith #endif
2301eb62cbbSBarry Smith         else {
231227d817aSBarry Smith           if (mat->was_assembled) {
232905e6a2fSBarry Smith             if (!aij->colmap) {
233905e6a2fSBarry Smith               ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
234905e6a2fSBarry Smith             }
235b1fc9764SSatish Balay #if defined (USE_CTABLE)
236*fa46199cSSatish Balay             ierr = TableFind(aij->colmap,in[j]+1,&col); CHKERRQ(ierr);
237*fa46199cSSatish Balay 	    col--;
238b1fc9764SSatish Balay #else
239905e6a2fSBarry Smith             col = aij->colmap[in[j]] - 1;
240b1fc9764SSatish Balay #endif
241ec8511deSBarry Smith             if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
2422493cbb0SBarry Smith               ierr = DisAssemble_MPIAIJ(mat); CHKERRQ(ierr);
2434b0e389bSBarry Smith               col =  in[j];
2449bf004c3SSatish Balay               /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
245f9508a3cSSatish Balay               B = aij->B;
246f9508a3cSSatish Balay               b = (Mat_SeqAIJ *) B->data;
247f9508a3cSSatish Balay               bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j;
248f9508a3cSSatish Balay               ba = b->a;
249d6dfbf8fSBarry Smith             }
250c48de900SBarry Smith           } else col = in[j];
2514b0e389bSBarry Smith           if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
25230770e4dSSatish Balay           MatSetValues_SeqAIJ_B_Private(row,col,value,addv);
25330770e4dSSatish Balay           /* ierr = MatSetValues_SeqAIJ(aij->B,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */
2541eb62cbbSBarry Smith         }
2551eb62cbbSBarry Smith       }
2565ef9f2a5SBarry Smith     } else {
25790f02eecSBarry Smith       if (roworiented && !aij->donotstash) {
2584b0e389bSBarry Smith         ierr = StashValues_Private(&aij->stash,im[i],n,in,v+i*n,addv);CHKERRQ(ierr);
2595ef9f2a5SBarry Smith       } else {
26090f02eecSBarry Smith         if (!aij->donotstash) {
2614b0e389bSBarry Smith           row = im[i];
2624b0e389bSBarry Smith           for ( j=0; j<n; j++ ) {
2634b0e389bSBarry Smith             ierr = StashValues_Private(&aij->stash,row,1,in+j,v+i+j*m,addv);CHKERRQ(ierr);
2644b0e389bSBarry Smith           }
2654b0e389bSBarry Smith         }
2661eb62cbbSBarry Smith       }
2678a729477SBarry Smith     }
26890f02eecSBarry Smith   }
2693a40ed3dSBarry Smith   PetscFunctionReturn(0);
2708a729477SBarry Smith }
2718a729477SBarry Smith 
2725615d1e5SSatish Balay #undef __FUNC__
2735615d1e5SSatish Balay #define __FUNC__ "MatGetValues_MPIAIJ"
2748f6be9afSLois Curfman McInnes int MatGetValues_MPIAIJ(Mat mat,int m,int *idxm,int n,int *idxn,Scalar *v)
275b49de8d1SLois Curfman McInnes {
276b49de8d1SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data;
277b49de8d1SLois Curfman McInnes   int        ierr,i,j, rstart = aij->rstart, rend = aij->rend;
278b49de8d1SLois Curfman McInnes   int        cstart = aij->cstart, cend = aij->cend,row,col;
279b49de8d1SLois Curfman McInnes 
2803a40ed3dSBarry Smith   PetscFunctionBegin;
281b49de8d1SLois Curfman McInnes   for ( i=0; i<m; i++ ) {
282a8c6a408SBarry Smith     if (idxm[i] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Negative row");
283a8c6a408SBarry Smith     if (idxm[i] >= aij->M) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Row too large");
284b49de8d1SLois Curfman McInnes     if (idxm[i] >= rstart && idxm[i] < rend) {
285b49de8d1SLois Curfman McInnes       row = idxm[i] - rstart;
286b49de8d1SLois Curfman McInnes       for ( j=0; j<n; j++ ) {
287a8c6a408SBarry Smith         if (idxn[j] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Negative column");
288a8c6a408SBarry Smith         if (idxn[j] >= aij->N) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Column too large");
289b49de8d1SLois Curfman McInnes         if (idxn[j] >= cstart && idxn[j] < cend){
290b49de8d1SLois Curfman McInnes           col = idxn[j] - cstart;
291b49de8d1SLois Curfman McInnes           ierr = MatGetValues(aij->A,1,&row,1,&col,v+i*n+j); CHKERRQ(ierr);
292fa852ad4SSatish Balay         } else {
293905e6a2fSBarry Smith           if (!aij->colmap) {
294905e6a2fSBarry Smith             ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
295905e6a2fSBarry Smith           }
296b1fc9764SSatish Balay #if defined (USE_CTABLE)
297*fa46199cSSatish Balay           ierr = TableFind(aij->colmap,idxn[j]+1,&col); CHKERRQ(ierr);
298*fa46199cSSatish Balay           col --;
299b1fc9764SSatish Balay #else
300905e6a2fSBarry Smith           col = aij->colmap[idxn[j]] - 1;
301b1fc9764SSatish Balay #endif
302e60e1c95SSatish Balay           if ((col < 0) || (aij->garray[col] != idxn[j])) *(v+i*n+j) = 0.0;
303d9d09a02SSatish Balay           else {
304b49de8d1SLois Curfman McInnes             ierr = MatGetValues(aij->B,1,&row,1,&col,v+i*n+j); CHKERRQ(ierr);
305b49de8d1SLois Curfman McInnes           }
306b49de8d1SLois Curfman McInnes         }
307b49de8d1SLois Curfman McInnes       }
308a8c6a408SBarry Smith     } else {
309a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_SUP,0,"Only local values currently supported");
310b49de8d1SLois Curfman McInnes     }
311b49de8d1SLois Curfman McInnes   }
3123a40ed3dSBarry Smith   PetscFunctionReturn(0);
313b49de8d1SLois Curfman McInnes }
314b49de8d1SLois Curfman McInnes 
3155615d1e5SSatish Balay #undef __FUNC__
3165615d1e5SSatish Balay #define __FUNC__ "MatAssemblyBegin_MPIAIJ"
3178f6be9afSLois Curfman McInnes int MatAssemblyBegin_MPIAIJ(Mat mat,MatAssemblyType mode)
3188a729477SBarry Smith {
31944a69424SLois Curfman McInnes   Mat_MPIAIJ  *aij = (Mat_MPIAIJ *) mat->data;
320d6dfbf8fSBarry Smith   MPI_Comm    comm = mat->comm;
32117699dbbSLois Curfman McInnes   int         size = aij->size, *owners = aij->rowners;
32217699dbbSLois Curfman McInnes   int         rank = aij->rank,tag = mat->tag, *owner,*starts,count,ierr;
3231eb62cbbSBarry Smith   MPI_Request *send_waits,*recv_waits;
3246abc6512SBarry Smith   int         *nprocs,i,j,idx,*procs,nsends,nreceives,nmax,*work;
3251eb62cbbSBarry Smith   InsertMode  addv;
3261eb62cbbSBarry Smith   Scalar      *rvalues,*svalues;
3271eb62cbbSBarry Smith 
3283a40ed3dSBarry Smith   PetscFunctionBegin;
329b5bd3ad5SBarry Smith   if (aij->donotstash) {
330b5bd3ad5SBarry Smith     aij->svalues    = 0; aij->rvalues    = 0;
331b5bd3ad5SBarry Smith     aij->nsends     = 0; aij->nrecvs     = 0;
332b5bd3ad5SBarry Smith     aij->send_waits = 0; aij->recv_waits = 0;
333b5bd3ad5SBarry Smith     aij->rmax       = 0;
334b5bd3ad5SBarry Smith     PetscFunctionReturn(0);
335b5bd3ad5SBarry Smith   }
336b5bd3ad5SBarry Smith 
3371eb62cbbSBarry Smith   /* make sure all processors are either in INSERTMODE or ADDMODE */
338ca161407SBarry Smith   ierr = MPI_Allreduce(&mat->insertmode,&addv,1,MPI_INT,MPI_BOR,comm);CHKERRQ(ierr);
339dbb450caSBarry Smith   if (addv == (ADD_VALUES|INSERT_VALUES)) {
340a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Some processors inserted others added");
3411eb62cbbSBarry Smith   }
34247794344SBarry Smith   mat->insertmode = addv; /* in case this processor had no cache */
3431eb62cbbSBarry Smith 
3441eb62cbbSBarry Smith   /*  first count number of contributors to each processor */
3450452661fSBarry Smith   nprocs = (int *) PetscMalloc( 2*size*sizeof(int) ); CHKPTRQ(nprocs);
346cddf8d76SBarry Smith   PetscMemzero(nprocs,2*size*sizeof(int)); procs = nprocs + size;
3470452661fSBarry Smith   owner = (int *) PetscMalloc( (aij->stash.n+1)*sizeof(int) ); CHKPTRQ(owner);
3481eb62cbbSBarry Smith   for ( i=0; i<aij->stash.n; i++ ) {
3491eb62cbbSBarry Smith     idx = aij->stash.idx[i];
35017699dbbSLois Curfman McInnes     for ( j=0; j<size; j++ ) {
3511eb62cbbSBarry Smith       if (idx >= owners[j] && idx < owners[j+1]) {
3521eb62cbbSBarry Smith         nprocs[j]++; procs[j] = 1; owner[i] = j; break;
3538a729477SBarry Smith       }
3548a729477SBarry Smith     }
3558a729477SBarry Smith   }
35617699dbbSLois Curfman McInnes   nsends = 0;  for ( i=0; i<size; i++ ) { nsends += procs[i];}
3571eb62cbbSBarry Smith 
3581eb62cbbSBarry Smith   /* inform other processors of number of messages and max length*/
3590452661fSBarry Smith   work = (int *) PetscMalloc( size*sizeof(int) ); CHKPTRQ(work);
360ca161407SBarry Smith   ierr = MPI_Allreduce(procs, work,size,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr);
36117699dbbSLois Curfman McInnes   nreceives = work[rank];
362ca161407SBarry Smith   ierr = MPI_Allreduce( nprocs, work,size,MPI_INT,MPI_MAX,comm);CHKERRQ(ierr);
36317699dbbSLois Curfman McInnes   nmax = work[rank];
3640452661fSBarry Smith   PetscFree(work);
3651eb62cbbSBarry Smith 
3661eb62cbbSBarry Smith   /* post receives:
3671eb62cbbSBarry Smith        1) each message will consist of ordered pairs
3681eb62cbbSBarry Smith      (global index,value) we store the global index as a double
369d6dfbf8fSBarry Smith      to simplify the message passing.
3701eb62cbbSBarry Smith        2) since we don't know how long each individual message is we
3711eb62cbbSBarry Smith      allocate the largest needed buffer for each receive. Potentially
3721eb62cbbSBarry Smith      this is a lot of wasted space.
3731eb62cbbSBarry Smith 
3741eb62cbbSBarry Smith 
3751eb62cbbSBarry Smith        This could be done better.
3761eb62cbbSBarry Smith   */
377ca161407SBarry Smith   rvalues    = (Scalar *) PetscMalloc(3*(nreceives+1)*(nmax+1)*sizeof(Scalar));CHKPTRQ(rvalues);
378ca161407SBarry Smith   recv_waits = (MPI_Request *) PetscMalloc((nreceives+1)*sizeof(MPI_Request));CHKPTRQ(recv_waits);
3791eb62cbbSBarry Smith   for ( i=0; i<nreceives; i++ ) {
380ca161407SBarry Smith     ierr = MPI_Irecv(rvalues+3*nmax*i,3*nmax,MPIU_SCALAR,MPI_ANY_SOURCE,tag,
381ca161407SBarry Smith               comm,recv_waits+i);CHKERRQ(ierr);
3821eb62cbbSBarry Smith   }
3831eb62cbbSBarry Smith 
3841eb62cbbSBarry Smith   /* do sends:
3851eb62cbbSBarry Smith       1) starts[i] gives the starting index in svalues for stuff going to
3861eb62cbbSBarry Smith          the ith processor
3871eb62cbbSBarry Smith   */
3880452661fSBarry Smith   svalues    = (Scalar *) PetscMalloc(3*(aij->stash.n+1)*sizeof(Scalar));CHKPTRQ(svalues);
389ca161407SBarry Smith   send_waits = (MPI_Request *) PetscMalloc( (nsends+1)*sizeof(MPI_Request));CHKPTRQ(send_waits);
3900452661fSBarry Smith   starts     = (int *) PetscMalloc( size*sizeof(int) ); CHKPTRQ(starts);
3911eb62cbbSBarry Smith   starts[0]  = 0;
39217699dbbSLois Curfman McInnes   for ( i=1; i<size; i++ ) { starts[i] = starts[i-1] + nprocs[i-1];}
3931eb62cbbSBarry Smith   for ( i=0; i<aij->stash.n; i++ ) {
3941eb62cbbSBarry Smith     svalues[3*starts[owner[i]]]       = (Scalar)  aij->stash.idx[i];
3951eb62cbbSBarry Smith     svalues[3*starts[owner[i]]+1]     = (Scalar)  aij->stash.idy[i];
3961eb62cbbSBarry Smith     svalues[3*(starts[owner[i]]++)+2] =  aij->stash.array[i];
3971eb62cbbSBarry Smith   }
3980452661fSBarry Smith   PetscFree(owner);
3991eb62cbbSBarry Smith   starts[0] = 0;
40017699dbbSLois Curfman McInnes   for ( i=1; i<size; i++ ) { starts[i] = starts[i-1] + nprocs[i-1];}
4011eb62cbbSBarry Smith   count = 0;
40217699dbbSLois Curfman McInnes   for ( i=0; i<size; i++ ) {
4031eb62cbbSBarry Smith     if (procs[i]) {
404ca161407SBarry Smith       ierr = MPI_Isend(svalues+3*starts[i],3*nprocs[i],MPIU_SCALAR,i,tag,comm,send_waits+count++);CHKERRQ(ierr);
4051eb62cbbSBarry Smith     }
4061eb62cbbSBarry Smith   }
407b5bd3ad5SBarry Smith   PetscFree(starts);
408b5bd3ad5SBarry Smith   PetscFree(nprocs);
4091eb62cbbSBarry Smith 
4101eb62cbbSBarry Smith   /* Free cache space */
41110a665d1SBarry Smith   PLogInfo(aij->A,"MatAssemblyBegin_MPIAIJ:Number of off-processor values %d\n",aij->stash.n);
41278b31e54SBarry Smith   ierr = StashDestroy_Private(&aij->stash); CHKERRQ(ierr);
4131eb62cbbSBarry Smith 
4141eb62cbbSBarry Smith   aij->svalues    = svalues;    aij->rvalues    = rvalues;
4151eb62cbbSBarry Smith   aij->nsends     = nsends;     aij->nrecvs     = nreceives;
4161eb62cbbSBarry Smith   aij->send_waits = send_waits; aij->recv_waits = recv_waits;
4171eb62cbbSBarry Smith   aij->rmax       = nmax;
4181eb62cbbSBarry Smith 
4193a40ed3dSBarry Smith   PetscFunctionReturn(0);
4201eb62cbbSBarry Smith }
42144a69424SLois Curfman McInnes extern int MatSetUpMultiply_MPIAIJ(Mat);
4221eb62cbbSBarry Smith 
4235615d1e5SSatish Balay #undef __FUNC__
4245615d1e5SSatish Balay #define __FUNC__ "MatAssemblyEnd_MPIAIJ"
4258f6be9afSLois Curfman McInnes int MatAssemblyEnd_MPIAIJ(Mat mat,MatAssemblyType mode)
4261eb62cbbSBarry Smith {
42744a69424SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data;
4281eb62cbbSBarry Smith   MPI_Status  *send_status,recv_status;
429416022c9SBarry Smith   int         imdex,nrecvs = aij->nrecvs, count = nrecvs, i, n, ierr;
430905e6a2fSBarry Smith   int         row,col,other_disassembled;
4311eb62cbbSBarry Smith   Scalar      *values,val;
43247794344SBarry Smith   InsertMode  addv = mat->insertmode;
4331eb62cbbSBarry Smith 
4343a40ed3dSBarry Smith   PetscFunctionBegin;
435b5bd3ad5SBarry Smith 
4361eb62cbbSBarry Smith   /*  wait on receives */
4371eb62cbbSBarry Smith   while (count) {
438ca161407SBarry Smith     ierr = MPI_Waitany(nrecvs,aij->recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
4391eb62cbbSBarry Smith     /* unpack receives into our local space */
440d6dfbf8fSBarry Smith     values = aij->rvalues + 3*imdex*aij->rmax;
441ca161407SBarry Smith     ierr = MPI_Get_count(&recv_status,MPIU_SCALAR,&n);CHKERRQ(ierr);
4421eb62cbbSBarry Smith     n = n/3;
4431eb62cbbSBarry Smith     for ( i=0; i<n; i++ ) {
444227d817aSBarry Smith       row = (int) PetscReal(values[3*i]) - aij->rstart;
445227d817aSBarry Smith       col = (int) PetscReal(values[3*i+1]);
4461eb62cbbSBarry Smith       val = values[3*i+2];
4471eb62cbbSBarry Smith       if (col >= aij->cstart && col < aij->cend) {
4481eb62cbbSBarry Smith         col -= aij->cstart;
4496fd7127cSSatish Balay         ierr = MatSetValues(aij->A,1,&row,1,&col,&val,addv); CHKERRQ(ierr);
4503a40ed3dSBarry Smith       } else {
45155a1b374SBarry Smith         if (mat->was_assembled || mat->assembled) {
452905e6a2fSBarry Smith           if (!aij->colmap) {
453905e6a2fSBarry Smith             ierr = CreateColmap_MPIAIJ_Private(mat); CHKERRQ(ierr);
454905e6a2fSBarry Smith           }
455b1fc9764SSatish Balay #if defined (USE_CTABLE)
456*fa46199cSSatish Balay             ierr = TableFind(aij->colmap,col+1,&col); CHKERRQ(ierr);
457*fa46199cSSatish Balay 	    col --;
458b1fc9764SSatish Balay #else
459905e6a2fSBarry Smith           col = aij->colmap[col] - 1;
460b1fc9764SSatish Balay #endif
461ec8511deSBarry Smith           if (col < 0  && !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
4622493cbb0SBarry Smith             ierr = DisAssemble_MPIAIJ(mat); CHKERRQ(ierr);
463227d817aSBarry Smith             col = (int) PetscReal(values[3*i+1]);
464d6dfbf8fSBarry Smith           }
4659e25ed09SBarry Smith         }
4666fd7127cSSatish Balay         ierr = MatSetValues(aij->B,1,&row,1,&col,&val,addv); CHKERRQ(ierr);
4671eb62cbbSBarry Smith       }
4681eb62cbbSBarry Smith     }
4691eb62cbbSBarry Smith     count--;
4701eb62cbbSBarry Smith   }
471570da906SBarry Smith   if (aij->recv_waits) PetscFree(aij->recv_waits);
472570da906SBarry Smith   if (aij->rvalues)    PetscFree(aij->rvalues);
4731eb62cbbSBarry Smith 
4741eb62cbbSBarry Smith   /* wait on sends */
4751eb62cbbSBarry Smith   if (aij->nsends) {
4760a198c4cSBarry Smith     send_status = (MPI_Status *) PetscMalloc(aij->nsends*sizeof(MPI_Status));CHKPTRQ(send_status);
477ca161407SBarry Smith     ierr        = MPI_Waitall(aij->nsends,aij->send_waits,send_status);CHKERRQ(ierr);
4780452661fSBarry Smith     PetscFree(send_status);
4791eb62cbbSBarry Smith   }
480b5bd3ad5SBarry Smith   if (aij->send_waits) PetscFree(aij->send_waits);
481b5bd3ad5SBarry Smith   if (aij->svalues)    PetscFree(aij->svalues);
4821eb62cbbSBarry Smith 
48378b31e54SBarry Smith   ierr = MatAssemblyBegin(aij->A,mode); CHKERRQ(ierr);
48478b31e54SBarry Smith   ierr = MatAssemblyEnd(aij->A,mode); CHKERRQ(ierr);
4851eb62cbbSBarry Smith 
4862493cbb0SBarry Smith   /* determine if any processor has disassembled, if so we must
4872493cbb0SBarry Smith      also disassemble ourselfs, in order that we may reassemble. */
48841e46ba1SBarry Smith   /*
48941e46ba1SBarry Smith      if nonzero structure of submatrix B cannot change then we know that
49041e46ba1SBarry Smith      no processor disassembled thus we can skip this stuff
49141e46ba1SBarry Smith   */
49241e46ba1SBarry Smith   if (!((Mat_SeqAIJ*) aij->B->data)->nonew)  {
493ca161407SBarry Smith     ierr = MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPI_INT,MPI_PROD,mat->comm);CHKERRQ(ierr);
494227d817aSBarry Smith     if (mat->was_assembled && !other_disassembled) {
4952493cbb0SBarry Smith       ierr = DisAssemble_MPIAIJ(mat); CHKERRQ(ierr);
4962493cbb0SBarry Smith     }
49741e46ba1SBarry Smith   }
4982493cbb0SBarry Smith 
4996d4a8577SBarry Smith   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
50078b31e54SBarry Smith     ierr = MatSetUpMultiply_MPIAIJ(mat); CHKERRQ(ierr);
5015e42470aSBarry Smith   }
50278b31e54SBarry Smith   ierr = MatAssemblyBegin(aij->B,mode); CHKERRQ(ierr);
50378b31e54SBarry Smith   ierr = MatAssemblyEnd(aij->B,mode); CHKERRQ(ierr);
5045e42470aSBarry Smith 
5057a0afa10SBarry Smith   if (aij->rowvalues) {PetscFree(aij->rowvalues); aij->rowvalues = 0;}
5063a40ed3dSBarry Smith   PetscFunctionReturn(0);
5078a729477SBarry Smith }
5088a729477SBarry Smith 
5095615d1e5SSatish Balay #undef __FUNC__
5105615d1e5SSatish Balay #define __FUNC__ "MatZeroEntries_MPIAIJ"
5118f6be9afSLois Curfman McInnes int MatZeroEntries_MPIAIJ(Mat A)
5121eb62cbbSBarry Smith {
51344a69424SLois Curfman McInnes   Mat_MPIAIJ *l = (Mat_MPIAIJ *) A->data;
514dbd7a890SLois Curfman McInnes   int        ierr;
5153a40ed3dSBarry Smith 
5163a40ed3dSBarry Smith   PetscFunctionBegin;
51778b31e54SBarry Smith   ierr = MatZeroEntries(l->A); CHKERRQ(ierr);
51878b31e54SBarry Smith   ierr = MatZeroEntries(l->B); CHKERRQ(ierr);
5193a40ed3dSBarry Smith   PetscFunctionReturn(0);
5201eb62cbbSBarry Smith }
5211eb62cbbSBarry Smith 
5225615d1e5SSatish Balay #undef __FUNC__
5235615d1e5SSatish Balay #define __FUNC__ "MatZeroRows_MPIAIJ"
5248f6be9afSLois Curfman McInnes int MatZeroRows_MPIAIJ(Mat A,IS is,Scalar *diag)
5251eb62cbbSBarry Smith {
52644a69424SLois Curfman McInnes   Mat_MPIAIJ     *l = (Mat_MPIAIJ *) A->data;
52717699dbbSLois Curfman McInnes   int            i,ierr,N, *rows,*owners = l->rowners,size = l->size;
5286a5c57faSSatish Balay   int            *procs,*nprocs,j,found,idx,nsends,*work,row;
52917699dbbSLois Curfman McInnes   int            nmax,*svalues,*starts,*owner,nrecvs,rank = l->rank;
5305392566eSBarry Smith   int            *rvalues,tag = A->tag,count,base,slen,n,*source;
5316a5c57faSSatish Balay   int            *lens,imdex,*lrows,*values,rstart=l->rstart;
532d6dfbf8fSBarry Smith   MPI_Comm       comm = A->comm;
5331eb62cbbSBarry Smith   MPI_Request    *send_waits,*recv_waits;
5341eb62cbbSBarry Smith   MPI_Status     recv_status,*send_status;
5351eb62cbbSBarry Smith   IS             istmp;
5361eb62cbbSBarry Smith 
5373a40ed3dSBarry Smith   PetscFunctionBegin;
53877c4ece6SBarry Smith   ierr = ISGetSize(is,&N); CHKERRQ(ierr);
53978b31e54SBarry Smith   ierr = ISGetIndices(is,&rows); CHKERRQ(ierr);
5401eb62cbbSBarry Smith 
5411eb62cbbSBarry Smith   /*  first count number of contributors to each processor */
5420452661fSBarry Smith   nprocs = (int *) PetscMalloc( 2*size*sizeof(int) ); CHKPTRQ(nprocs);
543cddf8d76SBarry Smith   PetscMemzero(nprocs,2*size*sizeof(int)); procs = nprocs + size;
5440452661fSBarry Smith   owner = (int *) PetscMalloc((N+1)*sizeof(int)); CHKPTRQ(owner); /* see note*/
5451eb62cbbSBarry Smith   for ( i=0; i<N; i++ ) {
5461eb62cbbSBarry Smith     idx = rows[i];
5471eb62cbbSBarry Smith     found = 0;
54817699dbbSLois Curfman McInnes     for ( j=0; j<size; j++ ) {
5491eb62cbbSBarry Smith       if (idx >= owners[j] && idx < owners[j+1]) {
5501eb62cbbSBarry Smith         nprocs[j]++; procs[j] = 1; owner[i] = j; found = 1; break;
5511eb62cbbSBarry Smith       }
5521eb62cbbSBarry Smith     }
553a8c6a408SBarry Smith     if (!found) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Index out of range");
5541eb62cbbSBarry Smith   }
55517699dbbSLois Curfman McInnes   nsends = 0;  for ( i=0; i<size; i++ ) { nsends += procs[i];}
5561eb62cbbSBarry Smith 
5571eb62cbbSBarry Smith   /* inform other processors of number of messages and max length*/
5580452661fSBarry Smith   work = (int *) PetscMalloc( size*sizeof(int) ); CHKPTRQ(work);
559ca161407SBarry Smith   ierr = MPI_Allreduce( procs, work,size,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr);
56017699dbbSLois Curfman McInnes   nrecvs = work[rank];
561ca161407SBarry Smith   ierr = MPI_Allreduce( nprocs, work,size,MPI_INT,MPI_MAX,comm);CHKERRQ(ierr);
56217699dbbSLois Curfman McInnes   nmax = work[rank];
5630452661fSBarry Smith   PetscFree(work);
5641eb62cbbSBarry Smith 
5651eb62cbbSBarry Smith   /* post receives:   */
5663a40ed3dSBarry Smith   rvalues = (int *) PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(int));CHKPTRQ(rvalues);
567ca161407SBarry Smith   recv_waits = (MPI_Request *) PetscMalloc((nrecvs+1)*sizeof(MPI_Request));CHKPTRQ(recv_waits);
5681eb62cbbSBarry Smith   for ( i=0; i<nrecvs; i++ ) {
569ca161407SBarry Smith     ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPI_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr);
5701eb62cbbSBarry Smith   }
5711eb62cbbSBarry Smith 
5721eb62cbbSBarry Smith   /* do sends:
5731eb62cbbSBarry Smith       1) starts[i] gives the starting index in svalues for stuff going to
5741eb62cbbSBarry Smith          the ith processor
5751eb62cbbSBarry Smith   */
5760452661fSBarry Smith   svalues = (int *) PetscMalloc( (N+1)*sizeof(int) ); CHKPTRQ(svalues);
5773a40ed3dSBarry Smith   send_waits = (MPI_Request *) PetscMalloc( (nsends+1)*sizeof(MPI_Request));CHKPTRQ(send_waits);
5780452661fSBarry Smith   starts = (int *) PetscMalloc( (size+1)*sizeof(int) ); CHKPTRQ(starts);
5791eb62cbbSBarry Smith   starts[0] = 0;
58017699dbbSLois Curfman McInnes   for ( i=1; i<size; i++ ) { starts[i] = starts[i-1] + nprocs[i-1];}
5811eb62cbbSBarry Smith   for ( i=0; i<N; i++ ) {
5821eb62cbbSBarry Smith     svalues[starts[owner[i]]++] = rows[i];
5831eb62cbbSBarry Smith   }
5841eb62cbbSBarry Smith   ISRestoreIndices(is,&rows);
5851eb62cbbSBarry Smith 
5861eb62cbbSBarry Smith   starts[0] = 0;
58717699dbbSLois Curfman McInnes   for ( i=1; i<size+1; i++ ) { starts[i] = starts[i-1] + nprocs[i-1];}
5881eb62cbbSBarry Smith   count = 0;
58917699dbbSLois Curfman McInnes   for ( i=0; i<size; i++ ) {
5901eb62cbbSBarry Smith     if (procs[i]) {
591ca161407SBarry Smith       ierr = MPI_Isend(svalues+starts[i],nprocs[i],MPI_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr);
5921eb62cbbSBarry Smith     }
5931eb62cbbSBarry Smith   }
5940452661fSBarry Smith   PetscFree(starts);
5951eb62cbbSBarry Smith 
59617699dbbSLois Curfman McInnes   base = owners[rank];
5971eb62cbbSBarry Smith 
5981eb62cbbSBarry Smith   /*  wait on receives */
5990452661fSBarry Smith   lens   = (int *) PetscMalloc( 2*(nrecvs+1)*sizeof(int) ); CHKPTRQ(lens);
6001eb62cbbSBarry Smith   source = lens + nrecvs;
6011eb62cbbSBarry Smith   count  = nrecvs; slen = 0;
6021eb62cbbSBarry Smith   while (count) {
603ca161407SBarry Smith     ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
6041eb62cbbSBarry Smith     /* unpack receives into our local space */
605ca161407SBarry Smith     ierr = MPI_Get_count(&recv_status,MPI_INT,&n);CHKERRQ(ierr);
606d6dfbf8fSBarry Smith     source[imdex]  = recv_status.MPI_SOURCE;
607d6dfbf8fSBarry Smith     lens[imdex]  = n;
6081eb62cbbSBarry Smith     slen += n;
6091eb62cbbSBarry Smith     count--;
6101eb62cbbSBarry Smith   }
6110452661fSBarry Smith   PetscFree(recv_waits);
6121eb62cbbSBarry Smith 
6131eb62cbbSBarry Smith   /* move the data into the send scatter */
6140452661fSBarry Smith   lrows = (int *) PetscMalloc( (slen+1)*sizeof(int) ); CHKPTRQ(lrows);
6151eb62cbbSBarry Smith   count = 0;
6161eb62cbbSBarry Smith   for ( i=0; i<nrecvs; i++ ) {
6171eb62cbbSBarry Smith     values = rvalues + i*nmax;
6181eb62cbbSBarry Smith     for ( j=0; j<lens[i]; j++ ) {
6191eb62cbbSBarry Smith       lrows[count++] = values[j] - base;
6201eb62cbbSBarry Smith     }
6211eb62cbbSBarry Smith   }
6220452661fSBarry Smith   PetscFree(rvalues); PetscFree(lens);
6230452661fSBarry Smith   PetscFree(owner); PetscFree(nprocs);
6241eb62cbbSBarry Smith 
6251eb62cbbSBarry Smith   /* actually zap the local rows */
626029af93fSBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,slen,lrows,&istmp);CHKERRQ(ierr);
627464493b3SBarry Smith   PLogObjectParent(A,istmp);
6286a5c57faSSatish Balay 
6296eb55b6aSBarry Smith   /*
6306eb55b6aSBarry Smith         Zero the required rows. If the "diagonal block" of the matrix
6316eb55b6aSBarry Smith      is square and the user wishes to set the diagonal we use seperate
6326eb55b6aSBarry Smith      code so that MatSetValues() is not called for each diagonal allocating
6336eb55b6aSBarry Smith      new memory, thus calling lots of mallocs and slowing things down.
6346eb55b6aSBarry Smith 
6356eb55b6aSBarry Smith        Contributed by: Mathew Knepley
6366eb55b6aSBarry Smith   */
637e2d53e46SBarry Smith   /* must zero l->B before l->A because the (diag) case below may put values into l->B*/
638e2d53e46SBarry Smith   ierr = MatZeroRows(l->B,istmp,0); CHKERRQ(ierr);
6396eb55b6aSBarry Smith   if (diag && (l->A->M == l->A->N)) {
6406eb55b6aSBarry Smith     ierr      = MatZeroRows(l->A,istmp,diag); CHKERRQ(ierr);
641e2d53e46SBarry Smith   } else if (diag) {
642e2d53e46SBarry Smith     ierr = MatZeroRows(l->A,istmp,0); CHKERRQ(ierr);
643*fa46199cSSatish Balay     if (((Mat_SeqAIJ*)l->A->data)->nonew) {
644*fa46199cSSatish Balay       SETERRQ(PETSC_ERR_SUP,0,"MatZeroRows() on rectangular matrices cannot be used with the Mat options\n\
645*fa46199cSSatish Balay MAT_NO_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR");
6466525c446SSatish Balay     }
647e2d53e46SBarry Smith     for ( i = 0; i < slen; i++ ) {
648e2d53e46SBarry Smith       row  = lrows[i] + rstart;
649e2d53e46SBarry Smith       ierr = MatSetValues(A,1,&row,1,&row,diag,INSERT_VALUES);CHKERRQ(ierr);
650e2d53e46SBarry Smith     }
651e2d53e46SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
652e2d53e46SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
6536eb55b6aSBarry Smith   } else {
6546a5c57faSSatish Balay     ierr = MatZeroRows(l->A,istmp,0); CHKERRQ(ierr);
6556eb55b6aSBarry Smith   }
65678b31e54SBarry Smith   ierr = ISDestroy(istmp); CHKERRQ(ierr);
6576a5c57faSSatish Balay   PetscFree(lrows);
65872dacd9aSBarry Smith 
6591eb62cbbSBarry Smith   /* wait on sends */
6601eb62cbbSBarry Smith   if (nsends) {
661ca161407SBarry Smith     send_status = (MPI_Status *) PetscMalloc(nsends*sizeof(MPI_Status));CHKPTRQ(send_status);
662ca161407SBarry Smith     ierr        = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr);
6630452661fSBarry Smith     PetscFree(send_status);
6641eb62cbbSBarry Smith   }
6650452661fSBarry Smith   PetscFree(send_waits); PetscFree(svalues);
6661eb62cbbSBarry Smith 
6673a40ed3dSBarry Smith   PetscFunctionReturn(0);
6681eb62cbbSBarry Smith }
6691eb62cbbSBarry Smith 
6705615d1e5SSatish Balay #undef __FUNC__
6715615d1e5SSatish Balay #define __FUNC__ "MatMult_MPIAIJ"
6728f6be9afSLois Curfman McInnes int MatMult_MPIAIJ(Mat A,Vec xx,Vec yy)
6731eb62cbbSBarry Smith {
674416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data;
675fbd6ef76SBarry Smith   int        ierr,nt;
676416022c9SBarry Smith 
6773a40ed3dSBarry Smith   PetscFunctionBegin;
678a2ce50c7SBarry Smith   ierr = VecGetLocalSize(xx,&nt);  CHKERRQ(ierr);
679fbd6ef76SBarry Smith   if (nt != a->n) {
680a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_SIZ,0,"Incompatible partition of A and xx");
681fbd6ef76SBarry Smith   }
68243a90d84SBarry Smith   ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx); CHKERRQ(ierr);
683f830108cSBarry Smith   ierr = (*a->A->ops->mult)(a->A,xx,yy); CHKERRQ(ierr);
68443a90d84SBarry Smith   ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx); CHKERRQ(ierr);
685f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy); CHKERRQ(ierr);
6863a40ed3dSBarry Smith   PetscFunctionReturn(0);
6871eb62cbbSBarry Smith }
6881eb62cbbSBarry Smith 
6895615d1e5SSatish Balay #undef __FUNC__
6905615d1e5SSatish Balay #define __FUNC__ "MatMultAdd_MPIAIJ"
6918f6be9afSLois Curfman McInnes int MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
692da3a660dSBarry Smith {
693416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data;
694da3a660dSBarry Smith   int        ierr;
6953a40ed3dSBarry Smith 
6963a40ed3dSBarry Smith   PetscFunctionBegin;
69743a90d84SBarry Smith   ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
698f830108cSBarry Smith   ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz); CHKERRQ(ierr);
69943a90d84SBarry Smith   ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
700f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz); CHKERRQ(ierr);
7013a40ed3dSBarry Smith   PetscFunctionReturn(0);
702da3a660dSBarry Smith }
703da3a660dSBarry Smith 
7045615d1e5SSatish Balay #undef __FUNC__
7055615d1e5SSatish Balay #define __FUNC__ "MatMultTrans_MPIAIJ"
7068f6be9afSLois Curfman McInnes int MatMultTrans_MPIAIJ(Mat A,Vec xx,Vec yy)
707da3a660dSBarry Smith {
708416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data;
709da3a660dSBarry Smith   int        ierr;
710da3a660dSBarry Smith 
7113a40ed3dSBarry Smith   PetscFunctionBegin;
712da3a660dSBarry Smith   /* do nondiagonal part */
713f830108cSBarry Smith   ierr = (*a->B->ops->multtrans)(a->B,xx,a->lvec); CHKERRQ(ierr);
714da3a660dSBarry Smith   /* send it on its way */
715537820f0SBarry Smith   ierr = VecScatterBegin(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx); CHKERRQ(ierr);
716da3a660dSBarry Smith   /* do local part */
717f830108cSBarry Smith   ierr = (*a->A->ops->multtrans)(a->A,xx,yy); CHKERRQ(ierr);
718da3a660dSBarry Smith   /* receive remote parts: note this assumes the values are not actually */
719da3a660dSBarry Smith   /* inserted in yy until the next line, which is true for my implementation*/
720da3a660dSBarry Smith   /* but is not perhaps always true. */
721537820f0SBarry Smith   ierr = VecScatterEnd(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx); CHKERRQ(ierr);
7223a40ed3dSBarry Smith   PetscFunctionReturn(0);
723da3a660dSBarry Smith }
724da3a660dSBarry Smith 
7255615d1e5SSatish Balay #undef __FUNC__
7265615d1e5SSatish Balay #define __FUNC__ "MatMultTransAdd_MPIAIJ"
7278f6be9afSLois Curfman McInnes int MatMultTransAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
728da3a660dSBarry Smith {
729416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data;
730da3a660dSBarry Smith   int        ierr;
731da3a660dSBarry Smith 
7323a40ed3dSBarry Smith   PetscFunctionBegin;
733da3a660dSBarry Smith   /* do nondiagonal part */
734f830108cSBarry Smith   ierr = (*a->B->ops->multtrans)(a->B,xx,a->lvec); CHKERRQ(ierr);
735da3a660dSBarry Smith   /* send it on its way */
736537820f0SBarry Smith   ierr = VecScatterBegin(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx); CHKERRQ(ierr);
737da3a660dSBarry Smith   /* do local part */
738f830108cSBarry Smith   ierr = (*a->A->ops->multtransadd)(a->A,xx,yy,zz); CHKERRQ(ierr);
739da3a660dSBarry Smith   /* receive remote parts: note this assumes the values are not actually */
740da3a660dSBarry Smith   /* inserted in yy until the next line, which is true for my implementation*/
741da3a660dSBarry Smith   /* but is not perhaps always true. */
7420a198c4cSBarry Smith   ierr = VecScatterEnd(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx); CHKERRQ(ierr);
7433a40ed3dSBarry Smith   PetscFunctionReturn(0);
744da3a660dSBarry Smith }
745da3a660dSBarry Smith 
7461eb62cbbSBarry Smith /*
7471eb62cbbSBarry Smith   This only works correctly for square matrices where the subblock A->A is the
7481eb62cbbSBarry Smith    diagonal block
7491eb62cbbSBarry Smith */
7505615d1e5SSatish Balay #undef __FUNC__
7515615d1e5SSatish Balay #define __FUNC__ "MatGetDiagonal_MPIAIJ"
7528f6be9afSLois Curfman McInnes int MatGetDiagonal_MPIAIJ(Mat A,Vec v)
7531eb62cbbSBarry Smith {
7543a40ed3dSBarry Smith   int        ierr;
755416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data;
7563a40ed3dSBarry Smith 
7573a40ed3dSBarry Smith   PetscFunctionBegin;
758a8c6a408SBarry Smith   if (a->M != a->N) SETERRQ(PETSC_ERR_SUP,0,"Supports only square matrix where A->A is diag block");
7595baf8537SBarry Smith   if (a->rstart != a->cstart || a->rend != a->cend) {
760a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_SIZ,0,"row partition must equal col partition");
7613a40ed3dSBarry Smith   }
7623a40ed3dSBarry Smith   ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr);
7633a40ed3dSBarry Smith   PetscFunctionReturn(0);
7641eb62cbbSBarry Smith }
7651eb62cbbSBarry Smith 
7665615d1e5SSatish Balay #undef __FUNC__
7675615d1e5SSatish Balay #define __FUNC__ "MatScale_MPIAIJ"
7688f6be9afSLois Curfman McInnes int MatScale_MPIAIJ(Scalar *aa,Mat A)
769052efed2SBarry Smith {
770052efed2SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data;
771052efed2SBarry Smith   int        ierr;
7723a40ed3dSBarry Smith 
7733a40ed3dSBarry Smith   PetscFunctionBegin;
774052efed2SBarry Smith   ierr = MatScale(aa,a->A); CHKERRQ(ierr);
775052efed2SBarry Smith   ierr = MatScale(aa,a->B); CHKERRQ(ierr);
7763a40ed3dSBarry Smith   PetscFunctionReturn(0);
777052efed2SBarry Smith }
778052efed2SBarry Smith 
7795615d1e5SSatish Balay #undef __FUNC__
780d4bb536fSBarry Smith #define __FUNC__ "MatDestroy_MPIAIJ"
781e1311b90SBarry Smith int MatDestroy_MPIAIJ(Mat mat)
7821eb62cbbSBarry Smith {
78344a69424SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data;
7841eb62cbbSBarry Smith   int        ierr;
78583e2fdc7SBarry Smith 
7863a40ed3dSBarry Smith   PetscFunctionBegin;
78770429bc8SBarry Smith   if (--mat->refct > 0) PetscFunctionReturn(0);
78870429bc8SBarry Smith 
78970429bc8SBarry Smith   if (mat->mapping) {
79070429bc8SBarry Smith     ierr = ISLocalToGlobalMappingDestroy(mat->mapping); CHKERRQ(ierr);
79170429bc8SBarry Smith   }
79270429bc8SBarry Smith   if (mat->bmapping) {
79370429bc8SBarry Smith     ierr = ISLocalToGlobalMappingDestroy(mat->bmapping); CHKERRQ(ierr);
79470429bc8SBarry Smith   }
79561b13de0SBarry Smith   if (mat->rmap) {
79661b13de0SBarry Smith     ierr = MapDestroy(mat->rmap);CHKERRQ(ierr);
79761b13de0SBarry Smith   }
79861b13de0SBarry Smith   if (mat->cmap) {
79961b13de0SBarry Smith     ierr = MapDestroy(mat->cmap);CHKERRQ(ierr);
80061b13de0SBarry Smith   }
8013a40ed3dSBarry Smith #if defined(USE_PETSC_LOG)
802e1311b90SBarry Smith   PLogObjectState((PetscObject)mat,"Rows=%d, Cols=%d",aij->M,aij->N);
803a5a9c739SBarry Smith #endif
80483e2fdc7SBarry Smith   ierr = StashDestroy_Private(&aij->stash); CHKERRQ(ierr);
8050452661fSBarry Smith   PetscFree(aij->rowners);
80678b31e54SBarry Smith   ierr = MatDestroy(aij->A); CHKERRQ(ierr);
80778b31e54SBarry Smith   ierr = MatDestroy(aij->B); CHKERRQ(ierr);
808b1fc9764SSatish Balay #if defined (USE_CTABLE)
809b1fc9764SSatish Balay   if (aij->colmap) TableDelete(aij->colmap);
810b1fc9764SSatish Balay #else
8110452661fSBarry Smith   if (aij->colmap) PetscFree(aij->colmap);
812b1fc9764SSatish Balay #endif
8130452661fSBarry Smith   if (aij->garray) PetscFree(aij->garray);
8141eb62cbbSBarry Smith   if (aij->lvec)   VecDestroy(aij->lvec);
815a56f8943SBarry Smith   if (aij->Mvctx)  VecScatterDestroy(aij->Mvctx);
8167a0afa10SBarry Smith   if (aij->rowvalues) PetscFree(aij->rowvalues);
8170452661fSBarry Smith   PetscFree(aij);
818a5a9c739SBarry Smith   PLogObjectDestroy(mat);
8190452661fSBarry Smith   PetscHeaderDestroy(mat);
8203a40ed3dSBarry Smith   PetscFunctionReturn(0);
8211eb62cbbSBarry Smith }
822ee50ffe9SBarry Smith 
8235615d1e5SSatish Balay #undef __FUNC__
824d4bb536fSBarry Smith #define __FUNC__ "MatView_MPIAIJ_Binary"
8258f6be9afSLois Curfman McInnes extern int MatView_MPIAIJ_Binary(Mat mat,Viewer viewer)
8261eb62cbbSBarry Smith {
827416022c9SBarry Smith   Mat_MPIAIJ  *aij = (Mat_MPIAIJ *) mat->data;
828416022c9SBarry Smith   int         ierr;
829416022c9SBarry Smith 
8303a40ed3dSBarry Smith   PetscFunctionBegin;
83117699dbbSLois Curfman McInnes   if (aij->size == 1) {
832416022c9SBarry Smith     ierr = MatView(aij->A,viewer); CHKERRQ(ierr);
833416022c9SBarry Smith   }
834a8c6a408SBarry Smith   else SETERRQ(PETSC_ERR_SUP,0,"Only uniprocessor output supported");
8353a40ed3dSBarry Smith   PetscFunctionReturn(0);
836416022c9SBarry Smith }
837416022c9SBarry Smith 
8385615d1e5SSatish Balay #undef __FUNC__
8397b2a1423SBarry Smith #define __FUNC__ "MatView_MPIAIJ_ASCIIorDraworSocket"
8407b2a1423SBarry Smith extern int MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,Viewer viewer)
841416022c9SBarry Smith {
84244a69424SLois Curfman McInnes   Mat_MPIAIJ  *aij = (Mat_MPIAIJ *) mat->data;
843dbb450caSBarry Smith   Mat_SeqAIJ* C = (Mat_SeqAIJ*)aij->A->data;
84477ed5343SBarry Smith   int         ierr, format,shift = C->indexshift,rank = aij->rank ;
84577ed5343SBarry Smith   int         size = aij->size;
846d636dbe3SBarry Smith   FILE        *fd;
84719bcc07fSBarry Smith   ViewerType  vtype;
848416022c9SBarry Smith 
8493a40ed3dSBarry Smith   PetscFunctionBegin;
85019bcc07fSBarry Smith   ierr = ViewerGetType(viewer,&vtype); CHKERRQ(ierr);
8513f1db9ecSBarry Smith   if (PetscTypeCompare(vtype,ASCII_VIEWER)) {
85290ace30eSBarry Smith     ierr = ViewerGetFormat(viewer,&format);
8530a198c4cSBarry Smith     if (format == VIEWER_FORMAT_ASCII_INFO_LONG) {
8544e220ebcSLois Curfman McInnes       MatInfo info;
8554e220ebcSLois Curfman McInnes       int     flg;
856a56f8943SBarry Smith       MPI_Comm_rank(mat->comm,&rank);
85790ace30eSBarry Smith       ierr = ViewerASCIIGetPointer(viewer,&fd); CHKERRQ(ierr);
8584e220ebcSLois Curfman McInnes       ierr = MatGetInfo(mat,MAT_LOCAL,&info);
85995e01e2fSLois Curfman McInnes       ierr = OptionsHasName(PETSC_NULL,"-mat_aij_no_inode",&flg); CHKERRQ(ierr);
86077c4ece6SBarry Smith       PetscSequentialPhaseBegin(mat->comm,1);
86195e01e2fSLois Curfman McInnes       if (flg) fprintf(fd,"[%d] Local rows %d nz %d nz alloced %d mem %d, not using I-node routines\n",
8624e220ebcSLois Curfman McInnes          rank,aij->m,(int)info.nz_used,(int)info.nz_allocated,(int)info.memory);
86395e01e2fSLois Curfman McInnes       else fprintf(fd,"[%d] Local rows %d nz %d nz alloced %d mem %d, using I-node routines\n",
8644e220ebcSLois Curfman McInnes          rank,aij->m,(int)info.nz_used,(int)info.nz_allocated,(int)info.memory);
8654e220ebcSLois Curfman McInnes       ierr = MatGetInfo(aij->A,MAT_LOCAL,&info);
8664e220ebcSLois Curfman McInnes       fprintf(fd,"[%d] on-diagonal part: nz %d \n",rank,(int)info.nz_used);
8674e220ebcSLois Curfman McInnes       ierr = MatGetInfo(aij->B,MAT_LOCAL,&info);
8684e220ebcSLois Curfman McInnes       fprintf(fd,"[%d] off-diagonal part: nz %d \n",rank,(int)info.nz_used);
869a56f8943SBarry Smith       fflush(fd);
87077c4ece6SBarry Smith       PetscSequentialPhaseEnd(mat->comm,1);
871a40aa06bSLois Curfman McInnes       ierr = VecScatterView(aij->Mvctx,viewer); CHKERRQ(ierr);
8723a40ed3dSBarry Smith       PetscFunctionReturn(0);
8733a40ed3dSBarry Smith     } else if (format == VIEWER_FORMAT_ASCII_INFO) {
8743a40ed3dSBarry Smith       PetscFunctionReturn(0);
87508480c60SBarry Smith     }
8763f1db9ecSBarry Smith   } else if (PetscTypeCompare(vtype,DRAW_VIEWER)) {
87719bcc07fSBarry Smith     Draw       draw;
87819bcc07fSBarry Smith     PetscTruth isnull;
87977ed5343SBarry Smith     ierr = ViewerDrawGetDraw(viewer,0,&draw); CHKERRQ(ierr);
8803a40ed3dSBarry Smith     ierr = DrawIsNull(draw,&isnull); CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
88119bcc07fSBarry Smith   }
88219bcc07fSBarry Smith 
88317699dbbSLois Curfman McInnes   if (size == 1) {
88478b31e54SBarry Smith     ierr = MatView(aij->A,viewer); CHKERRQ(ierr);
8853a40ed3dSBarry Smith   } else {
88695373324SBarry Smith     /* assemble the entire matrix onto first processor. */
88795373324SBarry Smith     Mat         A;
888ec8511deSBarry Smith     Mat_SeqAIJ *Aloc;
8892eb8c8abSBarry Smith     int         M = aij->M, N = aij->N,m,*ai,*aj,row,*cols,i,*ct;
89095373324SBarry Smith     Scalar      *a;
8912ee70a88SLois Curfman McInnes 
89217699dbbSLois Curfman McInnes     if (!rank) {
89355843e3eSBarry Smith       ierr = MatCreateMPIAIJ(mat->comm,M,N,M,N,0,PETSC_NULL,0,PETSC_NULL,&A);CHKERRQ(ierr);
8943a40ed3dSBarry Smith     } else {
89555843e3eSBarry Smith       ierr = MatCreateMPIAIJ(mat->comm,0,0,M,N,0,PETSC_NULL,0,PETSC_NULL,&A);CHKERRQ(ierr);
89695373324SBarry Smith     }
897464493b3SBarry Smith     PLogObjectParent(mat,A);
898416022c9SBarry Smith 
89995373324SBarry Smith     /* copy over the A part */
900ec8511deSBarry Smith     Aloc = (Mat_SeqAIJ*) aij->A->data;
9012ee70a88SLois Curfman McInnes     m = Aloc->m; ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
90295373324SBarry Smith     row = aij->rstart;
903dbb450caSBarry Smith     for ( i=0; i<ai[m]+shift; i++ ) {aj[i] += aij->cstart + shift;}
90495373324SBarry Smith     for ( i=0; i<m; i++ ) {
905416022c9SBarry Smith       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);CHKERRQ(ierr);
90695373324SBarry Smith       row++; a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
90795373324SBarry Smith     }
9082ee70a88SLois Curfman McInnes     aj = Aloc->j;
909dbb450caSBarry Smith     for ( i=0; i<ai[m]+shift; i++ ) {aj[i] -= aij->cstart + shift;}
91095373324SBarry Smith 
91195373324SBarry Smith     /* copy over the B part */
912ec8511deSBarry Smith     Aloc = (Mat_SeqAIJ*) aij->B->data;
9132ee70a88SLois Curfman McInnes     m = Aloc->m;  ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
91495373324SBarry Smith     row = aij->rstart;
9150452661fSBarry Smith     ct = cols = (int *) PetscMalloc( (ai[m]+1)*sizeof(int) ); CHKPTRQ(cols);
916dbb450caSBarry Smith     for ( i=0; i<ai[m]+shift; i++ ) {cols[i] = aij->garray[aj[i]+shift];}
91795373324SBarry Smith     for ( i=0; i<m; i++ ) {
918416022c9SBarry Smith       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);CHKERRQ(ierr);
91995373324SBarry Smith       row++; a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
92095373324SBarry Smith     }
9210452661fSBarry Smith     PetscFree(ct);
9226d4a8577SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
9236d4a8577SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
92455843e3eSBarry Smith     /*
92555843e3eSBarry Smith        Everyone has to call to draw the matrix since the graphics waits are
92655843e3eSBarry Smith        synchronized across all processors that share the Draw object
92755843e3eSBarry Smith     */
9283f1db9ecSBarry Smith     if (!rank || PetscTypeCompare(vtype,DRAW_VIEWER)) {
92978b31e54SBarry Smith       ierr = MatView(((Mat_MPIAIJ*)(A->data))->A,viewer); CHKERRQ(ierr);
93095373324SBarry Smith     }
93178b31e54SBarry Smith     ierr = MatDestroy(A); CHKERRQ(ierr);
93295373324SBarry Smith   }
9333a40ed3dSBarry Smith   PetscFunctionReturn(0);
9341eb62cbbSBarry Smith }
9351eb62cbbSBarry Smith 
9365615d1e5SSatish Balay #undef __FUNC__
937d4bb536fSBarry Smith #define __FUNC__ "MatView_MPIAIJ"
938e1311b90SBarry Smith int MatView_MPIAIJ(Mat mat,Viewer viewer)
939416022c9SBarry Smith {
940416022c9SBarry Smith   int         ierr;
94119bcc07fSBarry Smith   ViewerType  vtype;
942416022c9SBarry Smith 
9433a40ed3dSBarry Smith   PetscFunctionBegin;
94419bcc07fSBarry Smith   ierr = ViewerGetType(viewer,&vtype); CHKERRQ(ierr);
9453f1db9ecSBarry Smith   if (PetscTypeCompare(vtype,ASCII_VIEWER) || PetscTypeCompare(vtype,DRAW_VIEWER) ||
9467b2a1423SBarry Smith       PetscTypeCompare(vtype,SOCKET_VIEWER)) {
9477b2a1423SBarry Smith     ierr = MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer); CHKERRQ(ierr);
9483f1db9ecSBarry Smith   } else if (PetscTypeCompare(vtype,BINARY_VIEWER)) {
9493a40ed3dSBarry Smith     ierr = MatView_MPIAIJ_Binary(mat,viewer);CHKERRQ(ierr);
9505cd90555SBarry Smith   } else {
9515cd90555SBarry Smith     SETERRQ(1,1,"Viewer type not supported by PETSc object");
952416022c9SBarry Smith   }
9533a40ed3dSBarry Smith   PetscFunctionReturn(0);
954416022c9SBarry Smith }
955416022c9SBarry Smith 
9561eb62cbbSBarry Smith /*
9571eb62cbbSBarry Smith     This has to provide several versions.
9581eb62cbbSBarry Smith 
9591eb62cbbSBarry Smith      2) a) use only local smoothing updating outer values only once.
9601eb62cbbSBarry Smith         b) local smoothing updating outer values each inner iteration
961d6dfbf8fSBarry Smith      3) color updating out values betwen colors.
9621eb62cbbSBarry Smith */
9635615d1e5SSatish Balay #undef __FUNC__
9645615d1e5SSatish Balay #define __FUNC__ "MatRelax_MPIAIJ"
9658f6be9afSLois Curfman McInnes int MatRelax_MPIAIJ(Mat matin,Vec bb,double omega,MatSORType flag,
966dbb450caSBarry Smith                            double fshift,int its,Vec xx)
9678a729477SBarry Smith {
96844a69424SLois Curfman McInnes   Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data;
969d6dfbf8fSBarry Smith   Mat        AA = mat->A, BB = mat->B;
970ec8511deSBarry Smith   Mat_SeqAIJ *A = (Mat_SeqAIJ *) AA->data, *B = (Mat_SeqAIJ *)BB->data;
971c16cb8f2SBarry Smith   Scalar     *b,*x,*xs,*ls,d,*v,sum;
9726abc6512SBarry Smith   int        ierr,*idx, *diag;
973416022c9SBarry Smith   int        n = mat->n, m = mat->m, i,shift = A->indexshift;
9748a729477SBarry Smith 
9753a40ed3dSBarry Smith   PetscFunctionBegin;
9762e8a6d31SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
9772e8a6d31SBarry Smith   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
9782e8a6d31SBarry Smith   ierr = VecGetArray(mat->lvec,&ls);CHKERRQ(ierr);
979dbb450caSBarry Smith   xs = x + shift; /* shift by one for index start of 1 */
980dbb450caSBarry Smith   ls = ls + shift;
98183e2fdc7SBarry Smith   if (!A->diag) {ierr = MatMarkDiag_SeqAIJ(AA); CHKERRQ(ierr);}
982d6dfbf8fSBarry Smith   diag = A->diag;
983c16cb8f2SBarry Smith   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){
984da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
985f830108cSBarry Smith       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,its,xx);CHKERRQ(ierr);
9862e8a6d31SBarry Smith       ierr = VecRestoreArray(xx,&x); CHKERRQ(ierr);
9872e8a6d31SBarry Smith       ierr = VecRestoreArray(bb,&b); CHKERRQ(ierr);
9882e8a6d31SBarry Smith       ierr = VecRestoreArray(mat->lvec,&ls);CHKERRQ(ierr);
9893a40ed3dSBarry Smith       PetscFunctionReturn(0);
990da3a660dSBarry Smith     }
9913a40ed3dSBarry Smith     ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
9923a40ed3dSBarry Smith     ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
993d6dfbf8fSBarry Smith     while (its--) {
994d6dfbf8fSBarry Smith       /* go down through the rows */
995d6dfbf8fSBarry Smith       for ( i=0; i<m; i++ ) {
996d6dfbf8fSBarry Smith         n    = A->i[i+1] - A->i[i];
9974d197716SBarry Smith 	PLogFlops(4*n+3);
998dbb450caSBarry Smith         idx  = A->j + A->i[i] + shift;
999dbb450caSBarry Smith         v    = A->a + A->i[i] + shift;
1000d6dfbf8fSBarry Smith         sum  = b[i];
1001d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1002dbb450caSBarry Smith         d    = fshift + A->a[diag[i]+shift];
1003d6dfbf8fSBarry Smith         n    = B->i[i+1] - B->i[i];
1004dbb450caSBarry Smith         idx  = B->j + B->i[i] + shift;
1005dbb450caSBarry Smith         v    = B->a + B->i[i] + shift;
1006d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,ls,v,idx,n);
100755a1b374SBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + A->a[diag[i]+shift]*x[i])/d;
1008d6dfbf8fSBarry Smith       }
1009d6dfbf8fSBarry Smith       /* come up through the rows */
1010d6dfbf8fSBarry Smith       for ( i=m-1; i>-1; i-- ) {
1011d6dfbf8fSBarry Smith         n    = A->i[i+1] - A->i[i];
10124d197716SBarry Smith 	PLogFlops(4*n+3)
1013dbb450caSBarry Smith         idx  = A->j + A->i[i] + shift;
1014dbb450caSBarry Smith         v    = A->a + A->i[i] + shift;
1015d6dfbf8fSBarry Smith         sum  = b[i];
1016d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1017dbb450caSBarry Smith         d    = fshift + A->a[diag[i]+shift];
1018d6dfbf8fSBarry Smith         n    = B->i[i+1] - B->i[i];
1019dbb450caSBarry Smith         idx  = B->j + B->i[i] + shift;
1020dbb450caSBarry Smith         v    = B->a + B->i[i] + shift;
1021d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,ls,v,idx,n);
102255a1b374SBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + A->a[diag[i]+shift]*x[i])/d;
1023d6dfbf8fSBarry Smith       }
1024d6dfbf8fSBarry Smith     }
10253a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_FORWARD_SWEEP){
1026da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
1027f830108cSBarry Smith       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,its,xx);CHKERRQ(ierr);
10282e8a6d31SBarry Smith       ierr = VecRestoreArray(xx,&x); CHKERRQ(ierr);
10292e8a6d31SBarry Smith       ierr = VecRestoreArray(bb,&b); CHKERRQ(ierr);
10302e8a6d31SBarry Smith       ierr = VecRestoreArray(mat->lvec,&ls);CHKERRQ(ierr);
10313a40ed3dSBarry Smith       PetscFunctionReturn(0);
1032da3a660dSBarry Smith     }
10333a40ed3dSBarry Smith     ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10343a40ed3dSBarry Smith     ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
1035d6dfbf8fSBarry Smith     while (its--) {
1036d6dfbf8fSBarry Smith       for ( i=0; i<m; i++ ) {
1037d6dfbf8fSBarry Smith         n    = A->i[i+1] - A->i[i];
10384d197716SBarry Smith 	PLogFlops(4*n+3);
1039dbb450caSBarry Smith         idx  = A->j + A->i[i] + shift;
1040dbb450caSBarry Smith         v    = A->a + A->i[i] + shift;
1041d6dfbf8fSBarry Smith         sum  = b[i];
1042d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1043dbb450caSBarry Smith         d    = fshift + A->a[diag[i]+shift];
1044d6dfbf8fSBarry Smith         n    = B->i[i+1] - B->i[i];
1045dbb450caSBarry Smith         idx  = B->j + B->i[i] + shift;
1046dbb450caSBarry Smith         v    = B->a + B->i[i] + shift;
1047d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,ls,v,idx,n);
104855a1b374SBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + A->a[diag[i]+shift]*x[i])/d;
1049d6dfbf8fSBarry Smith       }
1050d6dfbf8fSBarry Smith     }
10513a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_BACKWARD_SWEEP){
1052da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
1053f830108cSBarry Smith       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,its,xx);CHKERRQ(ierr);
10542e8a6d31SBarry Smith       ierr = VecRestoreArray(xx,&x); CHKERRQ(ierr);
10552e8a6d31SBarry Smith       ierr = VecRestoreArray(bb,&b); CHKERRQ(ierr);
10562e8a6d31SBarry Smith       ierr = VecRestoreArray(mat->lvec,&ls);CHKERRQ(ierr);
10573a40ed3dSBarry Smith       PetscFunctionReturn(0);
1058da3a660dSBarry Smith     }
10592e8a6d31SBarry Smith     ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10602e8a6d31SBarry Smith     ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
1061d6dfbf8fSBarry Smith     while (its--) {
1062d6dfbf8fSBarry Smith       for ( i=m-1; i>-1; i-- ) {
1063d6dfbf8fSBarry Smith         n    = A->i[i+1] - A->i[i];
10644d197716SBarry Smith 	PLogFlops(4*n+3);
1065dbb450caSBarry Smith         idx  = A->j + A->i[i] + shift;
1066dbb450caSBarry Smith         v    = A->a + A->i[i] + shift;
1067d6dfbf8fSBarry Smith         sum  = b[i];
1068d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1069dbb450caSBarry Smith         d    = fshift + A->a[diag[i]+shift];
1070d6dfbf8fSBarry Smith         n    = B->i[i+1] - B->i[i];
1071dbb450caSBarry Smith         idx  = B->j + B->i[i] + shift;
1072dbb450caSBarry Smith         v    = B->a + B->i[i] + shift;
1073d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,ls,v,idx,n);
107455a1b374SBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + A->a[diag[i]+shift]*x[i])/d;
1075d6dfbf8fSBarry Smith       }
1076d6dfbf8fSBarry Smith     }
10773a40ed3dSBarry Smith   } else {
1078a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_SUP,0,"Parallel SOR not supported");
1079c16cb8f2SBarry Smith   }
10802e8a6d31SBarry Smith   ierr = VecRestoreArray(xx,&x); CHKERRQ(ierr);
10812e8a6d31SBarry Smith   ierr = VecRestoreArray(bb,&b); CHKERRQ(ierr);
10822e8a6d31SBarry Smith   ierr = VecRestoreArray(mat->lvec,&ls);CHKERRQ(ierr);
10833a40ed3dSBarry Smith   PetscFunctionReturn(0);
10848a729477SBarry Smith }
1085a66be287SLois Curfman McInnes 
10865615d1e5SSatish Balay #undef __FUNC__
1087d4bb536fSBarry Smith #define __FUNC__ "MatGetInfo_MPIAIJ"
10888f6be9afSLois Curfman McInnes int MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info)
1089a66be287SLois Curfman McInnes {
1090a66be287SLois Curfman McInnes   Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data;
1091a66be287SLois Curfman McInnes   Mat        A = mat->A, B = mat->B;
10924e220ebcSLois Curfman McInnes   int        ierr;
10934e220ebcSLois Curfman McInnes   double     isend[5], irecv[5];
1094a66be287SLois Curfman McInnes 
10953a40ed3dSBarry Smith   PetscFunctionBegin;
10964e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
10974e220ebcSLois Curfman McInnes   ierr = MatGetInfo(A,MAT_LOCAL,info); CHKERRQ(ierr);
10984e220ebcSLois Curfman McInnes   isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
10994e220ebcSLois Curfman McInnes   isend[3] = info->memory;  isend[4] = info->mallocs;
11004e220ebcSLois Curfman McInnes   ierr = MatGetInfo(B,MAT_LOCAL,info); CHKERRQ(ierr);
11014e220ebcSLois Curfman McInnes   isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
11024e220ebcSLois Curfman McInnes   isend[3] += info->memory;  isend[4] += info->mallocs;
1103a66be287SLois Curfman McInnes   if (flag == MAT_LOCAL) {
11044e220ebcSLois Curfman McInnes     info->nz_used      = isend[0];
11054e220ebcSLois Curfman McInnes     info->nz_allocated = isend[1];
11064e220ebcSLois Curfman McInnes     info->nz_unneeded  = isend[2];
11074e220ebcSLois Curfman McInnes     info->memory       = isend[3];
11084e220ebcSLois Curfman McInnes     info->mallocs      = isend[4];
1109a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_MAX) {
1110ca161407SBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPI_DOUBLE,MPI_MAX,matin->comm);CHKERRQ(ierr);
11114e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
11124e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
11134e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
11144e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
11154e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1116a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_SUM) {
1117ca161407SBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPI_DOUBLE,MPI_SUM,matin->comm);CHKERRQ(ierr);
11184e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
11194e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
11204e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
11214e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
11224e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1123a66be287SLois Curfman McInnes   }
11244e220ebcSLois Curfman McInnes   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
11254e220ebcSLois Curfman McInnes   info->fill_ratio_needed = 0;
11264e220ebcSLois Curfman McInnes   info->factor_mallocs    = 0;
11278d700155SBarry Smith   info->rows_global       = (double)mat->M;
11288d700155SBarry Smith   info->columns_global    = (double)mat->N;
11298d700155SBarry Smith   info->rows_local        = (double)mat->m;
11308d700155SBarry Smith   info->columns_local     = (double)mat->N;
11314e220ebcSLois Curfman McInnes 
11323a40ed3dSBarry Smith   PetscFunctionReturn(0);
1133a66be287SLois Curfman McInnes }
1134a66be287SLois Curfman McInnes 
11355615d1e5SSatish Balay #undef __FUNC__
1136d4bb536fSBarry Smith #define __FUNC__ "MatSetOption_MPIAIJ"
11378f6be9afSLois Curfman McInnes int MatSetOption_MPIAIJ(Mat A,MatOption op)
1138c74985f6SBarry Smith {
1139c0bbcb79SLois Curfman McInnes   Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data;
11401dee9f54SBarry Smith   int        ierr;
1141c74985f6SBarry Smith 
11423a40ed3dSBarry Smith   PetscFunctionBegin;
11436d4a8577SBarry Smith   if (op == MAT_NO_NEW_NONZERO_LOCATIONS ||
11446d4a8577SBarry Smith       op == MAT_YES_NEW_NONZERO_LOCATIONS ||
11456da5968aSLois Curfman McInnes       op == MAT_COLUMNS_UNSORTED ||
1146c2653b3dSLois Curfman McInnes       op == MAT_COLUMNS_SORTED ||
11474787f768SSatish Balay       op == MAT_NEW_NONZERO_ALLOCATION_ERR ||
11484787f768SSatish Balay       op == MAT_NEW_NONZERO_LOCATION_ERR) {
11491dee9f54SBarry Smith         ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
11501dee9f54SBarry Smith         ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
1151b1fbbac0SLois Curfman McInnes   } else if (op == MAT_ROW_ORIENTED) {
1152aeafbbfcSLois Curfman McInnes     a->roworiented = 1;
11531dee9f54SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
11541dee9f54SBarry Smith     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
1155b1fbbac0SLois Curfman McInnes   } else if (op == MAT_ROWS_SORTED ||
11566da5968aSLois Curfman McInnes              op == MAT_ROWS_UNSORTED ||
11576d4a8577SBarry Smith              op == MAT_SYMMETRIC ||
11586d4a8577SBarry Smith              op == MAT_STRUCTURALLY_SYMMETRIC ||
11596d4a8577SBarry Smith              op == MAT_YES_NEW_DIAGONALS)
1160981c4779SBarry Smith     PLogInfo(A,"MatSetOption_MPIAIJ:Option ignored\n");
11616d4a8577SBarry Smith   else if (op == MAT_COLUMN_ORIENTED) {
11624b0e389bSBarry Smith     a->roworiented = 0;
11631dee9f54SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
11641dee9f54SBarry Smith     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
11652b362799SSatish Balay   } else if (op == MAT_IGNORE_OFF_PROC_ENTRIES) {
116690f02eecSBarry Smith     a->donotstash = 1;
11673a40ed3dSBarry Smith   } else if (op == MAT_NO_NEW_DIAGONALS){
11683a40ed3dSBarry Smith     SETERRQ(PETSC_ERR_SUP,0,"MAT_NO_NEW_DIAGONALS");
11693a40ed3dSBarry Smith   } else {
11703a40ed3dSBarry Smith     SETERRQ(PETSC_ERR_SUP,0,"unknown option");
11713a40ed3dSBarry Smith   }
11723a40ed3dSBarry Smith   PetscFunctionReturn(0);
1173c74985f6SBarry Smith }
1174c74985f6SBarry Smith 
11755615d1e5SSatish Balay #undef __FUNC__
1176d4bb536fSBarry Smith #define __FUNC__ "MatGetSize_MPIAIJ"
11778f6be9afSLois Curfman McInnes int MatGetSize_MPIAIJ(Mat matin,int *m,int *n)
1178c74985f6SBarry Smith {
117944a69424SLois Curfman McInnes   Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data;
11803a40ed3dSBarry Smith 
11813a40ed3dSBarry Smith   PetscFunctionBegin;
11820752156aSBarry Smith   if (m) *m = mat->M;
11830752156aSBarry Smith   if (n) *n = mat->N;
11843a40ed3dSBarry Smith   PetscFunctionReturn(0);
1185c74985f6SBarry Smith }
1186c74985f6SBarry Smith 
11875615d1e5SSatish Balay #undef __FUNC__
1188d4bb536fSBarry Smith #define __FUNC__ "MatGetLocalSize_MPIAIJ"
11898f6be9afSLois Curfman McInnes int MatGetLocalSize_MPIAIJ(Mat matin,int *m,int *n)
1190c74985f6SBarry Smith {
119144a69424SLois Curfman McInnes   Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data;
11923a40ed3dSBarry Smith 
11933a40ed3dSBarry Smith   PetscFunctionBegin;
11940752156aSBarry Smith   if (m) *m = mat->m;
1195f830108cSBarry Smith   if (n) *n = mat->n;
11963a40ed3dSBarry Smith   PetscFunctionReturn(0);
1197c74985f6SBarry Smith }
1198c74985f6SBarry Smith 
11995615d1e5SSatish Balay #undef __FUNC__
1200d4bb536fSBarry Smith #define __FUNC__ "MatGetOwnershipRange_MPIAIJ"
12018f6be9afSLois Curfman McInnes int MatGetOwnershipRange_MPIAIJ(Mat matin,int *m,int *n)
1202c74985f6SBarry Smith {
120344a69424SLois Curfman McInnes   Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data;
12043a40ed3dSBarry Smith 
12053a40ed3dSBarry Smith   PetscFunctionBegin;
1206c74985f6SBarry Smith   *m = mat->rstart; *n = mat->rend;
12073a40ed3dSBarry Smith   PetscFunctionReturn(0);
1208c74985f6SBarry Smith }
1209c74985f6SBarry Smith 
12106d84be18SBarry Smith extern int MatGetRow_SeqAIJ(Mat,int,int*,int**,Scalar**);
12116d84be18SBarry Smith extern int MatRestoreRow_SeqAIJ(Mat,int,int*,int**,Scalar**);
12126d84be18SBarry Smith 
12135615d1e5SSatish Balay #undef __FUNC__
12145615d1e5SSatish Balay #define __FUNC__ "MatGetRow_MPIAIJ"
12156d84be18SBarry Smith int MatGetRow_MPIAIJ(Mat matin,int row,int *nz,int **idx,Scalar **v)
121639e00950SLois Curfman McInnes {
1217154123eaSLois Curfman McInnes   Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data;
121870f0671dSBarry Smith   Scalar     *vworkA, *vworkB, **pvA, **pvB,*v_p;
1219154123eaSLois Curfman McInnes   int        i, ierr, *cworkA, *cworkB, **pcA, **pcB, cstart = mat->cstart;
1220154123eaSLois Curfman McInnes   int        nztot, nzA, nzB, lrow, rstart = mat->rstart, rend = mat->rend;
122170f0671dSBarry Smith   int        *cmap, *idx_p;
122239e00950SLois Curfman McInnes 
12233a40ed3dSBarry Smith   PetscFunctionBegin;
1224a8c6a408SBarry Smith   if (mat->getrowactive == PETSC_TRUE) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Already active");
12257a0afa10SBarry Smith   mat->getrowactive = PETSC_TRUE;
12267a0afa10SBarry Smith 
122770f0671dSBarry Smith   if (!mat->rowvalues && (idx || v)) {
12287a0afa10SBarry Smith     /*
12297a0afa10SBarry Smith         allocate enough space to hold information from the longest row.
12307a0afa10SBarry Smith     */
12317a0afa10SBarry Smith     Mat_SeqAIJ *Aa = (Mat_SeqAIJ *) mat->A->data,*Ba = (Mat_SeqAIJ *) mat->B->data;
1232c16cb8f2SBarry Smith     int     max = 1,m = mat->m,tmp;
1233c16cb8f2SBarry Smith     for ( i=0; i<m; i++ ) {
12347a0afa10SBarry Smith       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
12357a0afa10SBarry Smith       if (max < tmp) { max = tmp; }
12367a0afa10SBarry Smith     }
12373f97c4b0SBarry Smith     mat->rowvalues  = (Scalar *) PetscMalloc(max*(sizeof(int)+sizeof(Scalar)));CHKPTRQ(mat->rowvalues);
12387a0afa10SBarry Smith     mat->rowindices = (int *) (mat->rowvalues + max);
12397a0afa10SBarry Smith   }
12407a0afa10SBarry Smith 
1241a8c6a408SBarry Smith   if (row < rstart || row >= rend) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Only local rows")
1242abc0e9e4SLois Curfman McInnes   lrow = row - rstart;
124339e00950SLois Curfman McInnes 
1244154123eaSLois Curfman McInnes   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1245154123eaSLois Curfman McInnes   if (!v)   {pvA = 0; pvB = 0;}
1246154123eaSLois Curfman McInnes   if (!idx) {pcA = 0; if (!v) pcB = 0;}
1247f830108cSBarry Smith   ierr = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA); CHKERRQ(ierr);
1248f830108cSBarry Smith   ierr = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB); CHKERRQ(ierr);
1249154123eaSLois Curfman McInnes   nztot = nzA + nzB;
1250154123eaSLois Curfman McInnes 
125170f0671dSBarry Smith   cmap  = mat->garray;
1252154123eaSLois Curfman McInnes   if (v  || idx) {
1253154123eaSLois Curfman McInnes     if (nztot) {
1254154123eaSLois Curfman McInnes       /* Sort by increasing column numbers, assuming A and B already sorted */
125570f0671dSBarry Smith       int imark = -1;
1256154123eaSLois Curfman McInnes       if (v) {
125770f0671dSBarry Smith         *v = v_p = mat->rowvalues;
125839e00950SLois Curfman McInnes         for ( i=0; i<nzB; i++ ) {
125970f0671dSBarry Smith           if (cmap[cworkB[i]] < cstart)   v_p[i] = vworkB[i];
1260154123eaSLois Curfman McInnes           else break;
1261154123eaSLois Curfman McInnes         }
1262154123eaSLois Curfman McInnes         imark = i;
126370f0671dSBarry Smith         for ( i=0; i<nzA; i++ )     v_p[imark+i] = vworkA[i];
126470f0671dSBarry Smith         for ( i=imark; i<nzB; i++ ) v_p[nzA+i]   = vworkB[i];
1265154123eaSLois Curfman McInnes       }
1266154123eaSLois Curfman McInnes       if (idx) {
126770f0671dSBarry Smith         *idx = idx_p = mat->rowindices;
126870f0671dSBarry Smith         if (imark > -1) {
126970f0671dSBarry Smith           for ( i=0; i<imark; i++ ) {
127070f0671dSBarry Smith             idx_p[i] = cmap[cworkB[i]];
127170f0671dSBarry Smith           }
127270f0671dSBarry Smith         } else {
1273154123eaSLois Curfman McInnes           for ( i=0; i<nzB; i++ ) {
127470f0671dSBarry Smith             if (cmap[cworkB[i]] < cstart)   idx_p[i] = cmap[cworkB[i]];
1275154123eaSLois Curfman McInnes             else break;
1276154123eaSLois Curfman McInnes           }
1277154123eaSLois Curfman McInnes           imark = i;
127870f0671dSBarry Smith         }
127970f0671dSBarry Smith         for ( i=0; i<nzA; i++ )     idx_p[imark+i] = cstart + cworkA[i];
128070f0671dSBarry Smith         for ( i=imark; i<nzB; i++ ) idx_p[nzA+i]   = cmap[cworkB[i]];
128139e00950SLois Curfman McInnes       }
12823f97c4b0SBarry Smith     } else {
12831ca473b0SSatish Balay       if (idx) *idx = 0;
12841ca473b0SSatish Balay       if (v)   *v   = 0;
12851ca473b0SSatish Balay     }
1286154123eaSLois Curfman McInnes   }
128739e00950SLois Curfman McInnes   *nz = nztot;
1288f830108cSBarry Smith   ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA); CHKERRQ(ierr);
1289f830108cSBarry Smith   ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB); CHKERRQ(ierr);
12903a40ed3dSBarry Smith   PetscFunctionReturn(0);
129139e00950SLois Curfman McInnes }
129239e00950SLois Curfman McInnes 
12935615d1e5SSatish Balay #undef __FUNC__
1294d4bb536fSBarry Smith #define __FUNC__ "MatRestoreRow_MPIAIJ"
12956d84be18SBarry Smith int MatRestoreRow_MPIAIJ(Mat mat,int row,int *nz,int **idx,Scalar **v)
129639e00950SLois Curfman McInnes {
12977a0afa10SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data;
12983a40ed3dSBarry Smith 
12993a40ed3dSBarry Smith   PetscFunctionBegin;
13007a0afa10SBarry Smith   if (aij->getrowactive == PETSC_FALSE) {
1301a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"MatGetRow not called");
13027a0afa10SBarry Smith   }
13037a0afa10SBarry Smith   aij->getrowactive = PETSC_FALSE;
13043a40ed3dSBarry Smith   PetscFunctionReturn(0);
130539e00950SLois Curfman McInnes }
130639e00950SLois Curfman McInnes 
13075615d1e5SSatish Balay #undef __FUNC__
13085615d1e5SSatish Balay #define __FUNC__ "MatNorm_MPIAIJ"
13098f6be9afSLois Curfman McInnes int MatNorm_MPIAIJ(Mat mat,NormType type,double *norm)
1310855ac2c5SLois Curfman McInnes {
1311855ac2c5SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data;
1312ec8511deSBarry Smith   Mat_SeqAIJ *amat = (Mat_SeqAIJ*) aij->A->data, *bmat = (Mat_SeqAIJ*) aij->B->data;
1313416022c9SBarry Smith   int        ierr, i, j, cstart = aij->cstart,shift = amat->indexshift;
1314416022c9SBarry Smith   double     sum = 0.0;
131504ca555eSLois Curfman McInnes   Scalar     *v;
131604ca555eSLois Curfman McInnes 
13173a40ed3dSBarry Smith   PetscFunctionBegin;
131817699dbbSLois Curfman McInnes   if (aij->size == 1) {
131914183eadSLois Curfman McInnes     ierr =  MatNorm(aij->A,type,norm); CHKERRQ(ierr);
132037fa93a5SLois Curfman McInnes   } else {
132104ca555eSLois Curfman McInnes     if (type == NORM_FROBENIUS) {
132204ca555eSLois Curfman McInnes       v = amat->a;
132304ca555eSLois Curfman McInnes       for (i=0; i<amat->nz; i++ ) {
13243a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX)
1325e20fef11SSatish Balay         sum += PetscReal(PetscConj(*v)*(*v)); v++;
132604ca555eSLois Curfman McInnes #else
132704ca555eSLois Curfman McInnes         sum += (*v)*(*v); v++;
132804ca555eSLois Curfman McInnes #endif
132904ca555eSLois Curfman McInnes       }
133004ca555eSLois Curfman McInnes       v = bmat->a;
133104ca555eSLois Curfman McInnes       for (i=0; i<bmat->nz; i++ ) {
13323a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX)
1333e20fef11SSatish Balay         sum += PetscReal(PetscConj(*v)*(*v)); v++;
133404ca555eSLois Curfman McInnes #else
133504ca555eSLois Curfman McInnes         sum += (*v)*(*v); v++;
133604ca555eSLois Curfman McInnes #endif
133704ca555eSLois Curfman McInnes       }
1338ca161407SBarry Smith       ierr = MPI_Allreduce(&sum,norm,1,MPI_DOUBLE,MPI_SUM,mat->comm);CHKERRQ(ierr);
133904ca555eSLois Curfman McInnes       *norm = sqrt(*norm);
13403a40ed3dSBarry Smith     } else if (type == NORM_1) { /* max column norm */
134104ca555eSLois Curfman McInnes       double *tmp, *tmp2;
134204ca555eSLois Curfman McInnes       int    *jj, *garray = aij->garray;
1343758f045eSSatish Balay       tmp  = (double *) PetscMalloc( (aij->N+1)*sizeof(double) ); CHKPTRQ(tmp);
1344758f045eSSatish Balay       tmp2 = (double *) PetscMalloc( (aij->N+1)*sizeof(double) ); CHKPTRQ(tmp2);
1345cddf8d76SBarry Smith       PetscMemzero(tmp,aij->N*sizeof(double));
134604ca555eSLois Curfman McInnes       *norm = 0.0;
134704ca555eSLois Curfman McInnes       v = amat->a; jj = amat->j;
134804ca555eSLois Curfman McInnes       for ( j=0; j<amat->nz; j++ ) {
1349579c6b6fSBarry Smith         tmp[cstart + *jj++ + shift] += PetscAbsScalar(*v);  v++;
135004ca555eSLois Curfman McInnes       }
135104ca555eSLois Curfman McInnes       v = bmat->a; jj = bmat->j;
135204ca555eSLois Curfman McInnes       for ( j=0; j<bmat->nz; j++ ) {
1353579c6b6fSBarry Smith         tmp[garray[*jj++ + shift]] += PetscAbsScalar(*v); v++;
135404ca555eSLois Curfman McInnes       }
1355ca161407SBarry Smith       ierr = MPI_Allreduce(tmp,tmp2,aij->N,MPI_DOUBLE,MPI_SUM,mat->comm);CHKERRQ(ierr);
135604ca555eSLois Curfman McInnes       for ( j=0; j<aij->N; j++ ) {
135704ca555eSLois Curfman McInnes         if (tmp2[j] > *norm) *norm = tmp2[j];
135804ca555eSLois Curfman McInnes       }
13590452661fSBarry Smith       PetscFree(tmp); PetscFree(tmp2);
13603a40ed3dSBarry Smith     } else if (type == NORM_INFINITY) { /* max row norm */
1361515d9167SLois Curfman McInnes       double ntemp = 0.0;
136204ca555eSLois Curfman McInnes       for ( j=0; j<amat->m; j++ ) {
1363dbb450caSBarry Smith         v = amat->a + amat->i[j] + shift;
136404ca555eSLois Curfman McInnes         sum = 0.0;
136504ca555eSLois Curfman McInnes         for ( i=0; i<amat->i[j+1]-amat->i[j]; i++ ) {
1366cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
136704ca555eSLois Curfman McInnes         }
1368dbb450caSBarry Smith         v = bmat->a + bmat->i[j] + shift;
136904ca555eSLois Curfman McInnes         for ( i=0; i<bmat->i[j+1]-bmat->i[j]; i++ ) {
1370cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
137104ca555eSLois Curfman McInnes         }
1372515d9167SLois Curfman McInnes         if (sum > ntemp) ntemp = sum;
137304ca555eSLois Curfman McInnes       }
1374ca161407SBarry Smith       ierr = MPI_Allreduce(&ntemp,norm,1,MPI_DOUBLE,MPI_MAX,mat->comm);CHKERRQ(ierr);
1375ca161407SBarry Smith     } else {
1376a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_SUP,0,"No support for two norm");
137704ca555eSLois Curfman McInnes     }
137837fa93a5SLois Curfman McInnes   }
13793a40ed3dSBarry Smith   PetscFunctionReturn(0);
1380855ac2c5SLois Curfman McInnes }
1381855ac2c5SLois Curfman McInnes 
13825615d1e5SSatish Balay #undef __FUNC__
13835615d1e5SSatish Balay #define __FUNC__ "MatTranspose_MPIAIJ"
13848f6be9afSLois Curfman McInnes int MatTranspose_MPIAIJ(Mat A,Mat *matout)
1385b7c46309SBarry Smith {
1386b7c46309SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data;
1387dbb450caSBarry Smith   Mat_SeqAIJ *Aloc = (Mat_SeqAIJ *) a->A->data;
1388416022c9SBarry Smith   int        ierr,shift = Aloc->indexshift;
1389b7c46309SBarry Smith   int        M = a->M, N = a->N,m,*ai,*aj,row,*cols,i,*ct;
13903a40ed3dSBarry Smith   Mat        B;
1391b7c46309SBarry Smith   Scalar     *array;
1392b7c46309SBarry Smith 
13933a40ed3dSBarry Smith   PetscFunctionBegin;
1394d4bb536fSBarry Smith   if (matout == PETSC_NULL && M != N) {
1395a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_SIZ,0,"Square matrix only for in-place");
1396d4bb536fSBarry Smith   }
1397d4bb536fSBarry Smith 
1398d4bb536fSBarry Smith   ierr = MatCreateMPIAIJ(A->comm,a->n,a->m,N,M,0,PETSC_NULL,0,PETSC_NULL,&B);CHKERRQ(ierr);
1399b7c46309SBarry Smith 
1400b7c46309SBarry Smith   /* copy over the A part */
1401ec8511deSBarry Smith   Aloc = (Mat_SeqAIJ*) a->A->data;
1402b7c46309SBarry Smith   m = Aloc->m; ai = Aloc->i; aj = Aloc->j; array = Aloc->a;
1403b7c46309SBarry Smith   row = a->rstart;
1404dbb450caSBarry Smith   for ( i=0; i<ai[m]+shift; i++ ) {aj[i] += a->cstart + shift;}
1405b7c46309SBarry Smith   for ( i=0; i<m; i++ ) {
1406416022c9SBarry Smith     ierr = MatSetValues(B,ai[i+1]-ai[i],aj,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
1407b7c46309SBarry Smith     row++; array += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
1408b7c46309SBarry Smith   }
1409b7c46309SBarry Smith   aj = Aloc->j;
14104af08d9eSBarry Smith   for ( i=0; i<ai[m]+shift; i++ ) {aj[i] -= a->cstart + shift;}
1411b7c46309SBarry Smith 
1412b7c46309SBarry Smith   /* copy over the B part */
1413ec8511deSBarry Smith   Aloc = (Mat_SeqAIJ*) a->B->data;
1414b7c46309SBarry Smith   m = Aloc->m;  ai = Aloc->i; aj = Aloc->j; array = Aloc->a;
1415b7c46309SBarry Smith   row = a->rstart;
14160452661fSBarry Smith   ct = cols = (int *) PetscMalloc( (1+ai[m]-shift)*sizeof(int) ); CHKPTRQ(cols);
1417dbb450caSBarry Smith   for ( i=0; i<ai[m]+shift; i++ ) {cols[i] = a->garray[aj[i]+shift];}
1418b7c46309SBarry Smith   for ( i=0; i<m; i++ ) {
1419416022c9SBarry Smith     ierr = MatSetValues(B,ai[i+1]-ai[i],cols,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
1420b7c46309SBarry Smith     row++; array += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
1421b7c46309SBarry Smith   }
14220452661fSBarry Smith   PetscFree(ct);
14236d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
14246d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
14253638b69dSLois Curfman McInnes   if (matout != PETSC_NULL) {
14260de55854SLois Curfman McInnes     *matout = B;
14270de55854SLois Curfman McInnes   } else {
1428f830108cSBarry Smith     PetscOps       *Abops;
1429f830108cSBarry Smith     struct _MatOps *Aops;
1430f830108cSBarry Smith 
14310de55854SLois Curfman McInnes     /* This isn't really an in-place transpose .... but free data structures from a */
14320452661fSBarry Smith     PetscFree(a->rowners);
14330de55854SLois Curfman McInnes     ierr = MatDestroy(a->A); CHKERRQ(ierr);
14340de55854SLois Curfman McInnes     ierr = MatDestroy(a->B); CHKERRQ(ierr);
1435b1fc9764SSatish Balay #if defined (USE_CTABLE)
1436b1fc9764SSatish Balay     if (a->colmap) TableDelete(a->colmap);
1437b1fc9764SSatish Balay #else
14380452661fSBarry Smith     if (a->colmap) PetscFree(a->colmap);
1439b1fc9764SSatish Balay #endif
14400452661fSBarry Smith     if (a->garray) PetscFree(a->garray);
14410de55854SLois Curfman McInnes     if (a->lvec) VecDestroy(a->lvec);
1442a56f8943SBarry Smith     if (a->Mvctx) VecScatterDestroy(a->Mvctx);
14430452661fSBarry Smith     PetscFree(a);
1444f830108cSBarry Smith 
1445f830108cSBarry Smith     /*
1446f830108cSBarry Smith        This is horrible, horrible code. We need to keep the
1447f830108cSBarry Smith       A pointers for the bops and ops but copy everything
1448f830108cSBarry Smith       else from C.
1449f830108cSBarry Smith     */
1450f830108cSBarry Smith     Abops = A->bops;
1451f830108cSBarry Smith     Aops  = A->ops;
1452f09e8eb9SSatish Balay     PetscMemcpy(A,B,sizeof(struct _p_Mat));
1453f830108cSBarry Smith     A->bops = Abops;
1454f830108cSBarry Smith     A->ops  = Aops;
14550452661fSBarry Smith     PetscHeaderDestroy(B);
14560de55854SLois Curfman McInnes   }
14573a40ed3dSBarry Smith   PetscFunctionReturn(0);
1458b7c46309SBarry Smith }
1459b7c46309SBarry Smith 
14605615d1e5SSatish Balay #undef __FUNC__
14615615d1e5SSatish Balay #define __FUNC__ "MatDiagonalScale_MPIAIJ"
14624b967eb1SSatish Balay int MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr)
1463a008b906SSatish Balay {
14644b967eb1SSatish Balay   Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data;
14654b967eb1SSatish Balay   Mat a = aij->A, b = aij->B;
1466a008b906SSatish Balay   int ierr,s1,s2,s3;
1467a008b906SSatish Balay 
14683a40ed3dSBarry Smith   PetscFunctionBegin;
14694b967eb1SSatish Balay   ierr = MatGetLocalSize(mat,&s2,&s3); CHKERRQ(ierr);
14704b967eb1SSatish Balay   if (rr) {
1471e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr);
1472a8c6a408SBarry Smith     if (s1!=s3) SETERRQ(PETSC_ERR_ARG_SIZ,0,"right vector non-conforming local size");
14734b967eb1SSatish Balay     /* Overlap communication with computation. */
147443a90d84SBarry Smith     ierr = VecScatterBegin(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx); CHKERRQ(ierr);
1475a008b906SSatish Balay   }
14764b967eb1SSatish Balay   if (ll) {
1477e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr);
1478a8c6a408SBarry Smith     if (s1!=s2) SETERRQ(PETSC_ERR_ARG_SIZ,0,"left vector non-conforming local size");
1479f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,ll,0); CHKERRQ(ierr);
14804b967eb1SSatish Balay   }
14814b967eb1SSatish Balay   /* scale  the diagonal block */
1482f830108cSBarry Smith   ierr = (*a->ops->diagonalscale)(a,ll,rr); CHKERRQ(ierr);
14834b967eb1SSatish Balay 
14844b967eb1SSatish Balay   if (rr) {
14854b967eb1SSatish Balay     /* Do a scatter end and then right scale the off-diagonal block */
148643a90d84SBarry Smith     ierr = VecScatterEnd(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx); CHKERRQ(ierr);
1487f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,0,aij->lvec); CHKERRQ(ierr);
14884b967eb1SSatish Balay   }
14894b967eb1SSatish Balay 
14903a40ed3dSBarry Smith   PetscFunctionReturn(0);
1491a008b906SSatish Balay }
1492a008b906SSatish Balay 
1493a008b906SSatish Balay 
1494682d7d0cSBarry Smith extern int MatPrintHelp_SeqAIJ(Mat);
14955615d1e5SSatish Balay #undef __FUNC__
1496d4bb536fSBarry Smith #define __FUNC__ "MatPrintHelp_MPIAIJ"
14978f6be9afSLois Curfman McInnes int MatPrintHelp_MPIAIJ(Mat A)
1498682d7d0cSBarry Smith {
1499682d7d0cSBarry Smith   Mat_MPIAIJ *a   = (Mat_MPIAIJ*) A->data;
15003a40ed3dSBarry Smith   int        ierr;
1501682d7d0cSBarry Smith 
15023a40ed3dSBarry Smith   PetscFunctionBegin;
15033a40ed3dSBarry Smith   if (!a->rank) {
15043a40ed3dSBarry Smith     ierr = MatPrintHelp_SeqAIJ(a->A);CHKERRQ(ierr);
15053a40ed3dSBarry Smith   }
15063a40ed3dSBarry Smith   PetscFunctionReturn(0);
1507682d7d0cSBarry Smith }
1508682d7d0cSBarry Smith 
15095615d1e5SSatish Balay #undef __FUNC__
1510d4bb536fSBarry Smith #define __FUNC__ "MatGetBlockSize_MPIAIJ"
15118f6be9afSLois Curfman McInnes int MatGetBlockSize_MPIAIJ(Mat A,int *bs)
15125a838052SSatish Balay {
15133a40ed3dSBarry Smith   PetscFunctionBegin;
15145a838052SSatish Balay   *bs = 1;
15153a40ed3dSBarry Smith   PetscFunctionReturn(0);
15165a838052SSatish Balay }
15175615d1e5SSatish Balay #undef __FUNC__
1518d4bb536fSBarry Smith #define __FUNC__ "MatSetUnfactored_MPIAIJ"
15198f6be9afSLois Curfman McInnes int MatSetUnfactored_MPIAIJ(Mat A)
1520bb5a7306SBarry Smith {
1521bb5a7306SBarry Smith   Mat_MPIAIJ *a   = (Mat_MPIAIJ*) A->data;
1522bb5a7306SBarry Smith   int        ierr;
15233a40ed3dSBarry Smith 
15243a40ed3dSBarry Smith   PetscFunctionBegin;
1525bb5a7306SBarry Smith   ierr = MatSetUnfactored(a->A); CHKERRQ(ierr);
15263a40ed3dSBarry Smith   PetscFunctionReturn(0);
1527bb5a7306SBarry Smith }
1528bb5a7306SBarry Smith 
1529d4bb536fSBarry Smith #undef __FUNC__
1530d4bb536fSBarry Smith #define __FUNC__ "MatEqual_MPIAIJ"
1531d4bb536fSBarry Smith int MatEqual_MPIAIJ(Mat A, Mat B, PetscTruth *flag)
1532d4bb536fSBarry Smith {
1533d4bb536fSBarry Smith   Mat_MPIAIJ *matB = (Mat_MPIAIJ *) B->data,*matA = (Mat_MPIAIJ *) A->data;
1534d4bb536fSBarry Smith   Mat        a, b, c, d;
1535d4bb536fSBarry Smith   PetscTruth flg;
1536d4bb536fSBarry Smith   int        ierr;
1537d4bb536fSBarry Smith 
15383a40ed3dSBarry Smith   PetscFunctionBegin;
1539a8c6a408SBarry Smith   if (B->type != MATMPIAIJ) SETERRQ(PETSC_ERR_ARG_INCOMP,0,"Matrices must be same type");
1540d4bb536fSBarry Smith   a = matA->A; b = matA->B;
1541d4bb536fSBarry Smith   c = matB->A; d = matB->B;
1542d4bb536fSBarry Smith 
1543d4bb536fSBarry Smith   ierr = MatEqual(a, c, &flg); CHKERRQ(ierr);
1544d4bb536fSBarry Smith   if (flg == PETSC_TRUE) {
1545d4bb536fSBarry Smith     ierr = MatEqual(b, d, &flg); CHKERRQ(ierr);
1546d4bb536fSBarry Smith   }
1547ca161407SBarry Smith   ierr = MPI_Allreduce(&flg, flag, 1, MPI_INT, MPI_LAND, A->comm);CHKERRQ(ierr);
15483a40ed3dSBarry Smith   PetscFunctionReturn(0);
1549d4bb536fSBarry Smith }
1550d4bb536fSBarry Smith 
1551cb5b572fSBarry Smith #undef __FUNC__
1552cb5b572fSBarry Smith #define __FUNC__ "MatCopy_MPIAIJ"
1553cb5b572fSBarry Smith int MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str)
1554cb5b572fSBarry Smith {
1555cb5b572fSBarry Smith   int        ierr;
1556cb5b572fSBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;
1557cb5b572fSBarry Smith   Mat_MPIAIJ *b = (Mat_MPIAIJ *)B->data;
1558cb5b572fSBarry Smith 
1559cb5b572fSBarry Smith   PetscFunctionBegin;
1560cb5b572fSBarry Smith   if (str != SAME_NONZERO_PATTERN || B->type != MATMPIAIJ) {
1561cb5b572fSBarry Smith     /* because of the column compression in the off-processor part of the matrix a->B,
1562cb5b572fSBarry Smith        the number of columns in a->B and b->B may be different, hence we cannot call
1563cb5b572fSBarry Smith        the MatCopy() directly on the two parts. If need be, we can provide a more
1564cb5b572fSBarry Smith        efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices
1565cb5b572fSBarry Smith        then copying the submatrices */
1566cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1567cb5b572fSBarry Smith   } else {
1568cb5b572fSBarry Smith     ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr);
1569cb5b572fSBarry Smith     ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr);
1570cb5b572fSBarry Smith   }
1571cb5b572fSBarry Smith   PetscFunctionReturn(0);
1572cb5b572fSBarry Smith }
1573cb5b572fSBarry Smith 
15742e8a6d31SBarry Smith extern int MatDuplicate_MPIAIJ(Mat,MatDuplicateOption,Mat *);
15752f86bd48SSatish Balay extern int MatIncreaseOverlap_MPIAIJ(Mat , int, IS *, int);
15760a198c4cSBarry Smith extern int MatFDColoringCreate_MPIAIJ(Mat,ISColoring,MatFDColoring);
15777b2a1423SBarry Smith extern int MatGetSubMatrices_MPIAIJ (Mat ,int , IS *,IS *,MatReuse,Mat **);
15787b2a1423SBarry Smith extern int MatGetSubMatrix_MPIAIJ (Mat ,IS,IS,int,MatReuse,Mat *);
157900e6dbe6SBarry Smith 
15808a729477SBarry Smith /* -------------------------------------------------------------------*/
1581cda55fadSBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ,
1582cda55fadSBarry Smith        MatGetRow_MPIAIJ,
1583cda55fadSBarry Smith        MatRestoreRow_MPIAIJ,
1584cda55fadSBarry Smith        MatMult_MPIAIJ,
1585cda55fadSBarry Smith        MatMultAdd_MPIAIJ,
1586cda55fadSBarry Smith        MatMultTrans_MPIAIJ,
1587cda55fadSBarry Smith        MatMultTransAdd_MPIAIJ,
1588cda55fadSBarry Smith        0,
1589cda55fadSBarry Smith        0,
1590cda55fadSBarry Smith        0,
1591cda55fadSBarry Smith        0,
1592cda55fadSBarry Smith        0,
1593cda55fadSBarry Smith        0,
159444a69424SLois Curfman McInnes        MatRelax_MPIAIJ,
1595b7c46309SBarry Smith        MatTranspose_MPIAIJ,
1596cda55fadSBarry Smith        MatGetInfo_MPIAIJ,
1597cda55fadSBarry Smith        MatEqual_MPIAIJ,
1598cda55fadSBarry Smith        MatGetDiagonal_MPIAIJ,
1599cda55fadSBarry Smith        MatDiagonalScale_MPIAIJ,
1600cda55fadSBarry Smith        MatNorm_MPIAIJ,
1601cda55fadSBarry Smith        MatAssemblyBegin_MPIAIJ,
1602cda55fadSBarry Smith        MatAssemblyEnd_MPIAIJ,
16031eb62cbbSBarry Smith        0,
1604cda55fadSBarry Smith        MatSetOption_MPIAIJ,
1605cda55fadSBarry Smith        MatZeroEntries_MPIAIJ,
1606cda55fadSBarry Smith        MatZeroRows_MPIAIJ,
1607cda55fadSBarry Smith        0,
1608cda55fadSBarry Smith        0,
1609cda55fadSBarry Smith        0,
1610cda55fadSBarry Smith        0,
1611cda55fadSBarry Smith        MatGetSize_MPIAIJ,
1612cda55fadSBarry Smith        MatGetLocalSize_MPIAIJ,
1613cda55fadSBarry Smith        MatGetOwnershipRange_MPIAIJ,
1614cda55fadSBarry Smith        0,
1615cda55fadSBarry Smith        0,
1616cda55fadSBarry Smith        0,
1617cda55fadSBarry Smith        0,
16182e8a6d31SBarry Smith        MatDuplicate_MPIAIJ,
1619cda55fadSBarry Smith        0,
1620cda55fadSBarry Smith        0,
1621cda55fadSBarry Smith        0,
1622cda55fadSBarry Smith        0,
1623cda55fadSBarry Smith        0,
1624cda55fadSBarry Smith        MatGetSubMatrices_MPIAIJ,
1625cda55fadSBarry Smith        MatIncreaseOverlap_MPIAIJ,
1626cda55fadSBarry Smith        MatGetValues_MPIAIJ,
1627cb5b572fSBarry Smith        MatCopy_MPIAIJ,
1628052efed2SBarry Smith        MatPrintHelp_MPIAIJ,
1629cda55fadSBarry Smith        MatScale_MPIAIJ,
1630cda55fadSBarry Smith        0,
1631cda55fadSBarry Smith        0,
1632cda55fadSBarry Smith        0,
1633cda55fadSBarry Smith        MatGetBlockSize_MPIAIJ,
1634cda55fadSBarry Smith        0,
1635cda55fadSBarry Smith        0,
1636cda55fadSBarry Smith        0,
1637cda55fadSBarry Smith        0,
1638cda55fadSBarry Smith        MatFDColoringCreate_MPIAIJ,
1639cda55fadSBarry Smith        0,
1640cda55fadSBarry Smith        MatSetUnfactored_MPIAIJ,
1641cda55fadSBarry Smith        0,
1642cda55fadSBarry Smith        0,
1643cda55fadSBarry Smith        MatGetSubMatrix_MPIAIJ,
1644cda55fadSBarry Smith        0,
1645cda55fadSBarry Smith        0,
1646cda55fadSBarry Smith        MatGetMaps_Petsc};
164736ce4990SBarry Smith 
16482e8a6d31SBarry Smith /* ----------------------------------------------------------------------------------------*/
16492e8a6d31SBarry Smith 
1650fb2e594dSBarry Smith EXTERN_C_BEGIN
16512e8a6d31SBarry Smith #undef __FUNC__
16522e8a6d31SBarry Smith #define __FUNC__ "MatStoreValues_MPIAIJ"
16532e8a6d31SBarry Smith int MatStoreValues_MPIAIJ(Mat mat)
16542e8a6d31SBarry Smith {
16552e8a6d31SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data;
16562e8a6d31SBarry Smith   int        ierr;
16572e8a6d31SBarry Smith 
16582e8a6d31SBarry Smith   PetscFunctionBegin;
16592e8a6d31SBarry Smith   ierr = MatStoreValues(aij->A);CHKERRQ(ierr);
16602e8a6d31SBarry Smith   ierr = MatStoreValues(aij->B);CHKERRQ(ierr);
16612e8a6d31SBarry Smith   PetscFunctionReturn(0);
16622e8a6d31SBarry Smith }
1663fb2e594dSBarry Smith EXTERN_C_END
16642e8a6d31SBarry Smith 
1665fb2e594dSBarry Smith EXTERN_C_BEGIN
16662e8a6d31SBarry Smith #undef __FUNC__
16672e8a6d31SBarry Smith #define __FUNC__ "MatRetrieveValues_MPIAIJ"
16682e8a6d31SBarry Smith int MatRetrieveValues_MPIAIJ(Mat mat)
16692e8a6d31SBarry Smith {
16702e8a6d31SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data;
16712e8a6d31SBarry Smith   int        ierr;
16722e8a6d31SBarry Smith 
16732e8a6d31SBarry Smith   PetscFunctionBegin;
16742e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr);
16752e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr);
16762e8a6d31SBarry Smith   PetscFunctionReturn(0);
16772e8a6d31SBarry Smith }
1678fb2e594dSBarry Smith EXTERN_C_END
16798a729477SBarry Smith 
168027508adbSBarry Smith #include "pc.h"
168127508adbSBarry Smith EXTERN_C_BEGIN
16825ef9f2a5SBarry Smith extern int MatGetDiagonalBlock_MPIAIJ(Mat,PetscTruth *,MatReuse,Mat *);
168327508adbSBarry Smith EXTERN_C_END
168427508adbSBarry Smith 
16855615d1e5SSatish Balay #undef __FUNC__
16865615d1e5SSatish Balay #define __FUNC__ "MatCreateMPIAIJ"
16871987afe7SBarry Smith /*@C
1688ff756334SLois Curfman McInnes    MatCreateMPIAIJ - Creates a sparse parallel matrix in AIJ format
16893a511b96SLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
16903a511b96SLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameters
16913a511b96SLois Curfman McInnes    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
16923a511b96SLois Curfman McInnes    performance can be increased by more than a factor of 50.
16938a729477SBarry Smith 
1694db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
1695db81eaa0SLois Curfman McInnes 
16968a729477SBarry Smith    Input Parameters:
1697db81eaa0SLois Curfman McInnes +  comm - MPI communicator
16987d3e4905SLois Curfman McInnes .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
169992e8d321SLois Curfman McInnes            This value should be the same as the local size used in creating the
170092e8d321SLois Curfman McInnes            y vector for the matrix-vector product y = Ax.
17011a3896d6SBarry Smith .  n - This value should be the same as the local size used in creating the
17021a3896d6SBarry Smith        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
17031a3896d6SBarry Smith        calculated if N is given) For square matrices n is almost always m.
170460d380a7SBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
170560d380a7SBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
1706af1d9917SSatish Balay .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
1707af1d9917SSatish Balay            (same value is used for all local rows)
1708af1d9917SSatish Balay .  d_nnz - array containing the number of nonzeros in the various rows of the
1709af1d9917SSatish Balay            DIAGONAL portion of the local submatrix (possibly different for each row)
1710af1d9917SSatish Balay            or PETSC_NULL, if d_nz is used to specify the nonzero structure.
1711af1d9917SSatish Balay            The size of this array is equal to the number of local rows, i.e 'm'.
1712af1d9917SSatish Balay            You must leave room for the diagonal entry even if it is zero.
1713af1d9917SSatish Balay .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
1714af1d9917SSatish Balay            submatrix (same value is used for all local rows).
1715af1d9917SSatish Balay -  o_nnz - array containing the number of nonzeros in the various rows of the
1716af1d9917SSatish Balay            OFF-DIAGONAL portion of the local submatrix (possibly different for
1717af1d9917SSatish Balay            each row) or PETSC_NULL, if o_nz is used to specify the nonzero
1718af1d9917SSatish Balay            structure. The size of this array is equal to the number
1719af1d9917SSatish Balay            of local rows, i.e 'm'.
17208a729477SBarry Smith 
1721ff756334SLois Curfman McInnes    Output Parameter:
172244cd7ae7SLois Curfman McInnes .  A - the matrix
17238a729477SBarry Smith 
1724b259b22eSLois Curfman McInnes    Notes:
1725af1d9917SSatish Balay    m,n,M,N parameters specify the size of the matrix, and its partitioning across
1726af1d9917SSatish Balay    processors, while d_nz,d_nnz,o_nz,o_nnz parameters specify the approximate
1727af1d9917SSatish Balay    storage requirements for this matrix.
1728af1d9917SSatish Balay 
1729af1d9917SSatish Balay    If PETSC_DECIDE or  PETSC_DETERMINE is used for a particular argument on one
1730af1d9917SSatish Balay    processor than it must be used on all processors that share the object for
1731af1d9917SSatish Balay    that argument.
1732be79a94dSBarry Smith 
1733ff756334SLois Curfman McInnes    The AIJ format (also called the Yale sparse matrix format or
1734ff756334SLois Curfman McInnes    compressed row storage), is fully compatible with standard Fortran 77
17350002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
17360002213bSLois Curfman McInnes    either one (as in Fortran) or zero.  See the users manual for details.
1737ff756334SLois Curfman McInnes 
1738ff756334SLois Curfman McInnes    The user MUST specify either the local or global matrix dimensions
1739ff756334SLois Curfman McInnes    (possibly both).
1740ff756334SLois Curfman McInnes 
1741af1d9917SSatish Balay    The parallel matrix is partitioned such that the first m0 rows belong to
1742af1d9917SSatish Balay    process 0, the next m1 rows belong to process 1, the next m2 rows belong
1743af1d9917SSatish Balay    to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.
1744af1d9917SSatish Balay 
1745af1d9917SSatish Balay    The DIAGONAL portion of the local submatrix of a processor can be defined
1746af1d9917SSatish Balay    as the submatrix which is obtained by extraction the part corresponding
1747af1d9917SSatish Balay    to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the
1748af1d9917SSatish Balay    first row that belongs to the processor, and r2 is the last row belonging
1749af1d9917SSatish Balay    to the this processor. This is a square mxm matrix. The remaining portion
1750af1d9917SSatish Balay    of the local submatrix (mxN) constitute the OFF-DIAGONAL portion.
1751af1d9917SSatish Balay 
1752af1d9917SSatish Balay    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
1753af1d9917SSatish Balay 
17545511cfe3SLois Curfman McInnes    By default, this format uses inodes (identical nodes) when possible.
17555511cfe3SLois Curfman McInnes    We search for consecutive rows with the same nonzero structure, thereby
17565511cfe3SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
17575511cfe3SLois Curfman McInnes 
17585511cfe3SLois Curfman McInnes    Options Database Keys:
1759db81eaa0SLois Curfman McInnes +  -mat_aij_no_inode  - Do not use inodes
1760db81eaa0SLois Curfman McInnes .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
1761db81eaa0SLois Curfman McInnes -  -mat_aij_oneindex - Internally use indexing starting at 1
1762db81eaa0SLois Curfman McInnes         rather than 0.  Note that when calling MatSetValues(),
1763db81eaa0SLois Curfman McInnes         the user still MUST index entries starting at 0!
1764494eafd4SBarry Smith .   -mat_mpi - use the parallel matrix data structures even on one processor
1765494eafd4SBarry Smith                (defaults to using SeqBAIJ format on one processor)
1766494eafd4SBarry Smith .   -mat_mpi - use the parallel matrix data structures even on one processor
1767494eafd4SBarry Smith                (defaults to using SeqAIJ format on one processor)
1768494eafd4SBarry Smith 
17695511cfe3SLois Curfman McInnes 
1770af1d9917SSatish Balay    Example usage:
1771e0245417SLois Curfman McInnes 
1772af1d9917SSatish Balay    Consider the following 8x8 matrix with 34 non-zero values, that is
1773af1d9917SSatish Balay    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
1774af1d9917SSatish Balay    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
1775af1d9917SSatish Balay    as follows:
17762191d07cSBarry Smith 
1777db81eaa0SLois Curfman McInnes .vb
1778af1d9917SSatish Balay             1  2  0  |  0  3  0  |  0  4
1779af1d9917SSatish Balay     Proc0   0  5  6  |  7  0  0  |  8  0
1780af1d9917SSatish Balay             9  0 10  | 11  0  0  | 12  0
1781af1d9917SSatish Balay     -------------------------------------
1782af1d9917SSatish Balay            13  0 14  | 15 16 17  |  0  0
1783af1d9917SSatish Balay     Proc1   0 18  0  | 19 20 21  |  0  0
1784af1d9917SSatish Balay             0  0  0  | 22 23  0  | 24  0
1785af1d9917SSatish Balay     -------------------------------------
1786af1d9917SSatish Balay     Proc2  25 26 27  |  0  0 28  | 29  0
1787af1d9917SSatish Balay            30  0  0  | 31 32 33  |  0 34
1788db81eaa0SLois Curfman McInnes .ve
1789b810aeb4SBarry Smith 
1790af1d9917SSatish Balay    This can be represented as a collection of submatrices as:
17915511cfe3SLois Curfman McInnes 
1792af1d9917SSatish Balay .vb
1793af1d9917SSatish Balay       A B C
1794af1d9917SSatish Balay       D E F
1795af1d9917SSatish Balay       G H I
1796af1d9917SSatish Balay .ve
1797af1d9917SSatish Balay 
1798af1d9917SSatish Balay    Where the submatrices A,B,C are owned by proc0, D,E,F are
1799af1d9917SSatish Balay    owned by proc1, G,H,I are owned by proc2.
1800af1d9917SSatish Balay 
1801af1d9917SSatish Balay    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
1802af1d9917SSatish Balay    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
1803af1d9917SSatish Balay    The 'M','N' parameters are 8,8, and have the same values on all procs.
1804af1d9917SSatish Balay 
1805af1d9917SSatish Balay    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
1806af1d9917SSatish Balay    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
1807af1d9917SSatish Balay    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
1808af1d9917SSatish Balay    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
1809af1d9917SSatish Balay    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
1810af1d9917SSatish Balay    matrix, ans [DF] as another SeqAIJ matrix.
1811af1d9917SSatish Balay 
1812af1d9917SSatish Balay    When d_nz, o_nz parameters are specified, d_nz storage elements are
1813af1d9917SSatish Balay    allocated for every row of the local diagonal submatrix, and o_nz
1814af1d9917SSatish Balay    storage locations are allocated for every row of the OFF-DIAGONAL submat.
1815af1d9917SSatish Balay    One way to choose d_nz and o_nz is to use the max nonzerors per local
1816af1d9917SSatish Balay    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
1817af1d9917SSatish Balay    In this case, the values of d_nz,o_nz are:
1818af1d9917SSatish Balay .vb
1819af1d9917SSatish Balay      proc0 : dnz = 2, o_nz = 2
1820af1d9917SSatish Balay      proc1 : dnz = 3, o_nz = 2
1821af1d9917SSatish Balay      proc2 : dnz = 1, o_nz = 4
1822af1d9917SSatish Balay .ve
1823af1d9917SSatish Balay    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
1824af1d9917SSatish Balay    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
1825af1d9917SSatish Balay    for proc3. i.e we are using 12+15+10=37 storage locations to store
1826af1d9917SSatish Balay    34 values.
1827af1d9917SSatish Balay 
1828af1d9917SSatish Balay    When d_nnz, o_nnz parameters are specified, the storage is specified
1829af1d9917SSatish Balay    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
1830af1d9917SSatish Balay    In the above case the values for d_nnz,o_nnz are:
1831af1d9917SSatish Balay .vb
1832af1d9917SSatish Balay      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
1833af1d9917SSatish Balay      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
1834af1d9917SSatish Balay      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
1835af1d9917SSatish Balay .ve
1836af1d9917SSatish Balay    Here the space allocated is sum of all the above values i.e 34, and
1837af1d9917SSatish Balay    hence pre-allocation is perfect.
18383a511b96SLois Curfman McInnes 
1839027ccd11SLois Curfman McInnes    Level: intermediate
1840027ccd11SLois Curfman McInnes 
1841dbd7a890SLois Curfman McInnes .keywords: matrix, aij, compressed row, sparse, parallel
1842ff756334SLois Curfman McInnes 
1843fafbff53SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues()
18448a729477SBarry Smith @*/
1845e1311b90SBarry Smith int MatCreateMPIAIJ(MPI_Comm comm,int m,int n,int M,int N,int d_nz,int *d_nnz,int o_nz,int *o_nnz,Mat *A)
18468a729477SBarry Smith {
184744cd7ae7SLois Curfman McInnes   Mat          B;
184844cd7ae7SLois Curfman McInnes   Mat_MPIAIJ   *b;
1849eac7125bSBarry Smith   int          ierr, i,size,flag1 = 0, flag2 = 0;
1850416022c9SBarry Smith 
18513a40ed3dSBarry Smith   PetscFunctionBegin;
18523914022bSBarry Smith   MPI_Comm_size(comm,&size);
18537be0e774SBarry Smith   ierr = OptionsHasName(PETSC_NULL,"-mat_mpiaij",&flag1); CHKERRQ(ierr);
18547be0e774SBarry Smith   ierr = OptionsHasName(PETSC_NULL,"-mat_mpi",&flag2); CHKERRQ(ierr);
18557be0e774SBarry Smith   if (!flag1 && !flag2 && size == 1) {
18563914022bSBarry Smith     if (M == PETSC_DECIDE) M = m;
18573914022bSBarry Smith     if (N == PETSC_DECIDE) N = n;
18583914022bSBarry Smith     ierr = MatCreateSeqAIJ(comm,M,N,d_nz,d_nnz,A); CHKERRQ(ierr);
18593a40ed3dSBarry Smith     PetscFunctionReturn(0);
18603914022bSBarry Smith   }
18613914022bSBarry Smith 
186244cd7ae7SLois Curfman McInnes   *A = 0;
18633f1db9ecSBarry Smith   PetscHeaderCreate(B,_p_Mat,struct _MatOps,MAT_COOKIE,MATMPIAIJ,"Mat",comm,MatDestroy,MatView);
186444cd7ae7SLois Curfman McInnes   PLogObjectCreate(B);
186544cd7ae7SLois Curfman McInnes   B->data            = (void *) (b = PetscNew(Mat_MPIAIJ)); CHKPTRQ(b);
186644cd7ae7SLois Curfman McInnes   PetscMemzero(b,sizeof(Mat_MPIAIJ));
1867cda55fadSBarry Smith   PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));
1868e1311b90SBarry Smith   B->ops->destroy    = MatDestroy_MPIAIJ;
1869e1311b90SBarry Smith   B->ops->view       = MatView_MPIAIJ;
187044cd7ae7SLois Curfman McInnes   B->factor          = 0;
187144cd7ae7SLois Curfman McInnes   B->assembled       = PETSC_FALSE;
187290f02eecSBarry Smith   B->mapping         = 0;
1873d6dfbf8fSBarry Smith 
187447794344SBarry Smith   B->insertmode      = NOT_SET_VALUES;
18759eb4d147SSatish Balay   b->size            = size;
187644cd7ae7SLois Curfman McInnes   MPI_Comm_rank(comm,&b->rank);
18771eb62cbbSBarry Smith 
18783a40ed3dSBarry Smith   if (m == PETSC_DECIDE && (d_nnz != PETSC_NULL || o_nnz != PETSC_NULL)) {
1879a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"Cannot have PETSC_DECIDE rows but set d_nnz or o_nnz");
18803a40ed3dSBarry Smith   }
18811987afe7SBarry Smith 
1882eac7125bSBarry Smith   ierr = PetscSplitOwnership(comm,&m,&M);CHKERRQ(ierr);
1883eac7125bSBarry Smith   ierr = PetscSplitOwnership(comm,&n,&N);CHKERRQ(ierr);
188444cd7ae7SLois Curfman McInnes   b->m = m; B->m = m;
188544cd7ae7SLois Curfman McInnes   b->n = n; B->n = n;
188644cd7ae7SLois Curfman McInnes   b->N = N; B->N = N;
188744cd7ae7SLois Curfman McInnes   b->M = M; B->M = M;
18881eb62cbbSBarry Smith 
1889c7fcc2eaSBarry Smith   /* the information in the maps duplicates the information computed below, eventually
1890c7fcc2eaSBarry Smith      we should remove the duplicate information that is not contained in the maps */
1891253a16ddSBarry Smith   ierr = MapCreateMPI(comm,m,M,&B->rmap);CHKERRQ(ierr);
1892253a16ddSBarry Smith   ierr = MapCreateMPI(comm,n,N,&B->cmap);CHKERRQ(ierr);
1893c7fcc2eaSBarry Smith 
18941eb62cbbSBarry Smith   /* build local table of row and column ownerships */
189544cd7ae7SLois Curfman McInnes   b->rowners = (int *) PetscMalloc(2*(b->size+2)*sizeof(int)); CHKPTRQ(b->rowners);
1896f09e8eb9SSatish Balay   PLogObjectMemory(B,2*(b->size+2)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_MPIAIJ));
1897603f58a4SSatish Balay   b->cowners = b->rowners + b->size + 2;
1898ca161407SBarry Smith   ierr = MPI_Allgather(&m,1,MPI_INT,b->rowners+1,1,MPI_INT,comm);CHKERRQ(ierr);
189944cd7ae7SLois Curfman McInnes   b->rowners[0] = 0;
190044cd7ae7SLois Curfman McInnes   for ( i=2; i<=b->size; i++ ) {
190144cd7ae7SLois Curfman McInnes     b->rowners[i] += b->rowners[i-1];
19028a729477SBarry Smith   }
190344cd7ae7SLois Curfman McInnes   b->rstart = b->rowners[b->rank];
190444cd7ae7SLois Curfman McInnes   b->rend   = b->rowners[b->rank+1];
1905ca161407SBarry Smith   ierr = MPI_Allgather(&n,1,MPI_INT,b->cowners+1,1,MPI_INT,comm);CHKERRQ(ierr);
190644cd7ae7SLois Curfman McInnes   b->cowners[0] = 0;
190744cd7ae7SLois Curfman McInnes   for ( i=2; i<=b->size; i++ ) {
190844cd7ae7SLois Curfman McInnes     b->cowners[i] += b->cowners[i-1];
19098a729477SBarry Smith   }
191044cd7ae7SLois Curfman McInnes   b->cstart = b->cowners[b->rank];
191144cd7ae7SLois Curfman McInnes   b->cend   = b->cowners[b->rank+1];
19128a729477SBarry Smith 
19135ace5be8SLois Curfman McInnes   if (d_nz == PETSC_DEFAULT) d_nz = 5;
1914029af93fSBarry Smith   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,m,n,d_nz,d_nnz,&b->A); CHKERRQ(ierr);
191544cd7ae7SLois Curfman McInnes   PLogObjectParent(B,b->A);
19167b8455f0SLois Curfman McInnes   if (o_nz == PETSC_DEFAULT) o_nz = 0;
1917029af93fSBarry Smith   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,m,N,o_nz,o_nnz,&b->B); CHKERRQ(ierr);
191844cd7ae7SLois Curfman McInnes   PLogObjectParent(B,b->B);
19198a729477SBarry Smith 
19201eb62cbbSBarry Smith   /* build cache for off array entries formed */
192144cd7ae7SLois Curfman McInnes   ierr = StashBuild_Private(&b->stash); CHKERRQ(ierr);
192290f02eecSBarry Smith   b->donotstash  = 0;
192344cd7ae7SLois Curfman McInnes   b->colmap      = 0;
192444cd7ae7SLois Curfman McInnes   b->garray      = 0;
192544cd7ae7SLois Curfman McInnes   b->roworiented = 1;
19268a729477SBarry Smith 
19271eb62cbbSBarry Smith   /* stuff used for matrix vector multiply */
192844cd7ae7SLois Curfman McInnes   b->lvec      = 0;
192944cd7ae7SLois Curfman McInnes   b->Mvctx     = 0;
19308a729477SBarry Smith 
19317a0afa10SBarry Smith   /* stuff for MatGetRow() */
193244cd7ae7SLois Curfman McInnes   b->rowindices   = 0;
193344cd7ae7SLois Curfman McInnes   b->rowvalues    = 0;
193444cd7ae7SLois Curfman McInnes   b->getrowactive = PETSC_FALSE;
19357a0afa10SBarry Smith 
19362e8a6d31SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",
19372e8a6d31SBarry Smith                                      "MatStoreValues_MPIAIJ",
19382e8a6d31SBarry Smith                                      (void*)MatStoreValues_MPIAIJ);CHKERRQ(ierr);
19392e8a6d31SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",
19402e8a6d31SBarry Smith                                      "MatRetrieveValues_MPIAIJ",
19412e8a6d31SBarry Smith                                      (void*)MatRetrieveValues_MPIAIJ);CHKERRQ(ierr);
19425ef9f2a5SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetDiagonalBlock_C",
19435ef9f2a5SBarry Smith                                      "MatGetDiagonalBlock_MPIAIJ",
19445ef9f2a5SBarry Smith                                      (void*)MatGetDiagonalBlock_MPIAIJ);CHKERRQ(ierr);
194544cd7ae7SLois Curfman McInnes   *A = B;
19463a40ed3dSBarry Smith   PetscFunctionReturn(0);
1947d6dfbf8fSBarry Smith }
1948c74985f6SBarry Smith 
19495615d1e5SSatish Balay #undef __FUNC__
19502e8a6d31SBarry Smith #define __FUNC__ "MatDuplicate_MPIAIJ"
19512e8a6d31SBarry Smith int MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
1952d6dfbf8fSBarry Smith {
1953d6dfbf8fSBarry Smith   Mat        mat;
1954416022c9SBarry Smith   Mat_MPIAIJ *a,*oldmat = (Mat_MPIAIJ *) matin->data;
1955a1b97e82SLois Curfman McInnes   int        ierr, len=0, flg;
1956d6dfbf8fSBarry Smith 
19573a40ed3dSBarry Smith   PetscFunctionBegin;
1958416022c9SBarry Smith   *newmat       = 0;
19593f1db9ecSBarry Smith   PetscHeaderCreate(mat,_p_Mat,struct _MatOps,MAT_COOKIE,MATMPIAIJ,"Mat",matin->comm,MatDestroy,MatView);
1960a5a9c739SBarry Smith   PLogObjectCreate(mat);
19610452661fSBarry Smith   mat->data       = (void *) (a = PetscNew(Mat_MPIAIJ)); CHKPTRQ(a);
1962cda55fadSBarry Smith   PetscMemcpy(mat->ops,&MatOps_Values,sizeof(struct _MatOps));
1963e1311b90SBarry Smith   mat->ops->destroy = MatDestroy_MPIAIJ;
1964e1311b90SBarry Smith   mat->ops->view    = MatView_MPIAIJ;
1965d6dfbf8fSBarry Smith   mat->factor       = matin->factor;
1966c456f294SBarry Smith   mat->assembled    = PETSC_TRUE;
1967e7641de0SSatish Balay   mat->insertmode   = NOT_SET_VALUES;
1968d6dfbf8fSBarry Smith 
196944cd7ae7SLois Curfman McInnes   a->m = mat->m   = oldmat->m;
197044cd7ae7SLois Curfman McInnes   a->n = mat->n   = oldmat->n;
197144cd7ae7SLois Curfman McInnes   a->M = mat->M   = oldmat->M;
197244cd7ae7SLois Curfman McInnes   a->N = mat->N   = oldmat->N;
1973d6dfbf8fSBarry Smith 
1974416022c9SBarry Smith   a->rstart       = oldmat->rstart;
1975416022c9SBarry Smith   a->rend         = oldmat->rend;
1976416022c9SBarry Smith   a->cstart       = oldmat->cstart;
1977416022c9SBarry Smith   a->cend         = oldmat->cend;
197817699dbbSLois Curfman McInnes   a->size         = oldmat->size;
197917699dbbSLois Curfman McInnes   a->rank         = oldmat->rank;
1980e7641de0SSatish Balay   a->donotstash   = oldmat->donotstash;
1981e7641de0SSatish Balay   a->roworiented  = oldmat->roworiented;
1982e7641de0SSatish Balay   a->rowindices   = 0;
1983bcd2baecSBarry Smith   a->rowvalues    = 0;
1984bcd2baecSBarry Smith   a->getrowactive = PETSC_FALSE;
1985d6dfbf8fSBarry Smith 
1986603f58a4SSatish Balay   a->rowners = (int *) PetscMalloc(2*(a->size+2)*sizeof(int)); CHKPTRQ(a->rowners);
1987f09e8eb9SSatish Balay   PLogObjectMemory(mat,2*(a->size+2)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_MPIAIJ));
1988603f58a4SSatish Balay   a->cowners = a->rowners + a->size + 2;
1989603f58a4SSatish Balay   PetscMemcpy(a->rowners,oldmat->rowners,2*(a->size+2)*sizeof(int));
1990416022c9SBarry Smith   ierr = StashInitialize_Private(&a->stash); CHKERRQ(ierr);
19912ee70a88SLois Curfman McInnes   if (oldmat->colmap) {
1992b1fc9764SSatish Balay #if defined (USE_CTABLE)
1993*fa46199cSSatish Balay     ierr = TableCreateCopy(oldmat->colmap,&a->colmap); CHKERRQ(ierr);
1994b1fc9764SSatish Balay #else
19950452661fSBarry Smith     a->colmap = (int *) PetscMalloc((a->N)*sizeof(int));CHKPTRQ(a->colmap);
1996416022c9SBarry Smith     PLogObjectMemory(mat,(a->N)*sizeof(int));
1997416022c9SBarry Smith     PetscMemcpy(a->colmap,oldmat->colmap,(a->N)*sizeof(int));
1998b1fc9764SSatish Balay #endif
1999416022c9SBarry Smith   } else a->colmap = 0;
20003f41c07dSBarry Smith   if (oldmat->garray) {
20013f41c07dSBarry Smith     len = ((Mat_SeqAIJ *) (oldmat->B->data))->n;
20023f41c07dSBarry Smith     a->garray = (int *) PetscMalloc((len+1)*sizeof(int)); CHKPTRQ(a->garray);
2003464493b3SBarry Smith     PLogObjectMemory(mat,len*sizeof(int));
20043f41c07dSBarry Smith     if (len) PetscMemcpy(a->garray,oldmat->garray,len*sizeof(int));
2005416022c9SBarry Smith   } else a->garray = 0;
2006d6dfbf8fSBarry Smith 
2007416022c9SBarry Smith   ierr =  VecDuplicate(oldmat->lvec,&a->lvec); CHKERRQ(ierr);
2008416022c9SBarry Smith   PLogObjectParent(mat,a->lvec);
2009a56f8943SBarry Smith   ierr =  VecScatterCopy(oldmat->Mvctx,&a->Mvctx); CHKERRQ(ierr);
2010416022c9SBarry Smith   PLogObjectParent(mat,a->Mvctx);
20112e8a6d31SBarry Smith   ierr =  MatDuplicate(oldmat->A,cpvalues,&a->A); CHKERRQ(ierr);
2012416022c9SBarry Smith   PLogObjectParent(mat,a->A);
20132e8a6d31SBarry Smith   ierr =  MatDuplicate(oldmat->B,cpvalues,&a->B); CHKERRQ(ierr);
2014416022c9SBarry Smith   PLogObjectParent(mat,a->B);
20155dd7a6c7SBarry Smith   ierr = OptionsHasName(PETSC_NULL,"-help",&flg); CHKERRQ(ierr);
201625cdf11fSBarry Smith   if (flg) {
2017682d7d0cSBarry Smith     ierr = MatPrintHelp(mat); CHKERRQ(ierr);
2018682d7d0cSBarry Smith   }
201927508adbSBarry Smith   ierr = FListDuplicate(matin->qlist,&mat->qlist);CHKERRQ(ierr);
20208a729477SBarry Smith   *newmat = mat;
20213a40ed3dSBarry Smith   PetscFunctionReturn(0);
20228a729477SBarry Smith }
2023416022c9SBarry Smith 
202477c4ece6SBarry Smith #include "sys.h"
2025416022c9SBarry Smith 
20265615d1e5SSatish Balay #undef __FUNC__
20275615d1e5SSatish Balay #define __FUNC__ "MatLoad_MPIAIJ"
202819bcc07fSBarry Smith int MatLoad_MPIAIJ(Viewer viewer,MatType type,Mat *newmat)
2029416022c9SBarry Smith {
2030d65a2f8fSBarry Smith   Mat          A;
2031d65a2f8fSBarry Smith   Scalar       *vals,*svals;
203219bcc07fSBarry Smith   MPI_Comm     comm = ((PetscObject)viewer)->comm;
2033416022c9SBarry Smith   MPI_Status   status;
20343a40ed3dSBarry Smith   int          i, nz, ierr, j,rstart, rend, fd;
203517699dbbSLois Curfman McInnes   int          header[4],rank,size,*rowlengths = 0,M,N,m,*rowners,maxnz,*cols;
2036d65a2f8fSBarry Smith   int          *ourlens,*sndcounts = 0,*procsnz = 0, *offlens,jj,*mycols,*smycols;
2037b362ba68SBarry Smith   int          tag = ((PetscObject)viewer)->tag,cend,cstart,n;
2038416022c9SBarry Smith 
20393a40ed3dSBarry Smith   PetscFunctionBegin;
204017699dbbSLois Curfman McInnes   MPI_Comm_size(comm,&size); MPI_Comm_rank(comm,&rank);
204117699dbbSLois Curfman McInnes   if (!rank) {
204290ace30eSBarry Smith     ierr = ViewerBinaryGetDescriptor(viewer,&fd); CHKERRQ(ierr);
20430752156aSBarry Smith     ierr = PetscBinaryRead(fd,(char *)header,4,PETSC_INT); CHKERRQ(ierr);
2044a8c6a408SBarry Smith     if (header[0] != MAT_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,0,"not matrix object");
2045d64ed03dSBarry Smith     if (header[3] < 0) {
2046a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_FILE_UNEXPECTED,1,"Matrix in special format on disk, cannot load as MPIAIJ");
2047d64ed03dSBarry Smith     }
20486c5fab8fSBarry Smith   }
20496c5fab8fSBarry Smith 
2050ca161407SBarry Smith   ierr = MPI_Bcast(header+1,3,MPI_INT,0,comm);CHKERRQ(ierr);
2051416022c9SBarry Smith   M = header[1]; N = header[2];
2052416022c9SBarry Smith   /* determine ownership of all rows */
205317699dbbSLois Curfman McInnes   m = M/size + ((M % size) > rank);
20540452661fSBarry Smith   rowners = (int *) PetscMalloc((size+2)*sizeof(int)); CHKPTRQ(rowners);
2055ca161407SBarry Smith   ierr = MPI_Allgather(&m,1,MPI_INT,rowners+1,1,MPI_INT,comm);CHKERRQ(ierr);
2056416022c9SBarry Smith   rowners[0] = 0;
205717699dbbSLois Curfman McInnes   for ( i=2; i<=size; i++ ) {
2058416022c9SBarry Smith     rowners[i] += rowners[i-1];
2059416022c9SBarry Smith   }
206017699dbbSLois Curfman McInnes   rstart = rowners[rank];
206117699dbbSLois Curfman McInnes   rend   = rowners[rank+1];
2062416022c9SBarry Smith 
2063416022c9SBarry Smith   /* distribute row lengths to all processors */
20640452661fSBarry Smith   ourlens = (int*) PetscMalloc( 2*(rend-rstart)*sizeof(int) ); CHKPTRQ(ourlens);
2065416022c9SBarry Smith   offlens = ourlens + (rend-rstart);
206617699dbbSLois Curfman McInnes   if (!rank) {
20670452661fSBarry Smith     rowlengths = (int*) PetscMalloc( M*sizeof(int) ); CHKPTRQ(rowlengths);
20680752156aSBarry Smith     ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT); CHKERRQ(ierr);
20690452661fSBarry Smith     sndcounts = (int*) PetscMalloc( size*sizeof(int) ); CHKPTRQ(sndcounts);
207017699dbbSLois Curfman McInnes     for ( i=0; i<size; i++ ) sndcounts[i] = rowners[i+1] - rowners[i];
2071ca161407SBarry Smith     ierr = MPI_Scatterv(rowlengths,sndcounts,rowners,MPI_INT,ourlens,rend-rstart,MPI_INT,0,comm);CHKERRQ(ierr);
20720452661fSBarry Smith     PetscFree(sndcounts);
20733a40ed3dSBarry Smith   } else {
2074ca161407SBarry Smith     ierr = MPI_Scatterv(0,0,0,MPI_INT,ourlens,rend-rstart,MPI_INT, 0,comm);CHKERRQ(ierr);
2075416022c9SBarry Smith   }
2076416022c9SBarry Smith 
207717699dbbSLois Curfman McInnes   if (!rank) {
2078416022c9SBarry Smith     /* calculate the number of nonzeros on each processor */
20790452661fSBarry Smith     procsnz = (int*) PetscMalloc( size*sizeof(int) ); CHKPTRQ(procsnz);
2080cddf8d76SBarry Smith     PetscMemzero(procsnz,size*sizeof(int));
208117699dbbSLois Curfman McInnes     for ( i=0; i<size; i++ ) {
2082416022c9SBarry Smith       for ( j=rowners[i]; j< rowners[i+1]; j++ ) {
2083416022c9SBarry Smith         procsnz[i] += rowlengths[j];
2084416022c9SBarry Smith       }
2085416022c9SBarry Smith     }
20860452661fSBarry Smith     PetscFree(rowlengths);
2087416022c9SBarry Smith 
2088416022c9SBarry Smith     /* determine max buffer needed and allocate it */
2089416022c9SBarry Smith     maxnz = 0;
209017699dbbSLois Curfman McInnes     for ( i=0; i<size; i++ ) {
20910452661fSBarry Smith       maxnz = PetscMax(maxnz,procsnz[i]);
2092416022c9SBarry Smith     }
20930452661fSBarry Smith     cols = (int *) PetscMalloc( maxnz*sizeof(int) ); CHKPTRQ(cols);
2094416022c9SBarry Smith 
2095416022c9SBarry Smith     /* read in my part of the matrix column indices  */
2096416022c9SBarry Smith     nz     = procsnz[0];
20970452661fSBarry Smith     mycols = (int *) PetscMalloc( nz*sizeof(int) ); CHKPTRQ(mycols);
20980752156aSBarry Smith     ierr   = PetscBinaryRead(fd,mycols,nz,PETSC_INT); CHKERRQ(ierr);
2099d65a2f8fSBarry Smith 
2100d65a2f8fSBarry Smith     /* read in every one elses and ship off */
210117699dbbSLois Curfman McInnes     for ( i=1; i<size; i++ ) {
2102d65a2f8fSBarry Smith       nz   = procsnz[i];
21030752156aSBarry Smith       ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT); CHKERRQ(ierr);
2104ca161407SBarry Smith       ierr = MPI_Send(cols,nz,MPI_INT,i,tag,comm);CHKERRQ(ierr);
2105d65a2f8fSBarry Smith     }
21060452661fSBarry Smith     PetscFree(cols);
21073a40ed3dSBarry Smith   } else {
2108416022c9SBarry Smith     /* determine buffer space needed for message */
2109416022c9SBarry Smith     nz = 0;
2110416022c9SBarry Smith     for ( i=0; i<m; i++ ) {
2111416022c9SBarry Smith       nz += ourlens[i];
2112416022c9SBarry Smith     }
21130452661fSBarry Smith     mycols = (int*) PetscMalloc( nz*sizeof(int) ); CHKPTRQ(mycols);
2114416022c9SBarry Smith 
2115416022c9SBarry Smith     /* receive message of column indices*/
2116ca161407SBarry Smith     ierr = MPI_Recv(mycols,nz,MPI_INT,0,tag,comm,&status);CHKERRQ(ierr);
2117ca161407SBarry Smith     ierr = MPI_Get_count(&status,MPI_INT,&maxnz);CHKERRQ(ierr);
2118a8c6a408SBarry Smith     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,0,"something is wrong with file");
2119416022c9SBarry Smith   }
2120416022c9SBarry Smith 
2121b362ba68SBarry Smith   /* determine column ownership if matrix is not square */
2122b362ba68SBarry Smith   if (N != M) {
2123b362ba68SBarry Smith     n      = N/size + ((N % size) > rank);
2124b362ba68SBarry Smith     ierr   = MPI_Scan(&n,&cend,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr);
2125b362ba68SBarry Smith     cstart = cend - n;
2126b362ba68SBarry Smith   } else {
2127b362ba68SBarry Smith     cstart = rstart;
2128b362ba68SBarry Smith     cend   = rend;
2129fb2e594dSBarry Smith     n      = cend - cstart;
2130b362ba68SBarry Smith   }
2131b362ba68SBarry Smith 
2132416022c9SBarry Smith   /* loop over local rows, determining number of off diagonal entries */
2133cddf8d76SBarry Smith   PetscMemzero(offlens,m*sizeof(int));
2134416022c9SBarry Smith   jj = 0;
2135416022c9SBarry Smith   for ( i=0; i<m; i++ ) {
2136416022c9SBarry Smith     for ( j=0; j<ourlens[i]; j++ ) {
2137b362ba68SBarry Smith       if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++;
2138416022c9SBarry Smith       jj++;
2139416022c9SBarry Smith     }
2140416022c9SBarry Smith   }
2141d65a2f8fSBarry Smith 
2142d65a2f8fSBarry Smith   /* create our matrix */
2143416022c9SBarry Smith   for ( i=0; i<m; i++ ) {
2144416022c9SBarry Smith     ourlens[i] -= offlens[i];
2145416022c9SBarry Smith   }
2146b362ba68SBarry Smith   ierr = MatCreateMPIAIJ(comm,m,n,M,N,0,ourlens,0,offlens,newmat);CHKERRQ(ierr);
2147d65a2f8fSBarry Smith   A = *newmat;
2148fb2e594dSBarry Smith   ierr = MatSetOption(A,MAT_COLUMNS_SORTED); CHKERRQ(ierr);
2149d65a2f8fSBarry Smith   for ( i=0; i<m; i++ ) {
2150d65a2f8fSBarry Smith     ourlens[i] += offlens[i];
2151d65a2f8fSBarry Smith   }
2152416022c9SBarry Smith 
215317699dbbSLois Curfman McInnes   if (!rank) {
21540452661fSBarry Smith     vals = (Scalar *) PetscMalloc( maxnz*sizeof(Scalar) ); CHKPTRQ(vals);
2155416022c9SBarry Smith 
2156416022c9SBarry Smith     /* read in my part of the matrix numerical values  */
2157416022c9SBarry Smith     nz   = procsnz[0];
21580752156aSBarry Smith     ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR); CHKERRQ(ierr);
2159d65a2f8fSBarry Smith 
2160d65a2f8fSBarry Smith     /* insert into matrix */
2161d65a2f8fSBarry Smith     jj      = rstart;
2162d65a2f8fSBarry Smith     smycols = mycols;
2163d65a2f8fSBarry Smith     svals   = vals;
2164d65a2f8fSBarry Smith     for ( i=0; i<m; i++ ) {
2165d65a2f8fSBarry Smith       ierr = MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
2166d65a2f8fSBarry Smith       smycols += ourlens[i];
2167d65a2f8fSBarry Smith       svals   += ourlens[i];
2168d65a2f8fSBarry Smith       jj++;
2169416022c9SBarry Smith     }
2170416022c9SBarry Smith 
2171d65a2f8fSBarry Smith     /* read in other processors and ship out */
217217699dbbSLois Curfman McInnes     for ( i=1; i<size; i++ ) {
2173416022c9SBarry Smith       nz   = procsnz[i];
21740752156aSBarry Smith       ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR); CHKERRQ(ierr);
2175ca161407SBarry Smith       ierr = MPI_Send(vals,nz,MPIU_SCALAR,i,A->tag,comm);CHKERRQ(ierr);
2176416022c9SBarry Smith     }
21770452661fSBarry Smith     PetscFree(procsnz);
21783a40ed3dSBarry Smith   } else {
2179d65a2f8fSBarry Smith     /* receive numeric values */
21800452661fSBarry Smith     vals = (Scalar*) PetscMalloc( nz*sizeof(Scalar) ); CHKPTRQ(vals);
2181416022c9SBarry Smith 
2182d65a2f8fSBarry Smith     /* receive message of values*/
2183ca161407SBarry Smith     ierr = MPI_Recv(vals,nz,MPIU_SCALAR,0,A->tag,comm,&status);CHKERRQ(ierr);
2184ca161407SBarry Smith     ierr = MPI_Get_count(&status,MPIU_SCALAR,&maxnz);CHKERRQ(ierr);
2185a8c6a408SBarry Smith     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,0,"something is wrong with file");
2186d65a2f8fSBarry Smith 
2187d65a2f8fSBarry Smith     /* insert into matrix */
2188d65a2f8fSBarry Smith     jj      = rstart;
2189d65a2f8fSBarry Smith     smycols = mycols;
2190d65a2f8fSBarry Smith     svals   = vals;
2191d65a2f8fSBarry Smith     for ( i=0; i<m; i++ ) {
2192d65a2f8fSBarry Smith       ierr     = MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
2193d65a2f8fSBarry Smith       smycols += ourlens[i];
2194d65a2f8fSBarry Smith       svals   += ourlens[i];
2195d65a2f8fSBarry Smith       jj++;
2196d65a2f8fSBarry Smith     }
2197d65a2f8fSBarry Smith   }
21980452661fSBarry Smith   PetscFree(ourlens); PetscFree(vals); PetscFree(mycols); PetscFree(rowners);
2199d65a2f8fSBarry Smith 
22006d4a8577SBarry Smith   ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
22016d4a8577SBarry Smith   ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
22023a40ed3dSBarry Smith   PetscFunctionReturn(0);
2203416022c9SBarry Smith }
2204a0ff6018SBarry Smith 
220529da9460SBarry Smith #undef __FUNC__
220629da9460SBarry Smith #define __FUNC__ "MatGetSubMatrix_MPIAIJ"
2207a0ff6018SBarry Smith /*
220829da9460SBarry Smith     Not great since it makes two copies of the submatrix, first an SeqAIJ
220929da9460SBarry Smith   in local and then by concatenating the local matrices the end result.
221029da9460SBarry Smith   Writing it directly would be much like MatGetSubMatrices_MPIAIJ()
2211a0ff6018SBarry Smith */
22127b2a1423SBarry Smith int MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,int csize,MatReuse call,Mat *newmat)
2213a0ff6018SBarry Smith {
221400e6dbe6SBarry Smith   int        ierr, i, m,n,rstart,row,rend,nz,*cwork,size,rank,j;
2215fee21e36SBarry Smith   Mat        *local,M, Mreuse;
221600e6dbe6SBarry Smith   Scalar     *vwork,*aa;
221700e6dbe6SBarry Smith   MPI_Comm   comm = mat->comm;
221800e6dbe6SBarry Smith   Mat_SeqAIJ *aij;
221900e6dbe6SBarry Smith   int        *ii, *jj,nlocal,*dlens,*olens,dlen,olen,jend;
2220a0ff6018SBarry Smith 
2221a0ff6018SBarry Smith   PetscFunctionBegin;
222200e6dbe6SBarry Smith   MPI_Comm_rank(comm,&rank);
222300e6dbe6SBarry Smith   MPI_Comm_size(comm,&size);
222400e6dbe6SBarry Smith 
2225fee21e36SBarry Smith   if (call ==  MAT_REUSE_MATRIX) {
2226fee21e36SBarry Smith     ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);CHKERRQ(ierr);
2227fee21e36SBarry Smith     if (!Mreuse) SETERRQ(1,1,"Submatrix passed in was not used before, cannot reuse");
2228fee21e36SBarry Smith     local = &Mreuse;
2229fee21e36SBarry Smith     ierr  = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&local);CHKERRQ(ierr);
2230fee21e36SBarry Smith   } else {
2231a0ff6018SBarry Smith     ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr);
2232fee21e36SBarry Smith     Mreuse = *local;
2233fee21e36SBarry Smith     PetscFree(local);
2234fee21e36SBarry Smith   }
2235a0ff6018SBarry Smith 
2236a0ff6018SBarry Smith   /*
2237a0ff6018SBarry Smith       m - number of local rows
2238a0ff6018SBarry Smith       n - number of columns (same on all processors)
2239a0ff6018SBarry Smith       rstart - first row in new global matrix generated
2240a0ff6018SBarry Smith   */
2241fee21e36SBarry Smith   ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr);
2242a0ff6018SBarry Smith   if (call == MAT_INITIAL_MATRIX) {
2243fee21e36SBarry Smith     aij = (Mat_SeqAIJ *) (Mreuse)->data;
2244a8c6a408SBarry Smith     if (aij->indexshift) SETERRQ(PETSC_ERR_SUP,1,"No support for index shifted matrix");
224500e6dbe6SBarry Smith     ii  = aij->i;
224600e6dbe6SBarry Smith     jj  = aij->j;
224700e6dbe6SBarry Smith 
2248a0ff6018SBarry Smith     /*
224900e6dbe6SBarry Smith         Determine the number of non-zeros in the diagonal and off-diagonal
225000e6dbe6SBarry Smith         portions of the matrix in order to do correct preallocation
2251a0ff6018SBarry Smith     */
225200e6dbe6SBarry Smith 
225300e6dbe6SBarry Smith     /* first get start and end of "diagonal" columns */
22546a6a5d1dSBarry Smith     if (csize == PETSC_DECIDE) {
225500e6dbe6SBarry Smith       nlocal = n/size + ((n % size) > rank);
22566a6a5d1dSBarry Smith     } else {
22576a6a5d1dSBarry Smith       nlocal = csize;
22586a6a5d1dSBarry Smith     }
2259ca161407SBarry Smith     ierr   = MPI_Scan(&nlocal,&rend,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr);
226000e6dbe6SBarry Smith     rstart = rend - nlocal;
22616a6a5d1dSBarry Smith     if (rank == size - 1 && rend != n) {
22626a6a5d1dSBarry Smith       SETERRQ(1,1,"Local column sizes do not add up to total number of columns");
22636a6a5d1dSBarry Smith     }
226400e6dbe6SBarry Smith 
226500e6dbe6SBarry Smith     /* next, compute all the lengths */
226600e6dbe6SBarry Smith     dlens = (int *) PetscMalloc( (2*m+1)*sizeof(int) );CHKPTRQ(dlens);
226700e6dbe6SBarry Smith     olens = dlens + m;
226800e6dbe6SBarry Smith     for ( i=0; i<m; i++ ) {
226900e6dbe6SBarry Smith       jend = ii[i+1] - ii[i];
227000e6dbe6SBarry Smith       olen = 0;
227100e6dbe6SBarry Smith       dlen = 0;
227200e6dbe6SBarry Smith       for ( j=0; j<jend; j++ ) {
227300e6dbe6SBarry Smith         if ( *jj < rstart || *jj >= rend) olen++;
227400e6dbe6SBarry Smith         else dlen++;
227500e6dbe6SBarry Smith         jj++;
227600e6dbe6SBarry Smith       }
227700e6dbe6SBarry Smith       olens[i] = olen;
227800e6dbe6SBarry Smith       dlens[i] = dlen;
227900e6dbe6SBarry Smith     }
22802d207970SBarry Smith     ierr = MatCreateMPIAIJ(comm,m,nlocal,PETSC_DECIDE,n,0,dlens,0,olens,&M);CHKERRQ(ierr);
228100e6dbe6SBarry Smith     PetscFree(dlens);
2282a0ff6018SBarry Smith   } else {
2283a0ff6018SBarry Smith     int ml,nl;
2284a0ff6018SBarry Smith 
2285a0ff6018SBarry Smith     M = *newmat;
2286a0ff6018SBarry Smith     ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr);
2287a8c6a408SBarry Smith     if (ml != m) SETERRQ(PETSC_ERR_ARG_SIZ,1,"Previous matrix must be same size/layout as request");
2288a0ff6018SBarry Smith     ierr = MatZeroEntries(M);CHKERRQ(ierr);
2289c48de900SBarry Smith     /*
2290c48de900SBarry Smith          The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
2291c48de900SBarry Smith        rather than the slower MatSetValues().
2292c48de900SBarry Smith     */
2293c48de900SBarry Smith     M->was_assembled = PETSC_TRUE;
2294c48de900SBarry Smith     M->assembled     = PETSC_FALSE;
2295a0ff6018SBarry Smith   }
2296a0ff6018SBarry Smith   ierr = MatGetOwnershipRange(M,&rstart,&rend); CHKERRQ(ierr);
2297fee21e36SBarry Smith   aij = (Mat_SeqAIJ *) (Mreuse)->data;
2298a8c6a408SBarry Smith   if (aij->indexshift) SETERRQ(PETSC_ERR_SUP,1,"No support for index shifted matrix");
229900e6dbe6SBarry Smith   ii  = aij->i;
230000e6dbe6SBarry Smith   jj  = aij->j;
230100e6dbe6SBarry Smith   aa  = aij->a;
2302a0ff6018SBarry Smith   for (i=0; i<m; i++) {
2303a0ff6018SBarry Smith     row   = rstart + i;
230400e6dbe6SBarry Smith     nz    = ii[i+1] - ii[i];
230500e6dbe6SBarry Smith     cwork = jj;     jj += nz;
230600e6dbe6SBarry Smith     vwork = aa;     aa += nz;
23078c638d02SBarry Smith     ierr = MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES); CHKERRQ(ierr);
2308a0ff6018SBarry Smith   }
2309a0ff6018SBarry Smith 
2310a0ff6018SBarry Smith   ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
2311a0ff6018SBarry Smith   ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
2312a0ff6018SBarry Smith   *newmat = M;
2313fee21e36SBarry Smith 
2314fee21e36SBarry Smith   /* save submatrix used in processor for next request */
2315fee21e36SBarry Smith   if (call ==  MAT_INITIAL_MATRIX) {
2316fee21e36SBarry Smith     ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr);
2317fee21e36SBarry Smith     ierr = PetscObjectDereference((PetscObject)Mreuse);CHKERRQ(ierr);
2318fee21e36SBarry Smith   }
2319fee21e36SBarry Smith 
2320a0ff6018SBarry Smith   PetscFunctionReturn(0);
2321a0ff6018SBarry Smith }
2322