xref: /petsc/src/mat/impls/aij/mpi/mpiaij.c (revision bd3bf7d396b69e647ab833ab2354dcf1423069b4)
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 
7ca44d042SBarry Smith EXTERN int MatSetUpMultiply_MPIAIJ(Mat);
8ca44d042SBarry Smith EXTERN int DisAssemble_MPIAIJ(Mat);
987828ca2SBarry Smith EXTERN int MatSetValues_SeqAIJ(Mat,int,int*,int,int*,PetscScalar*,InsertMode);
1087828ca2SBarry Smith EXTERN int MatGetRow_SeqAIJ(Mat,int,int*,int**,PetscScalar**);
1187828ca2SBarry Smith EXTERN int MatRestoreRow_SeqAIJ(Mat,int,int*,int**,PetscScalar**);
12ca44d042SBarry Smith EXTERN int MatPrintHelp_SeqAIJ(Mat);
139b9367d5SHong Zhang EXTERN int MatUseSuperLU_DIST_MPIAIJ(Mat);
14bc5ccf88SSatish Balay 
150f5bd95cSBarry Smith /*
160f5bd95cSBarry Smith   Local utility routine that creates a mapping from the global column
179e25ed09SBarry Smith number to the local number in the off-diagonal part of the local
180f5bd95cSBarry Smith storage of the matrix.  When PETSC_USE_CTABLE is used this is scalable at
190f5bd95cSBarry Smith a slightly higher hash table cost; without it it is not scalable (each processor
200f5bd95cSBarry Smith has an order N integer array but is fast to acess.
219e25ed09SBarry Smith */
224a2ae208SSatish Balay #undef __FUNCT__
234a2ae208SSatish Balay #define __FUNCT__ "CreateColmap_MPIAIJ_Private"
240a198c4cSBarry Smith int CreateColmap_MPIAIJ_Private(Mat mat)
259e25ed09SBarry Smith {
2644a69424SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
27273d9f13SBarry Smith   int        n = aij->B->n,i,ierr;
28dbb450caSBarry Smith 
293a40ed3dSBarry Smith   PetscFunctionBegin;
30aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
31273d9f13SBarry Smith   ierr = PetscTableCreate(n,&aij->colmap);CHKERRQ(ierr);
32b1fc9764SSatish Balay   for (i=0; i<n; i++){
330f5bd95cSBarry Smith     ierr = PetscTableAdd(aij->colmap,aij->garray[i]+1,i+1);CHKERRQ(ierr);
34b1fc9764SSatish Balay   }
35b1fc9764SSatish Balay #else
36b0a32e0cSBarry Smith   ierr = PetscMalloc((mat->N+1)*sizeof(int),&aij->colmap);CHKERRQ(ierr);
37b0a32e0cSBarry Smith   PetscLogObjectMemory(mat,mat->N*sizeof(int));
38273d9f13SBarry Smith   ierr = PetscMemzero(aij->colmap,mat->N*sizeof(int));CHKERRQ(ierr);
39905e6a2fSBarry Smith   for (i=0; i<n; i++) aij->colmap[aij->garray[i]] = i+1;
40b1fc9764SSatish Balay #endif
413a40ed3dSBarry Smith   PetscFunctionReturn(0);
429e25ed09SBarry Smith }
439e25ed09SBarry Smith 
440520107fSSatish Balay #define CHUNKSIZE   15
4530770e4dSSatish Balay #define MatSetValues_SeqAIJ_A_Private(row,col,value,addv) \
460520107fSSatish Balay { \
470520107fSSatish Balay  \
480520107fSSatish Balay     rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift; \
4930770e4dSSatish Balay     rmax = aimax[row]; nrow = ailen[row];  \
50f5e9677aSSatish Balay     col1 = col - shift; \
51f5e9677aSSatish Balay      \
52ba4e3ef2SSatish Balay     low = 0; high = nrow; \
53ba4e3ef2SSatish Balay     while (high-low > 5) { \
54ba4e3ef2SSatish Balay       t = (low+high)/2; \
55ba4e3ef2SSatish Balay       if (rp[t] > col) high = t; \
56ba4e3ef2SSatish Balay       else             low  = t; \
57ba4e3ef2SSatish Balay     } \
582335bdd2SSatish Balay       for (_i=low; _i<high; _i++) { \
59f5e9677aSSatish Balay         if (rp[_i] > col1) break; \
60f5e9677aSSatish Balay         if (rp[_i] == col1) { \
610520107fSSatish Balay           if (addv == ADD_VALUES) ap[_i] += value;   \
620520107fSSatish Balay           else                  ap[_i] = value; \
6330770e4dSSatish Balay           goto a_noinsert; \
640520107fSSatish Balay         } \
650520107fSSatish Balay       }  \
6689280ab3SLois Curfman McInnes       if (nonew == 1) goto a_noinsert; \
6729bbc08cSBarry Smith       else if (nonew == -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero into matrix"); \
680520107fSSatish Balay       if (nrow >= rmax) { \
690520107fSSatish Balay         /* there is no extra room in row, therefore enlarge */ \
70273d9f13SBarry Smith         int    new_nz = ai[am] + CHUNKSIZE,len,*new_i,*new_j; \
7187828ca2SBarry Smith         PetscScalar *new_a; \
720520107fSSatish Balay  \
7329bbc08cSBarry Smith         if (nonew == -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix"); \
7489280ab3SLois Curfman McInnes  \
750520107fSSatish Balay         /* malloc new storage space */ \
7687828ca2SBarry Smith         len     = new_nz*(sizeof(int)+sizeof(PetscScalar))+(am+1)*sizeof(int); \
77b0a32e0cSBarry Smith         ierr    = PetscMalloc(len,&new_a);CHKERRQ(ierr); \
780520107fSSatish Balay         new_j   = (int*)(new_a + new_nz); \
790520107fSSatish Balay         new_i   = new_j + new_nz; \
800520107fSSatish Balay  \
810520107fSSatish Balay         /* copy over old data into new slots */ \
820520107fSSatish Balay         for (ii=0; ii<row+1; ii++) {new_i[ii] = ai[ii];} \
83273d9f13SBarry Smith         for (ii=row+1; ii<am+1; ii++) {new_i[ii] = ai[ii]+CHUNKSIZE;} \
84549d3d68SSatish Balay         ierr = PetscMemcpy(new_j,aj,(ai[row]+nrow+shift)*sizeof(int));CHKERRQ(ierr); \
850520107fSSatish Balay         len = (new_nz - CHUNKSIZE - ai[row] - nrow - shift); \
86549d3d68SSatish Balay         ierr = PetscMemcpy(new_j+ai[row]+shift+nrow+CHUNKSIZE,aj+ai[row]+shift+nrow, \
87549d3d68SSatish Balay                                                            len*sizeof(int));CHKERRQ(ierr); \
8887828ca2SBarry Smith         ierr = PetscMemcpy(new_a,aa,(ai[row]+nrow+shift)*sizeof(PetscScalar));CHKERRQ(ierr); \
89549d3d68SSatish Balay         ierr = PetscMemcpy(new_a+ai[row]+shift+nrow+CHUNKSIZE,aa+ai[row]+shift+nrow, \
9087828ca2SBarry Smith                                                            len*sizeof(PetscScalar));CHKERRQ(ierr);  \
910520107fSSatish Balay         /* free up old matrix storage */ \
92f5e9677aSSatish Balay  \
93606d414cSSatish Balay         ierr = PetscFree(a->a);CHKERRQ(ierr);  \
94606d414cSSatish Balay         if (!a->singlemalloc) { \
95606d414cSSatish Balay            ierr = PetscFree(a->i);CHKERRQ(ierr); \
96606d414cSSatish Balay            ierr = PetscFree(a->j);CHKERRQ(ierr); \
97606d414cSSatish Balay         } \
980520107fSSatish Balay         aa = a->a = new_a; ai = a->i = new_i; aj = a->j = new_j;  \
997c922b88SBarry Smith         a->singlemalloc = PETSC_TRUE; \
1000520107fSSatish Balay  \
1010520107fSSatish Balay         rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift; \
10230770e4dSSatish Balay         rmax = aimax[row] = aimax[row] + CHUNKSIZE; \
10387828ca2SBarry Smith         PetscLogObjectMemory(A,CHUNKSIZE*(sizeof(int) + sizeof(PetscScalar))); \
1040520107fSSatish Balay         a->maxnz += CHUNKSIZE; \
1050520107fSSatish Balay         a->reallocs++; \
1060520107fSSatish Balay       } \
1070520107fSSatish Balay       N = nrow++ - 1; a->nz++; \
1080520107fSSatish Balay       /* shift up all the later entries in this row */ \
1090520107fSSatish Balay       for (ii=N; ii>=_i; ii--) { \
1100520107fSSatish Balay         rp[ii+1] = rp[ii]; \
1110520107fSSatish Balay         ap[ii+1] = ap[ii]; \
1120520107fSSatish Balay       } \
113f5e9677aSSatish Balay       rp[_i] = col1;  \
1140520107fSSatish Balay       ap[_i] = value;  \
11530770e4dSSatish Balay       a_noinsert: ; \
1160520107fSSatish Balay       ailen[row] = nrow; \
1170520107fSSatish Balay }
1180a198c4cSBarry Smith 
11930770e4dSSatish Balay #define MatSetValues_SeqAIJ_B_Private(row,col,value,addv) \
12030770e4dSSatish Balay { \
12130770e4dSSatish Balay  \
12230770e4dSSatish Balay     rp   = bj + bi[row] + shift; ap = ba + bi[row] + shift; \
12330770e4dSSatish Balay     rmax = bimax[row]; nrow = bilen[row];  \
12430770e4dSSatish Balay     col1 = col - shift; \
12530770e4dSSatish Balay      \
126ba4e3ef2SSatish Balay     low = 0; high = nrow; \
127ba4e3ef2SSatish Balay     while (high-low > 5) { \
128ba4e3ef2SSatish Balay       t = (low+high)/2; \
129ba4e3ef2SSatish Balay       if (rp[t] > col) high = t; \
130ba4e3ef2SSatish Balay       else             low  = t; \
131ba4e3ef2SSatish Balay     } \
1322335bdd2SSatish Balay        for (_i=low; _i<high; _i++) { \
13330770e4dSSatish Balay         if (rp[_i] > col1) break; \
13430770e4dSSatish Balay         if (rp[_i] == col1) { \
13530770e4dSSatish Balay           if (addv == ADD_VALUES) ap[_i] += value;   \
13630770e4dSSatish Balay           else                  ap[_i] = value; \
13730770e4dSSatish Balay           goto b_noinsert; \
13830770e4dSSatish Balay         } \
13930770e4dSSatish Balay       }  \
14089280ab3SLois Curfman McInnes       if (nonew == 1) goto b_noinsert; \
14129bbc08cSBarry Smith       else if (nonew == -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero into matrix"); \
14230770e4dSSatish Balay       if (nrow >= rmax) { \
14330770e4dSSatish Balay         /* there is no extra room in row, therefore enlarge */ \
144273d9f13SBarry Smith         int    new_nz = bi[bm] + CHUNKSIZE,len,*new_i,*new_j; \
14587828ca2SBarry Smith         PetscScalar *new_a; \
14630770e4dSSatish Balay  \
14729bbc08cSBarry Smith         if (nonew == -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix"); \
14889280ab3SLois Curfman McInnes  \
14930770e4dSSatish Balay         /* malloc new storage space */ \
15087828ca2SBarry Smith         len     = new_nz*(sizeof(int)+sizeof(PetscScalar))+(bm+1)*sizeof(int); \
151b0a32e0cSBarry Smith         ierr    = PetscMalloc(len,&new_a);CHKERRQ(ierr); \
15230770e4dSSatish Balay         new_j   = (int*)(new_a + new_nz); \
15330770e4dSSatish Balay         new_i   = new_j + new_nz; \
15430770e4dSSatish Balay  \
15530770e4dSSatish Balay         /* copy over old data into new slots */ \
15630770e4dSSatish Balay         for (ii=0; ii<row+1; ii++) {new_i[ii] = bi[ii];} \
157273d9f13SBarry Smith         for (ii=row+1; ii<bm+1; ii++) {new_i[ii] = bi[ii]+CHUNKSIZE;} \
158549d3d68SSatish Balay         ierr = PetscMemcpy(new_j,bj,(bi[row]+nrow+shift)*sizeof(int));CHKERRQ(ierr); \
15930770e4dSSatish Balay         len = (new_nz - CHUNKSIZE - bi[row] - nrow - shift); \
160549d3d68SSatish Balay         ierr = PetscMemcpy(new_j+bi[row]+shift+nrow+CHUNKSIZE,bj+bi[row]+shift+nrow, \
161549d3d68SSatish Balay                                                            len*sizeof(int));CHKERRQ(ierr); \
16287828ca2SBarry Smith         ierr = PetscMemcpy(new_a,ba,(bi[row]+nrow+shift)*sizeof(PetscScalar));CHKERRQ(ierr); \
163549d3d68SSatish Balay         ierr = PetscMemcpy(new_a+bi[row]+shift+nrow+CHUNKSIZE,ba+bi[row]+shift+nrow, \
16487828ca2SBarry Smith                                                            len*sizeof(PetscScalar));CHKERRQ(ierr);  \
16530770e4dSSatish Balay         /* free up old matrix storage */ \
16630770e4dSSatish Balay  \
167606d414cSSatish Balay         ierr = PetscFree(b->a);CHKERRQ(ierr);  \
168606d414cSSatish Balay         if (!b->singlemalloc) { \
169606d414cSSatish Balay           ierr = PetscFree(b->i);CHKERRQ(ierr); \
170606d414cSSatish Balay           ierr = PetscFree(b->j);CHKERRQ(ierr); \
171606d414cSSatish Balay         } \
17274c639caSSatish Balay         ba = b->a = new_a; bi = b->i = new_i; bj = b->j = new_j;  \
1737c922b88SBarry Smith         b->singlemalloc = PETSC_TRUE; \
17430770e4dSSatish Balay  \
17530770e4dSSatish Balay         rp   = bj + bi[row] + shift; ap = ba + bi[row] + shift; \
17630770e4dSSatish Balay         rmax = bimax[row] = bimax[row] + CHUNKSIZE; \
17787828ca2SBarry Smith         PetscLogObjectMemory(B,CHUNKSIZE*(sizeof(int) + sizeof(PetscScalar))); \
17874c639caSSatish Balay         b->maxnz += CHUNKSIZE; \
17974c639caSSatish Balay         b->reallocs++; \
18030770e4dSSatish Balay       } \
18174c639caSSatish Balay       N = nrow++ - 1; b->nz++; \
18230770e4dSSatish Balay       /* shift up all the later entries in this row */ \
18330770e4dSSatish Balay       for (ii=N; ii>=_i; ii--) { \
18430770e4dSSatish Balay         rp[ii+1] = rp[ii]; \
18530770e4dSSatish Balay         ap[ii+1] = ap[ii]; \
18630770e4dSSatish Balay       } \
18730770e4dSSatish Balay       rp[_i] = col1;  \
18830770e4dSSatish Balay       ap[_i] = value;  \
18930770e4dSSatish Balay       b_noinsert: ; \
19030770e4dSSatish Balay       bilen[row] = nrow; \
19130770e4dSSatish Balay }
19230770e4dSSatish Balay 
1934a2ae208SSatish Balay #undef __FUNCT__
1944a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_MPIAIJ"
19587828ca2SBarry Smith int MatSetValues_MPIAIJ(Mat mat,int m,int *im,int n,int *in,PetscScalar *v,InsertMode addv)
1968a729477SBarry Smith {
19744a69424SLois Curfman McInnes   Mat_MPIAIJ   *aij = (Mat_MPIAIJ*)mat->data;
19887828ca2SBarry Smith   PetscScalar  value;
1991eb62cbbSBarry Smith   int          ierr,i,j,rstart = aij->rstart,rend = aij->rend;
2001eb62cbbSBarry Smith   int          cstart = aij->cstart,cend = aij->cend,row,col;
201273d9f13SBarry Smith   PetscTruth   roworiented = aij->roworiented;
2028a729477SBarry Smith 
2030520107fSSatish Balay   /* Some Variables required in the macro */
2044ee7247eSSatish Balay   Mat          A = aij->A;
2054ee7247eSSatish Balay   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
20630770e4dSSatish Balay   int          *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j;
20787828ca2SBarry Smith   PetscScalar  *aa = a->a;
208329f5518SBarry Smith   PetscTruth   ignorezeroentries = (((a->ignorezeroentries)&&(addv==ADD_VALUES))?PETSC_TRUE:PETSC_FALSE);
20930770e4dSSatish Balay   Mat          B = aij->B;
21030770e4dSSatish Balay   Mat_SeqAIJ   *b = (Mat_SeqAIJ*)B->data;
211273d9f13SBarry Smith   int          *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->m,am = aij->A->m;
21287828ca2SBarry Smith   PetscScalar  *ba = b->a;
21330770e4dSSatish Balay 
214ba4e3ef2SSatish Balay   int          *rp,ii,nrow,_i,rmax,N,col1,low,high,t;
21530770e4dSSatish Balay   int          nonew = a->nonew,shift = a->indexshift;
21687828ca2SBarry Smith   PetscScalar  *ap;
2174ee7247eSSatish Balay 
2183a40ed3dSBarry Smith   PetscFunctionBegin;
2198a729477SBarry Smith   for (i=0; i<m; i++) {
2205ef9f2a5SBarry Smith     if (im[i] < 0) continue;
221aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g)
222273d9f13SBarry Smith     if (im[i] >= mat->M) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
2230a198c4cSBarry Smith #endif
2244b0e389bSBarry Smith     if (im[i] >= rstart && im[i] < rend) {
2254b0e389bSBarry Smith       row = im[i] - rstart;
2261eb62cbbSBarry Smith       for (j=0; j<n; j++) {
2274b0e389bSBarry Smith         if (in[j] >= cstart && in[j] < cend){
2284b0e389bSBarry Smith           col = in[j] - cstart;
2294b0e389bSBarry Smith           if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
230329f5518SBarry Smith           if (ignorezeroentries && value == 0.0) continue;
23130770e4dSSatish Balay           MatSetValues_SeqAIJ_A_Private(row,col,value,addv);
2320520107fSSatish Balay           /* ierr = MatSetValues_SeqAIJ(aij->A,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */
233273d9f13SBarry Smith         } else if (in[j] < 0) continue;
234aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g)
235273d9f13SBarry Smith         else if (in[j] >= mat->N) {SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Column too large");}
2360a198c4cSBarry Smith #endif
2371eb62cbbSBarry Smith         else {
238227d817aSBarry Smith           if (mat->was_assembled) {
239905e6a2fSBarry Smith             if (!aij->colmap) {
240905e6a2fSBarry Smith               ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
241905e6a2fSBarry Smith             }
242aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
2430f5bd95cSBarry Smith             ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr);
244fa46199cSSatish Balay 	    col--;
245b1fc9764SSatish Balay #else
246905e6a2fSBarry Smith             col = aij->colmap[in[j]] - 1;
247b1fc9764SSatish Balay #endif
248ec8511deSBarry Smith             if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
2492493cbb0SBarry Smith               ierr = DisAssemble_MPIAIJ(mat);CHKERRQ(ierr);
2504b0e389bSBarry Smith               col =  in[j];
2519bf004c3SSatish Balay               /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
252f9508a3cSSatish Balay               B = aij->B;
253f9508a3cSSatish Balay               b = (Mat_SeqAIJ*)B->data;
254f9508a3cSSatish Balay               bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j;
255f9508a3cSSatish Balay               ba = b->a;
256d6dfbf8fSBarry Smith             }
257c48de900SBarry Smith           } else col = in[j];
2584b0e389bSBarry Smith           if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
259329f5518SBarry Smith           if (ignorezeroentries && value == 0.0) continue;
26030770e4dSSatish Balay           MatSetValues_SeqAIJ_B_Private(row,col,value,addv);
26130770e4dSSatish Balay           /* ierr = MatSetValues_SeqAIJ(aij->B,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */
2621eb62cbbSBarry Smith         }
2631eb62cbbSBarry Smith       }
2645ef9f2a5SBarry Smith     } else {
26590f02eecSBarry Smith       if (!aij->donotstash) {
266d36fbae8SSatish Balay         if (roworiented) {
267329f5518SBarry Smith           if (ignorezeroentries && v[i*n] == 0.0) continue;
2688798bf22SSatish Balay           ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n);CHKERRQ(ierr);
269d36fbae8SSatish Balay         } else {
270329f5518SBarry Smith           if (ignorezeroentries && v[i] == 0.0) continue;
2718798bf22SSatish Balay           ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m);CHKERRQ(ierr);
2724b0e389bSBarry Smith         }
2731eb62cbbSBarry Smith       }
2748a729477SBarry Smith     }
27590f02eecSBarry Smith   }
2763a40ed3dSBarry Smith   PetscFunctionReturn(0);
2778a729477SBarry Smith }
2788a729477SBarry Smith 
2794a2ae208SSatish Balay #undef __FUNCT__
2804a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_MPIAIJ"
28187828ca2SBarry Smith int MatGetValues_MPIAIJ(Mat mat,int m,int *idxm,int n,int *idxn,PetscScalar *v)
282b49de8d1SLois Curfman McInnes {
283b49de8d1SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
284b49de8d1SLois Curfman McInnes   int        ierr,i,j,rstart = aij->rstart,rend = aij->rend;
285b49de8d1SLois Curfman McInnes   int        cstart = aij->cstart,cend = aij->cend,row,col;
286b49de8d1SLois Curfman McInnes 
2873a40ed3dSBarry Smith   PetscFunctionBegin;
288b49de8d1SLois Curfman McInnes   for (i=0; i<m; i++) {
28929bbc08cSBarry Smith     if (idxm[i] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Negative row");
290273d9f13SBarry Smith     if (idxm[i] >= mat->M) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
291b49de8d1SLois Curfman McInnes     if (idxm[i] >= rstart && idxm[i] < rend) {
292b49de8d1SLois Curfman McInnes       row = idxm[i] - rstart;
293b49de8d1SLois Curfman McInnes       for (j=0; j<n; j++) {
29429bbc08cSBarry Smith         if (idxn[j] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Negative column");
295273d9f13SBarry Smith         if (idxn[j] >= mat->N) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
296b49de8d1SLois Curfman McInnes         if (idxn[j] >= cstart && idxn[j] < cend){
297b49de8d1SLois Curfman McInnes           col = idxn[j] - cstart;
298b49de8d1SLois Curfman McInnes           ierr = MatGetValues(aij->A,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
299fa852ad4SSatish Balay         } else {
300905e6a2fSBarry Smith           if (!aij->colmap) {
301905e6a2fSBarry Smith             ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
302905e6a2fSBarry Smith           }
303aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
3040f5bd95cSBarry Smith           ierr = PetscTableFind(aij->colmap,idxn[j]+1,&col);CHKERRQ(ierr);
305fa46199cSSatish Balay           col --;
306b1fc9764SSatish Balay #else
307905e6a2fSBarry Smith           col = aij->colmap[idxn[j]] - 1;
308b1fc9764SSatish Balay #endif
309e60e1c95SSatish Balay           if ((col < 0) || (aij->garray[col] != idxn[j])) *(v+i*n+j) = 0.0;
310d9d09a02SSatish Balay           else {
311b49de8d1SLois Curfman McInnes             ierr = MatGetValues(aij->B,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
312b49de8d1SLois Curfman McInnes           }
313b49de8d1SLois Curfman McInnes         }
314b49de8d1SLois Curfman McInnes       }
315a8c6a408SBarry Smith     } else {
31629bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"Only local values currently supported");
317b49de8d1SLois Curfman McInnes     }
318b49de8d1SLois Curfman McInnes   }
3193a40ed3dSBarry Smith   PetscFunctionReturn(0);
320b49de8d1SLois Curfman McInnes }
321bc5ccf88SSatish Balay 
3224a2ae208SSatish Balay #undef __FUNCT__
3234a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyBegin_MPIAIJ"
324bc5ccf88SSatish Balay int MatAssemblyBegin_MPIAIJ(Mat mat,MatAssemblyType mode)
325bc5ccf88SSatish Balay {
326bc5ccf88SSatish Balay   Mat_MPIAIJ  *aij = (Mat_MPIAIJ*)mat->data;
327d36fbae8SSatish Balay   int         ierr,nstash,reallocs;
328bc5ccf88SSatish Balay   InsertMode  addv;
329bc5ccf88SSatish Balay 
330bc5ccf88SSatish Balay   PetscFunctionBegin;
331bc5ccf88SSatish Balay   if (aij->donotstash) {
332bc5ccf88SSatish Balay     PetscFunctionReturn(0);
333bc5ccf88SSatish Balay   }
334bc5ccf88SSatish Balay 
335bc5ccf88SSatish Balay   /* make sure all processors are either in INSERTMODE or ADDMODE */
336bc5ccf88SSatish Balay   ierr = MPI_Allreduce(&mat->insertmode,&addv,1,MPI_INT,MPI_BOR,mat->comm);CHKERRQ(ierr);
337bc5ccf88SSatish Balay   if (addv == (ADD_VALUES|INSERT_VALUES)) {
33829bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Some processors inserted others added");
339bc5ccf88SSatish Balay   }
340bc5ccf88SSatish Balay   mat->insertmode = addv; /* in case this processor had no cache */
341bc5ccf88SSatish Balay 
3428798bf22SSatish Balay   ierr = MatStashScatterBegin_Private(&mat->stash,aij->rowners);CHKERRQ(ierr);
3438798bf22SSatish Balay   ierr = MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);CHKERRQ(ierr);
344b0a32e0cSBarry Smith   PetscLogInfo(aij->A,"MatAssemblyBegin_MPIAIJ:Stash has %d entries, uses %d mallocs.\n",nstash,reallocs);
345bc5ccf88SSatish Balay   PetscFunctionReturn(0);
346bc5ccf88SSatish Balay }
347bc5ccf88SSatish Balay 
348bc5ccf88SSatish Balay 
3494a2ae208SSatish Balay #undef __FUNCT__
3504a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_MPIAIJ"
351bc5ccf88SSatish Balay int MatAssemblyEnd_MPIAIJ(Mat mat,MatAssemblyType mode)
352bc5ccf88SSatish Balay {
353bc5ccf88SSatish Balay   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
354a2d1c673SSatish Balay   int         i,j,rstart,ncols,n,ierr,flg;
355bc5ccf88SSatish Balay   int         *row,*col,other_disassembled;
35687828ca2SBarry Smith   PetscScalar *val;
357bc5ccf88SSatish Balay   InsertMode  addv = mat->insertmode;
3589e070d67SMatthew Knepley #if defined(PETSC_HAVE_SUPERLUDIST)
3599b9367d5SHong Zhang   PetscTruth  flag;
3609e070d67SMatthew Knepley #endif
361bc5ccf88SSatish Balay 
362bc5ccf88SSatish Balay   PetscFunctionBegin;
363bc5ccf88SSatish Balay   if (!aij->donotstash) {
364a2d1c673SSatish Balay     while (1) {
3658798bf22SSatish Balay       ierr = MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);CHKERRQ(ierr);
366a2d1c673SSatish Balay       if (!flg) break;
367a2d1c673SSatish Balay 
368bc5ccf88SSatish Balay       for (i=0; i<n;) {
369bc5ccf88SSatish Balay         /* Now identify the consecutive vals belonging to the same row */
370bc5ccf88SSatish Balay         for (j=i,rstart=row[j]; j<n; j++) { if (row[j] != rstart) break; }
371bc5ccf88SSatish Balay         if (j < n) ncols = j-i;
372bc5ccf88SSatish Balay         else       ncols = n-i;
373bc5ccf88SSatish Balay         /* Now assemble all these values with a single function call */
374bc5ccf88SSatish Balay         ierr = MatSetValues_MPIAIJ(mat,1,row+i,ncols,col+i,val+i,addv);CHKERRQ(ierr);
375bc5ccf88SSatish Balay         i = j;
376bc5ccf88SSatish Balay       }
377bc5ccf88SSatish Balay     }
3788798bf22SSatish Balay     ierr = MatStashScatterEnd_Private(&mat->stash);CHKERRQ(ierr);
379bc5ccf88SSatish Balay   }
380bc5ccf88SSatish Balay 
381bc5ccf88SSatish Balay   ierr = MatAssemblyBegin(aij->A,mode);CHKERRQ(ierr);
382bc5ccf88SSatish Balay   ierr = MatAssemblyEnd(aij->A,mode);CHKERRQ(ierr);
383bc5ccf88SSatish Balay 
384bc5ccf88SSatish Balay   /* determine if any processor has disassembled, if so we must
385bc5ccf88SSatish Balay      also disassemble ourselfs, in order that we may reassemble. */
386bc5ccf88SSatish Balay   /*
387bc5ccf88SSatish Balay      if nonzero structure of submatrix B cannot change then we know that
388bc5ccf88SSatish Balay      no processor disassembled thus we can skip this stuff
389bc5ccf88SSatish Balay   */
390bc5ccf88SSatish Balay   if (!((Mat_SeqAIJ*)aij->B->data)->nonew)  {
391bc5ccf88SSatish Balay     ierr = MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPI_INT,MPI_PROD,mat->comm);CHKERRQ(ierr);
392bc5ccf88SSatish Balay     if (mat->was_assembled && !other_disassembled) {
393bc5ccf88SSatish Balay       ierr = DisAssemble_MPIAIJ(mat);CHKERRQ(ierr);
394bc5ccf88SSatish Balay     }
395bc5ccf88SSatish Balay   }
396bc5ccf88SSatish Balay 
397bc5ccf88SSatish Balay   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
398bc5ccf88SSatish Balay     ierr = MatSetUpMultiply_MPIAIJ(mat);CHKERRQ(ierr);
399bc5ccf88SSatish Balay   }
400bc5ccf88SSatish Balay   ierr = MatAssemblyBegin(aij->B,mode);CHKERRQ(ierr);
401bc5ccf88SSatish Balay   ierr = MatAssemblyEnd(aij->B,mode);CHKERRQ(ierr);
402bc5ccf88SSatish Balay 
403606d414cSSatish Balay   if (aij->rowvalues) {
404606d414cSSatish Balay     ierr = PetscFree(aij->rowvalues);CHKERRQ(ierr);
405606d414cSSatish Balay     aij->rowvalues = 0;
406606d414cSSatish Balay   }
4079b9367d5SHong Zhang #if defined(PETSC_HAVE_SUPERLUDIST)
4089b9367d5SHong Zhang   ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_superlu_dist",&flag);CHKERRQ(ierr);
4099b9367d5SHong Zhang   if (flag) { ierr = MatUseSuperLU_DIST_MPIAIJ(mat);CHKERRQ(ierr); }
4109b9367d5SHong Zhang #endif
411bc5ccf88SSatish Balay   PetscFunctionReturn(0);
412bc5ccf88SSatish Balay }
413bc5ccf88SSatish Balay 
4144a2ae208SSatish Balay #undef __FUNCT__
4154a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_MPIAIJ"
4168f6be9afSLois Curfman McInnes int MatZeroEntries_MPIAIJ(Mat A)
4171eb62cbbSBarry Smith {
41844a69424SLois Curfman McInnes   Mat_MPIAIJ *l = (Mat_MPIAIJ*)A->data;
419dbd7a890SLois Curfman McInnes   int        ierr;
4203a40ed3dSBarry Smith 
4213a40ed3dSBarry Smith   PetscFunctionBegin;
42278b31e54SBarry Smith   ierr = MatZeroEntries(l->A);CHKERRQ(ierr);
42378b31e54SBarry Smith   ierr = MatZeroEntries(l->B);CHKERRQ(ierr);
4243a40ed3dSBarry Smith   PetscFunctionReturn(0);
4251eb62cbbSBarry Smith }
4261eb62cbbSBarry Smith 
4274a2ae208SSatish Balay #undef __FUNCT__
4284a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_MPIAIJ"
42987828ca2SBarry Smith int MatZeroRows_MPIAIJ(Mat A,IS is,PetscScalar *diag)
4301eb62cbbSBarry Smith {
43144a69424SLois Curfman McInnes   Mat_MPIAIJ     *l = (Mat_MPIAIJ*)A->data;
43217699dbbSLois Curfman McInnes   int            i,ierr,N,*rows,*owners = l->rowners,size = l->size;
43335d8aa7fSBarry Smith   int            *procs,*nprocs,j,idx,nsends,*work,row;
43417699dbbSLois Curfman McInnes   int            nmax,*svalues,*starts,*owner,nrecvs,rank = l->rank;
4355392566eSBarry Smith   int            *rvalues,tag = A->tag,count,base,slen,n,*source;
4366a5c57faSSatish Balay   int            *lens,imdex,*lrows,*values,rstart=l->rstart;
437d6dfbf8fSBarry Smith   MPI_Comm       comm = A->comm;
4381eb62cbbSBarry Smith   MPI_Request    *send_waits,*recv_waits;
4391eb62cbbSBarry Smith   MPI_Status     recv_status,*send_status;
4401eb62cbbSBarry Smith   IS             istmp;
44135d8aa7fSBarry Smith   PetscTruth     found;
4421eb62cbbSBarry Smith 
4433a40ed3dSBarry Smith   PetscFunctionBegin;
444e03a110bSBarry Smith   ierr = ISGetLocalSize(is,&N);CHKERRQ(ierr);
44578b31e54SBarry Smith   ierr = ISGetIndices(is,&rows);CHKERRQ(ierr);
4461eb62cbbSBarry Smith 
4471eb62cbbSBarry Smith   /*  first count number of contributors to each processor */
448b0a32e0cSBarry Smith   ierr = PetscMalloc(2*size*sizeof(int),&nprocs);CHKERRQ(ierr);
449549d3d68SSatish Balay   ierr   = PetscMemzero(nprocs,2*size*sizeof(int));CHKERRQ(ierr);
450549d3d68SSatish Balay   procs  = nprocs + size;
451b0a32e0cSBarry Smith   ierr = PetscMalloc((N+1)*sizeof(int),&owner);CHKERRQ(ierr); /* see note*/
4521eb62cbbSBarry Smith   for (i=0; i<N; i++) {
4531eb62cbbSBarry Smith     idx = rows[i];
45435d8aa7fSBarry Smith     found = PETSC_FALSE;
45517699dbbSLois Curfman McInnes     for (j=0; j<size; j++) {
4561eb62cbbSBarry Smith       if (idx >= owners[j] && idx < owners[j+1]) {
45735d8aa7fSBarry Smith         nprocs[j]++; procs[j] = 1; owner[i] = j; found = PETSC_TRUE; break;
4581eb62cbbSBarry Smith       }
4591eb62cbbSBarry Smith     }
46029bbc08cSBarry Smith     if (!found) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Index out of range");
4611eb62cbbSBarry Smith   }
46217699dbbSLois Curfman McInnes   nsends = 0;  for (i=0; i<size; i++) { nsends += procs[i];}
4631eb62cbbSBarry Smith 
4641eb62cbbSBarry Smith   /* inform other processors of number of messages and max length*/
465b0a32e0cSBarry Smith   ierr = PetscMalloc(2*size*sizeof(int),&work);CHKERRQ(ierr);
4666831982aSBarry Smith   ierr   = MPI_Allreduce(nprocs,work,2*size,MPI_INT,PetscMaxSum_Op,comm);CHKERRQ(ierr);
4676831982aSBarry Smith   nrecvs = work[size+rank];
46817699dbbSLois Curfman McInnes   nmax   = work[rank];
469606d414cSSatish Balay   ierr   = PetscFree(work);CHKERRQ(ierr);
4701eb62cbbSBarry Smith 
4711eb62cbbSBarry Smith   /* post receives:   */
472b0a32e0cSBarry Smith   ierr = PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(int),&rvalues);CHKERRQ(ierr);
473b0a32e0cSBarry Smith   ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr);
4741eb62cbbSBarry Smith   for (i=0; i<nrecvs; i++) {
475ca161407SBarry Smith     ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPI_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr);
4761eb62cbbSBarry Smith   }
4771eb62cbbSBarry Smith 
4781eb62cbbSBarry Smith   /* do sends:
4791eb62cbbSBarry Smith       1) starts[i] gives the starting index in svalues for stuff going to
4801eb62cbbSBarry Smith          the ith processor
4811eb62cbbSBarry Smith   */
482b0a32e0cSBarry Smith   ierr = PetscMalloc((N+1)*sizeof(int),&svalues);CHKERRQ(ierr);
483b0a32e0cSBarry Smith   ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr);
484b0a32e0cSBarry Smith   ierr = PetscMalloc((size+1)*sizeof(int),&starts);CHKERRQ(ierr);
4851eb62cbbSBarry Smith   starts[0] = 0;
48617699dbbSLois Curfman McInnes   for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[i-1];}
4871eb62cbbSBarry Smith   for (i=0; i<N; i++) {
4881eb62cbbSBarry Smith     svalues[starts[owner[i]]++] = rows[i];
4891eb62cbbSBarry Smith   }
4906831982aSBarry Smith   ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr);
4911eb62cbbSBarry Smith 
4921eb62cbbSBarry Smith   starts[0] = 0;
49317699dbbSLois Curfman McInnes   for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[i-1];}
4941eb62cbbSBarry Smith   count = 0;
49517699dbbSLois Curfman McInnes   for (i=0; i<size; i++) {
4961eb62cbbSBarry Smith     if (procs[i]) {
497ca161407SBarry Smith       ierr = MPI_Isend(svalues+starts[i],nprocs[i],MPI_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr);
4981eb62cbbSBarry Smith     }
4991eb62cbbSBarry Smith   }
500606d414cSSatish Balay   ierr = PetscFree(starts);CHKERRQ(ierr);
5011eb62cbbSBarry Smith 
50217699dbbSLois Curfman McInnes   base = owners[rank];
5031eb62cbbSBarry Smith 
5041eb62cbbSBarry Smith   /*  wait on receives */
505b0a32e0cSBarry Smith   ierr   = PetscMalloc(2*(nrecvs+1)*sizeof(int),&lens);CHKERRQ(ierr);
5061eb62cbbSBarry Smith   source = lens + nrecvs;
5071eb62cbbSBarry Smith   count  = nrecvs; slen = 0;
5081eb62cbbSBarry Smith   while (count) {
509ca161407SBarry Smith     ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
5101eb62cbbSBarry Smith     /* unpack receives into our local space */
511ca161407SBarry Smith     ierr = MPI_Get_count(&recv_status,MPI_INT,&n);CHKERRQ(ierr);
512d6dfbf8fSBarry Smith     source[imdex]  = recv_status.MPI_SOURCE;
513d6dfbf8fSBarry Smith     lens[imdex]    = n;
5141eb62cbbSBarry Smith     slen          += n;
5151eb62cbbSBarry Smith     count--;
5161eb62cbbSBarry Smith   }
517606d414cSSatish Balay   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
5181eb62cbbSBarry Smith 
5191eb62cbbSBarry Smith   /* move the data into the send scatter */
520b0a32e0cSBarry Smith   ierr = PetscMalloc((slen+1)*sizeof(int),&lrows);CHKERRQ(ierr);
5211eb62cbbSBarry Smith   count = 0;
5221eb62cbbSBarry Smith   for (i=0; i<nrecvs; i++) {
5231eb62cbbSBarry Smith     values = rvalues + i*nmax;
5241eb62cbbSBarry Smith     for (j=0; j<lens[i]; j++) {
5251eb62cbbSBarry Smith       lrows[count++] = values[j] - base;
5261eb62cbbSBarry Smith     }
5271eb62cbbSBarry Smith   }
528606d414cSSatish Balay   ierr = PetscFree(rvalues);CHKERRQ(ierr);
529606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
530606d414cSSatish Balay   ierr = PetscFree(owner);CHKERRQ(ierr);
531606d414cSSatish Balay   ierr = PetscFree(nprocs);CHKERRQ(ierr);
5321eb62cbbSBarry Smith 
5331eb62cbbSBarry Smith   /* actually zap the local rows */
534029af93fSBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,slen,lrows,&istmp);CHKERRQ(ierr);
535b0a32e0cSBarry Smith   PetscLogObjectParent(A,istmp);
5366a5c57faSSatish Balay 
5376eb55b6aSBarry Smith   /*
5386eb55b6aSBarry Smith         Zero the required rows. If the "diagonal block" of the matrix
5396eb55b6aSBarry Smith      is square and the user wishes to set the diagonal we use seperate
5406eb55b6aSBarry Smith      code so that MatSetValues() is not called for each diagonal allocating
5416eb55b6aSBarry Smith      new memory, thus calling lots of mallocs and slowing things down.
5426eb55b6aSBarry Smith 
5436eb55b6aSBarry Smith        Contributed by: Mathew Knepley
5446eb55b6aSBarry Smith   */
545e2d53e46SBarry Smith   /* must zero l->B before l->A because the (diag) case below may put values into l->B*/
546e2d53e46SBarry Smith   ierr = MatZeroRows(l->B,istmp,0);CHKERRQ(ierr);
5476eb55b6aSBarry Smith   if (diag && (l->A->M == l->A->N)) {
5486eb55b6aSBarry Smith     ierr      = MatZeroRows(l->A,istmp,diag);CHKERRQ(ierr);
549e2d53e46SBarry Smith   } else if (diag) {
550e2d53e46SBarry Smith     ierr = MatZeroRows(l->A,istmp,0);CHKERRQ(ierr);
551fa46199cSSatish Balay     if (((Mat_SeqAIJ*)l->A->data)->nonew) {
55229bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"MatZeroRows() on rectangular matrices cannot be used with the Mat options\n\
553fa46199cSSatish Balay MAT_NO_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR");
5546525c446SSatish Balay     }
555e2d53e46SBarry Smith     for (i = 0; i < slen; i++) {
556e2d53e46SBarry Smith       row  = lrows[i] + rstart;
557e2d53e46SBarry Smith       ierr = MatSetValues(A,1,&row,1,&row,diag,INSERT_VALUES);CHKERRQ(ierr);
558e2d53e46SBarry Smith     }
559e2d53e46SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
560e2d53e46SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
5616eb55b6aSBarry Smith   } else {
5626a5c57faSSatish Balay     ierr = MatZeroRows(l->A,istmp,0);CHKERRQ(ierr);
5636eb55b6aSBarry Smith   }
56478b31e54SBarry Smith   ierr = ISDestroy(istmp);CHKERRQ(ierr);
565606d414cSSatish Balay   ierr = PetscFree(lrows);CHKERRQ(ierr);
56672dacd9aSBarry Smith 
5671eb62cbbSBarry Smith   /* wait on sends */
5681eb62cbbSBarry Smith   if (nsends) {
569b0a32e0cSBarry Smith     ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr);
570ca161407SBarry Smith     ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr);
571606d414cSSatish Balay     ierr = PetscFree(send_status);CHKERRQ(ierr);
5721eb62cbbSBarry Smith   }
573606d414cSSatish Balay   ierr = PetscFree(send_waits);CHKERRQ(ierr);
574606d414cSSatish Balay   ierr = PetscFree(svalues);CHKERRQ(ierr);
5751eb62cbbSBarry Smith 
5763a40ed3dSBarry Smith   PetscFunctionReturn(0);
5771eb62cbbSBarry Smith }
5781eb62cbbSBarry Smith 
5794a2ae208SSatish Balay #undef __FUNCT__
5804a2ae208SSatish Balay #define __FUNCT__ "MatMult_MPIAIJ"
5818f6be9afSLois Curfman McInnes int MatMult_MPIAIJ(Mat A,Vec xx,Vec yy)
5821eb62cbbSBarry Smith {
583416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
584fbd6ef76SBarry Smith   int        ierr,nt;
585416022c9SBarry Smith 
5863a40ed3dSBarry Smith   PetscFunctionBegin;
587a2ce50c7SBarry Smith   ierr = VecGetLocalSize(xx,&nt);CHKERRQ(ierr);
588273d9f13SBarry Smith   if (nt != A->n) {
589273d9f13SBarry Smith     SETERRQ2(PETSC_ERR_ARG_SIZ,"Incompatible partition of A (%d) and xx (%d)",A->n,nt);
590fbd6ef76SBarry Smith   }
59143a90d84SBarry Smith   ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
592f830108cSBarry Smith   ierr = (*a->A->ops->mult)(a->A,xx,yy);CHKERRQ(ierr);
59343a90d84SBarry Smith   ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
594f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);CHKERRQ(ierr);
5953a40ed3dSBarry Smith   PetscFunctionReturn(0);
5961eb62cbbSBarry Smith }
5971eb62cbbSBarry Smith 
5984a2ae208SSatish Balay #undef __FUNCT__
5994a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_MPIAIJ"
6008f6be9afSLois Curfman McInnes int MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
601da3a660dSBarry Smith {
602416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
603da3a660dSBarry Smith   int        ierr;
6043a40ed3dSBarry Smith 
6053a40ed3dSBarry Smith   PetscFunctionBegin;
60643a90d84SBarry Smith   ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
607f830108cSBarry Smith   ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
60843a90d84SBarry Smith   ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
609f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);CHKERRQ(ierr);
6103a40ed3dSBarry Smith   PetscFunctionReturn(0);
611da3a660dSBarry Smith }
612da3a660dSBarry Smith 
6134a2ae208SSatish Balay #undef __FUNCT__
6144a2ae208SSatish Balay #define __FUNCT__ "MatMultTranspose_MPIAIJ"
6157c922b88SBarry Smith int MatMultTranspose_MPIAIJ(Mat A,Vec xx,Vec yy)
616da3a660dSBarry Smith {
617416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
618da3a660dSBarry Smith   int        ierr;
619da3a660dSBarry Smith 
6203a40ed3dSBarry Smith   PetscFunctionBegin;
621da3a660dSBarry Smith   /* do nondiagonal part */
6227c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
623da3a660dSBarry Smith   /* send it on its way */
624537820f0SBarry Smith   ierr = VecScatterBegin(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
625da3a660dSBarry Smith   /* do local part */
6267c922b88SBarry Smith   ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr);
627da3a660dSBarry Smith   /* receive remote parts: note this assumes the values are not actually */
628da3a660dSBarry Smith   /* inserted in yy until the next line, which is true for my implementation*/
629da3a660dSBarry Smith   /* but is not perhaps always true. */
630537820f0SBarry Smith   ierr = VecScatterEnd(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
6313a40ed3dSBarry Smith   PetscFunctionReturn(0);
632da3a660dSBarry Smith }
633da3a660dSBarry Smith 
6344a2ae208SSatish Balay #undef __FUNCT__
6354a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_MPIAIJ"
6367c922b88SBarry Smith int MatMultTransposeAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
637da3a660dSBarry Smith {
638416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
639da3a660dSBarry Smith   int        ierr;
640da3a660dSBarry Smith 
6413a40ed3dSBarry Smith   PetscFunctionBegin;
642da3a660dSBarry Smith   /* do nondiagonal part */
6437c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
644da3a660dSBarry Smith   /* send it on its way */
645537820f0SBarry Smith   ierr = VecScatterBegin(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
646da3a660dSBarry Smith   /* do local part */
6477c922b88SBarry Smith   ierr = (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
648da3a660dSBarry Smith   /* receive remote parts: note this assumes the values are not actually */
649da3a660dSBarry Smith   /* inserted in yy until the next line, which is true for my implementation*/
650da3a660dSBarry Smith   /* but is not perhaps always true. */
6510a198c4cSBarry Smith   ierr = VecScatterEnd(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
6523a40ed3dSBarry Smith   PetscFunctionReturn(0);
653da3a660dSBarry Smith }
654da3a660dSBarry Smith 
6551eb62cbbSBarry Smith /*
6561eb62cbbSBarry Smith   This only works correctly for square matrices where the subblock A->A is the
6571eb62cbbSBarry Smith    diagonal block
6581eb62cbbSBarry Smith */
6594a2ae208SSatish Balay #undef __FUNCT__
6604a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_MPIAIJ"
6618f6be9afSLois Curfman McInnes int MatGetDiagonal_MPIAIJ(Mat A,Vec v)
6621eb62cbbSBarry Smith {
6633a40ed3dSBarry Smith   int        ierr;
664416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
6653a40ed3dSBarry Smith 
6663a40ed3dSBarry Smith   PetscFunctionBegin;
667273d9f13SBarry Smith   if (A->M != A->N) SETERRQ(PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block");
6685baf8537SBarry Smith   if (a->rstart != a->cstart || a->rend != a->cend) {
66929bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_SIZ,"row partition must equal col partition");
6703a40ed3dSBarry Smith   }
6713a40ed3dSBarry Smith   ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr);
6723a40ed3dSBarry Smith   PetscFunctionReturn(0);
6731eb62cbbSBarry Smith }
6741eb62cbbSBarry Smith 
6754a2ae208SSatish Balay #undef __FUNCT__
6764a2ae208SSatish Balay #define __FUNCT__ "MatScale_MPIAIJ"
67787828ca2SBarry Smith int MatScale_MPIAIJ(PetscScalar *aa,Mat A)
678052efed2SBarry Smith {
679052efed2SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
680052efed2SBarry Smith   int        ierr;
6813a40ed3dSBarry Smith 
6823a40ed3dSBarry Smith   PetscFunctionBegin;
683052efed2SBarry Smith   ierr = MatScale(aa,a->A);CHKERRQ(ierr);
684052efed2SBarry Smith   ierr = MatScale(aa,a->B);CHKERRQ(ierr);
6853a40ed3dSBarry Smith   PetscFunctionReturn(0);
686052efed2SBarry Smith }
687052efed2SBarry Smith 
6884a2ae208SSatish Balay #undef __FUNCT__
6894a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_MPIAIJ"
690e1311b90SBarry Smith int MatDestroy_MPIAIJ(Mat mat)
6911eb62cbbSBarry Smith {
69244a69424SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
6931eb62cbbSBarry Smith   int        ierr;
69483e2fdc7SBarry Smith 
6953a40ed3dSBarry Smith   PetscFunctionBegin;
696aa482453SBarry Smith #if defined(PETSC_USE_LOG)
697b0a32e0cSBarry Smith   PetscLogObjectState((PetscObject)mat,"Rows=%d, Cols=%d",mat->M,mat->N);
698a5a9c739SBarry Smith #endif
6998798bf22SSatish Balay   ierr = MatStashDestroy_Private(&mat->stash);CHKERRQ(ierr);
700606d414cSSatish Balay   ierr = PetscFree(aij->rowners);CHKERRQ(ierr);
70178b31e54SBarry Smith   ierr = MatDestroy(aij->A);CHKERRQ(ierr);
70278b31e54SBarry Smith   ierr = MatDestroy(aij->B);CHKERRQ(ierr);
703aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
7040f5bd95cSBarry Smith   if (aij->colmap) {ierr = PetscTableDelete(aij->colmap);CHKERRQ(ierr);}
705b1fc9764SSatish Balay #else
706606d414cSSatish Balay   if (aij->colmap) {ierr = PetscFree(aij->colmap);CHKERRQ(ierr);}
707b1fc9764SSatish Balay #endif
708606d414cSSatish Balay   if (aij->garray) {ierr = PetscFree(aij->garray);CHKERRQ(ierr);}
7097c922b88SBarry Smith   if (aij->lvec)   {ierr = VecDestroy(aij->lvec);CHKERRQ(ierr);}
7107c922b88SBarry Smith   if (aij->Mvctx)  {ierr = VecScatterDestroy(aij->Mvctx);CHKERRQ(ierr);}
711606d414cSSatish Balay   if (aij->rowvalues) {ierr = PetscFree(aij->rowvalues);CHKERRQ(ierr);}
712606d414cSSatish Balay   ierr = PetscFree(aij);CHKERRQ(ierr);
7133a40ed3dSBarry Smith   PetscFunctionReturn(0);
7141eb62cbbSBarry Smith }
715ee50ffe9SBarry Smith 
7164a2ae208SSatish Balay #undef __FUNCT__
7174a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ_ASCIIorDraworSocket"
718b0a32e0cSBarry Smith int MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
719416022c9SBarry Smith {
72044a69424SLois Curfman McInnes   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*)mat->data;
721dbb450caSBarry Smith   Mat_SeqAIJ*       C = (Mat_SeqAIJ*)aij->A->data;
722fb9695e5SSatish Balay   int               ierr,shift = C->indexshift,rank = aij->rank,size = aij->size;
723f1af5d2fSBarry Smith   PetscTruth        isdraw,isascii,flg;
724b0a32e0cSBarry Smith   PetscViewer       sviewer;
725f3ef73ceSBarry Smith   PetscViewerFormat format;
726416022c9SBarry Smith 
7273a40ed3dSBarry Smith   PetscFunctionBegin;
728fb9695e5SSatish Balay   ierr  = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
729b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
7300f5bd95cSBarry Smith   if (isascii) {
731b0a32e0cSBarry Smith     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
732fb9695e5SSatish Balay     if (format == PETSC_VIEWER_ASCII_INFO_LONG) {
7334e220ebcSLois Curfman McInnes       MatInfo info;
7341dab6e02SBarry Smith       ierr = MPI_Comm_rank(mat->comm,&rank);CHKERRQ(ierr);
735888f2ed8SSatish Balay       ierr = MatGetInfo(mat,MAT_LOCAL,&info);CHKERRQ(ierr);
736b0a32e0cSBarry Smith       ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_no_inode",&flg);CHKERRQ(ierr);
7376831982aSBarry Smith       if (flg) {
738b0a32e0cSBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %d nz %d nz alloced %d mem %d, not using I-node routines\n",
739273d9f13SBarry Smith 					      rank,mat->m,(int)info.nz_used,(int)info.nz_allocated,(int)info.memory);CHKERRQ(ierr);
7406831982aSBarry Smith       } else {
741b0a32e0cSBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %d nz %d nz alloced %d mem %d, using I-node routines\n",
742273d9f13SBarry Smith 		    rank,mat->m,(int)info.nz_used,(int)info.nz_allocated,(int)info.memory);CHKERRQ(ierr);
7436831982aSBarry Smith       }
744888f2ed8SSatish Balay       ierr = MatGetInfo(aij->A,MAT_LOCAL,&info);CHKERRQ(ierr);
745b0a32e0cSBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %d \n",rank,(int)info.nz_used);CHKERRQ(ierr);
746888f2ed8SSatish Balay       ierr = MatGetInfo(aij->B,MAT_LOCAL,&info);CHKERRQ(ierr);
747b0a32e0cSBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %d \n",rank,(int)info.nz_used);CHKERRQ(ierr);
748b0a32e0cSBarry Smith       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
749a40aa06bSLois Curfman McInnes       ierr = VecScatterView(aij->Mvctx,viewer);CHKERRQ(ierr);
7503a40ed3dSBarry Smith       PetscFunctionReturn(0);
751fb9695e5SSatish Balay     } else if (format == PETSC_VIEWER_ASCII_INFO) {
7523a40ed3dSBarry Smith       PetscFunctionReturn(0);
75308480c60SBarry Smith     }
7540f5bd95cSBarry Smith   } else if (isdraw) {
755b0a32e0cSBarry Smith     PetscDraw       draw;
75619bcc07fSBarry Smith     PetscTruth isnull;
757b0a32e0cSBarry Smith     ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
758b0a32e0cSBarry Smith     ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
75919bcc07fSBarry Smith   }
76019bcc07fSBarry Smith 
76117699dbbSLois Curfman McInnes   if (size == 1) {
762e36acaf3SBarry Smith     ierr = PetscObjectSetName((PetscObject)aij->A,mat->name);CHKERRQ(ierr);
76378b31e54SBarry Smith     ierr = MatView(aij->A,viewer);CHKERRQ(ierr);
7643a40ed3dSBarry Smith   } else {
76595373324SBarry Smith     /* assemble the entire matrix onto first processor. */
76695373324SBarry Smith     Mat         A;
767ec8511deSBarry Smith     Mat_SeqAIJ *Aloc;
768273d9f13SBarry Smith     int         M = mat->M,N = mat->N,m,*ai,*aj,row,*cols,i,*ct;
76987828ca2SBarry Smith     PetscScalar *a;
7702ee70a88SLois Curfman McInnes 
77117699dbbSLois Curfman McInnes     if (!rank) {
77255843e3eSBarry Smith       ierr = MatCreateMPIAIJ(mat->comm,M,N,M,N,0,PETSC_NULL,0,PETSC_NULL,&A);CHKERRQ(ierr);
7733a40ed3dSBarry Smith     } else {
77455843e3eSBarry Smith       ierr = MatCreateMPIAIJ(mat->comm,0,0,M,N,0,PETSC_NULL,0,PETSC_NULL,&A);CHKERRQ(ierr);
77595373324SBarry Smith     }
776b0a32e0cSBarry Smith     PetscLogObjectParent(mat,A);
777416022c9SBarry Smith 
77895373324SBarry Smith     /* copy over the A part */
779ec8511deSBarry Smith     Aloc = (Mat_SeqAIJ*)aij->A->data;
780273d9f13SBarry Smith     m = aij->A->m; ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
78195373324SBarry Smith     row = aij->rstart;
782dbb450caSBarry Smith     for (i=0; i<ai[m]+shift; i++) {aj[i] += aij->cstart + shift;}
78395373324SBarry Smith     for (i=0; i<m; i++) {
784416022c9SBarry Smith       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);CHKERRQ(ierr);
78595373324SBarry Smith       row++; a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
78695373324SBarry Smith     }
7872ee70a88SLois Curfman McInnes     aj = Aloc->j;
788dbb450caSBarry Smith     for (i=0; i<ai[m]+shift; i++) {aj[i] -= aij->cstart + shift;}
78995373324SBarry Smith 
79095373324SBarry Smith     /* copy over the B part */
791ec8511deSBarry Smith     Aloc = (Mat_SeqAIJ*)aij->B->data;
792273d9f13SBarry Smith     m    = aij->B->m;  ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
79395373324SBarry Smith     row  = aij->rstart;
794b0a32e0cSBarry Smith     ierr = PetscMalloc((ai[m]+1)*sizeof(int),&cols);CHKERRQ(ierr);
795b0a32e0cSBarry Smith     ct   = cols;
796dbb450caSBarry Smith     for (i=0; i<ai[m]+shift; i++) {cols[i] = aij->garray[aj[i]+shift];}
79795373324SBarry Smith     for (i=0; i<m; i++) {
798416022c9SBarry Smith       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);CHKERRQ(ierr);
79995373324SBarry Smith       row++; a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
80095373324SBarry Smith     }
801606d414cSSatish Balay     ierr = PetscFree(ct);CHKERRQ(ierr);
8026d4a8577SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
8036d4a8577SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
80455843e3eSBarry Smith     /*
80555843e3eSBarry Smith        Everyone has to call to draw the matrix since the graphics waits are
806b0a32e0cSBarry Smith        synchronized across all processors that share the PetscDraw object
80755843e3eSBarry Smith     */
808b0a32e0cSBarry Smith     ierr = PetscViewerGetSingleton(viewer,&sviewer);CHKERRQ(ierr);
809e03a110bSBarry Smith     if (!rank) {
810e36acaf3SBarry Smith       ierr = PetscObjectSetName((PetscObject)((Mat_MPIAIJ*)(A->data))->A,mat->name);CHKERRQ(ierr);
8116831982aSBarry Smith       ierr = MatView(((Mat_MPIAIJ*)(A->data))->A,sviewer);CHKERRQ(ierr);
81295373324SBarry Smith     }
813b0a32e0cSBarry Smith     ierr = PetscViewerRestoreSingleton(viewer,&sviewer);CHKERRQ(ierr);
81478b31e54SBarry Smith     ierr = MatDestroy(A);CHKERRQ(ierr);
81595373324SBarry Smith   }
8163a40ed3dSBarry Smith   PetscFunctionReturn(0);
8171eb62cbbSBarry Smith }
8181eb62cbbSBarry Smith 
8194a2ae208SSatish Balay #undef __FUNCT__
8204a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ"
821b0a32e0cSBarry Smith int MatView_MPIAIJ(Mat mat,PetscViewer viewer)
822416022c9SBarry Smith {
823416022c9SBarry Smith   int        ierr;
8246831982aSBarry Smith   PetscTruth isascii,isdraw,issocket,isbinary;
825416022c9SBarry Smith 
8263a40ed3dSBarry Smith   PetscFunctionBegin;
827b0a32e0cSBarry Smith   ierr  = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
828fb9695e5SSatish Balay   ierr  = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
829fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
830b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_SOCKET,&issocket);CHKERRQ(ierr);
8310f5bd95cSBarry Smith   if (isascii || isdraw || isbinary || issocket) {
8327b2a1423SBarry Smith     ierr = MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer);CHKERRQ(ierr);
8335cd90555SBarry Smith   } else {
83429bbc08cSBarry Smith     SETERRQ1(1,"Viewer type %s not supported by MPIAIJ matrices",((PetscObject)viewer)->type_name);
835416022c9SBarry Smith   }
8363a40ed3dSBarry Smith   PetscFunctionReturn(0);
837416022c9SBarry Smith }
838416022c9SBarry Smith 
8391eb62cbbSBarry Smith 
840c14dc6b6SHong Zhang 
8414a2ae208SSatish Balay #undef __FUNCT__
8424a2ae208SSatish Balay #define __FUNCT__ "MatRelax_MPIAIJ"
843c14dc6b6SHong Zhang int MatRelax_MPIAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,int its,int lits,Vec xx)
8448a729477SBarry Smith {
84544a69424SLois Curfman McInnes   Mat_MPIAIJ   *mat = (Mat_MPIAIJ*)matin->data;
846c14dc6b6SHong Zhang   int          ierr;
847c14dc6b6SHong Zhang   Vec          bb1;
848a6c309e7SHong Zhang   PetscScalar  mone=-1.0;
8498a729477SBarry Smith 
8503a40ed3dSBarry Smith   PetscFunctionBegin;
85191723122SBarry Smith   if (its <= 0 || lits <= 0) SETERRQ2(PETSC_ERR_ARG_WRONG,"Relaxation requires global its %d and local its %d both positive",its,lits);
852c14dc6b6SHong Zhang 
853c14dc6b6SHong Zhang   ierr = VecDuplicate(bb,&bb1);CHKERRQ(ierr);
8542798e883SHong Zhang 
855c16cb8f2SBarry Smith   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){
856da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
857*bd3bf7d3SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,lits,xx);CHKERRQ(ierr);
8582798e883SHong Zhang       its--;
859da3a660dSBarry Smith     }
8602798e883SHong Zhang 
8612798e883SHong Zhang     while (its--) {
8623a40ed3dSBarry Smith       ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
8633a40ed3dSBarry Smith       ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
8642798e883SHong Zhang 
865c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
866c14dc6b6SHong Zhang       ierr = VecScale(&mone,mat->lvec);CHKERRQ(ierr);
867c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
8682798e883SHong Zhang 
869c14dc6b6SHong Zhang       /* local sweep */
870*bd3bf7d3SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,lits,xx);
871c14dc6b6SHong Zhang       CHKERRQ(ierr);
8722798e883SHong Zhang     }
8733a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_FORWARD_SWEEP){
874da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
875c14dc6b6SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,PETSC_NULL,xx);CHKERRQ(ierr);
8762798e883SHong Zhang       its--;
877da3a660dSBarry Smith     }
8782798e883SHong Zhang     while (its--) {
8793a40ed3dSBarry Smith       ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
8803a40ed3dSBarry Smith       ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
8812798e883SHong Zhang 
882c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
883c14dc6b6SHong Zhang       ierr = VecScale(&mone,mat->lvec);CHKERRQ(ierr);
884c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
885c14dc6b6SHong Zhang 
886c14dc6b6SHong Zhang       /* local sweep */
887a6c309e7SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,PETSC_NULL,xx);
888c14dc6b6SHong Zhang       CHKERRQ(ierr);
8892798e883SHong Zhang     }
8903a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_BACKWARD_SWEEP){
891da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
892c14dc6b6SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,PETSC_NULL,xx);CHKERRQ(ierr);
8932798e883SHong Zhang       its--;
894da3a660dSBarry Smith     }
8952798e883SHong Zhang     while (its--) {
8962e8a6d31SBarry Smith       ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
8972e8a6d31SBarry Smith       ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
8982798e883SHong Zhang 
899c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
900c14dc6b6SHong Zhang       ierr = VecScale(&mone,mat->lvec);CHKERRQ(ierr);
901c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
9022798e883SHong Zhang 
903c14dc6b6SHong Zhang       /* local sweep */
904a6c309e7SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,PETSC_NULL,xx);
905c14dc6b6SHong Zhang       CHKERRQ(ierr);
9062798e883SHong Zhang     }
9073a40ed3dSBarry Smith   } else {
90829bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"Parallel SOR not supported");
909c16cb8f2SBarry Smith   }
910c14dc6b6SHong Zhang 
911c14dc6b6SHong Zhang   ierr = VecDestroy(bb1);CHKERRQ(ierr);
9123a40ed3dSBarry Smith   PetscFunctionReturn(0);
9138a729477SBarry Smith }
914a66be287SLois Curfman McInnes 
9154a2ae208SSatish Balay #undef __FUNCT__
9164a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_MPIAIJ"
9178f6be9afSLois Curfman McInnes int MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info)
918a66be287SLois Curfman McInnes {
919a66be287SLois Curfman McInnes   Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data;
920a66be287SLois Curfman McInnes   Mat        A = mat->A,B = mat->B;
9214e220ebcSLois Curfman McInnes   int        ierr;
922329f5518SBarry Smith   PetscReal  isend[5],irecv[5];
923a66be287SLois Curfman McInnes 
9243a40ed3dSBarry Smith   PetscFunctionBegin;
9254e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
9264e220ebcSLois Curfman McInnes   ierr = MatGetInfo(A,MAT_LOCAL,info);CHKERRQ(ierr);
9274e220ebcSLois Curfman McInnes   isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
9284e220ebcSLois Curfman McInnes   isend[3] = info->memory;  isend[4] = info->mallocs;
9294e220ebcSLois Curfman McInnes   ierr = MatGetInfo(B,MAT_LOCAL,info);CHKERRQ(ierr);
9304e220ebcSLois Curfman McInnes   isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
9314e220ebcSLois Curfman McInnes   isend[3] += info->memory;  isend[4] += info->mallocs;
932a66be287SLois Curfman McInnes   if (flag == MAT_LOCAL) {
9334e220ebcSLois Curfman McInnes     info->nz_used      = isend[0];
9344e220ebcSLois Curfman McInnes     info->nz_allocated = isend[1];
9354e220ebcSLois Curfman McInnes     info->nz_unneeded  = isend[2];
9364e220ebcSLois Curfman McInnes     info->memory       = isend[3];
9374e220ebcSLois Curfman McInnes     info->mallocs      = isend[4];
938a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_MAX) {
939d7d1e502SBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_MAX,matin->comm);CHKERRQ(ierr);
9404e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
9414e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
9424e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
9434e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
9444e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
945a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_SUM) {
946d7d1e502SBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_SUM,matin->comm);CHKERRQ(ierr);
9474e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
9484e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
9494e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
9504e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
9514e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
952a66be287SLois Curfman McInnes   }
9534e220ebcSLois Curfman McInnes   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
9544e220ebcSLois Curfman McInnes   info->fill_ratio_needed = 0;
9554e220ebcSLois Curfman McInnes   info->factor_mallocs    = 0;
956273d9f13SBarry Smith   info->rows_global       = (double)matin->M;
957273d9f13SBarry Smith   info->columns_global    = (double)matin->N;
958273d9f13SBarry Smith   info->rows_local        = (double)matin->m;
959273d9f13SBarry Smith   info->columns_local     = (double)matin->N;
9604e220ebcSLois Curfman McInnes 
9613a40ed3dSBarry Smith   PetscFunctionReturn(0);
962a66be287SLois Curfman McInnes }
963a66be287SLois Curfman McInnes 
9644a2ae208SSatish Balay #undef __FUNCT__
9654a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_MPIAIJ"
9668f6be9afSLois Curfman McInnes int MatSetOption_MPIAIJ(Mat A,MatOption op)
967c74985f6SBarry Smith {
968c0bbcb79SLois Curfman McInnes   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
9691dee9f54SBarry Smith   int        ierr;
970c74985f6SBarry Smith 
9713a40ed3dSBarry Smith   PetscFunctionBegin;
97212c028f9SKris Buschelman   switch (op) {
97312c028f9SKris Buschelman   case MAT_NO_NEW_NONZERO_LOCATIONS:
97412c028f9SKris Buschelman   case MAT_YES_NEW_NONZERO_LOCATIONS:
97512c028f9SKris Buschelman   case MAT_COLUMNS_UNSORTED:
97612c028f9SKris Buschelman   case MAT_COLUMNS_SORTED:
97712c028f9SKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
97812c028f9SKris Buschelman   case MAT_KEEP_ZEROED_ROWS:
97912c028f9SKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
98012c028f9SKris Buschelman   case MAT_USE_INODES:
98112c028f9SKris Buschelman   case MAT_DO_NOT_USE_INODES:
98212c028f9SKris Buschelman   case MAT_IGNORE_ZERO_ENTRIES:
9831dee9f54SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
9841dee9f54SBarry Smith     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
98512c028f9SKris Buschelman     break;
98612c028f9SKris Buschelman   case MAT_ROW_ORIENTED:
9877c922b88SBarry Smith     a->roworiented = PETSC_TRUE;
9881dee9f54SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
9891dee9f54SBarry Smith     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
99012c028f9SKris Buschelman     break;
99112c028f9SKris Buschelman   case MAT_ROWS_SORTED:
99212c028f9SKris Buschelman   case MAT_ROWS_UNSORTED:
99312c028f9SKris Buschelman   case MAT_YES_NEW_DIAGONALS:
994d03495bdSKris Buschelman   case MAT_USE_SINGLE_PRECISION_SOLVES:
995b0a32e0cSBarry Smith     PetscLogInfo(A,"MatSetOption_MPIAIJ:Option ignored\n");
99612c028f9SKris Buschelman     break;
99712c028f9SKris Buschelman   case MAT_COLUMN_ORIENTED:
9987c922b88SBarry Smith     a->roworiented = PETSC_FALSE;
9991dee9f54SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
10001dee9f54SBarry Smith     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
100112c028f9SKris Buschelman     break;
100212c028f9SKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
10037c922b88SBarry Smith     a->donotstash = PETSC_TRUE;
100412c028f9SKris Buschelman     break;
100512c028f9SKris Buschelman   case MAT_NO_NEW_DIAGONALS:
100629bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"MAT_NO_NEW_DIAGONALS");
100712c028f9SKris Buschelman   default:
100829bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"unknown option");
10093a40ed3dSBarry Smith   }
10103a40ed3dSBarry Smith   PetscFunctionReturn(0);
1011c74985f6SBarry Smith }
1012c74985f6SBarry Smith 
10134a2ae208SSatish Balay #undef __FUNCT__
10144a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_MPIAIJ"
101587828ca2SBarry Smith int MatGetRow_MPIAIJ(Mat matin,int row,int *nz,int **idx,PetscScalar **v)
101639e00950SLois Curfman McInnes {
1017154123eaSLois Curfman McInnes   Mat_MPIAIJ   *mat = (Mat_MPIAIJ*)matin->data;
101887828ca2SBarry Smith   PetscScalar  *vworkA,*vworkB,**pvA,**pvB,*v_p;
1019154123eaSLois Curfman McInnes   int          i,ierr,*cworkA,*cworkB,**pcA,**pcB,cstart = mat->cstart;
1020154123eaSLois Curfman McInnes   int          nztot,nzA,nzB,lrow,rstart = mat->rstart,rend = mat->rend;
102170f0671dSBarry Smith   int          *cmap,*idx_p;
102239e00950SLois Curfman McInnes 
10233a40ed3dSBarry Smith   PetscFunctionBegin;
102429bbc08cSBarry Smith   if (mat->getrowactive == PETSC_TRUE) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Already active");
10257a0afa10SBarry Smith   mat->getrowactive = PETSC_TRUE;
10267a0afa10SBarry Smith 
102770f0671dSBarry Smith   if (!mat->rowvalues && (idx || v)) {
10287a0afa10SBarry Smith     /*
10297a0afa10SBarry Smith         allocate enough space to hold information from the longest row.
10307a0afa10SBarry Smith     */
10317a0afa10SBarry Smith     Mat_SeqAIJ *Aa = (Mat_SeqAIJ*)mat->A->data,*Ba = (Mat_SeqAIJ*)mat->B->data;
1032273d9f13SBarry Smith     int     max = 1,tmp;
1033273d9f13SBarry Smith     for (i=0; i<matin->m; i++) {
10347a0afa10SBarry Smith       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
10357a0afa10SBarry Smith       if (max < tmp) { max = tmp; }
10367a0afa10SBarry Smith     }
103787828ca2SBarry Smith     ierr = PetscMalloc(max*(sizeof(int)+sizeof(PetscScalar)),&mat->rowvalues);CHKERRQ(ierr);
10387a0afa10SBarry Smith     mat->rowindices = (int*)(mat->rowvalues + max);
10397a0afa10SBarry Smith   }
10407a0afa10SBarry Smith 
104129bbc08cSBarry Smith   if (row < rstart || row >= rend) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Only local rows")
1042abc0e9e4SLois Curfman McInnes   lrow = row - rstart;
104339e00950SLois Curfman McInnes 
1044154123eaSLois Curfman McInnes   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1045154123eaSLois Curfman McInnes   if (!v)   {pvA = 0; pvB = 0;}
1046154123eaSLois Curfman McInnes   if (!idx) {pcA = 0; if (!v) pcB = 0;}
1047f830108cSBarry Smith   ierr = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1048f830108cSBarry Smith   ierr = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
1049154123eaSLois Curfman McInnes   nztot = nzA + nzB;
1050154123eaSLois Curfman McInnes 
105170f0671dSBarry Smith   cmap  = mat->garray;
1052154123eaSLois Curfman McInnes   if (v  || idx) {
1053154123eaSLois Curfman McInnes     if (nztot) {
1054154123eaSLois Curfman McInnes       /* Sort by increasing column numbers, assuming A and B already sorted */
105570f0671dSBarry Smith       int imark = -1;
1056154123eaSLois Curfman McInnes       if (v) {
105770f0671dSBarry Smith         *v = v_p = mat->rowvalues;
105839e00950SLois Curfman McInnes         for (i=0; i<nzB; i++) {
105970f0671dSBarry Smith           if (cmap[cworkB[i]] < cstart)   v_p[i] = vworkB[i];
1060154123eaSLois Curfman McInnes           else break;
1061154123eaSLois Curfman McInnes         }
1062154123eaSLois Curfman McInnes         imark = i;
106370f0671dSBarry Smith         for (i=0; i<nzA; i++)     v_p[imark+i] = vworkA[i];
106470f0671dSBarry Smith         for (i=imark; i<nzB; i++) v_p[nzA+i]   = vworkB[i];
1065154123eaSLois Curfman McInnes       }
1066154123eaSLois Curfman McInnes       if (idx) {
106770f0671dSBarry Smith         *idx = idx_p = mat->rowindices;
106870f0671dSBarry Smith         if (imark > -1) {
106970f0671dSBarry Smith           for (i=0; i<imark; i++) {
107070f0671dSBarry Smith             idx_p[i] = cmap[cworkB[i]];
107170f0671dSBarry Smith           }
107270f0671dSBarry Smith         } else {
1073154123eaSLois Curfman McInnes           for (i=0; i<nzB; i++) {
107470f0671dSBarry Smith             if (cmap[cworkB[i]] < cstart)   idx_p[i] = cmap[cworkB[i]];
1075154123eaSLois Curfman McInnes             else break;
1076154123eaSLois Curfman McInnes           }
1077154123eaSLois Curfman McInnes           imark = i;
107870f0671dSBarry Smith         }
107970f0671dSBarry Smith         for (i=0; i<nzA; i++)     idx_p[imark+i] = cstart + cworkA[i];
108070f0671dSBarry Smith         for (i=imark; i<nzB; i++) idx_p[nzA+i]   = cmap[cworkB[i]];
108139e00950SLois Curfman McInnes       }
10823f97c4b0SBarry Smith     } else {
10831ca473b0SSatish Balay       if (idx) *idx = 0;
10841ca473b0SSatish Balay       if (v)   *v   = 0;
10851ca473b0SSatish Balay     }
1086154123eaSLois Curfman McInnes   }
108739e00950SLois Curfman McInnes   *nz = nztot;
1088f830108cSBarry Smith   ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1089f830108cSBarry Smith   ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
10903a40ed3dSBarry Smith   PetscFunctionReturn(0);
109139e00950SLois Curfman McInnes }
109239e00950SLois Curfman McInnes 
10934a2ae208SSatish Balay #undef __FUNCT__
10944a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_MPIAIJ"
109587828ca2SBarry Smith int MatRestoreRow_MPIAIJ(Mat mat,int row,int *nz,int **idx,PetscScalar **v)
109639e00950SLois Curfman McInnes {
10977a0afa10SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
10983a40ed3dSBarry Smith 
10993a40ed3dSBarry Smith   PetscFunctionBegin;
11007a0afa10SBarry Smith   if (aij->getrowactive == PETSC_FALSE) {
110129bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"MatGetRow not called");
11027a0afa10SBarry Smith   }
11037a0afa10SBarry Smith   aij->getrowactive = PETSC_FALSE;
11043a40ed3dSBarry Smith   PetscFunctionReturn(0);
110539e00950SLois Curfman McInnes }
110639e00950SLois Curfman McInnes 
11074a2ae208SSatish Balay #undef __FUNCT__
11084a2ae208SSatish Balay #define __FUNCT__ "MatNorm_MPIAIJ"
1109329f5518SBarry Smith int MatNorm_MPIAIJ(Mat mat,NormType type,PetscReal *norm)
1110855ac2c5SLois Curfman McInnes {
1111855ac2c5SLois Curfman McInnes   Mat_MPIAIJ   *aij = (Mat_MPIAIJ*)mat->data;
1112ec8511deSBarry Smith   Mat_SeqAIJ   *amat = (Mat_SeqAIJ*)aij->A->data,*bmat = (Mat_SeqAIJ*)aij->B->data;
1113416022c9SBarry Smith   int          ierr,i,j,cstart = aij->cstart,shift = amat->indexshift;
1114329f5518SBarry Smith   PetscReal    sum = 0.0;
111587828ca2SBarry Smith   PetscScalar  *v;
111604ca555eSLois Curfman McInnes 
11173a40ed3dSBarry Smith   PetscFunctionBegin;
111817699dbbSLois Curfman McInnes   if (aij->size == 1) {
111914183eadSLois Curfman McInnes     ierr =  MatNorm(aij->A,type,norm);CHKERRQ(ierr);
112037fa93a5SLois Curfman McInnes   } else {
112104ca555eSLois Curfman McInnes     if (type == NORM_FROBENIUS) {
112204ca555eSLois Curfman McInnes       v = amat->a;
112304ca555eSLois Curfman McInnes       for (i=0; i<amat->nz; i++) {
1124aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
1125329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
112604ca555eSLois Curfman McInnes #else
112704ca555eSLois Curfman McInnes         sum += (*v)*(*v); v++;
112804ca555eSLois Curfman McInnes #endif
112904ca555eSLois Curfman McInnes       }
113004ca555eSLois Curfman McInnes       v = bmat->a;
113104ca555eSLois Curfman McInnes       for (i=0; i<bmat->nz; i++) {
1132aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
1133329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
113404ca555eSLois Curfman McInnes #else
113504ca555eSLois Curfman McInnes         sum += (*v)*(*v); v++;
113604ca555eSLois Curfman McInnes #endif
113704ca555eSLois Curfman McInnes       }
1138d7d1e502SBarry Smith       ierr = MPI_Allreduce(&sum,norm,1,MPIU_REAL,MPI_SUM,mat->comm);CHKERRQ(ierr);
113904ca555eSLois Curfman McInnes       *norm = sqrt(*norm);
11403a40ed3dSBarry Smith     } else if (type == NORM_1) { /* max column norm */
1141329f5518SBarry Smith       PetscReal *tmp,*tmp2;
114204ca555eSLois Curfman McInnes       int    *jj,*garray = aij->garray;
1143b0a32e0cSBarry Smith       ierr = PetscMalloc((mat->N+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1144b0a32e0cSBarry Smith       ierr = PetscMalloc((mat->N+1)*sizeof(PetscReal),&tmp2);CHKERRQ(ierr);
1145273d9f13SBarry Smith       ierr = PetscMemzero(tmp,mat->N*sizeof(PetscReal));CHKERRQ(ierr);
114604ca555eSLois Curfman McInnes       *norm = 0.0;
114704ca555eSLois Curfman McInnes       v = amat->a; jj = amat->j;
114804ca555eSLois Curfman McInnes       for (j=0; j<amat->nz; j++) {
1149579c6b6fSBarry Smith         tmp[cstart + *jj++ + shift] += PetscAbsScalar(*v);  v++;
115004ca555eSLois Curfman McInnes       }
115104ca555eSLois Curfman McInnes       v = bmat->a; jj = bmat->j;
115204ca555eSLois Curfman McInnes       for (j=0; j<bmat->nz; j++) {
1153579c6b6fSBarry Smith         tmp[garray[*jj++ + shift]] += PetscAbsScalar(*v); v++;
115404ca555eSLois Curfman McInnes       }
1155d7d1e502SBarry Smith       ierr = MPI_Allreduce(tmp,tmp2,mat->N,MPIU_REAL,MPI_SUM,mat->comm);CHKERRQ(ierr);
1156273d9f13SBarry Smith       for (j=0; j<mat->N; j++) {
115704ca555eSLois Curfman McInnes         if (tmp2[j] > *norm) *norm = tmp2[j];
115804ca555eSLois Curfman McInnes       }
1159606d414cSSatish Balay       ierr = PetscFree(tmp);CHKERRQ(ierr);
1160606d414cSSatish Balay       ierr = PetscFree(tmp2);CHKERRQ(ierr);
11613a40ed3dSBarry Smith     } else if (type == NORM_INFINITY) { /* max row norm */
1162329f5518SBarry Smith       PetscReal ntemp = 0.0;
1163273d9f13SBarry Smith       for (j=0; j<aij->A->m; j++) {
1164dbb450caSBarry Smith         v = amat->a + amat->i[j] + shift;
116504ca555eSLois Curfman McInnes         sum = 0.0;
116604ca555eSLois Curfman McInnes         for (i=0; i<amat->i[j+1]-amat->i[j]; i++) {
1167cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
116804ca555eSLois Curfman McInnes         }
1169dbb450caSBarry Smith         v = bmat->a + bmat->i[j] + shift;
117004ca555eSLois Curfman McInnes         for (i=0; i<bmat->i[j+1]-bmat->i[j]; i++) {
1171cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
117204ca555eSLois Curfman McInnes         }
1173515d9167SLois Curfman McInnes         if (sum > ntemp) ntemp = sum;
117404ca555eSLois Curfman McInnes       }
1175d7d1e502SBarry Smith       ierr = MPI_Allreduce(&ntemp,norm,1,MPIU_REAL,MPI_MAX,mat->comm);CHKERRQ(ierr);
1176ca161407SBarry Smith     } else {
117729bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"No support for two norm");
117804ca555eSLois Curfman McInnes     }
117937fa93a5SLois Curfman McInnes   }
11803a40ed3dSBarry Smith   PetscFunctionReturn(0);
1181855ac2c5SLois Curfman McInnes }
1182855ac2c5SLois Curfman McInnes 
11834a2ae208SSatish Balay #undef __FUNCT__
11844a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_MPIAIJ"
11858f6be9afSLois Curfman McInnes int MatTranspose_MPIAIJ(Mat A,Mat *matout)
1186b7c46309SBarry Smith {
1187b7c46309SBarry Smith   Mat_MPIAIJ   *a = (Mat_MPIAIJ*)A->data;
1188dbb450caSBarry Smith   Mat_SeqAIJ   *Aloc = (Mat_SeqAIJ*)a->A->data;
1189416022c9SBarry Smith   int          ierr,shift = Aloc->indexshift;
1190273d9f13SBarry Smith   int          M = A->M,N = A->N,m,*ai,*aj,row,*cols,i,*ct;
11913a40ed3dSBarry Smith   Mat          B;
119287828ca2SBarry Smith   PetscScalar  *array;
1193b7c46309SBarry Smith 
11943a40ed3dSBarry Smith   PetscFunctionBegin;
11957c922b88SBarry Smith   if (!matout && M != N) {
119629bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1197d4bb536fSBarry Smith   }
1198d4bb536fSBarry Smith 
1199273d9f13SBarry Smith   ierr = MatCreateMPIAIJ(A->comm,A->n,A->m,N,M,0,PETSC_NULL,0,PETSC_NULL,&B);CHKERRQ(ierr);
1200b7c46309SBarry Smith 
1201b7c46309SBarry Smith   /* copy over the A part */
1202ec8511deSBarry Smith   Aloc = (Mat_SeqAIJ*)a->A->data;
1203273d9f13SBarry Smith   m = a->A->m; ai = Aloc->i; aj = Aloc->j; array = Aloc->a;
1204b7c46309SBarry Smith   row = a->rstart;
1205dbb450caSBarry Smith   for (i=0; i<ai[m]+shift; i++) {aj[i] += a->cstart + shift;}
1206b7c46309SBarry Smith   for (i=0; i<m; i++) {
1207416022c9SBarry Smith     ierr = MatSetValues(B,ai[i+1]-ai[i],aj,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
1208b7c46309SBarry Smith     row++; array += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
1209b7c46309SBarry Smith   }
1210b7c46309SBarry Smith   aj = Aloc->j;
12114af08d9eSBarry Smith   for (i=0; i<ai[m]+shift; i++) {aj[i] -= a->cstart + shift;}
1212b7c46309SBarry Smith 
1213b7c46309SBarry Smith   /* copy over the B part */
1214ec8511deSBarry Smith   Aloc = (Mat_SeqAIJ*)a->B->data;
1215273d9f13SBarry Smith   m = a->B->m;  ai = Aloc->i; aj = Aloc->j; array = Aloc->a;
1216b7c46309SBarry Smith   row  = a->rstart;
1217b0a32e0cSBarry Smith   ierr = PetscMalloc((1+ai[m]-shift)*sizeof(int),&cols);CHKERRQ(ierr);
1218b0a32e0cSBarry Smith   ct   = cols;
1219dbb450caSBarry Smith   for (i=0; i<ai[m]+shift; i++) {cols[i] = a->garray[aj[i]+shift];}
1220b7c46309SBarry Smith   for (i=0; i<m; i++) {
1221416022c9SBarry Smith     ierr = MatSetValues(B,ai[i+1]-ai[i],cols,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
1222b7c46309SBarry Smith     row++; array += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
1223b7c46309SBarry Smith   }
1224606d414cSSatish Balay   ierr = PetscFree(ct);CHKERRQ(ierr);
12256d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
12266d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
12277c922b88SBarry Smith   if (matout) {
12280de55854SLois Curfman McInnes     *matout = B;
12290de55854SLois Curfman McInnes   } else {
1230273d9f13SBarry Smith     ierr = MatHeaderCopy(A,B);CHKERRQ(ierr);
12310de55854SLois Curfman McInnes   }
12323a40ed3dSBarry Smith   PetscFunctionReturn(0);
1233b7c46309SBarry Smith }
1234b7c46309SBarry Smith 
12354a2ae208SSatish Balay #undef __FUNCT__
12364a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_MPIAIJ"
12374b967eb1SSatish Balay int MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr)
1238a008b906SSatish Balay {
12394b967eb1SSatish Balay   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
12404b967eb1SSatish Balay   Mat        a = aij->A,b = aij->B;
1241a008b906SSatish Balay   int        ierr,s1,s2,s3;
1242a008b906SSatish Balay 
12433a40ed3dSBarry Smith   PetscFunctionBegin;
12444b967eb1SSatish Balay   ierr = MatGetLocalSize(mat,&s2,&s3);CHKERRQ(ierr);
12454b967eb1SSatish Balay   if (rr) {
1246e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr);
124729bbc08cSBarry Smith     if (s1!=s3) SETERRQ(PETSC_ERR_ARG_SIZ,"right vector non-conforming local size");
12484b967eb1SSatish Balay     /* Overlap communication with computation. */
124943a90d84SBarry Smith     ierr = VecScatterBegin(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx);CHKERRQ(ierr);
1250a008b906SSatish Balay   }
12514b967eb1SSatish Balay   if (ll) {
1252e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr);
125329bbc08cSBarry Smith     if (s1!=s2) SETERRQ(PETSC_ERR_ARG_SIZ,"left vector non-conforming local size");
1254f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,ll,0);CHKERRQ(ierr);
12554b967eb1SSatish Balay   }
12564b967eb1SSatish Balay   /* scale  the diagonal block */
1257f830108cSBarry Smith   ierr = (*a->ops->diagonalscale)(a,ll,rr);CHKERRQ(ierr);
12584b967eb1SSatish Balay 
12594b967eb1SSatish Balay   if (rr) {
12604b967eb1SSatish Balay     /* Do a scatter end and then right scale the off-diagonal block */
126143a90d84SBarry Smith     ierr = VecScatterEnd(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx);CHKERRQ(ierr);
1262f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,0,aij->lvec);CHKERRQ(ierr);
12634b967eb1SSatish Balay   }
12644b967eb1SSatish Balay 
12653a40ed3dSBarry Smith   PetscFunctionReturn(0);
1266a008b906SSatish Balay }
1267a008b906SSatish Balay 
1268a008b906SSatish Balay 
12694a2ae208SSatish Balay #undef __FUNCT__
12704a2ae208SSatish Balay #define __FUNCT__ "MatPrintHelp_MPIAIJ"
12718f6be9afSLois Curfman McInnes int MatPrintHelp_MPIAIJ(Mat A)
1272682d7d0cSBarry Smith {
1273682d7d0cSBarry Smith   Mat_MPIAIJ *a   = (Mat_MPIAIJ*)A->data;
12743a40ed3dSBarry Smith   int        ierr;
1275682d7d0cSBarry Smith 
12763a40ed3dSBarry Smith   PetscFunctionBegin;
12773a40ed3dSBarry Smith   if (!a->rank) {
12783a40ed3dSBarry Smith     ierr = MatPrintHelp_SeqAIJ(a->A);CHKERRQ(ierr);
12793a40ed3dSBarry Smith   }
12803a40ed3dSBarry Smith   PetscFunctionReturn(0);
1281682d7d0cSBarry Smith }
1282682d7d0cSBarry Smith 
12834a2ae208SSatish Balay #undef __FUNCT__
12844a2ae208SSatish Balay #define __FUNCT__ "MatGetBlockSize_MPIAIJ"
12858f6be9afSLois Curfman McInnes int MatGetBlockSize_MPIAIJ(Mat A,int *bs)
12865a838052SSatish Balay {
12873a40ed3dSBarry Smith   PetscFunctionBegin;
12885a838052SSatish Balay   *bs = 1;
12893a40ed3dSBarry Smith   PetscFunctionReturn(0);
12905a838052SSatish Balay }
12914a2ae208SSatish Balay #undef __FUNCT__
12924a2ae208SSatish Balay #define __FUNCT__ "MatSetUnfactored_MPIAIJ"
12938f6be9afSLois Curfman McInnes int MatSetUnfactored_MPIAIJ(Mat A)
1294bb5a7306SBarry Smith {
1295bb5a7306SBarry Smith   Mat_MPIAIJ *a   = (Mat_MPIAIJ*)A->data;
1296bb5a7306SBarry Smith   int        ierr;
12973a40ed3dSBarry Smith 
12983a40ed3dSBarry Smith   PetscFunctionBegin;
1299bb5a7306SBarry Smith   ierr = MatSetUnfactored(a->A);CHKERRQ(ierr);
13003a40ed3dSBarry Smith   PetscFunctionReturn(0);
1301bb5a7306SBarry Smith }
1302bb5a7306SBarry Smith 
13034a2ae208SSatish Balay #undef __FUNCT__
13044a2ae208SSatish Balay #define __FUNCT__ "MatEqual_MPIAIJ"
1305d4bb536fSBarry Smith int MatEqual_MPIAIJ(Mat A,Mat B,PetscTruth *flag)
1306d4bb536fSBarry Smith {
1307d4bb536fSBarry Smith   Mat_MPIAIJ *matB = (Mat_MPIAIJ*)B->data,*matA = (Mat_MPIAIJ*)A->data;
1308d4bb536fSBarry Smith   Mat        a,b,c,d;
1309d4bb536fSBarry Smith   PetscTruth flg;
1310d4bb536fSBarry Smith   int        ierr;
1311d4bb536fSBarry Smith 
13123a40ed3dSBarry Smith   PetscFunctionBegin;
1313273d9f13SBarry Smith   ierr = PetscTypeCompare((PetscObject)B,MATMPIAIJ,&flg);CHKERRQ(ierr);
1314273d9f13SBarry Smith   if (!flg) SETERRQ(PETSC_ERR_ARG_INCOMP,"Matrices must be same type");
1315d4bb536fSBarry Smith   a = matA->A; b = matA->B;
1316d4bb536fSBarry Smith   c = matB->A; d = matB->B;
1317d4bb536fSBarry Smith 
1318d4bb536fSBarry Smith   ierr = MatEqual(a,c,&flg);CHKERRQ(ierr);
1319d4bb536fSBarry Smith   if (flg == PETSC_TRUE) {
1320d4bb536fSBarry Smith     ierr = MatEqual(b,d,&flg);CHKERRQ(ierr);
1321d4bb536fSBarry Smith   }
1322ca161407SBarry Smith   ierr = MPI_Allreduce(&flg,flag,1,MPI_INT,MPI_LAND,A->comm);CHKERRQ(ierr);
13233a40ed3dSBarry Smith   PetscFunctionReturn(0);
1324d4bb536fSBarry Smith }
1325d4bb536fSBarry Smith 
13264a2ae208SSatish Balay #undef __FUNCT__
13274a2ae208SSatish Balay #define __FUNCT__ "MatCopy_MPIAIJ"
1328cb5b572fSBarry Smith int MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str)
1329cb5b572fSBarry Smith {
1330cb5b572fSBarry Smith   int        ierr;
1331cb5b572fSBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;
1332cb5b572fSBarry Smith   Mat_MPIAIJ *b = (Mat_MPIAIJ *)B->data;
1333273d9f13SBarry Smith   PetscTruth flg;
1334cb5b572fSBarry Smith 
1335cb5b572fSBarry Smith   PetscFunctionBegin;
1336273d9f13SBarry Smith   ierr = PetscTypeCompare((PetscObject)B,MATMPIAIJ,&flg);CHKERRQ(ierr);
1337273d9f13SBarry Smith   if (str != SAME_NONZERO_PATTERN || !flg) {
1338cb5b572fSBarry Smith     /* because of the column compression in the off-processor part of the matrix a->B,
1339cb5b572fSBarry Smith        the number of columns in a->B and b->B may be different, hence we cannot call
1340cb5b572fSBarry Smith        the MatCopy() directly on the two parts. If need be, we can provide a more
1341cb5b572fSBarry Smith        efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices
1342cb5b572fSBarry Smith        then copying the submatrices */
1343cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1344cb5b572fSBarry Smith   } else {
1345cb5b572fSBarry Smith     ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr);
1346cb5b572fSBarry Smith     ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr);
1347cb5b572fSBarry Smith   }
1348cb5b572fSBarry Smith   PetscFunctionReturn(0);
1349cb5b572fSBarry Smith }
1350cb5b572fSBarry Smith 
13514a2ae208SSatish Balay #undef __FUNCT__
13524a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_MPIAIJ"
1353273d9f13SBarry Smith int MatSetUpPreallocation_MPIAIJ(Mat A)
1354273d9f13SBarry Smith {
1355273d9f13SBarry Smith   int        ierr;
1356273d9f13SBarry Smith 
1357273d9f13SBarry Smith   PetscFunctionBegin;
1358273d9f13SBarry Smith   ierr =  MatMPIAIJSetPreallocation(A,PETSC_DEFAULT,0,PETSC_DEFAULT,0);CHKERRQ(ierr);
1359273d9f13SBarry Smith   PetscFunctionReturn(0);
1360273d9f13SBarry Smith }
1361273d9f13SBarry Smith 
1362ca44d042SBarry Smith EXTERN int MatDuplicate_MPIAIJ(Mat,MatDuplicateOption,Mat *);
1363ca44d042SBarry Smith EXTERN int MatIncreaseOverlap_MPIAIJ(Mat,int,IS *,int);
1364ca44d042SBarry Smith EXTERN int MatFDColoringCreate_MPIAIJ(Mat,ISColoring,MatFDColoring);
1365ca44d042SBarry Smith EXTERN int MatGetSubMatrices_MPIAIJ (Mat,int,IS *,IS *,MatReuse,Mat **);
1366ca44d042SBarry Smith EXTERN int MatGetSubMatrix_MPIAIJ (Mat,IS,IS,int,MatReuse,Mat *);
136787828ca2SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
1368b9617806SBarry Smith EXTERN int MatLUFactorSymbolic_MPIAIJ_TFS(Mat,IS,IS,MatLUInfo*,Mat*);
1369b9617806SBarry Smith #endif
137000e6dbe6SBarry Smith 
1371ac90fabeSBarry Smith #include "petscblaslapack.h"
1372ac90fabeSBarry Smith 
1373ac90fabeSBarry Smith #undef __FUNCT__
1374ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_MPIAIJ"
1375ac90fabeSBarry Smith int MatAXPY_MPIAIJ(PetscScalar *a,Mat X,Mat Y,MatStructure str)
1376ac90fabeSBarry Smith {
1377ac90fabeSBarry Smith   int        ierr,one;
1378ac90fabeSBarry Smith   Mat_MPIAIJ *xx  = (Mat_MPIAIJ *)X->data,*yy = (Mat_MPIAIJ *)Y->data;
1379ac90fabeSBarry Smith   Mat_SeqAIJ *x,*y;
1380ac90fabeSBarry Smith 
1381ac90fabeSBarry Smith   PetscFunctionBegin;
1382ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
1383ac90fabeSBarry Smith     x  = (Mat_SeqAIJ *)xx->A->data;
1384ac90fabeSBarry Smith     y  = (Mat_SeqAIJ *)yy->A->data;
1385ac90fabeSBarry Smith     BLaxpy_(&x->nz,a,x->a,&one,y->a,&one);
1386ac90fabeSBarry Smith     x  = (Mat_SeqAIJ *)xx->B->data;
1387ac90fabeSBarry Smith     y  = (Mat_SeqAIJ *)yy->B->data;
1388ac90fabeSBarry Smith     BLaxpy_(&x->nz,a,x->a,&one,y->a,&one);
1389ac90fabeSBarry Smith   } else {
1390ac90fabeSBarry Smith     ierr = MatAXPY_Basic(a,X,Y,str);CHKERRQ(ierr);
1391ac90fabeSBarry Smith   }
1392ac90fabeSBarry Smith   PetscFunctionReturn(0);
1393ac90fabeSBarry Smith }
1394ac90fabeSBarry Smith 
13958a729477SBarry Smith /* -------------------------------------------------------------------*/
1396cda55fadSBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ,
1397cda55fadSBarry Smith        MatGetRow_MPIAIJ,
1398cda55fadSBarry Smith        MatRestoreRow_MPIAIJ,
1399cda55fadSBarry Smith        MatMult_MPIAIJ,
1400cda55fadSBarry Smith        MatMultAdd_MPIAIJ,
14017c922b88SBarry Smith        MatMultTranspose_MPIAIJ,
14027c922b88SBarry Smith        MatMultTransposeAdd_MPIAIJ,
1403cda55fadSBarry Smith        0,
1404cda55fadSBarry Smith        0,
1405cda55fadSBarry Smith        0,
1406cda55fadSBarry Smith        0,
1407cda55fadSBarry Smith        0,
1408cda55fadSBarry Smith        0,
140944a69424SLois Curfman McInnes        MatRelax_MPIAIJ,
1410b7c46309SBarry Smith        MatTranspose_MPIAIJ,
1411cda55fadSBarry Smith        MatGetInfo_MPIAIJ,
1412cda55fadSBarry Smith        MatEqual_MPIAIJ,
1413cda55fadSBarry Smith        MatGetDiagonal_MPIAIJ,
1414cda55fadSBarry Smith        MatDiagonalScale_MPIAIJ,
1415cda55fadSBarry Smith        MatNorm_MPIAIJ,
1416cda55fadSBarry Smith        MatAssemblyBegin_MPIAIJ,
1417cda55fadSBarry Smith        MatAssemblyEnd_MPIAIJ,
14181eb62cbbSBarry Smith        0,
1419cda55fadSBarry Smith        MatSetOption_MPIAIJ,
1420cda55fadSBarry Smith        MatZeroEntries_MPIAIJ,
1421cda55fadSBarry Smith        MatZeroRows_MPIAIJ,
142287828ca2SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
1423b9617806SBarry Smith        MatLUFactorSymbolic_MPIAIJ_TFS,
1424b9617806SBarry Smith #else
1425cda55fadSBarry Smith        0,
1426b9617806SBarry Smith #endif
1427cda55fadSBarry Smith        0,
1428cda55fadSBarry Smith        0,
1429cda55fadSBarry Smith        0,
1430273d9f13SBarry Smith        MatSetUpPreallocation_MPIAIJ,
1431cda55fadSBarry Smith        0,
1432cda55fadSBarry Smith        0,
1433cda55fadSBarry Smith        0,
1434cda55fadSBarry Smith        0,
14352e8a6d31SBarry Smith        MatDuplicate_MPIAIJ,
1436cda55fadSBarry Smith        0,
1437cda55fadSBarry Smith        0,
1438cda55fadSBarry Smith        0,
1439cda55fadSBarry Smith        0,
1440ac90fabeSBarry Smith        MatAXPY_MPIAIJ,
1441cda55fadSBarry Smith        MatGetSubMatrices_MPIAIJ,
1442cda55fadSBarry Smith        MatIncreaseOverlap_MPIAIJ,
1443cda55fadSBarry Smith        MatGetValues_MPIAIJ,
1444cb5b572fSBarry Smith        MatCopy_MPIAIJ,
1445052efed2SBarry Smith        MatPrintHelp_MPIAIJ,
1446cda55fadSBarry Smith        MatScale_MPIAIJ,
1447cda55fadSBarry Smith        0,
1448cda55fadSBarry Smith        0,
1449cda55fadSBarry Smith        0,
1450cda55fadSBarry Smith        MatGetBlockSize_MPIAIJ,
1451cda55fadSBarry Smith        0,
1452cda55fadSBarry Smith        0,
1453cda55fadSBarry Smith        0,
1454cda55fadSBarry Smith        0,
1455cda55fadSBarry Smith        MatFDColoringCreate_MPIAIJ,
1456cda55fadSBarry Smith        0,
1457cda55fadSBarry Smith        MatSetUnfactored_MPIAIJ,
1458cda55fadSBarry Smith        0,
1459cda55fadSBarry Smith        0,
1460cda55fadSBarry Smith        MatGetSubMatrix_MPIAIJ,
1461e03a110bSBarry Smith        MatDestroy_MPIAIJ,
1462e03a110bSBarry Smith        MatView_MPIAIJ,
14638a124369SBarry Smith        MatGetPetscMaps_Petsc,
1464a2243be0SBarry Smith        0,
1465a2243be0SBarry Smith        0,
1466a2243be0SBarry Smith        0,
1467a2243be0SBarry Smith        0,
1468a2243be0SBarry Smith        0,
1469a2243be0SBarry Smith        0,
1470a2243be0SBarry Smith        0,
1471a2243be0SBarry Smith        0,
1472a2243be0SBarry Smith        MatSetColoring_MPIAIJ,
1473779c1a83SBarry Smith        MatSetValuesAdic_MPIAIJ,
1474779c1a83SBarry Smith        MatSetValuesAdifor_MPIAIJ
1475a2243be0SBarry Smith };
147636ce4990SBarry Smith 
14772e8a6d31SBarry Smith /* ----------------------------------------------------------------------------------------*/
14782e8a6d31SBarry Smith 
1479fb2e594dSBarry Smith EXTERN_C_BEGIN
14804a2ae208SSatish Balay #undef __FUNCT__
14814a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_MPIAIJ"
14822e8a6d31SBarry Smith int MatStoreValues_MPIAIJ(Mat mat)
14832e8a6d31SBarry Smith {
14842e8a6d31SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data;
14852e8a6d31SBarry Smith   int        ierr;
14862e8a6d31SBarry Smith 
14872e8a6d31SBarry Smith   PetscFunctionBegin;
14882e8a6d31SBarry Smith   ierr = MatStoreValues(aij->A);CHKERRQ(ierr);
14892e8a6d31SBarry Smith   ierr = MatStoreValues(aij->B);CHKERRQ(ierr);
14902e8a6d31SBarry Smith   PetscFunctionReturn(0);
14912e8a6d31SBarry Smith }
1492fb2e594dSBarry Smith EXTERN_C_END
14932e8a6d31SBarry Smith 
1494fb2e594dSBarry Smith EXTERN_C_BEGIN
14954a2ae208SSatish Balay #undef __FUNCT__
14964a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_MPIAIJ"
14972e8a6d31SBarry Smith int MatRetrieveValues_MPIAIJ(Mat mat)
14982e8a6d31SBarry Smith {
14992e8a6d31SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data;
15002e8a6d31SBarry Smith   int        ierr;
15012e8a6d31SBarry Smith 
15022e8a6d31SBarry Smith   PetscFunctionBegin;
15032e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr);
15042e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr);
15052e8a6d31SBarry Smith   PetscFunctionReturn(0);
15062e8a6d31SBarry Smith }
1507fb2e594dSBarry Smith EXTERN_C_END
15088a729477SBarry Smith 
1509e090d566SSatish Balay #include "petscpc.h"
151027508adbSBarry Smith EXTERN_C_BEGIN
1511ca44d042SBarry Smith EXTERN int MatGetDiagonalBlock_MPIAIJ(Mat,PetscTruth *,MatReuse,Mat *);
151227508adbSBarry Smith EXTERN_C_END
151327508adbSBarry Smith 
1514273d9f13SBarry Smith EXTERN_C_BEGIN
15154a2ae208SSatish Balay #undef __FUNCT__
15164a2ae208SSatish Balay #define __FUNCT__ "MatCreate_MPIAIJ"
1517273d9f13SBarry Smith int MatCreate_MPIAIJ(Mat B)
15188a729477SBarry Smith {
151944cd7ae7SLois Curfman McInnes   Mat_MPIAIJ *b;
1520f1af5d2fSBarry Smith   int        ierr,i,size;
1521e86c5b9aSHong Zhang #if defined(PETSC_HAVE_SUPERLUDIST)
1522e86c5b9aSHong Zhang   PetscTruth flg;
1523e86c5b9aSHong Zhang #endif
1524e86c5b9aSHong Zhang 
1525416022c9SBarry Smith 
15263a40ed3dSBarry Smith   PetscFunctionBegin;
1527273d9f13SBarry Smith   ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr);
15281d55c564SBarry Smith 
1529b0a32e0cSBarry Smith   ierr            = PetscNew(Mat_MPIAIJ,&b);CHKERRQ(ierr);
1530b0a32e0cSBarry Smith   B->data         = (void*)b;
1531549d3d68SSatish Balay   ierr            = PetscMemzero(b,sizeof(Mat_MPIAIJ));CHKERRQ(ierr);
1532549d3d68SSatish Balay   ierr            = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
153344cd7ae7SLois Curfman McInnes   B->factor       = 0;
153444cd7ae7SLois Curfman McInnes   B->assembled    = PETSC_FALSE;
153590f02eecSBarry Smith   B->mapping      = 0;
1536d6dfbf8fSBarry Smith 
153747794344SBarry Smith   B->insertmode      = NOT_SET_VALUES;
15389eb4d147SSatish Balay   b->size            = size;
1539273d9f13SBarry Smith   ierr = MPI_Comm_rank(B->comm,&b->rank);CHKERRQ(ierr);
15401eb62cbbSBarry Smith 
1541273d9f13SBarry Smith   ierr = PetscSplitOwnership(B->comm,&B->m,&B->M);CHKERRQ(ierr);
1542273d9f13SBarry Smith   ierr = PetscSplitOwnership(B->comm,&B->n,&B->N);CHKERRQ(ierr);
15431eb62cbbSBarry Smith 
1544c7fcc2eaSBarry Smith   /* the information in the maps duplicates the information computed below, eventually
1545c7fcc2eaSBarry Smith      we should remove the duplicate information that is not contained in the maps */
15468a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->m,B->M,&B->rmap);CHKERRQ(ierr);
15478a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->n,B->N,&B->cmap);CHKERRQ(ierr);
1548c7fcc2eaSBarry Smith 
15491eb62cbbSBarry Smith   /* build local table of row and column ownerships */
1550b0a32e0cSBarry Smith   ierr = PetscMalloc(2*(b->size+2)*sizeof(int),&b->rowners);CHKERRQ(ierr);
1551b0a32e0cSBarry Smith   PetscLogObjectMemory(B,2*(b->size+2)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_MPIAIJ));
1552603f58a4SSatish Balay   b->cowners = b->rowners + b->size + 2;
1553273d9f13SBarry Smith   ierr = MPI_Allgather(&B->m,1,MPI_INT,b->rowners+1,1,MPI_INT,B->comm);CHKERRQ(ierr);
155444cd7ae7SLois Curfman McInnes   b->rowners[0] = 0;
155544cd7ae7SLois Curfman McInnes   for (i=2; i<=b->size; i++) {
155644cd7ae7SLois Curfman McInnes     b->rowners[i] += b->rowners[i-1];
15578a729477SBarry Smith   }
155844cd7ae7SLois Curfman McInnes   b->rstart = b->rowners[b->rank];
155944cd7ae7SLois Curfman McInnes   b->rend   = b->rowners[b->rank+1];
1560273d9f13SBarry Smith   ierr = MPI_Allgather(&B->n,1,MPI_INT,b->cowners+1,1,MPI_INT,B->comm);CHKERRQ(ierr);
156144cd7ae7SLois Curfman McInnes   b->cowners[0] = 0;
156244cd7ae7SLois Curfman McInnes   for (i=2; i<=b->size; i++) {
156344cd7ae7SLois Curfman McInnes     b->cowners[i] += b->cowners[i-1];
15648a729477SBarry Smith   }
156544cd7ae7SLois Curfman McInnes   b->cstart = b->cowners[b->rank];
156644cd7ae7SLois Curfman McInnes   b->cend   = b->cowners[b->rank+1];
15678a729477SBarry Smith 
15681eb62cbbSBarry Smith   /* build cache for off array entries formed */
15697e2c5f70SBarry Smith   ierr = MatStashCreate_Private(B->comm,1,&B->stash);CHKERRQ(ierr);
15707c922b88SBarry Smith   b->donotstash  = PETSC_FALSE;
157144cd7ae7SLois Curfman McInnes   b->colmap      = 0;
157244cd7ae7SLois Curfman McInnes   b->garray      = 0;
15737c922b88SBarry Smith   b->roworiented = PETSC_TRUE;
15748a729477SBarry Smith 
15751eb62cbbSBarry Smith   /* stuff used for matrix vector multiply */
15767c922b88SBarry Smith   b->lvec      = PETSC_NULL;
15777c922b88SBarry Smith   b->Mvctx     = PETSC_NULL;
15788a729477SBarry Smith 
15797a0afa10SBarry Smith   /* stuff for MatGetRow() */
158044cd7ae7SLois Curfman McInnes   b->rowindices   = 0;
158144cd7ae7SLois Curfman McInnes   b->rowvalues    = 0;
158244cd7ae7SLois Curfman McInnes   b->getrowactive = PETSC_FALSE;
15837a0afa10SBarry Smith 
1584e86c5b9aSHong Zhang #if defined(PETSC_HAVE_SUPERLUDIST)
15859b9367d5SHong Zhang   ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_superlu_dist",&flg);CHKERRQ(ierr);
15869b9367d5SHong Zhang   if (flg) { ierr = MatUseSuperLU_DIST_MPIAIJ(B);CHKERRQ(ierr); }
1587e86c5b9aSHong Zhang #endif
15889b9367d5SHong Zhang 
1589f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
15902e8a6d31SBarry Smith                                      "MatStoreValues_MPIAIJ",
15910c97f09dSSatish Balay                                      MatStoreValues_MPIAIJ);CHKERRQ(ierr);
1592f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
15932e8a6d31SBarry Smith                                      "MatRetrieveValues_MPIAIJ",
15940c97f09dSSatish Balay                                      MatRetrieveValues_MPIAIJ);CHKERRQ(ierr);
1595f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetDiagonalBlock_C",
15965ef9f2a5SBarry Smith                                      "MatGetDiagonalBlock_MPIAIJ",
15970c97f09dSSatish Balay                                      MatGetDiagonalBlock_MPIAIJ);CHKERRQ(ierr);
15989b9367d5SHong Zhang 
15993a40ed3dSBarry Smith   PetscFunctionReturn(0);
1600d6dfbf8fSBarry Smith }
1601273d9f13SBarry Smith EXTERN_C_END
1602c74985f6SBarry Smith 
16034a2ae208SSatish Balay #undef __FUNCT__
16044a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_MPIAIJ"
16052e8a6d31SBarry Smith int MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
1606d6dfbf8fSBarry Smith {
1607d6dfbf8fSBarry Smith   Mat        mat;
1608416022c9SBarry Smith   Mat_MPIAIJ *a,*oldmat = (Mat_MPIAIJ*)matin->data;
160916d6aa21SSatish Balay   int        ierr;
1610d6dfbf8fSBarry Smith 
16113a40ed3dSBarry Smith   PetscFunctionBegin;
1612416022c9SBarry Smith   *newmat       = 0;
1613273d9f13SBarry Smith   ierr = MatCreate(matin->comm,matin->m,matin->n,matin->M,matin->N,&mat);CHKERRQ(ierr);
1614273d9f13SBarry Smith   ierr = MatSetType(mat,MATMPIAIJ);CHKERRQ(ierr);
1615273d9f13SBarry Smith   a    = (Mat_MPIAIJ*)mat->data;
1616549d3d68SSatish Balay   ierr              = PetscMemcpy(mat->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
1617d6dfbf8fSBarry Smith   mat->factor       = matin->factor;
1618c456f294SBarry Smith   mat->assembled    = PETSC_TRUE;
1619e7641de0SSatish Balay   mat->insertmode   = NOT_SET_VALUES;
1620273d9f13SBarry Smith   mat->preallocated = PETSC_TRUE;
1621d6dfbf8fSBarry Smith 
1622416022c9SBarry Smith   a->rstart       = oldmat->rstart;
1623416022c9SBarry Smith   a->rend         = oldmat->rend;
1624416022c9SBarry Smith   a->cstart       = oldmat->cstart;
1625416022c9SBarry Smith   a->cend         = oldmat->cend;
162617699dbbSLois Curfman McInnes   a->size         = oldmat->size;
162717699dbbSLois Curfman McInnes   a->rank         = oldmat->rank;
1628e7641de0SSatish Balay   a->donotstash   = oldmat->donotstash;
1629e7641de0SSatish Balay   a->roworiented  = oldmat->roworiented;
1630e7641de0SSatish Balay   a->rowindices   = 0;
1631bcd2baecSBarry Smith   a->rowvalues    = 0;
1632bcd2baecSBarry Smith   a->getrowactive = PETSC_FALSE;
1633d6dfbf8fSBarry Smith 
1634549d3d68SSatish Balay   ierr       = PetscMemcpy(a->rowners,oldmat->rowners,2*(a->size+2)*sizeof(int));CHKERRQ(ierr);
16358798bf22SSatish Balay   ierr       = MatStashCreate_Private(matin->comm,1,&mat->stash);CHKERRQ(ierr);
16362ee70a88SLois Curfman McInnes   if (oldmat->colmap) {
1637aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
16380f5bd95cSBarry Smith     ierr = PetscTableCreateCopy(oldmat->colmap,&a->colmap);CHKERRQ(ierr);
1639b1fc9764SSatish Balay #else
1640b0a32e0cSBarry Smith     ierr = PetscMalloc((mat->N)*sizeof(int),&a->colmap);CHKERRQ(ierr);
1641b0a32e0cSBarry Smith     PetscLogObjectMemory(mat,(mat->N)*sizeof(int));
1642273d9f13SBarry Smith     ierr      = PetscMemcpy(a->colmap,oldmat->colmap,(mat->N)*sizeof(int));CHKERRQ(ierr);
1643b1fc9764SSatish Balay #endif
1644416022c9SBarry Smith   } else a->colmap = 0;
16453f41c07dSBarry Smith   if (oldmat->garray) {
164616d6aa21SSatish Balay     int len;
1647273d9f13SBarry Smith     len  = oldmat->B->n;
1648b0a32e0cSBarry Smith     ierr = PetscMalloc((len+1)*sizeof(int),&a->garray);CHKERRQ(ierr);
1649b0a32e0cSBarry Smith     PetscLogObjectMemory(mat,len*sizeof(int));
1650549d3d68SSatish Balay     if (len) { ierr = PetscMemcpy(a->garray,oldmat->garray,len*sizeof(int));CHKERRQ(ierr); }
1651416022c9SBarry Smith   } else a->garray = 0;
1652d6dfbf8fSBarry Smith 
1653416022c9SBarry Smith   ierr =  VecDuplicate(oldmat->lvec,&a->lvec);CHKERRQ(ierr);
1654b0a32e0cSBarry Smith   PetscLogObjectParent(mat,a->lvec);
1655a56f8943SBarry Smith   ierr =  VecScatterCopy(oldmat->Mvctx,&a->Mvctx);CHKERRQ(ierr);
1656b0a32e0cSBarry Smith   PetscLogObjectParent(mat,a->Mvctx);
16572e8a6d31SBarry Smith   ierr =  MatDuplicate(oldmat->A,cpvalues,&a->A);CHKERRQ(ierr);
1658b0a32e0cSBarry Smith   PetscLogObjectParent(mat,a->A);
16592e8a6d31SBarry Smith   ierr =  MatDuplicate(oldmat->B,cpvalues,&a->B);CHKERRQ(ierr);
1660b0a32e0cSBarry Smith   PetscLogObjectParent(mat,a->B);
1661b0a32e0cSBarry Smith   ierr = PetscFListDuplicate(matin->qlist,&mat->qlist);CHKERRQ(ierr);
16628a729477SBarry Smith   *newmat = mat;
16633a40ed3dSBarry Smith   PetscFunctionReturn(0);
16648a729477SBarry Smith }
1665416022c9SBarry Smith 
1666e090d566SSatish Balay #include "petscsys.h"
1667416022c9SBarry Smith 
1668273d9f13SBarry Smith EXTERN_C_BEGIN
16694a2ae208SSatish Balay #undef __FUNCT__
16704a2ae208SSatish Balay #define __FUNCT__ "MatLoad_MPIAIJ"
1671b0a32e0cSBarry Smith int MatLoad_MPIAIJ(PetscViewer viewer,MatType type,Mat *newmat)
1672416022c9SBarry Smith {
1673d65a2f8fSBarry Smith   Mat          A;
167487828ca2SBarry Smith   PetscScalar  *vals,*svals;
167519bcc07fSBarry Smith   MPI_Comm     comm = ((PetscObject)viewer)->comm;
1676416022c9SBarry Smith   MPI_Status   status;
16773a40ed3dSBarry Smith   int          i,nz,ierr,j,rstart,rend,fd;
167817699dbbSLois Curfman McInnes   int          header[4],rank,size,*rowlengths = 0,M,N,m,*rowners,maxnz,*cols;
1679d65a2f8fSBarry Smith   int          *ourlens,*sndcounts = 0,*procsnz = 0,*offlens,jj,*mycols,*smycols;
1680b362ba68SBarry Smith   int          tag = ((PetscObject)viewer)->tag,cend,cstart,n;
1681416022c9SBarry Smith 
16823a40ed3dSBarry Smith   PetscFunctionBegin;
16831dab6e02SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
16841dab6e02SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
168517699dbbSLois Curfman McInnes   if (!rank) {
1686b0a32e0cSBarry Smith     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
16870752156aSBarry Smith     ierr = PetscBinaryRead(fd,(char *)header,4,PETSC_INT);CHKERRQ(ierr);
1688552e946dSBarry Smith     if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
1689d64ed03dSBarry Smith     if (header[3] < 0) {
169029bbc08cSBarry Smith       SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix in special format on disk, cannot load as MPIAIJ");
1691d64ed03dSBarry Smith     }
16926c5fab8fSBarry Smith   }
16936c5fab8fSBarry Smith 
1694ca161407SBarry Smith   ierr = MPI_Bcast(header+1,3,MPI_INT,0,comm);CHKERRQ(ierr);
1695416022c9SBarry Smith   M = header[1]; N = header[2];
1696416022c9SBarry Smith   /* determine ownership of all rows */
169717699dbbSLois Curfman McInnes   m = M/size + ((M % size) > rank);
1698b0a32e0cSBarry Smith   ierr = PetscMalloc((size+2)*sizeof(int),&rowners);CHKERRQ(ierr);
1699ca161407SBarry Smith   ierr = MPI_Allgather(&m,1,MPI_INT,rowners+1,1,MPI_INT,comm);CHKERRQ(ierr);
1700416022c9SBarry Smith   rowners[0] = 0;
170117699dbbSLois Curfman McInnes   for (i=2; i<=size; i++) {
1702416022c9SBarry Smith     rowners[i] += rowners[i-1];
1703416022c9SBarry Smith   }
170417699dbbSLois Curfman McInnes   rstart = rowners[rank];
170517699dbbSLois Curfman McInnes   rend   = rowners[rank+1];
1706416022c9SBarry Smith 
1707416022c9SBarry Smith   /* distribute row lengths to all processors */
1708b0a32e0cSBarry Smith   ierr    = PetscMalloc(2*(rend-rstart+1)*sizeof(int),&ourlens);CHKERRQ(ierr);
1709416022c9SBarry Smith   offlens = ourlens + (rend-rstart);
171017699dbbSLois Curfman McInnes   if (!rank) {
1711b0a32e0cSBarry Smith     ierr = PetscMalloc(M*sizeof(int),&rowlengths);CHKERRQ(ierr);
17120752156aSBarry Smith     ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
1713b0a32e0cSBarry Smith     ierr = PetscMalloc(size*sizeof(int),&sndcounts);CHKERRQ(ierr);
171417699dbbSLois Curfman McInnes     for (i=0; i<size; i++) sndcounts[i] = rowners[i+1] - rowners[i];
1715ca161407SBarry Smith     ierr = MPI_Scatterv(rowlengths,sndcounts,rowners,MPI_INT,ourlens,rend-rstart,MPI_INT,0,comm);CHKERRQ(ierr);
1716606d414cSSatish Balay     ierr = PetscFree(sndcounts);CHKERRQ(ierr);
17173a40ed3dSBarry Smith   } else {
1718ca161407SBarry Smith     ierr = MPI_Scatterv(0,0,0,MPI_INT,ourlens,rend-rstart,MPI_INT,0,comm);CHKERRQ(ierr);
1719416022c9SBarry Smith   }
1720416022c9SBarry Smith 
172117699dbbSLois Curfman McInnes   if (!rank) {
1722416022c9SBarry Smith     /* calculate the number of nonzeros on each processor */
1723b0a32e0cSBarry Smith     ierr = PetscMalloc(size*sizeof(int),&procsnz);CHKERRQ(ierr);
1724549d3d68SSatish Balay     ierr = PetscMemzero(procsnz,size*sizeof(int));CHKERRQ(ierr);
172517699dbbSLois Curfman McInnes     for (i=0; i<size; i++) {
1726416022c9SBarry Smith       for (j=rowners[i]; j< rowners[i+1]; j++) {
1727416022c9SBarry Smith         procsnz[i] += rowlengths[j];
1728416022c9SBarry Smith       }
1729416022c9SBarry Smith     }
1730606d414cSSatish Balay     ierr = PetscFree(rowlengths);CHKERRQ(ierr);
1731416022c9SBarry Smith 
1732416022c9SBarry Smith     /* determine max buffer needed and allocate it */
1733416022c9SBarry Smith     maxnz = 0;
173417699dbbSLois Curfman McInnes     for (i=0; i<size; i++) {
17350452661fSBarry Smith       maxnz = PetscMax(maxnz,procsnz[i]);
1736416022c9SBarry Smith     }
1737b0a32e0cSBarry Smith     ierr = PetscMalloc(maxnz*sizeof(int),&cols);CHKERRQ(ierr);
1738416022c9SBarry Smith 
1739416022c9SBarry Smith     /* read in my part of the matrix column indices  */
1740416022c9SBarry Smith     nz   = procsnz[0];
1741b0a32e0cSBarry Smith     ierr = PetscMalloc(nz*sizeof(int),&mycols);CHKERRQ(ierr);
17420752156aSBarry Smith     ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr);
1743d65a2f8fSBarry Smith 
1744d65a2f8fSBarry Smith     /* read in every one elses and ship off */
174517699dbbSLois Curfman McInnes     for (i=1; i<size; i++) {
1746d65a2f8fSBarry Smith       nz   = procsnz[i];
17470752156aSBarry Smith       ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr);
1748ca161407SBarry Smith       ierr = MPI_Send(cols,nz,MPI_INT,i,tag,comm);CHKERRQ(ierr);
1749d65a2f8fSBarry Smith     }
1750606d414cSSatish Balay     ierr = PetscFree(cols);CHKERRQ(ierr);
17513a40ed3dSBarry Smith   } else {
1752416022c9SBarry Smith     /* determine buffer space needed for message */
1753416022c9SBarry Smith     nz = 0;
1754416022c9SBarry Smith     for (i=0; i<m; i++) {
1755416022c9SBarry Smith       nz += ourlens[i];
1756416022c9SBarry Smith     }
1757b0a32e0cSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(int),&mycols);CHKERRQ(ierr);
1758416022c9SBarry Smith 
1759416022c9SBarry Smith     /* receive message of column indices*/
1760ca161407SBarry Smith     ierr = MPI_Recv(mycols,nz,MPI_INT,0,tag,comm,&status);CHKERRQ(ierr);
1761ca161407SBarry Smith     ierr = MPI_Get_count(&status,MPI_INT,&maxnz);CHKERRQ(ierr);
176229bbc08cSBarry Smith     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
1763416022c9SBarry Smith   }
1764416022c9SBarry Smith 
1765b362ba68SBarry Smith   /* determine column ownership if matrix is not square */
1766b362ba68SBarry Smith   if (N != M) {
1767b362ba68SBarry Smith     n      = N/size + ((N % size) > rank);
1768b362ba68SBarry Smith     ierr   = MPI_Scan(&n,&cend,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr);
1769b362ba68SBarry Smith     cstart = cend - n;
1770b362ba68SBarry Smith   } else {
1771b362ba68SBarry Smith     cstart = rstart;
1772b362ba68SBarry Smith     cend   = rend;
1773fb2e594dSBarry Smith     n      = cend - cstart;
1774b362ba68SBarry Smith   }
1775b362ba68SBarry Smith 
1776416022c9SBarry Smith   /* loop over local rows, determining number of off diagonal entries */
1777549d3d68SSatish Balay   ierr = PetscMemzero(offlens,m*sizeof(int));CHKERRQ(ierr);
1778416022c9SBarry Smith   jj = 0;
1779416022c9SBarry Smith   for (i=0; i<m; i++) {
1780416022c9SBarry Smith     for (j=0; j<ourlens[i]; j++) {
1781b362ba68SBarry Smith       if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++;
1782416022c9SBarry Smith       jj++;
1783416022c9SBarry Smith     }
1784416022c9SBarry Smith   }
1785d65a2f8fSBarry Smith 
1786d65a2f8fSBarry Smith   /* create our matrix */
1787416022c9SBarry Smith   for (i=0; i<m; i++) {
1788416022c9SBarry Smith     ourlens[i] -= offlens[i];
1789416022c9SBarry Smith   }
1790b362ba68SBarry Smith   ierr = MatCreateMPIAIJ(comm,m,n,M,N,0,ourlens,0,offlens,newmat);CHKERRQ(ierr);
1791d65a2f8fSBarry Smith   A = *newmat;
1792fb2e594dSBarry Smith   ierr = MatSetOption(A,MAT_COLUMNS_SORTED);CHKERRQ(ierr);
1793d65a2f8fSBarry Smith   for (i=0; i<m; i++) {
1794d65a2f8fSBarry Smith     ourlens[i] += offlens[i];
1795d65a2f8fSBarry Smith   }
1796416022c9SBarry Smith 
179717699dbbSLois Curfman McInnes   if (!rank) {
179887828ca2SBarry Smith     ierr = PetscMalloc(maxnz*sizeof(PetscScalar),&vals);CHKERRQ(ierr);
1799416022c9SBarry Smith 
1800416022c9SBarry Smith     /* read in my part of the matrix numerical values  */
1801416022c9SBarry Smith     nz   = procsnz[0];
18020752156aSBarry Smith     ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
1803d65a2f8fSBarry Smith 
1804d65a2f8fSBarry Smith     /* insert into matrix */
1805d65a2f8fSBarry Smith     jj      = rstart;
1806d65a2f8fSBarry Smith     smycols = mycols;
1807d65a2f8fSBarry Smith     svals   = vals;
1808d65a2f8fSBarry Smith     for (i=0; i<m; i++) {
1809d65a2f8fSBarry Smith       ierr = MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
1810d65a2f8fSBarry Smith       smycols += ourlens[i];
1811d65a2f8fSBarry Smith       svals   += ourlens[i];
1812d65a2f8fSBarry Smith       jj++;
1813416022c9SBarry Smith     }
1814416022c9SBarry Smith 
1815d65a2f8fSBarry Smith     /* read in other processors and ship out */
181617699dbbSLois Curfman McInnes     for (i=1; i<size; i++) {
1817416022c9SBarry Smith       nz   = procsnz[i];
18180752156aSBarry Smith       ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
1819ca161407SBarry Smith       ierr = MPI_Send(vals,nz,MPIU_SCALAR,i,A->tag,comm);CHKERRQ(ierr);
1820416022c9SBarry Smith     }
1821606d414cSSatish Balay     ierr = PetscFree(procsnz);CHKERRQ(ierr);
18223a40ed3dSBarry Smith   } else {
1823d65a2f8fSBarry Smith     /* receive numeric values */
182487828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&vals);CHKERRQ(ierr);
1825416022c9SBarry Smith 
1826d65a2f8fSBarry Smith     /* receive message of values*/
1827ca161407SBarry Smith     ierr = MPI_Recv(vals,nz,MPIU_SCALAR,0,A->tag,comm,&status);CHKERRQ(ierr);
1828ca161407SBarry Smith     ierr = MPI_Get_count(&status,MPIU_SCALAR,&maxnz);CHKERRQ(ierr);
182929bbc08cSBarry Smith     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
1830d65a2f8fSBarry Smith 
1831d65a2f8fSBarry Smith     /* insert into matrix */
1832d65a2f8fSBarry Smith     jj      = rstart;
1833d65a2f8fSBarry Smith     smycols = mycols;
1834d65a2f8fSBarry Smith     svals   = vals;
1835d65a2f8fSBarry Smith     for (i=0; i<m; i++) {
1836d65a2f8fSBarry Smith       ierr     = MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
1837d65a2f8fSBarry Smith       smycols += ourlens[i];
1838d65a2f8fSBarry Smith       svals   += ourlens[i];
1839d65a2f8fSBarry Smith       jj++;
1840d65a2f8fSBarry Smith     }
1841d65a2f8fSBarry Smith   }
1842606d414cSSatish Balay   ierr = PetscFree(ourlens);CHKERRQ(ierr);
1843606d414cSSatish Balay   ierr = PetscFree(vals);CHKERRQ(ierr);
1844606d414cSSatish Balay   ierr = PetscFree(mycols);CHKERRQ(ierr);
1845606d414cSSatish Balay   ierr = PetscFree(rowners);CHKERRQ(ierr);
1846d65a2f8fSBarry Smith 
18476d4a8577SBarry Smith   ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18486d4a8577SBarry Smith   ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18493a40ed3dSBarry Smith   PetscFunctionReturn(0);
1850416022c9SBarry Smith }
1851273d9f13SBarry Smith EXTERN_C_END
1852a0ff6018SBarry Smith 
18534a2ae208SSatish Balay #undef __FUNCT__
18544a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_MPIAIJ"
1855a0ff6018SBarry Smith /*
185629da9460SBarry Smith     Not great since it makes two copies of the submatrix, first an SeqAIJ
185729da9460SBarry Smith   in local and then by concatenating the local matrices the end result.
185829da9460SBarry Smith   Writing it directly would be much like MatGetSubMatrices_MPIAIJ()
1859a0ff6018SBarry Smith */
18607b2a1423SBarry Smith int MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,int csize,MatReuse call,Mat *newmat)
1861a0ff6018SBarry Smith {
186200e6dbe6SBarry Smith   int          ierr,i,m,n,rstart,row,rend,nz,*cwork,size,rank,j;
18637e2c5f70SBarry Smith   int          *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend;
1864fee21e36SBarry Smith   Mat          *local,M,Mreuse;
186587828ca2SBarry Smith   PetscScalar  *vwork,*aa;
186600e6dbe6SBarry Smith   MPI_Comm     comm = mat->comm;
186700e6dbe6SBarry Smith   Mat_SeqAIJ   *aij;
18687e2c5f70SBarry Smith 
1869a0ff6018SBarry Smith 
1870a0ff6018SBarry Smith   PetscFunctionBegin;
18711dab6e02SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
18721dab6e02SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
187300e6dbe6SBarry Smith 
1874fee21e36SBarry Smith   if (call ==  MAT_REUSE_MATRIX) {
1875fee21e36SBarry Smith     ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);CHKERRQ(ierr);
187629bbc08cSBarry Smith     if (!Mreuse) SETERRQ(1,"Submatrix passed in was not used before, cannot reuse");
1877fee21e36SBarry Smith     local = &Mreuse;
1878fee21e36SBarry Smith     ierr  = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&local);CHKERRQ(ierr);
1879fee21e36SBarry Smith   } else {
1880a0ff6018SBarry Smith     ierr   = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr);
1881fee21e36SBarry Smith     Mreuse = *local;
1882606d414cSSatish Balay     ierr   = PetscFree(local);CHKERRQ(ierr);
1883fee21e36SBarry Smith   }
1884a0ff6018SBarry Smith 
1885a0ff6018SBarry Smith   /*
1886a0ff6018SBarry Smith       m - number of local rows
1887a0ff6018SBarry Smith       n - number of columns (same on all processors)
1888a0ff6018SBarry Smith       rstart - first row in new global matrix generated
1889a0ff6018SBarry Smith   */
1890fee21e36SBarry Smith   ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr);
1891a0ff6018SBarry Smith   if (call == MAT_INITIAL_MATRIX) {
1892fee21e36SBarry Smith     aij = (Mat_SeqAIJ*)(Mreuse)->data;
189329bbc08cSBarry Smith     if (aij->indexshift) SETERRQ(PETSC_ERR_SUP,"No support for index shifted matrix");
189400e6dbe6SBarry Smith     ii  = aij->i;
189500e6dbe6SBarry Smith     jj  = aij->j;
189600e6dbe6SBarry Smith 
1897a0ff6018SBarry Smith     /*
189800e6dbe6SBarry Smith         Determine the number of non-zeros in the diagonal and off-diagonal
189900e6dbe6SBarry Smith         portions of the matrix in order to do correct preallocation
1900a0ff6018SBarry Smith     */
190100e6dbe6SBarry Smith 
190200e6dbe6SBarry Smith     /* first get start and end of "diagonal" columns */
19036a6a5d1dSBarry Smith     if (csize == PETSC_DECIDE) {
190400e6dbe6SBarry Smith       nlocal = n/size + ((n % size) > rank);
19056a6a5d1dSBarry Smith     } else {
19066a6a5d1dSBarry Smith       nlocal = csize;
19076a6a5d1dSBarry Smith     }
1908ca161407SBarry Smith     ierr   = MPI_Scan(&nlocal,&rend,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr);
190900e6dbe6SBarry Smith     rstart = rend - nlocal;
19106a6a5d1dSBarry Smith     if (rank == size - 1 && rend != n) {
191129bbc08cSBarry Smith       SETERRQ(1,"Local column sizes do not add up to total number of columns");
19126a6a5d1dSBarry Smith     }
191300e6dbe6SBarry Smith 
191400e6dbe6SBarry Smith     /* next, compute all the lengths */
1915b0a32e0cSBarry Smith     ierr  = PetscMalloc((2*m+1)*sizeof(int),&dlens);CHKERRQ(ierr);
191600e6dbe6SBarry Smith     olens = dlens + m;
191700e6dbe6SBarry Smith     for (i=0; i<m; i++) {
191800e6dbe6SBarry Smith       jend = ii[i+1] - ii[i];
191900e6dbe6SBarry Smith       olen = 0;
192000e6dbe6SBarry Smith       dlen = 0;
192100e6dbe6SBarry Smith       for (j=0; j<jend; j++) {
192200e6dbe6SBarry Smith         if (*jj < rstart || *jj >= rend) olen++;
192300e6dbe6SBarry Smith         else dlen++;
192400e6dbe6SBarry Smith         jj++;
192500e6dbe6SBarry Smith       }
192600e6dbe6SBarry Smith       olens[i] = olen;
192700e6dbe6SBarry Smith       dlens[i] = dlen;
192800e6dbe6SBarry Smith     }
19292d207970SBarry Smith     ierr = MatCreateMPIAIJ(comm,m,nlocal,PETSC_DECIDE,n,0,dlens,0,olens,&M);CHKERRQ(ierr);
1930606d414cSSatish Balay     ierr = PetscFree(dlens);CHKERRQ(ierr);
1931a0ff6018SBarry Smith   } else {
1932a0ff6018SBarry Smith     int ml,nl;
1933a0ff6018SBarry Smith 
1934a0ff6018SBarry Smith     M = *newmat;
1935a0ff6018SBarry Smith     ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr);
193629bbc08cSBarry Smith     if (ml != m) SETERRQ(PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
1937a0ff6018SBarry Smith     ierr = MatZeroEntries(M);CHKERRQ(ierr);
1938c48de900SBarry Smith     /*
1939c48de900SBarry Smith          The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
1940c48de900SBarry Smith        rather than the slower MatSetValues().
1941c48de900SBarry Smith     */
1942c48de900SBarry Smith     M->was_assembled = PETSC_TRUE;
1943c48de900SBarry Smith     M->assembled     = PETSC_FALSE;
1944a0ff6018SBarry Smith   }
1945a0ff6018SBarry Smith   ierr = MatGetOwnershipRange(M,&rstart,&rend);CHKERRQ(ierr);
1946fee21e36SBarry Smith   aij = (Mat_SeqAIJ*)(Mreuse)->data;
194729bbc08cSBarry Smith   if (aij->indexshift) SETERRQ(PETSC_ERR_SUP,"No support for index shifted matrix");
194800e6dbe6SBarry Smith   ii  = aij->i;
194900e6dbe6SBarry Smith   jj  = aij->j;
195000e6dbe6SBarry Smith   aa  = aij->a;
1951a0ff6018SBarry Smith   for (i=0; i<m; i++) {
1952a0ff6018SBarry Smith     row   = rstart + i;
195300e6dbe6SBarry Smith     nz    = ii[i+1] - ii[i];
195400e6dbe6SBarry Smith     cwork = jj;     jj += nz;
195500e6dbe6SBarry Smith     vwork = aa;     aa += nz;
19568c638d02SBarry Smith     ierr = MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr);
1957a0ff6018SBarry Smith   }
1958a0ff6018SBarry Smith 
1959a0ff6018SBarry Smith   ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1960a0ff6018SBarry Smith   ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1961a0ff6018SBarry Smith   *newmat = M;
1962fee21e36SBarry Smith 
1963fee21e36SBarry Smith   /* save submatrix used in processor for next request */
1964fee21e36SBarry Smith   if (call ==  MAT_INITIAL_MATRIX) {
1965fee21e36SBarry Smith     ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr);
1966fee21e36SBarry Smith     ierr = PetscObjectDereference((PetscObject)Mreuse);CHKERRQ(ierr);
1967fee21e36SBarry Smith   }
1968fee21e36SBarry Smith 
1969a0ff6018SBarry Smith   PetscFunctionReturn(0);
1970a0ff6018SBarry Smith }
1971273d9f13SBarry Smith 
19724a2ae208SSatish Balay #undef __FUNCT__
19734a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJSetPreallocation"
1974273d9f13SBarry Smith /*@C
1975273d9f13SBarry Smith    MatMPIAIJSetPreallocation - Creates a sparse parallel matrix in AIJ format
1976273d9f13SBarry Smith    (the default parallel PETSc format).  For good matrix assembly performance
1977273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameters
1978273d9f13SBarry Smith    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
1979273d9f13SBarry Smith    performance can be increased by more than a factor of 50.
1980273d9f13SBarry Smith 
1981273d9f13SBarry Smith    Collective on MPI_Comm
1982273d9f13SBarry Smith 
1983273d9f13SBarry Smith    Input Parameters:
1984273d9f13SBarry Smith +  A - the matrix
1985273d9f13SBarry Smith .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
1986273d9f13SBarry Smith            (same value is used for all local rows)
1987273d9f13SBarry Smith .  d_nnz - array containing the number of nonzeros in the various rows of the
1988273d9f13SBarry Smith            DIAGONAL portion of the local submatrix (possibly different for each row)
1989273d9f13SBarry Smith            or PETSC_NULL, if d_nz is used to specify the nonzero structure.
1990273d9f13SBarry Smith            The size of this array is equal to the number of local rows, i.e 'm'.
1991273d9f13SBarry Smith            You must leave room for the diagonal entry even if it is zero.
1992273d9f13SBarry Smith .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
1993273d9f13SBarry Smith            submatrix (same value is used for all local rows).
1994273d9f13SBarry Smith -  o_nnz - array containing the number of nonzeros in the various rows of the
1995273d9f13SBarry Smith            OFF-DIAGONAL portion of the local submatrix (possibly different for
1996273d9f13SBarry Smith            each row) or PETSC_NULL, if o_nz is used to specify the nonzero
1997273d9f13SBarry Smith            structure. The size of this array is equal to the number
1998273d9f13SBarry Smith            of local rows, i.e 'm'.
1999273d9f13SBarry Smith 
2000273d9f13SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
2001273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2002273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2003273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users manual for details.
2004273d9f13SBarry Smith 
2005273d9f13SBarry Smith    The user MUST specify either the local or global matrix dimensions
2006273d9f13SBarry Smith    (possibly both).
2007273d9f13SBarry Smith 
2008273d9f13SBarry Smith    The parallel matrix is partitioned such that the first m0 rows belong to
2009273d9f13SBarry Smith    process 0, the next m1 rows belong to process 1, the next m2 rows belong
2010273d9f13SBarry Smith    to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.
2011273d9f13SBarry Smith 
2012273d9f13SBarry Smith    The DIAGONAL portion of the local submatrix of a processor can be defined
2013273d9f13SBarry Smith    as the submatrix which is obtained by extraction the part corresponding
2014273d9f13SBarry Smith    to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the
2015273d9f13SBarry Smith    first row that belongs to the processor, and r2 is the last row belonging
2016273d9f13SBarry Smith    to the this processor. This is a square mxm matrix. The remaining portion
2017273d9f13SBarry Smith    of the local submatrix (mxN) constitute the OFF-DIAGONAL portion.
2018273d9f13SBarry Smith 
2019273d9f13SBarry Smith    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
2020273d9f13SBarry Smith 
2021273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible.
2022273d9f13SBarry Smith    We search for consecutive rows with the same nonzero structure, thereby
2023273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2024273d9f13SBarry Smith 
2025273d9f13SBarry Smith    Options Database Keys:
2026273d9f13SBarry Smith +  -mat_aij_no_inode  - Do not use inodes
2027273d9f13SBarry Smith .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2028273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2029273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2030273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2031273d9f13SBarry Smith 
2032273d9f13SBarry Smith    Example usage:
2033273d9f13SBarry Smith 
2034273d9f13SBarry Smith    Consider the following 8x8 matrix with 34 non-zero values, that is
2035273d9f13SBarry Smith    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
2036273d9f13SBarry Smith    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
2037273d9f13SBarry Smith    as follows:
2038273d9f13SBarry Smith 
2039273d9f13SBarry Smith .vb
2040273d9f13SBarry Smith             1  2  0  |  0  3  0  |  0  4
2041273d9f13SBarry Smith     Proc0   0  5  6  |  7  0  0  |  8  0
2042273d9f13SBarry Smith             9  0 10  | 11  0  0  | 12  0
2043273d9f13SBarry Smith     -------------------------------------
2044273d9f13SBarry Smith            13  0 14  | 15 16 17  |  0  0
2045273d9f13SBarry Smith     Proc1   0 18  0  | 19 20 21  |  0  0
2046273d9f13SBarry Smith             0  0  0  | 22 23  0  | 24  0
2047273d9f13SBarry Smith     -------------------------------------
2048273d9f13SBarry Smith     Proc2  25 26 27  |  0  0 28  | 29  0
2049273d9f13SBarry Smith            30  0  0  | 31 32 33  |  0 34
2050273d9f13SBarry Smith .ve
2051273d9f13SBarry Smith 
2052273d9f13SBarry Smith    This can be represented as a collection of submatrices as:
2053273d9f13SBarry Smith 
2054273d9f13SBarry Smith .vb
2055273d9f13SBarry Smith       A B C
2056273d9f13SBarry Smith       D E F
2057273d9f13SBarry Smith       G H I
2058273d9f13SBarry Smith .ve
2059273d9f13SBarry Smith 
2060273d9f13SBarry Smith    Where the submatrices A,B,C are owned by proc0, D,E,F are
2061273d9f13SBarry Smith    owned by proc1, G,H,I are owned by proc2.
2062273d9f13SBarry Smith 
2063273d9f13SBarry Smith    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2064273d9f13SBarry Smith    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2065273d9f13SBarry Smith    The 'M','N' parameters are 8,8, and have the same values on all procs.
2066273d9f13SBarry Smith 
2067273d9f13SBarry Smith    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
2068273d9f13SBarry Smith    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
2069273d9f13SBarry Smith    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
2070273d9f13SBarry Smith    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
2071273d9f13SBarry Smith    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
2072273d9f13SBarry Smith    matrix, ans [DF] as another SeqAIJ matrix.
2073273d9f13SBarry Smith 
2074273d9f13SBarry Smith    When d_nz, o_nz parameters are specified, d_nz storage elements are
2075273d9f13SBarry Smith    allocated for every row of the local diagonal submatrix, and o_nz
2076273d9f13SBarry Smith    storage locations are allocated for every row of the OFF-DIAGONAL submat.
2077273d9f13SBarry Smith    One way to choose d_nz and o_nz is to use the max nonzerors per local
2078273d9f13SBarry Smith    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
2079273d9f13SBarry Smith    In this case, the values of d_nz,o_nz are:
2080273d9f13SBarry Smith .vb
2081273d9f13SBarry Smith      proc0 : dnz = 2, o_nz = 2
2082273d9f13SBarry Smith      proc1 : dnz = 3, o_nz = 2
2083273d9f13SBarry Smith      proc2 : dnz = 1, o_nz = 4
2084273d9f13SBarry Smith .ve
2085273d9f13SBarry Smith    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
2086273d9f13SBarry Smith    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
2087273d9f13SBarry Smith    for proc3. i.e we are using 12+15+10=37 storage locations to store
2088273d9f13SBarry Smith    34 values.
2089273d9f13SBarry Smith 
2090273d9f13SBarry Smith    When d_nnz, o_nnz parameters are specified, the storage is specified
2091273d9f13SBarry Smith    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
2092273d9f13SBarry Smith    In the above case the values for d_nnz,o_nnz are:
2093273d9f13SBarry Smith .vb
2094273d9f13SBarry Smith      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
2095273d9f13SBarry Smith      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
2096273d9f13SBarry Smith      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
2097273d9f13SBarry Smith .ve
2098273d9f13SBarry Smith    Here the space allocated is sum of all the above values i.e 34, and
2099273d9f13SBarry Smith    hence pre-allocation is perfect.
2100273d9f13SBarry Smith 
2101273d9f13SBarry Smith    Level: intermediate
2102273d9f13SBarry Smith 
2103273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
2104273d9f13SBarry Smith 
2105273d9f13SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues()
2106273d9f13SBarry Smith @*/
2107273d9f13SBarry Smith int MatMPIAIJSetPreallocation(Mat B,int d_nz,int *d_nnz,int o_nz,int *o_nnz)
2108273d9f13SBarry Smith {
2109273d9f13SBarry Smith   Mat_MPIAIJ   *b;
2110273d9f13SBarry Smith   int          ierr,i;
2111273d9f13SBarry Smith   PetscTruth   flg2;
2112273d9f13SBarry Smith 
2113273d9f13SBarry Smith   PetscFunctionBegin;
2114273d9f13SBarry Smith   ierr = PetscTypeCompare((PetscObject)B,MATMPIAIJ,&flg2);CHKERRQ(ierr);
2115273d9f13SBarry Smith   if (!flg2) PetscFunctionReturn(0);
2116273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
2117435da068SBarry Smith   if (d_nz == PETSC_DEFAULT || d_nz == PETSC_DECIDE) d_nz = 5;
2118435da068SBarry Smith   if (o_nz == PETSC_DEFAULT || o_nz == PETSC_DECIDE) o_nz = 2;
2119435da068SBarry Smith   if (d_nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"d_nz cannot be less than 0: value %d",d_nz);
2120435da068SBarry Smith   if (o_nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"o_nz cannot be less than 0: value %d",o_nz);
2121273d9f13SBarry Smith   if (d_nnz) {
2122273d9f13SBarry Smith     for (i=0; i<B->m; i++) {
2123273d9f13SBarry Smith       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]);
2124273d9f13SBarry Smith     }
2125273d9f13SBarry Smith   }
2126273d9f13SBarry Smith   if (o_nnz) {
2127273d9f13SBarry Smith     for (i=0; i<B->m; i++) {
2128273d9f13SBarry Smith       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]);
2129273d9f13SBarry Smith     }
2130273d9f13SBarry Smith   }
2131273d9f13SBarry Smith   b = (Mat_MPIAIJ*)B->data;
2132273d9f13SBarry Smith 
2133273d9f13SBarry Smith   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,B->m,B->n,d_nz,d_nnz,&b->A);CHKERRQ(ierr);
2134b0a32e0cSBarry Smith   PetscLogObjectParent(B,b->A);
2135273d9f13SBarry Smith   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,B->m,B->N,o_nz,o_nnz,&b->B);CHKERRQ(ierr);
2136b0a32e0cSBarry Smith   PetscLogObjectParent(B,b->B);
2137273d9f13SBarry Smith 
2138273d9f13SBarry Smith   PetscFunctionReturn(0);
2139273d9f13SBarry Smith }
2140273d9f13SBarry Smith 
21414a2ae208SSatish Balay #undef __FUNCT__
21424a2ae208SSatish Balay #define __FUNCT__ "MatCreateMPIAIJ"
2143273d9f13SBarry Smith /*@C
2144273d9f13SBarry Smith    MatCreateMPIAIJ - Creates a sparse parallel matrix in AIJ format
2145273d9f13SBarry Smith    (the default parallel PETSc format).  For good matrix assembly performance
2146273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameters
2147273d9f13SBarry Smith    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
2148273d9f13SBarry Smith    performance can be increased by more than a factor of 50.
2149273d9f13SBarry Smith 
2150273d9f13SBarry Smith    Collective on MPI_Comm
2151273d9f13SBarry Smith 
2152273d9f13SBarry Smith    Input Parameters:
2153273d9f13SBarry Smith +  comm - MPI communicator
2154273d9f13SBarry Smith .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
2155273d9f13SBarry Smith            This value should be the same as the local size used in creating the
2156273d9f13SBarry Smith            y vector for the matrix-vector product y = Ax.
2157273d9f13SBarry Smith .  n - This value should be the same as the local size used in creating the
2158273d9f13SBarry Smith        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
2159273d9f13SBarry Smith        calculated if N is given) For square matrices n is almost always m.
2160273d9f13SBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
2161273d9f13SBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
2162273d9f13SBarry Smith .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
2163273d9f13SBarry Smith            (same value is used for all local rows)
2164273d9f13SBarry Smith .  d_nnz - array containing the number of nonzeros in the various rows of the
2165273d9f13SBarry Smith            DIAGONAL portion of the local submatrix (possibly different for each row)
2166273d9f13SBarry Smith            or PETSC_NULL, if d_nz is used to specify the nonzero structure.
2167273d9f13SBarry Smith            The size of this array is equal to the number of local rows, i.e 'm'.
2168273d9f13SBarry Smith            You must leave room for the diagonal entry even if it is zero.
2169273d9f13SBarry Smith .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
2170273d9f13SBarry Smith            submatrix (same value is used for all local rows).
2171273d9f13SBarry Smith -  o_nnz - array containing the number of nonzeros in the various rows of the
2172273d9f13SBarry Smith            OFF-DIAGONAL portion of the local submatrix (possibly different for
2173273d9f13SBarry Smith            each row) or PETSC_NULL, if o_nz is used to specify the nonzero
2174273d9f13SBarry Smith            structure. The size of this array is equal to the number
2175273d9f13SBarry Smith            of local rows, i.e 'm'.
2176273d9f13SBarry Smith 
2177273d9f13SBarry Smith    Output Parameter:
2178273d9f13SBarry Smith .  A - the matrix
2179273d9f13SBarry Smith 
2180273d9f13SBarry Smith    Notes:
2181273d9f13SBarry Smith    m,n,M,N parameters specify the size of the matrix, and its partitioning across
2182273d9f13SBarry Smith    processors, while d_nz,d_nnz,o_nz,o_nnz parameters specify the approximate
2183273d9f13SBarry Smith    storage requirements for this matrix.
2184273d9f13SBarry Smith 
2185273d9f13SBarry Smith    If PETSC_DECIDE or  PETSC_DETERMINE is used for a particular argument on one
2186273d9f13SBarry Smith    processor than it must be used on all processors that share the object for
2187273d9f13SBarry Smith    that argument.
2188273d9f13SBarry Smith 
2189273d9f13SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
2190273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2191273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2192273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users manual for details.
2193273d9f13SBarry Smith 
2194273d9f13SBarry Smith    The user MUST specify either the local or global matrix dimensions
2195273d9f13SBarry Smith    (possibly both).
2196273d9f13SBarry Smith 
2197273d9f13SBarry Smith    The parallel matrix is partitioned such that the first m0 rows belong to
2198273d9f13SBarry Smith    process 0, the next m1 rows belong to process 1, the next m2 rows belong
2199273d9f13SBarry Smith    to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.
2200273d9f13SBarry Smith 
2201273d9f13SBarry Smith    The DIAGONAL portion of the local submatrix of a processor can be defined
2202273d9f13SBarry Smith    as the submatrix which is obtained by extraction the part corresponding
2203273d9f13SBarry Smith    to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the
2204273d9f13SBarry Smith    first row that belongs to the processor, and r2 is the last row belonging
2205273d9f13SBarry Smith    to the this processor. This is a square mxm matrix. The remaining portion
2206273d9f13SBarry Smith    of the local submatrix (mxN) constitute the OFF-DIAGONAL portion.
2207273d9f13SBarry Smith 
2208273d9f13SBarry Smith    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
2209273d9f13SBarry Smith 
2210273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible.
2211273d9f13SBarry Smith    We search for consecutive rows with the same nonzero structure, thereby
2212273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2213273d9f13SBarry Smith 
2214273d9f13SBarry Smith    Options Database Keys:
2215273d9f13SBarry Smith +  -mat_aij_no_inode  - Do not use inodes
2216273d9f13SBarry Smith .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2217273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2218273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2219273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2220273d9f13SBarry Smith 
2221273d9f13SBarry Smith 
2222273d9f13SBarry Smith    Example usage:
2223273d9f13SBarry Smith 
2224273d9f13SBarry Smith    Consider the following 8x8 matrix with 34 non-zero values, that is
2225273d9f13SBarry Smith    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
2226273d9f13SBarry Smith    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
2227273d9f13SBarry Smith    as follows:
2228273d9f13SBarry Smith 
2229273d9f13SBarry Smith .vb
2230273d9f13SBarry Smith             1  2  0  |  0  3  0  |  0  4
2231273d9f13SBarry Smith     Proc0   0  5  6  |  7  0  0  |  8  0
2232273d9f13SBarry Smith             9  0 10  | 11  0  0  | 12  0
2233273d9f13SBarry Smith     -------------------------------------
2234273d9f13SBarry Smith            13  0 14  | 15 16 17  |  0  0
2235273d9f13SBarry Smith     Proc1   0 18  0  | 19 20 21  |  0  0
2236273d9f13SBarry Smith             0  0  0  | 22 23  0  | 24  0
2237273d9f13SBarry Smith     -------------------------------------
2238273d9f13SBarry Smith     Proc2  25 26 27  |  0  0 28  | 29  0
2239273d9f13SBarry Smith            30  0  0  | 31 32 33  |  0 34
2240273d9f13SBarry Smith .ve
2241273d9f13SBarry Smith 
2242273d9f13SBarry Smith    This can be represented as a collection of submatrices as:
2243273d9f13SBarry Smith 
2244273d9f13SBarry Smith .vb
2245273d9f13SBarry Smith       A B C
2246273d9f13SBarry Smith       D E F
2247273d9f13SBarry Smith       G H I
2248273d9f13SBarry Smith .ve
2249273d9f13SBarry Smith 
2250273d9f13SBarry Smith    Where the submatrices A,B,C are owned by proc0, D,E,F are
2251273d9f13SBarry Smith    owned by proc1, G,H,I are owned by proc2.
2252273d9f13SBarry Smith 
2253273d9f13SBarry Smith    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2254273d9f13SBarry Smith    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2255273d9f13SBarry Smith    The 'M','N' parameters are 8,8, and have the same values on all procs.
2256273d9f13SBarry Smith 
2257273d9f13SBarry Smith    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
2258273d9f13SBarry Smith    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
2259273d9f13SBarry Smith    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
2260273d9f13SBarry Smith    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
2261273d9f13SBarry Smith    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
2262273d9f13SBarry Smith    matrix, ans [DF] as another SeqAIJ matrix.
2263273d9f13SBarry Smith 
2264273d9f13SBarry Smith    When d_nz, o_nz parameters are specified, d_nz storage elements are
2265273d9f13SBarry Smith    allocated for every row of the local diagonal submatrix, and o_nz
2266273d9f13SBarry Smith    storage locations are allocated for every row of the OFF-DIAGONAL submat.
2267273d9f13SBarry Smith    One way to choose d_nz and o_nz is to use the max nonzerors per local
2268273d9f13SBarry Smith    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
2269273d9f13SBarry Smith    In this case, the values of d_nz,o_nz are:
2270273d9f13SBarry Smith .vb
2271273d9f13SBarry Smith      proc0 : dnz = 2, o_nz = 2
2272273d9f13SBarry Smith      proc1 : dnz = 3, o_nz = 2
2273273d9f13SBarry Smith      proc2 : dnz = 1, o_nz = 4
2274273d9f13SBarry Smith .ve
2275273d9f13SBarry Smith    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
2276273d9f13SBarry Smith    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
2277273d9f13SBarry Smith    for proc3. i.e we are using 12+15+10=37 storage locations to store
2278273d9f13SBarry Smith    34 values.
2279273d9f13SBarry Smith 
2280273d9f13SBarry Smith    When d_nnz, o_nnz parameters are specified, the storage is specified
2281273d9f13SBarry Smith    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
2282273d9f13SBarry Smith    In the above case the values for d_nnz,o_nnz are:
2283273d9f13SBarry Smith .vb
2284273d9f13SBarry Smith      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
2285273d9f13SBarry Smith      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
2286273d9f13SBarry Smith      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
2287273d9f13SBarry Smith .ve
2288273d9f13SBarry Smith    Here the space allocated is sum of all the above values i.e 34, and
2289273d9f13SBarry Smith    hence pre-allocation is perfect.
2290273d9f13SBarry Smith 
2291273d9f13SBarry Smith    Level: intermediate
2292273d9f13SBarry Smith 
2293273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
2294273d9f13SBarry Smith 
2295273d9f13SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues()
2296273d9f13SBarry Smith @*/
2297273d9f13SBarry Smith int MatCreateMPIAIJ(MPI_Comm comm,int m,int n,int M,int N,int d_nz,int *d_nnz,int o_nz,int *o_nnz,Mat *A)
2298273d9f13SBarry Smith {
2299273d9f13SBarry Smith   int ierr,size;
2300273d9f13SBarry Smith 
2301273d9f13SBarry Smith   PetscFunctionBegin;
2302273d9f13SBarry Smith   ierr = MatCreate(comm,m,n,M,N,A);CHKERRQ(ierr);
2303273d9f13SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
2304273d9f13SBarry Smith   if (size > 1) {
2305273d9f13SBarry Smith     ierr = MatSetType(*A,MATMPIAIJ);CHKERRQ(ierr);
2306273d9f13SBarry Smith     ierr = MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr);
2307273d9f13SBarry Smith   } else {
2308273d9f13SBarry Smith     ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2309273d9f13SBarry Smith     ierr = MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);CHKERRQ(ierr);
2310273d9f13SBarry Smith   }
2311273d9f13SBarry Smith   PetscFunctionReturn(0);
2312273d9f13SBarry Smith }
2313195d93cdSBarry Smith 
23144a2ae208SSatish Balay #undef __FUNCT__
23154a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJGetSeqAIJ"
2316195d93cdSBarry Smith int MatMPIAIJGetSeqAIJ(Mat A,Mat *Ad,Mat *Ao,int **colmap)
2317195d93cdSBarry Smith {
2318195d93cdSBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;
2319195d93cdSBarry Smith   PetscFunctionBegin;
2320195d93cdSBarry Smith   *Ad     = a->A;
2321195d93cdSBarry Smith   *Ao     = a->B;
2322195d93cdSBarry Smith   *colmap = a->garray;
2323195d93cdSBarry Smith   PetscFunctionReturn(0);
2324195d93cdSBarry Smith }
2325a2243be0SBarry Smith 
2326a2243be0SBarry Smith #undef __FUNCT__
2327a2243be0SBarry Smith #define __FUNCT__ "MatSetColoring_MPIAIJ"
2328a2243be0SBarry Smith int MatSetColoring_MPIAIJ(Mat A,ISColoring coloring)
2329a2243be0SBarry Smith {
2330a2243be0SBarry Smith   int        ierr;
2331a2243be0SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2332a2243be0SBarry Smith 
2333a2243be0SBarry Smith   PetscFunctionBegin;
2334a2243be0SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
2335a2243be0SBarry Smith     int        *allcolors,*colors,i;
2336a2243be0SBarry Smith     ISColoring ocoloring;
2337a2243be0SBarry Smith 
2338a2243be0SBarry Smith     /* set coloring for diagonal portion */
2339a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->A,coloring);CHKERRQ(ierr);
2340a2243be0SBarry Smith 
2341a2243be0SBarry Smith     /* set coloring for off-diagonal portion */
2342a2243be0SBarry Smith     ierr = ISAllGatherIndices(A->comm,coloring->n,coloring->colors,PETSC_NULL,&allcolors);CHKERRQ(ierr);
2343a2243be0SBarry Smith     ierr = PetscMalloc((a->B->n+1)*sizeof(int),&colors);CHKERRQ(ierr);
2344a2243be0SBarry Smith     for (i=0; i<a->B->n; i++) {
2345a2243be0SBarry Smith       colors[i] = allcolors[a->garray[i]];
2346a2243be0SBarry Smith     }
2347a2243be0SBarry Smith     ierr = PetscFree(allcolors);CHKERRQ(ierr);
2348a2243be0SBarry Smith     ierr = ISColoringCreate(MPI_COMM_SELF,a->B->n,colors,&ocoloring);CHKERRQ(ierr);
2349a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr);
2350a2243be0SBarry Smith     ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr);
2351a2243be0SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
2352a2243be0SBarry Smith     int        *colors,i,*larray;
2353a2243be0SBarry Smith     ISColoring ocoloring;
2354a2243be0SBarry Smith 
2355a2243be0SBarry Smith     /* set coloring for diagonal portion */
2356a2243be0SBarry Smith     ierr = PetscMalloc((a->A->n+1)*sizeof(int),&larray);CHKERRQ(ierr);
2357a2243be0SBarry Smith     for (i=0; i<a->A->n; i++) {
2358a2243be0SBarry Smith       larray[i] = i + a->cstart;
2359a2243be0SBarry Smith     }
2360a2243be0SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->A->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
2361a2243be0SBarry Smith     ierr = PetscMalloc((a->A->n+1)*sizeof(int),&colors);CHKERRQ(ierr);
2362a2243be0SBarry Smith     for (i=0; i<a->A->n; i++) {
2363a2243be0SBarry Smith       colors[i] = coloring->colors[larray[i]];
2364a2243be0SBarry Smith     }
2365a2243be0SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
2366a2243be0SBarry Smith     ierr = ISColoringCreate(MPI_COMM_SELF,a->A->n,colors,&ocoloring);CHKERRQ(ierr);
2367a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->A,ocoloring);CHKERRQ(ierr);
2368a2243be0SBarry Smith     ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr);
2369a2243be0SBarry Smith 
2370a2243be0SBarry Smith     /* set coloring for off-diagonal portion */
2371a2243be0SBarry Smith     ierr = PetscMalloc((a->B->n+1)*sizeof(int),&larray);CHKERRQ(ierr);
2372a2243be0SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->B->n,a->garray,PETSC_NULL,larray);CHKERRQ(ierr);
2373a2243be0SBarry Smith     ierr = PetscMalloc((a->B->n+1)*sizeof(int),&colors);CHKERRQ(ierr);
2374a2243be0SBarry Smith     for (i=0; i<a->B->n; i++) {
2375a2243be0SBarry Smith       colors[i] = coloring->colors[larray[i]];
2376a2243be0SBarry Smith     }
2377a2243be0SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
2378a2243be0SBarry Smith     ierr = ISColoringCreate(MPI_COMM_SELF,a->B->n,colors,&ocoloring);CHKERRQ(ierr);
2379a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr);
2380a2243be0SBarry Smith     ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr);
2381a2243be0SBarry Smith   } else {
2382a2243be0SBarry Smith     SETERRQ1(1,"No support ISColoringType %d",coloring->ctype);
2383a2243be0SBarry Smith   }
2384a2243be0SBarry Smith 
2385a2243be0SBarry Smith   PetscFunctionReturn(0);
2386a2243be0SBarry Smith }
2387a2243be0SBarry Smith 
2388a2243be0SBarry Smith #undef __FUNCT__
2389779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdic_MPIAIJ"
2390779c1a83SBarry Smith int MatSetValuesAdic_MPIAIJ(Mat A,void *advalues)
2391a2243be0SBarry Smith {
2392a2243be0SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2393a2243be0SBarry Smith   int        ierr;
2394a2243be0SBarry Smith 
2395a2243be0SBarry Smith   PetscFunctionBegin;
2396779c1a83SBarry Smith   ierr = MatSetValuesAdic_SeqAIJ(a->A,advalues);CHKERRQ(ierr);
2397779c1a83SBarry Smith   ierr = MatSetValuesAdic_SeqAIJ(a->B,advalues);CHKERRQ(ierr);
2398779c1a83SBarry Smith   PetscFunctionReturn(0);
2399779c1a83SBarry Smith }
2400779c1a83SBarry Smith 
2401779c1a83SBarry Smith #undef __FUNCT__
2402779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdifor_MPIAIJ"
2403779c1a83SBarry Smith int MatSetValuesAdifor_MPIAIJ(Mat A,int nl,void *advalues)
2404779c1a83SBarry Smith {
2405779c1a83SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2406779c1a83SBarry Smith   int        ierr;
2407779c1a83SBarry Smith 
2408779c1a83SBarry Smith   PetscFunctionBegin;
2409779c1a83SBarry Smith   ierr = MatSetValuesAdifor_SeqAIJ(a->A,nl,advalues);CHKERRQ(ierr);
2410779c1a83SBarry Smith   ierr = MatSetValuesAdifor_SeqAIJ(a->B,nl,advalues);CHKERRQ(ierr);
2411a2243be0SBarry Smith   PetscFunctionReturn(0);
2412a2243be0SBarry Smith }
2413