xref: /petsc/src/mat/impls/aij/mpi/mpiaij.c (revision 471340fb25752a158dc55af0084a76f82aa9f15e)
173f4d377SMatthew Knepley /*$Id: mpiaij.c,v 1.344 2001/08/10 03:30:48 bsmith Exp $*/
28a729477SBarry Smith 
370f55243SBarry Smith #include "src/mat/impls/aij/mpi/mpiaij.h"
4f5eb4b81SSatish Balay #include "src/vec/vecimpl.h"
5d9942c19SSatish Balay #include "src/inline/spops.h"
68a729477SBarry Smith 
7ca44d042SBarry Smith EXTERN int MatSetUpMultiply_MPIAIJ(Mat);
8ca44d042SBarry Smith EXTERN int DisAssemble_MPIAIJ(Mat);
987828ca2SBarry Smith EXTERN int MatSetValues_SeqAIJ(Mat,int,int*,int,int*,PetscScalar*,InsertMode);
1087828ca2SBarry Smith EXTERN int MatGetRow_SeqAIJ(Mat,int,int*,int**,PetscScalar**);
1187828ca2SBarry Smith EXTERN int MatRestoreRow_SeqAIJ(Mat,int,int*,int**,PetscScalar**);
12ca44d042SBarry Smith EXTERN int MatPrintHelp_SeqAIJ(Mat);
139b9367d5SHong Zhang EXTERN int MatUseSuperLU_DIST_MPIAIJ(Mat);
144032b0d1SHong Zhang EXTERN int MatUseSpooles_MPIAIJ(Mat);
15bc5ccf88SSatish Balay 
160f5bd95cSBarry Smith /*
170f5bd95cSBarry Smith   Local utility routine that creates a mapping from the global column
189e25ed09SBarry Smith number to the local number in the off-diagonal part of the local
190f5bd95cSBarry Smith storage of the matrix.  When PETSC_USE_CTABLE is used this is scalable at
200f5bd95cSBarry Smith a slightly higher hash table cost; without it it is not scalable (each processor
210f5bd95cSBarry Smith has an order N integer array but is fast to acess.
229e25ed09SBarry Smith */
234a2ae208SSatish Balay #undef __FUNCT__
244a2ae208SSatish Balay #define __FUNCT__ "CreateColmap_MPIAIJ_Private"
250a198c4cSBarry Smith int CreateColmap_MPIAIJ_Private(Mat mat)
269e25ed09SBarry Smith {
2744a69424SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
28273d9f13SBarry Smith   int        n = aij->B->n,i,ierr;
29dbb450caSBarry Smith 
303a40ed3dSBarry Smith   PetscFunctionBegin;
31aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
32273d9f13SBarry Smith   ierr = PetscTableCreate(n,&aij->colmap);CHKERRQ(ierr);
33b1fc9764SSatish Balay   for (i=0; i<n; i++){
340f5bd95cSBarry Smith     ierr = PetscTableAdd(aij->colmap,aij->garray[i]+1,i+1);CHKERRQ(ierr);
35b1fc9764SSatish Balay   }
36b1fc9764SSatish Balay #else
37b0a32e0cSBarry Smith   ierr = PetscMalloc((mat->N+1)*sizeof(int),&aij->colmap);CHKERRQ(ierr);
38b0a32e0cSBarry Smith   PetscLogObjectMemory(mat,mat->N*sizeof(int));
39273d9f13SBarry Smith   ierr = PetscMemzero(aij->colmap,mat->N*sizeof(int));CHKERRQ(ierr);
40905e6a2fSBarry Smith   for (i=0; i<n; i++) aij->colmap[aij->garray[i]] = i+1;
41b1fc9764SSatish Balay #endif
423a40ed3dSBarry Smith   PetscFunctionReturn(0);
439e25ed09SBarry Smith }
449e25ed09SBarry Smith 
450520107fSSatish Balay #define CHUNKSIZE   15
4630770e4dSSatish Balay #define MatSetValues_SeqAIJ_A_Private(row,col,value,addv) \
470520107fSSatish Balay { \
480520107fSSatish Balay  \
490520107fSSatish Balay     rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift; \
5030770e4dSSatish Balay     rmax = aimax[row]; nrow = ailen[row];  \
51f5e9677aSSatish Balay     col1 = col - shift; \
52f5e9677aSSatish Balay      \
53ba4e3ef2SSatish Balay     low = 0; high = nrow; \
54ba4e3ef2SSatish Balay     while (high-low > 5) { \
55ba4e3ef2SSatish Balay       t = (low+high)/2; \
56ba4e3ef2SSatish Balay       if (rp[t] > col) high = t; \
57ba4e3ef2SSatish Balay       else             low  = t; \
58ba4e3ef2SSatish Balay     } \
592335bdd2SSatish Balay       for (_i=low; _i<high; _i++) { \
60f5e9677aSSatish Balay         if (rp[_i] > col1) break; \
61f5e9677aSSatish Balay         if (rp[_i] == col1) { \
620520107fSSatish Balay           if (addv == ADD_VALUES) ap[_i] += value;   \
630520107fSSatish Balay           else                  ap[_i] = value; \
6430770e4dSSatish Balay           goto a_noinsert; \
650520107fSSatish Balay         } \
660520107fSSatish Balay       }  \
6789280ab3SLois Curfman McInnes       if (nonew == 1) goto a_noinsert; \
6829bbc08cSBarry Smith       else if (nonew == -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero into matrix"); \
690520107fSSatish Balay       if (nrow >= rmax) { \
700520107fSSatish Balay         /* there is no extra room in row, therefore enlarge */ \
71273d9f13SBarry Smith         int    new_nz = ai[am] + CHUNKSIZE,len,*new_i,*new_j; \
7287828ca2SBarry Smith         PetscScalar *new_a; \
730520107fSSatish Balay  \
7429bbc08cSBarry Smith         if (nonew == -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix"); \
7589280ab3SLois Curfman McInnes  \
760520107fSSatish Balay         /* malloc new storage space */ \
7787828ca2SBarry Smith         len     = new_nz*(sizeof(int)+sizeof(PetscScalar))+(am+1)*sizeof(int); \
78b0a32e0cSBarry Smith         ierr    = PetscMalloc(len,&new_a);CHKERRQ(ierr); \
790520107fSSatish Balay         new_j   = (int*)(new_a + new_nz); \
800520107fSSatish Balay         new_i   = new_j + new_nz; \
810520107fSSatish Balay  \
820520107fSSatish Balay         /* copy over old data into new slots */ \
830520107fSSatish Balay         for (ii=0; ii<row+1; ii++) {new_i[ii] = ai[ii];} \
84273d9f13SBarry Smith         for (ii=row+1; ii<am+1; ii++) {new_i[ii] = ai[ii]+CHUNKSIZE;} \
85549d3d68SSatish Balay         ierr = PetscMemcpy(new_j,aj,(ai[row]+nrow+shift)*sizeof(int));CHKERRQ(ierr); \
860520107fSSatish Balay         len = (new_nz - CHUNKSIZE - ai[row] - nrow - shift); \
87549d3d68SSatish Balay         ierr = PetscMemcpy(new_j+ai[row]+shift+nrow+CHUNKSIZE,aj+ai[row]+shift+nrow, \
88549d3d68SSatish Balay                                                            len*sizeof(int));CHKERRQ(ierr); \
8987828ca2SBarry Smith         ierr = PetscMemcpy(new_a,aa,(ai[row]+nrow+shift)*sizeof(PetscScalar));CHKERRQ(ierr); \
90549d3d68SSatish Balay         ierr = PetscMemcpy(new_a+ai[row]+shift+nrow+CHUNKSIZE,aa+ai[row]+shift+nrow, \
9187828ca2SBarry Smith                                                            len*sizeof(PetscScalar));CHKERRQ(ierr);  \
920520107fSSatish Balay         /* free up old matrix storage */ \
93f5e9677aSSatish Balay  \
94606d414cSSatish Balay         ierr = PetscFree(a->a);CHKERRQ(ierr);  \
95606d414cSSatish Balay         if (!a->singlemalloc) { \
96606d414cSSatish Balay            ierr = PetscFree(a->i);CHKERRQ(ierr); \
97606d414cSSatish Balay            ierr = PetscFree(a->j);CHKERRQ(ierr); \
98606d414cSSatish Balay         } \
990520107fSSatish Balay         aa = a->a = new_a; ai = a->i = new_i; aj = a->j = new_j;  \
1007c922b88SBarry Smith         a->singlemalloc = PETSC_TRUE; \
1010520107fSSatish Balay  \
1020520107fSSatish Balay         rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift; \
10330770e4dSSatish Balay         rmax = aimax[row] = aimax[row] + CHUNKSIZE; \
10487828ca2SBarry Smith         PetscLogObjectMemory(A,CHUNKSIZE*(sizeof(int) + sizeof(PetscScalar))); \
1050520107fSSatish Balay         a->maxnz += CHUNKSIZE; \
1060520107fSSatish Balay         a->reallocs++; \
1070520107fSSatish Balay       } \
1080520107fSSatish Balay       N = nrow++ - 1; a->nz++; \
1090520107fSSatish Balay       /* shift up all the later entries in this row */ \
1100520107fSSatish Balay       for (ii=N; ii>=_i; ii--) { \
1110520107fSSatish Balay         rp[ii+1] = rp[ii]; \
1120520107fSSatish Balay         ap[ii+1] = ap[ii]; \
1130520107fSSatish Balay       } \
114f5e9677aSSatish Balay       rp[_i] = col1;  \
1150520107fSSatish Balay       ap[_i] = value;  \
11630770e4dSSatish Balay       a_noinsert: ; \
1170520107fSSatish Balay       ailen[row] = nrow; \
1180520107fSSatish Balay }
1190a198c4cSBarry Smith 
12030770e4dSSatish Balay #define MatSetValues_SeqAIJ_B_Private(row,col,value,addv) \
12130770e4dSSatish Balay { \
12230770e4dSSatish Balay  \
12330770e4dSSatish Balay     rp   = bj + bi[row] + shift; ap = ba + bi[row] + shift; \
12430770e4dSSatish Balay     rmax = bimax[row]; nrow = bilen[row];  \
12530770e4dSSatish Balay     col1 = col - shift; \
12630770e4dSSatish Balay      \
127ba4e3ef2SSatish Balay     low = 0; high = nrow; \
128ba4e3ef2SSatish Balay     while (high-low > 5) { \
129ba4e3ef2SSatish Balay       t = (low+high)/2; \
130ba4e3ef2SSatish Balay       if (rp[t] > col) high = t; \
131ba4e3ef2SSatish Balay       else             low  = t; \
132ba4e3ef2SSatish Balay     } \
1332335bdd2SSatish Balay        for (_i=low; _i<high; _i++) { \
13430770e4dSSatish Balay         if (rp[_i] > col1) break; \
13530770e4dSSatish Balay         if (rp[_i] == col1) { \
13630770e4dSSatish Balay           if (addv == ADD_VALUES) ap[_i] += value;   \
13730770e4dSSatish Balay           else                  ap[_i] = value; \
13830770e4dSSatish Balay           goto b_noinsert; \
13930770e4dSSatish Balay         } \
14030770e4dSSatish Balay       }  \
14189280ab3SLois Curfman McInnes       if (nonew == 1) goto b_noinsert; \
14229bbc08cSBarry Smith       else if (nonew == -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero into matrix"); \
14330770e4dSSatish Balay       if (nrow >= rmax) { \
14430770e4dSSatish Balay         /* there is no extra room in row, therefore enlarge */ \
145273d9f13SBarry Smith         int    new_nz = bi[bm] + CHUNKSIZE,len,*new_i,*new_j; \
14687828ca2SBarry Smith         PetscScalar *new_a; \
14730770e4dSSatish Balay  \
14829bbc08cSBarry Smith         if (nonew == -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix"); \
14989280ab3SLois Curfman McInnes  \
15030770e4dSSatish Balay         /* malloc new storage space */ \
15187828ca2SBarry Smith         len     = new_nz*(sizeof(int)+sizeof(PetscScalar))+(bm+1)*sizeof(int); \
152b0a32e0cSBarry Smith         ierr    = PetscMalloc(len,&new_a);CHKERRQ(ierr); \
15330770e4dSSatish Balay         new_j   = (int*)(new_a + new_nz); \
15430770e4dSSatish Balay         new_i   = new_j + new_nz; \
15530770e4dSSatish Balay  \
15630770e4dSSatish Balay         /* copy over old data into new slots */ \
15730770e4dSSatish Balay         for (ii=0; ii<row+1; ii++) {new_i[ii] = bi[ii];} \
158273d9f13SBarry Smith         for (ii=row+1; ii<bm+1; ii++) {new_i[ii] = bi[ii]+CHUNKSIZE;} \
159549d3d68SSatish Balay         ierr = PetscMemcpy(new_j,bj,(bi[row]+nrow+shift)*sizeof(int));CHKERRQ(ierr); \
16030770e4dSSatish Balay         len = (new_nz - CHUNKSIZE - bi[row] - nrow - shift); \
161549d3d68SSatish Balay         ierr = PetscMemcpy(new_j+bi[row]+shift+nrow+CHUNKSIZE,bj+bi[row]+shift+nrow, \
162549d3d68SSatish Balay                                                            len*sizeof(int));CHKERRQ(ierr); \
16387828ca2SBarry Smith         ierr = PetscMemcpy(new_a,ba,(bi[row]+nrow+shift)*sizeof(PetscScalar));CHKERRQ(ierr); \
164549d3d68SSatish Balay         ierr = PetscMemcpy(new_a+bi[row]+shift+nrow+CHUNKSIZE,ba+bi[row]+shift+nrow, \
16587828ca2SBarry Smith                                                            len*sizeof(PetscScalar));CHKERRQ(ierr);  \
16630770e4dSSatish Balay         /* free up old matrix storage */ \
16730770e4dSSatish Balay  \
168606d414cSSatish Balay         ierr = PetscFree(b->a);CHKERRQ(ierr);  \
169606d414cSSatish Balay         if (!b->singlemalloc) { \
170606d414cSSatish Balay           ierr = PetscFree(b->i);CHKERRQ(ierr); \
171606d414cSSatish Balay           ierr = PetscFree(b->j);CHKERRQ(ierr); \
172606d414cSSatish Balay         } \
17374c639caSSatish Balay         ba = b->a = new_a; bi = b->i = new_i; bj = b->j = new_j;  \
1747c922b88SBarry Smith         b->singlemalloc = PETSC_TRUE; \
17530770e4dSSatish Balay  \
17630770e4dSSatish Balay         rp   = bj + bi[row] + shift; ap = ba + bi[row] + shift; \
17730770e4dSSatish Balay         rmax = bimax[row] = bimax[row] + CHUNKSIZE; \
17887828ca2SBarry Smith         PetscLogObjectMemory(B,CHUNKSIZE*(sizeof(int) + sizeof(PetscScalar))); \
17974c639caSSatish Balay         b->maxnz += CHUNKSIZE; \
18074c639caSSatish Balay         b->reallocs++; \
18130770e4dSSatish Balay       } \
18274c639caSSatish Balay       N = nrow++ - 1; b->nz++; \
18330770e4dSSatish Balay       /* shift up all the later entries in this row */ \
18430770e4dSSatish Balay       for (ii=N; ii>=_i; ii--) { \
18530770e4dSSatish Balay         rp[ii+1] = rp[ii]; \
18630770e4dSSatish Balay         ap[ii+1] = ap[ii]; \
18730770e4dSSatish Balay       } \
18830770e4dSSatish Balay       rp[_i] = col1;  \
18930770e4dSSatish Balay       ap[_i] = value;  \
19030770e4dSSatish Balay       b_noinsert: ; \
19130770e4dSSatish Balay       bilen[row] = nrow; \
19230770e4dSSatish Balay }
19330770e4dSSatish Balay 
1944a2ae208SSatish Balay #undef __FUNCT__
1954a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_MPIAIJ"
19687828ca2SBarry Smith int MatSetValues_MPIAIJ(Mat mat,int m,int *im,int n,int *in,PetscScalar *v,InsertMode addv)
1978a729477SBarry Smith {
19844a69424SLois Curfman McInnes   Mat_MPIAIJ   *aij = (Mat_MPIAIJ*)mat->data;
19987828ca2SBarry Smith   PetscScalar  value;
2001eb62cbbSBarry Smith   int          ierr,i,j,rstart = aij->rstart,rend = aij->rend;
2011eb62cbbSBarry Smith   int          cstart = aij->cstart,cend = aij->cend,row,col;
202273d9f13SBarry Smith   PetscTruth   roworiented = aij->roworiented;
2038a729477SBarry Smith 
2040520107fSSatish Balay   /* Some Variables required in the macro */
2054ee7247eSSatish Balay   Mat          A = aij->A;
2064ee7247eSSatish Balay   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
20730770e4dSSatish Balay   int          *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j;
20887828ca2SBarry Smith   PetscScalar  *aa = a->a;
209329f5518SBarry Smith   PetscTruth   ignorezeroentries = (((a->ignorezeroentries)&&(addv==ADD_VALUES))?PETSC_TRUE:PETSC_FALSE);
21030770e4dSSatish Balay   Mat          B = aij->B;
21130770e4dSSatish Balay   Mat_SeqAIJ   *b = (Mat_SeqAIJ*)B->data;
212273d9f13SBarry Smith   int          *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->m,am = aij->A->m;
21387828ca2SBarry Smith   PetscScalar  *ba = b->a;
21430770e4dSSatish Balay 
215ba4e3ef2SSatish Balay   int          *rp,ii,nrow,_i,rmax,N,col1,low,high,t;
21630770e4dSSatish Balay   int          nonew = a->nonew,shift = a->indexshift;
21787828ca2SBarry Smith   PetscScalar  *ap;
2184ee7247eSSatish Balay 
2193a40ed3dSBarry Smith   PetscFunctionBegin;
2208a729477SBarry Smith   for (i=0; i<m; i++) {
2215ef9f2a5SBarry Smith     if (im[i] < 0) continue;
222aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g)
223273d9f13SBarry Smith     if (im[i] >= mat->M) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
2240a198c4cSBarry Smith #endif
2254b0e389bSBarry Smith     if (im[i] >= rstart && im[i] < rend) {
2264b0e389bSBarry Smith       row = im[i] - rstart;
2271eb62cbbSBarry Smith       for (j=0; j<n; j++) {
2284b0e389bSBarry Smith         if (in[j] >= cstart && in[j] < cend){
2294b0e389bSBarry Smith           col = in[j] - cstart;
2304b0e389bSBarry Smith           if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
231329f5518SBarry Smith           if (ignorezeroentries && value == 0.0) continue;
23230770e4dSSatish Balay           MatSetValues_SeqAIJ_A_Private(row,col,value,addv);
2330520107fSSatish Balay           /* ierr = MatSetValues_SeqAIJ(aij->A,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */
234273d9f13SBarry Smith         } else if (in[j] < 0) continue;
235aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g)
236273d9f13SBarry Smith         else if (in[j] >= mat->N) {SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Column too large");}
2370a198c4cSBarry Smith #endif
2381eb62cbbSBarry Smith         else {
239227d817aSBarry Smith           if (mat->was_assembled) {
240905e6a2fSBarry Smith             if (!aij->colmap) {
241905e6a2fSBarry Smith               ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
242905e6a2fSBarry Smith             }
243aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
2440f5bd95cSBarry Smith             ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr);
245fa46199cSSatish Balay 	    col--;
246b1fc9764SSatish Balay #else
247905e6a2fSBarry Smith             col = aij->colmap[in[j]] - 1;
248b1fc9764SSatish Balay #endif
249ec8511deSBarry Smith             if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
2502493cbb0SBarry Smith               ierr = DisAssemble_MPIAIJ(mat);CHKERRQ(ierr);
2514b0e389bSBarry Smith               col =  in[j];
2529bf004c3SSatish Balay               /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
253f9508a3cSSatish Balay               B = aij->B;
254f9508a3cSSatish Balay               b = (Mat_SeqAIJ*)B->data;
255f9508a3cSSatish Balay               bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j;
256f9508a3cSSatish Balay               ba = b->a;
257d6dfbf8fSBarry Smith             }
258c48de900SBarry Smith           } else col = in[j];
2594b0e389bSBarry Smith           if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
260329f5518SBarry Smith           if (ignorezeroentries && value == 0.0) continue;
26130770e4dSSatish Balay           MatSetValues_SeqAIJ_B_Private(row,col,value,addv);
26230770e4dSSatish Balay           /* ierr = MatSetValues_SeqAIJ(aij->B,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */
2631eb62cbbSBarry Smith         }
2641eb62cbbSBarry Smith       }
2655ef9f2a5SBarry Smith     } else {
26690f02eecSBarry Smith       if (!aij->donotstash) {
267d36fbae8SSatish Balay         if (roworiented) {
268329f5518SBarry Smith           if (ignorezeroentries && v[i*n] == 0.0) continue;
2698798bf22SSatish Balay           ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n);CHKERRQ(ierr);
270d36fbae8SSatish Balay         } else {
271329f5518SBarry Smith           if (ignorezeroentries && v[i] == 0.0) continue;
2728798bf22SSatish Balay           ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m);CHKERRQ(ierr);
2734b0e389bSBarry Smith         }
2741eb62cbbSBarry Smith       }
2758a729477SBarry Smith     }
27690f02eecSBarry Smith   }
2773a40ed3dSBarry Smith   PetscFunctionReturn(0);
2788a729477SBarry Smith }
2798a729477SBarry Smith 
2804a2ae208SSatish Balay #undef __FUNCT__
2814a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_MPIAIJ"
28287828ca2SBarry Smith int MatGetValues_MPIAIJ(Mat mat,int m,int *idxm,int n,int *idxn,PetscScalar *v)
283b49de8d1SLois Curfman McInnes {
284b49de8d1SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
285b49de8d1SLois Curfman McInnes   int        ierr,i,j,rstart = aij->rstart,rend = aij->rend;
286b49de8d1SLois Curfman McInnes   int        cstart = aij->cstart,cend = aij->cend,row,col;
287b49de8d1SLois Curfman McInnes 
2883a40ed3dSBarry Smith   PetscFunctionBegin;
289b49de8d1SLois Curfman McInnes   for (i=0; i<m; i++) {
29029bbc08cSBarry Smith     if (idxm[i] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Negative row");
291273d9f13SBarry Smith     if (idxm[i] >= mat->M) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
292b49de8d1SLois Curfman McInnes     if (idxm[i] >= rstart && idxm[i] < rend) {
293b49de8d1SLois Curfman McInnes       row = idxm[i] - rstart;
294b49de8d1SLois Curfman McInnes       for (j=0; j<n; j++) {
29529bbc08cSBarry Smith         if (idxn[j] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Negative column");
296273d9f13SBarry Smith         if (idxn[j] >= mat->N) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
297b49de8d1SLois Curfman McInnes         if (idxn[j] >= cstart && idxn[j] < cend){
298b49de8d1SLois Curfman McInnes           col = idxn[j] - cstart;
299b49de8d1SLois Curfman McInnes           ierr = MatGetValues(aij->A,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
300fa852ad4SSatish Balay         } else {
301905e6a2fSBarry Smith           if (!aij->colmap) {
302905e6a2fSBarry Smith             ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
303905e6a2fSBarry Smith           }
304aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
3050f5bd95cSBarry Smith           ierr = PetscTableFind(aij->colmap,idxn[j]+1,&col);CHKERRQ(ierr);
306fa46199cSSatish Balay           col --;
307b1fc9764SSatish Balay #else
308905e6a2fSBarry Smith           col = aij->colmap[idxn[j]] - 1;
309b1fc9764SSatish Balay #endif
310e60e1c95SSatish Balay           if ((col < 0) || (aij->garray[col] != idxn[j])) *(v+i*n+j) = 0.0;
311d9d09a02SSatish Balay           else {
312b49de8d1SLois Curfman McInnes             ierr = MatGetValues(aij->B,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
313b49de8d1SLois Curfman McInnes           }
314b49de8d1SLois Curfman McInnes         }
315b49de8d1SLois Curfman McInnes       }
316a8c6a408SBarry Smith     } else {
31729bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"Only local values currently supported");
318b49de8d1SLois Curfman McInnes     }
319b49de8d1SLois Curfman McInnes   }
3203a40ed3dSBarry Smith   PetscFunctionReturn(0);
321b49de8d1SLois Curfman McInnes }
322bc5ccf88SSatish Balay 
3234a2ae208SSatish Balay #undef __FUNCT__
3244a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyBegin_MPIAIJ"
325bc5ccf88SSatish Balay int MatAssemblyBegin_MPIAIJ(Mat mat,MatAssemblyType mode)
326bc5ccf88SSatish Balay {
327bc5ccf88SSatish Balay   Mat_MPIAIJ  *aij = (Mat_MPIAIJ*)mat->data;
328d36fbae8SSatish Balay   int         ierr,nstash,reallocs;
329bc5ccf88SSatish Balay   InsertMode  addv;
330bc5ccf88SSatish Balay 
331bc5ccf88SSatish Balay   PetscFunctionBegin;
332bc5ccf88SSatish Balay   if (aij->donotstash) {
333bc5ccf88SSatish Balay     PetscFunctionReturn(0);
334bc5ccf88SSatish Balay   }
335bc5ccf88SSatish Balay 
336bc5ccf88SSatish Balay   /* make sure all processors are either in INSERTMODE or ADDMODE */
337bc5ccf88SSatish Balay   ierr = MPI_Allreduce(&mat->insertmode,&addv,1,MPI_INT,MPI_BOR,mat->comm);CHKERRQ(ierr);
338bc5ccf88SSatish Balay   if (addv == (ADD_VALUES|INSERT_VALUES)) {
33929bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Some processors inserted others added");
340bc5ccf88SSatish Balay   }
341bc5ccf88SSatish Balay   mat->insertmode = addv; /* in case this processor had no cache */
342bc5ccf88SSatish Balay 
3438798bf22SSatish Balay   ierr = MatStashScatterBegin_Private(&mat->stash,aij->rowners);CHKERRQ(ierr);
3448798bf22SSatish Balay   ierr = MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);CHKERRQ(ierr);
345b0a32e0cSBarry Smith   PetscLogInfo(aij->A,"MatAssemblyBegin_MPIAIJ:Stash has %d entries, uses %d mallocs.\n",nstash,reallocs);
346bc5ccf88SSatish Balay   PetscFunctionReturn(0);
347bc5ccf88SSatish Balay }
348bc5ccf88SSatish Balay 
349bc5ccf88SSatish Balay 
3504a2ae208SSatish Balay #undef __FUNCT__
3514a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_MPIAIJ"
352bc5ccf88SSatish Balay int MatAssemblyEnd_MPIAIJ(Mat mat,MatAssemblyType mode)
353bc5ccf88SSatish Balay {
354bc5ccf88SSatish Balay   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
355a2d1c673SSatish Balay   int         i,j,rstart,ncols,n,ierr,flg;
356bc5ccf88SSatish Balay   int         *row,*col,other_disassembled;
35787828ca2SBarry Smith   PetscScalar *val;
358bc5ccf88SSatish Balay   InsertMode  addv = mat->insertmode;
3594032b0d1SHong Zhang #if defined(PETSC_HAVE_SUPERLUDIST) || defined(PETSC_HAVE_SPOOLES)
3609b9367d5SHong Zhang   PetscTruth  flag;
3619e070d67SMatthew Knepley #endif
362bc5ccf88SSatish Balay 
363bc5ccf88SSatish Balay   PetscFunctionBegin;
364bc5ccf88SSatish Balay   if (!aij->donotstash) {
365a2d1c673SSatish Balay     while (1) {
3668798bf22SSatish Balay       ierr = MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);CHKERRQ(ierr);
367a2d1c673SSatish Balay       if (!flg) break;
368a2d1c673SSatish Balay 
369bc5ccf88SSatish Balay       for (i=0; i<n;) {
370bc5ccf88SSatish Balay         /* Now identify the consecutive vals belonging to the same row */
371bc5ccf88SSatish Balay         for (j=i,rstart=row[j]; j<n; j++) { if (row[j] != rstart) break; }
372bc5ccf88SSatish Balay         if (j < n) ncols = j-i;
373bc5ccf88SSatish Balay         else       ncols = n-i;
374bc5ccf88SSatish Balay         /* Now assemble all these values with a single function call */
375bc5ccf88SSatish Balay         ierr = MatSetValues_MPIAIJ(mat,1,row+i,ncols,col+i,val+i,addv);CHKERRQ(ierr);
376bc5ccf88SSatish Balay         i = j;
377bc5ccf88SSatish Balay       }
378bc5ccf88SSatish Balay     }
3798798bf22SSatish Balay     ierr = MatStashScatterEnd_Private(&mat->stash);CHKERRQ(ierr);
380bc5ccf88SSatish Balay   }
381bc5ccf88SSatish Balay 
382bc5ccf88SSatish Balay   ierr = MatAssemblyBegin(aij->A,mode);CHKERRQ(ierr);
383bc5ccf88SSatish Balay   ierr = MatAssemblyEnd(aij->A,mode);CHKERRQ(ierr);
384bc5ccf88SSatish Balay 
385bc5ccf88SSatish Balay   /* determine if any processor has disassembled, if so we must
386bc5ccf88SSatish Balay      also disassemble ourselfs, in order that we may reassemble. */
387bc5ccf88SSatish Balay   /*
388bc5ccf88SSatish Balay      if nonzero structure of submatrix B cannot change then we know that
389bc5ccf88SSatish Balay      no processor disassembled thus we can skip this stuff
390bc5ccf88SSatish Balay   */
391bc5ccf88SSatish Balay   if (!((Mat_SeqAIJ*)aij->B->data)->nonew)  {
392bc5ccf88SSatish Balay     ierr = MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPI_INT,MPI_PROD,mat->comm);CHKERRQ(ierr);
393bc5ccf88SSatish Balay     if (mat->was_assembled && !other_disassembled) {
394bc5ccf88SSatish Balay       ierr = DisAssemble_MPIAIJ(mat);CHKERRQ(ierr);
395bc5ccf88SSatish Balay     }
396bc5ccf88SSatish Balay   }
397bc5ccf88SSatish Balay 
398bc5ccf88SSatish Balay   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
399bc5ccf88SSatish Balay     ierr = MatSetUpMultiply_MPIAIJ(mat);CHKERRQ(ierr);
400bc5ccf88SSatish Balay   }
401bc5ccf88SSatish Balay   ierr = MatAssemblyBegin(aij->B,mode);CHKERRQ(ierr);
402bc5ccf88SSatish Balay   ierr = MatAssemblyEnd(aij->B,mode);CHKERRQ(ierr);
403bc5ccf88SSatish Balay 
404606d414cSSatish Balay   if (aij->rowvalues) {
405606d414cSSatish Balay     ierr = PetscFree(aij->rowvalues);CHKERRQ(ierr);
406606d414cSSatish Balay     aij->rowvalues = 0;
407606d414cSSatish Balay   }
4089b9367d5SHong Zhang #if defined(PETSC_HAVE_SUPERLUDIST)
4099b9367d5SHong Zhang   ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_superlu_dist",&flag);CHKERRQ(ierr);
4109b9367d5SHong Zhang   if (flag) { ierr = MatUseSuperLU_DIST_MPIAIJ(mat);CHKERRQ(ierr); }
4119b9367d5SHong Zhang #endif
4124032b0d1SHong Zhang 
4134032b0d1SHong Zhang #if defined(PETSC_HAVE_SPOOLES)
4144032b0d1SHong Zhang   ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_spooles",&flag);CHKERRQ(ierr);
4154032b0d1SHong Zhang   if (flag) { ierr = MatUseSpooles_MPIAIJ(mat);CHKERRQ(ierr); }
4164032b0d1SHong Zhang #endif
417bc5ccf88SSatish Balay   PetscFunctionReturn(0);
418bc5ccf88SSatish Balay }
419bc5ccf88SSatish Balay 
4204a2ae208SSatish Balay #undef __FUNCT__
4214a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_MPIAIJ"
4228f6be9afSLois Curfman McInnes int MatZeroEntries_MPIAIJ(Mat A)
4231eb62cbbSBarry Smith {
42444a69424SLois Curfman McInnes   Mat_MPIAIJ *l = (Mat_MPIAIJ*)A->data;
425dbd7a890SLois Curfman McInnes   int        ierr;
4263a40ed3dSBarry Smith 
4273a40ed3dSBarry Smith   PetscFunctionBegin;
42878b31e54SBarry Smith   ierr = MatZeroEntries(l->A);CHKERRQ(ierr);
42978b31e54SBarry Smith   ierr = MatZeroEntries(l->B);CHKERRQ(ierr);
4303a40ed3dSBarry Smith   PetscFunctionReturn(0);
4311eb62cbbSBarry Smith }
4321eb62cbbSBarry Smith 
4334a2ae208SSatish Balay #undef __FUNCT__
4344a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_MPIAIJ"
43587828ca2SBarry Smith int MatZeroRows_MPIAIJ(Mat A,IS is,PetscScalar *diag)
4361eb62cbbSBarry Smith {
43744a69424SLois Curfman McInnes   Mat_MPIAIJ     *l = (Mat_MPIAIJ*)A->data;
43817699dbbSLois Curfman McInnes   int            i,ierr,N,*rows,*owners = l->rowners,size = l->size;
43935d8aa7fSBarry Smith   int            *procs,*nprocs,j,idx,nsends,*work,row;
44017699dbbSLois Curfman McInnes   int            nmax,*svalues,*starts,*owner,nrecvs,rank = l->rank;
4415392566eSBarry Smith   int            *rvalues,tag = A->tag,count,base,slen,n,*source;
4426a5c57faSSatish Balay   int            *lens,imdex,*lrows,*values,rstart=l->rstart;
443d6dfbf8fSBarry Smith   MPI_Comm       comm = A->comm;
4441eb62cbbSBarry Smith   MPI_Request    *send_waits,*recv_waits;
4451eb62cbbSBarry Smith   MPI_Status     recv_status,*send_status;
4461eb62cbbSBarry Smith   IS             istmp;
44735d8aa7fSBarry Smith   PetscTruth     found;
4481eb62cbbSBarry Smith 
4493a40ed3dSBarry Smith   PetscFunctionBegin;
450e03a110bSBarry Smith   ierr = ISGetLocalSize(is,&N);CHKERRQ(ierr);
45178b31e54SBarry Smith   ierr = ISGetIndices(is,&rows);CHKERRQ(ierr);
4521eb62cbbSBarry Smith 
4531eb62cbbSBarry Smith   /*  first count number of contributors to each processor */
454b0a32e0cSBarry Smith   ierr = PetscMalloc(2*size*sizeof(int),&nprocs);CHKERRQ(ierr);
455549d3d68SSatish Balay   ierr   = PetscMemzero(nprocs,2*size*sizeof(int));CHKERRQ(ierr);
456549d3d68SSatish Balay   procs  = nprocs + size;
457b0a32e0cSBarry Smith   ierr = PetscMalloc((N+1)*sizeof(int),&owner);CHKERRQ(ierr); /* see note*/
4581eb62cbbSBarry Smith   for (i=0; i<N; i++) {
4591eb62cbbSBarry Smith     idx = rows[i];
46035d8aa7fSBarry Smith     found = PETSC_FALSE;
46117699dbbSLois Curfman McInnes     for (j=0; j<size; j++) {
4621eb62cbbSBarry Smith       if (idx >= owners[j] && idx < owners[j+1]) {
46335d8aa7fSBarry Smith         nprocs[j]++; procs[j] = 1; owner[i] = j; found = PETSC_TRUE; break;
4641eb62cbbSBarry Smith       }
4651eb62cbbSBarry Smith     }
46629bbc08cSBarry Smith     if (!found) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Index out of range");
4671eb62cbbSBarry Smith   }
46817699dbbSLois Curfman McInnes   nsends = 0;  for (i=0; i<size; i++) { nsends += procs[i];}
4691eb62cbbSBarry Smith 
4701eb62cbbSBarry Smith   /* inform other processors of number of messages and max length*/
471b0a32e0cSBarry Smith   ierr = PetscMalloc(2*size*sizeof(int),&work);CHKERRQ(ierr);
4726831982aSBarry Smith   ierr   = MPI_Allreduce(nprocs,work,2*size,MPI_INT,PetscMaxSum_Op,comm);CHKERRQ(ierr);
4736831982aSBarry Smith   nrecvs = work[size+rank];
47417699dbbSLois Curfman McInnes   nmax   = work[rank];
475606d414cSSatish Balay   ierr   = PetscFree(work);CHKERRQ(ierr);
4761eb62cbbSBarry Smith 
4771eb62cbbSBarry Smith   /* post receives:   */
478b0a32e0cSBarry Smith   ierr = PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(int),&rvalues);CHKERRQ(ierr);
479b0a32e0cSBarry Smith   ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr);
4801eb62cbbSBarry Smith   for (i=0; i<nrecvs; i++) {
481ca161407SBarry Smith     ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPI_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr);
4821eb62cbbSBarry Smith   }
4831eb62cbbSBarry Smith 
4841eb62cbbSBarry Smith   /* do sends:
4851eb62cbbSBarry Smith       1) starts[i] gives the starting index in svalues for stuff going to
4861eb62cbbSBarry Smith          the ith processor
4871eb62cbbSBarry Smith   */
488b0a32e0cSBarry Smith   ierr = PetscMalloc((N+1)*sizeof(int),&svalues);CHKERRQ(ierr);
489b0a32e0cSBarry Smith   ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr);
490b0a32e0cSBarry Smith   ierr = PetscMalloc((size+1)*sizeof(int),&starts);CHKERRQ(ierr);
4911eb62cbbSBarry Smith   starts[0] = 0;
49217699dbbSLois Curfman McInnes   for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[i-1];}
4931eb62cbbSBarry Smith   for (i=0; i<N; i++) {
4941eb62cbbSBarry Smith     svalues[starts[owner[i]]++] = rows[i];
4951eb62cbbSBarry Smith   }
4966831982aSBarry Smith   ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr);
4971eb62cbbSBarry Smith 
4981eb62cbbSBarry Smith   starts[0] = 0;
49917699dbbSLois Curfman McInnes   for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[i-1];}
5001eb62cbbSBarry Smith   count = 0;
50117699dbbSLois Curfman McInnes   for (i=0; i<size; i++) {
5021eb62cbbSBarry Smith     if (procs[i]) {
503ca161407SBarry Smith       ierr = MPI_Isend(svalues+starts[i],nprocs[i],MPI_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr);
5041eb62cbbSBarry Smith     }
5051eb62cbbSBarry Smith   }
506606d414cSSatish Balay   ierr = PetscFree(starts);CHKERRQ(ierr);
5071eb62cbbSBarry Smith 
50817699dbbSLois Curfman McInnes   base = owners[rank];
5091eb62cbbSBarry Smith 
5101eb62cbbSBarry Smith   /*  wait on receives */
511b0a32e0cSBarry Smith   ierr   = PetscMalloc(2*(nrecvs+1)*sizeof(int),&lens);CHKERRQ(ierr);
5121eb62cbbSBarry Smith   source = lens + nrecvs;
5131eb62cbbSBarry Smith   count  = nrecvs; slen = 0;
5141eb62cbbSBarry Smith   while (count) {
515ca161407SBarry Smith     ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
5161eb62cbbSBarry Smith     /* unpack receives into our local space */
517ca161407SBarry Smith     ierr = MPI_Get_count(&recv_status,MPI_INT,&n);CHKERRQ(ierr);
518d6dfbf8fSBarry Smith     source[imdex]  = recv_status.MPI_SOURCE;
519d6dfbf8fSBarry Smith     lens[imdex]    = n;
5201eb62cbbSBarry Smith     slen          += n;
5211eb62cbbSBarry Smith     count--;
5221eb62cbbSBarry Smith   }
523606d414cSSatish Balay   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
5241eb62cbbSBarry Smith 
5251eb62cbbSBarry Smith   /* move the data into the send scatter */
526b0a32e0cSBarry Smith   ierr = PetscMalloc((slen+1)*sizeof(int),&lrows);CHKERRQ(ierr);
5271eb62cbbSBarry Smith   count = 0;
5281eb62cbbSBarry Smith   for (i=0; i<nrecvs; i++) {
5291eb62cbbSBarry Smith     values = rvalues + i*nmax;
5301eb62cbbSBarry Smith     for (j=0; j<lens[i]; j++) {
5311eb62cbbSBarry Smith       lrows[count++] = values[j] - base;
5321eb62cbbSBarry Smith     }
5331eb62cbbSBarry Smith   }
534606d414cSSatish Balay   ierr = PetscFree(rvalues);CHKERRQ(ierr);
535606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
536606d414cSSatish Balay   ierr = PetscFree(owner);CHKERRQ(ierr);
537606d414cSSatish Balay   ierr = PetscFree(nprocs);CHKERRQ(ierr);
5381eb62cbbSBarry Smith 
5391eb62cbbSBarry Smith   /* actually zap the local rows */
540029af93fSBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,slen,lrows,&istmp);CHKERRQ(ierr);
541b0a32e0cSBarry Smith   PetscLogObjectParent(A,istmp);
5426a5c57faSSatish Balay 
5436eb55b6aSBarry Smith   /*
5446eb55b6aSBarry Smith         Zero the required rows. If the "diagonal block" of the matrix
5456eb55b6aSBarry Smith      is square and the user wishes to set the diagonal we use seperate
5466eb55b6aSBarry Smith      code so that MatSetValues() is not called for each diagonal allocating
5476eb55b6aSBarry Smith      new memory, thus calling lots of mallocs and slowing things down.
5486eb55b6aSBarry Smith 
5496eb55b6aSBarry Smith        Contributed by: Mathew Knepley
5506eb55b6aSBarry Smith   */
551e2d53e46SBarry Smith   /* must zero l->B before l->A because the (diag) case below may put values into l->B*/
552e2d53e46SBarry Smith   ierr = MatZeroRows(l->B,istmp,0);CHKERRQ(ierr);
5536eb55b6aSBarry Smith   if (diag && (l->A->M == l->A->N)) {
5546eb55b6aSBarry Smith     ierr      = MatZeroRows(l->A,istmp,diag);CHKERRQ(ierr);
555e2d53e46SBarry Smith   } else if (diag) {
556e2d53e46SBarry Smith     ierr = MatZeroRows(l->A,istmp,0);CHKERRQ(ierr);
557fa46199cSSatish Balay     if (((Mat_SeqAIJ*)l->A->data)->nonew) {
55829bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"MatZeroRows() on rectangular matrices cannot be used with the Mat options\n\
559fa46199cSSatish Balay MAT_NO_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR");
5606525c446SSatish Balay     }
561e2d53e46SBarry Smith     for (i = 0; i < slen; i++) {
562e2d53e46SBarry Smith       row  = lrows[i] + rstart;
563e2d53e46SBarry Smith       ierr = MatSetValues(A,1,&row,1,&row,diag,INSERT_VALUES);CHKERRQ(ierr);
564e2d53e46SBarry Smith     }
565e2d53e46SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
566e2d53e46SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
5676eb55b6aSBarry Smith   } else {
5686a5c57faSSatish Balay     ierr = MatZeroRows(l->A,istmp,0);CHKERRQ(ierr);
5696eb55b6aSBarry Smith   }
57078b31e54SBarry Smith   ierr = ISDestroy(istmp);CHKERRQ(ierr);
571606d414cSSatish Balay   ierr = PetscFree(lrows);CHKERRQ(ierr);
57272dacd9aSBarry Smith 
5731eb62cbbSBarry Smith   /* wait on sends */
5741eb62cbbSBarry Smith   if (nsends) {
575b0a32e0cSBarry Smith     ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr);
576ca161407SBarry Smith     ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr);
577606d414cSSatish Balay     ierr = PetscFree(send_status);CHKERRQ(ierr);
5781eb62cbbSBarry Smith   }
579606d414cSSatish Balay   ierr = PetscFree(send_waits);CHKERRQ(ierr);
580606d414cSSatish Balay   ierr = PetscFree(svalues);CHKERRQ(ierr);
5811eb62cbbSBarry Smith 
5823a40ed3dSBarry Smith   PetscFunctionReturn(0);
5831eb62cbbSBarry Smith }
5841eb62cbbSBarry Smith 
5854a2ae208SSatish Balay #undef __FUNCT__
5864a2ae208SSatish Balay #define __FUNCT__ "MatMult_MPIAIJ"
5878f6be9afSLois Curfman McInnes int MatMult_MPIAIJ(Mat A,Vec xx,Vec yy)
5881eb62cbbSBarry Smith {
589416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
590fbd6ef76SBarry Smith   int        ierr,nt;
591416022c9SBarry Smith 
5923a40ed3dSBarry Smith   PetscFunctionBegin;
593a2ce50c7SBarry Smith   ierr = VecGetLocalSize(xx,&nt);CHKERRQ(ierr);
594273d9f13SBarry Smith   if (nt != A->n) {
595273d9f13SBarry Smith     SETERRQ2(PETSC_ERR_ARG_SIZ,"Incompatible partition of A (%d) and xx (%d)",A->n,nt);
596fbd6ef76SBarry Smith   }
59743a90d84SBarry Smith   ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
598f830108cSBarry Smith   ierr = (*a->A->ops->mult)(a->A,xx,yy);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,yy,yy);CHKERRQ(ierr);
6013a40ed3dSBarry Smith   PetscFunctionReturn(0);
6021eb62cbbSBarry Smith }
6031eb62cbbSBarry Smith 
6044a2ae208SSatish Balay #undef __FUNCT__
6054a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_MPIAIJ"
6068f6be9afSLois Curfman McInnes int MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
607da3a660dSBarry Smith {
608416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
609da3a660dSBarry Smith   int        ierr;
6103a40ed3dSBarry Smith 
6113a40ed3dSBarry Smith   PetscFunctionBegin;
61243a90d84SBarry Smith   ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
613f830108cSBarry Smith   ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
61443a90d84SBarry Smith   ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
615f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);CHKERRQ(ierr);
6163a40ed3dSBarry Smith   PetscFunctionReturn(0);
617da3a660dSBarry Smith }
618da3a660dSBarry Smith 
6194a2ae208SSatish Balay #undef __FUNCT__
6204a2ae208SSatish Balay #define __FUNCT__ "MatMultTranspose_MPIAIJ"
6217c922b88SBarry Smith int MatMultTranspose_MPIAIJ(Mat A,Vec xx,Vec yy)
622da3a660dSBarry Smith {
623416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
624da3a660dSBarry Smith   int        ierr;
625da3a660dSBarry Smith 
6263a40ed3dSBarry Smith   PetscFunctionBegin;
627da3a660dSBarry Smith   /* do nondiagonal part */
6287c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
629da3a660dSBarry Smith   /* send it on its way */
630537820f0SBarry Smith   ierr = VecScatterBegin(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
631da3a660dSBarry Smith   /* do local part */
6327c922b88SBarry Smith   ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr);
633da3a660dSBarry Smith   /* receive remote parts: note this assumes the values are not actually */
634da3a660dSBarry Smith   /* inserted in yy until the next line, which is true for my implementation*/
635da3a660dSBarry Smith   /* but is not perhaps always true. */
636537820f0SBarry Smith   ierr = VecScatterEnd(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
6373a40ed3dSBarry Smith   PetscFunctionReturn(0);
638da3a660dSBarry Smith }
639da3a660dSBarry Smith 
6404a2ae208SSatish Balay #undef __FUNCT__
6414a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_MPIAIJ"
6427c922b88SBarry Smith int MatMultTransposeAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
643da3a660dSBarry Smith {
644416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
645da3a660dSBarry Smith   int        ierr;
646da3a660dSBarry Smith 
6473a40ed3dSBarry Smith   PetscFunctionBegin;
648da3a660dSBarry Smith   /* do nondiagonal part */
6497c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
650da3a660dSBarry Smith   /* send it on its way */
651537820f0SBarry Smith   ierr = VecScatterBegin(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
652da3a660dSBarry Smith   /* do local part */
6537c922b88SBarry Smith   ierr = (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
654da3a660dSBarry Smith   /* receive remote parts: note this assumes the values are not actually */
655da3a660dSBarry Smith   /* inserted in yy until the next line, which is true for my implementation*/
656da3a660dSBarry Smith   /* but is not perhaps always true. */
6570a198c4cSBarry Smith   ierr = VecScatterEnd(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
6583a40ed3dSBarry Smith   PetscFunctionReturn(0);
659da3a660dSBarry Smith }
660da3a660dSBarry Smith 
6611eb62cbbSBarry Smith /*
6621eb62cbbSBarry Smith   This only works correctly for square matrices where the subblock A->A is the
6631eb62cbbSBarry Smith    diagonal block
6641eb62cbbSBarry Smith */
6654a2ae208SSatish Balay #undef __FUNCT__
6664a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_MPIAIJ"
6678f6be9afSLois Curfman McInnes int MatGetDiagonal_MPIAIJ(Mat A,Vec v)
6681eb62cbbSBarry Smith {
6693a40ed3dSBarry Smith   int        ierr;
670416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
6713a40ed3dSBarry Smith 
6723a40ed3dSBarry Smith   PetscFunctionBegin;
673273d9f13SBarry Smith   if (A->M != A->N) SETERRQ(PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block");
6745baf8537SBarry Smith   if (a->rstart != a->cstart || a->rend != a->cend) {
67529bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_SIZ,"row partition must equal col partition");
6763a40ed3dSBarry Smith   }
6773a40ed3dSBarry Smith   ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr);
6783a40ed3dSBarry Smith   PetscFunctionReturn(0);
6791eb62cbbSBarry Smith }
6801eb62cbbSBarry Smith 
6814a2ae208SSatish Balay #undef __FUNCT__
6824a2ae208SSatish Balay #define __FUNCT__ "MatScale_MPIAIJ"
68387828ca2SBarry Smith int MatScale_MPIAIJ(PetscScalar *aa,Mat A)
684052efed2SBarry Smith {
685052efed2SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
686052efed2SBarry Smith   int        ierr;
6873a40ed3dSBarry Smith 
6883a40ed3dSBarry Smith   PetscFunctionBegin;
689052efed2SBarry Smith   ierr = MatScale(aa,a->A);CHKERRQ(ierr);
690052efed2SBarry Smith   ierr = MatScale(aa,a->B);CHKERRQ(ierr);
6913a40ed3dSBarry Smith   PetscFunctionReturn(0);
692052efed2SBarry Smith }
693052efed2SBarry Smith 
6944a2ae208SSatish Balay #undef __FUNCT__
6954a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_MPIAIJ"
696e1311b90SBarry Smith int MatDestroy_MPIAIJ(Mat mat)
6971eb62cbbSBarry Smith {
69844a69424SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
6991eb62cbbSBarry Smith   int        ierr;
70083e2fdc7SBarry Smith 
7013a40ed3dSBarry Smith   PetscFunctionBegin;
702aa482453SBarry Smith #if defined(PETSC_USE_LOG)
703b0a32e0cSBarry Smith   PetscLogObjectState((PetscObject)mat,"Rows=%d, Cols=%d",mat->M,mat->N);
704a5a9c739SBarry Smith #endif
7058798bf22SSatish Balay   ierr = MatStashDestroy_Private(&mat->stash);CHKERRQ(ierr);
706606d414cSSatish Balay   ierr = PetscFree(aij->rowners);CHKERRQ(ierr);
70778b31e54SBarry Smith   ierr = MatDestroy(aij->A);CHKERRQ(ierr);
70878b31e54SBarry Smith   ierr = MatDestroy(aij->B);CHKERRQ(ierr);
709aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
7100f5bd95cSBarry Smith   if (aij->colmap) {ierr = PetscTableDelete(aij->colmap);CHKERRQ(ierr);}
711b1fc9764SSatish Balay #else
712606d414cSSatish Balay   if (aij->colmap) {ierr = PetscFree(aij->colmap);CHKERRQ(ierr);}
713b1fc9764SSatish Balay #endif
714606d414cSSatish Balay   if (aij->garray) {ierr = PetscFree(aij->garray);CHKERRQ(ierr);}
7157c922b88SBarry Smith   if (aij->lvec)   {ierr = VecDestroy(aij->lvec);CHKERRQ(ierr);}
7167c922b88SBarry Smith   if (aij->Mvctx)  {ierr = VecScatterDestroy(aij->Mvctx);CHKERRQ(ierr);}
717606d414cSSatish Balay   if (aij->rowvalues) {ierr = PetscFree(aij->rowvalues);CHKERRQ(ierr);}
718606d414cSSatish Balay   ierr = PetscFree(aij);CHKERRQ(ierr);
7193a40ed3dSBarry Smith   PetscFunctionReturn(0);
7201eb62cbbSBarry Smith }
721ee50ffe9SBarry Smith 
7224aedb280SBarry Smith extern int MatMPIAIJFactorInfo_SuperLu(Mat,PetscViewer);
723a072f7f7SHong Zhang extern int MatFactorInfo_Spooles(Mat,PetscViewer);
7244aedb280SBarry Smith 
7254a2ae208SSatish Balay #undef __FUNCT__
7264a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ_ASCIIorDraworSocket"
727b0a32e0cSBarry Smith int MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
728416022c9SBarry Smith {
72944a69424SLois Curfman McInnes   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*)mat->data;
730dbb450caSBarry Smith   Mat_SeqAIJ*       C = (Mat_SeqAIJ*)aij->A->data;
731fb9695e5SSatish Balay   int               ierr,shift = C->indexshift,rank = aij->rank,size = aij->size;
732f1af5d2fSBarry Smith   PetscTruth        isdraw,isascii,flg;
733b0a32e0cSBarry Smith   PetscViewer       sviewer;
734f3ef73ceSBarry Smith   PetscViewerFormat format;
735416022c9SBarry Smith 
7363a40ed3dSBarry Smith   PetscFunctionBegin;
737fb9695e5SSatish Balay   ierr  = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
738b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
7390f5bd95cSBarry Smith   if (isascii) {
740b0a32e0cSBarry Smith     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
741fb9695e5SSatish Balay     if (format == PETSC_VIEWER_ASCII_INFO_LONG) {
7424e220ebcSLois Curfman McInnes       MatInfo info;
7431dab6e02SBarry Smith       ierr = MPI_Comm_rank(mat->comm,&rank);CHKERRQ(ierr);
744888f2ed8SSatish Balay       ierr = MatGetInfo(mat,MAT_LOCAL,&info);CHKERRQ(ierr);
745b0a32e0cSBarry Smith       ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_no_inode",&flg);CHKERRQ(ierr);
7466831982aSBarry Smith       if (flg) {
747b0a32e0cSBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %d nz %d nz alloced %d mem %d, not using I-node routines\n",
748273d9f13SBarry Smith 					      rank,mat->m,(int)info.nz_used,(int)info.nz_allocated,(int)info.memory);CHKERRQ(ierr);
7496831982aSBarry Smith       } else {
750b0a32e0cSBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %d nz %d nz alloced %d mem %d, using I-node routines\n",
751273d9f13SBarry Smith 		    rank,mat->m,(int)info.nz_used,(int)info.nz_allocated,(int)info.memory);CHKERRQ(ierr);
7526831982aSBarry Smith       }
753888f2ed8SSatish Balay       ierr = MatGetInfo(aij->A,MAT_LOCAL,&info);CHKERRQ(ierr);
754b0a32e0cSBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %d \n",rank,(int)info.nz_used);CHKERRQ(ierr);
755888f2ed8SSatish Balay       ierr = MatGetInfo(aij->B,MAT_LOCAL,&info);CHKERRQ(ierr);
756b0a32e0cSBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %d \n",rank,(int)info.nz_used);CHKERRQ(ierr);
757b0a32e0cSBarry Smith       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
758a40aa06bSLois Curfman McInnes       ierr = VecScatterView(aij->Mvctx,viewer);CHKERRQ(ierr);
7593a40ed3dSBarry Smith       PetscFunctionReturn(0);
760fb9695e5SSatish Balay     } else if (format == PETSC_VIEWER_ASCII_INFO) {
7613a40ed3dSBarry Smith       PetscFunctionReturn(0);
7624aedb280SBarry Smith     } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
7634aedb280SBarry Smith #if defined(PETSC_HAVE_SUPERLUDIST) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_COMPLEX)
7644aedb280SBarry Smith       ierr = MatMPIAIJFactorInfo_SuperLu(mat,viewer);CHKERRQ(ierr);
7654aedb280SBarry Smith #endif
7660473ca23SHong Zhang #if defined(PETSC_HAVE_SPOOLES) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_COMPLEX)
767a072f7f7SHong Zhang       ierr = MatFactorInfo_Spooles(mat,viewer);CHKERRQ(ierr);
7680473ca23SHong Zhang #endif
769*471340fbSHong Zhang 
7704aedb280SBarry Smith       PetscFunctionReturn(0);
77108480c60SBarry Smith     }
7720f5bd95cSBarry Smith   } else if (isdraw) {
773b0a32e0cSBarry Smith     PetscDraw       draw;
77419bcc07fSBarry Smith     PetscTruth isnull;
775b0a32e0cSBarry Smith     ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
776b0a32e0cSBarry Smith     ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
77719bcc07fSBarry Smith   }
77819bcc07fSBarry Smith 
77917699dbbSLois Curfman McInnes   if (size == 1) {
780e36acaf3SBarry Smith     ierr = PetscObjectSetName((PetscObject)aij->A,mat->name);CHKERRQ(ierr);
78178b31e54SBarry Smith     ierr = MatView(aij->A,viewer);CHKERRQ(ierr);
7823a40ed3dSBarry Smith   } else {
78395373324SBarry Smith     /* assemble the entire matrix onto first processor. */
78495373324SBarry Smith     Mat         A;
785ec8511deSBarry Smith     Mat_SeqAIJ *Aloc;
786273d9f13SBarry Smith     int         M = mat->M,N = mat->N,m,*ai,*aj,row,*cols,i,*ct;
78787828ca2SBarry Smith     PetscScalar *a;
7882ee70a88SLois Curfman McInnes 
78917699dbbSLois Curfman McInnes     if (!rank) {
79055843e3eSBarry Smith       ierr = MatCreateMPIAIJ(mat->comm,M,N,M,N,0,PETSC_NULL,0,PETSC_NULL,&A);CHKERRQ(ierr);
7913a40ed3dSBarry Smith     } else {
79255843e3eSBarry Smith       ierr = MatCreateMPIAIJ(mat->comm,0,0,M,N,0,PETSC_NULL,0,PETSC_NULL,&A);CHKERRQ(ierr);
79395373324SBarry Smith     }
794b0a32e0cSBarry Smith     PetscLogObjectParent(mat,A);
795416022c9SBarry Smith 
79695373324SBarry Smith     /* copy over the A part */
797ec8511deSBarry Smith     Aloc = (Mat_SeqAIJ*)aij->A->data;
798273d9f13SBarry Smith     m = aij->A->m; ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
79995373324SBarry Smith     row = aij->rstart;
800dbb450caSBarry Smith     for (i=0; i<ai[m]+shift; i++) {aj[i] += aij->cstart + shift;}
80195373324SBarry Smith     for (i=0; i<m; i++) {
802416022c9SBarry Smith       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);CHKERRQ(ierr);
80395373324SBarry Smith       row++; a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
80495373324SBarry Smith     }
8052ee70a88SLois Curfman McInnes     aj = Aloc->j;
806dbb450caSBarry Smith     for (i=0; i<ai[m]+shift; i++) {aj[i] -= aij->cstart + shift;}
80795373324SBarry Smith 
80895373324SBarry Smith     /* copy over the B part */
809ec8511deSBarry Smith     Aloc = (Mat_SeqAIJ*)aij->B->data;
810273d9f13SBarry Smith     m    = aij->B->m;  ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
81195373324SBarry Smith     row  = aij->rstart;
812b0a32e0cSBarry Smith     ierr = PetscMalloc((ai[m]+1)*sizeof(int),&cols);CHKERRQ(ierr);
813b0a32e0cSBarry Smith     ct   = cols;
814dbb450caSBarry Smith     for (i=0; i<ai[m]+shift; i++) {cols[i] = aij->garray[aj[i]+shift];}
81595373324SBarry Smith     for (i=0; i<m; i++) {
816416022c9SBarry Smith       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);CHKERRQ(ierr);
81795373324SBarry Smith       row++; a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
81895373324SBarry Smith     }
819606d414cSSatish Balay     ierr = PetscFree(ct);CHKERRQ(ierr);
8206d4a8577SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
8216d4a8577SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
82255843e3eSBarry Smith     /*
82355843e3eSBarry Smith        Everyone has to call to draw the matrix since the graphics waits are
824b0a32e0cSBarry Smith        synchronized across all processors that share the PetscDraw object
82555843e3eSBarry Smith     */
826b0a32e0cSBarry Smith     ierr = PetscViewerGetSingleton(viewer,&sviewer);CHKERRQ(ierr);
827e03a110bSBarry Smith     if (!rank) {
828e36acaf3SBarry Smith       ierr = PetscObjectSetName((PetscObject)((Mat_MPIAIJ*)(A->data))->A,mat->name);CHKERRQ(ierr);
8296831982aSBarry Smith       ierr = MatView(((Mat_MPIAIJ*)(A->data))->A,sviewer);CHKERRQ(ierr);
83095373324SBarry Smith     }
831b0a32e0cSBarry Smith     ierr = PetscViewerRestoreSingleton(viewer,&sviewer);CHKERRQ(ierr);
83278b31e54SBarry Smith     ierr = MatDestroy(A);CHKERRQ(ierr);
83395373324SBarry Smith   }
8343a40ed3dSBarry Smith   PetscFunctionReturn(0);
8351eb62cbbSBarry Smith }
8361eb62cbbSBarry Smith 
8374a2ae208SSatish Balay #undef __FUNCT__
8384a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ"
839b0a32e0cSBarry Smith int MatView_MPIAIJ(Mat mat,PetscViewer viewer)
840416022c9SBarry Smith {
841416022c9SBarry Smith   int        ierr;
8426831982aSBarry Smith   PetscTruth isascii,isdraw,issocket,isbinary;
843416022c9SBarry Smith 
8443a40ed3dSBarry Smith   PetscFunctionBegin;
845b0a32e0cSBarry Smith   ierr  = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
846fb9695e5SSatish Balay   ierr  = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
847fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
848b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_SOCKET,&issocket);CHKERRQ(ierr);
8490f5bd95cSBarry Smith   if (isascii || isdraw || isbinary || issocket) {
8507b2a1423SBarry Smith     ierr = MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer);CHKERRQ(ierr);
8515cd90555SBarry Smith   } else {
85229bbc08cSBarry Smith     SETERRQ1(1,"Viewer type %s not supported by MPIAIJ matrices",((PetscObject)viewer)->type_name);
853416022c9SBarry Smith   }
8543a40ed3dSBarry Smith   PetscFunctionReturn(0);
855416022c9SBarry Smith }
856416022c9SBarry Smith 
8571eb62cbbSBarry Smith 
858c14dc6b6SHong Zhang 
8594a2ae208SSatish Balay #undef __FUNCT__
8604a2ae208SSatish Balay #define __FUNCT__ "MatRelax_MPIAIJ"
861c14dc6b6SHong Zhang int MatRelax_MPIAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,int its,int lits,Vec xx)
8628a729477SBarry Smith {
86344a69424SLois Curfman McInnes   Mat_MPIAIJ   *mat = (Mat_MPIAIJ*)matin->data;
864c14dc6b6SHong Zhang   int          ierr;
865c14dc6b6SHong Zhang   Vec          bb1;
866a6c309e7SHong Zhang   PetscScalar  mone=-1.0;
8678a729477SBarry Smith 
8683a40ed3dSBarry Smith   PetscFunctionBegin;
86991723122SBarry Smith   if (its <= 0 || lits <= 0) SETERRQ2(PETSC_ERR_ARG_WRONG,"Relaxation requires global its %d and local its %d both positive",its,lits);
870c14dc6b6SHong Zhang 
871c14dc6b6SHong Zhang   ierr = VecDuplicate(bb,&bb1);CHKERRQ(ierr);
8722798e883SHong Zhang 
873c16cb8f2SBarry Smith   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){
874da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
875bd3bf7d3SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,lits,xx);CHKERRQ(ierr);
8762798e883SHong Zhang       its--;
877da3a660dSBarry Smith     }
8782798e883SHong Zhang 
8792798e883SHong Zhang     while (its--) {
8803a40ed3dSBarry Smith       ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
8813a40ed3dSBarry Smith       ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
8822798e883SHong Zhang 
883c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
884c14dc6b6SHong Zhang       ierr = VecScale(&mone,mat->lvec);CHKERRQ(ierr);
885c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
8862798e883SHong Zhang 
887c14dc6b6SHong Zhang       /* local sweep */
888bd3bf7d3SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,lits,xx);
889c14dc6b6SHong Zhang       CHKERRQ(ierr);
8902798e883SHong Zhang     }
8913a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_FORWARD_SWEEP){
892da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
893c14dc6b6SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,PETSC_NULL,xx);CHKERRQ(ierr);
8942798e883SHong Zhang       its--;
895da3a660dSBarry Smith     }
8962798e883SHong Zhang     while (its--) {
8973a40ed3dSBarry Smith       ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
8983a40ed3dSBarry Smith       ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
8992798e883SHong Zhang 
900c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
901c14dc6b6SHong Zhang       ierr = VecScale(&mone,mat->lvec);CHKERRQ(ierr);
902c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
903c14dc6b6SHong Zhang 
904c14dc6b6SHong Zhang       /* local sweep */
905a6c309e7SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,PETSC_NULL,xx);
906c14dc6b6SHong Zhang       CHKERRQ(ierr);
9072798e883SHong Zhang     }
9083a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_BACKWARD_SWEEP){
909da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
910c14dc6b6SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,PETSC_NULL,xx);CHKERRQ(ierr);
9112798e883SHong Zhang       its--;
912da3a660dSBarry Smith     }
9132798e883SHong Zhang     while (its--) {
9142e8a6d31SBarry Smith       ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
9152e8a6d31SBarry Smith       ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
9162798e883SHong Zhang 
917c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
918c14dc6b6SHong Zhang       ierr = VecScale(&mone,mat->lvec);CHKERRQ(ierr);
919c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
9202798e883SHong Zhang 
921c14dc6b6SHong Zhang       /* local sweep */
922a6c309e7SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,PETSC_NULL,xx);
923c14dc6b6SHong Zhang       CHKERRQ(ierr);
9242798e883SHong Zhang     }
9253a40ed3dSBarry Smith   } else {
92629bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"Parallel SOR not supported");
927c16cb8f2SBarry Smith   }
928c14dc6b6SHong Zhang 
929c14dc6b6SHong Zhang   ierr = VecDestroy(bb1);CHKERRQ(ierr);
9303a40ed3dSBarry Smith   PetscFunctionReturn(0);
9318a729477SBarry Smith }
932a66be287SLois Curfman McInnes 
9334a2ae208SSatish Balay #undef __FUNCT__
9344a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_MPIAIJ"
9358f6be9afSLois Curfman McInnes int MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info)
936a66be287SLois Curfman McInnes {
937a66be287SLois Curfman McInnes   Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data;
938a66be287SLois Curfman McInnes   Mat        A = mat->A,B = mat->B;
9394e220ebcSLois Curfman McInnes   int        ierr;
940329f5518SBarry Smith   PetscReal  isend[5],irecv[5];
941a66be287SLois Curfman McInnes 
9423a40ed3dSBarry Smith   PetscFunctionBegin;
9434e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
9444e220ebcSLois Curfman McInnes   ierr = MatGetInfo(A,MAT_LOCAL,info);CHKERRQ(ierr);
9454e220ebcSLois Curfman McInnes   isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
9464e220ebcSLois Curfman McInnes   isend[3] = info->memory;  isend[4] = info->mallocs;
9474e220ebcSLois Curfman McInnes   ierr = MatGetInfo(B,MAT_LOCAL,info);CHKERRQ(ierr);
9484e220ebcSLois Curfman McInnes   isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
9494e220ebcSLois Curfman McInnes   isend[3] += info->memory;  isend[4] += info->mallocs;
950a66be287SLois Curfman McInnes   if (flag == MAT_LOCAL) {
9514e220ebcSLois Curfman McInnes     info->nz_used      = isend[0];
9524e220ebcSLois Curfman McInnes     info->nz_allocated = isend[1];
9534e220ebcSLois Curfman McInnes     info->nz_unneeded  = isend[2];
9544e220ebcSLois Curfman McInnes     info->memory       = isend[3];
9554e220ebcSLois Curfman McInnes     info->mallocs      = isend[4];
956a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_MAX) {
957d7d1e502SBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_MAX,matin->comm);CHKERRQ(ierr);
9584e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
9594e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
9604e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
9614e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
9624e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
963a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_SUM) {
964d7d1e502SBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_SUM,matin->comm);CHKERRQ(ierr);
9654e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
9664e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
9674e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
9684e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
9694e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
970a66be287SLois Curfman McInnes   }
9714e220ebcSLois Curfman McInnes   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
9724e220ebcSLois Curfman McInnes   info->fill_ratio_needed = 0;
9734e220ebcSLois Curfman McInnes   info->factor_mallocs    = 0;
974273d9f13SBarry Smith   info->rows_global       = (double)matin->M;
975273d9f13SBarry Smith   info->columns_global    = (double)matin->N;
976273d9f13SBarry Smith   info->rows_local        = (double)matin->m;
977273d9f13SBarry Smith   info->columns_local     = (double)matin->N;
9784e220ebcSLois Curfman McInnes 
9793a40ed3dSBarry Smith   PetscFunctionReturn(0);
980a66be287SLois Curfman McInnes }
981a66be287SLois Curfman McInnes 
9824a2ae208SSatish Balay #undef __FUNCT__
9834a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_MPIAIJ"
9848f6be9afSLois Curfman McInnes int MatSetOption_MPIAIJ(Mat A,MatOption op)
985c74985f6SBarry Smith {
986c0bbcb79SLois Curfman McInnes   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
9871dee9f54SBarry Smith   int        ierr;
988c74985f6SBarry Smith 
9893a40ed3dSBarry Smith   PetscFunctionBegin;
99012c028f9SKris Buschelman   switch (op) {
99112c028f9SKris Buschelman   case MAT_NO_NEW_NONZERO_LOCATIONS:
99212c028f9SKris Buschelman   case MAT_YES_NEW_NONZERO_LOCATIONS:
99312c028f9SKris Buschelman   case MAT_COLUMNS_UNSORTED:
99412c028f9SKris Buschelman   case MAT_COLUMNS_SORTED:
99512c028f9SKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
99612c028f9SKris Buschelman   case MAT_KEEP_ZEROED_ROWS:
99712c028f9SKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
99812c028f9SKris Buschelman   case MAT_USE_INODES:
99912c028f9SKris Buschelman   case MAT_DO_NOT_USE_INODES:
100012c028f9SKris Buschelman   case MAT_IGNORE_ZERO_ENTRIES:
10011dee9f54SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
10021dee9f54SBarry Smith     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
100312c028f9SKris Buschelman     break;
100412c028f9SKris Buschelman   case MAT_ROW_ORIENTED:
10057c922b88SBarry Smith     a->roworiented = PETSC_TRUE;
10061dee9f54SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
10071dee9f54SBarry Smith     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
100812c028f9SKris Buschelman     break;
100912c028f9SKris Buschelman   case MAT_ROWS_SORTED:
101012c028f9SKris Buschelman   case MAT_ROWS_UNSORTED:
101112c028f9SKris Buschelman   case MAT_YES_NEW_DIAGONALS:
1012d03495bdSKris Buschelman   case MAT_USE_SINGLE_PRECISION_SOLVES:
1013b0a32e0cSBarry Smith     PetscLogInfo(A,"MatSetOption_MPIAIJ:Option ignored\n");
101412c028f9SKris Buschelman     break;
101512c028f9SKris Buschelman   case MAT_COLUMN_ORIENTED:
10167c922b88SBarry Smith     a->roworiented = PETSC_FALSE;
10171dee9f54SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
10181dee9f54SBarry Smith     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
101912c028f9SKris Buschelman     break;
102012c028f9SKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
10217c922b88SBarry Smith     a->donotstash = PETSC_TRUE;
102212c028f9SKris Buschelman     break;
102312c028f9SKris Buschelman   case MAT_NO_NEW_DIAGONALS:
102429bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"MAT_NO_NEW_DIAGONALS");
102512c028f9SKris Buschelman   default:
102629bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"unknown option");
10273a40ed3dSBarry Smith   }
10283a40ed3dSBarry Smith   PetscFunctionReturn(0);
1029c74985f6SBarry Smith }
1030c74985f6SBarry Smith 
10314a2ae208SSatish Balay #undef __FUNCT__
10324a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_MPIAIJ"
103387828ca2SBarry Smith int MatGetRow_MPIAIJ(Mat matin,int row,int *nz,int **idx,PetscScalar **v)
103439e00950SLois Curfman McInnes {
1035154123eaSLois Curfman McInnes   Mat_MPIAIJ   *mat = (Mat_MPIAIJ*)matin->data;
103687828ca2SBarry Smith   PetscScalar  *vworkA,*vworkB,**pvA,**pvB,*v_p;
1037154123eaSLois Curfman McInnes   int          i,ierr,*cworkA,*cworkB,**pcA,**pcB,cstart = mat->cstart;
1038154123eaSLois Curfman McInnes   int          nztot,nzA,nzB,lrow,rstart = mat->rstart,rend = mat->rend;
103970f0671dSBarry Smith   int          *cmap,*idx_p;
104039e00950SLois Curfman McInnes 
10413a40ed3dSBarry Smith   PetscFunctionBegin;
104229bbc08cSBarry Smith   if (mat->getrowactive == PETSC_TRUE) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Already active");
10437a0afa10SBarry Smith   mat->getrowactive = PETSC_TRUE;
10447a0afa10SBarry Smith 
104570f0671dSBarry Smith   if (!mat->rowvalues && (idx || v)) {
10467a0afa10SBarry Smith     /*
10477a0afa10SBarry Smith         allocate enough space to hold information from the longest row.
10487a0afa10SBarry Smith     */
10497a0afa10SBarry Smith     Mat_SeqAIJ *Aa = (Mat_SeqAIJ*)mat->A->data,*Ba = (Mat_SeqAIJ*)mat->B->data;
1050273d9f13SBarry Smith     int     max = 1,tmp;
1051273d9f13SBarry Smith     for (i=0; i<matin->m; i++) {
10527a0afa10SBarry Smith       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
10537a0afa10SBarry Smith       if (max < tmp) { max = tmp; }
10547a0afa10SBarry Smith     }
105587828ca2SBarry Smith     ierr = PetscMalloc(max*(sizeof(int)+sizeof(PetscScalar)),&mat->rowvalues);CHKERRQ(ierr);
10567a0afa10SBarry Smith     mat->rowindices = (int*)(mat->rowvalues + max);
10577a0afa10SBarry Smith   }
10587a0afa10SBarry Smith 
105929bbc08cSBarry Smith   if (row < rstart || row >= rend) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Only local rows")
1060abc0e9e4SLois Curfman McInnes   lrow = row - rstart;
106139e00950SLois Curfman McInnes 
1062154123eaSLois Curfman McInnes   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1063154123eaSLois Curfman McInnes   if (!v)   {pvA = 0; pvB = 0;}
1064154123eaSLois Curfman McInnes   if (!idx) {pcA = 0; if (!v) pcB = 0;}
1065f830108cSBarry Smith   ierr = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1066f830108cSBarry Smith   ierr = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
1067154123eaSLois Curfman McInnes   nztot = nzA + nzB;
1068154123eaSLois Curfman McInnes 
106970f0671dSBarry Smith   cmap  = mat->garray;
1070154123eaSLois Curfman McInnes   if (v  || idx) {
1071154123eaSLois Curfman McInnes     if (nztot) {
1072154123eaSLois Curfman McInnes       /* Sort by increasing column numbers, assuming A and B already sorted */
107370f0671dSBarry Smith       int imark = -1;
1074154123eaSLois Curfman McInnes       if (v) {
107570f0671dSBarry Smith         *v = v_p = mat->rowvalues;
107639e00950SLois Curfman McInnes         for (i=0; i<nzB; i++) {
107770f0671dSBarry Smith           if (cmap[cworkB[i]] < cstart)   v_p[i] = vworkB[i];
1078154123eaSLois Curfman McInnes           else break;
1079154123eaSLois Curfman McInnes         }
1080154123eaSLois Curfman McInnes         imark = i;
108170f0671dSBarry Smith         for (i=0; i<nzA; i++)     v_p[imark+i] = vworkA[i];
108270f0671dSBarry Smith         for (i=imark; i<nzB; i++) v_p[nzA+i]   = vworkB[i];
1083154123eaSLois Curfman McInnes       }
1084154123eaSLois Curfman McInnes       if (idx) {
108570f0671dSBarry Smith         *idx = idx_p = mat->rowindices;
108670f0671dSBarry Smith         if (imark > -1) {
108770f0671dSBarry Smith           for (i=0; i<imark; i++) {
108870f0671dSBarry Smith             idx_p[i] = cmap[cworkB[i]];
108970f0671dSBarry Smith           }
109070f0671dSBarry Smith         } else {
1091154123eaSLois Curfman McInnes           for (i=0; i<nzB; i++) {
109270f0671dSBarry Smith             if (cmap[cworkB[i]] < cstart)   idx_p[i] = cmap[cworkB[i]];
1093154123eaSLois Curfman McInnes             else break;
1094154123eaSLois Curfman McInnes           }
1095154123eaSLois Curfman McInnes           imark = i;
109670f0671dSBarry Smith         }
109770f0671dSBarry Smith         for (i=0; i<nzA; i++)     idx_p[imark+i] = cstart + cworkA[i];
109870f0671dSBarry Smith         for (i=imark; i<nzB; i++) idx_p[nzA+i]   = cmap[cworkB[i]];
109939e00950SLois Curfman McInnes       }
11003f97c4b0SBarry Smith     } else {
11011ca473b0SSatish Balay       if (idx) *idx = 0;
11021ca473b0SSatish Balay       if (v)   *v   = 0;
11031ca473b0SSatish Balay     }
1104154123eaSLois Curfman McInnes   }
110539e00950SLois Curfman McInnes   *nz = nztot;
1106f830108cSBarry Smith   ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1107f830108cSBarry Smith   ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
11083a40ed3dSBarry Smith   PetscFunctionReturn(0);
110939e00950SLois Curfman McInnes }
111039e00950SLois Curfman McInnes 
11114a2ae208SSatish Balay #undef __FUNCT__
11124a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_MPIAIJ"
111387828ca2SBarry Smith int MatRestoreRow_MPIAIJ(Mat mat,int row,int *nz,int **idx,PetscScalar **v)
111439e00950SLois Curfman McInnes {
11157a0afa10SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
11163a40ed3dSBarry Smith 
11173a40ed3dSBarry Smith   PetscFunctionBegin;
11187a0afa10SBarry Smith   if (aij->getrowactive == PETSC_FALSE) {
111929bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"MatGetRow not called");
11207a0afa10SBarry Smith   }
11217a0afa10SBarry Smith   aij->getrowactive = PETSC_FALSE;
11223a40ed3dSBarry Smith   PetscFunctionReturn(0);
112339e00950SLois Curfman McInnes }
112439e00950SLois Curfman McInnes 
11254a2ae208SSatish Balay #undef __FUNCT__
11264a2ae208SSatish Balay #define __FUNCT__ "MatNorm_MPIAIJ"
1127329f5518SBarry Smith int MatNorm_MPIAIJ(Mat mat,NormType type,PetscReal *norm)
1128855ac2c5SLois Curfman McInnes {
1129855ac2c5SLois Curfman McInnes   Mat_MPIAIJ   *aij = (Mat_MPIAIJ*)mat->data;
1130ec8511deSBarry Smith   Mat_SeqAIJ   *amat = (Mat_SeqAIJ*)aij->A->data,*bmat = (Mat_SeqAIJ*)aij->B->data;
1131416022c9SBarry Smith   int          ierr,i,j,cstart = aij->cstart,shift = amat->indexshift;
1132329f5518SBarry Smith   PetscReal    sum = 0.0;
113387828ca2SBarry Smith   PetscScalar  *v;
113404ca555eSLois Curfman McInnes 
11353a40ed3dSBarry Smith   PetscFunctionBegin;
113617699dbbSLois Curfman McInnes   if (aij->size == 1) {
113714183eadSLois Curfman McInnes     ierr =  MatNorm(aij->A,type,norm);CHKERRQ(ierr);
113837fa93a5SLois Curfman McInnes   } else {
113904ca555eSLois Curfman McInnes     if (type == NORM_FROBENIUS) {
114004ca555eSLois Curfman McInnes       v = amat->a;
114104ca555eSLois Curfman McInnes       for (i=0; i<amat->nz; i++) {
1142aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
1143329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
114404ca555eSLois Curfman McInnes #else
114504ca555eSLois Curfman McInnes         sum += (*v)*(*v); v++;
114604ca555eSLois Curfman McInnes #endif
114704ca555eSLois Curfman McInnes       }
114804ca555eSLois Curfman McInnes       v = bmat->a;
114904ca555eSLois Curfman McInnes       for (i=0; i<bmat->nz; i++) {
1150aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
1151329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
115204ca555eSLois Curfman McInnes #else
115304ca555eSLois Curfman McInnes         sum += (*v)*(*v); v++;
115404ca555eSLois Curfman McInnes #endif
115504ca555eSLois Curfman McInnes       }
1156d7d1e502SBarry Smith       ierr = MPI_Allreduce(&sum,norm,1,MPIU_REAL,MPI_SUM,mat->comm);CHKERRQ(ierr);
115704ca555eSLois Curfman McInnes       *norm = sqrt(*norm);
11583a40ed3dSBarry Smith     } else if (type == NORM_1) { /* max column norm */
1159329f5518SBarry Smith       PetscReal *tmp,*tmp2;
116004ca555eSLois Curfman McInnes       int    *jj,*garray = aij->garray;
1161b0a32e0cSBarry Smith       ierr = PetscMalloc((mat->N+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1162b0a32e0cSBarry Smith       ierr = PetscMalloc((mat->N+1)*sizeof(PetscReal),&tmp2);CHKERRQ(ierr);
1163273d9f13SBarry Smith       ierr = PetscMemzero(tmp,mat->N*sizeof(PetscReal));CHKERRQ(ierr);
116404ca555eSLois Curfman McInnes       *norm = 0.0;
116504ca555eSLois Curfman McInnes       v = amat->a; jj = amat->j;
116604ca555eSLois Curfman McInnes       for (j=0; j<amat->nz; j++) {
1167579c6b6fSBarry Smith         tmp[cstart + *jj++ + shift] += PetscAbsScalar(*v);  v++;
116804ca555eSLois Curfman McInnes       }
116904ca555eSLois Curfman McInnes       v = bmat->a; jj = bmat->j;
117004ca555eSLois Curfman McInnes       for (j=0; j<bmat->nz; j++) {
1171579c6b6fSBarry Smith         tmp[garray[*jj++ + shift]] += PetscAbsScalar(*v); v++;
117204ca555eSLois Curfman McInnes       }
1173d7d1e502SBarry Smith       ierr = MPI_Allreduce(tmp,tmp2,mat->N,MPIU_REAL,MPI_SUM,mat->comm);CHKERRQ(ierr);
1174273d9f13SBarry Smith       for (j=0; j<mat->N; j++) {
117504ca555eSLois Curfman McInnes         if (tmp2[j] > *norm) *norm = tmp2[j];
117604ca555eSLois Curfman McInnes       }
1177606d414cSSatish Balay       ierr = PetscFree(tmp);CHKERRQ(ierr);
1178606d414cSSatish Balay       ierr = PetscFree(tmp2);CHKERRQ(ierr);
11793a40ed3dSBarry Smith     } else if (type == NORM_INFINITY) { /* max row norm */
1180329f5518SBarry Smith       PetscReal ntemp = 0.0;
1181273d9f13SBarry Smith       for (j=0; j<aij->A->m; j++) {
1182dbb450caSBarry Smith         v = amat->a + amat->i[j] + shift;
118304ca555eSLois Curfman McInnes         sum = 0.0;
118404ca555eSLois Curfman McInnes         for (i=0; i<amat->i[j+1]-amat->i[j]; i++) {
1185cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
118604ca555eSLois Curfman McInnes         }
1187dbb450caSBarry Smith         v = bmat->a + bmat->i[j] + shift;
118804ca555eSLois Curfman McInnes         for (i=0; i<bmat->i[j+1]-bmat->i[j]; i++) {
1189cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
119004ca555eSLois Curfman McInnes         }
1191515d9167SLois Curfman McInnes         if (sum > ntemp) ntemp = sum;
119204ca555eSLois Curfman McInnes       }
1193d7d1e502SBarry Smith       ierr = MPI_Allreduce(&ntemp,norm,1,MPIU_REAL,MPI_MAX,mat->comm);CHKERRQ(ierr);
1194ca161407SBarry Smith     } else {
119529bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"No support for two norm");
119604ca555eSLois Curfman McInnes     }
119737fa93a5SLois Curfman McInnes   }
11983a40ed3dSBarry Smith   PetscFunctionReturn(0);
1199855ac2c5SLois Curfman McInnes }
1200855ac2c5SLois Curfman McInnes 
12014a2ae208SSatish Balay #undef __FUNCT__
12024a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_MPIAIJ"
12038f6be9afSLois Curfman McInnes int MatTranspose_MPIAIJ(Mat A,Mat *matout)
1204b7c46309SBarry Smith {
1205b7c46309SBarry Smith   Mat_MPIAIJ   *a = (Mat_MPIAIJ*)A->data;
1206dbb450caSBarry Smith   Mat_SeqAIJ   *Aloc = (Mat_SeqAIJ*)a->A->data;
1207416022c9SBarry Smith   int          ierr,shift = Aloc->indexshift;
1208273d9f13SBarry Smith   int          M = A->M,N = A->N,m,*ai,*aj,row,*cols,i,*ct;
12093a40ed3dSBarry Smith   Mat          B;
121087828ca2SBarry Smith   PetscScalar  *array;
1211b7c46309SBarry Smith 
12123a40ed3dSBarry Smith   PetscFunctionBegin;
12137c922b88SBarry Smith   if (!matout && M != N) {
121429bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1215d4bb536fSBarry Smith   }
1216d4bb536fSBarry Smith 
1217273d9f13SBarry Smith   ierr = MatCreateMPIAIJ(A->comm,A->n,A->m,N,M,0,PETSC_NULL,0,PETSC_NULL,&B);CHKERRQ(ierr);
1218b7c46309SBarry Smith 
1219b7c46309SBarry Smith   /* copy over the A part */
1220ec8511deSBarry Smith   Aloc = (Mat_SeqAIJ*)a->A->data;
1221273d9f13SBarry Smith   m = a->A->m; ai = Aloc->i; aj = Aloc->j; array = Aloc->a;
1222b7c46309SBarry Smith   row = a->rstart;
1223dbb450caSBarry Smith   for (i=0; i<ai[m]+shift; i++) {aj[i] += a->cstart + shift;}
1224b7c46309SBarry Smith   for (i=0; i<m; i++) {
1225416022c9SBarry Smith     ierr = MatSetValues(B,ai[i+1]-ai[i],aj,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
1226b7c46309SBarry Smith     row++; array += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
1227b7c46309SBarry Smith   }
1228b7c46309SBarry Smith   aj = Aloc->j;
12294af08d9eSBarry Smith   for (i=0; i<ai[m]+shift; i++) {aj[i] -= a->cstart + shift;}
1230b7c46309SBarry Smith 
1231b7c46309SBarry Smith   /* copy over the B part */
1232ec8511deSBarry Smith   Aloc = (Mat_SeqAIJ*)a->B->data;
1233273d9f13SBarry Smith   m = a->B->m;  ai = Aloc->i; aj = Aloc->j; array = Aloc->a;
1234b7c46309SBarry Smith   row  = a->rstart;
1235b0a32e0cSBarry Smith   ierr = PetscMalloc((1+ai[m]-shift)*sizeof(int),&cols);CHKERRQ(ierr);
1236b0a32e0cSBarry Smith   ct   = cols;
1237dbb450caSBarry Smith   for (i=0; i<ai[m]+shift; i++) {cols[i] = a->garray[aj[i]+shift];}
1238b7c46309SBarry Smith   for (i=0; i<m; i++) {
1239416022c9SBarry Smith     ierr = MatSetValues(B,ai[i+1]-ai[i],cols,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
1240b7c46309SBarry Smith     row++; array += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
1241b7c46309SBarry Smith   }
1242606d414cSSatish Balay   ierr = PetscFree(ct);CHKERRQ(ierr);
12436d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
12446d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
12457c922b88SBarry Smith   if (matout) {
12460de55854SLois Curfman McInnes     *matout = B;
12470de55854SLois Curfman McInnes   } else {
1248273d9f13SBarry Smith     ierr = MatHeaderCopy(A,B);CHKERRQ(ierr);
12490de55854SLois Curfman McInnes   }
12503a40ed3dSBarry Smith   PetscFunctionReturn(0);
1251b7c46309SBarry Smith }
1252b7c46309SBarry Smith 
12534a2ae208SSatish Balay #undef __FUNCT__
12544a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_MPIAIJ"
12554b967eb1SSatish Balay int MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr)
1256a008b906SSatish Balay {
12574b967eb1SSatish Balay   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
12584b967eb1SSatish Balay   Mat        a = aij->A,b = aij->B;
1259a008b906SSatish Balay   int        ierr,s1,s2,s3;
1260a008b906SSatish Balay 
12613a40ed3dSBarry Smith   PetscFunctionBegin;
12624b967eb1SSatish Balay   ierr = MatGetLocalSize(mat,&s2,&s3);CHKERRQ(ierr);
12634b967eb1SSatish Balay   if (rr) {
1264e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr);
126529bbc08cSBarry Smith     if (s1!=s3) SETERRQ(PETSC_ERR_ARG_SIZ,"right vector non-conforming local size");
12664b967eb1SSatish Balay     /* Overlap communication with computation. */
126743a90d84SBarry Smith     ierr = VecScatterBegin(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx);CHKERRQ(ierr);
1268a008b906SSatish Balay   }
12694b967eb1SSatish Balay   if (ll) {
1270e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr);
127129bbc08cSBarry Smith     if (s1!=s2) SETERRQ(PETSC_ERR_ARG_SIZ,"left vector non-conforming local size");
1272f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,ll,0);CHKERRQ(ierr);
12734b967eb1SSatish Balay   }
12744b967eb1SSatish Balay   /* scale  the diagonal block */
1275f830108cSBarry Smith   ierr = (*a->ops->diagonalscale)(a,ll,rr);CHKERRQ(ierr);
12764b967eb1SSatish Balay 
12774b967eb1SSatish Balay   if (rr) {
12784b967eb1SSatish Balay     /* Do a scatter end and then right scale the off-diagonal block */
127943a90d84SBarry Smith     ierr = VecScatterEnd(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx);CHKERRQ(ierr);
1280f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,0,aij->lvec);CHKERRQ(ierr);
12814b967eb1SSatish Balay   }
12824b967eb1SSatish Balay 
12833a40ed3dSBarry Smith   PetscFunctionReturn(0);
1284a008b906SSatish Balay }
1285a008b906SSatish Balay 
1286a008b906SSatish Balay 
12874a2ae208SSatish Balay #undef __FUNCT__
12884a2ae208SSatish Balay #define __FUNCT__ "MatPrintHelp_MPIAIJ"
12898f6be9afSLois Curfman McInnes int MatPrintHelp_MPIAIJ(Mat A)
1290682d7d0cSBarry Smith {
1291682d7d0cSBarry Smith   Mat_MPIAIJ *a   = (Mat_MPIAIJ*)A->data;
12923a40ed3dSBarry Smith   int        ierr;
1293682d7d0cSBarry Smith 
12943a40ed3dSBarry Smith   PetscFunctionBegin;
12953a40ed3dSBarry Smith   if (!a->rank) {
12963a40ed3dSBarry Smith     ierr = MatPrintHelp_SeqAIJ(a->A);CHKERRQ(ierr);
12973a40ed3dSBarry Smith   }
12983a40ed3dSBarry Smith   PetscFunctionReturn(0);
1299682d7d0cSBarry Smith }
1300682d7d0cSBarry Smith 
13014a2ae208SSatish Balay #undef __FUNCT__
13024a2ae208SSatish Balay #define __FUNCT__ "MatGetBlockSize_MPIAIJ"
13038f6be9afSLois Curfman McInnes int MatGetBlockSize_MPIAIJ(Mat A,int *bs)
13045a838052SSatish Balay {
13053a40ed3dSBarry Smith   PetscFunctionBegin;
13065a838052SSatish Balay   *bs = 1;
13073a40ed3dSBarry Smith   PetscFunctionReturn(0);
13085a838052SSatish Balay }
13094a2ae208SSatish Balay #undef __FUNCT__
13104a2ae208SSatish Balay #define __FUNCT__ "MatSetUnfactored_MPIAIJ"
13118f6be9afSLois Curfman McInnes int MatSetUnfactored_MPIAIJ(Mat A)
1312bb5a7306SBarry Smith {
1313bb5a7306SBarry Smith   Mat_MPIAIJ *a   = (Mat_MPIAIJ*)A->data;
1314bb5a7306SBarry Smith   int        ierr;
13153a40ed3dSBarry Smith 
13163a40ed3dSBarry Smith   PetscFunctionBegin;
1317bb5a7306SBarry Smith   ierr = MatSetUnfactored(a->A);CHKERRQ(ierr);
13183a40ed3dSBarry Smith   PetscFunctionReturn(0);
1319bb5a7306SBarry Smith }
1320bb5a7306SBarry Smith 
13214a2ae208SSatish Balay #undef __FUNCT__
13224a2ae208SSatish Balay #define __FUNCT__ "MatEqual_MPIAIJ"
1323d4bb536fSBarry Smith int MatEqual_MPIAIJ(Mat A,Mat B,PetscTruth *flag)
1324d4bb536fSBarry Smith {
1325d4bb536fSBarry Smith   Mat_MPIAIJ *matB = (Mat_MPIAIJ*)B->data,*matA = (Mat_MPIAIJ*)A->data;
1326d4bb536fSBarry Smith   Mat        a,b,c,d;
1327d4bb536fSBarry Smith   PetscTruth flg;
1328d4bb536fSBarry Smith   int        ierr;
1329d4bb536fSBarry Smith 
13303a40ed3dSBarry Smith   PetscFunctionBegin;
1331273d9f13SBarry Smith   ierr = PetscTypeCompare((PetscObject)B,MATMPIAIJ,&flg);CHKERRQ(ierr);
1332273d9f13SBarry Smith   if (!flg) SETERRQ(PETSC_ERR_ARG_INCOMP,"Matrices must be same type");
1333d4bb536fSBarry Smith   a = matA->A; b = matA->B;
1334d4bb536fSBarry Smith   c = matB->A; d = matB->B;
1335d4bb536fSBarry Smith 
1336d4bb536fSBarry Smith   ierr = MatEqual(a,c,&flg);CHKERRQ(ierr);
1337d4bb536fSBarry Smith   if (flg == PETSC_TRUE) {
1338d4bb536fSBarry Smith     ierr = MatEqual(b,d,&flg);CHKERRQ(ierr);
1339d4bb536fSBarry Smith   }
1340ca161407SBarry Smith   ierr = MPI_Allreduce(&flg,flag,1,MPI_INT,MPI_LAND,A->comm);CHKERRQ(ierr);
13413a40ed3dSBarry Smith   PetscFunctionReturn(0);
1342d4bb536fSBarry Smith }
1343d4bb536fSBarry Smith 
13444a2ae208SSatish Balay #undef __FUNCT__
13454a2ae208SSatish Balay #define __FUNCT__ "MatCopy_MPIAIJ"
1346cb5b572fSBarry Smith int MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str)
1347cb5b572fSBarry Smith {
1348cb5b572fSBarry Smith   int        ierr;
1349cb5b572fSBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;
1350cb5b572fSBarry Smith   Mat_MPIAIJ *b = (Mat_MPIAIJ *)B->data;
1351273d9f13SBarry Smith   PetscTruth flg;
1352cb5b572fSBarry Smith 
1353cb5b572fSBarry Smith   PetscFunctionBegin;
1354273d9f13SBarry Smith   ierr = PetscTypeCompare((PetscObject)B,MATMPIAIJ,&flg);CHKERRQ(ierr);
1355273d9f13SBarry Smith   if (str != SAME_NONZERO_PATTERN || !flg) {
1356cb5b572fSBarry Smith     /* because of the column compression in the off-processor part of the matrix a->B,
1357cb5b572fSBarry Smith        the number of columns in a->B and b->B may be different, hence we cannot call
1358cb5b572fSBarry Smith        the MatCopy() directly on the two parts. If need be, we can provide a more
1359cb5b572fSBarry Smith        efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices
1360cb5b572fSBarry Smith        then copying the submatrices */
1361cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1362cb5b572fSBarry Smith   } else {
1363cb5b572fSBarry Smith     ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr);
1364cb5b572fSBarry Smith     ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr);
1365cb5b572fSBarry Smith   }
1366cb5b572fSBarry Smith   PetscFunctionReturn(0);
1367cb5b572fSBarry Smith }
1368cb5b572fSBarry Smith 
13694a2ae208SSatish Balay #undef __FUNCT__
13704a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_MPIAIJ"
1371273d9f13SBarry Smith int MatSetUpPreallocation_MPIAIJ(Mat A)
1372273d9f13SBarry Smith {
1373273d9f13SBarry Smith   int        ierr;
1374273d9f13SBarry Smith 
1375273d9f13SBarry Smith   PetscFunctionBegin;
1376273d9f13SBarry Smith   ierr =  MatMPIAIJSetPreallocation(A,PETSC_DEFAULT,0,PETSC_DEFAULT,0);CHKERRQ(ierr);
1377273d9f13SBarry Smith   PetscFunctionReturn(0);
1378273d9f13SBarry Smith }
1379273d9f13SBarry Smith 
1380ca44d042SBarry Smith EXTERN int MatDuplicate_MPIAIJ(Mat,MatDuplicateOption,Mat *);
1381ca44d042SBarry Smith EXTERN int MatIncreaseOverlap_MPIAIJ(Mat,int,IS *,int);
1382ca44d042SBarry Smith EXTERN int MatFDColoringCreate_MPIAIJ(Mat,ISColoring,MatFDColoring);
1383ca44d042SBarry Smith EXTERN int MatGetSubMatrices_MPIAIJ (Mat,int,IS *,IS *,MatReuse,Mat **);
1384ca44d042SBarry Smith EXTERN int MatGetSubMatrix_MPIAIJ (Mat,IS,IS,int,MatReuse,Mat *);
138587828ca2SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
1386b9617806SBarry Smith EXTERN int MatLUFactorSymbolic_MPIAIJ_TFS(Mat,IS,IS,MatLUInfo*,Mat*);
1387b9617806SBarry Smith #endif
138800e6dbe6SBarry Smith 
1389ac90fabeSBarry Smith #include "petscblaslapack.h"
1390ac90fabeSBarry Smith 
1391ac90fabeSBarry Smith #undef __FUNCT__
1392ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_MPIAIJ"
1393ac90fabeSBarry Smith int MatAXPY_MPIAIJ(PetscScalar *a,Mat X,Mat Y,MatStructure str)
1394ac90fabeSBarry Smith {
1395ac90fabeSBarry Smith   int        ierr,one;
1396ac90fabeSBarry Smith   Mat_MPIAIJ *xx  = (Mat_MPIAIJ *)X->data,*yy = (Mat_MPIAIJ *)Y->data;
1397ac90fabeSBarry Smith   Mat_SeqAIJ *x,*y;
1398ac90fabeSBarry Smith 
1399ac90fabeSBarry Smith   PetscFunctionBegin;
1400ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
1401ac90fabeSBarry Smith     x  = (Mat_SeqAIJ *)xx->A->data;
1402ac90fabeSBarry Smith     y  = (Mat_SeqAIJ *)yy->A->data;
1403ac90fabeSBarry Smith     BLaxpy_(&x->nz,a,x->a,&one,y->a,&one);
1404ac90fabeSBarry Smith     x  = (Mat_SeqAIJ *)xx->B->data;
1405ac90fabeSBarry Smith     y  = (Mat_SeqAIJ *)yy->B->data;
1406ac90fabeSBarry Smith     BLaxpy_(&x->nz,a,x->a,&one,y->a,&one);
1407ac90fabeSBarry Smith   } else {
1408ac90fabeSBarry Smith     ierr = MatAXPY_Basic(a,X,Y,str);CHKERRQ(ierr);
1409ac90fabeSBarry Smith   }
1410ac90fabeSBarry Smith   PetscFunctionReturn(0);
1411ac90fabeSBarry Smith }
1412ac90fabeSBarry Smith 
14138a729477SBarry Smith /* -------------------------------------------------------------------*/
1414cda55fadSBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ,
1415cda55fadSBarry Smith        MatGetRow_MPIAIJ,
1416cda55fadSBarry Smith        MatRestoreRow_MPIAIJ,
1417cda55fadSBarry Smith        MatMult_MPIAIJ,
1418cda55fadSBarry Smith        MatMultAdd_MPIAIJ,
14197c922b88SBarry Smith        MatMultTranspose_MPIAIJ,
14207c922b88SBarry Smith        MatMultTransposeAdd_MPIAIJ,
1421cda55fadSBarry Smith        0,
1422cda55fadSBarry Smith        0,
1423cda55fadSBarry Smith        0,
1424cda55fadSBarry Smith        0,
1425cda55fadSBarry Smith        0,
1426cda55fadSBarry Smith        0,
142744a69424SLois Curfman McInnes        MatRelax_MPIAIJ,
1428b7c46309SBarry Smith        MatTranspose_MPIAIJ,
1429cda55fadSBarry Smith        MatGetInfo_MPIAIJ,
1430cda55fadSBarry Smith        MatEqual_MPIAIJ,
1431cda55fadSBarry Smith        MatGetDiagonal_MPIAIJ,
1432cda55fadSBarry Smith        MatDiagonalScale_MPIAIJ,
1433cda55fadSBarry Smith        MatNorm_MPIAIJ,
1434cda55fadSBarry Smith        MatAssemblyBegin_MPIAIJ,
1435cda55fadSBarry Smith        MatAssemblyEnd_MPIAIJ,
14361eb62cbbSBarry Smith        0,
1437cda55fadSBarry Smith        MatSetOption_MPIAIJ,
1438cda55fadSBarry Smith        MatZeroEntries_MPIAIJ,
1439cda55fadSBarry Smith        MatZeroRows_MPIAIJ,
144087828ca2SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
1441b9617806SBarry Smith        MatLUFactorSymbolic_MPIAIJ_TFS,
1442b9617806SBarry Smith #else
1443cda55fadSBarry Smith        0,
1444b9617806SBarry Smith #endif
1445cda55fadSBarry Smith        0,
1446cda55fadSBarry Smith        0,
1447cda55fadSBarry Smith        0,
1448273d9f13SBarry Smith        MatSetUpPreallocation_MPIAIJ,
1449cda55fadSBarry Smith        0,
1450cda55fadSBarry Smith        0,
1451cda55fadSBarry Smith        0,
1452cda55fadSBarry Smith        0,
14532e8a6d31SBarry Smith        MatDuplicate_MPIAIJ,
1454cda55fadSBarry Smith        0,
1455cda55fadSBarry Smith        0,
1456cda55fadSBarry Smith        0,
1457cda55fadSBarry Smith        0,
1458ac90fabeSBarry Smith        MatAXPY_MPIAIJ,
1459cda55fadSBarry Smith        MatGetSubMatrices_MPIAIJ,
1460cda55fadSBarry Smith        MatIncreaseOverlap_MPIAIJ,
1461cda55fadSBarry Smith        MatGetValues_MPIAIJ,
1462cb5b572fSBarry Smith        MatCopy_MPIAIJ,
1463052efed2SBarry Smith        MatPrintHelp_MPIAIJ,
1464cda55fadSBarry Smith        MatScale_MPIAIJ,
1465cda55fadSBarry Smith        0,
1466cda55fadSBarry Smith        0,
1467cda55fadSBarry Smith        0,
1468cda55fadSBarry Smith        MatGetBlockSize_MPIAIJ,
1469cda55fadSBarry Smith        0,
1470cda55fadSBarry Smith        0,
1471cda55fadSBarry Smith        0,
1472cda55fadSBarry Smith        0,
1473cda55fadSBarry Smith        MatFDColoringCreate_MPIAIJ,
1474cda55fadSBarry Smith        0,
1475cda55fadSBarry Smith        MatSetUnfactored_MPIAIJ,
1476cda55fadSBarry Smith        0,
1477cda55fadSBarry Smith        0,
1478cda55fadSBarry Smith        MatGetSubMatrix_MPIAIJ,
1479e03a110bSBarry Smith        MatDestroy_MPIAIJ,
1480e03a110bSBarry Smith        MatView_MPIAIJ,
14818a124369SBarry Smith        MatGetPetscMaps_Petsc,
1482a2243be0SBarry Smith        0,
1483a2243be0SBarry Smith        0,
1484a2243be0SBarry Smith        0,
1485a2243be0SBarry Smith        0,
1486a2243be0SBarry Smith        0,
1487a2243be0SBarry Smith        0,
1488a2243be0SBarry Smith        0,
1489a2243be0SBarry Smith        0,
1490a2243be0SBarry Smith        MatSetColoring_MPIAIJ,
1491779c1a83SBarry Smith        MatSetValuesAdic_MPIAIJ,
1492779c1a83SBarry Smith        MatSetValuesAdifor_MPIAIJ
1493a2243be0SBarry Smith };
149436ce4990SBarry Smith 
14952e8a6d31SBarry Smith /* ----------------------------------------------------------------------------------------*/
14962e8a6d31SBarry Smith 
1497fb2e594dSBarry Smith EXTERN_C_BEGIN
14984a2ae208SSatish Balay #undef __FUNCT__
14994a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_MPIAIJ"
15002e8a6d31SBarry Smith int MatStoreValues_MPIAIJ(Mat mat)
15012e8a6d31SBarry Smith {
15022e8a6d31SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data;
15032e8a6d31SBarry Smith   int        ierr;
15042e8a6d31SBarry Smith 
15052e8a6d31SBarry Smith   PetscFunctionBegin;
15062e8a6d31SBarry Smith   ierr = MatStoreValues(aij->A);CHKERRQ(ierr);
15072e8a6d31SBarry Smith   ierr = MatStoreValues(aij->B);CHKERRQ(ierr);
15082e8a6d31SBarry Smith   PetscFunctionReturn(0);
15092e8a6d31SBarry Smith }
1510fb2e594dSBarry Smith EXTERN_C_END
15112e8a6d31SBarry Smith 
1512fb2e594dSBarry Smith EXTERN_C_BEGIN
15134a2ae208SSatish Balay #undef __FUNCT__
15144a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_MPIAIJ"
15152e8a6d31SBarry Smith int MatRetrieveValues_MPIAIJ(Mat mat)
15162e8a6d31SBarry Smith {
15172e8a6d31SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data;
15182e8a6d31SBarry Smith   int        ierr;
15192e8a6d31SBarry Smith 
15202e8a6d31SBarry Smith   PetscFunctionBegin;
15212e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr);
15222e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr);
15232e8a6d31SBarry Smith   PetscFunctionReturn(0);
15242e8a6d31SBarry Smith }
1525fb2e594dSBarry Smith EXTERN_C_END
15268a729477SBarry Smith 
1527e090d566SSatish Balay #include "petscpc.h"
152827508adbSBarry Smith EXTERN_C_BEGIN
1529ca44d042SBarry Smith EXTERN int MatGetDiagonalBlock_MPIAIJ(Mat,PetscTruth *,MatReuse,Mat *);
153027508adbSBarry Smith EXTERN_C_END
153127508adbSBarry Smith 
1532273d9f13SBarry Smith EXTERN_C_BEGIN
15334a2ae208SSatish Balay #undef __FUNCT__
15344a2ae208SSatish Balay #define __FUNCT__ "MatCreate_MPIAIJ"
1535273d9f13SBarry Smith int MatCreate_MPIAIJ(Mat B)
15368a729477SBarry Smith {
153744cd7ae7SLois Curfman McInnes   Mat_MPIAIJ *b;
1538f1af5d2fSBarry Smith   int        ierr,i,size;
1539416022c9SBarry Smith 
15403a40ed3dSBarry Smith   PetscFunctionBegin;
1541273d9f13SBarry Smith   ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr);
15421d55c564SBarry Smith 
1543b0a32e0cSBarry Smith   ierr            = PetscNew(Mat_MPIAIJ,&b);CHKERRQ(ierr);
1544b0a32e0cSBarry Smith   B->data         = (void*)b;
1545549d3d68SSatish Balay   ierr            = PetscMemzero(b,sizeof(Mat_MPIAIJ));CHKERRQ(ierr);
1546549d3d68SSatish Balay   ierr            = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
154744cd7ae7SLois Curfman McInnes   B->factor       = 0;
154844cd7ae7SLois Curfman McInnes   B->assembled    = PETSC_FALSE;
154990f02eecSBarry Smith   B->mapping      = 0;
1550d6dfbf8fSBarry Smith 
155147794344SBarry Smith   B->insertmode      = NOT_SET_VALUES;
15529eb4d147SSatish Balay   b->size            = size;
1553273d9f13SBarry Smith   ierr = MPI_Comm_rank(B->comm,&b->rank);CHKERRQ(ierr);
15541eb62cbbSBarry Smith 
1555273d9f13SBarry Smith   ierr = PetscSplitOwnership(B->comm,&B->m,&B->M);CHKERRQ(ierr);
1556273d9f13SBarry Smith   ierr = PetscSplitOwnership(B->comm,&B->n,&B->N);CHKERRQ(ierr);
15571eb62cbbSBarry Smith 
1558c7fcc2eaSBarry Smith   /* the information in the maps duplicates the information computed below, eventually
1559c7fcc2eaSBarry Smith      we should remove the duplicate information that is not contained in the maps */
15608a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->m,B->M,&B->rmap);CHKERRQ(ierr);
15618a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->n,B->N,&B->cmap);CHKERRQ(ierr);
1562c7fcc2eaSBarry Smith 
15631eb62cbbSBarry Smith   /* build local table of row and column ownerships */
1564b0a32e0cSBarry Smith   ierr = PetscMalloc(2*(b->size+2)*sizeof(int),&b->rowners);CHKERRQ(ierr);
1565b0a32e0cSBarry Smith   PetscLogObjectMemory(B,2*(b->size+2)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_MPIAIJ));
1566603f58a4SSatish Balay   b->cowners = b->rowners + b->size + 2;
1567273d9f13SBarry Smith   ierr = MPI_Allgather(&B->m,1,MPI_INT,b->rowners+1,1,MPI_INT,B->comm);CHKERRQ(ierr);
156844cd7ae7SLois Curfman McInnes   b->rowners[0] = 0;
156944cd7ae7SLois Curfman McInnes   for (i=2; i<=b->size; i++) {
157044cd7ae7SLois Curfman McInnes     b->rowners[i] += b->rowners[i-1];
15718a729477SBarry Smith   }
157244cd7ae7SLois Curfman McInnes   b->rstart = b->rowners[b->rank];
157344cd7ae7SLois Curfman McInnes   b->rend   = b->rowners[b->rank+1];
1574273d9f13SBarry Smith   ierr = MPI_Allgather(&B->n,1,MPI_INT,b->cowners+1,1,MPI_INT,B->comm);CHKERRQ(ierr);
157544cd7ae7SLois Curfman McInnes   b->cowners[0] = 0;
157644cd7ae7SLois Curfman McInnes   for (i=2; i<=b->size; i++) {
157744cd7ae7SLois Curfman McInnes     b->cowners[i] += b->cowners[i-1];
15788a729477SBarry Smith   }
157944cd7ae7SLois Curfman McInnes   b->cstart = b->cowners[b->rank];
158044cd7ae7SLois Curfman McInnes   b->cend   = b->cowners[b->rank+1];
15818a729477SBarry Smith 
15821eb62cbbSBarry Smith   /* build cache for off array entries formed */
15837e2c5f70SBarry Smith   ierr = MatStashCreate_Private(B->comm,1,&B->stash);CHKERRQ(ierr);
15847c922b88SBarry Smith   b->donotstash  = PETSC_FALSE;
158544cd7ae7SLois Curfman McInnes   b->colmap      = 0;
158644cd7ae7SLois Curfman McInnes   b->garray      = 0;
15877c922b88SBarry Smith   b->roworiented = PETSC_TRUE;
15888a729477SBarry Smith 
15891eb62cbbSBarry Smith   /* stuff used for matrix vector multiply */
15907c922b88SBarry Smith   b->lvec      = PETSC_NULL;
15917c922b88SBarry Smith   b->Mvctx     = PETSC_NULL;
15928a729477SBarry Smith 
15937a0afa10SBarry Smith   /* stuff for MatGetRow() */
159444cd7ae7SLois Curfman McInnes   b->rowindices   = 0;
159544cd7ae7SLois Curfman McInnes   b->rowvalues    = 0;
159644cd7ae7SLois Curfman McInnes   b->getrowactive = PETSC_FALSE;
15971a8d6bc9SBarry Smith 
1598f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
15992e8a6d31SBarry Smith                                      "MatStoreValues_MPIAIJ",
16000c97f09dSSatish Balay                                      MatStoreValues_MPIAIJ);CHKERRQ(ierr);
1601f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
16022e8a6d31SBarry Smith                                      "MatRetrieveValues_MPIAIJ",
16030c97f09dSSatish Balay                                      MatRetrieveValues_MPIAIJ);CHKERRQ(ierr);
1604f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetDiagonalBlock_C",
16055ef9f2a5SBarry Smith                                      "MatGetDiagonalBlock_MPIAIJ",
16060c97f09dSSatish Balay                                      MatGetDiagonalBlock_MPIAIJ);CHKERRQ(ierr);
16079b9367d5SHong Zhang 
16083a40ed3dSBarry Smith   PetscFunctionReturn(0);
1609d6dfbf8fSBarry Smith }
1610273d9f13SBarry Smith EXTERN_C_END
1611c74985f6SBarry Smith 
16124a2ae208SSatish Balay #undef __FUNCT__
16134a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_MPIAIJ"
16142e8a6d31SBarry Smith int MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
1615d6dfbf8fSBarry Smith {
1616d6dfbf8fSBarry Smith   Mat        mat;
1617416022c9SBarry Smith   Mat_MPIAIJ *a,*oldmat = (Mat_MPIAIJ*)matin->data;
161816d6aa21SSatish Balay   int        ierr;
1619d6dfbf8fSBarry Smith 
16203a40ed3dSBarry Smith   PetscFunctionBegin;
1621416022c9SBarry Smith   *newmat       = 0;
1622273d9f13SBarry Smith   ierr = MatCreate(matin->comm,matin->m,matin->n,matin->M,matin->N,&mat);CHKERRQ(ierr);
1623273d9f13SBarry Smith   ierr = MatSetType(mat,MATMPIAIJ);CHKERRQ(ierr);
1624273d9f13SBarry Smith   a    = (Mat_MPIAIJ*)mat->data;
1625549d3d68SSatish Balay   ierr              = PetscMemcpy(mat->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
1626d6dfbf8fSBarry Smith   mat->factor       = matin->factor;
1627c456f294SBarry Smith   mat->assembled    = PETSC_TRUE;
1628e7641de0SSatish Balay   mat->insertmode   = NOT_SET_VALUES;
1629273d9f13SBarry Smith   mat->preallocated = PETSC_TRUE;
1630d6dfbf8fSBarry Smith 
1631416022c9SBarry Smith   a->rstart       = oldmat->rstart;
1632416022c9SBarry Smith   a->rend         = oldmat->rend;
1633416022c9SBarry Smith   a->cstart       = oldmat->cstart;
1634416022c9SBarry Smith   a->cend         = oldmat->cend;
163517699dbbSLois Curfman McInnes   a->size         = oldmat->size;
163617699dbbSLois Curfman McInnes   a->rank         = oldmat->rank;
1637e7641de0SSatish Balay   a->donotstash   = oldmat->donotstash;
1638e7641de0SSatish Balay   a->roworiented  = oldmat->roworiented;
1639e7641de0SSatish Balay   a->rowindices   = 0;
1640bcd2baecSBarry Smith   a->rowvalues    = 0;
1641bcd2baecSBarry Smith   a->getrowactive = PETSC_FALSE;
1642d6dfbf8fSBarry Smith 
1643549d3d68SSatish Balay   ierr       = PetscMemcpy(a->rowners,oldmat->rowners,2*(a->size+2)*sizeof(int));CHKERRQ(ierr);
16448798bf22SSatish Balay   ierr       = MatStashCreate_Private(matin->comm,1,&mat->stash);CHKERRQ(ierr);
16452ee70a88SLois Curfman McInnes   if (oldmat->colmap) {
1646aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
16470f5bd95cSBarry Smith     ierr = PetscTableCreateCopy(oldmat->colmap,&a->colmap);CHKERRQ(ierr);
1648b1fc9764SSatish Balay #else
1649b0a32e0cSBarry Smith     ierr = PetscMalloc((mat->N)*sizeof(int),&a->colmap);CHKERRQ(ierr);
1650b0a32e0cSBarry Smith     PetscLogObjectMemory(mat,(mat->N)*sizeof(int));
1651273d9f13SBarry Smith     ierr      = PetscMemcpy(a->colmap,oldmat->colmap,(mat->N)*sizeof(int));CHKERRQ(ierr);
1652b1fc9764SSatish Balay #endif
1653416022c9SBarry Smith   } else a->colmap = 0;
16543f41c07dSBarry Smith   if (oldmat->garray) {
165516d6aa21SSatish Balay     int len;
1656273d9f13SBarry Smith     len  = oldmat->B->n;
1657b0a32e0cSBarry Smith     ierr = PetscMalloc((len+1)*sizeof(int),&a->garray);CHKERRQ(ierr);
1658b0a32e0cSBarry Smith     PetscLogObjectMemory(mat,len*sizeof(int));
1659549d3d68SSatish Balay     if (len) { ierr = PetscMemcpy(a->garray,oldmat->garray,len*sizeof(int));CHKERRQ(ierr); }
1660416022c9SBarry Smith   } else a->garray = 0;
1661d6dfbf8fSBarry Smith 
1662416022c9SBarry Smith   ierr =  VecDuplicate(oldmat->lvec,&a->lvec);CHKERRQ(ierr);
1663b0a32e0cSBarry Smith   PetscLogObjectParent(mat,a->lvec);
1664a56f8943SBarry Smith   ierr =  VecScatterCopy(oldmat->Mvctx,&a->Mvctx);CHKERRQ(ierr);
1665b0a32e0cSBarry Smith   PetscLogObjectParent(mat,a->Mvctx);
16662e8a6d31SBarry Smith   ierr =  MatDuplicate(oldmat->A,cpvalues,&a->A);CHKERRQ(ierr);
1667b0a32e0cSBarry Smith   PetscLogObjectParent(mat,a->A);
16682e8a6d31SBarry Smith   ierr =  MatDuplicate(oldmat->B,cpvalues,&a->B);CHKERRQ(ierr);
1669b0a32e0cSBarry Smith   PetscLogObjectParent(mat,a->B);
1670b0a32e0cSBarry Smith   ierr = PetscFListDuplicate(matin->qlist,&mat->qlist);CHKERRQ(ierr);
16718a729477SBarry Smith   *newmat = mat;
16723a40ed3dSBarry Smith   PetscFunctionReturn(0);
16738a729477SBarry Smith }
1674416022c9SBarry Smith 
1675e090d566SSatish Balay #include "petscsys.h"
1676416022c9SBarry Smith 
1677273d9f13SBarry Smith EXTERN_C_BEGIN
16784a2ae208SSatish Balay #undef __FUNCT__
16794a2ae208SSatish Balay #define __FUNCT__ "MatLoad_MPIAIJ"
1680b0a32e0cSBarry Smith int MatLoad_MPIAIJ(PetscViewer viewer,MatType type,Mat *newmat)
1681416022c9SBarry Smith {
1682d65a2f8fSBarry Smith   Mat          A;
168387828ca2SBarry Smith   PetscScalar  *vals,*svals;
168419bcc07fSBarry Smith   MPI_Comm     comm = ((PetscObject)viewer)->comm;
1685416022c9SBarry Smith   MPI_Status   status;
16863a40ed3dSBarry Smith   int          i,nz,ierr,j,rstart,rend,fd;
168717699dbbSLois Curfman McInnes   int          header[4],rank,size,*rowlengths = 0,M,N,m,*rowners,maxnz,*cols;
1688d65a2f8fSBarry Smith   int          *ourlens,*sndcounts = 0,*procsnz = 0,*offlens,jj,*mycols,*smycols;
1689b362ba68SBarry Smith   int          tag = ((PetscObject)viewer)->tag,cend,cstart,n;
1690416022c9SBarry Smith 
16913a40ed3dSBarry Smith   PetscFunctionBegin;
16921dab6e02SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
16931dab6e02SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
169417699dbbSLois Curfman McInnes   if (!rank) {
1695b0a32e0cSBarry Smith     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
16960752156aSBarry Smith     ierr = PetscBinaryRead(fd,(char *)header,4,PETSC_INT);CHKERRQ(ierr);
1697552e946dSBarry Smith     if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
1698d64ed03dSBarry Smith     if (header[3] < 0) {
169929bbc08cSBarry Smith       SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix in special format on disk, cannot load as MPIAIJ");
1700d64ed03dSBarry Smith     }
17016c5fab8fSBarry Smith   }
17026c5fab8fSBarry Smith 
1703ca161407SBarry Smith   ierr = MPI_Bcast(header+1,3,MPI_INT,0,comm);CHKERRQ(ierr);
1704416022c9SBarry Smith   M = header[1]; N = header[2];
1705416022c9SBarry Smith   /* determine ownership of all rows */
170617699dbbSLois Curfman McInnes   m = M/size + ((M % size) > rank);
1707b0a32e0cSBarry Smith   ierr = PetscMalloc((size+2)*sizeof(int),&rowners);CHKERRQ(ierr);
1708ca161407SBarry Smith   ierr = MPI_Allgather(&m,1,MPI_INT,rowners+1,1,MPI_INT,comm);CHKERRQ(ierr);
1709416022c9SBarry Smith   rowners[0] = 0;
171017699dbbSLois Curfman McInnes   for (i=2; i<=size; i++) {
1711416022c9SBarry Smith     rowners[i] += rowners[i-1];
1712416022c9SBarry Smith   }
171317699dbbSLois Curfman McInnes   rstart = rowners[rank];
171417699dbbSLois Curfman McInnes   rend   = rowners[rank+1];
1715416022c9SBarry Smith 
1716416022c9SBarry Smith   /* distribute row lengths to all processors */
1717b0a32e0cSBarry Smith   ierr    = PetscMalloc(2*(rend-rstart+1)*sizeof(int),&ourlens);CHKERRQ(ierr);
1718416022c9SBarry Smith   offlens = ourlens + (rend-rstart);
171917699dbbSLois Curfman McInnes   if (!rank) {
1720b0a32e0cSBarry Smith     ierr = PetscMalloc(M*sizeof(int),&rowlengths);CHKERRQ(ierr);
17210752156aSBarry Smith     ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
1722b0a32e0cSBarry Smith     ierr = PetscMalloc(size*sizeof(int),&sndcounts);CHKERRQ(ierr);
172317699dbbSLois Curfman McInnes     for (i=0; i<size; i++) sndcounts[i] = rowners[i+1] - rowners[i];
1724ca161407SBarry Smith     ierr = MPI_Scatterv(rowlengths,sndcounts,rowners,MPI_INT,ourlens,rend-rstart,MPI_INT,0,comm);CHKERRQ(ierr);
1725606d414cSSatish Balay     ierr = PetscFree(sndcounts);CHKERRQ(ierr);
17263a40ed3dSBarry Smith   } else {
1727ca161407SBarry Smith     ierr = MPI_Scatterv(0,0,0,MPI_INT,ourlens,rend-rstart,MPI_INT,0,comm);CHKERRQ(ierr);
1728416022c9SBarry Smith   }
1729416022c9SBarry Smith 
173017699dbbSLois Curfman McInnes   if (!rank) {
1731416022c9SBarry Smith     /* calculate the number of nonzeros on each processor */
1732b0a32e0cSBarry Smith     ierr = PetscMalloc(size*sizeof(int),&procsnz);CHKERRQ(ierr);
1733549d3d68SSatish Balay     ierr = PetscMemzero(procsnz,size*sizeof(int));CHKERRQ(ierr);
173417699dbbSLois Curfman McInnes     for (i=0; i<size; i++) {
1735416022c9SBarry Smith       for (j=rowners[i]; j< rowners[i+1]; j++) {
1736416022c9SBarry Smith         procsnz[i] += rowlengths[j];
1737416022c9SBarry Smith       }
1738416022c9SBarry Smith     }
1739606d414cSSatish Balay     ierr = PetscFree(rowlengths);CHKERRQ(ierr);
1740416022c9SBarry Smith 
1741416022c9SBarry Smith     /* determine max buffer needed and allocate it */
1742416022c9SBarry Smith     maxnz = 0;
174317699dbbSLois Curfman McInnes     for (i=0; i<size; i++) {
17440452661fSBarry Smith       maxnz = PetscMax(maxnz,procsnz[i]);
1745416022c9SBarry Smith     }
1746b0a32e0cSBarry Smith     ierr = PetscMalloc(maxnz*sizeof(int),&cols);CHKERRQ(ierr);
1747416022c9SBarry Smith 
1748416022c9SBarry Smith     /* read in my part of the matrix column indices  */
1749416022c9SBarry Smith     nz   = procsnz[0];
1750b0a32e0cSBarry Smith     ierr = PetscMalloc(nz*sizeof(int),&mycols);CHKERRQ(ierr);
17510752156aSBarry Smith     ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr);
1752d65a2f8fSBarry Smith 
1753d65a2f8fSBarry Smith     /* read in every one elses and ship off */
175417699dbbSLois Curfman McInnes     for (i=1; i<size; i++) {
1755d65a2f8fSBarry Smith       nz   = procsnz[i];
17560752156aSBarry Smith       ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr);
1757ca161407SBarry Smith       ierr = MPI_Send(cols,nz,MPI_INT,i,tag,comm);CHKERRQ(ierr);
1758d65a2f8fSBarry Smith     }
1759606d414cSSatish Balay     ierr = PetscFree(cols);CHKERRQ(ierr);
17603a40ed3dSBarry Smith   } else {
1761416022c9SBarry Smith     /* determine buffer space needed for message */
1762416022c9SBarry Smith     nz = 0;
1763416022c9SBarry Smith     for (i=0; i<m; i++) {
1764416022c9SBarry Smith       nz += ourlens[i];
1765416022c9SBarry Smith     }
1766b0a32e0cSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(int),&mycols);CHKERRQ(ierr);
1767416022c9SBarry Smith 
1768416022c9SBarry Smith     /* receive message of column indices*/
1769ca161407SBarry Smith     ierr = MPI_Recv(mycols,nz,MPI_INT,0,tag,comm,&status);CHKERRQ(ierr);
1770ca161407SBarry Smith     ierr = MPI_Get_count(&status,MPI_INT,&maxnz);CHKERRQ(ierr);
177129bbc08cSBarry Smith     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
1772416022c9SBarry Smith   }
1773416022c9SBarry Smith 
1774b362ba68SBarry Smith   /* determine column ownership if matrix is not square */
1775b362ba68SBarry Smith   if (N != M) {
1776b362ba68SBarry Smith     n      = N/size + ((N % size) > rank);
1777b362ba68SBarry Smith     ierr   = MPI_Scan(&n,&cend,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr);
1778b362ba68SBarry Smith     cstart = cend - n;
1779b362ba68SBarry Smith   } else {
1780b362ba68SBarry Smith     cstart = rstart;
1781b362ba68SBarry Smith     cend   = rend;
1782fb2e594dSBarry Smith     n      = cend - cstart;
1783b362ba68SBarry Smith   }
1784b362ba68SBarry Smith 
1785416022c9SBarry Smith   /* loop over local rows, determining number of off diagonal entries */
1786549d3d68SSatish Balay   ierr = PetscMemzero(offlens,m*sizeof(int));CHKERRQ(ierr);
1787416022c9SBarry Smith   jj = 0;
1788416022c9SBarry Smith   for (i=0; i<m; i++) {
1789416022c9SBarry Smith     for (j=0; j<ourlens[i]; j++) {
1790b362ba68SBarry Smith       if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++;
1791416022c9SBarry Smith       jj++;
1792416022c9SBarry Smith     }
1793416022c9SBarry Smith   }
1794d65a2f8fSBarry Smith 
1795d65a2f8fSBarry Smith   /* create our matrix */
1796416022c9SBarry Smith   for (i=0; i<m; i++) {
1797416022c9SBarry Smith     ourlens[i] -= offlens[i];
1798416022c9SBarry Smith   }
1799b362ba68SBarry Smith   ierr = MatCreateMPIAIJ(comm,m,n,M,N,0,ourlens,0,offlens,newmat);CHKERRQ(ierr);
1800d65a2f8fSBarry Smith   A = *newmat;
1801fb2e594dSBarry Smith   ierr = MatSetOption(A,MAT_COLUMNS_SORTED);CHKERRQ(ierr);
1802d65a2f8fSBarry Smith   for (i=0; i<m; i++) {
1803d65a2f8fSBarry Smith     ourlens[i] += offlens[i];
1804d65a2f8fSBarry Smith   }
1805416022c9SBarry Smith 
180617699dbbSLois Curfman McInnes   if (!rank) {
180787828ca2SBarry Smith     ierr = PetscMalloc(maxnz*sizeof(PetscScalar),&vals);CHKERRQ(ierr);
1808416022c9SBarry Smith 
1809416022c9SBarry Smith     /* read in my part of the matrix numerical values  */
1810416022c9SBarry Smith     nz   = procsnz[0];
18110752156aSBarry Smith     ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
1812d65a2f8fSBarry Smith 
1813d65a2f8fSBarry Smith     /* insert into matrix */
1814d65a2f8fSBarry Smith     jj      = rstart;
1815d65a2f8fSBarry Smith     smycols = mycols;
1816d65a2f8fSBarry Smith     svals   = vals;
1817d65a2f8fSBarry Smith     for (i=0; i<m; i++) {
1818d65a2f8fSBarry Smith       ierr = MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
1819d65a2f8fSBarry Smith       smycols += ourlens[i];
1820d65a2f8fSBarry Smith       svals   += ourlens[i];
1821d65a2f8fSBarry Smith       jj++;
1822416022c9SBarry Smith     }
1823416022c9SBarry Smith 
1824d65a2f8fSBarry Smith     /* read in other processors and ship out */
182517699dbbSLois Curfman McInnes     for (i=1; i<size; i++) {
1826416022c9SBarry Smith       nz   = procsnz[i];
18270752156aSBarry Smith       ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
1828ca161407SBarry Smith       ierr = MPI_Send(vals,nz,MPIU_SCALAR,i,A->tag,comm);CHKERRQ(ierr);
1829416022c9SBarry Smith     }
1830606d414cSSatish Balay     ierr = PetscFree(procsnz);CHKERRQ(ierr);
18313a40ed3dSBarry Smith   } else {
1832d65a2f8fSBarry Smith     /* receive numeric values */
183387828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&vals);CHKERRQ(ierr);
1834416022c9SBarry Smith 
1835d65a2f8fSBarry Smith     /* receive message of values*/
1836ca161407SBarry Smith     ierr = MPI_Recv(vals,nz,MPIU_SCALAR,0,A->tag,comm,&status);CHKERRQ(ierr);
1837ca161407SBarry Smith     ierr = MPI_Get_count(&status,MPIU_SCALAR,&maxnz);CHKERRQ(ierr);
183829bbc08cSBarry Smith     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
1839d65a2f8fSBarry Smith 
1840d65a2f8fSBarry Smith     /* insert into matrix */
1841d65a2f8fSBarry Smith     jj      = rstart;
1842d65a2f8fSBarry Smith     smycols = mycols;
1843d65a2f8fSBarry Smith     svals   = vals;
1844d65a2f8fSBarry Smith     for (i=0; i<m; i++) {
1845d65a2f8fSBarry Smith       ierr     = MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
1846d65a2f8fSBarry Smith       smycols += ourlens[i];
1847d65a2f8fSBarry Smith       svals   += ourlens[i];
1848d65a2f8fSBarry Smith       jj++;
1849d65a2f8fSBarry Smith     }
1850d65a2f8fSBarry Smith   }
1851606d414cSSatish Balay   ierr = PetscFree(ourlens);CHKERRQ(ierr);
1852606d414cSSatish Balay   ierr = PetscFree(vals);CHKERRQ(ierr);
1853606d414cSSatish Balay   ierr = PetscFree(mycols);CHKERRQ(ierr);
1854606d414cSSatish Balay   ierr = PetscFree(rowners);CHKERRQ(ierr);
1855d65a2f8fSBarry Smith 
18566d4a8577SBarry Smith   ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18576d4a8577SBarry Smith   ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18583a40ed3dSBarry Smith   PetscFunctionReturn(0);
1859416022c9SBarry Smith }
1860273d9f13SBarry Smith EXTERN_C_END
1861a0ff6018SBarry Smith 
18624a2ae208SSatish Balay #undef __FUNCT__
18634a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_MPIAIJ"
1864a0ff6018SBarry Smith /*
186529da9460SBarry Smith     Not great since it makes two copies of the submatrix, first an SeqAIJ
186629da9460SBarry Smith   in local and then by concatenating the local matrices the end result.
186729da9460SBarry Smith   Writing it directly would be much like MatGetSubMatrices_MPIAIJ()
1868a0ff6018SBarry Smith */
18697b2a1423SBarry Smith int MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,int csize,MatReuse call,Mat *newmat)
1870a0ff6018SBarry Smith {
187100e6dbe6SBarry Smith   int          ierr,i,m,n,rstart,row,rend,nz,*cwork,size,rank,j;
18727e2c5f70SBarry Smith   int          *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend;
1873fee21e36SBarry Smith   Mat          *local,M,Mreuse;
187487828ca2SBarry Smith   PetscScalar  *vwork,*aa;
187500e6dbe6SBarry Smith   MPI_Comm     comm = mat->comm;
187600e6dbe6SBarry Smith   Mat_SeqAIJ   *aij;
18777e2c5f70SBarry Smith 
1878a0ff6018SBarry Smith 
1879a0ff6018SBarry Smith   PetscFunctionBegin;
18801dab6e02SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
18811dab6e02SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
188200e6dbe6SBarry Smith 
1883fee21e36SBarry Smith   if (call ==  MAT_REUSE_MATRIX) {
1884fee21e36SBarry Smith     ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);CHKERRQ(ierr);
188529bbc08cSBarry Smith     if (!Mreuse) SETERRQ(1,"Submatrix passed in was not used before, cannot reuse");
1886fee21e36SBarry Smith     local = &Mreuse;
1887fee21e36SBarry Smith     ierr  = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&local);CHKERRQ(ierr);
1888fee21e36SBarry Smith   } else {
1889a0ff6018SBarry Smith     ierr   = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr);
1890fee21e36SBarry Smith     Mreuse = *local;
1891606d414cSSatish Balay     ierr   = PetscFree(local);CHKERRQ(ierr);
1892fee21e36SBarry Smith   }
1893a0ff6018SBarry Smith 
1894a0ff6018SBarry Smith   /*
1895a0ff6018SBarry Smith       m - number of local rows
1896a0ff6018SBarry Smith       n - number of columns (same on all processors)
1897a0ff6018SBarry Smith       rstart - first row in new global matrix generated
1898a0ff6018SBarry Smith   */
1899fee21e36SBarry Smith   ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr);
1900a0ff6018SBarry Smith   if (call == MAT_INITIAL_MATRIX) {
1901fee21e36SBarry Smith     aij = (Mat_SeqAIJ*)(Mreuse)->data;
190229bbc08cSBarry Smith     if (aij->indexshift) SETERRQ(PETSC_ERR_SUP,"No support for index shifted matrix");
190300e6dbe6SBarry Smith     ii  = aij->i;
190400e6dbe6SBarry Smith     jj  = aij->j;
190500e6dbe6SBarry Smith 
1906a0ff6018SBarry Smith     /*
190700e6dbe6SBarry Smith         Determine the number of non-zeros in the diagonal and off-diagonal
190800e6dbe6SBarry Smith         portions of the matrix in order to do correct preallocation
1909a0ff6018SBarry Smith     */
191000e6dbe6SBarry Smith 
191100e6dbe6SBarry Smith     /* first get start and end of "diagonal" columns */
19126a6a5d1dSBarry Smith     if (csize == PETSC_DECIDE) {
191300e6dbe6SBarry Smith       nlocal = n/size + ((n % size) > rank);
19146a6a5d1dSBarry Smith     } else {
19156a6a5d1dSBarry Smith       nlocal = csize;
19166a6a5d1dSBarry Smith     }
1917ca161407SBarry Smith     ierr   = MPI_Scan(&nlocal,&rend,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr);
191800e6dbe6SBarry Smith     rstart = rend - nlocal;
19196a6a5d1dSBarry Smith     if (rank == size - 1 && rend != n) {
192029bbc08cSBarry Smith       SETERRQ(1,"Local column sizes do not add up to total number of columns");
19216a6a5d1dSBarry Smith     }
192200e6dbe6SBarry Smith 
192300e6dbe6SBarry Smith     /* next, compute all the lengths */
1924b0a32e0cSBarry Smith     ierr  = PetscMalloc((2*m+1)*sizeof(int),&dlens);CHKERRQ(ierr);
192500e6dbe6SBarry Smith     olens = dlens + m;
192600e6dbe6SBarry Smith     for (i=0; i<m; i++) {
192700e6dbe6SBarry Smith       jend = ii[i+1] - ii[i];
192800e6dbe6SBarry Smith       olen = 0;
192900e6dbe6SBarry Smith       dlen = 0;
193000e6dbe6SBarry Smith       for (j=0; j<jend; j++) {
193100e6dbe6SBarry Smith         if (*jj < rstart || *jj >= rend) olen++;
193200e6dbe6SBarry Smith         else dlen++;
193300e6dbe6SBarry Smith         jj++;
193400e6dbe6SBarry Smith       }
193500e6dbe6SBarry Smith       olens[i] = olen;
193600e6dbe6SBarry Smith       dlens[i] = dlen;
193700e6dbe6SBarry Smith     }
19382d207970SBarry Smith     ierr = MatCreateMPIAIJ(comm,m,nlocal,PETSC_DECIDE,n,0,dlens,0,olens,&M);CHKERRQ(ierr);
1939606d414cSSatish Balay     ierr = PetscFree(dlens);CHKERRQ(ierr);
1940a0ff6018SBarry Smith   } else {
1941a0ff6018SBarry Smith     int ml,nl;
1942a0ff6018SBarry Smith 
1943a0ff6018SBarry Smith     M = *newmat;
1944a0ff6018SBarry Smith     ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr);
194529bbc08cSBarry Smith     if (ml != m) SETERRQ(PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
1946a0ff6018SBarry Smith     ierr = MatZeroEntries(M);CHKERRQ(ierr);
1947c48de900SBarry Smith     /*
1948c48de900SBarry Smith          The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
1949c48de900SBarry Smith        rather than the slower MatSetValues().
1950c48de900SBarry Smith     */
1951c48de900SBarry Smith     M->was_assembled = PETSC_TRUE;
1952c48de900SBarry Smith     M->assembled     = PETSC_FALSE;
1953a0ff6018SBarry Smith   }
1954a0ff6018SBarry Smith   ierr = MatGetOwnershipRange(M,&rstart,&rend);CHKERRQ(ierr);
1955fee21e36SBarry Smith   aij = (Mat_SeqAIJ*)(Mreuse)->data;
195629bbc08cSBarry Smith   if (aij->indexshift) SETERRQ(PETSC_ERR_SUP,"No support for index shifted matrix");
195700e6dbe6SBarry Smith   ii  = aij->i;
195800e6dbe6SBarry Smith   jj  = aij->j;
195900e6dbe6SBarry Smith   aa  = aij->a;
1960a0ff6018SBarry Smith   for (i=0; i<m; i++) {
1961a0ff6018SBarry Smith     row   = rstart + i;
196200e6dbe6SBarry Smith     nz    = ii[i+1] - ii[i];
196300e6dbe6SBarry Smith     cwork = jj;     jj += nz;
196400e6dbe6SBarry Smith     vwork = aa;     aa += nz;
19658c638d02SBarry Smith     ierr = MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr);
1966a0ff6018SBarry Smith   }
1967a0ff6018SBarry Smith 
1968a0ff6018SBarry Smith   ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1969a0ff6018SBarry Smith   ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1970a0ff6018SBarry Smith   *newmat = M;
1971fee21e36SBarry Smith 
1972fee21e36SBarry Smith   /* save submatrix used in processor for next request */
1973fee21e36SBarry Smith   if (call ==  MAT_INITIAL_MATRIX) {
1974fee21e36SBarry Smith     ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr);
1975fee21e36SBarry Smith     ierr = PetscObjectDereference((PetscObject)Mreuse);CHKERRQ(ierr);
1976fee21e36SBarry Smith   }
1977fee21e36SBarry Smith 
1978a0ff6018SBarry Smith   PetscFunctionReturn(0);
1979a0ff6018SBarry Smith }
1980273d9f13SBarry Smith 
19814a2ae208SSatish Balay #undef __FUNCT__
19824a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJSetPreallocation"
1983273d9f13SBarry Smith /*@C
1984273d9f13SBarry Smith    MatMPIAIJSetPreallocation - Creates a sparse parallel matrix in AIJ format
1985273d9f13SBarry Smith    (the default parallel PETSc format).  For good matrix assembly performance
1986273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameters
1987273d9f13SBarry Smith    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
1988273d9f13SBarry Smith    performance can be increased by more than a factor of 50.
1989273d9f13SBarry Smith 
1990273d9f13SBarry Smith    Collective on MPI_Comm
1991273d9f13SBarry Smith 
1992273d9f13SBarry Smith    Input Parameters:
1993273d9f13SBarry Smith +  A - the matrix
1994273d9f13SBarry Smith .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
1995273d9f13SBarry Smith            (same value is used for all local rows)
1996273d9f13SBarry Smith .  d_nnz - array containing the number of nonzeros in the various rows of the
1997273d9f13SBarry Smith            DIAGONAL portion of the local submatrix (possibly different for each row)
1998273d9f13SBarry Smith            or PETSC_NULL, if d_nz is used to specify the nonzero structure.
1999273d9f13SBarry Smith            The size of this array is equal to the number of local rows, i.e 'm'.
2000273d9f13SBarry Smith            You must leave room for the diagonal entry even if it is zero.
2001273d9f13SBarry Smith .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
2002273d9f13SBarry Smith            submatrix (same value is used for all local rows).
2003273d9f13SBarry Smith -  o_nnz - array containing the number of nonzeros in the various rows of the
2004273d9f13SBarry Smith            OFF-DIAGONAL portion of the local submatrix (possibly different for
2005273d9f13SBarry Smith            each row) or PETSC_NULL, if o_nz is used to specify the nonzero
2006273d9f13SBarry Smith            structure. The size of this array is equal to the number
2007273d9f13SBarry Smith            of local rows, i.e 'm'.
2008273d9f13SBarry Smith 
2009273d9f13SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
2010273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2011273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2012273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users manual for details.
2013273d9f13SBarry Smith 
2014273d9f13SBarry Smith    The user MUST specify either the local or global matrix dimensions
2015273d9f13SBarry Smith    (possibly both).
2016273d9f13SBarry Smith 
2017273d9f13SBarry Smith    The parallel matrix is partitioned such that the first m0 rows belong to
2018273d9f13SBarry Smith    process 0, the next m1 rows belong to process 1, the next m2 rows belong
2019273d9f13SBarry Smith    to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.
2020273d9f13SBarry Smith 
2021273d9f13SBarry Smith    The DIAGONAL portion of the local submatrix of a processor can be defined
2022273d9f13SBarry Smith    as the submatrix which is obtained by extraction the part corresponding
2023273d9f13SBarry Smith    to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the
2024273d9f13SBarry Smith    first row that belongs to the processor, and r2 is the last row belonging
2025273d9f13SBarry Smith    to the this processor. This is a square mxm matrix. The remaining portion
2026273d9f13SBarry Smith    of the local submatrix (mxN) constitute the OFF-DIAGONAL portion.
2027273d9f13SBarry Smith 
2028273d9f13SBarry Smith    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
2029273d9f13SBarry Smith 
2030273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible.
2031273d9f13SBarry Smith    We search for consecutive rows with the same nonzero structure, thereby
2032273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2033273d9f13SBarry Smith 
2034273d9f13SBarry Smith    Options Database Keys:
2035273d9f13SBarry Smith +  -mat_aij_no_inode  - Do not use inodes
2036273d9f13SBarry Smith .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2037273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2038273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2039273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2040273d9f13SBarry Smith 
2041273d9f13SBarry Smith    Example usage:
2042273d9f13SBarry Smith 
2043273d9f13SBarry Smith    Consider the following 8x8 matrix with 34 non-zero values, that is
2044273d9f13SBarry Smith    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
2045273d9f13SBarry Smith    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
2046273d9f13SBarry Smith    as follows:
2047273d9f13SBarry Smith 
2048273d9f13SBarry Smith .vb
2049273d9f13SBarry Smith             1  2  0  |  0  3  0  |  0  4
2050273d9f13SBarry Smith     Proc0   0  5  6  |  7  0  0  |  8  0
2051273d9f13SBarry Smith             9  0 10  | 11  0  0  | 12  0
2052273d9f13SBarry Smith     -------------------------------------
2053273d9f13SBarry Smith            13  0 14  | 15 16 17  |  0  0
2054273d9f13SBarry Smith     Proc1   0 18  0  | 19 20 21  |  0  0
2055273d9f13SBarry Smith             0  0  0  | 22 23  0  | 24  0
2056273d9f13SBarry Smith     -------------------------------------
2057273d9f13SBarry Smith     Proc2  25 26 27  |  0  0 28  | 29  0
2058273d9f13SBarry Smith            30  0  0  | 31 32 33  |  0 34
2059273d9f13SBarry Smith .ve
2060273d9f13SBarry Smith 
2061273d9f13SBarry Smith    This can be represented as a collection of submatrices as:
2062273d9f13SBarry Smith 
2063273d9f13SBarry Smith .vb
2064273d9f13SBarry Smith       A B C
2065273d9f13SBarry Smith       D E F
2066273d9f13SBarry Smith       G H I
2067273d9f13SBarry Smith .ve
2068273d9f13SBarry Smith 
2069273d9f13SBarry Smith    Where the submatrices A,B,C are owned by proc0, D,E,F are
2070273d9f13SBarry Smith    owned by proc1, G,H,I are owned by proc2.
2071273d9f13SBarry Smith 
2072273d9f13SBarry Smith    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2073273d9f13SBarry Smith    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2074273d9f13SBarry Smith    The 'M','N' parameters are 8,8, and have the same values on all procs.
2075273d9f13SBarry Smith 
2076273d9f13SBarry Smith    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
2077273d9f13SBarry Smith    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
2078273d9f13SBarry Smith    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
2079273d9f13SBarry Smith    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
2080273d9f13SBarry Smith    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
2081273d9f13SBarry Smith    matrix, ans [DF] as another SeqAIJ matrix.
2082273d9f13SBarry Smith 
2083273d9f13SBarry Smith    When d_nz, o_nz parameters are specified, d_nz storage elements are
2084273d9f13SBarry Smith    allocated for every row of the local diagonal submatrix, and o_nz
2085273d9f13SBarry Smith    storage locations are allocated for every row of the OFF-DIAGONAL submat.
2086273d9f13SBarry Smith    One way to choose d_nz and o_nz is to use the max nonzerors per local
2087273d9f13SBarry Smith    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
2088273d9f13SBarry Smith    In this case, the values of d_nz,o_nz are:
2089273d9f13SBarry Smith .vb
2090273d9f13SBarry Smith      proc0 : dnz = 2, o_nz = 2
2091273d9f13SBarry Smith      proc1 : dnz = 3, o_nz = 2
2092273d9f13SBarry Smith      proc2 : dnz = 1, o_nz = 4
2093273d9f13SBarry Smith .ve
2094273d9f13SBarry Smith    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
2095273d9f13SBarry Smith    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
2096273d9f13SBarry Smith    for proc3. i.e we are using 12+15+10=37 storage locations to store
2097273d9f13SBarry Smith    34 values.
2098273d9f13SBarry Smith 
2099273d9f13SBarry Smith    When d_nnz, o_nnz parameters are specified, the storage is specified
2100273d9f13SBarry Smith    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
2101273d9f13SBarry Smith    In the above case the values for d_nnz,o_nnz are:
2102273d9f13SBarry Smith .vb
2103273d9f13SBarry Smith      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
2104273d9f13SBarry Smith      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
2105273d9f13SBarry Smith      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
2106273d9f13SBarry Smith .ve
2107273d9f13SBarry Smith    Here the space allocated is sum of all the above values i.e 34, and
2108273d9f13SBarry Smith    hence pre-allocation is perfect.
2109273d9f13SBarry Smith 
2110273d9f13SBarry Smith    Level: intermediate
2111273d9f13SBarry Smith 
2112273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
2113273d9f13SBarry Smith 
2114273d9f13SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues()
2115273d9f13SBarry Smith @*/
2116273d9f13SBarry Smith int MatMPIAIJSetPreallocation(Mat B,int d_nz,int *d_nnz,int o_nz,int *o_nnz)
2117273d9f13SBarry Smith {
2118273d9f13SBarry Smith   Mat_MPIAIJ   *b;
2119273d9f13SBarry Smith   int          ierr,i;
2120273d9f13SBarry Smith   PetscTruth   flg2;
2121273d9f13SBarry Smith 
2122273d9f13SBarry Smith   PetscFunctionBegin;
2123273d9f13SBarry Smith   ierr = PetscTypeCompare((PetscObject)B,MATMPIAIJ,&flg2);CHKERRQ(ierr);
2124273d9f13SBarry Smith   if (!flg2) PetscFunctionReturn(0);
2125273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
2126435da068SBarry Smith   if (d_nz == PETSC_DEFAULT || d_nz == PETSC_DECIDE) d_nz = 5;
2127435da068SBarry Smith   if (o_nz == PETSC_DEFAULT || o_nz == PETSC_DECIDE) o_nz = 2;
2128435da068SBarry Smith   if (d_nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"d_nz cannot be less than 0: value %d",d_nz);
2129435da068SBarry Smith   if (o_nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"o_nz cannot be less than 0: value %d",o_nz);
2130273d9f13SBarry Smith   if (d_nnz) {
2131273d9f13SBarry Smith     for (i=0; i<B->m; i++) {
2132273d9f13SBarry 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]);
2133273d9f13SBarry Smith     }
2134273d9f13SBarry Smith   }
2135273d9f13SBarry Smith   if (o_nnz) {
2136273d9f13SBarry Smith     for (i=0; i<B->m; i++) {
2137273d9f13SBarry 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]);
2138273d9f13SBarry Smith     }
2139273d9f13SBarry Smith   }
2140273d9f13SBarry Smith   b = (Mat_MPIAIJ*)B->data;
2141273d9f13SBarry Smith 
2142273d9f13SBarry Smith   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,B->m,B->n,d_nz,d_nnz,&b->A);CHKERRQ(ierr);
2143b0a32e0cSBarry Smith   PetscLogObjectParent(B,b->A);
2144273d9f13SBarry Smith   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,B->m,B->N,o_nz,o_nnz,&b->B);CHKERRQ(ierr);
2145b0a32e0cSBarry Smith   PetscLogObjectParent(B,b->B);
2146273d9f13SBarry Smith 
2147273d9f13SBarry Smith   PetscFunctionReturn(0);
2148273d9f13SBarry Smith }
2149273d9f13SBarry Smith 
21504a2ae208SSatish Balay #undef __FUNCT__
21514a2ae208SSatish Balay #define __FUNCT__ "MatCreateMPIAIJ"
2152273d9f13SBarry Smith /*@C
2153273d9f13SBarry Smith    MatCreateMPIAIJ - Creates a sparse parallel matrix in AIJ format
2154273d9f13SBarry Smith    (the default parallel PETSc format).  For good matrix assembly performance
2155273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameters
2156273d9f13SBarry Smith    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
2157273d9f13SBarry Smith    performance can be increased by more than a factor of 50.
2158273d9f13SBarry Smith 
2159273d9f13SBarry Smith    Collective on MPI_Comm
2160273d9f13SBarry Smith 
2161273d9f13SBarry Smith    Input Parameters:
2162273d9f13SBarry Smith +  comm - MPI communicator
2163273d9f13SBarry Smith .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
2164273d9f13SBarry Smith            This value should be the same as the local size used in creating the
2165273d9f13SBarry Smith            y vector for the matrix-vector product y = Ax.
2166273d9f13SBarry Smith .  n - This value should be the same as the local size used in creating the
2167273d9f13SBarry Smith        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
2168273d9f13SBarry Smith        calculated if N is given) For square matrices n is almost always m.
2169273d9f13SBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
2170273d9f13SBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
2171273d9f13SBarry Smith .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
2172273d9f13SBarry Smith            (same value is used for all local rows)
2173273d9f13SBarry Smith .  d_nnz - array containing the number of nonzeros in the various rows of the
2174273d9f13SBarry Smith            DIAGONAL portion of the local submatrix (possibly different for each row)
2175273d9f13SBarry Smith            or PETSC_NULL, if d_nz is used to specify the nonzero structure.
2176273d9f13SBarry Smith            The size of this array is equal to the number of local rows, i.e 'm'.
2177273d9f13SBarry Smith            You must leave room for the diagonal entry even if it is zero.
2178273d9f13SBarry Smith .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
2179273d9f13SBarry Smith            submatrix (same value is used for all local rows).
2180273d9f13SBarry Smith -  o_nnz - array containing the number of nonzeros in the various rows of the
2181273d9f13SBarry Smith            OFF-DIAGONAL portion of the local submatrix (possibly different for
2182273d9f13SBarry Smith            each row) or PETSC_NULL, if o_nz is used to specify the nonzero
2183273d9f13SBarry Smith            structure. The size of this array is equal to the number
2184273d9f13SBarry Smith            of local rows, i.e 'm'.
2185273d9f13SBarry Smith 
2186273d9f13SBarry Smith    Output Parameter:
2187273d9f13SBarry Smith .  A - the matrix
2188273d9f13SBarry Smith 
2189273d9f13SBarry Smith    Notes:
2190273d9f13SBarry Smith    m,n,M,N parameters specify the size of the matrix, and its partitioning across
2191273d9f13SBarry Smith    processors, while d_nz,d_nnz,o_nz,o_nnz parameters specify the approximate
2192273d9f13SBarry Smith    storage requirements for this matrix.
2193273d9f13SBarry Smith 
2194273d9f13SBarry Smith    If PETSC_DECIDE or  PETSC_DETERMINE is used for a particular argument on one
2195273d9f13SBarry Smith    processor than it must be used on all processors that share the object for
2196273d9f13SBarry Smith    that argument.
2197273d9f13SBarry Smith 
2198273d9f13SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
2199273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2200273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2201273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users manual for details.
2202273d9f13SBarry Smith 
2203273d9f13SBarry Smith    The user MUST specify either the local or global matrix dimensions
2204273d9f13SBarry Smith    (possibly both).
2205273d9f13SBarry Smith 
2206273d9f13SBarry Smith    The parallel matrix is partitioned such that the first m0 rows belong to
2207273d9f13SBarry Smith    process 0, the next m1 rows belong to process 1, the next m2 rows belong
2208273d9f13SBarry Smith    to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.
2209273d9f13SBarry Smith 
2210273d9f13SBarry Smith    The DIAGONAL portion of the local submatrix of a processor can be defined
2211273d9f13SBarry Smith    as the submatrix which is obtained by extraction the part corresponding
2212273d9f13SBarry Smith    to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the
2213273d9f13SBarry Smith    first row that belongs to the processor, and r2 is the last row belonging
2214273d9f13SBarry Smith    to the this processor. This is a square mxm matrix. The remaining portion
2215273d9f13SBarry Smith    of the local submatrix (mxN) constitute the OFF-DIAGONAL portion.
2216273d9f13SBarry Smith 
2217273d9f13SBarry Smith    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
2218273d9f13SBarry Smith 
2219273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible.
2220273d9f13SBarry Smith    We search for consecutive rows with the same nonzero structure, thereby
2221273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2222273d9f13SBarry Smith 
2223273d9f13SBarry Smith    Options Database Keys:
2224273d9f13SBarry Smith +  -mat_aij_no_inode  - Do not use inodes
2225273d9f13SBarry Smith .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2226273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2227273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2228273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2229273d9f13SBarry Smith 
2230273d9f13SBarry Smith 
2231273d9f13SBarry Smith    Example usage:
2232273d9f13SBarry Smith 
2233273d9f13SBarry Smith    Consider the following 8x8 matrix with 34 non-zero values, that is
2234273d9f13SBarry Smith    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
2235273d9f13SBarry Smith    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
2236273d9f13SBarry Smith    as follows:
2237273d9f13SBarry Smith 
2238273d9f13SBarry Smith .vb
2239273d9f13SBarry Smith             1  2  0  |  0  3  0  |  0  4
2240273d9f13SBarry Smith     Proc0   0  5  6  |  7  0  0  |  8  0
2241273d9f13SBarry Smith             9  0 10  | 11  0  0  | 12  0
2242273d9f13SBarry Smith     -------------------------------------
2243273d9f13SBarry Smith            13  0 14  | 15 16 17  |  0  0
2244273d9f13SBarry Smith     Proc1   0 18  0  | 19 20 21  |  0  0
2245273d9f13SBarry Smith             0  0  0  | 22 23  0  | 24  0
2246273d9f13SBarry Smith     -------------------------------------
2247273d9f13SBarry Smith     Proc2  25 26 27  |  0  0 28  | 29  0
2248273d9f13SBarry Smith            30  0  0  | 31 32 33  |  0 34
2249273d9f13SBarry Smith .ve
2250273d9f13SBarry Smith 
2251273d9f13SBarry Smith    This can be represented as a collection of submatrices as:
2252273d9f13SBarry Smith 
2253273d9f13SBarry Smith .vb
2254273d9f13SBarry Smith       A B C
2255273d9f13SBarry Smith       D E F
2256273d9f13SBarry Smith       G H I
2257273d9f13SBarry Smith .ve
2258273d9f13SBarry Smith 
2259273d9f13SBarry Smith    Where the submatrices A,B,C are owned by proc0, D,E,F are
2260273d9f13SBarry Smith    owned by proc1, G,H,I are owned by proc2.
2261273d9f13SBarry Smith 
2262273d9f13SBarry Smith    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2263273d9f13SBarry Smith    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2264273d9f13SBarry Smith    The 'M','N' parameters are 8,8, and have the same values on all procs.
2265273d9f13SBarry Smith 
2266273d9f13SBarry Smith    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
2267273d9f13SBarry Smith    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
2268273d9f13SBarry Smith    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
2269273d9f13SBarry Smith    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
2270273d9f13SBarry Smith    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
2271273d9f13SBarry Smith    matrix, ans [DF] as another SeqAIJ matrix.
2272273d9f13SBarry Smith 
2273273d9f13SBarry Smith    When d_nz, o_nz parameters are specified, d_nz storage elements are
2274273d9f13SBarry Smith    allocated for every row of the local diagonal submatrix, and o_nz
2275273d9f13SBarry Smith    storage locations are allocated for every row of the OFF-DIAGONAL submat.
2276273d9f13SBarry Smith    One way to choose d_nz and o_nz is to use the max nonzerors per local
2277273d9f13SBarry Smith    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
2278273d9f13SBarry Smith    In this case, the values of d_nz,o_nz are:
2279273d9f13SBarry Smith .vb
2280273d9f13SBarry Smith      proc0 : dnz = 2, o_nz = 2
2281273d9f13SBarry Smith      proc1 : dnz = 3, o_nz = 2
2282273d9f13SBarry Smith      proc2 : dnz = 1, o_nz = 4
2283273d9f13SBarry Smith .ve
2284273d9f13SBarry Smith    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
2285273d9f13SBarry Smith    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
2286273d9f13SBarry Smith    for proc3. i.e we are using 12+15+10=37 storage locations to store
2287273d9f13SBarry Smith    34 values.
2288273d9f13SBarry Smith 
2289273d9f13SBarry Smith    When d_nnz, o_nnz parameters are specified, the storage is specified
2290273d9f13SBarry Smith    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
2291273d9f13SBarry Smith    In the above case the values for d_nnz,o_nnz are:
2292273d9f13SBarry Smith .vb
2293273d9f13SBarry Smith      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
2294273d9f13SBarry Smith      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
2295273d9f13SBarry Smith      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
2296273d9f13SBarry Smith .ve
2297273d9f13SBarry Smith    Here the space allocated is sum of all the above values i.e 34, and
2298273d9f13SBarry Smith    hence pre-allocation is perfect.
2299273d9f13SBarry Smith 
2300273d9f13SBarry Smith    Level: intermediate
2301273d9f13SBarry Smith 
2302273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
2303273d9f13SBarry Smith 
2304273d9f13SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues()
2305273d9f13SBarry Smith @*/
2306273d9f13SBarry 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)
2307273d9f13SBarry Smith {
2308273d9f13SBarry Smith   int ierr,size;
2309273d9f13SBarry Smith 
2310273d9f13SBarry Smith   PetscFunctionBegin;
2311273d9f13SBarry Smith   ierr = MatCreate(comm,m,n,M,N,A);CHKERRQ(ierr);
2312273d9f13SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
2313273d9f13SBarry Smith   if (size > 1) {
2314273d9f13SBarry Smith     ierr = MatSetType(*A,MATMPIAIJ);CHKERRQ(ierr);
2315273d9f13SBarry Smith     ierr = MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr);
2316273d9f13SBarry Smith   } else {
2317273d9f13SBarry Smith     ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2318273d9f13SBarry Smith     ierr = MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);CHKERRQ(ierr);
2319273d9f13SBarry Smith   }
2320273d9f13SBarry Smith   PetscFunctionReturn(0);
2321273d9f13SBarry Smith }
2322195d93cdSBarry Smith 
23234a2ae208SSatish Balay #undef __FUNCT__
23244a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJGetSeqAIJ"
2325195d93cdSBarry Smith int MatMPIAIJGetSeqAIJ(Mat A,Mat *Ad,Mat *Ao,int **colmap)
2326195d93cdSBarry Smith {
2327195d93cdSBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;
2328195d93cdSBarry Smith   PetscFunctionBegin;
2329195d93cdSBarry Smith   *Ad     = a->A;
2330195d93cdSBarry Smith   *Ao     = a->B;
2331195d93cdSBarry Smith   *colmap = a->garray;
2332195d93cdSBarry Smith   PetscFunctionReturn(0);
2333195d93cdSBarry Smith }
2334a2243be0SBarry Smith 
2335a2243be0SBarry Smith #undef __FUNCT__
2336a2243be0SBarry Smith #define __FUNCT__ "MatSetColoring_MPIAIJ"
2337a2243be0SBarry Smith int MatSetColoring_MPIAIJ(Mat A,ISColoring coloring)
2338a2243be0SBarry Smith {
2339a2243be0SBarry Smith   int        ierr;
2340a2243be0SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2341a2243be0SBarry Smith 
2342a2243be0SBarry Smith   PetscFunctionBegin;
2343a2243be0SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
2344a2243be0SBarry Smith     int        *allcolors,*colors,i;
2345a2243be0SBarry Smith     ISColoring ocoloring;
2346a2243be0SBarry Smith 
2347a2243be0SBarry Smith     /* set coloring for diagonal portion */
2348a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->A,coloring);CHKERRQ(ierr);
2349a2243be0SBarry Smith 
2350a2243be0SBarry Smith     /* set coloring for off-diagonal portion */
2351a2243be0SBarry Smith     ierr = ISAllGatherIndices(A->comm,coloring->n,coloring->colors,PETSC_NULL,&allcolors);CHKERRQ(ierr);
2352a2243be0SBarry Smith     ierr = PetscMalloc((a->B->n+1)*sizeof(int),&colors);CHKERRQ(ierr);
2353a2243be0SBarry Smith     for (i=0; i<a->B->n; i++) {
2354a2243be0SBarry Smith       colors[i] = allcolors[a->garray[i]];
2355a2243be0SBarry Smith     }
2356a2243be0SBarry Smith     ierr = PetscFree(allcolors);CHKERRQ(ierr);
2357a2243be0SBarry Smith     ierr = ISColoringCreate(MPI_COMM_SELF,a->B->n,colors,&ocoloring);CHKERRQ(ierr);
2358a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr);
2359a2243be0SBarry Smith     ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr);
2360a2243be0SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
2361a2243be0SBarry Smith     int        *colors,i,*larray;
2362a2243be0SBarry Smith     ISColoring ocoloring;
2363a2243be0SBarry Smith 
2364a2243be0SBarry Smith     /* set coloring for diagonal portion */
2365a2243be0SBarry Smith     ierr = PetscMalloc((a->A->n+1)*sizeof(int),&larray);CHKERRQ(ierr);
2366a2243be0SBarry Smith     for (i=0; i<a->A->n; i++) {
2367a2243be0SBarry Smith       larray[i] = i + a->cstart;
2368a2243be0SBarry Smith     }
2369a2243be0SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->A->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
2370a2243be0SBarry Smith     ierr = PetscMalloc((a->A->n+1)*sizeof(int),&colors);CHKERRQ(ierr);
2371a2243be0SBarry Smith     for (i=0; i<a->A->n; i++) {
2372a2243be0SBarry Smith       colors[i] = coloring->colors[larray[i]];
2373a2243be0SBarry Smith     }
2374a2243be0SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
237512c595b3SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,a->A->n,colors,&ocoloring);CHKERRQ(ierr);
2376a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->A,ocoloring);CHKERRQ(ierr);
2377a2243be0SBarry Smith     ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr);
2378a2243be0SBarry Smith 
2379a2243be0SBarry Smith     /* set coloring for off-diagonal portion */
2380a2243be0SBarry Smith     ierr = PetscMalloc((a->B->n+1)*sizeof(int),&larray);CHKERRQ(ierr);
2381a2243be0SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->B->n,a->garray,PETSC_NULL,larray);CHKERRQ(ierr);
2382a2243be0SBarry Smith     ierr = PetscMalloc((a->B->n+1)*sizeof(int),&colors);CHKERRQ(ierr);
2383a2243be0SBarry Smith     for (i=0; i<a->B->n; i++) {
2384a2243be0SBarry Smith       colors[i] = coloring->colors[larray[i]];
2385a2243be0SBarry Smith     }
2386a2243be0SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
2387a2243be0SBarry Smith     ierr = ISColoringCreate(MPI_COMM_SELF,a->B->n,colors,&ocoloring);CHKERRQ(ierr);
2388a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr);
2389a2243be0SBarry Smith     ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr);
2390a2243be0SBarry Smith   } else {
2391a2243be0SBarry Smith     SETERRQ1(1,"No support ISColoringType %d",coloring->ctype);
2392a2243be0SBarry Smith   }
2393a2243be0SBarry Smith 
2394a2243be0SBarry Smith   PetscFunctionReturn(0);
2395a2243be0SBarry Smith }
2396a2243be0SBarry Smith 
2397a2243be0SBarry Smith #undef __FUNCT__
2398779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdic_MPIAIJ"
2399779c1a83SBarry Smith int MatSetValuesAdic_MPIAIJ(Mat A,void *advalues)
2400a2243be0SBarry Smith {
2401a2243be0SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2402a2243be0SBarry Smith   int        ierr;
2403a2243be0SBarry Smith 
2404a2243be0SBarry Smith   PetscFunctionBegin;
2405779c1a83SBarry Smith   ierr = MatSetValuesAdic_SeqAIJ(a->A,advalues);CHKERRQ(ierr);
2406779c1a83SBarry Smith   ierr = MatSetValuesAdic_SeqAIJ(a->B,advalues);CHKERRQ(ierr);
2407779c1a83SBarry Smith   PetscFunctionReturn(0);
2408779c1a83SBarry Smith }
2409779c1a83SBarry Smith 
2410779c1a83SBarry Smith #undef __FUNCT__
2411779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdifor_MPIAIJ"
2412779c1a83SBarry Smith int MatSetValuesAdifor_MPIAIJ(Mat A,int nl,void *advalues)
2413779c1a83SBarry Smith {
2414779c1a83SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2415779c1a83SBarry Smith   int        ierr;
2416779c1a83SBarry Smith 
2417779c1a83SBarry Smith   PetscFunctionBegin;
2418779c1a83SBarry Smith   ierr = MatSetValuesAdifor_SeqAIJ(a->A,nl,advalues);CHKERRQ(ierr);
2419779c1a83SBarry Smith   ierr = MatSetValuesAdifor_SeqAIJ(a->B,nl,advalues);CHKERRQ(ierr);
2420a2243be0SBarry Smith   PetscFunctionReturn(0);
2421a2243be0SBarry Smith }
2422