xref: /petsc/src/mat/impls/aij/mpi/mpiaij.c (revision 273d9f13de75c4ed17021f7f2c11eebb99d26f0d)
1*273d9f13SBarry Smith /*$Id: mpiaij.c,v 1.321 2000/09/28 21:11:06 bsmith Exp bsmith $*/
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 */
215615d1e5SSatish Balay #undef __FUNC__
22e03a110bSBarry Smith #define __FUNC__ /*<a name="CreateColmap_MPIAIJ_Private"></a>*/"CreateColmap_MPIAIJ_Private"
230a198c4cSBarry Smith int CreateColmap_MPIAIJ_Private(Mat mat)
249e25ed09SBarry Smith {
2544a69424SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
26*273d9f13SBarry Smith   int        n = aij->B->n,i,ierr;
27dbb450caSBarry Smith 
283a40ed3dSBarry Smith   PetscFunctionBegin;
29aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
30*273d9f13SBarry 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
35*273d9f13SBarry Smith   aij->colmap = (int*)PetscMalloc((mat->N+1)*sizeof(int));CHKPTRQ(aij->colmap);
36*273d9f13SBarry Smith   PLogObjectMemory(mat,mat->N*sizeof(int));
37*273d9f13SBarry 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 */ \
69*273d9f13SBarry 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 */ \
75*273d9f13SBarry Smith         len     = new_nz*(sizeof(int)+sizeof(Scalar))+(am+1)*sizeof(int); \
760520107fSSatish Balay         new_a   = (Scalar*)PetscMalloc(len);CHKPTRQ(new_a); \
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];} \
82*273d9f13SBarry 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; \
1020520107fSSatish Balay         PLogObjectMemory(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 */ \
143*273d9f13SBarry 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 */ \
149*273d9f13SBarry Smith         len     = new_nz*(sizeof(int)+sizeof(Scalar))+(bm+1)*sizeof(int); \
15030770e4dSSatish Balay         new_a   = (Scalar*)PetscMalloc(len);CHKPTRQ(new_a); \
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];} \
156*273d9f13SBarry 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; \
17674c639caSSatish Balay         PLogObjectMemory(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 
1925615d1e5SSatish Balay #undef __FUNC__
193b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"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;
200*273d9f13SBarry 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;
210*273d9f13SBarry 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)
221*273d9f13SBarry 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); */
232*273d9f13SBarry Smith         } else if (in[j] < 0) continue;
233aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g)
234*273d9f13SBarry 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 
2785615d1e5SSatish Balay #undef __FUNC__
279b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"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");
289*273d9f13SBarry 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");
294*273d9f13SBarry 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 
321bc5ccf88SSatish Balay #undef __FUNC__
322b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"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);
3435a655dc6SBarry Smith   PLogInfo(aij->A,"MatAssemblyBegin_MPIAIJ:Stash has %d entries, uses %d mallocs.\n",nstash,reallocs);
344bc5ccf88SSatish Balay   PetscFunctionReturn(0);
345bc5ccf88SSatish Balay }
346bc5ccf88SSatish Balay 
347bc5ccf88SSatish Balay 
348bc5ccf88SSatish Balay #undef __FUNC__
349b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"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 
4065615d1e5SSatish Balay #undef __FUNC__
407b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"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 
4195615d1e5SSatish Balay #undef __FUNC__
420b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"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;
4256a5c57faSSatish Balay   int            *procs,*nprocs,j,found,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;
4331eb62cbbSBarry Smith 
4343a40ed3dSBarry Smith   PetscFunctionBegin;
435e03a110bSBarry Smith   ierr = ISGetLocalSize(is,&N);CHKERRQ(ierr);
43678b31e54SBarry Smith   ierr = ISGetIndices(is,&rows);CHKERRQ(ierr);
4371eb62cbbSBarry Smith 
4381eb62cbbSBarry Smith   /*  first count number of contributors to each processor */
4390452661fSBarry Smith   nprocs = (int*)PetscMalloc(2*size*sizeof(int));CHKPTRQ(nprocs);
440549d3d68SSatish Balay   ierr   = PetscMemzero(nprocs,2*size*sizeof(int));CHKERRQ(ierr);
441549d3d68SSatish Balay   procs  = nprocs + size;
4420452661fSBarry Smith   owner  = (int*)PetscMalloc((N+1)*sizeof(int));CHKPTRQ(owner); /* see note*/
4431eb62cbbSBarry Smith   for (i=0; i<N; i++) {
4441eb62cbbSBarry Smith     idx = rows[i];
4451eb62cbbSBarry Smith     found = 0;
44617699dbbSLois Curfman McInnes     for (j=0; j<size; j++) {
4471eb62cbbSBarry Smith       if (idx >= owners[j] && idx < owners[j+1]) {
4481eb62cbbSBarry Smith         nprocs[j]++; procs[j] = 1; owner[i] = j; found = 1; break;
4491eb62cbbSBarry Smith       }
4501eb62cbbSBarry Smith     }
45129bbc08cSBarry Smith     if (!found) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Index out of range");
4521eb62cbbSBarry Smith   }
45317699dbbSLois Curfman McInnes   nsends = 0;  for (i=0; i<size; i++) { nsends += procs[i];}
4541eb62cbbSBarry Smith 
4551eb62cbbSBarry Smith   /* inform other processors of number of messages and max length*/
4566831982aSBarry Smith   work   = (int*)PetscMalloc(2*size*sizeof(int));CHKPTRQ(work);
4576831982aSBarry Smith   ierr   = MPI_Allreduce(nprocs,work,2*size,MPI_INT,PetscMaxSum_Op,comm);CHKERRQ(ierr);
4586831982aSBarry Smith   nrecvs = work[size+rank];
45917699dbbSLois Curfman McInnes   nmax   = work[rank];
460606d414cSSatish Balay   ierr   = PetscFree(work);CHKERRQ(ierr);
4611eb62cbbSBarry Smith 
4621eb62cbbSBarry Smith   /* post receives:   */
4633a40ed3dSBarry Smith   rvalues = (int*)PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(int));CHKPTRQ(rvalues);
464ca161407SBarry Smith   recv_waits = (MPI_Request*)PetscMalloc((nrecvs+1)*sizeof(MPI_Request));CHKPTRQ(recv_waits);
4651eb62cbbSBarry Smith   for (i=0; i<nrecvs; i++) {
466ca161407SBarry Smith     ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPI_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr);
4671eb62cbbSBarry Smith   }
4681eb62cbbSBarry Smith 
4691eb62cbbSBarry Smith   /* do sends:
4701eb62cbbSBarry Smith       1) starts[i] gives the starting index in svalues for stuff going to
4711eb62cbbSBarry Smith          the ith processor
4721eb62cbbSBarry Smith   */
4730452661fSBarry Smith   svalues = (int*)PetscMalloc((N+1)*sizeof(int));CHKPTRQ(svalues);
4743a40ed3dSBarry Smith   send_waits = (MPI_Request*)PetscMalloc((nsends+1)*sizeof(MPI_Request));CHKPTRQ(send_waits);
4750452661fSBarry Smith   starts = (int*)PetscMalloc((size+1)*sizeof(int));CHKPTRQ(starts);
4761eb62cbbSBarry Smith   starts[0] = 0;
47717699dbbSLois Curfman McInnes   for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[i-1];}
4781eb62cbbSBarry Smith   for (i=0; i<N; i++) {
4791eb62cbbSBarry Smith     svalues[starts[owner[i]]++] = rows[i];
4801eb62cbbSBarry Smith   }
4816831982aSBarry Smith   ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr);
4821eb62cbbSBarry Smith 
4831eb62cbbSBarry Smith   starts[0] = 0;
48417699dbbSLois Curfman McInnes   for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[i-1];}
4851eb62cbbSBarry Smith   count = 0;
48617699dbbSLois Curfman McInnes   for (i=0; i<size; i++) {
4871eb62cbbSBarry Smith     if (procs[i]) {
488ca161407SBarry Smith       ierr = MPI_Isend(svalues+starts[i],nprocs[i],MPI_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr);
4891eb62cbbSBarry Smith     }
4901eb62cbbSBarry Smith   }
491606d414cSSatish Balay   ierr = PetscFree(starts);CHKERRQ(ierr);
4921eb62cbbSBarry Smith 
49317699dbbSLois Curfman McInnes   base = owners[rank];
4941eb62cbbSBarry Smith 
4951eb62cbbSBarry Smith   /*  wait on receives */
4960452661fSBarry Smith   lens   = (int*)PetscMalloc(2*(nrecvs+1)*sizeof(int));CHKPTRQ(lens);
4971eb62cbbSBarry Smith   source = lens + nrecvs;
4981eb62cbbSBarry Smith   count  = nrecvs; slen = 0;
4991eb62cbbSBarry Smith   while (count) {
500ca161407SBarry Smith     ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
5011eb62cbbSBarry Smith     /* unpack receives into our local space */
502ca161407SBarry Smith     ierr = MPI_Get_count(&recv_status,MPI_INT,&n);CHKERRQ(ierr);
503d6dfbf8fSBarry Smith     source[imdex]  = recv_status.MPI_SOURCE;
504d6dfbf8fSBarry Smith     lens[imdex]    = n;
5051eb62cbbSBarry Smith     slen          += n;
5061eb62cbbSBarry Smith     count--;
5071eb62cbbSBarry Smith   }
508606d414cSSatish Balay   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
5091eb62cbbSBarry Smith 
5101eb62cbbSBarry Smith   /* move the data into the send scatter */
5110452661fSBarry Smith   lrows = (int*)PetscMalloc((slen+1)*sizeof(int));CHKPTRQ(lrows);
5121eb62cbbSBarry Smith   count = 0;
5131eb62cbbSBarry Smith   for (i=0; i<nrecvs; i++) {
5141eb62cbbSBarry Smith     values = rvalues + i*nmax;
5151eb62cbbSBarry Smith     for (j=0; j<lens[i]; j++) {
5161eb62cbbSBarry Smith       lrows[count++] = values[j] - base;
5171eb62cbbSBarry Smith     }
5181eb62cbbSBarry Smith   }
519606d414cSSatish Balay   ierr = PetscFree(rvalues);CHKERRQ(ierr);
520606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
521606d414cSSatish Balay   ierr = PetscFree(owner);CHKERRQ(ierr);
522606d414cSSatish Balay   ierr = PetscFree(nprocs);CHKERRQ(ierr);
5231eb62cbbSBarry Smith 
5241eb62cbbSBarry Smith   /* actually zap the local rows */
525029af93fSBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,slen,lrows,&istmp);CHKERRQ(ierr);
526464493b3SBarry Smith   PLogObjectParent(A,istmp);
5276a5c57faSSatish Balay 
5286eb55b6aSBarry Smith   /*
5296eb55b6aSBarry Smith         Zero the required rows. If the "diagonal block" of the matrix
5306eb55b6aSBarry Smith      is square and the user wishes to set the diagonal we use seperate
5316eb55b6aSBarry Smith      code so that MatSetValues() is not called for each diagonal allocating
5326eb55b6aSBarry Smith      new memory, thus calling lots of mallocs and slowing things down.
5336eb55b6aSBarry Smith 
5346eb55b6aSBarry Smith        Contributed by: Mathew Knepley
5356eb55b6aSBarry Smith   */
536e2d53e46SBarry Smith   /* must zero l->B before l->A because the (diag) case below may put values into l->B*/
537e2d53e46SBarry Smith   ierr = MatZeroRows(l->B,istmp,0);CHKERRQ(ierr);
5386eb55b6aSBarry Smith   if (diag && (l->A->M == l->A->N)) {
5396eb55b6aSBarry Smith     ierr      = MatZeroRows(l->A,istmp,diag);CHKERRQ(ierr);
540e2d53e46SBarry Smith   } else if (diag) {
541e2d53e46SBarry Smith     ierr = MatZeroRows(l->A,istmp,0);CHKERRQ(ierr);
542fa46199cSSatish Balay     if (((Mat_SeqAIJ*)l->A->data)->nonew) {
54329bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"MatZeroRows() on rectangular matrices cannot be used with the Mat options\n\
544fa46199cSSatish Balay MAT_NO_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR");
5456525c446SSatish Balay     }
546e2d53e46SBarry Smith     for (i = 0; i < slen; i++) {
547e2d53e46SBarry Smith       row  = lrows[i] + rstart;
548e2d53e46SBarry Smith       ierr = MatSetValues(A,1,&row,1,&row,diag,INSERT_VALUES);CHKERRQ(ierr);
549e2d53e46SBarry Smith     }
550e2d53e46SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
551e2d53e46SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
5526eb55b6aSBarry Smith   } else {
5536a5c57faSSatish Balay     ierr = MatZeroRows(l->A,istmp,0);CHKERRQ(ierr);
5546eb55b6aSBarry Smith   }
55578b31e54SBarry Smith   ierr = ISDestroy(istmp);CHKERRQ(ierr);
556606d414cSSatish Balay   ierr = PetscFree(lrows);CHKERRQ(ierr);
55772dacd9aSBarry Smith 
5581eb62cbbSBarry Smith   /* wait on sends */
5591eb62cbbSBarry Smith   if (nsends) {
560ca161407SBarry Smith     send_status = (MPI_Status*)PetscMalloc(nsends*sizeof(MPI_Status));CHKPTRQ(send_status);
561ca161407SBarry Smith     ierr        = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr);
562606d414cSSatish Balay     ierr        = PetscFree(send_status);CHKERRQ(ierr);
5631eb62cbbSBarry Smith   }
564606d414cSSatish Balay   ierr = PetscFree(send_waits);CHKERRQ(ierr);
565606d414cSSatish Balay   ierr = PetscFree(svalues);CHKERRQ(ierr);
5661eb62cbbSBarry Smith 
5673a40ed3dSBarry Smith   PetscFunctionReturn(0);
5681eb62cbbSBarry Smith }
5691eb62cbbSBarry Smith 
5705615d1e5SSatish Balay #undef __FUNC__
571b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatMult_MPIAIJ"
5728f6be9afSLois Curfman McInnes int MatMult_MPIAIJ(Mat A,Vec xx,Vec yy)
5731eb62cbbSBarry Smith {
574416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
575fbd6ef76SBarry Smith   int        ierr,nt;
576416022c9SBarry Smith 
5773a40ed3dSBarry Smith   PetscFunctionBegin;
578a2ce50c7SBarry Smith   ierr = VecGetLocalSize(xx,&nt);CHKERRQ(ierr);
579*273d9f13SBarry Smith   if (nt != A->n) {
580*273d9f13SBarry Smith     SETERRQ2(PETSC_ERR_ARG_SIZ,"Incompatible partition of A (%d) and xx (%d)",A->n,nt);
581fbd6ef76SBarry Smith   }
58243a90d84SBarry Smith   ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
583f830108cSBarry Smith   ierr = (*a->A->ops->mult)(a->A,xx,yy);CHKERRQ(ierr);
58443a90d84SBarry Smith   ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
585f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);CHKERRQ(ierr);
5863a40ed3dSBarry Smith   PetscFunctionReturn(0);
5871eb62cbbSBarry Smith }
5881eb62cbbSBarry Smith 
5895615d1e5SSatish Balay #undef __FUNC__
590b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatMultAdd_MPIAIJ"
5918f6be9afSLois Curfman McInnes int MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
592da3a660dSBarry Smith {
593416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
594da3a660dSBarry Smith   int        ierr;
5953a40ed3dSBarry Smith 
5963a40ed3dSBarry Smith   PetscFunctionBegin;
59743a90d84SBarry Smith   ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
598f830108cSBarry Smith   ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
59943a90d84SBarry Smith   ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
600f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);CHKERRQ(ierr);
6013a40ed3dSBarry Smith   PetscFunctionReturn(0);
602da3a660dSBarry Smith }
603da3a660dSBarry Smith 
6045615d1e5SSatish Balay #undef __FUNC__
605b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatMultTranspose_MPIAIJ"
6067c922b88SBarry Smith int MatMultTranspose_MPIAIJ(Mat A,Vec xx,Vec yy)
607da3a660dSBarry Smith {
608416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
609da3a660dSBarry Smith   int        ierr;
610da3a660dSBarry Smith 
6113a40ed3dSBarry Smith   PetscFunctionBegin;
612da3a660dSBarry Smith   /* do nondiagonal part */
6137c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
614da3a660dSBarry Smith   /* send it on its way */
615537820f0SBarry Smith   ierr = VecScatterBegin(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
616da3a660dSBarry Smith   /* do local part */
6177c922b88SBarry Smith   ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr);
618da3a660dSBarry Smith   /* receive remote parts: note this assumes the values are not actually */
619da3a660dSBarry Smith   /* inserted in yy until the next line, which is true for my implementation*/
620da3a660dSBarry Smith   /* but is not perhaps always true. */
621537820f0SBarry Smith   ierr = VecScatterEnd(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
6223a40ed3dSBarry Smith   PetscFunctionReturn(0);
623da3a660dSBarry Smith }
624da3a660dSBarry Smith 
6255615d1e5SSatish Balay #undef __FUNC__
626b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatMultTransposeAdd_MPIAIJ"
6277c922b88SBarry Smith int MatMultTransposeAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
628da3a660dSBarry Smith {
629416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
630da3a660dSBarry Smith   int        ierr;
631da3a660dSBarry Smith 
6323a40ed3dSBarry Smith   PetscFunctionBegin;
633da3a660dSBarry Smith   /* do nondiagonal part */
6347c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
635da3a660dSBarry Smith   /* send it on its way */
636537820f0SBarry Smith   ierr = VecScatterBegin(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
637da3a660dSBarry Smith   /* do local part */
6387c922b88SBarry Smith   ierr = (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
639da3a660dSBarry Smith   /* receive remote parts: note this assumes the values are not actually */
640da3a660dSBarry Smith   /* inserted in yy until the next line, which is true for my implementation*/
641da3a660dSBarry Smith   /* but is not perhaps always true. */
6420a198c4cSBarry Smith   ierr = VecScatterEnd(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
6433a40ed3dSBarry Smith   PetscFunctionReturn(0);
644da3a660dSBarry Smith }
645da3a660dSBarry Smith 
6461eb62cbbSBarry Smith /*
6471eb62cbbSBarry Smith   This only works correctly for square matrices where the subblock A->A is the
6481eb62cbbSBarry Smith    diagonal block
6491eb62cbbSBarry Smith */
6505615d1e5SSatish Balay #undef __FUNC__
651b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatGetDiagonal_MPIAIJ"
6528f6be9afSLois Curfman McInnes int MatGetDiagonal_MPIAIJ(Mat A,Vec v)
6531eb62cbbSBarry Smith {
6543a40ed3dSBarry Smith   int        ierr;
655416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
6563a40ed3dSBarry Smith 
6573a40ed3dSBarry Smith   PetscFunctionBegin;
658*273d9f13SBarry Smith   if (A->M != A->N) SETERRQ(PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block");
6595baf8537SBarry Smith   if (a->rstart != a->cstart || a->rend != a->cend) {
66029bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_SIZ,"row partition must equal col partition");
6613a40ed3dSBarry Smith   }
6623a40ed3dSBarry Smith   ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr);
6633a40ed3dSBarry Smith   PetscFunctionReturn(0);
6641eb62cbbSBarry Smith }
6651eb62cbbSBarry Smith 
6665615d1e5SSatish Balay #undef __FUNC__
667b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatScale_MPIAIJ"
6688f6be9afSLois Curfman McInnes int MatScale_MPIAIJ(Scalar *aa,Mat A)
669052efed2SBarry Smith {
670052efed2SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
671052efed2SBarry Smith   int        ierr;
6723a40ed3dSBarry Smith 
6733a40ed3dSBarry Smith   PetscFunctionBegin;
674052efed2SBarry Smith   ierr = MatScale(aa,a->A);CHKERRQ(ierr);
675052efed2SBarry Smith   ierr = MatScale(aa,a->B);CHKERRQ(ierr);
6763a40ed3dSBarry Smith   PetscFunctionReturn(0);
677052efed2SBarry Smith }
678052efed2SBarry Smith 
6795615d1e5SSatish Balay #undef __FUNC__
680b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatDestroy_MPIAIJ"
681e1311b90SBarry Smith int MatDestroy_MPIAIJ(Mat mat)
6821eb62cbbSBarry Smith {
68344a69424SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
6841eb62cbbSBarry Smith   int        ierr;
68583e2fdc7SBarry Smith 
6863a40ed3dSBarry Smith   PetscFunctionBegin;
687aa482453SBarry Smith #if defined(PETSC_USE_LOG)
688*273d9f13SBarry Smith   PLogObjectState((PetscObject)mat,"Rows=%d, Cols=%d",mat->M,mat->N);
689a5a9c739SBarry Smith #endif
6908798bf22SSatish Balay   ierr = MatStashDestroy_Private(&mat->stash);CHKERRQ(ierr);
691606d414cSSatish Balay   ierr = PetscFree(aij->rowners);CHKERRQ(ierr);
69278b31e54SBarry Smith   ierr = MatDestroy(aij->A);CHKERRQ(ierr);
69378b31e54SBarry Smith   ierr = MatDestroy(aij->B);CHKERRQ(ierr);
694aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
6950f5bd95cSBarry Smith   if (aij->colmap) {ierr = PetscTableDelete(aij->colmap);CHKERRQ(ierr);}
696b1fc9764SSatish Balay #else
697606d414cSSatish Balay   if (aij->colmap) {ierr = PetscFree(aij->colmap);CHKERRQ(ierr);}
698b1fc9764SSatish Balay #endif
699606d414cSSatish Balay   if (aij->garray) {ierr = PetscFree(aij->garray);CHKERRQ(ierr);}
7007c922b88SBarry Smith   if (aij->lvec)   {ierr = VecDestroy(aij->lvec);CHKERRQ(ierr);}
7017c922b88SBarry Smith   if (aij->Mvctx)  {ierr = VecScatterDestroy(aij->Mvctx);CHKERRQ(ierr);}
702606d414cSSatish Balay   if (aij->rowvalues) {ierr = PetscFree(aij->rowvalues);CHKERRQ(ierr);}
703606d414cSSatish Balay   ierr = PetscFree(aij);CHKERRQ(ierr);
7043a40ed3dSBarry Smith   PetscFunctionReturn(0);
7051eb62cbbSBarry Smith }
706ee50ffe9SBarry Smith 
7075615d1e5SSatish Balay #undef __FUNC__
708b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatView_MPIAIJ_ASCIIorDraworSocket"
709bc5ccf88SSatish Balay int MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,Viewer viewer)
710416022c9SBarry Smith {
71144a69424SLois Curfman McInnes   Mat_MPIAIJ  *aij = (Mat_MPIAIJ*)mat->data;
712dbb450caSBarry Smith   Mat_SeqAIJ* C = (Mat_SeqAIJ*)aij->A->data;
7136831982aSBarry Smith   int         ierr,format,shift = C->indexshift,rank = aij->rank,size = aij->size;
714f1af5d2fSBarry Smith   PetscTruth  isdraw,isascii,flg;
715e03a110bSBarry Smith   Viewer      sviewer;
716416022c9SBarry Smith 
7173a40ed3dSBarry Smith   PetscFunctionBegin;
7186831982aSBarry Smith   ierr  = PetscTypeCompare((PetscObject)viewer,DRAW_VIEWER,&isdraw);CHKERRQ(ierr);
7196831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,ASCII_VIEWER,&isascii);CHKERRQ(ierr);
7200f5bd95cSBarry Smith   if (isascii) {
721d8467735SBarry Smith     ierr = ViewerGetFormat(viewer,&format);CHKERRQ(ierr);
7220a198c4cSBarry Smith     if (format == VIEWER_FORMAT_ASCII_INFO_LONG) {
7234e220ebcSLois Curfman McInnes       MatInfo info;
7241dab6e02SBarry Smith       ierr = MPI_Comm_rank(mat->comm,&rank);CHKERRQ(ierr);
725888f2ed8SSatish Balay       ierr = MatGetInfo(mat,MAT_LOCAL,&info);CHKERRQ(ierr);
72695e01e2fSLois Curfman McInnes       ierr = OptionsHasName(PETSC_NULL,"-mat_aij_no_inode",&flg);CHKERRQ(ierr);
7276831982aSBarry Smith       if (flg) {
7286831982aSBarry Smith         ierr = ViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %d nz %d nz alloced %d mem %d, not using I-node routines\n",
729*273d9f13SBarry Smith 					      rank,mat->m,(int)info.nz_used,(int)info.nz_allocated,(int)info.memory);CHKERRQ(ierr);
7306831982aSBarry Smith       } else {
7316831982aSBarry Smith         ierr = ViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %d nz %d nz alloced %d mem %d, using I-node routines\n",
732*273d9f13SBarry Smith 		    rank,mat->m,(int)info.nz_used,(int)info.nz_allocated,(int)info.memory);CHKERRQ(ierr);
7336831982aSBarry Smith       }
734888f2ed8SSatish Balay       ierr = MatGetInfo(aij->A,MAT_LOCAL,&info);CHKERRQ(ierr);
7356831982aSBarry Smith       ierr = ViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %d \n",rank,(int)info.nz_used);CHKERRQ(ierr);
736888f2ed8SSatish Balay       ierr = MatGetInfo(aij->B,MAT_LOCAL,&info);CHKERRQ(ierr);
7376831982aSBarry Smith       ierr = ViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %d \n",rank,(int)info.nz_used);CHKERRQ(ierr);
7386831982aSBarry Smith       ierr = ViewerFlush(viewer);CHKERRQ(ierr);
739a40aa06bSLois Curfman McInnes       ierr = VecScatterView(aij->Mvctx,viewer);CHKERRQ(ierr);
7403a40ed3dSBarry Smith       PetscFunctionReturn(0);
7413a40ed3dSBarry Smith     } else if (format == VIEWER_FORMAT_ASCII_INFO) {
7423a40ed3dSBarry Smith       PetscFunctionReturn(0);
74308480c60SBarry Smith     }
7440f5bd95cSBarry Smith   } else if (isdraw) {
74519bcc07fSBarry Smith     Draw       draw;
74619bcc07fSBarry Smith     PetscTruth isnull;
74777ed5343SBarry Smith     ierr = ViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
7483a40ed3dSBarry Smith     ierr = DrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
74919bcc07fSBarry Smith   }
75019bcc07fSBarry Smith 
75117699dbbSLois Curfman McInnes   if (size == 1) {
75278b31e54SBarry Smith     ierr = MatView(aij->A,viewer);CHKERRQ(ierr);
7533a40ed3dSBarry Smith   } else {
75495373324SBarry Smith     /* assemble the entire matrix onto first processor. */
75595373324SBarry Smith     Mat         A;
756ec8511deSBarry Smith     Mat_SeqAIJ *Aloc;
757*273d9f13SBarry Smith     int         M = mat->M,N = mat->N,m,*ai,*aj,row,*cols,i,*ct;
75895373324SBarry Smith     Scalar      *a;
7592ee70a88SLois Curfman McInnes 
76017699dbbSLois Curfman McInnes     if (!rank) {
76155843e3eSBarry Smith       ierr = MatCreateMPIAIJ(mat->comm,M,N,M,N,0,PETSC_NULL,0,PETSC_NULL,&A);CHKERRQ(ierr);
7623a40ed3dSBarry Smith     } else {
76355843e3eSBarry Smith       ierr = MatCreateMPIAIJ(mat->comm,0,0,M,N,0,PETSC_NULL,0,PETSC_NULL,&A);CHKERRQ(ierr);
76495373324SBarry Smith     }
765464493b3SBarry Smith     PLogObjectParent(mat,A);
766416022c9SBarry Smith 
76795373324SBarry Smith     /* copy over the A part */
768ec8511deSBarry Smith     Aloc = (Mat_SeqAIJ*)aij->A->data;
769*273d9f13SBarry Smith     m = aij->A->m; ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
77095373324SBarry Smith     row = aij->rstart;
771dbb450caSBarry Smith     for (i=0; i<ai[m]+shift; i++) {aj[i] += aij->cstart + shift;}
77295373324SBarry Smith     for (i=0; i<m; i++) {
773416022c9SBarry Smith       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);CHKERRQ(ierr);
77495373324SBarry Smith       row++; a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
77595373324SBarry Smith     }
7762ee70a88SLois Curfman McInnes     aj = Aloc->j;
777dbb450caSBarry Smith     for (i=0; i<ai[m]+shift; i++) {aj[i] -= aij->cstart + shift;}
77895373324SBarry Smith 
77995373324SBarry Smith     /* copy over the B part */
780ec8511deSBarry Smith     Aloc = (Mat_SeqAIJ*)aij->B->data;
781*273d9f13SBarry Smith     m = aij->B->m;  ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
78295373324SBarry Smith     row = aij->rstart;
7830452661fSBarry Smith     ct = cols = (int*)PetscMalloc((ai[m]+1)*sizeof(int));CHKPTRQ(cols);
784dbb450caSBarry Smith     for (i=0; i<ai[m]+shift; i++) {cols[i] = aij->garray[aj[i]+shift];}
78595373324SBarry Smith     for (i=0; i<m; i++) {
786416022c9SBarry Smith       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);CHKERRQ(ierr);
78795373324SBarry Smith       row++; a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
78895373324SBarry Smith     }
789606d414cSSatish Balay     ierr = PetscFree(ct);CHKERRQ(ierr);
7906d4a8577SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7916d4a8577SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
79255843e3eSBarry Smith     /*
79355843e3eSBarry Smith        Everyone has to call to draw the matrix since the graphics waits are
79455843e3eSBarry Smith        synchronized across all processors that share the Draw object
79555843e3eSBarry Smith     */
7966831982aSBarry Smith     ierr = ViewerGetSingleton(viewer,&sviewer);CHKERRQ(ierr);
797e03a110bSBarry Smith     if (!rank) {
7986831982aSBarry Smith       ierr = MatView(((Mat_MPIAIJ*)(A->data))->A,sviewer);CHKERRQ(ierr);
79995373324SBarry Smith     }
800e03a110bSBarry Smith     ierr = ViewerRestoreSingleton(viewer,&sviewer);CHKERRQ(ierr);
80178b31e54SBarry Smith     ierr = MatDestroy(A);CHKERRQ(ierr);
80295373324SBarry Smith   }
8033a40ed3dSBarry Smith   PetscFunctionReturn(0);
8041eb62cbbSBarry Smith }
8051eb62cbbSBarry Smith 
8065615d1e5SSatish Balay #undef __FUNC__
807b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatView_MPIAIJ"
808e1311b90SBarry Smith int MatView_MPIAIJ(Mat mat,Viewer viewer)
809416022c9SBarry Smith {
810416022c9SBarry Smith   int        ierr;
8116831982aSBarry Smith   PetscTruth isascii,isdraw,issocket,isbinary;
812416022c9SBarry Smith 
8133a40ed3dSBarry Smith   PetscFunctionBegin;
8146831982aSBarry Smith   ierr  = PetscTypeCompare((PetscObject)viewer,ASCII_VIEWER,&isascii);CHKERRQ(ierr);
8156831982aSBarry Smith   ierr  = PetscTypeCompare((PetscObject)viewer,DRAW_VIEWER,&isdraw);CHKERRQ(ierr);
8166831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,BINARY_VIEWER,&isbinary);CHKERRQ(ierr);
8176831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,SOCKET_VIEWER,&issocket);CHKERRQ(ierr);
8180f5bd95cSBarry Smith   if (isascii || isdraw || isbinary || issocket) {
8197b2a1423SBarry Smith     ierr = MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer);CHKERRQ(ierr);
8205cd90555SBarry Smith   } else {
82129bbc08cSBarry Smith     SETERRQ1(1,"Viewer type %s not supported by MPIAIJ matrices",((PetscObject)viewer)->type_name);
822416022c9SBarry Smith   }
8233a40ed3dSBarry Smith   PetscFunctionReturn(0);
824416022c9SBarry Smith }
825416022c9SBarry Smith 
8261eb62cbbSBarry Smith /*
8271eb62cbbSBarry Smith     This has to provide several versions.
8281eb62cbbSBarry Smith 
8291eb62cbbSBarry Smith      2) a) use only local smoothing updating outer values only once.
8301eb62cbbSBarry Smith         b) local smoothing updating outer values each inner iteration
831d6dfbf8fSBarry Smith      3) color updating out values betwen colors.
8321eb62cbbSBarry Smith */
8335615d1e5SSatish Balay #undef __FUNC__
834b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatRelax_MPIAIJ"
835*273d9f13SBarry Smith int MatRelax_MPIAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,int its,Vec xx)
8368a729477SBarry Smith {
83744a69424SLois Curfman McInnes   Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data;
838d6dfbf8fSBarry Smith   Mat        AA = mat->A,BB = mat->B;
839ec8511deSBarry Smith   Mat_SeqAIJ *A = (Mat_SeqAIJ*)AA->data,*B = (Mat_SeqAIJ *)BB->data;
840c16cb8f2SBarry Smith   Scalar     *b,*x,*xs,*ls,d,*v,sum;
8416abc6512SBarry Smith   int        ierr,*idx,*diag;
842*273d9f13SBarry Smith   int        n = matin->n,m = matin->m,i,shift = A->indexshift;
8438a729477SBarry Smith 
8443a40ed3dSBarry Smith   PetscFunctionBegin;
8457c922b88SBarry Smith   if (!A->diag) {ierr = MatMarkDiagonal_SeqAIJ(AA);CHKERRQ(ierr);}
846d6dfbf8fSBarry Smith   diag = A->diag;
847c16cb8f2SBarry Smith   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){
848da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
849f830108cSBarry Smith       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,its,xx);CHKERRQ(ierr);
8503a40ed3dSBarry Smith       PetscFunctionReturn(0);
851da3a660dSBarry Smith     }
8523a40ed3dSBarry Smith     ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
8533a40ed3dSBarry Smith     ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
854184914b5SBarry Smith     ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
855184914b5SBarry Smith     if (xx != bb) {
856184914b5SBarry Smith       ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
857184914b5SBarry Smith     } else {
858184914b5SBarry Smith       b = x;
859184914b5SBarry Smith     }
860184914b5SBarry Smith     ierr = VecGetArray(mat->lvec,&ls);CHKERRQ(ierr);
861184914b5SBarry Smith     xs = x + shift; /* shift by one for index start of 1 */
862184914b5SBarry Smith     ls = ls + shift;
863d6dfbf8fSBarry Smith     while (its--) {
864d6dfbf8fSBarry Smith       /* go down through the rows */
865d6dfbf8fSBarry Smith       for (i=0; i<m; i++) {
866d6dfbf8fSBarry Smith         n    = A->i[i+1] - A->i[i];
8674d197716SBarry Smith 	PLogFlops(4*n+3);
868dbb450caSBarry Smith         idx  = A->j + A->i[i] + shift;
869dbb450caSBarry Smith         v    = A->a + A->i[i] + shift;
870d6dfbf8fSBarry Smith         sum  = b[i];
871d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
872dbb450caSBarry Smith         d    = fshift + A->a[diag[i]+shift];
873d6dfbf8fSBarry Smith         n    = B->i[i+1] - B->i[i];
874dbb450caSBarry Smith         idx  = B->j + B->i[i] + shift;
875dbb450caSBarry Smith         v    = B->a + B->i[i] + shift;
876d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,ls,v,idx,n);
87755a1b374SBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + A->a[diag[i]+shift]*x[i])/d;
878d6dfbf8fSBarry Smith       }
879d6dfbf8fSBarry Smith       /* come up through the rows */
880d6dfbf8fSBarry Smith       for (i=m-1; i>-1; i--) {
881d6dfbf8fSBarry Smith         n    = A->i[i+1] - A->i[i];
882102ac143SSatish Balay 	PLogFlops(4*n+3);
883dbb450caSBarry Smith         idx  = A->j + A->i[i] + shift;
884dbb450caSBarry Smith         v    = A->a + A->i[i] + shift;
885d6dfbf8fSBarry Smith         sum  = b[i];
886d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
887dbb450caSBarry Smith         d    = fshift + A->a[diag[i]+shift];
888d6dfbf8fSBarry Smith         n    = B->i[i+1] - B->i[i];
889dbb450caSBarry Smith         idx  = B->j + B->i[i] + shift;
890dbb450caSBarry Smith         v    = B->a + B->i[i] + shift;
891d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,ls,v,idx,n);
89255a1b374SBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + A->a[diag[i]+shift]*x[i])/d;
893d6dfbf8fSBarry Smith       }
894d6dfbf8fSBarry Smith     }
895184914b5SBarry Smith     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
896184914b5SBarry Smith     if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr); }
897184914b5SBarry Smith     ierr = VecRestoreArray(mat->lvec,&ls);CHKERRQ(ierr);
8983a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_FORWARD_SWEEP){
899da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
900f830108cSBarry Smith       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,its,xx);CHKERRQ(ierr);
9013a40ed3dSBarry Smith       PetscFunctionReturn(0);
902da3a660dSBarry Smith     }
9033a40ed3dSBarry Smith     ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
9043a40ed3dSBarry Smith     ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
905184914b5SBarry Smith     ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
906184914b5SBarry Smith     if (xx != bb) {
907184914b5SBarry Smith       ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
908184914b5SBarry Smith     } else {
909184914b5SBarry Smith       b = x;
910184914b5SBarry Smith     }
911184914b5SBarry Smith     ierr = VecGetArray(mat->lvec,&ls);CHKERRQ(ierr);
912184914b5SBarry Smith     xs = x + shift; /* shift by one for index start of 1 */
913184914b5SBarry Smith     ls = ls + shift;
914d6dfbf8fSBarry Smith     while (its--) {
915d6dfbf8fSBarry Smith       for (i=0; i<m; i++) {
916d6dfbf8fSBarry Smith         n    = A->i[i+1] - A->i[i];
9174d197716SBarry Smith 	PLogFlops(4*n+3);
918dbb450caSBarry Smith         idx  = A->j + A->i[i] + shift;
919dbb450caSBarry Smith         v    = A->a + A->i[i] + shift;
920d6dfbf8fSBarry Smith         sum  = b[i];
921d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
922dbb450caSBarry Smith         d    = fshift + A->a[diag[i]+shift];
923d6dfbf8fSBarry Smith         n    = B->i[i+1] - B->i[i];
924dbb450caSBarry Smith         idx  = B->j + B->i[i] + shift;
925dbb450caSBarry Smith         v    = B->a + B->i[i] + shift;
926d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,ls,v,idx,n);
92755a1b374SBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + A->a[diag[i]+shift]*x[i])/d;
928d6dfbf8fSBarry Smith       }
929d6dfbf8fSBarry Smith     }
930184914b5SBarry Smith     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
931184914b5SBarry Smith     if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr); }
932184914b5SBarry Smith     ierr = VecRestoreArray(mat->lvec,&ls);CHKERRQ(ierr);
9333a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_BACKWARD_SWEEP){
934da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
935f830108cSBarry Smith       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,its,xx);CHKERRQ(ierr);
9363a40ed3dSBarry Smith       PetscFunctionReturn(0);
937da3a660dSBarry Smith     }
9382e8a6d31SBarry Smith     ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
9392e8a6d31SBarry Smith     ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
940184914b5SBarry Smith     ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
941184914b5SBarry Smith     if (xx != bb) {
942184914b5SBarry Smith       ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
943184914b5SBarry Smith     } else {
944184914b5SBarry Smith       b = x;
945184914b5SBarry Smith     }
946184914b5SBarry Smith     ierr = VecGetArray(mat->lvec,&ls);CHKERRQ(ierr);
947184914b5SBarry Smith     xs = x + shift; /* shift by one for index start of 1 */
948184914b5SBarry Smith     ls = ls + shift;
949d6dfbf8fSBarry Smith     while (its--) {
950d6dfbf8fSBarry Smith       for (i=m-1; i>-1; i--) {
951d6dfbf8fSBarry Smith         n    = A->i[i+1] - A->i[i];
9524d197716SBarry Smith 	PLogFlops(4*n+3);
953dbb450caSBarry Smith         idx  = A->j + A->i[i] + shift;
954dbb450caSBarry Smith         v    = A->a + A->i[i] + shift;
955d6dfbf8fSBarry Smith         sum  = b[i];
956d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
957dbb450caSBarry Smith         d    = fshift + A->a[diag[i]+shift];
958d6dfbf8fSBarry Smith         n    = B->i[i+1] - B->i[i];
959dbb450caSBarry Smith         idx  = B->j + B->i[i] + shift;
960dbb450caSBarry Smith         v    = B->a + B->i[i] + shift;
961d6dfbf8fSBarry Smith         SPARSEDENSEMDOT(sum,ls,v,idx,n);
96255a1b374SBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + A->a[diag[i]+shift]*x[i])/d;
963d6dfbf8fSBarry Smith       }
964d6dfbf8fSBarry Smith     }
965184914b5SBarry Smith     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
966184914b5SBarry Smith     if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr); }
967184914b5SBarry Smith     ierr = VecRestoreArray(mat->lvec,&ls);CHKERRQ(ierr);
9683a40ed3dSBarry Smith   } else {
96929bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"Parallel SOR not supported");
970c16cb8f2SBarry Smith   }
9713a40ed3dSBarry Smith   PetscFunctionReturn(0);
9728a729477SBarry Smith }
973a66be287SLois Curfman McInnes 
9745615d1e5SSatish Balay #undef __FUNC__
975b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatGetInfo_MPIAIJ"
9768f6be9afSLois Curfman McInnes int MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info)
977a66be287SLois Curfman McInnes {
978a66be287SLois Curfman McInnes   Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data;
979a66be287SLois Curfman McInnes   Mat        A = mat->A,B = mat->B;
9804e220ebcSLois Curfman McInnes   int        ierr;
981329f5518SBarry Smith   PetscReal  isend[5],irecv[5];
982a66be287SLois Curfman McInnes 
9833a40ed3dSBarry Smith   PetscFunctionBegin;
9844e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
9854e220ebcSLois Curfman McInnes   ierr = MatGetInfo(A,MAT_LOCAL,info);CHKERRQ(ierr);
9864e220ebcSLois Curfman McInnes   isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
9874e220ebcSLois Curfman McInnes   isend[3] = info->memory;  isend[4] = info->mallocs;
9884e220ebcSLois Curfman McInnes   ierr = MatGetInfo(B,MAT_LOCAL,info);CHKERRQ(ierr);
9894e220ebcSLois Curfman McInnes   isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
9904e220ebcSLois Curfman McInnes   isend[3] += info->memory;  isend[4] += info->mallocs;
991a66be287SLois Curfman McInnes   if (flag == MAT_LOCAL) {
9924e220ebcSLois Curfman McInnes     info->nz_used      = isend[0];
9934e220ebcSLois Curfman McInnes     info->nz_allocated = isend[1];
9944e220ebcSLois Curfman McInnes     info->nz_unneeded  = isend[2];
9954e220ebcSLois Curfman McInnes     info->memory       = isend[3];
9964e220ebcSLois Curfman McInnes     info->mallocs      = isend[4];
997a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_MAX) {
998ca161407SBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPI_DOUBLE,MPI_MAX,matin->comm);CHKERRQ(ierr);
9994e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
10004e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
10014e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
10024e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
10034e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1004a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_SUM) {
1005ca161407SBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPI_DOUBLE,MPI_SUM,matin->comm);CHKERRQ(ierr);
10064e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
10074e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
10084e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
10094e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
10104e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1011a66be287SLois Curfman McInnes   }
10124e220ebcSLois Curfman McInnes   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
10134e220ebcSLois Curfman McInnes   info->fill_ratio_needed = 0;
10144e220ebcSLois Curfman McInnes   info->factor_mallocs    = 0;
1015*273d9f13SBarry Smith   info->rows_global       = (double)matin->M;
1016*273d9f13SBarry Smith   info->columns_global    = (double)matin->N;
1017*273d9f13SBarry Smith   info->rows_local        = (double)matin->m;
1018*273d9f13SBarry Smith   info->columns_local     = (double)matin->N;
10194e220ebcSLois Curfman McInnes 
10203a40ed3dSBarry Smith   PetscFunctionReturn(0);
1021a66be287SLois Curfman McInnes }
1022a66be287SLois Curfman McInnes 
10235615d1e5SSatish Balay #undef __FUNC__
1024b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatSetOption_MPIAIJ"
10258f6be9afSLois Curfman McInnes int MatSetOption_MPIAIJ(Mat A,MatOption op)
1026c74985f6SBarry Smith {
1027c0bbcb79SLois Curfman McInnes   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
10281dee9f54SBarry Smith   int        ierr;
1029c74985f6SBarry Smith 
10303a40ed3dSBarry Smith   PetscFunctionBegin;
10316d4a8577SBarry Smith   if (op == MAT_NO_NEW_NONZERO_LOCATIONS ||
10326d4a8577SBarry Smith       op == MAT_YES_NEW_NONZERO_LOCATIONS ||
10336da5968aSLois Curfman McInnes       op == MAT_COLUMNS_UNSORTED ||
1034c2653b3dSLois Curfman McInnes       op == MAT_COLUMNS_SORTED ||
10354787f768SSatish Balay       op == MAT_NEW_NONZERO_ALLOCATION_ERR ||
10367c922b88SBarry Smith       op == MAT_KEEP_ZEROED_ROWS ||
1037329f5518SBarry Smith       op == MAT_NEW_NONZERO_LOCATION_ERR ||
10384c49b128SBarry Smith       op == MAT_USE_INODES ||
10394c49b128SBarry Smith       op == MAT_DO_NOT_USE_INODES ||
1040329f5518SBarry Smith       op == MAT_IGNORE_ZERO_ENTRIES) {
10411dee9f54SBarry Smith         ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
10421dee9f54SBarry Smith         ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
1043b1fbbac0SLois Curfman McInnes   } else if (op == MAT_ROW_ORIENTED) {
10447c922b88SBarry Smith     a->roworiented = PETSC_TRUE;
10451dee9f54SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
10461dee9f54SBarry Smith     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
1047b1fbbac0SLois Curfman McInnes   } else if (op == MAT_ROWS_SORTED ||
10486da5968aSLois Curfman McInnes              op == MAT_ROWS_UNSORTED ||
10496d4a8577SBarry Smith              op == MAT_SYMMETRIC ||
10506d4a8577SBarry Smith              op == MAT_STRUCTURALLY_SYMMETRIC ||
1051329f5518SBarry Smith              op == MAT_YES_NEW_DIAGONALS) {
1052981c4779SBarry Smith     PLogInfo(A,"MatSetOption_MPIAIJ:Option ignored\n");
1053329f5518SBarry Smith   } else if (op == MAT_COLUMN_ORIENTED) {
10547c922b88SBarry Smith     a->roworiented = PETSC_FALSE;
10551dee9f54SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
10561dee9f54SBarry Smith     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
10572b362799SSatish Balay   } else if (op == MAT_IGNORE_OFF_PROC_ENTRIES) {
10587c922b88SBarry Smith     a->donotstash = PETSC_TRUE;
10593a40ed3dSBarry Smith   } else if (op == MAT_NO_NEW_DIAGONALS){
106029bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"MAT_NO_NEW_DIAGONALS");
10613a40ed3dSBarry Smith   } else {
106229bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"unknown option");
10633a40ed3dSBarry Smith   }
10643a40ed3dSBarry Smith   PetscFunctionReturn(0);
1065c74985f6SBarry Smith }
1066c74985f6SBarry Smith 
10675615d1e5SSatish Balay #undef __FUNC__
1068b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatGetOwnershipRange_MPIAIJ"
10698f6be9afSLois Curfman McInnes int MatGetOwnershipRange_MPIAIJ(Mat matin,int *m,int *n)
1070c74985f6SBarry Smith {
107144a69424SLois Curfman McInnes   Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data;
10723a40ed3dSBarry Smith 
10733a40ed3dSBarry Smith   PetscFunctionBegin;
10744c49b128SBarry Smith   if (m) *m = mat->rstart;
10754c49b128SBarry Smith   if (n) *n = mat->rend;
10763a40ed3dSBarry Smith   PetscFunctionReturn(0);
1077c74985f6SBarry Smith }
1078c74985f6SBarry Smith 
10795615d1e5SSatish Balay #undef __FUNC__
1080b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatGetRow_MPIAIJ"
10816d84be18SBarry Smith int MatGetRow_MPIAIJ(Mat matin,int row,int *nz,int **idx,Scalar **v)
108239e00950SLois Curfman McInnes {
1083154123eaSLois Curfman McInnes   Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data;
108470f0671dSBarry Smith   Scalar     *vworkA,*vworkB,**pvA,**pvB,*v_p;
1085154123eaSLois Curfman McInnes   int        i,ierr,*cworkA,*cworkB,**pcA,**pcB,cstart = mat->cstart;
1086154123eaSLois Curfman McInnes   int        nztot,nzA,nzB,lrow,rstart = mat->rstart,rend = mat->rend;
108770f0671dSBarry Smith   int        *cmap,*idx_p;
108839e00950SLois Curfman McInnes 
10893a40ed3dSBarry Smith   PetscFunctionBegin;
109029bbc08cSBarry Smith   if (mat->getrowactive == PETSC_TRUE) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Already active");
10917a0afa10SBarry Smith   mat->getrowactive = PETSC_TRUE;
10927a0afa10SBarry Smith 
109370f0671dSBarry Smith   if (!mat->rowvalues && (idx || v)) {
10947a0afa10SBarry Smith     /*
10957a0afa10SBarry Smith         allocate enough space to hold information from the longest row.
10967a0afa10SBarry Smith     */
10977a0afa10SBarry Smith     Mat_SeqAIJ *Aa = (Mat_SeqAIJ*)mat->A->data,*Ba = (Mat_SeqAIJ*)mat->B->data;
1098*273d9f13SBarry Smith     int     max = 1,tmp;
1099*273d9f13SBarry Smith     for (i=0; i<matin->m; i++) {
11007a0afa10SBarry Smith       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
11017a0afa10SBarry Smith       if (max < tmp) { max = tmp; }
11027a0afa10SBarry Smith     }
11033f97c4b0SBarry Smith     mat->rowvalues  = (Scalar*)PetscMalloc(max*(sizeof(int)+sizeof(Scalar)));CHKPTRQ(mat->rowvalues);
11047a0afa10SBarry Smith     mat->rowindices = (int*)(mat->rowvalues + max);
11057a0afa10SBarry Smith   }
11067a0afa10SBarry Smith 
110729bbc08cSBarry Smith   if (row < rstart || row >= rend) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Only local rows")
1108abc0e9e4SLois Curfman McInnes   lrow = row - rstart;
110939e00950SLois Curfman McInnes 
1110154123eaSLois Curfman McInnes   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1111154123eaSLois Curfman McInnes   if (!v)   {pvA = 0; pvB = 0;}
1112154123eaSLois Curfman McInnes   if (!idx) {pcA = 0; if (!v) pcB = 0;}
1113f830108cSBarry Smith   ierr = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1114f830108cSBarry Smith   ierr = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
1115154123eaSLois Curfman McInnes   nztot = nzA + nzB;
1116154123eaSLois Curfman McInnes 
111770f0671dSBarry Smith   cmap  = mat->garray;
1118154123eaSLois Curfman McInnes   if (v  || idx) {
1119154123eaSLois Curfman McInnes     if (nztot) {
1120154123eaSLois Curfman McInnes       /* Sort by increasing column numbers, assuming A and B already sorted */
112170f0671dSBarry Smith       int imark = -1;
1122154123eaSLois Curfman McInnes       if (v) {
112370f0671dSBarry Smith         *v = v_p = mat->rowvalues;
112439e00950SLois Curfman McInnes         for (i=0; i<nzB; i++) {
112570f0671dSBarry Smith           if (cmap[cworkB[i]] < cstart)   v_p[i] = vworkB[i];
1126154123eaSLois Curfman McInnes           else break;
1127154123eaSLois Curfman McInnes         }
1128154123eaSLois Curfman McInnes         imark = i;
112970f0671dSBarry Smith         for (i=0; i<nzA; i++)     v_p[imark+i] = vworkA[i];
113070f0671dSBarry Smith         for (i=imark; i<nzB; i++) v_p[nzA+i]   = vworkB[i];
1131154123eaSLois Curfman McInnes       }
1132154123eaSLois Curfman McInnes       if (idx) {
113370f0671dSBarry Smith         *idx = idx_p = mat->rowindices;
113470f0671dSBarry Smith         if (imark > -1) {
113570f0671dSBarry Smith           for (i=0; i<imark; i++) {
113670f0671dSBarry Smith             idx_p[i] = cmap[cworkB[i]];
113770f0671dSBarry Smith           }
113870f0671dSBarry Smith         } else {
1139154123eaSLois Curfman McInnes           for (i=0; i<nzB; i++) {
114070f0671dSBarry Smith             if (cmap[cworkB[i]] < cstart)   idx_p[i] = cmap[cworkB[i]];
1141154123eaSLois Curfman McInnes             else break;
1142154123eaSLois Curfman McInnes           }
1143154123eaSLois Curfman McInnes           imark = i;
114470f0671dSBarry Smith         }
114570f0671dSBarry Smith         for (i=0; i<nzA; i++)     idx_p[imark+i] = cstart + cworkA[i];
114670f0671dSBarry Smith         for (i=imark; i<nzB; i++) idx_p[nzA+i]   = cmap[cworkB[i]];
114739e00950SLois Curfman McInnes       }
11483f97c4b0SBarry Smith     } else {
11491ca473b0SSatish Balay       if (idx) *idx = 0;
11501ca473b0SSatish Balay       if (v)   *v   = 0;
11511ca473b0SSatish Balay     }
1152154123eaSLois Curfman McInnes   }
115339e00950SLois Curfman McInnes   *nz = nztot;
1154f830108cSBarry Smith   ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1155f830108cSBarry Smith   ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
11563a40ed3dSBarry Smith   PetscFunctionReturn(0);
115739e00950SLois Curfman McInnes }
115839e00950SLois Curfman McInnes 
11595615d1e5SSatish Balay #undef __FUNC__
1160b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatRestoreRow_MPIAIJ"
11616d84be18SBarry Smith int MatRestoreRow_MPIAIJ(Mat mat,int row,int *nz,int **idx,Scalar **v)
116239e00950SLois Curfman McInnes {
11637a0afa10SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
11643a40ed3dSBarry Smith 
11653a40ed3dSBarry Smith   PetscFunctionBegin;
11667a0afa10SBarry Smith   if (aij->getrowactive == PETSC_FALSE) {
116729bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"MatGetRow not called");
11687a0afa10SBarry Smith   }
11697a0afa10SBarry Smith   aij->getrowactive = PETSC_FALSE;
11703a40ed3dSBarry Smith   PetscFunctionReturn(0);
117139e00950SLois Curfman McInnes }
117239e00950SLois Curfman McInnes 
11735615d1e5SSatish Balay #undef __FUNC__
1174b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatNorm_MPIAIJ"
1175329f5518SBarry Smith int MatNorm_MPIAIJ(Mat mat,NormType type,PetscReal *norm)
1176855ac2c5SLois Curfman McInnes {
1177855ac2c5SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
1178ec8511deSBarry Smith   Mat_SeqAIJ *amat = (Mat_SeqAIJ*)aij->A->data,*bmat = (Mat_SeqAIJ*)aij->B->data;
1179416022c9SBarry Smith   int        ierr,i,j,cstart = aij->cstart,shift = amat->indexshift;
1180329f5518SBarry Smith   PetscReal  sum = 0.0;
118104ca555eSLois Curfman McInnes   Scalar     *v;
118204ca555eSLois Curfman McInnes 
11833a40ed3dSBarry Smith   PetscFunctionBegin;
118417699dbbSLois Curfman McInnes   if (aij->size == 1) {
118514183eadSLois Curfman McInnes     ierr =  MatNorm(aij->A,type,norm);CHKERRQ(ierr);
118637fa93a5SLois Curfman McInnes   } else {
118704ca555eSLois Curfman McInnes     if (type == NORM_FROBENIUS) {
118804ca555eSLois Curfman McInnes       v = amat->a;
118904ca555eSLois Curfman McInnes       for (i=0; i<amat->nz; i++) {
1190aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
1191329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
119204ca555eSLois Curfman McInnes #else
119304ca555eSLois Curfman McInnes         sum += (*v)*(*v); v++;
119404ca555eSLois Curfman McInnes #endif
119504ca555eSLois Curfman McInnes       }
119604ca555eSLois Curfman McInnes       v = bmat->a;
119704ca555eSLois Curfman McInnes       for (i=0; i<bmat->nz; i++) {
1198aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
1199329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
120004ca555eSLois Curfman McInnes #else
120104ca555eSLois Curfman McInnes         sum += (*v)*(*v); v++;
120204ca555eSLois Curfman McInnes #endif
120304ca555eSLois Curfman McInnes       }
1204ca161407SBarry Smith       ierr = MPI_Allreduce(&sum,norm,1,MPI_DOUBLE,MPI_SUM,mat->comm);CHKERRQ(ierr);
120504ca555eSLois Curfman McInnes       *norm = sqrt(*norm);
12063a40ed3dSBarry Smith     } else if (type == NORM_1) { /* max column norm */
1207329f5518SBarry Smith       PetscReal *tmp,*tmp2;
120804ca555eSLois Curfman McInnes       int    *jj,*garray = aij->garray;
1209*273d9f13SBarry Smith       tmp  = (PetscReal*)PetscMalloc((mat->N+1)*sizeof(PetscReal));CHKPTRQ(tmp);
1210*273d9f13SBarry Smith       tmp2 = (PetscReal*)PetscMalloc((mat->N+1)*sizeof(PetscReal));CHKPTRQ(tmp2);
1211*273d9f13SBarry Smith       ierr = PetscMemzero(tmp,mat->N*sizeof(PetscReal));CHKERRQ(ierr);
121204ca555eSLois Curfman McInnes       *norm = 0.0;
121304ca555eSLois Curfman McInnes       v = amat->a; jj = amat->j;
121404ca555eSLois Curfman McInnes       for (j=0; j<amat->nz; j++) {
1215579c6b6fSBarry Smith         tmp[cstart + *jj++ + shift] += PetscAbsScalar(*v);  v++;
121604ca555eSLois Curfman McInnes       }
121704ca555eSLois Curfman McInnes       v = bmat->a; jj = bmat->j;
121804ca555eSLois Curfman McInnes       for (j=0; j<bmat->nz; j++) {
1219579c6b6fSBarry Smith         tmp[garray[*jj++ + shift]] += PetscAbsScalar(*v); v++;
122004ca555eSLois Curfman McInnes       }
1221*273d9f13SBarry Smith       ierr = MPI_Allreduce(tmp,tmp2,mat->N,MPI_DOUBLE,MPI_SUM,mat->comm);CHKERRQ(ierr);
1222*273d9f13SBarry Smith       for (j=0; j<mat->N; j++) {
122304ca555eSLois Curfman McInnes         if (tmp2[j] > *norm) *norm = tmp2[j];
122404ca555eSLois Curfman McInnes       }
1225606d414cSSatish Balay       ierr = PetscFree(tmp);CHKERRQ(ierr);
1226606d414cSSatish Balay       ierr = PetscFree(tmp2);CHKERRQ(ierr);
12273a40ed3dSBarry Smith     } else if (type == NORM_INFINITY) { /* max row norm */
1228329f5518SBarry Smith       PetscReal ntemp = 0.0;
1229*273d9f13SBarry Smith       for (j=0; j<aij->A->m; j++) {
1230dbb450caSBarry Smith         v = amat->a + amat->i[j] + shift;
123104ca555eSLois Curfman McInnes         sum = 0.0;
123204ca555eSLois Curfman McInnes         for (i=0; i<amat->i[j+1]-amat->i[j]; i++) {
1233cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
123404ca555eSLois Curfman McInnes         }
1235dbb450caSBarry Smith         v = bmat->a + bmat->i[j] + shift;
123604ca555eSLois Curfman McInnes         for (i=0; i<bmat->i[j+1]-bmat->i[j]; i++) {
1237cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
123804ca555eSLois Curfman McInnes         }
1239515d9167SLois Curfman McInnes         if (sum > ntemp) ntemp = sum;
124004ca555eSLois Curfman McInnes       }
1241ca161407SBarry Smith       ierr = MPI_Allreduce(&ntemp,norm,1,MPI_DOUBLE,MPI_MAX,mat->comm);CHKERRQ(ierr);
1242ca161407SBarry Smith     } else {
124329bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"No support for two norm");
124404ca555eSLois Curfman McInnes     }
124537fa93a5SLois Curfman McInnes   }
12463a40ed3dSBarry Smith   PetscFunctionReturn(0);
1247855ac2c5SLois Curfman McInnes }
1248855ac2c5SLois Curfman McInnes 
12495615d1e5SSatish Balay #undef __FUNC__
1250b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatTranspose_MPIAIJ"
12518f6be9afSLois Curfman McInnes int MatTranspose_MPIAIJ(Mat A,Mat *matout)
1252b7c46309SBarry Smith {
1253b7c46309SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
1254dbb450caSBarry Smith   Mat_SeqAIJ *Aloc = (Mat_SeqAIJ*)a->A->data;
1255416022c9SBarry Smith   int        ierr,shift = Aloc->indexshift;
1256*273d9f13SBarry Smith   int        M = A->M,N = A->N,m,*ai,*aj,row,*cols,i,*ct;
12573a40ed3dSBarry Smith   Mat        B;
1258b7c46309SBarry Smith   Scalar     *array;
1259b7c46309SBarry Smith 
12603a40ed3dSBarry Smith   PetscFunctionBegin;
12617c922b88SBarry Smith   if (!matout && M != N) {
126229bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1263d4bb536fSBarry Smith   }
1264d4bb536fSBarry Smith 
1265*273d9f13SBarry Smith   ierr = MatCreateMPIAIJ(A->comm,A->n,A->m,N,M,0,PETSC_NULL,0,PETSC_NULL,&B);CHKERRQ(ierr);
1266b7c46309SBarry Smith 
1267b7c46309SBarry Smith   /* copy over the A part */
1268ec8511deSBarry Smith   Aloc = (Mat_SeqAIJ*)a->A->data;
1269*273d9f13SBarry Smith   m = a->A->m; ai = Aloc->i; aj = Aloc->j; array = Aloc->a;
1270b7c46309SBarry Smith   row = a->rstart;
1271dbb450caSBarry Smith   for (i=0; i<ai[m]+shift; i++) {aj[i] += a->cstart + shift;}
1272b7c46309SBarry Smith   for (i=0; i<m; i++) {
1273416022c9SBarry Smith     ierr = MatSetValues(B,ai[i+1]-ai[i],aj,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
1274b7c46309SBarry Smith     row++; array += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
1275b7c46309SBarry Smith   }
1276b7c46309SBarry Smith   aj = Aloc->j;
12774af08d9eSBarry Smith   for (i=0; i<ai[m]+shift; i++) {aj[i] -= a->cstart + shift;}
1278b7c46309SBarry Smith 
1279b7c46309SBarry Smith   /* copy over the B part */
1280ec8511deSBarry Smith   Aloc = (Mat_SeqAIJ*)a->B->data;
1281*273d9f13SBarry Smith   m = a->B->m;  ai = Aloc->i; aj = Aloc->j; array = Aloc->a;
1282b7c46309SBarry Smith   row = a->rstart;
12830452661fSBarry Smith   ct = cols = (int*)PetscMalloc((1+ai[m]-shift)*sizeof(int));CHKPTRQ(cols);
1284dbb450caSBarry Smith   for (i=0; i<ai[m]+shift; i++) {cols[i] = a->garray[aj[i]+shift];}
1285b7c46309SBarry Smith   for (i=0; i<m; i++) {
1286416022c9SBarry Smith     ierr = MatSetValues(B,ai[i+1]-ai[i],cols,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
1287b7c46309SBarry Smith     row++; array += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
1288b7c46309SBarry Smith   }
1289606d414cSSatish Balay   ierr = PetscFree(ct);CHKERRQ(ierr);
12906d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
12916d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
12927c922b88SBarry Smith   if (matout) {
12930de55854SLois Curfman McInnes     *matout = B;
12940de55854SLois Curfman McInnes   } else {
1295*273d9f13SBarry Smith     ierr = MatHeaderCopy(A,B);CHKERRQ(ierr);
12960de55854SLois Curfman McInnes   }
12973a40ed3dSBarry Smith   PetscFunctionReturn(0);
1298b7c46309SBarry Smith }
1299b7c46309SBarry Smith 
13005615d1e5SSatish Balay #undef __FUNC__
1301b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatDiagonalScale_MPIAIJ"
13024b967eb1SSatish Balay int MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr)
1303a008b906SSatish Balay {
13044b967eb1SSatish Balay   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
13054b967eb1SSatish Balay   Mat        a = aij->A,b = aij->B;
1306a008b906SSatish Balay   int        ierr,s1,s2,s3;
1307a008b906SSatish Balay 
13083a40ed3dSBarry Smith   PetscFunctionBegin;
13094b967eb1SSatish Balay   ierr = MatGetLocalSize(mat,&s2,&s3);CHKERRQ(ierr);
13104b967eb1SSatish Balay   if (rr) {
1311e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr);
131229bbc08cSBarry Smith     if (s1!=s3) SETERRQ(PETSC_ERR_ARG_SIZ,"right vector non-conforming local size");
13134b967eb1SSatish Balay     /* Overlap communication with computation. */
131443a90d84SBarry Smith     ierr = VecScatterBegin(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx);CHKERRQ(ierr);
1315a008b906SSatish Balay   }
13164b967eb1SSatish Balay   if (ll) {
1317e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr);
131829bbc08cSBarry Smith     if (s1!=s2) SETERRQ(PETSC_ERR_ARG_SIZ,"left vector non-conforming local size");
1319f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,ll,0);CHKERRQ(ierr);
13204b967eb1SSatish Balay   }
13214b967eb1SSatish Balay   /* scale  the diagonal block */
1322f830108cSBarry Smith   ierr = (*a->ops->diagonalscale)(a,ll,rr);CHKERRQ(ierr);
13234b967eb1SSatish Balay 
13244b967eb1SSatish Balay   if (rr) {
13254b967eb1SSatish Balay     /* Do a scatter end and then right scale the off-diagonal block */
132643a90d84SBarry Smith     ierr = VecScatterEnd(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx);CHKERRQ(ierr);
1327f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,0,aij->lvec);CHKERRQ(ierr);
13284b967eb1SSatish Balay   }
13294b967eb1SSatish Balay 
13303a40ed3dSBarry Smith   PetscFunctionReturn(0);
1331a008b906SSatish Balay }
1332a008b906SSatish Balay 
1333a008b906SSatish Balay 
13345615d1e5SSatish Balay #undef __FUNC__
1335b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatPrintHelp_MPIAIJ"
13368f6be9afSLois Curfman McInnes int MatPrintHelp_MPIAIJ(Mat A)
1337682d7d0cSBarry Smith {
1338682d7d0cSBarry Smith   Mat_MPIAIJ *a   = (Mat_MPIAIJ*)A->data;
13393a40ed3dSBarry Smith   int        ierr;
1340682d7d0cSBarry Smith 
13413a40ed3dSBarry Smith   PetscFunctionBegin;
13423a40ed3dSBarry Smith   if (!a->rank) {
13433a40ed3dSBarry Smith     ierr = MatPrintHelp_SeqAIJ(a->A);CHKERRQ(ierr);
13443a40ed3dSBarry Smith   }
13453a40ed3dSBarry Smith   PetscFunctionReturn(0);
1346682d7d0cSBarry Smith }
1347682d7d0cSBarry Smith 
13485615d1e5SSatish Balay #undef __FUNC__
1349b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatGetBlockSize_MPIAIJ"
13508f6be9afSLois Curfman McInnes int MatGetBlockSize_MPIAIJ(Mat A,int *bs)
13515a838052SSatish Balay {
13523a40ed3dSBarry Smith   PetscFunctionBegin;
13535a838052SSatish Balay   *bs = 1;
13543a40ed3dSBarry Smith   PetscFunctionReturn(0);
13555a838052SSatish Balay }
13565615d1e5SSatish Balay #undef __FUNC__
1357b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatSetUnfactored_MPIAIJ"
13588f6be9afSLois Curfman McInnes int MatSetUnfactored_MPIAIJ(Mat A)
1359bb5a7306SBarry Smith {
1360bb5a7306SBarry Smith   Mat_MPIAIJ *a   = (Mat_MPIAIJ*)A->data;
1361bb5a7306SBarry Smith   int        ierr;
13623a40ed3dSBarry Smith 
13633a40ed3dSBarry Smith   PetscFunctionBegin;
1364bb5a7306SBarry Smith   ierr = MatSetUnfactored(a->A);CHKERRQ(ierr);
13653a40ed3dSBarry Smith   PetscFunctionReturn(0);
1366bb5a7306SBarry Smith }
1367bb5a7306SBarry Smith 
1368d4bb536fSBarry Smith #undef __FUNC__
1369b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatEqual_MPIAIJ"
1370d4bb536fSBarry Smith int MatEqual_MPIAIJ(Mat A,Mat B,PetscTruth *flag)
1371d4bb536fSBarry Smith {
1372d4bb536fSBarry Smith   Mat_MPIAIJ *matB = (Mat_MPIAIJ*)B->data,*matA = (Mat_MPIAIJ*)A->data;
1373d4bb536fSBarry Smith   Mat        a,b,c,d;
1374d4bb536fSBarry Smith   PetscTruth flg;
1375d4bb536fSBarry Smith   int        ierr;
1376d4bb536fSBarry Smith 
13773a40ed3dSBarry Smith   PetscFunctionBegin;
1378*273d9f13SBarry Smith   ierr = PetscTypeCompare((PetscObject)B,MATMPIAIJ,&flg);CHKERRQ(ierr);
1379*273d9f13SBarry Smith   if (!flg) SETERRQ(PETSC_ERR_ARG_INCOMP,"Matrices must be same type");
1380d4bb536fSBarry Smith   a = matA->A; b = matA->B;
1381d4bb536fSBarry Smith   c = matB->A; d = matB->B;
1382d4bb536fSBarry Smith 
1383d4bb536fSBarry Smith   ierr = MatEqual(a,c,&flg);CHKERRQ(ierr);
1384d4bb536fSBarry Smith   if (flg == PETSC_TRUE) {
1385d4bb536fSBarry Smith     ierr = MatEqual(b,d,&flg);CHKERRQ(ierr);
1386d4bb536fSBarry Smith   }
1387ca161407SBarry Smith   ierr = MPI_Allreduce(&flg,flag,1,MPI_INT,MPI_LAND,A->comm);CHKERRQ(ierr);
13883a40ed3dSBarry Smith   PetscFunctionReturn(0);
1389d4bb536fSBarry Smith }
1390d4bb536fSBarry Smith 
1391cb5b572fSBarry Smith #undef __FUNC__
1392b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatCopy_MPIAIJ"
1393cb5b572fSBarry Smith int MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str)
1394cb5b572fSBarry Smith {
1395cb5b572fSBarry Smith   int        ierr;
1396cb5b572fSBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;
1397cb5b572fSBarry Smith   Mat_MPIAIJ *b = (Mat_MPIAIJ *)B->data;
1398*273d9f13SBarry Smith   PetscTruth flg;
1399cb5b572fSBarry Smith 
1400cb5b572fSBarry Smith   PetscFunctionBegin;
1401*273d9f13SBarry Smith   ierr = PetscTypeCompare((PetscObject)B,MATMPIAIJ,&flg);CHKERRQ(ierr);
1402*273d9f13SBarry Smith   if (str != SAME_NONZERO_PATTERN || !flg) {
1403cb5b572fSBarry Smith     /* because of the column compression in the off-processor part of the matrix a->B,
1404cb5b572fSBarry Smith        the number of columns in a->B and b->B may be different, hence we cannot call
1405cb5b572fSBarry Smith        the MatCopy() directly on the two parts. If need be, we can provide a more
1406cb5b572fSBarry Smith        efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices
1407cb5b572fSBarry Smith        then copying the submatrices */
1408cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1409cb5b572fSBarry Smith   } else {
1410cb5b572fSBarry Smith     ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr);
1411cb5b572fSBarry Smith     ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr);
1412cb5b572fSBarry Smith   }
1413cb5b572fSBarry Smith   PetscFunctionReturn(0);
1414cb5b572fSBarry Smith }
1415cb5b572fSBarry Smith 
1416*273d9f13SBarry Smith #undef __FUNC__
1417*273d9f13SBarry Smith #define __FUNC__ /*<a name="MatSetUpPreallocation_MPIAIJ"></a>*/"MatSetUpPreallocation_MPIAIJ"
1418*273d9f13SBarry Smith int MatSetUpPreallocation_MPIAIJ(Mat A)
1419*273d9f13SBarry Smith {
1420*273d9f13SBarry Smith   int        ierr;
1421*273d9f13SBarry Smith 
1422*273d9f13SBarry Smith   PetscFunctionBegin;
1423*273d9f13SBarry Smith   ierr =  MatMPIAIJSetPreallocation(A,PETSC_DEFAULT,0,PETSC_DEFAULT,0);CHKERRQ(ierr);
1424*273d9f13SBarry Smith   PetscFunctionReturn(0);
1425*273d9f13SBarry Smith }
1426*273d9f13SBarry Smith 
1427ca44d042SBarry Smith EXTERN int MatDuplicate_MPIAIJ(Mat,MatDuplicateOption,Mat *);
1428ca44d042SBarry Smith EXTERN int MatIncreaseOverlap_MPIAIJ(Mat,int,IS *,int);
1429ca44d042SBarry Smith EXTERN int MatFDColoringCreate_MPIAIJ(Mat,ISColoring,MatFDColoring);
1430ca44d042SBarry Smith EXTERN int MatGetSubMatrices_MPIAIJ (Mat,int,IS *,IS *,MatReuse,Mat **);
1431ca44d042SBarry Smith EXTERN int MatGetSubMatrix_MPIAIJ (Mat,IS,IS,int,MatReuse,Mat *);
143200e6dbe6SBarry Smith 
14338a729477SBarry Smith /* -------------------------------------------------------------------*/
1434cda55fadSBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ,
1435cda55fadSBarry Smith        MatGetRow_MPIAIJ,
1436cda55fadSBarry Smith        MatRestoreRow_MPIAIJ,
1437cda55fadSBarry Smith        MatMult_MPIAIJ,
1438cda55fadSBarry Smith        MatMultAdd_MPIAIJ,
14397c922b88SBarry Smith        MatMultTranspose_MPIAIJ,
14407c922b88SBarry Smith        MatMultTransposeAdd_MPIAIJ,
1441cda55fadSBarry Smith        0,
1442cda55fadSBarry Smith        0,
1443cda55fadSBarry Smith        0,
1444cda55fadSBarry Smith        0,
1445cda55fadSBarry Smith        0,
1446cda55fadSBarry Smith        0,
144744a69424SLois Curfman McInnes        MatRelax_MPIAIJ,
1448b7c46309SBarry Smith        MatTranspose_MPIAIJ,
1449cda55fadSBarry Smith        MatGetInfo_MPIAIJ,
1450cda55fadSBarry Smith        MatEqual_MPIAIJ,
1451cda55fadSBarry Smith        MatGetDiagonal_MPIAIJ,
1452cda55fadSBarry Smith        MatDiagonalScale_MPIAIJ,
1453cda55fadSBarry Smith        MatNorm_MPIAIJ,
1454cda55fadSBarry Smith        MatAssemblyBegin_MPIAIJ,
1455cda55fadSBarry Smith        MatAssemblyEnd_MPIAIJ,
14561eb62cbbSBarry Smith        0,
1457cda55fadSBarry Smith        MatSetOption_MPIAIJ,
1458cda55fadSBarry Smith        MatZeroEntries_MPIAIJ,
1459cda55fadSBarry Smith        MatZeroRows_MPIAIJ,
1460cda55fadSBarry Smith        0,
1461cda55fadSBarry Smith        0,
1462cda55fadSBarry Smith        0,
1463cda55fadSBarry Smith        0,
1464*273d9f13SBarry Smith        MatSetUpPreallocation_MPIAIJ,
1465*273d9f13SBarry Smith        0,
1466cda55fadSBarry Smith        MatGetOwnershipRange_MPIAIJ,
1467cda55fadSBarry Smith        0,
1468cda55fadSBarry Smith        0,
1469cda55fadSBarry Smith        0,
1470cda55fadSBarry Smith        0,
14712e8a6d31SBarry Smith        MatDuplicate_MPIAIJ,
1472cda55fadSBarry Smith        0,
1473cda55fadSBarry Smith        0,
1474cda55fadSBarry Smith        0,
1475cda55fadSBarry Smith        0,
1476cda55fadSBarry Smith        0,
1477cda55fadSBarry Smith        MatGetSubMatrices_MPIAIJ,
1478cda55fadSBarry Smith        MatIncreaseOverlap_MPIAIJ,
1479cda55fadSBarry Smith        MatGetValues_MPIAIJ,
1480cb5b572fSBarry Smith        MatCopy_MPIAIJ,
1481052efed2SBarry Smith        MatPrintHelp_MPIAIJ,
1482cda55fadSBarry Smith        MatScale_MPIAIJ,
1483cda55fadSBarry Smith        0,
1484cda55fadSBarry Smith        0,
1485cda55fadSBarry Smith        0,
1486cda55fadSBarry Smith        MatGetBlockSize_MPIAIJ,
1487cda55fadSBarry Smith        0,
1488cda55fadSBarry Smith        0,
1489cda55fadSBarry Smith        0,
1490cda55fadSBarry Smith        0,
1491cda55fadSBarry Smith        MatFDColoringCreate_MPIAIJ,
1492cda55fadSBarry Smith        0,
1493cda55fadSBarry Smith        MatSetUnfactored_MPIAIJ,
1494cda55fadSBarry Smith        0,
1495cda55fadSBarry Smith        0,
1496cda55fadSBarry Smith        MatGetSubMatrix_MPIAIJ,
1497e03a110bSBarry Smith        MatDestroy_MPIAIJ,
1498e03a110bSBarry Smith        MatView_MPIAIJ,
1499cda55fadSBarry Smith        MatGetMaps_Petsc};
150036ce4990SBarry Smith 
15012e8a6d31SBarry Smith /* ----------------------------------------------------------------------------------------*/
15022e8a6d31SBarry Smith 
1503fb2e594dSBarry Smith EXTERN_C_BEGIN
15042e8a6d31SBarry Smith #undef __FUNC__
1505b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatStoreValues_MPIAIJ"
15062e8a6d31SBarry Smith int MatStoreValues_MPIAIJ(Mat mat)
15072e8a6d31SBarry Smith {
15082e8a6d31SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data;
15092e8a6d31SBarry Smith   int        ierr;
15102e8a6d31SBarry Smith 
15112e8a6d31SBarry Smith   PetscFunctionBegin;
15122e8a6d31SBarry Smith   ierr = MatStoreValues(aij->A);CHKERRQ(ierr);
15132e8a6d31SBarry Smith   ierr = MatStoreValues(aij->B);CHKERRQ(ierr);
15142e8a6d31SBarry Smith   PetscFunctionReturn(0);
15152e8a6d31SBarry Smith }
1516fb2e594dSBarry Smith EXTERN_C_END
15172e8a6d31SBarry Smith 
1518fb2e594dSBarry Smith EXTERN_C_BEGIN
15192e8a6d31SBarry Smith #undef __FUNC__
1520b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatRetrieveValues_MPIAIJ"
15212e8a6d31SBarry Smith int MatRetrieveValues_MPIAIJ(Mat mat)
15222e8a6d31SBarry Smith {
15232e8a6d31SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data;
15242e8a6d31SBarry Smith   int        ierr;
15252e8a6d31SBarry Smith 
15262e8a6d31SBarry Smith   PetscFunctionBegin;
15272e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr);
15282e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr);
15292e8a6d31SBarry Smith   PetscFunctionReturn(0);
15302e8a6d31SBarry Smith }
1531fb2e594dSBarry Smith EXTERN_C_END
15328a729477SBarry Smith 
1533e090d566SSatish Balay #include "petscpc.h"
153427508adbSBarry Smith EXTERN_C_BEGIN
1535ca44d042SBarry Smith EXTERN int MatGetDiagonalBlock_MPIAIJ(Mat,PetscTruth *,MatReuse,Mat *);
153627508adbSBarry Smith EXTERN_C_END
153727508adbSBarry Smith 
1538*273d9f13SBarry Smith EXTERN_C_BEGIN
15395615d1e5SSatish Balay #undef __FUNC__
1540*273d9f13SBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatCreate_MPIAIJ"
1541*273d9f13SBarry Smith int MatCreate_MPIAIJ(Mat B)
15428a729477SBarry Smith {
154344cd7ae7SLois Curfman McInnes   Mat_MPIAIJ   *b;
1544f1af5d2fSBarry Smith   int          ierr,i,size;
1545416022c9SBarry Smith 
15463a40ed3dSBarry Smith   PetscFunctionBegin;
1547*273d9f13SBarry Smith   ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr);
15481d55c564SBarry Smith 
154944cd7ae7SLois Curfman McInnes   B->data         = (void*)(b = PetscNew(Mat_MPIAIJ));CHKPTRQ(b);
1550549d3d68SSatish Balay   ierr            = PetscMemzero(b,sizeof(Mat_MPIAIJ));CHKERRQ(ierr);
1551549d3d68SSatish Balay   ierr            = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
155244cd7ae7SLois Curfman McInnes   B->factor       = 0;
155344cd7ae7SLois Curfman McInnes   B->assembled    = PETSC_FALSE;
155490f02eecSBarry Smith   B->mapping      = 0;
1555d6dfbf8fSBarry Smith 
155647794344SBarry Smith   B->insertmode      = NOT_SET_VALUES;
15579eb4d147SSatish Balay   b->size            = size;
1558*273d9f13SBarry Smith   ierr = MPI_Comm_rank(B->comm,&b->rank);CHKERRQ(ierr);
15591eb62cbbSBarry Smith 
1560*273d9f13SBarry Smith   ierr = PetscSplitOwnership(B->comm,&B->m,&B->M);CHKERRQ(ierr);
1561*273d9f13SBarry Smith   ierr = PetscSplitOwnership(B->comm,&B->n,&B->N);CHKERRQ(ierr);
15621eb62cbbSBarry Smith 
1563c7fcc2eaSBarry Smith   /* the information in the maps duplicates the information computed below, eventually
1564c7fcc2eaSBarry Smith      we should remove the duplicate information that is not contained in the maps */
1565*273d9f13SBarry Smith   ierr = MapCreateMPI(B->comm,B->m,B->M,&B->rmap);CHKERRQ(ierr);
1566*273d9f13SBarry Smith   ierr = MapCreateMPI(B->comm,B->n,B->N,&B->cmap);CHKERRQ(ierr);
1567c7fcc2eaSBarry Smith 
15681eb62cbbSBarry Smith   /* build local table of row and column ownerships */
156944cd7ae7SLois Curfman McInnes   b->rowners = (int*)PetscMalloc(2*(b->size+2)*sizeof(int));CHKPTRQ(b->rowners);
1570f09e8eb9SSatish Balay   PLogObjectMemory(B,2*(b->size+2)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_MPIAIJ));
1571603f58a4SSatish Balay   b->cowners = b->rowners + b->size + 2;
1572*273d9f13SBarry Smith   ierr = MPI_Allgather(&B->m,1,MPI_INT,b->rowners+1,1,MPI_INT,B->comm);CHKERRQ(ierr);
157344cd7ae7SLois Curfman McInnes   b->rowners[0] = 0;
157444cd7ae7SLois Curfman McInnes   for (i=2; i<=b->size; i++) {
157544cd7ae7SLois Curfman McInnes     b->rowners[i] += b->rowners[i-1];
15768a729477SBarry Smith   }
157744cd7ae7SLois Curfman McInnes   b->rstart = b->rowners[b->rank];
157844cd7ae7SLois Curfman McInnes   b->rend   = b->rowners[b->rank+1];
1579*273d9f13SBarry Smith   ierr = MPI_Allgather(&B->n,1,MPI_INT,b->cowners+1,1,MPI_INT,B->comm);CHKERRQ(ierr);
158044cd7ae7SLois Curfman McInnes   b->cowners[0] = 0;
158144cd7ae7SLois Curfman McInnes   for (i=2; i<=b->size; i++) {
158244cd7ae7SLois Curfman McInnes     b->cowners[i] += b->cowners[i-1];
15838a729477SBarry Smith   }
158444cd7ae7SLois Curfman McInnes   b->cstart = b->cowners[b->rank];
158544cd7ae7SLois Curfman McInnes   b->cend   = b->cowners[b->rank+1];
15868a729477SBarry Smith 
15871eb62cbbSBarry Smith   /* build cache for off array entries formed */
15887e2c5f70SBarry Smith   ierr = MatStashCreate_Private(B->comm,1,&B->stash);CHKERRQ(ierr);
15897c922b88SBarry Smith   b->donotstash  = PETSC_FALSE;
159044cd7ae7SLois Curfman McInnes   b->colmap      = 0;
159144cd7ae7SLois Curfman McInnes   b->garray      = 0;
15927c922b88SBarry Smith   b->roworiented = PETSC_TRUE;
15938a729477SBarry Smith 
15941eb62cbbSBarry Smith   /* stuff used for matrix vector multiply */
15957c922b88SBarry Smith   b->lvec      = PETSC_NULL;
15967c922b88SBarry Smith   b->Mvctx     = PETSC_NULL;
15978a729477SBarry Smith 
15987a0afa10SBarry Smith   /* stuff for MatGetRow() */
159944cd7ae7SLois Curfman McInnes   b->rowindices   = 0;
160044cd7ae7SLois Curfman McInnes   b->rowvalues    = 0;
160144cd7ae7SLois Curfman McInnes   b->getrowactive = PETSC_FALSE;
16027a0afa10SBarry Smith 
1603f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
16042e8a6d31SBarry Smith                                      "MatStoreValues_MPIAIJ",
16050c97f09dSSatish Balay                                      MatStoreValues_MPIAIJ);CHKERRQ(ierr);
1606f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
16072e8a6d31SBarry Smith                                      "MatRetrieveValues_MPIAIJ",
16080c97f09dSSatish Balay                                      MatRetrieveValues_MPIAIJ);CHKERRQ(ierr);
1609f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetDiagonalBlock_C",
16105ef9f2a5SBarry Smith                                      "MatGetDiagonalBlock_MPIAIJ",
16110c97f09dSSatish Balay                                      MatGetDiagonalBlock_MPIAIJ);CHKERRQ(ierr);
16123a40ed3dSBarry Smith   PetscFunctionReturn(0);
1613d6dfbf8fSBarry Smith }
1614*273d9f13SBarry Smith EXTERN_C_END
1615c74985f6SBarry Smith 
16165615d1e5SSatish Balay #undef __FUNC__
1617b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatDuplicate_MPIAIJ"
16182e8a6d31SBarry Smith int MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
1619d6dfbf8fSBarry Smith {
1620d6dfbf8fSBarry Smith   Mat        mat;
1621416022c9SBarry Smith   Mat_MPIAIJ *a,*oldmat = (Mat_MPIAIJ*)matin->data;
1622f1af5d2fSBarry Smith   int        ierr,len = 0;
1623d6dfbf8fSBarry Smith 
16243a40ed3dSBarry Smith   PetscFunctionBegin;
1625416022c9SBarry Smith   *newmat       = 0;
1626*273d9f13SBarry Smith   ierr = MatCreate(matin->comm,matin->m,matin->n,matin->M,matin->N,&mat);CHKERRQ(ierr);
1627*273d9f13SBarry Smith   ierr = MatSetType(mat,MATMPIAIJ);CHKERRQ(ierr);
1628*273d9f13SBarry Smith   a    = (Mat_MPIAIJ*)mat->data;
1629549d3d68SSatish Balay   ierr              = PetscMemcpy(mat->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
1630d6dfbf8fSBarry Smith   mat->factor       = matin->factor;
1631c456f294SBarry Smith   mat->assembled    = PETSC_TRUE;
1632e7641de0SSatish Balay   mat->insertmode   = NOT_SET_VALUES;
1633*273d9f13SBarry Smith   mat->preallocated = PETSC_TRUE;
1634d6dfbf8fSBarry Smith 
1635416022c9SBarry Smith   a->rstart       = oldmat->rstart;
1636416022c9SBarry Smith   a->rend         = oldmat->rend;
1637416022c9SBarry Smith   a->cstart       = oldmat->cstart;
1638416022c9SBarry Smith   a->cend         = oldmat->cend;
163917699dbbSLois Curfman McInnes   a->size         = oldmat->size;
164017699dbbSLois Curfman McInnes   a->rank         = oldmat->rank;
1641e7641de0SSatish Balay   a->donotstash   = oldmat->donotstash;
1642e7641de0SSatish Balay   a->roworiented  = oldmat->roworiented;
1643e7641de0SSatish Balay   a->rowindices   = 0;
1644bcd2baecSBarry Smith   a->rowvalues    = 0;
1645bcd2baecSBarry Smith   a->getrowactive = PETSC_FALSE;
1646d6dfbf8fSBarry Smith 
1647549d3d68SSatish Balay   ierr       = PetscMemcpy(a->rowners,oldmat->rowners,2*(a->size+2)*sizeof(int));CHKERRQ(ierr);
16488798bf22SSatish Balay   ierr       = MatStashCreate_Private(matin->comm,1,&mat->stash);CHKERRQ(ierr);
16492ee70a88SLois Curfman McInnes   if (oldmat->colmap) {
1650aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
16510f5bd95cSBarry Smith     ierr = PetscTableCreateCopy(oldmat->colmap,&a->colmap);CHKERRQ(ierr);
1652b1fc9764SSatish Balay #else
1653*273d9f13SBarry Smith     a->colmap = (int*)PetscMalloc((mat->N)*sizeof(int));CHKPTRQ(a->colmap);
1654*273d9f13SBarry Smith     PLogObjectMemory(mat,(mat->N)*sizeof(int));
1655*273d9f13SBarry Smith     ierr      = PetscMemcpy(a->colmap,oldmat->colmap,(mat->N)*sizeof(int));CHKERRQ(ierr);
1656b1fc9764SSatish Balay #endif
1657416022c9SBarry Smith   } else a->colmap = 0;
16583f41c07dSBarry Smith   if (oldmat->garray) {
1659*273d9f13SBarry Smith     len = oldmat->B->n;
16603f41c07dSBarry Smith     a->garray = (int*)PetscMalloc((len+1)*sizeof(int));CHKPTRQ(a->garray);
1661464493b3SBarry Smith     PLogObjectMemory(mat,len*sizeof(int));
1662549d3d68SSatish Balay     if (len) { ierr = PetscMemcpy(a->garray,oldmat->garray,len*sizeof(int));CHKERRQ(ierr); }
1663416022c9SBarry Smith   } else a->garray = 0;
1664d6dfbf8fSBarry Smith 
1665416022c9SBarry Smith   ierr =  VecDuplicate(oldmat->lvec,&a->lvec);CHKERRQ(ierr);
1666416022c9SBarry Smith   PLogObjectParent(mat,a->lvec);
1667a56f8943SBarry Smith   ierr =  VecScatterCopy(oldmat->Mvctx,&a->Mvctx);CHKERRQ(ierr);
1668416022c9SBarry Smith   PLogObjectParent(mat,a->Mvctx);
16692e8a6d31SBarry Smith   ierr =  MatDuplicate(oldmat->A,cpvalues,&a->A);CHKERRQ(ierr);
1670416022c9SBarry Smith   PLogObjectParent(mat,a->A);
16712e8a6d31SBarry Smith   ierr =  MatDuplicate(oldmat->B,cpvalues,&a->B);CHKERRQ(ierr);
1672416022c9SBarry Smith   PLogObjectParent(mat,a->B);
167327508adbSBarry Smith   ierr = FListDuplicate(matin->qlist,&mat->qlist);CHKERRQ(ierr);
16748a729477SBarry Smith   *newmat = mat;
16753a40ed3dSBarry Smith   PetscFunctionReturn(0);
16768a729477SBarry Smith }
1677416022c9SBarry Smith 
1678e090d566SSatish Balay #include "petscsys.h"
1679416022c9SBarry Smith 
1680*273d9f13SBarry Smith EXTERN_C_BEGIN
16815615d1e5SSatish Balay #undef __FUNC__
1682b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatLoad_MPIAIJ"
168319bcc07fSBarry Smith int MatLoad_MPIAIJ(Viewer viewer,MatType type,Mat *newmat)
1684416022c9SBarry Smith {
1685d65a2f8fSBarry Smith   Mat          A;
1686d65a2f8fSBarry Smith   Scalar       *vals,*svals;
168719bcc07fSBarry Smith   MPI_Comm     comm = ((PetscObject)viewer)->comm;
1688416022c9SBarry Smith   MPI_Status   status;
16893a40ed3dSBarry Smith   int          i,nz,ierr,j,rstart,rend,fd;
169017699dbbSLois Curfman McInnes   int          header[4],rank,size,*rowlengths = 0,M,N,m,*rowners,maxnz,*cols;
1691d65a2f8fSBarry Smith   int          *ourlens,*sndcounts = 0,*procsnz = 0,*offlens,jj,*mycols,*smycols;
1692b362ba68SBarry Smith   int          tag = ((PetscObject)viewer)->tag,cend,cstart,n;
1693416022c9SBarry Smith 
16943a40ed3dSBarry Smith   PetscFunctionBegin;
16951dab6e02SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
16961dab6e02SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
169717699dbbSLois Curfman McInnes   if (!rank) {
169890ace30eSBarry Smith     ierr = ViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
16990752156aSBarry Smith     ierr = PetscBinaryRead(fd,(char *)header,4,PETSC_INT);CHKERRQ(ierr);
170029bbc08cSBarry Smith     if (header[0] != MAT_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
1701d64ed03dSBarry Smith     if (header[3] < 0) {
170229bbc08cSBarry Smith       SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix in special format on disk, cannot load as MPIAIJ");
1703d64ed03dSBarry Smith     }
17046c5fab8fSBarry Smith   }
17056c5fab8fSBarry Smith 
1706ca161407SBarry Smith   ierr = MPI_Bcast(header+1,3,MPI_INT,0,comm);CHKERRQ(ierr);
1707416022c9SBarry Smith   M = header[1]; N = header[2];
1708416022c9SBarry Smith   /* determine ownership of all rows */
170917699dbbSLois Curfman McInnes   m = M/size + ((M % size) > rank);
17100452661fSBarry Smith   rowners = (int*)PetscMalloc((size+2)*sizeof(int));CHKPTRQ(rowners);
1711ca161407SBarry Smith   ierr = MPI_Allgather(&m,1,MPI_INT,rowners+1,1,MPI_INT,comm);CHKERRQ(ierr);
1712416022c9SBarry Smith   rowners[0] = 0;
171317699dbbSLois Curfman McInnes   for (i=2; i<=size; i++) {
1714416022c9SBarry Smith     rowners[i] += rowners[i-1];
1715416022c9SBarry Smith   }
171617699dbbSLois Curfman McInnes   rstart = rowners[rank];
171717699dbbSLois Curfman McInnes   rend   = rowners[rank+1];
1718416022c9SBarry Smith 
1719416022c9SBarry Smith   /* distribute row lengths to all processors */
17200452661fSBarry Smith   ourlens = (int*)PetscMalloc(2*(rend-rstart)*sizeof(int));CHKPTRQ(ourlens);
1721416022c9SBarry Smith   offlens = ourlens + (rend-rstart);
172217699dbbSLois Curfman McInnes   if (!rank) {
17230452661fSBarry Smith     rowlengths = (int*)PetscMalloc(M*sizeof(int));CHKPTRQ(rowlengths);
17240752156aSBarry Smith     ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
17250452661fSBarry Smith     sndcounts = (int*)PetscMalloc(size*sizeof(int));CHKPTRQ(sndcounts);
172617699dbbSLois Curfman McInnes     for (i=0; i<size; i++) sndcounts[i] = rowners[i+1] - rowners[i];
1727ca161407SBarry Smith     ierr = MPI_Scatterv(rowlengths,sndcounts,rowners,MPI_INT,ourlens,rend-rstart,MPI_INT,0,comm);CHKERRQ(ierr);
1728606d414cSSatish Balay     ierr = PetscFree(sndcounts);CHKERRQ(ierr);
17293a40ed3dSBarry Smith   } else {
1730ca161407SBarry Smith     ierr = MPI_Scatterv(0,0,0,MPI_INT,ourlens,rend-rstart,MPI_INT,0,comm);CHKERRQ(ierr);
1731416022c9SBarry Smith   }
1732416022c9SBarry Smith 
173317699dbbSLois Curfman McInnes   if (!rank) {
1734416022c9SBarry Smith     /* calculate the number of nonzeros on each processor */
17350452661fSBarry Smith     procsnz = (int*)PetscMalloc(size*sizeof(int));CHKPTRQ(procsnz);
1736549d3d68SSatish Balay     ierr    = PetscMemzero(procsnz,size*sizeof(int));CHKERRQ(ierr);
173717699dbbSLois Curfman McInnes     for (i=0; i<size; i++) {
1738416022c9SBarry Smith       for (j=rowners[i]; j< rowners[i+1]; j++) {
1739416022c9SBarry Smith         procsnz[i] += rowlengths[j];
1740416022c9SBarry Smith       }
1741416022c9SBarry Smith     }
1742606d414cSSatish Balay     ierr = PetscFree(rowlengths);CHKERRQ(ierr);
1743416022c9SBarry Smith 
1744416022c9SBarry Smith     /* determine max buffer needed and allocate it */
1745416022c9SBarry Smith     maxnz = 0;
174617699dbbSLois Curfman McInnes     for (i=0; i<size; i++) {
17470452661fSBarry Smith       maxnz = PetscMax(maxnz,procsnz[i]);
1748416022c9SBarry Smith     }
17490452661fSBarry Smith     cols = (int*)PetscMalloc(maxnz*sizeof(int));CHKPTRQ(cols);
1750416022c9SBarry Smith 
1751416022c9SBarry Smith     /* read in my part of the matrix column indices  */
1752416022c9SBarry Smith     nz     = procsnz[0];
17530452661fSBarry Smith     mycols = (int*)PetscMalloc(nz*sizeof(int));CHKPTRQ(mycols);
17540752156aSBarry Smith     ierr   = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr);
1755d65a2f8fSBarry Smith 
1756d65a2f8fSBarry Smith     /* read in every one elses and ship off */
175717699dbbSLois Curfman McInnes     for (i=1; i<size; i++) {
1758d65a2f8fSBarry Smith       nz   = procsnz[i];
17590752156aSBarry Smith       ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr);
1760ca161407SBarry Smith       ierr = MPI_Send(cols,nz,MPI_INT,i,tag,comm);CHKERRQ(ierr);
1761d65a2f8fSBarry Smith     }
1762606d414cSSatish Balay     ierr = PetscFree(cols);CHKERRQ(ierr);
17633a40ed3dSBarry Smith   } else {
1764416022c9SBarry Smith     /* determine buffer space needed for message */
1765416022c9SBarry Smith     nz = 0;
1766416022c9SBarry Smith     for (i=0; i<m; i++) {
1767416022c9SBarry Smith       nz += ourlens[i];
1768416022c9SBarry Smith     }
17690452661fSBarry Smith     mycols = (int*)PetscMalloc(nz*sizeof(int));CHKPTRQ(mycols);
1770416022c9SBarry Smith 
1771416022c9SBarry Smith     /* receive message of column indices*/
1772ca161407SBarry Smith     ierr = MPI_Recv(mycols,nz,MPI_INT,0,tag,comm,&status);CHKERRQ(ierr);
1773ca161407SBarry Smith     ierr = MPI_Get_count(&status,MPI_INT,&maxnz);CHKERRQ(ierr);
177429bbc08cSBarry Smith     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
1775416022c9SBarry Smith   }
1776416022c9SBarry Smith 
1777b362ba68SBarry Smith   /* determine column ownership if matrix is not square */
1778b362ba68SBarry Smith   if (N != M) {
1779b362ba68SBarry Smith     n      = N/size + ((N % size) > rank);
1780b362ba68SBarry Smith     ierr   = MPI_Scan(&n,&cend,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr);
1781b362ba68SBarry Smith     cstart = cend - n;
1782b362ba68SBarry Smith   } else {
1783b362ba68SBarry Smith     cstart = rstart;
1784b362ba68SBarry Smith     cend   = rend;
1785fb2e594dSBarry Smith     n      = cend - cstart;
1786b362ba68SBarry Smith   }
1787b362ba68SBarry Smith 
1788416022c9SBarry Smith   /* loop over local rows, determining number of off diagonal entries */
1789549d3d68SSatish Balay   ierr = PetscMemzero(offlens,m*sizeof(int));CHKERRQ(ierr);
1790416022c9SBarry Smith   jj = 0;
1791416022c9SBarry Smith   for (i=0; i<m; i++) {
1792416022c9SBarry Smith     for (j=0; j<ourlens[i]; j++) {
1793b362ba68SBarry Smith       if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++;
1794416022c9SBarry Smith       jj++;
1795416022c9SBarry Smith     }
1796416022c9SBarry Smith   }
1797d65a2f8fSBarry Smith 
1798d65a2f8fSBarry Smith   /* create our matrix */
1799416022c9SBarry Smith   for (i=0; i<m; i++) {
1800416022c9SBarry Smith     ourlens[i] -= offlens[i];
1801416022c9SBarry Smith   }
1802b362ba68SBarry Smith   ierr = MatCreateMPIAIJ(comm,m,n,M,N,0,ourlens,0,offlens,newmat);CHKERRQ(ierr);
1803d65a2f8fSBarry Smith   A = *newmat;
1804fb2e594dSBarry Smith   ierr = MatSetOption(A,MAT_COLUMNS_SORTED);CHKERRQ(ierr);
1805d65a2f8fSBarry Smith   for (i=0; i<m; i++) {
1806d65a2f8fSBarry Smith     ourlens[i] += offlens[i];
1807d65a2f8fSBarry Smith   }
1808416022c9SBarry Smith 
180917699dbbSLois Curfman McInnes   if (!rank) {
18100452661fSBarry Smith     vals = (Scalar*)PetscMalloc(maxnz*sizeof(Scalar));CHKPTRQ(vals);
1811416022c9SBarry Smith 
1812416022c9SBarry Smith     /* read in my part of the matrix numerical values  */
1813416022c9SBarry Smith     nz   = procsnz[0];
18140752156aSBarry Smith     ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
1815d65a2f8fSBarry Smith 
1816d65a2f8fSBarry Smith     /* insert into matrix */
1817d65a2f8fSBarry Smith     jj      = rstart;
1818d65a2f8fSBarry Smith     smycols = mycols;
1819d65a2f8fSBarry Smith     svals   = vals;
1820d65a2f8fSBarry Smith     for (i=0; i<m; i++) {
1821d65a2f8fSBarry Smith       ierr = MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
1822d65a2f8fSBarry Smith       smycols += ourlens[i];
1823d65a2f8fSBarry Smith       svals   += ourlens[i];
1824d65a2f8fSBarry Smith       jj++;
1825416022c9SBarry Smith     }
1826416022c9SBarry Smith 
1827d65a2f8fSBarry Smith     /* read in other processors and ship out */
182817699dbbSLois Curfman McInnes     for (i=1; i<size; i++) {
1829416022c9SBarry Smith       nz   = procsnz[i];
18300752156aSBarry Smith       ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
1831ca161407SBarry Smith       ierr = MPI_Send(vals,nz,MPIU_SCALAR,i,A->tag,comm);CHKERRQ(ierr);
1832416022c9SBarry Smith     }
1833606d414cSSatish Balay     ierr = PetscFree(procsnz);CHKERRQ(ierr);
18343a40ed3dSBarry Smith   } else {
1835d65a2f8fSBarry Smith     /* receive numeric values */
18360452661fSBarry Smith     vals = (Scalar*)PetscMalloc(nz*sizeof(Scalar));CHKPTRQ(vals);
1837416022c9SBarry Smith 
1838d65a2f8fSBarry Smith     /* receive message of values*/
1839ca161407SBarry Smith     ierr = MPI_Recv(vals,nz,MPIU_SCALAR,0,A->tag,comm,&status);CHKERRQ(ierr);
1840ca161407SBarry Smith     ierr = MPI_Get_count(&status,MPIU_SCALAR,&maxnz);CHKERRQ(ierr);
184129bbc08cSBarry Smith     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
1842d65a2f8fSBarry Smith 
1843d65a2f8fSBarry Smith     /* insert into matrix */
1844d65a2f8fSBarry Smith     jj      = rstart;
1845d65a2f8fSBarry Smith     smycols = mycols;
1846d65a2f8fSBarry Smith     svals   = vals;
1847d65a2f8fSBarry Smith     for (i=0; i<m; i++) {
1848d65a2f8fSBarry Smith       ierr     = MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
1849d65a2f8fSBarry Smith       smycols += ourlens[i];
1850d65a2f8fSBarry Smith       svals   += ourlens[i];
1851d65a2f8fSBarry Smith       jj++;
1852d65a2f8fSBarry Smith     }
1853d65a2f8fSBarry Smith   }
1854606d414cSSatish Balay   ierr = PetscFree(ourlens);CHKERRQ(ierr);
1855606d414cSSatish Balay   ierr = PetscFree(vals);CHKERRQ(ierr);
1856606d414cSSatish Balay   ierr = PetscFree(mycols);CHKERRQ(ierr);
1857606d414cSSatish Balay   ierr = PetscFree(rowners);CHKERRQ(ierr);
1858d65a2f8fSBarry Smith 
18596d4a8577SBarry Smith   ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18606d4a8577SBarry Smith   ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18613a40ed3dSBarry Smith   PetscFunctionReturn(0);
1862416022c9SBarry Smith }
1863*273d9f13SBarry Smith EXTERN_C_END
1864a0ff6018SBarry Smith 
186529da9460SBarry Smith #undef __FUNC__
1866b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatGetSubMatrix_MPIAIJ"
1867a0ff6018SBarry Smith /*
186829da9460SBarry Smith     Not great since it makes two copies of the submatrix, first an SeqAIJ
186929da9460SBarry Smith   in local and then by concatenating the local matrices the end result.
187029da9460SBarry Smith   Writing it directly would be much like MatGetSubMatrices_MPIAIJ()
1871a0ff6018SBarry Smith */
18727b2a1423SBarry Smith int MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,int csize,MatReuse call,Mat *newmat)
1873a0ff6018SBarry Smith {
187400e6dbe6SBarry Smith   int        ierr,i,m,n,rstart,row,rend,nz,*cwork,size,rank,j;
18757e2c5f70SBarry Smith   int        *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend;
1876fee21e36SBarry Smith   Mat        *local,M,Mreuse;
187700e6dbe6SBarry Smith   Scalar     *vwork,*aa;
187800e6dbe6SBarry Smith   MPI_Comm   comm = mat->comm;
187900e6dbe6SBarry Smith   Mat_SeqAIJ *aij;
18807e2c5f70SBarry Smith 
1881a0ff6018SBarry Smith 
1882a0ff6018SBarry Smith   PetscFunctionBegin;
18831dab6e02SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
18841dab6e02SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
188500e6dbe6SBarry Smith 
1886fee21e36SBarry Smith   if (call ==  MAT_REUSE_MATRIX) {
1887fee21e36SBarry Smith     ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);CHKERRQ(ierr);
188829bbc08cSBarry Smith     if (!Mreuse) SETERRQ(1,"Submatrix passed in was not used before, cannot reuse");
1889fee21e36SBarry Smith     local = &Mreuse;
1890fee21e36SBarry Smith     ierr  = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&local);CHKERRQ(ierr);
1891fee21e36SBarry Smith   } else {
1892a0ff6018SBarry Smith     ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr);
1893fee21e36SBarry Smith     Mreuse = *local;
1894606d414cSSatish Balay     ierr = PetscFree(local);CHKERRQ(ierr);
1895fee21e36SBarry Smith   }
1896a0ff6018SBarry Smith 
1897a0ff6018SBarry Smith   /*
1898a0ff6018SBarry Smith       m - number of local rows
1899a0ff6018SBarry Smith       n - number of columns (same on all processors)
1900a0ff6018SBarry Smith       rstart - first row in new global matrix generated
1901a0ff6018SBarry Smith   */
1902fee21e36SBarry Smith   ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr);
1903a0ff6018SBarry Smith   if (call == MAT_INITIAL_MATRIX) {
1904fee21e36SBarry Smith     aij = (Mat_SeqAIJ*)(Mreuse)->data;
190529bbc08cSBarry Smith     if (aij->indexshift) SETERRQ(PETSC_ERR_SUP,"No support for index shifted matrix");
190600e6dbe6SBarry Smith     ii  = aij->i;
190700e6dbe6SBarry Smith     jj  = aij->j;
190800e6dbe6SBarry Smith 
1909a0ff6018SBarry Smith     /*
191000e6dbe6SBarry Smith         Determine the number of non-zeros in the diagonal and off-diagonal
191100e6dbe6SBarry Smith         portions of the matrix in order to do correct preallocation
1912a0ff6018SBarry Smith     */
191300e6dbe6SBarry Smith 
191400e6dbe6SBarry Smith     /* first get start and end of "diagonal" columns */
19156a6a5d1dSBarry Smith     if (csize == PETSC_DECIDE) {
191600e6dbe6SBarry Smith       nlocal = n/size + ((n % size) > rank);
19176a6a5d1dSBarry Smith     } else {
19186a6a5d1dSBarry Smith       nlocal = csize;
19196a6a5d1dSBarry Smith     }
1920ca161407SBarry Smith     ierr   = MPI_Scan(&nlocal,&rend,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr);
192100e6dbe6SBarry Smith     rstart = rend - nlocal;
19226a6a5d1dSBarry Smith     if (rank == size - 1 && rend != n) {
192329bbc08cSBarry Smith       SETERRQ(1,"Local column sizes do not add up to total number of columns");
19246a6a5d1dSBarry Smith     }
192500e6dbe6SBarry Smith 
192600e6dbe6SBarry Smith     /* next, compute all the lengths */
192700e6dbe6SBarry Smith     dlens = (int*)PetscMalloc((2*m+1)*sizeof(int));CHKPTRQ(dlens);
192800e6dbe6SBarry Smith     olens = dlens + m;
192900e6dbe6SBarry Smith     for (i=0; i<m; i++) {
193000e6dbe6SBarry Smith       jend = ii[i+1] - ii[i];
193100e6dbe6SBarry Smith       olen = 0;
193200e6dbe6SBarry Smith       dlen = 0;
193300e6dbe6SBarry Smith       for (j=0; j<jend; j++) {
193400e6dbe6SBarry Smith         if (*jj < rstart || *jj >= rend) olen++;
193500e6dbe6SBarry Smith         else dlen++;
193600e6dbe6SBarry Smith         jj++;
193700e6dbe6SBarry Smith       }
193800e6dbe6SBarry Smith       olens[i] = olen;
193900e6dbe6SBarry Smith       dlens[i] = dlen;
194000e6dbe6SBarry Smith     }
19412d207970SBarry Smith     ierr = MatCreateMPIAIJ(comm,m,nlocal,PETSC_DECIDE,n,0,dlens,0,olens,&M);CHKERRQ(ierr);
1942606d414cSSatish Balay     ierr = PetscFree(dlens);CHKERRQ(ierr);
1943a0ff6018SBarry Smith   } else {
1944a0ff6018SBarry Smith     int ml,nl;
1945a0ff6018SBarry Smith 
1946a0ff6018SBarry Smith     M = *newmat;
1947a0ff6018SBarry Smith     ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr);
194829bbc08cSBarry Smith     if (ml != m) SETERRQ(PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
1949a0ff6018SBarry Smith     ierr = MatZeroEntries(M);CHKERRQ(ierr);
1950c48de900SBarry Smith     /*
1951c48de900SBarry Smith          The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
1952c48de900SBarry Smith        rather than the slower MatSetValues().
1953c48de900SBarry Smith     */
1954c48de900SBarry Smith     M->was_assembled = PETSC_TRUE;
1955c48de900SBarry Smith     M->assembled     = PETSC_FALSE;
1956a0ff6018SBarry Smith   }
1957a0ff6018SBarry Smith   ierr = MatGetOwnershipRange(M,&rstart,&rend);CHKERRQ(ierr);
1958fee21e36SBarry Smith   aij = (Mat_SeqAIJ*)(Mreuse)->data;
195929bbc08cSBarry Smith   if (aij->indexshift) SETERRQ(PETSC_ERR_SUP,"No support for index shifted matrix");
196000e6dbe6SBarry Smith   ii  = aij->i;
196100e6dbe6SBarry Smith   jj  = aij->j;
196200e6dbe6SBarry Smith   aa  = aij->a;
1963a0ff6018SBarry Smith   for (i=0; i<m; i++) {
1964a0ff6018SBarry Smith     row   = rstart + i;
196500e6dbe6SBarry Smith     nz    = ii[i+1] - ii[i];
196600e6dbe6SBarry Smith     cwork = jj;     jj += nz;
196700e6dbe6SBarry Smith     vwork = aa;     aa += nz;
19688c638d02SBarry Smith     ierr = MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr);
1969a0ff6018SBarry Smith   }
1970a0ff6018SBarry Smith 
1971a0ff6018SBarry Smith   ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1972a0ff6018SBarry Smith   ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1973a0ff6018SBarry Smith   *newmat = M;
1974fee21e36SBarry Smith 
1975fee21e36SBarry Smith   /* save submatrix used in processor for next request */
1976fee21e36SBarry Smith   if (call ==  MAT_INITIAL_MATRIX) {
1977fee21e36SBarry Smith     ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr);
1978fee21e36SBarry Smith     ierr = PetscObjectDereference((PetscObject)Mreuse);CHKERRQ(ierr);
1979fee21e36SBarry Smith   }
1980fee21e36SBarry Smith 
1981a0ff6018SBarry Smith   PetscFunctionReturn(0);
1982a0ff6018SBarry Smith }
1983*273d9f13SBarry Smith 
1984*273d9f13SBarry Smith #undef __FUNC__
1985*273d9f13SBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatMPIAIJSetPreallocation"
1986*273d9f13SBarry Smith /*@C
1987*273d9f13SBarry Smith    MatMPIAIJSetPreallocation - Creates a sparse parallel matrix in AIJ format
1988*273d9f13SBarry Smith    (the default parallel PETSc format).  For good matrix assembly performance
1989*273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameters
1990*273d9f13SBarry Smith    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
1991*273d9f13SBarry Smith    performance can be increased by more than a factor of 50.
1992*273d9f13SBarry Smith 
1993*273d9f13SBarry Smith    Collective on MPI_Comm
1994*273d9f13SBarry Smith 
1995*273d9f13SBarry Smith    Input Parameters:
1996*273d9f13SBarry Smith +  A - the matrix
1997*273d9f13SBarry Smith .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
1998*273d9f13SBarry Smith            (same value is used for all local rows)
1999*273d9f13SBarry Smith .  d_nnz - array containing the number of nonzeros in the various rows of the
2000*273d9f13SBarry Smith            DIAGONAL portion of the local submatrix (possibly different for each row)
2001*273d9f13SBarry Smith            or PETSC_NULL, if d_nz is used to specify the nonzero structure.
2002*273d9f13SBarry Smith            The size of this array is equal to the number of local rows, i.e 'm'.
2003*273d9f13SBarry Smith            You must leave room for the diagonal entry even if it is zero.
2004*273d9f13SBarry Smith .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
2005*273d9f13SBarry Smith            submatrix (same value is used for all local rows).
2006*273d9f13SBarry Smith -  o_nnz - array containing the number of nonzeros in the various rows of the
2007*273d9f13SBarry Smith            OFF-DIAGONAL portion of the local submatrix (possibly different for
2008*273d9f13SBarry Smith            each row) or PETSC_NULL, if o_nz is used to specify the nonzero
2009*273d9f13SBarry Smith            structure. The size of this array is equal to the number
2010*273d9f13SBarry Smith            of local rows, i.e 'm'.
2011*273d9f13SBarry Smith 
2012*273d9f13SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
2013*273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2014*273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2015*273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users manual for details.
2016*273d9f13SBarry Smith 
2017*273d9f13SBarry Smith    The user MUST specify either the local or global matrix dimensions
2018*273d9f13SBarry Smith    (possibly both).
2019*273d9f13SBarry Smith 
2020*273d9f13SBarry Smith    The parallel matrix is partitioned such that the first m0 rows belong to
2021*273d9f13SBarry Smith    process 0, the next m1 rows belong to process 1, the next m2 rows belong
2022*273d9f13SBarry Smith    to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.
2023*273d9f13SBarry Smith 
2024*273d9f13SBarry Smith    The DIAGONAL portion of the local submatrix of a processor can be defined
2025*273d9f13SBarry Smith    as the submatrix which is obtained by extraction the part corresponding
2026*273d9f13SBarry Smith    to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the
2027*273d9f13SBarry Smith    first row that belongs to the processor, and r2 is the last row belonging
2028*273d9f13SBarry Smith    to the this processor. This is a square mxm matrix. The remaining portion
2029*273d9f13SBarry Smith    of the local submatrix (mxN) constitute the OFF-DIAGONAL portion.
2030*273d9f13SBarry Smith 
2031*273d9f13SBarry Smith    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
2032*273d9f13SBarry Smith 
2033*273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible.
2034*273d9f13SBarry Smith    We search for consecutive rows with the same nonzero structure, thereby
2035*273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2036*273d9f13SBarry Smith 
2037*273d9f13SBarry Smith    Options Database Keys:
2038*273d9f13SBarry Smith +  -mat_aij_no_inode  - Do not use inodes
2039*273d9f13SBarry Smith .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2040*273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2041*273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2042*273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2043*273d9f13SBarry Smith 
2044*273d9f13SBarry Smith    Example usage:
2045*273d9f13SBarry Smith 
2046*273d9f13SBarry Smith    Consider the following 8x8 matrix with 34 non-zero values, that is
2047*273d9f13SBarry Smith    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
2048*273d9f13SBarry Smith    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
2049*273d9f13SBarry Smith    as follows:
2050*273d9f13SBarry Smith 
2051*273d9f13SBarry Smith .vb
2052*273d9f13SBarry Smith             1  2  0  |  0  3  0  |  0  4
2053*273d9f13SBarry Smith     Proc0   0  5  6  |  7  0  0  |  8  0
2054*273d9f13SBarry Smith             9  0 10  | 11  0  0  | 12  0
2055*273d9f13SBarry Smith     -------------------------------------
2056*273d9f13SBarry Smith            13  0 14  | 15 16 17  |  0  0
2057*273d9f13SBarry Smith     Proc1   0 18  0  | 19 20 21  |  0  0
2058*273d9f13SBarry Smith             0  0  0  | 22 23  0  | 24  0
2059*273d9f13SBarry Smith     -------------------------------------
2060*273d9f13SBarry Smith     Proc2  25 26 27  |  0  0 28  | 29  0
2061*273d9f13SBarry Smith            30  0  0  | 31 32 33  |  0 34
2062*273d9f13SBarry Smith .ve
2063*273d9f13SBarry Smith 
2064*273d9f13SBarry Smith    This can be represented as a collection of submatrices as:
2065*273d9f13SBarry Smith 
2066*273d9f13SBarry Smith .vb
2067*273d9f13SBarry Smith       A B C
2068*273d9f13SBarry Smith       D E F
2069*273d9f13SBarry Smith       G H I
2070*273d9f13SBarry Smith .ve
2071*273d9f13SBarry Smith 
2072*273d9f13SBarry Smith    Where the submatrices A,B,C are owned by proc0, D,E,F are
2073*273d9f13SBarry Smith    owned by proc1, G,H,I are owned by proc2.
2074*273d9f13SBarry Smith 
2075*273d9f13SBarry Smith    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2076*273d9f13SBarry Smith    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2077*273d9f13SBarry Smith    The 'M','N' parameters are 8,8, and have the same values on all procs.
2078*273d9f13SBarry Smith 
2079*273d9f13SBarry Smith    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
2080*273d9f13SBarry Smith    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
2081*273d9f13SBarry Smith    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
2082*273d9f13SBarry Smith    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
2083*273d9f13SBarry Smith    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
2084*273d9f13SBarry Smith    matrix, ans [DF] as another SeqAIJ matrix.
2085*273d9f13SBarry Smith 
2086*273d9f13SBarry Smith    When d_nz, o_nz parameters are specified, d_nz storage elements are
2087*273d9f13SBarry Smith    allocated for every row of the local diagonal submatrix, and o_nz
2088*273d9f13SBarry Smith    storage locations are allocated for every row of the OFF-DIAGONAL submat.
2089*273d9f13SBarry Smith    One way to choose d_nz and o_nz is to use the max nonzerors per local
2090*273d9f13SBarry Smith    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
2091*273d9f13SBarry Smith    In this case, the values of d_nz,o_nz are:
2092*273d9f13SBarry Smith .vb
2093*273d9f13SBarry Smith      proc0 : dnz = 2, o_nz = 2
2094*273d9f13SBarry Smith      proc1 : dnz = 3, o_nz = 2
2095*273d9f13SBarry Smith      proc2 : dnz = 1, o_nz = 4
2096*273d9f13SBarry Smith .ve
2097*273d9f13SBarry Smith    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
2098*273d9f13SBarry Smith    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
2099*273d9f13SBarry Smith    for proc3. i.e we are using 12+15+10=37 storage locations to store
2100*273d9f13SBarry Smith    34 values.
2101*273d9f13SBarry Smith 
2102*273d9f13SBarry Smith    When d_nnz, o_nnz parameters are specified, the storage is specified
2103*273d9f13SBarry Smith    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
2104*273d9f13SBarry Smith    In the above case the values for d_nnz,o_nnz are:
2105*273d9f13SBarry Smith .vb
2106*273d9f13SBarry Smith      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
2107*273d9f13SBarry Smith      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
2108*273d9f13SBarry Smith      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
2109*273d9f13SBarry Smith .ve
2110*273d9f13SBarry Smith    Here the space allocated is sum of all the above values i.e 34, and
2111*273d9f13SBarry Smith    hence pre-allocation is perfect.
2112*273d9f13SBarry Smith 
2113*273d9f13SBarry Smith    Level: intermediate
2114*273d9f13SBarry Smith 
2115*273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
2116*273d9f13SBarry Smith 
2117*273d9f13SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues()
2118*273d9f13SBarry Smith @*/
2119*273d9f13SBarry Smith int MatMPIAIJSetPreallocation(Mat B,int d_nz,int *d_nnz,int o_nz,int *o_nnz)
2120*273d9f13SBarry Smith {
2121*273d9f13SBarry Smith   Mat_MPIAIJ   *b;
2122*273d9f13SBarry Smith   int          ierr,i;
2123*273d9f13SBarry Smith   PetscTruth   flg2;
2124*273d9f13SBarry Smith 
2125*273d9f13SBarry Smith   PetscFunctionBegin;
2126*273d9f13SBarry Smith   ierr = PetscTypeCompare((PetscObject)B,MATMPIAIJ,&flg2);CHKERRQ(ierr);
2127*273d9f13SBarry Smith   if (!flg2) PetscFunctionReturn(0);
2128*273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
2129*273d9f13SBarry Smith   if (d_nz < -2) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"d_nz cannot be less than -2: value %d",d_nz);
2130*273d9f13SBarry Smith   if (o_nz < -2) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"o_nz cannot be less than -2: value %d",o_nz);
2131*273d9f13SBarry Smith   if (d_nnz) {
2132*273d9f13SBarry Smith     for (i=0; i<B->m; i++) {
2133*273d9f13SBarry 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]);
2134*273d9f13SBarry Smith     }
2135*273d9f13SBarry Smith   }
2136*273d9f13SBarry Smith   if (o_nnz) {
2137*273d9f13SBarry Smith     for (i=0; i<B->m; i++) {
2138*273d9f13SBarry 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]);
2139*273d9f13SBarry Smith     }
2140*273d9f13SBarry Smith   }
2141*273d9f13SBarry Smith   b = (Mat_MPIAIJ*)B->data;
2142*273d9f13SBarry Smith 
2143*273d9f13SBarry Smith   if (d_nz == PETSC_DEFAULT) d_nz = 5;
2144*273d9f13SBarry Smith   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,B->m,B->n,d_nz,d_nnz,&b->A);CHKERRQ(ierr);
2145*273d9f13SBarry Smith   PLogObjectParent(B,b->A);
2146*273d9f13SBarry Smith   if (o_nz == PETSC_DEFAULT) o_nz = 0;
2147*273d9f13SBarry Smith   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,B->m,B->N,o_nz,o_nnz,&b->B);CHKERRQ(ierr);
2148*273d9f13SBarry Smith   PLogObjectParent(B,b->B);
2149*273d9f13SBarry Smith 
2150*273d9f13SBarry Smith   PetscFunctionReturn(0);
2151*273d9f13SBarry Smith }
2152*273d9f13SBarry Smith 
2153*273d9f13SBarry Smith #undef __FUNC__
2154*273d9f13SBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatCreateMPIAIJ"
2155*273d9f13SBarry Smith /*@C
2156*273d9f13SBarry Smith    MatCreateMPIAIJ - Creates a sparse parallel matrix in AIJ format
2157*273d9f13SBarry Smith    (the default parallel PETSc format).  For good matrix assembly performance
2158*273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameters
2159*273d9f13SBarry Smith    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
2160*273d9f13SBarry Smith    performance can be increased by more than a factor of 50.
2161*273d9f13SBarry Smith 
2162*273d9f13SBarry Smith    Collective on MPI_Comm
2163*273d9f13SBarry Smith 
2164*273d9f13SBarry Smith    Input Parameters:
2165*273d9f13SBarry Smith +  comm - MPI communicator
2166*273d9f13SBarry Smith .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
2167*273d9f13SBarry Smith            This value should be the same as the local size used in creating the
2168*273d9f13SBarry Smith            y vector for the matrix-vector product y = Ax.
2169*273d9f13SBarry Smith .  n - This value should be the same as the local size used in creating the
2170*273d9f13SBarry Smith        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
2171*273d9f13SBarry Smith        calculated if N is given) For square matrices n is almost always m.
2172*273d9f13SBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
2173*273d9f13SBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
2174*273d9f13SBarry Smith .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
2175*273d9f13SBarry Smith            (same value is used for all local rows)
2176*273d9f13SBarry Smith .  d_nnz - array containing the number of nonzeros in the various rows of the
2177*273d9f13SBarry Smith            DIAGONAL portion of the local submatrix (possibly different for each row)
2178*273d9f13SBarry Smith            or PETSC_NULL, if d_nz is used to specify the nonzero structure.
2179*273d9f13SBarry Smith            The size of this array is equal to the number of local rows, i.e 'm'.
2180*273d9f13SBarry Smith            You must leave room for the diagonal entry even if it is zero.
2181*273d9f13SBarry Smith .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
2182*273d9f13SBarry Smith            submatrix (same value is used for all local rows).
2183*273d9f13SBarry Smith -  o_nnz - array containing the number of nonzeros in the various rows of the
2184*273d9f13SBarry Smith            OFF-DIAGONAL portion of the local submatrix (possibly different for
2185*273d9f13SBarry Smith            each row) or PETSC_NULL, if o_nz is used to specify the nonzero
2186*273d9f13SBarry Smith            structure. The size of this array is equal to the number
2187*273d9f13SBarry Smith            of local rows, i.e 'm'.
2188*273d9f13SBarry Smith 
2189*273d9f13SBarry Smith    Output Parameter:
2190*273d9f13SBarry Smith .  A - the matrix
2191*273d9f13SBarry Smith 
2192*273d9f13SBarry Smith    Notes:
2193*273d9f13SBarry Smith    m,n,M,N parameters specify the size of the matrix, and its partitioning across
2194*273d9f13SBarry Smith    processors, while d_nz,d_nnz,o_nz,o_nnz parameters specify the approximate
2195*273d9f13SBarry Smith    storage requirements for this matrix.
2196*273d9f13SBarry Smith 
2197*273d9f13SBarry Smith    If PETSC_DECIDE or  PETSC_DETERMINE is used for a particular argument on one
2198*273d9f13SBarry Smith    processor than it must be used on all processors that share the object for
2199*273d9f13SBarry Smith    that argument.
2200*273d9f13SBarry Smith 
2201*273d9f13SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
2202*273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2203*273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2204*273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users manual for details.
2205*273d9f13SBarry Smith 
2206*273d9f13SBarry Smith    The user MUST specify either the local or global matrix dimensions
2207*273d9f13SBarry Smith    (possibly both).
2208*273d9f13SBarry Smith 
2209*273d9f13SBarry Smith    The parallel matrix is partitioned such that the first m0 rows belong to
2210*273d9f13SBarry Smith    process 0, the next m1 rows belong to process 1, the next m2 rows belong
2211*273d9f13SBarry Smith    to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.
2212*273d9f13SBarry Smith 
2213*273d9f13SBarry Smith    The DIAGONAL portion of the local submatrix of a processor can be defined
2214*273d9f13SBarry Smith    as the submatrix which is obtained by extraction the part corresponding
2215*273d9f13SBarry Smith    to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the
2216*273d9f13SBarry Smith    first row that belongs to the processor, and r2 is the last row belonging
2217*273d9f13SBarry Smith    to the this processor. This is a square mxm matrix. The remaining portion
2218*273d9f13SBarry Smith    of the local submatrix (mxN) constitute the OFF-DIAGONAL portion.
2219*273d9f13SBarry Smith 
2220*273d9f13SBarry Smith    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
2221*273d9f13SBarry Smith 
2222*273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible.
2223*273d9f13SBarry Smith    We search for consecutive rows with the same nonzero structure, thereby
2224*273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2225*273d9f13SBarry Smith 
2226*273d9f13SBarry Smith    Options Database Keys:
2227*273d9f13SBarry Smith +  -mat_aij_no_inode  - Do not use inodes
2228*273d9f13SBarry Smith .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2229*273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2230*273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2231*273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2232*273d9f13SBarry Smith 
2233*273d9f13SBarry Smith 
2234*273d9f13SBarry Smith    Example usage:
2235*273d9f13SBarry Smith 
2236*273d9f13SBarry Smith    Consider the following 8x8 matrix with 34 non-zero values, that is
2237*273d9f13SBarry Smith    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
2238*273d9f13SBarry Smith    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
2239*273d9f13SBarry Smith    as follows:
2240*273d9f13SBarry Smith 
2241*273d9f13SBarry Smith .vb
2242*273d9f13SBarry Smith             1  2  0  |  0  3  0  |  0  4
2243*273d9f13SBarry Smith     Proc0   0  5  6  |  7  0  0  |  8  0
2244*273d9f13SBarry Smith             9  0 10  | 11  0  0  | 12  0
2245*273d9f13SBarry Smith     -------------------------------------
2246*273d9f13SBarry Smith            13  0 14  | 15 16 17  |  0  0
2247*273d9f13SBarry Smith     Proc1   0 18  0  | 19 20 21  |  0  0
2248*273d9f13SBarry Smith             0  0  0  | 22 23  0  | 24  0
2249*273d9f13SBarry Smith     -------------------------------------
2250*273d9f13SBarry Smith     Proc2  25 26 27  |  0  0 28  | 29  0
2251*273d9f13SBarry Smith            30  0  0  | 31 32 33  |  0 34
2252*273d9f13SBarry Smith .ve
2253*273d9f13SBarry Smith 
2254*273d9f13SBarry Smith    This can be represented as a collection of submatrices as:
2255*273d9f13SBarry Smith 
2256*273d9f13SBarry Smith .vb
2257*273d9f13SBarry Smith       A B C
2258*273d9f13SBarry Smith       D E F
2259*273d9f13SBarry Smith       G H I
2260*273d9f13SBarry Smith .ve
2261*273d9f13SBarry Smith 
2262*273d9f13SBarry Smith    Where the submatrices A,B,C are owned by proc0, D,E,F are
2263*273d9f13SBarry Smith    owned by proc1, G,H,I are owned by proc2.
2264*273d9f13SBarry Smith 
2265*273d9f13SBarry Smith    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2266*273d9f13SBarry Smith    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2267*273d9f13SBarry Smith    The 'M','N' parameters are 8,8, and have the same values on all procs.
2268*273d9f13SBarry Smith 
2269*273d9f13SBarry Smith    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
2270*273d9f13SBarry Smith    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
2271*273d9f13SBarry Smith    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
2272*273d9f13SBarry Smith    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
2273*273d9f13SBarry Smith    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
2274*273d9f13SBarry Smith    matrix, ans [DF] as another SeqAIJ matrix.
2275*273d9f13SBarry Smith 
2276*273d9f13SBarry Smith    When d_nz, o_nz parameters are specified, d_nz storage elements are
2277*273d9f13SBarry Smith    allocated for every row of the local diagonal submatrix, and o_nz
2278*273d9f13SBarry Smith    storage locations are allocated for every row of the OFF-DIAGONAL submat.
2279*273d9f13SBarry Smith    One way to choose d_nz and o_nz is to use the max nonzerors per local
2280*273d9f13SBarry Smith    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
2281*273d9f13SBarry Smith    In this case, the values of d_nz,o_nz are:
2282*273d9f13SBarry Smith .vb
2283*273d9f13SBarry Smith      proc0 : dnz = 2, o_nz = 2
2284*273d9f13SBarry Smith      proc1 : dnz = 3, o_nz = 2
2285*273d9f13SBarry Smith      proc2 : dnz = 1, o_nz = 4
2286*273d9f13SBarry Smith .ve
2287*273d9f13SBarry Smith    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
2288*273d9f13SBarry Smith    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
2289*273d9f13SBarry Smith    for proc3. i.e we are using 12+15+10=37 storage locations to store
2290*273d9f13SBarry Smith    34 values.
2291*273d9f13SBarry Smith 
2292*273d9f13SBarry Smith    When d_nnz, o_nnz parameters are specified, the storage is specified
2293*273d9f13SBarry Smith    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
2294*273d9f13SBarry Smith    In the above case the values for d_nnz,o_nnz are:
2295*273d9f13SBarry Smith .vb
2296*273d9f13SBarry Smith      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
2297*273d9f13SBarry Smith      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
2298*273d9f13SBarry Smith      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
2299*273d9f13SBarry Smith .ve
2300*273d9f13SBarry Smith    Here the space allocated is sum of all the above values i.e 34, and
2301*273d9f13SBarry Smith    hence pre-allocation is perfect.
2302*273d9f13SBarry Smith 
2303*273d9f13SBarry Smith    Level: intermediate
2304*273d9f13SBarry Smith 
2305*273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
2306*273d9f13SBarry Smith 
2307*273d9f13SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues()
2308*273d9f13SBarry Smith @*/
2309*273d9f13SBarry 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)
2310*273d9f13SBarry Smith {
2311*273d9f13SBarry Smith   int ierr,size;
2312*273d9f13SBarry Smith 
2313*273d9f13SBarry Smith   PetscFunctionBegin;
2314*273d9f13SBarry Smith   ierr = MatCreate(comm,m,n,M,N,A);CHKERRQ(ierr);
2315*273d9f13SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
2316*273d9f13SBarry Smith   if (size > 1) {
2317*273d9f13SBarry Smith     ierr = MatSetType(*A,MATMPIAIJ);CHKERRQ(ierr);
2318*273d9f13SBarry Smith     ierr = MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr);
2319*273d9f13SBarry Smith   } else {
2320*273d9f13SBarry Smith     ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2321*273d9f13SBarry Smith     ierr = MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);CHKERRQ(ierr);
2322*273d9f13SBarry Smith   }
2323*273d9f13SBarry Smith   PetscFunctionReturn(0);
2324*273d9f13SBarry Smith }
2325