xref: /petsc/src/mat/impls/aij/mpi/mpiaij.c (revision 103bf8bdf12c48661751db1a129e5b517eb1c07d)
1be1d678aSKris Buschelman #define PETSCMAT_DLL
28a729477SBarry Smith 
3b47fd4b1SSatish Balay #include "src/mat/impls/aij/mpi/mpiaij.h"   /*I "petscmat.h" I*/
4d9942c19SSatish Balay #include "src/inline/spops.h"
58a729477SBarry Smith 
60f5bd95cSBarry Smith /*
70f5bd95cSBarry Smith   Local utility routine that creates a mapping from the global column
89e25ed09SBarry Smith number to the local number in the off-diagonal part of the local
90f5bd95cSBarry Smith storage of the matrix.  When PETSC_USE_CTABLE is used this is scalable at
100f5bd95cSBarry Smith a slightly higher hash table cost; without it it is not scalable (each processor
110f5bd95cSBarry Smith has an order N integer array but is fast to acess.
129e25ed09SBarry Smith */
134a2ae208SSatish Balay #undef __FUNCT__
144a2ae208SSatish Balay #define __FUNCT__ "CreateColmap_MPIAIJ_Private"
15dfbe8321SBarry Smith PetscErrorCode CreateColmap_MPIAIJ_Private(Mat mat)
169e25ed09SBarry Smith {
1744a69424SLois Curfman McInnes   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
186849ba73SBarry Smith   PetscErrorCode ierr;
19899cda47SBarry Smith   PetscInt       n = aij->B->cmap.n,i;
20dbb450caSBarry Smith 
213a40ed3dSBarry Smith   PetscFunctionBegin;
22aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
23273d9f13SBarry Smith   ierr = PetscTableCreate(n,&aij->colmap);CHKERRQ(ierr);
24b1fc9764SSatish Balay   for (i=0; i<n; i++){
250f5bd95cSBarry Smith     ierr = PetscTableAdd(aij->colmap,aij->garray[i]+1,i+1);CHKERRQ(ierr);
26b1fc9764SSatish Balay   }
27b1fc9764SSatish Balay #else
28899cda47SBarry Smith   ierr = PetscMalloc((mat->cmap.N+1)*sizeof(PetscInt),&aij->colmap);CHKERRQ(ierr);
29899cda47SBarry Smith   ierr = PetscLogObjectMemory(mat,mat->cmap.N*sizeof(PetscInt));CHKERRQ(ierr);
30899cda47SBarry Smith   ierr = PetscMemzero(aij->colmap,mat->cmap.N*sizeof(PetscInt));CHKERRQ(ierr);
31905e6a2fSBarry Smith   for (i=0; i<n; i++) aij->colmap[aij->garray[i]] = i+1;
32b1fc9764SSatish Balay #endif
333a40ed3dSBarry Smith   PetscFunctionReturn(0);
349e25ed09SBarry Smith }
359e25ed09SBarry Smith 
36085a36d4SBarry Smith 
370520107fSSatish Balay #define CHUNKSIZE   15
3830770e4dSSatish Balay #define MatSetValues_SeqAIJ_A_Private(row,col,value,addv) \
390520107fSSatish Balay { \
407cd84e04SBarry Smith     if (col <= lastcol1) low1 = 0; else high1 = nrow1; \
41fd3458f5SBarry Smith     lastcol1 = col;\
42fd3458f5SBarry Smith     while (high1-low1 > 5) { \
43fd3458f5SBarry Smith       t = (low1+high1)/2; \
44fd3458f5SBarry Smith       if (rp1[t] > col) high1 = t; \
45fd3458f5SBarry Smith       else             low1  = t; \
46ba4e3ef2SSatish Balay     } \
47fd3458f5SBarry Smith       for (_i=low1; _i<high1; _i++) { \
48fd3458f5SBarry Smith         if (rp1[_i] > col) break; \
49fd3458f5SBarry Smith         if (rp1[_i] == col) { \
50fd3458f5SBarry Smith           if (addv == ADD_VALUES) ap1[_i] += value;   \
51fd3458f5SBarry Smith           else                    ap1[_i] = value; \
5230770e4dSSatish Balay           goto a_noinsert; \
530520107fSSatish Balay         } \
540520107fSSatish Balay       }  \
55abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries) goto a_noinsert; \
5689280ab3SLois Curfman McInnes       if (nonew == 1) goto a_noinsert; \
57085a36d4SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \
58e6b907acSBarry Smith       MatSeqXAIJReallocateAIJ(A,am,1,nrow1,row,col,rmax1,aa,ai,aj,rp1,ap1,aimax,nonew); \
59669a8dbcSSatish Balay       N = nrow1++ - 1; a->nz++; high1++; \
600520107fSSatish Balay       /* shift up all the later entries in this row */ \
610520107fSSatish Balay       for (ii=N; ii>=_i; ii--) { \
62fd3458f5SBarry Smith         rp1[ii+1] = rp1[ii]; \
63fd3458f5SBarry Smith         ap1[ii+1] = ap1[ii]; \
640520107fSSatish Balay       } \
65fd3458f5SBarry Smith       rp1[_i] = col;  \
66fd3458f5SBarry Smith       ap1[_i] = value;  \
6730770e4dSSatish Balay       a_noinsert: ; \
68fd3458f5SBarry Smith       ailen[row] = nrow1; \
690520107fSSatish Balay }
700a198c4cSBarry Smith 
71085a36d4SBarry Smith 
7230770e4dSSatish Balay #define MatSetValues_SeqAIJ_B_Private(row,col,value,addv) \
7330770e4dSSatish Balay { \
747cd84e04SBarry Smith     if (col <= lastcol2) low2 = 0; else high2 = nrow2; \
75fd3458f5SBarry Smith     lastcol2 = col;\
76fd3458f5SBarry Smith     while (high2-low2 > 5) { \
77fd3458f5SBarry Smith       t = (low2+high2)/2; \
78fd3458f5SBarry Smith       if (rp2[t] > col) high2 = t; \
79fd3458f5SBarry Smith       else             low2  = t; \
80ba4e3ef2SSatish Balay     } \
81fd3458f5SBarry Smith        for (_i=low2; _i<high2; _i++) { \
82fd3458f5SBarry Smith         if (rp2[_i] > col) break; \
83fd3458f5SBarry Smith         if (rp2[_i] == col) { \
84fd3458f5SBarry Smith           if (addv == ADD_VALUES) ap2[_i] += value;   \
85fd3458f5SBarry Smith           else                    ap2[_i] = value; \
8630770e4dSSatish Balay           goto b_noinsert; \
8730770e4dSSatish Balay         } \
8830770e4dSSatish Balay       }  \
89abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries) goto b_noinsert; \
9089280ab3SLois Curfman McInnes       if (nonew == 1) goto b_noinsert; \
91085a36d4SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \
92e6b907acSBarry Smith       MatSeqXAIJReallocateAIJ(B,bm,1,nrow2,row,col,rmax2,ba,bi,bj,rp2,ap2,bimax,nonew); \
93669a8dbcSSatish Balay       N = nrow2++ - 1; b->nz++; high2++;\
9430770e4dSSatish Balay       /* shift up all the later entries in this row */ \
9530770e4dSSatish Balay       for (ii=N; ii>=_i; ii--) { \
96fd3458f5SBarry Smith         rp2[ii+1] = rp2[ii]; \
97fd3458f5SBarry Smith         ap2[ii+1] = ap2[ii]; \
9830770e4dSSatish Balay       } \
99fd3458f5SBarry Smith       rp2[_i] = col;  \
100fd3458f5SBarry Smith       ap2[_i] = value;  \
10130770e4dSSatish Balay       b_noinsert: ; \
102fd3458f5SBarry Smith       bilen[row] = nrow2; \
10330770e4dSSatish Balay }
10430770e4dSSatish Balay 
1054a2ae208SSatish Balay #undef __FUNCT__
1062fd7e33dSBarry Smith #define __FUNCT__ "MatSetValuesRow_MPIAIJ"
1072fd7e33dSBarry Smith PetscErrorCode MatSetValuesRow_MPIAIJ(Mat A,PetscInt row,const PetscScalar v[])
1082fd7e33dSBarry Smith {
1092fd7e33dSBarry Smith   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)A->data;
1102fd7e33dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)mat->A->data,*b = (Mat_SeqAIJ*)mat->B->data;
1112fd7e33dSBarry Smith   PetscErrorCode ierr;
1122fd7e33dSBarry Smith   PetscInt       l,*garray = mat->garray,diag;
1132fd7e33dSBarry Smith 
1142fd7e33dSBarry Smith   PetscFunctionBegin;
1152fd7e33dSBarry Smith   /* code only works for square matrices A */
1162fd7e33dSBarry Smith 
1172fd7e33dSBarry Smith   /* find size of row to the left of the diagonal part */
1182fd7e33dSBarry Smith   ierr = MatGetOwnershipRange(A,&diag,0);CHKERRQ(ierr);
1192fd7e33dSBarry Smith   row  = row - diag;
1202fd7e33dSBarry Smith   for (l=0; l<b->i[row+1]-b->i[row]; l++) {
1212fd7e33dSBarry Smith     if (garray[b->j[b->i[row]+l]] > diag) break;
1222fd7e33dSBarry Smith   }
1232fd7e33dSBarry Smith   ierr = PetscMemcpy(b->a+b->i[row],v,l*sizeof(PetscScalar));CHKERRQ(ierr);
1242fd7e33dSBarry Smith 
1252fd7e33dSBarry Smith   /* diagonal part */
1262fd7e33dSBarry Smith   ierr = PetscMemcpy(a->a+a->i[row],v+l,(a->i[row+1]-a->i[row])*sizeof(PetscScalar));CHKERRQ(ierr);
1272fd7e33dSBarry Smith 
1282fd7e33dSBarry Smith   /* right of diagonal part */
1292fd7e33dSBarry Smith   ierr = PetscMemcpy(b->a+b->i[row]+l,v+l+a->i[row+1]-a->i[row],(b->i[row+1]-b->i[row]-l)*sizeof(PetscScalar));CHKERRQ(ierr);
1302fd7e33dSBarry Smith   PetscFunctionReturn(0);
1312fd7e33dSBarry Smith }
1322fd7e33dSBarry Smith 
1332fd7e33dSBarry Smith #undef __FUNCT__
1344a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_MPIAIJ"
135b1d57f15SBarry Smith PetscErrorCode MatSetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
1368a729477SBarry Smith {
13744a69424SLois Curfman McInnes   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
13887828ca2SBarry Smith   PetscScalar    value;
139dfbe8321SBarry Smith   PetscErrorCode ierr;
140899cda47SBarry Smith   PetscInt       i,j,rstart = mat->rmap.rstart,rend = mat->rmap.rend;
141899cda47SBarry Smith   PetscInt       cstart = mat->cmap.rstart,cend = mat->cmap.rend,row,col;
142273d9f13SBarry Smith   PetscTruth     roworiented = aij->roworiented;
1438a729477SBarry Smith 
1440520107fSSatish Balay   /* Some Variables required in the macro */
1454ee7247eSSatish Balay   Mat            A = aij->A;
1464ee7247eSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
14757809a77SBarry Smith   PetscInt       *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j;
14887828ca2SBarry Smith   PetscScalar    *aa = a->a;
149329f5518SBarry Smith   PetscTruth     ignorezeroentries = (((a->ignorezeroentries)&&(addv==ADD_VALUES))?PETSC_TRUE:PETSC_FALSE);
15030770e4dSSatish Balay   Mat            B = aij->B;
15130770e4dSSatish Balay   Mat_SeqAIJ     *b = (Mat_SeqAIJ*)B->data;
152899cda47SBarry Smith   PetscInt       *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap.n,am = aij->A->rmap.n;
15387828ca2SBarry Smith   PetscScalar    *ba = b->a;
15430770e4dSSatish Balay 
155fd3458f5SBarry Smith   PetscInt       *rp1,*rp2,ii,nrow1,nrow2,_i,rmax1,rmax2,N,low1,high1,low2,high2,t,lastcol1,lastcol2;
156fd3458f5SBarry Smith   PetscInt       nonew = a->nonew;
157fd3458f5SBarry Smith   PetscScalar    *ap1,*ap2;
1584ee7247eSSatish Balay 
1593a40ed3dSBarry Smith   PetscFunctionBegin;
1608a729477SBarry Smith   for (i=0; i<m; i++) {
1615ef9f2a5SBarry Smith     if (im[i] < 0) continue;
1622515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
163899cda47SBarry Smith     if (im[i] >= mat->rmap.N) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",im[i],mat->rmap.N-1);
1640a198c4cSBarry Smith #endif
1654b0e389bSBarry Smith     if (im[i] >= rstart && im[i] < rend) {
1664b0e389bSBarry Smith       row      = im[i] - rstart;
167fd3458f5SBarry Smith       lastcol1 = -1;
168fd3458f5SBarry Smith       rp1      = aj + ai[row];
169fd3458f5SBarry Smith       ap1      = aa + ai[row];
170fd3458f5SBarry Smith       rmax1    = aimax[row];
171fd3458f5SBarry Smith       nrow1    = ailen[row];
172fd3458f5SBarry Smith       low1     = 0;
173fd3458f5SBarry Smith       high1    = nrow1;
174fd3458f5SBarry Smith       lastcol2 = -1;
175fd3458f5SBarry Smith       rp2      = bj + bi[row];
176d498b1e9SBarry Smith       ap2      = ba + bi[row];
177fd3458f5SBarry Smith       rmax2    = bimax[row];
178d498b1e9SBarry Smith       nrow2    = bilen[row];
179fd3458f5SBarry Smith       low2     = 0;
180fd3458f5SBarry Smith       high2    = nrow2;
181fd3458f5SBarry Smith 
1821eb62cbbSBarry Smith       for (j=0; j<n; j++) {
1834b0e389bSBarry Smith         if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
184abc0a331SBarry Smith         if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES)) continue;
185fd3458f5SBarry Smith         if (in[j] >= cstart && in[j] < cend){
186fd3458f5SBarry Smith           col = in[j] - cstart;
18730770e4dSSatish Balay           MatSetValues_SeqAIJ_A_Private(row,col,value,addv);
188273d9f13SBarry Smith         } else if (in[j] < 0) continue;
1892515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
190899cda47SBarry Smith         else if (in[j] >= mat->cmap.N) {SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[j],mat->cmap.N-1);}
1910a198c4cSBarry Smith #endif
1921eb62cbbSBarry Smith         else {
193227d817aSBarry Smith           if (mat->was_assembled) {
194905e6a2fSBarry Smith             if (!aij->colmap) {
195905e6a2fSBarry Smith               ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
196905e6a2fSBarry Smith             }
197aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
1980f5bd95cSBarry Smith             ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr);
199fa46199cSSatish Balay 	    col--;
200b1fc9764SSatish Balay #else
201905e6a2fSBarry Smith             col = aij->colmap[in[j]] - 1;
202b1fc9764SSatish Balay #endif
203ec8511deSBarry Smith             if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
2042493cbb0SBarry Smith               ierr = DisAssemble_MPIAIJ(mat);CHKERRQ(ierr);
2054b0e389bSBarry Smith               col =  in[j];
2069bf004c3SSatish Balay               /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
207f9508a3cSSatish Balay               B = aij->B;
208f9508a3cSSatish Balay               b = (Mat_SeqAIJ*)B->data;
209f9508a3cSSatish Balay               bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j;
210d498b1e9SBarry Smith               rp2      = bj + bi[row];
211d498b1e9SBarry Smith               ap2      = ba + bi[row];
212d498b1e9SBarry Smith               rmax2    = bimax[row];
213d498b1e9SBarry Smith               nrow2    = bilen[row];
214d498b1e9SBarry Smith               low2     = 0;
215d498b1e9SBarry Smith               high2    = nrow2;
216899cda47SBarry Smith               bm       = aij->B->rmap.n;
217f9508a3cSSatish Balay               ba = b->a;
218d6dfbf8fSBarry Smith             }
219c48de900SBarry Smith           } else col = in[j];
22030770e4dSSatish Balay           MatSetValues_SeqAIJ_B_Private(row,col,value,addv);
2211eb62cbbSBarry Smith         }
2221eb62cbbSBarry Smith       }
2235ef9f2a5SBarry Smith     } else {
22490f02eecSBarry Smith       if (!aij->donotstash) {
225d36fbae8SSatish Balay         if (roworiented) {
2265b8514ebSBarry Smith           if (ignorezeroentries && v[i*n] == 0.0) continue;
2278798bf22SSatish Balay           ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n);CHKERRQ(ierr);
228d36fbae8SSatish Balay         } else {
2295b8514ebSBarry Smith           if (ignorezeroentries && v[i] == 0.0) continue;
2308798bf22SSatish Balay           ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m);CHKERRQ(ierr);
2314b0e389bSBarry Smith         }
2321eb62cbbSBarry Smith       }
2338a729477SBarry Smith     }
23490f02eecSBarry Smith   }
2353a40ed3dSBarry Smith   PetscFunctionReturn(0);
2368a729477SBarry Smith }
2378a729477SBarry Smith 
238085a36d4SBarry Smith 
2394a2ae208SSatish Balay #undef __FUNCT__
2404a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_MPIAIJ"
241b1d57f15SBarry Smith PetscErrorCode MatGetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[])
242b49de8d1SLois Curfman McInnes {
243b49de8d1SLois Curfman McInnes   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
244dfbe8321SBarry Smith   PetscErrorCode ierr;
245899cda47SBarry Smith   PetscInt       i,j,rstart = mat->rmap.rstart,rend = mat->rmap.rend;
246899cda47SBarry Smith   PetscInt       cstart = mat->cmap.rstart,cend = mat->cmap.rend,row,col;
247b49de8d1SLois Curfman McInnes 
2483a40ed3dSBarry Smith   PetscFunctionBegin;
249b49de8d1SLois Curfman McInnes   for (i=0; i<m; i++) {
25077431f27SBarry Smith     if (idxm[i] < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",idxm[i]);
251899cda47SBarry Smith     if (idxm[i] >= mat->rmap.N) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",idxm[i],mat->rmap.N-1);
252b49de8d1SLois Curfman McInnes     if (idxm[i] >= rstart && idxm[i] < rend) {
253b49de8d1SLois Curfman McInnes       row = idxm[i] - rstart;
254b49de8d1SLois Curfman McInnes       for (j=0; j<n; j++) {
25577431f27SBarry Smith         if (idxn[j] < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",idxn[j]);
256899cda47SBarry Smith         if (idxn[j] >= mat->cmap.N) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",idxn[j],mat->cmap.N-1);
257b49de8d1SLois Curfman McInnes         if (idxn[j] >= cstart && idxn[j] < cend){
258b49de8d1SLois Curfman McInnes           col = idxn[j] - cstart;
259b49de8d1SLois Curfman McInnes           ierr = MatGetValues(aij->A,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
260fa852ad4SSatish Balay         } else {
261905e6a2fSBarry Smith           if (!aij->colmap) {
262905e6a2fSBarry Smith             ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
263905e6a2fSBarry Smith           }
264aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
2650f5bd95cSBarry Smith           ierr = PetscTableFind(aij->colmap,idxn[j]+1,&col);CHKERRQ(ierr);
266fa46199cSSatish Balay           col --;
267b1fc9764SSatish Balay #else
268905e6a2fSBarry Smith           col = aij->colmap[idxn[j]] - 1;
269b1fc9764SSatish Balay #endif
270e60e1c95SSatish Balay           if ((col < 0) || (aij->garray[col] != idxn[j])) *(v+i*n+j) = 0.0;
271d9d09a02SSatish Balay           else {
272b49de8d1SLois Curfman McInnes             ierr = MatGetValues(aij->B,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
273b49de8d1SLois Curfman McInnes           }
274b49de8d1SLois Curfman McInnes         }
275b49de8d1SLois Curfman McInnes       }
276a8c6a408SBarry Smith     } else {
27729bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"Only local values currently supported");
278b49de8d1SLois Curfman McInnes     }
279b49de8d1SLois Curfman McInnes   }
2803a40ed3dSBarry Smith   PetscFunctionReturn(0);
281b49de8d1SLois Curfman McInnes }
282bc5ccf88SSatish Balay 
2834a2ae208SSatish Balay #undef __FUNCT__
2844a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyBegin_MPIAIJ"
285dfbe8321SBarry Smith PetscErrorCode MatAssemblyBegin_MPIAIJ(Mat mat,MatAssemblyType mode)
286bc5ccf88SSatish Balay {
287bc5ccf88SSatish Balay   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
288dfbe8321SBarry Smith   PetscErrorCode ierr;
289b1d57f15SBarry Smith   PetscInt       nstash,reallocs;
290bc5ccf88SSatish Balay   InsertMode     addv;
291bc5ccf88SSatish Balay 
292bc5ccf88SSatish Balay   PetscFunctionBegin;
293bc5ccf88SSatish Balay   if (aij->donotstash) {
294bc5ccf88SSatish Balay     PetscFunctionReturn(0);
295bc5ccf88SSatish Balay   }
296bc5ccf88SSatish Balay 
297bc5ccf88SSatish Balay   /* make sure all processors are either in INSERTMODE or ADDMODE */
298bc5ccf88SSatish Balay   ierr = MPI_Allreduce(&mat->insertmode,&addv,1,MPI_INT,MPI_BOR,mat->comm);CHKERRQ(ierr);
299bc5ccf88SSatish Balay   if (addv == (ADD_VALUES|INSERT_VALUES)) {
30029bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Some processors inserted others added");
301bc5ccf88SSatish Balay   }
302bc5ccf88SSatish Balay   mat->insertmode = addv; /* in case this processor had no cache */
303bc5ccf88SSatish Balay 
304899cda47SBarry Smith   ierr = MatStashScatterBegin_Private(&mat->stash,mat->rmap.range);CHKERRQ(ierr);
3058798bf22SSatish Balay   ierr = MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);CHKERRQ(ierr);
306ae15b995SBarry Smith   ierr = PetscInfo2(aij->A,"Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);CHKERRQ(ierr);
307bc5ccf88SSatish Balay   PetscFunctionReturn(0);
308bc5ccf88SSatish Balay }
309bc5ccf88SSatish Balay 
3104a2ae208SSatish Balay #undef __FUNCT__
3114a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_MPIAIJ"
312dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_MPIAIJ(Mat mat,MatAssemblyType mode)
313bc5ccf88SSatish Balay {
314bc5ccf88SSatish Balay   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
31591c97fd4SSatish Balay   Mat_SeqAIJ     *a=(Mat_SeqAIJ *)aij->A->data;
3166849ba73SBarry Smith   PetscErrorCode ierr;
317b1d57f15SBarry Smith   PetscMPIInt    n;
318b1d57f15SBarry Smith   PetscInt       i,j,rstart,ncols,flg;
319b1d57f15SBarry Smith   PetscInt       *row,*col,other_disassembled;
32087828ca2SBarry Smith   PetscScalar    *val;
321bc5ccf88SSatish Balay   InsertMode     addv = mat->insertmode;
322bc5ccf88SSatish Balay 
32391c97fd4SSatish Balay   /* do not use 'b = (Mat_SeqAIJ *)aij->B->data' as B can be reset in disassembly */
324bc5ccf88SSatish Balay   PetscFunctionBegin;
325bc5ccf88SSatish Balay   if (!aij->donotstash) {
326a2d1c673SSatish Balay     while (1) {
3278798bf22SSatish Balay       ierr = MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);CHKERRQ(ierr);
328a2d1c673SSatish Balay       if (!flg) break;
329a2d1c673SSatish Balay 
330bc5ccf88SSatish Balay       for (i=0; i<n;) {
331bc5ccf88SSatish Balay         /* Now identify the consecutive vals belonging to the same row */
332bc5ccf88SSatish Balay         for (j=i,rstart=row[j]; j<n; j++) { if (row[j] != rstart) break; }
333bc5ccf88SSatish Balay         if (j < n) ncols = j-i;
334bc5ccf88SSatish Balay         else       ncols = n-i;
335bc5ccf88SSatish Balay         /* Now assemble all these values with a single function call */
336bc5ccf88SSatish Balay         ierr = MatSetValues_MPIAIJ(mat,1,row+i,ncols,col+i,val+i,addv);CHKERRQ(ierr);
337bc5ccf88SSatish Balay         i = j;
338bc5ccf88SSatish Balay       }
339bc5ccf88SSatish Balay     }
3408798bf22SSatish Balay     ierr = MatStashScatterEnd_Private(&mat->stash);CHKERRQ(ierr);
341bc5ccf88SSatish Balay   }
3422f53aa61SHong Zhang   a->compressedrow.use     = PETSC_FALSE;
343bc5ccf88SSatish Balay   ierr = MatAssemblyBegin(aij->A,mode);CHKERRQ(ierr);
344bc5ccf88SSatish Balay   ierr = MatAssemblyEnd(aij->A,mode);CHKERRQ(ierr);
345bc5ccf88SSatish Balay 
346bc5ccf88SSatish Balay   /* determine if any processor has disassembled, if so we must
347bc5ccf88SSatish Balay      also disassemble ourselfs, in order that we may reassemble. */
348bc5ccf88SSatish Balay   /*
349bc5ccf88SSatish Balay      if nonzero structure of submatrix B cannot change then we know that
350bc5ccf88SSatish Balay      no processor disassembled thus we can skip this stuff
351bc5ccf88SSatish Balay   */
352bc5ccf88SSatish Balay   if (!((Mat_SeqAIJ*)aij->B->data)->nonew)  {
353bc5ccf88SSatish Balay     ierr = MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPI_INT,MPI_PROD,mat->comm);CHKERRQ(ierr);
354bc5ccf88SSatish Balay     if (mat->was_assembled && !other_disassembled) {
355bc5ccf88SSatish Balay       ierr = DisAssemble_MPIAIJ(mat);CHKERRQ(ierr);
356ad59fb31SSatish Balay     }
357ad59fb31SSatish Balay   }
358bc5ccf88SSatish Balay   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
359bc5ccf88SSatish Balay     ierr = MatSetUpMultiply_MPIAIJ(mat);CHKERRQ(ierr);
360bc5ccf88SSatish Balay   }
361923f20ffSKris Buschelman   ierr = MatSetOption(aij->B,MAT_DO_NOT_USE_INODES);CHKERRQ(ierr);
36291c97fd4SSatish Balay   ((Mat_SeqAIJ *)aij->B->data)->compressedrow.use = PETSC_TRUE; /* b->compressedrow.use */
363bc5ccf88SSatish Balay   ierr = MatAssemblyBegin(aij->B,mode);CHKERRQ(ierr);
364bc5ccf88SSatish Balay   ierr = MatAssemblyEnd(aij->B,mode);CHKERRQ(ierr);
365bc5ccf88SSatish Balay 
366606d414cSSatish Balay   ierr = PetscFree(aij->rowvalues);CHKERRQ(ierr);
367606d414cSSatish Balay   aij->rowvalues = 0;
368a30b2313SHong Zhang 
369a30b2313SHong Zhang   /* used by MatAXPY() */
37091c97fd4SSatish Balay   a->xtoy = 0; ((Mat_SeqAIJ *)aij->B->data)->xtoy = 0;  /* b->xtoy = 0 */
37191c97fd4SSatish Balay   a->XtoY = 0; ((Mat_SeqAIJ *)aij->B->data)->XtoY = 0;  /* b->XtoY = 0 */
372a30b2313SHong Zhang 
373bc5ccf88SSatish Balay   PetscFunctionReturn(0);
374bc5ccf88SSatish Balay }
375bc5ccf88SSatish Balay 
3764a2ae208SSatish Balay #undef __FUNCT__
3774a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_MPIAIJ"
378dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_MPIAIJ(Mat A)
3791eb62cbbSBarry Smith {
38044a69424SLois Curfman McInnes   Mat_MPIAIJ     *l = (Mat_MPIAIJ*)A->data;
381dfbe8321SBarry Smith   PetscErrorCode ierr;
3823a40ed3dSBarry Smith 
3833a40ed3dSBarry Smith   PetscFunctionBegin;
38478b31e54SBarry Smith   ierr = MatZeroEntries(l->A);CHKERRQ(ierr);
38578b31e54SBarry Smith   ierr = MatZeroEntries(l->B);CHKERRQ(ierr);
3863a40ed3dSBarry Smith   PetscFunctionReturn(0);
3871eb62cbbSBarry Smith }
3881eb62cbbSBarry Smith 
3894a2ae208SSatish Balay #undef __FUNCT__
3904a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_MPIAIJ"
391f4df32b1SMatthew Knepley PetscErrorCode MatZeroRows_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag)
3921eb62cbbSBarry Smith {
39344a69424SLois Curfman McInnes   Mat_MPIAIJ     *l = (Mat_MPIAIJ*)A->data;
3946849ba73SBarry Smith   PetscErrorCode ierr;
3956543fbbaSBarry Smith   PetscMPIInt    size = l->size,imdex,n,rank = l->rank,tag = A->tag,lastidx = -1;
396899cda47SBarry Smith   PetscInt       i,*owners = A->rmap.range;
397b1d57f15SBarry Smith   PetscInt       *nprocs,j,idx,nsends,row;
398b1d57f15SBarry Smith   PetscInt       nmax,*svalues,*starts,*owner,nrecvs;
399b1d57f15SBarry Smith   PetscInt       *rvalues,count,base,slen,*source;
400899cda47SBarry Smith   PetscInt       *lens,*lrows,*values,rstart=A->rmap.rstart;
401d6dfbf8fSBarry Smith   MPI_Comm       comm = A->comm;
4021eb62cbbSBarry Smith   MPI_Request    *send_waits,*recv_waits;
4031eb62cbbSBarry Smith   MPI_Status     recv_status,*send_status;
4046543fbbaSBarry Smith #if defined(PETSC_DEBUG)
4056543fbbaSBarry Smith   PetscTruth     found = PETSC_FALSE;
4066543fbbaSBarry Smith #endif
4071eb62cbbSBarry Smith 
4083a40ed3dSBarry Smith   PetscFunctionBegin;
4091eb62cbbSBarry Smith   /*  first count number of contributors to each processor */
410b1d57f15SBarry Smith   ierr = PetscMalloc(2*size*sizeof(PetscInt),&nprocs);CHKERRQ(ierr);
411b1d57f15SBarry Smith   ierr = PetscMemzero(nprocs,2*size*sizeof(PetscInt));CHKERRQ(ierr);
412b1d57f15SBarry Smith   ierr = PetscMalloc((N+1)*sizeof(PetscInt),&owner);CHKERRQ(ierr); /* see note*/
4136543fbbaSBarry Smith   j = 0;
4141eb62cbbSBarry Smith   for (i=0; i<N; i++) {
4156543fbbaSBarry Smith     if (lastidx > (idx = rows[i])) j = 0;
4166543fbbaSBarry Smith     lastidx = idx;
4176543fbbaSBarry Smith     for (; j<size; j++) {
4181eb62cbbSBarry Smith       if (idx >= owners[j] && idx < owners[j+1]) {
4196543fbbaSBarry Smith         nprocs[2*j]++;
4206543fbbaSBarry Smith         nprocs[2*j+1] = 1;
4216543fbbaSBarry Smith         owner[i] = j;
4226543fbbaSBarry Smith #if defined(PETSC_DEBUG)
4236543fbbaSBarry Smith         found = PETSC_TRUE;
4246543fbbaSBarry Smith #endif
4256543fbbaSBarry Smith         break;
4261eb62cbbSBarry Smith       }
4271eb62cbbSBarry Smith     }
4286543fbbaSBarry Smith #if defined(PETSC_DEBUG)
42929bbc08cSBarry Smith     if (!found) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Index out of range");
4306543fbbaSBarry Smith     found = PETSC_FALSE;
4316543fbbaSBarry Smith #endif
4321eb62cbbSBarry Smith   }
433c1dc657dSBarry Smith   nsends = 0;  for (i=0; i<size; i++) { nsends += nprocs[2*i+1];}
4341eb62cbbSBarry Smith 
4351eb62cbbSBarry Smith   /* inform other processors of number of messages and max length*/
436c1dc657dSBarry Smith   ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr);
4371eb62cbbSBarry Smith 
4381eb62cbbSBarry Smith   /* post receives:   */
439b1d57f15SBarry Smith   ierr = PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(PetscInt),&rvalues);CHKERRQ(ierr);
440b0a32e0cSBarry Smith   ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr);
4411eb62cbbSBarry Smith   for (i=0; i<nrecvs; i++) {
442b1d57f15SBarry Smith     ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPIU_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr);
4431eb62cbbSBarry Smith   }
4441eb62cbbSBarry Smith 
4451eb62cbbSBarry Smith   /* do sends:
4461eb62cbbSBarry Smith       1) starts[i] gives the starting index in svalues for stuff going to
4471eb62cbbSBarry Smith          the ith processor
4481eb62cbbSBarry Smith   */
449b1d57f15SBarry Smith   ierr = PetscMalloc((N+1)*sizeof(PetscInt),&svalues);CHKERRQ(ierr);
450b0a32e0cSBarry Smith   ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr);
451b1d57f15SBarry Smith   ierr = PetscMalloc((size+1)*sizeof(PetscInt),&starts);CHKERRQ(ierr);
4521eb62cbbSBarry Smith   starts[0] = 0;
453c1dc657dSBarry Smith   for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
4541eb62cbbSBarry Smith   for (i=0; i<N; i++) {
4551eb62cbbSBarry Smith     svalues[starts[owner[i]]++] = rows[i];
4561eb62cbbSBarry Smith   }
4571eb62cbbSBarry Smith 
4581eb62cbbSBarry Smith   starts[0] = 0;
459c1dc657dSBarry Smith   for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
4601eb62cbbSBarry Smith   count = 0;
46117699dbbSLois Curfman McInnes   for (i=0; i<size; i++) {
462c1dc657dSBarry Smith     if (nprocs[2*i+1]) {
463b1d57f15SBarry Smith       ierr = MPI_Isend(svalues+starts[i],nprocs[2*i],MPIU_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr);
4641eb62cbbSBarry Smith     }
4651eb62cbbSBarry Smith   }
466606d414cSSatish Balay   ierr = PetscFree(starts);CHKERRQ(ierr);
4671eb62cbbSBarry Smith 
46817699dbbSLois Curfman McInnes   base = owners[rank];
4691eb62cbbSBarry Smith 
4701eb62cbbSBarry Smith   /*  wait on receives */
471b1d57f15SBarry Smith   ierr   = PetscMalloc(2*(nrecvs+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
4721eb62cbbSBarry Smith   source = lens + nrecvs;
4731eb62cbbSBarry Smith   count  = nrecvs; slen = 0;
4741eb62cbbSBarry Smith   while (count) {
475ca161407SBarry Smith     ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
4761eb62cbbSBarry Smith     /* unpack receives into our local space */
477b1d57f15SBarry Smith     ierr = MPI_Get_count(&recv_status,MPIU_INT,&n);CHKERRQ(ierr);
478d6dfbf8fSBarry Smith     source[imdex]  = recv_status.MPI_SOURCE;
479d6dfbf8fSBarry Smith     lens[imdex]    = n;
4801eb62cbbSBarry Smith     slen          += n;
4811eb62cbbSBarry Smith     count--;
4821eb62cbbSBarry Smith   }
483606d414cSSatish Balay   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
4841eb62cbbSBarry Smith 
4851eb62cbbSBarry Smith   /* move the data into the send scatter */
486b1d57f15SBarry Smith   ierr = PetscMalloc((slen+1)*sizeof(PetscInt),&lrows);CHKERRQ(ierr);
4871eb62cbbSBarry Smith   count = 0;
4881eb62cbbSBarry Smith   for (i=0; i<nrecvs; i++) {
4891eb62cbbSBarry Smith     values = rvalues + i*nmax;
4901eb62cbbSBarry Smith     for (j=0; j<lens[i]; j++) {
4911eb62cbbSBarry Smith       lrows[count++] = values[j] - base;
4921eb62cbbSBarry Smith     }
4931eb62cbbSBarry Smith   }
494606d414cSSatish Balay   ierr = PetscFree(rvalues);CHKERRQ(ierr);
495606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
496606d414cSSatish Balay   ierr = PetscFree(owner);CHKERRQ(ierr);
497606d414cSSatish Balay   ierr = PetscFree(nprocs);CHKERRQ(ierr);
4981eb62cbbSBarry Smith 
4991eb62cbbSBarry Smith   /* actually zap the local rows */
5006eb55b6aSBarry Smith   /*
5016eb55b6aSBarry Smith         Zero the required rows. If the "diagonal block" of the matrix
502a8c7a070SBarry Smith      is square and the user wishes to set the diagonal we use separate
5036eb55b6aSBarry Smith      code so that MatSetValues() is not called for each diagonal allocating
5046eb55b6aSBarry Smith      new memory, thus calling lots of mallocs and slowing things down.
5056eb55b6aSBarry Smith 
506f4df32b1SMatthew Knepley        Contributed by: Matthew Knepley
5076eb55b6aSBarry Smith   */
508e2d53e46SBarry Smith   /* must zero l->B before l->A because the (diag) case below may put values into l->B*/
509f4df32b1SMatthew Knepley   ierr = MatZeroRows(l->B,slen,lrows,0.0);CHKERRQ(ierr);
510899cda47SBarry Smith   if ((diag != 0.0) && (l->A->rmap.N == l->A->cmap.N)) {
511f4df32b1SMatthew Knepley     ierr      = MatZeroRows(l->A,slen,lrows,diag);CHKERRQ(ierr);
512f4df32b1SMatthew Knepley   } else if (diag != 0.0) {
513f4df32b1SMatthew Knepley     ierr = MatZeroRows(l->A,slen,lrows,0.0);CHKERRQ(ierr);
514fa46199cSSatish Balay     if (((Mat_SeqAIJ*)l->A->data)->nonew) {
51529bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"MatZeroRows() on rectangular matrices cannot be used with the Mat options\n\
516fa46199cSSatish Balay MAT_NO_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR");
5176525c446SSatish Balay     }
518e2d53e46SBarry Smith     for (i = 0; i < slen; i++) {
519e2d53e46SBarry Smith       row  = lrows[i] + rstart;
520f4df32b1SMatthew Knepley       ierr = MatSetValues(A,1,&row,1,&row,&diag,INSERT_VALUES);CHKERRQ(ierr);
521e2d53e46SBarry Smith     }
522e2d53e46SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
523e2d53e46SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
5246eb55b6aSBarry Smith   } else {
525f4df32b1SMatthew Knepley     ierr = MatZeroRows(l->A,slen,lrows,0.0);CHKERRQ(ierr);
5266eb55b6aSBarry Smith   }
527606d414cSSatish Balay   ierr = PetscFree(lrows);CHKERRQ(ierr);
52872dacd9aSBarry Smith 
5291eb62cbbSBarry Smith   /* wait on sends */
5301eb62cbbSBarry Smith   if (nsends) {
531b0a32e0cSBarry Smith     ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr);
532ca161407SBarry Smith     ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr);
533606d414cSSatish Balay     ierr = PetscFree(send_status);CHKERRQ(ierr);
5341eb62cbbSBarry Smith   }
535606d414cSSatish Balay   ierr = PetscFree(send_waits);CHKERRQ(ierr);
536606d414cSSatish Balay   ierr = PetscFree(svalues);CHKERRQ(ierr);
5371eb62cbbSBarry Smith 
5383a40ed3dSBarry Smith   PetscFunctionReturn(0);
5391eb62cbbSBarry Smith }
5401eb62cbbSBarry Smith 
5414a2ae208SSatish Balay #undef __FUNCT__
5424a2ae208SSatish Balay #define __FUNCT__ "MatMult_MPIAIJ"
543dfbe8321SBarry Smith PetscErrorCode MatMult_MPIAIJ(Mat A,Vec xx,Vec yy)
5441eb62cbbSBarry Smith {
545416022c9SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
546dfbe8321SBarry Smith   PetscErrorCode ierr;
547b1d57f15SBarry Smith   PetscInt       nt;
548416022c9SBarry Smith 
5493a40ed3dSBarry Smith   PetscFunctionBegin;
550a2ce50c7SBarry Smith   ierr = VecGetLocalSize(xx,&nt);CHKERRQ(ierr);
551899cda47SBarry Smith   if (nt != A->cmap.n) {
552899cda47SBarry Smith     SETERRQ2(PETSC_ERR_ARG_SIZ,"Incompatible partition of A (%D) and xx (%D)",A->cmap.n,nt);
553fbd6ef76SBarry Smith   }
55443a90d84SBarry Smith   ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
555f830108cSBarry Smith   ierr = (*a->A->ops->mult)(a->A,xx,yy);CHKERRQ(ierr);
55643a90d84SBarry Smith   ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
557f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);CHKERRQ(ierr);
5583a40ed3dSBarry Smith   PetscFunctionReturn(0);
5591eb62cbbSBarry Smith }
5601eb62cbbSBarry Smith 
5614a2ae208SSatish Balay #undef __FUNCT__
5624a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_MPIAIJ"
563dfbe8321SBarry Smith PetscErrorCode MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
564da3a660dSBarry Smith {
565416022c9SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
566dfbe8321SBarry Smith   PetscErrorCode ierr;
5673a40ed3dSBarry Smith 
5683a40ed3dSBarry Smith   PetscFunctionBegin;
56943a90d84SBarry Smith   ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
570f830108cSBarry Smith   ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
57143a90d84SBarry Smith   ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
572f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);CHKERRQ(ierr);
5733a40ed3dSBarry Smith   PetscFunctionReturn(0);
574da3a660dSBarry Smith }
575da3a660dSBarry Smith 
5764a2ae208SSatish Balay #undef __FUNCT__
5774a2ae208SSatish Balay #define __FUNCT__ "MatMultTranspose_MPIAIJ"
578dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_MPIAIJ(Mat A,Vec xx,Vec yy)
579da3a660dSBarry Smith {
580416022c9SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
581dfbe8321SBarry Smith   PetscErrorCode ierr;
582a5ff213dSBarry Smith   PetscTruth     merged;
583da3a660dSBarry Smith 
5843a40ed3dSBarry Smith   PetscFunctionBegin;
585a5ff213dSBarry Smith   ierr = VecScatterGetMerged(a->Mvctx,&merged);CHKERRQ(ierr);
586da3a660dSBarry Smith   /* do nondiagonal part */
5877c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
588a5ff213dSBarry Smith   if (!merged) {
589da3a660dSBarry Smith     /* send it on its way */
590537820f0SBarry Smith     ierr = VecScatterBegin(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
591da3a660dSBarry Smith     /* do local part */
5927c922b88SBarry Smith     ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr);
593da3a660dSBarry Smith     /* receive remote parts: note this assumes the values are not actually */
594a5ff213dSBarry Smith     /* added in yy until the next line, */
595537820f0SBarry Smith     ierr = VecScatterEnd(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
596a5ff213dSBarry Smith   } else {
597a5ff213dSBarry Smith     /* do local part */
598a5ff213dSBarry Smith     ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr);
599a5ff213dSBarry Smith     /* send it on its way */
600a5ff213dSBarry Smith     ierr = VecScatterBegin(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
601a5ff213dSBarry Smith     /* values actually were received in the Begin() but we need to call this nop */
602a5ff213dSBarry Smith     ierr = VecScatterEnd(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
603a5ff213dSBarry Smith   }
6043a40ed3dSBarry Smith   PetscFunctionReturn(0);
605da3a660dSBarry Smith }
606da3a660dSBarry Smith 
607cd0d46ebSvictorle EXTERN_C_BEGIN
608cd0d46ebSvictorle #undef __FUNCT__
6095fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_MPIAIJ"
61013c77408SMatthew Knepley PetscErrorCode PETSCMAT_DLLEXPORT MatIsTranspose_MPIAIJ(Mat Amat,Mat Bmat,PetscReal tol,PetscTruth *f)
611cd0d46ebSvictorle {
6124f423910Svictorle   MPI_Comm       comm;
613cd0d46ebSvictorle   Mat_MPIAIJ     *Aij = (Mat_MPIAIJ *) Amat->data, *Bij;
61466501d38Svictorle   Mat            Adia = Aij->A, Bdia, Aoff,Boff,*Aoffs,*Boffs;
615cd0d46ebSvictorle   IS             Me,Notme;
6166849ba73SBarry Smith   PetscErrorCode ierr;
617b1d57f15SBarry Smith   PetscInt       M,N,first,last,*notme,i;
618b1d57f15SBarry Smith   PetscMPIInt    size;
619cd0d46ebSvictorle 
620cd0d46ebSvictorle   PetscFunctionBegin;
62142e5f5b4Svictorle 
62242e5f5b4Svictorle   /* Easy test: symmetric diagonal block */
62366501d38Svictorle   Bij = (Mat_MPIAIJ *) Bmat->data; Bdia = Bij->A;
6245485867bSBarry Smith   ierr = MatIsTranspose(Adia,Bdia,tol,f);CHKERRQ(ierr);
625cd0d46ebSvictorle   if (!*f) PetscFunctionReturn(0);
6264f423910Svictorle   ierr = PetscObjectGetComm((PetscObject)Amat,&comm);CHKERRQ(ierr);
627b1d57f15SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
628b1d57f15SBarry Smith   if (size == 1) PetscFunctionReturn(0);
62942e5f5b4Svictorle 
63042e5f5b4Svictorle   /* Hard test: off-diagonal block. This takes a MatGetSubMatrix. */
631cd0d46ebSvictorle   ierr = MatGetSize(Amat,&M,&N);CHKERRQ(ierr);
632cd0d46ebSvictorle   ierr = MatGetOwnershipRange(Amat,&first,&last);CHKERRQ(ierr);
633b1d57f15SBarry Smith   ierr = PetscMalloc((N-last+first)*sizeof(PetscInt),&notme);CHKERRQ(ierr);
634cd0d46ebSvictorle   for (i=0; i<first; i++) notme[i] = i;
635cd0d46ebSvictorle   for (i=last; i<M; i++) notme[i-last+first] = i;
636268466fbSBarry Smith   ierr = ISCreateGeneral(MPI_COMM_SELF,N-last+first,notme,&Notme);CHKERRQ(ierr);
637268466fbSBarry Smith   ierr = ISCreateStride(MPI_COMM_SELF,last-first,first,1,&Me);CHKERRQ(ierr);
638268466fbSBarry Smith   ierr = MatGetSubMatrices(Amat,1,&Me,&Notme,MAT_INITIAL_MATRIX,&Aoffs);CHKERRQ(ierr);
63966501d38Svictorle   Aoff = Aoffs[0];
640268466fbSBarry Smith   ierr = MatGetSubMatrices(Bmat,1,&Notme,&Me,MAT_INITIAL_MATRIX,&Boffs);CHKERRQ(ierr);
64166501d38Svictorle   Boff = Boffs[0];
6425485867bSBarry Smith   ierr = MatIsTranspose(Aoff,Boff,tol,f);CHKERRQ(ierr);
64366501d38Svictorle   ierr = MatDestroyMatrices(1,&Aoffs);CHKERRQ(ierr);
64466501d38Svictorle   ierr = MatDestroyMatrices(1,&Boffs);CHKERRQ(ierr);
64542e5f5b4Svictorle   ierr = ISDestroy(Me);CHKERRQ(ierr);
64642e5f5b4Svictorle   ierr = ISDestroy(Notme);CHKERRQ(ierr);
64742e5f5b4Svictorle 
648cd0d46ebSvictorle   PetscFunctionReturn(0);
649cd0d46ebSvictorle }
650cd0d46ebSvictorle EXTERN_C_END
651cd0d46ebSvictorle 
6524a2ae208SSatish Balay #undef __FUNCT__
6534a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_MPIAIJ"
654dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
655da3a660dSBarry Smith {
656416022c9SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
657dfbe8321SBarry Smith   PetscErrorCode ierr;
658da3a660dSBarry Smith 
6593a40ed3dSBarry Smith   PetscFunctionBegin;
660da3a660dSBarry Smith   /* do nondiagonal part */
6617c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
662da3a660dSBarry Smith   /* send it on its way */
663537820f0SBarry Smith   ierr = VecScatterBegin(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
664da3a660dSBarry Smith   /* do local part */
6657c922b88SBarry Smith   ierr = (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
666a5ff213dSBarry Smith   /* receive remote parts */
6670a198c4cSBarry Smith   ierr = VecScatterEnd(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
6683a40ed3dSBarry Smith   PetscFunctionReturn(0);
669da3a660dSBarry Smith }
670da3a660dSBarry Smith 
6711eb62cbbSBarry Smith /*
6721eb62cbbSBarry Smith   This only works correctly for square matrices where the subblock A->A is the
6731eb62cbbSBarry Smith    diagonal block
6741eb62cbbSBarry Smith */
6754a2ae208SSatish Balay #undef __FUNCT__
6764a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_MPIAIJ"
677dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_MPIAIJ(Mat A,Vec v)
6781eb62cbbSBarry Smith {
679dfbe8321SBarry Smith   PetscErrorCode ierr;
680416022c9SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
6813a40ed3dSBarry Smith 
6823a40ed3dSBarry Smith   PetscFunctionBegin;
683899cda47SBarry Smith   if (A->rmap.N != A->cmap.N) SETERRQ(PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block");
684899cda47SBarry Smith   if (A->rmap.rstart != A->cmap.rstart || A->rmap.rend != A->cmap.rend) {
68529bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_SIZ,"row partition must equal col partition");
6863a40ed3dSBarry Smith   }
6873a40ed3dSBarry Smith   ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr);
6883a40ed3dSBarry Smith   PetscFunctionReturn(0);
6891eb62cbbSBarry Smith }
6901eb62cbbSBarry Smith 
6914a2ae208SSatish Balay #undef __FUNCT__
6924a2ae208SSatish Balay #define __FUNCT__ "MatScale_MPIAIJ"
693f4df32b1SMatthew Knepley PetscErrorCode MatScale_MPIAIJ(Mat A,PetscScalar aa)
694052efed2SBarry Smith {
695052efed2SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
696dfbe8321SBarry Smith   PetscErrorCode ierr;
6973a40ed3dSBarry Smith 
6983a40ed3dSBarry Smith   PetscFunctionBegin;
699f4df32b1SMatthew Knepley   ierr = MatScale(a->A,aa);CHKERRQ(ierr);
700f4df32b1SMatthew Knepley   ierr = MatScale(a->B,aa);CHKERRQ(ierr);
7013a40ed3dSBarry Smith   PetscFunctionReturn(0);
702052efed2SBarry Smith }
703052efed2SBarry Smith 
7044a2ae208SSatish Balay #undef __FUNCT__
7054a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_MPIAIJ"
706dfbe8321SBarry Smith PetscErrorCode MatDestroy_MPIAIJ(Mat mat)
7071eb62cbbSBarry Smith {
70844a69424SLois Curfman McInnes   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
709dfbe8321SBarry Smith   PetscErrorCode ierr;
71083e2fdc7SBarry Smith 
7113a40ed3dSBarry Smith   PetscFunctionBegin;
712aa482453SBarry Smith #if defined(PETSC_USE_LOG)
713899cda47SBarry Smith   PetscLogObjectState((PetscObject)mat,"Rows=%D, Cols=%D",mat->rmap.N,mat->cmap.N);
714a5a9c739SBarry Smith #endif
7158798bf22SSatish Balay   ierr = MatStashDestroy_Private(&mat->stash);CHKERRQ(ierr);
71678b31e54SBarry Smith   ierr = MatDestroy(aij->A);CHKERRQ(ierr);
71778b31e54SBarry Smith   ierr = MatDestroy(aij->B);CHKERRQ(ierr);
718aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
7190f5bd95cSBarry Smith   if (aij->colmap) {ierr = PetscTableDelete(aij->colmap);CHKERRQ(ierr);}
720b1fc9764SSatish Balay #else
72105b42c5fSBarry Smith   ierr = PetscFree(aij->colmap);CHKERRQ(ierr);
722b1fc9764SSatish Balay #endif
72305b42c5fSBarry Smith   ierr = PetscFree(aij->garray);CHKERRQ(ierr);
7247c922b88SBarry Smith   if (aij->lvec)   {ierr = VecDestroy(aij->lvec);CHKERRQ(ierr);}
7257c922b88SBarry Smith   if (aij->Mvctx)  {ierr = VecScatterDestroy(aij->Mvctx);CHKERRQ(ierr);}
72605b42c5fSBarry Smith   ierr = PetscFree(aij->rowvalues);CHKERRQ(ierr);
727606d414cSSatish Balay   ierr = PetscFree(aij);CHKERRQ(ierr);
728901853e0SKris Buschelman 
729901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
730901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
731901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatGetDiagonalBlock_C","",PETSC_NULL);CHKERRQ(ierr);
732901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr);
733901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
734ff69c46cSKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
735901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatDiagonalScaleLocal_C","",PETSC_NULL);CHKERRQ(ierr);
7363a40ed3dSBarry Smith   PetscFunctionReturn(0);
7371eb62cbbSBarry Smith }
738ee50ffe9SBarry Smith 
7394a2ae208SSatish Balay #undef __FUNCT__
7408e2fed03SBarry Smith #define __FUNCT__ "MatView_MPIAIJ_Binary"
741dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ_Binary(Mat mat,PetscViewer viewer)
7428e2fed03SBarry Smith {
7438e2fed03SBarry Smith   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*)mat->data;
7448e2fed03SBarry Smith   Mat_SeqAIJ*       A = (Mat_SeqAIJ*)aij->A->data;
7458e2fed03SBarry Smith   Mat_SeqAIJ*       B = (Mat_SeqAIJ*)aij->B->data;
7466849ba73SBarry Smith   PetscErrorCode    ierr;
74732dcc486SBarry Smith   PetscMPIInt       rank,size,tag = ((PetscObject)viewer)->tag;
7486f69ff64SBarry Smith   int               fd;
749a788621eSSatish Balay   PetscInt          nz,header[4],*row_lengths,*range=0,rlen,i;
750899cda47SBarry Smith   PetscInt          nzmax,*column_indices,j,k,col,*garray = aij->garray,cnt,cstart = mat->cmap.rstart,rnz;
7518e2fed03SBarry Smith   PetscScalar       *column_values;
7528e2fed03SBarry Smith 
7538e2fed03SBarry Smith   PetscFunctionBegin;
7548e2fed03SBarry Smith   ierr = MPI_Comm_rank(mat->comm,&rank);CHKERRQ(ierr);
7558e2fed03SBarry Smith   ierr = MPI_Comm_size(mat->comm,&size);CHKERRQ(ierr);
7568e2fed03SBarry Smith   nz   = A->nz + B->nz;
757958c9bccSBarry Smith   if (!rank) {
7588e2fed03SBarry Smith     header[0] = MAT_FILE_COOKIE;
759899cda47SBarry Smith     header[1] = mat->rmap.N;
760899cda47SBarry Smith     header[2] = mat->cmap.N;
761b1d57f15SBarry Smith     ierr = MPI_Reduce(&nz,&header[3],1,MPIU_INT,MPI_SUM,0,mat->comm);CHKERRQ(ierr);
7628e2fed03SBarry Smith     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
7636f69ff64SBarry Smith     ierr = PetscBinaryWrite(fd,header,4,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
7648e2fed03SBarry Smith     /* get largest number of rows any processor has */
765899cda47SBarry Smith     rlen = mat->rmap.n;
766357abbc8SBarry Smith     range = mat->rmap.range;
7678e2fed03SBarry Smith     for (i=1; i<size; i++) {
7688e2fed03SBarry Smith       rlen = PetscMax(rlen,range[i+1] - range[i]);
7698e2fed03SBarry Smith     }
7708e2fed03SBarry Smith   } else {
771b1d57f15SBarry Smith     ierr = MPI_Reduce(&nz,0,1,MPIU_INT,MPI_SUM,0,mat->comm);CHKERRQ(ierr);
772899cda47SBarry Smith     rlen = mat->rmap.n;
7738e2fed03SBarry Smith   }
7748e2fed03SBarry Smith 
7758e2fed03SBarry Smith   /* load up the local row counts */
776b1d57f15SBarry Smith   ierr = PetscMalloc((rlen+1)*sizeof(PetscInt),&row_lengths);CHKERRQ(ierr);
777899cda47SBarry Smith   for (i=0; i<mat->rmap.n; i++) {
7788e2fed03SBarry Smith     row_lengths[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i];
7798e2fed03SBarry Smith   }
7808e2fed03SBarry Smith 
7818e2fed03SBarry Smith   /* store the row lengths to the file */
782958c9bccSBarry Smith   if (!rank) {
7838e2fed03SBarry Smith     MPI_Status status;
784899cda47SBarry Smith     ierr = PetscBinaryWrite(fd,row_lengths,mat->rmap.n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
7858e2fed03SBarry Smith     for (i=1; i<size; i++) {
7868e2fed03SBarry Smith       rlen = range[i+1] - range[i];
787b1d57f15SBarry Smith       ierr = MPI_Recv(row_lengths,rlen,MPIU_INT,i,tag,mat->comm,&status);CHKERRQ(ierr);
7886f69ff64SBarry Smith       ierr = PetscBinaryWrite(fd,row_lengths,rlen,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
7898e2fed03SBarry Smith     }
7908e2fed03SBarry Smith   } else {
791899cda47SBarry Smith     ierr = MPI_Send(row_lengths,mat->rmap.n,MPIU_INT,0,tag,mat->comm);CHKERRQ(ierr);
7928e2fed03SBarry Smith   }
7938e2fed03SBarry Smith   ierr = PetscFree(row_lengths);CHKERRQ(ierr);
7948e2fed03SBarry Smith 
7958e2fed03SBarry Smith   /* load up the local column indices */
7968e2fed03SBarry Smith   nzmax = nz; /* )th processor needs space a largest processor needs */
797b1d57f15SBarry Smith   ierr = MPI_Reduce(&nz,&nzmax,1,MPIU_INT,MPI_MAX,0,mat->comm);CHKERRQ(ierr);
798b1d57f15SBarry Smith   ierr = PetscMalloc((nzmax+1)*sizeof(PetscInt),&column_indices);CHKERRQ(ierr);
7998e2fed03SBarry Smith   cnt  = 0;
800899cda47SBarry Smith   for (i=0; i<mat->rmap.n; i++) {
8018e2fed03SBarry Smith     for (j=B->i[i]; j<B->i[i+1]; j++) {
8028e2fed03SBarry Smith       if ( (col = garray[B->j[j]]) > cstart) break;
8038e2fed03SBarry Smith       column_indices[cnt++] = col;
8048e2fed03SBarry Smith     }
8058e2fed03SBarry Smith     for (k=A->i[i]; k<A->i[i+1]; k++) {
8068e2fed03SBarry Smith       column_indices[cnt++] = A->j[k] + cstart;
8078e2fed03SBarry Smith     }
8088e2fed03SBarry Smith     for (; j<B->i[i+1]; j++) {
8098e2fed03SBarry Smith       column_indices[cnt++] = garray[B->j[j]];
8108e2fed03SBarry Smith     }
8118e2fed03SBarry Smith   }
81277431f27SBarry Smith   if (cnt != A->nz + B->nz) SETERRQ2(PETSC_ERR_LIB,"Internal PETSc error: cnt = %D nz = %D",cnt,A->nz+B->nz);
8138e2fed03SBarry Smith 
8148e2fed03SBarry Smith   /* store the column indices to the file */
815958c9bccSBarry Smith   if (!rank) {
8168e2fed03SBarry Smith     MPI_Status status;
8176f69ff64SBarry Smith     ierr = PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
8188e2fed03SBarry Smith     for (i=1; i<size; i++) {
819b1d57f15SBarry Smith       ierr = MPI_Recv(&rnz,1,MPIU_INT,i,tag,mat->comm,&status);CHKERRQ(ierr);
82077431f27SBarry Smith       if (rnz > nzmax) SETERRQ2(PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax);
821b1d57f15SBarry Smith       ierr = MPI_Recv(column_indices,rnz,MPIU_INT,i,tag,mat->comm,&status);CHKERRQ(ierr);
8226f69ff64SBarry Smith       ierr = PetscBinaryWrite(fd,column_indices,rnz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
8238e2fed03SBarry Smith     }
8248e2fed03SBarry Smith   } else {
825b1d57f15SBarry Smith     ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,mat->comm);CHKERRQ(ierr);
826b1d57f15SBarry Smith     ierr = MPI_Send(column_indices,nz,MPIU_INT,0,tag,mat->comm);CHKERRQ(ierr);
8278e2fed03SBarry Smith   }
8288e2fed03SBarry Smith   ierr = PetscFree(column_indices);CHKERRQ(ierr);
8298e2fed03SBarry Smith 
8308e2fed03SBarry Smith   /* load up the local column values */
8318e2fed03SBarry Smith   ierr = PetscMalloc((nzmax+1)*sizeof(PetscScalar),&column_values);CHKERRQ(ierr);
8328e2fed03SBarry Smith   cnt  = 0;
833899cda47SBarry Smith   for (i=0; i<mat->rmap.n; i++) {
8348e2fed03SBarry Smith     for (j=B->i[i]; j<B->i[i+1]; j++) {
8358e2fed03SBarry Smith       if ( garray[B->j[j]] > cstart) break;
8368e2fed03SBarry Smith       column_values[cnt++] = B->a[j];
8378e2fed03SBarry Smith     }
8388e2fed03SBarry Smith     for (k=A->i[i]; k<A->i[i+1]; k++) {
8398e2fed03SBarry Smith       column_values[cnt++] = A->a[k];
8408e2fed03SBarry Smith     }
8418e2fed03SBarry Smith     for (; j<B->i[i+1]; j++) {
8428e2fed03SBarry Smith       column_values[cnt++] = B->a[j];
8438e2fed03SBarry Smith     }
8448e2fed03SBarry Smith   }
84577431f27SBarry Smith   if (cnt != A->nz + B->nz) SETERRQ2(PETSC_ERR_PLIB,"Internal PETSc error: cnt = %D nz = %D",cnt,A->nz+B->nz);
8468e2fed03SBarry Smith 
8478e2fed03SBarry Smith   /* store the column values to the file */
848958c9bccSBarry Smith   if (!rank) {
8498e2fed03SBarry Smith     MPI_Status status;
8506f69ff64SBarry Smith     ierr = PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr);
8518e2fed03SBarry Smith     for (i=1; i<size; i++) {
852b1d57f15SBarry Smith       ierr = MPI_Recv(&rnz,1,MPIU_INT,i,tag,mat->comm,&status);CHKERRQ(ierr);
85377431f27SBarry Smith       if (rnz > nzmax) SETERRQ2(PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax);
854b021249fSBarry Smith       ierr = MPI_Recv(column_values,rnz,MPIU_SCALAR,i,tag,mat->comm,&status);CHKERRQ(ierr);
8556f69ff64SBarry Smith       ierr = PetscBinaryWrite(fd,column_values,rnz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr);
8568e2fed03SBarry Smith     }
8578e2fed03SBarry Smith   } else {
858b1d57f15SBarry Smith     ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,mat->comm);CHKERRQ(ierr);
8598e2fed03SBarry Smith     ierr = MPI_Send(column_values,nz,MPIU_SCALAR,0,tag,mat->comm);CHKERRQ(ierr);
8608e2fed03SBarry Smith   }
8618e2fed03SBarry Smith   ierr = PetscFree(column_values);CHKERRQ(ierr);
8628e2fed03SBarry Smith   PetscFunctionReturn(0);
8638e2fed03SBarry Smith }
8648e2fed03SBarry Smith 
8658e2fed03SBarry Smith #undef __FUNCT__
8664a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ_ASCIIorDraworSocket"
867dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
868416022c9SBarry Smith {
86944a69424SLois Curfman McInnes   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*)mat->data;
870dfbe8321SBarry Smith   PetscErrorCode    ierr;
87132dcc486SBarry Smith   PetscMPIInt       rank = aij->rank,size = aij->size;
872d38fa0fbSBarry Smith   PetscTruth        isdraw,iascii,isbinary;
873b0a32e0cSBarry Smith   PetscViewer       sviewer;
874f3ef73ceSBarry Smith   PetscViewerFormat format;
875416022c9SBarry Smith 
8763a40ed3dSBarry Smith   PetscFunctionBegin;
877fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
87832077d6dSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
8798e2fed03SBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
88032077d6dSBarry Smith   if (iascii) {
881b0a32e0cSBarry Smith     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
882456192e2SBarry Smith     if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
8834e220ebcSLois Curfman McInnes       MatInfo    info;
884923f20ffSKris Buschelman       PetscTruth inodes;
885923f20ffSKris Buschelman 
8861dab6e02SBarry Smith       ierr = MPI_Comm_rank(mat->comm,&rank);CHKERRQ(ierr);
887888f2ed8SSatish Balay       ierr = MatGetInfo(mat,MAT_LOCAL,&info);CHKERRQ(ierr);
888923f20ffSKris Buschelman       ierr = MatInodeGetInodeSizes(aij->A,PETSC_NULL,(PetscInt **)&inodes,PETSC_NULL);CHKERRQ(ierr);
889923f20ffSKris Buschelman       if (!inodes) {
89077431f27SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, not using I-node routines\n",
891899cda47SBarry Smith 					      rank,mat->rmap.n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);CHKERRQ(ierr);
8926831982aSBarry Smith       } else {
89377431f27SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, using I-node routines\n",
894899cda47SBarry Smith 		    rank,mat->rmap.n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);CHKERRQ(ierr);
8956831982aSBarry Smith       }
896888f2ed8SSatish Balay       ierr = MatGetInfo(aij->A,MAT_LOCAL,&info);CHKERRQ(ierr);
89777431f27SBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr);
898888f2ed8SSatish Balay       ierr = MatGetInfo(aij->B,MAT_LOCAL,&info);CHKERRQ(ierr);
89977431f27SBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr);
900b0a32e0cSBarry Smith       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
901a40aa06bSLois Curfman McInnes       ierr = VecScatterView(aij->Mvctx,viewer);CHKERRQ(ierr);
9023a40ed3dSBarry Smith       PetscFunctionReturn(0);
903fb9695e5SSatish Balay     } else if (format == PETSC_VIEWER_ASCII_INFO) {
904923f20ffSKris Buschelman       PetscInt   inodecount,inodelimit,*inodes;
905923f20ffSKris Buschelman       ierr = MatInodeGetInodeSizes(aij->A,&inodecount,&inodes,&inodelimit);CHKERRQ(ierr);
906923f20ffSKris Buschelman       if (inodes) {
907923f20ffSKris Buschelman         ierr = PetscViewerASCIIPrintf(viewer,"using I-node (on process 0) routines: found %D nodes, limit used is %D\n",inodecount,inodelimit);CHKERRQ(ierr);
908d38fa0fbSBarry Smith       } else {
909d38fa0fbSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"not using I-node (on process 0) routines\n");CHKERRQ(ierr);
910d38fa0fbSBarry Smith       }
9113a40ed3dSBarry Smith       PetscFunctionReturn(0);
9124aedb280SBarry Smith     } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
9134aedb280SBarry Smith       PetscFunctionReturn(0);
91408480c60SBarry Smith     }
9158e2fed03SBarry Smith   } else if (isbinary) {
9168e2fed03SBarry Smith     if (size == 1) {
9178e2fed03SBarry Smith       ierr = PetscObjectSetName((PetscObject)aij->A,mat->name);CHKERRQ(ierr);
9188e2fed03SBarry Smith       ierr = MatView(aij->A,viewer);CHKERRQ(ierr);
9198e2fed03SBarry Smith     } else {
9208e2fed03SBarry Smith       ierr = MatView_MPIAIJ_Binary(mat,viewer);CHKERRQ(ierr);
9218e2fed03SBarry Smith     }
9228e2fed03SBarry Smith     PetscFunctionReturn(0);
9230f5bd95cSBarry Smith   } else if (isdraw) {
924b0a32e0cSBarry Smith     PetscDraw  draw;
92519bcc07fSBarry Smith     PetscTruth isnull;
926b0a32e0cSBarry Smith     ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
927b0a32e0cSBarry Smith     ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
92819bcc07fSBarry Smith   }
92919bcc07fSBarry Smith 
93017699dbbSLois Curfman McInnes   if (size == 1) {
931e36acaf3SBarry Smith     ierr = PetscObjectSetName((PetscObject)aij->A,mat->name);CHKERRQ(ierr);
93278b31e54SBarry Smith     ierr = MatView(aij->A,viewer);CHKERRQ(ierr);
9333a40ed3dSBarry Smith   } else {
93495373324SBarry Smith     /* assemble the entire matrix onto first processor. */
93595373324SBarry Smith     Mat         A;
936ec8511deSBarry Smith     Mat_SeqAIJ  *Aloc;
937899cda47SBarry Smith     PetscInt    M = mat->rmap.N,N = mat->cmap.N,m,*ai,*aj,row,*cols,i,*ct;
93887828ca2SBarry Smith     PetscScalar *a;
9392ee70a88SLois Curfman McInnes 
940f69a0ea3SMatthew Knepley     ierr = MatCreate(mat->comm,&A);CHKERRQ(ierr);
94117699dbbSLois Curfman McInnes     if (!rank) {
942f69a0ea3SMatthew Knepley       ierr = MatSetSizes(A,M,N,M,N);CHKERRQ(ierr);
9433a40ed3dSBarry Smith     } else {
944f69a0ea3SMatthew Knepley       ierr = MatSetSizes(A,0,0,M,N);CHKERRQ(ierr);
94595373324SBarry Smith     }
946f204ca49SKris Buschelman     /* This is just a temporary matrix, so explicitly using MATMPIAIJ is probably best */
947f204ca49SKris Buschelman     ierr = MatSetType(A,MATMPIAIJ);CHKERRQ(ierr);
948f204ca49SKris Buschelman     ierr = MatMPIAIJSetPreallocation(A,0,PETSC_NULL,0,PETSC_NULL);CHKERRQ(ierr);
94952e6d16bSBarry Smith     ierr = PetscLogObjectParent(mat,A);CHKERRQ(ierr);
950416022c9SBarry Smith 
95195373324SBarry Smith     /* copy over the A part */
952ec8511deSBarry Smith     Aloc = (Mat_SeqAIJ*)aij->A->data;
953899cda47SBarry Smith     m = aij->A->rmap.n; ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
954899cda47SBarry Smith     row = mat->rmap.rstart;
955899cda47SBarry Smith     for (i=0; i<ai[m]; i++) {aj[i] += mat->cmap.rstart ;}
95695373324SBarry Smith     for (i=0; i<m; i++) {
957416022c9SBarry Smith       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);CHKERRQ(ierr);
95895373324SBarry Smith       row++; a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
95995373324SBarry Smith     }
9602ee70a88SLois Curfman McInnes     aj = Aloc->j;
961899cda47SBarry Smith     for (i=0; i<ai[m]; i++) {aj[i] -= mat->cmap.rstart;}
96295373324SBarry Smith 
96395373324SBarry Smith     /* copy over the B part */
964ec8511deSBarry Smith     Aloc = (Mat_SeqAIJ*)aij->B->data;
965899cda47SBarry Smith     m    = aij->B->rmap.n;  ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
966899cda47SBarry Smith     row  = mat->rmap.rstart;
967b1d57f15SBarry Smith     ierr = PetscMalloc((ai[m]+1)*sizeof(PetscInt),&cols);CHKERRQ(ierr);
968b0a32e0cSBarry Smith     ct   = cols;
969bfec09a0SHong Zhang     for (i=0; i<ai[m]; i++) {cols[i] = aij->garray[aj[i]];}
97095373324SBarry Smith     for (i=0; i<m; i++) {
971416022c9SBarry Smith       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);CHKERRQ(ierr);
97295373324SBarry Smith       row++; a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
97395373324SBarry Smith     }
974606d414cSSatish Balay     ierr = PetscFree(ct);CHKERRQ(ierr);
9756d4a8577SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9766d4a8577SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
97755843e3eSBarry Smith     /*
97855843e3eSBarry Smith        Everyone has to call to draw the matrix since the graphics waits are
979b0a32e0cSBarry Smith        synchronized across all processors that share the PetscDraw object
98055843e3eSBarry Smith     */
981b0a32e0cSBarry Smith     ierr = PetscViewerGetSingleton(viewer,&sviewer);CHKERRQ(ierr);
982e03a110bSBarry Smith     if (!rank) {
983e36acaf3SBarry Smith       ierr = PetscObjectSetName((PetscObject)((Mat_MPIAIJ*)(A->data))->A,mat->name);CHKERRQ(ierr);
9846831982aSBarry Smith       ierr = MatView(((Mat_MPIAIJ*)(A->data))->A,sviewer);CHKERRQ(ierr);
98595373324SBarry Smith     }
986b0a32e0cSBarry Smith     ierr = PetscViewerRestoreSingleton(viewer,&sviewer);CHKERRQ(ierr);
98778b31e54SBarry Smith     ierr = MatDestroy(A);CHKERRQ(ierr);
98895373324SBarry Smith   }
9893a40ed3dSBarry Smith   PetscFunctionReturn(0);
9901eb62cbbSBarry Smith }
9911eb62cbbSBarry Smith 
9924a2ae208SSatish Balay #undef __FUNCT__
9934a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ"
994dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ(Mat mat,PetscViewer viewer)
995416022c9SBarry Smith {
996dfbe8321SBarry Smith   PetscErrorCode ierr;
99732077d6dSBarry Smith   PetscTruth     iascii,isdraw,issocket,isbinary;
998416022c9SBarry Smith 
9993a40ed3dSBarry Smith   PetscFunctionBegin;
100032077d6dSBarry Smith   ierr  = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
1001fb9695e5SSatish Balay   ierr  = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
1002fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
1003b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_SOCKET,&issocket);CHKERRQ(ierr);
100432077d6dSBarry Smith   if (iascii || isdraw || isbinary || issocket) {
10057b2a1423SBarry Smith     ierr = MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer);CHKERRQ(ierr);
10065cd90555SBarry Smith   } else {
100779a5c55eSBarry Smith     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported by MPIAIJ matrices",((PetscObject)viewer)->type_name);
1008416022c9SBarry Smith   }
10093a40ed3dSBarry Smith   PetscFunctionReturn(0);
1010416022c9SBarry Smith }
1011416022c9SBarry Smith 
10121eb62cbbSBarry Smith 
1013c14dc6b6SHong Zhang 
10144a2ae208SSatish Balay #undef __FUNCT__
10154a2ae208SSatish Balay #define __FUNCT__ "MatRelax_MPIAIJ"
1016b1d57f15SBarry Smith PetscErrorCode MatRelax_MPIAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
10178a729477SBarry Smith {
101844a69424SLois Curfman McInnes   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)matin->data;
1019dfbe8321SBarry Smith   PetscErrorCode ierr;
1020c14dc6b6SHong Zhang   Vec            bb1;
10218a729477SBarry Smith 
10223a40ed3dSBarry Smith   PetscFunctionBegin;
102377431f27SBarry Smith   if (its <= 0 || lits <= 0) SETERRQ2(PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D and local its %D both positive",its,lits);
1024c14dc6b6SHong Zhang 
1025c14dc6b6SHong Zhang   ierr = VecDuplicate(bb,&bb1);CHKERRQ(ierr);
10262798e883SHong Zhang 
1027c16cb8f2SBarry Smith   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){
1028da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
1029bd3bf7d3SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,lits,xx);CHKERRQ(ierr);
10302798e883SHong Zhang       its--;
1031da3a660dSBarry Smith     }
10322798e883SHong Zhang 
10332798e883SHong Zhang     while (its--) {
10343a40ed3dSBarry Smith       ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10353a40ed3dSBarry Smith       ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10362798e883SHong Zhang 
1037c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
1038efb30889SBarry Smith       ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr);
1039c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
10402798e883SHong Zhang 
1041c14dc6b6SHong Zhang       /* local sweep */
1042bd3bf7d3SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,lits,xx);
1043c14dc6b6SHong Zhang       CHKERRQ(ierr);
10442798e883SHong Zhang     }
10453a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_FORWARD_SWEEP){
1046da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
1047c14dc6b6SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,PETSC_NULL,xx);CHKERRQ(ierr);
10482798e883SHong Zhang       its--;
1049da3a660dSBarry Smith     }
10502798e883SHong Zhang     while (its--) {
10513a40ed3dSBarry Smith       ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10523a40ed3dSBarry Smith       ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10532798e883SHong Zhang 
1054c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
1055efb30889SBarry Smith       ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr);
1056c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
1057c14dc6b6SHong Zhang 
1058c14dc6b6SHong Zhang       /* local sweep */
1059a6c309e7SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,PETSC_NULL,xx);
1060c14dc6b6SHong Zhang       CHKERRQ(ierr);
10612798e883SHong Zhang     }
10623a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_BACKWARD_SWEEP){
1063da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
1064c14dc6b6SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,PETSC_NULL,xx);CHKERRQ(ierr);
10652798e883SHong Zhang       its--;
1066da3a660dSBarry Smith     }
10672798e883SHong Zhang     while (its--) {
10682e8a6d31SBarry Smith       ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10692e8a6d31SBarry Smith       ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10702798e883SHong Zhang 
1071c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
1072efb30889SBarry Smith       ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr);
1073c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
10742798e883SHong Zhang 
1075c14dc6b6SHong Zhang       /* local sweep */
1076a6c309e7SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,PETSC_NULL,xx);
1077c14dc6b6SHong Zhang       CHKERRQ(ierr);
10782798e883SHong Zhang     }
10793a40ed3dSBarry Smith   } else {
108029bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"Parallel SOR not supported");
1081c16cb8f2SBarry Smith   }
1082c14dc6b6SHong Zhang 
1083c14dc6b6SHong Zhang   ierr = VecDestroy(bb1);CHKERRQ(ierr);
10843a40ed3dSBarry Smith   PetscFunctionReturn(0);
10858a729477SBarry Smith }
1086a66be287SLois Curfman McInnes 
10874a2ae208SSatish Balay #undef __FUNCT__
108842e855d1Svictor #define __FUNCT__ "MatPermute_MPIAIJ"
108942e855d1Svictor PetscErrorCode MatPermute_MPIAIJ(Mat A,IS rowp,IS colp,Mat *B)
109042e855d1Svictor {
109142e855d1Svictor   MPI_Comm       comm,pcomm;
109242e855d1Svictor   PetscInt       first,local_size,nrows,*rows;
109342e855d1Svictor   int            ntids;
109442e855d1Svictor   IS             crowp,growp,irowp,lrowp,lcolp,icolp;
109542e855d1Svictor   PetscErrorCode ierr;
109642e855d1Svictor 
109742e855d1Svictor   PetscFunctionBegin;
109842e855d1Svictor   ierr = PetscObjectGetComm((PetscObject)A,&comm); CHKERRQ(ierr);
109942e855d1Svictor   /* make a collective version of 'rowp' */
110042e855d1Svictor   ierr = PetscObjectGetComm((PetscObject)rowp,&pcomm); CHKERRQ(ierr);
110142e855d1Svictor   if (pcomm==comm) {
110242e855d1Svictor     crowp = rowp;
110342e855d1Svictor   } else {
110442e855d1Svictor     ierr = ISGetSize(rowp,&nrows); CHKERRQ(ierr);
110542e855d1Svictor     ierr = ISGetIndices(rowp,&rows); CHKERRQ(ierr);
110642e855d1Svictor     ierr = ISCreateGeneral(comm,nrows,rows,&crowp); CHKERRQ(ierr);
110742e855d1Svictor     ierr = ISRestoreIndices(rowp,&rows); CHKERRQ(ierr);
110842e855d1Svictor   }
110942e855d1Svictor   /* collect the global row permutation and invert it */
111042e855d1Svictor   ierr = ISAllGather(crowp,&growp); CHKERRQ(ierr);
111142e855d1Svictor   ierr = ISSetPermutation(growp); CHKERRQ(ierr);
111242e855d1Svictor   if (pcomm!=comm) {
111342e855d1Svictor     ierr = ISDestroy(crowp); CHKERRQ(ierr);
111442e855d1Svictor   }
111542e855d1Svictor   ierr = ISInvertPermutation(growp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
111642e855d1Svictor   /* get the local target indices */
111742e855d1Svictor   ierr = MatGetOwnershipRange(A,&first,PETSC_NULL); CHKERRQ(ierr);
111842e855d1Svictor   ierr = MatGetLocalSize(A,&local_size,PETSC_NULL); CHKERRQ(ierr);
111942e855d1Svictor   ierr = ISGetIndices(irowp,&rows); CHKERRQ(ierr);
112042e855d1Svictor   ierr = ISCreateGeneral(MPI_COMM_SELF,local_size,rows+first,&lrowp); CHKERRQ(ierr);
112142e855d1Svictor   ierr = ISRestoreIndices(irowp,&rows); CHKERRQ(ierr);
112242e855d1Svictor   ierr = ISDestroy(irowp); CHKERRQ(ierr);
112342e855d1Svictor   /* the column permutation is so much easier;
112442e855d1Svictor      make a local version of 'colp' and invert it */
112542e855d1Svictor   ierr = PetscObjectGetComm((PetscObject)colp,&pcomm); CHKERRQ(ierr);
112642e855d1Svictor   ierr = MPI_Comm_size(pcomm,&ntids); CHKERRQ(ierr);
112742e855d1Svictor   if (ntids==1) {
112842e855d1Svictor     lcolp = colp;
112942e855d1Svictor   } else {
113042e855d1Svictor     ierr = ISGetSize(colp,&nrows); CHKERRQ(ierr);
113142e855d1Svictor     ierr = ISGetIndices(colp,&rows); CHKERRQ(ierr);
113242e855d1Svictor     ierr = ISCreateGeneral(MPI_COMM_SELF,nrows,rows,&lcolp); CHKERRQ(ierr);
113342e855d1Svictor   }
113442e855d1Svictor   ierr = ISInvertPermutation(lcolp,PETSC_DECIDE,&icolp); CHKERRQ(ierr);
113542e855d1Svictor   ierr = ISSetPermutation(lcolp); CHKERRQ(ierr);
113642e855d1Svictor   if (ntids>1) {
113742e855d1Svictor     ierr = ISRestoreIndices(colp,&rows); CHKERRQ(ierr);
113842e855d1Svictor     ierr = ISDestroy(lcolp); CHKERRQ(ierr);
113942e855d1Svictor   }
114042e855d1Svictor   /* now we just get the submatrix */
114142e855d1Svictor   ierr = MatGetSubMatrix(A,lrowp,icolp,local_size,MAT_INITIAL_MATRIX,B); CHKERRQ(ierr);
114242e855d1Svictor   /* clean up */
114342e855d1Svictor   ierr = ISDestroy(lrowp); CHKERRQ(ierr);
114442e855d1Svictor   ierr = ISDestroy(icolp); CHKERRQ(ierr);
114542e855d1Svictor   PetscFunctionReturn(0);
114642e855d1Svictor }
114742e855d1Svictor 
114842e855d1Svictor #undef __FUNCT__
11494a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_MPIAIJ"
1150dfbe8321SBarry Smith PetscErrorCode MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info)
1151a66be287SLois Curfman McInnes {
1152a66be287SLois Curfman McInnes   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)matin->data;
1153a66be287SLois Curfman McInnes   Mat            A = mat->A,B = mat->B;
1154dfbe8321SBarry Smith   PetscErrorCode ierr;
1155329f5518SBarry Smith   PetscReal      isend[5],irecv[5];
1156a66be287SLois Curfman McInnes 
11573a40ed3dSBarry Smith   PetscFunctionBegin;
11584e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
11594e220ebcSLois Curfman McInnes   ierr = MatGetInfo(A,MAT_LOCAL,info);CHKERRQ(ierr);
11604e220ebcSLois Curfman McInnes   isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
11614e220ebcSLois Curfman McInnes   isend[3] = info->memory;  isend[4] = info->mallocs;
11624e220ebcSLois Curfman McInnes   ierr = MatGetInfo(B,MAT_LOCAL,info);CHKERRQ(ierr);
11634e220ebcSLois Curfman McInnes   isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
11644e220ebcSLois Curfman McInnes   isend[3] += info->memory;  isend[4] += info->mallocs;
1165a66be287SLois Curfman McInnes   if (flag == MAT_LOCAL) {
11664e220ebcSLois Curfman McInnes     info->nz_used      = isend[0];
11674e220ebcSLois Curfman McInnes     info->nz_allocated = isend[1];
11684e220ebcSLois Curfman McInnes     info->nz_unneeded  = isend[2];
11694e220ebcSLois Curfman McInnes     info->memory       = isend[3];
11704e220ebcSLois Curfman McInnes     info->mallocs      = isend[4];
1171a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_MAX) {
1172d7d1e502SBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_MAX,matin->comm);CHKERRQ(ierr);
11734e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
11744e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
11754e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
11764e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
11774e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1178a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_SUM) {
1179d7d1e502SBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_SUM,matin->comm);CHKERRQ(ierr);
11804e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
11814e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
11824e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
11834e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
11844e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1185a66be287SLois Curfman McInnes   }
11864e220ebcSLois Curfman McInnes   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
11874e220ebcSLois Curfman McInnes   info->fill_ratio_needed = 0;
11884e220ebcSLois Curfman McInnes   info->factor_mallocs    = 0;
1189899cda47SBarry Smith   info->rows_global       = (double)matin->rmap.N;
1190899cda47SBarry Smith   info->columns_global    = (double)matin->cmap.N;
1191899cda47SBarry Smith   info->rows_local        = (double)matin->rmap.n;
1192899cda47SBarry Smith   info->columns_local     = (double)matin->cmap.N;
11934e220ebcSLois Curfman McInnes 
11943a40ed3dSBarry Smith   PetscFunctionReturn(0);
1195a66be287SLois Curfman McInnes }
1196a66be287SLois Curfman McInnes 
11974a2ae208SSatish Balay #undef __FUNCT__
11984a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_MPIAIJ"
1199dfbe8321SBarry Smith PetscErrorCode MatSetOption_MPIAIJ(Mat A,MatOption op)
1200c74985f6SBarry Smith {
1201c0bbcb79SLois Curfman McInnes   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
1202dfbe8321SBarry Smith   PetscErrorCode ierr;
1203c74985f6SBarry Smith 
12043a40ed3dSBarry Smith   PetscFunctionBegin;
120512c028f9SKris Buschelman   switch (op) {
120612c028f9SKris Buschelman   case MAT_NO_NEW_NONZERO_LOCATIONS:
120712c028f9SKris Buschelman   case MAT_YES_NEW_NONZERO_LOCATIONS:
120812c028f9SKris Buschelman   case MAT_COLUMNS_UNSORTED:
120912c028f9SKris Buschelman   case MAT_COLUMNS_SORTED:
121012c028f9SKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
121112c028f9SKris Buschelman   case MAT_KEEP_ZEROED_ROWS:
121212c028f9SKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
121312c028f9SKris Buschelman   case MAT_USE_INODES:
121412c028f9SKris Buschelman   case MAT_DO_NOT_USE_INODES:
121512c028f9SKris Buschelman   case MAT_IGNORE_ZERO_ENTRIES:
12161dee9f54SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
12171dee9f54SBarry Smith     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
121812c028f9SKris Buschelman     break;
121912c028f9SKris Buschelman   case MAT_ROW_ORIENTED:
12207c922b88SBarry Smith     a->roworiented = PETSC_TRUE;
12211dee9f54SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
12221dee9f54SBarry Smith     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
122312c028f9SKris Buschelman     break;
122412c028f9SKris Buschelman   case MAT_ROWS_SORTED:
122512c028f9SKris Buschelman   case MAT_ROWS_UNSORTED:
122612c028f9SKris Buschelman   case MAT_YES_NEW_DIAGONALS:
1227ae15b995SBarry Smith     ierr = PetscInfo(A,"Option ignored\n");CHKERRQ(ierr);
122812c028f9SKris Buschelman     break;
122912c028f9SKris Buschelman   case MAT_COLUMN_ORIENTED:
12307c922b88SBarry Smith     a->roworiented = PETSC_FALSE;
12311dee9f54SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
12321dee9f54SBarry Smith     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
123312c028f9SKris Buschelman     break;
123412c028f9SKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
12357c922b88SBarry Smith     a->donotstash = PETSC_TRUE;
123612c028f9SKris Buschelman     break;
123712c028f9SKris Buschelman   case MAT_NO_NEW_DIAGONALS:
123829bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"MAT_NO_NEW_DIAGONALS");
123977e54ba9SKris Buschelman   case MAT_SYMMETRIC:
124025f421beSHong Zhang     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
124125f421beSHong Zhang     break;
124277e54ba9SKris Buschelman   case MAT_STRUCTURALLY_SYMMETRIC:
1243bf108f30SBarry Smith   case MAT_HERMITIAN:
1244bf108f30SBarry Smith   case MAT_SYMMETRY_ETERNAL:
1245bf108f30SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
1246bf108f30SBarry Smith     break;
12479a4540c5SBarry Smith   case MAT_NOT_SYMMETRIC:
12489a4540c5SBarry Smith   case MAT_NOT_STRUCTURALLY_SYMMETRIC:
12499a4540c5SBarry Smith   case MAT_NOT_HERMITIAN:
12509a4540c5SBarry Smith   case MAT_NOT_SYMMETRY_ETERNAL:
125177e54ba9SKris Buschelman     break;
125212c028f9SKris Buschelman   default:
1253ad86a440SBarry Smith     SETERRQ1(PETSC_ERR_SUP,"unknown option %d",op);
12543a40ed3dSBarry Smith   }
12553a40ed3dSBarry Smith   PetscFunctionReturn(0);
1256c74985f6SBarry Smith }
1257c74985f6SBarry Smith 
12584a2ae208SSatish Balay #undef __FUNCT__
12594a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_MPIAIJ"
1260b1d57f15SBarry Smith PetscErrorCode MatGetRow_MPIAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
126139e00950SLois Curfman McInnes {
1262154123eaSLois Curfman McInnes   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)matin->data;
126387828ca2SBarry Smith   PetscScalar    *vworkA,*vworkB,**pvA,**pvB,*v_p;
12646849ba73SBarry Smith   PetscErrorCode ierr;
1265899cda47SBarry Smith   PetscInt       i,*cworkA,*cworkB,**pcA,**pcB,cstart = matin->cmap.rstart;
1266899cda47SBarry Smith   PetscInt       nztot,nzA,nzB,lrow,rstart = matin->rmap.rstart,rend = matin->rmap.rend;
1267b1d57f15SBarry Smith   PetscInt       *cmap,*idx_p;
126839e00950SLois Curfman McInnes 
12693a40ed3dSBarry Smith   PetscFunctionBegin;
1270abc0a331SBarry Smith   if (mat->getrowactive) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Already active");
12717a0afa10SBarry Smith   mat->getrowactive = PETSC_TRUE;
12727a0afa10SBarry Smith 
127370f0671dSBarry Smith   if (!mat->rowvalues && (idx || v)) {
12747a0afa10SBarry Smith     /*
12757a0afa10SBarry Smith         allocate enough space to hold information from the longest row.
12767a0afa10SBarry Smith     */
12777a0afa10SBarry Smith     Mat_SeqAIJ *Aa = (Mat_SeqAIJ*)mat->A->data,*Ba = (Mat_SeqAIJ*)mat->B->data;
1278b1d57f15SBarry Smith     PetscInt     max = 1,tmp;
1279899cda47SBarry Smith     for (i=0; i<matin->rmap.n; i++) {
12807a0afa10SBarry Smith       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
12817a0afa10SBarry Smith       if (max < tmp) { max = tmp; }
12827a0afa10SBarry Smith     }
1283b1d57f15SBarry Smith     ierr = PetscMalloc(max*(sizeof(PetscInt)+sizeof(PetscScalar)),&mat->rowvalues);CHKERRQ(ierr);
1284b1d57f15SBarry Smith     mat->rowindices = (PetscInt*)(mat->rowvalues + max);
12857a0afa10SBarry Smith   }
12867a0afa10SBarry Smith 
128729bbc08cSBarry Smith   if (row < rstart || row >= rend) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Only local rows")
1288abc0e9e4SLois Curfman McInnes   lrow = row - rstart;
128939e00950SLois Curfman McInnes 
1290154123eaSLois Curfman McInnes   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1291154123eaSLois Curfman McInnes   if (!v)   {pvA = 0; pvB = 0;}
1292154123eaSLois Curfman McInnes   if (!idx) {pcA = 0; if (!v) pcB = 0;}
1293f830108cSBarry Smith   ierr = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1294f830108cSBarry Smith   ierr = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
1295154123eaSLois Curfman McInnes   nztot = nzA + nzB;
1296154123eaSLois Curfman McInnes 
129770f0671dSBarry Smith   cmap  = mat->garray;
1298154123eaSLois Curfman McInnes   if (v  || idx) {
1299154123eaSLois Curfman McInnes     if (nztot) {
1300154123eaSLois Curfman McInnes       /* Sort by increasing column numbers, assuming A and B already sorted */
1301b1d57f15SBarry Smith       PetscInt imark = -1;
1302154123eaSLois Curfman McInnes       if (v) {
130370f0671dSBarry Smith         *v = v_p = mat->rowvalues;
130439e00950SLois Curfman McInnes         for (i=0; i<nzB; i++) {
130570f0671dSBarry Smith           if (cmap[cworkB[i]] < cstart)   v_p[i] = vworkB[i];
1306154123eaSLois Curfman McInnes           else break;
1307154123eaSLois Curfman McInnes         }
1308154123eaSLois Curfman McInnes         imark = i;
130970f0671dSBarry Smith         for (i=0; i<nzA; i++)     v_p[imark+i] = vworkA[i];
131070f0671dSBarry Smith         for (i=imark; i<nzB; i++) v_p[nzA+i]   = vworkB[i];
1311154123eaSLois Curfman McInnes       }
1312154123eaSLois Curfman McInnes       if (idx) {
131370f0671dSBarry Smith         *idx = idx_p = mat->rowindices;
131470f0671dSBarry Smith         if (imark > -1) {
131570f0671dSBarry Smith           for (i=0; i<imark; i++) {
131670f0671dSBarry Smith             idx_p[i] = cmap[cworkB[i]];
131770f0671dSBarry Smith           }
131870f0671dSBarry Smith         } else {
1319154123eaSLois Curfman McInnes           for (i=0; i<nzB; i++) {
132070f0671dSBarry Smith             if (cmap[cworkB[i]] < cstart)   idx_p[i] = cmap[cworkB[i]];
1321154123eaSLois Curfman McInnes             else break;
1322154123eaSLois Curfman McInnes           }
1323154123eaSLois Curfman McInnes           imark = i;
132470f0671dSBarry Smith         }
132570f0671dSBarry Smith         for (i=0; i<nzA; i++)     idx_p[imark+i] = cstart + cworkA[i];
132670f0671dSBarry Smith         for (i=imark; i<nzB; i++) idx_p[nzA+i]   = cmap[cworkB[i]];
132739e00950SLois Curfman McInnes       }
13283f97c4b0SBarry Smith     } else {
13291ca473b0SSatish Balay       if (idx) *idx = 0;
13301ca473b0SSatish Balay       if (v)   *v   = 0;
13311ca473b0SSatish Balay     }
1332154123eaSLois Curfman McInnes   }
133339e00950SLois Curfman McInnes   *nz = nztot;
1334f830108cSBarry Smith   ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1335f830108cSBarry Smith   ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
13363a40ed3dSBarry Smith   PetscFunctionReturn(0);
133739e00950SLois Curfman McInnes }
133839e00950SLois Curfman McInnes 
13394a2ae208SSatish Balay #undef __FUNCT__
13404a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_MPIAIJ"
1341b1d57f15SBarry Smith PetscErrorCode MatRestoreRow_MPIAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
134239e00950SLois Curfman McInnes {
13437a0afa10SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
13443a40ed3dSBarry Smith 
13453a40ed3dSBarry Smith   PetscFunctionBegin;
1346abc0a331SBarry Smith   if (!aij->getrowactive) {
1347abc0a331SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"MatGetRow() must be called first");
13487a0afa10SBarry Smith   }
13497a0afa10SBarry Smith   aij->getrowactive = PETSC_FALSE;
13503a40ed3dSBarry Smith   PetscFunctionReturn(0);
135139e00950SLois Curfman McInnes }
135239e00950SLois Curfman McInnes 
13534a2ae208SSatish Balay #undef __FUNCT__
13544a2ae208SSatish Balay #define __FUNCT__ "MatNorm_MPIAIJ"
1355dfbe8321SBarry Smith PetscErrorCode MatNorm_MPIAIJ(Mat mat,NormType type,PetscReal *norm)
1356855ac2c5SLois Curfman McInnes {
1357855ac2c5SLois Curfman McInnes   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
1358ec8511deSBarry Smith   Mat_SeqAIJ     *amat = (Mat_SeqAIJ*)aij->A->data,*bmat = (Mat_SeqAIJ*)aij->B->data;
1359dfbe8321SBarry Smith   PetscErrorCode ierr;
1360899cda47SBarry Smith   PetscInt       i,j,cstart = mat->cmap.rstart;
1361329f5518SBarry Smith   PetscReal      sum = 0.0;
136287828ca2SBarry Smith   PetscScalar    *v;
136304ca555eSLois Curfman McInnes 
13643a40ed3dSBarry Smith   PetscFunctionBegin;
136517699dbbSLois Curfman McInnes   if (aij->size == 1) {
136614183eadSLois Curfman McInnes     ierr =  MatNorm(aij->A,type,norm);CHKERRQ(ierr);
136737fa93a5SLois Curfman McInnes   } else {
136804ca555eSLois Curfman McInnes     if (type == NORM_FROBENIUS) {
136904ca555eSLois Curfman McInnes       v = amat->a;
137004ca555eSLois Curfman McInnes       for (i=0; i<amat->nz; i++) {
1371aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
1372329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
137304ca555eSLois Curfman McInnes #else
137404ca555eSLois Curfman McInnes         sum += (*v)*(*v); v++;
137504ca555eSLois Curfman McInnes #endif
137604ca555eSLois Curfman McInnes       }
137704ca555eSLois Curfman McInnes       v = bmat->a;
137804ca555eSLois Curfman McInnes       for (i=0; i<bmat->nz; i++) {
1379aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
1380329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
138104ca555eSLois Curfman McInnes #else
138204ca555eSLois Curfman McInnes         sum += (*v)*(*v); v++;
138304ca555eSLois Curfman McInnes #endif
138404ca555eSLois Curfman McInnes       }
1385d7d1e502SBarry Smith       ierr = MPI_Allreduce(&sum,norm,1,MPIU_REAL,MPI_SUM,mat->comm);CHKERRQ(ierr);
138604ca555eSLois Curfman McInnes       *norm = sqrt(*norm);
13873a40ed3dSBarry Smith     } else if (type == NORM_1) { /* max column norm */
1388329f5518SBarry Smith       PetscReal *tmp,*tmp2;
1389b1d57f15SBarry Smith       PetscInt    *jj,*garray = aij->garray;
1390899cda47SBarry Smith       ierr = PetscMalloc((mat->cmap.N+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1391899cda47SBarry Smith       ierr = PetscMalloc((mat->cmap.N+1)*sizeof(PetscReal),&tmp2);CHKERRQ(ierr);
1392899cda47SBarry Smith       ierr = PetscMemzero(tmp,mat->cmap.N*sizeof(PetscReal));CHKERRQ(ierr);
139304ca555eSLois Curfman McInnes       *norm = 0.0;
139404ca555eSLois Curfman McInnes       v = amat->a; jj = amat->j;
139504ca555eSLois Curfman McInnes       for (j=0; j<amat->nz; j++) {
1396bfec09a0SHong Zhang         tmp[cstart + *jj++ ] += PetscAbsScalar(*v);  v++;
139704ca555eSLois Curfman McInnes       }
139804ca555eSLois Curfman McInnes       v = bmat->a; jj = bmat->j;
139904ca555eSLois Curfman McInnes       for (j=0; j<bmat->nz; j++) {
1400bfec09a0SHong Zhang         tmp[garray[*jj++]] += PetscAbsScalar(*v); v++;
140104ca555eSLois Curfman McInnes       }
1402899cda47SBarry Smith       ierr = MPI_Allreduce(tmp,tmp2,mat->cmap.N,MPIU_REAL,MPI_SUM,mat->comm);CHKERRQ(ierr);
1403899cda47SBarry Smith       for (j=0; j<mat->cmap.N; j++) {
140404ca555eSLois Curfman McInnes         if (tmp2[j] > *norm) *norm = tmp2[j];
140504ca555eSLois Curfman McInnes       }
1406606d414cSSatish Balay       ierr = PetscFree(tmp);CHKERRQ(ierr);
1407606d414cSSatish Balay       ierr = PetscFree(tmp2);CHKERRQ(ierr);
14083a40ed3dSBarry Smith     } else if (type == NORM_INFINITY) { /* max row norm */
1409329f5518SBarry Smith       PetscReal ntemp = 0.0;
1410899cda47SBarry Smith       for (j=0; j<aij->A->rmap.n; j++) {
1411bfec09a0SHong Zhang         v = amat->a + amat->i[j];
141204ca555eSLois Curfman McInnes         sum = 0.0;
141304ca555eSLois Curfman McInnes         for (i=0; i<amat->i[j+1]-amat->i[j]; i++) {
1414cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
141504ca555eSLois Curfman McInnes         }
1416bfec09a0SHong Zhang         v = bmat->a + bmat->i[j];
141704ca555eSLois Curfman McInnes         for (i=0; i<bmat->i[j+1]-bmat->i[j]; i++) {
1418cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
141904ca555eSLois Curfman McInnes         }
1420515d9167SLois Curfman McInnes         if (sum > ntemp) ntemp = sum;
142104ca555eSLois Curfman McInnes       }
1422d7d1e502SBarry Smith       ierr = MPI_Allreduce(&ntemp,norm,1,MPIU_REAL,MPI_MAX,mat->comm);CHKERRQ(ierr);
1423ca161407SBarry Smith     } else {
142429bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"No support for two norm");
142504ca555eSLois Curfman McInnes     }
142637fa93a5SLois Curfman McInnes   }
14273a40ed3dSBarry Smith   PetscFunctionReturn(0);
1428855ac2c5SLois Curfman McInnes }
1429855ac2c5SLois Curfman McInnes 
14304a2ae208SSatish Balay #undef __FUNCT__
14314a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_MPIAIJ"
1432dfbe8321SBarry Smith PetscErrorCode MatTranspose_MPIAIJ(Mat A,Mat *matout)
1433b7c46309SBarry Smith {
1434b7c46309SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
1435dbb450caSBarry Smith   Mat_SeqAIJ     *Aloc = (Mat_SeqAIJ*)a->A->data;
1436dfbe8321SBarry Smith   PetscErrorCode ierr;
1437899cda47SBarry Smith   PetscInt       M = A->rmap.N,N = A->cmap.N,m,*ai,*aj,row,*cols,i,*ct;
14383a40ed3dSBarry Smith   Mat            B;
143987828ca2SBarry Smith   PetscScalar    *array;
1440b7c46309SBarry Smith 
14413a40ed3dSBarry Smith   PetscFunctionBegin;
14427c922b88SBarry Smith   if (!matout && M != N) {
144329bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1444d4bb536fSBarry Smith   }
1445d4bb536fSBarry Smith 
1446f69a0ea3SMatthew Knepley   ierr = MatCreate(A->comm,&B);CHKERRQ(ierr);
1447899cda47SBarry Smith   ierr = MatSetSizes(B,A->cmap.n,A->rmap.n,N,M);CHKERRQ(ierr);
1448f204ca49SKris Buschelman   ierr = MatSetType(B,A->type_name);CHKERRQ(ierr);
1449f204ca49SKris Buschelman   ierr = MatMPIAIJSetPreallocation(B,0,PETSC_NULL,0,PETSC_NULL);CHKERRQ(ierr);
1450b7c46309SBarry Smith 
1451b7c46309SBarry Smith   /* copy over the A part */
1452ec8511deSBarry Smith   Aloc = (Mat_SeqAIJ*)a->A->data;
1453899cda47SBarry Smith   m = a->A->rmap.n; ai = Aloc->i; aj = Aloc->j; array = Aloc->a;
1454899cda47SBarry Smith   row = A->rmap.rstart;
1455899cda47SBarry Smith   for (i=0; i<ai[m]; i++) {aj[i] += A->cmap.rstart ;}
1456b7c46309SBarry Smith   for (i=0; i<m; i++) {
1457416022c9SBarry Smith     ierr = MatSetValues(B,ai[i+1]-ai[i],aj,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
1458b7c46309SBarry Smith     row++; array += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
1459b7c46309SBarry Smith   }
1460b7c46309SBarry Smith   aj = Aloc->j;
1461899cda47SBarry Smith   for (i=0; i<ai[m]; i++) {aj[i] -= A->cmap.rstart ;}
1462b7c46309SBarry Smith 
1463b7c46309SBarry Smith   /* copy over the B part */
1464ec8511deSBarry Smith   Aloc = (Mat_SeqAIJ*)a->B->data;
1465899cda47SBarry Smith   m = a->B->rmap.n;  ai = Aloc->i; aj = Aloc->j; array = Aloc->a;
1466899cda47SBarry Smith   row  = A->rmap.rstart;
1467b1d57f15SBarry Smith   ierr = PetscMalloc((1+ai[m])*sizeof(PetscInt),&cols);CHKERRQ(ierr);
1468b0a32e0cSBarry Smith   ct   = cols;
1469bfec09a0SHong Zhang   for (i=0; i<ai[m]; i++) {cols[i] = a->garray[aj[i]];}
1470b7c46309SBarry Smith   for (i=0; i<m; i++) {
1471416022c9SBarry Smith     ierr = MatSetValues(B,ai[i+1]-ai[i],cols,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
1472b7c46309SBarry Smith     row++; array += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
1473b7c46309SBarry Smith   }
1474606d414cSSatish Balay   ierr = PetscFree(ct);CHKERRQ(ierr);
14756d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14766d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14777c922b88SBarry Smith   if (matout) {
14780de55854SLois Curfman McInnes     *matout = B;
14790de55854SLois Curfman McInnes   } else {
1480273d9f13SBarry Smith     ierr = MatHeaderCopy(A,B);CHKERRQ(ierr);
14810de55854SLois Curfman McInnes   }
14823a40ed3dSBarry Smith   PetscFunctionReturn(0);
1483b7c46309SBarry Smith }
1484b7c46309SBarry Smith 
14854a2ae208SSatish Balay #undef __FUNCT__
14864a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_MPIAIJ"
1487dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr)
1488a008b906SSatish Balay {
14894b967eb1SSatish Balay   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
14904b967eb1SSatish Balay   Mat            a = aij->A,b = aij->B;
1491dfbe8321SBarry Smith   PetscErrorCode ierr;
1492b1d57f15SBarry Smith   PetscInt       s1,s2,s3;
1493a008b906SSatish Balay 
14943a40ed3dSBarry Smith   PetscFunctionBegin;
14954b967eb1SSatish Balay   ierr = MatGetLocalSize(mat,&s2,&s3);CHKERRQ(ierr);
14964b967eb1SSatish Balay   if (rr) {
1497e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr);
149829bbc08cSBarry Smith     if (s1!=s3) SETERRQ(PETSC_ERR_ARG_SIZ,"right vector non-conforming local size");
14994b967eb1SSatish Balay     /* Overlap communication with computation. */
150043a90d84SBarry Smith     ierr = VecScatterBegin(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx);CHKERRQ(ierr);
1501a008b906SSatish Balay   }
15024b967eb1SSatish Balay   if (ll) {
1503e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr);
150429bbc08cSBarry Smith     if (s1!=s2) SETERRQ(PETSC_ERR_ARG_SIZ,"left vector non-conforming local size");
1505f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,ll,0);CHKERRQ(ierr);
15064b967eb1SSatish Balay   }
15074b967eb1SSatish Balay   /* scale  the diagonal block */
1508f830108cSBarry Smith   ierr = (*a->ops->diagonalscale)(a,ll,rr);CHKERRQ(ierr);
15094b967eb1SSatish Balay 
15104b967eb1SSatish Balay   if (rr) {
15114b967eb1SSatish Balay     /* Do a scatter end and then right scale the off-diagonal block */
151243a90d84SBarry Smith     ierr = VecScatterEnd(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx);CHKERRQ(ierr);
1513f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,0,aij->lvec);CHKERRQ(ierr);
15144b967eb1SSatish Balay   }
15154b967eb1SSatish Balay 
15163a40ed3dSBarry Smith   PetscFunctionReturn(0);
1517a008b906SSatish Balay }
1518a008b906SSatish Balay 
15194a2ae208SSatish Balay #undef __FUNCT__
1520521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_MPIAIJ"
1521521d7252SBarry Smith PetscErrorCode MatSetBlockSize_MPIAIJ(Mat A,PetscInt bs)
15225a838052SSatish Balay {
1523521d7252SBarry Smith   Mat_MPIAIJ     *a   = (Mat_MPIAIJ*)A->data;
1524521d7252SBarry Smith   PetscErrorCode ierr;
1525521d7252SBarry Smith 
15263a40ed3dSBarry Smith   PetscFunctionBegin;
1527521d7252SBarry Smith   ierr = MatSetBlockSize(a->A,bs);CHKERRQ(ierr);
1528521d7252SBarry Smith   ierr = MatSetBlockSize(a->B,bs);CHKERRQ(ierr);
15293a40ed3dSBarry Smith   PetscFunctionReturn(0);
15305a838052SSatish Balay }
15314a2ae208SSatish Balay #undef __FUNCT__
15324a2ae208SSatish Balay #define __FUNCT__ "MatSetUnfactored_MPIAIJ"
1533dfbe8321SBarry Smith PetscErrorCode MatSetUnfactored_MPIAIJ(Mat A)
1534bb5a7306SBarry Smith {
1535bb5a7306SBarry Smith   Mat_MPIAIJ     *a   = (Mat_MPIAIJ*)A->data;
1536dfbe8321SBarry Smith   PetscErrorCode ierr;
15373a40ed3dSBarry Smith 
15383a40ed3dSBarry Smith   PetscFunctionBegin;
1539bb5a7306SBarry Smith   ierr = MatSetUnfactored(a->A);CHKERRQ(ierr);
15403a40ed3dSBarry Smith   PetscFunctionReturn(0);
1541bb5a7306SBarry Smith }
1542bb5a7306SBarry Smith 
15434a2ae208SSatish Balay #undef __FUNCT__
15444a2ae208SSatish Balay #define __FUNCT__ "MatEqual_MPIAIJ"
1545dfbe8321SBarry Smith PetscErrorCode MatEqual_MPIAIJ(Mat A,Mat B,PetscTruth *flag)
1546d4bb536fSBarry Smith {
1547d4bb536fSBarry Smith   Mat_MPIAIJ     *matB = (Mat_MPIAIJ*)B->data,*matA = (Mat_MPIAIJ*)A->data;
1548d4bb536fSBarry Smith   Mat            a,b,c,d;
1549d4bb536fSBarry Smith   PetscTruth     flg;
1550dfbe8321SBarry Smith   PetscErrorCode ierr;
1551d4bb536fSBarry Smith 
15523a40ed3dSBarry Smith   PetscFunctionBegin;
1553d4bb536fSBarry Smith   a = matA->A; b = matA->B;
1554d4bb536fSBarry Smith   c = matB->A; d = matB->B;
1555d4bb536fSBarry Smith 
1556d4bb536fSBarry Smith   ierr = MatEqual(a,c,&flg);CHKERRQ(ierr);
1557abc0a331SBarry Smith   if (flg) {
1558d4bb536fSBarry Smith     ierr = MatEqual(b,d,&flg);CHKERRQ(ierr);
1559d4bb536fSBarry Smith   }
1560ca161407SBarry Smith   ierr = MPI_Allreduce(&flg,flag,1,MPI_INT,MPI_LAND,A->comm);CHKERRQ(ierr);
15613a40ed3dSBarry Smith   PetscFunctionReturn(0);
1562d4bb536fSBarry Smith }
1563d4bb536fSBarry Smith 
15644a2ae208SSatish Balay #undef __FUNCT__
15654a2ae208SSatish Balay #define __FUNCT__ "MatCopy_MPIAIJ"
1566dfbe8321SBarry Smith PetscErrorCode MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str)
1567cb5b572fSBarry Smith {
1568dfbe8321SBarry Smith   PetscErrorCode ierr;
1569cb5b572fSBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ *)A->data;
1570cb5b572fSBarry Smith   Mat_MPIAIJ     *b = (Mat_MPIAIJ *)B->data;
1571cb5b572fSBarry Smith 
1572cb5b572fSBarry Smith   PetscFunctionBegin;
157333f4a19fSKris Buschelman   /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
157433f4a19fSKris Buschelman   if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) {
1575cb5b572fSBarry Smith     /* because of the column compression in the off-processor part of the matrix a->B,
1576cb5b572fSBarry Smith        the number of columns in a->B and b->B may be different, hence we cannot call
1577cb5b572fSBarry Smith        the MatCopy() directly on the two parts. If need be, we can provide a more
1578cb5b572fSBarry Smith        efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices
1579cb5b572fSBarry Smith        then copying the submatrices */
1580cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1581cb5b572fSBarry Smith   } else {
1582cb5b572fSBarry Smith     ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr);
1583cb5b572fSBarry Smith     ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr);
1584cb5b572fSBarry Smith   }
1585cb5b572fSBarry Smith   PetscFunctionReturn(0);
1586cb5b572fSBarry Smith }
1587cb5b572fSBarry Smith 
15884a2ae208SSatish Balay #undef __FUNCT__
15894a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_MPIAIJ"
1590dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_MPIAIJ(Mat A)
1591273d9f13SBarry Smith {
1592dfbe8321SBarry Smith   PetscErrorCode ierr;
1593273d9f13SBarry Smith 
1594273d9f13SBarry Smith   PetscFunctionBegin;
1595273d9f13SBarry Smith   ierr =  MatMPIAIJSetPreallocation(A,PETSC_DEFAULT,0,PETSC_DEFAULT,0);CHKERRQ(ierr);
1596273d9f13SBarry Smith   PetscFunctionReturn(0);
1597273d9f13SBarry Smith }
1598273d9f13SBarry Smith 
1599ac90fabeSBarry Smith #include "petscblaslapack.h"
1600ac90fabeSBarry Smith #undef __FUNCT__
1601ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_MPIAIJ"
1602f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_MPIAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
1603ac90fabeSBarry Smith {
1604dfbe8321SBarry Smith   PetscErrorCode ierr;
1605b1d57f15SBarry Smith   PetscInt       i;
1606ac90fabeSBarry Smith   Mat_MPIAIJ     *xx = (Mat_MPIAIJ *)X->data,*yy = (Mat_MPIAIJ *)Y->data;
16074ce68768SBarry Smith   PetscBLASInt   bnz,one=1;
1608ac90fabeSBarry Smith   Mat_SeqAIJ     *x,*y;
1609ac90fabeSBarry Smith 
1610ac90fabeSBarry Smith   PetscFunctionBegin;
1611ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
1612f4df32b1SMatthew Knepley     PetscScalar alpha = a;
1613ac90fabeSBarry Smith     x = (Mat_SeqAIJ *)xx->A->data;
1614ac90fabeSBarry Smith     y = (Mat_SeqAIJ *)yy->A->data;
16154ce68768SBarry Smith     bnz = (PetscBLASInt)x->nz;
1616f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
1617ac90fabeSBarry Smith     x = (Mat_SeqAIJ *)xx->B->data;
1618ac90fabeSBarry Smith     y = (Mat_SeqAIJ *)yy->B->data;
16194ce68768SBarry Smith     bnz = (PetscBLASInt)x->nz;
1620f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
1621a30b2313SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) {
1622f4df32b1SMatthew Knepley     ierr = MatAXPY_SeqAIJ(yy->A,a,xx->A,str);CHKERRQ(ierr);
1623c537a176SHong Zhang 
1624c537a176SHong Zhang     x = (Mat_SeqAIJ *)xx->B->data;
1625a30b2313SHong Zhang     y = (Mat_SeqAIJ *)yy->B->data;
1626a30b2313SHong Zhang     if (y->xtoy && y->XtoY != xx->B) {
1627a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
1628a30b2313SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
1629c537a176SHong Zhang     }
1630a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
1631899cda47SBarry Smith       ierr = MatAXPYGetxtoy_Private(xx->B->rmap.n,x->i,x->j,xx->garray,y->i,y->j,yy->garray,&y->xtoy);CHKERRQ(ierr);
1632a30b2313SHong Zhang       y->XtoY = xx->B;
1633c537a176SHong Zhang     }
1634f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
1635ac90fabeSBarry Smith   } else {
1636f4df32b1SMatthew Knepley     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
1637ac90fabeSBarry Smith   }
1638ac90fabeSBarry Smith   PetscFunctionReturn(0);
1639ac90fabeSBarry Smith }
1640ac90fabeSBarry Smith 
1641354c94deSBarry Smith EXTERN PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_SeqAIJ(Mat);
1642354c94deSBarry Smith 
1643354c94deSBarry Smith #undef __FUNCT__
1644354c94deSBarry Smith #define __FUNCT__ "MatConjugate_MPIAIJ"
1645354c94deSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_MPIAIJ(Mat mat)
1646354c94deSBarry Smith {
1647354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
1648354c94deSBarry Smith   PetscErrorCode ierr;
1649354c94deSBarry Smith   Mat_MPIAIJ     *aij = (Mat_MPIAIJ *)mat->data;
1650354c94deSBarry Smith 
1651354c94deSBarry Smith   PetscFunctionBegin;
1652354c94deSBarry Smith   ierr = MatConjugate_SeqAIJ(aij->A);CHKERRQ(ierr);
1653354c94deSBarry Smith   ierr = MatConjugate_SeqAIJ(aij->B);CHKERRQ(ierr);
1654354c94deSBarry Smith #else
1655354c94deSBarry Smith   PetscFunctionBegin;
1656354c94deSBarry Smith #endif
1657354c94deSBarry Smith   PetscFunctionReturn(0);
1658354c94deSBarry Smith }
1659354c94deSBarry Smith 
166099cafbc1SBarry Smith #undef __FUNCT__
166199cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_MPIAIJ"
166299cafbc1SBarry Smith PetscErrorCode MatRealPart_MPIAIJ(Mat A)
166399cafbc1SBarry Smith {
166499cafbc1SBarry Smith   Mat_MPIAIJ   *a = (Mat_MPIAIJ*)A->data;
166599cafbc1SBarry Smith   PetscErrorCode ierr;
166699cafbc1SBarry Smith 
166799cafbc1SBarry Smith   PetscFunctionBegin;
166899cafbc1SBarry Smith   ierr = MatRealPart(a->A);CHKERRQ(ierr);
166999cafbc1SBarry Smith   ierr = MatRealPart(a->B);CHKERRQ(ierr);
167099cafbc1SBarry Smith   PetscFunctionReturn(0);
167199cafbc1SBarry Smith }
167299cafbc1SBarry Smith 
167399cafbc1SBarry Smith #undef __FUNCT__
167499cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_MPIAIJ"
167599cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_MPIAIJ(Mat A)
167699cafbc1SBarry Smith {
167799cafbc1SBarry Smith   Mat_MPIAIJ   *a = (Mat_MPIAIJ*)A->data;
167899cafbc1SBarry Smith   PetscErrorCode ierr;
167999cafbc1SBarry Smith 
168099cafbc1SBarry Smith   PetscFunctionBegin;
168199cafbc1SBarry Smith   ierr = MatImaginaryPart(a->A);CHKERRQ(ierr);
168299cafbc1SBarry Smith   ierr = MatImaginaryPart(a->B);CHKERRQ(ierr);
168399cafbc1SBarry Smith   PetscFunctionReturn(0);
168499cafbc1SBarry Smith }
168599cafbc1SBarry Smith 
1686*103bf8bdSMatthew Knepley #ifdef PETSC_HAVE_PBGL
1687*103bf8bdSMatthew Knepley #include <boost/parallel/mpi/bsp_process_group.hpp>
1688*103bf8bdSMatthew Knepley typedef boost::parallel::mpi::bsp_process_group            process_group_type;
1689*103bf8bdSMatthew Knepley 
1690*103bf8bdSMatthew Knepley #include <boost/graph/distributed/adjacency_list.hpp>
1691*103bf8bdSMatthew Knepley #include <boost/parallel/mpi/bsp_process_group.hpp>
1692*103bf8bdSMatthew Knepley 
1693*103bf8bdSMatthew Knepley #include <boost/graph/distributed/petsc/interface.hpp>
1694*103bf8bdSMatthew Knepley #include <boost/graph/distributed/ilu_0.hpp>
1695*103bf8bdSMatthew Knepley 
1696*103bf8bdSMatthew Knepley namespace petsc = boost::distributed::petsc;
1697*103bf8bdSMatthew Knepley using namespace std;
1698*103bf8bdSMatthew Knepley typedef double                                             value_type;
1699*103bf8bdSMatthew Knepley typedef boost::graph::distributed::ilu_elimination_state   elimination_state;
1700*103bf8bdSMatthew Knepley typedef boost::adjacency_list<boost::listS,
1701*103bf8bdSMatthew Knepley                        boost::distributedS<process_group_type, boost::vecS>,
1702*103bf8bdSMatthew Knepley                        boost::bidirectionalS,
1703*103bf8bdSMatthew Knepley                        // Vertex properties
1704*103bf8bdSMatthew Knepley                        boost::no_property,
1705*103bf8bdSMatthew Knepley                        // Edge properties
1706*103bf8bdSMatthew Knepley                        boost::property<boost::edge_weight_t, value_type,
1707*103bf8bdSMatthew Knepley                          boost::property<boost::edge_finished_t, elimination_state> > > graph_type;
1708*103bf8bdSMatthew Knepley 
1709*103bf8bdSMatthew Knepley typedef boost::graph_traits<graph_type>::vertex_descriptor        vertex_type;
1710*103bf8bdSMatthew Knepley typedef boost::graph_traits<graph_type>::edge_descriptor          edge_type;
1711*103bf8bdSMatthew Knepley typedef boost::property_map<graph_type, boost::edge_weight_t>::type      weight_map_type;
1712*103bf8bdSMatthew Knepley 
1713*103bf8bdSMatthew Knepley #undef __FUNCT__
1714*103bf8bdSMatthew Knepley #define __FUNCT__ "MatILUFactorSymbolic_MPIAIJ"
1715*103bf8bdSMatthew Knepley /*
1716*103bf8bdSMatthew Knepley   This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu>
1717*103bf8bdSMatthew Knepley */
1718*103bf8bdSMatthew Knepley PetscErrorCode MatILUFactorSymbolic_MPIAIJ(Mat A, IS isrow, IS iscol, MatFactorInfo *info, Mat *fact)
1719*103bf8bdSMatthew Knepley {
1720*103bf8bdSMatthew Knepley   Mat_MPIAIJ          *a = (Mat_MPIAIJ *) A->data;
1721*103bf8bdSMatthew Knepley   PetscTruth           row_identity, col_identity;
1722*103bf8bdSMatthew Knepley   PetscObjectContainer c;
1723*103bf8bdSMatthew Knepley   PetscInt             m, n, M, N;
1724*103bf8bdSMatthew Knepley   PetscErrorCode       ierr;
1725*103bf8bdSMatthew Knepley 
1726*103bf8bdSMatthew Knepley   PetscFunctionBegin;
1727*103bf8bdSMatthew Knepley   if (info->levels != 0) SETERRQ(PETSC_ERR_SUP,"Only levels = 0 supported for parallel ilu");
1728*103bf8bdSMatthew Knepley   ierr = ISIdentity(isrow, &row_identity);CHKERRQ(ierr);
1729*103bf8bdSMatthew Knepley   ierr = ISIdentity(iscol, &col_identity);CHKERRQ(ierr);
1730*103bf8bdSMatthew Knepley   if (!row_identity || !col_identity) {
1731*103bf8bdSMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONG,"Row and column permutations must be identity for parallel ILU");
1732*103bf8bdSMatthew Knepley   }
1733*103bf8bdSMatthew Knepley 
1734*103bf8bdSMatthew Knepley   process_group_type pg;
1735*103bf8bdSMatthew Knepley   graph_type*        graph_p = new graph_type(petsc::num_global_vertices(A), pg, petsc::matrix_distribution(A, pg));
1736*103bf8bdSMatthew Knepley   graph_type&        graph   = *graph_p;
1737*103bf8bdSMatthew Knepley   petsc::read_matrix(A, graph, get(boost::edge_weight, graph));
1738*103bf8bdSMatthew Knepley 
1739*103bf8bdSMatthew Knepley   //write_graphviz("petsc_matrix_as_graph.dot", graph, default_writer(), matrix_graph_writer<graph_type>(graph));
1740*103bf8bdSMatthew Knepley   boost::property_map<graph_type, boost::edge_finished_t>::type finished = get(boost::edge_finished, graph);
1741*103bf8bdSMatthew Knepley   BGL_FORALL_EDGES(e, graph, graph_type)
1742*103bf8bdSMatthew Knepley     put(finished, e, boost::graph::distributed::unseen);
1743*103bf8bdSMatthew Knepley 
1744*103bf8bdSMatthew Knepley   ilu_0(graph, get(boost::edge_weight, graph), get(boost::edge_finished, graph));
1745*103bf8bdSMatthew Knepley 
1746*103bf8bdSMatthew Knepley   /* put together the new matrix */
1747*103bf8bdSMatthew Knepley   ierr = MatCreate(A->comm, fact);CHKERRQ(ierr);
1748*103bf8bdSMatthew Knepley   ierr = MatGetLocalSize(A, &m, &n);CHKERRQ(ierr);
1749*103bf8bdSMatthew Knepley   ierr = MatGetSize(A, &M, &N);CHKERRQ(ierr);
1750*103bf8bdSMatthew Knepley   ierr = MatSetSizes(*fact, m, n, M, N);CHKERRQ(ierr);
1751*103bf8bdSMatthew Knepley   ierr = MatSetType(*fact, A->type_name);CHKERRQ(ierr);
1752*103bf8bdSMatthew Knepley   (*fact)->factor = FACTOR_LU;
1753*103bf8bdSMatthew Knepley 
1754*103bf8bdSMatthew Knepley   ierr = PetscObjectContainerCreate(A->comm, &c);
1755*103bf8bdSMatthew Knepley   ierr = PetscObjectContainerSetPointer(c, graph_p);
1756*103bf8bdSMatthew Knepley   ierr = PetscObjectCompose((PetscObject) (*fact), "graph", (PetscObject) c);
1757*103bf8bdSMatthew Knepley   PetscFunctionReturn(0);
1758*103bf8bdSMatthew Knepley }
1759*103bf8bdSMatthew Knepley 
1760*103bf8bdSMatthew Knepley #undef __FUNCT__
1761*103bf8bdSMatthew Knepley #define __FUNCT__ "MatLUFactorNumeric_MPIAIJ"
1762*103bf8bdSMatthew Knepley PetscErrorCode MatLUFactorNumeric_MPIAIJ(Mat A, MatFactorInfo *info, Mat *B)
1763*103bf8bdSMatthew Knepley {
1764*103bf8bdSMatthew Knepley   PetscFunctionBegin;
1765*103bf8bdSMatthew Knepley   PetscFunctionReturn(0);
1766*103bf8bdSMatthew Knepley }
1767*103bf8bdSMatthew Knepley 
1768*103bf8bdSMatthew Knepley #undef __FUNCT__
1769*103bf8bdSMatthew Knepley #define __FUNCT__ "MatSolve_MPIAIJ"
1770*103bf8bdSMatthew Knepley /*
1771*103bf8bdSMatthew Knepley   This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu>
1772*103bf8bdSMatthew Knepley */
1773*103bf8bdSMatthew Knepley PetscErrorCode MatSolve_MPIAIJ(Mat A, Vec b, Vec x)
1774*103bf8bdSMatthew Knepley {
1775*103bf8bdSMatthew Knepley   graph_type*          graph_p;
1776*103bf8bdSMatthew Knepley   PetscObjectContainer c;
1777*103bf8bdSMatthew Knepley   PetscErrorCode       ierr;
1778*103bf8bdSMatthew Knepley 
1779*103bf8bdSMatthew Knepley   PetscFunctionBegin;
1780*103bf8bdSMatthew Knepley   ierr = PetscObjectQuery((PetscObject) A, "graph", (PetscObject *) &c);CHKERRQ(ierr);
1781*103bf8bdSMatthew Knepley   ierr = PetscObjectContainerGetPointer(c, (void **) &graph_p);CHKERRQ(ierr);
1782*103bf8bdSMatthew Knepley   ierr = VecCopy(b, x); CHKERRQ(ierr);
1783*103bf8bdSMatthew Knepley   PetscFunctionReturn(0);
1784*103bf8bdSMatthew Knepley }
1785*103bf8bdSMatthew Knepley #endif
1786*103bf8bdSMatthew Knepley 
17878a729477SBarry Smith /* -------------------------------------------------------------------*/
1788cda55fadSBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ,
1789cda55fadSBarry Smith        MatGetRow_MPIAIJ,
1790cda55fadSBarry Smith        MatRestoreRow_MPIAIJ,
1791cda55fadSBarry Smith        MatMult_MPIAIJ,
179297304618SKris Buschelman /* 4*/ MatMultAdd_MPIAIJ,
17937c922b88SBarry Smith        MatMultTranspose_MPIAIJ,
17947c922b88SBarry Smith        MatMultTransposeAdd_MPIAIJ,
1795*103bf8bdSMatthew Knepley #ifdef PETSC_HAVE_PBGL
1796*103bf8bdSMatthew Knepley        MatSolve_MPIAIJ,
1797*103bf8bdSMatthew Knepley #else
1798cda55fadSBarry Smith        0,
1799*103bf8bdSMatthew Knepley #endif
1800cda55fadSBarry Smith        0,
1801cda55fadSBarry Smith        0,
180297304618SKris Buschelman /*10*/ 0,
1803cda55fadSBarry Smith        0,
1804cda55fadSBarry Smith        0,
180544a69424SLois Curfman McInnes        MatRelax_MPIAIJ,
1806b7c46309SBarry Smith        MatTranspose_MPIAIJ,
180797304618SKris Buschelman /*15*/ MatGetInfo_MPIAIJ,
1808cda55fadSBarry Smith        MatEqual_MPIAIJ,
1809cda55fadSBarry Smith        MatGetDiagonal_MPIAIJ,
1810cda55fadSBarry Smith        MatDiagonalScale_MPIAIJ,
1811cda55fadSBarry Smith        MatNorm_MPIAIJ,
181297304618SKris Buschelman /*20*/ MatAssemblyBegin_MPIAIJ,
1813cda55fadSBarry Smith        MatAssemblyEnd_MPIAIJ,
18141eb62cbbSBarry Smith        0,
1815cda55fadSBarry Smith        MatSetOption_MPIAIJ,
1816cda55fadSBarry Smith        MatZeroEntries_MPIAIJ,
181797304618SKris Buschelman /*25*/ MatZeroRows_MPIAIJ,
1818cda55fadSBarry Smith        0,
1819*103bf8bdSMatthew Knepley #ifdef PETSC_HAVE_PBGL
1820*103bf8bdSMatthew Knepley        MatLUFactorNumeric_MPIAIJ,
1821*103bf8bdSMatthew Knepley #else
1822cda55fadSBarry Smith        0,
1823*103bf8bdSMatthew Knepley #endif
1824cda55fadSBarry Smith        0,
1825cda55fadSBarry Smith        0,
182697304618SKris Buschelman /*30*/ MatSetUpPreallocation_MPIAIJ,
1827*103bf8bdSMatthew Knepley #ifdef PETSC_HAVE_PBGL
1828*103bf8bdSMatthew Knepley        MatILUFactorSymbolic_MPIAIJ,
1829*103bf8bdSMatthew Knepley #else
1830cda55fadSBarry Smith        0,
1831*103bf8bdSMatthew Knepley #endif
1832cda55fadSBarry Smith        0,
1833cda55fadSBarry Smith        0,
1834cda55fadSBarry Smith        0,
183597304618SKris Buschelman /*35*/ MatDuplicate_MPIAIJ,
1836cda55fadSBarry Smith        0,
1837cda55fadSBarry Smith        0,
1838cda55fadSBarry Smith        0,
1839cda55fadSBarry Smith        0,
184097304618SKris Buschelman /*40*/ MatAXPY_MPIAIJ,
1841cda55fadSBarry Smith        MatGetSubMatrices_MPIAIJ,
1842cda55fadSBarry Smith        MatIncreaseOverlap_MPIAIJ,
1843cda55fadSBarry Smith        MatGetValues_MPIAIJ,
1844cb5b572fSBarry Smith        MatCopy_MPIAIJ,
18458c07d4e3SBarry Smith /*45*/ 0,
1846cda55fadSBarry Smith        MatScale_MPIAIJ,
1847cda55fadSBarry Smith        0,
1848cda55fadSBarry Smith        0,
1849cda55fadSBarry Smith        0,
1850521d7252SBarry Smith /*50*/ MatSetBlockSize_MPIAIJ,
1851cda55fadSBarry Smith        0,
1852cda55fadSBarry Smith        0,
1853cda55fadSBarry Smith        0,
1854cda55fadSBarry Smith        0,
185597304618SKris Buschelman /*55*/ MatFDColoringCreate_MPIAIJ,
1856cda55fadSBarry Smith        0,
1857cda55fadSBarry Smith        MatSetUnfactored_MPIAIJ,
185842e855d1Svictor        MatPermute_MPIAIJ,
1859cda55fadSBarry Smith        0,
186097304618SKris Buschelman /*60*/ MatGetSubMatrix_MPIAIJ,
1861e03a110bSBarry Smith        MatDestroy_MPIAIJ,
1862e03a110bSBarry Smith        MatView_MPIAIJ,
1863357abbc8SBarry Smith        0,
1864a2243be0SBarry Smith        0,
186597304618SKris Buschelman /*65*/ 0,
1866a2243be0SBarry Smith        0,
1867a2243be0SBarry Smith        0,
1868a2243be0SBarry Smith        0,
1869a2243be0SBarry Smith        0,
187097304618SKris Buschelman /*70*/ 0,
1871a2243be0SBarry Smith        0,
1872a2243be0SBarry Smith        MatSetColoring_MPIAIJ,
1873dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
1874779c1a83SBarry Smith        MatSetValuesAdic_MPIAIJ,
1875dcf5cc72SBarry Smith #else
1876dcf5cc72SBarry Smith        0,
1877dcf5cc72SBarry Smith #endif
187897304618SKris Buschelman        MatSetValuesAdifor_MPIAIJ,
187997304618SKris Buschelman /*75*/ 0,
188097304618SKris Buschelman        0,
188197304618SKris Buschelman        0,
188297304618SKris Buschelman        0,
188397304618SKris Buschelman        0,
188497304618SKris Buschelman /*80*/ 0,
188597304618SKris Buschelman        0,
188697304618SKris Buschelman        0,
188797304618SKris Buschelman        0,
188841acf15aSKris Buschelman /*84*/ MatLoad_MPIAIJ,
18896284ec50SHong Zhang        0,
18906284ec50SHong Zhang        0,
18916284ec50SHong Zhang        0,
18926284ec50SHong Zhang        0,
1893865e5f61SKris Buschelman        0,
1894865e5f61SKris Buschelman /*90*/ MatMatMult_MPIAIJ_MPIAIJ,
189526be0446SHong Zhang        MatMatMultSymbolic_MPIAIJ_MPIAIJ,
189626be0446SHong Zhang        MatMatMultNumeric_MPIAIJ_MPIAIJ,
18977a7894deSKris Buschelman        MatPtAP_Basic,
18987a7894deSKris Buschelman        MatPtAPSymbolic_MPIAIJ,
18997a7894deSKris Buschelman /*95*/ MatPtAPNumeric_MPIAIJ,
19007a7894deSKris Buschelman        0,
19017a7894deSKris Buschelman        0,
19027a7894deSKris Buschelman        0,
19037a7894deSKris Buschelman        0,
19047a7894deSKris Buschelman /*100*/0,
1905865e5f61SKris Buschelman        MatPtAPSymbolic_MPIAIJ_MPIAIJ,
19067a7894deSKris Buschelman        MatPtAPNumeric_MPIAIJ_MPIAIJ,
19072fd7e33dSBarry Smith        MatConjugate_MPIAIJ,
19082fd7e33dSBarry Smith        0,
190999cafbc1SBarry Smith /*105*/MatSetValuesRow_MPIAIJ,
191099cafbc1SBarry Smith        MatRealPart_MPIAIJ,
191199cafbc1SBarry Smith        MatImaginaryPart_MPIAIJ};
191236ce4990SBarry Smith 
19132e8a6d31SBarry Smith /* ----------------------------------------------------------------------------------------*/
19142e8a6d31SBarry Smith 
1915fb2e594dSBarry Smith EXTERN_C_BEGIN
19164a2ae208SSatish Balay #undef __FUNCT__
19174a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_MPIAIJ"
1918be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_MPIAIJ(Mat mat)
19192e8a6d31SBarry Smith {
19202e8a6d31SBarry Smith   Mat_MPIAIJ     *aij = (Mat_MPIAIJ *)mat->data;
1921dfbe8321SBarry Smith   PetscErrorCode ierr;
19222e8a6d31SBarry Smith 
19232e8a6d31SBarry Smith   PetscFunctionBegin;
19242e8a6d31SBarry Smith   ierr = MatStoreValues(aij->A);CHKERRQ(ierr);
19252e8a6d31SBarry Smith   ierr = MatStoreValues(aij->B);CHKERRQ(ierr);
19262e8a6d31SBarry Smith   PetscFunctionReturn(0);
19272e8a6d31SBarry Smith }
1928fb2e594dSBarry Smith EXTERN_C_END
19292e8a6d31SBarry Smith 
1930fb2e594dSBarry Smith EXTERN_C_BEGIN
19314a2ae208SSatish Balay #undef __FUNCT__
19324a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_MPIAIJ"
1933be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_MPIAIJ(Mat mat)
19342e8a6d31SBarry Smith {
19352e8a6d31SBarry Smith   Mat_MPIAIJ     *aij = (Mat_MPIAIJ *)mat->data;
1936dfbe8321SBarry Smith   PetscErrorCode ierr;
19372e8a6d31SBarry Smith 
19382e8a6d31SBarry Smith   PetscFunctionBegin;
19392e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr);
19402e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr);
19412e8a6d31SBarry Smith   PetscFunctionReturn(0);
19422e8a6d31SBarry Smith }
1943fb2e594dSBarry Smith EXTERN_C_END
19448a729477SBarry Smith 
1945e090d566SSatish Balay #include "petscpc.h"
194627508adbSBarry Smith EXTERN_C_BEGIN
19474a2ae208SSatish Balay #undef __FUNCT__
1948a23d5eceSKris Buschelman #define __FUNCT__ "MatMPIAIJSetPreallocation_MPIAIJ"
1949be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJSetPreallocation_MPIAIJ(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
1950a23d5eceSKris Buschelman {
1951a23d5eceSKris Buschelman   Mat_MPIAIJ     *b;
1952dfbe8321SBarry Smith   PetscErrorCode ierr;
1953b1d57f15SBarry Smith   PetscInt       i;
1954a23d5eceSKris Buschelman 
1955a23d5eceSKris Buschelman   PetscFunctionBegin;
1956a23d5eceSKris Buschelman   B->preallocated = PETSC_TRUE;
1957a23d5eceSKris Buschelman   if (d_nz == PETSC_DEFAULT || d_nz == PETSC_DECIDE) d_nz = 5;
1958a23d5eceSKris Buschelman   if (o_nz == PETSC_DEFAULT || o_nz == PETSC_DECIDE) o_nz = 2;
195977431f27SBarry Smith   if (d_nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"d_nz cannot be less than 0: value %D",d_nz);
196077431f27SBarry Smith   if (o_nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"o_nz cannot be less than 0: value %D",o_nz);
1961899cda47SBarry Smith 
1962899cda47SBarry Smith   B->rmap.bs = B->cmap.bs = 1;
1963899cda47SBarry Smith   ierr = PetscMapInitialize(B->comm,&B->rmap);CHKERRQ(ierr);
1964899cda47SBarry Smith   ierr = PetscMapInitialize(B->comm,&B->cmap);CHKERRQ(ierr);
1965a23d5eceSKris Buschelman   if (d_nnz) {
1966899cda47SBarry Smith     for (i=0; i<B->rmap.n; i++) {
196777431f27SBarry 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]);
1968a23d5eceSKris Buschelman     }
1969a23d5eceSKris Buschelman   }
1970a23d5eceSKris Buschelman   if (o_nnz) {
1971899cda47SBarry Smith     for (i=0; i<B->rmap.n; i++) {
197277431f27SBarry 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]);
1973a23d5eceSKris Buschelman     }
1974a23d5eceSKris Buschelman   }
1975a23d5eceSKris Buschelman   b = (Mat_MPIAIJ*)B->data;
1976899cda47SBarry Smith 
1977899cda47SBarry Smith   /* Explicitly create 2 MATSEQAIJ matrices. */
1978899cda47SBarry Smith   ierr = MatCreate(PETSC_COMM_SELF,&b->A);CHKERRQ(ierr);
1979899cda47SBarry Smith   ierr = MatSetSizes(b->A,B->rmap.n,B->cmap.n,B->rmap.n,B->cmap.n);CHKERRQ(ierr);
1980899cda47SBarry Smith   ierr = MatSetType(b->A,MATSEQAIJ);CHKERRQ(ierr);
1981899cda47SBarry Smith   ierr = PetscLogObjectParent(B,b->A);CHKERRQ(ierr);
1982899cda47SBarry Smith   ierr = MatCreate(PETSC_COMM_SELF,&b->B);CHKERRQ(ierr);
1983899cda47SBarry Smith   ierr = MatSetSizes(b->B,B->rmap.n,B->cmap.N,B->rmap.n,B->cmap.N);CHKERRQ(ierr);
1984899cda47SBarry Smith   ierr = MatSetType(b->B,MATSEQAIJ);CHKERRQ(ierr);
1985899cda47SBarry Smith   ierr = PetscLogObjectParent(B,b->B);CHKERRQ(ierr);
1986899cda47SBarry Smith 
1987c60e587dSKris Buschelman   ierr = MatSeqAIJSetPreallocation(b->A,d_nz,d_nnz);CHKERRQ(ierr);
1988c60e587dSKris Buschelman   ierr = MatSeqAIJSetPreallocation(b->B,o_nz,o_nnz);CHKERRQ(ierr);
1989a23d5eceSKris Buschelman 
1990a23d5eceSKris Buschelman   PetscFunctionReturn(0);
1991a23d5eceSKris Buschelman }
1992a23d5eceSKris Buschelman EXTERN_C_END
1993a23d5eceSKris Buschelman 
19944a2ae208SSatish Balay #undef __FUNCT__
19954a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_MPIAIJ"
1996dfbe8321SBarry Smith PetscErrorCode MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
1997d6dfbf8fSBarry Smith {
1998d6dfbf8fSBarry Smith   Mat            mat;
1999416022c9SBarry Smith   Mat_MPIAIJ     *a,*oldmat = (Mat_MPIAIJ*)matin->data;
2000dfbe8321SBarry Smith   PetscErrorCode ierr;
2001d6dfbf8fSBarry Smith 
20023a40ed3dSBarry Smith   PetscFunctionBegin;
2003416022c9SBarry Smith   *newmat       = 0;
2004f69a0ea3SMatthew Knepley   ierr = MatCreate(matin->comm,&mat);CHKERRQ(ierr);
2005899cda47SBarry Smith   ierr = MatSetSizes(mat,matin->rmap.n,matin->cmap.n,matin->rmap.N,matin->cmap.N);CHKERRQ(ierr);
2006be5d1d56SKris Buschelman   ierr = MatSetType(mat,matin->type_name);CHKERRQ(ierr);
20071d5dac46SHong Zhang   ierr = PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
2008273d9f13SBarry Smith   a    = (Mat_MPIAIJ*)mat->data;
2009e1b6402fSHong Zhang 
2010d6dfbf8fSBarry Smith   mat->factor       = matin->factor;
2011899cda47SBarry Smith   mat->rmap.bs      = matin->rmap.bs;
2012c456f294SBarry Smith   mat->assembled    = PETSC_TRUE;
2013e7641de0SSatish Balay   mat->insertmode   = NOT_SET_VALUES;
2014273d9f13SBarry Smith   mat->preallocated = PETSC_TRUE;
2015d6dfbf8fSBarry Smith 
201617699dbbSLois Curfman McInnes   a->size           = oldmat->size;
201717699dbbSLois Curfman McInnes   a->rank           = oldmat->rank;
2018e7641de0SSatish Balay   a->donotstash     = oldmat->donotstash;
2019e7641de0SSatish Balay   a->roworiented    = oldmat->roworiented;
2020e7641de0SSatish Balay   a->rowindices     = 0;
2021bcd2baecSBarry Smith   a->rowvalues      = 0;
2022bcd2baecSBarry Smith   a->getrowactive   = PETSC_FALSE;
2023d6dfbf8fSBarry Smith 
2024899cda47SBarry Smith   ierr = PetscMapCopy(mat->comm,&matin->rmap,&mat->rmap);CHKERRQ(ierr);
2025899cda47SBarry Smith   ierr = PetscMapCopy(mat->comm,&matin->cmap,&mat->cmap);CHKERRQ(ierr);
2026899cda47SBarry Smith 
20278798bf22SSatish Balay   ierr = MatStashCreate_Private(matin->comm,1,&mat->stash);CHKERRQ(ierr);
20282ee70a88SLois Curfman McInnes   if (oldmat->colmap) {
2029aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
20300f5bd95cSBarry Smith     ierr = PetscTableCreateCopy(oldmat->colmap,&a->colmap);CHKERRQ(ierr);
2031b1fc9764SSatish Balay #else
2032899cda47SBarry Smith     ierr = PetscMalloc((mat->cmap.N)*sizeof(PetscInt),&a->colmap);CHKERRQ(ierr);
2033899cda47SBarry Smith     ierr = PetscLogObjectMemory(mat,(mat->cmap.N)*sizeof(PetscInt));CHKERRQ(ierr);
2034899cda47SBarry Smith     ierr = PetscMemcpy(a->colmap,oldmat->colmap,(mat->cmap.N)*sizeof(PetscInt));CHKERRQ(ierr);
2035b1fc9764SSatish Balay #endif
2036416022c9SBarry Smith   } else a->colmap = 0;
20373f41c07dSBarry Smith   if (oldmat->garray) {
2038b1d57f15SBarry Smith     PetscInt len;
2039899cda47SBarry Smith     len  = oldmat->B->cmap.n;
2040b1d57f15SBarry Smith     ierr = PetscMalloc((len+1)*sizeof(PetscInt),&a->garray);CHKERRQ(ierr);
204152e6d16bSBarry Smith     ierr = PetscLogObjectMemory(mat,len*sizeof(PetscInt));CHKERRQ(ierr);
2042b1d57f15SBarry Smith     if (len) { ierr = PetscMemcpy(a->garray,oldmat->garray,len*sizeof(PetscInt));CHKERRQ(ierr); }
2043416022c9SBarry Smith   } else a->garray = 0;
2044d6dfbf8fSBarry Smith 
2045416022c9SBarry Smith   ierr = VecDuplicate(oldmat->lvec,&a->lvec);CHKERRQ(ierr);
204652e6d16bSBarry Smith   ierr = PetscLogObjectParent(mat,a->lvec);CHKERRQ(ierr);
2047a56f8943SBarry Smith   ierr = VecScatterCopy(oldmat->Mvctx,&a->Mvctx);CHKERRQ(ierr);
204852e6d16bSBarry Smith   ierr = PetscLogObjectParent(mat,a->Mvctx);CHKERRQ(ierr);
20492e8a6d31SBarry Smith   ierr = MatDuplicate(oldmat->A,cpvalues,&a->A);CHKERRQ(ierr);
205052e6d16bSBarry Smith   ierr = PetscLogObjectParent(mat,a->A);CHKERRQ(ierr);
20512e8a6d31SBarry Smith   ierr = MatDuplicate(oldmat->B,cpvalues,&a->B);CHKERRQ(ierr);
205252e6d16bSBarry Smith   ierr = PetscLogObjectParent(mat,a->B);CHKERRQ(ierr);
2053b0a32e0cSBarry Smith   ierr = PetscFListDuplicate(matin->qlist,&mat->qlist);CHKERRQ(ierr);
20548a729477SBarry Smith   *newmat = mat;
20553a40ed3dSBarry Smith   PetscFunctionReturn(0);
20568a729477SBarry Smith }
2057416022c9SBarry Smith 
2058e090d566SSatish Balay #include "petscsys.h"
2059416022c9SBarry Smith 
20604a2ae208SSatish Balay #undef __FUNCT__
20614a2ae208SSatish Balay #define __FUNCT__ "MatLoad_MPIAIJ"
2062f69a0ea3SMatthew Knepley PetscErrorCode MatLoad_MPIAIJ(PetscViewer viewer, MatType type,Mat *newmat)
2063416022c9SBarry Smith {
2064d65a2f8fSBarry Smith   Mat            A;
206587828ca2SBarry Smith   PetscScalar    *vals,*svals;
206619bcc07fSBarry Smith   MPI_Comm       comm = ((PetscObject)viewer)->comm;
2067416022c9SBarry Smith   MPI_Status     status;
20686849ba73SBarry Smith   PetscErrorCode ierr;
2069dc231df0SBarry Smith   PetscMPIInt    rank,size,tag = ((PetscObject)viewer)->tag,maxnz;
2070167e7480SBarry Smith   PetscInt       i,nz,j,rstart,rend,mmax;
2071b1d57f15SBarry Smith   PetscInt       header[4],*rowlengths = 0,M,N,m,*cols;
2072910ba992SMatthew Knepley   PetscInt       *ourlens = PETSC_NULL,*procsnz = PETSC_NULL,*offlens = PETSC_NULL,jj,*mycols,*smycols;
2073dc231df0SBarry Smith   PetscInt       cend,cstart,n,*rowners;
2074b1d57f15SBarry Smith   int            fd;
2075416022c9SBarry Smith 
20763a40ed3dSBarry Smith   PetscFunctionBegin;
20771dab6e02SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
20781dab6e02SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
207917699dbbSLois Curfman McInnes   if (!rank) {
2080b0a32e0cSBarry Smith     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
20810752156aSBarry Smith     ierr = PetscBinaryRead(fd,(char *)header,4,PETSC_INT);CHKERRQ(ierr);
2082552e946dSBarry Smith     if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
20836c5fab8fSBarry Smith   }
20846c5fab8fSBarry Smith 
2085b1d57f15SBarry Smith   ierr = MPI_Bcast(header+1,3,MPIU_INT,0,comm);CHKERRQ(ierr);
2086416022c9SBarry Smith   M = header[1]; N = header[2];
2087416022c9SBarry Smith   /* determine ownership of all rows */
208829cdbbc8SSatish Balay   m    = M/size + ((M % size) > rank);
2089dc231df0SBarry Smith   ierr = PetscMalloc((size+1)*sizeof(PetscInt),&rowners);CHKERRQ(ierr);
2090dc231df0SBarry Smith   ierr = MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);CHKERRQ(ierr);
2091167e7480SBarry Smith 
2092167e7480SBarry Smith   /* First process needs enough room for process with most rows */
2093167e7480SBarry Smith   if (!rank) {
2094167e7480SBarry Smith     mmax       = rowners[1];
2095167e7480SBarry Smith     for (i=2; i<size; i++) {
2096167e7480SBarry Smith       mmax = PetscMax(mmax,rowners[i]);
2097167e7480SBarry Smith     }
2098167e7480SBarry Smith   } else mmax = m;
2099167e7480SBarry Smith 
2100416022c9SBarry Smith   rowners[0] = 0;
210117699dbbSLois Curfman McInnes   for (i=2; i<=size; i++) {
2102167e7480SBarry Smith     mmax       = PetscMax(mmax,rowners[i]);
2103416022c9SBarry Smith     rowners[i] += rowners[i-1];
2104416022c9SBarry Smith   }
210517699dbbSLois Curfman McInnes   rstart = rowners[rank];
210617699dbbSLois Curfman McInnes   rend   = rowners[rank+1];
2107416022c9SBarry Smith 
2108416022c9SBarry Smith   /* distribute row lengths to all processors */
2109167e7480SBarry Smith   ierr    = PetscMalloc2(mmax,PetscInt,&ourlens,mmax,PetscInt,&offlens);CHKERRQ(ierr);
211017699dbbSLois Curfman McInnes   if (!rank) {
2111dc231df0SBarry Smith     ierr = PetscBinaryRead(fd,ourlens,m,PETSC_INT);CHKERRQ(ierr);
2112dc231df0SBarry Smith     ierr = PetscMalloc(m*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
2113b1d57f15SBarry Smith     ierr = PetscMalloc(size*sizeof(PetscInt),&procsnz);CHKERRQ(ierr);
2114b1d57f15SBarry Smith     ierr = PetscMemzero(procsnz,size*sizeof(PetscInt));CHKERRQ(ierr);
2115dc231df0SBarry Smith     for (j=0; j<m; j++) {
2116dc231df0SBarry Smith       procsnz[0] += ourlens[j];
2117dc231df0SBarry Smith     }
2118dc231df0SBarry Smith     for (i=1; i<size; i++) {
2119dc231df0SBarry Smith       ierr = PetscBinaryRead(fd,rowlengths,rowners[i+1]-rowners[i],PETSC_INT);CHKERRQ(ierr);
2120dc231df0SBarry Smith       /* calculate the number of nonzeros on each processor */
2121dc231df0SBarry Smith       for (j=0; j<rowners[i+1]-rowners[i]; j++) {
2122416022c9SBarry Smith         procsnz[i] += rowlengths[j];
2123416022c9SBarry Smith       }
2124dc231df0SBarry Smith       ierr = MPI_Send(rowlengths,rowners[i+1]-rowners[i],MPIU_INT,i,tag,comm);CHKERRQ(ierr);
2125416022c9SBarry Smith     }
2126606d414cSSatish Balay     ierr = PetscFree(rowlengths);CHKERRQ(ierr);
2127dc231df0SBarry Smith   } else {
2128dc231df0SBarry Smith     ierr = MPI_Recv(ourlens,m,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr);
2129dc231df0SBarry Smith   }
2130416022c9SBarry Smith 
2131dc231df0SBarry Smith   if (!rank) {
2132416022c9SBarry Smith     /* determine max buffer needed and allocate it */
2133416022c9SBarry Smith     maxnz = 0;
21348a8e0b3aSBarry Smith     for (i=0; i<size; i++) {
21350452661fSBarry Smith       maxnz = PetscMax(maxnz,procsnz[i]);
2136416022c9SBarry Smith     }
2137b1d57f15SBarry Smith     ierr = PetscMalloc(maxnz*sizeof(PetscInt),&cols);CHKERRQ(ierr);
2138416022c9SBarry Smith 
2139416022c9SBarry Smith     /* read in my part of the matrix column indices  */
2140416022c9SBarry Smith     nz   = procsnz[0];
2141b1d57f15SBarry Smith     ierr = PetscMalloc(nz*sizeof(PetscInt),&mycols);CHKERRQ(ierr);
21420752156aSBarry Smith     ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr);
2143d65a2f8fSBarry Smith 
2144d65a2f8fSBarry Smith     /* read in every one elses and ship off */
214517699dbbSLois Curfman McInnes     for (i=1; i<size; i++) {
2146d65a2f8fSBarry Smith       nz   = procsnz[i];
21470752156aSBarry Smith       ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr);
2148b1d57f15SBarry Smith       ierr = MPI_Send(cols,nz,MPIU_INT,i,tag,comm);CHKERRQ(ierr);
2149d65a2f8fSBarry Smith     }
2150606d414cSSatish Balay     ierr = PetscFree(cols);CHKERRQ(ierr);
21513a40ed3dSBarry Smith   } else {
2152416022c9SBarry Smith     /* determine buffer space needed for message */
2153416022c9SBarry Smith     nz = 0;
2154416022c9SBarry Smith     for (i=0; i<m; i++) {
2155416022c9SBarry Smith       nz += ourlens[i];
2156416022c9SBarry Smith     }
2157dc231df0SBarry Smith     ierr = PetscMalloc(nz*sizeof(PetscInt),&mycols);CHKERRQ(ierr);
2158416022c9SBarry Smith 
2159416022c9SBarry Smith     /* receive message of column indices*/
2160b1d57f15SBarry Smith     ierr = MPI_Recv(mycols,nz,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr);
2161b1d57f15SBarry Smith     ierr = MPI_Get_count(&status,MPIU_INT,&maxnz);CHKERRQ(ierr);
216229bbc08cSBarry Smith     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
2163416022c9SBarry Smith   }
2164416022c9SBarry Smith 
2165b362ba68SBarry Smith   /* determine column ownership if matrix is not square */
2166b362ba68SBarry Smith   if (N != M) {
2167b362ba68SBarry Smith     n      = N/size + ((N % size) > rank);
2168b1d57f15SBarry Smith     ierr   = MPI_Scan(&n,&cend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr);
2169b362ba68SBarry Smith     cstart = cend - n;
2170b362ba68SBarry Smith   } else {
2171b362ba68SBarry Smith     cstart = rstart;
2172b362ba68SBarry Smith     cend   = rend;
2173fb2e594dSBarry Smith     n      = cend - cstart;
2174b362ba68SBarry Smith   }
2175b362ba68SBarry Smith 
2176416022c9SBarry Smith   /* loop over local rows, determining number of off diagonal entries */
2177b1d57f15SBarry Smith   ierr = PetscMemzero(offlens,m*sizeof(PetscInt));CHKERRQ(ierr);
2178416022c9SBarry Smith   jj = 0;
2179416022c9SBarry Smith   for (i=0; i<m; i++) {
2180416022c9SBarry Smith     for (j=0; j<ourlens[i]; j++) {
2181b362ba68SBarry Smith       if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++;
2182416022c9SBarry Smith       jj++;
2183416022c9SBarry Smith     }
2184416022c9SBarry Smith   }
2185d65a2f8fSBarry Smith 
2186d65a2f8fSBarry Smith   /* create our matrix */
2187416022c9SBarry Smith   for (i=0; i<m; i++) {
2188416022c9SBarry Smith     ourlens[i] -= offlens[i];
2189416022c9SBarry Smith   }
2190f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,&A);CHKERRQ(ierr);
2191f69a0ea3SMatthew Knepley   ierr = MatSetSizes(A,m,n,M,N);CHKERRQ(ierr);
2192d10c748bSKris Buschelman   ierr = MatSetType(A,type);CHKERRQ(ierr);
2193d10c748bSKris Buschelman   ierr = MatMPIAIJSetPreallocation(A,0,ourlens,0,offlens);CHKERRQ(ierr);
2194d10c748bSKris Buschelman 
2195fb2e594dSBarry Smith   ierr = MatSetOption(A,MAT_COLUMNS_SORTED);CHKERRQ(ierr);
2196d65a2f8fSBarry Smith   for (i=0; i<m; i++) {
2197d65a2f8fSBarry Smith     ourlens[i] += offlens[i];
2198d65a2f8fSBarry Smith   }
2199416022c9SBarry Smith 
220017699dbbSLois Curfman McInnes   if (!rank) {
2201906b51c7SHong Zhang     ierr = PetscMalloc((maxnz+1)*sizeof(PetscScalar),&vals);CHKERRQ(ierr);
2202416022c9SBarry Smith 
2203416022c9SBarry Smith     /* read in my part of the matrix numerical values  */
2204416022c9SBarry Smith     nz   = procsnz[0];
22050752156aSBarry Smith     ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
2206d65a2f8fSBarry Smith 
2207d65a2f8fSBarry Smith     /* insert into matrix */
2208d65a2f8fSBarry Smith     jj      = rstart;
2209d65a2f8fSBarry Smith     smycols = mycols;
2210d65a2f8fSBarry Smith     svals   = vals;
2211d65a2f8fSBarry Smith     for (i=0; i<m; i++) {
2212dc231df0SBarry Smith       ierr = MatSetValues_MPIAIJ(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
2213d65a2f8fSBarry Smith       smycols += ourlens[i];
2214d65a2f8fSBarry Smith       svals   += ourlens[i];
2215d65a2f8fSBarry Smith       jj++;
2216416022c9SBarry Smith     }
2217416022c9SBarry Smith 
2218d65a2f8fSBarry Smith     /* read in other processors and ship out */
221917699dbbSLois Curfman McInnes     for (i=1; i<size; i++) {
2220416022c9SBarry Smith       nz   = procsnz[i];
22210752156aSBarry Smith       ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
2222ca161407SBarry Smith       ierr = MPI_Send(vals,nz,MPIU_SCALAR,i,A->tag,comm);CHKERRQ(ierr);
2223416022c9SBarry Smith     }
2224606d414cSSatish Balay     ierr = PetscFree(procsnz);CHKERRQ(ierr);
22253a40ed3dSBarry Smith   } else {
2226d65a2f8fSBarry Smith     /* receive numeric values */
222787828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&vals);CHKERRQ(ierr);
2228416022c9SBarry Smith 
2229d65a2f8fSBarry Smith     /* receive message of values*/
2230ca161407SBarry Smith     ierr = MPI_Recv(vals,nz,MPIU_SCALAR,0,A->tag,comm,&status);CHKERRQ(ierr);
2231ca161407SBarry Smith     ierr = MPI_Get_count(&status,MPIU_SCALAR,&maxnz);CHKERRQ(ierr);
223229bbc08cSBarry Smith     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
2233d65a2f8fSBarry Smith 
2234d65a2f8fSBarry Smith     /* insert into matrix */
2235d65a2f8fSBarry Smith     jj      = rstart;
2236d65a2f8fSBarry Smith     smycols = mycols;
2237d65a2f8fSBarry Smith     svals   = vals;
2238d65a2f8fSBarry Smith     for (i=0; i<m; i++) {
2239dc231df0SBarry Smith       ierr     = MatSetValues_MPIAIJ(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
2240d65a2f8fSBarry Smith       smycols += ourlens[i];
2241d65a2f8fSBarry Smith       svals   += ourlens[i];
2242d65a2f8fSBarry Smith       jj++;
2243d65a2f8fSBarry Smith     }
2244d65a2f8fSBarry Smith   }
2245dc231df0SBarry Smith   ierr = PetscFree2(ourlens,offlens);CHKERRQ(ierr);
2246606d414cSSatish Balay   ierr = PetscFree(vals);CHKERRQ(ierr);
2247606d414cSSatish Balay   ierr = PetscFree(mycols);CHKERRQ(ierr);
2248606d414cSSatish Balay   ierr = PetscFree(rowners);CHKERRQ(ierr);
2249d65a2f8fSBarry Smith 
22506d4a8577SBarry Smith   ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
22516d4a8577SBarry Smith   ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2252d10c748bSKris Buschelman   *newmat = A;
22533a40ed3dSBarry Smith   PetscFunctionReturn(0);
2254416022c9SBarry Smith }
2255a0ff6018SBarry Smith 
22564a2ae208SSatish Balay #undef __FUNCT__
22574a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_MPIAIJ"
2258a0ff6018SBarry Smith /*
225929da9460SBarry Smith     Not great since it makes two copies of the submatrix, first an SeqAIJ
226029da9460SBarry Smith   in local and then by concatenating the local matrices the end result.
226129da9460SBarry Smith   Writing it directly would be much like MatGetSubMatrices_MPIAIJ()
2262a0ff6018SBarry Smith */
2263b1d57f15SBarry Smith PetscErrorCode MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse call,Mat *newmat)
2264a0ff6018SBarry Smith {
2265dfbe8321SBarry Smith   PetscErrorCode ierr;
226632dcc486SBarry Smith   PetscMPIInt    rank,size;
2267b1d57f15SBarry Smith   PetscInt       i,m,n,rstart,row,rend,nz,*cwork,j;
2268b1d57f15SBarry Smith   PetscInt       *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal;
2269fee21e36SBarry Smith   Mat            *local,M,Mreuse;
227087828ca2SBarry Smith   PetscScalar    *vwork,*aa;
227100e6dbe6SBarry Smith   MPI_Comm       comm = mat->comm;
227200e6dbe6SBarry Smith   Mat_SeqAIJ     *aij;
22737e2c5f70SBarry Smith 
2274a0ff6018SBarry Smith 
2275a0ff6018SBarry Smith   PetscFunctionBegin;
22761dab6e02SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
22771dab6e02SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
227800e6dbe6SBarry Smith 
2279fee21e36SBarry Smith   if (call ==  MAT_REUSE_MATRIX) {
2280fee21e36SBarry Smith     ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);CHKERRQ(ierr);
2281e005ede5SBarry Smith     if (!Mreuse) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
2282fee21e36SBarry Smith     local = &Mreuse;
2283fee21e36SBarry Smith     ierr  = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&local);CHKERRQ(ierr);
2284fee21e36SBarry Smith   } else {
2285a0ff6018SBarry Smith     ierr   = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr);
2286fee21e36SBarry Smith     Mreuse = *local;
2287606d414cSSatish Balay     ierr   = PetscFree(local);CHKERRQ(ierr);
2288fee21e36SBarry Smith   }
2289a0ff6018SBarry Smith 
2290a0ff6018SBarry Smith   /*
2291a0ff6018SBarry Smith       m - number of local rows
2292a0ff6018SBarry Smith       n - number of columns (same on all processors)
2293a0ff6018SBarry Smith       rstart - first row in new global matrix generated
2294a0ff6018SBarry Smith   */
2295fee21e36SBarry Smith   ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr);
2296a0ff6018SBarry Smith   if (call == MAT_INITIAL_MATRIX) {
2297fee21e36SBarry Smith     aij = (Mat_SeqAIJ*)(Mreuse)->data;
229800e6dbe6SBarry Smith     ii  = aij->i;
229900e6dbe6SBarry Smith     jj  = aij->j;
230000e6dbe6SBarry Smith 
2301a0ff6018SBarry Smith     /*
230200e6dbe6SBarry Smith         Determine the number of non-zeros in the diagonal and off-diagonal
230300e6dbe6SBarry Smith         portions of the matrix in order to do correct preallocation
2304a0ff6018SBarry Smith     */
230500e6dbe6SBarry Smith 
230600e6dbe6SBarry Smith     /* first get start and end of "diagonal" columns */
23076a6a5d1dSBarry Smith     if (csize == PETSC_DECIDE) {
2308ab50ec6bSBarry Smith       ierr = ISGetSize(isrow,&mglobal);CHKERRQ(ierr);
2309ab50ec6bSBarry Smith       if (mglobal == n) { /* square matrix */
2310e2c4fddaSBarry Smith 	nlocal = m;
23116a6a5d1dSBarry Smith       } else {
2312ab50ec6bSBarry Smith         nlocal = n/size + ((n % size) > rank);
2313ab50ec6bSBarry Smith       }
2314ab50ec6bSBarry Smith     } else {
23156a6a5d1dSBarry Smith       nlocal = csize;
23166a6a5d1dSBarry Smith     }
2317b1d57f15SBarry Smith     ierr   = MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr);
231800e6dbe6SBarry Smith     rstart = rend - nlocal;
23196a6a5d1dSBarry Smith     if (rank == size - 1 && rend != n) {
232077431f27SBarry Smith       SETERRQ2(PETSC_ERR_ARG_SIZ,"Local column sizes %D do not add up to total number of columns %D",rend,n);
23216a6a5d1dSBarry Smith     }
232200e6dbe6SBarry Smith 
232300e6dbe6SBarry Smith     /* next, compute all the lengths */
2324b1d57f15SBarry Smith     ierr  = PetscMalloc((2*m+1)*sizeof(PetscInt),&dlens);CHKERRQ(ierr);
232500e6dbe6SBarry Smith     olens = dlens + m;
232600e6dbe6SBarry Smith     for (i=0; i<m; i++) {
232700e6dbe6SBarry Smith       jend = ii[i+1] - ii[i];
232800e6dbe6SBarry Smith       olen = 0;
232900e6dbe6SBarry Smith       dlen = 0;
233000e6dbe6SBarry Smith       for (j=0; j<jend; j++) {
233100e6dbe6SBarry Smith         if (*jj < rstart || *jj >= rend) olen++;
233200e6dbe6SBarry Smith         else dlen++;
233300e6dbe6SBarry Smith         jj++;
233400e6dbe6SBarry Smith       }
233500e6dbe6SBarry Smith       olens[i] = olen;
233600e6dbe6SBarry Smith       dlens[i] = dlen;
233700e6dbe6SBarry Smith     }
2338f69a0ea3SMatthew Knepley     ierr = MatCreate(comm,&M);CHKERRQ(ierr);
2339f69a0ea3SMatthew Knepley     ierr = MatSetSizes(M,m,nlocal,PETSC_DECIDE,n);CHKERRQ(ierr);
2340e2d9671bSKris Buschelman     ierr = MatSetType(M,mat->type_name);CHKERRQ(ierr);
2341e2d9671bSKris Buschelman     ierr = MatMPIAIJSetPreallocation(M,0,dlens,0,olens);CHKERRQ(ierr);
2342606d414cSSatish Balay     ierr = PetscFree(dlens);CHKERRQ(ierr);
2343a0ff6018SBarry Smith   } else {
2344b1d57f15SBarry Smith     PetscInt ml,nl;
2345a0ff6018SBarry Smith 
2346a0ff6018SBarry Smith     M = *newmat;
2347a0ff6018SBarry Smith     ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr);
234829bbc08cSBarry Smith     if (ml != m) SETERRQ(PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
2349a0ff6018SBarry Smith     ierr = MatZeroEntries(M);CHKERRQ(ierr);
2350c48de900SBarry Smith     /*
2351c48de900SBarry Smith          The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
2352c48de900SBarry Smith        rather than the slower MatSetValues().
2353c48de900SBarry Smith     */
2354c48de900SBarry Smith     M->was_assembled = PETSC_TRUE;
2355c48de900SBarry Smith     M->assembled     = PETSC_FALSE;
2356a0ff6018SBarry Smith   }
2357a0ff6018SBarry Smith   ierr = MatGetOwnershipRange(M,&rstart,&rend);CHKERRQ(ierr);
2358fee21e36SBarry Smith   aij = (Mat_SeqAIJ*)(Mreuse)->data;
235900e6dbe6SBarry Smith   ii  = aij->i;
236000e6dbe6SBarry Smith   jj  = aij->j;
236100e6dbe6SBarry Smith   aa  = aij->a;
2362a0ff6018SBarry Smith   for (i=0; i<m; i++) {
2363a0ff6018SBarry Smith     row   = rstart + i;
236400e6dbe6SBarry Smith     nz    = ii[i+1] - ii[i];
236500e6dbe6SBarry Smith     cwork = jj;     jj += nz;
236600e6dbe6SBarry Smith     vwork = aa;     aa += nz;
23678c638d02SBarry Smith     ierr = MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr);
2368a0ff6018SBarry Smith   }
2369a0ff6018SBarry Smith 
2370a0ff6018SBarry Smith   ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2371a0ff6018SBarry Smith   ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2372a0ff6018SBarry Smith   *newmat = M;
2373fee21e36SBarry Smith 
2374fee21e36SBarry Smith   /* save submatrix used in processor for next request */
2375fee21e36SBarry Smith   if (call ==  MAT_INITIAL_MATRIX) {
2376fee21e36SBarry Smith     ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr);
2377fee21e36SBarry Smith     ierr = PetscObjectDereference((PetscObject)Mreuse);CHKERRQ(ierr);
2378fee21e36SBarry Smith   }
2379fee21e36SBarry Smith 
2380a0ff6018SBarry Smith   PetscFunctionReturn(0);
2381a0ff6018SBarry Smith }
2382273d9f13SBarry Smith 
2383e2e86b8fSSatish Balay EXTERN_C_BEGIN
23844a2ae208SSatish Balay #undef __FUNCT__
2385ccd8e176SBarry Smith #define __FUNCT__ "MatMPIAIJSetPreallocationCSR_MPIAIJ"
2386b7940d39SSatish Balay PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJSetPreallocationCSR_MPIAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
2387ccd8e176SBarry Smith {
2388899cda47SBarry Smith   PetscInt       m,cstart, cend,j,nnz,i,d;
2389899cda47SBarry Smith   PetscInt       *d_nnz,*o_nnz,nnz_max = 0,rstart,ii;
2390ccd8e176SBarry Smith   const PetscInt *JJ;
2391ccd8e176SBarry Smith   PetscScalar    *values;
2392ccd8e176SBarry Smith   PetscErrorCode ierr;
2393ccd8e176SBarry Smith 
2394ccd8e176SBarry Smith   PetscFunctionBegin;
2395b7940d39SSatish Balay   if (Ii[0]) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Ii[0] must be 0 it is %D",Ii[0]);
2396899cda47SBarry Smith 
2397899cda47SBarry Smith   B->rmap.bs = B->cmap.bs = 1;
2398899cda47SBarry Smith   ierr = PetscMapInitialize(B->comm,&B->rmap);CHKERRQ(ierr);
2399899cda47SBarry Smith   ierr = PetscMapInitialize(B->comm,&B->cmap);CHKERRQ(ierr);
2400899cda47SBarry Smith   m      = B->rmap.n;
2401899cda47SBarry Smith   cstart = B->cmap.rstart;
2402899cda47SBarry Smith   cend   = B->cmap.rend;
2403899cda47SBarry Smith   rstart = B->rmap.rstart;
2404899cda47SBarry Smith 
2405ccd8e176SBarry Smith   ierr  = PetscMalloc((2*m+1)*sizeof(PetscInt),&d_nnz);CHKERRQ(ierr);
2406ccd8e176SBarry Smith   o_nnz = d_nnz + m;
2407ccd8e176SBarry Smith 
2408ccd8e176SBarry Smith   for (i=0; i<m; i++) {
2409b7940d39SSatish Balay     nnz     = Ii[i+1]- Ii[i];
2410b7940d39SSatish Balay     JJ      = J + Ii[i];
2411ccd8e176SBarry Smith     nnz_max = PetscMax(nnz_max,nnz);
2412a1661176SMatthew Knepley     if (nnz < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative %D number of columns",i,nnz);
2413ccd8e176SBarry Smith     for (j=0; j<nnz; j++) {
2414ccd8e176SBarry Smith       if (*JJ >= cstart) break;
2415ccd8e176SBarry Smith       JJ++;
2416ccd8e176SBarry Smith     }
2417ccd8e176SBarry Smith     d = 0;
2418ccd8e176SBarry Smith     for (; j<nnz; j++) {
2419ccd8e176SBarry Smith       if (*JJ++ >= cend) break;
2420ccd8e176SBarry Smith       d++;
2421ccd8e176SBarry Smith     }
2422ccd8e176SBarry Smith     d_nnz[i] = d;
2423ccd8e176SBarry Smith     o_nnz[i] = nnz - d;
2424ccd8e176SBarry Smith   }
2425ccd8e176SBarry Smith   ierr = MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);CHKERRQ(ierr);
2426ccd8e176SBarry Smith   ierr = PetscFree(d_nnz);CHKERRQ(ierr);
2427ccd8e176SBarry Smith 
2428ccd8e176SBarry Smith   if (v) values = (PetscScalar*)v;
2429ccd8e176SBarry Smith   else {
2430ccd8e176SBarry Smith     ierr = PetscMalloc((nnz_max+1)*sizeof(PetscScalar),&values);CHKERRQ(ierr);
2431ccd8e176SBarry Smith     ierr = PetscMemzero(values,nnz_max*sizeof(PetscScalar));CHKERRQ(ierr);
2432ccd8e176SBarry Smith   }
2433ccd8e176SBarry Smith 
2434ccd8e176SBarry Smith   ierr = MatSetOption(B,MAT_COLUMNS_SORTED);CHKERRQ(ierr);
2435ccd8e176SBarry Smith   for (i=0; i<m; i++) {
2436ccd8e176SBarry Smith     ii   = i + rstart;
2437b7940d39SSatish Balay     nnz  = Ii[i+1]- Ii[i];
2438b7940d39SSatish Balay     ierr = MatSetValues_MPIAIJ(B,1,&ii,nnz,J+Ii[i],values+(v ? Ii[i] : 0),INSERT_VALUES);CHKERRQ(ierr);
2439ccd8e176SBarry Smith   }
2440ccd8e176SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2441ccd8e176SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2442ccd8e176SBarry Smith   ierr = MatSetOption(B,MAT_COLUMNS_UNSORTED);CHKERRQ(ierr);
2443ccd8e176SBarry Smith 
2444ccd8e176SBarry Smith   if (!v) {
2445ccd8e176SBarry Smith     ierr = PetscFree(values);CHKERRQ(ierr);
2446ccd8e176SBarry Smith   }
2447ccd8e176SBarry Smith   PetscFunctionReturn(0);
2448ccd8e176SBarry Smith }
2449e2e86b8fSSatish Balay EXTERN_C_END
2450ccd8e176SBarry Smith 
2451ccd8e176SBarry Smith #undef __FUNCT__
2452ccd8e176SBarry Smith #define __FUNCT__ "MatMPIAIJSetPreallocationCSR"
24531eea217eSSatish Balay /*@
2454ccd8e176SBarry Smith    MatMPIAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in AIJ format
2455ccd8e176SBarry Smith    (the default parallel PETSc format).
2456ccd8e176SBarry Smith 
2457ccd8e176SBarry Smith    Collective on MPI_Comm
2458ccd8e176SBarry Smith 
2459ccd8e176SBarry Smith    Input Parameters:
2460a1661176SMatthew Knepley +  B - the matrix
2461ccd8e176SBarry Smith .  i - the indices into j for the start of each local row (starts with zero)
2462ccd8e176SBarry Smith .  j - the column indices for each local row (starts with zero) these must be sorted for each row
2463ccd8e176SBarry Smith -  v - optional values in the matrix
2464ccd8e176SBarry Smith 
2465ccd8e176SBarry Smith    Level: developer
2466ccd8e176SBarry Smith 
24672fb0ec9aSBarry Smith    Notes: this actually copies the values from i[], j[], and a[] to put them into PETSc's internal
24682fb0ec9aSBarry Smith      storage format. Thus changing the values in a[] after this call will not effect the matrix values.
24692fb0ec9aSBarry Smith 
2470ccd8e176SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
2471ccd8e176SBarry Smith 
24722fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatCreateMPIAIJ(), MPIAIJ,
24732fb0ec9aSBarry Smith           MatCreateSeqAIJWithArrays()
2474ccd8e176SBarry Smith @*/
2475be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
2476ccd8e176SBarry Smith {
2477ccd8e176SBarry Smith   PetscErrorCode ierr,(*f)(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]);
2478ccd8e176SBarry Smith 
2479ccd8e176SBarry Smith   PetscFunctionBegin;
2480ccd8e176SBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)B,"MatMPIAIJSetPreallocationCSR_C",(void (**)(void))&f);CHKERRQ(ierr);
2481ccd8e176SBarry Smith   if (f) {
2482ccd8e176SBarry Smith     ierr = (*f)(B,i,j,v);CHKERRQ(ierr);
2483ccd8e176SBarry Smith   }
2484ccd8e176SBarry Smith   PetscFunctionReturn(0);
2485ccd8e176SBarry Smith }
2486ccd8e176SBarry Smith 
2487ccd8e176SBarry Smith #undef __FUNCT__
24884a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJSetPreallocation"
2489273d9f13SBarry Smith /*@C
2490ccd8e176SBarry Smith    MatMPIAIJSetPreallocation - Preallocates memory for a sparse parallel matrix in AIJ format
2491273d9f13SBarry Smith    (the default parallel PETSc format).  For good matrix assembly performance
2492273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameters
2493273d9f13SBarry Smith    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
2494273d9f13SBarry Smith    performance can be increased by more than a factor of 50.
2495273d9f13SBarry Smith 
2496273d9f13SBarry Smith    Collective on MPI_Comm
2497273d9f13SBarry Smith 
2498273d9f13SBarry Smith    Input Parameters:
2499273d9f13SBarry Smith +  A - the matrix
2500273d9f13SBarry Smith .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
2501273d9f13SBarry Smith            (same value is used for all local rows)
2502273d9f13SBarry Smith .  d_nnz - array containing the number of nonzeros in the various rows of the
2503273d9f13SBarry Smith            DIAGONAL portion of the local submatrix (possibly different for each row)
2504273d9f13SBarry Smith            or PETSC_NULL, if d_nz is used to specify the nonzero structure.
2505273d9f13SBarry Smith            The size of this array is equal to the number of local rows, i.e 'm'.
2506273d9f13SBarry Smith            You must leave room for the diagonal entry even if it is zero.
2507273d9f13SBarry Smith .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
2508273d9f13SBarry Smith            submatrix (same value is used for all local rows).
2509273d9f13SBarry Smith -  o_nnz - array containing the number of nonzeros in the various rows of the
2510273d9f13SBarry Smith            OFF-DIAGONAL portion of the local submatrix (possibly different for
2511273d9f13SBarry Smith            each row) or PETSC_NULL, if o_nz is used to specify the nonzero
2512273d9f13SBarry Smith            structure. The size of this array is equal to the number
2513273d9f13SBarry Smith            of local rows, i.e 'm'.
2514273d9f13SBarry Smith 
251549a6f317SBarry Smith    If the *_nnz parameter is given then the *_nz parameter is ignored
251649a6f317SBarry Smith 
2517273d9f13SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
2518ccd8e176SBarry Smith    compressed row storage (CSR)), is fully compatible with standard Fortran 77
2519ccd8e176SBarry Smith    storage.  The stored row and column indices begin with zero.  See the users manual for details.
2520273d9f13SBarry Smith 
2521273d9f13SBarry Smith    The parallel matrix is partitioned such that the first m0 rows belong to
2522273d9f13SBarry Smith    process 0, the next m1 rows belong to process 1, the next m2 rows belong
2523273d9f13SBarry Smith    to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.
2524273d9f13SBarry Smith 
2525273d9f13SBarry Smith    The DIAGONAL portion of the local submatrix of a processor can be defined
2526273d9f13SBarry Smith    as the submatrix which is obtained by extraction the part corresponding
2527273d9f13SBarry Smith    to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the
2528273d9f13SBarry Smith    first row that belongs to the processor, and r2 is the last row belonging
2529273d9f13SBarry Smith    to the this processor. This is a square mxm matrix. The remaining portion
2530273d9f13SBarry Smith    of the local submatrix (mxN) constitute the OFF-DIAGONAL portion.
2531273d9f13SBarry Smith 
2532273d9f13SBarry Smith    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
2533273d9f13SBarry Smith 
2534273d9f13SBarry Smith    Example usage:
2535273d9f13SBarry Smith 
2536273d9f13SBarry Smith    Consider the following 8x8 matrix with 34 non-zero values, that is
2537273d9f13SBarry Smith    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
2538273d9f13SBarry Smith    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
2539273d9f13SBarry Smith    as follows:
2540273d9f13SBarry Smith 
2541273d9f13SBarry Smith .vb
2542273d9f13SBarry Smith             1  2  0  |  0  3  0  |  0  4
2543273d9f13SBarry Smith     Proc0   0  5  6  |  7  0  0  |  8  0
2544273d9f13SBarry Smith             9  0 10  | 11  0  0  | 12  0
2545273d9f13SBarry Smith     -------------------------------------
2546273d9f13SBarry Smith            13  0 14  | 15 16 17  |  0  0
2547273d9f13SBarry Smith     Proc1   0 18  0  | 19 20 21  |  0  0
2548273d9f13SBarry Smith             0  0  0  | 22 23  0  | 24  0
2549273d9f13SBarry Smith     -------------------------------------
2550273d9f13SBarry Smith     Proc2  25 26 27  |  0  0 28  | 29  0
2551273d9f13SBarry Smith            30  0  0  | 31 32 33  |  0 34
2552273d9f13SBarry Smith .ve
2553273d9f13SBarry Smith 
2554273d9f13SBarry Smith    This can be represented as a collection of submatrices as:
2555273d9f13SBarry Smith 
2556273d9f13SBarry Smith .vb
2557273d9f13SBarry Smith       A B C
2558273d9f13SBarry Smith       D E F
2559273d9f13SBarry Smith       G H I
2560273d9f13SBarry Smith .ve
2561273d9f13SBarry Smith 
2562273d9f13SBarry Smith    Where the submatrices A,B,C are owned by proc0, D,E,F are
2563273d9f13SBarry Smith    owned by proc1, G,H,I are owned by proc2.
2564273d9f13SBarry Smith 
2565273d9f13SBarry Smith    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2566273d9f13SBarry Smith    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2567273d9f13SBarry Smith    The 'M','N' parameters are 8,8, and have the same values on all procs.
2568273d9f13SBarry Smith 
2569273d9f13SBarry Smith    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
2570273d9f13SBarry Smith    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
2571273d9f13SBarry Smith    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
2572273d9f13SBarry Smith    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
2573273d9f13SBarry Smith    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
2574273d9f13SBarry Smith    matrix, ans [DF] as another SeqAIJ matrix.
2575273d9f13SBarry Smith 
2576273d9f13SBarry Smith    When d_nz, o_nz parameters are specified, d_nz storage elements are
2577273d9f13SBarry Smith    allocated for every row of the local diagonal submatrix, and o_nz
2578273d9f13SBarry Smith    storage locations are allocated for every row of the OFF-DIAGONAL submat.
2579273d9f13SBarry Smith    One way to choose d_nz and o_nz is to use the max nonzerors per local
2580273d9f13SBarry Smith    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
2581273d9f13SBarry Smith    In this case, the values of d_nz,o_nz are:
2582273d9f13SBarry Smith .vb
2583273d9f13SBarry Smith      proc0 : dnz = 2, o_nz = 2
2584273d9f13SBarry Smith      proc1 : dnz = 3, o_nz = 2
2585273d9f13SBarry Smith      proc2 : dnz = 1, o_nz = 4
2586273d9f13SBarry Smith .ve
2587273d9f13SBarry Smith    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
2588273d9f13SBarry Smith    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
2589273d9f13SBarry Smith    for proc3. i.e we are using 12+15+10=37 storage locations to store
2590273d9f13SBarry Smith    34 values.
2591273d9f13SBarry Smith 
2592273d9f13SBarry Smith    When d_nnz, o_nnz parameters are specified, the storage is specified
2593273d9f13SBarry Smith    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
2594273d9f13SBarry Smith    In the above case the values for d_nnz,o_nnz are:
2595273d9f13SBarry Smith .vb
2596273d9f13SBarry Smith      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
2597273d9f13SBarry Smith      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
2598273d9f13SBarry Smith      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
2599273d9f13SBarry Smith .ve
2600273d9f13SBarry Smith    Here the space allocated is sum of all the above values i.e 34, and
2601273d9f13SBarry Smith    hence pre-allocation is perfect.
2602273d9f13SBarry Smith 
2603273d9f13SBarry Smith    Level: intermediate
2604273d9f13SBarry Smith 
2605273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
2606273d9f13SBarry Smith 
2607ccd8e176SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateMPIAIJ(), MatMPIAIJSetPreallocationCSR(),
2608ccd8e176SBarry Smith           MPIAIJ
2609273d9f13SBarry Smith @*/
2610be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJSetPreallocation(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
2611273d9f13SBarry Smith {
2612b1d57f15SBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[]);
2613273d9f13SBarry Smith 
2614273d9f13SBarry Smith   PetscFunctionBegin;
2615a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatMPIAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
2616a23d5eceSKris Buschelman   if (f) {
2617a23d5eceSKris Buschelman     ierr = (*f)(B,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr);
2618273d9f13SBarry Smith   }
2619273d9f13SBarry Smith   PetscFunctionReturn(0);
2620273d9f13SBarry Smith }
2621273d9f13SBarry Smith 
26224a2ae208SSatish Balay #undef __FUNCT__
26232fb0ec9aSBarry Smith #define __FUNCT__ "MatCreateMPIAIJWithArrays"
26242fb0ec9aSBarry Smith /*@C
26252fb0ec9aSBarry Smith      MatCreateMPIAIJWithArrays - creates a MPI AIJ matrix using arrays that contain in standard
26262fb0ec9aSBarry Smith          CSR format the local rows.
26272fb0ec9aSBarry Smith 
26282fb0ec9aSBarry Smith    Collective on MPI_Comm
26292fb0ec9aSBarry Smith 
26302fb0ec9aSBarry Smith    Input Parameters:
26312fb0ec9aSBarry Smith +  comm - MPI communicator
26322fb0ec9aSBarry Smith .  m - number of local rows (Cannot be PETSC_DECIDE)
26332fb0ec9aSBarry Smith .  n - This value should be the same as the local size used in creating the
26342fb0ec9aSBarry Smith        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
26352fb0ec9aSBarry Smith        calculated if N is given) For square matrices n is almost always m.
26362fb0ec9aSBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
26372fb0ec9aSBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
26382fb0ec9aSBarry Smith .   i - row indices
26392fb0ec9aSBarry Smith .   j - column indices
26402fb0ec9aSBarry Smith -   a - matrix values
26412fb0ec9aSBarry Smith 
26422fb0ec9aSBarry Smith    Output Parameter:
26432fb0ec9aSBarry Smith .   mat - the matrix
26442fb0ec9aSBarry Smith    Level: intermediate
26452fb0ec9aSBarry Smith 
26462fb0ec9aSBarry Smith    Notes:
26472fb0ec9aSBarry Smith        The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc;
26482fb0ec9aSBarry Smith      thus you CANNOT change the matrix entries by changing the values of a[] after you have
26492fb0ec9aSBarry Smith      called this routine.
26502fb0ec9aSBarry Smith 
26512fb0ec9aSBarry Smith        The i and j indices are 0 based
26522fb0ec9aSBarry Smith 
26532fb0ec9aSBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
26542fb0ec9aSBarry Smith 
26552fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
26562fb0ec9aSBarry Smith           MPIAIJ, MatCreateMPIAIJ()
26572fb0ec9aSBarry Smith @*/
26582fb0ec9aSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatCreateMPIIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,const PetscInt i[],const PetscInt j[],const PetscScalar a[],Mat *mat)
26592fb0ec9aSBarry Smith {
26602fb0ec9aSBarry Smith   PetscErrorCode ierr;
26612fb0ec9aSBarry Smith 
26622fb0ec9aSBarry Smith  PetscFunctionBegin;
26632fb0ec9aSBarry Smith   if (i[0]) {
26642fb0ec9aSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
26652fb0ec9aSBarry Smith   }
26662fb0ec9aSBarry Smith   if (m < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative");
26672fb0ec9aSBarry Smith   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
26682fb0ec9aSBarry Smith   ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr);
26692fb0ec9aSBarry Smith   ierr = MatMPIAIJSetPreallocationCSR(*mat,i,j,a);CHKERRQ(ierr);
26702fb0ec9aSBarry Smith   PetscFunctionReturn(0);
26712fb0ec9aSBarry Smith }
26722fb0ec9aSBarry Smith 
26732fb0ec9aSBarry Smith #undef __FUNCT__
26744a2ae208SSatish Balay #define __FUNCT__ "MatCreateMPIAIJ"
2675273d9f13SBarry Smith /*@C
2676273d9f13SBarry Smith    MatCreateMPIAIJ - Creates a sparse parallel matrix in AIJ format
2677273d9f13SBarry Smith    (the default parallel PETSc format).  For good matrix assembly performance
2678273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameters
2679273d9f13SBarry Smith    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
2680273d9f13SBarry Smith    performance can be increased by more than a factor of 50.
2681273d9f13SBarry Smith 
2682273d9f13SBarry Smith    Collective on MPI_Comm
2683273d9f13SBarry Smith 
2684273d9f13SBarry Smith    Input Parameters:
2685273d9f13SBarry Smith +  comm - MPI communicator
2686273d9f13SBarry Smith .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
2687273d9f13SBarry Smith            This value should be the same as the local size used in creating the
2688273d9f13SBarry Smith            y vector for the matrix-vector product y = Ax.
2689273d9f13SBarry Smith .  n - This value should be the same as the local size used in creating the
2690273d9f13SBarry Smith        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
2691273d9f13SBarry Smith        calculated if N is given) For square matrices n is almost always m.
2692273d9f13SBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
2693273d9f13SBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
2694273d9f13SBarry Smith .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
2695273d9f13SBarry Smith            (same value is used for all local rows)
2696273d9f13SBarry Smith .  d_nnz - array containing the number of nonzeros in the various rows of the
2697273d9f13SBarry Smith            DIAGONAL portion of the local submatrix (possibly different for each row)
2698273d9f13SBarry Smith            or PETSC_NULL, if d_nz is used to specify the nonzero structure.
2699273d9f13SBarry Smith            The size of this array is equal to the number of local rows, i.e 'm'.
2700273d9f13SBarry Smith            You must leave room for the diagonal entry even if it is zero.
2701273d9f13SBarry Smith .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
2702273d9f13SBarry Smith            submatrix (same value is used for all local rows).
2703273d9f13SBarry Smith -  o_nnz - array containing the number of nonzeros in the various rows of the
2704273d9f13SBarry Smith            OFF-DIAGONAL portion of the local submatrix (possibly different for
2705273d9f13SBarry Smith            each row) or PETSC_NULL, if o_nz is used to specify the nonzero
2706273d9f13SBarry Smith            structure. The size of this array is equal to the number
2707273d9f13SBarry Smith            of local rows, i.e 'm'.
2708273d9f13SBarry Smith 
2709273d9f13SBarry Smith    Output Parameter:
2710273d9f13SBarry Smith .  A - the matrix
2711273d9f13SBarry Smith 
2712273d9f13SBarry Smith    Notes:
271349a6f317SBarry Smith    If the *_nnz parameter is given then the *_nz parameter is ignored
271449a6f317SBarry Smith 
2715273d9f13SBarry Smith    m,n,M,N parameters specify the size of the matrix, and its partitioning across
2716273d9f13SBarry Smith    processors, while d_nz,d_nnz,o_nz,o_nnz parameters specify the approximate
2717273d9f13SBarry Smith    storage requirements for this matrix.
2718273d9f13SBarry Smith 
2719273d9f13SBarry Smith    If PETSC_DECIDE or  PETSC_DETERMINE is used for a particular argument on one
2720273d9f13SBarry Smith    processor than it must be used on all processors that share the object for
2721273d9f13SBarry Smith    that argument.
2722273d9f13SBarry Smith 
2723273d9f13SBarry Smith    The user MUST specify either the local or global matrix dimensions
2724273d9f13SBarry Smith    (possibly both).
2725273d9f13SBarry Smith 
2726273d9f13SBarry Smith    The parallel matrix is partitioned such that the first m0 rows belong to
2727273d9f13SBarry Smith    process 0, the next m1 rows belong to process 1, the next m2 rows belong
2728273d9f13SBarry Smith    to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.
2729273d9f13SBarry Smith 
2730273d9f13SBarry Smith    The DIAGONAL portion of the local submatrix of a processor can be defined
2731273d9f13SBarry Smith    as the submatrix which is obtained by extraction the part corresponding
2732273d9f13SBarry Smith    to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the
2733273d9f13SBarry Smith    first row that belongs to the processor, and r2 is the last row belonging
2734273d9f13SBarry Smith    to the this processor. This is a square mxm matrix. The remaining portion
2735273d9f13SBarry Smith    of the local submatrix (mxN) constitute the OFF-DIAGONAL portion.
2736273d9f13SBarry Smith 
2737273d9f13SBarry Smith    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
2738273d9f13SBarry Smith 
273997d05335SKris Buschelman    When calling this routine with a single process communicator, a matrix of
274097d05335SKris Buschelman    type SEQAIJ is returned.  If a matrix of type MPIAIJ is desired for this
274197d05335SKris Buschelman    type of communicator, use the construction mechanism:
274297d05335SKris Buschelman      MatCreate(...,&A); MatSetType(A,MPIAIJ); MatMPIAIJSetPreallocation(A,...);
274397d05335SKris Buschelman 
2744273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible.
2745273d9f13SBarry Smith    We search for consecutive rows with the same nonzero structure, thereby
2746273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2747273d9f13SBarry Smith 
2748273d9f13SBarry Smith    Options Database Keys:
2749923f20ffSKris Buschelman +  -mat_no_inode  - Do not use inodes
2750923f20ffSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2751273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2752273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2753273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2754273d9f13SBarry Smith 
2755273d9f13SBarry Smith 
2756273d9f13SBarry Smith    Example usage:
2757273d9f13SBarry Smith 
2758273d9f13SBarry Smith    Consider the following 8x8 matrix with 34 non-zero values, that is
2759273d9f13SBarry Smith    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
2760273d9f13SBarry Smith    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
2761273d9f13SBarry Smith    as follows:
2762273d9f13SBarry Smith 
2763273d9f13SBarry Smith .vb
2764273d9f13SBarry Smith             1  2  0  |  0  3  0  |  0  4
2765273d9f13SBarry Smith     Proc0   0  5  6  |  7  0  0  |  8  0
2766273d9f13SBarry Smith             9  0 10  | 11  0  0  | 12  0
2767273d9f13SBarry Smith     -------------------------------------
2768273d9f13SBarry Smith            13  0 14  | 15 16 17  |  0  0
2769273d9f13SBarry Smith     Proc1   0 18  0  | 19 20 21  |  0  0
2770273d9f13SBarry Smith             0  0  0  | 22 23  0  | 24  0
2771273d9f13SBarry Smith     -------------------------------------
2772273d9f13SBarry Smith     Proc2  25 26 27  |  0  0 28  | 29  0
2773273d9f13SBarry Smith            30  0  0  | 31 32 33  |  0 34
2774273d9f13SBarry Smith .ve
2775273d9f13SBarry Smith 
2776273d9f13SBarry Smith    This can be represented as a collection of submatrices as:
2777273d9f13SBarry Smith 
2778273d9f13SBarry Smith .vb
2779273d9f13SBarry Smith       A B C
2780273d9f13SBarry Smith       D E F
2781273d9f13SBarry Smith       G H I
2782273d9f13SBarry Smith .ve
2783273d9f13SBarry Smith 
2784273d9f13SBarry Smith    Where the submatrices A,B,C are owned by proc0, D,E,F are
2785273d9f13SBarry Smith    owned by proc1, G,H,I are owned by proc2.
2786273d9f13SBarry Smith 
2787273d9f13SBarry Smith    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2788273d9f13SBarry Smith    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2789273d9f13SBarry Smith    The 'M','N' parameters are 8,8, and have the same values on all procs.
2790273d9f13SBarry Smith 
2791273d9f13SBarry Smith    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
2792273d9f13SBarry Smith    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
2793273d9f13SBarry Smith    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
2794273d9f13SBarry Smith    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
2795273d9f13SBarry Smith    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
2796273d9f13SBarry Smith    matrix, ans [DF] as another SeqAIJ matrix.
2797273d9f13SBarry Smith 
2798273d9f13SBarry Smith    When d_nz, o_nz parameters are specified, d_nz storage elements are
2799273d9f13SBarry Smith    allocated for every row of the local diagonal submatrix, and o_nz
2800273d9f13SBarry Smith    storage locations are allocated for every row of the OFF-DIAGONAL submat.
2801273d9f13SBarry Smith    One way to choose d_nz and o_nz is to use the max nonzerors per local
2802273d9f13SBarry Smith    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
2803273d9f13SBarry Smith    In this case, the values of d_nz,o_nz are:
2804273d9f13SBarry Smith .vb
2805273d9f13SBarry Smith      proc0 : dnz = 2, o_nz = 2
2806273d9f13SBarry Smith      proc1 : dnz = 3, o_nz = 2
2807273d9f13SBarry Smith      proc2 : dnz = 1, o_nz = 4
2808273d9f13SBarry Smith .ve
2809273d9f13SBarry Smith    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
2810273d9f13SBarry Smith    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
2811273d9f13SBarry Smith    for proc3. i.e we are using 12+15+10=37 storage locations to store
2812273d9f13SBarry Smith    34 values.
2813273d9f13SBarry Smith 
2814273d9f13SBarry Smith    When d_nnz, o_nnz parameters are specified, the storage is specified
2815273d9f13SBarry Smith    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
2816273d9f13SBarry Smith    In the above case the values for d_nnz,o_nnz are:
2817273d9f13SBarry Smith .vb
2818273d9f13SBarry Smith      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
2819273d9f13SBarry Smith      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
2820273d9f13SBarry Smith      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
2821273d9f13SBarry Smith .ve
2822273d9f13SBarry Smith    Here the space allocated is sum of all the above values i.e 34, and
2823273d9f13SBarry Smith    hence pre-allocation is perfect.
2824273d9f13SBarry Smith 
2825273d9f13SBarry Smith    Level: intermediate
2826273d9f13SBarry Smith 
2827273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
2828273d9f13SBarry Smith 
2829ccd8e176SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
28302fb0ec9aSBarry Smith           MPIAIJ, MatCreateMPIAIJWithArrays()
2831273d9f13SBarry Smith @*/
2832be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateMPIAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[],Mat *A)
2833273d9f13SBarry Smith {
28346849ba73SBarry Smith   PetscErrorCode ierr;
2835b1d57f15SBarry Smith   PetscMPIInt    size;
2836273d9f13SBarry Smith 
2837273d9f13SBarry Smith   PetscFunctionBegin;
2838f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
2839f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*A,m,n,M,N);CHKERRQ(ierr);
2840273d9f13SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
2841273d9f13SBarry Smith   if (size > 1) {
2842273d9f13SBarry Smith     ierr = MatSetType(*A,MATMPIAIJ);CHKERRQ(ierr);
2843273d9f13SBarry Smith     ierr = MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr);
2844273d9f13SBarry Smith   } else {
2845273d9f13SBarry Smith     ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2846273d9f13SBarry Smith     ierr = MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);CHKERRQ(ierr);
2847273d9f13SBarry Smith   }
2848273d9f13SBarry Smith   PetscFunctionReturn(0);
2849273d9f13SBarry Smith }
2850195d93cdSBarry Smith 
28514a2ae208SSatish Balay #undef __FUNCT__
28524a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJGetSeqAIJ"
2853be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJGetSeqAIJ(Mat A,Mat *Ad,Mat *Ao,PetscInt *colmap[])
2854195d93cdSBarry Smith {
2855195d93cdSBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;
2856b1d57f15SBarry Smith 
2857195d93cdSBarry Smith   PetscFunctionBegin;
2858195d93cdSBarry Smith   *Ad     = a->A;
2859195d93cdSBarry Smith   *Ao     = a->B;
2860195d93cdSBarry Smith   *colmap = a->garray;
2861195d93cdSBarry Smith   PetscFunctionReturn(0);
2862195d93cdSBarry Smith }
2863a2243be0SBarry Smith 
2864a2243be0SBarry Smith #undef __FUNCT__
2865a2243be0SBarry Smith #define __FUNCT__ "MatSetColoring_MPIAIJ"
2866dfbe8321SBarry Smith PetscErrorCode MatSetColoring_MPIAIJ(Mat A,ISColoring coloring)
2867a2243be0SBarry Smith {
2868dfbe8321SBarry Smith   PetscErrorCode ierr;
2869b1d57f15SBarry Smith   PetscInt       i;
2870a2243be0SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
2871a2243be0SBarry Smith 
2872a2243be0SBarry Smith   PetscFunctionBegin;
2873a2243be0SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
287408b6dcc0SBarry Smith     ISColoringValue *allcolors,*colors;
2875a2243be0SBarry Smith     ISColoring      ocoloring;
2876a2243be0SBarry Smith 
2877a2243be0SBarry Smith     /* set coloring for diagonal portion */
2878a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->A,coloring);CHKERRQ(ierr);
2879a2243be0SBarry Smith 
2880a2243be0SBarry Smith     /* set coloring for off-diagonal portion */
288108b6dcc0SBarry Smith     ierr = ISAllGatherColors(A->comm,coloring->n,coloring->colors,PETSC_NULL,&allcolors);CHKERRQ(ierr);
2882899cda47SBarry Smith     ierr = PetscMalloc((a->B->cmap.n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
2883899cda47SBarry Smith     for (i=0; i<a->B->cmap.n; i++) {
2884a2243be0SBarry Smith       colors[i] = allcolors[a->garray[i]];
2885a2243be0SBarry Smith     }
2886a2243be0SBarry Smith     ierr = PetscFree(allcolors);CHKERRQ(ierr);
288795a80f87SBarry Smith     ierr = ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap.n,colors,&ocoloring);CHKERRQ(ierr);
2888a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr);
2889a2243be0SBarry Smith     ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr);
2890a2243be0SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
289108b6dcc0SBarry Smith     ISColoringValue *colors;
2892b1d57f15SBarry Smith     PetscInt        *larray;
2893a2243be0SBarry Smith     ISColoring      ocoloring;
2894a2243be0SBarry Smith 
2895a2243be0SBarry Smith     /* set coloring for diagonal portion */
2896899cda47SBarry Smith     ierr = PetscMalloc((a->A->cmap.n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr);
2897899cda47SBarry Smith     for (i=0; i<a->A->cmap.n; i++) {
2898899cda47SBarry Smith       larray[i] = i + A->cmap.rstart;
2899a2243be0SBarry Smith     }
2900899cda47SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->A->cmap.n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
2901899cda47SBarry Smith     ierr = PetscMalloc((a->A->cmap.n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
2902899cda47SBarry Smith     for (i=0; i<a->A->cmap.n; i++) {
2903a2243be0SBarry Smith       colors[i] = coloring->colors[larray[i]];
2904a2243be0SBarry Smith     }
2905a2243be0SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
290695a80f87SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,a->A->cmap.n,colors,&ocoloring);CHKERRQ(ierr);
2907a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->A,ocoloring);CHKERRQ(ierr);
2908a2243be0SBarry Smith     ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr);
2909a2243be0SBarry Smith 
2910a2243be0SBarry Smith     /* set coloring for off-diagonal portion */
2911899cda47SBarry Smith     ierr = PetscMalloc((a->B->cmap.n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr);
2912899cda47SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->B->cmap.n,a->garray,PETSC_NULL,larray);CHKERRQ(ierr);
2913899cda47SBarry Smith     ierr = PetscMalloc((a->B->cmap.n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
2914899cda47SBarry Smith     for (i=0; i<a->B->cmap.n; i++) {
2915a2243be0SBarry Smith       colors[i] = coloring->colors[larray[i]];
2916a2243be0SBarry Smith     }
2917a2243be0SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
291895a80f87SBarry Smith     ierr = ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap.n,colors,&ocoloring);CHKERRQ(ierr);
2919a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr);
2920a2243be0SBarry Smith     ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr);
2921a2243be0SBarry Smith   } else {
292277431f27SBarry Smith     SETERRQ1(PETSC_ERR_SUP,"No support ISColoringType %d",(int)coloring->ctype);
2923a2243be0SBarry Smith   }
2924a2243be0SBarry Smith 
2925a2243be0SBarry Smith   PetscFunctionReturn(0);
2926a2243be0SBarry Smith }
2927a2243be0SBarry Smith 
2928dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
2929a2243be0SBarry Smith #undef __FUNCT__
2930779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdic_MPIAIJ"
2931dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_MPIAIJ(Mat A,void *advalues)
2932a2243be0SBarry Smith {
2933a2243be0SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
2934dfbe8321SBarry Smith   PetscErrorCode ierr;
2935a2243be0SBarry Smith 
2936a2243be0SBarry Smith   PetscFunctionBegin;
2937779c1a83SBarry Smith   ierr = MatSetValuesAdic_SeqAIJ(a->A,advalues);CHKERRQ(ierr);
2938779c1a83SBarry Smith   ierr = MatSetValuesAdic_SeqAIJ(a->B,advalues);CHKERRQ(ierr);
2939779c1a83SBarry Smith   PetscFunctionReturn(0);
2940779c1a83SBarry Smith }
2941dcf5cc72SBarry Smith #endif
2942779c1a83SBarry Smith 
2943779c1a83SBarry Smith #undef __FUNCT__
2944779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdifor_MPIAIJ"
2945b1d57f15SBarry Smith PetscErrorCode MatSetValuesAdifor_MPIAIJ(Mat A,PetscInt nl,void *advalues)
2946779c1a83SBarry Smith {
2947779c1a83SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
2948dfbe8321SBarry Smith   PetscErrorCode ierr;
2949779c1a83SBarry Smith 
2950779c1a83SBarry Smith   PetscFunctionBegin;
2951779c1a83SBarry Smith   ierr = MatSetValuesAdifor_SeqAIJ(a->A,nl,advalues);CHKERRQ(ierr);
2952779c1a83SBarry Smith   ierr = MatSetValuesAdifor_SeqAIJ(a->B,nl,advalues);CHKERRQ(ierr);
2953a2243be0SBarry Smith   PetscFunctionReturn(0);
2954a2243be0SBarry Smith }
2955c5d6d63eSBarry Smith 
2956c5d6d63eSBarry Smith #undef __FUNCT__
295751dd7536SBarry Smith #define __FUNCT__ "MatMerge"
2958c5d6d63eSBarry Smith /*@C
295951dd7536SBarry Smith       MatMerge - Creates a single large PETSc matrix by concatinating sequential
296051dd7536SBarry Smith                  matrices from each processor
2961c5d6d63eSBarry Smith 
2962c5d6d63eSBarry Smith     Collective on MPI_Comm
2963c5d6d63eSBarry Smith 
2964c5d6d63eSBarry Smith    Input Parameters:
296551dd7536SBarry Smith +    comm - the communicators the parallel matrix will live on
2966d6bb3c2dSHong Zhang .    inmat - the input sequential matrices
29670e36024fSHong Zhang .    n - number of local columns (or PETSC_DECIDE)
2968d6bb3c2dSHong Zhang -    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
296951dd7536SBarry Smith 
297051dd7536SBarry Smith    Output Parameter:
297151dd7536SBarry Smith .    outmat - the parallel matrix generated
2972c5d6d63eSBarry Smith 
29737e25d530SSatish Balay     Level: advanced
29747e25d530SSatish Balay 
2975f08fae4eSHong Zhang    Notes: The number of columns of the matrix in EACH processor MUST be the same.
2976c5d6d63eSBarry Smith 
2977c5d6d63eSBarry Smith @*/
2978be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMerge(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
2979c5d6d63eSBarry Smith {
2980dfbe8321SBarry Smith   PetscErrorCode ierr;
2981b7940d39SSatish Balay   PetscInt       m,N,i,rstart,nnz,Ii,*dnz,*onz;
2982ba8c8a56SBarry Smith   PetscInt       *indx;
2983ba8c8a56SBarry Smith   PetscScalar    *values;
2984c5d6d63eSBarry Smith 
2985c5d6d63eSBarry Smith   PetscFunctionBegin;
29860e36024fSHong Zhang   ierr = MatGetSize(inmat,&m,&N);CHKERRQ(ierr);
2987d6bb3c2dSHong Zhang   if (scall == MAT_INITIAL_MATRIX){
2988d6bb3c2dSHong Zhang     /* count nonzeros in each row, for diagonal and off diagonal portion of matrix */
29890e36024fSHong Zhang     if (n == PETSC_DECIDE){
2990357abbc8SBarry Smith       ierr = PetscSplitOwnership(comm,&n,&N);CHKERRQ(ierr);
29910e36024fSHong Zhang     }
2992357abbc8SBarry Smith     ierr = MPI_Scan(&m, &rstart,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr);
2993357abbc8SBarry Smith     rstart -= m;
2994d6bb3c2dSHong Zhang 
2995d6bb3c2dSHong Zhang     ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr);
2996d6bb3c2dSHong Zhang     for (i=0;i<m;i++) {
2997ba8c8a56SBarry Smith       ierr = MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,PETSC_NULL);CHKERRQ(ierr);
2998d6bb3c2dSHong Zhang       ierr = MatPreallocateSet(i+rstart,nnz,indx,dnz,onz);CHKERRQ(ierr);
2999ba8c8a56SBarry Smith       ierr = MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,PETSC_NULL);CHKERRQ(ierr);
3000d6bb3c2dSHong Zhang     }
3001d6bb3c2dSHong Zhang     /* This routine will ONLY return MPIAIJ type matrix */
3002f69a0ea3SMatthew Knepley     ierr = MatCreate(comm,outmat);CHKERRQ(ierr);
3003f69a0ea3SMatthew Knepley     ierr = MatSetSizes(*outmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
3004d6bb3c2dSHong Zhang     ierr = MatSetType(*outmat,MATMPIAIJ);CHKERRQ(ierr);
3005d6bb3c2dSHong Zhang     ierr = MatMPIAIJSetPreallocation(*outmat,0,dnz,0,onz);CHKERRQ(ierr);
3006d6bb3c2dSHong Zhang     ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr);
3007d6bb3c2dSHong Zhang 
3008d6bb3c2dSHong Zhang   } else if (scall == MAT_REUSE_MATRIX){
3009d6bb3c2dSHong Zhang     ierr = MatGetOwnershipRange(*outmat,&rstart,PETSC_NULL);CHKERRQ(ierr);
3010d6bb3c2dSHong Zhang   } else {
301177431f27SBarry Smith     SETERRQ1(PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall);
3012d6bb3c2dSHong Zhang   }
3013d6bb3c2dSHong Zhang 
3014d6bb3c2dSHong Zhang   for (i=0;i<m;i++) {
3015ba8c8a56SBarry Smith     ierr = MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
3016b7940d39SSatish Balay     Ii    = i + rstart;
3017b7940d39SSatish Balay     ierr = MatSetValues(*outmat,1,&Ii,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr);
3018ba8c8a56SBarry Smith     ierr = MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
3019d6bb3c2dSHong Zhang   }
3020d6bb3c2dSHong Zhang   ierr = MatDestroy(inmat);CHKERRQ(ierr);
3021d6bb3c2dSHong Zhang   ierr = MatAssemblyBegin(*outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3022d6bb3c2dSHong Zhang   ierr = MatAssemblyEnd(*outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
302351dd7536SBarry Smith 
3024c5d6d63eSBarry Smith   PetscFunctionReturn(0);
3025c5d6d63eSBarry Smith }
3026c5d6d63eSBarry Smith 
3027c5d6d63eSBarry Smith #undef __FUNCT__
3028c5d6d63eSBarry Smith #define __FUNCT__ "MatFileSplit"
3029dfbe8321SBarry Smith PetscErrorCode MatFileSplit(Mat A,char *outfile)
3030c5d6d63eSBarry Smith {
3031dfbe8321SBarry Smith   PetscErrorCode    ierr;
303232dcc486SBarry Smith   PetscMPIInt       rank;
3033b1d57f15SBarry Smith   PetscInt          m,N,i,rstart,nnz;
3034de4209c5SBarry Smith   size_t            len;
3035b1d57f15SBarry Smith   const PetscInt    *indx;
3036c5d6d63eSBarry Smith   PetscViewer       out;
3037c5d6d63eSBarry Smith   char              *name;
3038c5d6d63eSBarry Smith   Mat               B;
3039b3cc6726SBarry Smith   const PetscScalar *values;
3040c5d6d63eSBarry Smith 
3041c5d6d63eSBarry Smith   PetscFunctionBegin;
3042c5d6d63eSBarry Smith   ierr = MatGetLocalSize(A,&m,0);CHKERRQ(ierr);
3043c5d6d63eSBarry Smith   ierr = MatGetSize(A,0,&N);CHKERRQ(ierr);
3044f204ca49SKris Buschelman   /* Should this be the type of the diagonal block of A? */
3045f69a0ea3SMatthew Knepley   ierr = MatCreate(PETSC_COMM_SELF,&B);CHKERRQ(ierr);
3046f69a0ea3SMatthew Knepley   ierr = MatSetSizes(B,m,N,m,N);CHKERRQ(ierr);
3047f204ca49SKris Buschelman   ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
3048f204ca49SKris Buschelman   ierr = MatSeqAIJSetPreallocation(B,0,PETSC_NULL);CHKERRQ(ierr);
3049c5d6d63eSBarry Smith   ierr = MatGetOwnershipRange(A,&rstart,0);CHKERRQ(ierr);
3050c5d6d63eSBarry Smith   for (i=0;i<m;i++) {
3051c5d6d63eSBarry Smith     ierr = MatGetRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr);
3052c5d6d63eSBarry Smith     ierr = MatSetValues(B,1,&i,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr);
3053c5d6d63eSBarry Smith     ierr = MatRestoreRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr);
3054c5d6d63eSBarry Smith   }
3055c5d6d63eSBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3056c5d6d63eSBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3057c5d6d63eSBarry Smith 
3058c5d6d63eSBarry Smith   ierr = MPI_Comm_rank(A->comm,&rank);CHKERRQ(ierr);
3059c5d6d63eSBarry Smith   ierr = PetscStrlen(outfile,&len);CHKERRQ(ierr);
3060c5d6d63eSBarry Smith   ierr = PetscMalloc((len+5)*sizeof(char),&name);CHKERRQ(ierr);
3061c5d6d63eSBarry Smith   sprintf(name,"%s.%d",outfile,rank);
3062852598b0SBarry Smith   ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,name,FILE_MODE_APPEND,&out);CHKERRQ(ierr);
3063c5d6d63eSBarry Smith   ierr = PetscFree(name);
3064c5d6d63eSBarry Smith   ierr = MatView(B,out);CHKERRQ(ierr);
3065c5d6d63eSBarry Smith   ierr = PetscViewerDestroy(out);CHKERRQ(ierr);
3066c5d6d63eSBarry Smith   ierr = MatDestroy(B);CHKERRQ(ierr);
3067c5d6d63eSBarry Smith   PetscFunctionReturn(0);
3068c5d6d63eSBarry Smith }
3069e5f2cdd8SHong Zhang 
307051a7d1a8SHong Zhang EXTERN PetscErrorCode MatDestroy_MPIAIJ(Mat);
307151a7d1a8SHong Zhang #undef __FUNCT__
307251a7d1a8SHong Zhang #define __FUNCT__ "MatDestroy_MPIAIJ_SeqsToMPI"
3073be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatDestroy_MPIAIJ_SeqsToMPI(Mat A)
307451a7d1a8SHong Zhang {
307551a7d1a8SHong Zhang   PetscErrorCode       ierr;
3076671beff6SHong Zhang   Mat_Merge_SeqsToMPI  *merge;
3077671beff6SHong Zhang   PetscObjectContainer container;
307851a7d1a8SHong Zhang 
307951a7d1a8SHong Zhang   PetscFunctionBegin;
3080671beff6SHong Zhang   ierr = PetscObjectQuery((PetscObject)A,"MatMergeSeqsToMPI",(PetscObject *)&container);CHKERRQ(ierr);
3081671beff6SHong Zhang   if (container) {
30827f79fd58SMatthew Knepley     ierr = PetscObjectContainerGetPointer(container,(void **)&merge);CHKERRQ(ierr);
308351a7d1a8SHong Zhang     ierr = PetscFree(merge->id_r);CHKERRQ(ierr);
30843e06a4e6SHong Zhang     ierr = PetscFree(merge->len_s);CHKERRQ(ierr);
30853e06a4e6SHong Zhang     ierr = PetscFree(merge->len_r);CHKERRQ(ierr);
308651a7d1a8SHong Zhang     ierr = PetscFree(merge->bi);CHKERRQ(ierr);
308751a7d1a8SHong Zhang     ierr = PetscFree(merge->bj);CHKERRQ(ierr);
308802c68681SHong Zhang     ierr = PetscFree(merge->buf_ri);CHKERRQ(ierr);
308902c68681SHong Zhang     ierr = PetscFree(merge->buf_rj);CHKERRQ(ierr);
309005b42c5fSBarry Smith     ierr = PetscFree(merge->coi);CHKERRQ(ierr);
309105b42c5fSBarry Smith     ierr = PetscFree(merge->coj);CHKERRQ(ierr);
309205b42c5fSBarry Smith     ierr = PetscFree(merge->owners_co);CHKERRQ(ierr);
30932c72b5baSSatish Balay     ierr = PetscFree(merge->rowmap.range);CHKERRQ(ierr);
3094671beff6SHong Zhang 
3095671beff6SHong Zhang     ierr = PetscObjectContainerDestroy(container);CHKERRQ(ierr);
3096671beff6SHong Zhang     ierr = PetscObjectCompose((PetscObject)A,"MatMergeSeqsToMPI",0);CHKERRQ(ierr);
3097671beff6SHong Zhang   }
309851a7d1a8SHong Zhang   ierr = PetscFree(merge);CHKERRQ(ierr);
309951a7d1a8SHong Zhang 
310051a7d1a8SHong Zhang   ierr = MatDestroy_MPIAIJ(A);CHKERRQ(ierr);
310151a7d1a8SHong Zhang   PetscFunctionReturn(0);
310251a7d1a8SHong Zhang }
310351a7d1a8SHong Zhang 
310458cb9c82SHong Zhang #include "src/mat/utils/freespace.h"
3105be0fcf8dSHong Zhang #include "petscbt.h"
310638f152feSBarry Smith static PetscEvent logkey_seqstompinum = 0;
3107e5f2cdd8SHong Zhang #undef __FUNCT__
310838f152feSBarry Smith #define __FUNCT__ "MatMerge_SeqsToMPINumeric"
3109e5f2cdd8SHong Zhang /*@C
3110f08fae4eSHong Zhang       MatMerge_SeqsToMPI - Creates a MPIAIJ matrix by adding sequential
3111e5f2cdd8SHong Zhang                  matrices from each processor
3112e5f2cdd8SHong Zhang 
3113e5f2cdd8SHong Zhang     Collective on MPI_Comm
3114e5f2cdd8SHong Zhang 
3115e5f2cdd8SHong Zhang    Input Parameters:
3116e5f2cdd8SHong Zhang +    comm - the communicators the parallel matrix will live on
3117f08fae4eSHong Zhang .    seqmat - the input sequential matrices
31180e36024fSHong Zhang .    m - number of local rows (or PETSC_DECIDE)
31190e36024fSHong Zhang .    n - number of local columns (or PETSC_DECIDE)
3120e5f2cdd8SHong Zhang -    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
3121e5f2cdd8SHong Zhang 
3122e5f2cdd8SHong Zhang    Output Parameter:
3123f08fae4eSHong Zhang .    mpimat - the parallel matrix generated
3124e5f2cdd8SHong Zhang 
3125e5f2cdd8SHong Zhang     Level: advanced
3126e5f2cdd8SHong Zhang 
3127affca5deSHong Zhang    Notes:
3128affca5deSHong Zhang      The dimensions of the sequential matrix in each processor MUST be the same.
3129affca5deSHong Zhang      The input seqmat is included into the container "Mat_Merge_SeqsToMPI", and will be
3130affca5deSHong Zhang      destroyed when mpimat is destroyed. Call PetscObjectQuery() to access seqmat.
3131e5f2cdd8SHong Zhang @*/
3132be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMerge_SeqsToMPINumeric(Mat seqmat,Mat mpimat)
313355d1abb9SHong Zhang {
313455d1abb9SHong Zhang   PetscErrorCode       ierr;
313555d1abb9SHong Zhang   MPI_Comm             comm=mpimat->comm;
313655d1abb9SHong Zhang   Mat_SeqAIJ           *a=(Mat_SeqAIJ*)seqmat->data;
3137b1d57f15SBarry Smith   PetscMPIInt          size,rank,taga,*len_s;
3138899cda47SBarry Smith   PetscInt             N=mpimat->cmap.N,i,j,*owners,*ai=a->i,*aj=a->j;
3139b1d57f15SBarry Smith   PetscInt             proc,m;
3140b1d57f15SBarry Smith   PetscInt             **buf_ri,**buf_rj;
3141b1d57f15SBarry Smith   PetscInt             k,anzi,*bj_i,*bi,*bj,arow,bnzi,nextaj;
3142b1d57f15SBarry Smith   PetscInt             nrows,**buf_ri_k,**nextrow,**nextai;
314355d1abb9SHong Zhang   MPI_Request          *s_waits,*r_waits;
314455d1abb9SHong Zhang   MPI_Status           *status;
3145ce805ee4SHong Zhang   MatScalar            *aa=a->a,**abuf_r,*ba_i;
314655d1abb9SHong Zhang   Mat_Merge_SeqsToMPI  *merge;
314755d1abb9SHong Zhang   PetscObjectContainer container;
314855d1abb9SHong Zhang 
314955d1abb9SHong Zhang   PetscFunctionBegin;
31503c2c1871SHong Zhang   if (!logkey_seqstompinum) {
31513c2c1871SHong Zhang     ierr = PetscLogEventRegister(&logkey_seqstompinum,"MatMerge_SeqsToMPINumeric",MAT_COOKIE);
31523c2c1871SHong Zhang   }
31533c2c1871SHong Zhang   ierr = PetscLogEventBegin(logkey_seqstompinum,seqmat,0,0,0);CHKERRQ(ierr);
31543c2c1871SHong Zhang 
315555d1abb9SHong Zhang   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
315655d1abb9SHong Zhang   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
315755d1abb9SHong Zhang 
315855d1abb9SHong Zhang   ierr = PetscObjectQuery((PetscObject)mpimat,"MatMergeSeqsToMPI",(PetscObject *)&container);CHKERRQ(ierr);
315955d1abb9SHong Zhang   if (container) {
316055d1abb9SHong Zhang     ierr  = PetscObjectContainerGetPointer(container,(void **)&merge);CHKERRQ(ierr);
316155d1abb9SHong Zhang   }
316255d1abb9SHong Zhang   bi     = merge->bi;
316355d1abb9SHong Zhang   bj     = merge->bj;
316455d1abb9SHong Zhang   buf_ri = merge->buf_ri;
316555d1abb9SHong Zhang   buf_rj = merge->buf_rj;
316655d1abb9SHong Zhang 
316755d1abb9SHong Zhang   ierr   = PetscMalloc(size*sizeof(MPI_Status),&status);CHKERRQ(ierr);
3168357abbc8SBarry Smith   owners = merge->rowmap.range;
316955d1abb9SHong Zhang   len_s  = merge->len_s;
317055d1abb9SHong Zhang 
317155d1abb9SHong Zhang   /* send and recv matrix values */
317255d1abb9SHong Zhang   /*-----------------------------*/
3173357abbc8SBarry Smith   ierr = PetscObjectGetNewTag((PetscObject)mpimat,&taga);CHKERRQ(ierr);
317455d1abb9SHong Zhang   ierr = PetscPostIrecvScalar(comm,taga,merge->nrecv,merge->id_r,merge->len_r,&abuf_r,&r_waits);CHKERRQ(ierr);
317555d1abb9SHong Zhang 
317655d1abb9SHong Zhang   ierr = PetscMalloc((merge->nsend+1)*sizeof(MPI_Request),&s_waits);CHKERRQ(ierr);
317755d1abb9SHong Zhang   for (proc=0,k=0; proc<size; proc++){
317855d1abb9SHong Zhang     if (!len_s[proc]) continue;
317955d1abb9SHong Zhang     i = owners[proc];
318055d1abb9SHong Zhang     ierr = MPI_Isend(aa+ai[i],len_s[proc],MPIU_MATSCALAR,proc,taga,comm,s_waits+k);CHKERRQ(ierr);
318155d1abb9SHong Zhang     k++;
318255d1abb9SHong Zhang   }
318355d1abb9SHong Zhang 
31840c468ba9SBarry Smith   if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,r_waits,status);CHKERRQ(ierr);}
31850c468ba9SBarry Smith   if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,s_waits,status);CHKERRQ(ierr);}
318655d1abb9SHong Zhang   ierr = PetscFree(status);CHKERRQ(ierr);
318755d1abb9SHong Zhang 
318855d1abb9SHong Zhang   ierr = PetscFree(s_waits);CHKERRQ(ierr);
318955d1abb9SHong Zhang   ierr = PetscFree(r_waits);CHKERRQ(ierr);
319055d1abb9SHong Zhang 
319155d1abb9SHong Zhang   /* insert mat values of mpimat */
319255d1abb9SHong Zhang   /*----------------------------*/
319355d1abb9SHong Zhang   ierr = PetscMalloc(N*sizeof(MatScalar),&ba_i);CHKERRQ(ierr);
3194b1d57f15SBarry Smith   ierr = PetscMalloc((3*merge->nrecv+1)*sizeof(PetscInt**),&buf_ri_k);CHKERRQ(ierr);
319555d1abb9SHong Zhang   nextrow = buf_ri_k + merge->nrecv;
319655d1abb9SHong Zhang   nextai  = nextrow + merge->nrecv;
319755d1abb9SHong Zhang 
319855d1abb9SHong Zhang   for (k=0; k<merge->nrecv; k++){
319955d1abb9SHong Zhang     buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
320055d1abb9SHong Zhang     nrows = *(buf_ri_k[k]);
320155d1abb9SHong Zhang     nextrow[k]  = buf_ri_k[k]+1;  /* next row number of k-th recved i-structure */
320255d1abb9SHong Zhang     nextai[k]   = buf_ri_k[k] + (nrows + 1);/* poins to the next i-structure of k-th recved i-structure  */
320355d1abb9SHong Zhang   }
320455d1abb9SHong Zhang 
320555d1abb9SHong Zhang   /* set values of ba */
3206357abbc8SBarry Smith   m = merge->rowmap.n;
320755d1abb9SHong Zhang   for (i=0; i<m; i++) {
320855d1abb9SHong Zhang     arow = owners[rank] + i;
320955d1abb9SHong Zhang     bj_i = bj+bi[i];  /* col indices of the i-th row of mpimat */
321055d1abb9SHong Zhang     bnzi = bi[i+1] - bi[i];
321155d1abb9SHong Zhang     ierr = PetscMemzero(ba_i,bnzi*sizeof(MatScalar));CHKERRQ(ierr);
321255d1abb9SHong Zhang 
321355d1abb9SHong Zhang     /* add local non-zero vals of this proc's seqmat into ba */
321455d1abb9SHong Zhang     anzi = ai[arow+1] - ai[arow];
321555d1abb9SHong Zhang     aj   = a->j + ai[arow];
321655d1abb9SHong Zhang     aa   = a->a + ai[arow];
321755d1abb9SHong Zhang     nextaj = 0;
321855d1abb9SHong Zhang     for (j=0; nextaj<anzi; j++){
321955d1abb9SHong Zhang       if (*(bj_i + j) == aj[nextaj]){ /* bcol == acol */
322055d1abb9SHong Zhang         ba_i[j] += aa[nextaj++];
322155d1abb9SHong Zhang       }
322255d1abb9SHong Zhang     }
322355d1abb9SHong Zhang 
322455d1abb9SHong Zhang     /* add received vals into ba */
322555d1abb9SHong Zhang     for (k=0; k<merge->nrecv; k++){ /* k-th received message */
322655d1abb9SHong Zhang       /* i-th row */
322755d1abb9SHong Zhang       if (i == *nextrow[k]) {
322855d1abb9SHong Zhang         anzi = *(nextai[k]+1) - *nextai[k];
322955d1abb9SHong Zhang         aj   = buf_rj[k] + *(nextai[k]);
323055d1abb9SHong Zhang         aa   = abuf_r[k] + *(nextai[k]);
323155d1abb9SHong Zhang         nextaj = 0;
323255d1abb9SHong Zhang         for (j=0; nextaj<anzi; j++){
323355d1abb9SHong Zhang           if (*(bj_i + j) == aj[nextaj]){ /* bcol == acol */
323455d1abb9SHong Zhang             ba_i[j] += aa[nextaj++];
323555d1abb9SHong Zhang           }
323655d1abb9SHong Zhang         }
323755d1abb9SHong Zhang         nextrow[k]++; nextai[k]++;
323855d1abb9SHong Zhang       }
323955d1abb9SHong Zhang     }
324055d1abb9SHong Zhang     ierr = MatSetValues(mpimat,1,&arow,bnzi,bj_i,ba_i,INSERT_VALUES);CHKERRQ(ierr);
324155d1abb9SHong Zhang   }
324255d1abb9SHong Zhang   ierr = MatAssemblyBegin(mpimat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
324355d1abb9SHong Zhang   ierr = MatAssemblyEnd(mpimat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
324455d1abb9SHong Zhang 
324555d1abb9SHong Zhang   ierr = PetscFree(abuf_r);CHKERRQ(ierr);
324655d1abb9SHong Zhang   ierr = PetscFree(ba_i);CHKERRQ(ierr);
324755d1abb9SHong Zhang   ierr = PetscFree(buf_ri_k);CHKERRQ(ierr);
32483c2c1871SHong Zhang   ierr = PetscLogEventEnd(logkey_seqstompinum,seqmat,0,0,0);CHKERRQ(ierr);
324955d1abb9SHong Zhang   PetscFunctionReturn(0);
325055d1abb9SHong Zhang }
325138f152feSBarry Smith 
32523c2c1871SHong Zhang static PetscEvent logkey_seqstompisym = 0;
325338f152feSBarry Smith #undef __FUNCT__
325438f152feSBarry Smith #define __FUNCT__ "MatMerge_SeqsToMPISymbolic"
3255be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMerge_SeqsToMPISymbolic(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,Mat *mpimat)
3256e5f2cdd8SHong Zhang {
3257f08fae4eSHong Zhang   PetscErrorCode       ierr;
325855a3bba9SHong Zhang   Mat                  B_mpi;
3259c2234fe3SHong Zhang   Mat_SeqAIJ           *a=(Mat_SeqAIJ*)seqmat->data;
3260b1d57f15SBarry Smith   PetscMPIInt          size,rank,tagi,tagj,*len_s,*len_si,*len_ri;
3261b1d57f15SBarry Smith   PetscInt             **buf_rj,**buf_ri,**buf_ri_k;
3262899cda47SBarry Smith   PetscInt             M=seqmat->rmap.n,N=seqmat->cmap.n,i,*owners,*ai=a->i,*aj=a->j;
3263b1d57f15SBarry Smith   PetscInt             len,proc,*dnz,*onz;
3264b1d57f15SBarry Smith   PetscInt             k,anzi,*bi,*bj,*lnk,nlnk,arow,bnzi,nspacedouble=0;
3265b1d57f15SBarry Smith   PetscInt             nrows,*buf_s,*buf_si,*buf_si_i,**nextrow,**nextai;
326655d1abb9SHong Zhang   MPI_Request          *si_waits,*sj_waits,*ri_waits,*rj_waits;
326758cb9c82SHong Zhang   MPI_Status           *status;
3268a1a86e44SBarry Smith   PetscFreeSpaceList   free_space=PETSC_NULL,current_space=PETSC_NULL;
3269be0fcf8dSHong Zhang   PetscBT              lnkbt;
327051a7d1a8SHong Zhang   Mat_Merge_SeqsToMPI  *merge;
3271671beff6SHong Zhang   PetscObjectContainer container;
327202c68681SHong Zhang 
3273e5f2cdd8SHong Zhang   PetscFunctionBegin;
32743c2c1871SHong Zhang   if (!logkey_seqstompisym) {
32753c2c1871SHong Zhang     ierr = PetscLogEventRegister(&logkey_seqstompisym,"MatMerge_SeqsToMPISymbolic",MAT_COOKIE);
32763c2c1871SHong Zhang   }
32773c2c1871SHong Zhang   ierr = PetscLogEventBegin(logkey_seqstompisym,seqmat,0,0,0);CHKERRQ(ierr);
32783c2c1871SHong Zhang 
327938f152feSBarry Smith   /* make sure it is a PETSc comm */
328038f152feSBarry Smith   ierr = PetscCommDuplicate(comm,&comm,PETSC_NULL);CHKERRQ(ierr);
3281e5f2cdd8SHong Zhang   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
3282e5f2cdd8SHong Zhang   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
328355d1abb9SHong Zhang 
328451a7d1a8SHong Zhang   ierr = PetscNew(Mat_Merge_SeqsToMPI,&merge);CHKERRQ(ierr);
3285c2234fe3SHong Zhang   ierr = PetscMalloc(size*sizeof(MPI_Status),&status);CHKERRQ(ierr);
3286e5f2cdd8SHong Zhang 
32876abd8857SHong Zhang   /* determine row ownership */
3288f08fae4eSHong Zhang   /*---------------------------------------------------------*/
3289899cda47SBarry Smith   merge->rowmap.n = m;
3290899cda47SBarry Smith   merge->rowmap.N = M;
3291fc42d0c8SSatish Balay   merge->rowmap.bs = 1;
3292899cda47SBarry Smith   ierr = PetscMapInitialize(comm,&merge->rowmap);CHKERRQ(ierr);
3293b1d57f15SBarry Smith   ierr = PetscMalloc(size*sizeof(PetscMPIInt),&len_si);CHKERRQ(ierr);
3294b1d57f15SBarry Smith   ierr = PetscMalloc(size*sizeof(PetscMPIInt),&merge->len_s);CHKERRQ(ierr);
329555d1abb9SHong Zhang 
3296357abbc8SBarry Smith   m      = merge->rowmap.n;
3297357abbc8SBarry Smith   M      = merge->rowmap.N;
3298357abbc8SBarry Smith   owners = merge->rowmap.range;
32996abd8857SHong Zhang 
33006abd8857SHong Zhang   /* determine the number of messages to send, their lengths */
33016abd8857SHong Zhang   /*---------------------------------------------------------*/
33023e06a4e6SHong Zhang   len_s  = merge->len_s;
330351a7d1a8SHong Zhang 
33042257cef7SHong Zhang   len = 0;  /* length of buf_si[] */
3305c2234fe3SHong Zhang   merge->nsend = 0;
3306409913e3SHong Zhang   for (proc=0; proc<size; proc++){
33072257cef7SHong Zhang     len_si[proc] = 0;
33083e06a4e6SHong Zhang     if (proc == rank){
33096abd8857SHong Zhang       len_s[proc] = 0;
33103e06a4e6SHong Zhang     } else {
331102c68681SHong Zhang       len_si[proc] = owners[proc+1] - owners[proc] + 1;
33123e06a4e6SHong Zhang       len_s[proc] = ai[owners[proc+1]] - ai[owners[proc]]; /* num of rows to be sent to [proc] */
33133e06a4e6SHong Zhang     }
33143e06a4e6SHong Zhang     if (len_s[proc]) {
3315c2234fe3SHong Zhang       merge->nsend++;
33162257cef7SHong Zhang       nrows = 0;
33172257cef7SHong Zhang       for (i=owners[proc]; i<owners[proc+1]; i++){
33182257cef7SHong Zhang         if (ai[i+1] > ai[i]) nrows++;
33192257cef7SHong Zhang       }
33202257cef7SHong Zhang       len_si[proc] = 2*(nrows+1);
33212257cef7SHong Zhang       len += len_si[proc];
3322409913e3SHong Zhang     }
332358cb9c82SHong Zhang   }
3324409913e3SHong Zhang 
33252257cef7SHong Zhang   /* determine the number and length of messages to receive for ij-structure */
33262257cef7SHong Zhang   /*-------------------------------------------------------------------------*/
332751a7d1a8SHong Zhang   ierr = PetscGatherNumberOfMessages(comm,PETSC_NULL,len_s,&merge->nrecv);CHKERRQ(ierr);
332855d1abb9SHong Zhang   ierr = PetscGatherMessageLengths2(comm,merge->nsend,merge->nrecv,len_s,len_si,&merge->id_r,&merge->len_r,&len_ri);CHKERRQ(ierr);
3329671beff6SHong Zhang 
33303e06a4e6SHong Zhang   /* post the Irecv of j-structure */
33313e06a4e6SHong Zhang   /*-------------------------------*/
33322c72b5baSSatish Balay   ierr = PetscCommGetNewTag(comm,&tagj);CHKERRQ(ierr);
33333e06a4e6SHong Zhang   ierr = PetscPostIrecvInt(comm,tagj,merge->nrecv,merge->id_r,merge->len_r,&buf_rj,&rj_waits);CHKERRQ(ierr);
333402c68681SHong Zhang 
33353e06a4e6SHong Zhang   /* post the Isend of j-structure */
3336affca5deSHong Zhang   /*--------------------------------*/
33372257cef7SHong Zhang   ierr = PetscMalloc((2*merge->nsend+1)*sizeof(MPI_Request),&si_waits);CHKERRQ(ierr);
333802c68681SHong Zhang   sj_waits = si_waits + merge->nsend;
33393e06a4e6SHong Zhang 
33402257cef7SHong Zhang   for (proc=0, k=0; proc<size; proc++){
3341409913e3SHong Zhang     if (!len_s[proc]) continue;
334202c68681SHong Zhang     i = owners[proc];
3343b1d57f15SBarry Smith     ierr = MPI_Isend(aj+ai[i],len_s[proc],MPIU_INT,proc,tagj,comm,sj_waits+k);CHKERRQ(ierr);
334451a7d1a8SHong Zhang     k++;
334551a7d1a8SHong Zhang   }
334651a7d1a8SHong Zhang 
33473e06a4e6SHong Zhang   /* receives and sends of j-structure are complete */
33483e06a4e6SHong Zhang   /*------------------------------------------------*/
33490c468ba9SBarry Smith   if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,rj_waits,status);CHKERRQ(ierr);}
33500c468ba9SBarry Smith   if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,sj_waits,status);CHKERRQ(ierr);}
335102c68681SHong Zhang 
335202c68681SHong Zhang   /* send and recv i-structure */
335302c68681SHong Zhang   /*---------------------------*/
33542c72b5baSSatish Balay   ierr = PetscCommGetNewTag(comm,&tagi);CHKERRQ(ierr);
335502c68681SHong Zhang   ierr = PetscPostIrecvInt(comm,tagi,merge->nrecv,merge->id_r,len_ri,&buf_ri,&ri_waits);CHKERRQ(ierr);
335602c68681SHong Zhang 
3357b1d57f15SBarry Smith   ierr = PetscMalloc((len+1)*sizeof(PetscInt),&buf_s);CHKERRQ(ierr);
33583e06a4e6SHong Zhang   buf_si = buf_s;  /* points to the beginning of k-th msg to be sent */
33592257cef7SHong Zhang   for (proc=0,k=0; proc<size; proc++){
336002c68681SHong Zhang     if (!len_s[proc]) continue;
33613e06a4e6SHong Zhang     /* form outgoing message for i-structure:
33623e06a4e6SHong Zhang          buf_si[0]:                 nrows to be sent
33633e06a4e6SHong Zhang                [1:nrows]:           row index (global)
33643e06a4e6SHong Zhang                [nrows+1:2*nrows+1]: i-structure index
33653e06a4e6SHong Zhang     */
33663e06a4e6SHong Zhang     /*-------------------------------------------*/
33672257cef7SHong Zhang     nrows = len_si[proc]/2 - 1;
33683e06a4e6SHong Zhang     buf_si_i    = buf_si + nrows+1;
33693e06a4e6SHong Zhang     buf_si[0]   = nrows;
33703e06a4e6SHong Zhang     buf_si_i[0] = 0;
33713e06a4e6SHong Zhang     nrows = 0;
33723e06a4e6SHong Zhang     for (i=owners[proc]; i<owners[proc+1]; i++){
33733e06a4e6SHong Zhang       anzi = ai[i+1] - ai[i];
33743e06a4e6SHong Zhang       if (anzi) {
33753e06a4e6SHong Zhang         buf_si_i[nrows+1] = buf_si_i[nrows] + anzi; /* i-structure */
33763e06a4e6SHong Zhang         buf_si[nrows+1] = i-owners[proc]; /* local row index */
33773e06a4e6SHong Zhang         nrows++;
33783e06a4e6SHong Zhang       }
33793e06a4e6SHong Zhang     }
3380b1d57f15SBarry Smith     ierr = MPI_Isend(buf_si,len_si[proc],MPIU_INT,proc,tagi,comm,si_waits+k);CHKERRQ(ierr);
338102c68681SHong Zhang     k++;
33822257cef7SHong Zhang     buf_si += len_si[proc];
338302c68681SHong Zhang   }
33842257cef7SHong Zhang 
33850c468ba9SBarry Smith   if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,ri_waits,status);CHKERRQ(ierr);}
33860c468ba9SBarry Smith   if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,si_waits,status);CHKERRQ(ierr);}
338702c68681SHong Zhang 
3388ae15b995SBarry Smith   ierr = PetscInfo2(seqmat,"nsend: %D, nrecv: %D\n",merge->nsend,merge->nrecv);CHKERRQ(ierr);
33893e06a4e6SHong Zhang   for (i=0; i<merge->nrecv; i++){
3390ae15b995SBarry Smith     ierr = PetscInfo3(seqmat,"recv len_ri=%D, len_rj=%D from [%D]\n",len_ri[i],merge->len_r[i],merge->id_r[i]);CHKERRQ(ierr);
33913e06a4e6SHong Zhang   }
33923e06a4e6SHong Zhang 
33933e06a4e6SHong Zhang   ierr = PetscFree(len_si);CHKERRQ(ierr);
339402c68681SHong Zhang   ierr = PetscFree(len_ri);CHKERRQ(ierr);
339502c68681SHong Zhang   ierr = PetscFree(rj_waits);CHKERRQ(ierr);
33963e06a4e6SHong Zhang   ierr = PetscFree(si_waits);CHKERRQ(ierr);
33972257cef7SHong Zhang   ierr = PetscFree(ri_waits);CHKERRQ(ierr);
33983e06a4e6SHong Zhang   ierr = PetscFree(buf_s);CHKERRQ(ierr);
3399bcc1bcd5SHong Zhang   ierr = PetscFree(status);CHKERRQ(ierr);
340058cb9c82SHong Zhang 
3401bcc1bcd5SHong Zhang   /* compute a local seq matrix in each processor */
3402bcc1bcd5SHong Zhang   /*----------------------------------------------*/
340358cb9c82SHong Zhang   /* allocate bi array and free space for accumulating nonzero column info */
3404b1d57f15SBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&bi);CHKERRQ(ierr);
340558cb9c82SHong Zhang   bi[0] = 0;
340658cb9c82SHong Zhang 
3407be0fcf8dSHong Zhang   /* create and initialize a linked list */
3408be0fcf8dSHong Zhang   nlnk = N+1;
3409be0fcf8dSHong Zhang   ierr = PetscLLCreate(N,N,nlnk,lnk,lnkbt);CHKERRQ(ierr);
341058cb9c82SHong Zhang 
3411bcc1bcd5SHong Zhang   /* initial FreeSpace size is 2*(num of local nnz(seqmat)) */
341258cb9c82SHong Zhang   len = 0;
3413bcc1bcd5SHong Zhang   len  = ai[owners[rank+1]] - ai[owners[rank]];
3414a1a86e44SBarry Smith   ierr = PetscFreeSpaceGet((PetscInt)(2*len+1),&free_space);CHKERRQ(ierr);
341558cb9c82SHong Zhang   current_space = free_space;
341658cb9c82SHong Zhang 
3417bcc1bcd5SHong Zhang   /* determine symbolic info for each local row */
3418b1d57f15SBarry Smith   ierr = PetscMalloc((3*merge->nrecv+1)*sizeof(PetscInt**),&buf_ri_k);CHKERRQ(ierr);
34193e06a4e6SHong Zhang   nextrow = buf_ri_k + merge->nrecv;
34203e06a4e6SHong Zhang   nextai  = nextrow + merge->nrecv;
34213e06a4e6SHong Zhang   for (k=0; k<merge->nrecv; k++){
34222257cef7SHong Zhang     buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
34233e06a4e6SHong Zhang     nrows = *buf_ri_k[k];
34243e06a4e6SHong Zhang     nextrow[k]  = buf_ri_k[k] + 1;  /* next row number of k-th recved i-structure */
34252257cef7SHong Zhang     nextai[k]   = buf_ri_k[k] + (nrows + 1);/* poins to the next i-structure of k-th recved i-structure  */
34263e06a4e6SHong Zhang   }
34272257cef7SHong Zhang 
3428bcc1bcd5SHong Zhang   ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr);
3429bcc1bcd5SHong Zhang   len = 0;
343058cb9c82SHong Zhang   for (i=0;i<m;i++) {
343158cb9c82SHong Zhang     bnzi   = 0;
343258cb9c82SHong Zhang     /* add local non-zero cols of this proc's seqmat into lnk */
343358cb9c82SHong Zhang     arow   = owners[rank] + i;
343458cb9c82SHong Zhang     anzi   = ai[arow+1] - ai[arow];
343558cb9c82SHong Zhang     aj     = a->j + ai[arow];
3436be0fcf8dSHong Zhang     ierr = PetscLLAdd(anzi,aj,N,nlnk,lnk,lnkbt);CHKERRQ(ierr);
343758cb9c82SHong Zhang     bnzi += nlnk;
343858cb9c82SHong Zhang     /* add received col data into lnk */
343951a7d1a8SHong Zhang     for (k=0; k<merge->nrecv; k++){ /* k-th received message */
344055d1abb9SHong Zhang       if (i == *nextrow[k]) { /* i-th row */
34413e06a4e6SHong Zhang         anzi = *(nextai[k]+1) - *nextai[k];
34423e06a4e6SHong Zhang         aj   = buf_rj[k] + *nextai[k];
34433e06a4e6SHong Zhang         ierr = PetscLLAdd(anzi,aj,N,nlnk,lnk,lnkbt);CHKERRQ(ierr);
34443e06a4e6SHong Zhang         bnzi += nlnk;
34453e06a4e6SHong Zhang         nextrow[k]++; nextai[k]++;
34463e06a4e6SHong Zhang       }
344758cb9c82SHong Zhang     }
3448bcc1bcd5SHong Zhang     if (len < bnzi) len = bnzi;  /* =max(bnzi) */
344958cb9c82SHong Zhang 
345058cb9c82SHong Zhang     /* if free space is not available, make more free space */
345158cb9c82SHong Zhang     if (current_space->local_remaining<bnzi) {
3452a1a86e44SBarry Smith       ierr = PetscFreeSpaceGet(current_space->total_array_size,&current_space);CHKERRQ(ierr);
345358cb9c82SHong Zhang       nspacedouble++;
345458cb9c82SHong Zhang     }
345558cb9c82SHong Zhang     /* copy data into free space, then initialize lnk */
3456be0fcf8dSHong Zhang     ierr = PetscLLClean(N,N,bnzi,lnk,current_space->array,lnkbt);CHKERRQ(ierr);
3457bcc1bcd5SHong Zhang     ierr = MatPreallocateSet(i+owners[rank],bnzi,current_space->array,dnz,onz);CHKERRQ(ierr);
3458bcc1bcd5SHong Zhang 
345958cb9c82SHong Zhang     current_space->array           += bnzi;
346058cb9c82SHong Zhang     current_space->local_used      += bnzi;
346158cb9c82SHong Zhang     current_space->local_remaining -= bnzi;
346258cb9c82SHong Zhang 
346358cb9c82SHong Zhang     bi[i+1] = bi[i] + bnzi;
346458cb9c82SHong Zhang   }
3465bcc1bcd5SHong Zhang 
3466bcc1bcd5SHong Zhang   ierr = PetscFree(buf_ri_k);CHKERRQ(ierr);
3467bcc1bcd5SHong Zhang 
3468b1d57f15SBarry Smith   ierr = PetscMalloc((bi[m]+1)*sizeof(PetscInt),&bj);CHKERRQ(ierr);
3469a1a86e44SBarry Smith   ierr = PetscFreeSpaceContiguous(&free_space,bj);CHKERRQ(ierr);
3470be0fcf8dSHong Zhang   ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr);
3471409913e3SHong Zhang 
3472bcc1bcd5SHong Zhang   /* create symbolic parallel matrix B_mpi */
3473bcc1bcd5SHong Zhang   /*---------------------------------------*/
3474f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,&B_mpi);CHKERRQ(ierr);
347554b84b50SHong Zhang   if (n==PETSC_DECIDE) {
3476f69a0ea3SMatthew Knepley     ierr = MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,N);CHKERRQ(ierr);
347754b84b50SHong Zhang   } else {
3478f69a0ea3SMatthew Knepley     ierr = MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
347954b84b50SHong Zhang   }
3480bcc1bcd5SHong Zhang   ierr = MatSetType(B_mpi,MATMPIAIJ);CHKERRQ(ierr);
3481bcc1bcd5SHong Zhang   ierr = MatMPIAIJSetPreallocation(B_mpi,0,dnz,0,onz);CHKERRQ(ierr);
3482bcc1bcd5SHong Zhang   ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr);
348358cb9c82SHong Zhang 
34846abd8857SHong Zhang   /* B_mpi is not ready for use - assembly will be done by MatMerge_SeqsToMPINumeric() */
34856abd8857SHong Zhang   B_mpi->assembled     = PETSC_FALSE;
3486affca5deSHong Zhang   B_mpi->ops->destroy  = MatDestroy_MPIAIJ_SeqsToMPI;
3487affca5deSHong Zhang   merge->bi            = bi;
3488affca5deSHong Zhang   merge->bj            = bj;
348902c68681SHong Zhang   merge->buf_ri        = buf_ri;
349002c68681SHong Zhang   merge->buf_rj        = buf_rj;
3491de0260b3SHong Zhang   merge->coi           = PETSC_NULL;
3492de0260b3SHong Zhang   merge->coj           = PETSC_NULL;
3493de0260b3SHong Zhang   merge->owners_co     = PETSC_NULL;
3494affca5deSHong Zhang 
3495affca5deSHong Zhang   /* attach the supporting struct to B_mpi for reuse */
3496affca5deSHong Zhang   ierr = PetscObjectContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr);
3497affca5deSHong Zhang   ierr = PetscObjectContainerSetPointer(container,merge);CHKERRQ(ierr);
3498affca5deSHong Zhang   ierr = PetscObjectCompose((PetscObject)B_mpi,"MatMergeSeqsToMPI",(PetscObject)container);CHKERRQ(ierr);
3499affca5deSHong Zhang   *mpimat = B_mpi;
350038f152feSBarry Smith 
350138f152feSBarry Smith   ierr = PetscCommDestroy(&comm);CHKERRQ(ierr);
35023c2c1871SHong Zhang   ierr = PetscLogEventEnd(logkey_seqstompisym,seqmat,0,0,0);CHKERRQ(ierr);
3503e5f2cdd8SHong Zhang   PetscFunctionReturn(0);
3504e5f2cdd8SHong Zhang }
350525616d81SHong Zhang 
3506e462e02cSHong Zhang static PetscEvent logkey_seqstompi = 0;
350738f152feSBarry Smith #undef __FUNCT__
350838f152feSBarry Smith #define __FUNCT__ "MatMerge_SeqsToMPI"
3509be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMerge_SeqsToMPI(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,MatReuse scall,Mat *mpimat)
351055d1abb9SHong Zhang {
351155d1abb9SHong Zhang   PetscErrorCode   ierr;
351255d1abb9SHong Zhang 
351355d1abb9SHong Zhang   PetscFunctionBegin;
3514e462e02cSHong Zhang   if (!logkey_seqstompi) {
3515e462e02cSHong Zhang     ierr = PetscLogEventRegister(&logkey_seqstompi,"MatMerge_SeqsToMPI",MAT_COOKIE);
3516e462e02cSHong Zhang   }
3517e462e02cSHong Zhang   ierr = PetscLogEventBegin(logkey_seqstompi,seqmat,0,0,0);CHKERRQ(ierr);
351855d1abb9SHong Zhang   if (scall == MAT_INITIAL_MATRIX){
351955d1abb9SHong Zhang     ierr = MatMerge_SeqsToMPISymbolic(comm,seqmat,m,n,mpimat);CHKERRQ(ierr);
352055d1abb9SHong Zhang   }
352155d1abb9SHong Zhang   ierr = MatMerge_SeqsToMPINumeric(seqmat,*mpimat);CHKERRQ(ierr);
3522e462e02cSHong Zhang   ierr = PetscLogEventEnd(logkey_seqstompi,seqmat,0,0,0);CHKERRQ(ierr);
352355d1abb9SHong Zhang   PetscFunctionReturn(0);
352455d1abb9SHong Zhang }
3525a61c8c0fSHong Zhang static PetscEvent logkey_getlocalmat = 0;
352625616d81SHong Zhang #undef __FUNCT__
352725616d81SHong Zhang #define __FUNCT__ "MatGetLocalMat"
352825616d81SHong Zhang /*@C
352932fba14fSHong Zhang      MatGetLocalMat - Creates a SeqAIJ matrix by taking all its local rows
353025616d81SHong Zhang 
353132fba14fSHong Zhang     Not Collective
353225616d81SHong Zhang 
353325616d81SHong Zhang    Input Parameters:
353425616d81SHong Zhang +    A - the matrix
353525616d81SHong Zhang .    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
353625616d81SHong Zhang 
353725616d81SHong Zhang    Output Parameter:
353825616d81SHong Zhang .    A_loc - the local sequential matrix generated
353925616d81SHong Zhang 
354025616d81SHong Zhang     Level: developer
354125616d81SHong Zhang 
354225616d81SHong Zhang @*/
3543be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatGetLocalMat(Mat A,MatReuse scall,Mat *A_loc)
354425616d81SHong Zhang {
354525616d81SHong Zhang   PetscErrorCode  ierr;
354601b7ae99SHong Zhang   Mat_MPIAIJ      *mpimat=(Mat_MPIAIJ*)A->data;
354701b7ae99SHong Zhang   Mat_SeqAIJ      *mat,*a=(Mat_SeqAIJ*)(mpimat->A)->data,*b=(Mat_SeqAIJ*)(mpimat->B)->data;
354801b7ae99SHong Zhang   PetscInt        *ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j,*cmap=mpimat->garray;
3549dea91ad1SHong Zhang   PetscScalar     *aa=a->a,*ba=b->a,*ca;
3550899cda47SBarry Smith   PetscInt        am=A->rmap.n,i,j,k,cstart=A->cmap.rstart;
35515a7d977cSHong Zhang   PetscInt        *ci,*cj,col,ncols_d,ncols_o,jo;
355225616d81SHong Zhang 
355325616d81SHong Zhang   PetscFunctionBegin;
3554e462e02cSHong Zhang   if (!logkey_getlocalmat) {
3555e462e02cSHong Zhang     ierr = PetscLogEventRegister(&logkey_getlocalmat,"MatGetLocalMat",MAT_COOKIE);
3556e462e02cSHong Zhang   }
3557e462e02cSHong Zhang   ierr = PetscLogEventBegin(logkey_getlocalmat,A,0,0,0);CHKERRQ(ierr);
355801b7ae99SHong Zhang   if (scall == MAT_INITIAL_MATRIX){
3559dea91ad1SHong Zhang     ierr = PetscMalloc((1+am)*sizeof(PetscInt),&ci);CHKERRQ(ierr);
3560dea91ad1SHong Zhang     ci[0] = 0;
356101b7ae99SHong Zhang     for (i=0; i<am; i++){
3562dea91ad1SHong Zhang       ci[i+1] = ci[i] + (ai[i+1] - ai[i]) + (bi[i+1] - bi[i]);
356301b7ae99SHong Zhang     }
3564dea91ad1SHong Zhang     ierr = PetscMalloc((1+ci[am])*sizeof(PetscInt),&cj);CHKERRQ(ierr);
3565dea91ad1SHong Zhang     ierr = PetscMalloc((1+ci[am])*sizeof(PetscScalar),&ca);CHKERRQ(ierr);
3566dea91ad1SHong Zhang     k = 0;
356701b7ae99SHong Zhang     for (i=0; i<am; i++) {
35685a7d977cSHong Zhang       ncols_o = bi[i+1] - bi[i];
35695a7d977cSHong Zhang       ncols_d = ai[i+1] - ai[i];
357001b7ae99SHong Zhang       /* off-diagonal portion of A */
35715a7d977cSHong Zhang       for (jo=0; jo<ncols_o; jo++) {
35725a7d977cSHong Zhang         col = cmap[*bj];
35735a7d977cSHong Zhang         if (col >= cstart) break;
35745a7d977cSHong Zhang         cj[k]   = col; bj++;
35755a7d977cSHong Zhang         ca[k++] = *ba++;
35765a7d977cSHong Zhang       }
35775a7d977cSHong Zhang       /* diagonal portion of A */
35785a7d977cSHong Zhang       for (j=0; j<ncols_d; j++) {
35795a7d977cSHong Zhang         cj[k]   = cstart + *aj++;
35805a7d977cSHong Zhang         ca[k++] = *aa++;
35815a7d977cSHong Zhang       }
35825a7d977cSHong Zhang       /* off-diagonal portion of A */
35835a7d977cSHong Zhang       for (j=jo; j<ncols_o; j++) {
35845a7d977cSHong Zhang         cj[k]   = cmap[*bj++];
35855a7d977cSHong Zhang         ca[k++] = *ba++;
35865a7d977cSHong Zhang       }
358725616d81SHong Zhang     }
3588dea91ad1SHong Zhang     /* put together the new matrix */
3589899cda47SBarry Smith     ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,am,A->cmap.N,ci,cj,ca,A_loc);CHKERRQ(ierr);
3590dea91ad1SHong Zhang     /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
3591dea91ad1SHong Zhang     /* Since these are PETSc arrays, change flags to free them as necessary. */
3592dea91ad1SHong Zhang     mat          = (Mat_SeqAIJ*)(*A_loc)->data;
3593e6b907acSBarry Smith     mat->free_a  = PETSC_TRUE;
3594e6b907acSBarry Smith     mat->free_ij = PETSC_TRUE;
3595dea91ad1SHong Zhang     mat->nonew   = 0;
35965a7d977cSHong Zhang   } else if (scall == MAT_REUSE_MATRIX){
35975a7d977cSHong Zhang     mat=(Mat_SeqAIJ*)(*A_loc)->data;
35985a7d977cSHong Zhang     ci = mat->i; cj = mat->j; ca = mat->a;
35995a7d977cSHong Zhang     for (i=0; i<am; i++) {
36005a7d977cSHong Zhang       /* off-diagonal portion of A */
36015a7d977cSHong Zhang       ncols_o = bi[i+1] - bi[i];
36025a7d977cSHong Zhang       for (jo=0; jo<ncols_o; jo++) {
36035a7d977cSHong Zhang         col = cmap[*bj];
36045a7d977cSHong Zhang         if (col >= cstart) break;
3605f33d1a9aSHong Zhang         *ca++ = *ba++; bj++;
36065a7d977cSHong Zhang       }
36075a7d977cSHong Zhang       /* diagonal portion of A */
3608ecc9b87dSHong Zhang       ncols_d = ai[i+1] - ai[i];
3609ecc9b87dSHong Zhang       for (j=0; j<ncols_d; j++) *ca++ = *aa++;
36105a7d977cSHong Zhang       /* off-diagonal portion of A */
3611f33d1a9aSHong Zhang       for (j=jo; j<ncols_o; j++) {
3612f33d1a9aSHong Zhang         *ca++ = *ba++; bj++;
3613f33d1a9aSHong Zhang       }
36145a7d977cSHong Zhang     }
36155a7d977cSHong Zhang   } else {
36165a7d977cSHong Zhang     SETERRQ1(PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall);
361725616d81SHong Zhang   }
361801b7ae99SHong Zhang 
3619e462e02cSHong Zhang   ierr = PetscLogEventEnd(logkey_getlocalmat,A,0,0,0);CHKERRQ(ierr);
362025616d81SHong Zhang   PetscFunctionReturn(0);
362125616d81SHong Zhang }
362225616d81SHong Zhang 
362332fba14fSHong Zhang static PetscEvent logkey_getlocalmatcondensed = 0;
362432fba14fSHong Zhang #undef __FUNCT__
362532fba14fSHong Zhang #define __FUNCT__ "MatGetLocalMatCondensed"
362632fba14fSHong Zhang /*@C
362732fba14fSHong Zhang      MatGetLocalMatCondensed - Creates a SeqAIJ matrix by taking all its local rows and NON-ZERO columns
362832fba14fSHong Zhang 
362932fba14fSHong Zhang     Not Collective
363032fba14fSHong Zhang 
363132fba14fSHong Zhang    Input Parameters:
363232fba14fSHong Zhang +    A - the matrix
363332fba14fSHong Zhang .    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
363432fba14fSHong Zhang -    row, col - index sets of rows and columns to extract (or PETSC_NULL)
363532fba14fSHong Zhang 
363632fba14fSHong Zhang    Output Parameter:
363732fba14fSHong Zhang .    A_loc - the local sequential matrix generated
363832fba14fSHong Zhang 
363932fba14fSHong Zhang     Level: developer
364032fba14fSHong Zhang 
364132fba14fSHong Zhang @*/
3642be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatGetLocalMatCondensed(Mat A,MatReuse scall,IS *row,IS *col,Mat *A_loc)
364332fba14fSHong Zhang {
364432fba14fSHong Zhang   Mat_MPIAIJ        *a=(Mat_MPIAIJ*)A->data;
364532fba14fSHong Zhang   PetscErrorCode    ierr;
364632fba14fSHong Zhang   PetscInt          i,start,end,ncols,nzA,nzB,*cmap,imark,*idx;
364732fba14fSHong Zhang   IS                isrowa,iscola;
364832fba14fSHong Zhang   Mat               *aloc;
364932fba14fSHong Zhang 
365032fba14fSHong Zhang   PetscFunctionBegin;
365132fba14fSHong Zhang   if (!logkey_getlocalmatcondensed) {
365232fba14fSHong Zhang     ierr = PetscLogEventRegister(&logkey_getlocalmatcondensed,"MatGetLocalMatCondensed",MAT_COOKIE);
365332fba14fSHong Zhang   }
365432fba14fSHong Zhang   ierr = PetscLogEventBegin(logkey_getlocalmatcondensed,A,0,0,0);CHKERRQ(ierr);
365532fba14fSHong Zhang   if (!row){
3656899cda47SBarry Smith     start = A->rmap.rstart; end = A->rmap.rend;
365732fba14fSHong Zhang     ierr = ISCreateStride(PETSC_COMM_SELF,end-start,start,1,&isrowa);CHKERRQ(ierr);
365832fba14fSHong Zhang   } else {
365932fba14fSHong Zhang     isrowa = *row;
366032fba14fSHong Zhang   }
366132fba14fSHong Zhang   if (!col){
3662899cda47SBarry Smith     start = A->cmap.rstart;
366332fba14fSHong Zhang     cmap  = a->garray;
3664899cda47SBarry Smith     nzA   = a->A->cmap.n;
3665899cda47SBarry Smith     nzB   = a->B->cmap.n;
366632fba14fSHong Zhang     ierr  = PetscMalloc((nzA+nzB)*sizeof(PetscInt), &idx);CHKERRQ(ierr);
366732fba14fSHong Zhang     ncols = 0;
366832fba14fSHong Zhang     for (i=0; i<nzB; i++) {
366932fba14fSHong Zhang       if (cmap[i] < start) idx[ncols++] = cmap[i];
367032fba14fSHong Zhang       else break;
367132fba14fSHong Zhang     }
367232fba14fSHong Zhang     imark = i;
367332fba14fSHong Zhang     for (i=0; i<nzA; i++) idx[ncols++] = start + i;
367432fba14fSHong Zhang     for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i];
367532fba14fSHong Zhang     ierr = ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,&iscola);CHKERRQ(ierr);
367632fba14fSHong Zhang     ierr = PetscFree(idx);CHKERRQ(ierr);
367732fba14fSHong Zhang   } else {
367832fba14fSHong Zhang     iscola = *col;
367932fba14fSHong Zhang   }
368032fba14fSHong Zhang   if (scall != MAT_INITIAL_MATRIX){
368132fba14fSHong Zhang     ierr = PetscMalloc(sizeof(Mat),&aloc);CHKERRQ(ierr);
368232fba14fSHong Zhang     aloc[0] = *A_loc;
368332fba14fSHong Zhang   }
368432fba14fSHong Zhang   ierr = MatGetSubMatrices(A,1,&isrowa,&iscola,scall,&aloc);CHKERRQ(ierr);
368532fba14fSHong Zhang   *A_loc = aloc[0];
368632fba14fSHong Zhang   ierr = PetscFree(aloc);CHKERRQ(ierr);
368732fba14fSHong Zhang   if (!row){
368832fba14fSHong Zhang     ierr = ISDestroy(isrowa);CHKERRQ(ierr);
368932fba14fSHong Zhang   }
369032fba14fSHong Zhang   if (!col){
369132fba14fSHong Zhang     ierr = ISDestroy(iscola);CHKERRQ(ierr);
369232fba14fSHong Zhang   }
369332fba14fSHong Zhang   ierr = PetscLogEventEnd(logkey_getlocalmatcondensed,A,0,0,0);CHKERRQ(ierr);
369432fba14fSHong Zhang   PetscFunctionReturn(0);
369532fba14fSHong Zhang }
369632fba14fSHong Zhang 
3697a61c8c0fSHong Zhang static PetscEvent logkey_GetBrowsOfAcols = 0;
369825616d81SHong Zhang #undef __FUNCT__
369925616d81SHong Zhang #define __FUNCT__ "MatGetBrowsOfAcols"
370025616d81SHong Zhang /*@C
370132fba14fSHong Zhang     MatGetBrowsOfAcols - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns of local A
370225616d81SHong Zhang 
370325616d81SHong Zhang     Collective on Mat
370425616d81SHong Zhang 
370525616d81SHong Zhang    Input Parameters:
3706e240928fSHong Zhang +    A,B - the matrices in mpiaij format
370725616d81SHong Zhang .    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
370825616d81SHong Zhang -    rowb, colb - index sets of rows and columns of B to extract (or PETSC_NULL)
370925616d81SHong Zhang 
371025616d81SHong Zhang    Output Parameter:
371125616d81SHong Zhang +    rowb, colb - index sets of rows and columns of B to extract
3712899cda47SBarry Smith .    brstart - row index of B_seq from which next B->rmap.n rows are taken from B's local rows
371325616d81SHong Zhang -    B_seq - the sequential matrix generated
371425616d81SHong Zhang 
371525616d81SHong Zhang     Level: developer
371625616d81SHong Zhang 
371725616d81SHong Zhang @*/
3718be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatGetBrowsOfAcols(Mat A,Mat B,MatReuse scall,IS *rowb,IS *colb,PetscInt *brstart,Mat *B_seq)
371925616d81SHong Zhang {
3720899cda47SBarry Smith   Mat_MPIAIJ        *a=(Mat_MPIAIJ*)A->data;
372125616d81SHong Zhang   PetscErrorCode    ierr;
3722b1d57f15SBarry Smith   PetscInt          *idx,i,start,ncols,nzA,nzB,*cmap,imark;
372325616d81SHong Zhang   IS                isrowb,iscolb;
372425616d81SHong Zhang   Mat               *bseq;
372525616d81SHong Zhang 
372625616d81SHong Zhang   PetscFunctionBegin;
3727899cda47SBarry Smith   if (A->cmap.rstart != B->rmap.rstart || A->cmap.rend != B->rmap.rend){
3728899cda47SBarry Smith     SETERRQ4(PETSC_ERR_ARG_SIZ,"Matrix local dimensions are incompatible, (%D, %D) != (%D,%D)",A->cmap.rstart,A->cmap.rend,B->rmap.rstart,B->rmap.rend);
372925616d81SHong Zhang   }
3730e462e02cSHong Zhang   if (!logkey_GetBrowsOfAcols) {
3731e462e02cSHong Zhang     ierr = PetscLogEventRegister(&logkey_GetBrowsOfAcols,"MatGetBrowsOfAcols",MAT_COOKIE);
3732e462e02cSHong Zhang   }
3733e462e02cSHong Zhang   ierr = PetscLogEventBegin(logkey_GetBrowsOfAcols,A,B,0,0);CHKERRQ(ierr);
373425616d81SHong Zhang 
373525616d81SHong Zhang   if (scall == MAT_INITIAL_MATRIX){
3736899cda47SBarry Smith     start = A->cmap.rstart;
373725616d81SHong Zhang     cmap  = a->garray;
3738899cda47SBarry Smith     nzA   = a->A->cmap.n;
3739899cda47SBarry Smith     nzB   = a->B->cmap.n;
3740b1d57f15SBarry Smith     ierr  = PetscMalloc((nzA+nzB)*sizeof(PetscInt), &idx);CHKERRQ(ierr);
374125616d81SHong Zhang     ncols = 0;
37420390132cSHong Zhang     for (i=0; i<nzB; i++) {  /* row < local row index */
374325616d81SHong Zhang       if (cmap[i] < start) idx[ncols++] = cmap[i];
374425616d81SHong Zhang       else break;
374525616d81SHong Zhang     }
374625616d81SHong Zhang     imark = i;
37470390132cSHong Zhang     for (i=0; i<nzA; i++) idx[ncols++] = start + i;  /* local rows */
37480390132cSHong Zhang     for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i]; /* row > local row index */
374925616d81SHong Zhang     ierr = ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,&isrowb);CHKERRQ(ierr);
375025616d81SHong Zhang     ierr = PetscFree(idx);CHKERRQ(ierr);
375125616d81SHong Zhang     *brstart = imark;
3752899cda47SBarry Smith     ierr = ISCreateStride(PETSC_COMM_SELF,B->cmap.N,0,1,&iscolb);CHKERRQ(ierr);
375325616d81SHong Zhang   } else {
375425616d81SHong Zhang     if (!rowb || !colb) SETERRQ(PETSC_ERR_SUP,"IS rowb and colb must be provided for MAT_REUSE_MATRIX");
375525616d81SHong Zhang     isrowb = *rowb; iscolb = *colb;
375625616d81SHong Zhang     ierr = PetscMalloc(sizeof(Mat),&bseq);CHKERRQ(ierr);
375725616d81SHong Zhang     bseq[0] = *B_seq;
375825616d81SHong Zhang   }
375925616d81SHong Zhang   ierr = MatGetSubMatrices(B,1,&isrowb,&iscolb,scall,&bseq);CHKERRQ(ierr);
376025616d81SHong Zhang   *B_seq = bseq[0];
376125616d81SHong Zhang   ierr = PetscFree(bseq);CHKERRQ(ierr);
376225616d81SHong Zhang   if (!rowb){
376325616d81SHong Zhang     ierr = ISDestroy(isrowb);CHKERRQ(ierr);
376425616d81SHong Zhang   } else {
376525616d81SHong Zhang     *rowb = isrowb;
376625616d81SHong Zhang   }
376725616d81SHong Zhang   if (!colb){
376825616d81SHong Zhang     ierr = ISDestroy(iscolb);CHKERRQ(ierr);
376925616d81SHong Zhang   } else {
377025616d81SHong Zhang     *colb = iscolb;
377125616d81SHong Zhang   }
3772e462e02cSHong Zhang   ierr = PetscLogEventEnd(logkey_GetBrowsOfAcols,A,B,0,0);CHKERRQ(ierr);
377325616d81SHong Zhang   PetscFunctionReturn(0);
377425616d81SHong Zhang }
3775429d309bSHong Zhang 
3776a61c8c0fSHong Zhang static PetscEvent logkey_GetBrowsOfAocols = 0;
3777a61c8c0fSHong Zhang #undef __FUNCT__
3778a61c8c0fSHong Zhang #define __FUNCT__ "MatGetBrowsOfAoCols"
3779429d309bSHong Zhang /*@C
3780429d309bSHong Zhang     MatGetBrowsOfAoCols - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns
378101b7ae99SHong Zhang     of the OFF-DIAGONAL portion of local A
3782429d309bSHong Zhang 
3783429d309bSHong Zhang     Collective on Mat
3784429d309bSHong Zhang 
3785429d309bSHong Zhang    Input Parameters:
3786429d309bSHong Zhang +    A,B - the matrices in mpiaij format
378787025532SHong Zhang .    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
378887025532SHong Zhang .    startsj - starting point in B's sending and receiving j-arrays, saved for MAT_REUSE (or PETSC_NULL)
378987025532SHong Zhang -    bufa_ptr - array for sending matrix values, saved for MAT_REUSE (or PETSC_NULL)
3790429d309bSHong Zhang 
3791429d309bSHong Zhang    Output Parameter:
379287025532SHong Zhang +    B_oth - the sequential matrix generated
3793429d309bSHong Zhang 
3794429d309bSHong Zhang     Level: developer
3795429d309bSHong Zhang 
3796429d309bSHong Zhang @*/
3797be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatGetBrowsOfAoCols(Mat A,Mat B,MatReuse scall,PetscInt **startsj,PetscScalar **bufa_ptr,Mat *B_oth)
3798429d309bSHong Zhang {
3799a6b2eed2SHong Zhang   VecScatter_MPI_General *gen_to,*gen_from;
3800429d309bSHong Zhang   PetscErrorCode         ierr;
3801899cda47SBarry Smith   Mat_MPIAIJ             *a=(Mat_MPIAIJ*)A->data;
380287025532SHong Zhang   Mat_SeqAIJ             *b_oth;
3803a6b2eed2SHong Zhang   VecScatter             ctx=a->Mvctx;
3804a6b2eed2SHong Zhang   MPI_Comm               comm=ctx->comm;
380587025532SHong Zhang   PetscMPIInt            *rprocs,*sprocs,tag=ctx->tag,rank;
3806899cda47SBarry Smith   PetscInt               *rowlen,*bufj,*bufJ,ncols,aBn=a->B->cmap.n,row,*b_othi,*b_othj;
380787025532SHong Zhang   PetscScalar            *rvalues,*svalues,*b_otha,*bufa,*bufA;
3808d679ab29SBarry Smith   PetscInt               i,k,l,nrecvs,nsends,nrows,*srow,*rstarts,*rstartsj = 0,*sstarts,*sstartsj,len;
3809910ba992SMatthew Knepley   MPI_Request            *rwaits = PETSC_NULL,*swaits = PETSC_NULL;
381087025532SHong Zhang   MPI_Status             *sstatus,rstatus;
3811ba8c8a56SBarry Smith   PetscInt               *cols;
3812ba8c8a56SBarry Smith   PetscScalar            *vals;
381313f74950SBarry Smith   PetscMPIInt            j;
3814429d309bSHong Zhang 
3815429d309bSHong Zhang   PetscFunctionBegin;
3816899cda47SBarry Smith   if (A->cmap.rstart != B->rmap.rstart || A->cmap.rend != B->rmap.rend){
3817899cda47SBarry Smith     SETERRQ4(PETSC_ERR_ARG_SIZ,"Matrix local dimensions are incompatible, (%d, %d) != (%d,%d)",A->cmap.rstart,A->cmap.rend,B->rmap.rstart,B->rmap.rend);
3818429d309bSHong Zhang   }
3819429d309bSHong Zhang   if (!logkey_GetBrowsOfAocols) {
38201677a5d7SHong Zhang     ierr = PetscLogEventRegister(&logkey_GetBrowsOfAocols,"MatGetBrAoCol",MAT_COOKIE);
3821429d309bSHong Zhang   }
3822429d309bSHong Zhang   ierr = PetscLogEventBegin(logkey_GetBrowsOfAocols,A,B,0,0);CHKERRQ(ierr);
3823a6b2eed2SHong Zhang   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
3824a6b2eed2SHong Zhang 
3825a6b2eed2SHong Zhang   gen_to   = (VecScatter_MPI_General*)ctx->todata;
3826a6b2eed2SHong Zhang   gen_from = (VecScatter_MPI_General*)ctx->fromdata;
3827a6b2eed2SHong Zhang   rvalues  = gen_from->values; /* holds the length of sending row */
3828a6b2eed2SHong Zhang   svalues  = gen_to->values;   /* holds the length of receiving row */
3829a6b2eed2SHong Zhang   nrecvs   = gen_from->n;
3830a6b2eed2SHong Zhang   nsends   = gen_to->n;
3831d7ee0231SBarry Smith 
3832d7ee0231SBarry Smith   ierr = PetscMalloc2(nrecvs,MPI_Request,&rwaits,nsends,MPI_Request,&swaits);CHKERRQ(ierr);
3833a6b2eed2SHong Zhang   srow     = gen_to->indices;   /* local row index to be sent */
3834a6b2eed2SHong Zhang   rstarts  = gen_from->starts;
3835a6b2eed2SHong Zhang   sstarts  = gen_to->starts;
3836a6b2eed2SHong Zhang   rprocs   = gen_from->procs;
3837a6b2eed2SHong Zhang   sprocs   = gen_to->procs;
3838a6b2eed2SHong Zhang   sstatus  = gen_to->sstatus;
3839429d309bSHong Zhang 
3840dea91ad1SHong Zhang   if (!startsj || !bufa_ptr) scall = MAT_INITIAL_MATRIX;
3841429d309bSHong Zhang   if (scall == MAT_INITIAL_MATRIX){
3842a6b2eed2SHong Zhang     /* i-array */
3843a6b2eed2SHong Zhang     /*---------*/
3844a6b2eed2SHong Zhang     /*  post receives */
3845a6b2eed2SHong Zhang     for (i=0; i<nrecvs; i++){
3846a6b2eed2SHong Zhang       rowlen = (PetscInt*)rvalues + rstarts[i];
384787025532SHong Zhang       nrows = rstarts[i+1]-rstarts[i];
384887025532SHong Zhang       ierr = MPI_Irecv(rowlen,nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr);
3849429d309bSHong Zhang     }
3850a6b2eed2SHong Zhang 
3851a6b2eed2SHong Zhang     /* pack the outgoing message */
385287025532SHong Zhang     ierr = PetscMalloc((nsends+nrecvs+3)*sizeof(PetscInt),&sstartsj);CHKERRQ(ierr);
3853a6b2eed2SHong Zhang     rstartsj = sstartsj + nsends +1;
3854a6b2eed2SHong Zhang     sstartsj[0] = 0;  rstartsj[0] = 0;
3855a6b2eed2SHong Zhang     len = 0; /* total length of j or a array to be sent */
3856a6b2eed2SHong Zhang     k = 0;
3857a6b2eed2SHong Zhang     for (i=0; i<nsends; i++){
3858a6b2eed2SHong Zhang       rowlen = (PetscInt*)svalues + sstarts[i];
385987025532SHong Zhang       nrows = sstarts[i+1]-sstarts[i]; /* num of rows */
386087025532SHong Zhang       for (j=0; j<nrows; j++) {
3861899cda47SBarry Smith         row = srow[k] + B->rmap.range[rank]; /* global row idx */
3862ba8c8a56SBarry Smith         ierr = MatGetRow_MPIAIJ(B,row,&rowlen[j],PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); /* rowlength */
3863a6b2eed2SHong Zhang         len += rowlen[j];
3864ba8c8a56SBarry Smith         ierr = MatRestoreRow_MPIAIJ(B,row,&ncols,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
3865a6b2eed2SHong Zhang         k++;
3866429d309bSHong Zhang       }
386787025532SHong Zhang       ierr = MPI_Isend(rowlen,nrows,MPIU_INT,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr);
3868dea91ad1SHong Zhang        sstartsj[i+1] = len;  /* starting point of (i+1)-th outgoing msg in bufj and bufa */
3869429d309bSHong Zhang     }
387087025532SHong Zhang     /* recvs and sends of i-array are completed */
387187025532SHong Zhang     i = nrecvs;
387287025532SHong Zhang     while (i--) {
387387025532SHong Zhang       ierr = MPI_Waitany(nrecvs,rwaits,&j,&rstatus);CHKERRQ(ierr);
387487025532SHong Zhang     }
38750c468ba9SBarry Smith     if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);}
3876a6b2eed2SHong Zhang     /* allocate buffers for sending j and a arrays */
3877a6b2eed2SHong Zhang     ierr = PetscMalloc((len+1)*sizeof(PetscInt),&bufj);CHKERRQ(ierr);
3878a6b2eed2SHong Zhang     ierr = PetscMalloc((len+1)*sizeof(PetscScalar),&bufa);CHKERRQ(ierr);
3879a6b2eed2SHong Zhang 
388087025532SHong Zhang     /* create i-array of B_oth */
388187025532SHong Zhang     ierr = PetscMalloc((aBn+2)*sizeof(PetscInt),&b_othi);CHKERRQ(ierr);
388287025532SHong Zhang     b_othi[0] = 0;
3883a6b2eed2SHong Zhang     len = 0; /* total length of j or a array to be received */
3884a6b2eed2SHong Zhang     k = 0;
3885a6b2eed2SHong Zhang     for (i=0; i<nrecvs; i++){
3886a6b2eed2SHong Zhang       rowlen = (PetscInt*)rvalues + rstarts[i];
388787025532SHong Zhang       nrows = rstarts[i+1]-rstarts[i];
388887025532SHong Zhang       for (j=0; j<nrows; j++) {
388987025532SHong Zhang         b_othi[k+1] = b_othi[k] + rowlen[j];
3890a6b2eed2SHong Zhang         len += rowlen[j]; k++;
3891a6b2eed2SHong Zhang       }
3892dea91ad1SHong Zhang       rstartsj[i+1] = len; /* starting point of (i+1)-th incoming msg in bufj and bufa */
3893a6b2eed2SHong Zhang     }
3894a6b2eed2SHong Zhang 
389587025532SHong Zhang     /* allocate space for j and a arrrays of B_oth */
389687025532SHong Zhang     ierr = PetscMalloc((b_othi[aBn]+1)*sizeof(PetscInt),&b_othj);CHKERRQ(ierr);
389787025532SHong Zhang     ierr = PetscMalloc((b_othi[aBn]+1)*sizeof(PetscScalar),&b_otha);CHKERRQ(ierr);
3898a6b2eed2SHong Zhang 
389987025532SHong Zhang     /* j-array */
390087025532SHong Zhang     /*---------*/
3901a6b2eed2SHong Zhang     /*  post receives of j-array */
3902a6b2eed2SHong Zhang     for (i=0; i<nrecvs; i++){
390387025532SHong Zhang       nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */
390487025532SHong Zhang       ierr = MPI_Irecv(b_othj+rstartsj[i],nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr);
3905a6b2eed2SHong Zhang     }
3906a6b2eed2SHong Zhang     k = 0;
3907a6b2eed2SHong Zhang     for (i=0; i<nsends; i++){
390887025532SHong Zhang       nrows = sstarts[i+1]-sstarts[i]; /* num of rows */
3909a6b2eed2SHong Zhang       bufJ = bufj+sstartsj[i];
391087025532SHong Zhang       for (j=0; j<nrows; j++) {
3911899cda47SBarry Smith         row  = srow[k++] + B->rmap.range[rank]; /* global row idx */
3912ba8c8a56SBarry Smith         ierr = MatGetRow_MPIAIJ(B,row,&ncols,&cols,PETSC_NULL);CHKERRQ(ierr);
3913a6b2eed2SHong Zhang         for (l=0; l<ncols; l++){
3914a6b2eed2SHong Zhang           *bufJ++ = cols[l];
391587025532SHong Zhang         }
3916ba8c8a56SBarry Smith         ierr = MatRestoreRow_MPIAIJ(B,row,&ncols,&cols,PETSC_NULL);CHKERRQ(ierr);
391787025532SHong Zhang       }
391887025532SHong Zhang       ierr = MPI_Isend(bufj+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_INT,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr);
391987025532SHong Zhang     }
392087025532SHong Zhang 
392187025532SHong Zhang     /* recvs and sends of j-array are completed */
392287025532SHong Zhang     i = nrecvs;
392387025532SHong Zhang     while (i--) {
392487025532SHong Zhang       ierr = MPI_Waitany(nrecvs,rwaits,&j,&rstatus);CHKERRQ(ierr);
392587025532SHong Zhang     }
39260c468ba9SBarry Smith     if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);}
392787025532SHong Zhang   } else if (scall == MAT_REUSE_MATRIX){
392887025532SHong Zhang     sstartsj = *startsj;
392987025532SHong Zhang     rstartsj = sstartsj + nsends +1;
393087025532SHong Zhang     bufa     = *bufa_ptr;
393187025532SHong Zhang     b_oth    = (Mat_SeqAIJ*)(*B_oth)->data;
393287025532SHong Zhang     b_otha   = b_oth->a;
393387025532SHong Zhang   } else {
393487025532SHong Zhang     SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Matrix P does not posses an object container");
393587025532SHong Zhang   }
393687025532SHong Zhang 
393787025532SHong Zhang   /* a-array */
393887025532SHong Zhang   /*---------*/
393987025532SHong Zhang   /*  post receives of a-array */
394087025532SHong Zhang   for (i=0; i<nrecvs; i++){
394187025532SHong Zhang     nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */
394287025532SHong Zhang     ierr = MPI_Irecv(b_otha+rstartsj[i],nrows,MPIU_SCALAR,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr);
394387025532SHong Zhang   }
394487025532SHong Zhang   k = 0;
394587025532SHong Zhang   for (i=0; i<nsends; i++){
394687025532SHong Zhang     nrows = sstarts[i+1]-sstarts[i];
394787025532SHong Zhang     bufA = bufa+sstartsj[i];
394887025532SHong Zhang     for (j=0; j<nrows; j++) {
3949899cda47SBarry Smith       row  = srow[k++] + B->rmap.range[rank]; /* global row idx */
3950ba8c8a56SBarry Smith       ierr = MatGetRow_MPIAIJ(B,row,&ncols,PETSC_NULL,&vals);CHKERRQ(ierr);
395187025532SHong Zhang       for (l=0; l<ncols; l++){
3952a6b2eed2SHong Zhang         *bufA++ = vals[l];
3953a6b2eed2SHong Zhang       }
3954ba8c8a56SBarry Smith       ierr = MatRestoreRow_MPIAIJ(B,row,&ncols,PETSC_NULL,&vals);CHKERRQ(ierr);
395587025532SHong Zhang 
3956a6b2eed2SHong Zhang     }
395787025532SHong Zhang     ierr = MPI_Isend(bufa+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_SCALAR,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr);
3958a6b2eed2SHong Zhang   }
395987025532SHong Zhang   /* recvs and sends of a-array are completed */
396087025532SHong Zhang   i = nrecvs;
396187025532SHong Zhang   while (i--) {
396287025532SHong Zhang     ierr = MPI_Waitany(nrecvs,rwaits,&j,&rstatus);CHKERRQ(ierr);
396387025532SHong Zhang   }
39640c468ba9SBarry Smith   if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);}
3965d7ee0231SBarry Smith   ierr = PetscFree2(rwaits,swaits);CHKERRQ(ierr);
3966a6b2eed2SHong Zhang 
396787025532SHong Zhang   if (scall == MAT_INITIAL_MATRIX){
3968a6b2eed2SHong Zhang     /* put together the new matrix */
3969899cda47SBarry Smith     ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,aBn,B->cmap.N,b_othi,b_othj,b_otha,B_oth);CHKERRQ(ierr);
3970a6b2eed2SHong Zhang 
3971a6b2eed2SHong Zhang     /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
3972a6b2eed2SHong Zhang     /* Since these are PETSc arrays, change flags to free them as necessary. */
397387025532SHong Zhang     b_oth          = (Mat_SeqAIJ *)(*B_oth)->data;
3974e6b907acSBarry Smith     b_oth->free_a  = PETSC_TRUE;
3975e6b907acSBarry Smith     b_oth->free_ij = PETSC_TRUE;
397687025532SHong Zhang     b_oth->nonew   = 0;
3977a6b2eed2SHong Zhang 
3978a6b2eed2SHong Zhang     ierr = PetscFree(bufj);CHKERRQ(ierr);
3979dea91ad1SHong Zhang     if (!startsj || !bufa_ptr){
3980dea91ad1SHong Zhang       ierr = PetscFree(sstartsj);CHKERRQ(ierr);
3981dea91ad1SHong Zhang       ierr = PetscFree(bufa_ptr);CHKERRQ(ierr);
3982dea91ad1SHong Zhang     } else {
398387025532SHong Zhang       *startsj  = sstartsj;
398487025532SHong Zhang       *bufa_ptr = bufa;
398587025532SHong Zhang     }
3986dea91ad1SHong Zhang   }
3987429d309bSHong Zhang   ierr = PetscLogEventEnd(logkey_GetBrowsOfAocols,A,B,0,0);CHKERRQ(ierr);
3988a6b2eed2SHong Zhang 
3989429d309bSHong Zhang   PetscFunctionReturn(0);
3990429d309bSHong Zhang }
3991ccd8e176SBarry Smith 
399243eb5e2fSMatthew Knepley #undef __FUNCT__
399343eb5e2fSMatthew Knepley #define __FUNCT__ "MatGetCommunicationStructs"
399443eb5e2fSMatthew Knepley /*@C
399543eb5e2fSMatthew Knepley   MatGetCommunicationStructs - Provides access to the communication structures used in matrix-vector multiplication.
399643eb5e2fSMatthew Knepley 
399743eb5e2fSMatthew Knepley   Not Collective
399843eb5e2fSMatthew Knepley 
399943eb5e2fSMatthew Knepley   Input Parameters:
400043eb5e2fSMatthew Knepley . A - The matrix in mpiaij format
400143eb5e2fSMatthew Knepley 
400243eb5e2fSMatthew Knepley   Output Parameter:
400343eb5e2fSMatthew Knepley + lvec - The local vector holding off-process values from the argument to a matrix-vector product
400443eb5e2fSMatthew Knepley . colmap - A map from global column index to local index into lvec
400543eb5e2fSMatthew Knepley - multScatter - A scatter from the argument of a matrix-vector product to lvec
400643eb5e2fSMatthew Knepley 
400743eb5e2fSMatthew Knepley   Level: developer
400843eb5e2fSMatthew Knepley 
400943eb5e2fSMatthew Knepley @*/
401043eb5e2fSMatthew Knepley #if defined (PETSC_USE_CTABLE)
401143eb5e2fSMatthew Knepley PetscErrorCode PETSCMAT_DLLEXPORT MatGetCommunicationStructs(Mat A, Vec *lvec, PetscTable *colmap, VecScatter *multScatter)
401243eb5e2fSMatthew Knepley #else
401343eb5e2fSMatthew Knepley PetscErrorCode PETSCMAT_DLLEXPORT MatGetCommunicationStructs(Mat A, Vec *lvec, PetscInt *colmap[], VecScatter *multScatter)
401443eb5e2fSMatthew Knepley #endif
401543eb5e2fSMatthew Knepley {
401643eb5e2fSMatthew Knepley   Mat_MPIAIJ *a;
401743eb5e2fSMatthew Knepley 
401843eb5e2fSMatthew Knepley   PetscFunctionBegin;
401943eb5e2fSMatthew Knepley   PetscValidHeaderSpecific(A, MAT_COOKIE, 1);
402043eb5e2fSMatthew Knepley   PetscValidPointer(lvec, 2)
402143eb5e2fSMatthew Knepley   PetscValidPointer(colmap, 3)
402243eb5e2fSMatthew Knepley   PetscValidPointer(multScatter, 4)
402343eb5e2fSMatthew Knepley   a = (Mat_MPIAIJ *) A->data;
402443eb5e2fSMatthew Knepley   if (lvec) *lvec = a->lvec;
402543eb5e2fSMatthew Knepley   if (colmap) *colmap = a->colmap;
402643eb5e2fSMatthew Knepley   if (multScatter) *multScatter = a->Mvctx;
402743eb5e2fSMatthew Knepley   PetscFunctionReturn(0);
402843eb5e2fSMatthew Knepley }
402943eb5e2fSMatthew Knepley 
403017667f90SBarry Smith EXTERN_C_BEGIN
403117667f90SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_MPIAIJ_MPICRL(Mat,MatType,MatReuse,Mat*);
403217667f90SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_MPIAIJ_MPICSRPERM(Mat,MatType,MatReuse,Mat*);
403317667f90SBarry Smith EXTERN_C_END
403417667f90SBarry Smith 
4035ccd8e176SBarry Smith /*MC
4036ccd8e176SBarry Smith    MATMPIAIJ - MATMPIAIJ = "mpiaij" - A matrix type to be used for parallel sparse matrices.
4037ccd8e176SBarry Smith 
4038ccd8e176SBarry Smith    Options Database Keys:
4039ccd8e176SBarry Smith . -mat_type mpiaij - sets the matrix type to "mpiaij" during a call to MatSetFromOptions()
4040ccd8e176SBarry Smith 
4041ccd8e176SBarry Smith   Level: beginner
4042ccd8e176SBarry Smith 
4043ccd8e176SBarry Smith .seealso: MatCreateMPIAIJ
4044ccd8e176SBarry Smith M*/
4045ccd8e176SBarry Smith 
4046ccd8e176SBarry Smith EXTERN_C_BEGIN
4047ccd8e176SBarry Smith #undef __FUNCT__
4048ccd8e176SBarry Smith #define __FUNCT__ "MatCreate_MPIAIJ"
4049be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_MPIAIJ(Mat B)
4050ccd8e176SBarry Smith {
4051ccd8e176SBarry Smith   Mat_MPIAIJ     *b;
4052ccd8e176SBarry Smith   PetscErrorCode ierr;
4053ccd8e176SBarry Smith   PetscMPIInt    size;
4054ccd8e176SBarry Smith 
4055ccd8e176SBarry Smith   PetscFunctionBegin;
4056ccd8e176SBarry Smith   ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr);
4057ccd8e176SBarry Smith 
4058ccd8e176SBarry Smith   ierr            = PetscNew(Mat_MPIAIJ,&b);CHKERRQ(ierr);
4059ccd8e176SBarry Smith   B->data         = (void*)b;
4060ccd8e176SBarry Smith   ierr            = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
4061ccd8e176SBarry Smith   B->factor       = 0;
4062899cda47SBarry Smith   B->rmap.bs      = 1;
4063ccd8e176SBarry Smith   B->assembled    = PETSC_FALSE;
4064ccd8e176SBarry Smith   B->mapping      = 0;
4065ccd8e176SBarry Smith 
4066ccd8e176SBarry Smith   B->insertmode      = NOT_SET_VALUES;
4067ccd8e176SBarry Smith   b->size            = size;
4068ccd8e176SBarry Smith   ierr = MPI_Comm_rank(B->comm,&b->rank);CHKERRQ(ierr);
4069ccd8e176SBarry Smith 
4070ccd8e176SBarry Smith   /* build cache for off array entries formed */
4071ccd8e176SBarry Smith   ierr = MatStashCreate_Private(B->comm,1,&B->stash);CHKERRQ(ierr);
4072ccd8e176SBarry Smith   b->donotstash  = PETSC_FALSE;
4073ccd8e176SBarry Smith   b->colmap      = 0;
4074ccd8e176SBarry Smith   b->garray      = 0;
4075ccd8e176SBarry Smith   b->roworiented = PETSC_TRUE;
4076ccd8e176SBarry Smith 
4077ccd8e176SBarry Smith   /* stuff used for matrix vector multiply */
4078ccd8e176SBarry Smith   b->lvec      = PETSC_NULL;
4079ccd8e176SBarry Smith   b->Mvctx     = PETSC_NULL;
4080ccd8e176SBarry Smith 
4081ccd8e176SBarry Smith   /* stuff for MatGetRow() */
4082ccd8e176SBarry Smith   b->rowindices   = 0;
4083ccd8e176SBarry Smith   b->rowvalues    = 0;
4084ccd8e176SBarry Smith   b->getrowactive = PETSC_FALSE;
4085ccd8e176SBarry Smith 
4086ccd8e176SBarry Smith 
4087ccd8e176SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
4088ccd8e176SBarry Smith                                      "MatStoreValues_MPIAIJ",
4089ccd8e176SBarry Smith                                      MatStoreValues_MPIAIJ);CHKERRQ(ierr);
4090ccd8e176SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
4091ccd8e176SBarry Smith                                      "MatRetrieveValues_MPIAIJ",
4092ccd8e176SBarry Smith                                      MatRetrieveValues_MPIAIJ);CHKERRQ(ierr);
4093ccd8e176SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetDiagonalBlock_C",
4094ccd8e176SBarry Smith 				     "MatGetDiagonalBlock_MPIAIJ",
4095ccd8e176SBarry Smith                                      MatGetDiagonalBlock_MPIAIJ);CHKERRQ(ierr);
4096ccd8e176SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C",
4097ccd8e176SBarry Smith 				     "MatIsTranspose_MPIAIJ",
4098ccd8e176SBarry Smith 				     MatIsTranspose_MPIAIJ);CHKERRQ(ierr);
4099ccd8e176SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAIJSetPreallocation_C",
4100ccd8e176SBarry Smith 				     "MatMPIAIJSetPreallocation_MPIAIJ",
4101ccd8e176SBarry Smith 				     MatMPIAIJSetPreallocation_MPIAIJ);CHKERRQ(ierr);
4102ccd8e176SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAIJSetPreallocationCSR_C",
4103ccd8e176SBarry Smith 				     "MatMPIAIJSetPreallocationCSR_MPIAIJ",
4104ccd8e176SBarry Smith 				     MatMPIAIJSetPreallocationCSR_MPIAIJ);CHKERRQ(ierr);
4105ccd8e176SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatDiagonalScaleLocal_C",
4106ccd8e176SBarry Smith 				     "MatDiagonalScaleLocal_MPIAIJ",
4107ccd8e176SBarry Smith 				     MatDiagonalScaleLocal_MPIAIJ);CHKERRQ(ierr);
410817667f90SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_mpicsrperm_C",
410917667f90SBarry Smith                                      "MatConvert_MPIAIJ_MPICSRPERM",
411017667f90SBarry Smith                                       MatConvert_MPIAIJ_MPICSRPERM);CHKERRQ(ierr);
411117667f90SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_mpicrl_C",
411217667f90SBarry Smith                                      "MatConvert_MPIAIJ_MPICRL",
411317667f90SBarry Smith                                       MatConvert_MPIAIJ_MPICRL);CHKERRQ(ierr);
411417667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATMPIAIJ);CHKERRQ(ierr);
4115ccd8e176SBarry Smith   PetscFunctionReturn(0);
4116ccd8e176SBarry Smith }
4117ccd8e176SBarry Smith EXTERN_C_END
411881824310SBarry Smith 
411981824310SBarry Smith /*
412081824310SBarry Smith     Special version for direct calls from Fortran
412181824310SBarry Smith */
412281824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
412381824310SBarry Smith #define matsetvaluesmpiaij_ MATSETVALUESMPIAIJ
412481824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
412581824310SBarry Smith #define matsetvaluesmpiaij_ matsetvaluesmpiaij
412681824310SBarry Smith #endif
412781824310SBarry Smith 
412881824310SBarry Smith /* Change these macros so can be used in void function */
412981824310SBarry Smith #undef CHKERRQ
413081824310SBarry Smith #define CHKERRQ(ierr) CHKERRABORT(mat->comm,ierr)
413181824310SBarry Smith #undef SETERRQ2
413281824310SBarry Smith #define SETERRQ2(ierr,b,c,d) CHKERRABORT(mat->comm,ierr)
413381824310SBarry Smith #undef SETERRQ
413481824310SBarry Smith #define SETERRQ(ierr,b) CHKERRABORT(mat->comm,ierr)
413581824310SBarry Smith 
413681824310SBarry Smith EXTERN_C_BEGIN
413781824310SBarry Smith #undef __FUNCT__
413881824310SBarry Smith #define __FUNCT__ "matsetvaluesmpiaij_"
41391f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesmpiaij_(Mat *mmat,PetscInt *mm,const PetscInt im[],PetscInt *mn,const PetscInt in[],const PetscScalar v[],InsertMode *maddv,PetscErrorCode *_ierr)
414081824310SBarry Smith {
414181824310SBarry Smith   Mat            mat = *mmat;
414281824310SBarry Smith   PetscInt       m = *mm, n = *mn;
414381824310SBarry Smith   InsertMode     addv = *maddv;
414481824310SBarry Smith   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
414581824310SBarry Smith   PetscScalar    value;
414681824310SBarry Smith   PetscErrorCode ierr;
4147899cda47SBarry Smith 
414881824310SBarry Smith   MatPreallocated(mat);
414981824310SBarry Smith   if (mat->insertmode == NOT_SET_VALUES) {
415081824310SBarry Smith     mat->insertmode = addv;
415181824310SBarry Smith   }
415281824310SBarry Smith #if defined(PETSC_USE_DEBUG)
415381824310SBarry Smith   else if (mat->insertmode != addv) {
415481824310SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
415581824310SBarry Smith   }
415681824310SBarry Smith #endif
415781824310SBarry Smith   {
4158899cda47SBarry Smith   PetscInt       i,j,rstart = mat->rmap.rstart,rend = mat->rmap.rend;
4159899cda47SBarry Smith   PetscInt       cstart = mat->cmap.rstart,cend = mat->cmap.rend,row,col;
416081824310SBarry Smith   PetscTruth     roworiented = aij->roworiented;
416181824310SBarry Smith 
416281824310SBarry Smith   /* Some Variables required in the macro */
416381824310SBarry Smith   Mat            A = aij->A;
416481824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
416581824310SBarry Smith   PetscInt       *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j;
416681824310SBarry Smith   PetscScalar    *aa = a->a;
416781824310SBarry Smith   PetscTruth     ignorezeroentries = (((a->ignorezeroentries)&&(addv==ADD_VALUES))?PETSC_TRUE:PETSC_FALSE);
416881824310SBarry Smith   Mat            B = aij->B;
416981824310SBarry Smith   Mat_SeqAIJ     *b = (Mat_SeqAIJ*)B->data;
4170899cda47SBarry Smith   PetscInt       *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap.n,am = aij->A->rmap.n;
417181824310SBarry Smith   PetscScalar    *ba = b->a;
417281824310SBarry Smith 
417381824310SBarry Smith   PetscInt       *rp1,*rp2,ii,nrow1,nrow2,_i,rmax1,rmax2,N,low1,high1,low2,high2,t,lastcol1,lastcol2;
417481824310SBarry Smith   PetscInt       nonew = a->nonew;
417581824310SBarry Smith   PetscScalar    *ap1,*ap2;
417681824310SBarry Smith 
417781824310SBarry Smith   PetscFunctionBegin;
417881824310SBarry Smith   for (i=0; i<m; i++) {
417981824310SBarry Smith     if (im[i] < 0) continue;
418081824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4181899cda47SBarry Smith     if (im[i] >= mat->rmap.N) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",im[i],mat->rmap.N-1);
418281824310SBarry Smith #endif
418381824310SBarry Smith     if (im[i] >= rstart && im[i] < rend) {
418481824310SBarry Smith       row      = im[i] - rstart;
418581824310SBarry Smith       lastcol1 = -1;
418681824310SBarry Smith       rp1      = aj + ai[row];
418781824310SBarry Smith       ap1      = aa + ai[row];
418881824310SBarry Smith       rmax1    = aimax[row];
418981824310SBarry Smith       nrow1    = ailen[row];
419081824310SBarry Smith       low1     = 0;
419181824310SBarry Smith       high1    = nrow1;
419281824310SBarry Smith       lastcol2 = -1;
419381824310SBarry Smith       rp2      = bj + bi[row];
419481824310SBarry Smith       ap2      = ba + bi[row];
419581824310SBarry Smith       rmax2    = bimax[row];
419681824310SBarry Smith       nrow2    = bilen[row];
419781824310SBarry Smith       low2     = 0;
419881824310SBarry Smith       high2    = nrow2;
419981824310SBarry Smith 
420081824310SBarry Smith       for (j=0; j<n; j++) {
420181824310SBarry Smith         if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
420281824310SBarry Smith         if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES)) continue;
420381824310SBarry Smith         if (in[j] >= cstart && in[j] < cend){
420481824310SBarry Smith           col = in[j] - cstart;
420581824310SBarry Smith           MatSetValues_SeqAIJ_A_Private(row,col,value,addv);
420681824310SBarry Smith         } else if (in[j] < 0) continue;
420781824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4208899cda47SBarry Smith         else if (in[j] >= mat->cmap.N) {SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[j],mat->cmap.N-1);}
420981824310SBarry Smith #endif
421081824310SBarry Smith         else {
421181824310SBarry Smith           if (mat->was_assembled) {
421281824310SBarry Smith             if (!aij->colmap) {
421381824310SBarry Smith               ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
421481824310SBarry Smith             }
421581824310SBarry Smith #if defined (PETSC_USE_CTABLE)
421681824310SBarry Smith             ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr);
421781824310SBarry Smith 	    col--;
421881824310SBarry Smith #else
421981824310SBarry Smith             col = aij->colmap[in[j]] - 1;
422081824310SBarry Smith #endif
422181824310SBarry Smith             if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
422281824310SBarry Smith               ierr = DisAssemble_MPIAIJ(mat);CHKERRQ(ierr);
422381824310SBarry Smith               col =  in[j];
422481824310SBarry Smith               /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
422581824310SBarry Smith               B = aij->B;
422681824310SBarry Smith               b = (Mat_SeqAIJ*)B->data;
422781824310SBarry Smith               bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j;
422881824310SBarry Smith               rp2      = bj + bi[row];
422981824310SBarry Smith               ap2      = ba + bi[row];
423081824310SBarry Smith               rmax2    = bimax[row];
423181824310SBarry Smith               nrow2    = bilen[row];
423281824310SBarry Smith               low2     = 0;
423381824310SBarry Smith               high2    = nrow2;
4234899cda47SBarry Smith               bm       = aij->B->rmap.n;
423581824310SBarry Smith               ba = b->a;
423681824310SBarry Smith             }
423781824310SBarry Smith           } else col = in[j];
423881824310SBarry Smith           MatSetValues_SeqAIJ_B_Private(row,col,value,addv);
423981824310SBarry Smith         }
424081824310SBarry Smith       }
424181824310SBarry Smith     } else {
424281824310SBarry Smith       if (!aij->donotstash) {
424381824310SBarry Smith         if (roworiented) {
424481824310SBarry Smith           if (ignorezeroentries && v[i*n] == 0.0) continue;
424581824310SBarry Smith           ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n);CHKERRQ(ierr);
424681824310SBarry Smith         } else {
424781824310SBarry Smith           if (ignorezeroentries && v[i] == 0.0) continue;
424881824310SBarry Smith           ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m);CHKERRQ(ierr);
424981824310SBarry Smith         }
425081824310SBarry Smith       }
425181824310SBarry Smith     }
425281824310SBarry Smith   }}
425381824310SBarry Smith   PetscFunctionReturnVoid();
425481824310SBarry Smith }
425581824310SBarry Smith EXTERN_C_END
4256