xref: /petsc/src/mat/impls/aij/mpi/mpiaij.c (revision 268466fb80fbd96f1409ea3c48e598fcc3189700)
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);
13bc5ccf88SSatish Balay 
140f5bd95cSBarry Smith /*
150f5bd95cSBarry Smith   Local utility routine that creates a mapping from the global column
169e25ed09SBarry Smith number to the local number in the off-diagonal part of the local
170f5bd95cSBarry Smith storage of the matrix.  When PETSC_USE_CTABLE is used this is scalable at
180f5bd95cSBarry Smith a slightly higher hash table cost; without it it is not scalable (each processor
190f5bd95cSBarry Smith has an order N integer array but is fast to acess.
209e25ed09SBarry Smith */
214a2ae208SSatish Balay #undef __FUNCT__
224a2ae208SSatish Balay #define __FUNCT__ "CreateColmap_MPIAIJ_Private"
230a198c4cSBarry Smith int CreateColmap_MPIAIJ_Private(Mat mat)
249e25ed09SBarry Smith {
2544a69424SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
26273d9f13SBarry Smith   int        n = aij->B->n,i,ierr;
27dbb450caSBarry Smith 
283a40ed3dSBarry Smith   PetscFunctionBegin;
29aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
30273d9f13SBarry Smith   ierr = PetscTableCreate(n,&aij->colmap);CHKERRQ(ierr);
31b1fc9764SSatish Balay   for (i=0; i<n; i++){
320f5bd95cSBarry Smith     ierr = PetscTableAdd(aij->colmap,aij->garray[i]+1,i+1);CHKERRQ(ierr);
33b1fc9764SSatish Balay   }
34b1fc9764SSatish Balay #else
35b0a32e0cSBarry Smith   ierr = PetscMalloc((mat->N+1)*sizeof(int),&aij->colmap);CHKERRQ(ierr);
36b0a32e0cSBarry Smith   PetscLogObjectMemory(mat,mat->N*sizeof(int));
37273d9f13SBarry Smith   ierr = PetscMemzero(aij->colmap,mat->N*sizeof(int));CHKERRQ(ierr);
38905e6a2fSBarry Smith   for (i=0; i<n; i++) aij->colmap[aij->garray[i]] = i+1;
39b1fc9764SSatish Balay #endif
403a40ed3dSBarry Smith   PetscFunctionReturn(0);
419e25ed09SBarry Smith }
429e25ed09SBarry Smith 
430520107fSSatish Balay #define CHUNKSIZE   15
4430770e4dSSatish Balay #define MatSetValues_SeqAIJ_A_Private(row,col,value,addv) \
450520107fSSatish Balay { \
460520107fSSatish Balay  \
470520107fSSatish Balay     rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift; \
4830770e4dSSatish Balay     rmax = aimax[row]; nrow = ailen[row];  \
49f5e9677aSSatish Balay     col1 = col - shift; \
50f5e9677aSSatish Balay      \
51ba4e3ef2SSatish Balay     low = 0; high = nrow; \
52ba4e3ef2SSatish Balay     while (high-low > 5) { \
53ba4e3ef2SSatish Balay       t = (low+high)/2; \
54ba4e3ef2SSatish Balay       if (rp[t] > col) high = t; \
55ba4e3ef2SSatish Balay       else             low  = t; \
56ba4e3ef2SSatish Balay     } \
572335bdd2SSatish Balay       for (_i=low; _i<high; _i++) { \
58f5e9677aSSatish Balay         if (rp[_i] > col1) break; \
59f5e9677aSSatish Balay         if (rp[_i] == col1) { \
600520107fSSatish Balay           if (addv == ADD_VALUES) ap[_i] += value;   \
610520107fSSatish Balay           else                  ap[_i] = value; \
6230770e4dSSatish Balay           goto a_noinsert; \
630520107fSSatish Balay         } \
640520107fSSatish Balay       }  \
6589280ab3SLois Curfman McInnes       if (nonew == 1) goto a_noinsert; \
6629bbc08cSBarry Smith       else if (nonew == -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero into matrix"); \
670520107fSSatish Balay       if (nrow >= rmax) { \
680520107fSSatish Balay         /* there is no extra room in row, therefore enlarge */ \
69273d9f13SBarry Smith         int    new_nz = ai[am] + CHUNKSIZE,len,*new_i,*new_j; \
7087828ca2SBarry Smith         PetscScalar *new_a; \
710520107fSSatish Balay  \
7229bbc08cSBarry Smith         if (nonew == -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix"); \
7389280ab3SLois Curfman McInnes  \
740520107fSSatish Balay         /* malloc new storage space */ \
7587828ca2SBarry Smith         len     = new_nz*(sizeof(int)+sizeof(PetscScalar))+(am+1)*sizeof(int); \
76b0a32e0cSBarry Smith         ierr    = PetscMalloc(len,&new_a);CHKERRQ(ierr); \
770520107fSSatish Balay         new_j   = (int*)(new_a + new_nz); \
780520107fSSatish Balay         new_i   = new_j + new_nz; \
790520107fSSatish Balay  \
800520107fSSatish Balay         /* copy over old data into new slots */ \
810520107fSSatish Balay         for (ii=0; ii<row+1; ii++) {new_i[ii] = ai[ii];} \
82273d9f13SBarry Smith         for (ii=row+1; ii<am+1; ii++) {new_i[ii] = ai[ii]+CHUNKSIZE;} \
83549d3d68SSatish Balay         ierr = PetscMemcpy(new_j,aj,(ai[row]+nrow+shift)*sizeof(int));CHKERRQ(ierr); \
840520107fSSatish Balay         len = (new_nz - CHUNKSIZE - ai[row] - nrow - shift); \
85549d3d68SSatish Balay         ierr = PetscMemcpy(new_j+ai[row]+shift+nrow+CHUNKSIZE,aj+ai[row]+shift+nrow, \
86549d3d68SSatish Balay                                                            len*sizeof(int));CHKERRQ(ierr); \
8787828ca2SBarry Smith         ierr = PetscMemcpy(new_a,aa,(ai[row]+nrow+shift)*sizeof(PetscScalar));CHKERRQ(ierr); \
88549d3d68SSatish Balay         ierr = PetscMemcpy(new_a+ai[row]+shift+nrow+CHUNKSIZE,aa+ai[row]+shift+nrow, \
8987828ca2SBarry Smith                                                            len*sizeof(PetscScalar));CHKERRQ(ierr);  \
900520107fSSatish Balay         /* free up old matrix storage */ \
91f5e9677aSSatish Balay  \
92606d414cSSatish Balay         ierr = PetscFree(a->a);CHKERRQ(ierr);  \
93606d414cSSatish Balay         if (!a->singlemalloc) { \
94606d414cSSatish Balay            ierr = PetscFree(a->i);CHKERRQ(ierr); \
95606d414cSSatish Balay            ierr = PetscFree(a->j);CHKERRQ(ierr); \
96606d414cSSatish Balay         } \
970520107fSSatish Balay         aa = a->a = new_a; ai = a->i = new_i; aj = a->j = new_j;  \
987c922b88SBarry Smith         a->singlemalloc = PETSC_TRUE; \
990520107fSSatish Balay  \
1000520107fSSatish Balay         rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift; \
10130770e4dSSatish Balay         rmax = aimax[row] = aimax[row] + CHUNKSIZE; \
10287828ca2SBarry Smith         PetscLogObjectMemory(A,CHUNKSIZE*(sizeof(int) + sizeof(PetscScalar))); \
1030520107fSSatish Balay         a->maxnz += CHUNKSIZE; \
1040520107fSSatish Balay         a->reallocs++; \
1050520107fSSatish Balay       } \
1060520107fSSatish Balay       N = nrow++ - 1; a->nz++; \
1070520107fSSatish Balay       /* shift up all the later entries in this row */ \
1080520107fSSatish Balay       for (ii=N; ii>=_i; ii--) { \
1090520107fSSatish Balay         rp[ii+1] = rp[ii]; \
1100520107fSSatish Balay         ap[ii+1] = ap[ii]; \
1110520107fSSatish Balay       } \
112f5e9677aSSatish Balay       rp[_i] = col1;  \
1130520107fSSatish Balay       ap[_i] = value;  \
11430770e4dSSatish Balay       a_noinsert: ; \
1150520107fSSatish Balay       ailen[row] = nrow; \
1160520107fSSatish Balay }
1170a198c4cSBarry Smith 
11830770e4dSSatish Balay #define MatSetValues_SeqAIJ_B_Private(row,col,value,addv) \
11930770e4dSSatish Balay { \
12030770e4dSSatish Balay  \
12130770e4dSSatish Balay     rp   = bj + bi[row] + shift; ap = ba + bi[row] + shift; \
12230770e4dSSatish Balay     rmax = bimax[row]; nrow = bilen[row];  \
12330770e4dSSatish Balay     col1 = col - shift; \
12430770e4dSSatish Balay      \
125ba4e3ef2SSatish Balay     low = 0; high = nrow; \
126ba4e3ef2SSatish Balay     while (high-low > 5) { \
127ba4e3ef2SSatish Balay       t = (low+high)/2; \
128ba4e3ef2SSatish Balay       if (rp[t] > col) high = t; \
129ba4e3ef2SSatish Balay       else             low  = t; \
130ba4e3ef2SSatish Balay     } \
1312335bdd2SSatish Balay        for (_i=low; _i<high; _i++) { \
13230770e4dSSatish Balay         if (rp[_i] > col1) break; \
13330770e4dSSatish Balay         if (rp[_i] == col1) { \
13430770e4dSSatish Balay           if (addv == ADD_VALUES) ap[_i] += value;   \
13530770e4dSSatish Balay           else                  ap[_i] = value; \
13630770e4dSSatish Balay           goto b_noinsert; \
13730770e4dSSatish Balay         } \
13830770e4dSSatish Balay       }  \
13989280ab3SLois Curfman McInnes       if (nonew == 1) goto b_noinsert; \
14029bbc08cSBarry Smith       else if (nonew == -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero into matrix"); \
14130770e4dSSatish Balay       if (nrow >= rmax) { \
14230770e4dSSatish Balay         /* there is no extra room in row, therefore enlarge */ \
143273d9f13SBarry Smith         int    new_nz = bi[bm] + CHUNKSIZE,len,*new_i,*new_j; \
14487828ca2SBarry Smith         PetscScalar *new_a; \
14530770e4dSSatish Balay  \
14629bbc08cSBarry Smith         if (nonew == -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix"); \
14789280ab3SLois Curfman McInnes  \
14830770e4dSSatish Balay         /* malloc new storage space */ \
14987828ca2SBarry Smith         len     = new_nz*(sizeof(int)+sizeof(PetscScalar))+(bm+1)*sizeof(int); \
150b0a32e0cSBarry Smith         ierr    = PetscMalloc(len,&new_a);CHKERRQ(ierr); \
15130770e4dSSatish Balay         new_j   = (int*)(new_a + new_nz); \
15230770e4dSSatish Balay         new_i   = new_j + new_nz; \
15330770e4dSSatish Balay  \
15430770e4dSSatish Balay         /* copy over old data into new slots */ \
15530770e4dSSatish Balay         for (ii=0; ii<row+1; ii++) {new_i[ii] = bi[ii];} \
156273d9f13SBarry Smith         for (ii=row+1; ii<bm+1; ii++) {new_i[ii] = bi[ii]+CHUNKSIZE;} \
157549d3d68SSatish Balay         ierr = PetscMemcpy(new_j,bj,(bi[row]+nrow+shift)*sizeof(int));CHKERRQ(ierr); \
15830770e4dSSatish Balay         len = (new_nz - CHUNKSIZE - bi[row] - nrow - shift); \
159549d3d68SSatish Balay         ierr = PetscMemcpy(new_j+bi[row]+shift+nrow+CHUNKSIZE,bj+bi[row]+shift+nrow, \
160549d3d68SSatish Balay                                                            len*sizeof(int));CHKERRQ(ierr); \
16187828ca2SBarry Smith         ierr = PetscMemcpy(new_a,ba,(bi[row]+nrow+shift)*sizeof(PetscScalar));CHKERRQ(ierr); \
162549d3d68SSatish Balay         ierr = PetscMemcpy(new_a+bi[row]+shift+nrow+CHUNKSIZE,ba+bi[row]+shift+nrow, \
16387828ca2SBarry Smith                                                            len*sizeof(PetscScalar));CHKERRQ(ierr);  \
16430770e4dSSatish Balay         /* free up old matrix storage */ \
16530770e4dSSatish Balay  \
166606d414cSSatish Balay         ierr = PetscFree(b->a);CHKERRQ(ierr);  \
167606d414cSSatish Balay         if (!b->singlemalloc) { \
168606d414cSSatish Balay           ierr = PetscFree(b->i);CHKERRQ(ierr); \
169606d414cSSatish Balay           ierr = PetscFree(b->j);CHKERRQ(ierr); \
170606d414cSSatish Balay         } \
17174c639caSSatish Balay         ba = b->a = new_a; bi = b->i = new_i; bj = b->j = new_j;  \
1727c922b88SBarry Smith         b->singlemalloc = PETSC_TRUE; \
17330770e4dSSatish Balay  \
17430770e4dSSatish Balay         rp   = bj + bi[row] + shift; ap = ba + bi[row] + shift; \
17530770e4dSSatish Balay         rmax = bimax[row] = bimax[row] + CHUNKSIZE; \
17687828ca2SBarry Smith         PetscLogObjectMemory(B,CHUNKSIZE*(sizeof(int) + sizeof(PetscScalar))); \
17774c639caSSatish Balay         b->maxnz += CHUNKSIZE; \
17874c639caSSatish Balay         b->reallocs++; \
17930770e4dSSatish Balay       } \
18074c639caSSatish Balay       N = nrow++ - 1; b->nz++; \
18130770e4dSSatish Balay       /* shift up all the later entries in this row */ \
18230770e4dSSatish Balay       for (ii=N; ii>=_i; ii--) { \
18330770e4dSSatish Balay         rp[ii+1] = rp[ii]; \
18430770e4dSSatish Balay         ap[ii+1] = ap[ii]; \
18530770e4dSSatish Balay       } \
18630770e4dSSatish Balay       rp[_i] = col1;  \
18730770e4dSSatish Balay       ap[_i] = value;  \
18830770e4dSSatish Balay       b_noinsert: ; \
18930770e4dSSatish Balay       bilen[row] = nrow; \
19030770e4dSSatish Balay }
19130770e4dSSatish Balay 
1924a2ae208SSatish Balay #undef __FUNCT__
1934a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_MPIAIJ"
194f15d580aSBarry Smith int MatSetValues_MPIAIJ(Mat mat,int m,const int im[],int n,const int in[],const PetscScalar v[],InsertMode addv)
1958a729477SBarry Smith {
19644a69424SLois Curfman McInnes   Mat_MPIAIJ   *aij = (Mat_MPIAIJ*)mat->data;
19787828ca2SBarry Smith   PetscScalar  value;
1981eb62cbbSBarry Smith   int          ierr,i,j,rstart = aij->rstart,rend = aij->rend;
1991eb62cbbSBarry Smith   int          cstart = aij->cstart,cend = aij->cend,row,col;
200273d9f13SBarry Smith   PetscTruth   roworiented = aij->roworiented;
2018a729477SBarry Smith 
2020520107fSSatish Balay   /* Some Variables required in the macro */
2034ee7247eSSatish Balay   Mat          A = aij->A;
2044ee7247eSSatish Balay   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
20530770e4dSSatish Balay   int          *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j;
20687828ca2SBarry Smith   PetscScalar  *aa = a->a;
207329f5518SBarry Smith   PetscTruth   ignorezeroentries = (((a->ignorezeroentries)&&(addv==ADD_VALUES))?PETSC_TRUE:PETSC_FALSE);
20830770e4dSSatish Balay   Mat          B = aij->B;
20930770e4dSSatish Balay   Mat_SeqAIJ   *b = (Mat_SeqAIJ*)B->data;
210273d9f13SBarry Smith   int          *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->m,am = aij->A->m;
21187828ca2SBarry Smith   PetscScalar  *ba = b->a;
21230770e4dSSatish Balay 
213ba4e3ef2SSatish Balay   int          *rp,ii,nrow,_i,rmax,N,col1,low,high,t;
214bfec09a0SHong Zhang   int          nonew = a->nonew,shift=0;
21587828ca2SBarry Smith   PetscScalar  *ap;
2164ee7247eSSatish Balay 
2173a40ed3dSBarry Smith   PetscFunctionBegin;
2188a729477SBarry Smith   for (i=0; i<m; i++) {
2195ef9f2a5SBarry Smith     if (im[i] < 0) continue;
220aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g)
221590ac198SBarry Smith     if (im[i] >= mat->M) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %d max %d",im[i],mat->M-1);
2220a198c4cSBarry Smith #endif
2234b0e389bSBarry Smith     if (im[i] >= rstart && im[i] < rend) {
2244b0e389bSBarry Smith       row = im[i] - rstart;
2251eb62cbbSBarry Smith       for (j=0; j<n; j++) {
2264b0e389bSBarry Smith         if (in[j] >= cstart && in[j] < cend){
2274b0e389bSBarry Smith           col = in[j] - cstart;
2284b0e389bSBarry Smith           if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
229329f5518SBarry Smith           if (ignorezeroentries && value == 0.0) continue;
23030770e4dSSatish Balay           MatSetValues_SeqAIJ_A_Private(row,col,value,addv);
2310520107fSSatish Balay           /* ierr = MatSetValues_SeqAIJ(aij->A,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */
232273d9f13SBarry Smith         } else if (in[j] < 0) continue;
233aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g)
234590ac198SBarry Smith         else if (in[j] >= mat->N) {SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %d max %d",in[j],mat->N-1);}
2350a198c4cSBarry Smith #endif
2361eb62cbbSBarry Smith         else {
237227d817aSBarry Smith           if (mat->was_assembled) {
238905e6a2fSBarry Smith             if (!aij->colmap) {
239905e6a2fSBarry Smith               ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
240905e6a2fSBarry Smith             }
241aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
2420f5bd95cSBarry Smith             ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr);
243fa46199cSSatish Balay 	    col--;
244b1fc9764SSatish Balay #else
245905e6a2fSBarry Smith             col = aij->colmap[in[j]] - 1;
246b1fc9764SSatish Balay #endif
247ec8511deSBarry Smith             if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
2482493cbb0SBarry Smith               ierr = DisAssemble_MPIAIJ(mat);CHKERRQ(ierr);
2494b0e389bSBarry Smith               col =  in[j];
2509bf004c3SSatish Balay               /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
251f9508a3cSSatish Balay               B = aij->B;
252f9508a3cSSatish Balay               b = (Mat_SeqAIJ*)B->data;
253f9508a3cSSatish Balay               bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j;
254f9508a3cSSatish Balay               ba = b->a;
255d6dfbf8fSBarry Smith             }
256c48de900SBarry Smith           } else col = in[j];
2574b0e389bSBarry Smith           if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
258329f5518SBarry Smith           if (ignorezeroentries && value == 0.0) continue;
25930770e4dSSatish Balay           MatSetValues_SeqAIJ_B_Private(row,col,value,addv);
26030770e4dSSatish Balay           /* ierr = MatSetValues_SeqAIJ(aij->B,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */
2611eb62cbbSBarry Smith         }
2621eb62cbbSBarry Smith       }
2635ef9f2a5SBarry Smith     } else {
26490f02eecSBarry Smith       if (!aij->donotstash) {
265d36fbae8SSatish Balay         if (roworiented) {
266329f5518SBarry Smith           if (ignorezeroentries && v[i*n] == 0.0) continue;
2678798bf22SSatish Balay           ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n);CHKERRQ(ierr);
268d36fbae8SSatish Balay         } else {
269329f5518SBarry Smith           if (ignorezeroentries && v[i] == 0.0) continue;
2708798bf22SSatish Balay           ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m);CHKERRQ(ierr);
2714b0e389bSBarry Smith         }
2721eb62cbbSBarry Smith       }
2738a729477SBarry Smith     }
27490f02eecSBarry Smith   }
2753a40ed3dSBarry Smith   PetscFunctionReturn(0);
2768a729477SBarry Smith }
2778a729477SBarry Smith 
2784a2ae208SSatish Balay #undef __FUNCT__
2794a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_MPIAIJ"
280f15d580aSBarry Smith int MatGetValues_MPIAIJ(Mat mat,int m,const int idxm[],int n,const int idxn[],PetscScalar v[])
281b49de8d1SLois Curfman McInnes {
282b49de8d1SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
283b49de8d1SLois Curfman McInnes   int        ierr,i,j,rstart = aij->rstart,rend = aij->rend;
284b49de8d1SLois Curfman McInnes   int        cstart = aij->cstart,cend = aij->cend,row,col;
285b49de8d1SLois Curfman McInnes 
2863a40ed3dSBarry Smith   PetscFunctionBegin;
287b49de8d1SLois Curfman McInnes   for (i=0; i<m; i++) {
288590ac198SBarry Smith     if (idxm[i] < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %d",idxm[i]);
289952d7e9fSSatish Balay     if (idxm[i] >= mat->M) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %d max %d",idxm[i],mat->M-1);
290b49de8d1SLois Curfman McInnes     if (idxm[i] >= rstart && idxm[i] < rend) {
291b49de8d1SLois Curfman McInnes       row = idxm[i] - rstart;
292b49de8d1SLois Curfman McInnes       for (j=0; j<n; j++) {
293590ac198SBarry Smith         if (idxn[j] < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %d",idxn[j]);
294590ac198SBarry Smith         if (idxn[j] >= mat->N) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %d max %d",idxn[j],mat->N-1);
295b49de8d1SLois Curfman McInnes         if (idxn[j] >= cstart && idxn[j] < cend){
296b49de8d1SLois Curfman McInnes           col = idxn[j] - cstart;
297b49de8d1SLois Curfman McInnes           ierr = MatGetValues(aij->A,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
298fa852ad4SSatish Balay         } else {
299905e6a2fSBarry Smith           if (!aij->colmap) {
300905e6a2fSBarry Smith             ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
301905e6a2fSBarry Smith           }
302aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
3030f5bd95cSBarry Smith           ierr = PetscTableFind(aij->colmap,idxn[j]+1,&col);CHKERRQ(ierr);
304fa46199cSSatish Balay           col --;
305b1fc9764SSatish Balay #else
306905e6a2fSBarry Smith           col = aij->colmap[idxn[j]] - 1;
307b1fc9764SSatish Balay #endif
308e60e1c95SSatish Balay           if ((col < 0) || (aij->garray[col] != idxn[j])) *(v+i*n+j) = 0.0;
309d9d09a02SSatish Balay           else {
310b49de8d1SLois Curfman McInnes             ierr = MatGetValues(aij->B,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
311b49de8d1SLois Curfman McInnes           }
312b49de8d1SLois Curfman McInnes         }
313b49de8d1SLois Curfman McInnes       }
314a8c6a408SBarry Smith     } else {
31529bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"Only local values currently supported");
316b49de8d1SLois Curfman McInnes     }
317b49de8d1SLois Curfman McInnes   }
3183a40ed3dSBarry Smith   PetscFunctionReturn(0);
319b49de8d1SLois Curfman McInnes }
320bc5ccf88SSatish Balay 
3214a2ae208SSatish Balay #undef __FUNCT__
3224a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyBegin_MPIAIJ"
323bc5ccf88SSatish Balay int MatAssemblyBegin_MPIAIJ(Mat mat,MatAssemblyType mode)
324bc5ccf88SSatish Balay {
325bc5ccf88SSatish Balay   Mat_MPIAIJ  *aij = (Mat_MPIAIJ*)mat->data;
326d36fbae8SSatish Balay   int         ierr,nstash,reallocs;
327bc5ccf88SSatish Balay   InsertMode  addv;
328bc5ccf88SSatish Balay 
329bc5ccf88SSatish Balay   PetscFunctionBegin;
330bc5ccf88SSatish Balay   if (aij->donotstash) {
331bc5ccf88SSatish Balay     PetscFunctionReturn(0);
332bc5ccf88SSatish Balay   }
333bc5ccf88SSatish Balay 
334bc5ccf88SSatish Balay   /* make sure all processors are either in INSERTMODE or ADDMODE */
335bc5ccf88SSatish Balay   ierr = MPI_Allreduce(&mat->insertmode,&addv,1,MPI_INT,MPI_BOR,mat->comm);CHKERRQ(ierr);
336bc5ccf88SSatish Balay   if (addv == (ADD_VALUES|INSERT_VALUES)) {
33729bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Some processors inserted others added");
338bc5ccf88SSatish Balay   }
339bc5ccf88SSatish Balay   mat->insertmode = addv; /* in case this processor had no cache */
340bc5ccf88SSatish Balay 
3418798bf22SSatish Balay   ierr = MatStashScatterBegin_Private(&mat->stash,aij->rowners);CHKERRQ(ierr);
3428798bf22SSatish Balay   ierr = MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);CHKERRQ(ierr);
343b0a32e0cSBarry Smith   PetscLogInfo(aij->A,"MatAssemblyBegin_MPIAIJ:Stash has %d entries, uses %d mallocs.\n",nstash,reallocs);
344bc5ccf88SSatish Balay   PetscFunctionReturn(0);
345bc5ccf88SSatish Balay }
346bc5ccf88SSatish Balay 
347bc5ccf88SSatish Balay 
3484a2ae208SSatish Balay #undef __FUNCT__
3494a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_MPIAIJ"
350bc5ccf88SSatish Balay int MatAssemblyEnd_MPIAIJ(Mat mat,MatAssemblyType mode)
351bc5ccf88SSatish Balay {
352bc5ccf88SSatish Balay   Mat_MPIAIJ  *aij = (Mat_MPIAIJ*)mat->data;
35324f910e3SHong Zhang   Mat_SeqAIJ  *a=(Mat_SeqAIJ *)aij->A->data,*b= (Mat_SeqAIJ *)aij->B->data;
354a2d1c673SSatish Balay   int         i,j,rstart,ncols,n,ierr,flg;
355bc5ccf88SSatish Balay   int         *row,*col,other_disassembled;
35687828ca2SBarry Smith   PetscScalar *val;
357bc5ccf88SSatish Balay   InsertMode  addv = mat->insertmode;
358eee76cafSHong Zhang #if defined(PETSC_HAVE_SUPERLUDIST) || defined(PETSC_HAVE_SPOOLES) || defined(PETSC_HAVE_MUMPS)
3599b9367d5SHong Zhang   PetscTruth  flag;
3609e070d67SMatthew Knepley #endif
361bc5ccf88SSatish Balay 
362bc5ccf88SSatish Balay   PetscFunctionBegin;
363bc5ccf88SSatish Balay   if (!aij->donotstash) {
364a2d1c673SSatish Balay     while (1) {
3658798bf22SSatish Balay       ierr = MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);CHKERRQ(ierr);
366a2d1c673SSatish Balay       if (!flg) break;
367a2d1c673SSatish Balay 
368bc5ccf88SSatish Balay       for (i=0; i<n;) {
369bc5ccf88SSatish Balay         /* Now identify the consecutive vals belonging to the same row */
370bc5ccf88SSatish Balay         for (j=i,rstart=row[j]; j<n; j++) { if (row[j] != rstart) break; }
371bc5ccf88SSatish Balay         if (j < n) ncols = j-i;
372bc5ccf88SSatish Balay         else       ncols = n-i;
373bc5ccf88SSatish Balay         /* Now assemble all these values with a single function call */
374bc5ccf88SSatish Balay         ierr = MatSetValues_MPIAIJ(mat,1,row+i,ncols,col+i,val+i,addv);CHKERRQ(ierr);
375bc5ccf88SSatish Balay         i = j;
376bc5ccf88SSatish Balay       }
377bc5ccf88SSatish Balay     }
3788798bf22SSatish Balay     ierr = MatStashScatterEnd_Private(&mat->stash);CHKERRQ(ierr);
379bc5ccf88SSatish Balay   }
380bc5ccf88SSatish Balay 
381bc5ccf88SSatish Balay   ierr = MatAssemblyBegin(aij->A,mode);CHKERRQ(ierr);
382bc5ccf88SSatish Balay   ierr = MatAssemblyEnd(aij->A,mode);CHKERRQ(ierr);
383bc5ccf88SSatish Balay 
384bc5ccf88SSatish Balay   /* determine if any processor has disassembled, if so we must
385bc5ccf88SSatish Balay      also disassemble ourselfs, in order that we may reassemble. */
386bc5ccf88SSatish Balay   /*
387bc5ccf88SSatish Balay      if nonzero structure of submatrix B cannot change then we know that
388bc5ccf88SSatish Balay      no processor disassembled thus we can skip this stuff
389bc5ccf88SSatish Balay   */
390bc5ccf88SSatish Balay   if (!((Mat_SeqAIJ*)aij->B->data)->nonew)  {
391bc5ccf88SSatish Balay     ierr = MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPI_INT,MPI_PROD,mat->comm);CHKERRQ(ierr);
392bc5ccf88SSatish Balay     if (mat->was_assembled && !other_disassembled) {
393bc5ccf88SSatish Balay       ierr = DisAssemble_MPIAIJ(mat);CHKERRQ(ierr);
394bc5ccf88SSatish Balay     }
395bc5ccf88SSatish Balay   }
396bc5ccf88SSatish Balay 
397bc5ccf88SSatish Balay   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
398bc5ccf88SSatish Balay     ierr = MatSetUpMultiply_MPIAIJ(mat);CHKERRQ(ierr);
399bc5ccf88SSatish Balay   }
400bc5ccf88SSatish Balay   ierr = MatAssemblyBegin(aij->B,mode);CHKERRQ(ierr);
401bc5ccf88SSatish Balay   ierr = MatAssemblyEnd(aij->B,mode);CHKERRQ(ierr);
402bc5ccf88SSatish Balay 
403606d414cSSatish Balay   if (aij->rowvalues) {
404606d414cSSatish Balay     ierr = PetscFree(aij->rowvalues);CHKERRQ(ierr);
405606d414cSSatish Balay     aij->rowvalues = 0;
406606d414cSSatish Balay   }
407a30b2313SHong Zhang 
408a30b2313SHong Zhang   /* used by MatAXPY() */
409a30b2313SHong Zhang   a->xtoy = 0; b->xtoy = 0;
410a30b2313SHong Zhang   a->XtoY = 0; b->XtoY = 0;
411a30b2313SHong Zhang 
4129b9367d5SHong Zhang #if defined(PETSC_HAVE_SUPERLUDIST)
413e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(mat->prefix,"-mat_aij_superlu_dist",&flag);CHKERRQ(ierr);
4149b9367d5SHong Zhang   if (flag) { ierr = MatUseSuperLU_DIST_MPIAIJ(mat);CHKERRQ(ierr); }
4159b9367d5SHong Zhang #endif
4164032b0d1SHong Zhang 
4174032b0d1SHong Zhang #if defined(PETSC_HAVE_SPOOLES)
418e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(mat->prefix,"-mat_aij_spooles",&flag);CHKERRQ(ierr);
4194032b0d1SHong Zhang   if (flag) { ierr = MatUseSpooles_MPIAIJ(mat);CHKERRQ(ierr); }
4204032b0d1SHong Zhang #endif
421eee76cafSHong Zhang 
422eee76cafSHong Zhang #if defined(PETSC_HAVE_MUMPS)
423eee76cafSHong Zhang   ierr = PetscOptionsHasName(mat->prefix,"-mat_aij_mumps",&flag);CHKERRQ(ierr);
424eee76cafSHong Zhang   if (flag) { ierr = MatUseMUMPS_MPIAIJ(mat);CHKERRQ(ierr); }
425eee76cafSHong Zhang #endif
426bc5ccf88SSatish Balay   PetscFunctionReturn(0);
427bc5ccf88SSatish Balay }
428bc5ccf88SSatish Balay 
4294a2ae208SSatish Balay #undef __FUNCT__
4304a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_MPIAIJ"
4318f6be9afSLois Curfman McInnes int MatZeroEntries_MPIAIJ(Mat A)
4321eb62cbbSBarry Smith {
43344a69424SLois Curfman McInnes   Mat_MPIAIJ *l = (Mat_MPIAIJ*)A->data;
434dbd7a890SLois Curfman McInnes   int        ierr;
4353a40ed3dSBarry Smith 
4363a40ed3dSBarry Smith   PetscFunctionBegin;
43778b31e54SBarry Smith   ierr = MatZeroEntries(l->A);CHKERRQ(ierr);
43878b31e54SBarry Smith   ierr = MatZeroEntries(l->B);CHKERRQ(ierr);
4393a40ed3dSBarry Smith   PetscFunctionReturn(0);
4401eb62cbbSBarry Smith }
4411eb62cbbSBarry Smith 
4424a2ae208SSatish Balay #undef __FUNCT__
4434a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_MPIAIJ"
444*268466fbSBarry Smith int MatZeroRows_MPIAIJ(Mat A,IS is,const PetscScalar *diag)
4451eb62cbbSBarry Smith {
44644a69424SLois Curfman McInnes   Mat_MPIAIJ     *l = (Mat_MPIAIJ*)A->data;
44717699dbbSLois Curfman McInnes   int            i,ierr,N,*rows,*owners = l->rowners,size = l->size;
448c1dc657dSBarry Smith   int            *nprocs,j,idx,nsends,row;
44917699dbbSLois Curfman McInnes   int            nmax,*svalues,*starts,*owner,nrecvs,rank = l->rank;
4505392566eSBarry Smith   int            *rvalues,tag = A->tag,count,base,slen,n,*source;
4516a5c57faSSatish Balay   int            *lens,imdex,*lrows,*values,rstart=l->rstart;
452d6dfbf8fSBarry Smith   MPI_Comm       comm = A->comm;
4531eb62cbbSBarry Smith   MPI_Request    *send_waits,*recv_waits;
4541eb62cbbSBarry Smith   MPI_Status     recv_status,*send_status;
4551eb62cbbSBarry Smith   IS             istmp;
45635d8aa7fSBarry Smith   PetscTruth     found;
4571eb62cbbSBarry Smith 
4583a40ed3dSBarry Smith   PetscFunctionBegin;
459e03a110bSBarry Smith   ierr = ISGetLocalSize(is,&N);CHKERRQ(ierr);
46078b31e54SBarry Smith   ierr = ISGetIndices(is,&rows);CHKERRQ(ierr);
4611eb62cbbSBarry Smith 
4621eb62cbbSBarry Smith   /*  first count number of contributors to each processor */
463b0a32e0cSBarry Smith   ierr = PetscMalloc(2*size*sizeof(int),&nprocs);CHKERRQ(ierr);
464549d3d68SSatish Balay   ierr   = PetscMemzero(nprocs,2*size*sizeof(int));CHKERRQ(ierr);
465b0a32e0cSBarry Smith   ierr = PetscMalloc((N+1)*sizeof(int),&owner);CHKERRQ(ierr); /* see note*/
4661eb62cbbSBarry Smith   for (i=0; i<N; i++) {
4671eb62cbbSBarry Smith     idx = rows[i];
46835d8aa7fSBarry Smith     found = PETSC_FALSE;
46917699dbbSLois Curfman McInnes     for (j=0; j<size; j++) {
4701eb62cbbSBarry Smith       if (idx >= owners[j] && idx < owners[j+1]) {
471c1dc657dSBarry Smith         nprocs[2*j]++; nprocs[2*j+1] = 1; owner[i] = j; found = PETSC_TRUE; break;
4721eb62cbbSBarry Smith       }
4731eb62cbbSBarry Smith     }
47429bbc08cSBarry Smith     if (!found) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Index out of range");
4751eb62cbbSBarry Smith   }
476c1dc657dSBarry Smith   nsends = 0;  for (i=0; i<size; i++) { nsends += nprocs[2*i+1];}
4771eb62cbbSBarry Smith 
4781eb62cbbSBarry Smith   /* inform other processors of number of messages and max length*/
479c1dc657dSBarry Smith   ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr);
4801eb62cbbSBarry Smith 
4811eb62cbbSBarry Smith   /* post receives:   */
482b0a32e0cSBarry Smith   ierr = PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(int),&rvalues);CHKERRQ(ierr);
483b0a32e0cSBarry Smith   ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr);
4841eb62cbbSBarry Smith   for (i=0; i<nrecvs; i++) {
485ca161407SBarry Smith     ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPI_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr);
4861eb62cbbSBarry Smith   }
4871eb62cbbSBarry Smith 
4881eb62cbbSBarry Smith   /* do sends:
4891eb62cbbSBarry Smith       1) starts[i] gives the starting index in svalues for stuff going to
4901eb62cbbSBarry Smith          the ith processor
4911eb62cbbSBarry Smith   */
492b0a32e0cSBarry Smith   ierr = PetscMalloc((N+1)*sizeof(int),&svalues);CHKERRQ(ierr);
493b0a32e0cSBarry Smith   ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr);
494b0a32e0cSBarry Smith   ierr = PetscMalloc((size+1)*sizeof(int),&starts);CHKERRQ(ierr);
4951eb62cbbSBarry Smith   starts[0] = 0;
496c1dc657dSBarry Smith   for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
4971eb62cbbSBarry Smith   for (i=0; i<N; i++) {
4981eb62cbbSBarry Smith     svalues[starts[owner[i]]++] = rows[i];
4991eb62cbbSBarry Smith   }
5006831982aSBarry Smith   ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr);
5011eb62cbbSBarry Smith 
5021eb62cbbSBarry Smith   starts[0] = 0;
503c1dc657dSBarry Smith   for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
5041eb62cbbSBarry Smith   count = 0;
50517699dbbSLois Curfman McInnes   for (i=0; i<size; i++) {
506c1dc657dSBarry Smith     if (nprocs[2*i+1]) {
507c1dc657dSBarry Smith       ierr = MPI_Isend(svalues+starts[i],nprocs[2*i],MPI_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr);
5081eb62cbbSBarry Smith     }
5091eb62cbbSBarry Smith   }
510606d414cSSatish Balay   ierr = PetscFree(starts);CHKERRQ(ierr);
5111eb62cbbSBarry Smith 
51217699dbbSLois Curfman McInnes   base = owners[rank];
5131eb62cbbSBarry Smith 
5141eb62cbbSBarry Smith   /*  wait on receives */
515b0a32e0cSBarry Smith   ierr   = PetscMalloc(2*(nrecvs+1)*sizeof(int),&lens);CHKERRQ(ierr);
5161eb62cbbSBarry Smith   source = lens + nrecvs;
5171eb62cbbSBarry Smith   count  = nrecvs; slen = 0;
5181eb62cbbSBarry Smith   while (count) {
519ca161407SBarry Smith     ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
5201eb62cbbSBarry Smith     /* unpack receives into our local space */
521ca161407SBarry Smith     ierr = MPI_Get_count(&recv_status,MPI_INT,&n);CHKERRQ(ierr);
522d6dfbf8fSBarry Smith     source[imdex]  = recv_status.MPI_SOURCE;
523d6dfbf8fSBarry Smith     lens[imdex]    = n;
5241eb62cbbSBarry Smith     slen          += n;
5251eb62cbbSBarry Smith     count--;
5261eb62cbbSBarry Smith   }
527606d414cSSatish Balay   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
5281eb62cbbSBarry Smith 
5291eb62cbbSBarry Smith   /* move the data into the send scatter */
530b0a32e0cSBarry Smith   ierr = PetscMalloc((slen+1)*sizeof(int),&lrows);CHKERRQ(ierr);
5311eb62cbbSBarry Smith   count = 0;
5321eb62cbbSBarry Smith   for (i=0; i<nrecvs; i++) {
5331eb62cbbSBarry Smith     values = rvalues + i*nmax;
5341eb62cbbSBarry Smith     for (j=0; j<lens[i]; j++) {
5351eb62cbbSBarry Smith       lrows[count++] = values[j] - base;
5361eb62cbbSBarry Smith     }
5371eb62cbbSBarry Smith   }
538606d414cSSatish Balay   ierr = PetscFree(rvalues);CHKERRQ(ierr);
539606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
540606d414cSSatish Balay   ierr = PetscFree(owner);CHKERRQ(ierr);
541606d414cSSatish Balay   ierr = PetscFree(nprocs);CHKERRQ(ierr);
5421eb62cbbSBarry Smith 
5431eb62cbbSBarry Smith   /* actually zap the local rows */
544029af93fSBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,slen,lrows,&istmp);CHKERRQ(ierr);
545b0a32e0cSBarry Smith   PetscLogObjectParent(A,istmp);
5466a5c57faSSatish Balay 
5476eb55b6aSBarry Smith   /*
5486eb55b6aSBarry Smith         Zero the required rows. If the "diagonal block" of the matrix
5496eb55b6aSBarry Smith      is square and the user wishes to set the diagonal we use seperate
5506eb55b6aSBarry Smith      code so that MatSetValues() is not called for each diagonal allocating
5516eb55b6aSBarry Smith      new memory, thus calling lots of mallocs and slowing things down.
5526eb55b6aSBarry Smith 
5536eb55b6aSBarry Smith        Contributed by: Mathew Knepley
5546eb55b6aSBarry Smith   */
555e2d53e46SBarry Smith   /* must zero l->B before l->A because the (diag) case below may put values into l->B*/
556e2d53e46SBarry Smith   ierr = MatZeroRows(l->B,istmp,0);CHKERRQ(ierr);
5576eb55b6aSBarry Smith   if (diag && (l->A->M == l->A->N)) {
5586eb55b6aSBarry Smith     ierr      = MatZeroRows(l->A,istmp,diag);CHKERRQ(ierr);
559e2d53e46SBarry Smith   } else if (diag) {
560e2d53e46SBarry Smith     ierr = MatZeroRows(l->A,istmp,0);CHKERRQ(ierr);
561fa46199cSSatish Balay     if (((Mat_SeqAIJ*)l->A->data)->nonew) {
56229bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"MatZeroRows() on rectangular matrices cannot be used with the Mat options\n\
563fa46199cSSatish Balay MAT_NO_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR");
5646525c446SSatish Balay     }
565e2d53e46SBarry Smith     for (i = 0; i < slen; i++) {
566e2d53e46SBarry Smith       row  = lrows[i] + rstart;
567e2d53e46SBarry Smith       ierr = MatSetValues(A,1,&row,1,&row,diag,INSERT_VALUES);CHKERRQ(ierr);
568e2d53e46SBarry Smith     }
569e2d53e46SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
570e2d53e46SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
5716eb55b6aSBarry Smith   } else {
5726a5c57faSSatish Balay     ierr = MatZeroRows(l->A,istmp,0);CHKERRQ(ierr);
5736eb55b6aSBarry Smith   }
57478b31e54SBarry Smith   ierr = ISDestroy(istmp);CHKERRQ(ierr);
575606d414cSSatish Balay   ierr = PetscFree(lrows);CHKERRQ(ierr);
57672dacd9aSBarry Smith 
5771eb62cbbSBarry Smith   /* wait on sends */
5781eb62cbbSBarry Smith   if (nsends) {
579b0a32e0cSBarry Smith     ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr);
580ca161407SBarry Smith     ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr);
581606d414cSSatish Balay     ierr = PetscFree(send_status);CHKERRQ(ierr);
5821eb62cbbSBarry Smith   }
583606d414cSSatish Balay   ierr = PetscFree(send_waits);CHKERRQ(ierr);
584606d414cSSatish Balay   ierr = PetscFree(svalues);CHKERRQ(ierr);
5851eb62cbbSBarry Smith 
5863a40ed3dSBarry Smith   PetscFunctionReturn(0);
5871eb62cbbSBarry Smith }
5881eb62cbbSBarry Smith 
5894a2ae208SSatish Balay #undef __FUNCT__
5904a2ae208SSatish Balay #define __FUNCT__ "MatMult_MPIAIJ"
5918f6be9afSLois Curfman McInnes int MatMult_MPIAIJ(Mat A,Vec xx,Vec yy)
5921eb62cbbSBarry Smith {
593416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
594fbd6ef76SBarry Smith   int        ierr,nt;
595416022c9SBarry Smith 
5963a40ed3dSBarry Smith   PetscFunctionBegin;
597a2ce50c7SBarry Smith   ierr = VecGetLocalSize(xx,&nt);CHKERRQ(ierr);
598273d9f13SBarry Smith   if (nt != A->n) {
599273d9f13SBarry Smith     SETERRQ2(PETSC_ERR_ARG_SIZ,"Incompatible partition of A (%d) and xx (%d)",A->n,nt);
600fbd6ef76SBarry Smith   }
60143a90d84SBarry Smith   ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
602f830108cSBarry Smith   ierr = (*a->A->ops->mult)(a->A,xx,yy);CHKERRQ(ierr);
60343a90d84SBarry Smith   ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
604f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);CHKERRQ(ierr);
6053a40ed3dSBarry Smith   PetscFunctionReturn(0);
6061eb62cbbSBarry Smith }
6071eb62cbbSBarry Smith 
6084a2ae208SSatish Balay #undef __FUNCT__
6094a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_MPIAIJ"
6108f6be9afSLois Curfman McInnes int MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
611da3a660dSBarry Smith {
612416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
613da3a660dSBarry Smith   int        ierr;
6143a40ed3dSBarry Smith 
6153a40ed3dSBarry Smith   PetscFunctionBegin;
61643a90d84SBarry Smith   ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
617f830108cSBarry Smith   ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
61843a90d84SBarry Smith   ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
619f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);CHKERRQ(ierr);
6203a40ed3dSBarry Smith   PetscFunctionReturn(0);
621da3a660dSBarry Smith }
622da3a660dSBarry Smith 
6234a2ae208SSatish Balay #undef __FUNCT__
6244a2ae208SSatish Balay #define __FUNCT__ "MatMultTranspose_MPIAIJ"
6257c922b88SBarry Smith int MatMultTranspose_MPIAIJ(Mat A,Vec xx,Vec yy)
626da3a660dSBarry Smith {
627416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
628da3a660dSBarry Smith   int        ierr;
629da3a660dSBarry Smith 
6303a40ed3dSBarry Smith   PetscFunctionBegin;
631da3a660dSBarry Smith   /* do nondiagonal part */
6327c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
633da3a660dSBarry Smith   /* send it on its way */
634537820f0SBarry Smith   ierr = VecScatterBegin(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
635da3a660dSBarry Smith   /* do local part */
6367c922b88SBarry Smith   ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr);
637da3a660dSBarry Smith   /* receive remote parts: note this assumes the values are not actually */
638da3a660dSBarry Smith   /* inserted in yy until the next line, which is true for my implementation*/
639da3a660dSBarry Smith   /* but is not perhaps always true. */
640537820f0SBarry Smith   ierr = VecScatterEnd(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
6413a40ed3dSBarry Smith   PetscFunctionReturn(0);
642da3a660dSBarry Smith }
643da3a660dSBarry Smith 
644cd0d46ebSvictorle EXTERN_C_BEGIN
645cd0d46ebSvictorle #undef __FUNCT__
646cd0d46ebSvictorle #define __FUNCT__ "MatIsSymmetric_MPIAIJ"
647cd0d46ebSvictorle int MatIsSymmetric_MPIAIJ(Mat Amat,Mat Bmat,PetscTruth *f)
648cd0d46ebSvictorle {
649cd0d46ebSvictorle   Mat_MPIAIJ *Aij = (Mat_MPIAIJ *) Amat->data, *Bij;
65066501d38Svictorle   Mat        Adia = Aij->A, Bdia, Aoff,Boff,*Aoffs,*Boffs;
651cd0d46ebSvictorle   IS         Me,Notme;
652cd0d46ebSvictorle   int        M,N,first,last,*notme,i, ierr;
653cd0d46ebSvictorle 
654cd0d46ebSvictorle   PetscFunctionBegin;
65542e5f5b4Svictorle 
65642e5f5b4Svictorle   /* Easy test: symmetric diagonal block */
65766501d38Svictorle   Bij = (Mat_MPIAIJ *) Bmat->data; Bdia = Bij->A;
65866501d38Svictorle   ierr = MatIsSymmetric(Adia,Bdia,f); CHKERRQ(ierr);
659cd0d46ebSvictorle   if (!*f) PetscFunctionReturn(0);
66042e5f5b4Svictorle 
66142e5f5b4Svictorle   /* Hard test: off-diagonal block. This takes a MatGetSubMatrix. */
662cd0d46ebSvictorle   ierr = MatGetSize(Amat,&M,&N); CHKERRQ(ierr);
663cd0d46ebSvictorle   ierr = MatGetOwnershipRange(Amat,&first,&last); CHKERRQ(ierr);
664cd0d46ebSvictorle   ierr = PetscMalloc((N-last+first)*sizeof(int),&notme); CHKERRQ(ierr);
665cd0d46ebSvictorle   for (i=0; i<first; i++) notme[i] = i;
666cd0d46ebSvictorle   for (i=last; i<M; i++) notme[i-last+first] = i;
667*268466fbSBarry Smith   ierr = ISCreateGeneral(MPI_COMM_SELF,N-last+first,notme,&Notme); CHKERRQ(ierr);
668*268466fbSBarry Smith   ierr = ISCreateStride(MPI_COMM_SELF,last-first,first,1,&Me); CHKERRQ(ierr);
669*268466fbSBarry Smith   ierr = MatGetSubMatrices(Amat,1,&Me,&Notme,MAT_INITIAL_MATRIX,&Aoffs); CHKERRQ(ierr);
67066501d38Svictorle   Aoff = Aoffs[0];
671*268466fbSBarry Smith   ierr = MatGetSubMatrices(Bmat,1,&Notme,&Me,MAT_INITIAL_MATRIX,&Boffs); CHKERRQ(ierr);
67266501d38Svictorle   Boff = Boffs[0];
673cd0d46ebSvictorle   ierr = MatIsSymmetric(Aoff,Boff,f); CHKERRQ(ierr);
67466501d38Svictorle   ierr = MatDestroyMatrices(1,&Aoffs); CHKERRQ(ierr);
67566501d38Svictorle   ierr = MatDestroyMatrices(1,&Boffs); CHKERRQ(ierr);
67642e5f5b4Svictorle   ierr = ISDestroy(Me); CHKERRQ(ierr);
67742e5f5b4Svictorle   ierr = ISDestroy(Notme); CHKERRQ(ierr);
67842e5f5b4Svictorle 
679cd0d46ebSvictorle   PetscFunctionReturn(0);
680cd0d46ebSvictorle }
681cd0d46ebSvictorle EXTERN_C_END
682cd0d46ebSvictorle 
6834a2ae208SSatish Balay #undef __FUNCT__
6844a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_MPIAIJ"
6857c922b88SBarry Smith int MatMultTransposeAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
686da3a660dSBarry Smith {
687416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
688da3a660dSBarry Smith   int        ierr;
689da3a660dSBarry Smith 
6903a40ed3dSBarry Smith   PetscFunctionBegin;
691da3a660dSBarry Smith   /* do nondiagonal part */
6927c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
693da3a660dSBarry Smith   /* send it on its way */
694537820f0SBarry Smith   ierr = VecScatterBegin(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
695da3a660dSBarry Smith   /* do local part */
6967c922b88SBarry Smith   ierr = (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
697da3a660dSBarry Smith   /* receive remote parts: note this assumes the values are not actually */
698da3a660dSBarry Smith   /* inserted in yy until the next line, which is true for my implementation*/
699da3a660dSBarry Smith   /* but is not perhaps always true. */
7000a198c4cSBarry Smith   ierr = VecScatterEnd(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
7013a40ed3dSBarry Smith   PetscFunctionReturn(0);
702da3a660dSBarry Smith }
703da3a660dSBarry Smith 
7041eb62cbbSBarry Smith /*
7051eb62cbbSBarry Smith   This only works correctly for square matrices where the subblock A->A is the
7061eb62cbbSBarry Smith    diagonal block
7071eb62cbbSBarry Smith */
7084a2ae208SSatish Balay #undef __FUNCT__
7094a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_MPIAIJ"
7108f6be9afSLois Curfman McInnes int MatGetDiagonal_MPIAIJ(Mat A,Vec v)
7111eb62cbbSBarry Smith {
7123a40ed3dSBarry Smith   int        ierr;
713416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
7143a40ed3dSBarry Smith 
7153a40ed3dSBarry Smith   PetscFunctionBegin;
716273d9f13SBarry Smith   if (A->M != A->N) SETERRQ(PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block");
7175baf8537SBarry Smith   if (a->rstart != a->cstart || a->rend != a->cend) {
71829bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_SIZ,"row partition must equal col partition");
7193a40ed3dSBarry Smith   }
7203a40ed3dSBarry Smith   ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr);
7213a40ed3dSBarry Smith   PetscFunctionReturn(0);
7221eb62cbbSBarry Smith }
7231eb62cbbSBarry Smith 
7244a2ae208SSatish Balay #undef __FUNCT__
7254a2ae208SSatish Balay #define __FUNCT__ "MatScale_MPIAIJ"
726*268466fbSBarry Smith int MatScale_MPIAIJ(const PetscScalar aa[],Mat A)
727052efed2SBarry Smith {
728052efed2SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
729052efed2SBarry Smith   int        ierr;
7303a40ed3dSBarry Smith 
7313a40ed3dSBarry Smith   PetscFunctionBegin;
732052efed2SBarry Smith   ierr = MatScale(aa,a->A);CHKERRQ(ierr);
733052efed2SBarry Smith   ierr = MatScale(aa,a->B);CHKERRQ(ierr);
7343a40ed3dSBarry Smith   PetscFunctionReturn(0);
735052efed2SBarry Smith }
736052efed2SBarry Smith 
7374a2ae208SSatish Balay #undef __FUNCT__
7384a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_MPIAIJ"
739e1311b90SBarry Smith int MatDestroy_MPIAIJ(Mat mat)
7401eb62cbbSBarry Smith {
74144a69424SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
7421eb62cbbSBarry Smith   int        ierr;
74383e2fdc7SBarry Smith 
7443a40ed3dSBarry Smith   PetscFunctionBegin;
745aa482453SBarry Smith #if defined(PETSC_USE_LOG)
746b0a32e0cSBarry Smith   PetscLogObjectState((PetscObject)mat,"Rows=%d, Cols=%d",mat->M,mat->N);
747a5a9c739SBarry Smith #endif
7488798bf22SSatish Balay   ierr = MatStashDestroy_Private(&mat->stash);CHKERRQ(ierr);
749606d414cSSatish Balay   ierr = PetscFree(aij->rowners);CHKERRQ(ierr);
75078b31e54SBarry Smith   ierr = MatDestroy(aij->A);CHKERRQ(ierr);
75178b31e54SBarry Smith   ierr = MatDestroy(aij->B);CHKERRQ(ierr);
752aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
7530f5bd95cSBarry Smith   if (aij->colmap) {ierr = PetscTableDelete(aij->colmap);CHKERRQ(ierr);}
754b1fc9764SSatish Balay #else
755606d414cSSatish Balay   if (aij->colmap) {ierr = PetscFree(aij->colmap);CHKERRQ(ierr);}
756b1fc9764SSatish Balay #endif
757606d414cSSatish Balay   if (aij->garray) {ierr = PetscFree(aij->garray);CHKERRQ(ierr);}
7587c922b88SBarry Smith   if (aij->lvec)   {ierr = VecDestroy(aij->lvec);CHKERRQ(ierr);}
7597c922b88SBarry Smith   if (aij->Mvctx)  {ierr = VecScatterDestroy(aij->Mvctx);CHKERRQ(ierr);}
760606d414cSSatish Balay   if (aij->rowvalues) {ierr = PetscFree(aij->rowvalues);CHKERRQ(ierr);}
761606d414cSSatish Balay   ierr = PetscFree(aij);CHKERRQ(ierr);
7623a40ed3dSBarry Smith   PetscFunctionReturn(0);
7631eb62cbbSBarry Smith }
764ee50ffe9SBarry Smith 
7654aedb280SBarry Smith extern int MatMPIAIJFactorInfo_SuperLu(Mat,PetscViewer);
766a072f7f7SHong Zhang extern int MatFactorInfo_Spooles(Mat,PetscViewer);
7671dfdf1d9SHong Zhang extern int MatFactorInfo_MUMPS(Mat,PetscViewer);
7684aedb280SBarry Smith 
7694a2ae208SSatish Balay #undef __FUNCT__
7708e2fed03SBarry Smith #define __FUNCT__ "MatView_MPIAIJ_Binary"
7718e2fed03SBarry Smith int MatView_MPIAIJ_Binary(Mat mat,PetscViewer viewer)
7728e2fed03SBarry Smith {
7738e2fed03SBarry Smith   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*)mat->data;
7748e2fed03SBarry Smith   Mat_SeqAIJ*       A = (Mat_SeqAIJ*)aij->A->data;
7758e2fed03SBarry Smith   Mat_SeqAIJ*       B = (Mat_SeqAIJ*)aij->B->data;
7768e2fed03SBarry Smith   int               nz,fd,ierr,header[4],rank,size,*row_lengths,*range,rlen,i,tag = ((PetscObject)viewer)->tag;
777b021249fSBarry Smith   int               nzmax,*column_indices,j,k,col,*garray = aij->garray,cnt,cstart = aij->cstart,rnz;
7788e2fed03SBarry Smith   PetscScalar       *column_values;
7798e2fed03SBarry Smith 
7808e2fed03SBarry Smith   PetscFunctionBegin;
7818e2fed03SBarry Smith   ierr = MPI_Comm_rank(mat->comm,&rank);CHKERRQ(ierr);
7828e2fed03SBarry Smith   ierr = MPI_Comm_size(mat->comm,&size);CHKERRQ(ierr);
7838e2fed03SBarry Smith   nz   = A->nz + B->nz;
7848e2fed03SBarry Smith   if (rank == 0) {
7858e2fed03SBarry Smith     header[0] = MAT_FILE_COOKIE;
7868e2fed03SBarry Smith     header[1] = mat->M;
7878e2fed03SBarry Smith     header[2] = mat->N;
7888e2fed03SBarry Smith     ierr = MPI_Reduce(&nz,&header[3],1,MPI_INT,MPI_SUM,0,mat->comm);CHKERRQ(ierr);
7898e2fed03SBarry Smith     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
7908e2fed03SBarry Smith     ierr = PetscBinaryWrite(fd,header,4,PETSC_INT,1);CHKERRQ(ierr);
7918e2fed03SBarry Smith     /* get largest number of rows any processor has */
7928e2fed03SBarry Smith     rlen = mat->m;
7938e2fed03SBarry Smith     ierr = PetscMapGetGlobalRange(mat->rmap,&range);CHKERRQ(ierr);
7948e2fed03SBarry Smith     for (i=1; i<size; i++) {
7958e2fed03SBarry Smith       rlen = PetscMax(rlen,range[i+1] - range[i]);
7968e2fed03SBarry Smith     }
7978e2fed03SBarry Smith   } else {
7988e2fed03SBarry Smith     ierr = MPI_Reduce(&nz,0,1,MPI_INT,MPI_SUM,0,mat->comm);CHKERRQ(ierr);
7998e2fed03SBarry Smith     rlen = mat->m;
8008e2fed03SBarry Smith   }
8018e2fed03SBarry Smith 
8028e2fed03SBarry Smith   /* load up the local row counts */
8038e2fed03SBarry Smith   ierr = PetscMalloc((rlen+1)*sizeof(int),&row_lengths);CHKERRQ(ierr);
8048e2fed03SBarry Smith   for (i=0; i<mat->m; i++) {
8058e2fed03SBarry Smith     row_lengths[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i];
8068e2fed03SBarry Smith   }
8078e2fed03SBarry Smith 
8088e2fed03SBarry Smith   /* store the row lengths to the file */
8098e2fed03SBarry Smith   if (rank == 0) {
8108e2fed03SBarry Smith     MPI_Status status;
8118e2fed03SBarry Smith     ierr = PetscBinaryWrite(fd,row_lengths,mat->m,PETSC_INT,1);CHKERRQ(ierr);
8128e2fed03SBarry Smith     for (i=1; i<size; i++) {
8138e2fed03SBarry Smith       rlen = range[i+1] - range[i];
8148e2fed03SBarry Smith       ierr = MPI_Recv(row_lengths,rlen,MPI_INT,i,tag,mat->comm,&status);CHKERRQ(ierr);
8158e2fed03SBarry Smith       ierr = PetscBinaryWrite(fd,row_lengths,rlen,PETSC_INT,1);CHKERRQ(ierr);
8168e2fed03SBarry Smith     }
8178e2fed03SBarry Smith   } else {
8188e2fed03SBarry Smith     ierr = MPI_Send(row_lengths,mat->m,MPI_INT,0,tag,mat->comm);CHKERRQ(ierr);
8198e2fed03SBarry Smith   }
8208e2fed03SBarry Smith   ierr = PetscFree(row_lengths);CHKERRQ(ierr);
8218e2fed03SBarry Smith 
8228e2fed03SBarry Smith   /* load up the local column indices */
8238e2fed03SBarry Smith   nzmax = nz; /* )th processor needs space a largest processor needs */
8248e2fed03SBarry Smith   ierr = MPI_Reduce(&nz,&nzmax,1,MPI_INT,MPI_MAX,0,mat->comm);CHKERRQ(ierr);
8258e2fed03SBarry Smith   ierr = PetscMalloc((nzmax+1)*sizeof(int),&column_indices);CHKERRQ(ierr);
8268e2fed03SBarry Smith   cnt  = 0;
8278e2fed03SBarry Smith   for (i=0; i<mat->m; i++) {
8288e2fed03SBarry Smith     for (j=B->i[i]; j<B->i[i+1]; j++) {
8298e2fed03SBarry Smith       if ( (col = garray[B->j[j]]) > cstart) break;
8308e2fed03SBarry Smith       column_indices[cnt++] = col;
8318e2fed03SBarry Smith     }
8328e2fed03SBarry Smith     for (k=A->i[i]; k<A->i[i+1]; k++) {
8338e2fed03SBarry Smith       column_indices[cnt++] = A->j[k] + cstart;
8348e2fed03SBarry Smith     }
8358e2fed03SBarry Smith     for (; j<B->i[i+1]; j++) {
8368e2fed03SBarry Smith       column_indices[cnt++] = garray[B->j[j]];
8378e2fed03SBarry Smith     }
8388e2fed03SBarry Smith   }
8398e2fed03SBarry Smith   if (cnt != A->nz + B->nz) SETERRQ2(PETSC_ERR_LIB,"Internal PETSc error: cnt = %d nz = %d",cnt,A->nz+B->nz);
8408e2fed03SBarry Smith 
8418e2fed03SBarry Smith   /* store the column indices to the file */
8428e2fed03SBarry Smith   if (rank == 0) {
8438e2fed03SBarry Smith     MPI_Status status;
8448e2fed03SBarry Smith     ierr = PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,1);CHKERRQ(ierr);
8458e2fed03SBarry Smith     for (i=1; i<size; i++) {
846b021249fSBarry Smith       ierr = MPI_Recv(&rnz,1,MPI_INT,i,tag,mat->comm,&status);CHKERRQ(ierr);
847b021249fSBarry Smith       if (rnz > nzmax) SETERRQ2(PETSC_ERR_LIB,"Internal PETSc error: nz = %d nzmax = %d",nz,nzmax);
848b021249fSBarry Smith       ierr = MPI_Recv(column_indices,rnz,MPI_INT,i,tag,mat->comm,&status);CHKERRQ(ierr);
849b021249fSBarry Smith       ierr = PetscBinaryWrite(fd,column_indices,rnz,PETSC_INT,1);CHKERRQ(ierr);
8508e2fed03SBarry Smith     }
8518e2fed03SBarry Smith   } else {
8528e2fed03SBarry Smith     ierr = MPI_Send(&nz,1,MPI_INT,0,tag,mat->comm);CHKERRQ(ierr);
8538e2fed03SBarry Smith     ierr = MPI_Send(column_indices,nz,MPI_INT,0,tag,mat->comm);CHKERRQ(ierr);
8548e2fed03SBarry Smith   }
8558e2fed03SBarry Smith   ierr = PetscFree(column_indices);CHKERRQ(ierr);
8568e2fed03SBarry Smith 
8578e2fed03SBarry Smith   /* load up the local column values */
8588e2fed03SBarry Smith   ierr = PetscMalloc((nzmax+1)*sizeof(PetscScalar),&column_values);CHKERRQ(ierr);
8598e2fed03SBarry Smith   cnt  = 0;
8608e2fed03SBarry Smith   for (i=0; i<mat->m; i++) {
8618e2fed03SBarry Smith     for (j=B->i[i]; j<B->i[i+1]; j++) {
8628e2fed03SBarry Smith       if ( garray[B->j[j]] > cstart) break;
8638e2fed03SBarry Smith       column_values[cnt++] = B->a[j];
8648e2fed03SBarry Smith     }
8658e2fed03SBarry Smith     for (k=A->i[i]; k<A->i[i+1]; k++) {
8668e2fed03SBarry Smith       column_values[cnt++] = A->a[k];
8678e2fed03SBarry Smith     }
8688e2fed03SBarry Smith     for (; j<B->i[i+1]; j++) {
8698e2fed03SBarry Smith       column_values[cnt++] = B->a[j];
8708e2fed03SBarry Smith     }
8718e2fed03SBarry Smith   }
8728e2fed03SBarry Smith   if (cnt != A->nz + B->nz) SETERRQ2(1,"Internal PETSc error: cnt = %d nz = %d",cnt,A->nz+B->nz);
8738e2fed03SBarry Smith 
8748e2fed03SBarry Smith   /* store the column values to the file */
8758e2fed03SBarry Smith   if (rank == 0) {
8768e2fed03SBarry Smith     MPI_Status status;
8778e2fed03SBarry Smith     ierr = PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,1);CHKERRQ(ierr);
8788e2fed03SBarry Smith     for (i=1; i<size; i++) {
879b021249fSBarry Smith       ierr = MPI_Recv(&rnz,1,MPI_INT,i,tag,mat->comm,&status);CHKERRQ(ierr);
880b021249fSBarry Smith       if (rnz > nzmax) SETERRQ2(PETSC_ERR_LIB,"Internal PETSc error: nz = %d nzmax = %d",nz,nzmax);
881b021249fSBarry Smith       ierr = MPI_Recv(column_values,rnz,MPIU_SCALAR,i,tag,mat->comm,&status);CHKERRQ(ierr);
882b021249fSBarry Smith       ierr = PetscBinaryWrite(fd,column_values,rnz,PETSC_SCALAR,1);CHKERRQ(ierr);
8838e2fed03SBarry Smith     }
8848e2fed03SBarry Smith   } else {
8858e2fed03SBarry Smith     ierr = MPI_Send(&nz,1,MPI_INT,0,tag,mat->comm);CHKERRQ(ierr);
8868e2fed03SBarry Smith     ierr = MPI_Send(column_values,nz,MPIU_SCALAR,0,tag,mat->comm);CHKERRQ(ierr);
8878e2fed03SBarry Smith   }
8888e2fed03SBarry Smith   ierr = PetscFree(column_values);CHKERRQ(ierr);
8898e2fed03SBarry Smith   PetscFunctionReturn(0);
8908e2fed03SBarry Smith }
8918e2fed03SBarry Smith 
8928e2fed03SBarry Smith #undef __FUNCT__
8934a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ_ASCIIorDraworSocket"
894b0a32e0cSBarry Smith int MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
895416022c9SBarry Smith {
89644a69424SLois Curfman McInnes   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*)mat->data;
897bfec09a0SHong Zhang   int               ierr,rank = aij->rank,size = aij->size;
8988e2fed03SBarry Smith   PetscTruth        isdraw,isascii,flg,isbinary;
899b0a32e0cSBarry Smith   PetscViewer       sviewer;
900f3ef73ceSBarry Smith   PetscViewerFormat format;
901416022c9SBarry Smith 
9023a40ed3dSBarry Smith   PetscFunctionBegin;
903fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
904b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
9058e2fed03SBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
9060f5bd95cSBarry Smith   if (isascii) {
907b0a32e0cSBarry Smith     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
908456192e2SBarry Smith     if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
9094e220ebcSLois Curfman McInnes       MatInfo info;
9101dab6e02SBarry Smith       ierr = MPI_Comm_rank(mat->comm,&rank);CHKERRQ(ierr);
911888f2ed8SSatish Balay       ierr = MatGetInfo(mat,MAT_LOCAL,&info);CHKERRQ(ierr);
912b0a32e0cSBarry Smith       ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_no_inode",&flg);CHKERRQ(ierr);
9136831982aSBarry Smith       if (flg) {
914b0a32e0cSBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %d nz %d nz alloced %d mem %d, not using I-node routines\n",
915273d9f13SBarry Smith 					      rank,mat->m,(int)info.nz_used,(int)info.nz_allocated,(int)info.memory);CHKERRQ(ierr);
9166831982aSBarry Smith       } else {
917b0a32e0cSBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %d nz %d nz alloced %d mem %d, using I-node routines\n",
918273d9f13SBarry Smith 		    rank,mat->m,(int)info.nz_used,(int)info.nz_allocated,(int)info.memory);CHKERRQ(ierr);
9196831982aSBarry Smith       }
920888f2ed8SSatish Balay       ierr = MatGetInfo(aij->A,MAT_LOCAL,&info);CHKERRQ(ierr);
921b0a32e0cSBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %d \n",rank,(int)info.nz_used);CHKERRQ(ierr);
922888f2ed8SSatish Balay       ierr = MatGetInfo(aij->B,MAT_LOCAL,&info);CHKERRQ(ierr);
923b0a32e0cSBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %d \n",rank,(int)info.nz_used);CHKERRQ(ierr);
924b0a32e0cSBarry Smith       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
925a40aa06bSLois Curfman McInnes       ierr = VecScatterView(aij->Mvctx,viewer);CHKERRQ(ierr);
9263a40ed3dSBarry Smith       PetscFunctionReturn(0);
927fb9695e5SSatish Balay     } else if (format == PETSC_VIEWER_ASCII_INFO) {
9283a40ed3dSBarry Smith       PetscFunctionReturn(0);
9294aedb280SBarry Smith     } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
9303cea93caSBarry Smith #if defined(PETSC_HAVE_SUPERLUDIST) && !defined(PETSC_USE_SINGLE)
9314aedb280SBarry Smith       ierr = MatMPIAIJFactorInfo_SuperLu(mat,viewer);CHKERRQ(ierr);
9324aedb280SBarry Smith #endif
9336ea74821SHong Zhang #if defined(PETSC_HAVE_SPOOLES) && !defined(PETSC_USE_SINGLE)
934a072f7f7SHong Zhang       ierr = MatFactorInfo_Spooles(mat,viewer);CHKERRQ(ierr);
9350473ca23SHong Zhang #endif
9361dfdf1d9SHong Zhang #if defined(PETSC_HAVE_MUMPS) && !defined(PETSC_USE_SINGLE)
9371dfdf1d9SHong Zhang       ierr = MatFactorInfo_MUMPS(mat,viewer);CHKERRQ(ierr);
9381dfdf1d9SHong Zhang #endif
9394aedb280SBarry Smith       PetscFunctionReturn(0);
94008480c60SBarry Smith     }
9418e2fed03SBarry Smith   } else if (isbinary) {
9428e2fed03SBarry Smith     if (size == 1) {
9438e2fed03SBarry Smith       ierr = PetscObjectSetName((PetscObject)aij->A,mat->name);CHKERRQ(ierr);
9448e2fed03SBarry Smith       ierr = MatView(aij->A,viewer);CHKERRQ(ierr);
9458e2fed03SBarry Smith     } else {
9468e2fed03SBarry Smith       ierr = MatView_MPIAIJ_Binary(mat,viewer);CHKERRQ(ierr);
9478e2fed03SBarry Smith     }
9488e2fed03SBarry Smith     PetscFunctionReturn(0);
9490f5bd95cSBarry Smith   } else if (isdraw) {
950b0a32e0cSBarry Smith     PetscDraw  draw;
95119bcc07fSBarry Smith     PetscTruth isnull;
952b0a32e0cSBarry Smith     ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
953b0a32e0cSBarry Smith     ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
95419bcc07fSBarry Smith   }
95519bcc07fSBarry Smith 
95617699dbbSLois Curfman McInnes   if (size == 1) {
957e36acaf3SBarry Smith     ierr = PetscObjectSetName((PetscObject)aij->A,mat->name);CHKERRQ(ierr);
95878b31e54SBarry Smith     ierr = MatView(aij->A,viewer);CHKERRQ(ierr);
9593a40ed3dSBarry Smith   } else {
96095373324SBarry Smith     /* assemble the entire matrix onto first processor. */
96195373324SBarry Smith     Mat         A;
962ec8511deSBarry Smith     Mat_SeqAIJ *Aloc;
963273d9f13SBarry Smith     int         M = mat->M,N = mat->N,m,*ai,*aj,row,*cols,i,*ct;
96487828ca2SBarry Smith     PetscScalar *a;
9652ee70a88SLois Curfman McInnes 
96617699dbbSLois Curfman McInnes     if (!rank) {
96755843e3eSBarry Smith       ierr = MatCreateMPIAIJ(mat->comm,M,N,M,N,0,PETSC_NULL,0,PETSC_NULL,&A);CHKERRQ(ierr);
9683a40ed3dSBarry Smith     } else {
96955843e3eSBarry Smith       ierr = MatCreateMPIAIJ(mat->comm,0,0,M,N,0,PETSC_NULL,0,PETSC_NULL,&A);CHKERRQ(ierr);
97095373324SBarry Smith     }
971b0a32e0cSBarry Smith     PetscLogObjectParent(mat,A);
972416022c9SBarry Smith 
97395373324SBarry Smith     /* copy over the A part */
974ec8511deSBarry Smith     Aloc = (Mat_SeqAIJ*)aij->A->data;
975273d9f13SBarry Smith     m = aij->A->m; ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
97695373324SBarry Smith     row = aij->rstart;
977bfec09a0SHong Zhang     for (i=0; i<ai[m]; i++) {aj[i] += aij->cstart ;}
97895373324SBarry Smith     for (i=0; i<m; i++) {
979416022c9SBarry Smith       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);CHKERRQ(ierr);
98095373324SBarry Smith       row++; a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
98195373324SBarry Smith     }
9822ee70a88SLois Curfman McInnes     aj = Aloc->j;
983bfec09a0SHong Zhang     for (i=0; i<ai[m]; i++) {aj[i] -= aij->cstart;}
98495373324SBarry Smith 
98595373324SBarry Smith     /* copy over the B part */
986ec8511deSBarry Smith     Aloc = (Mat_SeqAIJ*)aij->B->data;
987273d9f13SBarry Smith     m    = aij->B->m;  ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
98895373324SBarry Smith     row  = aij->rstart;
989b0a32e0cSBarry Smith     ierr = PetscMalloc((ai[m]+1)*sizeof(int),&cols);CHKERRQ(ierr);
990b0a32e0cSBarry Smith     ct   = cols;
991bfec09a0SHong Zhang     for (i=0; i<ai[m]; i++) {cols[i] = aij->garray[aj[i]];}
99295373324SBarry Smith     for (i=0; i<m; i++) {
993416022c9SBarry Smith       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);CHKERRQ(ierr);
99495373324SBarry Smith       row++; a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
99595373324SBarry Smith     }
996606d414cSSatish Balay     ierr = PetscFree(ct);CHKERRQ(ierr);
9976d4a8577SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9986d4a8577SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
99955843e3eSBarry Smith     /*
100055843e3eSBarry Smith        Everyone has to call to draw the matrix since the graphics waits are
1001b0a32e0cSBarry Smith        synchronized across all processors that share the PetscDraw object
100255843e3eSBarry Smith     */
1003b0a32e0cSBarry Smith     ierr = PetscViewerGetSingleton(viewer,&sviewer);CHKERRQ(ierr);
1004e03a110bSBarry Smith     if (!rank) {
1005e36acaf3SBarry Smith       ierr = PetscObjectSetName((PetscObject)((Mat_MPIAIJ*)(A->data))->A,mat->name);CHKERRQ(ierr);
10066831982aSBarry Smith       ierr = MatView(((Mat_MPIAIJ*)(A->data))->A,sviewer);CHKERRQ(ierr);
100795373324SBarry Smith     }
1008b0a32e0cSBarry Smith     ierr = PetscViewerRestoreSingleton(viewer,&sviewer);CHKERRQ(ierr);
100978b31e54SBarry Smith     ierr = MatDestroy(A);CHKERRQ(ierr);
101095373324SBarry Smith   }
10113a40ed3dSBarry Smith   PetscFunctionReturn(0);
10121eb62cbbSBarry Smith }
10131eb62cbbSBarry Smith 
10144a2ae208SSatish Balay #undef __FUNCT__
10154a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ"
1016b0a32e0cSBarry Smith int MatView_MPIAIJ(Mat mat,PetscViewer viewer)
1017416022c9SBarry Smith {
1018416022c9SBarry Smith   int        ierr;
10196831982aSBarry Smith   PetscTruth isascii,isdraw,issocket,isbinary;
1020416022c9SBarry Smith 
10213a40ed3dSBarry Smith   PetscFunctionBegin;
1022b0a32e0cSBarry Smith   ierr  = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
1023fb9695e5SSatish Balay   ierr  = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
1024fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
1025b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_SOCKET,&issocket);CHKERRQ(ierr);
10260f5bd95cSBarry Smith   if (isascii || isdraw || isbinary || issocket) {
10277b2a1423SBarry Smith     ierr = MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer);CHKERRQ(ierr);
10285cd90555SBarry Smith   } else {
102929bbc08cSBarry Smith     SETERRQ1(1,"Viewer type %s not supported by MPIAIJ matrices",((PetscObject)viewer)->type_name);
1030416022c9SBarry Smith   }
10313a40ed3dSBarry Smith   PetscFunctionReturn(0);
1032416022c9SBarry Smith }
1033416022c9SBarry Smith 
10341eb62cbbSBarry Smith 
1035c14dc6b6SHong Zhang 
10364a2ae208SSatish Balay #undef __FUNCT__
10374a2ae208SSatish Balay #define __FUNCT__ "MatRelax_MPIAIJ"
1038c14dc6b6SHong Zhang int MatRelax_MPIAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,int its,int lits,Vec xx)
10398a729477SBarry Smith {
104044a69424SLois Curfman McInnes   Mat_MPIAIJ   *mat = (Mat_MPIAIJ*)matin->data;
1041c14dc6b6SHong Zhang   int          ierr;
1042c14dc6b6SHong Zhang   Vec          bb1;
1043a6c309e7SHong Zhang   PetscScalar  mone=-1.0;
10448a729477SBarry Smith 
10453a40ed3dSBarry Smith   PetscFunctionBegin;
104691723122SBarry Smith   if (its <= 0 || lits <= 0) SETERRQ2(PETSC_ERR_ARG_WRONG,"Relaxation requires global its %d and local its %d both positive",its,lits);
1047c14dc6b6SHong Zhang 
1048c14dc6b6SHong Zhang   ierr = VecDuplicate(bb,&bb1);CHKERRQ(ierr);
10492798e883SHong Zhang 
1050c16cb8f2SBarry Smith   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){
1051da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
1052bd3bf7d3SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,lits,xx);CHKERRQ(ierr);
10532798e883SHong Zhang       its--;
1054da3a660dSBarry Smith     }
10552798e883SHong Zhang 
10562798e883SHong Zhang     while (its--) {
10573a40ed3dSBarry Smith       ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10583a40ed3dSBarry Smith       ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10592798e883SHong Zhang 
1060c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
1061c14dc6b6SHong Zhang       ierr = VecScale(&mone,mat->lvec);CHKERRQ(ierr);
1062c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
10632798e883SHong Zhang 
1064c14dc6b6SHong Zhang       /* local sweep */
1065bd3bf7d3SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,lits,xx);
1066c14dc6b6SHong Zhang       CHKERRQ(ierr);
10672798e883SHong Zhang     }
10683a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_FORWARD_SWEEP){
1069da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
1070c14dc6b6SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,PETSC_NULL,xx);CHKERRQ(ierr);
10712798e883SHong Zhang       its--;
1072da3a660dSBarry Smith     }
10732798e883SHong Zhang     while (its--) {
10743a40ed3dSBarry Smith       ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10753a40ed3dSBarry Smith       ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10762798e883SHong Zhang 
1077c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
1078c14dc6b6SHong Zhang       ierr = VecScale(&mone,mat->lvec);CHKERRQ(ierr);
1079c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
1080c14dc6b6SHong Zhang 
1081c14dc6b6SHong Zhang       /* local sweep */
1082a6c309e7SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,PETSC_NULL,xx);
1083c14dc6b6SHong Zhang       CHKERRQ(ierr);
10842798e883SHong Zhang     }
10853a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_BACKWARD_SWEEP){
1086da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
1087c14dc6b6SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,PETSC_NULL,xx);CHKERRQ(ierr);
10882798e883SHong Zhang       its--;
1089da3a660dSBarry Smith     }
10902798e883SHong Zhang     while (its--) {
10912e8a6d31SBarry Smith       ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10922e8a6d31SBarry Smith       ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10932798e883SHong Zhang 
1094c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
1095c14dc6b6SHong Zhang       ierr = VecScale(&mone,mat->lvec);CHKERRQ(ierr);
1096c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
10972798e883SHong Zhang 
1098c14dc6b6SHong Zhang       /* local sweep */
1099a6c309e7SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,PETSC_NULL,xx);
1100c14dc6b6SHong Zhang       CHKERRQ(ierr);
11012798e883SHong Zhang     }
11023a40ed3dSBarry Smith   } else {
110329bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"Parallel SOR not supported");
1104c16cb8f2SBarry Smith   }
1105c14dc6b6SHong Zhang 
1106c14dc6b6SHong Zhang   ierr = VecDestroy(bb1);CHKERRQ(ierr);
11073a40ed3dSBarry Smith   PetscFunctionReturn(0);
11088a729477SBarry Smith }
1109a66be287SLois Curfman McInnes 
11104a2ae208SSatish Balay #undef __FUNCT__
11114a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_MPIAIJ"
11128f6be9afSLois Curfman McInnes int MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info)
1113a66be287SLois Curfman McInnes {
1114a66be287SLois Curfman McInnes   Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data;
1115a66be287SLois Curfman McInnes   Mat        A = mat->A,B = mat->B;
11164e220ebcSLois Curfman McInnes   int        ierr;
1117329f5518SBarry Smith   PetscReal  isend[5],irecv[5];
1118a66be287SLois Curfman McInnes 
11193a40ed3dSBarry Smith   PetscFunctionBegin;
11204e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
11214e220ebcSLois Curfman McInnes   ierr = MatGetInfo(A,MAT_LOCAL,info);CHKERRQ(ierr);
11224e220ebcSLois Curfman McInnes   isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
11234e220ebcSLois Curfman McInnes   isend[3] = info->memory;  isend[4] = info->mallocs;
11244e220ebcSLois Curfman McInnes   ierr = MatGetInfo(B,MAT_LOCAL,info);CHKERRQ(ierr);
11254e220ebcSLois Curfman McInnes   isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
11264e220ebcSLois Curfman McInnes   isend[3] += info->memory;  isend[4] += info->mallocs;
1127a66be287SLois Curfman McInnes   if (flag == MAT_LOCAL) {
11284e220ebcSLois Curfman McInnes     info->nz_used      = isend[0];
11294e220ebcSLois Curfman McInnes     info->nz_allocated = isend[1];
11304e220ebcSLois Curfman McInnes     info->nz_unneeded  = isend[2];
11314e220ebcSLois Curfman McInnes     info->memory       = isend[3];
11324e220ebcSLois Curfman McInnes     info->mallocs      = isend[4];
1133a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_MAX) {
1134d7d1e502SBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_MAX,matin->comm);CHKERRQ(ierr);
11354e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
11364e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
11374e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
11384e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
11394e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1140a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_SUM) {
1141d7d1e502SBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_SUM,matin->comm);CHKERRQ(ierr);
11424e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
11434e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
11444e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
11454e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
11464e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1147a66be287SLois Curfman McInnes   }
11484e220ebcSLois Curfman McInnes   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
11494e220ebcSLois Curfman McInnes   info->fill_ratio_needed = 0;
11504e220ebcSLois Curfman McInnes   info->factor_mallocs    = 0;
1151273d9f13SBarry Smith   info->rows_global       = (double)matin->M;
1152273d9f13SBarry Smith   info->columns_global    = (double)matin->N;
1153273d9f13SBarry Smith   info->rows_local        = (double)matin->m;
1154273d9f13SBarry Smith   info->columns_local     = (double)matin->N;
11554e220ebcSLois Curfman McInnes 
11563a40ed3dSBarry Smith   PetscFunctionReturn(0);
1157a66be287SLois Curfman McInnes }
1158a66be287SLois Curfman McInnes 
11594a2ae208SSatish Balay #undef __FUNCT__
11604a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_MPIAIJ"
11618f6be9afSLois Curfman McInnes int MatSetOption_MPIAIJ(Mat A,MatOption op)
1162c74985f6SBarry Smith {
1163c0bbcb79SLois Curfman McInnes   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
11641dee9f54SBarry Smith   int        ierr;
1165c74985f6SBarry Smith 
11663a40ed3dSBarry Smith   PetscFunctionBegin;
116712c028f9SKris Buschelman   switch (op) {
116812c028f9SKris Buschelman   case MAT_NO_NEW_NONZERO_LOCATIONS:
116912c028f9SKris Buschelman   case MAT_YES_NEW_NONZERO_LOCATIONS:
117012c028f9SKris Buschelman   case MAT_COLUMNS_UNSORTED:
117112c028f9SKris Buschelman   case MAT_COLUMNS_SORTED:
117212c028f9SKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
117312c028f9SKris Buschelman   case MAT_KEEP_ZEROED_ROWS:
117412c028f9SKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
117512c028f9SKris Buschelman   case MAT_USE_INODES:
117612c028f9SKris Buschelman   case MAT_DO_NOT_USE_INODES:
117712c028f9SKris Buschelman   case MAT_IGNORE_ZERO_ENTRIES:
11781dee9f54SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
11791dee9f54SBarry Smith     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
118012c028f9SKris Buschelman     break;
118112c028f9SKris Buschelman   case MAT_ROW_ORIENTED:
11827c922b88SBarry Smith     a->roworiented = PETSC_TRUE;
11831dee9f54SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
11841dee9f54SBarry Smith     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
118512c028f9SKris Buschelman     break;
118612c028f9SKris Buschelman   case MAT_ROWS_SORTED:
118712c028f9SKris Buschelman   case MAT_ROWS_UNSORTED:
118812c028f9SKris Buschelman   case MAT_YES_NEW_DIAGONALS:
1189b0a32e0cSBarry Smith     PetscLogInfo(A,"MatSetOption_MPIAIJ:Option ignored\n");
119012c028f9SKris Buschelman     break;
119112c028f9SKris Buschelman   case MAT_COLUMN_ORIENTED:
11927c922b88SBarry Smith     a->roworiented = PETSC_FALSE;
11931dee9f54SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
11941dee9f54SBarry Smith     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
119512c028f9SKris Buschelman     break;
119612c028f9SKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
11977c922b88SBarry Smith     a->donotstash = PETSC_TRUE;
119812c028f9SKris Buschelman     break;
119912c028f9SKris Buschelman   case MAT_NO_NEW_DIAGONALS:
120029bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"MAT_NO_NEW_DIAGONALS");
120112c028f9SKris Buschelman   default:
120229bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"unknown option");
12033a40ed3dSBarry Smith   }
12043a40ed3dSBarry Smith   PetscFunctionReturn(0);
1205c74985f6SBarry Smith }
1206c74985f6SBarry Smith 
12074a2ae208SSatish Balay #undef __FUNCT__
12084a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_MPIAIJ"
120987828ca2SBarry Smith int MatGetRow_MPIAIJ(Mat matin,int row,int *nz,int **idx,PetscScalar **v)
121039e00950SLois Curfman McInnes {
1211154123eaSLois Curfman McInnes   Mat_MPIAIJ   *mat = (Mat_MPIAIJ*)matin->data;
121287828ca2SBarry Smith   PetscScalar  *vworkA,*vworkB,**pvA,**pvB,*v_p;
1213154123eaSLois Curfman McInnes   int          i,ierr,*cworkA,*cworkB,**pcA,**pcB,cstart = mat->cstart;
1214154123eaSLois Curfman McInnes   int          nztot,nzA,nzB,lrow,rstart = mat->rstart,rend = mat->rend;
121570f0671dSBarry Smith   int          *cmap,*idx_p;
121639e00950SLois Curfman McInnes 
12173a40ed3dSBarry Smith   PetscFunctionBegin;
121829bbc08cSBarry Smith   if (mat->getrowactive == PETSC_TRUE) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Already active");
12197a0afa10SBarry Smith   mat->getrowactive = PETSC_TRUE;
12207a0afa10SBarry Smith 
122170f0671dSBarry Smith   if (!mat->rowvalues && (idx || v)) {
12227a0afa10SBarry Smith     /*
12237a0afa10SBarry Smith         allocate enough space to hold information from the longest row.
12247a0afa10SBarry Smith     */
12257a0afa10SBarry Smith     Mat_SeqAIJ *Aa = (Mat_SeqAIJ*)mat->A->data,*Ba = (Mat_SeqAIJ*)mat->B->data;
1226273d9f13SBarry Smith     int     max = 1,tmp;
1227273d9f13SBarry Smith     for (i=0; i<matin->m; i++) {
12287a0afa10SBarry Smith       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
12297a0afa10SBarry Smith       if (max < tmp) { max = tmp; }
12307a0afa10SBarry Smith     }
123187828ca2SBarry Smith     ierr = PetscMalloc(max*(sizeof(int)+sizeof(PetscScalar)),&mat->rowvalues);CHKERRQ(ierr);
12327a0afa10SBarry Smith     mat->rowindices = (int*)(mat->rowvalues + max);
12337a0afa10SBarry Smith   }
12347a0afa10SBarry Smith 
123529bbc08cSBarry Smith   if (row < rstart || row >= rend) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Only local rows")
1236abc0e9e4SLois Curfman McInnes   lrow = row - rstart;
123739e00950SLois Curfman McInnes 
1238154123eaSLois Curfman McInnes   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1239154123eaSLois Curfman McInnes   if (!v)   {pvA = 0; pvB = 0;}
1240154123eaSLois Curfman McInnes   if (!idx) {pcA = 0; if (!v) pcB = 0;}
1241f830108cSBarry Smith   ierr = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1242f830108cSBarry Smith   ierr = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
1243154123eaSLois Curfman McInnes   nztot = nzA + nzB;
1244154123eaSLois Curfman McInnes 
124570f0671dSBarry Smith   cmap  = mat->garray;
1246154123eaSLois Curfman McInnes   if (v  || idx) {
1247154123eaSLois Curfman McInnes     if (nztot) {
1248154123eaSLois Curfman McInnes       /* Sort by increasing column numbers, assuming A and B already sorted */
124970f0671dSBarry Smith       int imark = -1;
1250154123eaSLois Curfman McInnes       if (v) {
125170f0671dSBarry Smith         *v = v_p = mat->rowvalues;
125239e00950SLois Curfman McInnes         for (i=0; i<nzB; i++) {
125370f0671dSBarry Smith           if (cmap[cworkB[i]] < cstart)   v_p[i] = vworkB[i];
1254154123eaSLois Curfman McInnes           else break;
1255154123eaSLois Curfman McInnes         }
1256154123eaSLois Curfman McInnes         imark = i;
125770f0671dSBarry Smith         for (i=0; i<nzA; i++)     v_p[imark+i] = vworkA[i];
125870f0671dSBarry Smith         for (i=imark; i<nzB; i++) v_p[nzA+i]   = vworkB[i];
1259154123eaSLois Curfman McInnes       }
1260154123eaSLois Curfman McInnes       if (idx) {
126170f0671dSBarry Smith         *idx = idx_p = mat->rowindices;
126270f0671dSBarry Smith         if (imark > -1) {
126370f0671dSBarry Smith           for (i=0; i<imark; i++) {
126470f0671dSBarry Smith             idx_p[i] = cmap[cworkB[i]];
126570f0671dSBarry Smith           }
126670f0671dSBarry Smith         } else {
1267154123eaSLois Curfman McInnes           for (i=0; i<nzB; i++) {
126870f0671dSBarry Smith             if (cmap[cworkB[i]] < cstart)   idx_p[i] = cmap[cworkB[i]];
1269154123eaSLois Curfman McInnes             else break;
1270154123eaSLois Curfman McInnes           }
1271154123eaSLois Curfman McInnes           imark = i;
127270f0671dSBarry Smith         }
127370f0671dSBarry Smith         for (i=0; i<nzA; i++)     idx_p[imark+i] = cstart + cworkA[i];
127470f0671dSBarry Smith         for (i=imark; i<nzB; i++) idx_p[nzA+i]   = cmap[cworkB[i]];
127539e00950SLois Curfman McInnes       }
12763f97c4b0SBarry Smith     } else {
12771ca473b0SSatish Balay       if (idx) *idx = 0;
12781ca473b0SSatish Balay       if (v)   *v   = 0;
12791ca473b0SSatish Balay     }
1280154123eaSLois Curfman McInnes   }
128139e00950SLois Curfman McInnes   *nz = nztot;
1282f830108cSBarry Smith   ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1283f830108cSBarry Smith   ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
12843a40ed3dSBarry Smith   PetscFunctionReturn(0);
128539e00950SLois Curfman McInnes }
128639e00950SLois Curfman McInnes 
12874a2ae208SSatish Balay #undef __FUNCT__
12884a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_MPIAIJ"
128987828ca2SBarry Smith int MatRestoreRow_MPIAIJ(Mat mat,int row,int *nz,int **idx,PetscScalar **v)
129039e00950SLois Curfman McInnes {
12917a0afa10SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
12923a40ed3dSBarry Smith 
12933a40ed3dSBarry Smith   PetscFunctionBegin;
12947a0afa10SBarry Smith   if (aij->getrowactive == PETSC_FALSE) {
129529bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"MatGetRow not called");
12967a0afa10SBarry Smith   }
12977a0afa10SBarry Smith   aij->getrowactive = PETSC_FALSE;
12983a40ed3dSBarry Smith   PetscFunctionReturn(0);
129939e00950SLois Curfman McInnes }
130039e00950SLois Curfman McInnes 
13014a2ae208SSatish Balay #undef __FUNCT__
13024a2ae208SSatish Balay #define __FUNCT__ "MatNorm_MPIAIJ"
1303329f5518SBarry Smith int MatNorm_MPIAIJ(Mat mat,NormType type,PetscReal *norm)
1304855ac2c5SLois Curfman McInnes {
1305855ac2c5SLois Curfman McInnes   Mat_MPIAIJ   *aij = (Mat_MPIAIJ*)mat->data;
1306ec8511deSBarry Smith   Mat_SeqAIJ   *amat = (Mat_SeqAIJ*)aij->A->data,*bmat = (Mat_SeqAIJ*)aij->B->data;
1307bfec09a0SHong Zhang   int          ierr,i,j,cstart = aij->cstart;
1308329f5518SBarry Smith   PetscReal    sum = 0.0;
130987828ca2SBarry Smith   PetscScalar  *v;
131004ca555eSLois Curfman McInnes 
13113a40ed3dSBarry Smith   PetscFunctionBegin;
131217699dbbSLois Curfman McInnes   if (aij->size == 1) {
131314183eadSLois Curfman McInnes     ierr =  MatNorm(aij->A,type,norm);CHKERRQ(ierr);
131437fa93a5SLois Curfman McInnes   } else {
131504ca555eSLois Curfman McInnes     if (type == NORM_FROBENIUS) {
131604ca555eSLois Curfman McInnes       v = amat->a;
131704ca555eSLois Curfman McInnes       for (i=0; i<amat->nz; i++) {
1318aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
1319329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
132004ca555eSLois Curfman McInnes #else
132104ca555eSLois Curfman McInnes         sum += (*v)*(*v); v++;
132204ca555eSLois Curfman McInnes #endif
132304ca555eSLois Curfman McInnes       }
132404ca555eSLois Curfman McInnes       v = bmat->a;
132504ca555eSLois Curfman McInnes       for (i=0; i<bmat->nz; i++) {
1326aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
1327329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
132804ca555eSLois Curfman McInnes #else
132904ca555eSLois Curfman McInnes         sum += (*v)*(*v); v++;
133004ca555eSLois Curfman McInnes #endif
133104ca555eSLois Curfman McInnes       }
1332d7d1e502SBarry Smith       ierr = MPI_Allreduce(&sum,norm,1,MPIU_REAL,MPI_SUM,mat->comm);CHKERRQ(ierr);
133304ca555eSLois Curfman McInnes       *norm = sqrt(*norm);
13343a40ed3dSBarry Smith     } else if (type == NORM_1) { /* max column norm */
1335329f5518SBarry Smith       PetscReal *tmp,*tmp2;
133604ca555eSLois Curfman McInnes       int    *jj,*garray = aij->garray;
1337b0a32e0cSBarry Smith       ierr = PetscMalloc((mat->N+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1338b0a32e0cSBarry Smith       ierr = PetscMalloc((mat->N+1)*sizeof(PetscReal),&tmp2);CHKERRQ(ierr);
1339273d9f13SBarry Smith       ierr = PetscMemzero(tmp,mat->N*sizeof(PetscReal));CHKERRQ(ierr);
134004ca555eSLois Curfman McInnes       *norm = 0.0;
134104ca555eSLois Curfman McInnes       v = amat->a; jj = amat->j;
134204ca555eSLois Curfman McInnes       for (j=0; j<amat->nz; j++) {
1343bfec09a0SHong Zhang         tmp[cstart + *jj++ ] += PetscAbsScalar(*v);  v++;
134404ca555eSLois Curfman McInnes       }
134504ca555eSLois Curfman McInnes       v = bmat->a; jj = bmat->j;
134604ca555eSLois Curfman McInnes       for (j=0; j<bmat->nz; j++) {
1347bfec09a0SHong Zhang         tmp[garray[*jj++]] += PetscAbsScalar(*v); v++;
134804ca555eSLois Curfman McInnes       }
1349d7d1e502SBarry Smith       ierr = MPI_Allreduce(tmp,tmp2,mat->N,MPIU_REAL,MPI_SUM,mat->comm);CHKERRQ(ierr);
1350273d9f13SBarry Smith       for (j=0; j<mat->N; j++) {
135104ca555eSLois Curfman McInnes         if (tmp2[j] > *norm) *norm = tmp2[j];
135204ca555eSLois Curfman McInnes       }
1353606d414cSSatish Balay       ierr = PetscFree(tmp);CHKERRQ(ierr);
1354606d414cSSatish Balay       ierr = PetscFree(tmp2);CHKERRQ(ierr);
13553a40ed3dSBarry Smith     } else if (type == NORM_INFINITY) { /* max row norm */
1356329f5518SBarry Smith       PetscReal ntemp = 0.0;
1357273d9f13SBarry Smith       for (j=0; j<aij->A->m; j++) {
1358bfec09a0SHong Zhang         v = amat->a + amat->i[j];
135904ca555eSLois Curfman McInnes         sum = 0.0;
136004ca555eSLois Curfman McInnes         for (i=0; i<amat->i[j+1]-amat->i[j]; i++) {
1361cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
136204ca555eSLois Curfman McInnes         }
1363bfec09a0SHong Zhang         v = bmat->a + bmat->i[j];
136404ca555eSLois Curfman McInnes         for (i=0; i<bmat->i[j+1]-bmat->i[j]; i++) {
1365cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
136604ca555eSLois Curfman McInnes         }
1367515d9167SLois Curfman McInnes         if (sum > ntemp) ntemp = sum;
136804ca555eSLois Curfman McInnes       }
1369d7d1e502SBarry Smith       ierr = MPI_Allreduce(&ntemp,norm,1,MPIU_REAL,MPI_MAX,mat->comm);CHKERRQ(ierr);
1370ca161407SBarry Smith     } else {
137129bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"No support for two norm");
137204ca555eSLois Curfman McInnes     }
137337fa93a5SLois Curfman McInnes   }
13743a40ed3dSBarry Smith   PetscFunctionReturn(0);
1375855ac2c5SLois Curfman McInnes }
1376855ac2c5SLois Curfman McInnes 
13774a2ae208SSatish Balay #undef __FUNCT__
13784a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_MPIAIJ"
13798f6be9afSLois Curfman McInnes int MatTranspose_MPIAIJ(Mat A,Mat *matout)
1380b7c46309SBarry Smith {
1381b7c46309SBarry Smith   Mat_MPIAIJ   *a = (Mat_MPIAIJ*)A->data;
1382dbb450caSBarry Smith   Mat_SeqAIJ   *Aloc = (Mat_SeqAIJ*)a->A->data;
1383bfec09a0SHong Zhang   int          ierr;
1384273d9f13SBarry Smith   int          M = A->M,N = A->N,m,*ai,*aj,row,*cols,i,*ct;
13853a40ed3dSBarry Smith   Mat          B;
138687828ca2SBarry Smith   PetscScalar  *array;
1387b7c46309SBarry Smith 
13883a40ed3dSBarry Smith   PetscFunctionBegin;
13897c922b88SBarry Smith   if (!matout && M != N) {
139029bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1391d4bb536fSBarry Smith   }
1392d4bb536fSBarry Smith 
1393273d9f13SBarry Smith   ierr = MatCreateMPIAIJ(A->comm,A->n,A->m,N,M,0,PETSC_NULL,0,PETSC_NULL,&B);CHKERRQ(ierr);
1394b7c46309SBarry Smith 
1395b7c46309SBarry Smith   /* copy over the A part */
1396ec8511deSBarry Smith   Aloc = (Mat_SeqAIJ*)a->A->data;
1397273d9f13SBarry Smith   m = a->A->m; ai = Aloc->i; aj = Aloc->j; array = Aloc->a;
1398b7c46309SBarry Smith   row = a->rstart;
1399bfec09a0SHong Zhang   for (i=0; i<ai[m]; i++) {aj[i] += a->cstart ;}
1400b7c46309SBarry Smith   for (i=0; i<m; i++) {
1401416022c9SBarry Smith     ierr = MatSetValues(B,ai[i+1]-ai[i],aj,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
1402b7c46309SBarry Smith     row++; array += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
1403b7c46309SBarry Smith   }
1404b7c46309SBarry Smith   aj = Aloc->j;
1405bfec09a0SHong Zhang   for (i=0; i<ai[m]; i++) {aj[i] -= a->cstart ;}
1406b7c46309SBarry Smith 
1407b7c46309SBarry Smith   /* copy over the B part */
1408ec8511deSBarry Smith   Aloc = (Mat_SeqAIJ*)a->B->data;
1409273d9f13SBarry Smith   m = a->B->m;  ai = Aloc->i; aj = Aloc->j; array = Aloc->a;
1410b7c46309SBarry Smith   row  = a->rstart;
1411bfec09a0SHong Zhang   ierr = PetscMalloc((1+ai[m])*sizeof(int),&cols);CHKERRQ(ierr);
1412b0a32e0cSBarry Smith   ct   = cols;
1413bfec09a0SHong Zhang   for (i=0; i<ai[m]; i++) {cols[i] = a->garray[aj[i]];}
1414b7c46309SBarry Smith   for (i=0; i<m; i++) {
1415416022c9SBarry Smith     ierr = MatSetValues(B,ai[i+1]-ai[i],cols,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
1416b7c46309SBarry Smith     row++; array += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
1417b7c46309SBarry Smith   }
1418606d414cSSatish Balay   ierr = PetscFree(ct);CHKERRQ(ierr);
14196d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14206d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14217c922b88SBarry Smith   if (matout) {
14220de55854SLois Curfman McInnes     *matout = B;
14230de55854SLois Curfman McInnes   } else {
1424273d9f13SBarry Smith     ierr = MatHeaderCopy(A,B);CHKERRQ(ierr);
14250de55854SLois Curfman McInnes   }
14263a40ed3dSBarry Smith   PetscFunctionReturn(0);
1427b7c46309SBarry Smith }
1428b7c46309SBarry Smith 
14294a2ae208SSatish Balay #undef __FUNCT__
14304a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_MPIAIJ"
14314b967eb1SSatish Balay int MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr)
1432a008b906SSatish Balay {
14334b967eb1SSatish Balay   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
14344b967eb1SSatish Balay   Mat        a = aij->A,b = aij->B;
1435a008b906SSatish Balay   int        ierr,s1,s2,s3;
1436a008b906SSatish Balay 
14373a40ed3dSBarry Smith   PetscFunctionBegin;
14384b967eb1SSatish Balay   ierr = MatGetLocalSize(mat,&s2,&s3);CHKERRQ(ierr);
14394b967eb1SSatish Balay   if (rr) {
1440e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr);
144129bbc08cSBarry Smith     if (s1!=s3) SETERRQ(PETSC_ERR_ARG_SIZ,"right vector non-conforming local size");
14424b967eb1SSatish Balay     /* Overlap communication with computation. */
144343a90d84SBarry Smith     ierr = VecScatterBegin(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx);CHKERRQ(ierr);
1444a008b906SSatish Balay   }
14454b967eb1SSatish Balay   if (ll) {
1446e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr);
144729bbc08cSBarry Smith     if (s1!=s2) SETERRQ(PETSC_ERR_ARG_SIZ,"left vector non-conforming local size");
1448f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,ll,0);CHKERRQ(ierr);
14494b967eb1SSatish Balay   }
14504b967eb1SSatish Balay   /* scale  the diagonal block */
1451f830108cSBarry Smith   ierr = (*a->ops->diagonalscale)(a,ll,rr);CHKERRQ(ierr);
14524b967eb1SSatish Balay 
14534b967eb1SSatish Balay   if (rr) {
14544b967eb1SSatish Balay     /* Do a scatter end and then right scale the off-diagonal block */
145543a90d84SBarry Smith     ierr = VecScatterEnd(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx);CHKERRQ(ierr);
1456f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,0,aij->lvec);CHKERRQ(ierr);
14574b967eb1SSatish Balay   }
14584b967eb1SSatish Balay 
14593a40ed3dSBarry Smith   PetscFunctionReturn(0);
1460a008b906SSatish Balay }
1461a008b906SSatish Balay 
1462a008b906SSatish Balay 
14634a2ae208SSatish Balay #undef __FUNCT__
14644a2ae208SSatish Balay #define __FUNCT__ "MatPrintHelp_MPIAIJ"
14658f6be9afSLois Curfman McInnes int MatPrintHelp_MPIAIJ(Mat A)
1466682d7d0cSBarry Smith {
1467682d7d0cSBarry Smith   Mat_MPIAIJ *a   = (Mat_MPIAIJ*)A->data;
14683a40ed3dSBarry Smith   int        ierr;
1469682d7d0cSBarry Smith 
14703a40ed3dSBarry Smith   PetscFunctionBegin;
14713a40ed3dSBarry Smith   if (!a->rank) {
14723a40ed3dSBarry Smith     ierr = MatPrintHelp_SeqAIJ(a->A);CHKERRQ(ierr);
14733a40ed3dSBarry Smith   }
14743a40ed3dSBarry Smith   PetscFunctionReturn(0);
1475682d7d0cSBarry Smith }
1476682d7d0cSBarry Smith 
14774a2ae208SSatish Balay #undef __FUNCT__
14784a2ae208SSatish Balay #define __FUNCT__ "MatGetBlockSize_MPIAIJ"
14798f6be9afSLois Curfman McInnes int MatGetBlockSize_MPIAIJ(Mat A,int *bs)
14805a838052SSatish Balay {
14813a40ed3dSBarry Smith   PetscFunctionBegin;
14825a838052SSatish Balay   *bs = 1;
14833a40ed3dSBarry Smith   PetscFunctionReturn(0);
14845a838052SSatish Balay }
14854a2ae208SSatish Balay #undef __FUNCT__
14864a2ae208SSatish Balay #define __FUNCT__ "MatSetUnfactored_MPIAIJ"
14878f6be9afSLois Curfman McInnes int MatSetUnfactored_MPIAIJ(Mat A)
1488bb5a7306SBarry Smith {
1489bb5a7306SBarry Smith   Mat_MPIAIJ *a   = (Mat_MPIAIJ*)A->data;
1490bb5a7306SBarry Smith   int        ierr;
14913a40ed3dSBarry Smith 
14923a40ed3dSBarry Smith   PetscFunctionBegin;
1493bb5a7306SBarry Smith   ierr = MatSetUnfactored(a->A);CHKERRQ(ierr);
14943a40ed3dSBarry Smith   PetscFunctionReturn(0);
1495bb5a7306SBarry Smith }
1496bb5a7306SBarry Smith 
14974a2ae208SSatish Balay #undef __FUNCT__
14984a2ae208SSatish Balay #define __FUNCT__ "MatEqual_MPIAIJ"
1499d4bb536fSBarry Smith int MatEqual_MPIAIJ(Mat A,Mat B,PetscTruth *flag)
1500d4bb536fSBarry Smith {
1501d4bb536fSBarry Smith   Mat_MPIAIJ *matB = (Mat_MPIAIJ*)B->data,*matA = (Mat_MPIAIJ*)A->data;
1502d4bb536fSBarry Smith   Mat        a,b,c,d;
1503d4bb536fSBarry Smith   PetscTruth flg;
1504d4bb536fSBarry Smith   int        ierr;
1505d4bb536fSBarry Smith 
15063a40ed3dSBarry Smith   PetscFunctionBegin;
1507d4bb536fSBarry Smith   a = matA->A; b = matA->B;
1508d4bb536fSBarry Smith   c = matB->A; d = matB->B;
1509d4bb536fSBarry Smith 
1510d4bb536fSBarry Smith   ierr = MatEqual(a,c,&flg);CHKERRQ(ierr);
1511d4bb536fSBarry Smith   if (flg == PETSC_TRUE) {
1512d4bb536fSBarry Smith     ierr = MatEqual(b,d,&flg);CHKERRQ(ierr);
1513d4bb536fSBarry Smith   }
1514ca161407SBarry Smith   ierr = MPI_Allreduce(&flg,flag,1,MPI_INT,MPI_LAND,A->comm);CHKERRQ(ierr);
15153a40ed3dSBarry Smith   PetscFunctionReturn(0);
1516d4bb536fSBarry Smith }
1517d4bb536fSBarry Smith 
15184a2ae208SSatish Balay #undef __FUNCT__
15194a2ae208SSatish Balay #define __FUNCT__ "MatCopy_MPIAIJ"
1520cb5b572fSBarry Smith int MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str)
1521cb5b572fSBarry Smith {
1522cb5b572fSBarry Smith   int        ierr;
1523cb5b572fSBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;
1524cb5b572fSBarry Smith   Mat_MPIAIJ *b = (Mat_MPIAIJ *)B->data;
1525cb5b572fSBarry Smith 
1526cb5b572fSBarry Smith   PetscFunctionBegin;
152733f4a19fSKris Buschelman   /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
152833f4a19fSKris Buschelman   if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) {
1529cb5b572fSBarry Smith     /* because of the column compression in the off-processor part of the matrix a->B,
1530cb5b572fSBarry Smith        the number of columns in a->B and b->B may be different, hence we cannot call
1531cb5b572fSBarry Smith        the MatCopy() directly on the two parts. If need be, we can provide a more
1532cb5b572fSBarry Smith        efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices
1533cb5b572fSBarry Smith        then copying the submatrices */
1534cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1535cb5b572fSBarry Smith   } else {
1536cb5b572fSBarry Smith     ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr);
1537cb5b572fSBarry Smith     ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr);
1538cb5b572fSBarry Smith   }
1539cb5b572fSBarry Smith   PetscFunctionReturn(0);
1540cb5b572fSBarry Smith }
1541cb5b572fSBarry Smith 
15424a2ae208SSatish Balay #undef __FUNCT__
15434a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_MPIAIJ"
1544273d9f13SBarry Smith int MatSetUpPreallocation_MPIAIJ(Mat A)
1545273d9f13SBarry Smith {
1546273d9f13SBarry Smith   int        ierr;
1547273d9f13SBarry Smith 
1548273d9f13SBarry Smith   PetscFunctionBegin;
1549273d9f13SBarry Smith   ierr =  MatMPIAIJSetPreallocation(A,PETSC_DEFAULT,0,PETSC_DEFAULT,0);CHKERRQ(ierr);
1550273d9f13SBarry Smith   PetscFunctionReturn(0);
1551273d9f13SBarry Smith }
1552273d9f13SBarry Smith 
1553ca44d042SBarry Smith EXTERN int MatDuplicate_MPIAIJ(Mat,MatDuplicateOption,Mat *);
1554ca44d042SBarry Smith EXTERN int MatIncreaseOverlap_MPIAIJ(Mat,int,IS *,int);
1555ca44d042SBarry Smith EXTERN int MatFDColoringCreate_MPIAIJ(Mat,ISColoring,MatFDColoring);
1556*268466fbSBarry Smith EXTERN int MatGetSubMatrices_MPIAIJ (Mat,int,const IS[],const IS[],MatReuse,Mat *[]);
1557ca44d042SBarry Smith EXTERN int MatGetSubMatrix_MPIAIJ (Mat,IS,IS,int,MatReuse,Mat *);
155887828ca2SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
1559b380c88cSHong Zhang EXTERN int MatLUFactorSymbolic_MPIAIJ_TFS(Mat,IS,IS,MatFactorInfo*,Mat*);
1560b9617806SBarry Smith #endif
156100e6dbe6SBarry Smith 
1562ac90fabeSBarry Smith #include "petscblaslapack.h"
1563*268466fbSBarry Smith extern int MatAXPY_SeqAIJ(const PetscScalar[],Mat,Mat,MatStructure);
1564ac90fabeSBarry Smith #undef __FUNCT__
1565ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_MPIAIJ"
1566*268466fbSBarry Smith int MatAXPY_MPIAIJ(const PetscScalar a[],Mat X,Mat Y,MatStructure str)
1567ac90fabeSBarry Smith {
156824f910e3SHong Zhang   int        ierr,one=1,i;
1569ac90fabeSBarry Smith   Mat_MPIAIJ *xx = (Mat_MPIAIJ *)X->data,*yy = (Mat_MPIAIJ *)Y->data;
1570ac90fabeSBarry Smith   Mat_SeqAIJ *x,*y;
1571ac90fabeSBarry Smith 
1572ac90fabeSBarry Smith   PetscFunctionBegin;
1573ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
1574ac90fabeSBarry Smith     x = (Mat_SeqAIJ *)xx->A->data;
1575ac90fabeSBarry Smith     y = (Mat_SeqAIJ *)yy->A->data;
1576*268466fbSBarry Smith     BLaxpy_(&x->nz,(PetscScalar*)a,x->a,&one,y->a,&one);
1577ac90fabeSBarry Smith     x = (Mat_SeqAIJ *)xx->B->data;
1578ac90fabeSBarry Smith     y = (Mat_SeqAIJ *)yy->B->data;
1579*268466fbSBarry Smith     BLaxpy_(&x->nz,(PetscScalar*)a,x->a,&one,y->a,&one);
1580a30b2313SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) {
1581c537a176SHong Zhang     ierr = MatAXPY_SeqAIJ(a,xx->A,yy->A,str);CHKERRQ(ierr);
1582c537a176SHong Zhang 
1583c537a176SHong Zhang     x = (Mat_SeqAIJ *)xx->B->data;
1584a30b2313SHong Zhang     y = (Mat_SeqAIJ *)yy->B->data;
1585a30b2313SHong Zhang     if (y->xtoy && y->XtoY != xx->B) {
1586a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
1587a30b2313SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
1588c537a176SHong Zhang     }
1589a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
159024f910e3SHong Zhang       ierr = MatAXPYGetxtoy_Private(xx->B->m,x->i,x->j,xx->garray,y->i,y->j,yy->garray,&y->xtoy);CHKERRQ(ierr);
1591a30b2313SHong Zhang       y->XtoY = xx->B;
1592c537a176SHong Zhang     }
1593a30b2313SHong Zhang     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += (*a)*(x->a[i]);
1594ac90fabeSBarry Smith   } else {
1595ac90fabeSBarry Smith     ierr = MatAXPY_Basic(a,X,Y,str);CHKERRQ(ierr);
1596ac90fabeSBarry Smith   }
1597ac90fabeSBarry Smith   PetscFunctionReturn(0);
1598ac90fabeSBarry Smith }
1599ac90fabeSBarry Smith 
16008a729477SBarry Smith /* -------------------------------------------------------------------*/
1601cda55fadSBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ,
1602cda55fadSBarry Smith        MatGetRow_MPIAIJ,
1603cda55fadSBarry Smith        MatRestoreRow_MPIAIJ,
1604cda55fadSBarry Smith        MatMult_MPIAIJ,
1605cda55fadSBarry Smith        MatMultAdd_MPIAIJ,
16067c922b88SBarry Smith        MatMultTranspose_MPIAIJ,
16077c922b88SBarry Smith        MatMultTransposeAdd_MPIAIJ,
1608cda55fadSBarry Smith        0,
1609cda55fadSBarry Smith        0,
1610cda55fadSBarry Smith        0,
1611cda55fadSBarry Smith        0,
1612cda55fadSBarry Smith        0,
1613cda55fadSBarry Smith        0,
161444a69424SLois Curfman McInnes        MatRelax_MPIAIJ,
1615b7c46309SBarry Smith        MatTranspose_MPIAIJ,
1616cda55fadSBarry Smith        MatGetInfo_MPIAIJ,
1617cda55fadSBarry Smith        MatEqual_MPIAIJ,
1618cda55fadSBarry Smith        MatGetDiagonal_MPIAIJ,
1619cda55fadSBarry Smith        MatDiagonalScale_MPIAIJ,
1620cda55fadSBarry Smith        MatNorm_MPIAIJ,
1621cda55fadSBarry Smith        MatAssemblyBegin_MPIAIJ,
1622cda55fadSBarry Smith        MatAssemblyEnd_MPIAIJ,
16231eb62cbbSBarry Smith        0,
1624cda55fadSBarry Smith        MatSetOption_MPIAIJ,
1625cda55fadSBarry Smith        MatZeroEntries_MPIAIJ,
1626cda55fadSBarry Smith        MatZeroRows_MPIAIJ,
162787828ca2SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
1628b9617806SBarry Smith        MatLUFactorSymbolic_MPIAIJ_TFS,
1629b9617806SBarry Smith #else
1630cda55fadSBarry Smith        0,
1631b9617806SBarry Smith #endif
1632cda55fadSBarry Smith        0,
1633cda55fadSBarry Smith        0,
1634cda55fadSBarry Smith        0,
1635273d9f13SBarry Smith        MatSetUpPreallocation_MPIAIJ,
1636cda55fadSBarry Smith        0,
1637cda55fadSBarry Smith        0,
1638cda55fadSBarry Smith        0,
1639cda55fadSBarry Smith        0,
16402e8a6d31SBarry Smith        MatDuplicate_MPIAIJ,
1641cda55fadSBarry Smith        0,
1642cda55fadSBarry Smith        0,
1643cda55fadSBarry Smith        0,
1644cda55fadSBarry Smith        0,
1645ac90fabeSBarry Smith        MatAXPY_MPIAIJ,
1646cda55fadSBarry Smith        MatGetSubMatrices_MPIAIJ,
1647cda55fadSBarry Smith        MatIncreaseOverlap_MPIAIJ,
1648cda55fadSBarry Smith        MatGetValues_MPIAIJ,
1649cb5b572fSBarry Smith        MatCopy_MPIAIJ,
1650052efed2SBarry Smith        MatPrintHelp_MPIAIJ,
1651cda55fadSBarry Smith        MatScale_MPIAIJ,
1652cda55fadSBarry Smith        0,
1653cda55fadSBarry Smith        0,
1654cda55fadSBarry Smith        0,
1655cda55fadSBarry Smith        MatGetBlockSize_MPIAIJ,
1656cda55fadSBarry Smith        0,
1657cda55fadSBarry Smith        0,
1658cda55fadSBarry Smith        0,
1659cda55fadSBarry Smith        0,
1660cda55fadSBarry Smith        MatFDColoringCreate_MPIAIJ,
1661cda55fadSBarry Smith        0,
1662cda55fadSBarry Smith        MatSetUnfactored_MPIAIJ,
1663cda55fadSBarry Smith        0,
1664cda55fadSBarry Smith        0,
1665cda55fadSBarry Smith        MatGetSubMatrix_MPIAIJ,
1666e03a110bSBarry Smith        MatDestroy_MPIAIJ,
1667e03a110bSBarry Smith        MatView_MPIAIJ,
16688a124369SBarry Smith        MatGetPetscMaps_Petsc,
1669a2243be0SBarry Smith        0,
1670a2243be0SBarry Smith        0,
1671a2243be0SBarry Smith        0,
1672a2243be0SBarry Smith        0,
1673a2243be0SBarry Smith        0,
1674a2243be0SBarry Smith        0,
1675a2243be0SBarry Smith        0,
1676a2243be0SBarry Smith        0,
1677a2243be0SBarry Smith        MatSetColoring_MPIAIJ,
1678779c1a83SBarry Smith        MatSetValuesAdic_MPIAIJ,
1679779c1a83SBarry Smith        MatSetValuesAdifor_MPIAIJ
1680a2243be0SBarry Smith };
168136ce4990SBarry Smith 
16822e8a6d31SBarry Smith /* ----------------------------------------------------------------------------------------*/
16832e8a6d31SBarry Smith 
1684fb2e594dSBarry Smith EXTERN_C_BEGIN
16854a2ae208SSatish Balay #undef __FUNCT__
16864a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_MPIAIJ"
16872e8a6d31SBarry Smith int MatStoreValues_MPIAIJ(Mat mat)
16882e8a6d31SBarry Smith {
16892e8a6d31SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data;
16902e8a6d31SBarry Smith   int        ierr;
16912e8a6d31SBarry Smith 
16922e8a6d31SBarry Smith   PetscFunctionBegin;
16932e8a6d31SBarry Smith   ierr = MatStoreValues(aij->A);CHKERRQ(ierr);
16942e8a6d31SBarry Smith   ierr = MatStoreValues(aij->B);CHKERRQ(ierr);
16952e8a6d31SBarry Smith   PetscFunctionReturn(0);
16962e8a6d31SBarry Smith }
1697fb2e594dSBarry Smith EXTERN_C_END
16982e8a6d31SBarry Smith 
1699fb2e594dSBarry Smith EXTERN_C_BEGIN
17004a2ae208SSatish Balay #undef __FUNCT__
17014a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_MPIAIJ"
17022e8a6d31SBarry Smith int MatRetrieveValues_MPIAIJ(Mat mat)
17032e8a6d31SBarry Smith {
17042e8a6d31SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data;
17052e8a6d31SBarry Smith   int        ierr;
17062e8a6d31SBarry Smith 
17072e8a6d31SBarry Smith   PetscFunctionBegin;
17082e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr);
17092e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr);
17102e8a6d31SBarry Smith   PetscFunctionReturn(0);
17112e8a6d31SBarry Smith }
1712fb2e594dSBarry Smith EXTERN_C_END
17138a729477SBarry Smith 
1714e090d566SSatish Balay #include "petscpc.h"
171527508adbSBarry Smith EXTERN_C_BEGIN
1716ca44d042SBarry Smith EXTERN int MatGetDiagonalBlock_MPIAIJ(Mat,PetscTruth *,MatReuse,Mat *);
171792b32695SKris Buschelman EXTERN int MatDiagonalScaleLocal_MPIAIJ(Mat,Vec);
171827508adbSBarry Smith EXTERN_C_END
171927508adbSBarry Smith 
1720273d9f13SBarry Smith EXTERN_C_BEGIN
17214a2ae208SSatish Balay #undef __FUNCT__
1722a23d5eceSKris Buschelman #define __FUNCT__ "MatMPIAIJSetPreallocation_MPIAIJ"
1723a23d5eceSKris Buschelman int MatMPIAIJSetPreallocation_MPIAIJ(Mat B,int d_nz,int *d_nnz,int o_nz,int *o_nnz)
1724a23d5eceSKris Buschelman {
1725a23d5eceSKris Buschelman   Mat_MPIAIJ   *b;
1726a23d5eceSKris Buschelman   int          ierr,i;
1727a23d5eceSKris Buschelman 
1728a23d5eceSKris Buschelman   PetscFunctionBegin;
1729a23d5eceSKris Buschelman   B->preallocated = PETSC_TRUE;
1730a23d5eceSKris Buschelman   if (d_nz == PETSC_DEFAULT || d_nz == PETSC_DECIDE) d_nz = 5;
1731a23d5eceSKris Buschelman   if (o_nz == PETSC_DEFAULT || o_nz == PETSC_DECIDE) o_nz = 2;
1732a23d5eceSKris Buschelman   if (d_nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"d_nz cannot be less than 0: value %d",d_nz);
1733a23d5eceSKris Buschelman   if (o_nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"o_nz cannot be less than 0: value %d",o_nz);
1734a23d5eceSKris Buschelman   if (d_nnz) {
1735a23d5eceSKris Buschelman     for (i=0; i<B->m; i++) {
1736a23d5eceSKris Buschelman       if (d_nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"d_nnz cannot be less than 0: local row %d value %d",i,d_nnz[i]);
1737a23d5eceSKris Buschelman     }
1738a23d5eceSKris Buschelman   }
1739a23d5eceSKris Buschelman   if (o_nnz) {
1740a23d5eceSKris Buschelman     for (i=0; i<B->m; i++) {
1741a23d5eceSKris Buschelman       if (o_nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"o_nnz cannot be less than 0: local row %d value %d",i,o_nnz[i]);
1742a23d5eceSKris Buschelman     }
1743a23d5eceSKris Buschelman   }
1744a23d5eceSKris Buschelman   b = (Mat_MPIAIJ*)B->data;
1745a23d5eceSKris Buschelman 
1746a23d5eceSKris Buschelman   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,B->m,B->n,d_nz,d_nnz,&b->A);CHKERRQ(ierr);
1747a23d5eceSKris Buschelman   PetscLogObjectParent(B,b->A);
1748a23d5eceSKris Buschelman   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,B->m,B->N,o_nz,o_nnz,&b->B);CHKERRQ(ierr);
1749a23d5eceSKris Buschelman   PetscLogObjectParent(B,b->B);
1750a23d5eceSKris Buschelman 
1751a23d5eceSKris Buschelman   PetscFunctionReturn(0);
1752a23d5eceSKris Buschelman }
1753a23d5eceSKris Buschelman EXTERN_C_END
1754a23d5eceSKris Buschelman 
1755a23d5eceSKris Buschelman EXTERN_C_BEGIN
1756a23d5eceSKris Buschelman #undef __FUNCT__
17574a2ae208SSatish Balay #define __FUNCT__ "MatCreate_MPIAIJ"
1758273d9f13SBarry Smith int MatCreate_MPIAIJ(Mat B)
17598a729477SBarry Smith {
176044cd7ae7SLois Curfman McInnes   Mat_MPIAIJ *b;
1761f1af5d2fSBarry Smith   int        ierr,i,size;
1762416022c9SBarry Smith 
17633a40ed3dSBarry Smith   PetscFunctionBegin;
1764273d9f13SBarry Smith   ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr);
17651d55c564SBarry Smith 
1766b0a32e0cSBarry Smith   ierr            = PetscNew(Mat_MPIAIJ,&b);CHKERRQ(ierr);
1767b0a32e0cSBarry Smith   B->data         = (void*)b;
1768549d3d68SSatish Balay   ierr            = PetscMemzero(b,sizeof(Mat_MPIAIJ));CHKERRQ(ierr);
1769549d3d68SSatish Balay   ierr            = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
177044cd7ae7SLois Curfman McInnes   B->factor       = 0;
177144cd7ae7SLois Curfman McInnes   B->assembled    = PETSC_FALSE;
177290f02eecSBarry Smith   B->mapping      = 0;
1773d6dfbf8fSBarry Smith 
177447794344SBarry Smith   B->insertmode      = NOT_SET_VALUES;
17759eb4d147SSatish Balay   b->size            = size;
1776273d9f13SBarry Smith   ierr = MPI_Comm_rank(B->comm,&b->rank);CHKERRQ(ierr);
17771eb62cbbSBarry Smith 
1778273d9f13SBarry Smith   ierr = PetscSplitOwnership(B->comm,&B->m,&B->M);CHKERRQ(ierr);
1779273d9f13SBarry Smith   ierr = PetscSplitOwnership(B->comm,&B->n,&B->N);CHKERRQ(ierr);
17801eb62cbbSBarry Smith 
1781c7fcc2eaSBarry Smith   /* the information in the maps duplicates the information computed below, eventually
1782c7fcc2eaSBarry Smith      we should remove the duplicate information that is not contained in the maps */
17838a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->m,B->M,&B->rmap);CHKERRQ(ierr);
17848a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->n,B->N,&B->cmap);CHKERRQ(ierr);
1785c7fcc2eaSBarry Smith 
17861eb62cbbSBarry Smith   /* build local table of row and column ownerships */
1787b0a32e0cSBarry Smith   ierr = PetscMalloc(2*(b->size+2)*sizeof(int),&b->rowners);CHKERRQ(ierr);
1788b0a32e0cSBarry Smith   PetscLogObjectMemory(B,2*(b->size+2)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_MPIAIJ));
1789603f58a4SSatish Balay   b->cowners = b->rowners + b->size + 2;
1790273d9f13SBarry Smith   ierr = MPI_Allgather(&B->m,1,MPI_INT,b->rowners+1,1,MPI_INT,B->comm);CHKERRQ(ierr);
179144cd7ae7SLois Curfman McInnes   b->rowners[0] = 0;
179244cd7ae7SLois Curfman McInnes   for (i=2; i<=b->size; i++) {
179344cd7ae7SLois Curfman McInnes     b->rowners[i] += b->rowners[i-1];
17948a729477SBarry Smith   }
179544cd7ae7SLois Curfman McInnes   b->rstart = b->rowners[b->rank];
179644cd7ae7SLois Curfman McInnes   b->rend   = b->rowners[b->rank+1];
1797273d9f13SBarry Smith   ierr = MPI_Allgather(&B->n,1,MPI_INT,b->cowners+1,1,MPI_INT,B->comm);CHKERRQ(ierr);
179844cd7ae7SLois Curfman McInnes   b->cowners[0] = 0;
179944cd7ae7SLois Curfman McInnes   for (i=2; i<=b->size; i++) {
180044cd7ae7SLois Curfman McInnes     b->cowners[i] += b->cowners[i-1];
18018a729477SBarry Smith   }
180244cd7ae7SLois Curfman McInnes   b->cstart = b->cowners[b->rank];
180344cd7ae7SLois Curfman McInnes   b->cend   = b->cowners[b->rank+1];
18048a729477SBarry Smith 
18051eb62cbbSBarry Smith   /* build cache for off array entries formed */
18067e2c5f70SBarry Smith   ierr = MatStashCreate_Private(B->comm,1,&B->stash);CHKERRQ(ierr);
18077c922b88SBarry Smith   b->donotstash  = PETSC_FALSE;
180844cd7ae7SLois Curfman McInnes   b->colmap      = 0;
180944cd7ae7SLois Curfman McInnes   b->garray      = 0;
18107c922b88SBarry Smith   b->roworiented = PETSC_TRUE;
18118a729477SBarry Smith 
18121eb62cbbSBarry Smith   /* stuff used for matrix vector multiply */
18137c922b88SBarry Smith   b->lvec      = PETSC_NULL;
18147c922b88SBarry Smith   b->Mvctx     = PETSC_NULL;
18158a729477SBarry Smith 
18167a0afa10SBarry Smith   /* stuff for MatGetRow() */
181744cd7ae7SLois Curfman McInnes   b->rowindices   = 0;
181844cd7ae7SLois Curfman McInnes   b->rowvalues    = 0;
181944cd7ae7SLois Curfman McInnes   b->getrowactive = PETSC_FALSE;
18201a8d6bc9SBarry Smith 
1821f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
18222e8a6d31SBarry Smith                                      "MatStoreValues_MPIAIJ",
18230c97f09dSSatish Balay                                      MatStoreValues_MPIAIJ);CHKERRQ(ierr);
1824f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
18252e8a6d31SBarry Smith                                      "MatRetrieveValues_MPIAIJ",
18260c97f09dSSatish Balay                                      MatRetrieveValues_MPIAIJ);CHKERRQ(ierr);
1827f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetDiagonalBlock_C",
18285ef9f2a5SBarry Smith 				     "MatGetDiagonalBlock_MPIAIJ",
18290c97f09dSSatish Balay                                      MatGetDiagonalBlock_MPIAIJ);CHKERRQ(ierr);
1830cd0d46ebSvictorle   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsSymmetric_C",
1831cd0d46ebSvictorle 				     "MatIsSymmetric_MPIAIJ",
1832cd0d46ebSvictorle 				     MatIsSymmetric_MPIAIJ); CHKERRQ(ierr);
1833a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAIJSetPreallocation_C",
1834a23d5eceSKris Buschelman 				     "MatMPIAIJSetPreallocation_MPIAIJ",
1835a23d5eceSKris Buschelman 				     MatMPIAIJSetPreallocation_MPIAIJ); CHKERRQ(ierr);
183692b32695SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatDiagonalScaleLocal_C",
183792b32695SKris Buschelman 				     "MatDiagonalScaleLocal_MPIAIJ",
183892b32695SKris Buschelman 				     MatDiagonalScaleLocal_MPIAIJ); CHKERRQ(ierr);
18393a40ed3dSBarry Smith   PetscFunctionReturn(0);
1840d6dfbf8fSBarry Smith }
1841273d9f13SBarry Smith EXTERN_C_END
1842c74985f6SBarry Smith 
18434a2ae208SSatish Balay #undef __FUNCT__
18444a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_MPIAIJ"
18452e8a6d31SBarry Smith int MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
1846d6dfbf8fSBarry Smith {
1847d6dfbf8fSBarry Smith   Mat        mat;
1848416022c9SBarry Smith   Mat_MPIAIJ *a,*oldmat = (Mat_MPIAIJ*)matin->data;
184916d6aa21SSatish Balay   int        ierr;
1850d6dfbf8fSBarry Smith 
18513a40ed3dSBarry Smith   PetscFunctionBegin;
1852416022c9SBarry Smith   *newmat       = 0;
1853273d9f13SBarry Smith   ierr = MatCreate(matin->comm,matin->m,matin->n,matin->M,matin->N,&mat);CHKERRQ(ierr);
1854273d9f13SBarry Smith   ierr = MatSetType(mat,MATMPIAIJ);CHKERRQ(ierr);
1855273d9f13SBarry Smith   a    = (Mat_MPIAIJ*)mat->data;
1856549d3d68SSatish Balay   ierr              = PetscMemcpy(mat->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
1857d6dfbf8fSBarry Smith   mat->factor       = matin->factor;
1858c456f294SBarry Smith   mat->assembled    = PETSC_TRUE;
1859e7641de0SSatish Balay   mat->insertmode   = NOT_SET_VALUES;
1860273d9f13SBarry Smith   mat->preallocated = PETSC_TRUE;
1861d6dfbf8fSBarry Smith 
1862416022c9SBarry Smith   a->rstart       = oldmat->rstart;
1863416022c9SBarry Smith   a->rend         = oldmat->rend;
1864416022c9SBarry Smith   a->cstart       = oldmat->cstart;
1865416022c9SBarry Smith   a->cend         = oldmat->cend;
186617699dbbSLois Curfman McInnes   a->size         = oldmat->size;
186717699dbbSLois Curfman McInnes   a->rank         = oldmat->rank;
1868e7641de0SSatish Balay   a->donotstash   = oldmat->donotstash;
1869e7641de0SSatish Balay   a->roworiented  = oldmat->roworiented;
1870e7641de0SSatish Balay   a->rowindices   = 0;
1871bcd2baecSBarry Smith   a->rowvalues    = 0;
1872bcd2baecSBarry Smith   a->getrowactive = PETSC_FALSE;
1873d6dfbf8fSBarry Smith 
1874549d3d68SSatish Balay   ierr       = PetscMemcpy(a->rowners,oldmat->rowners,2*(a->size+2)*sizeof(int));CHKERRQ(ierr);
18758798bf22SSatish Balay   ierr       = MatStashCreate_Private(matin->comm,1,&mat->stash);CHKERRQ(ierr);
18762ee70a88SLois Curfman McInnes   if (oldmat->colmap) {
1877aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
18780f5bd95cSBarry Smith     ierr = PetscTableCreateCopy(oldmat->colmap,&a->colmap);CHKERRQ(ierr);
1879b1fc9764SSatish Balay #else
1880b0a32e0cSBarry Smith     ierr = PetscMalloc((mat->N)*sizeof(int),&a->colmap);CHKERRQ(ierr);
1881b0a32e0cSBarry Smith     PetscLogObjectMemory(mat,(mat->N)*sizeof(int));
1882273d9f13SBarry Smith     ierr      = PetscMemcpy(a->colmap,oldmat->colmap,(mat->N)*sizeof(int));CHKERRQ(ierr);
1883b1fc9764SSatish Balay #endif
1884416022c9SBarry Smith   } else a->colmap = 0;
18853f41c07dSBarry Smith   if (oldmat->garray) {
188616d6aa21SSatish Balay     int len;
1887273d9f13SBarry Smith     len  = oldmat->B->n;
1888b0a32e0cSBarry Smith     ierr = PetscMalloc((len+1)*sizeof(int),&a->garray);CHKERRQ(ierr);
1889b0a32e0cSBarry Smith     PetscLogObjectMemory(mat,len*sizeof(int));
1890549d3d68SSatish Balay     if (len) { ierr = PetscMemcpy(a->garray,oldmat->garray,len*sizeof(int));CHKERRQ(ierr); }
1891416022c9SBarry Smith   } else a->garray = 0;
1892d6dfbf8fSBarry Smith 
1893416022c9SBarry Smith   ierr =  VecDuplicate(oldmat->lvec,&a->lvec);CHKERRQ(ierr);
1894b0a32e0cSBarry Smith   PetscLogObjectParent(mat,a->lvec);
1895a56f8943SBarry Smith   ierr =  VecScatterCopy(oldmat->Mvctx,&a->Mvctx);CHKERRQ(ierr);
1896b0a32e0cSBarry Smith   PetscLogObjectParent(mat,a->Mvctx);
18972e8a6d31SBarry Smith   ierr =  MatDuplicate(oldmat->A,cpvalues,&a->A);CHKERRQ(ierr);
1898b0a32e0cSBarry Smith   PetscLogObjectParent(mat,a->A);
18992e8a6d31SBarry Smith   ierr =  MatDuplicate(oldmat->B,cpvalues,&a->B);CHKERRQ(ierr);
1900b0a32e0cSBarry Smith   PetscLogObjectParent(mat,a->B);
1901b0a32e0cSBarry Smith   ierr = PetscFListDuplicate(matin->qlist,&mat->qlist);CHKERRQ(ierr);
19028a729477SBarry Smith   *newmat = mat;
19033a40ed3dSBarry Smith   PetscFunctionReturn(0);
19048a729477SBarry Smith }
1905416022c9SBarry Smith 
1906e090d566SSatish Balay #include "petscsys.h"
1907416022c9SBarry Smith 
1908273d9f13SBarry Smith EXTERN_C_BEGIN
19094a2ae208SSatish Balay #undef __FUNCT__
19104a2ae208SSatish Balay #define __FUNCT__ "MatLoad_MPIAIJ"
1911b0a32e0cSBarry Smith int MatLoad_MPIAIJ(PetscViewer viewer,MatType type,Mat *newmat)
1912416022c9SBarry Smith {
1913d65a2f8fSBarry Smith   Mat          A;
191487828ca2SBarry Smith   PetscScalar  *vals,*svals;
191519bcc07fSBarry Smith   MPI_Comm     comm = ((PetscObject)viewer)->comm;
1916416022c9SBarry Smith   MPI_Status   status;
19173a40ed3dSBarry Smith   int          i,nz,ierr,j,rstart,rend,fd;
191817699dbbSLois Curfman McInnes   int          header[4],rank,size,*rowlengths = 0,M,N,m,*rowners,maxnz,*cols;
1919d65a2f8fSBarry Smith   int          *ourlens,*sndcounts = 0,*procsnz = 0,*offlens,jj,*mycols,*smycols;
1920b362ba68SBarry Smith   int          tag = ((PetscObject)viewer)->tag,cend,cstart,n;
1921eee76cafSHong Zhang #if defined(PETSC_HAVE_SPOOLES) || defined(PETSC_HAVE_SUPERLUDIST) || defined(PETSC_HAVE_MUMPS)
192227fa2452SHong Zhang   PetscTruth   flag;
192327fa2452SHong Zhang #endif
1924416022c9SBarry Smith 
19253a40ed3dSBarry Smith   PetscFunctionBegin;
19261dab6e02SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
19271dab6e02SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
192817699dbbSLois Curfman McInnes   if (!rank) {
1929b0a32e0cSBarry Smith     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
19300752156aSBarry Smith     ierr = PetscBinaryRead(fd,(char *)header,4,PETSC_INT);CHKERRQ(ierr);
1931552e946dSBarry Smith     if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
1932d64ed03dSBarry Smith     if (header[3] < 0) {
193329bbc08cSBarry Smith       SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix in special format on disk, cannot load as MPIAIJ");
1934d64ed03dSBarry Smith     }
19356c5fab8fSBarry Smith   }
19366c5fab8fSBarry Smith 
1937ca161407SBarry Smith   ierr = MPI_Bcast(header+1,3,MPI_INT,0,comm);CHKERRQ(ierr);
1938416022c9SBarry Smith   M = header[1]; N = header[2];
1939416022c9SBarry Smith   /* determine ownership of all rows */
194017699dbbSLois Curfman McInnes   m = M/size + ((M % size) > rank);
1941b0a32e0cSBarry Smith   ierr = PetscMalloc((size+2)*sizeof(int),&rowners);CHKERRQ(ierr);
1942ca161407SBarry Smith   ierr = MPI_Allgather(&m,1,MPI_INT,rowners+1,1,MPI_INT,comm);CHKERRQ(ierr);
1943416022c9SBarry Smith   rowners[0] = 0;
194417699dbbSLois Curfman McInnes   for (i=2; i<=size; i++) {
1945416022c9SBarry Smith     rowners[i] += rowners[i-1];
1946416022c9SBarry Smith   }
194717699dbbSLois Curfman McInnes   rstart = rowners[rank];
194817699dbbSLois Curfman McInnes   rend   = rowners[rank+1];
1949416022c9SBarry Smith 
1950416022c9SBarry Smith   /* distribute row lengths to all processors */
1951b0a32e0cSBarry Smith   ierr    = PetscMalloc(2*(rend-rstart+1)*sizeof(int),&ourlens);CHKERRQ(ierr);
1952416022c9SBarry Smith   offlens = ourlens + (rend-rstart);
195317699dbbSLois Curfman McInnes   if (!rank) {
1954b0a32e0cSBarry Smith     ierr = PetscMalloc(M*sizeof(int),&rowlengths);CHKERRQ(ierr);
19550752156aSBarry Smith     ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
1956b0a32e0cSBarry Smith     ierr = PetscMalloc(size*sizeof(int),&sndcounts);CHKERRQ(ierr);
195717699dbbSLois Curfman McInnes     for (i=0; i<size; i++) sndcounts[i] = rowners[i+1] - rowners[i];
1958ca161407SBarry Smith     ierr = MPI_Scatterv(rowlengths,sndcounts,rowners,MPI_INT,ourlens,rend-rstart,MPI_INT,0,comm);CHKERRQ(ierr);
1959606d414cSSatish Balay     ierr = PetscFree(sndcounts);CHKERRQ(ierr);
19603a40ed3dSBarry Smith   } else {
1961ca161407SBarry Smith     ierr = MPI_Scatterv(0,0,0,MPI_INT,ourlens,rend-rstart,MPI_INT,0,comm);CHKERRQ(ierr);
1962416022c9SBarry Smith   }
1963416022c9SBarry Smith 
196417699dbbSLois Curfman McInnes   if (!rank) {
1965416022c9SBarry Smith     /* calculate the number of nonzeros on each processor */
1966b0a32e0cSBarry Smith     ierr = PetscMalloc(size*sizeof(int),&procsnz);CHKERRQ(ierr);
1967549d3d68SSatish Balay     ierr = PetscMemzero(procsnz,size*sizeof(int));CHKERRQ(ierr);
196817699dbbSLois Curfman McInnes     for (i=0; i<size; i++) {
1969416022c9SBarry Smith       for (j=rowners[i]; j< rowners[i+1]; j++) {
1970416022c9SBarry Smith         procsnz[i] += rowlengths[j];
1971416022c9SBarry Smith       }
1972416022c9SBarry Smith     }
1973606d414cSSatish Balay     ierr = PetscFree(rowlengths);CHKERRQ(ierr);
1974416022c9SBarry Smith 
1975416022c9SBarry Smith     /* determine max buffer needed and allocate it */
1976416022c9SBarry Smith     maxnz = 0;
197717699dbbSLois Curfman McInnes     for (i=0; i<size; i++) {
19780452661fSBarry Smith       maxnz = PetscMax(maxnz,procsnz[i]);
1979416022c9SBarry Smith     }
1980b0a32e0cSBarry Smith     ierr = PetscMalloc(maxnz*sizeof(int),&cols);CHKERRQ(ierr);
1981416022c9SBarry Smith 
1982416022c9SBarry Smith     /* read in my part of the matrix column indices  */
1983416022c9SBarry Smith     nz   = procsnz[0];
1984b0a32e0cSBarry Smith     ierr = PetscMalloc(nz*sizeof(int),&mycols);CHKERRQ(ierr);
19850752156aSBarry Smith     ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr);
1986d65a2f8fSBarry Smith 
1987d65a2f8fSBarry Smith     /* read in every one elses and ship off */
198817699dbbSLois Curfman McInnes     for (i=1; i<size; i++) {
1989d65a2f8fSBarry Smith       nz   = procsnz[i];
19900752156aSBarry Smith       ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr);
1991ca161407SBarry Smith       ierr = MPI_Send(cols,nz,MPI_INT,i,tag,comm);CHKERRQ(ierr);
1992d65a2f8fSBarry Smith     }
1993606d414cSSatish Balay     ierr = PetscFree(cols);CHKERRQ(ierr);
19943a40ed3dSBarry Smith   } else {
1995416022c9SBarry Smith     /* determine buffer space needed for message */
1996416022c9SBarry Smith     nz = 0;
1997416022c9SBarry Smith     for (i=0; i<m; i++) {
1998416022c9SBarry Smith       nz += ourlens[i];
1999416022c9SBarry Smith     }
2000b0a32e0cSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(int),&mycols);CHKERRQ(ierr);
2001416022c9SBarry Smith 
2002416022c9SBarry Smith     /* receive message of column indices*/
2003ca161407SBarry Smith     ierr = MPI_Recv(mycols,nz,MPI_INT,0,tag,comm,&status);CHKERRQ(ierr);
2004ca161407SBarry Smith     ierr = MPI_Get_count(&status,MPI_INT,&maxnz);CHKERRQ(ierr);
200529bbc08cSBarry Smith     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
2006416022c9SBarry Smith   }
2007416022c9SBarry Smith 
2008b362ba68SBarry Smith   /* determine column ownership if matrix is not square */
2009b362ba68SBarry Smith   if (N != M) {
2010b362ba68SBarry Smith     n      = N/size + ((N % size) > rank);
2011b362ba68SBarry Smith     ierr   = MPI_Scan(&n,&cend,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr);
2012b362ba68SBarry Smith     cstart = cend - n;
2013b362ba68SBarry Smith   } else {
2014b362ba68SBarry Smith     cstart = rstart;
2015b362ba68SBarry Smith     cend   = rend;
2016fb2e594dSBarry Smith     n      = cend - cstart;
2017b362ba68SBarry Smith   }
2018b362ba68SBarry Smith 
2019416022c9SBarry Smith   /* loop over local rows, determining number of off diagonal entries */
2020549d3d68SSatish Balay   ierr = PetscMemzero(offlens,m*sizeof(int));CHKERRQ(ierr);
2021416022c9SBarry Smith   jj = 0;
2022416022c9SBarry Smith   for (i=0; i<m; i++) {
2023416022c9SBarry Smith     for (j=0; j<ourlens[i]; j++) {
2024b362ba68SBarry Smith       if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++;
2025416022c9SBarry Smith       jj++;
2026416022c9SBarry Smith     }
2027416022c9SBarry Smith   }
2028d65a2f8fSBarry Smith 
2029d65a2f8fSBarry Smith   /* create our matrix */
2030416022c9SBarry Smith   for (i=0; i<m; i++) {
2031416022c9SBarry Smith     ourlens[i] -= offlens[i];
2032416022c9SBarry Smith   }
2033b362ba68SBarry Smith   ierr = MatCreateMPIAIJ(comm,m,n,M,N,0,ourlens,0,offlens,newmat);CHKERRQ(ierr);
2034d65a2f8fSBarry Smith   A = *newmat;
2035fb2e594dSBarry Smith   ierr = MatSetOption(A,MAT_COLUMNS_SORTED);CHKERRQ(ierr);
2036d65a2f8fSBarry Smith   for (i=0; i<m; i++) {
2037d65a2f8fSBarry Smith     ourlens[i] += offlens[i];
2038d65a2f8fSBarry Smith   }
2039416022c9SBarry Smith 
204017699dbbSLois Curfman McInnes   if (!rank) {
204187828ca2SBarry Smith     ierr = PetscMalloc(maxnz*sizeof(PetscScalar),&vals);CHKERRQ(ierr);
2042416022c9SBarry Smith 
2043416022c9SBarry Smith     /* read in my part of the matrix numerical values  */
2044416022c9SBarry Smith     nz   = procsnz[0];
20450752156aSBarry Smith     ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
2046d65a2f8fSBarry Smith 
2047d65a2f8fSBarry Smith     /* insert into matrix */
2048d65a2f8fSBarry Smith     jj      = rstart;
2049d65a2f8fSBarry Smith     smycols = mycols;
2050d65a2f8fSBarry Smith     svals   = vals;
2051d65a2f8fSBarry Smith     for (i=0; i<m; i++) {
2052d65a2f8fSBarry Smith       ierr = MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
2053d65a2f8fSBarry Smith       smycols += ourlens[i];
2054d65a2f8fSBarry Smith       svals   += ourlens[i];
2055d65a2f8fSBarry Smith       jj++;
2056416022c9SBarry Smith     }
2057416022c9SBarry Smith 
2058d65a2f8fSBarry Smith     /* read in other processors and ship out */
205917699dbbSLois Curfman McInnes     for (i=1; i<size; i++) {
2060416022c9SBarry Smith       nz   = procsnz[i];
20610752156aSBarry Smith       ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
2062ca161407SBarry Smith       ierr = MPI_Send(vals,nz,MPIU_SCALAR,i,A->tag,comm);CHKERRQ(ierr);
2063416022c9SBarry Smith     }
2064606d414cSSatish Balay     ierr = PetscFree(procsnz);CHKERRQ(ierr);
20653a40ed3dSBarry Smith   } else {
2066d65a2f8fSBarry Smith     /* receive numeric values */
206787828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&vals);CHKERRQ(ierr);
2068416022c9SBarry Smith 
2069d65a2f8fSBarry Smith     /* receive message of values*/
2070ca161407SBarry Smith     ierr = MPI_Recv(vals,nz,MPIU_SCALAR,0,A->tag,comm,&status);CHKERRQ(ierr);
2071ca161407SBarry Smith     ierr = MPI_Get_count(&status,MPIU_SCALAR,&maxnz);CHKERRQ(ierr);
207229bbc08cSBarry Smith     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
2073d65a2f8fSBarry Smith 
2074d65a2f8fSBarry Smith     /* insert into matrix */
2075d65a2f8fSBarry Smith     jj      = rstart;
2076d65a2f8fSBarry Smith     smycols = mycols;
2077d65a2f8fSBarry Smith     svals   = vals;
2078d65a2f8fSBarry Smith     for (i=0; i<m; i++) {
2079d65a2f8fSBarry Smith       ierr     = MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
2080d65a2f8fSBarry Smith       smycols += ourlens[i];
2081d65a2f8fSBarry Smith       svals   += ourlens[i];
2082d65a2f8fSBarry Smith       jj++;
2083d65a2f8fSBarry Smith     }
2084d65a2f8fSBarry Smith   }
2085606d414cSSatish Balay   ierr = PetscFree(ourlens);CHKERRQ(ierr);
2086606d414cSSatish Balay   ierr = PetscFree(vals);CHKERRQ(ierr);
2087606d414cSSatish Balay   ierr = PetscFree(mycols);CHKERRQ(ierr);
2088606d414cSSatish Balay   ierr = PetscFree(rowners);CHKERRQ(ierr);
2089d65a2f8fSBarry Smith 
20906d4a8577SBarry Smith   ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20916d4a8577SBarry Smith   ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
209227fa2452SHong Zhang #if defined(PETSC_HAVE_SPOOLES)
209327fa2452SHong Zhang   ierr = PetscOptionsHasName(A->prefix,"-mat_aij_spooles",&flag);CHKERRQ(ierr);
20940f39d4ecSHong Zhang   if (flag) {
20950f39d4ecSHong Zhang     if (size == 1) {
20960f39d4ecSHong Zhang       ierr = MatUseSpooles_SeqAIJ(A);CHKERRQ(ierr);
20970f39d4ecSHong Zhang     } else {
20980f39d4ecSHong Zhang       ierr = MatUseSpooles_MPIAIJ(A);CHKERRQ(ierr);
20990f39d4ecSHong Zhang     }
21000f39d4ecSHong Zhang   }
210127fa2452SHong Zhang #endif
210227fa2452SHong Zhang #if defined(PETSC_HAVE_SUPERLUDIST)
210327fa2452SHong Zhang   ierr = PetscOptionsHasName(A->prefix,"-mat_aij_superlu_dist",&flag);CHKERRQ(ierr);
210427fa2452SHong Zhang   if (flag) { ierr = MatUseSuperLU_DIST_MPIAIJ(A);CHKERRQ(ierr); }
210527fa2452SHong Zhang #endif
2106eee76cafSHong Zhang #if defined(PETSC_HAVE_MUMPS)
2107eee76cafSHong Zhang   ierr = PetscOptionsHasName(A->prefix,"-mat_aij_mumps",&flag);CHKERRQ(ierr);
2108eee76cafSHong Zhang   if (flag) { ierr = MatUseMUMPS_MPIAIJ(A);CHKERRQ(ierr); }
2109eee76cafSHong Zhang #endif
21103a40ed3dSBarry Smith   PetscFunctionReturn(0);
2111416022c9SBarry Smith }
2112273d9f13SBarry Smith EXTERN_C_END
2113a0ff6018SBarry Smith 
21144a2ae208SSatish Balay #undef __FUNCT__
21154a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_MPIAIJ"
2116a0ff6018SBarry Smith /*
211729da9460SBarry Smith     Not great since it makes two copies of the submatrix, first an SeqAIJ
211829da9460SBarry Smith   in local and then by concatenating the local matrices the end result.
211929da9460SBarry Smith   Writing it directly would be much like MatGetSubMatrices_MPIAIJ()
2120a0ff6018SBarry Smith */
21217b2a1423SBarry Smith int MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,int csize,MatReuse call,Mat *newmat)
2122a0ff6018SBarry Smith {
212300e6dbe6SBarry Smith   int          ierr,i,m,n,rstart,row,rend,nz,*cwork,size,rank,j;
2124ab50ec6bSBarry Smith   int          *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal;
2125fee21e36SBarry Smith   Mat          *local,M,Mreuse;
212687828ca2SBarry Smith   PetscScalar  *vwork,*aa;
212700e6dbe6SBarry Smith   MPI_Comm     comm = mat->comm;
212800e6dbe6SBarry Smith   Mat_SeqAIJ   *aij;
21297e2c5f70SBarry Smith 
2130a0ff6018SBarry Smith 
2131a0ff6018SBarry Smith   PetscFunctionBegin;
21321dab6e02SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
21331dab6e02SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
213400e6dbe6SBarry Smith 
2135fee21e36SBarry Smith   if (call ==  MAT_REUSE_MATRIX) {
2136fee21e36SBarry Smith     ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);CHKERRQ(ierr);
213729bbc08cSBarry Smith     if (!Mreuse) SETERRQ(1,"Submatrix passed in was not used before, cannot reuse");
2138fee21e36SBarry Smith     local = &Mreuse;
2139fee21e36SBarry Smith     ierr  = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&local);CHKERRQ(ierr);
2140fee21e36SBarry Smith   } else {
2141a0ff6018SBarry Smith     ierr   = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr);
2142fee21e36SBarry Smith     Mreuse = *local;
2143606d414cSSatish Balay     ierr   = PetscFree(local);CHKERRQ(ierr);
2144fee21e36SBarry Smith   }
2145a0ff6018SBarry Smith 
2146a0ff6018SBarry Smith   /*
2147a0ff6018SBarry Smith       m - number of local rows
2148a0ff6018SBarry Smith       n - number of columns (same on all processors)
2149a0ff6018SBarry Smith       rstart - first row in new global matrix generated
2150a0ff6018SBarry Smith   */
2151fee21e36SBarry Smith   ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr);
2152a0ff6018SBarry Smith   if (call == MAT_INITIAL_MATRIX) {
2153fee21e36SBarry Smith     aij = (Mat_SeqAIJ*)(Mreuse)->data;
215400e6dbe6SBarry Smith     ii  = aij->i;
215500e6dbe6SBarry Smith     jj  = aij->j;
215600e6dbe6SBarry Smith 
2157a0ff6018SBarry Smith     /*
215800e6dbe6SBarry Smith         Determine the number of non-zeros in the diagonal and off-diagonal
215900e6dbe6SBarry Smith         portions of the matrix in order to do correct preallocation
2160a0ff6018SBarry Smith     */
216100e6dbe6SBarry Smith 
216200e6dbe6SBarry Smith     /* first get start and end of "diagonal" columns */
21636a6a5d1dSBarry Smith     if (csize == PETSC_DECIDE) {
2164ab50ec6bSBarry Smith       ierr = ISGetSize(isrow,&mglobal);CHKERRQ(ierr);
2165ab50ec6bSBarry Smith       if (mglobal == n) { /* square matrix */
2166e2c4fddaSBarry Smith 	nlocal = m;
21676a6a5d1dSBarry Smith       } else {
2168ab50ec6bSBarry Smith         nlocal = n/size + ((n % size) > rank);
2169ab50ec6bSBarry Smith       }
2170ab50ec6bSBarry Smith     } else {
21716a6a5d1dSBarry Smith       nlocal = csize;
21726a6a5d1dSBarry Smith     }
2173ca161407SBarry Smith     ierr   = MPI_Scan(&nlocal,&rend,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr);
217400e6dbe6SBarry Smith     rstart = rend - nlocal;
21756a6a5d1dSBarry Smith     if (rank == size - 1 && rend != n) {
217619e8cfbbSBarry Smith       SETERRQ2(1,"Local column sizes %d do not add up to total number of columns %d",rend,n);
21776a6a5d1dSBarry Smith     }
217800e6dbe6SBarry Smith 
217900e6dbe6SBarry Smith     /* next, compute all the lengths */
2180b0a32e0cSBarry Smith     ierr  = PetscMalloc((2*m+1)*sizeof(int),&dlens);CHKERRQ(ierr);
218100e6dbe6SBarry Smith     olens = dlens + m;
218200e6dbe6SBarry Smith     for (i=0; i<m; i++) {
218300e6dbe6SBarry Smith       jend = ii[i+1] - ii[i];
218400e6dbe6SBarry Smith       olen = 0;
218500e6dbe6SBarry Smith       dlen = 0;
218600e6dbe6SBarry Smith       for (j=0; j<jend; j++) {
218700e6dbe6SBarry Smith         if (*jj < rstart || *jj >= rend) olen++;
218800e6dbe6SBarry Smith         else dlen++;
218900e6dbe6SBarry Smith         jj++;
219000e6dbe6SBarry Smith       }
219100e6dbe6SBarry Smith       olens[i] = olen;
219200e6dbe6SBarry Smith       dlens[i] = dlen;
219300e6dbe6SBarry Smith     }
21942d207970SBarry Smith     ierr = MatCreateMPIAIJ(comm,m,nlocal,PETSC_DECIDE,n,0,dlens,0,olens,&M);CHKERRQ(ierr);
2195606d414cSSatish Balay     ierr = PetscFree(dlens);CHKERRQ(ierr);
2196a0ff6018SBarry Smith   } else {
2197a0ff6018SBarry Smith     int ml,nl;
2198a0ff6018SBarry Smith 
2199a0ff6018SBarry Smith     M = *newmat;
2200a0ff6018SBarry Smith     ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr);
220129bbc08cSBarry Smith     if (ml != m) SETERRQ(PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
2202a0ff6018SBarry Smith     ierr = MatZeroEntries(M);CHKERRQ(ierr);
2203c48de900SBarry Smith     /*
2204c48de900SBarry Smith          The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
2205c48de900SBarry Smith        rather than the slower MatSetValues().
2206c48de900SBarry Smith     */
2207c48de900SBarry Smith     M->was_assembled = PETSC_TRUE;
2208c48de900SBarry Smith     M->assembled     = PETSC_FALSE;
2209a0ff6018SBarry Smith   }
2210a0ff6018SBarry Smith   ierr = MatGetOwnershipRange(M,&rstart,&rend);CHKERRQ(ierr);
2211fee21e36SBarry Smith   aij = (Mat_SeqAIJ*)(Mreuse)->data;
221200e6dbe6SBarry Smith   ii  = aij->i;
221300e6dbe6SBarry Smith   jj  = aij->j;
221400e6dbe6SBarry Smith   aa  = aij->a;
2215a0ff6018SBarry Smith   for (i=0; i<m; i++) {
2216a0ff6018SBarry Smith     row   = rstart + i;
221700e6dbe6SBarry Smith     nz    = ii[i+1] - ii[i];
221800e6dbe6SBarry Smith     cwork = jj;     jj += nz;
221900e6dbe6SBarry Smith     vwork = aa;     aa += nz;
22208c638d02SBarry Smith     ierr = MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr);
2221a0ff6018SBarry Smith   }
2222a0ff6018SBarry Smith 
2223a0ff6018SBarry Smith   ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2224a0ff6018SBarry Smith   ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2225a0ff6018SBarry Smith   *newmat = M;
2226fee21e36SBarry Smith 
2227fee21e36SBarry Smith   /* save submatrix used in processor for next request */
2228fee21e36SBarry Smith   if (call ==  MAT_INITIAL_MATRIX) {
2229fee21e36SBarry Smith     ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr);
2230fee21e36SBarry Smith     ierr = PetscObjectDereference((PetscObject)Mreuse);CHKERRQ(ierr);
2231fee21e36SBarry Smith   }
2232fee21e36SBarry Smith 
2233a0ff6018SBarry Smith   PetscFunctionReturn(0);
2234a0ff6018SBarry Smith }
2235273d9f13SBarry Smith 
22364a2ae208SSatish Balay #undef __FUNCT__
22374a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJSetPreallocation"
2238273d9f13SBarry Smith /*@C
2239273d9f13SBarry Smith    MatMPIAIJSetPreallocation - Creates a sparse parallel matrix in AIJ format
2240273d9f13SBarry Smith    (the default parallel PETSc format).  For good matrix assembly performance
2241273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameters
2242273d9f13SBarry Smith    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
2243273d9f13SBarry Smith    performance can be increased by more than a factor of 50.
2244273d9f13SBarry Smith 
2245273d9f13SBarry Smith    Collective on MPI_Comm
2246273d9f13SBarry Smith 
2247273d9f13SBarry Smith    Input Parameters:
2248273d9f13SBarry Smith +  A - the matrix
2249273d9f13SBarry Smith .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
2250273d9f13SBarry Smith            (same value is used for all local rows)
2251273d9f13SBarry Smith .  d_nnz - array containing the number of nonzeros in the various rows of the
2252273d9f13SBarry Smith            DIAGONAL portion of the local submatrix (possibly different for each row)
2253273d9f13SBarry Smith            or PETSC_NULL, if d_nz is used to specify the nonzero structure.
2254273d9f13SBarry Smith            The size of this array is equal to the number of local rows, i.e 'm'.
2255273d9f13SBarry Smith            You must leave room for the diagonal entry even if it is zero.
2256273d9f13SBarry Smith .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
2257273d9f13SBarry Smith            submatrix (same value is used for all local rows).
2258273d9f13SBarry Smith -  o_nnz - array containing the number of nonzeros in the various rows of the
2259273d9f13SBarry Smith            OFF-DIAGONAL portion of the local submatrix (possibly different for
2260273d9f13SBarry Smith            each row) or PETSC_NULL, if o_nz is used to specify the nonzero
2261273d9f13SBarry Smith            structure. The size of this array is equal to the number
2262273d9f13SBarry Smith            of local rows, i.e 'm'.
2263273d9f13SBarry Smith 
2264273d9f13SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
2265273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2266273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2267273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users manual for details.
2268273d9f13SBarry Smith 
2269273d9f13SBarry Smith    The user MUST specify either the local or global matrix dimensions
2270273d9f13SBarry Smith    (possibly both).
2271273d9f13SBarry Smith 
2272273d9f13SBarry Smith    The parallel matrix is partitioned such that the first m0 rows belong to
2273273d9f13SBarry Smith    process 0, the next m1 rows belong to process 1, the next m2 rows belong
2274273d9f13SBarry Smith    to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.
2275273d9f13SBarry Smith 
2276273d9f13SBarry Smith    The DIAGONAL portion of the local submatrix of a processor can be defined
2277273d9f13SBarry Smith    as the submatrix which is obtained by extraction the part corresponding
2278273d9f13SBarry Smith    to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the
2279273d9f13SBarry Smith    first row that belongs to the processor, and r2 is the last row belonging
2280273d9f13SBarry Smith    to the this processor. This is a square mxm matrix. The remaining portion
2281273d9f13SBarry Smith    of the local submatrix (mxN) constitute the OFF-DIAGONAL portion.
2282273d9f13SBarry Smith 
2283273d9f13SBarry Smith    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
2284273d9f13SBarry Smith 
2285273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible.
2286273d9f13SBarry Smith    We search for consecutive rows with the same nonzero structure, thereby
2287273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2288273d9f13SBarry Smith 
2289273d9f13SBarry Smith    Options Database Keys:
2290273d9f13SBarry Smith +  -mat_aij_no_inode  - Do not use inodes
2291273d9f13SBarry Smith .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2292273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2293273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2294273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2295273d9f13SBarry Smith 
2296273d9f13SBarry Smith    Example usage:
2297273d9f13SBarry Smith 
2298273d9f13SBarry Smith    Consider the following 8x8 matrix with 34 non-zero values, that is
2299273d9f13SBarry Smith    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
2300273d9f13SBarry Smith    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
2301273d9f13SBarry Smith    as follows:
2302273d9f13SBarry Smith 
2303273d9f13SBarry Smith .vb
2304273d9f13SBarry Smith             1  2  0  |  0  3  0  |  0  4
2305273d9f13SBarry Smith     Proc0   0  5  6  |  7  0  0  |  8  0
2306273d9f13SBarry Smith             9  0 10  | 11  0  0  | 12  0
2307273d9f13SBarry Smith     -------------------------------------
2308273d9f13SBarry Smith            13  0 14  | 15 16 17  |  0  0
2309273d9f13SBarry Smith     Proc1   0 18  0  | 19 20 21  |  0  0
2310273d9f13SBarry Smith             0  0  0  | 22 23  0  | 24  0
2311273d9f13SBarry Smith     -------------------------------------
2312273d9f13SBarry Smith     Proc2  25 26 27  |  0  0 28  | 29  0
2313273d9f13SBarry Smith            30  0  0  | 31 32 33  |  0 34
2314273d9f13SBarry Smith .ve
2315273d9f13SBarry Smith 
2316273d9f13SBarry Smith    This can be represented as a collection of submatrices as:
2317273d9f13SBarry Smith 
2318273d9f13SBarry Smith .vb
2319273d9f13SBarry Smith       A B C
2320273d9f13SBarry Smith       D E F
2321273d9f13SBarry Smith       G H I
2322273d9f13SBarry Smith .ve
2323273d9f13SBarry Smith 
2324273d9f13SBarry Smith    Where the submatrices A,B,C are owned by proc0, D,E,F are
2325273d9f13SBarry Smith    owned by proc1, G,H,I are owned by proc2.
2326273d9f13SBarry Smith 
2327273d9f13SBarry Smith    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2328273d9f13SBarry Smith    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2329273d9f13SBarry Smith    The 'M','N' parameters are 8,8, and have the same values on all procs.
2330273d9f13SBarry Smith 
2331273d9f13SBarry Smith    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
2332273d9f13SBarry Smith    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
2333273d9f13SBarry Smith    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
2334273d9f13SBarry Smith    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
2335273d9f13SBarry Smith    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
2336273d9f13SBarry Smith    matrix, ans [DF] as another SeqAIJ matrix.
2337273d9f13SBarry Smith 
2338273d9f13SBarry Smith    When d_nz, o_nz parameters are specified, d_nz storage elements are
2339273d9f13SBarry Smith    allocated for every row of the local diagonal submatrix, and o_nz
2340273d9f13SBarry Smith    storage locations are allocated for every row of the OFF-DIAGONAL submat.
2341273d9f13SBarry Smith    One way to choose d_nz and o_nz is to use the max nonzerors per local
2342273d9f13SBarry Smith    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
2343273d9f13SBarry Smith    In this case, the values of d_nz,o_nz are:
2344273d9f13SBarry Smith .vb
2345273d9f13SBarry Smith      proc0 : dnz = 2, o_nz = 2
2346273d9f13SBarry Smith      proc1 : dnz = 3, o_nz = 2
2347273d9f13SBarry Smith      proc2 : dnz = 1, o_nz = 4
2348273d9f13SBarry Smith .ve
2349273d9f13SBarry Smith    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
2350273d9f13SBarry Smith    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
2351273d9f13SBarry Smith    for proc3. i.e we are using 12+15+10=37 storage locations to store
2352273d9f13SBarry Smith    34 values.
2353273d9f13SBarry Smith 
2354273d9f13SBarry Smith    When d_nnz, o_nnz parameters are specified, the storage is specified
2355273d9f13SBarry Smith    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
2356273d9f13SBarry Smith    In the above case the values for d_nnz,o_nnz are:
2357273d9f13SBarry Smith .vb
2358273d9f13SBarry Smith      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
2359273d9f13SBarry Smith      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
2360273d9f13SBarry Smith      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
2361273d9f13SBarry Smith .ve
2362273d9f13SBarry Smith    Here the space allocated is sum of all the above values i.e 34, and
2363273d9f13SBarry Smith    hence pre-allocation is perfect.
2364273d9f13SBarry Smith 
2365273d9f13SBarry Smith    Level: intermediate
2366273d9f13SBarry Smith 
2367273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
2368273d9f13SBarry Smith 
2369273d9f13SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues()
2370273d9f13SBarry Smith @*/
2371ca01db9bSBarry Smith int MatMPIAIJSetPreallocation(Mat B,int d_nz,const int d_nnz[],int o_nz,const int o_nnz[])
2372273d9f13SBarry Smith {
2373ca01db9bSBarry Smith   int ierr,(*f)(Mat,int,const int[],int,const int[]);
2374273d9f13SBarry Smith 
2375273d9f13SBarry Smith   PetscFunctionBegin;
2376a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatMPIAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
2377a23d5eceSKris Buschelman   if (f) {
2378a23d5eceSKris Buschelman     ierr = (*f)(B,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr);
2379273d9f13SBarry Smith   }
2380273d9f13SBarry Smith   PetscFunctionReturn(0);
2381273d9f13SBarry Smith }
2382273d9f13SBarry Smith 
23834a2ae208SSatish Balay #undef __FUNCT__
23844a2ae208SSatish Balay #define __FUNCT__ "MatCreateMPIAIJ"
2385273d9f13SBarry Smith /*@C
2386273d9f13SBarry Smith    MatCreateMPIAIJ - Creates a sparse parallel matrix in AIJ format
2387273d9f13SBarry Smith    (the default parallel PETSc format).  For good matrix assembly performance
2388273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameters
2389273d9f13SBarry Smith    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
2390273d9f13SBarry Smith    performance can be increased by more than a factor of 50.
2391273d9f13SBarry Smith 
2392273d9f13SBarry Smith    Collective on MPI_Comm
2393273d9f13SBarry Smith 
2394273d9f13SBarry Smith    Input Parameters:
2395273d9f13SBarry Smith +  comm - MPI communicator
2396273d9f13SBarry Smith .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
2397273d9f13SBarry Smith            This value should be the same as the local size used in creating the
2398273d9f13SBarry Smith            y vector for the matrix-vector product y = Ax.
2399273d9f13SBarry Smith .  n - This value should be the same as the local size used in creating the
2400273d9f13SBarry Smith        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
2401273d9f13SBarry Smith        calculated if N is given) For square matrices n is almost always m.
2402273d9f13SBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
2403273d9f13SBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
2404273d9f13SBarry Smith .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
2405273d9f13SBarry Smith            (same value is used for all local rows)
2406273d9f13SBarry Smith .  d_nnz - array containing the number of nonzeros in the various rows of the
2407273d9f13SBarry Smith            DIAGONAL portion of the local submatrix (possibly different for each row)
2408273d9f13SBarry Smith            or PETSC_NULL, if d_nz is used to specify the nonzero structure.
2409273d9f13SBarry Smith            The size of this array is equal to the number of local rows, i.e 'm'.
2410273d9f13SBarry Smith            You must leave room for the diagonal entry even if it is zero.
2411273d9f13SBarry Smith .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
2412273d9f13SBarry Smith            submatrix (same value is used for all local rows).
2413273d9f13SBarry Smith -  o_nnz - array containing the number of nonzeros in the various rows of the
2414273d9f13SBarry Smith            OFF-DIAGONAL portion of the local submatrix (possibly different for
2415273d9f13SBarry Smith            each row) or PETSC_NULL, if o_nz is used to specify the nonzero
2416273d9f13SBarry Smith            structure. The size of this array is equal to the number
2417273d9f13SBarry Smith            of local rows, i.e 'm'.
2418273d9f13SBarry Smith 
2419273d9f13SBarry Smith    Output Parameter:
2420273d9f13SBarry Smith .  A - the matrix
2421273d9f13SBarry Smith 
2422273d9f13SBarry Smith    Notes:
2423273d9f13SBarry Smith    m,n,M,N parameters specify the size of the matrix, and its partitioning across
2424273d9f13SBarry Smith    processors, while d_nz,d_nnz,o_nz,o_nnz parameters specify the approximate
2425273d9f13SBarry Smith    storage requirements for this matrix.
2426273d9f13SBarry Smith 
2427273d9f13SBarry Smith    If PETSC_DECIDE or  PETSC_DETERMINE is used for a particular argument on one
2428273d9f13SBarry Smith    processor than it must be used on all processors that share the object for
2429273d9f13SBarry Smith    that argument.
2430273d9f13SBarry Smith 
2431273d9f13SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
2432273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2433273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2434273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users manual for details.
2435273d9f13SBarry Smith 
2436273d9f13SBarry Smith    The user MUST specify either the local or global matrix dimensions
2437273d9f13SBarry Smith    (possibly both).
2438273d9f13SBarry Smith 
2439273d9f13SBarry Smith    The parallel matrix is partitioned such that the first m0 rows belong to
2440273d9f13SBarry Smith    process 0, the next m1 rows belong to process 1, the next m2 rows belong
2441273d9f13SBarry Smith    to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.
2442273d9f13SBarry Smith 
2443273d9f13SBarry Smith    The DIAGONAL portion of the local submatrix of a processor can be defined
2444273d9f13SBarry Smith    as the submatrix which is obtained by extraction the part corresponding
2445273d9f13SBarry Smith    to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the
2446273d9f13SBarry Smith    first row that belongs to the processor, and r2 is the last row belonging
2447273d9f13SBarry Smith    to the this processor. This is a square mxm matrix. The remaining portion
2448273d9f13SBarry Smith    of the local submatrix (mxN) constitute the OFF-DIAGONAL portion.
2449273d9f13SBarry Smith 
2450273d9f13SBarry Smith    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
2451273d9f13SBarry Smith 
245297d05335SKris Buschelman    When calling this routine with a single process communicator, a matrix of
245397d05335SKris Buschelman    type SEQAIJ is returned.  If a matrix of type MPIAIJ is desired for this
245497d05335SKris Buschelman    type of communicator, use the construction mechanism:
245597d05335SKris Buschelman      MatCreate(...,&A); MatSetType(A,MPIAIJ); MatMPIAIJSetPreallocation(A,...);
245697d05335SKris Buschelman 
2457273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible.
2458273d9f13SBarry Smith    We search for consecutive rows with the same nonzero structure, thereby
2459273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2460273d9f13SBarry Smith 
2461273d9f13SBarry Smith    Options Database Keys:
2462273d9f13SBarry Smith +  -mat_aij_no_inode  - Do not use inodes
2463273d9f13SBarry Smith .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2464273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2465273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2466273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2467273d9f13SBarry Smith 
2468273d9f13SBarry Smith 
2469273d9f13SBarry Smith    Example usage:
2470273d9f13SBarry Smith 
2471273d9f13SBarry Smith    Consider the following 8x8 matrix with 34 non-zero values, that is
2472273d9f13SBarry Smith    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
2473273d9f13SBarry Smith    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
2474273d9f13SBarry Smith    as follows:
2475273d9f13SBarry Smith 
2476273d9f13SBarry Smith .vb
2477273d9f13SBarry Smith             1  2  0  |  0  3  0  |  0  4
2478273d9f13SBarry Smith     Proc0   0  5  6  |  7  0  0  |  8  0
2479273d9f13SBarry Smith             9  0 10  | 11  0  0  | 12  0
2480273d9f13SBarry Smith     -------------------------------------
2481273d9f13SBarry Smith            13  0 14  | 15 16 17  |  0  0
2482273d9f13SBarry Smith     Proc1   0 18  0  | 19 20 21  |  0  0
2483273d9f13SBarry Smith             0  0  0  | 22 23  0  | 24  0
2484273d9f13SBarry Smith     -------------------------------------
2485273d9f13SBarry Smith     Proc2  25 26 27  |  0  0 28  | 29  0
2486273d9f13SBarry Smith            30  0  0  | 31 32 33  |  0 34
2487273d9f13SBarry Smith .ve
2488273d9f13SBarry Smith 
2489273d9f13SBarry Smith    This can be represented as a collection of submatrices as:
2490273d9f13SBarry Smith 
2491273d9f13SBarry Smith .vb
2492273d9f13SBarry Smith       A B C
2493273d9f13SBarry Smith       D E F
2494273d9f13SBarry Smith       G H I
2495273d9f13SBarry Smith .ve
2496273d9f13SBarry Smith 
2497273d9f13SBarry Smith    Where the submatrices A,B,C are owned by proc0, D,E,F are
2498273d9f13SBarry Smith    owned by proc1, G,H,I are owned by proc2.
2499273d9f13SBarry Smith 
2500273d9f13SBarry Smith    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2501273d9f13SBarry Smith    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2502273d9f13SBarry Smith    The 'M','N' parameters are 8,8, and have the same values on all procs.
2503273d9f13SBarry Smith 
2504273d9f13SBarry Smith    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
2505273d9f13SBarry Smith    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
2506273d9f13SBarry Smith    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
2507273d9f13SBarry Smith    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
2508273d9f13SBarry Smith    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
2509273d9f13SBarry Smith    matrix, ans [DF] as another SeqAIJ matrix.
2510273d9f13SBarry Smith 
2511273d9f13SBarry Smith    When d_nz, o_nz parameters are specified, d_nz storage elements are
2512273d9f13SBarry Smith    allocated for every row of the local diagonal submatrix, and o_nz
2513273d9f13SBarry Smith    storage locations are allocated for every row of the OFF-DIAGONAL submat.
2514273d9f13SBarry Smith    One way to choose d_nz and o_nz is to use the max nonzerors per local
2515273d9f13SBarry Smith    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
2516273d9f13SBarry Smith    In this case, the values of d_nz,o_nz are:
2517273d9f13SBarry Smith .vb
2518273d9f13SBarry Smith      proc0 : dnz = 2, o_nz = 2
2519273d9f13SBarry Smith      proc1 : dnz = 3, o_nz = 2
2520273d9f13SBarry Smith      proc2 : dnz = 1, o_nz = 4
2521273d9f13SBarry Smith .ve
2522273d9f13SBarry Smith    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
2523273d9f13SBarry Smith    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
2524273d9f13SBarry Smith    for proc3. i.e we are using 12+15+10=37 storage locations to store
2525273d9f13SBarry Smith    34 values.
2526273d9f13SBarry Smith 
2527273d9f13SBarry Smith    When d_nnz, o_nnz parameters are specified, the storage is specified
2528273d9f13SBarry Smith    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
2529273d9f13SBarry Smith    In the above case the values for d_nnz,o_nnz are:
2530273d9f13SBarry Smith .vb
2531273d9f13SBarry Smith      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
2532273d9f13SBarry Smith      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
2533273d9f13SBarry Smith      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
2534273d9f13SBarry Smith .ve
2535273d9f13SBarry Smith    Here the space allocated is sum of all the above values i.e 34, and
2536273d9f13SBarry Smith    hence pre-allocation is perfect.
2537273d9f13SBarry Smith 
2538273d9f13SBarry Smith    Level: intermediate
2539273d9f13SBarry Smith 
2540273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
2541273d9f13SBarry Smith 
2542273d9f13SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues()
2543273d9f13SBarry Smith @*/
2544ca01db9bSBarry Smith int MatCreateMPIAIJ(MPI_Comm comm,int m,int n,int M,int N,int d_nz,const int d_nnz[],int o_nz,const int o_nnz[],Mat *A)
2545273d9f13SBarry Smith {
2546273d9f13SBarry Smith   int ierr,size;
2547273d9f13SBarry Smith 
2548273d9f13SBarry Smith   PetscFunctionBegin;
2549273d9f13SBarry Smith   ierr = MatCreate(comm,m,n,M,N,A);CHKERRQ(ierr);
2550273d9f13SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
2551273d9f13SBarry Smith   if (size > 1) {
2552273d9f13SBarry Smith     ierr = MatSetType(*A,MATMPIAIJ);CHKERRQ(ierr);
2553273d9f13SBarry Smith     ierr = MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr);
2554273d9f13SBarry Smith   } else {
2555273d9f13SBarry Smith     ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2556273d9f13SBarry Smith     ierr = MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);CHKERRQ(ierr);
2557273d9f13SBarry Smith   }
2558273d9f13SBarry Smith   PetscFunctionReturn(0);
2559273d9f13SBarry Smith }
2560195d93cdSBarry Smith 
25614a2ae208SSatish Balay #undef __FUNCT__
25624a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJGetSeqAIJ"
2563*268466fbSBarry Smith int MatMPIAIJGetSeqAIJ(Mat A,Mat *Ad,Mat *Ao,int *colmap[])
2564195d93cdSBarry Smith {
2565195d93cdSBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;
2566195d93cdSBarry Smith   PetscFunctionBegin;
2567195d93cdSBarry Smith   *Ad     = a->A;
2568195d93cdSBarry Smith   *Ao     = a->B;
2569195d93cdSBarry Smith   *colmap = a->garray;
2570195d93cdSBarry Smith   PetscFunctionReturn(0);
2571195d93cdSBarry Smith }
2572a2243be0SBarry Smith 
2573a2243be0SBarry Smith #undef __FUNCT__
2574a2243be0SBarry Smith #define __FUNCT__ "MatSetColoring_MPIAIJ"
2575a2243be0SBarry Smith int MatSetColoring_MPIAIJ(Mat A,ISColoring coloring)
2576a2243be0SBarry Smith {
257708b6dcc0SBarry Smith   int        ierr,i;
2578a2243be0SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2579a2243be0SBarry Smith 
2580a2243be0SBarry Smith   PetscFunctionBegin;
2581a2243be0SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
258208b6dcc0SBarry Smith     ISColoringValue *allcolors,*colors;
2583a2243be0SBarry Smith     ISColoring      ocoloring;
2584a2243be0SBarry Smith 
2585a2243be0SBarry Smith     /* set coloring for diagonal portion */
2586a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->A,coloring);CHKERRQ(ierr);
2587a2243be0SBarry Smith 
2588a2243be0SBarry Smith     /* set coloring for off-diagonal portion */
258908b6dcc0SBarry Smith     ierr = ISAllGatherColors(A->comm,coloring->n,coloring->colors,PETSC_NULL,&allcolors);CHKERRQ(ierr);
259008b6dcc0SBarry Smith     ierr = PetscMalloc((a->B->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
2591a2243be0SBarry Smith     for (i=0; i<a->B->n; i++) {
2592a2243be0SBarry Smith       colors[i] = allcolors[a->garray[i]];
2593a2243be0SBarry Smith     }
2594a2243be0SBarry Smith     ierr = PetscFree(allcolors);CHKERRQ(ierr);
2595a2243be0SBarry Smith     ierr = ISColoringCreate(MPI_COMM_SELF,a->B->n,colors,&ocoloring);CHKERRQ(ierr);
2596a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr);
2597a2243be0SBarry Smith     ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr);
2598a2243be0SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
259908b6dcc0SBarry Smith     ISColoringValue *colors;
260008b6dcc0SBarry Smith     int             *larray;
2601a2243be0SBarry Smith     ISColoring      ocoloring;
2602a2243be0SBarry Smith 
2603a2243be0SBarry Smith     /* set coloring for diagonal portion */
2604a2243be0SBarry Smith     ierr = PetscMalloc((a->A->n+1)*sizeof(int),&larray);CHKERRQ(ierr);
2605a2243be0SBarry Smith     for (i=0; i<a->A->n; i++) {
2606a2243be0SBarry Smith       larray[i] = i + a->cstart;
2607a2243be0SBarry Smith     }
2608a2243be0SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->A->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
260908b6dcc0SBarry Smith     ierr = PetscMalloc((a->A->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
2610a2243be0SBarry Smith     for (i=0; i<a->A->n; i++) {
2611a2243be0SBarry Smith       colors[i] = coloring->colors[larray[i]];
2612a2243be0SBarry Smith     }
2613a2243be0SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
261412c595b3SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,a->A->n,colors,&ocoloring);CHKERRQ(ierr);
2615a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->A,ocoloring);CHKERRQ(ierr);
2616a2243be0SBarry Smith     ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr);
2617a2243be0SBarry Smith 
2618a2243be0SBarry Smith     /* set coloring for off-diagonal portion */
2619a2243be0SBarry Smith     ierr = PetscMalloc((a->B->n+1)*sizeof(int),&larray);CHKERRQ(ierr);
2620a2243be0SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->B->n,a->garray,PETSC_NULL,larray);CHKERRQ(ierr);
262108b6dcc0SBarry Smith     ierr = PetscMalloc((a->B->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
2622a2243be0SBarry Smith     for (i=0; i<a->B->n; i++) {
2623a2243be0SBarry Smith       colors[i] = coloring->colors[larray[i]];
2624a2243be0SBarry Smith     }
2625a2243be0SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
2626a2243be0SBarry Smith     ierr = ISColoringCreate(MPI_COMM_SELF,a->B->n,colors,&ocoloring);CHKERRQ(ierr);
2627a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr);
2628a2243be0SBarry Smith     ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr);
2629a2243be0SBarry Smith   } else {
2630a2243be0SBarry Smith     SETERRQ1(1,"No support ISColoringType %d",coloring->ctype);
2631a2243be0SBarry Smith   }
2632a2243be0SBarry Smith 
2633a2243be0SBarry Smith   PetscFunctionReturn(0);
2634a2243be0SBarry Smith }
2635a2243be0SBarry Smith 
2636a2243be0SBarry Smith #undef __FUNCT__
2637779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdic_MPIAIJ"
2638779c1a83SBarry Smith int MatSetValuesAdic_MPIAIJ(Mat A,void *advalues)
2639a2243be0SBarry Smith {
2640a2243be0SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2641a2243be0SBarry Smith   int        ierr;
2642a2243be0SBarry Smith 
2643a2243be0SBarry Smith   PetscFunctionBegin;
2644779c1a83SBarry Smith   ierr = MatSetValuesAdic_SeqAIJ(a->A,advalues);CHKERRQ(ierr);
2645779c1a83SBarry Smith   ierr = MatSetValuesAdic_SeqAIJ(a->B,advalues);CHKERRQ(ierr);
2646779c1a83SBarry Smith   PetscFunctionReturn(0);
2647779c1a83SBarry Smith }
2648779c1a83SBarry Smith 
2649779c1a83SBarry Smith #undef __FUNCT__
2650779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdifor_MPIAIJ"
2651779c1a83SBarry Smith int MatSetValuesAdifor_MPIAIJ(Mat A,int nl,void *advalues)
2652779c1a83SBarry Smith {
2653779c1a83SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2654779c1a83SBarry Smith   int        ierr;
2655779c1a83SBarry Smith 
2656779c1a83SBarry Smith   PetscFunctionBegin;
2657779c1a83SBarry Smith   ierr = MatSetValuesAdifor_SeqAIJ(a->A,nl,advalues);CHKERRQ(ierr);
2658779c1a83SBarry Smith   ierr = MatSetValuesAdifor_SeqAIJ(a->B,nl,advalues);CHKERRQ(ierr);
2659a2243be0SBarry Smith   PetscFunctionReturn(0);
2660a2243be0SBarry Smith }
2661c5d6d63eSBarry Smith 
2662c5d6d63eSBarry Smith #undef __FUNCT__
266351dd7536SBarry Smith #define __FUNCT__ "MatMerge"
2664c5d6d63eSBarry Smith /*@C
266551dd7536SBarry Smith       MatMerge - Creates a single large PETSc matrix by concatinating sequential
266651dd7536SBarry Smith                  matrices from each processor
2667c5d6d63eSBarry Smith 
2668c5d6d63eSBarry Smith     Collective on MPI_Comm
2669c5d6d63eSBarry Smith 
2670c5d6d63eSBarry Smith    Input Parameters:
267151dd7536SBarry Smith +    comm - the communicators the parallel matrix will live on
267251dd7536SBarry Smith -    inmat - the input sequential matrices
267351dd7536SBarry Smith 
267451dd7536SBarry Smith    Output Parameter:
267551dd7536SBarry Smith .    outmat - the parallel matrix generated
2676c5d6d63eSBarry Smith 
26777e25d530SSatish Balay     Level: advanced
26787e25d530SSatish Balay 
2679c5d6d63eSBarry Smith    Notes: The number of columns of the matrix in EACH of the seperate files
2680c5d6d63eSBarry Smith       MUST be the same.
2681c5d6d63eSBarry Smith 
2682c5d6d63eSBarry Smith @*/
268351dd7536SBarry Smith int MatMerge(MPI_Comm comm,Mat inmat, Mat *outmat)
2684c5d6d63eSBarry Smith {
2685a15ac5e4SHong Zhang   int         ierr,m,n,i,rstart,*indx,nnz,I,*dnz,*onz;
2686c5d6d63eSBarry Smith   PetscScalar *values;
268751dd7536SBarry Smith   PetscMap    columnmap,rowmap;
2688c5d6d63eSBarry Smith 
2689c5d6d63eSBarry Smith   PetscFunctionBegin;
2690c5d6d63eSBarry Smith 
269151dd7536SBarry Smith   ierr = MatGetSize(inmat,&m,&n);CHKERRQ(ierr);
269251dd7536SBarry Smith 
269351dd7536SBarry Smith   /* count nonzeros in each row, for diagonal and off diagonal portion of matrix */
269451dd7536SBarry Smith   ierr = PetscMapCreate(comm,&columnmap);CHKERRQ(ierr);
269551dd7536SBarry Smith   ierr = PetscMapSetSize(columnmap,n);CHKERRQ(ierr);
269651dd7536SBarry Smith   ierr = PetscMapSetType(columnmap,MAP_MPI);CHKERRQ(ierr);
269751dd7536SBarry Smith   ierr = PetscMapGetLocalSize(columnmap,&n);CHKERRQ(ierr);
269851dd7536SBarry Smith   ierr = PetscMapDestroy(columnmap);CHKERRQ(ierr);
269951dd7536SBarry Smith 
270051dd7536SBarry Smith   ierr = PetscMapCreate(comm,&rowmap);CHKERRQ(ierr);
270151dd7536SBarry Smith   ierr = PetscMapSetLocalSize(rowmap,m);CHKERRQ(ierr);
270251dd7536SBarry Smith   ierr = PetscMapSetType(rowmap,MAP_MPI);CHKERRQ(ierr);
270351dd7536SBarry Smith   ierr = PetscMapGetLocalRange(rowmap,&rstart,0);CHKERRQ(ierr);
270451dd7536SBarry Smith   ierr = PetscMapDestroy(rowmap);CHKERRQ(ierr);
270551dd7536SBarry Smith 
270651dd7536SBarry Smith   ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr);
2707c5d6d63eSBarry Smith   for (i=0;i<m;i++) {
270851dd7536SBarry Smith     ierr = MatGetRow(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
270951dd7536SBarry Smith     ierr = MatPreallocateSet(i+rstart,nnz,indx,dnz,onz);CHKERRQ(ierr);
271051dd7536SBarry Smith     ierr = MatRestoreRow(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
2711c5d6d63eSBarry Smith   }
271251dd7536SBarry Smith   ierr = MatCreateMPIAIJ(comm,m,n,PETSC_DETERMINE,PETSC_DETERMINE,0,dnz,0,onz,outmat);CHKERRQ(ierr);
271351dd7536SBarry Smith   ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr);
2714c5d6d63eSBarry Smith 
271551dd7536SBarry Smith   for (i=0;i<m;i++) {
271651dd7536SBarry Smith     ierr = MatGetRow(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
271751dd7536SBarry Smith     I    = i + rstart;
271851dd7536SBarry Smith     ierr = MatSetValues(*outmat,1,&I,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr);
271951dd7536SBarry Smith     ierr = MatRestoreRow(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
272051dd7536SBarry Smith   }
272151dd7536SBarry Smith   ierr = MatDestroy(inmat);CHKERRQ(ierr);
272251dd7536SBarry Smith   ierr = MatAssemblyBegin(*outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
272351dd7536SBarry Smith   ierr = MatAssemblyEnd(*outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
272451dd7536SBarry Smith 
2725c5d6d63eSBarry Smith   PetscFunctionReturn(0);
2726c5d6d63eSBarry Smith }
2727c5d6d63eSBarry Smith 
2728c5d6d63eSBarry Smith #undef __FUNCT__
2729c5d6d63eSBarry Smith #define __FUNCT__ "MatFileSplit"
2730c5d6d63eSBarry Smith int MatFileSplit(Mat A,char *outfile)
2731c5d6d63eSBarry Smith {
2732c5d6d63eSBarry Smith   int         ierr,rank,len,m,N,i,rstart,*indx,nnz;
2733c5d6d63eSBarry Smith   PetscViewer out;
2734c5d6d63eSBarry Smith   char        *name;
2735c5d6d63eSBarry Smith   Mat         B;
2736c5d6d63eSBarry Smith   PetscScalar *values;
2737c5d6d63eSBarry Smith 
2738c5d6d63eSBarry Smith   PetscFunctionBegin;
2739c5d6d63eSBarry Smith 
2740c5d6d63eSBarry Smith   ierr = MatGetLocalSize(A,&m,0);CHKERRQ(ierr);
2741c5d6d63eSBarry Smith   ierr = MatGetSize(A,0,&N);CHKERRQ(ierr);
2742c5d6d63eSBarry Smith   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,m,N,0,0,&B);CHKERRQ(ierr);
2743c5d6d63eSBarry Smith   ierr = MatGetOwnershipRange(A,&rstart,0);CHKERRQ(ierr);
2744c5d6d63eSBarry Smith   for (i=0;i<m;i++) {
2745c5d6d63eSBarry Smith     ierr = MatGetRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr);
2746c5d6d63eSBarry Smith     ierr = MatSetValues(B,1,&i,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr);
2747c5d6d63eSBarry Smith     ierr = MatRestoreRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr);
2748c5d6d63eSBarry Smith   }
2749c5d6d63eSBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2750c5d6d63eSBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2751c5d6d63eSBarry Smith 
2752c5d6d63eSBarry Smith   ierr = MPI_Comm_rank(A->comm,&rank);CHKERRQ(ierr);
2753c5d6d63eSBarry Smith   ierr = PetscStrlen(outfile,&len);CHKERRQ(ierr);
2754c5d6d63eSBarry Smith   ierr = PetscMalloc((len+5)*sizeof(char),&name);CHKERRQ(ierr);
2755c5d6d63eSBarry Smith   sprintf(name,"%s.%d",outfile,rank);
2756c5d6d63eSBarry Smith   ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,name,PETSC_BINARY_CREATE,&out);CHKERRQ(ierr);
2757c5d6d63eSBarry Smith   ierr = PetscFree(name);
2758c5d6d63eSBarry Smith   ierr = MatView(B,out);CHKERRQ(ierr);
2759c5d6d63eSBarry Smith   ierr = PetscViewerDestroy(out);CHKERRQ(ierr);
2760c5d6d63eSBarry Smith   ierr = MatDestroy(B);CHKERRQ(ierr);
2761c5d6d63eSBarry Smith   PetscFunctionReturn(0);
2762c5d6d63eSBarry Smith }
2763