xref: /petsc/src/mat/impls/aij/mpi/mpiaij.c (revision a2d1c673a0257ed9b5719df99a4d7488fb13e120)
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*a2d1c673SSatish Balay static char vcid[] = "$Id: mpiaij.c,v 1.286 1999/03/09 21:32:49 balay Exp balay $";
3cb512458SBarry Smith #endif
48a729477SBarry Smith 
570f55243SBarry Smith #include "src/mat/impls/aij/mpi/mpiaij.h"
6f5eb4b81SSatish Balay #include "src/vec/vecimpl.h"
7d9942c19SSatish Balay #include "src/inline/spops.h"
88a729477SBarry Smith 
9bc5ccf88SSatish Balay extern int MatSetUpMultiply_MPIAIJ(Mat);
10bc5ccf88SSatish Balay extern int DisAssemble_MPIAIJ(Mat);
11bc5ccf88SSatish Balay extern int MatSetValues_SeqAIJ(Mat,int,int*,int,int*,Scalar*,InsertMode);
12bc5ccf88SSatish Balay extern int MatGetRow_SeqAIJ(Mat,int,int*,int**,Scalar**);
13bc5ccf88SSatish Balay extern int MatRestoreRow_SeqAIJ(Mat,int,int*,int**,Scalar**);
14bc5ccf88SSatish Balay extern int MatPrintHelp_SeqAIJ(Mat);
15bc5ccf88SSatish Balay 
169e25ed09SBarry Smith /* local utility routine that creates a mapping from the global column
179e25ed09SBarry Smith number to the local number in the off-diagonal part of the local
189e25ed09SBarry Smith storage of the matrix.  This is done in a non scable way since the
199e25ed09SBarry Smith length of colmap equals the global matrix length.
209e25ed09SBarry Smith */
215615d1e5SSatish Balay #undef __FUNC__
22d4bb536fSBarry Smith #define __FUNC__ "CreateColmap_MPIAIJ_Private"
230a198c4cSBarry Smith int CreateColmap_MPIAIJ_Private(Mat mat)
249e25ed09SBarry Smith {
2544a69424SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data;
26ec8511deSBarry Smith   Mat_SeqAIJ *B = (Mat_SeqAIJ*) aij->B->data;
276dee3f7eSBarry Smith   int        n = B->n,i;
286dee3f7eSBarry Smith #if defined (USE_CTABLE)
296dee3f7eSBarry Smith   int        ierr;
306dee3f7eSBarry Smith #endif
31dbb450caSBarry Smith 
323a40ed3dSBarry Smith   PetscFunctionBegin;
33b1fc9764SSatish Balay #if defined (USE_CTABLE)
34fa46199cSSatish Balay   ierr = TableCreate(aij->n/5,&aij->colmap); CHKERRQ(ierr);
35b1fc9764SSatish Balay   for ( i=0; i<n; i++ ){
36b1fc9764SSatish Balay     ierr = TableAdd(aij->colmap,aij->garray[i]+1,i+1);CHKERRQ(ierr);
37b1fc9764SSatish Balay   }
38b1fc9764SSatish Balay #else
39758f045eSSatish Balay   aij->colmap = (int *) PetscMalloc((aij->N+1)*sizeof(int));CHKPTRQ(aij->colmap);
40464493b3SBarry Smith   PLogObjectMemory(mat,aij->N*sizeof(int));
41cddf8d76SBarry Smith   PetscMemzero(aij->colmap,aij->N*sizeof(int));
42905e6a2fSBarry Smith   for ( i=0; i<n; i++ ) aij->colmap[aij->garray[i]] = i+1;
43b1fc9764SSatish Balay #endif
443a40ed3dSBarry Smith   PetscFunctionReturn(0);
459e25ed09SBarry Smith }
469e25ed09SBarry Smith 
470520107fSSatish Balay #define CHUNKSIZE   15
4830770e4dSSatish Balay #define MatSetValues_SeqAIJ_A_Private(row,col,value,addv) \
490520107fSSatish Balay { \
500520107fSSatish Balay  \
510520107fSSatish Balay     rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift; \
5230770e4dSSatish Balay     rmax = aimax[row]; nrow = ailen[row];  \
53f5e9677aSSatish Balay     col1 = col - shift; \
54f5e9677aSSatish Balay      \
55ba4e3ef2SSatish Balay     low = 0; high = nrow; \
56ba4e3ef2SSatish Balay     while (high-low > 5) { \
57ba4e3ef2SSatish Balay       t = (low+high)/2; \
58ba4e3ef2SSatish Balay       if (rp[t] > col) high = t; \
59ba4e3ef2SSatish Balay       else             low  = t; \
60ba4e3ef2SSatish Balay     } \
610520107fSSatish Balay       for ( _i=0; _i<nrow; _i++ ) { \
62f5e9677aSSatish Balay         if (rp[_i] > col1) break; \
63f5e9677aSSatish Balay         if (rp[_i] == col1) { \
640520107fSSatish Balay           if (addv == ADD_VALUES) ap[_i] += value;   \
650520107fSSatish Balay           else                  ap[_i] = value; \
6630770e4dSSatish Balay           goto a_noinsert; \
670520107fSSatish Balay         } \
680520107fSSatish Balay       }  \
6989280ab3SLois Curfman McInnes       if (nonew == 1) goto a_noinsert; \
70a8c6a408SBarry Smith       else if (nonew == -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Inserting a new nonzero into matrix"); \
710520107fSSatish Balay       if (nrow >= rmax) { \
720520107fSSatish Balay         /* there is no extra room in row, therefore enlarge */ \
730520107fSSatish Balay         int    new_nz = ai[a->m] + CHUNKSIZE,len,*new_i,*new_j; \
740520107fSSatish Balay         Scalar *new_a; \
750520107fSSatish Balay  \
76a8c6a408SBarry Smith         if (nonew == -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Inserting a new nonzero in the matrix"); \
7789280ab3SLois Curfman McInnes  \
780520107fSSatish Balay         /* malloc new storage space */ \
790520107fSSatish Balay         len     = new_nz*(sizeof(int)+sizeof(Scalar))+(a->m+1)*sizeof(int); \
800520107fSSatish Balay         new_a   = (Scalar *) PetscMalloc( len ); CHKPTRQ(new_a); \
810520107fSSatish Balay         new_j   = (int *) (new_a + new_nz); \
820520107fSSatish Balay         new_i   = new_j + new_nz; \
830520107fSSatish Balay  \
840520107fSSatish Balay         /* copy over old data into new slots */ \
850520107fSSatish Balay         for ( ii=0; ii<row+1; ii++ ) {new_i[ii] = ai[ii];} \
860520107fSSatish Balay         for ( ii=row+1; ii<a->m+1; ii++ ) {new_i[ii] = ai[ii]+CHUNKSIZE;} \
870520107fSSatish Balay         PetscMemcpy(new_j,aj,(ai[row]+nrow+shift)*sizeof(int)); \
880520107fSSatish Balay         len = (new_nz - CHUNKSIZE - ai[row] - nrow - shift); \
890520107fSSatish Balay         PetscMemcpy(new_j+ai[row]+shift+nrow+CHUNKSIZE,aj+ai[row]+shift+nrow, \
900520107fSSatish Balay                                                            len*sizeof(int)); \
910520107fSSatish Balay         PetscMemcpy(new_a,aa,(ai[row]+nrow+shift)*sizeof(Scalar)); \
920520107fSSatish Balay         PetscMemcpy(new_a+ai[row]+shift+nrow+CHUNKSIZE,aa+ai[row]+shift+nrow, \
930520107fSSatish Balay                                                            len*sizeof(Scalar));  \
940520107fSSatish Balay         /* free up old matrix storage */ \
95f5e9677aSSatish Balay  \
960520107fSSatish Balay         PetscFree(a->a);  \
970520107fSSatish Balay         if (!a->singlemalloc) {PetscFree(a->i);PetscFree(a->j);} \
980520107fSSatish Balay         aa = a->a = new_a; ai = a->i = new_i; aj = a->j = new_j;  \
990520107fSSatish Balay         a->singlemalloc = 1; \
1000520107fSSatish Balay  \
1010520107fSSatish Balay         rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift; \
10230770e4dSSatish Balay         rmax = aimax[row] = aimax[row] + CHUNKSIZE; \
1030520107fSSatish Balay         PLogObjectMemory(A,CHUNKSIZE*(sizeof(int) + sizeof(Scalar))); \
1040520107fSSatish Balay         a->maxnz += CHUNKSIZE; \
1050520107fSSatish Balay         a->reallocs++; \
1060520107fSSatish Balay       } \
1070520107fSSatish Balay       N = nrow++ - 1; a->nz++; \
1080520107fSSatish Balay       /* shift up all the later entries in this row */ \
1090520107fSSatish Balay       for ( ii=N; ii>=_i; ii-- ) { \
1100520107fSSatish Balay         rp[ii+1] = rp[ii]; \
1110520107fSSatish Balay         ap[ii+1] = ap[ii]; \
1120520107fSSatish Balay       } \
113f5e9677aSSatish Balay       rp[_i] = col1;  \
1140520107fSSatish Balay       ap[_i] = value;  \
11530770e4dSSatish Balay       a_noinsert: ; \
1160520107fSSatish Balay       ailen[row] = nrow; \
1170520107fSSatish Balay }
1180a198c4cSBarry Smith 
11930770e4dSSatish Balay #define MatSetValues_SeqAIJ_B_Private(row,col,value,addv) \
12030770e4dSSatish Balay { \
12130770e4dSSatish Balay  \
12230770e4dSSatish Balay     rp   = bj + bi[row] + shift; ap = ba + bi[row] + shift; \
12330770e4dSSatish Balay     rmax = bimax[row]; nrow = bilen[row];  \
12430770e4dSSatish Balay     col1 = col - shift; \
12530770e4dSSatish Balay      \
126ba4e3ef2SSatish Balay     low = 0; high = nrow; \
127ba4e3ef2SSatish Balay     while (high-low > 5) { \
128ba4e3ef2SSatish Balay       t = (low+high)/2; \
129ba4e3ef2SSatish Balay       if (rp[t] > col) high = t; \
130ba4e3ef2SSatish Balay       else             low  = t; \
131ba4e3ef2SSatish Balay     } \
13230770e4dSSatish Balay        for ( _i=0; _i<nrow; _i++ ) { \
13330770e4dSSatish Balay         if (rp[_i] > col1) break; \
13430770e4dSSatish Balay         if (rp[_i] == col1) { \
13530770e4dSSatish Balay           if (addv == ADD_VALUES) ap[_i] += value;   \
13630770e4dSSatish Balay           else                  ap[_i] = value; \
13730770e4dSSatish Balay           goto b_noinsert; \
13830770e4dSSatish Balay         } \
13930770e4dSSatish Balay       }  \
14089280ab3SLois Curfman McInnes       if (nonew == 1) goto b_noinsert; \
141a8c6a408SBarry Smith       else if (nonew == -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Inserting a new nonzero into matrix"); \
14230770e4dSSatish Balay       if (nrow >= rmax) { \
14330770e4dSSatish Balay         /* there is no extra room in row, therefore enlarge */ \
14474c639caSSatish Balay         int    new_nz = bi[b->m] + CHUNKSIZE,len,*new_i,*new_j; \
14530770e4dSSatish Balay         Scalar *new_a; \
14630770e4dSSatish Balay  \
147a8c6a408SBarry Smith         if (nonew == -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Inserting a new nonzero in the matrix"); \
14889280ab3SLois Curfman McInnes  \
14930770e4dSSatish Balay         /* malloc new storage space */ \
15074c639caSSatish Balay         len     = new_nz*(sizeof(int)+sizeof(Scalar))+(b->m+1)*sizeof(int); \
15130770e4dSSatish Balay         new_a   = (Scalar *) PetscMalloc( len ); CHKPTRQ(new_a); \
15230770e4dSSatish Balay         new_j   = (int *) (new_a + new_nz); \
15330770e4dSSatish Balay         new_i   = new_j + new_nz; \
15430770e4dSSatish Balay  \
15530770e4dSSatish Balay         /* copy over old data into new slots */ \
15630770e4dSSatish Balay         for ( ii=0; ii<row+1; ii++ ) {new_i[ii] = bi[ii];} \
15774c639caSSatish Balay         for ( ii=row+1; ii<b->m+1; ii++ ) {new_i[ii] = bi[ii]+CHUNKSIZE;} \
15830770e4dSSatish Balay         PetscMemcpy(new_j,bj,(bi[row]+nrow+shift)*sizeof(int)); \
15930770e4dSSatish Balay         len = (new_nz - CHUNKSIZE - bi[row] - nrow - shift); \
16030770e4dSSatish Balay         PetscMemcpy(new_j+bi[row]+shift+nrow+CHUNKSIZE,bj+bi[row]+shift+nrow, \
16130770e4dSSatish Balay                                                            len*sizeof(int)); \
16230770e4dSSatish Balay         PetscMemcpy(new_a,ba,(bi[row]+nrow+shift)*sizeof(Scalar)); \
16330770e4dSSatish Balay         PetscMemcpy(new_a+bi[row]+shift+nrow+CHUNKSIZE,ba+bi[row]+shift+nrow, \
16430770e4dSSatish Balay                                                            len*sizeof(Scalar));  \
16530770e4dSSatish Balay         /* free up old matrix storage */ \
16630770e4dSSatish Balay  \
16774c639caSSatish Balay         PetscFree(b->a);  \
16874c639caSSatish Balay         if (!b->singlemalloc) {PetscFree(b->i);PetscFree(b->j);} \
16974c639caSSatish Balay         ba = b->a = new_a; bi = b->i = new_i; bj = b->j = new_j;  \
17074c639caSSatish Balay         b->singlemalloc = 1; \
17130770e4dSSatish Balay  \
17230770e4dSSatish Balay         rp   = bj + bi[row] + shift; ap = ba + bi[row] + shift; \
17330770e4dSSatish Balay         rmax = bimax[row] = bimax[row] + CHUNKSIZE; \
17474c639caSSatish Balay         PLogObjectMemory(B,CHUNKSIZE*(sizeof(int) + sizeof(Scalar))); \
17574c639caSSatish Balay         b->maxnz += CHUNKSIZE; \
17674c639caSSatish Balay         b->reallocs++; \
17730770e4dSSatish Balay       } \
17874c639caSSatish Balay       N = nrow++ - 1; b->nz++; \
17930770e4dSSatish Balay       /* shift up all the later entries in this row */ \
18030770e4dSSatish Balay       for ( ii=N; ii>=_i; ii-- ) { \
18130770e4dSSatish Balay         rp[ii+1] = rp[ii]; \
18230770e4dSSatish Balay         ap[ii+1] = ap[ii]; \
18330770e4dSSatish Balay       } \
18430770e4dSSatish Balay       rp[_i] = col1;  \
18530770e4dSSatish Balay       ap[_i] = value;  \
18630770e4dSSatish Balay       b_noinsert: ; \
18730770e4dSSatish Balay       bilen[row] = nrow; \
18830770e4dSSatish Balay }
18930770e4dSSatish Balay 
1905615d1e5SSatish Balay #undef __FUNC__
1915615d1e5SSatish Balay #define __FUNC__ "MatSetValues_MPIAIJ"
1928f6be9afSLois Curfman McInnes int MatSetValues_MPIAIJ(Mat mat,int m,int *im,int n,int *in,Scalar *v,InsertMode addv)
1938a729477SBarry Smith {
19444a69424SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data;
1954b0e389bSBarry Smith   Scalar     value;
1961eb62cbbSBarry Smith   int        ierr,i,j, rstart = aij->rstart, rend = aij->rend;
1971eb62cbbSBarry Smith   int        cstart = aij->cstart, cend = aij->cend,row,col;
198905e6a2fSBarry Smith   int        roworiented = aij->roworiented;
1998a729477SBarry Smith 
2000520107fSSatish Balay   /* Some Variables required in the macro */
2014ee7247eSSatish Balay   Mat        A = aij->A;
2024ee7247eSSatish Balay   Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data;
20330770e4dSSatish Balay   int        *aimax = a->imax, *ai = a->i, *ailen = a->ilen,*aj = a->j;
20430770e4dSSatish Balay   Scalar     *aa = a->a;
20530770e4dSSatish Balay 
20630770e4dSSatish Balay   Mat        B = aij->B;
20730770e4dSSatish Balay   Mat_SeqAIJ *b = (Mat_SeqAIJ *) B->data;
20830770e4dSSatish Balay   int        *bimax = b->imax, *bi = b->i, *bilen = b->ilen,*bj = b->j;
20930770e4dSSatish Balay   Scalar     *ba = b->a;
21030770e4dSSatish Balay 
211ba4e3ef2SSatish Balay   int        *rp,ii,nrow,_i,rmax, N, col1,low,high,t;
21230770e4dSSatish Balay   int        nonew = a->nonew,shift = a->indexshift;
21330770e4dSSatish Balay   Scalar     *ap;
2144ee7247eSSatish Balay 
2153a40ed3dSBarry Smith   PetscFunctionBegin;
2168a729477SBarry Smith   for ( i=0; i<m; i++ ) {
2175ef9f2a5SBarry Smith     if (im[i] < 0) continue;
2183a40ed3dSBarry Smith #if defined(USE_PETSC_BOPT_g)
219a8c6a408SBarry Smith     if (im[i] >= aij->M) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Row too large");
2200a198c4cSBarry Smith #endif
2214b0e389bSBarry Smith     if (im[i] >= rstart && im[i] < rend) {
2224b0e389bSBarry Smith       row = im[i] - rstart;
2231eb62cbbSBarry Smith       for ( j=0; j<n; j++ ) {
2244b0e389bSBarry Smith         if (in[j] >= cstart && in[j] < cend){
2254b0e389bSBarry Smith           col = in[j] - cstart;
2264b0e389bSBarry Smith           if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
22730770e4dSSatish Balay           MatSetValues_SeqAIJ_A_Private(row,col,value,addv);
2280520107fSSatish Balay           /* ierr = MatSetValues_SeqAIJ(aij->A,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */
2291eb62cbbSBarry Smith         }
2305ef9f2a5SBarry Smith         else if (in[j] < 0) continue;
2313a40ed3dSBarry Smith #if defined(USE_PETSC_BOPT_g)
232a8c6a408SBarry Smith         else if (in[j] >= aij->N) {SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Column too large");}
2330a198c4cSBarry Smith #endif
2341eb62cbbSBarry Smith         else {
235227d817aSBarry Smith           if (mat->was_assembled) {
236905e6a2fSBarry Smith             if (!aij->colmap) {
237905e6a2fSBarry Smith               ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
238905e6a2fSBarry Smith             }
239b1fc9764SSatish Balay #if defined (USE_CTABLE)
240fa46199cSSatish Balay             ierr = TableFind(aij->colmap,in[j]+1,&col); CHKERRQ(ierr);
241fa46199cSSatish Balay 	    col--;
242b1fc9764SSatish Balay #else
243905e6a2fSBarry Smith             col = aij->colmap[in[j]] - 1;
244b1fc9764SSatish Balay #endif
245ec8511deSBarry Smith             if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
2462493cbb0SBarry Smith               ierr = DisAssemble_MPIAIJ(mat); CHKERRQ(ierr);
2474b0e389bSBarry Smith               col =  in[j];
2489bf004c3SSatish Balay               /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
249f9508a3cSSatish Balay               B = aij->B;
250f9508a3cSSatish Balay               b = (Mat_SeqAIJ *) B->data;
251f9508a3cSSatish Balay               bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j;
252f9508a3cSSatish Balay               ba = b->a;
253d6dfbf8fSBarry Smith             }
254c48de900SBarry Smith           } else col = in[j];
2554b0e389bSBarry Smith           if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
25630770e4dSSatish Balay           MatSetValues_SeqAIJ_B_Private(row,col,value,addv);
25730770e4dSSatish Balay           /* ierr = MatSetValues_SeqAIJ(aij->B,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */
2581eb62cbbSBarry Smith         }
2591eb62cbbSBarry Smith       }
2605ef9f2a5SBarry Smith     } else {
26190f02eecSBarry Smith       if (roworiented && !aij->donotstash) {
262*a2d1c673SSatish Balay         ierr = StashValues_Private(&aij->stash,im[i],n,in,v+i*n);CHKERRQ(ierr);
2635ef9f2a5SBarry Smith       } else {
26490f02eecSBarry Smith         if (!aij->donotstash) {
2654b0e389bSBarry Smith           row = im[i];
2664b0e389bSBarry Smith           for ( j=0; j<n; j++ ) {
267*a2d1c673SSatish Balay             ierr = StashValues_Private(&aij->stash,row,1,in+j,v+i+j*m);CHKERRQ(ierr);
2684b0e389bSBarry Smith           }
2694b0e389bSBarry Smith         }
2701eb62cbbSBarry Smith       }
2718a729477SBarry Smith     }
27290f02eecSBarry Smith   }
2733a40ed3dSBarry Smith   PetscFunctionReturn(0);
2748a729477SBarry Smith }
2758a729477SBarry Smith 
2765615d1e5SSatish Balay #undef __FUNC__
2775615d1e5SSatish Balay #define __FUNC__ "MatGetValues_MPIAIJ"
2788f6be9afSLois Curfman McInnes int MatGetValues_MPIAIJ(Mat mat,int m,int *idxm,int n,int *idxn,Scalar *v)
279b49de8d1SLois Curfman McInnes {
280b49de8d1SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data;
281b49de8d1SLois Curfman McInnes   int        ierr,i,j, rstart = aij->rstart, rend = aij->rend;
282b49de8d1SLois Curfman McInnes   int        cstart = aij->cstart, cend = aij->cend,row,col;
283b49de8d1SLois Curfman McInnes 
2843a40ed3dSBarry Smith   PetscFunctionBegin;
285b49de8d1SLois Curfman McInnes   for ( i=0; i<m; i++ ) {
286a8c6a408SBarry Smith     if (idxm[i] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Negative row");
287a8c6a408SBarry Smith     if (idxm[i] >= aij->M) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Row too large");
288b49de8d1SLois Curfman McInnes     if (idxm[i] >= rstart && idxm[i] < rend) {
289b49de8d1SLois Curfman McInnes       row = idxm[i] - rstart;
290b49de8d1SLois Curfman McInnes       for ( j=0; j<n; j++ ) {
291a8c6a408SBarry Smith         if (idxn[j] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Negative column");
292a8c6a408SBarry Smith         if (idxn[j] >= aij->N) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Column too large");
293b49de8d1SLois Curfman McInnes         if (idxn[j] >= cstart && idxn[j] < cend){
294b49de8d1SLois Curfman McInnes           col = idxn[j] - cstart;
295b49de8d1SLois Curfman McInnes           ierr = MatGetValues(aij->A,1,&row,1,&col,v+i*n+j); CHKERRQ(ierr);
296fa852ad4SSatish Balay         } else {
297905e6a2fSBarry Smith           if (!aij->colmap) {
298905e6a2fSBarry Smith             ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
299905e6a2fSBarry Smith           }
300b1fc9764SSatish Balay #if defined (USE_CTABLE)
301fa46199cSSatish Balay           ierr = TableFind(aij->colmap,idxn[j]+1,&col); CHKERRQ(ierr);
302fa46199cSSatish Balay           col --;
303b1fc9764SSatish Balay #else
304905e6a2fSBarry Smith           col = aij->colmap[idxn[j]] - 1;
305b1fc9764SSatish Balay #endif
306e60e1c95SSatish Balay           if ((col < 0) || (aij->garray[col] != idxn[j])) *(v+i*n+j) = 0.0;
307d9d09a02SSatish Balay           else {
308b49de8d1SLois Curfman McInnes             ierr = MatGetValues(aij->B,1,&row,1,&col,v+i*n+j); CHKERRQ(ierr);
309b49de8d1SLois Curfman McInnes           }
310b49de8d1SLois Curfman McInnes         }
311b49de8d1SLois Curfman McInnes       }
312a8c6a408SBarry Smith     } else {
313a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_SUP,0,"Only local values currently supported");
314b49de8d1SLois Curfman McInnes     }
315b49de8d1SLois Curfman McInnes   }
3163a40ed3dSBarry Smith   PetscFunctionReturn(0);
317b49de8d1SLois Curfman McInnes }
318bc5ccf88SSatish Balay 
319bc5ccf88SSatish Balay #undef __FUNC__
320bc5ccf88SSatish Balay #define __FUNC__ "MatAssemblyBegin_MPIAIJ"
321bc5ccf88SSatish Balay int MatAssemblyBegin_MPIAIJ(Mat mat,MatAssemblyType mode)
322bc5ccf88SSatish Balay {
323bc5ccf88SSatish Balay   Mat_MPIAIJ  *aij = (Mat_MPIAIJ *) mat->data;
324bc5ccf88SSatish Balay   int         ierr;
325bc5ccf88SSatish Balay   InsertMode  addv;
326bc5ccf88SSatish Balay 
327bc5ccf88SSatish Balay   PetscFunctionBegin;
328bc5ccf88SSatish Balay   if (aij->donotstash) {
329bc5ccf88SSatish Balay     PetscFunctionReturn(0);
330bc5ccf88SSatish Balay   }
331bc5ccf88SSatish Balay 
332bc5ccf88SSatish Balay   /* make sure all processors are either in INSERTMODE or ADDMODE */
333bc5ccf88SSatish Balay   ierr = MPI_Allreduce(&mat->insertmode,&addv,1,MPI_INT,MPI_BOR,mat->comm);CHKERRQ(ierr);
334bc5ccf88SSatish Balay   if (addv == (ADD_VALUES|INSERT_VALUES)) {
335bc5ccf88SSatish Balay     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Some processors inserted others added");
336bc5ccf88SSatish Balay   }
337bc5ccf88SSatish Balay   mat->insertmode = addv; /* in case this processor had no cache */
338bc5ccf88SSatish Balay 
339bc5ccf88SSatish Balay   ierr =  StashScatterBegin_Private(&aij->stash,aij->rowners); CHKERRQ(ierr);
340bc5ccf88SSatish Balay   /* Free cache space */
341bc5ccf88SSatish Balay   PLogInfo(aij->A,"MatAssemblyBegin_MPIAIJ:Number of off-processor values %d\n",aij->stash.n);
342bc5ccf88SSatish Balay   PetscFunctionReturn(0);
343bc5ccf88SSatish Balay }
344bc5ccf88SSatish Balay 
345bc5ccf88SSatish Balay 
346bc5ccf88SSatish Balay #undef __FUNC__
347bc5ccf88SSatish Balay #define __FUNC__ "MatAssemblyEnd_MPIAIJ"
348bc5ccf88SSatish Balay int MatAssemblyEnd_MPIAIJ(Mat mat,MatAssemblyType mode)
349bc5ccf88SSatish Balay {
350bc5ccf88SSatish Balay   Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data;
351*a2d1c673SSatish Balay   int         i,j,rstart,ncols,n,ierr,flg;
352bc5ccf88SSatish Balay   int         *row,*col,other_disassembled;
353bc5ccf88SSatish Balay   Scalar      *val;
354bc5ccf88SSatish Balay   InsertMode  addv = mat->insertmode;
355bc5ccf88SSatish Balay 
356bc5ccf88SSatish Balay   PetscFunctionBegin;
357bc5ccf88SSatish Balay   if (!aij->donotstash) {
358*a2d1c673SSatish Balay     while (1) {
359*a2d1c673SSatish Balay       ierr = StashScatterGetMesg_Private(&aij->stash,&n,&row,&col,&val,&flg); CHKERRQ(ierr);
360*a2d1c673SSatish Balay       if (!flg) break;
361*a2d1c673SSatish Balay 
362bc5ccf88SSatish Balay       for ( i=0; i<n; ) {
363bc5ccf88SSatish Balay         /* Now identify the consecutive vals belonging to the same row */
364bc5ccf88SSatish Balay         for ( j=i,rstart=row[j]; j<n; j++ ) { if (row[j] != rstart) break; }
365bc5ccf88SSatish Balay         if (j < n) ncols = j-i;
366bc5ccf88SSatish Balay         else       ncols = n-i;
367bc5ccf88SSatish Balay         /* Now assemble all these values with a single function call */
368bc5ccf88SSatish Balay         ierr = MatSetValues_MPIAIJ(mat,1,row+i,ncols,col+i,val+i,addv); CHKERRQ(ierr);
369bc5ccf88SSatish Balay         i = j;
370bc5ccf88SSatish Balay       }
371bc5ccf88SSatish Balay     }
372*a2d1c673SSatish Balay     ierr = StashScatterEnd_Private(&aij->stash); CHKERRQ(ierr);
373bc5ccf88SSatish Balay   }
374bc5ccf88SSatish Balay 
375bc5ccf88SSatish Balay   ierr = MatAssemblyBegin(aij->A,mode); CHKERRQ(ierr);
376bc5ccf88SSatish Balay   ierr = MatAssemblyEnd(aij->A,mode); CHKERRQ(ierr);
377bc5ccf88SSatish Balay 
378bc5ccf88SSatish Balay   /* determine if any processor has disassembled, if so we must
379bc5ccf88SSatish Balay      also disassemble ourselfs, in order that we may reassemble. */
380bc5ccf88SSatish Balay   /*
381bc5ccf88SSatish Balay      if nonzero structure of submatrix B cannot change then we know that
382bc5ccf88SSatish Balay      no processor disassembled thus we can skip this stuff
383bc5ccf88SSatish Balay   */
384bc5ccf88SSatish Balay   if (!((Mat_SeqAIJ*) aij->B->data)->nonew)  {
385bc5ccf88SSatish Balay     ierr = MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPI_INT,MPI_PROD,mat->comm);CHKERRQ(ierr);
386bc5ccf88SSatish Balay     if (mat->was_assembled && !other_disassembled) {
387bc5ccf88SSatish Balay       ierr = DisAssemble_MPIAIJ(mat); CHKERRQ(ierr);
388bc5ccf88SSatish Balay     }
389bc5ccf88SSatish Balay   }
390bc5ccf88SSatish Balay 
391bc5ccf88SSatish Balay   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
392bc5ccf88SSatish Balay     ierr = MatSetUpMultiply_MPIAIJ(mat); CHKERRQ(ierr);
393bc5ccf88SSatish Balay   }
394bc5ccf88SSatish Balay   ierr = MatAssemblyBegin(aij->B,mode); CHKERRQ(ierr);
395bc5ccf88SSatish Balay   ierr = MatAssemblyEnd(aij->B,mode); CHKERRQ(ierr);
396bc5ccf88SSatish Balay 
397bc5ccf88SSatish Balay   if (aij->rowvalues) {PetscFree(aij->rowvalues); aij->rowvalues = 0;}
398bc5ccf88SSatish Balay   PetscFunctionReturn(0);
399bc5ccf88SSatish Balay }
400bc5ccf88SSatish Balay 
4015615d1e5SSatish Balay #undef __FUNC__
4025615d1e5SSatish Balay #define __FUNC__ "MatZeroEntries_MPIAIJ"
4038f6be9afSLois Curfman McInnes int MatZeroEntries_MPIAIJ(Mat A)
4041eb62cbbSBarry Smith {
40544a69424SLois Curfman McInnes   Mat_MPIAIJ *l = (Mat_MPIAIJ *) A->data;
406dbd7a890SLois Curfman McInnes   int        ierr;
4073a40ed3dSBarry Smith 
4083a40ed3dSBarry Smith   PetscFunctionBegin;
40978b31e54SBarry Smith   ierr = MatZeroEntries(l->A); CHKERRQ(ierr);
41078b31e54SBarry Smith   ierr = MatZeroEntries(l->B); CHKERRQ(ierr);
4113a40ed3dSBarry Smith   PetscFunctionReturn(0);
4121eb62cbbSBarry Smith }
4131eb62cbbSBarry Smith 
4145615d1e5SSatish Balay #undef __FUNC__
4155615d1e5SSatish Balay #define __FUNC__ "MatZeroRows_MPIAIJ"
4168f6be9afSLois Curfman McInnes int MatZeroRows_MPIAIJ(Mat A,IS is,Scalar *diag)
4171eb62cbbSBarry Smith {
41844a69424SLois Curfman McInnes   Mat_MPIAIJ     *l = (Mat_MPIAIJ *) A->data;
41917699dbbSLois Curfman McInnes   int            i,ierr,N, *rows,*owners = l->rowners,size = l->size;
4206a5c57faSSatish Balay   int            *procs,*nprocs,j,found,idx,nsends,*work,row;
42117699dbbSLois Curfman McInnes   int            nmax,*svalues,*starts,*owner,nrecvs,rank = l->rank;
4225392566eSBarry Smith   int            *rvalues,tag = A->tag,count,base,slen,n,*source;
4236a5c57faSSatish Balay   int            *lens,imdex,*lrows,*values,rstart=l->rstart;
424d6dfbf8fSBarry Smith   MPI_Comm       comm = A->comm;
4251eb62cbbSBarry Smith   MPI_Request    *send_waits,*recv_waits;
4261eb62cbbSBarry Smith   MPI_Status     recv_status,*send_status;
4271eb62cbbSBarry Smith   IS             istmp;
4281eb62cbbSBarry Smith 
4293a40ed3dSBarry Smith   PetscFunctionBegin;
43077c4ece6SBarry Smith   ierr = ISGetSize(is,&N); CHKERRQ(ierr);
43178b31e54SBarry Smith   ierr = ISGetIndices(is,&rows); CHKERRQ(ierr);
4321eb62cbbSBarry Smith 
4331eb62cbbSBarry Smith   /*  first count number of contributors to each processor */
4340452661fSBarry Smith   nprocs = (int *) PetscMalloc( 2*size*sizeof(int) ); CHKPTRQ(nprocs);
435cddf8d76SBarry Smith   PetscMemzero(nprocs,2*size*sizeof(int)); procs = nprocs + size;
4360452661fSBarry Smith   owner = (int *) PetscMalloc((N+1)*sizeof(int)); CHKPTRQ(owner); /* see note*/
4371eb62cbbSBarry Smith   for ( i=0; i<N; i++ ) {
4381eb62cbbSBarry Smith     idx = rows[i];
4391eb62cbbSBarry Smith     found = 0;
44017699dbbSLois Curfman McInnes     for ( j=0; j<size; j++ ) {
4411eb62cbbSBarry Smith       if (idx >= owners[j] && idx < owners[j+1]) {
4421eb62cbbSBarry Smith         nprocs[j]++; procs[j] = 1; owner[i] = j; found = 1; break;
4431eb62cbbSBarry Smith       }
4441eb62cbbSBarry Smith     }
445a8c6a408SBarry Smith     if (!found) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Index out of range");
4461eb62cbbSBarry Smith   }
44717699dbbSLois Curfman McInnes   nsends = 0;  for ( i=0; i<size; i++ ) { nsends += procs[i];}
4481eb62cbbSBarry Smith 
4491eb62cbbSBarry Smith   /* inform other processors of number of messages and max length*/
4500452661fSBarry Smith   work = (int *) PetscMalloc( size*sizeof(int) ); CHKPTRQ(work);
451ca161407SBarry Smith   ierr = MPI_Allreduce( procs, work,size,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr);
45217699dbbSLois Curfman McInnes   nrecvs = work[rank];
453ca161407SBarry Smith   ierr = MPI_Allreduce( nprocs, work,size,MPI_INT,MPI_MAX,comm);CHKERRQ(ierr);
45417699dbbSLois Curfman McInnes   nmax = work[rank];
4550452661fSBarry Smith   PetscFree(work);
4561eb62cbbSBarry Smith 
4571eb62cbbSBarry Smith   /* post receives:   */
4583a40ed3dSBarry Smith   rvalues = (int *) PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(int));CHKPTRQ(rvalues);
459ca161407SBarry Smith   recv_waits = (MPI_Request *) PetscMalloc((nrecvs+1)*sizeof(MPI_Request));CHKPTRQ(recv_waits);
4601eb62cbbSBarry Smith   for ( i=0; i<nrecvs; i++ ) {
461ca161407SBarry Smith     ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPI_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr);
4621eb62cbbSBarry Smith   }
4631eb62cbbSBarry Smith 
4641eb62cbbSBarry Smith   /* do sends:
4651eb62cbbSBarry Smith       1) starts[i] gives the starting index in svalues for stuff going to
4661eb62cbbSBarry Smith          the ith processor
4671eb62cbbSBarry Smith   */
4680452661fSBarry Smith   svalues = (int *) PetscMalloc( (N+1)*sizeof(int) ); CHKPTRQ(svalues);
4693a40ed3dSBarry Smith   send_waits = (MPI_Request *) PetscMalloc( (nsends+1)*sizeof(MPI_Request));CHKPTRQ(send_waits);
4700452661fSBarry Smith   starts = (int *) PetscMalloc( (size+1)*sizeof(int) ); CHKPTRQ(starts);
4711eb62cbbSBarry Smith   starts[0] = 0;
47217699dbbSLois Curfman McInnes   for ( i=1; i<size; i++ ) { starts[i] = starts[i-1] + nprocs[i-1];}
4731eb62cbbSBarry Smith   for ( i=0; i<N; i++ ) {
4741eb62cbbSBarry Smith     svalues[starts[owner[i]]++] = rows[i];
4751eb62cbbSBarry Smith   }
4761eb62cbbSBarry Smith   ISRestoreIndices(is,&rows);
4771eb62cbbSBarry Smith 
4781eb62cbbSBarry Smith   starts[0] = 0;
47917699dbbSLois Curfman McInnes   for ( i=1; i<size+1; i++ ) { starts[i] = starts[i-1] + nprocs[i-1];}
4801eb62cbbSBarry Smith   count = 0;
48117699dbbSLois Curfman McInnes   for ( i=0; i<size; i++ ) {
4821eb62cbbSBarry Smith     if (procs[i]) {
483ca161407SBarry Smith       ierr = MPI_Isend(svalues+starts[i],nprocs[i],MPI_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr);
4841eb62cbbSBarry Smith     }
4851eb62cbbSBarry Smith   }
4860452661fSBarry Smith   PetscFree(starts);
4871eb62cbbSBarry Smith 
48817699dbbSLois Curfman McInnes   base = owners[rank];
4891eb62cbbSBarry Smith 
4901eb62cbbSBarry Smith   /*  wait on receives */
4910452661fSBarry Smith   lens   = (int *) PetscMalloc( 2*(nrecvs+1)*sizeof(int) ); CHKPTRQ(lens);
4921eb62cbbSBarry Smith   source = lens + nrecvs;
4931eb62cbbSBarry Smith   count  = nrecvs; slen = 0;
4941eb62cbbSBarry Smith   while (count) {
495ca161407SBarry Smith     ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
4961eb62cbbSBarry Smith     /* unpack receives into our local space */
497ca161407SBarry Smith     ierr = MPI_Get_count(&recv_status,MPI_INT,&n);CHKERRQ(ierr);
498d6dfbf8fSBarry Smith     source[imdex]  = recv_status.MPI_SOURCE;
499d6dfbf8fSBarry Smith     lens[imdex]  = n;
5001eb62cbbSBarry Smith     slen += n;
5011eb62cbbSBarry Smith     count--;
5021eb62cbbSBarry Smith   }
5030452661fSBarry Smith   PetscFree(recv_waits);
5041eb62cbbSBarry Smith 
5051eb62cbbSBarry Smith   /* move the data into the send scatter */
5060452661fSBarry Smith   lrows = (int *) PetscMalloc( (slen+1)*sizeof(int) ); CHKPTRQ(lrows);
5071eb62cbbSBarry Smith   count = 0;
5081eb62cbbSBarry Smith   for ( i=0; i<nrecvs; i++ ) {
5091eb62cbbSBarry Smith     values = rvalues + i*nmax;
5101eb62cbbSBarry Smith     for ( j=0; j<lens[i]; j++ ) {
5111eb62cbbSBarry Smith       lrows[count++] = values[j] - base;
5121eb62cbbSBarry Smith     }
5131eb62cbbSBarry Smith   }
5140452661fSBarry Smith   PetscFree(rvalues); PetscFree(lens);
5150452661fSBarry Smith   PetscFree(owner); PetscFree(nprocs);
5161eb62cbbSBarry Smith 
5171eb62cbbSBarry Smith   /* actually zap the local rows */
518029af93fSBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,slen,lrows,&istmp);CHKERRQ(ierr);
519464493b3SBarry Smith   PLogObjectParent(A,istmp);
5206a5c57faSSatish Balay 
5216eb55b6aSBarry Smith   /*
5226eb55b6aSBarry Smith         Zero the required rows. If the "diagonal block" of the matrix
5236eb55b6aSBarry Smith      is square and the user wishes to set the diagonal we use seperate
5246eb55b6aSBarry Smith      code so that MatSetValues() is not called for each diagonal allocating
5256eb55b6aSBarry Smith      new memory, thus calling lots of mallocs and slowing things down.
5266eb55b6aSBarry Smith 
5276eb55b6aSBarry Smith        Contributed by: Mathew Knepley
5286eb55b6aSBarry Smith   */
529e2d53e46SBarry Smith   /* must zero l->B before l->A because the (diag) case below may put values into l->B*/
530e2d53e46SBarry Smith   ierr = MatZeroRows(l->B,istmp,0); CHKERRQ(ierr);
5316eb55b6aSBarry Smith   if (diag && (l->A->M == l->A->N)) {
5326eb55b6aSBarry Smith     ierr      = MatZeroRows(l->A,istmp,diag); CHKERRQ(ierr);
533e2d53e46SBarry Smith   } else if (diag) {
534e2d53e46SBarry Smith     ierr = MatZeroRows(l->A,istmp,0); CHKERRQ(ierr);
535fa46199cSSatish Balay     if (((Mat_SeqAIJ*)l->A->data)->nonew) {
536fa46199cSSatish Balay       SETERRQ(PETSC_ERR_SUP,0,"MatZeroRows() on rectangular matrices cannot be used with the Mat options\n\
537fa46199cSSatish Balay MAT_NO_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR");
5386525c446SSatish Balay     }
539e2d53e46SBarry Smith     for ( i = 0; i < slen; i++ ) {
540e2d53e46SBarry Smith       row  = lrows[i] + rstart;
541e2d53e46SBarry Smith       ierr = MatSetValues(A,1,&row,1,&row,diag,INSERT_VALUES);CHKERRQ(ierr);
542e2d53e46SBarry Smith     }
543e2d53e46SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
544e2d53e46SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
5456eb55b6aSBarry Smith   } else {
5466a5c57faSSatish Balay     ierr = MatZeroRows(l->A,istmp,0); CHKERRQ(ierr);
5476eb55b6aSBarry Smith   }
54878b31e54SBarry Smith   ierr = ISDestroy(istmp); CHKERRQ(ierr);
5496a5c57faSSatish Balay   PetscFree(lrows);
55072dacd9aSBarry Smith 
5511eb62cbbSBarry Smith   /* wait on sends */
5521eb62cbbSBarry Smith   if (nsends) {
553ca161407SBarry Smith     send_status = (MPI_Status *) PetscMalloc(nsends*sizeof(MPI_Status));CHKPTRQ(send_status);
554ca161407SBarry Smith     ierr        = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr);
5550452661fSBarry Smith     PetscFree(send_status);
5561eb62cbbSBarry Smith   }
5570452661fSBarry Smith   PetscFree(send_waits); PetscFree(svalues);
5581eb62cbbSBarry Smith 
5593a40ed3dSBarry Smith   PetscFunctionReturn(0);
5601eb62cbbSBarry Smith }
5611eb62cbbSBarry Smith 
5625615d1e5SSatish Balay #undef __FUNC__
5635615d1e5SSatish Balay #define __FUNC__ "MatMult_MPIAIJ"
5648f6be9afSLois Curfman McInnes int MatMult_MPIAIJ(Mat A,Vec xx,Vec yy)
5651eb62cbbSBarry Smith {
566416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data;
567fbd6ef76SBarry Smith   int        ierr,nt;
568416022c9SBarry Smith 
5693a40ed3dSBarry Smith   PetscFunctionBegin;
570a2ce50c7SBarry Smith   ierr = VecGetLocalSize(xx,&nt);  CHKERRQ(ierr);
571fbd6ef76SBarry Smith   if (nt != a->n) {
572a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_SIZ,0,"Incompatible partition of A and xx");
573fbd6ef76SBarry Smith   }
57443a90d84SBarry Smith   ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx); CHKERRQ(ierr);
575f830108cSBarry Smith   ierr = (*a->A->ops->mult)(a->A,xx,yy); CHKERRQ(ierr);
57643a90d84SBarry Smith   ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx); CHKERRQ(ierr);
577f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy); CHKERRQ(ierr);
5783a40ed3dSBarry Smith   PetscFunctionReturn(0);
5791eb62cbbSBarry Smith }
5801eb62cbbSBarry Smith 
5815615d1e5SSatish Balay #undef __FUNC__
5825615d1e5SSatish Balay #define __FUNC__ "MatMultAdd_MPIAIJ"
5838f6be9afSLois Curfman McInnes int MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
584da3a660dSBarry Smith {
585416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data;
586da3a660dSBarry Smith   int        ierr;
5873a40ed3dSBarry Smith 
5883a40ed3dSBarry Smith   PetscFunctionBegin;
58943a90d84SBarry Smith   ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
590f830108cSBarry Smith   ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz); CHKERRQ(ierr);
59143a90d84SBarry Smith   ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
592f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz); CHKERRQ(ierr);
5933a40ed3dSBarry Smith   PetscFunctionReturn(0);
594da3a660dSBarry Smith }
595da3a660dSBarry Smith 
5965615d1e5SSatish Balay #undef __FUNC__
5975615d1e5SSatish Balay #define __FUNC__ "MatMultTrans_MPIAIJ"
5988f6be9afSLois Curfman McInnes int MatMultTrans_MPIAIJ(Mat A,Vec xx,Vec yy)
599da3a660dSBarry Smith {
600416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data;
601da3a660dSBarry Smith   int        ierr;
602da3a660dSBarry Smith 
6033a40ed3dSBarry Smith   PetscFunctionBegin;
604da3a660dSBarry Smith   /* do nondiagonal part */
605f830108cSBarry Smith   ierr = (*a->B->ops->multtrans)(a->B,xx,a->lvec); CHKERRQ(ierr);
606da3a660dSBarry Smith   /* send it on its way */
607537820f0SBarry Smith   ierr = VecScatterBegin(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx); CHKERRQ(ierr);
608da3a660dSBarry Smith   /* do local part */
609f830108cSBarry Smith   ierr = (*a->A->ops->multtrans)(a->A,xx,yy); CHKERRQ(ierr);
610da3a660dSBarry Smith   /* receive remote parts: note this assumes the values are not actually */
611da3a660dSBarry Smith   /* inserted in yy until the next line, which is true for my implementation*/
612da3a660dSBarry Smith   /* but is not perhaps always true. */
613537820f0SBarry Smith   ierr = VecScatterEnd(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx); CHKERRQ(ierr);
6143a40ed3dSBarry Smith   PetscFunctionReturn(0);
615da3a660dSBarry Smith }
616da3a660dSBarry Smith 
6175615d1e5SSatish Balay #undef __FUNC__
6185615d1e5SSatish Balay #define __FUNC__ "MatMultTransAdd_MPIAIJ"
6198f6be9afSLois Curfman McInnes int MatMultTransAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
620da3a660dSBarry Smith {
621416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data;
622da3a660dSBarry Smith   int        ierr;
623da3a660dSBarry Smith 
6243a40ed3dSBarry Smith   PetscFunctionBegin;
625da3a660dSBarry Smith   /* do nondiagonal part */
626f830108cSBarry Smith   ierr = (*a->B->ops->multtrans)(a->B,xx,a->lvec); CHKERRQ(ierr);
627da3a660dSBarry Smith   /* send it on its way */
628537820f0SBarry Smith   ierr = VecScatterBegin(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx); CHKERRQ(ierr);
629da3a660dSBarry Smith   /* do local part */
630f830108cSBarry Smith   ierr = (*a->A->ops->multtransadd)(a->A,xx,yy,zz); CHKERRQ(ierr);
631da3a660dSBarry Smith   /* receive remote parts: note this assumes the values are not actually */
632da3a660dSBarry Smith   /* inserted in yy until the next line, which is true for my implementation*/
633da3a660dSBarry Smith   /* but is not perhaps always true. */
6340a198c4cSBarry Smith   ierr = VecScatterEnd(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx); CHKERRQ(ierr);
6353a40ed3dSBarry Smith   PetscFunctionReturn(0);
636da3a660dSBarry Smith }
637da3a660dSBarry Smith 
6381eb62cbbSBarry Smith /*
6391eb62cbbSBarry Smith   This only works correctly for square matrices where the subblock A->A is the
6401eb62cbbSBarry Smith    diagonal block
6411eb62cbbSBarry Smith */
6425615d1e5SSatish Balay #undef __FUNC__
6435615d1e5SSatish Balay #define __FUNC__ "MatGetDiagonal_MPIAIJ"
6448f6be9afSLois Curfman McInnes int MatGetDiagonal_MPIAIJ(Mat A,Vec v)
6451eb62cbbSBarry Smith {
6463a40ed3dSBarry Smith   int        ierr;
647416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data;
6483a40ed3dSBarry Smith 
6493a40ed3dSBarry Smith   PetscFunctionBegin;
650a8c6a408SBarry Smith   if (a->M != a->N) SETERRQ(PETSC_ERR_SUP,0,"Supports only square matrix where A->A is diag block");
6515baf8537SBarry Smith   if (a->rstart != a->cstart || a->rend != a->cend) {
652a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_SIZ,0,"row partition must equal col partition");
6533a40ed3dSBarry Smith   }
6543a40ed3dSBarry Smith   ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr);
6553a40ed3dSBarry Smith   PetscFunctionReturn(0);
6561eb62cbbSBarry Smith }
6571eb62cbbSBarry Smith 
6585615d1e5SSatish Balay #undef __FUNC__
6595615d1e5SSatish Balay #define __FUNC__ "MatScale_MPIAIJ"
6608f6be9afSLois Curfman McInnes int MatScale_MPIAIJ(Scalar *aa,Mat A)
661052efed2SBarry Smith {
662052efed2SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data;
663052efed2SBarry Smith   int        ierr;
6643a40ed3dSBarry Smith 
6653a40ed3dSBarry Smith   PetscFunctionBegin;
666052efed2SBarry Smith   ierr = MatScale(aa,a->A); CHKERRQ(ierr);
667052efed2SBarry Smith   ierr = MatScale(aa,a->B); CHKERRQ(ierr);
6683a40ed3dSBarry Smith   PetscFunctionReturn(0);
669052efed2SBarry Smith }
670052efed2SBarry Smith 
6715615d1e5SSatish Balay #undef __FUNC__
672d4bb536fSBarry Smith #define __FUNC__ "MatDestroy_MPIAIJ"
673e1311b90SBarry Smith int MatDestroy_MPIAIJ(Mat mat)
6741eb62cbbSBarry Smith {
67544a69424SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data;
6761eb62cbbSBarry Smith   int        ierr;
67783e2fdc7SBarry Smith 
6783a40ed3dSBarry Smith   PetscFunctionBegin;
67970429bc8SBarry Smith   if (--mat->refct > 0) PetscFunctionReturn(0);
68070429bc8SBarry Smith 
68170429bc8SBarry Smith   if (mat->mapping) {
68270429bc8SBarry Smith     ierr = ISLocalToGlobalMappingDestroy(mat->mapping); CHKERRQ(ierr);
68370429bc8SBarry Smith   }
68470429bc8SBarry Smith   if (mat->bmapping) {
68570429bc8SBarry Smith     ierr = ISLocalToGlobalMappingDestroy(mat->bmapping); CHKERRQ(ierr);
68670429bc8SBarry Smith   }
68761b13de0SBarry Smith   if (mat->rmap) {
68861b13de0SBarry Smith     ierr = MapDestroy(mat->rmap);CHKERRQ(ierr);
68961b13de0SBarry Smith   }
69061b13de0SBarry Smith   if (mat->cmap) {
69161b13de0SBarry Smith     ierr = MapDestroy(mat->cmap);CHKERRQ(ierr);
69261b13de0SBarry Smith   }
6933a40ed3dSBarry Smith #if defined(USE_PETSC_LOG)
694e1311b90SBarry Smith   PLogObjectState((PetscObject)mat,"Rows=%d, Cols=%d",aij->M,aij->N);
695a5a9c739SBarry Smith #endif
69683e2fdc7SBarry Smith   ierr = StashDestroy_Private(&aij->stash); CHKERRQ(ierr);
6970452661fSBarry Smith   PetscFree(aij->rowners);
69878b31e54SBarry Smith   ierr = MatDestroy(aij->A); CHKERRQ(ierr);
69978b31e54SBarry Smith   ierr = MatDestroy(aij->B); CHKERRQ(ierr);
700b1fc9764SSatish Balay #if defined (USE_CTABLE)
701b1fc9764SSatish Balay   if (aij->colmap) TableDelete(aij->colmap);
702b1fc9764SSatish Balay #else
7030452661fSBarry Smith   if (aij->colmap) PetscFree(aij->colmap);
704b1fc9764SSatish Balay #endif
7050452661fSBarry Smith   if (aij->garray) PetscFree(aij->garray);
7061eb62cbbSBarry Smith   if (aij->lvec)   VecDestroy(aij->lvec);
707a56f8943SBarry Smith   if (aij->Mvctx)  VecScatterDestroy(aij->Mvctx);
7087a0afa10SBarry Smith   if (aij->rowvalues) PetscFree(aij->rowvalues);
7090452661fSBarry Smith   PetscFree(aij);
710a5a9c739SBarry Smith   PLogObjectDestroy(mat);
7110452661fSBarry Smith   PetscHeaderDestroy(mat);
7123a40ed3dSBarry Smith   PetscFunctionReturn(0);
7131eb62cbbSBarry Smith }
714ee50ffe9SBarry Smith 
7155615d1e5SSatish Balay #undef __FUNC__
716d4bb536fSBarry Smith #define __FUNC__ "MatView_MPIAIJ_Binary"
717bc5ccf88SSatish Balay int MatView_MPIAIJ_Binary(Mat mat,Viewer viewer)
7181eb62cbbSBarry Smith {
719416022c9SBarry Smith   Mat_MPIAIJ  *aij = (Mat_MPIAIJ *) mat->data;
720416022c9SBarry Smith   int         ierr;
721416022c9SBarry Smith 
7223a40ed3dSBarry Smith   PetscFunctionBegin;
72317699dbbSLois Curfman McInnes   if (aij->size == 1) {
724416022c9SBarry Smith     ierr = MatView(aij->A,viewer); CHKERRQ(ierr);
725416022c9SBarry Smith   }
726a8c6a408SBarry Smith   else SETERRQ(PETSC_ERR_SUP,0,"Only uniprocessor output supported");
7273a40ed3dSBarry Smith   PetscFunctionReturn(0);
728416022c9SBarry Smith }
729416022c9SBarry Smith 
7305615d1e5SSatish Balay #undef __FUNC__
7317b2a1423SBarry Smith #define __FUNC__ "MatView_MPIAIJ_ASCIIorDraworSocket"
732bc5ccf88SSatish Balay int MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,Viewer viewer)
733416022c9SBarry Smith {
73444a69424SLois Curfman McInnes   Mat_MPIAIJ  *aij = (Mat_MPIAIJ *) mat->data;
735dbb450caSBarry Smith   Mat_SeqAIJ* C = (Mat_SeqAIJ*)aij->A->data;
73677ed5343SBarry Smith   int         ierr, format,shift = C->indexshift,rank = aij->rank ;
73777ed5343SBarry Smith   int         size = aij->size;
738d636dbe3SBarry Smith   FILE        *fd;
73919bcc07fSBarry Smith   ViewerType  vtype;
740416022c9SBarry Smith 
7413a40ed3dSBarry Smith   PetscFunctionBegin;
74219bcc07fSBarry Smith   ierr = ViewerGetType(viewer,&vtype); CHKERRQ(ierr);
7433f1db9ecSBarry Smith   if (PetscTypeCompare(vtype,ASCII_VIEWER)) {
744d8467735SBarry Smith     ierr = ViewerGetFormat(viewer,&format);CHKERRQ(ierr);
7450a198c4cSBarry Smith     if (format == VIEWER_FORMAT_ASCII_INFO_LONG) {
7464e220ebcSLois Curfman McInnes       MatInfo info;
7474e220ebcSLois Curfman McInnes       int     flg;
748a56f8943SBarry Smith       MPI_Comm_rank(mat->comm,&rank);
74990ace30eSBarry Smith       ierr = ViewerASCIIGetPointer(viewer,&fd); CHKERRQ(ierr);
7504e220ebcSLois Curfman McInnes       ierr = MatGetInfo(mat,MAT_LOCAL,&info);
75195e01e2fSLois Curfman McInnes       ierr = OptionsHasName(PETSC_NULL,"-mat_aij_no_inode",&flg); CHKERRQ(ierr);
75277c4ece6SBarry Smith       PetscSequentialPhaseBegin(mat->comm,1);
75395e01e2fSLois Curfman McInnes       if (flg) fprintf(fd,"[%d] Local rows %d nz %d nz alloced %d mem %d, not using I-node routines\n",
7544e220ebcSLois Curfman McInnes          rank,aij->m,(int)info.nz_used,(int)info.nz_allocated,(int)info.memory);
75595e01e2fSLois Curfman McInnes       else fprintf(fd,"[%d] Local rows %d nz %d nz alloced %d mem %d, using I-node routines\n",
7564e220ebcSLois Curfman McInnes          rank,aij->m,(int)info.nz_used,(int)info.nz_allocated,(int)info.memory);
7574e220ebcSLois Curfman McInnes       ierr = MatGetInfo(aij->A,MAT_LOCAL,&info);
7584e220ebcSLois Curfman McInnes       fprintf(fd,"[%d] on-diagonal part: nz %d \n",rank,(int)info.nz_used);
7594e220ebcSLois Curfman McInnes       ierr = MatGetInfo(aij->B,MAT_LOCAL,&info);
7604e220ebcSLois Curfman McInnes       fprintf(fd,"[%d] off-diagonal part: nz %d \n",rank,(int)info.nz_used);
761a56f8943SBarry Smith       fflush(fd);
76277c4ece6SBarry Smith       PetscSequentialPhaseEnd(mat->comm,1);
763a40aa06bSLois Curfman McInnes       ierr = VecScatterView(aij->Mvctx,viewer); CHKERRQ(ierr);
7643a40ed3dSBarry Smith       PetscFunctionReturn(0);
7653a40ed3dSBarry Smith     } else if (format == VIEWER_FORMAT_ASCII_INFO) {
7663a40ed3dSBarry Smith       PetscFunctionReturn(0);
76708480c60SBarry Smith     }
7683f1db9ecSBarry Smith   } else if (PetscTypeCompare(vtype,DRAW_VIEWER)) {
76919bcc07fSBarry Smith     Draw       draw;
77019bcc07fSBarry Smith     PetscTruth isnull;
77177ed5343SBarry Smith     ierr = ViewerDrawGetDraw(viewer,0,&draw); CHKERRQ(ierr);
7723a40ed3dSBarry Smith     ierr = DrawIsNull(draw,&isnull); CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
77319bcc07fSBarry Smith   }
77419bcc07fSBarry Smith 
77517699dbbSLois Curfman McInnes   if (size == 1) {
77678b31e54SBarry Smith     ierr = MatView(aij->A,viewer); CHKERRQ(ierr);
7773a40ed3dSBarry Smith   } else {
77895373324SBarry Smith     /* assemble the entire matrix onto first processor. */
77995373324SBarry Smith     Mat         A;
780ec8511deSBarry Smith     Mat_SeqAIJ *Aloc;
7812eb8c8abSBarry Smith     int         M = aij->M, N = aij->N,m,*ai,*aj,row,*cols,i,*ct;
78295373324SBarry Smith     Scalar      *a;
7832ee70a88SLois Curfman McInnes 
78417699dbbSLois Curfman McInnes     if (!rank) {
78555843e3eSBarry Smith       ierr = MatCreateMPIAIJ(mat->comm,M,N,M,N,0,PETSC_NULL,0,PETSC_NULL,&A);CHKERRQ(ierr);
7863a40ed3dSBarry Smith     } else {
78755843e3eSBarry Smith       ierr = MatCreateMPIAIJ(mat->comm,0,0,M,N,0,PETSC_NULL,0,PETSC_NULL,&A);CHKERRQ(ierr);
78895373324SBarry Smith     }
789464493b3SBarry Smith     PLogObjectParent(mat,A);
790416022c9SBarry Smith 
79195373324SBarry Smith     /* copy over the A part */
792ec8511deSBarry Smith     Aloc = (Mat_SeqAIJ*) aij->A->data;
7932ee70a88SLois Curfman McInnes     m = Aloc->m; ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
79495373324SBarry Smith     row = aij->rstart;
795dbb450caSBarry Smith     for ( i=0; i<ai[m]+shift; i++ ) {aj[i] += aij->cstart + shift;}
79695373324SBarry Smith     for ( i=0; i<m; i++ ) {
797416022c9SBarry Smith       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);CHKERRQ(ierr);
79895373324SBarry Smith       row++; a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
79995373324SBarry Smith     }
8002ee70a88SLois Curfman McInnes     aj = Aloc->j;
801dbb450caSBarry Smith     for ( i=0; i<ai[m]+shift; i++ ) {aj[i] -= aij->cstart + shift;}
80295373324SBarry Smith 
80395373324SBarry Smith     /* copy over the B part */
804ec8511deSBarry Smith     Aloc = (Mat_SeqAIJ*) aij->B->data;
8052ee70a88SLois Curfman McInnes     m = Aloc->m;  ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
80695373324SBarry Smith     row = aij->rstart;
8070452661fSBarry Smith     ct = cols = (int *) PetscMalloc( (ai[m]+1)*sizeof(int) ); CHKPTRQ(cols);
808dbb450caSBarry Smith     for ( i=0; i<ai[m]+shift; i++ ) {cols[i] = aij->garray[aj[i]+shift];}
80995373324SBarry Smith     for ( i=0; i<m; i++ ) {
810416022c9SBarry Smith       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);CHKERRQ(ierr);
81195373324SBarry Smith       row++; a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
81295373324SBarry Smith     }
8130452661fSBarry Smith     PetscFree(ct);
8146d4a8577SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
8156d4a8577SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
81655843e3eSBarry Smith     /*
81755843e3eSBarry Smith        Everyone has to call to draw the matrix since the graphics waits are
81855843e3eSBarry Smith        synchronized across all processors that share the Draw object
81955843e3eSBarry Smith     */
8203f1db9ecSBarry Smith     if (!rank || PetscTypeCompare(vtype,DRAW_VIEWER)) {
82178b31e54SBarry Smith       ierr = MatView(((Mat_MPIAIJ*)(A->data))->A,viewer); CHKERRQ(ierr);
82295373324SBarry Smith     }
82378b31e54SBarry Smith     ierr = MatDestroy(A); CHKERRQ(ierr);
82495373324SBarry Smith   }
8253a40ed3dSBarry Smith   PetscFunctionReturn(0);
8261eb62cbbSBarry Smith }
8271eb62cbbSBarry Smith 
8285615d1e5SSatish Balay #undef __FUNC__
829d4bb536fSBarry Smith #define __FUNC__ "MatView_MPIAIJ"
830e1311b90SBarry Smith int MatView_MPIAIJ(Mat mat,Viewer viewer)
831416022c9SBarry Smith {
832416022c9SBarry Smith   int         ierr;
83319bcc07fSBarry Smith   ViewerType  vtype;
834416022c9SBarry Smith 
8353a40ed3dSBarry Smith   PetscFunctionBegin;
83619bcc07fSBarry Smith   ierr = ViewerGetType(viewer,&vtype); CHKERRQ(ierr);
8373f1db9ecSBarry Smith   if (PetscTypeCompare(vtype,ASCII_VIEWER) || PetscTypeCompare(vtype,DRAW_VIEWER) ||
8387b2a1423SBarry Smith       PetscTypeCompare(vtype,SOCKET_VIEWER)) {
8397b2a1423SBarry Smith     ierr = MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer); CHKERRQ(ierr);
8403f1db9ecSBarry Smith   } else if (PetscTypeCompare(vtype,BINARY_VIEWER)) {
8413a40ed3dSBarry Smith     ierr = MatView_MPIAIJ_Binary(mat,viewer);CHKERRQ(ierr);
8425cd90555SBarry Smith   } else {
8435cd90555SBarry Smith     SETERRQ(1,1,"Viewer type not supported by PETSc object");
844416022c9SBarry Smith   }
8453a40ed3dSBarry Smith   PetscFunctionReturn(0);
846416022c9SBarry Smith }
847416022c9SBarry Smith 
8481eb62cbbSBarry Smith /*
8491eb62cbbSBarry Smith     This has to provide several versions.
8501eb62cbbSBarry Smith 
8511eb62cbbSBarry Smith      2) a) use only local smoothing updating outer values only once.
8521eb62cbbSBarry Smith         b) local smoothing updating outer values each inner iteration
853d6dfbf8fSBarry Smith      3) color updating out values betwen colors.
8541eb62cbbSBarry Smith */
8555615d1e5SSatish Balay #undef __FUNC__
8565615d1e5SSatish Balay #define __FUNC__ "MatRelax_MPIAIJ"
8578f6be9afSLois Curfman McInnes int MatRelax_MPIAIJ(Mat matin,Vec bb,double omega,MatSORType flag,
858dbb450caSBarry Smith                            double fshift,int its,Vec xx)
8598a729477SBarry Smith {
86044a69424SLois Curfman McInnes   Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data;
861d6dfbf8fSBarry Smith   Mat        AA = mat->A, BB = mat->B;
862ec8511deSBarry Smith   Mat_SeqAIJ *A = (Mat_SeqAIJ *) AA->data, *B = (Mat_SeqAIJ *)BB->data;
863c16cb8f2SBarry Smith   Scalar     *b,*x,*xs,*ls,d,*v,sum;
8646abc6512SBarry Smith   int        ierr,*idx, *diag;
865416022c9SBarry Smith   int        n = mat->n, m = mat->m, i,shift = A->indexshift;
8668a729477SBarry Smith 
8673a40ed3dSBarry Smith   PetscFunctionBegin;
8682e8a6d31SBarry Smith   ierr = VecGetArray(xx,&x); CHKERRQ(ierr);
8692e8a6d31SBarry Smith   ierr = VecGetArray(bb,&b); CHKERRQ(ierr);
8702e8a6d31SBarry Smith   ierr = VecGetArray(mat->lvec,&ls);CHKERRQ(ierr);
871dbb450caSBarry Smith   xs = x + shift; /* shift by one for index start of 1 */
872dbb450caSBarry Smith   ls = ls + shift;
87383e2fdc7SBarry Smith   if (!A->diag) {ierr = MatMarkDiag_SeqAIJ(AA); CHKERRQ(ierr);}
874d6dfbf8fSBarry Smith   diag = A->diag;
875c16cb8f2SBarry Smith   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){
876da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
877f830108cSBarry Smith       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,its,xx);CHKERRQ(ierr);
8782e8a6d31SBarry Smith       ierr = VecRestoreArray(xx,&x); CHKERRQ(ierr);
8792e8a6d31SBarry Smith       ierr = VecRestoreArray(bb,&b); CHKERRQ(ierr);
8802e8a6d31SBarry Smith       ierr = VecRestoreArray(mat->lvec,&ls);CHKERRQ(ierr);
8813a40ed3dSBarry Smith       PetscFunctionReturn(0);
882da3a660dSBarry Smith     }
8833a40ed3dSBarry Smith     ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
8843a40ed3dSBarry Smith     ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
885d6dfbf8fSBarry Smith     while (its--) {
886d6dfbf8fSBarry Smith       /* go down through the rows */
887d6dfbf8fSBarry Smith       for ( i=0; i<m; i++ ) {
888d6dfbf8fSBarry Smith         n    = A->i[i+1] - A->i[i];
8894d197716SBarry Smith 	PLogFlops(4*n+3);
890dbb450caSBarry Smith         idx  = A->j + A->i[i] + shift;
891dbb450caSBarry Smith         v    = A->a + A->i[i] + shift;
892d6dfbf8fSBarry Smith         sum  = b[i];
893d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
894dbb450caSBarry Smith         d    = fshift + A->a[diag[i]+shift];
895d6dfbf8fSBarry Smith         n    = B->i[i+1] - B->i[i];
896dbb450caSBarry Smith         idx  = B->j + B->i[i] + shift;
897dbb450caSBarry Smith         v    = B->a + B->i[i] + shift;
898d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,ls,v,idx,n);
89955a1b374SBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + A->a[diag[i]+shift]*x[i])/d;
900d6dfbf8fSBarry Smith       }
901d6dfbf8fSBarry Smith       /* come up through the rows */
902d6dfbf8fSBarry Smith       for ( i=m-1; i>-1; i-- ) {
903d6dfbf8fSBarry Smith         n    = A->i[i+1] - A->i[i];
9044d197716SBarry Smith 	PLogFlops(4*n+3)
905dbb450caSBarry Smith         idx  = A->j + A->i[i] + shift;
906dbb450caSBarry Smith         v    = A->a + A->i[i] + shift;
907d6dfbf8fSBarry Smith         sum  = b[i];
908d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
909dbb450caSBarry Smith         d    = fshift + A->a[diag[i]+shift];
910d6dfbf8fSBarry Smith         n    = B->i[i+1] - B->i[i];
911dbb450caSBarry Smith         idx  = B->j + B->i[i] + shift;
912dbb450caSBarry Smith         v    = B->a + B->i[i] + shift;
913d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,ls,v,idx,n);
91455a1b374SBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + A->a[diag[i]+shift]*x[i])/d;
915d6dfbf8fSBarry Smith       }
916d6dfbf8fSBarry Smith     }
9173a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_FORWARD_SWEEP){
918da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
919f830108cSBarry Smith       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,its,xx);CHKERRQ(ierr);
9202e8a6d31SBarry Smith       ierr = VecRestoreArray(xx,&x); CHKERRQ(ierr);
9212e8a6d31SBarry Smith       ierr = VecRestoreArray(bb,&b); CHKERRQ(ierr);
9222e8a6d31SBarry Smith       ierr = VecRestoreArray(mat->lvec,&ls);CHKERRQ(ierr);
9233a40ed3dSBarry Smith       PetscFunctionReturn(0);
924da3a660dSBarry Smith     }
9253a40ed3dSBarry Smith     ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
9263a40ed3dSBarry Smith     ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
927d6dfbf8fSBarry Smith     while (its--) {
928d6dfbf8fSBarry Smith       for ( i=0; i<m; i++ ) {
929d6dfbf8fSBarry Smith         n    = A->i[i+1] - A->i[i];
9304d197716SBarry Smith 	PLogFlops(4*n+3);
931dbb450caSBarry Smith         idx  = A->j + A->i[i] + shift;
932dbb450caSBarry Smith         v    = A->a + A->i[i] + shift;
933d6dfbf8fSBarry Smith         sum  = b[i];
934d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
935dbb450caSBarry Smith         d    = fshift + A->a[diag[i]+shift];
936d6dfbf8fSBarry Smith         n    = B->i[i+1] - B->i[i];
937dbb450caSBarry Smith         idx  = B->j + B->i[i] + shift;
938dbb450caSBarry Smith         v    = B->a + B->i[i] + shift;
939d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,ls,v,idx,n);
94055a1b374SBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + A->a[diag[i]+shift]*x[i])/d;
941d6dfbf8fSBarry Smith       }
942d6dfbf8fSBarry Smith     }
9433a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_BACKWARD_SWEEP){
944da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
945f830108cSBarry Smith       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,its,xx);CHKERRQ(ierr);
9462e8a6d31SBarry Smith       ierr = VecRestoreArray(xx,&x); CHKERRQ(ierr);
9472e8a6d31SBarry Smith       ierr = VecRestoreArray(bb,&b); CHKERRQ(ierr);
9482e8a6d31SBarry Smith       ierr = VecRestoreArray(mat->lvec,&ls);CHKERRQ(ierr);
9493a40ed3dSBarry Smith       PetscFunctionReturn(0);
950da3a660dSBarry Smith     }
9512e8a6d31SBarry Smith     ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
9522e8a6d31SBarry Smith     ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
953d6dfbf8fSBarry Smith     while (its--) {
954d6dfbf8fSBarry Smith       for ( i=m-1; i>-1; i-- ) {
955d6dfbf8fSBarry Smith         n    = A->i[i+1] - A->i[i];
9564d197716SBarry Smith 	PLogFlops(4*n+3);
957dbb450caSBarry Smith         idx  = A->j + A->i[i] + shift;
958dbb450caSBarry Smith         v    = A->a + A->i[i] + shift;
959d6dfbf8fSBarry Smith         sum  = b[i];
960d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
961dbb450caSBarry Smith         d    = fshift + A->a[diag[i]+shift];
962d6dfbf8fSBarry Smith         n    = B->i[i+1] - B->i[i];
963dbb450caSBarry Smith         idx  = B->j + B->i[i] + shift;
964dbb450caSBarry Smith         v    = B->a + B->i[i] + shift;
965d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,ls,v,idx,n);
96655a1b374SBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + A->a[diag[i]+shift]*x[i])/d;
967d6dfbf8fSBarry Smith       }
968d6dfbf8fSBarry Smith     }
9693a40ed3dSBarry Smith   } else {
970a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_SUP,0,"Parallel SOR not supported");
971c16cb8f2SBarry Smith   }
9722e8a6d31SBarry Smith   ierr = VecRestoreArray(xx,&x); CHKERRQ(ierr);
9732e8a6d31SBarry Smith   ierr = VecRestoreArray(bb,&b); CHKERRQ(ierr);
9742e8a6d31SBarry Smith   ierr = VecRestoreArray(mat->lvec,&ls);CHKERRQ(ierr);
9753a40ed3dSBarry Smith   PetscFunctionReturn(0);
9768a729477SBarry Smith }
977a66be287SLois Curfman McInnes 
9785615d1e5SSatish Balay #undef __FUNC__
979d4bb536fSBarry Smith #define __FUNC__ "MatGetInfo_MPIAIJ"
9808f6be9afSLois Curfman McInnes int MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info)
981a66be287SLois Curfman McInnes {
982a66be287SLois Curfman McInnes   Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data;
983a66be287SLois Curfman McInnes   Mat        A = mat->A, B = mat->B;
9844e220ebcSLois Curfman McInnes   int        ierr;
9854e220ebcSLois Curfman McInnes   double     isend[5], irecv[5];
986a66be287SLois Curfman McInnes 
9873a40ed3dSBarry Smith   PetscFunctionBegin;
9884e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
9894e220ebcSLois Curfman McInnes   ierr = MatGetInfo(A,MAT_LOCAL,info); CHKERRQ(ierr);
9904e220ebcSLois Curfman McInnes   isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
9914e220ebcSLois Curfman McInnes   isend[3] = info->memory;  isend[4] = info->mallocs;
9924e220ebcSLois Curfman McInnes   ierr = MatGetInfo(B,MAT_LOCAL,info); CHKERRQ(ierr);
9934e220ebcSLois Curfman McInnes   isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
9944e220ebcSLois Curfman McInnes   isend[3] += info->memory;  isend[4] += info->mallocs;
995a66be287SLois Curfman McInnes   if (flag == MAT_LOCAL) {
9964e220ebcSLois Curfman McInnes     info->nz_used      = isend[0];
9974e220ebcSLois Curfman McInnes     info->nz_allocated = isend[1];
9984e220ebcSLois Curfman McInnes     info->nz_unneeded  = isend[2];
9994e220ebcSLois Curfman McInnes     info->memory       = isend[3];
10004e220ebcSLois Curfman McInnes     info->mallocs      = isend[4];
1001a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_MAX) {
1002ca161407SBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPI_DOUBLE,MPI_MAX,matin->comm);CHKERRQ(ierr);
10034e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
10044e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
10054e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
10064e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
10074e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1008a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_SUM) {
1009ca161407SBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPI_DOUBLE,MPI_SUM,matin->comm);CHKERRQ(ierr);
10104e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
10114e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
10124e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
10134e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
10144e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1015a66be287SLois Curfman McInnes   }
10164e220ebcSLois Curfman McInnes   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
10174e220ebcSLois Curfman McInnes   info->fill_ratio_needed = 0;
10184e220ebcSLois Curfman McInnes   info->factor_mallocs    = 0;
10198d700155SBarry Smith   info->rows_global       = (double)mat->M;
10208d700155SBarry Smith   info->columns_global    = (double)mat->N;
10218d700155SBarry Smith   info->rows_local        = (double)mat->m;
10228d700155SBarry Smith   info->columns_local     = (double)mat->N;
10234e220ebcSLois Curfman McInnes 
10243a40ed3dSBarry Smith   PetscFunctionReturn(0);
1025a66be287SLois Curfman McInnes }
1026a66be287SLois Curfman McInnes 
10275615d1e5SSatish Balay #undef __FUNC__
1028d4bb536fSBarry Smith #define __FUNC__ "MatSetOption_MPIAIJ"
10298f6be9afSLois Curfman McInnes int MatSetOption_MPIAIJ(Mat A,MatOption op)
1030c74985f6SBarry Smith {
1031c0bbcb79SLois Curfman McInnes   Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data;
10321dee9f54SBarry Smith   int        ierr;
1033c74985f6SBarry Smith 
10343a40ed3dSBarry Smith   PetscFunctionBegin;
10356d4a8577SBarry Smith   if (op == MAT_NO_NEW_NONZERO_LOCATIONS ||
10366d4a8577SBarry Smith       op == MAT_YES_NEW_NONZERO_LOCATIONS ||
10376da5968aSLois Curfman McInnes       op == MAT_COLUMNS_UNSORTED ||
1038c2653b3dSLois Curfman McInnes       op == MAT_COLUMNS_SORTED ||
10394787f768SSatish Balay       op == MAT_NEW_NONZERO_ALLOCATION_ERR ||
10404787f768SSatish Balay       op == MAT_NEW_NONZERO_LOCATION_ERR) {
10411dee9f54SBarry Smith         ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
10421dee9f54SBarry Smith         ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
1043b1fbbac0SLois Curfman McInnes   } else if (op == MAT_ROW_ORIENTED) {
1044aeafbbfcSLois Curfman McInnes     a->roworiented = 1;
10451dee9f54SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
10461dee9f54SBarry Smith     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
1047b1fbbac0SLois Curfman McInnes   } else if (op == MAT_ROWS_SORTED ||
10486da5968aSLois Curfman McInnes              op == MAT_ROWS_UNSORTED ||
10496d4a8577SBarry Smith              op == MAT_SYMMETRIC ||
10506d4a8577SBarry Smith              op == MAT_STRUCTURALLY_SYMMETRIC ||
10516d4a8577SBarry Smith              op == MAT_YES_NEW_DIAGONALS)
1052981c4779SBarry Smith     PLogInfo(A,"MatSetOption_MPIAIJ:Option ignored\n");
10536d4a8577SBarry Smith   else if (op == MAT_COLUMN_ORIENTED) {
10544b0e389bSBarry Smith     a->roworiented = 0;
10551dee9f54SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
10561dee9f54SBarry Smith     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
10572b362799SSatish Balay   } else if (op == MAT_IGNORE_OFF_PROC_ENTRIES) {
105890f02eecSBarry Smith     a->donotstash = 1;
10593a40ed3dSBarry Smith   } else if (op == MAT_NO_NEW_DIAGONALS){
10603a40ed3dSBarry Smith     SETERRQ(PETSC_ERR_SUP,0,"MAT_NO_NEW_DIAGONALS");
10613a40ed3dSBarry Smith   } else {
10623a40ed3dSBarry Smith     SETERRQ(PETSC_ERR_SUP,0,"unknown option");
10633a40ed3dSBarry Smith   }
10643a40ed3dSBarry Smith   PetscFunctionReturn(0);
1065c74985f6SBarry Smith }
1066c74985f6SBarry Smith 
10675615d1e5SSatish Balay #undef __FUNC__
1068d4bb536fSBarry Smith #define __FUNC__ "MatGetSize_MPIAIJ"
10698f6be9afSLois Curfman McInnes int MatGetSize_MPIAIJ(Mat matin,int *m,int *n)
1070c74985f6SBarry Smith {
107144a69424SLois Curfman McInnes   Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data;
10723a40ed3dSBarry Smith 
10733a40ed3dSBarry Smith   PetscFunctionBegin;
10740752156aSBarry Smith   if (m) *m = mat->M;
10750752156aSBarry Smith   if (n) *n = mat->N;
10763a40ed3dSBarry Smith   PetscFunctionReturn(0);
1077c74985f6SBarry Smith }
1078c74985f6SBarry Smith 
10795615d1e5SSatish Balay #undef __FUNC__
1080d4bb536fSBarry Smith #define __FUNC__ "MatGetLocalSize_MPIAIJ"
10818f6be9afSLois Curfman McInnes int MatGetLocalSize_MPIAIJ(Mat matin,int *m,int *n)
1082c74985f6SBarry Smith {
108344a69424SLois Curfman McInnes   Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data;
10843a40ed3dSBarry Smith 
10853a40ed3dSBarry Smith   PetscFunctionBegin;
10860752156aSBarry Smith   if (m) *m = mat->m;
1087f830108cSBarry Smith   if (n) *n = mat->n;
10883a40ed3dSBarry Smith   PetscFunctionReturn(0);
1089c74985f6SBarry Smith }
1090c74985f6SBarry Smith 
10915615d1e5SSatish Balay #undef __FUNC__
1092d4bb536fSBarry Smith #define __FUNC__ "MatGetOwnershipRange_MPIAIJ"
10938f6be9afSLois Curfman McInnes int MatGetOwnershipRange_MPIAIJ(Mat matin,int *m,int *n)
1094c74985f6SBarry Smith {
109544a69424SLois Curfman McInnes   Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data;
10963a40ed3dSBarry Smith 
10973a40ed3dSBarry Smith   PetscFunctionBegin;
1098c74985f6SBarry Smith   *m = mat->rstart; *n = mat->rend;
10993a40ed3dSBarry Smith   PetscFunctionReturn(0);
1100c74985f6SBarry Smith }
1101c74985f6SBarry Smith 
11025615d1e5SSatish Balay #undef __FUNC__
11035615d1e5SSatish Balay #define __FUNC__ "MatGetRow_MPIAIJ"
11046d84be18SBarry Smith int MatGetRow_MPIAIJ(Mat matin,int row,int *nz,int **idx,Scalar **v)
110539e00950SLois Curfman McInnes {
1106154123eaSLois Curfman McInnes   Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data;
110770f0671dSBarry Smith   Scalar     *vworkA, *vworkB, **pvA, **pvB,*v_p;
1108154123eaSLois Curfman McInnes   int        i, ierr, *cworkA, *cworkB, **pcA, **pcB, cstart = mat->cstart;
1109154123eaSLois Curfman McInnes   int        nztot, nzA, nzB, lrow, rstart = mat->rstart, rend = mat->rend;
111070f0671dSBarry Smith   int        *cmap, *idx_p;
111139e00950SLois Curfman McInnes 
11123a40ed3dSBarry Smith   PetscFunctionBegin;
1113a8c6a408SBarry Smith   if (mat->getrowactive == PETSC_TRUE) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Already active");
11147a0afa10SBarry Smith   mat->getrowactive = PETSC_TRUE;
11157a0afa10SBarry Smith 
111670f0671dSBarry Smith   if (!mat->rowvalues && (idx || v)) {
11177a0afa10SBarry Smith     /*
11187a0afa10SBarry Smith         allocate enough space to hold information from the longest row.
11197a0afa10SBarry Smith     */
11207a0afa10SBarry Smith     Mat_SeqAIJ *Aa = (Mat_SeqAIJ *) mat->A->data,*Ba = (Mat_SeqAIJ *) mat->B->data;
1121c16cb8f2SBarry Smith     int     max = 1,m = mat->m,tmp;
1122c16cb8f2SBarry Smith     for ( i=0; i<m; i++ ) {
11237a0afa10SBarry Smith       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
11247a0afa10SBarry Smith       if (max < tmp) { max = tmp; }
11257a0afa10SBarry Smith     }
11263f97c4b0SBarry Smith     mat->rowvalues  = (Scalar *) PetscMalloc(max*(sizeof(int)+sizeof(Scalar)));CHKPTRQ(mat->rowvalues);
11277a0afa10SBarry Smith     mat->rowindices = (int *) (mat->rowvalues + max);
11287a0afa10SBarry Smith   }
11297a0afa10SBarry Smith 
1130a8c6a408SBarry Smith   if (row < rstart || row >= rend) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Only local rows")
1131abc0e9e4SLois Curfman McInnes   lrow = row - rstart;
113239e00950SLois Curfman McInnes 
1133154123eaSLois Curfman McInnes   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1134154123eaSLois Curfman McInnes   if (!v)   {pvA = 0; pvB = 0;}
1135154123eaSLois Curfman McInnes   if (!idx) {pcA = 0; if (!v) pcB = 0;}
1136f830108cSBarry Smith   ierr = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA); CHKERRQ(ierr);
1137f830108cSBarry Smith   ierr = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB); CHKERRQ(ierr);
1138154123eaSLois Curfman McInnes   nztot = nzA + nzB;
1139154123eaSLois Curfman McInnes 
114070f0671dSBarry Smith   cmap  = mat->garray;
1141154123eaSLois Curfman McInnes   if (v  || idx) {
1142154123eaSLois Curfman McInnes     if (nztot) {
1143154123eaSLois Curfman McInnes       /* Sort by increasing column numbers, assuming A and B already sorted */
114470f0671dSBarry Smith       int imark = -1;
1145154123eaSLois Curfman McInnes       if (v) {
114670f0671dSBarry Smith         *v = v_p = mat->rowvalues;
114739e00950SLois Curfman McInnes         for ( i=0; i<nzB; i++ ) {
114870f0671dSBarry Smith           if (cmap[cworkB[i]] < cstart)   v_p[i] = vworkB[i];
1149154123eaSLois Curfman McInnes           else break;
1150154123eaSLois Curfman McInnes         }
1151154123eaSLois Curfman McInnes         imark = i;
115270f0671dSBarry Smith         for ( i=0; i<nzA; i++ )     v_p[imark+i] = vworkA[i];
115370f0671dSBarry Smith         for ( i=imark; i<nzB; i++ ) v_p[nzA+i]   = vworkB[i];
1154154123eaSLois Curfman McInnes       }
1155154123eaSLois Curfman McInnes       if (idx) {
115670f0671dSBarry Smith         *idx = idx_p = mat->rowindices;
115770f0671dSBarry Smith         if (imark > -1) {
115870f0671dSBarry Smith           for ( i=0; i<imark; i++ ) {
115970f0671dSBarry Smith             idx_p[i] = cmap[cworkB[i]];
116070f0671dSBarry Smith           }
116170f0671dSBarry Smith         } else {
1162154123eaSLois Curfman McInnes           for ( i=0; i<nzB; i++ ) {
116370f0671dSBarry Smith             if (cmap[cworkB[i]] < cstart)   idx_p[i] = cmap[cworkB[i]];
1164154123eaSLois Curfman McInnes             else break;
1165154123eaSLois Curfman McInnes           }
1166154123eaSLois Curfman McInnes           imark = i;
116770f0671dSBarry Smith         }
116870f0671dSBarry Smith         for ( i=0; i<nzA; i++ )     idx_p[imark+i] = cstart + cworkA[i];
116970f0671dSBarry Smith         for ( i=imark; i<nzB; i++ ) idx_p[nzA+i]   = cmap[cworkB[i]];
117039e00950SLois Curfman McInnes       }
11713f97c4b0SBarry Smith     } else {
11721ca473b0SSatish Balay       if (idx) *idx = 0;
11731ca473b0SSatish Balay       if (v)   *v   = 0;
11741ca473b0SSatish Balay     }
1175154123eaSLois Curfman McInnes   }
117639e00950SLois Curfman McInnes   *nz = nztot;
1177f830108cSBarry Smith   ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA); CHKERRQ(ierr);
1178f830108cSBarry Smith   ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB); CHKERRQ(ierr);
11793a40ed3dSBarry Smith   PetscFunctionReturn(0);
118039e00950SLois Curfman McInnes }
118139e00950SLois Curfman McInnes 
11825615d1e5SSatish Balay #undef __FUNC__
1183d4bb536fSBarry Smith #define __FUNC__ "MatRestoreRow_MPIAIJ"
11846d84be18SBarry Smith int MatRestoreRow_MPIAIJ(Mat mat,int row,int *nz,int **idx,Scalar **v)
118539e00950SLois Curfman McInnes {
11867a0afa10SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data;
11873a40ed3dSBarry Smith 
11883a40ed3dSBarry Smith   PetscFunctionBegin;
11897a0afa10SBarry Smith   if (aij->getrowactive == PETSC_FALSE) {
1190a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"MatGetRow not called");
11917a0afa10SBarry Smith   }
11927a0afa10SBarry Smith   aij->getrowactive = PETSC_FALSE;
11933a40ed3dSBarry Smith   PetscFunctionReturn(0);
119439e00950SLois Curfman McInnes }
119539e00950SLois Curfman McInnes 
11965615d1e5SSatish Balay #undef __FUNC__
11975615d1e5SSatish Balay #define __FUNC__ "MatNorm_MPIAIJ"
11988f6be9afSLois Curfman McInnes int MatNorm_MPIAIJ(Mat mat,NormType type,double *norm)
1199855ac2c5SLois Curfman McInnes {
1200855ac2c5SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data;
1201ec8511deSBarry Smith   Mat_SeqAIJ *amat = (Mat_SeqAIJ*) aij->A->data, *bmat = (Mat_SeqAIJ*) aij->B->data;
1202416022c9SBarry Smith   int        ierr, i, j, cstart = aij->cstart,shift = amat->indexshift;
1203416022c9SBarry Smith   double     sum = 0.0;
120404ca555eSLois Curfman McInnes   Scalar     *v;
120504ca555eSLois Curfman McInnes 
12063a40ed3dSBarry Smith   PetscFunctionBegin;
120717699dbbSLois Curfman McInnes   if (aij->size == 1) {
120814183eadSLois Curfman McInnes     ierr =  MatNorm(aij->A,type,norm); CHKERRQ(ierr);
120937fa93a5SLois Curfman McInnes   } else {
121004ca555eSLois Curfman McInnes     if (type == NORM_FROBENIUS) {
121104ca555eSLois Curfman McInnes       v = amat->a;
121204ca555eSLois Curfman McInnes       for (i=0; i<amat->nz; i++ ) {
12133a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX)
1214e20fef11SSatish Balay         sum += PetscReal(PetscConj(*v)*(*v)); v++;
121504ca555eSLois Curfman McInnes #else
121604ca555eSLois Curfman McInnes         sum += (*v)*(*v); v++;
121704ca555eSLois Curfman McInnes #endif
121804ca555eSLois Curfman McInnes       }
121904ca555eSLois Curfman McInnes       v = bmat->a;
122004ca555eSLois Curfman McInnes       for (i=0; i<bmat->nz; i++ ) {
12213a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX)
1222e20fef11SSatish Balay         sum += PetscReal(PetscConj(*v)*(*v)); v++;
122304ca555eSLois Curfman McInnes #else
122404ca555eSLois Curfman McInnes         sum += (*v)*(*v); v++;
122504ca555eSLois Curfman McInnes #endif
122604ca555eSLois Curfman McInnes       }
1227ca161407SBarry Smith       ierr = MPI_Allreduce(&sum,norm,1,MPI_DOUBLE,MPI_SUM,mat->comm);CHKERRQ(ierr);
122804ca555eSLois Curfman McInnes       *norm = sqrt(*norm);
12293a40ed3dSBarry Smith     } else if (type == NORM_1) { /* max column norm */
123004ca555eSLois Curfman McInnes       double *tmp, *tmp2;
123104ca555eSLois Curfman McInnes       int    *jj, *garray = aij->garray;
1232758f045eSSatish Balay       tmp  = (double *) PetscMalloc( (aij->N+1)*sizeof(double) ); CHKPTRQ(tmp);
1233758f045eSSatish Balay       tmp2 = (double *) PetscMalloc( (aij->N+1)*sizeof(double) ); CHKPTRQ(tmp2);
1234cddf8d76SBarry Smith       PetscMemzero(tmp,aij->N*sizeof(double));
123504ca555eSLois Curfman McInnes       *norm = 0.0;
123604ca555eSLois Curfman McInnes       v = amat->a; jj = amat->j;
123704ca555eSLois Curfman McInnes       for ( j=0; j<amat->nz; j++ ) {
1238579c6b6fSBarry Smith         tmp[cstart + *jj++ + shift] += PetscAbsScalar(*v);  v++;
123904ca555eSLois Curfman McInnes       }
124004ca555eSLois Curfman McInnes       v = bmat->a; jj = bmat->j;
124104ca555eSLois Curfman McInnes       for ( j=0; j<bmat->nz; j++ ) {
1242579c6b6fSBarry Smith         tmp[garray[*jj++ + shift]] += PetscAbsScalar(*v); v++;
124304ca555eSLois Curfman McInnes       }
1244ca161407SBarry Smith       ierr = MPI_Allreduce(tmp,tmp2,aij->N,MPI_DOUBLE,MPI_SUM,mat->comm);CHKERRQ(ierr);
124504ca555eSLois Curfman McInnes       for ( j=0; j<aij->N; j++ ) {
124604ca555eSLois Curfman McInnes         if (tmp2[j] > *norm) *norm = tmp2[j];
124704ca555eSLois Curfman McInnes       }
12480452661fSBarry Smith       PetscFree(tmp); PetscFree(tmp2);
12493a40ed3dSBarry Smith     } else if (type == NORM_INFINITY) { /* max row norm */
1250515d9167SLois Curfman McInnes       double ntemp = 0.0;
125104ca555eSLois Curfman McInnes       for ( j=0; j<amat->m; j++ ) {
1252dbb450caSBarry Smith         v = amat->a + amat->i[j] + shift;
125304ca555eSLois Curfman McInnes         sum = 0.0;
125404ca555eSLois Curfman McInnes         for ( i=0; i<amat->i[j+1]-amat->i[j]; i++ ) {
1255cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
125604ca555eSLois Curfman McInnes         }
1257dbb450caSBarry Smith         v = bmat->a + bmat->i[j] + shift;
125804ca555eSLois Curfman McInnes         for ( i=0; i<bmat->i[j+1]-bmat->i[j]; i++ ) {
1259cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
126004ca555eSLois Curfman McInnes         }
1261515d9167SLois Curfman McInnes         if (sum > ntemp) ntemp = sum;
126204ca555eSLois Curfman McInnes       }
1263ca161407SBarry Smith       ierr = MPI_Allreduce(&ntemp,norm,1,MPI_DOUBLE,MPI_MAX,mat->comm);CHKERRQ(ierr);
1264ca161407SBarry Smith     } else {
1265a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_SUP,0,"No support for two norm");
126604ca555eSLois Curfman McInnes     }
126737fa93a5SLois Curfman McInnes   }
12683a40ed3dSBarry Smith   PetscFunctionReturn(0);
1269855ac2c5SLois Curfman McInnes }
1270855ac2c5SLois Curfman McInnes 
12715615d1e5SSatish Balay #undef __FUNC__
12725615d1e5SSatish Balay #define __FUNC__ "MatTranspose_MPIAIJ"
12738f6be9afSLois Curfman McInnes int MatTranspose_MPIAIJ(Mat A,Mat *matout)
1274b7c46309SBarry Smith {
1275b7c46309SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data;
1276dbb450caSBarry Smith   Mat_SeqAIJ *Aloc = (Mat_SeqAIJ *) a->A->data;
1277416022c9SBarry Smith   int        ierr,shift = Aloc->indexshift;
1278b7c46309SBarry Smith   int        M = a->M, N = a->N,m,*ai,*aj,row,*cols,i,*ct;
12793a40ed3dSBarry Smith   Mat        B;
1280b7c46309SBarry Smith   Scalar     *array;
1281b7c46309SBarry Smith 
12823a40ed3dSBarry Smith   PetscFunctionBegin;
1283d4bb536fSBarry Smith   if (matout == PETSC_NULL && M != N) {
1284a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_SIZ,0,"Square matrix only for in-place");
1285d4bb536fSBarry Smith   }
1286d4bb536fSBarry Smith 
1287d4bb536fSBarry Smith   ierr = MatCreateMPIAIJ(A->comm,a->n,a->m,N,M,0,PETSC_NULL,0,PETSC_NULL,&B);CHKERRQ(ierr);
1288b7c46309SBarry Smith 
1289b7c46309SBarry Smith   /* copy over the A part */
1290ec8511deSBarry Smith   Aloc = (Mat_SeqAIJ*) a->A->data;
1291b7c46309SBarry Smith   m = Aloc->m; ai = Aloc->i; aj = Aloc->j; array = Aloc->a;
1292b7c46309SBarry Smith   row = a->rstart;
1293dbb450caSBarry Smith   for ( i=0; i<ai[m]+shift; i++ ) {aj[i] += a->cstart + shift;}
1294b7c46309SBarry Smith   for ( i=0; i<m; i++ ) {
1295416022c9SBarry Smith     ierr = MatSetValues(B,ai[i+1]-ai[i],aj,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
1296b7c46309SBarry Smith     row++; array += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
1297b7c46309SBarry Smith   }
1298b7c46309SBarry Smith   aj = Aloc->j;
12994af08d9eSBarry Smith   for ( i=0; i<ai[m]+shift; i++ ) {aj[i] -= a->cstart + shift;}
1300b7c46309SBarry Smith 
1301b7c46309SBarry Smith   /* copy over the B part */
1302ec8511deSBarry Smith   Aloc = (Mat_SeqAIJ*) a->B->data;
1303b7c46309SBarry Smith   m = Aloc->m;  ai = Aloc->i; aj = Aloc->j; array = Aloc->a;
1304b7c46309SBarry Smith   row = a->rstart;
13050452661fSBarry Smith   ct = cols = (int *) PetscMalloc( (1+ai[m]-shift)*sizeof(int) ); CHKPTRQ(cols);
1306dbb450caSBarry Smith   for ( i=0; i<ai[m]+shift; i++ ) {cols[i] = a->garray[aj[i]+shift];}
1307b7c46309SBarry Smith   for ( i=0; i<m; i++ ) {
1308416022c9SBarry Smith     ierr = MatSetValues(B,ai[i+1]-ai[i],cols,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
1309b7c46309SBarry Smith     row++; array += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
1310b7c46309SBarry Smith   }
13110452661fSBarry Smith   PetscFree(ct);
13126d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
13136d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
13143638b69dSLois Curfman McInnes   if (matout != PETSC_NULL) {
13150de55854SLois Curfman McInnes     *matout = B;
13160de55854SLois Curfman McInnes   } else {
1317f830108cSBarry Smith     PetscOps       *Abops;
1318f830108cSBarry Smith     struct _MatOps *Aops;
1319f830108cSBarry Smith 
13200de55854SLois Curfman McInnes     /* This isn't really an in-place transpose .... but free data structures from a */
13210452661fSBarry Smith     PetscFree(a->rowners);
13220de55854SLois Curfman McInnes     ierr = MatDestroy(a->A); CHKERRQ(ierr);
13230de55854SLois Curfman McInnes     ierr = MatDestroy(a->B); CHKERRQ(ierr);
1324b1fc9764SSatish Balay #if defined (USE_CTABLE)
1325b1fc9764SSatish Balay     if (a->colmap) TableDelete(a->colmap);
1326b1fc9764SSatish Balay #else
13270452661fSBarry Smith     if (a->colmap) PetscFree(a->colmap);
1328b1fc9764SSatish Balay #endif
13290452661fSBarry Smith     if (a->garray) PetscFree(a->garray);
13300de55854SLois Curfman McInnes     if (a->lvec) VecDestroy(a->lvec);
1331a56f8943SBarry Smith     if (a->Mvctx) VecScatterDestroy(a->Mvctx);
13320452661fSBarry Smith     PetscFree(a);
1333f830108cSBarry Smith 
1334f830108cSBarry Smith     /*
1335f830108cSBarry Smith        This is horrible, horrible code. We need to keep the
1336f830108cSBarry Smith       A pointers for the bops and ops but copy everything
1337f830108cSBarry Smith       else from C.
1338f830108cSBarry Smith     */
1339f830108cSBarry Smith     Abops = A->bops;
1340f830108cSBarry Smith     Aops  = A->ops;
1341f09e8eb9SSatish Balay     PetscMemcpy(A,B,sizeof(struct _p_Mat));
1342f830108cSBarry Smith     A->bops = Abops;
1343f830108cSBarry Smith     A->ops  = Aops;
13440452661fSBarry Smith     PetscHeaderDestroy(B);
13450de55854SLois Curfman McInnes   }
13463a40ed3dSBarry Smith   PetscFunctionReturn(0);
1347b7c46309SBarry Smith }
1348b7c46309SBarry Smith 
13495615d1e5SSatish Balay #undef __FUNC__
13505615d1e5SSatish Balay #define __FUNC__ "MatDiagonalScale_MPIAIJ"
13514b967eb1SSatish Balay int MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr)
1352a008b906SSatish Balay {
13534b967eb1SSatish Balay   Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data;
13544b967eb1SSatish Balay   Mat a = aij->A, b = aij->B;
1355a008b906SSatish Balay   int ierr,s1,s2,s3;
1356a008b906SSatish Balay 
13573a40ed3dSBarry Smith   PetscFunctionBegin;
13584b967eb1SSatish Balay   ierr = MatGetLocalSize(mat,&s2,&s3); CHKERRQ(ierr);
13594b967eb1SSatish Balay   if (rr) {
1360e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr);
1361a8c6a408SBarry Smith     if (s1!=s3) SETERRQ(PETSC_ERR_ARG_SIZ,0,"right vector non-conforming local size");
13624b967eb1SSatish Balay     /* Overlap communication with computation. */
136343a90d84SBarry Smith     ierr = VecScatterBegin(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx); CHKERRQ(ierr);
1364a008b906SSatish Balay   }
13654b967eb1SSatish Balay   if (ll) {
1366e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr);
1367a8c6a408SBarry Smith     if (s1!=s2) SETERRQ(PETSC_ERR_ARG_SIZ,0,"left vector non-conforming local size");
1368f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,ll,0); CHKERRQ(ierr);
13694b967eb1SSatish Balay   }
13704b967eb1SSatish Balay   /* scale  the diagonal block */
1371f830108cSBarry Smith   ierr = (*a->ops->diagonalscale)(a,ll,rr); CHKERRQ(ierr);
13724b967eb1SSatish Balay 
13734b967eb1SSatish Balay   if (rr) {
13744b967eb1SSatish Balay     /* Do a scatter end and then right scale the off-diagonal block */
137543a90d84SBarry Smith     ierr = VecScatterEnd(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx); CHKERRQ(ierr);
1376f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,0,aij->lvec); CHKERRQ(ierr);
13774b967eb1SSatish Balay   }
13784b967eb1SSatish Balay 
13793a40ed3dSBarry Smith   PetscFunctionReturn(0);
1380a008b906SSatish Balay }
1381a008b906SSatish Balay 
1382a008b906SSatish Balay 
13835615d1e5SSatish Balay #undef __FUNC__
1384d4bb536fSBarry Smith #define __FUNC__ "MatPrintHelp_MPIAIJ"
13858f6be9afSLois Curfman McInnes int MatPrintHelp_MPIAIJ(Mat A)
1386682d7d0cSBarry Smith {
1387682d7d0cSBarry Smith   Mat_MPIAIJ *a   = (Mat_MPIAIJ*) A->data;
13883a40ed3dSBarry Smith   int        ierr;
1389682d7d0cSBarry Smith 
13903a40ed3dSBarry Smith   PetscFunctionBegin;
13913a40ed3dSBarry Smith   if (!a->rank) {
13923a40ed3dSBarry Smith     ierr = MatPrintHelp_SeqAIJ(a->A);CHKERRQ(ierr);
13933a40ed3dSBarry Smith   }
13943a40ed3dSBarry Smith   PetscFunctionReturn(0);
1395682d7d0cSBarry Smith }
1396682d7d0cSBarry Smith 
13975615d1e5SSatish Balay #undef __FUNC__
1398d4bb536fSBarry Smith #define __FUNC__ "MatGetBlockSize_MPIAIJ"
13998f6be9afSLois Curfman McInnes int MatGetBlockSize_MPIAIJ(Mat A,int *bs)
14005a838052SSatish Balay {
14013a40ed3dSBarry Smith   PetscFunctionBegin;
14025a838052SSatish Balay   *bs = 1;
14033a40ed3dSBarry Smith   PetscFunctionReturn(0);
14045a838052SSatish Balay }
14055615d1e5SSatish Balay #undef __FUNC__
1406d4bb536fSBarry Smith #define __FUNC__ "MatSetUnfactored_MPIAIJ"
14078f6be9afSLois Curfman McInnes int MatSetUnfactored_MPIAIJ(Mat A)
1408bb5a7306SBarry Smith {
1409bb5a7306SBarry Smith   Mat_MPIAIJ *a   = (Mat_MPIAIJ*) A->data;
1410bb5a7306SBarry Smith   int        ierr;
14113a40ed3dSBarry Smith 
14123a40ed3dSBarry Smith   PetscFunctionBegin;
1413bb5a7306SBarry Smith   ierr = MatSetUnfactored(a->A); CHKERRQ(ierr);
14143a40ed3dSBarry Smith   PetscFunctionReturn(0);
1415bb5a7306SBarry Smith }
1416bb5a7306SBarry Smith 
1417d4bb536fSBarry Smith #undef __FUNC__
1418d4bb536fSBarry Smith #define __FUNC__ "MatEqual_MPIAIJ"
1419d4bb536fSBarry Smith int MatEqual_MPIAIJ(Mat A, Mat B, PetscTruth *flag)
1420d4bb536fSBarry Smith {
1421d4bb536fSBarry Smith   Mat_MPIAIJ *matB = (Mat_MPIAIJ *) B->data,*matA = (Mat_MPIAIJ *) A->data;
1422d4bb536fSBarry Smith   Mat        a, b, c, d;
1423d4bb536fSBarry Smith   PetscTruth flg;
1424d4bb536fSBarry Smith   int        ierr;
1425d4bb536fSBarry Smith 
14263a40ed3dSBarry Smith   PetscFunctionBegin;
1427a8c6a408SBarry Smith   if (B->type != MATMPIAIJ) SETERRQ(PETSC_ERR_ARG_INCOMP,0,"Matrices must be same type");
1428d4bb536fSBarry Smith   a = matA->A; b = matA->B;
1429d4bb536fSBarry Smith   c = matB->A; d = matB->B;
1430d4bb536fSBarry Smith 
1431d4bb536fSBarry Smith   ierr = MatEqual(a, c, &flg); CHKERRQ(ierr);
1432d4bb536fSBarry Smith   if (flg == PETSC_TRUE) {
1433d4bb536fSBarry Smith     ierr = MatEqual(b, d, &flg); CHKERRQ(ierr);
1434d4bb536fSBarry Smith   }
1435ca161407SBarry Smith   ierr = MPI_Allreduce(&flg, flag, 1, MPI_INT, MPI_LAND, A->comm);CHKERRQ(ierr);
14363a40ed3dSBarry Smith   PetscFunctionReturn(0);
1437d4bb536fSBarry Smith }
1438d4bb536fSBarry Smith 
1439cb5b572fSBarry Smith #undef __FUNC__
1440cb5b572fSBarry Smith #define __FUNC__ "MatCopy_MPIAIJ"
1441cb5b572fSBarry Smith int MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str)
1442cb5b572fSBarry Smith {
1443cb5b572fSBarry Smith   int        ierr;
1444cb5b572fSBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;
1445cb5b572fSBarry Smith   Mat_MPIAIJ *b = (Mat_MPIAIJ *)B->data;
1446cb5b572fSBarry Smith 
1447cb5b572fSBarry Smith   PetscFunctionBegin;
1448cb5b572fSBarry Smith   if (str != SAME_NONZERO_PATTERN || B->type != MATMPIAIJ) {
1449cb5b572fSBarry Smith     /* because of the column compression in the off-processor part of the matrix a->B,
1450cb5b572fSBarry Smith        the number of columns in a->B and b->B may be different, hence we cannot call
1451cb5b572fSBarry Smith        the MatCopy() directly on the two parts. If need be, we can provide a more
1452cb5b572fSBarry Smith        efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices
1453cb5b572fSBarry Smith        then copying the submatrices */
1454cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1455cb5b572fSBarry Smith   } else {
1456cb5b572fSBarry Smith     ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr);
1457cb5b572fSBarry Smith     ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr);
1458cb5b572fSBarry Smith   }
1459cb5b572fSBarry Smith   PetscFunctionReturn(0);
1460cb5b572fSBarry Smith }
1461cb5b572fSBarry Smith 
14622e8a6d31SBarry Smith extern int MatDuplicate_MPIAIJ(Mat,MatDuplicateOption,Mat *);
14632f86bd48SSatish Balay extern int MatIncreaseOverlap_MPIAIJ(Mat , int, IS *, int);
14640a198c4cSBarry Smith extern int MatFDColoringCreate_MPIAIJ(Mat,ISColoring,MatFDColoring);
14657b2a1423SBarry Smith extern int MatGetSubMatrices_MPIAIJ (Mat ,int , IS *,IS *,MatReuse,Mat **);
14667b2a1423SBarry Smith extern int MatGetSubMatrix_MPIAIJ (Mat ,IS,IS,int,MatReuse,Mat *);
146700e6dbe6SBarry Smith 
14688a729477SBarry Smith /* -------------------------------------------------------------------*/
1469cda55fadSBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ,
1470cda55fadSBarry Smith        MatGetRow_MPIAIJ,
1471cda55fadSBarry Smith        MatRestoreRow_MPIAIJ,
1472cda55fadSBarry Smith        MatMult_MPIAIJ,
1473cda55fadSBarry Smith        MatMultAdd_MPIAIJ,
1474cda55fadSBarry Smith        MatMultTrans_MPIAIJ,
1475cda55fadSBarry Smith        MatMultTransAdd_MPIAIJ,
1476cda55fadSBarry Smith        0,
1477cda55fadSBarry Smith        0,
1478cda55fadSBarry Smith        0,
1479cda55fadSBarry Smith        0,
1480cda55fadSBarry Smith        0,
1481cda55fadSBarry Smith        0,
148244a69424SLois Curfman McInnes        MatRelax_MPIAIJ,
1483b7c46309SBarry Smith        MatTranspose_MPIAIJ,
1484cda55fadSBarry Smith        MatGetInfo_MPIAIJ,
1485cda55fadSBarry Smith        MatEqual_MPIAIJ,
1486cda55fadSBarry Smith        MatGetDiagonal_MPIAIJ,
1487cda55fadSBarry Smith        MatDiagonalScale_MPIAIJ,
1488cda55fadSBarry Smith        MatNorm_MPIAIJ,
1489cda55fadSBarry Smith        MatAssemblyBegin_MPIAIJ,
1490cda55fadSBarry Smith        MatAssemblyEnd_MPIAIJ,
14911eb62cbbSBarry Smith        0,
1492cda55fadSBarry Smith        MatSetOption_MPIAIJ,
1493cda55fadSBarry Smith        MatZeroEntries_MPIAIJ,
1494cda55fadSBarry Smith        MatZeroRows_MPIAIJ,
1495cda55fadSBarry Smith        0,
1496cda55fadSBarry Smith        0,
1497cda55fadSBarry Smith        0,
1498cda55fadSBarry Smith        0,
1499cda55fadSBarry Smith        MatGetSize_MPIAIJ,
1500cda55fadSBarry Smith        MatGetLocalSize_MPIAIJ,
1501cda55fadSBarry Smith        MatGetOwnershipRange_MPIAIJ,
1502cda55fadSBarry Smith        0,
1503cda55fadSBarry Smith        0,
1504cda55fadSBarry Smith        0,
1505cda55fadSBarry Smith        0,
15062e8a6d31SBarry Smith        MatDuplicate_MPIAIJ,
1507cda55fadSBarry Smith        0,
1508cda55fadSBarry Smith        0,
1509cda55fadSBarry Smith        0,
1510cda55fadSBarry Smith        0,
1511cda55fadSBarry Smith        0,
1512cda55fadSBarry Smith        MatGetSubMatrices_MPIAIJ,
1513cda55fadSBarry Smith        MatIncreaseOverlap_MPIAIJ,
1514cda55fadSBarry Smith        MatGetValues_MPIAIJ,
1515cb5b572fSBarry Smith        MatCopy_MPIAIJ,
1516052efed2SBarry Smith        MatPrintHelp_MPIAIJ,
1517cda55fadSBarry Smith        MatScale_MPIAIJ,
1518cda55fadSBarry Smith        0,
1519cda55fadSBarry Smith        0,
1520cda55fadSBarry Smith        0,
1521cda55fadSBarry Smith        MatGetBlockSize_MPIAIJ,
1522cda55fadSBarry Smith        0,
1523cda55fadSBarry Smith        0,
1524cda55fadSBarry Smith        0,
1525cda55fadSBarry Smith        0,
1526cda55fadSBarry Smith        MatFDColoringCreate_MPIAIJ,
1527cda55fadSBarry Smith        0,
1528cda55fadSBarry Smith        MatSetUnfactored_MPIAIJ,
1529cda55fadSBarry Smith        0,
1530cda55fadSBarry Smith        0,
1531cda55fadSBarry Smith        MatGetSubMatrix_MPIAIJ,
1532cda55fadSBarry Smith        0,
1533cda55fadSBarry Smith        0,
1534cda55fadSBarry Smith        MatGetMaps_Petsc};
153536ce4990SBarry Smith 
15362e8a6d31SBarry Smith /* ----------------------------------------------------------------------------------------*/
15372e8a6d31SBarry Smith 
1538fb2e594dSBarry Smith EXTERN_C_BEGIN
15392e8a6d31SBarry Smith #undef __FUNC__
15402e8a6d31SBarry Smith #define __FUNC__ "MatStoreValues_MPIAIJ"
15412e8a6d31SBarry Smith int MatStoreValues_MPIAIJ(Mat mat)
15422e8a6d31SBarry Smith {
15432e8a6d31SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data;
15442e8a6d31SBarry Smith   int        ierr;
15452e8a6d31SBarry Smith 
15462e8a6d31SBarry Smith   PetscFunctionBegin;
15472e8a6d31SBarry Smith   ierr = MatStoreValues(aij->A);CHKERRQ(ierr);
15482e8a6d31SBarry Smith   ierr = MatStoreValues(aij->B);CHKERRQ(ierr);
15492e8a6d31SBarry Smith   PetscFunctionReturn(0);
15502e8a6d31SBarry Smith }
1551fb2e594dSBarry Smith EXTERN_C_END
15522e8a6d31SBarry Smith 
1553fb2e594dSBarry Smith EXTERN_C_BEGIN
15542e8a6d31SBarry Smith #undef __FUNC__
15552e8a6d31SBarry Smith #define __FUNC__ "MatRetrieveValues_MPIAIJ"
15562e8a6d31SBarry Smith int MatRetrieveValues_MPIAIJ(Mat mat)
15572e8a6d31SBarry Smith {
15582e8a6d31SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data;
15592e8a6d31SBarry Smith   int        ierr;
15602e8a6d31SBarry Smith 
15612e8a6d31SBarry Smith   PetscFunctionBegin;
15622e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr);
15632e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr);
15642e8a6d31SBarry Smith   PetscFunctionReturn(0);
15652e8a6d31SBarry Smith }
1566fb2e594dSBarry Smith EXTERN_C_END
15678a729477SBarry Smith 
156827508adbSBarry Smith #include "pc.h"
156927508adbSBarry Smith EXTERN_C_BEGIN
15705ef9f2a5SBarry Smith extern int MatGetDiagonalBlock_MPIAIJ(Mat,PetscTruth *,MatReuse,Mat *);
157127508adbSBarry Smith EXTERN_C_END
157227508adbSBarry Smith 
15735615d1e5SSatish Balay #undef __FUNC__
15745615d1e5SSatish Balay #define __FUNC__ "MatCreateMPIAIJ"
15751987afe7SBarry Smith /*@C
1576ff756334SLois Curfman McInnes    MatCreateMPIAIJ - Creates a sparse parallel matrix in AIJ format
15773a511b96SLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
15783a511b96SLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameters
15793a511b96SLois Curfman McInnes    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
15803a511b96SLois Curfman McInnes    performance can be increased by more than a factor of 50.
15818a729477SBarry Smith 
1582db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
1583db81eaa0SLois Curfman McInnes 
15848a729477SBarry Smith    Input Parameters:
1585db81eaa0SLois Curfman McInnes +  comm - MPI communicator
15867d3e4905SLois Curfman McInnes .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
158792e8d321SLois Curfman McInnes            This value should be the same as the local size used in creating the
158892e8d321SLois Curfman McInnes            y vector for the matrix-vector product y = Ax.
15891a3896d6SBarry Smith .  n - This value should be the same as the local size used in creating the
15901a3896d6SBarry Smith        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
15911a3896d6SBarry Smith        calculated if N is given) For square matrices n is almost always m.
159260d380a7SBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
159360d380a7SBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
1594af1d9917SSatish Balay .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
1595af1d9917SSatish Balay            (same value is used for all local rows)
1596af1d9917SSatish Balay .  d_nnz - array containing the number of nonzeros in the various rows of the
1597af1d9917SSatish Balay            DIAGONAL portion of the local submatrix (possibly different for each row)
1598af1d9917SSatish Balay            or PETSC_NULL, if d_nz is used to specify the nonzero structure.
1599af1d9917SSatish Balay            The size of this array is equal to the number of local rows, i.e 'm'.
1600af1d9917SSatish Balay            You must leave room for the diagonal entry even if it is zero.
1601af1d9917SSatish Balay .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
1602af1d9917SSatish Balay            submatrix (same value is used for all local rows).
1603af1d9917SSatish Balay -  o_nnz - array containing the number of nonzeros in the various rows of the
1604af1d9917SSatish Balay            OFF-DIAGONAL portion of the local submatrix (possibly different for
1605af1d9917SSatish Balay            each row) or PETSC_NULL, if o_nz is used to specify the nonzero
1606af1d9917SSatish Balay            structure. The size of this array is equal to the number
1607af1d9917SSatish Balay            of local rows, i.e 'm'.
16088a729477SBarry Smith 
1609ff756334SLois Curfman McInnes    Output Parameter:
161044cd7ae7SLois Curfman McInnes .  A - the matrix
16118a729477SBarry Smith 
1612b259b22eSLois Curfman McInnes    Notes:
1613af1d9917SSatish Balay    m,n,M,N parameters specify the size of the matrix, and its partitioning across
1614af1d9917SSatish Balay    processors, while d_nz,d_nnz,o_nz,o_nnz parameters specify the approximate
1615af1d9917SSatish Balay    storage requirements for this matrix.
1616af1d9917SSatish Balay 
1617af1d9917SSatish Balay    If PETSC_DECIDE or  PETSC_DETERMINE is used for a particular argument on one
1618af1d9917SSatish Balay    processor than it must be used on all processors that share the object for
1619af1d9917SSatish Balay    that argument.
1620be79a94dSBarry Smith 
1621ff756334SLois Curfman McInnes    The AIJ format (also called the Yale sparse matrix format or
1622ff756334SLois Curfman McInnes    compressed row storage), is fully compatible with standard Fortran 77
16230002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
16240002213bSLois Curfman McInnes    either one (as in Fortran) or zero.  See the users manual for details.
1625ff756334SLois Curfman McInnes 
1626ff756334SLois Curfman McInnes    The user MUST specify either the local or global matrix dimensions
1627ff756334SLois Curfman McInnes    (possibly both).
1628ff756334SLois Curfman McInnes 
1629af1d9917SSatish Balay    The parallel matrix is partitioned such that the first m0 rows belong to
1630af1d9917SSatish Balay    process 0, the next m1 rows belong to process 1, the next m2 rows belong
1631af1d9917SSatish Balay    to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.
1632af1d9917SSatish Balay 
1633af1d9917SSatish Balay    The DIAGONAL portion of the local submatrix of a processor can be defined
1634af1d9917SSatish Balay    as the submatrix which is obtained by extraction the part corresponding
1635af1d9917SSatish Balay    to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the
1636af1d9917SSatish Balay    first row that belongs to the processor, and r2 is the last row belonging
1637af1d9917SSatish Balay    to the this processor. This is a square mxm matrix. The remaining portion
1638af1d9917SSatish Balay    of the local submatrix (mxN) constitute the OFF-DIAGONAL portion.
1639af1d9917SSatish Balay 
1640af1d9917SSatish Balay    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
1641af1d9917SSatish Balay 
16425511cfe3SLois Curfman McInnes    By default, this format uses inodes (identical nodes) when possible.
16435511cfe3SLois Curfman McInnes    We search for consecutive rows with the same nonzero structure, thereby
16445511cfe3SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
16455511cfe3SLois Curfman McInnes 
16465511cfe3SLois Curfman McInnes    Options Database Keys:
1647db81eaa0SLois Curfman McInnes +  -mat_aij_no_inode  - Do not use inodes
1648db81eaa0SLois Curfman McInnes .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
1649db81eaa0SLois Curfman McInnes -  -mat_aij_oneindex - Internally use indexing starting at 1
1650db81eaa0SLois Curfman McInnes         rather than 0.  Note that when calling MatSetValues(),
1651db81eaa0SLois Curfman McInnes         the user still MUST index entries starting at 0!
1652494eafd4SBarry Smith .   -mat_mpi - use the parallel matrix data structures even on one processor
1653494eafd4SBarry Smith                (defaults to using SeqBAIJ format on one processor)
1654494eafd4SBarry Smith .   -mat_mpi - use the parallel matrix data structures even on one processor
1655494eafd4SBarry Smith                (defaults to using SeqAIJ format on one processor)
1656494eafd4SBarry Smith 
16575511cfe3SLois Curfman McInnes 
1658af1d9917SSatish Balay    Example usage:
1659e0245417SLois Curfman McInnes 
1660af1d9917SSatish Balay    Consider the following 8x8 matrix with 34 non-zero values, that is
1661af1d9917SSatish Balay    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
1662af1d9917SSatish Balay    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
1663af1d9917SSatish Balay    as follows:
16642191d07cSBarry Smith 
1665db81eaa0SLois Curfman McInnes .vb
1666af1d9917SSatish Balay             1  2  0  |  0  3  0  |  0  4
1667af1d9917SSatish Balay     Proc0   0  5  6  |  7  0  0  |  8  0
1668af1d9917SSatish Balay             9  0 10  | 11  0  0  | 12  0
1669af1d9917SSatish Balay     -------------------------------------
1670af1d9917SSatish Balay            13  0 14  | 15 16 17  |  0  0
1671af1d9917SSatish Balay     Proc1   0 18  0  | 19 20 21  |  0  0
1672af1d9917SSatish Balay             0  0  0  | 22 23  0  | 24  0
1673af1d9917SSatish Balay     -------------------------------------
1674af1d9917SSatish Balay     Proc2  25 26 27  |  0  0 28  | 29  0
1675af1d9917SSatish Balay            30  0  0  | 31 32 33  |  0 34
1676db81eaa0SLois Curfman McInnes .ve
1677b810aeb4SBarry Smith 
1678af1d9917SSatish Balay    This can be represented as a collection of submatrices as:
16795511cfe3SLois Curfman McInnes 
1680af1d9917SSatish Balay .vb
1681af1d9917SSatish Balay       A B C
1682af1d9917SSatish Balay       D E F
1683af1d9917SSatish Balay       G H I
1684af1d9917SSatish Balay .ve
1685af1d9917SSatish Balay 
1686af1d9917SSatish Balay    Where the submatrices A,B,C are owned by proc0, D,E,F are
1687af1d9917SSatish Balay    owned by proc1, G,H,I are owned by proc2.
1688af1d9917SSatish Balay 
1689af1d9917SSatish Balay    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
1690af1d9917SSatish Balay    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
1691af1d9917SSatish Balay    The 'M','N' parameters are 8,8, and have the same values on all procs.
1692af1d9917SSatish Balay 
1693af1d9917SSatish Balay    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
1694af1d9917SSatish Balay    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
1695af1d9917SSatish Balay    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
1696af1d9917SSatish Balay    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
1697af1d9917SSatish Balay    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
1698af1d9917SSatish Balay    matrix, ans [DF] as another SeqAIJ matrix.
1699af1d9917SSatish Balay 
1700af1d9917SSatish Balay    When d_nz, o_nz parameters are specified, d_nz storage elements are
1701af1d9917SSatish Balay    allocated for every row of the local diagonal submatrix, and o_nz
1702af1d9917SSatish Balay    storage locations are allocated for every row of the OFF-DIAGONAL submat.
1703af1d9917SSatish Balay    One way to choose d_nz and o_nz is to use the max nonzerors per local
1704af1d9917SSatish Balay    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
1705af1d9917SSatish Balay    In this case, the values of d_nz,o_nz are:
1706af1d9917SSatish Balay .vb
1707af1d9917SSatish Balay      proc0 : dnz = 2, o_nz = 2
1708af1d9917SSatish Balay      proc1 : dnz = 3, o_nz = 2
1709af1d9917SSatish Balay      proc2 : dnz = 1, o_nz = 4
1710af1d9917SSatish Balay .ve
1711af1d9917SSatish Balay    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
1712af1d9917SSatish Balay    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
1713af1d9917SSatish Balay    for proc3. i.e we are using 12+15+10=37 storage locations to store
1714af1d9917SSatish Balay    34 values.
1715af1d9917SSatish Balay 
1716af1d9917SSatish Balay    When d_nnz, o_nnz parameters are specified, the storage is specified
1717af1d9917SSatish Balay    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
1718af1d9917SSatish Balay    In the above case the values for d_nnz,o_nnz are:
1719af1d9917SSatish Balay .vb
1720af1d9917SSatish Balay      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
1721af1d9917SSatish Balay      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
1722af1d9917SSatish Balay      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
1723af1d9917SSatish Balay .ve
1724af1d9917SSatish Balay    Here the space allocated is sum of all the above values i.e 34, and
1725af1d9917SSatish Balay    hence pre-allocation is perfect.
17263a511b96SLois Curfman McInnes 
1727027ccd11SLois Curfman McInnes    Level: intermediate
1728027ccd11SLois Curfman McInnes 
1729dbd7a890SLois Curfman McInnes .keywords: matrix, aij, compressed row, sparse, parallel
1730ff756334SLois Curfman McInnes 
1731fafbff53SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues()
17328a729477SBarry Smith @*/
1733e1311b90SBarry 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)
17348a729477SBarry Smith {
173544cd7ae7SLois Curfman McInnes   Mat          B;
173644cd7ae7SLois Curfman McInnes   Mat_MPIAIJ   *b;
1737eac7125bSBarry Smith   int          ierr, i,size,flag1 = 0, flag2 = 0;
1738416022c9SBarry Smith 
17393a40ed3dSBarry Smith   PetscFunctionBegin;
17403914022bSBarry Smith   MPI_Comm_size(comm,&size);
17417be0e774SBarry Smith   ierr = OptionsHasName(PETSC_NULL,"-mat_mpiaij",&flag1); CHKERRQ(ierr);
17427be0e774SBarry Smith   ierr = OptionsHasName(PETSC_NULL,"-mat_mpi",&flag2); CHKERRQ(ierr);
17437be0e774SBarry Smith   if (!flag1 && !flag2 && size == 1) {
17443914022bSBarry Smith     if (M == PETSC_DECIDE) M = m;
17453914022bSBarry Smith     if (N == PETSC_DECIDE) N = n;
17463914022bSBarry Smith     ierr = MatCreateSeqAIJ(comm,M,N,d_nz,d_nnz,A); CHKERRQ(ierr);
17473a40ed3dSBarry Smith     PetscFunctionReturn(0);
17483914022bSBarry Smith   }
17493914022bSBarry Smith 
175044cd7ae7SLois Curfman McInnes   *A = 0;
17513f1db9ecSBarry Smith   PetscHeaderCreate(B,_p_Mat,struct _MatOps,MAT_COOKIE,MATMPIAIJ,"Mat",comm,MatDestroy,MatView);
175244cd7ae7SLois Curfman McInnes   PLogObjectCreate(B);
175344cd7ae7SLois Curfman McInnes   B->data            = (void *) (b = PetscNew(Mat_MPIAIJ)); CHKPTRQ(b);
175444cd7ae7SLois Curfman McInnes   PetscMemzero(b,sizeof(Mat_MPIAIJ));
1755cda55fadSBarry Smith   PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));
1756e1311b90SBarry Smith   B->ops->destroy    = MatDestroy_MPIAIJ;
1757e1311b90SBarry Smith   B->ops->view       = MatView_MPIAIJ;
175844cd7ae7SLois Curfman McInnes   B->factor          = 0;
175944cd7ae7SLois Curfman McInnes   B->assembled       = PETSC_FALSE;
176090f02eecSBarry Smith   B->mapping         = 0;
1761d6dfbf8fSBarry Smith 
176247794344SBarry Smith   B->insertmode      = NOT_SET_VALUES;
17639eb4d147SSatish Balay   b->size            = size;
176444cd7ae7SLois Curfman McInnes   MPI_Comm_rank(comm,&b->rank);
17651eb62cbbSBarry Smith 
17663a40ed3dSBarry Smith   if (m == PETSC_DECIDE && (d_nnz != PETSC_NULL || o_nnz != PETSC_NULL)) {
1767a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"Cannot have PETSC_DECIDE rows but set d_nnz or o_nnz");
17683a40ed3dSBarry Smith   }
17691987afe7SBarry Smith 
1770eac7125bSBarry Smith   ierr = PetscSplitOwnership(comm,&m,&M);CHKERRQ(ierr);
1771eac7125bSBarry Smith   ierr = PetscSplitOwnership(comm,&n,&N);CHKERRQ(ierr);
177244cd7ae7SLois Curfman McInnes   b->m = m; B->m = m;
177344cd7ae7SLois Curfman McInnes   b->n = n; B->n = n;
177444cd7ae7SLois Curfman McInnes   b->N = N; B->N = N;
177544cd7ae7SLois Curfman McInnes   b->M = M; B->M = M;
17761eb62cbbSBarry Smith 
1777c7fcc2eaSBarry Smith   /* the information in the maps duplicates the information computed below, eventually
1778c7fcc2eaSBarry Smith      we should remove the duplicate information that is not contained in the maps */
1779253a16ddSBarry Smith   ierr = MapCreateMPI(comm,m,M,&B->rmap);CHKERRQ(ierr);
1780253a16ddSBarry Smith   ierr = MapCreateMPI(comm,n,N,&B->cmap);CHKERRQ(ierr);
1781c7fcc2eaSBarry Smith 
17821eb62cbbSBarry Smith   /* build local table of row and column ownerships */
178344cd7ae7SLois Curfman McInnes   b->rowners = (int *) PetscMalloc(2*(b->size+2)*sizeof(int)); CHKPTRQ(b->rowners);
1784f09e8eb9SSatish Balay   PLogObjectMemory(B,2*(b->size+2)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_MPIAIJ));
1785603f58a4SSatish Balay   b->cowners = b->rowners + b->size + 2;
1786ca161407SBarry Smith   ierr = MPI_Allgather(&m,1,MPI_INT,b->rowners+1,1,MPI_INT,comm);CHKERRQ(ierr);
178744cd7ae7SLois Curfman McInnes   b->rowners[0] = 0;
178844cd7ae7SLois Curfman McInnes   for ( i=2; i<=b->size; i++ ) {
178944cd7ae7SLois Curfman McInnes     b->rowners[i] += b->rowners[i-1];
17908a729477SBarry Smith   }
179144cd7ae7SLois Curfman McInnes   b->rstart = b->rowners[b->rank];
179244cd7ae7SLois Curfman McInnes   b->rend   = b->rowners[b->rank+1];
1793ca161407SBarry Smith   ierr = MPI_Allgather(&n,1,MPI_INT,b->cowners+1,1,MPI_INT,comm);CHKERRQ(ierr);
179444cd7ae7SLois Curfman McInnes   b->cowners[0] = 0;
179544cd7ae7SLois Curfman McInnes   for ( i=2; i<=b->size; i++ ) {
179644cd7ae7SLois Curfman McInnes     b->cowners[i] += b->cowners[i-1];
17978a729477SBarry Smith   }
179844cd7ae7SLois Curfman McInnes   b->cstart = b->cowners[b->rank];
179944cd7ae7SLois Curfman McInnes   b->cend   = b->cowners[b->rank+1];
18008a729477SBarry Smith 
18015ace5be8SLois Curfman McInnes   if (d_nz == PETSC_DEFAULT) d_nz = 5;
1802029af93fSBarry Smith   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,m,n,d_nz,d_nnz,&b->A); CHKERRQ(ierr);
180344cd7ae7SLois Curfman McInnes   PLogObjectParent(B,b->A);
18047b8455f0SLois Curfman McInnes   if (o_nz == PETSC_DEFAULT) o_nz = 0;
1805029af93fSBarry Smith   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,m,N,o_nz,o_nnz,&b->B); CHKERRQ(ierr);
180644cd7ae7SLois Curfman McInnes   PLogObjectParent(B,b->B);
18078a729477SBarry Smith 
18081eb62cbbSBarry Smith   /* build cache for off array entries formed */
1809*a2d1c673SSatish Balay   ierr = StashCreate_Private(comm,1,1,&b->stash); CHKERRQ(ierr);
181090f02eecSBarry Smith   b->donotstash  = 0;
181144cd7ae7SLois Curfman McInnes   b->colmap      = 0;
181244cd7ae7SLois Curfman McInnes   b->garray      = 0;
181344cd7ae7SLois Curfman McInnes   b->roworiented = 1;
18148a729477SBarry Smith 
18151eb62cbbSBarry Smith   /* stuff used for matrix vector multiply */
181644cd7ae7SLois Curfman McInnes   b->lvec      = 0;
181744cd7ae7SLois Curfman McInnes   b->Mvctx     = 0;
18188a729477SBarry Smith 
18197a0afa10SBarry Smith   /* stuff for MatGetRow() */
182044cd7ae7SLois Curfman McInnes   b->rowindices   = 0;
182144cd7ae7SLois Curfman McInnes   b->rowvalues    = 0;
182244cd7ae7SLois Curfman McInnes   b->getrowactive = PETSC_FALSE;
18237a0afa10SBarry Smith 
18242e8a6d31SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",
18252e8a6d31SBarry Smith                                      "MatStoreValues_MPIAIJ",
18262e8a6d31SBarry Smith                                      (void*)MatStoreValues_MPIAIJ);CHKERRQ(ierr);
18272e8a6d31SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",
18282e8a6d31SBarry Smith                                      "MatRetrieveValues_MPIAIJ",
18292e8a6d31SBarry Smith                                      (void*)MatRetrieveValues_MPIAIJ);CHKERRQ(ierr);
18305ef9f2a5SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetDiagonalBlock_C",
18315ef9f2a5SBarry Smith                                      "MatGetDiagonalBlock_MPIAIJ",
18325ef9f2a5SBarry Smith                                      (void*)MatGetDiagonalBlock_MPIAIJ);CHKERRQ(ierr);
183344cd7ae7SLois Curfman McInnes   *A = B;
18343a40ed3dSBarry Smith   PetscFunctionReturn(0);
1835d6dfbf8fSBarry Smith }
1836c74985f6SBarry Smith 
18375615d1e5SSatish Balay #undef __FUNC__
18382e8a6d31SBarry Smith #define __FUNC__ "MatDuplicate_MPIAIJ"
18392e8a6d31SBarry Smith int MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
1840d6dfbf8fSBarry Smith {
1841d6dfbf8fSBarry Smith   Mat        mat;
1842416022c9SBarry Smith   Mat_MPIAIJ *a,*oldmat = (Mat_MPIAIJ *) matin->data;
1843a1b97e82SLois Curfman McInnes   int        ierr, len=0, flg;
1844d6dfbf8fSBarry Smith 
18453a40ed3dSBarry Smith   PetscFunctionBegin;
1846416022c9SBarry Smith   *newmat       = 0;
18473f1db9ecSBarry Smith   PetscHeaderCreate(mat,_p_Mat,struct _MatOps,MAT_COOKIE,MATMPIAIJ,"Mat",matin->comm,MatDestroy,MatView);
1848a5a9c739SBarry Smith   PLogObjectCreate(mat);
18490452661fSBarry Smith   mat->data       = (void *) (a = PetscNew(Mat_MPIAIJ)); CHKPTRQ(a);
1850cda55fadSBarry Smith   PetscMemcpy(mat->ops,&MatOps_Values,sizeof(struct _MatOps));
1851e1311b90SBarry Smith   mat->ops->destroy = MatDestroy_MPIAIJ;
1852e1311b90SBarry Smith   mat->ops->view    = MatView_MPIAIJ;
1853d6dfbf8fSBarry Smith   mat->factor       = matin->factor;
1854c456f294SBarry Smith   mat->assembled    = PETSC_TRUE;
1855e7641de0SSatish Balay   mat->insertmode   = NOT_SET_VALUES;
1856d6dfbf8fSBarry Smith 
185744cd7ae7SLois Curfman McInnes   a->m = mat->m   = oldmat->m;
185844cd7ae7SLois Curfman McInnes   a->n = mat->n   = oldmat->n;
185944cd7ae7SLois Curfman McInnes   a->M = mat->M   = oldmat->M;
186044cd7ae7SLois Curfman McInnes   a->N = mat->N   = oldmat->N;
1861d6dfbf8fSBarry Smith 
1862416022c9SBarry Smith   a->rstart       = oldmat->rstart;
1863416022c9SBarry Smith   a->rend         = oldmat->rend;
1864416022c9SBarry Smith   a->cstart       = oldmat->cstart;
1865416022c9SBarry Smith   a->cend         = oldmat->cend;
186617699dbbSLois Curfman McInnes   a->size         = oldmat->size;
186717699dbbSLois Curfman McInnes   a->rank         = oldmat->rank;
1868e7641de0SSatish Balay   a->donotstash   = oldmat->donotstash;
1869e7641de0SSatish Balay   a->roworiented  = oldmat->roworiented;
1870e7641de0SSatish Balay   a->rowindices   = 0;
1871bcd2baecSBarry Smith   a->rowvalues    = 0;
1872bcd2baecSBarry Smith   a->getrowactive = PETSC_FALSE;
1873d6dfbf8fSBarry Smith 
1874603f58a4SSatish Balay   a->rowners = (int *) PetscMalloc(2*(a->size+2)*sizeof(int)); CHKPTRQ(a->rowners);
1875f09e8eb9SSatish Balay   PLogObjectMemory(mat,2*(a->size+2)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_MPIAIJ));
1876603f58a4SSatish Balay   a->cowners = a->rowners + a->size + 2;
1877603f58a4SSatish Balay   PetscMemcpy(a->rowners,oldmat->rowners,2*(a->size+2)*sizeof(int));
1878*a2d1c673SSatish Balay   ierr = StashCreate_Private(matin->comm,1,1,&a->stash); CHKERRQ(ierr);
18792ee70a88SLois Curfman McInnes   if (oldmat->colmap) {
1880b1fc9764SSatish Balay #if defined (USE_CTABLE)
1881fa46199cSSatish Balay     ierr = TableCreateCopy(oldmat->colmap,&a->colmap); CHKERRQ(ierr);
1882b1fc9764SSatish Balay #else
18830452661fSBarry Smith     a->colmap = (int *) PetscMalloc((a->N)*sizeof(int));CHKPTRQ(a->colmap);
1884416022c9SBarry Smith     PLogObjectMemory(mat,(a->N)*sizeof(int));
1885416022c9SBarry Smith     PetscMemcpy(a->colmap,oldmat->colmap,(a->N)*sizeof(int));
1886b1fc9764SSatish Balay #endif
1887416022c9SBarry Smith   } else a->colmap = 0;
18883f41c07dSBarry Smith   if (oldmat->garray) {
18893f41c07dSBarry Smith     len = ((Mat_SeqAIJ *) (oldmat->B->data))->n;
18903f41c07dSBarry Smith     a->garray = (int *) PetscMalloc((len+1)*sizeof(int)); CHKPTRQ(a->garray);
1891464493b3SBarry Smith     PLogObjectMemory(mat,len*sizeof(int));
18923f41c07dSBarry Smith     if (len) PetscMemcpy(a->garray,oldmat->garray,len*sizeof(int));
1893416022c9SBarry Smith   } else a->garray = 0;
1894d6dfbf8fSBarry Smith 
1895416022c9SBarry Smith   ierr =  VecDuplicate(oldmat->lvec,&a->lvec); CHKERRQ(ierr);
1896416022c9SBarry Smith   PLogObjectParent(mat,a->lvec);
1897a56f8943SBarry Smith   ierr =  VecScatterCopy(oldmat->Mvctx,&a->Mvctx); CHKERRQ(ierr);
1898416022c9SBarry Smith   PLogObjectParent(mat,a->Mvctx);
18992e8a6d31SBarry Smith   ierr =  MatDuplicate(oldmat->A,cpvalues,&a->A); CHKERRQ(ierr);
1900416022c9SBarry Smith   PLogObjectParent(mat,a->A);
19012e8a6d31SBarry Smith   ierr =  MatDuplicate(oldmat->B,cpvalues,&a->B); CHKERRQ(ierr);
1902416022c9SBarry Smith   PLogObjectParent(mat,a->B);
19035dd7a6c7SBarry Smith   ierr = OptionsHasName(PETSC_NULL,"-help",&flg); CHKERRQ(ierr);
190425cdf11fSBarry Smith   if (flg) {
1905682d7d0cSBarry Smith     ierr = MatPrintHelp(mat); CHKERRQ(ierr);
1906682d7d0cSBarry Smith   }
190727508adbSBarry Smith   ierr = FListDuplicate(matin->qlist,&mat->qlist);CHKERRQ(ierr);
19088a729477SBarry Smith   *newmat = mat;
19093a40ed3dSBarry Smith   PetscFunctionReturn(0);
19108a729477SBarry Smith }
1911416022c9SBarry Smith 
191277c4ece6SBarry Smith #include "sys.h"
1913416022c9SBarry Smith 
19145615d1e5SSatish Balay #undef __FUNC__
19155615d1e5SSatish Balay #define __FUNC__ "MatLoad_MPIAIJ"
191619bcc07fSBarry Smith int MatLoad_MPIAIJ(Viewer viewer,MatType type,Mat *newmat)
1917416022c9SBarry Smith {
1918d65a2f8fSBarry Smith   Mat          A;
1919d65a2f8fSBarry Smith   Scalar       *vals,*svals;
192019bcc07fSBarry Smith   MPI_Comm     comm = ((PetscObject)viewer)->comm;
1921416022c9SBarry Smith   MPI_Status   status;
19223a40ed3dSBarry Smith   int          i, nz, ierr, j,rstart, rend, fd;
192317699dbbSLois Curfman McInnes   int          header[4],rank,size,*rowlengths = 0,M,N,m,*rowners,maxnz,*cols;
1924d65a2f8fSBarry Smith   int          *ourlens,*sndcounts = 0,*procsnz = 0, *offlens,jj,*mycols,*smycols;
1925b362ba68SBarry Smith   int          tag = ((PetscObject)viewer)->tag,cend,cstart,n;
1926416022c9SBarry Smith 
19273a40ed3dSBarry Smith   PetscFunctionBegin;
192817699dbbSLois Curfman McInnes   MPI_Comm_size(comm,&size); MPI_Comm_rank(comm,&rank);
192917699dbbSLois Curfman McInnes   if (!rank) {
193090ace30eSBarry Smith     ierr = ViewerBinaryGetDescriptor(viewer,&fd); CHKERRQ(ierr);
19310752156aSBarry Smith     ierr = PetscBinaryRead(fd,(char *)header,4,PETSC_INT); CHKERRQ(ierr);
1932a8c6a408SBarry Smith     if (header[0] != MAT_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,0,"not matrix object");
1933d64ed03dSBarry Smith     if (header[3] < 0) {
1934a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_FILE_UNEXPECTED,1,"Matrix in special format on disk, cannot load as MPIAIJ");
1935d64ed03dSBarry Smith     }
19366c5fab8fSBarry Smith   }
19376c5fab8fSBarry Smith 
1938ca161407SBarry Smith   ierr = MPI_Bcast(header+1,3,MPI_INT,0,comm);CHKERRQ(ierr);
1939416022c9SBarry Smith   M = header[1]; N = header[2];
1940416022c9SBarry Smith   /* determine ownership of all rows */
194117699dbbSLois Curfman McInnes   m = M/size + ((M % size) > rank);
19420452661fSBarry Smith   rowners = (int *) PetscMalloc((size+2)*sizeof(int)); CHKPTRQ(rowners);
1943ca161407SBarry Smith   ierr = MPI_Allgather(&m,1,MPI_INT,rowners+1,1,MPI_INT,comm);CHKERRQ(ierr);
1944416022c9SBarry Smith   rowners[0] = 0;
194517699dbbSLois Curfman McInnes   for ( i=2; i<=size; i++ ) {
1946416022c9SBarry Smith     rowners[i] += rowners[i-1];
1947416022c9SBarry Smith   }
194817699dbbSLois Curfman McInnes   rstart = rowners[rank];
194917699dbbSLois Curfman McInnes   rend   = rowners[rank+1];
1950416022c9SBarry Smith 
1951416022c9SBarry Smith   /* distribute row lengths to all processors */
19520452661fSBarry Smith   ourlens = (int*) PetscMalloc( 2*(rend-rstart)*sizeof(int) ); CHKPTRQ(ourlens);
1953416022c9SBarry Smith   offlens = ourlens + (rend-rstart);
195417699dbbSLois Curfman McInnes   if (!rank) {
19550452661fSBarry Smith     rowlengths = (int*) PetscMalloc( M*sizeof(int) ); CHKPTRQ(rowlengths);
19560752156aSBarry Smith     ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT); CHKERRQ(ierr);
19570452661fSBarry Smith     sndcounts = (int*) PetscMalloc( size*sizeof(int) ); CHKPTRQ(sndcounts);
195817699dbbSLois Curfman McInnes     for ( i=0; i<size; i++ ) sndcounts[i] = rowners[i+1] - rowners[i];
1959ca161407SBarry Smith     ierr = MPI_Scatterv(rowlengths,sndcounts,rowners,MPI_INT,ourlens,rend-rstart,MPI_INT,0,comm);CHKERRQ(ierr);
19600452661fSBarry Smith     PetscFree(sndcounts);
19613a40ed3dSBarry Smith   } else {
1962ca161407SBarry Smith     ierr = MPI_Scatterv(0,0,0,MPI_INT,ourlens,rend-rstart,MPI_INT, 0,comm);CHKERRQ(ierr);
1963416022c9SBarry Smith   }
1964416022c9SBarry Smith 
196517699dbbSLois Curfman McInnes   if (!rank) {
1966416022c9SBarry Smith     /* calculate the number of nonzeros on each processor */
19670452661fSBarry Smith     procsnz = (int*) PetscMalloc( size*sizeof(int) ); CHKPTRQ(procsnz);
1968cddf8d76SBarry Smith     PetscMemzero(procsnz,size*sizeof(int));
196917699dbbSLois Curfman McInnes     for ( i=0; i<size; i++ ) {
1970416022c9SBarry Smith       for ( j=rowners[i]; j< rowners[i+1]; j++ ) {
1971416022c9SBarry Smith         procsnz[i] += rowlengths[j];
1972416022c9SBarry Smith       }
1973416022c9SBarry Smith     }
19740452661fSBarry Smith     PetscFree(rowlengths);
1975416022c9SBarry Smith 
1976416022c9SBarry Smith     /* determine max buffer needed and allocate it */
1977416022c9SBarry Smith     maxnz = 0;
197817699dbbSLois Curfman McInnes     for ( i=0; i<size; i++ ) {
19790452661fSBarry Smith       maxnz = PetscMax(maxnz,procsnz[i]);
1980416022c9SBarry Smith     }
19810452661fSBarry Smith     cols = (int *) PetscMalloc( maxnz*sizeof(int) ); CHKPTRQ(cols);
1982416022c9SBarry Smith 
1983416022c9SBarry Smith     /* read in my part of the matrix column indices  */
1984416022c9SBarry Smith     nz     = procsnz[0];
19850452661fSBarry Smith     mycols = (int *) PetscMalloc( nz*sizeof(int) ); CHKPTRQ(mycols);
19860752156aSBarry Smith     ierr   = PetscBinaryRead(fd,mycols,nz,PETSC_INT); CHKERRQ(ierr);
1987d65a2f8fSBarry Smith 
1988d65a2f8fSBarry Smith     /* read in every one elses and ship off */
198917699dbbSLois Curfman McInnes     for ( i=1; i<size; i++ ) {
1990d65a2f8fSBarry Smith       nz   = procsnz[i];
19910752156aSBarry Smith       ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT); CHKERRQ(ierr);
1992ca161407SBarry Smith       ierr = MPI_Send(cols,nz,MPI_INT,i,tag,comm);CHKERRQ(ierr);
1993d65a2f8fSBarry Smith     }
19940452661fSBarry Smith     PetscFree(cols);
19953a40ed3dSBarry Smith   } else {
1996416022c9SBarry Smith     /* determine buffer space needed for message */
1997416022c9SBarry Smith     nz = 0;
1998416022c9SBarry Smith     for ( i=0; i<m; i++ ) {
1999416022c9SBarry Smith       nz += ourlens[i];
2000416022c9SBarry Smith     }
20010452661fSBarry Smith     mycols = (int*) PetscMalloc( nz*sizeof(int) ); CHKPTRQ(mycols);
2002416022c9SBarry Smith 
2003416022c9SBarry Smith     /* receive message of column indices*/
2004ca161407SBarry Smith     ierr = MPI_Recv(mycols,nz,MPI_INT,0,tag,comm,&status);CHKERRQ(ierr);
2005ca161407SBarry Smith     ierr = MPI_Get_count(&status,MPI_INT,&maxnz);CHKERRQ(ierr);
2006a8c6a408SBarry Smith     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,0,"something is wrong with file");
2007416022c9SBarry Smith   }
2008416022c9SBarry Smith 
2009b362ba68SBarry Smith   /* determine column ownership if matrix is not square */
2010b362ba68SBarry Smith   if (N != M) {
2011b362ba68SBarry Smith     n      = N/size + ((N % size) > rank);
2012b362ba68SBarry Smith     ierr   = MPI_Scan(&n,&cend,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr);
2013b362ba68SBarry Smith     cstart = cend - n;
2014b362ba68SBarry Smith   } else {
2015b362ba68SBarry Smith     cstart = rstart;
2016b362ba68SBarry Smith     cend   = rend;
2017fb2e594dSBarry Smith     n      = cend - cstart;
2018b362ba68SBarry Smith   }
2019b362ba68SBarry Smith 
2020416022c9SBarry Smith   /* loop over local rows, determining number of off diagonal entries */
2021cddf8d76SBarry Smith   PetscMemzero(offlens,m*sizeof(int));
2022416022c9SBarry Smith   jj = 0;
2023416022c9SBarry Smith   for ( i=0; i<m; i++ ) {
2024416022c9SBarry Smith     for ( j=0; j<ourlens[i]; j++ ) {
2025b362ba68SBarry Smith       if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++;
2026416022c9SBarry Smith       jj++;
2027416022c9SBarry Smith     }
2028416022c9SBarry Smith   }
2029d65a2f8fSBarry Smith 
2030d65a2f8fSBarry Smith   /* create our matrix */
2031416022c9SBarry Smith   for ( i=0; i<m; i++ ) {
2032416022c9SBarry Smith     ourlens[i] -= offlens[i];
2033416022c9SBarry Smith   }
2034b362ba68SBarry Smith   ierr = MatCreateMPIAIJ(comm,m,n,M,N,0,ourlens,0,offlens,newmat);CHKERRQ(ierr);
2035d65a2f8fSBarry Smith   A = *newmat;
2036fb2e594dSBarry Smith   ierr = MatSetOption(A,MAT_COLUMNS_SORTED); CHKERRQ(ierr);
2037d65a2f8fSBarry Smith   for ( i=0; i<m; i++ ) {
2038d65a2f8fSBarry Smith     ourlens[i] += offlens[i];
2039d65a2f8fSBarry Smith   }
2040416022c9SBarry Smith 
204117699dbbSLois Curfman McInnes   if (!rank) {
20420452661fSBarry Smith     vals = (Scalar *) PetscMalloc( maxnz*sizeof(Scalar) ); CHKPTRQ(vals);
2043416022c9SBarry Smith 
2044416022c9SBarry Smith     /* read in my part of the matrix numerical values  */
2045416022c9SBarry Smith     nz   = procsnz[0];
20460752156aSBarry Smith     ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR); CHKERRQ(ierr);
2047d65a2f8fSBarry Smith 
2048d65a2f8fSBarry Smith     /* insert into matrix */
2049d65a2f8fSBarry Smith     jj      = rstart;
2050d65a2f8fSBarry Smith     smycols = mycols;
2051d65a2f8fSBarry Smith     svals   = vals;
2052d65a2f8fSBarry Smith     for ( i=0; i<m; i++ ) {
2053d65a2f8fSBarry Smith       ierr = MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
2054d65a2f8fSBarry Smith       smycols += ourlens[i];
2055d65a2f8fSBarry Smith       svals   += ourlens[i];
2056d65a2f8fSBarry Smith       jj++;
2057416022c9SBarry Smith     }
2058416022c9SBarry Smith 
2059d65a2f8fSBarry Smith     /* read in other processors and ship out */
206017699dbbSLois Curfman McInnes     for ( i=1; i<size; i++ ) {
2061416022c9SBarry Smith       nz   = procsnz[i];
20620752156aSBarry Smith       ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR); CHKERRQ(ierr);
2063ca161407SBarry Smith       ierr = MPI_Send(vals,nz,MPIU_SCALAR,i,A->tag,comm);CHKERRQ(ierr);
2064416022c9SBarry Smith     }
20650452661fSBarry Smith     PetscFree(procsnz);
20663a40ed3dSBarry Smith   } else {
2067d65a2f8fSBarry Smith     /* receive numeric values */
20680452661fSBarry Smith     vals = (Scalar*) PetscMalloc( nz*sizeof(Scalar) ); CHKPTRQ(vals);
2069416022c9SBarry Smith 
2070d65a2f8fSBarry Smith     /* receive message of values*/
2071ca161407SBarry Smith     ierr = MPI_Recv(vals,nz,MPIU_SCALAR,0,A->tag,comm,&status);CHKERRQ(ierr);
2072ca161407SBarry Smith     ierr = MPI_Get_count(&status,MPIU_SCALAR,&maxnz);CHKERRQ(ierr);
2073a8c6a408SBarry Smith     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,0,"something is wrong with file");
2074d65a2f8fSBarry Smith 
2075d65a2f8fSBarry Smith     /* insert into matrix */
2076d65a2f8fSBarry Smith     jj      = rstart;
2077d65a2f8fSBarry Smith     smycols = mycols;
2078d65a2f8fSBarry Smith     svals   = vals;
2079d65a2f8fSBarry Smith     for ( i=0; i<m; i++ ) {
2080d65a2f8fSBarry Smith       ierr     = MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
2081d65a2f8fSBarry Smith       smycols += ourlens[i];
2082d65a2f8fSBarry Smith       svals   += ourlens[i];
2083d65a2f8fSBarry Smith       jj++;
2084d65a2f8fSBarry Smith     }
2085d65a2f8fSBarry Smith   }
20860452661fSBarry Smith   PetscFree(ourlens); PetscFree(vals); PetscFree(mycols); PetscFree(rowners);
2087d65a2f8fSBarry Smith 
20886d4a8577SBarry Smith   ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
20896d4a8577SBarry Smith   ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
20903a40ed3dSBarry Smith   PetscFunctionReturn(0);
2091416022c9SBarry Smith }
2092a0ff6018SBarry Smith 
209329da9460SBarry Smith #undef __FUNC__
209429da9460SBarry Smith #define __FUNC__ "MatGetSubMatrix_MPIAIJ"
2095a0ff6018SBarry Smith /*
209629da9460SBarry Smith     Not great since it makes two copies of the submatrix, first an SeqAIJ
209729da9460SBarry Smith   in local and then by concatenating the local matrices the end result.
209829da9460SBarry Smith   Writing it directly would be much like MatGetSubMatrices_MPIAIJ()
2099a0ff6018SBarry Smith */
21007b2a1423SBarry Smith int MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,int csize,MatReuse call,Mat *newmat)
2101a0ff6018SBarry Smith {
210200e6dbe6SBarry Smith   int        ierr, i, m,n,rstart,row,rend,nz,*cwork,size,rank,j;
2103fee21e36SBarry Smith   Mat        *local,M, Mreuse;
210400e6dbe6SBarry Smith   Scalar     *vwork,*aa;
210500e6dbe6SBarry Smith   MPI_Comm   comm = mat->comm;
210600e6dbe6SBarry Smith   Mat_SeqAIJ *aij;
210700e6dbe6SBarry Smith   int        *ii, *jj,nlocal,*dlens,*olens,dlen,olen,jend;
2108a0ff6018SBarry Smith 
2109a0ff6018SBarry Smith   PetscFunctionBegin;
211000e6dbe6SBarry Smith   MPI_Comm_rank(comm,&rank);
211100e6dbe6SBarry Smith   MPI_Comm_size(comm,&size);
211200e6dbe6SBarry Smith 
2113fee21e36SBarry Smith   if (call ==  MAT_REUSE_MATRIX) {
2114fee21e36SBarry Smith     ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);CHKERRQ(ierr);
2115fee21e36SBarry Smith     if (!Mreuse) SETERRQ(1,1,"Submatrix passed in was not used before, cannot reuse");
2116fee21e36SBarry Smith     local = &Mreuse;
2117fee21e36SBarry Smith     ierr  = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&local);CHKERRQ(ierr);
2118fee21e36SBarry Smith   } else {
2119a0ff6018SBarry Smith     ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr);
2120fee21e36SBarry Smith     Mreuse = *local;
2121fee21e36SBarry Smith     PetscFree(local);
2122fee21e36SBarry Smith   }
2123a0ff6018SBarry Smith 
2124a0ff6018SBarry Smith   /*
2125a0ff6018SBarry Smith       m - number of local rows
2126a0ff6018SBarry Smith       n - number of columns (same on all processors)
2127a0ff6018SBarry Smith       rstart - first row in new global matrix generated
2128a0ff6018SBarry Smith   */
2129fee21e36SBarry Smith   ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr);
2130a0ff6018SBarry Smith   if (call == MAT_INITIAL_MATRIX) {
2131fee21e36SBarry Smith     aij = (Mat_SeqAIJ *) (Mreuse)->data;
2132a8c6a408SBarry Smith     if (aij->indexshift) SETERRQ(PETSC_ERR_SUP,1,"No support for index shifted matrix");
213300e6dbe6SBarry Smith     ii  = aij->i;
213400e6dbe6SBarry Smith     jj  = aij->j;
213500e6dbe6SBarry Smith 
2136a0ff6018SBarry Smith     /*
213700e6dbe6SBarry Smith         Determine the number of non-zeros in the diagonal and off-diagonal
213800e6dbe6SBarry Smith         portions of the matrix in order to do correct preallocation
2139a0ff6018SBarry Smith     */
214000e6dbe6SBarry Smith 
214100e6dbe6SBarry Smith     /* first get start and end of "diagonal" columns */
21426a6a5d1dSBarry Smith     if (csize == PETSC_DECIDE) {
214300e6dbe6SBarry Smith       nlocal = n/size + ((n % size) > rank);
21446a6a5d1dSBarry Smith     } else {
21456a6a5d1dSBarry Smith       nlocal = csize;
21466a6a5d1dSBarry Smith     }
2147ca161407SBarry Smith     ierr   = MPI_Scan(&nlocal,&rend,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr);
214800e6dbe6SBarry Smith     rstart = rend - nlocal;
21496a6a5d1dSBarry Smith     if (rank == size - 1 && rend != n) {
21506a6a5d1dSBarry Smith       SETERRQ(1,1,"Local column sizes do not add up to total number of columns");
21516a6a5d1dSBarry Smith     }
215200e6dbe6SBarry Smith 
215300e6dbe6SBarry Smith     /* next, compute all the lengths */
215400e6dbe6SBarry Smith     dlens = (int *) PetscMalloc( (2*m+1)*sizeof(int) );CHKPTRQ(dlens);
215500e6dbe6SBarry Smith     olens = dlens + m;
215600e6dbe6SBarry Smith     for ( i=0; i<m; i++ ) {
215700e6dbe6SBarry Smith       jend = ii[i+1] - ii[i];
215800e6dbe6SBarry Smith       olen = 0;
215900e6dbe6SBarry Smith       dlen = 0;
216000e6dbe6SBarry Smith       for ( j=0; j<jend; j++ ) {
216100e6dbe6SBarry Smith         if ( *jj < rstart || *jj >= rend) olen++;
216200e6dbe6SBarry Smith         else dlen++;
216300e6dbe6SBarry Smith         jj++;
216400e6dbe6SBarry Smith       }
216500e6dbe6SBarry Smith       olens[i] = olen;
216600e6dbe6SBarry Smith       dlens[i] = dlen;
216700e6dbe6SBarry Smith     }
21682d207970SBarry Smith     ierr = MatCreateMPIAIJ(comm,m,nlocal,PETSC_DECIDE,n,0,dlens,0,olens,&M);CHKERRQ(ierr);
216900e6dbe6SBarry Smith     PetscFree(dlens);
2170a0ff6018SBarry Smith   } else {
2171a0ff6018SBarry Smith     int ml,nl;
2172a0ff6018SBarry Smith 
2173a0ff6018SBarry Smith     M = *newmat;
2174a0ff6018SBarry Smith     ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr);
2175a8c6a408SBarry Smith     if (ml != m) SETERRQ(PETSC_ERR_ARG_SIZ,1,"Previous matrix must be same size/layout as request");
2176a0ff6018SBarry Smith     ierr = MatZeroEntries(M);CHKERRQ(ierr);
2177c48de900SBarry Smith     /*
2178c48de900SBarry Smith          The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
2179c48de900SBarry Smith        rather than the slower MatSetValues().
2180c48de900SBarry Smith     */
2181c48de900SBarry Smith     M->was_assembled = PETSC_TRUE;
2182c48de900SBarry Smith     M->assembled     = PETSC_FALSE;
2183a0ff6018SBarry Smith   }
2184a0ff6018SBarry Smith   ierr = MatGetOwnershipRange(M,&rstart,&rend); CHKERRQ(ierr);
2185fee21e36SBarry Smith   aij = (Mat_SeqAIJ *) (Mreuse)->data;
2186a8c6a408SBarry Smith   if (aij->indexshift) SETERRQ(PETSC_ERR_SUP,1,"No support for index shifted matrix");
218700e6dbe6SBarry Smith   ii  = aij->i;
218800e6dbe6SBarry Smith   jj  = aij->j;
218900e6dbe6SBarry Smith   aa  = aij->a;
2190a0ff6018SBarry Smith   for (i=0; i<m; i++) {
2191a0ff6018SBarry Smith     row   = rstart + i;
219200e6dbe6SBarry Smith     nz    = ii[i+1] - ii[i];
219300e6dbe6SBarry Smith     cwork = jj;     jj += nz;
219400e6dbe6SBarry Smith     vwork = aa;     aa += nz;
21958c638d02SBarry Smith     ierr = MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES); CHKERRQ(ierr);
2196a0ff6018SBarry Smith   }
2197a0ff6018SBarry Smith 
2198a0ff6018SBarry Smith   ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
2199a0ff6018SBarry Smith   ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
2200a0ff6018SBarry Smith   *newmat = M;
2201fee21e36SBarry Smith 
2202fee21e36SBarry Smith   /* save submatrix used in processor for next request */
2203fee21e36SBarry Smith   if (call ==  MAT_INITIAL_MATRIX) {
2204fee21e36SBarry Smith     ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr);
2205fee21e36SBarry Smith     ierr = PetscObjectDereference((PetscObject)Mreuse);CHKERRQ(ierr);
2206fee21e36SBarry Smith   }
2207fee21e36SBarry Smith 
2208a0ff6018SBarry Smith   PetscFunctionReturn(0);
2209a0ff6018SBarry Smith }
2210