xref: /petsc/src/mat/impls/aij/mpi/mpiaij.c (revision 4e9800392e9bd1e766097dd4159f1dd77fe50479)
18a729477SBarry Smith 
2c6db04a5SJed Brown #include <../src/mat/impls/aij/mpi/mpiaij.h>   /*I "petscmat.h" I*/
3c6db04a5SJed Brown #include <petscblaslapack.h>
48a729477SBarry Smith 
501bebe75SBarry Smith /*MC
601bebe75SBarry Smith    MATAIJ - MATAIJ = "aij" - A matrix type to be used for sparse matrices.
701bebe75SBarry Smith 
801bebe75SBarry Smith    This matrix type is identical to MATSEQAIJ when constructed with a single process communicator,
901bebe75SBarry Smith    and MATMPIAIJ otherwise.  As a result, for single process communicators,
1001bebe75SBarry Smith   MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation is supported
1101bebe75SBarry Smith   for communicators controlling multiple processes.  It is recommended that you call both of
1201bebe75SBarry Smith   the above preallocation routines for simplicity.
1301bebe75SBarry Smith 
1401bebe75SBarry Smith    Options Database Keys:
1501bebe75SBarry Smith . -mat_type aij - sets the matrix type to "aij" during a call to MatSetFromOptions()
1601bebe75SBarry Smith 
1701bebe75SBarry Smith   Developer Notes: Subclasses include MATAIJCUSP, MATAIJPERM, MATAIJCRL, and also automatically switches over to use inodes when
1801bebe75SBarry Smith    enough exist.
1901bebe75SBarry Smith 
2001bebe75SBarry Smith   Level: beginner
2101bebe75SBarry Smith 
2269b1f4b7SBarry Smith .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ
2301bebe75SBarry Smith M*/
2401bebe75SBarry Smith 
2501bebe75SBarry Smith /*MC
2601bebe75SBarry Smith    MATAIJCRL - MATAIJCRL = "aijcrl" - A matrix type to be used for sparse matrices.
2701bebe75SBarry Smith 
2801bebe75SBarry Smith    This matrix type is identical to MATSEQAIJCRL when constructed with a single process communicator,
2901bebe75SBarry Smith    and MATMPIAIJCRL otherwise.  As a result, for single process communicators,
3001bebe75SBarry Smith    MatSeqAIJSetPreallocation() is supported, and similarly MatMPIAIJSetPreallocation() is supported
3101bebe75SBarry Smith   for communicators controlling multiple processes.  It is recommended that you call both of
3201bebe75SBarry Smith   the above preallocation routines for simplicity.
3301bebe75SBarry Smith 
3401bebe75SBarry Smith    Options Database Keys:
3501bebe75SBarry Smith . -mat_type aijcrl - sets the matrix type to "aijcrl" during a call to MatSetFromOptions()
3601bebe75SBarry Smith 
3701bebe75SBarry Smith   Level: beginner
3801bebe75SBarry Smith 
3901bebe75SBarry Smith .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL
4001bebe75SBarry Smith M*/
4101bebe75SBarry Smith 
42dd6ea824SBarry Smith #undef __FUNCT__
43f2c98031SJed Brown #define __FUNCT__ "MatFindNonzeroRows_MPIAIJ"
44f2c98031SJed Brown PetscErrorCode MatFindNonzeroRows_MPIAIJ(Mat M,IS *keptrows)
4527d4218bSShri Abhyankar {
4627d4218bSShri Abhyankar   PetscErrorCode  ierr;
4727d4218bSShri Abhyankar   Mat_MPIAIJ      *mat = (Mat_MPIAIJ*)M->data;
4827d4218bSShri Abhyankar   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)mat->A->data;
4927d4218bSShri Abhyankar   Mat_SeqAIJ      *b = (Mat_SeqAIJ*)mat->B->data;
5027d4218bSShri Abhyankar   const PetscInt  *ia,*ib;
5127d4218bSShri Abhyankar   const MatScalar *aa,*bb;
5227d4218bSShri Abhyankar   PetscInt        na,nb,i,j,*rows,cnt=0,n0rows;
5327d4218bSShri Abhyankar   PetscInt        m = M->rmap->n,rstart = M->rmap->rstart;
5427d4218bSShri Abhyankar 
5527d4218bSShri Abhyankar   PetscFunctionBegin;
5627d4218bSShri Abhyankar   *keptrows = 0;
5727d4218bSShri Abhyankar   ia = a->i;
5827d4218bSShri Abhyankar   ib = b->i;
5927d4218bSShri Abhyankar   for (i=0; i<m; i++) {
6027d4218bSShri Abhyankar     na = ia[i+1] - ia[i];
6127d4218bSShri Abhyankar     nb = ib[i+1] - ib[i];
6227d4218bSShri Abhyankar     if (!na && !nb) {
6327d4218bSShri Abhyankar       cnt++;
6427d4218bSShri Abhyankar       goto ok1;
6527d4218bSShri Abhyankar     }
6627d4218bSShri Abhyankar     aa = a->a + ia[i];
6727d4218bSShri Abhyankar     for (j=0; j<na; j++) {
6827d4218bSShri Abhyankar       if (aa[j] != 0.0) goto ok1;
6927d4218bSShri Abhyankar     }
7027d4218bSShri Abhyankar     bb = b->a + ib[i];
7127d4218bSShri Abhyankar     for (j=0; j <nb; j++) {
7227d4218bSShri Abhyankar       if (bb[j] != 0.0) goto ok1;
7327d4218bSShri Abhyankar     }
7427d4218bSShri Abhyankar     cnt++;
7527d4218bSShri Abhyankar     ok1:;
7627d4218bSShri Abhyankar   }
7727d4218bSShri Abhyankar   ierr = MPI_Allreduce(&cnt,&n0rows,1,MPIU_INT,MPI_SUM,((PetscObject)M)->comm);CHKERRQ(ierr);
7827d4218bSShri Abhyankar   if (!n0rows) PetscFunctionReturn(0);
7927d4218bSShri Abhyankar   ierr = PetscMalloc((M->rmap->n-cnt)*sizeof(PetscInt),&rows);CHKERRQ(ierr);
8027d4218bSShri Abhyankar   cnt = 0;
8127d4218bSShri Abhyankar   for (i=0; i<m; i++) {
8227d4218bSShri Abhyankar     na = ia[i+1] - ia[i];
8327d4218bSShri Abhyankar     nb = ib[i+1] - ib[i];
8427d4218bSShri Abhyankar     if (!na && !nb) continue;
8527d4218bSShri Abhyankar     aa = a->a + ia[i];
8627d4218bSShri Abhyankar     for(j=0; j<na;j++) {
8727d4218bSShri Abhyankar       if (aa[j] != 0.0) {
8827d4218bSShri Abhyankar         rows[cnt++] = rstart + i;
8927d4218bSShri Abhyankar         goto ok2;
9027d4218bSShri Abhyankar       }
9127d4218bSShri Abhyankar     }
9227d4218bSShri Abhyankar     bb = b->a + ib[i];
9327d4218bSShri Abhyankar     for (j=0; j<nb; j++) {
9427d4218bSShri Abhyankar       if (bb[j] != 0.0) {
9527d4218bSShri Abhyankar         rows[cnt++] = rstart + i;
9627d4218bSShri Abhyankar         goto ok2;
9727d4218bSShri Abhyankar       }
9827d4218bSShri Abhyankar     }
9927d4218bSShri Abhyankar     ok2:;
10027d4218bSShri Abhyankar   }
10177c8aea5SJed Brown   ierr = ISCreateGeneral(((PetscObject)M)->comm,cnt,rows,PETSC_OWN_POINTER,keptrows);CHKERRQ(ierr);
10227d4218bSShri Abhyankar   PetscFunctionReturn(0);
10327d4218bSShri Abhyankar }
10427d4218bSShri Abhyankar 
10527d4218bSShri Abhyankar #undef __FUNCT__
106f1f41ecbSJed Brown #define __FUNCT__ "MatFindZeroDiagonals_MPIAIJ"
107f1f41ecbSJed Brown PetscErrorCode MatFindZeroDiagonals_MPIAIJ(Mat M,IS *zrows)
108f1f41ecbSJed Brown {
109f1f41ecbSJed Brown   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)M->data;
110f1f41ecbSJed Brown   PetscErrorCode ierr;
111f1f41ecbSJed Brown   PetscInt       i,rstart,nrows,*rows;
112f1f41ecbSJed Brown 
113f1f41ecbSJed Brown   PetscFunctionBegin;
114f1f41ecbSJed Brown   *zrows = PETSC_NULL;
115f1f41ecbSJed Brown   ierr = MatFindZeroDiagonals_SeqAIJ_Private(aij->A,&nrows,&rows);CHKERRQ(ierr);
116f1f41ecbSJed Brown   ierr = MatGetOwnershipRange(M,&rstart,PETSC_NULL);CHKERRQ(ierr);
117f1f41ecbSJed Brown   for (i=0; i<nrows; i++) rows[i] += rstart;
118f1f41ecbSJed Brown   ierr = ISCreateGeneral(((PetscObject)M)->comm,nrows,rows,PETSC_OWN_POINTER,zrows);CHKERRQ(ierr);
119f1f41ecbSJed Brown   PetscFunctionReturn(0);
120f1f41ecbSJed Brown }
121f1f41ecbSJed Brown 
122f1f41ecbSJed Brown #undef __FUNCT__
1230716a85fSBarry Smith #define __FUNCT__ "MatGetColumnNorms_MPIAIJ"
1240716a85fSBarry Smith PetscErrorCode MatGetColumnNorms_MPIAIJ(Mat A,NormType type,PetscReal *norms)
1250716a85fSBarry Smith {
1260716a85fSBarry Smith   PetscErrorCode ierr;
1270716a85fSBarry Smith   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)A->data;
1280716a85fSBarry Smith   PetscInt       i,n,*garray = aij->garray;
1290716a85fSBarry Smith   Mat_SeqAIJ     *a_aij = (Mat_SeqAIJ*) aij->A->data;
1300716a85fSBarry Smith   Mat_SeqAIJ     *b_aij = (Mat_SeqAIJ*) aij->B->data;
1310716a85fSBarry Smith   PetscReal      *work;
1320716a85fSBarry Smith 
1330716a85fSBarry Smith   PetscFunctionBegin;
1340716a85fSBarry Smith   ierr = MatGetSize(A,PETSC_NULL,&n);CHKERRQ(ierr);
1350716a85fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscReal),&work);CHKERRQ(ierr);
1360716a85fSBarry Smith   ierr = PetscMemzero(work,n*sizeof(PetscReal));CHKERRQ(ierr);
1370716a85fSBarry Smith   if (type == NORM_2) {
1380716a85fSBarry Smith     for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) {
1390716a85fSBarry Smith       work[A->cmap->rstart + a_aij->j[i]] += PetscAbsScalar(a_aij->a[i]*a_aij->a[i]);
1400716a85fSBarry Smith     }
1410716a85fSBarry Smith     for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) {
1420716a85fSBarry Smith       work[garray[b_aij->j[i]]] += PetscAbsScalar(b_aij->a[i]*b_aij->a[i]);
1430716a85fSBarry Smith     }
1440716a85fSBarry Smith   } else if (type == NORM_1) {
1450716a85fSBarry Smith     for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) {
1460716a85fSBarry Smith       work[A->cmap->rstart + a_aij->j[i]] += PetscAbsScalar(a_aij->a[i]);
1470716a85fSBarry Smith     }
1480716a85fSBarry Smith     for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) {
1490716a85fSBarry Smith       work[garray[b_aij->j[i]]] += PetscAbsScalar(b_aij->a[i]);
1500716a85fSBarry Smith     }
1510716a85fSBarry Smith   } else if (type == NORM_INFINITY) {
1520716a85fSBarry Smith     for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) {
1530716a85fSBarry Smith       work[A->cmap->rstart + a_aij->j[i]] = PetscMax(PetscAbsScalar(a_aij->a[i]), work[A->cmap->rstart + a_aij->j[i]]);
1540716a85fSBarry Smith     }
1550716a85fSBarry Smith     for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) {
1560716a85fSBarry Smith       work[garray[b_aij->j[i]]] = PetscMax(PetscAbsScalar(b_aij->a[i]),work[garray[b_aij->j[i]]]);
1570716a85fSBarry Smith     }
1580716a85fSBarry Smith 
1590716a85fSBarry Smith   } else SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONG,"Unknown NormType");
1600716a85fSBarry Smith   if (type == NORM_INFINITY) {
1610716a85fSBarry Smith     ierr = MPI_Allreduce(work,norms,n,MPIU_REAL,MPIU_MAX,A->hdr.comm);CHKERRQ(ierr);
1620716a85fSBarry Smith   } else {
1630716a85fSBarry Smith     ierr = MPI_Allreduce(work,norms,n,MPIU_REAL,MPIU_SUM,A->hdr.comm);CHKERRQ(ierr);
1640716a85fSBarry Smith   }
1650716a85fSBarry Smith   ierr = PetscFree(work);CHKERRQ(ierr);
1660716a85fSBarry Smith   if (type == NORM_2) {
1678f1a2a5eSBarry Smith     for (i=0; i<n; i++) norms[i] = PetscSqrtReal(norms[i]);
1680716a85fSBarry Smith   }
1690716a85fSBarry Smith   PetscFunctionReturn(0);
1700716a85fSBarry Smith }
1710716a85fSBarry Smith 
1720716a85fSBarry Smith #undef __FUNCT__
173dd6ea824SBarry Smith #define __FUNCT__ "MatDistribute_MPIAIJ"
174dd6ea824SBarry Smith /*
175dd6ea824SBarry Smith     Distributes a SeqAIJ matrix across a set of processes. Code stolen from
176dd6ea824SBarry Smith     MatLoad_MPIAIJ(). Horrible lack of reuse. Should be a routine for each matrix type.
177dd6ea824SBarry Smith 
178dd6ea824SBarry Smith     Only for square matrices
179dd6ea824SBarry Smith */
180dd6ea824SBarry Smith PetscErrorCode MatDistribute_MPIAIJ(MPI_Comm comm,Mat gmat,PetscInt m,MatReuse reuse,Mat *inmat)
181dd6ea824SBarry Smith {
182dd6ea824SBarry Smith   PetscMPIInt    rank,size;
183dd6ea824SBarry Smith   PetscInt       *rowners,*dlens,*olens,i,rstart,rend,j,jj,nz,*gmataj,cnt,row,*ld;
184dd6ea824SBarry Smith   PetscErrorCode ierr;
185dd6ea824SBarry Smith   Mat            mat;
186dd6ea824SBarry Smith   Mat_SeqAIJ     *gmata;
187dd6ea824SBarry Smith   PetscMPIInt    tag;
188dd6ea824SBarry Smith   MPI_Status     status;
189ace3abfcSBarry Smith   PetscBool      aij;
190dd6ea824SBarry Smith   MatScalar      *gmataa,*ao,*ad,*gmataarestore=0;
191dd6ea824SBarry Smith 
192dd6ea824SBarry Smith   PetscFunctionBegin;
193dd6ea824SBarry Smith   CHKMEMQ;
194dd6ea824SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
195dd6ea824SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
196dd6ea824SBarry Smith   if (!rank) {
197251f4c67SDmitry Karpeev     ierr = PetscObjectTypeCompare((PetscObject)gmat,MATSEQAIJ,&aij);CHKERRQ(ierr);
19865e19b50SBarry Smith     if (!aij) SETERRQ1(((PetscObject)gmat)->comm,PETSC_ERR_SUP,"Currently no support for input matrix of type %s\n",((PetscObject)gmat)->type_name);
199dd6ea824SBarry Smith   }
200dd6ea824SBarry Smith   if (reuse == MAT_INITIAL_MATRIX) {
201dd6ea824SBarry Smith     ierr = MatCreate(comm,&mat);CHKERRQ(ierr);
202dd6ea824SBarry Smith     ierr = MatSetSizes(mat,m,m,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
203a2f3521dSMark F. Adams     ierr = MatSetBlockSizes(mat,gmat->rmap->bs,gmat->cmap->bs);CHKERRQ(ierr);
204dd6ea824SBarry Smith     ierr = MatSetType(mat,MATAIJ);CHKERRQ(ierr);
205dd6ea824SBarry Smith     ierr = PetscMalloc((size+1)*sizeof(PetscInt),&rowners);CHKERRQ(ierr);
206dd6ea824SBarry Smith     ierr = PetscMalloc2(m,PetscInt,&dlens,m,PetscInt,&olens);CHKERRQ(ierr);
207dd6ea824SBarry Smith     ierr = MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);CHKERRQ(ierr);
208dd6ea824SBarry Smith     rowners[0] = 0;
209dd6ea824SBarry Smith     for (i=2; i<=size; i++) {
210dd6ea824SBarry Smith       rowners[i] += rowners[i-1];
211dd6ea824SBarry Smith     }
212dd6ea824SBarry Smith     rstart = rowners[rank];
213dd6ea824SBarry Smith     rend   = rowners[rank+1];
214dd6ea824SBarry Smith     ierr   = PetscObjectGetNewTag((PetscObject)mat,&tag);CHKERRQ(ierr);
215dd6ea824SBarry Smith     if (!rank) {
216dd6ea824SBarry Smith       gmata = (Mat_SeqAIJ*) gmat->data;
217dd6ea824SBarry Smith       /* send row lengths to all processors */
218dd6ea824SBarry Smith       for (i=0; i<m; i++) dlens[i] = gmata->ilen[i];
219dd6ea824SBarry Smith       for (i=1; i<size; i++) {
220dd6ea824SBarry Smith 	ierr = MPI_Send(gmata->ilen + rowners[i],rowners[i+1]-rowners[i],MPIU_INT,i,tag,comm);CHKERRQ(ierr);
221dd6ea824SBarry Smith       }
222dd6ea824SBarry Smith       /* determine number diagonal and off-diagonal counts */
223dd6ea824SBarry Smith       ierr = PetscMemzero(olens,m*sizeof(PetscInt));CHKERRQ(ierr);
224dd6ea824SBarry Smith       ierr = PetscMalloc(m*sizeof(PetscInt),&ld);CHKERRQ(ierr);
225dd6ea824SBarry Smith       ierr = PetscMemzero(ld,m*sizeof(PetscInt));CHKERRQ(ierr);
226dd6ea824SBarry Smith       jj = 0;
227dd6ea824SBarry Smith       for (i=0; i<m; i++) {
228dd6ea824SBarry Smith 	for (j=0; j<dlens[i]; j++) {
229dd6ea824SBarry Smith           if (gmata->j[jj] < rstart) ld[i]++;
230dd6ea824SBarry Smith 	  if (gmata->j[jj] < rstart || gmata->j[jj] >= rend) olens[i]++;
231dd6ea824SBarry Smith 	  jj++;
232dd6ea824SBarry Smith 	}
233dd6ea824SBarry Smith       }
234dd6ea824SBarry Smith       /* send column indices to other processes */
235dd6ea824SBarry Smith       for (i=1; i<size; i++) {
236dd6ea824SBarry Smith 	nz   = gmata->i[rowners[i+1]]-gmata->i[rowners[i]];
237dd6ea824SBarry Smith 	ierr = MPI_Send(&nz,1,MPIU_INT,i,tag,comm);CHKERRQ(ierr);
238dd6ea824SBarry Smith 	ierr = MPI_Send(gmata->j + gmata->i[rowners[i]],nz,MPIU_INT,i,tag,comm);CHKERRQ(ierr);
239dd6ea824SBarry Smith       }
240dd6ea824SBarry Smith 
241dd6ea824SBarry Smith       /* send numerical values to other processes */
242dd6ea824SBarry Smith       for (i=1; i<size; i++) {
243dd6ea824SBarry Smith         nz   = gmata->i[rowners[i+1]]-gmata->i[rowners[i]];
244dd6ea824SBarry Smith         ierr = MPI_Send(gmata->a + gmata->i[rowners[i]],nz,MPIU_SCALAR,i,tag,comm);CHKERRQ(ierr);
245dd6ea824SBarry Smith       }
246dd6ea824SBarry Smith       gmataa = gmata->a;
247dd6ea824SBarry Smith       gmataj = gmata->j;
248dd6ea824SBarry Smith 
249dd6ea824SBarry Smith     } else {
250dd6ea824SBarry Smith       /* receive row lengths */
251dd6ea824SBarry Smith       ierr = MPI_Recv(dlens,m,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr);
252dd6ea824SBarry Smith       /* receive column indices */
253dd6ea824SBarry Smith       ierr = MPI_Recv(&nz,1,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr);
254dd6ea824SBarry Smith       ierr = PetscMalloc2(nz,PetscScalar,&gmataa,nz,PetscInt,&gmataj);CHKERRQ(ierr);
255dd6ea824SBarry Smith       ierr = MPI_Recv(gmataj,nz,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr);
256dd6ea824SBarry Smith       /* determine number diagonal and off-diagonal counts */
257dd6ea824SBarry Smith       ierr = PetscMemzero(olens,m*sizeof(PetscInt));CHKERRQ(ierr);
258dd6ea824SBarry Smith       ierr = PetscMalloc(m*sizeof(PetscInt),&ld);CHKERRQ(ierr);
259dd6ea824SBarry Smith       ierr = PetscMemzero(ld,m*sizeof(PetscInt));CHKERRQ(ierr);
260dd6ea824SBarry Smith       jj = 0;
261dd6ea824SBarry Smith       for (i=0; i<m; i++) {
262dd6ea824SBarry Smith 	for (j=0; j<dlens[i]; j++) {
263dd6ea824SBarry Smith           if (gmataj[jj] < rstart) ld[i]++;
264dd6ea824SBarry Smith 	  if (gmataj[jj] < rstart || gmataj[jj] >= rend) olens[i]++;
265dd6ea824SBarry Smith 	  jj++;
266dd6ea824SBarry Smith 	}
267dd6ea824SBarry Smith       }
268dd6ea824SBarry Smith       /* receive numerical values */
269dd6ea824SBarry Smith       ierr = PetscMemzero(gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr);
270dd6ea824SBarry Smith       ierr = MPI_Recv(gmataa,nz,MPIU_SCALAR,0,tag,comm,&status);CHKERRQ(ierr);
271dd6ea824SBarry Smith     }
272dd6ea824SBarry Smith     /* set preallocation */
273dd6ea824SBarry Smith     for (i=0; i<m; i++) {
274dd6ea824SBarry Smith       dlens[i] -= olens[i];
275dd6ea824SBarry Smith     }
276dd6ea824SBarry Smith     ierr = MatSeqAIJSetPreallocation(mat,0,dlens);CHKERRQ(ierr);
277dd6ea824SBarry Smith     ierr = MatMPIAIJSetPreallocation(mat,0,dlens,0,olens);CHKERRQ(ierr);
278dd6ea824SBarry Smith 
279dd6ea824SBarry Smith     for (i=0; i<m; i++) {
280dd6ea824SBarry Smith       dlens[i] += olens[i];
281dd6ea824SBarry Smith     }
282dd6ea824SBarry Smith     cnt  = 0;
283dd6ea824SBarry Smith     for (i=0; i<m; i++) {
284dd6ea824SBarry Smith       row  = rstart + i;
285dd6ea824SBarry Smith       ierr = MatSetValues(mat,1,&row,dlens[i],gmataj+cnt,gmataa+cnt,INSERT_VALUES);CHKERRQ(ierr);
286dd6ea824SBarry Smith       cnt += dlens[i];
287dd6ea824SBarry Smith     }
288dd6ea824SBarry Smith     if (rank) {
289dd6ea824SBarry Smith       ierr = PetscFree2(gmataa,gmataj);CHKERRQ(ierr);
290dd6ea824SBarry Smith     }
291dd6ea824SBarry Smith     ierr = PetscFree2(dlens,olens);CHKERRQ(ierr);
292dd6ea824SBarry Smith     ierr = PetscFree(rowners);CHKERRQ(ierr);
293dd6ea824SBarry Smith     ((Mat_MPIAIJ*)(mat->data))->ld = ld;
294dd6ea824SBarry Smith     *inmat = mat;
295dd6ea824SBarry Smith   } else {   /* column indices are already set; only need to move over numerical values from process 0 */
296dd6ea824SBarry Smith     Mat_SeqAIJ *Ad = (Mat_SeqAIJ*)((Mat_MPIAIJ*)((*inmat)->data))->A->data;
297dd6ea824SBarry Smith     Mat_SeqAIJ *Ao = (Mat_SeqAIJ*)((Mat_MPIAIJ*)((*inmat)->data))->B->data;
298dd6ea824SBarry Smith     mat   = *inmat;
299dd6ea824SBarry Smith     ierr  = PetscObjectGetNewTag((PetscObject)mat,&tag);CHKERRQ(ierr);
300dd6ea824SBarry Smith     if (!rank) {
301dd6ea824SBarry Smith       /* send numerical values to other processes */
302dd6ea824SBarry Smith       gmata = (Mat_SeqAIJ*) gmat->data;
303dd6ea824SBarry Smith       ierr   = MatGetOwnershipRanges(mat,(const PetscInt**)&rowners);CHKERRQ(ierr);
304dd6ea824SBarry Smith       gmataa = gmata->a;
305dd6ea824SBarry Smith       for (i=1; i<size; i++) {
306dd6ea824SBarry Smith         nz   = gmata->i[rowners[i+1]]-gmata->i[rowners[i]];
307dd6ea824SBarry Smith         ierr = MPI_Send(gmataa + gmata->i[rowners[i]],nz,MPIU_SCALAR,i,tag,comm);CHKERRQ(ierr);
308dd6ea824SBarry Smith       }
309dd6ea824SBarry Smith       nz   = gmata->i[rowners[1]]-gmata->i[rowners[0]];
310dd6ea824SBarry Smith     } else {
311dd6ea824SBarry Smith       /* receive numerical values from process 0*/
312dd6ea824SBarry Smith       nz   = Ad->nz + Ao->nz;
313dd6ea824SBarry Smith       ierr = PetscMalloc(nz*sizeof(PetscScalar),&gmataa);CHKERRQ(ierr); gmataarestore = gmataa;
314dd6ea824SBarry Smith       ierr = MPI_Recv(gmataa,nz,MPIU_SCALAR,0,tag,comm,&status);CHKERRQ(ierr);
315dd6ea824SBarry Smith     }
316dd6ea824SBarry Smith     /* transfer numerical values into the diagonal A and off diagonal B parts of mat */
317dd6ea824SBarry Smith     ld = ((Mat_MPIAIJ*)(mat->data))->ld;
318dd6ea824SBarry Smith     ad = Ad->a;
319dd6ea824SBarry Smith     ao = Ao->a;
320d0f46423SBarry Smith     if (mat->rmap->n) {
321dd6ea824SBarry Smith       i  = 0;
322dd6ea824SBarry Smith       nz = ld[i];                                   ierr = PetscMemcpy(ao,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); ao += nz; gmataa += nz;
323dd6ea824SBarry Smith       nz = Ad->i[i+1] - Ad->i[i];                   ierr = PetscMemcpy(ad,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); ad += nz; gmataa += nz;
324dd6ea824SBarry Smith     }
325d0f46423SBarry Smith     for (i=1; i<mat->rmap->n; i++) {
326dd6ea824SBarry Smith       nz = Ao->i[i] - Ao->i[i-1] - ld[i-1] + ld[i]; ierr = PetscMemcpy(ao,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); ao += nz; gmataa += nz;
327dd6ea824SBarry Smith       nz = Ad->i[i+1] - Ad->i[i];                   ierr = PetscMemcpy(ad,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); ad += nz; gmataa += nz;
328dd6ea824SBarry Smith     }
329dd6ea824SBarry Smith     i--;
330d0f46423SBarry Smith     if (mat->rmap->n) {
331dd6ea824SBarry Smith       nz = Ao->i[i+1] - Ao->i[i] - ld[i];           ierr = PetscMemcpy(ao,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); ao += nz; gmataa += nz;
332dd6ea824SBarry Smith     }
333dd6ea824SBarry Smith     if (rank) {
334dd6ea824SBarry Smith       ierr = PetscFree(gmataarestore);CHKERRQ(ierr);
335dd6ea824SBarry Smith     }
336dd6ea824SBarry Smith   }
337dd6ea824SBarry Smith   ierr = MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
338dd6ea824SBarry Smith   ierr = MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
339dd6ea824SBarry Smith   CHKMEMQ;
340dd6ea824SBarry Smith   PetscFunctionReturn(0);
341dd6ea824SBarry Smith }
342dd6ea824SBarry Smith 
3430f5bd95cSBarry Smith /*
3440f5bd95cSBarry Smith   Local utility routine that creates a mapping from the global column
3459e25ed09SBarry Smith number to the local number in the off-diagonal part of the local
3460f5bd95cSBarry Smith storage of the matrix.  When PETSC_USE_CTABLE is used this is scalable at
3470f5bd95cSBarry Smith a slightly higher hash table cost; without it it is not scalable (each processor
3480f5bd95cSBarry Smith has an order N integer array but is fast to acess.
3499e25ed09SBarry Smith */
3504a2ae208SSatish Balay #undef __FUNCT__
351ab9863d7SBarry Smith #define __FUNCT__ "MatCreateColmap_MPIAIJ_Private"
352ab9863d7SBarry Smith PetscErrorCode MatCreateColmap_MPIAIJ_Private(Mat mat)
3539e25ed09SBarry Smith {
35444a69424SLois Curfman McInnes   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
3556849ba73SBarry Smith   PetscErrorCode ierr;
356d0f46423SBarry Smith   PetscInt       n = aij->B->cmap->n,i;
357dbb450caSBarry Smith 
3583a40ed3dSBarry Smith   PetscFunctionBegin;
3595e1f6667SBarry Smith   if (!aij->garray) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"MPIAIJ Matrix was assembled but is missing garray");
360aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
361e23dfa41SBarry Smith   ierr = PetscTableCreate(n,mat->cmap->N+1,&aij->colmap);CHKERRQ(ierr);
362b1fc9764SSatish Balay   for (i=0; i<n; i++){
3633861aac3SJed Brown     ierr = PetscTableAdd(aij->colmap,aij->garray[i]+1,i+1,INSERT_VALUES);CHKERRQ(ierr);
364b1fc9764SSatish Balay   }
365b1fc9764SSatish Balay #else
366d0f46423SBarry Smith   ierr = PetscMalloc((mat->cmap->N+1)*sizeof(PetscInt),&aij->colmap);CHKERRQ(ierr);
367d0f46423SBarry Smith   ierr = PetscLogObjectMemory(mat,mat->cmap->N*sizeof(PetscInt));CHKERRQ(ierr);
368d0f46423SBarry Smith   ierr = PetscMemzero(aij->colmap,mat->cmap->N*sizeof(PetscInt));CHKERRQ(ierr);
369905e6a2fSBarry Smith   for (i=0; i<n; i++) aij->colmap[aij->garray[i]] = i+1;
370b1fc9764SSatish Balay #endif
3713a40ed3dSBarry Smith   PetscFunctionReturn(0);
3729e25ed09SBarry Smith }
3739e25ed09SBarry Smith 
37430770e4dSSatish Balay #define MatSetValues_SeqAIJ_A_Private(row,col,value,addv) \
3750520107fSSatish Balay { \
3767cd84e04SBarry Smith     if (col <= lastcol1) low1 = 0; else high1 = nrow1; \
377fd3458f5SBarry Smith     lastcol1 = col;\
378fd3458f5SBarry Smith     while (high1-low1 > 5) { \
379fd3458f5SBarry Smith       t = (low1+high1)/2; \
380fd3458f5SBarry Smith       if (rp1[t] > col) high1 = t; \
381fd3458f5SBarry Smith       else             low1  = t; \
382ba4e3ef2SSatish Balay     } \
383fd3458f5SBarry Smith       for (_i=low1; _i<high1; _i++) { \
384fd3458f5SBarry Smith         if (rp1[_i] > col) break; \
385fd3458f5SBarry Smith         if (rp1[_i] == col) { \
386fd3458f5SBarry Smith           if (addv == ADD_VALUES) ap1[_i] += value;   \
387fd3458f5SBarry Smith           else                    ap1[_i] = value; \
38830770e4dSSatish Balay           goto a_noinsert; \
3890520107fSSatish Balay         } \
3900520107fSSatish Balay       }  \
391e44c0bd4SBarry Smith       if (value == 0.0 && ignorezeroentries) {low1 = 0; high1 = nrow1;goto a_noinsert;} \
392e44c0bd4SBarry Smith       if (nonew == 1) {low1 = 0; high1 = nrow1; goto a_noinsert;}		\
393e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \
394fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,am,1,nrow1,row,col,rmax1,aa,ai,aj,rp1,ap1,aimax,nonew,MatScalar); \
395669a8dbcSSatish Balay       N = nrow1++ - 1; a->nz++; high1++; \
3960520107fSSatish Balay       /* shift up all the later entries in this row */ \
3970520107fSSatish Balay       for (ii=N; ii>=_i; ii--) { \
398fd3458f5SBarry Smith         rp1[ii+1] = rp1[ii]; \
399fd3458f5SBarry Smith         ap1[ii+1] = ap1[ii]; \
4000520107fSSatish Balay       } \
401fd3458f5SBarry Smith       rp1[_i] = col;  \
402fd3458f5SBarry Smith       ap1[_i] = value;  \
40330770e4dSSatish Balay       a_noinsert: ; \
404fd3458f5SBarry Smith       ailen[row] = nrow1; \
4050520107fSSatish Balay }
4060a198c4cSBarry Smith 
407085a36d4SBarry Smith 
40830770e4dSSatish Balay #define MatSetValues_SeqAIJ_B_Private(row,col,value,addv) \
40930770e4dSSatish Balay { \
4107cd84e04SBarry Smith     if (col <= lastcol2) low2 = 0; else high2 = nrow2; \
411fd3458f5SBarry Smith     lastcol2 = col;\
412fd3458f5SBarry Smith     while (high2-low2 > 5) { \
413fd3458f5SBarry Smith       t = (low2+high2)/2; \
414fd3458f5SBarry Smith       if (rp2[t] > col) high2 = t; \
415fd3458f5SBarry Smith       else             low2  = t; \
416ba4e3ef2SSatish Balay     } \
417fd3458f5SBarry Smith     for (_i=low2; _i<high2; _i++) {		\
418fd3458f5SBarry Smith       if (rp2[_i] > col) break;			\
419fd3458f5SBarry Smith       if (rp2[_i] == col) {			      \
420fd3458f5SBarry Smith 	if (addv == ADD_VALUES) ap2[_i] += value;     \
421fd3458f5SBarry Smith 	else                    ap2[_i] = value;      \
42230770e4dSSatish Balay 	goto b_noinsert;			      \
42330770e4dSSatish Balay       }						      \
42430770e4dSSatish Balay     }							      \
425e44c0bd4SBarry Smith     if (value == 0.0 && ignorezeroentries) {low2 = 0; high2 = nrow2; goto b_noinsert;} \
426e44c0bd4SBarry Smith     if (nonew == 1) {low2 = 0; high2 = nrow2; goto b_noinsert;}		\
427e32f2f54SBarry Smith     if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \
428fef13f97SBarry Smith     MatSeqXAIJReallocateAIJ(B,bm,1,nrow2,row,col,rmax2,ba,bi,bj,rp2,ap2,bimax,nonew,MatScalar); \
429669a8dbcSSatish Balay     N = nrow2++ - 1; b->nz++; high2++;					\
43030770e4dSSatish Balay     /* shift up all the later entries in this row */			\
43130770e4dSSatish Balay     for (ii=N; ii>=_i; ii--) {						\
432fd3458f5SBarry Smith       rp2[ii+1] = rp2[ii];						\
433fd3458f5SBarry Smith       ap2[ii+1] = ap2[ii];						\
43430770e4dSSatish Balay     }									\
435fd3458f5SBarry Smith     rp2[_i] = col;							\
436fd3458f5SBarry Smith     ap2[_i] = value;							\
43730770e4dSSatish Balay     b_noinsert: ;								\
438fd3458f5SBarry Smith     bilen[row] = nrow2;							\
43930770e4dSSatish Balay }
44030770e4dSSatish Balay 
4414a2ae208SSatish Balay #undef __FUNCT__
4422fd7e33dSBarry Smith #define __FUNCT__ "MatSetValuesRow_MPIAIJ"
4432fd7e33dSBarry Smith PetscErrorCode MatSetValuesRow_MPIAIJ(Mat A,PetscInt row,const PetscScalar v[])
4442fd7e33dSBarry Smith {
4452fd7e33dSBarry Smith   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)A->data;
4462fd7e33dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)mat->A->data,*b = (Mat_SeqAIJ*)mat->B->data;
4472fd7e33dSBarry Smith   PetscErrorCode ierr;
4482fd7e33dSBarry Smith   PetscInt       l,*garray = mat->garray,diag;
4492fd7e33dSBarry Smith 
4502fd7e33dSBarry Smith   PetscFunctionBegin;
4512fd7e33dSBarry Smith   /* code only works for square matrices A */
4522fd7e33dSBarry Smith 
4532fd7e33dSBarry Smith   /* find size of row to the left of the diagonal part */
4542fd7e33dSBarry Smith   ierr = MatGetOwnershipRange(A,&diag,0);CHKERRQ(ierr);
4552fd7e33dSBarry Smith   row  = row - diag;
4562fd7e33dSBarry Smith   for (l=0; l<b->i[row+1]-b->i[row]; l++) {
4572fd7e33dSBarry Smith     if (garray[b->j[b->i[row]+l]] > diag) break;
4582fd7e33dSBarry Smith   }
4592fd7e33dSBarry Smith   ierr = PetscMemcpy(b->a+b->i[row],v,l*sizeof(PetscScalar));CHKERRQ(ierr);
4602fd7e33dSBarry Smith 
4612fd7e33dSBarry Smith   /* diagonal part */
4622fd7e33dSBarry Smith   ierr = PetscMemcpy(a->a+a->i[row],v+l,(a->i[row+1]-a->i[row])*sizeof(PetscScalar));CHKERRQ(ierr);
4632fd7e33dSBarry Smith 
4642fd7e33dSBarry Smith   /* right of diagonal part */
4652fd7e33dSBarry Smith   ierr = PetscMemcpy(b->a+b->i[row]+l,v+l+a->i[row+1]-a->i[row],(b->i[row+1]-b->i[row]-l)*sizeof(PetscScalar));CHKERRQ(ierr);
4662fd7e33dSBarry Smith   PetscFunctionReturn(0);
4672fd7e33dSBarry Smith }
4682fd7e33dSBarry Smith 
4692fd7e33dSBarry Smith #undef __FUNCT__
4704a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_MPIAIJ"
471b1d57f15SBarry Smith PetscErrorCode MatSetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
4728a729477SBarry Smith {
47344a69424SLois Curfman McInnes   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
47487828ca2SBarry Smith   PetscScalar    value;
475dfbe8321SBarry Smith   PetscErrorCode ierr;
476d0f46423SBarry Smith   PetscInt       i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend;
477d0f46423SBarry Smith   PetscInt       cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col;
478ace3abfcSBarry Smith   PetscBool      roworiented = aij->roworiented;
4798a729477SBarry Smith 
4800520107fSSatish Balay   /* Some Variables required in the macro */
4814ee7247eSSatish Balay   Mat            A = aij->A;
4824ee7247eSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
48357809a77SBarry Smith   PetscInt       *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j;
484a77337e4SBarry Smith   MatScalar      *aa = a->a;
485ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
48630770e4dSSatish Balay   Mat            B = aij->B;
48730770e4dSSatish Balay   Mat_SeqAIJ     *b = (Mat_SeqAIJ*)B->data;
488d0f46423SBarry Smith   PetscInt       *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap->n,am = aij->A->rmap->n;
489a77337e4SBarry Smith   MatScalar      *ba = b->a;
49030770e4dSSatish Balay 
491fd3458f5SBarry Smith   PetscInt       *rp1,*rp2,ii,nrow1,nrow2,_i,rmax1,rmax2,N,low1,high1,low2,high2,t,lastcol1,lastcol2;
4928d76821aSHong Zhang   PetscInt       nonew;
493a77337e4SBarry Smith   MatScalar      *ap1,*ap2;
4944ee7247eSSatish Balay 
4953a40ed3dSBarry Smith   PetscFunctionBegin;
49671fd2e92SBarry Smith   if (v) PetscValidScalarPointer(v,6);
4978a729477SBarry Smith   for (i=0; i<m; i++) {
4985ef9f2a5SBarry Smith     if (im[i] < 0) continue;
4992515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
500e32f2f54SBarry Smith     if (im[i] >= mat->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",im[i],mat->rmap->N-1);
5010a198c4cSBarry Smith #endif
5024b0e389bSBarry Smith     if (im[i] >= rstart && im[i] < rend) {
5034b0e389bSBarry Smith       row      = im[i] - rstart;
504fd3458f5SBarry Smith       lastcol1 = -1;
505fd3458f5SBarry Smith       rp1      = aj + ai[row];
506fd3458f5SBarry Smith       ap1      = aa + ai[row];
507fd3458f5SBarry Smith       rmax1    = aimax[row];
508fd3458f5SBarry Smith       nrow1    = ailen[row];
509fd3458f5SBarry Smith       low1     = 0;
510fd3458f5SBarry Smith       high1    = nrow1;
511fd3458f5SBarry Smith       lastcol2 = -1;
512fd3458f5SBarry Smith       rp2      = bj + bi[row];
513d498b1e9SBarry Smith       ap2      = ba + bi[row];
514fd3458f5SBarry Smith       rmax2    = bimax[row];
515d498b1e9SBarry Smith       nrow2    = bilen[row];
516fd3458f5SBarry Smith       low2     = 0;
517fd3458f5SBarry Smith       high2    = nrow2;
518fd3458f5SBarry Smith 
5191eb62cbbSBarry Smith       for (j=0; j<n; j++) {
52016371a99SBarry Smith         if (v) {if (roworiented) value = v[i*n+j]; else value = v[i+j*m];} else value = 0.0;
521abc0a331SBarry Smith         if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES)) continue;
522fd3458f5SBarry Smith         if (in[j] >= cstart && in[j] < cend){
523fd3458f5SBarry Smith           col = in[j] - cstart;
5248d76821aSHong Zhang           nonew = a->nonew;
52530770e4dSSatish Balay           MatSetValues_SeqAIJ_A_Private(row,col,value,addv);
526273d9f13SBarry Smith         } else if (in[j] < 0) continue;
5272515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
528cb9801acSJed Brown         else if (in[j] >= mat->cmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[j],mat->cmap->N-1);
5290a198c4cSBarry Smith #endif
5301eb62cbbSBarry Smith         else {
531227d817aSBarry Smith           if (mat->was_assembled) {
532905e6a2fSBarry Smith             if (!aij->colmap) {
533ab9863d7SBarry Smith               ierr = MatCreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
534905e6a2fSBarry Smith             }
535aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
5360f5bd95cSBarry Smith             ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr);
537fa46199cSSatish Balay 	    col--;
538b1fc9764SSatish Balay #else
539905e6a2fSBarry Smith             col = aij->colmap[in[j]] - 1;
540b1fc9764SSatish Balay #endif
541ec8511deSBarry Smith             if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
542ab9863d7SBarry Smith               ierr = MatDisAssemble_MPIAIJ(mat);CHKERRQ(ierr);
5434b0e389bSBarry Smith               col =  in[j];
5449bf004c3SSatish Balay               /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
545f9508a3cSSatish Balay               B = aij->B;
546f9508a3cSSatish Balay               b = (Mat_SeqAIJ*)B->data;
547e44c0bd4SBarry Smith               bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j; ba = b->a;
548d498b1e9SBarry Smith               rp2      = bj + bi[row];
549d498b1e9SBarry Smith               ap2      = ba + bi[row];
550d498b1e9SBarry Smith               rmax2    = bimax[row];
551d498b1e9SBarry Smith               nrow2    = bilen[row];
552d498b1e9SBarry Smith               low2     = 0;
553d498b1e9SBarry Smith               high2    = nrow2;
554d0f46423SBarry Smith               bm       = aij->B->rmap->n;
555f9508a3cSSatish Balay               ba = b->a;
556d6dfbf8fSBarry Smith             }
557c48de900SBarry Smith           } else col = in[j];
5588d76821aSHong Zhang           nonew = b->nonew;
55930770e4dSSatish Balay           MatSetValues_SeqAIJ_B_Private(row,col,value,addv);
5601eb62cbbSBarry Smith         }
5611eb62cbbSBarry Smith       }
5625ef9f2a5SBarry Smith     } else {
5634cb17eb5SBarry Smith       if (mat->nooffprocentries) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Setting off process row %D even though MatSetOption(,MAT_NO_OFF_PROC_ENTRIES,PETSC_TRUE) was set",im[i]);
56490f02eecSBarry Smith       if (!aij->donotstash) {
5655080c13bSMatthew G Knepley         mat->assembled = PETSC_FALSE;
566d36fbae8SSatish Balay         if (roworiented) {
567ace3abfcSBarry Smith           ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr);
568d36fbae8SSatish Balay         } else {
569ace3abfcSBarry Smith           ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr);
5704b0e389bSBarry Smith         }
5711eb62cbbSBarry Smith       }
5728a729477SBarry Smith     }
57390f02eecSBarry Smith   }
5743a40ed3dSBarry Smith   PetscFunctionReturn(0);
5758a729477SBarry Smith }
5768a729477SBarry Smith 
5774a2ae208SSatish Balay #undef __FUNCT__
5784a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_MPIAIJ"
579b1d57f15SBarry Smith PetscErrorCode MatGetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[])
580b49de8d1SLois Curfman McInnes {
581b49de8d1SLois Curfman McInnes   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
582dfbe8321SBarry Smith   PetscErrorCode ierr;
583d0f46423SBarry Smith   PetscInt       i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend;
584d0f46423SBarry Smith   PetscInt       cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col;
585b49de8d1SLois Curfman McInnes 
5863a40ed3dSBarry Smith   PetscFunctionBegin;
587b49de8d1SLois Curfman McInnes   for (i=0; i<m; i++) {
588e32f2f54SBarry Smith     if (idxm[i] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",idxm[i]);*/
589e32f2f54SBarry Smith     if (idxm[i] >= mat->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",idxm[i],mat->rmap->N-1);
590b49de8d1SLois Curfman McInnes     if (idxm[i] >= rstart && idxm[i] < rend) {
591b49de8d1SLois Curfman McInnes       row = idxm[i] - rstart;
592b49de8d1SLois Curfman McInnes       for (j=0; j<n; j++) {
593e32f2f54SBarry Smith         if (idxn[j] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",idxn[j]); */
594e32f2f54SBarry Smith         if (idxn[j] >= mat->cmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",idxn[j],mat->cmap->N-1);
595b49de8d1SLois Curfman McInnes         if (idxn[j] >= cstart && idxn[j] < cend){
596b49de8d1SLois Curfman McInnes           col = idxn[j] - cstart;
597b49de8d1SLois Curfman McInnes           ierr = MatGetValues(aij->A,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
598fa852ad4SSatish Balay         } else {
599905e6a2fSBarry Smith           if (!aij->colmap) {
600ab9863d7SBarry Smith             ierr = MatCreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
601905e6a2fSBarry Smith           }
602aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
6030f5bd95cSBarry Smith           ierr = PetscTableFind(aij->colmap,idxn[j]+1,&col);CHKERRQ(ierr);
604fa46199cSSatish Balay           col --;
605b1fc9764SSatish Balay #else
606905e6a2fSBarry Smith           col = aij->colmap[idxn[j]] - 1;
607b1fc9764SSatish Balay #endif
608e60e1c95SSatish Balay           if ((col < 0) || (aij->garray[col] != idxn[j])) *(v+i*n+j) = 0.0;
609d9d09a02SSatish Balay           else {
610b49de8d1SLois Curfman McInnes             ierr = MatGetValues(aij->B,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
611b49de8d1SLois Curfman McInnes           }
612b49de8d1SLois Curfman McInnes         }
613b49de8d1SLois Curfman McInnes       }
614a8c6a408SBarry Smith     } else {
615e32f2f54SBarry Smith       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local values currently supported");
616b49de8d1SLois Curfman McInnes     }
617b49de8d1SLois Curfman McInnes   }
6183a40ed3dSBarry Smith   PetscFunctionReturn(0);
619b49de8d1SLois Curfman McInnes }
620bc5ccf88SSatish Balay 
621bd0c2dcbSBarry Smith extern PetscErrorCode MatMultDiagonalBlock_MPIAIJ(Mat,Vec,Vec);
622bd0c2dcbSBarry Smith 
6234a2ae208SSatish Balay #undef __FUNCT__
6244a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyBegin_MPIAIJ"
625dfbe8321SBarry Smith PetscErrorCode MatAssemblyBegin_MPIAIJ(Mat mat,MatAssemblyType mode)
626bc5ccf88SSatish Balay {
627bc5ccf88SSatish Balay   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
628dfbe8321SBarry Smith   PetscErrorCode ierr;
629b1d57f15SBarry Smith   PetscInt       nstash,reallocs;
630bc5ccf88SSatish Balay   InsertMode     addv;
631bc5ccf88SSatish Balay 
632bc5ccf88SSatish Balay   PetscFunctionBegin;
6334cb17eb5SBarry Smith   if (aij->donotstash || mat->nooffprocentries) {
634bc5ccf88SSatish Balay     PetscFunctionReturn(0);
635bc5ccf88SSatish Balay   }
636bc5ccf88SSatish Balay 
637bc5ccf88SSatish Balay   /* make sure all processors are either in INSERTMODE or ADDMODE */
6387adad957SLisandro Dalcin   ierr = MPI_Allreduce(&mat->insertmode,&addv,1,MPI_INT,MPI_BOR,((PetscObject)mat)->comm);CHKERRQ(ierr);
639e7e72b3dSBarry Smith   if (addv == (ADD_VALUES|INSERT_VALUES)) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Some processors inserted others added");
640bc5ccf88SSatish Balay   mat->insertmode = addv; /* in case this processor had no cache */
641bc5ccf88SSatish Balay 
642d0f46423SBarry Smith   ierr = MatStashScatterBegin_Private(mat,&mat->stash,mat->rmap->range);CHKERRQ(ierr);
6438798bf22SSatish Balay   ierr = MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);CHKERRQ(ierr);
644ae15b995SBarry Smith   ierr = PetscInfo2(aij->A,"Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);CHKERRQ(ierr);
645bc5ccf88SSatish Balay   PetscFunctionReturn(0);
646bc5ccf88SSatish Balay }
647bc5ccf88SSatish Balay 
6484a2ae208SSatish Balay #undef __FUNCT__
6494a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_MPIAIJ"
650dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_MPIAIJ(Mat mat,MatAssemblyType mode)
651bc5ccf88SSatish Balay {
652bc5ccf88SSatish Balay   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
65391c97fd4SSatish Balay   Mat_SeqAIJ     *a=(Mat_SeqAIJ *)aij->A->data;
6546849ba73SBarry Smith   PetscErrorCode ierr;
655b1d57f15SBarry Smith   PetscMPIInt    n;
656b1d57f15SBarry Smith   PetscInt       i,j,rstart,ncols,flg;
657e44c0bd4SBarry Smith   PetscInt       *row,*col;
658ace3abfcSBarry Smith   PetscBool      other_disassembled;
65987828ca2SBarry Smith   PetscScalar    *val;
660bc5ccf88SSatish Balay   InsertMode     addv = mat->insertmode;
661bc5ccf88SSatish Balay 
66291c97fd4SSatish Balay   /* do not use 'b = (Mat_SeqAIJ *)aij->B->data' as B can be reset in disassembly */
663bc5ccf88SSatish Balay   PetscFunctionBegin;
6644cb17eb5SBarry Smith   if (!aij->donotstash && !mat->nooffprocentries) {
665a2d1c673SSatish Balay     while (1) {
6668798bf22SSatish Balay       ierr = MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);CHKERRQ(ierr);
667a2d1c673SSatish Balay       if (!flg) break;
668a2d1c673SSatish Balay 
669bc5ccf88SSatish Balay       for (i=0; i<n;) {
670bc5ccf88SSatish Balay         /* Now identify the consecutive vals belonging to the same row */
671bc5ccf88SSatish Balay         for (j=i,rstart=row[j]; j<n; j++) { if (row[j] != rstart) break; }
672bc5ccf88SSatish Balay         if (j < n) ncols = j-i;
673bc5ccf88SSatish Balay         else       ncols = n-i;
674bc5ccf88SSatish Balay         /* Now assemble all these values with a single function call */
675bc5ccf88SSatish Balay         ierr = MatSetValues_MPIAIJ(mat,1,row+i,ncols,col+i,val+i,addv);CHKERRQ(ierr);
676bc5ccf88SSatish Balay         i = j;
677bc5ccf88SSatish Balay       }
678bc5ccf88SSatish Balay     }
6798798bf22SSatish Balay     ierr = MatStashScatterEnd_Private(&mat->stash);CHKERRQ(ierr);
680bc5ccf88SSatish Balay   }
681bc5ccf88SSatish Balay   ierr = MatAssemblyBegin(aij->A,mode);CHKERRQ(ierr);
682bc5ccf88SSatish Balay   ierr = MatAssemblyEnd(aij->A,mode);CHKERRQ(ierr);
683bc5ccf88SSatish Balay 
684bc5ccf88SSatish Balay   /* determine if any processor has disassembled, if so we must
685bc5ccf88SSatish Balay      also disassemble ourselfs, in order that we may reassemble. */
686bc5ccf88SSatish Balay   /*
687bc5ccf88SSatish Balay      if nonzero structure of submatrix B cannot change then we know that
688bc5ccf88SSatish Balay      no processor disassembled thus we can skip this stuff
689bc5ccf88SSatish Balay   */
690bc5ccf88SSatish Balay   if (!((Mat_SeqAIJ*)aij->B->data)->nonew)  {
6917adad957SLisandro Dalcin     ierr = MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPI_INT,MPI_PROD,((PetscObject)mat)->comm);CHKERRQ(ierr);
692bc5ccf88SSatish Balay     if (mat->was_assembled && !other_disassembled) {
693ab9863d7SBarry Smith       ierr = MatDisAssemble_MPIAIJ(mat);CHKERRQ(ierr);
694ad59fb31SSatish Balay     }
695ad59fb31SSatish Balay   }
696bc5ccf88SSatish Balay   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
697bc5ccf88SSatish Balay     ierr = MatSetUpMultiply_MPIAIJ(mat);CHKERRQ(ierr);
698bc5ccf88SSatish Balay   }
6994e0d8c25SBarry Smith   ierr = MatSetOption(aij->B,MAT_USE_INODES,PETSC_FALSE);CHKERRQ(ierr);
7004e35b6f3SSatish Balay   ierr = MatSetOption(aij->B,MAT_CHECK_COMPRESSED_ROW,PETSC_FALSE);CHKERRQ(ierr);
701bc5ccf88SSatish Balay   ierr = MatAssemblyBegin(aij->B,mode);CHKERRQ(ierr);
702bc5ccf88SSatish Balay   ierr = MatAssemblyEnd(aij->B,mode);CHKERRQ(ierr);
703bc5ccf88SSatish Balay 
7041d79065fSBarry Smith   ierr = PetscFree2(aij->rowvalues,aij->rowindices);CHKERRQ(ierr);
705606d414cSSatish Balay   aij->rowvalues = 0;
706a30b2313SHong Zhang 
707a30b2313SHong Zhang   /* used by MatAXPY() */
70891c97fd4SSatish Balay   a->xtoy = 0; ((Mat_SeqAIJ *)aij->B->data)->xtoy = 0;  /* b->xtoy = 0 */
70991c97fd4SSatish Balay   a->XtoY = 0; ((Mat_SeqAIJ *)aij->B->data)->XtoY = 0;  /* b->XtoY = 0 */
710a30b2313SHong Zhang 
7116bf464f9SBarry Smith   ierr = VecDestroy(&aij->diag);CHKERRQ(ierr);
712bd0c2dcbSBarry Smith   if (a->inode.size) mat->ops->multdiagonalblock = MatMultDiagonalBlock_MPIAIJ;
713bc5ccf88SSatish Balay   PetscFunctionReturn(0);
714bc5ccf88SSatish Balay }
715bc5ccf88SSatish Balay 
7164a2ae208SSatish Balay #undef __FUNCT__
7174a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_MPIAIJ"
718dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_MPIAIJ(Mat A)
7191eb62cbbSBarry Smith {
72044a69424SLois Curfman McInnes   Mat_MPIAIJ     *l = (Mat_MPIAIJ*)A->data;
721dfbe8321SBarry Smith   PetscErrorCode ierr;
7223a40ed3dSBarry Smith 
7233a40ed3dSBarry Smith   PetscFunctionBegin;
72478b31e54SBarry Smith   ierr = MatZeroEntries(l->A);CHKERRQ(ierr);
72578b31e54SBarry Smith   ierr = MatZeroEntries(l->B);CHKERRQ(ierr);
7263a40ed3dSBarry Smith   PetscFunctionReturn(0);
7271eb62cbbSBarry Smith }
7281eb62cbbSBarry Smith 
7294a2ae208SSatish Balay #undef __FUNCT__
7304a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_MPIAIJ"
7312b40b63fSBarry Smith PetscErrorCode MatZeroRows_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
7321eb62cbbSBarry Smith {
73344a69424SLois Curfman McInnes   Mat_MPIAIJ        *l = (Mat_MPIAIJ*)A->data;
7346849ba73SBarry Smith   PetscErrorCode    ierr;
7357adad957SLisandro Dalcin   PetscMPIInt       size = l->size,imdex,n,rank = l->rank,tag = ((PetscObject)A)->tag,lastidx = -1;
736d0f46423SBarry Smith   PetscInt          i,*owners = A->rmap->range;
737b1d57f15SBarry Smith   PetscInt          *nprocs,j,idx,nsends,row;
738b1d57f15SBarry Smith   PetscInt          nmax,*svalues,*starts,*owner,nrecvs;
739b1d57f15SBarry Smith   PetscInt          *rvalues,count,base,slen,*source;
740d0f46423SBarry Smith   PetscInt          *lens,*lrows,*values,rstart=A->rmap->rstart;
7417adad957SLisandro Dalcin   MPI_Comm          comm = ((PetscObject)A)->comm;
7421eb62cbbSBarry Smith   MPI_Request       *send_waits,*recv_waits;
7431eb62cbbSBarry Smith   MPI_Status        recv_status,*send_status;
74497b48c8fSBarry Smith   const PetscScalar *xx;
74597b48c8fSBarry Smith   PetscScalar       *bb;
7466543fbbaSBarry Smith #if defined(PETSC_DEBUG)
747ace3abfcSBarry Smith   PetscBool      found = PETSC_FALSE;
7486543fbbaSBarry Smith #endif
7491eb62cbbSBarry Smith 
7503a40ed3dSBarry Smith   PetscFunctionBegin;
7511eb62cbbSBarry Smith   /*  first count number of contributors to each processor */
752b1d57f15SBarry Smith   ierr = PetscMalloc(2*size*sizeof(PetscInt),&nprocs);CHKERRQ(ierr);
753b1d57f15SBarry Smith   ierr = PetscMemzero(nprocs,2*size*sizeof(PetscInt));CHKERRQ(ierr);
754b1d57f15SBarry Smith   ierr = PetscMalloc((N+1)*sizeof(PetscInt),&owner);CHKERRQ(ierr); /* see note*/
7556543fbbaSBarry Smith   j = 0;
7561eb62cbbSBarry Smith   for (i=0; i<N; i++) {
7576543fbbaSBarry Smith     if (lastidx > (idx = rows[i])) j = 0;
7586543fbbaSBarry Smith     lastidx = idx;
7596543fbbaSBarry Smith     for (; j<size; j++) {
7601eb62cbbSBarry Smith       if (idx >= owners[j] && idx < owners[j+1]) {
7616543fbbaSBarry Smith         nprocs[2*j]++;
7626543fbbaSBarry Smith         nprocs[2*j+1] = 1;
7636543fbbaSBarry Smith         owner[i] = j;
7646543fbbaSBarry Smith #if defined(PETSC_DEBUG)
7656543fbbaSBarry Smith         found = PETSC_TRUE;
7666543fbbaSBarry Smith #endif
7676543fbbaSBarry Smith         break;
7681eb62cbbSBarry Smith       }
7691eb62cbbSBarry Smith     }
7706543fbbaSBarry Smith #if defined(PETSC_DEBUG)
771e32f2f54SBarry Smith     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Index out of range");
7726543fbbaSBarry Smith     found = PETSC_FALSE;
7736543fbbaSBarry Smith #endif
7741eb62cbbSBarry Smith   }
775c1dc657dSBarry Smith   nsends = 0;  for (i=0; i<size; i++) { nsends += nprocs[2*i+1];}
7761eb62cbbSBarry Smith 
7777367270fSBarry Smith   if (A->nooffproczerorows) {
7787367270fSBarry Smith     if (nsends > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"You called MatSetOption(,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) but set an off process zero row");
7797367270fSBarry Smith     nrecvs = nsends;
7807367270fSBarry Smith     nmax   = N;
7817367270fSBarry Smith   } else {
7821eb62cbbSBarry Smith     /* inform other processors of number of messages and max length*/
783c1dc657dSBarry Smith     ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr);
7847367270fSBarry Smith   }
7851eb62cbbSBarry Smith 
7861eb62cbbSBarry Smith   /* post receives:   */
787b1d57f15SBarry Smith   ierr = PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(PetscInt),&rvalues);CHKERRQ(ierr);
788b0a32e0cSBarry Smith   ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr);
7891eb62cbbSBarry Smith   for (i=0; i<nrecvs; i++) {
790b1d57f15SBarry Smith     ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPIU_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr);
7911eb62cbbSBarry Smith   }
7921eb62cbbSBarry Smith 
7931eb62cbbSBarry Smith   /* do sends:
7941eb62cbbSBarry Smith       1) starts[i] gives the starting index in svalues for stuff going to
7951eb62cbbSBarry Smith          the ith processor
7961eb62cbbSBarry Smith   */
797b1d57f15SBarry Smith   ierr = PetscMalloc((N+1)*sizeof(PetscInt),&svalues);CHKERRQ(ierr);
798b0a32e0cSBarry Smith   ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr);
799b1d57f15SBarry Smith   ierr = PetscMalloc((size+1)*sizeof(PetscInt),&starts);CHKERRQ(ierr);
8001eb62cbbSBarry Smith   starts[0] = 0;
801c1dc657dSBarry Smith   for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
8021eb62cbbSBarry Smith   for (i=0; i<N; i++) {
8031eb62cbbSBarry Smith     svalues[starts[owner[i]]++] = rows[i];
8041eb62cbbSBarry Smith   }
8051eb62cbbSBarry Smith 
8061eb62cbbSBarry Smith   starts[0] = 0;
807c1dc657dSBarry Smith   for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
8081eb62cbbSBarry Smith   count = 0;
80917699dbbSLois Curfman McInnes   for (i=0; i<size; i++) {
810c1dc657dSBarry Smith     if (nprocs[2*i+1]) {
811b1d57f15SBarry Smith       ierr = MPI_Isend(svalues+starts[i],nprocs[2*i],MPIU_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr);
8121eb62cbbSBarry Smith     }
8131eb62cbbSBarry Smith   }
814606d414cSSatish Balay   ierr = PetscFree(starts);CHKERRQ(ierr);
8151eb62cbbSBarry Smith 
81617699dbbSLois Curfman McInnes   base = owners[rank];
8171eb62cbbSBarry Smith 
8181eb62cbbSBarry Smith   /*  wait on receives */
8191d79065fSBarry Smith   ierr   = PetscMalloc2(nrecvs,PetscInt,&lens,nrecvs,PetscInt,&source);CHKERRQ(ierr);
8201eb62cbbSBarry Smith   count  = nrecvs; slen = 0;
8211eb62cbbSBarry Smith   while (count) {
822ca161407SBarry Smith     ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
8231eb62cbbSBarry Smith     /* unpack receives into our local space */
824b1d57f15SBarry Smith     ierr = MPI_Get_count(&recv_status,MPIU_INT,&n);CHKERRQ(ierr);
825d6dfbf8fSBarry Smith     source[imdex]  = recv_status.MPI_SOURCE;
826d6dfbf8fSBarry Smith     lens[imdex]    = n;
8271eb62cbbSBarry Smith     slen          += n;
8281eb62cbbSBarry Smith     count--;
8291eb62cbbSBarry Smith   }
830606d414cSSatish Balay   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
8311eb62cbbSBarry Smith 
8321eb62cbbSBarry Smith   /* move the data into the send scatter */
833b1d57f15SBarry Smith   ierr = PetscMalloc((slen+1)*sizeof(PetscInt),&lrows);CHKERRQ(ierr);
8341eb62cbbSBarry Smith   count = 0;
8351eb62cbbSBarry Smith   for (i=0; i<nrecvs; i++) {
8361eb62cbbSBarry Smith     values = rvalues + i*nmax;
8371eb62cbbSBarry Smith     for (j=0; j<lens[i]; j++) {
8381eb62cbbSBarry Smith       lrows[count++] = values[j] - base;
8391eb62cbbSBarry Smith     }
8401eb62cbbSBarry Smith   }
841606d414cSSatish Balay   ierr = PetscFree(rvalues);CHKERRQ(ierr);
8421d79065fSBarry Smith   ierr = PetscFree2(lens,source);CHKERRQ(ierr);
843606d414cSSatish Balay   ierr = PetscFree(owner);CHKERRQ(ierr);
844606d414cSSatish Balay   ierr = PetscFree(nprocs);CHKERRQ(ierr);
8451eb62cbbSBarry Smith 
84697b48c8fSBarry Smith   /* fix right hand side if needed */
84797b48c8fSBarry Smith   if (x && b) {
84897b48c8fSBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
84997b48c8fSBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
850564f14d6SBarry Smith     for (i=0; i<slen; i++) {
85197b48c8fSBarry Smith       bb[lrows[i]] = diag*xx[lrows[i]];
85297b48c8fSBarry Smith     }
85397b48c8fSBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
85497b48c8fSBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
85597b48c8fSBarry Smith   }
8566eb55b6aSBarry Smith   /*
8576eb55b6aSBarry Smith         Zero the required rows. If the "diagonal block" of the matrix
858a8c7a070SBarry Smith      is square and the user wishes to set the diagonal we use separate
8596eb55b6aSBarry Smith      code so that MatSetValues() is not called for each diagonal allocating
8606eb55b6aSBarry Smith      new memory, thus calling lots of mallocs and slowing things down.
8616eb55b6aSBarry Smith 
8626eb55b6aSBarry Smith   */
863e2d53e46SBarry Smith   /* must zero l->B before l->A because the (diag) case below may put values into l->B*/
8642b40b63fSBarry Smith   ierr = MatZeroRows(l->B,slen,lrows,0.0,0,0);CHKERRQ(ierr);
865d0f46423SBarry Smith   if ((diag != 0.0) && (l->A->rmap->N == l->A->cmap->N)) {
8662b40b63fSBarry Smith     ierr = MatZeroRows(l->A,slen,lrows,diag,0,0);CHKERRQ(ierr);
867f4df32b1SMatthew Knepley   } else if (diag != 0.0) {
8682b40b63fSBarry Smith     ierr = MatZeroRows(l->A,slen,lrows,0.0,0,0);CHKERRQ(ierr);
869fa46199cSSatish Balay     if (((Mat_SeqAIJ*)l->A->data)->nonew) {
870e32f2f54SBarry Smith       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatZeroRows() on rectangular matrices cannot be used with the Mat options\n\
871512a5fc5SBarry Smith MAT_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR");
8726525c446SSatish Balay     }
873e2d53e46SBarry Smith     for (i = 0; i < slen; i++) {
874e2d53e46SBarry Smith       row  = lrows[i] + rstart;
875f4df32b1SMatthew Knepley       ierr = MatSetValues(A,1,&row,1,&row,&diag,INSERT_VALUES);CHKERRQ(ierr);
876e2d53e46SBarry Smith     }
877e2d53e46SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
878e2d53e46SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
8796eb55b6aSBarry Smith   } else {
8802b40b63fSBarry Smith     ierr = MatZeroRows(l->A,slen,lrows,0.0,0,0);CHKERRQ(ierr);
8816eb55b6aSBarry Smith   }
882606d414cSSatish Balay   ierr = PetscFree(lrows);CHKERRQ(ierr);
88372dacd9aSBarry Smith 
8841eb62cbbSBarry Smith   /* wait on sends */
8851eb62cbbSBarry Smith   if (nsends) {
886b0a32e0cSBarry Smith     ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr);
887ca161407SBarry Smith     ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr);
888606d414cSSatish Balay     ierr = PetscFree(send_status);CHKERRQ(ierr);
8891eb62cbbSBarry Smith   }
890606d414cSSatish Balay   ierr = PetscFree(send_waits);CHKERRQ(ierr);
891606d414cSSatish Balay   ierr = PetscFree(svalues);CHKERRQ(ierr);
8923a40ed3dSBarry Smith   PetscFunctionReturn(0);
8931eb62cbbSBarry Smith }
8941eb62cbbSBarry Smith 
8954a2ae208SSatish Balay #undef __FUNCT__
8969c7c4993SBarry Smith #define __FUNCT__ "MatZeroRowsColumns_MPIAIJ"
8979c7c4993SBarry Smith PetscErrorCode MatZeroRowsColumns_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
8989c7c4993SBarry Smith {
8999c7c4993SBarry Smith   Mat_MPIAIJ        *l = (Mat_MPIAIJ*)A->data;
9009c7c4993SBarry Smith   PetscErrorCode    ierr;
9019c7c4993SBarry Smith   PetscMPIInt       size = l->size,imdex,n,rank = l->rank,tag = ((PetscObject)A)->tag,lastidx = -1;
9029c7c4993SBarry Smith   PetscInt          i,*owners = A->rmap->range;
903564f14d6SBarry Smith   PetscInt          *nprocs,j,idx,nsends;
9049c7c4993SBarry Smith   PetscInt          nmax,*svalues,*starts,*owner,nrecvs;
9059c7c4993SBarry Smith   PetscInt          *rvalues,count,base,slen,*source;
906564f14d6SBarry Smith   PetscInt          *lens,*lrows,*values,m;
9079c7c4993SBarry Smith   MPI_Comm          comm = ((PetscObject)A)->comm;
9089c7c4993SBarry Smith   MPI_Request       *send_waits,*recv_waits;
9099c7c4993SBarry Smith   MPI_Status        recv_status,*send_status;
9109c7c4993SBarry Smith   const PetscScalar *xx;
911564f14d6SBarry Smith   PetscScalar       *bb,*mask;
912564f14d6SBarry Smith   Vec               xmask,lmask;
913564f14d6SBarry Smith   Mat_SeqAIJ        *aij = (Mat_SeqAIJ*)l->B->data;
914564f14d6SBarry Smith   const PetscInt    *aj, *ii,*ridx;
915564f14d6SBarry Smith   PetscScalar       *aa;
9169c7c4993SBarry Smith #if defined(PETSC_DEBUG)
9179c7c4993SBarry Smith   PetscBool         found = PETSC_FALSE;
9189c7c4993SBarry Smith #endif
9199c7c4993SBarry Smith 
9209c7c4993SBarry Smith   PetscFunctionBegin;
9219c7c4993SBarry Smith   /*  first count number of contributors to each processor */
9229c7c4993SBarry Smith   ierr = PetscMalloc(2*size*sizeof(PetscInt),&nprocs);CHKERRQ(ierr);
9239c7c4993SBarry Smith   ierr = PetscMemzero(nprocs,2*size*sizeof(PetscInt));CHKERRQ(ierr);
9249c7c4993SBarry Smith   ierr = PetscMalloc((N+1)*sizeof(PetscInt),&owner);CHKERRQ(ierr); /* see note*/
9259c7c4993SBarry Smith   j = 0;
9269c7c4993SBarry Smith   for (i=0; i<N; i++) {
9279c7c4993SBarry Smith     if (lastidx > (idx = rows[i])) j = 0;
9289c7c4993SBarry Smith     lastidx = idx;
9299c7c4993SBarry Smith     for (; j<size; j++) {
9309c7c4993SBarry Smith       if (idx >= owners[j] && idx < owners[j+1]) {
9319c7c4993SBarry Smith         nprocs[2*j]++;
9329c7c4993SBarry Smith         nprocs[2*j+1] = 1;
9339c7c4993SBarry Smith         owner[i] = j;
9349c7c4993SBarry Smith #if defined(PETSC_DEBUG)
9359c7c4993SBarry Smith         found = PETSC_TRUE;
9369c7c4993SBarry Smith #endif
9379c7c4993SBarry Smith         break;
9389c7c4993SBarry Smith       }
9399c7c4993SBarry Smith     }
9409c7c4993SBarry Smith #if defined(PETSC_DEBUG)
9419c7c4993SBarry Smith     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Index out of range");
9429c7c4993SBarry Smith     found = PETSC_FALSE;
9439c7c4993SBarry Smith #endif
9449c7c4993SBarry Smith   }
9459c7c4993SBarry Smith   nsends = 0;  for (i=0; i<size; i++) { nsends += nprocs[2*i+1];}
9469c7c4993SBarry Smith 
9479c7c4993SBarry Smith   /* inform other processors of number of messages and max length*/
9489c7c4993SBarry Smith   ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr);
9499c7c4993SBarry Smith 
9509c7c4993SBarry Smith   /* post receives:   */
9519c7c4993SBarry Smith   ierr = PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(PetscInt),&rvalues);CHKERRQ(ierr);
9529c7c4993SBarry Smith   ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr);
9539c7c4993SBarry Smith   for (i=0; i<nrecvs; i++) {
9549c7c4993SBarry Smith     ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPIU_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr);
9559c7c4993SBarry Smith   }
9569c7c4993SBarry Smith 
9579c7c4993SBarry Smith   /* do sends:
9589c7c4993SBarry Smith       1) starts[i] gives the starting index in svalues for stuff going to
9599c7c4993SBarry Smith          the ith processor
9609c7c4993SBarry Smith   */
9619c7c4993SBarry Smith   ierr = PetscMalloc((N+1)*sizeof(PetscInt),&svalues);CHKERRQ(ierr);
9629c7c4993SBarry Smith   ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr);
9639c7c4993SBarry Smith   ierr = PetscMalloc((size+1)*sizeof(PetscInt),&starts);CHKERRQ(ierr);
9649c7c4993SBarry Smith   starts[0] = 0;
9659c7c4993SBarry Smith   for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
9669c7c4993SBarry Smith   for (i=0; i<N; i++) {
9679c7c4993SBarry Smith     svalues[starts[owner[i]]++] = rows[i];
9689c7c4993SBarry Smith   }
9699c7c4993SBarry Smith 
9709c7c4993SBarry Smith   starts[0] = 0;
9719c7c4993SBarry Smith   for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
9729c7c4993SBarry Smith   count = 0;
9739c7c4993SBarry Smith   for (i=0; i<size; i++) {
9749c7c4993SBarry Smith     if (nprocs[2*i+1]) {
9759c7c4993SBarry Smith       ierr = MPI_Isend(svalues+starts[i],nprocs[2*i],MPIU_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr);
9769c7c4993SBarry Smith     }
9779c7c4993SBarry Smith   }
9789c7c4993SBarry Smith   ierr = PetscFree(starts);CHKERRQ(ierr);
9799c7c4993SBarry Smith 
9809c7c4993SBarry Smith   base = owners[rank];
9819c7c4993SBarry Smith 
9829c7c4993SBarry Smith   /*  wait on receives */
9839c7c4993SBarry Smith   ierr   = PetscMalloc2(nrecvs,PetscInt,&lens,nrecvs,PetscInt,&source);CHKERRQ(ierr);
9849c7c4993SBarry Smith   count  = nrecvs; slen = 0;
9859c7c4993SBarry Smith   while (count) {
9869c7c4993SBarry Smith     ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
9879c7c4993SBarry Smith     /* unpack receives into our local space */
9889c7c4993SBarry Smith     ierr = MPI_Get_count(&recv_status,MPIU_INT,&n);CHKERRQ(ierr);
9899c7c4993SBarry Smith     source[imdex]  = recv_status.MPI_SOURCE;
9909c7c4993SBarry Smith     lens[imdex]    = n;
9919c7c4993SBarry Smith     slen          += n;
9929c7c4993SBarry Smith     count--;
9939c7c4993SBarry Smith   }
9949c7c4993SBarry Smith   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
9959c7c4993SBarry Smith 
9969c7c4993SBarry Smith   /* move the data into the send scatter */
9979c7c4993SBarry Smith   ierr = PetscMalloc((slen+1)*sizeof(PetscInt),&lrows);CHKERRQ(ierr);
9989c7c4993SBarry Smith   count = 0;
9999c7c4993SBarry Smith   for (i=0; i<nrecvs; i++) {
10009c7c4993SBarry Smith     values = rvalues + i*nmax;
10019c7c4993SBarry Smith     for (j=0; j<lens[i]; j++) {
10029c7c4993SBarry Smith       lrows[count++] = values[j] - base;
10039c7c4993SBarry Smith     }
10049c7c4993SBarry Smith   }
10059c7c4993SBarry Smith   ierr = PetscFree(rvalues);CHKERRQ(ierr);
10069c7c4993SBarry Smith   ierr = PetscFree2(lens,source);CHKERRQ(ierr);
10079c7c4993SBarry Smith   ierr = PetscFree(owner);CHKERRQ(ierr);
10089c7c4993SBarry Smith   ierr = PetscFree(nprocs);CHKERRQ(ierr);
1009564f14d6SBarry Smith   /* lrows are the local rows to be zeroed, slen is the number of local rows */
10109c7c4993SBarry Smith 
1011564f14d6SBarry Smith   /* zero diagonal part of matrix */
1012564f14d6SBarry Smith   ierr = MatZeroRowsColumns(l->A,slen,lrows,diag,x,b);CHKERRQ(ierr);
10139c7c4993SBarry Smith 
1014564f14d6SBarry Smith   /* handle off diagonal part of matrix */
1015564f14d6SBarry Smith   ierr = MatGetVecs(A,&xmask,PETSC_NULL);CHKERRQ(ierr);
1016564f14d6SBarry Smith   ierr = VecDuplicate(l->lvec,&lmask);CHKERRQ(ierr);
1017564f14d6SBarry Smith   ierr = VecGetArray(xmask,&bb);CHKERRQ(ierr);
10189c7c4993SBarry Smith   for (i=0; i<slen; i++) {
1019564f14d6SBarry Smith     bb[lrows[i]] = 1;
10209c7c4993SBarry Smith   }
1021564f14d6SBarry Smith   ierr = VecRestoreArray(xmask,&bb);CHKERRQ(ierr);
1022564f14d6SBarry Smith   ierr = VecScatterBegin(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1023564f14d6SBarry Smith   ierr = VecScatterEnd(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
10246bf464f9SBarry Smith   ierr = VecDestroy(&xmask);CHKERRQ(ierr);
1025377aa5a1SBarry Smith   if (x) {
1026564f14d6SBarry Smith     ierr = VecScatterBegin(l->Mvctx,x,l->lvec,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1027564f14d6SBarry Smith     ierr = VecScatterEnd(l->Mvctx,x,l->lvec,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1028564f14d6SBarry Smith     ierr = VecGetArrayRead(l->lvec,&xx);CHKERRQ(ierr);
1029564f14d6SBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
1030377aa5a1SBarry Smith   }
1031377aa5a1SBarry Smith   ierr = VecGetArray(lmask,&mask);CHKERRQ(ierr);
1032564f14d6SBarry Smith 
1033564f14d6SBarry Smith   /* remove zeroed rows of off diagonal matrix */
1034564f14d6SBarry Smith   ii = aij->i;
1035564f14d6SBarry Smith   for (i=0; i<slen; i++) {
1036564f14d6SBarry Smith     ierr = PetscMemzero(aij->a + ii[lrows[i]],(ii[lrows[i]+1] - ii[lrows[i]])*sizeof(PetscScalar));CHKERRQ(ierr);
10379c7c4993SBarry Smith   }
1038564f14d6SBarry Smith 
1039564f14d6SBarry Smith   /* loop over all elements of off process part of matrix zeroing removed columns*/
1040564f14d6SBarry Smith   if (aij->compressedrow.use){
1041564f14d6SBarry Smith     m    = aij->compressedrow.nrows;
1042564f14d6SBarry Smith     ii   = aij->compressedrow.i;
1043564f14d6SBarry Smith     ridx = aij->compressedrow.rindex;
1044564f14d6SBarry Smith     for (i=0; i<m; i++){
1045564f14d6SBarry Smith       n   = ii[i+1] - ii[i];
1046564f14d6SBarry Smith       aj  = aij->j + ii[i];
1047564f14d6SBarry Smith       aa  = aij->a + ii[i];
1048564f14d6SBarry Smith 
1049564f14d6SBarry Smith       for (j=0; j<n; j++) {
105025266a92SSatish Balay         if (PetscAbsScalar(mask[*aj])) {
1051377aa5a1SBarry Smith           if (b) bb[*ridx] -= *aa*xx[*aj];
1052564f14d6SBarry Smith           *aa        = 0.0;
1053564f14d6SBarry Smith         }
1054564f14d6SBarry Smith         aa++;
1055564f14d6SBarry Smith         aj++;
1056564f14d6SBarry Smith       }
1057564f14d6SBarry Smith       ridx++;
1058564f14d6SBarry Smith     }
1059564f14d6SBarry Smith   } else { /* do not use compressed row format */
1060564f14d6SBarry Smith     m = l->B->rmap->n;
1061564f14d6SBarry Smith     for (i=0; i<m; i++) {
1062564f14d6SBarry Smith       n   = ii[i+1] - ii[i];
1063564f14d6SBarry Smith       aj  = aij->j + ii[i];
1064564f14d6SBarry Smith       aa  = aij->a + ii[i];
1065564f14d6SBarry Smith       for (j=0; j<n; j++) {
106625266a92SSatish Balay         if (PetscAbsScalar(mask[*aj])) {
1067377aa5a1SBarry Smith           if (b) bb[i] -= *aa*xx[*aj];
1068564f14d6SBarry Smith           *aa    = 0.0;
1069564f14d6SBarry Smith         }
1070564f14d6SBarry Smith         aa++;
1071564f14d6SBarry Smith         aj++;
1072564f14d6SBarry Smith       }
1073564f14d6SBarry Smith     }
1074564f14d6SBarry Smith   }
1075377aa5a1SBarry Smith   if (x) {
1076564f14d6SBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
1077564f14d6SBarry Smith     ierr = VecRestoreArrayRead(l->lvec,&xx);CHKERRQ(ierr);
1078377aa5a1SBarry Smith   }
1079377aa5a1SBarry Smith   ierr = VecRestoreArray(lmask,&mask);CHKERRQ(ierr);
10806bf464f9SBarry Smith   ierr = VecDestroy(&lmask);CHKERRQ(ierr);
10819c7c4993SBarry Smith   ierr = PetscFree(lrows);CHKERRQ(ierr);
10829c7c4993SBarry Smith 
10839c7c4993SBarry Smith   /* wait on sends */
10849c7c4993SBarry Smith   if (nsends) {
10859c7c4993SBarry Smith     ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr);
10869c7c4993SBarry Smith     ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr);
10879c7c4993SBarry Smith     ierr = PetscFree(send_status);CHKERRQ(ierr);
10889c7c4993SBarry Smith   }
10899c7c4993SBarry Smith   ierr = PetscFree(send_waits);CHKERRQ(ierr);
10909c7c4993SBarry Smith   ierr = PetscFree(svalues);CHKERRQ(ierr);
10919c7c4993SBarry Smith 
10929c7c4993SBarry Smith   PetscFunctionReturn(0);
10939c7c4993SBarry Smith }
10949c7c4993SBarry Smith 
10959c7c4993SBarry Smith #undef __FUNCT__
10964a2ae208SSatish Balay #define __FUNCT__ "MatMult_MPIAIJ"
1097dfbe8321SBarry Smith PetscErrorCode MatMult_MPIAIJ(Mat A,Vec xx,Vec yy)
10981eb62cbbSBarry Smith {
1099416022c9SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
1100dfbe8321SBarry Smith   PetscErrorCode ierr;
1101b1d57f15SBarry Smith   PetscInt       nt;
1102416022c9SBarry Smith 
11033a40ed3dSBarry Smith   PetscFunctionBegin;
1104a2ce50c7SBarry Smith   ierr = VecGetLocalSize(xx,&nt);CHKERRQ(ierr);
110565e19b50SBarry Smith   if (nt != A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Incompatible partition of A (%D) and xx (%D)",A->cmap->n,nt);
1106ca9f406cSSatish Balay   ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1107f830108cSBarry Smith   ierr = (*a->A->ops->mult)(a->A,xx,yy);CHKERRQ(ierr);
1108ca9f406cSSatish Balay   ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1109f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);CHKERRQ(ierr);
11103a40ed3dSBarry Smith   PetscFunctionReturn(0);
11111eb62cbbSBarry Smith }
11121eb62cbbSBarry Smith 
11134a2ae208SSatish Balay #undef __FUNCT__
1114bd0c2dcbSBarry Smith #define __FUNCT__ "MatMultDiagonalBlock_MPIAIJ"
1115bd0c2dcbSBarry Smith PetscErrorCode MatMultDiagonalBlock_MPIAIJ(Mat A,Vec bb,Vec xx)
1116bd0c2dcbSBarry Smith {
1117bd0c2dcbSBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
1118bd0c2dcbSBarry Smith   PetscErrorCode ierr;
1119bd0c2dcbSBarry Smith 
1120bd0c2dcbSBarry Smith   PetscFunctionBegin;
1121bd0c2dcbSBarry Smith   ierr = MatMultDiagonalBlock(a->A,bb,xx);CHKERRQ(ierr);
1122bd0c2dcbSBarry Smith   PetscFunctionReturn(0);
1123bd0c2dcbSBarry Smith }
1124bd0c2dcbSBarry Smith 
1125bd0c2dcbSBarry Smith #undef __FUNCT__
11264a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_MPIAIJ"
1127dfbe8321SBarry Smith PetscErrorCode MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1128da3a660dSBarry Smith {
1129416022c9SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
1130dfbe8321SBarry Smith   PetscErrorCode ierr;
11313a40ed3dSBarry Smith 
11323a40ed3dSBarry Smith   PetscFunctionBegin;
1133ca9f406cSSatish Balay   ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1134f830108cSBarry Smith   ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
1135ca9f406cSSatish Balay   ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1136f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);CHKERRQ(ierr);
11373a40ed3dSBarry Smith   PetscFunctionReturn(0);
1138da3a660dSBarry Smith }
1139da3a660dSBarry Smith 
11404a2ae208SSatish Balay #undef __FUNCT__
11414a2ae208SSatish Balay #define __FUNCT__ "MatMultTranspose_MPIAIJ"
1142dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_MPIAIJ(Mat A,Vec xx,Vec yy)
1143da3a660dSBarry Smith {
1144416022c9SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
1145dfbe8321SBarry Smith   PetscErrorCode ierr;
1146ace3abfcSBarry Smith   PetscBool      merged;
1147da3a660dSBarry Smith 
11483a40ed3dSBarry Smith   PetscFunctionBegin;
1149a5ff213dSBarry Smith   ierr = VecScatterGetMerged(a->Mvctx,&merged);CHKERRQ(ierr);
1150da3a660dSBarry Smith   /* do nondiagonal part */
11517c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
1152a5ff213dSBarry Smith   if (!merged) {
1153da3a660dSBarry Smith     /* send it on its way */
1154ca9f406cSSatish Balay     ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1155da3a660dSBarry Smith     /* do local part */
11567c922b88SBarry Smith     ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr);
1157da3a660dSBarry Smith     /* receive remote parts: note this assumes the values are not actually */
1158a5ff213dSBarry Smith     /* added in yy until the next line, */
1159ca9f406cSSatish Balay     ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1160a5ff213dSBarry Smith   } else {
1161a5ff213dSBarry Smith     /* do local part */
1162a5ff213dSBarry Smith     ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr);
1163a5ff213dSBarry Smith     /* send it on its way */
1164ca9f406cSSatish Balay     ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1165a5ff213dSBarry Smith     /* values actually were received in the Begin() but we need to call this nop */
1166ca9f406cSSatish Balay     ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1167a5ff213dSBarry Smith   }
11683a40ed3dSBarry Smith   PetscFunctionReturn(0);
1169da3a660dSBarry Smith }
1170da3a660dSBarry Smith 
1171cd0d46ebSvictorle EXTERN_C_BEGIN
1172cd0d46ebSvictorle #undef __FUNCT__
11735fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_MPIAIJ"
11747087cfbeSBarry Smith PetscErrorCode  MatIsTranspose_MPIAIJ(Mat Amat,Mat Bmat,PetscReal tol,PetscBool  *f)
1175cd0d46ebSvictorle {
11764f423910Svictorle   MPI_Comm       comm;
1177cd0d46ebSvictorle   Mat_MPIAIJ     *Aij = (Mat_MPIAIJ *) Amat->data, *Bij;
117866501d38Svictorle   Mat            Adia = Aij->A, Bdia, Aoff,Boff,*Aoffs,*Boffs;
1179cd0d46ebSvictorle   IS             Me,Notme;
11806849ba73SBarry Smith   PetscErrorCode ierr;
1181b1d57f15SBarry Smith   PetscInt       M,N,first,last,*notme,i;
1182b1d57f15SBarry Smith   PetscMPIInt    size;
1183cd0d46ebSvictorle 
1184cd0d46ebSvictorle   PetscFunctionBegin;
118542e5f5b4Svictorle 
118642e5f5b4Svictorle   /* Easy test: symmetric diagonal block */
118766501d38Svictorle   Bij = (Mat_MPIAIJ *) Bmat->data; Bdia = Bij->A;
11885485867bSBarry Smith   ierr = MatIsTranspose(Adia,Bdia,tol,f);CHKERRQ(ierr);
1189cd0d46ebSvictorle   if (!*f) PetscFunctionReturn(0);
11904f423910Svictorle   ierr = PetscObjectGetComm((PetscObject)Amat,&comm);CHKERRQ(ierr);
1191b1d57f15SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
1192b1d57f15SBarry Smith   if (size == 1) PetscFunctionReturn(0);
119342e5f5b4Svictorle 
119442e5f5b4Svictorle   /* Hard test: off-diagonal block. This takes a MatGetSubMatrix. */
1195cd0d46ebSvictorle   ierr = MatGetSize(Amat,&M,&N);CHKERRQ(ierr);
1196cd0d46ebSvictorle   ierr = MatGetOwnershipRange(Amat,&first,&last);CHKERRQ(ierr);
1197b1d57f15SBarry Smith   ierr = PetscMalloc((N-last+first)*sizeof(PetscInt),&notme);CHKERRQ(ierr);
1198cd0d46ebSvictorle   for (i=0; i<first; i++) notme[i] = i;
1199cd0d46ebSvictorle   for (i=last; i<M; i++) notme[i-last+first] = i;
120070b3c8c7SBarry Smith   ierr = ISCreateGeneral(MPI_COMM_SELF,N-last+first,notme,PETSC_COPY_VALUES,&Notme);CHKERRQ(ierr);
1201268466fbSBarry Smith   ierr = ISCreateStride(MPI_COMM_SELF,last-first,first,1,&Me);CHKERRQ(ierr);
1202268466fbSBarry Smith   ierr = MatGetSubMatrices(Amat,1,&Me,&Notme,MAT_INITIAL_MATRIX,&Aoffs);CHKERRQ(ierr);
120366501d38Svictorle   Aoff = Aoffs[0];
1204268466fbSBarry Smith   ierr = MatGetSubMatrices(Bmat,1,&Notme,&Me,MAT_INITIAL_MATRIX,&Boffs);CHKERRQ(ierr);
120566501d38Svictorle   Boff = Boffs[0];
12065485867bSBarry Smith   ierr = MatIsTranspose(Aoff,Boff,tol,f);CHKERRQ(ierr);
120766501d38Svictorle   ierr = MatDestroyMatrices(1,&Aoffs);CHKERRQ(ierr);
120866501d38Svictorle   ierr = MatDestroyMatrices(1,&Boffs);CHKERRQ(ierr);
12096bf464f9SBarry Smith   ierr = ISDestroy(&Me);CHKERRQ(ierr);
12106bf464f9SBarry Smith   ierr = ISDestroy(&Notme);CHKERRQ(ierr);
12113e0d0d19SHong Zhang   ierr = PetscFree(notme);CHKERRQ(ierr);
1212cd0d46ebSvictorle    PetscFunctionReturn(0);
1213cd0d46ebSvictorle }
1214cd0d46ebSvictorle EXTERN_C_END
1215cd0d46ebSvictorle 
12164a2ae208SSatish Balay #undef __FUNCT__
12174a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_MPIAIJ"
1218dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1219da3a660dSBarry Smith {
1220416022c9SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
1221dfbe8321SBarry Smith   PetscErrorCode ierr;
1222da3a660dSBarry Smith 
12233a40ed3dSBarry Smith   PetscFunctionBegin;
1224da3a660dSBarry Smith   /* do nondiagonal part */
12257c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
1226da3a660dSBarry Smith   /* send it on its way */
1227ca9f406cSSatish Balay   ierr = VecScatterBegin(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1228da3a660dSBarry Smith   /* do local part */
12297c922b88SBarry Smith   ierr = (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
1230a5ff213dSBarry Smith   /* receive remote parts */
1231ca9f406cSSatish Balay   ierr = VecScatterEnd(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
12323a40ed3dSBarry Smith   PetscFunctionReturn(0);
1233da3a660dSBarry Smith }
1234da3a660dSBarry Smith 
12351eb62cbbSBarry Smith /*
12361eb62cbbSBarry Smith   This only works correctly for square matrices where the subblock A->A is the
12371eb62cbbSBarry Smith    diagonal block
12381eb62cbbSBarry Smith */
12394a2ae208SSatish Balay #undef __FUNCT__
12404a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_MPIAIJ"
1241dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_MPIAIJ(Mat A,Vec v)
12421eb62cbbSBarry Smith {
1243dfbe8321SBarry Smith   PetscErrorCode ierr;
1244416022c9SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
12453a40ed3dSBarry Smith 
12463a40ed3dSBarry Smith   PetscFunctionBegin;
1247e7e72b3dSBarry Smith   if (A->rmap->N != A->cmap->N) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block");
1248e7e72b3dSBarry Smith   if (A->rmap->rstart != A->cmap->rstart || A->rmap->rend != A->cmap->rend) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"row partition must equal col partition");
12493a40ed3dSBarry Smith   ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr);
12503a40ed3dSBarry Smith   PetscFunctionReturn(0);
12511eb62cbbSBarry Smith }
12521eb62cbbSBarry Smith 
12534a2ae208SSatish Balay #undef __FUNCT__
12544a2ae208SSatish Balay #define __FUNCT__ "MatScale_MPIAIJ"
1255f4df32b1SMatthew Knepley PetscErrorCode MatScale_MPIAIJ(Mat A,PetscScalar aa)
1256052efed2SBarry Smith {
1257052efed2SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
1258dfbe8321SBarry Smith   PetscErrorCode ierr;
12593a40ed3dSBarry Smith 
12603a40ed3dSBarry Smith   PetscFunctionBegin;
1261f4df32b1SMatthew Knepley   ierr = MatScale(a->A,aa);CHKERRQ(ierr);
1262f4df32b1SMatthew Knepley   ierr = MatScale(a->B,aa);CHKERRQ(ierr);
12633a40ed3dSBarry Smith   PetscFunctionReturn(0);
1264052efed2SBarry Smith }
1265052efed2SBarry Smith 
12664a2ae208SSatish Balay #undef __FUNCT__
12674a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_MPIAIJ"
1268dfbe8321SBarry Smith PetscErrorCode MatDestroy_MPIAIJ(Mat mat)
12691eb62cbbSBarry Smith {
127044a69424SLois Curfman McInnes   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
1271dfbe8321SBarry Smith   PetscErrorCode ierr;
127283e2fdc7SBarry Smith 
12733a40ed3dSBarry Smith   PetscFunctionBegin;
1274aa482453SBarry Smith #if defined(PETSC_USE_LOG)
1275d0f46423SBarry Smith   PetscLogObjectState((PetscObject)mat,"Rows=%D, Cols=%D",mat->rmap->N,mat->cmap->N);
1276a5a9c739SBarry Smith #endif
12778798bf22SSatish Balay   ierr = MatStashDestroy_Private(&mat->stash);CHKERRQ(ierr);
12786bf464f9SBarry Smith   ierr = VecDestroy(&aij->diag);CHKERRQ(ierr);
12796bf464f9SBarry Smith   ierr = MatDestroy(&aij->A);CHKERRQ(ierr);
12806bf464f9SBarry Smith   ierr = MatDestroy(&aij->B);CHKERRQ(ierr);
1281aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
12826bc0bbbfSBarry Smith   ierr = PetscTableDestroy(&aij->colmap);CHKERRQ(ierr);
1283b1fc9764SSatish Balay #else
128405b42c5fSBarry Smith   ierr = PetscFree(aij->colmap);CHKERRQ(ierr);
1285b1fc9764SSatish Balay #endif
128605b42c5fSBarry Smith   ierr = PetscFree(aij->garray);CHKERRQ(ierr);
12876bf464f9SBarry Smith   ierr = VecDestroy(&aij->lvec);CHKERRQ(ierr);
12886bf464f9SBarry Smith   ierr = VecScatterDestroy(&aij->Mvctx);CHKERRQ(ierr);
128903095fedSBarry Smith   ierr = PetscFree2(aij->rowvalues,aij->rowindices);CHKERRQ(ierr);
12908aa348c1SBarry Smith   ierr = PetscFree(aij->ld);CHKERRQ(ierr);
1291bf0cc555SLisandro Dalcin   ierr = PetscFree(mat->data);CHKERRQ(ierr);
1292901853e0SKris Buschelman 
1293dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)mat,0);CHKERRQ(ierr);
1294901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
1295901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
1296901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatGetDiagonalBlock_C","",PETSC_NULL);CHKERRQ(ierr);
1297901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr);
1298901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
1299ff69c46cSKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
1300901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatDiagonalScaleLocal_C","",PETSC_NULL);CHKERRQ(ierr);
1301471cc821SHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_mpisbaij_C","",PETSC_NULL);CHKERRQ(ierr);
13023a40ed3dSBarry Smith   PetscFunctionReturn(0);
13031eb62cbbSBarry Smith }
1304ee50ffe9SBarry Smith 
13054a2ae208SSatish Balay #undef __FUNCT__
13068e2fed03SBarry Smith #define __FUNCT__ "MatView_MPIAIJ_Binary"
1307dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ_Binary(Mat mat,PetscViewer viewer)
13088e2fed03SBarry Smith {
13098e2fed03SBarry Smith   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*)mat->data;
13108e2fed03SBarry Smith   Mat_SeqAIJ*       A = (Mat_SeqAIJ*)aij->A->data;
13118e2fed03SBarry Smith   Mat_SeqAIJ*       B = (Mat_SeqAIJ*)aij->B->data;
13126849ba73SBarry Smith   PetscErrorCode    ierr;
131332dcc486SBarry Smith   PetscMPIInt       rank,size,tag = ((PetscObject)viewer)->tag;
13146f69ff64SBarry Smith   int               fd;
1315a788621eSSatish Balay   PetscInt          nz,header[4],*row_lengths,*range=0,rlen,i;
1316d0f46423SBarry Smith   PetscInt          nzmax,*column_indices,j,k,col,*garray = aij->garray,cnt,cstart = mat->cmap->rstart,rnz;
13178e2fed03SBarry Smith   PetscScalar       *column_values;
131885ebf7a4SBarry Smith   PetscInt          message_count,flowcontrolcount;
13198e2fed03SBarry Smith 
13208e2fed03SBarry Smith   PetscFunctionBegin;
13217adad957SLisandro Dalcin   ierr = MPI_Comm_rank(((PetscObject)mat)->comm,&rank);CHKERRQ(ierr);
13227adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr);
13238e2fed03SBarry Smith   nz   = A->nz + B->nz;
1324958c9bccSBarry Smith   if (!rank) {
13250700a824SBarry Smith     header[0] = MAT_FILE_CLASSID;
1326d0f46423SBarry Smith     header[1] = mat->rmap->N;
1327d0f46423SBarry Smith     header[2] = mat->cmap->N;
13287adad957SLisandro Dalcin     ierr = MPI_Reduce(&nz,&header[3],1,MPIU_INT,MPI_SUM,0,((PetscObject)mat)->comm);CHKERRQ(ierr);
13298e2fed03SBarry Smith     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
13306f69ff64SBarry Smith     ierr = PetscBinaryWrite(fd,header,4,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
13318e2fed03SBarry Smith     /* get largest number of rows any processor has */
1332d0f46423SBarry Smith     rlen = mat->rmap->n;
1333d0f46423SBarry Smith     range = mat->rmap->range;
13348e2fed03SBarry Smith     for (i=1; i<size; i++) {
13358e2fed03SBarry Smith       rlen = PetscMax(rlen,range[i+1] - range[i]);
13368e2fed03SBarry Smith     }
13378e2fed03SBarry Smith   } else {
13387adad957SLisandro Dalcin     ierr = MPI_Reduce(&nz,0,1,MPIU_INT,MPI_SUM,0,((PetscObject)mat)->comm);CHKERRQ(ierr);
1339d0f46423SBarry Smith     rlen = mat->rmap->n;
13408e2fed03SBarry Smith   }
13418e2fed03SBarry Smith 
13428e2fed03SBarry Smith   /* load up the local row counts */
1343b1d57f15SBarry Smith   ierr = PetscMalloc((rlen+1)*sizeof(PetscInt),&row_lengths);CHKERRQ(ierr);
1344d0f46423SBarry Smith   for (i=0; i<mat->rmap->n; i++) {
13458e2fed03SBarry Smith     row_lengths[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i];
13468e2fed03SBarry Smith   }
13478e2fed03SBarry Smith 
13488e2fed03SBarry Smith   /* store the row lengths to the file */
134985ebf7a4SBarry Smith   ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr);
1350958c9bccSBarry Smith   if (!rank) {
1351d0f46423SBarry Smith     ierr = PetscBinaryWrite(fd,row_lengths,mat->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
13528e2fed03SBarry Smith     for (i=1; i<size; i++) {
135385ebf7a4SBarry Smith       ierr = PetscViewerFlowControlStepMaster(viewer,i,message_count,flowcontrolcount);CHKERRQ(ierr);
13548e2fed03SBarry Smith       rlen = range[i+1] - range[i];
1355a25532f0SBarry Smith       ierr = MPIULong_Recv(row_lengths,rlen,MPIU_INT,i,tag,((PetscObject)mat)->comm);CHKERRQ(ierr);
13566f69ff64SBarry Smith       ierr = PetscBinaryWrite(fd,row_lengths,rlen,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
13578e2fed03SBarry Smith     }
135885ebf7a4SBarry Smith     ierr = PetscViewerFlowControlEndMaster(viewer,message_count);CHKERRQ(ierr);
13598e2fed03SBarry Smith   } else {
136085ebf7a4SBarry Smith     ierr = PetscViewerFlowControlStepWorker(viewer,rank,message_count);CHKERRQ(ierr);
1361a25532f0SBarry Smith     ierr = MPIULong_Send(row_lengths,mat->rmap->n,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr);
136285ebf7a4SBarry Smith     ierr = PetscViewerFlowControlEndWorker(viewer,message_count);CHKERRQ(ierr);
13638e2fed03SBarry Smith   }
13648e2fed03SBarry Smith   ierr = PetscFree(row_lengths);CHKERRQ(ierr);
13658e2fed03SBarry Smith 
13668e2fed03SBarry Smith   /* load up the local column indices */
13678e2fed03SBarry Smith   nzmax = nz; /* )th processor needs space a largest processor needs */
13687adad957SLisandro Dalcin   ierr = MPI_Reduce(&nz,&nzmax,1,MPIU_INT,MPI_MAX,0,((PetscObject)mat)->comm);CHKERRQ(ierr);
1369b1d57f15SBarry Smith   ierr = PetscMalloc((nzmax+1)*sizeof(PetscInt),&column_indices);CHKERRQ(ierr);
13708e2fed03SBarry Smith   cnt  = 0;
1371d0f46423SBarry Smith   for (i=0; i<mat->rmap->n; i++) {
13728e2fed03SBarry Smith     for (j=B->i[i]; j<B->i[i+1]; j++) {
13738e2fed03SBarry Smith       if ( (col = garray[B->j[j]]) > cstart) break;
13748e2fed03SBarry Smith       column_indices[cnt++] = col;
13758e2fed03SBarry Smith     }
13768e2fed03SBarry Smith     for (k=A->i[i]; k<A->i[i+1]; k++) {
13778e2fed03SBarry Smith       column_indices[cnt++] = A->j[k] + cstart;
13788e2fed03SBarry Smith     }
13798e2fed03SBarry Smith     for (; j<B->i[i+1]; j++) {
13808e2fed03SBarry Smith       column_indices[cnt++] = garray[B->j[j]];
13818e2fed03SBarry Smith     }
13828e2fed03SBarry Smith   }
1383e32f2f54SBarry Smith   if (cnt != A->nz + B->nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: cnt = %D nz = %D",cnt,A->nz+B->nz);
13848e2fed03SBarry Smith 
13858e2fed03SBarry Smith   /* store the column indices to the file */
138685ebf7a4SBarry Smith    ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr);
1387958c9bccSBarry Smith   if (!rank) {
13888e2fed03SBarry Smith     MPI_Status status;
13896f69ff64SBarry Smith     ierr = PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
13908e2fed03SBarry Smith     for (i=1; i<size; i++) {
139185ebf7a4SBarry Smith       ierr = PetscViewerFlowControlStepMaster(viewer,i,message_count,flowcontrolcount);CHKERRQ(ierr);
13927adad957SLisandro Dalcin       ierr = MPI_Recv(&rnz,1,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr);
1393e32f2f54SBarry Smith       if (rnz > nzmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax);
1394a25532f0SBarry Smith       ierr = MPIULong_Recv(column_indices,rnz,MPIU_INT,i,tag,((PetscObject)mat)->comm);CHKERRQ(ierr);
13956f69ff64SBarry Smith       ierr = PetscBinaryWrite(fd,column_indices,rnz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
13968e2fed03SBarry Smith     }
139785ebf7a4SBarry Smith      ierr = PetscViewerFlowControlEndMaster(viewer,message_count);CHKERRQ(ierr);
13988e2fed03SBarry Smith   } else {
139985ebf7a4SBarry Smith     ierr = PetscViewerFlowControlStepWorker(viewer,rank,message_count);CHKERRQ(ierr);
14007adad957SLisandro Dalcin     ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr);
1401a25532f0SBarry Smith     ierr = MPIULong_Send(column_indices,nz,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr);
140285ebf7a4SBarry Smith     ierr = PetscViewerFlowControlEndWorker(viewer,message_count);CHKERRQ(ierr);
14038e2fed03SBarry Smith   }
14048e2fed03SBarry Smith   ierr = PetscFree(column_indices);CHKERRQ(ierr);
14058e2fed03SBarry Smith 
14068e2fed03SBarry Smith   /* load up the local column values */
14078e2fed03SBarry Smith   ierr = PetscMalloc((nzmax+1)*sizeof(PetscScalar),&column_values);CHKERRQ(ierr);
14088e2fed03SBarry Smith   cnt  = 0;
1409d0f46423SBarry Smith   for (i=0; i<mat->rmap->n; i++) {
14108e2fed03SBarry Smith     for (j=B->i[i]; j<B->i[i+1]; j++) {
14118e2fed03SBarry Smith       if ( garray[B->j[j]] > cstart) break;
14128e2fed03SBarry Smith       column_values[cnt++] = B->a[j];
14138e2fed03SBarry Smith     }
14148e2fed03SBarry Smith     for (k=A->i[i]; k<A->i[i+1]; k++) {
14158e2fed03SBarry Smith       column_values[cnt++] = A->a[k];
14168e2fed03SBarry Smith     }
14178e2fed03SBarry Smith     for (; j<B->i[i+1]; j++) {
14188e2fed03SBarry Smith       column_values[cnt++] = B->a[j];
14198e2fed03SBarry Smith     }
14208e2fed03SBarry Smith   }
1421e32f2f54SBarry Smith   if (cnt != A->nz + B->nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal PETSc error: cnt = %D nz = %D",cnt,A->nz+B->nz);
14228e2fed03SBarry Smith 
14238e2fed03SBarry Smith   /* store the column values to the file */
142485ebf7a4SBarry Smith    ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr);
1425958c9bccSBarry Smith   if (!rank) {
14268e2fed03SBarry Smith     MPI_Status status;
14276f69ff64SBarry Smith     ierr = PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr);
14288e2fed03SBarry Smith     for (i=1; i<size; i++) {
142985ebf7a4SBarry Smith        ierr = PetscViewerFlowControlStepMaster(viewer,i,message_count,flowcontrolcount);CHKERRQ(ierr);
14307adad957SLisandro Dalcin       ierr = MPI_Recv(&rnz,1,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr);
1431e32f2f54SBarry Smith       if (rnz > nzmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax);
1432a25532f0SBarry Smith       ierr = MPIULong_Recv(column_values,rnz,MPIU_SCALAR,i,tag,((PetscObject)mat)->comm);CHKERRQ(ierr);
14336f69ff64SBarry Smith       ierr = PetscBinaryWrite(fd,column_values,rnz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr);
14348e2fed03SBarry Smith     }
143585ebf7a4SBarry Smith     ierr = PetscViewerFlowControlEndMaster(viewer,message_count);CHKERRQ(ierr);
14368e2fed03SBarry Smith   } else {
143785ebf7a4SBarry Smith     ierr = PetscViewerFlowControlStepWorker(viewer,rank,message_count);CHKERRQ(ierr);
14387adad957SLisandro Dalcin     ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr);
1439a25532f0SBarry Smith     ierr = MPIULong_Send(column_values,nz,MPIU_SCALAR,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr);
144085ebf7a4SBarry Smith     ierr = PetscViewerFlowControlEndWorker(viewer,message_count);CHKERRQ(ierr);
14418e2fed03SBarry Smith   }
14428e2fed03SBarry Smith   ierr = PetscFree(column_values);CHKERRQ(ierr);
14438e2fed03SBarry Smith   PetscFunctionReturn(0);
14448e2fed03SBarry Smith }
14458e2fed03SBarry Smith 
14468e2fed03SBarry Smith #undef __FUNCT__
14474a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ_ASCIIorDraworSocket"
1448dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
1449416022c9SBarry Smith {
145044a69424SLois Curfman McInnes   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*)mat->data;
1451dfbe8321SBarry Smith   PetscErrorCode    ierr;
145232dcc486SBarry Smith   PetscMPIInt       rank = aij->rank,size = aij->size;
1453ace3abfcSBarry Smith   PetscBool         isdraw,iascii,isbinary;
1454b0a32e0cSBarry Smith   PetscViewer       sviewer;
1455f3ef73ceSBarry Smith   PetscViewerFormat format;
1456416022c9SBarry Smith 
14573a40ed3dSBarry Smith   PetscFunctionBegin;
1458251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1459251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1460251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
146132077d6dSBarry Smith   if (iascii) {
1462b0a32e0cSBarry Smith     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
1463456192e2SBarry Smith     if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
14644e220ebcSLois Curfman McInnes       MatInfo    info;
1465ace3abfcSBarry Smith       PetscBool  inodes;
1466923f20ffSKris Buschelman 
14677adad957SLisandro Dalcin       ierr = MPI_Comm_rank(((PetscObject)mat)->comm,&rank);CHKERRQ(ierr);
1468888f2ed8SSatish Balay       ierr = MatGetInfo(mat,MAT_LOCAL,&info);CHKERRQ(ierr);
1469923f20ffSKris Buschelman       ierr = MatInodeGetInodeSizes(aij->A,PETSC_NULL,(PetscInt **)&inodes,PETSC_NULL);CHKERRQ(ierr);
14707b23a99aSBarry Smith       ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_TRUE);CHKERRQ(ierr);
1471923f20ffSKris Buschelman       if (!inodes) {
147277431f27SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, not using I-node routines\n",
1473d0f46423SBarry Smith 					      rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);CHKERRQ(ierr);
14746831982aSBarry Smith       } else {
147577431f27SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, using I-node routines\n",
1476d0f46423SBarry Smith 		    rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);CHKERRQ(ierr);
14776831982aSBarry Smith       }
1478888f2ed8SSatish Balay       ierr = MatGetInfo(aij->A,MAT_LOCAL,&info);CHKERRQ(ierr);
147977431f27SBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr);
1480888f2ed8SSatish Balay       ierr = MatGetInfo(aij->B,MAT_LOCAL,&info);CHKERRQ(ierr);
148177431f27SBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr);
1482b0a32e0cSBarry Smith       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
14837b23a99aSBarry Smith       ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_FALSE);CHKERRQ(ierr);
148407d81ca4SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Information on VecScatter used in matrix-vector product: \n");CHKERRQ(ierr);
1485a40aa06bSLois Curfman McInnes       ierr = VecScatterView(aij->Mvctx,viewer);CHKERRQ(ierr);
14863a40ed3dSBarry Smith       PetscFunctionReturn(0);
1487fb9695e5SSatish Balay     } else if (format == PETSC_VIEWER_ASCII_INFO) {
1488923f20ffSKris Buschelman       PetscInt   inodecount,inodelimit,*inodes;
1489923f20ffSKris Buschelman       ierr = MatInodeGetInodeSizes(aij->A,&inodecount,&inodes,&inodelimit);CHKERRQ(ierr);
1490923f20ffSKris Buschelman       if (inodes) {
1491923f20ffSKris Buschelman         ierr = PetscViewerASCIIPrintf(viewer,"using I-node (on process 0) routines: found %D nodes, limit used is %D\n",inodecount,inodelimit);CHKERRQ(ierr);
1492d38fa0fbSBarry Smith       } else {
1493d38fa0fbSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"not using I-node (on process 0) routines\n");CHKERRQ(ierr);
1494d38fa0fbSBarry Smith       }
14953a40ed3dSBarry Smith       PetscFunctionReturn(0);
14964aedb280SBarry Smith     } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
14974aedb280SBarry Smith       PetscFunctionReturn(0);
149808480c60SBarry Smith     }
14998e2fed03SBarry Smith   } else if (isbinary) {
15008e2fed03SBarry Smith     if (size == 1) {
15017adad957SLisandro Dalcin       ierr = PetscObjectSetName((PetscObject)aij->A,((PetscObject)mat)->name);CHKERRQ(ierr);
15028e2fed03SBarry Smith       ierr = MatView(aij->A,viewer);CHKERRQ(ierr);
15038e2fed03SBarry Smith     } else {
15048e2fed03SBarry Smith       ierr = MatView_MPIAIJ_Binary(mat,viewer);CHKERRQ(ierr);
15058e2fed03SBarry Smith     }
15068e2fed03SBarry Smith     PetscFunctionReturn(0);
15070f5bd95cSBarry Smith   } else if (isdraw) {
1508b0a32e0cSBarry Smith     PetscDraw  draw;
1509ace3abfcSBarry Smith     PetscBool  isnull;
1510b0a32e0cSBarry Smith     ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
1511b0a32e0cSBarry Smith     ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
151219bcc07fSBarry Smith   }
151319bcc07fSBarry Smith 
151417699dbbSLois Curfman McInnes   if (size == 1) {
15157adad957SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)aij->A,((PetscObject)mat)->name);CHKERRQ(ierr);
151678b31e54SBarry Smith     ierr = MatView(aij->A,viewer);CHKERRQ(ierr);
15173a40ed3dSBarry Smith   } else {
151895373324SBarry Smith     /* assemble the entire matrix onto first processor. */
151995373324SBarry Smith     Mat         A;
1520ec8511deSBarry Smith     Mat_SeqAIJ  *Aloc;
1521d0f46423SBarry Smith     PetscInt    M = mat->rmap->N,N = mat->cmap->N,m,*ai,*aj,row,*cols,i,*ct;
1522dd6ea824SBarry Smith     MatScalar   *a;
15232ee70a88SLois Curfman McInnes 
152432a366e4SMatthew Knepley     if (mat->rmap->N > 1024) {
1525ace3abfcSBarry Smith       PetscBool  flg = PETSC_FALSE;
152632a366e4SMatthew Knepley 
1527acfcf0e5SJed Brown       ierr = PetscOptionsGetBool(((PetscObject) mat)->prefix, "-mat_ascii_output_large", &flg,PETSC_NULL);CHKERRQ(ierr);
152832a366e4SMatthew Knepley       if (!flg) {
1529e7e72b3dSBarry Smith         SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_OUTOFRANGE,"ASCII matrix output not allowed for matrices with more than 1024 rows, use binary format instead.\nYou can override this restriction using -mat_ascii_output_large.");
153032a366e4SMatthew Knepley       }
153132a366e4SMatthew Knepley     }
15320805154bSBarry Smith 
15337adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)mat)->comm,&A);CHKERRQ(ierr);
153417699dbbSLois Curfman McInnes     if (!rank) {
1535f69a0ea3SMatthew Knepley       ierr = MatSetSizes(A,M,N,M,N);CHKERRQ(ierr);
15363a40ed3dSBarry Smith     } else {
1537f69a0ea3SMatthew Knepley       ierr = MatSetSizes(A,0,0,M,N);CHKERRQ(ierr);
153895373324SBarry Smith     }
1539f204ca49SKris Buschelman     /* This is just a temporary matrix, so explicitly using MATMPIAIJ is probably best */
1540f204ca49SKris Buschelman     ierr = MatSetType(A,MATMPIAIJ);CHKERRQ(ierr);
1541f204ca49SKris Buschelman     ierr = MatMPIAIJSetPreallocation(A,0,PETSC_NULL,0,PETSC_NULL);CHKERRQ(ierr);
15422b82e772SSatish Balay     ierr = MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr);
154352e6d16bSBarry Smith     ierr = PetscLogObjectParent(mat,A);CHKERRQ(ierr);
1544416022c9SBarry Smith 
154595373324SBarry Smith     /* copy over the A part */
1546ec8511deSBarry Smith     Aloc = (Mat_SeqAIJ*)aij->A->data;
1547d0f46423SBarry Smith     m = aij->A->rmap->n; ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
1548d0f46423SBarry Smith     row = mat->rmap->rstart;
1549d0f46423SBarry Smith     for (i=0; i<ai[m]; i++) {aj[i] += mat->cmap->rstart ;}
155095373324SBarry Smith     for (i=0; i<m; i++) {
1551416022c9SBarry Smith       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);CHKERRQ(ierr);
155295373324SBarry Smith       row++; a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
155395373324SBarry Smith     }
15542ee70a88SLois Curfman McInnes     aj = Aloc->j;
1555d0f46423SBarry Smith     for (i=0; i<ai[m]; i++) {aj[i] -= mat->cmap->rstart;}
155695373324SBarry Smith 
155795373324SBarry Smith     /* copy over the B part */
1558ec8511deSBarry Smith     Aloc = (Mat_SeqAIJ*)aij->B->data;
1559d0f46423SBarry Smith     m    = aij->B->rmap->n;  ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
1560d0f46423SBarry Smith     row  = mat->rmap->rstart;
1561b1d57f15SBarry Smith     ierr = PetscMalloc((ai[m]+1)*sizeof(PetscInt),&cols);CHKERRQ(ierr);
1562b0a32e0cSBarry Smith     ct   = cols;
1563bfec09a0SHong Zhang     for (i=0; i<ai[m]; i++) {cols[i] = aij->garray[aj[i]];}
156495373324SBarry Smith     for (i=0; i<m; i++) {
1565416022c9SBarry Smith       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);CHKERRQ(ierr);
156695373324SBarry Smith       row++; a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
156795373324SBarry Smith     }
1568606d414cSSatish Balay     ierr = PetscFree(ct);CHKERRQ(ierr);
15696d4a8577SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
15706d4a8577SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
157155843e3eSBarry Smith     /*
157255843e3eSBarry Smith        Everyone has to call to draw the matrix since the graphics waits are
1573b0a32e0cSBarry Smith        synchronized across all processors that share the PetscDraw object
157455843e3eSBarry Smith     */
1575b0a32e0cSBarry Smith     ierr = PetscViewerGetSingleton(viewer,&sviewer);CHKERRQ(ierr);
1576e03a110bSBarry Smith     if (!rank) {
15777adad957SLisandro Dalcin       ierr = PetscObjectSetName((PetscObject)((Mat_MPIAIJ*)(A->data))->A,((PetscObject)mat)->name);CHKERRQ(ierr);
15787566de4bSShri Abhyankar       /* Set the type name to MATMPIAIJ so that the correct type can be printed out by PetscObjectPrintClassNamePrefixType() in MatView_SeqAIJ_ASCII()*/
15797566de4bSShri Abhyankar       PetscStrcpy(((PetscObject)((Mat_MPIAIJ*)(A->data))->A)->type_name,MATMPIAIJ);
15806831982aSBarry Smith       ierr = MatView(((Mat_MPIAIJ*)(A->data))->A,sviewer);CHKERRQ(ierr);
158195373324SBarry Smith     }
1582b0a32e0cSBarry Smith     ierr = PetscViewerRestoreSingleton(viewer,&sviewer);CHKERRQ(ierr);
15836bf464f9SBarry Smith     ierr = MatDestroy(&A);CHKERRQ(ierr);
158495373324SBarry Smith   }
15853a40ed3dSBarry Smith   PetscFunctionReturn(0);
15861eb62cbbSBarry Smith }
15871eb62cbbSBarry Smith 
15884a2ae208SSatish Balay #undef __FUNCT__
15894a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ"
1590dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ(Mat mat,PetscViewer viewer)
1591416022c9SBarry Smith {
1592dfbe8321SBarry Smith   PetscErrorCode ierr;
1593ace3abfcSBarry Smith   PetscBool      iascii,isdraw,issocket,isbinary;
1594416022c9SBarry Smith 
15953a40ed3dSBarry Smith   PetscFunctionBegin;
1596251f4c67SDmitry Karpeev   ierr  = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1597251f4c67SDmitry Karpeev   ierr  = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1598251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
1599251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSOCKET,&issocket);CHKERRQ(ierr);
160032077d6dSBarry Smith   if (iascii || isdraw || isbinary || issocket) {
16017b2a1423SBarry Smith     ierr = MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer);CHKERRQ(ierr);
16025cd90555SBarry Smith   } else {
1603e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported by MPIAIJ matrices",((PetscObject)viewer)->type_name);
1604416022c9SBarry Smith   }
16053a40ed3dSBarry Smith   PetscFunctionReturn(0);
1606416022c9SBarry Smith }
1607416022c9SBarry Smith 
16084a2ae208SSatish Balay #undef __FUNCT__
160941f059aeSBarry Smith #define __FUNCT__ "MatSOR_MPIAIJ"
161041f059aeSBarry Smith PetscErrorCode MatSOR_MPIAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
16118a729477SBarry Smith {
161244a69424SLois Curfman McInnes   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)matin->data;
1613dfbe8321SBarry Smith   PetscErrorCode ierr;
16146987fefcSBarry Smith   Vec            bb1 = 0;
1615ace3abfcSBarry Smith   PetscBool      hasop;
16168a729477SBarry Smith 
16173a40ed3dSBarry Smith   PetscFunctionBegin;
1618a2b30743SBarry Smith   if (flag == SOR_APPLY_UPPER) {
161941f059aeSBarry Smith     ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
1620a2b30743SBarry Smith     PetscFunctionReturn(0);
1621a2b30743SBarry Smith   }
1622a2b30743SBarry Smith 
1623*4e980039SJed Brown   if (its > 1 || ~flag & SOR_ZERO_INITIAL_GUESS || flag & SOR_EISENSTAT) {
1624*4e980039SJed Brown     ierr = VecDuplicate(bb,&bb1);CHKERRQ(ierr);
1625*4e980039SJed Brown   }
1626*4e980039SJed Brown 
1627c16cb8f2SBarry Smith   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){
1628da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
162941f059aeSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
16302798e883SHong Zhang       its--;
1631da3a660dSBarry Smith     }
16322798e883SHong Zhang 
16332798e883SHong Zhang     while (its--) {
1634ca9f406cSSatish Balay       ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1635ca9f406cSSatish Balay       ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
16362798e883SHong Zhang 
1637c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
1638efb30889SBarry Smith       ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr);
1639c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
16402798e883SHong Zhang 
1641c14dc6b6SHong Zhang       /* local sweep */
164241f059aeSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr);
16432798e883SHong Zhang     }
16443a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_FORWARD_SWEEP){
1645da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
164641f059aeSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
16472798e883SHong Zhang       its--;
1648da3a660dSBarry Smith     }
16492798e883SHong Zhang     while (its--) {
1650ca9f406cSSatish Balay       ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1651ca9f406cSSatish Balay       ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
16522798e883SHong Zhang 
1653c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
1654efb30889SBarry Smith       ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr);
1655c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
1656c14dc6b6SHong Zhang 
1657c14dc6b6SHong Zhang       /* local sweep */
165841f059aeSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr);
16592798e883SHong Zhang     }
16603a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_BACKWARD_SWEEP){
1661da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
166241f059aeSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
16632798e883SHong Zhang       its--;
1664da3a660dSBarry Smith     }
16652798e883SHong Zhang     while (its--) {
1666ca9f406cSSatish Balay       ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1667ca9f406cSSatish Balay       ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
16682798e883SHong Zhang 
1669c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
1670efb30889SBarry Smith       ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr);
1671c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
16722798e883SHong Zhang 
1673c14dc6b6SHong Zhang       /* local sweep */
167441f059aeSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr);
16752798e883SHong Zhang     }
1676a7420bb7SBarry Smith   }  else if (flag & SOR_EISENSTAT) {
1677a7420bb7SBarry Smith     Vec         xx1;
1678a7420bb7SBarry Smith 
1679a7420bb7SBarry Smith     ierr = VecDuplicate(bb,&xx1);CHKERRQ(ierr);
168041f059aeSBarry Smith     ierr = (*mat->A->ops->sor)(mat->A,bb,omega,(MatSORType)(SOR_ZERO_INITIAL_GUESS | SOR_LOCAL_BACKWARD_SWEEP),fshift,lits,1,xx);CHKERRQ(ierr);
1681a7420bb7SBarry Smith 
1682a7420bb7SBarry Smith     ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1683a7420bb7SBarry Smith     ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1684a7420bb7SBarry Smith     if (!mat->diag) {
1685a7420bb7SBarry Smith       ierr = MatGetVecs(matin,&mat->diag,PETSC_NULL);CHKERRQ(ierr);
1686a7420bb7SBarry Smith       ierr = MatGetDiagonal(matin,mat->diag);CHKERRQ(ierr);
1687a7420bb7SBarry Smith     }
1688bd0c2dcbSBarry Smith     ierr = MatHasOperation(matin,MATOP_MULT_DIAGONAL_BLOCK,&hasop);CHKERRQ(ierr);
1689bd0c2dcbSBarry Smith     if (hasop) {
1690bd0c2dcbSBarry Smith       ierr = MatMultDiagonalBlock(matin,xx,bb1);CHKERRQ(ierr);
1691bd0c2dcbSBarry Smith     } else {
1692a7420bb7SBarry Smith       ierr = VecPointwiseMult(bb1,mat->diag,xx);CHKERRQ(ierr);
1693bd0c2dcbSBarry Smith     }
1694887ee2caSBarry Smith     ierr = VecAYPX(bb1,(omega-2.0)/omega,bb);CHKERRQ(ierr);
1695887ee2caSBarry Smith 
1696a7420bb7SBarry Smith     ierr = MatMultAdd(mat->B,mat->lvec,bb1,bb1);CHKERRQ(ierr);
1697a7420bb7SBarry Smith 
1698a7420bb7SBarry Smith     /* local sweep */
169941f059aeSBarry Smith     ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,(MatSORType)(SOR_ZERO_INITIAL_GUESS | SOR_LOCAL_FORWARD_SWEEP),fshift,lits,1,xx1);CHKERRQ(ierr);
1700a7420bb7SBarry Smith     ierr = VecAXPY(xx,1.0,xx1);CHKERRQ(ierr);
17016bf464f9SBarry Smith     ierr = VecDestroy(&xx1);CHKERRQ(ierr);
170244b1af1bSBarry Smith   } else SETERRQ(((PetscObject)matin)->comm,PETSC_ERR_SUP,"Parallel SOR not supported");
1703c14dc6b6SHong Zhang 
17046bf464f9SBarry Smith   ierr = VecDestroy(&bb1);CHKERRQ(ierr);
17053a40ed3dSBarry Smith   PetscFunctionReturn(0);
17068a729477SBarry Smith }
1707a66be287SLois Curfman McInnes 
17084a2ae208SSatish Balay #undef __FUNCT__
170942e855d1Svictor #define __FUNCT__ "MatPermute_MPIAIJ"
171042e855d1Svictor PetscErrorCode MatPermute_MPIAIJ(Mat A,IS rowp,IS colp,Mat *B)
171142e855d1Svictor {
171223f569faSJed Brown   MPI_Comm       comm;
171323f569faSJed Brown   PetscInt       first,local_rowsize,local_colsize;
17145d0c19d7SBarry Smith   const PetscInt *rows;
171542e855d1Svictor   IS             crowp,growp,irowp,lrowp,lcolp,icolp;
171642e855d1Svictor   PetscErrorCode ierr;
171742e855d1Svictor 
171842e855d1Svictor   PetscFunctionBegin;
171942e855d1Svictor   ierr = PetscObjectGetComm((PetscObject)A,&comm);CHKERRQ(ierr);
172023f569faSJed Brown   /* make a collective version of 'rowp', this is to be tolerant of users who pass serial index sets */
172123f569faSJed Brown   ierr = ISOnComm(rowp,comm,PETSC_USE_POINTER,&crowp);CHKERRQ(ierr);
172242e855d1Svictor   /* collect the global row permutation and invert it */
172342e855d1Svictor   ierr = ISAllGather(crowp,&growp);CHKERRQ(ierr);
172442e855d1Svictor   ierr = ISSetPermutation(growp);CHKERRQ(ierr);
17256bf464f9SBarry Smith   ierr = ISDestroy(&crowp);CHKERRQ(ierr);
172642e855d1Svictor   ierr = ISInvertPermutation(growp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
172723f569faSJed Brown   ierr = ISDestroy(&growp);CHKERRQ(ierr);
172842e855d1Svictor   /* get the local target indices */
172942e855d1Svictor   ierr = MatGetOwnershipRange(A,&first,PETSC_NULL);CHKERRQ(ierr);
173023f569faSJed Brown   ierr = MatGetLocalSize(A,&local_rowsize,&local_colsize);CHKERRQ(ierr);
173142e855d1Svictor   ierr = ISGetIndices(irowp,&rows);CHKERRQ(ierr);
173223f569faSJed Brown   ierr = ISCreateGeneral(PETSC_COMM_SELF,local_rowsize,rows+first,PETSC_COPY_VALUES,&lrowp);CHKERRQ(ierr);
173342e855d1Svictor   ierr = ISRestoreIndices(irowp,&rows);CHKERRQ(ierr);
17346bf464f9SBarry Smith   ierr = ISDestroy(&irowp);CHKERRQ(ierr);
173542e855d1Svictor   /* the column permutation is so much easier;
173642e855d1Svictor      make a local version of 'colp' and invert it */
173723f569faSJed Brown   ierr = ISOnComm(colp,PETSC_COMM_SELF,PETSC_USE_POINTER,&lcolp);CHKERRQ(ierr);
1738dbf0e21dSBarry Smith   ierr = ISSetPermutation(lcolp);CHKERRQ(ierr);
173942e855d1Svictor   ierr = ISInvertPermutation(lcolp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
17406bf464f9SBarry Smith   ierr = ISDestroy(&lcolp);CHKERRQ(ierr);
174142e855d1Svictor   /* now we just get the submatrix */
174223f569faSJed Brown   ierr = MatGetSubMatrix_MPIAIJ_Private(A,lrowp,icolp,local_colsize,MAT_INITIAL_MATRIX,B);CHKERRQ(ierr);
174342e855d1Svictor   /* clean up */
17446bf464f9SBarry Smith   ierr = ISDestroy(&lrowp);CHKERRQ(ierr);
17456bf464f9SBarry Smith   ierr = ISDestroy(&icolp);CHKERRQ(ierr);
174642e855d1Svictor   PetscFunctionReturn(0);
174742e855d1Svictor }
174842e855d1Svictor 
174942e855d1Svictor #undef __FUNCT__
17504a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_MPIAIJ"
1751dfbe8321SBarry Smith PetscErrorCode MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info)
1752a66be287SLois Curfman McInnes {
1753a66be287SLois Curfman McInnes   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)matin->data;
1754a66be287SLois Curfman McInnes   Mat            A = mat->A,B = mat->B;
1755dfbe8321SBarry Smith   PetscErrorCode ierr;
1756329f5518SBarry Smith   PetscReal      isend[5],irecv[5];
1757a66be287SLois Curfman McInnes 
17583a40ed3dSBarry Smith   PetscFunctionBegin;
17594e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
17604e220ebcSLois Curfman McInnes   ierr = MatGetInfo(A,MAT_LOCAL,info);CHKERRQ(ierr);
17614e220ebcSLois Curfman McInnes   isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
17624e220ebcSLois Curfman McInnes   isend[3] = info->memory;  isend[4] = info->mallocs;
17634e220ebcSLois Curfman McInnes   ierr = MatGetInfo(B,MAT_LOCAL,info);CHKERRQ(ierr);
17644e220ebcSLois Curfman McInnes   isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
17654e220ebcSLois Curfman McInnes   isend[3] += info->memory;  isend[4] += info->mallocs;
1766a66be287SLois Curfman McInnes   if (flag == MAT_LOCAL) {
17674e220ebcSLois Curfman McInnes     info->nz_used      = isend[0];
17684e220ebcSLois Curfman McInnes     info->nz_allocated = isend[1];
17694e220ebcSLois Curfman McInnes     info->nz_unneeded  = isend[2];
17704e220ebcSLois Curfman McInnes     info->memory       = isend[3];
17714e220ebcSLois Curfman McInnes     info->mallocs      = isend[4];
1772a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_MAX) {
1773d9822059SBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_MAX,((PetscObject)matin)->comm);CHKERRQ(ierr);
17744e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
17754e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
17764e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
17774e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
17784e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1779a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_SUM) {
1780d9822059SBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_SUM,((PetscObject)matin)->comm);CHKERRQ(ierr);
17814e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
17824e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
17834e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
17844e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
17854e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1786a66be287SLois Curfman McInnes   }
17874e220ebcSLois Curfman McInnes   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
17884e220ebcSLois Curfman McInnes   info->fill_ratio_needed = 0;
17894e220ebcSLois Curfman McInnes   info->factor_mallocs    = 0;
17904e220ebcSLois Curfman McInnes 
17913a40ed3dSBarry Smith   PetscFunctionReturn(0);
1792a66be287SLois Curfman McInnes }
1793a66be287SLois Curfman McInnes 
17944a2ae208SSatish Balay #undef __FUNCT__
17954a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_MPIAIJ"
1796ace3abfcSBarry Smith PetscErrorCode MatSetOption_MPIAIJ(Mat A,MatOption op,PetscBool  flg)
1797c74985f6SBarry Smith {
1798c0bbcb79SLois Curfman McInnes   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
1799dfbe8321SBarry Smith   PetscErrorCode ierr;
1800c74985f6SBarry Smith 
18013a40ed3dSBarry Smith   PetscFunctionBegin;
180212c028f9SKris Buschelman   switch (op) {
1803512a5fc5SBarry Smith   case MAT_NEW_NONZERO_LOCATIONS:
180412c028f9SKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
180528b2fa4aSMatthew Knepley   case MAT_UNUSED_NONZERO_LOCATION_ERR:
1806a9817697SBarry Smith   case MAT_KEEP_NONZERO_PATTERN:
180712c028f9SKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
180812c028f9SKris Buschelman   case MAT_USE_INODES:
180912c028f9SKris Buschelman   case MAT_IGNORE_ZERO_ENTRIES:
1810fa1f0d2cSMatthew G Knepley     MatCheckPreallocated(A,1);
18114e0d8c25SBarry Smith     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
18124e0d8c25SBarry Smith     ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr);
181312c028f9SKris Buschelman     break;
181412c028f9SKris Buschelman   case MAT_ROW_ORIENTED:
18154e0d8c25SBarry Smith     a->roworiented = flg;
18164e0d8c25SBarry Smith     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
18174e0d8c25SBarry Smith     ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr);
181812c028f9SKris Buschelman     break;
18194e0d8c25SBarry Smith   case MAT_NEW_DIAGONALS:
1820290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
182112c028f9SKris Buschelman     break;
182212c028f9SKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
18235c0f0b64SBarry Smith     a->donotstash = flg;
182412c028f9SKris Buschelman     break;
1825ffa07934SHong Zhang   case MAT_SPD:
1826ffa07934SHong Zhang     A->spd_set                         = PETSC_TRUE;
1827ffa07934SHong Zhang     A->spd                             = flg;
1828ffa07934SHong Zhang     if (flg) {
1829ffa07934SHong Zhang       A->symmetric                     = PETSC_TRUE;
1830ffa07934SHong Zhang       A->structurally_symmetric        = PETSC_TRUE;
1831ffa07934SHong Zhang       A->symmetric_set                 = PETSC_TRUE;
1832ffa07934SHong Zhang       A->structurally_symmetric_set    = PETSC_TRUE;
1833ffa07934SHong Zhang     }
1834ffa07934SHong Zhang     break;
183577e54ba9SKris Buschelman   case MAT_SYMMETRIC:
18364e0d8c25SBarry Smith     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
183725f421beSHong Zhang     break;
183877e54ba9SKris Buschelman   case MAT_STRUCTURALLY_SYMMETRIC:
1839eeffb40dSHong Zhang     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
1840eeffb40dSHong Zhang     break;
1841bf108f30SBarry Smith   case MAT_HERMITIAN:
1842eeffb40dSHong Zhang     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
1843eeffb40dSHong Zhang     break;
1844bf108f30SBarry Smith   case MAT_SYMMETRY_ETERNAL:
18454e0d8c25SBarry Smith     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
184677e54ba9SKris Buschelman     break;
184712c028f9SKris Buschelman   default:
1848e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
18493a40ed3dSBarry Smith   }
18503a40ed3dSBarry Smith   PetscFunctionReturn(0);
1851c74985f6SBarry Smith }
1852c74985f6SBarry Smith 
18534a2ae208SSatish Balay #undef __FUNCT__
18544a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_MPIAIJ"
1855b1d57f15SBarry Smith PetscErrorCode MatGetRow_MPIAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
185639e00950SLois Curfman McInnes {
1857154123eaSLois Curfman McInnes   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)matin->data;
185887828ca2SBarry Smith   PetscScalar    *vworkA,*vworkB,**pvA,**pvB,*v_p;
18596849ba73SBarry Smith   PetscErrorCode ierr;
1860d0f46423SBarry Smith   PetscInt       i,*cworkA,*cworkB,**pcA,**pcB,cstart = matin->cmap->rstart;
1861d0f46423SBarry Smith   PetscInt       nztot,nzA,nzB,lrow,rstart = matin->rmap->rstart,rend = matin->rmap->rend;
1862b1d57f15SBarry Smith   PetscInt       *cmap,*idx_p;
186339e00950SLois Curfman McInnes 
18643a40ed3dSBarry Smith   PetscFunctionBegin;
1865e32f2f54SBarry Smith   if (mat->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Already active");
18667a0afa10SBarry Smith   mat->getrowactive = PETSC_TRUE;
18677a0afa10SBarry Smith 
186870f0671dSBarry Smith   if (!mat->rowvalues && (idx || v)) {
18697a0afa10SBarry Smith     /*
18707a0afa10SBarry Smith         allocate enough space to hold information from the longest row.
18717a0afa10SBarry Smith     */
18727a0afa10SBarry Smith     Mat_SeqAIJ *Aa = (Mat_SeqAIJ*)mat->A->data,*Ba = (Mat_SeqAIJ*)mat->B->data;
1873b1d57f15SBarry Smith     PetscInt   max = 1,tmp;
1874d0f46423SBarry Smith     for (i=0; i<matin->rmap->n; i++) {
18757a0afa10SBarry Smith       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
18767a0afa10SBarry Smith       if (max < tmp) { max = tmp; }
18777a0afa10SBarry Smith     }
18781d79065fSBarry Smith     ierr = PetscMalloc2(max,PetscScalar,&mat->rowvalues,max,PetscInt,&mat->rowindices);CHKERRQ(ierr);
18797a0afa10SBarry Smith   }
18807a0afa10SBarry Smith 
1881e7e72b3dSBarry Smith   if (row < rstart || row >= rend) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only local rows");
1882abc0e9e4SLois Curfman McInnes   lrow = row - rstart;
188339e00950SLois Curfman McInnes 
1884154123eaSLois Curfman McInnes   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1885154123eaSLois Curfman McInnes   if (!v)   {pvA = 0; pvB = 0;}
1886154123eaSLois Curfman McInnes   if (!idx) {pcA = 0; if (!v) pcB = 0;}
1887f830108cSBarry Smith   ierr = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1888f830108cSBarry Smith   ierr = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
1889154123eaSLois Curfman McInnes   nztot = nzA + nzB;
1890154123eaSLois Curfman McInnes 
189170f0671dSBarry Smith   cmap  = mat->garray;
1892154123eaSLois Curfman McInnes   if (v  || idx) {
1893154123eaSLois Curfman McInnes     if (nztot) {
1894154123eaSLois Curfman McInnes       /* Sort by increasing column numbers, assuming A and B already sorted */
1895b1d57f15SBarry Smith       PetscInt imark = -1;
1896154123eaSLois Curfman McInnes       if (v) {
189770f0671dSBarry Smith         *v = v_p = mat->rowvalues;
189839e00950SLois Curfman McInnes         for (i=0; i<nzB; i++) {
189970f0671dSBarry Smith           if (cmap[cworkB[i]] < cstart)   v_p[i] = vworkB[i];
1900154123eaSLois Curfman McInnes           else break;
1901154123eaSLois Curfman McInnes         }
1902154123eaSLois Curfman McInnes         imark = i;
190370f0671dSBarry Smith         for (i=0; i<nzA; i++)     v_p[imark+i] = vworkA[i];
190470f0671dSBarry Smith         for (i=imark; i<nzB; i++) v_p[nzA+i]   = vworkB[i];
1905154123eaSLois Curfman McInnes       }
1906154123eaSLois Curfman McInnes       if (idx) {
190770f0671dSBarry Smith         *idx = idx_p = mat->rowindices;
190870f0671dSBarry Smith         if (imark > -1) {
190970f0671dSBarry Smith           for (i=0; i<imark; i++) {
191070f0671dSBarry Smith             idx_p[i] = cmap[cworkB[i]];
191170f0671dSBarry Smith           }
191270f0671dSBarry Smith         } else {
1913154123eaSLois Curfman McInnes           for (i=0; i<nzB; i++) {
191470f0671dSBarry Smith             if (cmap[cworkB[i]] < cstart)   idx_p[i] = cmap[cworkB[i]];
1915154123eaSLois Curfman McInnes             else break;
1916154123eaSLois Curfman McInnes           }
1917154123eaSLois Curfman McInnes           imark = i;
191870f0671dSBarry Smith         }
191970f0671dSBarry Smith         for (i=0; i<nzA; i++)     idx_p[imark+i] = cstart + cworkA[i];
192070f0671dSBarry Smith         for (i=imark; i<nzB; i++) idx_p[nzA+i]   = cmap[cworkB[i]];
192139e00950SLois Curfman McInnes       }
19223f97c4b0SBarry Smith     } else {
19231ca473b0SSatish Balay       if (idx) *idx = 0;
19241ca473b0SSatish Balay       if (v)   *v   = 0;
19251ca473b0SSatish Balay     }
1926154123eaSLois Curfman McInnes   }
192739e00950SLois Curfman McInnes   *nz = nztot;
1928f830108cSBarry Smith   ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1929f830108cSBarry Smith   ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
19303a40ed3dSBarry Smith   PetscFunctionReturn(0);
193139e00950SLois Curfman McInnes }
193239e00950SLois Curfman McInnes 
19334a2ae208SSatish Balay #undef __FUNCT__
19344a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_MPIAIJ"
1935b1d57f15SBarry Smith PetscErrorCode MatRestoreRow_MPIAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
193639e00950SLois Curfman McInnes {
19377a0afa10SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
19383a40ed3dSBarry Smith 
19393a40ed3dSBarry Smith   PetscFunctionBegin;
1940e7e72b3dSBarry Smith   if (!aij->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"MatGetRow() must be called first");
19417a0afa10SBarry Smith   aij->getrowactive = PETSC_FALSE;
19423a40ed3dSBarry Smith   PetscFunctionReturn(0);
194339e00950SLois Curfman McInnes }
194439e00950SLois Curfman McInnes 
19454a2ae208SSatish Balay #undef __FUNCT__
19464a2ae208SSatish Balay #define __FUNCT__ "MatNorm_MPIAIJ"
1947dfbe8321SBarry Smith PetscErrorCode MatNorm_MPIAIJ(Mat mat,NormType type,PetscReal *norm)
1948855ac2c5SLois Curfman McInnes {
1949855ac2c5SLois Curfman McInnes   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
1950ec8511deSBarry Smith   Mat_SeqAIJ     *amat = (Mat_SeqAIJ*)aij->A->data,*bmat = (Mat_SeqAIJ*)aij->B->data;
1951dfbe8321SBarry Smith   PetscErrorCode ierr;
1952d0f46423SBarry Smith   PetscInt       i,j,cstart = mat->cmap->rstart;
1953329f5518SBarry Smith   PetscReal      sum = 0.0;
1954a77337e4SBarry Smith   MatScalar      *v;
195504ca555eSLois Curfman McInnes 
19563a40ed3dSBarry Smith   PetscFunctionBegin;
195717699dbbSLois Curfman McInnes   if (aij->size == 1) {
195814183eadSLois Curfman McInnes     ierr =  MatNorm(aij->A,type,norm);CHKERRQ(ierr);
195937fa93a5SLois Curfman McInnes   } else {
196004ca555eSLois Curfman McInnes     if (type == NORM_FROBENIUS) {
196104ca555eSLois Curfman McInnes       v = amat->a;
196204ca555eSLois Curfman McInnes       for (i=0; i<amat->nz; i++) {
1963aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
1964329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
196504ca555eSLois Curfman McInnes #else
196604ca555eSLois Curfman McInnes         sum += (*v)*(*v); v++;
196704ca555eSLois Curfman McInnes #endif
196804ca555eSLois Curfman McInnes       }
196904ca555eSLois Curfman McInnes       v = bmat->a;
197004ca555eSLois Curfman McInnes       for (i=0; i<bmat->nz; i++) {
1971aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
1972329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
197304ca555eSLois Curfman McInnes #else
197404ca555eSLois Curfman McInnes         sum += (*v)*(*v); v++;
197504ca555eSLois Curfman McInnes #endif
197604ca555eSLois Curfman McInnes       }
1977d9822059SBarry Smith       ierr = MPI_Allreduce(&sum,norm,1,MPIU_REAL,MPIU_SUM,((PetscObject)mat)->comm);CHKERRQ(ierr);
19788f1a2a5eSBarry Smith       *norm = PetscSqrtReal(*norm);
19793a40ed3dSBarry Smith     } else if (type == NORM_1) { /* max column norm */
1980329f5518SBarry Smith       PetscReal *tmp,*tmp2;
1981b1d57f15SBarry Smith       PetscInt  *jj,*garray = aij->garray;
1982d0f46423SBarry Smith       ierr = PetscMalloc((mat->cmap->N+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1983d0f46423SBarry Smith       ierr = PetscMalloc((mat->cmap->N+1)*sizeof(PetscReal),&tmp2);CHKERRQ(ierr);
1984d0f46423SBarry Smith       ierr = PetscMemzero(tmp,mat->cmap->N*sizeof(PetscReal));CHKERRQ(ierr);
198504ca555eSLois Curfman McInnes       *norm = 0.0;
198604ca555eSLois Curfman McInnes       v = amat->a; jj = amat->j;
198704ca555eSLois Curfman McInnes       for (j=0; j<amat->nz; j++) {
1988bfec09a0SHong Zhang         tmp[cstart + *jj++ ] += PetscAbsScalar(*v);  v++;
198904ca555eSLois Curfman McInnes       }
199004ca555eSLois Curfman McInnes       v = bmat->a; jj = bmat->j;
199104ca555eSLois Curfman McInnes       for (j=0; j<bmat->nz; j++) {
1992bfec09a0SHong Zhang         tmp[garray[*jj++]] += PetscAbsScalar(*v); v++;
199304ca555eSLois Curfman McInnes       }
1994d9822059SBarry Smith       ierr = MPI_Allreduce(tmp,tmp2,mat->cmap->N,MPIU_REAL,MPIU_SUM,((PetscObject)mat)->comm);CHKERRQ(ierr);
1995d0f46423SBarry Smith       for (j=0; j<mat->cmap->N; j++) {
199604ca555eSLois Curfman McInnes         if (tmp2[j] > *norm) *norm = tmp2[j];
199704ca555eSLois Curfman McInnes       }
1998606d414cSSatish Balay       ierr = PetscFree(tmp);CHKERRQ(ierr);
1999606d414cSSatish Balay       ierr = PetscFree(tmp2);CHKERRQ(ierr);
20003a40ed3dSBarry Smith     } else if (type == NORM_INFINITY) { /* max row norm */
2001329f5518SBarry Smith       PetscReal ntemp = 0.0;
2002d0f46423SBarry Smith       for (j=0; j<aij->A->rmap->n; j++) {
2003bfec09a0SHong Zhang         v = amat->a + amat->i[j];
200404ca555eSLois Curfman McInnes         sum = 0.0;
200504ca555eSLois Curfman McInnes         for (i=0; i<amat->i[j+1]-amat->i[j]; i++) {
2006cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
200704ca555eSLois Curfman McInnes         }
2008bfec09a0SHong Zhang         v = bmat->a + bmat->i[j];
200904ca555eSLois Curfman McInnes         for (i=0; i<bmat->i[j+1]-bmat->i[j]; i++) {
2010cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
201104ca555eSLois Curfman McInnes         }
2012515d9167SLois Curfman McInnes         if (sum > ntemp) ntemp = sum;
201304ca555eSLois Curfman McInnes       }
2014d9822059SBarry Smith       ierr = MPI_Allreduce(&ntemp,norm,1,MPIU_REAL,MPIU_MAX,((PetscObject)mat)->comm);CHKERRQ(ierr);
2015ca161407SBarry Smith     } else {
2016e7e72b3dSBarry Smith       SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_SUP,"No support for two norm");
201704ca555eSLois Curfman McInnes     }
201837fa93a5SLois Curfman McInnes   }
20193a40ed3dSBarry Smith   PetscFunctionReturn(0);
2020855ac2c5SLois Curfman McInnes }
2021855ac2c5SLois Curfman McInnes 
20224a2ae208SSatish Balay #undef __FUNCT__
20234a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_MPIAIJ"
2024fc4dec0aSBarry Smith PetscErrorCode MatTranspose_MPIAIJ(Mat A,MatReuse reuse,Mat *matout)
2025b7c46309SBarry Smith {
2026b7c46309SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
2027da668accSHong Zhang   Mat_SeqAIJ     *Aloc=(Mat_SeqAIJ*)a->A->data,*Bloc=(Mat_SeqAIJ*)a->B->data;
2028dfbe8321SBarry Smith   PetscErrorCode ierr;
2029d0f46423SBarry Smith   PetscInt       M = A->rmap->N,N = A->cmap->N,ma,na,mb,*ai,*aj,*bi,*bj,row,*cols,*cols_tmp,i,*d_nnz;
2030d0f46423SBarry Smith   PetscInt       cstart=A->cmap->rstart,ncol;
20313a40ed3dSBarry Smith   Mat            B;
2032a77337e4SBarry Smith   MatScalar      *array;
2033b7c46309SBarry Smith 
20343a40ed3dSBarry Smith   PetscFunctionBegin;
2035e7e72b3dSBarry Smith   if (reuse == MAT_REUSE_MATRIX && A == *matout && M != N) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
2036da668accSHong Zhang 
2037d0f46423SBarry Smith   ma = A->rmap->n; na = A->cmap->n; mb = a->B->rmap->n;
2038da668accSHong Zhang   ai = Aloc->i; aj = Aloc->j;
2039da668accSHong Zhang   bi = Bloc->i; bj = Bloc->j;
2040fc73b1b3SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *matout == A) {
2041fc73b1b3SBarry Smith     /* compute d_nnz for preallocation; o_nnz is approximated by d_nnz to avoid communication */
2042fc73b1b3SBarry Smith     ierr = PetscMalloc((1+na)*sizeof(PetscInt),&d_nnz);CHKERRQ(ierr);
2043da668accSHong Zhang     ierr = PetscMemzero(d_nnz,(1+na)*sizeof(PetscInt));CHKERRQ(ierr);
2044da668accSHong Zhang     for (i=0; i<ai[ma]; i++){
2045da668accSHong Zhang       d_nnz[aj[i]] ++;
2046da668accSHong Zhang       aj[i] += cstart; /* global col index to be used by MatSetValues() */
2047d4bb536fSBarry Smith     }
2048d4bb536fSBarry Smith 
20497adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)A)->comm,&B);CHKERRQ(ierr);
2050d0f46423SBarry Smith     ierr = MatSetSizes(B,A->cmap->n,A->rmap->n,N,M);CHKERRQ(ierr);
2051a2f3521dSMark F. Adams     ierr = MatSetBlockSizes(B,A->cmap->bs,A->rmap->bs); CHKERRQ(ierr);
20527adad957SLisandro Dalcin     ierr = MatSetType(B,((PetscObject)A)->type_name);CHKERRQ(ierr);
2053da668accSHong Zhang     ierr = MatMPIAIJSetPreallocation(B,0,d_nnz,0,d_nnz);CHKERRQ(ierr);
20541d567fd6SHong Zhang     ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr);
2055fc73b1b3SBarry Smith     ierr = PetscFree(d_nnz);CHKERRQ(ierr);
2056fc4dec0aSBarry Smith   } else {
2057fc4dec0aSBarry Smith     B = *matout;
20586ffab4bbSHong Zhang     ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
20596ffab4bbSHong Zhang     for (i=0; i<ai[ma]; i++){
20606ffab4bbSHong Zhang       aj[i] += cstart; /* global col index to be used by MatSetValues() */
20616ffab4bbSHong Zhang     }
2062fc4dec0aSBarry Smith   }
2063b7c46309SBarry Smith 
2064b7c46309SBarry Smith   /* copy over the A part */
2065da668accSHong Zhang   array = Aloc->a;
2066d0f46423SBarry Smith   row = A->rmap->rstart;
2067da668accSHong Zhang   for (i=0; i<ma; i++) {
2068da668accSHong Zhang     ncol = ai[i+1]-ai[i];
2069da668accSHong Zhang     ierr = MatSetValues(B,ncol,aj,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
2070da668accSHong Zhang     row++; array += ncol; aj += ncol;
2071b7c46309SBarry Smith   }
2072b7c46309SBarry Smith   aj = Aloc->j;
2073da668accSHong Zhang   for (i=0; i<ai[ma]; i++) aj[i] -= cstart; /* resume local col index */
2074b7c46309SBarry Smith 
2075b7c46309SBarry Smith   /* copy over the B part */
2076fc73b1b3SBarry Smith   ierr = PetscMalloc(bi[mb]*sizeof(PetscInt),&cols);CHKERRQ(ierr);
2077fc73b1b3SBarry Smith   ierr = PetscMemzero(cols,bi[mb]*sizeof(PetscInt));CHKERRQ(ierr);
2078da668accSHong Zhang   array = Bloc->a;
2079d0f46423SBarry Smith   row = A->rmap->rstart;
2080da668accSHong Zhang   for (i=0; i<bi[mb]; i++) {cols[i] = a->garray[bj[i]];}
208161a2fbbaSHong Zhang   cols_tmp = cols;
2082da668accSHong Zhang   for (i=0; i<mb; i++) {
2083da668accSHong Zhang     ncol = bi[i+1]-bi[i];
208461a2fbbaSHong Zhang     ierr = MatSetValues(B,ncol,cols_tmp,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
208561a2fbbaSHong Zhang     row++; array += ncol; cols_tmp += ncol;
2086b7c46309SBarry Smith   }
2087fc73b1b3SBarry Smith   ierr = PetscFree(cols);CHKERRQ(ierr);
2088fc73b1b3SBarry Smith 
20896d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20906d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2091815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *matout != A) {
20920de55854SLois Curfman McInnes     *matout = B;
20930de55854SLois Curfman McInnes   } else {
2094eb6b5d47SBarry Smith     ierr = MatHeaderMerge(A,B);CHKERRQ(ierr);
20950de55854SLois Curfman McInnes   }
20963a40ed3dSBarry Smith   PetscFunctionReturn(0);
2097b7c46309SBarry Smith }
2098b7c46309SBarry Smith 
20994a2ae208SSatish Balay #undef __FUNCT__
21004a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_MPIAIJ"
2101dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr)
2102a008b906SSatish Balay {
21034b967eb1SSatish Balay   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
21044b967eb1SSatish Balay   Mat            a = aij->A,b = aij->B;
2105dfbe8321SBarry Smith   PetscErrorCode ierr;
2106b1d57f15SBarry Smith   PetscInt       s1,s2,s3;
2107a008b906SSatish Balay 
21083a40ed3dSBarry Smith   PetscFunctionBegin;
21094b967eb1SSatish Balay   ierr = MatGetLocalSize(mat,&s2,&s3);CHKERRQ(ierr);
21104b967eb1SSatish Balay   if (rr) {
2111e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr);
2112e32f2f54SBarry Smith     if (s1!=s3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"right vector non-conforming local size");
21134b967eb1SSatish Balay     /* Overlap communication with computation. */
2114ca9f406cSSatish Balay     ierr = VecScatterBegin(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2115a008b906SSatish Balay   }
21164b967eb1SSatish Balay   if (ll) {
2117e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr);
2118e32f2f54SBarry Smith     if (s1!=s2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"left vector non-conforming local size");
2119f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,ll,0);CHKERRQ(ierr);
21204b967eb1SSatish Balay   }
21214b967eb1SSatish Balay   /* scale  the diagonal block */
2122f830108cSBarry Smith   ierr = (*a->ops->diagonalscale)(a,ll,rr);CHKERRQ(ierr);
21234b967eb1SSatish Balay 
21244b967eb1SSatish Balay   if (rr) {
21254b967eb1SSatish Balay     /* Do a scatter end and then right scale the off-diagonal block */
2126ca9f406cSSatish Balay     ierr = VecScatterEnd(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2127f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,0,aij->lvec);CHKERRQ(ierr);
21284b967eb1SSatish Balay   }
21294b967eb1SSatish Balay 
21303a40ed3dSBarry Smith   PetscFunctionReturn(0);
2131a008b906SSatish Balay }
2132a008b906SSatish Balay 
21334a2ae208SSatish Balay #undef __FUNCT__
21344a2ae208SSatish Balay #define __FUNCT__ "MatSetUnfactored_MPIAIJ"
2135dfbe8321SBarry Smith PetscErrorCode MatSetUnfactored_MPIAIJ(Mat A)
2136bb5a7306SBarry Smith {
2137bb5a7306SBarry Smith   Mat_MPIAIJ     *a   = (Mat_MPIAIJ*)A->data;
2138dfbe8321SBarry Smith   PetscErrorCode ierr;
21393a40ed3dSBarry Smith 
21403a40ed3dSBarry Smith   PetscFunctionBegin;
2141bb5a7306SBarry Smith   ierr = MatSetUnfactored(a->A);CHKERRQ(ierr);
21423a40ed3dSBarry Smith   PetscFunctionReturn(0);
2143bb5a7306SBarry Smith }
2144bb5a7306SBarry Smith 
21454a2ae208SSatish Balay #undef __FUNCT__
21464a2ae208SSatish Balay #define __FUNCT__ "MatEqual_MPIAIJ"
2147ace3abfcSBarry Smith PetscErrorCode MatEqual_MPIAIJ(Mat A,Mat B,PetscBool  *flag)
2148d4bb536fSBarry Smith {
2149d4bb536fSBarry Smith   Mat_MPIAIJ     *matB = (Mat_MPIAIJ*)B->data,*matA = (Mat_MPIAIJ*)A->data;
2150d4bb536fSBarry Smith   Mat            a,b,c,d;
2151ace3abfcSBarry Smith   PetscBool      flg;
2152dfbe8321SBarry Smith   PetscErrorCode ierr;
2153d4bb536fSBarry Smith 
21543a40ed3dSBarry Smith   PetscFunctionBegin;
2155d4bb536fSBarry Smith   a = matA->A; b = matA->B;
2156d4bb536fSBarry Smith   c = matB->A; d = matB->B;
2157d4bb536fSBarry Smith 
2158d4bb536fSBarry Smith   ierr = MatEqual(a,c,&flg);CHKERRQ(ierr);
2159abc0a331SBarry Smith   if (flg) {
2160d4bb536fSBarry Smith     ierr = MatEqual(b,d,&flg);CHKERRQ(ierr);
2161d4bb536fSBarry Smith   }
21627adad957SLisandro Dalcin   ierr = MPI_Allreduce(&flg,flag,1,MPI_INT,MPI_LAND,((PetscObject)A)->comm);CHKERRQ(ierr);
21633a40ed3dSBarry Smith   PetscFunctionReturn(0);
2164d4bb536fSBarry Smith }
2165d4bb536fSBarry Smith 
21664a2ae208SSatish Balay #undef __FUNCT__
21674a2ae208SSatish Balay #define __FUNCT__ "MatCopy_MPIAIJ"
2168dfbe8321SBarry Smith PetscErrorCode MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str)
2169cb5b572fSBarry Smith {
2170dfbe8321SBarry Smith   PetscErrorCode ierr;
2171cb5b572fSBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ *)A->data;
2172cb5b572fSBarry Smith   Mat_MPIAIJ     *b = (Mat_MPIAIJ *)B->data;
2173cb5b572fSBarry Smith 
2174cb5b572fSBarry Smith   PetscFunctionBegin;
217533f4a19fSKris Buschelman   /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
217633f4a19fSKris Buschelman   if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) {
2177cb5b572fSBarry Smith     /* because of the column compression in the off-processor part of the matrix a->B,
2178cb5b572fSBarry Smith        the number of columns in a->B and b->B may be different, hence we cannot call
2179cb5b572fSBarry Smith        the MatCopy() directly on the two parts. If need be, we can provide a more
2180cb5b572fSBarry Smith        efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices
2181cb5b572fSBarry Smith        then copying the submatrices */
2182cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2183cb5b572fSBarry Smith   } else {
2184cb5b572fSBarry Smith     ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr);
2185cb5b572fSBarry Smith     ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr);
2186cb5b572fSBarry Smith   }
2187cb5b572fSBarry Smith   PetscFunctionReturn(0);
2188cb5b572fSBarry Smith }
2189cb5b572fSBarry Smith 
21904a2ae208SSatish Balay #undef __FUNCT__
21914994cf47SJed Brown #define __FUNCT__ "MatSetUp_MPIAIJ"
21924994cf47SJed Brown PetscErrorCode MatSetUp_MPIAIJ(Mat A)
2193273d9f13SBarry Smith {
2194dfbe8321SBarry Smith   PetscErrorCode ierr;
2195273d9f13SBarry Smith 
2196273d9f13SBarry Smith   PetscFunctionBegin;
2197273d9f13SBarry Smith   ierr =  MatMPIAIJSetPreallocation(A,PETSC_DEFAULT,0,PETSC_DEFAULT,0);CHKERRQ(ierr);
2198273d9f13SBarry Smith   PetscFunctionReturn(0);
2199273d9f13SBarry Smith }
2200273d9f13SBarry Smith 
2201ac90fabeSBarry Smith #undef __FUNCT__
220295b7e79eSJed Brown #define __FUNCT__ "MatAXPYGetPreallocation_MPIAIJ"
220395b7e79eSJed Brown /* This is the same as MatAXPYGetPreallocation_SeqAIJ, except that the local-to-global map is provided */
220495b7e79eSJed Brown static PetscErrorCode MatAXPYGetPreallocation_MPIAIJ(Mat Y,const PetscInt *yltog,Mat X,const PetscInt *xltog,PetscInt* nnz)
220595b7e79eSJed Brown {
220695b7e79eSJed Brown   PetscInt          i,m=Y->rmap->N;
220795b7e79eSJed Brown   Mat_SeqAIJ        *x = (Mat_SeqAIJ*)X->data;
220895b7e79eSJed Brown   Mat_SeqAIJ        *y = (Mat_SeqAIJ*)Y->data;
220995b7e79eSJed Brown   const PetscInt    *xi = x->i,*yi = y->i;
221095b7e79eSJed Brown 
221195b7e79eSJed Brown   PetscFunctionBegin;
221295b7e79eSJed Brown   /* Set the number of nonzeros in the new matrix */
221395b7e79eSJed Brown   for(i=0; i<m; i++) {
221495b7e79eSJed Brown     PetscInt j,k,nzx = xi[i+1] - xi[i],nzy = yi[i+1] - yi[i];
221595b7e79eSJed Brown     const PetscInt *xj = x->j+xi[i],*yj = y->j+yi[i];
221695b7e79eSJed Brown     nnz[i] = 0;
221795b7e79eSJed Brown     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
221895b7e79eSJed Brown       for (; k<nzy && yltog[yj[k]]<xltog[xj[j]]; k++) nnz[i]++; /* Catch up to X */
221995b7e79eSJed Brown       if (k<nzy && yltog[yj[k]]==xltog[xj[j]]) k++;             /* Skip duplicate */
222095b7e79eSJed Brown       nnz[i]++;
222195b7e79eSJed Brown     }
222295b7e79eSJed Brown     for (; k<nzy; k++) nnz[i]++;
222395b7e79eSJed Brown   }
222495b7e79eSJed Brown   PetscFunctionReturn(0);
222595b7e79eSJed Brown }
222695b7e79eSJed Brown 
222795b7e79eSJed Brown #undef __FUNCT__
2228ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_MPIAIJ"
2229f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_MPIAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2230ac90fabeSBarry Smith {
2231dfbe8321SBarry Smith   PetscErrorCode ierr;
2232b1d57f15SBarry Smith   PetscInt       i;
2233ac90fabeSBarry Smith   Mat_MPIAIJ     *xx = (Mat_MPIAIJ *)X->data,*yy = (Mat_MPIAIJ *)Y->data;
22344ce68768SBarry Smith   PetscBLASInt   bnz,one=1;
2235ac90fabeSBarry Smith   Mat_SeqAIJ     *x,*y;
2236ac90fabeSBarry Smith 
2237ac90fabeSBarry Smith   PetscFunctionBegin;
2238ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2239f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2240ac90fabeSBarry Smith     x = (Mat_SeqAIJ *)xx->A->data;
2241ac90fabeSBarry Smith     y = (Mat_SeqAIJ *)yy->A->data;
22420805154bSBarry Smith     bnz = PetscBLASIntCast(x->nz);
2243f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
2244ac90fabeSBarry Smith     x = (Mat_SeqAIJ *)xx->B->data;
2245ac90fabeSBarry Smith     y = (Mat_SeqAIJ *)yy->B->data;
22460805154bSBarry Smith     bnz = PetscBLASIntCast(x->nz);
2247f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
2248a30b2313SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) {
2249f4df32b1SMatthew Knepley     ierr = MatAXPY_SeqAIJ(yy->A,a,xx->A,str);CHKERRQ(ierr);
2250c537a176SHong Zhang 
2251c537a176SHong Zhang     x = (Mat_SeqAIJ *)xx->B->data;
2252a30b2313SHong Zhang     y = (Mat_SeqAIJ *)yy->B->data;
2253a30b2313SHong Zhang     if (y->xtoy && y->XtoY != xx->B) {
2254a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
22556bf464f9SBarry Smith       ierr = MatDestroy(&y->XtoY);CHKERRQ(ierr);
2256c537a176SHong Zhang     }
2257a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
2258d0f46423SBarry Smith       ierr = MatAXPYGetxtoy_Private(xx->B->rmap->n,x->i,x->j,xx->garray,y->i,y->j,yy->garray,&y->xtoy);CHKERRQ(ierr);
2259a30b2313SHong Zhang       y->XtoY = xx->B;
2260407f6b05SHong Zhang       ierr = PetscObjectReference((PetscObject)xx->B);CHKERRQ(ierr);
2261c537a176SHong Zhang     }
2262f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
2263ac90fabeSBarry Smith   } else {
22649f5f6813SShri Abhyankar     Mat B;
22659f5f6813SShri Abhyankar     PetscInt *nnz_d,*nnz_o;
22669f5f6813SShri Abhyankar     ierr = PetscMalloc(yy->A->rmap->N*sizeof(PetscInt),&nnz_d);CHKERRQ(ierr);
22679f5f6813SShri Abhyankar     ierr = PetscMalloc(yy->B->rmap->N*sizeof(PetscInt),&nnz_o);CHKERRQ(ierr);
22689f5f6813SShri Abhyankar     ierr = MatCreate(((PetscObject)Y)->comm,&B);CHKERRQ(ierr);
2269bc5a2726SShri Abhyankar     ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr);
22709f5f6813SShri Abhyankar     ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr);
2271a2f3521dSMark F. Adams     ierr = MatSetBlockSizes(B,Y->rmap->bs,Y->cmap->bs);CHKERRQ(ierr);
22729f5f6813SShri Abhyankar     ierr = MatSetType(B,MATMPIAIJ);CHKERRQ(ierr);
22739f5f6813SShri Abhyankar     ierr = MatAXPYGetPreallocation_SeqAIJ(yy->A,xx->A,nnz_d);CHKERRQ(ierr);
227495b7e79eSJed Brown     ierr = MatAXPYGetPreallocation_MPIAIJ(yy->B,yy->garray,xx->B,xx->garray,nnz_o);CHKERRQ(ierr);
2275ecd8bba6SJed Brown     ierr = MatMPIAIJSetPreallocation(B,0,nnz_d,0,nnz_o);CHKERRQ(ierr);
22769f5f6813SShri Abhyankar     ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr);
22779f5f6813SShri Abhyankar     ierr = MatHeaderReplace(Y,B);
22789f5f6813SShri Abhyankar     ierr = PetscFree(nnz_d);CHKERRQ(ierr);
22799f5f6813SShri Abhyankar     ierr = PetscFree(nnz_o);CHKERRQ(ierr);
2280ac90fabeSBarry Smith   }
2281ac90fabeSBarry Smith   PetscFunctionReturn(0);
2282ac90fabeSBarry Smith }
2283ac90fabeSBarry Smith 
22847087cfbeSBarry Smith extern PetscErrorCode  MatConjugate_SeqAIJ(Mat);
2285354c94deSBarry Smith 
2286354c94deSBarry Smith #undef __FUNCT__
2287354c94deSBarry Smith #define __FUNCT__ "MatConjugate_MPIAIJ"
22887087cfbeSBarry Smith PetscErrorCode  MatConjugate_MPIAIJ(Mat mat)
2289354c94deSBarry Smith {
2290354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2291354c94deSBarry Smith   PetscErrorCode ierr;
2292354c94deSBarry Smith   Mat_MPIAIJ     *aij = (Mat_MPIAIJ *)mat->data;
2293354c94deSBarry Smith 
2294354c94deSBarry Smith   PetscFunctionBegin;
2295354c94deSBarry Smith   ierr = MatConjugate_SeqAIJ(aij->A);CHKERRQ(ierr);
2296354c94deSBarry Smith   ierr = MatConjugate_SeqAIJ(aij->B);CHKERRQ(ierr);
2297354c94deSBarry Smith #else
2298354c94deSBarry Smith   PetscFunctionBegin;
2299354c94deSBarry Smith #endif
2300354c94deSBarry Smith   PetscFunctionReturn(0);
2301354c94deSBarry Smith }
2302354c94deSBarry Smith 
230399cafbc1SBarry Smith #undef __FUNCT__
230499cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_MPIAIJ"
230599cafbc1SBarry Smith PetscErrorCode MatRealPart_MPIAIJ(Mat A)
230699cafbc1SBarry Smith {
230799cafbc1SBarry Smith   Mat_MPIAIJ   *a = (Mat_MPIAIJ*)A->data;
230899cafbc1SBarry Smith   PetscErrorCode ierr;
230999cafbc1SBarry Smith 
231099cafbc1SBarry Smith   PetscFunctionBegin;
231199cafbc1SBarry Smith   ierr = MatRealPart(a->A);CHKERRQ(ierr);
231299cafbc1SBarry Smith   ierr = MatRealPart(a->B);CHKERRQ(ierr);
231399cafbc1SBarry Smith   PetscFunctionReturn(0);
231499cafbc1SBarry Smith }
231599cafbc1SBarry Smith 
231699cafbc1SBarry Smith #undef __FUNCT__
231799cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_MPIAIJ"
231899cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_MPIAIJ(Mat A)
231999cafbc1SBarry Smith {
232099cafbc1SBarry Smith   Mat_MPIAIJ   *a = (Mat_MPIAIJ*)A->data;
232199cafbc1SBarry Smith   PetscErrorCode ierr;
232299cafbc1SBarry Smith 
232399cafbc1SBarry Smith   PetscFunctionBegin;
232499cafbc1SBarry Smith   ierr = MatImaginaryPart(a->A);CHKERRQ(ierr);
232599cafbc1SBarry Smith   ierr = MatImaginaryPart(a->B);CHKERRQ(ierr);
232699cafbc1SBarry Smith   PetscFunctionReturn(0);
232799cafbc1SBarry Smith }
232899cafbc1SBarry Smith 
2329103bf8bdSMatthew Knepley #ifdef PETSC_HAVE_PBGL
2330103bf8bdSMatthew Knepley 
2331103bf8bdSMatthew Knepley #include <boost/parallel/mpi/bsp_process_group.hpp>
2332a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_default_graph.hpp>
2333a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_0_block.hpp>
2334a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_preconditioner.hpp>
2335103bf8bdSMatthew Knepley #include <boost/graph/distributed/petsc/interface.hpp>
2336a2c909beSMatthew Knepley #include <boost/multi_array.hpp>
2337d0f46423SBarry Smith #include <boost/parallel/distributed_property_map->hpp>
2338103bf8bdSMatthew Knepley 
2339103bf8bdSMatthew Knepley #undef __FUNCT__
2340103bf8bdSMatthew Knepley #define __FUNCT__ "MatILUFactorSymbolic_MPIAIJ"
2341103bf8bdSMatthew Knepley /*
2342103bf8bdSMatthew Knepley   This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu>
2343103bf8bdSMatthew Knepley */
23440481f469SBarry Smith PetscErrorCode MatILUFactorSymbolic_MPIAIJ(Mat fact,Mat A, IS isrow, IS iscol, const MatFactorInfo *info)
2345103bf8bdSMatthew Knepley {
2346a2c909beSMatthew Knepley   namespace petsc = boost::distributed::petsc;
2347a2c909beSMatthew Knepley 
2348a2c909beSMatthew Knepley   namespace graph_dist = boost::graph::distributed;
2349a2c909beSMatthew Knepley   using boost::graph::distributed::ilu_default::process_group_type;
2350a2c909beSMatthew Knepley   using boost::graph::ilu_permuted;
2351a2c909beSMatthew Knepley 
2352ace3abfcSBarry Smith   PetscBool       row_identity, col_identity;
2353776b82aeSLisandro Dalcin   PetscContainer  c;
2354103bf8bdSMatthew Knepley   PetscInt        m, n, M, N;
2355103bf8bdSMatthew Knepley   PetscErrorCode  ierr;
2356103bf8bdSMatthew Knepley 
2357103bf8bdSMatthew Knepley   PetscFunctionBegin;
2358e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels = 0 supported for parallel ilu");
2359103bf8bdSMatthew Knepley   ierr = ISIdentity(isrow, &row_identity);CHKERRQ(ierr);
2360103bf8bdSMatthew Knepley   ierr = ISIdentity(iscol, &col_identity);CHKERRQ(ierr);
2361103bf8bdSMatthew Knepley   if (!row_identity || !col_identity) {
2362e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Row and column permutations must be identity for parallel ILU");
2363103bf8bdSMatthew Knepley   }
2364103bf8bdSMatthew Knepley 
2365103bf8bdSMatthew Knepley   process_group_type pg;
2366a2c909beSMatthew Knepley   typedef graph_dist::ilu_default::ilu_level_graph_type  lgraph_type;
2367a2c909beSMatthew Knepley   lgraph_type*   lgraph_p = new lgraph_type(petsc::num_global_vertices(A), pg, petsc::matrix_distribution(A, pg));
2368a2c909beSMatthew Knepley   lgraph_type&   level_graph = *lgraph_p;
2369a2c909beSMatthew Knepley   graph_dist::ilu_default::graph_type&            graph(level_graph.graph);
2370a2c909beSMatthew Knepley 
2371103bf8bdSMatthew Knepley   petsc::read_matrix(A, graph, get(boost::edge_weight, graph));
2372a2c909beSMatthew Knepley   ilu_permuted(level_graph);
2373103bf8bdSMatthew Knepley 
2374103bf8bdSMatthew Knepley   /* put together the new matrix */
23757adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)A)->comm, fact);CHKERRQ(ierr);
2376103bf8bdSMatthew Knepley   ierr = MatGetLocalSize(A, &m, &n);CHKERRQ(ierr);
2377103bf8bdSMatthew Knepley   ierr = MatGetSize(A, &M, &N);CHKERRQ(ierr);
2378719d5645SBarry Smith   ierr = MatSetSizes(fact, m, n, M, N);CHKERRQ(ierr);
2379a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(fact,A->rmap->bs,A->cmap->bs); CHKERRQ(ierr);
2380719d5645SBarry Smith   ierr = MatSetType(fact, ((PetscObject)A)->type_name);CHKERRQ(ierr);
2381719d5645SBarry Smith   ierr = MatAssemblyBegin(fact, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2382719d5645SBarry Smith   ierr = MatAssemblyEnd(fact, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2383103bf8bdSMatthew Knepley 
23847adad957SLisandro Dalcin   ierr = PetscContainerCreate(((PetscObject)A)->comm, &c);
2385776b82aeSLisandro Dalcin   ierr = PetscContainerSetPointer(c, lgraph_p);
2386719d5645SBarry Smith   ierr = PetscObjectCompose((PetscObject) (fact), "graph", (PetscObject) c);
2387bf0cc555SLisandro Dalcin   ierr = PetscContainerDestroy(&c);
2388103bf8bdSMatthew Knepley   PetscFunctionReturn(0);
2389103bf8bdSMatthew Knepley }
2390103bf8bdSMatthew Knepley 
2391103bf8bdSMatthew Knepley #undef __FUNCT__
2392103bf8bdSMatthew Knepley #define __FUNCT__ "MatLUFactorNumeric_MPIAIJ"
23930481f469SBarry Smith PetscErrorCode MatLUFactorNumeric_MPIAIJ(Mat B,Mat A, const MatFactorInfo *info)
2394103bf8bdSMatthew Knepley {
2395103bf8bdSMatthew Knepley   PetscFunctionBegin;
2396103bf8bdSMatthew Knepley   PetscFunctionReturn(0);
2397103bf8bdSMatthew Knepley }
2398103bf8bdSMatthew Knepley 
2399103bf8bdSMatthew Knepley #undef __FUNCT__
2400103bf8bdSMatthew Knepley #define __FUNCT__ "MatSolve_MPIAIJ"
2401103bf8bdSMatthew Knepley /*
2402103bf8bdSMatthew Knepley   This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu>
2403103bf8bdSMatthew Knepley */
2404103bf8bdSMatthew Knepley PetscErrorCode MatSolve_MPIAIJ(Mat A, Vec b, Vec x)
2405103bf8bdSMatthew Knepley {
2406a2c909beSMatthew Knepley   namespace graph_dist = boost::graph::distributed;
2407a2c909beSMatthew Knepley 
2408a2c909beSMatthew Knepley   typedef graph_dist::ilu_default::ilu_level_graph_type  lgraph_type;
2409a2c909beSMatthew Knepley   lgraph_type*   lgraph_p;
2410776b82aeSLisandro Dalcin   PetscContainer c;
2411103bf8bdSMatthew Knepley   PetscErrorCode ierr;
2412103bf8bdSMatthew Knepley 
2413103bf8bdSMatthew Knepley   PetscFunctionBegin;
2414103bf8bdSMatthew Knepley   ierr = PetscObjectQuery((PetscObject) A, "graph", (PetscObject *) &c);CHKERRQ(ierr);
2415776b82aeSLisandro Dalcin   ierr = PetscContainerGetPointer(c, (void **) &lgraph_p);CHKERRQ(ierr);
2416103bf8bdSMatthew Knepley   ierr = VecCopy(b, x);CHKERRQ(ierr);
2417a2c909beSMatthew Knepley 
2418a2c909beSMatthew Knepley   PetscScalar* array_x;
2419a2c909beSMatthew Knepley   ierr = VecGetArray(x, &array_x);CHKERRQ(ierr);
2420a2c909beSMatthew Knepley   PetscInt sx;
2421a2c909beSMatthew Knepley   ierr = VecGetSize(x, &sx);CHKERRQ(ierr);
2422a2c909beSMatthew Knepley 
2423a2c909beSMatthew Knepley   PetscScalar* array_b;
2424a2c909beSMatthew Knepley   ierr = VecGetArray(b, &array_b);CHKERRQ(ierr);
2425a2c909beSMatthew Knepley   PetscInt sb;
2426a2c909beSMatthew Knepley   ierr = VecGetSize(b, &sb);CHKERRQ(ierr);
2427a2c909beSMatthew Knepley 
2428a2c909beSMatthew Knepley   lgraph_type&   level_graph = *lgraph_p;
2429a2c909beSMatthew Knepley   graph_dist::ilu_default::graph_type&            graph(level_graph.graph);
2430a2c909beSMatthew Knepley 
2431a2c909beSMatthew Knepley   typedef boost::multi_array_ref<PetscScalar, 1> array_ref_type;
2432a2c909beSMatthew Knepley   array_ref_type                                 ref_b(array_b, boost::extents[num_vertices(graph)]),
2433a2c909beSMatthew Knepley                                                  ref_x(array_x, boost::extents[num_vertices(graph)]);
2434a2c909beSMatthew Knepley 
2435a2c909beSMatthew Knepley   typedef boost::iterator_property_map<array_ref_type::iterator,
2436a2c909beSMatthew Knepley                                 boost::property_map<graph_dist::ilu_default::graph_type, boost::vertex_index_t>::type>  gvector_type;
2437a2c909beSMatthew Knepley   gvector_type                                   vector_b(ref_b.begin(), get(boost::vertex_index, graph)),
2438a2c909beSMatthew Knepley                                                  vector_x(ref_x.begin(), get(boost::vertex_index, graph));
2439a2c909beSMatthew Knepley 
2440a2c909beSMatthew Knepley   ilu_set_solve(*lgraph_p, vector_b, vector_x);
2441a2c909beSMatthew Knepley 
2442103bf8bdSMatthew Knepley   PetscFunctionReturn(0);
2443103bf8bdSMatthew Knepley }
2444103bf8bdSMatthew Knepley #endif
2445103bf8bdSMatthew Knepley 
244669db28dcSHong Zhang typedef struct { /* used by MatGetRedundantMatrix() for reusing matredundant */
244769db28dcSHong Zhang   PetscInt       nzlocal,nsends,nrecvs;
24481d79065fSBarry Smith   PetscMPIInt    *send_rank,*recv_rank;
24491d79065fSBarry Smith   PetscInt       *sbuf_nz,*rbuf_nz,*sbuf_j,**rbuf_j;
245069db28dcSHong Zhang   PetscScalar    *sbuf_a,**rbuf_a;
2451bf0cc555SLisandro Dalcin   PetscErrorCode (*Destroy)(Mat);
245269db28dcSHong Zhang } Mat_Redundant;
245369db28dcSHong Zhang 
245469db28dcSHong Zhang #undef __FUNCT__
245569db28dcSHong Zhang #define __FUNCT__ "PetscContainerDestroy_MatRedundant"
245669db28dcSHong Zhang PetscErrorCode PetscContainerDestroy_MatRedundant(void *ptr)
245769db28dcSHong Zhang {
245869db28dcSHong Zhang   PetscErrorCode       ierr;
245969db28dcSHong Zhang   Mat_Redundant        *redund=(Mat_Redundant*)ptr;
246069db28dcSHong Zhang   PetscInt             i;
246169db28dcSHong Zhang 
246269db28dcSHong Zhang   PetscFunctionBegin;
24631d79065fSBarry Smith   ierr = PetscFree2(redund->send_rank,redund->recv_rank);CHKERRQ(ierr);
246469db28dcSHong Zhang   ierr = PetscFree(redund->sbuf_j);CHKERRQ(ierr);
246569db28dcSHong Zhang   ierr = PetscFree(redund->sbuf_a);CHKERRQ(ierr);
246669db28dcSHong Zhang   for (i=0; i<redund->nrecvs; i++){
246769db28dcSHong Zhang     ierr = PetscFree(redund->rbuf_j[i]);CHKERRQ(ierr);
246869db28dcSHong Zhang     ierr = PetscFree(redund->rbuf_a[i]);CHKERRQ(ierr);
246969db28dcSHong Zhang   }
24701d79065fSBarry Smith   ierr = PetscFree4(redund->sbuf_nz,redund->rbuf_nz,redund->rbuf_j,redund->rbuf_a);CHKERRQ(ierr);
247169db28dcSHong Zhang   ierr = PetscFree(redund);CHKERRQ(ierr);
247269db28dcSHong Zhang   PetscFunctionReturn(0);
247369db28dcSHong Zhang }
247469db28dcSHong Zhang 
247569db28dcSHong Zhang #undef __FUNCT__
247669db28dcSHong Zhang #define __FUNCT__ "MatDestroy_MatRedundant"
247769db28dcSHong Zhang PetscErrorCode MatDestroy_MatRedundant(Mat A)
247869db28dcSHong Zhang {
247969db28dcSHong Zhang   PetscErrorCode  ierr;
248069db28dcSHong Zhang   PetscContainer  container;
248169db28dcSHong Zhang   Mat_Redundant   *redund=PETSC_NULL;
248269db28dcSHong Zhang 
248369db28dcSHong Zhang   PetscFunctionBegin;
248469db28dcSHong Zhang   ierr = PetscObjectQuery((PetscObject)A,"Mat_Redundant",(PetscObject *)&container);CHKERRQ(ierr);
2485bf0cc555SLisandro Dalcin   if (!container) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Container does not exit");
248669db28dcSHong Zhang   ierr = PetscContainerGetPointer(container,(void **)&redund);CHKERRQ(ierr);
2487bf0cc555SLisandro Dalcin   A->ops->destroy = redund->Destroy;
248869db28dcSHong Zhang   ierr = PetscObjectCompose((PetscObject)A,"Mat_Redundant",0);CHKERRQ(ierr);
2489bf0cc555SLisandro Dalcin   if (A->ops->destroy) {
249069db28dcSHong Zhang     ierr = (*A->ops->destroy)(A);CHKERRQ(ierr);
2491bf0cc555SLisandro Dalcin   }
249269db28dcSHong Zhang   PetscFunctionReturn(0);
249369db28dcSHong Zhang }
249469db28dcSHong Zhang 
249569db28dcSHong Zhang #undef __FUNCT__
249669db28dcSHong Zhang #define __FUNCT__ "MatGetRedundantMatrix_MPIAIJ"
249769db28dcSHong Zhang PetscErrorCode MatGetRedundantMatrix_MPIAIJ(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,PetscInt mlocal_sub,MatReuse reuse,Mat *matredundant)
249869db28dcSHong Zhang {
249969db28dcSHong Zhang   PetscMPIInt    rank,size;
25007adad957SLisandro Dalcin   MPI_Comm       comm=((PetscObject)mat)->comm;
250169db28dcSHong Zhang   PetscErrorCode ierr;
250269db28dcSHong Zhang   PetscInt       nsends=0,nrecvs=0,i,rownz_max=0;
250369db28dcSHong Zhang   PetscMPIInt    *send_rank=PETSC_NULL,*recv_rank=PETSC_NULL;
2504d0f46423SBarry Smith   PetscInt       *rowrange=mat->rmap->range;
250569db28dcSHong Zhang   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
250669db28dcSHong Zhang   Mat            A=aij->A,B=aij->B,C=*matredundant;
250769db28dcSHong Zhang   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data,*b=(Mat_SeqAIJ*)B->data;
250869db28dcSHong Zhang   PetscScalar    *sbuf_a;
250969db28dcSHong Zhang   PetscInt       nzlocal=a->nz+b->nz;
2510d0f46423SBarry Smith   PetscInt       j,cstart=mat->cmap->rstart,cend=mat->cmap->rend,row,nzA,nzB,ncols,*cworkA,*cworkB;
2511d0f46423SBarry Smith   PetscInt       rstart=mat->rmap->rstart,rend=mat->rmap->rend,*bmap=aij->garray,M,N;
251269db28dcSHong Zhang   PetscInt       *cols,ctmp,lwrite,*rptr,l,*sbuf_j;
2513a77337e4SBarry Smith   MatScalar      *aworkA,*aworkB;
2514a77337e4SBarry Smith   PetscScalar    *vals;
251569db28dcSHong Zhang   PetscMPIInt    tag1,tag2,tag3,imdex;
251669db28dcSHong Zhang   MPI_Request    *s_waits1=PETSC_NULL,*s_waits2=PETSC_NULL,*s_waits3=PETSC_NULL,
251769db28dcSHong Zhang                  *r_waits1=PETSC_NULL,*r_waits2=PETSC_NULL,*r_waits3=PETSC_NULL;
251869db28dcSHong Zhang   MPI_Status     recv_status,*send_status;
251969db28dcSHong Zhang   PetscInt       *sbuf_nz=PETSC_NULL,*rbuf_nz=PETSC_NULL,count;
252069db28dcSHong Zhang   PetscInt       **rbuf_j=PETSC_NULL;
252169db28dcSHong Zhang   PetscScalar    **rbuf_a=PETSC_NULL;
252269db28dcSHong Zhang   Mat_Redundant  *redund=PETSC_NULL;
252369db28dcSHong Zhang   PetscContainer container;
252469db28dcSHong Zhang 
252569db28dcSHong Zhang   PetscFunctionBegin;
252669db28dcSHong Zhang   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
252769db28dcSHong Zhang   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
252869db28dcSHong Zhang 
252969db28dcSHong Zhang   if (reuse == MAT_REUSE_MATRIX) {
253069db28dcSHong Zhang     ierr = MatGetSize(C,&M,&N);CHKERRQ(ierr);
2531e32f2f54SBarry Smith     if (M != N || M != mat->rmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong global size");
253269db28dcSHong Zhang     ierr = MatGetLocalSize(C,&M,&N);CHKERRQ(ierr);
2533e32f2f54SBarry Smith     if (M != N || M != mlocal_sub) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong local size");
253469db28dcSHong Zhang     ierr = PetscObjectQuery((PetscObject)C,"Mat_Redundant",(PetscObject *)&container);CHKERRQ(ierr);
2535bf0cc555SLisandro Dalcin     if (!container) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Container does not exit");
253669db28dcSHong Zhang     ierr = PetscContainerGetPointer(container,(void **)&redund);CHKERRQ(ierr);
2537e32f2f54SBarry Smith     if (nzlocal != redund->nzlocal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong nzlocal");
253869db28dcSHong Zhang 
253969db28dcSHong Zhang     nsends    = redund->nsends;
254069db28dcSHong Zhang     nrecvs    = redund->nrecvs;
25411d79065fSBarry Smith     send_rank = redund->send_rank;
25421d79065fSBarry Smith     recv_rank = redund->recv_rank;
25431d79065fSBarry Smith     sbuf_nz   = redund->sbuf_nz;
25441d79065fSBarry Smith     rbuf_nz   = redund->rbuf_nz;
254569db28dcSHong Zhang     sbuf_j    = redund->sbuf_j;
254669db28dcSHong Zhang     sbuf_a    = redund->sbuf_a;
254769db28dcSHong Zhang     rbuf_j    = redund->rbuf_j;
254869db28dcSHong Zhang     rbuf_a    = redund->rbuf_a;
254969db28dcSHong Zhang   }
255069db28dcSHong Zhang 
255169db28dcSHong Zhang   if (reuse == MAT_INITIAL_MATRIX){
255269db28dcSHong Zhang     PetscMPIInt  subrank,subsize;
255369db28dcSHong Zhang     PetscInt     nleftover,np_subcomm;
255469db28dcSHong Zhang     /* get the destination processors' id send_rank, nsends and nrecvs */
255569db28dcSHong Zhang     ierr = MPI_Comm_rank(subcomm,&subrank);CHKERRQ(ierr);
255669db28dcSHong Zhang     ierr = MPI_Comm_size(subcomm,&subsize);CHKERRQ(ierr);
25571d79065fSBarry Smith     ierr = PetscMalloc2(size,PetscMPIInt,&send_rank,size,PetscMPIInt,&recv_rank);
255869db28dcSHong Zhang     np_subcomm = size/nsubcomm;
255969db28dcSHong Zhang     nleftover  = size - nsubcomm*np_subcomm;
256069db28dcSHong Zhang     nsends = 0; nrecvs = 0;
256169db28dcSHong Zhang     for (i=0; i<size; i++){ /* i=rank*/
256269db28dcSHong Zhang       if (subrank == i/nsubcomm && rank != i){ /* my_subrank == other's subrank */
256369db28dcSHong Zhang         send_rank[nsends] = i; nsends++;
256469db28dcSHong Zhang         recv_rank[nrecvs++] = i;
256569db28dcSHong Zhang       }
256669db28dcSHong Zhang     }
256769db28dcSHong Zhang     if (rank >= size - nleftover){/* this proc is a leftover processor */
256869db28dcSHong Zhang       i = size-nleftover-1;
256969db28dcSHong Zhang       j = 0;
257069db28dcSHong Zhang       while (j < nsubcomm - nleftover){
257169db28dcSHong Zhang         send_rank[nsends++] = i;
257269db28dcSHong Zhang         i--; j++;
257369db28dcSHong Zhang       }
257469db28dcSHong Zhang     }
257569db28dcSHong Zhang 
257669db28dcSHong Zhang     if (nleftover && subsize == size/nsubcomm && subrank==subsize-1){ /* this proc recvs from leftover processors */
257769db28dcSHong Zhang       for (i=0; i<nleftover; i++){
257869db28dcSHong Zhang         recv_rank[nrecvs++] = size-nleftover+i;
257969db28dcSHong Zhang       }
258069db28dcSHong Zhang     }
258169db28dcSHong Zhang 
258269db28dcSHong Zhang     /* allocate sbuf_j, sbuf_a */
258369db28dcSHong Zhang     i = nzlocal + rowrange[rank+1] - rowrange[rank] + 2;
258469db28dcSHong Zhang     ierr = PetscMalloc(i*sizeof(PetscInt),&sbuf_j);CHKERRQ(ierr);
258569db28dcSHong Zhang     ierr = PetscMalloc((nzlocal+1)*sizeof(PetscScalar),&sbuf_a);CHKERRQ(ierr);
258669db28dcSHong Zhang   } /* endof if (reuse == MAT_INITIAL_MATRIX) */
258769db28dcSHong Zhang 
258869db28dcSHong Zhang   /* copy mat's local entries into the buffers */
258969db28dcSHong Zhang   if (reuse == MAT_INITIAL_MATRIX){
259069db28dcSHong Zhang     rownz_max = 0;
259169db28dcSHong Zhang     rptr = sbuf_j;
259269db28dcSHong Zhang     cols = sbuf_j + rend-rstart + 1;
259369db28dcSHong Zhang     vals = sbuf_a;
259469db28dcSHong Zhang     rptr[0] = 0;
259569db28dcSHong Zhang     for (i=0; i<rend-rstart; i++){
259669db28dcSHong Zhang       row = i + rstart;
259769db28dcSHong Zhang       nzA    = a->i[i+1] - a->i[i]; nzB = b->i[i+1] - b->i[i];
259869db28dcSHong Zhang       ncols  = nzA + nzB;
259969db28dcSHong Zhang       cworkA = a->j + a->i[i]; cworkB = b->j + b->i[i];
260069db28dcSHong Zhang       aworkA = a->a + a->i[i]; aworkB = b->a + b->i[i];
260169db28dcSHong Zhang       /* load the column indices for this row into cols */
260269db28dcSHong Zhang       lwrite = 0;
260369db28dcSHong Zhang       for (l=0; l<nzB; l++) {
260469db28dcSHong Zhang         if ((ctmp = bmap[cworkB[l]]) < cstart){
260569db28dcSHong Zhang           vals[lwrite]   = aworkB[l];
260669db28dcSHong Zhang           cols[lwrite++] = ctmp;
260769db28dcSHong Zhang         }
260869db28dcSHong Zhang       }
260969db28dcSHong Zhang       for (l=0; l<nzA; l++){
261069db28dcSHong Zhang         vals[lwrite]   = aworkA[l];
261169db28dcSHong Zhang         cols[lwrite++] = cstart + cworkA[l];
261269db28dcSHong Zhang       }
261369db28dcSHong Zhang       for (l=0; l<nzB; l++) {
261469db28dcSHong Zhang         if ((ctmp = bmap[cworkB[l]]) >= cend){
261569db28dcSHong Zhang           vals[lwrite]   = aworkB[l];
261669db28dcSHong Zhang           cols[lwrite++] = ctmp;
261769db28dcSHong Zhang         }
261869db28dcSHong Zhang       }
261969db28dcSHong Zhang       vals += ncols;
262069db28dcSHong Zhang       cols += ncols;
262169db28dcSHong Zhang       rptr[i+1] = rptr[i] + ncols;
262269db28dcSHong Zhang       if (rownz_max < ncols) rownz_max = ncols;
262369db28dcSHong Zhang     }
2624e32f2f54SBarry Smith     if (rptr[rend-rstart] != a->nz + b->nz) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_PLIB, "rptr[%d] %d != %d + %d",rend-rstart,rptr[rend-rstart+1],a->nz,b->nz);
262569db28dcSHong Zhang   } else { /* only copy matrix values into sbuf_a */
262669db28dcSHong Zhang     rptr = sbuf_j;
262769db28dcSHong Zhang     vals = sbuf_a;
262869db28dcSHong Zhang     rptr[0] = 0;
262969db28dcSHong Zhang     for (i=0; i<rend-rstart; i++){
263069db28dcSHong Zhang       row = i + rstart;
263169db28dcSHong Zhang       nzA    = a->i[i+1] - a->i[i]; nzB = b->i[i+1] - b->i[i];
263269db28dcSHong Zhang       ncols  = nzA + nzB;
263369db28dcSHong Zhang       cworkA = a->j + a->i[i]; cworkB = b->j + b->i[i];
263469db28dcSHong Zhang       aworkA = a->a + a->i[i]; aworkB = b->a + b->i[i];
263569db28dcSHong Zhang       lwrite = 0;
263669db28dcSHong Zhang       for (l=0; l<nzB; l++) {
263769db28dcSHong Zhang         if ((ctmp = bmap[cworkB[l]]) < cstart) vals[lwrite++] = aworkB[l];
263869db28dcSHong Zhang       }
263969db28dcSHong Zhang       for (l=0; l<nzA; l++) vals[lwrite++] = aworkA[l];
264069db28dcSHong Zhang       for (l=0; l<nzB; l++) {
264169db28dcSHong Zhang         if ((ctmp = bmap[cworkB[l]]) >= cend) vals[lwrite++] = aworkB[l];
264269db28dcSHong Zhang       }
264369db28dcSHong Zhang       vals += ncols;
264469db28dcSHong Zhang       rptr[i+1] = rptr[i] + ncols;
264569db28dcSHong Zhang     }
264669db28dcSHong Zhang   } /* endof if (reuse == MAT_INITIAL_MATRIX) */
264769db28dcSHong Zhang 
264869db28dcSHong Zhang   /* send nzlocal to others, and recv other's nzlocal */
264969db28dcSHong Zhang   /*--------------------------------------------------*/
265069db28dcSHong Zhang   if (reuse == MAT_INITIAL_MATRIX){
265169db28dcSHong Zhang     ierr = PetscMalloc2(3*(nsends + nrecvs)+1,MPI_Request,&s_waits3,nsends+1,MPI_Status,&send_status);CHKERRQ(ierr);
265269db28dcSHong Zhang     s_waits2 = s_waits3 + nsends;
265369db28dcSHong Zhang     s_waits1 = s_waits2 + nsends;
265469db28dcSHong Zhang     r_waits1 = s_waits1 + nsends;
265569db28dcSHong Zhang     r_waits2 = r_waits1 + nrecvs;
265669db28dcSHong Zhang     r_waits3 = r_waits2 + nrecvs;
265769db28dcSHong Zhang   } else {
265869db28dcSHong Zhang     ierr = PetscMalloc2(nsends + nrecvs +1,MPI_Request,&s_waits3,nsends+1,MPI_Status,&send_status);CHKERRQ(ierr);
265969db28dcSHong Zhang     r_waits3 = s_waits3 + nsends;
266069db28dcSHong Zhang   }
266169db28dcSHong Zhang 
266269db28dcSHong Zhang   ierr = PetscObjectGetNewTag((PetscObject)mat,&tag3);CHKERRQ(ierr);
266369db28dcSHong Zhang   if (reuse == MAT_INITIAL_MATRIX){
266469db28dcSHong Zhang     /* get new tags to keep the communication clean */
266569db28dcSHong Zhang     ierr = PetscObjectGetNewTag((PetscObject)mat,&tag1);CHKERRQ(ierr);
266669db28dcSHong Zhang     ierr = PetscObjectGetNewTag((PetscObject)mat,&tag2);CHKERRQ(ierr);
26671d79065fSBarry Smith     ierr = PetscMalloc4(nsends,PetscInt,&sbuf_nz,nrecvs,PetscInt,&rbuf_nz,nrecvs,PetscInt*,&rbuf_j,nrecvs,PetscScalar*,&rbuf_a);CHKERRQ(ierr);
266869db28dcSHong Zhang 
266969db28dcSHong Zhang     /* post receives of other's nzlocal */
267069db28dcSHong Zhang     for (i=0; i<nrecvs; i++){
267169db28dcSHong Zhang       ierr = MPI_Irecv(rbuf_nz+i,1,MPIU_INT,MPI_ANY_SOURCE,tag1,comm,r_waits1+i);CHKERRQ(ierr);
267269db28dcSHong Zhang     }
267369db28dcSHong Zhang     /* send nzlocal to others */
267469db28dcSHong Zhang     for (i=0; i<nsends; i++){
267569db28dcSHong Zhang       sbuf_nz[i] = nzlocal;
267669db28dcSHong Zhang       ierr = MPI_Isend(sbuf_nz+i,1,MPIU_INT,send_rank[i],tag1,comm,s_waits1+i);CHKERRQ(ierr);
267769db28dcSHong Zhang     }
267869db28dcSHong Zhang     /* wait on receives of nzlocal; allocate space for rbuf_j, rbuf_a */
267969db28dcSHong Zhang     count = nrecvs;
268069db28dcSHong Zhang     while (count) {
268169db28dcSHong Zhang       ierr = MPI_Waitany(nrecvs,r_waits1,&imdex,&recv_status);CHKERRQ(ierr);
268269db28dcSHong Zhang       recv_rank[imdex] = recv_status.MPI_SOURCE;
268369db28dcSHong Zhang       /* allocate rbuf_a and rbuf_j; then post receives of rbuf_j */
268469db28dcSHong Zhang       ierr = PetscMalloc((rbuf_nz[imdex]+1)*sizeof(PetscScalar),&rbuf_a[imdex]);CHKERRQ(ierr);
268569db28dcSHong Zhang 
268669db28dcSHong Zhang       i = rowrange[recv_status.MPI_SOURCE+1] - rowrange[recv_status.MPI_SOURCE]; /* number of expected mat->i */
268769db28dcSHong Zhang       rbuf_nz[imdex] += i + 2;
268869db28dcSHong Zhang       ierr = PetscMalloc(rbuf_nz[imdex]*sizeof(PetscInt),&rbuf_j[imdex]);CHKERRQ(ierr);
268969db28dcSHong Zhang       ierr = MPI_Irecv(rbuf_j[imdex],rbuf_nz[imdex],MPIU_INT,recv_status.MPI_SOURCE,tag2,comm,r_waits2+imdex);CHKERRQ(ierr);
269069db28dcSHong Zhang       count--;
269169db28dcSHong Zhang     }
269269db28dcSHong Zhang     /* wait on sends of nzlocal */
269369db28dcSHong Zhang     if (nsends) {ierr = MPI_Waitall(nsends,s_waits1,send_status);CHKERRQ(ierr);}
269469db28dcSHong Zhang     /* send mat->i,j to others, and recv from other's */
269569db28dcSHong Zhang     /*------------------------------------------------*/
269669db28dcSHong Zhang     for (i=0; i<nsends; i++){
269769db28dcSHong Zhang       j = nzlocal + rowrange[rank+1] - rowrange[rank] + 1;
269869db28dcSHong Zhang       ierr = MPI_Isend(sbuf_j,j,MPIU_INT,send_rank[i],tag2,comm,s_waits2+i);CHKERRQ(ierr);
269969db28dcSHong Zhang     }
270069db28dcSHong Zhang     /* wait on receives of mat->i,j */
270169db28dcSHong Zhang     /*------------------------------*/
270269db28dcSHong Zhang     count = nrecvs;
270369db28dcSHong Zhang     while (count) {
270469db28dcSHong Zhang       ierr = MPI_Waitany(nrecvs,r_waits2,&imdex,&recv_status);CHKERRQ(ierr);
2705e32f2f54SBarry Smith       if (recv_rank[imdex] != recv_status.MPI_SOURCE) SETERRQ2(PETSC_COMM_SELF,1, "recv_rank %d != MPI_SOURCE %d",recv_rank[imdex],recv_status.MPI_SOURCE);
270669db28dcSHong Zhang       count--;
270769db28dcSHong Zhang     }
270869db28dcSHong Zhang     /* wait on sends of mat->i,j */
270969db28dcSHong Zhang     /*---------------------------*/
271069db28dcSHong Zhang     if (nsends) {
271169db28dcSHong Zhang       ierr = MPI_Waitall(nsends,s_waits2,send_status);CHKERRQ(ierr);
271269db28dcSHong Zhang     }
271369db28dcSHong Zhang   } /* endof if (reuse == MAT_INITIAL_MATRIX) */
271469db28dcSHong Zhang 
271569db28dcSHong Zhang   /* post receives, send and receive mat->a */
271669db28dcSHong Zhang   /*----------------------------------------*/
271769db28dcSHong Zhang   for (imdex=0; imdex<nrecvs; imdex++) {
271869db28dcSHong Zhang     ierr = MPI_Irecv(rbuf_a[imdex],rbuf_nz[imdex],MPIU_SCALAR,recv_rank[imdex],tag3,comm,r_waits3+imdex);CHKERRQ(ierr);
271969db28dcSHong Zhang   }
272069db28dcSHong Zhang   for (i=0; i<nsends; i++){
272169db28dcSHong Zhang     ierr = MPI_Isend(sbuf_a,nzlocal,MPIU_SCALAR,send_rank[i],tag3,comm,s_waits3+i);CHKERRQ(ierr);
272269db28dcSHong Zhang   }
272369db28dcSHong Zhang   count = nrecvs;
272469db28dcSHong Zhang   while (count) {
272569db28dcSHong Zhang     ierr = MPI_Waitany(nrecvs,r_waits3,&imdex,&recv_status);CHKERRQ(ierr);
2726e32f2f54SBarry Smith     if (recv_rank[imdex] != recv_status.MPI_SOURCE) SETERRQ2(PETSC_COMM_SELF,1, "recv_rank %d != MPI_SOURCE %d",recv_rank[imdex],recv_status.MPI_SOURCE);
272769db28dcSHong Zhang     count--;
272869db28dcSHong Zhang   }
272969db28dcSHong Zhang   if (nsends) {
273069db28dcSHong Zhang     ierr = MPI_Waitall(nsends,s_waits3,send_status);CHKERRQ(ierr);
273169db28dcSHong Zhang   }
273269db28dcSHong Zhang 
273369db28dcSHong Zhang   ierr = PetscFree2(s_waits3,send_status);CHKERRQ(ierr);
273469db28dcSHong Zhang 
273569db28dcSHong Zhang   /* create redundant matrix */
273669db28dcSHong Zhang   /*-------------------------*/
273769db28dcSHong Zhang   if (reuse == MAT_INITIAL_MATRIX){
273869db28dcSHong Zhang     /* compute rownz_max for preallocation */
273969db28dcSHong Zhang     for (imdex=0; imdex<nrecvs; imdex++){
274069db28dcSHong Zhang       j = rowrange[recv_rank[imdex]+1] - rowrange[recv_rank[imdex]];
274169db28dcSHong Zhang       rptr = rbuf_j[imdex];
274269db28dcSHong Zhang       for (i=0; i<j; i++){
274369db28dcSHong Zhang         ncols = rptr[i+1] - rptr[i];
274469db28dcSHong Zhang         if (rownz_max < ncols) rownz_max = ncols;
274569db28dcSHong Zhang       }
274669db28dcSHong Zhang     }
274769db28dcSHong Zhang 
274869db28dcSHong Zhang     ierr = MatCreate(subcomm,&C);CHKERRQ(ierr);
274969db28dcSHong Zhang     ierr = MatSetSizes(C,mlocal_sub,mlocal_sub,PETSC_DECIDE,PETSC_DECIDE);CHKERRQ(ierr);
2750a2f3521dSMark F. Adams     ierr = MatSetBlockSizes(C,mat->rmap->bs,mat->cmap->bs); CHKERRQ(ierr);
275169db28dcSHong Zhang     ierr = MatSetFromOptions(C);CHKERRQ(ierr);
275269db28dcSHong Zhang     ierr = MatSeqAIJSetPreallocation(C,rownz_max,PETSC_NULL);CHKERRQ(ierr);
275369db28dcSHong Zhang     ierr = MatMPIAIJSetPreallocation(C,rownz_max,PETSC_NULL,rownz_max,PETSC_NULL);CHKERRQ(ierr);
275469db28dcSHong Zhang   } else {
275569db28dcSHong Zhang     C = *matredundant;
275669db28dcSHong Zhang   }
275769db28dcSHong Zhang 
275869db28dcSHong Zhang   /* insert local matrix entries */
275969db28dcSHong Zhang   rptr = sbuf_j;
276069db28dcSHong Zhang   cols = sbuf_j + rend-rstart + 1;
276169db28dcSHong Zhang   vals = sbuf_a;
276269db28dcSHong Zhang   for (i=0; i<rend-rstart; i++){
276369db28dcSHong Zhang     row   = i + rstart;
276469db28dcSHong Zhang     ncols = rptr[i+1] - rptr[i];
276569db28dcSHong Zhang     ierr = MatSetValues(C,1,&row,ncols,cols,vals,INSERT_VALUES);CHKERRQ(ierr);
276669db28dcSHong Zhang     vals += ncols;
276769db28dcSHong Zhang     cols += ncols;
276869db28dcSHong Zhang   }
276969db28dcSHong Zhang   /* insert received matrix entries */
277069db28dcSHong Zhang   for (imdex=0; imdex<nrecvs; imdex++){
277169db28dcSHong Zhang     rstart = rowrange[recv_rank[imdex]];
277269db28dcSHong Zhang     rend   = rowrange[recv_rank[imdex]+1];
277369db28dcSHong Zhang     rptr = rbuf_j[imdex];
277469db28dcSHong Zhang     cols = rbuf_j[imdex] + rend-rstart + 1;
277569db28dcSHong Zhang     vals = rbuf_a[imdex];
277669db28dcSHong Zhang     for (i=0; i<rend-rstart; i++){
277769db28dcSHong Zhang       row   = i + rstart;
277869db28dcSHong Zhang       ncols = rptr[i+1] - rptr[i];
277969db28dcSHong Zhang       ierr = MatSetValues(C,1,&row,ncols,cols,vals,INSERT_VALUES);CHKERRQ(ierr);
278069db28dcSHong Zhang       vals += ncols;
278169db28dcSHong Zhang       cols += ncols;
278269db28dcSHong Zhang     }
278369db28dcSHong Zhang   }
278469db28dcSHong Zhang   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
278569db28dcSHong Zhang   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
278669db28dcSHong Zhang   ierr = MatGetSize(C,&M,&N);CHKERRQ(ierr);
2787e32f2f54SBarry Smith   if (M != mat->rmap->N || N != mat->cmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"redundant mat size %d != input mat size %d",M,mat->rmap->N);
278869db28dcSHong Zhang   if (reuse == MAT_INITIAL_MATRIX) {
278969db28dcSHong Zhang     PetscContainer container;
279069db28dcSHong Zhang     *matredundant = C;
279169db28dcSHong Zhang     /* create a supporting struct and attach it to C for reuse */
279238f2d2fdSLisandro Dalcin     ierr = PetscNewLog(C,Mat_Redundant,&redund);CHKERRQ(ierr);
279369db28dcSHong Zhang     ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr);
279469db28dcSHong Zhang     ierr = PetscContainerSetPointer(container,redund);CHKERRQ(ierr);
279569db28dcSHong Zhang     ierr = PetscContainerSetUserDestroy(container,PetscContainerDestroy_MatRedundant);CHKERRQ(ierr);
2796bf0cc555SLisandro Dalcin     ierr = PetscObjectCompose((PetscObject)C,"Mat_Redundant",(PetscObject)container);CHKERRQ(ierr);
2797bf0cc555SLisandro Dalcin     ierr = PetscContainerDestroy(&container);CHKERRQ(ierr);
279869db28dcSHong Zhang 
279969db28dcSHong Zhang     redund->nzlocal = nzlocal;
280069db28dcSHong Zhang     redund->nsends  = nsends;
280169db28dcSHong Zhang     redund->nrecvs  = nrecvs;
280269db28dcSHong Zhang     redund->send_rank = send_rank;
28031d79065fSBarry Smith     redund->recv_rank = recv_rank;
280469db28dcSHong Zhang     redund->sbuf_nz = sbuf_nz;
28051d79065fSBarry Smith     redund->rbuf_nz = rbuf_nz;
280669db28dcSHong Zhang     redund->sbuf_j  = sbuf_j;
280769db28dcSHong Zhang     redund->sbuf_a  = sbuf_a;
280869db28dcSHong Zhang     redund->rbuf_j  = rbuf_j;
280969db28dcSHong Zhang     redund->rbuf_a  = rbuf_a;
281069db28dcSHong Zhang 
2811bf0cc555SLisandro Dalcin     redund->Destroy = C->ops->destroy;
281269db28dcSHong Zhang     C->ops->destroy = MatDestroy_MatRedundant;
281369db28dcSHong Zhang   }
281469db28dcSHong Zhang   PetscFunctionReturn(0);
281569db28dcSHong Zhang }
281669db28dcSHong Zhang 
281703bc72f1SMatthew Knepley #undef __FUNCT__
2818c91732d9SHong Zhang #define __FUNCT__ "MatGetRowMaxAbs_MPIAIJ"
2819c91732d9SHong Zhang PetscErrorCode MatGetRowMaxAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[])
2820c91732d9SHong Zhang {
2821c91732d9SHong Zhang   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
2822c91732d9SHong Zhang   PetscErrorCode ierr;
2823c91732d9SHong Zhang   PetscInt       i,*idxb = 0;
2824c91732d9SHong Zhang   PetscScalar    *va,*vb;
2825c91732d9SHong Zhang   Vec            vtmp;
2826c91732d9SHong Zhang 
2827c91732d9SHong Zhang   PetscFunctionBegin;
2828c91732d9SHong Zhang   ierr = MatGetRowMaxAbs(a->A,v,idx);CHKERRQ(ierr);
2829c91732d9SHong Zhang   ierr = VecGetArray(v,&va);CHKERRQ(ierr);
2830c91732d9SHong Zhang   if (idx) {
2831192daf7cSBarry Smith     for (i=0; i<A->rmap->n; i++) {
2832d0f46423SBarry Smith       if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart;
2833c91732d9SHong Zhang     }
2834c91732d9SHong Zhang   }
2835c91732d9SHong Zhang 
2836d0f46423SBarry Smith   ierr = VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);CHKERRQ(ierr);
2837c91732d9SHong Zhang   if (idx) {
2838d0f46423SBarry Smith     ierr = PetscMalloc(A->rmap->n*sizeof(PetscInt),&idxb);CHKERRQ(ierr);
2839c91732d9SHong Zhang   }
2840c91732d9SHong Zhang   ierr = MatGetRowMaxAbs(a->B,vtmp,idxb);CHKERRQ(ierr);
2841c91732d9SHong Zhang   ierr = VecGetArray(vtmp,&vb);CHKERRQ(ierr);
2842c91732d9SHong Zhang 
2843d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++){
2844c91732d9SHong Zhang     if (PetscAbsScalar(va[i]) < PetscAbsScalar(vb[i])) {
2845c91732d9SHong Zhang       va[i] = vb[i];
2846c91732d9SHong Zhang       if (idx) idx[i] = a->garray[idxb[i]];
2847c91732d9SHong Zhang     }
2848c91732d9SHong Zhang   }
2849c91732d9SHong Zhang 
2850c91732d9SHong Zhang   ierr = VecRestoreArray(v,&va);CHKERRQ(ierr);
2851c91732d9SHong Zhang   ierr = VecRestoreArray(vtmp,&vb);CHKERRQ(ierr);
2852c91732d9SHong Zhang   ierr = PetscFree(idxb);CHKERRQ(ierr);
28536bf464f9SBarry Smith   ierr = VecDestroy(&vtmp);CHKERRQ(ierr);
2854c91732d9SHong Zhang   PetscFunctionReturn(0);
2855c91732d9SHong Zhang }
2856c91732d9SHong Zhang 
2857c91732d9SHong Zhang #undef __FUNCT__
2858c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_MPIAIJ"
2859c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[])
2860c87e5d42SMatthew Knepley {
2861c87e5d42SMatthew Knepley   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
2862c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2863c87e5d42SMatthew Knepley   PetscInt       i,*idxb = 0;
2864c87e5d42SMatthew Knepley   PetscScalar    *va,*vb;
2865c87e5d42SMatthew Knepley   Vec            vtmp;
2866c87e5d42SMatthew Knepley 
2867c87e5d42SMatthew Knepley   PetscFunctionBegin;
2868c87e5d42SMatthew Knepley   ierr = MatGetRowMinAbs(a->A,v,idx);CHKERRQ(ierr);
2869c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&va);CHKERRQ(ierr);
2870c87e5d42SMatthew Knepley   if (idx) {
2871c87e5d42SMatthew Knepley     for (i=0; i<A->cmap->n; i++) {
2872c87e5d42SMatthew Knepley       if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart;
2873c87e5d42SMatthew Knepley     }
2874c87e5d42SMatthew Knepley   }
2875c87e5d42SMatthew Knepley 
2876c87e5d42SMatthew Knepley   ierr = VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);CHKERRQ(ierr);
2877c87e5d42SMatthew Knepley   if (idx) {
2878c87e5d42SMatthew Knepley     ierr = PetscMalloc(A->rmap->n*sizeof(PetscInt),&idxb);CHKERRQ(ierr);
2879c87e5d42SMatthew Knepley   }
2880c87e5d42SMatthew Knepley   ierr = MatGetRowMinAbs(a->B,vtmp,idxb);CHKERRQ(ierr);
2881c87e5d42SMatthew Knepley   ierr = VecGetArray(vtmp,&vb);CHKERRQ(ierr);
2882c87e5d42SMatthew Knepley 
2883c87e5d42SMatthew Knepley   for (i=0; i<A->rmap->n; i++){
2884c87e5d42SMatthew Knepley     if (PetscAbsScalar(va[i]) > PetscAbsScalar(vb[i])) {
2885c87e5d42SMatthew Knepley       va[i] = vb[i];
2886c87e5d42SMatthew Knepley       if (idx) idx[i] = a->garray[idxb[i]];
2887c87e5d42SMatthew Knepley     }
2888c87e5d42SMatthew Knepley   }
2889c87e5d42SMatthew Knepley 
2890c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&va);CHKERRQ(ierr);
2891c87e5d42SMatthew Knepley   ierr = VecRestoreArray(vtmp,&vb);CHKERRQ(ierr);
2892c87e5d42SMatthew Knepley   ierr = PetscFree(idxb);CHKERRQ(ierr);
28936bf464f9SBarry Smith   ierr = VecDestroy(&vtmp);CHKERRQ(ierr);
2894c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
2895c87e5d42SMatthew Knepley }
2896c87e5d42SMatthew Knepley 
2897c87e5d42SMatthew Knepley #undef __FUNCT__
289803bc72f1SMatthew Knepley #define __FUNCT__ "MatGetRowMin_MPIAIJ"
289903bc72f1SMatthew Knepley PetscErrorCode MatGetRowMin_MPIAIJ(Mat A, Vec v, PetscInt idx[])
290003bc72f1SMatthew Knepley {
290103bc72f1SMatthew Knepley   Mat_MPIAIJ    *mat    = (Mat_MPIAIJ *) A->data;
2902d0f46423SBarry Smith   PetscInt       n      = A->rmap->n;
2903d0f46423SBarry Smith   PetscInt       cstart = A->cmap->rstart;
290403bc72f1SMatthew Knepley   PetscInt      *cmap   = mat->garray;
290503bc72f1SMatthew Knepley   PetscInt      *diagIdx, *offdiagIdx;
290603bc72f1SMatthew Knepley   Vec            diagV, offdiagV;
290703bc72f1SMatthew Knepley   PetscScalar   *a, *diagA, *offdiagA;
290803bc72f1SMatthew Knepley   PetscInt       r;
290903bc72f1SMatthew Knepley   PetscErrorCode ierr;
291003bc72f1SMatthew Knepley 
291103bc72f1SMatthew Knepley   PetscFunctionBegin;
291203bc72f1SMatthew Knepley   ierr = PetscMalloc2(n,PetscInt,&diagIdx,n,PetscInt,&offdiagIdx);CHKERRQ(ierr);
2913e64afeacSLisandro Dalcin   ierr = VecCreateSeq(((PetscObject)A)->comm, n, &diagV);CHKERRQ(ierr);
2914e64afeacSLisandro Dalcin   ierr = VecCreateSeq(((PetscObject)A)->comm, n, &offdiagV);CHKERRQ(ierr);
291503bc72f1SMatthew Knepley   ierr = MatGetRowMin(mat->A, diagV,    diagIdx);CHKERRQ(ierr);
291603bc72f1SMatthew Knepley   ierr = MatGetRowMin(mat->B, offdiagV, offdiagIdx);CHKERRQ(ierr);
291703bc72f1SMatthew Knepley   ierr = VecGetArray(v,        &a);CHKERRQ(ierr);
291803bc72f1SMatthew Knepley   ierr = VecGetArray(diagV,    &diagA);CHKERRQ(ierr);
291903bc72f1SMatthew Knepley   ierr = VecGetArray(offdiagV, &offdiagA);CHKERRQ(ierr);
292003bc72f1SMatthew Knepley   for(r = 0; r < n; ++r) {
2921028cd4eaSSatish Balay     if (PetscAbsScalar(diagA[r]) <= PetscAbsScalar(offdiagA[r])) {
292203bc72f1SMatthew Knepley       a[r]   = diagA[r];
292303bc72f1SMatthew Knepley       idx[r] = cstart + diagIdx[r];
292403bc72f1SMatthew Knepley     } else {
292503bc72f1SMatthew Knepley       a[r]   = offdiagA[r];
292603bc72f1SMatthew Knepley       idx[r] = cmap[offdiagIdx[r]];
292703bc72f1SMatthew Knepley     }
292803bc72f1SMatthew Knepley   }
292903bc72f1SMatthew Knepley   ierr = VecRestoreArray(v,        &a);CHKERRQ(ierr);
293003bc72f1SMatthew Knepley   ierr = VecRestoreArray(diagV,    &diagA);CHKERRQ(ierr);
293103bc72f1SMatthew Knepley   ierr = VecRestoreArray(offdiagV, &offdiagA);CHKERRQ(ierr);
29326bf464f9SBarry Smith   ierr = VecDestroy(&diagV);CHKERRQ(ierr);
29336bf464f9SBarry Smith   ierr = VecDestroy(&offdiagV);CHKERRQ(ierr);
293403bc72f1SMatthew Knepley   ierr = PetscFree2(diagIdx, offdiagIdx);CHKERRQ(ierr);
293503bc72f1SMatthew Knepley   PetscFunctionReturn(0);
293603bc72f1SMatthew Knepley }
293703bc72f1SMatthew Knepley 
29385494a064SHong Zhang #undef __FUNCT__
2939c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMax_MPIAIJ"
2940c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMax_MPIAIJ(Mat A, Vec v, PetscInt idx[])
2941c87e5d42SMatthew Knepley {
2942c87e5d42SMatthew Knepley   Mat_MPIAIJ    *mat    = (Mat_MPIAIJ *) A->data;
2943c87e5d42SMatthew Knepley   PetscInt       n      = A->rmap->n;
2944c87e5d42SMatthew Knepley   PetscInt       cstart = A->cmap->rstart;
2945c87e5d42SMatthew Knepley   PetscInt      *cmap   = mat->garray;
2946c87e5d42SMatthew Knepley   PetscInt      *diagIdx, *offdiagIdx;
2947c87e5d42SMatthew Knepley   Vec            diagV, offdiagV;
2948c87e5d42SMatthew Knepley   PetscScalar   *a, *diagA, *offdiagA;
2949c87e5d42SMatthew Knepley   PetscInt       r;
2950c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2951c87e5d42SMatthew Knepley 
2952c87e5d42SMatthew Knepley   PetscFunctionBegin;
2953c87e5d42SMatthew Knepley   ierr = PetscMalloc2(n,PetscInt,&diagIdx,n,PetscInt,&offdiagIdx);CHKERRQ(ierr);
2954c87e5d42SMatthew Knepley   ierr = VecCreateSeq(((PetscObject)A)->comm, n, &diagV);CHKERRQ(ierr);
2955c87e5d42SMatthew Knepley   ierr = VecCreateSeq(((PetscObject)A)->comm, n, &offdiagV);CHKERRQ(ierr);
2956c87e5d42SMatthew Knepley   ierr = MatGetRowMax(mat->A, diagV,    diagIdx);CHKERRQ(ierr);
2957c87e5d42SMatthew Knepley   ierr = MatGetRowMax(mat->B, offdiagV, offdiagIdx);CHKERRQ(ierr);
2958c87e5d42SMatthew Knepley   ierr = VecGetArray(v,        &a);CHKERRQ(ierr);
2959c87e5d42SMatthew Knepley   ierr = VecGetArray(diagV,    &diagA);CHKERRQ(ierr);
2960c87e5d42SMatthew Knepley   ierr = VecGetArray(offdiagV, &offdiagA);CHKERRQ(ierr);
2961c87e5d42SMatthew Knepley   for(r = 0; r < n; ++r) {
2962c87e5d42SMatthew Knepley     if (PetscAbsScalar(diagA[r]) >= PetscAbsScalar(offdiagA[r])) {
2963c87e5d42SMatthew Knepley       a[r]   = diagA[r];
2964c87e5d42SMatthew Knepley       idx[r] = cstart + diagIdx[r];
2965c87e5d42SMatthew Knepley     } else {
2966c87e5d42SMatthew Knepley       a[r]   = offdiagA[r];
2967c87e5d42SMatthew Knepley       idx[r] = cmap[offdiagIdx[r]];
2968c87e5d42SMatthew Knepley     }
2969c87e5d42SMatthew Knepley   }
2970c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,        &a);CHKERRQ(ierr);
2971c87e5d42SMatthew Knepley   ierr = VecRestoreArray(diagV,    &diagA);CHKERRQ(ierr);
2972c87e5d42SMatthew Knepley   ierr = VecRestoreArray(offdiagV, &offdiagA);CHKERRQ(ierr);
29736bf464f9SBarry Smith   ierr = VecDestroy(&diagV);CHKERRQ(ierr);
29746bf464f9SBarry Smith   ierr = VecDestroy(&offdiagV);CHKERRQ(ierr);
2975c87e5d42SMatthew Knepley   ierr = PetscFree2(diagIdx, offdiagIdx);CHKERRQ(ierr);
2976c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
2977c87e5d42SMatthew Knepley }
2978c87e5d42SMatthew Knepley 
2979c87e5d42SMatthew Knepley #undef __FUNCT__
2980d1adec66SJed Brown #define __FUNCT__ "MatGetSeqNonzeroStructure_MPIAIJ"
2981d1adec66SJed Brown PetscErrorCode MatGetSeqNonzeroStructure_MPIAIJ(Mat mat,Mat *newmat)
29825494a064SHong Zhang {
29835494a064SHong Zhang   PetscErrorCode ierr;
2984f6d58c54SBarry Smith   Mat            *dummy;
29855494a064SHong Zhang 
29865494a064SHong Zhang   PetscFunctionBegin;
2987f6d58c54SBarry Smith   ierr = MatGetSubMatrix_MPIAIJ_All(mat,MAT_DO_NOT_GET_VALUES,MAT_INITIAL_MATRIX,&dummy);CHKERRQ(ierr);
2988f6d58c54SBarry Smith   *newmat = *dummy;
2989f6d58c54SBarry Smith   ierr = PetscFree(dummy);CHKERRQ(ierr);
29905494a064SHong Zhang   PetscFunctionReturn(0);
29915494a064SHong Zhang }
29925494a064SHong Zhang 
29937087cfbeSBarry Smith extern PetscErrorCode  MatFDColoringApply_AIJ(Mat,MatFDColoring,Vec,MatStructure*,void*);
2994bbead8a2SBarry Smith 
2995bbead8a2SBarry Smith #undef __FUNCT__
2996bbead8a2SBarry Smith #define __FUNCT__ "MatInvertBlockDiagonal_MPIAIJ"
2997713ccfa9SJed Brown PetscErrorCode  MatInvertBlockDiagonal_MPIAIJ(Mat A,const PetscScalar **values)
2998bbead8a2SBarry Smith {
2999bbead8a2SBarry Smith   Mat_MPIAIJ    *a = (Mat_MPIAIJ*) A->data;
3000bbead8a2SBarry Smith   PetscErrorCode ierr;
3001bbead8a2SBarry Smith 
3002bbead8a2SBarry Smith   PetscFunctionBegin;
3003bbead8a2SBarry Smith   ierr = MatInvertBlockDiagonal(a->A,values);CHKERRQ(ierr);
3004bbead8a2SBarry Smith   PetscFunctionReturn(0);
3005bbead8a2SBarry Smith }
3006bbead8a2SBarry Smith 
3007bbead8a2SBarry Smith 
30088a729477SBarry Smith /* -------------------------------------------------------------------*/
3009cda55fadSBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ,
3010cda55fadSBarry Smith        MatGetRow_MPIAIJ,
3011cda55fadSBarry Smith        MatRestoreRow_MPIAIJ,
3012cda55fadSBarry Smith        MatMult_MPIAIJ,
301397304618SKris Buschelman /* 4*/ MatMultAdd_MPIAIJ,
30147c922b88SBarry Smith        MatMultTranspose_MPIAIJ,
30157c922b88SBarry Smith        MatMultTransposeAdd_MPIAIJ,
3016103bf8bdSMatthew Knepley #ifdef PETSC_HAVE_PBGL
3017103bf8bdSMatthew Knepley        MatSolve_MPIAIJ,
3018103bf8bdSMatthew Knepley #else
3019cda55fadSBarry Smith        0,
3020103bf8bdSMatthew Knepley #endif
3021cda55fadSBarry Smith        0,
3022cda55fadSBarry Smith        0,
302397304618SKris Buschelman /*10*/ 0,
3024cda55fadSBarry Smith        0,
3025cda55fadSBarry Smith        0,
302641f059aeSBarry Smith        MatSOR_MPIAIJ,
3027b7c46309SBarry Smith        MatTranspose_MPIAIJ,
302897304618SKris Buschelman /*15*/ MatGetInfo_MPIAIJ,
3029cda55fadSBarry Smith        MatEqual_MPIAIJ,
3030cda55fadSBarry Smith        MatGetDiagonal_MPIAIJ,
3031cda55fadSBarry Smith        MatDiagonalScale_MPIAIJ,
3032cda55fadSBarry Smith        MatNorm_MPIAIJ,
303397304618SKris Buschelman /*20*/ MatAssemblyBegin_MPIAIJ,
3034cda55fadSBarry Smith        MatAssemblyEnd_MPIAIJ,
3035cda55fadSBarry Smith        MatSetOption_MPIAIJ,
3036cda55fadSBarry Smith        MatZeroEntries_MPIAIJ,
3037d519adbfSMatthew Knepley /*24*/ MatZeroRows_MPIAIJ,
3038cda55fadSBarry Smith        0,
3039103bf8bdSMatthew Knepley #ifdef PETSC_HAVE_PBGL
3040719d5645SBarry Smith        0,
3041103bf8bdSMatthew Knepley #else
3042cda55fadSBarry Smith        0,
3043103bf8bdSMatthew Knepley #endif
3044cda55fadSBarry Smith        0,
3045cda55fadSBarry Smith        0,
30464994cf47SJed Brown /*29*/ MatSetUp_MPIAIJ,
3047103bf8bdSMatthew Knepley #ifdef PETSC_HAVE_PBGL
3048719d5645SBarry Smith        0,
3049103bf8bdSMatthew Knepley #else
3050cda55fadSBarry Smith        0,
3051103bf8bdSMatthew Knepley #endif
3052cda55fadSBarry Smith        0,
3053cda55fadSBarry Smith        0,
3054cda55fadSBarry Smith        0,
3055d519adbfSMatthew Knepley /*34*/ MatDuplicate_MPIAIJ,
3056cda55fadSBarry Smith        0,
3057cda55fadSBarry Smith        0,
3058cda55fadSBarry Smith        0,
3059cda55fadSBarry Smith        0,
3060d519adbfSMatthew Knepley /*39*/ MatAXPY_MPIAIJ,
3061cda55fadSBarry Smith        MatGetSubMatrices_MPIAIJ,
3062cda55fadSBarry Smith        MatIncreaseOverlap_MPIAIJ,
3063cda55fadSBarry Smith        MatGetValues_MPIAIJ,
3064cb5b572fSBarry Smith        MatCopy_MPIAIJ,
3065d519adbfSMatthew Knepley /*44*/ MatGetRowMax_MPIAIJ,
3066cda55fadSBarry Smith        MatScale_MPIAIJ,
3067cda55fadSBarry Smith        0,
3068cda55fadSBarry Smith        0,
3069564f14d6SBarry Smith        MatZeroRowsColumns_MPIAIJ,
3070f73d5cc4SBarry Smith /*49*/ 0,
3071cda55fadSBarry Smith        0,
3072cda55fadSBarry Smith        0,
3073cda55fadSBarry Smith        0,
3074cda55fadSBarry Smith        0,
3075d519adbfSMatthew Knepley /*54*/ MatFDColoringCreate_MPIAIJ,
3076cda55fadSBarry Smith        0,
3077cda55fadSBarry Smith        MatSetUnfactored_MPIAIJ,
307829dcf524SDmitry Karpeev        0, /* MatPermute_MPIAIJ, impl currently broken */
3079cda55fadSBarry Smith        0,
3080d519adbfSMatthew Knepley /*59*/ MatGetSubMatrix_MPIAIJ,
3081e03a110bSBarry Smith        MatDestroy_MPIAIJ,
3082e03a110bSBarry Smith        MatView_MPIAIJ,
3083357abbc8SBarry Smith        0,
3084a2243be0SBarry Smith        0,
3085d519adbfSMatthew Knepley /*64*/ 0,
3086a2243be0SBarry Smith        0,
3087a2243be0SBarry Smith        0,
3088a2243be0SBarry Smith        0,
3089a2243be0SBarry Smith        0,
3090d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_MPIAIJ,
3091c87e5d42SMatthew Knepley        MatGetRowMinAbs_MPIAIJ,
3092a2243be0SBarry Smith        0,
3093a2243be0SBarry Smith        MatSetColoring_MPIAIJ,
3094dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
3095779c1a83SBarry Smith        MatSetValuesAdic_MPIAIJ,
3096dcf5cc72SBarry Smith #else
3097dcf5cc72SBarry Smith        0,
3098dcf5cc72SBarry Smith #endif
309997304618SKris Buschelman        MatSetValuesAdifor_MPIAIJ,
31003acb8795SBarry Smith /*75*/ MatFDColoringApply_AIJ,
310197304618SKris Buschelman        0,
310297304618SKris Buschelman        0,
310397304618SKris Buschelman        0,
3104f1f41ecbSJed Brown        MatFindZeroDiagonals_MPIAIJ,
310597304618SKris Buschelman /*80*/ 0,
310697304618SKris Buschelman        0,
310797304618SKris Buschelman        0,
31085bba2384SShri Abhyankar /*83*/ MatLoad_MPIAIJ,
31096284ec50SHong Zhang        0,
31106284ec50SHong Zhang        0,
31116284ec50SHong Zhang        0,
31126284ec50SHong Zhang        0,
3113865e5f61SKris Buschelman        0,
3114d519adbfSMatthew Knepley /*89*/ MatMatMult_MPIAIJ_MPIAIJ,
311526be0446SHong Zhang        MatMatMultSymbolic_MPIAIJ_MPIAIJ,
311626be0446SHong Zhang        MatMatMultNumeric_MPIAIJ_MPIAIJ,
31177a7894deSKris Buschelman        MatPtAP_Basic,
31187a7894deSKris Buschelman        MatPtAPSymbolic_MPIAIJ,
3119d519adbfSMatthew Knepley /*94*/ MatPtAPNumeric_MPIAIJ,
31207a7894deSKris Buschelman        0,
31217a7894deSKris Buschelman        0,
31227a7894deSKris Buschelman        0,
31237a7894deSKris Buschelman        0,
3124d519adbfSMatthew Knepley /*99*/ 0,
3125865e5f61SKris Buschelman        MatPtAPSymbolic_MPIAIJ_MPIAIJ,
31267a7894deSKris Buschelman        MatPtAPNumeric_MPIAIJ_MPIAIJ,
31272fd7e33dSBarry Smith        MatConjugate_MPIAIJ,
31282fd7e33dSBarry Smith        0,
3129d519adbfSMatthew Knepley /*104*/MatSetValuesRow_MPIAIJ,
313099cafbc1SBarry Smith        MatRealPart_MPIAIJ,
313169db28dcSHong Zhang        MatImaginaryPart_MPIAIJ,
313269db28dcSHong Zhang        0,
313369db28dcSHong Zhang        0,
3134d519adbfSMatthew Knepley /*109*/0,
313503bc72f1SMatthew Knepley        MatGetRedundantMatrix_MPIAIJ,
31365494a064SHong Zhang        MatGetRowMin_MPIAIJ,
31375494a064SHong Zhang        0,
31385494a064SHong Zhang        0,
3139d1adec66SJed Brown /*114*/MatGetSeqNonzeroStructure_MPIAIJ,
3140bd0c2dcbSBarry Smith        0,
3141bd0c2dcbSBarry Smith        0,
3142bd0c2dcbSBarry Smith        0,
3143bd0c2dcbSBarry Smith        0,
31448fb81238SShri Abhyankar /*119*/0,
31458fb81238SShri Abhyankar        0,
31468fb81238SShri Abhyankar        0,
3147d6037b41SHong Zhang        0,
3148b9614d88SDmitry Karpeev        MatGetMultiProcBlock_MPIAIJ,
3149f2c98031SJed Brown /*124*/MatFindNonzeroRows_MPIAIJ,
31500716a85fSBarry Smith        MatGetColumnNorms_MPIAIJ,
3151bbead8a2SBarry Smith        MatInvertBlockDiagonal_MPIAIJ,
3152b9614d88SDmitry Karpeev        0,
315337868618SMatthew G Knepley        MatGetSubMatricesParallel_MPIAIJ,
3154187b3c17SHong Zhang /*129*/0,
3155187b3c17SHong Zhang        MatTransposeMatMult_MPIAIJ_MPIAIJ,
3156187b3c17SHong Zhang        MatTransposeMatMultSymbolic_MPIAIJ_MPIAIJ,
3157187b3c17SHong Zhang        MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ,
3158187b3c17SHong Zhang        0,
3159187b3c17SHong Zhang /*134*/0,
3160187b3c17SHong Zhang        0,
3161187b3c17SHong Zhang        0,
3162187b3c17SHong Zhang        0,
3163187b3c17SHong Zhang        0
3164bd0c2dcbSBarry Smith };
316536ce4990SBarry Smith 
31662e8a6d31SBarry Smith /* ----------------------------------------------------------------------------------------*/
31672e8a6d31SBarry Smith 
3168fb2e594dSBarry Smith EXTERN_C_BEGIN
31694a2ae208SSatish Balay #undef __FUNCT__
31704a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_MPIAIJ"
31717087cfbeSBarry Smith PetscErrorCode  MatStoreValues_MPIAIJ(Mat mat)
31722e8a6d31SBarry Smith {
31732e8a6d31SBarry Smith   Mat_MPIAIJ     *aij = (Mat_MPIAIJ *)mat->data;
3174dfbe8321SBarry Smith   PetscErrorCode ierr;
31752e8a6d31SBarry Smith 
31762e8a6d31SBarry Smith   PetscFunctionBegin;
31772e8a6d31SBarry Smith   ierr = MatStoreValues(aij->A);CHKERRQ(ierr);
31782e8a6d31SBarry Smith   ierr = MatStoreValues(aij->B);CHKERRQ(ierr);
31792e8a6d31SBarry Smith   PetscFunctionReturn(0);
31802e8a6d31SBarry Smith }
3181fb2e594dSBarry Smith EXTERN_C_END
31822e8a6d31SBarry Smith 
3183fb2e594dSBarry Smith EXTERN_C_BEGIN
31844a2ae208SSatish Balay #undef __FUNCT__
31854a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_MPIAIJ"
31867087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_MPIAIJ(Mat mat)
31872e8a6d31SBarry Smith {
31882e8a6d31SBarry Smith   Mat_MPIAIJ     *aij = (Mat_MPIAIJ *)mat->data;
3189dfbe8321SBarry Smith   PetscErrorCode ierr;
31902e8a6d31SBarry Smith 
31912e8a6d31SBarry Smith   PetscFunctionBegin;
31922e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr);
31932e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr);
31942e8a6d31SBarry Smith   PetscFunctionReturn(0);
31952e8a6d31SBarry Smith }
3196fb2e594dSBarry Smith EXTERN_C_END
31978a729477SBarry Smith 
319827508adbSBarry Smith EXTERN_C_BEGIN
31994a2ae208SSatish Balay #undef __FUNCT__
3200a23d5eceSKris Buschelman #define __FUNCT__ "MatMPIAIJSetPreallocation_MPIAIJ"
32017087cfbeSBarry Smith PetscErrorCode  MatMPIAIJSetPreallocation_MPIAIJ(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
3202a23d5eceSKris Buschelman {
3203a23d5eceSKris Buschelman   Mat_MPIAIJ     *b;
3204dfbe8321SBarry Smith   PetscErrorCode ierr;
3205b1d57f15SBarry Smith   PetscInt       i;
32062576faa2SJed Brown   PetscBool      d_realalloc = PETSC_FALSE,o_realalloc = PETSC_FALSE;
3207a23d5eceSKris Buschelman 
3208a23d5eceSKris Buschelman   PetscFunctionBegin;
32092576faa2SJed Brown   if (d_nz >= 0 || d_nnz) d_realalloc = PETSC_TRUE;
32102576faa2SJed Brown   if (o_nz >= 0 || o_nnz) o_realalloc = PETSC_TRUE;
3211a23d5eceSKris Buschelman   if (d_nz == PETSC_DEFAULT || d_nz == PETSC_DECIDE) d_nz = 5;
3212a23d5eceSKris Buschelman   if (o_nz == PETSC_DEFAULT || o_nz == PETSC_DECIDE) o_nz = 2;
3213e32f2f54SBarry Smith   if (d_nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"d_nz cannot be less than 0: value %D",d_nz);
3214e32f2f54SBarry Smith   if (o_nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"o_nz cannot be less than 0: value %D",o_nz);
3215899cda47SBarry Smith 
321626283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
321726283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3218a23d5eceSKris Buschelman   if (d_nnz) {
3219d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
3220e32f2f54SBarry Smith       if (d_nnz[i] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"d_nnz cannot be less than 0: local row %D value %D",i,d_nnz[i]);
3221a23d5eceSKris Buschelman     }
3222a23d5eceSKris Buschelman   }
3223a23d5eceSKris Buschelman   if (o_nnz) {
3224d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
3225e32f2f54SBarry Smith       if (o_nnz[i] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"o_nnz cannot be less than 0: local row %D value %D",i,o_nnz[i]);
3226a23d5eceSKris Buschelman     }
3227a23d5eceSKris Buschelman   }
3228a23d5eceSKris Buschelman   b = (Mat_MPIAIJ*)B->data;
3229899cda47SBarry Smith 
3230526dfc15SBarry Smith   if (!B->preallocated) {
3231899cda47SBarry Smith     /* Explicitly create 2 MATSEQAIJ matrices. */
3232899cda47SBarry Smith     ierr = MatCreate(PETSC_COMM_SELF,&b->A);CHKERRQ(ierr);
3233d0f46423SBarry Smith     ierr = MatSetSizes(b->A,B->rmap->n,B->cmap->n,B->rmap->n,B->cmap->n);CHKERRQ(ierr);
3234f9e9af59SJed Brown     ierr = MatSetBlockSizes(b->A,B->rmap->bs,B->cmap->bs);CHKERRQ(ierr);
3235899cda47SBarry Smith     ierr = MatSetType(b->A,MATSEQAIJ);CHKERRQ(ierr);
3236899cda47SBarry Smith     ierr = PetscLogObjectParent(B,b->A);CHKERRQ(ierr);
3237899cda47SBarry Smith     ierr = MatCreate(PETSC_COMM_SELF,&b->B);CHKERRQ(ierr);
3238d0f46423SBarry Smith     ierr = MatSetSizes(b->B,B->rmap->n,B->cmap->N,B->rmap->n,B->cmap->N);CHKERRQ(ierr);
3239f9e9af59SJed Brown     ierr = MatSetBlockSizes(b->B,B->rmap->bs,B->cmap->bs);CHKERRQ(ierr);
3240899cda47SBarry Smith     ierr = MatSetType(b->B,MATSEQAIJ);CHKERRQ(ierr);
3241899cda47SBarry Smith     ierr = PetscLogObjectParent(B,b->B);CHKERRQ(ierr);
3242526dfc15SBarry Smith   }
3243899cda47SBarry Smith 
3244c60e587dSKris Buschelman   ierr = MatSeqAIJSetPreallocation(b->A,d_nz,d_nnz);CHKERRQ(ierr);
3245c60e587dSKris Buschelman   ierr = MatSeqAIJSetPreallocation(b->B,o_nz,o_nnz);CHKERRQ(ierr);
32462576faa2SJed Brown   /* Do not error if the user did not give real preallocation information. Ugly because this would overwrite a previous user call to MatSetOption(). */
32472576faa2SJed Brown   if (!d_realalloc) {ierr = MatSetOption(b->A,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr);}
32482576faa2SJed Brown   if (!o_realalloc) {ierr = MatSetOption(b->B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr);}
3249526dfc15SBarry Smith   B->preallocated = PETSC_TRUE;
3250a23d5eceSKris Buschelman   PetscFunctionReturn(0);
3251a23d5eceSKris Buschelman }
3252a23d5eceSKris Buschelman EXTERN_C_END
3253a23d5eceSKris Buschelman 
32544a2ae208SSatish Balay #undef __FUNCT__
32554a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_MPIAIJ"
3256dfbe8321SBarry Smith PetscErrorCode MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
3257d6dfbf8fSBarry Smith {
3258d6dfbf8fSBarry Smith   Mat            mat;
3259416022c9SBarry Smith   Mat_MPIAIJ     *a,*oldmat = (Mat_MPIAIJ*)matin->data;
3260dfbe8321SBarry Smith   PetscErrorCode ierr;
3261d6dfbf8fSBarry Smith 
32623a40ed3dSBarry Smith   PetscFunctionBegin;
3263416022c9SBarry Smith   *newmat       = 0;
32647adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)matin)->comm,&mat);CHKERRQ(ierr);
3265d0f46423SBarry Smith   ierr = MatSetSizes(mat,matin->rmap->n,matin->cmap->n,matin->rmap->N,matin->cmap->N);CHKERRQ(ierr);
3266a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(mat,matin->rmap->bs,matin->cmap->bs);CHKERRQ(ierr);
32677adad957SLisandro Dalcin   ierr = MatSetType(mat,((PetscObject)matin)->type_name);CHKERRQ(ierr);
32681d5dac46SHong Zhang   ierr = PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
3269273d9f13SBarry Smith   a    = (Mat_MPIAIJ*)mat->data;
3270e1b6402fSHong Zhang 
3271d5f3da31SBarry Smith   mat->factortype    = matin->factortype;
3272d0f46423SBarry Smith   mat->rmap->bs      = matin->rmap->bs;
3273a2f3521dSMark F. Adams   mat->cmap->bs      = matin->cmap->bs;
3274c456f294SBarry Smith   mat->assembled    = PETSC_TRUE;
3275e7641de0SSatish Balay   mat->insertmode   = NOT_SET_VALUES;
3276273d9f13SBarry Smith   mat->preallocated = PETSC_TRUE;
3277d6dfbf8fSBarry Smith 
327817699dbbSLois Curfman McInnes   a->size           = oldmat->size;
327917699dbbSLois Curfman McInnes   a->rank           = oldmat->rank;
3280e7641de0SSatish Balay   a->donotstash     = oldmat->donotstash;
3281e7641de0SSatish Balay   a->roworiented    = oldmat->roworiented;
3282e7641de0SSatish Balay   a->rowindices     = 0;
3283bcd2baecSBarry Smith   a->rowvalues      = 0;
3284bcd2baecSBarry Smith   a->getrowactive   = PETSC_FALSE;
3285d6dfbf8fSBarry Smith 
32861e1e43feSBarry Smith   ierr = PetscLayoutReference(matin->rmap,&mat->rmap);CHKERRQ(ierr);
32871e1e43feSBarry Smith   ierr = PetscLayoutReference(matin->cmap,&mat->cmap);CHKERRQ(ierr);
3288899cda47SBarry Smith 
32892ee70a88SLois Curfman McInnes   if (oldmat->colmap) {
3290aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
32910f5bd95cSBarry Smith     ierr = PetscTableCreateCopy(oldmat->colmap,&a->colmap);CHKERRQ(ierr);
3292b1fc9764SSatish Balay #else
3293d0f46423SBarry Smith     ierr = PetscMalloc((mat->cmap->N)*sizeof(PetscInt),&a->colmap);CHKERRQ(ierr);
3294d0f46423SBarry Smith     ierr = PetscLogObjectMemory(mat,(mat->cmap->N)*sizeof(PetscInt));CHKERRQ(ierr);
3295d0f46423SBarry Smith     ierr = PetscMemcpy(a->colmap,oldmat->colmap,(mat->cmap->N)*sizeof(PetscInt));CHKERRQ(ierr);
3296b1fc9764SSatish Balay #endif
3297416022c9SBarry Smith   } else a->colmap = 0;
32983f41c07dSBarry Smith   if (oldmat->garray) {
3299b1d57f15SBarry Smith     PetscInt len;
3300d0f46423SBarry Smith     len  = oldmat->B->cmap->n;
3301b1d57f15SBarry Smith     ierr = PetscMalloc((len+1)*sizeof(PetscInt),&a->garray);CHKERRQ(ierr);
330252e6d16bSBarry Smith     ierr = PetscLogObjectMemory(mat,len*sizeof(PetscInt));CHKERRQ(ierr);
3303b1d57f15SBarry Smith     if (len) { ierr = PetscMemcpy(a->garray,oldmat->garray,len*sizeof(PetscInt));CHKERRQ(ierr); }
3304416022c9SBarry Smith   } else a->garray = 0;
3305d6dfbf8fSBarry Smith 
3306416022c9SBarry Smith   ierr = VecDuplicate(oldmat->lvec,&a->lvec);CHKERRQ(ierr);
330752e6d16bSBarry Smith   ierr = PetscLogObjectParent(mat,a->lvec);CHKERRQ(ierr);
3308a56f8943SBarry Smith   ierr = VecScatterCopy(oldmat->Mvctx,&a->Mvctx);CHKERRQ(ierr);
330952e6d16bSBarry Smith   ierr = PetscLogObjectParent(mat,a->Mvctx);CHKERRQ(ierr);
33102e8a6d31SBarry Smith   ierr = MatDuplicate(oldmat->A,cpvalues,&a->A);CHKERRQ(ierr);
331152e6d16bSBarry Smith   ierr = PetscLogObjectParent(mat,a->A);CHKERRQ(ierr);
33122e8a6d31SBarry Smith   ierr = MatDuplicate(oldmat->B,cpvalues,&a->B);CHKERRQ(ierr);
331352e6d16bSBarry Smith   ierr = PetscLogObjectParent(mat,a->B);CHKERRQ(ierr);
33147adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)matin)->qlist,&((PetscObject)mat)->qlist);CHKERRQ(ierr);
33158a729477SBarry Smith   *newmat = mat;
33163a40ed3dSBarry Smith   PetscFunctionReturn(0);
33178a729477SBarry Smith }
3318416022c9SBarry Smith 
33191a4ee126SBarry Smith 
33201a4ee126SBarry Smith 
33214a2ae208SSatish Balay #undef __FUNCT__
33225bba2384SShri Abhyankar #define __FUNCT__ "MatLoad_MPIAIJ"
3323112444f4SShri Abhyankar PetscErrorCode MatLoad_MPIAIJ(Mat newMat, PetscViewer viewer)
33248fb81238SShri Abhyankar {
33258fb81238SShri Abhyankar   PetscScalar    *vals,*svals;
33268fb81238SShri Abhyankar   MPI_Comm       comm = ((PetscObject)viewer)->comm;
33278fb81238SShri Abhyankar   PetscErrorCode ierr;
33281a4ee126SBarry Smith   PetscMPIInt    rank,size,tag = ((PetscObject)viewer)->tag;
33298fb81238SShri Abhyankar   PetscInt       i,nz,j,rstart,rend,mmax,maxnz = 0,grows,gcols;
33308fb81238SShri Abhyankar   PetscInt       header[4],*rowlengths = 0,M,N,m,*cols;
33318fb81238SShri Abhyankar   PetscInt       *ourlens = PETSC_NULL,*procsnz = PETSC_NULL,*offlens = PETSC_NULL,jj,*mycols,*smycols;
33328fb81238SShri Abhyankar   PetscInt       cend,cstart,n,*rowners,sizesset=1;
33338fb81238SShri Abhyankar   int            fd;
33348fb81238SShri Abhyankar 
33358fb81238SShri Abhyankar   PetscFunctionBegin;
33368fb81238SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
33378fb81238SShri Abhyankar   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
33388fb81238SShri Abhyankar   if (!rank) {
33398fb81238SShri Abhyankar     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
33408fb81238SShri Abhyankar     ierr = PetscBinaryRead(fd,(char *)header,4,PETSC_INT);CHKERRQ(ierr);
33418fb81238SShri Abhyankar     if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
33428fb81238SShri Abhyankar   }
33438fb81238SShri Abhyankar 
33448fb81238SShri Abhyankar   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) sizesset = 0;
33458fb81238SShri Abhyankar 
33468fb81238SShri Abhyankar   ierr = MPI_Bcast(header+1,3,MPIU_INT,0,comm);CHKERRQ(ierr);
33478fb81238SShri Abhyankar   M = header[1]; N = header[2];
33488fb81238SShri Abhyankar   /* If global rows/cols are set to PETSC_DECIDE, set it to the sizes given in the file */
33498fb81238SShri Abhyankar   if (sizesset && newMat->rmap->N < 0) newMat->rmap->N = M;
33508fb81238SShri Abhyankar   if (sizesset && newMat->cmap->N < 0) newMat->cmap->N = N;
33518fb81238SShri Abhyankar 
33528fb81238SShri Abhyankar   /* If global sizes are set, check if they are consistent with that given in the file */
33538fb81238SShri Abhyankar   if (sizesset) {
33548fb81238SShri Abhyankar     ierr = MatGetSize(newMat,&grows,&gcols);CHKERRQ(ierr);
33558fb81238SShri Abhyankar   }
3356abd38a8fSBarry Smith   if (sizesset && newMat->rmap->N != grows) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Inconsistent # of rows:Matrix in file has (%d) and input matrix has (%d)",M,grows);
3357abd38a8fSBarry Smith   if (sizesset && newMat->cmap->N != gcols) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Inconsistent # of cols:Matrix in file has (%d) and input matrix has (%d)",N,gcols);
33588fb81238SShri Abhyankar 
33598fb81238SShri Abhyankar   /* determine ownership of all rows */
33608fb81238SShri Abhyankar   if (newMat->rmap->n < 0 ) m    = M/size + ((M % size) > rank); /* PETSC_DECIDE */
33614683f7a4SShri Abhyankar   else m = newMat->rmap->n; /* Set by user */
33628fb81238SShri Abhyankar 
33638fb81238SShri Abhyankar   ierr = PetscMalloc((size+1)*sizeof(PetscInt),&rowners);CHKERRQ(ierr);
33648fb81238SShri Abhyankar   ierr = MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);CHKERRQ(ierr);
33658fb81238SShri Abhyankar 
33668fb81238SShri Abhyankar   /* First process needs enough room for process with most rows */
33678fb81238SShri Abhyankar   if (!rank) {
33688fb81238SShri Abhyankar     mmax = rowners[1];
33695c4ea359SMatthew G Knepley     for(i=2; i<=size; i++) {
33701251c579SMatthew G Knepley       mmax = PetscMax(mmax, rowners[i]);
33718fb81238SShri Abhyankar     }
33728fb81238SShri Abhyankar   } else mmax = m;
33738fb81238SShri Abhyankar 
33748fb81238SShri Abhyankar   rowners[0] = 0;
33758fb81238SShri Abhyankar   for (i=2; i<=size; i++) {
33768fb81238SShri Abhyankar     rowners[i] += rowners[i-1];
33778fb81238SShri Abhyankar   }
33788fb81238SShri Abhyankar   rstart = rowners[rank];
33798fb81238SShri Abhyankar   rend   = rowners[rank+1];
33808fb81238SShri Abhyankar 
33818fb81238SShri Abhyankar   /* distribute row lengths to all processors */
33828fb81238SShri Abhyankar   ierr    = PetscMalloc2(mmax,PetscInt,&ourlens,mmax,PetscInt,&offlens);CHKERRQ(ierr);
33838fb81238SShri Abhyankar   if (!rank) {
33848fb81238SShri Abhyankar     ierr = PetscBinaryRead(fd,ourlens,m,PETSC_INT);CHKERRQ(ierr);
33855c4ea359SMatthew G Knepley     ierr = PetscMalloc(mmax*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
33868fb81238SShri Abhyankar     ierr = PetscMalloc(size*sizeof(PetscInt),&procsnz);CHKERRQ(ierr);
33878fb81238SShri Abhyankar     ierr = PetscMemzero(procsnz,size*sizeof(PetscInt));CHKERRQ(ierr);
33888fb81238SShri Abhyankar     for (j=0; j<m; j++) {
33898fb81238SShri Abhyankar       procsnz[0] += ourlens[j];
33908fb81238SShri Abhyankar     }
33918fb81238SShri Abhyankar     for (i=1; i<size; i++) {
33928fb81238SShri Abhyankar       ierr = PetscBinaryRead(fd,rowlengths,rowners[i+1]-rowners[i],PETSC_INT);CHKERRQ(ierr);
33938fb81238SShri Abhyankar       /* calculate the number of nonzeros on each processor */
33948fb81238SShri Abhyankar       for (j=0; j<rowners[i+1]-rowners[i]; j++) {
33958fb81238SShri Abhyankar         procsnz[i] += rowlengths[j];
33968fb81238SShri Abhyankar       }
3397a25532f0SBarry Smith       ierr = MPIULong_Send(rowlengths,rowners[i+1]-rowners[i],MPIU_INT,i,tag,comm);CHKERRQ(ierr);
33988fb81238SShri Abhyankar     }
33998fb81238SShri Abhyankar     ierr = PetscFree(rowlengths);CHKERRQ(ierr);
34008fb81238SShri Abhyankar   } else {
3401a25532f0SBarry Smith     ierr = MPIULong_Recv(ourlens,m,MPIU_INT,0,tag,comm);CHKERRQ(ierr);
34028fb81238SShri Abhyankar   }
34038fb81238SShri Abhyankar 
34048fb81238SShri Abhyankar   if (!rank) {
34058fb81238SShri Abhyankar     /* determine max buffer needed and allocate it */
34068fb81238SShri Abhyankar     maxnz = 0;
34078fb81238SShri Abhyankar     for (i=0; i<size; i++) {
34088fb81238SShri Abhyankar       maxnz = PetscMax(maxnz,procsnz[i]);
34098fb81238SShri Abhyankar     }
34108fb81238SShri Abhyankar     ierr = PetscMalloc(maxnz*sizeof(PetscInt),&cols);CHKERRQ(ierr);
34118fb81238SShri Abhyankar 
34128fb81238SShri Abhyankar     /* read in my part of the matrix column indices  */
34138fb81238SShri Abhyankar     nz   = procsnz[0];
34148fb81238SShri Abhyankar     ierr = PetscMalloc(nz*sizeof(PetscInt),&mycols);CHKERRQ(ierr);
34158fb81238SShri Abhyankar     ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr);
34168fb81238SShri Abhyankar 
34178fb81238SShri Abhyankar     /* read in every one elses and ship off */
34188fb81238SShri Abhyankar     for (i=1; i<size; i++) {
34198fb81238SShri Abhyankar       nz     = procsnz[i];
34208fb81238SShri Abhyankar       ierr   = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr);
3421a25532f0SBarry Smith       ierr   = MPIULong_Send(cols,nz,MPIU_INT,i,tag,comm);CHKERRQ(ierr);
34228fb81238SShri Abhyankar     }
34238fb81238SShri Abhyankar     ierr = PetscFree(cols);CHKERRQ(ierr);
34248fb81238SShri Abhyankar   } else {
34258fb81238SShri Abhyankar     /* determine buffer space needed for message */
34268fb81238SShri Abhyankar     nz = 0;
34278fb81238SShri Abhyankar     for (i=0; i<m; i++) {
34288fb81238SShri Abhyankar       nz += ourlens[i];
34298fb81238SShri Abhyankar     }
34308fb81238SShri Abhyankar     ierr = PetscMalloc(nz*sizeof(PetscInt),&mycols);CHKERRQ(ierr);
34318fb81238SShri Abhyankar 
34328fb81238SShri Abhyankar     /* receive message of column indices*/
3433a25532f0SBarry Smith     ierr = MPIULong_Recv(mycols,nz,MPIU_INT,0,tag,comm);CHKERRQ(ierr);
34348fb81238SShri Abhyankar   }
34358fb81238SShri Abhyankar 
34368fb81238SShri Abhyankar   /* determine column ownership if matrix is not square */
34378fb81238SShri Abhyankar   if (N != M) {
34388fb81238SShri Abhyankar     if (newMat->cmap->n < 0) n      = N/size + ((N % size) > rank);
34398fb81238SShri Abhyankar     else n = newMat->cmap->n;
34408fb81238SShri Abhyankar     ierr   = MPI_Scan(&n,&cend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr);
34418fb81238SShri Abhyankar     cstart = cend - n;
34428fb81238SShri Abhyankar   } else {
34438fb81238SShri Abhyankar     cstart = rstart;
34448fb81238SShri Abhyankar     cend   = rend;
34458fb81238SShri Abhyankar     n      = cend - cstart;
34468fb81238SShri Abhyankar   }
34478fb81238SShri Abhyankar 
34488fb81238SShri Abhyankar   /* loop over local rows, determining number of off diagonal entries */
34498fb81238SShri Abhyankar   ierr = PetscMemzero(offlens,m*sizeof(PetscInt));CHKERRQ(ierr);
34508fb81238SShri Abhyankar   jj = 0;
34518fb81238SShri Abhyankar   for (i=0; i<m; i++) {
34528fb81238SShri Abhyankar     for (j=0; j<ourlens[i]; j++) {
34538fb81238SShri Abhyankar       if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++;
34548fb81238SShri Abhyankar       jj++;
34558fb81238SShri Abhyankar     }
34568fb81238SShri Abhyankar   }
34578fb81238SShri Abhyankar 
34588fb81238SShri Abhyankar   for (i=0; i<m; i++) {
34598fb81238SShri Abhyankar     ourlens[i] -= offlens[i];
34608fb81238SShri Abhyankar   }
34618fb81238SShri Abhyankar   if (!sizesset) {
34628fb81238SShri Abhyankar     ierr = MatSetSizes(newMat,m,n,M,N);CHKERRQ(ierr);
34638fb81238SShri Abhyankar   }
34648fb81238SShri Abhyankar   ierr = MatMPIAIJSetPreallocation(newMat,0,ourlens,0,offlens);CHKERRQ(ierr);
34658fb81238SShri Abhyankar 
34668fb81238SShri Abhyankar   for (i=0; i<m; i++) {
34678fb81238SShri Abhyankar     ourlens[i] += offlens[i];
34688fb81238SShri Abhyankar   }
34698fb81238SShri Abhyankar 
34708fb81238SShri Abhyankar   if (!rank) {
34718fb81238SShri Abhyankar     ierr = PetscMalloc((maxnz+1)*sizeof(PetscScalar),&vals);CHKERRQ(ierr);
34728fb81238SShri Abhyankar 
34738fb81238SShri Abhyankar     /* read in my part of the matrix numerical values  */
34748fb81238SShri Abhyankar     nz   = procsnz[0];
34758fb81238SShri Abhyankar     ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
34768fb81238SShri Abhyankar 
34778fb81238SShri Abhyankar     /* insert into matrix */
34788fb81238SShri Abhyankar     jj      = rstart;
34798fb81238SShri Abhyankar     smycols = mycols;
34808fb81238SShri Abhyankar     svals   = vals;
34818fb81238SShri Abhyankar     for (i=0; i<m; i++) {
34828fb81238SShri Abhyankar       ierr = MatSetValues_MPIAIJ(newMat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
34838fb81238SShri Abhyankar       smycols += ourlens[i];
34848fb81238SShri Abhyankar       svals   += ourlens[i];
34858fb81238SShri Abhyankar       jj++;
34868fb81238SShri Abhyankar     }
34878fb81238SShri Abhyankar 
34888fb81238SShri Abhyankar     /* read in other processors and ship out */
34898fb81238SShri Abhyankar     for (i=1; i<size; i++) {
34908fb81238SShri Abhyankar       nz     = procsnz[i];
34918fb81238SShri Abhyankar       ierr   = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
3492a25532f0SBarry Smith       ierr   = MPIULong_Send(vals,nz,MPIU_SCALAR,i,((PetscObject)newMat)->tag,comm);CHKERRQ(ierr);
34938fb81238SShri Abhyankar     }
34948fb81238SShri Abhyankar     ierr = PetscFree(procsnz);CHKERRQ(ierr);
34958fb81238SShri Abhyankar   } else {
34968fb81238SShri Abhyankar     /* receive numeric values */
34978fb81238SShri Abhyankar     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&vals);CHKERRQ(ierr);
34988fb81238SShri Abhyankar 
34998fb81238SShri Abhyankar     /* receive message of values*/
3500a25532f0SBarry Smith     ierr   = MPIULong_Recv(vals,nz,MPIU_SCALAR,0,((PetscObject)newMat)->tag,comm);CHKERRQ(ierr);
35018fb81238SShri Abhyankar 
35028fb81238SShri Abhyankar     /* insert into matrix */
35038fb81238SShri Abhyankar     jj      = rstart;
35048fb81238SShri Abhyankar     smycols = mycols;
35058fb81238SShri Abhyankar     svals   = vals;
35068fb81238SShri Abhyankar     for (i=0; i<m; i++) {
35078fb81238SShri Abhyankar       ierr     = MatSetValues_MPIAIJ(newMat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
35088fb81238SShri Abhyankar       smycols += ourlens[i];
35098fb81238SShri Abhyankar       svals   += ourlens[i];
35108fb81238SShri Abhyankar       jj++;
35118fb81238SShri Abhyankar     }
35128fb81238SShri Abhyankar   }
35138fb81238SShri Abhyankar   ierr = PetscFree2(ourlens,offlens);CHKERRQ(ierr);
35148fb81238SShri Abhyankar   ierr = PetscFree(vals);CHKERRQ(ierr);
35158fb81238SShri Abhyankar   ierr = PetscFree(mycols);CHKERRQ(ierr);
35168fb81238SShri Abhyankar   ierr = PetscFree(rowners);CHKERRQ(ierr);
35178fb81238SShri Abhyankar 
35188fb81238SShri Abhyankar   ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
35198fb81238SShri Abhyankar   ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
35208fb81238SShri Abhyankar   PetscFunctionReturn(0);
35218fb81238SShri Abhyankar }
35228fb81238SShri Abhyankar 
35238fb81238SShri Abhyankar #undef __FUNCT__
35244a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_MPIAIJ"
35254aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,MatReuse call,Mat *newmat)
35264aa3045dSJed Brown {
35274aa3045dSJed Brown   PetscErrorCode ierr;
35284aa3045dSJed Brown   IS             iscol_local;
35294aa3045dSJed Brown   PetscInt       csize;
35304aa3045dSJed Brown 
35314aa3045dSJed Brown   PetscFunctionBegin;
35324aa3045dSJed Brown   ierr = ISGetLocalSize(iscol,&csize);CHKERRQ(ierr);
3533b79d0421SJed Brown   if (call == MAT_REUSE_MATRIX) {
3534b79d0421SJed Brown     ierr = PetscObjectQuery((PetscObject)*newmat,"ISAllGather",(PetscObject*)&iscol_local);CHKERRQ(ierr);
3535e32f2f54SBarry Smith     if (!iscol_local) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
3536b79d0421SJed Brown   } else {
3537c5bfad50SMark F. Adams     PetscInt cbs;
3538c5bfad50SMark F. Adams     ierr = ISGetBlockSize(iscol,&cbs); CHKERRQ(ierr);
35394aa3045dSJed Brown     ierr = ISAllGather(iscol,&iscol_local);CHKERRQ(ierr);
3540c5bfad50SMark F. Adams     ierr = ISSetBlockSize(iscol_local,cbs); CHKERRQ(ierr);
3541b79d0421SJed Brown   }
35424aa3045dSJed Brown   ierr = MatGetSubMatrix_MPIAIJ_Private(mat,isrow,iscol_local,csize,call,newmat);CHKERRQ(ierr);
3543b79d0421SJed Brown   if (call == MAT_INITIAL_MATRIX) {
3544b79d0421SJed Brown     ierr = PetscObjectCompose((PetscObject)*newmat,"ISAllGather",(PetscObject)iscol_local);CHKERRQ(ierr);
35456bf464f9SBarry Smith     ierr = ISDestroy(&iscol_local);CHKERRQ(ierr);
3546b79d0421SJed Brown   }
35474aa3045dSJed Brown   PetscFunctionReturn(0);
35484aa3045dSJed Brown }
35494aa3045dSJed Brown 
355029dcf524SDmitry Karpeev extern PetscErrorCode MatGetSubMatrices_MPIAIJ_Local(Mat,PetscInt,const IS[],const IS[],MatReuse,PetscBool*,Mat*);
35514aa3045dSJed Brown #undef __FUNCT__
35524aa3045dSJed Brown #define __FUNCT__ "MatGetSubMatrix_MPIAIJ_Private"
3553a0ff6018SBarry Smith /*
355429da9460SBarry Smith     Not great since it makes two copies of the submatrix, first an SeqAIJ
355529da9460SBarry Smith   in local and then by concatenating the local matrices the end result.
355629da9460SBarry Smith   Writing it directly would be much like MatGetSubMatrices_MPIAIJ()
35574aa3045dSJed Brown 
35584aa3045dSJed Brown   Note: This requires a sequential iscol with all indices.
3559a0ff6018SBarry Smith */
35604aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIAIJ_Private(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse call,Mat *newmat)
3561a0ff6018SBarry Smith {
3562dfbe8321SBarry Smith   PetscErrorCode ierr;
356332dcc486SBarry Smith   PetscMPIInt    rank,size;
3564a2f3521dSMark F. Adams   PetscInt       i,m,n,rstart,row,rend,nz,*cwork,j,bs,cbs;
356529dcf524SDmitry Karpeev   PetscInt       *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal,ncol;
356629dcf524SDmitry Karpeev   PetscBool      allcolumns, colflag;
356729dcf524SDmitry Karpeev   Mat            M,Mreuse;
3568a77337e4SBarry Smith   MatScalar      *vwork,*aa;
35697adad957SLisandro Dalcin   MPI_Comm       comm = ((PetscObject)mat)->comm;
357000e6dbe6SBarry Smith   Mat_SeqAIJ     *aij;
35717e2c5f70SBarry Smith 
3572a0ff6018SBarry Smith 
3573a0ff6018SBarry Smith   PetscFunctionBegin;
35741dab6e02SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
35751dab6e02SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
357600e6dbe6SBarry Smith 
357729dcf524SDmitry Karpeev   ierr = ISIdentity(iscol,&colflag);CHKERRQ(ierr);
357829dcf524SDmitry Karpeev   ierr = ISGetLocalSize(iscol,&ncol);CHKERRQ(ierr);
357929dcf524SDmitry Karpeev   if (colflag && ncol == mat->cmap->N){
358029dcf524SDmitry Karpeev     allcolumns = PETSC_TRUE;
358129dcf524SDmitry Karpeev   } else {
358229dcf524SDmitry Karpeev     allcolumns = PETSC_FALSE;
358329dcf524SDmitry Karpeev   }
3584fee21e36SBarry Smith   if (call ==  MAT_REUSE_MATRIX) {
3585fee21e36SBarry Smith     ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);CHKERRQ(ierr);
3586e32f2f54SBarry Smith     if (!Mreuse) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
358729dcf524SDmitry Karpeev     ierr  = MatGetSubMatrices_MPIAIJ_Local(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&allcolumns,&Mreuse);CHKERRQ(ierr);
3588fee21e36SBarry Smith   } else {
358929dcf524SDmitry Karpeev     ierr   = MatGetSubMatrices_MPIAIJ_Local(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&allcolumns,&Mreuse);CHKERRQ(ierr);
3590fee21e36SBarry Smith   }
3591a0ff6018SBarry Smith 
3592a0ff6018SBarry Smith   /*
3593a0ff6018SBarry Smith       m - number of local rows
3594a0ff6018SBarry Smith       n - number of columns (same on all processors)
3595a0ff6018SBarry Smith       rstart - first row in new global matrix generated
3596a0ff6018SBarry Smith   */
3597fee21e36SBarry Smith   ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr);
3598a2f3521dSMark F. Adams   ierr = MatGetBlockSizes(Mreuse,&bs,&cbs);CHKERRQ(ierr);
3599a0ff6018SBarry Smith   if (call == MAT_INITIAL_MATRIX) {
3600fee21e36SBarry Smith     aij = (Mat_SeqAIJ*)(Mreuse)->data;
360100e6dbe6SBarry Smith     ii  = aij->i;
360200e6dbe6SBarry Smith     jj  = aij->j;
360300e6dbe6SBarry Smith 
3604a0ff6018SBarry Smith     /*
360500e6dbe6SBarry Smith         Determine the number of non-zeros in the diagonal and off-diagonal
360600e6dbe6SBarry Smith         portions of the matrix in order to do correct preallocation
3607a0ff6018SBarry Smith     */
360800e6dbe6SBarry Smith 
360900e6dbe6SBarry Smith     /* first get start and end of "diagonal" columns */
36106a6a5d1dSBarry Smith     if (csize == PETSC_DECIDE) {
3611ab50ec6bSBarry Smith       ierr = ISGetSize(isrow,&mglobal);CHKERRQ(ierr);
3612ab50ec6bSBarry Smith       if (mglobal == n) { /* square matrix */
3613e2c4fddaSBarry Smith 	nlocal = m;
36146a6a5d1dSBarry Smith       } else {
3615ab50ec6bSBarry Smith         nlocal = n/size + ((n % size) > rank);
3616ab50ec6bSBarry Smith       }
3617ab50ec6bSBarry Smith     } else {
36186a6a5d1dSBarry Smith       nlocal = csize;
36196a6a5d1dSBarry Smith     }
3620b1d57f15SBarry Smith     ierr   = MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr);
362100e6dbe6SBarry Smith     rstart = rend - nlocal;
362265e19b50SBarry Smith     if (rank == size - 1 && rend != n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local column sizes %D do not add up to total number of columns %D",rend,n);
362300e6dbe6SBarry Smith 
362400e6dbe6SBarry Smith     /* next, compute all the lengths */
3625b1d57f15SBarry Smith     ierr  = PetscMalloc((2*m+1)*sizeof(PetscInt),&dlens);CHKERRQ(ierr);
362600e6dbe6SBarry Smith     olens = dlens + m;
362700e6dbe6SBarry Smith     for (i=0; i<m; i++) {
362800e6dbe6SBarry Smith       jend = ii[i+1] - ii[i];
362900e6dbe6SBarry Smith       olen = 0;
363000e6dbe6SBarry Smith       dlen = 0;
363100e6dbe6SBarry Smith       for (j=0; j<jend; j++) {
363200e6dbe6SBarry Smith         if (*jj < rstart || *jj >= rend) olen++;
363300e6dbe6SBarry Smith         else dlen++;
363400e6dbe6SBarry Smith         jj++;
363500e6dbe6SBarry Smith       }
363600e6dbe6SBarry Smith       olens[i] = olen;
363700e6dbe6SBarry Smith       dlens[i] = dlen;
363800e6dbe6SBarry Smith     }
3639f69a0ea3SMatthew Knepley     ierr = MatCreate(comm,&M);CHKERRQ(ierr);
3640f69a0ea3SMatthew Knepley     ierr = MatSetSizes(M,m,nlocal,PETSC_DECIDE,n);CHKERRQ(ierr);
3641a2f3521dSMark F. Adams     ierr = MatSetBlockSizes(M,bs,cbs); CHKERRQ(ierr);
36427adad957SLisandro Dalcin     ierr = MatSetType(M,((PetscObject)mat)->type_name);CHKERRQ(ierr);
3643e2d9671bSKris Buschelman     ierr = MatMPIAIJSetPreallocation(M,0,dlens,0,olens);CHKERRQ(ierr);
3644606d414cSSatish Balay     ierr = PetscFree(dlens);CHKERRQ(ierr);
3645a0ff6018SBarry Smith   } else {
3646b1d57f15SBarry Smith     PetscInt ml,nl;
3647a0ff6018SBarry Smith 
3648a0ff6018SBarry Smith     M = *newmat;
3649a0ff6018SBarry Smith     ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr);
3650e32f2f54SBarry Smith     if (ml != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
3651a0ff6018SBarry Smith     ierr = MatZeroEntries(M);CHKERRQ(ierr);
3652c48de900SBarry Smith     /*
3653c48de900SBarry Smith          The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
3654c48de900SBarry Smith        rather than the slower MatSetValues().
3655c48de900SBarry Smith     */
3656c48de900SBarry Smith     M->was_assembled = PETSC_TRUE;
3657c48de900SBarry Smith     M->assembled     = PETSC_FALSE;
3658a0ff6018SBarry Smith   }
3659a0ff6018SBarry Smith   ierr = MatGetOwnershipRange(M,&rstart,&rend);CHKERRQ(ierr);
3660fee21e36SBarry Smith   aij = (Mat_SeqAIJ*)(Mreuse)->data;
366100e6dbe6SBarry Smith   ii  = aij->i;
366200e6dbe6SBarry Smith   jj  = aij->j;
366300e6dbe6SBarry Smith   aa  = aij->a;
3664a0ff6018SBarry Smith   for (i=0; i<m; i++) {
3665a0ff6018SBarry Smith     row   = rstart + i;
366600e6dbe6SBarry Smith     nz    = ii[i+1] - ii[i];
366700e6dbe6SBarry Smith     cwork = jj;     jj += nz;
366800e6dbe6SBarry Smith     vwork = aa;     aa += nz;
36698c638d02SBarry Smith     ierr = MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr);
3670a0ff6018SBarry Smith   }
3671a0ff6018SBarry Smith 
3672a0ff6018SBarry Smith   ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3673a0ff6018SBarry Smith   ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3674a0ff6018SBarry Smith   *newmat = M;
3675fee21e36SBarry Smith 
3676fee21e36SBarry Smith   /* save submatrix used in processor for next request */
3677fee21e36SBarry Smith   if (call ==  MAT_INITIAL_MATRIX) {
3678fee21e36SBarry Smith     ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr);
3679bf0cc555SLisandro Dalcin     ierr = MatDestroy(&Mreuse);CHKERRQ(ierr);
3680fee21e36SBarry Smith   }
3681fee21e36SBarry Smith 
3682a0ff6018SBarry Smith   PetscFunctionReturn(0);
3683a0ff6018SBarry Smith }
3684273d9f13SBarry Smith 
3685e2e86b8fSSatish Balay EXTERN_C_BEGIN
36864a2ae208SSatish Balay #undef __FUNCT__
3687ccd8e176SBarry Smith #define __FUNCT__ "MatMPIAIJSetPreallocationCSR_MPIAIJ"
36887087cfbeSBarry Smith PetscErrorCode  MatMPIAIJSetPreallocationCSR_MPIAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3689ccd8e176SBarry Smith {
3690899cda47SBarry Smith   PetscInt       m,cstart, cend,j,nnz,i,d;
3691899cda47SBarry Smith   PetscInt       *d_nnz,*o_nnz,nnz_max = 0,rstart,ii;
3692ccd8e176SBarry Smith   const PetscInt *JJ;
3693ccd8e176SBarry Smith   PetscScalar    *values;
3694ccd8e176SBarry Smith   PetscErrorCode ierr;
3695ccd8e176SBarry Smith 
3696ccd8e176SBarry Smith   PetscFunctionBegin;
3697e32f2f54SBarry Smith   if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Ii[0] must be 0 it is %D",Ii[0]);
3698899cda47SBarry Smith 
369926283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
370026283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3701d0f46423SBarry Smith   m      = B->rmap->n;
3702d0f46423SBarry Smith   cstart = B->cmap->rstart;
3703d0f46423SBarry Smith   cend   = B->cmap->rend;
3704d0f46423SBarry Smith   rstart = B->rmap->rstart;
3705899cda47SBarry Smith 
37061d79065fSBarry Smith   ierr  = PetscMalloc2(m,PetscInt,&d_nnz,m,PetscInt,&o_nnz);CHKERRQ(ierr);
3707ccd8e176SBarry Smith 
3708ecc77c7aSBarry Smith #if defined(PETSC_USE_DEBUGGING)
3709ecc77c7aSBarry Smith   for (i=0; i<m; i++) {
3710ecc77c7aSBarry Smith     nnz     = Ii[i+1]- Ii[i];
3711ecc77c7aSBarry Smith     JJ      = J + Ii[i];
3712e32f2f54SBarry Smith     if (nnz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative %D number of columns",i,nnz);
3713ecc77c7aSBarry Smith     if (nnz && (JJ[0] < 0)) SETERRRQ1(PETSC_ERR_ARG_WRONGSTATE,"Row %D starts with negative column index",i,j);
3714d0f46423SBarry Smith     if (nnz && (JJ[nnz-1] >= B->cmap->N) SETERRRQ3(PETSC_ERR_ARG_WRONGSTATE,"Row %D ends with too large a column index %D (max allowed %D)",i,JJ[nnz-1],B->cmap->N);
3715ecc77c7aSBarry Smith   }
3716ecc77c7aSBarry Smith #endif
3717ecc77c7aSBarry Smith 
3718ccd8e176SBarry Smith   for (i=0; i<m; i++) {
3719b7940d39SSatish Balay     nnz     = Ii[i+1]- Ii[i];
3720b7940d39SSatish Balay     JJ      = J + Ii[i];
3721ccd8e176SBarry Smith     nnz_max = PetscMax(nnz_max,nnz);
3722ccd8e176SBarry Smith     d       = 0;
37230daa03b5SJed Brown     for (j=0; j<nnz; j++) {
37240daa03b5SJed Brown       if (cstart <= JJ[j] && JJ[j] < cend) d++;
3725ccd8e176SBarry Smith     }
3726ccd8e176SBarry Smith     d_nnz[i] = d;
3727ccd8e176SBarry Smith     o_nnz[i] = nnz - d;
3728ccd8e176SBarry Smith   }
3729ccd8e176SBarry Smith   ierr = MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);CHKERRQ(ierr);
37301d79065fSBarry Smith   ierr = PetscFree2(d_nnz,o_nnz);CHKERRQ(ierr);
3731ccd8e176SBarry Smith 
3732ccd8e176SBarry Smith   if (v) values = (PetscScalar*)v;
3733ccd8e176SBarry Smith   else {
3734ccd8e176SBarry Smith     ierr = PetscMalloc((nnz_max+1)*sizeof(PetscScalar),&values);CHKERRQ(ierr);
3735ccd8e176SBarry Smith     ierr = PetscMemzero(values,nnz_max*sizeof(PetscScalar));CHKERRQ(ierr);
3736ccd8e176SBarry Smith   }
3737ccd8e176SBarry Smith 
3738ccd8e176SBarry Smith   for (i=0; i<m; i++) {
3739ccd8e176SBarry Smith     ii   = i + rstart;
3740b7940d39SSatish Balay     nnz  = Ii[i+1]- Ii[i];
3741b7940d39SSatish Balay     ierr = MatSetValues_MPIAIJ(B,1,&ii,nnz,J+Ii[i],values+(v ? Ii[i] : 0),INSERT_VALUES);CHKERRQ(ierr);
3742ccd8e176SBarry Smith   }
3743ccd8e176SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3744ccd8e176SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3745ccd8e176SBarry Smith 
3746ccd8e176SBarry Smith   if (!v) {
3747ccd8e176SBarry Smith     ierr = PetscFree(values);CHKERRQ(ierr);
3748ccd8e176SBarry Smith   }
37497827cd58SJed Brown   ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
3750ccd8e176SBarry Smith   PetscFunctionReturn(0);
3751ccd8e176SBarry Smith }
3752e2e86b8fSSatish Balay EXTERN_C_END
3753ccd8e176SBarry Smith 
3754ccd8e176SBarry Smith #undef __FUNCT__
3755ccd8e176SBarry Smith #define __FUNCT__ "MatMPIAIJSetPreallocationCSR"
37561eea217eSSatish Balay /*@
3757ccd8e176SBarry Smith    MatMPIAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in AIJ format
3758ccd8e176SBarry Smith    (the default parallel PETSc format).
3759ccd8e176SBarry Smith 
3760ccd8e176SBarry Smith    Collective on MPI_Comm
3761ccd8e176SBarry Smith 
3762ccd8e176SBarry Smith    Input Parameters:
3763a1661176SMatthew Knepley +  B - the matrix
3764ccd8e176SBarry Smith .  i - the indices into j for the start of each local row (starts with zero)
37650daa03b5SJed Brown .  j - the column indices for each local row (starts with zero)
3766ccd8e176SBarry Smith -  v - optional values in the matrix
3767ccd8e176SBarry Smith 
3768ccd8e176SBarry Smith    Level: developer
3769ccd8e176SBarry Smith 
377012251496SSatish Balay    Notes:
377112251496SSatish Balay        The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc;
377212251496SSatish Balay      thus you CANNOT change the matrix entries by changing the values of a[] after you have
377312251496SSatish Balay      called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays.
377412251496SSatish Balay 
377512251496SSatish Balay        The i and j indices are 0 based, and i indices are indices corresponding to the local j array.
377612251496SSatish Balay 
377712251496SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
377812251496SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
377912251496SSatish Balay     as shown:
378012251496SSatish Balay 
378112251496SSatish Balay         1 0 0
378212251496SSatish Balay         2 0 3     P0
378312251496SSatish Balay        -------
378412251496SSatish Balay         4 5 6     P1
378512251496SSatish Balay 
378612251496SSatish Balay      Process0 [P0]: rows_owned=[0,1]
378712251496SSatish Balay         i =  {0,1,3}  [size = nrow+1  = 2+1]
378812251496SSatish Balay         j =  {0,0,2}  [size = nz = 6]
378912251496SSatish Balay         v =  {1,2,3}  [size = nz = 6]
379012251496SSatish Balay 
379112251496SSatish Balay      Process1 [P1]: rows_owned=[2]
379212251496SSatish Balay         i =  {0,3}    [size = nrow+1  = 1+1]
379312251496SSatish Balay         j =  {0,1,2}  [size = nz = 6]
379412251496SSatish Balay         v =  {4,5,6}  [size = nz = 6]
379512251496SSatish Balay 
3796ccd8e176SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
3797ccd8e176SBarry Smith 
379869b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatCreateAIJ(), MPIAIJ,
37998d7a6e47SBarry Smith           MatCreateSeqAIJWithArrays(), MatCreateMPIAIJWithSplitArrays()
3800ccd8e176SBarry Smith @*/
38017087cfbeSBarry Smith PetscErrorCode  MatMPIAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
3802ccd8e176SBarry Smith {
38034ac538c5SBarry Smith   PetscErrorCode ierr;
3804ccd8e176SBarry Smith 
3805ccd8e176SBarry Smith   PetscFunctionBegin;
38064ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatMPIAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr);
3807ccd8e176SBarry Smith   PetscFunctionReturn(0);
3808ccd8e176SBarry Smith }
3809ccd8e176SBarry Smith 
3810ccd8e176SBarry Smith #undef __FUNCT__
38114a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJSetPreallocation"
3812273d9f13SBarry Smith /*@C
3813ccd8e176SBarry Smith    MatMPIAIJSetPreallocation - Preallocates memory for a sparse parallel matrix in AIJ format
3814273d9f13SBarry Smith    (the default parallel PETSc format).  For good matrix assembly performance
3815273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameters
3816273d9f13SBarry Smith    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
3817273d9f13SBarry Smith    performance can be increased by more than a factor of 50.
3818273d9f13SBarry Smith 
3819273d9f13SBarry Smith    Collective on MPI_Comm
3820273d9f13SBarry Smith 
3821273d9f13SBarry Smith    Input Parameters:
3822273d9f13SBarry Smith +  A - the matrix
3823273d9f13SBarry Smith .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
3824273d9f13SBarry Smith            (same value is used for all local rows)
3825273d9f13SBarry Smith .  d_nnz - array containing the number of nonzeros in the various rows of the
3826273d9f13SBarry Smith            DIAGONAL portion of the local submatrix (possibly different for each row)
3827273d9f13SBarry Smith            or PETSC_NULL, if d_nz is used to specify the nonzero structure.
3828273d9f13SBarry Smith            The size of this array is equal to the number of local rows, i.e 'm'.
38293287b5eaSJed Brown            For matrices that will be factored, you must leave room for (and set)
38303287b5eaSJed Brown            the diagonal entry even if it is zero.
3831273d9f13SBarry Smith .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
3832273d9f13SBarry Smith            submatrix (same value is used for all local rows).
3833273d9f13SBarry Smith -  o_nnz - array containing the number of nonzeros in the various rows of the
3834273d9f13SBarry Smith            OFF-DIAGONAL portion of the local submatrix (possibly different for
3835273d9f13SBarry Smith            each row) or PETSC_NULL, if o_nz is used to specify the nonzero
3836273d9f13SBarry Smith            structure. The size of this array is equal to the number
3837273d9f13SBarry Smith            of local rows, i.e 'm'.
3838273d9f13SBarry Smith 
383949a6f317SBarry Smith    If the *_nnz parameter is given then the *_nz parameter is ignored
384049a6f317SBarry Smith 
3841273d9f13SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
3842ccd8e176SBarry Smith    compressed row storage (CSR)), is fully compatible with standard Fortran 77
38430598bfebSBarry Smith    storage.  The stored row and column indices begin with zero.
38440598bfebSBarry Smith    See the <A href="../../docs/manual.pdf#nameddest=ch_mat">Mat chapter of the users manual</A> for details.
3845273d9f13SBarry Smith 
3846273d9f13SBarry Smith    The parallel matrix is partitioned such that the first m0 rows belong to
3847273d9f13SBarry Smith    process 0, the next m1 rows belong to process 1, the next m2 rows belong
3848273d9f13SBarry Smith    to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.
3849273d9f13SBarry Smith 
3850273d9f13SBarry Smith    The DIAGONAL portion of the local submatrix of a processor can be defined
3851a05b864aSJed Brown    as the submatrix which is obtained by extraction the part corresponding to
3852a05b864aSJed Brown    the rows r1-r2 and columns c1-c2 of the global matrix, where r1 is the
3853a05b864aSJed Brown    first row that belongs to the processor, r2 is the last row belonging to
3854a05b864aSJed Brown    the this processor, and c1-c2 is range of indices of the local part of a
3855a05b864aSJed Brown    vector suitable for applying the matrix to.  This is an mxn matrix.  In the
3856a05b864aSJed Brown    common case of a square matrix, the row and column ranges are the same and
3857a05b864aSJed Brown    the DIAGONAL part is also square. The remaining portion of the local
3858a05b864aSJed Brown    submatrix (mxN) constitute the OFF-DIAGONAL portion.
3859273d9f13SBarry Smith 
3860273d9f13SBarry Smith    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
3861273d9f13SBarry Smith 
3862aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3863aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3864aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3865aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3866aa95bbe8SBarry Smith 
3867273d9f13SBarry Smith    Example usage:
3868273d9f13SBarry Smith 
3869273d9f13SBarry Smith    Consider the following 8x8 matrix with 34 non-zero values, that is
3870273d9f13SBarry Smith    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
3871273d9f13SBarry Smith    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
3872273d9f13SBarry Smith    as follows:
3873273d9f13SBarry Smith 
3874273d9f13SBarry Smith .vb
3875273d9f13SBarry Smith             1  2  0  |  0  3  0  |  0  4
3876273d9f13SBarry Smith     Proc0   0  5  6  |  7  0  0  |  8  0
3877273d9f13SBarry Smith             9  0 10  | 11  0  0  | 12  0
3878273d9f13SBarry Smith     -------------------------------------
3879273d9f13SBarry Smith            13  0 14  | 15 16 17  |  0  0
3880273d9f13SBarry Smith     Proc1   0 18  0  | 19 20 21  |  0  0
3881273d9f13SBarry Smith             0  0  0  | 22 23  0  | 24  0
3882273d9f13SBarry Smith     -------------------------------------
3883273d9f13SBarry Smith     Proc2  25 26 27  |  0  0 28  | 29  0
3884273d9f13SBarry Smith            30  0  0  | 31 32 33  |  0 34
3885273d9f13SBarry Smith .ve
3886273d9f13SBarry Smith 
3887273d9f13SBarry Smith    This can be represented as a collection of submatrices as:
3888273d9f13SBarry Smith 
3889273d9f13SBarry Smith .vb
3890273d9f13SBarry Smith       A B C
3891273d9f13SBarry Smith       D E F
3892273d9f13SBarry Smith       G H I
3893273d9f13SBarry Smith .ve
3894273d9f13SBarry Smith 
3895273d9f13SBarry Smith    Where the submatrices A,B,C are owned by proc0, D,E,F are
3896273d9f13SBarry Smith    owned by proc1, G,H,I are owned by proc2.
3897273d9f13SBarry Smith 
3898273d9f13SBarry Smith    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
3899273d9f13SBarry Smith    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
3900273d9f13SBarry Smith    The 'M','N' parameters are 8,8, and have the same values on all procs.
3901273d9f13SBarry Smith 
3902273d9f13SBarry Smith    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
3903273d9f13SBarry Smith    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
3904273d9f13SBarry Smith    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
3905273d9f13SBarry Smith    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
3906273d9f13SBarry Smith    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
3907273d9f13SBarry Smith    matrix, ans [DF] as another SeqAIJ matrix.
3908273d9f13SBarry Smith 
3909273d9f13SBarry Smith    When d_nz, o_nz parameters are specified, d_nz storage elements are
3910273d9f13SBarry Smith    allocated for every row of the local diagonal submatrix, and o_nz
3911273d9f13SBarry Smith    storage locations are allocated for every row of the OFF-DIAGONAL submat.
3912273d9f13SBarry Smith    One way to choose d_nz and o_nz is to use the max nonzerors per local
3913273d9f13SBarry Smith    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
3914273d9f13SBarry Smith    In this case, the values of d_nz,o_nz are:
3915273d9f13SBarry Smith .vb
3916273d9f13SBarry Smith      proc0 : dnz = 2, o_nz = 2
3917273d9f13SBarry Smith      proc1 : dnz = 3, o_nz = 2
3918273d9f13SBarry Smith      proc2 : dnz = 1, o_nz = 4
3919273d9f13SBarry Smith .ve
3920273d9f13SBarry Smith    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
3921273d9f13SBarry Smith    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
3922273d9f13SBarry Smith    for proc3. i.e we are using 12+15+10=37 storage locations to store
3923273d9f13SBarry Smith    34 values.
3924273d9f13SBarry Smith 
3925273d9f13SBarry Smith    When d_nnz, o_nnz parameters are specified, the storage is specified
3926273d9f13SBarry Smith    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
3927273d9f13SBarry Smith    In the above case the values for d_nnz,o_nnz are:
3928273d9f13SBarry Smith .vb
3929273d9f13SBarry Smith      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
3930273d9f13SBarry Smith      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
3931273d9f13SBarry Smith      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
3932273d9f13SBarry Smith .ve
3933273d9f13SBarry Smith    Here the space allocated is sum of all the above values i.e 34, and
3934273d9f13SBarry Smith    hence pre-allocation is perfect.
3935273d9f13SBarry Smith 
3936273d9f13SBarry Smith    Level: intermediate
3937273d9f13SBarry Smith 
3938273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
3939273d9f13SBarry Smith 
394069b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateAIJ(), MatMPIAIJSetPreallocationCSR(),
3941aa95bbe8SBarry Smith           MPIAIJ, MatGetInfo()
3942273d9f13SBarry Smith @*/
39437087cfbeSBarry Smith PetscErrorCode  MatMPIAIJSetPreallocation(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
3944273d9f13SBarry Smith {
39454ac538c5SBarry Smith   PetscErrorCode ierr;
3946273d9f13SBarry Smith 
3947273d9f13SBarry Smith   PetscFunctionBegin;
39486ba663aaSJed Brown   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
39496ba663aaSJed Brown   PetscValidType(B,1);
39504ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatMPIAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[]),(B,d_nz,d_nnz,o_nz,o_nnz));CHKERRQ(ierr);
3951273d9f13SBarry Smith   PetscFunctionReturn(0);
3952273d9f13SBarry Smith }
3953273d9f13SBarry Smith 
39544a2ae208SSatish Balay #undef __FUNCT__
39552fb0ec9aSBarry Smith #define __FUNCT__ "MatCreateMPIAIJWithArrays"
395658d36128SBarry Smith /*@
39572fb0ec9aSBarry Smith      MatCreateMPIAIJWithArrays - creates a MPI AIJ matrix using arrays that contain in standard
39582fb0ec9aSBarry Smith          CSR format the local rows.
39592fb0ec9aSBarry Smith 
39602fb0ec9aSBarry Smith    Collective on MPI_Comm
39612fb0ec9aSBarry Smith 
39622fb0ec9aSBarry Smith    Input Parameters:
39632fb0ec9aSBarry Smith +  comm - MPI communicator
39642fb0ec9aSBarry Smith .  m - number of local rows (Cannot be PETSC_DECIDE)
39652fb0ec9aSBarry Smith .  n - This value should be the same as the local size used in creating the
39662fb0ec9aSBarry Smith        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
39672fb0ec9aSBarry Smith        calculated if N is given) For square matrices n is almost always m.
39682fb0ec9aSBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
39692fb0ec9aSBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
39702fb0ec9aSBarry Smith .   i - row indices
39712fb0ec9aSBarry Smith .   j - column indices
39722fb0ec9aSBarry Smith -   a - matrix values
39732fb0ec9aSBarry Smith 
39742fb0ec9aSBarry Smith    Output Parameter:
39752fb0ec9aSBarry Smith .   mat - the matrix
397603bfb495SBarry Smith 
39772fb0ec9aSBarry Smith    Level: intermediate
39782fb0ec9aSBarry Smith 
39792fb0ec9aSBarry Smith    Notes:
39802fb0ec9aSBarry Smith        The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc;
39812fb0ec9aSBarry Smith      thus you CANNOT change the matrix entries by changing the values of a[] after you have
39828d7a6e47SBarry Smith      called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays.
39832fb0ec9aSBarry Smith 
398412251496SSatish Balay        The i and j indices are 0 based, and i indices are indices corresponding to the local j array.
398512251496SSatish Balay 
398612251496SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
398712251496SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
398812251496SSatish Balay     as shown:
398912251496SSatish Balay 
399012251496SSatish Balay         1 0 0
399112251496SSatish Balay         2 0 3     P0
399212251496SSatish Balay        -------
399312251496SSatish Balay         4 5 6     P1
399412251496SSatish Balay 
399512251496SSatish Balay      Process0 [P0]: rows_owned=[0,1]
399612251496SSatish Balay         i =  {0,1,3}  [size = nrow+1  = 2+1]
399712251496SSatish Balay         j =  {0,0,2}  [size = nz = 6]
399812251496SSatish Balay         v =  {1,2,3}  [size = nz = 6]
399912251496SSatish Balay 
400012251496SSatish Balay      Process1 [P1]: rows_owned=[2]
400112251496SSatish Balay         i =  {0,3}    [size = nrow+1  = 1+1]
400212251496SSatish Balay         j =  {0,1,2}  [size = nz = 6]
400312251496SSatish Balay         v =  {4,5,6}  [size = nz = 6]
40042fb0ec9aSBarry Smith 
40052fb0ec9aSBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
40062fb0ec9aSBarry Smith 
40072fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
400869b1f4b7SBarry Smith           MPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithSplitArrays()
40092fb0ec9aSBarry Smith @*/
40107087cfbeSBarry Smith PetscErrorCode  MatCreateMPIAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,const PetscInt i[],const PetscInt j[],const PetscScalar a[],Mat *mat)
40112fb0ec9aSBarry Smith {
40122fb0ec9aSBarry Smith   PetscErrorCode ierr;
40132fb0ec9aSBarry Smith 
40142fb0ec9aSBarry Smith   PetscFunctionBegin;
401569b1f4b7SBarry Smith   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4016e32f2f54SBarry Smith   if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative");
40172fb0ec9aSBarry Smith   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
4018d4146a68SBarry Smith   ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr);
4019a2f3521dSMark F. Adams   /* ierr = MatSetBlockSizes(M,bs,cbs); CHKERRQ(ierr); */
40202fb0ec9aSBarry Smith   ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr);
40212fb0ec9aSBarry Smith   ierr = MatMPIAIJSetPreallocationCSR(*mat,i,j,a);CHKERRQ(ierr);
40222fb0ec9aSBarry Smith   PetscFunctionReturn(0);
40232fb0ec9aSBarry Smith }
40242fb0ec9aSBarry Smith 
40252fb0ec9aSBarry Smith #undef __FUNCT__
402669b1f4b7SBarry Smith #define __FUNCT__ "MatCreateAIJ"
4027273d9f13SBarry Smith /*@C
402869b1f4b7SBarry Smith    MatCreateAIJ - Creates a sparse parallel matrix in AIJ format
4029273d9f13SBarry Smith    (the default parallel PETSc format).  For good matrix assembly performance
4030273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameters
4031273d9f13SBarry Smith    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
4032273d9f13SBarry Smith    performance can be increased by more than a factor of 50.
4033273d9f13SBarry Smith 
4034273d9f13SBarry Smith    Collective on MPI_Comm
4035273d9f13SBarry Smith 
4036273d9f13SBarry Smith    Input Parameters:
4037273d9f13SBarry Smith +  comm - MPI communicator
4038273d9f13SBarry Smith .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
4039273d9f13SBarry Smith            This value should be the same as the local size used in creating the
4040273d9f13SBarry Smith            y vector for the matrix-vector product y = Ax.
4041273d9f13SBarry Smith .  n - This value should be the same as the local size used in creating the
4042273d9f13SBarry Smith        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
4043273d9f13SBarry Smith        calculated if N is given) For square matrices n is almost always m.
4044273d9f13SBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
4045273d9f13SBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
4046273d9f13SBarry Smith .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
4047273d9f13SBarry Smith            (same value is used for all local rows)
4048273d9f13SBarry Smith .  d_nnz - array containing the number of nonzeros in the various rows of the
4049273d9f13SBarry Smith            DIAGONAL portion of the local submatrix (possibly different for each row)
4050273d9f13SBarry Smith            or PETSC_NULL, if d_nz is used to specify the nonzero structure.
4051273d9f13SBarry Smith            The size of this array is equal to the number of local rows, i.e 'm'.
4052273d9f13SBarry Smith .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
4053273d9f13SBarry Smith            submatrix (same value is used for all local rows).
4054273d9f13SBarry Smith -  o_nnz - array containing the number of nonzeros in the various rows of the
4055273d9f13SBarry Smith            OFF-DIAGONAL portion of the local submatrix (possibly different for
4056273d9f13SBarry Smith            each row) or PETSC_NULL, if o_nz is used to specify the nonzero
4057273d9f13SBarry Smith            structure. The size of this array is equal to the number
4058273d9f13SBarry Smith            of local rows, i.e 'm'.
4059273d9f13SBarry Smith 
4060273d9f13SBarry Smith    Output Parameter:
4061273d9f13SBarry Smith .  A - the matrix
4062273d9f13SBarry Smith 
4063175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
4064ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
4065175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
4066175b88e8SBarry Smith 
4067273d9f13SBarry Smith    Notes:
406849a6f317SBarry Smith    If the *_nnz parameter is given then the *_nz parameter is ignored
406949a6f317SBarry Smith 
4070273d9f13SBarry Smith    m,n,M,N parameters specify the size of the matrix, and its partitioning across
4071273d9f13SBarry Smith    processors, while d_nz,d_nnz,o_nz,o_nnz parameters specify the approximate
4072273d9f13SBarry Smith    storage requirements for this matrix.
4073273d9f13SBarry Smith 
4074273d9f13SBarry Smith    If PETSC_DECIDE or  PETSC_DETERMINE is used for a particular argument on one
4075273d9f13SBarry Smith    processor than it must be used on all processors that share the object for
4076273d9f13SBarry Smith    that argument.
4077273d9f13SBarry Smith 
4078273d9f13SBarry Smith    The user MUST specify either the local or global matrix dimensions
4079273d9f13SBarry Smith    (possibly both).
4080273d9f13SBarry Smith 
408133a7c187SSatish Balay    The parallel matrix is partitioned across processors such that the
408233a7c187SSatish Balay    first m0 rows belong to process 0, the next m1 rows belong to
408333a7c187SSatish Balay    process 1, the next m2 rows belong to process 2 etc.. where
408433a7c187SSatish Balay    m0,m1,m2,.. are the input parameter 'm'. i.e each processor stores
408533a7c187SSatish Balay    values corresponding to [m x N] submatrix.
4086273d9f13SBarry Smith 
408733a7c187SSatish Balay    The columns are logically partitioned with the n0 columns belonging
408833a7c187SSatish Balay    to 0th partition, the next n1 columns belonging to the next
408933a7c187SSatish Balay    partition etc.. where n0,n1,n2... are the the input parameter 'n'.
409033a7c187SSatish Balay 
409133a7c187SSatish Balay    The DIAGONAL portion of the local submatrix on any given processor
409233a7c187SSatish Balay    is the submatrix corresponding to the rows and columns m,n
409333a7c187SSatish Balay    corresponding to the given processor. i.e diagonal matrix on
409433a7c187SSatish Balay    process 0 is [m0 x n0], diagonal matrix on process 1 is [m1 x n1]
409533a7c187SSatish Balay    etc. The remaining portion of the local submatrix [m x (N-n)]
409633a7c187SSatish Balay    constitute the OFF-DIAGONAL portion. The example below better
409733a7c187SSatish Balay    illustrates this concept.
409833a7c187SSatish Balay 
409933a7c187SSatish Balay    For a square global matrix we define each processor's diagonal portion
410033a7c187SSatish Balay    to be its local rows and the corresponding columns (a square submatrix);
410133a7c187SSatish Balay    each processor's off-diagonal portion encompasses the remainder of the
410233a7c187SSatish Balay    local matrix (a rectangular submatrix).
4103273d9f13SBarry Smith 
4104273d9f13SBarry Smith    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
4105273d9f13SBarry Smith 
410697d05335SKris Buschelman    When calling this routine with a single process communicator, a matrix of
410797d05335SKris Buschelman    type SEQAIJ is returned.  If a matrix of type MPIAIJ is desired for this
410897d05335SKris Buschelman    type of communicator, use the construction mechanism:
410978102f6cSMatthew Knepley      MatCreate(...,&A); MatSetType(A,MATMPIAIJ); MatSetSizes(A, m,n,M,N); MatMPIAIJSetPreallocation(A,...);
411097d05335SKris Buschelman 
4111273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible.
4112273d9f13SBarry Smith    We search for consecutive rows with the same nonzero structure, thereby
4113273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
4114273d9f13SBarry Smith 
4115273d9f13SBarry Smith    Options Database Keys:
4116923f20ffSKris Buschelman +  -mat_no_inode  - Do not use inodes
4117923f20ffSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
4118273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
4119273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
4120273d9f13SBarry Smith         the user still MUST index entries starting at 0!
4121273d9f13SBarry Smith 
4122273d9f13SBarry Smith 
4123273d9f13SBarry Smith    Example usage:
4124273d9f13SBarry Smith 
4125273d9f13SBarry Smith    Consider the following 8x8 matrix with 34 non-zero values, that is
4126273d9f13SBarry Smith    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
4127273d9f13SBarry Smith    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
4128273d9f13SBarry Smith    as follows:
4129273d9f13SBarry Smith 
4130273d9f13SBarry Smith .vb
4131273d9f13SBarry Smith             1  2  0  |  0  3  0  |  0  4
4132273d9f13SBarry Smith     Proc0   0  5  6  |  7  0  0  |  8  0
4133273d9f13SBarry Smith             9  0 10  | 11  0  0  | 12  0
4134273d9f13SBarry Smith     -------------------------------------
4135273d9f13SBarry Smith            13  0 14  | 15 16 17  |  0  0
4136273d9f13SBarry Smith     Proc1   0 18  0  | 19 20 21  |  0  0
4137273d9f13SBarry Smith             0  0  0  | 22 23  0  | 24  0
4138273d9f13SBarry Smith     -------------------------------------
4139273d9f13SBarry Smith     Proc2  25 26 27  |  0  0 28  | 29  0
4140273d9f13SBarry Smith            30  0  0  | 31 32 33  |  0 34
4141273d9f13SBarry Smith .ve
4142273d9f13SBarry Smith 
4143273d9f13SBarry Smith    This can be represented as a collection of submatrices as:
4144273d9f13SBarry Smith 
4145273d9f13SBarry Smith .vb
4146273d9f13SBarry Smith       A B C
4147273d9f13SBarry Smith       D E F
4148273d9f13SBarry Smith       G H I
4149273d9f13SBarry Smith .ve
4150273d9f13SBarry Smith 
4151273d9f13SBarry Smith    Where the submatrices A,B,C are owned by proc0, D,E,F are
4152273d9f13SBarry Smith    owned by proc1, G,H,I are owned by proc2.
4153273d9f13SBarry Smith 
4154273d9f13SBarry Smith    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
4155273d9f13SBarry Smith    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
4156273d9f13SBarry Smith    The 'M','N' parameters are 8,8, and have the same values on all procs.
4157273d9f13SBarry Smith 
4158273d9f13SBarry Smith    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
4159273d9f13SBarry Smith    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
4160273d9f13SBarry Smith    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
4161273d9f13SBarry Smith    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
4162273d9f13SBarry Smith    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
4163273d9f13SBarry Smith    matrix, ans [DF] as another SeqAIJ matrix.
4164273d9f13SBarry Smith 
4165273d9f13SBarry Smith    When d_nz, o_nz parameters are specified, d_nz storage elements are
4166273d9f13SBarry Smith    allocated for every row of the local diagonal submatrix, and o_nz
4167273d9f13SBarry Smith    storage locations are allocated for every row of the OFF-DIAGONAL submat.
4168273d9f13SBarry Smith    One way to choose d_nz and o_nz is to use the max nonzerors per local
4169273d9f13SBarry Smith    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
4170273d9f13SBarry Smith    In this case, the values of d_nz,o_nz are:
4171273d9f13SBarry Smith .vb
4172273d9f13SBarry Smith      proc0 : dnz = 2, o_nz = 2
4173273d9f13SBarry Smith      proc1 : dnz = 3, o_nz = 2
4174273d9f13SBarry Smith      proc2 : dnz = 1, o_nz = 4
4175273d9f13SBarry Smith .ve
4176273d9f13SBarry Smith    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
4177273d9f13SBarry Smith    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
4178273d9f13SBarry Smith    for proc3. i.e we are using 12+15+10=37 storage locations to store
4179273d9f13SBarry Smith    34 values.
4180273d9f13SBarry Smith 
4181273d9f13SBarry Smith    When d_nnz, o_nnz parameters are specified, the storage is specified
4182273d9f13SBarry Smith    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
4183273d9f13SBarry Smith    In the above case the values for d_nnz,o_nnz are:
4184273d9f13SBarry Smith .vb
4185273d9f13SBarry Smith      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
4186273d9f13SBarry Smith      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
4187273d9f13SBarry Smith      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
4188273d9f13SBarry Smith .ve
4189273d9f13SBarry Smith    Here the space allocated is sum of all the above values i.e 34, and
4190273d9f13SBarry Smith    hence pre-allocation is perfect.
4191273d9f13SBarry Smith 
4192273d9f13SBarry Smith    Level: intermediate
4193273d9f13SBarry Smith 
4194273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
4195273d9f13SBarry Smith 
4196ccd8e176SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
41972fb0ec9aSBarry Smith           MPIAIJ, MatCreateMPIAIJWithArrays()
4198273d9f13SBarry Smith @*/
419969b1f4b7SBarry Smith PetscErrorCode  MatCreateAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[],Mat *A)
4200273d9f13SBarry Smith {
42016849ba73SBarry Smith   PetscErrorCode ierr;
4202b1d57f15SBarry Smith   PetscMPIInt    size;
4203273d9f13SBarry Smith 
4204273d9f13SBarry Smith   PetscFunctionBegin;
4205f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
4206f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*A,m,n,M,N);CHKERRQ(ierr);
4207273d9f13SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
4208273d9f13SBarry Smith   if (size > 1) {
4209273d9f13SBarry Smith     ierr = MatSetType(*A,MATMPIAIJ);CHKERRQ(ierr);
4210273d9f13SBarry Smith     ierr = MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr);
4211273d9f13SBarry Smith   } else {
4212273d9f13SBarry Smith     ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
4213273d9f13SBarry Smith     ierr = MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);CHKERRQ(ierr);
4214273d9f13SBarry Smith   }
4215273d9f13SBarry Smith   PetscFunctionReturn(0);
4216273d9f13SBarry Smith }
4217195d93cdSBarry Smith 
42184a2ae208SSatish Balay #undef __FUNCT__
42194a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJGetSeqAIJ"
42207087cfbeSBarry Smith PetscErrorCode  MatMPIAIJGetSeqAIJ(Mat A,Mat *Ad,Mat *Ao,PetscInt *colmap[])
4221195d93cdSBarry Smith {
4222195d93cdSBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;
4223b1d57f15SBarry Smith 
4224195d93cdSBarry Smith   PetscFunctionBegin;
4225195d93cdSBarry Smith   *Ad     = a->A;
4226195d93cdSBarry Smith   *Ao     = a->B;
4227195d93cdSBarry Smith   *colmap = a->garray;
4228195d93cdSBarry Smith   PetscFunctionReturn(0);
4229195d93cdSBarry Smith }
4230a2243be0SBarry Smith 
4231a2243be0SBarry Smith #undef __FUNCT__
4232a2243be0SBarry Smith #define __FUNCT__ "MatSetColoring_MPIAIJ"
4233dfbe8321SBarry Smith PetscErrorCode MatSetColoring_MPIAIJ(Mat A,ISColoring coloring)
4234a2243be0SBarry Smith {
4235dfbe8321SBarry Smith   PetscErrorCode ierr;
4236b1d57f15SBarry Smith   PetscInt       i;
4237a2243be0SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
4238a2243be0SBarry Smith 
4239a2243be0SBarry Smith   PetscFunctionBegin;
42408ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
424108b6dcc0SBarry Smith     ISColoringValue *allcolors,*colors;
4242a2243be0SBarry Smith     ISColoring      ocoloring;
4243a2243be0SBarry Smith 
4244a2243be0SBarry Smith     /* set coloring for diagonal portion */
4245a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->A,coloring);CHKERRQ(ierr);
4246a2243be0SBarry Smith 
4247a2243be0SBarry Smith     /* set coloring for off-diagonal portion */
42487adad957SLisandro Dalcin     ierr = ISAllGatherColors(((PetscObject)A)->comm,coloring->n,coloring->colors,PETSC_NULL,&allcolors);CHKERRQ(ierr);
4249d0f46423SBarry Smith     ierr = PetscMalloc((a->B->cmap->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
4250d0f46423SBarry Smith     for (i=0; i<a->B->cmap->n; i++) {
4251a2243be0SBarry Smith       colors[i] = allcolors[a->garray[i]];
4252a2243be0SBarry Smith     }
4253a2243be0SBarry Smith     ierr = PetscFree(allcolors);CHKERRQ(ierr);
4254d0f46423SBarry Smith     ierr = ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
4255a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr);
42566bf464f9SBarry Smith     ierr = ISColoringDestroy(&ocoloring);CHKERRQ(ierr);
4257a2243be0SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
425808b6dcc0SBarry Smith     ISColoringValue *colors;
4259b1d57f15SBarry Smith     PetscInt        *larray;
4260a2243be0SBarry Smith     ISColoring      ocoloring;
4261a2243be0SBarry Smith 
4262a2243be0SBarry Smith     /* set coloring for diagonal portion */
4263d0f46423SBarry Smith     ierr = PetscMalloc((a->A->cmap->n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr);
4264d0f46423SBarry Smith     for (i=0; i<a->A->cmap->n; i++) {
4265d0f46423SBarry Smith       larray[i] = i + A->cmap->rstart;
4266a2243be0SBarry Smith     }
4267992144d0SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,a->A->cmap->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
4268d0f46423SBarry Smith     ierr = PetscMalloc((a->A->cmap->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
4269d0f46423SBarry Smith     for (i=0; i<a->A->cmap->n; i++) {
4270a2243be0SBarry Smith       colors[i] = coloring->colors[larray[i]];
4271a2243be0SBarry Smith     }
4272a2243be0SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
4273d0f46423SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,a->A->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
4274a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->A,ocoloring);CHKERRQ(ierr);
42756bf464f9SBarry Smith     ierr = ISColoringDestroy(&ocoloring);CHKERRQ(ierr);
4276a2243be0SBarry Smith 
4277a2243be0SBarry Smith     /* set coloring for off-diagonal portion */
4278d0f46423SBarry Smith     ierr = PetscMalloc((a->B->cmap->n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr);
4279992144d0SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,a->B->cmap->n,a->garray,PETSC_NULL,larray);CHKERRQ(ierr);
4280d0f46423SBarry Smith     ierr = PetscMalloc((a->B->cmap->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
4281d0f46423SBarry Smith     for (i=0; i<a->B->cmap->n; i++) {
4282a2243be0SBarry Smith       colors[i] = coloring->colors[larray[i]];
4283a2243be0SBarry Smith     }
4284a2243be0SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
4285d0f46423SBarry Smith     ierr = ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
4286a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr);
42876bf464f9SBarry Smith     ierr = ISColoringDestroy(&ocoloring);CHKERRQ(ierr);
42886bf464f9SBarry Smith   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support ISColoringType %d",(int)coloring->ctype);
4289a2243be0SBarry Smith 
4290a2243be0SBarry Smith   PetscFunctionReturn(0);
4291a2243be0SBarry Smith }
4292a2243be0SBarry Smith 
4293dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
4294a2243be0SBarry Smith #undef __FUNCT__
4295779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdic_MPIAIJ"
4296dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_MPIAIJ(Mat A,void *advalues)
4297a2243be0SBarry Smith {
4298a2243be0SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
4299dfbe8321SBarry Smith   PetscErrorCode ierr;
4300a2243be0SBarry Smith 
4301a2243be0SBarry Smith   PetscFunctionBegin;
4302779c1a83SBarry Smith   ierr = MatSetValuesAdic_SeqAIJ(a->A,advalues);CHKERRQ(ierr);
4303779c1a83SBarry Smith   ierr = MatSetValuesAdic_SeqAIJ(a->B,advalues);CHKERRQ(ierr);
4304779c1a83SBarry Smith   PetscFunctionReturn(0);
4305779c1a83SBarry Smith }
4306dcf5cc72SBarry Smith #endif
4307779c1a83SBarry Smith 
4308779c1a83SBarry Smith #undef __FUNCT__
4309779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdifor_MPIAIJ"
4310b1d57f15SBarry Smith PetscErrorCode MatSetValuesAdifor_MPIAIJ(Mat A,PetscInt nl,void *advalues)
4311779c1a83SBarry Smith {
4312779c1a83SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
4313dfbe8321SBarry Smith   PetscErrorCode ierr;
4314779c1a83SBarry Smith 
4315779c1a83SBarry Smith   PetscFunctionBegin;
4316779c1a83SBarry Smith   ierr = MatSetValuesAdifor_SeqAIJ(a->A,nl,advalues);CHKERRQ(ierr);
4317779c1a83SBarry Smith   ierr = MatSetValuesAdifor_SeqAIJ(a->B,nl,advalues);CHKERRQ(ierr);
4318a2243be0SBarry Smith   PetscFunctionReturn(0);
4319a2243be0SBarry Smith }
4320c5d6d63eSBarry Smith 
4321c5d6d63eSBarry Smith #undef __FUNCT__
432290431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJConcatenateSeqAIJSymbolic"
432390431a8fSHong Zhang PetscErrorCode  MatCreateMPIAIJConcatenateSeqAIJSymbolic(MPI_Comm comm,Mat inmat,PetscInt n,Mat *outmat)
43249b8102ccSHong Zhang {
43259b8102ccSHong Zhang   PetscErrorCode ierr;
4326a2f3521dSMark F. Adams   PetscInt       m,N,i,rstart,nnz,*dnz,*onz,sum,bs,cbs;
43279b8102ccSHong Zhang   PetscInt       *indx;
43289b8102ccSHong Zhang 
43299b8102ccSHong Zhang   PetscFunctionBegin;
43309b8102ccSHong Zhang   /* This routine will ONLY return MPIAIJ type matrix */
43319b8102ccSHong Zhang   ierr = MatGetSize(inmat,&m,&N);CHKERRQ(ierr);
4332a2f3521dSMark F. Adams   ierr = MatGetBlockSizes(inmat,&bs,&cbs);CHKERRQ(ierr);
43339b8102ccSHong Zhang   if (n == PETSC_DECIDE){
43349b8102ccSHong Zhang     ierr = PetscSplitOwnership(comm,&n,&N);CHKERRQ(ierr);
43359b8102ccSHong Zhang   }
4336a22543b6SHong Zhang   /* Check sum(n) = N */
4337a95133b1SBarry Smith   ierr = MPI_Allreduce(&n,&sum,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr);
4338a22543b6SHong Zhang   if (sum != N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Sum of local columns != global columns %d",N);
4339a22543b6SHong Zhang 
43409b8102ccSHong Zhang   ierr = MPI_Scan(&m, &rstart,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr);
43419b8102ccSHong Zhang   rstart -= m;
43429b8102ccSHong Zhang 
43439b8102ccSHong Zhang   ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr);
43449b8102ccSHong Zhang   for (i=0;i<m;i++) {
43459b8102ccSHong Zhang     ierr = MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,PETSC_NULL);CHKERRQ(ierr);
43469b8102ccSHong Zhang     ierr = MatPreallocateSet(i+rstart,nnz,indx,dnz,onz);CHKERRQ(ierr);
43479b8102ccSHong Zhang     ierr = MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,PETSC_NULL);CHKERRQ(ierr);
43489b8102ccSHong Zhang   }
43499b8102ccSHong Zhang 
43509b8102ccSHong Zhang   ierr = MatCreate(comm,outmat);CHKERRQ(ierr);
43519b8102ccSHong Zhang   ierr = MatSetSizes(*outmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE); CHKERRQ(ierr);
4352a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(*outmat,bs,cbs); CHKERRQ(ierr);
43539b8102ccSHong Zhang   ierr = MatSetType(*outmat,MATMPIAIJ);  CHKERRQ(ierr);
43549b8102ccSHong Zhang   ierr = MatMPIAIJSetPreallocation(*outmat,0,dnz,0,onz);CHKERRQ(ierr);
43559b8102ccSHong Zhang   ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr);
43569b8102ccSHong Zhang   PetscFunctionReturn(0);
43579b8102ccSHong Zhang }
43589b8102ccSHong Zhang 
43599b8102ccSHong Zhang #undef __FUNCT__
436090431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJConcatenateSeqAIJNumeric"
436190431a8fSHong Zhang PetscErrorCode  MatCreateMPIAIJConcatenateSeqAIJNumeric(MPI_Comm comm,Mat inmat,PetscInt n,Mat outmat)
43629b8102ccSHong Zhang {
43639b8102ccSHong Zhang   PetscErrorCode ierr;
43649b8102ccSHong Zhang   PetscInt       m,N,i,rstart,nnz,Ii;
43659b8102ccSHong Zhang   PetscInt       *indx;
43669b8102ccSHong Zhang   PetscScalar    *values;
43679b8102ccSHong Zhang 
43689b8102ccSHong Zhang   PetscFunctionBegin;
43699b8102ccSHong Zhang   ierr = MatGetSize(inmat,&m,&N);CHKERRQ(ierr);
43709b8102ccSHong Zhang   ierr = MatGetOwnershipRange(outmat,&rstart,PETSC_NULL);CHKERRQ(ierr);
43719b8102ccSHong Zhang   for (i=0;i<m;i++) {
43729b8102ccSHong Zhang     ierr = MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
43739b8102ccSHong Zhang     Ii    = i + rstart;
4374a22543b6SHong Zhang     ierr = MatSetValues_MPIAIJ(outmat,1,&Ii,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr);
43759b8102ccSHong Zhang     ierr = MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
43769b8102ccSHong Zhang   }
43779b8102ccSHong Zhang   ierr = MatAssemblyBegin(outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
43789b8102ccSHong Zhang   ierr = MatAssemblyEnd(outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
43799b8102ccSHong Zhang   PetscFunctionReturn(0);
43809b8102ccSHong Zhang }
43819b8102ccSHong Zhang 
43829b8102ccSHong Zhang #undef __FUNCT__
438390431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJConcatenateSeqAIJ"
4384bc08b0f1SBarry Smith /*@
438590431a8fSHong Zhang       MatCreateMPIAIJConcatenateSeqAIJ - Creates a single large PETSc matrix by concatenating sequential
438651dd7536SBarry Smith                  matrices from each processor
4387c5d6d63eSBarry Smith 
4388c5d6d63eSBarry Smith     Collective on MPI_Comm
4389c5d6d63eSBarry Smith 
4390c5d6d63eSBarry Smith    Input Parameters:
439151dd7536SBarry Smith +    comm - the communicators the parallel matrix will live on
4392d6bb3c2dSHong Zhang .    inmat - the input sequential matrices
43930e36024fSHong Zhang .    n - number of local columns (or PETSC_DECIDE)
4394d6bb3c2dSHong Zhang -    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
439551dd7536SBarry Smith 
439651dd7536SBarry Smith    Output Parameter:
439751dd7536SBarry Smith .    outmat - the parallel matrix generated
4398c5d6d63eSBarry Smith 
43997e25d530SSatish Balay     Level: advanced
44007e25d530SSatish Balay 
4401f08fae4eSHong Zhang    Notes: The number of columns of the matrix in EACH processor MUST be the same.
4402c5d6d63eSBarry Smith 
4403c5d6d63eSBarry Smith @*/
440490431a8fSHong Zhang PetscErrorCode  MatCreateMPIAIJConcatenateSeqAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
4405c5d6d63eSBarry Smith {
4406dfbe8321SBarry Smith   PetscErrorCode ierr;
4407c5d6d63eSBarry Smith 
4408c5d6d63eSBarry Smith   PetscFunctionBegin;
44099b8102ccSHong Zhang   ierr = PetscLogEventBegin(MAT_Merge,inmat,0,0,0);CHKERRQ(ierr);
4410d6bb3c2dSHong Zhang   if (scall == MAT_INITIAL_MATRIX){
441190431a8fSHong Zhang     ierr = MatCreateMPIAIJConcatenateSeqAIJSymbolic(comm,inmat,n,outmat);CHKERRQ(ierr);
44120e36024fSHong Zhang   }
441390431a8fSHong Zhang   ierr = MatCreateMPIAIJConcatenateSeqAIJNumeric(comm,inmat,n,*outmat);CHKERRQ(ierr);
44149b8102ccSHong Zhang   ierr = PetscLogEventEnd(MAT_Merge,inmat,0,0,0);CHKERRQ(ierr);
4415c5d6d63eSBarry Smith   PetscFunctionReturn(0);
4416c5d6d63eSBarry Smith }
4417c5d6d63eSBarry Smith 
4418c5d6d63eSBarry Smith #undef __FUNCT__
4419c5d6d63eSBarry Smith #define __FUNCT__ "MatFileSplit"
4420dfbe8321SBarry Smith PetscErrorCode MatFileSplit(Mat A,char *outfile)
4421c5d6d63eSBarry Smith {
4422dfbe8321SBarry Smith   PetscErrorCode    ierr;
442332dcc486SBarry Smith   PetscMPIInt       rank;
4424b1d57f15SBarry Smith   PetscInt          m,N,i,rstart,nnz;
4425de4209c5SBarry Smith   size_t            len;
4426b1d57f15SBarry Smith   const PetscInt    *indx;
4427c5d6d63eSBarry Smith   PetscViewer       out;
4428c5d6d63eSBarry Smith   char              *name;
4429c5d6d63eSBarry Smith   Mat               B;
4430b3cc6726SBarry Smith   const PetscScalar *values;
4431c5d6d63eSBarry Smith 
4432c5d6d63eSBarry Smith   PetscFunctionBegin;
4433c5d6d63eSBarry Smith   ierr = MatGetLocalSize(A,&m,0);CHKERRQ(ierr);
4434c5d6d63eSBarry Smith   ierr = MatGetSize(A,0,&N);CHKERRQ(ierr);
4435f204ca49SKris Buschelman   /* Should this be the type of the diagonal block of A? */
4436f69a0ea3SMatthew Knepley   ierr = MatCreate(PETSC_COMM_SELF,&B);CHKERRQ(ierr);
4437f69a0ea3SMatthew Knepley   ierr = MatSetSizes(B,m,N,m,N);CHKERRQ(ierr);
4438a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(B,A->rmap->bs,A->cmap->bs);CHKERRQ(ierr);
4439f204ca49SKris Buschelman   ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
4440f204ca49SKris Buschelman   ierr = MatSeqAIJSetPreallocation(B,0,PETSC_NULL);CHKERRQ(ierr);
4441c5d6d63eSBarry Smith   ierr = MatGetOwnershipRange(A,&rstart,0);CHKERRQ(ierr);
4442c5d6d63eSBarry Smith   for (i=0;i<m;i++) {
4443c5d6d63eSBarry Smith     ierr = MatGetRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr);
4444c5d6d63eSBarry Smith     ierr = MatSetValues(B,1,&i,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr);
4445c5d6d63eSBarry Smith     ierr = MatRestoreRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr);
4446c5d6d63eSBarry Smith   }
4447c5d6d63eSBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4448c5d6d63eSBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4449c5d6d63eSBarry Smith 
44507adad957SLisandro Dalcin   ierr = MPI_Comm_rank(((PetscObject)A)->comm,&rank);CHKERRQ(ierr);
4451c5d6d63eSBarry Smith   ierr = PetscStrlen(outfile,&len);CHKERRQ(ierr);
4452c5d6d63eSBarry Smith   ierr = PetscMalloc((len+5)*sizeof(char),&name);CHKERRQ(ierr);
4453c5d6d63eSBarry Smith   sprintf(name,"%s.%d",outfile,rank);
4454852598b0SBarry Smith   ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,name,FILE_MODE_APPEND,&out);CHKERRQ(ierr);
4455c5d6d63eSBarry Smith   ierr = PetscFree(name);
4456c5d6d63eSBarry Smith   ierr = MatView(B,out);CHKERRQ(ierr);
44576bf464f9SBarry Smith   ierr = PetscViewerDestroy(&out);CHKERRQ(ierr);
44586bf464f9SBarry Smith   ierr = MatDestroy(&B);CHKERRQ(ierr);
4459c5d6d63eSBarry Smith   PetscFunctionReturn(0);
4460c5d6d63eSBarry Smith }
4461e5f2cdd8SHong Zhang 
446209573ac7SBarry Smith extern PetscErrorCode MatDestroy_MPIAIJ(Mat);
446351a7d1a8SHong Zhang #undef __FUNCT__
446451a7d1a8SHong Zhang #define __FUNCT__ "MatDestroy_MPIAIJ_SeqsToMPI"
44657087cfbeSBarry Smith PetscErrorCode  MatDestroy_MPIAIJ_SeqsToMPI(Mat A)
446651a7d1a8SHong Zhang {
446751a7d1a8SHong Zhang   PetscErrorCode       ierr;
4468671beff6SHong Zhang   Mat_Merge_SeqsToMPI  *merge;
4469776b82aeSLisandro Dalcin   PetscContainer       container;
447051a7d1a8SHong Zhang 
447151a7d1a8SHong Zhang   PetscFunctionBegin;
4472671beff6SHong Zhang   ierr = PetscObjectQuery((PetscObject)A,"MatMergeSeqsToMPI",(PetscObject *)&container);CHKERRQ(ierr);
4473671beff6SHong Zhang   if (container) {
4474776b82aeSLisandro Dalcin     ierr = PetscContainerGetPointer(container,(void **)&merge);CHKERRQ(ierr);
447551a7d1a8SHong Zhang     ierr = PetscFree(merge->id_r);CHKERRQ(ierr);
44763e06a4e6SHong Zhang     ierr = PetscFree(merge->len_s);CHKERRQ(ierr);
44773e06a4e6SHong Zhang     ierr = PetscFree(merge->len_r);CHKERRQ(ierr);
447851a7d1a8SHong Zhang     ierr = PetscFree(merge->bi);CHKERRQ(ierr);
447951a7d1a8SHong Zhang     ierr = PetscFree(merge->bj);CHKERRQ(ierr);
4480533163c2SBarry Smith     ierr = PetscFree(merge->buf_ri[0]);CHKERRQ(ierr);
448102c68681SHong Zhang     ierr = PetscFree(merge->buf_ri);CHKERRQ(ierr);
4482533163c2SBarry Smith     ierr = PetscFree(merge->buf_rj[0]);CHKERRQ(ierr);
448302c68681SHong Zhang     ierr = PetscFree(merge->buf_rj);CHKERRQ(ierr);
448405b42c5fSBarry Smith     ierr = PetscFree(merge->coi);CHKERRQ(ierr);
448505b42c5fSBarry Smith     ierr = PetscFree(merge->coj);CHKERRQ(ierr);
448605b42c5fSBarry Smith     ierr = PetscFree(merge->owners_co);CHKERRQ(ierr);
44876bf464f9SBarry Smith     ierr = PetscLayoutDestroy(&merge->rowmap);CHKERRQ(ierr);
4488bf0cc555SLisandro Dalcin     ierr = PetscFree(merge);CHKERRQ(ierr);
4489671beff6SHong Zhang     ierr = PetscObjectCompose((PetscObject)A,"MatMergeSeqsToMPI",0);CHKERRQ(ierr);
4490671beff6SHong Zhang   }
449151a7d1a8SHong Zhang   ierr = MatDestroy_MPIAIJ(A);CHKERRQ(ierr);
449251a7d1a8SHong Zhang   PetscFunctionReturn(0);
449351a7d1a8SHong Zhang }
449451a7d1a8SHong Zhang 
4495c6db04a5SJed Brown #include <../src/mat/utils/freespace.h>
4496c6db04a5SJed Brown #include <petscbt.h>
44974ebed01fSBarry Smith 
4498e5f2cdd8SHong Zhang #undef __FUNCT__
449990431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJSumSeqAIJNumeric"
450090431a8fSHong Zhang PetscErrorCode  MatCreateMPIAIJSumSeqAIJNumeric(Mat seqmat,Mat mpimat)
450155d1abb9SHong Zhang {
450255d1abb9SHong Zhang   PetscErrorCode       ierr;
45037adad957SLisandro Dalcin   MPI_Comm             comm=((PetscObject)mpimat)->comm;
450455d1abb9SHong Zhang   Mat_SeqAIJ           *a=(Mat_SeqAIJ*)seqmat->data;
4505b1d57f15SBarry Smith   PetscMPIInt          size,rank,taga,*len_s;
4506d0f46423SBarry Smith   PetscInt             N=mpimat->cmap->N,i,j,*owners,*ai=a->i,*aj=a->j;
4507b1d57f15SBarry Smith   PetscInt             proc,m;
4508b1d57f15SBarry Smith   PetscInt             **buf_ri,**buf_rj;
4509b1d57f15SBarry Smith   PetscInt             k,anzi,*bj_i,*bi,*bj,arow,bnzi,nextaj;
4510b1d57f15SBarry Smith   PetscInt             nrows,**buf_ri_k,**nextrow,**nextai;
451155d1abb9SHong Zhang   MPI_Request          *s_waits,*r_waits;
451255d1abb9SHong Zhang   MPI_Status           *status;
4513a77337e4SBarry Smith   MatScalar            *aa=a->a;
4514dd6ea824SBarry Smith   MatScalar            **abuf_r,*ba_i;
451555d1abb9SHong Zhang   Mat_Merge_SeqsToMPI  *merge;
4516776b82aeSLisandro Dalcin   PetscContainer       container;
451755d1abb9SHong Zhang 
451855d1abb9SHong Zhang   PetscFunctionBegin;
45194ebed01fSBarry Smith   ierr = PetscLogEventBegin(MAT_Seqstompinum,seqmat,0,0,0);CHKERRQ(ierr);
45203c2c1871SHong Zhang 
452155d1abb9SHong Zhang   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
452255d1abb9SHong Zhang   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
452355d1abb9SHong Zhang 
452455d1abb9SHong Zhang   ierr = PetscObjectQuery((PetscObject)mpimat,"MatMergeSeqsToMPI",(PetscObject *)&container);CHKERRQ(ierr);
4525776b82aeSLisandro Dalcin   ierr  = PetscContainerGetPointer(container,(void **)&merge);CHKERRQ(ierr);
4526bf0cc555SLisandro Dalcin 
452755d1abb9SHong Zhang   bi     = merge->bi;
452855d1abb9SHong Zhang   bj     = merge->bj;
452955d1abb9SHong Zhang   buf_ri = merge->buf_ri;
453055d1abb9SHong Zhang   buf_rj = merge->buf_rj;
453155d1abb9SHong Zhang 
453255d1abb9SHong Zhang   ierr   = PetscMalloc(size*sizeof(MPI_Status),&status);CHKERRQ(ierr);
45337a2fc3feSBarry Smith   owners = merge->rowmap->range;
453455d1abb9SHong Zhang   len_s  = merge->len_s;
453555d1abb9SHong Zhang 
453655d1abb9SHong Zhang   /* send and recv matrix values */
453755d1abb9SHong Zhang   /*-----------------------------*/
4538357abbc8SBarry Smith   ierr = PetscObjectGetNewTag((PetscObject)mpimat,&taga);CHKERRQ(ierr);
453955d1abb9SHong Zhang   ierr = PetscPostIrecvScalar(comm,taga,merge->nrecv,merge->id_r,merge->len_r,&abuf_r,&r_waits);CHKERRQ(ierr);
454055d1abb9SHong Zhang 
454155d1abb9SHong Zhang   ierr = PetscMalloc((merge->nsend+1)*sizeof(MPI_Request),&s_waits);CHKERRQ(ierr);
454255d1abb9SHong Zhang   for (proc=0,k=0; proc<size; proc++){
454355d1abb9SHong Zhang     if (!len_s[proc]) continue;
454455d1abb9SHong Zhang     i = owners[proc];
454555d1abb9SHong Zhang     ierr = MPI_Isend(aa+ai[i],len_s[proc],MPIU_MATSCALAR,proc,taga,comm,s_waits+k);CHKERRQ(ierr);
454655d1abb9SHong Zhang     k++;
454755d1abb9SHong Zhang   }
454855d1abb9SHong Zhang 
45490c468ba9SBarry Smith   if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,r_waits,status);CHKERRQ(ierr);}
45500c468ba9SBarry Smith   if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,s_waits,status);CHKERRQ(ierr);}
455155d1abb9SHong Zhang   ierr = PetscFree(status);CHKERRQ(ierr);
455255d1abb9SHong Zhang 
455355d1abb9SHong Zhang   ierr = PetscFree(s_waits);CHKERRQ(ierr);
455455d1abb9SHong Zhang   ierr = PetscFree(r_waits);CHKERRQ(ierr);
455555d1abb9SHong Zhang 
455655d1abb9SHong Zhang   /* insert mat values of mpimat */
455755d1abb9SHong Zhang   /*----------------------------*/
4558a77337e4SBarry Smith   ierr = PetscMalloc(N*sizeof(PetscScalar),&ba_i);CHKERRQ(ierr);
45590572522cSBarry Smith   ierr = PetscMalloc3(merge->nrecv,PetscInt*,&buf_ri_k,merge->nrecv,PetscInt*,&nextrow,merge->nrecv,PetscInt*,&nextai);CHKERRQ(ierr);
456055d1abb9SHong Zhang 
456155d1abb9SHong Zhang   for (k=0; k<merge->nrecv; k++){
456255d1abb9SHong Zhang     buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
456355d1abb9SHong Zhang     nrows = *(buf_ri_k[k]);
456455d1abb9SHong Zhang     nextrow[k]  = buf_ri_k[k]+1;  /* next row number of k-th recved i-structure */
456555d1abb9SHong Zhang     nextai[k]   = buf_ri_k[k] + (nrows + 1);/* poins to the next i-structure of k-th recved i-structure  */
456655d1abb9SHong Zhang   }
456755d1abb9SHong Zhang 
456855d1abb9SHong Zhang   /* set values of ba */
45697a2fc3feSBarry Smith   m = merge->rowmap->n;
457055d1abb9SHong Zhang   for (i=0; i<m; i++) {
457155d1abb9SHong Zhang     arow = owners[rank] + i;
457255d1abb9SHong Zhang     bj_i = bj+bi[i];  /* col indices of the i-th row of mpimat */
457355d1abb9SHong Zhang     bnzi = bi[i+1] - bi[i];
4574a77337e4SBarry Smith     ierr = PetscMemzero(ba_i,bnzi*sizeof(PetscScalar));CHKERRQ(ierr);
457555d1abb9SHong Zhang 
457655d1abb9SHong Zhang     /* add local non-zero vals of this proc's seqmat into ba */
457755d1abb9SHong Zhang     anzi = ai[arow+1] - ai[arow];
457855d1abb9SHong Zhang     aj   = a->j + ai[arow];
457955d1abb9SHong Zhang     aa   = a->a + ai[arow];
458055d1abb9SHong Zhang     nextaj = 0;
458155d1abb9SHong Zhang     for (j=0; nextaj<anzi; j++){
458255d1abb9SHong Zhang       if (*(bj_i + j) == aj[nextaj]){ /* bcol == acol */
458355d1abb9SHong Zhang         ba_i[j] += aa[nextaj++];
458455d1abb9SHong Zhang       }
458555d1abb9SHong Zhang     }
458655d1abb9SHong Zhang 
458755d1abb9SHong Zhang     /* add received vals into ba */
458855d1abb9SHong Zhang     for (k=0; k<merge->nrecv; k++){ /* k-th received message */
458955d1abb9SHong Zhang       /* i-th row */
459055d1abb9SHong Zhang       if (i == *nextrow[k]) {
459155d1abb9SHong Zhang         anzi = *(nextai[k]+1) - *nextai[k];
459255d1abb9SHong Zhang         aj   = buf_rj[k] + *(nextai[k]);
459355d1abb9SHong Zhang         aa   = abuf_r[k] + *(nextai[k]);
459455d1abb9SHong Zhang         nextaj = 0;
459555d1abb9SHong Zhang         for (j=0; nextaj<anzi; j++){
459655d1abb9SHong Zhang           if (*(bj_i + j) == aj[nextaj]){ /* bcol == acol */
459755d1abb9SHong Zhang             ba_i[j] += aa[nextaj++];
459855d1abb9SHong Zhang           }
459955d1abb9SHong Zhang         }
460055d1abb9SHong Zhang         nextrow[k]++; nextai[k]++;
460155d1abb9SHong Zhang       }
460255d1abb9SHong Zhang     }
460355d1abb9SHong Zhang     ierr = MatSetValues(mpimat,1,&arow,bnzi,bj_i,ba_i,INSERT_VALUES);CHKERRQ(ierr);
460455d1abb9SHong Zhang   }
460555d1abb9SHong Zhang   ierr = MatAssemblyBegin(mpimat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
460655d1abb9SHong Zhang   ierr = MatAssemblyEnd(mpimat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
460755d1abb9SHong Zhang 
4608533163c2SBarry Smith   ierr = PetscFree(abuf_r[0]);CHKERRQ(ierr);
460955d1abb9SHong Zhang   ierr = PetscFree(abuf_r);CHKERRQ(ierr);
461055d1abb9SHong Zhang   ierr = PetscFree(ba_i);CHKERRQ(ierr);
46111d79065fSBarry Smith   ierr = PetscFree3(buf_ri_k,nextrow,nextai);CHKERRQ(ierr);
46124ebed01fSBarry Smith   ierr = PetscLogEventEnd(MAT_Seqstompinum,seqmat,0,0,0);CHKERRQ(ierr);
461355d1abb9SHong Zhang   PetscFunctionReturn(0);
461455d1abb9SHong Zhang }
461538f152feSBarry Smith 
46166bc0bbbfSBarry Smith extern PetscErrorCode  MatDestroy_MPIAIJ_SeqsToMPI(Mat);
46176bc0bbbfSBarry Smith 
461838f152feSBarry Smith #undef __FUNCT__
461990431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJSumSeqAIJSymbolic"
462090431a8fSHong Zhang PetscErrorCode  MatCreateMPIAIJSumSeqAIJSymbolic(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,Mat *mpimat)
4621e5f2cdd8SHong Zhang {
4622f08fae4eSHong Zhang   PetscErrorCode       ierr;
462355a3bba9SHong Zhang   Mat                  B_mpi;
4624c2234fe3SHong Zhang   Mat_SeqAIJ           *a=(Mat_SeqAIJ*)seqmat->data;
4625b1d57f15SBarry Smith   PetscMPIInt          size,rank,tagi,tagj,*len_s,*len_si,*len_ri;
4626b1d57f15SBarry Smith   PetscInt             **buf_rj,**buf_ri,**buf_ri_k;
4627d0f46423SBarry Smith   PetscInt             M=seqmat->rmap->n,N=seqmat->cmap->n,i,*owners,*ai=a->i,*aj=a->j;
4628a2f3521dSMark F. Adams   PetscInt             len,proc,*dnz,*onz,bs,cbs;
4629b1d57f15SBarry Smith   PetscInt             k,anzi,*bi,*bj,*lnk,nlnk,arow,bnzi,nspacedouble=0;
4630b1d57f15SBarry Smith   PetscInt             nrows,*buf_s,*buf_si,*buf_si_i,**nextrow,**nextai;
463155d1abb9SHong Zhang   MPI_Request          *si_waits,*sj_waits,*ri_waits,*rj_waits;
463258cb9c82SHong Zhang   MPI_Status           *status;
4633a1a86e44SBarry Smith   PetscFreeSpaceList   free_space=PETSC_NULL,current_space=PETSC_NULL;
4634be0fcf8dSHong Zhang   PetscBT              lnkbt;
463551a7d1a8SHong Zhang   Mat_Merge_SeqsToMPI  *merge;
4636776b82aeSLisandro Dalcin   PetscContainer       container;
463702c68681SHong Zhang 
4638e5f2cdd8SHong Zhang   PetscFunctionBegin;
46394ebed01fSBarry Smith   ierr = PetscLogEventBegin(MAT_Seqstompisym,seqmat,0,0,0);CHKERRQ(ierr);
46403c2c1871SHong Zhang 
464138f152feSBarry Smith   /* make sure it is a PETSc comm */
464238f152feSBarry Smith   ierr = PetscCommDuplicate(comm,&comm,PETSC_NULL);CHKERRQ(ierr);
4643e5f2cdd8SHong Zhang   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
4644e5f2cdd8SHong Zhang   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
464555d1abb9SHong Zhang 
464651a7d1a8SHong Zhang   ierr = PetscNew(Mat_Merge_SeqsToMPI,&merge);CHKERRQ(ierr);
4647c2234fe3SHong Zhang   ierr = PetscMalloc(size*sizeof(MPI_Status),&status);CHKERRQ(ierr);
4648e5f2cdd8SHong Zhang 
46496abd8857SHong Zhang   /* determine row ownership */
4650f08fae4eSHong Zhang   /*---------------------------------------------------------*/
465126283091SBarry Smith   ierr = PetscLayoutCreate(comm,&merge->rowmap);CHKERRQ(ierr);
465226283091SBarry Smith   ierr = PetscLayoutSetLocalSize(merge->rowmap,m);CHKERRQ(ierr);
465326283091SBarry Smith   ierr = PetscLayoutSetSize(merge->rowmap,M);CHKERRQ(ierr);
465426283091SBarry Smith   ierr = PetscLayoutSetBlockSize(merge->rowmap,1);CHKERRQ(ierr);
465526283091SBarry Smith   ierr = PetscLayoutSetUp(merge->rowmap);CHKERRQ(ierr);
4656b1d57f15SBarry Smith   ierr = PetscMalloc(size*sizeof(PetscMPIInt),&len_si);CHKERRQ(ierr);
4657b1d57f15SBarry Smith   ierr = PetscMalloc(size*sizeof(PetscMPIInt),&merge->len_s);CHKERRQ(ierr);
465855d1abb9SHong Zhang 
46597a2fc3feSBarry Smith   m      = merge->rowmap->n;
46607a2fc3feSBarry Smith   M      = merge->rowmap->N;
46617a2fc3feSBarry Smith   owners = merge->rowmap->range;
46626abd8857SHong Zhang 
46636abd8857SHong Zhang   /* determine the number of messages to send, their lengths */
46646abd8857SHong Zhang   /*---------------------------------------------------------*/
46653e06a4e6SHong Zhang   len_s  = merge->len_s;
466651a7d1a8SHong Zhang 
46672257cef7SHong Zhang   len = 0;  /* length of buf_si[] */
4668c2234fe3SHong Zhang   merge->nsend = 0;
4669409913e3SHong Zhang   for (proc=0; proc<size; proc++){
46702257cef7SHong Zhang     len_si[proc] = 0;
46713e06a4e6SHong Zhang     if (proc == rank){
46726abd8857SHong Zhang       len_s[proc] = 0;
46733e06a4e6SHong Zhang     } else {
467402c68681SHong Zhang       len_si[proc] = owners[proc+1] - owners[proc] + 1;
46753e06a4e6SHong Zhang       len_s[proc] = ai[owners[proc+1]] - ai[owners[proc]]; /* num of rows to be sent to [proc] */
46763e06a4e6SHong Zhang     }
46773e06a4e6SHong Zhang     if (len_s[proc]) {
4678c2234fe3SHong Zhang       merge->nsend++;
46792257cef7SHong Zhang       nrows = 0;
46802257cef7SHong Zhang       for (i=owners[proc]; i<owners[proc+1]; i++){
46812257cef7SHong Zhang         if (ai[i+1] > ai[i]) nrows++;
46822257cef7SHong Zhang       }
46832257cef7SHong Zhang       len_si[proc] = 2*(nrows+1);
46842257cef7SHong Zhang       len += len_si[proc];
4685409913e3SHong Zhang     }
468658cb9c82SHong Zhang   }
4687409913e3SHong Zhang 
46882257cef7SHong Zhang   /* determine the number and length of messages to receive for ij-structure */
46892257cef7SHong Zhang   /*-------------------------------------------------------------------------*/
469051a7d1a8SHong Zhang   ierr = PetscGatherNumberOfMessages(comm,PETSC_NULL,len_s,&merge->nrecv);CHKERRQ(ierr);
469155d1abb9SHong Zhang   ierr = PetscGatherMessageLengths2(comm,merge->nsend,merge->nrecv,len_s,len_si,&merge->id_r,&merge->len_r,&len_ri);CHKERRQ(ierr);
4692671beff6SHong Zhang 
46933e06a4e6SHong Zhang   /* post the Irecv of j-structure */
46943e06a4e6SHong Zhang   /*-------------------------------*/
46952c72b5baSSatish Balay   ierr = PetscCommGetNewTag(comm,&tagj);CHKERRQ(ierr);
46963e06a4e6SHong Zhang   ierr = PetscPostIrecvInt(comm,tagj,merge->nrecv,merge->id_r,merge->len_r,&buf_rj,&rj_waits);CHKERRQ(ierr);
469702c68681SHong Zhang 
46983e06a4e6SHong Zhang   /* post the Isend of j-structure */
4699affca5deSHong Zhang   /*--------------------------------*/
47001d79065fSBarry Smith   ierr = PetscMalloc2(merge->nsend,MPI_Request,&si_waits,merge->nsend,MPI_Request,&sj_waits);CHKERRQ(ierr);
47013e06a4e6SHong Zhang 
47022257cef7SHong Zhang   for (proc=0, k=0; proc<size; proc++){
4703409913e3SHong Zhang     if (!len_s[proc]) continue;
470402c68681SHong Zhang     i = owners[proc];
4705b1d57f15SBarry Smith     ierr = MPI_Isend(aj+ai[i],len_s[proc],MPIU_INT,proc,tagj,comm,sj_waits+k);CHKERRQ(ierr);
470651a7d1a8SHong Zhang     k++;
470751a7d1a8SHong Zhang   }
470851a7d1a8SHong Zhang 
47093e06a4e6SHong Zhang   /* receives and sends of j-structure are complete */
47103e06a4e6SHong Zhang   /*------------------------------------------------*/
47110c468ba9SBarry Smith   if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,rj_waits,status);CHKERRQ(ierr);}
47120c468ba9SBarry Smith   if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,sj_waits,status);CHKERRQ(ierr);}
471302c68681SHong Zhang 
471402c68681SHong Zhang   /* send and recv i-structure */
471502c68681SHong Zhang   /*---------------------------*/
47162c72b5baSSatish Balay   ierr = PetscCommGetNewTag(comm,&tagi);CHKERRQ(ierr);
471702c68681SHong Zhang   ierr = PetscPostIrecvInt(comm,tagi,merge->nrecv,merge->id_r,len_ri,&buf_ri,&ri_waits);CHKERRQ(ierr);
471802c68681SHong Zhang 
4719b1d57f15SBarry Smith   ierr = PetscMalloc((len+1)*sizeof(PetscInt),&buf_s);CHKERRQ(ierr);
47203e06a4e6SHong Zhang   buf_si = buf_s;  /* points to the beginning of k-th msg to be sent */
47212257cef7SHong Zhang   for (proc=0,k=0; proc<size; proc++){
472202c68681SHong Zhang     if (!len_s[proc]) continue;
47233e06a4e6SHong Zhang     /* form outgoing message for i-structure:
47243e06a4e6SHong Zhang          buf_si[0]:                 nrows to be sent
47253e06a4e6SHong Zhang                [1:nrows]:           row index (global)
47263e06a4e6SHong Zhang                [nrows+1:2*nrows+1]: i-structure index
47273e06a4e6SHong Zhang     */
47283e06a4e6SHong Zhang     /*-------------------------------------------*/
47292257cef7SHong Zhang     nrows = len_si[proc]/2 - 1;
47303e06a4e6SHong Zhang     buf_si_i    = buf_si + nrows+1;
47313e06a4e6SHong Zhang     buf_si[0]   = nrows;
47323e06a4e6SHong Zhang     buf_si_i[0] = 0;
47333e06a4e6SHong Zhang     nrows = 0;
47343e06a4e6SHong Zhang     for (i=owners[proc]; i<owners[proc+1]; i++){
47353e06a4e6SHong Zhang       anzi = ai[i+1] - ai[i];
47363e06a4e6SHong Zhang       if (anzi) {
47373e06a4e6SHong Zhang         buf_si_i[nrows+1] = buf_si_i[nrows] + anzi; /* i-structure */
47383e06a4e6SHong Zhang         buf_si[nrows+1] = i-owners[proc]; /* local row index */
47393e06a4e6SHong Zhang         nrows++;
47403e06a4e6SHong Zhang       }
47413e06a4e6SHong Zhang     }
4742b1d57f15SBarry Smith     ierr = MPI_Isend(buf_si,len_si[proc],MPIU_INT,proc,tagi,comm,si_waits+k);CHKERRQ(ierr);
474302c68681SHong Zhang     k++;
47442257cef7SHong Zhang     buf_si += len_si[proc];
474502c68681SHong Zhang   }
47462257cef7SHong Zhang 
47470c468ba9SBarry Smith   if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,ri_waits,status);CHKERRQ(ierr);}
47480c468ba9SBarry Smith   if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,si_waits,status);CHKERRQ(ierr);}
474902c68681SHong Zhang 
4750ae15b995SBarry Smith   ierr = PetscInfo2(seqmat,"nsend: %D, nrecv: %D\n",merge->nsend,merge->nrecv);CHKERRQ(ierr);
47513e06a4e6SHong Zhang   for (i=0; i<merge->nrecv; i++){
4752ae15b995SBarry Smith     ierr = PetscInfo3(seqmat,"recv len_ri=%D, len_rj=%D from [%D]\n",len_ri[i],merge->len_r[i],merge->id_r[i]);CHKERRQ(ierr);
47533e06a4e6SHong Zhang   }
47543e06a4e6SHong Zhang 
47553e06a4e6SHong Zhang   ierr = PetscFree(len_si);CHKERRQ(ierr);
475602c68681SHong Zhang   ierr = PetscFree(len_ri);CHKERRQ(ierr);
475702c68681SHong Zhang   ierr = PetscFree(rj_waits);CHKERRQ(ierr);
47581d79065fSBarry Smith   ierr = PetscFree2(si_waits,sj_waits);CHKERRQ(ierr);
47592257cef7SHong Zhang   ierr = PetscFree(ri_waits);CHKERRQ(ierr);
47603e06a4e6SHong Zhang   ierr = PetscFree(buf_s);CHKERRQ(ierr);
4761bcc1bcd5SHong Zhang   ierr = PetscFree(status);CHKERRQ(ierr);
476258cb9c82SHong Zhang 
4763bcc1bcd5SHong Zhang   /* compute a local seq matrix in each processor */
4764bcc1bcd5SHong Zhang   /*----------------------------------------------*/
476558cb9c82SHong Zhang   /* allocate bi array and free space for accumulating nonzero column info */
4766b1d57f15SBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&bi);CHKERRQ(ierr);
476758cb9c82SHong Zhang   bi[0] = 0;
476858cb9c82SHong Zhang 
4769be0fcf8dSHong Zhang   /* create and initialize a linked list */
4770be0fcf8dSHong Zhang   nlnk = N+1;
4771be0fcf8dSHong Zhang   ierr = PetscLLCreate(N,N,nlnk,lnk,lnkbt);CHKERRQ(ierr);
477258cb9c82SHong Zhang 
4773bcc1bcd5SHong Zhang   /* initial FreeSpace size is 2*(num of local nnz(seqmat)) */
477458cb9c82SHong Zhang   len = 0;
4775bcc1bcd5SHong Zhang   len  = ai[owners[rank+1]] - ai[owners[rank]];
4776a1a86e44SBarry Smith   ierr = PetscFreeSpaceGet((PetscInt)(2*len+1),&free_space);CHKERRQ(ierr);
477758cb9c82SHong Zhang   current_space = free_space;
477858cb9c82SHong Zhang 
4779bcc1bcd5SHong Zhang   /* determine symbolic info for each local row */
47800572522cSBarry Smith   ierr = PetscMalloc3(merge->nrecv,PetscInt*,&buf_ri_k,merge->nrecv,PetscInt*,&nextrow,merge->nrecv,PetscInt*,&nextai);CHKERRQ(ierr);
47811d79065fSBarry Smith 
47823e06a4e6SHong Zhang   for (k=0; k<merge->nrecv; k++){
47832257cef7SHong Zhang     buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
47843e06a4e6SHong Zhang     nrows = *buf_ri_k[k];
47853e06a4e6SHong Zhang     nextrow[k]  = buf_ri_k[k] + 1;  /* next row number of k-th recved i-structure */
47862257cef7SHong Zhang     nextai[k]   = buf_ri_k[k] + (nrows + 1);/* poins to the next i-structure of k-th recved i-structure  */
47873e06a4e6SHong Zhang   }
47882257cef7SHong Zhang 
4789bcc1bcd5SHong Zhang   ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr);
4790bcc1bcd5SHong Zhang   len = 0;
479158cb9c82SHong Zhang   for (i=0;i<m;i++) {
479258cb9c82SHong Zhang     bnzi   = 0;
479358cb9c82SHong Zhang     /* add local non-zero cols of this proc's seqmat into lnk */
479458cb9c82SHong Zhang     arow   = owners[rank] + i;
479558cb9c82SHong Zhang     anzi   = ai[arow+1] - ai[arow];
479658cb9c82SHong Zhang     aj     = a->j + ai[arow];
4797dadf0e6bSHong Zhang     ierr = PetscLLAddSorted(anzi,aj,N,nlnk,lnk,lnkbt);CHKERRQ(ierr);
479858cb9c82SHong Zhang     bnzi += nlnk;
479958cb9c82SHong Zhang     /* add received col data into lnk */
480051a7d1a8SHong Zhang     for (k=0; k<merge->nrecv; k++){ /* k-th received message */
480155d1abb9SHong Zhang       if (i == *nextrow[k]) { /* i-th row */
48023e06a4e6SHong Zhang         anzi = *(nextai[k]+1) - *nextai[k];
48033e06a4e6SHong Zhang         aj   = buf_rj[k] + *nextai[k];
4804dadf0e6bSHong Zhang         ierr = PetscLLAddSorted(anzi,aj,N,nlnk,lnk,lnkbt);CHKERRQ(ierr);
48053e06a4e6SHong Zhang         bnzi += nlnk;
48063e06a4e6SHong Zhang         nextrow[k]++; nextai[k]++;
48073e06a4e6SHong Zhang       }
480858cb9c82SHong Zhang     }
4809bcc1bcd5SHong Zhang     if (len < bnzi) len = bnzi;  /* =max(bnzi) */
481058cb9c82SHong Zhang 
481158cb9c82SHong Zhang     /* if free space is not available, make more free space */
481258cb9c82SHong Zhang     if (current_space->local_remaining<bnzi) {
48134238b7adSHong Zhang       ierr = PetscFreeSpaceGet(bnzi+current_space->total_array_size,&current_space);CHKERRQ(ierr);
481458cb9c82SHong Zhang       nspacedouble++;
481558cb9c82SHong Zhang     }
481658cb9c82SHong Zhang     /* copy data into free space, then initialize lnk */
4817be0fcf8dSHong Zhang     ierr = PetscLLClean(N,N,bnzi,lnk,current_space->array,lnkbt);CHKERRQ(ierr);
4818bcc1bcd5SHong Zhang     ierr = MatPreallocateSet(i+owners[rank],bnzi,current_space->array,dnz,onz);CHKERRQ(ierr);
4819bcc1bcd5SHong Zhang 
482058cb9c82SHong Zhang     current_space->array           += bnzi;
482158cb9c82SHong Zhang     current_space->local_used      += bnzi;
482258cb9c82SHong Zhang     current_space->local_remaining -= bnzi;
482358cb9c82SHong Zhang 
482458cb9c82SHong Zhang     bi[i+1] = bi[i] + bnzi;
482558cb9c82SHong Zhang   }
4826bcc1bcd5SHong Zhang 
48271d79065fSBarry Smith   ierr = PetscFree3(buf_ri_k,nextrow,nextai);CHKERRQ(ierr);
4828bcc1bcd5SHong Zhang 
4829b1d57f15SBarry Smith   ierr = PetscMalloc((bi[m]+1)*sizeof(PetscInt),&bj);CHKERRQ(ierr);
4830a1a86e44SBarry Smith   ierr = PetscFreeSpaceContiguous(&free_space,bj);CHKERRQ(ierr);
4831be0fcf8dSHong Zhang   ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr);
4832409913e3SHong Zhang 
4833bcc1bcd5SHong Zhang   /* create symbolic parallel matrix B_mpi */
4834bcc1bcd5SHong Zhang   /*---------------------------------------*/
4835a2f3521dSMark F. Adams     ierr = MatGetBlockSizes(seqmat,&bs,&cbs);CHKERRQ(ierr);
4836f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,&B_mpi);CHKERRQ(ierr);
483754b84b50SHong Zhang   if (n==PETSC_DECIDE) {
4838f69a0ea3SMatthew Knepley     ierr = MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,N);CHKERRQ(ierr);
483954b84b50SHong Zhang   } else {
4840f69a0ea3SMatthew Knepley     ierr = MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
484154b84b50SHong Zhang   }
4842a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(B_mpi,bs,cbs); CHKERRQ(ierr);
4843bcc1bcd5SHong Zhang   ierr = MatSetType(B_mpi,MATMPIAIJ);CHKERRQ(ierr);
4844bcc1bcd5SHong Zhang   ierr = MatMPIAIJSetPreallocation(B_mpi,0,dnz,0,onz);CHKERRQ(ierr);
4845bcc1bcd5SHong Zhang   ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr);
48467e63b356SHong Zhang   ierr = MatSetOption(B_mpi,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr);
484758cb9c82SHong Zhang 
484890431a8fSHong Zhang   /* B_mpi is not ready for use - assembly will be done by MatCreateMPIAIJSumSeqAIJNumeric() */
48496abd8857SHong Zhang   B_mpi->assembled     = PETSC_FALSE;
4850affca5deSHong Zhang   B_mpi->ops->destroy  = MatDestroy_MPIAIJ_SeqsToMPI;
4851affca5deSHong Zhang   merge->bi            = bi;
4852affca5deSHong Zhang   merge->bj            = bj;
485302c68681SHong Zhang   merge->buf_ri        = buf_ri;
485402c68681SHong Zhang   merge->buf_rj        = buf_rj;
4855de0260b3SHong Zhang   merge->coi           = PETSC_NULL;
4856de0260b3SHong Zhang   merge->coj           = PETSC_NULL;
4857de0260b3SHong Zhang   merge->owners_co     = PETSC_NULL;
4858affca5deSHong Zhang 
4859bf0cc555SLisandro Dalcin   ierr = PetscCommDestroy(&comm);CHKERRQ(ierr);
4860bf0cc555SLisandro Dalcin 
4861affca5deSHong Zhang   /* attach the supporting struct to B_mpi for reuse */
4862776b82aeSLisandro Dalcin   ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr);
4863776b82aeSLisandro Dalcin   ierr = PetscContainerSetPointer(container,merge);CHKERRQ(ierr);
4864affca5deSHong Zhang   ierr = PetscObjectCompose((PetscObject)B_mpi,"MatMergeSeqsToMPI",(PetscObject)container);CHKERRQ(ierr);
4865bf0cc555SLisandro Dalcin   ierr = PetscContainerDestroy(&container);CHKERRQ(ierr);
4866affca5deSHong Zhang   *mpimat = B_mpi;
486738f152feSBarry Smith 
48684ebed01fSBarry Smith   ierr = PetscLogEventEnd(MAT_Seqstompisym,seqmat,0,0,0);CHKERRQ(ierr);
4869e5f2cdd8SHong Zhang   PetscFunctionReturn(0);
4870e5f2cdd8SHong Zhang }
487125616d81SHong Zhang 
487238f152feSBarry Smith #undef __FUNCT__
487390431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJSumSeqAIJ"
4874d4036a1aSHong Zhang /*@C
487590431a8fSHong Zhang       MatCreateMPIAIJSumSeqAIJ - Creates a MPIAIJ matrix by adding sequential
4876d4036a1aSHong Zhang                  matrices from each processor
4877d4036a1aSHong Zhang 
4878d4036a1aSHong Zhang     Collective on MPI_Comm
4879d4036a1aSHong Zhang 
4880d4036a1aSHong Zhang    Input Parameters:
4881d4036a1aSHong Zhang +    comm - the communicators the parallel matrix will live on
4882d4036a1aSHong Zhang .    seqmat - the input sequential matrices
4883d4036a1aSHong Zhang .    m - number of local rows (or PETSC_DECIDE)
4884d4036a1aSHong Zhang .    n - number of local columns (or PETSC_DECIDE)
4885d4036a1aSHong Zhang -    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
4886d4036a1aSHong Zhang 
4887d4036a1aSHong Zhang    Output Parameter:
4888d4036a1aSHong Zhang .    mpimat - the parallel matrix generated
4889d4036a1aSHong Zhang 
4890d4036a1aSHong Zhang     Level: advanced
4891d4036a1aSHong Zhang 
4892d4036a1aSHong Zhang    Notes:
4893d4036a1aSHong Zhang      The dimensions of the sequential matrix in each processor MUST be the same.
4894d4036a1aSHong Zhang      The input seqmat is included into the container "Mat_Merge_SeqsToMPI", and will be
4895d4036a1aSHong Zhang      destroyed when mpimat is destroyed. Call PetscObjectQuery() to access seqmat.
4896d4036a1aSHong Zhang @*/
489790431a8fSHong Zhang PetscErrorCode  MatCreateMPIAIJSumSeqAIJ(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,MatReuse scall,Mat *mpimat)
489855d1abb9SHong Zhang {
489955d1abb9SHong Zhang   PetscErrorCode   ierr;
49007e63b356SHong Zhang   PetscMPIInt     size;
490155d1abb9SHong Zhang 
490255d1abb9SHong Zhang   PetscFunctionBegin;
49037e63b356SHong Zhang   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
49047e63b356SHong Zhang   if (size == 1){
49057e63b356SHong Zhang      ierr = PetscLogEventBegin(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr);
49067e63b356SHong Zhang      if (scall == MAT_INITIAL_MATRIX){
49077e63b356SHong Zhang        ierr = MatDuplicate(seqmat,MAT_COPY_VALUES,mpimat);CHKERRQ(ierr);
49087e63b356SHong Zhang      } else {
49097e63b356SHong Zhang        ierr = MatCopy(seqmat,*mpimat,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
49107e63b356SHong Zhang      }
49117e63b356SHong Zhang      ierr = PetscLogEventEnd(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr);
49127e63b356SHong Zhang      PetscFunctionReturn(0);
49137e63b356SHong Zhang   }
49144ebed01fSBarry Smith   ierr = PetscLogEventBegin(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr);
491555d1abb9SHong Zhang   if (scall == MAT_INITIAL_MATRIX){
491690431a8fSHong Zhang     ierr = MatCreateMPIAIJSumSeqAIJSymbolic(comm,seqmat,m,n,mpimat);CHKERRQ(ierr);
491755d1abb9SHong Zhang   }
491890431a8fSHong Zhang   ierr = MatCreateMPIAIJSumSeqAIJNumeric(seqmat,*mpimat);CHKERRQ(ierr);
49194ebed01fSBarry Smith   ierr = PetscLogEventEnd(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr);
492055d1abb9SHong Zhang   PetscFunctionReturn(0);
492155d1abb9SHong Zhang }
49224ebed01fSBarry Smith 
492325616d81SHong Zhang #undef __FUNCT__
49244a2b5492SBarry Smith #define __FUNCT__ "MatMPIAIJGetLocalMat"
4925bc08b0f1SBarry Smith /*@
49264a2b5492SBarry Smith      MatMPIAIJGetLocalMat - Creates a SeqAIJ from a MPIAIJ matrix by taking all its local rows and putting them into a sequential vector with
49278661ff28SBarry Smith           mlocal rows and n columns. Where mlocal is the row count obtained with MatGetLocalSize() and n is the global column count obtained
49288661ff28SBarry Smith           with MatGetSize()
492925616d81SHong Zhang 
493032fba14fSHong Zhang     Not Collective
493125616d81SHong Zhang 
493225616d81SHong Zhang    Input Parameters:
493325616d81SHong Zhang +    A - the matrix
493425616d81SHong Zhang .    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
493525616d81SHong Zhang 
493625616d81SHong Zhang    Output Parameter:
493725616d81SHong Zhang .    A_loc - the local sequential matrix generated
493825616d81SHong Zhang 
493925616d81SHong Zhang     Level: developer
494025616d81SHong Zhang 
4941ba264940SBarry Smith .seealso: MatGetOwnerShipRange(), MatMPIAIJGetLocalMatCondensed()
49428661ff28SBarry Smith 
494325616d81SHong Zhang @*/
49444a2b5492SBarry Smith PetscErrorCode  MatMPIAIJGetLocalMat(Mat A,MatReuse scall,Mat *A_loc)
494525616d81SHong Zhang {
494625616d81SHong Zhang   PetscErrorCode  ierr;
494701b7ae99SHong Zhang   Mat_MPIAIJ      *mpimat=(Mat_MPIAIJ*)A->data;
494801b7ae99SHong Zhang   Mat_SeqAIJ      *mat,*a=(Mat_SeqAIJ*)(mpimat->A)->data,*b=(Mat_SeqAIJ*)(mpimat->B)->data;
494901b7ae99SHong Zhang   PetscInt        *ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j,*cmap=mpimat->garray;
4950a77337e4SBarry Smith   MatScalar       *aa=a->a,*ba=b->a,*cam;
4951a77337e4SBarry Smith   PetscScalar     *ca;
4952d0f46423SBarry Smith   PetscInt        am=A->rmap->n,i,j,k,cstart=A->cmap->rstart;
49535a7d977cSHong Zhang   PetscInt        *ci,*cj,col,ncols_d,ncols_o,jo;
49548661ff28SBarry Smith   PetscBool       match;
495525616d81SHong Zhang 
495625616d81SHong Zhang   PetscFunctionBegin;
4957251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&match);CHKERRQ(ierr);
49588661ff28SBarry Smith   if (!match) SETERRQ(((PetscObject)A)->comm, PETSC_ERR_SUP,"Requires MPIAIJ matrix as input");
49594ebed01fSBarry Smith   ierr = PetscLogEventBegin(MAT_Getlocalmat,A,0,0,0);CHKERRQ(ierr);
496001b7ae99SHong Zhang   if (scall == MAT_INITIAL_MATRIX){
4961dea91ad1SHong Zhang     ierr = PetscMalloc((1+am)*sizeof(PetscInt),&ci);CHKERRQ(ierr);
4962dea91ad1SHong Zhang     ci[0] = 0;
496301b7ae99SHong Zhang     for (i=0; i<am; i++){
4964dea91ad1SHong Zhang       ci[i+1] = ci[i] + (ai[i+1] - ai[i]) + (bi[i+1] - bi[i]);
496501b7ae99SHong Zhang     }
4966dea91ad1SHong Zhang     ierr = PetscMalloc((1+ci[am])*sizeof(PetscInt),&cj);CHKERRQ(ierr);
4967dea91ad1SHong Zhang     ierr = PetscMalloc((1+ci[am])*sizeof(PetscScalar),&ca);CHKERRQ(ierr);
4968dea91ad1SHong Zhang     k = 0;
496901b7ae99SHong Zhang     for (i=0; i<am; i++) {
49705a7d977cSHong Zhang       ncols_o = bi[i+1] - bi[i];
49715a7d977cSHong Zhang       ncols_d = ai[i+1] - ai[i];
497201b7ae99SHong Zhang       /* off-diagonal portion of A */
49735a7d977cSHong Zhang       for (jo=0; jo<ncols_o; jo++) {
49745a7d977cSHong Zhang         col = cmap[*bj];
49755a7d977cSHong Zhang         if (col >= cstart) break;
49765a7d977cSHong Zhang         cj[k]   = col; bj++;
49775a7d977cSHong Zhang         ca[k++] = *ba++;
49785a7d977cSHong Zhang       }
49795a7d977cSHong Zhang       /* diagonal portion of A */
49805a7d977cSHong Zhang       for (j=0; j<ncols_d; j++) {
49815a7d977cSHong Zhang         cj[k]   = cstart + *aj++;
49825a7d977cSHong Zhang         ca[k++] = *aa++;
49835a7d977cSHong Zhang       }
49845a7d977cSHong Zhang       /* off-diagonal portion of A */
49855a7d977cSHong Zhang       for (j=jo; j<ncols_o; j++) {
49865a7d977cSHong Zhang         cj[k]   = cmap[*bj++];
49875a7d977cSHong Zhang         ca[k++] = *ba++;
49885a7d977cSHong Zhang       }
498925616d81SHong Zhang     }
4990dea91ad1SHong Zhang     /* put together the new matrix */
4991d0f46423SBarry Smith     ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,am,A->cmap->N,ci,cj,ca,A_loc);CHKERRQ(ierr);
4992dea91ad1SHong Zhang     /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
4993dea91ad1SHong Zhang     /* Since these are PETSc arrays, change flags to free them as necessary. */
4994dea91ad1SHong Zhang     mat          = (Mat_SeqAIJ*)(*A_loc)->data;
4995e6b907acSBarry Smith     mat->free_a  = PETSC_TRUE;
4996e6b907acSBarry Smith     mat->free_ij = PETSC_TRUE;
4997dea91ad1SHong Zhang     mat->nonew   = 0;
49985a7d977cSHong Zhang   } else if (scall == MAT_REUSE_MATRIX){
49995a7d977cSHong Zhang     mat=(Mat_SeqAIJ*)(*A_loc)->data;
5000a77337e4SBarry Smith     ci = mat->i; cj = mat->j; cam = mat->a;
50015a7d977cSHong Zhang     for (i=0; i<am; i++) {
50025a7d977cSHong Zhang       /* off-diagonal portion of A */
50035a7d977cSHong Zhang       ncols_o = bi[i+1] - bi[i];
50045a7d977cSHong Zhang       for (jo=0; jo<ncols_o; jo++) {
50055a7d977cSHong Zhang         col = cmap[*bj];
50065a7d977cSHong Zhang         if (col >= cstart) break;
5007a77337e4SBarry Smith         *cam++ = *ba++; bj++;
50085a7d977cSHong Zhang       }
50095a7d977cSHong Zhang       /* diagonal portion of A */
5010ecc9b87dSHong Zhang       ncols_d = ai[i+1] - ai[i];
5011a77337e4SBarry Smith       for (j=0; j<ncols_d; j++) *cam++ = *aa++;
50125a7d977cSHong Zhang       /* off-diagonal portion of A */
5013f33d1a9aSHong Zhang       for (j=jo; j<ncols_o; j++) {
5014a77337e4SBarry Smith         *cam++ = *ba++; bj++;
5015f33d1a9aSHong Zhang       }
50165a7d977cSHong Zhang     }
50178661ff28SBarry Smith   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall);
50184ebed01fSBarry Smith   ierr = PetscLogEventEnd(MAT_Getlocalmat,A,0,0,0);CHKERRQ(ierr);
501925616d81SHong Zhang   PetscFunctionReturn(0);
502025616d81SHong Zhang }
502125616d81SHong Zhang 
502232fba14fSHong Zhang #undef __FUNCT__
50234a2b5492SBarry Smith #define __FUNCT__ "MatMPIAIJGetLocalMatCondensed"
502432fba14fSHong Zhang /*@C
5025ba264940SBarry Smith      MatMPIAIJGetLocalMatCondensed - Creates a SeqAIJ matrix from an MPIAIJ matrix by taking all its local rows and NON-ZERO columns
502632fba14fSHong Zhang 
502732fba14fSHong Zhang     Not Collective
502832fba14fSHong Zhang 
502932fba14fSHong Zhang    Input Parameters:
503032fba14fSHong Zhang +    A - the matrix
503132fba14fSHong Zhang .    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
503232fba14fSHong Zhang -    row, col - index sets of rows and columns to extract (or PETSC_NULL)
503332fba14fSHong Zhang 
503432fba14fSHong Zhang    Output Parameter:
503532fba14fSHong Zhang .    A_loc - the local sequential matrix generated
503632fba14fSHong Zhang 
503732fba14fSHong Zhang     Level: developer
503832fba14fSHong Zhang 
5039ba264940SBarry Smith .seealso: MatGetOwnershipRange(), MatMPIAIJGetLocalMat()
5040ba264940SBarry Smith 
504132fba14fSHong Zhang @*/
50424a2b5492SBarry Smith PetscErrorCode  MatMPIAIJGetLocalMatCondensed(Mat A,MatReuse scall,IS *row,IS *col,Mat *A_loc)
504332fba14fSHong Zhang {
504432fba14fSHong Zhang   Mat_MPIAIJ        *a=(Mat_MPIAIJ*)A->data;
504532fba14fSHong Zhang   PetscErrorCode    ierr;
504632fba14fSHong Zhang   PetscInt          i,start,end,ncols,nzA,nzB,*cmap,imark,*idx;
504732fba14fSHong Zhang   IS                isrowa,iscola;
504832fba14fSHong Zhang   Mat               *aloc;
50494a2b5492SBarry Smith   PetscBool       match;
505032fba14fSHong Zhang 
505132fba14fSHong Zhang   PetscFunctionBegin;
5052251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&match);CHKERRQ(ierr);
50534a2b5492SBarry Smith   if (!match) SETERRQ(((PetscObject)A)->comm, PETSC_ERR_SUP,"Requires MPIAIJ matrix as input");
50544ebed01fSBarry Smith   ierr = PetscLogEventBegin(MAT_Getlocalmatcondensed,A,0,0,0);CHKERRQ(ierr);
505532fba14fSHong Zhang   if (!row){
5056d0f46423SBarry Smith     start = A->rmap->rstart; end = A->rmap->rend;
505732fba14fSHong Zhang     ierr = ISCreateStride(PETSC_COMM_SELF,end-start,start,1,&isrowa);CHKERRQ(ierr);
505832fba14fSHong Zhang   } else {
505932fba14fSHong Zhang     isrowa = *row;
506032fba14fSHong Zhang   }
506132fba14fSHong Zhang   if (!col){
5062d0f46423SBarry Smith     start = A->cmap->rstart;
506332fba14fSHong Zhang     cmap  = a->garray;
5064d0f46423SBarry Smith     nzA   = a->A->cmap->n;
5065d0f46423SBarry Smith     nzB   = a->B->cmap->n;
506632fba14fSHong Zhang     ierr  = PetscMalloc((nzA+nzB)*sizeof(PetscInt), &idx);CHKERRQ(ierr);
506732fba14fSHong Zhang     ncols = 0;
506832fba14fSHong Zhang     for (i=0; i<nzB; i++) {
506932fba14fSHong Zhang       if (cmap[i] < start) idx[ncols++] = cmap[i];
507032fba14fSHong Zhang       else break;
507132fba14fSHong Zhang     }
507232fba14fSHong Zhang     imark = i;
507332fba14fSHong Zhang     for (i=0; i<nzA; i++) idx[ncols++] = start + i;
507432fba14fSHong Zhang     for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i];
5075d67e408aSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,&iscola);CHKERRQ(ierr);
507632fba14fSHong Zhang   } else {
507732fba14fSHong Zhang     iscola = *col;
507832fba14fSHong Zhang   }
507932fba14fSHong Zhang   if (scall != MAT_INITIAL_MATRIX){
508032fba14fSHong Zhang     ierr = PetscMalloc(sizeof(Mat),&aloc);CHKERRQ(ierr);
508132fba14fSHong Zhang     aloc[0] = *A_loc;
508232fba14fSHong Zhang   }
508332fba14fSHong Zhang   ierr = MatGetSubMatrices(A,1,&isrowa,&iscola,scall,&aloc);CHKERRQ(ierr);
508432fba14fSHong Zhang   *A_loc = aloc[0];
508532fba14fSHong Zhang   ierr = PetscFree(aloc);CHKERRQ(ierr);
508632fba14fSHong Zhang   if (!row){
50876bf464f9SBarry Smith     ierr = ISDestroy(&isrowa);CHKERRQ(ierr);
508832fba14fSHong Zhang   }
508932fba14fSHong Zhang   if (!col){
50906bf464f9SBarry Smith     ierr = ISDestroy(&iscola);CHKERRQ(ierr);
509132fba14fSHong Zhang   }
50924ebed01fSBarry Smith   ierr = PetscLogEventEnd(MAT_Getlocalmatcondensed,A,0,0,0);CHKERRQ(ierr);
509332fba14fSHong Zhang   PetscFunctionReturn(0);
509432fba14fSHong Zhang }
509532fba14fSHong Zhang 
509625616d81SHong Zhang #undef __FUNCT__
509725616d81SHong Zhang #define __FUNCT__ "MatGetBrowsOfAcols"
509825616d81SHong Zhang /*@C
509932fba14fSHong Zhang     MatGetBrowsOfAcols - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns of local A
510025616d81SHong Zhang 
510125616d81SHong Zhang     Collective on Mat
510225616d81SHong Zhang 
510325616d81SHong Zhang    Input Parameters:
5104e240928fSHong Zhang +    A,B - the matrices in mpiaij format
510525616d81SHong Zhang .    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
510625616d81SHong Zhang -    rowb, colb - index sets of rows and columns of B to extract (or PETSC_NULL)
510725616d81SHong Zhang 
510825616d81SHong Zhang    Output Parameter:
510925616d81SHong Zhang +    rowb, colb - index sets of rows and columns of B to extract
511025616d81SHong Zhang -    B_seq - the sequential matrix generated
511125616d81SHong Zhang 
511225616d81SHong Zhang     Level: developer
511325616d81SHong Zhang 
511425616d81SHong Zhang @*/
511566bfb163SHong Zhang PetscErrorCode  MatGetBrowsOfAcols(Mat A,Mat B,MatReuse scall,IS *rowb,IS *colb,Mat *B_seq)
511625616d81SHong Zhang {
5117899cda47SBarry Smith   Mat_MPIAIJ        *a=(Mat_MPIAIJ*)A->data;
511825616d81SHong Zhang   PetscErrorCode    ierr;
5119b1d57f15SBarry Smith   PetscInt          *idx,i,start,ncols,nzA,nzB,*cmap,imark;
512025616d81SHong Zhang   IS                isrowb,iscolb;
512166bfb163SHong Zhang   Mat               *bseq=PETSC_NULL;
512225616d81SHong Zhang 
512325616d81SHong Zhang   PetscFunctionBegin;
5124d0f46423SBarry Smith   if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend){
5125e32f2f54SBarry Smith     SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Matrix local dimensions are incompatible, (%D, %D) != (%D,%D)",A->cmap->rstart,A->cmap->rend,B->rmap->rstart,B->rmap->rend);
512625616d81SHong Zhang   }
51274ebed01fSBarry Smith   ierr = PetscLogEventBegin(MAT_GetBrowsOfAcols,A,B,0,0);CHKERRQ(ierr);
512825616d81SHong Zhang 
512925616d81SHong Zhang   if (scall == MAT_INITIAL_MATRIX){
5130d0f46423SBarry Smith     start = A->cmap->rstart;
513125616d81SHong Zhang     cmap  = a->garray;
5132d0f46423SBarry Smith     nzA   = a->A->cmap->n;
5133d0f46423SBarry Smith     nzB   = a->B->cmap->n;
5134b1d57f15SBarry Smith     ierr  = PetscMalloc((nzA+nzB)*sizeof(PetscInt), &idx);CHKERRQ(ierr);
513525616d81SHong Zhang     ncols = 0;
51360390132cSHong Zhang     for (i=0; i<nzB; i++) {  /* row < local row index */
513725616d81SHong Zhang       if (cmap[i] < start) idx[ncols++] = cmap[i];
513825616d81SHong Zhang       else break;
513925616d81SHong Zhang     }
514025616d81SHong Zhang     imark = i;
51410390132cSHong Zhang     for (i=0; i<nzA; i++) idx[ncols++] = start + i;  /* local rows */
51420390132cSHong Zhang     for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i]; /* row > local row index */
5143d67e408aSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,&isrowb);CHKERRQ(ierr);
5144d0f46423SBarry Smith     ierr = ISCreateStride(PETSC_COMM_SELF,B->cmap->N,0,1,&iscolb);CHKERRQ(ierr);
514525616d81SHong Zhang   } else {
5146e32f2f54SBarry Smith     if (!rowb || !colb) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"IS rowb and colb must be provided for MAT_REUSE_MATRIX");
514725616d81SHong Zhang     isrowb = *rowb; iscolb = *colb;
514825616d81SHong Zhang     ierr = PetscMalloc(sizeof(Mat),&bseq);CHKERRQ(ierr);
514925616d81SHong Zhang     bseq[0] = *B_seq;
515025616d81SHong Zhang   }
515125616d81SHong Zhang   ierr = MatGetSubMatrices(B,1,&isrowb,&iscolb,scall,&bseq);CHKERRQ(ierr);
515225616d81SHong Zhang   *B_seq = bseq[0];
515325616d81SHong Zhang   ierr = PetscFree(bseq);CHKERRQ(ierr);
515425616d81SHong Zhang   if (!rowb){
51556bf464f9SBarry Smith     ierr = ISDestroy(&isrowb);CHKERRQ(ierr);
515625616d81SHong Zhang   } else {
515725616d81SHong Zhang     *rowb = isrowb;
515825616d81SHong Zhang   }
515925616d81SHong Zhang   if (!colb){
51606bf464f9SBarry Smith     ierr = ISDestroy(&iscolb);CHKERRQ(ierr);
516125616d81SHong Zhang   } else {
516225616d81SHong Zhang     *colb = iscolb;
516325616d81SHong Zhang   }
51644ebed01fSBarry Smith   ierr = PetscLogEventEnd(MAT_GetBrowsOfAcols,A,B,0,0);CHKERRQ(ierr);
516525616d81SHong Zhang   PetscFunctionReturn(0);
516625616d81SHong Zhang }
5167429d309bSHong Zhang 
5168a61c8c0fSHong Zhang #undef __FUNCT__
5169f8487c73SHong Zhang #define __FUNCT__ "MatGetBrowsOfAoCols_MPIAIJ"
5170f8487c73SHong Zhang /*
5171f8487c73SHong Zhang     MatGetBrowsOfAoCols_MPIAIJ - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns
517201b7ae99SHong Zhang     of the OFF-DIAGONAL portion of local A
5173429d309bSHong Zhang 
5174429d309bSHong Zhang     Collective on Mat
5175429d309bSHong Zhang 
5176429d309bSHong Zhang    Input Parameters:
5177429d309bSHong Zhang +    A,B - the matrices in mpiaij format
5178598bc09dSHong Zhang -    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
5179429d309bSHong Zhang 
5180429d309bSHong Zhang    Output Parameter:
5181b7f45c76SHong Zhang +    startsj_s - starting point in B's sending j-arrays, saved for MAT_REUSE (or PETSC_NULL)
5182b7f45c76SHong Zhang .    startsj_r - starting point in B's receiving j-arrays, saved for MAT_REUSE (or PETSC_NULL)
5183598bc09dSHong Zhang .    bufa_ptr - array for sending matrix values, saved for MAT_REUSE (or PETSC_NULL)
5184598bc09dSHong Zhang -    B_oth - the sequential matrix generated with size aBn=a->B->cmap->n by B->cmap->N
5185429d309bSHong Zhang 
5186429d309bSHong Zhang     Level: developer
5187429d309bSHong Zhang 
5188f8487c73SHong Zhang */
5189b7f45c76SHong Zhang PetscErrorCode  MatGetBrowsOfAoCols_MPIAIJ(Mat A,Mat B,MatReuse scall,PetscInt **startsj_s,PetscInt **startsj_r,MatScalar **bufa_ptr,Mat *B_oth)
5190429d309bSHong Zhang {
5191a6b2eed2SHong Zhang   VecScatter_MPI_General *gen_to,*gen_from;
5192429d309bSHong Zhang   PetscErrorCode         ierr;
5193899cda47SBarry Smith   Mat_MPIAIJ             *a=(Mat_MPIAIJ*)A->data;
519487025532SHong Zhang   Mat_SeqAIJ             *b_oth;
5195a6b2eed2SHong Zhang   VecScatter             ctx=a->Mvctx;
51967adad957SLisandro Dalcin   MPI_Comm               comm=((PetscObject)ctx)->comm;
51977adad957SLisandro Dalcin   PetscMPIInt            *rprocs,*sprocs,tag=((PetscObject)ctx)->tag,rank;
5198d0f46423SBarry Smith   PetscInt               *rowlen,*bufj,*bufJ,ncols,aBn=a->B->cmap->n,row,*b_othi,*b_othj;
5199dd6ea824SBarry Smith   PetscScalar            *rvalues,*svalues;
5200dd6ea824SBarry Smith   MatScalar              *b_otha,*bufa,*bufA;
5201e42f35eeSHong Zhang   PetscInt               i,j,k,l,ll,nrecvs,nsends,nrows,*srow,*rstarts,*rstartsj = 0,*sstarts,*sstartsj,len;
5202910ba992SMatthew Knepley   MPI_Request            *rwaits = PETSC_NULL,*swaits = PETSC_NULL;
520387025532SHong Zhang   MPI_Status             *sstatus,rstatus;
5204aa5bb8c0SSatish Balay   PetscMPIInt            jj;
5205e42f35eeSHong Zhang   PetscInt               *cols,sbs,rbs;
5206ba8c8a56SBarry Smith   PetscScalar            *vals;
5207429d309bSHong Zhang 
5208429d309bSHong Zhang   PetscFunctionBegin;
5209d0f46423SBarry Smith   if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend){
5210e32f2f54SBarry Smith     SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Matrix local dimensions are incompatible, (%d, %d) != (%d,%d)",A->cmap->rstart,A->cmap->rend,B->rmap->rstart,B->rmap->rend);
5211429d309bSHong Zhang   }
52124ebed01fSBarry Smith   ierr = PetscLogEventBegin(MAT_GetBrowsOfAocols,A,B,0,0);CHKERRQ(ierr);
5213a6b2eed2SHong Zhang   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
5214a6b2eed2SHong Zhang 
5215a6b2eed2SHong Zhang   gen_to   = (VecScatter_MPI_General*)ctx->todata;
5216a6b2eed2SHong Zhang   gen_from = (VecScatter_MPI_General*)ctx->fromdata;
5217e42f35eeSHong Zhang   rvalues  = gen_from->values; /* holds the length of receiving row */
5218e42f35eeSHong Zhang   svalues  = gen_to->values;   /* holds the length of sending row */
5219a6b2eed2SHong Zhang   nrecvs   = gen_from->n;
5220a6b2eed2SHong Zhang   nsends   = gen_to->n;
5221d7ee0231SBarry Smith 
5222d7ee0231SBarry Smith   ierr = PetscMalloc2(nrecvs,MPI_Request,&rwaits,nsends,MPI_Request,&swaits);CHKERRQ(ierr);
5223a6b2eed2SHong Zhang   srow     = gen_to->indices;   /* local row index to be sent */
5224a6b2eed2SHong Zhang   sstarts  = gen_to->starts;
5225a6b2eed2SHong Zhang   sprocs   = gen_to->procs;
5226a6b2eed2SHong Zhang   sstatus  = gen_to->sstatus;
5227e42f35eeSHong Zhang   sbs      = gen_to->bs;
5228e42f35eeSHong Zhang   rstarts  = gen_from->starts;
5229e42f35eeSHong Zhang   rprocs   = gen_from->procs;
5230e42f35eeSHong Zhang   rbs      = gen_from->bs;
5231429d309bSHong Zhang 
5232b7f45c76SHong Zhang   if (!startsj_s || !bufa_ptr) scall = MAT_INITIAL_MATRIX;
5233429d309bSHong Zhang   if (scall == MAT_INITIAL_MATRIX){
5234a6b2eed2SHong Zhang     /* i-array */
5235a6b2eed2SHong Zhang     /*---------*/
5236a6b2eed2SHong Zhang     /*  post receives */
5237a6b2eed2SHong Zhang     for (i=0; i<nrecvs; i++){
5238e42f35eeSHong Zhang       rowlen = (PetscInt*)rvalues + rstarts[i]*rbs;
5239e42f35eeSHong Zhang       nrows = (rstarts[i+1]-rstarts[i])*rbs; /* num of indices to be received */
524087025532SHong Zhang       ierr = MPI_Irecv(rowlen,nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr);
5241429d309bSHong Zhang     }
5242a6b2eed2SHong Zhang 
5243a6b2eed2SHong Zhang     /* pack the outgoing message */
52441d79065fSBarry Smith     ierr = PetscMalloc2(nsends+1,PetscInt,&sstartsj,nrecvs+1,PetscInt,&rstartsj);CHKERRQ(ierr);
5245a6b2eed2SHong Zhang     sstartsj[0] = 0;  rstartsj[0] = 0;
5246a6b2eed2SHong Zhang     len = 0; /* total length of j or a array to be sent */
5247a6b2eed2SHong Zhang     k = 0;
5248a6b2eed2SHong Zhang     for (i=0; i<nsends; i++){
5249e42f35eeSHong Zhang       rowlen = (PetscInt*)svalues + sstarts[i]*sbs;
5250e42f35eeSHong Zhang       nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */
525187025532SHong Zhang       for (j=0; j<nrows; j++) {
5252d0f46423SBarry Smith         row = srow[k] + B->rmap->range[rank]; /* global row idx */
5253e42f35eeSHong Zhang         for (l=0; l<sbs; l++){
5254e42f35eeSHong Zhang           ierr = MatGetRow_MPIAIJ(B,row+l,&ncols,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); /* rowlength */
5255e42f35eeSHong Zhang           rowlen[j*sbs+l] = ncols;
5256e42f35eeSHong Zhang           len += ncols;
5257e42f35eeSHong Zhang           ierr = MatRestoreRow_MPIAIJ(B,row+l,&ncols,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
5258e42f35eeSHong Zhang         }
5259a6b2eed2SHong Zhang         k++;
5260429d309bSHong Zhang       }
5261e42f35eeSHong Zhang       ierr = MPI_Isend(rowlen,nrows*sbs,MPIU_INT,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr);
5262dea91ad1SHong Zhang       sstartsj[i+1] = len;  /* starting point of (i+1)-th outgoing msg in bufj and bufa */
5263429d309bSHong Zhang     }
526487025532SHong Zhang     /* recvs and sends of i-array are completed */
526587025532SHong Zhang     i = nrecvs;
526687025532SHong Zhang     while (i--) {
5267aa5bb8c0SSatish Balay       ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr);
526887025532SHong Zhang     }
52690c468ba9SBarry Smith     if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);}
5270e42f35eeSHong Zhang 
5271a6b2eed2SHong Zhang     /* allocate buffers for sending j and a arrays */
5272a6b2eed2SHong Zhang     ierr = PetscMalloc((len+1)*sizeof(PetscInt),&bufj);CHKERRQ(ierr);
5273a6b2eed2SHong Zhang     ierr = PetscMalloc((len+1)*sizeof(PetscScalar),&bufa);CHKERRQ(ierr);
5274a6b2eed2SHong Zhang 
527587025532SHong Zhang     /* create i-array of B_oth */
527687025532SHong Zhang     ierr = PetscMalloc((aBn+2)*sizeof(PetscInt),&b_othi);CHKERRQ(ierr);
527787025532SHong Zhang     b_othi[0] = 0;
5278a6b2eed2SHong Zhang     len = 0; /* total length of j or a array to be received */
5279a6b2eed2SHong Zhang     k = 0;
5280a6b2eed2SHong Zhang     for (i=0; i<nrecvs; i++){
5281fd0ff01cSHong Zhang       rowlen = (PetscInt*)rvalues + rstarts[i]*rbs;
5282e42f35eeSHong Zhang       nrows = rbs*(rstarts[i+1]-rstarts[i]); /* num of rows to be recieved */
528387025532SHong Zhang       for (j=0; j<nrows; j++) {
528487025532SHong Zhang         b_othi[k+1] = b_othi[k] + rowlen[j];
5285a6b2eed2SHong Zhang         len += rowlen[j]; k++;
5286a6b2eed2SHong Zhang       }
5287dea91ad1SHong Zhang       rstartsj[i+1] = len; /* starting point of (i+1)-th incoming msg in bufj and bufa */
5288a6b2eed2SHong Zhang     }
5289a6b2eed2SHong Zhang 
529087025532SHong Zhang     /* allocate space for j and a arrrays of B_oth */
529187025532SHong Zhang     ierr = PetscMalloc((b_othi[aBn]+1)*sizeof(PetscInt),&b_othj);CHKERRQ(ierr);
5292dd6ea824SBarry Smith     ierr = PetscMalloc((b_othi[aBn]+1)*sizeof(MatScalar),&b_otha);CHKERRQ(ierr);
5293a6b2eed2SHong Zhang 
529487025532SHong Zhang     /* j-array */
529587025532SHong Zhang     /*---------*/
5296a6b2eed2SHong Zhang     /*  post receives of j-array */
5297a6b2eed2SHong Zhang     for (i=0; i<nrecvs; i++){
529887025532SHong Zhang       nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */
529987025532SHong Zhang       ierr = MPI_Irecv(b_othj+rstartsj[i],nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr);
5300a6b2eed2SHong Zhang     }
5301e42f35eeSHong Zhang 
5302e42f35eeSHong Zhang     /* pack the outgoing message j-array */
5303a6b2eed2SHong Zhang     k = 0;
5304a6b2eed2SHong Zhang     for (i=0; i<nsends; i++){
5305e42f35eeSHong Zhang       nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */
5306a6b2eed2SHong Zhang       bufJ = bufj+sstartsj[i];
530787025532SHong Zhang       for (j=0; j<nrows; j++) {
5308d0f46423SBarry Smith         row  = srow[k++] + B->rmap->range[rank]; /* global row idx */
5309e42f35eeSHong Zhang         for (ll=0; ll<sbs; ll++){
5310e42f35eeSHong Zhang           ierr = MatGetRow_MPIAIJ(B,row+ll,&ncols,&cols,PETSC_NULL);CHKERRQ(ierr);
5311a6b2eed2SHong Zhang           for (l=0; l<ncols; l++){
5312a6b2eed2SHong Zhang             *bufJ++ = cols[l];
531387025532SHong Zhang           }
5314e42f35eeSHong Zhang           ierr = MatRestoreRow_MPIAIJ(B,row+ll,&ncols,&cols,PETSC_NULL);CHKERRQ(ierr);
5315e42f35eeSHong Zhang         }
531687025532SHong Zhang       }
531787025532SHong Zhang       ierr = MPI_Isend(bufj+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_INT,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr);
531887025532SHong Zhang     }
531987025532SHong Zhang 
532087025532SHong Zhang     /* recvs and sends of j-array are completed */
532187025532SHong Zhang     i = nrecvs;
532287025532SHong Zhang     while (i--) {
5323aa5bb8c0SSatish Balay       ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr);
532487025532SHong Zhang     }
53250c468ba9SBarry Smith     if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);}
532687025532SHong Zhang   } else if (scall == MAT_REUSE_MATRIX){
5327b7f45c76SHong Zhang     sstartsj = *startsj_s;
53281d79065fSBarry Smith     rstartsj = *startsj_r;
532987025532SHong Zhang     bufa     = *bufa_ptr;
533087025532SHong Zhang     b_oth    = (Mat_SeqAIJ*)(*B_oth)->data;
533187025532SHong Zhang     b_otha   = b_oth->a;
533287025532SHong Zhang   } else {
5333e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Matrix P does not posses an object container");
533487025532SHong Zhang   }
533587025532SHong Zhang 
533687025532SHong Zhang   /* a-array */
533787025532SHong Zhang   /*---------*/
533887025532SHong Zhang   /*  post receives of a-array */
533987025532SHong Zhang   for (i=0; i<nrecvs; i++){
534087025532SHong Zhang     nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */
534187025532SHong Zhang     ierr = MPI_Irecv(b_otha+rstartsj[i],nrows,MPIU_SCALAR,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr);
534287025532SHong Zhang   }
5343e42f35eeSHong Zhang 
5344e42f35eeSHong Zhang   /* pack the outgoing message a-array */
534587025532SHong Zhang   k = 0;
534687025532SHong Zhang   for (i=0; i<nsends; i++){
5347e42f35eeSHong Zhang     nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */
534887025532SHong Zhang     bufA = bufa+sstartsj[i];
534987025532SHong Zhang     for (j=0; j<nrows; j++) {
5350d0f46423SBarry Smith       row  = srow[k++] + B->rmap->range[rank]; /* global row idx */
5351e42f35eeSHong Zhang       for (ll=0; ll<sbs; ll++){
5352e42f35eeSHong Zhang         ierr = MatGetRow_MPIAIJ(B,row+ll,&ncols,PETSC_NULL,&vals);CHKERRQ(ierr);
535387025532SHong Zhang         for (l=0; l<ncols; l++){
5354a6b2eed2SHong Zhang           *bufA++ = vals[l];
5355a6b2eed2SHong Zhang         }
5356e42f35eeSHong Zhang         ierr = MatRestoreRow_MPIAIJ(B,row+ll,&ncols,PETSC_NULL,&vals);CHKERRQ(ierr);
5357e42f35eeSHong Zhang       }
5358a6b2eed2SHong Zhang     }
535987025532SHong Zhang     ierr = MPI_Isend(bufa+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_SCALAR,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr);
5360a6b2eed2SHong Zhang   }
536187025532SHong Zhang   /* recvs and sends of a-array are completed */
536287025532SHong Zhang   i = nrecvs;
536387025532SHong Zhang   while (i--) {
5364aa5bb8c0SSatish Balay     ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr);
536587025532SHong Zhang   }
53660c468ba9SBarry Smith   if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);}
5367d7ee0231SBarry Smith   ierr = PetscFree2(rwaits,swaits);CHKERRQ(ierr);
5368a6b2eed2SHong Zhang 
536987025532SHong Zhang   if (scall == MAT_INITIAL_MATRIX){
5370a6b2eed2SHong Zhang     /* put together the new matrix */
5371d0f46423SBarry Smith     ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,aBn,B->cmap->N,b_othi,b_othj,b_otha,B_oth);CHKERRQ(ierr);
5372a6b2eed2SHong Zhang 
5373a6b2eed2SHong Zhang     /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
5374a6b2eed2SHong Zhang     /* Since these are PETSc arrays, change flags to free them as necessary. */
537587025532SHong Zhang     b_oth          = (Mat_SeqAIJ *)(*B_oth)->data;
5376e6b907acSBarry Smith     b_oth->free_a  = PETSC_TRUE;
5377e6b907acSBarry Smith     b_oth->free_ij = PETSC_TRUE;
537887025532SHong Zhang     b_oth->nonew   = 0;
5379a6b2eed2SHong Zhang 
5380a6b2eed2SHong Zhang     ierr = PetscFree(bufj);CHKERRQ(ierr);
5381b7f45c76SHong Zhang     if (!startsj_s || !bufa_ptr){
53821d79065fSBarry Smith       ierr = PetscFree2(sstartsj,rstartsj);CHKERRQ(ierr);
5383dea91ad1SHong Zhang       ierr = PetscFree(bufa_ptr);CHKERRQ(ierr);
5384dea91ad1SHong Zhang     } else {
5385b7f45c76SHong Zhang       *startsj_s = sstartsj;
53861d79065fSBarry Smith       *startsj_r = rstartsj;
538787025532SHong Zhang       *bufa_ptr  = bufa;
538887025532SHong Zhang     }
5389dea91ad1SHong Zhang   }
53904ebed01fSBarry Smith   ierr = PetscLogEventEnd(MAT_GetBrowsOfAocols,A,B,0,0);CHKERRQ(ierr);
5391429d309bSHong Zhang   PetscFunctionReturn(0);
5392429d309bSHong Zhang }
5393ccd8e176SBarry Smith 
539443eb5e2fSMatthew Knepley #undef __FUNCT__
539543eb5e2fSMatthew Knepley #define __FUNCT__ "MatGetCommunicationStructs"
539643eb5e2fSMatthew Knepley /*@C
539743eb5e2fSMatthew Knepley   MatGetCommunicationStructs - Provides access to the communication structures used in matrix-vector multiplication.
539843eb5e2fSMatthew Knepley 
539943eb5e2fSMatthew Knepley   Not Collective
540043eb5e2fSMatthew Knepley 
540143eb5e2fSMatthew Knepley   Input Parameters:
540243eb5e2fSMatthew Knepley . A - The matrix in mpiaij format
540343eb5e2fSMatthew Knepley 
540443eb5e2fSMatthew Knepley   Output Parameter:
540543eb5e2fSMatthew Knepley + lvec - The local vector holding off-process values from the argument to a matrix-vector product
540643eb5e2fSMatthew Knepley . colmap - A map from global column index to local index into lvec
540743eb5e2fSMatthew Knepley - multScatter - A scatter from the argument of a matrix-vector product to lvec
540843eb5e2fSMatthew Knepley 
540943eb5e2fSMatthew Knepley   Level: developer
541043eb5e2fSMatthew Knepley 
541143eb5e2fSMatthew Knepley @*/
541243eb5e2fSMatthew Knepley #if defined (PETSC_USE_CTABLE)
54137087cfbeSBarry Smith PetscErrorCode  MatGetCommunicationStructs(Mat A, Vec *lvec, PetscTable *colmap, VecScatter *multScatter)
541443eb5e2fSMatthew Knepley #else
54157087cfbeSBarry Smith PetscErrorCode  MatGetCommunicationStructs(Mat A, Vec *lvec, PetscInt *colmap[], VecScatter *multScatter)
541643eb5e2fSMatthew Knepley #endif
541743eb5e2fSMatthew Knepley {
541843eb5e2fSMatthew Knepley   Mat_MPIAIJ *a;
541943eb5e2fSMatthew Knepley 
542043eb5e2fSMatthew Knepley   PetscFunctionBegin;
54210700a824SBarry Smith   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
5422e414b56bSJed Brown   PetscValidPointer(lvec, 2);
5423e414b56bSJed Brown   PetscValidPointer(colmap, 3);
5424e414b56bSJed Brown   PetscValidPointer(multScatter, 4);
542543eb5e2fSMatthew Knepley   a = (Mat_MPIAIJ *) A->data;
542643eb5e2fSMatthew Knepley   if (lvec) *lvec = a->lvec;
542743eb5e2fSMatthew Knepley   if (colmap) *colmap = a->colmap;
542843eb5e2fSMatthew Knepley   if (multScatter) *multScatter = a->Mvctx;
542943eb5e2fSMatthew Knepley   PetscFunctionReturn(0);
543043eb5e2fSMatthew Knepley }
543143eb5e2fSMatthew Knepley 
543217667f90SBarry Smith EXTERN_C_BEGIN
54337087cfbeSBarry Smith extern PetscErrorCode  MatConvert_MPIAIJ_MPIAIJCRL(Mat,const MatType,MatReuse,Mat*);
54347087cfbeSBarry Smith extern PetscErrorCode  MatConvert_MPIAIJ_MPIAIJPERM(Mat,const MatType,MatReuse,Mat*);
54357087cfbeSBarry Smith extern PetscErrorCode  MatConvert_MPIAIJ_MPISBAIJ(Mat,const MatType,MatReuse,Mat*);
543617667f90SBarry Smith EXTERN_C_END
543717667f90SBarry Smith 
5438fc4dec0aSBarry Smith #undef __FUNCT__
5439fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMultNumeric_MPIDense_MPIAIJ"
5440fc4dec0aSBarry Smith /*
5441fc4dec0aSBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
5442fc4dec0aSBarry Smith 
5443fc4dec0aSBarry Smith                n                       p                          p
5444fc4dec0aSBarry Smith         (              )       (              )         (                  )
5445fc4dec0aSBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
5446fc4dec0aSBarry Smith         (              )       (              )         (                  )
5447fc4dec0aSBarry Smith 
5448fc4dec0aSBarry Smith */
5449fc4dec0aSBarry Smith PetscErrorCode MatMatMultNumeric_MPIDense_MPIAIJ(Mat A,Mat B,Mat C)
5450fc4dec0aSBarry Smith {
5451fc4dec0aSBarry Smith   PetscErrorCode     ierr;
5452fc4dec0aSBarry Smith   Mat                At,Bt,Ct;
5453fc4dec0aSBarry Smith 
5454fc4dec0aSBarry Smith   PetscFunctionBegin;
5455fc4dec0aSBarry Smith   ierr = MatTranspose(A,MAT_INITIAL_MATRIX,&At);CHKERRQ(ierr);
5456fc4dec0aSBarry Smith   ierr = MatTranspose(B,MAT_INITIAL_MATRIX,&Bt);CHKERRQ(ierr);
5457fc4dec0aSBarry Smith   ierr = MatMatMult(Bt,At,MAT_INITIAL_MATRIX,1.0,&Ct);CHKERRQ(ierr);
54586bf464f9SBarry Smith   ierr = MatDestroy(&At);CHKERRQ(ierr);
54596bf464f9SBarry Smith   ierr = MatDestroy(&Bt);CHKERRQ(ierr);
5460fc4dec0aSBarry Smith   ierr = MatTranspose(Ct,MAT_REUSE_MATRIX,&C);CHKERRQ(ierr);
54616bf464f9SBarry Smith   ierr = MatDestroy(&Ct);CHKERRQ(ierr);
5462fc4dec0aSBarry Smith   PetscFunctionReturn(0);
5463fc4dec0aSBarry Smith }
5464fc4dec0aSBarry Smith 
5465fc4dec0aSBarry Smith #undef __FUNCT__
5466fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMultSymbolic_MPIDense_MPIAIJ"
5467fc4dec0aSBarry Smith PetscErrorCode MatMatMultSymbolic_MPIDense_MPIAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
5468fc4dec0aSBarry Smith {
5469fc4dec0aSBarry Smith   PetscErrorCode ierr;
5470d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
5471fc4dec0aSBarry Smith   Mat            Cmat;
5472fc4dec0aSBarry Smith 
5473fc4dec0aSBarry Smith   PetscFunctionBegin;
5474e32f2f54SBarry Smith   if (A->cmap->n != B->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"A->cmap->n %d != B->rmap->n %d\n",A->cmap->n,B->rmap->n);
547539804f7cSBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,&Cmat);CHKERRQ(ierr);
5476fc4dec0aSBarry Smith   ierr = MatSetSizes(Cmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
5477a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(Cmat,A->rmap->bs,B->cmap->bs);CHKERRQ(ierr);
5478fc4dec0aSBarry Smith   ierr = MatSetType(Cmat,MATMPIDENSE);CHKERRQ(ierr);
5479fc4dec0aSBarry Smith   ierr = MatMPIDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr);
548038556019SBarry Smith   ierr = MatAssemblyBegin(Cmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
548138556019SBarry Smith   ierr = MatAssemblyEnd(Cmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
5482fc4dec0aSBarry Smith   *C   = Cmat;
54838cdbd757SHong Zhang   (*C)->ops->matmult = MatMatMult_MPIDense_MPIAIJ;
5484fc4dec0aSBarry Smith   PetscFunctionReturn(0);
5485fc4dec0aSBarry Smith }
5486fc4dec0aSBarry Smith 
5487fc4dec0aSBarry Smith /* ----------------------------------------------------------------*/
5488fc4dec0aSBarry Smith #undef __FUNCT__
5489fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMult_MPIDense_MPIAIJ"
5490fc4dec0aSBarry Smith PetscErrorCode MatMatMult_MPIDense_MPIAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
5491fc4dec0aSBarry Smith {
5492fc4dec0aSBarry Smith   PetscErrorCode ierr;
5493fc4dec0aSBarry Smith 
5494fc4dec0aSBarry Smith   PetscFunctionBegin;
5495fc4dec0aSBarry Smith   if (scall == MAT_INITIAL_MATRIX){
5496fc4dec0aSBarry Smith     ierr = MatMatMultSymbolic_MPIDense_MPIAIJ(A,B,fill,C);CHKERRQ(ierr);
5497fc4dec0aSBarry Smith   }
5498fc4dec0aSBarry Smith   ierr = MatMatMultNumeric_MPIDense_MPIAIJ(A,B,*C);CHKERRQ(ierr);
5499fc4dec0aSBarry Smith   PetscFunctionReturn(0);
5500fc4dec0aSBarry Smith }
5501fc4dec0aSBarry Smith 
55025c9eb25fSBarry Smith EXTERN_C_BEGIN
5503611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
5504bccb9932SShri Abhyankar extern PetscErrorCode MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*);
5505611f576cSBarry Smith #endif
55063bf14a46SMatthew Knepley #if defined(PETSC_HAVE_PASTIX)
55073bf14a46SMatthew Knepley extern PetscErrorCode MatGetFactor_mpiaij_pastix(Mat,MatFactorType,Mat*);
55083bf14a46SMatthew Knepley #endif
5509611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU_DIST)
55105c9eb25fSBarry Smith extern PetscErrorCode MatGetFactor_mpiaij_superlu_dist(Mat,MatFactorType,Mat*);
5511611f576cSBarry Smith #endif
5512611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
55135c9eb25fSBarry Smith extern PetscErrorCode MatGetFactor_mpiaij_spooles(Mat,MatFactorType,Mat*);
5514611f576cSBarry Smith #endif
55155c9eb25fSBarry Smith EXTERN_C_END
55165c9eb25fSBarry Smith 
5517ccd8e176SBarry Smith /*MC
5518ccd8e176SBarry Smith    MATMPIAIJ - MATMPIAIJ = "mpiaij" - A matrix type to be used for parallel sparse matrices.
5519ccd8e176SBarry Smith 
5520ccd8e176SBarry Smith    Options Database Keys:
5521ccd8e176SBarry Smith . -mat_type mpiaij - sets the matrix type to "mpiaij" during a call to MatSetFromOptions()
5522ccd8e176SBarry Smith 
5523ccd8e176SBarry Smith   Level: beginner
5524ccd8e176SBarry Smith 
552569b1f4b7SBarry Smith .seealso: MatCreateAIJ()
5526ccd8e176SBarry Smith M*/
5527ccd8e176SBarry Smith 
5528ccd8e176SBarry Smith EXTERN_C_BEGIN
5529ccd8e176SBarry Smith #undef __FUNCT__
5530ccd8e176SBarry Smith #define __FUNCT__ "MatCreate_MPIAIJ"
55317087cfbeSBarry Smith PetscErrorCode  MatCreate_MPIAIJ(Mat B)
5532ccd8e176SBarry Smith {
5533ccd8e176SBarry Smith   Mat_MPIAIJ     *b;
5534ccd8e176SBarry Smith   PetscErrorCode ierr;
5535ccd8e176SBarry Smith   PetscMPIInt    size;
5536ccd8e176SBarry Smith 
5537ccd8e176SBarry Smith   PetscFunctionBegin;
55387adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
5539ccd8e176SBarry Smith 
554038f2d2fdSLisandro Dalcin   ierr            = PetscNewLog(B,Mat_MPIAIJ,&b);CHKERRQ(ierr);
5541ccd8e176SBarry Smith   B->data         = (void*)b;
5542ccd8e176SBarry Smith   ierr            = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
5543ccd8e176SBarry Smith   B->assembled    = PETSC_FALSE;
5544ccd8e176SBarry Smith 
5545ccd8e176SBarry Smith   B->insertmode   = NOT_SET_VALUES;
5546ccd8e176SBarry Smith   b->size         = size;
55477adad957SLisandro Dalcin   ierr = MPI_Comm_rank(((PetscObject)B)->comm,&b->rank);CHKERRQ(ierr);
5548ccd8e176SBarry Smith 
5549ccd8e176SBarry Smith   /* build cache for off array entries formed */
55507adad957SLisandro Dalcin   ierr = MatStashCreate_Private(((PetscObject)B)->comm,1,&B->stash);CHKERRQ(ierr);
5551ccd8e176SBarry Smith   b->donotstash  = PETSC_FALSE;
5552ccd8e176SBarry Smith   b->colmap      = 0;
5553ccd8e176SBarry Smith   b->garray      = 0;
5554ccd8e176SBarry Smith   b->roworiented = PETSC_TRUE;
5555ccd8e176SBarry Smith 
5556ccd8e176SBarry Smith   /* stuff used for matrix vector multiply */
5557ccd8e176SBarry Smith   b->lvec      = PETSC_NULL;
5558ccd8e176SBarry Smith   b->Mvctx     = PETSC_NULL;
5559ccd8e176SBarry Smith 
5560ccd8e176SBarry Smith   /* stuff for MatGetRow() */
5561ccd8e176SBarry Smith   b->rowindices   = 0;
5562ccd8e176SBarry Smith   b->rowvalues    = 0;
5563ccd8e176SBarry Smith   b->getrowactive = PETSC_FALSE;
5564ccd8e176SBarry Smith 
5565611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
5566ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_spooles_C",
55675c9eb25fSBarry Smith                                      "MatGetFactor_mpiaij_spooles",
55685c9eb25fSBarry Smith                                      MatGetFactor_mpiaij_spooles);CHKERRQ(ierr);
5569611f576cSBarry Smith #endif
5570611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
5571ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C",
5572bccb9932SShri Abhyankar                                      "MatGetFactor_aij_mumps",
5573bccb9932SShri Abhyankar                                      MatGetFactor_aij_mumps);CHKERRQ(ierr);
5574611f576cSBarry Smith #endif
55753bf14a46SMatthew Knepley #if defined(PETSC_HAVE_PASTIX)
5576ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C",
55773bf14a46SMatthew Knepley 					   "MatGetFactor_mpiaij_pastix",
55783bf14a46SMatthew Knepley 					   MatGetFactor_mpiaij_pastix);CHKERRQ(ierr);
55793bf14a46SMatthew Knepley #endif
5580611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU_DIST)
5581ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_dist_C",
55825c9eb25fSBarry Smith                                      "MatGetFactor_mpiaij_superlu_dist",
55835c9eb25fSBarry Smith                                      MatGetFactor_mpiaij_superlu_dist);CHKERRQ(ierr);
5584611f576cSBarry Smith #endif
5585ccd8e176SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
5586ccd8e176SBarry Smith                                      "MatStoreValues_MPIAIJ",
5587ccd8e176SBarry Smith                                      MatStoreValues_MPIAIJ);CHKERRQ(ierr);
5588ccd8e176SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
5589ccd8e176SBarry Smith                                      "MatRetrieveValues_MPIAIJ",
5590ccd8e176SBarry Smith                                      MatRetrieveValues_MPIAIJ);CHKERRQ(ierr);
5591ccd8e176SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetDiagonalBlock_C",
5592ccd8e176SBarry Smith 				     "MatGetDiagonalBlock_MPIAIJ",
5593ccd8e176SBarry Smith                                      MatGetDiagonalBlock_MPIAIJ);CHKERRQ(ierr);
5594ccd8e176SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C",
5595ccd8e176SBarry Smith 				     "MatIsTranspose_MPIAIJ",
5596ccd8e176SBarry Smith 				     MatIsTranspose_MPIAIJ);CHKERRQ(ierr);
5597ccd8e176SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAIJSetPreallocation_C",
5598ccd8e176SBarry Smith 				     "MatMPIAIJSetPreallocation_MPIAIJ",
5599ccd8e176SBarry Smith 				     MatMPIAIJSetPreallocation_MPIAIJ);CHKERRQ(ierr);
5600ccd8e176SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAIJSetPreallocationCSR_C",
5601ccd8e176SBarry Smith 				     "MatMPIAIJSetPreallocationCSR_MPIAIJ",
5602ccd8e176SBarry Smith 				     MatMPIAIJSetPreallocationCSR_MPIAIJ);CHKERRQ(ierr);
5603ccd8e176SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatDiagonalScaleLocal_C",
5604ccd8e176SBarry Smith 				     "MatDiagonalScaleLocal_MPIAIJ",
5605ccd8e176SBarry Smith 				     MatDiagonalScaleLocal_MPIAIJ);CHKERRQ(ierr);
56065a11e1b2SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_mpiaijperm_C",
56075a11e1b2SBarry Smith                                      "MatConvert_MPIAIJ_MPIAIJPERM",
56085a11e1b2SBarry Smith                                       MatConvert_MPIAIJ_MPIAIJPERM);CHKERRQ(ierr);
56095a11e1b2SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_mpiaijcrl_C",
56105a11e1b2SBarry Smith                                      "MatConvert_MPIAIJ_MPIAIJCRL",
56115a11e1b2SBarry Smith                                       MatConvert_MPIAIJ_MPIAIJCRL);CHKERRQ(ierr);
5612471cc821SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_mpisbaij_C",
5613471cc821SHong Zhang                                      "MatConvert_MPIAIJ_MPISBAIJ",
5614471cc821SHong Zhang                                       MatConvert_MPIAIJ_MPISBAIJ);CHKERRQ(ierr);
5615fc4dec0aSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_mpidense_mpiaij_C",
5616fc4dec0aSBarry Smith                                      "MatMatMult_MPIDense_MPIAIJ",
5617fc4dec0aSBarry Smith                                       MatMatMult_MPIDense_MPIAIJ);CHKERRQ(ierr);
5618fc4dec0aSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_mpidense_mpiaij_C",
5619fc4dec0aSBarry Smith                                      "MatMatMultSymbolic_MPIDense_MPIAIJ",
5620fc4dec0aSBarry Smith                                      MatMatMultSymbolic_MPIDense_MPIAIJ);CHKERRQ(ierr);
5621fc4dec0aSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_mpidense_mpiaij_C",
5622fc4dec0aSBarry Smith                                      "MatMatMultNumeric_MPIDense_MPIAIJ",
5623fc4dec0aSBarry Smith                                       MatMatMultNumeric_MPIDense_MPIAIJ);CHKERRQ(ierr);
562417667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATMPIAIJ);CHKERRQ(ierr);
5625ccd8e176SBarry Smith   PetscFunctionReturn(0);
5626ccd8e176SBarry Smith }
5627ccd8e176SBarry Smith EXTERN_C_END
562881824310SBarry Smith 
562903bfb495SBarry Smith #undef __FUNCT__
563003bfb495SBarry Smith #define __FUNCT__ "MatCreateMPIAIJWithSplitArrays"
563158d36128SBarry Smith /*@
563203bfb495SBarry Smith      MatCreateMPIAIJWithSplitArrays - creates a MPI AIJ matrix using arrays that contain the "diagonal"
563303bfb495SBarry Smith          and "off-diagonal" part of the matrix in CSR format.
563403bfb495SBarry Smith 
563503bfb495SBarry Smith    Collective on MPI_Comm
563603bfb495SBarry Smith 
563703bfb495SBarry Smith    Input Parameters:
563803bfb495SBarry Smith +  comm - MPI communicator
563903bfb495SBarry Smith .  m - number of local rows (Cannot be PETSC_DECIDE)
564003bfb495SBarry Smith .  n - This value should be the same as the local size used in creating the
564103bfb495SBarry Smith        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
564203bfb495SBarry Smith        calculated if N is given) For square matrices n is almost always m.
564303bfb495SBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
564403bfb495SBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
564503bfb495SBarry Smith .   i - row indices for "diagonal" portion of matrix
564603bfb495SBarry Smith .   j - column indices
564703bfb495SBarry Smith .   a - matrix values
564803bfb495SBarry Smith .   oi - row indices for "off-diagonal" portion of matrix
564903bfb495SBarry Smith .   oj - column indices
565003bfb495SBarry Smith -   oa - matrix values
565103bfb495SBarry Smith 
565203bfb495SBarry Smith    Output Parameter:
565303bfb495SBarry Smith .   mat - the matrix
565403bfb495SBarry Smith 
565503bfb495SBarry Smith    Level: advanced
565603bfb495SBarry Smith 
565703bfb495SBarry Smith    Notes:
5658292fb18eSBarry Smith        The i, j, and a arrays ARE NOT copied by this routine into the internal format used by PETSc. The user
5659292fb18eSBarry Smith        must free the arrays once the matrix has been destroyed and not before.
566003bfb495SBarry Smith 
566103bfb495SBarry Smith        The i and j indices are 0 based
566203bfb495SBarry Smith 
566369b1f4b7SBarry Smith        See MatCreateAIJ() for the definition of "diagonal" and "off-diagonal" portion of the matrix
566403bfb495SBarry Smith 
56657b55108eSBarry Smith        This sets local rows and cannot be used to set off-processor values.
56667b55108eSBarry Smith 
56677b55108eSBarry Smith        You cannot later use MatSetValues() to change values in this matrix.
566803bfb495SBarry Smith 
566903bfb495SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
567003bfb495SBarry Smith 
567103bfb495SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
567269b1f4b7SBarry Smith           MPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithArrays()
567303bfb495SBarry Smith @*/
56747087cfbeSBarry Smith PetscErrorCode  MatCreateMPIAIJWithSplitArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,PetscInt i[],PetscInt j[],PetscScalar a[],
567503bfb495SBarry Smith 								PetscInt oi[], PetscInt oj[],PetscScalar oa[],Mat *mat)
567603bfb495SBarry Smith {
567703bfb495SBarry Smith   PetscErrorCode ierr;
567803bfb495SBarry Smith   Mat_MPIAIJ     *maij;
567903bfb495SBarry Smith 
568003bfb495SBarry Smith  PetscFunctionBegin;
5681e32f2f54SBarry Smith   if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative");
5682ea345e14SBarry Smith   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
5683ea345e14SBarry Smith   if (oi[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"oi (row indices) must start with 0");
568403bfb495SBarry Smith   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
568503bfb495SBarry Smith   ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr);
568603bfb495SBarry Smith   ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr);
568703bfb495SBarry Smith   maij = (Mat_MPIAIJ*) (*mat)->data;
56888d7a6e47SBarry Smith   maij->donotstash     = PETSC_TRUE;
56898d7a6e47SBarry Smith   (*mat)->preallocated = PETSC_TRUE;
569003bfb495SBarry Smith 
569126283091SBarry Smith   ierr = PetscLayoutSetUp((*mat)->rmap);CHKERRQ(ierr);
569226283091SBarry Smith   ierr = PetscLayoutSetUp((*mat)->cmap);CHKERRQ(ierr);
569303bfb495SBarry Smith 
569403bfb495SBarry Smith   ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,n,i,j,a,&maij->A);CHKERRQ(ierr);
5695d0f46423SBarry Smith   ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,(*mat)->cmap->N,oi,oj,oa,&maij->B);CHKERRQ(ierr);
569603bfb495SBarry Smith 
56978d7a6e47SBarry Smith   ierr = MatAssemblyBegin(maij->A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
56988d7a6e47SBarry Smith   ierr = MatAssemblyEnd(maij->A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
56998d7a6e47SBarry Smith   ierr = MatAssemblyBegin(maij->B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
57008d7a6e47SBarry Smith   ierr = MatAssemblyEnd(maij->B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
57018d7a6e47SBarry Smith 
570203bfb495SBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
570303bfb495SBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
570403bfb495SBarry Smith   PetscFunctionReturn(0);
570503bfb495SBarry Smith }
570603bfb495SBarry Smith 
570781824310SBarry Smith /*
570881824310SBarry Smith     Special version for direct calls from Fortran
570981824310SBarry Smith */
5710b45d2f2cSJed Brown #include <petsc-private/fortranimpl.h>
57117087cfbeSBarry Smith 
571281824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
571381824310SBarry Smith #define matsetvaluesmpiaij_ MATSETVALUESMPIAIJ
571481824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
571581824310SBarry Smith #define matsetvaluesmpiaij_ matsetvaluesmpiaij
571681824310SBarry Smith #endif
571781824310SBarry Smith 
571881824310SBarry Smith /* Change these macros so can be used in void function */
571981824310SBarry Smith #undef CHKERRQ
5720e32f2f54SBarry Smith #define CHKERRQ(ierr) CHKERRABORT(PETSC_COMM_WORLD,ierr)
572181824310SBarry Smith #undef SETERRQ2
5722e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
57234994cf47SJed Brown #undef SETERRQ3
57244994cf47SJed Brown #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)
572581824310SBarry Smith #undef SETERRQ
5726e32f2f54SBarry Smith #define SETERRQ(c,ierr,b) CHKERRABORT(c,ierr)
572781824310SBarry Smith 
572881824310SBarry Smith EXTERN_C_BEGIN
572981824310SBarry Smith #undef __FUNCT__
573081824310SBarry Smith #define __FUNCT__ "matsetvaluesmpiaij_"
57311f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesmpiaij_(Mat *mmat,PetscInt *mm,const PetscInt im[],PetscInt *mn,const PetscInt in[],const PetscScalar v[],InsertMode *maddv,PetscErrorCode *_ierr)
573281824310SBarry Smith {
573381824310SBarry Smith   Mat             mat = *mmat;
573481824310SBarry Smith   PetscInt        m = *mm, n = *mn;
573581824310SBarry Smith   InsertMode      addv = *maddv;
573681824310SBarry Smith   Mat_MPIAIJ      *aij = (Mat_MPIAIJ*)mat->data;
573781824310SBarry Smith   PetscScalar     value;
573881824310SBarry Smith   PetscErrorCode  ierr;
5739899cda47SBarry Smith 
57404994cf47SJed Brown   MatCheckPreallocated(mat,1);
574181824310SBarry Smith   if (mat->insertmode == NOT_SET_VALUES) {
574281824310SBarry Smith     mat->insertmode = addv;
574381824310SBarry Smith   }
574481824310SBarry Smith #if defined(PETSC_USE_DEBUG)
574581824310SBarry Smith   else if (mat->insertmode != addv) {
5746e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
574781824310SBarry Smith   }
574881824310SBarry Smith #endif
574981824310SBarry Smith   {
5750d0f46423SBarry Smith   PetscInt        i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend;
5751d0f46423SBarry Smith   PetscInt        cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col;
5752ace3abfcSBarry Smith   PetscBool       roworiented = aij->roworiented;
575381824310SBarry Smith 
575481824310SBarry Smith   /* Some Variables required in the macro */
575581824310SBarry Smith   Mat             A = aij->A;
575681824310SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
575781824310SBarry Smith   PetscInt        *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j;
5758dd6ea824SBarry Smith   MatScalar       *aa = a->a;
5759ace3abfcSBarry Smith   PetscBool       ignorezeroentries = (((a->ignorezeroentries)&&(addv==ADD_VALUES))?PETSC_TRUE:PETSC_FALSE);
576081824310SBarry Smith   Mat             B = aij->B;
576181824310SBarry Smith   Mat_SeqAIJ      *b = (Mat_SeqAIJ*)B->data;
5762d0f46423SBarry Smith   PetscInt        *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap->n,am = aij->A->rmap->n;
5763dd6ea824SBarry Smith   MatScalar       *ba = b->a;
576481824310SBarry Smith 
576581824310SBarry Smith   PetscInt        *rp1,*rp2,ii,nrow1,nrow2,_i,rmax1,rmax2,N,low1,high1,low2,high2,t,lastcol1,lastcol2;
576681824310SBarry Smith   PetscInt        nonew = a->nonew;
5767dd6ea824SBarry Smith   MatScalar       *ap1,*ap2;
576881824310SBarry Smith 
576981824310SBarry Smith   PetscFunctionBegin;
577081824310SBarry Smith   for (i=0; i<m; i++) {
577181824310SBarry Smith     if (im[i] < 0) continue;
577281824310SBarry Smith #if defined(PETSC_USE_DEBUG)
5773e32f2f54SBarry Smith     if (im[i] >= mat->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",im[i],mat->rmap->N-1);
577481824310SBarry Smith #endif
577581824310SBarry Smith     if (im[i] >= rstart && im[i] < rend) {
577681824310SBarry Smith       row      = im[i] - rstart;
577781824310SBarry Smith       lastcol1 = -1;
577881824310SBarry Smith       rp1      = aj + ai[row];
577981824310SBarry Smith       ap1      = aa + ai[row];
578081824310SBarry Smith       rmax1    = aimax[row];
578181824310SBarry Smith       nrow1    = ailen[row];
578281824310SBarry Smith       low1     = 0;
578381824310SBarry Smith       high1    = nrow1;
578481824310SBarry Smith       lastcol2 = -1;
578581824310SBarry Smith       rp2      = bj + bi[row];
578681824310SBarry Smith       ap2      = ba + bi[row];
578781824310SBarry Smith       rmax2    = bimax[row];
578881824310SBarry Smith       nrow2    = bilen[row];
578981824310SBarry Smith       low2     = 0;
579081824310SBarry Smith       high2    = nrow2;
579181824310SBarry Smith 
579281824310SBarry Smith       for (j=0; j<n; j++) {
579381824310SBarry Smith         if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
579481824310SBarry Smith         if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES)) continue;
579581824310SBarry Smith         if (in[j] >= cstart && in[j] < cend){
579681824310SBarry Smith           col = in[j] - cstart;
579781824310SBarry Smith           MatSetValues_SeqAIJ_A_Private(row,col,value,addv);
579881824310SBarry Smith         } else if (in[j] < 0) continue;
579981824310SBarry Smith #if defined(PETSC_USE_DEBUG)
5800cb9801acSJed Brown         else if (in[j] >= mat->cmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[j],mat->cmap->N-1);
580181824310SBarry Smith #endif
580281824310SBarry Smith         else {
580381824310SBarry Smith           if (mat->was_assembled) {
580481824310SBarry Smith             if (!aij->colmap) {
5805ab9863d7SBarry Smith               ierr = MatCreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
580681824310SBarry Smith             }
580781824310SBarry Smith #if defined (PETSC_USE_CTABLE)
580881824310SBarry Smith             ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr);
580981824310SBarry Smith 	    col--;
581081824310SBarry Smith #else
581181824310SBarry Smith             col = aij->colmap[in[j]] - 1;
581281824310SBarry Smith #endif
581381824310SBarry Smith             if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
5814ab9863d7SBarry Smith               ierr = MatDisAssemble_MPIAIJ(mat);CHKERRQ(ierr);
581581824310SBarry Smith               col =  in[j];
581681824310SBarry Smith               /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
581781824310SBarry Smith               B = aij->B;
581881824310SBarry Smith               b = (Mat_SeqAIJ*)B->data;
581981824310SBarry Smith               bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j;
582081824310SBarry Smith               rp2      = bj + bi[row];
582181824310SBarry Smith               ap2      = ba + bi[row];
582281824310SBarry Smith               rmax2    = bimax[row];
582381824310SBarry Smith               nrow2    = bilen[row];
582481824310SBarry Smith               low2     = 0;
582581824310SBarry Smith               high2    = nrow2;
5826d0f46423SBarry Smith               bm       = aij->B->rmap->n;
582781824310SBarry Smith               ba = b->a;
582881824310SBarry Smith             }
582981824310SBarry Smith           } else col = in[j];
583081824310SBarry Smith           MatSetValues_SeqAIJ_B_Private(row,col,value,addv);
583181824310SBarry Smith         }
583281824310SBarry Smith       }
583381824310SBarry Smith     } else {
583481824310SBarry Smith       if (!aij->donotstash) {
583581824310SBarry Smith         if (roworiented) {
5836ace3abfcSBarry Smith           ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr);
583781824310SBarry Smith         } else {
5838ace3abfcSBarry Smith           ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr);
583981824310SBarry Smith         }
584081824310SBarry Smith       }
584181824310SBarry Smith     }
584281824310SBarry Smith   }}
584381824310SBarry Smith   PetscFunctionReturnVoid();
584481824310SBarry Smith }
584581824310SBarry Smith EXTERN_C_END
584603bfb495SBarry Smith 
5847