xref: /petsc/src/mat/impls/aij/mpi/mpiaij.c (revision b362ba68db6f650e6a15e032e9ede2da51236b3c)
1b57c8cebSBarry Smith 
2a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
3*b362ba68SBarry Smith static char vcid[] = "$Id: mpiaij.c,v 1.262 1998/10/09 19:22:12 bsmith Exp bsmith $";
4cb512458SBarry Smith #endif
58a729477SBarry Smith 
63369ce9aSBarry Smith #include "pinclude/pviewer.h"
770f55243SBarry Smith #include "src/mat/impls/aij/mpi/mpiaij.h"
8f5eb4b81SSatish Balay #include "src/vec/vecimpl.h"
9d9942c19SSatish Balay #include "src/inline/spops.h"
108a729477SBarry Smith 
119e25ed09SBarry Smith /* local utility routine that creates a mapping from the global column
129e25ed09SBarry Smith number to the local number in the off-diagonal part of the local
139e25ed09SBarry Smith storage of the matrix.  This is done in a non scable way since the
149e25ed09SBarry Smith length of colmap equals the global matrix length.
159e25ed09SBarry Smith */
165615d1e5SSatish Balay #undef __FUNC__
17d4bb536fSBarry Smith #define __FUNC__ "CreateColmap_MPIAIJ_Private"
180a198c4cSBarry Smith int CreateColmap_MPIAIJ_Private(Mat mat)
199e25ed09SBarry Smith {
2044a69424SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data;
21ec8511deSBarry Smith   Mat_SeqAIJ *B = (Mat_SeqAIJ*) aij->B->data;
22905e6a2fSBarry Smith   int        n = B->n,i;
23dbb450caSBarry Smith 
243a40ed3dSBarry Smith   PetscFunctionBegin;
25758f045eSSatish Balay   aij->colmap = (int *) PetscMalloc((aij->N+1)*sizeof(int));CHKPTRQ(aij->colmap);
26464493b3SBarry Smith   PLogObjectMemory(mat,aij->N*sizeof(int));
27cddf8d76SBarry Smith   PetscMemzero(aij->colmap,aij->N*sizeof(int));
28905e6a2fSBarry Smith   for ( i=0; i<n; i++ ) aij->colmap[aij->garray[i]] = i+1;
293a40ed3dSBarry Smith   PetscFunctionReturn(0);
309e25ed09SBarry Smith }
319e25ed09SBarry Smith 
322493cbb0SBarry Smith extern int DisAssemble_MPIAIJ(Mat);
332493cbb0SBarry Smith 
340520107fSSatish Balay #define CHUNKSIZE   15
3530770e4dSSatish Balay #define MatSetValues_SeqAIJ_A_Private(row,col,value,addv) \
360520107fSSatish Balay { \
370520107fSSatish Balay  \
380520107fSSatish Balay     rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift; \
3930770e4dSSatish Balay     rmax = aimax[row]; nrow = ailen[row];  \
40f5e9677aSSatish Balay     col1 = col - shift; \
41f5e9677aSSatish Balay      \
42ba4e3ef2SSatish Balay     low = 0; high = nrow; \
43ba4e3ef2SSatish Balay     while (high-low > 5) { \
44ba4e3ef2SSatish Balay       t = (low+high)/2; \
45ba4e3ef2SSatish Balay       if (rp[t] > col) high = t; \
46ba4e3ef2SSatish Balay       else             low  = t; \
47ba4e3ef2SSatish Balay     } \
480520107fSSatish Balay       for ( _i=0; _i<nrow; _i++ ) { \
49f5e9677aSSatish Balay         if (rp[_i] > col1) break; \
50f5e9677aSSatish Balay         if (rp[_i] == col1) { \
510520107fSSatish Balay           if (addv == ADD_VALUES) ap[_i] += value;   \
520520107fSSatish Balay           else                  ap[_i] = value; \
5330770e4dSSatish Balay           goto a_noinsert; \
540520107fSSatish Balay         } \
550520107fSSatish Balay       }  \
5689280ab3SLois Curfman McInnes       if (nonew == 1) goto a_noinsert; \
57a8c6a408SBarry Smith       else if (nonew == -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Inserting a new nonzero into matrix"); \
580520107fSSatish Balay       if (nrow >= rmax) { \
590520107fSSatish Balay         /* there is no extra room in row, therefore enlarge */ \
600520107fSSatish Balay         int    new_nz = ai[a->m] + CHUNKSIZE,len,*new_i,*new_j; \
610520107fSSatish Balay         Scalar *new_a; \
620520107fSSatish Balay  \
63a8c6a408SBarry Smith         if (nonew == -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Inserting a new nonzero in the matrix"); \
6489280ab3SLois Curfman McInnes  \
650520107fSSatish Balay         /* malloc new storage space */ \
660520107fSSatish Balay         len     = new_nz*(sizeof(int)+sizeof(Scalar))+(a->m+1)*sizeof(int); \
670520107fSSatish Balay         new_a   = (Scalar *) PetscMalloc( len ); CHKPTRQ(new_a); \
680520107fSSatish Balay         new_j   = (int *) (new_a + new_nz); \
690520107fSSatish Balay         new_i   = new_j + new_nz; \
700520107fSSatish Balay  \
710520107fSSatish Balay         /* copy over old data into new slots */ \
720520107fSSatish Balay         for ( ii=0; ii<row+1; ii++ ) {new_i[ii] = ai[ii];} \
730520107fSSatish Balay         for ( ii=row+1; ii<a->m+1; ii++ ) {new_i[ii] = ai[ii]+CHUNKSIZE;} \
740520107fSSatish Balay         PetscMemcpy(new_j,aj,(ai[row]+nrow+shift)*sizeof(int)); \
750520107fSSatish Balay         len = (new_nz - CHUNKSIZE - ai[row] - nrow - shift); \
760520107fSSatish Balay         PetscMemcpy(new_j+ai[row]+shift+nrow+CHUNKSIZE,aj+ai[row]+shift+nrow, \
770520107fSSatish Balay                                                            len*sizeof(int)); \
780520107fSSatish Balay         PetscMemcpy(new_a,aa,(ai[row]+nrow+shift)*sizeof(Scalar)); \
790520107fSSatish Balay         PetscMemcpy(new_a+ai[row]+shift+nrow+CHUNKSIZE,aa+ai[row]+shift+nrow, \
800520107fSSatish Balay                                                            len*sizeof(Scalar));  \
810520107fSSatish Balay         /* free up old matrix storage */ \
82f5e9677aSSatish Balay  \
830520107fSSatish Balay         PetscFree(a->a);  \
840520107fSSatish Balay         if (!a->singlemalloc) {PetscFree(a->i);PetscFree(a->j);} \
850520107fSSatish Balay         aa = a->a = new_a; ai = a->i = new_i; aj = a->j = new_j;  \
860520107fSSatish Balay         a->singlemalloc = 1; \
870520107fSSatish Balay  \
880520107fSSatish Balay         rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift; \
8930770e4dSSatish Balay         rmax = aimax[row] = aimax[row] + CHUNKSIZE; \
900520107fSSatish Balay         PLogObjectMemory(A,CHUNKSIZE*(sizeof(int) + sizeof(Scalar))); \
910520107fSSatish Balay         a->maxnz += CHUNKSIZE; \
920520107fSSatish Balay         a->reallocs++; \
930520107fSSatish Balay       } \
940520107fSSatish Balay       N = nrow++ - 1; a->nz++; \
950520107fSSatish Balay       /* shift up all the later entries in this row */ \
960520107fSSatish Balay       for ( ii=N; ii>=_i; ii-- ) { \
970520107fSSatish Balay         rp[ii+1] = rp[ii]; \
980520107fSSatish Balay         ap[ii+1] = ap[ii]; \
990520107fSSatish Balay       } \
100f5e9677aSSatish Balay       rp[_i] = col1;  \
1010520107fSSatish Balay       ap[_i] = value;  \
10230770e4dSSatish Balay       a_noinsert: ; \
1030520107fSSatish Balay       ailen[row] = nrow; \
1040520107fSSatish Balay }
1050a198c4cSBarry Smith 
10630770e4dSSatish Balay #define MatSetValues_SeqAIJ_B_Private(row,col,value,addv) \
10730770e4dSSatish Balay { \
10830770e4dSSatish Balay  \
10930770e4dSSatish Balay     rp   = bj + bi[row] + shift; ap = ba + bi[row] + shift; \
11030770e4dSSatish Balay     rmax = bimax[row]; nrow = bilen[row];  \
11130770e4dSSatish Balay     col1 = col - shift; \
11230770e4dSSatish Balay      \
113ba4e3ef2SSatish Balay     low = 0; high = nrow; \
114ba4e3ef2SSatish Balay     while (high-low > 5) { \
115ba4e3ef2SSatish Balay       t = (low+high)/2; \
116ba4e3ef2SSatish Balay       if (rp[t] > col) high = t; \
117ba4e3ef2SSatish Balay       else             low  = t; \
118ba4e3ef2SSatish Balay     } \
11930770e4dSSatish Balay        for ( _i=0; _i<nrow; _i++ ) { \
12030770e4dSSatish Balay         if (rp[_i] > col1) break; \
12130770e4dSSatish Balay         if (rp[_i] == col1) { \
12230770e4dSSatish Balay           if (addv == ADD_VALUES) ap[_i] += value;   \
12330770e4dSSatish Balay           else                  ap[_i] = value; \
12430770e4dSSatish Balay           goto b_noinsert; \
12530770e4dSSatish Balay         } \
12630770e4dSSatish Balay       }  \
12789280ab3SLois Curfman McInnes       if (nonew == 1) goto b_noinsert; \
128a8c6a408SBarry Smith       else if (nonew == -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Inserting a new nonzero into matrix"); \
12930770e4dSSatish Balay       if (nrow >= rmax) { \
13030770e4dSSatish Balay         /* there is no extra room in row, therefore enlarge */ \
13174c639caSSatish Balay         int    new_nz = bi[b->m] + CHUNKSIZE,len,*new_i,*new_j; \
13230770e4dSSatish Balay         Scalar *new_a; \
13330770e4dSSatish Balay  \
134a8c6a408SBarry Smith         if (nonew == -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Inserting a new nonzero in the matrix"); \
13589280ab3SLois Curfman McInnes  \
13630770e4dSSatish Balay         /* malloc new storage space */ \
13774c639caSSatish Balay         len     = new_nz*(sizeof(int)+sizeof(Scalar))+(b->m+1)*sizeof(int); \
13830770e4dSSatish Balay         new_a   = (Scalar *) PetscMalloc( len ); CHKPTRQ(new_a); \
13930770e4dSSatish Balay         new_j   = (int *) (new_a + new_nz); \
14030770e4dSSatish Balay         new_i   = new_j + new_nz; \
14130770e4dSSatish Balay  \
14230770e4dSSatish Balay         /* copy over old data into new slots */ \
14330770e4dSSatish Balay         for ( ii=0; ii<row+1; ii++ ) {new_i[ii] = bi[ii];} \
14474c639caSSatish Balay         for ( ii=row+1; ii<b->m+1; ii++ ) {new_i[ii] = bi[ii]+CHUNKSIZE;} \
14530770e4dSSatish Balay         PetscMemcpy(new_j,bj,(bi[row]+nrow+shift)*sizeof(int)); \
14630770e4dSSatish Balay         len = (new_nz - CHUNKSIZE - bi[row] - nrow - shift); \
14730770e4dSSatish Balay         PetscMemcpy(new_j+bi[row]+shift+nrow+CHUNKSIZE,bj+bi[row]+shift+nrow, \
14830770e4dSSatish Balay                                                            len*sizeof(int)); \
14930770e4dSSatish Balay         PetscMemcpy(new_a,ba,(bi[row]+nrow+shift)*sizeof(Scalar)); \
15030770e4dSSatish Balay         PetscMemcpy(new_a+bi[row]+shift+nrow+CHUNKSIZE,ba+bi[row]+shift+nrow, \
15130770e4dSSatish Balay                                                            len*sizeof(Scalar));  \
15230770e4dSSatish Balay         /* free up old matrix storage */ \
15330770e4dSSatish Balay  \
15474c639caSSatish Balay         PetscFree(b->a);  \
15574c639caSSatish Balay         if (!b->singlemalloc) {PetscFree(b->i);PetscFree(b->j);} \
15674c639caSSatish Balay         ba = b->a = new_a; bi = b->i = new_i; bj = b->j = new_j;  \
15774c639caSSatish Balay         b->singlemalloc = 1; \
15830770e4dSSatish Balay  \
15930770e4dSSatish Balay         rp   = bj + bi[row] + shift; ap = ba + bi[row] + shift; \
16030770e4dSSatish Balay         rmax = bimax[row] = bimax[row] + CHUNKSIZE; \
16174c639caSSatish Balay         PLogObjectMemory(B,CHUNKSIZE*(sizeof(int) + sizeof(Scalar))); \
16274c639caSSatish Balay         b->maxnz += CHUNKSIZE; \
16374c639caSSatish Balay         b->reallocs++; \
16430770e4dSSatish Balay       } \
16574c639caSSatish Balay       N = nrow++ - 1; b->nz++; \
16630770e4dSSatish Balay       /* shift up all the later entries in this row */ \
16730770e4dSSatish Balay       for ( ii=N; ii>=_i; ii-- ) { \
16830770e4dSSatish Balay         rp[ii+1] = rp[ii]; \
16930770e4dSSatish Balay         ap[ii+1] = ap[ii]; \
17030770e4dSSatish Balay       } \
17130770e4dSSatish Balay       rp[_i] = col1;  \
17230770e4dSSatish Balay       ap[_i] = value;  \
17330770e4dSSatish Balay       b_noinsert: ; \
17430770e4dSSatish Balay       bilen[row] = nrow; \
17530770e4dSSatish Balay }
17630770e4dSSatish Balay 
1770520107fSSatish Balay extern int MatSetValues_SeqAIJ(Mat,int,int*,int,int*,Scalar*,InsertMode);
1785615d1e5SSatish Balay #undef __FUNC__
1795615d1e5SSatish Balay #define __FUNC__ "MatSetValues_MPIAIJ"
1808f6be9afSLois Curfman McInnes int MatSetValues_MPIAIJ(Mat mat,int m,int *im,int n,int *in,Scalar *v,InsertMode addv)
1818a729477SBarry Smith {
18244a69424SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data;
1834b0e389bSBarry Smith   Scalar     value;
1841eb62cbbSBarry Smith   int        ierr,i,j, rstart = aij->rstart, rend = aij->rend;
1851eb62cbbSBarry Smith   int        cstart = aij->cstart, cend = aij->cend,row,col;
186905e6a2fSBarry Smith   int        roworiented = aij->roworiented;
1878a729477SBarry Smith 
1880520107fSSatish Balay   /* Some Variables required in the macro */
1894ee7247eSSatish Balay   Mat        A = aij->A;
1904ee7247eSSatish Balay   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
19130770e4dSSatish Balay   int        *aimax = a->imax, *ai = a->i, *ailen = a->ilen,*aj = a->j;
19230770e4dSSatish Balay   Scalar     *aa = a->a;
19330770e4dSSatish Balay 
19430770e4dSSatish Balay   Mat        B = aij->B;
19530770e4dSSatish Balay   Mat_SeqAIJ *b = (Mat_SeqAIJ *) B->data;
19630770e4dSSatish Balay   int        *bimax = b->imax, *bi = b->i, *bilen = b->ilen,*bj = b->j;
19730770e4dSSatish Balay   Scalar     *ba = b->a;
19830770e4dSSatish Balay 
199ba4e3ef2SSatish Balay   int        *rp,ii,nrow,_i,rmax, N, col1,low,high,t;
20030770e4dSSatish Balay   int        nonew = a->nonew,shift = a->indexshift;
20130770e4dSSatish Balay   Scalar     *ap;
2024ee7247eSSatish Balay 
2033a40ed3dSBarry Smith   PetscFunctionBegin;
2048a729477SBarry Smith   for ( i=0; i<m; i++ ) {
2053a40ed3dSBarry Smith #if defined(USE_PETSC_BOPT_g)
206a8c6a408SBarry Smith     if (im[i] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Negative row");
207a8c6a408SBarry Smith     if (im[i] >= aij->M) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Row too large");
2080a198c4cSBarry Smith #endif
2094b0e389bSBarry Smith     if (im[i] >= rstart && im[i] < rend) {
2104b0e389bSBarry Smith       row = im[i] - rstart;
2111eb62cbbSBarry Smith       for ( j=0; j<n; j++ ) {
2124b0e389bSBarry Smith         if (in[j] >= cstart && in[j] < cend){
2134b0e389bSBarry Smith           col = in[j] - cstart;
2144b0e389bSBarry Smith           if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
21530770e4dSSatish Balay           MatSetValues_SeqAIJ_A_Private(row,col,value,addv);
2160520107fSSatish Balay           /* ierr = MatSetValues_SeqAIJ(aij->A,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */
2171eb62cbbSBarry Smith         }
2183a40ed3dSBarry Smith #if defined(USE_PETSC_BOPT_g)
219a8c6a408SBarry Smith         else if (in[j] < 0) {SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Negative column");}
220a8c6a408SBarry Smith         else if (in[j] >= aij->N) {SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Column too large");}
2210a198c4cSBarry Smith #endif
2221eb62cbbSBarry Smith         else {
223227d817aSBarry Smith           if (mat->was_assembled) {
224905e6a2fSBarry Smith             if (!aij->colmap) {
225905e6a2fSBarry Smith               ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
226905e6a2fSBarry Smith             }
227905e6a2fSBarry Smith             col = aij->colmap[in[j]] - 1;
228ec8511deSBarry Smith             if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
2292493cbb0SBarry Smith               ierr = DisAssemble_MPIAIJ(mat); CHKERRQ(ierr);
2304b0e389bSBarry Smith               col =  in[j];
2319bf004c3SSatish Balay               /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
232f9508a3cSSatish Balay               B = aij->B;
233f9508a3cSSatish Balay               b = (Mat_SeqAIJ *) B->data;
234f9508a3cSSatish Balay               bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j;
235f9508a3cSSatish Balay               ba = b->a;
236d6dfbf8fSBarry Smith             }
237c48de900SBarry Smith           } else col = in[j];
2384b0e389bSBarry Smith           if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
23930770e4dSSatish Balay           MatSetValues_SeqAIJ_B_Private(row,col,value,addv);
24030770e4dSSatish Balay           /* ierr = MatSetValues_SeqAIJ(aij->B,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */
2411eb62cbbSBarry Smith         }
2421eb62cbbSBarry Smith       }
2431eb62cbbSBarry Smith     }
2441eb62cbbSBarry Smith     else {
24590f02eecSBarry Smith       if (roworiented && !aij->donotstash) {
2464b0e389bSBarry Smith         ierr = StashValues_Private(&aij->stash,im[i],n,in,v+i*n,addv);CHKERRQ(ierr);
2474b0e389bSBarry Smith       }
2484b0e389bSBarry Smith       else {
24990f02eecSBarry Smith         if (!aij->donotstash) {
2504b0e389bSBarry Smith           row = im[i];
2514b0e389bSBarry Smith           for ( j=0; j<n; j++ ) {
2524b0e389bSBarry Smith             ierr = StashValues_Private(&aij->stash,row,1,in+j,v+i+j*m,addv);CHKERRQ(ierr);
2534b0e389bSBarry Smith           }
2544b0e389bSBarry Smith         }
2551eb62cbbSBarry Smith       }
2568a729477SBarry Smith     }
25790f02eecSBarry Smith   }
2583a40ed3dSBarry Smith   PetscFunctionReturn(0);
2598a729477SBarry Smith }
2608a729477SBarry Smith 
2615615d1e5SSatish Balay #undef __FUNC__
2625615d1e5SSatish Balay #define __FUNC__ "MatGetValues_MPIAIJ"
2638f6be9afSLois Curfman McInnes int MatGetValues_MPIAIJ(Mat mat,int m,int *idxm,int n,int *idxn,Scalar *v)
264b49de8d1SLois Curfman McInnes {
265b49de8d1SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data;
266b49de8d1SLois Curfman McInnes   int        ierr,i,j, rstart = aij->rstart, rend = aij->rend;
267b49de8d1SLois Curfman McInnes   int        cstart = aij->cstart, cend = aij->cend,row,col;
268b49de8d1SLois Curfman McInnes 
2693a40ed3dSBarry Smith   PetscFunctionBegin;
270b49de8d1SLois Curfman McInnes   for ( i=0; i<m; i++ ) {
271a8c6a408SBarry Smith     if (idxm[i] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Negative row");
272a8c6a408SBarry Smith     if (idxm[i] >= aij->M) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Row too large");
273b49de8d1SLois Curfman McInnes     if (idxm[i] >= rstart && idxm[i] < rend) {
274b49de8d1SLois Curfman McInnes       row = idxm[i] - rstart;
275b49de8d1SLois Curfman McInnes       for ( j=0; j<n; j++ ) {
276a8c6a408SBarry Smith         if (idxn[j] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Negative column");
277a8c6a408SBarry Smith         if (idxn[j] >= aij->N) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Column too large");
278b49de8d1SLois Curfman McInnes         if (idxn[j] >= cstart && idxn[j] < cend){
279b49de8d1SLois Curfman McInnes           col = idxn[j] - cstart;
280b49de8d1SLois Curfman McInnes           ierr = MatGetValues(aij->A,1,&row,1,&col,v+i*n+j); CHKERRQ(ierr);
281fa852ad4SSatish Balay         } else {
282905e6a2fSBarry Smith           if (!aij->colmap) {
283905e6a2fSBarry Smith             ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
284905e6a2fSBarry Smith           }
285905e6a2fSBarry Smith           col = aij->colmap[idxn[j]] - 1;
286e60e1c95SSatish Balay           if ((col < 0) || (aij->garray[col] != idxn[j])) *(v+i*n+j) = 0.0;
287d9d09a02SSatish Balay           else {
288b49de8d1SLois Curfman McInnes             ierr = MatGetValues(aij->B,1,&row,1,&col,v+i*n+j); CHKERRQ(ierr);
289b49de8d1SLois Curfman McInnes           }
290b49de8d1SLois Curfman McInnes         }
291b49de8d1SLois Curfman McInnes       }
292a8c6a408SBarry Smith     } else {
293a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_SUP,0,"Only local values currently supported");
294b49de8d1SLois Curfman McInnes     }
295b49de8d1SLois Curfman McInnes   }
2963a40ed3dSBarry Smith   PetscFunctionReturn(0);
297b49de8d1SLois Curfman McInnes }
298b49de8d1SLois Curfman McInnes 
2995615d1e5SSatish Balay #undef __FUNC__
3005615d1e5SSatish Balay #define __FUNC__ "MatAssemblyBegin_MPIAIJ"
3018f6be9afSLois Curfman McInnes int MatAssemblyBegin_MPIAIJ(Mat mat,MatAssemblyType mode)
3028a729477SBarry Smith {
30344a69424SLois Curfman McInnes   Mat_MPIAIJ  *aij = (Mat_MPIAIJ *) mat->data;
304d6dfbf8fSBarry Smith   MPI_Comm    comm = mat->comm;
30517699dbbSLois Curfman McInnes   int         size = aij->size, *owners = aij->rowners;
30617699dbbSLois Curfman McInnes   int         rank = aij->rank,tag = mat->tag, *owner,*starts,count,ierr;
3071eb62cbbSBarry Smith   MPI_Request *send_waits,*recv_waits;
3086abc6512SBarry Smith   int         *nprocs,i,j,idx,*procs,nsends,nreceives,nmax,*work;
3091eb62cbbSBarry Smith   InsertMode  addv;
3101eb62cbbSBarry Smith   Scalar      *rvalues,*svalues;
3111eb62cbbSBarry Smith 
3123a40ed3dSBarry Smith   PetscFunctionBegin;
313b5bd3ad5SBarry Smith   if (aij->donotstash) {
314b5bd3ad5SBarry Smith     aij->svalues    = 0; aij->rvalues    = 0;
315b5bd3ad5SBarry Smith     aij->nsends     = 0; aij->nrecvs     = 0;
316b5bd3ad5SBarry Smith     aij->send_waits = 0; aij->recv_waits = 0;
317b5bd3ad5SBarry Smith     aij->rmax       = 0;
318b5bd3ad5SBarry Smith     PetscFunctionReturn(0);
319b5bd3ad5SBarry Smith   }
320b5bd3ad5SBarry Smith 
3211eb62cbbSBarry Smith   /* make sure all processors are either in INSERTMODE or ADDMODE */
322ca161407SBarry Smith   ierr = MPI_Allreduce(&mat->insertmode,&addv,1,MPI_INT,MPI_BOR,comm);CHKERRQ(ierr);
323dbb450caSBarry Smith   if (addv == (ADD_VALUES|INSERT_VALUES)) {
324a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Some processors inserted others added");
3251eb62cbbSBarry Smith   }
32647794344SBarry Smith   mat->insertmode = addv; /* in case this processor had no cache */
3271eb62cbbSBarry Smith 
3281eb62cbbSBarry Smith   /*  first count number of contributors to each processor */
3290452661fSBarry Smith   nprocs = (int *) PetscMalloc( 2*size*sizeof(int) ); CHKPTRQ(nprocs);
330cddf8d76SBarry Smith   PetscMemzero(nprocs,2*size*sizeof(int)); procs = nprocs + size;
3310452661fSBarry Smith   owner = (int *) PetscMalloc( (aij->stash.n+1)*sizeof(int) ); CHKPTRQ(owner);
3321eb62cbbSBarry Smith   for ( i=0; i<aij->stash.n; i++ ) {
3331eb62cbbSBarry Smith     idx = aij->stash.idx[i];
33417699dbbSLois Curfman McInnes     for ( j=0; j<size; j++ ) {
3351eb62cbbSBarry Smith       if (idx >= owners[j] && idx < owners[j+1]) {
3361eb62cbbSBarry Smith         nprocs[j]++; procs[j] = 1; owner[i] = j; break;
3378a729477SBarry Smith       }
3388a729477SBarry Smith     }
3398a729477SBarry Smith   }
34017699dbbSLois Curfman McInnes   nsends = 0;  for ( i=0; i<size; i++ ) { nsends += procs[i];}
3411eb62cbbSBarry Smith 
3421eb62cbbSBarry Smith   /* inform other processors of number of messages and max length*/
3430452661fSBarry Smith   work = (int *) PetscMalloc( size*sizeof(int) ); CHKPTRQ(work);
344ca161407SBarry Smith   ierr = MPI_Allreduce(procs, work,size,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr);
34517699dbbSLois Curfman McInnes   nreceives = work[rank];
346ca161407SBarry Smith   ierr = MPI_Allreduce( nprocs, work,size,MPI_INT,MPI_MAX,comm);CHKERRQ(ierr);
34717699dbbSLois Curfman McInnes   nmax = work[rank];
3480452661fSBarry Smith   PetscFree(work);
3491eb62cbbSBarry Smith 
3501eb62cbbSBarry Smith   /* post receives:
3511eb62cbbSBarry Smith        1) each message will consist of ordered pairs
3521eb62cbbSBarry Smith      (global index,value) we store the global index as a double
353d6dfbf8fSBarry Smith      to simplify the message passing.
3541eb62cbbSBarry Smith        2) since we don't know how long each individual message is we
3551eb62cbbSBarry Smith      allocate the largest needed buffer for each receive. Potentially
3561eb62cbbSBarry Smith      this is a lot of wasted space.
3571eb62cbbSBarry Smith 
3581eb62cbbSBarry Smith 
3591eb62cbbSBarry Smith        This could be done better.
3601eb62cbbSBarry Smith   */
361ca161407SBarry Smith   rvalues    = (Scalar *) PetscMalloc(3*(nreceives+1)*(nmax+1)*sizeof(Scalar));CHKPTRQ(rvalues);
362ca161407SBarry Smith   recv_waits = (MPI_Request *) PetscMalloc((nreceives+1)*sizeof(MPI_Request));CHKPTRQ(recv_waits);
3631eb62cbbSBarry Smith   for ( i=0; i<nreceives; i++ ) {
364ca161407SBarry Smith     ierr = MPI_Irecv(rvalues+3*nmax*i,3*nmax,MPIU_SCALAR,MPI_ANY_SOURCE,tag,
365ca161407SBarry Smith               comm,recv_waits+i);CHKERRQ(ierr);
3661eb62cbbSBarry Smith   }
3671eb62cbbSBarry Smith 
3681eb62cbbSBarry Smith   /* do sends:
3691eb62cbbSBarry Smith       1) starts[i] gives the starting index in svalues for stuff going to
3701eb62cbbSBarry Smith          the ith processor
3711eb62cbbSBarry Smith   */
3720452661fSBarry Smith   svalues    = (Scalar *) PetscMalloc(3*(aij->stash.n+1)*sizeof(Scalar));CHKPTRQ(svalues);
373ca161407SBarry Smith   send_waits = (MPI_Request *) PetscMalloc( (nsends+1)*sizeof(MPI_Request));CHKPTRQ(send_waits);
3740452661fSBarry Smith   starts     = (int *) PetscMalloc( size*sizeof(int) ); CHKPTRQ(starts);
3751eb62cbbSBarry Smith   starts[0]  = 0;
37617699dbbSLois Curfman McInnes   for ( i=1; i<size; i++ ) { starts[i] = starts[i-1] + nprocs[i-1];}
3771eb62cbbSBarry Smith   for ( i=0; i<aij->stash.n; i++ ) {
3781eb62cbbSBarry Smith     svalues[3*starts[owner[i]]]       = (Scalar)  aij->stash.idx[i];
3791eb62cbbSBarry Smith     svalues[3*starts[owner[i]]+1]     = (Scalar)  aij->stash.idy[i];
3801eb62cbbSBarry Smith     svalues[3*(starts[owner[i]]++)+2] =  aij->stash.array[i];
3811eb62cbbSBarry Smith   }
3820452661fSBarry Smith   PetscFree(owner);
3831eb62cbbSBarry Smith   starts[0] = 0;
38417699dbbSLois Curfman McInnes   for ( i=1; i<size; i++ ) { starts[i] = starts[i-1] + nprocs[i-1];}
3851eb62cbbSBarry Smith   count = 0;
38617699dbbSLois Curfman McInnes   for ( i=0; i<size; i++ ) {
3871eb62cbbSBarry Smith     if (procs[i]) {
388ca161407SBarry Smith       ierr = MPI_Isend(svalues+3*starts[i],3*nprocs[i],MPIU_SCALAR,i,tag,comm,send_waits+count++);CHKERRQ(ierr);
3891eb62cbbSBarry Smith     }
3901eb62cbbSBarry Smith   }
391b5bd3ad5SBarry Smith   PetscFree(starts);
392b5bd3ad5SBarry Smith   PetscFree(nprocs);
3931eb62cbbSBarry Smith 
3941eb62cbbSBarry Smith   /* Free cache space */
39510a665d1SBarry Smith   PLogInfo(aij->A,"MatAssemblyBegin_MPIAIJ:Number of off-processor values %d\n",aij->stash.n);
39678b31e54SBarry Smith   ierr = StashDestroy_Private(&aij->stash); CHKERRQ(ierr);
3971eb62cbbSBarry Smith 
3981eb62cbbSBarry Smith   aij->svalues    = svalues;    aij->rvalues    = rvalues;
3991eb62cbbSBarry Smith   aij->nsends     = nsends;     aij->nrecvs     = nreceives;
4001eb62cbbSBarry Smith   aij->send_waits = send_waits; aij->recv_waits = recv_waits;
4011eb62cbbSBarry Smith   aij->rmax       = nmax;
4021eb62cbbSBarry Smith 
4033a40ed3dSBarry Smith   PetscFunctionReturn(0);
4041eb62cbbSBarry Smith }
40544a69424SLois Curfman McInnes extern int MatSetUpMultiply_MPIAIJ(Mat);
4061eb62cbbSBarry Smith 
4075615d1e5SSatish Balay #undef __FUNC__
4085615d1e5SSatish Balay #define __FUNC__ "MatAssemblyEnd_MPIAIJ"
4098f6be9afSLois Curfman McInnes int MatAssemblyEnd_MPIAIJ(Mat mat,MatAssemblyType mode)
4101eb62cbbSBarry Smith {
41144a69424SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data;
4121eb62cbbSBarry Smith   MPI_Status  *send_status,recv_status;
413416022c9SBarry Smith   int         imdex,nrecvs = aij->nrecvs, count = nrecvs, i, n, ierr;
414905e6a2fSBarry Smith   int         row,col,other_disassembled;
4151eb62cbbSBarry Smith   Scalar      *values,val;
41647794344SBarry Smith   InsertMode  addv = mat->insertmode;
4171eb62cbbSBarry Smith 
4183a40ed3dSBarry Smith   PetscFunctionBegin;
419b5bd3ad5SBarry Smith 
4201eb62cbbSBarry Smith   /*  wait on receives */
4211eb62cbbSBarry Smith   while (count) {
422ca161407SBarry Smith     ierr = MPI_Waitany(nrecvs,aij->recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
4231eb62cbbSBarry Smith     /* unpack receives into our local space */
424d6dfbf8fSBarry Smith     values = aij->rvalues + 3*imdex*aij->rmax;
425ca161407SBarry Smith     ierr = MPI_Get_count(&recv_status,MPIU_SCALAR,&n);CHKERRQ(ierr);
4261eb62cbbSBarry Smith     n = n/3;
4271eb62cbbSBarry Smith     for ( i=0; i<n; i++ ) {
428227d817aSBarry Smith       row = (int) PetscReal(values[3*i]) - aij->rstart;
429227d817aSBarry Smith       col = (int) PetscReal(values[3*i+1]);
4301eb62cbbSBarry Smith       val = values[3*i+2];
4311eb62cbbSBarry Smith       if (col >= aij->cstart && col < aij->cend) {
4321eb62cbbSBarry Smith         col -= aij->cstart;
4336fd7127cSSatish Balay         ierr = MatSetValues(aij->A,1,&row,1,&col,&val,addv); CHKERRQ(ierr);
4343a40ed3dSBarry Smith       } else {
43555a1b374SBarry Smith         if (mat->was_assembled || mat->assembled) {
436905e6a2fSBarry Smith           if (!aij->colmap) {
437905e6a2fSBarry Smith             ierr = CreateColmap_MPIAIJ_Private(mat); CHKERRQ(ierr);
438905e6a2fSBarry Smith           }
439905e6a2fSBarry Smith           col = aij->colmap[col] - 1;
440ec8511deSBarry Smith           if (col < 0  && !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
4412493cbb0SBarry Smith             ierr = DisAssemble_MPIAIJ(mat); CHKERRQ(ierr);
442227d817aSBarry Smith             col = (int) PetscReal(values[3*i+1]);
443d6dfbf8fSBarry Smith           }
4449e25ed09SBarry Smith         }
4456fd7127cSSatish Balay         ierr = MatSetValues(aij->B,1,&row,1,&col,&val,addv); CHKERRQ(ierr);
4461eb62cbbSBarry Smith       }
4471eb62cbbSBarry Smith     }
4481eb62cbbSBarry Smith     count--;
4491eb62cbbSBarry Smith   }
450570da906SBarry Smith   if (aij->recv_waits) PetscFree(aij->recv_waits);
451570da906SBarry Smith   if (aij->rvalues)    PetscFree(aij->rvalues);
4521eb62cbbSBarry Smith 
4531eb62cbbSBarry Smith   /* wait on sends */
4541eb62cbbSBarry Smith   if (aij->nsends) {
4550a198c4cSBarry Smith     send_status = (MPI_Status *) PetscMalloc(aij->nsends*sizeof(MPI_Status));CHKPTRQ(send_status);
456ca161407SBarry Smith     ierr        = MPI_Waitall(aij->nsends,aij->send_waits,send_status);CHKERRQ(ierr);
4570452661fSBarry Smith     PetscFree(send_status);
4581eb62cbbSBarry Smith   }
459b5bd3ad5SBarry Smith   if (aij->send_waits) PetscFree(aij->send_waits);
460b5bd3ad5SBarry Smith   if (aij->svalues)    PetscFree(aij->svalues);
4611eb62cbbSBarry Smith 
46278b31e54SBarry Smith   ierr = MatAssemblyBegin(aij->A,mode); CHKERRQ(ierr);
46378b31e54SBarry Smith   ierr = MatAssemblyEnd(aij->A,mode); CHKERRQ(ierr);
4641eb62cbbSBarry Smith 
4652493cbb0SBarry Smith   /* determine if any processor has disassembled, if so we must
4662493cbb0SBarry Smith      also disassemble ourselfs, in order that we may reassemble. */
46741e46ba1SBarry Smith   /*
46841e46ba1SBarry Smith      if nonzero structure of submatrix B cannot change then we know that
46941e46ba1SBarry Smith      no processor disassembled thus we can skip this stuff
47041e46ba1SBarry Smith   */
47141e46ba1SBarry Smith   if (!((Mat_SeqAIJ*) aij->B->data)->nonew)  {
472ca161407SBarry Smith     ierr = MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPI_INT,MPI_PROD,mat->comm);CHKERRQ(ierr);
473227d817aSBarry Smith     if (mat->was_assembled && !other_disassembled) {
4742493cbb0SBarry Smith       ierr = DisAssemble_MPIAIJ(mat); CHKERRQ(ierr);
4752493cbb0SBarry Smith     }
47641e46ba1SBarry Smith   }
4772493cbb0SBarry Smith 
4786d4a8577SBarry Smith   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
47978b31e54SBarry Smith     ierr = MatSetUpMultiply_MPIAIJ(mat); CHKERRQ(ierr);
4805e42470aSBarry Smith   }
48178b31e54SBarry Smith   ierr = MatAssemblyBegin(aij->B,mode); CHKERRQ(ierr);
48278b31e54SBarry Smith   ierr = MatAssemblyEnd(aij->B,mode); CHKERRQ(ierr);
4835e42470aSBarry Smith 
4847a0afa10SBarry Smith   if (aij->rowvalues) {PetscFree(aij->rowvalues); aij->rowvalues = 0;}
4853a40ed3dSBarry Smith   PetscFunctionReturn(0);
4868a729477SBarry Smith }
4878a729477SBarry Smith 
4885615d1e5SSatish Balay #undef __FUNC__
4895615d1e5SSatish Balay #define __FUNC__ "MatZeroEntries_MPIAIJ"
4908f6be9afSLois Curfman McInnes int MatZeroEntries_MPIAIJ(Mat A)
4911eb62cbbSBarry Smith {
49244a69424SLois Curfman McInnes   Mat_MPIAIJ *l = (Mat_MPIAIJ *) A->data;
493dbd7a890SLois Curfman McInnes   int        ierr;
4943a40ed3dSBarry Smith 
4953a40ed3dSBarry Smith   PetscFunctionBegin;
49678b31e54SBarry Smith   ierr = MatZeroEntries(l->A); CHKERRQ(ierr);
49778b31e54SBarry Smith   ierr = MatZeroEntries(l->B); CHKERRQ(ierr);
4983a40ed3dSBarry Smith   PetscFunctionReturn(0);
4991eb62cbbSBarry Smith }
5001eb62cbbSBarry Smith 
5015615d1e5SSatish Balay #undef __FUNC__
5025615d1e5SSatish Balay #define __FUNC__ "MatZeroRows_MPIAIJ"
5038f6be9afSLois Curfman McInnes int MatZeroRows_MPIAIJ(Mat A,IS is,Scalar *diag)
5041eb62cbbSBarry Smith {
50544a69424SLois Curfman McInnes   Mat_MPIAIJ     *l = (Mat_MPIAIJ *) A->data;
50617699dbbSLois Curfman McInnes   int            i,ierr,N, *rows,*owners = l->rowners,size = l->size;
5076a5c57faSSatish Balay   int            *procs,*nprocs,j,found,idx,nsends,*work,row;
50817699dbbSLois Curfman McInnes   int            nmax,*svalues,*starts,*owner,nrecvs,rank = l->rank;
5095392566eSBarry Smith   int            *rvalues,tag = A->tag,count,base,slen,n,*source;
5106a5c57faSSatish Balay   int            *lens,imdex,*lrows,*values,rstart=l->rstart;
511d6dfbf8fSBarry Smith   MPI_Comm       comm = A->comm;
5121eb62cbbSBarry Smith   MPI_Request    *send_waits,*recv_waits;
5131eb62cbbSBarry Smith   MPI_Status     recv_status,*send_status;
5141eb62cbbSBarry Smith   IS             istmp;
5156eb55b6aSBarry Smith   PetscTruth     localdiag;
5161eb62cbbSBarry Smith 
5173a40ed3dSBarry Smith   PetscFunctionBegin;
51877c4ece6SBarry Smith   ierr = ISGetSize(is,&N); CHKERRQ(ierr);
51978b31e54SBarry Smith   ierr = ISGetIndices(is,&rows); CHKERRQ(ierr);
5201eb62cbbSBarry Smith 
5211eb62cbbSBarry Smith   /*  first count number of contributors to each processor */
5220452661fSBarry Smith   nprocs = (int *) PetscMalloc( 2*size*sizeof(int) ); CHKPTRQ(nprocs);
523cddf8d76SBarry Smith   PetscMemzero(nprocs,2*size*sizeof(int)); procs = nprocs + size;
5240452661fSBarry Smith   owner = (int *) PetscMalloc((N+1)*sizeof(int)); CHKPTRQ(owner); /* see note*/
5251eb62cbbSBarry Smith   for ( i=0; i<N; i++ ) {
5261eb62cbbSBarry Smith     idx = rows[i];
5271eb62cbbSBarry Smith     found = 0;
52817699dbbSLois Curfman McInnes     for ( j=0; j<size; j++ ) {
5291eb62cbbSBarry Smith       if (idx >= owners[j] && idx < owners[j+1]) {
5301eb62cbbSBarry Smith         nprocs[j]++; procs[j] = 1; owner[i] = j; found = 1; break;
5311eb62cbbSBarry Smith       }
5321eb62cbbSBarry Smith     }
533a8c6a408SBarry Smith     if (!found) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Index out of range");
5341eb62cbbSBarry Smith   }
53517699dbbSLois Curfman McInnes   nsends = 0;  for ( i=0; i<size; i++ ) { nsends += procs[i];}
5361eb62cbbSBarry Smith 
5371eb62cbbSBarry Smith   /* inform other processors of number of messages and max length*/
5380452661fSBarry Smith   work = (int *) PetscMalloc( size*sizeof(int) ); CHKPTRQ(work);
539ca161407SBarry Smith   ierr = MPI_Allreduce( procs, work,size,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr);
54017699dbbSLois Curfman McInnes   nrecvs = work[rank];
541ca161407SBarry Smith   ierr = MPI_Allreduce( nprocs, work,size,MPI_INT,MPI_MAX,comm);CHKERRQ(ierr);
54217699dbbSLois Curfman McInnes   nmax = work[rank];
5430452661fSBarry Smith   PetscFree(work);
5441eb62cbbSBarry Smith 
5451eb62cbbSBarry Smith   /* post receives:   */
5463a40ed3dSBarry Smith   rvalues = (int *) PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(int));CHKPTRQ(rvalues);
547ca161407SBarry Smith   recv_waits = (MPI_Request *) PetscMalloc((nrecvs+1)*sizeof(MPI_Request));CHKPTRQ(recv_waits);
5481eb62cbbSBarry Smith   for ( i=0; i<nrecvs; i++ ) {
549ca161407SBarry Smith     ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPI_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr);
5501eb62cbbSBarry Smith   }
5511eb62cbbSBarry Smith 
5521eb62cbbSBarry Smith   /* do sends:
5531eb62cbbSBarry Smith       1) starts[i] gives the starting index in svalues for stuff going to
5541eb62cbbSBarry Smith          the ith processor
5551eb62cbbSBarry Smith   */
5560452661fSBarry Smith   svalues = (int *) PetscMalloc( (N+1)*sizeof(int) ); CHKPTRQ(svalues);
5573a40ed3dSBarry Smith   send_waits = (MPI_Request *) PetscMalloc( (nsends+1)*sizeof(MPI_Request));CHKPTRQ(send_waits);
5580452661fSBarry Smith   starts = (int *) PetscMalloc( (size+1)*sizeof(int) ); CHKPTRQ(starts);
5591eb62cbbSBarry Smith   starts[0] = 0;
56017699dbbSLois Curfman McInnes   for ( i=1; i<size; i++ ) { starts[i] = starts[i-1] + nprocs[i-1];}
5611eb62cbbSBarry Smith   for ( i=0; i<N; i++ ) {
5621eb62cbbSBarry Smith     svalues[starts[owner[i]]++] = rows[i];
5631eb62cbbSBarry Smith   }
5641eb62cbbSBarry Smith   ISRestoreIndices(is,&rows);
5651eb62cbbSBarry Smith 
5661eb62cbbSBarry Smith   starts[0] = 0;
56717699dbbSLois Curfman McInnes   for ( i=1; i<size+1; i++ ) { starts[i] = starts[i-1] + nprocs[i-1];}
5681eb62cbbSBarry Smith   count = 0;
56917699dbbSLois Curfman McInnes   for ( i=0; i<size; i++ ) {
5701eb62cbbSBarry Smith     if (procs[i]) {
571ca161407SBarry Smith       ierr = MPI_Isend(svalues+starts[i],nprocs[i],MPI_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr);
5721eb62cbbSBarry Smith     }
5731eb62cbbSBarry Smith   }
5740452661fSBarry Smith   PetscFree(starts);
5751eb62cbbSBarry Smith 
57617699dbbSLois Curfman McInnes   base = owners[rank];
5771eb62cbbSBarry Smith 
5781eb62cbbSBarry Smith   /*  wait on receives */
5790452661fSBarry Smith   lens   = (int *) PetscMalloc( 2*(nrecvs+1)*sizeof(int) ); CHKPTRQ(lens);
5801eb62cbbSBarry Smith   source = lens + nrecvs;
5811eb62cbbSBarry Smith   count  = nrecvs; slen = 0;
5821eb62cbbSBarry Smith   while (count) {
583ca161407SBarry Smith     ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
5841eb62cbbSBarry Smith     /* unpack receives into our local space */
585ca161407SBarry Smith     ierr = MPI_Get_count(&recv_status,MPI_INT,&n);CHKERRQ(ierr);
586d6dfbf8fSBarry Smith     source[imdex]  = recv_status.MPI_SOURCE;
587d6dfbf8fSBarry Smith     lens[imdex]  = n;
5881eb62cbbSBarry Smith     slen += n;
5891eb62cbbSBarry Smith     count--;
5901eb62cbbSBarry Smith   }
5910452661fSBarry Smith   PetscFree(recv_waits);
5921eb62cbbSBarry Smith 
5931eb62cbbSBarry Smith   /* move the data into the send scatter */
5940452661fSBarry Smith   lrows = (int *) PetscMalloc( (slen+1)*sizeof(int) ); CHKPTRQ(lrows);
5951eb62cbbSBarry Smith   count = 0;
5961eb62cbbSBarry Smith   for ( i=0; i<nrecvs; i++ ) {
5971eb62cbbSBarry Smith     values = rvalues + i*nmax;
5981eb62cbbSBarry Smith     for ( j=0; j<lens[i]; j++ ) {
5991eb62cbbSBarry Smith       lrows[count++] = values[j] - base;
6001eb62cbbSBarry Smith     }
6011eb62cbbSBarry Smith   }
6020452661fSBarry Smith   PetscFree(rvalues); PetscFree(lens);
6030452661fSBarry Smith   PetscFree(owner); PetscFree(nprocs);
6041eb62cbbSBarry Smith 
6051eb62cbbSBarry Smith   /* actually zap the local rows */
606029af93fSBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,slen,lrows,&istmp);CHKERRQ(ierr);
607464493b3SBarry Smith   PLogObjectParent(A,istmp);
6086a5c57faSSatish Balay 
6096eb55b6aSBarry Smith   /*
6106eb55b6aSBarry Smith         Zero the required rows. If the "diagonal block" of the matrix
6116eb55b6aSBarry Smith      is square and the user wishes to set the diagonal we use seperate
6126eb55b6aSBarry Smith      code so that MatSetValues() is not called for each diagonal allocating
6136eb55b6aSBarry Smith      new memory, thus calling lots of mallocs and slowing things down.
6146eb55b6aSBarry Smith 
6156eb55b6aSBarry Smith        Contributed by: Mathew Knepley
6166eb55b6aSBarry Smith   */
6176eb55b6aSBarry Smith   localdiag = PETSC_FALSE;
6186eb55b6aSBarry Smith   if (diag && (l->A->M == l->A->N)) {
6196eb55b6aSBarry Smith     localdiag = PETSC_TRUE;
6206eb55b6aSBarry Smith     ierr      = MatZeroRows(l->A,istmp,diag); CHKERRQ(ierr);
6216eb55b6aSBarry Smith   } else {
6226a5c57faSSatish Balay     ierr = MatZeroRows(l->A,istmp,0); CHKERRQ(ierr);
6236eb55b6aSBarry Smith   }
62478b31e54SBarry Smith   ierr = MatZeroRows(l->B,istmp,0); CHKERRQ(ierr);
62578b31e54SBarry Smith   ierr = ISDestroy(istmp); CHKERRQ(ierr);
6261eb62cbbSBarry Smith 
6276eb55b6aSBarry Smith   if (diag && (localdiag == PETSC_FALSE)) {
6286eb55b6aSBarry Smith     for ( i = 0; i < slen; i++ ) {
6296eb55b6aSBarry Smith       row = lrows[i] + rstart;
6306eb55b6aSBarry Smith       MatSetValues(A,1,&row,1,&row,diag,INSERT_VALUES);
6316eb55b6aSBarry Smith     }
6326eb55b6aSBarry Smith     MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
6336eb55b6aSBarry Smith     MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
6346eb55b6aSBarry Smith   }
6356eb55b6aSBarry Smith 
6366a5c57faSSatish Balay   if (diag) {
6376a5c57faSSatish Balay     for ( i = 0; i < slen; i++ ) {
6386a5c57faSSatish Balay       row = lrows[i] + rstart;
6396a5c57faSSatish Balay       MatSetValues(A,1,&row,1,&row,diag,INSERT_VALUES);
6406a5c57faSSatish Balay     }
6416a5c57faSSatish Balay     MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
6426a5c57faSSatish Balay     MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
6436a5c57faSSatish Balay   }
6446a5c57faSSatish Balay   PetscFree(lrows);
64572dacd9aSBarry Smith 
6461eb62cbbSBarry Smith   /* wait on sends */
6471eb62cbbSBarry Smith   if (nsends) {
648ca161407SBarry Smith     send_status = (MPI_Status *) PetscMalloc(nsends*sizeof(MPI_Status));CHKPTRQ(send_status);
649ca161407SBarry Smith     ierr        = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr);
6500452661fSBarry Smith     PetscFree(send_status);
6511eb62cbbSBarry Smith   }
6520452661fSBarry Smith   PetscFree(send_waits); PetscFree(svalues);
6531eb62cbbSBarry Smith 
6543a40ed3dSBarry Smith   PetscFunctionReturn(0);
6551eb62cbbSBarry Smith }
6561eb62cbbSBarry Smith 
6575615d1e5SSatish Balay #undef __FUNC__
6585615d1e5SSatish Balay #define __FUNC__ "MatMult_MPIAIJ"
6598f6be9afSLois Curfman McInnes int MatMult_MPIAIJ(Mat A,Vec xx,Vec yy)
6601eb62cbbSBarry Smith {
661416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data;
662fbd6ef76SBarry Smith   int        ierr,nt;
663416022c9SBarry Smith 
6643a40ed3dSBarry Smith   PetscFunctionBegin;
665a2ce50c7SBarry Smith   ierr = VecGetLocalSize(xx,&nt);  CHKERRQ(ierr);
666fbd6ef76SBarry Smith   if (nt != a->n) {
667a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_SIZ,0,"Incompatible partition of A and xx");
668fbd6ef76SBarry Smith   }
66943a90d84SBarry Smith   ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx); CHKERRQ(ierr);
670f830108cSBarry Smith   ierr = (*a->A->ops->mult)(a->A,xx,yy); CHKERRQ(ierr);
67143a90d84SBarry Smith   ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx); CHKERRQ(ierr);
672f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy); CHKERRQ(ierr);
6733a40ed3dSBarry Smith   PetscFunctionReturn(0);
6741eb62cbbSBarry Smith }
6751eb62cbbSBarry Smith 
6765615d1e5SSatish Balay #undef __FUNC__
6775615d1e5SSatish Balay #define __FUNC__ "MatMultAdd_MPIAIJ"
6788f6be9afSLois Curfman McInnes int MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
679da3a660dSBarry Smith {
680416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data;
681da3a660dSBarry Smith   int        ierr;
6823a40ed3dSBarry Smith 
6833a40ed3dSBarry Smith   PetscFunctionBegin;
68443a90d84SBarry Smith   ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
685f830108cSBarry Smith   ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz); CHKERRQ(ierr);
68643a90d84SBarry Smith   ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
687f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz); CHKERRQ(ierr);
6883a40ed3dSBarry Smith   PetscFunctionReturn(0);
689da3a660dSBarry Smith }
690da3a660dSBarry Smith 
6915615d1e5SSatish Balay #undef __FUNC__
6925615d1e5SSatish Balay #define __FUNC__ "MatMultTrans_MPIAIJ"
6938f6be9afSLois Curfman McInnes int MatMultTrans_MPIAIJ(Mat A,Vec xx,Vec yy)
694da3a660dSBarry Smith {
695416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data;
696da3a660dSBarry Smith   int        ierr;
697da3a660dSBarry Smith 
6983a40ed3dSBarry Smith   PetscFunctionBegin;
699da3a660dSBarry Smith   /* do nondiagonal part */
700f830108cSBarry Smith   ierr = (*a->B->ops->multtrans)(a->B,xx,a->lvec); CHKERRQ(ierr);
701da3a660dSBarry Smith   /* send it on its way */
702537820f0SBarry Smith   ierr = VecScatterBegin(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx); CHKERRQ(ierr);
703da3a660dSBarry Smith   /* do local part */
704f830108cSBarry Smith   ierr = (*a->A->ops->multtrans)(a->A,xx,yy); CHKERRQ(ierr);
705da3a660dSBarry Smith   /* receive remote parts: note this assumes the values are not actually */
706da3a660dSBarry Smith   /* inserted in yy until the next line, which is true for my implementation*/
707da3a660dSBarry Smith   /* but is not perhaps always true. */
708537820f0SBarry Smith   ierr = VecScatterEnd(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx); CHKERRQ(ierr);
7093a40ed3dSBarry Smith   PetscFunctionReturn(0);
710da3a660dSBarry Smith }
711da3a660dSBarry Smith 
7125615d1e5SSatish Balay #undef __FUNC__
7135615d1e5SSatish Balay #define __FUNC__ "MatMultTransAdd_MPIAIJ"
7148f6be9afSLois Curfman McInnes int MatMultTransAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
715da3a660dSBarry Smith {
716416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data;
717da3a660dSBarry Smith   int        ierr;
718da3a660dSBarry Smith 
7193a40ed3dSBarry Smith   PetscFunctionBegin;
720da3a660dSBarry Smith   /* do nondiagonal part */
721f830108cSBarry Smith   ierr = (*a->B->ops->multtrans)(a->B,xx,a->lvec); CHKERRQ(ierr);
722da3a660dSBarry Smith   /* send it on its way */
723537820f0SBarry Smith   ierr = VecScatterBegin(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx); CHKERRQ(ierr);
724da3a660dSBarry Smith   /* do local part */
725f830108cSBarry Smith   ierr = (*a->A->ops->multtransadd)(a->A,xx,yy,zz); CHKERRQ(ierr);
726da3a660dSBarry Smith   /* receive remote parts: note this assumes the values are not actually */
727da3a660dSBarry Smith   /* inserted in yy until the next line, which is true for my implementation*/
728da3a660dSBarry Smith   /* but is not perhaps always true. */
7290a198c4cSBarry Smith   ierr = VecScatterEnd(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx); CHKERRQ(ierr);
7303a40ed3dSBarry Smith   PetscFunctionReturn(0);
731da3a660dSBarry Smith }
732da3a660dSBarry Smith 
7331eb62cbbSBarry Smith /*
7341eb62cbbSBarry Smith   This only works correctly for square matrices where the subblock A->A is the
7351eb62cbbSBarry Smith    diagonal block
7361eb62cbbSBarry Smith */
7375615d1e5SSatish Balay #undef __FUNC__
7385615d1e5SSatish Balay #define __FUNC__ "MatGetDiagonal_MPIAIJ"
7398f6be9afSLois Curfman McInnes int MatGetDiagonal_MPIAIJ(Mat A,Vec v)
7401eb62cbbSBarry Smith {
7413a40ed3dSBarry Smith   int        ierr;
742416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data;
7433a40ed3dSBarry Smith 
7443a40ed3dSBarry Smith   PetscFunctionBegin;
745a8c6a408SBarry Smith   if (a->M != a->N) SETERRQ(PETSC_ERR_SUP,0,"Supports only square matrix where A->A is diag block");
7465baf8537SBarry Smith   if (a->rstart != a->cstart || a->rend != a->cend) {
747a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_SIZ,0,"row partition must equal col partition");
7483a40ed3dSBarry Smith   }
7493a40ed3dSBarry Smith   ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr);
7503a40ed3dSBarry Smith   PetscFunctionReturn(0);
7511eb62cbbSBarry Smith }
7521eb62cbbSBarry Smith 
7535615d1e5SSatish Balay #undef __FUNC__
7545615d1e5SSatish Balay #define __FUNC__ "MatScale_MPIAIJ"
7558f6be9afSLois Curfman McInnes int MatScale_MPIAIJ(Scalar *aa,Mat A)
756052efed2SBarry Smith {
757052efed2SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data;
758052efed2SBarry Smith   int        ierr;
7593a40ed3dSBarry Smith 
7603a40ed3dSBarry Smith   PetscFunctionBegin;
761052efed2SBarry Smith   ierr = MatScale(aa,a->A); CHKERRQ(ierr);
762052efed2SBarry Smith   ierr = MatScale(aa,a->B); CHKERRQ(ierr);
7633a40ed3dSBarry Smith   PetscFunctionReturn(0);
764052efed2SBarry Smith }
765052efed2SBarry Smith 
7665615d1e5SSatish Balay #undef __FUNC__
767d4bb536fSBarry Smith #define __FUNC__ "MatDestroy_MPIAIJ"
768e1311b90SBarry Smith int MatDestroy_MPIAIJ(Mat mat)
7691eb62cbbSBarry Smith {
77044a69424SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data;
7711eb62cbbSBarry Smith   int        ierr;
77283e2fdc7SBarry Smith 
7733a40ed3dSBarry Smith   PetscFunctionBegin;
77470429bc8SBarry Smith   if (--mat->refct > 0) PetscFunctionReturn(0);
77570429bc8SBarry Smith 
77670429bc8SBarry Smith   if (mat->mapping) {
77770429bc8SBarry Smith     ierr = ISLocalToGlobalMappingDestroy(mat->mapping); CHKERRQ(ierr);
77870429bc8SBarry Smith   }
77970429bc8SBarry Smith   if (mat->bmapping) {
78070429bc8SBarry Smith     ierr = ISLocalToGlobalMappingDestroy(mat->bmapping); CHKERRQ(ierr);
78170429bc8SBarry Smith   }
78261b13de0SBarry Smith   if (mat->rmap) {
78361b13de0SBarry Smith     ierr = MapDestroy(mat->rmap);CHKERRQ(ierr);
78461b13de0SBarry Smith   }
78561b13de0SBarry Smith   if (mat->cmap) {
78661b13de0SBarry Smith     ierr = MapDestroy(mat->cmap);CHKERRQ(ierr);
78761b13de0SBarry Smith   }
7883a40ed3dSBarry Smith #if defined(USE_PETSC_LOG)
789e1311b90SBarry Smith   PLogObjectState((PetscObject)mat,"Rows=%d, Cols=%d",aij->M,aij->N);
790a5a9c739SBarry Smith #endif
79183e2fdc7SBarry Smith   ierr = StashDestroy_Private(&aij->stash); CHKERRQ(ierr);
7920452661fSBarry Smith   PetscFree(aij->rowners);
79378b31e54SBarry Smith   ierr = MatDestroy(aij->A); CHKERRQ(ierr);
79478b31e54SBarry Smith   ierr = MatDestroy(aij->B); CHKERRQ(ierr);
7950452661fSBarry Smith   if (aij->colmap) PetscFree(aij->colmap);
7960452661fSBarry Smith   if (aij->garray) PetscFree(aij->garray);
7971eb62cbbSBarry Smith   if (aij->lvec)   VecDestroy(aij->lvec);
798a56f8943SBarry Smith   if (aij->Mvctx)  VecScatterDestroy(aij->Mvctx);
7997a0afa10SBarry Smith   if (aij->rowvalues) PetscFree(aij->rowvalues);
8000452661fSBarry Smith   PetscFree(aij);
801a5a9c739SBarry Smith   PLogObjectDestroy(mat);
8020452661fSBarry Smith   PetscHeaderDestroy(mat);
8033a40ed3dSBarry Smith   PetscFunctionReturn(0);
8041eb62cbbSBarry Smith }
805ee50ffe9SBarry Smith 
8065615d1e5SSatish Balay #undef __FUNC__
807d4bb536fSBarry Smith #define __FUNC__ "MatView_MPIAIJ_Binary"
8088f6be9afSLois Curfman McInnes extern int MatView_MPIAIJ_Binary(Mat mat,Viewer viewer)
8091eb62cbbSBarry Smith {
810416022c9SBarry Smith   Mat_MPIAIJ  *aij = (Mat_MPIAIJ *) mat->data;
811416022c9SBarry Smith   int         ierr;
812416022c9SBarry Smith 
8133a40ed3dSBarry Smith   PetscFunctionBegin;
81417699dbbSLois Curfman McInnes   if (aij->size == 1) {
815416022c9SBarry Smith     ierr = MatView(aij->A,viewer); CHKERRQ(ierr);
816416022c9SBarry Smith   }
817a8c6a408SBarry Smith   else SETERRQ(PETSC_ERR_SUP,0,"Only uniprocessor output supported");
8183a40ed3dSBarry Smith   PetscFunctionReturn(0);
819416022c9SBarry Smith }
820416022c9SBarry Smith 
8215615d1e5SSatish Balay #undef __FUNC__
822d4bb536fSBarry Smith #define __FUNC__ "MatView_MPIAIJ_ASCIIorDraworMatlab"
8238f6be9afSLois Curfman McInnes extern int MatView_MPIAIJ_ASCIIorDraworMatlab(Mat mat,Viewer viewer)
824416022c9SBarry Smith {
82544a69424SLois Curfman McInnes   Mat_MPIAIJ  *aij = (Mat_MPIAIJ *) mat->data;
826dbb450caSBarry Smith   Mat_SeqAIJ* C = (Mat_SeqAIJ*)aij->A->data;
827a56f8943SBarry Smith   int         ierr, format,shift = C->indexshift,rank;
828d636dbe3SBarry Smith   FILE        *fd;
82919bcc07fSBarry Smith   ViewerType  vtype;
830416022c9SBarry Smith 
8313a40ed3dSBarry Smith   PetscFunctionBegin;
83219bcc07fSBarry Smith   ierr = ViewerGetType(viewer,&vtype); CHKERRQ(ierr);
83319bcc07fSBarry Smith   if (vtype  == ASCII_FILES_VIEWER || vtype == ASCII_FILE_VIEWER) {
83490ace30eSBarry Smith     ierr = ViewerGetFormat(viewer,&format);
8350a198c4cSBarry Smith     if (format == VIEWER_FORMAT_ASCII_INFO_LONG) {
8364e220ebcSLois Curfman McInnes       MatInfo info;
8374e220ebcSLois Curfman McInnes       int     flg;
838a56f8943SBarry Smith       MPI_Comm_rank(mat->comm,&rank);
83990ace30eSBarry Smith       ierr = ViewerASCIIGetPointer(viewer,&fd); CHKERRQ(ierr);
8404e220ebcSLois Curfman McInnes       ierr = MatGetInfo(mat,MAT_LOCAL,&info);
84195e01e2fSLois Curfman McInnes       ierr = OptionsHasName(PETSC_NULL,"-mat_aij_no_inode",&flg); CHKERRQ(ierr);
84277c4ece6SBarry Smith       PetscSequentialPhaseBegin(mat->comm,1);
84395e01e2fSLois Curfman McInnes       if (flg) fprintf(fd,"[%d] Local rows %d nz %d nz alloced %d mem %d, not using I-node routines\n",
8444e220ebcSLois Curfman McInnes          rank,aij->m,(int)info.nz_used,(int)info.nz_allocated,(int)info.memory);
84595e01e2fSLois Curfman McInnes       else fprintf(fd,"[%d] Local rows %d nz %d nz alloced %d mem %d, using I-node routines\n",
8464e220ebcSLois Curfman McInnes          rank,aij->m,(int)info.nz_used,(int)info.nz_allocated,(int)info.memory);
8474e220ebcSLois Curfman McInnes       ierr = MatGetInfo(aij->A,MAT_LOCAL,&info);
8484e220ebcSLois Curfman McInnes       fprintf(fd,"[%d] on-diagonal part: nz %d \n",rank,(int)info.nz_used);
8494e220ebcSLois Curfman McInnes       ierr = MatGetInfo(aij->B,MAT_LOCAL,&info);
8504e220ebcSLois Curfman McInnes       fprintf(fd,"[%d] off-diagonal part: nz %d \n",rank,(int)info.nz_used);
851a56f8943SBarry Smith       fflush(fd);
85277c4ece6SBarry Smith       PetscSequentialPhaseEnd(mat->comm,1);
853a40aa06bSLois Curfman McInnes       ierr = VecScatterView(aij->Mvctx,viewer); CHKERRQ(ierr);
8543a40ed3dSBarry Smith       PetscFunctionReturn(0);
8553a40ed3dSBarry Smith     } else if (format == VIEWER_FORMAT_ASCII_INFO) {
8563a40ed3dSBarry Smith       PetscFunctionReturn(0);
85708480c60SBarry Smith     }
858416022c9SBarry Smith   }
859416022c9SBarry Smith 
86019bcc07fSBarry Smith   if (vtype == DRAW_VIEWER) {
86119bcc07fSBarry Smith     Draw       draw;
86219bcc07fSBarry Smith     PetscTruth isnull;
86319bcc07fSBarry Smith     ierr = ViewerDrawGetDraw(viewer,&draw); CHKERRQ(ierr);
8643a40ed3dSBarry Smith     ierr = DrawIsNull(draw,&isnull); CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
86519bcc07fSBarry Smith   }
86619bcc07fSBarry Smith 
86719bcc07fSBarry Smith   if (vtype == ASCII_FILE_VIEWER) {
86890ace30eSBarry Smith     ierr = ViewerASCIIGetPointer(viewer,&fd); CHKERRQ(ierr);
86977c4ece6SBarry Smith     PetscSequentialPhaseBegin(mat->comm,1);
870d13ab20cSBarry Smith     fprintf(fd,"[%d] rows %d starts %d ends %d cols %d starts %d ends %d\n",
87117699dbbSLois Curfman McInnes            aij->rank,aij->m,aij->rstart,aij->rend,aij->n,aij->cstart,
8721eb62cbbSBarry Smith            aij->cend);
87378b31e54SBarry Smith     ierr = MatView(aij->A,viewer); CHKERRQ(ierr);
87478b31e54SBarry Smith     ierr = MatView(aij->B,viewer); CHKERRQ(ierr);
875d13ab20cSBarry Smith     fflush(fd);
87677c4ece6SBarry Smith     PetscSequentialPhaseEnd(mat->comm,1);
8773a40ed3dSBarry Smith   } else {
878a56f8943SBarry Smith     int size = aij->size;
879a56f8943SBarry Smith     rank = aij->rank;
88017699dbbSLois Curfman McInnes     if (size == 1) {
88178b31e54SBarry Smith       ierr = MatView(aij->A,viewer); CHKERRQ(ierr);
8823a40ed3dSBarry Smith     } else {
88395373324SBarry Smith       /* assemble the entire matrix onto first processor. */
88495373324SBarry Smith       Mat         A;
885ec8511deSBarry Smith       Mat_SeqAIJ *Aloc;
8862eb8c8abSBarry Smith       int         M = aij->M, N = aij->N,m,*ai,*aj,row,*cols,i,*ct;
88795373324SBarry Smith       Scalar      *a;
8882ee70a88SLois Curfman McInnes 
88917699dbbSLois Curfman McInnes       if (!rank) {
89055843e3eSBarry Smith         ierr = MatCreateMPIAIJ(mat->comm,M,N,M,N,0,PETSC_NULL,0,PETSC_NULL,&A);CHKERRQ(ierr);
8913a40ed3dSBarry Smith       } else {
89255843e3eSBarry Smith         ierr = MatCreateMPIAIJ(mat->comm,0,0,M,N,0,PETSC_NULL,0,PETSC_NULL,&A);CHKERRQ(ierr);
89395373324SBarry Smith       }
894464493b3SBarry Smith       PLogObjectParent(mat,A);
895416022c9SBarry Smith 
89695373324SBarry Smith       /* copy over the A part */
897ec8511deSBarry Smith       Aloc = (Mat_SeqAIJ*) aij->A->data;
8982ee70a88SLois Curfman McInnes       m = Aloc->m; ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
89995373324SBarry Smith       row = aij->rstart;
900dbb450caSBarry Smith       for ( i=0; i<ai[m]+shift; i++ ) {aj[i] += aij->cstart + shift;}
90195373324SBarry Smith       for ( i=0; i<m; i++ ) {
902416022c9SBarry Smith         ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);CHKERRQ(ierr);
90395373324SBarry Smith         row++; a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
90495373324SBarry Smith       }
9052ee70a88SLois Curfman McInnes       aj = Aloc->j;
906dbb450caSBarry Smith       for ( i=0; i<ai[m]+shift; i++ ) {aj[i] -= aij->cstart + shift;}
90795373324SBarry Smith 
90895373324SBarry Smith       /* copy over the B part */
909ec8511deSBarry Smith       Aloc = (Mat_SeqAIJ*) aij->B->data;
9102ee70a88SLois Curfman McInnes       m = Aloc->m;  ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
91195373324SBarry Smith       row = aij->rstart;
9120452661fSBarry Smith       ct = cols = (int *) PetscMalloc( (ai[m]+1)*sizeof(int) ); CHKPTRQ(cols);
913dbb450caSBarry Smith       for ( i=0; i<ai[m]+shift; i++ ) {cols[i] = aij->garray[aj[i]+shift];}
91495373324SBarry Smith       for ( i=0; i<m; i++ ) {
915416022c9SBarry Smith         ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);CHKERRQ(ierr);
91695373324SBarry Smith         row++; a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
91795373324SBarry Smith       }
9180452661fSBarry Smith       PetscFree(ct);
9196d4a8577SBarry Smith       ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
9206d4a8577SBarry Smith       ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
92155843e3eSBarry Smith       /*
92255843e3eSBarry Smith          Everyone has to call to draw the matrix since the graphics waits are
92355843e3eSBarry Smith          synchronized across all processors that share the Draw object
92455843e3eSBarry Smith       */
92555843e3eSBarry Smith       if (!rank || vtype == DRAW_VIEWER) {
92678b31e54SBarry Smith         ierr = MatView(((Mat_MPIAIJ*)(A->data))->A,viewer); CHKERRQ(ierr);
92795373324SBarry Smith       }
92878b31e54SBarry Smith       ierr = MatDestroy(A); CHKERRQ(ierr);
92995373324SBarry Smith     }
93095373324SBarry Smith   }
9313a40ed3dSBarry Smith   PetscFunctionReturn(0);
9321eb62cbbSBarry Smith }
9331eb62cbbSBarry Smith 
9345615d1e5SSatish Balay #undef __FUNC__
935d4bb536fSBarry Smith #define __FUNC__ "MatView_MPIAIJ"
936e1311b90SBarry Smith int MatView_MPIAIJ(Mat mat,Viewer viewer)
937416022c9SBarry Smith {
938416022c9SBarry Smith   int         ierr;
93919bcc07fSBarry Smith   ViewerType  vtype;
940416022c9SBarry Smith 
9413a40ed3dSBarry Smith   PetscFunctionBegin;
94219bcc07fSBarry Smith   ierr = ViewerGetType(viewer,&vtype); CHKERRQ(ierr);
94319bcc07fSBarry Smith   if (vtype == ASCII_FILE_VIEWER || vtype == ASCII_FILES_VIEWER ||
94419bcc07fSBarry Smith       vtype == DRAW_VIEWER       || vtype == MATLAB_VIEWER) {
945d7e8b826SBarry Smith     ierr = MatView_MPIAIJ_ASCIIorDraworMatlab(mat,viewer); CHKERRQ(ierr);
9465cd90555SBarry Smith   } else if (vtype == BINARY_FILE_VIEWER) {
9473a40ed3dSBarry Smith     ierr = MatView_MPIAIJ_Binary(mat,viewer);CHKERRQ(ierr);
9485cd90555SBarry Smith   } else {
9495cd90555SBarry Smith     SETERRQ(1,1,"Viewer type not supported by PETSc object");
950416022c9SBarry Smith   }
9513a40ed3dSBarry Smith   PetscFunctionReturn(0);
952416022c9SBarry Smith }
953416022c9SBarry Smith 
9541eb62cbbSBarry Smith /*
9551eb62cbbSBarry Smith     This has to provide several versions.
9561eb62cbbSBarry Smith 
9571eb62cbbSBarry Smith      2) a) use only local smoothing updating outer values only once.
9581eb62cbbSBarry Smith         b) local smoothing updating outer values each inner iteration
959d6dfbf8fSBarry Smith      3) color updating out values betwen colors.
9601eb62cbbSBarry Smith */
9615615d1e5SSatish Balay #undef __FUNC__
9625615d1e5SSatish Balay #define __FUNC__ "MatRelax_MPIAIJ"
9638f6be9afSLois Curfman McInnes int MatRelax_MPIAIJ(Mat matin,Vec bb,double omega,MatSORType flag,
964dbb450caSBarry Smith                            double fshift,int its,Vec xx)
9658a729477SBarry Smith {
96644a69424SLois Curfman McInnes   Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data;
967d6dfbf8fSBarry Smith   Mat        AA = mat->A, BB = mat->B;
968ec8511deSBarry Smith   Mat_SeqAIJ *A = (Mat_SeqAIJ *) AA->data, *B = (Mat_SeqAIJ *)BB->data;
969c16cb8f2SBarry Smith   Scalar     *b,*x,*xs,*ls,d,*v,sum;
9706abc6512SBarry Smith   int        ierr,*idx, *diag;
971416022c9SBarry Smith   int        n = mat->n, m = mat->m, i,shift = A->indexshift;
9728a729477SBarry Smith 
9733a40ed3dSBarry Smith   PetscFunctionBegin;
9742e8a6d31SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
9752e8a6d31SBarry Smith   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
9762e8a6d31SBarry Smith   ierr = VecGetArray(mat->lvec,&ls);CHKERRQ(ierr);
977dbb450caSBarry Smith   xs = x + shift; /* shift by one for index start of 1 */
978dbb450caSBarry Smith   ls = ls + shift;
97983e2fdc7SBarry Smith   if (!A->diag) {ierr = MatMarkDiag_SeqAIJ(AA); CHKERRQ(ierr);}
980d6dfbf8fSBarry Smith   diag = A->diag;
981c16cb8f2SBarry Smith   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){
982da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
983f830108cSBarry Smith       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,its,xx);CHKERRQ(ierr);
9842e8a6d31SBarry Smith       ierr = VecRestoreArray(xx,&x); CHKERRQ(ierr);
9852e8a6d31SBarry Smith       ierr = VecRestoreArray(bb,&b); CHKERRQ(ierr);
9862e8a6d31SBarry Smith       ierr = VecRestoreArray(mat->lvec,&ls);CHKERRQ(ierr);
9873a40ed3dSBarry Smith       PetscFunctionReturn(0);
988da3a660dSBarry Smith     }
9893a40ed3dSBarry Smith     ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
9903a40ed3dSBarry Smith     ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
991d6dfbf8fSBarry Smith     while (its--) {
992d6dfbf8fSBarry Smith       /* go down through the rows */
993d6dfbf8fSBarry Smith       for ( i=0; i<m; i++ ) {
994d6dfbf8fSBarry Smith         n    = A->i[i+1] - A->i[i];
9954d197716SBarry Smith 	PLogFlops(4*n+3);
996dbb450caSBarry Smith         idx  = A->j + A->i[i] + shift;
997dbb450caSBarry Smith         v    = A->a + A->i[i] + shift;
998d6dfbf8fSBarry Smith         sum  = b[i];
999d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1000dbb450caSBarry Smith         d    = fshift + A->a[diag[i]+shift];
1001d6dfbf8fSBarry Smith         n    = B->i[i+1] - B->i[i];
1002dbb450caSBarry Smith         idx  = B->j + B->i[i] + shift;
1003dbb450caSBarry Smith         v    = B->a + B->i[i] + shift;
1004d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,ls,v,idx,n);
100555a1b374SBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + A->a[diag[i]+shift]*x[i])/d;
1006d6dfbf8fSBarry Smith       }
1007d6dfbf8fSBarry Smith       /* come up through the rows */
1008d6dfbf8fSBarry Smith       for ( i=m-1; i>-1; i-- ) {
1009d6dfbf8fSBarry Smith         n    = A->i[i+1] - A->i[i];
10104d197716SBarry Smith 	PLogFlops(4*n+3)
1011dbb450caSBarry Smith         idx  = A->j + A->i[i] + shift;
1012dbb450caSBarry Smith         v    = A->a + A->i[i] + shift;
1013d6dfbf8fSBarry Smith         sum  = b[i];
1014d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1015dbb450caSBarry Smith         d    = fshift + A->a[diag[i]+shift];
1016d6dfbf8fSBarry Smith         n    = B->i[i+1] - B->i[i];
1017dbb450caSBarry Smith         idx  = B->j + B->i[i] + shift;
1018dbb450caSBarry Smith         v    = B->a + B->i[i] + shift;
1019d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,ls,v,idx,n);
102055a1b374SBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + A->a[diag[i]+shift]*x[i])/d;
1021d6dfbf8fSBarry Smith       }
1022d6dfbf8fSBarry Smith     }
10233a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_FORWARD_SWEEP){
1024da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
1025f830108cSBarry Smith       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,its,xx);CHKERRQ(ierr);
10262e8a6d31SBarry Smith       ierr = VecRestoreArray(xx,&x); CHKERRQ(ierr);
10272e8a6d31SBarry Smith       ierr = VecRestoreArray(bb,&b); CHKERRQ(ierr);
10282e8a6d31SBarry Smith       ierr = VecRestoreArray(mat->lvec,&ls);CHKERRQ(ierr);
10293a40ed3dSBarry Smith       PetscFunctionReturn(0);
1030da3a660dSBarry Smith     }
10313a40ed3dSBarry Smith     ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10323a40ed3dSBarry Smith     ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
1033d6dfbf8fSBarry Smith     while (its--) {
1034d6dfbf8fSBarry Smith       for ( i=0; i<m; i++ ) {
1035d6dfbf8fSBarry Smith         n    = A->i[i+1] - A->i[i];
10364d197716SBarry Smith 	PLogFlops(4*n+3);
1037dbb450caSBarry Smith         idx  = A->j + A->i[i] + shift;
1038dbb450caSBarry Smith         v    = A->a + A->i[i] + shift;
1039d6dfbf8fSBarry Smith         sum  = b[i];
1040d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1041dbb450caSBarry Smith         d    = fshift + A->a[diag[i]+shift];
1042d6dfbf8fSBarry Smith         n    = B->i[i+1] - B->i[i];
1043dbb450caSBarry Smith         idx  = B->j + B->i[i] + shift;
1044dbb450caSBarry Smith         v    = B->a + B->i[i] + shift;
1045d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,ls,v,idx,n);
104655a1b374SBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + A->a[diag[i]+shift]*x[i])/d;
1047d6dfbf8fSBarry Smith       }
1048d6dfbf8fSBarry Smith     }
10493a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_BACKWARD_SWEEP){
1050da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
1051f830108cSBarry Smith       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,its,xx);CHKERRQ(ierr);
10522e8a6d31SBarry Smith       ierr = VecRestoreArray(xx,&x); CHKERRQ(ierr);
10532e8a6d31SBarry Smith       ierr = VecRestoreArray(bb,&b); CHKERRQ(ierr);
10542e8a6d31SBarry Smith       ierr = VecRestoreArray(mat->lvec,&ls);CHKERRQ(ierr);
10553a40ed3dSBarry Smith       PetscFunctionReturn(0);
1056da3a660dSBarry Smith     }
10572e8a6d31SBarry Smith     ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10582e8a6d31SBarry Smith     ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
1059d6dfbf8fSBarry Smith     while (its--) {
1060d6dfbf8fSBarry Smith       for ( i=m-1; i>-1; i-- ) {
1061d6dfbf8fSBarry Smith         n    = A->i[i+1] - A->i[i];
10624d197716SBarry Smith 	PLogFlops(4*n+3);
1063dbb450caSBarry Smith         idx  = A->j + A->i[i] + shift;
1064dbb450caSBarry Smith         v    = A->a + A->i[i] + shift;
1065d6dfbf8fSBarry Smith         sum  = b[i];
1066d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1067dbb450caSBarry Smith         d    = fshift + A->a[diag[i]+shift];
1068d6dfbf8fSBarry Smith         n    = B->i[i+1] - B->i[i];
1069dbb450caSBarry Smith         idx  = B->j + B->i[i] + shift;
1070dbb450caSBarry Smith         v    = B->a + B->i[i] + shift;
1071d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,ls,v,idx,n);
107255a1b374SBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + A->a[diag[i]+shift]*x[i])/d;
1073d6dfbf8fSBarry Smith       }
1074d6dfbf8fSBarry Smith     }
10753a40ed3dSBarry Smith   } else {
1076a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_SUP,0,"Parallel SOR not supported");
1077c16cb8f2SBarry Smith   }
10782e8a6d31SBarry Smith   ierr = VecRestoreArray(xx,&x); CHKERRQ(ierr);
10792e8a6d31SBarry Smith   ierr = VecRestoreArray(bb,&b); CHKERRQ(ierr);
10802e8a6d31SBarry Smith   ierr = VecRestoreArray(mat->lvec,&ls);CHKERRQ(ierr);
10813a40ed3dSBarry Smith   PetscFunctionReturn(0);
10828a729477SBarry Smith }
1083a66be287SLois Curfman McInnes 
10845615d1e5SSatish Balay #undef __FUNC__
1085d4bb536fSBarry Smith #define __FUNC__ "MatGetInfo_MPIAIJ"
10868f6be9afSLois Curfman McInnes int MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info)
1087a66be287SLois Curfman McInnes {
1088a66be287SLois Curfman McInnes   Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data;
1089a66be287SLois Curfman McInnes   Mat        A = mat->A, B = mat->B;
10904e220ebcSLois Curfman McInnes   int        ierr;
10914e220ebcSLois Curfman McInnes   double     isend[5], irecv[5];
1092a66be287SLois Curfman McInnes 
10933a40ed3dSBarry Smith   PetscFunctionBegin;
10944e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
10954e220ebcSLois Curfman McInnes   ierr = MatGetInfo(A,MAT_LOCAL,info); CHKERRQ(ierr);
10964e220ebcSLois Curfman McInnes   isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
10974e220ebcSLois Curfman McInnes   isend[3] = info->memory;  isend[4] = info->mallocs;
10984e220ebcSLois Curfman McInnes   ierr = MatGetInfo(B,MAT_LOCAL,info); CHKERRQ(ierr);
10994e220ebcSLois Curfman McInnes   isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
11004e220ebcSLois Curfman McInnes   isend[3] += info->memory;  isend[4] += info->mallocs;
1101a66be287SLois Curfman McInnes   if (flag == MAT_LOCAL) {
11024e220ebcSLois Curfman McInnes     info->nz_used      = isend[0];
11034e220ebcSLois Curfman McInnes     info->nz_allocated = isend[1];
11044e220ebcSLois Curfman McInnes     info->nz_unneeded  = isend[2];
11054e220ebcSLois Curfman McInnes     info->memory       = isend[3];
11064e220ebcSLois Curfman McInnes     info->mallocs      = isend[4];
1107a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_MAX) {
1108ca161407SBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPI_DOUBLE,MPI_MAX,matin->comm);CHKERRQ(ierr);
11094e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
11104e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
11114e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
11124e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
11134e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1114a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_SUM) {
1115ca161407SBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPI_DOUBLE,MPI_SUM,matin->comm);CHKERRQ(ierr);
11164e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
11174e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
11184e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
11194e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
11204e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1121a66be287SLois Curfman McInnes   }
11224e220ebcSLois Curfman McInnes   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
11234e220ebcSLois Curfman McInnes   info->fill_ratio_needed = 0;
11244e220ebcSLois Curfman McInnes   info->factor_mallocs    = 0;
11258d700155SBarry Smith   info->rows_global       = (double)mat->M;
11268d700155SBarry Smith   info->columns_global    = (double)mat->N;
11278d700155SBarry Smith   info->rows_local        = (double)mat->m;
11288d700155SBarry Smith   info->columns_local     = (double)mat->N;
11294e220ebcSLois Curfman McInnes 
11303a40ed3dSBarry Smith   PetscFunctionReturn(0);
1131a66be287SLois Curfman McInnes }
1132a66be287SLois Curfman McInnes 
11335615d1e5SSatish Balay #undef __FUNC__
1134d4bb536fSBarry Smith #define __FUNC__ "MatSetOption_MPIAIJ"
11358f6be9afSLois Curfman McInnes int MatSetOption_MPIAIJ(Mat A,MatOption op)
1136c74985f6SBarry Smith {
1137c0bbcb79SLois Curfman McInnes   Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data;
1138c74985f6SBarry Smith 
11393a40ed3dSBarry Smith   PetscFunctionBegin;
11406d4a8577SBarry Smith   if (op == MAT_NO_NEW_NONZERO_LOCATIONS ||
11416d4a8577SBarry Smith       op == MAT_YES_NEW_NONZERO_LOCATIONS ||
11426da5968aSLois Curfman McInnes       op == MAT_COLUMNS_UNSORTED ||
1143c2653b3dSLois Curfman McInnes       op == MAT_COLUMNS_SORTED ||
114496854ed6SLois Curfman McInnes       op == MAT_NEW_NONZERO_ALLOCATION_ERROR ||
1145c2653b3dSLois Curfman McInnes       op == MAT_NEW_NONZERO_LOCATION_ERROR) {
1146b1fbbac0SLois Curfman McInnes         MatSetOption(a->A,op);
1147b1fbbac0SLois Curfman McInnes         MatSetOption(a->B,op);
1148b1fbbac0SLois Curfman McInnes   } else if (op == MAT_ROW_ORIENTED) {
1149aeafbbfcSLois Curfman McInnes     a->roworiented = 1;
1150c0bbcb79SLois Curfman McInnes     MatSetOption(a->A,op);
1151c0bbcb79SLois Curfman McInnes     MatSetOption(a->B,op);
1152b1fbbac0SLois Curfman McInnes   } else if (op == MAT_ROWS_SORTED ||
11536da5968aSLois Curfman McInnes              op == MAT_ROWS_UNSORTED ||
11546d4a8577SBarry Smith              op == MAT_SYMMETRIC ||
11556d4a8577SBarry Smith              op == MAT_STRUCTURALLY_SYMMETRIC ||
11566d4a8577SBarry Smith              op == MAT_YES_NEW_DIAGONALS)
1157981c4779SBarry Smith     PLogInfo(A,"MatSetOption_MPIAIJ:Option ignored\n");
11586d4a8577SBarry Smith   else if (op == MAT_COLUMN_ORIENTED) {
11594b0e389bSBarry Smith     a->roworiented = 0;
11604b0e389bSBarry Smith     MatSetOption(a->A,op);
11614b0e389bSBarry Smith     MatSetOption(a->B,op);
11622b362799SSatish Balay   } else if (op == MAT_IGNORE_OFF_PROC_ENTRIES) {
116390f02eecSBarry Smith     a->donotstash = 1;
11643a40ed3dSBarry Smith   } else if (op == MAT_NO_NEW_DIAGONALS){
11653a40ed3dSBarry Smith     SETERRQ(PETSC_ERR_SUP,0,"MAT_NO_NEW_DIAGONALS");
11663a40ed3dSBarry Smith   } else {
11673a40ed3dSBarry Smith     SETERRQ(PETSC_ERR_SUP,0,"unknown option");
11683a40ed3dSBarry Smith   }
11693a40ed3dSBarry Smith   PetscFunctionReturn(0);
1170c74985f6SBarry Smith }
1171c74985f6SBarry Smith 
11725615d1e5SSatish Balay #undef __FUNC__
1173d4bb536fSBarry Smith #define __FUNC__ "MatGetSize_MPIAIJ"
11748f6be9afSLois Curfman McInnes int MatGetSize_MPIAIJ(Mat matin,int *m,int *n)
1175c74985f6SBarry Smith {
117644a69424SLois Curfman McInnes   Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data;
11773a40ed3dSBarry Smith 
11783a40ed3dSBarry Smith   PetscFunctionBegin;
11790752156aSBarry Smith   if (m) *m = mat->M;
11800752156aSBarry Smith   if (n) *n = mat->N;
11813a40ed3dSBarry Smith   PetscFunctionReturn(0);
1182c74985f6SBarry Smith }
1183c74985f6SBarry Smith 
11845615d1e5SSatish Balay #undef __FUNC__
1185d4bb536fSBarry Smith #define __FUNC__ "MatGetLocalSize_MPIAIJ"
11868f6be9afSLois Curfman McInnes int MatGetLocalSize_MPIAIJ(Mat matin,int *m,int *n)
1187c74985f6SBarry Smith {
118844a69424SLois Curfman McInnes   Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data;
11893a40ed3dSBarry Smith 
11903a40ed3dSBarry Smith   PetscFunctionBegin;
11910752156aSBarry Smith   if (m) *m = mat->m;
1192f830108cSBarry Smith   if (n) *n = mat->n;
11933a40ed3dSBarry Smith   PetscFunctionReturn(0);
1194c74985f6SBarry Smith }
1195c74985f6SBarry Smith 
11965615d1e5SSatish Balay #undef __FUNC__
1197d4bb536fSBarry Smith #define __FUNC__ "MatGetOwnershipRange_MPIAIJ"
11988f6be9afSLois Curfman McInnes int MatGetOwnershipRange_MPIAIJ(Mat matin,int *m,int *n)
1199c74985f6SBarry Smith {
120044a69424SLois Curfman McInnes   Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data;
12013a40ed3dSBarry Smith 
12023a40ed3dSBarry Smith   PetscFunctionBegin;
1203c74985f6SBarry Smith   *m = mat->rstart; *n = mat->rend;
12043a40ed3dSBarry Smith   PetscFunctionReturn(0);
1205c74985f6SBarry Smith }
1206c74985f6SBarry Smith 
12076d84be18SBarry Smith extern int MatGetRow_SeqAIJ(Mat,int,int*,int**,Scalar**);
12086d84be18SBarry Smith extern int MatRestoreRow_SeqAIJ(Mat,int,int*,int**,Scalar**);
12096d84be18SBarry Smith 
12105615d1e5SSatish Balay #undef __FUNC__
12115615d1e5SSatish Balay #define __FUNC__ "MatGetRow_MPIAIJ"
12126d84be18SBarry Smith int MatGetRow_MPIAIJ(Mat matin,int row,int *nz,int **idx,Scalar **v)
121339e00950SLois Curfman McInnes {
1214154123eaSLois Curfman McInnes   Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data;
121570f0671dSBarry Smith   Scalar     *vworkA, *vworkB, **pvA, **pvB,*v_p;
1216154123eaSLois Curfman McInnes   int        i, ierr, *cworkA, *cworkB, **pcA, **pcB, cstart = mat->cstart;
1217154123eaSLois Curfman McInnes   int        nztot, nzA, nzB, lrow, rstart = mat->rstart, rend = mat->rend;
121870f0671dSBarry Smith   int        *cmap, *idx_p;
121939e00950SLois Curfman McInnes 
12203a40ed3dSBarry Smith   PetscFunctionBegin;
1221a8c6a408SBarry Smith   if (mat->getrowactive == PETSC_TRUE) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Already active");
12227a0afa10SBarry Smith   mat->getrowactive = PETSC_TRUE;
12237a0afa10SBarry Smith 
122470f0671dSBarry Smith   if (!mat->rowvalues && (idx || v)) {
12257a0afa10SBarry Smith     /*
12267a0afa10SBarry Smith         allocate enough space to hold information from the longest row.
12277a0afa10SBarry Smith     */
12287a0afa10SBarry Smith     Mat_SeqAIJ *Aa = (Mat_SeqAIJ *) mat->A->data,*Ba = (Mat_SeqAIJ *) mat->B->data;
1229c16cb8f2SBarry Smith     int     max = 1,m = mat->m,tmp;
1230c16cb8f2SBarry Smith     for ( i=0; i<m; i++ ) {
12317a0afa10SBarry Smith       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
12327a0afa10SBarry Smith       if (max < tmp) { max = tmp; }
12337a0afa10SBarry Smith     }
12343f97c4b0SBarry Smith     mat->rowvalues  = (Scalar *) PetscMalloc(max*(sizeof(int)+sizeof(Scalar)));CHKPTRQ(mat->rowvalues);
12357a0afa10SBarry Smith     mat->rowindices = (int *) (mat->rowvalues + max);
12367a0afa10SBarry Smith   }
12377a0afa10SBarry Smith 
1238a8c6a408SBarry Smith   if (row < rstart || row >= rend) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Only local rows")
1239abc0e9e4SLois Curfman McInnes   lrow = row - rstart;
124039e00950SLois Curfman McInnes 
1241154123eaSLois Curfman McInnes   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1242154123eaSLois Curfman McInnes   if (!v)   {pvA = 0; pvB = 0;}
1243154123eaSLois Curfman McInnes   if (!idx) {pcA = 0; if (!v) pcB = 0;}
1244f830108cSBarry Smith   ierr = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA); CHKERRQ(ierr);
1245f830108cSBarry Smith   ierr = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB); CHKERRQ(ierr);
1246154123eaSLois Curfman McInnes   nztot = nzA + nzB;
1247154123eaSLois Curfman McInnes 
124870f0671dSBarry Smith   cmap  = mat->garray;
1249154123eaSLois Curfman McInnes   if (v  || idx) {
1250154123eaSLois Curfman McInnes     if (nztot) {
1251154123eaSLois Curfman McInnes       /* Sort by increasing column numbers, assuming A and B already sorted */
125270f0671dSBarry Smith       int imark = -1;
1253154123eaSLois Curfman McInnes       if (v) {
125470f0671dSBarry Smith         *v = v_p = mat->rowvalues;
125539e00950SLois Curfman McInnes         for ( i=0; i<nzB; i++ ) {
125670f0671dSBarry Smith           if (cmap[cworkB[i]] < cstart)   v_p[i] = vworkB[i];
1257154123eaSLois Curfman McInnes           else break;
1258154123eaSLois Curfman McInnes         }
1259154123eaSLois Curfman McInnes         imark = i;
126070f0671dSBarry Smith         for ( i=0; i<nzA; i++ )     v_p[imark+i] = vworkA[i];
126170f0671dSBarry Smith         for ( i=imark; i<nzB; i++ ) v_p[nzA+i]   = vworkB[i];
1262154123eaSLois Curfman McInnes       }
1263154123eaSLois Curfman McInnes       if (idx) {
126470f0671dSBarry Smith         *idx = idx_p = mat->rowindices;
126570f0671dSBarry Smith         if (imark > -1) {
126670f0671dSBarry Smith           for ( i=0; i<imark; i++ ) {
126770f0671dSBarry Smith             idx_p[i] = cmap[cworkB[i]];
126870f0671dSBarry Smith           }
126970f0671dSBarry Smith         } else {
1270154123eaSLois Curfman McInnes           for ( i=0; i<nzB; i++ ) {
127170f0671dSBarry Smith             if (cmap[cworkB[i]] < cstart)   idx_p[i] = cmap[cworkB[i]];
1272154123eaSLois Curfman McInnes             else break;
1273154123eaSLois Curfman McInnes           }
1274154123eaSLois Curfman McInnes           imark = i;
127570f0671dSBarry Smith         }
127670f0671dSBarry Smith         for ( i=0; i<nzA; i++ )     idx_p[imark+i] = cstart + cworkA[i];
127770f0671dSBarry Smith         for ( i=imark; i<nzB; i++ ) idx_p[nzA+i]   = cmap[cworkB[i]];
127839e00950SLois Curfman McInnes       }
12793f97c4b0SBarry Smith     } else {
12801ca473b0SSatish Balay       if (idx) *idx = 0;
12811ca473b0SSatish Balay       if (v)   *v   = 0;
12821ca473b0SSatish Balay     }
1283154123eaSLois Curfman McInnes   }
128439e00950SLois Curfman McInnes   *nz = nztot;
1285f830108cSBarry Smith   ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA); CHKERRQ(ierr);
1286f830108cSBarry Smith   ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB); CHKERRQ(ierr);
12873a40ed3dSBarry Smith   PetscFunctionReturn(0);
128839e00950SLois Curfman McInnes }
128939e00950SLois Curfman McInnes 
12905615d1e5SSatish Balay #undef __FUNC__
1291d4bb536fSBarry Smith #define __FUNC__ "MatRestoreRow_MPIAIJ"
12926d84be18SBarry Smith int MatRestoreRow_MPIAIJ(Mat mat,int row,int *nz,int **idx,Scalar **v)
129339e00950SLois Curfman McInnes {
12947a0afa10SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data;
12953a40ed3dSBarry Smith 
12963a40ed3dSBarry Smith   PetscFunctionBegin;
12977a0afa10SBarry Smith   if (aij->getrowactive == PETSC_FALSE) {
1298a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"MatGetRow not called");
12997a0afa10SBarry Smith   }
13007a0afa10SBarry Smith   aij->getrowactive = PETSC_FALSE;
13013a40ed3dSBarry Smith   PetscFunctionReturn(0);
130239e00950SLois Curfman McInnes }
130339e00950SLois Curfman McInnes 
13045615d1e5SSatish Balay #undef __FUNC__
13055615d1e5SSatish Balay #define __FUNC__ "MatNorm_MPIAIJ"
13068f6be9afSLois Curfman McInnes int MatNorm_MPIAIJ(Mat mat,NormType type,double *norm)
1307855ac2c5SLois Curfman McInnes {
1308855ac2c5SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data;
1309ec8511deSBarry Smith   Mat_SeqAIJ *amat = (Mat_SeqAIJ*) aij->A->data, *bmat = (Mat_SeqAIJ*) aij->B->data;
1310416022c9SBarry Smith   int        ierr, i, j, cstart = aij->cstart,shift = amat->indexshift;
1311416022c9SBarry Smith   double     sum = 0.0;
131204ca555eSLois Curfman McInnes   Scalar     *v;
131304ca555eSLois Curfman McInnes 
13143a40ed3dSBarry Smith   PetscFunctionBegin;
131517699dbbSLois Curfman McInnes   if (aij->size == 1) {
131614183eadSLois Curfman McInnes     ierr =  MatNorm(aij->A,type,norm); CHKERRQ(ierr);
131737fa93a5SLois Curfman McInnes   } else {
131804ca555eSLois Curfman McInnes     if (type == NORM_FROBENIUS) {
131904ca555eSLois Curfman McInnes       v = amat->a;
132004ca555eSLois Curfman McInnes       for (i=0; i<amat->nz; i++ ) {
13213a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX)
1322e20fef11SSatish Balay         sum += PetscReal(PetscConj(*v)*(*v)); v++;
132304ca555eSLois Curfman McInnes #else
132404ca555eSLois Curfman McInnes         sum += (*v)*(*v); v++;
132504ca555eSLois Curfman McInnes #endif
132604ca555eSLois Curfman McInnes       }
132704ca555eSLois Curfman McInnes       v = bmat->a;
132804ca555eSLois Curfman McInnes       for (i=0; i<bmat->nz; i++ ) {
13293a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX)
1330e20fef11SSatish Balay         sum += PetscReal(PetscConj(*v)*(*v)); v++;
133104ca555eSLois Curfman McInnes #else
133204ca555eSLois Curfman McInnes         sum += (*v)*(*v); v++;
133304ca555eSLois Curfman McInnes #endif
133404ca555eSLois Curfman McInnes       }
1335ca161407SBarry Smith       ierr = MPI_Allreduce(&sum,norm,1,MPI_DOUBLE,MPI_SUM,mat->comm);CHKERRQ(ierr);
133604ca555eSLois Curfman McInnes       *norm = sqrt(*norm);
13373a40ed3dSBarry Smith     } else if (type == NORM_1) { /* max column norm */
133804ca555eSLois Curfman McInnes       double *tmp, *tmp2;
133904ca555eSLois Curfman McInnes       int    *jj, *garray = aij->garray;
1340758f045eSSatish Balay       tmp  = (double *) PetscMalloc( (aij->N+1)*sizeof(double) ); CHKPTRQ(tmp);
1341758f045eSSatish Balay       tmp2 = (double *) PetscMalloc( (aij->N+1)*sizeof(double) ); CHKPTRQ(tmp2);
1342cddf8d76SBarry Smith       PetscMemzero(tmp,aij->N*sizeof(double));
134304ca555eSLois Curfman McInnes       *norm = 0.0;
134404ca555eSLois Curfman McInnes       v = amat->a; jj = amat->j;
134504ca555eSLois Curfman McInnes       for ( j=0; j<amat->nz; j++ ) {
1346579c6b6fSBarry Smith         tmp[cstart + *jj++ + shift] += PetscAbsScalar(*v);  v++;
134704ca555eSLois Curfman McInnes       }
134804ca555eSLois Curfman McInnes       v = bmat->a; jj = bmat->j;
134904ca555eSLois Curfman McInnes       for ( j=0; j<bmat->nz; j++ ) {
1350579c6b6fSBarry Smith         tmp[garray[*jj++ + shift]] += PetscAbsScalar(*v); v++;
135104ca555eSLois Curfman McInnes       }
1352ca161407SBarry Smith       ierr = MPI_Allreduce(tmp,tmp2,aij->N,MPI_DOUBLE,MPI_SUM,mat->comm);CHKERRQ(ierr);
135304ca555eSLois Curfman McInnes       for ( j=0; j<aij->N; j++ ) {
135404ca555eSLois Curfman McInnes         if (tmp2[j] > *norm) *norm = tmp2[j];
135504ca555eSLois Curfman McInnes       }
13560452661fSBarry Smith       PetscFree(tmp); PetscFree(tmp2);
13573a40ed3dSBarry Smith     } else if (type == NORM_INFINITY) { /* max row norm */
1358515d9167SLois Curfman McInnes       double ntemp = 0.0;
135904ca555eSLois Curfman McInnes       for ( j=0; j<amat->m; j++ ) {
1360dbb450caSBarry Smith         v = amat->a + amat->i[j] + shift;
136104ca555eSLois Curfman McInnes         sum = 0.0;
136204ca555eSLois Curfman McInnes         for ( i=0; i<amat->i[j+1]-amat->i[j]; i++ ) {
1363cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
136404ca555eSLois Curfman McInnes         }
1365dbb450caSBarry Smith         v = bmat->a + bmat->i[j] + shift;
136604ca555eSLois Curfman McInnes         for ( i=0; i<bmat->i[j+1]-bmat->i[j]; i++ ) {
1367cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
136804ca555eSLois Curfman McInnes         }
1369515d9167SLois Curfman McInnes         if (sum > ntemp) ntemp = sum;
137004ca555eSLois Curfman McInnes       }
1371ca161407SBarry Smith       ierr = MPI_Allreduce(&ntemp,norm,1,MPI_DOUBLE,MPI_MAX,mat->comm);CHKERRQ(ierr);
1372ca161407SBarry Smith     } else {
1373a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_SUP,0,"No support for two norm");
137404ca555eSLois Curfman McInnes     }
137537fa93a5SLois Curfman McInnes   }
13763a40ed3dSBarry Smith   PetscFunctionReturn(0);
1377855ac2c5SLois Curfman McInnes }
1378855ac2c5SLois Curfman McInnes 
13795615d1e5SSatish Balay #undef __FUNC__
13805615d1e5SSatish Balay #define __FUNC__ "MatTranspose_MPIAIJ"
13818f6be9afSLois Curfman McInnes int MatTranspose_MPIAIJ(Mat A,Mat *matout)
1382b7c46309SBarry Smith {
1383b7c46309SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data;
1384dbb450caSBarry Smith   Mat_SeqAIJ *Aloc = (Mat_SeqAIJ *) a->A->data;
1385416022c9SBarry Smith   int        ierr,shift = Aloc->indexshift;
1386b7c46309SBarry Smith   int        M = a->M, N = a->N,m,*ai,*aj,row,*cols,i,*ct;
13873a40ed3dSBarry Smith   Mat        B;
1388b7c46309SBarry Smith   Scalar     *array;
1389b7c46309SBarry Smith 
13903a40ed3dSBarry Smith   PetscFunctionBegin;
1391d4bb536fSBarry Smith   if (matout == PETSC_NULL && M != N) {
1392a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_SIZ,0,"Square matrix only for in-place");
1393d4bb536fSBarry Smith   }
1394d4bb536fSBarry Smith 
1395d4bb536fSBarry Smith   ierr = MatCreateMPIAIJ(A->comm,a->n,a->m,N,M,0,PETSC_NULL,0,PETSC_NULL,&B);CHKERRQ(ierr);
1396b7c46309SBarry Smith 
1397b7c46309SBarry Smith   /* copy over the A part */
1398ec8511deSBarry Smith   Aloc = (Mat_SeqAIJ*) a->A->data;
1399b7c46309SBarry Smith   m = Aloc->m; ai = Aloc->i; aj = Aloc->j; array = Aloc->a;
1400b7c46309SBarry Smith   row = a->rstart;
1401dbb450caSBarry Smith   for ( i=0; i<ai[m]+shift; i++ ) {aj[i] += a->cstart + shift;}
1402b7c46309SBarry Smith   for ( i=0; i<m; i++ ) {
1403416022c9SBarry Smith     ierr = MatSetValues(B,ai[i+1]-ai[i],aj,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
1404b7c46309SBarry Smith     row++; array += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
1405b7c46309SBarry Smith   }
1406b7c46309SBarry Smith   aj = Aloc->j;
14074af08d9eSBarry Smith   for ( i=0; i<ai[m]+shift; i++ ) {aj[i] -= a->cstart + shift;}
1408b7c46309SBarry Smith 
1409b7c46309SBarry Smith   /* copy over the B part */
1410ec8511deSBarry Smith   Aloc = (Mat_SeqAIJ*) a->B->data;
1411b7c46309SBarry Smith   m = Aloc->m;  ai = Aloc->i; aj = Aloc->j; array = Aloc->a;
1412b7c46309SBarry Smith   row = a->rstart;
14130452661fSBarry Smith   ct = cols = (int *) PetscMalloc( (1+ai[m]-shift)*sizeof(int) ); CHKPTRQ(cols);
1414dbb450caSBarry Smith   for ( i=0; i<ai[m]+shift; i++ ) {cols[i] = a->garray[aj[i]+shift];}
1415b7c46309SBarry Smith   for ( i=0; i<m; i++ ) {
1416416022c9SBarry Smith     ierr = MatSetValues(B,ai[i+1]-ai[i],cols,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
1417b7c46309SBarry Smith     row++; array += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
1418b7c46309SBarry Smith   }
14190452661fSBarry Smith   PetscFree(ct);
14206d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
14216d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
14223638b69dSLois Curfman McInnes   if (matout != PETSC_NULL) {
14230de55854SLois Curfman McInnes     *matout = B;
14240de55854SLois Curfman McInnes   } else {
1425f830108cSBarry Smith     PetscOps       *Abops;
1426f830108cSBarry Smith     struct _MatOps *Aops;
1427f830108cSBarry Smith 
14280de55854SLois Curfman McInnes     /* This isn't really an in-place transpose .... but free data structures from a */
14290452661fSBarry Smith     PetscFree(a->rowners);
14300de55854SLois Curfman McInnes     ierr = MatDestroy(a->A); CHKERRQ(ierr);
14310de55854SLois Curfman McInnes     ierr = MatDestroy(a->B); CHKERRQ(ierr);
14320452661fSBarry Smith     if (a->colmap) PetscFree(a->colmap);
14330452661fSBarry Smith     if (a->garray) PetscFree(a->garray);
14340de55854SLois Curfman McInnes     if (a->lvec) VecDestroy(a->lvec);
1435a56f8943SBarry Smith     if (a->Mvctx) VecScatterDestroy(a->Mvctx);
14360452661fSBarry Smith     PetscFree(a);
1437f830108cSBarry Smith 
1438f830108cSBarry Smith     /*
1439f830108cSBarry Smith        This is horrible, horrible code. We need to keep the
1440f830108cSBarry Smith       A pointers for the bops and ops but copy everything
1441f830108cSBarry Smith       else from C.
1442f830108cSBarry Smith     */
1443f830108cSBarry Smith     Abops = A->bops;
1444f830108cSBarry Smith     Aops  = A->ops;
1445f09e8eb9SSatish Balay     PetscMemcpy(A,B,sizeof(struct _p_Mat));
1446f830108cSBarry Smith     A->bops = Abops;
1447f830108cSBarry Smith     A->ops  = Aops;
14480452661fSBarry Smith     PetscHeaderDestroy(B);
14490de55854SLois Curfman McInnes   }
14503a40ed3dSBarry Smith   PetscFunctionReturn(0);
1451b7c46309SBarry Smith }
1452b7c46309SBarry Smith 
14535615d1e5SSatish Balay #undef __FUNC__
14545615d1e5SSatish Balay #define __FUNC__ "MatDiagonalScale_MPIAIJ"
14554b967eb1SSatish Balay int MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr)
1456a008b906SSatish Balay {
14574b967eb1SSatish Balay   Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data;
14584b967eb1SSatish Balay   Mat a = aij->A, b = aij->B;
1459a008b906SSatish Balay   int ierr,s1,s2,s3;
1460a008b906SSatish Balay 
14613a40ed3dSBarry Smith   PetscFunctionBegin;
14624b967eb1SSatish Balay   ierr = MatGetLocalSize(mat,&s2,&s3); CHKERRQ(ierr);
14634b967eb1SSatish Balay   if (rr) {
1464e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr);
1465a8c6a408SBarry Smith     if (s1!=s3) SETERRQ(PETSC_ERR_ARG_SIZ,0,"right vector non-conforming local size");
14664b967eb1SSatish Balay     /* Overlap communication with computation. */
146743a90d84SBarry Smith     ierr = VecScatterBegin(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx); CHKERRQ(ierr);
1468a008b906SSatish Balay   }
14694b967eb1SSatish Balay   if (ll) {
1470e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr);
1471a8c6a408SBarry Smith     if (s1!=s2) SETERRQ(PETSC_ERR_ARG_SIZ,0,"left vector non-conforming local size");
1472f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,ll,0); CHKERRQ(ierr);
14734b967eb1SSatish Balay   }
14744b967eb1SSatish Balay   /* scale  the diagonal block */
1475f830108cSBarry Smith   ierr = (*a->ops->diagonalscale)(a,ll,rr); CHKERRQ(ierr);
14764b967eb1SSatish Balay 
14774b967eb1SSatish Balay   if (rr) {
14784b967eb1SSatish Balay     /* Do a scatter end and then right scale the off-diagonal block */
147943a90d84SBarry Smith     ierr = VecScatterEnd(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx); CHKERRQ(ierr);
1480f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,0,aij->lvec); CHKERRQ(ierr);
14814b967eb1SSatish Balay   }
14824b967eb1SSatish Balay 
14833a40ed3dSBarry Smith   PetscFunctionReturn(0);
1484a008b906SSatish Balay }
1485a008b906SSatish Balay 
1486a008b906SSatish Balay 
1487682d7d0cSBarry Smith extern int MatPrintHelp_SeqAIJ(Mat);
14885615d1e5SSatish Balay #undef __FUNC__
1489d4bb536fSBarry Smith #define __FUNC__ "MatPrintHelp_MPIAIJ"
14908f6be9afSLois Curfman McInnes int MatPrintHelp_MPIAIJ(Mat A)
1491682d7d0cSBarry Smith {
1492682d7d0cSBarry Smith   Mat_MPIAIJ *a   = (Mat_MPIAIJ*) A->data;
14933a40ed3dSBarry Smith   int        ierr;
1494682d7d0cSBarry Smith 
14953a40ed3dSBarry Smith   PetscFunctionBegin;
14963a40ed3dSBarry Smith   if (!a->rank) {
14973a40ed3dSBarry Smith     ierr = MatPrintHelp_SeqAIJ(a->A);CHKERRQ(ierr);
14983a40ed3dSBarry Smith   }
14993a40ed3dSBarry Smith   PetscFunctionReturn(0);
1500682d7d0cSBarry Smith }
1501682d7d0cSBarry Smith 
15025615d1e5SSatish Balay #undef __FUNC__
1503d4bb536fSBarry Smith #define __FUNC__ "MatGetBlockSize_MPIAIJ"
15048f6be9afSLois Curfman McInnes int MatGetBlockSize_MPIAIJ(Mat A,int *bs)
15055a838052SSatish Balay {
15063a40ed3dSBarry Smith   PetscFunctionBegin;
15075a838052SSatish Balay   *bs = 1;
15083a40ed3dSBarry Smith   PetscFunctionReturn(0);
15095a838052SSatish Balay }
15105615d1e5SSatish Balay #undef __FUNC__
1511d4bb536fSBarry Smith #define __FUNC__ "MatSetUnfactored_MPIAIJ"
15128f6be9afSLois Curfman McInnes int MatSetUnfactored_MPIAIJ(Mat A)
1513bb5a7306SBarry Smith {
1514bb5a7306SBarry Smith   Mat_MPIAIJ *a   = (Mat_MPIAIJ*) A->data;
1515bb5a7306SBarry Smith   int        ierr;
15163a40ed3dSBarry Smith 
15173a40ed3dSBarry Smith   PetscFunctionBegin;
1518bb5a7306SBarry Smith   ierr = MatSetUnfactored(a->A); CHKERRQ(ierr);
15193a40ed3dSBarry Smith   PetscFunctionReturn(0);
1520bb5a7306SBarry Smith }
1521bb5a7306SBarry Smith 
1522d4bb536fSBarry Smith #undef __FUNC__
1523d4bb536fSBarry Smith #define __FUNC__ "MatEqual_MPIAIJ"
1524d4bb536fSBarry Smith int MatEqual_MPIAIJ(Mat A, Mat B, PetscTruth *flag)
1525d4bb536fSBarry Smith {
1526d4bb536fSBarry Smith   Mat_MPIAIJ *matB = (Mat_MPIAIJ *) B->data,*matA = (Mat_MPIAIJ *) A->data;
1527d4bb536fSBarry Smith   Mat        a, b, c, d;
1528d4bb536fSBarry Smith   PetscTruth flg;
1529d4bb536fSBarry Smith   int        ierr;
1530d4bb536fSBarry Smith 
15313a40ed3dSBarry Smith   PetscFunctionBegin;
1532a8c6a408SBarry Smith   if (B->type != MATMPIAIJ) SETERRQ(PETSC_ERR_ARG_INCOMP,0,"Matrices must be same type");
1533d4bb536fSBarry Smith   a = matA->A; b = matA->B;
1534d4bb536fSBarry Smith   c = matB->A; d = matB->B;
1535d4bb536fSBarry Smith 
1536d4bb536fSBarry Smith   ierr = MatEqual(a, c, &flg); CHKERRQ(ierr);
1537d4bb536fSBarry Smith   if (flg == PETSC_TRUE) {
1538d4bb536fSBarry Smith     ierr = MatEqual(b, d, &flg); CHKERRQ(ierr);
1539d4bb536fSBarry Smith   }
1540ca161407SBarry Smith   ierr = MPI_Allreduce(&flg, flag, 1, MPI_INT, MPI_LAND, A->comm);CHKERRQ(ierr);
15413a40ed3dSBarry Smith   PetscFunctionReturn(0);
1542d4bb536fSBarry Smith }
1543d4bb536fSBarry Smith 
1544cb5b572fSBarry Smith #undef __FUNC__
1545cb5b572fSBarry Smith #define __FUNC__ "MatCopy_MPIAIJ"
1546cb5b572fSBarry Smith int MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str)
1547cb5b572fSBarry Smith {
1548cb5b572fSBarry Smith   int        ierr;
1549cb5b572fSBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;
1550cb5b572fSBarry Smith   Mat_MPIAIJ *b = (Mat_MPIAIJ *)B->data;
1551cb5b572fSBarry Smith 
1552cb5b572fSBarry Smith   PetscFunctionBegin;
1553cb5b572fSBarry Smith   if (str != SAME_NONZERO_PATTERN || B->type != MATMPIAIJ) {
1554cb5b572fSBarry Smith     /* because of the column compression in the off-processor part of the matrix a->B,
1555cb5b572fSBarry Smith        the number of columns in a->B and b->B may be different, hence we cannot call
1556cb5b572fSBarry Smith        the MatCopy() directly on the two parts. If need be, we can provide a more
1557cb5b572fSBarry Smith        efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices
1558cb5b572fSBarry Smith        then copying the submatrices */
1559cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1560cb5b572fSBarry Smith   } else {
1561cb5b572fSBarry Smith     ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr);
1562cb5b572fSBarry Smith     ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr);
1563cb5b572fSBarry Smith   }
1564cb5b572fSBarry Smith   PetscFunctionReturn(0);
1565cb5b572fSBarry Smith }
1566cb5b572fSBarry Smith 
15672e8a6d31SBarry Smith extern int MatDuplicate_MPIAIJ(Mat,MatDuplicateOption,Mat *);
15682f86bd48SSatish Balay extern int MatIncreaseOverlap_MPIAIJ(Mat , int, IS *, int);
15690a198c4cSBarry Smith extern int MatFDColoringCreate_MPIAIJ(Mat,ISColoring,MatFDColoring);
15700a198c4cSBarry Smith extern int MatGetSubMatrices_MPIAIJ (Mat ,int , IS *,IS *,MatGetSubMatrixCall,Mat **);
15716a6a5d1dSBarry Smith extern int MatGetSubMatrix_MPIAIJ (Mat ,IS,IS,int,MatGetSubMatrixCall,Mat *);
157200e6dbe6SBarry Smith 
15738a729477SBarry Smith /* -------------------------------------------------------------------*/
1574cda55fadSBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ,
1575cda55fadSBarry Smith        MatGetRow_MPIAIJ,
1576cda55fadSBarry Smith        MatRestoreRow_MPIAIJ,
1577cda55fadSBarry Smith        MatMult_MPIAIJ,
1578cda55fadSBarry Smith        MatMultAdd_MPIAIJ,
1579cda55fadSBarry Smith        MatMultTrans_MPIAIJ,
1580cda55fadSBarry Smith        MatMultTransAdd_MPIAIJ,
1581cda55fadSBarry Smith        0,
1582cda55fadSBarry Smith        0,
1583cda55fadSBarry Smith        0,
1584cda55fadSBarry Smith        0,
1585cda55fadSBarry Smith        0,
1586cda55fadSBarry Smith        0,
158744a69424SLois Curfman McInnes        MatRelax_MPIAIJ,
1588b7c46309SBarry Smith        MatTranspose_MPIAIJ,
1589cda55fadSBarry Smith        MatGetInfo_MPIAIJ,
1590cda55fadSBarry Smith        MatEqual_MPIAIJ,
1591cda55fadSBarry Smith        MatGetDiagonal_MPIAIJ,
1592cda55fadSBarry Smith        MatDiagonalScale_MPIAIJ,
1593cda55fadSBarry Smith        MatNorm_MPIAIJ,
1594cda55fadSBarry Smith        MatAssemblyBegin_MPIAIJ,
1595cda55fadSBarry Smith        MatAssemblyEnd_MPIAIJ,
15961eb62cbbSBarry Smith        0,
1597cda55fadSBarry Smith        MatSetOption_MPIAIJ,
1598cda55fadSBarry Smith        MatZeroEntries_MPIAIJ,
1599cda55fadSBarry Smith        MatZeroRows_MPIAIJ,
1600cda55fadSBarry Smith        0,
1601cda55fadSBarry Smith        0,
1602cda55fadSBarry Smith        0,
1603cda55fadSBarry Smith        0,
1604cda55fadSBarry Smith        MatGetSize_MPIAIJ,
1605cda55fadSBarry Smith        MatGetLocalSize_MPIAIJ,
1606cda55fadSBarry Smith        MatGetOwnershipRange_MPIAIJ,
1607cda55fadSBarry Smith        0,
1608cda55fadSBarry Smith        0,
1609cda55fadSBarry Smith        0,
1610cda55fadSBarry Smith        0,
16112e8a6d31SBarry Smith        MatDuplicate_MPIAIJ,
1612cda55fadSBarry Smith        0,
1613cda55fadSBarry Smith        0,
1614cda55fadSBarry Smith        0,
1615cda55fadSBarry Smith        0,
1616cda55fadSBarry Smith        0,
1617cda55fadSBarry Smith        MatGetSubMatrices_MPIAIJ,
1618cda55fadSBarry Smith        MatIncreaseOverlap_MPIAIJ,
1619cda55fadSBarry Smith        MatGetValues_MPIAIJ,
1620cb5b572fSBarry Smith        MatCopy_MPIAIJ,
1621052efed2SBarry Smith        MatPrintHelp_MPIAIJ,
1622cda55fadSBarry Smith        MatScale_MPIAIJ,
1623cda55fadSBarry Smith        0,
1624cda55fadSBarry Smith        0,
1625cda55fadSBarry Smith        0,
1626cda55fadSBarry Smith        MatGetBlockSize_MPIAIJ,
1627cda55fadSBarry Smith        0,
1628cda55fadSBarry Smith        0,
1629cda55fadSBarry Smith        0,
1630cda55fadSBarry Smith        0,
1631cda55fadSBarry Smith        MatFDColoringCreate_MPIAIJ,
1632cda55fadSBarry Smith        0,
1633cda55fadSBarry Smith        MatSetUnfactored_MPIAIJ,
1634cda55fadSBarry Smith        0,
1635cda55fadSBarry Smith        0,
1636cda55fadSBarry Smith        MatGetSubMatrix_MPIAIJ,
1637cda55fadSBarry Smith        0,
1638cda55fadSBarry Smith        0,
1639cda55fadSBarry Smith        MatGetMaps_Petsc};
164036ce4990SBarry Smith 
16412e8a6d31SBarry Smith /* ----------------------------------------------------------------------------------------*/
16422e8a6d31SBarry Smith 
16432e8a6d31SBarry Smith #undef __FUNC__
16442e8a6d31SBarry Smith #define __FUNC__ "MatStoreValues_MPIAIJ"
16452e8a6d31SBarry Smith int MatStoreValues_MPIAIJ(Mat mat)
16462e8a6d31SBarry Smith {
16472e8a6d31SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data;
16482e8a6d31SBarry Smith   int        ierr;
16492e8a6d31SBarry Smith 
16502e8a6d31SBarry Smith   PetscFunctionBegin;
16512e8a6d31SBarry Smith   ierr = MatStoreValues(aij->A);CHKERRQ(ierr);
16522e8a6d31SBarry Smith   ierr = MatStoreValues(aij->B);CHKERRQ(ierr);
16532e8a6d31SBarry Smith   PetscFunctionReturn(0);
16542e8a6d31SBarry Smith }
16552e8a6d31SBarry Smith 
16562e8a6d31SBarry Smith #undef __FUNC__
16572e8a6d31SBarry Smith #define __FUNC__ "MatRetrieveValues_MPIAIJ"
16582e8a6d31SBarry Smith int MatRetrieveValues_MPIAIJ(Mat mat)
16592e8a6d31SBarry Smith {
16602e8a6d31SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data;
16612e8a6d31SBarry Smith   int        ierr;
16622e8a6d31SBarry Smith 
16632e8a6d31SBarry Smith   PetscFunctionBegin;
16642e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr);
16652e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr);
16662e8a6d31SBarry Smith   PetscFunctionReturn(0);
16672e8a6d31SBarry Smith }
16688a729477SBarry Smith 
16695615d1e5SSatish Balay #undef __FUNC__
16705615d1e5SSatish Balay #define __FUNC__ "MatCreateMPIAIJ"
16711987afe7SBarry Smith /*@C
1672ff756334SLois Curfman McInnes    MatCreateMPIAIJ - Creates a sparse parallel matrix in AIJ format
16733a511b96SLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
16743a511b96SLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameters
16753a511b96SLois Curfman McInnes    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
16763a511b96SLois Curfman McInnes    performance can be increased by more than a factor of 50.
16778a729477SBarry Smith 
1678db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
1679db81eaa0SLois Curfman McInnes 
16808a729477SBarry Smith    Input Parameters:
1681db81eaa0SLois Curfman McInnes +  comm - MPI communicator
16827d3e4905SLois Curfman McInnes .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
168392e8d321SLois Curfman McInnes            This value should be the same as the local size used in creating the
168492e8d321SLois Curfman McInnes            y vector for the matrix-vector product y = Ax.
16851a3896d6SBarry Smith .  n - This value should be the same as the local size used in creating the
16861a3896d6SBarry Smith        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
16871a3896d6SBarry Smith        calculated if N is given) For square matrices n is almost always m.
168860d380a7SBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
168960d380a7SBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
1690ab693e5aSLois Curfman McInnes .  d_nz - number of nonzeros per row in diagonal portion of local submatrix
1691ff756334SLois Curfman McInnes            (same for all local rows)
16922bd5e0b2SLois Curfman McInnes .  d_nzz - array containing the number of nonzeros in the various rows of the
169392e8d321SLois Curfman McInnes            diagonal portion of the local submatrix (possibly different for each row)
16942bd5e0b2SLois Curfman McInnes            or PETSC_NULL. You must leave room for the diagonal entry even if
16952bd5e0b2SLois Curfman McInnes            it is zero.
16962bd5e0b2SLois Curfman McInnes .  o_nz - number of nonzeros per row in the off-diagonal portion of local
1697ab693e5aSLois Curfman McInnes            submatrix (same for all local rows).
1698db81eaa0SLois Curfman McInnes -  o_nzz - array containing the number of nonzeros in the various rows of the
16992bd5e0b2SLois Curfman McInnes            off-diagonal portion of the local submatrix (possibly different for
17002bd5e0b2SLois Curfman McInnes            each row) or PETSC_NULL.
17018a729477SBarry Smith 
1702ff756334SLois Curfman McInnes    Output Parameter:
170344cd7ae7SLois Curfman McInnes .  A - the matrix
17048a729477SBarry Smith 
1705b259b22eSLois Curfman McInnes    Notes:
1706be79a94dSBarry Smith    If PETSC_DECIDE or  PETSC_DETERMINE is used for a particular argument on one processor
1707be79a94dSBarry Smith    than it must be used on all processors that share the object for that argument.
1708be79a94dSBarry Smith 
1709ff756334SLois Curfman McInnes    The AIJ format (also called the Yale sparse matrix format or
1710ff756334SLois Curfman McInnes    compressed row storage), is fully compatible with standard Fortran 77
17110002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
17120002213bSLois Curfman McInnes    either one (as in Fortran) or zero.  See the users manual for details.
1713ff756334SLois Curfman McInnes 
1714ff756334SLois Curfman McInnes    The user MUST specify either the local or global matrix dimensions
1715ff756334SLois Curfman McInnes    (possibly both).
1716ff756334SLois Curfman McInnes 
17175511cfe3SLois Curfman McInnes    By default, this format uses inodes (identical nodes) when possible.
17185511cfe3SLois Curfman McInnes    We search for consecutive rows with the same nonzero structure, thereby
17195511cfe3SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
17205511cfe3SLois Curfman McInnes 
17215511cfe3SLois Curfman McInnes    Options Database Keys:
1722db81eaa0SLois Curfman McInnes +  -mat_aij_no_inode  - Do not use inodes
1723db81eaa0SLois Curfman McInnes .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
1724db81eaa0SLois Curfman McInnes -  -mat_aij_oneindex - Internally use indexing starting at 1
1725db81eaa0SLois Curfman McInnes         rather than 0.  Note that when calling MatSetValues(),
1726db81eaa0SLois Curfman McInnes         the user still MUST index entries starting at 0!
1727494eafd4SBarry Smith .   -mat_mpi - use the parallel matrix data structures even on one processor
1728494eafd4SBarry Smith                (defaults to using SeqBAIJ format on one processor)
1729494eafd4SBarry Smith .   -mat_mpi - use the parallel matrix data structures even on one processor
1730494eafd4SBarry Smith                (defaults to using SeqAIJ format on one processor)
1731494eafd4SBarry Smith 
17325511cfe3SLois Curfman McInnes 
1733e0245417SLois Curfman McInnes    Storage Information:
1734e0245417SLois Curfman McInnes    For a square global matrix we define each processor's diagonal portion
1735e0245417SLois Curfman McInnes    to be its local rows and the corresponding columns (a square submatrix);
1736e0245417SLois Curfman McInnes    each processor's off-diagonal portion encompasses the remainder of the
1737e0245417SLois Curfman McInnes    local matrix (a rectangular submatrix).
1738e0245417SLois Curfman McInnes 
1739e0245417SLois Curfman McInnes    The user can specify preallocated storage for the diagonal part of
17405ace5be8SLois Curfman McInnes    the local submatrix with either d_nz or d_nnz (not both).  Set
17415ace5be8SLois Curfman McInnes    d_nz=PETSC_DEFAULT and d_nnz=PETSC_NULL for PETSc to control dynamic
17425ace5be8SLois Curfman McInnes    memory allocation.  Likewise, specify preallocated storage for the
17435ace5be8SLois Curfman McInnes    off-diagonal part of the local submatrix with o_nz or o_nnz (not both).
1744ff756334SLois Curfman McInnes 
17455511cfe3SLois Curfman McInnes    Consider a processor that owns rows 3, 4 and 5 of a parallel matrix. In
17465511cfe3SLois Curfman McInnes    the figure below we depict these three local rows and all columns (0-11).
17472191d07cSBarry Smith 
1748db81eaa0SLois Curfman McInnes .vb
1749db81eaa0SLois Curfman McInnes              0 1 2 3 4 5 6 7 8 9 10 11
1750db81eaa0SLois Curfman McInnes             -------------------
1751db81eaa0SLois Curfman McInnes      row 3  |  o o o d d d o o o o o o
1752db81eaa0SLois Curfman McInnes      row 4  |  o o o d d d o o o o o o
1753db81eaa0SLois Curfman McInnes      row 5  |  o o o d d d o o o o o o
1754db81eaa0SLois Curfman McInnes             -------------------
1755db81eaa0SLois Curfman McInnes .ve
1756b810aeb4SBarry Smith 
17575511cfe3SLois Curfman McInnes    Thus, any entries in the d locations are stored in the d (diagonal)
17585511cfe3SLois Curfman McInnes    submatrix, and any entries in the o locations are stored in the
17595511cfe3SLois Curfman McInnes    o (off-diagonal) submatrix.  Note that the d and the o submatrices are
17605511cfe3SLois Curfman McInnes    stored simply in the MATSEQAIJ format for compressed row storage.
17615511cfe3SLois Curfman McInnes 
17625511cfe3SLois Curfman McInnes    Now d_nz should indicate the number of nonzeros per row in the d matrix,
17635511cfe3SLois Curfman McInnes    and o_nz should indicate the number of nonzeros per row in the o matrix.
17645511cfe3SLois Curfman McInnes    In general, for PDE problems in which most nonzeros are near the diagonal,
17653d323bbdSBarry Smith    one expects d_nz >> o_nz. For large problems you MUST preallocate memory
176692e8d321SLois Curfman McInnes    or you will get TERRIBLE performance; see the users' manual chapter on
17676da5968aSLois Curfman McInnes    matrices.
17683a511b96SLois Curfman McInnes 
1769dbd7a890SLois Curfman McInnes .keywords: matrix, aij, compressed row, sparse, parallel
1770ff756334SLois Curfman McInnes 
1771fafbff53SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues()
17728a729477SBarry Smith @*/
1773e1311b90SBarry 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)
17748a729477SBarry Smith {
177544cd7ae7SLois Curfman McInnes   Mat          B;
177644cd7ae7SLois Curfman McInnes   Mat_MPIAIJ   *b;
17777be0e774SBarry Smith   int          ierr, i,sum[2],work[2],size,flag1 = 0, flag2 = 0;
1778416022c9SBarry Smith 
17793a40ed3dSBarry Smith   PetscFunctionBegin;
17803914022bSBarry Smith   MPI_Comm_size(comm,&size);
17817be0e774SBarry Smith   ierr = OptionsHasName(PETSC_NULL,"-mat_mpiaij",&flag1); CHKERRQ(ierr);
17827be0e774SBarry Smith   ierr = OptionsHasName(PETSC_NULL,"-mat_mpi",&flag2); CHKERRQ(ierr);
17837be0e774SBarry Smith   if (!flag1 && !flag2 && size == 1) {
17843914022bSBarry Smith     if (M == PETSC_DECIDE) M = m;
17853914022bSBarry Smith     if (N == PETSC_DECIDE) N = n;
17863914022bSBarry Smith     ierr = MatCreateSeqAIJ(comm,M,N,d_nz,d_nnz,A); CHKERRQ(ierr);
17873a40ed3dSBarry Smith     PetscFunctionReturn(0);
17883914022bSBarry Smith   }
17893914022bSBarry Smith 
179044cd7ae7SLois Curfman McInnes   *A = 0;
1791f830108cSBarry Smith   PetscHeaderCreate(B,_p_Mat,struct _MatOps,MAT_COOKIE,MATMPIAIJ,comm,MatDestroy,MatView);
179244cd7ae7SLois Curfman McInnes   PLogObjectCreate(B);
179344cd7ae7SLois Curfman McInnes   B->data            = (void *) (b = PetscNew(Mat_MPIAIJ)); CHKPTRQ(b);
179444cd7ae7SLois Curfman McInnes   PetscMemzero(b,sizeof(Mat_MPIAIJ));
1795cda55fadSBarry Smith   PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));
1796e1311b90SBarry Smith   B->ops->destroy    = MatDestroy_MPIAIJ;
1797e1311b90SBarry Smith   B->ops->view       = MatView_MPIAIJ;
179844cd7ae7SLois Curfman McInnes   B->factor          = 0;
179944cd7ae7SLois Curfman McInnes   B->assembled       = PETSC_FALSE;
180090f02eecSBarry Smith   B->mapping         = 0;
1801d6dfbf8fSBarry Smith 
180247794344SBarry Smith   B->insertmode      = NOT_SET_VALUES;
18039eb4d147SSatish Balay   b->size            = size;
180444cd7ae7SLois Curfman McInnes   MPI_Comm_rank(comm,&b->rank);
18051eb62cbbSBarry Smith 
18063a40ed3dSBarry Smith   if (m == PETSC_DECIDE && (d_nnz != PETSC_NULL || o_nnz != PETSC_NULL)) {
1807a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"Cannot have PETSC_DECIDE rows but set d_nnz or o_nnz");
18083a40ed3dSBarry Smith   }
18091987afe7SBarry Smith 
1810dbd7a890SLois Curfman McInnes   if (M == PETSC_DECIDE || N == PETSC_DECIDE) {
18111eb62cbbSBarry Smith     work[0] = m; work[1] = n;
1812ca161407SBarry Smith     ierr = MPI_Allreduce( work, sum,2,MPI_INT,MPI_SUM,comm );CHKERRQ(ierr);
1813dbd7a890SLois Curfman McInnes     if (M == PETSC_DECIDE) M = sum[0];
1814dbd7a890SLois Curfman McInnes     if (N == PETSC_DECIDE) N = sum[1];
18151eb62cbbSBarry Smith   }
181644cd7ae7SLois Curfman McInnes   if (m == PETSC_DECIDE) {m = M/b->size + ((M % b->size) > b->rank);}
181744cd7ae7SLois Curfman McInnes   if (n == PETSC_DECIDE) {n = N/b->size + ((N % b->size) > b->rank);}
181844cd7ae7SLois Curfman McInnes   b->m = m; B->m = m;
181944cd7ae7SLois Curfman McInnes   b->n = n; B->n = n;
182044cd7ae7SLois Curfman McInnes   b->N = N; B->N = N;
182144cd7ae7SLois Curfman McInnes   b->M = M; B->M = M;
18221eb62cbbSBarry Smith 
1823c7fcc2eaSBarry Smith   /* the information in the maps duplicates the information computed below, eventually
1824c7fcc2eaSBarry Smith      we should remove the duplicate information that is not contained in the maps */
1825253a16ddSBarry Smith   ierr = MapCreateMPI(comm,m,M,&B->rmap);CHKERRQ(ierr);
1826253a16ddSBarry Smith   ierr = MapCreateMPI(comm,n,N,&B->cmap);CHKERRQ(ierr);
1827c7fcc2eaSBarry Smith 
18281eb62cbbSBarry Smith   /* build local table of row and column ownerships */
182944cd7ae7SLois Curfman McInnes   b->rowners = (int *) PetscMalloc(2*(b->size+2)*sizeof(int)); CHKPTRQ(b->rowners);
1830f09e8eb9SSatish Balay   PLogObjectMemory(B,2*(b->size+2)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_MPIAIJ));
1831603f58a4SSatish Balay   b->cowners = b->rowners + b->size + 2;
1832ca161407SBarry Smith   ierr = MPI_Allgather(&m,1,MPI_INT,b->rowners+1,1,MPI_INT,comm);CHKERRQ(ierr);
183344cd7ae7SLois Curfman McInnes   b->rowners[0] = 0;
183444cd7ae7SLois Curfman McInnes   for ( i=2; i<=b->size; i++ ) {
183544cd7ae7SLois Curfman McInnes     b->rowners[i] += b->rowners[i-1];
18368a729477SBarry Smith   }
183744cd7ae7SLois Curfman McInnes   b->rstart = b->rowners[b->rank];
183844cd7ae7SLois Curfman McInnes   b->rend   = b->rowners[b->rank+1];
1839ca161407SBarry Smith   ierr = MPI_Allgather(&n,1,MPI_INT,b->cowners+1,1,MPI_INT,comm);CHKERRQ(ierr);
184044cd7ae7SLois Curfman McInnes   b->cowners[0] = 0;
184144cd7ae7SLois Curfman McInnes   for ( i=2; i<=b->size; i++ ) {
184244cd7ae7SLois Curfman McInnes     b->cowners[i] += b->cowners[i-1];
18438a729477SBarry Smith   }
184444cd7ae7SLois Curfman McInnes   b->cstart = b->cowners[b->rank];
184544cd7ae7SLois Curfman McInnes   b->cend   = b->cowners[b->rank+1];
18468a729477SBarry Smith 
18475ace5be8SLois Curfman McInnes   if (d_nz == PETSC_DEFAULT) d_nz = 5;
1848029af93fSBarry Smith   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,m,n,d_nz,d_nnz,&b->A); CHKERRQ(ierr);
184944cd7ae7SLois Curfman McInnes   PLogObjectParent(B,b->A);
18507b8455f0SLois Curfman McInnes   if (o_nz == PETSC_DEFAULT) o_nz = 0;
1851029af93fSBarry Smith   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,m,N,o_nz,o_nnz,&b->B); CHKERRQ(ierr);
185244cd7ae7SLois Curfman McInnes   PLogObjectParent(B,b->B);
18538a729477SBarry Smith 
18541eb62cbbSBarry Smith   /* build cache for off array entries formed */
185544cd7ae7SLois Curfman McInnes   ierr = StashBuild_Private(&b->stash); CHKERRQ(ierr);
185690f02eecSBarry Smith   b->donotstash  = 0;
185744cd7ae7SLois Curfman McInnes   b->colmap      = 0;
185844cd7ae7SLois Curfman McInnes   b->garray      = 0;
185944cd7ae7SLois Curfman McInnes   b->roworiented = 1;
18608a729477SBarry Smith 
18611eb62cbbSBarry Smith   /* stuff used for matrix vector multiply */
186244cd7ae7SLois Curfman McInnes   b->lvec      = 0;
186344cd7ae7SLois Curfman McInnes   b->Mvctx     = 0;
18648a729477SBarry Smith 
18657a0afa10SBarry Smith   /* stuff for MatGetRow() */
186644cd7ae7SLois Curfman McInnes   b->rowindices   = 0;
186744cd7ae7SLois Curfman McInnes   b->rowvalues    = 0;
186844cd7ae7SLois Curfman McInnes   b->getrowactive = PETSC_FALSE;
18697a0afa10SBarry Smith 
18702e8a6d31SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",
18712e8a6d31SBarry Smith                                      "MatStoreValues_MPIAIJ",
18722e8a6d31SBarry Smith                                      (void*)MatStoreValues_MPIAIJ);CHKERRQ(ierr);
18732e8a6d31SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",
18742e8a6d31SBarry Smith                                      "MatRetrieveValues_MPIAIJ",
18752e8a6d31SBarry Smith                                      (void*)MatRetrieveValues_MPIAIJ);CHKERRQ(ierr);
187644cd7ae7SLois Curfman McInnes   *A = B;
18773a40ed3dSBarry Smith   PetscFunctionReturn(0);
1878d6dfbf8fSBarry Smith }
1879c74985f6SBarry Smith 
18805615d1e5SSatish Balay #undef __FUNC__
18812e8a6d31SBarry Smith #define __FUNC__ "MatDuplicate_MPIAIJ"
18822e8a6d31SBarry Smith int MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
1883d6dfbf8fSBarry Smith {
1884d6dfbf8fSBarry Smith   Mat        mat;
1885416022c9SBarry Smith   Mat_MPIAIJ *a,*oldmat = (Mat_MPIAIJ *) matin->data;
1886a1b97e82SLois Curfman McInnes   int        ierr, len=0, flg;
1887d6dfbf8fSBarry Smith 
18883a40ed3dSBarry Smith   PetscFunctionBegin;
1889416022c9SBarry Smith   *newmat       = 0;
1890f830108cSBarry Smith   PetscHeaderCreate(mat,_p_Mat,struct _MatOps,MAT_COOKIE,MATMPIAIJ,matin->comm,MatDestroy,MatView);
1891a5a9c739SBarry Smith   PLogObjectCreate(mat);
18920452661fSBarry Smith   mat->data       = (void *) (a = PetscNew(Mat_MPIAIJ)); CHKPTRQ(a);
1893cda55fadSBarry Smith   PetscMemcpy(mat->ops,&MatOps_Values,sizeof(struct _MatOps));
1894e1311b90SBarry Smith   mat->ops->destroy    = MatDestroy_MPIAIJ;
1895e1311b90SBarry Smith   mat->ops->view       = MatView_MPIAIJ;
1896d6dfbf8fSBarry Smith   mat->factor     = matin->factor;
1897c456f294SBarry Smith   mat->assembled  = PETSC_TRUE;
1898d6dfbf8fSBarry Smith 
189944cd7ae7SLois Curfman McInnes   a->m = mat->m   = oldmat->m;
190044cd7ae7SLois Curfman McInnes   a->n = mat->n   = oldmat->n;
190144cd7ae7SLois Curfman McInnes   a->M = mat->M   = oldmat->M;
190244cd7ae7SLois Curfman McInnes   a->N = mat->N   = oldmat->N;
1903d6dfbf8fSBarry Smith 
1904416022c9SBarry Smith   a->rstart       = oldmat->rstart;
1905416022c9SBarry Smith   a->rend         = oldmat->rend;
1906416022c9SBarry Smith   a->cstart       = oldmat->cstart;
1907416022c9SBarry Smith   a->cend         = oldmat->cend;
190817699dbbSLois Curfman McInnes   a->size         = oldmat->size;
190917699dbbSLois Curfman McInnes   a->rank         = oldmat->rank;
191047794344SBarry Smith   mat->insertmode = NOT_SET_VALUES;
1911bcd2baecSBarry Smith   a->rowvalues    = 0;
1912bcd2baecSBarry Smith   a->getrowactive = PETSC_FALSE;
1913d6dfbf8fSBarry Smith 
1914603f58a4SSatish Balay   a->rowners = (int *) PetscMalloc(2*(a->size+2)*sizeof(int)); CHKPTRQ(a->rowners);
1915f09e8eb9SSatish Balay   PLogObjectMemory(mat,2*(a->size+2)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_MPIAIJ));
1916603f58a4SSatish Balay   a->cowners = a->rowners + a->size + 2;
1917603f58a4SSatish Balay   PetscMemcpy(a->rowners,oldmat->rowners,2*(a->size+2)*sizeof(int));
1918416022c9SBarry Smith   ierr = StashInitialize_Private(&a->stash); CHKERRQ(ierr);
19192ee70a88SLois Curfman McInnes   if (oldmat->colmap) {
19200452661fSBarry Smith     a->colmap = (int *) PetscMalloc((a->N)*sizeof(int));CHKPTRQ(a->colmap);
1921416022c9SBarry Smith     PLogObjectMemory(mat,(a->N)*sizeof(int));
1922416022c9SBarry Smith     PetscMemcpy(a->colmap,oldmat->colmap,(a->N)*sizeof(int));
1923416022c9SBarry Smith   } else a->colmap = 0;
19243f41c07dSBarry Smith   if (oldmat->garray) {
19253f41c07dSBarry Smith     len = ((Mat_SeqAIJ *) (oldmat->B->data))->n;
19263f41c07dSBarry Smith     a->garray = (int *) PetscMalloc((len+1)*sizeof(int)); CHKPTRQ(a->garray);
1927464493b3SBarry Smith     PLogObjectMemory(mat,len*sizeof(int));
19283f41c07dSBarry Smith     if (len) PetscMemcpy(a->garray,oldmat->garray,len*sizeof(int));
1929416022c9SBarry Smith   } else a->garray = 0;
1930d6dfbf8fSBarry Smith 
1931416022c9SBarry Smith   ierr =  VecDuplicate(oldmat->lvec,&a->lvec); CHKERRQ(ierr);
1932416022c9SBarry Smith   PLogObjectParent(mat,a->lvec);
1933a56f8943SBarry Smith   ierr =  VecScatterCopy(oldmat->Mvctx,&a->Mvctx); CHKERRQ(ierr);
1934416022c9SBarry Smith   PLogObjectParent(mat,a->Mvctx);
19352e8a6d31SBarry Smith   ierr =  MatDuplicate(oldmat->A,cpvalues,&a->A); CHKERRQ(ierr);
1936416022c9SBarry Smith   PLogObjectParent(mat,a->A);
19372e8a6d31SBarry Smith   ierr =  MatDuplicate(oldmat->B,cpvalues,&a->B); CHKERRQ(ierr);
1938416022c9SBarry Smith   PLogObjectParent(mat,a->B);
19395dd7a6c7SBarry Smith   ierr = OptionsHasName(PETSC_NULL,"-help",&flg); CHKERRQ(ierr);
194025cdf11fSBarry Smith   if (flg) {
1941682d7d0cSBarry Smith     ierr = MatPrintHelp(mat); CHKERRQ(ierr);
1942682d7d0cSBarry Smith   }
19438a729477SBarry Smith   *newmat = mat;
19443a40ed3dSBarry Smith   PetscFunctionReturn(0);
19458a729477SBarry Smith }
1946416022c9SBarry Smith 
194777c4ece6SBarry Smith #include "sys.h"
1948416022c9SBarry Smith 
19495615d1e5SSatish Balay #undef __FUNC__
19505615d1e5SSatish Balay #define __FUNC__ "MatLoad_MPIAIJ"
195119bcc07fSBarry Smith int MatLoad_MPIAIJ(Viewer viewer,MatType type,Mat *newmat)
1952416022c9SBarry Smith {
1953d65a2f8fSBarry Smith   Mat          A;
1954d65a2f8fSBarry Smith   Scalar       *vals,*svals;
195519bcc07fSBarry Smith   MPI_Comm     comm = ((PetscObject)viewer)->comm;
1956416022c9SBarry Smith   MPI_Status   status;
19573a40ed3dSBarry Smith   int          i, nz, ierr, j,rstart, rend, fd;
195817699dbbSLois Curfman McInnes   int          header[4],rank,size,*rowlengths = 0,M,N,m,*rowners,maxnz,*cols;
1959d65a2f8fSBarry Smith   int          *ourlens,*sndcounts = 0,*procsnz = 0, *offlens,jj,*mycols,*smycols;
1960*b362ba68SBarry Smith   int          tag = ((PetscObject)viewer)->tag,cend,cstart,n;
1961416022c9SBarry Smith 
19623a40ed3dSBarry Smith   PetscFunctionBegin;
196317699dbbSLois Curfman McInnes   MPI_Comm_size(comm,&size); MPI_Comm_rank(comm,&rank);
196417699dbbSLois Curfman McInnes   if (!rank) {
196590ace30eSBarry Smith     ierr = ViewerBinaryGetDescriptor(viewer,&fd); CHKERRQ(ierr);
19660752156aSBarry Smith     ierr = PetscBinaryRead(fd,(char *)header,4,PETSC_INT); CHKERRQ(ierr);
1967a8c6a408SBarry Smith     if (header[0] != MAT_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,0,"not matrix object");
1968d64ed03dSBarry Smith     if (header[3] < 0) {
1969a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_FILE_UNEXPECTED,1,"Matrix in special format on disk, cannot load as MPIAIJ");
1970d64ed03dSBarry Smith     }
19716c5fab8fSBarry Smith   }
19726c5fab8fSBarry Smith 
1973ca161407SBarry Smith   ierr = MPI_Bcast(header+1,3,MPI_INT,0,comm);CHKERRQ(ierr);
1974416022c9SBarry Smith   M = header[1]; N = header[2];
1975416022c9SBarry Smith   /* determine ownership of all rows */
197617699dbbSLois Curfman McInnes   m = M/size + ((M % size) > rank);
19770452661fSBarry Smith   rowners = (int *) PetscMalloc((size+2)*sizeof(int)); CHKPTRQ(rowners);
1978ca161407SBarry Smith   ierr = MPI_Allgather(&m,1,MPI_INT,rowners+1,1,MPI_INT,comm);CHKERRQ(ierr);
1979416022c9SBarry Smith   rowners[0] = 0;
198017699dbbSLois Curfman McInnes   for ( i=2; i<=size; i++ ) {
1981416022c9SBarry Smith     rowners[i] += rowners[i-1];
1982416022c9SBarry Smith   }
198317699dbbSLois Curfman McInnes   rstart = rowners[rank];
198417699dbbSLois Curfman McInnes   rend   = rowners[rank+1];
1985416022c9SBarry Smith 
1986416022c9SBarry Smith   /* distribute row lengths to all processors */
19870452661fSBarry Smith   ourlens = (int*) PetscMalloc( 2*(rend-rstart)*sizeof(int) ); CHKPTRQ(ourlens);
1988416022c9SBarry Smith   offlens = ourlens + (rend-rstart);
198917699dbbSLois Curfman McInnes   if (!rank) {
19900452661fSBarry Smith     rowlengths = (int*) PetscMalloc( M*sizeof(int) ); CHKPTRQ(rowlengths);
19910752156aSBarry Smith     ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT); CHKERRQ(ierr);
19920452661fSBarry Smith     sndcounts = (int*) PetscMalloc( size*sizeof(int) ); CHKPTRQ(sndcounts);
199317699dbbSLois Curfman McInnes     for ( i=0; i<size; i++ ) sndcounts[i] = rowners[i+1] - rowners[i];
1994ca161407SBarry Smith     ierr = MPI_Scatterv(rowlengths,sndcounts,rowners,MPI_INT,ourlens,rend-rstart,MPI_INT,0,comm);CHKERRQ(ierr);
19950452661fSBarry Smith     PetscFree(sndcounts);
19963a40ed3dSBarry Smith   } else {
1997ca161407SBarry Smith     ierr = MPI_Scatterv(0,0,0,MPI_INT,ourlens,rend-rstart,MPI_INT, 0,comm);CHKERRQ(ierr);
1998416022c9SBarry Smith   }
1999416022c9SBarry Smith 
200017699dbbSLois Curfman McInnes   if (!rank) {
2001416022c9SBarry Smith     /* calculate the number of nonzeros on each processor */
20020452661fSBarry Smith     procsnz = (int*) PetscMalloc( size*sizeof(int) ); CHKPTRQ(procsnz);
2003cddf8d76SBarry Smith     PetscMemzero(procsnz,size*sizeof(int));
200417699dbbSLois Curfman McInnes     for ( i=0; i<size; i++ ) {
2005416022c9SBarry Smith       for ( j=rowners[i]; j< rowners[i+1]; j++ ) {
2006416022c9SBarry Smith         procsnz[i] += rowlengths[j];
2007416022c9SBarry Smith       }
2008416022c9SBarry Smith     }
20090452661fSBarry Smith     PetscFree(rowlengths);
2010416022c9SBarry Smith 
2011416022c9SBarry Smith     /* determine max buffer needed and allocate it */
2012416022c9SBarry Smith     maxnz = 0;
201317699dbbSLois Curfman McInnes     for ( i=0; i<size; i++ ) {
20140452661fSBarry Smith       maxnz = PetscMax(maxnz,procsnz[i]);
2015416022c9SBarry Smith     }
20160452661fSBarry Smith     cols = (int *) PetscMalloc( maxnz*sizeof(int) ); CHKPTRQ(cols);
2017416022c9SBarry Smith 
2018416022c9SBarry Smith     /* read in my part of the matrix column indices  */
2019416022c9SBarry Smith     nz     = procsnz[0];
20200452661fSBarry Smith     mycols = (int *) PetscMalloc( nz*sizeof(int) ); CHKPTRQ(mycols);
20210752156aSBarry Smith     ierr   = PetscBinaryRead(fd,mycols,nz,PETSC_INT); CHKERRQ(ierr);
2022d65a2f8fSBarry Smith 
2023d65a2f8fSBarry Smith     /* read in every one elses and ship off */
202417699dbbSLois Curfman McInnes     for ( i=1; i<size; i++ ) {
2025d65a2f8fSBarry Smith       nz   = procsnz[i];
20260752156aSBarry Smith       ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT); CHKERRQ(ierr);
2027ca161407SBarry Smith       ierr = MPI_Send(cols,nz,MPI_INT,i,tag,comm);CHKERRQ(ierr);
2028d65a2f8fSBarry Smith     }
20290452661fSBarry Smith     PetscFree(cols);
20303a40ed3dSBarry Smith   } else {
2031416022c9SBarry Smith     /* determine buffer space needed for message */
2032416022c9SBarry Smith     nz = 0;
2033416022c9SBarry Smith     for ( i=0; i<m; i++ ) {
2034416022c9SBarry Smith       nz += ourlens[i];
2035416022c9SBarry Smith     }
20360452661fSBarry Smith     mycols = (int*) PetscMalloc( nz*sizeof(int) ); CHKPTRQ(mycols);
2037416022c9SBarry Smith 
2038416022c9SBarry Smith     /* receive message of column indices*/
2039ca161407SBarry Smith     ierr = MPI_Recv(mycols,nz,MPI_INT,0,tag,comm,&status);CHKERRQ(ierr);
2040ca161407SBarry Smith     ierr = MPI_Get_count(&status,MPI_INT,&maxnz);CHKERRQ(ierr);
2041a8c6a408SBarry Smith     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,0,"something is wrong with file");
2042416022c9SBarry Smith   }
2043416022c9SBarry Smith 
2044*b362ba68SBarry Smith   /* determine column ownership if matrix is not square */
2045*b362ba68SBarry Smith   if (N != M) {
2046*b362ba68SBarry Smith     n      = N/size + ((N % size) > rank);
2047*b362ba68SBarry Smith     ierr   = MPI_Scan(&n,&cend,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr);
2048*b362ba68SBarry Smith     cstart = cend - n;
2049*b362ba68SBarry Smith   } else {
2050*b362ba68SBarry Smith     cstart = rstart;
2051*b362ba68SBarry Smith     cend   = rend;
2052*b362ba68SBarry Smith   }
2053*b362ba68SBarry Smith 
2054416022c9SBarry Smith   /* loop over local rows, determining number of off diagonal entries */
2055cddf8d76SBarry Smith   PetscMemzero(offlens,m*sizeof(int));
2056416022c9SBarry Smith   jj = 0;
2057416022c9SBarry Smith   for ( i=0; i<m; i++ ) {
2058416022c9SBarry Smith     for ( j=0; j<ourlens[i]; j++ ) {
2059*b362ba68SBarry Smith       if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++;
2060416022c9SBarry Smith       jj++;
2061416022c9SBarry Smith     }
2062416022c9SBarry Smith   }
2063d65a2f8fSBarry Smith 
2064d65a2f8fSBarry Smith   /* create our matrix */
2065416022c9SBarry Smith   for ( i=0; i<m; i++ ) {
2066416022c9SBarry Smith     ourlens[i] -= offlens[i];
2067416022c9SBarry Smith   }
2068*b362ba68SBarry Smith   ierr = MatCreateMPIAIJ(comm,m,n,M,N,0,ourlens,0,offlens,newmat);CHKERRQ(ierr);
2069d65a2f8fSBarry Smith   A = *newmat;
20706d4a8577SBarry Smith   MatSetOption(A,MAT_COLUMNS_SORTED);
2071d65a2f8fSBarry Smith   for ( i=0; i<m; i++ ) {
2072d65a2f8fSBarry Smith     ourlens[i] += offlens[i];
2073d65a2f8fSBarry Smith   }
2074416022c9SBarry Smith 
207517699dbbSLois Curfman McInnes   if (!rank) {
20760452661fSBarry Smith     vals = (Scalar *) PetscMalloc( maxnz*sizeof(Scalar) ); CHKPTRQ(vals);
2077416022c9SBarry Smith 
2078416022c9SBarry Smith     /* read in my part of the matrix numerical values  */
2079416022c9SBarry Smith     nz = procsnz[0];
20800752156aSBarry Smith     ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR); CHKERRQ(ierr);
2081d65a2f8fSBarry Smith 
2082d65a2f8fSBarry Smith     /* insert into matrix */
2083d65a2f8fSBarry Smith     jj      = rstart;
2084d65a2f8fSBarry Smith     smycols = mycols;
2085d65a2f8fSBarry Smith     svals   = vals;
2086d65a2f8fSBarry Smith     for ( i=0; i<m; i++ ) {
2087d65a2f8fSBarry Smith       ierr = MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
2088d65a2f8fSBarry Smith       smycols += ourlens[i];
2089d65a2f8fSBarry Smith       svals   += ourlens[i];
2090d65a2f8fSBarry Smith       jj++;
2091416022c9SBarry Smith     }
2092416022c9SBarry Smith 
2093d65a2f8fSBarry Smith     /* read in other processors and ship out */
209417699dbbSLois Curfman McInnes     for ( i=1; i<size; i++ ) {
2095416022c9SBarry Smith       nz   = procsnz[i];
20960752156aSBarry Smith       ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR); CHKERRQ(ierr);
2097ca161407SBarry Smith       ierr = MPI_Send(vals,nz,MPIU_SCALAR,i,A->tag,comm);CHKERRQ(ierr);
2098416022c9SBarry Smith     }
20990452661fSBarry Smith     PetscFree(procsnz);
21003a40ed3dSBarry Smith   } else {
2101d65a2f8fSBarry Smith     /* receive numeric values */
21020452661fSBarry Smith     vals = (Scalar*) PetscMalloc( nz*sizeof(Scalar) ); CHKPTRQ(vals);
2103416022c9SBarry Smith 
2104d65a2f8fSBarry Smith     /* receive message of values*/
2105ca161407SBarry Smith     ierr = MPI_Recv(vals,nz,MPIU_SCALAR,0,A->tag,comm,&status);CHKERRQ(ierr);
2106ca161407SBarry Smith     ierr = MPI_Get_count(&status,MPIU_SCALAR,&maxnz);CHKERRQ(ierr);
2107a8c6a408SBarry Smith     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,0,"something is wrong with file");
2108d65a2f8fSBarry Smith 
2109d65a2f8fSBarry Smith     /* insert into matrix */
2110d65a2f8fSBarry Smith     jj      = rstart;
2111d65a2f8fSBarry Smith     smycols = mycols;
2112d65a2f8fSBarry Smith     svals   = vals;
2113d65a2f8fSBarry Smith     for ( i=0; i<m; i++ ) {
2114d65a2f8fSBarry Smith       ierr     = MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
2115d65a2f8fSBarry Smith       smycols += ourlens[i];
2116d65a2f8fSBarry Smith       svals   += ourlens[i];
2117d65a2f8fSBarry Smith       jj++;
2118d65a2f8fSBarry Smith     }
2119d65a2f8fSBarry Smith   }
21200452661fSBarry Smith   PetscFree(ourlens); PetscFree(vals); PetscFree(mycols); PetscFree(rowners);
2121d65a2f8fSBarry Smith 
21226d4a8577SBarry Smith   ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
21236d4a8577SBarry Smith   ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
21243a40ed3dSBarry Smith   PetscFunctionReturn(0);
2125416022c9SBarry Smith }
2126a0ff6018SBarry Smith 
212729da9460SBarry Smith #undef __FUNC__
212829da9460SBarry Smith #define __FUNC__ "MatGetSubMatrix_MPIAIJ"
2129a0ff6018SBarry Smith /*
213029da9460SBarry Smith     Not great since it makes two copies of the submatrix, first an SeqAIJ
213129da9460SBarry Smith   in local and then by concatenating the local matrices the end result.
213229da9460SBarry Smith   Writing it directly would be much like MatGetSubMatrices_MPIAIJ()
2133a0ff6018SBarry Smith */
21346a6a5d1dSBarry Smith int MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,int csize,MatGetSubMatrixCall call,Mat *newmat)
2135a0ff6018SBarry Smith {
213600e6dbe6SBarry Smith   int        ierr, i, m,n,rstart,row,rend,nz,*cwork,size,rank,j;
2137fee21e36SBarry Smith   Mat        *local,M, Mreuse;
213800e6dbe6SBarry Smith   Scalar     *vwork,*aa;
213900e6dbe6SBarry Smith   MPI_Comm   comm = mat->comm;
214000e6dbe6SBarry Smith   Mat_SeqAIJ *aij;
214100e6dbe6SBarry Smith   int        *ii, *jj,nlocal,*dlens,*olens,dlen,olen,jend;
2142a0ff6018SBarry Smith 
2143a0ff6018SBarry Smith   PetscFunctionBegin;
214400e6dbe6SBarry Smith   MPI_Comm_rank(comm,&rank);
214500e6dbe6SBarry Smith   MPI_Comm_size(comm,&size);
214600e6dbe6SBarry Smith 
2147fee21e36SBarry Smith   if (call ==  MAT_REUSE_MATRIX) {
2148fee21e36SBarry Smith     ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);CHKERRQ(ierr);
2149fee21e36SBarry Smith     if (!Mreuse) SETERRQ(1,1,"Submatrix passed in was not used before, cannot reuse");
2150fee21e36SBarry Smith     local = &Mreuse;
2151fee21e36SBarry Smith     ierr  = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&local);CHKERRQ(ierr);
2152fee21e36SBarry Smith   } else {
2153a0ff6018SBarry Smith     ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr);
2154fee21e36SBarry Smith     Mreuse = *local;
2155fee21e36SBarry Smith     PetscFree(local);
2156fee21e36SBarry Smith   }
2157a0ff6018SBarry Smith 
2158a0ff6018SBarry Smith   /*
2159a0ff6018SBarry Smith       m - number of local rows
2160a0ff6018SBarry Smith       n - number of columns (same on all processors)
2161a0ff6018SBarry Smith       rstart - first row in new global matrix generated
2162a0ff6018SBarry Smith   */
2163fee21e36SBarry Smith   ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr);
2164a0ff6018SBarry Smith   if (call == MAT_INITIAL_MATRIX) {
2165fee21e36SBarry Smith     aij = (Mat_SeqAIJ *) (Mreuse)->data;
2166a8c6a408SBarry Smith     if (aij->indexshift) SETERRQ(PETSC_ERR_SUP,1,"No support for index shifted matrix");
216700e6dbe6SBarry Smith     ii  = aij->i;
216800e6dbe6SBarry Smith     jj  = aij->j;
216900e6dbe6SBarry Smith 
2170a0ff6018SBarry Smith     /*
217100e6dbe6SBarry Smith         Determine the number of non-zeros in the diagonal and off-diagonal
217200e6dbe6SBarry Smith         portions of the matrix in order to do correct preallocation
2173a0ff6018SBarry Smith     */
217400e6dbe6SBarry Smith 
217500e6dbe6SBarry Smith     /* first get start and end of "diagonal" columns */
21766a6a5d1dSBarry Smith     if (csize == PETSC_DECIDE) {
217700e6dbe6SBarry Smith       nlocal = n/size + ((n % size) > rank);
21786a6a5d1dSBarry Smith     } else {
21796a6a5d1dSBarry Smith       nlocal = csize;
21806a6a5d1dSBarry Smith     }
2181ca161407SBarry Smith     ierr   = MPI_Scan(&nlocal,&rend,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr);
218200e6dbe6SBarry Smith     rstart = rend - nlocal;
21836a6a5d1dSBarry Smith     if (rank == size - 1 && rend != n) {
21846a6a5d1dSBarry Smith       SETERRQ(1,1,"Local column sizes do not add up to total number of columns");
21856a6a5d1dSBarry Smith     }
218600e6dbe6SBarry Smith 
218700e6dbe6SBarry Smith     /* next, compute all the lengths */
218800e6dbe6SBarry Smith     dlens = (int *) PetscMalloc( (2*m+1)*sizeof(int) );CHKPTRQ(dlens);
218900e6dbe6SBarry Smith     olens = dlens + m;
219000e6dbe6SBarry Smith     for ( i=0; i<m; i++ ) {
219100e6dbe6SBarry Smith       jend = ii[i+1] - ii[i];
219200e6dbe6SBarry Smith       olen = 0;
219300e6dbe6SBarry Smith       dlen = 0;
219400e6dbe6SBarry Smith       for ( j=0; j<jend; j++ ) {
219500e6dbe6SBarry Smith         if ( *jj < rstart || *jj >= rend) olen++;
219600e6dbe6SBarry Smith         else dlen++;
219700e6dbe6SBarry Smith         jj++;
219800e6dbe6SBarry Smith       }
219900e6dbe6SBarry Smith       olens[i] = olen;
220000e6dbe6SBarry Smith       dlens[i] = dlen;
220100e6dbe6SBarry Smith     }
220200e6dbe6SBarry Smith     ierr = MatCreateMPIAIJ(comm,m,PETSC_DECIDE,PETSC_DECIDE,n,0,dlens,0,olens,&M);CHKERRQ(ierr);
220300e6dbe6SBarry Smith     PetscFree(dlens);
2204a0ff6018SBarry Smith   } else {
2205a0ff6018SBarry Smith     int ml,nl;
2206a0ff6018SBarry Smith 
2207a0ff6018SBarry Smith     M = *newmat;
2208a0ff6018SBarry Smith     ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr);
2209a8c6a408SBarry Smith     if (ml != m) SETERRQ(PETSC_ERR_ARG_SIZ,1,"Previous matrix must be same size/layout as request");
2210a0ff6018SBarry Smith     ierr = MatZeroEntries(M);CHKERRQ(ierr);
2211c48de900SBarry Smith     /*
2212c48de900SBarry Smith          The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
2213c48de900SBarry Smith        rather than the slower MatSetValues().
2214c48de900SBarry Smith     */
2215c48de900SBarry Smith     M->was_assembled = PETSC_TRUE;
2216c48de900SBarry Smith     M->assembled     = PETSC_FALSE;
2217a0ff6018SBarry Smith   }
2218a0ff6018SBarry Smith   ierr = MatGetOwnershipRange(M,&rstart,&rend); CHKERRQ(ierr);
2219fee21e36SBarry Smith   aij = (Mat_SeqAIJ *) (Mreuse)->data;
2220a8c6a408SBarry Smith   if (aij->indexshift) SETERRQ(PETSC_ERR_SUP,1,"No support for index shifted matrix");
222100e6dbe6SBarry Smith   ii  = aij->i;
222200e6dbe6SBarry Smith   jj  = aij->j;
222300e6dbe6SBarry Smith   aa  = aij->a;
2224a0ff6018SBarry Smith   for (i=0; i<m; i++) {
2225a0ff6018SBarry Smith     row   = rstart + i;
222600e6dbe6SBarry Smith     nz    = ii[i+1] - ii[i];
222700e6dbe6SBarry Smith     cwork = jj;     jj += nz;
222800e6dbe6SBarry Smith     vwork = aa;     aa += nz;
22298c638d02SBarry Smith     ierr = MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES); CHKERRQ(ierr);
2230a0ff6018SBarry Smith   }
2231a0ff6018SBarry Smith 
2232a0ff6018SBarry Smith   ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
2233a0ff6018SBarry Smith   ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
2234a0ff6018SBarry Smith   *newmat = M;
2235fee21e36SBarry Smith 
2236fee21e36SBarry Smith   /* save submatrix used in processor for next request */
2237fee21e36SBarry Smith   if (call ==  MAT_INITIAL_MATRIX) {
2238fee21e36SBarry Smith     ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr);
2239fee21e36SBarry Smith     ierr = PetscObjectDereference((PetscObject)Mreuse);CHKERRQ(ierr);
2240fee21e36SBarry Smith   }
2241fee21e36SBarry Smith 
2242a0ff6018SBarry Smith   PetscFunctionReturn(0);
2243a0ff6018SBarry Smith }
2244