xref: /petsc/src/mat/impls/aij/mpi/mpiaij.c (revision 6849ba73f22fecb8f92ef896a42e4e8bd4cd6965)
18a729477SBarry Smith 
270f55243SBarry Smith #include "src/mat/impls/aij/mpi/mpiaij.h"
3d9942c19SSatish Balay #include "src/inline/spops.h"
48a729477SBarry Smith 
50f5bd95cSBarry Smith /*
60f5bd95cSBarry Smith   Local utility routine that creates a mapping from the global column
79e25ed09SBarry Smith number to the local number in the off-diagonal part of the local
80f5bd95cSBarry Smith storage of the matrix.  When PETSC_USE_CTABLE is used this is scalable at
90f5bd95cSBarry Smith a slightly higher hash table cost; without it it is not scalable (each processor
100f5bd95cSBarry Smith has an order N integer array but is fast to acess.
119e25ed09SBarry Smith */
124a2ae208SSatish Balay #undef __FUNCT__
134a2ae208SSatish Balay #define __FUNCT__ "CreateColmap_MPIAIJ_Private"
14dfbe8321SBarry Smith PetscErrorCode CreateColmap_MPIAIJ_Private(Mat mat)
159e25ed09SBarry Smith {
1644a69424SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
17*6849ba73SBarry Smith   PetscErrorCode ierr;
18*6849ba73SBarry Smith   int        n = aij->B->n,i;
19dbb450caSBarry Smith 
203a40ed3dSBarry Smith   PetscFunctionBegin;
21aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
22273d9f13SBarry Smith   ierr = PetscTableCreate(n,&aij->colmap);CHKERRQ(ierr);
23b1fc9764SSatish Balay   for (i=0; i<n; i++){
240f5bd95cSBarry Smith     ierr = PetscTableAdd(aij->colmap,aij->garray[i]+1,i+1);CHKERRQ(ierr);
25b1fc9764SSatish Balay   }
26b1fc9764SSatish Balay #else
27b0a32e0cSBarry Smith   ierr = PetscMalloc((mat->N+1)*sizeof(int),&aij->colmap);CHKERRQ(ierr);
28b0a32e0cSBarry Smith   PetscLogObjectMemory(mat,mat->N*sizeof(int));
29273d9f13SBarry Smith   ierr = PetscMemzero(aij->colmap,mat->N*sizeof(int));CHKERRQ(ierr);
30905e6a2fSBarry Smith   for (i=0; i<n; i++) aij->colmap[aij->garray[i]] = i+1;
31b1fc9764SSatish Balay #endif
323a40ed3dSBarry Smith   PetscFunctionReturn(0);
339e25ed09SBarry Smith }
349e25ed09SBarry Smith 
350520107fSSatish Balay #define CHUNKSIZE   15
3630770e4dSSatish Balay #define MatSetValues_SeqAIJ_A_Private(row,col,value,addv) \
370520107fSSatish Balay { \
380520107fSSatish Balay  \
390520107fSSatish Balay     rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift; \
4030770e4dSSatish Balay     rmax = aimax[row]; nrow = ailen[row];  \
41f5e9677aSSatish Balay     col1 = col - shift; \
42f5e9677aSSatish Balay      \
43ba4e3ef2SSatish Balay     low = 0; high = nrow; \
44ba4e3ef2SSatish Balay     while (high-low > 5) { \
45ba4e3ef2SSatish Balay       t = (low+high)/2; \
46ba4e3ef2SSatish Balay       if (rp[t] > col) high = t; \
47ba4e3ef2SSatish Balay       else             low  = t; \
48ba4e3ef2SSatish Balay     } \
492335bdd2SSatish Balay       for (_i=low; _i<high; _i++) { \
50f5e9677aSSatish Balay         if (rp[_i] > col1) break; \
51f5e9677aSSatish Balay         if (rp[_i] == col1) { \
520520107fSSatish Balay           if (addv == ADD_VALUES) ap[_i] += value;   \
530520107fSSatish Balay           else                  ap[_i] = value; \
5430770e4dSSatish Balay           goto a_noinsert; \
550520107fSSatish Balay         } \
560520107fSSatish Balay       }  \
5789280ab3SLois Curfman McInnes       if (nonew == 1) goto a_noinsert; \
58a45adfd6SMatthew Knepley       else if (nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%d, %d) into matrix", row, col); \
590520107fSSatish Balay       if (nrow >= rmax) { \
600520107fSSatish Balay         /* there is no extra room in row, therefore enlarge */ \
61273d9f13SBarry Smith         int    new_nz = ai[am] + CHUNKSIZE,len,*new_i,*new_j; \
6287828ca2SBarry Smith         PetscScalar *new_a; \
630520107fSSatish Balay  \
64a45adfd6SMatthew Knepley         if (nonew == -2) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%d, %d) in the matrix", row, col); \
6589280ab3SLois Curfman McInnes  \
660520107fSSatish Balay         /* malloc new storage space */ \
6787828ca2SBarry Smith         len     = new_nz*(sizeof(int)+sizeof(PetscScalar))+(am+1)*sizeof(int); \
68b0a32e0cSBarry Smith         ierr    = PetscMalloc(len,&new_a);CHKERRQ(ierr); \
690520107fSSatish Balay         new_j   = (int*)(new_a + new_nz); \
700520107fSSatish Balay         new_i   = new_j + new_nz; \
710520107fSSatish Balay  \
720520107fSSatish Balay         /* copy over old data into new slots */ \
730520107fSSatish Balay         for (ii=0; ii<row+1; ii++) {new_i[ii] = ai[ii];} \
74273d9f13SBarry Smith         for (ii=row+1; ii<am+1; ii++) {new_i[ii] = ai[ii]+CHUNKSIZE;} \
75549d3d68SSatish Balay         ierr = PetscMemcpy(new_j,aj,(ai[row]+nrow+shift)*sizeof(int));CHKERRQ(ierr); \
760520107fSSatish Balay         len = (new_nz - CHUNKSIZE - ai[row] - nrow - shift); \
77549d3d68SSatish Balay         ierr = PetscMemcpy(new_j+ai[row]+shift+nrow+CHUNKSIZE,aj+ai[row]+shift+nrow, \
78549d3d68SSatish Balay                                                            len*sizeof(int));CHKERRQ(ierr); \
7987828ca2SBarry Smith         ierr = PetscMemcpy(new_a,aa,(ai[row]+nrow+shift)*sizeof(PetscScalar));CHKERRQ(ierr); \
80549d3d68SSatish Balay         ierr = PetscMemcpy(new_a+ai[row]+shift+nrow+CHUNKSIZE,aa+ai[row]+shift+nrow, \
8187828ca2SBarry Smith                                                            len*sizeof(PetscScalar));CHKERRQ(ierr);  \
820520107fSSatish Balay         /* free up old matrix storage */ \
83f5e9677aSSatish Balay  \
84606d414cSSatish Balay         ierr = PetscFree(a->a);CHKERRQ(ierr);  \
85606d414cSSatish Balay         if (!a->singlemalloc) { \
86606d414cSSatish Balay            ierr = PetscFree(a->i);CHKERRQ(ierr); \
87606d414cSSatish Balay            ierr = PetscFree(a->j);CHKERRQ(ierr); \
88606d414cSSatish Balay         } \
890520107fSSatish Balay         aa = a->a = new_a; ai = a->i = new_i; aj = a->j = new_j;  \
907c922b88SBarry Smith         a->singlemalloc = PETSC_TRUE; \
910520107fSSatish Balay  \
920520107fSSatish Balay         rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift; \
9330770e4dSSatish Balay         rmax = aimax[row] = aimax[row] + CHUNKSIZE; \
9487828ca2SBarry Smith         PetscLogObjectMemory(A,CHUNKSIZE*(sizeof(int) + sizeof(PetscScalar))); \
950520107fSSatish Balay         a->maxnz += CHUNKSIZE; \
960520107fSSatish Balay         a->reallocs++; \
970520107fSSatish Balay       } \
980520107fSSatish Balay       N = nrow++ - 1; a->nz++; \
990520107fSSatish Balay       /* shift up all the later entries in this row */ \
1000520107fSSatish Balay       for (ii=N; ii>=_i; ii--) { \
1010520107fSSatish Balay         rp[ii+1] = rp[ii]; \
1020520107fSSatish Balay         ap[ii+1] = ap[ii]; \
1030520107fSSatish Balay       } \
104f5e9677aSSatish Balay       rp[_i] = col1;  \
1050520107fSSatish Balay       ap[_i] = value;  \
10630770e4dSSatish Balay       a_noinsert: ; \
1070520107fSSatish Balay       ailen[row] = nrow; \
1080520107fSSatish Balay }
1090a198c4cSBarry Smith 
11030770e4dSSatish Balay #define MatSetValues_SeqAIJ_B_Private(row,col,value,addv) \
11130770e4dSSatish Balay { \
11230770e4dSSatish Balay  \
11330770e4dSSatish Balay     rp   = bj + bi[row] + shift; ap = ba + bi[row] + shift; \
11430770e4dSSatish Balay     rmax = bimax[row]; nrow = bilen[row];  \
11530770e4dSSatish Balay     col1 = col - shift; \
11630770e4dSSatish Balay      \
117ba4e3ef2SSatish Balay     low = 0; high = nrow; \
118ba4e3ef2SSatish Balay     while (high-low > 5) { \
119ba4e3ef2SSatish Balay       t = (low+high)/2; \
120ba4e3ef2SSatish Balay       if (rp[t] > col) high = t; \
121ba4e3ef2SSatish Balay       else             low  = t; \
122ba4e3ef2SSatish Balay     } \
1232335bdd2SSatish Balay        for (_i=low; _i<high; _i++) { \
12430770e4dSSatish Balay         if (rp[_i] > col1) break; \
12530770e4dSSatish Balay         if (rp[_i] == col1) { \
12630770e4dSSatish Balay           if (addv == ADD_VALUES) ap[_i] += value;   \
12730770e4dSSatish Balay           else                  ap[_i] = value; \
12830770e4dSSatish Balay           goto b_noinsert; \
12930770e4dSSatish Balay         } \
13030770e4dSSatish Balay       }  \
13189280ab3SLois Curfman McInnes       if (nonew == 1) goto b_noinsert; \
132a45adfd6SMatthew Knepley       else if (nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%d, %d) into matrix", row, col); \
13330770e4dSSatish Balay       if (nrow >= rmax) { \
13430770e4dSSatish Balay         /* there is no extra room in row, therefore enlarge */ \
135273d9f13SBarry Smith         int    new_nz = bi[bm] + CHUNKSIZE,len,*new_i,*new_j; \
13687828ca2SBarry Smith         PetscScalar *new_a; \
13730770e4dSSatish Balay  \
138a45adfd6SMatthew Knepley         if (nonew == -2) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%d, %d) in the matrix", row, col); \
13989280ab3SLois Curfman McInnes  \
14030770e4dSSatish Balay         /* malloc new storage space */ \
14187828ca2SBarry Smith         len     = new_nz*(sizeof(int)+sizeof(PetscScalar))+(bm+1)*sizeof(int); \
142b0a32e0cSBarry Smith         ierr    = PetscMalloc(len,&new_a);CHKERRQ(ierr); \
14330770e4dSSatish Balay         new_j   = (int*)(new_a + new_nz); \
14430770e4dSSatish Balay         new_i   = new_j + new_nz; \
14530770e4dSSatish Balay  \
14630770e4dSSatish Balay         /* copy over old data into new slots */ \
14730770e4dSSatish Balay         for (ii=0; ii<row+1; ii++) {new_i[ii] = bi[ii];} \
148273d9f13SBarry Smith         for (ii=row+1; ii<bm+1; ii++) {new_i[ii] = bi[ii]+CHUNKSIZE;} \
149549d3d68SSatish Balay         ierr = PetscMemcpy(new_j,bj,(bi[row]+nrow+shift)*sizeof(int));CHKERRQ(ierr); \
15030770e4dSSatish Balay         len = (new_nz - CHUNKSIZE - bi[row] - nrow - shift); \
151549d3d68SSatish Balay         ierr = PetscMemcpy(new_j+bi[row]+shift+nrow+CHUNKSIZE,bj+bi[row]+shift+nrow, \
152549d3d68SSatish Balay                                                            len*sizeof(int));CHKERRQ(ierr); \
15387828ca2SBarry Smith         ierr = PetscMemcpy(new_a,ba,(bi[row]+nrow+shift)*sizeof(PetscScalar));CHKERRQ(ierr); \
154549d3d68SSatish Balay         ierr = PetscMemcpy(new_a+bi[row]+shift+nrow+CHUNKSIZE,ba+bi[row]+shift+nrow, \
15587828ca2SBarry Smith                                                            len*sizeof(PetscScalar));CHKERRQ(ierr);  \
15630770e4dSSatish Balay         /* free up old matrix storage */ \
15730770e4dSSatish Balay  \
158606d414cSSatish Balay         ierr = PetscFree(b->a);CHKERRQ(ierr);  \
159606d414cSSatish Balay         if (!b->singlemalloc) { \
160606d414cSSatish Balay           ierr = PetscFree(b->i);CHKERRQ(ierr); \
161606d414cSSatish Balay           ierr = PetscFree(b->j);CHKERRQ(ierr); \
162606d414cSSatish Balay         } \
16374c639caSSatish Balay         ba = b->a = new_a; bi = b->i = new_i; bj = b->j = new_j;  \
1647c922b88SBarry Smith         b->singlemalloc = PETSC_TRUE; \
16530770e4dSSatish Balay  \
16630770e4dSSatish Balay         rp   = bj + bi[row] + shift; ap = ba + bi[row] + shift; \
16730770e4dSSatish Balay         rmax = bimax[row] = bimax[row] + CHUNKSIZE; \
16887828ca2SBarry Smith         PetscLogObjectMemory(B,CHUNKSIZE*(sizeof(int) + sizeof(PetscScalar))); \
16974c639caSSatish Balay         b->maxnz += CHUNKSIZE; \
17074c639caSSatish Balay         b->reallocs++; \
17130770e4dSSatish Balay       } \
17274c639caSSatish Balay       N = nrow++ - 1; b->nz++; \
17330770e4dSSatish Balay       /* shift up all the later entries in this row */ \
17430770e4dSSatish Balay       for (ii=N; ii>=_i; ii--) { \
17530770e4dSSatish Balay         rp[ii+1] = rp[ii]; \
17630770e4dSSatish Balay         ap[ii+1] = ap[ii]; \
17730770e4dSSatish Balay       } \
17830770e4dSSatish Balay       rp[_i] = col1;  \
17930770e4dSSatish Balay       ap[_i] = value;  \
18030770e4dSSatish Balay       b_noinsert: ; \
18130770e4dSSatish Balay       bilen[row] = nrow; \
18230770e4dSSatish Balay }
18330770e4dSSatish Balay 
1844a2ae208SSatish Balay #undef __FUNCT__
1854a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_MPIAIJ"
186dfbe8321SBarry Smith PetscErrorCode MatSetValues_MPIAIJ(Mat mat,int m,const int im[],int n,const int in[],const PetscScalar v[],InsertMode addv)
1878a729477SBarry Smith {
18844a69424SLois Curfman McInnes   Mat_MPIAIJ   *aij = (Mat_MPIAIJ*)mat->data;
18987828ca2SBarry Smith   PetscScalar  value;
190dfbe8321SBarry Smith   PetscErrorCode ierr;
191dfbe8321SBarry Smith   int          i,j,rstart = aij->rstart,rend = aij->rend;
1921eb62cbbSBarry Smith   int          cstart = aij->cstart,cend = aij->cend,row,col;
193273d9f13SBarry Smith   PetscTruth   roworiented = aij->roworiented;
1948a729477SBarry Smith 
1950520107fSSatish Balay   /* Some Variables required in the macro */
1964ee7247eSSatish Balay   Mat          A = aij->A;
1974ee7247eSSatish Balay   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
19830770e4dSSatish Balay   int          *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j;
19987828ca2SBarry Smith   PetscScalar  *aa = a->a;
200329f5518SBarry Smith   PetscTruth   ignorezeroentries = (((a->ignorezeroentries)&&(addv==ADD_VALUES))?PETSC_TRUE:PETSC_FALSE);
20130770e4dSSatish Balay   Mat          B = aij->B;
20230770e4dSSatish Balay   Mat_SeqAIJ   *b = (Mat_SeqAIJ*)B->data;
203273d9f13SBarry Smith   int          *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->m,am = aij->A->m;
20487828ca2SBarry Smith   PetscScalar  *ba = b->a;
20530770e4dSSatish Balay 
206ba4e3ef2SSatish Balay   int          *rp,ii,nrow,_i,rmax,N,col1,low,high,t;
207bfec09a0SHong Zhang   int          nonew = a->nonew,shift=0;
20887828ca2SBarry Smith   PetscScalar  *ap;
2094ee7247eSSatish Balay 
2103a40ed3dSBarry Smith   PetscFunctionBegin;
2118a729477SBarry Smith   for (i=0; i<m; i++) {
2125ef9f2a5SBarry Smith     if (im[i] < 0) continue;
213aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g)
214590ac198SBarry Smith     if (im[i] >= mat->M) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %d max %d",im[i],mat->M-1);
2150a198c4cSBarry Smith #endif
2164b0e389bSBarry Smith     if (im[i] >= rstart && im[i] < rend) {
2174b0e389bSBarry Smith       row = im[i] - rstart;
2181eb62cbbSBarry Smith       for (j=0; j<n; j++) {
2194b0e389bSBarry Smith         if (in[j] >= cstart && in[j] < cend){
2204b0e389bSBarry Smith           col = in[j] - cstart;
2214b0e389bSBarry Smith           if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
2225b8514ebSBarry Smith           if (ignorezeroentries && value == 0.0) continue;
22330770e4dSSatish Balay           MatSetValues_SeqAIJ_A_Private(row,col,value,addv);
2240520107fSSatish Balay           /* ierr = MatSetValues_SeqAIJ(aij->A,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */
225273d9f13SBarry Smith         } else if (in[j] < 0) continue;
226aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g)
227590ac198SBarry Smith         else if (in[j] >= mat->N) {SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %d max %d",in[j],mat->N-1);}
2280a198c4cSBarry Smith #endif
2291eb62cbbSBarry Smith         else {
230227d817aSBarry Smith           if (mat->was_assembled) {
231905e6a2fSBarry Smith             if (!aij->colmap) {
232905e6a2fSBarry Smith               ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
233905e6a2fSBarry Smith             }
234aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
2350f5bd95cSBarry Smith             ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr);
236fa46199cSSatish Balay 	    col--;
237b1fc9764SSatish Balay #else
238905e6a2fSBarry Smith             col = aij->colmap[in[j]] - 1;
239b1fc9764SSatish Balay #endif
240ec8511deSBarry Smith             if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
2412493cbb0SBarry Smith               ierr = DisAssemble_MPIAIJ(mat);CHKERRQ(ierr);
2424b0e389bSBarry Smith               col =  in[j];
2439bf004c3SSatish Balay               /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
244f9508a3cSSatish Balay               B = aij->B;
245f9508a3cSSatish Balay               b = (Mat_SeqAIJ*)B->data;
246f9508a3cSSatish Balay               bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j;
247f9508a3cSSatish Balay               ba = b->a;
248d6dfbf8fSBarry Smith             }
249c48de900SBarry Smith           } else col = in[j];
2504b0e389bSBarry Smith           if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
2515b8514ebSBarry Smith           if (ignorezeroentries && value == 0.0) continue;
25230770e4dSSatish Balay           MatSetValues_SeqAIJ_B_Private(row,col,value,addv);
25330770e4dSSatish Balay           /* ierr = MatSetValues_SeqAIJ(aij->B,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */
2541eb62cbbSBarry Smith         }
2551eb62cbbSBarry Smith       }
2565ef9f2a5SBarry Smith     } else {
25790f02eecSBarry Smith       if (!aij->donotstash) {
258d36fbae8SSatish Balay         if (roworiented) {
2595b8514ebSBarry Smith           if (ignorezeroentries && v[i*n] == 0.0) continue;
2608798bf22SSatish Balay           ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n);CHKERRQ(ierr);
261d36fbae8SSatish Balay         } else {
2625b8514ebSBarry Smith           if (ignorezeroentries && v[i] == 0.0) continue;
2638798bf22SSatish Balay           ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m);CHKERRQ(ierr);
2644b0e389bSBarry Smith         }
2651eb62cbbSBarry Smith       }
2668a729477SBarry Smith     }
26790f02eecSBarry Smith   }
2683a40ed3dSBarry Smith   PetscFunctionReturn(0);
2698a729477SBarry Smith }
2708a729477SBarry Smith 
2714a2ae208SSatish Balay #undef __FUNCT__
2724a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_MPIAIJ"
273dfbe8321SBarry Smith PetscErrorCode MatGetValues_MPIAIJ(Mat mat,int m,const int idxm[],int n,const int idxn[],PetscScalar v[])
274b49de8d1SLois Curfman McInnes {
275b49de8d1SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
276dfbe8321SBarry Smith   PetscErrorCode ierr;
277dfbe8321SBarry Smith   int        i,j,rstart = aij->rstart,rend = aij->rend;
278b49de8d1SLois Curfman McInnes   int        cstart = aij->cstart,cend = aij->cend,row,col;
279b49de8d1SLois Curfman McInnes 
2803a40ed3dSBarry Smith   PetscFunctionBegin;
281b49de8d1SLois Curfman McInnes   for (i=0; i<m; i++) {
282590ac198SBarry Smith     if (idxm[i] < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %d",idxm[i]);
283952d7e9fSSatish Balay     if (idxm[i] >= mat->M) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %d max %d",idxm[i],mat->M-1);
284b49de8d1SLois Curfman McInnes     if (idxm[i] >= rstart && idxm[i] < rend) {
285b49de8d1SLois Curfman McInnes       row = idxm[i] - rstart;
286b49de8d1SLois Curfman McInnes       for (j=0; j<n; j++) {
287590ac198SBarry Smith         if (idxn[j] < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %d",idxn[j]);
288590ac198SBarry Smith         if (idxn[j] >= mat->N) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %d max %d",idxn[j],mat->N-1);
289b49de8d1SLois Curfman McInnes         if (idxn[j] >= cstart && idxn[j] < cend){
290b49de8d1SLois Curfman McInnes           col = idxn[j] - cstart;
291b49de8d1SLois Curfman McInnes           ierr = MatGetValues(aij->A,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
292fa852ad4SSatish Balay         } else {
293905e6a2fSBarry Smith           if (!aij->colmap) {
294905e6a2fSBarry Smith             ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
295905e6a2fSBarry Smith           }
296aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
2970f5bd95cSBarry Smith           ierr = PetscTableFind(aij->colmap,idxn[j]+1,&col);CHKERRQ(ierr);
298fa46199cSSatish Balay           col --;
299b1fc9764SSatish Balay #else
300905e6a2fSBarry Smith           col = aij->colmap[idxn[j]] - 1;
301b1fc9764SSatish Balay #endif
302e60e1c95SSatish Balay           if ((col < 0) || (aij->garray[col] != idxn[j])) *(v+i*n+j) = 0.0;
303d9d09a02SSatish Balay           else {
304b49de8d1SLois Curfman McInnes             ierr = MatGetValues(aij->B,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
305b49de8d1SLois Curfman McInnes           }
306b49de8d1SLois Curfman McInnes         }
307b49de8d1SLois Curfman McInnes       }
308a8c6a408SBarry Smith     } else {
30929bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"Only local values currently supported");
310b49de8d1SLois Curfman McInnes     }
311b49de8d1SLois Curfman McInnes   }
3123a40ed3dSBarry Smith   PetscFunctionReturn(0);
313b49de8d1SLois Curfman McInnes }
314bc5ccf88SSatish Balay 
3154a2ae208SSatish Balay #undef __FUNCT__
3164a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyBegin_MPIAIJ"
317dfbe8321SBarry Smith PetscErrorCode MatAssemblyBegin_MPIAIJ(Mat mat,MatAssemblyType mode)
318bc5ccf88SSatish Balay {
319bc5ccf88SSatish Balay   Mat_MPIAIJ  *aij = (Mat_MPIAIJ*)mat->data;
320dfbe8321SBarry Smith   PetscErrorCode ierr;
321dfbe8321SBarry Smith   int         nstash,reallocs;
322bc5ccf88SSatish Balay   InsertMode  addv;
323bc5ccf88SSatish Balay 
324bc5ccf88SSatish Balay   PetscFunctionBegin;
325bc5ccf88SSatish Balay   if (aij->donotstash) {
326bc5ccf88SSatish Balay     PetscFunctionReturn(0);
327bc5ccf88SSatish Balay   }
328bc5ccf88SSatish Balay 
329bc5ccf88SSatish Balay   /* make sure all processors are either in INSERTMODE or ADDMODE */
330bc5ccf88SSatish Balay   ierr = MPI_Allreduce(&mat->insertmode,&addv,1,MPI_INT,MPI_BOR,mat->comm);CHKERRQ(ierr);
331bc5ccf88SSatish Balay   if (addv == (ADD_VALUES|INSERT_VALUES)) {
33229bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Some processors inserted others added");
333bc5ccf88SSatish Balay   }
334bc5ccf88SSatish Balay   mat->insertmode = addv; /* in case this processor had no cache */
335bc5ccf88SSatish Balay 
3368798bf22SSatish Balay   ierr = MatStashScatterBegin_Private(&mat->stash,aij->rowners);CHKERRQ(ierr);
3378798bf22SSatish Balay   ierr = MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);CHKERRQ(ierr);
338b0a32e0cSBarry Smith   PetscLogInfo(aij->A,"MatAssemblyBegin_MPIAIJ:Stash has %d entries, uses %d mallocs.\n",nstash,reallocs);
339bc5ccf88SSatish Balay   PetscFunctionReturn(0);
340bc5ccf88SSatish Balay }
341bc5ccf88SSatish Balay 
342bc5ccf88SSatish Balay 
3434a2ae208SSatish Balay #undef __FUNCT__
3444a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_MPIAIJ"
345dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_MPIAIJ(Mat mat,MatAssemblyType mode)
346bc5ccf88SSatish Balay {
347bc5ccf88SSatish Balay   Mat_MPIAIJ  *aij = (Mat_MPIAIJ*)mat->data;
34824f910e3SHong Zhang   Mat_SeqAIJ  *a=(Mat_SeqAIJ *)aij->A->data,*b= (Mat_SeqAIJ *)aij->B->data;
349*6849ba73SBarry Smith   PetscErrorCode ierr;
350*6849ba73SBarry Smith   int         i,j,rstart,ncols,n,flg;
351bc5ccf88SSatish Balay   int         *row,*col,other_disassembled;
35287828ca2SBarry Smith   PetscScalar *val;
353bc5ccf88SSatish Balay   InsertMode  addv = mat->insertmode;
354bc5ccf88SSatish Balay 
355bc5ccf88SSatish Balay   PetscFunctionBegin;
356bc5ccf88SSatish Balay   if (!aij->donotstash) {
357a2d1c673SSatish Balay     while (1) {
3588798bf22SSatish Balay       ierr = MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);CHKERRQ(ierr);
359a2d1c673SSatish Balay       if (!flg) break;
360a2d1c673SSatish Balay 
361bc5ccf88SSatish Balay       for (i=0; i<n;) {
362bc5ccf88SSatish Balay         /* Now identify the consecutive vals belonging to the same row */
363bc5ccf88SSatish Balay         for (j=i,rstart=row[j]; j<n; j++) { if (row[j] != rstart) break; }
364bc5ccf88SSatish Balay         if (j < n) ncols = j-i;
365bc5ccf88SSatish Balay         else       ncols = n-i;
366bc5ccf88SSatish Balay         /* Now assemble all these values with a single function call */
367bc5ccf88SSatish Balay         ierr = MatSetValues_MPIAIJ(mat,1,row+i,ncols,col+i,val+i,addv);CHKERRQ(ierr);
368bc5ccf88SSatish Balay         i = j;
369bc5ccf88SSatish Balay       }
370bc5ccf88SSatish Balay     }
3718798bf22SSatish Balay     ierr = MatStashScatterEnd_Private(&mat->stash);CHKERRQ(ierr);
372bc5ccf88SSatish Balay   }
373bc5ccf88SSatish Balay 
374bc5ccf88SSatish Balay   ierr = MatAssemblyBegin(aij->A,mode);CHKERRQ(ierr);
375bc5ccf88SSatish Balay   ierr = MatAssemblyEnd(aij->A,mode);CHKERRQ(ierr);
376bc5ccf88SSatish Balay 
377bc5ccf88SSatish Balay   /* determine if any processor has disassembled, if so we must
378bc5ccf88SSatish Balay      also disassemble ourselfs, in order that we may reassemble. */
379bc5ccf88SSatish Balay   /*
380bc5ccf88SSatish Balay      if nonzero structure of submatrix B cannot change then we know that
381bc5ccf88SSatish Balay      no processor disassembled thus we can skip this stuff
382bc5ccf88SSatish Balay   */
383bc5ccf88SSatish Balay   if (!((Mat_SeqAIJ*)aij->B->data)->nonew)  {
384bc5ccf88SSatish Balay     ierr = MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPI_INT,MPI_PROD,mat->comm);CHKERRQ(ierr);
385bc5ccf88SSatish Balay     if (mat->was_assembled && !other_disassembled) {
386bc5ccf88SSatish Balay       ierr = DisAssemble_MPIAIJ(mat);CHKERRQ(ierr);
387bc5ccf88SSatish Balay     }
388bc5ccf88SSatish Balay   }
389bc5ccf88SSatish Balay 
390bc5ccf88SSatish Balay   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
391bc5ccf88SSatish Balay     ierr = MatSetUpMultiply_MPIAIJ(mat);CHKERRQ(ierr);
392bc5ccf88SSatish Balay   }
393bc5ccf88SSatish Balay   ierr = MatAssemblyBegin(aij->B,mode);CHKERRQ(ierr);
394bc5ccf88SSatish Balay   ierr = MatAssemblyEnd(aij->B,mode);CHKERRQ(ierr);
395bc5ccf88SSatish Balay 
396606d414cSSatish Balay   if (aij->rowvalues) {
397606d414cSSatish Balay     ierr = PetscFree(aij->rowvalues);CHKERRQ(ierr);
398606d414cSSatish Balay     aij->rowvalues = 0;
399606d414cSSatish Balay   }
400a30b2313SHong Zhang 
401a30b2313SHong Zhang   /* used by MatAXPY() */
402a30b2313SHong Zhang   a->xtoy = 0; b->xtoy = 0;
403a30b2313SHong Zhang   a->XtoY = 0; b->XtoY = 0;
404a30b2313SHong Zhang 
405bc5ccf88SSatish Balay   PetscFunctionReturn(0);
406bc5ccf88SSatish Balay }
407bc5ccf88SSatish Balay 
4084a2ae208SSatish Balay #undef __FUNCT__
4094a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_MPIAIJ"
410dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_MPIAIJ(Mat A)
4111eb62cbbSBarry Smith {
41244a69424SLois Curfman McInnes   Mat_MPIAIJ *l = (Mat_MPIAIJ*)A->data;
413dfbe8321SBarry Smith   PetscErrorCode ierr;
4143a40ed3dSBarry Smith 
4153a40ed3dSBarry Smith   PetscFunctionBegin;
41678b31e54SBarry Smith   ierr = MatZeroEntries(l->A);CHKERRQ(ierr);
41778b31e54SBarry Smith   ierr = MatZeroEntries(l->B);CHKERRQ(ierr);
4183a40ed3dSBarry Smith   PetscFunctionReturn(0);
4191eb62cbbSBarry Smith }
4201eb62cbbSBarry Smith 
4214a2ae208SSatish Balay #undef __FUNCT__
4224a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_MPIAIJ"
423dfbe8321SBarry Smith PetscErrorCode MatZeroRows_MPIAIJ(Mat A,IS is,const PetscScalar *diag)
4241eb62cbbSBarry Smith {
42544a69424SLois Curfman McInnes   Mat_MPIAIJ     *l = (Mat_MPIAIJ*)A->data;
426*6849ba73SBarry Smith   PetscErrorCode ierr;
427*6849ba73SBarry Smith   int            i,N,*rows,*owners = l->rowners,size = l->size;
428c1dc657dSBarry Smith   int            *nprocs,j,idx,nsends,row;
42917699dbbSLois Curfman McInnes   int            nmax,*svalues,*starts,*owner,nrecvs,rank = l->rank;
4305392566eSBarry Smith   int            *rvalues,tag = A->tag,count,base,slen,n,*source;
4316a5c57faSSatish Balay   int            *lens,imdex,*lrows,*values,rstart=l->rstart;
432d6dfbf8fSBarry Smith   MPI_Comm       comm = A->comm;
4331eb62cbbSBarry Smith   MPI_Request    *send_waits,*recv_waits;
4341eb62cbbSBarry Smith   MPI_Status     recv_status,*send_status;
4351eb62cbbSBarry Smith   IS             istmp;
43635d8aa7fSBarry Smith   PetscTruth     found;
4371eb62cbbSBarry Smith 
4383a40ed3dSBarry Smith   PetscFunctionBegin;
439e03a110bSBarry Smith   ierr = ISGetLocalSize(is,&N);CHKERRQ(ierr);
44078b31e54SBarry Smith   ierr = ISGetIndices(is,&rows);CHKERRQ(ierr);
4411eb62cbbSBarry Smith 
4421eb62cbbSBarry Smith   /*  first count number of contributors to each processor */
443b0a32e0cSBarry Smith   ierr = PetscMalloc(2*size*sizeof(int),&nprocs);CHKERRQ(ierr);
444549d3d68SSatish Balay   ierr   = PetscMemzero(nprocs,2*size*sizeof(int));CHKERRQ(ierr);
445b0a32e0cSBarry Smith   ierr = PetscMalloc((N+1)*sizeof(int),&owner);CHKERRQ(ierr); /* see note*/
4461eb62cbbSBarry Smith   for (i=0; i<N; i++) {
4471eb62cbbSBarry Smith     idx = rows[i];
44835d8aa7fSBarry Smith     found = PETSC_FALSE;
44917699dbbSLois Curfman McInnes     for (j=0; j<size; j++) {
4501eb62cbbSBarry Smith       if (idx >= owners[j] && idx < owners[j+1]) {
451c1dc657dSBarry Smith         nprocs[2*j]++; nprocs[2*j+1] = 1; owner[i] = j; found = PETSC_TRUE; break;
4521eb62cbbSBarry Smith       }
4531eb62cbbSBarry Smith     }
45429bbc08cSBarry Smith     if (!found) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Index out of range");
4551eb62cbbSBarry Smith   }
456c1dc657dSBarry Smith   nsends = 0;  for (i=0; i<size; i++) { nsends += nprocs[2*i+1];}
4571eb62cbbSBarry Smith 
4581eb62cbbSBarry Smith   /* inform other processors of number of messages and max length*/
459c1dc657dSBarry Smith   ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr);
4601eb62cbbSBarry Smith 
4611eb62cbbSBarry Smith   /* post receives:   */
462b0a32e0cSBarry Smith   ierr = PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(int),&rvalues);CHKERRQ(ierr);
463b0a32e0cSBarry Smith   ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr);
4641eb62cbbSBarry Smith   for (i=0; i<nrecvs; i++) {
465ca161407SBarry Smith     ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPI_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr);
4661eb62cbbSBarry Smith   }
4671eb62cbbSBarry Smith 
4681eb62cbbSBarry Smith   /* do sends:
4691eb62cbbSBarry Smith       1) starts[i] gives the starting index in svalues for stuff going to
4701eb62cbbSBarry Smith          the ith processor
4711eb62cbbSBarry Smith   */
472b0a32e0cSBarry Smith   ierr = PetscMalloc((N+1)*sizeof(int),&svalues);CHKERRQ(ierr);
473b0a32e0cSBarry Smith   ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr);
474b0a32e0cSBarry Smith   ierr = PetscMalloc((size+1)*sizeof(int),&starts);CHKERRQ(ierr);
4751eb62cbbSBarry Smith   starts[0] = 0;
476c1dc657dSBarry Smith   for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
4771eb62cbbSBarry Smith   for (i=0; i<N; i++) {
4781eb62cbbSBarry Smith     svalues[starts[owner[i]]++] = rows[i];
4791eb62cbbSBarry Smith   }
4806831982aSBarry Smith   ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr);
4811eb62cbbSBarry Smith 
4821eb62cbbSBarry Smith   starts[0] = 0;
483c1dc657dSBarry Smith   for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
4841eb62cbbSBarry Smith   count = 0;
48517699dbbSLois Curfman McInnes   for (i=0; i<size; i++) {
486c1dc657dSBarry Smith     if (nprocs[2*i+1]) {
487c1dc657dSBarry Smith       ierr = MPI_Isend(svalues+starts[i],nprocs[2*i],MPI_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr);
4881eb62cbbSBarry Smith     }
4891eb62cbbSBarry Smith   }
490606d414cSSatish Balay   ierr = PetscFree(starts);CHKERRQ(ierr);
4911eb62cbbSBarry Smith 
49217699dbbSLois Curfman McInnes   base = owners[rank];
4931eb62cbbSBarry Smith 
4941eb62cbbSBarry Smith   /*  wait on receives */
495b0a32e0cSBarry Smith   ierr   = PetscMalloc(2*(nrecvs+1)*sizeof(int),&lens);CHKERRQ(ierr);
4961eb62cbbSBarry Smith   source = lens + nrecvs;
4971eb62cbbSBarry Smith   count  = nrecvs; slen = 0;
4981eb62cbbSBarry Smith   while (count) {
499ca161407SBarry Smith     ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
5001eb62cbbSBarry Smith     /* unpack receives into our local space */
501ca161407SBarry Smith     ierr = MPI_Get_count(&recv_status,MPI_INT,&n);CHKERRQ(ierr);
502d6dfbf8fSBarry Smith     source[imdex]  = recv_status.MPI_SOURCE;
503d6dfbf8fSBarry Smith     lens[imdex]    = n;
5041eb62cbbSBarry Smith     slen          += n;
5051eb62cbbSBarry Smith     count--;
5061eb62cbbSBarry Smith   }
507606d414cSSatish Balay   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
5081eb62cbbSBarry Smith 
5091eb62cbbSBarry Smith   /* move the data into the send scatter */
510b0a32e0cSBarry Smith   ierr = PetscMalloc((slen+1)*sizeof(int),&lrows);CHKERRQ(ierr);
5111eb62cbbSBarry Smith   count = 0;
5121eb62cbbSBarry Smith   for (i=0; i<nrecvs; i++) {
5131eb62cbbSBarry Smith     values = rvalues + i*nmax;
5141eb62cbbSBarry Smith     for (j=0; j<lens[i]; j++) {
5151eb62cbbSBarry Smith       lrows[count++] = values[j] - base;
5161eb62cbbSBarry Smith     }
5171eb62cbbSBarry Smith   }
518606d414cSSatish Balay   ierr = PetscFree(rvalues);CHKERRQ(ierr);
519606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
520606d414cSSatish Balay   ierr = PetscFree(owner);CHKERRQ(ierr);
521606d414cSSatish Balay   ierr = PetscFree(nprocs);CHKERRQ(ierr);
5221eb62cbbSBarry Smith 
5231eb62cbbSBarry Smith   /* actually zap the local rows */
524029af93fSBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,slen,lrows,&istmp);CHKERRQ(ierr);
525b0a32e0cSBarry Smith   PetscLogObjectParent(A,istmp);
5266a5c57faSSatish Balay 
5276eb55b6aSBarry Smith   /*
5286eb55b6aSBarry Smith         Zero the required rows. If the "diagonal block" of the matrix
5296eb55b6aSBarry Smith      is square and the user wishes to set the diagonal we use seperate
5306eb55b6aSBarry Smith      code so that MatSetValues() is not called for each diagonal allocating
5316eb55b6aSBarry Smith      new memory, thus calling lots of mallocs and slowing things down.
5326eb55b6aSBarry Smith 
5336eb55b6aSBarry Smith        Contributed by: Mathew Knepley
5346eb55b6aSBarry Smith   */
535e2d53e46SBarry Smith   /* must zero l->B before l->A because the (diag) case below may put values into l->B*/
536e2d53e46SBarry Smith   ierr = MatZeroRows(l->B,istmp,0);CHKERRQ(ierr);
5376eb55b6aSBarry Smith   if (diag && (l->A->M == l->A->N)) {
5386eb55b6aSBarry Smith     ierr      = MatZeroRows(l->A,istmp,diag);CHKERRQ(ierr);
539e2d53e46SBarry Smith   } else if (diag) {
540e2d53e46SBarry Smith     ierr = MatZeroRows(l->A,istmp,0);CHKERRQ(ierr);
541fa46199cSSatish Balay     if (((Mat_SeqAIJ*)l->A->data)->nonew) {
54229bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"MatZeroRows() on rectangular matrices cannot be used with the Mat options\n\
543fa46199cSSatish Balay MAT_NO_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR");
5446525c446SSatish Balay     }
545e2d53e46SBarry Smith     for (i = 0; i < slen; i++) {
546e2d53e46SBarry Smith       row  = lrows[i] + rstart;
547e2d53e46SBarry Smith       ierr = MatSetValues(A,1,&row,1,&row,diag,INSERT_VALUES);CHKERRQ(ierr);
548e2d53e46SBarry Smith     }
549e2d53e46SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
550e2d53e46SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
5516eb55b6aSBarry Smith   } else {
5526a5c57faSSatish Balay     ierr = MatZeroRows(l->A,istmp,0);CHKERRQ(ierr);
5536eb55b6aSBarry Smith   }
55478b31e54SBarry Smith   ierr = ISDestroy(istmp);CHKERRQ(ierr);
555606d414cSSatish Balay   ierr = PetscFree(lrows);CHKERRQ(ierr);
55672dacd9aSBarry Smith 
5571eb62cbbSBarry Smith   /* wait on sends */
5581eb62cbbSBarry Smith   if (nsends) {
559b0a32e0cSBarry Smith     ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr);
560ca161407SBarry Smith     ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr);
561606d414cSSatish Balay     ierr = PetscFree(send_status);CHKERRQ(ierr);
5621eb62cbbSBarry Smith   }
563606d414cSSatish Balay   ierr = PetscFree(send_waits);CHKERRQ(ierr);
564606d414cSSatish Balay   ierr = PetscFree(svalues);CHKERRQ(ierr);
5651eb62cbbSBarry Smith 
5663a40ed3dSBarry Smith   PetscFunctionReturn(0);
5671eb62cbbSBarry Smith }
5681eb62cbbSBarry Smith 
5694a2ae208SSatish Balay #undef __FUNCT__
5704a2ae208SSatish Balay #define __FUNCT__ "MatMult_MPIAIJ"
571dfbe8321SBarry Smith PetscErrorCode MatMult_MPIAIJ(Mat A,Vec xx,Vec yy)
5721eb62cbbSBarry Smith {
573416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
574dfbe8321SBarry Smith   PetscErrorCode ierr;
575dfbe8321SBarry Smith   int        nt;
576416022c9SBarry Smith 
5773a40ed3dSBarry Smith   PetscFunctionBegin;
578a2ce50c7SBarry Smith   ierr = VecGetLocalSize(xx,&nt);CHKERRQ(ierr);
579273d9f13SBarry Smith   if (nt != A->n) {
580273d9f13SBarry Smith     SETERRQ2(PETSC_ERR_ARG_SIZ,"Incompatible partition of A (%d) and xx (%d)",A->n,nt);
581fbd6ef76SBarry Smith   }
58243a90d84SBarry Smith   ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
583f830108cSBarry Smith   ierr = (*a->A->ops->mult)(a->A,xx,yy);CHKERRQ(ierr);
58443a90d84SBarry Smith   ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
585f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);CHKERRQ(ierr);
5863a40ed3dSBarry Smith   PetscFunctionReturn(0);
5871eb62cbbSBarry Smith }
5881eb62cbbSBarry Smith 
5894a2ae208SSatish Balay #undef __FUNCT__
5904a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_MPIAIJ"
591dfbe8321SBarry Smith PetscErrorCode MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
592da3a660dSBarry Smith {
593416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
594dfbe8321SBarry Smith   PetscErrorCode ierr;
5953a40ed3dSBarry Smith 
5963a40ed3dSBarry Smith   PetscFunctionBegin;
59743a90d84SBarry Smith   ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
598f830108cSBarry Smith   ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
59943a90d84SBarry Smith   ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
600f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);CHKERRQ(ierr);
6013a40ed3dSBarry Smith   PetscFunctionReturn(0);
602da3a660dSBarry Smith }
603da3a660dSBarry Smith 
6044a2ae208SSatish Balay #undef __FUNCT__
6054a2ae208SSatish Balay #define __FUNCT__ "MatMultTranspose_MPIAIJ"
606dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_MPIAIJ(Mat A,Vec xx,Vec yy)
607da3a660dSBarry Smith {
608416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
609dfbe8321SBarry Smith   PetscErrorCode ierr;
610da3a660dSBarry Smith 
6113a40ed3dSBarry Smith   PetscFunctionBegin;
612da3a660dSBarry Smith   /* do nondiagonal part */
6137c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
614da3a660dSBarry Smith   /* send it on its way */
615537820f0SBarry Smith   ierr = VecScatterBegin(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
616da3a660dSBarry Smith   /* do local part */
6177c922b88SBarry Smith   ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr);
618da3a660dSBarry Smith   /* receive remote parts: note this assumes the values are not actually */
619da3a660dSBarry Smith   /* inserted in yy until the next line, which is true for my implementation*/
620da3a660dSBarry Smith   /* but is not perhaps always true. */
621537820f0SBarry Smith   ierr = VecScatterEnd(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
6223a40ed3dSBarry Smith   PetscFunctionReturn(0);
623da3a660dSBarry Smith }
624da3a660dSBarry Smith 
625cd0d46ebSvictorle EXTERN_C_BEGIN
626cd0d46ebSvictorle #undef __FUNCT__
6275fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_MPIAIJ"
628dfbe8321SBarry Smith PetscErrorCode MatIsTranspose_MPIAIJ(Mat Amat,Mat Bmat,PetscTruth tol,PetscTruth *f)
629cd0d46ebSvictorle {
6304f423910Svictorle   MPI_Comm comm;
631cd0d46ebSvictorle   Mat_MPIAIJ *Aij = (Mat_MPIAIJ *) Amat->data, *Bij;
63266501d38Svictorle   Mat        Adia = Aij->A, Bdia, Aoff,Boff,*Aoffs,*Boffs;
633cd0d46ebSvictorle   IS         Me,Notme;
634*6849ba73SBarry Smith   PetscErrorCode ierr;
635*6849ba73SBarry Smith   int        M,N,first,last,*notme,ntids,i;
636cd0d46ebSvictorle 
637cd0d46ebSvictorle   PetscFunctionBegin;
63842e5f5b4Svictorle 
63942e5f5b4Svictorle   /* Easy test: symmetric diagonal block */
64066501d38Svictorle   Bij = (Mat_MPIAIJ *) Bmat->data; Bdia = Bij->A;
6415485867bSBarry Smith   ierr = MatIsTranspose(Adia,Bdia,tol,f);CHKERRQ(ierr);
642cd0d46ebSvictorle   if (!*f) PetscFunctionReturn(0);
6434f423910Svictorle   ierr = PetscObjectGetComm((PetscObject)Amat,&comm);CHKERRQ(ierr);
6444f423910Svictorle   ierr = MPI_Comm_size(comm,&ntids);CHKERRQ(ierr);
6454f423910Svictorle   if (ntids==1) PetscFunctionReturn(0);
64642e5f5b4Svictorle 
64742e5f5b4Svictorle   /* Hard test: off-diagonal block. This takes a MatGetSubMatrix. */
648cd0d46ebSvictorle   ierr = MatGetSize(Amat,&M,&N);CHKERRQ(ierr);
649cd0d46ebSvictorle   ierr = MatGetOwnershipRange(Amat,&first,&last);CHKERRQ(ierr);
650cd0d46ebSvictorle   ierr = PetscMalloc((N-last+first)*sizeof(int),&notme);CHKERRQ(ierr);
651cd0d46ebSvictorle   for (i=0; i<first; i++) notme[i] = i;
652cd0d46ebSvictorle   for (i=last; i<M; i++) notme[i-last+first] = i;
653268466fbSBarry Smith   ierr = ISCreateGeneral(MPI_COMM_SELF,N-last+first,notme,&Notme);CHKERRQ(ierr);
654268466fbSBarry Smith   ierr = ISCreateStride(MPI_COMM_SELF,last-first,first,1,&Me);CHKERRQ(ierr);
655268466fbSBarry Smith   ierr = MatGetSubMatrices(Amat,1,&Me,&Notme,MAT_INITIAL_MATRIX,&Aoffs);CHKERRQ(ierr);
65666501d38Svictorle   Aoff = Aoffs[0];
657268466fbSBarry Smith   ierr = MatGetSubMatrices(Bmat,1,&Notme,&Me,MAT_INITIAL_MATRIX,&Boffs);CHKERRQ(ierr);
65866501d38Svictorle   Boff = Boffs[0];
6595485867bSBarry Smith   ierr = MatIsTranspose(Aoff,Boff,tol,f);CHKERRQ(ierr);
66066501d38Svictorle   ierr = MatDestroyMatrices(1,&Aoffs);CHKERRQ(ierr);
66166501d38Svictorle   ierr = MatDestroyMatrices(1,&Boffs);CHKERRQ(ierr);
66242e5f5b4Svictorle   ierr = ISDestroy(Me);CHKERRQ(ierr);
66342e5f5b4Svictorle   ierr = ISDestroy(Notme);CHKERRQ(ierr);
66442e5f5b4Svictorle 
665cd0d46ebSvictorle   PetscFunctionReturn(0);
666cd0d46ebSvictorle }
667cd0d46ebSvictorle EXTERN_C_END
668cd0d46ebSvictorle 
6694a2ae208SSatish Balay #undef __FUNCT__
6704a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_MPIAIJ"
671dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
672da3a660dSBarry Smith {
673416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
674dfbe8321SBarry Smith   PetscErrorCode ierr;
675da3a660dSBarry Smith 
6763a40ed3dSBarry Smith   PetscFunctionBegin;
677da3a660dSBarry Smith   /* do nondiagonal part */
6787c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
679da3a660dSBarry Smith   /* send it on its way */
680537820f0SBarry Smith   ierr = VecScatterBegin(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
681da3a660dSBarry Smith   /* do local part */
6827c922b88SBarry Smith   ierr = (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
683da3a660dSBarry Smith   /* receive remote parts: note this assumes the values are not actually */
684da3a660dSBarry Smith   /* inserted in yy until the next line, which is true for my implementation*/
685da3a660dSBarry Smith   /* but is not perhaps always true. */
6860a198c4cSBarry Smith   ierr = VecScatterEnd(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
6873a40ed3dSBarry Smith   PetscFunctionReturn(0);
688da3a660dSBarry Smith }
689da3a660dSBarry Smith 
6901eb62cbbSBarry Smith /*
6911eb62cbbSBarry Smith   This only works correctly for square matrices where the subblock A->A is the
6921eb62cbbSBarry Smith    diagonal block
6931eb62cbbSBarry Smith */
6944a2ae208SSatish Balay #undef __FUNCT__
6954a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_MPIAIJ"
696dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_MPIAIJ(Mat A,Vec v)
6971eb62cbbSBarry Smith {
698dfbe8321SBarry Smith   PetscErrorCode ierr;
699416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
7003a40ed3dSBarry Smith 
7013a40ed3dSBarry Smith   PetscFunctionBegin;
702273d9f13SBarry Smith   if (A->M != A->N) SETERRQ(PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block");
7035baf8537SBarry Smith   if (a->rstart != a->cstart || a->rend != a->cend) {
70429bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_SIZ,"row partition must equal col partition");
7053a40ed3dSBarry Smith   }
7063a40ed3dSBarry Smith   ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr);
7073a40ed3dSBarry Smith   PetscFunctionReturn(0);
7081eb62cbbSBarry Smith }
7091eb62cbbSBarry Smith 
7104a2ae208SSatish Balay #undef __FUNCT__
7114a2ae208SSatish Balay #define __FUNCT__ "MatScale_MPIAIJ"
712dfbe8321SBarry Smith PetscErrorCode MatScale_MPIAIJ(const PetscScalar aa[],Mat A)
713052efed2SBarry Smith {
714052efed2SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
715dfbe8321SBarry Smith   PetscErrorCode ierr;
7163a40ed3dSBarry Smith 
7173a40ed3dSBarry Smith   PetscFunctionBegin;
718052efed2SBarry Smith   ierr = MatScale(aa,a->A);CHKERRQ(ierr);
719052efed2SBarry Smith   ierr = MatScale(aa,a->B);CHKERRQ(ierr);
7203a40ed3dSBarry Smith   PetscFunctionReturn(0);
721052efed2SBarry Smith }
722052efed2SBarry Smith 
7234a2ae208SSatish Balay #undef __FUNCT__
7244a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_MPIAIJ"
725dfbe8321SBarry Smith PetscErrorCode MatDestroy_MPIAIJ(Mat mat)
7261eb62cbbSBarry Smith {
72744a69424SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
728dfbe8321SBarry Smith   PetscErrorCode ierr;
72983e2fdc7SBarry Smith 
7303a40ed3dSBarry Smith   PetscFunctionBegin;
731aa482453SBarry Smith #if defined(PETSC_USE_LOG)
732b0a32e0cSBarry Smith   PetscLogObjectState((PetscObject)mat,"Rows=%d, Cols=%d",mat->M,mat->N);
733a5a9c739SBarry Smith #endif
7348798bf22SSatish Balay   ierr = MatStashDestroy_Private(&mat->stash);CHKERRQ(ierr);
735606d414cSSatish Balay   ierr = PetscFree(aij->rowners);CHKERRQ(ierr);
73678b31e54SBarry Smith   ierr = MatDestroy(aij->A);CHKERRQ(ierr);
73778b31e54SBarry Smith   ierr = MatDestroy(aij->B);CHKERRQ(ierr);
738aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
7390f5bd95cSBarry Smith   if (aij->colmap) {ierr = PetscTableDelete(aij->colmap);CHKERRQ(ierr);}
740b1fc9764SSatish Balay #else
741606d414cSSatish Balay   if (aij->colmap) {ierr = PetscFree(aij->colmap);CHKERRQ(ierr);}
742b1fc9764SSatish Balay #endif
743606d414cSSatish Balay   if (aij->garray) {ierr = PetscFree(aij->garray);CHKERRQ(ierr);}
7447c922b88SBarry Smith   if (aij->lvec)   {ierr = VecDestroy(aij->lvec);CHKERRQ(ierr);}
7457c922b88SBarry Smith   if (aij->Mvctx)  {ierr = VecScatterDestroy(aij->Mvctx);CHKERRQ(ierr);}
746606d414cSSatish Balay   if (aij->rowvalues) {ierr = PetscFree(aij->rowvalues);CHKERRQ(ierr);}
747606d414cSSatish Balay   ierr = PetscFree(aij);CHKERRQ(ierr);
7483a40ed3dSBarry Smith   PetscFunctionReturn(0);
7491eb62cbbSBarry Smith }
750ee50ffe9SBarry Smith 
7514a2ae208SSatish Balay #undef __FUNCT__
7528e2fed03SBarry Smith #define __FUNCT__ "MatView_MPIAIJ_Binary"
753dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ_Binary(Mat mat,PetscViewer viewer)
7548e2fed03SBarry Smith {
7558e2fed03SBarry Smith   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*)mat->data;
7568e2fed03SBarry Smith   Mat_SeqAIJ*       A = (Mat_SeqAIJ*)aij->A->data;
7578e2fed03SBarry Smith   Mat_SeqAIJ*       B = (Mat_SeqAIJ*)aij->B->data;
758*6849ba73SBarry Smith   PetscErrorCode ierr;
759*6849ba73SBarry Smith   int               nz,fd,header[4],rank,size,*row_lengths,*range,rlen,i,tag = ((PetscObject)viewer)->tag;
760b021249fSBarry Smith   int               nzmax,*column_indices,j,k,col,*garray = aij->garray,cnt,cstart = aij->cstart,rnz;
7618e2fed03SBarry Smith   PetscScalar       *column_values;
7628e2fed03SBarry Smith 
7638e2fed03SBarry Smith   PetscFunctionBegin;
7648e2fed03SBarry Smith   ierr = MPI_Comm_rank(mat->comm,&rank);CHKERRQ(ierr);
7658e2fed03SBarry Smith   ierr = MPI_Comm_size(mat->comm,&size);CHKERRQ(ierr);
7668e2fed03SBarry Smith   nz   = A->nz + B->nz;
767958c9bccSBarry Smith   if (!rank) {
7688e2fed03SBarry Smith     header[0] = MAT_FILE_COOKIE;
7698e2fed03SBarry Smith     header[1] = mat->M;
7708e2fed03SBarry Smith     header[2] = mat->N;
7718e2fed03SBarry Smith     ierr = MPI_Reduce(&nz,&header[3],1,MPI_INT,MPI_SUM,0,mat->comm);CHKERRQ(ierr);
7728e2fed03SBarry Smith     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
7738e2fed03SBarry Smith     ierr = PetscBinaryWrite(fd,header,4,PETSC_INT,1);CHKERRQ(ierr);
7748e2fed03SBarry Smith     /* get largest number of rows any processor has */
7758e2fed03SBarry Smith     rlen = mat->m;
7768e2fed03SBarry Smith     ierr = PetscMapGetGlobalRange(mat->rmap,&range);CHKERRQ(ierr);
7778e2fed03SBarry Smith     for (i=1; i<size; i++) {
7788e2fed03SBarry Smith       rlen = PetscMax(rlen,range[i+1] - range[i]);
7798e2fed03SBarry Smith     }
7808e2fed03SBarry Smith   } else {
7818e2fed03SBarry Smith     ierr = MPI_Reduce(&nz,0,1,MPI_INT,MPI_SUM,0,mat->comm);CHKERRQ(ierr);
7828e2fed03SBarry Smith     rlen = mat->m;
7838e2fed03SBarry Smith   }
7848e2fed03SBarry Smith 
7858e2fed03SBarry Smith   /* load up the local row counts */
7868e2fed03SBarry Smith   ierr = PetscMalloc((rlen+1)*sizeof(int),&row_lengths);CHKERRQ(ierr);
7878e2fed03SBarry Smith   for (i=0; i<mat->m; i++) {
7888e2fed03SBarry Smith     row_lengths[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i];
7898e2fed03SBarry Smith   }
7908e2fed03SBarry Smith 
7918e2fed03SBarry Smith   /* store the row lengths to the file */
792958c9bccSBarry Smith   if (!rank) {
7938e2fed03SBarry Smith     MPI_Status status;
7948e2fed03SBarry Smith     ierr = PetscBinaryWrite(fd,row_lengths,mat->m,PETSC_INT,1);CHKERRQ(ierr);
7958e2fed03SBarry Smith     for (i=1; i<size; i++) {
7968e2fed03SBarry Smith       rlen = range[i+1] - range[i];
7978e2fed03SBarry Smith       ierr = MPI_Recv(row_lengths,rlen,MPI_INT,i,tag,mat->comm,&status);CHKERRQ(ierr);
7988e2fed03SBarry Smith       ierr = PetscBinaryWrite(fd,row_lengths,rlen,PETSC_INT,1);CHKERRQ(ierr);
7998e2fed03SBarry Smith     }
8008e2fed03SBarry Smith   } else {
8018e2fed03SBarry Smith     ierr = MPI_Send(row_lengths,mat->m,MPI_INT,0,tag,mat->comm);CHKERRQ(ierr);
8028e2fed03SBarry Smith   }
8038e2fed03SBarry Smith   ierr = PetscFree(row_lengths);CHKERRQ(ierr);
8048e2fed03SBarry Smith 
8058e2fed03SBarry Smith   /* load up the local column indices */
8068e2fed03SBarry Smith   nzmax = nz; /* )th processor needs space a largest processor needs */
8078e2fed03SBarry Smith   ierr = MPI_Reduce(&nz,&nzmax,1,MPI_INT,MPI_MAX,0,mat->comm);CHKERRQ(ierr);
8088e2fed03SBarry Smith   ierr = PetscMalloc((nzmax+1)*sizeof(int),&column_indices);CHKERRQ(ierr);
8098e2fed03SBarry Smith   cnt  = 0;
8108e2fed03SBarry Smith   for (i=0; i<mat->m; i++) {
8118e2fed03SBarry Smith     for (j=B->i[i]; j<B->i[i+1]; j++) {
8128e2fed03SBarry Smith       if ( (col = garray[B->j[j]]) > cstart) break;
8138e2fed03SBarry Smith       column_indices[cnt++] = col;
8148e2fed03SBarry Smith     }
8158e2fed03SBarry Smith     for (k=A->i[i]; k<A->i[i+1]; k++) {
8168e2fed03SBarry Smith       column_indices[cnt++] = A->j[k] + cstart;
8178e2fed03SBarry Smith     }
8188e2fed03SBarry Smith     for (; j<B->i[i+1]; j++) {
8198e2fed03SBarry Smith       column_indices[cnt++] = garray[B->j[j]];
8208e2fed03SBarry Smith     }
8218e2fed03SBarry Smith   }
8228e2fed03SBarry Smith   if (cnt != A->nz + B->nz) SETERRQ2(PETSC_ERR_LIB,"Internal PETSc error: cnt = %d nz = %d",cnt,A->nz+B->nz);
8238e2fed03SBarry Smith 
8248e2fed03SBarry Smith   /* store the column indices to the file */
825958c9bccSBarry Smith   if (!rank) {
8268e2fed03SBarry Smith     MPI_Status status;
8278e2fed03SBarry Smith     ierr = PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,1);CHKERRQ(ierr);
8288e2fed03SBarry Smith     for (i=1; i<size; i++) {
829b021249fSBarry Smith       ierr = MPI_Recv(&rnz,1,MPI_INT,i,tag,mat->comm,&status);CHKERRQ(ierr);
830b021249fSBarry Smith       if (rnz > nzmax) SETERRQ2(PETSC_ERR_LIB,"Internal PETSc error: nz = %d nzmax = %d",nz,nzmax);
831b021249fSBarry Smith       ierr = MPI_Recv(column_indices,rnz,MPI_INT,i,tag,mat->comm,&status);CHKERRQ(ierr);
832b021249fSBarry Smith       ierr = PetscBinaryWrite(fd,column_indices,rnz,PETSC_INT,1);CHKERRQ(ierr);
8338e2fed03SBarry Smith     }
8348e2fed03SBarry Smith   } else {
8358e2fed03SBarry Smith     ierr = MPI_Send(&nz,1,MPI_INT,0,tag,mat->comm);CHKERRQ(ierr);
8368e2fed03SBarry Smith     ierr = MPI_Send(column_indices,nz,MPI_INT,0,tag,mat->comm);CHKERRQ(ierr);
8378e2fed03SBarry Smith   }
8388e2fed03SBarry Smith   ierr = PetscFree(column_indices);CHKERRQ(ierr);
8398e2fed03SBarry Smith 
8408e2fed03SBarry Smith   /* load up the local column values */
8418e2fed03SBarry Smith   ierr = PetscMalloc((nzmax+1)*sizeof(PetscScalar),&column_values);CHKERRQ(ierr);
8428e2fed03SBarry Smith   cnt  = 0;
8438e2fed03SBarry Smith   for (i=0; i<mat->m; i++) {
8448e2fed03SBarry Smith     for (j=B->i[i]; j<B->i[i+1]; j++) {
8458e2fed03SBarry Smith       if ( garray[B->j[j]] > cstart) break;
8468e2fed03SBarry Smith       column_values[cnt++] = B->a[j];
8478e2fed03SBarry Smith     }
8488e2fed03SBarry Smith     for (k=A->i[i]; k<A->i[i+1]; k++) {
8498e2fed03SBarry Smith       column_values[cnt++] = A->a[k];
8508e2fed03SBarry Smith     }
8518e2fed03SBarry Smith     for (; j<B->i[i+1]; j++) {
8528e2fed03SBarry Smith       column_values[cnt++] = B->a[j];
8538e2fed03SBarry Smith     }
8548e2fed03SBarry Smith   }
8558e2fed03SBarry Smith   if (cnt != A->nz + B->nz) SETERRQ2(1,"Internal PETSc error: cnt = %d nz = %d",cnt,A->nz+B->nz);
8568e2fed03SBarry Smith 
8578e2fed03SBarry Smith   /* store the column values to the file */
858958c9bccSBarry Smith   if (!rank) {
8598e2fed03SBarry Smith     MPI_Status status;
8608e2fed03SBarry Smith     ierr = PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,1);CHKERRQ(ierr);
8618e2fed03SBarry Smith     for (i=1; i<size; i++) {
862b021249fSBarry Smith       ierr = MPI_Recv(&rnz,1,MPI_INT,i,tag,mat->comm,&status);CHKERRQ(ierr);
863b021249fSBarry Smith       if (rnz > nzmax) SETERRQ2(PETSC_ERR_LIB,"Internal PETSc error: nz = %d nzmax = %d",nz,nzmax);
864b021249fSBarry Smith       ierr = MPI_Recv(column_values,rnz,MPIU_SCALAR,i,tag,mat->comm,&status);CHKERRQ(ierr);
865b021249fSBarry Smith       ierr = PetscBinaryWrite(fd,column_values,rnz,PETSC_SCALAR,1);CHKERRQ(ierr);
8668e2fed03SBarry Smith     }
8678e2fed03SBarry Smith   } else {
8688e2fed03SBarry Smith     ierr = MPI_Send(&nz,1,MPI_INT,0,tag,mat->comm);CHKERRQ(ierr);
8698e2fed03SBarry Smith     ierr = MPI_Send(column_values,nz,MPIU_SCALAR,0,tag,mat->comm);CHKERRQ(ierr);
8708e2fed03SBarry Smith   }
8718e2fed03SBarry Smith   ierr = PetscFree(column_values);CHKERRQ(ierr);
8728e2fed03SBarry Smith   PetscFunctionReturn(0);
8738e2fed03SBarry Smith }
8748e2fed03SBarry Smith 
8758e2fed03SBarry Smith #undef __FUNCT__
8764a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ_ASCIIorDraworSocket"
877dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
878416022c9SBarry Smith {
87944a69424SLois Curfman McInnes   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*)mat->data;
880dfbe8321SBarry Smith   PetscErrorCode ierr;
881dfbe8321SBarry Smith   int               rank = aij->rank,size = aij->size;
88232077d6dSBarry Smith   PetscTruth        isdraw,iascii,flg,isbinary;
883b0a32e0cSBarry Smith   PetscViewer       sviewer;
884f3ef73ceSBarry Smith   PetscViewerFormat format;
885416022c9SBarry Smith 
8863a40ed3dSBarry Smith   PetscFunctionBegin;
887fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
88832077d6dSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
8898e2fed03SBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
89032077d6dSBarry Smith   if (iascii) {
891b0a32e0cSBarry Smith     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
892456192e2SBarry Smith     if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
8934e220ebcSLois Curfman McInnes       MatInfo info;
8941dab6e02SBarry Smith       ierr = MPI_Comm_rank(mat->comm,&rank);CHKERRQ(ierr);
895888f2ed8SSatish Balay       ierr = MatGetInfo(mat,MAT_LOCAL,&info);CHKERRQ(ierr);
896b0a32e0cSBarry Smith       ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_no_inode",&flg);CHKERRQ(ierr);
8976831982aSBarry Smith       if (flg) {
898b0a32e0cSBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %d nz %d nz alloced %d mem %d, not using I-node routines\n",
899273d9f13SBarry Smith 					      rank,mat->m,(int)info.nz_used,(int)info.nz_allocated,(int)info.memory);CHKERRQ(ierr);
9006831982aSBarry Smith       } else {
901b0a32e0cSBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %d nz %d nz alloced %d mem %d, using I-node routines\n",
902273d9f13SBarry Smith 		    rank,mat->m,(int)info.nz_used,(int)info.nz_allocated,(int)info.memory);CHKERRQ(ierr);
9036831982aSBarry Smith       }
904888f2ed8SSatish Balay       ierr = MatGetInfo(aij->A,MAT_LOCAL,&info);CHKERRQ(ierr);
905b0a32e0cSBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %d \n",rank,(int)info.nz_used);CHKERRQ(ierr);
906888f2ed8SSatish Balay       ierr = MatGetInfo(aij->B,MAT_LOCAL,&info);CHKERRQ(ierr);
907b0a32e0cSBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %d \n",rank,(int)info.nz_used);CHKERRQ(ierr);
908b0a32e0cSBarry Smith       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
909a40aa06bSLois Curfman McInnes       ierr = VecScatterView(aij->Mvctx,viewer);CHKERRQ(ierr);
9103a40ed3dSBarry Smith       PetscFunctionReturn(0);
911fb9695e5SSatish Balay     } else if (format == PETSC_VIEWER_ASCII_INFO) {
9123a40ed3dSBarry Smith       PetscFunctionReturn(0);
9134aedb280SBarry Smith     } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
9144aedb280SBarry Smith       PetscFunctionReturn(0);
91508480c60SBarry Smith     }
9168e2fed03SBarry Smith   } else if (isbinary) {
9178e2fed03SBarry Smith     if (size == 1) {
9188e2fed03SBarry Smith       ierr = PetscObjectSetName((PetscObject)aij->A,mat->name);CHKERRQ(ierr);
9198e2fed03SBarry Smith       ierr = MatView(aij->A,viewer);CHKERRQ(ierr);
9208e2fed03SBarry Smith     } else {
9218e2fed03SBarry Smith       ierr = MatView_MPIAIJ_Binary(mat,viewer);CHKERRQ(ierr);
9228e2fed03SBarry Smith     }
9238e2fed03SBarry Smith     PetscFunctionReturn(0);
9240f5bd95cSBarry Smith   } else if (isdraw) {
925b0a32e0cSBarry Smith     PetscDraw  draw;
92619bcc07fSBarry Smith     PetscTruth isnull;
927b0a32e0cSBarry Smith     ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
928b0a32e0cSBarry Smith     ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
92919bcc07fSBarry Smith   }
93019bcc07fSBarry Smith 
93117699dbbSLois Curfman McInnes   if (size == 1) {
932e36acaf3SBarry Smith     ierr = PetscObjectSetName((PetscObject)aij->A,mat->name);CHKERRQ(ierr);
93378b31e54SBarry Smith     ierr = MatView(aij->A,viewer);CHKERRQ(ierr);
9343a40ed3dSBarry Smith   } else {
93595373324SBarry Smith     /* assemble the entire matrix onto first processor. */
93695373324SBarry Smith     Mat         A;
937ec8511deSBarry Smith     Mat_SeqAIJ *Aloc;
938273d9f13SBarry Smith     int         M = mat->M,N = mat->N,m,*ai,*aj,row,*cols,i,*ct;
93987828ca2SBarry Smith     PetscScalar *a;
9402ee70a88SLois Curfman McInnes 
94117699dbbSLois Curfman McInnes     if (!rank) {
942f204ca49SKris Buschelman       ierr = MatCreate(mat->comm,M,N,M,N,&A);CHKERRQ(ierr);
9433a40ed3dSBarry Smith     } else {
944f204ca49SKris Buschelman       ierr = MatCreate(mat->comm,0,0,M,N,&A);CHKERRQ(ierr);
94595373324SBarry Smith     }
946f204ca49SKris Buschelman     /* This is just a temporary matrix, so explicitly using MATMPIAIJ is probably best */
947f204ca49SKris Buschelman     ierr = MatSetType(A,MATMPIAIJ);CHKERRQ(ierr);
948f204ca49SKris Buschelman     ierr = MatMPIAIJSetPreallocation(A,0,PETSC_NULL,0,PETSC_NULL);CHKERRQ(ierr);
949b0a32e0cSBarry Smith     PetscLogObjectParent(mat,A);
950416022c9SBarry Smith 
95195373324SBarry Smith     /* copy over the A part */
952ec8511deSBarry Smith     Aloc = (Mat_SeqAIJ*)aij->A->data;
953273d9f13SBarry Smith     m = aij->A->m; ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
95495373324SBarry Smith     row = aij->rstart;
955bfec09a0SHong Zhang     for (i=0; i<ai[m]; i++) {aj[i] += aij->cstart ;}
95695373324SBarry Smith     for (i=0; i<m; i++) {
957416022c9SBarry Smith       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);CHKERRQ(ierr);
95895373324SBarry Smith       row++; a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
95995373324SBarry Smith     }
9602ee70a88SLois Curfman McInnes     aj = Aloc->j;
961bfec09a0SHong Zhang     for (i=0; i<ai[m]; i++) {aj[i] -= aij->cstart;}
96295373324SBarry Smith 
96395373324SBarry Smith     /* copy over the B part */
964ec8511deSBarry Smith     Aloc = (Mat_SeqAIJ*)aij->B->data;
965273d9f13SBarry Smith     m    = aij->B->m;  ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
96695373324SBarry Smith     row  = aij->rstart;
967b0a32e0cSBarry Smith     ierr = PetscMalloc((ai[m]+1)*sizeof(int),&cols);CHKERRQ(ierr);
968b0a32e0cSBarry Smith     ct   = cols;
969bfec09a0SHong Zhang     for (i=0; i<ai[m]; i++) {cols[i] = aij->garray[aj[i]];}
97095373324SBarry Smith     for (i=0; i<m; i++) {
971416022c9SBarry Smith       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);CHKERRQ(ierr);
97295373324SBarry Smith       row++; a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
97395373324SBarry Smith     }
974606d414cSSatish Balay     ierr = PetscFree(ct);CHKERRQ(ierr);
9756d4a8577SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9766d4a8577SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
97755843e3eSBarry Smith     /*
97855843e3eSBarry Smith        Everyone has to call to draw the matrix since the graphics waits are
979b0a32e0cSBarry Smith        synchronized across all processors that share the PetscDraw object
98055843e3eSBarry Smith     */
981b0a32e0cSBarry Smith     ierr = PetscViewerGetSingleton(viewer,&sviewer);CHKERRQ(ierr);
982e03a110bSBarry Smith     if (!rank) {
983e36acaf3SBarry Smith       ierr = PetscObjectSetName((PetscObject)((Mat_MPIAIJ*)(A->data))->A,mat->name);CHKERRQ(ierr);
9846831982aSBarry Smith       ierr = MatView(((Mat_MPIAIJ*)(A->data))->A,sviewer);CHKERRQ(ierr);
98595373324SBarry Smith     }
986b0a32e0cSBarry Smith     ierr = PetscViewerRestoreSingleton(viewer,&sviewer);CHKERRQ(ierr);
98778b31e54SBarry Smith     ierr = MatDestroy(A);CHKERRQ(ierr);
98895373324SBarry Smith   }
9893a40ed3dSBarry Smith   PetscFunctionReturn(0);
9901eb62cbbSBarry Smith }
9911eb62cbbSBarry Smith 
9924a2ae208SSatish Balay #undef __FUNCT__
9934a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ"
994dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ(Mat mat,PetscViewer viewer)
995416022c9SBarry Smith {
996dfbe8321SBarry Smith   PetscErrorCode ierr;
99732077d6dSBarry Smith   PetscTruth iascii,isdraw,issocket,isbinary;
998416022c9SBarry Smith 
9993a40ed3dSBarry Smith   PetscFunctionBegin;
100032077d6dSBarry Smith   ierr  = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
1001fb9695e5SSatish Balay   ierr  = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
1002fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
1003b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_SOCKET,&issocket);CHKERRQ(ierr);
100432077d6dSBarry Smith   if (iascii || isdraw || isbinary || issocket) {
10057b2a1423SBarry Smith     ierr = MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer);CHKERRQ(ierr);
10065cd90555SBarry Smith   } else {
100729bbc08cSBarry Smith     SETERRQ1(1,"Viewer type %s not supported by MPIAIJ matrices",((PetscObject)viewer)->type_name);
1008416022c9SBarry Smith   }
10093a40ed3dSBarry Smith   PetscFunctionReturn(0);
1010416022c9SBarry Smith }
1011416022c9SBarry Smith 
10121eb62cbbSBarry Smith 
1013c14dc6b6SHong Zhang 
10144a2ae208SSatish Balay #undef __FUNCT__
10154a2ae208SSatish Balay #define __FUNCT__ "MatRelax_MPIAIJ"
1016dfbe8321SBarry Smith PetscErrorCode MatRelax_MPIAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,int its,int lits,Vec xx)
10178a729477SBarry Smith {
101844a69424SLois Curfman McInnes   Mat_MPIAIJ   *mat = (Mat_MPIAIJ*)matin->data;
1019dfbe8321SBarry Smith   PetscErrorCode ierr;
1020c14dc6b6SHong Zhang   Vec          bb1;
1021a6c309e7SHong Zhang   PetscScalar  mone=-1.0;
10228a729477SBarry Smith 
10233a40ed3dSBarry Smith   PetscFunctionBegin;
102491723122SBarry Smith   if (its <= 0 || lits <= 0) SETERRQ2(PETSC_ERR_ARG_WRONG,"Relaxation requires global its %d and local its %d both positive",its,lits);
1025c14dc6b6SHong Zhang 
1026c14dc6b6SHong Zhang   ierr = VecDuplicate(bb,&bb1);CHKERRQ(ierr);
10272798e883SHong Zhang 
1028c16cb8f2SBarry Smith   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){
1029da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
1030bd3bf7d3SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,lits,xx);CHKERRQ(ierr);
10312798e883SHong Zhang       its--;
1032da3a660dSBarry Smith     }
10332798e883SHong Zhang 
10342798e883SHong Zhang     while (its--) {
10353a40ed3dSBarry Smith       ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10363a40ed3dSBarry Smith       ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10372798e883SHong Zhang 
1038c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
1039c14dc6b6SHong Zhang       ierr = VecScale(&mone,mat->lvec);CHKERRQ(ierr);
1040c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
10412798e883SHong Zhang 
1042c14dc6b6SHong Zhang       /* local sweep */
1043bd3bf7d3SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,lits,xx);
1044c14dc6b6SHong Zhang       CHKERRQ(ierr);
10452798e883SHong Zhang     }
10463a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_FORWARD_SWEEP){
1047da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
1048c14dc6b6SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,PETSC_NULL,xx);CHKERRQ(ierr);
10492798e883SHong Zhang       its--;
1050da3a660dSBarry Smith     }
10512798e883SHong Zhang     while (its--) {
10523a40ed3dSBarry Smith       ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10533a40ed3dSBarry Smith       ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10542798e883SHong Zhang 
1055c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
1056c14dc6b6SHong Zhang       ierr = VecScale(&mone,mat->lvec);CHKERRQ(ierr);
1057c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
1058c14dc6b6SHong Zhang 
1059c14dc6b6SHong Zhang       /* local sweep */
1060a6c309e7SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,PETSC_NULL,xx);
1061c14dc6b6SHong Zhang       CHKERRQ(ierr);
10622798e883SHong Zhang     }
10633a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_BACKWARD_SWEEP){
1064da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
1065c14dc6b6SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,PETSC_NULL,xx);CHKERRQ(ierr);
10662798e883SHong Zhang       its--;
1067da3a660dSBarry Smith     }
10682798e883SHong Zhang     while (its--) {
10692e8a6d31SBarry Smith       ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10702e8a6d31SBarry Smith       ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10712798e883SHong Zhang 
1072c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
1073c14dc6b6SHong Zhang       ierr = VecScale(&mone,mat->lvec);CHKERRQ(ierr);
1074c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
10752798e883SHong Zhang 
1076c14dc6b6SHong Zhang       /* local sweep */
1077a6c309e7SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,PETSC_NULL,xx);
1078c14dc6b6SHong Zhang       CHKERRQ(ierr);
10792798e883SHong Zhang     }
10803a40ed3dSBarry Smith   } else {
108129bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"Parallel SOR not supported");
1082c16cb8f2SBarry Smith   }
1083c14dc6b6SHong Zhang 
1084c14dc6b6SHong Zhang   ierr = VecDestroy(bb1);CHKERRQ(ierr);
10853a40ed3dSBarry Smith   PetscFunctionReturn(0);
10868a729477SBarry Smith }
1087a66be287SLois Curfman McInnes 
10884a2ae208SSatish Balay #undef __FUNCT__
10894a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_MPIAIJ"
1090dfbe8321SBarry Smith PetscErrorCode MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info)
1091a66be287SLois Curfman McInnes {
1092a66be287SLois Curfman McInnes   Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data;
1093a66be287SLois Curfman McInnes   Mat        A = mat->A,B = mat->B;
1094dfbe8321SBarry Smith   PetscErrorCode ierr;
1095329f5518SBarry Smith   PetscReal  isend[5],irecv[5];
1096a66be287SLois Curfman McInnes 
10973a40ed3dSBarry Smith   PetscFunctionBegin;
10984e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
10994e220ebcSLois Curfman McInnes   ierr = MatGetInfo(A,MAT_LOCAL,info);CHKERRQ(ierr);
11004e220ebcSLois Curfman McInnes   isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
11014e220ebcSLois Curfman McInnes   isend[3] = info->memory;  isend[4] = info->mallocs;
11024e220ebcSLois Curfman McInnes   ierr = MatGetInfo(B,MAT_LOCAL,info);CHKERRQ(ierr);
11034e220ebcSLois Curfman McInnes   isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
11044e220ebcSLois Curfman McInnes   isend[3] += info->memory;  isend[4] += info->mallocs;
1105a66be287SLois Curfman McInnes   if (flag == MAT_LOCAL) {
11064e220ebcSLois Curfman McInnes     info->nz_used      = isend[0];
11074e220ebcSLois Curfman McInnes     info->nz_allocated = isend[1];
11084e220ebcSLois Curfman McInnes     info->nz_unneeded  = isend[2];
11094e220ebcSLois Curfman McInnes     info->memory       = isend[3];
11104e220ebcSLois Curfman McInnes     info->mallocs      = isend[4];
1111a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_MAX) {
1112d7d1e502SBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_MAX,matin->comm);CHKERRQ(ierr);
11134e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
11144e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
11154e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
11164e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
11174e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1118a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_SUM) {
1119d7d1e502SBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_SUM,matin->comm);CHKERRQ(ierr);
11204e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
11214e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
11224e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
11234e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
11244e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1125a66be287SLois Curfman McInnes   }
11264e220ebcSLois Curfman McInnes   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
11274e220ebcSLois Curfman McInnes   info->fill_ratio_needed = 0;
11284e220ebcSLois Curfman McInnes   info->factor_mallocs    = 0;
1129273d9f13SBarry Smith   info->rows_global       = (double)matin->M;
1130273d9f13SBarry Smith   info->columns_global    = (double)matin->N;
1131273d9f13SBarry Smith   info->rows_local        = (double)matin->m;
1132273d9f13SBarry Smith   info->columns_local     = (double)matin->N;
11334e220ebcSLois Curfman McInnes 
11343a40ed3dSBarry Smith   PetscFunctionReturn(0);
1135a66be287SLois Curfman McInnes }
1136a66be287SLois Curfman McInnes 
11374a2ae208SSatish Balay #undef __FUNCT__
11384a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_MPIAIJ"
1139dfbe8321SBarry Smith PetscErrorCode MatSetOption_MPIAIJ(Mat A,MatOption op)
1140c74985f6SBarry Smith {
1141c0bbcb79SLois Curfman McInnes   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
1142dfbe8321SBarry Smith   PetscErrorCode ierr;
1143c74985f6SBarry Smith 
11443a40ed3dSBarry Smith   PetscFunctionBegin;
114512c028f9SKris Buschelman   switch (op) {
114612c028f9SKris Buschelman   case MAT_NO_NEW_NONZERO_LOCATIONS:
114712c028f9SKris Buschelman   case MAT_YES_NEW_NONZERO_LOCATIONS:
114812c028f9SKris Buschelman   case MAT_COLUMNS_UNSORTED:
114912c028f9SKris Buschelman   case MAT_COLUMNS_SORTED:
115012c028f9SKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
115112c028f9SKris Buschelman   case MAT_KEEP_ZEROED_ROWS:
115212c028f9SKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
115312c028f9SKris Buschelman   case MAT_USE_INODES:
115412c028f9SKris Buschelman   case MAT_DO_NOT_USE_INODES:
115512c028f9SKris Buschelman   case MAT_IGNORE_ZERO_ENTRIES:
11561dee9f54SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
11571dee9f54SBarry Smith     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
115812c028f9SKris Buschelman     break;
115912c028f9SKris Buschelman   case MAT_ROW_ORIENTED:
11607c922b88SBarry Smith     a->roworiented = PETSC_TRUE;
11611dee9f54SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
11621dee9f54SBarry Smith     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
116312c028f9SKris Buschelman     break;
116412c028f9SKris Buschelman   case MAT_ROWS_SORTED:
116512c028f9SKris Buschelman   case MAT_ROWS_UNSORTED:
116612c028f9SKris Buschelman   case MAT_YES_NEW_DIAGONALS:
1167b0a32e0cSBarry Smith     PetscLogInfo(A,"MatSetOption_MPIAIJ:Option ignored\n");
116812c028f9SKris Buschelman     break;
116912c028f9SKris Buschelman   case MAT_COLUMN_ORIENTED:
11707c922b88SBarry Smith     a->roworiented = PETSC_FALSE;
11711dee9f54SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
11721dee9f54SBarry Smith     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
117312c028f9SKris Buschelman     break;
117412c028f9SKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
11757c922b88SBarry Smith     a->donotstash = PETSC_TRUE;
117612c028f9SKris Buschelman     break;
117712c028f9SKris Buschelman   case MAT_NO_NEW_DIAGONALS:
117829bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"MAT_NO_NEW_DIAGONALS");
117977e54ba9SKris Buschelman   case MAT_SYMMETRIC:
118077e54ba9SKris Buschelman   case MAT_STRUCTURALLY_SYMMETRIC:
1181bf108f30SBarry Smith   case MAT_HERMITIAN:
1182bf108f30SBarry Smith   case MAT_SYMMETRY_ETERNAL:
1183bf108f30SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
1184bf108f30SBarry Smith     break;
11859a4540c5SBarry Smith   case MAT_NOT_SYMMETRIC:
11869a4540c5SBarry Smith   case MAT_NOT_STRUCTURALLY_SYMMETRIC:
11879a4540c5SBarry Smith   case MAT_NOT_HERMITIAN:
11889a4540c5SBarry Smith   case MAT_NOT_SYMMETRY_ETERNAL:
118977e54ba9SKris Buschelman     break;
119012c028f9SKris Buschelman   default:
119129bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"unknown option");
11923a40ed3dSBarry Smith   }
11933a40ed3dSBarry Smith   PetscFunctionReturn(0);
1194c74985f6SBarry Smith }
1195c74985f6SBarry Smith 
11964a2ae208SSatish Balay #undef __FUNCT__
11974a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_MPIAIJ"
1198dfbe8321SBarry Smith PetscErrorCode MatGetRow_MPIAIJ(Mat matin,int row,int *nz,int **idx,PetscScalar **v)
119939e00950SLois Curfman McInnes {
1200154123eaSLois Curfman McInnes   Mat_MPIAIJ   *mat = (Mat_MPIAIJ*)matin->data;
120187828ca2SBarry Smith   PetscScalar  *vworkA,*vworkB,**pvA,**pvB,*v_p;
1202*6849ba73SBarry Smith   PetscErrorCode ierr;
1203*6849ba73SBarry Smith   int          i,*cworkA,*cworkB,**pcA,**pcB,cstart = mat->cstart;
1204154123eaSLois Curfman McInnes   int          nztot,nzA,nzB,lrow,rstart = mat->rstart,rend = mat->rend;
120570f0671dSBarry Smith   int          *cmap,*idx_p;
120639e00950SLois Curfman McInnes 
12073a40ed3dSBarry Smith   PetscFunctionBegin;
120829bbc08cSBarry Smith   if (mat->getrowactive == PETSC_TRUE) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Already active");
12097a0afa10SBarry Smith   mat->getrowactive = PETSC_TRUE;
12107a0afa10SBarry Smith 
121170f0671dSBarry Smith   if (!mat->rowvalues && (idx || v)) {
12127a0afa10SBarry Smith     /*
12137a0afa10SBarry Smith         allocate enough space to hold information from the longest row.
12147a0afa10SBarry Smith     */
12157a0afa10SBarry Smith     Mat_SeqAIJ *Aa = (Mat_SeqAIJ*)mat->A->data,*Ba = (Mat_SeqAIJ*)mat->B->data;
1216273d9f13SBarry Smith     int     max = 1,tmp;
1217273d9f13SBarry Smith     for (i=0; i<matin->m; i++) {
12187a0afa10SBarry Smith       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
12197a0afa10SBarry Smith       if (max < tmp) { max = tmp; }
12207a0afa10SBarry Smith     }
122187828ca2SBarry Smith     ierr = PetscMalloc(max*(sizeof(int)+sizeof(PetscScalar)),&mat->rowvalues);CHKERRQ(ierr);
12227a0afa10SBarry Smith     mat->rowindices = (int*)(mat->rowvalues + max);
12237a0afa10SBarry Smith   }
12247a0afa10SBarry Smith 
122529bbc08cSBarry Smith   if (row < rstart || row >= rend) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Only local rows")
1226abc0e9e4SLois Curfman McInnes   lrow = row - rstart;
122739e00950SLois Curfman McInnes 
1228154123eaSLois Curfman McInnes   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1229154123eaSLois Curfman McInnes   if (!v)   {pvA = 0; pvB = 0;}
1230154123eaSLois Curfman McInnes   if (!idx) {pcA = 0; if (!v) pcB = 0;}
1231f830108cSBarry Smith   ierr = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1232f830108cSBarry Smith   ierr = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
1233154123eaSLois Curfman McInnes   nztot = nzA + nzB;
1234154123eaSLois Curfman McInnes 
123570f0671dSBarry Smith   cmap  = mat->garray;
1236154123eaSLois Curfman McInnes   if (v  || idx) {
1237154123eaSLois Curfman McInnes     if (nztot) {
1238154123eaSLois Curfman McInnes       /* Sort by increasing column numbers, assuming A and B already sorted */
123970f0671dSBarry Smith       int imark = -1;
1240154123eaSLois Curfman McInnes       if (v) {
124170f0671dSBarry Smith         *v = v_p = mat->rowvalues;
124239e00950SLois Curfman McInnes         for (i=0; i<nzB; i++) {
124370f0671dSBarry Smith           if (cmap[cworkB[i]] < cstart)   v_p[i] = vworkB[i];
1244154123eaSLois Curfman McInnes           else break;
1245154123eaSLois Curfman McInnes         }
1246154123eaSLois Curfman McInnes         imark = i;
124770f0671dSBarry Smith         for (i=0; i<nzA; i++)     v_p[imark+i] = vworkA[i];
124870f0671dSBarry Smith         for (i=imark; i<nzB; i++) v_p[nzA+i]   = vworkB[i];
1249154123eaSLois Curfman McInnes       }
1250154123eaSLois Curfman McInnes       if (idx) {
125170f0671dSBarry Smith         *idx = idx_p = mat->rowindices;
125270f0671dSBarry Smith         if (imark > -1) {
125370f0671dSBarry Smith           for (i=0; i<imark; i++) {
125470f0671dSBarry Smith             idx_p[i] = cmap[cworkB[i]];
125570f0671dSBarry Smith           }
125670f0671dSBarry Smith         } else {
1257154123eaSLois Curfman McInnes           for (i=0; i<nzB; i++) {
125870f0671dSBarry Smith             if (cmap[cworkB[i]] < cstart)   idx_p[i] = cmap[cworkB[i]];
1259154123eaSLois Curfman McInnes             else break;
1260154123eaSLois Curfman McInnes           }
1261154123eaSLois Curfman McInnes           imark = i;
126270f0671dSBarry Smith         }
126370f0671dSBarry Smith         for (i=0; i<nzA; i++)     idx_p[imark+i] = cstart + cworkA[i];
126470f0671dSBarry Smith         for (i=imark; i<nzB; i++) idx_p[nzA+i]   = cmap[cworkB[i]];
126539e00950SLois Curfman McInnes       }
12663f97c4b0SBarry Smith     } else {
12671ca473b0SSatish Balay       if (idx) *idx = 0;
12681ca473b0SSatish Balay       if (v)   *v   = 0;
12691ca473b0SSatish Balay     }
1270154123eaSLois Curfman McInnes   }
127139e00950SLois Curfman McInnes   *nz = nztot;
1272f830108cSBarry Smith   ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1273f830108cSBarry Smith   ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
12743a40ed3dSBarry Smith   PetscFunctionReturn(0);
127539e00950SLois Curfman McInnes }
127639e00950SLois Curfman McInnes 
12774a2ae208SSatish Balay #undef __FUNCT__
12784a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_MPIAIJ"
1279dfbe8321SBarry Smith PetscErrorCode MatRestoreRow_MPIAIJ(Mat mat,int row,int *nz,int **idx,PetscScalar **v)
128039e00950SLois Curfman McInnes {
12817a0afa10SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
12823a40ed3dSBarry Smith 
12833a40ed3dSBarry Smith   PetscFunctionBegin;
12847a0afa10SBarry Smith   if (aij->getrowactive == PETSC_FALSE) {
128529bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"MatGetRow not called");
12867a0afa10SBarry Smith   }
12877a0afa10SBarry Smith   aij->getrowactive = PETSC_FALSE;
12883a40ed3dSBarry Smith   PetscFunctionReturn(0);
128939e00950SLois Curfman McInnes }
129039e00950SLois Curfman McInnes 
12914a2ae208SSatish Balay #undef __FUNCT__
12924a2ae208SSatish Balay #define __FUNCT__ "MatNorm_MPIAIJ"
1293dfbe8321SBarry Smith PetscErrorCode MatNorm_MPIAIJ(Mat mat,NormType type,PetscReal *norm)
1294855ac2c5SLois Curfman McInnes {
1295855ac2c5SLois Curfman McInnes   Mat_MPIAIJ   *aij = (Mat_MPIAIJ*)mat->data;
1296ec8511deSBarry Smith   Mat_SeqAIJ   *amat = (Mat_SeqAIJ*)aij->A->data,*bmat = (Mat_SeqAIJ*)aij->B->data;
1297dfbe8321SBarry Smith   PetscErrorCode ierr;
1298dfbe8321SBarry Smith   int          i,j,cstart = aij->cstart;
1299329f5518SBarry Smith   PetscReal    sum = 0.0;
130087828ca2SBarry Smith   PetscScalar  *v;
130104ca555eSLois Curfman McInnes 
13023a40ed3dSBarry Smith   PetscFunctionBegin;
130317699dbbSLois Curfman McInnes   if (aij->size == 1) {
130414183eadSLois Curfman McInnes     ierr =  MatNorm(aij->A,type,norm);CHKERRQ(ierr);
130537fa93a5SLois Curfman McInnes   } else {
130604ca555eSLois Curfman McInnes     if (type == NORM_FROBENIUS) {
130704ca555eSLois Curfman McInnes       v = amat->a;
130804ca555eSLois Curfman McInnes       for (i=0; i<amat->nz; i++) {
1309aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
1310329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
131104ca555eSLois Curfman McInnes #else
131204ca555eSLois Curfman McInnes         sum += (*v)*(*v); v++;
131304ca555eSLois Curfman McInnes #endif
131404ca555eSLois Curfman McInnes       }
131504ca555eSLois Curfman McInnes       v = bmat->a;
131604ca555eSLois Curfman McInnes       for (i=0; i<bmat->nz; i++) {
1317aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
1318329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
131904ca555eSLois Curfman McInnes #else
132004ca555eSLois Curfman McInnes         sum += (*v)*(*v); v++;
132104ca555eSLois Curfman McInnes #endif
132204ca555eSLois Curfman McInnes       }
1323d7d1e502SBarry Smith       ierr = MPI_Allreduce(&sum,norm,1,MPIU_REAL,MPI_SUM,mat->comm);CHKERRQ(ierr);
132404ca555eSLois Curfman McInnes       *norm = sqrt(*norm);
13253a40ed3dSBarry Smith     } else if (type == NORM_1) { /* max column norm */
1326329f5518SBarry Smith       PetscReal *tmp,*tmp2;
132704ca555eSLois Curfman McInnes       int    *jj,*garray = aij->garray;
1328b0a32e0cSBarry Smith       ierr = PetscMalloc((mat->N+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1329b0a32e0cSBarry Smith       ierr = PetscMalloc((mat->N+1)*sizeof(PetscReal),&tmp2);CHKERRQ(ierr);
1330273d9f13SBarry Smith       ierr = PetscMemzero(tmp,mat->N*sizeof(PetscReal));CHKERRQ(ierr);
133104ca555eSLois Curfman McInnes       *norm = 0.0;
133204ca555eSLois Curfman McInnes       v = amat->a; jj = amat->j;
133304ca555eSLois Curfman McInnes       for (j=0; j<amat->nz; j++) {
1334bfec09a0SHong Zhang         tmp[cstart + *jj++ ] += PetscAbsScalar(*v);  v++;
133504ca555eSLois Curfman McInnes       }
133604ca555eSLois Curfman McInnes       v = bmat->a; jj = bmat->j;
133704ca555eSLois Curfman McInnes       for (j=0; j<bmat->nz; j++) {
1338bfec09a0SHong Zhang         tmp[garray[*jj++]] += PetscAbsScalar(*v); v++;
133904ca555eSLois Curfman McInnes       }
1340d7d1e502SBarry Smith       ierr = MPI_Allreduce(tmp,tmp2,mat->N,MPIU_REAL,MPI_SUM,mat->comm);CHKERRQ(ierr);
1341273d9f13SBarry Smith       for (j=0; j<mat->N; j++) {
134204ca555eSLois Curfman McInnes         if (tmp2[j] > *norm) *norm = tmp2[j];
134304ca555eSLois Curfman McInnes       }
1344606d414cSSatish Balay       ierr = PetscFree(tmp);CHKERRQ(ierr);
1345606d414cSSatish Balay       ierr = PetscFree(tmp2);CHKERRQ(ierr);
13463a40ed3dSBarry Smith     } else if (type == NORM_INFINITY) { /* max row norm */
1347329f5518SBarry Smith       PetscReal ntemp = 0.0;
1348273d9f13SBarry Smith       for (j=0; j<aij->A->m; j++) {
1349bfec09a0SHong Zhang         v = amat->a + amat->i[j];
135004ca555eSLois Curfman McInnes         sum = 0.0;
135104ca555eSLois Curfman McInnes         for (i=0; i<amat->i[j+1]-amat->i[j]; i++) {
1352cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
135304ca555eSLois Curfman McInnes         }
1354bfec09a0SHong Zhang         v = bmat->a + bmat->i[j];
135504ca555eSLois Curfman McInnes         for (i=0; i<bmat->i[j+1]-bmat->i[j]; i++) {
1356cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
135704ca555eSLois Curfman McInnes         }
1358515d9167SLois Curfman McInnes         if (sum > ntemp) ntemp = sum;
135904ca555eSLois Curfman McInnes       }
1360d7d1e502SBarry Smith       ierr = MPI_Allreduce(&ntemp,norm,1,MPIU_REAL,MPI_MAX,mat->comm);CHKERRQ(ierr);
1361ca161407SBarry Smith     } else {
136229bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"No support for two norm");
136304ca555eSLois Curfman McInnes     }
136437fa93a5SLois Curfman McInnes   }
13653a40ed3dSBarry Smith   PetscFunctionReturn(0);
1366855ac2c5SLois Curfman McInnes }
1367855ac2c5SLois Curfman McInnes 
13684a2ae208SSatish Balay #undef __FUNCT__
13694a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_MPIAIJ"
1370dfbe8321SBarry Smith PetscErrorCode MatTranspose_MPIAIJ(Mat A,Mat *matout)
1371b7c46309SBarry Smith {
1372b7c46309SBarry Smith   Mat_MPIAIJ   *a = (Mat_MPIAIJ*)A->data;
1373dbb450caSBarry Smith   Mat_SeqAIJ   *Aloc = (Mat_SeqAIJ*)a->A->data;
1374dfbe8321SBarry Smith   PetscErrorCode ierr;
1375273d9f13SBarry Smith   int          M = A->M,N = A->N,m,*ai,*aj,row,*cols,i,*ct;
13763a40ed3dSBarry Smith   Mat          B;
137787828ca2SBarry Smith   PetscScalar  *array;
1378b7c46309SBarry Smith 
13793a40ed3dSBarry Smith   PetscFunctionBegin;
13807c922b88SBarry Smith   if (!matout && M != N) {
138129bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1382d4bb536fSBarry Smith   }
1383d4bb536fSBarry Smith 
1384f204ca49SKris Buschelman   ierr = MatCreate(A->comm,A->n,A->m,N,M,&B);CHKERRQ(ierr);
1385f204ca49SKris Buschelman   ierr = MatSetType(B,A->type_name);CHKERRQ(ierr);
1386f204ca49SKris Buschelman   ierr = MatMPIAIJSetPreallocation(B,0,PETSC_NULL,0,PETSC_NULL);CHKERRQ(ierr);
1387b7c46309SBarry Smith 
1388b7c46309SBarry Smith   /* copy over the A part */
1389ec8511deSBarry Smith   Aloc = (Mat_SeqAIJ*)a->A->data;
1390273d9f13SBarry Smith   m = a->A->m; ai = Aloc->i; aj = Aloc->j; array = Aloc->a;
1391b7c46309SBarry Smith   row = a->rstart;
1392bfec09a0SHong Zhang   for (i=0; i<ai[m]; i++) {aj[i] += a->cstart ;}
1393b7c46309SBarry Smith   for (i=0; i<m; i++) {
1394416022c9SBarry Smith     ierr = MatSetValues(B,ai[i+1]-ai[i],aj,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
1395b7c46309SBarry Smith     row++; array += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
1396b7c46309SBarry Smith   }
1397b7c46309SBarry Smith   aj = Aloc->j;
1398bfec09a0SHong Zhang   for (i=0; i<ai[m]; i++) {aj[i] -= a->cstart ;}
1399b7c46309SBarry Smith 
1400b7c46309SBarry Smith   /* copy over the B part */
1401ec8511deSBarry Smith   Aloc = (Mat_SeqAIJ*)a->B->data;
1402273d9f13SBarry Smith   m = a->B->m;  ai = Aloc->i; aj = Aloc->j; array = Aloc->a;
1403b7c46309SBarry Smith   row  = a->rstart;
1404bfec09a0SHong Zhang   ierr = PetscMalloc((1+ai[m])*sizeof(int),&cols);CHKERRQ(ierr);
1405b0a32e0cSBarry Smith   ct   = cols;
1406bfec09a0SHong Zhang   for (i=0; i<ai[m]; i++) {cols[i] = a->garray[aj[i]];}
1407b7c46309SBarry Smith   for (i=0; i<m; i++) {
1408416022c9SBarry Smith     ierr = MatSetValues(B,ai[i+1]-ai[i],cols,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
1409b7c46309SBarry Smith     row++; array += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
1410b7c46309SBarry Smith   }
1411606d414cSSatish Balay   ierr = PetscFree(ct);CHKERRQ(ierr);
14126d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14136d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14147c922b88SBarry Smith   if (matout) {
14150de55854SLois Curfman McInnes     *matout = B;
14160de55854SLois Curfman McInnes   } else {
1417273d9f13SBarry Smith     ierr = MatHeaderCopy(A,B);CHKERRQ(ierr);
14180de55854SLois Curfman McInnes   }
14193a40ed3dSBarry Smith   PetscFunctionReturn(0);
1420b7c46309SBarry Smith }
1421b7c46309SBarry Smith 
14224a2ae208SSatish Balay #undef __FUNCT__
14234a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_MPIAIJ"
1424dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr)
1425a008b906SSatish Balay {
14264b967eb1SSatish Balay   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
14274b967eb1SSatish Balay   Mat        a = aij->A,b = aij->B;
1428dfbe8321SBarry Smith   PetscErrorCode ierr;
1429dfbe8321SBarry Smith   int        s1,s2,s3;
1430a008b906SSatish Balay 
14313a40ed3dSBarry Smith   PetscFunctionBegin;
14324b967eb1SSatish Balay   ierr = MatGetLocalSize(mat,&s2,&s3);CHKERRQ(ierr);
14334b967eb1SSatish Balay   if (rr) {
1434e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr);
143529bbc08cSBarry Smith     if (s1!=s3) SETERRQ(PETSC_ERR_ARG_SIZ,"right vector non-conforming local size");
14364b967eb1SSatish Balay     /* Overlap communication with computation. */
143743a90d84SBarry Smith     ierr = VecScatterBegin(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx);CHKERRQ(ierr);
1438a008b906SSatish Balay   }
14394b967eb1SSatish Balay   if (ll) {
1440e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr);
144129bbc08cSBarry Smith     if (s1!=s2) SETERRQ(PETSC_ERR_ARG_SIZ,"left vector non-conforming local size");
1442f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,ll,0);CHKERRQ(ierr);
14434b967eb1SSatish Balay   }
14444b967eb1SSatish Balay   /* scale  the diagonal block */
1445f830108cSBarry Smith   ierr = (*a->ops->diagonalscale)(a,ll,rr);CHKERRQ(ierr);
14464b967eb1SSatish Balay 
14474b967eb1SSatish Balay   if (rr) {
14484b967eb1SSatish Balay     /* Do a scatter end and then right scale the off-diagonal block */
144943a90d84SBarry Smith     ierr = VecScatterEnd(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx);CHKERRQ(ierr);
1450f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,0,aij->lvec);CHKERRQ(ierr);
14514b967eb1SSatish Balay   }
14524b967eb1SSatish Balay 
14533a40ed3dSBarry Smith   PetscFunctionReturn(0);
1454a008b906SSatish Balay }
1455a008b906SSatish Balay 
1456a008b906SSatish Balay 
14574a2ae208SSatish Balay #undef __FUNCT__
14584a2ae208SSatish Balay #define __FUNCT__ "MatPrintHelp_MPIAIJ"
1459dfbe8321SBarry Smith PetscErrorCode MatPrintHelp_MPIAIJ(Mat A)
1460682d7d0cSBarry Smith {
1461682d7d0cSBarry Smith   Mat_MPIAIJ *a   = (Mat_MPIAIJ*)A->data;
1462dfbe8321SBarry Smith   PetscErrorCode ierr;
1463682d7d0cSBarry Smith 
14643a40ed3dSBarry Smith   PetscFunctionBegin;
14653a40ed3dSBarry Smith   if (!a->rank) {
14663a40ed3dSBarry Smith     ierr = MatPrintHelp_SeqAIJ(a->A);CHKERRQ(ierr);
14673a40ed3dSBarry Smith   }
14683a40ed3dSBarry Smith   PetscFunctionReturn(0);
1469682d7d0cSBarry Smith }
1470682d7d0cSBarry Smith 
14714a2ae208SSatish Balay #undef __FUNCT__
14724a2ae208SSatish Balay #define __FUNCT__ "MatGetBlockSize_MPIAIJ"
1473dfbe8321SBarry Smith PetscErrorCode MatGetBlockSize_MPIAIJ(Mat A,int *bs)
14745a838052SSatish Balay {
14753a40ed3dSBarry Smith   PetscFunctionBegin;
14765a838052SSatish Balay   *bs = 1;
14773a40ed3dSBarry Smith   PetscFunctionReturn(0);
14785a838052SSatish Balay }
14794a2ae208SSatish Balay #undef __FUNCT__
14804a2ae208SSatish Balay #define __FUNCT__ "MatSetUnfactored_MPIAIJ"
1481dfbe8321SBarry Smith PetscErrorCode MatSetUnfactored_MPIAIJ(Mat A)
1482bb5a7306SBarry Smith {
1483bb5a7306SBarry Smith   Mat_MPIAIJ *a   = (Mat_MPIAIJ*)A->data;
1484dfbe8321SBarry Smith   PetscErrorCode ierr;
14853a40ed3dSBarry Smith 
14863a40ed3dSBarry Smith   PetscFunctionBegin;
1487bb5a7306SBarry Smith   ierr = MatSetUnfactored(a->A);CHKERRQ(ierr);
14883a40ed3dSBarry Smith   PetscFunctionReturn(0);
1489bb5a7306SBarry Smith }
1490bb5a7306SBarry Smith 
14914a2ae208SSatish Balay #undef __FUNCT__
14924a2ae208SSatish Balay #define __FUNCT__ "MatEqual_MPIAIJ"
1493dfbe8321SBarry Smith PetscErrorCode MatEqual_MPIAIJ(Mat A,Mat B,PetscTruth *flag)
1494d4bb536fSBarry Smith {
1495d4bb536fSBarry Smith   Mat_MPIAIJ *matB = (Mat_MPIAIJ*)B->data,*matA = (Mat_MPIAIJ*)A->data;
1496d4bb536fSBarry Smith   Mat        a,b,c,d;
1497d4bb536fSBarry Smith   PetscTruth flg;
1498dfbe8321SBarry Smith   PetscErrorCode ierr;
1499d4bb536fSBarry Smith 
15003a40ed3dSBarry Smith   PetscFunctionBegin;
1501d4bb536fSBarry Smith   a = matA->A; b = matA->B;
1502d4bb536fSBarry Smith   c = matB->A; d = matB->B;
1503d4bb536fSBarry Smith 
1504d4bb536fSBarry Smith   ierr = MatEqual(a,c,&flg);CHKERRQ(ierr);
1505d4bb536fSBarry Smith   if (flg == PETSC_TRUE) {
1506d4bb536fSBarry Smith     ierr = MatEqual(b,d,&flg);CHKERRQ(ierr);
1507d4bb536fSBarry Smith   }
1508ca161407SBarry Smith   ierr = MPI_Allreduce(&flg,flag,1,MPI_INT,MPI_LAND,A->comm);CHKERRQ(ierr);
15093a40ed3dSBarry Smith   PetscFunctionReturn(0);
1510d4bb536fSBarry Smith }
1511d4bb536fSBarry Smith 
15124a2ae208SSatish Balay #undef __FUNCT__
15134a2ae208SSatish Balay #define __FUNCT__ "MatCopy_MPIAIJ"
1514dfbe8321SBarry Smith PetscErrorCode MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str)
1515cb5b572fSBarry Smith {
1516dfbe8321SBarry Smith   PetscErrorCode ierr;
1517cb5b572fSBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;
1518cb5b572fSBarry Smith   Mat_MPIAIJ *b = (Mat_MPIAIJ *)B->data;
1519cb5b572fSBarry Smith 
1520cb5b572fSBarry Smith   PetscFunctionBegin;
152133f4a19fSKris Buschelman   /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
152233f4a19fSKris Buschelman   if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) {
1523cb5b572fSBarry Smith     /* because of the column compression in the off-processor part of the matrix a->B,
1524cb5b572fSBarry Smith        the number of columns in a->B and b->B may be different, hence we cannot call
1525cb5b572fSBarry Smith        the MatCopy() directly on the two parts. If need be, we can provide a more
1526cb5b572fSBarry Smith        efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices
1527cb5b572fSBarry Smith        then copying the submatrices */
1528cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1529cb5b572fSBarry Smith   } else {
1530cb5b572fSBarry Smith     ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr);
1531cb5b572fSBarry Smith     ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr);
1532cb5b572fSBarry Smith   }
1533cb5b572fSBarry Smith   PetscFunctionReturn(0);
1534cb5b572fSBarry Smith }
1535cb5b572fSBarry Smith 
15364a2ae208SSatish Balay #undef __FUNCT__
15374a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_MPIAIJ"
1538dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_MPIAIJ(Mat A)
1539273d9f13SBarry Smith {
1540dfbe8321SBarry Smith   PetscErrorCode ierr;
1541273d9f13SBarry Smith 
1542273d9f13SBarry Smith   PetscFunctionBegin;
1543273d9f13SBarry Smith   ierr =  MatMPIAIJSetPreallocation(A,PETSC_DEFAULT,0,PETSC_DEFAULT,0);CHKERRQ(ierr);
1544273d9f13SBarry Smith   PetscFunctionReturn(0);
1545273d9f13SBarry Smith }
1546273d9f13SBarry Smith 
1547ac90fabeSBarry Smith #include "petscblaslapack.h"
1548ac90fabeSBarry Smith #undef __FUNCT__
1549ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_MPIAIJ"
1550dfbe8321SBarry Smith PetscErrorCode MatAXPY_MPIAIJ(const PetscScalar a[],Mat X,Mat Y,MatStructure str)
1551ac90fabeSBarry Smith {
1552dfbe8321SBarry Smith   PetscErrorCode ierr;
1553dfbe8321SBarry Smith   int        one=1,i;
1554ac90fabeSBarry Smith   Mat_MPIAIJ *xx = (Mat_MPIAIJ *)X->data,*yy = (Mat_MPIAIJ *)Y->data;
1555ac90fabeSBarry Smith   Mat_SeqAIJ *x,*y;
1556ac90fabeSBarry Smith 
1557ac90fabeSBarry Smith   PetscFunctionBegin;
1558ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
1559ac90fabeSBarry Smith     x = (Mat_SeqAIJ *)xx->A->data;
1560ac90fabeSBarry Smith     y = (Mat_SeqAIJ *)yy->A->data;
1561268466fbSBarry Smith     BLaxpy_(&x->nz,(PetscScalar*)a,x->a,&one,y->a,&one);
1562ac90fabeSBarry Smith     x = (Mat_SeqAIJ *)xx->B->data;
1563ac90fabeSBarry Smith     y = (Mat_SeqAIJ *)yy->B->data;
1564268466fbSBarry Smith     BLaxpy_(&x->nz,(PetscScalar*)a,x->a,&one,y->a,&one);
1565a30b2313SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) {
1566c537a176SHong Zhang     ierr = MatAXPY_SeqAIJ(a,xx->A,yy->A,str);CHKERRQ(ierr);
1567c537a176SHong Zhang 
1568c537a176SHong Zhang     x = (Mat_SeqAIJ *)xx->B->data;
1569a30b2313SHong Zhang     y = (Mat_SeqAIJ *)yy->B->data;
1570a30b2313SHong Zhang     if (y->xtoy && y->XtoY != xx->B) {
1571a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
1572a30b2313SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
1573c537a176SHong Zhang     }
1574a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
157524f910e3SHong Zhang       ierr = MatAXPYGetxtoy_Private(xx->B->m,x->i,x->j,xx->garray,y->i,y->j,yy->garray,&y->xtoy);CHKERRQ(ierr);
1576a30b2313SHong Zhang       y->XtoY = xx->B;
1577c537a176SHong Zhang     }
1578a30b2313SHong Zhang     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += (*a)*(x->a[i]);
1579ac90fabeSBarry Smith   } else {
1580ac90fabeSBarry Smith     ierr = MatAXPY_Basic(a,X,Y,str);CHKERRQ(ierr);
1581ac90fabeSBarry Smith   }
1582ac90fabeSBarry Smith   PetscFunctionReturn(0);
1583ac90fabeSBarry Smith }
1584ac90fabeSBarry Smith 
15858a729477SBarry Smith /* -------------------------------------------------------------------*/
1586cda55fadSBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ,
1587cda55fadSBarry Smith        MatGetRow_MPIAIJ,
1588cda55fadSBarry Smith        MatRestoreRow_MPIAIJ,
1589cda55fadSBarry Smith        MatMult_MPIAIJ,
159097304618SKris Buschelman /* 4*/ MatMultAdd_MPIAIJ,
15917c922b88SBarry Smith        MatMultTranspose_MPIAIJ,
15927c922b88SBarry Smith        MatMultTransposeAdd_MPIAIJ,
1593cda55fadSBarry Smith        0,
1594cda55fadSBarry Smith        0,
1595cda55fadSBarry Smith        0,
159697304618SKris Buschelman /*10*/ 0,
1597cda55fadSBarry Smith        0,
1598cda55fadSBarry Smith        0,
159944a69424SLois Curfman McInnes        MatRelax_MPIAIJ,
1600b7c46309SBarry Smith        MatTranspose_MPIAIJ,
160197304618SKris Buschelman /*15*/ MatGetInfo_MPIAIJ,
1602cda55fadSBarry Smith        MatEqual_MPIAIJ,
1603cda55fadSBarry Smith        MatGetDiagonal_MPIAIJ,
1604cda55fadSBarry Smith        MatDiagonalScale_MPIAIJ,
1605cda55fadSBarry Smith        MatNorm_MPIAIJ,
160697304618SKris Buschelman /*20*/ MatAssemblyBegin_MPIAIJ,
1607cda55fadSBarry Smith        MatAssemblyEnd_MPIAIJ,
16081eb62cbbSBarry Smith        0,
1609cda55fadSBarry Smith        MatSetOption_MPIAIJ,
1610cda55fadSBarry Smith        MatZeroEntries_MPIAIJ,
161197304618SKris Buschelman /*25*/ MatZeroRows_MPIAIJ,
161287828ca2SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
1613b9617806SBarry Smith        MatLUFactorSymbolic_MPIAIJ_TFS,
1614b9617806SBarry Smith #else
1615cda55fadSBarry Smith        0,
1616b9617806SBarry Smith #endif
1617cda55fadSBarry Smith        0,
1618cda55fadSBarry Smith        0,
1619cda55fadSBarry Smith        0,
162097304618SKris Buschelman /*30*/ MatSetUpPreallocation_MPIAIJ,
1621cda55fadSBarry Smith        0,
1622cda55fadSBarry Smith        0,
1623cda55fadSBarry Smith        0,
1624cda55fadSBarry Smith        0,
162597304618SKris Buschelman /*35*/ MatDuplicate_MPIAIJ,
1626cda55fadSBarry Smith        0,
1627cda55fadSBarry Smith        0,
1628cda55fadSBarry Smith        0,
1629cda55fadSBarry Smith        0,
163097304618SKris Buschelman /*40*/ MatAXPY_MPIAIJ,
1631cda55fadSBarry Smith        MatGetSubMatrices_MPIAIJ,
1632cda55fadSBarry Smith        MatIncreaseOverlap_MPIAIJ,
1633cda55fadSBarry Smith        MatGetValues_MPIAIJ,
1634cb5b572fSBarry Smith        MatCopy_MPIAIJ,
163597304618SKris Buschelman /*45*/ MatPrintHelp_MPIAIJ,
1636cda55fadSBarry Smith        MatScale_MPIAIJ,
1637cda55fadSBarry Smith        0,
1638cda55fadSBarry Smith        0,
1639cda55fadSBarry Smith        0,
164097304618SKris Buschelman /*50*/ MatGetBlockSize_MPIAIJ,
1641cda55fadSBarry Smith        0,
1642cda55fadSBarry Smith        0,
1643cda55fadSBarry Smith        0,
1644cda55fadSBarry Smith        0,
164597304618SKris Buschelman /*55*/ MatFDColoringCreate_MPIAIJ,
1646cda55fadSBarry Smith        0,
1647cda55fadSBarry Smith        MatSetUnfactored_MPIAIJ,
1648cda55fadSBarry Smith        0,
1649cda55fadSBarry Smith        0,
165097304618SKris Buschelman /*60*/ MatGetSubMatrix_MPIAIJ,
1651e03a110bSBarry Smith        MatDestroy_MPIAIJ,
1652e03a110bSBarry Smith        MatView_MPIAIJ,
16538a124369SBarry Smith        MatGetPetscMaps_Petsc,
1654a2243be0SBarry Smith        0,
165597304618SKris Buschelman /*65*/ 0,
1656a2243be0SBarry Smith        0,
1657a2243be0SBarry Smith        0,
1658a2243be0SBarry Smith        0,
1659a2243be0SBarry Smith        0,
166097304618SKris Buschelman /*70*/ 0,
1661a2243be0SBarry Smith        0,
1662a2243be0SBarry Smith        MatSetColoring_MPIAIJ,
1663779c1a83SBarry Smith        MatSetValuesAdic_MPIAIJ,
166497304618SKris Buschelman        MatSetValuesAdifor_MPIAIJ,
166597304618SKris Buschelman /*75*/ 0,
166697304618SKris Buschelman        0,
166797304618SKris Buschelman        0,
166897304618SKris Buschelman        0,
166997304618SKris Buschelman        0,
167097304618SKris Buschelman /*80*/ 0,
167197304618SKris Buschelman        0,
167297304618SKris Buschelman        0,
167397304618SKris Buschelman        0,
16746284ec50SHong Zhang /*85*/ MatLoad_MPIAIJ,
16756284ec50SHong Zhang        0,
16766284ec50SHong Zhang        0,
16776284ec50SHong Zhang        0,
16786284ec50SHong Zhang        0,
16796284ec50SHong Zhang /*90*/ 0,
16806284ec50SHong Zhang        MatMatMult_MPIAIJ_MPIAIJ,
168126be0446SHong Zhang        MatMatMultSymbolic_MPIAIJ_MPIAIJ,
168226be0446SHong Zhang        MatMatMultNumeric_MPIAIJ_MPIAIJ,
16836284ec50SHong Zhang };
168436ce4990SBarry Smith 
16852e8a6d31SBarry Smith /* ----------------------------------------------------------------------------------------*/
16862e8a6d31SBarry Smith 
1687fb2e594dSBarry Smith EXTERN_C_BEGIN
16884a2ae208SSatish Balay #undef __FUNCT__
16894a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_MPIAIJ"
1690dfbe8321SBarry Smith PetscErrorCode MatStoreValues_MPIAIJ(Mat mat)
16912e8a6d31SBarry Smith {
16922e8a6d31SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data;
1693dfbe8321SBarry Smith   PetscErrorCode ierr;
16942e8a6d31SBarry Smith 
16952e8a6d31SBarry Smith   PetscFunctionBegin;
16962e8a6d31SBarry Smith   ierr = MatStoreValues(aij->A);CHKERRQ(ierr);
16972e8a6d31SBarry Smith   ierr = MatStoreValues(aij->B);CHKERRQ(ierr);
16982e8a6d31SBarry Smith   PetscFunctionReturn(0);
16992e8a6d31SBarry Smith }
1700fb2e594dSBarry Smith EXTERN_C_END
17012e8a6d31SBarry Smith 
1702fb2e594dSBarry Smith EXTERN_C_BEGIN
17034a2ae208SSatish Balay #undef __FUNCT__
17044a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_MPIAIJ"
1705dfbe8321SBarry Smith PetscErrorCode MatRetrieveValues_MPIAIJ(Mat mat)
17062e8a6d31SBarry Smith {
17072e8a6d31SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data;
1708dfbe8321SBarry Smith   PetscErrorCode ierr;
17092e8a6d31SBarry Smith 
17102e8a6d31SBarry Smith   PetscFunctionBegin;
17112e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr);
17122e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr);
17132e8a6d31SBarry Smith   PetscFunctionReturn(0);
17142e8a6d31SBarry Smith }
1715fb2e594dSBarry Smith EXTERN_C_END
17168a729477SBarry Smith 
1717e090d566SSatish Balay #include "petscpc.h"
171827508adbSBarry Smith EXTERN_C_BEGIN
17194a2ae208SSatish Balay #undef __FUNCT__
1720a23d5eceSKris Buschelman #define __FUNCT__ "MatMPIAIJSetPreallocation_MPIAIJ"
1721dfbe8321SBarry Smith PetscErrorCode MatMPIAIJSetPreallocation_MPIAIJ(Mat B,int d_nz,const int d_nnz[],int o_nz,const int o_nnz[])
1722a23d5eceSKris Buschelman {
1723a23d5eceSKris Buschelman   Mat_MPIAIJ   *b;
1724dfbe8321SBarry Smith   PetscErrorCode ierr;
1725dfbe8321SBarry Smith   int          i;
1726a23d5eceSKris Buschelman 
1727a23d5eceSKris Buschelman   PetscFunctionBegin;
1728a23d5eceSKris Buschelman   B->preallocated = PETSC_TRUE;
1729a23d5eceSKris Buschelman   if (d_nz == PETSC_DEFAULT || d_nz == PETSC_DECIDE) d_nz = 5;
1730a23d5eceSKris Buschelman   if (o_nz == PETSC_DEFAULT || o_nz == PETSC_DECIDE) o_nz = 2;
1731a23d5eceSKris Buschelman   if (d_nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"d_nz cannot be less than 0: value %d",d_nz);
1732a23d5eceSKris Buschelman   if (o_nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"o_nz cannot be less than 0: value %d",o_nz);
1733a23d5eceSKris Buschelman   if (d_nnz) {
1734a23d5eceSKris Buschelman     for (i=0; i<B->m; i++) {
1735a23d5eceSKris Buschelman       if (d_nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"d_nnz cannot be less than 0: local row %d value %d",i,d_nnz[i]);
1736a23d5eceSKris Buschelman     }
1737a23d5eceSKris Buschelman   }
1738a23d5eceSKris Buschelman   if (o_nnz) {
1739a23d5eceSKris Buschelman     for (i=0; i<B->m; i++) {
1740a23d5eceSKris Buschelman       if (o_nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"o_nnz cannot be less than 0: local row %d value %d",i,o_nnz[i]);
1741a23d5eceSKris Buschelman     }
1742a23d5eceSKris Buschelman   }
1743a23d5eceSKris Buschelman   b = (Mat_MPIAIJ*)B->data;
1744c60e587dSKris Buschelman   ierr = MatSeqAIJSetPreallocation(b->A,d_nz,d_nnz);CHKERRQ(ierr);
1745c60e587dSKris Buschelman   ierr = MatSeqAIJSetPreallocation(b->B,o_nz,o_nnz);CHKERRQ(ierr);
1746a23d5eceSKris Buschelman 
1747a23d5eceSKris Buschelman   PetscFunctionReturn(0);
1748a23d5eceSKris Buschelman }
1749a23d5eceSKris Buschelman EXTERN_C_END
1750a23d5eceSKris Buschelman 
17510bad9183SKris Buschelman /*MC
1752fafad747SKris Buschelman    MATMPIAIJ - MATMPIAIJ = "mpiaij" - A matrix type to be used for parallel sparse matrices.
17530bad9183SKris Buschelman 
17540bad9183SKris Buschelman    Options Database Keys:
17550bad9183SKris Buschelman . -mat_type mpiaij - sets the matrix type to "mpiaij" during a call to MatSetFromOptions()
17560bad9183SKris Buschelman 
17570bad9183SKris Buschelman   Level: beginner
17580bad9183SKris Buschelman 
17590bad9183SKris Buschelman .seealso: MatCreateMPIAIJ
17600bad9183SKris Buschelman M*/
17610bad9183SKris Buschelman 
1762a23d5eceSKris Buschelman EXTERN_C_BEGIN
1763a23d5eceSKris Buschelman #undef __FUNCT__
17644a2ae208SSatish Balay #define __FUNCT__ "MatCreate_MPIAIJ"
1765dfbe8321SBarry Smith PetscErrorCode MatCreate_MPIAIJ(Mat B)
17668a729477SBarry Smith {
176744cd7ae7SLois Curfman McInnes   Mat_MPIAIJ *b;
1768dfbe8321SBarry Smith   PetscErrorCode ierr;
1769dfbe8321SBarry Smith   int        i,size;
1770416022c9SBarry Smith 
17713a40ed3dSBarry Smith   PetscFunctionBegin;
1772273d9f13SBarry Smith   ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr);
17731d55c564SBarry Smith 
1774b0a32e0cSBarry Smith   ierr            = PetscNew(Mat_MPIAIJ,&b);CHKERRQ(ierr);
1775b0a32e0cSBarry Smith   B->data         = (void*)b;
1776549d3d68SSatish Balay   ierr            = PetscMemzero(b,sizeof(Mat_MPIAIJ));CHKERRQ(ierr);
1777549d3d68SSatish Balay   ierr            = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
177844cd7ae7SLois Curfman McInnes   B->factor       = 0;
177944cd7ae7SLois Curfman McInnes   B->assembled    = PETSC_FALSE;
178090f02eecSBarry Smith   B->mapping      = 0;
1781d6dfbf8fSBarry Smith 
178247794344SBarry Smith   B->insertmode      = NOT_SET_VALUES;
17839eb4d147SSatish Balay   b->size            = size;
1784273d9f13SBarry Smith   ierr = MPI_Comm_rank(B->comm,&b->rank);CHKERRQ(ierr);
17851eb62cbbSBarry Smith 
1786273d9f13SBarry Smith   ierr = PetscSplitOwnership(B->comm,&B->m,&B->M);CHKERRQ(ierr);
1787273d9f13SBarry Smith   ierr = PetscSplitOwnership(B->comm,&B->n,&B->N);CHKERRQ(ierr);
17881eb62cbbSBarry Smith 
1789c7fcc2eaSBarry Smith   /* the information in the maps duplicates the information computed below, eventually
1790c7fcc2eaSBarry Smith      we should remove the duplicate information that is not contained in the maps */
17918a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->m,B->M,&B->rmap);CHKERRQ(ierr);
17928a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->n,B->N,&B->cmap);CHKERRQ(ierr);
1793c7fcc2eaSBarry Smith 
17941eb62cbbSBarry Smith   /* build local table of row and column ownerships */
1795b0a32e0cSBarry Smith   ierr = PetscMalloc(2*(b->size+2)*sizeof(int),&b->rowners);CHKERRQ(ierr);
1796b0a32e0cSBarry Smith   PetscLogObjectMemory(B,2*(b->size+2)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_MPIAIJ));
1797603f58a4SSatish Balay   b->cowners = b->rowners + b->size + 2;
1798273d9f13SBarry Smith   ierr = MPI_Allgather(&B->m,1,MPI_INT,b->rowners+1,1,MPI_INT,B->comm);CHKERRQ(ierr);
179944cd7ae7SLois Curfman McInnes   b->rowners[0] = 0;
180044cd7ae7SLois Curfman McInnes   for (i=2; i<=b->size; i++) {
180144cd7ae7SLois Curfman McInnes     b->rowners[i] += b->rowners[i-1];
18028a729477SBarry Smith   }
180344cd7ae7SLois Curfman McInnes   b->rstart = b->rowners[b->rank];
180444cd7ae7SLois Curfman McInnes   b->rend   = b->rowners[b->rank+1];
1805273d9f13SBarry Smith   ierr = MPI_Allgather(&B->n,1,MPI_INT,b->cowners+1,1,MPI_INT,B->comm);CHKERRQ(ierr);
180644cd7ae7SLois Curfman McInnes   b->cowners[0] = 0;
180744cd7ae7SLois Curfman McInnes   for (i=2; i<=b->size; i++) {
180844cd7ae7SLois Curfman McInnes     b->cowners[i] += b->cowners[i-1];
18098a729477SBarry Smith   }
181044cd7ae7SLois Curfman McInnes   b->cstart = b->cowners[b->rank];
181144cd7ae7SLois Curfman McInnes   b->cend   = b->cowners[b->rank+1];
18128a729477SBarry Smith 
18131eb62cbbSBarry Smith   /* build cache for off array entries formed */
18147e2c5f70SBarry Smith   ierr = MatStashCreate_Private(B->comm,1,&B->stash);CHKERRQ(ierr);
18157c922b88SBarry Smith   b->donotstash  = PETSC_FALSE;
181644cd7ae7SLois Curfman McInnes   b->colmap      = 0;
181744cd7ae7SLois Curfman McInnes   b->garray      = 0;
18187c922b88SBarry Smith   b->roworiented = PETSC_TRUE;
18198a729477SBarry Smith 
18201eb62cbbSBarry Smith   /* stuff used for matrix vector multiply */
18217c922b88SBarry Smith   b->lvec      = PETSC_NULL;
18227c922b88SBarry Smith   b->Mvctx     = PETSC_NULL;
18238a729477SBarry Smith 
18247a0afa10SBarry Smith   /* stuff for MatGetRow() */
182544cd7ae7SLois Curfman McInnes   b->rowindices   = 0;
182644cd7ae7SLois Curfman McInnes   b->rowvalues    = 0;
182744cd7ae7SLois Curfman McInnes   b->getrowactive = PETSC_FALSE;
18281a8d6bc9SBarry Smith 
1829be5d1d56SKris Buschelman   /* Explicitly create 2 MATSEQAIJ matrices. */
18309c097c71SKris Buschelman   ierr = MatCreate(PETSC_COMM_SELF,B->m,B->n,B->m,B->n,&b->A);CHKERRQ(ierr);
1831c60e587dSKris Buschelman   ierr = MatSetType(b->A,MATSEQAIJ);CHKERRQ(ierr);
1832c60e587dSKris Buschelman   PetscLogObjectParent(B,b->A);
18339c097c71SKris Buschelman   ierr = MatCreate(PETSC_COMM_SELF,B->m,B->N,B->m,B->N,&b->B);CHKERRQ(ierr);
1834c60e587dSKris Buschelman   ierr = MatSetType(b->B,MATSEQAIJ);CHKERRQ(ierr);
1835c60e587dSKris Buschelman   PetscLogObjectParent(B,b->B);
1836c60e587dSKris Buschelman 
1837f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
18382e8a6d31SBarry Smith                                      "MatStoreValues_MPIAIJ",
18390c97f09dSSatish Balay                                      MatStoreValues_MPIAIJ);CHKERRQ(ierr);
1840f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
18412e8a6d31SBarry Smith                                      "MatRetrieveValues_MPIAIJ",
18420c97f09dSSatish Balay                                      MatRetrieveValues_MPIAIJ);CHKERRQ(ierr);
1843f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetDiagonalBlock_C",
18445ef9f2a5SBarry Smith 				     "MatGetDiagonalBlock_MPIAIJ",
18450c97f09dSSatish Balay                                      MatGetDiagonalBlock_MPIAIJ);CHKERRQ(ierr);
18465fbd3699SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C",
18475fbd3699SBarry Smith 				     "MatIsTranspose_MPIAIJ",
18485fbd3699SBarry Smith 				     MatIsTranspose_MPIAIJ);CHKERRQ(ierr);
1849a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAIJSetPreallocation_C",
1850a23d5eceSKris Buschelman 				     "MatMPIAIJSetPreallocation_MPIAIJ",
1851a23d5eceSKris Buschelman 				     MatMPIAIJSetPreallocation_MPIAIJ);CHKERRQ(ierr);
185292b32695SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatDiagonalScaleLocal_C",
185392b32695SKris Buschelman 				     "MatDiagonalScaleLocal_MPIAIJ",
185492b32695SKris Buschelman 				     MatDiagonalScaleLocal_MPIAIJ);CHKERRQ(ierr);
18553a40ed3dSBarry Smith   PetscFunctionReturn(0);
1856d6dfbf8fSBarry Smith }
1857273d9f13SBarry Smith EXTERN_C_END
1858c74985f6SBarry Smith 
1859209238afSKris Buschelman /*MC
1860002d173eSKris Buschelman    MATAIJ - MATAIJ = "aij" - A matrix type to be used for sparse matrices.
1861209238afSKris Buschelman 
1862209238afSKris Buschelman    This matrix type is identical to MATSEQAIJ when constructed with a single process communicator,
186330e3259aSHong Zhang    and MATMPIAIJ otherwise.  As a result, for single process communicators,
186430e3259aSHong Zhang   MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation is supported
186530e3259aSHong Zhang   for communicators controlling multiple processes.  It is recommended that you call both of
186630e3259aSHong Zhang   the above preallocation routines for simplicity.
1867209238afSKris Buschelman 
1868209238afSKris Buschelman    Options Database Keys:
1869209238afSKris Buschelman . -mat_type aij - sets the matrix type to "aij" during a call to MatSetFromOptions()
1870209238afSKris Buschelman 
1871209238afSKris Buschelman   Level: beginner
1872209238afSKris Buschelman 
1873209238afSKris Buschelman .seealso: MatCreateMPIAIJ,MATSEQAIJ,MATMPIAIJ
1874209238afSKris Buschelman M*/
1875209238afSKris Buschelman 
1876209238afSKris Buschelman EXTERN_C_BEGIN
1877209238afSKris Buschelman #undef __FUNCT__
1878209238afSKris Buschelman #define __FUNCT__ "MatCreate_AIJ"
1879dfbe8321SBarry Smith PetscErrorCode MatCreate_AIJ(Mat A)
1880dfbe8321SBarry Smith {
1881*6849ba73SBarry Smith   PetscErrorCode ierr;
1882*6849ba73SBarry Smith   int size;
1883209238afSKris Buschelman 
1884209238afSKris Buschelman   PetscFunctionBegin;
1885209238afSKris Buschelman   ierr = PetscObjectChangeTypeName((PetscObject)A,MATAIJ);CHKERRQ(ierr);
1886209238afSKris Buschelman   ierr = MPI_Comm_size(A->comm,&size);CHKERRQ(ierr);
1887209238afSKris Buschelman   if (size == 1) {
1888209238afSKris Buschelman     ierr = MatSetType(A,MATSEQAIJ);CHKERRQ(ierr);
1889209238afSKris Buschelman   } else {
1890209238afSKris Buschelman     ierr = MatSetType(A,MATMPIAIJ);CHKERRQ(ierr);
1891209238afSKris Buschelman   }
1892209238afSKris Buschelman   PetscFunctionReturn(0);
1893209238afSKris Buschelman }
1894209238afSKris Buschelman EXTERN_C_END
1895209238afSKris Buschelman 
18964a2ae208SSatish Balay #undef __FUNCT__
18974a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_MPIAIJ"
1898dfbe8321SBarry Smith PetscErrorCode MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
1899d6dfbf8fSBarry Smith {
1900d6dfbf8fSBarry Smith   Mat        mat;
1901416022c9SBarry Smith   Mat_MPIAIJ *a,*oldmat = (Mat_MPIAIJ*)matin->data;
1902dfbe8321SBarry Smith   PetscErrorCode ierr;
1903d6dfbf8fSBarry Smith 
19043a40ed3dSBarry Smith   PetscFunctionBegin;
1905416022c9SBarry Smith   *newmat       = 0;
1906273d9f13SBarry Smith   ierr = MatCreate(matin->comm,matin->m,matin->n,matin->M,matin->N,&mat);CHKERRQ(ierr);
1907be5d1d56SKris Buschelman   ierr = MatSetType(mat,matin->type_name);CHKERRQ(ierr);
19081d5dac46SHong Zhang   ierr = PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
1909273d9f13SBarry Smith   a    = (Mat_MPIAIJ*)mat->data;
1910e1b6402fSHong Zhang 
1911d6dfbf8fSBarry Smith   mat->factor       = matin->factor;
1912c456f294SBarry Smith   mat->assembled    = PETSC_TRUE;
1913e7641de0SSatish Balay   mat->insertmode   = NOT_SET_VALUES;
1914273d9f13SBarry Smith   mat->preallocated = PETSC_TRUE;
1915d6dfbf8fSBarry Smith 
1916416022c9SBarry Smith   a->rstart       = oldmat->rstart;
1917416022c9SBarry Smith   a->rend         = oldmat->rend;
1918416022c9SBarry Smith   a->cstart       = oldmat->cstart;
1919416022c9SBarry Smith   a->cend         = oldmat->cend;
192017699dbbSLois Curfman McInnes   a->size         = oldmat->size;
192117699dbbSLois Curfman McInnes   a->rank         = oldmat->rank;
1922e7641de0SSatish Balay   a->donotstash   = oldmat->donotstash;
1923e7641de0SSatish Balay   a->roworiented  = oldmat->roworiented;
1924e7641de0SSatish Balay   a->rowindices   = 0;
1925bcd2baecSBarry Smith   a->rowvalues    = 0;
1926bcd2baecSBarry Smith   a->getrowactive = PETSC_FALSE;
1927d6dfbf8fSBarry Smith 
1928549d3d68SSatish Balay   ierr       = PetscMemcpy(a->rowners,oldmat->rowners,2*(a->size+2)*sizeof(int));CHKERRQ(ierr);
19298798bf22SSatish Balay   ierr       = MatStashCreate_Private(matin->comm,1,&mat->stash);CHKERRQ(ierr);
19302ee70a88SLois Curfman McInnes   if (oldmat->colmap) {
1931aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
19320f5bd95cSBarry Smith     ierr = PetscTableCreateCopy(oldmat->colmap,&a->colmap);CHKERRQ(ierr);
1933b1fc9764SSatish Balay #else
1934b0a32e0cSBarry Smith     ierr = PetscMalloc((mat->N)*sizeof(int),&a->colmap);CHKERRQ(ierr);
1935b0a32e0cSBarry Smith     PetscLogObjectMemory(mat,(mat->N)*sizeof(int));
1936273d9f13SBarry Smith     ierr      = PetscMemcpy(a->colmap,oldmat->colmap,(mat->N)*sizeof(int));CHKERRQ(ierr);
1937b1fc9764SSatish Balay #endif
1938416022c9SBarry Smith   } else a->colmap = 0;
19393f41c07dSBarry Smith   if (oldmat->garray) {
194016d6aa21SSatish Balay     int len;
1941273d9f13SBarry Smith     len  = oldmat->B->n;
1942b0a32e0cSBarry Smith     ierr = PetscMalloc((len+1)*sizeof(int),&a->garray);CHKERRQ(ierr);
1943b0a32e0cSBarry Smith     PetscLogObjectMemory(mat,len*sizeof(int));
1944549d3d68SSatish Balay     if (len) { ierr = PetscMemcpy(a->garray,oldmat->garray,len*sizeof(int));CHKERRQ(ierr); }
1945416022c9SBarry Smith   } else a->garray = 0;
1946d6dfbf8fSBarry Smith 
1947416022c9SBarry Smith   ierr =  VecDuplicate(oldmat->lvec,&a->lvec);CHKERRQ(ierr);
1948b0a32e0cSBarry Smith   PetscLogObjectParent(mat,a->lvec);
1949a56f8943SBarry Smith   ierr =  VecScatterCopy(oldmat->Mvctx,&a->Mvctx);CHKERRQ(ierr);
1950b0a32e0cSBarry Smith   PetscLogObjectParent(mat,a->Mvctx);
19514efcb820SBarry Smith   ierr =  MatDestroy(a->A);CHKERRQ(ierr);
19522e8a6d31SBarry Smith   ierr =  MatDuplicate(oldmat->A,cpvalues,&a->A);CHKERRQ(ierr);
1953b0a32e0cSBarry Smith   PetscLogObjectParent(mat,a->A);
19544efcb820SBarry Smith   ierr =  MatDestroy(a->B);CHKERRQ(ierr);
19552e8a6d31SBarry Smith   ierr =  MatDuplicate(oldmat->B,cpvalues,&a->B);CHKERRQ(ierr);
1956b0a32e0cSBarry Smith   PetscLogObjectParent(mat,a->B);
1957b0a32e0cSBarry Smith   ierr = PetscFListDuplicate(matin->qlist,&mat->qlist);CHKERRQ(ierr);
19588a729477SBarry Smith   *newmat = mat;
19593a40ed3dSBarry Smith   PetscFunctionReturn(0);
19608a729477SBarry Smith }
1961416022c9SBarry Smith 
1962e090d566SSatish Balay #include "petscsys.h"
1963416022c9SBarry Smith 
19644a2ae208SSatish Balay #undef __FUNCT__
19654a2ae208SSatish Balay #define __FUNCT__ "MatLoad_MPIAIJ"
1966dfbe8321SBarry Smith PetscErrorCode MatLoad_MPIAIJ(PetscViewer viewer,const MatType type,Mat *newmat)
1967416022c9SBarry Smith {
1968d65a2f8fSBarry Smith   Mat          A;
196987828ca2SBarry Smith   PetscScalar  *vals,*svals;
197019bcc07fSBarry Smith   MPI_Comm     comm = ((PetscObject)viewer)->comm;
1971416022c9SBarry Smith   MPI_Status   status;
1972*6849ba73SBarry Smith   PetscErrorCode ierr;
1973*6849ba73SBarry Smith   int          i,nz,j,rstart,rend,fd;
197417699dbbSLois Curfman McInnes   int          header[4],rank,size,*rowlengths = 0,M,N,m,*rowners,maxnz,*cols;
1975d65a2f8fSBarry Smith   int          *ourlens,*sndcounts = 0,*procsnz = 0,*offlens,jj,*mycols,*smycols;
1976b362ba68SBarry Smith   int          tag = ((PetscObject)viewer)->tag,cend,cstart,n;
1977416022c9SBarry Smith 
19783a40ed3dSBarry Smith   PetscFunctionBegin;
19791dab6e02SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
19801dab6e02SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
198117699dbbSLois Curfman McInnes   if (!rank) {
1982b0a32e0cSBarry Smith     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
19830752156aSBarry Smith     ierr = PetscBinaryRead(fd,(char *)header,4,PETSC_INT);CHKERRQ(ierr);
1984552e946dSBarry Smith     if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
1985d64ed03dSBarry Smith     if (header[3] < 0) {
198629bbc08cSBarry Smith       SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix in special format on disk, cannot load as MPIAIJ");
1987d64ed03dSBarry Smith     }
19886c5fab8fSBarry Smith   }
19896c5fab8fSBarry Smith 
1990ca161407SBarry Smith   ierr = MPI_Bcast(header+1,3,MPI_INT,0,comm);CHKERRQ(ierr);
1991416022c9SBarry Smith   M = header[1]; N = header[2];
1992416022c9SBarry Smith   /* determine ownership of all rows */
199317699dbbSLois Curfman McInnes   m = M/size + ((M % size) > rank);
1994b0a32e0cSBarry Smith   ierr = PetscMalloc((size+2)*sizeof(int),&rowners);CHKERRQ(ierr);
1995ca161407SBarry Smith   ierr = MPI_Allgather(&m,1,MPI_INT,rowners+1,1,MPI_INT,comm);CHKERRQ(ierr);
1996416022c9SBarry Smith   rowners[0] = 0;
199717699dbbSLois Curfman McInnes   for (i=2; i<=size; i++) {
1998416022c9SBarry Smith     rowners[i] += rowners[i-1];
1999416022c9SBarry Smith   }
200017699dbbSLois Curfman McInnes   rstart = rowners[rank];
200117699dbbSLois Curfman McInnes   rend   = rowners[rank+1];
2002416022c9SBarry Smith 
2003416022c9SBarry Smith   /* distribute row lengths to all processors */
2004b0a32e0cSBarry Smith   ierr    = PetscMalloc(2*(rend-rstart+1)*sizeof(int),&ourlens);CHKERRQ(ierr);
2005416022c9SBarry Smith   offlens = ourlens + (rend-rstart);
200617699dbbSLois Curfman McInnes   if (!rank) {
2007b0a32e0cSBarry Smith     ierr = PetscMalloc(M*sizeof(int),&rowlengths);CHKERRQ(ierr);
20080752156aSBarry Smith     ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
2009b0a32e0cSBarry Smith     ierr = PetscMalloc(size*sizeof(int),&sndcounts);CHKERRQ(ierr);
201017699dbbSLois Curfman McInnes     for (i=0; i<size; i++) sndcounts[i] = rowners[i+1] - rowners[i];
2011ca161407SBarry Smith     ierr = MPI_Scatterv(rowlengths,sndcounts,rowners,MPI_INT,ourlens,rend-rstart,MPI_INT,0,comm);CHKERRQ(ierr);
2012606d414cSSatish Balay     ierr = PetscFree(sndcounts);CHKERRQ(ierr);
20133a40ed3dSBarry Smith   } else {
2014ca161407SBarry Smith     ierr = MPI_Scatterv(0,0,0,MPI_INT,ourlens,rend-rstart,MPI_INT,0,comm);CHKERRQ(ierr);
2015416022c9SBarry Smith   }
2016416022c9SBarry Smith 
201717699dbbSLois Curfman McInnes   if (!rank) {
2018416022c9SBarry Smith     /* calculate the number of nonzeros on each processor */
2019b0a32e0cSBarry Smith     ierr = PetscMalloc(size*sizeof(int),&procsnz);CHKERRQ(ierr);
2020549d3d68SSatish Balay     ierr = PetscMemzero(procsnz,size*sizeof(int));CHKERRQ(ierr);
202117699dbbSLois Curfman McInnes     for (i=0; i<size; i++) {
2022416022c9SBarry Smith       for (j=rowners[i]; j< rowners[i+1]; j++) {
2023416022c9SBarry Smith         procsnz[i] += rowlengths[j];
2024416022c9SBarry Smith       }
2025416022c9SBarry Smith     }
2026606d414cSSatish Balay     ierr = PetscFree(rowlengths);CHKERRQ(ierr);
2027416022c9SBarry Smith 
2028416022c9SBarry Smith     /* determine max buffer needed and allocate it */
2029416022c9SBarry Smith     maxnz = 0;
203017699dbbSLois Curfman McInnes     for (i=0; i<size; i++) {
20310452661fSBarry Smith       maxnz = PetscMax(maxnz,procsnz[i]);
2032416022c9SBarry Smith     }
2033b0a32e0cSBarry Smith     ierr = PetscMalloc(maxnz*sizeof(int),&cols);CHKERRQ(ierr);
2034416022c9SBarry Smith 
2035416022c9SBarry Smith     /* read in my part of the matrix column indices  */
2036416022c9SBarry Smith     nz   = procsnz[0];
2037b0a32e0cSBarry Smith     ierr = PetscMalloc(nz*sizeof(int),&mycols);CHKERRQ(ierr);
20380752156aSBarry Smith     ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr);
2039d65a2f8fSBarry Smith 
2040d65a2f8fSBarry Smith     /* read in every one elses and ship off */
204117699dbbSLois Curfman McInnes     for (i=1; i<size; i++) {
2042d65a2f8fSBarry Smith       nz   = procsnz[i];
20430752156aSBarry Smith       ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr);
2044ca161407SBarry Smith       ierr = MPI_Send(cols,nz,MPI_INT,i,tag,comm);CHKERRQ(ierr);
2045d65a2f8fSBarry Smith     }
2046606d414cSSatish Balay     ierr = PetscFree(cols);CHKERRQ(ierr);
20473a40ed3dSBarry Smith   } else {
2048416022c9SBarry Smith     /* determine buffer space needed for message */
2049416022c9SBarry Smith     nz = 0;
2050416022c9SBarry Smith     for (i=0; i<m; i++) {
2051416022c9SBarry Smith       nz += ourlens[i];
2052416022c9SBarry Smith     }
2053b0a32e0cSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(int),&mycols);CHKERRQ(ierr);
2054416022c9SBarry Smith 
2055416022c9SBarry Smith     /* receive message of column indices*/
2056ca161407SBarry Smith     ierr = MPI_Recv(mycols,nz,MPI_INT,0,tag,comm,&status);CHKERRQ(ierr);
2057ca161407SBarry Smith     ierr = MPI_Get_count(&status,MPI_INT,&maxnz);CHKERRQ(ierr);
205829bbc08cSBarry Smith     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
2059416022c9SBarry Smith   }
2060416022c9SBarry Smith 
2061b362ba68SBarry Smith   /* determine column ownership if matrix is not square */
2062b362ba68SBarry Smith   if (N != M) {
2063b362ba68SBarry Smith     n      = N/size + ((N % size) > rank);
2064b362ba68SBarry Smith     ierr   = MPI_Scan(&n,&cend,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr);
2065b362ba68SBarry Smith     cstart = cend - n;
2066b362ba68SBarry Smith   } else {
2067b362ba68SBarry Smith     cstart = rstart;
2068b362ba68SBarry Smith     cend   = rend;
2069fb2e594dSBarry Smith     n      = cend - cstart;
2070b362ba68SBarry Smith   }
2071b362ba68SBarry Smith 
2072416022c9SBarry Smith   /* loop over local rows, determining number of off diagonal entries */
2073549d3d68SSatish Balay   ierr = PetscMemzero(offlens,m*sizeof(int));CHKERRQ(ierr);
2074416022c9SBarry Smith   jj = 0;
2075416022c9SBarry Smith   for (i=0; i<m; i++) {
2076416022c9SBarry Smith     for (j=0; j<ourlens[i]; j++) {
2077b362ba68SBarry Smith       if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++;
2078416022c9SBarry Smith       jj++;
2079416022c9SBarry Smith     }
2080416022c9SBarry Smith   }
2081d65a2f8fSBarry Smith 
2082d65a2f8fSBarry Smith   /* create our matrix */
2083416022c9SBarry Smith   for (i=0; i<m; i++) {
2084416022c9SBarry Smith     ourlens[i] -= offlens[i];
2085416022c9SBarry Smith   }
2086d10c748bSKris Buschelman   ierr = MatCreate(comm,m,n,M,N,&A);CHKERRQ(ierr);
2087d10c748bSKris Buschelman   ierr = MatSetType(A,type);CHKERRQ(ierr);
2088d10c748bSKris Buschelman   ierr = MatMPIAIJSetPreallocation(A,0,ourlens,0,offlens);CHKERRQ(ierr);
2089d10c748bSKris Buschelman 
2090fb2e594dSBarry Smith   ierr = MatSetOption(A,MAT_COLUMNS_SORTED);CHKERRQ(ierr);
2091d65a2f8fSBarry Smith   for (i=0; i<m; i++) {
2092d65a2f8fSBarry Smith     ourlens[i] += offlens[i];
2093d65a2f8fSBarry Smith   }
2094416022c9SBarry Smith 
209517699dbbSLois Curfman McInnes   if (!rank) {
209687828ca2SBarry Smith     ierr = PetscMalloc(maxnz*sizeof(PetscScalar),&vals);CHKERRQ(ierr);
2097416022c9SBarry Smith 
2098416022c9SBarry Smith     /* read in my part of the matrix numerical values  */
2099416022c9SBarry Smith     nz   = procsnz[0];
21000752156aSBarry Smith     ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
2101d65a2f8fSBarry Smith 
2102d65a2f8fSBarry Smith     /* insert into matrix */
2103d65a2f8fSBarry Smith     jj      = rstart;
2104d65a2f8fSBarry Smith     smycols = mycols;
2105d65a2f8fSBarry Smith     svals   = vals;
2106d65a2f8fSBarry Smith     for (i=0; i<m; i++) {
2107d65a2f8fSBarry Smith       ierr = MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
2108d65a2f8fSBarry Smith       smycols += ourlens[i];
2109d65a2f8fSBarry Smith       svals   += ourlens[i];
2110d65a2f8fSBarry Smith       jj++;
2111416022c9SBarry Smith     }
2112416022c9SBarry Smith 
2113d65a2f8fSBarry Smith     /* read in other processors and ship out */
211417699dbbSLois Curfman McInnes     for (i=1; i<size; i++) {
2115416022c9SBarry Smith       nz   = procsnz[i];
21160752156aSBarry Smith       ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
2117ca161407SBarry Smith       ierr = MPI_Send(vals,nz,MPIU_SCALAR,i,A->tag,comm);CHKERRQ(ierr);
2118416022c9SBarry Smith     }
2119606d414cSSatish Balay     ierr = PetscFree(procsnz);CHKERRQ(ierr);
21203a40ed3dSBarry Smith   } else {
2121d65a2f8fSBarry Smith     /* receive numeric values */
212287828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&vals);CHKERRQ(ierr);
2123416022c9SBarry Smith 
2124d65a2f8fSBarry Smith     /* receive message of values*/
2125ca161407SBarry Smith     ierr = MPI_Recv(vals,nz,MPIU_SCALAR,0,A->tag,comm,&status);CHKERRQ(ierr);
2126ca161407SBarry Smith     ierr = MPI_Get_count(&status,MPIU_SCALAR,&maxnz);CHKERRQ(ierr);
212729bbc08cSBarry Smith     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
2128d65a2f8fSBarry Smith 
2129d65a2f8fSBarry Smith     /* insert into matrix */
2130d65a2f8fSBarry Smith     jj      = rstart;
2131d65a2f8fSBarry Smith     smycols = mycols;
2132d65a2f8fSBarry Smith     svals   = vals;
2133d65a2f8fSBarry Smith     for (i=0; i<m; i++) {
2134d65a2f8fSBarry Smith       ierr     = MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
2135d65a2f8fSBarry Smith       smycols += ourlens[i];
2136d65a2f8fSBarry Smith       svals   += ourlens[i];
2137d65a2f8fSBarry Smith       jj++;
2138d65a2f8fSBarry Smith     }
2139d65a2f8fSBarry Smith   }
2140606d414cSSatish Balay   ierr = PetscFree(ourlens);CHKERRQ(ierr);
2141606d414cSSatish Balay   ierr = PetscFree(vals);CHKERRQ(ierr);
2142606d414cSSatish Balay   ierr = PetscFree(mycols);CHKERRQ(ierr);
2143606d414cSSatish Balay   ierr = PetscFree(rowners);CHKERRQ(ierr);
2144d65a2f8fSBarry Smith 
21456d4a8577SBarry Smith   ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
21466d4a8577SBarry Smith   ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2147d10c748bSKris Buschelman   *newmat = A;
21483a40ed3dSBarry Smith   PetscFunctionReturn(0);
2149416022c9SBarry Smith }
2150a0ff6018SBarry Smith 
21514a2ae208SSatish Balay #undef __FUNCT__
21524a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_MPIAIJ"
2153a0ff6018SBarry Smith /*
215429da9460SBarry Smith     Not great since it makes two copies of the submatrix, first an SeqAIJ
215529da9460SBarry Smith   in local and then by concatenating the local matrices the end result.
215629da9460SBarry Smith   Writing it directly would be much like MatGetSubMatrices_MPIAIJ()
2157a0ff6018SBarry Smith */
2158dfbe8321SBarry Smith PetscErrorCode MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,int csize,MatReuse call,Mat *newmat)
2159a0ff6018SBarry Smith {
2160dfbe8321SBarry Smith   PetscErrorCode ierr;
2161dfbe8321SBarry Smith   int          i,m,n,rstart,row,rend,nz,*cwork,size,rank,j;
2162ab50ec6bSBarry Smith   int          *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal;
2163fee21e36SBarry Smith   Mat          *local,M,Mreuse;
216487828ca2SBarry Smith   PetscScalar  *vwork,*aa;
216500e6dbe6SBarry Smith   MPI_Comm     comm = mat->comm;
216600e6dbe6SBarry Smith   Mat_SeqAIJ   *aij;
21677e2c5f70SBarry Smith 
2168a0ff6018SBarry Smith 
2169a0ff6018SBarry Smith   PetscFunctionBegin;
21701dab6e02SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
21711dab6e02SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
217200e6dbe6SBarry Smith 
2173fee21e36SBarry Smith   if (call ==  MAT_REUSE_MATRIX) {
2174fee21e36SBarry Smith     ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);CHKERRQ(ierr);
217529bbc08cSBarry Smith     if (!Mreuse) SETERRQ(1,"Submatrix passed in was not used before, cannot reuse");
2176fee21e36SBarry Smith     local = &Mreuse;
2177fee21e36SBarry Smith     ierr  = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&local);CHKERRQ(ierr);
2178fee21e36SBarry Smith   } else {
2179a0ff6018SBarry Smith     ierr   = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr);
2180fee21e36SBarry Smith     Mreuse = *local;
2181606d414cSSatish Balay     ierr   = PetscFree(local);CHKERRQ(ierr);
2182fee21e36SBarry Smith   }
2183a0ff6018SBarry Smith 
2184a0ff6018SBarry Smith   /*
2185a0ff6018SBarry Smith       m - number of local rows
2186a0ff6018SBarry Smith       n - number of columns (same on all processors)
2187a0ff6018SBarry Smith       rstart - first row in new global matrix generated
2188a0ff6018SBarry Smith   */
2189fee21e36SBarry Smith   ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr);
2190a0ff6018SBarry Smith   if (call == MAT_INITIAL_MATRIX) {
2191fee21e36SBarry Smith     aij = (Mat_SeqAIJ*)(Mreuse)->data;
219200e6dbe6SBarry Smith     ii  = aij->i;
219300e6dbe6SBarry Smith     jj  = aij->j;
219400e6dbe6SBarry Smith 
2195a0ff6018SBarry Smith     /*
219600e6dbe6SBarry Smith         Determine the number of non-zeros in the diagonal and off-diagonal
219700e6dbe6SBarry Smith         portions of the matrix in order to do correct preallocation
2198a0ff6018SBarry Smith     */
219900e6dbe6SBarry Smith 
220000e6dbe6SBarry Smith     /* first get start and end of "diagonal" columns */
22016a6a5d1dSBarry Smith     if (csize == PETSC_DECIDE) {
2202ab50ec6bSBarry Smith       ierr = ISGetSize(isrow,&mglobal);CHKERRQ(ierr);
2203ab50ec6bSBarry Smith       if (mglobal == n) { /* square matrix */
2204e2c4fddaSBarry Smith 	nlocal = m;
22056a6a5d1dSBarry Smith       } else {
2206ab50ec6bSBarry Smith         nlocal = n/size + ((n % size) > rank);
2207ab50ec6bSBarry Smith       }
2208ab50ec6bSBarry Smith     } else {
22096a6a5d1dSBarry Smith       nlocal = csize;
22106a6a5d1dSBarry Smith     }
2211ca161407SBarry Smith     ierr   = MPI_Scan(&nlocal,&rend,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr);
221200e6dbe6SBarry Smith     rstart = rend - nlocal;
22136a6a5d1dSBarry Smith     if (rank == size - 1 && rend != n) {
221419e8cfbbSBarry Smith       SETERRQ2(1,"Local column sizes %d do not add up to total number of columns %d",rend,n);
22156a6a5d1dSBarry Smith     }
221600e6dbe6SBarry Smith 
221700e6dbe6SBarry Smith     /* next, compute all the lengths */
2218b0a32e0cSBarry Smith     ierr  = PetscMalloc((2*m+1)*sizeof(int),&dlens);CHKERRQ(ierr);
221900e6dbe6SBarry Smith     olens = dlens + m;
222000e6dbe6SBarry Smith     for (i=0; i<m; i++) {
222100e6dbe6SBarry Smith       jend = ii[i+1] - ii[i];
222200e6dbe6SBarry Smith       olen = 0;
222300e6dbe6SBarry Smith       dlen = 0;
222400e6dbe6SBarry Smith       for (j=0; j<jend; j++) {
222500e6dbe6SBarry Smith         if (*jj < rstart || *jj >= rend) olen++;
222600e6dbe6SBarry Smith         else dlen++;
222700e6dbe6SBarry Smith         jj++;
222800e6dbe6SBarry Smith       }
222900e6dbe6SBarry Smith       olens[i] = olen;
223000e6dbe6SBarry Smith       dlens[i] = dlen;
223100e6dbe6SBarry Smith     }
2232e2d9671bSKris Buschelman     ierr = MatCreate(comm,m,nlocal,PETSC_DECIDE,n,&M);CHKERRQ(ierr);
2233e2d9671bSKris Buschelman     ierr = MatSetType(M,mat->type_name);CHKERRQ(ierr);
2234e2d9671bSKris Buschelman     ierr = MatMPIAIJSetPreallocation(M,0,dlens,0,olens);CHKERRQ(ierr);
2235606d414cSSatish Balay     ierr = PetscFree(dlens);CHKERRQ(ierr);
2236a0ff6018SBarry Smith   } else {
2237a0ff6018SBarry Smith     int ml,nl;
2238a0ff6018SBarry Smith 
2239a0ff6018SBarry Smith     M = *newmat;
2240a0ff6018SBarry Smith     ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr);
224129bbc08cSBarry Smith     if (ml != m) SETERRQ(PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
2242a0ff6018SBarry Smith     ierr = MatZeroEntries(M);CHKERRQ(ierr);
2243c48de900SBarry Smith     /*
2244c48de900SBarry Smith          The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
2245c48de900SBarry Smith        rather than the slower MatSetValues().
2246c48de900SBarry Smith     */
2247c48de900SBarry Smith     M->was_assembled = PETSC_TRUE;
2248c48de900SBarry Smith     M->assembled     = PETSC_FALSE;
2249a0ff6018SBarry Smith   }
2250a0ff6018SBarry Smith   ierr = MatGetOwnershipRange(M,&rstart,&rend);CHKERRQ(ierr);
2251fee21e36SBarry Smith   aij = (Mat_SeqAIJ*)(Mreuse)->data;
225200e6dbe6SBarry Smith   ii  = aij->i;
225300e6dbe6SBarry Smith   jj  = aij->j;
225400e6dbe6SBarry Smith   aa  = aij->a;
2255a0ff6018SBarry Smith   for (i=0; i<m; i++) {
2256a0ff6018SBarry Smith     row   = rstart + i;
225700e6dbe6SBarry Smith     nz    = ii[i+1] - ii[i];
225800e6dbe6SBarry Smith     cwork = jj;     jj += nz;
225900e6dbe6SBarry Smith     vwork = aa;     aa += nz;
22608c638d02SBarry Smith     ierr = MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr);
2261a0ff6018SBarry Smith   }
2262a0ff6018SBarry Smith 
2263a0ff6018SBarry Smith   ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2264a0ff6018SBarry Smith   ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2265a0ff6018SBarry Smith   *newmat = M;
2266fee21e36SBarry Smith 
2267fee21e36SBarry Smith   /* save submatrix used in processor for next request */
2268fee21e36SBarry Smith   if (call ==  MAT_INITIAL_MATRIX) {
2269fee21e36SBarry Smith     ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr);
2270fee21e36SBarry Smith     ierr = PetscObjectDereference((PetscObject)Mreuse);CHKERRQ(ierr);
2271fee21e36SBarry Smith   }
2272fee21e36SBarry Smith 
2273a0ff6018SBarry Smith   PetscFunctionReturn(0);
2274a0ff6018SBarry Smith }
2275273d9f13SBarry Smith 
22764a2ae208SSatish Balay #undef __FUNCT__
22774a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJSetPreallocation"
2278273d9f13SBarry Smith /*@C
2279273d9f13SBarry Smith    MatMPIAIJSetPreallocation - Creates a sparse parallel matrix in AIJ format
2280273d9f13SBarry Smith    (the default parallel PETSc format).  For good matrix assembly performance
2281273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameters
2282273d9f13SBarry Smith    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
2283273d9f13SBarry Smith    performance can be increased by more than a factor of 50.
2284273d9f13SBarry Smith 
2285273d9f13SBarry Smith    Collective on MPI_Comm
2286273d9f13SBarry Smith 
2287273d9f13SBarry Smith    Input Parameters:
2288273d9f13SBarry Smith +  A - the matrix
2289273d9f13SBarry Smith .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
2290273d9f13SBarry Smith            (same value is used for all local rows)
2291273d9f13SBarry Smith .  d_nnz - array containing the number of nonzeros in the various rows of the
2292273d9f13SBarry Smith            DIAGONAL portion of the local submatrix (possibly different for each row)
2293273d9f13SBarry Smith            or PETSC_NULL, if d_nz is used to specify the nonzero structure.
2294273d9f13SBarry Smith            The size of this array is equal to the number of local rows, i.e 'm'.
2295273d9f13SBarry Smith            You must leave room for the diagonal entry even if it is zero.
2296273d9f13SBarry Smith .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
2297273d9f13SBarry Smith            submatrix (same value is used for all local rows).
2298273d9f13SBarry Smith -  o_nnz - array containing the number of nonzeros in the various rows of the
2299273d9f13SBarry Smith            OFF-DIAGONAL portion of the local submatrix (possibly different for
2300273d9f13SBarry Smith            each row) or PETSC_NULL, if o_nz is used to specify the nonzero
2301273d9f13SBarry Smith            structure. The size of this array is equal to the number
2302273d9f13SBarry Smith            of local rows, i.e 'm'.
2303273d9f13SBarry Smith 
2304273d9f13SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
2305273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2306273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2307273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users manual for details.
2308273d9f13SBarry Smith 
2309273d9f13SBarry Smith    The user MUST specify either the local or global matrix dimensions
2310273d9f13SBarry Smith    (possibly both).
2311273d9f13SBarry Smith 
2312273d9f13SBarry Smith    The parallel matrix is partitioned such that the first m0 rows belong to
2313273d9f13SBarry Smith    process 0, the next m1 rows belong to process 1, the next m2 rows belong
2314273d9f13SBarry Smith    to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.
2315273d9f13SBarry Smith 
2316273d9f13SBarry Smith    The DIAGONAL portion of the local submatrix of a processor can be defined
2317273d9f13SBarry Smith    as the submatrix which is obtained by extraction the part corresponding
2318273d9f13SBarry Smith    to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the
2319273d9f13SBarry Smith    first row that belongs to the processor, and r2 is the last row belonging
2320273d9f13SBarry Smith    to the this processor. This is a square mxm matrix. The remaining portion
2321273d9f13SBarry Smith    of the local submatrix (mxN) constitute the OFF-DIAGONAL portion.
2322273d9f13SBarry Smith 
2323273d9f13SBarry Smith    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
2324273d9f13SBarry Smith 
2325273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible.
2326273d9f13SBarry Smith    We search for consecutive rows with the same nonzero structure, thereby
2327273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2328273d9f13SBarry Smith 
2329273d9f13SBarry Smith    Options Database Keys:
2330273d9f13SBarry Smith +  -mat_aij_no_inode  - Do not use inodes
2331273d9f13SBarry Smith .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2332273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2333273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2334273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2335273d9f13SBarry Smith 
2336273d9f13SBarry Smith    Example usage:
2337273d9f13SBarry Smith 
2338273d9f13SBarry Smith    Consider the following 8x8 matrix with 34 non-zero values, that is
2339273d9f13SBarry Smith    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
2340273d9f13SBarry Smith    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
2341273d9f13SBarry Smith    as follows:
2342273d9f13SBarry Smith 
2343273d9f13SBarry Smith .vb
2344273d9f13SBarry Smith             1  2  0  |  0  3  0  |  0  4
2345273d9f13SBarry Smith     Proc0   0  5  6  |  7  0  0  |  8  0
2346273d9f13SBarry Smith             9  0 10  | 11  0  0  | 12  0
2347273d9f13SBarry Smith     -------------------------------------
2348273d9f13SBarry Smith            13  0 14  | 15 16 17  |  0  0
2349273d9f13SBarry Smith     Proc1   0 18  0  | 19 20 21  |  0  0
2350273d9f13SBarry Smith             0  0  0  | 22 23  0  | 24  0
2351273d9f13SBarry Smith     -------------------------------------
2352273d9f13SBarry Smith     Proc2  25 26 27  |  0  0 28  | 29  0
2353273d9f13SBarry Smith            30  0  0  | 31 32 33  |  0 34
2354273d9f13SBarry Smith .ve
2355273d9f13SBarry Smith 
2356273d9f13SBarry Smith    This can be represented as a collection of submatrices as:
2357273d9f13SBarry Smith 
2358273d9f13SBarry Smith .vb
2359273d9f13SBarry Smith       A B C
2360273d9f13SBarry Smith       D E F
2361273d9f13SBarry Smith       G H I
2362273d9f13SBarry Smith .ve
2363273d9f13SBarry Smith 
2364273d9f13SBarry Smith    Where the submatrices A,B,C are owned by proc0, D,E,F are
2365273d9f13SBarry Smith    owned by proc1, G,H,I are owned by proc2.
2366273d9f13SBarry Smith 
2367273d9f13SBarry Smith    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2368273d9f13SBarry Smith    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2369273d9f13SBarry Smith    The 'M','N' parameters are 8,8, and have the same values on all procs.
2370273d9f13SBarry Smith 
2371273d9f13SBarry Smith    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
2372273d9f13SBarry Smith    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
2373273d9f13SBarry Smith    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
2374273d9f13SBarry Smith    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
2375273d9f13SBarry Smith    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
2376273d9f13SBarry Smith    matrix, ans [DF] as another SeqAIJ matrix.
2377273d9f13SBarry Smith 
2378273d9f13SBarry Smith    When d_nz, o_nz parameters are specified, d_nz storage elements are
2379273d9f13SBarry Smith    allocated for every row of the local diagonal submatrix, and o_nz
2380273d9f13SBarry Smith    storage locations are allocated for every row of the OFF-DIAGONAL submat.
2381273d9f13SBarry Smith    One way to choose d_nz and o_nz is to use the max nonzerors per local
2382273d9f13SBarry Smith    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
2383273d9f13SBarry Smith    In this case, the values of d_nz,o_nz are:
2384273d9f13SBarry Smith .vb
2385273d9f13SBarry Smith      proc0 : dnz = 2, o_nz = 2
2386273d9f13SBarry Smith      proc1 : dnz = 3, o_nz = 2
2387273d9f13SBarry Smith      proc2 : dnz = 1, o_nz = 4
2388273d9f13SBarry Smith .ve
2389273d9f13SBarry Smith    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
2390273d9f13SBarry Smith    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
2391273d9f13SBarry Smith    for proc3. i.e we are using 12+15+10=37 storage locations to store
2392273d9f13SBarry Smith    34 values.
2393273d9f13SBarry Smith 
2394273d9f13SBarry Smith    When d_nnz, o_nnz parameters are specified, the storage is specified
2395273d9f13SBarry Smith    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
2396273d9f13SBarry Smith    In the above case the values for d_nnz,o_nnz are:
2397273d9f13SBarry Smith .vb
2398273d9f13SBarry Smith      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
2399273d9f13SBarry Smith      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
2400273d9f13SBarry Smith      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
2401273d9f13SBarry Smith .ve
2402273d9f13SBarry Smith    Here the space allocated is sum of all the above values i.e 34, and
2403273d9f13SBarry Smith    hence pre-allocation is perfect.
2404273d9f13SBarry Smith 
2405273d9f13SBarry Smith    Level: intermediate
2406273d9f13SBarry Smith 
2407273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
2408273d9f13SBarry Smith 
2409273d9f13SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues()
2410273d9f13SBarry Smith @*/
2411dfbe8321SBarry Smith PetscErrorCode MatMPIAIJSetPreallocation(Mat B,int d_nz,const int d_nnz[],int o_nz,const int o_nnz[])
2412273d9f13SBarry Smith {
2413dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat,int,const int[],int,const int[]);
2414273d9f13SBarry Smith 
2415273d9f13SBarry Smith   PetscFunctionBegin;
2416a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatMPIAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
2417a23d5eceSKris Buschelman   if (f) {
2418a23d5eceSKris Buschelman     ierr = (*f)(B,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr);
2419273d9f13SBarry Smith   }
2420273d9f13SBarry Smith   PetscFunctionReturn(0);
2421273d9f13SBarry Smith }
2422273d9f13SBarry Smith 
24234a2ae208SSatish Balay #undef __FUNCT__
24244a2ae208SSatish Balay #define __FUNCT__ "MatCreateMPIAIJ"
2425273d9f13SBarry Smith /*@C
2426273d9f13SBarry Smith    MatCreateMPIAIJ - Creates a sparse parallel matrix in AIJ format
2427273d9f13SBarry Smith    (the default parallel PETSc format).  For good matrix assembly performance
2428273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameters
2429273d9f13SBarry Smith    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
2430273d9f13SBarry Smith    performance can be increased by more than a factor of 50.
2431273d9f13SBarry Smith 
2432273d9f13SBarry Smith    Collective on MPI_Comm
2433273d9f13SBarry Smith 
2434273d9f13SBarry Smith    Input Parameters:
2435273d9f13SBarry Smith +  comm - MPI communicator
2436273d9f13SBarry Smith .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
2437273d9f13SBarry Smith            This value should be the same as the local size used in creating the
2438273d9f13SBarry Smith            y vector for the matrix-vector product y = Ax.
2439273d9f13SBarry Smith .  n - This value should be the same as the local size used in creating the
2440273d9f13SBarry Smith        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
2441273d9f13SBarry Smith        calculated if N is given) For square matrices n is almost always m.
2442273d9f13SBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
2443273d9f13SBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
2444273d9f13SBarry Smith .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
2445273d9f13SBarry Smith            (same value is used for all local rows)
2446273d9f13SBarry Smith .  d_nnz - array containing the number of nonzeros in the various rows of the
2447273d9f13SBarry Smith            DIAGONAL portion of the local submatrix (possibly different for each row)
2448273d9f13SBarry Smith            or PETSC_NULL, if d_nz is used to specify the nonzero structure.
2449273d9f13SBarry Smith            The size of this array is equal to the number of local rows, i.e 'm'.
2450273d9f13SBarry Smith            You must leave room for the diagonal entry even if it is zero.
2451273d9f13SBarry Smith .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
2452273d9f13SBarry Smith            submatrix (same value is used for all local rows).
2453273d9f13SBarry Smith -  o_nnz - array containing the number of nonzeros in the various rows of the
2454273d9f13SBarry Smith            OFF-DIAGONAL portion of the local submatrix (possibly different for
2455273d9f13SBarry Smith            each row) or PETSC_NULL, if o_nz is used to specify the nonzero
2456273d9f13SBarry Smith            structure. The size of this array is equal to the number
2457273d9f13SBarry Smith            of local rows, i.e 'm'.
2458273d9f13SBarry Smith 
2459273d9f13SBarry Smith    Output Parameter:
2460273d9f13SBarry Smith .  A - the matrix
2461273d9f13SBarry Smith 
2462273d9f13SBarry Smith    Notes:
2463273d9f13SBarry Smith    m,n,M,N parameters specify the size of the matrix, and its partitioning across
2464273d9f13SBarry Smith    processors, while d_nz,d_nnz,o_nz,o_nnz parameters specify the approximate
2465273d9f13SBarry Smith    storage requirements for this matrix.
2466273d9f13SBarry Smith 
2467273d9f13SBarry Smith    If PETSC_DECIDE or  PETSC_DETERMINE is used for a particular argument on one
2468273d9f13SBarry Smith    processor than it must be used on all processors that share the object for
2469273d9f13SBarry Smith    that argument.
2470273d9f13SBarry Smith 
2471273d9f13SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
2472273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2473273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2474273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users manual for details.
2475273d9f13SBarry Smith 
2476273d9f13SBarry Smith    The user MUST specify either the local or global matrix dimensions
2477273d9f13SBarry Smith    (possibly both).
2478273d9f13SBarry Smith 
2479273d9f13SBarry Smith    The parallel matrix is partitioned such that the first m0 rows belong to
2480273d9f13SBarry Smith    process 0, the next m1 rows belong to process 1, the next m2 rows belong
2481273d9f13SBarry Smith    to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.
2482273d9f13SBarry Smith 
2483273d9f13SBarry Smith    The DIAGONAL portion of the local submatrix of a processor can be defined
2484273d9f13SBarry Smith    as the submatrix which is obtained by extraction the part corresponding
2485273d9f13SBarry Smith    to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the
2486273d9f13SBarry Smith    first row that belongs to the processor, and r2 is the last row belonging
2487273d9f13SBarry Smith    to the this processor. This is a square mxm matrix. The remaining portion
2488273d9f13SBarry Smith    of the local submatrix (mxN) constitute the OFF-DIAGONAL portion.
2489273d9f13SBarry Smith 
2490273d9f13SBarry Smith    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
2491273d9f13SBarry Smith 
249297d05335SKris Buschelman    When calling this routine with a single process communicator, a matrix of
249397d05335SKris Buschelman    type SEQAIJ is returned.  If a matrix of type MPIAIJ is desired for this
249497d05335SKris Buschelman    type of communicator, use the construction mechanism:
249597d05335SKris Buschelman      MatCreate(...,&A); MatSetType(A,MPIAIJ); MatMPIAIJSetPreallocation(A,...);
249697d05335SKris Buschelman 
2497273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible.
2498273d9f13SBarry Smith    We search for consecutive rows with the same nonzero structure, thereby
2499273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2500273d9f13SBarry Smith 
2501273d9f13SBarry Smith    Options Database Keys:
2502273d9f13SBarry Smith +  -mat_aij_no_inode  - Do not use inodes
2503273d9f13SBarry Smith .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2504273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2505273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2506273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2507273d9f13SBarry Smith 
2508273d9f13SBarry Smith 
2509273d9f13SBarry Smith    Example usage:
2510273d9f13SBarry Smith 
2511273d9f13SBarry Smith    Consider the following 8x8 matrix with 34 non-zero values, that is
2512273d9f13SBarry Smith    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
2513273d9f13SBarry Smith    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
2514273d9f13SBarry Smith    as follows:
2515273d9f13SBarry Smith 
2516273d9f13SBarry Smith .vb
2517273d9f13SBarry Smith             1  2  0  |  0  3  0  |  0  4
2518273d9f13SBarry Smith     Proc0   0  5  6  |  7  0  0  |  8  0
2519273d9f13SBarry Smith             9  0 10  | 11  0  0  | 12  0
2520273d9f13SBarry Smith     -------------------------------------
2521273d9f13SBarry Smith            13  0 14  | 15 16 17  |  0  0
2522273d9f13SBarry Smith     Proc1   0 18  0  | 19 20 21  |  0  0
2523273d9f13SBarry Smith             0  0  0  | 22 23  0  | 24  0
2524273d9f13SBarry Smith     -------------------------------------
2525273d9f13SBarry Smith     Proc2  25 26 27  |  0  0 28  | 29  0
2526273d9f13SBarry Smith            30  0  0  | 31 32 33  |  0 34
2527273d9f13SBarry Smith .ve
2528273d9f13SBarry Smith 
2529273d9f13SBarry Smith    This can be represented as a collection of submatrices as:
2530273d9f13SBarry Smith 
2531273d9f13SBarry Smith .vb
2532273d9f13SBarry Smith       A B C
2533273d9f13SBarry Smith       D E F
2534273d9f13SBarry Smith       G H I
2535273d9f13SBarry Smith .ve
2536273d9f13SBarry Smith 
2537273d9f13SBarry Smith    Where the submatrices A,B,C are owned by proc0, D,E,F are
2538273d9f13SBarry Smith    owned by proc1, G,H,I are owned by proc2.
2539273d9f13SBarry Smith 
2540273d9f13SBarry Smith    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2541273d9f13SBarry Smith    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2542273d9f13SBarry Smith    The 'M','N' parameters are 8,8, and have the same values on all procs.
2543273d9f13SBarry Smith 
2544273d9f13SBarry Smith    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
2545273d9f13SBarry Smith    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
2546273d9f13SBarry Smith    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
2547273d9f13SBarry Smith    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
2548273d9f13SBarry Smith    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
2549273d9f13SBarry Smith    matrix, ans [DF] as another SeqAIJ matrix.
2550273d9f13SBarry Smith 
2551273d9f13SBarry Smith    When d_nz, o_nz parameters are specified, d_nz storage elements are
2552273d9f13SBarry Smith    allocated for every row of the local diagonal submatrix, and o_nz
2553273d9f13SBarry Smith    storage locations are allocated for every row of the OFF-DIAGONAL submat.
2554273d9f13SBarry Smith    One way to choose d_nz and o_nz is to use the max nonzerors per local
2555273d9f13SBarry Smith    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
2556273d9f13SBarry Smith    In this case, the values of d_nz,o_nz are:
2557273d9f13SBarry Smith .vb
2558273d9f13SBarry Smith      proc0 : dnz = 2, o_nz = 2
2559273d9f13SBarry Smith      proc1 : dnz = 3, o_nz = 2
2560273d9f13SBarry Smith      proc2 : dnz = 1, o_nz = 4
2561273d9f13SBarry Smith .ve
2562273d9f13SBarry Smith    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
2563273d9f13SBarry Smith    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
2564273d9f13SBarry Smith    for proc3. i.e we are using 12+15+10=37 storage locations to store
2565273d9f13SBarry Smith    34 values.
2566273d9f13SBarry Smith 
2567273d9f13SBarry Smith    When d_nnz, o_nnz parameters are specified, the storage is specified
2568273d9f13SBarry Smith    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
2569273d9f13SBarry Smith    In the above case the values for d_nnz,o_nnz are:
2570273d9f13SBarry Smith .vb
2571273d9f13SBarry Smith      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
2572273d9f13SBarry Smith      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
2573273d9f13SBarry Smith      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
2574273d9f13SBarry Smith .ve
2575273d9f13SBarry Smith    Here the space allocated is sum of all the above values i.e 34, and
2576273d9f13SBarry Smith    hence pre-allocation is perfect.
2577273d9f13SBarry Smith 
2578273d9f13SBarry Smith    Level: intermediate
2579273d9f13SBarry Smith 
2580273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
2581273d9f13SBarry Smith 
2582273d9f13SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues()
2583273d9f13SBarry Smith @*/
2584dfbe8321SBarry Smith PetscErrorCode MatCreateMPIAIJ(MPI_Comm comm,int m,int n,int M,int N,int d_nz,const int d_nnz[],int o_nz,const int o_nnz[],Mat *A)
2585273d9f13SBarry Smith {
2586*6849ba73SBarry Smith   PetscErrorCode ierr;
2587*6849ba73SBarry Smith   int size;
2588273d9f13SBarry Smith 
2589273d9f13SBarry Smith   PetscFunctionBegin;
2590273d9f13SBarry Smith   ierr = MatCreate(comm,m,n,M,N,A);CHKERRQ(ierr);
2591273d9f13SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
2592273d9f13SBarry Smith   if (size > 1) {
2593273d9f13SBarry Smith     ierr = MatSetType(*A,MATMPIAIJ);CHKERRQ(ierr);
2594273d9f13SBarry Smith     ierr = MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr);
2595273d9f13SBarry Smith   } else {
2596273d9f13SBarry Smith     ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2597273d9f13SBarry Smith     ierr = MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);CHKERRQ(ierr);
2598273d9f13SBarry Smith   }
2599273d9f13SBarry Smith   PetscFunctionReturn(0);
2600273d9f13SBarry Smith }
2601195d93cdSBarry Smith 
26024a2ae208SSatish Balay #undef __FUNCT__
26034a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJGetSeqAIJ"
2604dfbe8321SBarry Smith PetscErrorCode MatMPIAIJGetSeqAIJ(Mat A,Mat *Ad,Mat *Ao,int *colmap[])
2605195d93cdSBarry Smith {
2606195d93cdSBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;
2607195d93cdSBarry Smith   PetscFunctionBegin;
2608195d93cdSBarry Smith   *Ad     = a->A;
2609195d93cdSBarry Smith   *Ao     = a->B;
2610195d93cdSBarry Smith   *colmap = a->garray;
2611195d93cdSBarry Smith   PetscFunctionReturn(0);
2612195d93cdSBarry Smith }
2613a2243be0SBarry Smith 
2614a2243be0SBarry Smith #undef __FUNCT__
2615a2243be0SBarry Smith #define __FUNCT__ "MatSetColoring_MPIAIJ"
2616dfbe8321SBarry Smith PetscErrorCode MatSetColoring_MPIAIJ(Mat A,ISColoring coloring)
2617a2243be0SBarry Smith {
2618dfbe8321SBarry Smith   PetscErrorCode ierr;
2619dfbe8321SBarry Smith   int        i;
2620a2243be0SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2621a2243be0SBarry Smith 
2622a2243be0SBarry Smith   PetscFunctionBegin;
2623a2243be0SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
262408b6dcc0SBarry Smith     ISColoringValue *allcolors,*colors;
2625a2243be0SBarry Smith     ISColoring      ocoloring;
2626a2243be0SBarry Smith 
2627a2243be0SBarry Smith     /* set coloring for diagonal portion */
2628a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->A,coloring);CHKERRQ(ierr);
2629a2243be0SBarry Smith 
2630a2243be0SBarry Smith     /* set coloring for off-diagonal portion */
263108b6dcc0SBarry Smith     ierr = ISAllGatherColors(A->comm,coloring->n,coloring->colors,PETSC_NULL,&allcolors);CHKERRQ(ierr);
263208b6dcc0SBarry Smith     ierr = PetscMalloc((a->B->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
2633a2243be0SBarry Smith     for (i=0; i<a->B->n; i++) {
2634a2243be0SBarry Smith       colors[i] = allcolors[a->garray[i]];
2635a2243be0SBarry Smith     }
2636a2243be0SBarry Smith     ierr = PetscFree(allcolors);CHKERRQ(ierr);
2637a2243be0SBarry Smith     ierr = ISColoringCreate(MPI_COMM_SELF,a->B->n,colors,&ocoloring);CHKERRQ(ierr);
2638a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr);
2639a2243be0SBarry Smith     ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr);
2640a2243be0SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
264108b6dcc0SBarry Smith     ISColoringValue *colors;
264208b6dcc0SBarry Smith     int             *larray;
2643a2243be0SBarry Smith     ISColoring      ocoloring;
2644a2243be0SBarry Smith 
2645a2243be0SBarry Smith     /* set coloring for diagonal portion */
2646a2243be0SBarry Smith     ierr = PetscMalloc((a->A->n+1)*sizeof(int),&larray);CHKERRQ(ierr);
2647a2243be0SBarry Smith     for (i=0; i<a->A->n; i++) {
2648a2243be0SBarry Smith       larray[i] = i + a->cstart;
2649a2243be0SBarry Smith     }
2650a2243be0SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->A->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
265108b6dcc0SBarry Smith     ierr = PetscMalloc((a->A->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
2652a2243be0SBarry Smith     for (i=0; i<a->A->n; i++) {
2653a2243be0SBarry Smith       colors[i] = coloring->colors[larray[i]];
2654a2243be0SBarry Smith     }
2655a2243be0SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
265612c595b3SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,a->A->n,colors,&ocoloring);CHKERRQ(ierr);
2657a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->A,ocoloring);CHKERRQ(ierr);
2658a2243be0SBarry Smith     ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr);
2659a2243be0SBarry Smith 
2660a2243be0SBarry Smith     /* set coloring for off-diagonal portion */
2661a2243be0SBarry Smith     ierr = PetscMalloc((a->B->n+1)*sizeof(int),&larray);CHKERRQ(ierr);
2662a2243be0SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->B->n,a->garray,PETSC_NULL,larray);CHKERRQ(ierr);
266308b6dcc0SBarry Smith     ierr = PetscMalloc((a->B->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
2664a2243be0SBarry Smith     for (i=0; i<a->B->n; i++) {
2665a2243be0SBarry Smith       colors[i] = coloring->colors[larray[i]];
2666a2243be0SBarry Smith     }
2667a2243be0SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
2668a2243be0SBarry Smith     ierr = ISColoringCreate(MPI_COMM_SELF,a->B->n,colors,&ocoloring);CHKERRQ(ierr);
2669a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr);
2670a2243be0SBarry Smith     ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr);
2671a2243be0SBarry Smith   } else {
2672a2243be0SBarry Smith     SETERRQ1(1,"No support ISColoringType %d",coloring->ctype);
2673a2243be0SBarry Smith   }
2674a2243be0SBarry Smith 
2675a2243be0SBarry Smith   PetscFunctionReturn(0);
2676a2243be0SBarry Smith }
2677a2243be0SBarry Smith 
2678a2243be0SBarry Smith #undef __FUNCT__
2679779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdic_MPIAIJ"
2680dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_MPIAIJ(Mat A,void *advalues)
2681a2243be0SBarry Smith {
2682a2243be0SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2683dfbe8321SBarry Smith   PetscErrorCode ierr;
2684a2243be0SBarry Smith 
2685a2243be0SBarry Smith   PetscFunctionBegin;
2686779c1a83SBarry Smith   ierr = MatSetValuesAdic_SeqAIJ(a->A,advalues);CHKERRQ(ierr);
2687779c1a83SBarry Smith   ierr = MatSetValuesAdic_SeqAIJ(a->B,advalues);CHKERRQ(ierr);
2688779c1a83SBarry Smith   PetscFunctionReturn(0);
2689779c1a83SBarry Smith }
2690779c1a83SBarry Smith 
2691779c1a83SBarry Smith #undef __FUNCT__
2692779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdifor_MPIAIJ"
2693dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdifor_MPIAIJ(Mat A,int nl,void *advalues)
2694779c1a83SBarry Smith {
2695779c1a83SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2696dfbe8321SBarry Smith   PetscErrorCode ierr;
2697779c1a83SBarry Smith 
2698779c1a83SBarry Smith   PetscFunctionBegin;
2699779c1a83SBarry Smith   ierr = MatSetValuesAdifor_SeqAIJ(a->A,nl,advalues);CHKERRQ(ierr);
2700779c1a83SBarry Smith   ierr = MatSetValuesAdifor_SeqAIJ(a->B,nl,advalues);CHKERRQ(ierr);
2701a2243be0SBarry Smith   PetscFunctionReturn(0);
2702a2243be0SBarry Smith }
2703c5d6d63eSBarry Smith 
2704c5d6d63eSBarry Smith #undef __FUNCT__
270551dd7536SBarry Smith #define __FUNCT__ "MatMerge"
2706c5d6d63eSBarry Smith /*@C
270751dd7536SBarry Smith       MatMerge - Creates a single large PETSc matrix by concatinating sequential
270851dd7536SBarry Smith                  matrices from each processor
2709c5d6d63eSBarry Smith 
2710c5d6d63eSBarry Smith     Collective on MPI_Comm
2711c5d6d63eSBarry Smith 
2712c5d6d63eSBarry Smith    Input Parameters:
271351dd7536SBarry Smith +    comm - the communicators the parallel matrix will live on
2714d6bb3c2dSHong Zhang .    inmat - the input sequential matrices
2715d6bb3c2dSHong Zhang -    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
271651dd7536SBarry Smith 
271751dd7536SBarry Smith    Output Parameter:
271851dd7536SBarry Smith .    outmat - the parallel matrix generated
2719c5d6d63eSBarry Smith 
27207e25d530SSatish Balay     Level: advanced
27217e25d530SSatish Balay 
2722c5d6d63eSBarry Smith    Notes: The number of columns of the matrix in EACH of the seperate files
2723c5d6d63eSBarry Smith       MUST be the same.
2724c5d6d63eSBarry Smith 
2725c5d6d63eSBarry Smith @*/
2726dfbe8321SBarry Smith PetscErrorCode MatMerge(MPI_Comm comm,Mat inmat,MatReuse scall,Mat *outmat)
2727c5d6d63eSBarry Smith {
2728dfbe8321SBarry Smith   PetscErrorCode ierr;
2729dfbe8321SBarry Smith   int               m,n,i,rstart,nnz,I,*dnz,*onz;
2730b3cc6726SBarry Smith   const int         *indx;
2731b3cc6726SBarry Smith   const PetscScalar *values;
273251dd7536SBarry Smith   PetscMap          columnmap,rowmap;
2733c5d6d63eSBarry Smith 
2734c5d6d63eSBarry Smith   PetscFunctionBegin;
2735c5d6d63eSBarry Smith 
273651dd7536SBarry Smith   ierr = MatGetSize(inmat,&m,&n);CHKERRQ(ierr);
2737d6bb3c2dSHong Zhang   if (scall == MAT_INITIAL_MATRIX){
2738d6bb3c2dSHong Zhang     /* count nonzeros in each row, for diagonal and off diagonal portion of matrix */
2739d6bb3c2dSHong Zhang     ierr = PetscMapCreate(comm,&columnmap);CHKERRQ(ierr);
2740d6bb3c2dSHong Zhang     ierr = PetscMapSetSize(columnmap,n);CHKERRQ(ierr);
2741d6bb3c2dSHong Zhang     ierr = PetscMapSetType(columnmap,MAP_MPI);CHKERRQ(ierr);
2742d6bb3c2dSHong Zhang     ierr = PetscMapGetLocalSize(columnmap,&n);CHKERRQ(ierr);
2743d6bb3c2dSHong Zhang     ierr = PetscMapDestroy(columnmap);CHKERRQ(ierr);
2744d6bb3c2dSHong Zhang 
2745d6bb3c2dSHong Zhang     ierr = PetscMapCreate(comm,&rowmap);CHKERRQ(ierr);
2746d6bb3c2dSHong Zhang     ierr = PetscMapSetLocalSize(rowmap,m);CHKERRQ(ierr);
2747d6bb3c2dSHong Zhang     ierr = PetscMapSetType(rowmap,MAP_MPI);CHKERRQ(ierr);
2748d6bb3c2dSHong Zhang     ierr = PetscMapGetLocalRange(rowmap,&rstart,0);CHKERRQ(ierr);
2749d6bb3c2dSHong Zhang     ierr = PetscMapDestroy(rowmap);CHKERRQ(ierr);
2750d6bb3c2dSHong Zhang 
2751d6bb3c2dSHong Zhang     ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr);
2752d6bb3c2dSHong Zhang     for (i=0;i<m;i++) {
2753d6bb3c2dSHong Zhang       ierr = MatGetRow(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
2754d6bb3c2dSHong Zhang       ierr = MatPreallocateSet(i+rstart,nnz,indx,dnz,onz);CHKERRQ(ierr);
2755d6bb3c2dSHong Zhang       ierr = MatRestoreRow(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
2756d6bb3c2dSHong Zhang     }
2757d6bb3c2dSHong Zhang     /* This routine will ONLY return MPIAIJ type matrix */
2758d6bb3c2dSHong Zhang     ierr = MatCreate(comm,m,n,PETSC_DETERMINE,PETSC_DETERMINE,outmat);CHKERRQ(ierr);
2759d6bb3c2dSHong Zhang     ierr = MatSetType(*outmat,MATMPIAIJ);CHKERRQ(ierr);
2760d6bb3c2dSHong Zhang     ierr = MatMPIAIJSetPreallocation(*outmat,0,dnz,0,onz);CHKERRQ(ierr);
2761d6bb3c2dSHong Zhang     ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr);
2762d6bb3c2dSHong Zhang 
2763d6bb3c2dSHong Zhang   } else if (scall == MAT_REUSE_MATRIX){
2764d6bb3c2dSHong Zhang     ierr = MatGetOwnershipRange(*outmat,&rstart,PETSC_NULL);CHKERRQ(ierr);
2765d6bb3c2dSHong Zhang   } else {
2766d6bb3c2dSHong Zhang     SETERRQ1(PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",scall);
2767d6bb3c2dSHong Zhang   }
2768d6bb3c2dSHong Zhang 
2769d6bb3c2dSHong Zhang   for (i=0;i<m;i++) {
2770d6bb3c2dSHong Zhang     ierr = MatGetRow(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
2771d6bb3c2dSHong Zhang     I    = i + rstart;
2772d6bb3c2dSHong Zhang     ierr = MatSetValues(*outmat,1,&I,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr);
2773d6bb3c2dSHong Zhang     ierr = MatRestoreRow(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
2774d6bb3c2dSHong Zhang   }
2775d6bb3c2dSHong Zhang   ierr = MatDestroy(inmat);CHKERRQ(ierr);
2776d6bb3c2dSHong Zhang   ierr = MatAssemblyBegin(*outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2777d6bb3c2dSHong Zhang   ierr = MatAssemblyEnd(*outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2778d6bb3c2dSHong Zhang #ifdef OLD
2779d6bb3c2dSHong Zhang   ierr = MatGetSize(inmat,&m,&n);CHKERRQ(ierr);
278051dd7536SBarry Smith 
278151dd7536SBarry Smith   /* count nonzeros in each row, for diagonal and off diagonal portion of matrix */
278251dd7536SBarry Smith   ierr = PetscMapCreate(comm,&columnmap);CHKERRQ(ierr);
278351dd7536SBarry Smith   ierr = PetscMapSetSize(columnmap,n);CHKERRQ(ierr);
278451dd7536SBarry Smith   ierr = PetscMapSetType(columnmap,MAP_MPI);CHKERRQ(ierr);
278551dd7536SBarry Smith   ierr = PetscMapGetLocalSize(columnmap,&n);CHKERRQ(ierr);
278651dd7536SBarry Smith   ierr = PetscMapDestroy(columnmap);CHKERRQ(ierr);
278751dd7536SBarry Smith 
278851dd7536SBarry Smith   ierr = PetscMapCreate(comm,&rowmap);CHKERRQ(ierr);
278951dd7536SBarry Smith   ierr = PetscMapSetLocalSize(rowmap,m);CHKERRQ(ierr);
279051dd7536SBarry Smith   ierr = PetscMapSetType(rowmap,MAP_MPI);CHKERRQ(ierr);
279151dd7536SBarry Smith   ierr = PetscMapGetLocalRange(rowmap,&rstart,0);CHKERRQ(ierr);
279251dd7536SBarry Smith   ierr = PetscMapDestroy(rowmap);CHKERRQ(ierr);
279351dd7536SBarry Smith 
279451dd7536SBarry Smith   ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr);
2795c5d6d63eSBarry Smith   for (i=0;i<m;i++) {
279651dd7536SBarry Smith     ierr = MatGetRow(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
279751dd7536SBarry Smith     ierr = MatPreallocateSet(i+rstart,nnz,indx,dnz,onz);CHKERRQ(ierr);
279851dd7536SBarry Smith     ierr = MatRestoreRow(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
2799c5d6d63eSBarry Smith   }
2800f204ca49SKris Buschelman   /* This routine will ONLY return MPIAIJ type matrix */
2801f204ca49SKris Buschelman   ierr = MatCreate(comm,m,n,PETSC_DETERMINE,PETSC_DETERMINE,outmat);CHKERRQ(ierr);
2802f204ca49SKris Buschelman   ierr = MatSetType(*outmat,MATMPIAIJ);CHKERRQ(ierr);
2803f204ca49SKris Buschelman   ierr = MatMPIAIJSetPreallocation(*outmat,0,dnz,0,onz);CHKERRQ(ierr);
280451dd7536SBarry Smith   ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr);
2805c5d6d63eSBarry Smith 
280651dd7536SBarry Smith   for (i=0;i<m;i++) {
280751dd7536SBarry Smith     ierr = MatGetRow(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
280851dd7536SBarry Smith     I    = i + rstart;
280951dd7536SBarry Smith     ierr = MatSetValues(*outmat,1,&I,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr);
281051dd7536SBarry Smith     ierr = MatRestoreRow(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
281151dd7536SBarry Smith   }
281251dd7536SBarry Smith   ierr = MatDestroy(inmat);CHKERRQ(ierr);
281351dd7536SBarry Smith   ierr = MatAssemblyBegin(*outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
281451dd7536SBarry Smith   ierr = MatAssemblyEnd(*outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2815d6bb3c2dSHong Zhang #endif
2816c5d6d63eSBarry Smith   PetscFunctionReturn(0);
2817c5d6d63eSBarry Smith }
2818c5d6d63eSBarry Smith 
2819c5d6d63eSBarry Smith #undef __FUNCT__
2820c5d6d63eSBarry Smith #define __FUNCT__ "MatFileSplit"
2821dfbe8321SBarry Smith PetscErrorCode MatFileSplit(Mat A,char *outfile)
2822c5d6d63eSBarry Smith {
2823dfbe8321SBarry Smith   PetscErrorCode ierr;
2824dfbe8321SBarry Smith   int               rank,m,N,i,rstart,nnz;
2825de4209c5SBarry Smith   size_t            len;
2826b3cc6726SBarry Smith   const int         *indx;
2827c5d6d63eSBarry Smith   PetscViewer       out;
2828c5d6d63eSBarry Smith   char              *name;
2829c5d6d63eSBarry Smith   Mat               B;
2830b3cc6726SBarry Smith   const PetscScalar *values;
2831c5d6d63eSBarry Smith 
2832c5d6d63eSBarry Smith   PetscFunctionBegin;
2833c5d6d63eSBarry Smith 
2834c5d6d63eSBarry Smith   ierr = MatGetLocalSize(A,&m,0);CHKERRQ(ierr);
2835c5d6d63eSBarry Smith   ierr = MatGetSize(A,0,&N);CHKERRQ(ierr);
2836f204ca49SKris Buschelman   /* Should this be the type of the diagonal block of A? */
2837f204ca49SKris Buschelman   ierr = MatCreate(PETSC_COMM_SELF,m,N,m,N,&B);CHKERRQ(ierr);
2838f204ca49SKris Buschelman   ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
2839f204ca49SKris Buschelman   ierr = MatSeqAIJSetPreallocation(B,0,PETSC_NULL);CHKERRQ(ierr);
2840c5d6d63eSBarry Smith   ierr = MatGetOwnershipRange(A,&rstart,0);CHKERRQ(ierr);
2841c5d6d63eSBarry Smith   for (i=0;i<m;i++) {
2842c5d6d63eSBarry Smith     ierr = MatGetRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr);
2843c5d6d63eSBarry Smith     ierr = MatSetValues(B,1,&i,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr);
2844c5d6d63eSBarry Smith     ierr = MatRestoreRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr);
2845c5d6d63eSBarry Smith   }
2846c5d6d63eSBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2847c5d6d63eSBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2848c5d6d63eSBarry Smith 
2849c5d6d63eSBarry Smith   ierr = MPI_Comm_rank(A->comm,&rank);CHKERRQ(ierr);
2850c5d6d63eSBarry Smith   ierr = PetscStrlen(outfile,&len);CHKERRQ(ierr);
2851c5d6d63eSBarry Smith   ierr = PetscMalloc((len+5)*sizeof(char),&name);CHKERRQ(ierr);
2852c5d6d63eSBarry Smith   sprintf(name,"%s.%d",outfile,rank);
285345f7d322SBarry Smith   ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,name,PETSC_FILE_CREATE,&out);CHKERRQ(ierr);
2854c5d6d63eSBarry Smith   ierr = PetscFree(name);
2855c5d6d63eSBarry Smith   ierr = MatView(B,out);CHKERRQ(ierr);
2856c5d6d63eSBarry Smith   ierr = PetscViewerDestroy(out);CHKERRQ(ierr);
2857c5d6d63eSBarry Smith   ierr = MatDestroy(B);CHKERRQ(ierr);
2858c5d6d63eSBarry Smith   PetscFunctionReturn(0);
2859c5d6d63eSBarry Smith }
2860