xref: /petsc/src/mat/impls/aij/mpi/mpiaij.c (revision 12c028f9705c7f73d22859dd953ec6a759be3484)
1*12c028f9SKris Buschelman /*$Id: mpiaij.c,v 1.337 2001/06/03 02:51:04 bsmith Exp buschelm $*/
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);
9ca44d042SBarry Smith EXTERN int MatSetValues_SeqAIJ(Mat,int,int*,int,int*,Scalar*,InsertMode);
10ca44d042SBarry Smith EXTERN int MatGetRow_SeqAIJ(Mat,int,int*,int**,Scalar**);
11ca44d042SBarry Smith EXTERN int MatRestoreRow_SeqAIJ(Mat,int,int*,int**,Scalar**);
12ca44d042SBarry Smith EXTERN int MatPrintHelp_SeqAIJ(Mat);
13bc5ccf88SSatish Balay 
140f5bd95cSBarry Smith /*
150f5bd95cSBarry Smith   Local utility routine that creates a mapping from the global column
169e25ed09SBarry Smith number to the local number in the off-diagonal part of the local
170f5bd95cSBarry Smith storage of the matrix.  When PETSC_USE_CTABLE is used this is scalable at
180f5bd95cSBarry Smith a slightly higher hash table cost; without it it is not scalable (each processor
190f5bd95cSBarry Smith has an order N integer array but is fast to acess.
209e25ed09SBarry Smith */
214a2ae208SSatish Balay #undef __FUNCT__
224a2ae208SSatish Balay #define __FUNCT__ "CreateColmap_MPIAIJ_Private"
230a198c4cSBarry Smith int CreateColmap_MPIAIJ_Private(Mat mat)
249e25ed09SBarry Smith {
2544a69424SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
26273d9f13SBarry Smith   int        n = aij->B->n,i,ierr;
27dbb450caSBarry Smith 
283a40ed3dSBarry Smith   PetscFunctionBegin;
29aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
30273d9f13SBarry Smith   ierr = PetscTableCreate(n,&aij->colmap);CHKERRQ(ierr);
31b1fc9764SSatish Balay   for (i=0; i<n; i++){
320f5bd95cSBarry Smith     ierr = PetscTableAdd(aij->colmap,aij->garray[i]+1,i+1);CHKERRQ(ierr);
33b1fc9764SSatish Balay   }
34b1fc9764SSatish Balay #else
35b0a32e0cSBarry Smith   ierr = PetscMalloc((mat->N+1)*sizeof(int),&aij->colmap);CHKERRQ(ierr);
36b0a32e0cSBarry Smith   PetscLogObjectMemory(mat,mat->N*sizeof(int));
37273d9f13SBarry Smith   ierr = PetscMemzero(aij->colmap,mat->N*sizeof(int));CHKERRQ(ierr);
38905e6a2fSBarry Smith   for (i=0; i<n; i++) aij->colmap[aij->garray[i]] = i+1;
39b1fc9764SSatish Balay #endif
403a40ed3dSBarry Smith   PetscFunctionReturn(0);
419e25ed09SBarry Smith }
429e25ed09SBarry Smith 
430520107fSSatish Balay #define CHUNKSIZE   15
4430770e4dSSatish Balay #define MatSetValues_SeqAIJ_A_Private(row,col,value,addv) \
450520107fSSatish Balay { \
460520107fSSatish Balay  \
470520107fSSatish Balay     rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift; \
4830770e4dSSatish Balay     rmax = aimax[row]; nrow = ailen[row];  \
49f5e9677aSSatish Balay     col1 = col - shift; \
50f5e9677aSSatish Balay      \
51ba4e3ef2SSatish Balay     low = 0; high = nrow; \
52ba4e3ef2SSatish Balay     while (high-low > 5) { \
53ba4e3ef2SSatish Balay       t = (low+high)/2; \
54ba4e3ef2SSatish Balay       if (rp[t] > col) high = t; \
55ba4e3ef2SSatish Balay       else             low  = t; \
56ba4e3ef2SSatish Balay     } \
572335bdd2SSatish Balay       for (_i=low; _i<high; _i++) { \
58f5e9677aSSatish Balay         if (rp[_i] > col1) break; \
59f5e9677aSSatish Balay         if (rp[_i] == col1) { \
600520107fSSatish Balay           if (addv == ADD_VALUES) ap[_i] += value;   \
610520107fSSatish Balay           else                  ap[_i] = value; \
6230770e4dSSatish Balay           goto a_noinsert; \
630520107fSSatish Balay         } \
640520107fSSatish Balay       }  \
6589280ab3SLois Curfman McInnes       if (nonew == 1) goto a_noinsert; \
6629bbc08cSBarry Smith       else if (nonew == -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero into matrix"); \
670520107fSSatish Balay       if (nrow >= rmax) { \
680520107fSSatish Balay         /* there is no extra room in row, therefore enlarge */ \
69273d9f13SBarry Smith         int    new_nz = ai[am] + CHUNKSIZE,len,*new_i,*new_j; \
700520107fSSatish Balay         Scalar *new_a; \
710520107fSSatish Balay  \
7229bbc08cSBarry Smith         if (nonew == -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix"); \
7389280ab3SLois Curfman McInnes  \
740520107fSSatish Balay         /* malloc new storage space */ \
75273d9f13SBarry Smith         len     = new_nz*(sizeof(int)+sizeof(Scalar))+(am+1)*sizeof(int); \
76b0a32e0cSBarry Smith         ierr    = PetscMalloc(len,&new_a);CHKERRQ(ierr); \
770520107fSSatish Balay         new_j   = (int*)(new_a + new_nz); \
780520107fSSatish Balay         new_i   = new_j + new_nz; \
790520107fSSatish Balay  \
800520107fSSatish Balay         /* copy over old data into new slots */ \
810520107fSSatish Balay         for (ii=0; ii<row+1; ii++) {new_i[ii] = ai[ii];} \
82273d9f13SBarry Smith         for (ii=row+1; ii<am+1; ii++) {new_i[ii] = ai[ii]+CHUNKSIZE;} \
83549d3d68SSatish Balay         ierr = PetscMemcpy(new_j,aj,(ai[row]+nrow+shift)*sizeof(int));CHKERRQ(ierr); \
840520107fSSatish Balay         len = (new_nz - CHUNKSIZE - ai[row] - nrow - shift); \
85549d3d68SSatish Balay         ierr = PetscMemcpy(new_j+ai[row]+shift+nrow+CHUNKSIZE,aj+ai[row]+shift+nrow, \
86549d3d68SSatish Balay                                                            len*sizeof(int));CHKERRQ(ierr); \
87549d3d68SSatish Balay         ierr = PetscMemcpy(new_a,aa,(ai[row]+nrow+shift)*sizeof(Scalar));CHKERRQ(ierr); \
88549d3d68SSatish Balay         ierr = PetscMemcpy(new_a+ai[row]+shift+nrow+CHUNKSIZE,aa+ai[row]+shift+nrow, \
89549d3d68SSatish Balay                                                            len*sizeof(Scalar));CHKERRQ(ierr);  \
900520107fSSatish Balay         /* free up old matrix storage */ \
91f5e9677aSSatish Balay  \
92606d414cSSatish Balay         ierr = PetscFree(a->a);CHKERRQ(ierr);  \
93606d414cSSatish Balay         if (!a->singlemalloc) { \
94606d414cSSatish Balay            ierr = PetscFree(a->i);CHKERRQ(ierr); \
95606d414cSSatish Balay            ierr = PetscFree(a->j);CHKERRQ(ierr); \
96606d414cSSatish Balay         } \
970520107fSSatish Balay         aa = a->a = new_a; ai = a->i = new_i; aj = a->j = new_j;  \
987c922b88SBarry Smith         a->singlemalloc = PETSC_TRUE; \
990520107fSSatish Balay  \
1000520107fSSatish Balay         rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift; \
10130770e4dSSatish Balay         rmax = aimax[row] = aimax[row] + CHUNKSIZE; \
102b0a32e0cSBarry Smith         PetscLogObjectMemory(A,CHUNKSIZE*(sizeof(int) + sizeof(Scalar))); \
1030520107fSSatish Balay         a->maxnz += CHUNKSIZE; \
1040520107fSSatish Balay         a->reallocs++; \
1050520107fSSatish Balay       } \
1060520107fSSatish Balay       N = nrow++ - 1; a->nz++; \
1070520107fSSatish Balay       /* shift up all the later entries in this row */ \
1080520107fSSatish Balay       for (ii=N; ii>=_i; ii--) { \
1090520107fSSatish Balay         rp[ii+1] = rp[ii]; \
1100520107fSSatish Balay         ap[ii+1] = ap[ii]; \
1110520107fSSatish Balay       } \
112f5e9677aSSatish Balay       rp[_i] = col1;  \
1130520107fSSatish Balay       ap[_i] = value;  \
11430770e4dSSatish Balay       a_noinsert: ; \
1150520107fSSatish Balay       ailen[row] = nrow; \
1160520107fSSatish Balay }
1170a198c4cSBarry Smith 
11830770e4dSSatish Balay #define MatSetValues_SeqAIJ_B_Private(row,col,value,addv) \
11930770e4dSSatish Balay { \
12030770e4dSSatish Balay  \
12130770e4dSSatish Balay     rp   = bj + bi[row] + shift; ap = ba + bi[row] + shift; \
12230770e4dSSatish Balay     rmax = bimax[row]; nrow = bilen[row];  \
12330770e4dSSatish Balay     col1 = col - shift; \
12430770e4dSSatish Balay      \
125ba4e3ef2SSatish Balay     low = 0; high = nrow; \
126ba4e3ef2SSatish Balay     while (high-low > 5) { \
127ba4e3ef2SSatish Balay       t = (low+high)/2; \
128ba4e3ef2SSatish Balay       if (rp[t] > col) high = t; \
129ba4e3ef2SSatish Balay       else             low  = t; \
130ba4e3ef2SSatish Balay     } \
1312335bdd2SSatish Balay        for (_i=low; _i<high; _i++) { \
13230770e4dSSatish Balay         if (rp[_i] > col1) break; \
13330770e4dSSatish Balay         if (rp[_i] == col1) { \
13430770e4dSSatish Balay           if (addv == ADD_VALUES) ap[_i] += value;   \
13530770e4dSSatish Balay           else                  ap[_i] = value; \
13630770e4dSSatish Balay           goto b_noinsert; \
13730770e4dSSatish Balay         } \
13830770e4dSSatish Balay       }  \
13989280ab3SLois Curfman McInnes       if (nonew == 1) goto b_noinsert; \
14029bbc08cSBarry Smith       else if (nonew == -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero into matrix"); \
14130770e4dSSatish Balay       if (nrow >= rmax) { \
14230770e4dSSatish Balay         /* there is no extra room in row, therefore enlarge */ \
143273d9f13SBarry Smith         int    new_nz = bi[bm] + CHUNKSIZE,len,*new_i,*new_j; \
14430770e4dSSatish Balay         Scalar *new_a; \
14530770e4dSSatish Balay  \
14629bbc08cSBarry Smith         if (nonew == -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix"); \
14789280ab3SLois Curfman McInnes  \
14830770e4dSSatish Balay         /* malloc new storage space */ \
149273d9f13SBarry Smith         len     = new_nz*(sizeof(int)+sizeof(Scalar))+(bm+1)*sizeof(int); \
150b0a32e0cSBarry Smith         ierr    = PetscMalloc(len,&new_a);CHKERRQ(ierr); \
15130770e4dSSatish Balay         new_j   = (int*)(new_a + new_nz); \
15230770e4dSSatish Balay         new_i   = new_j + new_nz; \
15330770e4dSSatish Balay  \
15430770e4dSSatish Balay         /* copy over old data into new slots */ \
15530770e4dSSatish Balay         for (ii=0; ii<row+1; ii++) {new_i[ii] = bi[ii];} \
156273d9f13SBarry Smith         for (ii=row+1; ii<bm+1; ii++) {new_i[ii] = bi[ii]+CHUNKSIZE;} \
157549d3d68SSatish Balay         ierr = PetscMemcpy(new_j,bj,(bi[row]+nrow+shift)*sizeof(int));CHKERRQ(ierr); \
15830770e4dSSatish Balay         len = (new_nz - CHUNKSIZE - bi[row] - nrow - shift); \
159549d3d68SSatish Balay         ierr = PetscMemcpy(new_j+bi[row]+shift+nrow+CHUNKSIZE,bj+bi[row]+shift+nrow, \
160549d3d68SSatish Balay                                                            len*sizeof(int));CHKERRQ(ierr); \
161549d3d68SSatish Balay         ierr = PetscMemcpy(new_a,ba,(bi[row]+nrow+shift)*sizeof(Scalar));CHKERRQ(ierr); \
162549d3d68SSatish Balay         ierr = PetscMemcpy(new_a+bi[row]+shift+nrow+CHUNKSIZE,ba+bi[row]+shift+nrow, \
163549d3d68SSatish Balay                                                            len*sizeof(Scalar));CHKERRQ(ierr);  \
16430770e4dSSatish Balay         /* free up old matrix storage */ \
16530770e4dSSatish Balay  \
166606d414cSSatish Balay         ierr = PetscFree(b->a);CHKERRQ(ierr);  \
167606d414cSSatish Balay         if (!b->singlemalloc) { \
168606d414cSSatish Balay           ierr = PetscFree(b->i);CHKERRQ(ierr); \
169606d414cSSatish Balay           ierr = PetscFree(b->j);CHKERRQ(ierr); \
170606d414cSSatish Balay         } \
17174c639caSSatish Balay         ba = b->a = new_a; bi = b->i = new_i; bj = b->j = new_j;  \
1727c922b88SBarry Smith         b->singlemalloc = PETSC_TRUE; \
17330770e4dSSatish Balay  \
17430770e4dSSatish Balay         rp   = bj + bi[row] + shift; ap = ba + bi[row] + shift; \
17530770e4dSSatish Balay         rmax = bimax[row] = bimax[row] + CHUNKSIZE; \
176b0a32e0cSBarry Smith         PetscLogObjectMemory(B,CHUNKSIZE*(sizeof(int) + sizeof(Scalar))); \
17774c639caSSatish Balay         b->maxnz += CHUNKSIZE; \
17874c639caSSatish Balay         b->reallocs++; \
17930770e4dSSatish Balay       } \
18074c639caSSatish Balay       N = nrow++ - 1; b->nz++; \
18130770e4dSSatish Balay       /* shift up all the later entries in this row */ \
18230770e4dSSatish Balay       for (ii=N; ii>=_i; ii--) { \
18330770e4dSSatish Balay         rp[ii+1] = rp[ii]; \
18430770e4dSSatish Balay         ap[ii+1] = ap[ii]; \
18530770e4dSSatish Balay       } \
18630770e4dSSatish Balay       rp[_i] = col1;  \
18730770e4dSSatish Balay       ap[_i] = value;  \
18830770e4dSSatish Balay       b_noinsert: ; \
18930770e4dSSatish Balay       bilen[row] = nrow; \
19030770e4dSSatish Balay }
19130770e4dSSatish Balay 
1924a2ae208SSatish Balay #undef __FUNCT__
1934a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_MPIAIJ"
1948f6be9afSLois Curfman McInnes int MatSetValues_MPIAIJ(Mat mat,int m,int *im,int n,int *in,Scalar *v,InsertMode addv)
1958a729477SBarry Smith {
19644a69424SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
1974b0e389bSBarry Smith   Scalar     value;
1981eb62cbbSBarry Smith   int        ierr,i,j,rstart = aij->rstart,rend = aij->rend;
1991eb62cbbSBarry Smith   int        cstart = aij->cstart,cend = aij->cend,row,col;
200273d9f13SBarry Smith   PetscTruth roworiented = aij->roworiented;
2018a729477SBarry Smith 
2020520107fSSatish Balay   /* Some Variables required in the macro */
2034ee7247eSSatish Balay   Mat        A = aij->A;
2044ee7247eSSatish Balay   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
20530770e4dSSatish Balay   int        *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j;
20630770e4dSSatish Balay   Scalar     *aa = a->a;
207329f5518SBarry Smith   PetscTruth ignorezeroentries = (((a->ignorezeroentries)&&(addv==ADD_VALUES))?PETSC_TRUE:PETSC_FALSE);
20830770e4dSSatish Balay   Mat        B = aij->B;
20930770e4dSSatish Balay   Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
210273d9f13SBarry Smith   int        *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->m,am = aij->A->m;
21130770e4dSSatish Balay   Scalar     *ba = b->a;
21230770e4dSSatish Balay 
213ba4e3ef2SSatish Balay   int        *rp,ii,nrow,_i,rmax,N,col1,low,high,t;
21430770e4dSSatish Balay   int        nonew = a->nonew,shift = a->indexshift;
21530770e4dSSatish Balay   Scalar     *ap;
2164ee7247eSSatish Balay 
2173a40ed3dSBarry Smith   PetscFunctionBegin;
2188a729477SBarry Smith   for (i=0; i<m; i++) {
2195ef9f2a5SBarry Smith     if (im[i] < 0) continue;
220aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g)
221273d9f13SBarry Smith     if (im[i] >= mat->M) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
2220a198c4cSBarry Smith #endif
2234b0e389bSBarry Smith     if (im[i] >= rstart && im[i] < rend) {
2244b0e389bSBarry Smith       row = im[i] - rstart;
2251eb62cbbSBarry Smith       for (j=0; j<n; j++) {
2264b0e389bSBarry Smith         if (in[j] >= cstart && in[j] < cend){
2274b0e389bSBarry Smith           col = in[j] - cstart;
2284b0e389bSBarry Smith           if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
229329f5518SBarry Smith           if (ignorezeroentries && value == 0.0) continue;
23030770e4dSSatish Balay           MatSetValues_SeqAIJ_A_Private(row,col,value,addv);
2310520107fSSatish Balay           /* ierr = MatSetValues_SeqAIJ(aij->A,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */
232273d9f13SBarry Smith         } else if (in[j] < 0) continue;
233aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g)
234273d9f13SBarry Smith         else if (in[j] >= mat->N) {SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Column too large");}
2350a198c4cSBarry Smith #endif
2361eb62cbbSBarry Smith         else {
237227d817aSBarry Smith           if (mat->was_assembled) {
238905e6a2fSBarry Smith             if (!aij->colmap) {
239905e6a2fSBarry Smith               ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
240905e6a2fSBarry Smith             }
241aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
2420f5bd95cSBarry Smith             ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr);
243fa46199cSSatish Balay 	    col--;
244b1fc9764SSatish Balay #else
245905e6a2fSBarry Smith             col = aij->colmap[in[j]] - 1;
246b1fc9764SSatish Balay #endif
247ec8511deSBarry Smith             if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
2482493cbb0SBarry Smith               ierr = DisAssemble_MPIAIJ(mat);CHKERRQ(ierr);
2494b0e389bSBarry Smith               col =  in[j];
2509bf004c3SSatish Balay               /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
251f9508a3cSSatish Balay               B = aij->B;
252f9508a3cSSatish Balay               b = (Mat_SeqAIJ*)B->data;
253f9508a3cSSatish Balay               bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j;
254f9508a3cSSatish Balay               ba = b->a;
255d6dfbf8fSBarry Smith             }
256c48de900SBarry Smith           } else col = in[j];
2574b0e389bSBarry Smith           if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
258329f5518SBarry Smith           if (ignorezeroentries && value == 0.0) continue;
25930770e4dSSatish Balay           MatSetValues_SeqAIJ_B_Private(row,col,value,addv);
26030770e4dSSatish Balay           /* ierr = MatSetValues_SeqAIJ(aij->B,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */
2611eb62cbbSBarry Smith         }
2621eb62cbbSBarry Smith       }
2635ef9f2a5SBarry Smith     } else {
26490f02eecSBarry Smith       if (!aij->donotstash) {
265d36fbae8SSatish Balay         if (roworiented) {
266329f5518SBarry Smith           if (ignorezeroentries && v[i*n] == 0.0) continue;
2678798bf22SSatish Balay           ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n);CHKERRQ(ierr);
268d36fbae8SSatish Balay         } else {
269329f5518SBarry Smith           if (ignorezeroentries && v[i] == 0.0) continue;
2708798bf22SSatish Balay           ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m);CHKERRQ(ierr);
2714b0e389bSBarry Smith         }
2721eb62cbbSBarry Smith       }
2738a729477SBarry Smith     }
27490f02eecSBarry Smith   }
2753a40ed3dSBarry Smith   PetscFunctionReturn(0);
2768a729477SBarry Smith }
2778a729477SBarry Smith 
2784a2ae208SSatish Balay #undef __FUNCT__
2794a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_MPIAIJ"
2808f6be9afSLois Curfman McInnes int MatGetValues_MPIAIJ(Mat mat,int m,int *idxm,int n,int *idxn,Scalar *v)
281b49de8d1SLois Curfman McInnes {
282b49de8d1SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
283b49de8d1SLois Curfman McInnes   int        ierr,i,j,rstart = aij->rstart,rend = aij->rend;
284b49de8d1SLois Curfman McInnes   int        cstart = aij->cstart,cend = aij->cend,row,col;
285b49de8d1SLois Curfman McInnes 
2863a40ed3dSBarry Smith   PetscFunctionBegin;
287b49de8d1SLois Curfman McInnes   for (i=0; i<m; i++) {
28829bbc08cSBarry Smith     if (idxm[i] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Negative row");
289273d9f13SBarry Smith     if (idxm[i] >= mat->M) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
290b49de8d1SLois Curfman McInnes     if (idxm[i] >= rstart && idxm[i] < rend) {
291b49de8d1SLois Curfman McInnes       row = idxm[i] - rstart;
292b49de8d1SLois Curfman McInnes       for (j=0; j<n; j++) {
29329bbc08cSBarry Smith         if (idxn[j] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Negative column");
294273d9f13SBarry Smith         if (idxn[j] >= mat->N) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
295b49de8d1SLois Curfman McInnes         if (idxn[j] >= cstart && idxn[j] < cend){
296b49de8d1SLois Curfman McInnes           col = idxn[j] - cstart;
297b49de8d1SLois Curfman McInnes           ierr = MatGetValues(aij->A,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
298fa852ad4SSatish Balay         } else {
299905e6a2fSBarry Smith           if (!aij->colmap) {
300905e6a2fSBarry Smith             ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
301905e6a2fSBarry Smith           }
302aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
3030f5bd95cSBarry Smith           ierr = PetscTableFind(aij->colmap,idxn[j]+1,&col);CHKERRQ(ierr);
304fa46199cSSatish Balay           col --;
305b1fc9764SSatish Balay #else
306905e6a2fSBarry Smith           col = aij->colmap[idxn[j]] - 1;
307b1fc9764SSatish Balay #endif
308e60e1c95SSatish Balay           if ((col < 0) || (aij->garray[col] != idxn[j])) *(v+i*n+j) = 0.0;
309d9d09a02SSatish Balay           else {
310b49de8d1SLois Curfman McInnes             ierr = MatGetValues(aij->B,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
311b49de8d1SLois Curfman McInnes           }
312b49de8d1SLois Curfman McInnes         }
313b49de8d1SLois Curfman McInnes       }
314a8c6a408SBarry Smith     } else {
31529bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"Only local values currently supported");
316b49de8d1SLois Curfman McInnes     }
317b49de8d1SLois Curfman McInnes   }
3183a40ed3dSBarry Smith   PetscFunctionReturn(0);
319b49de8d1SLois Curfman McInnes }
320bc5ccf88SSatish Balay 
3214a2ae208SSatish Balay #undef __FUNCT__
3224a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyBegin_MPIAIJ"
323bc5ccf88SSatish Balay int MatAssemblyBegin_MPIAIJ(Mat mat,MatAssemblyType mode)
324bc5ccf88SSatish Balay {
325bc5ccf88SSatish Balay   Mat_MPIAIJ  *aij = (Mat_MPIAIJ*)mat->data;
326d36fbae8SSatish Balay   int         ierr,nstash,reallocs;
327bc5ccf88SSatish Balay   InsertMode  addv;
328bc5ccf88SSatish Balay 
329bc5ccf88SSatish Balay   PetscFunctionBegin;
330bc5ccf88SSatish Balay   if (aij->donotstash) {
331bc5ccf88SSatish Balay     PetscFunctionReturn(0);
332bc5ccf88SSatish Balay   }
333bc5ccf88SSatish Balay 
334bc5ccf88SSatish Balay   /* make sure all processors are either in INSERTMODE or ADDMODE */
335bc5ccf88SSatish Balay   ierr = MPI_Allreduce(&mat->insertmode,&addv,1,MPI_INT,MPI_BOR,mat->comm);CHKERRQ(ierr);
336bc5ccf88SSatish Balay   if (addv == (ADD_VALUES|INSERT_VALUES)) {
33729bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Some processors inserted others added");
338bc5ccf88SSatish Balay   }
339bc5ccf88SSatish Balay   mat->insertmode = addv; /* in case this processor had no cache */
340bc5ccf88SSatish Balay 
3418798bf22SSatish Balay   ierr = MatStashScatterBegin_Private(&mat->stash,aij->rowners);CHKERRQ(ierr);
3428798bf22SSatish Balay   ierr = MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);CHKERRQ(ierr);
343b0a32e0cSBarry Smith   PetscLogInfo(aij->A,"MatAssemblyBegin_MPIAIJ:Stash has %d entries, uses %d mallocs.\n",nstash,reallocs);
344bc5ccf88SSatish Balay   PetscFunctionReturn(0);
345bc5ccf88SSatish Balay }
346bc5ccf88SSatish Balay 
347bc5ccf88SSatish Balay 
3484a2ae208SSatish Balay #undef __FUNCT__
3494a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_MPIAIJ"
350bc5ccf88SSatish Balay int MatAssemblyEnd_MPIAIJ(Mat mat,MatAssemblyType mode)
351bc5ccf88SSatish Balay {
352bc5ccf88SSatish Balay   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
353a2d1c673SSatish Balay   int         i,j,rstart,ncols,n,ierr,flg;
354bc5ccf88SSatish Balay   int         *row,*col,other_disassembled;
355bc5ccf88SSatish Balay   Scalar      *val;
356bc5ccf88SSatish Balay   InsertMode  addv = mat->insertmode;
357bc5ccf88SSatish Balay 
358bc5ccf88SSatish Balay   PetscFunctionBegin;
359bc5ccf88SSatish Balay   if (!aij->donotstash) {
360a2d1c673SSatish Balay     while (1) {
3618798bf22SSatish Balay       ierr = MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);CHKERRQ(ierr);
362a2d1c673SSatish Balay       if (!flg) break;
363a2d1c673SSatish Balay 
364bc5ccf88SSatish Balay       for (i=0; i<n;) {
365bc5ccf88SSatish Balay         /* Now identify the consecutive vals belonging to the same row */
366bc5ccf88SSatish Balay         for (j=i,rstart=row[j]; j<n; j++) { if (row[j] != rstart) break; }
367bc5ccf88SSatish Balay         if (j < n) ncols = j-i;
368bc5ccf88SSatish Balay         else       ncols = n-i;
369bc5ccf88SSatish Balay         /* Now assemble all these values with a single function call */
370bc5ccf88SSatish Balay         ierr = MatSetValues_MPIAIJ(mat,1,row+i,ncols,col+i,val+i,addv);CHKERRQ(ierr);
371bc5ccf88SSatish Balay         i = j;
372bc5ccf88SSatish Balay       }
373bc5ccf88SSatish Balay     }
3748798bf22SSatish Balay     ierr = MatStashScatterEnd_Private(&mat->stash);CHKERRQ(ierr);
375bc5ccf88SSatish Balay   }
376bc5ccf88SSatish Balay 
377bc5ccf88SSatish Balay   ierr = MatAssemblyBegin(aij->A,mode);CHKERRQ(ierr);
378bc5ccf88SSatish Balay   ierr = MatAssemblyEnd(aij->A,mode);CHKERRQ(ierr);
379bc5ccf88SSatish Balay 
380bc5ccf88SSatish Balay   /* determine if any processor has disassembled, if so we must
381bc5ccf88SSatish Balay      also disassemble ourselfs, in order that we may reassemble. */
382bc5ccf88SSatish Balay   /*
383bc5ccf88SSatish Balay      if nonzero structure of submatrix B cannot change then we know that
384bc5ccf88SSatish Balay      no processor disassembled thus we can skip this stuff
385bc5ccf88SSatish Balay   */
386bc5ccf88SSatish Balay   if (!((Mat_SeqAIJ*)aij->B->data)->nonew)  {
387bc5ccf88SSatish Balay     ierr = MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPI_INT,MPI_PROD,mat->comm);CHKERRQ(ierr);
388bc5ccf88SSatish Balay     if (mat->was_assembled && !other_disassembled) {
389bc5ccf88SSatish Balay       ierr = DisAssemble_MPIAIJ(mat);CHKERRQ(ierr);
390bc5ccf88SSatish Balay     }
391bc5ccf88SSatish Balay   }
392bc5ccf88SSatish Balay 
393bc5ccf88SSatish Balay   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
394bc5ccf88SSatish Balay     ierr = MatSetUpMultiply_MPIAIJ(mat);CHKERRQ(ierr);
395bc5ccf88SSatish Balay   }
396bc5ccf88SSatish Balay   ierr = MatAssemblyBegin(aij->B,mode);CHKERRQ(ierr);
397bc5ccf88SSatish Balay   ierr = MatAssemblyEnd(aij->B,mode);CHKERRQ(ierr);
398bc5ccf88SSatish Balay 
399606d414cSSatish Balay   if (aij->rowvalues) {
400606d414cSSatish Balay     ierr = PetscFree(aij->rowvalues);CHKERRQ(ierr);
401606d414cSSatish Balay     aij->rowvalues = 0;
402606d414cSSatish Balay   }
403bc5ccf88SSatish Balay   PetscFunctionReturn(0);
404bc5ccf88SSatish Balay }
405bc5ccf88SSatish Balay 
4064a2ae208SSatish Balay #undef __FUNCT__
4074a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_MPIAIJ"
4088f6be9afSLois Curfman McInnes int MatZeroEntries_MPIAIJ(Mat A)
4091eb62cbbSBarry Smith {
41044a69424SLois Curfman McInnes   Mat_MPIAIJ *l = (Mat_MPIAIJ*)A->data;
411dbd7a890SLois Curfman McInnes   int        ierr;
4123a40ed3dSBarry Smith 
4133a40ed3dSBarry Smith   PetscFunctionBegin;
41478b31e54SBarry Smith   ierr = MatZeroEntries(l->A);CHKERRQ(ierr);
41578b31e54SBarry Smith   ierr = MatZeroEntries(l->B);CHKERRQ(ierr);
4163a40ed3dSBarry Smith   PetscFunctionReturn(0);
4171eb62cbbSBarry Smith }
4181eb62cbbSBarry Smith 
4194a2ae208SSatish Balay #undef __FUNCT__
4204a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_MPIAIJ"
4218f6be9afSLois Curfman McInnes int MatZeroRows_MPIAIJ(Mat A,IS is,Scalar *diag)
4221eb62cbbSBarry Smith {
42344a69424SLois Curfman McInnes   Mat_MPIAIJ     *l = (Mat_MPIAIJ*)A->data;
42417699dbbSLois Curfman McInnes   int            i,ierr,N,*rows,*owners = l->rowners,size = l->size;
42535d8aa7fSBarry Smith   int            *procs,*nprocs,j,idx,nsends,*work,row;
42617699dbbSLois Curfman McInnes   int            nmax,*svalues,*starts,*owner,nrecvs,rank = l->rank;
4275392566eSBarry Smith   int            *rvalues,tag = A->tag,count,base,slen,n,*source;
4286a5c57faSSatish Balay   int            *lens,imdex,*lrows,*values,rstart=l->rstart;
429d6dfbf8fSBarry Smith   MPI_Comm       comm = A->comm;
4301eb62cbbSBarry Smith   MPI_Request    *send_waits,*recv_waits;
4311eb62cbbSBarry Smith   MPI_Status     recv_status,*send_status;
4321eb62cbbSBarry Smith   IS             istmp;
43335d8aa7fSBarry Smith   PetscTruth     found;
4341eb62cbbSBarry Smith 
4353a40ed3dSBarry Smith   PetscFunctionBegin;
436e03a110bSBarry Smith   ierr = ISGetLocalSize(is,&N);CHKERRQ(ierr);
43778b31e54SBarry Smith   ierr = ISGetIndices(is,&rows);CHKERRQ(ierr);
4381eb62cbbSBarry Smith 
4391eb62cbbSBarry Smith   /*  first count number of contributors to each processor */
440b0a32e0cSBarry Smith   ierr = PetscMalloc(2*size*sizeof(int),&nprocs);CHKERRQ(ierr);
441549d3d68SSatish Balay   ierr   = PetscMemzero(nprocs,2*size*sizeof(int));CHKERRQ(ierr);
442549d3d68SSatish Balay   procs  = nprocs + size;
443b0a32e0cSBarry Smith   ierr = PetscMalloc((N+1)*sizeof(int),&owner);CHKERRQ(ierr); /* see note*/
4441eb62cbbSBarry Smith   for (i=0; i<N; i++) {
4451eb62cbbSBarry Smith     idx = rows[i];
44635d8aa7fSBarry Smith     found = PETSC_FALSE;
44717699dbbSLois Curfman McInnes     for (j=0; j<size; j++) {
4481eb62cbbSBarry Smith       if (idx >= owners[j] && idx < owners[j+1]) {
44935d8aa7fSBarry Smith         nprocs[j]++; procs[j] = 1; owner[i] = j; found = PETSC_TRUE; break;
4501eb62cbbSBarry Smith       }
4511eb62cbbSBarry Smith     }
45229bbc08cSBarry Smith     if (!found) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Index out of range");
4531eb62cbbSBarry Smith   }
45417699dbbSLois Curfman McInnes   nsends = 0;  for (i=0; i<size; i++) { nsends += procs[i];}
4551eb62cbbSBarry Smith 
4561eb62cbbSBarry Smith   /* inform other processors of number of messages and max length*/
457b0a32e0cSBarry Smith   ierr = PetscMalloc(2*size*sizeof(int),&work);CHKERRQ(ierr);
4586831982aSBarry Smith   ierr   = MPI_Allreduce(nprocs,work,2*size,MPI_INT,PetscMaxSum_Op,comm);CHKERRQ(ierr);
4596831982aSBarry Smith   nrecvs = work[size+rank];
46017699dbbSLois Curfman McInnes   nmax   = work[rank];
461606d414cSSatish Balay   ierr   = PetscFree(work);CHKERRQ(ierr);
4621eb62cbbSBarry Smith 
4631eb62cbbSBarry Smith   /* post receives:   */
464b0a32e0cSBarry Smith   ierr = PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(int),&rvalues);CHKERRQ(ierr);
465b0a32e0cSBarry Smith   ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr);
4661eb62cbbSBarry Smith   for (i=0; i<nrecvs; i++) {
467ca161407SBarry Smith     ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPI_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr);
4681eb62cbbSBarry Smith   }
4691eb62cbbSBarry Smith 
4701eb62cbbSBarry Smith   /* do sends:
4711eb62cbbSBarry Smith       1) starts[i] gives the starting index in svalues for stuff going to
4721eb62cbbSBarry Smith          the ith processor
4731eb62cbbSBarry Smith   */
474b0a32e0cSBarry Smith   ierr = PetscMalloc((N+1)*sizeof(int),&svalues);CHKERRQ(ierr);
475b0a32e0cSBarry Smith   ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr);
476b0a32e0cSBarry Smith   ierr = PetscMalloc((size+1)*sizeof(int),&starts);CHKERRQ(ierr);
4771eb62cbbSBarry Smith   starts[0] = 0;
47817699dbbSLois Curfman McInnes   for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[i-1];}
4791eb62cbbSBarry Smith   for (i=0; i<N; i++) {
4801eb62cbbSBarry Smith     svalues[starts[owner[i]]++] = rows[i];
4811eb62cbbSBarry Smith   }
4826831982aSBarry Smith   ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr);
4831eb62cbbSBarry Smith 
4841eb62cbbSBarry Smith   starts[0] = 0;
48517699dbbSLois Curfman McInnes   for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[i-1];}
4861eb62cbbSBarry Smith   count = 0;
48717699dbbSLois Curfman McInnes   for (i=0; i<size; i++) {
4881eb62cbbSBarry Smith     if (procs[i]) {
489ca161407SBarry Smith       ierr = MPI_Isend(svalues+starts[i],nprocs[i],MPI_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr);
4901eb62cbbSBarry Smith     }
4911eb62cbbSBarry Smith   }
492606d414cSSatish Balay   ierr = PetscFree(starts);CHKERRQ(ierr);
4931eb62cbbSBarry Smith 
49417699dbbSLois Curfman McInnes   base = owners[rank];
4951eb62cbbSBarry Smith 
4961eb62cbbSBarry Smith   /*  wait on receives */
497b0a32e0cSBarry Smith   ierr   = PetscMalloc(2*(nrecvs+1)*sizeof(int),&lens);CHKERRQ(ierr);
4981eb62cbbSBarry Smith   source = lens + nrecvs;
4991eb62cbbSBarry Smith   count  = nrecvs; slen = 0;
5001eb62cbbSBarry Smith   while (count) {
501ca161407SBarry Smith     ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
5021eb62cbbSBarry Smith     /* unpack receives into our local space */
503ca161407SBarry Smith     ierr = MPI_Get_count(&recv_status,MPI_INT,&n);CHKERRQ(ierr);
504d6dfbf8fSBarry Smith     source[imdex]  = recv_status.MPI_SOURCE;
505d6dfbf8fSBarry Smith     lens[imdex]    = n;
5061eb62cbbSBarry Smith     slen          += n;
5071eb62cbbSBarry Smith     count--;
5081eb62cbbSBarry Smith   }
509606d414cSSatish Balay   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
5101eb62cbbSBarry Smith 
5111eb62cbbSBarry Smith   /* move the data into the send scatter */
512b0a32e0cSBarry Smith   ierr = PetscMalloc((slen+1)*sizeof(int),&lrows);CHKERRQ(ierr);
5131eb62cbbSBarry Smith   count = 0;
5141eb62cbbSBarry Smith   for (i=0; i<nrecvs; i++) {
5151eb62cbbSBarry Smith     values = rvalues + i*nmax;
5161eb62cbbSBarry Smith     for (j=0; j<lens[i]; j++) {
5171eb62cbbSBarry Smith       lrows[count++] = values[j] - base;
5181eb62cbbSBarry Smith     }
5191eb62cbbSBarry Smith   }
520606d414cSSatish Balay   ierr = PetscFree(rvalues);CHKERRQ(ierr);
521606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
522606d414cSSatish Balay   ierr = PetscFree(owner);CHKERRQ(ierr);
523606d414cSSatish Balay   ierr = PetscFree(nprocs);CHKERRQ(ierr);
5241eb62cbbSBarry Smith 
5251eb62cbbSBarry Smith   /* actually zap the local rows */
526029af93fSBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,slen,lrows,&istmp);CHKERRQ(ierr);
527b0a32e0cSBarry Smith   PetscLogObjectParent(A,istmp);
5286a5c57faSSatish Balay 
5296eb55b6aSBarry Smith   /*
5306eb55b6aSBarry Smith         Zero the required rows. If the "diagonal block" of the matrix
5316eb55b6aSBarry Smith      is square and the user wishes to set the diagonal we use seperate
5326eb55b6aSBarry Smith      code so that MatSetValues() is not called for each diagonal allocating
5336eb55b6aSBarry Smith      new memory, thus calling lots of mallocs and slowing things down.
5346eb55b6aSBarry Smith 
5356eb55b6aSBarry Smith        Contributed by: Mathew Knepley
5366eb55b6aSBarry Smith   */
537e2d53e46SBarry Smith   /* must zero l->B before l->A because the (diag) case below may put values into l->B*/
538e2d53e46SBarry Smith   ierr = MatZeroRows(l->B,istmp,0);CHKERRQ(ierr);
5396eb55b6aSBarry Smith   if (diag && (l->A->M == l->A->N)) {
5406eb55b6aSBarry Smith     ierr      = MatZeroRows(l->A,istmp,diag);CHKERRQ(ierr);
541e2d53e46SBarry Smith   } else if (diag) {
542e2d53e46SBarry Smith     ierr = MatZeroRows(l->A,istmp,0);CHKERRQ(ierr);
543fa46199cSSatish Balay     if (((Mat_SeqAIJ*)l->A->data)->nonew) {
54429bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"MatZeroRows() on rectangular matrices cannot be used with the Mat options\n\
545fa46199cSSatish Balay MAT_NO_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR");
5466525c446SSatish Balay     }
547e2d53e46SBarry Smith     for (i = 0; i < slen; i++) {
548e2d53e46SBarry Smith       row  = lrows[i] + rstart;
549e2d53e46SBarry Smith       ierr = MatSetValues(A,1,&row,1,&row,diag,INSERT_VALUES);CHKERRQ(ierr);
550e2d53e46SBarry Smith     }
551e2d53e46SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
552e2d53e46SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
5536eb55b6aSBarry Smith   } else {
5546a5c57faSSatish Balay     ierr = MatZeroRows(l->A,istmp,0);CHKERRQ(ierr);
5556eb55b6aSBarry Smith   }
55678b31e54SBarry Smith   ierr = ISDestroy(istmp);CHKERRQ(ierr);
557606d414cSSatish Balay   ierr = PetscFree(lrows);CHKERRQ(ierr);
55872dacd9aSBarry Smith 
5591eb62cbbSBarry Smith   /* wait on sends */
5601eb62cbbSBarry Smith   if (nsends) {
561b0a32e0cSBarry Smith     ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr);
562ca161407SBarry Smith     ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr);
563606d414cSSatish Balay     ierr = PetscFree(send_status);CHKERRQ(ierr);
5641eb62cbbSBarry Smith   }
565606d414cSSatish Balay   ierr = PetscFree(send_waits);CHKERRQ(ierr);
566606d414cSSatish Balay   ierr = PetscFree(svalues);CHKERRQ(ierr);
5671eb62cbbSBarry Smith 
5683a40ed3dSBarry Smith   PetscFunctionReturn(0);
5691eb62cbbSBarry Smith }
5701eb62cbbSBarry Smith 
5714a2ae208SSatish Balay #undef __FUNCT__
5724a2ae208SSatish Balay #define __FUNCT__ "MatMult_MPIAIJ"
5738f6be9afSLois Curfman McInnes int MatMult_MPIAIJ(Mat A,Vec xx,Vec yy)
5741eb62cbbSBarry Smith {
575416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
576fbd6ef76SBarry Smith   int        ierr,nt;
577416022c9SBarry Smith 
5783a40ed3dSBarry Smith   PetscFunctionBegin;
579a2ce50c7SBarry Smith   ierr = VecGetLocalSize(xx,&nt);CHKERRQ(ierr);
580273d9f13SBarry Smith   if (nt != A->n) {
581273d9f13SBarry Smith     SETERRQ2(PETSC_ERR_ARG_SIZ,"Incompatible partition of A (%d) and xx (%d)",A->n,nt);
582fbd6ef76SBarry Smith   }
58343a90d84SBarry Smith   ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
584f830108cSBarry Smith   ierr = (*a->A->ops->mult)(a->A,xx,yy);CHKERRQ(ierr);
58543a90d84SBarry Smith   ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
586f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);CHKERRQ(ierr);
5873a40ed3dSBarry Smith   PetscFunctionReturn(0);
5881eb62cbbSBarry Smith }
5891eb62cbbSBarry Smith 
5904a2ae208SSatish Balay #undef __FUNCT__
5914a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_MPIAIJ"
5928f6be9afSLois Curfman McInnes int MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
593da3a660dSBarry Smith {
594416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
595da3a660dSBarry Smith   int        ierr;
5963a40ed3dSBarry Smith 
5973a40ed3dSBarry Smith   PetscFunctionBegin;
59843a90d84SBarry Smith   ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
599f830108cSBarry Smith   ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
60043a90d84SBarry Smith   ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
601f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);CHKERRQ(ierr);
6023a40ed3dSBarry Smith   PetscFunctionReturn(0);
603da3a660dSBarry Smith }
604da3a660dSBarry Smith 
6054a2ae208SSatish Balay #undef __FUNCT__
6064a2ae208SSatish Balay #define __FUNCT__ "MatMultTranspose_MPIAIJ"
6077c922b88SBarry Smith int MatMultTranspose_MPIAIJ(Mat A,Vec xx,Vec yy)
608da3a660dSBarry Smith {
609416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
610da3a660dSBarry Smith   int        ierr;
611da3a660dSBarry Smith 
6123a40ed3dSBarry Smith   PetscFunctionBegin;
613da3a660dSBarry Smith   /* do nondiagonal part */
6147c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
615da3a660dSBarry Smith   /* send it on its way */
616537820f0SBarry Smith   ierr = VecScatterBegin(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
617da3a660dSBarry Smith   /* do local part */
6187c922b88SBarry Smith   ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr);
619da3a660dSBarry Smith   /* receive remote parts: note this assumes the values are not actually */
620da3a660dSBarry Smith   /* inserted in yy until the next line, which is true for my implementation*/
621da3a660dSBarry Smith   /* but is not perhaps always true. */
622537820f0SBarry Smith   ierr = VecScatterEnd(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
6233a40ed3dSBarry Smith   PetscFunctionReturn(0);
624da3a660dSBarry Smith }
625da3a660dSBarry Smith 
6264a2ae208SSatish Balay #undef __FUNCT__
6274a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_MPIAIJ"
6287c922b88SBarry Smith int MatMultTransposeAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
629da3a660dSBarry Smith {
630416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
631da3a660dSBarry Smith   int        ierr;
632da3a660dSBarry Smith 
6333a40ed3dSBarry Smith   PetscFunctionBegin;
634da3a660dSBarry Smith   /* do nondiagonal part */
6357c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
636da3a660dSBarry Smith   /* send it on its way */
637537820f0SBarry Smith   ierr = VecScatterBegin(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
638da3a660dSBarry Smith   /* do local part */
6397c922b88SBarry Smith   ierr = (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
640da3a660dSBarry Smith   /* receive remote parts: note this assumes the values are not actually */
641da3a660dSBarry Smith   /* inserted in yy until the next line, which is true for my implementation*/
642da3a660dSBarry Smith   /* but is not perhaps always true. */
6430a198c4cSBarry Smith   ierr = VecScatterEnd(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
6443a40ed3dSBarry Smith   PetscFunctionReturn(0);
645da3a660dSBarry Smith }
646da3a660dSBarry Smith 
6471eb62cbbSBarry Smith /*
6481eb62cbbSBarry Smith   This only works correctly for square matrices where the subblock A->A is the
6491eb62cbbSBarry Smith    diagonal block
6501eb62cbbSBarry Smith */
6514a2ae208SSatish Balay #undef __FUNCT__
6524a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_MPIAIJ"
6538f6be9afSLois Curfman McInnes int MatGetDiagonal_MPIAIJ(Mat A,Vec v)
6541eb62cbbSBarry Smith {
6553a40ed3dSBarry Smith   int        ierr;
656416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
6573a40ed3dSBarry Smith 
6583a40ed3dSBarry Smith   PetscFunctionBegin;
659273d9f13SBarry Smith   if (A->M != A->N) SETERRQ(PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block");
6605baf8537SBarry Smith   if (a->rstart != a->cstart || a->rend != a->cend) {
66129bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_SIZ,"row partition must equal col partition");
6623a40ed3dSBarry Smith   }
6633a40ed3dSBarry Smith   ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr);
6643a40ed3dSBarry Smith   PetscFunctionReturn(0);
6651eb62cbbSBarry Smith }
6661eb62cbbSBarry Smith 
6674a2ae208SSatish Balay #undef __FUNCT__
6684a2ae208SSatish Balay #define __FUNCT__ "MatScale_MPIAIJ"
6698f6be9afSLois Curfman McInnes int MatScale_MPIAIJ(Scalar *aa,Mat A)
670052efed2SBarry Smith {
671052efed2SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
672052efed2SBarry Smith   int        ierr;
6733a40ed3dSBarry Smith 
6743a40ed3dSBarry Smith   PetscFunctionBegin;
675052efed2SBarry Smith   ierr = MatScale(aa,a->A);CHKERRQ(ierr);
676052efed2SBarry Smith   ierr = MatScale(aa,a->B);CHKERRQ(ierr);
6773a40ed3dSBarry Smith   PetscFunctionReturn(0);
678052efed2SBarry Smith }
679052efed2SBarry Smith 
6804a2ae208SSatish Balay #undef __FUNCT__
6814a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_MPIAIJ"
682e1311b90SBarry Smith int MatDestroy_MPIAIJ(Mat mat)
6831eb62cbbSBarry Smith {
68444a69424SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
6851eb62cbbSBarry Smith   int        ierr;
68683e2fdc7SBarry Smith 
6873a40ed3dSBarry Smith   PetscFunctionBegin;
688aa482453SBarry Smith #if defined(PETSC_USE_LOG)
689b0a32e0cSBarry Smith   PetscLogObjectState((PetscObject)mat,"Rows=%d, Cols=%d",mat->M,mat->N);
690a5a9c739SBarry Smith #endif
6918798bf22SSatish Balay   ierr = MatStashDestroy_Private(&mat->stash);CHKERRQ(ierr);
692606d414cSSatish Balay   ierr = PetscFree(aij->rowners);CHKERRQ(ierr);
69378b31e54SBarry Smith   ierr = MatDestroy(aij->A);CHKERRQ(ierr);
69478b31e54SBarry Smith   ierr = MatDestroy(aij->B);CHKERRQ(ierr);
695aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
6960f5bd95cSBarry Smith   if (aij->colmap) {ierr = PetscTableDelete(aij->colmap);CHKERRQ(ierr);}
697b1fc9764SSatish Balay #else
698606d414cSSatish Balay   if (aij->colmap) {ierr = PetscFree(aij->colmap);CHKERRQ(ierr);}
699b1fc9764SSatish Balay #endif
700606d414cSSatish Balay   if (aij->garray) {ierr = PetscFree(aij->garray);CHKERRQ(ierr);}
7017c922b88SBarry Smith   if (aij->lvec)   {ierr = VecDestroy(aij->lvec);CHKERRQ(ierr);}
7027c922b88SBarry Smith   if (aij->Mvctx)  {ierr = VecScatterDestroy(aij->Mvctx);CHKERRQ(ierr);}
703606d414cSSatish Balay   if (aij->rowvalues) {ierr = PetscFree(aij->rowvalues);CHKERRQ(ierr);}
704606d414cSSatish Balay   ierr = PetscFree(aij);CHKERRQ(ierr);
7053a40ed3dSBarry Smith   PetscFunctionReturn(0);
7061eb62cbbSBarry Smith }
707ee50ffe9SBarry Smith 
7084a2ae208SSatish Balay #undef __FUNCT__
7094a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ_ASCIIorDraworSocket"
710b0a32e0cSBarry Smith int MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
711416022c9SBarry Smith {
71244a69424SLois Curfman McInnes   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*)mat->data;
713dbb450caSBarry Smith   Mat_SeqAIJ*       C = (Mat_SeqAIJ*)aij->A->data;
714fb9695e5SSatish Balay   int               ierr,shift = C->indexshift,rank = aij->rank,size = aij->size;
715f1af5d2fSBarry Smith   PetscTruth        isdraw,isascii,flg;
716b0a32e0cSBarry Smith   PetscViewer       sviewer;
717f3ef73ceSBarry Smith   PetscViewerFormat format;
718416022c9SBarry Smith 
7193a40ed3dSBarry Smith   PetscFunctionBegin;
720fb9695e5SSatish Balay   ierr  = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
721b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
7220f5bd95cSBarry Smith   if (isascii) {
723b0a32e0cSBarry Smith     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
724fb9695e5SSatish Balay     if (format == PETSC_VIEWER_ASCII_INFO_LONG) {
7254e220ebcSLois Curfman McInnes       MatInfo info;
7261dab6e02SBarry Smith       ierr = MPI_Comm_rank(mat->comm,&rank);CHKERRQ(ierr);
727888f2ed8SSatish Balay       ierr = MatGetInfo(mat,MAT_LOCAL,&info);CHKERRQ(ierr);
728b0a32e0cSBarry Smith       ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_no_inode",&flg);CHKERRQ(ierr);
7296831982aSBarry Smith       if (flg) {
730b0a32e0cSBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %d nz %d nz alloced %d mem %d, not using I-node routines\n",
731273d9f13SBarry Smith 					      rank,mat->m,(int)info.nz_used,(int)info.nz_allocated,(int)info.memory);CHKERRQ(ierr);
7326831982aSBarry Smith       } else {
733b0a32e0cSBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %d nz %d nz alloced %d mem %d, using I-node routines\n",
734273d9f13SBarry Smith 		    rank,mat->m,(int)info.nz_used,(int)info.nz_allocated,(int)info.memory);CHKERRQ(ierr);
7356831982aSBarry Smith       }
736888f2ed8SSatish Balay       ierr = MatGetInfo(aij->A,MAT_LOCAL,&info);CHKERRQ(ierr);
737b0a32e0cSBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %d \n",rank,(int)info.nz_used);CHKERRQ(ierr);
738888f2ed8SSatish Balay       ierr = MatGetInfo(aij->B,MAT_LOCAL,&info);CHKERRQ(ierr);
739b0a32e0cSBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %d \n",rank,(int)info.nz_used);CHKERRQ(ierr);
740b0a32e0cSBarry Smith       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
741a40aa06bSLois Curfman McInnes       ierr = VecScatterView(aij->Mvctx,viewer);CHKERRQ(ierr);
7423a40ed3dSBarry Smith       PetscFunctionReturn(0);
743fb9695e5SSatish Balay     } else if (format == PETSC_VIEWER_ASCII_INFO) {
7443a40ed3dSBarry Smith       PetscFunctionReturn(0);
74508480c60SBarry Smith     }
7460f5bd95cSBarry Smith   } else if (isdraw) {
747b0a32e0cSBarry Smith     PetscDraw       draw;
74819bcc07fSBarry Smith     PetscTruth isnull;
749b0a32e0cSBarry Smith     ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
750b0a32e0cSBarry Smith     ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
75119bcc07fSBarry Smith   }
75219bcc07fSBarry Smith 
75317699dbbSLois Curfman McInnes   if (size == 1) {
754e36acaf3SBarry Smith     ierr = PetscObjectSetName((PetscObject)aij->A,mat->name);CHKERRQ(ierr);
75578b31e54SBarry Smith     ierr = MatView(aij->A,viewer);CHKERRQ(ierr);
7563a40ed3dSBarry Smith   } else {
75795373324SBarry Smith     /* assemble the entire matrix onto first processor. */
75895373324SBarry Smith     Mat         A;
759ec8511deSBarry Smith     Mat_SeqAIJ *Aloc;
760273d9f13SBarry Smith     int         M = mat->M,N = mat->N,m,*ai,*aj,row,*cols,i,*ct;
76195373324SBarry Smith     Scalar      *a;
7622ee70a88SLois Curfman McInnes 
76317699dbbSLois Curfman McInnes     if (!rank) {
76455843e3eSBarry Smith       ierr = MatCreateMPIAIJ(mat->comm,M,N,M,N,0,PETSC_NULL,0,PETSC_NULL,&A);CHKERRQ(ierr);
7653a40ed3dSBarry Smith     } else {
76655843e3eSBarry Smith       ierr = MatCreateMPIAIJ(mat->comm,0,0,M,N,0,PETSC_NULL,0,PETSC_NULL,&A);CHKERRQ(ierr);
76795373324SBarry Smith     }
768b0a32e0cSBarry Smith     PetscLogObjectParent(mat,A);
769416022c9SBarry Smith 
77095373324SBarry Smith     /* copy over the A part */
771ec8511deSBarry Smith     Aloc = (Mat_SeqAIJ*)aij->A->data;
772273d9f13SBarry Smith     m = aij->A->m; ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
77395373324SBarry Smith     row = aij->rstart;
774dbb450caSBarry Smith     for (i=0; i<ai[m]+shift; i++) {aj[i] += aij->cstart + shift;}
77595373324SBarry Smith     for (i=0; i<m; i++) {
776416022c9SBarry Smith       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);CHKERRQ(ierr);
77795373324SBarry Smith       row++; a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
77895373324SBarry Smith     }
7792ee70a88SLois Curfman McInnes     aj = Aloc->j;
780dbb450caSBarry Smith     for (i=0; i<ai[m]+shift; i++) {aj[i] -= aij->cstart + shift;}
78195373324SBarry Smith 
78295373324SBarry Smith     /* copy over the B part */
783ec8511deSBarry Smith     Aloc = (Mat_SeqAIJ*)aij->B->data;
784273d9f13SBarry Smith     m    = aij->B->m;  ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
78595373324SBarry Smith     row  = aij->rstart;
786b0a32e0cSBarry Smith     ierr = PetscMalloc((ai[m]+1)*sizeof(int),&cols);CHKERRQ(ierr);
787b0a32e0cSBarry Smith     ct   = cols;
788dbb450caSBarry Smith     for (i=0; i<ai[m]+shift; i++) {cols[i] = aij->garray[aj[i]+shift];}
78995373324SBarry Smith     for (i=0; i<m; i++) {
790416022c9SBarry Smith       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);CHKERRQ(ierr);
79195373324SBarry Smith       row++; a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
79295373324SBarry Smith     }
793606d414cSSatish Balay     ierr = PetscFree(ct);CHKERRQ(ierr);
7946d4a8577SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7956d4a8577SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
79655843e3eSBarry Smith     /*
79755843e3eSBarry Smith        Everyone has to call to draw the matrix since the graphics waits are
798b0a32e0cSBarry Smith        synchronized across all processors that share the PetscDraw object
79955843e3eSBarry Smith     */
800b0a32e0cSBarry Smith     ierr = PetscViewerGetSingleton(viewer,&sviewer);CHKERRQ(ierr);
801e03a110bSBarry Smith     if (!rank) {
802e36acaf3SBarry Smith       ierr = PetscObjectSetName((PetscObject)((Mat_MPIAIJ*)(A->data))->A,mat->name);CHKERRQ(ierr);
8036831982aSBarry Smith       ierr = MatView(((Mat_MPIAIJ*)(A->data))->A,sviewer);CHKERRQ(ierr);
80495373324SBarry Smith     }
805b0a32e0cSBarry Smith     ierr = PetscViewerRestoreSingleton(viewer,&sviewer);CHKERRQ(ierr);
80678b31e54SBarry Smith     ierr = MatDestroy(A);CHKERRQ(ierr);
80795373324SBarry Smith   }
8083a40ed3dSBarry Smith   PetscFunctionReturn(0);
8091eb62cbbSBarry Smith }
8101eb62cbbSBarry Smith 
8114a2ae208SSatish Balay #undef __FUNCT__
8124a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ"
813b0a32e0cSBarry Smith int MatView_MPIAIJ(Mat mat,PetscViewer viewer)
814416022c9SBarry Smith {
815416022c9SBarry Smith   int        ierr;
8166831982aSBarry Smith   PetscTruth isascii,isdraw,issocket,isbinary;
817416022c9SBarry Smith 
8183a40ed3dSBarry Smith   PetscFunctionBegin;
819b0a32e0cSBarry Smith   ierr  = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
820fb9695e5SSatish Balay   ierr  = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
821fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
822b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_SOCKET,&issocket);CHKERRQ(ierr);
8230f5bd95cSBarry Smith   if (isascii || isdraw || isbinary || issocket) {
8247b2a1423SBarry Smith     ierr = MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer);CHKERRQ(ierr);
8255cd90555SBarry Smith   } else {
82629bbc08cSBarry Smith     SETERRQ1(1,"Viewer type %s not supported by MPIAIJ matrices",((PetscObject)viewer)->type_name);
827416022c9SBarry Smith   }
8283a40ed3dSBarry Smith   PetscFunctionReturn(0);
829416022c9SBarry Smith }
830416022c9SBarry Smith 
8311eb62cbbSBarry Smith /*
8321eb62cbbSBarry Smith     This has to provide several versions.
8331eb62cbbSBarry Smith 
8341eb62cbbSBarry Smith      2) a) use only local smoothing updating outer values only once.
8351eb62cbbSBarry Smith         b) local smoothing updating outer values each inner iteration
836d6dfbf8fSBarry Smith      3) color updating out values betwen colors.
8371eb62cbbSBarry Smith */
8384a2ae208SSatish Balay #undef __FUNCT__
8394a2ae208SSatish Balay #define __FUNCT__ "MatRelax_MPIAIJ"
840273d9f13SBarry Smith int MatRelax_MPIAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,int its,Vec xx)
8418a729477SBarry Smith {
84244a69424SLois Curfman McInnes   Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data;
843d6dfbf8fSBarry Smith   Mat        AA = mat->A,BB = mat->B;
844ec8511deSBarry Smith   Mat_SeqAIJ *A = (Mat_SeqAIJ*)AA->data,*B = (Mat_SeqAIJ *)BB->data;
845c16cb8f2SBarry Smith   Scalar     *b,*x,*xs,*ls,d,*v,sum;
8466abc6512SBarry Smith   int        ierr,*idx,*diag;
847273d9f13SBarry Smith   int        n = matin->n,m = matin->m,i,shift = A->indexshift;
8488a729477SBarry Smith 
8493a40ed3dSBarry Smith   PetscFunctionBegin;
8507c922b88SBarry Smith   if (!A->diag) {ierr = MatMarkDiagonal_SeqAIJ(AA);CHKERRQ(ierr);}
851d6dfbf8fSBarry Smith   diag = A->diag;
852c16cb8f2SBarry Smith   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){
853da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
854f830108cSBarry Smith       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,its,xx);CHKERRQ(ierr);
8553a40ed3dSBarry Smith       PetscFunctionReturn(0);
856da3a660dSBarry Smith     }
8573a40ed3dSBarry Smith     ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
8583a40ed3dSBarry Smith     ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
859184914b5SBarry Smith     ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
860184914b5SBarry Smith     if (xx != bb) {
861184914b5SBarry Smith       ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
862184914b5SBarry Smith     } else {
863184914b5SBarry Smith       b = x;
864184914b5SBarry Smith     }
865184914b5SBarry Smith     ierr = VecGetArray(mat->lvec,&ls);CHKERRQ(ierr);
866184914b5SBarry Smith     xs = x + shift; /* shift by one for index start of 1 */
867184914b5SBarry Smith     ls = ls + shift;
868d6dfbf8fSBarry Smith     while (its--) {
869d6dfbf8fSBarry Smith       /* go down through the rows */
870d6dfbf8fSBarry Smith       for (i=0; i<m; i++) {
871d6dfbf8fSBarry Smith         n    = A->i[i+1] - A->i[i];
872b0a32e0cSBarry Smith 	PetscLogFlops(4*n+3);
873dbb450caSBarry Smith         idx  = A->j + A->i[i] + shift;
874dbb450caSBarry Smith         v    = A->a + A->i[i] + shift;
875d6dfbf8fSBarry Smith         sum  = b[i];
876d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
877dbb450caSBarry Smith         d    = fshift + A->a[diag[i]+shift];
878d6dfbf8fSBarry Smith         n    = B->i[i+1] - B->i[i];
879dbb450caSBarry Smith         idx  = B->j + B->i[i] + shift;
880dbb450caSBarry Smith         v    = B->a + B->i[i] + shift;
881d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,ls,v,idx,n);
88255a1b374SBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + A->a[diag[i]+shift]*x[i])/d;
883d6dfbf8fSBarry Smith       }
884d6dfbf8fSBarry Smith       /* come up through the rows */
885d6dfbf8fSBarry Smith       for (i=m-1; i>-1; i--) {
886d6dfbf8fSBarry Smith         n    = A->i[i+1] - A->i[i];
887b0a32e0cSBarry Smith 	PetscLogFlops(4*n+3);
888dbb450caSBarry Smith         idx  = A->j + A->i[i] + shift;
889dbb450caSBarry Smith         v    = A->a + A->i[i] + shift;
890d6dfbf8fSBarry Smith         sum  = b[i];
891d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
892dbb450caSBarry Smith         d    = fshift + A->a[diag[i]+shift];
893d6dfbf8fSBarry Smith         n    = B->i[i+1] - B->i[i];
894dbb450caSBarry Smith         idx  = B->j + B->i[i] + shift;
895dbb450caSBarry Smith         v    = B->a + B->i[i] + shift;
896d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,ls,v,idx,n);
89755a1b374SBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + A->a[diag[i]+shift]*x[i])/d;
898d6dfbf8fSBarry Smith       }
899d6dfbf8fSBarry Smith     }
900184914b5SBarry Smith     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
901184914b5SBarry Smith     if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr); }
902184914b5SBarry Smith     ierr = VecRestoreArray(mat->lvec,&ls);CHKERRQ(ierr);
9033a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_FORWARD_SWEEP){
904da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
905f830108cSBarry Smith       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,its,xx);CHKERRQ(ierr);
9063a40ed3dSBarry Smith       PetscFunctionReturn(0);
907da3a660dSBarry Smith     }
9083a40ed3dSBarry Smith     ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
9093a40ed3dSBarry Smith     ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
910184914b5SBarry Smith     ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
911184914b5SBarry Smith     if (xx != bb) {
912184914b5SBarry Smith       ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
913184914b5SBarry Smith     } else {
914184914b5SBarry Smith       b = x;
915184914b5SBarry Smith     }
916184914b5SBarry Smith     ierr = VecGetArray(mat->lvec,&ls);CHKERRQ(ierr);
917184914b5SBarry Smith     xs = x + shift; /* shift by one for index start of 1 */
918184914b5SBarry Smith     ls = ls + shift;
919d6dfbf8fSBarry Smith     while (its--) {
920d6dfbf8fSBarry Smith       for (i=0; i<m; i++) {
921d6dfbf8fSBarry Smith         n    = A->i[i+1] - A->i[i];
922b0a32e0cSBarry Smith 	PetscLogFlops(4*n+3);
923dbb450caSBarry Smith         idx  = A->j + A->i[i] + shift;
924dbb450caSBarry Smith         v    = A->a + A->i[i] + shift;
925d6dfbf8fSBarry Smith         sum  = b[i];
926d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
927dbb450caSBarry Smith         d    = fshift + A->a[diag[i]+shift];
928d6dfbf8fSBarry Smith         n    = B->i[i+1] - B->i[i];
929dbb450caSBarry Smith         idx  = B->j + B->i[i] + shift;
930dbb450caSBarry Smith         v    = B->a + B->i[i] + shift;
931d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,ls,v,idx,n);
93255a1b374SBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + A->a[diag[i]+shift]*x[i])/d;
933d6dfbf8fSBarry Smith       }
934d6dfbf8fSBarry Smith     }
935184914b5SBarry Smith     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
936184914b5SBarry Smith     if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr); }
937184914b5SBarry Smith     ierr = VecRestoreArray(mat->lvec,&ls);CHKERRQ(ierr);
9383a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_BACKWARD_SWEEP){
939da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
940f830108cSBarry Smith       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,its,xx);CHKERRQ(ierr);
9413a40ed3dSBarry Smith       PetscFunctionReturn(0);
942da3a660dSBarry Smith     }
9432e8a6d31SBarry Smith     ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
9442e8a6d31SBarry Smith     ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
945184914b5SBarry Smith     ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
946184914b5SBarry Smith     if (xx != bb) {
947184914b5SBarry Smith       ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
948184914b5SBarry Smith     } else {
949184914b5SBarry Smith       b = x;
950184914b5SBarry Smith     }
951184914b5SBarry Smith     ierr = VecGetArray(mat->lvec,&ls);CHKERRQ(ierr);
952184914b5SBarry Smith     xs = x + shift; /* shift by one for index start of 1 */
953184914b5SBarry Smith     ls = ls + shift;
954d6dfbf8fSBarry Smith     while (its--) {
955d6dfbf8fSBarry Smith       for (i=m-1; i>-1; i--) {
956d6dfbf8fSBarry Smith         n    = A->i[i+1] - A->i[i];
957b0a32e0cSBarry Smith 	PetscLogFlops(4*n+3);
958dbb450caSBarry Smith         idx  = A->j + A->i[i] + shift;
959dbb450caSBarry Smith         v    = A->a + A->i[i] + shift;
960d6dfbf8fSBarry Smith         sum  = b[i];
961d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
962dbb450caSBarry Smith         d    = fshift + A->a[diag[i]+shift];
963d6dfbf8fSBarry Smith         n    = B->i[i+1] - B->i[i];
964dbb450caSBarry Smith         idx  = B->j + B->i[i] + shift;
965dbb450caSBarry Smith         v    = B->a + B->i[i] + shift;
966d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,ls,v,idx,n);
96755a1b374SBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + A->a[diag[i]+shift]*x[i])/d;
968d6dfbf8fSBarry Smith       }
969d6dfbf8fSBarry Smith     }
970184914b5SBarry Smith     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
971184914b5SBarry Smith     if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr); }
972184914b5SBarry Smith     ierr = VecRestoreArray(mat->lvec,&ls);CHKERRQ(ierr);
9733a40ed3dSBarry Smith   } else {
97429bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"Parallel SOR not supported");
975c16cb8f2SBarry Smith   }
9763a40ed3dSBarry Smith   PetscFunctionReturn(0);
9778a729477SBarry Smith }
978a66be287SLois Curfman McInnes 
9794a2ae208SSatish Balay #undef __FUNCT__
9804a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_MPIAIJ"
9818f6be9afSLois Curfman McInnes int MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info)
982a66be287SLois Curfman McInnes {
983a66be287SLois Curfman McInnes   Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data;
984a66be287SLois Curfman McInnes   Mat        A = mat->A,B = mat->B;
9854e220ebcSLois Curfman McInnes   int        ierr;
986329f5518SBarry Smith   PetscReal  isend[5],irecv[5];
987a66be287SLois Curfman McInnes 
9883a40ed3dSBarry Smith   PetscFunctionBegin;
9894e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
9904e220ebcSLois Curfman McInnes   ierr = MatGetInfo(A,MAT_LOCAL,info);CHKERRQ(ierr);
9914e220ebcSLois Curfman McInnes   isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
9924e220ebcSLois Curfman McInnes   isend[3] = info->memory;  isend[4] = info->mallocs;
9934e220ebcSLois Curfman McInnes   ierr = MatGetInfo(B,MAT_LOCAL,info);CHKERRQ(ierr);
9944e220ebcSLois Curfman McInnes   isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
9954e220ebcSLois Curfman McInnes   isend[3] += info->memory;  isend[4] += info->mallocs;
996a66be287SLois Curfman McInnes   if (flag == MAT_LOCAL) {
9974e220ebcSLois Curfman McInnes     info->nz_used      = isend[0];
9984e220ebcSLois Curfman McInnes     info->nz_allocated = isend[1];
9994e220ebcSLois Curfman McInnes     info->nz_unneeded  = isend[2];
10004e220ebcSLois Curfman McInnes     info->memory       = isend[3];
10014e220ebcSLois Curfman McInnes     info->mallocs      = isend[4];
1002a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_MAX) {
1003ca161407SBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPI_DOUBLE,MPI_MAX,matin->comm);CHKERRQ(ierr);
10044e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
10054e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
10064e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
10074e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
10084e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1009a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_SUM) {
1010ca161407SBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPI_DOUBLE,MPI_SUM,matin->comm);CHKERRQ(ierr);
10114e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
10124e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
10134e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
10144e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
10154e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1016a66be287SLois Curfman McInnes   }
10174e220ebcSLois Curfman McInnes   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
10184e220ebcSLois Curfman McInnes   info->fill_ratio_needed = 0;
10194e220ebcSLois Curfman McInnes   info->factor_mallocs    = 0;
1020273d9f13SBarry Smith   info->rows_global       = (double)matin->M;
1021273d9f13SBarry Smith   info->columns_global    = (double)matin->N;
1022273d9f13SBarry Smith   info->rows_local        = (double)matin->m;
1023273d9f13SBarry Smith   info->columns_local     = (double)matin->N;
10244e220ebcSLois Curfman McInnes 
10253a40ed3dSBarry Smith   PetscFunctionReturn(0);
1026a66be287SLois Curfman McInnes }
1027a66be287SLois Curfman McInnes 
10284a2ae208SSatish Balay #undef __FUNCT__
10294a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_MPIAIJ"
10308f6be9afSLois Curfman McInnes int MatSetOption_MPIAIJ(Mat A,MatOption op)
1031c74985f6SBarry Smith {
1032c0bbcb79SLois Curfman McInnes   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
10331dee9f54SBarry Smith   int        ierr;
1034c74985f6SBarry Smith 
10353a40ed3dSBarry Smith   PetscFunctionBegin;
1036*12c028f9SKris Buschelman   switch (op) {
1037*12c028f9SKris Buschelman   case MAT_NO_NEW_NONZERO_LOCATIONS:
1038*12c028f9SKris Buschelman   case MAT_YES_NEW_NONZERO_LOCATIONS:
1039*12c028f9SKris Buschelman   case MAT_COLUMNS_UNSORTED:
1040*12c028f9SKris Buschelman   case MAT_COLUMNS_SORTED:
1041*12c028f9SKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
1042*12c028f9SKris Buschelman   case MAT_KEEP_ZEROED_ROWS:
1043*12c028f9SKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
1044*12c028f9SKris Buschelman   case MAT_USE_INODES:
1045*12c028f9SKris Buschelman   case MAT_DO_NOT_USE_INODES:
1046*12c028f9SKris Buschelman   case MAT_IGNORE_ZERO_ENTRIES:
10471dee9f54SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
10481dee9f54SBarry Smith     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
1049*12c028f9SKris Buschelman     break;
1050*12c028f9SKris Buschelman   case MAT_ROW_ORIENTED:
10517c922b88SBarry Smith     a->roworiented = PETSC_TRUE;
10521dee9f54SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
10531dee9f54SBarry Smith     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
1054*12c028f9SKris Buschelman     break;
1055*12c028f9SKris Buschelman   case MAT_ROWS_SORTED:
1056*12c028f9SKris Buschelman   case MAT_ROWS_UNSORTED:
1057*12c028f9SKris Buschelman   case MAT_YES_NEW_DIAGONALS:
1058b0a32e0cSBarry Smith     PetscLogInfo(A,"MatSetOption_MPIAIJ:Option ignored\n");
1059*12c028f9SKris Buschelman     break;
1060*12c028f9SKris Buschelman   case MAT_COLUMN_ORIENTED:
10617c922b88SBarry Smith     a->roworiented = PETSC_FALSE;
10621dee9f54SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
10631dee9f54SBarry Smith     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
1064*12c028f9SKris Buschelman     break;
1065*12c028f9SKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
10667c922b88SBarry Smith     a->donotstash = PETSC_TRUE;
1067*12c028f9SKris Buschelman     break;
1068*12c028f9SKris Buschelman   case MAT_NO_NEW_DIAGONALS:
106929bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"MAT_NO_NEW_DIAGONALS");
1070*12c028f9SKris Buschelman     break;
1071*12c028f9SKris Buschelman   default:
107229bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"unknown option");
1073*12c028f9SKris Buschelman     break;
10743a40ed3dSBarry Smith   }
10753a40ed3dSBarry Smith   PetscFunctionReturn(0);
1076c74985f6SBarry Smith }
1077c74985f6SBarry Smith 
10784a2ae208SSatish Balay #undef __FUNCT__
10794a2ae208SSatish Balay #define __FUNCT__ "MatGetOwnershipRange_MPIAIJ"
10808f6be9afSLois Curfman McInnes int MatGetOwnershipRange_MPIAIJ(Mat matin,int *m,int *n)
1081c74985f6SBarry Smith {
108244a69424SLois Curfman McInnes   Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data;
10833a40ed3dSBarry Smith 
10843a40ed3dSBarry Smith   PetscFunctionBegin;
10854c49b128SBarry Smith   if (m) *m = mat->rstart;
10864c49b128SBarry Smith   if (n) *n = mat->rend;
10873a40ed3dSBarry Smith   PetscFunctionReturn(0);
1088c74985f6SBarry Smith }
1089c74985f6SBarry Smith 
10904a2ae208SSatish Balay #undef __FUNCT__
10914a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_MPIAIJ"
10926d84be18SBarry Smith int MatGetRow_MPIAIJ(Mat matin,int row,int *nz,int **idx,Scalar **v)
109339e00950SLois Curfman McInnes {
1094154123eaSLois Curfman McInnes   Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data;
109570f0671dSBarry Smith   Scalar     *vworkA,*vworkB,**pvA,**pvB,*v_p;
1096154123eaSLois Curfman McInnes   int        i,ierr,*cworkA,*cworkB,**pcA,**pcB,cstart = mat->cstart;
1097154123eaSLois Curfman McInnes   int        nztot,nzA,nzB,lrow,rstart = mat->rstart,rend = mat->rend;
109870f0671dSBarry Smith   int        *cmap,*idx_p;
109939e00950SLois Curfman McInnes 
11003a40ed3dSBarry Smith   PetscFunctionBegin;
110129bbc08cSBarry Smith   if (mat->getrowactive == PETSC_TRUE) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Already active");
11027a0afa10SBarry Smith   mat->getrowactive = PETSC_TRUE;
11037a0afa10SBarry Smith 
110470f0671dSBarry Smith   if (!mat->rowvalues && (idx || v)) {
11057a0afa10SBarry Smith     /*
11067a0afa10SBarry Smith         allocate enough space to hold information from the longest row.
11077a0afa10SBarry Smith     */
11087a0afa10SBarry Smith     Mat_SeqAIJ *Aa = (Mat_SeqAIJ*)mat->A->data,*Ba = (Mat_SeqAIJ*)mat->B->data;
1109273d9f13SBarry Smith     int     max = 1,tmp;
1110273d9f13SBarry Smith     for (i=0; i<matin->m; i++) {
11117a0afa10SBarry Smith       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
11127a0afa10SBarry Smith       if (max < tmp) { max = tmp; }
11137a0afa10SBarry Smith     }
1114a0e26f09SSatish Balay     ierr = PetscMalloc(max*(sizeof(int)+sizeof(Scalar)),&mat->rowvalues);CHKERRQ(ierr);
11157a0afa10SBarry Smith     mat->rowindices = (int*)(mat->rowvalues + max);
11167a0afa10SBarry Smith   }
11177a0afa10SBarry Smith 
111829bbc08cSBarry Smith   if (row < rstart || row >= rend) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Only local rows")
1119abc0e9e4SLois Curfman McInnes   lrow = row - rstart;
112039e00950SLois Curfman McInnes 
1121154123eaSLois Curfman McInnes   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1122154123eaSLois Curfman McInnes   if (!v)   {pvA = 0; pvB = 0;}
1123154123eaSLois Curfman McInnes   if (!idx) {pcA = 0; if (!v) pcB = 0;}
1124f830108cSBarry Smith   ierr = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1125f830108cSBarry Smith   ierr = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
1126154123eaSLois Curfman McInnes   nztot = nzA + nzB;
1127154123eaSLois Curfman McInnes 
112870f0671dSBarry Smith   cmap  = mat->garray;
1129154123eaSLois Curfman McInnes   if (v  || idx) {
1130154123eaSLois Curfman McInnes     if (nztot) {
1131154123eaSLois Curfman McInnes       /* Sort by increasing column numbers, assuming A and B already sorted */
113270f0671dSBarry Smith       int imark = -1;
1133154123eaSLois Curfman McInnes       if (v) {
113470f0671dSBarry Smith         *v = v_p = mat->rowvalues;
113539e00950SLois Curfman McInnes         for (i=0; i<nzB; i++) {
113670f0671dSBarry Smith           if (cmap[cworkB[i]] < cstart)   v_p[i] = vworkB[i];
1137154123eaSLois Curfman McInnes           else break;
1138154123eaSLois Curfman McInnes         }
1139154123eaSLois Curfman McInnes         imark = i;
114070f0671dSBarry Smith         for (i=0; i<nzA; i++)     v_p[imark+i] = vworkA[i];
114170f0671dSBarry Smith         for (i=imark; i<nzB; i++) v_p[nzA+i]   = vworkB[i];
1142154123eaSLois Curfman McInnes       }
1143154123eaSLois Curfman McInnes       if (idx) {
114470f0671dSBarry Smith         *idx = idx_p = mat->rowindices;
114570f0671dSBarry Smith         if (imark > -1) {
114670f0671dSBarry Smith           for (i=0; i<imark; i++) {
114770f0671dSBarry Smith             idx_p[i] = cmap[cworkB[i]];
114870f0671dSBarry Smith           }
114970f0671dSBarry Smith         } else {
1150154123eaSLois Curfman McInnes           for (i=0; i<nzB; i++) {
115170f0671dSBarry Smith             if (cmap[cworkB[i]] < cstart)   idx_p[i] = cmap[cworkB[i]];
1152154123eaSLois Curfman McInnes             else break;
1153154123eaSLois Curfman McInnes           }
1154154123eaSLois Curfman McInnes           imark = i;
115570f0671dSBarry Smith         }
115670f0671dSBarry Smith         for (i=0; i<nzA; i++)     idx_p[imark+i] = cstart + cworkA[i];
115770f0671dSBarry Smith         for (i=imark; i<nzB; i++) idx_p[nzA+i]   = cmap[cworkB[i]];
115839e00950SLois Curfman McInnes       }
11593f97c4b0SBarry Smith     } else {
11601ca473b0SSatish Balay       if (idx) *idx = 0;
11611ca473b0SSatish Balay       if (v)   *v   = 0;
11621ca473b0SSatish Balay     }
1163154123eaSLois Curfman McInnes   }
116439e00950SLois Curfman McInnes   *nz = nztot;
1165f830108cSBarry Smith   ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1166f830108cSBarry Smith   ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
11673a40ed3dSBarry Smith   PetscFunctionReturn(0);
116839e00950SLois Curfman McInnes }
116939e00950SLois Curfman McInnes 
11704a2ae208SSatish Balay #undef __FUNCT__
11714a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_MPIAIJ"
11726d84be18SBarry Smith int MatRestoreRow_MPIAIJ(Mat mat,int row,int *nz,int **idx,Scalar **v)
117339e00950SLois Curfman McInnes {
11747a0afa10SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
11753a40ed3dSBarry Smith 
11763a40ed3dSBarry Smith   PetscFunctionBegin;
11777a0afa10SBarry Smith   if (aij->getrowactive == PETSC_FALSE) {
117829bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"MatGetRow not called");
11797a0afa10SBarry Smith   }
11807a0afa10SBarry Smith   aij->getrowactive = PETSC_FALSE;
11813a40ed3dSBarry Smith   PetscFunctionReturn(0);
118239e00950SLois Curfman McInnes }
118339e00950SLois Curfman McInnes 
11844a2ae208SSatish Balay #undef __FUNCT__
11854a2ae208SSatish Balay #define __FUNCT__ "MatNorm_MPIAIJ"
1186329f5518SBarry Smith int MatNorm_MPIAIJ(Mat mat,NormType type,PetscReal *norm)
1187855ac2c5SLois Curfman McInnes {
1188855ac2c5SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
1189ec8511deSBarry Smith   Mat_SeqAIJ *amat = (Mat_SeqAIJ*)aij->A->data,*bmat = (Mat_SeqAIJ*)aij->B->data;
1190416022c9SBarry Smith   int        ierr,i,j,cstart = aij->cstart,shift = amat->indexshift;
1191329f5518SBarry Smith   PetscReal  sum = 0.0;
119204ca555eSLois Curfman McInnes   Scalar     *v;
119304ca555eSLois Curfman McInnes 
11943a40ed3dSBarry Smith   PetscFunctionBegin;
119517699dbbSLois Curfman McInnes   if (aij->size == 1) {
119614183eadSLois Curfman McInnes     ierr =  MatNorm(aij->A,type,norm);CHKERRQ(ierr);
119737fa93a5SLois Curfman McInnes   } else {
119804ca555eSLois Curfman McInnes     if (type == NORM_FROBENIUS) {
119904ca555eSLois Curfman McInnes       v = amat->a;
120004ca555eSLois Curfman McInnes       for (i=0; i<amat->nz; i++) {
1201aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
1202329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
120304ca555eSLois Curfman McInnes #else
120404ca555eSLois Curfman McInnes         sum += (*v)*(*v); v++;
120504ca555eSLois Curfman McInnes #endif
120604ca555eSLois Curfman McInnes       }
120704ca555eSLois Curfman McInnes       v = bmat->a;
120804ca555eSLois Curfman McInnes       for (i=0; i<bmat->nz; i++) {
1209aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
1210329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
121104ca555eSLois Curfman McInnes #else
121204ca555eSLois Curfman McInnes         sum += (*v)*(*v); v++;
121304ca555eSLois Curfman McInnes #endif
121404ca555eSLois Curfman McInnes       }
1215ca161407SBarry Smith       ierr = MPI_Allreduce(&sum,norm,1,MPI_DOUBLE,MPI_SUM,mat->comm);CHKERRQ(ierr);
121604ca555eSLois Curfman McInnes       *norm = sqrt(*norm);
12173a40ed3dSBarry Smith     } else if (type == NORM_1) { /* max column norm */
1218329f5518SBarry Smith       PetscReal *tmp,*tmp2;
121904ca555eSLois Curfman McInnes       int    *jj,*garray = aij->garray;
1220b0a32e0cSBarry Smith       ierr = PetscMalloc((mat->N+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1221b0a32e0cSBarry Smith       ierr = PetscMalloc((mat->N+1)*sizeof(PetscReal),&tmp2);CHKERRQ(ierr);
1222273d9f13SBarry Smith       ierr = PetscMemzero(tmp,mat->N*sizeof(PetscReal));CHKERRQ(ierr);
122304ca555eSLois Curfman McInnes       *norm = 0.0;
122404ca555eSLois Curfman McInnes       v = amat->a; jj = amat->j;
122504ca555eSLois Curfman McInnes       for (j=0; j<amat->nz; j++) {
1226579c6b6fSBarry Smith         tmp[cstart + *jj++ + shift] += PetscAbsScalar(*v);  v++;
122704ca555eSLois Curfman McInnes       }
122804ca555eSLois Curfman McInnes       v = bmat->a; jj = bmat->j;
122904ca555eSLois Curfman McInnes       for (j=0; j<bmat->nz; j++) {
1230579c6b6fSBarry Smith         tmp[garray[*jj++ + shift]] += PetscAbsScalar(*v); v++;
123104ca555eSLois Curfman McInnes       }
1232273d9f13SBarry Smith       ierr = MPI_Allreduce(tmp,tmp2,mat->N,MPI_DOUBLE,MPI_SUM,mat->comm);CHKERRQ(ierr);
1233273d9f13SBarry Smith       for (j=0; j<mat->N; j++) {
123404ca555eSLois Curfman McInnes         if (tmp2[j] > *norm) *norm = tmp2[j];
123504ca555eSLois Curfman McInnes       }
1236606d414cSSatish Balay       ierr = PetscFree(tmp);CHKERRQ(ierr);
1237606d414cSSatish Balay       ierr = PetscFree(tmp2);CHKERRQ(ierr);
12383a40ed3dSBarry Smith     } else if (type == NORM_INFINITY) { /* max row norm */
1239329f5518SBarry Smith       PetscReal ntemp = 0.0;
1240273d9f13SBarry Smith       for (j=0; j<aij->A->m; j++) {
1241dbb450caSBarry Smith         v = amat->a + amat->i[j] + shift;
124204ca555eSLois Curfman McInnes         sum = 0.0;
124304ca555eSLois Curfman McInnes         for (i=0; i<amat->i[j+1]-amat->i[j]; i++) {
1244cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
124504ca555eSLois Curfman McInnes         }
1246dbb450caSBarry Smith         v = bmat->a + bmat->i[j] + shift;
124704ca555eSLois Curfman McInnes         for (i=0; i<bmat->i[j+1]-bmat->i[j]; i++) {
1248cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
124904ca555eSLois Curfman McInnes         }
1250515d9167SLois Curfman McInnes         if (sum > ntemp) ntemp = sum;
125104ca555eSLois Curfman McInnes       }
1252ca161407SBarry Smith       ierr = MPI_Allreduce(&ntemp,norm,1,MPI_DOUBLE,MPI_MAX,mat->comm);CHKERRQ(ierr);
1253ca161407SBarry Smith     } else {
125429bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"No support for two norm");
125504ca555eSLois Curfman McInnes     }
125637fa93a5SLois Curfman McInnes   }
12573a40ed3dSBarry Smith   PetscFunctionReturn(0);
1258855ac2c5SLois Curfman McInnes }
1259855ac2c5SLois Curfman McInnes 
12604a2ae208SSatish Balay #undef __FUNCT__
12614a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_MPIAIJ"
12628f6be9afSLois Curfman McInnes int MatTranspose_MPIAIJ(Mat A,Mat *matout)
1263b7c46309SBarry Smith {
1264b7c46309SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
1265dbb450caSBarry Smith   Mat_SeqAIJ *Aloc = (Mat_SeqAIJ*)a->A->data;
1266416022c9SBarry Smith   int        ierr,shift = Aloc->indexshift;
1267273d9f13SBarry Smith   int        M = A->M,N = A->N,m,*ai,*aj,row,*cols,i,*ct;
12683a40ed3dSBarry Smith   Mat        B;
1269b7c46309SBarry Smith   Scalar     *array;
1270b7c46309SBarry Smith 
12713a40ed3dSBarry Smith   PetscFunctionBegin;
12727c922b88SBarry Smith   if (!matout && M != N) {
127329bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1274d4bb536fSBarry Smith   }
1275d4bb536fSBarry Smith 
1276273d9f13SBarry Smith   ierr = MatCreateMPIAIJ(A->comm,A->n,A->m,N,M,0,PETSC_NULL,0,PETSC_NULL,&B);CHKERRQ(ierr);
1277b7c46309SBarry Smith 
1278b7c46309SBarry Smith   /* copy over the A part */
1279ec8511deSBarry Smith   Aloc = (Mat_SeqAIJ*)a->A->data;
1280273d9f13SBarry Smith   m = a->A->m; ai = Aloc->i; aj = Aloc->j; array = Aloc->a;
1281b7c46309SBarry Smith   row = a->rstart;
1282dbb450caSBarry Smith   for (i=0; i<ai[m]+shift; i++) {aj[i] += a->cstart + shift;}
1283b7c46309SBarry Smith   for (i=0; i<m; i++) {
1284416022c9SBarry Smith     ierr = MatSetValues(B,ai[i+1]-ai[i],aj,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
1285b7c46309SBarry Smith     row++; array += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
1286b7c46309SBarry Smith   }
1287b7c46309SBarry Smith   aj = Aloc->j;
12884af08d9eSBarry Smith   for (i=0; i<ai[m]+shift; i++) {aj[i] -= a->cstart + shift;}
1289b7c46309SBarry Smith 
1290b7c46309SBarry Smith   /* copy over the B part */
1291ec8511deSBarry Smith   Aloc = (Mat_SeqAIJ*)a->B->data;
1292273d9f13SBarry Smith   m = a->B->m;  ai = Aloc->i; aj = Aloc->j; array = Aloc->a;
1293b7c46309SBarry Smith   row  = a->rstart;
1294b0a32e0cSBarry Smith   ierr = PetscMalloc((1+ai[m]-shift)*sizeof(int),&cols);CHKERRQ(ierr);
1295b0a32e0cSBarry Smith   ct   = cols;
1296dbb450caSBarry Smith   for (i=0; i<ai[m]+shift; i++) {cols[i] = a->garray[aj[i]+shift];}
1297b7c46309SBarry Smith   for (i=0; i<m; i++) {
1298416022c9SBarry Smith     ierr = MatSetValues(B,ai[i+1]-ai[i],cols,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
1299b7c46309SBarry Smith     row++; array += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
1300b7c46309SBarry Smith   }
1301606d414cSSatish Balay   ierr = PetscFree(ct);CHKERRQ(ierr);
13026d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
13036d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
13047c922b88SBarry Smith   if (matout) {
13050de55854SLois Curfman McInnes     *matout = B;
13060de55854SLois Curfman McInnes   } else {
1307273d9f13SBarry Smith     ierr = MatHeaderCopy(A,B);CHKERRQ(ierr);
13080de55854SLois Curfman McInnes   }
13093a40ed3dSBarry Smith   PetscFunctionReturn(0);
1310b7c46309SBarry Smith }
1311b7c46309SBarry Smith 
13124a2ae208SSatish Balay #undef __FUNCT__
13134a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_MPIAIJ"
13144b967eb1SSatish Balay int MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr)
1315a008b906SSatish Balay {
13164b967eb1SSatish Balay   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
13174b967eb1SSatish Balay   Mat        a = aij->A,b = aij->B;
1318a008b906SSatish Balay   int        ierr,s1,s2,s3;
1319a008b906SSatish Balay 
13203a40ed3dSBarry Smith   PetscFunctionBegin;
13214b967eb1SSatish Balay   ierr = MatGetLocalSize(mat,&s2,&s3);CHKERRQ(ierr);
13224b967eb1SSatish Balay   if (rr) {
1323e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr);
132429bbc08cSBarry Smith     if (s1!=s3) SETERRQ(PETSC_ERR_ARG_SIZ,"right vector non-conforming local size");
13254b967eb1SSatish Balay     /* Overlap communication with computation. */
132643a90d84SBarry Smith     ierr = VecScatterBegin(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx);CHKERRQ(ierr);
1327a008b906SSatish Balay   }
13284b967eb1SSatish Balay   if (ll) {
1329e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr);
133029bbc08cSBarry Smith     if (s1!=s2) SETERRQ(PETSC_ERR_ARG_SIZ,"left vector non-conforming local size");
1331f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,ll,0);CHKERRQ(ierr);
13324b967eb1SSatish Balay   }
13334b967eb1SSatish Balay   /* scale  the diagonal block */
1334f830108cSBarry Smith   ierr = (*a->ops->diagonalscale)(a,ll,rr);CHKERRQ(ierr);
13354b967eb1SSatish Balay 
13364b967eb1SSatish Balay   if (rr) {
13374b967eb1SSatish Balay     /* Do a scatter end and then right scale the off-diagonal block */
133843a90d84SBarry Smith     ierr = VecScatterEnd(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx);CHKERRQ(ierr);
1339f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,0,aij->lvec);CHKERRQ(ierr);
13404b967eb1SSatish Balay   }
13414b967eb1SSatish Balay 
13423a40ed3dSBarry Smith   PetscFunctionReturn(0);
1343a008b906SSatish Balay }
1344a008b906SSatish Balay 
1345a008b906SSatish Balay 
13464a2ae208SSatish Balay #undef __FUNCT__
13474a2ae208SSatish Balay #define __FUNCT__ "MatPrintHelp_MPIAIJ"
13488f6be9afSLois Curfman McInnes int MatPrintHelp_MPIAIJ(Mat A)
1349682d7d0cSBarry Smith {
1350682d7d0cSBarry Smith   Mat_MPIAIJ *a   = (Mat_MPIAIJ*)A->data;
13513a40ed3dSBarry Smith   int        ierr;
1352682d7d0cSBarry Smith 
13533a40ed3dSBarry Smith   PetscFunctionBegin;
13543a40ed3dSBarry Smith   if (!a->rank) {
13553a40ed3dSBarry Smith     ierr = MatPrintHelp_SeqAIJ(a->A);CHKERRQ(ierr);
13563a40ed3dSBarry Smith   }
13573a40ed3dSBarry Smith   PetscFunctionReturn(0);
1358682d7d0cSBarry Smith }
1359682d7d0cSBarry Smith 
13604a2ae208SSatish Balay #undef __FUNCT__
13614a2ae208SSatish Balay #define __FUNCT__ "MatGetBlockSize_MPIAIJ"
13628f6be9afSLois Curfman McInnes int MatGetBlockSize_MPIAIJ(Mat A,int *bs)
13635a838052SSatish Balay {
13643a40ed3dSBarry Smith   PetscFunctionBegin;
13655a838052SSatish Balay   *bs = 1;
13663a40ed3dSBarry Smith   PetscFunctionReturn(0);
13675a838052SSatish Balay }
13684a2ae208SSatish Balay #undef __FUNCT__
13694a2ae208SSatish Balay #define __FUNCT__ "MatSetUnfactored_MPIAIJ"
13708f6be9afSLois Curfman McInnes int MatSetUnfactored_MPIAIJ(Mat A)
1371bb5a7306SBarry Smith {
1372bb5a7306SBarry Smith   Mat_MPIAIJ *a   = (Mat_MPIAIJ*)A->data;
1373bb5a7306SBarry Smith   int        ierr;
13743a40ed3dSBarry Smith 
13753a40ed3dSBarry Smith   PetscFunctionBegin;
1376bb5a7306SBarry Smith   ierr = MatSetUnfactored(a->A);CHKERRQ(ierr);
13773a40ed3dSBarry Smith   PetscFunctionReturn(0);
1378bb5a7306SBarry Smith }
1379bb5a7306SBarry Smith 
13804a2ae208SSatish Balay #undef __FUNCT__
13814a2ae208SSatish Balay #define __FUNCT__ "MatEqual_MPIAIJ"
1382d4bb536fSBarry Smith int MatEqual_MPIAIJ(Mat A,Mat B,PetscTruth *flag)
1383d4bb536fSBarry Smith {
1384d4bb536fSBarry Smith   Mat_MPIAIJ *matB = (Mat_MPIAIJ*)B->data,*matA = (Mat_MPIAIJ*)A->data;
1385d4bb536fSBarry Smith   Mat        a,b,c,d;
1386d4bb536fSBarry Smith   PetscTruth flg;
1387d4bb536fSBarry Smith   int        ierr;
1388d4bb536fSBarry Smith 
13893a40ed3dSBarry Smith   PetscFunctionBegin;
1390273d9f13SBarry Smith   ierr = PetscTypeCompare((PetscObject)B,MATMPIAIJ,&flg);CHKERRQ(ierr);
1391273d9f13SBarry Smith   if (!flg) SETERRQ(PETSC_ERR_ARG_INCOMP,"Matrices must be same type");
1392d4bb536fSBarry Smith   a = matA->A; b = matA->B;
1393d4bb536fSBarry Smith   c = matB->A; d = matB->B;
1394d4bb536fSBarry Smith 
1395d4bb536fSBarry Smith   ierr = MatEqual(a,c,&flg);CHKERRQ(ierr);
1396d4bb536fSBarry Smith   if (flg == PETSC_TRUE) {
1397d4bb536fSBarry Smith     ierr = MatEqual(b,d,&flg);CHKERRQ(ierr);
1398d4bb536fSBarry Smith   }
1399ca161407SBarry Smith   ierr = MPI_Allreduce(&flg,flag,1,MPI_INT,MPI_LAND,A->comm);CHKERRQ(ierr);
14003a40ed3dSBarry Smith   PetscFunctionReturn(0);
1401d4bb536fSBarry Smith }
1402d4bb536fSBarry Smith 
14034a2ae208SSatish Balay #undef __FUNCT__
14044a2ae208SSatish Balay #define __FUNCT__ "MatCopy_MPIAIJ"
1405cb5b572fSBarry Smith int MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str)
1406cb5b572fSBarry Smith {
1407cb5b572fSBarry Smith   int        ierr;
1408cb5b572fSBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;
1409cb5b572fSBarry Smith   Mat_MPIAIJ *b = (Mat_MPIAIJ *)B->data;
1410273d9f13SBarry Smith   PetscTruth flg;
1411cb5b572fSBarry Smith 
1412cb5b572fSBarry Smith   PetscFunctionBegin;
1413273d9f13SBarry Smith   ierr = PetscTypeCompare((PetscObject)B,MATMPIAIJ,&flg);CHKERRQ(ierr);
1414273d9f13SBarry Smith   if (str != SAME_NONZERO_PATTERN || !flg) {
1415cb5b572fSBarry Smith     /* because of the column compression in the off-processor part of the matrix a->B,
1416cb5b572fSBarry Smith        the number of columns in a->B and b->B may be different, hence we cannot call
1417cb5b572fSBarry Smith        the MatCopy() directly on the two parts. If need be, we can provide a more
1418cb5b572fSBarry Smith        efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices
1419cb5b572fSBarry Smith        then copying the submatrices */
1420cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1421cb5b572fSBarry Smith   } else {
1422cb5b572fSBarry Smith     ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr);
1423cb5b572fSBarry Smith     ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr);
1424cb5b572fSBarry Smith   }
1425cb5b572fSBarry Smith   PetscFunctionReturn(0);
1426cb5b572fSBarry Smith }
1427cb5b572fSBarry Smith 
14284a2ae208SSatish Balay #undef __FUNCT__
14294a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_MPIAIJ"
1430273d9f13SBarry Smith int MatSetUpPreallocation_MPIAIJ(Mat A)
1431273d9f13SBarry Smith {
1432273d9f13SBarry Smith   int        ierr;
1433273d9f13SBarry Smith 
1434273d9f13SBarry Smith   PetscFunctionBegin;
1435273d9f13SBarry Smith   ierr =  MatMPIAIJSetPreallocation(A,PETSC_DEFAULT,0,PETSC_DEFAULT,0);CHKERRQ(ierr);
1436273d9f13SBarry Smith   PetscFunctionReturn(0);
1437273d9f13SBarry Smith }
1438273d9f13SBarry Smith 
1439ca44d042SBarry Smith EXTERN int MatDuplicate_MPIAIJ(Mat,MatDuplicateOption,Mat *);
1440ca44d042SBarry Smith EXTERN int MatIncreaseOverlap_MPIAIJ(Mat,int,IS *,int);
1441ca44d042SBarry Smith EXTERN int MatFDColoringCreate_MPIAIJ(Mat,ISColoring,MatFDColoring);
1442ca44d042SBarry Smith EXTERN int MatGetSubMatrices_MPIAIJ (Mat,int,IS *,IS *,MatReuse,Mat **);
1443ca44d042SBarry Smith EXTERN int MatGetSubMatrix_MPIAIJ (Mat,IS,IS,int,MatReuse,Mat *);
1444b9617806SBarry Smith #if !defined(PETSC_USE_COMPLEX)
1445b9617806SBarry Smith EXTERN int MatLUFactorSymbolic_MPIAIJ_TFS(Mat,IS,IS,MatLUInfo*,Mat*);
1446b9617806SBarry Smith #endif
144700e6dbe6SBarry Smith 
14488a729477SBarry Smith /* -------------------------------------------------------------------*/
1449cda55fadSBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ,
1450cda55fadSBarry Smith        MatGetRow_MPIAIJ,
1451cda55fadSBarry Smith        MatRestoreRow_MPIAIJ,
1452cda55fadSBarry Smith        MatMult_MPIAIJ,
1453cda55fadSBarry Smith        MatMultAdd_MPIAIJ,
14547c922b88SBarry Smith        MatMultTranspose_MPIAIJ,
14557c922b88SBarry Smith        MatMultTransposeAdd_MPIAIJ,
1456cda55fadSBarry Smith        0,
1457cda55fadSBarry Smith        0,
1458cda55fadSBarry Smith        0,
1459cda55fadSBarry Smith        0,
1460cda55fadSBarry Smith        0,
1461cda55fadSBarry Smith        0,
146244a69424SLois Curfman McInnes        MatRelax_MPIAIJ,
1463b7c46309SBarry Smith        MatTranspose_MPIAIJ,
1464cda55fadSBarry Smith        MatGetInfo_MPIAIJ,
1465cda55fadSBarry Smith        MatEqual_MPIAIJ,
1466cda55fadSBarry Smith        MatGetDiagonal_MPIAIJ,
1467cda55fadSBarry Smith        MatDiagonalScale_MPIAIJ,
1468cda55fadSBarry Smith        MatNorm_MPIAIJ,
1469cda55fadSBarry Smith        MatAssemblyBegin_MPIAIJ,
1470cda55fadSBarry Smith        MatAssemblyEnd_MPIAIJ,
14711eb62cbbSBarry Smith        0,
1472cda55fadSBarry Smith        MatSetOption_MPIAIJ,
1473cda55fadSBarry Smith        MatZeroEntries_MPIAIJ,
1474cda55fadSBarry Smith        MatZeroRows_MPIAIJ,
1475b9617806SBarry Smith #if !defined(PETSC_USE_COMPLEX)
1476b9617806SBarry Smith 				       MatLUFactorSymbolic_MPIAIJ_TFS,
1477b9617806SBarry Smith #else
1478cda55fadSBarry Smith        0,
1479b9617806SBarry Smith #endif
1480cda55fadSBarry Smith        0,
1481cda55fadSBarry Smith        0,
1482cda55fadSBarry Smith        0,
1483273d9f13SBarry Smith        MatSetUpPreallocation_MPIAIJ,
1484273d9f13SBarry Smith        0,
1485cda55fadSBarry Smith        MatGetOwnershipRange_MPIAIJ,
1486cda55fadSBarry Smith        0,
1487cda55fadSBarry Smith        0,
1488cda55fadSBarry Smith        0,
1489cda55fadSBarry Smith        0,
14902e8a6d31SBarry Smith        MatDuplicate_MPIAIJ,
1491cda55fadSBarry Smith        0,
1492cda55fadSBarry Smith        0,
1493cda55fadSBarry Smith        0,
1494cda55fadSBarry Smith        0,
1495cda55fadSBarry Smith        0,
1496cda55fadSBarry Smith        MatGetSubMatrices_MPIAIJ,
1497cda55fadSBarry Smith        MatIncreaseOverlap_MPIAIJ,
1498cda55fadSBarry Smith        MatGetValues_MPIAIJ,
1499cb5b572fSBarry Smith        MatCopy_MPIAIJ,
1500052efed2SBarry Smith        MatPrintHelp_MPIAIJ,
1501cda55fadSBarry Smith        MatScale_MPIAIJ,
1502cda55fadSBarry Smith        0,
1503cda55fadSBarry Smith        0,
1504cda55fadSBarry Smith        0,
1505cda55fadSBarry Smith        MatGetBlockSize_MPIAIJ,
1506cda55fadSBarry Smith        0,
1507cda55fadSBarry Smith        0,
1508cda55fadSBarry Smith        0,
1509cda55fadSBarry Smith        0,
1510cda55fadSBarry Smith        MatFDColoringCreate_MPIAIJ,
1511cda55fadSBarry Smith        0,
1512cda55fadSBarry Smith        MatSetUnfactored_MPIAIJ,
1513cda55fadSBarry Smith        0,
1514cda55fadSBarry Smith        0,
1515cda55fadSBarry Smith        MatGetSubMatrix_MPIAIJ,
1516e03a110bSBarry Smith        MatDestroy_MPIAIJ,
1517e03a110bSBarry Smith        MatView_MPIAIJ,
1518a2243be0SBarry Smith        MatGetMaps_Petsc,
1519a2243be0SBarry Smith        0,
1520a2243be0SBarry Smith        0,
1521a2243be0SBarry Smith        0,
1522a2243be0SBarry Smith        0,
1523a2243be0SBarry Smith        0,
1524a2243be0SBarry Smith        0,
1525a2243be0SBarry Smith        0,
1526a2243be0SBarry Smith        0,
1527a2243be0SBarry Smith        MatSetColoring_MPIAIJ,
1528779c1a83SBarry Smith        MatSetValuesAdic_MPIAIJ,
1529779c1a83SBarry Smith        MatSetValuesAdifor_MPIAIJ
1530a2243be0SBarry Smith };
153136ce4990SBarry Smith 
15322e8a6d31SBarry Smith /* ----------------------------------------------------------------------------------------*/
15332e8a6d31SBarry Smith 
1534fb2e594dSBarry Smith EXTERN_C_BEGIN
15354a2ae208SSatish Balay #undef __FUNCT__
15364a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_MPIAIJ"
15372e8a6d31SBarry Smith int MatStoreValues_MPIAIJ(Mat mat)
15382e8a6d31SBarry Smith {
15392e8a6d31SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data;
15402e8a6d31SBarry Smith   int        ierr;
15412e8a6d31SBarry Smith 
15422e8a6d31SBarry Smith   PetscFunctionBegin;
15432e8a6d31SBarry Smith   ierr = MatStoreValues(aij->A);CHKERRQ(ierr);
15442e8a6d31SBarry Smith   ierr = MatStoreValues(aij->B);CHKERRQ(ierr);
15452e8a6d31SBarry Smith   PetscFunctionReturn(0);
15462e8a6d31SBarry Smith }
1547fb2e594dSBarry Smith EXTERN_C_END
15482e8a6d31SBarry Smith 
1549fb2e594dSBarry Smith EXTERN_C_BEGIN
15504a2ae208SSatish Balay #undef __FUNCT__
15514a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_MPIAIJ"
15522e8a6d31SBarry Smith int MatRetrieveValues_MPIAIJ(Mat mat)
15532e8a6d31SBarry Smith {
15542e8a6d31SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data;
15552e8a6d31SBarry Smith   int        ierr;
15562e8a6d31SBarry Smith 
15572e8a6d31SBarry Smith   PetscFunctionBegin;
15582e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr);
15592e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr);
15602e8a6d31SBarry Smith   PetscFunctionReturn(0);
15612e8a6d31SBarry Smith }
1562fb2e594dSBarry Smith EXTERN_C_END
15638a729477SBarry Smith 
1564e090d566SSatish Balay #include "petscpc.h"
156527508adbSBarry Smith EXTERN_C_BEGIN
1566ca44d042SBarry Smith EXTERN int MatGetDiagonalBlock_MPIAIJ(Mat,PetscTruth *,MatReuse,Mat *);
156727508adbSBarry Smith EXTERN_C_END
156827508adbSBarry Smith 
1569273d9f13SBarry Smith EXTERN_C_BEGIN
15704a2ae208SSatish Balay #undef __FUNCT__
15714a2ae208SSatish Balay #define __FUNCT__ "MatCreate_MPIAIJ"
1572273d9f13SBarry Smith int MatCreate_MPIAIJ(Mat B)
15738a729477SBarry Smith {
157444cd7ae7SLois Curfman McInnes   Mat_MPIAIJ   *b;
1575f1af5d2fSBarry Smith   int          ierr,i,size;
1576416022c9SBarry Smith 
15773a40ed3dSBarry Smith   PetscFunctionBegin;
1578273d9f13SBarry Smith   ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr);
15791d55c564SBarry Smith 
1580b0a32e0cSBarry Smith   ierr            = PetscNew(Mat_MPIAIJ,&b);CHKERRQ(ierr);
1581b0a32e0cSBarry Smith   B->data         = (void*)b;
1582549d3d68SSatish Balay   ierr            = PetscMemzero(b,sizeof(Mat_MPIAIJ));CHKERRQ(ierr);
1583549d3d68SSatish Balay   ierr            = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
158444cd7ae7SLois Curfman McInnes   B->factor       = 0;
158544cd7ae7SLois Curfman McInnes   B->assembled    = PETSC_FALSE;
158690f02eecSBarry Smith   B->mapping      = 0;
1587d6dfbf8fSBarry Smith 
158847794344SBarry Smith   B->insertmode      = NOT_SET_VALUES;
15899eb4d147SSatish Balay   b->size            = size;
1590273d9f13SBarry Smith   ierr = MPI_Comm_rank(B->comm,&b->rank);CHKERRQ(ierr);
15911eb62cbbSBarry Smith 
1592273d9f13SBarry Smith   ierr = PetscSplitOwnership(B->comm,&B->m,&B->M);CHKERRQ(ierr);
1593273d9f13SBarry Smith   ierr = PetscSplitOwnership(B->comm,&B->n,&B->N);CHKERRQ(ierr);
15941eb62cbbSBarry Smith 
1595c7fcc2eaSBarry Smith   /* the information in the maps duplicates the information computed below, eventually
1596c7fcc2eaSBarry Smith      we should remove the duplicate information that is not contained in the maps */
1597273d9f13SBarry Smith   ierr = MapCreateMPI(B->comm,B->m,B->M,&B->rmap);CHKERRQ(ierr);
1598273d9f13SBarry Smith   ierr = MapCreateMPI(B->comm,B->n,B->N,&B->cmap);CHKERRQ(ierr);
1599c7fcc2eaSBarry Smith 
16001eb62cbbSBarry Smith   /* build local table of row and column ownerships */
1601b0a32e0cSBarry Smith   ierr = PetscMalloc(2*(b->size+2)*sizeof(int),&b->rowners);CHKERRQ(ierr);
1602b0a32e0cSBarry Smith   PetscLogObjectMemory(B,2*(b->size+2)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_MPIAIJ));
1603603f58a4SSatish Balay   b->cowners = b->rowners + b->size + 2;
1604273d9f13SBarry Smith   ierr = MPI_Allgather(&B->m,1,MPI_INT,b->rowners+1,1,MPI_INT,B->comm);CHKERRQ(ierr);
160544cd7ae7SLois Curfman McInnes   b->rowners[0] = 0;
160644cd7ae7SLois Curfman McInnes   for (i=2; i<=b->size; i++) {
160744cd7ae7SLois Curfman McInnes     b->rowners[i] += b->rowners[i-1];
16088a729477SBarry Smith   }
160944cd7ae7SLois Curfman McInnes   b->rstart = b->rowners[b->rank];
161044cd7ae7SLois Curfman McInnes   b->rend   = b->rowners[b->rank+1];
1611273d9f13SBarry Smith   ierr = MPI_Allgather(&B->n,1,MPI_INT,b->cowners+1,1,MPI_INT,B->comm);CHKERRQ(ierr);
161244cd7ae7SLois Curfman McInnes   b->cowners[0] = 0;
161344cd7ae7SLois Curfman McInnes   for (i=2; i<=b->size; i++) {
161444cd7ae7SLois Curfman McInnes     b->cowners[i] += b->cowners[i-1];
16158a729477SBarry Smith   }
161644cd7ae7SLois Curfman McInnes   b->cstart = b->cowners[b->rank];
161744cd7ae7SLois Curfman McInnes   b->cend   = b->cowners[b->rank+1];
16188a729477SBarry Smith 
16191eb62cbbSBarry Smith   /* build cache for off array entries formed */
16207e2c5f70SBarry Smith   ierr = MatStashCreate_Private(B->comm,1,&B->stash);CHKERRQ(ierr);
16217c922b88SBarry Smith   b->donotstash  = PETSC_FALSE;
162244cd7ae7SLois Curfman McInnes   b->colmap      = 0;
162344cd7ae7SLois Curfman McInnes   b->garray      = 0;
16247c922b88SBarry Smith   b->roworiented = PETSC_TRUE;
16258a729477SBarry Smith 
16261eb62cbbSBarry Smith   /* stuff used for matrix vector multiply */
16277c922b88SBarry Smith   b->lvec      = PETSC_NULL;
16287c922b88SBarry Smith   b->Mvctx     = PETSC_NULL;
16298a729477SBarry Smith 
16307a0afa10SBarry Smith   /* stuff for MatGetRow() */
163144cd7ae7SLois Curfman McInnes   b->rowindices   = 0;
163244cd7ae7SLois Curfman McInnes   b->rowvalues    = 0;
163344cd7ae7SLois Curfman McInnes   b->getrowactive = PETSC_FALSE;
16347a0afa10SBarry Smith 
1635f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
16362e8a6d31SBarry Smith                                      "MatStoreValues_MPIAIJ",
16370c97f09dSSatish Balay                                      MatStoreValues_MPIAIJ);CHKERRQ(ierr);
1638f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
16392e8a6d31SBarry Smith                                      "MatRetrieveValues_MPIAIJ",
16400c97f09dSSatish Balay                                      MatRetrieveValues_MPIAIJ);CHKERRQ(ierr);
1641f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetDiagonalBlock_C",
16425ef9f2a5SBarry Smith                                      "MatGetDiagonalBlock_MPIAIJ",
16430c97f09dSSatish Balay                                      MatGetDiagonalBlock_MPIAIJ);CHKERRQ(ierr);
16443a40ed3dSBarry Smith   PetscFunctionReturn(0);
1645d6dfbf8fSBarry Smith }
1646273d9f13SBarry Smith EXTERN_C_END
1647c74985f6SBarry Smith 
16484a2ae208SSatish Balay #undef __FUNCT__
16494a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_MPIAIJ"
16502e8a6d31SBarry Smith int MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
1651d6dfbf8fSBarry Smith {
1652d6dfbf8fSBarry Smith   Mat        mat;
1653416022c9SBarry Smith   Mat_MPIAIJ *a,*oldmat = (Mat_MPIAIJ*)matin->data;
165416d6aa21SSatish Balay   int        ierr;
1655d6dfbf8fSBarry Smith 
16563a40ed3dSBarry Smith   PetscFunctionBegin;
1657416022c9SBarry Smith   *newmat       = 0;
1658273d9f13SBarry Smith   ierr = MatCreate(matin->comm,matin->m,matin->n,matin->M,matin->N,&mat);CHKERRQ(ierr);
1659273d9f13SBarry Smith   ierr = MatSetType(mat,MATMPIAIJ);CHKERRQ(ierr);
1660273d9f13SBarry Smith   a    = (Mat_MPIAIJ*)mat->data;
1661549d3d68SSatish Balay   ierr              = PetscMemcpy(mat->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
1662d6dfbf8fSBarry Smith   mat->factor       = matin->factor;
1663c456f294SBarry Smith   mat->assembled    = PETSC_TRUE;
1664e7641de0SSatish Balay   mat->insertmode   = NOT_SET_VALUES;
1665273d9f13SBarry Smith   mat->preallocated = PETSC_TRUE;
1666d6dfbf8fSBarry Smith 
1667416022c9SBarry Smith   a->rstart       = oldmat->rstart;
1668416022c9SBarry Smith   a->rend         = oldmat->rend;
1669416022c9SBarry Smith   a->cstart       = oldmat->cstart;
1670416022c9SBarry Smith   a->cend         = oldmat->cend;
167117699dbbSLois Curfman McInnes   a->size         = oldmat->size;
167217699dbbSLois Curfman McInnes   a->rank         = oldmat->rank;
1673e7641de0SSatish Balay   a->donotstash   = oldmat->donotstash;
1674e7641de0SSatish Balay   a->roworiented  = oldmat->roworiented;
1675e7641de0SSatish Balay   a->rowindices   = 0;
1676bcd2baecSBarry Smith   a->rowvalues    = 0;
1677bcd2baecSBarry Smith   a->getrowactive = PETSC_FALSE;
1678d6dfbf8fSBarry Smith 
1679549d3d68SSatish Balay   ierr       = PetscMemcpy(a->rowners,oldmat->rowners,2*(a->size+2)*sizeof(int));CHKERRQ(ierr);
16808798bf22SSatish Balay   ierr       = MatStashCreate_Private(matin->comm,1,&mat->stash);CHKERRQ(ierr);
16812ee70a88SLois Curfman McInnes   if (oldmat->colmap) {
1682aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
16830f5bd95cSBarry Smith     ierr = PetscTableCreateCopy(oldmat->colmap,&a->colmap);CHKERRQ(ierr);
1684b1fc9764SSatish Balay #else
1685b0a32e0cSBarry Smith     ierr = PetscMalloc((mat->N)*sizeof(int),&a->colmap);CHKERRQ(ierr);
1686b0a32e0cSBarry Smith     PetscLogObjectMemory(mat,(mat->N)*sizeof(int));
1687273d9f13SBarry Smith     ierr      = PetscMemcpy(a->colmap,oldmat->colmap,(mat->N)*sizeof(int));CHKERRQ(ierr);
1688b1fc9764SSatish Balay #endif
1689416022c9SBarry Smith   } else a->colmap = 0;
16903f41c07dSBarry Smith   if (oldmat->garray) {
169116d6aa21SSatish Balay     int len;
1692273d9f13SBarry Smith     len  = oldmat->B->n;
1693b0a32e0cSBarry Smith     ierr = PetscMalloc((len+1)*sizeof(int),&a->garray);CHKERRQ(ierr);
1694b0a32e0cSBarry Smith     PetscLogObjectMemory(mat,len*sizeof(int));
1695549d3d68SSatish Balay     if (len) { ierr = PetscMemcpy(a->garray,oldmat->garray,len*sizeof(int));CHKERRQ(ierr); }
1696416022c9SBarry Smith   } else a->garray = 0;
1697d6dfbf8fSBarry Smith 
1698416022c9SBarry Smith   ierr =  VecDuplicate(oldmat->lvec,&a->lvec);CHKERRQ(ierr);
1699b0a32e0cSBarry Smith   PetscLogObjectParent(mat,a->lvec);
1700a56f8943SBarry Smith   ierr =  VecScatterCopy(oldmat->Mvctx,&a->Mvctx);CHKERRQ(ierr);
1701b0a32e0cSBarry Smith   PetscLogObjectParent(mat,a->Mvctx);
17022e8a6d31SBarry Smith   ierr =  MatDuplicate(oldmat->A,cpvalues,&a->A);CHKERRQ(ierr);
1703b0a32e0cSBarry Smith   PetscLogObjectParent(mat,a->A);
17042e8a6d31SBarry Smith   ierr =  MatDuplicate(oldmat->B,cpvalues,&a->B);CHKERRQ(ierr);
1705b0a32e0cSBarry Smith   PetscLogObjectParent(mat,a->B);
1706b0a32e0cSBarry Smith   ierr = PetscFListDuplicate(matin->qlist,&mat->qlist);CHKERRQ(ierr);
17078a729477SBarry Smith   *newmat = mat;
17083a40ed3dSBarry Smith   PetscFunctionReturn(0);
17098a729477SBarry Smith }
1710416022c9SBarry Smith 
1711e090d566SSatish Balay #include "petscsys.h"
1712416022c9SBarry Smith 
1713273d9f13SBarry Smith EXTERN_C_BEGIN
17144a2ae208SSatish Balay #undef __FUNCT__
17154a2ae208SSatish Balay #define __FUNCT__ "MatLoad_MPIAIJ"
1716b0a32e0cSBarry Smith int MatLoad_MPIAIJ(PetscViewer viewer,MatType type,Mat *newmat)
1717416022c9SBarry Smith {
1718d65a2f8fSBarry Smith   Mat          A;
1719d65a2f8fSBarry Smith   Scalar       *vals,*svals;
172019bcc07fSBarry Smith   MPI_Comm     comm = ((PetscObject)viewer)->comm;
1721416022c9SBarry Smith   MPI_Status   status;
17223a40ed3dSBarry Smith   int          i,nz,ierr,j,rstart,rend,fd;
172317699dbbSLois Curfman McInnes   int          header[4],rank,size,*rowlengths = 0,M,N,m,*rowners,maxnz,*cols;
1724d65a2f8fSBarry Smith   int          *ourlens,*sndcounts = 0,*procsnz = 0,*offlens,jj,*mycols,*smycols;
1725b362ba68SBarry Smith   int          tag = ((PetscObject)viewer)->tag,cend,cstart,n;
1726416022c9SBarry Smith 
17273a40ed3dSBarry Smith   PetscFunctionBegin;
17281dab6e02SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
17291dab6e02SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
173017699dbbSLois Curfman McInnes   if (!rank) {
1731b0a32e0cSBarry Smith     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
17320752156aSBarry Smith     ierr = PetscBinaryRead(fd,(char *)header,4,PETSC_INT);CHKERRQ(ierr);
173329bbc08cSBarry Smith     if (header[0] != MAT_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
1734d64ed03dSBarry Smith     if (header[3] < 0) {
173529bbc08cSBarry Smith       SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix in special format on disk, cannot load as MPIAIJ");
1736d64ed03dSBarry Smith     }
17376c5fab8fSBarry Smith   }
17386c5fab8fSBarry Smith 
1739ca161407SBarry Smith   ierr = MPI_Bcast(header+1,3,MPI_INT,0,comm);CHKERRQ(ierr);
1740416022c9SBarry Smith   M = header[1]; N = header[2];
1741416022c9SBarry Smith   /* determine ownership of all rows */
174217699dbbSLois Curfman McInnes   m = M/size + ((M % size) > rank);
1743b0a32e0cSBarry Smith   ierr = PetscMalloc((size+2)*sizeof(int),&rowners);CHKERRQ(ierr);
1744ca161407SBarry Smith   ierr = MPI_Allgather(&m,1,MPI_INT,rowners+1,1,MPI_INT,comm);CHKERRQ(ierr);
1745416022c9SBarry Smith   rowners[0] = 0;
174617699dbbSLois Curfman McInnes   for (i=2; i<=size; i++) {
1747416022c9SBarry Smith     rowners[i] += rowners[i-1];
1748416022c9SBarry Smith   }
174917699dbbSLois Curfman McInnes   rstart = rowners[rank];
175017699dbbSLois Curfman McInnes   rend   = rowners[rank+1];
1751416022c9SBarry Smith 
1752416022c9SBarry Smith   /* distribute row lengths to all processors */
1753b0a32e0cSBarry Smith   ierr    = PetscMalloc(2*(rend-rstart+1)*sizeof(int),&ourlens);CHKERRQ(ierr);
1754416022c9SBarry Smith   offlens = ourlens + (rend-rstart);
175517699dbbSLois Curfman McInnes   if (!rank) {
1756b0a32e0cSBarry Smith     ierr = PetscMalloc(M*sizeof(int),&rowlengths);CHKERRQ(ierr);
17570752156aSBarry Smith     ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
1758b0a32e0cSBarry Smith     ierr = PetscMalloc(size*sizeof(int),&sndcounts);CHKERRQ(ierr);
175917699dbbSLois Curfman McInnes     for (i=0; i<size; i++) sndcounts[i] = rowners[i+1] - rowners[i];
1760ca161407SBarry Smith     ierr = MPI_Scatterv(rowlengths,sndcounts,rowners,MPI_INT,ourlens,rend-rstart,MPI_INT,0,comm);CHKERRQ(ierr);
1761606d414cSSatish Balay     ierr = PetscFree(sndcounts);CHKERRQ(ierr);
17623a40ed3dSBarry Smith   } else {
1763ca161407SBarry Smith     ierr = MPI_Scatterv(0,0,0,MPI_INT,ourlens,rend-rstart,MPI_INT,0,comm);CHKERRQ(ierr);
1764416022c9SBarry Smith   }
1765416022c9SBarry Smith 
176617699dbbSLois Curfman McInnes   if (!rank) {
1767416022c9SBarry Smith     /* calculate the number of nonzeros on each processor */
1768b0a32e0cSBarry Smith     ierr = PetscMalloc(size*sizeof(int),&procsnz);CHKERRQ(ierr);
1769549d3d68SSatish Balay     ierr = PetscMemzero(procsnz,size*sizeof(int));CHKERRQ(ierr);
177017699dbbSLois Curfman McInnes     for (i=0; i<size; i++) {
1771416022c9SBarry Smith       for (j=rowners[i]; j< rowners[i+1]; j++) {
1772416022c9SBarry Smith         procsnz[i] += rowlengths[j];
1773416022c9SBarry Smith       }
1774416022c9SBarry Smith     }
1775606d414cSSatish Balay     ierr = PetscFree(rowlengths);CHKERRQ(ierr);
1776416022c9SBarry Smith 
1777416022c9SBarry Smith     /* determine max buffer needed and allocate it */
1778416022c9SBarry Smith     maxnz = 0;
177917699dbbSLois Curfman McInnes     for (i=0; i<size; i++) {
17800452661fSBarry Smith       maxnz = PetscMax(maxnz,procsnz[i]);
1781416022c9SBarry Smith     }
1782b0a32e0cSBarry Smith     ierr = PetscMalloc(maxnz*sizeof(int),&cols);CHKERRQ(ierr);
1783416022c9SBarry Smith 
1784416022c9SBarry Smith     /* read in my part of the matrix column indices  */
1785416022c9SBarry Smith     nz   = procsnz[0];
1786b0a32e0cSBarry Smith     ierr = PetscMalloc(nz*sizeof(int),&mycols);CHKERRQ(ierr);
17870752156aSBarry Smith     ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr);
1788d65a2f8fSBarry Smith 
1789d65a2f8fSBarry Smith     /* read in every one elses and ship off */
179017699dbbSLois Curfman McInnes     for (i=1; i<size; i++) {
1791d65a2f8fSBarry Smith       nz   = procsnz[i];
17920752156aSBarry Smith       ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr);
1793ca161407SBarry Smith       ierr = MPI_Send(cols,nz,MPI_INT,i,tag,comm);CHKERRQ(ierr);
1794d65a2f8fSBarry Smith     }
1795606d414cSSatish Balay     ierr = PetscFree(cols);CHKERRQ(ierr);
17963a40ed3dSBarry Smith   } else {
1797416022c9SBarry Smith     /* determine buffer space needed for message */
1798416022c9SBarry Smith     nz = 0;
1799416022c9SBarry Smith     for (i=0; i<m; i++) {
1800416022c9SBarry Smith       nz += ourlens[i];
1801416022c9SBarry Smith     }
1802b0a32e0cSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(int),&mycols);CHKERRQ(ierr);
1803416022c9SBarry Smith 
1804416022c9SBarry Smith     /* receive message of column indices*/
1805ca161407SBarry Smith     ierr = MPI_Recv(mycols,nz,MPI_INT,0,tag,comm,&status);CHKERRQ(ierr);
1806ca161407SBarry Smith     ierr = MPI_Get_count(&status,MPI_INT,&maxnz);CHKERRQ(ierr);
180729bbc08cSBarry Smith     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
1808416022c9SBarry Smith   }
1809416022c9SBarry Smith 
1810b362ba68SBarry Smith   /* determine column ownership if matrix is not square */
1811b362ba68SBarry Smith   if (N != M) {
1812b362ba68SBarry Smith     n      = N/size + ((N % size) > rank);
1813b362ba68SBarry Smith     ierr   = MPI_Scan(&n,&cend,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr);
1814b362ba68SBarry Smith     cstart = cend - n;
1815b362ba68SBarry Smith   } else {
1816b362ba68SBarry Smith     cstart = rstart;
1817b362ba68SBarry Smith     cend   = rend;
1818fb2e594dSBarry Smith     n      = cend - cstart;
1819b362ba68SBarry Smith   }
1820b362ba68SBarry Smith 
1821416022c9SBarry Smith   /* loop over local rows, determining number of off diagonal entries */
1822549d3d68SSatish Balay   ierr = PetscMemzero(offlens,m*sizeof(int));CHKERRQ(ierr);
1823416022c9SBarry Smith   jj = 0;
1824416022c9SBarry Smith   for (i=0; i<m; i++) {
1825416022c9SBarry Smith     for (j=0; j<ourlens[i]; j++) {
1826b362ba68SBarry Smith       if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++;
1827416022c9SBarry Smith       jj++;
1828416022c9SBarry Smith     }
1829416022c9SBarry Smith   }
1830d65a2f8fSBarry Smith 
1831d65a2f8fSBarry Smith   /* create our matrix */
1832416022c9SBarry Smith   for (i=0; i<m; i++) {
1833416022c9SBarry Smith     ourlens[i] -= offlens[i];
1834416022c9SBarry Smith   }
1835b362ba68SBarry Smith   ierr = MatCreateMPIAIJ(comm,m,n,M,N,0,ourlens,0,offlens,newmat);CHKERRQ(ierr);
1836d65a2f8fSBarry Smith   A = *newmat;
1837fb2e594dSBarry Smith   ierr = MatSetOption(A,MAT_COLUMNS_SORTED);CHKERRQ(ierr);
1838d65a2f8fSBarry Smith   for (i=0; i<m; i++) {
1839d65a2f8fSBarry Smith     ourlens[i] += offlens[i];
1840d65a2f8fSBarry Smith   }
1841416022c9SBarry Smith 
184217699dbbSLois Curfman McInnes   if (!rank) {
1843b0a32e0cSBarry Smith     ierr = PetscMalloc(maxnz*sizeof(Scalar),&vals);CHKERRQ(ierr);
1844416022c9SBarry Smith 
1845416022c9SBarry Smith     /* read in my part of the matrix numerical values  */
1846416022c9SBarry Smith     nz   = procsnz[0];
18470752156aSBarry Smith     ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
1848d65a2f8fSBarry Smith 
1849d65a2f8fSBarry Smith     /* insert into matrix */
1850d65a2f8fSBarry Smith     jj      = rstart;
1851d65a2f8fSBarry Smith     smycols = mycols;
1852d65a2f8fSBarry Smith     svals   = vals;
1853d65a2f8fSBarry Smith     for (i=0; i<m; i++) {
1854d65a2f8fSBarry Smith       ierr = MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
1855d65a2f8fSBarry Smith       smycols += ourlens[i];
1856d65a2f8fSBarry Smith       svals   += ourlens[i];
1857d65a2f8fSBarry Smith       jj++;
1858416022c9SBarry Smith     }
1859416022c9SBarry Smith 
1860d65a2f8fSBarry Smith     /* read in other processors and ship out */
186117699dbbSLois Curfman McInnes     for (i=1; i<size; i++) {
1862416022c9SBarry Smith       nz   = procsnz[i];
18630752156aSBarry Smith       ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
1864ca161407SBarry Smith       ierr = MPI_Send(vals,nz,MPIU_SCALAR,i,A->tag,comm);CHKERRQ(ierr);
1865416022c9SBarry Smith     }
1866606d414cSSatish Balay     ierr = PetscFree(procsnz);CHKERRQ(ierr);
18673a40ed3dSBarry Smith   } else {
1868d65a2f8fSBarry Smith     /* receive numeric values */
1869b0a32e0cSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(Scalar),&vals);CHKERRQ(ierr);
1870416022c9SBarry Smith 
1871d65a2f8fSBarry Smith     /* receive message of values*/
1872ca161407SBarry Smith     ierr = MPI_Recv(vals,nz,MPIU_SCALAR,0,A->tag,comm,&status);CHKERRQ(ierr);
1873ca161407SBarry Smith     ierr = MPI_Get_count(&status,MPIU_SCALAR,&maxnz);CHKERRQ(ierr);
187429bbc08cSBarry Smith     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
1875d65a2f8fSBarry Smith 
1876d65a2f8fSBarry Smith     /* insert into matrix */
1877d65a2f8fSBarry Smith     jj      = rstart;
1878d65a2f8fSBarry Smith     smycols = mycols;
1879d65a2f8fSBarry Smith     svals   = vals;
1880d65a2f8fSBarry Smith     for (i=0; i<m; i++) {
1881d65a2f8fSBarry Smith       ierr     = MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
1882d65a2f8fSBarry Smith       smycols += ourlens[i];
1883d65a2f8fSBarry Smith       svals   += ourlens[i];
1884d65a2f8fSBarry Smith       jj++;
1885d65a2f8fSBarry Smith     }
1886d65a2f8fSBarry Smith   }
1887606d414cSSatish Balay   ierr = PetscFree(ourlens);CHKERRQ(ierr);
1888606d414cSSatish Balay   ierr = PetscFree(vals);CHKERRQ(ierr);
1889606d414cSSatish Balay   ierr = PetscFree(mycols);CHKERRQ(ierr);
1890606d414cSSatish Balay   ierr = PetscFree(rowners);CHKERRQ(ierr);
1891d65a2f8fSBarry Smith 
18926d4a8577SBarry Smith   ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18936d4a8577SBarry Smith   ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18943a40ed3dSBarry Smith   PetscFunctionReturn(0);
1895416022c9SBarry Smith }
1896273d9f13SBarry Smith EXTERN_C_END
1897a0ff6018SBarry Smith 
18984a2ae208SSatish Balay #undef __FUNCT__
18994a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_MPIAIJ"
1900a0ff6018SBarry Smith /*
190129da9460SBarry Smith     Not great since it makes two copies of the submatrix, first an SeqAIJ
190229da9460SBarry Smith   in local and then by concatenating the local matrices the end result.
190329da9460SBarry Smith   Writing it directly would be much like MatGetSubMatrices_MPIAIJ()
1904a0ff6018SBarry Smith */
19057b2a1423SBarry Smith int MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,int csize,MatReuse call,Mat *newmat)
1906a0ff6018SBarry Smith {
190700e6dbe6SBarry Smith   int        ierr,i,m,n,rstart,row,rend,nz,*cwork,size,rank,j;
19087e2c5f70SBarry Smith   int        *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend;
1909fee21e36SBarry Smith   Mat        *local,M,Mreuse;
191000e6dbe6SBarry Smith   Scalar     *vwork,*aa;
191100e6dbe6SBarry Smith   MPI_Comm   comm = mat->comm;
191200e6dbe6SBarry Smith   Mat_SeqAIJ *aij;
19137e2c5f70SBarry Smith 
1914a0ff6018SBarry Smith 
1915a0ff6018SBarry Smith   PetscFunctionBegin;
19161dab6e02SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
19171dab6e02SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
191800e6dbe6SBarry Smith 
1919fee21e36SBarry Smith   if (call ==  MAT_REUSE_MATRIX) {
1920fee21e36SBarry Smith     ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);CHKERRQ(ierr);
192129bbc08cSBarry Smith     if (!Mreuse) SETERRQ(1,"Submatrix passed in was not used before, cannot reuse");
1922fee21e36SBarry Smith     local = &Mreuse;
1923fee21e36SBarry Smith     ierr  = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&local);CHKERRQ(ierr);
1924fee21e36SBarry Smith   } else {
1925a0ff6018SBarry Smith     ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr);
1926fee21e36SBarry Smith     Mreuse = *local;
1927606d414cSSatish Balay     ierr = PetscFree(local);CHKERRQ(ierr);
1928fee21e36SBarry Smith   }
1929a0ff6018SBarry Smith 
1930a0ff6018SBarry Smith   /*
1931a0ff6018SBarry Smith       m - number of local rows
1932a0ff6018SBarry Smith       n - number of columns (same on all processors)
1933a0ff6018SBarry Smith       rstart - first row in new global matrix generated
1934a0ff6018SBarry Smith   */
1935fee21e36SBarry Smith   ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr);
1936a0ff6018SBarry Smith   if (call == MAT_INITIAL_MATRIX) {
1937fee21e36SBarry Smith     aij = (Mat_SeqAIJ*)(Mreuse)->data;
193829bbc08cSBarry Smith     if (aij->indexshift) SETERRQ(PETSC_ERR_SUP,"No support for index shifted matrix");
193900e6dbe6SBarry Smith     ii  = aij->i;
194000e6dbe6SBarry Smith     jj  = aij->j;
194100e6dbe6SBarry Smith 
1942a0ff6018SBarry Smith     /*
194300e6dbe6SBarry Smith         Determine the number of non-zeros in the diagonal and off-diagonal
194400e6dbe6SBarry Smith         portions of the matrix in order to do correct preallocation
1945a0ff6018SBarry Smith     */
194600e6dbe6SBarry Smith 
194700e6dbe6SBarry Smith     /* first get start and end of "diagonal" columns */
19486a6a5d1dSBarry Smith     if (csize == PETSC_DECIDE) {
194900e6dbe6SBarry Smith       nlocal = n/size + ((n % size) > rank);
19506a6a5d1dSBarry Smith     } else {
19516a6a5d1dSBarry Smith       nlocal = csize;
19526a6a5d1dSBarry Smith     }
1953ca161407SBarry Smith     ierr   = MPI_Scan(&nlocal,&rend,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr);
195400e6dbe6SBarry Smith     rstart = rend - nlocal;
19556a6a5d1dSBarry Smith     if (rank == size - 1 && rend != n) {
195629bbc08cSBarry Smith       SETERRQ(1,"Local column sizes do not add up to total number of columns");
19576a6a5d1dSBarry Smith     }
195800e6dbe6SBarry Smith 
195900e6dbe6SBarry Smith     /* next, compute all the lengths */
1960b0a32e0cSBarry Smith     ierr  = PetscMalloc((2*m+1)*sizeof(int),&dlens);CHKERRQ(ierr);
196100e6dbe6SBarry Smith     olens = dlens + m;
196200e6dbe6SBarry Smith     for (i=0; i<m; i++) {
196300e6dbe6SBarry Smith       jend = ii[i+1] - ii[i];
196400e6dbe6SBarry Smith       olen = 0;
196500e6dbe6SBarry Smith       dlen = 0;
196600e6dbe6SBarry Smith       for (j=0; j<jend; j++) {
196700e6dbe6SBarry Smith         if (*jj < rstart || *jj >= rend) olen++;
196800e6dbe6SBarry Smith         else dlen++;
196900e6dbe6SBarry Smith         jj++;
197000e6dbe6SBarry Smith       }
197100e6dbe6SBarry Smith       olens[i] = olen;
197200e6dbe6SBarry Smith       dlens[i] = dlen;
197300e6dbe6SBarry Smith     }
19742d207970SBarry Smith     ierr = MatCreateMPIAIJ(comm,m,nlocal,PETSC_DECIDE,n,0,dlens,0,olens,&M);CHKERRQ(ierr);
1975606d414cSSatish Balay     ierr = PetscFree(dlens);CHKERRQ(ierr);
1976a0ff6018SBarry Smith   } else {
1977a0ff6018SBarry Smith     int ml,nl;
1978a0ff6018SBarry Smith 
1979a0ff6018SBarry Smith     M = *newmat;
1980a0ff6018SBarry Smith     ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr);
198129bbc08cSBarry Smith     if (ml != m) SETERRQ(PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
1982a0ff6018SBarry Smith     ierr = MatZeroEntries(M);CHKERRQ(ierr);
1983c48de900SBarry Smith     /*
1984c48de900SBarry Smith          The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
1985c48de900SBarry Smith        rather than the slower MatSetValues().
1986c48de900SBarry Smith     */
1987c48de900SBarry Smith     M->was_assembled = PETSC_TRUE;
1988c48de900SBarry Smith     M->assembled     = PETSC_FALSE;
1989a0ff6018SBarry Smith   }
1990a0ff6018SBarry Smith   ierr = MatGetOwnershipRange(M,&rstart,&rend);CHKERRQ(ierr);
1991fee21e36SBarry Smith   aij = (Mat_SeqAIJ*)(Mreuse)->data;
199229bbc08cSBarry Smith   if (aij->indexshift) SETERRQ(PETSC_ERR_SUP,"No support for index shifted matrix");
199300e6dbe6SBarry Smith   ii  = aij->i;
199400e6dbe6SBarry Smith   jj  = aij->j;
199500e6dbe6SBarry Smith   aa  = aij->a;
1996a0ff6018SBarry Smith   for (i=0; i<m; i++) {
1997a0ff6018SBarry Smith     row   = rstart + i;
199800e6dbe6SBarry Smith     nz    = ii[i+1] - ii[i];
199900e6dbe6SBarry Smith     cwork = jj;     jj += nz;
200000e6dbe6SBarry Smith     vwork = aa;     aa += nz;
20018c638d02SBarry Smith     ierr = MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr);
2002a0ff6018SBarry Smith   }
2003a0ff6018SBarry Smith 
2004a0ff6018SBarry Smith   ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2005a0ff6018SBarry Smith   ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2006a0ff6018SBarry Smith   *newmat = M;
2007fee21e36SBarry Smith 
2008fee21e36SBarry Smith   /* save submatrix used in processor for next request */
2009fee21e36SBarry Smith   if (call ==  MAT_INITIAL_MATRIX) {
2010fee21e36SBarry Smith     ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr);
2011fee21e36SBarry Smith     ierr = PetscObjectDereference((PetscObject)Mreuse);CHKERRQ(ierr);
2012fee21e36SBarry Smith   }
2013fee21e36SBarry Smith 
2014a0ff6018SBarry Smith   PetscFunctionReturn(0);
2015a0ff6018SBarry Smith }
2016273d9f13SBarry Smith 
20174a2ae208SSatish Balay #undef __FUNCT__
20184a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJSetPreallocation"
2019273d9f13SBarry Smith /*@C
2020273d9f13SBarry Smith    MatMPIAIJSetPreallocation - Creates a sparse parallel matrix in AIJ format
2021273d9f13SBarry Smith    (the default parallel PETSc format).  For good matrix assembly performance
2022273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameters
2023273d9f13SBarry Smith    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
2024273d9f13SBarry Smith    performance can be increased by more than a factor of 50.
2025273d9f13SBarry Smith 
2026273d9f13SBarry Smith    Collective on MPI_Comm
2027273d9f13SBarry Smith 
2028273d9f13SBarry Smith    Input Parameters:
2029273d9f13SBarry Smith +  A - the matrix
2030273d9f13SBarry Smith .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
2031273d9f13SBarry Smith            (same value is used for all local rows)
2032273d9f13SBarry Smith .  d_nnz - array containing the number of nonzeros in the various rows of the
2033273d9f13SBarry Smith            DIAGONAL portion of the local submatrix (possibly different for each row)
2034273d9f13SBarry Smith            or PETSC_NULL, if d_nz is used to specify the nonzero structure.
2035273d9f13SBarry Smith            The size of this array is equal to the number of local rows, i.e 'm'.
2036273d9f13SBarry Smith            You must leave room for the diagonal entry even if it is zero.
2037273d9f13SBarry Smith .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
2038273d9f13SBarry Smith            submatrix (same value is used for all local rows).
2039273d9f13SBarry Smith -  o_nnz - array containing the number of nonzeros in the various rows of the
2040273d9f13SBarry Smith            OFF-DIAGONAL portion of the local submatrix (possibly different for
2041273d9f13SBarry Smith            each row) or PETSC_NULL, if o_nz is used to specify the nonzero
2042273d9f13SBarry Smith            structure. The size of this array is equal to the number
2043273d9f13SBarry Smith            of local rows, i.e 'm'.
2044273d9f13SBarry Smith 
2045273d9f13SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
2046273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2047273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2048273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users manual for details.
2049273d9f13SBarry Smith 
2050273d9f13SBarry Smith    The user MUST specify either the local or global matrix dimensions
2051273d9f13SBarry Smith    (possibly both).
2052273d9f13SBarry Smith 
2053273d9f13SBarry Smith    The parallel matrix is partitioned such that the first m0 rows belong to
2054273d9f13SBarry Smith    process 0, the next m1 rows belong to process 1, the next m2 rows belong
2055273d9f13SBarry Smith    to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.
2056273d9f13SBarry Smith 
2057273d9f13SBarry Smith    The DIAGONAL portion of the local submatrix of a processor can be defined
2058273d9f13SBarry Smith    as the submatrix which is obtained by extraction the part corresponding
2059273d9f13SBarry Smith    to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the
2060273d9f13SBarry Smith    first row that belongs to the processor, and r2 is the last row belonging
2061273d9f13SBarry Smith    to the this processor. This is a square mxm matrix. The remaining portion
2062273d9f13SBarry Smith    of the local submatrix (mxN) constitute the OFF-DIAGONAL portion.
2063273d9f13SBarry Smith 
2064273d9f13SBarry Smith    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
2065273d9f13SBarry Smith 
2066273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible.
2067273d9f13SBarry Smith    We search for consecutive rows with the same nonzero structure, thereby
2068273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2069273d9f13SBarry Smith 
2070273d9f13SBarry Smith    Options Database Keys:
2071273d9f13SBarry Smith +  -mat_aij_no_inode  - Do not use inodes
2072273d9f13SBarry Smith .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2073273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2074273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2075273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2076273d9f13SBarry Smith 
2077273d9f13SBarry Smith    Example usage:
2078273d9f13SBarry Smith 
2079273d9f13SBarry Smith    Consider the following 8x8 matrix with 34 non-zero values, that is
2080273d9f13SBarry Smith    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
2081273d9f13SBarry Smith    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
2082273d9f13SBarry Smith    as follows:
2083273d9f13SBarry Smith 
2084273d9f13SBarry Smith .vb
2085273d9f13SBarry Smith             1  2  0  |  0  3  0  |  0  4
2086273d9f13SBarry Smith     Proc0   0  5  6  |  7  0  0  |  8  0
2087273d9f13SBarry Smith             9  0 10  | 11  0  0  | 12  0
2088273d9f13SBarry Smith     -------------------------------------
2089273d9f13SBarry Smith            13  0 14  | 15 16 17  |  0  0
2090273d9f13SBarry Smith     Proc1   0 18  0  | 19 20 21  |  0  0
2091273d9f13SBarry Smith             0  0  0  | 22 23  0  | 24  0
2092273d9f13SBarry Smith     -------------------------------------
2093273d9f13SBarry Smith     Proc2  25 26 27  |  0  0 28  | 29  0
2094273d9f13SBarry Smith            30  0  0  | 31 32 33  |  0 34
2095273d9f13SBarry Smith .ve
2096273d9f13SBarry Smith 
2097273d9f13SBarry Smith    This can be represented as a collection of submatrices as:
2098273d9f13SBarry Smith 
2099273d9f13SBarry Smith .vb
2100273d9f13SBarry Smith       A B C
2101273d9f13SBarry Smith       D E F
2102273d9f13SBarry Smith       G H I
2103273d9f13SBarry Smith .ve
2104273d9f13SBarry Smith 
2105273d9f13SBarry Smith    Where the submatrices A,B,C are owned by proc0, D,E,F are
2106273d9f13SBarry Smith    owned by proc1, G,H,I are owned by proc2.
2107273d9f13SBarry Smith 
2108273d9f13SBarry Smith    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2109273d9f13SBarry Smith    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2110273d9f13SBarry Smith    The 'M','N' parameters are 8,8, and have the same values on all procs.
2111273d9f13SBarry Smith 
2112273d9f13SBarry Smith    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
2113273d9f13SBarry Smith    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
2114273d9f13SBarry Smith    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
2115273d9f13SBarry Smith    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
2116273d9f13SBarry Smith    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
2117273d9f13SBarry Smith    matrix, ans [DF] as another SeqAIJ matrix.
2118273d9f13SBarry Smith 
2119273d9f13SBarry Smith    When d_nz, o_nz parameters are specified, d_nz storage elements are
2120273d9f13SBarry Smith    allocated for every row of the local diagonal submatrix, and o_nz
2121273d9f13SBarry Smith    storage locations are allocated for every row of the OFF-DIAGONAL submat.
2122273d9f13SBarry Smith    One way to choose d_nz and o_nz is to use the max nonzerors per local
2123273d9f13SBarry Smith    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
2124273d9f13SBarry Smith    In this case, the values of d_nz,o_nz are:
2125273d9f13SBarry Smith .vb
2126273d9f13SBarry Smith      proc0 : dnz = 2, o_nz = 2
2127273d9f13SBarry Smith      proc1 : dnz = 3, o_nz = 2
2128273d9f13SBarry Smith      proc2 : dnz = 1, o_nz = 4
2129273d9f13SBarry Smith .ve
2130273d9f13SBarry Smith    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
2131273d9f13SBarry Smith    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
2132273d9f13SBarry Smith    for proc3. i.e we are using 12+15+10=37 storage locations to store
2133273d9f13SBarry Smith    34 values.
2134273d9f13SBarry Smith 
2135273d9f13SBarry Smith    When d_nnz, o_nnz parameters are specified, the storage is specified
2136273d9f13SBarry Smith    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
2137273d9f13SBarry Smith    In the above case the values for d_nnz,o_nnz are:
2138273d9f13SBarry Smith .vb
2139273d9f13SBarry Smith      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
2140273d9f13SBarry Smith      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
2141273d9f13SBarry Smith      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
2142273d9f13SBarry Smith .ve
2143273d9f13SBarry Smith    Here the space allocated is sum of all the above values i.e 34, and
2144273d9f13SBarry Smith    hence pre-allocation is perfect.
2145273d9f13SBarry Smith 
2146273d9f13SBarry Smith    Level: intermediate
2147273d9f13SBarry Smith 
2148273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
2149273d9f13SBarry Smith 
2150273d9f13SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues()
2151273d9f13SBarry Smith @*/
2152273d9f13SBarry Smith int MatMPIAIJSetPreallocation(Mat B,int d_nz,int *d_nnz,int o_nz,int *o_nnz)
2153273d9f13SBarry Smith {
2154273d9f13SBarry Smith   Mat_MPIAIJ   *b;
2155273d9f13SBarry Smith   int          ierr,i;
2156273d9f13SBarry Smith   PetscTruth   flg2;
2157273d9f13SBarry Smith 
2158273d9f13SBarry Smith   PetscFunctionBegin;
2159273d9f13SBarry Smith   ierr = PetscTypeCompare((PetscObject)B,MATMPIAIJ,&flg2);CHKERRQ(ierr);
2160273d9f13SBarry Smith   if (!flg2) PetscFunctionReturn(0);
2161273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
2162435da068SBarry Smith   if (d_nz == PETSC_DEFAULT || d_nz == PETSC_DECIDE) d_nz = 5;
2163435da068SBarry Smith   if (o_nz == PETSC_DEFAULT || o_nz == PETSC_DECIDE) o_nz = 2;
2164435da068SBarry Smith   if (d_nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"d_nz cannot be less than 0: value %d",d_nz);
2165435da068SBarry Smith   if (o_nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"o_nz cannot be less than 0: value %d",o_nz);
2166273d9f13SBarry Smith   if (d_nnz) {
2167273d9f13SBarry Smith     for (i=0; i<B->m; i++) {
2168273d9f13SBarry 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]);
2169273d9f13SBarry Smith     }
2170273d9f13SBarry Smith   }
2171273d9f13SBarry Smith   if (o_nnz) {
2172273d9f13SBarry Smith     for (i=0; i<B->m; i++) {
2173273d9f13SBarry 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]);
2174273d9f13SBarry Smith     }
2175273d9f13SBarry Smith   }
2176273d9f13SBarry Smith   b = (Mat_MPIAIJ*)B->data;
2177273d9f13SBarry Smith 
2178273d9f13SBarry Smith   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,B->m,B->n,d_nz,d_nnz,&b->A);CHKERRQ(ierr);
2179b0a32e0cSBarry Smith   PetscLogObjectParent(B,b->A);
2180273d9f13SBarry Smith   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,B->m,B->N,o_nz,o_nnz,&b->B);CHKERRQ(ierr);
2181b0a32e0cSBarry Smith   PetscLogObjectParent(B,b->B);
2182273d9f13SBarry Smith 
2183273d9f13SBarry Smith   PetscFunctionReturn(0);
2184273d9f13SBarry Smith }
2185273d9f13SBarry Smith 
21864a2ae208SSatish Balay #undef __FUNCT__
21874a2ae208SSatish Balay #define __FUNCT__ "MatCreateMPIAIJ"
2188273d9f13SBarry Smith /*@C
2189273d9f13SBarry Smith    MatCreateMPIAIJ - Creates a sparse parallel matrix in AIJ format
2190273d9f13SBarry Smith    (the default parallel PETSc format).  For good matrix assembly performance
2191273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameters
2192273d9f13SBarry Smith    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
2193273d9f13SBarry Smith    performance can be increased by more than a factor of 50.
2194273d9f13SBarry Smith 
2195273d9f13SBarry Smith    Collective on MPI_Comm
2196273d9f13SBarry Smith 
2197273d9f13SBarry Smith    Input Parameters:
2198273d9f13SBarry Smith +  comm - MPI communicator
2199273d9f13SBarry Smith .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
2200273d9f13SBarry Smith            This value should be the same as the local size used in creating the
2201273d9f13SBarry Smith            y vector for the matrix-vector product y = Ax.
2202273d9f13SBarry Smith .  n - This value should be the same as the local size used in creating the
2203273d9f13SBarry Smith        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
2204273d9f13SBarry Smith        calculated if N is given) For square matrices n is almost always m.
2205273d9f13SBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
2206273d9f13SBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
2207273d9f13SBarry Smith .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
2208273d9f13SBarry Smith            (same value is used for all local rows)
2209273d9f13SBarry Smith .  d_nnz - array containing the number of nonzeros in the various rows of the
2210273d9f13SBarry Smith            DIAGONAL portion of the local submatrix (possibly different for each row)
2211273d9f13SBarry Smith            or PETSC_NULL, if d_nz is used to specify the nonzero structure.
2212273d9f13SBarry Smith            The size of this array is equal to the number of local rows, i.e 'm'.
2213273d9f13SBarry Smith            You must leave room for the diagonal entry even if it is zero.
2214273d9f13SBarry Smith .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
2215273d9f13SBarry Smith            submatrix (same value is used for all local rows).
2216273d9f13SBarry Smith -  o_nnz - array containing the number of nonzeros in the various rows of the
2217273d9f13SBarry Smith            OFF-DIAGONAL portion of the local submatrix (possibly different for
2218273d9f13SBarry Smith            each row) or PETSC_NULL, if o_nz is used to specify the nonzero
2219273d9f13SBarry Smith            structure. The size of this array is equal to the number
2220273d9f13SBarry Smith            of local rows, i.e 'm'.
2221273d9f13SBarry Smith 
2222273d9f13SBarry Smith    Output Parameter:
2223273d9f13SBarry Smith .  A - the matrix
2224273d9f13SBarry Smith 
2225273d9f13SBarry Smith    Notes:
2226273d9f13SBarry Smith    m,n,M,N parameters specify the size of the matrix, and its partitioning across
2227273d9f13SBarry Smith    processors, while d_nz,d_nnz,o_nz,o_nnz parameters specify the approximate
2228273d9f13SBarry Smith    storage requirements for this matrix.
2229273d9f13SBarry Smith 
2230273d9f13SBarry Smith    If PETSC_DECIDE or  PETSC_DETERMINE is used for a particular argument on one
2231273d9f13SBarry Smith    processor than it must be used on all processors that share the object for
2232273d9f13SBarry Smith    that argument.
2233273d9f13SBarry Smith 
2234273d9f13SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
2235273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2236273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2237273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users manual for details.
2238273d9f13SBarry Smith 
2239273d9f13SBarry Smith    The user MUST specify either the local or global matrix dimensions
2240273d9f13SBarry Smith    (possibly both).
2241273d9f13SBarry Smith 
2242273d9f13SBarry Smith    The parallel matrix is partitioned such that the first m0 rows belong to
2243273d9f13SBarry Smith    process 0, the next m1 rows belong to process 1, the next m2 rows belong
2244273d9f13SBarry Smith    to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.
2245273d9f13SBarry Smith 
2246273d9f13SBarry Smith    The DIAGONAL portion of the local submatrix of a processor can be defined
2247273d9f13SBarry Smith    as the submatrix which is obtained by extraction the part corresponding
2248273d9f13SBarry Smith    to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the
2249273d9f13SBarry Smith    first row that belongs to the processor, and r2 is the last row belonging
2250273d9f13SBarry Smith    to the this processor. This is a square mxm matrix. The remaining portion
2251273d9f13SBarry Smith    of the local submatrix (mxN) constitute the OFF-DIAGONAL portion.
2252273d9f13SBarry Smith 
2253273d9f13SBarry Smith    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
2254273d9f13SBarry Smith 
2255273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible.
2256273d9f13SBarry Smith    We search for consecutive rows with the same nonzero structure, thereby
2257273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2258273d9f13SBarry Smith 
2259273d9f13SBarry Smith    Options Database Keys:
2260273d9f13SBarry Smith +  -mat_aij_no_inode  - Do not use inodes
2261273d9f13SBarry Smith .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2262273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2263273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2264273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2265273d9f13SBarry Smith 
2266273d9f13SBarry Smith 
2267273d9f13SBarry Smith    Example usage:
2268273d9f13SBarry Smith 
2269273d9f13SBarry Smith    Consider the following 8x8 matrix with 34 non-zero values, that is
2270273d9f13SBarry Smith    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
2271273d9f13SBarry Smith    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
2272273d9f13SBarry Smith    as follows:
2273273d9f13SBarry Smith 
2274273d9f13SBarry Smith .vb
2275273d9f13SBarry Smith             1  2  0  |  0  3  0  |  0  4
2276273d9f13SBarry Smith     Proc0   0  5  6  |  7  0  0  |  8  0
2277273d9f13SBarry Smith             9  0 10  | 11  0  0  | 12  0
2278273d9f13SBarry Smith     -------------------------------------
2279273d9f13SBarry Smith            13  0 14  | 15 16 17  |  0  0
2280273d9f13SBarry Smith     Proc1   0 18  0  | 19 20 21  |  0  0
2281273d9f13SBarry Smith             0  0  0  | 22 23  0  | 24  0
2282273d9f13SBarry Smith     -------------------------------------
2283273d9f13SBarry Smith     Proc2  25 26 27  |  0  0 28  | 29  0
2284273d9f13SBarry Smith            30  0  0  | 31 32 33  |  0 34
2285273d9f13SBarry Smith .ve
2286273d9f13SBarry Smith 
2287273d9f13SBarry Smith    This can be represented as a collection of submatrices as:
2288273d9f13SBarry Smith 
2289273d9f13SBarry Smith .vb
2290273d9f13SBarry Smith       A B C
2291273d9f13SBarry Smith       D E F
2292273d9f13SBarry Smith       G H I
2293273d9f13SBarry Smith .ve
2294273d9f13SBarry Smith 
2295273d9f13SBarry Smith    Where the submatrices A,B,C are owned by proc0, D,E,F are
2296273d9f13SBarry Smith    owned by proc1, G,H,I are owned by proc2.
2297273d9f13SBarry Smith 
2298273d9f13SBarry Smith    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2299273d9f13SBarry Smith    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2300273d9f13SBarry Smith    The 'M','N' parameters are 8,8, and have the same values on all procs.
2301273d9f13SBarry Smith 
2302273d9f13SBarry Smith    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
2303273d9f13SBarry Smith    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
2304273d9f13SBarry Smith    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
2305273d9f13SBarry Smith    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
2306273d9f13SBarry Smith    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
2307273d9f13SBarry Smith    matrix, ans [DF] as another SeqAIJ matrix.
2308273d9f13SBarry Smith 
2309273d9f13SBarry Smith    When d_nz, o_nz parameters are specified, d_nz storage elements are
2310273d9f13SBarry Smith    allocated for every row of the local diagonal submatrix, and o_nz
2311273d9f13SBarry Smith    storage locations are allocated for every row of the OFF-DIAGONAL submat.
2312273d9f13SBarry Smith    One way to choose d_nz and o_nz is to use the max nonzerors per local
2313273d9f13SBarry Smith    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
2314273d9f13SBarry Smith    In this case, the values of d_nz,o_nz are:
2315273d9f13SBarry Smith .vb
2316273d9f13SBarry Smith      proc0 : dnz = 2, o_nz = 2
2317273d9f13SBarry Smith      proc1 : dnz = 3, o_nz = 2
2318273d9f13SBarry Smith      proc2 : dnz = 1, o_nz = 4
2319273d9f13SBarry Smith .ve
2320273d9f13SBarry Smith    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
2321273d9f13SBarry Smith    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
2322273d9f13SBarry Smith    for proc3. i.e we are using 12+15+10=37 storage locations to store
2323273d9f13SBarry Smith    34 values.
2324273d9f13SBarry Smith 
2325273d9f13SBarry Smith    When d_nnz, o_nnz parameters are specified, the storage is specified
2326273d9f13SBarry Smith    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
2327273d9f13SBarry Smith    In the above case the values for d_nnz,o_nnz are:
2328273d9f13SBarry Smith .vb
2329273d9f13SBarry Smith      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
2330273d9f13SBarry Smith      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
2331273d9f13SBarry Smith      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
2332273d9f13SBarry Smith .ve
2333273d9f13SBarry Smith    Here the space allocated is sum of all the above values i.e 34, and
2334273d9f13SBarry Smith    hence pre-allocation is perfect.
2335273d9f13SBarry Smith 
2336273d9f13SBarry Smith    Level: intermediate
2337273d9f13SBarry Smith 
2338273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
2339273d9f13SBarry Smith 
2340273d9f13SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues()
2341273d9f13SBarry Smith @*/
2342273d9f13SBarry 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)
2343273d9f13SBarry Smith {
2344273d9f13SBarry Smith   int ierr,size;
2345273d9f13SBarry Smith 
2346273d9f13SBarry Smith   PetscFunctionBegin;
2347273d9f13SBarry Smith   ierr = MatCreate(comm,m,n,M,N,A);CHKERRQ(ierr);
2348273d9f13SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
2349273d9f13SBarry Smith   if (size > 1) {
2350273d9f13SBarry Smith     ierr = MatSetType(*A,MATMPIAIJ);CHKERRQ(ierr);
2351273d9f13SBarry Smith     ierr = MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr);
2352273d9f13SBarry Smith   } else {
2353273d9f13SBarry Smith     ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2354273d9f13SBarry Smith     ierr = MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);CHKERRQ(ierr);
2355273d9f13SBarry Smith   }
2356273d9f13SBarry Smith   PetscFunctionReturn(0);
2357273d9f13SBarry Smith }
2358195d93cdSBarry Smith 
23594a2ae208SSatish Balay #undef __FUNCT__
23604a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJGetSeqAIJ"
2361195d93cdSBarry Smith int MatMPIAIJGetSeqAIJ(Mat A,Mat *Ad,Mat *Ao,int **colmap)
2362195d93cdSBarry Smith {
2363195d93cdSBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;
2364195d93cdSBarry Smith   PetscFunctionBegin;
2365195d93cdSBarry Smith   *Ad     = a->A;
2366195d93cdSBarry Smith   *Ao     = a->B;
2367195d93cdSBarry Smith   *colmap = a->garray;
2368195d93cdSBarry Smith   PetscFunctionReturn(0);
2369195d93cdSBarry Smith }
2370a2243be0SBarry Smith 
2371a2243be0SBarry Smith #undef __FUNCT__
2372a2243be0SBarry Smith #define __FUNCT__ "MatSetColoring_MPIAIJ"
2373a2243be0SBarry Smith int MatSetColoring_MPIAIJ(Mat A,ISColoring coloring)
2374a2243be0SBarry Smith {
2375a2243be0SBarry Smith   int        ierr;
2376a2243be0SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2377a2243be0SBarry Smith 
2378a2243be0SBarry Smith   PetscFunctionBegin;
2379a2243be0SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
2380a2243be0SBarry Smith     int        *allcolors,*colors,i;
2381a2243be0SBarry Smith     ISColoring ocoloring;
2382a2243be0SBarry Smith 
2383a2243be0SBarry Smith     /* set coloring for diagonal portion */
2384a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->A,coloring);CHKERRQ(ierr);
2385a2243be0SBarry Smith 
2386a2243be0SBarry Smith     /* set coloring for off-diagonal portion */
2387a2243be0SBarry Smith     ierr = ISAllGatherIndices(A->comm,coloring->n,coloring->colors,PETSC_NULL,&allcolors);CHKERRQ(ierr);
2388a2243be0SBarry Smith     ierr = PetscMalloc((a->B->n+1)*sizeof(int),&colors);CHKERRQ(ierr);
2389a2243be0SBarry Smith     for (i=0; i<a->B->n; i++) {
2390a2243be0SBarry Smith       colors[i] = allcolors[a->garray[i]];
2391a2243be0SBarry Smith     }
2392a2243be0SBarry Smith     ierr = PetscFree(allcolors);CHKERRQ(ierr);
2393a2243be0SBarry Smith     ierr = ISColoringCreate(MPI_COMM_SELF,a->B->n,colors,&ocoloring);CHKERRQ(ierr);
2394a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr);
2395a2243be0SBarry Smith     ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr);
2396a2243be0SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
2397a2243be0SBarry Smith     int        *colors,i,*larray;
2398a2243be0SBarry Smith     ISColoring ocoloring;
2399a2243be0SBarry Smith 
2400a2243be0SBarry Smith     /* set coloring for diagonal portion */
2401a2243be0SBarry Smith     ierr = PetscMalloc((a->A->n+1)*sizeof(int),&larray);CHKERRQ(ierr);
2402a2243be0SBarry Smith     for (i=0; i<a->A->n; i++) {
2403a2243be0SBarry Smith       larray[i] = i + a->cstart;
2404a2243be0SBarry Smith     }
2405a2243be0SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->A->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
2406a2243be0SBarry Smith     ierr = PetscMalloc((a->A->n+1)*sizeof(int),&colors);CHKERRQ(ierr);
2407a2243be0SBarry Smith     for (i=0; i<a->A->n; i++) {
2408a2243be0SBarry Smith       colors[i] = coloring->colors[larray[i]];
2409a2243be0SBarry Smith     }
2410a2243be0SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
2411a2243be0SBarry Smith     ierr = ISColoringCreate(MPI_COMM_SELF,a->A->n,colors,&ocoloring);CHKERRQ(ierr);
2412a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->A,ocoloring);CHKERRQ(ierr);
2413a2243be0SBarry Smith     ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr);
2414a2243be0SBarry Smith 
2415a2243be0SBarry Smith     /* set coloring for off-diagonal portion */
2416a2243be0SBarry Smith     ierr = PetscMalloc((a->B->n+1)*sizeof(int),&larray);CHKERRQ(ierr);
2417a2243be0SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->B->n,a->garray,PETSC_NULL,larray);CHKERRQ(ierr);
2418a2243be0SBarry Smith     ierr = PetscMalloc((a->B->n+1)*sizeof(int),&colors);CHKERRQ(ierr);
2419a2243be0SBarry Smith     for (i=0; i<a->B->n; i++) {
2420a2243be0SBarry Smith       colors[i] = coloring->colors[larray[i]];
2421a2243be0SBarry Smith     }
2422a2243be0SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
2423a2243be0SBarry Smith     ierr = ISColoringCreate(MPI_COMM_SELF,a->B->n,colors,&ocoloring);CHKERRQ(ierr);
2424a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr);
2425a2243be0SBarry Smith     ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr);
2426a2243be0SBarry Smith   } else {
2427a2243be0SBarry Smith     SETERRQ1(1,"No support ISColoringType %d",coloring->ctype);
2428a2243be0SBarry Smith   }
2429a2243be0SBarry Smith 
2430a2243be0SBarry Smith   PetscFunctionReturn(0);
2431a2243be0SBarry Smith }
2432a2243be0SBarry Smith 
2433a2243be0SBarry Smith #undef __FUNCT__
2434779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdic_MPIAIJ"
2435779c1a83SBarry Smith int MatSetValuesAdic_MPIAIJ(Mat A,void *advalues)
2436a2243be0SBarry Smith {
2437a2243be0SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2438a2243be0SBarry Smith   int        ierr;
2439a2243be0SBarry Smith 
2440a2243be0SBarry Smith   PetscFunctionBegin;
2441779c1a83SBarry Smith   ierr = MatSetValuesAdic_SeqAIJ(a->A,advalues);CHKERRQ(ierr);
2442779c1a83SBarry Smith   ierr = MatSetValuesAdic_SeqAIJ(a->B,advalues);CHKERRQ(ierr);
2443779c1a83SBarry Smith   PetscFunctionReturn(0);
2444779c1a83SBarry Smith }
2445779c1a83SBarry Smith 
2446779c1a83SBarry Smith #undef __FUNCT__
2447779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdifor_MPIAIJ"
2448779c1a83SBarry Smith int MatSetValuesAdifor_MPIAIJ(Mat A,int nl,void *advalues)
2449779c1a83SBarry Smith {
2450779c1a83SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2451779c1a83SBarry Smith   int        ierr;
2452779c1a83SBarry Smith 
2453779c1a83SBarry Smith   PetscFunctionBegin;
2454779c1a83SBarry Smith   ierr = MatSetValuesAdifor_SeqAIJ(a->A,nl,advalues);CHKERRQ(ierr);
2455779c1a83SBarry Smith   ierr = MatSetValuesAdifor_SeqAIJ(a->B,nl,advalues);CHKERRQ(ierr);
2456a2243be0SBarry Smith   PetscFunctionReturn(0);
2457a2243be0SBarry Smith }
2458