xref: /petsc/src/mat/impls/aij/mpi/mpiaij.c (revision 899cda47322a0d0eb8e2428039961ef470104e3e)
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;
19*899cda47SBarry 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
28*899cda47SBarry Smith   ierr = PetscMalloc((mat->cmap.N+1)*sizeof(PetscInt),&aij->colmap);CHKERRQ(ierr);
29*899cda47SBarry Smith   ierr = PetscLogObjectMemory(mat,mat->cmap.N*sizeof(PetscInt));CHKERRQ(ierr);
30*899cda47SBarry 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); \
58ed1caa07SMatthew Knepley       MatSeqXAIJReallocateAIJ(a,1,nrow1,row,col,rmax1,aa,ai,aj,am,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); \
92ed1caa07SMatthew Knepley       MatSeqXAIJReallocateAIJ(b,1,nrow2,row,col,rmax2,ba,bi,bj,bm,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;
140*899cda47SBarry Smith   PetscInt       i,j,rstart = mat->rmap.rstart,rend = mat->rmap.rend;
141*899cda47SBarry 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;
152*899cda47SBarry 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)
163*899cda47SBarry 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)
190*899cda47SBarry 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;
216*899cda47SBarry 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;
245*899cda47SBarry Smith   PetscInt       i,j,rstart = mat->rmap.rstart,rend = mat->rmap.rend;
246*899cda47SBarry 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]);
251*899cda47SBarry 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]);
256*899cda47SBarry 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 
304*899cda47SBarry 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;
31524f910e3SHong Zhang   Mat_SeqAIJ     *a=(Mat_SeqAIJ *)aij->A->data,*b= (Mat_SeqAIJ *)aij->B->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 
323bc5ccf88SSatish Balay   PetscFunctionBegin;
324bc5ccf88SSatish Balay   if (!aij->donotstash) {
325a2d1c673SSatish Balay     while (1) {
3268798bf22SSatish Balay       ierr = MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);CHKERRQ(ierr);
327a2d1c673SSatish Balay       if (!flg) break;
328a2d1c673SSatish Balay 
329bc5ccf88SSatish Balay       for (i=0; i<n;) {
330bc5ccf88SSatish Balay         /* Now identify the consecutive vals belonging to the same row */
331bc5ccf88SSatish Balay         for (j=i,rstart=row[j]; j<n; j++) { if (row[j] != rstart) break; }
332bc5ccf88SSatish Balay         if (j < n) ncols = j-i;
333bc5ccf88SSatish Balay         else       ncols = n-i;
334bc5ccf88SSatish Balay         /* Now assemble all these values with a single function call */
335bc5ccf88SSatish Balay         ierr = MatSetValues_MPIAIJ(mat,1,row+i,ncols,col+i,val+i,addv);CHKERRQ(ierr);
336bc5ccf88SSatish Balay         i = j;
337bc5ccf88SSatish Balay       }
338bc5ccf88SSatish Balay     }
3398798bf22SSatish Balay     ierr = MatStashScatterEnd_Private(&mat->stash);CHKERRQ(ierr);
340bc5ccf88SSatish Balay   }
3412f53aa61SHong Zhang   a->compressedrow.use     = PETSC_FALSE;
342bc5ccf88SSatish Balay   ierr = MatAssemblyBegin(aij->A,mode);CHKERRQ(ierr);
343bc5ccf88SSatish Balay   ierr = MatAssemblyEnd(aij->A,mode);CHKERRQ(ierr);
344bc5ccf88SSatish Balay 
345bc5ccf88SSatish Balay   /* determine if any processor has disassembled, if so we must
346bc5ccf88SSatish Balay      also disassemble ourselfs, in order that we may reassemble. */
347bc5ccf88SSatish Balay   /*
348bc5ccf88SSatish Balay      if nonzero structure of submatrix B cannot change then we know that
349bc5ccf88SSatish Balay      no processor disassembled thus we can skip this stuff
350bc5ccf88SSatish Balay   */
351bc5ccf88SSatish Balay   if (!((Mat_SeqAIJ*)aij->B->data)->nonew)  {
352bc5ccf88SSatish Balay     ierr = MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPI_INT,MPI_PROD,mat->comm);CHKERRQ(ierr);
353bc5ccf88SSatish Balay     if (mat->was_assembled && !other_disassembled) {
354bc5ccf88SSatish Balay       ierr = DisAssemble_MPIAIJ(mat);CHKERRQ(ierr);
355ad59fb31SSatish Balay     }
356ad59fb31SSatish Balay   }
357ad59fb31SSatish Balay   /* reaccess the b because aij->B was changed in MatSetValues() or DisAssemble() */
358f8a12787SBarry Smith   b    = (Mat_SeqAIJ *)aij->B->data;
359bc5ccf88SSatish Balay 
360bc5ccf88SSatish Balay   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
361bc5ccf88SSatish Balay     ierr = MatSetUpMultiply_MPIAIJ(mat);CHKERRQ(ierr);
362bc5ccf88SSatish Balay   }
363923f20ffSKris Buschelman   ierr = MatSetOption(aij->B,MAT_DO_NOT_USE_INODES);CHKERRQ(ierr);
36446f35f0bSHong Zhang   b->compressedrow.use = PETSC_TRUE;
365bc5ccf88SSatish Balay   ierr = MatAssemblyBegin(aij->B,mode);CHKERRQ(ierr);
366bc5ccf88SSatish Balay   ierr = MatAssemblyEnd(aij->B,mode);CHKERRQ(ierr);
367bc5ccf88SSatish Balay 
368606d414cSSatish Balay   if (aij->rowvalues) {
369606d414cSSatish Balay     ierr = PetscFree(aij->rowvalues);CHKERRQ(ierr);
370606d414cSSatish Balay     aij->rowvalues = 0;
371606d414cSSatish Balay   }
372a30b2313SHong Zhang 
373a30b2313SHong Zhang   /* used by MatAXPY() */
374a30b2313SHong Zhang   a->xtoy = 0; b->xtoy = 0;
375a30b2313SHong Zhang   a->XtoY = 0; b->XtoY = 0;
376a30b2313SHong Zhang 
377bc5ccf88SSatish Balay   PetscFunctionReturn(0);
378bc5ccf88SSatish Balay }
379bc5ccf88SSatish Balay 
3804a2ae208SSatish Balay #undef __FUNCT__
3814a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_MPIAIJ"
382dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_MPIAIJ(Mat A)
3831eb62cbbSBarry Smith {
38444a69424SLois Curfman McInnes   Mat_MPIAIJ     *l = (Mat_MPIAIJ*)A->data;
385dfbe8321SBarry Smith   PetscErrorCode ierr;
3863a40ed3dSBarry Smith 
3873a40ed3dSBarry Smith   PetscFunctionBegin;
38878b31e54SBarry Smith   ierr = MatZeroEntries(l->A);CHKERRQ(ierr);
38978b31e54SBarry Smith   ierr = MatZeroEntries(l->B);CHKERRQ(ierr);
3903a40ed3dSBarry Smith   PetscFunctionReturn(0);
3911eb62cbbSBarry Smith }
3921eb62cbbSBarry Smith 
3934a2ae208SSatish Balay #undef __FUNCT__
3944a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_MPIAIJ"
395f4df32b1SMatthew Knepley PetscErrorCode MatZeroRows_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag)
3961eb62cbbSBarry Smith {
39744a69424SLois Curfman McInnes   Mat_MPIAIJ     *l = (Mat_MPIAIJ*)A->data;
3986849ba73SBarry Smith   PetscErrorCode ierr;
3996543fbbaSBarry Smith   PetscMPIInt    size = l->size,imdex,n,rank = l->rank,tag = A->tag,lastidx = -1;
400*899cda47SBarry Smith   PetscInt       i,*owners = A->rmap.range;
401b1d57f15SBarry Smith   PetscInt       *nprocs,j,idx,nsends,row;
402b1d57f15SBarry Smith   PetscInt       nmax,*svalues,*starts,*owner,nrecvs;
403b1d57f15SBarry Smith   PetscInt       *rvalues,count,base,slen,*source;
404*899cda47SBarry Smith   PetscInt       *lens,*lrows,*values,rstart=A->rmap.rstart;
405d6dfbf8fSBarry Smith   MPI_Comm       comm = A->comm;
4061eb62cbbSBarry Smith   MPI_Request    *send_waits,*recv_waits;
4071eb62cbbSBarry Smith   MPI_Status     recv_status,*send_status;
4086543fbbaSBarry Smith #if defined(PETSC_DEBUG)
4096543fbbaSBarry Smith   PetscTruth     found = PETSC_FALSE;
4106543fbbaSBarry Smith #endif
4111eb62cbbSBarry Smith 
4123a40ed3dSBarry Smith   PetscFunctionBegin;
4131eb62cbbSBarry Smith   /*  first count number of contributors to each processor */
414b1d57f15SBarry Smith   ierr = PetscMalloc(2*size*sizeof(PetscInt),&nprocs);CHKERRQ(ierr);
415b1d57f15SBarry Smith   ierr = PetscMemzero(nprocs,2*size*sizeof(PetscInt));CHKERRQ(ierr);
416b1d57f15SBarry Smith   ierr = PetscMalloc((N+1)*sizeof(PetscInt),&owner);CHKERRQ(ierr); /* see note*/
4176543fbbaSBarry Smith   j = 0;
4181eb62cbbSBarry Smith   for (i=0; i<N; i++) {
4196543fbbaSBarry Smith     if (lastidx > (idx = rows[i])) j = 0;
4206543fbbaSBarry Smith     lastidx = idx;
4216543fbbaSBarry Smith     for (; j<size; j++) {
4221eb62cbbSBarry Smith       if (idx >= owners[j] && idx < owners[j+1]) {
4236543fbbaSBarry Smith         nprocs[2*j]++;
4246543fbbaSBarry Smith         nprocs[2*j+1] = 1;
4256543fbbaSBarry Smith         owner[i] = j;
4266543fbbaSBarry Smith #if defined(PETSC_DEBUG)
4276543fbbaSBarry Smith         found = PETSC_TRUE;
4286543fbbaSBarry Smith #endif
4296543fbbaSBarry Smith         break;
4301eb62cbbSBarry Smith       }
4311eb62cbbSBarry Smith     }
4326543fbbaSBarry Smith #if defined(PETSC_DEBUG)
43329bbc08cSBarry Smith     if (!found) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Index out of range");
4346543fbbaSBarry Smith     found = PETSC_FALSE;
4356543fbbaSBarry Smith #endif
4361eb62cbbSBarry Smith   }
437c1dc657dSBarry Smith   nsends = 0;  for (i=0; i<size; i++) { nsends += nprocs[2*i+1];}
4381eb62cbbSBarry Smith 
4391eb62cbbSBarry Smith   /* inform other processors of number of messages and max length*/
440c1dc657dSBarry Smith   ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr);
4411eb62cbbSBarry Smith 
4421eb62cbbSBarry Smith   /* post receives:   */
443b1d57f15SBarry Smith   ierr = PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(PetscInt),&rvalues);CHKERRQ(ierr);
444b0a32e0cSBarry Smith   ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr);
4451eb62cbbSBarry Smith   for (i=0; i<nrecvs; i++) {
446b1d57f15SBarry Smith     ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPIU_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr);
4471eb62cbbSBarry Smith   }
4481eb62cbbSBarry Smith 
4491eb62cbbSBarry Smith   /* do sends:
4501eb62cbbSBarry Smith       1) starts[i] gives the starting index in svalues for stuff going to
4511eb62cbbSBarry Smith          the ith processor
4521eb62cbbSBarry Smith   */
453b1d57f15SBarry Smith   ierr = PetscMalloc((N+1)*sizeof(PetscInt),&svalues);CHKERRQ(ierr);
454b0a32e0cSBarry Smith   ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr);
455b1d57f15SBarry Smith   ierr = PetscMalloc((size+1)*sizeof(PetscInt),&starts);CHKERRQ(ierr);
4561eb62cbbSBarry Smith   starts[0] = 0;
457c1dc657dSBarry Smith   for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
4581eb62cbbSBarry Smith   for (i=0; i<N; i++) {
4591eb62cbbSBarry Smith     svalues[starts[owner[i]]++] = rows[i];
4601eb62cbbSBarry Smith   }
4611eb62cbbSBarry Smith 
4621eb62cbbSBarry Smith   starts[0] = 0;
463c1dc657dSBarry Smith   for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
4641eb62cbbSBarry Smith   count = 0;
46517699dbbSLois Curfman McInnes   for (i=0; i<size; i++) {
466c1dc657dSBarry Smith     if (nprocs[2*i+1]) {
467b1d57f15SBarry Smith       ierr = MPI_Isend(svalues+starts[i],nprocs[2*i],MPIU_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr);
4681eb62cbbSBarry Smith     }
4691eb62cbbSBarry Smith   }
470606d414cSSatish Balay   ierr = PetscFree(starts);CHKERRQ(ierr);
4711eb62cbbSBarry Smith 
47217699dbbSLois Curfman McInnes   base = owners[rank];
4731eb62cbbSBarry Smith 
4741eb62cbbSBarry Smith   /*  wait on receives */
475b1d57f15SBarry Smith   ierr   = PetscMalloc(2*(nrecvs+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
4761eb62cbbSBarry Smith   source = lens + nrecvs;
4771eb62cbbSBarry Smith   count  = nrecvs; slen = 0;
4781eb62cbbSBarry Smith   while (count) {
479ca161407SBarry Smith     ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
4801eb62cbbSBarry Smith     /* unpack receives into our local space */
481b1d57f15SBarry Smith     ierr = MPI_Get_count(&recv_status,MPIU_INT,&n);CHKERRQ(ierr);
482d6dfbf8fSBarry Smith     source[imdex]  = recv_status.MPI_SOURCE;
483d6dfbf8fSBarry Smith     lens[imdex]    = n;
4841eb62cbbSBarry Smith     slen          += n;
4851eb62cbbSBarry Smith     count--;
4861eb62cbbSBarry Smith   }
487606d414cSSatish Balay   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
4881eb62cbbSBarry Smith 
4891eb62cbbSBarry Smith   /* move the data into the send scatter */
490b1d57f15SBarry Smith   ierr = PetscMalloc((slen+1)*sizeof(PetscInt),&lrows);CHKERRQ(ierr);
4911eb62cbbSBarry Smith   count = 0;
4921eb62cbbSBarry Smith   for (i=0; i<nrecvs; i++) {
4931eb62cbbSBarry Smith     values = rvalues + i*nmax;
4941eb62cbbSBarry Smith     for (j=0; j<lens[i]; j++) {
4951eb62cbbSBarry Smith       lrows[count++] = values[j] - base;
4961eb62cbbSBarry Smith     }
4971eb62cbbSBarry Smith   }
498606d414cSSatish Balay   ierr = PetscFree(rvalues);CHKERRQ(ierr);
499606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
500606d414cSSatish Balay   ierr = PetscFree(owner);CHKERRQ(ierr);
501606d414cSSatish Balay   ierr = PetscFree(nprocs);CHKERRQ(ierr);
5021eb62cbbSBarry Smith 
5031eb62cbbSBarry Smith   /* actually zap the local rows */
5046eb55b6aSBarry Smith   /*
5056eb55b6aSBarry Smith         Zero the required rows. If the "diagonal block" of the matrix
506a8c7a070SBarry Smith      is square and the user wishes to set the diagonal we use separate
5076eb55b6aSBarry Smith      code so that MatSetValues() is not called for each diagonal allocating
5086eb55b6aSBarry Smith      new memory, thus calling lots of mallocs and slowing things down.
5096eb55b6aSBarry Smith 
510f4df32b1SMatthew Knepley        Contributed by: Matthew Knepley
5116eb55b6aSBarry Smith   */
512e2d53e46SBarry Smith   /* must zero l->B before l->A because the (diag) case below may put values into l->B*/
513f4df32b1SMatthew Knepley   ierr = MatZeroRows(l->B,slen,lrows,0.0);CHKERRQ(ierr);
514*899cda47SBarry Smith   if ((diag != 0.0) && (l->A->rmap.N == l->A->cmap.N)) {
515f4df32b1SMatthew Knepley     ierr      = MatZeroRows(l->A,slen,lrows,diag);CHKERRQ(ierr);
516f4df32b1SMatthew Knepley   } else if (diag != 0.0) {
517f4df32b1SMatthew Knepley     ierr = MatZeroRows(l->A,slen,lrows,0.0);CHKERRQ(ierr);
518fa46199cSSatish Balay     if (((Mat_SeqAIJ*)l->A->data)->nonew) {
51929bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"MatZeroRows() on rectangular matrices cannot be used with the Mat options\n\
520fa46199cSSatish Balay MAT_NO_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR");
5216525c446SSatish Balay     }
522e2d53e46SBarry Smith     for (i = 0; i < slen; i++) {
523e2d53e46SBarry Smith       row  = lrows[i] + rstart;
524f4df32b1SMatthew Knepley       ierr = MatSetValues(A,1,&row,1,&row,&diag,INSERT_VALUES);CHKERRQ(ierr);
525e2d53e46SBarry Smith     }
526e2d53e46SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
527e2d53e46SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
5286eb55b6aSBarry Smith   } else {
529f4df32b1SMatthew Knepley     ierr = MatZeroRows(l->A,slen,lrows,0.0);CHKERRQ(ierr);
5306eb55b6aSBarry Smith   }
531606d414cSSatish Balay   ierr = PetscFree(lrows);CHKERRQ(ierr);
53272dacd9aSBarry Smith 
5331eb62cbbSBarry Smith   /* wait on sends */
5341eb62cbbSBarry Smith   if (nsends) {
535b0a32e0cSBarry Smith     ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr);
536ca161407SBarry Smith     ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr);
537606d414cSSatish Balay     ierr = PetscFree(send_status);CHKERRQ(ierr);
5381eb62cbbSBarry Smith   }
539606d414cSSatish Balay   ierr = PetscFree(send_waits);CHKERRQ(ierr);
540606d414cSSatish Balay   ierr = PetscFree(svalues);CHKERRQ(ierr);
5411eb62cbbSBarry Smith 
5423a40ed3dSBarry Smith   PetscFunctionReturn(0);
5431eb62cbbSBarry Smith }
5441eb62cbbSBarry Smith 
5454a2ae208SSatish Balay #undef __FUNCT__
5464a2ae208SSatish Balay #define __FUNCT__ "MatMult_MPIAIJ"
547dfbe8321SBarry Smith PetscErrorCode MatMult_MPIAIJ(Mat A,Vec xx,Vec yy)
5481eb62cbbSBarry Smith {
549416022c9SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
550dfbe8321SBarry Smith   PetscErrorCode ierr;
551b1d57f15SBarry Smith   PetscInt       nt;
552416022c9SBarry Smith 
5533a40ed3dSBarry Smith   PetscFunctionBegin;
554a2ce50c7SBarry Smith   ierr = VecGetLocalSize(xx,&nt);CHKERRQ(ierr);
555*899cda47SBarry Smith   if (nt != A->cmap.n) {
556*899cda47SBarry Smith     SETERRQ2(PETSC_ERR_ARG_SIZ,"Incompatible partition of A (%D) and xx (%D)",A->cmap.n,nt);
557fbd6ef76SBarry Smith   }
55843a90d84SBarry Smith   ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
559f830108cSBarry Smith   ierr = (*a->A->ops->mult)(a->A,xx,yy);CHKERRQ(ierr);
56043a90d84SBarry Smith   ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
561f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);CHKERRQ(ierr);
5623a40ed3dSBarry Smith   PetscFunctionReturn(0);
5631eb62cbbSBarry Smith }
5641eb62cbbSBarry Smith 
5654a2ae208SSatish Balay #undef __FUNCT__
5664a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_MPIAIJ"
567dfbe8321SBarry Smith PetscErrorCode MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
568da3a660dSBarry Smith {
569416022c9SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
570dfbe8321SBarry Smith   PetscErrorCode ierr;
5713a40ed3dSBarry Smith 
5723a40ed3dSBarry Smith   PetscFunctionBegin;
57343a90d84SBarry Smith   ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
574f830108cSBarry Smith   ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
57543a90d84SBarry Smith   ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
576f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);CHKERRQ(ierr);
5773a40ed3dSBarry Smith   PetscFunctionReturn(0);
578da3a660dSBarry Smith }
579da3a660dSBarry Smith 
5804a2ae208SSatish Balay #undef __FUNCT__
5814a2ae208SSatish Balay #define __FUNCT__ "MatMultTranspose_MPIAIJ"
582dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_MPIAIJ(Mat A,Vec xx,Vec yy)
583da3a660dSBarry Smith {
584416022c9SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
585dfbe8321SBarry Smith   PetscErrorCode ierr;
586a5ff213dSBarry Smith   PetscTruth     merged;
587da3a660dSBarry Smith 
5883a40ed3dSBarry Smith   PetscFunctionBegin;
589a5ff213dSBarry Smith   ierr = VecScatterGetMerged(a->Mvctx,&merged);CHKERRQ(ierr);
590da3a660dSBarry Smith   /* do nondiagonal part */
5917c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
592a5ff213dSBarry Smith   if (!merged) {
593da3a660dSBarry Smith     /* send it on its way */
594537820f0SBarry Smith     ierr = VecScatterBegin(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
595da3a660dSBarry Smith     /* do local part */
5967c922b88SBarry Smith     ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr);
597da3a660dSBarry Smith     /* receive remote parts: note this assumes the values are not actually */
598a5ff213dSBarry Smith     /* added in yy until the next line, */
599537820f0SBarry Smith     ierr = VecScatterEnd(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
600a5ff213dSBarry Smith   } else {
601a5ff213dSBarry Smith     /* do local part */
602a5ff213dSBarry Smith     ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr);
603a5ff213dSBarry Smith     /* send it on its way */
604a5ff213dSBarry Smith     ierr = VecScatterBegin(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
605a5ff213dSBarry Smith     /* values actually were received in the Begin() but we need to call this nop */
606a5ff213dSBarry Smith     ierr = VecScatterEnd(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
607a5ff213dSBarry Smith   }
6083a40ed3dSBarry Smith   PetscFunctionReturn(0);
609da3a660dSBarry Smith }
610da3a660dSBarry Smith 
611cd0d46ebSvictorle EXTERN_C_BEGIN
612cd0d46ebSvictorle #undef __FUNCT__
6135fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_MPIAIJ"
61413c77408SMatthew Knepley PetscErrorCode PETSCMAT_DLLEXPORT MatIsTranspose_MPIAIJ(Mat Amat,Mat Bmat,PetscReal tol,PetscTruth *f)
615cd0d46ebSvictorle {
6164f423910Svictorle   MPI_Comm       comm;
617cd0d46ebSvictorle   Mat_MPIAIJ     *Aij = (Mat_MPIAIJ *) Amat->data, *Bij;
61866501d38Svictorle   Mat            Adia = Aij->A, Bdia, Aoff,Boff,*Aoffs,*Boffs;
619cd0d46ebSvictorle   IS             Me,Notme;
6206849ba73SBarry Smith   PetscErrorCode ierr;
621b1d57f15SBarry Smith   PetscInt       M,N,first,last,*notme,i;
622b1d57f15SBarry Smith   PetscMPIInt    size;
623cd0d46ebSvictorle 
624cd0d46ebSvictorle   PetscFunctionBegin;
62542e5f5b4Svictorle 
62642e5f5b4Svictorle   /* Easy test: symmetric diagonal block */
62766501d38Svictorle   Bij = (Mat_MPIAIJ *) Bmat->data; Bdia = Bij->A;
6285485867bSBarry Smith   ierr = MatIsTranspose(Adia,Bdia,tol,f);CHKERRQ(ierr);
629cd0d46ebSvictorle   if (!*f) PetscFunctionReturn(0);
6304f423910Svictorle   ierr = PetscObjectGetComm((PetscObject)Amat,&comm);CHKERRQ(ierr);
631b1d57f15SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
632b1d57f15SBarry Smith   if (size == 1) PetscFunctionReturn(0);
63342e5f5b4Svictorle 
63442e5f5b4Svictorle   /* Hard test: off-diagonal block. This takes a MatGetSubMatrix. */
635cd0d46ebSvictorle   ierr = MatGetSize(Amat,&M,&N);CHKERRQ(ierr);
636cd0d46ebSvictorle   ierr = MatGetOwnershipRange(Amat,&first,&last);CHKERRQ(ierr);
637b1d57f15SBarry Smith   ierr = PetscMalloc((N-last+first)*sizeof(PetscInt),&notme);CHKERRQ(ierr);
638cd0d46ebSvictorle   for (i=0; i<first; i++) notme[i] = i;
639cd0d46ebSvictorle   for (i=last; i<M; i++) notme[i-last+first] = i;
640268466fbSBarry Smith   ierr = ISCreateGeneral(MPI_COMM_SELF,N-last+first,notme,&Notme);CHKERRQ(ierr);
641268466fbSBarry Smith   ierr = ISCreateStride(MPI_COMM_SELF,last-first,first,1,&Me);CHKERRQ(ierr);
642268466fbSBarry Smith   ierr = MatGetSubMatrices(Amat,1,&Me,&Notme,MAT_INITIAL_MATRIX,&Aoffs);CHKERRQ(ierr);
64366501d38Svictorle   Aoff = Aoffs[0];
644268466fbSBarry Smith   ierr = MatGetSubMatrices(Bmat,1,&Notme,&Me,MAT_INITIAL_MATRIX,&Boffs);CHKERRQ(ierr);
64566501d38Svictorle   Boff = Boffs[0];
6465485867bSBarry Smith   ierr = MatIsTranspose(Aoff,Boff,tol,f);CHKERRQ(ierr);
64766501d38Svictorle   ierr = MatDestroyMatrices(1,&Aoffs);CHKERRQ(ierr);
64866501d38Svictorle   ierr = MatDestroyMatrices(1,&Boffs);CHKERRQ(ierr);
64942e5f5b4Svictorle   ierr = ISDestroy(Me);CHKERRQ(ierr);
65042e5f5b4Svictorle   ierr = ISDestroy(Notme);CHKERRQ(ierr);
65142e5f5b4Svictorle 
652cd0d46ebSvictorle   PetscFunctionReturn(0);
653cd0d46ebSvictorle }
654cd0d46ebSvictorle EXTERN_C_END
655cd0d46ebSvictorle 
6564a2ae208SSatish Balay #undef __FUNCT__
6574a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_MPIAIJ"
658dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
659da3a660dSBarry Smith {
660416022c9SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
661dfbe8321SBarry Smith   PetscErrorCode ierr;
662da3a660dSBarry Smith 
6633a40ed3dSBarry Smith   PetscFunctionBegin;
664da3a660dSBarry Smith   /* do nondiagonal part */
6657c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
666da3a660dSBarry Smith   /* send it on its way */
667537820f0SBarry Smith   ierr = VecScatterBegin(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
668da3a660dSBarry Smith   /* do local part */
6697c922b88SBarry Smith   ierr = (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
670a5ff213dSBarry Smith   /* receive remote parts */
6710a198c4cSBarry Smith   ierr = VecScatterEnd(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
6723a40ed3dSBarry Smith   PetscFunctionReturn(0);
673da3a660dSBarry Smith }
674da3a660dSBarry Smith 
6751eb62cbbSBarry Smith /*
6761eb62cbbSBarry Smith   This only works correctly for square matrices where the subblock A->A is the
6771eb62cbbSBarry Smith    diagonal block
6781eb62cbbSBarry Smith */
6794a2ae208SSatish Balay #undef __FUNCT__
6804a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_MPIAIJ"
681dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_MPIAIJ(Mat A,Vec v)
6821eb62cbbSBarry Smith {
683dfbe8321SBarry Smith   PetscErrorCode ierr;
684416022c9SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
6853a40ed3dSBarry Smith 
6863a40ed3dSBarry Smith   PetscFunctionBegin;
687*899cda47SBarry Smith   if (A->rmap.N != A->cmap.N) SETERRQ(PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block");
688*899cda47SBarry Smith   if (A->rmap.rstart != A->cmap.rstart || A->rmap.rend != A->cmap.rend) {
68929bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_SIZ,"row partition must equal col partition");
6903a40ed3dSBarry Smith   }
6913a40ed3dSBarry Smith   ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr);
6923a40ed3dSBarry Smith   PetscFunctionReturn(0);
6931eb62cbbSBarry Smith }
6941eb62cbbSBarry Smith 
6954a2ae208SSatish Balay #undef __FUNCT__
6964a2ae208SSatish Balay #define __FUNCT__ "MatScale_MPIAIJ"
697f4df32b1SMatthew Knepley PetscErrorCode MatScale_MPIAIJ(Mat A,PetscScalar aa)
698052efed2SBarry Smith {
699052efed2SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
700dfbe8321SBarry Smith   PetscErrorCode ierr;
7013a40ed3dSBarry Smith 
7023a40ed3dSBarry Smith   PetscFunctionBegin;
703f4df32b1SMatthew Knepley   ierr = MatScale(a->A,aa);CHKERRQ(ierr);
704f4df32b1SMatthew Knepley   ierr = MatScale(a->B,aa);CHKERRQ(ierr);
7053a40ed3dSBarry Smith   PetscFunctionReturn(0);
706052efed2SBarry Smith }
707052efed2SBarry Smith 
7084a2ae208SSatish Balay #undef __FUNCT__
7094a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_MPIAIJ"
710dfbe8321SBarry Smith PetscErrorCode MatDestroy_MPIAIJ(Mat mat)
7111eb62cbbSBarry Smith {
71244a69424SLois Curfman McInnes   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
713dfbe8321SBarry Smith   PetscErrorCode ierr;
71483e2fdc7SBarry Smith 
7153a40ed3dSBarry Smith   PetscFunctionBegin;
716aa482453SBarry Smith #if defined(PETSC_USE_LOG)
717*899cda47SBarry Smith   PetscLogObjectState((PetscObject)mat,"Rows=%D, Cols=%D",mat->rmap.N,mat->cmap.N);
718a5a9c739SBarry Smith #endif
7198798bf22SSatish Balay   ierr = MatStashDestroy_Private(&mat->stash);CHKERRQ(ierr);
72078b31e54SBarry Smith   ierr = MatDestroy(aij->A);CHKERRQ(ierr);
72178b31e54SBarry Smith   ierr = MatDestroy(aij->B);CHKERRQ(ierr);
722aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
7230f5bd95cSBarry Smith   if (aij->colmap) {ierr = PetscTableDelete(aij->colmap);CHKERRQ(ierr);}
724b1fc9764SSatish Balay #else
725606d414cSSatish Balay   if (aij->colmap) {ierr = PetscFree(aij->colmap);CHKERRQ(ierr);}
726b1fc9764SSatish Balay #endif
727606d414cSSatish Balay   if (aij->garray) {ierr = PetscFree(aij->garray);CHKERRQ(ierr);}
7287c922b88SBarry Smith   if (aij->lvec)   {ierr = VecDestroy(aij->lvec);CHKERRQ(ierr);}
7297c922b88SBarry Smith   if (aij->Mvctx)  {ierr = VecScatterDestroy(aij->Mvctx);CHKERRQ(ierr);}
730606d414cSSatish Balay   if (aij->rowvalues) {ierr = PetscFree(aij->rowvalues);CHKERRQ(ierr);}
731606d414cSSatish Balay   ierr = PetscFree(aij);CHKERRQ(ierr);
732901853e0SKris Buschelman 
733901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
734901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
735901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatGetDiagonalBlock_C","",PETSC_NULL);CHKERRQ(ierr);
736901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr);
737901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
738ff69c46cSKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
739901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatDiagonalScaleLocal_C","",PETSC_NULL);CHKERRQ(ierr);
7403a40ed3dSBarry Smith   PetscFunctionReturn(0);
7411eb62cbbSBarry Smith }
742ee50ffe9SBarry Smith 
7434a2ae208SSatish Balay #undef __FUNCT__
7448e2fed03SBarry Smith #define __FUNCT__ "MatView_MPIAIJ_Binary"
745dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ_Binary(Mat mat,PetscViewer viewer)
7468e2fed03SBarry Smith {
7478e2fed03SBarry Smith   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*)mat->data;
7488e2fed03SBarry Smith   Mat_SeqAIJ*       A = (Mat_SeqAIJ*)aij->A->data;
7498e2fed03SBarry Smith   Mat_SeqAIJ*       B = (Mat_SeqAIJ*)aij->B->data;
7506849ba73SBarry Smith   PetscErrorCode    ierr;
75132dcc486SBarry Smith   PetscMPIInt       rank,size,tag = ((PetscObject)viewer)->tag;
7526f69ff64SBarry Smith   int               fd;
7536f69ff64SBarry Smith   PetscInt          nz,header[4],*row_lengths,*range,rlen,i;
754*899cda47SBarry Smith   PetscInt          nzmax,*column_indices,j,k,col,*garray = aij->garray,cnt,cstart = mat->cmap.rstart,rnz;
7558e2fed03SBarry Smith   PetscScalar       *column_values;
7568e2fed03SBarry Smith 
7578e2fed03SBarry Smith   PetscFunctionBegin;
7588e2fed03SBarry Smith   ierr = MPI_Comm_rank(mat->comm,&rank);CHKERRQ(ierr);
7598e2fed03SBarry Smith   ierr = MPI_Comm_size(mat->comm,&size);CHKERRQ(ierr);
7608e2fed03SBarry Smith   nz   = A->nz + B->nz;
761958c9bccSBarry Smith   if (!rank) {
7628e2fed03SBarry Smith     header[0] = MAT_FILE_COOKIE;
763*899cda47SBarry Smith     header[1] = mat->rmap.N;
764*899cda47SBarry Smith     header[2] = mat->cmap.N;
765b1d57f15SBarry Smith     ierr = MPI_Reduce(&nz,&header[3],1,MPIU_INT,MPI_SUM,0,mat->comm);CHKERRQ(ierr);
7668e2fed03SBarry Smith     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
7676f69ff64SBarry Smith     ierr = PetscBinaryWrite(fd,header,4,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
7688e2fed03SBarry Smith     /* get largest number of rows any processor has */
769*899cda47SBarry Smith     rlen = mat->rmap.n;
770357abbc8SBarry Smith     range = mat->rmap.range;
7718e2fed03SBarry Smith     for (i=1; i<size; i++) {
7728e2fed03SBarry Smith       rlen = PetscMax(rlen,range[i+1] - range[i]);
7738e2fed03SBarry Smith     }
7748e2fed03SBarry Smith   } else {
775b1d57f15SBarry Smith     ierr = MPI_Reduce(&nz,0,1,MPIU_INT,MPI_SUM,0,mat->comm);CHKERRQ(ierr);
776*899cda47SBarry Smith     rlen = mat->rmap.n;
7778e2fed03SBarry Smith   }
7788e2fed03SBarry Smith 
7798e2fed03SBarry Smith   /* load up the local row counts */
780b1d57f15SBarry Smith   ierr = PetscMalloc((rlen+1)*sizeof(PetscInt),&row_lengths);CHKERRQ(ierr);
781*899cda47SBarry Smith   for (i=0; i<mat->rmap.n; i++) {
7828e2fed03SBarry Smith     row_lengths[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i];
7838e2fed03SBarry Smith   }
7848e2fed03SBarry Smith 
7858e2fed03SBarry Smith   /* store the row lengths to the file */
786958c9bccSBarry Smith   if (!rank) {
7878e2fed03SBarry Smith     MPI_Status status;
788*899cda47SBarry Smith     ierr = PetscBinaryWrite(fd,row_lengths,mat->rmap.n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
7898e2fed03SBarry Smith     for (i=1; i<size; i++) {
7908e2fed03SBarry Smith       rlen = range[i+1] - range[i];
791b1d57f15SBarry Smith       ierr = MPI_Recv(row_lengths,rlen,MPIU_INT,i,tag,mat->comm,&status);CHKERRQ(ierr);
7926f69ff64SBarry Smith       ierr = PetscBinaryWrite(fd,row_lengths,rlen,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
7938e2fed03SBarry Smith     }
7948e2fed03SBarry Smith   } else {
795*899cda47SBarry Smith     ierr = MPI_Send(row_lengths,mat->rmap.n,MPIU_INT,0,tag,mat->comm);CHKERRQ(ierr);
7968e2fed03SBarry Smith   }
7978e2fed03SBarry Smith   ierr = PetscFree(row_lengths);CHKERRQ(ierr);
7988e2fed03SBarry Smith 
7998e2fed03SBarry Smith   /* load up the local column indices */
8008e2fed03SBarry Smith   nzmax = nz; /* )th processor needs space a largest processor needs */
801b1d57f15SBarry Smith   ierr = MPI_Reduce(&nz,&nzmax,1,MPIU_INT,MPI_MAX,0,mat->comm);CHKERRQ(ierr);
802b1d57f15SBarry Smith   ierr = PetscMalloc((nzmax+1)*sizeof(PetscInt),&column_indices);CHKERRQ(ierr);
8038e2fed03SBarry Smith   cnt  = 0;
804*899cda47SBarry Smith   for (i=0; i<mat->rmap.n; i++) {
8058e2fed03SBarry Smith     for (j=B->i[i]; j<B->i[i+1]; j++) {
8068e2fed03SBarry Smith       if ( (col = garray[B->j[j]]) > cstart) break;
8078e2fed03SBarry Smith       column_indices[cnt++] = col;
8088e2fed03SBarry Smith     }
8098e2fed03SBarry Smith     for (k=A->i[i]; k<A->i[i+1]; k++) {
8108e2fed03SBarry Smith       column_indices[cnt++] = A->j[k] + cstart;
8118e2fed03SBarry Smith     }
8128e2fed03SBarry Smith     for (; j<B->i[i+1]; j++) {
8138e2fed03SBarry Smith       column_indices[cnt++] = garray[B->j[j]];
8148e2fed03SBarry Smith     }
8158e2fed03SBarry Smith   }
81677431f27SBarry Smith   if (cnt != A->nz + B->nz) SETERRQ2(PETSC_ERR_LIB,"Internal PETSc error: cnt = %D nz = %D",cnt,A->nz+B->nz);
8178e2fed03SBarry Smith 
8188e2fed03SBarry Smith   /* store the column indices to the file */
819958c9bccSBarry Smith   if (!rank) {
8208e2fed03SBarry Smith     MPI_Status status;
8216f69ff64SBarry Smith     ierr = PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
8228e2fed03SBarry Smith     for (i=1; i<size; i++) {
823b1d57f15SBarry Smith       ierr = MPI_Recv(&rnz,1,MPIU_INT,i,tag,mat->comm,&status);CHKERRQ(ierr);
82477431f27SBarry Smith       if (rnz > nzmax) SETERRQ2(PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax);
825b1d57f15SBarry Smith       ierr = MPI_Recv(column_indices,rnz,MPIU_INT,i,tag,mat->comm,&status);CHKERRQ(ierr);
8266f69ff64SBarry Smith       ierr = PetscBinaryWrite(fd,column_indices,rnz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
8278e2fed03SBarry Smith     }
8288e2fed03SBarry Smith   } else {
829b1d57f15SBarry Smith     ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,mat->comm);CHKERRQ(ierr);
830b1d57f15SBarry Smith     ierr = MPI_Send(column_indices,nz,MPIU_INT,0,tag,mat->comm);CHKERRQ(ierr);
8318e2fed03SBarry Smith   }
8328e2fed03SBarry Smith   ierr = PetscFree(column_indices);CHKERRQ(ierr);
8338e2fed03SBarry Smith 
8348e2fed03SBarry Smith   /* load up the local column values */
8358e2fed03SBarry Smith   ierr = PetscMalloc((nzmax+1)*sizeof(PetscScalar),&column_values);CHKERRQ(ierr);
8368e2fed03SBarry Smith   cnt  = 0;
837*899cda47SBarry Smith   for (i=0; i<mat->rmap.n; i++) {
8388e2fed03SBarry Smith     for (j=B->i[i]; j<B->i[i+1]; j++) {
8398e2fed03SBarry Smith       if ( garray[B->j[j]] > cstart) break;
8408e2fed03SBarry Smith       column_values[cnt++] = B->a[j];
8418e2fed03SBarry Smith     }
8428e2fed03SBarry Smith     for (k=A->i[i]; k<A->i[i+1]; k++) {
8438e2fed03SBarry Smith       column_values[cnt++] = A->a[k];
8448e2fed03SBarry Smith     }
8458e2fed03SBarry Smith     for (; j<B->i[i+1]; j++) {
8468e2fed03SBarry Smith       column_values[cnt++] = B->a[j];
8478e2fed03SBarry Smith     }
8488e2fed03SBarry Smith   }
84977431f27SBarry Smith   if (cnt != A->nz + B->nz) SETERRQ2(PETSC_ERR_PLIB,"Internal PETSc error: cnt = %D nz = %D",cnt,A->nz+B->nz);
8508e2fed03SBarry Smith 
8518e2fed03SBarry Smith   /* store the column values to the file */
852958c9bccSBarry Smith   if (!rank) {
8538e2fed03SBarry Smith     MPI_Status status;
8546f69ff64SBarry Smith     ierr = PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr);
8558e2fed03SBarry Smith     for (i=1; i<size; i++) {
856b1d57f15SBarry Smith       ierr = MPI_Recv(&rnz,1,MPIU_INT,i,tag,mat->comm,&status);CHKERRQ(ierr);
85777431f27SBarry Smith       if (rnz > nzmax) SETERRQ2(PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax);
858b021249fSBarry Smith       ierr = MPI_Recv(column_values,rnz,MPIU_SCALAR,i,tag,mat->comm,&status);CHKERRQ(ierr);
8596f69ff64SBarry Smith       ierr = PetscBinaryWrite(fd,column_values,rnz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr);
8608e2fed03SBarry Smith     }
8618e2fed03SBarry Smith   } else {
862b1d57f15SBarry Smith     ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,mat->comm);CHKERRQ(ierr);
8638e2fed03SBarry Smith     ierr = MPI_Send(column_values,nz,MPIU_SCALAR,0,tag,mat->comm);CHKERRQ(ierr);
8648e2fed03SBarry Smith   }
8658e2fed03SBarry Smith   ierr = PetscFree(column_values);CHKERRQ(ierr);
8668e2fed03SBarry Smith   PetscFunctionReturn(0);
8678e2fed03SBarry Smith }
8688e2fed03SBarry Smith 
8698e2fed03SBarry Smith #undef __FUNCT__
8704a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ_ASCIIorDraworSocket"
871dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
872416022c9SBarry Smith {
87344a69424SLois Curfman McInnes   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*)mat->data;
874dfbe8321SBarry Smith   PetscErrorCode    ierr;
87532dcc486SBarry Smith   PetscMPIInt       rank = aij->rank,size = aij->size;
876d38fa0fbSBarry Smith   PetscTruth        isdraw,iascii,isbinary;
877b0a32e0cSBarry Smith   PetscViewer       sviewer;
878f3ef73ceSBarry Smith   PetscViewerFormat format;
879416022c9SBarry Smith 
8803a40ed3dSBarry Smith   PetscFunctionBegin;
881fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
88232077d6dSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
8838e2fed03SBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
88432077d6dSBarry Smith   if (iascii) {
885b0a32e0cSBarry Smith     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
886456192e2SBarry Smith     if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
8874e220ebcSLois Curfman McInnes       MatInfo    info;
888923f20ffSKris Buschelman       PetscTruth inodes;
889923f20ffSKris Buschelman 
8901dab6e02SBarry Smith       ierr = MPI_Comm_rank(mat->comm,&rank);CHKERRQ(ierr);
891888f2ed8SSatish Balay       ierr = MatGetInfo(mat,MAT_LOCAL,&info);CHKERRQ(ierr);
892923f20ffSKris Buschelman       ierr = MatInodeGetInodeSizes(aij->A,PETSC_NULL,(PetscInt **)&inodes,PETSC_NULL);CHKERRQ(ierr);
893923f20ffSKris Buschelman       if (!inodes) {
89477431f27SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, not using I-node routines\n",
895*899cda47SBarry Smith 					      rank,mat->rmap.n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);CHKERRQ(ierr);
8966831982aSBarry Smith       } else {
89777431f27SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, using I-node routines\n",
898*899cda47SBarry Smith 		    rank,mat->rmap.n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);CHKERRQ(ierr);
8996831982aSBarry Smith       }
900888f2ed8SSatish Balay       ierr = MatGetInfo(aij->A,MAT_LOCAL,&info);CHKERRQ(ierr);
90177431f27SBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr);
902888f2ed8SSatish Balay       ierr = MatGetInfo(aij->B,MAT_LOCAL,&info);CHKERRQ(ierr);
90377431f27SBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr);
904b0a32e0cSBarry Smith       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
905a40aa06bSLois Curfman McInnes       ierr = VecScatterView(aij->Mvctx,viewer);CHKERRQ(ierr);
9063a40ed3dSBarry Smith       PetscFunctionReturn(0);
907fb9695e5SSatish Balay     } else if (format == PETSC_VIEWER_ASCII_INFO) {
908923f20ffSKris Buschelman       PetscInt   inodecount,inodelimit,*inodes;
909923f20ffSKris Buschelman       ierr = MatInodeGetInodeSizes(aij->A,&inodecount,&inodes,&inodelimit);CHKERRQ(ierr);
910923f20ffSKris Buschelman       if (inodes) {
911923f20ffSKris Buschelman         ierr = PetscViewerASCIIPrintf(viewer,"using I-node (on process 0) routines: found %D nodes, limit used is %D\n",inodecount,inodelimit);CHKERRQ(ierr);
912d38fa0fbSBarry Smith       } else {
913d38fa0fbSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"not using I-node (on process 0) routines\n");CHKERRQ(ierr);
914d38fa0fbSBarry Smith       }
9153a40ed3dSBarry Smith       PetscFunctionReturn(0);
9164aedb280SBarry Smith     } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
9174aedb280SBarry Smith       PetscFunctionReturn(0);
91808480c60SBarry Smith     }
9198e2fed03SBarry Smith   } else if (isbinary) {
9208e2fed03SBarry Smith     if (size == 1) {
9218e2fed03SBarry Smith       ierr = PetscObjectSetName((PetscObject)aij->A,mat->name);CHKERRQ(ierr);
9228e2fed03SBarry Smith       ierr = MatView(aij->A,viewer);CHKERRQ(ierr);
9238e2fed03SBarry Smith     } else {
9248e2fed03SBarry Smith       ierr = MatView_MPIAIJ_Binary(mat,viewer);CHKERRQ(ierr);
9258e2fed03SBarry Smith     }
9268e2fed03SBarry Smith     PetscFunctionReturn(0);
9270f5bd95cSBarry Smith   } else if (isdraw) {
928b0a32e0cSBarry Smith     PetscDraw  draw;
92919bcc07fSBarry Smith     PetscTruth isnull;
930b0a32e0cSBarry Smith     ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
931b0a32e0cSBarry Smith     ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
93219bcc07fSBarry Smith   }
93319bcc07fSBarry Smith 
93417699dbbSLois Curfman McInnes   if (size == 1) {
935e36acaf3SBarry Smith     ierr = PetscObjectSetName((PetscObject)aij->A,mat->name);CHKERRQ(ierr);
93678b31e54SBarry Smith     ierr = MatView(aij->A,viewer);CHKERRQ(ierr);
9373a40ed3dSBarry Smith   } else {
93895373324SBarry Smith     /* assemble the entire matrix onto first processor. */
93995373324SBarry Smith     Mat         A;
940ec8511deSBarry Smith     Mat_SeqAIJ  *Aloc;
941*899cda47SBarry Smith     PetscInt    M = mat->rmap.N,N = mat->cmap.N,m,*ai,*aj,row,*cols,i,*ct;
94287828ca2SBarry Smith     PetscScalar *a;
9432ee70a88SLois Curfman McInnes 
944f69a0ea3SMatthew Knepley     ierr = MatCreate(mat->comm,&A);CHKERRQ(ierr);
94517699dbbSLois Curfman McInnes     if (!rank) {
946f69a0ea3SMatthew Knepley       ierr = MatSetSizes(A,M,N,M,N);CHKERRQ(ierr);
9473a40ed3dSBarry Smith     } else {
948f69a0ea3SMatthew Knepley       ierr = MatSetSizes(A,0,0,M,N);CHKERRQ(ierr);
94995373324SBarry Smith     }
950f204ca49SKris Buschelman     /* This is just a temporary matrix, so explicitly using MATMPIAIJ is probably best */
951f204ca49SKris Buschelman     ierr = MatSetType(A,MATMPIAIJ);CHKERRQ(ierr);
952f204ca49SKris Buschelman     ierr = MatMPIAIJSetPreallocation(A,0,PETSC_NULL,0,PETSC_NULL);CHKERRQ(ierr);
95352e6d16bSBarry Smith     ierr = PetscLogObjectParent(mat,A);CHKERRQ(ierr);
954416022c9SBarry Smith 
95595373324SBarry Smith     /* copy over the A part */
956ec8511deSBarry Smith     Aloc = (Mat_SeqAIJ*)aij->A->data;
957*899cda47SBarry Smith     m = aij->A->rmap.n; ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
958*899cda47SBarry Smith     row = mat->rmap.rstart;
959*899cda47SBarry Smith     for (i=0; i<ai[m]; i++) {aj[i] += mat->cmap.rstart ;}
96095373324SBarry Smith     for (i=0; i<m; i++) {
961416022c9SBarry Smith       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);CHKERRQ(ierr);
96295373324SBarry Smith       row++; a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
96395373324SBarry Smith     }
9642ee70a88SLois Curfman McInnes     aj = Aloc->j;
965*899cda47SBarry Smith     for (i=0; i<ai[m]; i++) {aj[i] -= mat->cmap.rstart;}
96695373324SBarry Smith 
96795373324SBarry Smith     /* copy over the B part */
968ec8511deSBarry Smith     Aloc = (Mat_SeqAIJ*)aij->B->data;
969*899cda47SBarry Smith     m    = aij->B->rmap.n;  ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
970*899cda47SBarry Smith     row  = mat->rmap.rstart;
971b1d57f15SBarry Smith     ierr = PetscMalloc((ai[m]+1)*sizeof(PetscInt),&cols);CHKERRQ(ierr);
972b0a32e0cSBarry Smith     ct   = cols;
973bfec09a0SHong Zhang     for (i=0; i<ai[m]; i++) {cols[i] = aij->garray[aj[i]];}
97495373324SBarry Smith     for (i=0; i<m; i++) {
975416022c9SBarry Smith       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);CHKERRQ(ierr);
97695373324SBarry Smith       row++; a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
97795373324SBarry Smith     }
978606d414cSSatish Balay     ierr = PetscFree(ct);CHKERRQ(ierr);
9796d4a8577SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9806d4a8577SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
98155843e3eSBarry Smith     /*
98255843e3eSBarry Smith        Everyone has to call to draw the matrix since the graphics waits are
983b0a32e0cSBarry Smith        synchronized across all processors that share the PetscDraw object
98455843e3eSBarry Smith     */
985b0a32e0cSBarry Smith     ierr = PetscViewerGetSingleton(viewer,&sviewer);CHKERRQ(ierr);
986e03a110bSBarry Smith     if (!rank) {
987e36acaf3SBarry Smith       ierr = PetscObjectSetName((PetscObject)((Mat_MPIAIJ*)(A->data))->A,mat->name);CHKERRQ(ierr);
9886831982aSBarry Smith       ierr = MatView(((Mat_MPIAIJ*)(A->data))->A,sviewer);CHKERRQ(ierr);
98995373324SBarry Smith     }
990b0a32e0cSBarry Smith     ierr = PetscViewerRestoreSingleton(viewer,&sviewer);CHKERRQ(ierr);
99178b31e54SBarry Smith     ierr = MatDestroy(A);CHKERRQ(ierr);
99295373324SBarry Smith   }
9933a40ed3dSBarry Smith   PetscFunctionReturn(0);
9941eb62cbbSBarry Smith }
9951eb62cbbSBarry Smith 
9964a2ae208SSatish Balay #undef __FUNCT__
9974a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ"
998dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ(Mat mat,PetscViewer viewer)
999416022c9SBarry Smith {
1000dfbe8321SBarry Smith   PetscErrorCode ierr;
100132077d6dSBarry Smith   PetscTruth     iascii,isdraw,issocket,isbinary;
1002416022c9SBarry Smith 
10033a40ed3dSBarry Smith   PetscFunctionBegin;
100432077d6dSBarry Smith   ierr  = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
1005fb9695e5SSatish Balay   ierr  = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
1006fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
1007b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_SOCKET,&issocket);CHKERRQ(ierr);
100832077d6dSBarry Smith   if (iascii || isdraw || isbinary || issocket) {
10097b2a1423SBarry Smith     ierr = MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer);CHKERRQ(ierr);
10105cd90555SBarry Smith   } else {
101179a5c55eSBarry Smith     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported by MPIAIJ matrices",((PetscObject)viewer)->type_name);
1012416022c9SBarry Smith   }
10133a40ed3dSBarry Smith   PetscFunctionReturn(0);
1014416022c9SBarry Smith }
1015416022c9SBarry Smith 
10161eb62cbbSBarry Smith 
1017c14dc6b6SHong Zhang 
10184a2ae208SSatish Balay #undef __FUNCT__
10194a2ae208SSatish Balay #define __FUNCT__ "MatRelax_MPIAIJ"
1020b1d57f15SBarry Smith PetscErrorCode MatRelax_MPIAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
10218a729477SBarry Smith {
102244a69424SLois Curfman McInnes   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)matin->data;
1023dfbe8321SBarry Smith   PetscErrorCode ierr;
1024c14dc6b6SHong Zhang   Vec            bb1;
10258a729477SBarry Smith 
10263a40ed3dSBarry Smith   PetscFunctionBegin;
102777431f27SBarry Smith   if (its <= 0 || lits <= 0) SETERRQ2(PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D and local its %D both positive",its,lits);
1028c14dc6b6SHong Zhang 
1029c14dc6b6SHong Zhang   ierr = VecDuplicate(bb,&bb1);CHKERRQ(ierr);
10302798e883SHong Zhang 
1031c16cb8f2SBarry Smith   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){
1032da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
1033bd3bf7d3SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,lits,xx);CHKERRQ(ierr);
10342798e883SHong Zhang       its--;
1035da3a660dSBarry Smith     }
10362798e883SHong Zhang 
10372798e883SHong Zhang     while (its--) {
10383a40ed3dSBarry Smith       ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10393a40ed3dSBarry Smith       ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10402798e883SHong Zhang 
1041c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
1042efb30889SBarry Smith       ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr);
1043c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
10442798e883SHong Zhang 
1045c14dc6b6SHong Zhang       /* local sweep */
1046bd3bf7d3SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,lits,xx);
1047c14dc6b6SHong Zhang       CHKERRQ(ierr);
10482798e883SHong Zhang     }
10493a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_FORWARD_SWEEP){
1050da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
1051c14dc6b6SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,PETSC_NULL,xx);CHKERRQ(ierr);
10522798e883SHong Zhang       its--;
1053da3a660dSBarry Smith     }
10542798e883SHong Zhang     while (its--) {
10553a40ed3dSBarry Smith       ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10563a40ed3dSBarry Smith       ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10572798e883SHong Zhang 
1058c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
1059efb30889SBarry Smith       ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr);
1060c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
1061c14dc6b6SHong Zhang 
1062c14dc6b6SHong Zhang       /* local sweep */
1063a6c309e7SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,PETSC_NULL,xx);
1064c14dc6b6SHong Zhang       CHKERRQ(ierr);
10652798e883SHong Zhang     }
10663a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_BACKWARD_SWEEP){
1067da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
1068c14dc6b6SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,PETSC_NULL,xx);CHKERRQ(ierr);
10692798e883SHong Zhang       its--;
1070da3a660dSBarry Smith     }
10712798e883SHong Zhang     while (its--) {
10722e8a6d31SBarry Smith       ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10732e8a6d31SBarry Smith       ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
10742798e883SHong Zhang 
1075c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
1076efb30889SBarry Smith       ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr);
1077c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
10782798e883SHong Zhang 
1079c14dc6b6SHong Zhang       /* local sweep */
1080a6c309e7SHong Zhang       ierr = (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,PETSC_NULL,xx);
1081c14dc6b6SHong Zhang       CHKERRQ(ierr);
10822798e883SHong Zhang     }
10833a40ed3dSBarry Smith   } else {
108429bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"Parallel SOR not supported");
1085c16cb8f2SBarry Smith   }
1086c14dc6b6SHong Zhang 
1087c14dc6b6SHong Zhang   ierr = VecDestroy(bb1);CHKERRQ(ierr);
10883a40ed3dSBarry Smith   PetscFunctionReturn(0);
10898a729477SBarry Smith }
1090a66be287SLois Curfman McInnes 
10914a2ae208SSatish Balay #undef __FUNCT__
109242e855d1Svictor #define __FUNCT__ "MatPermute_MPIAIJ"
109342e855d1Svictor PetscErrorCode MatPermute_MPIAIJ(Mat A,IS rowp,IS colp,Mat *B)
109442e855d1Svictor {
109542e855d1Svictor   MPI_Comm       comm,pcomm;
109642e855d1Svictor   PetscInt       first,local_size,nrows,*rows;
109742e855d1Svictor   int            ntids;
109842e855d1Svictor   IS             crowp,growp,irowp,lrowp,lcolp,icolp;
109942e855d1Svictor   PetscErrorCode ierr;
110042e855d1Svictor 
110142e855d1Svictor   PetscFunctionBegin;
110242e855d1Svictor   ierr = PetscObjectGetComm((PetscObject)A,&comm); CHKERRQ(ierr);
110342e855d1Svictor   /* make a collective version of 'rowp' */
110442e855d1Svictor   ierr = PetscObjectGetComm((PetscObject)rowp,&pcomm); CHKERRQ(ierr);
110542e855d1Svictor   if (pcomm==comm) {
110642e855d1Svictor     crowp = rowp;
110742e855d1Svictor   } else {
110842e855d1Svictor     ierr = ISGetSize(rowp,&nrows); CHKERRQ(ierr);
110942e855d1Svictor     ierr = ISGetIndices(rowp,&rows); CHKERRQ(ierr);
111042e855d1Svictor     ierr = ISCreateGeneral(comm,nrows,rows,&crowp); CHKERRQ(ierr);
111142e855d1Svictor     ierr = ISRestoreIndices(rowp,&rows); CHKERRQ(ierr);
111242e855d1Svictor   }
111342e855d1Svictor   /* collect the global row permutation and invert it */
111442e855d1Svictor   ierr = ISAllGather(crowp,&growp); CHKERRQ(ierr);
111542e855d1Svictor   ierr = ISSetPermutation(growp); CHKERRQ(ierr);
111642e855d1Svictor   if (pcomm!=comm) {
111742e855d1Svictor     ierr = ISDestroy(crowp); CHKERRQ(ierr);
111842e855d1Svictor   }
111942e855d1Svictor   ierr = ISInvertPermutation(growp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
112042e855d1Svictor   /* get the local target indices */
112142e855d1Svictor   ierr = MatGetOwnershipRange(A,&first,PETSC_NULL); CHKERRQ(ierr);
112242e855d1Svictor   ierr = MatGetLocalSize(A,&local_size,PETSC_NULL); CHKERRQ(ierr);
112342e855d1Svictor   ierr = ISGetIndices(irowp,&rows); CHKERRQ(ierr);
112442e855d1Svictor   ierr = ISCreateGeneral(MPI_COMM_SELF,local_size,rows+first,&lrowp); CHKERRQ(ierr);
112542e855d1Svictor   ierr = ISRestoreIndices(irowp,&rows); CHKERRQ(ierr);
112642e855d1Svictor   ierr = ISDestroy(irowp); CHKERRQ(ierr);
112742e855d1Svictor   /* the column permutation is so much easier;
112842e855d1Svictor      make a local version of 'colp' and invert it */
112942e855d1Svictor   ierr = PetscObjectGetComm((PetscObject)colp,&pcomm); CHKERRQ(ierr);
113042e855d1Svictor   ierr = MPI_Comm_size(pcomm,&ntids); CHKERRQ(ierr);
113142e855d1Svictor   if (ntids==1) {
113242e855d1Svictor     lcolp = colp;
113342e855d1Svictor   } else {
113442e855d1Svictor     ierr = ISGetSize(colp,&nrows); CHKERRQ(ierr);
113542e855d1Svictor     ierr = ISGetIndices(colp,&rows); CHKERRQ(ierr);
113642e855d1Svictor     ierr = ISCreateGeneral(MPI_COMM_SELF,nrows,rows,&lcolp); CHKERRQ(ierr);
113742e855d1Svictor   }
113842e855d1Svictor   ierr = ISInvertPermutation(lcolp,PETSC_DECIDE,&icolp); CHKERRQ(ierr);
113942e855d1Svictor   ierr = ISSetPermutation(lcolp); CHKERRQ(ierr);
114042e855d1Svictor   if (ntids>1) {
114142e855d1Svictor     ierr = ISRestoreIndices(colp,&rows); CHKERRQ(ierr);
114242e855d1Svictor     ierr = ISDestroy(lcolp); CHKERRQ(ierr);
114342e855d1Svictor   }
114442e855d1Svictor   /* now we just get the submatrix */
114542e855d1Svictor   ierr = MatGetSubMatrix(A,lrowp,icolp,local_size,MAT_INITIAL_MATRIX,B); CHKERRQ(ierr);
114642e855d1Svictor   /* clean up */
114742e855d1Svictor   ierr = ISDestroy(lrowp); CHKERRQ(ierr);
114842e855d1Svictor   ierr = ISDestroy(icolp); CHKERRQ(ierr);
114942e855d1Svictor   PetscFunctionReturn(0);
115042e855d1Svictor }
115142e855d1Svictor 
115242e855d1Svictor #undef __FUNCT__
11534a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_MPIAIJ"
1154dfbe8321SBarry Smith PetscErrorCode MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info)
1155a66be287SLois Curfman McInnes {
1156a66be287SLois Curfman McInnes   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)matin->data;
1157a66be287SLois Curfman McInnes   Mat            A = mat->A,B = mat->B;
1158dfbe8321SBarry Smith   PetscErrorCode ierr;
1159329f5518SBarry Smith   PetscReal      isend[5],irecv[5];
1160a66be287SLois Curfman McInnes 
11613a40ed3dSBarry Smith   PetscFunctionBegin;
11624e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
11634e220ebcSLois Curfman McInnes   ierr = MatGetInfo(A,MAT_LOCAL,info);CHKERRQ(ierr);
11644e220ebcSLois Curfman McInnes   isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
11654e220ebcSLois Curfman McInnes   isend[3] = info->memory;  isend[4] = info->mallocs;
11664e220ebcSLois Curfman McInnes   ierr = MatGetInfo(B,MAT_LOCAL,info);CHKERRQ(ierr);
11674e220ebcSLois Curfman McInnes   isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
11684e220ebcSLois Curfman McInnes   isend[3] += info->memory;  isend[4] += info->mallocs;
1169a66be287SLois Curfman McInnes   if (flag == MAT_LOCAL) {
11704e220ebcSLois Curfman McInnes     info->nz_used      = isend[0];
11714e220ebcSLois Curfman McInnes     info->nz_allocated = isend[1];
11724e220ebcSLois Curfman McInnes     info->nz_unneeded  = isend[2];
11734e220ebcSLois Curfman McInnes     info->memory       = isend[3];
11744e220ebcSLois Curfman McInnes     info->mallocs      = isend[4];
1175a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_MAX) {
1176d7d1e502SBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_MAX,matin->comm);CHKERRQ(ierr);
11774e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
11784e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
11794e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
11804e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
11814e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1182a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_SUM) {
1183d7d1e502SBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_SUM,matin->comm);CHKERRQ(ierr);
11844e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
11854e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
11864e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
11874e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
11884e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1189a66be287SLois Curfman McInnes   }
11904e220ebcSLois Curfman McInnes   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
11914e220ebcSLois Curfman McInnes   info->fill_ratio_needed = 0;
11924e220ebcSLois Curfman McInnes   info->factor_mallocs    = 0;
1193*899cda47SBarry Smith   info->rows_global       = (double)matin->rmap.N;
1194*899cda47SBarry Smith   info->columns_global    = (double)matin->cmap.N;
1195*899cda47SBarry Smith   info->rows_local        = (double)matin->rmap.n;
1196*899cda47SBarry Smith   info->columns_local     = (double)matin->cmap.N;
11974e220ebcSLois Curfman McInnes 
11983a40ed3dSBarry Smith   PetscFunctionReturn(0);
1199a66be287SLois Curfman McInnes }
1200a66be287SLois Curfman McInnes 
12014a2ae208SSatish Balay #undef __FUNCT__
12024a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_MPIAIJ"
1203dfbe8321SBarry Smith PetscErrorCode MatSetOption_MPIAIJ(Mat A,MatOption op)
1204c74985f6SBarry Smith {
1205c0bbcb79SLois Curfman McInnes   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
1206dfbe8321SBarry Smith   PetscErrorCode ierr;
1207c74985f6SBarry Smith 
12083a40ed3dSBarry Smith   PetscFunctionBegin;
120912c028f9SKris Buschelman   switch (op) {
121012c028f9SKris Buschelman   case MAT_NO_NEW_NONZERO_LOCATIONS:
121112c028f9SKris Buschelman   case MAT_YES_NEW_NONZERO_LOCATIONS:
121212c028f9SKris Buschelman   case MAT_COLUMNS_UNSORTED:
121312c028f9SKris Buschelman   case MAT_COLUMNS_SORTED:
121412c028f9SKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
121512c028f9SKris Buschelman   case MAT_KEEP_ZEROED_ROWS:
121612c028f9SKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
121712c028f9SKris Buschelman   case MAT_USE_INODES:
121812c028f9SKris Buschelman   case MAT_DO_NOT_USE_INODES:
121912c028f9SKris Buschelman   case MAT_IGNORE_ZERO_ENTRIES:
12201dee9f54SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
12211dee9f54SBarry Smith     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
122212c028f9SKris Buschelman     break;
122312c028f9SKris Buschelman   case MAT_ROW_ORIENTED:
12247c922b88SBarry Smith     a->roworiented = PETSC_TRUE;
12251dee9f54SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
12261dee9f54SBarry Smith     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
122712c028f9SKris Buschelman     break;
122812c028f9SKris Buschelman   case MAT_ROWS_SORTED:
122912c028f9SKris Buschelman   case MAT_ROWS_UNSORTED:
123012c028f9SKris Buschelman   case MAT_YES_NEW_DIAGONALS:
1231ae15b995SBarry Smith     ierr = PetscInfo(A,"Option ignored\n");CHKERRQ(ierr);
123212c028f9SKris Buschelman     break;
123312c028f9SKris Buschelman   case MAT_COLUMN_ORIENTED:
12347c922b88SBarry Smith     a->roworiented = PETSC_FALSE;
12351dee9f54SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
12361dee9f54SBarry Smith     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
123712c028f9SKris Buschelman     break;
123812c028f9SKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
12397c922b88SBarry Smith     a->donotstash = PETSC_TRUE;
124012c028f9SKris Buschelman     break;
124112c028f9SKris Buschelman   case MAT_NO_NEW_DIAGONALS:
124229bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"MAT_NO_NEW_DIAGONALS");
124377e54ba9SKris Buschelman   case MAT_SYMMETRIC:
124477e54ba9SKris Buschelman   case MAT_STRUCTURALLY_SYMMETRIC:
1245bf108f30SBarry Smith   case MAT_HERMITIAN:
1246bf108f30SBarry Smith   case MAT_SYMMETRY_ETERNAL:
1247bf108f30SBarry Smith     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
1248bf108f30SBarry Smith     break;
12499a4540c5SBarry Smith   case MAT_NOT_SYMMETRIC:
12509a4540c5SBarry Smith   case MAT_NOT_STRUCTURALLY_SYMMETRIC:
12519a4540c5SBarry Smith   case MAT_NOT_HERMITIAN:
12529a4540c5SBarry Smith   case MAT_NOT_SYMMETRY_ETERNAL:
125377e54ba9SKris Buschelman     break;
125412c028f9SKris Buschelman   default:
125529bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"unknown option");
12563a40ed3dSBarry Smith   }
12573a40ed3dSBarry Smith   PetscFunctionReturn(0);
1258c74985f6SBarry Smith }
1259c74985f6SBarry Smith 
12604a2ae208SSatish Balay #undef __FUNCT__
12614a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_MPIAIJ"
1262b1d57f15SBarry Smith PetscErrorCode MatGetRow_MPIAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
126339e00950SLois Curfman McInnes {
1264154123eaSLois Curfman McInnes   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)matin->data;
126587828ca2SBarry Smith   PetscScalar    *vworkA,*vworkB,**pvA,**pvB,*v_p;
12666849ba73SBarry Smith   PetscErrorCode ierr;
1267*899cda47SBarry Smith   PetscInt       i,*cworkA,*cworkB,**pcA,**pcB,cstart = matin->cmap.rstart;
1268*899cda47SBarry Smith   PetscInt       nztot,nzA,nzB,lrow,rstart = matin->rmap.rstart,rend = matin->rmap.rend;
1269b1d57f15SBarry Smith   PetscInt       *cmap,*idx_p;
127039e00950SLois Curfman McInnes 
12713a40ed3dSBarry Smith   PetscFunctionBegin;
1272abc0a331SBarry Smith   if (mat->getrowactive) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Already active");
12737a0afa10SBarry Smith   mat->getrowactive = PETSC_TRUE;
12747a0afa10SBarry Smith 
127570f0671dSBarry Smith   if (!mat->rowvalues && (idx || v)) {
12767a0afa10SBarry Smith     /*
12777a0afa10SBarry Smith         allocate enough space to hold information from the longest row.
12787a0afa10SBarry Smith     */
12797a0afa10SBarry Smith     Mat_SeqAIJ *Aa = (Mat_SeqAIJ*)mat->A->data,*Ba = (Mat_SeqAIJ*)mat->B->data;
1280b1d57f15SBarry Smith     PetscInt     max = 1,tmp;
1281*899cda47SBarry Smith     for (i=0; i<matin->rmap.n; i++) {
12827a0afa10SBarry Smith       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
12837a0afa10SBarry Smith       if (max < tmp) { max = tmp; }
12847a0afa10SBarry Smith     }
1285b1d57f15SBarry Smith     ierr = PetscMalloc(max*(sizeof(PetscInt)+sizeof(PetscScalar)),&mat->rowvalues);CHKERRQ(ierr);
1286b1d57f15SBarry Smith     mat->rowindices = (PetscInt*)(mat->rowvalues + max);
12877a0afa10SBarry Smith   }
12887a0afa10SBarry Smith 
128929bbc08cSBarry Smith   if (row < rstart || row >= rend) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Only local rows")
1290abc0e9e4SLois Curfman McInnes   lrow = row - rstart;
129139e00950SLois Curfman McInnes 
1292154123eaSLois Curfman McInnes   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1293154123eaSLois Curfman McInnes   if (!v)   {pvA = 0; pvB = 0;}
1294154123eaSLois Curfman McInnes   if (!idx) {pcA = 0; if (!v) pcB = 0;}
1295f830108cSBarry Smith   ierr = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1296f830108cSBarry Smith   ierr = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
1297154123eaSLois Curfman McInnes   nztot = nzA + nzB;
1298154123eaSLois Curfman McInnes 
129970f0671dSBarry Smith   cmap  = mat->garray;
1300154123eaSLois Curfman McInnes   if (v  || idx) {
1301154123eaSLois Curfman McInnes     if (nztot) {
1302154123eaSLois Curfman McInnes       /* Sort by increasing column numbers, assuming A and B already sorted */
1303b1d57f15SBarry Smith       PetscInt imark = -1;
1304154123eaSLois Curfman McInnes       if (v) {
130570f0671dSBarry Smith         *v = v_p = mat->rowvalues;
130639e00950SLois Curfman McInnes         for (i=0; i<nzB; i++) {
130770f0671dSBarry Smith           if (cmap[cworkB[i]] < cstart)   v_p[i] = vworkB[i];
1308154123eaSLois Curfman McInnes           else break;
1309154123eaSLois Curfman McInnes         }
1310154123eaSLois Curfman McInnes         imark = i;
131170f0671dSBarry Smith         for (i=0; i<nzA; i++)     v_p[imark+i] = vworkA[i];
131270f0671dSBarry Smith         for (i=imark; i<nzB; i++) v_p[nzA+i]   = vworkB[i];
1313154123eaSLois Curfman McInnes       }
1314154123eaSLois Curfman McInnes       if (idx) {
131570f0671dSBarry Smith         *idx = idx_p = mat->rowindices;
131670f0671dSBarry Smith         if (imark > -1) {
131770f0671dSBarry Smith           for (i=0; i<imark; i++) {
131870f0671dSBarry Smith             idx_p[i] = cmap[cworkB[i]];
131970f0671dSBarry Smith           }
132070f0671dSBarry Smith         } else {
1321154123eaSLois Curfman McInnes           for (i=0; i<nzB; i++) {
132270f0671dSBarry Smith             if (cmap[cworkB[i]] < cstart)   idx_p[i] = cmap[cworkB[i]];
1323154123eaSLois Curfman McInnes             else break;
1324154123eaSLois Curfman McInnes           }
1325154123eaSLois Curfman McInnes           imark = i;
132670f0671dSBarry Smith         }
132770f0671dSBarry Smith         for (i=0; i<nzA; i++)     idx_p[imark+i] = cstart + cworkA[i];
132870f0671dSBarry Smith         for (i=imark; i<nzB; i++) idx_p[nzA+i]   = cmap[cworkB[i]];
132939e00950SLois Curfman McInnes       }
13303f97c4b0SBarry Smith     } else {
13311ca473b0SSatish Balay       if (idx) *idx = 0;
13321ca473b0SSatish Balay       if (v)   *v   = 0;
13331ca473b0SSatish Balay     }
1334154123eaSLois Curfman McInnes   }
133539e00950SLois Curfman McInnes   *nz = nztot;
1336f830108cSBarry Smith   ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1337f830108cSBarry Smith   ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
13383a40ed3dSBarry Smith   PetscFunctionReturn(0);
133939e00950SLois Curfman McInnes }
134039e00950SLois Curfman McInnes 
13414a2ae208SSatish Balay #undef __FUNCT__
13424a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_MPIAIJ"
1343b1d57f15SBarry Smith PetscErrorCode MatRestoreRow_MPIAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
134439e00950SLois Curfman McInnes {
13457a0afa10SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
13463a40ed3dSBarry Smith 
13473a40ed3dSBarry Smith   PetscFunctionBegin;
1348abc0a331SBarry Smith   if (!aij->getrowactive) {
1349abc0a331SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"MatGetRow() must be called first");
13507a0afa10SBarry Smith   }
13517a0afa10SBarry Smith   aij->getrowactive = PETSC_FALSE;
13523a40ed3dSBarry Smith   PetscFunctionReturn(0);
135339e00950SLois Curfman McInnes }
135439e00950SLois Curfman McInnes 
13554a2ae208SSatish Balay #undef __FUNCT__
13564a2ae208SSatish Balay #define __FUNCT__ "MatNorm_MPIAIJ"
1357dfbe8321SBarry Smith PetscErrorCode MatNorm_MPIAIJ(Mat mat,NormType type,PetscReal *norm)
1358855ac2c5SLois Curfman McInnes {
1359855ac2c5SLois Curfman McInnes   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
1360ec8511deSBarry Smith   Mat_SeqAIJ     *amat = (Mat_SeqAIJ*)aij->A->data,*bmat = (Mat_SeqAIJ*)aij->B->data;
1361dfbe8321SBarry Smith   PetscErrorCode ierr;
1362*899cda47SBarry Smith   PetscInt       i,j,cstart = mat->cmap.rstart;
1363329f5518SBarry Smith   PetscReal      sum = 0.0;
136487828ca2SBarry Smith   PetscScalar    *v;
136504ca555eSLois Curfman McInnes 
13663a40ed3dSBarry Smith   PetscFunctionBegin;
136717699dbbSLois Curfman McInnes   if (aij->size == 1) {
136814183eadSLois Curfman McInnes     ierr =  MatNorm(aij->A,type,norm);CHKERRQ(ierr);
136937fa93a5SLois Curfman McInnes   } else {
137004ca555eSLois Curfman McInnes     if (type == NORM_FROBENIUS) {
137104ca555eSLois Curfman McInnes       v = amat->a;
137204ca555eSLois Curfman McInnes       for (i=0; i<amat->nz; i++) {
1373aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
1374329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
137504ca555eSLois Curfman McInnes #else
137604ca555eSLois Curfman McInnes         sum += (*v)*(*v); v++;
137704ca555eSLois Curfman McInnes #endif
137804ca555eSLois Curfman McInnes       }
137904ca555eSLois Curfman McInnes       v = bmat->a;
138004ca555eSLois Curfman McInnes       for (i=0; i<bmat->nz; i++) {
1381aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
1382329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
138304ca555eSLois Curfman McInnes #else
138404ca555eSLois Curfman McInnes         sum += (*v)*(*v); v++;
138504ca555eSLois Curfman McInnes #endif
138604ca555eSLois Curfman McInnes       }
1387d7d1e502SBarry Smith       ierr = MPI_Allreduce(&sum,norm,1,MPIU_REAL,MPI_SUM,mat->comm);CHKERRQ(ierr);
138804ca555eSLois Curfman McInnes       *norm = sqrt(*norm);
13893a40ed3dSBarry Smith     } else if (type == NORM_1) { /* max column norm */
1390329f5518SBarry Smith       PetscReal *tmp,*tmp2;
1391b1d57f15SBarry Smith       PetscInt    *jj,*garray = aij->garray;
1392*899cda47SBarry Smith       ierr = PetscMalloc((mat->cmap.N+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1393*899cda47SBarry Smith       ierr = PetscMalloc((mat->cmap.N+1)*sizeof(PetscReal),&tmp2);CHKERRQ(ierr);
1394*899cda47SBarry Smith       ierr = PetscMemzero(tmp,mat->cmap.N*sizeof(PetscReal));CHKERRQ(ierr);
139504ca555eSLois Curfman McInnes       *norm = 0.0;
139604ca555eSLois Curfman McInnes       v = amat->a; jj = amat->j;
139704ca555eSLois Curfman McInnes       for (j=0; j<amat->nz; j++) {
1398bfec09a0SHong Zhang         tmp[cstart + *jj++ ] += PetscAbsScalar(*v);  v++;
139904ca555eSLois Curfman McInnes       }
140004ca555eSLois Curfman McInnes       v = bmat->a; jj = bmat->j;
140104ca555eSLois Curfman McInnes       for (j=0; j<bmat->nz; j++) {
1402bfec09a0SHong Zhang         tmp[garray[*jj++]] += PetscAbsScalar(*v); v++;
140304ca555eSLois Curfman McInnes       }
1404*899cda47SBarry Smith       ierr = MPI_Allreduce(tmp,tmp2,mat->cmap.N,MPIU_REAL,MPI_SUM,mat->comm);CHKERRQ(ierr);
1405*899cda47SBarry Smith       for (j=0; j<mat->cmap.N; j++) {
140604ca555eSLois Curfman McInnes         if (tmp2[j] > *norm) *norm = tmp2[j];
140704ca555eSLois Curfman McInnes       }
1408606d414cSSatish Balay       ierr = PetscFree(tmp);CHKERRQ(ierr);
1409606d414cSSatish Balay       ierr = PetscFree(tmp2);CHKERRQ(ierr);
14103a40ed3dSBarry Smith     } else if (type == NORM_INFINITY) { /* max row norm */
1411329f5518SBarry Smith       PetscReal ntemp = 0.0;
1412*899cda47SBarry Smith       for (j=0; j<aij->A->rmap.n; j++) {
1413bfec09a0SHong Zhang         v = amat->a + amat->i[j];
141404ca555eSLois Curfman McInnes         sum = 0.0;
141504ca555eSLois Curfman McInnes         for (i=0; i<amat->i[j+1]-amat->i[j]; i++) {
1416cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
141704ca555eSLois Curfman McInnes         }
1418bfec09a0SHong Zhang         v = bmat->a + bmat->i[j];
141904ca555eSLois Curfman McInnes         for (i=0; i<bmat->i[j+1]-bmat->i[j]; i++) {
1420cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
142104ca555eSLois Curfman McInnes         }
1422515d9167SLois Curfman McInnes         if (sum > ntemp) ntemp = sum;
142304ca555eSLois Curfman McInnes       }
1424d7d1e502SBarry Smith       ierr = MPI_Allreduce(&ntemp,norm,1,MPIU_REAL,MPI_MAX,mat->comm);CHKERRQ(ierr);
1425ca161407SBarry Smith     } else {
142629bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"No support for two norm");
142704ca555eSLois Curfman McInnes     }
142837fa93a5SLois Curfman McInnes   }
14293a40ed3dSBarry Smith   PetscFunctionReturn(0);
1430855ac2c5SLois Curfman McInnes }
1431855ac2c5SLois Curfman McInnes 
14324a2ae208SSatish Balay #undef __FUNCT__
14334a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_MPIAIJ"
1434dfbe8321SBarry Smith PetscErrorCode MatTranspose_MPIAIJ(Mat A,Mat *matout)
1435b7c46309SBarry Smith {
1436b7c46309SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
1437dbb450caSBarry Smith   Mat_SeqAIJ     *Aloc = (Mat_SeqAIJ*)a->A->data;
1438dfbe8321SBarry Smith   PetscErrorCode ierr;
1439*899cda47SBarry Smith   PetscInt       M = A->rmap.N,N = A->cmap.N,m,*ai,*aj,row,*cols,i,*ct;
14403a40ed3dSBarry Smith   Mat            B;
144187828ca2SBarry Smith   PetscScalar    *array;
1442b7c46309SBarry Smith 
14433a40ed3dSBarry Smith   PetscFunctionBegin;
14447c922b88SBarry Smith   if (!matout && M != N) {
144529bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1446d4bb536fSBarry Smith   }
1447d4bb536fSBarry Smith 
1448f69a0ea3SMatthew Knepley   ierr = MatCreate(A->comm,&B);CHKERRQ(ierr);
1449*899cda47SBarry Smith   ierr = MatSetSizes(B,A->cmap.n,A->rmap.n,N,M);CHKERRQ(ierr);
1450f204ca49SKris Buschelman   ierr = MatSetType(B,A->type_name);CHKERRQ(ierr);
1451f204ca49SKris Buschelman   ierr = MatMPIAIJSetPreallocation(B,0,PETSC_NULL,0,PETSC_NULL);CHKERRQ(ierr);
1452b7c46309SBarry Smith 
1453b7c46309SBarry Smith   /* copy over the A part */
1454ec8511deSBarry Smith   Aloc = (Mat_SeqAIJ*)a->A->data;
1455*899cda47SBarry Smith   m = a->A->rmap.n; ai = Aloc->i; aj = Aloc->j; array = Aloc->a;
1456*899cda47SBarry Smith   row = A->rmap.rstart;
1457*899cda47SBarry Smith   for (i=0; i<ai[m]; i++) {aj[i] += A->cmap.rstart ;}
1458b7c46309SBarry Smith   for (i=0; i<m; i++) {
1459416022c9SBarry Smith     ierr = MatSetValues(B,ai[i+1]-ai[i],aj,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
1460b7c46309SBarry Smith     row++; array += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
1461b7c46309SBarry Smith   }
1462b7c46309SBarry Smith   aj = Aloc->j;
1463*899cda47SBarry Smith   for (i=0; i<ai[m]; i++) {aj[i] -= A->cmap.rstart ;}
1464b7c46309SBarry Smith 
1465b7c46309SBarry Smith   /* copy over the B part */
1466ec8511deSBarry Smith   Aloc = (Mat_SeqAIJ*)a->B->data;
1467*899cda47SBarry Smith   m = a->B->rmap.n;  ai = Aloc->i; aj = Aloc->j; array = Aloc->a;
1468*899cda47SBarry Smith   row  = A->rmap.rstart;
1469b1d57f15SBarry Smith   ierr = PetscMalloc((1+ai[m])*sizeof(PetscInt),&cols);CHKERRQ(ierr);
1470b0a32e0cSBarry Smith   ct   = cols;
1471bfec09a0SHong Zhang   for (i=0; i<ai[m]; i++) {cols[i] = a->garray[aj[i]];}
1472b7c46309SBarry Smith   for (i=0; i<m; i++) {
1473416022c9SBarry Smith     ierr = MatSetValues(B,ai[i+1]-ai[i],cols,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
1474b7c46309SBarry Smith     row++; array += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
1475b7c46309SBarry Smith   }
1476606d414cSSatish Balay   ierr = PetscFree(ct);CHKERRQ(ierr);
14776d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14786d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14797c922b88SBarry Smith   if (matout) {
14800de55854SLois Curfman McInnes     *matout = B;
14810de55854SLois Curfman McInnes   } else {
1482273d9f13SBarry Smith     ierr = MatHeaderCopy(A,B);CHKERRQ(ierr);
14830de55854SLois Curfman McInnes   }
14843a40ed3dSBarry Smith   PetscFunctionReturn(0);
1485b7c46309SBarry Smith }
1486b7c46309SBarry Smith 
14874a2ae208SSatish Balay #undef __FUNCT__
14884a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_MPIAIJ"
1489dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr)
1490a008b906SSatish Balay {
14914b967eb1SSatish Balay   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
14924b967eb1SSatish Balay   Mat            a = aij->A,b = aij->B;
1493dfbe8321SBarry Smith   PetscErrorCode ierr;
1494b1d57f15SBarry Smith   PetscInt       s1,s2,s3;
1495a008b906SSatish Balay 
14963a40ed3dSBarry Smith   PetscFunctionBegin;
14974b967eb1SSatish Balay   ierr = MatGetLocalSize(mat,&s2,&s3);CHKERRQ(ierr);
14984b967eb1SSatish Balay   if (rr) {
1499e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr);
150029bbc08cSBarry Smith     if (s1!=s3) SETERRQ(PETSC_ERR_ARG_SIZ,"right vector non-conforming local size");
15014b967eb1SSatish Balay     /* Overlap communication with computation. */
150243a90d84SBarry Smith     ierr = VecScatterBegin(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx);CHKERRQ(ierr);
1503a008b906SSatish Balay   }
15044b967eb1SSatish Balay   if (ll) {
1505e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr);
150629bbc08cSBarry Smith     if (s1!=s2) SETERRQ(PETSC_ERR_ARG_SIZ,"left vector non-conforming local size");
1507f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,ll,0);CHKERRQ(ierr);
15084b967eb1SSatish Balay   }
15094b967eb1SSatish Balay   /* scale  the diagonal block */
1510f830108cSBarry Smith   ierr = (*a->ops->diagonalscale)(a,ll,rr);CHKERRQ(ierr);
15114b967eb1SSatish Balay 
15124b967eb1SSatish Balay   if (rr) {
15134b967eb1SSatish Balay     /* Do a scatter end and then right scale the off-diagonal block */
151443a90d84SBarry Smith     ierr = VecScatterEnd(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx);CHKERRQ(ierr);
1515f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,0,aij->lvec);CHKERRQ(ierr);
15164b967eb1SSatish Balay   }
15174b967eb1SSatish Balay 
15183a40ed3dSBarry Smith   PetscFunctionReturn(0);
1519a008b906SSatish Balay }
1520a008b906SSatish Balay 
1521a008b906SSatish Balay 
15224a2ae208SSatish Balay #undef __FUNCT__
15234a2ae208SSatish Balay #define __FUNCT__ "MatPrintHelp_MPIAIJ"
1524dfbe8321SBarry Smith PetscErrorCode MatPrintHelp_MPIAIJ(Mat A)
1525682d7d0cSBarry Smith {
1526682d7d0cSBarry Smith   Mat_MPIAIJ     *a   = (Mat_MPIAIJ*)A->data;
1527dfbe8321SBarry Smith   PetscErrorCode ierr;
1528682d7d0cSBarry Smith 
15293a40ed3dSBarry Smith   PetscFunctionBegin;
15303a40ed3dSBarry Smith   if (!a->rank) {
15313a40ed3dSBarry Smith     ierr = MatPrintHelp_SeqAIJ(a->A);CHKERRQ(ierr);
15323a40ed3dSBarry Smith   }
15333a40ed3dSBarry Smith   PetscFunctionReturn(0);
1534682d7d0cSBarry Smith }
1535682d7d0cSBarry Smith 
15364a2ae208SSatish Balay #undef __FUNCT__
1537521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_MPIAIJ"
1538521d7252SBarry Smith PetscErrorCode MatSetBlockSize_MPIAIJ(Mat A,PetscInt bs)
15395a838052SSatish Balay {
1540521d7252SBarry Smith   Mat_MPIAIJ     *a   = (Mat_MPIAIJ*)A->data;
1541521d7252SBarry Smith   PetscErrorCode ierr;
1542521d7252SBarry Smith 
15433a40ed3dSBarry Smith   PetscFunctionBegin;
1544521d7252SBarry Smith   ierr = MatSetBlockSize(a->A,bs);CHKERRQ(ierr);
1545521d7252SBarry Smith   ierr = MatSetBlockSize(a->B,bs);CHKERRQ(ierr);
15463a40ed3dSBarry Smith   PetscFunctionReturn(0);
15475a838052SSatish Balay }
15484a2ae208SSatish Balay #undef __FUNCT__
15494a2ae208SSatish Balay #define __FUNCT__ "MatSetUnfactored_MPIAIJ"
1550dfbe8321SBarry Smith PetscErrorCode MatSetUnfactored_MPIAIJ(Mat A)
1551bb5a7306SBarry Smith {
1552bb5a7306SBarry Smith   Mat_MPIAIJ     *a   = (Mat_MPIAIJ*)A->data;
1553dfbe8321SBarry Smith   PetscErrorCode ierr;
15543a40ed3dSBarry Smith 
15553a40ed3dSBarry Smith   PetscFunctionBegin;
1556bb5a7306SBarry Smith   ierr = MatSetUnfactored(a->A);CHKERRQ(ierr);
15573a40ed3dSBarry Smith   PetscFunctionReturn(0);
1558bb5a7306SBarry Smith }
1559bb5a7306SBarry Smith 
15604a2ae208SSatish Balay #undef __FUNCT__
15614a2ae208SSatish Balay #define __FUNCT__ "MatEqual_MPIAIJ"
1562dfbe8321SBarry Smith PetscErrorCode MatEqual_MPIAIJ(Mat A,Mat B,PetscTruth *flag)
1563d4bb536fSBarry Smith {
1564d4bb536fSBarry Smith   Mat_MPIAIJ     *matB = (Mat_MPIAIJ*)B->data,*matA = (Mat_MPIAIJ*)A->data;
1565d4bb536fSBarry Smith   Mat            a,b,c,d;
1566d4bb536fSBarry Smith   PetscTruth     flg;
1567dfbe8321SBarry Smith   PetscErrorCode ierr;
1568d4bb536fSBarry Smith 
15693a40ed3dSBarry Smith   PetscFunctionBegin;
1570d4bb536fSBarry Smith   a = matA->A; b = matA->B;
1571d4bb536fSBarry Smith   c = matB->A; d = matB->B;
1572d4bb536fSBarry Smith 
1573d4bb536fSBarry Smith   ierr = MatEqual(a,c,&flg);CHKERRQ(ierr);
1574abc0a331SBarry Smith   if (flg) {
1575d4bb536fSBarry Smith     ierr = MatEqual(b,d,&flg);CHKERRQ(ierr);
1576d4bb536fSBarry Smith   }
1577ca161407SBarry Smith   ierr = MPI_Allreduce(&flg,flag,1,MPI_INT,MPI_LAND,A->comm);CHKERRQ(ierr);
15783a40ed3dSBarry Smith   PetscFunctionReturn(0);
1579d4bb536fSBarry Smith }
1580d4bb536fSBarry Smith 
15814a2ae208SSatish Balay #undef __FUNCT__
15824a2ae208SSatish Balay #define __FUNCT__ "MatCopy_MPIAIJ"
1583dfbe8321SBarry Smith PetscErrorCode MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str)
1584cb5b572fSBarry Smith {
1585dfbe8321SBarry Smith   PetscErrorCode ierr;
1586cb5b572fSBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ *)A->data;
1587cb5b572fSBarry Smith   Mat_MPIAIJ     *b = (Mat_MPIAIJ *)B->data;
1588cb5b572fSBarry Smith 
1589cb5b572fSBarry Smith   PetscFunctionBegin;
159033f4a19fSKris Buschelman   /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
159133f4a19fSKris Buschelman   if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) {
1592cb5b572fSBarry Smith     /* because of the column compression in the off-processor part of the matrix a->B,
1593cb5b572fSBarry Smith        the number of columns in a->B and b->B may be different, hence we cannot call
1594cb5b572fSBarry Smith        the MatCopy() directly on the two parts. If need be, we can provide a more
1595cb5b572fSBarry Smith        efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices
1596cb5b572fSBarry Smith        then copying the submatrices */
1597cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1598cb5b572fSBarry Smith   } else {
1599cb5b572fSBarry Smith     ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr);
1600cb5b572fSBarry Smith     ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr);
1601cb5b572fSBarry Smith   }
1602cb5b572fSBarry Smith   PetscFunctionReturn(0);
1603cb5b572fSBarry Smith }
1604cb5b572fSBarry Smith 
16054a2ae208SSatish Balay #undef __FUNCT__
16064a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_MPIAIJ"
1607dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_MPIAIJ(Mat A)
1608273d9f13SBarry Smith {
1609dfbe8321SBarry Smith   PetscErrorCode ierr;
1610273d9f13SBarry Smith 
1611273d9f13SBarry Smith   PetscFunctionBegin;
1612273d9f13SBarry Smith   ierr =  MatMPIAIJSetPreallocation(A,PETSC_DEFAULT,0,PETSC_DEFAULT,0);CHKERRQ(ierr);
1613273d9f13SBarry Smith   PetscFunctionReturn(0);
1614273d9f13SBarry Smith }
1615273d9f13SBarry Smith 
1616ac90fabeSBarry Smith #include "petscblaslapack.h"
1617ac90fabeSBarry Smith #undef __FUNCT__
1618ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_MPIAIJ"
1619f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_MPIAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
1620ac90fabeSBarry Smith {
1621dfbe8321SBarry Smith   PetscErrorCode ierr;
1622b1d57f15SBarry Smith   PetscInt       i;
1623ac90fabeSBarry Smith   Mat_MPIAIJ     *xx = (Mat_MPIAIJ *)X->data,*yy = (Mat_MPIAIJ *)Y->data;
16244ce68768SBarry Smith   PetscBLASInt   bnz,one=1;
1625ac90fabeSBarry Smith   Mat_SeqAIJ     *x,*y;
1626ac90fabeSBarry Smith 
1627ac90fabeSBarry Smith   PetscFunctionBegin;
1628ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
1629f4df32b1SMatthew Knepley     PetscScalar alpha = a;
1630ac90fabeSBarry Smith     x = (Mat_SeqAIJ *)xx->A->data;
1631ac90fabeSBarry Smith     y = (Mat_SeqAIJ *)yy->A->data;
16324ce68768SBarry Smith     bnz = (PetscBLASInt)x->nz;
1633f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
1634ac90fabeSBarry Smith     x = (Mat_SeqAIJ *)xx->B->data;
1635ac90fabeSBarry Smith     y = (Mat_SeqAIJ *)yy->B->data;
16364ce68768SBarry Smith     bnz = (PetscBLASInt)x->nz;
1637f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
1638a30b2313SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) {
1639f4df32b1SMatthew Knepley     ierr = MatAXPY_SeqAIJ(yy->A,a,xx->A,str);CHKERRQ(ierr);
1640c537a176SHong Zhang 
1641c537a176SHong Zhang     x = (Mat_SeqAIJ *)xx->B->data;
1642a30b2313SHong Zhang     y = (Mat_SeqAIJ *)yy->B->data;
1643a30b2313SHong Zhang     if (y->xtoy && y->XtoY != xx->B) {
1644a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
1645a30b2313SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
1646c537a176SHong Zhang     }
1647a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
1648*899cda47SBarry Smith       ierr = MatAXPYGetxtoy_Private(xx->B->rmap.n,x->i,x->j,xx->garray,y->i,y->j,yy->garray,&y->xtoy);CHKERRQ(ierr);
1649a30b2313SHong Zhang       y->XtoY = xx->B;
1650c537a176SHong Zhang     }
1651f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
1652ac90fabeSBarry Smith   } else {
1653f4df32b1SMatthew Knepley     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
1654ac90fabeSBarry Smith   }
1655ac90fabeSBarry Smith   PetscFunctionReturn(0);
1656ac90fabeSBarry Smith }
1657ac90fabeSBarry Smith 
1658354c94deSBarry Smith EXTERN PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_SeqAIJ(Mat);
1659354c94deSBarry Smith 
1660354c94deSBarry Smith #undef __FUNCT__
1661354c94deSBarry Smith #define __FUNCT__ "MatConjugate_MPIAIJ"
1662354c94deSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_MPIAIJ(Mat mat)
1663354c94deSBarry Smith {
1664354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
1665354c94deSBarry Smith   PetscErrorCode ierr;
1666354c94deSBarry Smith   Mat_MPIAIJ     *aij = (Mat_MPIAIJ *)mat->data;
1667354c94deSBarry Smith 
1668354c94deSBarry Smith   PetscFunctionBegin;
1669354c94deSBarry Smith   ierr = MatConjugate_SeqAIJ(aij->A);CHKERRQ(ierr);
1670354c94deSBarry Smith   ierr = MatConjugate_SeqAIJ(aij->B);CHKERRQ(ierr);
1671354c94deSBarry Smith #else
1672354c94deSBarry Smith   PetscFunctionBegin;
1673354c94deSBarry Smith #endif
1674354c94deSBarry Smith   PetscFunctionReturn(0);
1675354c94deSBarry Smith }
1676354c94deSBarry Smith 
167799cafbc1SBarry Smith #undef __FUNCT__
167899cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_MPIAIJ"
167999cafbc1SBarry Smith PetscErrorCode MatRealPart_MPIAIJ(Mat A)
168099cafbc1SBarry Smith {
168199cafbc1SBarry Smith   Mat_MPIAIJ   *a = (Mat_MPIAIJ*)A->data;
168299cafbc1SBarry Smith   PetscErrorCode ierr;
168399cafbc1SBarry Smith 
168499cafbc1SBarry Smith   PetscFunctionBegin;
168599cafbc1SBarry Smith   ierr = MatRealPart(a->A);CHKERRQ(ierr);
168699cafbc1SBarry Smith   ierr = MatRealPart(a->B);CHKERRQ(ierr);
168799cafbc1SBarry Smith   PetscFunctionReturn(0);
168899cafbc1SBarry Smith }
168999cafbc1SBarry Smith 
169099cafbc1SBarry Smith #undef __FUNCT__
169199cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_MPIAIJ"
169299cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_MPIAIJ(Mat A)
169399cafbc1SBarry Smith {
169499cafbc1SBarry Smith   Mat_MPIAIJ   *a = (Mat_MPIAIJ*)A->data;
169599cafbc1SBarry Smith   PetscErrorCode ierr;
169699cafbc1SBarry Smith 
169799cafbc1SBarry Smith   PetscFunctionBegin;
169899cafbc1SBarry Smith   ierr = MatImaginaryPart(a->A);CHKERRQ(ierr);
169999cafbc1SBarry Smith   ierr = MatImaginaryPart(a->B);CHKERRQ(ierr);
170099cafbc1SBarry Smith   PetscFunctionReturn(0);
170199cafbc1SBarry Smith }
170299cafbc1SBarry Smith 
17038a729477SBarry Smith /* -------------------------------------------------------------------*/
1704cda55fadSBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ,
1705cda55fadSBarry Smith        MatGetRow_MPIAIJ,
1706cda55fadSBarry Smith        MatRestoreRow_MPIAIJ,
1707cda55fadSBarry Smith        MatMult_MPIAIJ,
170897304618SKris Buschelman /* 4*/ MatMultAdd_MPIAIJ,
17097c922b88SBarry Smith        MatMultTranspose_MPIAIJ,
17107c922b88SBarry Smith        MatMultTransposeAdd_MPIAIJ,
1711cda55fadSBarry Smith        0,
1712cda55fadSBarry Smith        0,
1713cda55fadSBarry Smith        0,
171497304618SKris Buschelman /*10*/ 0,
1715cda55fadSBarry Smith        0,
1716cda55fadSBarry Smith        0,
171744a69424SLois Curfman McInnes        MatRelax_MPIAIJ,
1718b7c46309SBarry Smith        MatTranspose_MPIAIJ,
171997304618SKris Buschelman /*15*/ MatGetInfo_MPIAIJ,
1720cda55fadSBarry Smith        MatEqual_MPIAIJ,
1721cda55fadSBarry Smith        MatGetDiagonal_MPIAIJ,
1722cda55fadSBarry Smith        MatDiagonalScale_MPIAIJ,
1723cda55fadSBarry Smith        MatNorm_MPIAIJ,
172497304618SKris Buschelman /*20*/ MatAssemblyBegin_MPIAIJ,
1725cda55fadSBarry Smith        MatAssemblyEnd_MPIAIJ,
17261eb62cbbSBarry Smith        0,
1727cda55fadSBarry Smith        MatSetOption_MPIAIJ,
1728cda55fadSBarry Smith        MatZeroEntries_MPIAIJ,
172997304618SKris Buschelman /*25*/ MatZeroRows_MPIAIJ,
1730cda55fadSBarry Smith        0,
1731cda55fadSBarry Smith        0,
1732cda55fadSBarry Smith        0,
1733cda55fadSBarry Smith        0,
173497304618SKris Buschelman /*30*/ MatSetUpPreallocation_MPIAIJ,
1735cda55fadSBarry Smith        0,
1736cda55fadSBarry Smith        0,
1737cda55fadSBarry Smith        0,
1738cda55fadSBarry Smith        0,
173997304618SKris Buschelman /*35*/ MatDuplicate_MPIAIJ,
1740cda55fadSBarry Smith        0,
1741cda55fadSBarry Smith        0,
1742cda55fadSBarry Smith        0,
1743cda55fadSBarry Smith        0,
174497304618SKris Buschelman /*40*/ MatAXPY_MPIAIJ,
1745cda55fadSBarry Smith        MatGetSubMatrices_MPIAIJ,
1746cda55fadSBarry Smith        MatIncreaseOverlap_MPIAIJ,
1747cda55fadSBarry Smith        MatGetValues_MPIAIJ,
1748cb5b572fSBarry Smith        MatCopy_MPIAIJ,
174997304618SKris Buschelman /*45*/ MatPrintHelp_MPIAIJ,
1750cda55fadSBarry Smith        MatScale_MPIAIJ,
1751cda55fadSBarry Smith        0,
1752cda55fadSBarry Smith        0,
1753cda55fadSBarry Smith        0,
1754521d7252SBarry Smith /*50*/ MatSetBlockSize_MPIAIJ,
1755cda55fadSBarry Smith        0,
1756cda55fadSBarry Smith        0,
1757cda55fadSBarry Smith        0,
1758cda55fadSBarry Smith        0,
175997304618SKris Buschelman /*55*/ MatFDColoringCreate_MPIAIJ,
1760cda55fadSBarry Smith        0,
1761cda55fadSBarry Smith        MatSetUnfactored_MPIAIJ,
176242e855d1Svictor        MatPermute_MPIAIJ,
1763cda55fadSBarry Smith        0,
176497304618SKris Buschelman /*60*/ MatGetSubMatrix_MPIAIJ,
1765e03a110bSBarry Smith        MatDestroy_MPIAIJ,
1766e03a110bSBarry Smith        MatView_MPIAIJ,
1767357abbc8SBarry Smith        0,
1768a2243be0SBarry Smith        0,
176997304618SKris Buschelman /*65*/ 0,
1770a2243be0SBarry Smith        0,
1771a2243be0SBarry Smith        0,
1772a2243be0SBarry Smith        0,
1773a2243be0SBarry Smith        0,
177497304618SKris Buschelman /*70*/ 0,
1775a2243be0SBarry Smith        0,
1776a2243be0SBarry Smith        MatSetColoring_MPIAIJ,
1777dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
1778779c1a83SBarry Smith        MatSetValuesAdic_MPIAIJ,
1779dcf5cc72SBarry Smith #else
1780dcf5cc72SBarry Smith        0,
1781dcf5cc72SBarry Smith #endif
178297304618SKris Buschelman        MatSetValuesAdifor_MPIAIJ,
178397304618SKris Buschelman /*75*/ 0,
178497304618SKris Buschelman        0,
178597304618SKris Buschelman        0,
178697304618SKris Buschelman        0,
178797304618SKris Buschelman        0,
178897304618SKris Buschelman /*80*/ 0,
178997304618SKris Buschelman        0,
179097304618SKris Buschelman        0,
179197304618SKris Buschelman        0,
179241acf15aSKris Buschelman /*84*/ MatLoad_MPIAIJ,
17936284ec50SHong Zhang        0,
17946284ec50SHong Zhang        0,
17956284ec50SHong Zhang        0,
17966284ec50SHong Zhang        0,
1797865e5f61SKris Buschelman        0,
1798865e5f61SKris Buschelman /*90*/ MatMatMult_MPIAIJ_MPIAIJ,
179926be0446SHong Zhang        MatMatMultSymbolic_MPIAIJ_MPIAIJ,
180026be0446SHong Zhang        MatMatMultNumeric_MPIAIJ_MPIAIJ,
18017a7894deSKris Buschelman        MatPtAP_Basic,
18027a7894deSKris Buschelman        MatPtAPSymbolic_MPIAIJ,
18037a7894deSKris Buschelman /*95*/ MatPtAPNumeric_MPIAIJ,
18047a7894deSKris Buschelman        0,
18057a7894deSKris Buschelman        0,
18067a7894deSKris Buschelman        0,
18077a7894deSKris Buschelman        0,
18087a7894deSKris Buschelman /*100*/0,
1809865e5f61SKris Buschelman        MatPtAPSymbolic_MPIAIJ_MPIAIJ,
18107a7894deSKris Buschelman        MatPtAPNumeric_MPIAIJ_MPIAIJ,
18112fd7e33dSBarry Smith        MatConjugate_MPIAIJ,
18122fd7e33dSBarry Smith        0,
181399cafbc1SBarry Smith /*105*/MatSetValuesRow_MPIAIJ,
181499cafbc1SBarry Smith        MatRealPart_MPIAIJ,
181599cafbc1SBarry Smith        MatImaginaryPart_MPIAIJ};
181636ce4990SBarry Smith 
18172e8a6d31SBarry Smith /* ----------------------------------------------------------------------------------------*/
18182e8a6d31SBarry Smith 
1819fb2e594dSBarry Smith EXTERN_C_BEGIN
18204a2ae208SSatish Balay #undef __FUNCT__
18214a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_MPIAIJ"
1822be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_MPIAIJ(Mat mat)
18232e8a6d31SBarry Smith {
18242e8a6d31SBarry Smith   Mat_MPIAIJ     *aij = (Mat_MPIAIJ *)mat->data;
1825dfbe8321SBarry Smith   PetscErrorCode ierr;
18262e8a6d31SBarry Smith 
18272e8a6d31SBarry Smith   PetscFunctionBegin;
18282e8a6d31SBarry Smith   ierr = MatStoreValues(aij->A);CHKERRQ(ierr);
18292e8a6d31SBarry Smith   ierr = MatStoreValues(aij->B);CHKERRQ(ierr);
18302e8a6d31SBarry Smith   PetscFunctionReturn(0);
18312e8a6d31SBarry Smith }
1832fb2e594dSBarry Smith EXTERN_C_END
18332e8a6d31SBarry Smith 
1834fb2e594dSBarry Smith EXTERN_C_BEGIN
18354a2ae208SSatish Balay #undef __FUNCT__
18364a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_MPIAIJ"
1837be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_MPIAIJ(Mat mat)
18382e8a6d31SBarry Smith {
18392e8a6d31SBarry Smith   Mat_MPIAIJ     *aij = (Mat_MPIAIJ *)mat->data;
1840dfbe8321SBarry Smith   PetscErrorCode ierr;
18412e8a6d31SBarry Smith 
18422e8a6d31SBarry Smith   PetscFunctionBegin;
18432e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr);
18442e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr);
18452e8a6d31SBarry Smith   PetscFunctionReturn(0);
18462e8a6d31SBarry Smith }
1847fb2e594dSBarry Smith EXTERN_C_END
18488a729477SBarry Smith 
1849e090d566SSatish Balay #include "petscpc.h"
185027508adbSBarry Smith EXTERN_C_BEGIN
18514a2ae208SSatish Balay #undef __FUNCT__
1852a23d5eceSKris Buschelman #define __FUNCT__ "MatMPIAIJSetPreallocation_MPIAIJ"
1853be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJSetPreallocation_MPIAIJ(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
1854a23d5eceSKris Buschelman {
1855a23d5eceSKris Buschelman   Mat_MPIAIJ     *b;
1856dfbe8321SBarry Smith   PetscErrorCode ierr;
1857b1d57f15SBarry Smith   PetscInt       i;
1858a23d5eceSKris Buschelman 
1859a23d5eceSKris Buschelman   PetscFunctionBegin;
1860a23d5eceSKris Buschelman   B->preallocated = PETSC_TRUE;
1861a23d5eceSKris Buschelman   if (d_nz == PETSC_DEFAULT || d_nz == PETSC_DECIDE) d_nz = 5;
1862a23d5eceSKris Buschelman   if (o_nz == PETSC_DEFAULT || o_nz == PETSC_DECIDE) o_nz = 2;
186377431f27SBarry Smith   if (d_nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"d_nz cannot be less than 0: value %D",d_nz);
186477431f27SBarry Smith   if (o_nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"o_nz cannot be less than 0: value %D",o_nz);
1865*899cda47SBarry Smith 
1866*899cda47SBarry Smith   B->rmap.bs = B->cmap.bs = 1;
1867*899cda47SBarry Smith   ierr = PetscMapInitialize(B->comm,&B->rmap);CHKERRQ(ierr);
1868*899cda47SBarry Smith   ierr = PetscMapInitialize(B->comm,&B->cmap);CHKERRQ(ierr);
1869a23d5eceSKris Buschelman   if (d_nnz) {
1870*899cda47SBarry Smith     for (i=0; i<B->rmap.n; i++) {
187177431f27SBarry 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]);
1872a23d5eceSKris Buschelman     }
1873a23d5eceSKris Buschelman   }
1874a23d5eceSKris Buschelman   if (o_nnz) {
1875*899cda47SBarry Smith     for (i=0; i<B->rmap.n; i++) {
187677431f27SBarry 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]);
1877a23d5eceSKris Buschelman     }
1878a23d5eceSKris Buschelman   }
1879a23d5eceSKris Buschelman   b = (Mat_MPIAIJ*)B->data;
1880*899cda47SBarry Smith 
1881*899cda47SBarry Smith   /* Explicitly create 2 MATSEQAIJ matrices. */
1882*899cda47SBarry Smith   ierr = MatCreate(PETSC_COMM_SELF,&b->A);CHKERRQ(ierr);
1883*899cda47SBarry Smith   ierr = MatSetSizes(b->A,B->rmap.n,B->cmap.n,B->rmap.n,B->cmap.n);CHKERRQ(ierr);
1884*899cda47SBarry Smith   ierr = MatSetType(b->A,MATSEQAIJ);CHKERRQ(ierr);
1885*899cda47SBarry Smith   ierr = PetscLogObjectParent(B,b->A);CHKERRQ(ierr);
1886*899cda47SBarry Smith   ierr = MatCreate(PETSC_COMM_SELF,&b->B);CHKERRQ(ierr);
1887*899cda47SBarry Smith   ierr = MatSetSizes(b->B,B->rmap.n,B->cmap.N,B->rmap.n,B->cmap.N);CHKERRQ(ierr);
1888*899cda47SBarry Smith   ierr = MatSetType(b->B,MATSEQAIJ);CHKERRQ(ierr);
1889*899cda47SBarry Smith   ierr = PetscLogObjectParent(B,b->B);CHKERRQ(ierr);
1890*899cda47SBarry Smith 
1891c60e587dSKris Buschelman   ierr = MatSeqAIJSetPreallocation(b->A,d_nz,d_nnz);CHKERRQ(ierr);
1892c60e587dSKris Buschelman   ierr = MatSeqAIJSetPreallocation(b->B,o_nz,o_nnz);CHKERRQ(ierr);
1893a23d5eceSKris Buschelman 
1894a23d5eceSKris Buschelman   PetscFunctionReturn(0);
1895a23d5eceSKris Buschelman }
1896a23d5eceSKris Buschelman EXTERN_C_END
1897a23d5eceSKris Buschelman 
18984a2ae208SSatish Balay #undef __FUNCT__
18994a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_MPIAIJ"
1900dfbe8321SBarry Smith PetscErrorCode MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
1901d6dfbf8fSBarry Smith {
1902d6dfbf8fSBarry Smith   Mat            mat;
1903416022c9SBarry Smith   Mat_MPIAIJ     *a,*oldmat = (Mat_MPIAIJ*)matin->data;
1904dfbe8321SBarry Smith   PetscErrorCode ierr;
1905d6dfbf8fSBarry Smith 
19063a40ed3dSBarry Smith   PetscFunctionBegin;
1907416022c9SBarry Smith   *newmat       = 0;
1908f69a0ea3SMatthew Knepley   ierr = MatCreate(matin->comm,&mat);CHKERRQ(ierr);
1909*899cda47SBarry Smith   ierr = MatSetSizes(mat,matin->rmap.n,matin->cmap.n,matin->rmap.N,matin->cmap.N);CHKERRQ(ierr);
1910be5d1d56SKris Buschelman   ierr = MatSetType(mat,matin->type_name);CHKERRQ(ierr);
19111d5dac46SHong Zhang   ierr = PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
1912273d9f13SBarry Smith   a    = (Mat_MPIAIJ*)mat->data;
1913e1b6402fSHong Zhang 
1914d6dfbf8fSBarry Smith   mat->factor       = matin->factor;
1915*899cda47SBarry Smith   mat->rmap.bs      = matin->rmap.bs;
1916c456f294SBarry Smith   mat->assembled    = PETSC_TRUE;
1917e7641de0SSatish Balay   mat->insertmode   = NOT_SET_VALUES;
1918273d9f13SBarry Smith   mat->preallocated = PETSC_TRUE;
1919d6dfbf8fSBarry Smith 
192017699dbbSLois Curfman McInnes   a->size           = oldmat->size;
192117699dbbSLois Curfman McInnes   a->rank           = oldmat->rank;
1922e7641de0SSatish Balay   a->donotstash     = oldmat->donotstash;
1923e7641de0SSatish Balay   a->roworiented    = oldmat->roworiented;
1924e7641de0SSatish Balay   a->rowindices     = 0;
1925bcd2baecSBarry Smith   a->rowvalues      = 0;
1926bcd2baecSBarry Smith   a->getrowactive   = PETSC_FALSE;
1927d6dfbf8fSBarry Smith 
1928*899cda47SBarry Smith   ierr = PetscMapCopy(mat->comm,&matin->rmap,&mat->rmap);CHKERRQ(ierr);
1929*899cda47SBarry Smith   ierr = PetscMapCopy(mat->comm,&matin->cmap,&mat->cmap);CHKERRQ(ierr);
1930*899cda47SBarry Smith 
19318798bf22SSatish Balay   ierr = MatStashCreate_Private(matin->comm,1,&mat->stash);CHKERRQ(ierr);
19322ee70a88SLois Curfman McInnes   if (oldmat->colmap) {
1933aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
19340f5bd95cSBarry Smith     ierr = PetscTableCreateCopy(oldmat->colmap,&a->colmap);CHKERRQ(ierr);
1935b1fc9764SSatish Balay #else
1936*899cda47SBarry Smith     ierr = PetscMalloc((mat->cmap.N)*sizeof(PetscInt),&a->colmap);CHKERRQ(ierr);
1937*899cda47SBarry Smith     ierr = PetscLogObjectMemory(mat,(mat->cmap.N)*sizeof(PetscInt));CHKERRQ(ierr);
1938*899cda47SBarry Smith     ierr = PetscMemcpy(a->colmap,oldmat->colmap,(mat->cmap.N)*sizeof(PetscInt));CHKERRQ(ierr);
1939b1fc9764SSatish Balay #endif
1940416022c9SBarry Smith   } else a->colmap = 0;
19413f41c07dSBarry Smith   if (oldmat->garray) {
1942b1d57f15SBarry Smith     PetscInt len;
1943*899cda47SBarry Smith     len  = oldmat->B->cmap.n;
1944b1d57f15SBarry Smith     ierr = PetscMalloc((len+1)*sizeof(PetscInt),&a->garray);CHKERRQ(ierr);
194552e6d16bSBarry Smith     ierr = PetscLogObjectMemory(mat,len*sizeof(PetscInt));CHKERRQ(ierr);
1946b1d57f15SBarry Smith     if (len) { ierr = PetscMemcpy(a->garray,oldmat->garray,len*sizeof(PetscInt));CHKERRQ(ierr); }
1947416022c9SBarry Smith   } else a->garray = 0;
1948d6dfbf8fSBarry Smith 
1949416022c9SBarry Smith   ierr = VecDuplicate(oldmat->lvec,&a->lvec);CHKERRQ(ierr);
195052e6d16bSBarry Smith   ierr = PetscLogObjectParent(mat,a->lvec);CHKERRQ(ierr);
1951a56f8943SBarry Smith   ierr = VecScatterCopy(oldmat->Mvctx,&a->Mvctx);CHKERRQ(ierr);
195252e6d16bSBarry Smith   ierr = PetscLogObjectParent(mat,a->Mvctx);CHKERRQ(ierr);
19532e8a6d31SBarry Smith   ierr = MatDuplicate(oldmat->A,cpvalues,&a->A);CHKERRQ(ierr);
195452e6d16bSBarry Smith   ierr = PetscLogObjectParent(mat,a->A);CHKERRQ(ierr);
19552e8a6d31SBarry Smith   ierr = MatDuplicate(oldmat->B,cpvalues,&a->B);CHKERRQ(ierr);
195652e6d16bSBarry Smith   ierr = PetscLogObjectParent(mat,a->B);CHKERRQ(ierr);
1957b0a32e0cSBarry Smith   ierr = PetscFListDuplicate(matin->qlist,&mat->qlist);CHKERRQ(ierr);
19588a729477SBarry Smith   *newmat = mat;
19593a40ed3dSBarry Smith   PetscFunctionReturn(0);
19608a729477SBarry Smith }
1961416022c9SBarry Smith 
1962e090d566SSatish Balay #include "petscsys.h"
1963416022c9SBarry Smith 
19644a2ae208SSatish Balay #undef __FUNCT__
19654a2ae208SSatish Balay #define __FUNCT__ "MatLoad_MPIAIJ"
1966f69a0ea3SMatthew Knepley PetscErrorCode MatLoad_MPIAIJ(PetscViewer viewer, MatType type,Mat *newmat)
1967416022c9SBarry Smith {
1968d65a2f8fSBarry Smith   Mat            A;
196987828ca2SBarry Smith   PetscScalar    *vals,*svals;
197019bcc07fSBarry Smith   MPI_Comm       comm = ((PetscObject)viewer)->comm;
1971416022c9SBarry Smith   MPI_Status     status;
19726849ba73SBarry Smith   PetscErrorCode ierr;
1973dc231df0SBarry Smith   PetscMPIInt    rank,size,tag = ((PetscObject)viewer)->tag,maxnz;
1974167e7480SBarry Smith   PetscInt       i,nz,j,rstart,rend,mmax;
1975b1d57f15SBarry Smith   PetscInt       header[4],*rowlengths = 0,M,N,m,*cols;
1976b1d57f15SBarry Smith   PetscInt       *ourlens,*procsnz = 0,*offlens,jj,*mycols,*smycols;
1977dc231df0SBarry Smith   PetscInt       cend,cstart,n,*rowners;
1978b1d57f15SBarry Smith   int            fd;
1979416022c9SBarry Smith 
19803a40ed3dSBarry Smith   PetscFunctionBegin;
19811dab6e02SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
19821dab6e02SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
198317699dbbSLois Curfman McInnes   if (!rank) {
1984b0a32e0cSBarry Smith     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
19850752156aSBarry Smith     ierr = PetscBinaryRead(fd,(char *)header,4,PETSC_INT);CHKERRQ(ierr);
1986552e946dSBarry Smith     if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
19876c5fab8fSBarry Smith   }
19886c5fab8fSBarry Smith 
1989b1d57f15SBarry Smith   ierr = MPI_Bcast(header+1,3,MPIU_INT,0,comm);CHKERRQ(ierr);
1990416022c9SBarry Smith   M = header[1]; N = header[2];
1991416022c9SBarry Smith   /* determine ownership of all rows */
199229cdbbc8SSatish Balay   m    = M/size + ((M % size) > rank);
1993dc231df0SBarry Smith   ierr = PetscMalloc((size+1)*sizeof(PetscInt),&rowners);CHKERRQ(ierr);
1994dc231df0SBarry Smith   ierr = MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);CHKERRQ(ierr);
1995167e7480SBarry Smith 
1996167e7480SBarry Smith   /* First process needs enough room for process with most rows */
1997167e7480SBarry Smith   if (!rank) {
1998167e7480SBarry Smith     mmax       = rowners[1];
1999167e7480SBarry Smith     for (i=2; i<size; i++) {
2000167e7480SBarry Smith       mmax = PetscMax(mmax,rowners[i]);
2001167e7480SBarry Smith     }
2002167e7480SBarry Smith   } else mmax = m;
2003167e7480SBarry Smith 
2004416022c9SBarry Smith   rowners[0] = 0;
200517699dbbSLois Curfman McInnes   for (i=2; i<=size; i++) {
2006167e7480SBarry Smith     mmax       = PetscMax(mmax,rowners[i]);
2007416022c9SBarry Smith     rowners[i] += rowners[i-1];
2008416022c9SBarry Smith   }
200917699dbbSLois Curfman McInnes   rstart = rowners[rank];
201017699dbbSLois Curfman McInnes   rend   = rowners[rank+1];
2011416022c9SBarry Smith 
2012416022c9SBarry Smith   /* distribute row lengths to all processors */
2013167e7480SBarry Smith   ierr    = PetscMalloc2(mmax,PetscInt,&ourlens,mmax,PetscInt,&offlens);CHKERRQ(ierr);
201417699dbbSLois Curfman McInnes   if (!rank) {
2015dc231df0SBarry Smith     ierr = PetscBinaryRead(fd,ourlens,m,PETSC_INT);CHKERRQ(ierr);
2016dc231df0SBarry Smith     ierr = PetscMalloc(m*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
2017b1d57f15SBarry Smith     ierr = PetscMalloc(size*sizeof(PetscInt),&procsnz);CHKERRQ(ierr);
2018b1d57f15SBarry Smith     ierr = PetscMemzero(procsnz,size*sizeof(PetscInt));CHKERRQ(ierr);
2019dc231df0SBarry Smith     for (j=0; j<m; j++) {
2020dc231df0SBarry Smith       procsnz[0] += ourlens[j];
2021dc231df0SBarry Smith     }
2022dc231df0SBarry Smith     for (i=1; i<size; i++) {
2023dc231df0SBarry Smith       ierr = PetscBinaryRead(fd,rowlengths,rowners[i+1]-rowners[i],PETSC_INT);CHKERRQ(ierr);
2024dc231df0SBarry Smith       /* calculate the number of nonzeros on each processor */
2025dc231df0SBarry Smith       for (j=0; j<rowners[i+1]-rowners[i]; j++) {
2026416022c9SBarry Smith         procsnz[i] += rowlengths[j];
2027416022c9SBarry Smith       }
2028dc231df0SBarry Smith       ierr = MPI_Send(rowlengths,rowners[i+1]-rowners[i],MPIU_INT,i,tag,comm);CHKERRQ(ierr);
2029416022c9SBarry Smith     }
2030606d414cSSatish Balay     ierr = PetscFree(rowlengths);CHKERRQ(ierr);
2031dc231df0SBarry Smith   } else {
2032dc231df0SBarry Smith     ierr = MPI_Recv(ourlens,m,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr);
2033dc231df0SBarry Smith   }
2034416022c9SBarry Smith 
2035dc231df0SBarry Smith   if (!rank) {
2036416022c9SBarry Smith     /* determine max buffer needed and allocate it */
2037416022c9SBarry Smith     maxnz = 0;
20388a8e0b3aSBarry Smith     for (i=0; i<size; i++) {
20390452661fSBarry Smith       maxnz = PetscMax(maxnz,procsnz[i]);
2040416022c9SBarry Smith     }
2041b1d57f15SBarry Smith     ierr = PetscMalloc(maxnz*sizeof(PetscInt),&cols);CHKERRQ(ierr);
2042416022c9SBarry Smith 
2043416022c9SBarry Smith     /* read in my part of the matrix column indices  */
2044416022c9SBarry Smith     nz   = procsnz[0];
2045b1d57f15SBarry Smith     ierr = PetscMalloc(nz*sizeof(PetscInt),&mycols);CHKERRQ(ierr);
20460752156aSBarry Smith     ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr);
2047d65a2f8fSBarry Smith 
2048d65a2f8fSBarry Smith     /* read in every one elses and ship off */
204917699dbbSLois Curfman McInnes     for (i=1; i<size; i++) {
2050d65a2f8fSBarry Smith       nz   = procsnz[i];
20510752156aSBarry Smith       ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr);
2052b1d57f15SBarry Smith       ierr = MPI_Send(cols,nz,MPIU_INT,i,tag,comm);CHKERRQ(ierr);
2053d65a2f8fSBarry Smith     }
2054606d414cSSatish Balay     ierr = PetscFree(cols);CHKERRQ(ierr);
20553a40ed3dSBarry Smith   } else {
2056416022c9SBarry Smith     /* determine buffer space needed for message */
2057416022c9SBarry Smith     nz = 0;
2058416022c9SBarry Smith     for (i=0; i<m; i++) {
2059416022c9SBarry Smith       nz += ourlens[i];
2060416022c9SBarry Smith     }
2061dc231df0SBarry Smith     ierr = PetscMalloc(nz*sizeof(PetscInt),&mycols);CHKERRQ(ierr);
2062416022c9SBarry Smith 
2063416022c9SBarry Smith     /* receive message of column indices*/
2064b1d57f15SBarry Smith     ierr = MPI_Recv(mycols,nz,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr);
2065b1d57f15SBarry Smith     ierr = MPI_Get_count(&status,MPIU_INT,&maxnz);CHKERRQ(ierr);
206629bbc08cSBarry Smith     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
2067416022c9SBarry Smith   }
2068416022c9SBarry Smith 
2069b362ba68SBarry Smith   /* determine column ownership if matrix is not square */
2070b362ba68SBarry Smith   if (N != M) {
2071b362ba68SBarry Smith     n      = N/size + ((N % size) > rank);
2072b1d57f15SBarry Smith     ierr   = MPI_Scan(&n,&cend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr);
2073b362ba68SBarry Smith     cstart = cend - n;
2074b362ba68SBarry Smith   } else {
2075b362ba68SBarry Smith     cstart = rstart;
2076b362ba68SBarry Smith     cend   = rend;
2077fb2e594dSBarry Smith     n      = cend - cstart;
2078b362ba68SBarry Smith   }
2079b362ba68SBarry Smith 
2080416022c9SBarry Smith   /* loop over local rows, determining number of off diagonal entries */
2081b1d57f15SBarry Smith   ierr = PetscMemzero(offlens,m*sizeof(PetscInt));CHKERRQ(ierr);
2082416022c9SBarry Smith   jj = 0;
2083416022c9SBarry Smith   for (i=0; i<m; i++) {
2084416022c9SBarry Smith     for (j=0; j<ourlens[i]; j++) {
2085b362ba68SBarry Smith       if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++;
2086416022c9SBarry Smith       jj++;
2087416022c9SBarry Smith     }
2088416022c9SBarry Smith   }
2089d65a2f8fSBarry Smith 
2090d65a2f8fSBarry Smith   /* create our matrix */
2091416022c9SBarry Smith   for (i=0; i<m; i++) {
2092416022c9SBarry Smith     ourlens[i] -= offlens[i];
2093416022c9SBarry Smith   }
2094f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,&A);CHKERRQ(ierr);
2095f69a0ea3SMatthew Knepley   ierr = MatSetSizes(A,m,n,M,N);CHKERRQ(ierr);
2096d10c748bSKris Buschelman   ierr = MatSetType(A,type);CHKERRQ(ierr);
2097d10c748bSKris Buschelman   ierr = MatMPIAIJSetPreallocation(A,0,ourlens,0,offlens);CHKERRQ(ierr);
2098d10c748bSKris Buschelman 
2099fb2e594dSBarry Smith   ierr = MatSetOption(A,MAT_COLUMNS_SORTED);CHKERRQ(ierr);
2100d65a2f8fSBarry Smith   for (i=0; i<m; i++) {
2101d65a2f8fSBarry Smith     ourlens[i] += offlens[i];
2102d65a2f8fSBarry Smith   }
2103416022c9SBarry Smith 
210417699dbbSLois Curfman McInnes   if (!rank) {
2105906b51c7SHong Zhang     ierr = PetscMalloc((maxnz+1)*sizeof(PetscScalar),&vals);CHKERRQ(ierr);
2106416022c9SBarry Smith 
2107416022c9SBarry Smith     /* read in my part of the matrix numerical values  */
2108416022c9SBarry Smith     nz   = procsnz[0];
21090752156aSBarry Smith     ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
2110d65a2f8fSBarry Smith 
2111d65a2f8fSBarry Smith     /* insert into matrix */
2112d65a2f8fSBarry Smith     jj      = rstart;
2113d65a2f8fSBarry Smith     smycols = mycols;
2114d65a2f8fSBarry Smith     svals   = vals;
2115d65a2f8fSBarry Smith     for (i=0; i<m; i++) {
2116dc231df0SBarry Smith       ierr = MatSetValues_MPIAIJ(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
2117d65a2f8fSBarry Smith       smycols += ourlens[i];
2118d65a2f8fSBarry Smith       svals   += ourlens[i];
2119d65a2f8fSBarry Smith       jj++;
2120416022c9SBarry Smith     }
2121416022c9SBarry Smith 
2122d65a2f8fSBarry Smith     /* read in other processors and ship out */
212317699dbbSLois Curfman McInnes     for (i=1; i<size; i++) {
2124416022c9SBarry Smith       nz   = procsnz[i];
21250752156aSBarry Smith       ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
2126ca161407SBarry Smith       ierr = MPI_Send(vals,nz,MPIU_SCALAR,i,A->tag,comm);CHKERRQ(ierr);
2127416022c9SBarry Smith     }
2128606d414cSSatish Balay     ierr = PetscFree(procsnz);CHKERRQ(ierr);
21293a40ed3dSBarry Smith   } else {
2130d65a2f8fSBarry Smith     /* receive numeric values */
213187828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&vals);CHKERRQ(ierr);
2132416022c9SBarry Smith 
2133d65a2f8fSBarry Smith     /* receive message of values*/
2134ca161407SBarry Smith     ierr = MPI_Recv(vals,nz,MPIU_SCALAR,0,A->tag,comm,&status);CHKERRQ(ierr);
2135ca161407SBarry Smith     ierr = MPI_Get_count(&status,MPIU_SCALAR,&maxnz);CHKERRQ(ierr);
213629bbc08cSBarry Smith     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
2137d65a2f8fSBarry Smith 
2138d65a2f8fSBarry Smith     /* insert into matrix */
2139d65a2f8fSBarry Smith     jj      = rstart;
2140d65a2f8fSBarry Smith     smycols = mycols;
2141d65a2f8fSBarry Smith     svals   = vals;
2142d65a2f8fSBarry Smith     for (i=0; i<m; i++) {
2143dc231df0SBarry Smith       ierr     = MatSetValues_MPIAIJ(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
2144d65a2f8fSBarry Smith       smycols += ourlens[i];
2145d65a2f8fSBarry Smith       svals   += ourlens[i];
2146d65a2f8fSBarry Smith       jj++;
2147d65a2f8fSBarry Smith     }
2148d65a2f8fSBarry Smith   }
2149dc231df0SBarry Smith   ierr = PetscFree2(ourlens,offlens);CHKERRQ(ierr);
2150606d414cSSatish Balay   ierr = PetscFree(vals);CHKERRQ(ierr);
2151606d414cSSatish Balay   ierr = PetscFree(mycols);CHKERRQ(ierr);
2152606d414cSSatish Balay   ierr = PetscFree(rowners);CHKERRQ(ierr);
2153d65a2f8fSBarry Smith 
21546d4a8577SBarry Smith   ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
21556d4a8577SBarry Smith   ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2156d10c748bSKris Buschelman   *newmat = A;
21573a40ed3dSBarry Smith   PetscFunctionReturn(0);
2158416022c9SBarry Smith }
2159a0ff6018SBarry Smith 
21604a2ae208SSatish Balay #undef __FUNCT__
21614a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_MPIAIJ"
2162a0ff6018SBarry Smith /*
216329da9460SBarry Smith     Not great since it makes two copies of the submatrix, first an SeqAIJ
216429da9460SBarry Smith   in local and then by concatenating the local matrices the end result.
216529da9460SBarry Smith   Writing it directly would be much like MatGetSubMatrices_MPIAIJ()
2166a0ff6018SBarry Smith */
2167b1d57f15SBarry Smith PetscErrorCode MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse call,Mat *newmat)
2168a0ff6018SBarry Smith {
2169dfbe8321SBarry Smith   PetscErrorCode ierr;
217032dcc486SBarry Smith   PetscMPIInt    rank,size;
2171b1d57f15SBarry Smith   PetscInt       i,m,n,rstart,row,rend,nz,*cwork,j;
2172b1d57f15SBarry Smith   PetscInt       *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal;
2173fee21e36SBarry Smith   Mat            *local,M,Mreuse;
217487828ca2SBarry Smith   PetscScalar    *vwork,*aa;
217500e6dbe6SBarry Smith   MPI_Comm       comm = mat->comm;
217600e6dbe6SBarry Smith   Mat_SeqAIJ     *aij;
21777e2c5f70SBarry Smith 
2178a0ff6018SBarry Smith 
2179a0ff6018SBarry Smith   PetscFunctionBegin;
21801dab6e02SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
21811dab6e02SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
218200e6dbe6SBarry Smith 
2183fee21e36SBarry Smith   if (call ==  MAT_REUSE_MATRIX) {
2184fee21e36SBarry Smith     ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);CHKERRQ(ierr);
2185e005ede5SBarry Smith     if (!Mreuse) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
2186fee21e36SBarry Smith     local = &Mreuse;
2187fee21e36SBarry Smith     ierr  = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&local);CHKERRQ(ierr);
2188fee21e36SBarry Smith   } else {
2189a0ff6018SBarry Smith     ierr   = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr);
2190fee21e36SBarry Smith     Mreuse = *local;
2191606d414cSSatish Balay     ierr   = PetscFree(local);CHKERRQ(ierr);
2192fee21e36SBarry Smith   }
2193a0ff6018SBarry Smith 
2194a0ff6018SBarry Smith   /*
2195a0ff6018SBarry Smith       m - number of local rows
2196a0ff6018SBarry Smith       n - number of columns (same on all processors)
2197a0ff6018SBarry Smith       rstart - first row in new global matrix generated
2198a0ff6018SBarry Smith   */
2199fee21e36SBarry Smith   ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr);
2200a0ff6018SBarry Smith   if (call == MAT_INITIAL_MATRIX) {
2201fee21e36SBarry Smith     aij = (Mat_SeqAIJ*)(Mreuse)->data;
220200e6dbe6SBarry Smith     ii  = aij->i;
220300e6dbe6SBarry Smith     jj  = aij->j;
220400e6dbe6SBarry Smith 
2205a0ff6018SBarry Smith     /*
220600e6dbe6SBarry Smith         Determine the number of non-zeros in the diagonal and off-diagonal
220700e6dbe6SBarry Smith         portions of the matrix in order to do correct preallocation
2208a0ff6018SBarry Smith     */
220900e6dbe6SBarry Smith 
221000e6dbe6SBarry Smith     /* first get start and end of "diagonal" columns */
22116a6a5d1dSBarry Smith     if (csize == PETSC_DECIDE) {
2212ab50ec6bSBarry Smith       ierr = ISGetSize(isrow,&mglobal);CHKERRQ(ierr);
2213ab50ec6bSBarry Smith       if (mglobal == n) { /* square matrix */
2214e2c4fddaSBarry Smith 	nlocal = m;
22156a6a5d1dSBarry Smith       } else {
2216ab50ec6bSBarry Smith         nlocal = n/size + ((n % size) > rank);
2217ab50ec6bSBarry Smith       }
2218ab50ec6bSBarry Smith     } else {
22196a6a5d1dSBarry Smith       nlocal = csize;
22206a6a5d1dSBarry Smith     }
2221b1d57f15SBarry Smith     ierr   = MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr);
222200e6dbe6SBarry Smith     rstart = rend - nlocal;
22236a6a5d1dSBarry Smith     if (rank == size - 1 && rend != n) {
222477431f27SBarry Smith       SETERRQ2(PETSC_ERR_ARG_SIZ,"Local column sizes %D do not add up to total number of columns %D",rend,n);
22256a6a5d1dSBarry Smith     }
222600e6dbe6SBarry Smith 
222700e6dbe6SBarry Smith     /* next, compute all the lengths */
2228b1d57f15SBarry Smith     ierr  = PetscMalloc((2*m+1)*sizeof(PetscInt),&dlens);CHKERRQ(ierr);
222900e6dbe6SBarry Smith     olens = dlens + m;
223000e6dbe6SBarry Smith     for (i=0; i<m; i++) {
223100e6dbe6SBarry Smith       jend = ii[i+1] - ii[i];
223200e6dbe6SBarry Smith       olen = 0;
223300e6dbe6SBarry Smith       dlen = 0;
223400e6dbe6SBarry Smith       for (j=0; j<jend; j++) {
223500e6dbe6SBarry Smith         if (*jj < rstart || *jj >= rend) olen++;
223600e6dbe6SBarry Smith         else dlen++;
223700e6dbe6SBarry Smith         jj++;
223800e6dbe6SBarry Smith       }
223900e6dbe6SBarry Smith       olens[i] = olen;
224000e6dbe6SBarry Smith       dlens[i] = dlen;
224100e6dbe6SBarry Smith     }
2242f69a0ea3SMatthew Knepley     ierr = MatCreate(comm,&M);CHKERRQ(ierr);
2243f69a0ea3SMatthew Knepley     ierr = MatSetSizes(M,m,nlocal,PETSC_DECIDE,n);CHKERRQ(ierr);
2244e2d9671bSKris Buschelman     ierr = MatSetType(M,mat->type_name);CHKERRQ(ierr);
2245e2d9671bSKris Buschelman     ierr = MatMPIAIJSetPreallocation(M,0,dlens,0,olens);CHKERRQ(ierr);
2246606d414cSSatish Balay     ierr = PetscFree(dlens);CHKERRQ(ierr);
2247a0ff6018SBarry Smith   } else {
2248b1d57f15SBarry Smith     PetscInt ml,nl;
2249a0ff6018SBarry Smith 
2250a0ff6018SBarry Smith     M = *newmat;
2251a0ff6018SBarry Smith     ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr);
225229bbc08cSBarry Smith     if (ml != m) SETERRQ(PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
2253a0ff6018SBarry Smith     ierr = MatZeroEntries(M);CHKERRQ(ierr);
2254c48de900SBarry Smith     /*
2255c48de900SBarry Smith          The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
2256c48de900SBarry Smith        rather than the slower MatSetValues().
2257c48de900SBarry Smith     */
2258c48de900SBarry Smith     M->was_assembled = PETSC_TRUE;
2259c48de900SBarry Smith     M->assembled     = PETSC_FALSE;
2260a0ff6018SBarry Smith   }
2261a0ff6018SBarry Smith   ierr = MatGetOwnershipRange(M,&rstart,&rend);CHKERRQ(ierr);
2262fee21e36SBarry Smith   aij = (Mat_SeqAIJ*)(Mreuse)->data;
226300e6dbe6SBarry Smith   ii  = aij->i;
226400e6dbe6SBarry Smith   jj  = aij->j;
226500e6dbe6SBarry Smith   aa  = aij->a;
2266a0ff6018SBarry Smith   for (i=0; i<m; i++) {
2267a0ff6018SBarry Smith     row   = rstart + i;
226800e6dbe6SBarry Smith     nz    = ii[i+1] - ii[i];
226900e6dbe6SBarry Smith     cwork = jj;     jj += nz;
227000e6dbe6SBarry Smith     vwork = aa;     aa += nz;
22718c638d02SBarry Smith     ierr = MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr);
2272a0ff6018SBarry Smith   }
2273a0ff6018SBarry Smith 
2274a0ff6018SBarry Smith   ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2275a0ff6018SBarry Smith   ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2276a0ff6018SBarry Smith   *newmat = M;
2277fee21e36SBarry Smith 
2278fee21e36SBarry Smith   /* save submatrix used in processor for next request */
2279fee21e36SBarry Smith   if (call ==  MAT_INITIAL_MATRIX) {
2280fee21e36SBarry Smith     ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr);
2281fee21e36SBarry Smith     ierr = PetscObjectDereference((PetscObject)Mreuse);CHKERRQ(ierr);
2282fee21e36SBarry Smith   }
2283fee21e36SBarry Smith 
2284a0ff6018SBarry Smith   PetscFunctionReturn(0);
2285a0ff6018SBarry Smith }
2286273d9f13SBarry Smith 
2287e2e86b8fSSatish Balay EXTERN_C_BEGIN
22884a2ae208SSatish Balay #undef __FUNCT__
2289ccd8e176SBarry Smith #define __FUNCT__ "MatMPIAIJSetPreallocationCSR_MPIAIJ"
2290be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJSetPreallocationCSR_MPIAIJ(Mat B,const PetscInt I[],const PetscInt J[],const PetscScalar v[])
2291ccd8e176SBarry Smith {
2292*899cda47SBarry Smith   PetscInt       m,cstart, cend,j,nnz,i,d;
2293*899cda47SBarry Smith   PetscInt       *d_nnz,*o_nnz,nnz_max = 0,rstart,ii;
2294ccd8e176SBarry Smith   const PetscInt *JJ;
2295ccd8e176SBarry Smith   PetscScalar    *values;
2296ccd8e176SBarry Smith   PetscErrorCode ierr;
2297ccd8e176SBarry Smith 
2298ccd8e176SBarry Smith   PetscFunctionBegin;
2299a1661176SMatthew Knepley   if (I[0]) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"I[0] must be 0 it is %D",I[0]);
2300*899cda47SBarry Smith 
2301*899cda47SBarry Smith   B->rmap.bs = B->cmap.bs = 1;
2302*899cda47SBarry Smith   ierr = PetscMapInitialize(B->comm,&B->rmap);CHKERRQ(ierr);
2303*899cda47SBarry Smith   ierr = PetscMapInitialize(B->comm,&B->cmap);CHKERRQ(ierr);
2304*899cda47SBarry Smith   m      = B->rmap.n;
2305*899cda47SBarry Smith   cstart = B->cmap.rstart;
2306*899cda47SBarry Smith   cend   = B->cmap.rend;
2307*899cda47SBarry Smith   rstart = B->rmap.rstart;
2308*899cda47SBarry Smith 
2309ccd8e176SBarry Smith   ierr  = PetscMalloc((2*m+1)*sizeof(PetscInt),&d_nnz);CHKERRQ(ierr);
2310ccd8e176SBarry Smith   o_nnz = d_nnz + m;
2311ccd8e176SBarry Smith 
2312ccd8e176SBarry Smith   for (i=0; i<m; i++) {
2313ccd8e176SBarry Smith     nnz     = I[i+1]- I[i];
2314ccd8e176SBarry Smith     JJ      = J + I[i];
2315ccd8e176SBarry Smith     nnz_max = PetscMax(nnz_max,nnz);
2316a1661176SMatthew Knepley     if (nnz < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative %D number of columns",i,nnz);
2317ccd8e176SBarry Smith     for (j=0; j<nnz; j++) {
2318ccd8e176SBarry Smith       if (*JJ >= cstart) break;
2319ccd8e176SBarry Smith       JJ++;
2320ccd8e176SBarry Smith     }
2321ccd8e176SBarry Smith     d = 0;
2322ccd8e176SBarry Smith     for (; j<nnz; j++) {
2323ccd8e176SBarry Smith       if (*JJ++ >= cend) break;
2324ccd8e176SBarry Smith       d++;
2325ccd8e176SBarry Smith     }
2326ccd8e176SBarry Smith     d_nnz[i] = d;
2327ccd8e176SBarry Smith     o_nnz[i] = nnz - d;
2328ccd8e176SBarry Smith   }
2329ccd8e176SBarry Smith   ierr = MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);CHKERRQ(ierr);
2330ccd8e176SBarry Smith   ierr = PetscFree(d_nnz);CHKERRQ(ierr);
2331ccd8e176SBarry Smith 
2332ccd8e176SBarry Smith   if (v) values = (PetscScalar*)v;
2333ccd8e176SBarry Smith   else {
2334ccd8e176SBarry Smith     ierr = PetscMalloc((nnz_max+1)*sizeof(PetscScalar),&values);CHKERRQ(ierr);
2335ccd8e176SBarry Smith     ierr = PetscMemzero(values,nnz_max*sizeof(PetscScalar));CHKERRQ(ierr);
2336ccd8e176SBarry Smith   }
2337ccd8e176SBarry Smith 
2338ccd8e176SBarry Smith   ierr = MatSetOption(B,MAT_COLUMNS_SORTED);CHKERRQ(ierr);
2339ccd8e176SBarry Smith   for (i=0; i<m; i++) {
2340ccd8e176SBarry Smith     ii   = i + rstart;
2341ccd8e176SBarry Smith     nnz  = I[i+1]- I[i];
23425805c746SSatish Balay     ierr = MatSetValues_MPIAIJ(B,1,&ii,nnz,J+I[i],values+(v ? I[i] : 0),INSERT_VALUES);CHKERRQ(ierr);
2343ccd8e176SBarry Smith   }
2344ccd8e176SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2345ccd8e176SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2346ccd8e176SBarry Smith   ierr = MatSetOption(B,MAT_COLUMNS_UNSORTED);CHKERRQ(ierr);
2347ccd8e176SBarry Smith 
2348ccd8e176SBarry Smith   if (!v) {
2349ccd8e176SBarry Smith     ierr = PetscFree(values);CHKERRQ(ierr);
2350ccd8e176SBarry Smith   }
2351ccd8e176SBarry Smith   PetscFunctionReturn(0);
2352ccd8e176SBarry Smith }
2353e2e86b8fSSatish Balay EXTERN_C_END
2354ccd8e176SBarry Smith 
2355ccd8e176SBarry Smith #undef __FUNCT__
2356ccd8e176SBarry Smith #define __FUNCT__ "MatMPIAIJSetPreallocationCSR"
23571eea217eSSatish Balay /*@
2358ccd8e176SBarry Smith    MatMPIAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in AIJ format
2359ccd8e176SBarry Smith    (the default parallel PETSc format).
2360ccd8e176SBarry Smith 
2361ccd8e176SBarry Smith    Collective on MPI_Comm
2362ccd8e176SBarry Smith 
2363ccd8e176SBarry Smith    Input Parameters:
2364a1661176SMatthew Knepley +  B - the matrix
2365ccd8e176SBarry Smith .  i - the indices into j for the start of each local row (starts with zero)
2366ccd8e176SBarry Smith .  j - the column indices for each local row (starts with zero) these must be sorted for each row
2367ccd8e176SBarry Smith -  v - optional values in the matrix
2368ccd8e176SBarry Smith 
2369ccd8e176SBarry Smith    Level: developer
2370ccd8e176SBarry Smith 
2371ccd8e176SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
2372ccd8e176SBarry Smith 
2373ccd8e176SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatCreateMPIAIJ(), MPIAIJ
2374ccd8e176SBarry Smith @*/
2375be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
2376ccd8e176SBarry Smith {
2377ccd8e176SBarry Smith   PetscErrorCode ierr,(*f)(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]);
2378ccd8e176SBarry Smith 
2379ccd8e176SBarry Smith   PetscFunctionBegin;
2380ccd8e176SBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)B,"MatMPIAIJSetPreallocationCSR_C",(void (**)(void))&f);CHKERRQ(ierr);
2381ccd8e176SBarry Smith   if (f) {
2382ccd8e176SBarry Smith     ierr = (*f)(B,i,j,v);CHKERRQ(ierr);
2383ccd8e176SBarry Smith   }
2384ccd8e176SBarry Smith   PetscFunctionReturn(0);
2385ccd8e176SBarry Smith }
2386ccd8e176SBarry Smith 
2387ccd8e176SBarry Smith #undef __FUNCT__
23884a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJSetPreallocation"
2389273d9f13SBarry Smith /*@C
2390ccd8e176SBarry Smith    MatMPIAIJSetPreallocation - Preallocates memory for a sparse parallel matrix in AIJ format
2391273d9f13SBarry Smith    (the default parallel PETSc format).  For good matrix assembly performance
2392273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameters
2393273d9f13SBarry Smith    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
2394273d9f13SBarry Smith    performance can be increased by more than a factor of 50.
2395273d9f13SBarry Smith 
2396273d9f13SBarry Smith    Collective on MPI_Comm
2397273d9f13SBarry Smith 
2398273d9f13SBarry Smith    Input Parameters:
2399273d9f13SBarry Smith +  A - the matrix
2400273d9f13SBarry Smith .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
2401273d9f13SBarry Smith            (same value is used for all local rows)
2402273d9f13SBarry Smith .  d_nnz - array containing the number of nonzeros in the various rows of the
2403273d9f13SBarry Smith            DIAGONAL portion of the local submatrix (possibly different for each row)
2404273d9f13SBarry Smith            or PETSC_NULL, if d_nz is used to specify the nonzero structure.
2405273d9f13SBarry Smith            The size of this array is equal to the number of local rows, i.e 'm'.
2406273d9f13SBarry Smith            You must leave room for the diagonal entry even if it is zero.
2407273d9f13SBarry Smith .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
2408273d9f13SBarry Smith            submatrix (same value is used for all local rows).
2409273d9f13SBarry Smith -  o_nnz - array containing the number of nonzeros in the various rows of the
2410273d9f13SBarry Smith            OFF-DIAGONAL portion of the local submatrix (possibly different for
2411273d9f13SBarry Smith            each row) or PETSC_NULL, if o_nz is used to specify the nonzero
2412273d9f13SBarry Smith            structure. The size of this array is equal to the number
2413273d9f13SBarry Smith            of local rows, i.e 'm'.
2414273d9f13SBarry Smith 
241549a6f317SBarry Smith    If the *_nnz parameter is given then the *_nz parameter is ignored
241649a6f317SBarry Smith 
2417273d9f13SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
2418ccd8e176SBarry Smith    compressed row storage (CSR)), is fully compatible with standard Fortran 77
2419ccd8e176SBarry Smith    storage.  The stored row and column indices begin with zero.  See the users manual for details.
2420273d9f13SBarry Smith 
2421273d9f13SBarry Smith    The parallel matrix is partitioned such that the first m0 rows belong to
2422273d9f13SBarry Smith    process 0, the next m1 rows belong to process 1, the next m2 rows belong
2423273d9f13SBarry Smith    to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.
2424273d9f13SBarry Smith 
2425273d9f13SBarry Smith    The DIAGONAL portion of the local submatrix of a processor can be defined
2426273d9f13SBarry Smith    as the submatrix which is obtained by extraction the part corresponding
2427273d9f13SBarry Smith    to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the
2428273d9f13SBarry Smith    first row that belongs to the processor, and r2 is the last row belonging
2429273d9f13SBarry Smith    to the this processor. This is a square mxm matrix. The remaining portion
2430273d9f13SBarry Smith    of the local submatrix (mxN) constitute the OFF-DIAGONAL portion.
2431273d9f13SBarry Smith 
2432273d9f13SBarry Smith    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
2433273d9f13SBarry Smith 
2434273d9f13SBarry Smith    Example usage:
2435273d9f13SBarry Smith 
2436273d9f13SBarry Smith    Consider the following 8x8 matrix with 34 non-zero values, that is
2437273d9f13SBarry Smith    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
2438273d9f13SBarry Smith    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
2439273d9f13SBarry Smith    as follows:
2440273d9f13SBarry Smith 
2441273d9f13SBarry Smith .vb
2442273d9f13SBarry Smith             1  2  0  |  0  3  0  |  0  4
2443273d9f13SBarry Smith     Proc0   0  5  6  |  7  0  0  |  8  0
2444273d9f13SBarry Smith             9  0 10  | 11  0  0  | 12  0
2445273d9f13SBarry Smith     -------------------------------------
2446273d9f13SBarry Smith            13  0 14  | 15 16 17  |  0  0
2447273d9f13SBarry Smith     Proc1   0 18  0  | 19 20 21  |  0  0
2448273d9f13SBarry Smith             0  0  0  | 22 23  0  | 24  0
2449273d9f13SBarry Smith     -------------------------------------
2450273d9f13SBarry Smith     Proc2  25 26 27  |  0  0 28  | 29  0
2451273d9f13SBarry Smith            30  0  0  | 31 32 33  |  0 34
2452273d9f13SBarry Smith .ve
2453273d9f13SBarry Smith 
2454273d9f13SBarry Smith    This can be represented as a collection of submatrices as:
2455273d9f13SBarry Smith 
2456273d9f13SBarry Smith .vb
2457273d9f13SBarry Smith       A B C
2458273d9f13SBarry Smith       D E F
2459273d9f13SBarry Smith       G H I
2460273d9f13SBarry Smith .ve
2461273d9f13SBarry Smith 
2462273d9f13SBarry Smith    Where the submatrices A,B,C are owned by proc0, D,E,F are
2463273d9f13SBarry Smith    owned by proc1, G,H,I are owned by proc2.
2464273d9f13SBarry Smith 
2465273d9f13SBarry Smith    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2466273d9f13SBarry Smith    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2467273d9f13SBarry Smith    The 'M','N' parameters are 8,8, and have the same values on all procs.
2468273d9f13SBarry Smith 
2469273d9f13SBarry Smith    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
2470273d9f13SBarry Smith    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
2471273d9f13SBarry Smith    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
2472273d9f13SBarry Smith    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
2473273d9f13SBarry Smith    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
2474273d9f13SBarry Smith    matrix, ans [DF] as another SeqAIJ matrix.
2475273d9f13SBarry Smith 
2476273d9f13SBarry Smith    When d_nz, o_nz parameters are specified, d_nz storage elements are
2477273d9f13SBarry Smith    allocated for every row of the local diagonal submatrix, and o_nz
2478273d9f13SBarry Smith    storage locations are allocated for every row of the OFF-DIAGONAL submat.
2479273d9f13SBarry Smith    One way to choose d_nz and o_nz is to use the max nonzerors per local
2480273d9f13SBarry Smith    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
2481273d9f13SBarry Smith    In this case, the values of d_nz,o_nz are:
2482273d9f13SBarry Smith .vb
2483273d9f13SBarry Smith      proc0 : dnz = 2, o_nz = 2
2484273d9f13SBarry Smith      proc1 : dnz = 3, o_nz = 2
2485273d9f13SBarry Smith      proc2 : dnz = 1, o_nz = 4
2486273d9f13SBarry Smith .ve
2487273d9f13SBarry Smith    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
2488273d9f13SBarry Smith    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
2489273d9f13SBarry Smith    for proc3. i.e we are using 12+15+10=37 storage locations to store
2490273d9f13SBarry Smith    34 values.
2491273d9f13SBarry Smith 
2492273d9f13SBarry Smith    When d_nnz, o_nnz parameters are specified, the storage is specified
2493273d9f13SBarry Smith    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
2494273d9f13SBarry Smith    In the above case the values for d_nnz,o_nnz are:
2495273d9f13SBarry Smith .vb
2496273d9f13SBarry Smith      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
2497273d9f13SBarry Smith      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
2498273d9f13SBarry Smith      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
2499273d9f13SBarry Smith .ve
2500273d9f13SBarry Smith    Here the space allocated is sum of all the above values i.e 34, and
2501273d9f13SBarry Smith    hence pre-allocation is perfect.
2502273d9f13SBarry Smith 
2503273d9f13SBarry Smith    Level: intermediate
2504273d9f13SBarry Smith 
2505273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
2506273d9f13SBarry Smith 
2507ccd8e176SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateMPIAIJ(), MatMPIAIJSetPreallocationCSR(),
2508ccd8e176SBarry Smith           MPIAIJ
2509273d9f13SBarry Smith @*/
2510be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJSetPreallocation(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
2511273d9f13SBarry Smith {
2512b1d57f15SBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[]);
2513273d9f13SBarry Smith 
2514273d9f13SBarry Smith   PetscFunctionBegin;
2515a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatMPIAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
2516a23d5eceSKris Buschelman   if (f) {
2517a23d5eceSKris Buschelman     ierr = (*f)(B,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr);
2518273d9f13SBarry Smith   }
2519273d9f13SBarry Smith   PetscFunctionReturn(0);
2520273d9f13SBarry Smith }
2521273d9f13SBarry Smith 
25224a2ae208SSatish Balay #undef __FUNCT__
25234a2ae208SSatish Balay #define __FUNCT__ "MatCreateMPIAIJ"
2524273d9f13SBarry Smith /*@C
2525273d9f13SBarry Smith    MatCreateMPIAIJ - Creates a sparse parallel matrix in AIJ format
2526273d9f13SBarry Smith    (the default parallel PETSc format).  For good matrix assembly performance
2527273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameters
2528273d9f13SBarry Smith    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
2529273d9f13SBarry Smith    performance can be increased by more than a factor of 50.
2530273d9f13SBarry Smith 
2531273d9f13SBarry Smith    Collective on MPI_Comm
2532273d9f13SBarry Smith 
2533273d9f13SBarry Smith    Input Parameters:
2534273d9f13SBarry Smith +  comm - MPI communicator
2535273d9f13SBarry Smith .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
2536273d9f13SBarry Smith            This value should be the same as the local size used in creating the
2537273d9f13SBarry Smith            y vector for the matrix-vector product y = Ax.
2538273d9f13SBarry Smith .  n - This value should be the same as the local size used in creating the
2539273d9f13SBarry Smith        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
2540273d9f13SBarry Smith        calculated if N is given) For square matrices n is almost always m.
2541273d9f13SBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
2542273d9f13SBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
2543273d9f13SBarry Smith .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
2544273d9f13SBarry Smith            (same value is used for all local rows)
2545273d9f13SBarry Smith .  d_nnz - array containing the number of nonzeros in the various rows of the
2546273d9f13SBarry Smith            DIAGONAL portion of the local submatrix (possibly different for each row)
2547273d9f13SBarry Smith            or PETSC_NULL, if d_nz is used to specify the nonzero structure.
2548273d9f13SBarry Smith            The size of this array is equal to the number of local rows, i.e 'm'.
2549273d9f13SBarry Smith            You must leave room for the diagonal entry even if it is zero.
2550273d9f13SBarry Smith .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
2551273d9f13SBarry Smith            submatrix (same value is used for all local rows).
2552273d9f13SBarry Smith -  o_nnz - array containing the number of nonzeros in the various rows of the
2553273d9f13SBarry Smith            OFF-DIAGONAL portion of the local submatrix (possibly different for
2554273d9f13SBarry Smith            each row) or PETSC_NULL, if o_nz is used to specify the nonzero
2555273d9f13SBarry Smith            structure. The size of this array is equal to the number
2556273d9f13SBarry Smith            of local rows, i.e 'm'.
2557273d9f13SBarry Smith 
2558273d9f13SBarry Smith    Output Parameter:
2559273d9f13SBarry Smith .  A - the matrix
2560273d9f13SBarry Smith 
2561273d9f13SBarry Smith    Notes:
256249a6f317SBarry Smith    If the *_nnz parameter is given then the *_nz parameter is ignored
256349a6f317SBarry Smith 
2564273d9f13SBarry Smith    m,n,M,N parameters specify the size of the matrix, and its partitioning across
2565273d9f13SBarry Smith    processors, while d_nz,d_nnz,o_nz,o_nnz parameters specify the approximate
2566273d9f13SBarry Smith    storage requirements for this matrix.
2567273d9f13SBarry Smith 
2568273d9f13SBarry Smith    If PETSC_DECIDE or  PETSC_DETERMINE is used for a particular argument on one
2569273d9f13SBarry Smith    processor than it must be used on all processors that share the object for
2570273d9f13SBarry Smith    that argument.
2571273d9f13SBarry Smith 
2572273d9f13SBarry Smith    The user MUST specify either the local or global matrix dimensions
2573273d9f13SBarry Smith    (possibly both).
2574273d9f13SBarry Smith 
2575273d9f13SBarry Smith    The parallel matrix is partitioned such that the first m0 rows belong to
2576273d9f13SBarry Smith    process 0, the next m1 rows belong to process 1, the next m2 rows belong
2577273d9f13SBarry Smith    to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.
2578273d9f13SBarry Smith 
2579273d9f13SBarry Smith    The DIAGONAL portion of the local submatrix of a processor can be defined
2580273d9f13SBarry Smith    as the submatrix which is obtained by extraction the part corresponding
2581273d9f13SBarry Smith    to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the
2582273d9f13SBarry Smith    first row that belongs to the processor, and r2 is the last row belonging
2583273d9f13SBarry Smith    to the this processor. This is a square mxm matrix. The remaining portion
2584273d9f13SBarry Smith    of the local submatrix (mxN) constitute the OFF-DIAGONAL portion.
2585273d9f13SBarry Smith 
2586273d9f13SBarry Smith    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
2587273d9f13SBarry Smith 
258897d05335SKris Buschelman    When calling this routine with a single process communicator, a matrix of
258997d05335SKris Buschelman    type SEQAIJ is returned.  If a matrix of type MPIAIJ is desired for this
259097d05335SKris Buschelman    type of communicator, use the construction mechanism:
259197d05335SKris Buschelman      MatCreate(...,&A); MatSetType(A,MPIAIJ); MatMPIAIJSetPreallocation(A,...);
259297d05335SKris Buschelman 
2593273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible.
2594273d9f13SBarry Smith    We search for consecutive rows with the same nonzero structure, thereby
2595273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2596273d9f13SBarry Smith 
2597273d9f13SBarry Smith    Options Database Keys:
2598923f20ffSKris Buschelman +  -mat_no_inode  - Do not use inodes
2599923f20ffSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2600273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2601273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2602273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2603273d9f13SBarry Smith 
2604273d9f13SBarry Smith 
2605273d9f13SBarry Smith    Example usage:
2606273d9f13SBarry Smith 
2607273d9f13SBarry Smith    Consider the following 8x8 matrix with 34 non-zero values, that is
2608273d9f13SBarry Smith    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
2609273d9f13SBarry Smith    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
2610273d9f13SBarry Smith    as follows:
2611273d9f13SBarry Smith 
2612273d9f13SBarry Smith .vb
2613273d9f13SBarry Smith             1  2  0  |  0  3  0  |  0  4
2614273d9f13SBarry Smith     Proc0   0  5  6  |  7  0  0  |  8  0
2615273d9f13SBarry Smith             9  0 10  | 11  0  0  | 12  0
2616273d9f13SBarry Smith     -------------------------------------
2617273d9f13SBarry Smith            13  0 14  | 15 16 17  |  0  0
2618273d9f13SBarry Smith     Proc1   0 18  0  | 19 20 21  |  0  0
2619273d9f13SBarry Smith             0  0  0  | 22 23  0  | 24  0
2620273d9f13SBarry Smith     -------------------------------------
2621273d9f13SBarry Smith     Proc2  25 26 27  |  0  0 28  | 29  0
2622273d9f13SBarry Smith            30  0  0  | 31 32 33  |  0 34
2623273d9f13SBarry Smith .ve
2624273d9f13SBarry Smith 
2625273d9f13SBarry Smith    This can be represented as a collection of submatrices as:
2626273d9f13SBarry Smith 
2627273d9f13SBarry Smith .vb
2628273d9f13SBarry Smith       A B C
2629273d9f13SBarry Smith       D E F
2630273d9f13SBarry Smith       G H I
2631273d9f13SBarry Smith .ve
2632273d9f13SBarry Smith 
2633273d9f13SBarry Smith    Where the submatrices A,B,C are owned by proc0, D,E,F are
2634273d9f13SBarry Smith    owned by proc1, G,H,I are owned by proc2.
2635273d9f13SBarry Smith 
2636273d9f13SBarry Smith    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2637273d9f13SBarry Smith    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2638273d9f13SBarry Smith    The 'M','N' parameters are 8,8, and have the same values on all procs.
2639273d9f13SBarry Smith 
2640273d9f13SBarry Smith    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
2641273d9f13SBarry Smith    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
2642273d9f13SBarry Smith    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
2643273d9f13SBarry Smith    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
2644273d9f13SBarry Smith    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
2645273d9f13SBarry Smith    matrix, ans [DF] as another SeqAIJ matrix.
2646273d9f13SBarry Smith 
2647273d9f13SBarry Smith    When d_nz, o_nz parameters are specified, d_nz storage elements are
2648273d9f13SBarry Smith    allocated for every row of the local diagonal submatrix, and o_nz
2649273d9f13SBarry Smith    storage locations are allocated for every row of the OFF-DIAGONAL submat.
2650273d9f13SBarry Smith    One way to choose d_nz and o_nz is to use the max nonzerors per local
2651273d9f13SBarry Smith    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
2652273d9f13SBarry Smith    In this case, the values of d_nz,o_nz are:
2653273d9f13SBarry Smith .vb
2654273d9f13SBarry Smith      proc0 : dnz = 2, o_nz = 2
2655273d9f13SBarry Smith      proc1 : dnz = 3, o_nz = 2
2656273d9f13SBarry Smith      proc2 : dnz = 1, o_nz = 4
2657273d9f13SBarry Smith .ve
2658273d9f13SBarry Smith    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
2659273d9f13SBarry Smith    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
2660273d9f13SBarry Smith    for proc3. i.e we are using 12+15+10=37 storage locations to store
2661273d9f13SBarry Smith    34 values.
2662273d9f13SBarry Smith 
2663273d9f13SBarry Smith    When d_nnz, o_nnz parameters are specified, the storage is specified
2664273d9f13SBarry Smith    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
2665273d9f13SBarry Smith    In the above case the values for d_nnz,o_nnz are:
2666273d9f13SBarry Smith .vb
2667273d9f13SBarry Smith      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
2668273d9f13SBarry Smith      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
2669273d9f13SBarry Smith      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
2670273d9f13SBarry Smith .ve
2671273d9f13SBarry Smith    Here the space allocated is sum of all the above values i.e 34, and
2672273d9f13SBarry Smith    hence pre-allocation is perfect.
2673273d9f13SBarry Smith 
2674273d9f13SBarry Smith    Level: intermediate
2675273d9f13SBarry Smith 
2676273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
2677273d9f13SBarry Smith 
2678ccd8e176SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
2679ccd8e176SBarry Smith           MPIAIJ
2680273d9f13SBarry Smith @*/
2681be1d678aSKris 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)
2682273d9f13SBarry Smith {
26836849ba73SBarry Smith   PetscErrorCode ierr;
2684b1d57f15SBarry Smith   PetscMPIInt    size;
2685273d9f13SBarry Smith 
2686273d9f13SBarry Smith   PetscFunctionBegin;
2687f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
2688f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*A,m,n,M,N);CHKERRQ(ierr);
2689273d9f13SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
2690273d9f13SBarry Smith   if (size > 1) {
2691273d9f13SBarry Smith     ierr = MatSetType(*A,MATMPIAIJ);CHKERRQ(ierr);
2692273d9f13SBarry Smith     ierr = MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr);
2693273d9f13SBarry Smith   } else {
2694273d9f13SBarry Smith     ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2695273d9f13SBarry Smith     ierr = MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);CHKERRQ(ierr);
2696273d9f13SBarry Smith   }
2697273d9f13SBarry Smith   PetscFunctionReturn(0);
2698273d9f13SBarry Smith }
2699195d93cdSBarry Smith 
27004a2ae208SSatish Balay #undef __FUNCT__
27014a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJGetSeqAIJ"
2702be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJGetSeqAIJ(Mat A,Mat *Ad,Mat *Ao,PetscInt *colmap[])
2703195d93cdSBarry Smith {
2704195d93cdSBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;
2705b1d57f15SBarry Smith 
2706195d93cdSBarry Smith   PetscFunctionBegin;
2707195d93cdSBarry Smith   *Ad     = a->A;
2708195d93cdSBarry Smith   *Ao     = a->B;
2709195d93cdSBarry Smith   *colmap = a->garray;
2710195d93cdSBarry Smith   PetscFunctionReturn(0);
2711195d93cdSBarry Smith }
2712a2243be0SBarry Smith 
2713a2243be0SBarry Smith #undef __FUNCT__
2714a2243be0SBarry Smith #define __FUNCT__ "MatSetColoring_MPIAIJ"
2715dfbe8321SBarry Smith PetscErrorCode MatSetColoring_MPIAIJ(Mat A,ISColoring coloring)
2716a2243be0SBarry Smith {
2717dfbe8321SBarry Smith   PetscErrorCode ierr;
2718b1d57f15SBarry Smith   PetscInt       i;
2719a2243be0SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
2720a2243be0SBarry Smith 
2721a2243be0SBarry Smith   PetscFunctionBegin;
2722a2243be0SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
272308b6dcc0SBarry Smith     ISColoringValue *allcolors,*colors;
2724a2243be0SBarry Smith     ISColoring      ocoloring;
2725a2243be0SBarry Smith 
2726a2243be0SBarry Smith     /* set coloring for diagonal portion */
2727a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->A,coloring);CHKERRQ(ierr);
2728a2243be0SBarry Smith 
2729a2243be0SBarry Smith     /* set coloring for off-diagonal portion */
273008b6dcc0SBarry Smith     ierr = ISAllGatherColors(A->comm,coloring->n,coloring->colors,PETSC_NULL,&allcolors);CHKERRQ(ierr);
2731*899cda47SBarry Smith     ierr = PetscMalloc((a->B->cmap.n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
2732*899cda47SBarry Smith     for (i=0; i<a->B->cmap.n; i++) {
2733a2243be0SBarry Smith       colors[i] = allcolors[a->garray[i]];
2734a2243be0SBarry Smith     }
2735a2243be0SBarry Smith     ierr = PetscFree(allcolors);CHKERRQ(ierr);
2736*899cda47SBarry Smith     ierr = ISColoringCreate(MPI_COMM_SELF,a->B->cmap.n,coloring->n,colors,&ocoloring);CHKERRQ(ierr);
2737a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr);
2738a2243be0SBarry Smith     ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr);
2739a2243be0SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
274008b6dcc0SBarry Smith     ISColoringValue *colors;
2741b1d57f15SBarry Smith     PetscInt        *larray;
2742a2243be0SBarry Smith     ISColoring      ocoloring;
2743a2243be0SBarry Smith 
2744a2243be0SBarry Smith     /* set coloring for diagonal portion */
2745*899cda47SBarry Smith     ierr = PetscMalloc((a->A->cmap.n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr);
2746*899cda47SBarry Smith     for (i=0; i<a->A->cmap.n; i++) {
2747*899cda47SBarry Smith       larray[i] = i + A->cmap.rstart;
2748a2243be0SBarry Smith     }
2749*899cda47SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->A->cmap.n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
2750*899cda47SBarry Smith     ierr = PetscMalloc((a->A->cmap.n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
2751*899cda47SBarry Smith     for (i=0; i<a->A->cmap.n; i++) {
2752a2243be0SBarry Smith       colors[i] = coloring->colors[larray[i]];
2753a2243be0SBarry Smith     }
2754a2243be0SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
2755*899cda47SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,a->A->cmap.n,coloring->n,colors,&ocoloring);CHKERRQ(ierr);
2756a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->A,ocoloring);CHKERRQ(ierr);
2757a2243be0SBarry Smith     ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr);
2758a2243be0SBarry Smith 
2759a2243be0SBarry Smith     /* set coloring for off-diagonal portion */
2760*899cda47SBarry Smith     ierr = PetscMalloc((a->B->cmap.n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr);
2761*899cda47SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->B->cmap.n,a->garray,PETSC_NULL,larray);CHKERRQ(ierr);
2762*899cda47SBarry Smith     ierr = PetscMalloc((a->B->cmap.n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
2763*899cda47SBarry Smith     for (i=0; i<a->B->cmap.n; i++) {
2764a2243be0SBarry Smith       colors[i] = coloring->colors[larray[i]];
2765a2243be0SBarry Smith     }
2766a2243be0SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
2767*899cda47SBarry Smith     ierr = ISColoringCreate(MPI_COMM_SELF,a->B->cmap.n,coloring->n,colors,&ocoloring);CHKERRQ(ierr);
2768a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr);
2769a2243be0SBarry Smith     ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr);
2770a2243be0SBarry Smith   } else {
277177431f27SBarry Smith     SETERRQ1(PETSC_ERR_SUP,"No support ISColoringType %d",(int)coloring->ctype);
2772a2243be0SBarry Smith   }
2773a2243be0SBarry Smith 
2774a2243be0SBarry Smith   PetscFunctionReturn(0);
2775a2243be0SBarry Smith }
2776a2243be0SBarry Smith 
2777dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
2778a2243be0SBarry Smith #undef __FUNCT__
2779779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdic_MPIAIJ"
2780dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_MPIAIJ(Mat A,void *advalues)
2781a2243be0SBarry Smith {
2782a2243be0SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
2783dfbe8321SBarry Smith   PetscErrorCode ierr;
2784a2243be0SBarry Smith 
2785a2243be0SBarry Smith   PetscFunctionBegin;
2786779c1a83SBarry Smith   ierr = MatSetValuesAdic_SeqAIJ(a->A,advalues);CHKERRQ(ierr);
2787779c1a83SBarry Smith   ierr = MatSetValuesAdic_SeqAIJ(a->B,advalues);CHKERRQ(ierr);
2788779c1a83SBarry Smith   PetscFunctionReturn(0);
2789779c1a83SBarry Smith }
2790dcf5cc72SBarry Smith #endif
2791779c1a83SBarry Smith 
2792779c1a83SBarry Smith #undef __FUNCT__
2793779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdifor_MPIAIJ"
2794b1d57f15SBarry Smith PetscErrorCode MatSetValuesAdifor_MPIAIJ(Mat A,PetscInt nl,void *advalues)
2795779c1a83SBarry Smith {
2796779c1a83SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
2797dfbe8321SBarry Smith   PetscErrorCode ierr;
2798779c1a83SBarry Smith 
2799779c1a83SBarry Smith   PetscFunctionBegin;
2800779c1a83SBarry Smith   ierr = MatSetValuesAdifor_SeqAIJ(a->A,nl,advalues);CHKERRQ(ierr);
2801779c1a83SBarry Smith   ierr = MatSetValuesAdifor_SeqAIJ(a->B,nl,advalues);CHKERRQ(ierr);
2802a2243be0SBarry Smith   PetscFunctionReturn(0);
2803a2243be0SBarry Smith }
2804c5d6d63eSBarry Smith 
2805c5d6d63eSBarry Smith #undef __FUNCT__
280651dd7536SBarry Smith #define __FUNCT__ "MatMerge"
2807c5d6d63eSBarry Smith /*@C
280851dd7536SBarry Smith       MatMerge - Creates a single large PETSc matrix by concatinating sequential
280951dd7536SBarry Smith                  matrices from each processor
2810c5d6d63eSBarry Smith 
2811c5d6d63eSBarry Smith     Collective on MPI_Comm
2812c5d6d63eSBarry Smith 
2813c5d6d63eSBarry Smith    Input Parameters:
281451dd7536SBarry Smith +    comm - the communicators the parallel matrix will live on
2815d6bb3c2dSHong Zhang .    inmat - the input sequential matrices
28160e36024fSHong Zhang .    n - number of local columns (or PETSC_DECIDE)
2817d6bb3c2dSHong Zhang -    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
281851dd7536SBarry Smith 
281951dd7536SBarry Smith    Output Parameter:
282051dd7536SBarry Smith .    outmat - the parallel matrix generated
2821c5d6d63eSBarry Smith 
28227e25d530SSatish Balay     Level: advanced
28237e25d530SSatish Balay 
2824f08fae4eSHong Zhang    Notes: The number of columns of the matrix in EACH processor MUST be the same.
2825c5d6d63eSBarry Smith 
2826c5d6d63eSBarry Smith @*/
2827be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMerge(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
2828c5d6d63eSBarry Smith {
2829dfbe8321SBarry Smith   PetscErrorCode ierr;
2830b1d57f15SBarry Smith   PetscInt       m,N,i,rstart,nnz,I,*dnz,*onz;
2831ba8c8a56SBarry Smith   PetscInt       *indx;
2832ba8c8a56SBarry Smith   PetscScalar    *values;
2833c5d6d63eSBarry Smith 
2834c5d6d63eSBarry Smith   PetscFunctionBegin;
28350e36024fSHong Zhang   ierr = MatGetSize(inmat,&m,&N);CHKERRQ(ierr);
2836d6bb3c2dSHong Zhang   if (scall == MAT_INITIAL_MATRIX){
2837d6bb3c2dSHong Zhang     /* count nonzeros in each row, for diagonal and off diagonal portion of matrix */
28380e36024fSHong Zhang     if (n == PETSC_DECIDE){
2839357abbc8SBarry Smith       ierr = PetscSplitOwnership(comm,&n,&N);CHKERRQ(ierr);
28400e36024fSHong Zhang     }
2841357abbc8SBarry Smith     ierr = MPI_Scan(&m, &rstart,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr);
2842357abbc8SBarry Smith     rstart -= m;
2843d6bb3c2dSHong Zhang 
2844d6bb3c2dSHong Zhang     ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr);
2845d6bb3c2dSHong Zhang     for (i=0;i<m;i++) {
2846ba8c8a56SBarry Smith       ierr = MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,PETSC_NULL);CHKERRQ(ierr);
2847d6bb3c2dSHong Zhang       ierr = MatPreallocateSet(i+rstart,nnz,indx,dnz,onz);CHKERRQ(ierr);
2848ba8c8a56SBarry Smith       ierr = MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,PETSC_NULL);CHKERRQ(ierr);
2849d6bb3c2dSHong Zhang     }
2850d6bb3c2dSHong Zhang     /* This routine will ONLY return MPIAIJ type matrix */
2851f69a0ea3SMatthew Knepley     ierr = MatCreate(comm,outmat);CHKERRQ(ierr);
2852f69a0ea3SMatthew Knepley     ierr = MatSetSizes(*outmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
2853d6bb3c2dSHong Zhang     ierr = MatSetType(*outmat,MATMPIAIJ);CHKERRQ(ierr);
2854d6bb3c2dSHong Zhang     ierr = MatMPIAIJSetPreallocation(*outmat,0,dnz,0,onz);CHKERRQ(ierr);
2855d6bb3c2dSHong Zhang     ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr);
2856d6bb3c2dSHong Zhang 
2857d6bb3c2dSHong Zhang   } else if (scall == MAT_REUSE_MATRIX){
2858d6bb3c2dSHong Zhang     ierr = MatGetOwnershipRange(*outmat,&rstart,PETSC_NULL);CHKERRQ(ierr);
2859d6bb3c2dSHong Zhang   } else {
286077431f27SBarry Smith     SETERRQ1(PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall);
2861d6bb3c2dSHong Zhang   }
2862d6bb3c2dSHong Zhang 
2863d6bb3c2dSHong Zhang   for (i=0;i<m;i++) {
2864ba8c8a56SBarry Smith     ierr = MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
2865d6bb3c2dSHong Zhang     I    = i + rstart;
2866906b51c7SHong Zhang     ierr = MatSetValues(*outmat,1,&I,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr);
2867ba8c8a56SBarry Smith     ierr = MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
2868d6bb3c2dSHong Zhang   }
2869d6bb3c2dSHong Zhang   ierr = MatDestroy(inmat);CHKERRQ(ierr);
2870d6bb3c2dSHong Zhang   ierr = MatAssemblyBegin(*outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2871d6bb3c2dSHong Zhang   ierr = MatAssemblyEnd(*outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
287251dd7536SBarry Smith 
2873c5d6d63eSBarry Smith   PetscFunctionReturn(0);
2874c5d6d63eSBarry Smith }
2875c5d6d63eSBarry Smith 
2876c5d6d63eSBarry Smith #undef __FUNCT__
2877c5d6d63eSBarry Smith #define __FUNCT__ "MatFileSplit"
2878dfbe8321SBarry Smith PetscErrorCode MatFileSplit(Mat A,char *outfile)
2879c5d6d63eSBarry Smith {
2880dfbe8321SBarry Smith   PetscErrorCode    ierr;
288132dcc486SBarry Smith   PetscMPIInt       rank;
2882b1d57f15SBarry Smith   PetscInt          m,N,i,rstart,nnz;
2883de4209c5SBarry Smith   size_t            len;
2884b1d57f15SBarry Smith   const PetscInt    *indx;
2885c5d6d63eSBarry Smith   PetscViewer       out;
2886c5d6d63eSBarry Smith   char              *name;
2887c5d6d63eSBarry Smith   Mat               B;
2888b3cc6726SBarry Smith   const PetscScalar *values;
2889c5d6d63eSBarry Smith 
2890c5d6d63eSBarry Smith   PetscFunctionBegin;
2891c5d6d63eSBarry Smith   ierr = MatGetLocalSize(A,&m,0);CHKERRQ(ierr);
2892c5d6d63eSBarry Smith   ierr = MatGetSize(A,0,&N);CHKERRQ(ierr);
2893f204ca49SKris Buschelman   /* Should this be the type of the diagonal block of A? */
2894f69a0ea3SMatthew Knepley   ierr = MatCreate(PETSC_COMM_SELF,&B);CHKERRQ(ierr);
2895f69a0ea3SMatthew Knepley   ierr = MatSetSizes(B,m,N,m,N);CHKERRQ(ierr);
2896f204ca49SKris Buschelman   ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
2897f204ca49SKris Buschelman   ierr = MatSeqAIJSetPreallocation(B,0,PETSC_NULL);CHKERRQ(ierr);
2898c5d6d63eSBarry Smith   ierr = MatGetOwnershipRange(A,&rstart,0);CHKERRQ(ierr);
2899c5d6d63eSBarry Smith   for (i=0;i<m;i++) {
2900c5d6d63eSBarry Smith     ierr = MatGetRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr);
2901c5d6d63eSBarry Smith     ierr = MatSetValues(B,1,&i,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr);
2902c5d6d63eSBarry Smith     ierr = MatRestoreRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr);
2903c5d6d63eSBarry Smith   }
2904c5d6d63eSBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2905c5d6d63eSBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2906c5d6d63eSBarry Smith 
2907c5d6d63eSBarry Smith   ierr = MPI_Comm_rank(A->comm,&rank);CHKERRQ(ierr);
2908c5d6d63eSBarry Smith   ierr = PetscStrlen(outfile,&len);CHKERRQ(ierr);
2909c5d6d63eSBarry Smith   ierr = PetscMalloc((len+5)*sizeof(char),&name);CHKERRQ(ierr);
2910c5d6d63eSBarry Smith   sprintf(name,"%s.%d",outfile,rank);
2911852598b0SBarry Smith   ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,name,FILE_MODE_APPEND,&out);CHKERRQ(ierr);
2912c5d6d63eSBarry Smith   ierr = PetscFree(name);
2913c5d6d63eSBarry Smith   ierr = MatView(B,out);CHKERRQ(ierr);
2914c5d6d63eSBarry Smith   ierr = PetscViewerDestroy(out);CHKERRQ(ierr);
2915c5d6d63eSBarry Smith   ierr = MatDestroy(B);CHKERRQ(ierr);
2916c5d6d63eSBarry Smith   PetscFunctionReturn(0);
2917c5d6d63eSBarry Smith }
2918e5f2cdd8SHong Zhang 
291951a7d1a8SHong Zhang EXTERN PetscErrorCode MatDestroy_MPIAIJ(Mat);
292051a7d1a8SHong Zhang #undef __FUNCT__
292151a7d1a8SHong Zhang #define __FUNCT__ "MatDestroy_MPIAIJ_SeqsToMPI"
2922be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatDestroy_MPIAIJ_SeqsToMPI(Mat A)
292351a7d1a8SHong Zhang {
292451a7d1a8SHong Zhang   PetscErrorCode       ierr;
2925671beff6SHong Zhang   Mat_Merge_SeqsToMPI  *merge;
2926671beff6SHong Zhang   PetscObjectContainer container;
292751a7d1a8SHong Zhang 
292851a7d1a8SHong Zhang   PetscFunctionBegin;
2929671beff6SHong Zhang   ierr = PetscObjectQuery((PetscObject)A,"MatMergeSeqsToMPI",(PetscObject *)&container);CHKERRQ(ierr);
2930671beff6SHong Zhang   if (container) {
29317f79fd58SMatthew Knepley     ierr = PetscObjectContainerGetPointer(container,(void **)&merge);CHKERRQ(ierr);
293251a7d1a8SHong Zhang     ierr = PetscFree(merge->id_r);CHKERRQ(ierr);
29333e06a4e6SHong Zhang     ierr = PetscFree(merge->len_s);CHKERRQ(ierr);
29343e06a4e6SHong Zhang     ierr = PetscFree(merge->len_r);CHKERRQ(ierr);
293551a7d1a8SHong Zhang     ierr = PetscFree(merge->bi);CHKERRQ(ierr);
293651a7d1a8SHong Zhang     ierr = PetscFree(merge->bj);CHKERRQ(ierr);
293702c68681SHong Zhang     ierr = PetscFree(merge->buf_ri);CHKERRQ(ierr);
293802c68681SHong Zhang     ierr = PetscFree(merge->buf_rj);CHKERRQ(ierr);
2939de0260b3SHong Zhang     if (merge->coi){ierr = PetscFree(merge->coi);CHKERRQ(ierr);}
2940de0260b3SHong Zhang     if (merge->coj){ierr = PetscFree(merge->coj);CHKERRQ(ierr);}
2941de0260b3SHong Zhang     if (merge->owners_co){ierr = PetscFree(merge->owners_co);CHKERRQ(ierr);}
2942671beff6SHong Zhang 
2943671beff6SHong Zhang     ierr = PetscObjectContainerDestroy(container);CHKERRQ(ierr);
2944671beff6SHong Zhang     ierr = PetscObjectCompose((PetscObject)A,"MatMergeSeqsToMPI",0);CHKERRQ(ierr);
2945671beff6SHong Zhang   }
294651a7d1a8SHong Zhang   ierr = PetscFree(merge);CHKERRQ(ierr);
294751a7d1a8SHong Zhang 
294851a7d1a8SHong Zhang   ierr = MatDestroy_MPIAIJ(A);CHKERRQ(ierr);
294951a7d1a8SHong Zhang   PetscFunctionReturn(0);
295051a7d1a8SHong Zhang }
295151a7d1a8SHong Zhang 
295258cb9c82SHong Zhang #include "src/mat/utils/freespace.h"
2953be0fcf8dSHong Zhang #include "petscbt.h"
295438f152feSBarry Smith static PetscEvent logkey_seqstompinum = 0;
2955e5f2cdd8SHong Zhang #undef __FUNCT__
295638f152feSBarry Smith #define __FUNCT__ "MatMerge_SeqsToMPINumeric"
2957e5f2cdd8SHong Zhang /*@C
2958f08fae4eSHong Zhang       MatMerge_SeqsToMPI - Creates a MPIAIJ matrix by adding sequential
2959e5f2cdd8SHong Zhang                  matrices from each processor
2960e5f2cdd8SHong Zhang 
2961e5f2cdd8SHong Zhang     Collective on MPI_Comm
2962e5f2cdd8SHong Zhang 
2963e5f2cdd8SHong Zhang    Input Parameters:
2964e5f2cdd8SHong Zhang +    comm - the communicators the parallel matrix will live on
2965f08fae4eSHong Zhang .    seqmat - the input sequential matrices
29660e36024fSHong Zhang .    m - number of local rows (or PETSC_DECIDE)
29670e36024fSHong Zhang .    n - number of local columns (or PETSC_DECIDE)
2968e5f2cdd8SHong Zhang -    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
2969e5f2cdd8SHong Zhang 
2970e5f2cdd8SHong Zhang    Output Parameter:
2971f08fae4eSHong Zhang .    mpimat - the parallel matrix generated
2972e5f2cdd8SHong Zhang 
2973e5f2cdd8SHong Zhang     Level: advanced
2974e5f2cdd8SHong Zhang 
2975affca5deSHong Zhang    Notes:
2976affca5deSHong Zhang      The dimensions of the sequential matrix in each processor MUST be the same.
2977affca5deSHong Zhang      The input seqmat is included into the container "Mat_Merge_SeqsToMPI", and will be
2978affca5deSHong Zhang      destroyed when mpimat is destroyed. Call PetscObjectQuery() to access seqmat.
2979e5f2cdd8SHong Zhang @*/
2980be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMerge_SeqsToMPINumeric(Mat seqmat,Mat mpimat)
298155d1abb9SHong Zhang {
298255d1abb9SHong Zhang   PetscErrorCode       ierr;
298355d1abb9SHong Zhang   MPI_Comm             comm=mpimat->comm;
298455d1abb9SHong Zhang   Mat_SeqAIJ           *a=(Mat_SeqAIJ*)seqmat->data;
2985b1d57f15SBarry Smith   PetscMPIInt          size,rank,taga,*len_s;
2986*899cda47SBarry Smith   PetscInt             N=mpimat->cmap.N,i,j,*owners,*ai=a->i,*aj=a->j;
2987b1d57f15SBarry Smith   PetscInt             proc,m;
2988b1d57f15SBarry Smith   PetscInt             **buf_ri,**buf_rj;
2989b1d57f15SBarry Smith   PetscInt             k,anzi,*bj_i,*bi,*bj,arow,bnzi,nextaj;
2990b1d57f15SBarry Smith   PetscInt             nrows,**buf_ri_k,**nextrow,**nextai;
299155d1abb9SHong Zhang   MPI_Request          *s_waits,*r_waits;
299255d1abb9SHong Zhang   MPI_Status           *status;
2993ce805ee4SHong Zhang   MatScalar            *aa=a->a,**abuf_r,*ba_i;
299455d1abb9SHong Zhang   Mat_Merge_SeqsToMPI  *merge;
299555d1abb9SHong Zhang   PetscObjectContainer container;
299655d1abb9SHong Zhang 
299755d1abb9SHong Zhang   PetscFunctionBegin;
29983c2c1871SHong Zhang   if (!logkey_seqstompinum) {
29993c2c1871SHong Zhang     ierr = PetscLogEventRegister(&logkey_seqstompinum,"MatMerge_SeqsToMPINumeric",MAT_COOKIE);
30003c2c1871SHong Zhang   }
30013c2c1871SHong Zhang   ierr = PetscLogEventBegin(logkey_seqstompinum,seqmat,0,0,0);CHKERRQ(ierr);
30023c2c1871SHong Zhang 
300355d1abb9SHong Zhang   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
300455d1abb9SHong Zhang   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
300555d1abb9SHong Zhang 
300655d1abb9SHong Zhang   ierr = PetscObjectQuery((PetscObject)mpimat,"MatMergeSeqsToMPI",(PetscObject *)&container);CHKERRQ(ierr);
300755d1abb9SHong Zhang   if (container) {
300855d1abb9SHong Zhang     ierr  = PetscObjectContainerGetPointer(container,(void **)&merge);CHKERRQ(ierr);
300955d1abb9SHong Zhang   }
301055d1abb9SHong Zhang   bi     = merge->bi;
301155d1abb9SHong Zhang   bj     = merge->bj;
301255d1abb9SHong Zhang   buf_ri = merge->buf_ri;
301355d1abb9SHong Zhang   buf_rj = merge->buf_rj;
301455d1abb9SHong Zhang 
301555d1abb9SHong Zhang   ierr   = PetscMalloc(size*sizeof(MPI_Status),&status);CHKERRQ(ierr);
3016357abbc8SBarry Smith   owners = merge->rowmap.range;
301755d1abb9SHong Zhang   len_s  = merge->len_s;
301855d1abb9SHong Zhang 
301955d1abb9SHong Zhang   /* send and recv matrix values */
302055d1abb9SHong Zhang   /*-----------------------------*/
3021357abbc8SBarry Smith   ierr = PetscObjectGetNewTag((PetscObject)mpimat,&taga);CHKERRQ(ierr);
302255d1abb9SHong Zhang   ierr = PetscPostIrecvScalar(comm,taga,merge->nrecv,merge->id_r,merge->len_r,&abuf_r,&r_waits);CHKERRQ(ierr);
302355d1abb9SHong Zhang 
302455d1abb9SHong Zhang   ierr = PetscMalloc((merge->nsend+1)*sizeof(MPI_Request),&s_waits);CHKERRQ(ierr);
302555d1abb9SHong Zhang   for (proc=0,k=0; proc<size; proc++){
302655d1abb9SHong Zhang     if (!len_s[proc]) continue;
302755d1abb9SHong Zhang     i = owners[proc];
302855d1abb9SHong Zhang     ierr = MPI_Isend(aa+ai[i],len_s[proc],MPIU_MATSCALAR,proc,taga,comm,s_waits+k);CHKERRQ(ierr);
302955d1abb9SHong Zhang     k++;
303055d1abb9SHong Zhang   }
303155d1abb9SHong Zhang 
30320c468ba9SBarry Smith   if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,r_waits,status);CHKERRQ(ierr);}
30330c468ba9SBarry Smith   if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,s_waits,status);CHKERRQ(ierr);}
303455d1abb9SHong Zhang   ierr = PetscFree(status);CHKERRQ(ierr);
303555d1abb9SHong Zhang 
303655d1abb9SHong Zhang   ierr = PetscFree(s_waits);CHKERRQ(ierr);
303755d1abb9SHong Zhang   ierr = PetscFree(r_waits);CHKERRQ(ierr);
303855d1abb9SHong Zhang 
303955d1abb9SHong Zhang   /* insert mat values of mpimat */
304055d1abb9SHong Zhang   /*----------------------------*/
304155d1abb9SHong Zhang   ierr = PetscMalloc(N*sizeof(MatScalar),&ba_i);CHKERRQ(ierr);
3042b1d57f15SBarry Smith   ierr = PetscMalloc((3*merge->nrecv+1)*sizeof(PetscInt**),&buf_ri_k);CHKERRQ(ierr);
304355d1abb9SHong Zhang   nextrow = buf_ri_k + merge->nrecv;
304455d1abb9SHong Zhang   nextai  = nextrow + merge->nrecv;
304555d1abb9SHong Zhang 
304655d1abb9SHong Zhang   for (k=0; k<merge->nrecv; k++){
304755d1abb9SHong Zhang     buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
304855d1abb9SHong Zhang     nrows = *(buf_ri_k[k]);
304955d1abb9SHong Zhang     nextrow[k]  = buf_ri_k[k]+1;  /* next row number of k-th recved i-structure */
305055d1abb9SHong Zhang     nextai[k]   = buf_ri_k[k] + (nrows + 1);/* poins to the next i-structure of k-th recved i-structure  */
305155d1abb9SHong Zhang   }
305255d1abb9SHong Zhang 
305355d1abb9SHong Zhang   /* set values of ba */
3054357abbc8SBarry Smith   m = merge->rowmap.n;
305555d1abb9SHong Zhang   for (i=0; i<m; i++) {
305655d1abb9SHong Zhang     arow = owners[rank] + i;
305755d1abb9SHong Zhang     bj_i = bj+bi[i];  /* col indices of the i-th row of mpimat */
305855d1abb9SHong Zhang     bnzi = bi[i+1] - bi[i];
305955d1abb9SHong Zhang     ierr = PetscMemzero(ba_i,bnzi*sizeof(MatScalar));CHKERRQ(ierr);
306055d1abb9SHong Zhang 
306155d1abb9SHong Zhang     /* add local non-zero vals of this proc's seqmat into ba */
306255d1abb9SHong Zhang     anzi = ai[arow+1] - ai[arow];
306355d1abb9SHong Zhang     aj   = a->j + ai[arow];
306455d1abb9SHong Zhang     aa   = a->a + ai[arow];
306555d1abb9SHong Zhang     nextaj = 0;
306655d1abb9SHong Zhang     for (j=0; nextaj<anzi; j++){
306755d1abb9SHong Zhang       if (*(bj_i + j) == aj[nextaj]){ /* bcol == acol */
306855d1abb9SHong Zhang         ba_i[j] += aa[nextaj++];
306955d1abb9SHong Zhang       }
307055d1abb9SHong Zhang     }
307155d1abb9SHong Zhang 
307255d1abb9SHong Zhang     /* add received vals into ba */
307355d1abb9SHong Zhang     for (k=0; k<merge->nrecv; k++){ /* k-th received message */
307455d1abb9SHong Zhang       /* i-th row */
307555d1abb9SHong Zhang       if (i == *nextrow[k]) {
307655d1abb9SHong Zhang         anzi = *(nextai[k]+1) - *nextai[k];
307755d1abb9SHong Zhang         aj   = buf_rj[k] + *(nextai[k]);
307855d1abb9SHong Zhang         aa   = abuf_r[k] + *(nextai[k]);
307955d1abb9SHong Zhang         nextaj = 0;
308055d1abb9SHong Zhang         for (j=0; nextaj<anzi; j++){
308155d1abb9SHong Zhang           if (*(bj_i + j) == aj[nextaj]){ /* bcol == acol */
308255d1abb9SHong Zhang             ba_i[j] += aa[nextaj++];
308355d1abb9SHong Zhang           }
308455d1abb9SHong Zhang         }
308555d1abb9SHong Zhang         nextrow[k]++; nextai[k]++;
308655d1abb9SHong Zhang       }
308755d1abb9SHong Zhang     }
308855d1abb9SHong Zhang     ierr = MatSetValues(mpimat,1,&arow,bnzi,bj_i,ba_i,INSERT_VALUES);CHKERRQ(ierr);
308955d1abb9SHong Zhang   }
309055d1abb9SHong Zhang   ierr = MatAssemblyBegin(mpimat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
309155d1abb9SHong Zhang   ierr = MatAssemblyEnd(mpimat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
309255d1abb9SHong Zhang 
309355d1abb9SHong Zhang   ierr = PetscFree(abuf_r);CHKERRQ(ierr);
309455d1abb9SHong Zhang   ierr = PetscFree(ba_i);CHKERRQ(ierr);
309555d1abb9SHong Zhang   ierr = PetscFree(buf_ri_k);CHKERRQ(ierr);
30963c2c1871SHong Zhang   ierr = PetscLogEventEnd(logkey_seqstompinum,seqmat,0,0,0);CHKERRQ(ierr);
309755d1abb9SHong Zhang   PetscFunctionReturn(0);
309855d1abb9SHong Zhang }
309938f152feSBarry Smith 
31003c2c1871SHong Zhang static PetscEvent logkey_seqstompisym = 0;
310138f152feSBarry Smith #undef __FUNCT__
310238f152feSBarry Smith #define __FUNCT__ "MatMerge_SeqsToMPISymbolic"
3103be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMerge_SeqsToMPISymbolic(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,Mat *mpimat)
3104e5f2cdd8SHong Zhang {
3105f08fae4eSHong Zhang   PetscErrorCode       ierr;
310655a3bba9SHong Zhang   Mat                  B_mpi;
3107c2234fe3SHong Zhang   Mat_SeqAIJ           *a=(Mat_SeqAIJ*)seqmat->data;
3108b1d57f15SBarry Smith   PetscMPIInt          size,rank,tagi,tagj,*len_s,*len_si,*len_ri;
3109b1d57f15SBarry Smith   PetscInt             **buf_rj,**buf_ri,**buf_ri_k;
3110*899cda47SBarry Smith   PetscInt             M=seqmat->rmap.n,N=seqmat->cmap.n,i,*owners,*ai=a->i,*aj=a->j;
3111b1d57f15SBarry Smith   PetscInt             len,proc,*dnz,*onz;
3112b1d57f15SBarry Smith   PetscInt             k,anzi,*bi,*bj,*lnk,nlnk,arow,bnzi,nspacedouble=0;
3113b1d57f15SBarry Smith   PetscInt             nrows,*buf_s,*buf_si,*buf_si_i,**nextrow,**nextai;
311455d1abb9SHong Zhang   MPI_Request          *si_waits,*sj_waits,*ri_waits,*rj_waits;
311558cb9c82SHong Zhang   MPI_Status           *status;
3116a1a86e44SBarry Smith   PetscFreeSpaceList   free_space=PETSC_NULL,current_space=PETSC_NULL;
3117be0fcf8dSHong Zhang   PetscBT              lnkbt;
311851a7d1a8SHong Zhang   Mat_Merge_SeqsToMPI  *merge;
3119671beff6SHong Zhang   PetscObjectContainer container;
312002c68681SHong Zhang 
3121e5f2cdd8SHong Zhang   PetscFunctionBegin;
31223c2c1871SHong Zhang   if (!logkey_seqstompisym) {
31233c2c1871SHong Zhang     ierr = PetscLogEventRegister(&logkey_seqstompisym,"MatMerge_SeqsToMPISymbolic",MAT_COOKIE);
31243c2c1871SHong Zhang   }
31253c2c1871SHong Zhang   ierr = PetscLogEventBegin(logkey_seqstompisym,seqmat,0,0,0);CHKERRQ(ierr);
31263c2c1871SHong Zhang 
312738f152feSBarry Smith   /* make sure it is a PETSc comm */
312838f152feSBarry Smith   ierr = PetscCommDuplicate(comm,&comm,PETSC_NULL);CHKERRQ(ierr);
3129e5f2cdd8SHong Zhang   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
3130e5f2cdd8SHong Zhang   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
313155d1abb9SHong Zhang 
313251a7d1a8SHong Zhang   ierr = PetscNew(Mat_Merge_SeqsToMPI,&merge);CHKERRQ(ierr);
3133c2234fe3SHong Zhang   ierr = PetscMalloc(size*sizeof(MPI_Status),&status);CHKERRQ(ierr);
3134e5f2cdd8SHong Zhang 
31356abd8857SHong Zhang   /* determine row ownership */
3136f08fae4eSHong Zhang   /*---------------------------------------------------------*/
3137*899cda47SBarry Smith   merge->rowmap.n = m;
3138*899cda47SBarry Smith   merge->rowmap.N = M;
3139*899cda47SBarry Smith   ierr = PetscMapInitialize(comm,&merge->rowmap);CHKERRQ(ierr);
3140b1d57f15SBarry Smith   ierr = PetscMalloc(size*sizeof(PetscMPIInt),&len_si);CHKERRQ(ierr);
3141b1d57f15SBarry Smith   ierr = PetscMalloc(size*sizeof(PetscMPIInt),&merge->len_s);CHKERRQ(ierr);
314255d1abb9SHong Zhang 
3143357abbc8SBarry Smith   m      = merge->rowmap.n;
3144357abbc8SBarry Smith   M      = merge->rowmap.N;
3145357abbc8SBarry Smith   owners = merge->rowmap.range;
31466abd8857SHong Zhang 
31476abd8857SHong Zhang   /* determine the number of messages to send, their lengths */
31486abd8857SHong Zhang   /*---------------------------------------------------------*/
31493e06a4e6SHong Zhang   len_s  = merge->len_s;
315051a7d1a8SHong Zhang 
31512257cef7SHong Zhang   len = 0;  /* length of buf_si[] */
3152c2234fe3SHong Zhang   merge->nsend = 0;
3153409913e3SHong Zhang   for (proc=0; proc<size; proc++){
31542257cef7SHong Zhang     len_si[proc] = 0;
31553e06a4e6SHong Zhang     if (proc == rank){
31566abd8857SHong Zhang       len_s[proc] = 0;
31573e06a4e6SHong Zhang     } else {
315802c68681SHong Zhang       len_si[proc] = owners[proc+1] - owners[proc] + 1;
31593e06a4e6SHong Zhang       len_s[proc] = ai[owners[proc+1]] - ai[owners[proc]]; /* num of rows to be sent to [proc] */
31603e06a4e6SHong Zhang     }
31613e06a4e6SHong Zhang     if (len_s[proc]) {
3162c2234fe3SHong Zhang       merge->nsend++;
31632257cef7SHong Zhang       nrows = 0;
31642257cef7SHong Zhang       for (i=owners[proc]; i<owners[proc+1]; i++){
31652257cef7SHong Zhang         if (ai[i+1] > ai[i]) nrows++;
31662257cef7SHong Zhang       }
31672257cef7SHong Zhang       len_si[proc] = 2*(nrows+1);
31682257cef7SHong Zhang       len += len_si[proc];
3169409913e3SHong Zhang     }
317058cb9c82SHong Zhang   }
3171409913e3SHong Zhang 
31722257cef7SHong Zhang   /* determine the number and length of messages to receive for ij-structure */
31732257cef7SHong Zhang   /*-------------------------------------------------------------------------*/
317451a7d1a8SHong Zhang   ierr = PetscGatherNumberOfMessages(comm,PETSC_NULL,len_s,&merge->nrecv);CHKERRQ(ierr);
317555d1abb9SHong Zhang   ierr = PetscGatherMessageLengths2(comm,merge->nsend,merge->nrecv,len_s,len_si,&merge->id_r,&merge->len_r,&len_ri);CHKERRQ(ierr);
3176671beff6SHong Zhang 
31773e06a4e6SHong Zhang   /* post the Irecv of j-structure */
31783e06a4e6SHong Zhang   /*-------------------------------*/
3179357abbc8SBarry Smith   ierr = PetscObjectGetNewTag((PetscObject)mpimat,&tagj);CHKERRQ(ierr);
31803e06a4e6SHong Zhang   ierr = PetscPostIrecvInt(comm,tagj,merge->nrecv,merge->id_r,merge->len_r,&buf_rj,&rj_waits);CHKERRQ(ierr);
318102c68681SHong Zhang 
31823e06a4e6SHong Zhang   /* post the Isend of j-structure */
3183affca5deSHong Zhang   /*--------------------------------*/
31842257cef7SHong Zhang   ierr = PetscMalloc((2*merge->nsend+1)*sizeof(MPI_Request),&si_waits);CHKERRQ(ierr);
318502c68681SHong Zhang   sj_waits = si_waits + merge->nsend;
31863e06a4e6SHong Zhang 
31872257cef7SHong Zhang   for (proc=0, k=0; proc<size; proc++){
3188409913e3SHong Zhang     if (!len_s[proc]) continue;
318902c68681SHong Zhang     i = owners[proc];
3190b1d57f15SBarry Smith     ierr = MPI_Isend(aj+ai[i],len_s[proc],MPIU_INT,proc,tagj,comm,sj_waits+k);CHKERRQ(ierr);
319151a7d1a8SHong Zhang     k++;
319251a7d1a8SHong Zhang   }
319351a7d1a8SHong Zhang 
31943e06a4e6SHong Zhang   /* receives and sends of j-structure are complete */
31953e06a4e6SHong Zhang   /*------------------------------------------------*/
31960c468ba9SBarry Smith   if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,rj_waits,status);CHKERRQ(ierr);}
31970c468ba9SBarry Smith   if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,sj_waits,status);CHKERRQ(ierr);}
319802c68681SHong Zhang 
319902c68681SHong Zhang   /* send and recv i-structure */
320002c68681SHong Zhang   /*---------------------------*/
3201357abbc8SBarry Smith   ierr = PetscObjectGetNewTag((PetscObject)merge,&tagi);CHKERRQ(ierr);
320202c68681SHong Zhang   ierr = PetscPostIrecvInt(comm,tagi,merge->nrecv,merge->id_r,len_ri,&buf_ri,&ri_waits);CHKERRQ(ierr);
320302c68681SHong Zhang 
3204b1d57f15SBarry Smith   ierr = PetscMalloc((len+1)*sizeof(PetscInt),&buf_s);CHKERRQ(ierr);
32053e06a4e6SHong Zhang   buf_si = buf_s;  /* points to the beginning of k-th msg to be sent */
32062257cef7SHong Zhang   for (proc=0,k=0; proc<size; proc++){
320702c68681SHong Zhang     if (!len_s[proc]) continue;
32083e06a4e6SHong Zhang     /* form outgoing message for i-structure:
32093e06a4e6SHong Zhang          buf_si[0]:                 nrows to be sent
32103e06a4e6SHong Zhang                [1:nrows]:           row index (global)
32113e06a4e6SHong Zhang                [nrows+1:2*nrows+1]: i-structure index
32123e06a4e6SHong Zhang     */
32133e06a4e6SHong Zhang     /*-------------------------------------------*/
32142257cef7SHong Zhang     nrows = len_si[proc]/2 - 1;
32153e06a4e6SHong Zhang     buf_si_i    = buf_si + nrows+1;
32163e06a4e6SHong Zhang     buf_si[0]   = nrows;
32173e06a4e6SHong Zhang     buf_si_i[0] = 0;
32183e06a4e6SHong Zhang     nrows = 0;
32193e06a4e6SHong Zhang     for (i=owners[proc]; i<owners[proc+1]; i++){
32203e06a4e6SHong Zhang       anzi = ai[i+1] - ai[i];
32213e06a4e6SHong Zhang       if (anzi) {
32223e06a4e6SHong Zhang         buf_si_i[nrows+1] = buf_si_i[nrows] + anzi; /* i-structure */
32233e06a4e6SHong Zhang         buf_si[nrows+1] = i-owners[proc]; /* local row index */
32243e06a4e6SHong Zhang         nrows++;
32253e06a4e6SHong Zhang       }
32263e06a4e6SHong Zhang     }
3227b1d57f15SBarry Smith     ierr = MPI_Isend(buf_si,len_si[proc],MPIU_INT,proc,tagi,comm,si_waits+k);CHKERRQ(ierr);
322802c68681SHong Zhang     k++;
32292257cef7SHong Zhang     buf_si += len_si[proc];
323002c68681SHong Zhang   }
32312257cef7SHong Zhang 
32320c468ba9SBarry Smith   if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,ri_waits,status);CHKERRQ(ierr);}
32330c468ba9SBarry Smith   if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,si_waits,status);CHKERRQ(ierr);}
323402c68681SHong Zhang 
3235ae15b995SBarry Smith   ierr = PetscInfo2(seqmat,"nsend: %D, nrecv: %D\n",merge->nsend,merge->nrecv);CHKERRQ(ierr);
32363e06a4e6SHong Zhang   for (i=0; i<merge->nrecv; i++){
3237ae15b995SBarry 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);
32383e06a4e6SHong Zhang   }
32393e06a4e6SHong Zhang 
32403e06a4e6SHong Zhang   ierr = PetscFree(len_si);CHKERRQ(ierr);
324102c68681SHong Zhang   ierr = PetscFree(len_ri);CHKERRQ(ierr);
324202c68681SHong Zhang   ierr = PetscFree(rj_waits);CHKERRQ(ierr);
32433e06a4e6SHong Zhang   ierr = PetscFree(si_waits);CHKERRQ(ierr);
32442257cef7SHong Zhang   ierr = PetscFree(ri_waits);CHKERRQ(ierr);
32453e06a4e6SHong Zhang   ierr = PetscFree(buf_s);CHKERRQ(ierr);
3246bcc1bcd5SHong Zhang   ierr = PetscFree(status);CHKERRQ(ierr);
324758cb9c82SHong Zhang 
3248bcc1bcd5SHong Zhang   /* compute a local seq matrix in each processor */
3249bcc1bcd5SHong Zhang   /*----------------------------------------------*/
325058cb9c82SHong Zhang   /* allocate bi array and free space for accumulating nonzero column info */
3251b1d57f15SBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&bi);CHKERRQ(ierr);
325258cb9c82SHong Zhang   bi[0] = 0;
325358cb9c82SHong Zhang 
3254be0fcf8dSHong Zhang   /* create and initialize a linked list */
3255be0fcf8dSHong Zhang   nlnk = N+1;
3256be0fcf8dSHong Zhang   ierr = PetscLLCreate(N,N,nlnk,lnk,lnkbt);CHKERRQ(ierr);
325758cb9c82SHong Zhang 
3258bcc1bcd5SHong Zhang   /* initial FreeSpace size is 2*(num of local nnz(seqmat)) */
325958cb9c82SHong Zhang   len = 0;
3260bcc1bcd5SHong Zhang   len  = ai[owners[rank+1]] - ai[owners[rank]];
3261a1a86e44SBarry Smith   ierr = PetscFreeSpaceGet((PetscInt)(2*len+1),&free_space);CHKERRQ(ierr);
326258cb9c82SHong Zhang   current_space = free_space;
326358cb9c82SHong Zhang 
3264bcc1bcd5SHong Zhang   /* determine symbolic info for each local row */
3265b1d57f15SBarry Smith   ierr = PetscMalloc((3*merge->nrecv+1)*sizeof(PetscInt**),&buf_ri_k);CHKERRQ(ierr);
32663e06a4e6SHong Zhang   nextrow = buf_ri_k + merge->nrecv;
32673e06a4e6SHong Zhang   nextai  = nextrow + merge->nrecv;
32683e06a4e6SHong Zhang   for (k=0; k<merge->nrecv; k++){
32692257cef7SHong Zhang     buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
32703e06a4e6SHong Zhang     nrows = *buf_ri_k[k];
32713e06a4e6SHong Zhang     nextrow[k]  = buf_ri_k[k] + 1;  /* next row number of k-th recved i-structure */
32722257cef7SHong Zhang     nextai[k]   = buf_ri_k[k] + (nrows + 1);/* poins to the next i-structure of k-th recved i-structure  */
32733e06a4e6SHong Zhang   }
32742257cef7SHong Zhang 
3275bcc1bcd5SHong Zhang   ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr);
3276bcc1bcd5SHong Zhang   len = 0;
327758cb9c82SHong Zhang   for (i=0;i<m;i++) {
327858cb9c82SHong Zhang     bnzi   = 0;
327958cb9c82SHong Zhang     /* add local non-zero cols of this proc's seqmat into lnk */
328058cb9c82SHong Zhang     arow   = owners[rank] + i;
328158cb9c82SHong Zhang     anzi   = ai[arow+1] - ai[arow];
328258cb9c82SHong Zhang     aj     = a->j + ai[arow];
3283be0fcf8dSHong Zhang     ierr = PetscLLAdd(anzi,aj,N,nlnk,lnk,lnkbt);CHKERRQ(ierr);
328458cb9c82SHong Zhang     bnzi += nlnk;
328558cb9c82SHong Zhang     /* add received col data into lnk */
328651a7d1a8SHong Zhang     for (k=0; k<merge->nrecv; k++){ /* k-th received message */
328755d1abb9SHong Zhang       if (i == *nextrow[k]) { /* i-th row */
32883e06a4e6SHong Zhang         anzi = *(nextai[k]+1) - *nextai[k];
32893e06a4e6SHong Zhang         aj   = buf_rj[k] + *nextai[k];
32903e06a4e6SHong Zhang         ierr = PetscLLAdd(anzi,aj,N,nlnk,lnk,lnkbt);CHKERRQ(ierr);
32913e06a4e6SHong Zhang         bnzi += nlnk;
32923e06a4e6SHong Zhang         nextrow[k]++; nextai[k]++;
32933e06a4e6SHong Zhang       }
329458cb9c82SHong Zhang     }
3295bcc1bcd5SHong Zhang     if (len < bnzi) len = bnzi;  /* =max(bnzi) */
329658cb9c82SHong Zhang 
329758cb9c82SHong Zhang     /* if free space is not available, make more free space */
329858cb9c82SHong Zhang     if (current_space->local_remaining<bnzi) {
3299a1a86e44SBarry Smith       ierr = PetscFreeSpaceGet(current_space->total_array_size,&current_space);CHKERRQ(ierr);
330058cb9c82SHong Zhang       nspacedouble++;
330158cb9c82SHong Zhang     }
330258cb9c82SHong Zhang     /* copy data into free space, then initialize lnk */
3303be0fcf8dSHong Zhang     ierr = PetscLLClean(N,N,bnzi,lnk,current_space->array,lnkbt);CHKERRQ(ierr);
3304bcc1bcd5SHong Zhang     ierr = MatPreallocateSet(i+owners[rank],bnzi,current_space->array,dnz,onz);CHKERRQ(ierr);
3305bcc1bcd5SHong Zhang 
330658cb9c82SHong Zhang     current_space->array           += bnzi;
330758cb9c82SHong Zhang     current_space->local_used      += bnzi;
330858cb9c82SHong Zhang     current_space->local_remaining -= bnzi;
330958cb9c82SHong Zhang 
331058cb9c82SHong Zhang     bi[i+1] = bi[i] + bnzi;
331158cb9c82SHong Zhang   }
3312bcc1bcd5SHong Zhang 
3313bcc1bcd5SHong Zhang   ierr = PetscFree(buf_ri_k);CHKERRQ(ierr);
3314bcc1bcd5SHong Zhang 
3315b1d57f15SBarry Smith   ierr = PetscMalloc((bi[m]+1)*sizeof(PetscInt),&bj);CHKERRQ(ierr);
3316a1a86e44SBarry Smith   ierr = PetscFreeSpaceContiguous(&free_space,bj);CHKERRQ(ierr);
3317be0fcf8dSHong Zhang   ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr);
3318409913e3SHong Zhang 
3319bcc1bcd5SHong Zhang   /* create symbolic parallel matrix B_mpi */
3320bcc1bcd5SHong Zhang   /*---------------------------------------*/
3321f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,&B_mpi);CHKERRQ(ierr);
332254b84b50SHong Zhang   if (n==PETSC_DECIDE) {
3323f69a0ea3SMatthew Knepley     ierr = MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,N);CHKERRQ(ierr);
332454b84b50SHong Zhang   } else {
3325f69a0ea3SMatthew Knepley     ierr = MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
332654b84b50SHong Zhang   }
3327bcc1bcd5SHong Zhang   ierr = MatSetType(B_mpi,MATMPIAIJ);CHKERRQ(ierr);
3328bcc1bcd5SHong Zhang   ierr = MatMPIAIJSetPreallocation(B_mpi,0,dnz,0,onz);CHKERRQ(ierr);
3329bcc1bcd5SHong Zhang   ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr);
333058cb9c82SHong Zhang 
33316abd8857SHong Zhang   /* B_mpi is not ready for use - assembly will be done by MatMerge_SeqsToMPINumeric() */
33326abd8857SHong Zhang   B_mpi->assembled     = PETSC_FALSE;
3333affca5deSHong Zhang   B_mpi->ops->destroy  = MatDestroy_MPIAIJ_SeqsToMPI;
3334affca5deSHong Zhang   merge->bi            = bi;
3335affca5deSHong Zhang   merge->bj            = bj;
333602c68681SHong Zhang   merge->buf_ri        = buf_ri;
333702c68681SHong Zhang   merge->buf_rj        = buf_rj;
3338de0260b3SHong Zhang   merge->coi           = PETSC_NULL;
3339de0260b3SHong Zhang   merge->coj           = PETSC_NULL;
3340de0260b3SHong Zhang   merge->owners_co     = PETSC_NULL;
3341affca5deSHong Zhang 
3342affca5deSHong Zhang   /* attach the supporting struct to B_mpi for reuse */
3343affca5deSHong Zhang   ierr = PetscObjectContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr);
3344affca5deSHong Zhang   ierr = PetscObjectContainerSetPointer(container,merge);CHKERRQ(ierr);
3345affca5deSHong Zhang   ierr = PetscObjectCompose((PetscObject)B_mpi,"MatMergeSeqsToMPI",(PetscObject)container);CHKERRQ(ierr);
3346affca5deSHong Zhang   *mpimat = B_mpi;
334738f152feSBarry Smith 
334838f152feSBarry Smith   ierr = PetscCommDestroy(&comm);CHKERRQ(ierr);
33493c2c1871SHong Zhang   ierr = PetscLogEventEnd(logkey_seqstompisym,seqmat,0,0,0);CHKERRQ(ierr);
3350e5f2cdd8SHong Zhang   PetscFunctionReturn(0);
3351e5f2cdd8SHong Zhang }
335225616d81SHong Zhang 
3353e462e02cSHong Zhang static PetscEvent logkey_seqstompi = 0;
335438f152feSBarry Smith #undef __FUNCT__
335538f152feSBarry Smith #define __FUNCT__ "MatMerge_SeqsToMPI"
3356be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMerge_SeqsToMPI(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,MatReuse scall,Mat *mpimat)
335755d1abb9SHong Zhang {
335855d1abb9SHong Zhang   PetscErrorCode   ierr;
335955d1abb9SHong Zhang 
336055d1abb9SHong Zhang   PetscFunctionBegin;
3361e462e02cSHong Zhang   if (!logkey_seqstompi) {
3362e462e02cSHong Zhang     ierr = PetscLogEventRegister(&logkey_seqstompi,"MatMerge_SeqsToMPI",MAT_COOKIE);
3363e462e02cSHong Zhang   }
3364e462e02cSHong Zhang   ierr = PetscLogEventBegin(logkey_seqstompi,seqmat,0,0,0);CHKERRQ(ierr);
336555d1abb9SHong Zhang   if (scall == MAT_INITIAL_MATRIX){
336655d1abb9SHong Zhang     ierr = MatMerge_SeqsToMPISymbolic(comm,seqmat,m,n,mpimat);CHKERRQ(ierr);
336755d1abb9SHong Zhang   }
336855d1abb9SHong Zhang   ierr = MatMerge_SeqsToMPINumeric(seqmat,*mpimat);CHKERRQ(ierr);
3369e462e02cSHong Zhang   ierr = PetscLogEventEnd(logkey_seqstompi,seqmat,0,0,0);CHKERRQ(ierr);
337055d1abb9SHong Zhang   PetscFunctionReturn(0);
337155d1abb9SHong Zhang }
3372a61c8c0fSHong Zhang static PetscEvent logkey_getlocalmat = 0;
337325616d81SHong Zhang #undef __FUNCT__
337425616d81SHong Zhang #define __FUNCT__ "MatGetLocalMat"
337525616d81SHong Zhang /*@C
337632fba14fSHong Zhang      MatGetLocalMat - Creates a SeqAIJ matrix by taking all its local rows
337725616d81SHong Zhang 
337832fba14fSHong Zhang     Not Collective
337925616d81SHong Zhang 
338025616d81SHong Zhang    Input Parameters:
338125616d81SHong Zhang +    A - the matrix
338225616d81SHong Zhang .    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
338325616d81SHong Zhang 
338425616d81SHong Zhang    Output Parameter:
338525616d81SHong Zhang .    A_loc - the local sequential matrix generated
338625616d81SHong Zhang 
338725616d81SHong Zhang     Level: developer
338825616d81SHong Zhang 
338925616d81SHong Zhang @*/
3390be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatGetLocalMat(Mat A,MatReuse scall,Mat *A_loc)
339125616d81SHong Zhang {
339225616d81SHong Zhang   PetscErrorCode  ierr;
339301b7ae99SHong Zhang   Mat_MPIAIJ      *mpimat=(Mat_MPIAIJ*)A->data;
339401b7ae99SHong Zhang   Mat_SeqAIJ      *mat,*a=(Mat_SeqAIJ*)(mpimat->A)->data,*b=(Mat_SeqAIJ*)(mpimat->B)->data;
339501b7ae99SHong Zhang   PetscInt        *ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j,*cmap=mpimat->garray;
3396dea91ad1SHong Zhang   PetscScalar     *aa=a->a,*ba=b->a,*ca;
3397*899cda47SBarry Smith   PetscInt        am=A->rmap.n,i,j,k,cstart=A->cmap.rstart;
33985a7d977cSHong Zhang   PetscInt        *ci,*cj,col,ncols_d,ncols_o,jo;
339925616d81SHong Zhang 
340025616d81SHong Zhang   PetscFunctionBegin;
3401e462e02cSHong Zhang   if (!logkey_getlocalmat) {
3402e462e02cSHong Zhang     ierr = PetscLogEventRegister(&logkey_getlocalmat,"MatGetLocalMat",MAT_COOKIE);
3403e462e02cSHong Zhang   }
3404e462e02cSHong Zhang   ierr = PetscLogEventBegin(logkey_getlocalmat,A,0,0,0);CHKERRQ(ierr);
340501b7ae99SHong Zhang   if (scall == MAT_INITIAL_MATRIX){
3406dea91ad1SHong Zhang     ierr = PetscMalloc((1+am)*sizeof(PetscInt),&ci);CHKERRQ(ierr);
3407dea91ad1SHong Zhang     ci[0] = 0;
340801b7ae99SHong Zhang     for (i=0; i<am; i++){
3409dea91ad1SHong Zhang       ci[i+1] = ci[i] + (ai[i+1] - ai[i]) + (bi[i+1] - bi[i]);
341001b7ae99SHong Zhang     }
3411dea91ad1SHong Zhang     ierr = PetscMalloc((1+ci[am])*sizeof(PetscInt),&cj);CHKERRQ(ierr);
3412dea91ad1SHong Zhang     ierr = PetscMalloc((1+ci[am])*sizeof(PetscScalar),&ca);CHKERRQ(ierr);
3413dea91ad1SHong Zhang     k = 0;
341401b7ae99SHong Zhang     for (i=0; i<am; i++) {
34155a7d977cSHong Zhang       ncols_o = bi[i+1] - bi[i];
34165a7d977cSHong Zhang       ncols_d = ai[i+1] - ai[i];
341701b7ae99SHong Zhang       /* off-diagonal portion of A */
34185a7d977cSHong Zhang       for (jo=0; jo<ncols_o; jo++) {
34195a7d977cSHong Zhang         col = cmap[*bj];
34205a7d977cSHong Zhang         if (col >= cstart) break;
34215a7d977cSHong Zhang         cj[k]   = col; bj++;
34225a7d977cSHong Zhang         ca[k++] = *ba++;
34235a7d977cSHong Zhang       }
34245a7d977cSHong Zhang       /* diagonal portion of A */
34255a7d977cSHong Zhang       for (j=0; j<ncols_d; j++) {
34265a7d977cSHong Zhang         cj[k]   = cstart + *aj++;
34275a7d977cSHong Zhang         ca[k++] = *aa++;
34285a7d977cSHong Zhang       }
34295a7d977cSHong Zhang       /* off-diagonal portion of A */
34305a7d977cSHong Zhang       for (j=jo; j<ncols_o; j++) {
34315a7d977cSHong Zhang         cj[k]   = cmap[*bj++];
34325a7d977cSHong Zhang         ca[k++] = *ba++;
34335a7d977cSHong Zhang       }
343425616d81SHong Zhang     }
3435dea91ad1SHong Zhang     /* put together the new matrix */
3436*899cda47SBarry Smith     ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,am,A->cmap.N,ci,cj,ca,A_loc);CHKERRQ(ierr);
3437dea91ad1SHong Zhang     /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
3438dea91ad1SHong Zhang     /* Since these are PETSc arrays, change flags to free them as necessary. */
3439dea91ad1SHong Zhang     mat = (Mat_SeqAIJ*)(*A_loc)->data;
3440dea91ad1SHong Zhang     mat->freedata = PETSC_TRUE;
3441dea91ad1SHong Zhang     mat->nonew    = 0;
34425a7d977cSHong Zhang   } else if (scall == MAT_REUSE_MATRIX){
34435a7d977cSHong Zhang     mat=(Mat_SeqAIJ*)(*A_loc)->data;
34445a7d977cSHong Zhang     ci = mat->i; cj = mat->j; ca = mat->a;
34455a7d977cSHong Zhang     for (i=0; i<am; i++) {
34465a7d977cSHong Zhang       /* off-diagonal portion of A */
34475a7d977cSHong Zhang       ncols_o = bi[i+1] - bi[i];
34485a7d977cSHong Zhang       for (jo=0; jo<ncols_o; jo++) {
34495a7d977cSHong Zhang         col = cmap[*bj];
34505a7d977cSHong Zhang         if (col >= cstart) break;
3451f33d1a9aSHong Zhang         *ca++ = *ba++; bj++;
34525a7d977cSHong Zhang       }
34535a7d977cSHong Zhang       /* diagonal portion of A */
3454ecc9b87dSHong Zhang       ncols_d = ai[i+1] - ai[i];
3455ecc9b87dSHong Zhang       for (j=0; j<ncols_d; j++) *ca++ = *aa++;
34565a7d977cSHong Zhang       /* off-diagonal portion of A */
3457f33d1a9aSHong Zhang       for (j=jo; j<ncols_o; j++) {
3458f33d1a9aSHong Zhang         *ca++ = *ba++; bj++;
3459f33d1a9aSHong Zhang       }
34605a7d977cSHong Zhang     }
34615a7d977cSHong Zhang   } else {
34625a7d977cSHong Zhang     SETERRQ1(PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall);
346325616d81SHong Zhang   }
346401b7ae99SHong Zhang 
3465e462e02cSHong Zhang   ierr = PetscLogEventEnd(logkey_getlocalmat,A,0,0,0);CHKERRQ(ierr);
346625616d81SHong Zhang   PetscFunctionReturn(0);
346725616d81SHong Zhang }
346825616d81SHong Zhang 
346932fba14fSHong Zhang static PetscEvent logkey_getlocalmatcondensed = 0;
347032fba14fSHong Zhang #undef __FUNCT__
347132fba14fSHong Zhang #define __FUNCT__ "MatGetLocalMatCondensed"
347232fba14fSHong Zhang /*@C
347332fba14fSHong Zhang      MatGetLocalMatCondensed - Creates a SeqAIJ matrix by taking all its local rows and NON-ZERO columns
347432fba14fSHong Zhang 
347532fba14fSHong Zhang     Not Collective
347632fba14fSHong Zhang 
347732fba14fSHong Zhang    Input Parameters:
347832fba14fSHong Zhang +    A - the matrix
347932fba14fSHong Zhang .    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
348032fba14fSHong Zhang -    row, col - index sets of rows and columns to extract (or PETSC_NULL)
348132fba14fSHong Zhang 
348232fba14fSHong Zhang    Output Parameter:
348332fba14fSHong Zhang .    A_loc - the local sequential matrix generated
348432fba14fSHong Zhang 
348532fba14fSHong Zhang     Level: developer
348632fba14fSHong Zhang 
348732fba14fSHong Zhang @*/
3488be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatGetLocalMatCondensed(Mat A,MatReuse scall,IS *row,IS *col,Mat *A_loc)
348932fba14fSHong Zhang {
349032fba14fSHong Zhang   Mat_MPIAIJ        *a=(Mat_MPIAIJ*)A->data;
349132fba14fSHong Zhang   PetscErrorCode    ierr;
349232fba14fSHong Zhang   PetscInt          i,start,end,ncols,nzA,nzB,*cmap,imark,*idx;
349332fba14fSHong Zhang   IS                isrowa,iscola;
349432fba14fSHong Zhang   Mat               *aloc;
349532fba14fSHong Zhang 
349632fba14fSHong Zhang   PetscFunctionBegin;
349732fba14fSHong Zhang   if (!logkey_getlocalmatcondensed) {
349832fba14fSHong Zhang     ierr = PetscLogEventRegister(&logkey_getlocalmatcondensed,"MatGetLocalMatCondensed",MAT_COOKIE);
349932fba14fSHong Zhang   }
350032fba14fSHong Zhang   ierr = PetscLogEventBegin(logkey_getlocalmatcondensed,A,0,0,0);CHKERRQ(ierr);
350132fba14fSHong Zhang   if (!row){
3502*899cda47SBarry Smith     start = A->rmap.rstart; end = A->rmap.rend;
350332fba14fSHong Zhang     ierr = ISCreateStride(PETSC_COMM_SELF,end-start,start,1,&isrowa);CHKERRQ(ierr);
350432fba14fSHong Zhang   } else {
350532fba14fSHong Zhang     isrowa = *row;
350632fba14fSHong Zhang   }
350732fba14fSHong Zhang   if (!col){
3508*899cda47SBarry Smith     start = A->cmap.rstart;
350932fba14fSHong Zhang     cmap  = a->garray;
3510*899cda47SBarry Smith     nzA   = a->A->cmap.n;
3511*899cda47SBarry Smith     nzB   = a->B->cmap.n;
351232fba14fSHong Zhang     ierr  = PetscMalloc((nzA+nzB)*sizeof(PetscInt), &idx);CHKERRQ(ierr);
351332fba14fSHong Zhang     ncols = 0;
351432fba14fSHong Zhang     for (i=0; i<nzB; i++) {
351532fba14fSHong Zhang       if (cmap[i] < start) idx[ncols++] = cmap[i];
351632fba14fSHong Zhang       else break;
351732fba14fSHong Zhang     }
351832fba14fSHong Zhang     imark = i;
351932fba14fSHong Zhang     for (i=0; i<nzA; i++) idx[ncols++] = start + i;
352032fba14fSHong Zhang     for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i];
352132fba14fSHong Zhang     ierr = ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,&iscola);CHKERRQ(ierr);
352232fba14fSHong Zhang     ierr = PetscFree(idx);CHKERRQ(ierr);
352332fba14fSHong Zhang   } else {
352432fba14fSHong Zhang     iscola = *col;
352532fba14fSHong Zhang   }
352632fba14fSHong Zhang   if (scall != MAT_INITIAL_MATRIX){
352732fba14fSHong Zhang     ierr = PetscMalloc(sizeof(Mat),&aloc);CHKERRQ(ierr);
352832fba14fSHong Zhang     aloc[0] = *A_loc;
352932fba14fSHong Zhang   }
353032fba14fSHong Zhang   ierr = MatGetSubMatrices(A,1,&isrowa,&iscola,scall,&aloc);CHKERRQ(ierr);
353132fba14fSHong Zhang   *A_loc = aloc[0];
353232fba14fSHong Zhang   ierr = PetscFree(aloc);CHKERRQ(ierr);
353332fba14fSHong Zhang   if (!row){
353432fba14fSHong Zhang     ierr = ISDestroy(isrowa);CHKERRQ(ierr);
353532fba14fSHong Zhang   }
353632fba14fSHong Zhang   if (!col){
353732fba14fSHong Zhang     ierr = ISDestroy(iscola);CHKERRQ(ierr);
353832fba14fSHong Zhang   }
353932fba14fSHong Zhang   ierr = PetscLogEventEnd(logkey_getlocalmatcondensed,A,0,0,0);CHKERRQ(ierr);
354032fba14fSHong Zhang   PetscFunctionReturn(0);
354132fba14fSHong Zhang }
354232fba14fSHong Zhang 
3543a61c8c0fSHong Zhang static PetscEvent logkey_GetBrowsOfAcols = 0;
354425616d81SHong Zhang #undef __FUNCT__
354525616d81SHong Zhang #define __FUNCT__ "MatGetBrowsOfAcols"
354625616d81SHong Zhang /*@C
354732fba14fSHong Zhang     MatGetBrowsOfAcols - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns of local A
354825616d81SHong Zhang 
354925616d81SHong Zhang     Collective on Mat
355025616d81SHong Zhang 
355125616d81SHong Zhang    Input Parameters:
3552e240928fSHong Zhang +    A,B - the matrices in mpiaij format
355325616d81SHong Zhang .    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
355425616d81SHong Zhang -    rowb, colb - index sets of rows and columns of B to extract (or PETSC_NULL)
355525616d81SHong Zhang 
355625616d81SHong Zhang    Output Parameter:
355725616d81SHong Zhang +    rowb, colb - index sets of rows and columns of B to extract
3558*899cda47SBarry Smith .    brstart - row index of B_seq from which next B->rmap.n rows are taken from B's local rows
355925616d81SHong Zhang -    B_seq - the sequential matrix generated
356025616d81SHong Zhang 
356125616d81SHong Zhang     Level: developer
356225616d81SHong Zhang 
356325616d81SHong Zhang @*/
3564be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatGetBrowsOfAcols(Mat A,Mat B,MatReuse scall,IS *rowb,IS *colb,PetscInt *brstart,Mat *B_seq)
356525616d81SHong Zhang {
3566*899cda47SBarry Smith   Mat_MPIAIJ        *a=(Mat_MPIAIJ*)A->data;
356725616d81SHong Zhang   PetscErrorCode    ierr;
3568b1d57f15SBarry Smith   PetscInt          *idx,i,start,ncols,nzA,nzB,*cmap,imark;
356925616d81SHong Zhang   IS                isrowb,iscolb;
357025616d81SHong Zhang   Mat               *bseq;
357125616d81SHong Zhang 
357225616d81SHong Zhang   PetscFunctionBegin;
3573*899cda47SBarry Smith   if (A->cmap.rstart != B->rmap.rstart || A->cmap.rend != B->rmap.rend){
3574*899cda47SBarry 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);
357525616d81SHong Zhang   }
3576e462e02cSHong Zhang   if (!logkey_GetBrowsOfAcols) {
3577e462e02cSHong Zhang     ierr = PetscLogEventRegister(&logkey_GetBrowsOfAcols,"MatGetBrowsOfAcols",MAT_COOKIE);
3578e462e02cSHong Zhang   }
3579e462e02cSHong Zhang   ierr = PetscLogEventBegin(logkey_GetBrowsOfAcols,A,B,0,0);CHKERRQ(ierr);
358025616d81SHong Zhang 
358125616d81SHong Zhang   if (scall == MAT_INITIAL_MATRIX){
3582*899cda47SBarry Smith     start = A->cmap.rstart;
358325616d81SHong Zhang     cmap  = a->garray;
3584*899cda47SBarry Smith     nzA   = a->A->cmap.n;
3585*899cda47SBarry Smith     nzB   = a->B->cmap.n;
3586b1d57f15SBarry Smith     ierr  = PetscMalloc((nzA+nzB)*sizeof(PetscInt), &idx);CHKERRQ(ierr);
358725616d81SHong Zhang     ncols = 0;
35880390132cSHong Zhang     for (i=0; i<nzB; i++) {  /* row < local row index */
358925616d81SHong Zhang       if (cmap[i] < start) idx[ncols++] = cmap[i];
359025616d81SHong Zhang       else break;
359125616d81SHong Zhang     }
359225616d81SHong Zhang     imark = i;
35930390132cSHong Zhang     for (i=0; i<nzA; i++) idx[ncols++] = start + i;  /* local rows */
35940390132cSHong Zhang     for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i]; /* row > local row index */
359525616d81SHong Zhang     ierr = ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,&isrowb);CHKERRQ(ierr);
359625616d81SHong Zhang     ierr = PetscFree(idx);CHKERRQ(ierr);
359725616d81SHong Zhang     *brstart = imark;
3598*899cda47SBarry Smith     ierr = ISCreateStride(PETSC_COMM_SELF,B->cmap.N,0,1,&iscolb);CHKERRQ(ierr);
359925616d81SHong Zhang   } else {
360025616d81SHong Zhang     if (!rowb || !colb) SETERRQ(PETSC_ERR_SUP,"IS rowb and colb must be provided for MAT_REUSE_MATRIX");
360125616d81SHong Zhang     isrowb = *rowb; iscolb = *colb;
360225616d81SHong Zhang     ierr = PetscMalloc(sizeof(Mat),&bseq);CHKERRQ(ierr);
360325616d81SHong Zhang     bseq[0] = *B_seq;
360425616d81SHong Zhang   }
360525616d81SHong Zhang   ierr = MatGetSubMatrices(B,1,&isrowb,&iscolb,scall,&bseq);CHKERRQ(ierr);
360625616d81SHong Zhang   *B_seq = bseq[0];
360725616d81SHong Zhang   ierr = PetscFree(bseq);CHKERRQ(ierr);
360825616d81SHong Zhang   if (!rowb){
360925616d81SHong Zhang     ierr = ISDestroy(isrowb);CHKERRQ(ierr);
361025616d81SHong Zhang   } else {
361125616d81SHong Zhang     *rowb = isrowb;
361225616d81SHong Zhang   }
361325616d81SHong Zhang   if (!colb){
361425616d81SHong Zhang     ierr = ISDestroy(iscolb);CHKERRQ(ierr);
361525616d81SHong Zhang   } else {
361625616d81SHong Zhang     *colb = iscolb;
361725616d81SHong Zhang   }
3618e462e02cSHong Zhang   ierr = PetscLogEventEnd(logkey_GetBrowsOfAcols,A,B,0,0);CHKERRQ(ierr);
361925616d81SHong Zhang   PetscFunctionReturn(0);
362025616d81SHong Zhang }
3621429d309bSHong Zhang 
3622a61c8c0fSHong Zhang static PetscEvent logkey_GetBrowsOfAocols = 0;
3623a61c8c0fSHong Zhang #undef __FUNCT__
3624a61c8c0fSHong Zhang #define __FUNCT__ "MatGetBrowsOfAoCols"
3625429d309bSHong Zhang /*@C
3626429d309bSHong Zhang     MatGetBrowsOfAoCols - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns
362701b7ae99SHong Zhang     of the OFF-DIAGONAL portion of local A
3628429d309bSHong Zhang 
3629429d309bSHong Zhang     Collective on Mat
3630429d309bSHong Zhang 
3631429d309bSHong Zhang    Input Parameters:
3632429d309bSHong Zhang +    A,B - the matrices in mpiaij format
363387025532SHong Zhang .    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
363487025532SHong Zhang .    startsj - starting point in B's sending and receiving j-arrays, saved for MAT_REUSE (or PETSC_NULL)
363587025532SHong Zhang -    bufa_ptr - array for sending matrix values, saved for MAT_REUSE (or PETSC_NULL)
3636429d309bSHong Zhang 
3637429d309bSHong Zhang    Output Parameter:
363887025532SHong Zhang +    B_oth - the sequential matrix generated
3639429d309bSHong Zhang 
3640429d309bSHong Zhang     Level: developer
3641429d309bSHong Zhang 
3642429d309bSHong Zhang @*/
3643be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatGetBrowsOfAoCols(Mat A,Mat B,MatReuse scall,PetscInt **startsj,PetscScalar **bufa_ptr,Mat *B_oth)
3644429d309bSHong Zhang {
3645a6b2eed2SHong Zhang   VecScatter_MPI_General *gen_to,*gen_from;
3646429d309bSHong Zhang   PetscErrorCode         ierr;
3647*899cda47SBarry Smith   Mat_MPIAIJ             *a=(Mat_MPIAIJ*)A->data;
364887025532SHong Zhang   Mat_SeqAIJ             *b_oth;
3649a6b2eed2SHong Zhang   VecScatter             ctx=a->Mvctx;
3650a6b2eed2SHong Zhang   MPI_Comm               comm=ctx->comm;
365187025532SHong Zhang   PetscMPIInt            *rprocs,*sprocs,tag=ctx->tag,rank;
3652*899cda47SBarry Smith   PetscInt               *rowlen,*bufj,*bufJ,ncols,aBn=a->B->cmap.n,row,*b_othi,*b_othj;
365387025532SHong Zhang   PetscScalar            *rvalues,*svalues,*b_otha,*bufa,*bufA;
3654d679ab29SBarry Smith   PetscInt               i,k,l,nrecvs,nsends,nrows,*srow,*rstarts,*rstartsj = 0,*sstarts,*sstartsj,len;
3655a6b2eed2SHong Zhang   MPI_Request            *rwaits,*swaits;
365687025532SHong Zhang   MPI_Status             *sstatus,rstatus;
3657ba8c8a56SBarry Smith   PetscInt               *cols;
3658ba8c8a56SBarry Smith   PetscScalar            *vals;
365913f74950SBarry Smith   PetscMPIInt            j;
3660429d309bSHong Zhang 
3661429d309bSHong Zhang   PetscFunctionBegin;
3662*899cda47SBarry Smith   if (A->cmap.rstart != B->rmap.rstart || A->cmap.rend != B->rmap.rend){
3663*899cda47SBarry 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);
3664429d309bSHong Zhang   }
3665429d309bSHong Zhang   if (!logkey_GetBrowsOfAocols) {
36661677a5d7SHong Zhang     ierr = PetscLogEventRegister(&logkey_GetBrowsOfAocols,"MatGetBrAoCol",MAT_COOKIE);
3667429d309bSHong Zhang   }
3668429d309bSHong Zhang   ierr = PetscLogEventBegin(logkey_GetBrowsOfAocols,A,B,0,0);CHKERRQ(ierr);
3669a6b2eed2SHong Zhang   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
3670a6b2eed2SHong Zhang 
3671a6b2eed2SHong Zhang   gen_to   = (VecScatter_MPI_General*)ctx->todata;
3672a6b2eed2SHong Zhang   gen_from = (VecScatter_MPI_General*)ctx->fromdata;
3673a6b2eed2SHong Zhang   rvalues  = gen_from->values; /* holds the length of sending row */
3674a6b2eed2SHong Zhang   svalues  = gen_to->values;   /* holds the length of receiving row */
3675a6b2eed2SHong Zhang   nrecvs   = gen_from->n;
3676a6b2eed2SHong Zhang   nsends   = gen_to->n;
3677a6b2eed2SHong Zhang   rwaits   = gen_from->requests;
3678a6b2eed2SHong Zhang   swaits   = gen_to->requests;
3679a6b2eed2SHong Zhang   srow     = gen_to->indices;   /* local row index to be sent */
3680a6b2eed2SHong Zhang   rstarts  = gen_from->starts;
3681a6b2eed2SHong Zhang   sstarts  = gen_to->starts;
3682a6b2eed2SHong Zhang   rprocs   = gen_from->procs;
3683a6b2eed2SHong Zhang   sprocs   = gen_to->procs;
3684a6b2eed2SHong Zhang   sstatus  = gen_to->sstatus;
3685429d309bSHong Zhang 
3686dea91ad1SHong Zhang   if (!startsj || !bufa_ptr) scall = MAT_INITIAL_MATRIX;
3687429d309bSHong Zhang   if (scall == MAT_INITIAL_MATRIX){
3688a6b2eed2SHong Zhang     /* i-array */
3689a6b2eed2SHong Zhang     /*---------*/
3690a6b2eed2SHong Zhang     /*  post receives */
3691a6b2eed2SHong Zhang     for (i=0; i<nrecvs; i++){
3692a6b2eed2SHong Zhang       rowlen = (PetscInt*)rvalues + rstarts[i];
369387025532SHong Zhang       nrows = rstarts[i+1]-rstarts[i];
369487025532SHong Zhang       ierr = MPI_Irecv(rowlen,nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr);
3695429d309bSHong Zhang     }
3696a6b2eed2SHong Zhang 
3697a6b2eed2SHong Zhang     /* pack the outgoing message */
369887025532SHong Zhang     ierr = PetscMalloc((nsends+nrecvs+3)*sizeof(PetscInt),&sstartsj);CHKERRQ(ierr);
3699a6b2eed2SHong Zhang     rstartsj = sstartsj + nsends +1;
3700a6b2eed2SHong Zhang     sstartsj[0] = 0;  rstartsj[0] = 0;
3701a6b2eed2SHong Zhang     len = 0; /* total length of j or a array to be sent */
3702a6b2eed2SHong Zhang     k = 0;
3703a6b2eed2SHong Zhang     for (i=0; i<nsends; i++){
3704a6b2eed2SHong Zhang       rowlen = (PetscInt*)svalues + sstarts[i];
370587025532SHong Zhang       nrows = sstarts[i+1]-sstarts[i]; /* num of rows */
370687025532SHong Zhang       for (j=0; j<nrows; j++) {
3707*899cda47SBarry Smith         row = srow[k] + B->rmap.range[rank]; /* global row idx */
3708ba8c8a56SBarry Smith         ierr = MatGetRow_MPIAIJ(B,row,&rowlen[j],PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); /* rowlength */
3709a6b2eed2SHong Zhang         len += rowlen[j];
3710ba8c8a56SBarry Smith         ierr = MatRestoreRow_MPIAIJ(B,row,&ncols,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
3711a6b2eed2SHong Zhang         k++;
3712429d309bSHong Zhang       }
371387025532SHong Zhang       ierr = MPI_Isend(rowlen,nrows,MPIU_INT,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr);
3714dea91ad1SHong Zhang        sstartsj[i+1] = len;  /* starting point of (i+1)-th outgoing msg in bufj and bufa */
3715429d309bSHong Zhang     }
371687025532SHong Zhang     /* recvs and sends of i-array are completed */
371787025532SHong Zhang     i = nrecvs;
371887025532SHong Zhang     while (i--) {
371987025532SHong Zhang       ierr = MPI_Waitany(nrecvs,rwaits,&j,&rstatus);CHKERRQ(ierr);
372087025532SHong Zhang     }
37210c468ba9SBarry Smith     if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);}
3722a6b2eed2SHong Zhang     /* allocate buffers for sending j and a arrays */
3723a6b2eed2SHong Zhang     ierr = PetscMalloc((len+1)*sizeof(PetscInt),&bufj);CHKERRQ(ierr);
3724a6b2eed2SHong Zhang     ierr = PetscMalloc((len+1)*sizeof(PetscScalar),&bufa);CHKERRQ(ierr);
3725a6b2eed2SHong Zhang 
372687025532SHong Zhang     /* create i-array of B_oth */
372787025532SHong Zhang     ierr = PetscMalloc((aBn+2)*sizeof(PetscInt),&b_othi);CHKERRQ(ierr);
372887025532SHong Zhang     b_othi[0] = 0;
3729a6b2eed2SHong Zhang     len = 0; /* total length of j or a array to be received */
3730a6b2eed2SHong Zhang     k = 0;
3731a6b2eed2SHong Zhang     for (i=0; i<nrecvs; i++){
3732a6b2eed2SHong Zhang       rowlen = (PetscInt*)rvalues + rstarts[i];
373387025532SHong Zhang       nrows = rstarts[i+1]-rstarts[i];
373487025532SHong Zhang       for (j=0; j<nrows; j++) {
373587025532SHong Zhang         b_othi[k+1] = b_othi[k] + rowlen[j];
3736a6b2eed2SHong Zhang         len += rowlen[j]; k++;
3737a6b2eed2SHong Zhang       }
3738dea91ad1SHong Zhang       rstartsj[i+1] = len; /* starting point of (i+1)-th incoming msg in bufj and bufa */
3739a6b2eed2SHong Zhang     }
3740a6b2eed2SHong Zhang 
374187025532SHong Zhang     /* allocate space for j and a arrrays of B_oth */
374287025532SHong Zhang     ierr = PetscMalloc((b_othi[aBn]+1)*sizeof(PetscInt),&b_othj);CHKERRQ(ierr);
374387025532SHong Zhang     ierr = PetscMalloc((b_othi[aBn]+1)*sizeof(PetscScalar),&b_otha);CHKERRQ(ierr);
3744a6b2eed2SHong Zhang 
374587025532SHong Zhang     /* j-array */
374687025532SHong Zhang     /*---------*/
3747a6b2eed2SHong Zhang     /*  post receives of j-array */
3748a6b2eed2SHong Zhang     for (i=0; i<nrecvs; i++){
374987025532SHong Zhang       nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */
375087025532SHong Zhang       ierr = MPI_Irecv(b_othj+rstartsj[i],nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr);
3751a6b2eed2SHong Zhang     }
3752a6b2eed2SHong Zhang     k = 0;
3753a6b2eed2SHong Zhang     for (i=0; i<nsends; i++){
375487025532SHong Zhang       nrows = sstarts[i+1]-sstarts[i]; /* num of rows */
3755a6b2eed2SHong Zhang       bufJ = bufj+sstartsj[i];
375687025532SHong Zhang       for (j=0; j<nrows; j++) {
3757*899cda47SBarry Smith         row  = srow[k++] + B->rmap.range[rank]; /* global row idx */
3758ba8c8a56SBarry Smith         ierr = MatGetRow_MPIAIJ(B,row,&ncols,&cols,PETSC_NULL);CHKERRQ(ierr);
3759a6b2eed2SHong Zhang         for (l=0; l<ncols; l++){
3760a6b2eed2SHong Zhang           *bufJ++ = cols[l];
376187025532SHong Zhang         }
3762ba8c8a56SBarry Smith         ierr = MatRestoreRow_MPIAIJ(B,row,&ncols,&cols,PETSC_NULL);CHKERRQ(ierr);
376387025532SHong Zhang       }
376487025532SHong Zhang       ierr = MPI_Isend(bufj+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_INT,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr);
376587025532SHong Zhang     }
376687025532SHong Zhang 
376787025532SHong Zhang     /* recvs and sends of j-array are completed */
376887025532SHong Zhang     i = nrecvs;
376987025532SHong Zhang     while (i--) {
377087025532SHong Zhang       ierr = MPI_Waitany(nrecvs,rwaits,&j,&rstatus);CHKERRQ(ierr);
377187025532SHong Zhang     }
37720c468ba9SBarry Smith     if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);}
377387025532SHong Zhang   } else if (scall == MAT_REUSE_MATRIX){
377487025532SHong Zhang     sstartsj = *startsj;
377587025532SHong Zhang     rstartsj = sstartsj + nsends +1;
377687025532SHong Zhang     bufa     = *bufa_ptr;
377787025532SHong Zhang     b_oth    = (Mat_SeqAIJ*)(*B_oth)->data;
377887025532SHong Zhang     b_otha   = b_oth->a;
377987025532SHong Zhang   } else {
378087025532SHong Zhang     SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Matrix P does not posses an object container");
378187025532SHong Zhang   }
378287025532SHong Zhang 
378387025532SHong Zhang   /* a-array */
378487025532SHong Zhang   /*---------*/
378587025532SHong Zhang   /*  post receives of a-array */
378687025532SHong Zhang   for (i=0; i<nrecvs; i++){
378787025532SHong Zhang     nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */
378887025532SHong Zhang     ierr = MPI_Irecv(b_otha+rstartsj[i],nrows,MPIU_SCALAR,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr);
378987025532SHong Zhang   }
379087025532SHong Zhang   k = 0;
379187025532SHong Zhang   for (i=0; i<nsends; i++){
379287025532SHong Zhang     nrows = sstarts[i+1]-sstarts[i];
379387025532SHong Zhang     bufA = bufa+sstartsj[i];
379487025532SHong Zhang     for (j=0; j<nrows; j++) {
3795*899cda47SBarry Smith       row  = srow[k++] + B->rmap.range[rank]; /* global row idx */
3796ba8c8a56SBarry Smith       ierr = MatGetRow_MPIAIJ(B,row,&ncols,PETSC_NULL,&vals);CHKERRQ(ierr);
379787025532SHong Zhang       for (l=0; l<ncols; l++){
3798a6b2eed2SHong Zhang         *bufA++ = vals[l];
3799a6b2eed2SHong Zhang       }
3800ba8c8a56SBarry Smith       ierr = MatRestoreRow_MPIAIJ(B,row,&ncols,PETSC_NULL,&vals);CHKERRQ(ierr);
380187025532SHong Zhang 
3802a6b2eed2SHong Zhang     }
380387025532SHong Zhang     ierr = MPI_Isend(bufa+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_SCALAR,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr);
3804a6b2eed2SHong Zhang   }
380587025532SHong Zhang   /* recvs and sends of a-array are completed */
380687025532SHong Zhang   i = nrecvs;
380787025532SHong Zhang   while (i--) {
380887025532SHong Zhang     ierr = MPI_Waitany(nrecvs,rwaits,&j,&rstatus);CHKERRQ(ierr);
380987025532SHong Zhang   }
38100c468ba9SBarry Smith    if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);}
3811a6b2eed2SHong Zhang 
381287025532SHong Zhang   if (scall == MAT_INITIAL_MATRIX){
3813a6b2eed2SHong Zhang     /* put together the new matrix */
3814*899cda47SBarry Smith     ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,aBn,B->cmap.N,b_othi,b_othj,b_otha,B_oth);CHKERRQ(ierr);
3815a6b2eed2SHong Zhang 
3816a6b2eed2SHong Zhang     /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
3817a6b2eed2SHong Zhang     /* Since these are PETSc arrays, change flags to free them as necessary. */
381887025532SHong Zhang     b_oth = (Mat_SeqAIJ *)(*B_oth)->data;
381987025532SHong Zhang     b_oth->freedata = PETSC_TRUE;
382087025532SHong Zhang     b_oth->nonew    = 0;
3821a6b2eed2SHong Zhang 
3822a6b2eed2SHong Zhang     ierr = PetscFree(bufj);CHKERRQ(ierr);
3823dea91ad1SHong Zhang     if (!startsj || !bufa_ptr){
3824dea91ad1SHong Zhang       ierr = PetscFree(sstartsj);CHKERRQ(ierr);
3825dea91ad1SHong Zhang       ierr = PetscFree(bufa_ptr);CHKERRQ(ierr);
3826dea91ad1SHong Zhang     } else {
382787025532SHong Zhang       *startsj  = sstartsj;
382887025532SHong Zhang       *bufa_ptr = bufa;
382987025532SHong Zhang     }
3830dea91ad1SHong Zhang   }
3831429d309bSHong Zhang   ierr = PetscLogEventEnd(logkey_GetBrowsOfAocols,A,B,0,0);CHKERRQ(ierr);
3832a6b2eed2SHong Zhang 
3833429d309bSHong Zhang   PetscFunctionReturn(0);
3834429d309bSHong Zhang }
3835ccd8e176SBarry Smith 
383643eb5e2fSMatthew Knepley #undef __FUNCT__
383743eb5e2fSMatthew Knepley #define __FUNCT__ "MatGetCommunicationStructs"
383843eb5e2fSMatthew Knepley /*@C
383943eb5e2fSMatthew Knepley   MatGetCommunicationStructs - Provides access to the communication structures used in matrix-vector multiplication.
384043eb5e2fSMatthew Knepley 
384143eb5e2fSMatthew Knepley   Not Collective
384243eb5e2fSMatthew Knepley 
384343eb5e2fSMatthew Knepley   Input Parameters:
384443eb5e2fSMatthew Knepley . A - The matrix in mpiaij format
384543eb5e2fSMatthew Knepley 
384643eb5e2fSMatthew Knepley   Output Parameter:
384743eb5e2fSMatthew Knepley + lvec - The local vector holding off-process values from the argument to a matrix-vector product
384843eb5e2fSMatthew Knepley . colmap - A map from global column index to local index into lvec
384943eb5e2fSMatthew Knepley - multScatter - A scatter from the argument of a matrix-vector product to lvec
385043eb5e2fSMatthew Knepley 
385143eb5e2fSMatthew Knepley   Level: developer
385243eb5e2fSMatthew Knepley 
385343eb5e2fSMatthew Knepley @*/
385443eb5e2fSMatthew Knepley #if defined (PETSC_USE_CTABLE)
385543eb5e2fSMatthew Knepley PetscErrorCode PETSCMAT_DLLEXPORT MatGetCommunicationStructs(Mat A, Vec *lvec, PetscTable *colmap, VecScatter *multScatter)
385643eb5e2fSMatthew Knepley #else
385743eb5e2fSMatthew Knepley PetscErrorCode PETSCMAT_DLLEXPORT MatGetCommunicationStructs(Mat A, Vec *lvec, PetscInt *colmap[], VecScatter *multScatter)
385843eb5e2fSMatthew Knepley #endif
385943eb5e2fSMatthew Knepley {
386043eb5e2fSMatthew Knepley   Mat_MPIAIJ *a;
386143eb5e2fSMatthew Knepley 
386243eb5e2fSMatthew Knepley   PetscFunctionBegin;
386343eb5e2fSMatthew Knepley   PetscValidHeaderSpecific(A, MAT_COOKIE, 1);
386443eb5e2fSMatthew Knepley   PetscValidPointer(lvec, 2)
386543eb5e2fSMatthew Knepley   PetscValidPointer(colmap, 3)
386643eb5e2fSMatthew Knepley   PetscValidPointer(multScatter, 4)
386743eb5e2fSMatthew Knepley   a = (Mat_MPIAIJ *) A->data;
386843eb5e2fSMatthew Knepley   if (lvec) *lvec = a->lvec;
386943eb5e2fSMatthew Knepley   if (colmap) *colmap = a->colmap;
387043eb5e2fSMatthew Knepley   if (multScatter) *multScatter = a->Mvctx;
387143eb5e2fSMatthew Knepley   PetscFunctionReturn(0);
387243eb5e2fSMatthew Knepley }
387343eb5e2fSMatthew Knepley 
3874ccd8e176SBarry Smith /*MC
3875ccd8e176SBarry Smith    MATMPIAIJ - MATMPIAIJ = "mpiaij" - A matrix type to be used for parallel sparse matrices.
3876ccd8e176SBarry Smith 
3877ccd8e176SBarry Smith    Options Database Keys:
3878ccd8e176SBarry Smith . -mat_type mpiaij - sets the matrix type to "mpiaij" during a call to MatSetFromOptions()
3879ccd8e176SBarry Smith 
3880ccd8e176SBarry Smith   Level: beginner
3881ccd8e176SBarry Smith 
3882ccd8e176SBarry Smith .seealso: MatCreateMPIAIJ
3883ccd8e176SBarry Smith M*/
3884ccd8e176SBarry Smith 
3885ccd8e176SBarry Smith EXTERN_C_BEGIN
3886ccd8e176SBarry Smith #undef __FUNCT__
3887ccd8e176SBarry Smith #define __FUNCT__ "MatCreate_MPIAIJ"
3888be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_MPIAIJ(Mat B)
3889ccd8e176SBarry Smith {
3890ccd8e176SBarry Smith   Mat_MPIAIJ     *b;
3891ccd8e176SBarry Smith   PetscErrorCode ierr;
3892ccd8e176SBarry Smith   PetscMPIInt    size;
3893ccd8e176SBarry Smith 
3894ccd8e176SBarry Smith   PetscFunctionBegin;
3895ccd8e176SBarry Smith   ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr);
3896ccd8e176SBarry Smith 
3897ccd8e176SBarry Smith   ierr            = PetscNew(Mat_MPIAIJ,&b);CHKERRQ(ierr);
3898ccd8e176SBarry Smith   B->data         = (void*)b;
3899ccd8e176SBarry Smith   ierr            = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
3900ccd8e176SBarry Smith   B->factor       = 0;
3901*899cda47SBarry Smith   B->rmap.bs      = 1;
3902ccd8e176SBarry Smith   B->assembled    = PETSC_FALSE;
3903ccd8e176SBarry Smith   B->mapping      = 0;
3904ccd8e176SBarry Smith 
3905ccd8e176SBarry Smith   B->insertmode      = NOT_SET_VALUES;
3906ccd8e176SBarry Smith   b->size            = size;
3907ccd8e176SBarry Smith   ierr = MPI_Comm_rank(B->comm,&b->rank);CHKERRQ(ierr);
3908ccd8e176SBarry Smith 
3909ccd8e176SBarry Smith   /* build cache for off array entries formed */
3910ccd8e176SBarry Smith   ierr = MatStashCreate_Private(B->comm,1,&B->stash);CHKERRQ(ierr);
3911ccd8e176SBarry Smith   b->donotstash  = PETSC_FALSE;
3912ccd8e176SBarry Smith   b->colmap      = 0;
3913ccd8e176SBarry Smith   b->garray      = 0;
3914ccd8e176SBarry Smith   b->roworiented = PETSC_TRUE;
3915ccd8e176SBarry Smith 
3916ccd8e176SBarry Smith   /* stuff used for matrix vector multiply */
3917ccd8e176SBarry Smith   b->lvec      = PETSC_NULL;
3918ccd8e176SBarry Smith   b->Mvctx     = PETSC_NULL;
3919ccd8e176SBarry Smith 
3920ccd8e176SBarry Smith   /* stuff for MatGetRow() */
3921ccd8e176SBarry Smith   b->rowindices   = 0;
3922ccd8e176SBarry Smith   b->rowvalues    = 0;
3923ccd8e176SBarry Smith   b->getrowactive = PETSC_FALSE;
3924ccd8e176SBarry Smith 
3925ccd8e176SBarry Smith 
3926ccd8e176SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
3927ccd8e176SBarry Smith                                      "MatStoreValues_MPIAIJ",
3928ccd8e176SBarry Smith                                      MatStoreValues_MPIAIJ);CHKERRQ(ierr);
3929ccd8e176SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
3930ccd8e176SBarry Smith                                      "MatRetrieveValues_MPIAIJ",
3931ccd8e176SBarry Smith                                      MatRetrieveValues_MPIAIJ);CHKERRQ(ierr);
3932ccd8e176SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetDiagonalBlock_C",
3933ccd8e176SBarry Smith 				     "MatGetDiagonalBlock_MPIAIJ",
3934ccd8e176SBarry Smith                                      MatGetDiagonalBlock_MPIAIJ);CHKERRQ(ierr);
3935ccd8e176SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C",
3936ccd8e176SBarry Smith 				     "MatIsTranspose_MPIAIJ",
3937ccd8e176SBarry Smith 				     MatIsTranspose_MPIAIJ);CHKERRQ(ierr);
3938ccd8e176SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAIJSetPreallocation_C",
3939ccd8e176SBarry Smith 				     "MatMPIAIJSetPreallocation_MPIAIJ",
3940ccd8e176SBarry Smith 				     MatMPIAIJSetPreallocation_MPIAIJ);CHKERRQ(ierr);
3941ccd8e176SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAIJSetPreallocationCSR_C",
3942ccd8e176SBarry Smith 				     "MatMPIAIJSetPreallocationCSR_MPIAIJ",
3943ccd8e176SBarry Smith 				     MatMPIAIJSetPreallocationCSR_MPIAIJ);CHKERRQ(ierr);
3944ccd8e176SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatDiagonalScaleLocal_C",
3945ccd8e176SBarry Smith 				     "MatDiagonalScaleLocal_MPIAIJ",
3946ccd8e176SBarry Smith 				     MatDiagonalScaleLocal_MPIAIJ);CHKERRQ(ierr);
3947ccd8e176SBarry Smith   PetscFunctionReturn(0);
3948ccd8e176SBarry Smith }
3949ccd8e176SBarry Smith EXTERN_C_END
395081824310SBarry Smith 
395181824310SBarry Smith /*
395281824310SBarry Smith     Special version for direct calls from Fortran
395381824310SBarry Smith */
395481824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
395581824310SBarry Smith #define matsetvaluesmpiaij_ MATSETVALUESMPIAIJ
395681824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
395781824310SBarry Smith #define matsetvaluesmpiaij_ matsetvaluesmpiaij
395881824310SBarry Smith #endif
395981824310SBarry Smith 
396081824310SBarry Smith /* Change these macros so can be used in void function */
396181824310SBarry Smith #undef CHKERRQ
396281824310SBarry Smith #define CHKERRQ(ierr) CHKERRABORT(mat->comm,ierr)
396381824310SBarry Smith #undef SETERRQ2
396481824310SBarry Smith #define SETERRQ2(ierr,b,c,d) CHKERRABORT(mat->comm,ierr)
396581824310SBarry Smith #undef SETERRQ
396681824310SBarry Smith #define SETERRQ(ierr,b) CHKERRABORT(mat->comm,ierr)
396781824310SBarry Smith 
396881824310SBarry Smith EXTERN_C_BEGIN
396981824310SBarry Smith #undef __FUNCT__
397081824310SBarry Smith #define __FUNCT__ "matsetvaluesmpiaij_"
397181824310SBarry Smith void matsetvaluesmpiaij_(Mat *mmat,PetscInt *mm,const PetscInt im[],PetscInt *mn,const PetscInt in[],const PetscScalar v[],InsertMode *maddv)
397281824310SBarry Smith {
397381824310SBarry Smith   Mat            mat = *mmat;
397481824310SBarry Smith   PetscInt       m = *mm, n = *mn;
397581824310SBarry Smith   InsertMode     addv = *maddv;
397681824310SBarry Smith   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
397781824310SBarry Smith   PetscScalar    value;
397881824310SBarry Smith   PetscErrorCode ierr;
3979*899cda47SBarry Smith 
398081824310SBarry Smith   MatPreallocated(mat);
398181824310SBarry Smith   if (mat->insertmode == NOT_SET_VALUES) {
398281824310SBarry Smith     mat->insertmode = addv;
398381824310SBarry Smith   }
398481824310SBarry Smith #if defined(PETSC_USE_DEBUG)
398581824310SBarry Smith   else if (mat->insertmode != addv) {
398681824310SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
398781824310SBarry Smith   }
398881824310SBarry Smith #endif
398981824310SBarry Smith   {
3990*899cda47SBarry Smith   PetscInt       i,j,rstart = mat->rmap.rstart,rend = mat->rmap.rend;
3991*899cda47SBarry Smith   PetscInt       cstart = mat->cmap.rstart,cend = mat->cmap.rend,row,col;
399281824310SBarry Smith   PetscTruth     roworiented = aij->roworiented;
399381824310SBarry Smith 
399481824310SBarry Smith   /* Some Variables required in the macro */
399581824310SBarry Smith   Mat            A = aij->A;
399681824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
399781824310SBarry Smith   PetscInt       *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j;
399881824310SBarry Smith   PetscScalar    *aa = a->a;
399981824310SBarry Smith   PetscTruth     ignorezeroentries = (((a->ignorezeroentries)&&(addv==ADD_VALUES))?PETSC_TRUE:PETSC_FALSE);
400081824310SBarry Smith   Mat            B = aij->B;
400181824310SBarry Smith   Mat_SeqAIJ     *b = (Mat_SeqAIJ*)B->data;
4002*899cda47SBarry Smith   PetscInt       *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap.n,am = aij->A->rmap.n;
400381824310SBarry Smith   PetscScalar    *ba = b->a;
400481824310SBarry Smith 
400581824310SBarry Smith   PetscInt       *rp1,*rp2,ii,nrow1,nrow2,_i,rmax1,rmax2,N,low1,high1,low2,high2,t,lastcol1,lastcol2;
400681824310SBarry Smith   PetscInt       nonew = a->nonew;
400781824310SBarry Smith   PetscScalar    *ap1,*ap2;
400881824310SBarry Smith 
400981824310SBarry Smith   PetscFunctionBegin;
401081824310SBarry Smith   for (i=0; i<m; i++) {
401181824310SBarry Smith     if (im[i] < 0) continue;
401281824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4013*899cda47SBarry 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);
401481824310SBarry Smith #endif
401581824310SBarry Smith     if (im[i] >= rstart && im[i] < rend) {
401681824310SBarry Smith       row      = im[i] - rstart;
401781824310SBarry Smith       lastcol1 = -1;
401881824310SBarry Smith       rp1      = aj + ai[row];
401981824310SBarry Smith       ap1      = aa + ai[row];
402081824310SBarry Smith       rmax1    = aimax[row];
402181824310SBarry Smith       nrow1    = ailen[row];
402281824310SBarry Smith       low1     = 0;
402381824310SBarry Smith       high1    = nrow1;
402481824310SBarry Smith       lastcol2 = -1;
402581824310SBarry Smith       rp2      = bj + bi[row];
402681824310SBarry Smith       ap2      = ba + bi[row];
402781824310SBarry Smith       rmax2    = bimax[row];
402881824310SBarry Smith       nrow2    = bilen[row];
402981824310SBarry Smith       low2     = 0;
403081824310SBarry Smith       high2    = nrow2;
403181824310SBarry Smith 
403281824310SBarry Smith       for (j=0; j<n; j++) {
403381824310SBarry Smith         if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
403481824310SBarry Smith         if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES)) continue;
403581824310SBarry Smith         if (in[j] >= cstart && in[j] < cend){
403681824310SBarry Smith           col = in[j] - cstart;
403781824310SBarry Smith           MatSetValues_SeqAIJ_A_Private(row,col,value,addv);
403881824310SBarry Smith         } else if (in[j] < 0) continue;
403981824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4040*899cda47SBarry 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);}
404181824310SBarry Smith #endif
404281824310SBarry Smith         else {
404381824310SBarry Smith           if (mat->was_assembled) {
404481824310SBarry Smith             if (!aij->colmap) {
404581824310SBarry Smith               ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
404681824310SBarry Smith             }
404781824310SBarry Smith #if defined (PETSC_USE_CTABLE)
404881824310SBarry Smith             ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr);
404981824310SBarry Smith 	    col--;
405081824310SBarry Smith #else
405181824310SBarry Smith             col = aij->colmap[in[j]] - 1;
405281824310SBarry Smith #endif
405381824310SBarry Smith             if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
405481824310SBarry Smith               ierr = DisAssemble_MPIAIJ(mat);CHKERRQ(ierr);
405581824310SBarry Smith               col =  in[j];
405681824310SBarry Smith               /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
405781824310SBarry Smith               B = aij->B;
405881824310SBarry Smith               b = (Mat_SeqAIJ*)B->data;
405981824310SBarry Smith               bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j;
406081824310SBarry Smith               rp2      = bj + bi[row];
406181824310SBarry Smith               ap2      = ba + bi[row];
406281824310SBarry Smith               rmax2    = bimax[row];
406381824310SBarry Smith               nrow2    = bilen[row];
406481824310SBarry Smith               low2     = 0;
406581824310SBarry Smith               high2    = nrow2;
4066*899cda47SBarry Smith               bm       = aij->B->rmap.n;
406781824310SBarry Smith               ba = b->a;
406881824310SBarry Smith             }
406981824310SBarry Smith           } else col = in[j];
407081824310SBarry Smith           MatSetValues_SeqAIJ_B_Private(row,col,value,addv);
407181824310SBarry Smith         }
407281824310SBarry Smith       }
407381824310SBarry Smith     } else {
407481824310SBarry Smith       if (!aij->donotstash) {
407581824310SBarry Smith         if (roworiented) {
407681824310SBarry Smith           if (ignorezeroentries && v[i*n] == 0.0) continue;
407781824310SBarry Smith           ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n);CHKERRQ(ierr);
407881824310SBarry Smith         } else {
407981824310SBarry Smith           if (ignorezeroentries && v[i] == 0.0) continue;
408081824310SBarry Smith           ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m);CHKERRQ(ierr);
408181824310SBarry Smith         }
408281824310SBarry Smith       }
408381824310SBarry Smith     }
408481824310SBarry Smith   }}
408581824310SBarry Smith   PetscFunctionReturnVoid();
408681824310SBarry Smith }
408781824310SBarry Smith EXTERN_C_END
4088