xref: /petsc/src/mat/impls/aij/mpi/mpiaij.c (revision c537a17644e75ddee54e85f4b8155d17b42d28e5)
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"
19487828ca2SBarry Smith int MatSetValues_MPIAIJ(Mat mat,int m,int *im,int n,int *in,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;
21430770e4dSSatish Balay   int          nonew = a->nonew,shift = a->indexshift;
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)
221273d9f13SBarry Smith     if (im[i] >= mat->M) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
2220a198c4cSBarry Smith #endif
2234b0e389bSBarry Smith     if (im[i] >= rstart && im[i] < rend) {
2244b0e389bSBarry Smith       row = im[i] - rstart;
2251eb62cbbSBarry Smith       for (j=0; j<n; j++) {
2264b0e389bSBarry Smith         if (in[j] >= cstart && in[j] < cend){
2274b0e389bSBarry Smith           col = in[j] - cstart;
2284b0e389bSBarry Smith           if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
229329f5518SBarry Smith           if (ignorezeroentries && value == 0.0) continue;
23030770e4dSSatish Balay           MatSetValues_SeqAIJ_A_Private(row,col,value,addv);
2310520107fSSatish Balay           /* ierr = MatSetValues_SeqAIJ(aij->A,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */
232273d9f13SBarry Smith         } else if (in[j] < 0) continue;
233aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g)
234273d9f13SBarry Smith         else if (in[j] >= mat->N) {SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Column too large");}
2350a198c4cSBarry Smith #endif
2361eb62cbbSBarry Smith         else {
237227d817aSBarry Smith           if (mat->was_assembled) {
238905e6a2fSBarry Smith             if (!aij->colmap) {
239905e6a2fSBarry Smith               ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
240905e6a2fSBarry Smith             }
241aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
2420f5bd95cSBarry Smith             ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr);
243fa46199cSSatish Balay 	    col--;
244b1fc9764SSatish Balay #else
245905e6a2fSBarry Smith             col = aij->colmap[in[j]] - 1;
246b1fc9764SSatish Balay #endif
247ec8511deSBarry Smith             if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
2482493cbb0SBarry Smith               ierr = DisAssemble_MPIAIJ(mat);CHKERRQ(ierr);
2494b0e389bSBarry Smith               col =  in[j];
2509bf004c3SSatish Balay               /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
251f9508a3cSSatish Balay               B = aij->B;
252f9508a3cSSatish Balay               b = (Mat_SeqAIJ*)B->data;
253f9508a3cSSatish Balay               bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j;
254f9508a3cSSatish Balay               ba = b->a;
255d6dfbf8fSBarry Smith             }
256c48de900SBarry Smith           } else col = in[j];
2574b0e389bSBarry Smith           if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
258329f5518SBarry Smith           if (ignorezeroentries && value == 0.0) continue;
25930770e4dSSatish Balay           MatSetValues_SeqAIJ_B_Private(row,col,value,addv);
26030770e4dSSatish Balay           /* ierr = MatSetValues_SeqAIJ(aij->B,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */
2611eb62cbbSBarry Smith         }
2621eb62cbbSBarry Smith       }
2635ef9f2a5SBarry Smith     } else {
26490f02eecSBarry Smith       if (!aij->donotstash) {
265d36fbae8SSatish Balay         if (roworiented) {
266329f5518SBarry Smith           if (ignorezeroentries && v[i*n] == 0.0) continue;
2678798bf22SSatish Balay           ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n);CHKERRQ(ierr);
268d36fbae8SSatish Balay         } else {
269329f5518SBarry Smith           if (ignorezeroentries && v[i] == 0.0) continue;
2708798bf22SSatish Balay           ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m);CHKERRQ(ierr);
2714b0e389bSBarry Smith         }
2721eb62cbbSBarry Smith       }
2738a729477SBarry Smith     }
27490f02eecSBarry Smith   }
2753a40ed3dSBarry Smith   PetscFunctionReturn(0);
2768a729477SBarry Smith }
2778a729477SBarry Smith 
2784a2ae208SSatish Balay #undef __FUNCT__
2794a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_MPIAIJ"
28087828ca2SBarry Smith int MatGetValues_MPIAIJ(Mat mat,int m,int *idxm,int n,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++) {
28829bbc08cSBarry Smith     if (idxm[i] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Negative row");
289273d9f13SBarry Smith     if (idxm[i] >= mat->M) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
290b49de8d1SLois Curfman McInnes     if (idxm[i] >= rstart && idxm[i] < rend) {
291b49de8d1SLois Curfman McInnes       row = idxm[i] - rstart;
292b49de8d1SLois Curfman McInnes       for (j=0; j<n; j++) {
29329bbc08cSBarry Smith         if (idxn[j] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Negative column");
294273d9f13SBarry Smith         if (idxn[j] >= mat->N) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
295b49de8d1SLois Curfman McInnes         if (idxn[j] >= cstart && idxn[j] < cend){
296b49de8d1SLois Curfman McInnes           col = idxn[j] - cstart;
297b49de8d1SLois Curfman McInnes           ierr = MatGetValues(aij->A,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
298fa852ad4SSatish Balay         } else {
299905e6a2fSBarry Smith           if (!aij->colmap) {
300905e6a2fSBarry Smith             ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
301905e6a2fSBarry Smith           }
302aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
3030f5bd95cSBarry Smith           ierr = PetscTableFind(aij->colmap,idxn[j]+1,&col);CHKERRQ(ierr);
304fa46199cSSatish Balay           col --;
305b1fc9764SSatish Balay #else
306905e6a2fSBarry Smith           col = aij->colmap[idxn[j]] - 1;
307b1fc9764SSatish Balay #endif
308e60e1c95SSatish Balay           if ((col < 0) || (aij->garray[col] != idxn[j])) *(v+i*n+j) = 0.0;
309d9d09a02SSatish Balay           else {
310b49de8d1SLois Curfman McInnes             ierr = MatGetValues(aij->B,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
311b49de8d1SLois Curfman McInnes           }
312b49de8d1SLois Curfman McInnes         }
313b49de8d1SLois Curfman McInnes       }
314a8c6a408SBarry Smith     } else {
31529bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"Only local values currently supported");
316b49de8d1SLois Curfman McInnes     }
317b49de8d1SLois Curfman McInnes   }
3183a40ed3dSBarry Smith   PetscFunctionReturn(0);
319b49de8d1SLois Curfman McInnes }
320bc5ccf88SSatish Balay 
3214a2ae208SSatish Balay #undef __FUNCT__
3224a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyBegin_MPIAIJ"
323bc5ccf88SSatish Balay int MatAssemblyBegin_MPIAIJ(Mat mat,MatAssemblyType mode)
324bc5ccf88SSatish Balay {
325bc5ccf88SSatish Balay   Mat_MPIAIJ  *aij = (Mat_MPIAIJ*)mat->data;
326d36fbae8SSatish Balay   int         ierr,nstash,reallocs;
327bc5ccf88SSatish Balay   InsertMode  addv;
328bc5ccf88SSatish Balay 
329bc5ccf88SSatish Balay   PetscFunctionBegin;
330bc5ccf88SSatish Balay   if (aij->donotstash) {
331bc5ccf88SSatish Balay     PetscFunctionReturn(0);
332bc5ccf88SSatish Balay   }
333bc5ccf88SSatish Balay 
334bc5ccf88SSatish Balay   /* make sure all processors are either in INSERTMODE or ADDMODE */
335bc5ccf88SSatish Balay   ierr = MPI_Allreduce(&mat->insertmode,&addv,1,MPI_INT,MPI_BOR,mat->comm);CHKERRQ(ierr);
336bc5ccf88SSatish Balay   if (addv == (ADD_VALUES|INSERT_VALUES)) {
33729bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Some processors inserted others added");
338bc5ccf88SSatish Balay   }
339bc5ccf88SSatish Balay   mat->insertmode = addv; /* in case this processor had no cache */
340bc5ccf88SSatish Balay 
3418798bf22SSatish Balay   ierr = MatStashScatterBegin_Private(&mat->stash,aij->rowners);CHKERRQ(ierr);
3428798bf22SSatish Balay   ierr = MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);CHKERRQ(ierr);
343b0a32e0cSBarry Smith   PetscLogInfo(aij->A,"MatAssemblyBegin_MPIAIJ:Stash has %d entries, uses %d mallocs.\n",nstash,reallocs);
344bc5ccf88SSatish Balay   PetscFunctionReturn(0);
345bc5ccf88SSatish Balay }
346bc5ccf88SSatish Balay 
347bc5ccf88SSatish Balay 
3484a2ae208SSatish Balay #undef __FUNCT__
3494a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_MPIAIJ"
350bc5ccf88SSatish Balay int MatAssemblyEnd_MPIAIJ(Mat mat,MatAssemblyType mode)
351bc5ccf88SSatish Balay {
352bc5ccf88SSatish Balay   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
353a2d1c673SSatish Balay   int         i,j,rstart,ncols,n,ierr,flg;
354bc5ccf88SSatish Balay   int         *row,*col,other_disassembled;
35587828ca2SBarry Smith   PetscScalar *val;
356bc5ccf88SSatish Balay   InsertMode  addv = mat->insertmode;
3574032b0d1SHong Zhang #if defined(PETSC_HAVE_SUPERLUDIST) || defined(PETSC_HAVE_SPOOLES)
3589b9367d5SHong Zhang   PetscTruth  flag;
3599e070d67SMatthew Knepley #endif
360bc5ccf88SSatish Balay 
361bc5ccf88SSatish Balay   PetscFunctionBegin;
362bc5ccf88SSatish Balay   if (!aij->donotstash) {
363a2d1c673SSatish Balay     while (1) {
3648798bf22SSatish Balay       ierr = MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);CHKERRQ(ierr);
365a2d1c673SSatish Balay       if (!flg) break;
366a2d1c673SSatish Balay 
367bc5ccf88SSatish Balay       for (i=0; i<n;) {
368bc5ccf88SSatish Balay         /* Now identify the consecutive vals belonging to the same row */
369bc5ccf88SSatish Balay         for (j=i,rstart=row[j]; j<n; j++) { if (row[j] != rstart) break; }
370bc5ccf88SSatish Balay         if (j < n) ncols = j-i;
371bc5ccf88SSatish Balay         else       ncols = n-i;
372bc5ccf88SSatish Balay         /* Now assemble all these values with a single function call */
373bc5ccf88SSatish Balay         ierr = MatSetValues_MPIAIJ(mat,1,row+i,ncols,col+i,val+i,addv);CHKERRQ(ierr);
374bc5ccf88SSatish Balay         i = j;
375bc5ccf88SSatish Balay       }
376bc5ccf88SSatish Balay     }
3778798bf22SSatish Balay     ierr = MatStashScatterEnd_Private(&mat->stash);CHKERRQ(ierr);
378bc5ccf88SSatish Balay   }
379bc5ccf88SSatish Balay 
380bc5ccf88SSatish Balay   ierr = MatAssemblyBegin(aij->A,mode);CHKERRQ(ierr);
381bc5ccf88SSatish Balay   ierr = MatAssemblyEnd(aij->A,mode);CHKERRQ(ierr);
382bc5ccf88SSatish Balay 
383bc5ccf88SSatish Balay   /* determine if any processor has disassembled, if so we must
384bc5ccf88SSatish Balay      also disassemble ourselfs, in order that we may reassemble. */
385bc5ccf88SSatish Balay   /*
386bc5ccf88SSatish Balay      if nonzero structure of submatrix B cannot change then we know that
387bc5ccf88SSatish Balay      no processor disassembled thus we can skip this stuff
388bc5ccf88SSatish Balay   */
389bc5ccf88SSatish Balay   if (!((Mat_SeqAIJ*)aij->B->data)->nonew)  {
390bc5ccf88SSatish Balay     ierr = MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPI_INT,MPI_PROD,mat->comm);CHKERRQ(ierr);
391bc5ccf88SSatish Balay     if (mat->was_assembled && !other_disassembled) {
392bc5ccf88SSatish Balay       ierr = DisAssemble_MPIAIJ(mat);CHKERRQ(ierr);
393bc5ccf88SSatish Balay     }
394bc5ccf88SSatish Balay   }
395bc5ccf88SSatish Balay 
396bc5ccf88SSatish Balay   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
397bc5ccf88SSatish Balay     ierr = MatSetUpMultiply_MPIAIJ(mat);CHKERRQ(ierr);
398bc5ccf88SSatish Balay   }
399bc5ccf88SSatish Balay   ierr = MatAssemblyBegin(aij->B,mode);CHKERRQ(ierr);
400bc5ccf88SSatish Balay   ierr = MatAssemblyEnd(aij->B,mode);CHKERRQ(ierr);
401bc5ccf88SSatish Balay 
402606d414cSSatish Balay   if (aij->rowvalues) {
403606d414cSSatish Balay     ierr = PetscFree(aij->rowvalues);CHKERRQ(ierr);
404606d414cSSatish Balay     aij->rowvalues = 0;
405606d414cSSatish Balay   }
4069b9367d5SHong Zhang #if defined(PETSC_HAVE_SUPERLUDIST)
407e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(mat->prefix,"-mat_aij_superlu_dist",&flag);CHKERRQ(ierr);
4089b9367d5SHong Zhang   if (flag) { ierr = MatUseSuperLU_DIST_MPIAIJ(mat);CHKERRQ(ierr); }
4099b9367d5SHong Zhang #endif
4104032b0d1SHong Zhang 
4114032b0d1SHong Zhang #if defined(PETSC_HAVE_SPOOLES)
412e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(mat->prefix,"-mat_aij_spooles",&flag);CHKERRQ(ierr);
4134032b0d1SHong Zhang   if (flag) { ierr = MatUseSpooles_MPIAIJ(mat);CHKERRQ(ierr); }
4144032b0d1SHong Zhang #endif
415bc5ccf88SSatish Balay   PetscFunctionReturn(0);
416bc5ccf88SSatish Balay }
417bc5ccf88SSatish Balay 
4184a2ae208SSatish Balay #undef __FUNCT__
4194a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_MPIAIJ"
4208f6be9afSLois Curfman McInnes int MatZeroEntries_MPIAIJ(Mat A)
4211eb62cbbSBarry Smith {
42244a69424SLois Curfman McInnes   Mat_MPIAIJ *l = (Mat_MPIAIJ*)A->data;
423dbd7a890SLois Curfman McInnes   int        ierr;
4243a40ed3dSBarry Smith 
4253a40ed3dSBarry Smith   PetscFunctionBegin;
42678b31e54SBarry Smith   ierr = MatZeroEntries(l->A);CHKERRQ(ierr);
42778b31e54SBarry Smith   ierr = MatZeroEntries(l->B);CHKERRQ(ierr);
4283a40ed3dSBarry Smith   PetscFunctionReturn(0);
4291eb62cbbSBarry Smith }
4301eb62cbbSBarry Smith 
4314a2ae208SSatish Balay #undef __FUNCT__
4324a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_MPIAIJ"
43387828ca2SBarry Smith int MatZeroRows_MPIAIJ(Mat A,IS is,PetscScalar *diag)
4341eb62cbbSBarry Smith {
43544a69424SLois Curfman McInnes   Mat_MPIAIJ     *l = (Mat_MPIAIJ*)A->data;
43617699dbbSLois Curfman McInnes   int            i,ierr,N,*rows,*owners = l->rowners,size = l->size;
437c1dc657dSBarry Smith   int            *nprocs,j,idx,nsends,row;
43817699dbbSLois Curfman McInnes   int            nmax,*svalues,*starts,*owner,nrecvs,rank = l->rank;
4395392566eSBarry Smith   int            *rvalues,tag = A->tag,count,base,slen,n,*source;
4406a5c57faSSatish Balay   int            *lens,imdex,*lrows,*values,rstart=l->rstart;
441d6dfbf8fSBarry Smith   MPI_Comm       comm = A->comm;
4421eb62cbbSBarry Smith   MPI_Request    *send_waits,*recv_waits;
4431eb62cbbSBarry Smith   MPI_Status     recv_status,*send_status;
4441eb62cbbSBarry Smith   IS             istmp;
44535d8aa7fSBarry Smith   PetscTruth     found;
4461eb62cbbSBarry Smith 
4473a40ed3dSBarry Smith   PetscFunctionBegin;
448e03a110bSBarry Smith   ierr = ISGetLocalSize(is,&N);CHKERRQ(ierr);
44978b31e54SBarry Smith   ierr = ISGetIndices(is,&rows);CHKERRQ(ierr);
4501eb62cbbSBarry Smith 
4511eb62cbbSBarry Smith   /*  first count number of contributors to each processor */
452b0a32e0cSBarry Smith   ierr = PetscMalloc(2*size*sizeof(int),&nprocs);CHKERRQ(ierr);
453549d3d68SSatish Balay   ierr   = PetscMemzero(nprocs,2*size*sizeof(int));CHKERRQ(ierr);
454b0a32e0cSBarry Smith   ierr = PetscMalloc((N+1)*sizeof(int),&owner);CHKERRQ(ierr); /* see note*/
4551eb62cbbSBarry Smith   for (i=0; i<N; i++) {
4561eb62cbbSBarry Smith     idx = rows[i];
45735d8aa7fSBarry Smith     found = PETSC_FALSE;
45817699dbbSLois Curfman McInnes     for (j=0; j<size; j++) {
4591eb62cbbSBarry Smith       if (idx >= owners[j] && idx < owners[j+1]) {
460c1dc657dSBarry Smith         nprocs[2*j]++; nprocs[2*j+1] = 1; owner[i] = j; found = PETSC_TRUE; break;
4611eb62cbbSBarry Smith       }
4621eb62cbbSBarry Smith     }
46329bbc08cSBarry Smith     if (!found) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Index out of range");
4641eb62cbbSBarry Smith   }
465c1dc657dSBarry Smith   nsends = 0;  for (i=0; i<size; i++) { nsends += nprocs[2*i+1];}
4661eb62cbbSBarry Smith 
4671eb62cbbSBarry Smith   /* inform other processors of number of messages and max length*/
468c1dc657dSBarry Smith   ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr);
4691eb62cbbSBarry Smith 
4701eb62cbbSBarry Smith   /* post receives:   */
471b0a32e0cSBarry Smith   ierr = PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(int),&rvalues);CHKERRQ(ierr);
472b0a32e0cSBarry Smith   ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr);
4731eb62cbbSBarry Smith   for (i=0; i<nrecvs; i++) {
474ca161407SBarry Smith     ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPI_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr);
4751eb62cbbSBarry Smith   }
4761eb62cbbSBarry Smith 
4771eb62cbbSBarry Smith   /* do sends:
4781eb62cbbSBarry Smith       1) starts[i] gives the starting index in svalues for stuff going to
4791eb62cbbSBarry Smith          the ith processor
4801eb62cbbSBarry Smith   */
481b0a32e0cSBarry Smith   ierr = PetscMalloc((N+1)*sizeof(int),&svalues);CHKERRQ(ierr);
482b0a32e0cSBarry Smith   ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr);
483b0a32e0cSBarry Smith   ierr = PetscMalloc((size+1)*sizeof(int),&starts);CHKERRQ(ierr);
4841eb62cbbSBarry Smith   starts[0] = 0;
485c1dc657dSBarry Smith   for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
4861eb62cbbSBarry Smith   for (i=0; i<N; i++) {
4871eb62cbbSBarry Smith     svalues[starts[owner[i]]++] = rows[i];
4881eb62cbbSBarry Smith   }
4896831982aSBarry Smith   ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr);
4901eb62cbbSBarry Smith 
4911eb62cbbSBarry Smith   starts[0] = 0;
492c1dc657dSBarry Smith   for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
4931eb62cbbSBarry Smith   count = 0;
49417699dbbSLois Curfman McInnes   for (i=0; i<size; i++) {
495c1dc657dSBarry Smith     if (nprocs[2*i+1]) {
496c1dc657dSBarry Smith       ierr = MPI_Isend(svalues+starts[i],nprocs[2*i],MPI_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr);
4971eb62cbbSBarry Smith     }
4981eb62cbbSBarry Smith   }
499606d414cSSatish Balay   ierr = PetscFree(starts);CHKERRQ(ierr);
5001eb62cbbSBarry Smith 
50117699dbbSLois Curfman McInnes   base = owners[rank];
5021eb62cbbSBarry Smith 
5031eb62cbbSBarry Smith   /*  wait on receives */
504b0a32e0cSBarry Smith   ierr   = PetscMalloc(2*(nrecvs+1)*sizeof(int),&lens);CHKERRQ(ierr);
5051eb62cbbSBarry Smith   source = lens + nrecvs;
5061eb62cbbSBarry Smith   count  = nrecvs; slen = 0;
5071eb62cbbSBarry Smith   while (count) {
508ca161407SBarry Smith     ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
5091eb62cbbSBarry Smith     /* unpack receives into our local space */
510ca161407SBarry Smith     ierr = MPI_Get_count(&recv_status,MPI_INT,&n);CHKERRQ(ierr);
511d6dfbf8fSBarry Smith     source[imdex]  = recv_status.MPI_SOURCE;
512d6dfbf8fSBarry Smith     lens[imdex]    = n;
5131eb62cbbSBarry Smith     slen          += n;
5141eb62cbbSBarry Smith     count--;
5151eb62cbbSBarry Smith   }
516606d414cSSatish Balay   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
5171eb62cbbSBarry Smith 
5181eb62cbbSBarry Smith   /* move the data into the send scatter */
519b0a32e0cSBarry Smith   ierr = PetscMalloc((slen+1)*sizeof(int),&lrows);CHKERRQ(ierr);
5201eb62cbbSBarry Smith   count = 0;
5211eb62cbbSBarry Smith   for (i=0; i<nrecvs; i++) {
5221eb62cbbSBarry Smith     values = rvalues + i*nmax;
5231eb62cbbSBarry Smith     for (j=0; j<lens[i]; j++) {
5241eb62cbbSBarry Smith       lrows[count++] = values[j] - base;
5251eb62cbbSBarry Smith     }
5261eb62cbbSBarry Smith   }
527606d414cSSatish Balay   ierr = PetscFree(rvalues);CHKERRQ(ierr);
528606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
529606d414cSSatish Balay   ierr = PetscFree(owner);CHKERRQ(ierr);
530606d414cSSatish Balay   ierr = PetscFree(nprocs);CHKERRQ(ierr);
5311eb62cbbSBarry Smith 
5321eb62cbbSBarry Smith   /* actually zap the local rows */
533029af93fSBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,slen,lrows,&istmp);CHKERRQ(ierr);
534b0a32e0cSBarry Smith   PetscLogObjectParent(A,istmp);
5356a5c57faSSatish Balay 
5366eb55b6aSBarry Smith   /*
5376eb55b6aSBarry Smith         Zero the required rows. If the "diagonal block" of the matrix
5386eb55b6aSBarry Smith      is square and the user wishes to set the diagonal we use seperate
5396eb55b6aSBarry Smith      code so that MatSetValues() is not called for each diagonal allocating
5406eb55b6aSBarry Smith      new memory, thus calling lots of mallocs and slowing things down.
5416eb55b6aSBarry Smith 
5426eb55b6aSBarry Smith        Contributed by: Mathew Knepley
5436eb55b6aSBarry Smith   */
544e2d53e46SBarry Smith   /* must zero l->B before l->A because the (diag) case below may put values into l->B*/
545e2d53e46SBarry Smith   ierr = MatZeroRows(l->B,istmp,0);CHKERRQ(ierr);
5466eb55b6aSBarry Smith   if (diag && (l->A->M == l->A->N)) {
5476eb55b6aSBarry Smith     ierr      = MatZeroRows(l->A,istmp,diag);CHKERRQ(ierr);
548e2d53e46SBarry Smith   } else if (diag) {
549e2d53e46SBarry Smith     ierr = MatZeroRows(l->A,istmp,0);CHKERRQ(ierr);
550fa46199cSSatish Balay     if (((Mat_SeqAIJ*)l->A->data)->nonew) {
55129bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"MatZeroRows() on rectangular matrices cannot be used with the Mat options\n\
552fa46199cSSatish Balay MAT_NO_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR");
5536525c446SSatish Balay     }
554e2d53e46SBarry Smith     for (i = 0; i < slen; i++) {
555e2d53e46SBarry Smith       row  = lrows[i] + rstart;
556e2d53e46SBarry Smith       ierr = MatSetValues(A,1,&row,1,&row,diag,INSERT_VALUES);CHKERRQ(ierr);
557e2d53e46SBarry Smith     }
558e2d53e46SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
559e2d53e46SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
5606eb55b6aSBarry Smith   } else {
5616a5c57faSSatish Balay     ierr = MatZeroRows(l->A,istmp,0);CHKERRQ(ierr);
5626eb55b6aSBarry Smith   }
56378b31e54SBarry Smith   ierr = ISDestroy(istmp);CHKERRQ(ierr);
564606d414cSSatish Balay   ierr = PetscFree(lrows);CHKERRQ(ierr);
56572dacd9aSBarry Smith 
5661eb62cbbSBarry Smith   /* wait on sends */
5671eb62cbbSBarry Smith   if (nsends) {
568b0a32e0cSBarry Smith     ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr);
569ca161407SBarry Smith     ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr);
570606d414cSSatish Balay     ierr = PetscFree(send_status);CHKERRQ(ierr);
5711eb62cbbSBarry Smith   }
572606d414cSSatish Balay   ierr = PetscFree(send_waits);CHKERRQ(ierr);
573606d414cSSatish Balay   ierr = PetscFree(svalues);CHKERRQ(ierr);
5741eb62cbbSBarry Smith 
5753a40ed3dSBarry Smith   PetscFunctionReturn(0);
5761eb62cbbSBarry Smith }
5771eb62cbbSBarry Smith 
5784a2ae208SSatish Balay #undef __FUNCT__
5794a2ae208SSatish Balay #define __FUNCT__ "MatMult_MPIAIJ"
5808f6be9afSLois Curfman McInnes int MatMult_MPIAIJ(Mat A,Vec xx,Vec yy)
5811eb62cbbSBarry Smith {
582416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
583fbd6ef76SBarry Smith   int        ierr,nt;
584416022c9SBarry Smith 
5853a40ed3dSBarry Smith   PetscFunctionBegin;
586a2ce50c7SBarry Smith   ierr = VecGetLocalSize(xx,&nt);CHKERRQ(ierr);
587273d9f13SBarry Smith   if (nt != A->n) {
588273d9f13SBarry Smith     SETERRQ2(PETSC_ERR_ARG_SIZ,"Incompatible partition of A (%d) and xx (%d)",A->n,nt);
589fbd6ef76SBarry Smith   }
59043a90d84SBarry Smith   ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
591f830108cSBarry Smith   ierr = (*a->A->ops->mult)(a->A,xx,yy);CHKERRQ(ierr);
59243a90d84SBarry Smith   ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
593f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);CHKERRQ(ierr);
5943a40ed3dSBarry Smith   PetscFunctionReturn(0);
5951eb62cbbSBarry Smith }
5961eb62cbbSBarry Smith 
5974a2ae208SSatish Balay #undef __FUNCT__
5984a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_MPIAIJ"
5998f6be9afSLois Curfman McInnes int MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
600da3a660dSBarry Smith {
601416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
602da3a660dSBarry Smith   int        ierr;
6033a40ed3dSBarry Smith 
6043a40ed3dSBarry Smith   PetscFunctionBegin;
60543a90d84SBarry Smith   ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
606f830108cSBarry Smith   ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
60743a90d84SBarry Smith   ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
608f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);CHKERRQ(ierr);
6093a40ed3dSBarry Smith   PetscFunctionReturn(0);
610da3a660dSBarry Smith }
611da3a660dSBarry Smith 
6124a2ae208SSatish Balay #undef __FUNCT__
6134a2ae208SSatish Balay #define __FUNCT__ "MatMultTranspose_MPIAIJ"
6147c922b88SBarry Smith int MatMultTranspose_MPIAIJ(Mat A,Vec xx,Vec yy)
615da3a660dSBarry Smith {
616416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
617da3a660dSBarry Smith   int        ierr;
618da3a660dSBarry Smith 
6193a40ed3dSBarry Smith   PetscFunctionBegin;
620da3a660dSBarry Smith   /* do nondiagonal part */
6217c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
622da3a660dSBarry Smith   /* send it on its way */
623537820f0SBarry Smith   ierr = VecScatterBegin(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
624da3a660dSBarry Smith   /* do local part */
6257c922b88SBarry Smith   ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr);
626da3a660dSBarry Smith   /* receive remote parts: note this assumes the values are not actually */
627da3a660dSBarry Smith   /* inserted in yy until the next line, which is true for my implementation*/
628da3a660dSBarry Smith   /* but is not perhaps always true. */
629537820f0SBarry Smith   ierr = VecScatterEnd(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
6303a40ed3dSBarry Smith   PetscFunctionReturn(0);
631da3a660dSBarry Smith }
632da3a660dSBarry Smith 
6334a2ae208SSatish Balay #undef __FUNCT__
6344a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_MPIAIJ"
6357c922b88SBarry Smith int MatMultTransposeAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
636da3a660dSBarry Smith {
637416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
638da3a660dSBarry Smith   int        ierr;
639da3a660dSBarry Smith 
6403a40ed3dSBarry Smith   PetscFunctionBegin;
641da3a660dSBarry Smith   /* do nondiagonal part */
6427c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
643da3a660dSBarry Smith   /* send it on its way */
644537820f0SBarry Smith   ierr = VecScatterBegin(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
645da3a660dSBarry Smith   /* do local part */
6467c922b88SBarry Smith   ierr = (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
647da3a660dSBarry Smith   /* receive remote parts: note this assumes the values are not actually */
648da3a660dSBarry Smith   /* inserted in yy until the next line, which is true for my implementation*/
649da3a660dSBarry Smith   /* but is not perhaps always true. */
6500a198c4cSBarry Smith   ierr = VecScatterEnd(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
6513a40ed3dSBarry Smith   PetscFunctionReturn(0);
652da3a660dSBarry Smith }
653da3a660dSBarry Smith 
6541eb62cbbSBarry Smith /*
6551eb62cbbSBarry Smith   This only works correctly for square matrices where the subblock A->A is the
6561eb62cbbSBarry Smith    diagonal block
6571eb62cbbSBarry Smith */
6584a2ae208SSatish Balay #undef __FUNCT__
6594a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_MPIAIJ"
6608f6be9afSLois Curfman McInnes int MatGetDiagonal_MPIAIJ(Mat A,Vec v)
6611eb62cbbSBarry Smith {
6623a40ed3dSBarry Smith   int        ierr;
663416022c9SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
6643a40ed3dSBarry Smith 
6653a40ed3dSBarry Smith   PetscFunctionBegin;
666273d9f13SBarry Smith   if (A->M != A->N) SETERRQ(PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block");
6675baf8537SBarry Smith   if (a->rstart != a->cstart || a->rend != a->cend) {
66829bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_SIZ,"row partition must equal col partition");
6693a40ed3dSBarry Smith   }
6703a40ed3dSBarry Smith   ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr);
6713a40ed3dSBarry Smith   PetscFunctionReturn(0);
6721eb62cbbSBarry Smith }
6731eb62cbbSBarry Smith 
6744a2ae208SSatish Balay #undef __FUNCT__
6754a2ae208SSatish Balay #define __FUNCT__ "MatScale_MPIAIJ"
67687828ca2SBarry Smith int MatScale_MPIAIJ(PetscScalar *aa,Mat A)
677052efed2SBarry Smith {
678052efed2SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
679052efed2SBarry Smith   int        ierr;
6803a40ed3dSBarry Smith 
6813a40ed3dSBarry Smith   PetscFunctionBegin;
682052efed2SBarry Smith   ierr = MatScale(aa,a->A);CHKERRQ(ierr);
683052efed2SBarry Smith   ierr = MatScale(aa,a->B);CHKERRQ(ierr);
6843a40ed3dSBarry Smith   PetscFunctionReturn(0);
685052efed2SBarry Smith }
686052efed2SBarry Smith 
6874a2ae208SSatish Balay #undef __FUNCT__
6884a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_MPIAIJ"
689e1311b90SBarry Smith int MatDestroy_MPIAIJ(Mat mat)
6901eb62cbbSBarry Smith {
69144a69424SLois Curfman McInnes   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
6921eb62cbbSBarry Smith   int        ierr;
69383e2fdc7SBarry Smith 
6943a40ed3dSBarry Smith   PetscFunctionBegin;
695aa482453SBarry Smith #if defined(PETSC_USE_LOG)
696b0a32e0cSBarry Smith   PetscLogObjectState((PetscObject)mat,"Rows=%d, Cols=%d",mat->M,mat->N);
697a5a9c739SBarry Smith #endif
6988798bf22SSatish Balay   ierr = MatStashDestroy_Private(&mat->stash);CHKERRQ(ierr);
699606d414cSSatish Balay   ierr = PetscFree(aij->rowners);CHKERRQ(ierr);
70078b31e54SBarry Smith   ierr = MatDestroy(aij->A);CHKERRQ(ierr);
70178b31e54SBarry Smith   ierr = MatDestroy(aij->B);CHKERRQ(ierr);
702aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
7030f5bd95cSBarry Smith   if (aij->colmap) {ierr = PetscTableDelete(aij->colmap);CHKERRQ(ierr);}
704b1fc9764SSatish Balay #else
705606d414cSSatish Balay   if (aij->colmap) {ierr = PetscFree(aij->colmap);CHKERRQ(ierr);}
706b1fc9764SSatish Balay #endif
707606d414cSSatish Balay   if (aij->garray) {ierr = PetscFree(aij->garray);CHKERRQ(ierr);}
7087c922b88SBarry Smith   if (aij->lvec)   {ierr = VecDestroy(aij->lvec);CHKERRQ(ierr);}
7097c922b88SBarry Smith   if (aij->Mvctx)  {ierr = VecScatterDestroy(aij->Mvctx);CHKERRQ(ierr);}
710606d414cSSatish Balay   if (aij->rowvalues) {ierr = PetscFree(aij->rowvalues);CHKERRQ(ierr);}
711606d414cSSatish Balay   ierr = PetscFree(aij);CHKERRQ(ierr);
7123a40ed3dSBarry Smith   PetscFunctionReturn(0);
7131eb62cbbSBarry Smith }
714ee50ffe9SBarry Smith 
7154aedb280SBarry Smith extern int MatMPIAIJFactorInfo_SuperLu(Mat,PetscViewer);
716a072f7f7SHong Zhang extern int MatFactorInfo_Spooles(Mat,PetscViewer);
7174aedb280SBarry Smith 
7184a2ae208SSatish Balay #undef __FUNCT__
7198e2fed03SBarry Smith #define __FUNCT__ "MatView_MPIAIJ_Binary"
7208e2fed03SBarry Smith int MatView_MPIAIJ_Binary(Mat mat,PetscViewer viewer)
7218e2fed03SBarry Smith {
7228e2fed03SBarry Smith   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*)mat->data;
7238e2fed03SBarry Smith   Mat_SeqAIJ*       A = (Mat_SeqAIJ*)aij->A->data;
7248e2fed03SBarry Smith   Mat_SeqAIJ*       B = (Mat_SeqAIJ*)aij->B->data;
7258e2fed03SBarry Smith   int               nz,fd,ierr,header[4],rank,size,*row_lengths,*range,rlen,i,tag = ((PetscObject)viewer)->tag;
726b021249fSBarry Smith   int               nzmax,*column_indices,j,k,col,*garray = aij->garray,cnt,cstart = aij->cstart,rnz;
7278e2fed03SBarry Smith   PetscScalar       *column_values;
7288e2fed03SBarry Smith 
7298e2fed03SBarry Smith   PetscFunctionBegin;
7308e2fed03SBarry Smith   ierr = MPI_Comm_rank(mat->comm,&rank);CHKERRQ(ierr);
7318e2fed03SBarry Smith   ierr = MPI_Comm_size(mat->comm,&size);CHKERRQ(ierr);
7328e2fed03SBarry Smith   nz   = A->nz + B->nz;
7338e2fed03SBarry Smith   if (rank == 0) {
7348e2fed03SBarry Smith     header[0] = MAT_FILE_COOKIE;
7358e2fed03SBarry Smith     header[1] = mat->M;
7368e2fed03SBarry Smith     header[2] = mat->N;
7378e2fed03SBarry Smith     ierr = MPI_Reduce(&nz,&header[3],1,MPI_INT,MPI_SUM,0,mat->comm);CHKERRQ(ierr);
7388e2fed03SBarry Smith     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
7398e2fed03SBarry Smith     ierr = PetscBinaryWrite(fd,header,4,PETSC_INT,1);CHKERRQ(ierr);
7408e2fed03SBarry Smith     /* get largest number of rows any processor has */
7418e2fed03SBarry Smith     rlen = mat->m;
7428e2fed03SBarry Smith     ierr = PetscMapGetGlobalRange(mat->rmap,&range);CHKERRQ(ierr);
7438e2fed03SBarry Smith     for (i=1; i<size; i++) {
7448e2fed03SBarry Smith       rlen = PetscMax(rlen,range[i+1] - range[i]);
7458e2fed03SBarry Smith     }
7468e2fed03SBarry Smith   } else {
7478e2fed03SBarry Smith     ierr = MPI_Reduce(&nz,0,1,MPI_INT,MPI_SUM,0,mat->comm);CHKERRQ(ierr);
7488e2fed03SBarry Smith     rlen = mat->m;
7498e2fed03SBarry Smith   }
7508e2fed03SBarry Smith 
7518e2fed03SBarry Smith   /* load up the local row counts */
7528e2fed03SBarry Smith   ierr = PetscMalloc((rlen+1)*sizeof(int),&row_lengths);CHKERRQ(ierr);
7538e2fed03SBarry Smith   for (i=0; i<mat->m; i++) {
7548e2fed03SBarry Smith     row_lengths[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i];
7558e2fed03SBarry Smith   }
7568e2fed03SBarry Smith 
7578e2fed03SBarry Smith   /* store the row lengths to the file */
7588e2fed03SBarry Smith   if (rank == 0) {
7598e2fed03SBarry Smith     MPI_Status status;
7608e2fed03SBarry Smith     ierr = PetscBinaryWrite(fd,row_lengths,mat->m,PETSC_INT,1);CHKERRQ(ierr);
7618e2fed03SBarry Smith     for (i=1; i<size; i++) {
7628e2fed03SBarry Smith       rlen = range[i+1] - range[i];
7638e2fed03SBarry Smith       ierr = MPI_Recv(row_lengths,rlen,MPI_INT,i,tag,mat->comm,&status);CHKERRQ(ierr);
7648e2fed03SBarry Smith       ierr = PetscBinaryWrite(fd,row_lengths,rlen,PETSC_INT,1);CHKERRQ(ierr);
7658e2fed03SBarry Smith     }
7668e2fed03SBarry Smith   } else {
7678e2fed03SBarry Smith     ierr = MPI_Send(row_lengths,mat->m,MPI_INT,0,tag,mat->comm);CHKERRQ(ierr);
7688e2fed03SBarry Smith   }
7698e2fed03SBarry Smith   ierr = PetscFree(row_lengths);CHKERRQ(ierr);
7708e2fed03SBarry Smith 
7718e2fed03SBarry Smith   /* load up the local column indices */
7728e2fed03SBarry Smith   nzmax = nz; /* )th processor needs space a largest processor needs */
7738e2fed03SBarry Smith   ierr = MPI_Reduce(&nz,&nzmax,1,MPI_INT,MPI_MAX,0,mat->comm);CHKERRQ(ierr);
7748e2fed03SBarry Smith   ierr = PetscMalloc((nzmax+1)*sizeof(int),&column_indices);CHKERRQ(ierr);
7758e2fed03SBarry Smith   cnt  = 0;
7768e2fed03SBarry Smith   for (i=0; i<mat->m; i++) {
7778e2fed03SBarry Smith     for (j=B->i[i]; j<B->i[i+1]; j++) {
7788e2fed03SBarry Smith       if ( (col = garray[B->j[j]]) > cstart) break;
7798e2fed03SBarry Smith       column_indices[cnt++] = col;
7808e2fed03SBarry Smith     }
7818e2fed03SBarry Smith     for (k=A->i[i]; k<A->i[i+1]; k++) {
7828e2fed03SBarry Smith       column_indices[cnt++] = A->j[k] + cstart;
7838e2fed03SBarry Smith     }
7848e2fed03SBarry Smith     for (; j<B->i[i+1]; j++) {
7858e2fed03SBarry Smith       column_indices[cnt++] = garray[B->j[j]];
7868e2fed03SBarry Smith     }
7878e2fed03SBarry Smith   }
7888e2fed03SBarry Smith   if (cnt != A->nz + B->nz) SETERRQ2(PETSC_ERR_LIB,"Internal PETSc error: cnt = %d nz = %d",cnt,A->nz+B->nz);
7898e2fed03SBarry Smith 
7908e2fed03SBarry Smith   /* store the column indices to the file */
7918e2fed03SBarry Smith   if (rank == 0) {
7928e2fed03SBarry Smith     MPI_Status status;
7938e2fed03SBarry Smith     ierr = PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,1);CHKERRQ(ierr);
7948e2fed03SBarry Smith     for (i=1; i<size; i++) {
795b021249fSBarry Smith       ierr = MPI_Recv(&rnz,1,MPI_INT,i,tag,mat->comm,&status);CHKERRQ(ierr);
796b021249fSBarry Smith       if (rnz > nzmax) SETERRQ2(PETSC_ERR_LIB,"Internal PETSc error: nz = %d nzmax = %d",nz,nzmax);
797b021249fSBarry Smith       ierr = MPI_Recv(column_indices,rnz,MPI_INT,i,tag,mat->comm,&status);CHKERRQ(ierr);
798b021249fSBarry Smith       ierr = PetscBinaryWrite(fd,column_indices,rnz,PETSC_INT,1);CHKERRQ(ierr);
7998e2fed03SBarry Smith     }
8008e2fed03SBarry Smith   } else {
8018e2fed03SBarry Smith     ierr = MPI_Send(&nz,1,MPI_INT,0,tag,mat->comm);CHKERRQ(ierr);
8028e2fed03SBarry Smith     ierr = MPI_Send(column_indices,nz,MPI_INT,0,tag,mat->comm);CHKERRQ(ierr);
8038e2fed03SBarry Smith   }
8048e2fed03SBarry Smith   ierr = PetscFree(column_indices);CHKERRQ(ierr);
8058e2fed03SBarry Smith 
8068e2fed03SBarry Smith   /* load up the local column values */
8078e2fed03SBarry Smith   ierr = PetscMalloc((nzmax+1)*sizeof(PetscScalar),&column_values);CHKERRQ(ierr);
8088e2fed03SBarry Smith   cnt  = 0;
8098e2fed03SBarry Smith   for (i=0; i<mat->m; i++) {
8108e2fed03SBarry Smith     for (j=B->i[i]; j<B->i[i+1]; j++) {
8118e2fed03SBarry Smith       if ( garray[B->j[j]] > cstart) break;
8128e2fed03SBarry Smith       column_values[cnt++] = B->a[j];
8138e2fed03SBarry Smith     }
8148e2fed03SBarry Smith     for (k=A->i[i]; k<A->i[i+1]; k++) {
8158e2fed03SBarry Smith       column_values[cnt++] = A->a[k];
8168e2fed03SBarry Smith     }
8178e2fed03SBarry Smith     for (; j<B->i[i+1]; j++) {
8188e2fed03SBarry Smith       column_values[cnt++] = B->a[j];
8198e2fed03SBarry Smith     }
8208e2fed03SBarry Smith   }
8218e2fed03SBarry Smith   if (cnt != A->nz + B->nz) SETERRQ2(1,"Internal PETSc error: cnt = %d nz = %d",cnt,A->nz+B->nz);
8228e2fed03SBarry Smith 
8238e2fed03SBarry Smith   /* store the column values to the file */
8248e2fed03SBarry Smith   if (rank == 0) {
8258e2fed03SBarry Smith     MPI_Status status;
8268e2fed03SBarry Smith     ierr = PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,1);CHKERRQ(ierr);
8278e2fed03SBarry Smith     for (i=1; i<size; i++) {
828b021249fSBarry Smith       ierr = MPI_Recv(&rnz,1,MPI_INT,i,tag,mat->comm,&status);CHKERRQ(ierr);
829b021249fSBarry Smith       if (rnz > nzmax) SETERRQ2(PETSC_ERR_LIB,"Internal PETSc error: nz = %d nzmax = %d",nz,nzmax);
830b021249fSBarry Smith       ierr = MPI_Recv(column_values,rnz,MPIU_SCALAR,i,tag,mat->comm,&status);CHKERRQ(ierr);
831b021249fSBarry Smith       ierr = PetscBinaryWrite(fd,column_values,rnz,PETSC_SCALAR,1);CHKERRQ(ierr);
8328e2fed03SBarry Smith     }
8338e2fed03SBarry Smith   } else {
8348e2fed03SBarry Smith     ierr = MPI_Send(&nz,1,MPI_INT,0,tag,mat->comm);CHKERRQ(ierr);
8358e2fed03SBarry Smith     ierr = MPI_Send(column_values,nz,MPIU_SCALAR,0,tag,mat->comm);CHKERRQ(ierr);
8368e2fed03SBarry Smith   }
8378e2fed03SBarry Smith   ierr = PetscFree(column_values);CHKERRQ(ierr);
8388e2fed03SBarry Smith   PetscFunctionReturn(0);
8398e2fed03SBarry Smith }
8408e2fed03SBarry Smith 
8418e2fed03SBarry Smith #undef __FUNCT__
8424a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ_ASCIIorDraworSocket"
843b0a32e0cSBarry Smith int MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
844416022c9SBarry Smith {
84544a69424SLois Curfman McInnes   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*)mat->data;
846dbb450caSBarry Smith   Mat_SeqAIJ*       C = (Mat_SeqAIJ*)aij->A->data;
847fb9695e5SSatish Balay   int               ierr,shift = C->indexshift,rank = aij->rank,size = aij->size;
8488e2fed03SBarry Smith   PetscTruth        isdraw,isascii,flg,isbinary;
849b0a32e0cSBarry Smith   PetscViewer       sviewer;
850f3ef73ceSBarry Smith   PetscViewerFormat format;
851416022c9SBarry Smith 
8523a40ed3dSBarry Smith   PetscFunctionBegin;
853fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
854b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
8558e2fed03SBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
8560f5bd95cSBarry Smith   if (isascii) {
857b0a32e0cSBarry Smith     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
858456192e2SBarry Smith     if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
8594e220ebcSLois Curfman McInnes       MatInfo info;
8601dab6e02SBarry Smith       ierr = MPI_Comm_rank(mat->comm,&rank);CHKERRQ(ierr);
861888f2ed8SSatish Balay       ierr = MatGetInfo(mat,MAT_LOCAL,&info);CHKERRQ(ierr);
862b0a32e0cSBarry Smith       ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_no_inode",&flg);CHKERRQ(ierr);
8636831982aSBarry Smith       if (flg) {
864b0a32e0cSBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %d nz %d nz alloced %d mem %d, not using I-node routines\n",
865273d9f13SBarry Smith 					      rank,mat->m,(int)info.nz_used,(int)info.nz_allocated,(int)info.memory);CHKERRQ(ierr);
8666831982aSBarry Smith       } else {
867b0a32e0cSBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %d nz %d nz alloced %d mem %d, using I-node routines\n",
868273d9f13SBarry Smith 		    rank,mat->m,(int)info.nz_used,(int)info.nz_allocated,(int)info.memory);CHKERRQ(ierr);
8696831982aSBarry Smith       }
870888f2ed8SSatish Balay       ierr = MatGetInfo(aij->A,MAT_LOCAL,&info);CHKERRQ(ierr);
871b0a32e0cSBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %d \n",rank,(int)info.nz_used);CHKERRQ(ierr);
872888f2ed8SSatish Balay       ierr = MatGetInfo(aij->B,MAT_LOCAL,&info);CHKERRQ(ierr);
873b0a32e0cSBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %d \n",rank,(int)info.nz_used);CHKERRQ(ierr);
874b0a32e0cSBarry Smith       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
875a40aa06bSLois Curfman McInnes       ierr = VecScatterView(aij->Mvctx,viewer);CHKERRQ(ierr);
8763a40ed3dSBarry Smith       PetscFunctionReturn(0);
877fb9695e5SSatish Balay     } else if (format == PETSC_VIEWER_ASCII_INFO) {
8783a40ed3dSBarry Smith       PetscFunctionReturn(0);
8794aedb280SBarry Smith     } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
8803cea93caSBarry Smith #if defined(PETSC_HAVE_SUPERLUDIST) && !defined(PETSC_USE_SINGLE)
8814aedb280SBarry Smith       ierr = MatMPIAIJFactorInfo_SuperLu(mat,viewer);CHKERRQ(ierr);
8824aedb280SBarry Smith #endif
8836ea74821SHong Zhang #if defined(PETSC_HAVE_SPOOLES) && !defined(PETSC_USE_SINGLE)
884a072f7f7SHong Zhang       ierr = MatFactorInfo_Spooles(mat,viewer);CHKERRQ(ierr);
8850473ca23SHong Zhang #endif
886471340fbSHong Zhang 
8874aedb280SBarry Smith       PetscFunctionReturn(0);
88808480c60SBarry Smith     }
8898e2fed03SBarry Smith   } else if (isbinary) {
8908e2fed03SBarry Smith     if (size == 1) {
8918e2fed03SBarry Smith       ierr = PetscObjectSetName((PetscObject)aij->A,mat->name);CHKERRQ(ierr);
8928e2fed03SBarry Smith       ierr = MatView(aij->A,viewer);CHKERRQ(ierr);
8938e2fed03SBarry Smith     } else {
8948e2fed03SBarry Smith       ierr = MatView_MPIAIJ_Binary(mat,viewer);CHKERRQ(ierr);
8958e2fed03SBarry Smith     }
8968e2fed03SBarry Smith     PetscFunctionReturn(0);
8970f5bd95cSBarry Smith   } else if (isdraw) {
898b0a32e0cSBarry Smith     PetscDraw  draw;
89919bcc07fSBarry Smith     PetscTruth isnull;
900b0a32e0cSBarry Smith     ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
901b0a32e0cSBarry Smith     ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
90219bcc07fSBarry Smith   }
90319bcc07fSBarry Smith 
90417699dbbSLois Curfman McInnes   if (size == 1) {
905e36acaf3SBarry Smith     ierr = PetscObjectSetName((PetscObject)aij->A,mat->name);CHKERRQ(ierr);
90678b31e54SBarry Smith     ierr = MatView(aij->A,viewer);CHKERRQ(ierr);
9073a40ed3dSBarry Smith   } else {
90895373324SBarry Smith     /* assemble the entire matrix onto first processor. */
90995373324SBarry Smith     Mat         A;
910ec8511deSBarry Smith     Mat_SeqAIJ *Aloc;
911273d9f13SBarry Smith     int         M = mat->M,N = mat->N,m,*ai,*aj,row,*cols,i,*ct;
91287828ca2SBarry Smith     PetscScalar *a;
9132ee70a88SLois Curfman McInnes 
91417699dbbSLois Curfman McInnes     if (!rank) {
91555843e3eSBarry Smith       ierr = MatCreateMPIAIJ(mat->comm,M,N,M,N,0,PETSC_NULL,0,PETSC_NULL,&A);CHKERRQ(ierr);
9163a40ed3dSBarry Smith     } else {
91755843e3eSBarry Smith       ierr = MatCreateMPIAIJ(mat->comm,0,0,M,N,0,PETSC_NULL,0,PETSC_NULL,&A);CHKERRQ(ierr);
91895373324SBarry Smith     }
919b0a32e0cSBarry Smith     PetscLogObjectParent(mat,A);
920416022c9SBarry Smith 
92195373324SBarry Smith     /* copy over the A part */
922ec8511deSBarry Smith     Aloc = (Mat_SeqAIJ*)aij->A->data;
923273d9f13SBarry Smith     m = aij->A->m; ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
92495373324SBarry Smith     row = aij->rstart;
925dbb450caSBarry Smith     for (i=0; i<ai[m]+shift; i++) {aj[i] += aij->cstart + shift;}
92695373324SBarry Smith     for (i=0; i<m; i++) {
927416022c9SBarry Smith       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);CHKERRQ(ierr);
92895373324SBarry Smith       row++; a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
92995373324SBarry Smith     }
9302ee70a88SLois Curfman McInnes     aj = Aloc->j;
931dbb450caSBarry Smith     for (i=0; i<ai[m]+shift; i++) {aj[i] -= aij->cstart + shift;}
93295373324SBarry Smith 
93395373324SBarry Smith     /* copy over the B part */
934ec8511deSBarry Smith     Aloc = (Mat_SeqAIJ*)aij->B->data;
935273d9f13SBarry Smith     m    = aij->B->m;  ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
93695373324SBarry Smith     row  = aij->rstart;
937b0a32e0cSBarry Smith     ierr = PetscMalloc((ai[m]+1)*sizeof(int),&cols);CHKERRQ(ierr);
938b0a32e0cSBarry Smith     ct   = cols;
939dbb450caSBarry Smith     for (i=0; i<ai[m]+shift; i++) {cols[i] = aij->garray[aj[i]+shift];}
94095373324SBarry Smith     for (i=0; i<m; i++) {
941416022c9SBarry Smith       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);CHKERRQ(ierr);
94295373324SBarry Smith       row++; a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
94395373324SBarry Smith     }
944606d414cSSatish Balay     ierr = PetscFree(ct);CHKERRQ(ierr);
9456d4a8577SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9466d4a8577SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
94755843e3eSBarry Smith     /*
94855843e3eSBarry Smith        Everyone has to call to draw the matrix since the graphics waits are
949b0a32e0cSBarry Smith        synchronized across all processors that share the PetscDraw object
95055843e3eSBarry Smith     */
951b0a32e0cSBarry Smith     ierr = PetscViewerGetSingleton(viewer,&sviewer);CHKERRQ(ierr);
952e03a110bSBarry Smith     if (!rank) {
953e36acaf3SBarry Smith       ierr = PetscObjectSetName((PetscObject)((Mat_MPIAIJ*)(A->data))->A,mat->name);CHKERRQ(ierr);
9546831982aSBarry Smith       ierr = MatView(((Mat_MPIAIJ*)(A->data))->A,sviewer);CHKERRQ(ierr);
95595373324SBarry Smith     }
956b0a32e0cSBarry Smith     ierr = PetscViewerRestoreSingleton(viewer,&sviewer);CHKERRQ(ierr);
95778b31e54SBarry Smith     ierr = MatDestroy(A);CHKERRQ(ierr);
95895373324SBarry Smith   }
9593a40ed3dSBarry Smith   PetscFunctionReturn(0);
9601eb62cbbSBarry Smith }
9611eb62cbbSBarry Smith 
9624a2ae208SSatish Balay #undef __FUNCT__
9634a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ"
964b0a32e0cSBarry Smith int MatView_MPIAIJ(Mat mat,PetscViewer viewer)
965416022c9SBarry Smith {
966416022c9SBarry Smith   int        ierr;
9676831982aSBarry Smith   PetscTruth isascii,isdraw,issocket,isbinary;
968416022c9SBarry Smith 
9693a40ed3dSBarry Smith   PetscFunctionBegin;
970b0a32e0cSBarry Smith   ierr  = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
971fb9695e5SSatish Balay   ierr  = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
972fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
973b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_SOCKET,&issocket);CHKERRQ(ierr);
9740f5bd95cSBarry Smith   if (isascii || isdraw || isbinary || issocket) {
9757b2a1423SBarry Smith     ierr = MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer);CHKERRQ(ierr);
9765cd90555SBarry Smith   } else {
97729bbc08cSBarry Smith     SETERRQ1(1,"Viewer type %s not supported by MPIAIJ matrices",((PetscObject)viewer)->type_name);
978416022c9SBarry Smith   }
9793a40ed3dSBarry Smith   PetscFunctionReturn(0);
980416022c9SBarry Smith }
981416022c9SBarry Smith 
9821eb62cbbSBarry Smith 
983c14dc6b6SHong Zhang 
9844a2ae208SSatish Balay #undef __FUNCT__
9854a2ae208SSatish Balay #define __FUNCT__ "MatRelax_MPIAIJ"
986c14dc6b6SHong Zhang int MatRelax_MPIAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,int its,int lits,Vec xx)
9878a729477SBarry Smith {
98844a69424SLois Curfman McInnes   Mat_MPIAIJ   *mat = (Mat_MPIAIJ*)matin->data;
989c14dc6b6SHong Zhang   int          ierr;
990c14dc6b6SHong Zhang   Vec          bb1;
991a6c309e7SHong Zhang   PetscScalar  mone=-1.0;
9928a729477SBarry Smith 
9933a40ed3dSBarry Smith   PetscFunctionBegin;
99491723122SBarry Smith   if (its <= 0 || lits <= 0) SETERRQ2(PETSC_ERR_ARG_WRONG,"Relaxation requires global its %d and local its %d both positive",its,lits);
995c14dc6b6SHong Zhang 
996c14dc6b6SHong Zhang   ierr = VecDuplicate(bb,&bb1);CHKERRQ(ierr);
9972798e883SHong Zhang 
998c16cb8f2SBarry Smith   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){
999da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
1000bd3bf7d3SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,lits,xx);CHKERRQ(ierr);
10012798e883SHong Zhang       its--;
1002da3a660dSBarry Smith     }
10032798e883SHong Zhang 
10042798e883SHong Zhang     while (its--) {
10053a40ed3dSBarry Smith       ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10063a40ed3dSBarry Smith       ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10072798e883SHong Zhang 
1008c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
1009c14dc6b6SHong Zhang       ierr = VecScale(&mone,mat->lvec);CHKERRQ(ierr);
1010c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
10112798e883SHong Zhang 
1012c14dc6b6SHong Zhang       /* local sweep */
1013bd3bf7d3SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,lits,xx);
1014c14dc6b6SHong Zhang       CHKERRQ(ierr);
10152798e883SHong Zhang     }
10163a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_FORWARD_SWEEP){
1017da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
1018c14dc6b6SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,PETSC_NULL,xx);CHKERRQ(ierr);
10192798e883SHong Zhang       its--;
1020da3a660dSBarry Smith     }
10212798e883SHong Zhang     while (its--) {
10223a40ed3dSBarry Smith       ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10233a40ed3dSBarry Smith       ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10242798e883SHong Zhang 
1025c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
1026c14dc6b6SHong Zhang       ierr = VecScale(&mone,mat->lvec);CHKERRQ(ierr);
1027c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
1028c14dc6b6SHong Zhang 
1029c14dc6b6SHong Zhang       /* local sweep */
1030a6c309e7SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,PETSC_NULL,xx);
1031c14dc6b6SHong Zhang       CHKERRQ(ierr);
10322798e883SHong Zhang     }
10333a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_BACKWARD_SWEEP){
1034da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
1035c14dc6b6SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,PETSC_NULL,xx);CHKERRQ(ierr);
10362798e883SHong Zhang       its--;
1037da3a660dSBarry Smith     }
10382798e883SHong Zhang     while (its--) {
10392e8a6d31SBarry Smith       ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10402e8a6d31SBarry Smith       ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10412798e883SHong Zhang 
1042c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
1043c14dc6b6SHong Zhang       ierr = VecScale(&mone,mat->lvec);CHKERRQ(ierr);
1044c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
10452798e883SHong Zhang 
1046c14dc6b6SHong Zhang       /* local sweep */
1047a6c309e7SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,PETSC_NULL,xx);
1048c14dc6b6SHong Zhang       CHKERRQ(ierr);
10492798e883SHong Zhang     }
10503a40ed3dSBarry Smith   } else {
105129bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"Parallel SOR not supported");
1052c16cb8f2SBarry Smith   }
1053c14dc6b6SHong Zhang 
1054c14dc6b6SHong Zhang   ierr = VecDestroy(bb1);CHKERRQ(ierr);
10553a40ed3dSBarry Smith   PetscFunctionReturn(0);
10568a729477SBarry Smith }
1057a66be287SLois Curfman McInnes 
10584a2ae208SSatish Balay #undef __FUNCT__
10594a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_MPIAIJ"
10608f6be9afSLois Curfman McInnes int MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info)
1061a66be287SLois Curfman McInnes {
1062a66be287SLois Curfman McInnes   Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data;
1063a66be287SLois Curfman McInnes   Mat        A = mat->A,B = mat->B;
10644e220ebcSLois Curfman McInnes   int        ierr;
1065329f5518SBarry Smith   PetscReal  isend[5],irecv[5];
1066a66be287SLois Curfman McInnes 
10673a40ed3dSBarry Smith   PetscFunctionBegin;
10684e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
10694e220ebcSLois Curfman McInnes   ierr = MatGetInfo(A,MAT_LOCAL,info);CHKERRQ(ierr);
10704e220ebcSLois Curfman McInnes   isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
10714e220ebcSLois Curfman McInnes   isend[3] = info->memory;  isend[4] = info->mallocs;
10724e220ebcSLois Curfman McInnes   ierr = MatGetInfo(B,MAT_LOCAL,info);CHKERRQ(ierr);
10734e220ebcSLois Curfman McInnes   isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
10744e220ebcSLois Curfman McInnes   isend[3] += info->memory;  isend[4] += info->mallocs;
1075a66be287SLois Curfman McInnes   if (flag == MAT_LOCAL) {
10764e220ebcSLois Curfman McInnes     info->nz_used      = isend[0];
10774e220ebcSLois Curfman McInnes     info->nz_allocated = isend[1];
10784e220ebcSLois Curfman McInnes     info->nz_unneeded  = isend[2];
10794e220ebcSLois Curfman McInnes     info->memory       = isend[3];
10804e220ebcSLois Curfman McInnes     info->mallocs      = isend[4];
1081a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_MAX) {
1082d7d1e502SBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_MAX,matin->comm);CHKERRQ(ierr);
10834e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
10844e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
10854e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
10864e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
10874e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1088a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_SUM) {
1089d7d1e502SBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_SUM,matin->comm);CHKERRQ(ierr);
10904e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
10914e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
10924e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
10934e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
10944e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1095a66be287SLois Curfman McInnes   }
10964e220ebcSLois Curfman McInnes   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
10974e220ebcSLois Curfman McInnes   info->fill_ratio_needed = 0;
10984e220ebcSLois Curfman McInnes   info->factor_mallocs    = 0;
1099273d9f13SBarry Smith   info->rows_global       = (double)matin->M;
1100273d9f13SBarry Smith   info->columns_global    = (double)matin->N;
1101273d9f13SBarry Smith   info->rows_local        = (double)matin->m;
1102273d9f13SBarry Smith   info->columns_local     = (double)matin->N;
11034e220ebcSLois Curfman McInnes 
11043a40ed3dSBarry Smith   PetscFunctionReturn(0);
1105a66be287SLois Curfman McInnes }
1106a66be287SLois Curfman McInnes 
11074a2ae208SSatish Balay #undef __FUNCT__
11084a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_MPIAIJ"
11098f6be9afSLois Curfman McInnes int MatSetOption_MPIAIJ(Mat A,MatOption op)
1110c74985f6SBarry Smith {
1111c0bbcb79SLois Curfman McInnes   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
11121dee9f54SBarry Smith   int        ierr;
1113c74985f6SBarry Smith 
11143a40ed3dSBarry Smith   PetscFunctionBegin;
111512c028f9SKris Buschelman   switch (op) {
111612c028f9SKris Buschelman   case MAT_NO_NEW_NONZERO_LOCATIONS:
111712c028f9SKris Buschelman   case MAT_YES_NEW_NONZERO_LOCATIONS:
111812c028f9SKris Buschelman   case MAT_COLUMNS_UNSORTED:
111912c028f9SKris Buschelman   case MAT_COLUMNS_SORTED:
112012c028f9SKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
112112c028f9SKris Buschelman   case MAT_KEEP_ZEROED_ROWS:
112212c028f9SKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
112312c028f9SKris Buschelman   case MAT_USE_INODES:
112412c028f9SKris Buschelman   case MAT_DO_NOT_USE_INODES:
112512c028f9SKris Buschelman   case MAT_IGNORE_ZERO_ENTRIES:
11261dee9f54SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
11271dee9f54SBarry Smith     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
112812c028f9SKris Buschelman     break;
112912c028f9SKris Buschelman   case MAT_ROW_ORIENTED:
11307c922b88SBarry Smith     a->roworiented = PETSC_TRUE;
11311dee9f54SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
11321dee9f54SBarry Smith     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
113312c028f9SKris Buschelman     break;
113412c028f9SKris Buschelman   case MAT_ROWS_SORTED:
113512c028f9SKris Buschelman   case MAT_ROWS_UNSORTED:
113612c028f9SKris Buschelman   case MAT_YES_NEW_DIAGONALS:
1137b0a32e0cSBarry Smith     PetscLogInfo(A,"MatSetOption_MPIAIJ:Option ignored\n");
113812c028f9SKris Buschelman     break;
113912c028f9SKris Buschelman   case MAT_COLUMN_ORIENTED:
11407c922b88SBarry Smith     a->roworiented = PETSC_FALSE;
11411dee9f54SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
11421dee9f54SBarry Smith     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
114312c028f9SKris Buschelman     break;
114412c028f9SKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
11457c922b88SBarry Smith     a->donotstash = PETSC_TRUE;
114612c028f9SKris Buschelman     break;
114712c028f9SKris Buschelman   case MAT_NO_NEW_DIAGONALS:
114829bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"MAT_NO_NEW_DIAGONALS");
114912c028f9SKris Buschelman   default:
115029bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"unknown option");
11513a40ed3dSBarry Smith   }
11523a40ed3dSBarry Smith   PetscFunctionReturn(0);
1153c74985f6SBarry Smith }
1154c74985f6SBarry Smith 
11554a2ae208SSatish Balay #undef __FUNCT__
11564a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_MPIAIJ"
115787828ca2SBarry Smith int MatGetRow_MPIAIJ(Mat matin,int row,int *nz,int **idx,PetscScalar **v)
115839e00950SLois Curfman McInnes {
1159154123eaSLois Curfman McInnes   Mat_MPIAIJ   *mat = (Mat_MPIAIJ*)matin->data;
116087828ca2SBarry Smith   PetscScalar  *vworkA,*vworkB,**pvA,**pvB,*v_p;
1161154123eaSLois Curfman McInnes   int          i,ierr,*cworkA,*cworkB,**pcA,**pcB,cstart = mat->cstart;
1162154123eaSLois Curfman McInnes   int          nztot,nzA,nzB,lrow,rstart = mat->rstart,rend = mat->rend;
116370f0671dSBarry Smith   int          *cmap,*idx_p;
116439e00950SLois Curfman McInnes 
11653a40ed3dSBarry Smith   PetscFunctionBegin;
116629bbc08cSBarry Smith   if (mat->getrowactive == PETSC_TRUE) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Already active");
11677a0afa10SBarry Smith   mat->getrowactive = PETSC_TRUE;
11687a0afa10SBarry Smith 
116970f0671dSBarry Smith   if (!mat->rowvalues && (idx || v)) {
11707a0afa10SBarry Smith     /*
11717a0afa10SBarry Smith         allocate enough space to hold information from the longest row.
11727a0afa10SBarry Smith     */
11737a0afa10SBarry Smith     Mat_SeqAIJ *Aa = (Mat_SeqAIJ*)mat->A->data,*Ba = (Mat_SeqAIJ*)mat->B->data;
1174273d9f13SBarry Smith     int     max = 1,tmp;
1175273d9f13SBarry Smith     for (i=0; i<matin->m; i++) {
11767a0afa10SBarry Smith       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
11777a0afa10SBarry Smith       if (max < tmp) { max = tmp; }
11787a0afa10SBarry Smith     }
117987828ca2SBarry Smith     ierr = PetscMalloc(max*(sizeof(int)+sizeof(PetscScalar)),&mat->rowvalues);CHKERRQ(ierr);
11807a0afa10SBarry Smith     mat->rowindices = (int*)(mat->rowvalues + max);
11817a0afa10SBarry Smith   }
11827a0afa10SBarry Smith 
118329bbc08cSBarry Smith   if (row < rstart || row >= rend) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Only local rows")
1184abc0e9e4SLois Curfman McInnes   lrow = row - rstart;
118539e00950SLois Curfman McInnes 
1186154123eaSLois Curfman McInnes   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1187154123eaSLois Curfman McInnes   if (!v)   {pvA = 0; pvB = 0;}
1188154123eaSLois Curfman McInnes   if (!idx) {pcA = 0; if (!v) pcB = 0;}
1189f830108cSBarry Smith   ierr = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1190f830108cSBarry Smith   ierr = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
1191154123eaSLois Curfman McInnes   nztot = nzA + nzB;
1192154123eaSLois Curfman McInnes 
119370f0671dSBarry Smith   cmap  = mat->garray;
1194154123eaSLois Curfman McInnes   if (v  || idx) {
1195154123eaSLois Curfman McInnes     if (nztot) {
1196154123eaSLois Curfman McInnes       /* Sort by increasing column numbers, assuming A and B already sorted */
119770f0671dSBarry Smith       int imark = -1;
1198154123eaSLois Curfman McInnes       if (v) {
119970f0671dSBarry Smith         *v = v_p = mat->rowvalues;
120039e00950SLois Curfman McInnes         for (i=0; i<nzB; i++) {
120170f0671dSBarry Smith           if (cmap[cworkB[i]] < cstart)   v_p[i] = vworkB[i];
1202154123eaSLois Curfman McInnes           else break;
1203154123eaSLois Curfman McInnes         }
1204154123eaSLois Curfman McInnes         imark = i;
120570f0671dSBarry Smith         for (i=0; i<nzA; i++)     v_p[imark+i] = vworkA[i];
120670f0671dSBarry Smith         for (i=imark; i<nzB; i++) v_p[nzA+i]   = vworkB[i];
1207154123eaSLois Curfman McInnes       }
1208154123eaSLois Curfman McInnes       if (idx) {
120970f0671dSBarry Smith         *idx = idx_p = mat->rowindices;
121070f0671dSBarry Smith         if (imark > -1) {
121170f0671dSBarry Smith           for (i=0; i<imark; i++) {
121270f0671dSBarry Smith             idx_p[i] = cmap[cworkB[i]];
121370f0671dSBarry Smith           }
121470f0671dSBarry Smith         } else {
1215154123eaSLois Curfman McInnes           for (i=0; i<nzB; i++) {
121670f0671dSBarry Smith             if (cmap[cworkB[i]] < cstart)   idx_p[i] = cmap[cworkB[i]];
1217154123eaSLois Curfman McInnes             else break;
1218154123eaSLois Curfman McInnes           }
1219154123eaSLois Curfman McInnes           imark = i;
122070f0671dSBarry Smith         }
122170f0671dSBarry Smith         for (i=0; i<nzA; i++)     idx_p[imark+i] = cstart + cworkA[i];
122270f0671dSBarry Smith         for (i=imark; i<nzB; i++) idx_p[nzA+i]   = cmap[cworkB[i]];
122339e00950SLois Curfman McInnes       }
12243f97c4b0SBarry Smith     } else {
12251ca473b0SSatish Balay       if (idx) *idx = 0;
12261ca473b0SSatish Balay       if (v)   *v   = 0;
12271ca473b0SSatish Balay     }
1228154123eaSLois Curfman McInnes   }
122939e00950SLois Curfman McInnes   *nz = nztot;
1230f830108cSBarry Smith   ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1231f830108cSBarry Smith   ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
12323a40ed3dSBarry Smith   PetscFunctionReturn(0);
123339e00950SLois Curfman McInnes }
123439e00950SLois Curfman McInnes 
12354a2ae208SSatish Balay #undef __FUNCT__
12364a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_MPIAIJ"
123787828ca2SBarry Smith int MatRestoreRow_MPIAIJ(Mat mat,int row,int *nz,int **idx,PetscScalar **v)
123839e00950SLois Curfman McInnes {
12397a0afa10SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
12403a40ed3dSBarry Smith 
12413a40ed3dSBarry Smith   PetscFunctionBegin;
12427a0afa10SBarry Smith   if (aij->getrowactive == PETSC_FALSE) {
124329bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"MatGetRow not called");
12447a0afa10SBarry Smith   }
12457a0afa10SBarry Smith   aij->getrowactive = PETSC_FALSE;
12463a40ed3dSBarry Smith   PetscFunctionReturn(0);
124739e00950SLois Curfman McInnes }
124839e00950SLois Curfman McInnes 
12494a2ae208SSatish Balay #undef __FUNCT__
12504a2ae208SSatish Balay #define __FUNCT__ "MatNorm_MPIAIJ"
1251329f5518SBarry Smith int MatNorm_MPIAIJ(Mat mat,NormType type,PetscReal *norm)
1252855ac2c5SLois Curfman McInnes {
1253855ac2c5SLois Curfman McInnes   Mat_MPIAIJ   *aij = (Mat_MPIAIJ*)mat->data;
1254ec8511deSBarry Smith   Mat_SeqAIJ   *amat = (Mat_SeqAIJ*)aij->A->data,*bmat = (Mat_SeqAIJ*)aij->B->data;
1255416022c9SBarry Smith   int          ierr,i,j,cstart = aij->cstart,shift = amat->indexshift;
1256329f5518SBarry Smith   PetscReal    sum = 0.0;
125787828ca2SBarry Smith   PetscScalar  *v;
125804ca555eSLois Curfman McInnes 
12593a40ed3dSBarry Smith   PetscFunctionBegin;
126017699dbbSLois Curfman McInnes   if (aij->size == 1) {
126114183eadSLois Curfman McInnes     ierr =  MatNorm(aij->A,type,norm);CHKERRQ(ierr);
126237fa93a5SLois Curfman McInnes   } else {
126304ca555eSLois Curfman McInnes     if (type == NORM_FROBENIUS) {
126404ca555eSLois Curfman McInnes       v = amat->a;
126504ca555eSLois Curfman McInnes       for (i=0; i<amat->nz; i++) {
1266aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
1267329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
126804ca555eSLois Curfman McInnes #else
126904ca555eSLois Curfman McInnes         sum += (*v)*(*v); v++;
127004ca555eSLois Curfman McInnes #endif
127104ca555eSLois Curfman McInnes       }
127204ca555eSLois Curfman McInnes       v = bmat->a;
127304ca555eSLois Curfman McInnes       for (i=0; i<bmat->nz; i++) {
1274aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
1275329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
127604ca555eSLois Curfman McInnes #else
127704ca555eSLois Curfman McInnes         sum += (*v)*(*v); v++;
127804ca555eSLois Curfman McInnes #endif
127904ca555eSLois Curfman McInnes       }
1280d7d1e502SBarry Smith       ierr = MPI_Allreduce(&sum,norm,1,MPIU_REAL,MPI_SUM,mat->comm);CHKERRQ(ierr);
128104ca555eSLois Curfman McInnes       *norm = sqrt(*norm);
12823a40ed3dSBarry Smith     } else if (type == NORM_1) { /* max column norm */
1283329f5518SBarry Smith       PetscReal *tmp,*tmp2;
128404ca555eSLois Curfman McInnes       int    *jj,*garray = aij->garray;
1285b0a32e0cSBarry Smith       ierr = PetscMalloc((mat->N+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1286b0a32e0cSBarry Smith       ierr = PetscMalloc((mat->N+1)*sizeof(PetscReal),&tmp2);CHKERRQ(ierr);
1287273d9f13SBarry Smith       ierr = PetscMemzero(tmp,mat->N*sizeof(PetscReal));CHKERRQ(ierr);
128804ca555eSLois Curfman McInnes       *norm = 0.0;
128904ca555eSLois Curfman McInnes       v = amat->a; jj = amat->j;
129004ca555eSLois Curfman McInnes       for (j=0; j<amat->nz; j++) {
1291579c6b6fSBarry Smith         tmp[cstart + *jj++ + shift] += PetscAbsScalar(*v);  v++;
129204ca555eSLois Curfman McInnes       }
129304ca555eSLois Curfman McInnes       v = bmat->a; jj = bmat->j;
129404ca555eSLois Curfman McInnes       for (j=0; j<bmat->nz; j++) {
1295579c6b6fSBarry Smith         tmp[garray[*jj++ + shift]] += PetscAbsScalar(*v); v++;
129604ca555eSLois Curfman McInnes       }
1297d7d1e502SBarry Smith       ierr = MPI_Allreduce(tmp,tmp2,mat->N,MPIU_REAL,MPI_SUM,mat->comm);CHKERRQ(ierr);
1298273d9f13SBarry Smith       for (j=0; j<mat->N; j++) {
129904ca555eSLois Curfman McInnes         if (tmp2[j] > *norm) *norm = tmp2[j];
130004ca555eSLois Curfman McInnes       }
1301606d414cSSatish Balay       ierr = PetscFree(tmp);CHKERRQ(ierr);
1302606d414cSSatish Balay       ierr = PetscFree(tmp2);CHKERRQ(ierr);
13033a40ed3dSBarry Smith     } else if (type == NORM_INFINITY) { /* max row norm */
1304329f5518SBarry Smith       PetscReal ntemp = 0.0;
1305273d9f13SBarry Smith       for (j=0; j<aij->A->m; j++) {
1306dbb450caSBarry Smith         v = amat->a + amat->i[j] + shift;
130704ca555eSLois Curfman McInnes         sum = 0.0;
130804ca555eSLois Curfman McInnes         for (i=0; i<amat->i[j+1]-amat->i[j]; i++) {
1309cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
131004ca555eSLois Curfman McInnes         }
1311dbb450caSBarry Smith         v = bmat->a + bmat->i[j] + shift;
131204ca555eSLois Curfman McInnes         for (i=0; i<bmat->i[j+1]-bmat->i[j]; i++) {
1313cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
131404ca555eSLois Curfman McInnes         }
1315515d9167SLois Curfman McInnes         if (sum > ntemp) ntemp = sum;
131604ca555eSLois Curfman McInnes       }
1317d7d1e502SBarry Smith       ierr = MPI_Allreduce(&ntemp,norm,1,MPIU_REAL,MPI_MAX,mat->comm);CHKERRQ(ierr);
1318ca161407SBarry Smith     } else {
131929bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"No support for two norm");
132004ca555eSLois Curfman McInnes     }
132137fa93a5SLois Curfman McInnes   }
13223a40ed3dSBarry Smith   PetscFunctionReturn(0);
1323855ac2c5SLois Curfman McInnes }
1324855ac2c5SLois Curfman McInnes 
13254a2ae208SSatish Balay #undef __FUNCT__
13264a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_MPIAIJ"
13278f6be9afSLois Curfman McInnes int MatTranspose_MPIAIJ(Mat A,Mat *matout)
1328b7c46309SBarry Smith {
1329b7c46309SBarry Smith   Mat_MPIAIJ   *a = (Mat_MPIAIJ*)A->data;
1330dbb450caSBarry Smith   Mat_SeqAIJ   *Aloc = (Mat_SeqAIJ*)a->A->data;
1331416022c9SBarry Smith   int          ierr,shift = Aloc->indexshift;
1332273d9f13SBarry Smith   int          M = A->M,N = A->N,m,*ai,*aj,row,*cols,i,*ct;
13333a40ed3dSBarry Smith   Mat          B;
133487828ca2SBarry Smith   PetscScalar  *array;
1335b7c46309SBarry Smith 
13363a40ed3dSBarry Smith   PetscFunctionBegin;
13377c922b88SBarry Smith   if (!matout && M != N) {
133829bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1339d4bb536fSBarry Smith   }
1340d4bb536fSBarry Smith 
1341273d9f13SBarry Smith   ierr = MatCreateMPIAIJ(A->comm,A->n,A->m,N,M,0,PETSC_NULL,0,PETSC_NULL,&B);CHKERRQ(ierr);
1342b7c46309SBarry Smith 
1343b7c46309SBarry Smith   /* copy over the A part */
1344ec8511deSBarry Smith   Aloc = (Mat_SeqAIJ*)a->A->data;
1345273d9f13SBarry Smith   m = a->A->m; ai = Aloc->i; aj = Aloc->j; array = Aloc->a;
1346b7c46309SBarry Smith   row = a->rstart;
1347dbb450caSBarry Smith   for (i=0; i<ai[m]+shift; i++) {aj[i] += a->cstart + shift;}
1348b7c46309SBarry Smith   for (i=0; i<m; i++) {
1349416022c9SBarry Smith     ierr = MatSetValues(B,ai[i+1]-ai[i],aj,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
1350b7c46309SBarry Smith     row++; array += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
1351b7c46309SBarry Smith   }
1352b7c46309SBarry Smith   aj = Aloc->j;
13534af08d9eSBarry Smith   for (i=0; i<ai[m]+shift; i++) {aj[i] -= a->cstart + shift;}
1354b7c46309SBarry Smith 
1355b7c46309SBarry Smith   /* copy over the B part */
1356ec8511deSBarry Smith   Aloc = (Mat_SeqAIJ*)a->B->data;
1357273d9f13SBarry Smith   m = a->B->m;  ai = Aloc->i; aj = Aloc->j; array = Aloc->a;
1358b7c46309SBarry Smith   row  = a->rstart;
1359b0a32e0cSBarry Smith   ierr = PetscMalloc((1+ai[m]-shift)*sizeof(int),&cols);CHKERRQ(ierr);
1360b0a32e0cSBarry Smith   ct   = cols;
1361dbb450caSBarry Smith   for (i=0; i<ai[m]+shift; i++) {cols[i] = a->garray[aj[i]+shift];}
1362b7c46309SBarry Smith   for (i=0; i<m; i++) {
1363416022c9SBarry Smith     ierr = MatSetValues(B,ai[i+1]-ai[i],cols,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
1364b7c46309SBarry Smith     row++; array += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
1365b7c46309SBarry Smith   }
1366606d414cSSatish Balay   ierr = PetscFree(ct);CHKERRQ(ierr);
13676d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
13686d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
13697c922b88SBarry Smith   if (matout) {
13700de55854SLois Curfman McInnes     *matout = B;
13710de55854SLois Curfman McInnes   } else {
1372273d9f13SBarry Smith     ierr = MatHeaderCopy(A,B);CHKERRQ(ierr);
13730de55854SLois Curfman McInnes   }
13743a40ed3dSBarry Smith   PetscFunctionReturn(0);
1375b7c46309SBarry Smith }
1376b7c46309SBarry Smith 
13774a2ae208SSatish Balay #undef __FUNCT__
13784a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_MPIAIJ"
13794b967eb1SSatish Balay int MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr)
1380a008b906SSatish Balay {
13814b967eb1SSatish Balay   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
13824b967eb1SSatish Balay   Mat        a = aij->A,b = aij->B;
1383a008b906SSatish Balay   int        ierr,s1,s2,s3;
1384a008b906SSatish Balay 
13853a40ed3dSBarry Smith   PetscFunctionBegin;
13864b967eb1SSatish Balay   ierr = MatGetLocalSize(mat,&s2,&s3);CHKERRQ(ierr);
13874b967eb1SSatish Balay   if (rr) {
1388e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr);
138929bbc08cSBarry Smith     if (s1!=s3) SETERRQ(PETSC_ERR_ARG_SIZ,"right vector non-conforming local size");
13904b967eb1SSatish Balay     /* Overlap communication with computation. */
139143a90d84SBarry Smith     ierr = VecScatterBegin(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx);CHKERRQ(ierr);
1392a008b906SSatish Balay   }
13934b967eb1SSatish Balay   if (ll) {
1394e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr);
139529bbc08cSBarry Smith     if (s1!=s2) SETERRQ(PETSC_ERR_ARG_SIZ,"left vector non-conforming local size");
1396f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,ll,0);CHKERRQ(ierr);
13974b967eb1SSatish Balay   }
13984b967eb1SSatish Balay   /* scale  the diagonal block */
1399f830108cSBarry Smith   ierr = (*a->ops->diagonalscale)(a,ll,rr);CHKERRQ(ierr);
14004b967eb1SSatish Balay 
14014b967eb1SSatish Balay   if (rr) {
14024b967eb1SSatish Balay     /* Do a scatter end and then right scale the off-diagonal block */
140343a90d84SBarry Smith     ierr = VecScatterEnd(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx);CHKERRQ(ierr);
1404f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,0,aij->lvec);CHKERRQ(ierr);
14054b967eb1SSatish Balay   }
14064b967eb1SSatish Balay 
14073a40ed3dSBarry Smith   PetscFunctionReturn(0);
1408a008b906SSatish Balay }
1409a008b906SSatish Balay 
1410a008b906SSatish Balay 
14114a2ae208SSatish Balay #undef __FUNCT__
14124a2ae208SSatish Balay #define __FUNCT__ "MatPrintHelp_MPIAIJ"
14138f6be9afSLois Curfman McInnes int MatPrintHelp_MPIAIJ(Mat A)
1414682d7d0cSBarry Smith {
1415682d7d0cSBarry Smith   Mat_MPIAIJ *a   = (Mat_MPIAIJ*)A->data;
14163a40ed3dSBarry Smith   int        ierr;
1417682d7d0cSBarry Smith 
14183a40ed3dSBarry Smith   PetscFunctionBegin;
14193a40ed3dSBarry Smith   if (!a->rank) {
14203a40ed3dSBarry Smith     ierr = MatPrintHelp_SeqAIJ(a->A);CHKERRQ(ierr);
14213a40ed3dSBarry Smith   }
14223a40ed3dSBarry Smith   PetscFunctionReturn(0);
1423682d7d0cSBarry Smith }
1424682d7d0cSBarry Smith 
14254a2ae208SSatish Balay #undef __FUNCT__
14264a2ae208SSatish Balay #define __FUNCT__ "MatGetBlockSize_MPIAIJ"
14278f6be9afSLois Curfman McInnes int MatGetBlockSize_MPIAIJ(Mat A,int *bs)
14285a838052SSatish Balay {
14293a40ed3dSBarry Smith   PetscFunctionBegin;
14305a838052SSatish Balay   *bs = 1;
14313a40ed3dSBarry Smith   PetscFunctionReturn(0);
14325a838052SSatish Balay }
14334a2ae208SSatish Balay #undef __FUNCT__
14344a2ae208SSatish Balay #define __FUNCT__ "MatSetUnfactored_MPIAIJ"
14358f6be9afSLois Curfman McInnes int MatSetUnfactored_MPIAIJ(Mat A)
1436bb5a7306SBarry Smith {
1437bb5a7306SBarry Smith   Mat_MPIAIJ *a   = (Mat_MPIAIJ*)A->data;
1438bb5a7306SBarry Smith   int        ierr;
14393a40ed3dSBarry Smith 
14403a40ed3dSBarry Smith   PetscFunctionBegin;
1441bb5a7306SBarry Smith   ierr = MatSetUnfactored(a->A);CHKERRQ(ierr);
14423a40ed3dSBarry Smith   PetscFunctionReturn(0);
1443bb5a7306SBarry Smith }
1444bb5a7306SBarry Smith 
14454a2ae208SSatish Balay #undef __FUNCT__
14464a2ae208SSatish Balay #define __FUNCT__ "MatEqual_MPIAIJ"
1447d4bb536fSBarry Smith int MatEqual_MPIAIJ(Mat A,Mat B,PetscTruth *flag)
1448d4bb536fSBarry Smith {
1449d4bb536fSBarry Smith   Mat_MPIAIJ *matB = (Mat_MPIAIJ*)B->data,*matA = (Mat_MPIAIJ*)A->data;
1450d4bb536fSBarry Smith   Mat        a,b,c,d;
1451d4bb536fSBarry Smith   PetscTruth flg;
1452d4bb536fSBarry Smith   int        ierr;
1453d4bb536fSBarry Smith 
14543a40ed3dSBarry Smith   PetscFunctionBegin;
1455273d9f13SBarry Smith   ierr = PetscTypeCompare((PetscObject)B,MATMPIAIJ,&flg);CHKERRQ(ierr);
1456273d9f13SBarry Smith   if (!flg) SETERRQ(PETSC_ERR_ARG_INCOMP,"Matrices must be same type");
1457d4bb536fSBarry Smith   a = matA->A; b = matA->B;
1458d4bb536fSBarry Smith   c = matB->A; d = matB->B;
1459d4bb536fSBarry Smith 
1460d4bb536fSBarry Smith   ierr = MatEqual(a,c,&flg);CHKERRQ(ierr);
1461d4bb536fSBarry Smith   if (flg == PETSC_TRUE) {
1462d4bb536fSBarry Smith     ierr = MatEqual(b,d,&flg);CHKERRQ(ierr);
1463d4bb536fSBarry Smith   }
1464ca161407SBarry Smith   ierr = MPI_Allreduce(&flg,flag,1,MPI_INT,MPI_LAND,A->comm);CHKERRQ(ierr);
14653a40ed3dSBarry Smith   PetscFunctionReturn(0);
1466d4bb536fSBarry Smith }
1467d4bb536fSBarry Smith 
14684a2ae208SSatish Balay #undef __FUNCT__
14694a2ae208SSatish Balay #define __FUNCT__ "MatCopy_MPIAIJ"
1470cb5b572fSBarry Smith int MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str)
1471cb5b572fSBarry Smith {
1472cb5b572fSBarry Smith   int        ierr;
1473cb5b572fSBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;
1474cb5b572fSBarry Smith   Mat_MPIAIJ *b = (Mat_MPIAIJ *)B->data;
1475273d9f13SBarry Smith   PetscTruth flg;
1476cb5b572fSBarry Smith 
1477cb5b572fSBarry Smith   PetscFunctionBegin;
1478273d9f13SBarry Smith   ierr = PetscTypeCompare((PetscObject)B,MATMPIAIJ,&flg);CHKERRQ(ierr);
1479273d9f13SBarry Smith   if (str != SAME_NONZERO_PATTERN || !flg) {
1480cb5b572fSBarry Smith     /* because of the column compression in the off-processor part of the matrix a->B,
1481cb5b572fSBarry Smith        the number of columns in a->B and b->B may be different, hence we cannot call
1482cb5b572fSBarry Smith        the MatCopy() directly on the two parts. If need be, we can provide a more
1483cb5b572fSBarry Smith        efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices
1484cb5b572fSBarry Smith        then copying the submatrices */
1485cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1486cb5b572fSBarry Smith   } else {
1487cb5b572fSBarry Smith     ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr);
1488cb5b572fSBarry Smith     ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr);
1489cb5b572fSBarry Smith   }
1490cb5b572fSBarry Smith   PetscFunctionReturn(0);
1491cb5b572fSBarry Smith }
1492cb5b572fSBarry Smith 
14934a2ae208SSatish Balay #undef __FUNCT__
14944a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_MPIAIJ"
1495273d9f13SBarry Smith int MatSetUpPreallocation_MPIAIJ(Mat A)
1496273d9f13SBarry Smith {
1497273d9f13SBarry Smith   int        ierr;
1498273d9f13SBarry Smith 
1499273d9f13SBarry Smith   PetscFunctionBegin;
1500273d9f13SBarry Smith   ierr =  MatMPIAIJSetPreallocation(A,PETSC_DEFAULT,0,PETSC_DEFAULT,0);CHKERRQ(ierr);
1501273d9f13SBarry Smith   PetscFunctionReturn(0);
1502273d9f13SBarry Smith }
1503273d9f13SBarry Smith 
1504ca44d042SBarry Smith EXTERN int MatDuplicate_MPIAIJ(Mat,MatDuplicateOption,Mat *);
1505ca44d042SBarry Smith EXTERN int MatIncreaseOverlap_MPIAIJ(Mat,int,IS *,int);
1506ca44d042SBarry Smith EXTERN int MatFDColoringCreate_MPIAIJ(Mat,ISColoring,MatFDColoring);
1507ca44d042SBarry Smith EXTERN int MatGetSubMatrices_MPIAIJ (Mat,int,IS *,IS *,MatReuse,Mat **);
1508ca44d042SBarry Smith EXTERN int MatGetSubMatrix_MPIAIJ (Mat,IS,IS,int,MatReuse,Mat *);
150987828ca2SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
1510b380c88cSHong Zhang EXTERN int MatLUFactorSymbolic_MPIAIJ_TFS(Mat,IS,IS,MatFactorInfo*,Mat*);
1511b9617806SBarry Smith #endif
151200e6dbe6SBarry Smith 
1513ac90fabeSBarry Smith #include "petscblaslapack.h"
1514*c537a176SHong Zhang extern int MatAXPY_SeqAIJ(PetscScalar *,Mat,Mat,MatStructure);
1515ac90fabeSBarry Smith #undef __FUNCT__
1516ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_MPIAIJ"
1517ac90fabeSBarry Smith int MatAXPY_MPIAIJ(PetscScalar *a,Mat X,Mat Y,MatStructure str)
1518ac90fabeSBarry Smith {
1519*c537a176SHong Zhang   int        ierr,one=1;
1520ac90fabeSBarry Smith   Mat_MPIAIJ *xx = (Mat_MPIAIJ *)X->data,*yy = (Mat_MPIAIJ *)Y->data;
1521ac90fabeSBarry Smith   Mat_SeqAIJ *x,*y;
1522ac90fabeSBarry Smith 
1523ac90fabeSBarry Smith   PetscFunctionBegin;
1524ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
1525ac90fabeSBarry Smith     x  = (Mat_SeqAIJ *)xx->A->data;
1526ac90fabeSBarry Smith     y  = (Mat_SeqAIJ *)yy->A->data;
1527ac90fabeSBarry Smith     BLaxpy_(&x->nz,a,x->a,&one,y->a,&one);
1528ac90fabeSBarry Smith     x  = (Mat_SeqAIJ *)xx->B->data;
1529ac90fabeSBarry Smith     y  = (Mat_SeqAIJ *)yy->B->data;
1530ac90fabeSBarry Smith     BLaxpy_(&x->nz,a,x->a,&one,y->a,&one);
1531*c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) {  /* B indeces have problem: map to original index! */
1532*c537a176SHong Zhang     ierr = MatAXPY_SeqAIJ(a,xx->A,yy->A,str);CHKERRQ(ierr);
1533*c537a176SHong Zhang     /* ierr = MatAXPY_SeqAIJ(a,xx->B,yy->B,str);CHKERRQ(ierr); */
1534*c537a176SHong Zhang 
1535*c537a176SHong Zhang     int rank,nx,ny;
1536*c537a176SHong Zhang     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
1537*c537a176SHong Zhang     if (rank == 0) {
1538*c537a176SHong Zhang       x  = (Mat_SeqAIJ *)xx->B->data;
1539*c537a176SHong Zhang       int j,row=0,*garray=xx->garray;
1540*c537a176SHong Zhang       MatGetSize(xx->B,PETSC_NULL,&nx);
1541*c537a176SHong Zhang       MatGetSize(yy->B,PETSC_NULL,&ny);
1542*c537a176SHong Zhang       printf("nx: %d, ny: %d\n",nx,ny);
1543*c537a176SHong Zhang       for (j=0; j<nx; j++){
1544*c537a176SHong Zhang         printf("%d, ",garray[j]);
1545*c537a176SHong Zhang       }
1546*c537a176SHong Zhang       printf("\n----------- \n");
1547*c537a176SHong Zhang       for (j=x->i[row]; j<x->i[row+1]; j++){
1548*c537a176SHong Zhang         printf("%d, ",x->j[j]);
1549*c537a176SHong Zhang       }
1550*c537a176SHong Zhang       printf("\n----------- \n");
1551*c537a176SHong Zhang       /* printf("[%d], xx->A: \n", rank);
1552*c537a176SHong Zhang          ierr = MatView(xx->A,PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr); */
1553*c537a176SHong Zhang       printf("[%d], xx->B: \n", rank);
1554*c537a176SHong Zhang       ierr = MatView(xx->B,PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
1555*c537a176SHong Zhang       printf(" -----------\n");
1556*c537a176SHong Zhang     }
1557*c537a176SHong Zhang 
1558*c537a176SHong Zhang     ierr = MatView(X,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1559ac90fabeSBarry Smith   } else {
1560ac90fabeSBarry Smith     ierr = MatAXPY_Basic(a,X,Y,str);CHKERRQ(ierr);
1561ac90fabeSBarry Smith   }
1562ac90fabeSBarry Smith   PetscFunctionReturn(0);
1563ac90fabeSBarry Smith }
1564ac90fabeSBarry Smith 
15658a729477SBarry Smith /* -------------------------------------------------------------------*/
1566cda55fadSBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ,
1567cda55fadSBarry Smith        MatGetRow_MPIAIJ,
1568cda55fadSBarry Smith        MatRestoreRow_MPIAIJ,
1569cda55fadSBarry Smith        MatMult_MPIAIJ,
1570cda55fadSBarry Smith        MatMultAdd_MPIAIJ,
15717c922b88SBarry Smith        MatMultTranspose_MPIAIJ,
15727c922b88SBarry Smith        MatMultTransposeAdd_MPIAIJ,
1573cda55fadSBarry Smith        0,
1574cda55fadSBarry Smith        0,
1575cda55fadSBarry Smith        0,
1576cda55fadSBarry Smith        0,
1577cda55fadSBarry Smith        0,
1578cda55fadSBarry Smith        0,
157944a69424SLois Curfman McInnes        MatRelax_MPIAIJ,
1580b7c46309SBarry Smith        MatTranspose_MPIAIJ,
1581cda55fadSBarry Smith        MatGetInfo_MPIAIJ,
1582cda55fadSBarry Smith        MatEqual_MPIAIJ,
1583cda55fadSBarry Smith        MatGetDiagonal_MPIAIJ,
1584cda55fadSBarry Smith        MatDiagonalScale_MPIAIJ,
1585cda55fadSBarry Smith        MatNorm_MPIAIJ,
1586cda55fadSBarry Smith        MatAssemblyBegin_MPIAIJ,
1587cda55fadSBarry Smith        MatAssemblyEnd_MPIAIJ,
15881eb62cbbSBarry Smith        0,
1589cda55fadSBarry Smith        MatSetOption_MPIAIJ,
1590cda55fadSBarry Smith        MatZeroEntries_MPIAIJ,
1591cda55fadSBarry Smith        MatZeroRows_MPIAIJ,
159287828ca2SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
1593b9617806SBarry Smith        MatLUFactorSymbolic_MPIAIJ_TFS,
1594b9617806SBarry Smith #else
1595cda55fadSBarry Smith        0,
1596b9617806SBarry Smith #endif
1597cda55fadSBarry Smith        0,
1598cda55fadSBarry Smith        0,
1599cda55fadSBarry Smith        0,
1600273d9f13SBarry Smith        MatSetUpPreallocation_MPIAIJ,
1601cda55fadSBarry Smith        0,
1602cda55fadSBarry Smith        0,
1603cda55fadSBarry Smith        0,
1604cda55fadSBarry Smith        0,
16052e8a6d31SBarry Smith        MatDuplicate_MPIAIJ,
1606cda55fadSBarry Smith        0,
1607cda55fadSBarry Smith        0,
1608cda55fadSBarry Smith        0,
1609cda55fadSBarry Smith        0,
1610ac90fabeSBarry Smith        MatAXPY_MPIAIJ,
1611cda55fadSBarry Smith        MatGetSubMatrices_MPIAIJ,
1612cda55fadSBarry Smith        MatIncreaseOverlap_MPIAIJ,
1613cda55fadSBarry Smith        MatGetValues_MPIAIJ,
1614cb5b572fSBarry Smith        MatCopy_MPIAIJ,
1615052efed2SBarry Smith        MatPrintHelp_MPIAIJ,
1616cda55fadSBarry Smith        MatScale_MPIAIJ,
1617cda55fadSBarry Smith        0,
1618cda55fadSBarry Smith        0,
1619cda55fadSBarry Smith        0,
1620cda55fadSBarry Smith        MatGetBlockSize_MPIAIJ,
1621cda55fadSBarry Smith        0,
1622cda55fadSBarry Smith        0,
1623cda55fadSBarry Smith        0,
1624cda55fadSBarry Smith        0,
1625cda55fadSBarry Smith        MatFDColoringCreate_MPIAIJ,
1626cda55fadSBarry Smith        0,
1627cda55fadSBarry Smith        MatSetUnfactored_MPIAIJ,
1628cda55fadSBarry Smith        0,
1629cda55fadSBarry Smith        0,
1630cda55fadSBarry Smith        MatGetSubMatrix_MPIAIJ,
1631e03a110bSBarry Smith        MatDestroy_MPIAIJ,
1632e03a110bSBarry Smith        MatView_MPIAIJ,
16338a124369SBarry Smith        MatGetPetscMaps_Petsc,
1634a2243be0SBarry Smith        0,
1635a2243be0SBarry Smith        0,
1636a2243be0SBarry Smith        0,
1637a2243be0SBarry Smith        0,
1638a2243be0SBarry Smith        0,
1639a2243be0SBarry Smith        0,
1640a2243be0SBarry Smith        0,
1641a2243be0SBarry Smith        0,
1642a2243be0SBarry Smith        MatSetColoring_MPIAIJ,
1643779c1a83SBarry Smith        MatSetValuesAdic_MPIAIJ,
1644779c1a83SBarry Smith        MatSetValuesAdifor_MPIAIJ
1645a2243be0SBarry Smith };
164636ce4990SBarry Smith 
16472e8a6d31SBarry Smith /* ----------------------------------------------------------------------------------------*/
16482e8a6d31SBarry Smith 
1649fb2e594dSBarry Smith EXTERN_C_BEGIN
16504a2ae208SSatish Balay #undef __FUNCT__
16514a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_MPIAIJ"
16522e8a6d31SBarry Smith int MatStoreValues_MPIAIJ(Mat mat)
16532e8a6d31SBarry Smith {
16542e8a6d31SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data;
16552e8a6d31SBarry Smith   int        ierr;
16562e8a6d31SBarry Smith 
16572e8a6d31SBarry Smith   PetscFunctionBegin;
16582e8a6d31SBarry Smith   ierr = MatStoreValues(aij->A);CHKERRQ(ierr);
16592e8a6d31SBarry Smith   ierr = MatStoreValues(aij->B);CHKERRQ(ierr);
16602e8a6d31SBarry Smith   PetscFunctionReturn(0);
16612e8a6d31SBarry Smith }
1662fb2e594dSBarry Smith EXTERN_C_END
16632e8a6d31SBarry Smith 
1664fb2e594dSBarry Smith EXTERN_C_BEGIN
16654a2ae208SSatish Balay #undef __FUNCT__
16664a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_MPIAIJ"
16672e8a6d31SBarry Smith int MatRetrieveValues_MPIAIJ(Mat mat)
16682e8a6d31SBarry Smith {
16692e8a6d31SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data;
16702e8a6d31SBarry Smith   int        ierr;
16712e8a6d31SBarry Smith 
16722e8a6d31SBarry Smith   PetscFunctionBegin;
16732e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr);
16742e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr);
16752e8a6d31SBarry Smith   PetscFunctionReturn(0);
16762e8a6d31SBarry Smith }
1677fb2e594dSBarry Smith EXTERN_C_END
16788a729477SBarry Smith 
1679e090d566SSatish Balay #include "petscpc.h"
168027508adbSBarry Smith EXTERN_C_BEGIN
1681ca44d042SBarry Smith EXTERN int MatGetDiagonalBlock_MPIAIJ(Mat,PetscTruth *,MatReuse,Mat *);
168227508adbSBarry Smith EXTERN_C_END
168327508adbSBarry Smith 
1684273d9f13SBarry Smith EXTERN_C_BEGIN
16854a2ae208SSatish Balay #undef __FUNCT__
16864a2ae208SSatish Balay #define __FUNCT__ "MatCreate_MPIAIJ"
1687273d9f13SBarry Smith int MatCreate_MPIAIJ(Mat B)
16888a729477SBarry Smith {
168944cd7ae7SLois Curfman McInnes   Mat_MPIAIJ *b;
1690f1af5d2fSBarry Smith   int        ierr,i,size;
1691416022c9SBarry Smith 
16923a40ed3dSBarry Smith   PetscFunctionBegin;
1693273d9f13SBarry Smith   ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr);
16941d55c564SBarry Smith 
1695b0a32e0cSBarry Smith   ierr            = PetscNew(Mat_MPIAIJ,&b);CHKERRQ(ierr);
1696b0a32e0cSBarry Smith   B->data         = (void*)b;
1697549d3d68SSatish Balay   ierr            = PetscMemzero(b,sizeof(Mat_MPIAIJ));CHKERRQ(ierr);
1698549d3d68SSatish Balay   ierr            = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
169944cd7ae7SLois Curfman McInnes   B->factor       = 0;
170044cd7ae7SLois Curfman McInnes   B->assembled    = PETSC_FALSE;
170190f02eecSBarry Smith   B->mapping      = 0;
1702d6dfbf8fSBarry Smith 
170347794344SBarry Smith   B->insertmode      = NOT_SET_VALUES;
17049eb4d147SSatish Balay   b->size            = size;
1705273d9f13SBarry Smith   ierr = MPI_Comm_rank(B->comm,&b->rank);CHKERRQ(ierr);
17061eb62cbbSBarry Smith 
1707273d9f13SBarry Smith   ierr = PetscSplitOwnership(B->comm,&B->m,&B->M);CHKERRQ(ierr);
1708273d9f13SBarry Smith   ierr = PetscSplitOwnership(B->comm,&B->n,&B->N);CHKERRQ(ierr);
17091eb62cbbSBarry Smith 
1710c7fcc2eaSBarry Smith   /* the information in the maps duplicates the information computed below, eventually
1711c7fcc2eaSBarry Smith      we should remove the duplicate information that is not contained in the maps */
17128a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->m,B->M,&B->rmap);CHKERRQ(ierr);
17138a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->n,B->N,&B->cmap);CHKERRQ(ierr);
1714c7fcc2eaSBarry Smith 
17151eb62cbbSBarry Smith   /* build local table of row and column ownerships */
1716b0a32e0cSBarry Smith   ierr = PetscMalloc(2*(b->size+2)*sizeof(int),&b->rowners);CHKERRQ(ierr);
1717b0a32e0cSBarry Smith   PetscLogObjectMemory(B,2*(b->size+2)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_MPIAIJ));
1718603f58a4SSatish Balay   b->cowners = b->rowners + b->size + 2;
1719273d9f13SBarry Smith   ierr = MPI_Allgather(&B->m,1,MPI_INT,b->rowners+1,1,MPI_INT,B->comm);CHKERRQ(ierr);
172044cd7ae7SLois Curfman McInnes   b->rowners[0] = 0;
172144cd7ae7SLois Curfman McInnes   for (i=2; i<=b->size; i++) {
172244cd7ae7SLois Curfman McInnes     b->rowners[i] += b->rowners[i-1];
17238a729477SBarry Smith   }
172444cd7ae7SLois Curfman McInnes   b->rstart = b->rowners[b->rank];
172544cd7ae7SLois Curfman McInnes   b->rend   = b->rowners[b->rank+1];
1726273d9f13SBarry Smith   ierr = MPI_Allgather(&B->n,1,MPI_INT,b->cowners+1,1,MPI_INT,B->comm);CHKERRQ(ierr);
172744cd7ae7SLois Curfman McInnes   b->cowners[0] = 0;
172844cd7ae7SLois Curfman McInnes   for (i=2; i<=b->size; i++) {
172944cd7ae7SLois Curfman McInnes     b->cowners[i] += b->cowners[i-1];
17308a729477SBarry Smith   }
173144cd7ae7SLois Curfman McInnes   b->cstart = b->cowners[b->rank];
173244cd7ae7SLois Curfman McInnes   b->cend   = b->cowners[b->rank+1];
17338a729477SBarry Smith 
17341eb62cbbSBarry Smith   /* build cache for off array entries formed */
17357e2c5f70SBarry Smith   ierr = MatStashCreate_Private(B->comm,1,&B->stash);CHKERRQ(ierr);
17367c922b88SBarry Smith   b->donotstash  = PETSC_FALSE;
173744cd7ae7SLois Curfman McInnes   b->colmap      = 0;
173844cd7ae7SLois Curfman McInnes   b->garray      = 0;
17397c922b88SBarry Smith   b->roworiented = PETSC_TRUE;
17408a729477SBarry Smith 
17411eb62cbbSBarry Smith   /* stuff used for matrix vector multiply */
17427c922b88SBarry Smith   b->lvec      = PETSC_NULL;
17437c922b88SBarry Smith   b->Mvctx     = PETSC_NULL;
17448a729477SBarry Smith 
17457a0afa10SBarry Smith   /* stuff for MatGetRow() */
174644cd7ae7SLois Curfman McInnes   b->rowindices   = 0;
174744cd7ae7SLois Curfman McInnes   b->rowvalues    = 0;
174844cd7ae7SLois Curfman McInnes   b->getrowactive = PETSC_FALSE;
17491a8d6bc9SBarry Smith 
1750f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
17512e8a6d31SBarry Smith                                      "MatStoreValues_MPIAIJ",
17520c97f09dSSatish Balay                                      MatStoreValues_MPIAIJ);CHKERRQ(ierr);
1753f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
17542e8a6d31SBarry Smith                                      "MatRetrieveValues_MPIAIJ",
17550c97f09dSSatish Balay                                      MatRetrieveValues_MPIAIJ);CHKERRQ(ierr);
1756f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetDiagonalBlock_C",
17575ef9f2a5SBarry Smith                                      "MatGetDiagonalBlock_MPIAIJ",
17580c97f09dSSatish Balay                                      MatGetDiagonalBlock_MPIAIJ);CHKERRQ(ierr);
17599b9367d5SHong Zhang 
17603a40ed3dSBarry Smith   PetscFunctionReturn(0);
1761d6dfbf8fSBarry Smith }
1762273d9f13SBarry Smith EXTERN_C_END
1763c74985f6SBarry Smith 
17644a2ae208SSatish Balay #undef __FUNCT__
17654a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_MPIAIJ"
17662e8a6d31SBarry Smith int MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
1767d6dfbf8fSBarry Smith {
1768d6dfbf8fSBarry Smith   Mat        mat;
1769416022c9SBarry Smith   Mat_MPIAIJ *a,*oldmat = (Mat_MPIAIJ*)matin->data;
177016d6aa21SSatish Balay   int        ierr;
1771d6dfbf8fSBarry Smith 
17723a40ed3dSBarry Smith   PetscFunctionBegin;
1773416022c9SBarry Smith   *newmat       = 0;
1774273d9f13SBarry Smith   ierr = MatCreate(matin->comm,matin->m,matin->n,matin->M,matin->N,&mat);CHKERRQ(ierr);
1775273d9f13SBarry Smith   ierr = MatSetType(mat,MATMPIAIJ);CHKERRQ(ierr);
1776273d9f13SBarry Smith   a    = (Mat_MPIAIJ*)mat->data;
1777549d3d68SSatish Balay   ierr              = PetscMemcpy(mat->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
1778d6dfbf8fSBarry Smith   mat->factor       = matin->factor;
1779c456f294SBarry Smith   mat->assembled    = PETSC_TRUE;
1780e7641de0SSatish Balay   mat->insertmode   = NOT_SET_VALUES;
1781273d9f13SBarry Smith   mat->preallocated = PETSC_TRUE;
1782d6dfbf8fSBarry Smith 
1783416022c9SBarry Smith   a->rstart       = oldmat->rstart;
1784416022c9SBarry Smith   a->rend         = oldmat->rend;
1785416022c9SBarry Smith   a->cstart       = oldmat->cstart;
1786416022c9SBarry Smith   a->cend         = oldmat->cend;
178717699dbbSLois Curfman McInnes   a->size         = oldmat->size;
178817699dbbSLois Curfman McInnes   a->rank         = oldmat->rank;
1789e7641de0SSatish Balay   a->donotstash   = oldmat->donotstash;
1790e7641de0SSatish Balay   a->roworiented  = oldmat->roworiented;
1791e7641de0SSatish Balay   a->rowindices   = 0;
1792bcd2baecSBarry Smith   a->rowvalues    = 0;
1793bcd2baecSBarry Smith   a->getrowactive = PETSC_FALSE;
1794d6dfbf8fSBarry Smith 
1795549d3d68SSatish Balay   ierr       = PetscMemcpy(a->rowners,oldmat->rowners,2*(a->size+2)*sizeof(int));CHKERRQ(ierr);
17968798bf22SSatish Balay   ierr       = MatStashCreate_Private(matin->comm,1,&mat->stash);CHKERRQ(ierr);
17972ee70a88SLois Curfman McInnes   if (oldmat->colmap) {
1798aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
17990f5bd95cSBarry Smith     ierr = PetscTableCreateCopy(oldmat->colmap,&a->colmap);CHKERRQ(ierr);
1800b1fc9764SSatish Balay #else
1801b0a32e0cSBarry Smith     ierr = PetscMalloc((mat->N)*sizeof(int),&a->colmap);CHKERRQ(ierr);
1802b0a32e0cSBarry Smith     PetscLogObjectMemory(mat,(mat->N)*sizeof(int));
1803273d9f13SBarry Smith     ierr      = PetscMemcpy(a->colmap,oldmat->colmap,(mat->N)*sizeof(int));CHKERRQ(ierr);
1804b1fc9764SSatish Balay #endif
1805416022c9SBarry Smith   } else a->colmap = 0;
18063f41c07dSBarry Smith   if (oldmat->garray) {
180716d6aa21SSatish Balay     int len;
1808273d9f13SBarry Smith     len  = oldmat->B->n;
1809b0a32e0cSBarry Smith     ierr = PetscMalloc((len+1)*sizeof(int),&a->garray);CHKERRQ(ierr);
1810b0a32e0cSBarry Smith     PetscLogObjectMemory(mat,len*sizeof(int));
1811549d3d68SSatish Balay     if (len) { ierr = PetscMemcpy(a->garray,oldmat->garray,len*sizeof(int));CHKERRQ(ierr); }
1812416022c9SBarry Smith   } else a->garray = 0;
1813d6dfbf8fSBarry Smith 
1814416022c9SBarry Smith   ierr =  VecDuplicate(oldmat->lvec,&a->lvec);CHKERRQ(ierr);
1815b0a32e0cSBarry Smith   PetscLogObjectParent(mat,a->lvec);
1816a56f8943SBarry Smith   ierr =  VecScatterCopy(oldmat->Mvctx,&a->Mvctx);CHKERRQ(ierr);
1817b0a32e0cSBarry Smith   PetscLogObjectParent(mat,a->Mvctx);
18182e8a6d31SBarry Smith   ierr =  MatDuplicate(oldmat->A,cpvalues,&a->A);CHKERRQ(ierr);
1819b0a32e0cSBarry Smith   PetscLogObjectParent(mat,a->A);
18202e8a6d31SBarry Smith   ierr =  MatDuplicate(oldmat->B,cpvalues,&a->B);CHKERRQ(ierr);
1821b0a32e0cSBarry Smith   PetscLogObjectParent(mat,a->B);
1822b0a32e0cSBarry Smith   ierr = PetscFListDuplicate(matin->qlist,&mat->qlist);CHKERRQ(ierr);
18238a729477SBarry Smith   *newmat = mat;
18243a40ed3dSBarry Smith   PetscFunctionReturn(0);
18258a729477SBarry Smith }
1826416022c9SBarry Smith 
1827e090d566SSatish Balay #include "petscsys.h"
1828416022c9SBarry Smith 
1829273d9f13SBarry Smith EXTERN_C_BEGIN
18304a2ae208SSatish Balay #undef __FUNCT__
18314a2ae208SSatish Balay #define __FUNCT__ "MatLoad_MPIAIJ"
1832b0a32e0cSBarry Smith int MatLoad_MPIAIJ(PetscViewer viewer,MatType type,Mat *newmat)
1833416022c9SBarry Smith {
1834d65a2f8fSBarry Smith   Mat          A;
183587828ca2SBarry Smith   PetscScalar  *vals,*svals;
183619bcc07fSBarry Smith   MPI_Comm     comm = ((PetscObject)viewer)->comm;
1837416022c9SBarry Smith   MPI_Status   status;
18383a40ed3dSBarry Smith   int          i,nz,ierr,j,rstart,rend,fd;
183917699dbbSLois Curfman McInnes   int          header[4],rank,size,*rowlengths = 0,M,N,m,*rowners,maxnz,*cols;
1840d65a2f8fSBarry Smith   int          *ourlens,*sndcounts = 0,*procsnz = 0,*offlens,jj,*mycols,*smycols;
1841b362ba68SBarry Smith   int          tag = ((PetscObject)viewer)->tag,cend,cstart,n;
184227fa2452SHong Zhang #if defined(PETSC_HAVE_SPOOLES) || defined(PETSC_HAVE_SUPERLUDIST)
184327fa2452SHong Zhang   PetscTruth   flag;
184427fa2452SHong Zhang #endif
1845416022c9SBarry Smith 
18463a40ed3dSBarry Smith   PetscFunctionBegin;
18471dab6e02SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
18481dab6e02SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
184917699dbbSLois Curfman McInnes   if (!rank) {
1850b0a32e0cSBarry Smith     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
18510752156aSBarry Smith     ierr = PetscBinaryRead(fd,(char *)header,4,PETSC_INT);CHKERRQ(ierr);
1852552e946dSBarry Smith     if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
1853d64ed03dSBarry Smith     if (header[3] < 0) {
185429bbc08cSBarry Smith       SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix in special format on disk, cannot load as MPIAIJ");
1855d64ed03dSBarry Smith     }
18566c5fab8fSBarry Smith   }
18576c5fab8fSBarry Smith 
1858ca161407SBarry Smith   ierr = MPI_Bcast(header+1,3,MPI_INT,0,comm);CHKERRQ(ierr);
1859416022c9SBarry Smith   M = header[1]; N = header[2];
1860416022c9SBarry Smith   /* determine ownership of all rows */
186117699dbbSLois Curfman McInnes   m = M/size + ((M % size) > rank);
1862b0a32e0cSBarry Smith   ierr = PetscMalloc((size+2)*sizeof(int),&rowners);CHKERRQ(ierr);
1863ca161407SBarry Smith   ierr = MPI_Allgather(&m,1,MPI_INT,rowners+1,1,MPI_INT,comm);CHKERRQ(ierr);
1864416022c9SBarry Smith   rowners[0] = 0;
186517699dbbSLois Curfman McInnes   for (i=2; i<=size; i++) {
1866416022c9SBarry Smith     rowners[i] += rowners[i-1];
1867416022c9SBarry Smith   }
186817699dbbSLois Curfman McInnes   rstart = rowners[rank];
186917699dbbSLois Curfman McInnes   rend   = rowners[rank+1];
1870416022c9SBarry Smith 
1871416022c9SBarry Smith   /* distribute row lengths to all processors */
1872b0a32e0cSBarry Smith   ierr    = PetscMalloc(2*(rend-rstart+1)*sizeof(int),&ourlens);CHKERRQ(ierr);
1873416022c9SBarry Smith   offlens = ourlens + (rend-rstart);
187417699dbbSLois Curfman McInnes   if (!rank) {
1875b0a32e0cSBarry Smith     ierr = PetscMalloc(M*sizeof(int),&rowlengths);CHKERRQ(ierr);
18760752156aSBarry Smith     ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
1877b0a32e0cSBarry Smith     ierr = PetscMalloc(size*sizeof(int),&sndcounts);CHKERRQ(ierr);
187817699dbbSLois Curfman McInnes     for (i=0; i<size; i++) sndcounts[i] = rowners[i+1] - rowners[i];
1879ca161407SBarry Smith     ierr = MPI_Scatterv(rowlengths,sndcounts,rowners,MPI_INT,ourlens,rend-rstart,MPI_INT,0,comm);CHKERRQ(ierr);
1880606d414cSSatish Balay     ierr = PetscFree(sndcounts);CHKERRQ(ierr);
18813a40ed3dSBarry Smith   } else {
1882ca161407SBarry Smith     ierr = MPI_Scatterv(0,0,0,MPI_INT,ourlens,rend-rstart,MPI_INT,0,comm);CHKERRQ(ierr);
1883416022c9SBarry Smith   }
1884416022c9SBarry Smith 
188517699dbbSLois Curfman McInnes   if (!rank) {
1886416022c9SBarry Smith     /* calculate the number of nonzeros on each processor */
1887b0a32e0cSBarry Smith     ierr = PetscMalloc(size*sizeof(int),&procsnz);CHKERRQ(ierr);
1888549d3d68SSatish Balay     ierr = PetscMemzero(procsnz,size*sizeof(int));CHKERRQ(ierr);
188917699dbbSLois Curfman McInnes     for (i=0; i<size; i++) {
1890416022c9SBarry Smith       for (j=rowners[i]; j< rowners[i+1]; j++) {
1891416022c9SBarry Smith         procsnz[i] += rowlengths[j];
1892416022c9SBarry Smith       }
1893416022c9SBarry Smith     }
1894606d414cSSatish Balay     ierr = PetscFree(rowlengths);CHKERRQ(ierr);
1895416022c9SBarry Smith 
1896416022c9SBarry Smith     /* determine max buffer needed and allocate it */
1897416022c9SBarry Smith     maxnz = 0;
189817699dbbSLois Curfman McInnes     for (i=0; i<size; i++) {
18990452661fSBarry Smith       maxnz = PetscMax(maxnz,procsnz[i]);
1900416022c9SBarry Smith     }
1901b0a32e0cSBarry Smith     ierr = PetscMalloc(maxnz*sizeof(int),&cols);CHKERRQ(ierr);
1902416022c9SBarry Smith 
1903416022c9SBarry Smith     /* read in my part of the matrix column indices  */
1904416022c9SBarry Smith     nz   = procsnz[0];
1905b0a32e0cSBarry Smith     ierr = PetscMalloc(nz*sizeof(int),&mycols);CHKERRQ(ierr);
19060752156aSBarry Smith     ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr);
1907d65a2f8fSBarry Smith 
1908d65a2f8fSBarry Smith     /* read in every one elses and ship off */
190917699dbbSLois Curfman McInnes     for (i=1; i<size; i++) {
1910d65a2f8fSBarry Smith       nz   = procsnz[i];
19110752156aSBarry Smith       ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr);
1912ca161407SBarry Smith       ierr = MPI_Send(cols,nz,MPI_INT,i,tag,comm);CHKERRQ(ierr);
1913d65a2f8fSBarry Smith     }
1914606d414cSSatish Balay     ierr = PetscFree(cols);CHKERRQ(ierr);
19153a40ed3dSBarry Smith   } else {
1916416022c9SBarry Smith     /* determine buffer space needed for message */
1917416022c9SBarry Smith     nz = 0;
1918416022c9SBarry Smith     for (i=0; i<m; i++) {
1919416022c9SBarry Smith       nz += ourlens[i];
1920416022c9SBarry Smith     }
1921b0a32e0cSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(int),&mycols);CHKERRQ(ierr);
1922416022c9SBarry Smith 
1923416022c9SBarry Smith     /* receive message of column indices*/
1924ca161407SBarry Smith     ierr = MPI_Recv(mycols,nz,MPI_INT,0,tag,comm,&status);CHKERRQ(ierr);
1925ca161407SBarry Smith     ierr = MPI_Get_count(&status,MPI_INT,&maxnz);CHKERRQ(ierr);
192629bbc08cSBarry Smith     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
1927416022c9SBarry Smith   }
1928416022c9SBarry Smith 
1929b362ba68SBarry Smith   /* determine column ownership if matrix is not square */
1930b362ba68SBarry Smith   if (N != M) {
1931b362ba68SBarry Smith     n      = N/size + ((N % size) > rank);
1932b362ba68SBarry Smith     ierr   = MPI_Scan(&n,&cend,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr);
1933b362ba68SBarry Smith     cstart = cend - n;
1934b362ba68SBarry Smith   } else {
1935b362ba68SBarry Smith     cstart = rstart;
1936b362ba68SBarry Smith     cend   = rend;
1937fb2e594dSBarry Smith     n      = cend - cstart;
1938b362ba68SBarry Smith   }
1939b362ba68SBarry Smith 
1940416022c9SBarry Smith   /* loop over local rows, determining number of off diagonal entries */
1941549d3d68SSatish Balay   ierr = PetscMemzero(offlens,m*sizeof(int));CHKERRQ(ierr);
1942416022c9SBarry Smith   jj = 0;
1943416022c9SBarry Smith   for (i=0; i<m; i++) {
1944416022c9SBarry Smith     for (j=0; j<ourlens[i]; j++) {
1945b362ba68SBarry Smith       if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++;
1946416022c9SBarry Smith       jj++;
1947416022c9SBarry Smith     }
1948416022c9SBarry Smith   }
1949d65a2f8fSBarry Smith 
1950d65a2f8fSBarry Smith   /* create our matrix */
1951416022c9SBarry Smith   for (i=0; i<m; i++) {
1952416022c9SBarry Smith     ourlens[i] -= offlens[i];
1953416022c9SBarry Smith   }
1954b362ba68SBarry Smith   ierr = MatCreateMPIAIJ(comm,m,n,M,N,0,ourlens,0,offlens,newmat);CHKERRQ(ierr);
1955d65a2f8fSBarry Smith   A = *newmat;
1956fb2e594dSBarry Smith   ierr = MatSetOption(A,MAT_COLUMNS_SORTED);CHKERRQ(ierr);
1957d65a2f8fSBarry Smith   for (i=0; i<m; i++) {
1958d65a2f8fSBarry Smith     ourlens[i] += offlens[i];
1959d65a2f8fSBarry Smith   }
1960416022c9SBarry Smith 
196117699dbbSLois Curfman McInnes   if (!rank) {
196287828ca2SBarry Smith     ierr = PetscMalloc(maxnz*sizeof(PetscScalar),&vals);CHKERRQ(ierr);
1963416022c9SBarry Smith 
1964416022c9SBarry Smith     /* read in my part of the matrix numerical values  */
1965416022c9SBarry Smith     nz   = procsnz[0];
19660752156aSBarry Smith     ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
1967d65a2f8fSBarry Smith 
1968d65a2f8fSBarry Smith     /* insert into matrix */
1969d65a2f8fSBarry Smith     jj      = rstart;
1970d65a2f8fSBarry Smith     smycols = mycols;
1971d65a2f8fSBarry Smith     svals   = vals;
1972d65a2f8fSBarry Smith     for (i=0; i<m; i++) {
1973d65a2f8fSBarry Smith       ierr = MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
1974d65a2f8fSBarry Smith       smycols += ourlens[i];
1975d65a2f8fSBarry Smith       svals   += ourlens[i];
1976d65a2f8fSBarry Smith       jj++;
1977416022c9SBarry Smith     }
1978416022c9SBarry Smith 
1979d65a2f8fSBarry Smith     /* read in other processors and ship out */
198017699dbbSLois Curfman McInnes     for (i=1; i<size; i++) {
1981416022c9SBarry Smith       nz   = procsnz[i];
19820752156aSBarry Smith       ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
1983ca161407SBarry Smith       ierr = MPI_Send(vals,nz,MPIU_SCALAR,i,A->tag,comm);CHKERRQ(ierr);
1984416022c9SBarry Smith     }
1985606d414cSSatish Balay     ierr = PetscFree(procsnz);CHKERRQ(ierr);
19863a40ed3dSBarry Smith   } else {
1987d65a2f8fSBarry Smith     /* receive numeric values */
198887828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&vals);CHKERRQ(ierr);
1989416022c9SBarry Smith 
1990d65a2f8fSBarry Smith     /* receive message of values*/
1991ca161407SBarry Smith     ierr = MPI_Recv(vals,nz,MPIU_SCALAR,0,A->tag,comm,&status);CHKERRQ(ierr);
1992ca161407SBarry Smith     ierr = MPI_Get_count(&status,MPIU_SCALAR,&maxnz);CHKERRQ(ierr);
199329bbc08cSBarry Smith     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
1994d65a2f8fSBarry Smith 
1995d65a2f8fSBarry Smith     /* insert into matrix */
1996d65a2f8fSBarry Smith     jj      = rstart;
1997d65a2f8fSBarry Smith     smycols = mycols;
1998d65a2f8fSBarry Smith     svals   = vals;
1999d65a2f8fSBarry Smith     for (i=0; i<m; i++) {
2000d65a2f8fSBarry Smith       ierr     = MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
2001d65a2f8fSBarry Smith       smycols += ourlens[i];
2002d65a2f8fSBarry Smith       svals   += ourlens[i];
2003d65a2f8fSBarry Smith       jj++;
2004d65a2f8fSBarry Smith     }
2005d65a2f8fSBarry Smith   }
2006606d414cSSatish Balay   ierr = PetscFree(ourlens);CHKERRQ(ierr);
2007606d414cSSatish Balay   ierr = PetscFree(vals);CHKERRQ(ierr);
2008606d414cSSatish Balay   ierr = PetscFree(mycols);CHKERRQ(ierr);
2009606d414cSSatish Balay   ierr = PetscFree(rowners);CHKERRQ(ierr);
2010d65a2f8fSBarry Smith 
20116d4a8577SBarry Smith   ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20126d4a8577SBarry Smith   ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
201327fa2452SHong Zhang #if defined(PETSC_HAVE_SPOOLES)
201427fa2452SHong Zhang   ierr = PetscOptionsHasName(A->prefix,"-mat_aij_spooles",&flag);CHKERRQ(ierr);
20150f39d4ecSHong Zhang   if (flag) {
20160f39d4ecSHong Zhang     if (size == 1) {
20170f39d4ecSHong Zhang       ierr = MatUseSpooles_SeqAIJ(A);CHKERRQ(ierr);
20180f39d4ecSHong Zhang     } else {
20190f39d4ecSHong Zhang       ierr = MatUseSpooles_MPIAIJ(A);CHKERRQ(ierr);
20200f39d4ecSHong Zhang     }
20210f39d4ecSHong Zhang   }
202227fa2452SHong Zhang #endif
202327fa2452SHong Zhang #if defined(PETSC_HAVE_SUPERLUDIST)
202427fa2452SHong Zhang   ierr = PetscOptionsHasName(A->prefix,"-mat_aij_superlu_dist",&flag);CHKERRQ(ierr);
202527fa2452SHong Zhang   if (flag) { ierr = MatUseSuperLU_DIST_MPIAIJ(A);CHKERRQ(ierr); }
202627fa2452SHong Zhang #endif
20273a40ed3dSBarry Smith   PetscFunctionReturn(0);
2028416022c9SBarry Smith }
2029273d9f13SBarry Smith EXTERN_C_END
2030a0ff6018SBarry Smith 
20314a2ae208SSatish Balay #undef __FUNCT__
20324a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_MPIAIJ"
2033a0ff6018SBarry Smith /*
203429da9460SBarry Smith     Not great since it makes two copies of the submatrix, first an SeqAIJ
203529da9460SBarry Smith   in local and then by concatenating the local matrices the end result.
203629da9460SBarry Smith   Writing it directly would be much like MatGetSubMatrices_MPIAIJ()
2037a0ff6018SBarry Smith */
20387b2a1423SBarry Smith int MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,int csize,MatReuse call,Mat *newmat)
2039a0ff6018SBarry Smith {
204000e6dbe6SBarry Smith   int          ierr,i,m,n,rstart,row,rend,nz,*cwork,size,rank,j;
2041ab50ec6bSBarry Smith   int          *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal;
2042fee21e36SBarry Smith   Mat          *local,M,Mreuse;
204387828ca2SBarry Smith   PetscScalar  *vwork,*aa;
204400e6dbe6SBarry Smith   MPI_Comm     comm = mat->comm;
204500e6dbe6SBarry Smith   Mat_SeqAIJ   *aij;
20467e2c5f70SBarry Smith 
2047a0ff6018SBarry Smith 
2048a0ff6018SBarry Smith   PetscFunctionBegin;
20491dab6e02SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
20501dab6e02SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
205100e6dbe6SBarry Smith 
2052fee21e36SBarry Smith   if (call ==  MAT_REUSE_MATRIX) {
2053fee21e36SBarry Smith     ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);CHKERRQ(ierr);
205429bbc08cSBarry Smith     if (!Mreuse) SETERRQ(1,"Submatrix passed in was not used before, cannot reuse");
2055fee21e36SBarry Smith     local = &Mreuse;
2056fee21e36SBarry Smith     ierr  = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&local);CHKERRQ(ierr);
2057fee21e36SBarry Smith   } else {
2058a0ff6018SBarry Smith     ierr   = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr);
2059fee21e36SBarry Smith     Mreuse = *local;
2060606d414cSSatish Balay     ierr   = PetscFree(local);CHKERRQ(ierr);
2061fee21e36SBarry Smith   }
2062a0ff6018SBarry Smith 
2063a0ff6018SBarry Smith   /*
2064a0ff6018SBarry Smith       m - number of local rows
2065a0ff6018SBarry Smith       n - number of columns (same on all processors)
2066a0ff6018SBarry Smith       rstart - first row in new global matrix generated
2067a0ff6018SBarry Smith   */
2068fee21e36SBarry Smith   ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr);
2069a0ff6018SBarry Smith   if (call == MAT_INITIAL_MATRIX) {
2070fee21e36SBarry Smith     aij = (Mat_SeqAIJ*)(Mreuse)->data;
207129bbc08cSBarry Smith     if (aij->indexshift) SETERRQ(PETSC_ERR_SUP,"No support for index shifted matrix");
207200e6dbe6SBarry Smith     ii  = aij->i;
207300e6dbe6SBarry Smith     jj  = aij->j;
207400e6dbe6SBarry Smith 
2075a0ff6018SBarry Smith     /*
207600e6dbe6SBarry Smith         Determine the number of non-zeros in the diagonal and off-diagonal
207700e6dbe6SBarry Smith         portions of the matrix in order to do correct preallocation
2078a0ff6018SBarry Smith     */
207900e6dbe6SBarry Smith 
208000e6dbe6SBarry Smith     /* first get start and end of "diagonal" columns */
20816a6a5d1dSBarry Smith     if (csize == PETSC_DECIDE) {
2082ab50ec6bSBarry Smith       ierr = ISGetSize(isrow,&mglobal);CHKERRQ(ierr);
2083ab50ec6bSBarry Smith       if (mglobal == n) { /* square matrix */
2084e2c4fddaSBarry Smith 	nlocal = m;
20856a6a5d1dSBarry Smith       } else {
2086ab50ec6bSBarry Smith         nlocal = n/size + ((n % size) > rank);
2087ab50ec6bSBarry Smith       }
2088ab50ec6bSBarry Smith     } else {
20896a6a5d1dSBarry Smith       nlocal = csize;
20906a6a5d1dSBarry Smith     }
2091ca161407SBarry Smith     ierr   = MPI_Scan(&nlocal,&rend,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr);
209200e6dbe6SBarry Smith     rstart = rend - nlocal;
20936a6a5d1dSBarry Smith     if (rank == size - 1 && rend != n) {
209429bbc08cSBarry Smith       SETERRQ(1,"Local column sizes do not add up to total number of columns");
20956a6a5d1dSBarry Smith     }
209600e6dbe6SBarry Smith 
209700e6dbe6SBarry Smith     /* next, compute all the lengths */
2098b0a32e0cSBarry Smith     ierr  = PetscMalloc((2*m+1)*sizeof(int),&dlens);CHKERRQ(ierr);
209900e6dbe6SBarry Smith     olens = dlens + m;
210000e6dbe6SBarry Smith     for (i=0; i<m; i++) {
210100e6dbe6SBarry Smith       jend = ii[i+1] - ii[i];
210200e6dbe6SBarry Smith       olen = 0;
210300e6dbe6SBarry Smith       dlen = 0;
210400e6dbe6SBarry Smith       for (j=0; j<jend; j++) {
210500e6dbe6SBarry Smith         if (*jj < rstart || *jj >= rend) olen++;
210600e6dbe6SBarry Smith         else dlen++;
210700e6dbe6SBarry Smith         jj++;
210800e6dbe6SBarry Smith       }
210900e6dbe6SBarry Smith       olens[i] = olen;
211000e6dbe6SBarry Smith       dlens[i] = dlen;
211100e6dbe6SBarry Smith     }
21122d207970SBarry Smith     ierr = MatCreateMPIAIJ(comm,m,nlocal,PETSC_DECIDE,n,0,dlens,0,olens,&M);CHKERRQ(ierr);
2113606d414cSSatish Balay     ierr = PetscFree(dlens);CHKERRQ(ierr);
2114a0ff6018SBarry Smith   } else {
2115a0ff6018SBarry Smith     int ml,nl;
2116a0ff6018SBarry Smith 
2117a0ff6018SBarry Smith     M = *newmat;
2118a0ff6018SBarry Smith     ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr);
211929bbc08cSBarry Smith     if (ml != m) SETERRQ(PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
2120a0ff6018SBarry Smith     ierr = MatZeroEntries(M);CHKERRQ(ierr);
2121c48de900SBarry Smith     /*
2122c48de900SBarry Smith          The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
2123c48de900SBarry Smith        rather than the slower MatSetValues().
2124c48de900SBarry Smith     */
2125c48de900SBarry Smith     M->was_assembled = PETSC_TRUE;
2126c48de900SBarry Smith     M->assembled     = PETSC_FALSE;
2127a0ff6018SBarry Smith   }
2128a0ff6018SBarry Smith   ierr = MatGetOwnershipRange(M,&rstart,&rend);CHKERRQ(ierr);
2129fee21e36SBarry Smith   aij = (Mat_SeqAIJ*)(Mreuse)->data;
213029bbc08cSBarry Smith   if (aij->indexshift) SETERRQ(PETSC_ERR_SUP,"No support for index shifted matrix");
213100e6dbe6SBarry Smith   ii  = aij->i;
213200e6dbe6SBarry Smith   jj  = aij->j;
213300e6dbe6SBarry Smith   aa  = aij->a;
2134a0ff6018SBarry Smith   for (i=0; i<m; i++) {
2135a0ff6018SBarry Smith     row   = rstart + i;
213600e6dbe6SBarry Smith     nz    = ii[i+1] - ii[i];
213700e6dbe6SBarry Smith     cwork = jj;     jj += nz;
213800e6dbe6SBarry Smith     vwork = aa;     aa += nz;
21398c638d02SBarry Smith     ierr = MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr);
2140a0ff6018SBarry Smith   }
2141a0ff6018SBarry Smith 
2142a0ff6018SBarry Smith   ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2143a0ff6018SBarry Smith   ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2144a0ff6018SBarry Smith   *newmat = M;
2145fee21e36SBarry Smith 
2146fee21e36SBarry Smith   /* save submatrix used in processor for next request */
2147fee21e36SBarry Smith   if (call ==  MAT_INITIAL_MATRIX) {
2148fee21e36SBarry Smith     ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr);
2149fee21e36SBarry Smith     ierr = PetscObjectDereference((PetscObject)Mreuse);CHKERRQ(ierr);
2150fee21e36SBarry Smith   }
2151fee21e36SBarry Smith 
2152a0ff6018SBarry Smith   PetscFunctionReturn(0);
2153a0ff6018SBarry Smith }
2154273d9f13SBarry Smith 
21554a2ae208SSatish Balay #undef __FUNCT__
21564a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJSetPreallocation"
2157273d9f13SBarry Smith /*@C
2158273d9f13SBarry Smith    MatMPIAIJSetPreallocation - Creates a sparse parallel matrix in AIJ format
2159273d9f13SBarry Smith    (the default parallel PETSc format).  For good matrix assembly performance
2160273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameters
2161273d9f13SBarry Smith    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
2162273d9f13SBarry Smith    performance can be increased by more than a factor of 50.
2163273d9f13SBarry Smith 
2164273d9f13SBarry Smith    Collective on MPI_Comm
2165273d9f13SBarry Smith 
2166273d9f13SBarry Smith    Input Parameters:
2167273d9f13SBarry Smith +  A - the matrix
2168273d9f13SBarry Smith .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
2169273d9f13SBarry Smith            (same value is used for all local rows)
2170273d9f13SBarry Smith .  d_nnz - array containing the number of nonzeros in the various rows of the
2171273d9f13SBarry Smith            DIAGONAL portion of the local submatrix (possibly different for each row)
2172273d9f13SBarry Smith            or PETSC_NULL, if d_nz is used to specify the nonzero structure.
2173273d9f13SBarry Smith            The size of this array is equal to the number of local rows, i.e 'm'.
2174273d9f13SBarry Smith            You must leave room for the diagonal entry even if it is zero.
2175273d9f13SBarry Smith .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
2176273d9f13SBarry Smith            submatrix (same value is used for all local rows).
2177273d9f13SBarry Smith -  o_nnz - array containing the number of nonzeros in the various rows of the
2178273d9f13SBarry Smith            OFF-DIAGONAL portion of the local submatrix (possibly different for
2179273d9f13SBarry Smith            each row) or PETSC_NULL, if o_nz is used to specify the nonzero
2180273d9f13SBarry Smith            structure. The size of this array is equal to the number
2181273d9f13SBarry Smith            of local rows, i.e 'm'.
2182273d9f13SBarry Smith 
2183273d9f13SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
2184273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2185273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2186273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users manual for details.
2187273d9f13SBarry Smith 
2188273d9f13SBarry Smith    The user MUST specify either the local or global matrix dimensions
2189273d9f13SBarry Smith    (possibly both).
2190273d9f13SBarry Smith 
2191273d9f13SBarry Smith    The parallel matrix is partitioned such that the first m0 rows belong to
2192273d9f13SBarry Smith    process 0, the next m1 rows belong to process 1, the next m2 rows belong
2193273d9f13SBarry Smith    to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.
2194273d9f13SBarry Smith 
2195273d9f13SBarry Smith    The DIAGONAL portion of the local submatrix of a processor can be defined
2196273d9f13SBarry Smith    as the submatrix which is obtained by extraction the part corresponding
2197273d9f13SBarry Smith    to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the
2198273d9f13SBarry Smith    first row that belongs to the processor, and r2 is the last row belonging
2199273d9f13SBarry Smith    to the this processor. This is a square mxm matrix. The remaining portion
2200273d9f13SBarry Smith    of the local submatrix (mxN) constitute the OFF-DIAGONAL portion.
2201273d9f13SBarry Smith 
2202273d9f13SBarry Smith    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
2203273d9f13SBarry Smith 
2204273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible.
2205273d9f13SBarry Smith    We search for consecutive rows with the same nonzero structure, thereby
2206273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2207273d9f13SBarry Smith 
2208273d9f13SBarry Smith    Options Database Keys:
2209273d9f13SBarry Smith +  -mat_aij_no_inode  - Do not use inodes
2210273d9f13SBarry Smith .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2211273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2212273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2213273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2214273d9f13SBarry Smith 
2215273d9f13SBarry Smith    Example usage:
2216273d9f13SBarry Smith 
2217273d9f13SBarry Smith    Consider the following 8x8 matrix with 34 non-zero values, that is
2218273d9f13SBarry Smith    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
2219273d9f13SBarry Smith    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
2220273d9f13SBarry Smith    as follows:
2221273d9f13SBarry Smith 
2222273d9f13SBarry Smith .vb
2223273d9f13SBarry Smith             1  2  0  |  0  3  0  |  0  4
2224273d9f13SBarry Smith     Proc0   0  5  6  |  7  0  0  |  8  0
2225273d9f13SBarry Smith             9  0 10  | 11  0  0  | 12  0
2226273d9f13SBarry Smith     -------------------------------------
2227273d9f13SBarry Smith            13  0 14  | 15 16 17  |  0  0
2228273d9f13SBarry Smith     Proc1   0 18  0  | 19 20 21  |  0  0
2229273d9f13SBarry Smith             0  0  0  | 22 23  0  | 24  0
2230273d9f13SBarry Smith     -------------------------------------
2231273d9f13SBarry Smith     Proc2  25 26 27  |  0  0 28  | 29  0
2232273d9f13SBarry Smith            30  0  0  | 31 32 33  |  0 34
2233273d9f13SBarry Smith .ve
2234273d9f13SBarry Smith 
2235273d9f13SBarry Smith    This can be represented as a collection of submatrices as:
2236273d9f13SBarry Smith 
2237273d9f13SBarry Smith .vb
2238273d9f13SBarry Smith       A B C
2239273d9f13SBarry Smith       D E F
2240273d9f13SBarry Smith       G H I
2241273d9f13SBarry Smith .ve
2242273d9f13SBarry Smith 
2243273d9f13SBarry Smith    Where the submatrices A,B,C are owned by proc0, D,E,F are
2244273d9f13SBarry Smith    owned by proc1, G,H,I are owned by proc2.
2245273d9f13SBarry Smith 
2246273d9f13SBarry Smith    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2247273d9f13SBarry Smith    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2248273d9f13SBarry Smith    The 'M','N' parameters are 8,8, and have the same values on all procs.
2249273d9f13SBarry Smith 
2250273d9f13SBarry Smith    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
2251273d9f13SBarry Smith    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
2252273d9f13SBarry Smith    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
2253273d9f13SBarry Smith    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
2254273d9f13SBarry Smith    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
2255273d9f13SBarry Smith    matrix, ans [DF] as another SeqAIJ matrix.
2256273d9f13SBarry Smith 
2257273d9f13SBarry Smith    When d_nz, o_nz parameters are specified, d_nz storage elements are
2258273d9f13SBarry Smith    allocated for every row of the local diagonal submatrix, and o_nz
2259273d9f13SBarry Smith    storage locations are allocated for every row of the OFF-DIAGONAL submat.
2260273d9f13SBarry Smith    One way to choose d_nz and o_nz is to use the max nonzerors per local
2261273d9f13SBarry Smith    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
2262273d9f13SBarry Smith    In this case, the values of d_nz,o_nz are:
2263273d9f13SBarry Smith .vb
2264273d9f13SBarry Smith      proc0 : dnz = 2, o_nz = 2
2265273d9f13SBarry Smith      proc1 : dnz = 3, o_nz = 2
2266273d9f13SBarry Smith      proc2 : dnz = 1, o_nz = 4
2267273d9f13SBarry Smith .ve
2268273d9f13SBarry Smith    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
2269273d9f13SBarry Smith    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
2270273d9f13SBarry Smith    for proc3. i.e we are using 12+15+10=37 storage locations to store
2271273d9f13SBarry Smith    34 values.
2272273d9f13SBarry Smith 
2273273d9f13SBarry Smith    When d_nnz, o_nnz parameters are specified, the storage is specified
2274273d9f13SBarry Smith    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
2275273d9f13SBarry Smith    In the above case the values for d_nnz,o_nnz are:
2276273d9f13SBarry Smith .vb
2277273d9f13SBarry Smith      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
2278273d9f13SBarry Smith      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
2279273d9f13SBarry Smith      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
2280273d9f13SBarry Smith .ve
2281273d9f13SBarry Smith    Here the space allocated is sum of all the above values i.e 34, and
2282273d9f13SBarry Smith    hence pre-allocation is perfect.
2283273d9f13SBarry Smith 
2284273d9f13SBarry Smith    Level: intermediate
2285273d9f13SBarry Smith 
2286273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
2287273d9f13SBarry Smith 
2288273d9f13SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues()
2289273d9f13SBarry Smith @*/
2290273d9f13SBarry Smith int MatMPIAIJSetPreallocation(Mat B,int d_nz,int *d_nnz,int o_nz,int *o_nnz)
2291273d9f13SBarry Smith {
2292273d9f13SBarry Smith   Mat_MPIAIJ   *b;
2293273d9f13SBarry Smith   int          ierr,i;
2294273d9f13SBarry Smith   PetscTruth   flg2;
2295273d9f13SBarry Smith 
2296273d9f13SBarry Smith   PetscFunctionBegin;
2297273d9f13SBarry Smith   ierr = PetscTypeCompare((PetscObject)B,MATMPIAIJ,&flg2);CHKERRQ(ierr);
2298273d9f13SBarry Smith   if (!flg2) PetscFunctionReturn(0);
2299273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
2300435da068SBarry Smith   if (d_nz == PETSC_DEFAULT || d_nz == PETSC_DECIDE) d_nz = 5;
2301435da068SBarry Smith   if (o_nz == PETSC_DEFAULT || o_nz == PETSC_DECIDE) o_nz = 2;
2302435da068SBarry Smith   if (d_nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"d_nz cannot be less than 0: value %d",d_nz);
2303435da068SBarry Smith   if (o_nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"o_nz cannot be less than 0: value %d",o_nz);
2304273d9f13SBarry Smith   if (d_nnz) {
2305273d9f13SBarry Smith     for (i=0; i<B->m; i++) {
2306273d9f13SBarry Smith       if (d_nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"d_nnz cannot be less than 0: local row %d value %d",i,d_nnz[i]);
2307273d9f13SBarry Smith     }
2308273d9f13SBarry Smith   }
2309273d9f13SBarry Smith   if (o_nnz) {
2310273d9f13SBarry Smith     for (i=0; i<B->m; i++) {
2311273d9f13SBarry Smith       if (o_nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"o_nnz cannot be less than 0: local row %d value %d",i,o_nnz[i]);
2312273d9f13SBarry Smith     }
2313273d9f13SBarry Smith   }
2314273d9f13SBarry Smith   b = (Mat_MPIAIJ*)B->data;
2315273d9f13SBarry Smith 
2316273d9f13SBarry Smith   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,B->m,B->n,d_nz,d_nnz,&b->A);CHKERRQ(ierr);
2317b0a32e0cSBarry Smith   PetscLogObjectParent(B,b->A);
2318273d9f13SBarry Smith   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,B->m,B->N,o_nz,o_nnz,&b->B);CHKERRQ(ierr);
2319b0a32e0cSBarry Smith   PetscLogObjectParent(B,b->B);
2320273d9f13SBarry Smith 
2321273d9f13SBarry Smith   PetscFunctionReturn(0);
2322273d9f13SBarry Smith }
2323273d9f13SBarry Smith 
23244a2ae208SSatish Balay #undef __FUNCT__
23254a2ae208SSatish Balay #define __FUNCT__ "MatCreateMPIAIJ"
2326273d9f13SBarry Smith /*@C
2327273d9f13SBarry Smith    MatCreateMPIAIJ - Creates a sparse parallel matrix in AIJ format
2328273d9f13SBarry Smith    (the default parallel PETSc format).  For good matrix assembly performance
2329273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameters
2330273d9f13SBarry Smith    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
2331273d9f13SBarry Smith    performance can be increased by more than a factor of 50.
2332273d9f13SBarry Smith 
2333273d9f13SBarry Smith    Collective on MPI_Comm
2334273d9f13SBarry Smith 
2335273d9f13SBarry Smith    Input Parameters:
2336273d9f13SBarry Smith +  comm - MPI communicator
2337273d9f13SBarry Smith .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
2338273d9f13SBarry Smith            This value should be the same as the local size used in creating the
2339273d9f13SBarry Smith            y vector for the matrix-vector product y = Ax.
2340273d9f13SBarry Smith .  n - This value should be the same as the local size used in creating the
2341273d9f13SBarry Smith        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
2342273d9f13SBarry Smith        calculated if N is given) For square matrices n is almost always m.
2343273d9f13SBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
2344273d9f13SBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
2345273d9f13SBarry Smith .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
2346273d9f13SBarry Smith            (same value is used for all local rows)
2347273d9f13SBarry Smith .  d_nnz - array containing the number of nonzeros in the various rows of the
2348273d9f13SBarry Smith            DIAGONAL portion of the local submatrix (possibly different for each row)
2349273d9f13SBarry Smith            or PETSC_NULL, if d_nz is used to specify the nonzero structure.
2350273d9f13SBarry Smith            The size of this array is equal to the number of local rows, i.e 'm'.
2351273d9f13SBarry Smith            You must leave room for the diagonal entry even if it is zero.
2352273d9f13SBarry Smith .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
2353273d9f13SBarry Smith            submatrix (same value is used for all local rows).
2354273d9f13SBarry Smith -  o_nnz - array containing the number of nonzeros in the various rows of the
2355273d9f13SBarry Smith            OFF-DIAGONAL portion of the local submatrix (possibly different for
2356273d9f13SBarry Smith            each row) or PETSC_NULL, if o_nz is used to specify the nonzero
2357273d9f13SBarry Smith            structure. The size of this array is equal to the number
2358273d9f13SBarry Smith            of local rows, i.e 'm'.
2359273d9f13SBarry Smith 
2360273d9f13SBarry Smith    Output Parameter:
2361273d9f13SBarry Smith .  A - the matrix
2362273d9f13SBarry Smith 
2363273d9f13SBarry Smith    Notes:
2364273d9f13SBarry Smith    m,n,M,N parameters specify the size of the matrix, and its partitioning across
2365273d9f13SBarry Smith    processors, while d_nz,d_nnz,o_nz,o_nnz parameters specify the approximate
2366273d9f13SBarry Smith    storage requirements for this matrix.
2367273d9f13SBarry Smith 
2368273d9f13SBarry Smith    If PETSC_DECIDE or  PETSC_DETERMINE is used for a particular argument on one
2369273d9f13SBarry Smith    processor than it must be used on all processors that share the object for
2370273d9f13SBarry Smith    that argument.
2371273d9f13SBarry Smith 
2372273d9f13SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
2373273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2374273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2375273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users manual for details.
2376273d9f13SBarry Smith 
2377273d9f13SBarry Smith    The user MUST specify either the local or global matrix dimensions
2378273d9f13SBarry Smith    (possibly both).
2379273d9f13SBarry Smith 
2380273d9f13SBarry Smith    The parallel matrix is partitioned such that the first m0 rows belong to
2381273d9f13SBarry Smith    process 0, the next m1 rows belong to process 1, the next m2 rows belong
2382273d9f13SBarry Smith    to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.
2383273d9f13SBarry Smith 
2384273d9f13SBarry Smith    The DIAGONAL portion of the local submatrix of a processor can be defined
2385273d9f13SBarry Smith    as the submatrix which is obtained by extraction the part corresponding
2386273d9f13SBarry Smith    to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the
2387273d9f13SBarry Smith    first row that belongs to the processor, and r2 is the last row belonging
2388273d9f13SBarry Smith    to the this processor. This is a square mxm matrix. The remaining portion
2389273d9f13SBarry Smith    of the local submatrix (mxN) constitute the OFF-DIAGONAL portion.
2390273d9f13SBarry Smith 
2391273d9f13SBarry Smith    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
2392273d9f13SBarry Smith 
2393273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible.
2394273d9f13SBarry Smith    We search for consecutive rows with the same nonzero structure, thereby
2395273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2396273d9f13SBarry Smith 
2397273d9f13SBarry Smith    Options Database Keys:
2398273d9f13SBarry Smith +  -mat_aij_no_inode  - Do not use inodes
2399273d9f13SBarry Smith .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2400273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2401273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2402273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2403273d9f13SBarry Smith 
2404273d9f13SBarry Smith 
2405273d9f13SBarry Smith    Example usage:
2406273d9f13SBarry Smith 
2407273d9f13SBarry Smith    Consider the following 8x8 matrix with 34 non-zero values, that is
2408273d9f13SBarry Smith    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
2409273d9f13SBarry Smith    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
2410273d9f13SBarry Smith    as follows:
2411273d9f13SBarry Smith 
2412273d9f13SBarry Smith .vb
2413273d9f13SBarry Smith             1  2  0  |  0  3  0  |  0  4
2414273d9f13SBarry Smith     Proc0   0  5  6  |  7  0  0  |  8  0
2415273d9f13SBarry Smith             9  0 10  | 11  0  0  | 12  0
2416273d9f13SBarry Smith     -------------------------------------
2417273d9f13SBarry Smith            13  0 14  | 15 16 17  |  0  0
2418273d9f13SBarry Smith     Proc1   0 18  0  | 19 20 21  |  0  0
2419273d9f13SBarry Smith             0  0  0  | 22 23  0  | 24  0
2420273d9f13SBarry Smith     -------------------------------------
2421273d9f13SBarry Smith     Proc2  25 26 27  |  0  0 28  | 29  0
2422273d9f13SBarry Smith            30  0  0  | 31 32 33  |  0 34
2423273d9f13SBarry Smith .ve
2424273d9f13SBarry Smith 
2425273d9f13SBarry Smith    This can be represented as a collection of submatrices as:
2426273d9f13SBarry Smith 
2427273d9f13SBarry Smith .vb
2428273d9f13SBarry Smith       A B C
2429273d9f13SBarry Smith       D E F
2430273d9f13SBarry Smith       G H I
2431273d9f13SBarry Smith .ve
2432273d9f13SBarry Smith 
2433273d9f13SBarry Smith    Where the submatrices A,B,C are owned by proc0, D,E,F are
2434273d9f13SBarry Smith    owned by proc1, G,H,I are owned by proc2.
2435273d9f13SBarry Smith 
2436273d9f13SBarry Smith    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2437273d9f13SBarry Smith    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2438273d9f13SBarry Smith    The 'M','N' parameters are 8,8, and have the same values on all procs.
2439273d9f13SBarry Smith 
2440273d9f13SBarry Smith    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
2441273d9f13SBarry Smith    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
2442273d9f13SBarry Smith    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
2443273d9f13SBarry Smith    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
2444273d9f13SBarry Smith    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
2445273d9f13SBarry Smith    matrix, ans [DF] as another SeqAIJ matrix.
2446273d9f13SBarry Smith 
2447273d9f13SBarry Smith    When d_nz, o_nz parameters are specified, d_nz storage elements are
2448273d9f13SBarry Smith    allocated for every row of the local diagonal submatrix, and o_nz
2449273d9f13SBarry Smith    storage locations are allocated for every row of the OFF-DIAGONAL submat.
2450273d9f13SBarry Smith    One way to choose d_nz and o_nz is to use the max nonzerors per local
2451273d9f13SBarry Smith    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
2452273d9f13SBarry Smith    In this case, the values of d_nz,o_nz are:
2453273d9f13SBarry Smith .vb
2454273d9f13SBarry Smith      proc0 : dnz = 2, o_nz = 2
2455273d9f13SBarry Smith      proc1 : dnz = 3, o_nz = 2
2456273d9f13SBarry Smith      proc2 : dnz = 1, o_nz = 4
2457273d9f13SBarry Smith .ve
2458273d9f13SBarry Smith    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
2459273d9f13SBarry Smith    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
2460273d9f13SBarry Smith    for proc3. i.e we are using 12+15+10=37 storage locations to store
2461273d9f13SBarry Smith    34 values.
2462273d9f13SBarry Smith 
2463273d9f13SBarry Smith    When d_nnz, o_nnz parameters are specified, the storage is specified
2464273d9f13SBarry Smith    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
2465273d9f13SBarry Smith    In the above case the values for d_nnz,o_nnz are:
2466273d9f13SBarry Smith .vb
2467273d9f13SBarry Smith      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
2468273d9f13SBarry Smith      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
2469273d9f13SBarry Smith      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
2470273d9f13SBarry Smith .ve
2471273d9f13SBarry Smith    Here the space allocated is sum of all the above values i.e 34, and
2472273d9f13SBarry Smith    hence pre-allocation is perfect.
2473273d9f13SBarry Smith 
2474273d9f13SBarry Smith    Level: intermediate
2475273d9f13SBarry Smith 
2476273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
2477273d9f13SBarry Smith 
2478273d9f13SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues()
2479273d9f13SBarry Smith @*/
2480273d9f13SBarry Smith int MatCreateMPIAIJ(MPI_Comm comm,int m,int n,int M,int N,int d_nz,int *d_nnz,int o_nz,int *o_nnz,Mat *A)
2481273d9f13SBarry Smith {
2482273d9f13SBarry Smith   int ierr,size;
2483273d9f13SBarry Smith 
2484273d9f13SBarry Smith   PetscFunctionBegin;
2485273d9f13SBarry Smith   ierr = MatCreate(comm,m,n,M,N,A);CHKERRQ(ierr);
2486273d9f13SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
2487273d9f13SBarry Smith   if (size > 1) {
2488273d9f13SBarry Smith     ierr = MatSetType(*A,MATMPIAIJ);CHKERRQ(ierr);
2489273d9f13SBarry Smith     ierr = MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr);
2490273d9f13SBarry Smith   } else {
2491273d9f13SBarry Smith     ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2492273d9f13SBarry Smith     ierr = MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);CHKERRQ(ierr);
2493273d9f13SBarry Smith   }
2494273d9f13SBarry Smith   PetscFunctionReturn(0);
2495273d9f13SBarry Smith }
2496195d93cdSBarry Smith 
24974a2ae208SSatish Balay #undef __FUNCT__
24984a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJGetSeqAIJ"
2499195d93cdSBarry Smith int MatMPIAIJGetSeqAIJ(Mat A,Mat *Ad,Mat *Ao,int **colmap)
2500195d93cdSBarry Smith {
2501195d93cdSBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;
2502195d93cdSBarry Smith   PetscFunctionBegin;
2503195d93cdSBarry Smith   *Ad     = a->A;
2504195d93cdSBarry Smith   *Ao     = a->B;
2505195d93cdSBarry Smith   *colmap = a->garray;
2506195d93cdSBarry Smith   PetscFunctionReturn(0);
2507195d93cdSBarry Smith }
2508a2243be0SBarry Smith 
2509a2243be0SBarry Smith #undef __FUNCT__
2510a2243be0SBarry Smith #define __FUNCT__ "MatSetColoring_MPIAIJ"
2511a2243be0SBarry Smith int MatSetColoring_MPIAIJ(Mat A,ISColoring coloring)
2512a2243be0SBarry Smith {
251308b6dcc0SBarry Smith   int        ierr,i;
2514a2243be0SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2515a2243be0SBarry Smith 
2516a2243be0SBarry Smith   PetscFunctionBegin;
2517a2243be0SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
251808b6dcc0SBarry Smith     ISColoringValue *allcolors,*colors;
2519a2243be0SBarry Smith     ISColoring      ocoloring;
2520a2243be0SBarry Smith 
2521a2243be0SBarry Smith     /* set coloring for diagonal portion */
2522a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->A,coloring);CHKERRQ(ierr);
2523a2243be0SBarry Smith 
2524a2243be0SBarry Smith     /* set coloring for off-diagonal portion */
252508b6dcc0SBarry Smith     ierr = ISAllGatherColors(A->comm,coloring->n,coloring->colors,PETSC_NULL,&allcolors);CHKERRQ(ierr);
252608b6dcc0SBarry Smith     ierr = PetscMalloc((a->B->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
2527a2243be0SBarry Smith     for (i=0; i<a->B->n; i++) {
2528a2243be0SBarry Smith       colors[i] = allcolors[a->garray[i]];
2529a2243be0SBarry Smith     }
2530a2243be0SBarry Smith     ierr = PetscFree(allcolors);CHKERRQ(ierr);
2531a2243be0SBarry Smith     ierr = ISColoringCreate(MPI_COMM_SELF,a->B->n,colors,&ocoloring);CHKERRQ(ierr);
2532a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr);
2533a2243be0SBarry Smith     ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr);
2534a2243be0SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
253508b6dcc0SBarry Smith     ISColoringValue *colors;
253608b6dcc0SBarry Smith     int             *larray;
2537a2243be0SBarry Smith     ISColoring      ocoloring;
2538a2243be0SBarry Smith 
2539a2243be0SBarry Smith     /* set coloring for diagonal portion */
2540a2243be0SBarry Smith     ierr = PetscMalloc((a->A->n+1)*sizeof(int),&larray);CHKERRQ(ierr);
2541a2243be0SBarry Smith     for (i=0; i<a->A->n; i++) {
2542a2243be0SBarry Smith       larray[i] = i + a->cstart;
2543a2243be0SBarry Smith     }
2544a2243be0SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->A->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
254508b6dcc0SBarry Smith     ierr = PetscMalloc((a->A->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
2546a2243be0SBarry Smith     for (i=0; i<a->A->n; i++) {
2547a2243be0SBarry Smith       colors[i] = coloring->colors[larray[i]];
2548a2243be0SBarry Smith     }
2549a2243be0SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
255012c595b3SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,a->A->n,colors,&ocoloring);CHKERRQ(ierr);
2551a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->A,ocoloring);CHKERRQ(ierr);
2552a2243be0SBarry Smith     ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr);
2553a2243be0SBarry Smith 
2554a2243be0SBarry Smith     /* set coloring for off-diagonal portion */
2555a2243be0SBarry Smith     ierr = PetscMalloc((a->B->n+1)*sizeof(int),&larray);CHKERRQ(ierr);
2556a2243be0SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->B->n,a->garray,PETSC_NULL,larray);CHKERRQ(ierr);
255708b6dcc0SBarry Smith     ierr = PetscMalloc((a->B->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
2558a2243be0SBarry Smith     for (i=0; i<a->B->n; i++) {
2559a2243be0SBarry Smith       colors[i] = coloring->colors[larray[i]];
2560a2243be0SBarry Smith     }
2561a2243be0SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
2562a2243be0SBarry Smith     ierr = ISColoringCreate(MPI_COMM_SELF,a->B->n,colors,&ocoloring);CHKERRQ(ierr);
2563a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr);
2564a2243be0SBarry Smith     ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr);
2565a2243be0SBarry Smith   } else {
2566a2243be0SBarry Smith     SETERRQ1(1,"No support ISColoringType %d",coloring->ctype);
2567a2243be0SBarry Smith   }
2568a2243be0SBarry Smith 
2569a2243be0SBarry Smith   PetscFunctionReturn(0);
2570a2243be0SBarry Smith }
2571a2243be0SBarry Smith 
2572a2243be0SBarry Smith #undef __FUNCT__
2573779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdic_MPIAIJ"
2574779c1a83SBarry Smith int MatSetValuesAdic_MPIAIJ(Mat A,void *advalues)
2575a2243be0SBarry Smith {
2576a2243be0SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2577a2243be0SBarry Smith   int        ierr;
2578a2243be0SBarry Smith 
2579a2243be0SBarry Smith   PetscFunctionBegin;
2580779c1a83SBarry Smith   ierr = MatSetValuesAdic_SeqAIJ(a->A,advalues);CHKERRQ(ierr);
2581779c1a83SBarry Smith   ierr = MatSetValuesAdic_SeqAIJ(a->B,advalues);CHKERRQ(ierr);
2582779c1a83SBarry Smith   PetscFunctionReturn(0);
2583779c1a83SBarry Smith }
2584779c1a83SBarry Smith 
2585779c1a83SBarry Smith #undef __FUNCT__
2586779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdifor_MPIAIJ"
2587779c1a83SBarry Smith int MatSetValuesAdifor_MPIAIJ(Mat A,int nl,void *advalues)
2588779c1a83SBarry Smith {
2589779c1a83SBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2590779c1a83SBarry Smith   int        ierr;
2591779c1a83SBarry Smith 
2592779c1a83SBarry Smith   PetscFunctionBegin;
2593779c1a83SBarry Smith   ierr = MatSetValuesAdifor_SeqAIJ(a->A,nl,advalues);CHKERRQ(ierr);
2594779c1a83SBarry Smith   ierr = MatSetValuesAdifor_SeqAIJ(a->B,nl,advalues);CHKERRQ(ierr);
2595a2243be0SBarry Smith   PetscFunctionReturn(0);
2596a2243be0SBarry Smith }
2597c5d6d63eSBarry Smith 
2598c5d6d63eSBarry Smith #undef __FUNCT__
259951dd7536SBarry Smith #define __FUNCT__ "MatMerge"
2600c5d6d63eSBarry Smith /*@C
260151dd7536SBarry Smith       MatMerge - Creates a single large PETSc matrix by concatinating sequential
260251dd7536SBarry Smith                  matrices from each processor
2603c5d6d63eSBarry Smith 
2604c5d6d63eSBarry Smith     Collective on MPI_Comm
2605c5d6d63eSBarry Smith 
2606c5d6d63eSBarry Smith    Input Parameters:
260751dd7536SBarry Smith +    comm - the communicators the parallel matrix will live on
260851dd7536SBarry Smith -    inmat - the input sequential matrices
260951dd7536SBarry Smith 
261051dd7536SBarry Smith    Output Parameter:
261151dd7536SBarry Smith .    outmat - the parallel matrix generated
2612c5d6d63eSBarry Smith 
26137e25d530SSatish Balay     Level: advanced
26147e25d530SSatish Balay 
2615c5d6d63eSBarry Smith    Notes: The number of columns of the matrix in EACH of the seperate files
2616c5d6d63eSBarry Smith       MUST be the same.
2617c5d6d63eSBarry Smith 
2618c5d6d63eSBarry Smith @*/
261951dd7536SBarry Smith int MatMerge(MPI_Comm comm,Mat inmat, Mat *outmat)
2620c5d6d63eSBarry Smith {
2621a15ac5e4SHong Zhang   int         ierr,m,n,i,rstart,*indx,nnz,I,*dnz,*onz;
2622c5d6d63eSBarry Smith   PetscScalar *values;
262351dd7536SBarry Smith   PetscMap    columnmap,rowmap;
2624c5d6d63eSBarry Smith 
2625c5d6d63eSBarry Smith   PetscFunctionBegin;
2626c5d6d63eSBarry Smith 
262751dd7536SBarry Smith   ierr = MatGetSize(inmat,&m,&n);CHKERRQ(ierr);
262851dd7536SBarry Smith 
262951dd7536SBarry Smith   /* count nonzeros in each row, for diagonal and off diagonal portion of matrix */
263051dd7536SBarry Smith   ierr = PetscMapCreate(comm,&columnmap);CHKERRQ(ierr);
263151dd7536SBarry Smith   ierr = PetscMapSetSize(columnmap,n);CHKERRQ(ierr);
263251dd7536SBarry Smith   ierr = PetscMapSetType(columnmap,MAP_MPI);CHKERRQ(ierr);
263351dd7536SBarry Smith   ierr = PetscMapGetLocalSize(columnmap,&n);CHKERRQ(ierr);
263451dd7536SBarry Smith   ierr = PetscMapDestroy(columnmap);CHKERRQ(ierr);
263551dd7536SBarry Smith 
263651dd7536SBarry Smith   ierr = PetscMapCreate(comm,&rowmap);CHKERRQ(ierr);
263751dd7536SBarry Smith   ierr = PetscMapSetLocalSize(rowmap,m);CHKERRQ(ierr);
263851dd7536SBarry Smith   ierr = PetscMapSetType(rowmap,MAP_MPI);CHKERRQ(ierr);
263951dd7536SBarry Smith   ierr = PetscMapGetLocalRange(rowmap,&rstart,0);CHKERRQ(ierr);
264051dd7536SBarry Smith   ierr = PetscMapDestroy(rowmap);CHKERRQ(ierr);
264151dd7536SBarry Smith 
264251dd7536SBarry Smith   ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr);
2643c5d6d63eSBarry Smith   for (i=0;i<m;i++) {
264451dd7536SBarry Smith     ierr = MatGetRow(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
264551dd7536SBarry Smith     ierr = MatPreallocateSet(i+rstart,nnz,indx,dnz,onz);CHKERRQ(ierr);
264651dd7536SBarry Smith     ierr = MatRestoreRow(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
2647c5d6d63eSBarry Smith   }
264851dd7536SBarry Smith   ierr = MatCreateMPIAIJ(comm,m,n,PETSC_DETERMINE,PETSC_DETERMINE,0,dnz,0,onz,outmat);CHKERRQ(ierr);
264951dd7536SBarry Smith   ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr);
2650c5d6d63eSBarry Smith 
265151dd7536SBarry Smith   for (i=0;i<m;i++) {
265251dd7536SBarry Smith     ierr = MatGetRow(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
265351dd7536SBarry Smith     I    = i + rstart;
265451dd7536SBarry Smith     ierr = MatSetValues(*outmat,1,&I,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr);
265551dd7536SBarry Smith     ierr = MatRestoreRow(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
265651dd7536SBarry Smith   }
265751dd7536SBarry Smith   ierr = MatDestroy(inmat);CHKERRQ(ierr);
265851dd7536SBarry Smith   ierr = MatAssemblyBegin(*outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
265951dd7536SBarry Smith   ierr = MatAssemblyEnd(*outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
266051dd7536SBarry Smith 
2661c5d6d63eSBarry Smith   PetscFunctionReturn(0);
2662c5d6d63eSBarry Smith }
2663c5d6d63eSBarry Smith 
2664c5d6d63eSBarry Smith #undef __FUNCT__
2665c5d6d63eSBarry Smith #define __FUNCT__ "MatFileSplit"
2666c5d6d63eSBarry Smith int MatFileSplit(Mat A,char *outfile)
2667c5d6d63eSBarry Smith {
2668c5d6d63eSBarry Smith   int         ierr,rank,len,m,N,i,rstart,*indx,nnz;
2669c5d6d63eSBarry Smith   PetscViewer out;
2670c5d6d63eSBarry Smith   char        *name;
2671c5d6d63eSBarry Smith   Mat         B;
2672c5d6d63eSBarry Smith   PetscScalar *values;
2673c5d6d63eSBarry Smith 
2674c5d6d63eSBarry Smith   PetscFunctionBegin;
2675c5d6d63eSBarry Smith 
2676c5d6d63eSBarry Smith   ierr = MatGetLocalSize(A,&m,0);CHKERRQ(ierr);
2677c5d6d63eSBarry Smith   ierr = MatGetSize(A,0,&N);CHKERRQ(ierr);
2678c5d6d63eSBarry Smith   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,m,N,0,0,&B);CHKERRQ(ierr);
2679c5d6d63eSBarry Smith   ierr = MatGetOwnershipRange(A,&rstart,0);CHKERRQ(ierr);
2680c5d6d63eSBarry Smith   for (i=0;i<m;i++) {
2681c5d6d63eSBarry Smith     ierr = MatGetRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr);
2682c5d6d63eSBarry Smith     ierr = MatSetValues(B,1,&i,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr);
2683c5d6d63eSBarry Smith     ierr = MatRestoreRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr);
2684c5d6d63eSBarry Smith   }
2685c5d6d63eSBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2686c5d6d63eSBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2687c5d6d63eSBarry Smith 
2688c5d6d63eSBarry Smith   ierr = MPI_Comm_rank(A->comm,&rank);CHKERRQ(ierr);
2689c5d6d63eSBarry Smith   ierr = PetscStrlen(outfile,&len);CHKERRQ(ierr);
2690c5d6d63eSBarry Smith   ierr = PetscMalloc((len+5)*sizeof(char),&name);CHKERRQ(ierr);
2691c5d6d63eSBarry Smith   sprintf(name,"%s.%d",outfile,rank);
2692c5d6d63eSBarry Smith   ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,name,PETSC_BINARY_CREATE,&out);CHKERRQ(ierr);
2693c5d6d63eSBarry Smith   ierr = PetscFree(name);
2694c5d6d63eSBarry Smith   ierr = MatView(B,out);CHKERRQ(ierr);
2695c5d6d63eSBarry Smith   ierr = PetscViewerDestroy(out);CHKERRQ(ierr);
2696c5d6d63eSBarry Smith   ierr = MatDestroy(B);CHKERRQ(ierr);
2697c5d6d63eSBarry Smith   PetscFunctionReturn(0);
2698c5d6d63eSBarry Smith }
2699