xref: /petsc/src/mat/impls/aij/mpi/mpiaij.c (revision 181753b3a291004226dabd44d6c1e78262c1a4bd)
1 /*$Id: mpiaij.c,v 1.344 2001/08/10 03:30:48 bsmith Exp $*/
2 
3 #include "src/mat/impls/aij/mpi/mpiaij.h"
4 #include "src/vec/vecimpl.h"
5 #include "src/inline/spops.h"
6 
7 /*
8   Local utility routine that creates a mapping from the global column
9 number to the local number in the off-diagonal part of the local
10 storage of the matrix.  When PETSC_USE_CTABLE is used this is scalable at
11 a slightly higher hash table cost; without it it is not scalable (each processor
12 has an order N integer array but is fast to acess.
13 */
14 #undef __FUNCT__
15 #define __FUNCT__ "CreateColmap_MPIAIJ_Private"
16 int CreateColmap_MPIAIJ_Private(Mat mat)
17 {
18   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
19   int        n = aij->B->n,i,ierr;
20 
21   PetscFunctionBegin;
22 #if defined (PETSC_USE_CTABLE)
23   ierr = PetscTableCreate(n,&aij->colmap);CHKERRQ(ierr);
24   for (i=0; i<n; i++){
25     ierr = PetscTableAdd(aij->colmap,aij->garray[i]+1,i+1);CHKERRQ(ierr);
26   }
27 #else
28   ierr = PetscMalloc((mat->N+1)*sizeof(int),&aij->colmap);CHKERRQ(ierr);
29   PetscLogObjectMemory(mat,mat->N*sizeof(int));
30   ierr = PetscMemzero(aij->colmap,mat->N*sizeof(int));CHKERRQ(ierr);
31   for (i=0; i<n; i++) aij->colmap[aij->garray[i]] = i+1;
32 #endif
33   PetscFunctionReturn(0);
34 }
35 
36 #define CHUNKSIZE   15
37 #define MatSetValues_SeqAIJ_A_Private(row,col,value,addv) \
38 { \
39  \
40     rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift; \
41     rmax = aimax[row]; nrow = ailen[row];  \
42     col1 = col - shift; \
43      \
44     low = 0; high = nrow; \
45     while (high-low > 5) { \
46       t = (low+high)/2; \
47       if (rp[t] > col) high = t; \
48       else             low  = t; \
49     } \
50       for (_i=low; _i<high; _i++) { \
51         if (rp[_i] > col1) break; \
52         if (rp[_i] == col1) { \
53           if (addv == ADD_VALUES) ap[_i] += value;   \
54           else                  ap[_i] = value; \
55           goto a_noinsert; \
56         } \
57       }  \
58       if (nonew == 1) goto a_noinsert; \
59       else if (nonew == -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero into matrix"); \
60       if (nrow >= rmax) { \
61         /* there is no extra room in row, therefore enlarge */ \
62         int    new_nz = ai[am] + CHUNKSIZE,len,*new_i,*new_j; \
63         PetscScalar *new_a; \
64  \
65         if (nonew == -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix"); \
66  \
67         /* malloc new storage space */ \
68         len     = new_nz*(sizeof(int)+sizeof(PetscScalar))+(am+1)*sizeof(int); \
69         ierr    = PetscMalloc(len,&new_a);CHKERRQ(ierr); \
70         new_j   = (int*)(new_a + new_nz); \
71         new_i   = new_j + new_nz; \
72  \
73         /* copy over old data into new slots */ \
74         for (ii=0; ii<row+1; ii++) {new_i[ii] = ai[ii];} \
75         for (ii=row+1; ii<am+1; ii++) {new_i[ii] = ai[ii]+CHUNKSIZE;} \
76         ierr = PetscMemcpy(new_j,aj,(ai[row]+nrow+shift)*sizeof(int));CHKERRQ(ierr); \
77         len = (new_nz - CHUNKSIZE - ai[row] - nrow - shift); \
78         ierr = PetscMemcpy(new_j+ai[row]+shift+nrow+CHUNKSIZE,aj+ai[row]+shift+nrow, \
79                                                            len*sizeof(int));CHKERRQ(ierr); \
80         ierr = PetscMemcpy(new_a,aa,(ai[row]+nrow+shift)*sizeof(PetscScalar));CHKERRQ(ierr); \
81         ierr = PetscMemcpy(new_a+ai[row]+shift+nrow+CHUNKSIZE,aa+ai[row]+shift+nrow, \
82                                                            len*sizeof(PetscScalar));CHKERRQ(ierr);  \
83         /* free up old matrix storage */ \
84  \
85         ierr = PetscFree(a->a);CHKERRQ(ierr);  \
86         if (!a->singlemalloc) { \
87            ierr = PetscFree(a->i);CHKERRQ(ierr); \
88            ierr = PetscFree(a->j);CHKERRQ(ierr); \
89         } \
90         aa = a->a = new_a; ai = a->i = new_i; aj = a->j = new_j;  \
91         a->singlemalloc = PETSC_TRUE; \
92  \
93         rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift; \
94         rmax = aimax[row] = aimax[row] + CHUNKSIZE; \
95         PetscLogObjectMemory(A,CHUNKSIZE*(sizeof(int) + sizeof(PetscScalar))); \
96         a->maxnz += CHUNKSIZE; \
97         a->reallocs++; \
98       } \
99       N = nrow++ - 1; a->nz++; \
100       /* shift up all the later entries in this row */ \
101       for (ii=N; ii>=_i; ii--) { \
102         rp[ii+1] = rp[ii]; \
103         ap[ii+1] = ap[ii]; \
104       } \
105       rp[_i] = col1;  \
106       ap[_i] = value;  \
107       a_noinsert: ; \
108       ailen[row] = nrow; \
109 }
110 
111 #define MatSetValues_SeqAIJ_B_Private(row,col,value,addv) \
112 { \
113  \
114     rp   = bj + bi[row] + shift; ap = ba + bi[row] + shift; \
115     rmax = bimax[row]; nrow = bilen[row];  \
116     col1 = col - shift; \
117      \
118     low = 0; high = nrow; \
119     while (high-low > 5) { \
120       t = (low+high)/2; \
121       if (rp[t] > col) high = t; \
122       else             low  = t; \
123     } \
124        for (_i=low; _i<high; _i++) { \
125         if (rp[_i] > col1) break; \
126         if (rp[_i] == col1) { \
127           if (addv == ADD_VALUES) ap[_i] += value;   \
128           else                  ap[_i] = value; \
129           goto b_noinsert; \
130         } \
131       }  \
132       if (nonew == 1) goto b_noinsert; \
133       else if (nonew == -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero into matrix"); \
134       if (nrow >= rmax) { \
135         /* there is no extra room in row, therefore enlarge */ \
136         int    new_nz = bi[bm] + CHUNKSIZE,len,*new_i,*new_j; \
137         PetscScalar *new_a; \
138  \
139         if (nonew == -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix"); \
140  \
141         /* malloc new storage space */ \
142         len     = new_nz*(sizeof(int)+sizeof(PetscScalar))+(bm+1)*sizeof(int); \
143         ierr    = PetscMalloc(len,&new_a);CHKERRQ(ierr); \
144         new_j   = (int*)(new_a + new_nz); \
145         new_i   = new_j + new_nz; \
146  \
147         /* copy over old data into new slots */ \
148         for (ii=0; ii<row+1; ii++) {new_i[ii] = bi[ii];} \
149         for (ii=row+1; ii<bm+1; ii++) {new_i[ii] = bi[ii]+CHUNKSIZE;} \
150         ierr = PetscMemcpy(new_j,bj,(bi[row]+nrow+shift)*sizeof(int));CHKERRQ(ierr); \
151         len = (new_nz - CHUNKSIZE - bi[row] - nrow - shift); \
152         ierr = PetscMemcpy(new_j+bi[row]+shift+nrow+CHUNKSIZE,bj+bi[row]+shift+nrow, \
153                                                            len*sizeof(int));CHKERRQ(ierr); \
154         ierr = PetscMemcpy(new_a,ba,(bi[row]+nrow+shift)*sizeof(PetscScalar));CHKERRQ(ierr); \
155         ierr = PetscMemcpy(new_a+bi[row]+shift+nrow+CHUNKSIZE,ba+bi[row]+shift+nrow, \
156                                                            len*sizeof(PetscScalar));CHKERRQ(ierr);  \
157         /* free up old matrix storage */ \
158  \
159         ierr = PetscFree(b->a);CHKERRQ(ierr);  \
160         if (!b->singlemalloc) { \
161           ierr = PetscFree(b->i);CHKERRQ(ierr); \
162           ierr = PetscFree(b->j);CHKERRQ(ierr); \
163         } \
164         ba = b->a = new_a; bi = b->i = new_i; bj = b->j = new_j;  \
165         b->singlemalloc = PETSC_TRUE; \
166  \
167         rp   = bj + bi[row] + shift; ap = ba + bi[row] + shift; \
168         rmax = bimax[row] = bimax[row] + CHUNKSIZE; \
169         PetscLogObjectMemory(B,CHUNKSIZE*(sizeof(int) + sizeof(PetscScalar))); \
170         b->maxnz += CHUNKSIZE; \
171         b->reallocs++; \
172       } \
173       N = nrow++ - 1; b->nz++; \
174       /* shift up all the later entries in this row */ \
175       for (ii=N; ii>=_i; ii--) { \
176         rp[ii+1] = rp[ii]; \
177         ap[ii+1] = ap[ii]; \
178       } \
179       rp[_i] = col1;  \
180       ap[_i] = value;  \
181       b_noinsert: ; \
182       bilen[row] = nrow; \
183 }
184 
185 #undef __FUNCT__
186 #define __FUNCT__ "MatSetValues_MPIAIJ"
187 int MatSetValues_MPIAIJ(Mat mat,int m,const int im[],int n,const int in[],const PetscScalar v[],InsertMode addv)
188 {
189   Mat_MPIAIJ   *aij = (Mat_MPIAIJ*)mat->data;
190   PetscScalar  value;
191   int          ierr,i,j,rstart = aij->rstart,rend = aij->rend;
192   int          cstart = aij->cstart,cend = aij->cend,row,col;
193   PetscTruth   roworiented = aij->roworiented;
194 
195   /* Some Variables required in the macro */
196   Mat          A = aij->A;
197   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
198   int          *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j;
199   PetscScalar  *aa = a->a;
200   PetscTruth   ignorezeroentries = (((a->ignorezeroentries)&&(addv==ADD_VALUES))?PETSC_TRUE:PETSC_FALSE);
201   Mat          B = aij->B;
202   Mat_SeqAIJ   *b = (Mat_SeqAIJ*)B->data;
203   int          *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->m,am = aij->A->m;
204   PetscScalar  *ba = b->a;
205 
206   int          *rp,ii,nrow,_i,rmax,N,col1,low,high,t;
207   int          nonew = a->nonew,shift=0;
208   PetscScalar  *ap;
209 
210   PetscFunctionBegin;
211   for (i=0; i<m; i++) {
212     if (im[i] < 0) continue;
213 #if defined(PETSC_USE_BOPT_g)
214     if (im[i] >= mat->M) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %d max %d",im[i],mat->M-1);
215 #endif
216     if (im[i] >= rstart && im[i] < rend) {
217       row = im[i] - rstart;
218       for (j=0; j<n; j++) {
219         if (in[j] >= cstart && in[j] < cend){
220           col = in[j] - cstart;
221           if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
222           if (ignorezeroentries && value == 0.0) continue;
223           MatSetValues_SeqAIJ_A_Private(row,col,value,addv);
224           /* ierr = MatSetValues_SeqAIJ(aij->A,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */
225         } else if (in[j] < 0) continue;
226 #if defined(PETSC_USE_BOPT_g)
227         else if (in[j] >= mat->N) {SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %d max %d",in[j],mat->N-1);}
228 #endif
229         else {
230           if (mat->was_assembled) {
231             if (!aij->colmap) {
232               ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
233             }
234 #if defined (PETSC_USE_CTABLE)
235             ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr);
236 	    col--;
237 #else
238             col = aij->colmap[in[j]] - 1;
239 #endif
240             if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
241               ierr = DisAssemble_MPIAIJ(mat);CHKERRQ(ierr);
242               col =  in[j];
243               /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
244               B = aij->B;
245               b = (Mat_SeqAIJ*)B->data;
246               bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j;
247               ba = b->a;
248             }
249           } else col = in[j];
250           if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
251           if (ignorezeroentries && value == 0.0) continue;
252           MatSetValues_SeqAIJ_B_Private(row,col,value,addv);
253           /* ierr = MatSetValues_SeqAIJ(aij->B,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */
254         }
255       }
256     } else {
257       if (!aij->donotstash) {
258         if (roworiented) {
259           if (ignorezeroentries && v[i*n] == 0.0) continue;
260           ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n);CHKERRQ(ierr);
261         } else {
262           if (ignorezeroentries && v[i] == 0.0) continue;
263           ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m);CHKERRQ(ierr);
264         }
265       }
266     }
267   }
268   PetscFunctionReturn(0);
269 }
270 
271 #undef __FUNCT__
272 #define __FUNCT__ "MatGetValues_MPIAIJ"
273 int MatGetValues_MPIAIJ(Mat mat,int m,const int idxm[],int n,const int idxn[],PetscScalar v[])
274 {
275   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
276   int        ierr,i,j,rstart = aij->rstart,rend = aij->rend;
277   int        cstart = aij->cstart,cend = aij->cend,row,col;
278 
279   PetscFunctionBegin;
280   for (i=0; i<m; i++) {
281     if (idxm[i] < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %d",idxm[i]);
282     if (idxm[i] >= mat->M) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %d max %d",idxm[i],mat->M-1);
283     if (idxm[i] >= rstart && idxm[i] < rend) {
284       row = idxm[i] - rstart;
285       for (j=0; j<n; j++) {
286         if (idxn[j] < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %d",idxn[j]);
287         if (idxn[j] >= mat->N) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %d max %d",idxn[j],mat->N-1);
288         if (idxn[j] >= cstart && idxn[j] < cend){
289           col = idxn[j] - cstart;
290           ierr = MatGetValues(aij->A,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
291         } else {
292           if (!aij->colmap) {
293             ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
294           }
295 #if defined (PETSC_USE_CTABLE)
296           ierr = PetscTableFind(aij->colmap,idxn[j]+1,&col);CHKERRQ(ierr);
297           col --;
298 #else
299           col = aij->colmap[idxn[j]] - 1;
300 #endif
301           if ((col < 0) || (aij->garray[col] != idxn[j])) *(v+i*n+j) = 0.0;
302           else {
303             ierr = MatGetValues(aij->B,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
304           }
305         }
306       }
307     } else {
308       SETERRQ(PETSC_ERR_SUP,"Only local values currently supported");
309     }
310   }
311   PetscFunctionReturn(0);
312 }
313 
314 #undef __FUNCT__
315 #define __FUNCT__ "MatAssemblyBegin_MPIAIJ"
316 int MatAssemblyBegin_MPIAIJ(Mat mat,MatAssemblyType mode)
317 {
318   Mat_MPIAIJ  *aij = (Mat_MPIAIJ*)mat->data;
319   int         ierr,nstash,reallocs;
320   InsertMode  addv;
321 
322   PetscFunctionBegin;
323   if (aij->donotstash) {
324     PetscFunctionReturn(0);
325   }
326 
327   /* make sure all processors are either in INSERTMODE or ADDMODE */
328   ierr = MPI_Allreduce(&mat->insertmode,&addv,1,MPI_INT,MPI_BOR,mat->comm);CHKERRQ(ierr);
329   if (addv == (ADD_VALUES|INSERT_VALUES)) {
330     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Some processors inserted others added");
331   }
332   mat->insertmode = addv; /* in case this processor had no cache */
333 
334   ierr = MatStashScatterBegin_Private(&mat->stash,aij->rowners);CHKERRQ(ierr);
335   ierr = MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);CHKERRQ(ierr);
336   PetscLogInfo(aij->A,"MatAssemblyBegin_MPIAIJ:Stash has %d entries, uses %d mallocs.\n",nstash,reallocs);
337   PetscFunctionReturn(0);
338 }
339 
340 
341 #undef __FUNCT__
342 #define __FUNCT__ "MatAssemblyEnd_MPIAIJ"
343 int MatAssemblyEnd_MPIAIJ(Mat mat,MatAssemblyType mode)
344 {
345   Mat_MPIAIJ  *aij = (Mat_MPIAIJ*)mat->data;
346   Mat_SeqAIJ  *a=(Mat_SeqAIJ *)aij->A->data,*b= (Mat_SeqAIJ *)aij->B->data;
347   int         i,j,rstart,ncols,n,ierr,flg;
348   int         *row,*col,other_disassembled;
349   PetscScalar *val;
350   InsertMode  addv = mat->insertmode;
351 
352   PetscFunctionBegin;
353   if (!aij->donotstash) {
354     while (1) {
355       ierr = MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);CHKERRQ(ierr);
356       if (!flg) break;
357 
358       for (i=0; i<n;) {
359         /* Now identify the consecutive vals belonging to the same row */
360         for (j=i,rstart=row[j]; j<n; j++) { if (row[j] != rstart) break; }
361         if (j < n) ncols = j-i;
362         else       ncols = n-i;
363         /* Now assemble all these values with a single function call */
364         ierr = MatSetValues_MPIAIJ(mat,1,row+i,ncols,col+i,val+i,addv);CHKERRQ(ierr);
365         i = j;
366       }
367     }
368     ierr = MatStashScatterEnd_Private(&mat->stash);CHKERRQ(ierr);
369   }
370 
371   ierr = MatAssemblyBegin(aij->A,mode);CHKERRQ(ierr);
372   ierr = MatAssemblyEnd(aij->A,mode);CHKERRQ(ierr);
373 
374   /* determine if any processor has disassembled, if so we must
375      also disassemble ourselfs, in order that we may reassemble. */
376   /*
377      if nonzero structure of submatrix B cannot change then we know that
378      no processor disassembled thus we can skip this stuff
379   */
380   if (!((Mat_SeqAIJ*)aij->B->data)->nonew)  {
381     ierr = MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPI_INT,MPI_PROD,mat->comm);CHKERRQ(ierr);
382     if (mat->was_assembled && !other_disassembled) {
383       ierr = DisAssemble_MPIAIJ(mat);CHKERRQ(ierr);
384     }
385   }
386 
387   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
388     ierr = MatSetUpMultiply_MPIAIJ(mat);CHKERRQ(ierr);
389   }
390   ierr = MatAssemblyBegin(aij->B,mode);CHKERRQ(ierr);
391   ierr = MatAssemblyEnd(aij->B,mode);CHKERRQ(ierr);
392 
393   if (aij->rowvalues) {
394     ierr = PetscFree(aij->rowvalues);CHKERRQ(ierr);
395     aij->rowvalues = 0;
396   }
397 
398   /* used by MatAXPY() */
399   a->xtoy = 0; b->xtoy = 0;
400   a->XtoY = 0; b->XtoY = 0;
401 
402   PetscFunctionReturn(0);
403 }
404 
405 #undef __FUNCT__
406 #define __FUNCT__ "MatZeroEntries_MPIAIJ"
407 int MatZeroEntries_MPIAIJ(Mat A)
408 {
409   Mat_MPIAIJ *l = (Mat_MPIAIJ*)A->data;
410   int        ierr;
411 
412   PetscFunctionBegin;
413   ierr = MatZeroEntries(l->A);CHKERRQ(ierr);
414   ierr = MatZeroEntries(l->B);CHKERRQ(ierr);
415   PetscFunctionReturn(0);
416 }
417 
418 #undef __FUNCT__
419 #define __FUNCT__ "MatZeroRows_MPIAIJ"
420 int MatZeroRows_MPIAIJ(Mat A,IS is,const PetscScalar *diag)
421 {
422   Mat_MPIAIJ     *l = (Mat_MPIAIJ*)A->data;
423   int            i,ierr,N,*rows,*owners = l->rowners,size = l->size;
424   int            *nprocs,j,idx,nsends,row;
425   int            nmax,*svalues,*starts,*owner,nrecvs,rank = l->rank;
426   int            *rvalues,tag = A->tag,count,base,slen,n,*source;
427   int            *lens,imdex,*lrows,*values,rstart=l->rstart;
428   MPI_Comm       comm = A->comm;
429   MPI_Request    *send_waits,*recv_waits;
430   MPI_Status     recv_status,*send_status;
431   IS             istmp;
432   PetscTruth     found;
433 
434   PetscFunctionBegin;
435   ierr = ISGetLocalSize(is,&N);CHKERRQ(ierr);
436   ierr = ISGetIndices(is,&rows);CHKERRQ(ierr);
437 
438   /*  first count number of contributors to each processor */
439   ierr = PetscMalloc(2*size*sizeof(int),&nprocs);CHKERRQ(ierr);
440   ierr   = PetscMemzero(nprocs,2*size*sizeof(int));CHKERRQ(ierr);
441   ierr = PetscMalloc((N+1)*sizeof(int),&owner);CHKERRQ(ierr); /* see note*/
442   for (i=0; i<N; i++) {
443     idx = rows[i];
444     found = PETSC_FALSE;
445     for (j=0; j<size; j++) {
446       if (idx >= owners[j] && idx < owners[j+1]) {
447         nprocs[2*j]++; nprocs[2*j+1] = 1; owner[i] = j; found = PETSC_TRUE; break;
448       }
449     }
450     if (!found) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Index out of range");
451   }
452   nsends = 0;  for (i=0; i<size; i++) { nsends += nprocs[2*i+1];}
453 
454   /* inform other processors of number of messages and max length*/
455   ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr);
456 
457   /* post receives:   */
458   ierr = PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(int),&rvalues);CHKERRQ(ierr);
459   ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr);
460   for (i=0; i<nrecvs; i++) {
461     ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPI_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr);
462   }
463 
464   /* do sends:
465       1) starts[i] gives the starting index in svalues for stuff going to
466          the ith processor
467   */
468   ierr = PetscMalloc((N+1)*sizeof(int),&svalues);CHKERRQ(ierr);
469   ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr);
470   ierr = PetscMalloc((size+1)*sizeof(int),&starts);CHKERRQ(ierr);
471   starts[0] = 0;
472   for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
473   for (i=0; i<N; i++) {
474     svalues[starts[owner[i]]++] = rows[i];
475   }
476   ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr);
477 
478   starts[0] = 0;
479   for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
480   count = 0;
481   for (i=0; i<size; i++) {
482     if (nprocs[2*i+1]) {
483       ierr = MPI_Isend(svalues+starts[i],nprocs[2*i],MPI_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr);
484     }
485   }
486   ierr = PetscFree(starts);CHKERRQ(ierr);
487 
488   base = owners[rank];
489 
490   /*  wait on receives */
491   ierr   = PetscMalloc(2*(nrecvs+1)*sizeof(int),&lens);CHKERRQ(ierr);
492   source = lens + nrecvs;
493   count  = nrecvs; slen = 0;
494   while (count) {
495     ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
496     /* unpack receives into our local space */
497     ierr = MPI_Get_count(&recv_status,MPI_INT,&n);CHKERRQ(ierr);
498     source[imdex]  = recv_status.MPI_SOURCE;
499     lens[imdex]    = n;
500     slen          += n;
501     count--;
502   }
503   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
504 
505   /* move the data into the send scatter */
506   ierr = PetscMalloc((slen+1)*sizeof(int),&lrows);CHKERRQ(ierr);
507   count = 0;
508   for (i=0; i<nrecvs; i++) {
509     values = rvalues + i*nmax;
510     for (j=0; j<lens[i]; j++) {
511       lrows[count++] = values[j] - base;
512     }
513   }
514   ierr = PetscFree(rvalues);CHKERRQ(ierr);
515   ierr = PetscFree(lens);CHKERRQ(ierr);
516   ierr = PetscFree(owner);CHKERRQ(ierr);
517   ierr = PetscFree(nprocs);CHKERRQ(ierr);
518 
519   /* actually zap the local rows */
520   ierr = ISCreateGeneral(PETSC_COMM_SELF,slen,lrows,&istmp);CHKERRQ(ierr);
521   PetscLogObjectParent(A,istmp);
522 
523   /*
524         Zero the required rows. If the "diagonal block" of the matrix
525      is square and the user wishes to set the diagonal we use seperate
526      code so that MatSetValues() is not called for each diagonal allocating
527      new memory, thus calling lots of mallocs and slowing things down.
528 
529        Contributed by: Mathew Knepley
530   */
531   /* must zero l->B before l->A because the (diag) case below may put values into l->B*/
532   ierr = MatZeroRows(l->B,istmp,0);CHKERRQ(ierr);
533   if (diag && (l->A->M == l->A->N)) {
534     ierr      = MatZeroRows(l->A,istmp,diag);CHKERRQ(ierr);
535   } else if (diag) {
536     ierr = MatZeroRows(l->A,istmp,0);CHKERRQ(ierr);
537     if (((Mat_SeqAIJ*)l->A->data)->nonew) {
538       SETERRQ(PETSC_ERR_SUP,"MatZeroRows() on rectangular matrices cannot be used with the Mat options\n\
539 MAT_NO_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR");
540     }
541     for (i = 0; i < slen; i++) {
542       row  = lrows[i] + rstart;
543       ierr = MatSetValues(A,1,&row,1,&row,diag,INSERT_VALUES);CHKERRQ(ierr);
544     }
545     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
546     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
547   } else {
548     ierr = MatZeroRows(l->A,istmp,0);CHKERRQ(ierr);
549   }
550   ierr = ISDestroy(istmp);CHKERRQ(ierr);
551   ierr = PetscFree(lrows);CHKERRQ(ierr);
552 
553   /* wait on sends */
554   if (nsends) {
555     ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr);
556     ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr);
557     ierr = PetscFree(send_status);CHKERRQ(ierr);
558   }
559   ierr = PetscFree(send_waits);CHKERRQ(ierr);
560   ierr = PetscFree(svalues);CHKERRQ(ierr);
561 
562   PetscFunctionReturn(0);
563 }
564 
565 #undef __FUNCT__
566 #define __FUNCT__ "MatMult_MPIAIJ"
567 int MatMult_MPIAIJ(Mat A,Vec xx,Vec yy)
568 {
569   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
570   int        ierr,nt;
571 
572   PetscFunctionBegin;
573   ierr = VecGetLocalSize(xx,&nt);CHKERRQ(ierr);
574   if (nt != A->n) {
575     SETERRQ2(PETSC_ERR_ARG_SIZ,"Incompatible partition of A (%d) and xx (%d)",A->n,nt);
576   }
577   ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
578   ierr = (*a->A->ops->mult)(a->A,xx,yy);CHKERRQ(ierr);
579   ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
580   ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);CHKERRQ(ierr);
581   PetscFunctionReturn(0);
582 }
583 
584 #undef __FUNCT__
585 #define __FUNCT__ "MatMultAdd_MPIAIJ"
586 int MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
587 {
588   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
589   int        ierr;
590 
591   PetscFunctionBegin;
592   ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
593   ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
594   ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr);
595   ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);CHKERRQ(ierr);
596   PetscFunctionReturn(0);
597 }
598 
599 #undef __FUNCT__
600 #define __FUNCT__ "MatMultTranspose_MPIAIJ"
601 int MatMultTranspose_MPIAIJ(Mat A,Vec xx,Vec yy)
602 {
603   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
604   int        ierr;
605 
606   PetscFunctionBegin;
607   /* do nondiagonal part */
608   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
609   /* send it on its way */
610   ierr = VecScatterBegin(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
611   /* do local part */
612   ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr);
613   /* receive remote parts: note this assumes the values are not actually */
614   /* inserted in yy until the next line, which is true for my implementation*/
615   /* but is not perhaps always true. */
616   ierr = VecScatterEnd(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
617   PetscFunctionReturn(0);
618 }
619 
620 EXTERN_C_BEGIN
621 #undef __FUNCT__
622 #define __FUNCT__ "MatIsSymmetric_MPIAIJ"
623 int MatIsSymmetric_MPIAIJ(Mat Amat,Mat Bmat,PetscTruth *f)
624 {
625   MPI_Comm comm;
626   Mat_MPIAIJ *Aij = (Mat_MPIAIJ *) Amat->data, *Bij;
627   Mat        Adia = Aij->A, Bdia, Aoff,Boff,*Aoffs,*Boffs;
628   IS         Me,Notme;
629   int        M,N,first,last,*notme,ntids,i, ierr;
630 
631   PetscFunctionBegin;
632 
633   /* Easy test: symmetric diagonal block */
634   Bij = (Mat_MPIAIJ *) Bmat->data; Bdia = Bij->A;
635   ierr = MatIsSymmetric(Adia,Bdia,f); CHKERRQ(ierr);
636   if (!*f) PetscFunctionReturn(0);
637   ierr = PetscObjectGetComm((PetscObject)Amat,&comm); CHKERRQ(ierr);
638   ierr = MPI_Comm_size(comm,&ntids); CHKERRQ(ierr);
639   if (ntids==1) PetscFunctionReturn(0);
640 
641   /* Hard test: off-diagonal block. This takes a MatGetSubMatrix. */
642   ierr = MatGetSize(Amat,&M,&N); CHKERRQ(ierr);
643   ierr = MatGetOwnershipRange(Amat,&first,&last); CHKERRQ(ierr);
644   ierr = PetscMalloc((N-last+first)*sizeof(int),&notme); CHKERRQ(ierr);
645   for (i=0; i<first; i++) notme[i] = i;
646   for (i=last; i<M; i++) notme[i-last+first] = i;
647   ierr = ISCreateGeneral(MPI_COMM_SELF,N-last+first,notme,&Notme); CHKERRQ(ierr);
648   ierr = ISCreateStride(MPI_COMM_SELF,last-first,first,1,&Me); CHKERRQ(ierr);
649   ierr = MatGetSubMatrices(Amat,1,&Me,&Notme,MAT_INITIAL_MATRIX,&Aoffs); CHKERRQ(ierr);
650   Aoff = Aoffs[0];
651   ierr = MatGetSubMatrices(Bmat,1,&Notme,&Me,MAT_INITIAL_MATRIX,&Boffs); CHKERRQ(ierr);
652   Boff = Boffs[0];
653   ierr = MatIsSymmetric(Aoff,Boff,f); CHKERRQ(ierr);
654   ierr = MatDestroyMatrices(1,&Aoffs); CHKERRQ(ierr);
655   ierr = MatDestroyMatrices(1,&Boffs); CHKERRQ(ierr);
656   ierr = ISDestroy(Me); CHKERRQ(ierr);
657   ierr = ISDestroy(Notme); CHKERRQ(ierr);
658 
659   PetscFunctionReturn(0);
660 }
661 EXTERN_C_END
662 
663 #undef __FUNCT__
664 #define __FUNCT__ "MatMultTransposeAdd_MPIAIJ"
665 int MatMultTransposeAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
666 {
667   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
668   int        ierr;
669 
670   PetscFunctionBegin;
671   /* do nondiagonal part */
672   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
673   /* send it on its way */
674   ierr = VecScatterBegin(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
675   /* do local part */
676   ierr = (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
677   /* receive remote parts: note this assumes the values are not actually */
678   /* inserted in yy until the next line, which is true for my implementation*/
679   /* but is not perhaps always true. */
680   ierr = VecScatterEnd(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr);
681   PetscFunctionReturn(0);
682 }
683 
684 /*
685   This only works correctly for square matrices where the subblock A->A is the
686    diagonal block
687 */
688 #undef __FUNCT__
689 #define __FUNCT__ "MatGetDiagonal_MPIAIJ"
690 int MatGetDiagonal_MPIAIJ(Mat A,Vec v)
691 {
692   int        ierr;
693   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
694 
695   PetscFunctionBegin;
696   if (A->M != A->N) SETERRQ(PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block");
697   if (a->rstart != a->cstart || a->rend != a->cend) {
698     SETERRQ(PETSC_ERR_ARG_SIZ,"row partition must equal col partition");
699   }
700   ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr);
701   PetscFunctionReturn(0);
702 }
703 
704 #undef __FUNCT__
705 #define __FUNCT__ "MatScale_MPIAIJ"
706 int MatScale_MPIAIJ(const PetscScalar aa[],Mat A)
707 {
708   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
709   int        ierr;
710 
711   PetscFunctionBegin;
712   ierr = MatScale(aa,a->A);CHKERRQ(ierr);
713   ierr = MatScale(aa,a->B);CHKERRQ(ierr);
714   PetscFunctionReturn(0);
715 }
716 
717 #undef __FUNCT__
718 #define __FUNCT__ "MatDestroy_MPIAIJ"
719 int MatDestroy_MPIAIJ(Mat mat)
720 {
721   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
722   int        ierr;
723 
724   PetscFunctionBegin;
725 #if defined(PETSC_USE_LOG)
726   PetscLogObjectState((PetscObject)mat,"Rows=%d, Cols=%d",mat->M,mat->N);
727 #endif
728   ierr = MatStashDestroy_Private(&mat->stash);CHKERRQ(ierr);
729   ierr = PetscFree(aij->rowners);CHKERRQ(ierr);
730   ierr = MatDestroy(aij->A);CHKERRQ(ierr);
731   ierr = MatDestroy(aij->B);CHKERRQ(ierr);
732 #if defined (PETSC_USE_CTABLE)
733   if (aij->colmap) {ierr = PetscTableDelete(aij->colmap);CHKERRQ(ierr);}
734 #else
735   if (aij->colmap) {ierr = PetscFree(aij->colmap);CHKERRQ(ierr);}
736 #endif
737   if (aij->garray) {ierr = PetscFree(aij->garray);CHKERRQ(ierr);}
738   if (aij->lvec)   {ierr = VecDestroy(aij->lvec);CHKERRQ(ierr);}
739   if (aij->Mvctx)  {ierr = VecScatterDestroy(aij->Mvctx);CHKERRQ(ierr);}
740   if (aij->rowvalues) {ierr = PetscFree(aij->rowvalues);CHKERRQ(ierr);}
741   ierr = PetscFree(aij);CHKERRQ(ierr);
742   PetscFunctionReturn(0);
743 }
744 
745 #undef __FUNCT__
746 #define __FUNCT__ "MatView_MPIAIJ_Binary"
747 int MatView_MPIAIJ_Binary(Mat mat,PetscViewer viewer)
748 {
749   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*)mat->data;
750   Mat_SeqAIJ*       A = (Mat_SeqAIJ*)aij->A->data;
751   Mat_SeqAIJ*       B = (Mat_SeqAIJ*)aij->B->data;
752   int               nz,fd,ierr,header[4],rank,size,*row_lengths,*range,rlen,i,tag = ((PetscObject)viewer)->tag;
753   int               nzmax,*column_indices,j,k,col,*garray = aij->garray,cnt,cstart = aij->cstart,rnz;
754   PetscScalar       *column_values;
755 
756   PetscFunctionBegin;
757   ierr = MPI_Comm_rank(mat->comm,&rank);CHKERRQ(ierr);
758   ierr = MPI_Comm_size(mat->comm,&size);CHKERRQ(ierr);
759   nz   = A->nz + B->nz;
760   if (rank == 0) {
761     header[0] = MAT_FILE_COOKIE;
762     header[1] = mat->M;
763     header[2] = mat->N;
764     ierr = MPI_Reduce(&nz,&header[3],1,MPI_INT,MPI_SUM,0,mat->comm);CHKERRQ(ierr);
765     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
766     ierr = PetscBinaryWrite(fd,header,4,PETSC_INT,1);CHKERRQ(ierr);
767     /* get largest number of rows any processor has */
768     rlen = mat->m;
769     ierr = PetscMapGetGlobalRange(mat->rmap,&range);CHKERRQ(ierr);
770     for (i=1; i<size; i++) {
771       rlen = PetscMax(rlen,range[i+1] - range[i]);
772     }
773   } else {
774     ierr = MPI_Reduce(&nz,0,1,MPI_INT,MPI_SUM,0,mat->comm);CHKERRQ(ierr);
775     rlen = mat->m;
776   }
777 
778   /* load up the local row counts */
779   ierr = PetscMalloc((rlen+1)*sizeof(int),&row_lengths);CHKERRQ(ierr);
780   for (i=0; i<mat->m; i++) {
781     row_lengths[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i];
782   }
783 
784   /* store the row lengths to the file */
785   if (rank == 0) {
786     MPI_Status status;
787     ierr = PetscBinaryWrite(fd,row_lengths,mat->m,PETSC_INT,1);CHKERRQ(ierr);
788     for (i=1; i<size; i++) {
789       rlen = range[i+1] - range[i];
790       ierr = MPI_Recv(row_lengths,rlen,MPI_INT,i,tag,mat->comm,&status);CHKERRQ(ierr);
791       ierr = PetscBinaryWrite(fd,row_lengths,rlen,PETSC_INT,1);CHKERRQ(ierr);
792     }
793   } else {
794     ierr = MPI_Send(row_lengths,mat->m,MPI_INT,0,tag,mat->comm);CHKERRQ(ierr);
795   }
796   ierr = PetscFree(row_lengths);CHKERRQ(ierr);
797 
798   /* load up the local column indices */
799   nzmax = nz; /* )th processor needs space a largest processor needs */
800   ierr = MPI_Reduce(&nz,&nzmax,1,MPI_INT,MPI_MAX,0,mat->comm);CHKERRQ(ierr);
801   ierr = PetscMalloc((nzmax+1)*sizeof(int),&column_indices);CHKERRQ(ierr);
802   cnt  = 0;
803   for (i=0; i<mat->m; i++) {
804     for (j=B->i[i]; j<B->i[i+1]; j++) {
805       if ( (col = garray[B->j[j]]) > cstart) break;
806       column_indices[cnt++] = col;
807     }
808     for (k=A->i[i]; k<A->i[i+1]; k++) {
809       column_indices[cnt++] = A->j[k] + cstart;
810     }
811     for (; j<B->i[i+1]; j++) {
812       column_indices[cnt++] = garray[B->j[j]];
813     }
814   }
815   if (cnt != A->nz + B->nz) SETERRQ2(PETSC_ERR_LIB,"Internal PETSc error: cnt = %d nz = %d",cnt,A->nz+B->nz);
816 
817   /* store the column indices to the file */
818   if (rank == 0) {
819     MPI_Status status;
820     ierr = PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,1);CHKERRQ(ierr);
821     for (i=1; i<size; i++) {
822       ierr = MPI_Recv(&rnz,1,MPI_INT,i,tag,mat->comm,&status);CHKERRQ(ierr);
823       if (rnz > nzmax) SETERRQ2(PETSC_ERR_LIB,"Internal PETSc error: nz = %d nzmax = %d",nz,nzmax);
824       ierr = MPI_Recv(column_indices,rnz,MPI_INT,i,tag,mat->comm,&status);CHKERRQ(ierr);
825       ierr = PetscBinaryWrite(fd,column_indices,rnz,PETSC_INT,1);CHKERRQ(ierr);
826     }
827   } else {
828     ierr = MPI_Send(&nz,1,MPI_INT,0,tag,mat->comm);CHKERRQ(ierr);
829     ierr = MPI_Send(column_indices,nz,MPI_INT,0,tag,mat->comm);CHKERRQ(ierr);
830   }
831   ierr = PetscFree(column_indices);CHKERRQ(ierr);
832 
833   /* load up the local column values */
834   ierr = PetscMalloc((nzmax+1)*sizeof(PetscScalar),&column_values);CHKERRQ(ierr);
835   cnt  = 0;
836   for (i=0; i<mat->m; i++) {
837     for (j=B->i[i]; j<B->i[i+1]; j++) {
838       if ( garray[B->j[j]] > cstart) break;
839       column_values[cnt++] = B->a[j];
840     }
841     for (k=A->i[i]; k<A->i[i+1]; k++) {
842       column_values[cnt++] = A->a[k];
843     }
844     for (; j<B->i[i+1]; j++) {
845       column_values[cnt++] = B->a[j];
846     }
847   }
848   if (cnt != A->nz + B->nz) SETERRQ2(1,"Internal PETSc error: cnt = %d nz = %d",cnt,A->nz+B->nz);
849 
850   /* store the column values to the file */
851   if (rank == 0) {
852     MPI_Status status;
853     ierr = PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,1);CHKERRQ(ierr);
854     for (i=1; i<size; i++) {
855       ierr = MPI_Recv(&rnz,1,MPI_INT,i,tag,mat->comm,&status);CHKERRQ(ierr);
856       if (rnz > nzmax) SETERRQ2(PETSC_ERR_LIB,"Internal PETSc error: nz = %d nzmax = %d",nz,nzmax);
857       ierr = MPI_Recv(column_values,rnz,MPIU_SCALAR,i,tag,mat->comm,&status);CHKERRQ(ierr);
858       ierr = PetscBinaryWrite(fd,column_values,rnz,PETSC_SCALAR,1);CHKERRQ(ierr);
859     }
860   } else {
861     ierr = MPI_Send(&nz,1,MPI_INT,0,tag,mat->comm);CHKERRQ(ierr);
862     ierr = MPI_Send(column_values,nz,MPIU_SCALAR,0,tag,mat->comm);CHKERRQ(ierr);
863   }
864   ierr = PetscFree(column_values);CHKERRQ(ierr);
865   PetscFunctionReturn(0);
866 }
867 
868 #undef __FUNCT__
869 #define __FUNCT__ "MatView_MPIAIJ_ASCIIorDraworSocket"
870 int MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
871 {
872   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*)mat->data;
873   int               ierr,rank = aij->rank,size = aij->size;
874   PetscTruth        isdraw,isascii,flg,isbinary;
875   PetscViewer       sviewer;
876   PetscViewerFormat format;
877 
878   PetscFunctionBegin;
879   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
880   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
881   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
882   if (isascii) {
883     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
884     if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
885       MatInfo info;
886       ierr = MPI_Comm_rank(mat->comm,&rank);CHKERRQ(ierr);
887       ierr = MatGetInfo(mat,MAT_LOCAL,&info);CHKERRQ(ierr);
888       ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_no_inode",&flg);CHKERRQ(ierr);
889       if (flg) {
890         ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %d nz %d nz alloced %d mem %d, not using I-node routines\n",
891 					      rank,mat->m,(int)info.nz_used,(int)info.nz_allocated,(int)info.memory);CHKERRQ(ierr);
892       } else {
893         ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %d nz %d nz alloced %d mem %d, using I-node routines\n",
894 		    rank,mat->m,(int)info.nz_used,(int)info.nz_allocated,(int)info.memory);CHKERRQ(ierr);
895       }
896       ierr = MatGetInfo(aij->A,MAT_LOCAL,&info);CHKERRQ(ierr);
897       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %d \n",rank,(int)info.nz_used);CHKERRQ(ierr);
898       ierr = MatGetInfo(aij->B,MAT_LOCAL,&info);CHKERRQ(ierr);
899       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %d \n",rank,(int)info.nz_used);CHKERRQ(ierr);
900       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
901       ierr = VecScatterView(aij->Mvctx,viewer);CHKERRQ(ierr);
902       PetscFunctionReturn(0);
903     } else if (format == PETSC_VIEWER_ASCII_INFO) {
904       PetscFunctionReturn(0);
905     } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
906       PetscFunctionReturn(0);
907     }
908   } else if (isbinary) {
909     if (size == 1) {
910       ierr = PetscObjectSetName((PetscObject)aij->A,mat->name);CHKERRQ(ierr);
911       ierr = MatView(aij->A,viewer);CHKERRQ(ierr);
912     } else {
913       ierr = MatView_MPIAIJ_Binary(mat,viewer);CHKERRQ(ierr);
914     }
915     PetscFunctionReturn(0);
916   } else if (isdraw) {
917     PetscDraw  draw;
918     PetscTruth isnull;
919     ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
920     ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
921   }
922 
923   if (size == 1) {
924     ierr = PetscObjectSetName((PetscObject)aij->A,mat->name);CHKERRQ(ierr);
925     ierr = MatView(aij->A,viewer);CHKERRQ(ierr);
926   } else {
927     /* assemble the entire matrix onto first processor. */
928     Mat         A;
929     Mat_SeqAIJ *Aloc;
930     int         M = mat->M,N = mat->N,m,*ai,*aj,row,*cols,i,*ct;
931     PetscScalar *a;
932 
933     if (!rank) {
934       ierr = MatCreateMPIAIJ(mat->comm,M,N,M,N,0,PETSC_NULL,0,PETSC_NULL,&A);CHKERRQ(ierr);
935     } else {
936       ierr = MatCreateMPIAIJ(mat->comm,0,0,M,N,0,PETSC_NULL,0,PETSC_NULL,&A);CHKERRQ(ierr);
937     }
938     PetscLogObjectParent(mat,A);
939 
940     /* copy over the A part */
941     Aloc = (Mat_SeqAIJ*)aij->A->data;
942     m = aij->A->m; ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
943     row = aij->rstart;
944     for (i=0; i<ai[m]; i++) {aj[i] += aij->cstart ;}
945     for (i=0; i<m; i++) {
946       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);CHKERRQ(ierr);
947       row++; a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
948     }
949     aj = Aloc->j;
950     for (i=0; i<ai[m]; i++) {aj[i] -= aij->cstart;}
951 
952     /* copy over the B part */
953     Aloc = (Mat_SeqAIJ*)aij->B->data;
954     m    = aij->B->m;  ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
955     row  = aij->rstart;
956     ierr = PetscMalloc((ai[m]+1)*sizeof(int),&cols);CHKERRQ(ierr);
957     ct   = cols;
958     for (i=0; i<ai[m]; i++) {cols[i] = aij->garray[aj[i]];}
959     for (i=0; i<m; i++) {
960       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);CHKERRQ(ierr);
961       row++; a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
962     }
963     ierr = PetscFree(ct);CHKERRQ(ierr);
964     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
965     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
966     /*
967        Everyone has to call to draw the matrix since the graphics waits are
968        synchronized across all processors that share the PetscDraw object
969     */
970     ierr = PetscViewerGetSingleton(viewer,&sviewer);CHKERRQ(ierr);
971     if (!rank) {
972       ierr = PetscObjectSetName((PetscObject)((Mat_MPIAIJ*)(A->data))->A,mat->name);CHKERRQ(ierr);
973       ierr = MatView(((Mat_MPIAIJ*)(A->data))->A,sviewer);CHKERRQ(ierr);
974     }
975     ierr = PetscViewerRestoreSingleton(viewer,&sviewer);CHKERRQ(ierr);
976     ierr = MatDestroy(A);CHKERRQ(ierr);
977   }
978   PetscFunctionReturn(0);
979 }
980 
981 #undef __FUNCT__
982 #define __FUNCT__ "MatView_MPIAIJ"
983 int MatView_MPIAIJ(Mat mat,PetscViewer viewer)
984 {
985   int        ierr;
986   PetscTruth isascii,isdraw,issocket,isbinary;
987 
988   PetscFunctionBegin;
989   ierr  = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
990   ierr  = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
991   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
992   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_SOCKET,&issocket);CHKERRQ(ierr);
993   if (isascii || isdraw || isbinary || issocket) {
994     ierr = MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer);CHKERRQ(ierr);
995   } else {
996     SETERRQ1(1,"Viewer type %s not supported by MPIAIJ matrices",((PetscObject)viewer)->type_name);
997   }
998   PetscFunctionReturn(0);
999 }
1000 
1001 
1002 
1003 #undef __FUNCT__
1004 #define __FUNCT__ "MatRelax_MPIAIJ"
1005 int MatRelax_MPIAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,int its,int lits,Vec xx)
1006 {
1007   Mat_MPIAIJ   *mat = (Mat_MPIAIJ*)matin->data;
1008   int          ierr;
1009   Vec          bb1;
1010   PetscScalar  mone=-1.0;
1011 
1012   PetscFunctionBegin;
1013   if (its <= 0 || lits <= 0) SETERRQ2(PETSC_ERR_ARG_WRONG,"Relaxation requires global its %d and local its %d both positive",its,lits);
1014 
1015   ierr = VecDuplicate(bb,&bb1);CHKERRQ(ierr);
1016 
1017   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){
1018     if (flag & SOR_ZERO_INITIAL_GUESS) {
1019       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,lits,xx);CHKERRQ(ierr);
1020       its--;
1021     }
1022 
1023     while (its--) {
1024       ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
1025       ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
1026 
1027       /* update rhs: bb1 = bb - B*x */
1028       ierr = VecScale(&mone,mat->lvec);CHKERRQ(ierr);
1029       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
1030 
1031       /* local sweep */
1032       ierr = (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,lits,xx);
1033       CHKERRQ(ierr);
1034     }
1035   } else if (flag & SOR_LOCAL_FORWARD_SWEEP){
1036     if (flag & SOR_ZERO_INITIAL_GUESS) {
1037       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,PETSC_NULL,xx);CHKERRQ(ierr);
1038       its--;
1039     }
1040     while (its--) {
1041       ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
1042       ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
1043 
1044       /* update rhs: bb1 = bb - B*x */
1045       ierr = VecScale(&mone,mat->lvec);CHKERRQ(ierr);
1046       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
1047 
1048       /* local sweep */
1049       ierr = (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,PETSC_NULL,xx);
1050       CHKERRQ(ierr);
1051     }
1052   } else if (flag & SOR_LOCAL_BACKWARD_SWEEP){
1053     if (flag & SOR_ZERO_INITIAL_GUESS) {
1054       ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,PETSC_NULL,xx);CHKERRQ(ierr);
1055       its--;
1056     }
1057     while (its--) {
1058       ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
1059       ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr);
1060 
1061       /* update rhs: bb1 = bb - B*x */
1062       ierr = VecScale(&mone,mat->lvec);CHKERRQ(ierr);
1063       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
1064 
1065       /* local sweep */
1066       ierr = (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,PETSC_NULL,xx);
1067       CHKERRQ(ierr);
1068     }
1069   } else {
1070     SETERRQ(PETSC_ERR_SUP,"Parallel SOR not supported");
1071   }
1072 
1073   ierr = VecDestroy(bb1);CHKERRQ(ierr);
1074   PetscFunctionReturn(0);
1075 }
1076 
1077 #undef __FUNCT__
1078 #define __FUNCT__ "MatGetInfo_MPIAIJ"
1079 int MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info)
1080 {
1081   Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data;
1082   Mat        A = mat->A,B = mat->B;
1083   int        ierr;
1084   PetscReal  isend[5],irecv[5];
1085 
1086   PetscFunctionBegin;
1087   info->block_size     = 1.0;
1088   ierr = MatGetInfo(A,MAT_LOCAL,info);CHKERRQ(ierr);
1089   isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
1090   isend[3] = info->memory;  isend[4] = info->mallocs;
1091   ierr = MatGetInfo(B,MAT_LOCAL,info);CHKERRQ(ierr);
1092   isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
1093   isend[3] += info->memory;  isend[4] += info->mallocs;
1094   if (flag == MAT_LOCAL) {
1095     info->nz_used      = isend[0];
1096     info->nz_allocated = isend[1];
1097     info->nz_unneeded  = isend[2];
1098     info->memory       = isend[3];
1099     info->mallocs      = isend[4];
1100   } else if (flag == MAT_GLOBAL_MAX) {
1101     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_MAX,matin->comm);CHKERRQ(ierr);
1102     info->nz_used      = irecv[0];
1103     info->nz_allocated = irecv[1];
1104     info->nz_unneeded  = irecv[2];
1105     info->memory       = irecv[3];
1106     info->mallocs      = irecv[4];
1107   } else if (flag == MAT_GLOBAL_SUM) {
1108     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_SUM,matin->comm);CHKERRQ(ierr);
1109     info->nz_used      = irecv[0];
1110     info->nz_allocated = irecv[1];
1111     info->nz_unneeded  = irecv[2];
1112     info->memory       = irecv[3];
1113     info->mallocs      = irecv[4];
1114   }
1115   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
1116   info->fill_ratio_needed = 0;
1117   info->factor_mallocs    = 0;
1118   info->rows_global       = (double)matin->M;
1119   info->columns_global    = (double)matin->N;
1120   info->rows_local        = (double)matin->m;
1121   info->columns_local     = (double)matin->N;
1122 
1123   PetscFunctionReturn(0);
1124 }
1125 
1126 #undef __FUNCT__
1127 #define __FUNCT__ "MatSetOption_MPIAIJ"
1128 int MatSetOption_MPIAIJ(Mat A,MatOption op)
1129 {
1130   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
1131   int        ierr;
1132 
1133   PetscFunctionBegin;
1134   switch (op) {
1135   case MAT_NO_NEW_NONZERO_LOCATIONS:
1136   case MAT_YES_NEW_NONZERO_LOCATIONS:
1137   case MAT_COLUMNS_UNSORTED:
1138   case MAT_COLUMNS_SORTED:
1139   case MAT_NEW_NONZERO_ALLOCATION_ERR:
1140   case MAT_KEEP_ZEROED_ROWS:
1141   case MAT_NEW_NONZERO_LOCATION_ERR:
1142   case MAT_USE_INODES:
1143   case MAT_DO_NOT_USE_INODES:
1144   case MAT_IGNORE_ZERO_ENTRIES:
1145     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
1146     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
1147     break;
1148   case MAT_ROW_ORIENTED:
1149     a->roworiented = PETSC_TRUE;
1150     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
1151     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
1152     break;
1153   case MAT_ROWS_SORTED:
1154   case MAT_ROWS_UNSORTED:
1155   case MAT_YES_NEW_DIAGONALS:
1156     PetscLogInfo(A,"MatSetOption_MPIAIJ:Option ignored\n");
1157     break;
1158   case MAT_COLUMN_ORIENTED:
1159     a->roworiented = PETSC_FALSE;
1160     ierr = MatSetOption(a->A,op);CHKERRQ(ierr);
1161     ierr = MatSetOption(a->B,op);CHKERRQ(ierr);
1162     break;
1163   case MAT_IGNORE_OFF_PROC_ENTRIES:
1164     a->donotstash = PETSC_TRUE;
1165     break;
1166   case MAT_NO_NEW_DIAGONALS:
1167     SETERRQ(PETSC_ERR_SUP,"MAT_NO_NEW_DIAGONALS");
1168   default:
1169     SETERRQ(PETSC_ERR_SUP,"unknown option");
1170   }
1171   PetscFunctionReturn(0);
1172 }
1173 
1174 #undef __FUNCT__
1175 #define __FUNCT__ "MatGetRow_MPIAIJ"
1176 int MatGetRow_MPIAIJ(Mat matin,int row,int *nz,int **idx,PetscScalar **v)
1177 {
1178   Mat_MPIAIJ   *mat = (Mat_MPIAIJ*)matin->data;
1179   PetscScalar  *vworkA,*vworkB,**pvA,**pvB,*v_p;
1180   int          i,ierr,*cworkA,*cworkB,**pcA,**pcB,cstart = mat->cstart;
1181   int          nztot,nzA,nzB,lrow,rstart = mat->rstart,rend = mat->rend;
1182   int          *cmap,*idx_p;
1183 
1184   PetscFunctionBegin;
1185   if (mat->getrowactive == PETSC_TRUE) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Already active");
1186   mat->getrowactive = PETSC_TRUE;
1187 
1188   if (!mat->rowvalues && (idx || v)) {
1189     /*
1190         allocate enough space to hold information from the longest row.
1191     */
1192     Mat_SeqAIJ *Aa = (Mat_SeqAIJ*)mat->A->data,*Ba = (Mat_SeqAIJ*)mat->B->data;
1193     int     max = 1,tmp;
1194     for (i=0; i<matin->m; i++) {
1195       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
1196       if (max < tmp) { max = tmp; }
1197     }
1198     ierr = PetscMalloc(max*(sizeof(int)+sizeof(PetscScalar)),&mat->rowvalues);CHKERRQ(ierr);
1199     mat->rowindices = (int*)(mat->rowvalues + max);
1200   }
1201 
1202   if (row < rstart || row >= rend) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Only local rows")
1203   lrow = row - rstart;
1204 
1205   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1206   if (!v)   {pvA = 0; pvB = 0;}
1207   if (!idx) {pcA = 0; if (!v) pcB = 0;}
1208   ierr = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1209   ierr = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
1210   nztot = nzA + nzB;
1211 
1212   cmap  = mat->garray;
1213   if (v  || idx) {
1214     if (nztot) {
1215       /* Sort by increasing column numbers, assuming A and B already sorted */
1216       int imark = -1;
1217       if (v) {
1218         *v = v_p = mat->rowvalues;
1219         for (i=0; i<nzB; i++) {
1220           if (cmap[cworkB[i]] < cstart)   v_p[i] = vworkB[i];
1221           else break;
1222         }
1223         imark = i;
1224         for (i=0; i<nzA; i++)     v_p[imark+i] = vworkA[i];
1225         for (i=imark; i<nzB; i++) v_p[nzA+i]   = vworkB[i];
1226       }
1227       if (idx) {
1228         *idx = idx_p = mat->rowindices;
1229         if (imark > -1) {
1230           for (i=0; i<imark; i++) {
1231             idx_p[i] = cmap[cworkB[i]];
1232           }
1233         } else {
1234           for (i=0; i<nzB; i++) {
1235             if (cmap[cworkB[i]] < cstart)   idx_p[i] = cmap[cworkB[i]];
1236             else break;
1237           }
1238           imark = i;
1239         }
1240         for (i=0; i<nzA; i++)     idx_p[imark+i] = cstart + cworkA[i];
1241         for (i=imark; i<nzB; i++) idx_p[nzA+i]   = cmap[cworkB[i]];
1242       }
1243     } else {
1244       if (idx) *idx = 0;
1245       if (v)   *v   = 0;
1246     }
1247   }
1248   *nz = nztot;
1249   ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1250   ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
1251   PetscFunctionReturn(0);
1252 }
1253 
1254 #undef __FUNCT__
1255 #define __FUNCT__ "MatRestoreRow_MPIAIJ"
1256 int MatRestoreRow_MPIAIJ(Mat mat,int row,int *nz,int **idx,PetscScalar **v)
1257 {
1258   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
1259 
1260   PetscFunctionBegin;
1261   if (aij->getrowactive == PETSC_FALSE) {
1262     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"MatGetRow not called");
1263   }
1264   aij->getrowactive = PETSC_FALSE;
1265   PetscFunctionReturn(0);
1266 }
1267 
1268 #undef __FUNCT__
1269 #define __FUNCT__ "MatNorm_MPIAIJ"
1270 int MatNorm_MPIAIJ(Mat mat,NormType type,PetscReal *norm)
1271 {
1272   Mat_MPIAIJ   *aij = (Mat_MPIAIJ*)mat->data;
1273   Mat_SeqAIJ   *amat = (Mat_SeqAIJ*)aij->A->data,*bmat = (Mat_SeqAIJ*)aij->B->data;
1274   int          ierr,i,j,cstart = aij->cstart;
1275   PetscReal    sum = 0.0;
1276   PetscScalar  *v;
1277 
1278   PetscFunctionBegin;
1279   if (aij->size == 1) {
1280     ierr =  MatNorm(aij->A,type,norm);CHKERRQ(ierr);
1281   } else {
1282     if (type == NORM_FROBENIUS) {
1283       v = amat->a;
1284       for (i=0; i<amat->nz; i++) {
1285 #if defined(PETSC_USE_COMPLEX)
1286         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
1287 #else
1288         sum += (*v)*(*v); v++;
1289 #endif
1290       }
1291       v = bmat->a;
1292       for (i=0; i<bmat->nz; i++) {
1293 #if defined(PETSC_USE_COMPLEX)
1294         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
1295 #else
1296         sum += (*v)*(*v); v++;
1297 #endif
1298       }
1299       ierr = MPI_Allreduce(&sum,norm,1,MPIU_REAL,MPI_SUM,mat->comm);CHKERRQ(ierr);
1300       *norm = sqrt(*norm);
1301     } else if (type == NORM_1) { /* max column norm */
1302       PetscReal *tmp,*tmp2;
1303       int    *jj,*garray = aij->garray;
1304       ierr = PetscMalloc((mat->N+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1305       ierr = PetscMalloc((mat->N+1)*sizeof(PetscReal),&tmp2);CHKERRQ(ierr);
1306       ierr = PetscMemzero(tmp,mat->N*sizeof(PetscReal));CHKERRQ(ierr);
1307       *norm = 0.0;
1308       v = amat->a; jj = amat->j;
1309       for (j=0; j<amat->nz; j++) {
1310         tmp[cstart + *jj++ ] += PetscAbsScalar(*v);  v++;
1311       }
1312       v = bmat->a; jj = bmat->j;
1313       for (j=0; j<bmat->nz; j++) {
1314         tmp[garray[*jj++]] += PetscAbsScalar(*v); v++;
1315       }
1316       ierr = MPI_Allreduce(tmp,tmp2,mat->N,MPIU_REAL,MPI_SUM,mat->comm);CHKERRQ(ierr);
1317       for (j=0; j<mat->N; j++) {
1318         if (tmp2[j] > *norm) *norm = tmp2[j];
1319       }
1320       ierr = PetscFree(tmp);CHKERRQ(ierr);
1321       ierr = PetscFree(tmp2);CHKERRQ(ierr);
1322     } else if (type == NORM_INFINITY) { /* max row norm */
1323       PetscReal ntemp = 0.0;
1324       for (j=0; j<aij->A->m; j++) {
1325         v = amat->a + amat->i[j];
1326         sum = 0.0;
1327         for (i=0; i<amat->i[j+1]-amat->i[j]; i++) {
1328           sum += PetscAbsScalar(*v); v++;
1329         }
1330         v = bmat->a + bmat->i[j];
1331         for (i=0; i<bmat->i[j+1]-bmat->i[j]; i++) {
1332           sum += PetscAbsScalar(*v); v++;
1333         }
1334         if (sum > ntemp) ntemp = sum;
1335       }
1336       ierr = MPI_Allreduce(&ntemp,norm,1,MPIU_REAL,MPI_MAX,mat->comm);CHKERRQ(ierr);
1337     } else {
1338       SETERRQ(PETSC_ERR_SUP,"No support for two norm");
1339     }
1340   }
1341   PetscFunctionReturn(0);
1342 }
1343 
1344 #undef __FUNCT__
1345 #define __FUNCT__ "MatTranspose_MPIAIJ"
1346 int MatTranspose_MPIAIJ(Mat A,Mat *matout)
1347 {
1348   Mat_MPIAIJ   *a = (Mat_MPIAIJ*)A->data;
1349   Mat_SeqAIJ   *Aloc = (Mat_SeqAIJ*)a->A->data;
1350   int          ierr;
1351   int          M = A->M,N = A->N,m,*ai,*aj,row,*cols,i,*ct;
1352   Mat          B;
1353   PetscScalar  *array;
1354 
1355   PetscFunctionBegin;
1356   if (!matout && M != N) {
1357     SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1358   }
1359 
1360   ierr = MatCreateMPIAIJ(A->comm,A->n,A->m,N,M,0,PETSC_NULL,0,PETSC_NULL,&B);CHKERRQ(ierr);
1361 
1362   /* copy over the A part */
1363   Aloc = (Mat_SeqAIJ*)a->A->data;
1364   m = a->A->m; ai = Aloc->i; aj = Aloc->j; array = Aloc->a;
1365   row = a->rstart;
1366   for (i=0; i<ai[m]; i++) {aj[i] += a->cstart ;}
1367   for (i=0; i<m; i++) {
1368     ierr = MatSetValues(B,ai[i+1]-ai[i],aj,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
1369     row++; array += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
1370   }
1371   aj = Aloc->j;
1372   for (i=0; i<ai[m]; i++) {aj[i] -= a->cstart ;}
1373 
1374   /* copy over the B part */
1375   Aloc = (Mat_SeqAIJ*)a->B->data;
1376   m = a->B->m;  ai = Aloc->i; aj = Aloc->j; array = Aloc->a;
1377   row  = a->rstart;
1378   ierr = PetscMalloc((1+ai[m])*sizeof(int),&cols);CHKERRQ(ierr);
1379   ct   = cols;
1380   for (i=0; i<ai[m]; i++) {cols[i] = a->garray[aj[i]];}
1381   for (i=0; i<m; i++) {
1382     ierr = MatSetValues(B,ai[i+1]-ai[i],cols,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
1383     row++; array += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
1384   }
1385   ierr = PetscFree(ct);CHKERRQ(ierr);
1386   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1387   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1388   if (matout) {
1389     *matout = B;
1390   } else {
1391     ierr = MatHeaderCopy(A,B);CHKERRQ(ierr);
1392   }
1393   PetscFunctionReturn(0);
1394 }
1395 
1396 #undef __FUNCT__
1397 #define __FUNCT__ "MatDiagonalScale_MPIAIJ"
1398 int MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr)
1399 {
1400   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
1401   Mat        a = aij->A,b = aij->B;
1402   int        ierr,s1,s2,s3;
1403 
1404   PetscFunctionBegin;
1405   ierr = MatGetLocalSize(mat,&s2,&s3);CHKERRQ(ierr);
1406   if (rr) {
1407     ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr);
1408     if (s1!=s3) SETERRQ(PETSC_ERR_ARG_SIZ,"right vector non-conforming local size");
1409     /* Overlap communication with computation. */
1410     ierr = VecScatterBegin(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx);CHKERRQ(ierr);
1411   }
1412   if (ll) {
1413     ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr);
1414     if (s1!=s2) SETERRQ(PETSC_ERR_ARG_SIZ,"left vector non-conforming local size");
1415     ierr = (*b->ops->diagonalscale)(b,ll,0);CHKERRQ(ierr);
1416   }
1417   /* scale  the diagonal block */
1418   ierr = (*a->ops->diagonalscale)(a,ll,rr);CHKERRQ(ierr);
1419 
1420   if (rr) {
1421     /* Do a scatter end and then right scale the off-diagonal block */
1422     ierr = VecScatterEnd(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx);CHKERRQ(ierr);
1423     ierr = (*b->ops->diagonalscale)(b,0,aij->lvec);CHKERRQ(ierr);
1424   }
1425 
1426   PetscFunctionReturn(0);
1427 }
1428 
1429 
1430 #undef __FUNCT__
1431 #define __FUNCT__ "MatPrintHelp_MPIAIJ"
1432 int MatPrintHelp_MPIAIJ(Mat A)
1433 {
1434   Mat_MPIAIJ *a   = (Mat_MPIAIJ*)A->data;
1435   int        ierr;
1436 
1437   PetscFunctionBegin;
1438   if (!a->rank) {
1439     ierr = MatPrintHelp_SeqAIJ(a->A);CHKERRQ(ierr);
1440   }
1441   PetscFunctionReturn(0);
1442 }
1443 
1444 #undef __FUNCT__
1445 #define __FUNCT__ "MatGetBlockSize_MPIAIJ"
1446 int MatGetBlockSize_MPIAIJ(Mat A,int *bs)
1447 {
1448   PetscFunctionBegin;
1449   *bs = 1;
1450   PetscFunctionReturn(0);
1451 }
1452 #undef __FUNCT__
1453 #define __FUNCT__ "MatSetUnfactored_MPIAIJ"
1454 int MatSetUnfactored_MPIAIJ(Mat A)
1455 {
1456   Mat_MPIAIJ *a   = (Mat_MPIAIJ*)A->data;
1457   int        ierr;
1458 
1459   PetscFunctionBegin;
1460   ierr = MatSetUnfactored(a->A);CHKERRQ(ierr);
1461   PetscFunctionReturn(0);
1462 }
1463 
1464 #undef __FUNCT__
1465 #define __FUNCT__ "MatEqual_MPIAIJ"
1466 int MatEqual_MPIAIJ(Mat A,Mat B,PetscTruth *flag)
1467 {
1468   Mat_MPIAIJ *matB = (Mat_MPIAIJ*)B->data,*matA = (Mat_MPIAIJ*)A->data;
1469   Mat        a,b,c,d;
1470   PetscTruth flg;
1471   int        ierr;
1472 
1473   PetscFunctionBegin;
1474   a = matA->A; b = matA->B;
1475   c = matB->A; d = matB->B;
1476 
1477   ierr = MatEqual(a,c,&flg);CHKERRQ(ierr);
1478   if (flg == PETSC_TRUE) {
1479     ierr = MatEqual(b,d,&flg);CHKERRQ(ierr);
1480   }
1481   ierr = MPI_Allreduce(&flg,flag,1,MPI_INT,MPI_LAND,A->comm);CHKERRQ(ierr);
1482   PetscFunctionReturn(0);
1483 }
1484 
1485 #undef __FUNCT__
1486 #define __FUNCT__ "MatCopy_MPIAIJ"
1487 int MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str)
1488 {
1489   int        ierr;
1490   Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;
1491   Mat_MPIAIJ *b = (Mat_MPIAIJ *)B->data;
1492 
1493   PetscFunctionBegin;
1494   /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
1495   if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) {
1496     /* because of the column compression in the off-processor part of the matrix a->B,
1497        the number of columns in a->B and b->B may be different, hence we cannot call
1498        the MatCopy() directly on the two parts. If need be, we can provide a more
1499        efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices
1500        then copying the submatrices */
1501     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1502   } else {
1503     ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr);
1504     ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr);
1505   }
1506   PetscFunctionReturn(0);
1507 }
1508 
1509 #undef __FUNCT__
1510 #define __FUNCT__ "MatSetUpPreallocation_MPIAIJ"
1511 int MatSetUpPreallocation_MPIAIJ(Mat A)
1512 {
1513   int        ierr;
1514 
1515   PetscFunctionBegin;
1516   ierr =  MatMPIAIJSetPreallocation(A,PETSC_DEFAULT,0,PETSC_DEFAULT,0);CHKERRQ(ierr);
1517   PetscFunctionReturn(0);
1518 }
1519 
1520 #include "petscblaslapack.h"
1521 #undef __FUNCT__
1522 #define __FUNCT__ "MatAXPY_MPIAIJ"
1523 int MatAXPY_MPIAIJ(const PetscScalar a[],Mat X,Mat Y,MatStructure str)
1524 {
1525   int        ierr,one=1,i;
1526   Mat_MPIAIJ *xx = (Mat_MPIAIJ *)X->data,*yy = (Mat_MPIAIJ *)Y->data;
1527   Mat_SeqAIJ *x,*y;
1528 
1529   PetscFunctionBegin;
1530   if (str == SAME_NONZERO_PATTERN) {
1531     x = (Mat_SeqAIJ *)xx->A->data;
1532     y = (Mat_SeqAIJ *)yy->A->data;
1533     BLaxpy_(&x->nz,(PetscScalar*)a,x->a,&one,y->a,&one);
1534     x = (Mat_SeqAIJ *)xx->B->data;
1535     y = (Mat_SeqAIJ *)yy->B->data;
1536     BLaxpy_(&x->nz,(PetscScalar*)a,x->a,&one,y->a,&one);
1537   } else if (str == SUBSET_NONZERO_PATTERN) {
1538     ierr = MatAXPY_SeqAIJ(a,xx->A,yy->A,str);CHKERRQ(ierr);
1539 
1540     x = (Mat_SeqAIJ *)xx->B->data;
1541     y = (Mat_SeqAIJ *)yy->B->data;
1542     if (y->xtoy && y->XtoY != xx->B) {
1543       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
1544       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
1545     }
1546     if (!y->xtoy) { /* get xtoy */
1547       ierr = MatAXPYGetxtoy_Private(xx->B->m,x->i,x->j,xx->garray,y->i,y->j,yy->garray,&y->xtoy);CHKERRQ(ierr);
1548       y->XtoY = xx->B;
1549     }
1550     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += (*a)*(x->a[i]);
1551   } else {
1552     ierr = MatAXPY_Basic(a,X,Y,str);CHKERRQ(ierr);
1553   }
1554   PetscFunctionReturn(0);
1555 }
1556 
1557 /* -------------------------------------------------------------------*/
1558 static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ,
1559        MatGetRow_MPIAIJ,
1560        MatRestoreRow_MPIAIJ,
1561        MatMult_MPIAIJ,
1562 /* 4*/ MatMultAdd_MPIAIJ,
1563        MatMultTranspose_MPIAIJ,
1564        MatMultTransposeAdd_MPIAIJ,
1565        0,
1566        0,
1567        0,
1568 /*10*/ 0,
1569        0,
1570        0,
1571        MatRelax_MPIAIJ,
1572        MatTranspose_MPIAIJ,
1573 /*15*/ MatGetInfo_MPIAIJ,
1574        MatEqual_MPIAIJ,
1575        MatGetDiagonal_MPIAIJ,
1576        MatDiagonalScale_MPIAIJ,
1577        MatNorm_MPIAIJ,
1578 /*20*/ MatAssemblyBegin_MPIAIJ,
1579        MatAssemblyEnd_MPIAIJ,
1580        0,
1581        MatSetOption_MPIAIJ,
1582        MatZeroEntries_MPIAIJ,
1583 /*25*/ MatZeroRows_MPIAIJ,
1584 #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
1585        MatLUFactorSymbolic_MPIAIJ_TFS,
1586 #else
1587        0,
1588 #endif
1589        0,
1590        0,
1591        0,
1592 /*30*/ MatSetUpPreallocation_MPIAIJ,
1593        0,
1594        0,
1595        0,
1596        0,
1597 /*35*/ MatDuplicate_MPIAIJ,
1598        0,
1599        0,
1600        0,
1601        0,
1602 /*40*/ MatAXPY_MPIAIJ,
1603        MatGetSubMatrices_MPIAIJ,
1604        MatIncreaseOverlap_MPIAIJ,
1605        MatGetValues_MPIAIJ,
1606        MatCopy_MPIAIJ,
1607 /*45*/ MatPrintHelp_MPIAIJ,
1608        MatScale_MPIAIJ,
1609        0,
1610        0,
1611        0,
1612 /*50*/ MatGetBlockSize_MPIAIJ,
1613        0,
1614        0,
1615        0,
1616        0,
1617 /*55*/ MatFDColoringCreate_MPIAIJ,
1618        0,
1619        MatSetUnfactored_MPIAIJ,
1620        0,
1621        0,
1622 /*60*/ MatGetSubMatrix_MPIAIJ,
1623        MatDestroy_MPIAIJ,
1624        MatView_MPIAIJ,
1625        MatGetPetscMaps_Petsc,
1626        0,
1627 /*65*/ 0,
1628        0,
1629        0,
1630        0,
1631        0,
1632 /*70*/ 0,
1633        0,
1634        MatSetColoring_MPIAIJ,
1635        MatSetValuesAdic_MPIAIJ,
1636        MatSetValuesAdifor_MPIAIJ,
1637 /*75*/ 0,
1638        0,
1639        0,
1640        0,
1641        0,
1642 /*80*/ 0,
1643        0,
1644        0,
1645        0,
1646        0,
1647 /*85*/ MatLoad_MPIAIJ};
1648 
1649 /* ----------------------------------------------------------------------------------------*/
1650 
1651 EXTERN_C_BEGIN
1652 #undef __FUNCT__
1653 #define __FUNCT__ "MatStoreValues_MPIAIJ"
1654 int MatStoreValues_MPIAIJ(Mat mat)
1655 {
1656   Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data;
1657   int        ierr;
1658 
1659   PetscFunctionBegin;
1660   ierr = MatStoreValues(aij->A);CHKERRQ(ierr);
1661   ierr = MatStoreValues(aij->B);CHKERRQ(ierr);
1662   PetscFunctionReturn(0);
1663 }
1664 EXTERN_C_END
1665 
1666 EXTERN_C_BEGIN
1667 #undef __FUNCT__
1668 #define __FUNCT__ "MatRetrieveValues_MPIAIJ"
1669 int MatRetrieveValues_MPIAIJ(Mat mat)
1670 {
1671   Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data;
1672   int        ierr;
1673 
1674   PetscFunctionBegin;
1675   ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr);
1676   ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr);
1677   PetscFunctionReturn(0);
1678 }
1679 EXTERN_C_END
1680 
1681 #include "petscpc.h"
1682 EXTERN_C_BEGIN
1683 #undef __FUNCT__
1684 #define __FUNCT__ "MatMPIAIJSetPreallocation_MPIAIJ"
1685 int MatMPIAIJSetPreallocation_MPIAIJ(Mat B,int d_nz,const int d_nnz[],int o_nz,const int o_nnz[])
1686 {
1687   Mat_MPIAIJ   *b;
1688   int          ierr,i;
1689 
1690   PetscFunctionBegin;
1691   B->preallocated = PETSC_TRUE;
1692   if (d_nz == PETSC_DEFAULT || d_nz == PETSC_DECIDE) d_nz = 5;
1693   if (o_nz == PETSC_DEFAULT || o_nz == PETSC_DECIDE) o_nz = 2;
1694   if (d_nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"d_nz cannot be less than 0: value %d",d_nz);
1695   if (o_nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"o_nz cannot be less than 0: value %d",o_nz);
1696   if (d_nnz) {
1697     for (i=0; i<B->m; i++) {
1698       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]);
1699     }
1700   }
1701   if (o_nnz) {
1702     for (i=0; i<B->m; i++) {
1703       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]);
1704     }
1705   }
1706   b = (Mat_MPIAIJ*)B->data;
1707 
1708   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,B->m,B->n,d_nz,d_nnz,&b->A);CHKERRQ(ierr);
1709   PetscLogObjectParent(B,b->A);
1710   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,B->m,B->N,o_nz,o_nnz,&b->B);CHKERRQ(ierr);
1711   PetscLogObjectParent(B,b->B);
1712 
1713   PetscFunctionReturn(0);
1714 }
1715 EXTERN_C_END
1716 
1717 /*MC
1718    MATMPIAIJ = "mpiaij" - A matrix type to be used for parallel sparse matrices.
1719 
1720    Options Database Keys:
1721 . -mat_type mpiaij - sets the matrix type to "mpiaij" during a call to MatSetFromOptions()
1722 
1723   Level: beginner
1724 
1725 .seealso: MatCreateMPIAIJ
1726 M*/
1727 
1728 EXTERN_C_BEGIN
1729 #undef __FUNCT__
1730 #define __FUNCT__ "MatCreate_MPIAIJ"
1731 int MatCreate_MPIAIJ(Mat B)
1732 {
1733   Mat_MPIAIJ *b;
1734   int        ierr,i,size;
1735 
1736   PetscFunctionBegin;
1737   ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr);
1738 
1739   ierr            = PetscNew(Mat_MPIAIJ,&b);CHKERRQ(ierr);
1740   B->data         = (void*)b;
1741   ierr            = PetscMemzero(b,sizeof(Mat_MPIAIJ));CHKERRQ(ierr);
1742   ierr            = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
1743   B->factor       = 0;
1744   B->assembled    = PETSC_FALSE;
1745   B->mapping      = 0;
1746 
1747   B->insertmode      = NOT_SET_VALUES;
1748   b->size            = size;
1749   ierr = MPI_Comm_rank(B->comm,&b->rank);CHKERRQ(ierr);
1750 
1751   ierr = PetscSplitOwnership(B->comm,&B->m,&B->M);CHKERRQ(ierr);
1752   ierr = PetscSplitOwnership(B->comm,&B->n,&B->N);CHKERRQ(ierr);
1753 
1754   /* the information in the maps duplicates the information computed below, eventually
1755      we should remove the duplicate information that is not contained in the maps */
1756   ierr = PetscMapCreateMPI(B->comm,B->m,B->M,&B->rmap);CHKERRQ(ierr);
1757   ierr = PetscMapCreateMPI(B->comm,B->n,B->N,&B->cmap);CHKERRQ(ierr);
1758 
1759   /* build local table of row and column ownerships */
1760   ierr = PetscMalloc(2*(b->size+2)*sizeof(int),&b->rowners);CHKERRQ(ierr);
1761   PetscLogObjectMemory(B,2*(b->size+2)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_MPIAIJ));
1762   b->cowners = b->rowners + b->size + 2;
1763   ierr = MPI_Allgather(&B->m,1,MPI_INT,b->rowners+1,1,MPI_INT,B->comm);CHKERRQ(ierr);
1764   b->rowners[0] = 0;
1765   for (i=2; i<=b->size; i++) {
1766     b->rowners[i] += b->rowners[i-1];
1767   }
1768   b->rstart = b->rowners[b->rank];
1769   b->rend   = b->rowners[b->rank+1];
1770   ierr = MPI_Allgather(&B->n,1,MPI_INT,b->cowners+1,1,MPI_INT,B->comm);CHKERRQ(ierr);
1771   b->cowners[0] = 0;
1772   for (i=2; i<=b->size; i++) {
1773     b->cowners[i] += b->cowners[i-1];
1774   }
1775   b->cstart = b->cowners[b->rank];
1776   b->cend   = b->cowners[b->rank+1];
1777 
1778   /* build cache for off array entries formed */
1779   ierr = MatStashCreate_Private(B->comm,1,&B->stash);CHKERRQ(ierr);
1780   b->donotstash  = PETSC_FALSE;
1781   b->colmap      = 0;
1782   b->garray      = 0;
1783   b->roworiented = PETSC_TRUE;
1784 
1785   /* stuff used for matrix vector multiply */
1786   b->lvec      = PETSC_NULL;
1787   b->Mvctx     = PETSC_NULL;
1788 
1789   /* stuff for MatGetRow() */
1790   b->rowindices   = 0;
1791   b->rowvalues    = 0;
1792   b->getrowactive = PETSC_FALSE;
1793 
1794   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
1795                                      "MatStoreValues_MPIAIJ",
1796                                      MatStoreValues_MPIAIJ);CHKERRQ(ierr);
1797   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
1798                                      "MatRetrieveValues_MPIAIJ",
1799                                      MatRetrieveValues_MPIAIJ);CHKERRQ(ierr);
1800   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetDiagonalBlock_C",
1801 				     "MatGetDiagonalBlock_MPIAIJ",
1802                                      MatGetDiagonalBlock_MPIAIJ);CHKERRQ(ierr);
1803   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsSymmetric_C",
1804 				     "MatIsSymmetric_MPIAIJ",
1805 				     MatIsSymmetric_MPIAIJ); CHKERRQ(ierr);
1806   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAIJSetPreallocation_C",
1807 				     "MatMPIAIJSetPreallocation_MPIAIJ",
1808 				     MatMPIAIJSetPreallocation_MPIAIJ); CHKERRQ(ierr);
1809   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatDiagonalScaleLocal_C",
1810 				     "MatDiagonalScaleLocal_MPIAIJ",
1811 				     MatDiagonalScaleLocal_MPIAIJ); CHKERRQ(ierr);
1812   PetscFunctionReturn(0);
1813 }
1814 EXTERN_C_END
1815 
1816 #undef __FUNCT__
1817 #define __FUNCT__ "MatDuplicate_MPIAIJ"
1818 int MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
1819 {
1820   Mat        mat;
1821   Mat_MPIAIJ *a,*oldmat = (Mat_MPIAIJ*)matin->data;
1822   int        ierr;
1823 
1824   PetscFunctionBegin;
1825   *newmat       = 0;
1826   ierr = MatCreate(matin->comm,matin->m,matin->n,matin->M,matin->N,&mat);CHKERRQ(ierr);
1827   ierr = MatSetType(mat,MATMPIAIJ);CHKERRQ(ierr);
1828   a    = (Mat_MPIAIJ*)mat->data;
1829   ierr              = PetscMemcpy(mat->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
1830   mat->factor       = matin->factor;
1831   mat->assembled    = PETSC_TRUE;
1832   mat->insertmode   = NOT_SET_VALUES;
1833   mat->preallocated = PETSC_TRUE;
1834 
1835   a->rstart       = oldmat->rstart;
1836   a->rend         = oldmat->rend;
1837   a->cstart       = oldmat->cstart;
1838   a->cend         = oldmat->cend;
1839   a->size         = oldmat->size;
1840   a->rank         = oldmat->rank;
1841   a->donotstash   = oldmat->donotstash;
1842   a->roworiented  = oldmat->roworiented;
1843   a->rowindices   = 0;
1844   a->rowvalues    = 0;
1845   a->getrowactive = PETSC_FALSE;
1846 
1847   ierr       = PetscMemcpy(a->rowners,oldmat->rowners,2*(a->size+2)*sizeof(int));CHKERRQ(ierr);
1848   ierr       = MatStashCreate_Private(matin->comm,1,&mat->stash);CHKERRQ(ierr);
1849   if (oldmat->colmap) {
1850 #if defined (PETSC_USE_CTABLE)
1851     ierr = PetscTableCreateCopy(oldmat->colmap,&a->colmap);CHKERRQ(ierr);
1852 #else
1853     ierr = PetscMalloc((mat->N)*sizeof(int),&a->colmap);CHKERRQ(ierr);
1854     PetscLogObjectMemory(mat,(mat->N)*sizeof(int));
1855     ierr      = PetscMemcpy(a->colmap,oldmat->colmap,(mat->N)*sizeof(int));CHKERRQ(ierr);
1856 #endif
1857   } else a->colmap = 0;
1858   if (oldmat->garray) {
1859     int len;
1860     len  = oldmat->B->n;
1861     ierr = PetscMalloc((len+1)*sizeof(int),&a->garray);CHKERRQ(ierr);
1862     PetscLogObjectMemory(mat,len*sizeof(int));
1863     if (len) { ierr = PetscMemcpy(a->garray,oldmat->garray,len*sizeof(int));CHKERRQ(ierr); }
1864   } else a->garray = 0;
1865 
1866   ierr =  VecDuplicate(oldmat->lvec,&a->lvec);CHKERRQ(ierr);
1867   PetscLogObjectParent(mat,a->lvec);
1868   ierr =  VecScatterCopy(oldmat->Mvctx,&a->Mvctx);CHKERRQ(ierr);
1869   PetscLogObjectParent(mat,a->Mvctx);
1870   ierr =  MatDuplicate(oldmat->A,cpvalues,&a->A);CHKERRQ(ierr);
1871   PetscLogObjectParent(mat,a->A);
1872   ierr =  MatDuplicate(oldmat->B,cpvalues,&a->B);CHKERRQ(ierr);
1873   PetscLogObjectParent(mat,a->B);
1874   ierr = PetscFListDuplicate(matin->qlist,&mat->qlist);CHKERRQ(ierr);
1875   *newmat = mat;
1876   PetscFunctionReturn(0);
1877 }
1878 
1879 #include "petscsys.h"
1880 
1881 EXTERN_C_BEGIN
1882 #undef __FUNCT__
1883 #define __FUNCT__ "MatLoad_MPIAIJ"
1884 int MatLoad_MPIAIJ(PetscViewer viewer,MatType type,Mat *newmat)
1885 {
1886   Mat          A;
1887   PetscScalar  *vals,*svals;
1888   MPI_Comm     comm = ((PetscObject)viewer)->comm;
1889   MPI_Status   status;
1890   int          i,nz,ierr,j,rstart,rend,fd;
1891   int          header[4],rank,size,*rowlengths = 0,M,N,m,*rowners,maxnz,*cols;
1892   int          *ourlens,*sndcounts = 0,*procsnz = 0,*offlens,jj,*mycols,*smycols;
1893   int          tag = ((PetscObject)viewer)->tag,cend,cstart,n;
1894 
1895   PetscFunctionBegin;
1896   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
1897   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
1898   if (!rank) {
1899     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
1900     ierr = PetscBinaryRead(fd,(char *)header,4,PETSC_INT);CHKERRQ(ierr);
1901     if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
1902     if (header[3] < 0) {
1903       SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix in special format on disk, cannot load as MPIAIJ");
1904     }
1905   }
1906 
1907   ierr = MPI_Bcast(header+1,3,MPI_INT,0,comm);CHKERRQ(ierr);
1908   M = header[1]; N = header[2];
1909   /* determine ownership of all rows */
1910   m = M/size + ((M % size) > rank);
1911   ierr = PetscMalloc((size+2)*sizeof(int),&rowners);CHKERRQ(ierr);
1912   ierr = MPI_Allgather(&m,1,MPI_INT,rowners+1,1,MPI_INT,comm);CHKERRQ(ierr);
1913   rowners[0] = 0;
1914   for (i=2; i<=size; i++) {
1915     rowners[i] += rowners[i-1];
1916   }
1917   rstart = rowners[rank];
1918   rend   = rowners[rank+1];
1919 
1920   /* distribute row lengths to all processors */
1921   ierr    = PetscMalloc(2*(rend-rstart+1)*sizeof(int),&ourlens);CHKERRQ(ierr);
1922   offlens = ourlens + (rend-rstart);
1923   if (!rank) {
1924     ierr = PetscMalloc(M*sizeof(int),&rowlengths);CHKERRQ(ierr);
1925     ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
1926     ierr = PetscMalloc(size*sizeof(int),&sndcounts);CHKERRQ(ierr);
1927     for (i=0; i<size; i++) sndcounts[i] = rowners[i+1] - rowners[i];
1928     ierr = MPI_Scatterv(rowlengths,sndcounts,rowners,MPI_INT,ourlens,rend-rstart,MPI_INT,0,comm);CHKERRQ(ierr);
1929     ierr = PetscFree(sndcounts);CHKERRQ(ierr);
1930   } else {
1931     ierr = MPI_Scatterv(0,0,0,MPI_INT,ourlens,rend-rstart,MPI_INT,0,comm);CHKERRQ(ierr);
1932   }
1933 
1934   if (!rank) {
1935     /* calculate the number of nonzeros on each processor */
1936     ierr = PetscMalloc(size*sizeof(int),&procsnz);CHKERRQ(ierr);
1937     ierr = PetscMemzero(procsnz,size*sizeof(int));CHKERRQ(ierr);
1938     for (i=0; i<size; i++) {
1939       for (j=rowners[i]; j< rowners[i+1]; j++) {
1940         procsnz[i] += rowlengths[j];
1941       }
1942     }
1943     ierr = PetscFree(rowlengths);CHKERRQ(ierr);
1944 
1945     /* determine max buffer needed and allocate it */
1946     maxnz = 0;
1947     for (i=0; i<size; i++) {
1948       maxnz = PetscMax(maxnz,procsnz[i]);
1949     }
1950     ierr = PetscMalloc(maxnz*sizeof(int),&cols);CHKERRQ(ierr);
1951 
1952     /* read in my part of the matrix column indices  */
1953     nz   = procsnz[0];
1954     ierr = PetscMalloc(nz*sizeof(int),&mycols);CHKERRQ(ierr);
1955     ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr);
1956 
1957     /* read in every one elses and ship off */
1958     for (i=1; i<size; i++) {
1959       nz   = procsnz[i];
1960       ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr);
1961       ierr = MPI_Send(cols,nz,MPI_INT,i,tag,comm);CHKERRQ(ierr);
1962     }
1963     ierr = PetscFree(cols);CHKERRQ(ierr);
1964   } else {
1965     /* determine buffer space needed for message */
1966     nz = 0;
1967     for (i=0; i<m; i++) {
1968       nz += ourlens[i];
1969     }
1970     ierr = PetscMalloc((nz+1)*sizeof(int),&mycols);CHKERRQ(ierr);
1971 
1972     /* receive message of column indices*/
1973     ierr = MPI_Recv(mycols,nz,MPI_INT,0,tag,comm,&status);CHKERRQ(ierr);
1974     ierr = MPI_Get_count(&status,MPI_INT,&maxnz);CHKERRQ(ierr);
1975     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
1976   }
1977 
1978   /* determine column ownership if matrix is not square */
1979   if (N != M) {
1980     n      = N/size + ((N % size) > rank);
1981     ierr   = MPI_Scan(&n,&cend,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr);
1982     cstart = cend - n;
1983   } else {
1984     cstart = rstart;
1985     cend   = rend;
1986     n      = cend - cstart;
1987   }
1988 
1989   /* loop over local rows, determining number of off diagonal entries */
1990   ierr = PetscMemzero(offlens,m*sizeof(int));CHKERRQ(ierr);
1991   jj = 0;
1992   for (i=0; i<m; i++) {
1993     for (j=0; j<ourlens[i]; j++) {
1994       if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++;
1995       jj++;
1996     }
1997   }
1998 
1999   /* create our matrix */
2000   for (i=0; i<m; i++) {
2001     ourlens[i] -= offlens[i];
2002   }
2003   ierr = MatCreate(comm,m,n,M,N,&A);CHKERRQ(ierr);
2004   ierr = MatSetType(A,type);CHKERRQ(ierr);
2005   ierr = MatMPIAIJSetPreallocation(A,0,ourlens,0,offlens);CHKERRQ(ierr);
2006 
2007   ierr = MatSetOption(A,MAT_COLUMNS_SORTED);CHKERRQ(ierr);
2008   for (i=0; i<m; i++) {
2009     ourlens[i] += offlens[i];
2010   }
2011 
2012   if (!rank) {
2013     ierr = PetscMalloc(maxnz*sizeof(PetscScalar),&vals);CHKERRQ(ierr);
2014 
2015     /* read in my part of the matrix numerical values  */
2016     nz   = procsnz[0];
2017     ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
2018 
2019     /* insert into matrix */
2020     jj      = rstart;
2021     smycols = mycols;
2022     svals   = vals;
2023     for (i=0; i<m; i++) {
2024       ierr = MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
2025       smycols += ourlens[i];
2026       svals   += ourlens[i];
2027       jj++;
2028     }
2029 
2030     /* read in other processors and ship out */
2031     for (i=1; i<size; i++) {
2032       nz   = procsnz[i];
2033       ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
2034       ierr = MPI_Send(vals,nz,MPIU_SCALAR,i,A->tag,comm);CHKERRQ(ierr);
2035     }
2036     ierr = PetscFree(procsnz);CHKERRQ(ierr);
2037   } else {
2038     /* receive numeric values */
2039     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&vals);CHKERRQ(ierr);
2040 
2041     /* receive message of values*/
2042     ierr = MPI_Recv(vals,nz,MPIU_SCALAR,0,A->tag,comm,&status);CHKERRQ(ierr);
2043     ierr = MPI_Get_count(&status,MPIU_SCALAR,&maxnz);CHKERRQ(ierr);
2044     if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
2045 
2046     /* insert into matrix */
2047     jj      = rstart;
2048     smycols = mycols;
2049     svals   = vals;
2050     for (i=0; i<m; i++) {
2051       ierr     = MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
2052       smycols += ourlens[i];
2053       svals   += ourlens[i];
2054       jj++;
2055     }
2056   }
2057   ierr = PetscFree(ourlens);CHKERRQ(ierr);
2058   ierr = PetscFree(vals);CHKERRQ(ierr);
2059   ierr = PetscFree(mycols);CHKERRQ(ierr);
2060   ierr = PetscFree(rowners);CHKERRQ(ierr);
2061 
2062   ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2063   ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2064   *newmat = A;
2065   PetscFunctionReturn(0);
2066 }
2067 EXTERN_C_END
2068 
2069 #undef __FUNCT__
2070 #define __FUNCT__ "MatGetSubMatrix_MPIAIJ"
2071 /*
2072     Not great since it makes two copies of the submatrix, first an SeqAIJ
2073   in local and then by concatenating the local matrices the end result.
2074   Writing it directly would be much like MatGetSubMatrices_MPIAIJ()
2075 */
2076 int MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,int csize,MatReuse call,Mat *newmat)
2077 {
2078   int          ierr,i,m,n,rstart,row,rend,nz,*cwork,size,rank,j;
2079   int          *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal;
2080   Mat          *local,M,Mreuse;
2081   PetscScalar  *vwork,*aa;
2082   MPI_Comm     comm = mat->comm;
2083   Mat_SeqAIJ   *aij;
2084 
2085 
2086   PetscFunctionBegin;
2087   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
2088   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
2089 
2090   if (call ==  MAT_REUSE_MATRIX) {
2091     ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);CHKERRQ(ierr);
2092     if (!Mreuse) SETERRQ(1,"Submatrix passed in was not used before, cannot reuse");
2093     local = &Mreuse;
2094     ierr  = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&local);CHKERRQ(ierr);
2095   } else {
2096     ierr   = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr);
2097     Mreuse = *local;
2098     ierr   = PetscFree(local);CHKERRQ(ierr);
2099   }
2100 
2101   /*
2102       m - number of local rows
2103       n - number of columns (same on all processors)
2104       rstart - first row in new global matrix generated
2105   */
2106   ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr);
2107   if (call == MAT_INITIAL_MATRIX) {
2108     aij = (Mat_SeqAIJ*)(Mreuse)->data;
2109     ii  = aij->i;
2110     jj  = aij->j;
2111 
2112     /*
2113         Determine the number of non-zeros in the diagonal and off-diagonal
2114         portions of the matrix in order to do correct preallocation
2115     */
2116 
2117     /* first get start and end of "diagonal" columns */
2118     if (csize == PETSC_DECIDE) {
2119       ierr = ISGetSize(isrow,&mglobal);CHKERRQ(ierr);
2120       if (mglobal == n) { /* square matrix */
2121 	nlocal = m;
2122       } else {
2123         nlocal = n/size + ((n % size) > rank);
2124       }
2125     } else {
2126       nlocal = csize;
2127     }
2128     ierr   = MPI_Scan(&nlocal,&rend,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr);
2129     rstart = rend - nlocal;
2130     if (rank == size - 1 && rend != n) {
2131       SETERRQ2(1,"Local column sizes %d do not add up to total number of columns %d",rend,n);
2132     }
2133 
2134     /* next, compute all the lengths */
2135     ierr  = PetscMalloc((2*m+1)*sizeof(int),&dlens);CHKERRQ(ierr);
2136     olens = dlens + m;
2137     for (i=0; i<m; i++) {
2138       jend = ii[i+1] - ii[i];
2139       olen = 0;
2140       dlen = 0;
2141       for (j=0; j<jend; j++) {
2142         if (*jj < rstart || *jj >= rend) olen++;
2143         else dlen++;
2144         jj++;
2145       }
2146       olens[i] = olen;
2147       dlens[i] = dlen;
2148     }
2149     ierr = MatCreateMPIAIJ(comm,m,nlocal,PETSC_DECIDE,n,0,dlens,0,olens,&M);CHKERRQ(ierr);
2150     ierr = PetscFree(dlens);CHKERRQ(ierr);
2151   } else {
2152     int ml,nl;
2153 
2154     M = *newmat;
2155     ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr);
2156     if (ml != m) SETERRQ(PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
2157     ierr = MatZeroEntries(M);CHKERRQ(ierr);
2158     /*
2159          The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
2160        rather than the slower MatSetValues().
2161     */
2162     M->was_assembled = PETSC_TRUE;
2163     M->assembled     = PETSC_FALSE;
2164   }
2165   ierr = MatGetOwnershipRange(M,&rstart,&rend);CHKERRQ(ierr);
2166   aij = (Mat_SeqAIJ*)(Mreuse)->data;
2167   ii  = aij->i;
2168   jj  = aij->j;
2169   aa  = aij->a;
2170   for (i=0; i<m; i++) {
2171     row   = rstart + i;
2172     nz    = ii[i+1] - ii[i];
2173     cwork = jj;     jj += nz;
2174     vwork = aa;     aa += nz;
2175     ierr = MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr);
2176   }
2177 
2178   ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2179   ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2180   *newmat = M;
2181 
2182   /* save submatrix used in processor for next request */
2183   if (call ==  MAT_INITIAL_MATRIX) {
2184     ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr);
2185     ierr = PetscObjectDereference((PetscObject)Mreuse);CHKERRQ(ierr);
2186   }
2187 
2188   PetscFunctionReturn(0);
2189 }
2190 
2191 #undef __FUNCT__
2192 #define __FUNCT__ "MatMPIAIJSetPreallocation"
2193 /*@C
2194    MatMPIAIJSetPreallocation - Creates a sparse parallel matrix in AIJ format
2195    (the default parallel PETSc format).  For good matrix assembly performance
2196    the user should preallocate the matrix storage by setting the parameters
2197    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
2198    performance can be increased by more than a factor of 50.
2199 
2200    Collective on MPI_Comm
2201 
2202    Input Parameters:
2203 +  A - the matrix
2204 .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
2205            (same value is used for all local rows)
2206 .  d_nnz - array containing the number of nonzeros in the various rows of the
2207            DIAGONAL portion of the local submatrix (possibly different for each row)
2208            or PETSC_NULL, if d_nz is used to specify the nonzero structure.
2209            The size of this array is equal to the number of local rows, i.e 'm'.
2210            You must leave room for the diagonal entry even if it is zero.
2211 .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
2212            submatrix (same value is used for all local rows).
2213 -  o_nnz - array containing the number of nonzeros in the various rows of the
2214            OFF-DIAGONAL portion of the local submatrix (possibly different for
2215            each row) or PETSC_NULL, if o_nz is used to specify the nonzero
2216            structure. The size of this array is equal to the number
2217            of local rows, i.e 'm'.
2218 
2219    The AIJ format (also called the Yale sparse matrix format or
2220    compressed row storage), is fully compatible with standard Fortran 77
2221    storage.  That is, the stored row and column indices can begin at
2222    either one (as in Fortran) or zero.  See the users manual for details.
2223 
2224    The user MUST specify either the local or global matrix dimensions
2225    (possibly both).
2226 
2227    The parallel matrix is partitioned such that the first m0 rows belong to
2228    process 0, the next m1 rows belong to process 1, the next m2 rows belong
2229    to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.
2230 
2231    The DIAGONAL portion of the local submatrix of a processor can be defined
2232    as the submatrix which is obtained by extraction the part corresponding
2233    to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the
2234    first row that belongs to the processor, and r2 is the last row belonging
2235    to the this processor. This is a square mxm matrix. The remaining portion
2236    of the local submatrix (mxN) constitute the OFF-DIAGONAL portion.
2237 
2238    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
2239 
2240    By default, this format uses inodes (identical nodes) when possible.
2241    We search for consecutive rows with the same nonzero structure, thereby
2242    reusing matrix information to achieve increased efficiency.
2243 
2244    Options Database Keys:
2245 +  -mat_aij_no_inode  - Do not use inodes
2246 .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2247 -  -mat_aij_oneindex - Internally use indexing starting at 1
2248         rather than 0.  Note that when calling MatSetValues(),
2249         the user still MUST index entries starting at 0!
2250 
2251    Example usage:
2252 
2253    Consider the following 8x8 matrix with 34 non-zero values, that is
2254    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
2255    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
2256    as follows:
2257 
2258 .vb
2259             1  2  0  |  0  3  0  |  0  4
2260     Proc0   0  5  6  |  7  0  0  |  8  0
2261             9  0 10  | 11  0  0  | 12  0
2262     -------------------------------------
2263            13  0 14  | 15 16 17  |  0  0
2264     Proc1   0 18  0  | 19 20 21  |  0  0
2265             0  0  0  | 22 23  0  | 24  0
2266     -------------------------------------
2267     Proc2  25 26 27  |  0  0 28  | 29  0
2268            30  0  0  | 31 32 33  |  0 34
2269 .ve
2270 
2271    This can be represented as a collection of submatrices as:
2272 
2273 .vb
2274       A B C
2275       D E F
2276       G H I
2277 .ve
2278 
2279    Where the submatrices A,B,C are owned by proc0, D,E,F are
2280    owned by proc1, G,H,I are owned by proc2.
2281 
2282    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2283    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2284    The 'M','N' parameters are 8,8, and have the same values on all procs.
2285 
2286    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
2287    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
2288    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
2289    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
2290    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
2291    matrix, ans [DF] as another SeqAIJ matrix.
2292 
2293    When d_nz, o_nz parameters are specified, d_nz storage elements are
2294    allocated for every row of the local diagonal submatrix, and o_nz
2295    storage locations are allocated for every row of the OFF-DIAGONAL submat.
2296    One way to choose d_nz and o_nz is to use the max nonzerors per local
2297    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
2298    In this case, the values of d_nz,o_nz are:
2299 .vb
2300      proc0 : dnz = 2, o_nz = 2
2301      proc1 : dnz = 3, o_nz = 2
2302      proc2 : dnz = 1, o_nz = 4
2303 .ve
2304    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
2305    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
2306    for proc3. i.e we are using 12+15+10=37 storage locations to store
2307    34 values.
2308 
2309    When d_nnz, o_nnz parameters are specified, the storage is specified
2310    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
2311    In the above case the values for d_nnz,o_nnz are:
2312 .vb
2313      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
2314      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
2315      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
2316 .ve
2317    Here the space allocated is sum of all the above values i.e 34, and
2318    hence pre-allocation is perfect.
2319 
2320    Level: intermediate
2321 
2322 .keywords: matrix, aij, compressed row, sparse, parallel
2323 
2324 .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues()
2325 @*/
2326 int MatMPIAIJSetPreallocation(Mat B,int d_nz,const int d_nnz[],int o_nz,const int o_nnz[])
2327 {
2328   int ierr,(*f)(Mat,int,const int[],int,const int[]);
2329 
2330   PetscFunctionBegin;
2331   ierr = PetscObjectQueryFunction((PetscObject)B,"MatMPIAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
2332   if (f) {
2333     ierr = (*f)(B,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr);
2334   }
2335   PetscFunctionReturn(0);
2336 }
2337 
2338 #undef __FUNCT__
2339 #define __FUNCT__ "MatCreateMPIAIJ"
2340 /*@C
2341    MatCreateMPIAIJ - Creates a sparse parallel matrix in AIJ format
2342    (the default parallel PETSc format).  For good matrix assembly performance
2343    the user should preallocate the matrix storage by setting the parameters
2344    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
2345    performance can be increased by more than a factor of 50.
2346 
2347    Collective on MPI_Comm
2348 
2349    Input Parameters:
2350 +  comm - MPI communicator
2351 .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
2352            This value should be the same as the local size used in creating the
2353            y vector for the matrix-vector product y = Ax.
2354 .  n - This value should be the same as the local size used in creating the
2355        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
2356        calculated if N is given) For square matrices n is almost always m.
2357 .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
2358 .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
2359 .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
2360            (same value is used for all local rows)
2361 .  d_nnz - array containing the number of nonzeros in the various rows of the
2362            DIAGONAL portion of the local submatrix (possibly different for each row)
2363            or PETSC_NULL, if d_nz is used to specify the nonzero structure.
2364            The size of this array is equal to the number of local rows, i.e 'm'.
2365            You must leave room for the diagonal entry even if it is zero.
2366 .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
2367            submatrix (same value is used for all local rows).
2368 -  o_nnz - array containing the number of nonzeros in the various rows of the
2369            OFF-DIAGONAL portion of the local submatrix (possibly different for
2370            each row) or PETSC_NULL, if o_nz is used to specify the nonzero
2371            structure. The size of this array is equal to the number
2372            of local rows, i.e 'm'.
2373 
2374    Output Parameter:
2375 .  A - the matrix
2376 
2377    Notes:
2378    m,n,M,N parameters specify the size of the matrix, and its partitioning across
2379    processors, while d_nz,d_nnz,o_nz,o_nnz parameters specify the approximate
2380    storage requirements for this matrix.
2381 
2382    If PETSC_DECIDE or  PETSC_DETERMINE is used for a particular argument on one
2383    processor than it must be used on all processors that share the object for
2384    that argument.
2385 
2386    The AIJ format (also called the Yale sparse matrix format or
2387    compressed row storage), is fully compatible with standard Fortran 77
2388    storage.  That is, the stored row and column indices can begin at
2389    either one (as in Fortran) or zero.  See the users manual for details.
2390 
2391    The user MUST specify either the local or global matrix dimensions
2392    (possibly both).
2393 
2394    The parallel matrix is partitioned such that the first m0 rows belong to
2395    process 0, the next m1 rows belong to process 1, the next m2 rows belong
2396    to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.
2397 
2398    The DIAGONAL portion of the local submatrix of a processor can be defined
2399    as the submatrix which is obtained by extraction the part corresponding
2400    to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the
2401    first row that belongs to the processor, and r2 is the last row belonging
2402    to the this processor. This is a square mxm matrix. The remaining portion
2403    of the local submatrix (mxN) constitute the OFF-DIAGONAL portion.
2404 
2405    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
2406 
2407    When calling this routine with a single process communicator, a matrix of
2408    type SEQAIJ is returned.  If a matrix of type MPIAIJ is desired for this
2409    type of communicator, use the construction mechanism:
2410      MatCreate(...,&A); MatSetType(A,MPIAIJ); MatMPIAIJSetPreallocation(A,...);
2411 
2412    By default, this format uses inodes (identical nodes) when possible.
2413    We search for consecutive rows with the same nonzero structure, thereby
2414    reusing matrix information to achieve increased efficiency.
2415 
2416    Options Database Keys:
2417 +  -mat_aij_no_inode  - Do not use inodes
2418 .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2419 -  -mat_aij_oneindex - Internally use indexing starting at 1
2420         rather than 0.  Note that when calling MatSetValues(),
2421         the user still MUST index entries starting at 0!
2422 
2423 
2424    Example usage:
2425 
2426    Consider the following 8x8 matrix with 34 non-zero values, that is
2427    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
2428    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
2429    as follows:
2430 
2431 .vb
2432             1  2  0  |  0  3  0  |  0  4
2433     Proc0   0  5  6  |  7  0  0  |  8  0
2434             9  0 10  | 11  0  0  | 12  0
2435     -------------------------------------
2436            13  0 14  | 15 16 17  |  0  0
2437     Proc1   0 18  0  | 19 20 21  |  0  0
2438             0  0  0  | 22 23  0  | 24  0
2439     -------------------------------------
2440     Proc2  25 26 27  |  0  0 28  | 29  0
2441            30  0  0  | 31 32 33  |  0 34
2442 .ve
2443 
2444    This can be represented as a collection of submatrices as:
2445 
2446 .vb
2447       A B C
2448       D E F
2449       G H I
2450 .ve
2451 
2452    Where the submatrices A,B,C are owned by proc0, D,E,F are
2453    owned by proc1, G,H,I are owned by proc2.
2454 
2455    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2456    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2457    The 'M','N' parameters are 8,8, and have the same values on all procs.
2458 
2459    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
2460    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
2461    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
2462    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
2463    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
2464    matrix, ans [DF] as another SeqAIJ matrix.
2465 
2466    When d_nz, o_nz parameters are specified, d_nz storage elements are
2467    allocated for every row of the local diagonal submatrix, and o_nz
2468    storage locations are allocated for every row of the OFF-DIAGONAL submat.
2469    One way to choose d_nz and o_nz is to use the max nonzerors per local
2470    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
2471    In this case, the values of d_nz,o_nz are:
2472 .vb
2473      proc0 : dnz = 2, o_nz = 2
2474      proc1 : dnz = 3, o_nz = 2
2475      proc2 : dnz = 1, o_nz = 4
2476 .ve
2477    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
2478    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
2479    for proc3. i.e we are using 12+15+10=37 storage locations to store
2480    34 values.
2481 
2482    When d_nnz, o_nnz parameters are specified, the storage is specified
2483    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
2484    In the above case the values for d_nnz,o_nnz are:
2485 .vb
2486      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
2487      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
2488      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
2489 .ve
2490    Here the space allocated is sum of all the above values i.e 34, and
2491    hence pre-allocation is perfect.
2492 
2493    Level: intermediate
2494 
2495 .keywords: matrix, aij, compressed row, sparse, parallel
2496 
2497 .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues()
2498 @*/
2499 int MatCreateMPIAIJ(MPI_Comm comm,int m,int n,int M,int N,int d_nz,const int d_nnz[],int o_nz,const int o_nnz[],Mat *A)
2500 {
2501   int ierr,size;
2502 
2503   PetscFunctionBegin;
2504   ierr = MatCreate(comm,m,n,M,N,A);CHKERRQ(ierr);
2505   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
2506   if (size > 1) {
2507     ierr = MatSetType(*A,MATMPIAIJ);CHKERRQ(ierr);
2508     ierr = MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr);
2509   } else {
2510     ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2511     ierr = MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);CHKERRQ(ierr);
2512   }
2513   PetscFunctionReturn(0);
2514 }
2515 
2516 #undef __FUNCT__
2517 #define __FUNCT__ "MatMPIAIJGetSeqAIJ"
2518 int MatMPIAIJGetSeqAIJ(Mat A,Mat *Ad,Mat *Ao,int *colmap[])
2519 {
2520   Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;
2521   PetscFunctionBegin;
2522   *Ad     = a->A;
2523   *Ao     = a->B;
2524   *colmap = a->garray;
2525   PetscFunctionReturn(0);
2526 }
2527 
2528 #undef __FUNCT__
2529 #define __FUNCT__ "MatSetColoring_MPIAIJ"
2530 int MatSetColoring_MPIAIJ(Mat A,ISColoring coloring)
2531 {
2532   int        ierr,i;
2533   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2534 
2535   PetscFunctionBegin;
2536   if (coloring->ctype == IS_COLORING_LOCAL) {
2537     ISColoringValue *allcolors,*colors;
2538     ISColoring      ocoloring;
2539 
2540     /* set coloring for diagonal portion */
2541     ierr = MatSetColoring_SeqAIJ(a->A,coloring);CHKERRQ(ierr);
2542 
2543     /* set coloring for off-diagonal portion */
2544     ierr = ISAllGatherColors(A->comm,coloring->n,coloring->colors,PETSC_NULL,&allcolors);CHKERRQ(ierr);
2545     ierr = PetscMalloc((a->B->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
2546     for (i=0; i<a->B->n; i++) {
2547       colors[i] = allcolors[a->garray[i]];
2548     }
2549     ierr = PetscFree(allcolors);CHKERRQ(ierr);
2550     ierr = ISColoringCreate(MPI_COMM_SELF,a->B->n,colors,&ocoloring);CHKERRQ(ierr);
2551     ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr);
2552     ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr);
2553   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
2554     ISColoringValue *colors;
2555     int             *larray;
2556     ISColoring      ocoloring;
2557 
2558     /* set coloring for diagonal portion */
2559     ierr = PetscMalloc((a->A->n+1)*sizeof(int),&larray);CHKERRQ(ierr);
2560     for (i=0; i<a->A->n; i++) {
2561       larray[i] = i + a->cstart;
2562     }
2563     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->A->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
2564     ierr = PetscMalloc((a->A->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
2565     for (i=0; i<a->A->n; i++) {
2566       colors[i] = coloring->colors[larray[i]];
2567     }
2568     ierr = PetscFree(larray);CHKERRQ(ierr);
2569     ierr = ISColoringCreate(PETSC_COMM_SELF,a->A->n,colors,&ocoloring);CHKERRQ(ierr);
2570     ierr = MatSetColoring_SeqAIJ(a->A,ocoloring);CHKERRQ(ierr);
2571     ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr);
2572 
2573     /* set coloring for off-diagonal portion */
2574     ierr = PetscMalloc((a->B->n+1)*sizeof(int),&larray);CHKERRQ(ierr);
2575     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->B->n,a->garray,PETSC_NULL,larray);CHKERRQ(ierr);
2576     ierr = PetscMalloc((a->B->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
2577     for (i=0; i<a->B->n; i++) {
2578       colors[i] = coloring->colors[larray[i]];
2579     }
2580     ierr = PetscFree(larray);CHKERRQ(ierr);
2581     ierr = ISColoringCreate(MPI_COMM_SELF,a->B->n,colors,&ocoloring);CHKERRQ(ierr);
2582     ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr);
2583     ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr);
2584   } else {
2585     SETERRQ1(1,"No support ISColoringType %d",coloring->ctype);
2586   }
2587 
2588   PetscFunctionReturn(0);
2589 }
2590 
2591 #undef __FUNCT__
2592 #define __FUNCT__ "MatSetValuesAdic_MPIAIJ"
2593 int MatSetValuesAdic_MPIAIJ(Mat A,void *advalues)
2594 {
2595   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2596   int        ierr;
2597 
2598   PetscFunctionBegin;
2599   ierr = MatSetValuesAdic_SeqAIJ(a->A,advalues);CHKERRQ(ierr);
2600   ierr = MatSetValuesAdic_SeqAIJ(a->B,advalues);CHKERRQ(ierr);
2601   PetscFunctionReturn(0);
2602 }
2603 
2604 #undef __FUNCT__
2605 #define __FUNCT__ "MatSetValuesAdifor_MPIAIJ"
2606 int MatSetValuesAdifor_MPIAIJ(Mat A,int nl,void *advalues)
2607 {
2608   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2609   int        ierr;
2610 
2611   PetscFunctionBegin;
2612   ierr = MatSetValuesAdifor_SeqAIJ(a->A,nl,advalues);CHKERRQ(ierr);
2613   ierr = MatSetValuesAdifor_SeqAIJ(a->B,nl,advalues);CHKERRQ(ierr);
2614   PetscFunctionReturn(0);
2615 }
2616 
2617 #undef __FUNCT__
2618 #define __FUNCT__ "MatMerge"
2619 /*@C
2620       MatMerge - Creates a single large PETSc matrix by concatinating sequential
2621                  matrices from each processor
2622 
2623     Collective on MPI_Comm
2624 
2625    Input Parameters:
2626 +    comm - the communicators the parallel matrix will live on
2627 -    inmat - the input sequential matrices
2628 
2629    Output Parameter:
2630 .    outmat - the parallel matrix generated
2631 
2632     Level: advanced
2633 
2634    Notes: The number of columns of the matrix in EACH of the seperate files
2635       MUST be the same.
2636 
2637 @*/
2638 int MatMerge(MPI_Comm comm,Mat inmat, Mat *outmat)
2639 {
2640   int         ierr,m,n,i,rstart,*indx,nnz,I,*dnz,*onz;
2641   PetscScalar *values;
2642   PetscMap    columnmap,rowmap;
2643 
2644   PetscFunctionBegin;
2645 
2646   ierr = MatGetSize(inmat,&m,&n);CHKERRQ(ierr);
2647 
2648   /* count nonzeros in each row, for diagonal and off diagonal portion of matrix */
2649   ierr = PetscMapCreate(comm,&columnmap);CHKERRQ(ierr);
2650   ierr = PetscMapSetSize(columnmap,n);CHKERRQ(ierr);
2651   ierr = PetscMapSetType(columnmap,MAP_MPI);CHKERRQ(ierr);
2652   ierr = PetscMapGetLocalSize(columnmap,&n);CHKERRQ(ierr);
2653   ierr = PetscMapDestroy(columnmap);CHKERRQ(ierr);
2654 
2655   ierr = PetscMapCreate(comm,&rowmap);CHKERRQ(ierr);
2656   ierr = PetscMapSetLocalSize(rowmap,m);CHKERRQ(ierr);
2657   ierr = PetscMapSetType(rowmap,MAP_MPI);CHKERRQ(ierr);
2658   ierr = PetscMapGetLocalRange(rowmap,&rstart,0);CHKERRQ(ierr);
2659   ierr = PetscMapDestroy(rowmap);CHKERRQ(ierr);
2660 
2661   ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr);
2662   for (i=0;i<m;i++) {
2663     ierr = MatGetRow(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
2664     ierr = MatPreallocateSet(i+rstart,nnz,indx,dnz,onz);CHKERRQ(ierr);
2665     ierr = MatRestoreRow(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
2666   }
2667   ierr = MatCreateMPIAIJ(comm,m,n,PETSC_DETERMINE,PETSC_DETERMINE,0,dnz,0,onz,outmat);CHKERRQ(ierr);
2668   ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr);
2669 
2670   for (i=0;i<m;i++) {
2671     ierr = MatGetRow(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
2672     I    = i + rstart;
2673     ierr = MatSetValues(*outmat,1,&I,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr);
2674     ierr = MatRestoreRow(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
2675   }
2676   ierr = MatDestroy(inmat);CHKERRQ(ierr);
2677   ierr = MatAssemblyBegin(*outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2678   ierr = MatAssemblyEnd(*outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2679 
2680   PetscFunctionReturn(0);
2681 }
2682 
2683 #undef __FUNCT__
2684 #define __FUNCT__ "MatFileSplit"
2685 int MatFileSplit(Mat A,char *outfile)
2686 {
2687   int         ierr,rank,len,m,N,i,rstart,*indx,nnz;
2688   PetscViewer out;
2689   char        *name;
2690   Mat         B;
2691   PetscScalar *values;
2692 
2693   PetscFunctionBegin;
2694 
2695   ierr = MatGetLocalSize(A,&m,0);CHKERRQ(ierr);
2696   ierr = MatGetSize(A,0,&N);CHKERRQ(ierr);
2697   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,m,N,0,0,&B);CHKERRQ(ierr);
2698   ierr = MatGetOwnershipRange(A,&rstart,0);CHKERRQ(ierr);
2699   for (i=0;i<m;i++) {
2700     ierr = MatGetRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr);
2701     ierr = MatSetValues(B,1,&i,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr);
2702     ierr = MatRestoreRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr);
2703   }
2704   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2705   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2706 
2707   ierr = MPI_Comm_rank(A->comm,&rank);CHKERRQ(ierr);
2708   ierr = PetscStrlen(outfile,&len);CHKERRQ(ierr);
2709   ierr = PetscMalloc((len+5)*sizeof(char),&name);CHKERRQ(ierr);
2710   sprintf(name,"%s.%d",outfile,rank);
2711   ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,name,PETSC_BINARY_CREATE,&out);CHKERRQ(ierr);
2712   ierr = PetscFree(name);
2713   ierr = MatView(B,out);CHKERRQ(ierr);
2714   ierr = PetscViewerDestroy(out);CHKERRQ(ierr);
2715   ierr = MatDestroy(B);CHKERRQ(ierr);
2716   PetscFunctionReturn(0);
2717 }
2718