xref: /petsc/src/mat/impls/aij/mpi/mpiaij.c (revision 973046187a5e16e2b37c23287137a5e6ee1ae012)
173f4d377SMatthew Knepley /*$Id: mpiaij.c,v 1.344 2001/08/10 03:30:48 bsmith Exp $*/
28a729477SBarry Smith 
370f55243SBarry Smith #include "src/mat/impls/aij/mpi/mpiaij.h"
4f5eb4b81SSatish Balay #include "src/vec/vecimpl.h"
5d9942c19SSatish Balay #include "src/inline/spops.h"
68a729477SBarry Smith 
70f5bd95cSBarry Smith /*
80f5bd95cSBarry Smith   Local utility routine that creates a mapping from the global column
99e25ed09SBarry Smith number to the local number in the off-diagonal part of the local
100f5bd95cSBarry Smith storage of the matrix.  When PETSC_USE_CTABLE is used this is scalable at
110f5bd95cSBarry Smith a slightly higher hash table cost; without it it is not scalable (each processor
120f5bd95cSBarry Smith has an order N integer array but is fast to acess.
139e25ed09SBarry Smith */
144a2ae208SSatish Balay #undef __FUNCT__
154a2ae208SSatish Balay #define __FUNCT__ "CreateColmap_MPIAIJ_Private"
160a198c4cSBarry Smith int CreateColmap_MPIAIJ_Private(Mat mat)
179e25ed09SBarry Smith {
1844a69424SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
19273d9f13SBarry Smith   int        n = aij->B->n,i,ierr;
20dbb450caSBarry Smith 
213a40ed3dSBarry Smith   PetscFunctionBegin;
22aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
23273d9f13SBarry Smith   ierr = PetscTableCreate(n,&aij->colmap);CHKERRQ(ierr);
24b1fc9764SSatish Balay   for (i=0; i<n; i++){
250f5bd95cSBarry Smith     ierr = PetscTableAdd(aij->colmap,aij->garray[i]+1,i+1);CHKERRQ(ierr);
26b1fc9764SSatish Balay   }
27b1fc9764SSatish Balay #else
28b0a32e0cSBarry Smith   ierr = PetscMalloc((mat->N+1)*sizeof(int),&aij->colmap);CHKERRQ(ierr);
29b0a32e0cSBarry Smith   PetscLogObjectMemory(mat,mat->N*sizeof(int));
30273d9f13SBarry Smith   ierr = PetscMemzero(aij->colmap,mat->N*sizeof(int));CHKERRQ(ierr);
31905e6a2fSBarry Smith   for (i=0; i<n; i++) aij->colmap[aij->garray[i]] = i+1;
32b1fc9764SSatish Balay #endif
333a40ed3dSBarry Smith   PetscFunctionReturn(0);
349e25ed09SBarry Smith }
359e25ed09SBarry Smith 
360520107fSSatish Balay #define CHUNKSIZE   15
3730770e4dSSatish Balay #define MatSetValues_SeqAIJ_A_Private(row,col,value,addv) \
380520107fSSatish Balay { \
390520107fSSatish Balay  \
400520107fSSatish Balay     rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift; \
4130770e4dSSatish Balay     rmax = aimax[row]; nrow = ailen[row];  \
42f5e9677aSSatish Balay     col1 = col - shift; \
43f5e9677aSSatish Balay      \
44ba4e3ef2SSatish Balay     low = 0; high = nrow; \
45ba4e3ef2SSatish Balay     while (high-low > 5) { \
46ba4e3ef2SSatish Balay       t = (low+high)/2; \
47ba4e3ef2SSatish Balay       if (rp[t] > col) high = t; \
48ba4e3ef2SSatish Balay       else             low  = t; \
49ba4e3ef2SSatish Balay     } \
502335bdd2SSatish Balay       for (_i=low; _i<high; _i++) { \
51f5e9677aSSatish Balay         if (rp[_i] > col1) break; \
52f5e9677aSSatish Balay         if (rp[_i] == col1) { \
530520107fSSatish Balay           if (addv == ADD_VALUES) ap[_i] += value;   \
540520107fSSatish Balay           else                  ap[_i] = value; \
5530770e4dSSatish Balay           goto a_noinsert; \
560520107fSSatish Balay         } \
570520107fSSatish Balay       }  \
5889280ab3SLois Curfman McInnes       if (nonew == 1) goto a_noinsert; \
5929bbc08cSBarry Smith       else if (nonew == -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero into matrix"); \
600520107fSSatish Balay       if (nrow >= rmax) { \
610520107fSSatish Balay         /* there is no extra room in row, therefore enlarge */ \
62273d9f13SBarry Smith         int    new_nz = ai[am] + CHUNKSIZE,len,*new_i,*new_j; \
6387828ca2SBarry Smith         PetscScalar *new_a; \
640520107fSSatish Balay  \
6529bbc08cSBarry Smith         if (nonew == -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix"); \
6689280ab3SLois Curfman McInnes  \
670520107fSSatish Balay         /* malloc new storage space */ \
6887828ca2SBarry Smith         len     = new_nz*(sizeof(int)+sizeof(PetscScalar))+(am+1)*sizeof(int); \
69b0a32e0cSBarry Smith         ierr    = PetscMalloc(len,&new_a);CHKERRQ(ierr); \
700520107fSSatish Balay         new_j   = (int*)(new_a + new_nz); \
710520107fSSatish Balay         new_i   = new_j + new_nz; \
720520107fSSatish Balay  \
730520107fSSatish Balay         /* copy over old data into new slots */ \
740520107fSSatish Balay         for (ii=0; ii<row+1; ii++) {new_i[ii] = ai[ii];} \
75273d9f13SBarry Smith         for (ii=row+1; ii<am+1; ii++) {new_i[ii] = ai[ii]+CHUNKSIZE;} \
76549d3d68SSatish Balay         ierr = PetscMemcpy(new_j,aj,(ai[row]+nrow+shift)*sizeof(int));CHKERRQ(ierr); \
770520107fSSatish Balay         len = (new_nz - CHUNKSIZE - ai[row] - nrow - shift); \
78549d3d68SSatish Balay         ierr = PetscMemcpy(new_j+ai[row]+shift+nrow+CHUNKSIZE,aj+ai[row]+shift+nrow, \
79549d3d68SSatish Balay                                                            len*sizeof(int));CHKERRQ(ierr); \
8087828ca2SBarry Smith         ierr = PetscMemcpy(new_a,aa,(ai[row]+nrow+shift)*sizeof(PetscScalar));CHKERRQ(ierr); \
81549d3d68SSatish Balay         ierr = PetscMemcpy(new_a+ai[row]+shift+nrow+CHUNKSIZE,aa+ai[row]+shift+nrow, \
8287828ca2SBarry Smith                                                            len*sizeof(PetscScalar));CHKERRQ(ierr);  \
830520107fSSatish Balay         /* free up old matrix storage */ \
84f5e9677aSSatish Balay  \
85606d414cSSatish Balay         ierr = PetscFree(a->a);CHKERRQ(ierr);  \
86606d414cSSatish Balay         if (!a->singlemalloc) { \
87606d414cSSatish Balay            ierr = PetscFree(a->i);CHKERRQ(ierr); \
88606d414cSSatish Balay            ierr = PetscFree(a->j);CHKERRQ(ierr); \
89606d414cSSatish Balay         } \
900520107fSSatish Balay         aa = a->a = new_a; ai = a->i = new_i; aj = a->j = new_j;  \
917c922b88SBarry Smith         a->singlemalloc = PETSC_TRUE; \
920520107fSSatish Balay  \
930520107fSSatish Balay         rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift; \
9430770e4dSSatish Balay         rmax = aimax[row] = aimax[row] + CHUNKSIZE; \
9587828ca2SBarry Smith         PetscLogObjectMemory(A,CHUNKSIZE*(sizeof(int) + sizeof(PetscScalar))); \
960520107fSSatish Balay         a->maxnz += CHUNKSIZE; \
970520107fSSatish Balay         a->reallocs++; \
980520107fSSatish Balay       } \
990520107fSSatish Balay       N = nrow++ - 1; a->nz++; \
1000520107fSSatish Balay       /* shift up all the later entries in this row */ \
1010520107fSSatish Balay       for (ii=N; ii>=_i; ii--) { \
1020520107fSSatish Balay         rp[ii+1] = rp[ii]; \
1030520107fSSatish Balay         ap[ii+1] = ap[ii]; \
1040520107fSSatish Balay       } \
105f5e9677aSSatish Balay       rp[_i] = col1;  \
1060520107fSSatish Balay       ap[_i] = value;  \
10730770e4dSSatish Balay       a_noinsert: ; \
1080520107fSSatish Balay       ailen[row] = nrow; \
1090520107fSSatish Balay }
1100a198c4cSBarry Smith 
11130770e4dSSatish Balay #define MatSetValues_SeqAIJ_B_Private(row,col,value,addv) \
11230770e4dSSatish Balay { \
11330770e4dSSatish Balay  \
11430770e4dSSatish Balay     rp   = bj + bi[row] + shift; ap = ba + bi[row] + shift; \
11530770e4dSSatish Balay     rmax = bimax[row]; nrow = bilen[row];  \
11630770e4dSSatish Balay     col1 = col - shift; \
11730770e4dSSatish Balay      \
118ba4e3ef2SSatish Balay     low = 0; high = nrow; \
119ba4e3ef2SSatish Balay     while (high-low > 5) { \
120ba4e3ef2SSatish Balay       t = (low+high)/2; \
121ba4e3ef2SSatish Balay       if (rp[t] > col) high = t; \
122ba4e3ef2SSatish Balay       else             low  = t; \
123ba4e3ef2SSatish Balay     } \
1242335bdd2SSatish Balay        for (_i=low; _i<high; _i++) { \
12530770e4dSSatish Balay         if (rp[_i] > col1) break; \
12630770e4dSSatish Balay         if (rp[_i] == col1) { \
12730770e4dSSatish Balay           if (addv == ADD_VALUES) ap[_i] += value;   \
12830770e4dSSatish Balay           else                  ap[_i] = value; \
12930770e4dSSatish Balay           goto b_noinsert; \
13030770e4dSSatish Balay         } \
13130770e4dSSatish Balay       }  \
13289280ab3SLois Curfman McInnes       if (nonew == 1) goto b_noinsert; \
13329bbc08cSBarry Smith       else if (nonew == -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero into matrix"); \
13430770e4dSSatish Balay       if (nrow >= rmax) { \
13530770e4dSSatish Balay         /* there is no extra room in row, therefore enlarge */ \
136273d9f13SBarry Smith         int    new_nz = bi[bm] + CHUNKSIZE,len,*new_i,*new_j; \
13787828ca2SBarry Smith         PetscScalar *new_a; \
13830770e4dSSatish Balay  \
13929bbc08cSBarry Smith         if (nonew == -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix"); \
14089280ab3SLois Curfman McInnes  \
14130770e4dSSatish Balay         /* malloc new storage space */ \
14287828ca2SBarry Smith         len     = new_nz*(sizeof(int)+sizeof(PetscScalar))+(bm+1)*sizeof(int); \
143b0a32e0cSBarry Smith         ierr    = PetscMalloc(len,&new_a);CHKERRQ(ierr); \
14430770e4dSSatish Balay         new_j   = (int*)(new_a + new_nz); \
14530770e4dSSatish Balay         new_i   = new_j + new_nz; \
14630770e4dSSatish Balay  \
14730770e4dSSatish Balay         /* copy over old data into new slots */ \
14830770e4dSSatish Balay         for (ii=0; ii<row+1; ii++) {new_i[ii] = bi[ii];} \
149273d9f13SBarry Smith         for (ii=row+1; ii<bm+1; ii++) {new_i[ii] = bi[ii]+CHUNKSIZE;} \
150549d3d68SSatish Balay         ierr = PetscMemcpy(new_j,bj,(bi[row]+nrow+shift)*sizeof(int));CHKERRQ(ierr); \
15130770e4dSSatish Balay         len = (new_nz - CHUNKSIZE - bi[row] - nrow - shift); \
152549d3d68SSatish Balay         ierr = PetscMemcpy(new_j+bi[row]+shift+nrow+CHUNKSIZE,bj+bi[row]+shift+nrow, \
153549d3d68SSatish Balay                                                            len*sizeof(int));CHKERRQ(ierr); \
15487828ca2SBarry Smith         ierr = PetscMemcpy(new_a,ba,(bi[row]+nrow+shift)*sizeof(PetscScalar));CHKERRQ(ierr); \
155549d3d68SSatish Balay         ierr = PetscMemcpy(new_a+bi[row]+shift+nrow+CHUNKSIZE,ba+bi[row]+shift+nrow, \
15687828ca2SBarry Smith                                                            len*sizeof(PetscScalar));CHKERRQ(ierr);  \
15730770e4dSSatish Balay         /* free up old matrix storage */ \
15830770e4dSSatish Balay  \
159606d414cSSatish Balay         ierr = PetscFree(b->a);CHKERRQ(ierr);  \
160606d414cSSatish Balay         if (!b->singlemalloc) { \
161606d414cSSatish Balay           ierr = PetscFree(b->i);CHKERRQ(ierr); \
162606d414cSSatish Balay           ierr = PetscFree(b->j);CHKERRQ(ierr); \
163606d414cSSatish Balay         } \
16474c639caSSatish Balay         ba = b->a = new_a; bi = b->i = new_i; bj = b->j = new_j;  \
1657c922b88SBarry Smith         b->singlemalloc = PETSC_TRUE; \
16630770e4dSSatish Balay  \
16730770e4dSSatish Balay         rp   = bj + bi[row] + shift; ap = ba + bi[row] + shift; \
16830770e4dSSatish Balay         rmax = bimax[row] = bimax[row] + CHUNKSIZE; \
16987828ca2SBarry Smith         PetscLogObjectMemory(B,CHUNKSIZE*(sizeof(int) + sizeof(PetscScalar))); \
17074c639caSSatish Balay         b->maxnz += CHUNKSIZE; \
17174c639caSSatish Balay         b->reallocs++; \
17230770e4dSSatish Balay       } \
17374c639caSSatish Balay       N = nrow++ - 1; b->nz++; \
17430770e4dSSatish Balay       /* shift up all the later entries in this row */ \
17530770e4dSSatish Balay       for (ii=N; ii>=_i; ii--) { \
17630770e4dSSatish Balay         rp[ii+1] = rp[ii]; \
17730770e4dSSatish Balay         ap[ii+1] = ap[ii]; \
17830770e4dSSatish Balay       } \
17930770e4dSSatish Balay       rp[_i] = col1;  \
18030770e4dSSatish Balay       ap[_i] = value;  \
18130770e4dSSatish Balay       b_noinsert: ; \
18230770e4dSSatish Balay       bilen[row] = nrow; \
18330770e4dSSatish Balay }
18430770e4dSSatish Balay 
1854a2ae208SSatish Balay #undef __FUNCT__
1864a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_MPIAIJ"
187f15d580aSBarry Smith int MatSetValues_MPIAIJ(Mat mat,int m,const int im[],int n,const int in[],const PetscScalar v[],InsertMode addv)
1888a729477SBarry Smith {
18944a69424SLois Curfman McInnes   Mat_MPIAIJ   *aij = (Mat_MPIAIJ*)mat->data;
19087828ca2SBarry Smith   PetscScalar  value;
1911eb62cbbSBarry Smith   int          ierr,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];
222329f5518SBarry 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];
251329f5518SBarry 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) {
259329f5518SBarry 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 {
262329f5518SBarry 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"
273f15d580aSBarry Smith int 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;
276b49de8d1SLois Curfman McInnes   int        ierr,i,j,rstart = aij->rstart,rend = aij->rend;
277b49de8d1SLois Curfman McInnes   int        cstart = aij->cstart,cend = aij->cend,row,col;
278b49de8d1SLois Curfman McInnes 
2793a40ed3dSBarry Smith   PetscFunctionBegin;
280b49de8d1SLois Curfman McInnes   for (i=0; i<m; i++) {
281590ac198SBarry Smith     if (idxm[i] < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %d",idxm[i]);
282952d7e9fSSatish Balay     if (idxm[i] >= mat->M) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %d max %d",idxm[i],mat->M-1);
283b49de8d1SLois Curfman McInnes     if (idxm[i] >= rstart && idxm[i] < rend) {
284b49de8d1SLois Curfman McInnes       row = idxm[i] - rstart;
285b49de8d1SLois Curfman McInnes       for (j=0; j<n; j++) {
286590ac198SBarry Smith         if (idxn[j] < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %d",idxn[j]);
287590ac198SBarry Smith         if (idxn[j] >= mat->N) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %d max %d",idxn[j],mat->N-1);
288b49de8d1SLois Curfman McInnes         if (idxn[j] >= cstart && idxn[j] < cend){
289b49de8d1SLois Curfman McInnes           col = idxn[j] - cstart;
290b49de8d1SLois Curfman McInnes           ierr = MatGetValues(aij->A,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
291fa852ad4SSatish Balay         } else {
292905e6a2fSBarry Smith           if (!aij->colmap) {
293905e6a2fSBarry Smith             ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
294905e6a2fSBarry Smith           }
295aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
2960f5bd95cSBarry Smith           ierr = PetscTableFind(aij->colmap,idxn[j]+1,&col);CHKERRQ(ierr);
297fa46199cSSatish Balay           col --;
298b1fc9764SSatish Balay #else
299905e6a2fSBarry Smith           col = aij->colmap[idxn[j]] - 1;
300b1fc9764SSatish Balay #endif
301e60e1c95SSatish Balay           if ((col < 0) || (aij->garray[col] != idxn[j])) *(v+i*n+j) = 0.0;
302d9d09a02SSatish Balay           else {
303b49de8d1SLois Curfman McInnes             ierr = MatGetValues(aij->B,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
304b49de8d1SLois Curfman McInnes           }
305b49de8d1SLois Curfman McInnes         }
306b49de8d1SLois Curfman McInnes       }
307a8c6a408SBarry Smith     } else {
30829bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"Only local values currently supported");
309b49de8d1SLois Curfman McInnes     }
310b49de8d1SLois Curfman McInnes   }
3113a40ed3dSBarry Smith   PetscFunctionReturn(0);
312b49de8d1SLois Curfman McInnes }
313bc5ccf88SSatish Balay 
3144a2ae208SSatish Balay #undef __FUNCT__
3154a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyBegin_MPIAIJ"
316bc5ccf88SSatish Balay int MatAssemblyBegin_MPIAIJ(Mat mat,MatAssemblyType mode)
317bc5ccf88SSatish Balay {
318bc5ccf88SSatish Balay   Mat_MPIAIJ  *aij = (Mat_MPIAIJ*)mat->data;
319d36fbae8SSatish Balay   int         ierr,nstash,reallocs;
320bc5ccf88SSatish Balay   InsertMode  addv;
321bc5ccf88SSatish Balay 
322bc5ccf88SSatish Balay   PetscFunctionBegin;
323bc5ccf88SSatish Balay   if (aij->donotstash) {
324bc5ccf88SSatish Balay     PetscFunctionReturn(0);
325bc5ccf88SSatish Balay   }
326bc5ccf88SSatish Balay 
327bc5ccf88SSatish Balay   /* make sure all processors are either in INSERTMODE or ADDMODE */
328bc5ccf88SSatish Balay   ierr = MPI_Allreduce(&mat->insertmode,&addv,1,MPI_INT,MPI_BOR,mat->comm);CHKERRQ(ierr);
329bc5ccf88SSatish Balay   if (addv == (ADD_VALUES|INSERT_VALUES)) {
33029bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Some processors inserted others added");
331bc5ccf88SSatish Balay   }
332bc5ccf88SSatish Balay   mat->insertmode = addv; /* in case this processor had no cache */
333bc5ccf88SSatish Balay 
3348798bf22SSatish Balay   ierr = MatStashScatterBegin_Private(&mat->stash,aij->rowners);CHKERRQ(ierr);
3358798bf22SSatish Balay   ierr = MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);CHKERRQ(ierr);
336b0a32e0cSBarry Smith   PetscLogInfo(aij->A,"MatAssemblyBegin_MPIAIJ:Stash has %d entries, uses %d mallocs.\n",nstash,reallocs);
337bc5ccf88SSatish Balay   PetscFunctionReturn(0);
338bc5ccf88SSatish Balay }
339bc5ccf88SSatish Balay 
340bc5ccf88SSatish Balay 
3414a2ae208SSatish Balay #undef __FUNCT__
3424a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_MPIAIJ"
343bc5ccf88SSatish Balay int MatAssemblyEnd_MPIAIJ(Mat mat,MatAssemblyType mode)
344bc5ccf88SSatish Balay {
345bc5ccf88SSatish Balay   Mat_MPIAIJ  *aij = (Mat_MPIAIJ*)mat->data;
34624f910e3SHong Zhang   Mat_SeqAIJ  *a=(Mat_SeqAIJ *)aij->A->data,*b= (Mat_SeqAIJ *)aij->B->data;
347a2d1c673SSatish Balay   int         i,j,rstart,ncols,n,ierr,flg;
348bc5ccf88SSatish Balay   int         *row,*col,other_disassembled;
34987828ca2SBarry Smith   PetscScalar *val;
350bc5ccf88SSatish Balay   InsertMode  addv = mat->insertmode;
351bc5ccf88SSatish Balay 
352bc5ccf88SSatish Balay   PetscFunctionBegin;
353bc5ccf88SSatish Balay   if (!aij->donotstash) {
354a2d1c673SSatish Balay     while (1) {
3558798bf22SSatish Balay       ierr = MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);CHKERRQ(ierr);
356a2d1c673SSatish Balay       if (!flg) break;
357a2d1c673SSatish Balay 
358bc5ccf88SSatish Balay       for (i=0; i<n;) {
359bc5ccf88SSatish Balay         /* Now identify the consecutive vals belonging to the same row */
360bc5ccf88SSatish Balay         for (j=i,rstart=row[j]; j<n; j++) { if (row[j] != rstart) break; }
361bc5ccf88SSatish Balay         if (j < n) ncols = j-i;
362bc5ccf88SSatish Balay         else       ncols = n-i;
363bc5ccf88SSatish Balay         /* Now assemble all these values with a single function call */
364bc5ccf88SSatish Balay         ierr = MatSetValues_MPIAIJ(mat,1,row+i,ncols,col+i,val+i,addv);CHKERRQ(ierr);
365bc5ccf88SSatish Balay         i = j;
366bc5ccf88SSatish Balay       }
367bc5ccf88SSatish Balay     }
3688798bf22SSatish Balay     ierr = MatStashScatterEnd_Private(&mat->stash);CHKERRQ(ierr);
369bc5ccf88SSatish Balay   }
370bc5ccf88SSatish Balay 
371bc5ccf88SSatish Balay   ierr = MatAssemblyBegin(aij->A,mode);CHKERRQ(ierr);
372bc5ccf88SSatish Balay   ierr = MatAssemblyEnd(aij->A,mode);CHKERRQ(ierr);
373bc5ccf88SSatish Balay 
374bc5ccf88SSatish Balay   /* determine if any processor has disassembled, if so we must
375bc5ccf88SSatish Balay      also disassemble ourselfs, in order that we may reassemble. */
376bc5ccf88SSatish Balay   /*
377bc5ccf88SSatish Balay      if nonzero structure of submatrix B cannot change then we know that
378bc5ccf88SSatish Balay      no processor disassembled thus we can skip this stuff
379bc5ccf88SSatish Balay   */
380bc5ccf88SSatish Balay   if (!((Mat_SeqAIJ*)aij->B->data)->nonew)  {
381bc5ccf88SSatish Balay     ierr = MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPI_INT,MPI_PROD,mat->comm);CHKERRQ(ierr);
382bc5ccf88SSatish Balay     if (mat->was_assembled && !other_disassembled) {
383bc5ccf88SSatish Balay       ierr = DisAssemble_MPIAIJ(mat);CHKERRQ(ierr);
384bc5ccf88SSatish Balay     }
385bc5ccf88SSatish Balay   }
386bc5ccf88SSatish Balay 
387bc5ccf88SSatish Balay   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
388bc5ccf88SSatish Balay     ierr = MatSetUpMultiply_MPIAIJ(mat);CHKERRQ(ierr);
389bc5ccf88SSatish Balay   }
390bc5ccf88SSatish Balay   ierr = MatAssemblyBegin(aij->B,mode);CHKERRQ(ierr);
391bc5ccf88SSatish Balay   ierr = MatAssemblyEnd(aij->B,mode);CHKERRQ(ierr);
392bc5ccf88SSatish Balay 
393606d414cSSatish Balay   if (aij->rowvalues) {
394606d414cSSatish Balay     ierr = PetscFree(aij->rowvalues);CHKERRQ(ierr);
395606d414cSSatish Balay     aij->rowvalues = 0;
396606d414cSSatish Balay   }
397a30b2313SHong Zhang 
398a30b2313SHong Zhang   /* used by MatAXPY() */
399a30b2313SHong Zhang   a->xtoy = 0; b->xtoy = 0;
400a30b2313SHong Zhang   a->XtoY = 0; b->XtoY = 0;
401a30b2313SHong Zhang 
402bc5ccf88SSatish Balay   PetscFunctionReturn(0);
403bc5ccf88SSatish Balay }
404bc5ccf88SSatish Balay 
4054a2ae208SSatish Balay #undef __FUNCT__
4064a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_MPIAIJ"
4078f6be9afSLois Curfman McInnes int MatZeroEntries_MPIAIJ(Mat A)
4081eb62cbbSBarry Smith {
40944a69424SLois Curfman McInnes   Mat_MPIAIJ *l = (Mat_MPIAIJ*)A->data;
410dbd7a890SLois Curfman McInnes   int        ierr;
4113a40ed3dSBarry Smith 
4123a40ed3dSBarry Smith   PetscFunctionBegin;
41378b31e54SBarry Smith   ierr = MatZeroEntries(l->A);CHKERRQ(ierr);
41478b31e54SBarry Smith   ierr = MatZeroEntries(l->B);CHKERRQ(ierr);
4153a40ed3dSBarry Smith   PetscFunctionReturn(0);
4161eb62cbbSBarry Smith }
4171eb62cbbSBarry Smith 
4184a2ae208SSatish Balay #undef __FUNCT__
4194a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_MPIAIJ"
420268466fbSBarry Smith int MatZeroRows_MPIAIJ(Mat A,IS is,const PetscScalar *diag)
4211eb62cbbSBarry Smith {
42244a69424SLois Curfman McInnes   Mat_MPIAIJ     *l = (Mat_MPIAIJ*)A->data;
42317699dbbSLois Curfman McInnes   int            i,ierr,N,*rows,*owners = l->rowners,size = l->size;
424c1dc657dSBarry Smith   int            *nprocs,j,idx,nsends,row;
42517699dbbSLois Curfman McInnes   int            nmax,*svalues,*starts,*owner,nrecvs,rank = l->rank;
4265392566eSBarry Smith   int            *rvalues,tag = A->tag,count,base,slen,n,*source;
4276a5c57faSSatish Balay   int            *lens,imdex,*lrows,*values,rstart=l->rstart;
428d6dfbf8fSBarry Smith   MPI_Comm       comm = A->comm;
4291eb62cbbSBarry Smith   MPI_Request    *send_waits,*recv_waits;
4301eb62cbbSBarry Smith   MPI_Status     recv_status,*send_status;
4311eb62cbbSBarry Smith   IS             istmp;
43235d8aa7fSBarry Smith   PetscTruth     found;
4331eb62cbbSBarry Smith 
4343a40ed3dSBarry Smith   PetscFunctionBegin;
435e03a110bSBarry Smith   ierr = ISGetLocalSize(is,&N);CHKERRQ(ierr);
43678b31e54SBarry Smith   ierr = ISGetIndices(is,&rows);CHKERRQ(ierr);
4371eb62cbbSBarry Smith 
4381eb62cbbSBarry Smith   /*  first count number of contributors to each processor */
439b0a32e0cSBarry Smith   ierr = PetscMalloc(2*size*sizeof(int),&nprocs);CHKERRQ(ierr);
440549d3d68SSatish Balay   ierr   = PetscMemzero(nprocs,2*size*sizeof(int));CHKERRQ(ierr);
441b0a32e0cSBarry Smith   ierr = PetscMalloc((N+1)*sizeof(int),&owner);CHKERRQ(ierr); /* see note*/
4421eb62cbbSBarry Smith   for (i=0; i<N; i++) {
4431eb62cbbSBarry Smith     idx = rows[i];
44435d8aa7fSBarry Smith     found = PETSC_FALSE;
44517699dbbSLois Curfman McInnes     for (j=0; j<size; j++) {
4461eb62cbbSBarry Smith       if (idx >= owners[j] && idx < owners[j+1]) {
447c1dc657dSBarry Smith         nprocs[2*j]++; nprocs[2*j+1] = 1; owner[i] = j; found = PETSC_TRUE; break;
4481eb62cbbSBarry Smith       }
4491eb62cbbSBarry Smith     }
45029bbc08cSBarry Smith     if (!found) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Index out of range");
4511eb62cbbSBarry Smith   }
452c1dc657dSBarry Smith   nsends = 0;  for (i=0; i<size; i++) { nsends += nprocs[2*i+1];}
4531eb62cbbSBarry Smith 
4541eb62cbbSBarry Smith   /* inform other processors of number of messages and max length*/
455c1dc657dSBarry Smith   ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr);
4561eb62cbbSBarry Smith 
4571eb62cbbSBarry Smith   /* post receives:   */
458b0a32e0cSBarry Smith   ierr = PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(int),&rvalues);CHKERRQ(ierr);
459b0a32e0cSBarry Smith   ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr);
4601eb62cbbSBarry Smith   for (i=0; i<nrecvs; i++) {
461ca161407SBarry Smith     ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPI_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr);
4621eb62cbbSBarry Smith   }
4631eb62cbbSBarry Smith 
4641eb62cbbSBarry Smith   /* do sends:
4651eb62cbbSBarry Smith       1) starts[i] gives the starting index in svalues for stuff going to
4661eb62cbbSBarry Smith          the ith processor
4671eb62cbbSBarry Smith   */
468b0a32e0cSBarry Smith   ierr = PetscMalloc((N+1)*sizeof(int),&svalues);CHKERRQ(ierr);
469b0a32e0cSBarry Smith   ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr);
470b0a32e0cSBarry Smith   ierr = PetscMalloc((size+1)*sizeof(int),&starts);CHKERRQ(ierr);
4711eb62cbbSBarry Smith   starts[0] = 0;
472c1dc657dSBarry Smith   for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
4731eb62cbbSBarry Smith   for (i=0; i<N; i++) {
4741eb62cbbSBarry Smith     svalues[starts[owner[i]]++] = rows[i];
4751eb62cbbSBarry Smith   }
4766831982aSBarry Smith   ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr);
4771eb62cbbSBarry Smith 
4781eb62cbbSBarry Smith   starts[0] = 0;
479c1dc657dSBarry Smith   for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
4801eb62cbbSBarry Smith   count = 0;
48117699dbbSLois Curfman McInnes   for (i=0; i<size; i++) {
482c1dc657dSBarry Smith     if (nprocs[2*i+1]) {
483c1dc657dSBarry Smith       ierr = MPI_Isend(svalues+starts[i],nprocs[2*i],MPI_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr);
4841eb62cbbSBarry Smith     }
4851eb62cbbSBarry Smith   }
486606d414cSSatish Balay   ierr = PetscFree(starts);CHKERRQ(ierr);
4871eb62cbbSBarry Smith 
48817699dbbSLois Curfman McInnes   base = owners[rank];
4891eb62cbbSBarry Smith 
4901eb62cbbSBarry Smith   /*  wait on receives */
491b0a32e0cSBarry Smith   ierr   = PetscMalloc(2*(nrecvs+1)*sizeof(int),&lens);CHKERRQ(ierr);
4921eb62cbbSBarry Smith   source = lens + nrecvs;
4931eb62cbbSBarry Smith   count  = nrecvs; slen = 0;
4941eb62cbbSBarry Smith   while (count) {
495ca161407SBarry Smith     ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
4961eb62cbbSBarry Smith     /* unpack receives into our local space */
497ca161407SBarry Smith     ierr = MPI_Get_count(&recv_status,MPI_INT,&n);CHKERRQ(ierr);
498d6dfbf8fSBarry Smith     source[imdex]  = recv_status.MPI_SOURCE;
499d6dfbf8fSBarry Smith     lens[imdex]    = n;
5001eb62cbbSBarry Smith     slen          += n;
5011eb62cbbSBarry Smith     count--;
5021eb62cbbSBarry Smith   }
503606d414cSSatish Balay   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
5041eb62cbbSBarry Smith 
5051eb62cbbSBarry Smith   /* move the data into the send scatter */
506b0a32e0cSBarry Smith   ierr = PetscMalloc((slen+1)*sizeof(int),&lrows);CHKERRQ(ierr);
5071eb62cbbSBarry Smith   count = 0;
5081eb62cbbSBarry Smith   for (i=0; i<nrecvs; i++) {
5091eb62cbbSBarry Smith     values = rvalues + i*nmax;
5101eb62cbbSBarry Smith     for (j=0; j<lens[i]; j++) {
5111eb62cbbSBarry Smith       lrows[count++] = values[j] - base;
5121eb62cbbSBarry Smith     }
5131eb62cbbSBarry Smith   }
514606d414cSSatish Balay   ierr = PetscFree(rvalues);CHKERRQ(ierr);
515606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
516606d414cSSatish Balay   ierr = PetscFree(owner);CHKERRQ(ierr);
517606d414cSSatish Balay   ierr = PetscFree(nprocs);CHKERRQ(ierr);
5181eb62cbbSBarry Smith 
5191eb62cbbSBarry Smith   /* actually zap the local rows */
520029af93fSBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,slen,lrows,&istmp);CHKERRQ(ierr);
521b0a32e0cSBarry Smith   PetscLogObjectParent(A,istmp);
5226a5c57faSSatish Balay 
5236eb55b6aSBarry Smith   /*
5246eb55b6aSBarry Smith         Zero the required rows. If the "diagonal block" of the matrix
5256eb55b6aSBarry Smith      is square and the user wishes to set the diagonal we use seperate
5266eb55b6aSBarry Smith      code so that MatSetValues() is not called for each diagonal allocating
5276eb55b6aSBarry Smith      new memory, thus calling lots of mallocs and slowing things down.
5286eb55b6aSBarry Smith 
5296eb55b6aSBarry Smith        Contributed by: Mathew Knepley
5306eb55b6aSBarry Smith   */
531e2d53e46SBarry Smith   /* must zero l->B before l->A because the (diag) case below may put values into l->B*/
532e2d53e46SBarry Smith   ierr = MatZeroRows(l->B,istmp,0);CHKERRQ(ierr);
5336eb55b6aSBarry Smith   if (diag && (l->A->M == l->A->N)) {
5346eb55b6aSBarry Smith     ierr      = MatZeroRows(l->A,istmp,diag);CHKERRQ(ierr);
535e2d53e46SBarry Smith   } else if (diag) {
536e2d53e46SBarry Smith     ierr = MatZeroRows(l->A,istmp,0);CHKERRQ(ierr);
537fa46199cSSatish Balay     if (((Mat_SeqAIJ*)l->A->data)->nonew) {
53829bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"MatZeroRows() on rectangular matrices cannot be used with the Mat options\n\
539fa46199cSSatish Balay MAT_NO_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR");
5406525c446SSatish Balay     }
541e2d53e46SBarry Smith     for (i = 0; i < slen; i++) {
542e2d53e46SBarry Smith       row  = lrows[i] + rstart;
543e2d53e46SBarry Smith       ierr = MatSetValues(A,1,&row,1,&row,diag,INSERT_VALUES);CHKERRQ(ierr);
544e2d53e46SBarry Smith     }
545e2d53e46SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
546e2d53e46SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
5476eb55b6aSBarry Smith   } else {
5486a5c57faSSatish Balay     ierr = MatZeroRows(l->A,istmp,0);CHKERRQ(ierr);
5496eb55b6aSBarry Smith   }
55078b31e54SBarry Smith   ierr = ISDestroy(istmp);CHKERRQ(ierr);
551606d414cSSatish Balay   ierr = PetscFree(lrows);CHKERRQ(ierr);
55272dacd9aSBarry Smith 
5531eb62cbbSBarry Smith   /* wait on sends */
5541eb62cbbSBarry Smith   if (nsends) {
555b0a32e0cSBarry Smith     ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr);
556ca161407SBarry Smith     ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr);
557606d414cSSatish Balay     ierr = PetscFree(send_status);CHKERRQ(ierr);
5581eb62cbbSBarry Smith   }
559606d414cSSatish Balay   ierr = PetscFree(send_waits);CHKERRQ(ierr);
560606d414cSSatish Balay   ierr = PetscFree(svalues);CHKERRQ(ierr);
5611eb62cbbSBarry Smith 
5623a40ed3dSBarry Smith   PetscFunctionReturn(0);
5631eb62cbbSBarry Smith }
5641eb62cbbSBarry Smith 
5654a2ae208SSatish Balay #undef __FUNCT__
5664a2ae208SSatish Balay #define __FUNCT__ "MatMult_MPIAIJ"
5678f6be9afSLois Curfman McInnes int MatMult_MPIAIJ(Mat A,Vec xx,Vec yy)
5681eb62cbbSBarry Smith {
569416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
570fbd6ef76SBarry Smith   int        ierr,nt;
571416022c9SBarry Smith 
5723a40ed3dSBarry Smith   PetscFunctionBegin;
573a2ce50c7SBarry Smith   ierr = VecGetLocalSize(xx,&nt);CHKERRQ(ierr);
574273d9f13SBarry Smith   if (nt != A->n) {
575273d9f13SBarry Smith     SETERRQ2(PETSC_ERR_ARG_SIZ,"Incompatible partition of A (%d) and xx (%d)",A->n,nt);
576fbd6ef76SBarry Smith   }
57743a90d84SBarry Smith   ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
578f830108cSBarry Smith   ierr = (*a->A->ops->mult)(a->A,xx,yy);CHKERRQ(ierr);
57943a90d84SBarry Smith   ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
580f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);CHKERRQ(ierr);
5813a40ed3dSBarry Smith   PetscFunctionReturn(0);
5821eb62cbbSBarry Smith }
5831eb62cbbSBarry Smith 
5844a2ae208SSatish Balay #undef __FUNCT__
5854a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_MPIAIJ"
5868f6be9afSLois Curfman McInnes int MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
587da3a660dSBarry Smith {
588416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
589da3a660dSBarry Smith   int        ierr;
5903a40ed3dSBarry Smith 
5913a40ed3dSBarry Smith   PetscFunctionBegin;
59243a90d84SBarry Smith   ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
593f830108cSBarry Smith   ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
59443a90d84SBarry Smith   ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
595f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);CHKERRQ(ierr);
5963a40ed3dSBarry Smith   PetscFunctionReturn(0);
597da3a660dSBarry Smith }
598da3a660dSBarry Smith 
5994a2ae208SSatish Balay #undef __FUNCT__
6004a2ae208SSatish Balay #define __FUNCT__ "MatMultTranspose_MPIAIJ"
6017c922b88SBarry Smith int MatMultTranspose_MPIAIJ(Mat A,Vec xx,Vec yy)
602da3a660dSBarry Smith {
603416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
604da3a660dSBarry Smith   int        ierr;
605da3a660dSBarry Smith 
6063a40ed3dSBarry Smith   PetscFunctionBegin;
607da3a660dSBarry Smith   /* do nondiagonal part */
6087c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
609da3a660dSBarry Smith   /* send it on its way */
610537820f0SBarry Smith   ierr = VecScatterBegin(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
611da3a660dSBarry Smith   /* do local part */
6127c922b88SBarry Smith   ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr);
613da3a660dSBarry Smith   /* receive remote parts: note this assumes the values are not actually */
614da3a660dSBarry Smith   /* inserted in yy until the next line, which is true for my implementation*/
615da3a660dSBarry Smith   /* but is not perhaps always true. */
616537820f0SBarry Smith   ierr = VecScatterEnd(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
6173a40ed3dSBarry Smith   PetscFunctionReturn(0);
618da3a660dSBarry Smith }
619da3a660dSBarry Smith 
620cd0d46ebSvictorle EXTERN_C_BEGIN
621cd0d46ebSvictorle #undef __FUNCT__
622cd0d46ebSvictorle #define __FUNCT__ "MatIsSymmetric_MPIAIJ"
623cd0d46ebSvictorle int MatIsSymmetric_MPIAIJ(Mat Amat,Mat Bmat,PetscTruth *f)
624cd0d46ebSvictorle {
625cd0d46ebSvictorle   Mat_MPIAIJ *Aij = (Mat_MPIAIJ *) Amat->data, *Bij;
62666501d38Svictorle   Mat        Adia = Aij->A, Bdia, Aoff,Boff,*Aoffs,*Boffs;
627cd0d46ebSvictorle   IS         Me,Notme;
628cd0d46ebSvictorle   int        M,N,first,last,*notme,i, ierr;
629cd0d46ebSvictorle 
630cd0d46ebSvictorle   PetscFunctionBegin;
63142e5f5b4Svictorle 
63242e5f5b4Svictorle   /* Easy test: symmetric diagonal block */
63366501d38Svictorle   Bij = (Mat_MPIAIJ *) Bmat->data; Bdia = Bij->A;
63466501d38Svictorle   ierr = MatIsSymmetric(Adia,Bdia,f); CHKERRQ(ierr);
635cd0d46ebSvictorle   if (!*f) PetscFunctionReturn(0);
63642e5f5b4Svictorle 
63742e5f5b4Svictorle   /* Hard test: off-diagonal block. This takes a MatGetSubMatrix. */
638cd0d46ebSvictorle   ierr = MatGetSize(Amat,&M,&N); CHKERRQ(ierr);
639cd0d46ebSvictorle   ierr = MatGetOwnershipRange(Amat,&first,&last); CHKERRQ(ierr);
640cd0d46ebSvictorle   ierr = PetscMalloc((N-last+first)*sizeof(int),&notme); CHKERRQ(ierr);
641cd0d46ebSvictorle   for (i=0; i<first; i++) notme[i] = i;
642cd0d46ebSvictorle   for (i=last; i<M; i++) notme[i-last+first] = i;
643268466fbSBarry Smith   ierr = ISCreateGeneral(MPI_COMM_SELF,N-last+first,notme,&Notme); CHKERRQ(ierr);
644268466fbSBarry Smith   ierr = ISCreateStride(MPI_COMM_SELF,last-first,first,1,&Me); CHKERRQ(ierr);
645268466fbSBarry Smith   ierr = MatGetSubMatrices(Amat,1,&Me,&Notme,MAT_INITIAL_MATRIX,&Aoffs); CHKERRQ(ierr);
64666501d38Svictorle   Aoff = Aoffs[0];
647268466fbSBarry Smith   ierr = MatGetSubMatrices(Bmat,1,&Notme,&Me,MAT_INITIAL_MATRIX,&Boffs); CHKERRQ(ierr);
64866501d38Svictorle   Boff = Boffs[0];
649cd0d46ebSvictorle   ierr = MatIsSymmetric(Aoff,Boff,f); CHKERRQ(ierr);
65066501d38Svictorle   ierr = MatDestroyMatrices(1,&Aoffs); CHKERRQ(ierr);
65166501d38Svictorle   ierr = MatDestroyMatrices(1,&Boffs); CHKERRQ(ierr);
65242e5f5b4Svictorle   ierr = ISDestroy(Me); CHKERRQ(ierr);
65342e5f5b4Svictorle   ierr = ISDestroy(Notme); CHKERRQ(ierr);
65442e5f5b4Svictorle 
655cd0d46ebSvictorle   PetscFunctionReturn(0);
656cd0d46ebSvictorle }
657cd0d46ebSvictorle EXTERN_C_END
658cd0d46ebSvictorle 
6594a2ae208SSatish Balay #undef __FUNCT__
6604a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_MPIAIJ"
6617c922b88SBarry Smith int MatMultTransposeAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
662da3a660dSBarry Smith {
663416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
664da3a660dSBarry Smith   int        ierr;
665da3a660dSBarry Smith 
6663a40ed3dSBarry Smith   PetscFunctionBegin;
667da3a660dSBarry Smith   /* do nondiagonal part */
6687c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
669da3a660dSBarry Smith   /* send it on its way */
670537820f0SBarry Smith   ierr = VecScatterBegin(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
671da3a660dSBarry Smith   /* do local part */
6727c922b88SBarry Smith   ierr = (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
673da3a660dSBarry Smith   /* receive remote parts: note this assumes the values are not actually */
674da3a660dSBarry Smith   /* inserted in yy until the next line, which is true for my implementation*/
675da3a660dSBarry Smith   /* but is not perhaps always true. */
6760a198c4cSBarry Smith   ierr = VecScatterEnd(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
6773a40ed3dSBarry Smith   PetscFunctionReturn(0);
678da3a660dSBarry Smith }
679da3a660dSBarry Smith 
6801eb62cbbSBarry Smith /*
6811eb62cbbSBarry Smith   This only works correctly for square matrices where the subblock A->A is the
6821eb62cbbSBarry Smith    diagonal block
6831eb62cbbSBarry Smith */
6844a2ae208SSatish Balay #undef __FUNCT__
6854a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_MPIAIJ"
6868f6be9afSLois Curfman McInnes int MatGetDiagonal_MPIAIJ(Mat A,Vec v)
6871eb62cbbSBarry Smith {
6883a40ed3dSBarry Smith   int        ierr;
689416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
6903a40ed3dSBarry Smith 
6913a40ed3dSBarry Smith   PetscFunctionBegin;
692273d9f13SBarry Smith   if (A->M != A->N) SETERRQ(PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block");
6935baf8537SBarry Smith   if (a->rstart != a->cstart || a->rend != a->cend) {
69429bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_SIZ,"row partition must equal col partition");
6953a40ed3dSBarry Smith   }
6963a40ed3dSBarry Smith   ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr);
6973a40ed3dSBarry Smith   PetscFunctionReturn(0);
6981eb62cbbSBarry Smith }
6991eb62cbbSBarry Smith 
7004a2ae208SSatish Balay #undef __FUNCT__
7014a2ae208SSatish Balay #define __FUNCT__ "MatScale_MPIAIJ"
702268466fbSBarry Smith int MatScale_MPIAIJ(const PetscScalar aa[],Mat A)
703052efed2SBarry Smith {
704052efed2SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
705052efed2SBarry Smith   int        ierr;
7063a40ed3dSBarry Smith 
7073a40ed3dSBarry Smith   PetscFunctionBegin;
708052efed2SBarry Smith   ierr = MatScale(aa,a->A);CHKERRQ(ierr);
709052efed2SBarry Smith   ierr = MatScale(aa,a->B);CHKERRQ(ierr);
7103a40ed3dSBarry Smith   PetscFunctionReturn(0);
711052efed2SBarry Smith }
712052efed2SBarry Smith 
7134a2ae208SSatish Balay #undef __FUNCT__
7144a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_MPIAIJ"
715e1311b90SBarry Smith int MatDestroy_MPIAIJ(Mat mat)
7161eb62cbbSBarry Smith {
71744a69424SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
7181eb62cbbSBarry Smith   int        ierr;
71983e2fdc7SBarry Smith 
7203a40ed3dSBarry Smith   PetscFunctionBegin;
721aa482453SBarry Smith #if defined(PETSC_USE_LOG)
722b0a32e0cSBarry Smith   PetscLogObjectState((PetscObject)mat,"Rows=%d, Cols=%d",mat->M,mat->N);
723a5a9c739SBarry Smith #endif
7248798bf22SSatish Balay   ierr = MatStashDestroy_Private(&mat->stash);CHKERRQ(ierr);
725606d414cSSatish Balay   ierr = PetscFree(aij->rowners);CHKERRQ(ierr);
72678b31e54SBarry Smith   ierr = MatDestroy(aij->A);CHKERRQ(ierr);
72778b31e54SBarry Smith   ierr = MatDestroy(aij->B);CHKERRQ(ierr);
728aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
7290f5bd95cSBarry Smith   if (aij->colmap) {ierr = PetscTableDelete(aij->colmap);CHKERRQ(ierr);}
730b1fc9764SSatish Balay #else
731606d414cSSatish Balay   if (aij->colmap) {ierr = PetscFree(aij->colmap);CHKERRQ(ierr);}
732b1fc9764SSatish Balay #endif
733606d414cSSatish Balay   if (aij->garray) {ierr = PetscFree(aij->garray);CHKERRQ(ierr);}
7347c922b88SBarry Smith   if (aij->lvec)   {ierr = VecDestroy(aij->lvec);CHKERRQ(ierr);}
7357c922b88SBarry Smith   if (aij->Mvctx)  {ierr = VecScatterDestroy(aij->Mvctx);CHKERRQ(ierr);}
736606d414cSSatish Balay   if (aij->rowvalues) {ierr = PetscFree(aij->rowvalues);CHKERRQ(ierr);}
737606d414cSSatish Balay   ierr = PetscFree(aij);CHKERRQ(ierr);
7383a40ed3dSBarry Smith   PetscFunctionReturn(0);
7391eb62cbbSBarry Smith }
740ee50ffe9SBarry Smith 
7414a2ae208SSatish Balay #undef __FUNCT__
7428e2fed03SBarry Smith #define __FUNCT__ "MatView_MPIAIJ_Binary"
7438e2fed03SBarry Smith int MatView_MPIAIJ_Binary(Mat mat,PetscViewer viewer)
7448e2fed03SBarry Smith {
7458e2fed03SBarry Smith   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*)mat->data;
7468e2fed03SBarry Smith   Mat_SeqAIJ*       A = (Mat_SeqAIJ*)aij->A->data;
7478e2fed03SBarry Smith   Mat_SeqAIJ*       B = (Mat_SeqAIJ*)aij->B->data;
7488e2fed03SBarry Smith   int               nz,fd,ierr,header[4],rank,size,*row_lengths,*range,rlen,i,tag = ((PetscObject)viewer)->tag;
749b021249fSBarry Smith   int               nzmax,*column_indices,j,k,col,*garray = aij->garray,cnt,cstart = aij->cstart,rnz;
7508e2fed03SBarry Smith   PetscScalar       *column_values;
7518e2fed03SBarry Smith 
7528e2fed03SBarry Smith   PetscFunctionBegin;
7538e2fed03SBarry Smith   ierr = MPI_Comm_rank(mat->comm,&rank);CHKERRQ(ierr);
7548e2fed03SBarry Smith   ierr = MPI_Comm_size(mat->comm,&size);CHKERRQ(ierr);
7558e2fed03SBarry Smith   nz   = A->nz + B->nz;
7568e2fed03SBarry Smith   if (rank == 0) {
7578e2fed03SBarry Smith     header[0] = MAT_FILE_COOKIE;
7588e2fed03SBarry Smith     header[1] = mat->M;
7598e2fed03SBarry Smith     header[2] = mat->N;
7608e2fed03SBarry Smith     ierr = MPI_Reduce(&nz,&header[3],1,MPI_INT,MPI_SUM,0,mat->comm);CHKERRQ(ierr);
7618e2fed03SBarry Smith     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
7628e2fed03SBarry Smith     ierr = PetscBinaryWrite(fd,header,4,PETSC_INT,1);CHKERRQ(ierr);
7638e2fed03SBarry Smith     /* get largest number of rows any processor has */
7648e2fed03SBarry Smith     rlen = mat->m;
7658e2fed03SBarry Smith     ierr = PetscMapGetGlobalRange(mat->rmap,&range);CHKERRQ(ierr);
7668e2fed03SBarry Smith     for (i=1; i<size; i++) {
7678e2fed03SBarry Smith       rlen = PetscMax(rlen,range[i+1] - range[i]);
7688e2fed03SBarry Smith     }
7698e2fed03SBarry Smith   } else {
7708e2fed03SBarry Smith     ierr = MPI_Reduce(&nz,0,1,MPI_INT,MPI_SUM,0,mat->comm);CHKERRQ(ierr);
7718e2fed03SBarry Smith     rlen = mat->m;
7728e2fed03SBarry Smith   }
7738e2fed03SBarry Smith 
7748e2fed03SBarry Smith   /* load up the local row counts */
7758e2fed03SBarry Smith   ierr = PetscMalloc((rlen+1)*sizeof(int),&row_lengths);CHKERRQ(ierr);
7768e2fed03SBarry Smith   for (i=0; i<mat->m; i++) {
7778e2fed03SBarry Smith     row_lengths[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i];
7788e2fed03SBarry Smith   }
7798e2fed03SBarry Smith 
7808e2fed03SBarry Smith   /* store the row lengths to the file */
7818e2fed03SBarry Smith   if (rank == 0) {
7828e2fed03SBarry Smith     MPI_Status status;
7838e2fed03SBarry Smith     ierr = PetscBinaryWrite(fd,row_lengths,mat->m,PETSC_INT,1);CHKERRQ(ierr);
7848e2fed03SBarry Smith     for (i=1; i<size; i++) {
7858e2fed03SBarry Smith       rlen = range[i+1] - range[i];
7868e2fed03SBarry Smith       ierr = MPI_Recv(row_lengths,rlen,MPI_INT,i,tag,mat->comm,&status);CHKERRQ(ierr);
7878e2fed03SBarry Smith       ierr = PetscBinaryWrite(fd,row_lengths,rlen,PETSC_INT,1);CHKERRQ(ierr);
7888e2fed03SBarry Smith     }
7898e2fed03SBarry Smith   } else {
7908e2fed03SBarry Smith     ierr = MPI_Send(row_lengths,mat->m,MPI_INT,0,tag,mat->comm);CHKERRQ(ierr);
7918e2fed03SBarry Smith   }
7928e2fed03SBarry Smith   ierr = PetscFree(row_lengths);CHKERRQ(ierr);
7938e2fed03SBarry Smith 
7948e2fed03SBarry Smith   /* load up the local column indices */
7958e2fed03SBarry Smith   nzmax = nz; /* )th processor needs space a largest processor needs */
7968e2fed03SBarry Smith   ierr = MPI_Reduce(&nz,&nzmax,1,MPI_INT,MPI_MAX,0,mat->comm);CHKERRQ(ierr);
7978e2fed03SBarry Smith   ierr = PetscMalloc((nzmax+1)*sizeof(int),&column_indices);CHKERRQ(ierr);
7988e2fed03SBarry Smith   cnt  = 0;
7998e2fed03SBarry Smith   for (i=0; i<mat->m; i++) {
8008e2fed03SBarry Smith     for (j=B->i[i]; j<B->i[i+1]; j++) {
8018e2fed03SBarry Smith       if ( (col = garray[B->j[j]]) > cstart) break;
8028e2fed03SBarry Smith       column_indices[cnt++] = col;
8038e2fed03SBarry Smith     }
8048e2fed03SBarry Smith     for (k=A->i[i]; k<A->i[i+1]; k++) {
8058e2fed03SBarry Smith       column_indices[cnt++] = A->j[k] + cstart;
8068e2fed03SBarry Smith     }
8078e2fed03SBarry Smith     for (; j<B->i[i+1]; j++) {
8088e2fed03SBarry Smith       column_indices[cnt++] = garray[B->j[j]];
8098e2fed03SBarry Smith     }
8108e2fed03SBarry Smith   }
8118e2fed03SBarry Smith   if (cnt != A->nz + B->nz) SETERRQ2(PETSC_ERR_LIB,"Internal PETSc error: cnt = %d nz = %d",cnt,A->nz+B->nz);
8128e2fed03SBarry Smith 
8138e2fed03SBarry Smith   /* store the column indices to the file */
8148e2fed03SBarry Smith   if (rank == 0) {
8158e2fed03SBarry Smith     MPI_Status status;
8168e2fed03SBarry Smith     ierr = PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,1);CHKERRQ(ierr);
8178e2fed03SBarry Smith     for (i=1; i<size; i++) {
818b021249fSBarry Smith       ierr = MPI_Recv(&rnz,1,MPI_INT,i,tag,mat->comm,&status);CHKERRQ(ierr);
819b021249fSBarry Smith       if (rnz > nzmax) SETERRQ2(PETSC_ERR_LIB,"Internal PETSc error: nz = %d nzmax = %d",nz,nzmax);
820b021249fSBarry Smith       ierr = MPI_Recv(column_indices,rnz,MPI_INT,i,tag,mat->comm,&status);CHKERRQ(ierr);
821b021249fSBarry Smith       ierr = PetscBinaryWrite(fd,column_indices,rnz,PETSC_INT,1);CHKERRQ(ierr);
8228e2fed03SBarry Smith     }
8238e2fed03SBarry Smith   } else {
8248e2fed03SBarry Smith     ierr = MPI_Send(&nz,1,MPI_INT,0,tag,mat->comm);CHKERRQ(ierr);
8258e2fed03SBarry Smith     ierr = MPI_Send(column_indices,nz,MPI_INT,0,tag,mat->comm);CHKERRQ(ierr);
8268e2fed03SBarry Smith   }
8278e2fed03SBarry Smith   ierr = PetscFree(column_indices);CHKERRQ(ierr);
8288e2fed03SBarry Smith 
8298e2fed03SBarry Smith   /* load up the local column values */
8308e2fed03SBarry Smith   ierr = PetscMalloc((nzmax+1)*sizeof(PetscScalar),&column_values);CHKERRQ(ierr);
8318e2fed03SBarry Smith   cnt  = 0;
8328e2fed03SBarry Smith   for (i=0; i<mat->m; i++) {
8338e2fed03SBarry Smith     for (j=B->i[i]; j<B->i[i+1]; j++) {
8348e2fed03SBarry Smith       if ( garray[B->j[j]] > cstart) break;
8358e2fed03SBarry Smith       column_values[cnt++] = B->a[j];
8368e2fed03SBarry Smith     }
8378e2fed03SBarry Smith     for (k=A->i[i]; k<A->i[i+1]; k++) {
8388e2fed03SBarry Smith       column_values[cnt++] = A->a[k];
8398e2fed03SBarry Smith     }
8408e2fed03SBarry Smith     for (; j<B->i[i+1]; j++) {
8418e2fed03SBarry Smith       column_values[cnt++] = B->a[j];
8428e2fed03SBarry Smith     }
8438e2fed03SBarry Smith   }
8448e2fed03SBarry Smith   if (cnt != A->nz + B->nz) SETERRQ2(1,"Internal PETSc error: cnt = %d nz = %d",cnt,A->nz+B->nz);
8458e2fed03SBarry Smith 
8468e2fed03SBarry Smith   /* store the column values to the file */
8478e2fed03SBarry Smith   if (rank == 0) {
8488e2fed03SBarry Smith     MPI_Status status;
8498e2fed03SBarry Smith     ierr = PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,1);CHKERRQ(ierr);
8508e2fed03SBarry Smith     for (i=1; i<size; i++) {
851b021249fSBarry Smith       ierr = MPI_Recv(&rnz,1,MPI_INT,i,tag,mat->comm,&status);CHKERRQ(ierr);
852b021249fSBarry Smith       if (rnz > nzmax) SETERRQ2(PETSC_ERR_LIB,"Internal PETSc error: nz = %d nzmax = %d",nz,nzmax);
853b021249fSBarry Smith       ierr = MPI_Recv(column_values,rnz,MPIU_SCALAR,i,tag,mat->comm,&status);CHKERRQ(ierr);
854b021249fSBarry Smith       ierr = PetscBinaryWrite(fd,column_values,rnz,PETSC_SCALAR,1);CHKERRQ(ierr);
8558e2fed03SBarry Smith     }
8568e2fed03SBarry Smith   } else {
8578e2fed03SBarry Smith     ierr = MPI_Send(&nz,1,MPI_INT,0,tag,mat->comm);CHKERRQ(ierr);
8588e2fed03SBarry Smith     ierr = MPI_Send(column_values,nz,MPIU_SCALAR,0,tag,mat->comm);CHKERRQ(ierr);
8598e2fed03SBarry Smith   }
8608e2fed03SBarry Smith   ierr = PetscFree(column_values);CHKERRQ(ierr);
8618e2fed03SBarry Smith   PetscFunctionReturn(0);
8628e2fed03SBarry Smith }
8638e2fed03SBarry Smith 
8648e2fed03SBarry Smith #undef __FUNCT__
8654a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ_ASCIIorDraworSocket"
866b0a32e0cSBarry Smith int MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
867416022c9SBarry Smith {
86844a69424SLois Curfman McInnes   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*)mat->data;
869bfec09a0SHong Zhang   int               ierr,rank = aij->rank,size = aij->size;
8708e2fed03SBarry Smith   PetscTruth        isdraw,isascii,flg,isbinary;
871b0a32e0cSBarry Smith   PetscViewer       sviewer;
872f3ef73ceSBarry Smith   PetscViewerFormat format;
873416022c9SBarry Smith 
8743a40ed3dSBarry Smith   PetscFunctionBegin;
875fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
876b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
8778e2fed03SBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
8780f5bd95cSBarry Smith   if (isascii) {
879b0a32e0cSBarry Smith     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
880456192e2SBarry Smith     if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
8814e220ebcSLois Curfman McInnes       MatInfo info;
8821dab6e02SBarry Smith       ierr = MPI_Comm_rank(mat->comm,&rank);CHKERRQ(ierr);
883888f2ed8SSatish Balay       ierr = MatGetInfo(mat,MAT_LOCAL,&info);CHKERRQ(ierr);
884b0a32e0cSBarry Smith       ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_no_inode",&flg);CHKERRQ(ierr);
8856831982aSBarry Smith       if (flg) {
886b0a32e0cSBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %d nz %d nz alloced %d mem %d, not using I-node routines\n",
887273d9f13SBarry Smith 					      rank,mat->m,(int)info.nz_used,(int)info.nz_allocated,(int)info.memory);CHKERRQ(ierr);
8886831982aSBarry Smith       } else {
889b0a32e0cSBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %d nz %d nz alloced %d mem %d, using I-node routines\n",
890273d9f13SBarry Smith 		    rank,mat->m,(int)info.nz_used,(int)info.nz_allocated,(int)info.memory);CHKERRQ(ierr);
8916831982aSBarry Smith       }
892888f2ed8SSatish Balay       ierr = MatGetInfo(aij->A,MAT_LOCAL,&info);CHKERRQ(ierr);
893b0a32e0cSBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %d \n",rank,(int)info.nz_used);CHKERRQ(ierr);
894888f2ed8SSatish Balay       ierr = MatGetInfo(aij->B,MAT_LOCAL,&info);CHKERRQ(ierr);
895b0a32e0cSBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %d \n",rank,(int)info.nz_used);CHKERRQ(ierr);
896b0a32e0cSBarry Smith       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
897a40aa06bSLois Curfman McInnes       ierr = VecScatterView(aij->Mvctx,viewer);CHKERRQ(ierr);
8983a40ed3dSBarry Smith       PetscFunctionReturn(0);
899fb9695e5SSatish Balay     } else if (format == PETSC_VIEWER_ASCII_INFO) {
9003a40ed3dSBarry Smith       PetscFunctionReturn(0);
9014aedb280SBarry Smith     } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
9024aedb280SBarry Smith       PetscFunctionReturn(0);
90308480c60SBarry Smith     }
9048e2fed03SBarry Smith   } else if (isbinary) {
9058e2fed03SBarry Smith     if (size == 1) {
9068e2fed03SBarry Smith       ierr = PetscObjectSetName((PetscObject)aij->A,mat->name);CHKERRQ(ierr);
9078e2fed03SBarry Smith       ierr = MatView(aij->A,viewer);CHKERRQ(ierr);
9088e2fed03SBarry Smith     } else {
9098e2fed03SBarry Smith       ierr = MatView_MPIAIJ_Binary(mat,viewer);CHKERRQ(ierr);
9108e2fed03SBarry Smith     }
9118e2fed03SBarry Smith     PetscFunctionReturn(0);
9120f5bd95cSBarry Smith   } else if (isdraw) {
913b0a32e0cSBarry Smith     PetscDraw  draw;
91419bcc07fSBarry Smith     PetscTruth isnull;
915b0a32e0cSBarry Smith     ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
916b0a32e0cSBarry Smith     ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
91719bcc07fSBarry Smith   }
91819bcc07fSBarry Smith 
91917699dbbSLois Curfman McInnes   if (size == 1) {
920e36acaf3SBarry Smith     ierr = PetscObjectSetName((PetscObject)aij->A,mat->name);CHKERRQ(ierr);
92178b31e54SBarry Smith     ierr = MatView(aij->A,viewer);CHKERRQ(ierr);
9223a40ed3dSBarry Smith   } else {
92395373324SBarry Smith     /* assemble the entire matrix onto first processor. */
92495373324SBarry Smith     Mat         A;
925ec8511deSBarry Smith     Mat_SeqAIJ *Aloc;
926273d9f13SBarry Smith     int         M = mat->M,N = mat->N,m,*ai,*aj,row,*cols,i,*ct;
92787828ca2SBarry Smith     PetscScalar *a;
9282ee70a88SLois Curfman McInnes 
92917699dbbSLois Curfman McInnes     if (!rank) {
93055843e3eSBarry Smith       ierr = MatCreateMPIAIJ(mat->comm,M,N,M,N,0,PETSC_NULL,0,PETSC_NULL,&A);CHKERRQ(ierr);
9313a40ed3dSBarry Smith     } else {
93255843e3eSBarry Smith       ierr = MatCreateMPIAIJ(mat->comm,0,0,M,N,0,PETSC_NULL,0,PETSC_NULL,&A);CHKERRQ(ierr);
93395373324SBarry Smith     }
934b0a32e0cSBarry Smith     PetscLogObjectParent(mat,A);
935416022c9SBarry Smith 
93695373324SBarry Smith     /* copy over the A part */
937ec8511deSBarry Smith     Aloc = (Mat_SeqAIJ*)aij->A->data;
938273d9f13SBarry Smith     m = aij->A->m; ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
93995373324SBarry Smith     row = aij->rstart;
940bfec09a0SHong Zhang     for (i=0; i<ai[m]; i++) {aj[i] += aij->cstart ;}
94195373324SBarry Smith     for (i=0; i<m; i++) {
942416022c9SBarry Smith       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);CHKERRQ(ierr);
94395373324SBarry Smith       row++; a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
94495373324SBarry Smith     }
9452ee70a88SLois Curfman McInnes     aj = Aloc->j;
946bfec09a0SHong Zhang     for (i=0; i<ai[m]; i++) {aj[i] -= aij->cstart;}
94795373324SBarry Smith 
94895373324SBarry Smith     /* copy over the B part */
949ec8511deSBarry Smith     Aloc = (Mat_SeqAIJ*)aij->B->data;
950273d9f13SBarry Smith     m    = aij->B->m;  ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
95195373324SBarry Smith     row  = aij->rstart;
952b0a32e0cSBarry Smith     ierr = PetscMalloc((ai[m]+1)*sizeof(int),&cols);CHKERRQ(ierr);
953b0a32e0cSBarry Smith     ct   = cols;
954bfec09a0SHong Zhang     for (i=0; i<ai[m]; i++) {cols[i] = aij->garray[aj[i]];}
95595373324SBarry Smith     for (i=0; i<m; i++) {
956416022c9SBarry Smith       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);CHKERRQ(ierr);
95795373324SBarry Smith       row++; a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
95895373324SBarry Smith     }
959606d414cSSatish Balay     ierr = PetscFree(ct);CHKERRQ(ierr);
9606d4a8577SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9616d4a8577SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
96255843e3eSBarry Smith     /*
96355843e3eSBarry Smith        Everyone has to call to draw the matrix since the graphics waits are
964b0a32e0cSBarry Smith        synchronized across all processors that share the PetscDraw object
96555843e3eSBarry Smith     */
966b0a32e0cSBarry Smith     ierr = PetscViewerGetSingleton(viewer,&sviewer);CHKERRQ(ierr);
967e03a110bSBarry Smith     if (!rank) {
968e36acaf3SBarry Smith       ierr = PetscObjectSetName((PetscObject)((Mat_MPIAIJ*)(A->data))->A,mat->name);CHKERRQ(ierr);
9696831982aSBarry Smith       ierr = MatView(((Mat_MPIAIJ*)(A->data))->A,sviewer);CHKERRQ(ierr);
97095373324SBarry Smith     }
971b0a32e0cSBarry Smith     ierr = PetscViewerRestoreSingleton(viewer,&sviewer);CHKERRQ(ierr);
97278b31e54SBarry Smith     ierr = MatDestroy(A);CHKERRQ(ierr);
97395373324SBarry Smith   }
9743a40ed3dSBarry Smith   PetscFunctionReturn(0);
9751eb62cbbSBarry Smith }
9761eb62cbbSBarry Smith 
9774a2ae208SSatish Balay #undef __FUNCT__
9784a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ"
979b0a32e0cSBarry Smith int MatView_MPIAIJ(Mat mat,PetscViewer viewer)
980416022c9SBarry Smith {
981416022c9SBarry Smith   int        ierr;
9826831982aSBarry Smith   PetscTruth isascii,isdraw,issocket,isbinary;
983416022c9SBarry Smith 
9843a40ed3dSBarry Smith   PetscFunctionBegin;
985b0a32e0cSBarry Smith   ierr  = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
986fb9695e5SSatish Balay   ierr  = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
987fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
988b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_SOCKET,&issocket);CHKERRQ(ierr);
9890f5bd95cSBarry Smith   if (isascii || isdraw || isbinary || issocket) {
9907b2a1423SBarry Smith     ierr = MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer);CHKERRQ(ierr);
9915cd90555SBarry Smith   } else {
99229bbc08cSBarry Smith     SETERRQ1(1,"Viewer type %s not supported by MPIAIJ matrices",((PetscObject)viewer)->type_name);
993416022c9SBarry Smith   }
9943a40ed3dSBarry Smith   PetscFunctionReturn(0);
995416022c9SBarry Smith }
996416022c9SBarry Smith 
9971eb62cbbSBarry Smith 
998c14dc6b6SHong Zhang 
9994a2ae208SSatish Balay #undef __FUNCT__
10004a2ae208SSatish Balay #define __FUNCT__ "MatRelax_MPIAIJ"
1001c14dc6b6SHong Zhang int MatRelax_MPIAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,int its,int lits,Vec xx)
10028a729477SBarry Smith {
100344a69424SLois Curfman McInnes   Mat_MPIAIJ   *mat = (Mat_MPIAIJ*)matin->data;
1004c14dc6b6SHong Zhang   int          ierr;
1005c14dc6b6SHong Zhang   Vec          bb1;
1006a6c309e7SHong Zhang   PetscScalar  mone=-1.0;
10078a729477SBarry Smith 
10083a40ed3dSBarry Smith   PetscFunctionBegin;
100991723122SBarry Smith   if (its <= 0 || lits <= 0) SETERRQ2(PETSC_ERR_ARG_WRONG,"Relaxation requires global its %d and local its %d both positive",its,lits);
1010c14dc6b6SHong Zhang 
1011c14dc6b6SHong Zhang   ierr = VecDuplicate(bb,&bb1);CHKERRQ(ierr);
10122798e883SHong Zhang 
1013c16cb8f2SBarry Smith   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){
1014da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
1015bd3bf7d3SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,lits,xx);CHKERRQ(ierr);
10162798e883SHong Zhang       its--;
1017da3a660dSBarry Smith     }
10182798e883SHong Zhang 
10192798e883SHong Zhang     while (its--) {
10203a40ed3dSBarry Smith       ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10213a40ed3dSBarry Smith       ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10222798e883SHong Zhang 
1023c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
1024c14dc6b6SHong Zhang       ierr = VecScale(&mone,mat->lvec);CHKERRQ(ierr);
1025c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
10262798e883SHong Zhang 
1027c14dc6b6SHong Zhang       /* local sweep */
1028bd3bf7d3SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,lits,xx);
1029c14dc6b6SHong Zhang       CHKERRQ(ierr);
10302798e883SHong Zhang     }
10313a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_FORWARD_SWEEP){
1032da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
1033c14dc6b6SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,PETSC_NULL,xx);CHKERRQ(ierr);
10342798e883SHong Zhang       its--;
1035da3a660dSBarry Smith     }
10362798e883SHong Zhang     while (its--) {
10373a40ed3dSBarry Smith       ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10383a40ed3dSBarry Smith       ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10392798e883SHong Zhang 
1040c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
1041c14dc6b6SHong Zhang       ierr = VecScale(&mone,mat->lvec);CHKERRQ(ierr);
1042c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
1043c14dc6b6SHong Zhang 
1044c14dc6b6SHong Zhang       /* local sweep */
1045a6c309e7SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,PETSC_NULL,xx);
1046c14dc6b6SHong Zhang       CHKERRQ(ierr);
10472798e883SHong Zhang     }
10483a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_BACKWARD_SWEEP){
1049da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
1050c14dc6b6SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,PETSC_NULL,xx);CHKERRQ(ierr);
10512798e883SHong Zhang       its--;
1052da3a660dSBarry Smith     }
10532798e883SHong Zhang     while (its--) {
10542e8a6d31SBarry Smith       ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10552e8a6d31SBarry Smith       ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10562798e883SHong Zhang 
1057c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
1058c14dc6b6SHong Zhang       ierr = VecScale(&mone,mat->lvec);CHKERRQ(ierr);
1059c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
10602798e883SHong Zhang 
1061c14dc6b6SHong Zhang       /* local sweep */
1062a6c309e7SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,PETSC_NULL,xx);
1063c14dc6b6SHong Zhang       CHKERRQ(ierr);
10642798e883SHong Zhang     }
10653a40ed3dSBarry Smith   } else {
106629bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"Parallel SOR not supported");
1067c16cb8f2SBarry Smith   }
1068c14dc6b6SHong Zhang 
1069c14dc6b6SHong Zhang   ierr = VecDestroy(bb1);CHKERRQ(ierr);
10703a40ed3dSBarry Smith   PetscFunctionReturn(0);
10718a729477SBarry Smith }
1072a66be287SLois Curfman McInnes 
10734a2ae208SSatish Balay #undef __FUNCT__
10744a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_MPIAIJ"
10758f6be9afSLois Curfman McInnes int MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info)
1076a66be287SLois Curfman McInnes {
1077a66be287SLois Curfman McInnes   Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data;
1078a66be287SLois Curfman McInnes   Mat        A = mat->A,B = mat->B;
10794e220ebcSLois Curfman McInnes   int        ierr;
1080329f5518SBarry Smith   PetscReal  isend[5],irecv[5];
1081a66be287SLois Curfman McInnes 
10823a40ed3dSBarry Smith   PetscFunctionBegin;
10834e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
10844e220ebcSLois Curfman McInnes   ierr = MatGetInfo(A,MAT_LOCAL,info);CHKERRQ(ierr);
10854e220ebcSLois Curfman McInnes   isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
10864e220ebcSLois Curfman McInnes   isend[3] = info->memory;  isend[4] = info->mallocs;
10874e220ebcSLois Curfman McInnes   ierr = MatGetInfo(B,MAT_LOCAL,info);CHKERRQ(ierr);
10884e220ebcSLois Curfman McInnes   isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
10894e220ebcSLois Curfman McInnes   isend[3] += info->memory;  isend[4] += info->mallocs;
1090a66be287SLois Curfman McInnes   if (flag == MAT_LOCAL) {
10914e220ebcSLois Curfman McInnes     info->nz_used      = isend[0];
10924e220ebcSLois Curfman McInnes     info->nz_allocated = isend[1];
10934e220ebcSLois Curfman McInnes     info->nz_unneeded  = isend[2];
10944e220ebcSLois Curfman McInnes     info->memory       = isend[3];
10954e220ebcSLois Curfman McInnes     info->mallocs      = isend[4];
1096a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_MAX) {
1097d7d1e502SBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_MAX,matin->comm);CHKERRQ(ierr);
10984e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
10994e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
11004e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
11014e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
11024e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1103a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_SUM) {
1104d7d1e502SBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_SUM,matin->comm);CHKERRQ(ierr);
11054e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
11064e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
11074e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
11084e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
11094e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1110a66be287SLois Curfman McInnes   }
11114e220ebcSLois Curfman McInnes   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
11124e220ebcSLois Curfman McInnes   info->fill_ratio_needed = 0;
11134e220ebcSLois Curfman McInnes   info->factor_mallocs    = 0;
1114273d9f13SBarry Smith   info->rows_global       = (double)matin->M;
1115273d9f13SBarry Smith   info->columns_global    = (double)matin->N;
1116273d9f13SBarry Smith   info->rows_local        = (double)matin->m;
1117273d9f13SBarry Smith   info->columns_local     = (double)matin->N;
11184e220ebcSLois Curfman McInnes 
11193a40ed3dSBarry Smith   PetscFunctionReturn(0);
1120a66be287SLois Curfman McInnes }
1121a66be287SLois Curfman McInnes 
11224a2ae208SSatish Balay #undef __FUNCT__
11234a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_MPIAIJ"
11248f6be9afSLois Curfman McInnes int MatSetOption_MPIAIJ(Mat A,MatOption op)
1125c74985f6SBarry Smith {
1126c0bbcb79SLois Curfman McInnes   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
11271dee9f54SBarry Smith   int        ierr;
1128c74985f6SBarry Smith 
11293a40ed3dSBarry Smith   PetscFunctionBegin;
113012c028f9SKris Buschelman   switch (op) {
113112c028f9SKris Buschelman   case MAT_NO_NEW_NONZERO_LOCATIONS:
113212c028f9SKris Buschelman   case MAT_YES_NEW_NONZERO_LOCATIONS:
113312c028f9SKris Buschelman   case MAT_COLUMNS_UNSORTED:
113412c028f9SKris Buschelman   case MAT_COLUMNS_SORTED:
113512c028f9SKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
113612c028f9SKris Buschelman   case MAT_KEEP_ZEROED_ROWS:
113712c028f9SKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
113812c028f9SKris Buschelman   case MAT_USE_INODES:
113912c028f9SKris Buschelman   case MAT_DO_NOT_USE_INODES:
114012c028f9SKris Buschelman   case MAT_IGNORE_ZERO_ENTRIES:
11411dee9f54SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
11421dee9f54SBarry Smith     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
114312c028f9SKris Buschelman     break;
114412c028f9SKris Buschelman   case MAT_ROW_ORIENTED:
11457c922b88SBarry Smith     a->roworiented = PETSC_TRUE;
11461dee9f54SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
11471dee9f54SBarry Smith     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
114812c028f9SKris Buschelman     break;
114912c028f9SKris Buschelman   case MAT_ROWS_SORTED:
115012c028f9SKris Buschelman   case MAT_ROWS_UNSORTED:
115112c028f9SKris Buschelman   case MAT_YES_NEW_DIAGONALS:
1152b0a32e0cSBarry Smith     PetscLogInfo(A,"MatSetOption_MPIAIJ:Option ignored\n");
115312c028f9SKris Buschelman     break;
115412c028f9SKris Buschelman   case MAT_COLUMN_ORIENTED:
11557c922b88SBarry Smith     a->roworiented = PETSC_FALSE;
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_IGNORE_OFF_PROC_ENTRIES:
11607c922b88SBarry Smith     a->donotstash = PETSC_TRUE;
116112c028f9SKris Buschelman     break;
116212c028f9SKris Buschelman   case MAT_NO_NEW_DIAGONALS:
116329bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"MAT_NO_NEW_DIAGONALS");
116412c028f9SKris Buschelman   default:
116529bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"unknown option");
11663a40ed3dSBarry Smith   }
11673a40ed3dSBarry Smith   PetscFunctionReturn(0);
1168c74985f6SBarry Smith }
1169c74985f6SBarry Smith 
11704a2ae208SSatish Balay #undef __FUNCT__
11714a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_MPIAIJ"
117287828ca2SBarry Smith int MatGetRow_MPIAIJ(Mat matin,int row,int *nz,int **idx,PetscScalar **v)
117339e00950SLois Curfman McInnes {
1174154123eaSLois Curfman McInnes   Mat_MPIAIJ   *mat = (Mat_MPIAIJ*)matin->data;
117587828ca2SBarry Smith   PetscScalar  *vworkA,*vworkB,**pvA,**pvB,*v_p;
1176154123eaSLois Curfman McInnes   int          i,ierr,*cworkA,*cworkB,**pcA,**pcB,cstart = mat->cstart;
1177154123eaSLois Curfman McInnes   int          nztot,nzA,nzB,lrow,rstart = mat->rstart,rend = mat->rend;
117870f0671dSBarry Smith   int          *cmap,*idx_p;
117939e00950SLois Curfman McInnes 
11803a40ed3dSBarry Smith   PetscFunctionBegin;
118129bbc08cSBarry Smith   if (mat->getrowactive == PETSC_TRUE) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Already active");
11827a0afa10SBarry Smith   mat->getrowactive = PETSC_TRUE;
11837a0afa10SBarry Smith 
118470f0671dSBarry Smith   if (!mat->rowvalues && (idx || v)) {
11857a0afa10SBarry Smith     /*
11867a0afa10SBarry Smith         allocate enough space to hold information from the longest row.
11877a0afa10SBarry Smith     */
11887a0afa10SBarry Smith     Mat_SeqAIJ *Aa = (Mat_SeqAIJ*)mat->A->data,*Ba = (Mat_SeqAIJ*)mat->B->data;
1189273d9f13SBarry Smith     int     max = 1,tmp;
1190273d9f13SBarry Smith     for (i=0; i<matin->m; i++) {
11917a0afa10SBarry Smith       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
11927a0afa10SBarry Smith       if (max < tmp) { max = tmp; }
11937a0afa10SBarry Smith     }
119487828ca2SBarry Smith     ierr = PetscMalloc(max*(sizeof(int)+sizeof(PetscScalar)),&mat->rowvalues);CHKERRQ(ierr);
11957a0afa10SBarry Smith     mat->rowindices = (int*)(mat->rowvalues + max);
11967a0afa10SBarry Smith   }
11977a0afa10SBarry Smith 
119829bbc08cSBarry Smith   if (row < rstart || row >= rend) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Only local rows")
1199abc0e9e4SLois Curfman McInnes   lrow = row - rstart;
120039e00950SLois Curfman McInnes 
1201154123eaSLois Curfman McInnes   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1202154123eaSLois Curfman McInnes   if (!v)   {pvA = 0; pvB = 0;}
1203154123eaSLois Curfman McInnes   if (!idx) {pcA = 0; if (!v) pcB = 0;}
1204f830108cSBarry Smith   ierr = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1205f830108cSBarry Smith   ierr = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
1206154123eaSLois Curfman McInnes   nztot = nzA + nzB;
1207154123eaSLois Curfman McInnes 
120870f0671dSBarry Smith   cmap  = mat->garray;
1209154123eaSLois Curfman McInnes   if (v  || idx) {
1210154123eaSLois Curfman McInnes     if (nztot) {
1211154123eaSLois Curfman McInnes       /* Sort by increasing column numbers, assuming A and B already sorted */
121270f0671dSBarry Smith       int imark = -1;
1213154123eaSLois Curfman McInnes       if (v) {
121470f0671dSBarry Smith         *v = v_p = mat->rowvalues;
121539e00950SLois Curfman McInnes         for (i=0; i<nzB; i++) {
121670f0671dSBarry Smith           if (cmap[cworkB[i]] < cstart)   v_p[i] = vworkB[i];
1217154123eaSLois Curfman McInnes           else break;
1218154123eaSLois Curfman McInnes         }
1219154123eaSLois Curfman McInnes         imark = i;
122070f0671dSBarry Smith         for (i=0; i<nzA; i++)     v_p[imark+i] = vworkA[i];
122170f0671dSBarry Smith         for (i=imark; i<nzB; i++) v_p[nzA+i]   = vworkB[i];
1222154123eaSLois Curfman McInnes       }
1223154123eaSLois Curfman McInnes       if (idx) {
122470f0671dSBarry Smith         *idx = idx_p = mat->rowindices;
122570f0671dSBarry Smith         if (imark > -1) {
122670f0671dSBarry Smith           for (i=0; i<imark; i++) {
122770f0671dSBarry Smith             idx_p[i] = cmap[cworkB[i]];
122870f0671dSBarry Smith           }
122970f0671dSBarry Smith         } else {
1230154123eaSLois Curfman McInnes           for (i=0; i<nzB; i++) {
123170f0671dSBarry Smith             if (cmap[cworkB[i]] < cstart)   idx_p[i] = cmap[cworkB[i]];
1232154123eaSLois Curfman McInnes             else break;
1233154123eaSLois Curfman McInnes           }
1234154123eaSLois Curfman McInnes           imark = i;
123570f0671dSBarry Smith         }
123670f0671dSBarry Smith         for (i=0; i<nzA; i++)     idx_p[imark+i] = cstart + cworkA[i];
123770f0671dSBarry Smith         for (i=imark; i<nzB; i++) idx_p[nzA+i]   = cmap[cworkB[i]];
123839e00950SLois Curfman McInnes       }
12393f97c4b0SBarry Smith     } else {
12401ca473b0SSatish Balay       if (idx) *idx = 0;
12411ca473b0SSatish Balay       if (v)   *v   = 0;
12421ca473b0SSatish Balay     }
1243154123eaSLois Curfman McInnes   }
124439e00950SLois Curfman McInnes   *nz = nztot;
1245f830108cSBarry Smith   ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1246f830108cSBarry Smith   ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
12473a40ed3dSBarry Smith   PetscFunctionReturn(0);
124839e00950SLois Curfman McInnes }
124939e00950SLois Curfman McInnes 
12504a2ae208SSatish Balay #undef __FUNCT__
12514a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_MPIAIJ"
125287828ca2SBarry Smith int MatRestoreRow_MPIAIJ(Mat mat,int row,int *nz,int **idx,PetscScalar **v)
125339e00950SLois Curfman McInnes {
12547a0afa10SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
12553a40ed3dSBarry Smith 
12563a40ed3dSBarry Smith   PetscFunctionBegin;
12577a0afa10SBarry Smith   if (aij->getrowactive == PETSC_FALSE) {
125829bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"MatGetRow not called");
12597a0afa10SBarry Smith   }
12607a0afa10SBarry Smith   aij->getrowactive = PETSC_FALSE;
12613a40ed3dSBarry Smith   PetscFunctionReturn(0);
126239e00950SLois Curfman McInnes }
126339e00950SLois Curfman McInnes 
12644a2ae208SSatish Balay #undef __FUNCT__
12654a2ae208SSatish Balay #define __FUNCT__ "MatNorm_MPIAIJ"
1266329f5518SBarry Smith int MatNorm_MPIAIJ(Mat mat,NormType type,PetscReal *norm)
1267855ac2c5SLois Curfman McInnes {
1268855ac2c5SLois Curfman McInnes   Mat_MPIAIJ   *aij = (Mat_MPIAIJ*)mat->data;
1269ec8511deSBarry Smith   Mat_SeqAIJ   *amat = (Mat_SeqAIJ*)aij->A->data,*bmat = (Mat_SeqAIJ*)aij->B->data;
1270bfec09a0SHong Zhang   int          ierr,i,j,cstart = aij->cstart;
1271329f5518SBarry Smith   PetscReal    sum = 0.0;
127287828ca2SBarry Smith   PetscScalar  *v;
127304ca555eSLois Curfman McInnes 
12743a40ed3dSBarry Smith   PetscFunctionBegin;
127517699dbbSLois Curfman McInnes   if (aij->size == 1) {
127614183eadSLois Curfman McInnes     ierr =  MatNorm(aij->A,type,norm);CHKERRQ(ierr);
127737fa93a5SLois Curfman McInnes   } else {
127804ca555eSLois Curfman McInnes     if (type == NORM_FROBENIUS) {
127904ca555eSLois Curfman McInnes       v = amat->a;
128004ca555eSLois Curfman McInnes       for (i=0; i<amat->nz; i++) {
1281aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
1282329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
128304ca555eSLois Curfman McInnes #else
128404ca555eSLois Curfman McInnes         sum += (*v)*(*v); v++;
128504ca555eSLois Curfman McInnes #endif
128604ca555eSLois Curfman McInnes       }
128704ca555eSLois Curfman McInnes       v = bmat->a;
128804ca555eSLois Curfman McInnes       for (i=0; i<bmat->nz; i++) {
1289aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
1290329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
129104ca555eSLois Curfman McInnes #else
129204ca555eSLois Curfman McInnes         sum += (*v)*(*v); v++;
129304ca555eSLois Curfman McInnes #endif
129404ca555eSLois Curfman McInnes       }
1295d7d1e502SBarry Smith       ierr = MPI_Allreduce(&sum,norm,1,MPIU_REAL,MPI_SUM,mat->comm);CHKERRQ(ierr);
129604ca555eSLois Curfman McInnes       *norm = sqrt(*norm);
12973a40ed3dSBarry Smith     } else if (type == NORM_1) { /* max column norm */
1298329f5518SBarry Smith       PetscReal *tmp,*tmp2;
129904ca555eSLois Curfman McInnes       int    *jj,*garray = aij->garray;
1300b0a32e0cSBarry Smith       ierr = PetscMalloc((mat->N+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1301b0a32e0cSBarry Smith       ierr = PetscMalloc((mat->N+1)*sizeof(PetscReal),&tmp2);CHKERRQ(ierr);
1302273d9f13SBarry Smith       ierr = PetscMemzero(tmp,mat->N*sizeof(PetscReal));CHKERRQ(ierr);
130304ca555eSLois Curfman McInnes       *norm = 0.0;
130404ca555eSLois Curfman McInnes       v = amat->a; jj = amat->j;
130504ca555eSLois Curfman McInnes       for (j=0; j<amat->nz; j++) {
1306bfec09a0SHong Zhang         tmp[cstart + *jj++ ] += PetscAbsScalar(*v);  v++;
130704ca555eSLois Curfman McInnes       }
130804ca555eSLois Curfman McInnes       v = bmat->a; jj = bmat->j;
130904ca555eSLois Curfman McInnes       for (j=0; j<bmat->nz; j++) {
1310bfec09a0SHong Zhang         tmp[garray[*jj++]] += PetscAbsScalar(*v); v++;
131104ca555eSLois Curfman McInnes       }
1312d7d1e502SBarry Smith       ierr = MPI_Allreduce(tmp,tmp2,mat->N,MPIU_REAL,MPI_SUM,mat->comm);CHKERRQ(ierr);
1313273d9f13SBarry Smith       for (j=0; j<mat->N; j++) {
131404ca555eSLois Curfman McInnes         if (tmp2[j] > *norm) *norm = tmp2[j];
131504ca555eSLois Curfman McInnes       }
1316606d414cSSatish Balay       ierr = PetscFree(tmp);CHKERRQ(ierr);
1317606d414cSSatish Balay       ierr = PetscFree(tmp2);CHKERRQ(ierr);
13183a40ed3dSBarry Smith     } else if (type == NORM_INFINITY) { /* max row norm */
1319329f5518SBarry Smith       PetscReal ntemp = 0.0;
1320273d9f13SBarry Smith       for (j=0; j<aij->A->m; j++) {
1321bfec09a0SHong Zhang         v = amat->a + amat->i[j];
132204ca555eSLois Curfman McInnes         sum = 0.0;
132304ca555eSLois Curfman McInnes         for (i=0; i<amat->i[j+1]-amat->i[j]; i++) {
1324cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
132504ca555eSLois Curfman McInnes         }
1326bfec09a0SHong Zhang         v = bmat->a + bmat->i[j];
132704ca555eSLois Curfman McInnes         for (i=0; i<bmat->i[j+1]-bmat->i[j]; i++) {
1328cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
132904ca555eSLois Curfman McInnes         }
1330515d9167SLois Curfman McInnes         if (sum > ntemp) ntemp = sum;
133104ca555eSLois Curfman McInnes       }
1332d7d1e502SBarry Smith       ierr = MPI_Allreduce(&ntemp,norm,1,MPIU_REAL,MPI_MAX,mat->comm);CHKERRQ(ierr);
1333ca161407SBarry Smith     } else {
133429bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"No support for two norm");
133504ca555eSLois Curfman McInnes     }
133637fa93a5SLois Curfman McInnes   }
13373a40ed3dSBarry Smith   PetscFunctionReturn(0);
1338855ac2c5SLois Curfman McInnes }
1339855ac2c5SLois Curfman McInnes 
13404a2ae208SSatish Balay #undef __FUNCT__
13414a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_MPIAIJ"
13428f6be9afSLois Curfman McInnes int MatTranspose_MPIAIJ(Mat A,Mat *matout)
1343b7c46309SBarry Smith {
1344b7c46309SBarry Smith   Mat_MPIAIJ   *a = (Mat_MPIAIJ*)A->data;
1345dbb450caSBarry Smith   Mat_SeqAIJ   *Aloc = (Mat_SeqAIJ*)a->A->data;
1346bfec09a0SHong Zhang   int          ierr;
1347273d9f13SBarry Smith   int          M = A->M,N = A->N,m,*ai,*aj,row,*cols,i,*ct;
13483a40ed3dSBarry Smith   Mat          B;
134987828ca2SBarry Smith   PetscScalar  *array;
1350b7c46309SBarry Smith 
13513a40ed3dSBarry Smith   PetscFunctionBegin;
13527c922b88SBarry Smith   if (!matout && M != N) {
135329bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1354d4bb536fSBarry Smith   }
1355d4bb536fSBarry Smith 
1356273d9f13SBarry Smith   ierr = MatCreateMPIAIJ(A->comm,A->n,A->m,N,M,0,PETSC_NULL,0,PETSC_NULL,&B);CHKERRQ(ierr);
1357b7c46309SBarry Smith 
1358b7c46309SBarry Smith   /* copy over the A part */
1359ec8511deSBarry Smith   Aloc = (Mat_SeqAIJ*)a->A->data;
1360273d9f13SBarry Smith   m = a->A->m; ai = Aloc->i; aj = Aloc->j; array = Aloc->a;
1361b7c46309SBarry Smith   row = a->rstart;
1362bfec09a0SHong Zhang   for (i=0; i<ai[m]; i++) {aj[i] += a->cstart ;}
1363b7c46309SBarry Smith   for (i=0; i<m; i++) {
1364416022c9SBarry Smith     ierr = MatSetValues(B,ai[i+1]-ai[i],aj,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
1365b7c46309SBarry Smith     row++; array += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
1366b7c46309SBarry Smith   }
1367b7c46309SBarry Smith   aj = Aloc->j;
1368bfec09a0SHong Zhang   for (i=0; i<ai[m]; i++) {aj[i] -= a->cstart ;}
1369b7c46309SBarry Smith 
1370b7c46309SBarry Smith   /* copy over the B part */
1371ec8511deSBarry Smith   Aloc = (Mat_SeqAIJ*)a->B->data;
1372273d9f13SBarry Smith   m = a->B->m;  ai = Aloc->i; aj = Aloc->j; array = Aloc->a;
1373b7c46309SBarry Smith   row  = a->rstart;
1374bfec09a0SHong Zhang   ierr = PetscMalloc((1+ai[m])*sizeof(int),&cols);CHKERRQ(ierr);
1375b0a32e0cSBarry Smith   ct   = cols;
1376bfec09a0SHong Zhang   for (i=0; i<ai[m]; i++) {cols[i] = a->garray[aj[i]];}
1377b7c46309SBarry Smith   for (i=0; i<m; i++) {
1378416022c9SBarry Smith     ierr = MatSetValues(B,ai[i+1]-ai[i],cols,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
1379b7c46309SBarry Smith     row++; array += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
1380b7c46309SBarry Smith   }
1381606d414cSSatish Balay   ierr = PetscFree(ct);CHKERRQ(ierr);
13826d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
13836d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
13847c922b88SBarry Smith   if (matout) {
13850de55854SLois Curfman McInnes     *matout = B;
13860de55854SLois Curfman McInnes   } else {
1387273d9f13SBarry Smith     ierr = MatHeaderCopy(A,B);CHKERRQ(ierr);
13880de55854SLois Curfman McInnes   }
13893a40ed3dSBarry Smith   PetscFunctionReturn(0);
1390b7c46309SBarry Smith }
1391b7c46309SBarry Smith 
13924a2ae208SSatish Balay #undef __FUNCT__
13934a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_MPIAIJ"
13944b967eb1SSatish Balay int MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr)
1395a008b906SSatish Balay {
13964b967eb1SSatish Balay   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
13974b967eb1SSatish Balay   Mat        a = aij->A,b = aij->B;
1398a008b906SSatish Balay   int        ierr,s1,s2,s3;
1399a008b906SSatish Balay 
14003a40ed3dSBarry Smith   PetscFunctionBegin;
14014b967eb1SSatish Balay   ierr = MatGetLocalSize(mat,&s2,&s3);CHKERRQ(ierr);
14024b967eb1SSatish Balay   if (rr) {
1403e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr);
140429bbc08cSBarry Smith     if (s1!=s3) SETERRQ(PETSC_ERR_ARG_SIZ,"right vector non-conforming local size");
14054b967eb1SSatish Balay     /* Overlap communication with computation. */
140643a90d84SBarry Smith     ierr = VecScatterBegin(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx);CHKERRQ(ierr);
1407a008b906SSatish Balay   }
14084b967eb1SSatish Balay   if (ll) {
1409e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr);
141029bbc08cSBarry Smith     if (s1!=s2) SETERRQ(PETSC_ERR_ARG_SIZ,"left vector non-conforming local size");
1411f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,ll,0);CHKERRQ(ierr);
14124b967eb1SSatish Balay   }
14134b967eb1SSatish Balay   /* scale  the diagonal block */
1414f830108cSBarry Smith   ierr = (*a->ops->diagonalscale)(a,ll,rr);CHKERRQ(ierr);
14154b967eb1SSatish Balay 
14164b967eb1SSatish Balay   if (rr) {
14174b967eb1SSatish Balay     /* Do a scatter end and then right scale the off-diagonal block */
141843a90d84SBarry Smith     ierr = VecScatterEnd(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx);CHKERRQ(ierr);
1419f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,0,aij->lvec);CHKERRQ(ierr);
14204b967eb1SSatish Balay   }
14214b967eb1SSatish Balay 
14223a40ed3dSBarry Smith   PetscFunctionReturn(0);
1423a008b906SSatish Balay }
1424a008b906SSatish Balay 
1425a008b906SSatish Balay 
14264a2ae208SSatish Balay #undef __FUNCT__
14274a2ae208SSatish Balay #define __FUNCT__ "MatPrintHelp_MPIAIJ"
14288f6be9afSLois Curfman McInnes int MatPrintHelp_MPIAIJ(Mat A)
1429682d7d0cSBarry Smith {
1430682d7d0cSBarry Smith   Mat_MPIAIJ *a   = (Mat_MPIAIJ*)A->data;
14313a40ed3dSBarry Smith   int        ierr;
1432682d7d0cSBarry Smith 
14333a40ed3dSBarry Smith   PetscFunctionBegin;
14343a40ed3dSBarry Smith   if (!a->rank) {
14353a40ed3dSBarry Smith     ierr = MatPrintHelp_SeqAIJ(a->A);CHKERRQ(ierr);
14363a40ed3dSBarry Smith   }
14373a40ed3dSBarry Smith   PetscFunctionReturn(0);
1438682d7d0cSBarry Smith }
1439682d7d0cSBarry Smith 
14404a2ae208SSatish Balay #undef __FUNCT__
14414a2ae208SSatish Balay #define __FUNCT__ "MatGetBlockSize_MPIAIJ"
14428f6be9afSLois Curfman McInnes int MatGetBlockSize_MPIAIJ(Mat A,int *bs)
14435a838052SSatish Balay {
14443a40ed3dSBarry Smith   PetscFunctionBegin;
14455a838052SSatish Balay   *bs = 1;
14463a40ed3dSBarry Smith   PetscFunctionReturn(0);
14475a838052SSatish Balay }
14484a2ae208SSatish Balay #undef __FUNCT__
14494a2ae208SSatish Balay #define __FUNCT__ "MatSetUnfactored_MPIAIJ"
14508f6be9afSLois Curfman McInnes int MatSetUnfactored_MPIAIJ(Mat A)
1451bb5a7306SBarry Smith {
1452bb5a7306SBarry Smith   Mat_MPIAIJ *a   = (Mat_MPIAIJ*)A->data;
1453bb5a7306SBarry Smith   int        ierr;
14543a40ed3dSBarry Smith 
14553a40ed3dSBarry Smith   PetscFunctionBegin;
1456bb5a7306SBarry Smith   ierr = MatSetUnfactored(a->A);CHKERRQ(ierr);
14573a40ed3dSBarry Smith   PetscFunctionReturn(0);
1458bb5a7306SBarry Smith }
1459bb5a7306SBarry Smith 
14604a2ae208SSatish Balay #undef __FUNCT__
14614a2ae208SSatish Balay #define __FUNCT__ "MatEqual_MPIAIJ"
1462d4bb536fSBarry Smith int MatEqual_MPIAIJ(Mat A,Mat B,PetscTruth *flag)
1463d4bb536fSBarry Smith {
1464d4bb536fSBarry Smith   Mat_MPIAIJ *matB = (Mat_MPIAIJ*)B->data,*matA = (Mat_MPIAIJ*)A->data;
1465d4bb536fSBarry Smith   Mat        a,b,c,d;
1466d4bb536fSBarry Smith   PetscTruth flg;
1467d4bb536fSBarry Smith   int        ierr;
1468d4bb536fSBarry Smith 
14693a40ed3dSBarry Smith   PetscFunctionBegin;
1470d4bb536fSBarry Smith   a = matA->A; b = matA->B;
1471d4bb536fSBarry Smith   c = matB->A; d = matB->B;
1472d4bb536fSBarry Smith 
1473d4bb536fSBarry Smith   ierr = MatEqual(a,c,&flg);CHKERRQ(ierr);
1474d4bb536fSBarry Smith   if (flg == PETSC_TRUE) {
1475d4bb536fSBarry Smith     ierr = MatEqual(b,d,&flg);CHKERRQ(ierr);
1476d4bb536fSBarry Smith   }
1477ca161407SBarry Smith   ierr = MPI_Allreduce(&flg,flag,1,MPI_INT,MPI_LAND,A->comm);CHKERRQ(ierr);
14783a40ed3dSBarry Smith   PetscFunctionReturn(0);
1479d4bb536fSBarry Smith }
1480d4bb536fSBarry Smith 
14814a2ae208SSatish Balay #undef __FUNCT__
14824a2ae208SSatish Balay #define __FUNCT__ "MatCopy_MPIAIJ"
1483cb5b572fSBarry Smith int MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str)
1484cb5b572fSBarry Smith {
1485cb5b572fSBarry Smith   int        ierr;
1486cb5b572fSBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;
1487cb5b572fSBarry Smith   Mat_MPIAIJ *b = (Mat_MPIAIJ *)B->data;
1488cb5b572fSBarry Smith 
1489cb5b572fSBarry Smith   PetscFunctionBegin;
149033f4a19fSKris Buschelman   /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
149133f4a19fSKris Buschelman   if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) {
1492cb5b572fSBarry Smith     /* because of the column compression in the off-processor part of the matrix a->B,
1493cb5b572fSBarry Smith        the number of columns in a->B and b->B may be different, hence we cannot call
1494cb5b572fSBarry Smith        the MatCopy() directly on the two parts. If need be, we can provide a more
1495cb5b572fSBarry Smith        efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices
1496cb5b572fSBarry Smith        then copying the submatrices */
1497cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1498cb5b572fSBarry Smith   } else {
1499cb5b572fSBarry Smith     ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr);
1500cb5b572fSBarry Smith     ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr);
1501cb5b572fSBarry Smith   }
1502cb5b572fSBarry Smith   PetscFunctionReturn(0);
1503cb5b572fSBarry Smith }
1504cb5b572fSBarry Smith 
15054a2ae208SSatish Balay #undef __FUNCT__
15064a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_MPIAIJ"
1507273d9f13SBarry Smith int MatSetUpPreallocation_MPIAIJ(Mat A)
1508273d9f13SBarry Smith {
1509273d9f13SBarry Smith   int        ierr;
1510273d9f13SBarry Smith 
1511273d9f13SBarry Smith   PetscFunctionBegin;
1512273d9f13SBarry Smith   ierr =  MatMPIAIJSetPreallocation(A,PETSC_DEFAULT,0,PETSC_DEFAULT,0);CHKERRQ(ierr);
1513273d9f13SBarry Smith   PetscFunctionReturn(0);
1514273d9f13SBarry Smith }
1515273d9f13SBarry Smith 
1516ac90fabeSBarry Smith #include "petscblaslapack.h"
1517ac90fabeSBarry Smith #undef __FUNCT__
1518ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_MPIAIJ"
1519268466fbSBarry Smith int MatAXPY_MPIAIJ(const PetscScalar a[],Mat X,Mat Y,MatStructure str)
1520ac90fabeSBarry Smith {
152124f910e3SHong Zhang   int        ierr,one=1,i;
1522ac90fabeSBarry Smith   Mat_MPIAIJ *xx = (Mat_MPIAIJ *)X->data,*yy = (Mat_MPIAIJ *)Y->data;
1523ac90fabeSBarry Smith   Mat_SeqAIJ *x,*y;
1524ac90fabeSBarry Smith 
1525ac90fabeSBarry Smith   PetscFunctionBegin;
1526ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
1527ac90fabeSBarry Smith     x = (Mat_SeqAIJ *)xx->A->data;
1528ac90fabeSBarry Smith     y = (Mat_SeqAIJ *)yy->A->data;
1529268466fbSBarry Smith     BLaxpy_(&x->nz,(PetscScalar*)a,x->a,&one,y->a,&one);
1530ac90fabeSBarry Smith     x = (Mat_SeqAIJ *)xx->B->data;
1531ac90fabeSBarry Smith     y = (Mat_SeqAIJ *)yy->B->data;
1532268466fbSBarry Smith     BLaxpy_(&x->nz,(PetscScalar*)a,x->a,&one,y->a,&one);
1533a30b2313SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) {
1534c537a176SHong Zhang     ierr = MatAXPY_SeqAIJ(a,xx->A,yy->A,str);CHKERRQ(ierr);
1535c537a176SHong Zhang 
1536c537a176SHong Zhang     x = (Mat_SeqAIJ *)xx->B->data;
1537a30b2313SHong Zhang     y = (Mat_SeqAIJ *)yy->B->data;
1538a30b2313SHong Zhang     if (y->xtoy && y->XtoY != xx->B) {
1539a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
1540a30b2313SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
1541c537a176SHong Zhang     }
1542a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
154324f910e3SHong Zhang       ierr = MatAXPYGetxtoy_Private(xx->B->m,x->i,x->j,xx->garray,y->i,y->j,yy->garray,&y->xtoy);CHKERRQ(ierr);
1544a30b2313SHong Zhang       y->XtoY = xx->B;
1545c537a176SHong Zhang     }
1546a30b2313SHong Zhang     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += (*a)*(x->a[i]);
1547ac90fabeSBarry Smith   } else {
1548ac90fabeSBarry Smith     ierr = MatAXPY_Basic(a,X,Y,str);CHKERRQ(ierr);
1549ac90fabeSBarry Smith   }
1550ac90fabeSBarry Smith   PetscFunctionReturn(0);
1551ac90fabeSBarry Smith }
1552ac90fabeSBarry Smith 
15538a729477SBarry Smith /* -------------------------------------------------------------------*/
1554cda55fadSBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ,
1555cda55fadSBarry Smith        MatGetRow_MPIAIJ,
1556cda55fadSBarry Smith        MatRestoreRow_MPIAIJ,
1557cda55fadSBarry Smith        MatMult_MPIAIJ,
1558*97304618SKris Buschelman /* 4*/ MatMultAdd_MPIAIJ,
15597c922b88SBarry Smith        MatMultTranspose_MPIAIJ,
15607c922b88SBarry Smith        MatMultTransposeAdd_MPIAIJ,
1561cda55fadSBarry Smith        0,
1562cda55fadSBarry Smith        0,
1563cda55fadSBarry Smith        0,
1564*97304618SKris Buschelman /*10*/ 0,
1565cda55fadSBarry Smith        0,
1566cda55fadSBarry Smith        0,
156744a69424SLois Curfman McInnes        MatRelax_MPIAIJ,
1568b7c46309SBarry Smith        MatTranspose_MPIAIJ,
1569*97304618SKris Buschelman /*15*/ MatGetInfo_MPIAIJ,
1570cda55fadSBarry Smith        MatEqual_MPIAIJ,
1571cda55fadSBarry Smith        MatGetDiagonal_MPIAIJ,
1572cda55fadSBarry Smith        MatDiagonalScale_MPIAIJ,
1573cda55fadSBarry Smith        MatNorm_MPIAIJ,
1574*97304618SKris Buschelman /*20*/ MatAssemblyBegin_MPIAIJ,
1575cda55fadSBarry Smith        MatAssemblyEnd_MPIAIJ,
15761eb62cbbSBarry Smith        0,
1577cda55fadSBarry Smith        MatSetOption_MPIAIJ,
1578cda55fadSBarry Smith        MatZeroEntries_MPIAIJ,
1579*97304618SKris Buschelman /*25*/ MatZeroRows_MPIAIJ,
158087828ca2SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
1581b9617806SBarry Smith        MatLUFactorSymbolic_MPIAIJ_TFS,
1582b9617806SBarry Smith #else
1583cda55fadSBarry Smith        0,
1584b9617806SBarry Smith #endif
1585cda55fadSBarry Smith        0,
1586cda55fadSBarry Smith        0,
1587cda55fadSBarry Smith        0,
1588*97304618SKris Buschelman /*30*/ MatSetUpPreallocation_MPIAIJ,
1589cda55fadSBarry Smith        0,
1590cda55fadSBarry Smith        0,
1591cda55fadSBarry Smith        0,
1592cda55fadSBarry Smith        0,
1593*97304618SKris Buschelman /*35*/ MatDuplicate_MPIAIJ,
1594cda55fadSBarry Smith        0,
1595cda55fadSBarry Smith        0,
1596cda55fadSBarry Smith        0,
1597cda55fadSBarry Smith        0,
1598*97304618SKris Buschelman /*40*/ MatAXPY_MPIAIJ,
1599cda55fadSBarry Smith        MatGetSubMatrices_MPIAIJ,
1600cda55fadSBarry Smith        MatIncreaseOverlap_MPIAIJ,
1601cda55fadSBarry Smith        MatGetValues_MPIAIJ,
1602cb5b572fSBarry Smith        MatCopy_MPIAIJ,
1603*97304618SKris Buschelman /*45*/ MatPrintHelp_MPIAIJ,
1604cda55fadSBarry Smith        MatScale_MPIAIJ,
1605cda55fadSBarry Smith        0,
1606cda55fadSBarry Smith        0,
1607cda55fadSBarry Smith        0,
1608*97304618SKris Buschelman /*50*/ MatGetBlockSize_MPIAIJ,
1609cda55fadSBarry Smith        0,
1610cda55fadSBarry Smith        0,
1611cda55fadSBarry Smith        0,
1612cda55fadSBarry Smith        0,
1613*97304618SKris Buschelman /*55*/ MatFDColoringCreate_MPIAIJ,
1614cda55fadSBarry Smith        0,
1615cda55fadSBarry Smith        MatSetUnfactored_MPIAIJ,
1616cda55fadSBarry Smith        0,
1617cda55fadSBarry Smith        0,
1618*97304618SKris Buschelman /*60*/ MatGetSubMatrix_MPIAIJ,
1619e03a110bSBarry Smith        MatDestroy_MPIAIJ,
1620e03a110bSBarry Smith        MatView_MPIAIJ,
16218a124369SBarry Smith        MatGetPetscMaps_Petsc,
1622a2243be0SBarry Smith        0,
1623*97304618SKris Buschelman /*65*/ 0,
1624a2243be0SBarry Smith        0,
1625a2243be0SBarry Smith        0,
1626a2243be0SBarry Smith        0,
1627a2243be0SBarry Smith        0,
1628*97304618SKris Buschelman /*70*/ 0,
1629a2243be0SBarry Smith        0,
1630a2243be0SBarry Smith        MatSetColoring_MPIAIJ,
1631779c1a83SBarry Smith        MatSetValuesAdic_MPIAIJ,
1632*97304618SKris Buschelman        MatSetValuesAdifor_MPIAIJ,
1633*97304618SKris Buschelman /*75*/ 0,
1634*97304618SKris Buschelman        0,
1635*97304618SKris Buschelman        0,
1636*97304618SKris Buschelman        0,
1637*97304618SKris Buschelman        0,
1638*97304618SKris Buschelman /*80*/ 0,
1639*97304618SKris Buschelman        0,
1640*97304618SKris Buschelman        0,
1641*97304618SKris Buschelman        0,
1642*97304618SKris Buschelman        0,
1643*97304618SKris Buschelman /*85*/ MatLoad_MPIAIJ};
164436ce4990SBarry Smith 
16452e8a6d31SBarry Smith /* ----------------------------------------------------------------------------------------*/
16462e8a6d31SBarry Smith 
1647fb2e594dSBarry Smith EXTERN_C_BEGIN
16484a2ae208SSatish Balay #undef __FUNCT__
16494a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_MPIAIJ"
16502e8a6d31SBarry Smith int MatStoreValues_MPIAIJ(Mat mat)
16512e8a6d31SBarry Smith {
16522e8a6d31SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data;
16532e8a6d31SBarry Smith   int        ierr;
16542e8a6d31SBarry Smith 
16552e8a6d31SBarry Smith   PetscFunctionBegin;
16562e8a6d31SBarry Smith   ierr = MatStoreValues(aij->A);CHKERRQ(ierr);
16572e8a6d31SBarry Smith   ierr = MatStoreValues(aij->B);CHKERRQ(ierr);
16582e8a6d31SBarry Smith   PetscFunctionReturn(0);
16592e8a6d31SBarry Smith }
1660fb2e594dSBarry Smith EXTERN_C_END
16612e8a6d31SBarry Smith 
1662fb2e594dSBarry Smith EXTERN_C_BEGIN
16634a2ae208SSatish Balay #undef __FUNCT__
16644a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_MPIAIJ"
16652e8a6d31SBarry Smith int MatRetrieveValues_MPIAIJ(Mat mat)
16662e8a6d31SBarry Smith {
16672e8a6d31SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data;
16682e8a6d31SBarry Smith   int        ierr;
16692e8a6d31SBarry Smith 
16702e8a6d31SBarry Smith   PetscFunctionBegin;
16712e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr);
16722e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr);
16732e8a6d31SBarry Smith   PetscFunctionReturn(0);
16742e8a6d31SBarry Smith }
1675fb2e594dSBarry Smith EXTERN_C_END
16768a729477SBarry Smith 
1677e090d566SSatish Balay #include "petscpc.h"
167827508adbSBarry Smith EXTERN_C_BEGIN
16794a2ae208SSatish Balay #undef __FUNCT__
1680a23d5eceSKris Buschelman #define __FUNCT__ "MatMPIAIJSetPreallocation_MPIAIJ"
1681d10c748bSKris Buschelman int MatMPIAIJSetPreallocation_MPIAIJ(Mat B,int d_nz,const int d_nnz[],int o_nz,const int o_nnz[])
1682a23d5eceSKris Buschelman {
1683a23d5eceSKris Buschelman   Mat_MPIAIJ   *b;
1684a23d5eceSKris Buschelman   int          ierr,i;
1685a23d5eceSKris Buschelman 
1686a23d5eceSKris Buschelman   PetscFunctionBegin;
1687a23d5eceSKris Buschelman   B->preallocated = PETSC_TRUE;
1688a23d5eceSKris Buschelman   if (d_nz == PETSC_DEFAULT || d_nz == PETSC_DECIDE) d_nz = 5;
1689a23d5eceSKris Buschelman   if (o_nz == PETSC_DEFAULT || o_nz == PETSC_DECIDE) o_nz = 2;
1690a23d5eceSKris Buschelman   if (d_nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"d_nz cannot be less than 0: value %d",d_nz);
1691a23d5eceSKris Buschelman   if (o_nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"o_nz cannot be less than 0: value %d",o_nz);
1692a23d5eceSKris Buschelman   if (d_nnz) {
1693a23d5eceSKris Buschelman     for (i=0; i<B->m; i++) {
1694a23d5eceSKris 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]);
1695a23d5eceSKris Buschelman     }
1696a23d5eceSKris Buschelman   }
1697a23d5eceSKris Buschelman   if (o_nnz) {
1698a23d5eceSKris Buschelman     for (i=0; i<B->m; i++) {
1699a23d5eceSKris 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]);
1700a23d5eceSKris Buschelman     }
1701a23d5eceSKris Buschelman   }
1702a23d5eceSKris Buschelman   b = (Mat_MPIAIJ*)B->data;
1703a23d5eceSKris Buschelman 
1704a23d5eceSKris Buschelman   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,B->m,B->n,d_nz,d_nnz,&b->A);CHKERRQ(ierr);
1705a23d5eceSKris Buschelman   PetscLogObjectParent(B,b->A);
1706a23d5eceSKris Buschelman   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,B->m,B->N,o_nz,o_nnz,&b->B);CHKERRQ(ierr);
1707a23d5eceSKris Buschelman   PetscLogObjectParent(B,b->B);
1708a23d5eceSKris Buschelman 
1709a23d5eceSKris Buschelman   PetscFunctionReturn(0);
1710a23d5eceSKris Buschelman }
1711a23d5eceSKris Buschelman EXTERN_C_END
1712a23d5eceSKris Buschelman 
17130bad9183SKris Buschelman /*MC
17140bad9183SKris Buschelman    MATMPIAIJ = "mpiaij" - A matrix type to be used for parallel sparse matrices.
17150bad9183SKris Buschelman 
17160bad9183SKris Buschelman    Options Database Keys:
17170bad9183SKris Buschelman . -mat_type mpiaij - sets the matrix type to "mpiaij" during a call to MatSetFromOptions()
17180bad9183SKris Buschelman 
17190bad9183SKris Buschelman   Level: beginner
17200bad9183SKris Buschelman 
17210bad9183SKris Buschelman .seealso: MatCreateMPIAIJ
17220bad9183SKris Buschelman M*/
17230bad9183SKris Buschelman 
1724a23d5eceSKris Buschelman EXTERN_C_BEGIN
1725a23d5eceSKris Buschelman #undef __FUNCT__
17264a2ae208SSatish Balay #define __FUNCT__ "MatCreate_MPIAIJ"
1727273d9f13SBarry Smith int MatCreate_MPIAIJ(Mat B)
17288a729477SBarry Smith {
172944cd7ae7SLois Curfman McInnes   Mat_MPIAIJ *b;
1730f1af5d2fSBarry Smith   int        ierr,i,size;
1731416022c9SBarry Smith 
17323a40ed3dSBarry Smith   PetscFunctionBegin;
1733273d9f13SBarry Smith   ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr);
17341d55c564SBarry Smith 
1735b0a32e0cSBarry Smith   ierr            = PetscNew(Mat_MPIAIJ,&b);CHKERRQ(ierr);
1736b0a32e0cSBarry Smith   B->data         = (void*)b;
1737549d3d68SSatish Balay   ierr            = PetscMemzero(b,sizeof(Mat_MPIAIJ));CHKERRQ(ierr);
1738549d3d68SSatish Balay   ierr            = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
173944cd7ae7SLois Curfman McInnes   B->factor       = 0;
174044cd7ae7SLois Curfman McInnes   B->assembled    = PETSC_FALSE;
174190f02eecSBarry Smith   B->mapping      = 0;
1742d6dfbf8fSBarry Smith 
174347794344SBarry Smith   B->insertmode      = NOT_SET_VALUES;
17449eb4d147SSatish Balay   b->size            = size;
1745273d9f13SBarry Smith   ierr = MPI_Comm_rank(B->comm,&b->rank);CHKERRQ(ierr);
17461eb62cbbSBarry Smith 
1747273d9f13SBarry Smith   ierr = PetscSplitOwnership(B->comm,&B->m,&B->M);CHKERRQ(ierr);
1748273d9f13SBarry Smith   ierr = PetscSplitOwnership(B->comm,&B->n,&B->N);CHKERRQ(ierr);
17491eb62cbbSBarry Smith 
1750c7fcc2eaSBarry Smith   /* the information in the maps duplicates the information computed below, eventually
1751c7fcc2eaSBarry Smith      we should remove the duplicate information that is not contained in the maps */
17528a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->m,B->M,&B->rmap);CHKERRQ(ierr);
17538a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->n,B->N,&B->cmap);CHKERRQ(ierr);
1754c7fcc2eaSBarry Smith 
17551eb62cbbSBarry Smith   /* build local table of row and column ownerships */
1756b0a32e0cSBarry Smith   ierr = PetscMalloc(2*(b->size+2)*sizeof(int),&b->rowners);CHKERRQ(ierr);
1757b0a32e0cSBarry Smith   PetscLogObjectMemory(B,2*(b->size+2)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_MPIAIJ));
1758603f58a4SSatish Balay   b->cowners = b->rowners + b->size + 2;
1759273d9f13SBarry Smith   ierr = MPI_Allgather(&B->m,1,MPI_INT,b->rowners+1,1,MPI_INT,B->comm);CHKERRQ(ierr);
176044cd7ae7SLois Curfman McInnes   b->rowners[0] = 0;
176144cd7ae7SLois Curfman McInnes   for (i=2; i<=b->size; i++) {
176244cd7ae7SLois Curfman McInnes     b->rowners[i] += b->rowners[i-1];
17638a729477SBarry Smith   }
176444cd7ae7SLois Curfman McInnes   b->rstart = b->rowners[b->rank];
176544cd7ae7SLois Curfman McInnes   b->rend   = b->rowners[b->rank+1];
1766273d9f13SBarry Smith   ierr = MPI_Allgather(&B->n,1,MPI_INT,b->cowners+1,1,MPI_INT,B->comm);CHKERRQ(ierr);
176744cd7ae7SLois Curfman McInnes   b->cowners[0] = 0;
176844cd7ae7SLois Curfman McInnes   for (i=2; i<=b->size; i++) {
176944cd7ae7SLois Curfman McInnes     b->cowners[i] += b->cowners[i-1];
17708a729477SBarry Smith   }
177144cd7ae7SLois Curfman McInnes   b->cstart = b->cowners[b->rank];
177244cd7ae7SLois Curfman McInnes   b->cend   = b->cowners[b->rank+1];
17738a729477SBarry Smith 
17741eb62cbbSBarry Smith   /* build cache for off array entries formed */
17757e2c5f70SBarry Smith   ierr = MatStashCreate_Private(B->comm,1,&B->stash);CHKERRQ(ierr);
17767c922b88SBarry Smith   b->donotstash  = PETSC_FALSE;
177744cd7ae7SLois Curfman McInnes   b->colmap      = 0;
177844cd7ae7SLois Curfman McInnes   b->garray      = 0;
17797c922b88SBarry Smith   b->roworiented = PETSC_TRUE;
17808a729477SBarry Smith 
17811eb62cbbSBarry Smith   /* stuff used for matrix vector multiply */
17827c922b88SBarry Smith   b->lvec      = PETSC_NULL;
17837c922b88SBarry Smith   b->Mvctx     = PETSC_NULL;
17848a729477SBarry Smith 
17857a0afa10SBarry Smith   /* stuff for MatGetRow() */
178644cd7ae7SLois Curfman McInnes   b->rowindices   = 0;
178744cd7ae7SLois Curfman McInnes   b->rowvalues    = 0;
178844cd7ae7SLois Curfman McInnes   b->getrowactive = PETSC_FALSE;
17891a8d6bc9SBarry Smith 
1790f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
17912e8a6d31SBarry Smith                                      "MatStoreValues_MPIAIJ",
17920c97f09dSSatish Balay                                      MatStoreValues_MPIAIJ);CHKERRQ(ierr);
1793f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
17942e8a6d31SBarry Smith                                      "MatRetrieveValues_MPIAIJ",
17950c97f09dSSatish Balay                                      MatRetrieveValues_MPIAIJ);CHKERRQ(ierr);
1796f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetDiagonalBlock_C",
17975ef9f2a5SBarry Smith 				     "MatGetDiagonalBlock_MPIAIJ",
17980c97f09dSSatish Balay                                      MatGetDiagonalBlock_MPIAIJ);CHKERRQ(ierr);
1799cd0d46ebSvictorle   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsSymmetric_C",
1800cd0d46ebSvictorle 				     "MatIsSymmetric_MPIAIJ",
1801cd0d46ebSvictorle 				     MatIsSymmetric_MPIAIJ); CHKERRQ(ierr);
1802a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAIJSetPreallocation_C",
1803a23d5eceSKris Buschelman 				     "MatMPIAIJSetPreallocation_MPIAIJ",
1804a23d5eceSKris Buschelman 				     MatMPIAIJSetPreallocation_MPIAIJ); CHKERRQ(ierr);
180592b32695SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatDiagonalScaleLocal_C",
180692b32695SKris Buschelman 				     "MatDiagonalScaleLocal_MPIAIJ",
180792b32695SKris Buschelman 				     MatDiagonalScaleLocal_MPIAIJ); CHKERRQ(ierr);
18083a40ed3dSBarry Smith   PetscFunctionReturn(0);
1809d6dfbf8fSBarry Smith }
1810273d9f13SBarry Smith EXTERN_C_END
1811c74985f6SBarry Smith 
18124a2ae208SSatish Balay #undef __FUNCT__
18134a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_MPIAIJ"
18142e8a6d31SBarry Smith int MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
1815d6dfbf8fSBarry Smith {
1816d6dfbf8fSBarry Smith   Mat        mat;
1817416022c9SBarry Smith   Mat_MPIAIJ *a,*oldmat = (Mat_MPIAIJ*)matin->data;
181816d6aa21SSatish Balay   int        ierr;
1819d6dfbf8fSBarry Smith 
18203a40ed3dSBarry Smith   PetscFunctionBegin;
1821416022c9SBarry Smith   *newmat       = 0;
1822273d9f13SBarry Smith   ierr = MatCreate(matin->comm,matin->m,matin->n,matin->M,matin->N,&mat);CHKERRQ(ierr);
1823273d9f13SBarry Smith   ierr = MatSetType(mat,MATMPIAIJ);CHKERRQ(ierr);
1824273d9f13SBarry Smith   a    = (Mat_MPIAIJ*)mat->data;
1825549d3d68SSatish Balay   ierr              = PetscMemcpy(mat->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
1826d6dfbf8fSBarry Smith   mat->factor       = matin->factor;
1827c456f294SBarry Smith   mat->assembled    = PETSC_TRUE;
1828e7641de0SSatish Balay   mat->insertmode   = NOT_SET_VALUES;
1829273d9f13SBarry Smith   mat->preallocated = PETSC_TRUE;
1830d6dfbf8fSBarry Smith 
1831416022c9SBarry Smith   a->rstart       = oldmat->rstart;
1832416022c9SBarry Smith   a->rend         = oldmat->rend;
1833416022c9SBarry Smith   a->cstart       = oldmat->cstart;
1834416022c9SBarry Smith   a->cend         = oldmat->cend;
183517699dbbSLois Curfman McInnes   a->size         = oldmat->size;
183617699dbbSLois Curfman McInnes   a->rank         = oldmat->rank;
1837e7641de0SSatish Balay   a->donotstash   = oldmat->donotstash;
1838e7641de0SSatish Balay   a->roworiented  = oldmat->roworiented;
1839e7641de0SSatish Balay   a->rowindices   = 0;
1840bcd2baecSBarry Smith   a->rowvalues    = 0;
1841bcd2baecSBarry Smith   a->getrowactive = PETSC_FALSE;
1842d6dfbf8fSBarry Smith 
1843549d3d68SSatish Balay   ierr       = PetscMemcpy(a->rowners,oldmat->rowners,2*(a->size+2)*sizeof(int));CHKERRQ(ierr);
18448798bf22SSatish Balay   ierr       = MatStashCreate_Private(matin->comm,1,&mat->stash);CHKERRQ(ierr);
18452ee70a88SLois Curfman McInnes   if (oldmat->colmap) {
1846aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
18470f5bd95cSBarry Smith     ierr = PetscTableCreateCopy(oldmat->colmap,&a->colmap);CHKERRQ(ierr);
1848b1fc9764SSatish Balay #else
1849b0a32e0cSBarry Smith     ierr = PetscMalloc((mat->N)*sizeof(int),&a->colmap);CHKERRQ(ierr);
1850b0a32e0cSBarry Smith     PetscLogObjectMemory(mat,(mat->N)*sizeof(int));
1851273d9f13SBarry Smith     ierr      = PetscMemcpy(a->colmap,oldmat->colmap,(mat->N)*sizeof(int));CHKERRQ(ierr);
1852b1fc9764SSatish Balay #endif
1853416022c9SBarry Smith   } else a->colmap = 0;
18543f41c07dSBarry Smith   if (oldmat->garray) {
185516d6aa21SSatish Balay     int len;
1856273d9f13SBarry Smith     len  = oldmat->B->n;
1857b0a32e0cSBarry Smith     ierr = PetscMalloc((len+1)*sizeof(int),&a->garray);CHKERRQ(ierr);
1858b0a32e0cSBarry Smith     PetscLogObjectMemory(mat,len*sizeof(int));
1859549d3d68SSatish Balay     if (len) { ierr = PetscMemcpy(a->garray,oldmat->garray,len*sizeof(int));CHKERRQ(ierr); }
1860416022c9SBarry Smith   } else a->garray = 0;
1861d6dfbf8fSBarry Smith 
1862416022c9SBarry Smith   ierr =  VecDuplicate(oldmat->lvec,&a->lvec);CHKERRQ(ierr);
1863b0a32e0cSBarry Smith   PetscLogObjectParent(mat,a->lvec);
1864a56f8943SBarry Smith   ierr =  VecScatterCopy(oldmat->Mvctx,&a->Mvctx);CHKERRQ(ierr);
1865b0a32e0cSBarry Smith   PetscLogObjectParent(mat,a->Mvctx);
18662e8a6d31SBarry Smith   ierr =  MatDuplicate(oldmat->A,cpvalues,&a->A);CHKERRQ(ierr);
1867b0a32e0cSBarry Smith   PetscLogObjectParent(mat,a->A);
18682e8a6d31SBarry Smith   ierr =  MatDuplicate(oldmat->B,cpvalues,&a->B);CHKERRQ(ierr);
1869b0a32e0cSBarry Smith   PetscLogObjectParent(mat,a->B);
1870b0a32e0cSBarry Smith   ierr = PetscFListDuplicate(matin->qlist,&mat->qlist);CHKERRQ(ierr);
18718a729477SBarry Smith   *newmat = mat;
18723a40ed3dSBarry Smith   PetscFunctionReturn(0);
18738a729477SBarry Smith }
1874416022c9SBarry Smith 
1875e090d566SSatish Balay #include "petscsys.h"
1876416022c9SBarry Smith 
1877273d9f13SBarry Smith EXTERN_C_BEGIN
18784a2ae208SSatish Balay #undef __FUNCT__
18794a2ae208SSatish Balay #define __FUNCT__ "MatLoad_MPIAIJ"
1880b0a32e0cSBarry Smith int MatLoad_MPIAIJ(PetscViewer viewer,MatType type,Mat *newmat)
1881416022c9SBarry Smith {
1882d65a2f8fSBarry Smith   Mat          A;
188387828ca2SBarry Smith   PetscScalar  *vals,*svals;
188419bcc07fSBarry Smith   MPI_Comm     comm = ((PetscObject)viewer)->comm;
1885416022c9SBarry Smith   MPI_Status   status;
18863a40ed3dSBarry Smith   int          i,nz,ierr,j,rstart,rend,fd;
188717699dbbSLois Curfman McInnes   int          header[4],rank,size,*rowlengths = 0,M,N,m,*rowners,maxnz,*cols;
1888d65a2f8fSBarry Smith   int          *ourlens,*sndcounts = 0,*procsnz = 0,*offlens,jj,*mycols,*smycols;
1889b362ba68SBarry Smith   int          tag = ((PetscObject)viewer)->tag,cend,cstart,n;
1890416022c9SBarry Smith 
18913a40ed3dSBarry Smith   PetscFunctionBegin;
18921dab6e02SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
18931dab6e02SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
189417699dbbSLois Curfman McInnes   if (!rank) {
1895b0a32e0cSBarry Smith     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
18960752156aSBarry Smith     ierr = PetscBinaryRead(fd,(char *)header,4,PETSC_INT);CHKERRQ(ierr);
1897552e946dSBarry Smith     if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
1898d64ed03dSBarry Smith     if (header[3] < 0) {
189929bbc08cSBarry Smith       SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix in special format on disk, cannot load as MPIAIJ");
1900d64ed03dSBarry Smith     }
19016c5fab8fSBarry Smith   }
19026c5fab8fSBarry Smith 
1903ca161407SBarry Smith   ierr = MPI_Bcast(header+1,3,MPI_INT,0,comm);CHKERRQ(ierr);
1904416022c9SBarry Smith   M = header[1]; N = header[2];
1905416022c9SBarry Smith   /* determine ownership of all rows */
190617699dbbSLois Curfman McInnes   m = M/size + ((M % size) > rank);
1907b0a32e0cSBarry Smith   ierr = PetscMalloc((size+2)*sizeof(int),&rowners);CHKERRQ(ierr);
1908ca161407SBarry Smith   ierr = MPI_Allgather(&m,1,MPI_INT,rowners+1,1,MPI_INT,comm);CHKERRQ(ierr);
1909416022c9SBarry Smith   rowners[0] = 0;
191017699dbbSLois Curfman McInnes   for (i=2; i<=size; i++) {
1911416022c9SBarry Smith     rowners[i] += rowners[i-1];
1912416022c9SBarry Smith   }
191317699dbbSLois Curfman McInnes   rstart = rowners[rank];
191417699dbbSLois Curfman McInnes   rend   = rowners[rank+1];
1915416022c9SBarry Smith 
1916416022c9SBarry Smith   /* distribute row lengths to all processors */
1917b0a32e0cSBarry Smith   ierr    = PetscMalloc(2*(rend-rstart+1)*sizeof(int),&ourlens);CHKERRQ(ierr);
1918416022c9SBarry Smith   offlens = ourlens + (rend-rstart);
191917699dbbSLois Curfman McInnes   if (!rank) {
1920b0a32e0cSBarry Smith     ierr = PetscMalloc(M*sizeof(int),&rowlengths);CHKERRQ(ierr);
19210752156aSBarry Smith     ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
1922b0a32e0cSBarry Smith     ierr = PetscMalloc(size*sizeof(int),&sndcounts);CHKERRQ(ierr);
192317699dbbSLois Curfman McInnes     for (i=0; i<size; i++) sndcounts[i] = rowners[i+1] - rowners[i];
1924ca161407SBarry Smith     ierr = MPI_Scatterv(rowlengths,sndcounts,rowners,MPI_INT,ourlens,rend-rstart,MPI_INT,0,comm);CHKERRQ(ierr);
1925606d414cSSatish Balay     ierr = PetscFree(sndcounts);CHKERRQ(ierr);
19263a40ed3dSBarry Smith   } else {
1927ca161407SBarry Smith     ierr = MPI_Scatterv(0,0,0,MPI_INT,ourlens,rend-rstart,MPI_INT,0,comm);CHKERRQ(ierr);
1928416022c9SBarry Smith   }
1929416022c9SBarry Smith 
193017699dbbSLois Curfman McInnes   if (!rank) {
1931416022c9SBarry Smith     /* calculate the number of nonzeros on each processor */
1932b0a32e0cSBarry Smith     ierr = PetscMalloc(size*sizeof(int),&procsnz);CHKERRQ(ierr);
1933549d3d68SSatish Balay     ierr = PetscMemzero(procsnz,size*sizeof(int));CHKERRQ(ierr);
193417699dbbSLois Curfman McInnes     for (i=0; i<size; i++) {
1935416022c9SBarry Smith       for (j=rowners[i]; j< rowners[i+1]; j++) {
1936416022c9SBarry Smith         procsnz[i] += rowlengths[j];
1937416022c9SBarry Smith       }
1938416022c9SBarry Smith     }
1939606d414cSSatish Balay     ierr = PetscFree(rowlengths);CHKERRQ(ierr);
1940416022c9SBarry Smith 
1941416022c9SBarry Smith     /* determine max buffer needed and allocate it */
1942416022c9SBarry Smith     maxnz = 0;
194317699dbbSLois Curfman McInnes     for (i=0; i<size; i++) {
19440452661fSBarry Smith       maxnz = PetscMax(maxnz,procsnz[i]);
1945416022c9SBarry Smith     }
1946b0a32e0cSBarry Smith     ierr = PetscMalloc(maxnz*sizeof(int),&cols);CHKERRQ(ierr);
1947416022c9SBarry Smith 
1948416022c9SBarry Smith     /* read in my part of the matrix column indices  */
1949416022c9SBarry Smith     nz   = procsnz[0];
1950b0a32e0cSBarry Smith     ierr = PetscMalloc(nz*sizeof(int),&mycols);CHKERRQ(ierr);
19510752156aSBarry Smith     ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr);
1952d65a2f8fSBarry Smith 
1953d65a2f8fSBarry Smith     /* read in every one elses and ship off */
195417699dbbSLois Curfman McInnes     for (i=1; i<size; i++) {
1955d65a2f8fSBarry Smith       nz   = procsnz[i];
19560752156aSBarry Smith       ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr);
1957ca161407SBarry Smith       ierr = MPI_Send(cols,nz,MPI_INT,i,tag,comm);CHKERRQ(ierr);
1958d65a2f8fSBarry Smith     }
1959606d414cSSatish Balay     ierr = PetscFree(cols);CHKERRQ(ierr);
19603a40ed3dSBarry Smith   } else {
1961416022c9SBarry Smith     /* determine buffer space needed for message */
1962416022c9SBarry Smith     nz = 0;
1963416022c9SBarry Smith     for (i=0; i<m; i++) {
1964416022c9SBarry Smith       nz += ourlens[i];
1965416022c9SBarry Smith     }
1966b0a32e0cSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(int),&mycols);CHKERRQ(ierr);
1967416022c9SBarry Smith 
1968416022c9SBarry Smith     /* receive message of column indices*/
1969ca161407SBarry Smith     ierr = MPI_Recv(mycols,nz,MPI_INT,0,tag,comm,&status);CHKERRQ(ierr);
1970ca161407SBarry Smith     ierr = MPI_Get_count(&status,MPI_INT,&maxnz);CHKERRQ(ierr);
197129bbc08cSBarry Smith     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
1972416022c9SBarry Smith   }
1973416022c9SBarry Smith 
1974b362ba68SBarry Smith   /* determine column ownership if matrix is not square */
1975b362ba68SBarry Smith   if (N != M) {
1976b362ba68SBarry Smith     n      = N/size + ((N % size) > rank);
1977b362ba68SBarry Smith     ierr   = MPI_Scan(&n,&cend,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr);
1978b362ba68SBarry Smith     cstart = cend - n;
1979b362ba68SBarry Smith   } else {
1980b362ba68SBarry Smith     cstart = rstart;
1981b362ba68SBarry Smith     cend   = rend;
1982fb2e594dSBarry Smith     n      = cend - cstart;
1983b362ba68SBarry Smith   }
1984b362ba68SBarry Smith 
1985416022c9SBarry Smith   /* loop over local rows, determining number of off diagonal entries */
1986549d3d68SSatish Balay   ierr = PetscMemzero(offlens,m*sizeof(int));CHKERRQ(ierr);
1987416022c9SBarry Smith   jj = 0;
1988416022c9SBarry Smith   for (i=0; i<m; i++) {
1989416022c9SBarry Smith     for (j=0; j<ourlens[i]; j++) {
1990b362ba68SBarry Smith       if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++;
1991416022c9SBarry Smith       jj++;
1992416022c9SBarry Smith     }
1993416022c9SBarry Smith   }
1994d65a2f8fSBarry Smith 
1995d65a2f8fSBarry Smith   /* create our matrix */
1996416022c9SBarry Smith   for (i=0; i<m; i++) {
1997416022c9SBarry Smith     ourlens[i] -= offlens[i];
1998416022c9SBarry Smith   }
1999d10c748bSKris Buschelman   ierr = MatCreate(comm,m,n,M,N,&A);CHKERRQ(ierr);
2000d10c748bSKris Buschelman   ierr = MatSetType(A,type);CHKERRQ(ierr);
2001d10c748bSKris Buschelman   ierr = MatMPIAIJSetPreallocation(A,0,ourlens,0,offlens);CHKERRQ(ierr);
2002d10c748bSKris Buschelman 
2003fb2e594dSBarry Smith   ierr = MatSetOption(A,MAT_COLUMNS_SORTED);CHKERRQ(ierr);
2004d65a2f8fSBarry Smith   for (i=0; i<m; i++) {
2005d65a2f8fSBarry Smith     ourlens[i] += offlens[i];
2006d65a2f8fSBarry Smith   }
2007416022c9SBarry Smith 
200817699dbbSLois Curfman McInnes   if (!rank) {
200987828ca2SBarry Smith     ierr = PetscMalloc(maxnz*sizeof(PetscScalar),&vals);CHKERRQ(ierr);
2010416022c9SBarry Smith 
2011416022c9SBarry Smith     /* read in my part of the matrix numerical values  */
2012416022c9SBarry Smith     nz   = procsnz[0];
20130752156aSBarry Smith     ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
2014d65a2f8fSBarry Smith 
2015d65a2f8fSBarry Smith     /* insert into matrix */
2016d65a2f8fSBarry Smith     jj      = rstart;
2017d65a2f8fSBarry Smith     smycols = mycols;
2018d65a2f8fSBarry Smith     svals   = vals;
2019d65a2f8fSBarry Smith     for (i=0; i<m; i++) {
2020d65a2f8fSBarry Smith       ierr = MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
2021d65a2f8fSBarry Smith       smycols += ourlens[i];
2022d65a2f8fSBarry Smith       svals   += ourlens[i];
2023d65a2f8fSBarry Smith       jj++;
2024416022c9SBarry Smith     }
2025416022c9SBarry Smith 
2026d65a2f8fSBarry Smith     /* read in other processors and ship out */
202717699dbbSLois Curfman McInnes     for (i=1; i<size; i++) {
2028416022c9SBarry Smith       nz   = procsnz[i];
20290752156aSBarry Smith       ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
2030ca161407SBarry Smith       ierr = MPI_Send(vals,nz,MPIU_SCALAR,i,A->tag,comm);CHKERRQ(ierr);
2031416022c9SBarry Smith     }
2032606d414cSSatish Balay     ierr = PetscFree(procsnz);CHKERRQ(ierr);
20333a40ed3dSBarry Smith   } else {
2034d65a2f8fSBarry Smith     /* receive numeric values */
203587828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&vals);CHKERRQ(ierr);
2036416022c9SBarry Smith 
2037d65a2f8fSBarry Smith     /* receive message of values*/
2038ca161407SBarry Smith     ierr = MPI_Recv(vals,nz,MPIU_SCALAR,0,A->tag,comm,&status);CHKERRQ(ierr);
2039ca161407SBarry Smith     ierr = MPI_Get_count(&status,MPIU_SCALAR,&maxnz);CHKERRQ(ierr);
204029bbc08cSBarry Smith     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
2041d65a2f8fSBarry Smith 
2042d65a2f8fSBarry Smith     /* insert into matrix */
2043d65a2f8fSBarry Smith     jj      = rstart;
2044d65a2f8fSBarry Smith     smycols = mycols;
2045d65a2f8fSBarry Smith     svals   = vals;
2046d65a2f8fSBarry Smith     for (i=0; i<m; i++) {
2047d65a2f8fSBarry Smith       ierr     = MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
2048d65a2f8fSBarry Smith       smycols += ourlens[i];
2049d65a2f8fSBarry Smith       svals   += ourlens[i];
2050d65a2f8fSBarry Smith       jj++;
2051d65a2f8fSBarry Smith     }
2052d65a2f8fSBarry Smith   }
2053606d414cSSatish Balay   ierr = PetscFree(ourlens);CHKERRQ(ierr);
2054606d414cSSatish Balay   ierr = PetscFree(vals);CHKERRQ(ierr);
2055606d414cSSatish Balay   ierr = PetscFree(mycols);CHKERRQ(ierr);
2056606d414cSSatish Balay   ierr = PetscFree(rowners);CHKERRQ(ierr);
2057d65a2f8fSBarry Smith 
20586d4a8577SBarry Smith   ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20596d4a8577SBarry Smith   ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2060d10c748bSKris Buschelman   *newmat = A;
20613a40ed3dSBarry Smith   PetscFunctionReturn(0);
2062416022c9SBarry Smith }
2063273d9f13SBarry Smith EXTERN_C_END
2064a0ff6018SBarry Smith 
20654a2ae208SSatish Balay #undef __FUNCT__
20664a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_MPIAIJ"
2067a0ff6018SBarry Smith /*
206829da9460SBarry Smith     Not great since it makes two copies of the submatrix, first an SeqAIJ
206929da9460SBarry Smith   in local and then by concatenating the local matrices the end result.
207029da9460SBarry Smith   Writing it directly would be much like MatGetSubMatrices_MPIAIJ()
2071a0ff6018SBarry Smith */
20727b2a1423SBarry Smith int MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,int csize,MatReuse call,Mat *newmat)
2073a0ff6018SBarry Smith {
207400e6dbe6SBarry Smith   int          ierr,i,m,n,rstart,row,rend,nz,*cwork,size,rank,j;
2075ab50ec6bSBarry Smith   int          *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal;
2076fee21e36SBarry Smith   Mat          *local,M,Mreuse;
207787828ca2SBarry Smith   PetscScalar  *vwork,*aa;
207800e6dbe6SBarry Smith   MPI_Comm     comm = mat->comm;
207900e6dbe6SBarry Smith   Mat_SeqAIJ   *aij;
20807e2c5f70SBarry Smith 
2081a0ff6018SBarry Smith 
2082a0ff6018SBarry Smith   PetscFunctionBegin;
20831dab6e02SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
20841dab6e02SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
208500e6dbe6SBarry Smith 
2086fee21e36SBarry Smith   if (call ==  MAT_REUSE_MATRIX) {
2087fee21e36SBarry Smith     ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);CHKERRQ(ierr);
208829bbc08cSBarry Smith     if (!Mreuse) SETERRQ(1,"Submatrix passed in was not used before, cannot reuse");
2089fee21e36SBarry Smith     local = &Mreuse;
2090fee21e36SBarry Smith     ierr  = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&local);CHKERRQ(ierr);
2091fee21e36SBarry Smith   } else {
2092a0ff6018SBarry Smith     ierr   = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr);
2093fee21e36SBarry Smith     Mreuse = *local;
2094606d414cSSatish Balay     ierr   = PetscFree(local);CHKERRQ(ierr);
2095fee21e36SBarry Smith   }
2096a0ff6018SBarry Smith 
2097a0ff6018SBarry Smith   /*
2098a0ff6018SBarry Smith       m - number of local rows
2099a0ff6018SBarry Smith       n - number of columns (same on all processors)
2100a0ff6018SBarry Smith       rstart - first row in new global matrix generated
2101a0ff6018SBarry Smith   */
2102fee21e36SBarry Smith   ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr);
2103a0ff6018SBarry Smith   if (call == MAT_INITIAL_MATRIX) {
2104fee21e36SBarry Smith     aij = (Mat_SeqAIJ*)(Mreuse)->data;
210500e6dbe6SBarry Smith     ii  = aij->i;
210600e6dbe6SBarry Smith     jj  = aij->j;
210700e6dbe6SBarry Smith 
2108a0ff6018SBarry Smith     /*
210900e6dbe6SBarry Smith         Determine the number of non-zeros in the diagonal and off-diagonal
211000e6dbe6SBarry Smith         portions of the matrix in order to do correct preallocation
2111a0ff6018SBarry Smith     */
211200e6dbe6SBarry Smith 
211300e6dbe6SBarry Smith     /* first get start and end of "diagonal" columns */
21146a6a5d1dSBarry Smith     if (csize == PETSC_DECIDE) {
2115ab50ec6bSBarry Smith       ierr = ISGetSize(isrow,&mglobal);CHKERRQ(ierr);
2116ab50ec6bSBarry Smith       if (mglobal == n) { /* square matrix */
2117e2c4fddaSBarry Smith 	nlocal = m;
21186a6a5d1dSBarry Smith       } else {
2119ab50ec6bSBarry Smith         nlocal = n/size + ((n % size) > rank);
2120ab50ec6bSBarry Smith       }
2121ab50ec6bSBarry Smith     } else {
21226a6a5d1dSBarry Smith       nlocal = csize;
21236a6a5d1dSBarry Smith     }
2124ca161407SBarry Smith     ierr   = MPI_Scan(&nlocal,&rend,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr);
212500e6dbe6SBarry Smith     rstart = rend - nlocal;
21266a6a5d1dSBarry Smith     if (rank == size - 1 && rend != n) {
212719e8cfbbSBarry Smith       SETERRQ2(1,"Local column sizes %d do not add up to total number of columns %d",rend,n);
21286a6a5d1dSBarry Smith     }
212900e6dbe6SBarry Smith 
213000e6dbe6SBarry Smith     /* next, compute all the lengths */
2131b0a32e0cSBarry Smith     ierr  = PetscMalloc((2*m+1)*sizeof(int),&dlens);CHKERRQ(ierr);
213200e6dbe6SBarry Smith     olens = dlens + m;
213300e6dbe6SBarry Smith     for (i=0; i<m; i++) {
213400e6dbe6SBarry Smith       jend = ii[i+1] - ii[i];
213500e6dbe6SBarry Smith       olen = 0;
213600e6dbe6SBarry Smith       dlen = 0;
213700e6dbe6SBarry Smith       for (j=0; j<jend; j++) {
213800e6dbe6SBarry Smith         if (*jj < rstart || *jj >= rend) olen++;
213900e6dbe6SBarry Smith         else dlen++;
214000e6dbe6SBarry Smith         jj++;
214100e6dbe6SBarry Smith       }
214200e6dbe6SBarry Smith       olens[i] = olen;
214300e6dbe6SBarry Smith       dlens[i] = dlen;
214400e6dbe6SBarry Smith     }
21452d207970SBarry Smith     ierr = MatCreateMPIAIJ(comm,m,nlocal,PETSC_DECIDE,n,0,dlens,0,olens,&M);CHKERRQ(ierr);
2146606d414cSSatish Balay     ierr = PetscFree(dlens);CHKERRQ(ierr);
2147a0ff6018SBarry Smith   } else {
2148a0ff6018SBarry Smith     int ml,nl;
2149a0ff6018SBarry Smith 
2150a0ff6018SBarry Smith     M = *newmat;
2151a0ff6018SBarry Smith     ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr);
215229bbc08cSBarry Smith     if (ml != m) SETERRQ(PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
2153a0ff6018SBarry Smith     ierr = MatZeroEntries(M);CHKERRQ(ierr);
2154c48de900SBarry Smith     /*
2155c48de900SBarry Smith          The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
2156c48de900SBarry Smith        rather than the slower MatSetValues().
2157c48de900SBarry Smith     */
2158c48de900SBarry Smith     M->was_assembled = PETSC_TRUE;
2159c48de900SBarry Smith     M->assembled     = PETSC_FALSE;
2160a0ff6018SBarry Smith   }
2161a0ff6018SBarry Smith   ierr = MatGetOwnershipRange(M,&rstart,&rend);CHKERRQ(ierr);
2162fee21e36SBarry Smith   aij = (Mat_SeqAIJ*)(Mreuse)->data;
216300e6dbe6SBarry Smith   ii  = aij->i;
216400e6dbe6SBarry Smith   jj  = aij->j;
216500e6dbe6SBarry Smith   aa  = aij->a;
2166a0ff6018SBarry Smith   for (i=0; i<m; i++) {
2167a0ff6018SBarry Smith     row   = rstart + i;
216800e6dbe6SBarry Smith     nz    = ii[i+1] - ii[i];
216900e6dbe6SBarry Smith     cwork = jj;     jj += nz;
217000e6dbe6SBarry Smith     vwork = aa;     aa += nz;
21718c638d02SBarry Smith     ierr = MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr);
2172a0ff6018SBarry Smith   }
2173a0ff6018SBarry Smith 
2174a0ff6018SBarry Smith   ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2175a0ff6018SBarry Smith   ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2176a0ff6018SBarry Smith   *newmat = M;
2177fee21e36SBarry Smith 
2178fee21e36SBarry Smith   /* save submatrix used in processor for next request */
2179fee21e36SBarry Smith   if (call ==  MAT_INITIAL_MATRIX) {
2180fee21e36SBarry Smith     ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr);
2181fee21e36SBarry Smith     ierr = PetscObjectDereference((PetscObject)Mreuse);CHKERRQ(ierr);
2182fee21e36SBarry Smith   }
2183fee21e36SBarry Smith 
2184a0ff6018SBarry Smith   PetscFunctionReturn(0);
2185a0ff6018SBarry Smith }
2186273d9f13SBarry Smith 
21874a2ae208SSatish Balay #undef __FUNCT__
21884a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJSetPreallocation"
2189273d9f13SBarry Smith /*@C
2190273d9f13SBarry Smith    MatMPIAIJSetPreallocation - Creates a sparse parallel matrix in AIJ format
2191273d9f13SBarry Smith    (the default parallel PETSc format).  For good matrix assembly performance
2192273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameters
2193273d9f13SBarry Smith    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
2194273d9f13SBarry Smith    performance can be increased by more than a factor of 50.
2195273d9f13SBarry Smith 
2196273d9f13SBarry Smith    Collective on MPI_Comm
2197273d9f13SBarry Smith 
2198273d9f13SBarry Smith    Input Parameters:
2199273d9f13SBarry Smith +  A - the matrix
2200273d9f13SBarry Smith .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
2201273d9f13SBarry Smith            (same value is used for all local rows)
2202273d9f13SBarry Smith .  d_nnz - array containing the number of nonzeros in the various rows of the
2203273d9f13SBarry Smith            DIAGONAL portion of the local submatrix (possibly different for each row)
2204273d9f13SBarry Smith            or PETSC_NULL, if d_nz is used to specify the nonzero structure.
2205273d9f13SBarry Smith            The size of this array is equal to the number of local rows, i.e 'm'.
2206273d9f13SBarry Smith            You must leave room for the diagonal entry even if it is zero.
2207273d9f13SBarry Smith .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
2208273d9f13SBarry Smith            submatrix (same value is used for all local rows).
2209273d9f13SBarry Smith -  o_nnz - array containing the number of nonzeros in the various rows of the
2210273d9f13SBarry Smith            OFF-DIAGONAL portion of the local submatrix (possibly different for
2211273d9f13SBarry Smith            each row) or PETSC_NULL, if o_nz is used to specify the nonzero
2212273d9f13SBarry Smith            structure. The size of this array is equal to the number
2213273d9f13SBarry Smith            of local rows, i.e 'm'.
2214273d9f13SBarry Smith 
2215273d9f13SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
2216273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2217273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2218273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users manual for details.
2219273d9f13SBarry Smith 
2220273d9f13SBarry Smith    The user MUST specify either the local or global matrix dimensions
2221273d9f13SBarry Smith    (possibly both).
2222273d9f13SBarry Smith 
2223273d9f13SBarry Smith    The parallel matrix is partitioned such that the first m0 rows belong to
2224273d9f13SBarry Smith    process 0, the next m1 rows belong to process 1, the next m2 rows belong
2225273d9f13SBarry Smith    to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.
2226273d9f13SBarry Smith 
2227273d9f13SBarry Smith    The DIAGONAL portion of the local submatrix of a processor can be defined
2228273d9f13SBarry Smith    as the submatrix which is obtained by extraction the part corresponding
2229273d9f13SBarry Smith    to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the
2230273d9f13SBarry Smith    first row that belongs to the processor, and r2 is the last row belonging
2231273d9f13SBarry Smith    to the this processor. This is a square mxm matrix. The remaining portion
2232273d9f13SBarry Smith    of the local submatrix (mxN) constitute the OFF-DIAGONAL portion.
2233273d9f13SBarry Smith 
2234273d9f13SBarry Smith    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
2235273d9f13SBarry Smith 
2236273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible.
2237273d9f13SBarry Smith    We search for consecutive rows with the same nonzero structure, thereby
2238273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2239273d9f13SBarry Smith 
2240273d9f13SBarry Smith    Options Database Keys:
2241273d9f13SBarry Smith +  -mat_aij_no_inode  - Do not use inodes
2242273d9f13SBarry Smith .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2243273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2244273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2245273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2246273d9f13SBarry Smith 
2247273d9f13SBarry Smith    Example usage:
2248273d9f13SBarry Smith 
2249273d9f13SBarry Smith    Consider the following 8x8 matrix with 34 non-zero values, that is
2250273d9f13SBarry Smith    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
2251273d9f13SBarry Smith    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
2252273d9f13SBarry Smith    as follows:
2253273d9f13SBarry Smith 
2254273d9f13SBarry Smith .vb
2255273d9f13SBarry Smith             1  2  0  |  0  3  0  |  0  4
2256273d9f13SBarry Smith     Proc0   0  5  6  |  7  0  0  |  8  0
2257273d9f13SBarry Smith             9  0 10  | 11  0  0  | 12  0
2258273d9f13SBarry Smith     -------------------------------------
2259273d9f13SBarry Smith            13  0 14  | 15 16 17  |  0  0
2260273d9f13SBarry Smith     Proc1   0 18  0  | 19 20 21  |  0  0
2261273d9f13SBarry Smith             0  0  0  | 22 23  0  | 24  0
2262273d9f13SBarry Smith     -------------------------------------
2263273d9f13SBarry Smith     Proc2  25 26 27  |  0  0 28  | 29  0
2264273d9f13SBarry Smith            30  0  0  | 31 32 33  |  0 34
2265273d9f13SBarry Smith .ve
2266273d9f13SBarry Smith 
2267273d9f13SBarry Smith    This can be represented as a collection of submatrices as:
2268273d9f13SBarry Smith 
2269273d9f13SBarry Smith .vb
2270273d9f13SBarry Smith       A B C
2271273d9f13SBarry Smith       D E F
2272273d9f13SBarry Smith       G H I
2273273d9f13SBarry Smith .ve
2274273d9f13SBarry Smith 
2275273d9f13SBarry Smith    Where the submatrices A,B,C are owned by proc0, D,E,F are
2276273d9f13SBarry Smith    owned by proc1, G,H,I are owned by proc2.
2277273d9f13SBarry Smith 
2278273d9f13SBarry Smith    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2279273d9f13SBarry Smith    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2280273d9f13SBarry Smith    The 'M','N' parameters are 8,8, and have the same values on all procs.
2281273d9f13SBarry Smith 
2282273d9f13SBarry Smith    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
2283273d9f13SBarry Smith    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
2284273d9f13SBarry Smith    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
2285273d9f13SBarry Smith    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
2286273d9f13SBarry Smith    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
2287273d9f13SBarry Smith    matrix, ans [DF] as another SeqAIJ matrix.
2288273d9f13SBarry Smith 
2289273d9f13SBarry Smith    When d_nz, o_nz parameters are specified, d_nz storage elements are
2290273d9f13SBarry Smith    allocated for every row of the local diagonal submatrix, and o_nz
2291273d9f13SBarry Smith    storage locations are allocated for every row of the OFF-DIAGONAL submat.
2292273d9f13SBarry Smith    One way to choose d_nz and o_nz is to use the max nonzerors per local
2293273d9f13SBarry Smith    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
2294273d9f13SBarry Smith    In this case, the values of d_nz,o_nz are:
2295273d9f13SBarry Smith .vb
2296273d9f13SBarry Smith      proc0 : dnz = 2, o_nz = 2
2297273d9f13SBarry Smith      proc1 : dnz = 3, o_nz = 2
2298273d9f13SBarry Smith      proc2 : dnz = 1, o_nz = 4
2299273d9f13SBarry Smith .ve
2300273d9f13SBarry Smith    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
2301273d9f13SBarry Smith    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
2302273d9f13SBarry Smith    for proc3. i.e we are using 12+15+10=37 storage locations to store
2303273d9f13SBarry Smith    34 values.
2304273d9f13SBarry Smith 
2305273d9f13SBarry Smith    When d_nnz, o_nnz parameters are specified, the storage is specified
2306273d9f13SBarry Smith    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
2307273d9f13SBarry Smith    In the above case the values for d_nnz,o_nnz are:
2308273d9f13SBarry Smith .vb
2309273d9f13SBarry Smith      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
2310273d9f13SBarry Smith      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
2311273d9f13SBarry Smith      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
2312273d9f13SBarry Smith .ve
2313273d9f13SBarry Smith    Here the space allocated is sum of all the above values i.e 34, and
2314273d9f13SBarry Smith    hence pre-allocation is perfect.
2315273d9f13SBarry Smith 
2316273d9f13SBarry Smith    Level: intermediate
2317273d9f13SBarry Smith 
2318273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
2319273d9f13SBarry Smith 
2320273d9f13SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues()
2321273d9f13SBarry Smith @*/
2322ca01db9bSBarry Smith int MatMPIAIJSetPreallocation(Mat B,int d_nz,const int d_nnz[],int o_nz,const int o_nnz[])
2323273d9f13SBarry Smith {
2324ca01db9bSBarry Smith   int ierr,(*f)(Mat,int,const int[],int,const int[]);
2325273d9f13SBarry Smith 
2326273d9f13SBarry Smith   PetscFunctionBegin;
2327a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatMPIAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
2328a23d5eceSKris Buschelman   if (f) {
2329a23d5eceSKris Buschelman     ierr = (*f)(B,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr);
2330273d9f13SBarry Smith   }
2331273d9f13SBarry Smith   PetscFunctionReturn(0);
2332273d9f13SBarry Smith }
2333273d9f13SBarry Smith 
23344a2ae208SSatish Balay #undef __FUNCT__
23354a2ae208SSatish Balay #define __FUNCT__ "MatCreateMPIAIJ"
2336273d9f13SBarry Smith /*@C
2337273d9f13SBarry Smith    MatCreateMPIAIJ - Creates a sparse parallel matrix in AIJ format
2338273d9f13SBarry Smith    (the default parallel PETSc format).  For good matrix assembly performance
2339273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameters
2340273d9f13SBarry Smith    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
2341273d9f13SBarry Smith    performance can be increased by more than a factor of 50.
2342273d9f13SBarry Smith 
2343273d9f13SBarry Smith    Collective on MPI_Comm
2344273d9f13SBarry Smith 
2345273d9f13SBarry Smith    Input Parameters:
2346273d9f13SBarry Smith +  comm - MPI communicator
2347273d9f13SBarry Smith .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
2348273d9f13SBarry Smith            This value should be the same as the local size used in creating the
2349273d9f13SBarry Smith            y vector for the matrix-vector product y = Ax.
2350273d9f13SBarry Smith .  n - This value should be the same as the local size used in creating the
2351273d9f13SBarry Smith        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
2352273d9f13SBarry Smith        calculated if N is given) For square matrices n is almost always m.
2353273d9f13SBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
2354273d9f13SBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
2355273d9f13SBarry Smith .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
2356273d9f13SBarry Smith            (same value is used for all local rows)
2357273d9f13SBarry Smith .  d_nnz - array containing the number of nonzeros in the various rows of the
2358273d9f13SBarry Smith            DIAGONAL portion of the local submatrix (possibly different for each row)
2359273d9f13SBarry Smith            or PETSC_NULL, if d_nz is used to specify the nonzero structure.
2360273d9f13SBarry Smith            The size of this array is equal to the number of local rows, i.e 'm'.
2361273d9f13SBarry Smith            You must leave room for the diagonal entry even if it is zero.
2362273d9f13SBarry Smith .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
2363273d9f13SBarry Smith            submatrix (same value is used for all local rows).
2364273d9f13SBarry Smith -  o_nnz - array containing the number of nonzeros in the various rows of the
2365273d9f13SBarry Smith            OFF-DIAGONAL portion of the local submatrix (possibly different for
2366273d9f13SBarry Smith            each row) or PETSC_NULL, if o_nz is used to specify the nonzero
2367273d9f13SBarry Smith            structure. The size of this array is equal to the number
2368273d9f13SBarry Smith            of local rows, i.e 'm'.
2369273d9f13SBarry Smith 
2370273d9f13SBarry Smith    Output Parameter:
2371273d9f13SBarry Smith .  A - the matrix
2372273d9f13SBarry Smith 
2373273d9f13SBarry Smith    Notes:
2374273d9f13SBarry Smith    m,n,M,N parameters specify the size of the matrix, and its partitioning across
2375273d9f13SBarry Smith    processors, while d_nz,d_nnz,o_nz,o_nnz parameters specify the approximate
2376273d9f13SBarry Smith    storage requirements for this matrix.
2377273d9f13SBarry Smith 
2378273d9f13SBarry Smith    If PETSC_DECIDE or  PETSC_DETERMINE is used for a particular argument on one
2379273d9f13SBarry Smith    processor than it must be used on all processors that share the object for
2380273d9f13SBarry Smith    that argument.
2381273d9f13SBarry Smith 
2382273d9f13SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
2383273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2384273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2385273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users manual for details.
2386273d9f13SBarry Smith 
2387273d9f13SBarry Smith    The user MUST specify either the local or global matrix dimensions
2388273d9f13SBarry Smith    (possibly both).
2389273d9f13SBarry Smith 
2390273d9f13SBarry Smith    The parallel matrix is partitioned such that the first m0 rows belong to
2391273d9f13SBarry Smith    process 0, the next m1 rows belong to process 1, the next m2 rows belong
2392273d9f13SBarry Smith    to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.
2393273d9f13SBarry Smith 
2394273d9f13SBarry Smith    The DIAGONAL portion of the local submatrix of a processor can be defined
2395273d9f13SBarry Smith    as the submatrix which is obtained by extraction the part corresponding
2396273d9f13SBarry Smith    to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the
2397273d9f13SBarry Smith    first row that belongs to the processor, and r2 is the last row belonging
2398273d9f13SBarry Smith    to the this processor. This is a square mxm matrix. The remaining portion
2399273d9f13SBarry Smith    of the local submatrix (mxN) constitute the OFF-DIAGONAL portion.
2400273d9f13SBarry Smith 
2401273d9f13SBarry Smith    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
2402273d9f13SBarry Smith 
240397d05335SKris Buschelman    When calling this routine with a single process communicator, a matrix of
240497d05335SKris Buschelman    type SEQAIJ is returned.  If a matrix of type MPIAIJ is desired for this
240597d05335SKris Buschelman    type of communicator, use the construction mechanism:
240697d05335SKris Buschelman      MatCreate(...,&A); MatSetType(A,MPIAIJ); MatMPIAIJSetPreallocation(A,...);
240797d05335SKris Buschelman 
2408273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible.
2409273d9f13SBarry Smith    We search for consecutive rows with the same nonzero structure, thereby
2410273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2411273d9f13SBarry Smith 
2412273d9f13SBarry Smith    Options Database Keys:
2413273d9f13SBarry Smith +  -mat_aij_no_inode  - Do not use inodes
2414273d9f13SBarry Smith .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2415273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2416273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2417273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2418273d9f13SBarry Smith 
2419273d9f13SBarry Smith 
2420273d9f13SBarry Smith    Example usage:
2421273d9f13SBarry Smith 
2422273d9f13SBarry Smith    Consider the following 8x8 matrix with 34 non-zero values, that is
2423273d9f13SBarry Smith    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
2424273d9f13SBarry Smith    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
2425273d9f13SBarry Smith    as follows:
2426273d9f13SBarry Smith 
2427273d9f13SBarry Smith .vb
2428273d9f13SBarry Smith             1  2  0  |  0  3  0  |  0  4
2429273d9f13SBarry Smith     Proc0   0  5  6  |  7  0  0  |  8  0
2430273d9f13SBarry Smith             9  0 10  | 11  0  0  | 12  0
2431273d9f13SBarry Smith     -------------------------------------
2432273d9f13SBarry Smith            13  0 14  | 15 16 17  |  0  0
2433273d9f13SBarry Smith     Proc1   0 18  0  | 19 20 21  |  0  0
2434273d9f13SBarry Smith             0  0  0  | 22 23  0  | 24  0
2435273d9f13SBarry Smith     -------------------------------------
2436273d9f13SBarry Smith     Proc2  25 26 27  |  0  0 28  | 29  0
2437273d9f13SBarry Smith            30  0  0  | 31 32 33  |  0 34
2438273d9f13SBarry Smith .ve
2439273d9f13SBarry Smith 
2440273d9f13SBarry Smith    This can be represented as a collection of submatrices as:
2441273d9f13SBarry Smith 
2442273d9f13SBarry Smith .vb
2443273d9f13SBarry Smith       A B C
2444273d9f13SBarry Smith       D E F
2445273d9f13SBarry Smith       G H I
2446273d9f13SBarry Smith .ve
2447273d9f13SBarry Smith 
2448273d9f13SBarry Smith    Where the submatrices A,B,C are owned by proc0, D,E,F are
2449273d9f13SBarry Smith    owned by proc1, G,H,I are owned by proc2.
2450273d9f13SBarry Smith 
2451273d9f13SBarry Smith    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2452273d9f13SBarry Smith    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2453273d9f13SBarry Smith    The 'M','N' parameters are 8,8, and have the same values on all procs.
2454273d9f13SBarry Smith 
2455273d9f13SBarry Smith    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
2456273d9f13SBarry Smith    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
2457273d9f13SBarry Smith    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
2458273d9f13SBarry Smith    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
2459273d9f13SBarry Smith    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
2460273d9f13SBarry Smith    matrix, ans [DF] as another SeqAIJ matrix.
2461273d9f13SBarry Smith 
2462273d9f13SBarry Smith    When d_nz, o_nz parameters are specified, d_nz storage elements are
2463273d9f13SBarry Smith    allocated for every row of the local diagonal submatrix, and o_nz
2464273d9f13SBarry Smith    storage locations are allocated for every row of the OFF-DIAGONAL submat.
2465273d9f13SBarry Smith    One way to choose d_nz and o_nz is to use the max nonzerors per local
2466273d9f13SBarry Smith    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
2467273d9f13SBarry Smith    In this case, the values of d_nz,o_nz are:
2468273d9f13SBarry Smith .vb
2469273d9f13SBarry Smith      proc0 : dnz = 2, o_nz = 2
2470273d9f13SBarry Smith      proc1 : dnz = 3, o_nz = 2
2471273d9f13SBarry Smith      proc2 : dnz = 1, o_nz = 4
2472273d9f13SBarry Smith .ve
2473273d9f13SBarry Smith    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
2474273d9f13SBarry Smith    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
2475273d9f13SBarry Smith    for proc3. i.e we are using 12+15+10=37 storage locations to store
2476273d9f13SBarry Smith    34 values.
2477273d9f13SBarry Smith 
2478273d9f13SBarry Smith    When d_nnz, o_nnz parameters are specified, the storage is specified
2479273d9f13SBarry Smith    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
2480273d9f13SBarry Smith    In the above case the values for d_nnz,o_nnz are:
2481273d9f13SBarry Smith .vb
2482273d9f13SBarry Smith      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
2483273d9f13SBarry Smith      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
2484273d9f13SBarry Smith      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
2485273d9f13SBarry Smith .ve
2486273d9f13SBarry Smith    Here the space allocated is sum of all the above values i.e 34, and
2487273d9f13SBarry Smith    hence pre-allocation is perfect.
2488273d9f13SBarry Smith 
2489273d9f13SBarry Smith    Level: intermediate
2490273d9f13SBarry Smith 
2491273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
2492273d9f13SBarry Smith 
2493273d9f13SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues()
2494273d9f13SBarry Smith @*/
2495ca01db9bSBarry Smith int 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)
2496273d9f13SBarry Smith {
2497273d9f13SBarry Smith   int ierr,size;
2498273d9f13SBarry Smith 
2499273d9f13SBarry Smith   PetscFunctionBegin;
2500273d9f13SBarry Smith   ierr = MatCreate(comm,m,n,M,N,A);CHKERRQ(ierr);
2501273d9f13SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
2502273d9f13SBarry Smith   if (size > 1) {
2503273d9f13SBarry Smith     ierr = MatSetType(*A,MATMPIAIJ);CHKERRQ(ierr);
2504273d9f13SBarry Smith     ierr = MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr);
2505273d9f13SBarry Smith   } else {
2506273d9f13SBarry Smith     ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2507273d9f13SBarry Smith     ierr = MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);CHKERRQ(ierr);
2508273d9f13SBarry Smith   }
2509273d9f13SBarry Smith   PetscFunctionReturn(0);
2510273d9f13SBarry Smith }
2511195d93cdSBarry Smith 
25124a2ae208SSatish Balay #undef __FUNCT__
25134a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJGetSeqAIJ"
2514268466fbSBarry Smith int MatMPIAIJGetSeqAIJ(Mat A,Mat *Ad,Mat *Ao,int *colmap[])
2515195d93cdSBarry Smith {
2516195d93cdSBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;
2517195d93cdSBarry Smith   PetscFunctionBegin;
2518195d93cdSBarry Smith   *Ad     = a->A;
2519195d93cdSBarry Smith   *Ao     = a->B;
2520195d93cdSBarry Smith   *colmap = a->garray;
2521195d93cdSBarry Smith   PetscFunctionReturn(0);
2522195d93cdSBarry Smith }
2523a2243be0SBarry Smith 
2524a2243be0SBarry Smith #undef __FUNCT__
2525a2243be0SBarry Smith #define __FUNCT__ "MatSetColoring_MPIAIJ"
2526a2243be0SBarry Smith int MatSetColoring_MPIAIJ(Mat A,ISColoring coloring)
2527a2243be0SBarry Smith {
252808b6dcc0SBarry Smith   int        ierr,i;
2529a2243be0SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2530a2243be0SBarry Smith 
2531a2243be0SBarry Smith   PetscFunctionBegin;
2532a2243be0SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
253308b6dcc0SBarry Smith     ISColoringValue *allcolors,*colors;
2534a2243be0SBarry Smith     ISColoring      ocoloring;
2535a2243be0SBarry Smith 
2536a2243be0SBarry Smith     /* set coloring for diagonal portion */
2537a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->A,coloring);CHKERRQ(ierr);
2538a2243be0SBarry Smith 
2539a2243be0SBarry Smith     /* set coloring for off-diagonal portion */
254008b6dcc0SBarry Smith     ierr = ISAllGatherColors(A->comm,coloring->n,coloring->colors,PETSC_NULL,&allcolors);CHKERRQ(ierr);
254108b6dcc0SBarry Smith     ierr = PetscMalloc((a->B->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
2542a2243be0SBarry Smith     for (i=0; i<a->B->n; i++) {
2543a2243be0SBarry Smith       colors[i] = allcolors[a->garray[i]];
2544a2243be0SBarry Smith     }
2545a2243be0SBarry Smith     ierr = PetscFree(allcolors);CHKERRQ(ierr);
2546a2243be0SBarry Smith     ierr = ISColoringCreate(MPI_COMM_SELF,a->B->n,colors,&ocoloring);CHKERRQ(ierr);
2547a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr);
2548a2243be0SBarry Smith     ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr);
2549a2243be0SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
255008b6dcc0SBarry Smith     ISColoringValue *colors;
255108b6dcc0SBarry Smith     int             *larray;
2552a2243be0SBarry Smith     ISColoring      ocoloring;
2553a2243be0SBarry Smith 
2554a2243be0SBarry Smith     /* set coloring for diagonal portion */
2555a2243be0SBarry Smith     ierr = PetscMalloc((a->A->n+1)*sizeof(int),&larray);CHKERRQ(ierr);
2556a2243be0SBarry Smith     for (i=0; i<a->A->n; i++) {
2557a2243be0SBarry Smith       larray[i] = i + a->cstart;
2558a2243be0SBarry Smith     }
2559a2243be0SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->A->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
256008b6dcc0SBarry Smith     ierr = PetscMalloc((a->A->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
2561a2243be0SBarry Smith     for (i=0; i<a->A->n; i++) {
2562a2243be0SBarry Smith       colors[i] = coloring->colors[larray[i]];
2563a2243be0SBarry Smith     }
2564a2243be0SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
256512c595b3SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,a->A->n,colors,&ocoloring);CHKERRQ(ierr);
2566a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->A,ocoloring);CHKERRQ(ierr);
2567a2243be0SBarry Smith     ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr);
2568a2243be0SBarry Smith 
2569a2243be0SBarry Smith     /* set coloring for off-diagonal portion */
2570a2243be0SBarry Smith     ierr = PetscMalloc((a->B->n+1)*sizeof(int),&larray);CHKERRQ(ierr);
2571a2243be0SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->B->n,a->garray,PETSC_NULL,larray);CHKERRQ(ierr);
257208b6dcc0SBarry Smith     ierr = PetscMalloc((a->B->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
2573a2243be0SBarry Smith     for (i=0; i<a->B->n; i++) {
2574a2243be0SBarry Smith       colors[i] = coloring->colors[larray[i]];
2575a2243be0SBarry Smith     }
2576a2243be0SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
2577a2243be0SBarry Smith     ierr = ISColoringCreate(MPI_COMM_SELF,a->B->n,colors,&ocoloring);CHKERRQ(ierr);
2578a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr);
2579a2243be0SBarry Smith     ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr);
2580a2243be0SBarry Smith   } else {
2581a2243be0SBarry Smith     SETERRQ1(1,"No support ISColoringType %d",coloring->ctype);
2582a2243be0SBarry Smith   }
2583a2243be0SBarry Smith 
2584a2243be0SBarry Smith   PetscFunctionReturn(0);
2585a2243be0SBarry Smith }
2586a2243be0SBarry Smith 
2587a2243be0SBarry Smith #undef __FUNCT__
2588779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdic_MPIAIJ"
2589779c1a83SBarry Smith int MatSetValuesAdic_MPIAIJ(Mat A,void *advalues)
2590a2243be0SBarry Smith {
2591a2243be0SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2592a2243be0SBarry Smith   int        ierr;
2593a2243be0SBarry Smith 
2594a2243be0SBarry Smith   PetscFunctionBegin;
2595779c1a83SBarry Smith   ierr = MatSetValuesAdic_SeqAIJ(a->A,advalues);CHKERRQ(ierr);
2596779c1a83SBarry Smith   ierr = MatSetValuesAdic_SeqAIJ(a->B,advalues);CHKERRQ(ierr);
2597779c1a83SBarry Smith   PetscFunctionReturn(0);
2598779c1a83SBarry Smith }
2599779c1a83SBarry Smith 
2600779c1a83SBarry Smith #undef __FUNCT__
2601779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdifor_MPIAIJ"
2602779c1a83SBarry Smith int MatSetValuesAdifor_MPIAIJ(Mat A,int nl,void *advalues)
2603779c1a83SBarry Smith {
2604779c1a83SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2605779c1a83SBarry Smith   int        ierr;
2606779c1a83SBarry Smith 
2607779c1a83SBarry Smith   PetscFunctionBegin;
2608779c1a83SBarry Smith   ierr = MatSetValuesAdifor_SeqAIJ(a->A,nl,advalues);CHKERRQ(ierr);
2609779c1a83SBarry Smith   ierr = MatSetValuesAdifor_SeqAIJ(a->B,nl,advalues);CHKERRQ(ierr);
2610a2243be0SBarry Smith   PetscFunctionReturn(0);
2611a2243be0SBarry Smith }
2612c5d6d63eSBarry Smith 
2613c5d6d63eSBarry Smith #undef __FUNCT__
261451dd7536SBarry Smith #define __FUNCT__ "MatMerge"
2615c5d6d63eSBarry Smith /*@C
261651dd7536SBarry Smith       MatMerge - Creates a single large PETSc matrix by concatinating sequential
261751dd7536SBarry Smith                  matrices from each processor
2618c5d6d63eSBarry Smith 
2619c5d6d63eSBarry Smith     Collective on MPI_Comm
2620c5d6d63eSBarry Smith 
2621c5d6d63eSBarry Smith    Input Parameters:
262251dd7536SBarry Smith +    comm - the communicators the parallel matrix will live on
262351dd7536SBarry Smith -    inmat - the input sequential matrices
262451dd7536SBarry Smith 
262551dd7536SBarry Smith    Output Parameter:
262651dd7536SBarry Smith .    outmat - the parallel matrix generated
2627c5d6d63eSBarry Smith 
26287e25d530SSatish Balay     Level: advanced
26297e25d530SSatish Balay 
2630c5d6d63eSBarry Smith    Notes: The number of columns of the matrix in EACH of the seperate files
2631c5d6d63eSBarry Smith       MUST be the same.
2632c5d6d63eSBarry Smith 
2633c5d6d63eSBarry Smith @*/
263451dd7536SBarry Smith int MatMerge(MPI_Comm comm,Mat inmat, Mat *outmat)
2635c5d6d63eSBarry Smith {
2636a15ac5e4SHong Zhang   int         ierr,m,n,i,rstart,*indx,nnz,I,*dnz,*onz;
2637c5d6d63eSBarry Smith   PetscScalar *values;
263851dd7536SBarry Smith   PetscMap    columnmap,rowmap;
2639c5d6d63eSBarry Smith 
2640c5d6d63eSBarry Smith   PetscFunctionBegin;
2641c5d6d63eSBarry Smith 
264251dd7536SBarry Smith   ierr = MatGetSize(inmat,&m,&n);CHKERRQ(ierr);
264351dd7536SBarry Smith 
264451dd7536SBarry Smith   /* count nonzeros in each row, for diagonal and off diagonal portion of matrix */
264551dd7536SBarry Smith   ierr = PetscMapCreate(comm,&columnmap);CHKERRQ(ierr);
264651dd7536SBarry Smith   ierr = PetscMapSetSize(columnmap,n);CHKERRQ(ierr);
264751dd7536SBarry Smith   ierr = PetscMapSetType(columnmap,MAP_MPI);CHKERRQ(ierr);
264851dd7536SBarry Smith   ierr = PetscMapGetLocalSize(columnmap,&n);CHKERRQ(ierr);
264951dd7536SBarry Smith   ierr = PetscMapDestroy(columnmap);CHKERRQ(ierr);
265051dd7536SBarry Smith 
265151dd7536SBarry Smith   ierr = PetscMapCreate(comm,&rowmap);CHKERRQ(ierr);
265251dd7536SBarry Smith   ierr = PetscMapSetLocalSize(rowmap,m);CHKERRQ(ierr);
265351dd7536SBarry Smith   ierr = PetscMapSetType(rowmap,MAP_MPI);CHKERRQ(ierr);
265451dd7536SBarry Smith   ierr = PetscMapGetLocalRange(rowmap,&rstart,0);CHKERRQ(ierr);
265551dd7536SBarry Smith   ierr = PetscMapDestroy(rowmap);CHKERRQ(ierr);
265651dd7536SBarry Smith 
265751dd7536SBarry Smith   ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr);
2658c5d6d63eSBarry Smith   for (i=0;i<m;i++) {
265951dd7536SBarry Smith     ierr = MatGetRow(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
266051dd7536SBarry Smith     ierr = MatPreallocateSet(i+rstart,nnz,indx,dnz,onz);CHKERRQ(ierr);
266151dd7536SBarry Smith     ierr = MatRestoreRow(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
2662c5d6d63eSBarry Smith   }
266351dd7536SBarry Smith   ierr = MatCreateMPIAIJ(comm,m,n,PETSC_DETERMINE,PETSC_DETERMINE,0,dnz,0,onz,outmat);CHKERRQ(ierr);
266451dd7536SBarry Smith   ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr);
2665c5d6d63eSBarry Smith 
266651dd7536SBarry Smith   for (i=0;i<m;i++) {
266751dd7536SBarry Smith     ierr = MatGetRow(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
266851dd7536SBarry Smith     I    = i + rstart;
266951dd7536SBarry Smith     ierr = MatSetValues(*outmat,1,&I,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr);
267051dd7536SBarry Smith     ierr = MatRestoreRow(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
267151dd7536SBarry Smith   }
267251dd7536SBarry Smith   ierr = MatDestroy(inmat);CHKERRQ(ierr);
267351dd7536SBarry Smith   ierr = MatAssemblyBegin(*outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
267451dd7536SBarry Smith   ierr = MatAssemblyEnd(*outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
267551dd7536SBarry Smith 
2676c5d6d63eSBarry Smith   PetscFunctionReturn(0);
2677c5d6d63eSBarry Smith }
2678c5d6d63eSBarry Smith 
2679c5d6d63eSBarry Smith #undef __FUNCT__
2680c5d6d63eSBarry Smith #define __FUNCT__ "MatFileSplit"
2681c5d6d63eSBarry Smith int MatFileSplit(Mat A,char *outfile)
2682c5d6d63eSBarry Smith {
2683c5d6d63eSBarry Smith   int         ierr,rank,len,m,N,i,rstart,*indx,nnz;
2684c5d6d63eSBarry Smith   PetscViewer out;
2685c5d6d63eSBarry Smith   char        *name;
2686c5d6d63eSBarry Smith   Mat         B;
2687c5d6d63eSBarry Smith   PetscScalar *values;
2688c5d6d63eSBarry Smith 
2689c5d6d63eSBarry Smith   PetscFunctionBegin;
2690c5d6d63eSBarry Smith 
2691c5d6d63eSBarry Smith   ierr = MatGetLocalSize(A,&m,0);CHKERRQ(ierr);
2692c5d6d63eSBarry Smith   ierr = MatGetSize(A,0,&N);CHKERRQ(ierr);
2693c5d6d63eSBarry Smith   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,m,N,0,0,&B);CHKERRQ(ierr);
2694c5d6d63eSBarry Smith   ierr = MatGetOwnershipRange(A,&rstart,0);CHKERRQ(ierr);
2695c5d6d63eSBarry Smith   for (i=0;i<m;i++) {
2696c5d6d63eSBarry Smith     ierr = MatGetRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr);
2697c5d6d63eSBarry Smith     ierr = MatSetValues(B,1,&i,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr);
2698c5d6d63eSBarry Smith     ierr = MatRestoreRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr);
2699c5d6d63eSBarry Smith   }
2700c5d6d63eSBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2701c5d6d63eSBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2702c5d6d63eSBarry Smith 
2703c5d6d63eSBarry Smith   ierr = MPI_Comm_rank(A->comm,&rank);CHKERRQ(ierr);
2704c5d6d63eSBarry Smith   ierr = PetscStrlen(outfile,&len);CHKERRQ(ierr);
2705c5d6d63eSBarry Smith   ierr = PetscMalloc((len+5)*sizeof(char),&name);CHKERRQ(ierr);
2706c5d6d63eSBarry Smith   sprintf(name,"%s.%d",outfile,rank);
2707c5d6d63eSBarry Smith   ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,name,PETSC_BINARY_CREATE,&out);CHKERRQ(ierr);
2708c5d6d63eSBarry Smith   ierr = PetscFree(name);
2709c5d6d63eSBarry Smith   ierr = MatView(B,out);CHKERRQ(ierr);
2710c5d6d63eSBarry Smith   ierr = PetscViewerDestroy(out);CHKERRQ(ierr);
2711c5d6d63eSBarry Smith   ierr = MatDestroy(B);CHKERRQ(ierr);
2712c5d6d63eSBarry Smith   PetscFunctionReturn(0);
2713c5d6d63eSBarry Smith }
2714