xref: /petsc/src/mat/impls/aij/mpi/mpiaij.c (revision 4b4eb8d3b5c67aff3d3d02420f547e6671225154)
18a729477SBarry Smith 
2c6db04a5SJed Brown #include <../src/mat/impls/aij/mpi/mpiaij.h>   /*I "petscmat.h" I*/
39a6d0b0bSJed Brown #include <petsc-private/vecimpl.h>
4c6db04a5SJed Brown #include <petscblaslapack.h>
50c312b8eSJed Brown #include <petscsf.h>
68a729477SBarry Smith 
701bebe75SBarry Smith /*MC
801bebe75SBarry Smith    MATAIJ - MATAIJ = "aij" - A matrix type to be used for sparse matrices.
901bebe75SBarry Smith 
1001bebe75SBarry Smith    This matrix type is identical to MATSEQAIJ when constructed with a single process communicator,
1101bebe75SBarry Smith    and MATMPIAIJ otherwise.  As a result, for single process communicators,
1201bebe75SBarry Smith   MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation is supported
1301bebe75SBarry Smith   for communicators controlling multiple processes.  It is recommended that you call both of
1401bebe75SBarry Smith   the above preallocation routines for simplicity.
1501bebe75SBarry Smith 
1601bebe75SBarry Smith    Options Database Keys:
1701bebe75SBarry Smith . -mat_type aij - sets the matrix type to "aij" during a call to MatSetFromOptions()
1801bebe75SBarry Smith 
199ae82921SPaul Mullowney   Developer Notes: Subclasses include MATAIJCUSP, MATAIJCUSPARSE, MATAIJPERM, MATAIJCRL, and also automatically switches over to use inodes when
2001bebe75SBarry Smith    enough exist.
2101bebe75SBarry Smith 
2201bebe75SBarry Smith   Level: beginner
2301bebe75SBarry Smith 
2469b1f4b7SBarry Smith .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ
2501bebe75SBarry Smith M*/
2601bebe75SBarry Smith 
2701bebe75SBarry Smith /*MC
2801bebe75SBarry Smith    MATAIJCRL - MATAIJCRL = "aijcrl" - A matrix type to be used for sparse matrices.
2901bebe75SBarry Smith 
3001bebe75SBarry Smith    This matrix type is identical to MATSEQAIJCRL when constructed with a single process communicator,
3101bebe75SBarry Smith    and MATMPIAIJCRL otherwise.  As a result, for single process communicators,
3201bebe75SBarry Smith    MatSeqAIJSetPreallocation() is supported, and similarly MatMPIAIJSetPreallocation() is supported
3301bebe75SBarry Smith   for communicators controlling multiple processes.  It is recommended that you call both of
3401bebe75SBarry Smith   the above preallocation routines for simplicity.
3501bebe75SBarry Smith 
3601bebe75SBarry Smith    Options Database Keys:
3701bebe75SBarry Smith . -mat_type aijcrl - sets the matrix type to "aijcrl" during a call to MatSetFromOptions()
3801bebe75SBarry Smith 
3901bebe75SBarry Smith   Level: beginner
4001bebe75SBarry Smith 
4101bebe75SBarry Smith .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL
4201bebe75SBarry Smith M*/
4301bebe75SBarry Smith 
44dd6ea824SBarry Smith #undef __FUNCT__
45f2c98031SJed Brown #define __FUNCT__ "MatFindNonzeroRows_MPIAIJ"
46f2c98031SJed Brown PetscErrorCode MatFindNonzeroRows_MPIAIJ(Mat M,IS *keptrows)
4727d4218bSShri Abhyankar {
4827d4218bSShri Abhyankar   PetscErrorCode  ierr;
4927d4218bSShri Abhyankar   Mat_MPIAIJ      *mat = (Mat_MPIAIJ*)M->data;
5027d4218bSShri Abhyankar   Mat_SeqAIJ      *a   = (Mat_SeqAIJ*)mat->A->data;
5127d4218bSShri Abhyankar   Mat_SeqAIJ      *b   = (Mat_SeqAIJ*)mat->B->data;
5227d4218bSShri Abhyankar   const PetscInt  *ia,*ib;
5327d4218bSShri Abhyankar   const MatScalar *aa,*bb;
5427d4218bSShri Abhyankar   PetscInt        na,nb,i,j,*rows,cnt=0,n0rows;
5527d4218bSShri Abhyankar   PetscInt        m = M->rmap->n,rstart = M->rmap->rstart;
5627d4218bSShri Abhyankar 
5727d4218bSShri Abhyankar   PetscFunctionBegin;
5827d4218bSShri Abhyankar   *keptrows = 0;
5927d4218bSShri Abhyankar   ia        = a->i;
6027d4218bSShri Abhyankar   ib        = b->i;
6127d4218bSShri Abhyankar   for (i=0; i<m; i++) {
6227d4218bSShri Abhyankar     na = ia[i+1] - ia[i];
6327d4218bSShri Abhyankar     nb = ib[i+1] - ib[i];
6427d4218bSShri Abhyankar     if (!na && !nb) {
6527d4218bSShri Abhyankar       cnt++;
6627d4218bSShri Abhyankar       goto ok1;
6727d4218bSShri Abhyankar     }
6827d4218bSShri Abhyankar     aa = a->a + ia[i];
6927d4218bSShri Abhyankar     for (j=0; j<na; j++) {
7027d4218bSShri Abhyankar       if (aa[j] != 0.0) goto ok1;
7127d4218bSShri Abhyankar     }
7227d4218bSShri Abhyankar     bb = b->a + ib[i];
7327d4218bSShri Abhyankar     for (j=0; j <nb; j++) {
7427d4218bSShri Abhyankar       if (bb[j] != 0.0) goto ok1;
7527d4218bSShri Abhyankar     }
7627d4218bSShri Abhyankar     cnt++;
7727d4218bSShri Abhyankar ok1:;
7827d4218bSShri Abhyankar   }
79e56f5c9eSBarry Smith   ierr = MPI_Allreduce(&cnt,&n0rows,1,MPIU_INT,MPIU_SUM,PetscObjectComm((PetscObject)M));CHKERRQ(ierr);
8027d4218bSShri Abhyankar   if (!n0rows) PetscFunctionReturn(0);
81785e854fSJed Brown   ierr = PetscMalloc1((M->rmap->n-cnt),&rows);CHKERRQ(ierr);
8227d4218bSShri Abhyankar   cnt  = 0;
8327d4218bSShri Abhyankar   for (i=0; i<m; i++) {
8427d4218bSShri Abhyankar     na = ia[i+1] - ia[i];
8527d4218bSShri Abhyankar     nb = ib[i+1] - ib[i];
8627d4218bSShri Abhyankar     if (!na && !nb) continue;
8727d4218bSShri Abhyankar     aa = a->a + ia[i];
8827d4218bSShri Abhyankar     for (j=0; j<na;j++) {
8927d4218bSShri Abhyankar       if (aa[j] != 0.0) {
9027d4218bSShri Abhyankar         rows[cnt++] = rstart + i;
9127d4218bSShri Abhyankar         goto ok2;
9227d4218bSShri Abhyankar       }
9327d4218bSShri Abhyankar     }
9427d4218bSShri Abhyankar     bb = b->a + ib[i];
9527d4218bSShri Abhyankar     for (j=0; j<nb; j++) {
9627d4218bSShri Abhyankar       if (bb[j] != 0.0) {
9727d4218bSShri Abhyankar         rows[cnt++] = rstart + i;
9827d4218bSShri Abhyankar         goto ok2;
9927d4218bSShri Abhyankar       }
10027d4218bSShri Abhyankar     }
10127d4218bSShri Abhyankar ok2:;
10227d4218bSShri Abhyankar   }
103ce94432eSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject)M),cnt,rows,PETSC_OWN_POINTER,keptrows);CHKERRQ(ierr);
10427d4218bSShri Abhyankar   PetscFunctionReturn(0);
10527d4218bSShri Abhyankar }
10627d4218bSShri Abhyankar 
10727d4218bSShri Abhyankar #undef __FUNCT__
108f1f41ecbSJed Brown #define __FUNCT__ "MatFindZeroDiagonals_MPIAIJ"
109f1f41ecbSJed Brown PetscErrorCode MatFindZeroDiagonals_MPIAIJ(Mat M,IS *zrows)
110f1f41ecbSJed Brown {
111f1f41ecbSJed Brown   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)M->data;
112f1f41ecbSJed Brown   PetscErrorCode ierr;
113f1f41ecbSJed Brown   PetscInt       i,rstart,nrows,*rows;
114f1f41ecbSJed Brown 
115f1f41ecbSJed Brown   PetscFunctionBegin;
1160298fd71SBarry Smith   *zrows = NULL;
117f1f41ecbSJed Brown   ierr   = MatFindZeroDiagonals_SeqAIJ_Private(aij->A,&nrows,&rows);CHKERRQ(ierr);
1180298fd71SBarry Smith   ierr   = MatGetOwnershipRange(M,&rstart,NULL);CHKERRQ(ierr);
119f1f41ecbSJed Brown   for (i=0; i<nrows; i++) rows[i] += rstart;
120ce94432eSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject)M),nrows,rows,PETSC_OWN_POINTER,zrows);CHKERRQ(ierr);
121f1f41ecbSJed Brown   PetscFunctionReturn(0);
122f1f41ecbSJed Brown }
123f1f41ecbSJed Brown 
124f1f41ecbSJed Brown #undef __FUNCT__
1250716a85fSBarry Smith #define __FUNCT__ "MatGetColumnNorms_MPIAIJ"
1260716a85fSBarry Smith PetscErrorCode MatGetColumnNorms_MPIAIJ(Mat A,NormType type,PetscReal *norms)
1270716a85fSBarry Smith {
1280716a85fSBarry Smith   PetscErrorCode ierr;
1290716a85fSBarry Smith   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)A->data;
1300716a85fSBarry Smith   PetscInt       i,n,*garray = aij->garray;
1310716a85fSBarry Smith   Mat_SeqAIJ     *a_aij = (Mat_SeqAIJ*) aij->A->data;
1320716a85fSBarry Smith   Mat_SeqAIJ     *b_aij = (Mat_SeqAIJ*) aij->B->data;
1330716a85fSBarry Smith   PetscReal      *work;
1340716a85fSBarry Smith 
1350716a85fSBarry Smith   PetscFunctionBegin;
1360298fd71SBarry Smith   ierr = MatGetSize(A,NULL,&n);CHKERRQ(ierr);
1371795a4d1SJed Brown   ierr = PetscCalloc1(n,&work);CHKERRQ(ierr);
1380716a85fSBarry Smith   if (type == NORM_2) {
1390716a85fSBarry Smith     for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) {
1400716a85fSBarry Smith       work[A->cmap->rstart + a_aij->j[i]] += PetscAbsScalar(a_aij->a[i]*a_aij->a[i]);
1410716a85fSBarry Smith     }
1420716a85fSBarry Smith     for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) {
1430716a85fSBarry Smith       work[garray[b_aij->j[i]]] += PetscAbsScalar(b_aij->a[i]*b_aij->a[i]);
1440716a85fSBarry Smith     }
1450716a85fSBarry Smith   } else if (type == NORM_1) {
1460716a85fSBarry Smith     for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) {
1470716a85fSBarry Smith       work[A->cmap->rstart + a_aij->j[i]] += PetscAbsScalar(a_aij->a[i]);
1480716a85fSBarry Smith     }
1490716a85fSBarry Smith     for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) {
1500716a85fSBarry Smith       work[garray[b_aij->j[i]]] += PetscAbsScalar(b_aij->a[i]);
1510716a85fSBarry Smith     }
1520716a85fSBarry Smith   } else if (type == NORM_INFINITY) {
1530716a85fSBarry Smith     for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) {
1540716a85fSBarry Smith       work[A->cmap->rstart + a_aij->j[i]] = PetscMax(PetscAbsScalar(a_aij->a[i]), work[A->cmap->rstart + a_aij->j[i]]);
1550716a85fSBarry Smith     }
1560716a85fSBarry Smith     for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) {
1570716a85fSBarry Smith       work[garray[b_aij->j[i]]] = PetscMax(PetscAbsScalar(b_aij->a[i]),work[garray[b_aij->j[i]]]);
1580716a85fSBarry Smith     }
1590716a85fSBarry Smith 
160ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONG,"Unknown NormType");
1610716a85fSBarry Smith   if (type == NORM_INFINITY) {
1627ec4195eSRémi Lacroix     ierr = MPI_Allreduce(work,norms,n,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)A));CHKERRQ(ierr);
1630716a85fSBarry Smith   } else {
1647ec4195eSRémi Lacroix     ierr = MPI_Allreduce(work,norms,n,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)A));CHKERRQ(ierr);
1650716a85fSBarry Smith   }
1660716a85fSBarry Smith   ierr = PetscFree(work);CHKERRQ(ierr);
1670716a85fSBarry Smith   if (type == NORM_2) {
1688f1a2a5eSBarry Smith     for (i=0; i<n; i++) norms[i] = PetscSqrtReal(norms[i]);
1690716a85fSBarry Smith   }
1700716a85fSBarry Smith   PetscFunctionReturn(0);
1710716a85fSBarry Smith }
1720716a85fSBarry Smith 
1730716a85fSBarry Smith #undef __FUNCT__
174dd6ea824SBarry Smith #define __FUNCT__ "MatDistribute_MPIAIJ"
175dd6ea824SBarry Smith /*
176dd6ea824SBarry Smith     Distributes a SeqAIJ matrix across a set of processes. Code stolen from
177dd6ea824SBarry Smith     MatLoad_MPIAIJ(). Horrible lack of reuse. Should be a routine for each matrix type.
178dd6ea824SBarry Smith 
179dd6ea824SBarry Smith     Only for square matrices
180b30237c6SBarry Smith 
181b30237c6SBarry Smith     Used by a preconditioner, hence PETSC_EXTERN
182dd6ea824SBarry Smith */
1835a576424SJed Brown PETSC_EXTERN PetscErrorCode MatDistribute_MPIAIJ(MPI_Comm comm,Mat gmat,PetscInt m,MatReuse reuse,Mat *inmat)
184dd6ea824SBarry Smith {
185dd6ea824SBarry Smith   PetscMPIInt    rank,size;
186d892089bSMatthew G. Knepley   PetscInt       *rowners,*dlens,*olens,i,rstart,rend,j,jj,nz = 0,*gmataj,cnt,row,*ld,bses[2];
187dd6ea824SBarry Smith   PetscErrorCode ierr;
188dd6ea824SBarry Smith   Mat            mat;
189dd6ea824SBarry Smith   Mat_SeqAIJ     *gmata;
190dd6ea824SBarry Smith   PetscMPIInt    tag;
191dd6ea824SBarry Smith   MPI_Status     status;
192ace3abfcSBarry Smith   PetscBool      aij;
193dd6ea824SBarry Smith   MatScalar      *gmataa,*ao,*ad,*gmataarestore=0;
194dd6ea824SBarry Smith 
195dd6ea824SBarry Smith   PetscFunctionBegin;
196dd6ea824SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
197dd6ea824SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
198dd6ea824SBarry Smith   if (!rank) {
199251f4c67SDmitry Karpeev     ierr = PetscObjectTypeCompare((PetscObject)gmat,MATSEQAIJ,&aij);CHKERRQ(ierr);
200ce94432eSBarry Smith     if (!aij) SETERRQ1(PetscObjectComm((PetscObject)gmat),PETSC_ERR_SUP,"Currently no support for input matrix of type %s\n",((PetscObject)gmat)->type_name);
201dd6ea824SBarry Smith   }
202dd6ea824SBarry Smith   if (reuse == MAT_INITIAL_MATRIX) {
203dd6ea824SBarry Smith     ierr = MatCreate(comm,&mat);CHKERRQ(ierr);
204dd6ea824SBarry Smith     ierr = MatSetSizes(mat,m,m,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
20533d57670SJed Brown     ierr = MatGetBlockSizes(gmat,&bses[0],&bses[1]);CHKERRQ(ierr);
206efcf75d5SBarry Smith     ierr = MPI_Bcast(bses,2,MPIU_INT,0,comm);CHKERRQ(ierr);
207efcf75d5SBarry Smith     ierr = MatSetBlockSizes(mat,bses[0],bses[1]);CHKERRQ(ierr);
208dd6ea824SBarry Smith     ierr = MatSetType(mat,MATAIJ);CHKERRQ(ierr);
209785e854fSJed Brown     ierr = PetscMalloc1((size+1),&rowners);CHKERRQ(ierr);
210dcca6d9dSJed Brown     ierr = PetscMalloc2(m,&dlens,m,&olens);CHKERRQ(ierr);
211dd6ea824SBarry Smith     ierr = MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);CHKERRQ(ierr);
2122205254eSKarl Rupp 
213dd6ea824SBarry Smith     rowners[0] = 0;
2142205254eSKarl Rupp     for (i=2; i<=size; i++) rowners[i] += rowners[i-1];
215dd6ea824SBarry Smith     rstart = rowners[rank];
216dd6ea824SBarry Smith     rend   = rowners[rank+1];
217dd6ea824SBarry Smith     ierr   = PetscObjectGetNewTag((PetscObject)mat,&tag);CHKERRQ(ierr);
218dd6ea824SBarry Smith     if (!rank) {
219dd6ea824SBarry Smith       gmata = (Mat_SeqAIJ*) gmat->data;
220dd6ea824SBarry Smith       /* send row lengths to all processors */
221dd6ea824SBarry Smith       for (i=0; i<m; i++) dlens[i] = gmata->ilen[i];
222dd6ea824SBarry Smith       for (i=1; i<size; i++) {
223dd6ea824SBarry Smith         ierr = MPI_Send(gmata->ilen + rowners[i],rowners[i+1]-rowners[i],MPIU_INT,i,tag,comm);CHKERRQ(ierr);
224dd6ea824SBarry Smith       }
225dd6ea824SBarry Smith       /* determine number diagonal and off-diagonal counts */
226dd6ea824SBarry Smith       ierr = PetscMemzero(olens,m*sizeof(PetscInt));CHKERRQ(ierr);
2271795a4d1SJed Brown       ierr = PetscCalloc1(m,&ld);CHKERRQ(ierr);
228dd6ea824SBarry Smith       jj   = 0;
229dd6ea824SBarry Smith       for (i=0; i<m; i++) {
230dd6ea824SBarry Smith         for (j=0; j<dlens[i]; j++) {
231dd6ea824SBarry Smith           if (gmata->j[jj] < rstart) ld[i]++;
232dd6ea824SBarry Smith           if (gmata->j[jj] < rstart || gmata->j[jj] >= rend) olens[i]++;
233dd6ea824SBarry Smith           jj++;
234dd6ea824SBarry Smith         }
235dd6ea824SBarry Smith       }
236dd6ea824SBarry Smith       /* send column indices to other processes */
237dd6ea824SBarry Smith       for (i=1; i<size; i++) {
238dd6ea824SBarry Smith         nz   = gmata->i[rowners[i+1]]-gmata->i[rowners[i]];
239dd6ea824SBarry Smith         ierr = MPI_Send(&nz,1,MPIU_INT,i,tag,comm);CHKERRQ(ierr);
240dd6ea824SBarry Smith         ierr = MPI_Send(gmata->j + gmata->i[rowners[i]],nz,MPIU_INT,i,tag,comm);CHKERRQ(ierr);
241dd6ea824SBarry Smith       }
242dd6ea824SBarry Smith 
243dd6ea824SBarry Smith       /* send numerical values to other processes */
244dd6ea824SBarry Smith       for (i=1; i<size; i++) {
245dd6ea824SBarry Smith         nz   = gmata->i[rowners[i+1]]-gmata->i[rowners[i]];
246dd6ea824SBarry Smith         ierr = MPI_Send(gmata->a + gmata->i[rowners[i]],nz,MPIU_SCALAR,i,tag,comm);CHKERRQ(ierr);
247dd6ea824SBarry Smith       }
248dd6ea824SBarry Smith       gmataa = gmata->a;
249dd6ea824SBarry Smith       gmataj = gmata->j;
250dd6ea824SBarry Smith 
251dd6ea824SBarry Smith     } else {
252dd6ea824SBarry Smith       /* receive row lengths */
253dd6ea824SBarry Smith       ierr = MPI_Recv(dlens,m,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr);
254dd6ea824SBarry Smith       /* receive column indices */
255dd6ea824SBarry Smith       ierr = MPI_Recv(&nz,1,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr);
256dcca6d9dSJed Brown       ierr = PetscMalloc2(nz,&gmataa,nz,&gmataj);CHKERRQ(ierr);
257dd6ea824SBarry Smith       ierr = MPI_Recv(gmataj,nz,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr);
258dd6ea824SBarry Smith       /* determine number diagonal and off-diagonal counts */
259dd6ea824SBarry Smith       ierr = PetscMemzero(olens,m*sizeof(PetscInt));CHKERRQ(ierr);
2601795a4d1SJed Brown       ierr = PetscCalloc1(m,&ld);CHKERRQ(ierr);
261dd6ea824SBarry Smith       jj   = 0;
262dd6ea824SBarry Smith       for (i=0; i<m; i++) {
263dd6ea824SBarry Smith         for (j=0; j<dlens[i]; j++) {
264dd6ea824SBarry Smith           if (gmataj[jj] < rstart) ld[i]++;
265dd6ea824SBarry Smith           if (gmataj[jj] < rstart || gmataj[jj] >= rend) olens[i]++;
266dd6ea824SBarry Smith           jj++;
267dd6ea824SBarry Smith         }
268dd6ea824SBarry Smith       }
269dd6ea824SBarry Smith       /* receive numerical values */
270dd6ea824SBarry Smith       ierr = PetscMemzero(gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr);
271dd6ea824SBarry Smith       ierr = MPI_Recv(gmataa,nz,MPIU_SCALAR,0,tag,comm,&status);CHKERRQ(ierr);
272dd6ea824SBarry Smith     }
273dd6ea824SBarry Smith     /* set preallocation */
274dd6ea824SBarry Smith     for (i=0; i<m; i++) {
275dd6ea824SBarry Smith       dlens[i] -= olens[i];
276dd6ea824SBarry Smith     }
277dd6ea824SBarry Smith     ierr = MatSeqAIJSetPreallocation(mat,0,dlens);CHKERRQ(ierr);
278dd6ea824SBarry Smith     ierr = MatMPIAIJSetPreallocation(mat,0,dlens,0,olens);CHKERRQ(ierr);
279dd6ea824SBarry Smith 
280dd6ea824SBarry Smith     for (i=0; i<m; i++) {
281dd6ea824SBarry Smith       dlens[i] += olens[i];
282dd6ea824SBarry Smith     }
283dd6ea824SBarry Smith     cnt = 0;
284dd6ea824SBarry Smith     for (i=0; i<m; i++) {
285dd6ea824SBarry Smith       row  = rstart + i;
286dd6ea824SBarry Smith       ierr = MatSetValues(mat,1,&row,dlens[i],gmataj+cnt,gmataa+cnt,INSERT_VALUES);CHKERRQ(ierr);
287dd6ea824SBarry Smith       cnt += dlens[i];
288dd6ea824SBarry Smith     }
289dd6ea824SBarry Smith     if (rank) {
290dd6ea824SBarry Smith       ierr = PetscFree2(gmataa,gmataj);CHKERRQ(ierr);
291dd6ea824SBarry Smith     }
292dd6ea824SBarry Smith     ierr = PetscFree2(dlens,olens);CHKERRQ(ierr);
293dd6ea824SBarry Smith     ierr = PetscFree(rowners);CHKERRQ(ierr);
2942205254eSKarl Rupp 
295dd6ea824SBarry Smith     ((Mat_MPIAIJ*)(mat->data))->ld = ld;
2962205254eSKarl Rupp 
297dd6ea824SBarry Smith     *inmat = mat;
298dd6ea824SBarry Smith   } else {   /* column indices are already set; only need to move over numerical values from process 0 */
299dd6ea824SBarry Smith     Mat_SeqAIJ *Ad = (Mat_SeqAIJ*)((Mat_MPIAIJ*)((*inmat)->data))->A->data;
300dd6ea824SBarry Smith     Mat_SeqAIJ *Ao = (Mat_SeqAIJ*)((Mat_MPIAIJ*)((*inmat)->data))->B->data;
301dd6ea824SBarry Smith     mat  = *inmat;
302dd6ea824SBarry Smith     ierr = PetscObjectGetNewTag((PetscObject)mat,&tag);CHKERRQ(ierr);
303dd6ea824SBarry Smith     if (!rank) {
304dd6ea824SBarry Smith       /* send numerical values to other processes */
305dd6ea824SBarry Smith       gmata  = (Mat_SeqAIJ*) gmat->data;
306dd6ea824SBarry Smith       ierr   = MatGetOwnershipRanges(mat,(const PetscInt**)&rowners);CHKERRQ(ierr);
307dd6ea824SBarry Smith       gmataa = gmata->a;
308dd6ea824SBarry Smith       for (i=1; i<size; i++) {
309dd6ea824SBarry Smith         nz   = gmata->i[rowners[i+1]]-gmata->i[rowners[i]];
310dd6ea824SBarry Smith         ierr = MPI_Send(gmataa + gmata->i[rowners[i]],nz,MPIU_SCALAR,i,tag,comm);CHKERRQ(ierr);
311dd6ea824SBarry Smith       }
312dd6ea824SBarry Smith       nz = gmata->i[rowners[1]]-gmata->i[rowners[0]];
313dd6ea824SBarry Smith     } else {
314dd6ea824SBarry Smith       /* receive numerical values from process 0*/
315dd6ea824SBarry Smith       nz   = Ad->nz + Ao->nz;
316785e854fSJed Brown       ierr = PetscMalloc1(nz,&gmataa);CHKERRQ(ierr); gmataarestore = gmataa;
317dd6ea824SBarry Smith       ierr = MPI_Recv(gmataa,nz,MPIU_SCALAR,0,tag,comm,&status);CHKERRQ(ierr);
318dd6ea824SBarry Smith     }
319dd6ea824SBarry Smith     /* transfer numerical values into the diagonal A and off diagonal B parts of mat */
320dd6ea824SBarry Smith     ld = ((Mat_MPIAIJ*)(mat->data))->ld;
321dd6ea824SBarry Smith     ad = Ad->a;
322dd6ea824SBarry Smith     ao = Ao->a;
323d0f46423SBarry Smith     if (mat->rmap->n) {
324dd6ea824SBarry Smith       i  = 0;
325dd6ea824SBarry Smith       nz = ld[i];                                   ierr = PetscMemcpy(ao,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); ao += nz; gmataa += nz;
326dd6ea824SBarry Smith       nz = Ad->i[i+1] - Ad->i[i];                   ierr = PetscMemcpy(ad,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); ad += nz; gmataa += nz;
327dd6ea824SBarry Smith     }
328d0f46423SBarry Smith     for (i=1; i<mat->rmap->n; i++) {
329dd6ea824SBarry 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;
330dd6ea824SBarry Smith       nz = Ad->i[i+1] - Ad->i[i];                   ierr = PetscMemcpy(ad,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); ad += nz; gmataa += nz;
331dd6ea824SBarry Smith     }
332dd6ea824SBarry Smith     i--;
333d0f46423SBarry Smith     if (mat->rmap->n) {
33422d28d08SBarry Smith       nz = Ao->i[i+1] - Ao->i[i] - ld[i];           ierr = PetscMemcpy(ao,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr);
335dd6ea824SBarry Smith     }
336dd6ea824SBarry Smith     if (rank) {
337dd6ea824SBarry Smith       ierr = PetscFree(gmataarestore);CHKERRQ(ierr);
338dd6ea824SBarry Smith     }
339dd6ea824SBarry Smith   }
340dd6ea824SBarry Smith   ierr = MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
341dd6ea824SBarry Smith   ierr = MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
342dd6ea824SBarry Smith   PetscFunctionReturn(0);
343dd6ea824SBarry Smith }
344dd6ea824SBarry Smith 
3450f5bd95cSBarry Smith /*
3460f5bd95cSBarry Smith   Local utility routine that creates a mapping from the global column
3479e25ed09SBarry Smith number to the local number in the off-diagonal part of the local
3480f5bd95cSBarry Smith storage of the matrix.  When PETSC_USE_CTABLE is used this is scalable at
3490f5bd95cSBarry Smith a slightly higher hash table cost; without it it is not scalable (each processor
3500f5bd95cSBarry Smith has an order N integer array but is fast to acess.
3519e25ed09SBarry Smith */
3524a2ae208SSatish Balay #undef __FUNCT__
353ab9863d7SBarry Smith #define __FUNCT__ "MatCreateColmap_MPIAIJ_Private"
354ab9863d7SBarry Smith PetscErrorCode MatCreateColmap_MPIAIJ_Private(Mat mat)
3559e25ed09SBarry Smith {
35644a69424SLois Curfman McInnes   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
3576849ba73SBarry Smith   PetscErrorCode ierr;
358d0f46423SBarry Smith   PetscInt       n = aij->B->cmap->n,i;
359dbb450caSBarry Smith 
3603a40ed3dSBarry Smith   PetscFunctionBegin;
3615e1f6667SBarry Smith   if (!aij->garray) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"MPIAIJ Matrix was assembled but is missing garray");
362aa482453SBarry Smith #if defined(PETSC_USE_CTABLE)
363e23dfa41SBarry Smith   ierr = PetscTableCreate(n,mat->cmap->N+1,&aij->colmap);CHKERRQ(ierr);
364b1fc9764SSatish Balay   for (i=0; i<n; i++) {
3653861aac3SJed Brown     ierr = PetscTableAdd(aij->colmap,aij->garray[i]+1,i+1,INSERT_VALUES);CHKERRQ(ierr);
366b1fc9764SSatish Balay   }
367b1fc9764SSatish Balay #else
3681795a4d1SJed Brown   ierr = PetscCalloc1((mat->cmap->N+1),&aij->colmap);CHKERRQ(ierr);
3691795a4d1SJed Brown   ierr = PetscLogObjectMemory((PetscObject)mat,(mat->cmap->N+1)*sizeof(PetscInt));CHKERRQ(ierr);
370905e6a2fSBarry Smith   for (i=0; i<n; i++) aij->colmap[aij->garray[i]] = i+1;
371b1fc9764SSatish Balay #endif
3723a40ed3dSBarry Smith   PetscFunctionReturn(0);
3739e25ed09SBarry Smith }
3749e25ed09SBarry Smith 
37530770e4dSSatish Balay #define MatSetValues_SeqAIJ_A_Private(row,col,value,addv) \
3760520107fSSatish Balay { \
377db4deed7SKarl Rupp     if (col <= lastcol1)  low1 = 0;     \
378db4deed7SKarl Rupp     else                 high1 = nrow1; \
379fd3458f5SBarry Smith     lastcol1 = col;\
380fd3458f5SBarry Smith     while (high1-low1 > 5) { \
381fd3458f5SBarry Smith       t = (low1+high1)/2; \
382fd3458f5SBarry Smith       if (rp1[t] > col) high1 = t; \
383fd3458f5SBarry Smith       else              low1  = t; \
384ba4e3ef2SSatish Balay     } \
385fd3458f5SBarry Smith       for (_i=low1; _i<high1; _i++) { \
386fd3458f5SBarry Smith         if (rp1[_i] > col) break; \
387fd3458f5SBarry Smith         if (rp1[_i] == col) { \
388fd3458f5SBarry Smith           if (addv == ADD_VALUES) ap1[_i] += value;   \
389fd3458f5SBarry Smith           else                    ap1[_i] = value; \
39030770e4dSSatish Balay           goto a_noinsert; \
3910520107fSSatish Balay         } \
3920520107fSSatish Balay       }  \
393e44c0bd4SBarry Smith       if (value == 0.0 && ignorezeroentries) {low1 = 0; high1 = nrow1;goto a_noinsert;} \
394e44c0bd4SBarry Smith       if (nonew == 1) {low1 = 0; high1 = nrow1; goto a_noinsert;}                \
395e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \
396fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,am,1,nrow1,row,col,rmax1,aa,ai,aj,rp1,ap1,aimax,nonew,MatScalar); \
397669a8dbcSSatish Balay       N = nrow1++ - 1; a->nz++; high1++; \
3980520107fSSatish Balay       /* shift up all the later entries in this row */ \
3990520107fSSatish Balay       for (ii=N; ii>=_i; ii--) { \
400fd3458f5SBarry Smith         rp1[ii+1] = rp1[ii]; \
401fd3458f5SBarry Smith         ap1[ii+1] = ap1[ii]; \
4020520107fSSatish Balay       } \
403fd3458f5SBarry Smith       rp1[_i] = col;  \
404fd3458f5SBarry Smith       ap1[_i] = value;  \
405e56f5c9eSBarry Smith       A->nonzerostate++;\
40630770e4dSSatish Balay       a_noinsert: ; \
407fd3458f5SBarry Smith       ailen[row] = nrow1; \
4080520107fSSatish Balay }
4090a198c4cSBarry Smith 
410085a36d4SBarry Smith 
41130770e4dSSatish Balay #define MatSetValues_SeqAIJ_B_Private(row,col,value,addv) \
41230770e4dSSatish Balay   { \
413db4deed7SKarl Rupp     if (col <= lastcol2) low2 = 0;                        \
414db4deed7SKarl Rupp     else high2 = nrow2;                                   \
415fd3458f5SBarry Smith     lastcol2 = col;                                       \
416fd3458f5SBarry Smith     while (high2-low2 > 5) {                              \
417fd3458f5SBarry Smith       t = (low2+high2)/2;                                 \
418fd3458f5SBarry Smith       if (rp2[t] > col) high2 = t;                        \
419fd3458f5SBarry Smith       else             low2  = t;                         \
420ba4e3ef2SSatish Balay     }                                                     \
421fd3458f5SBarry Smith     for (_i=low2; _i<high2; _i++) {                       \
422fd3458f5SBarry Smith       if (rp2[_i] > col) break;                           \
423fd3458f5SBarry Smith       if (rp2[_i] == col) {                               \
424fd3458f5SBarry Smith         if (addv == ADD_VALUES) ap2[_i] += value;         \
425fd3458f5SBarry Smith         else                    ap2[_i] = value;          \
42630770e4dSSatish Balay         goto b_noinsert;                                  \
42730770e4dSSatish Balay       }                                                   \
42830770e4dSSatish Balay     }                                                     \
429e44c0bd4SBarry Smith     if (value == 0.0 && ignorezeroentries) {low2 = 0; high2 = nrow2; goto b_noinsert;} \
430e44c0bd4SBarry Smith     if (nonew == 1) {low2 = 0; high2 = nrow2; goto b_noinsert;}                        \
431e32f2f54SBarry Smith     if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \
432fef13f97SBarry Smith     MatSeqXAIJReallocateAIJ(B,bm,1,nrow2,row,col,rmax2,ba,bi,bj,rp2,ap2,bimax,nonew,MatScalar); \
433669a8dbcSSatish Balay     N = nrow2++ - 1; b->nz++; high2++;                    \
43430770e4dSSatish Balay     /* shift up all the later entries in this row */      \
43530770e4dSSatish Balay     for (ii=N; ii>=_i; ii--) {                            \
436fd3458f5SBarry Smith       rp2[ii+1] = rp2[ii];                                \
437fd3458f5SBarry Smith       ap2[ii+1] = ap2[ii];                                \
43830770e4dSSatish Balay     }                                                     \
439fd3458f5SBarry Smith     rp2[_i] = col;                                        \
440fd3458f5SBarry Smith     ap2[_i] = value;                                      \
441e56f5c9eSBarry Smith     B->nonzerostate++;                                    \
44230770e4dSSatish Balay     b_noinsert: ;                                         \
443fd3458f5SBarry Smith     bilen[row] = nrow2;                                   \
44430770e4dSSatish Balay   }
44530770e4dSSatish Balay 
4464a2ae208SSatish Balay #undef __FUNCT__
4472fd7e33dSBarry Smith #define __FUNCT__ "MatSetValuesRow_MPIAIJ"
4482fd7e33dSBarry Smith PetscErrorCode MatSetValuesRow_MPIAIJ(Mat A,PetscInt row,const PetscScalar v[])
4492fd7e33dSBarry Smith {
4502fd7e33dSBarry Smith   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)A->data;
4512fd7e33dSBarry Smith   Mat_SeqAIJ     *a   = (Mat_SeqAIJ*)mat->A->data,*b = (Mat_SeqAIJ*)mat->B->data;
4522fd7e33dSBarry Smith   PetscErrorCode ierr;
4532fd7e33dSBarry Smith   PetscInt       l,*garray = mat->garray,diag;
4542fd7e33dSBarry Smith 
4552fd7e33dSBarry Smith   PetscFunctionBegin;
4562fd7e33dSBarry Smith   /* code only works for square matrices A */
4572fd7e33dSBarry Smith 
4582fd7e33dSBarry Smith   /* find size of row to the left of the diagonal part */
4592fd7e33dSBarry Smith   ierr = MatGetOwnershipRange(A,&diag,0);CHKERRQ(ierr);
4602fd7e33dSBarry Smith   row  = row - diag;
4612fd7e33dSBarry Smith   for (l=0; l<b->i[row+1]-b->i[row]; l++) {
4622fd7e33dSBarry Smith     if (garray[b->j[b->i[row]+l]] > diag) break;
4632fd7e33dSBarry Smith   }
4642fd7e33dSBarry Smith   ierr = PetscMemcpy(b->a+b->i[row],v,l*sizeof(PetscScalar));CHKERRQ(ierr);
4652fd7e33dSBarry Smith 
4662fd7e33dSBarry Smith   /* diagonal part */
4672fd7e33dSBarry Smith   ierr = PetscMemcpy(a->a+a->i[row],v+l,(a->i[row+1]-a->i[row])*sizeof(PetscScalar));CHKERRQ(ierr);
4682fd7e33dSBarry Smith 
4692fd7e33dSBarry Smith   /* right of diagonal part */
4702fd7e33dSBarry 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);
4712fd7e33dSBarry Smith   PetscFunctionReturn(0);
4722fd7e33dSBarry Smith }
4732fd7e33dSBarry Smith 
4742fd7e33dSBarry Smith #undef __FUNCT__
4754a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_MPIAIJ"
476b1d57f15SBarry Smith PetscErrorCode MatSetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
4778a729477SBarry Smith {
47844a69424SLois Curfman McInnes   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
47987828ca2SBarry Smith   PetscScalar    value;
480dfbe8321SBarry Smith   PetscErrorCode ierr;
481d0f46423SBarry Smith   PetscInt       i,j,rstart  = mat->rmap->rstart,rend = mat->rmap->rend;
482d0f46423SBarry Smith   PetscInt       cstart      = mat->cmap->rstart,cend = mat->cmap->rend,row,col;
483ace3abfcSBarry Smith   PetscBool      roworiented = aij->roworiented;
4848a729477SBarry Smith 
4850520107fSSatish Balay   /* Some Variables required in the macro */
4864ee7247eSSatish Balay   Mat        A                 = aij->A;
4874ee7247eSSatish Balay   Mat_SeqAIJ *a                = (Mat_SeqAIJ*)A->data;
48857809a77SBarry Smith   PetscInt   *aimax            = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j;
489a77337e4SBarry Smith   MatScalar  *aa               = a->a;
490ace3abfcSBarry Smith   PetscBool  ignorezeroentries = a->ignorezeroentries;
49130770e4dSSatish Balay   Mat        B                 = aij->B;
49230770e4dSSatish Balay   Mat_SeqAIJ *b                = (Mat_SeqAIJ*)B->data;
493d0f46423SBarry Smith   PetscInt   *bimax            = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap->n,am = aij->A->rmap->n;
494a77337e4SBarry Smith   MatScalar  *ba               = b->a;
49530770e4dSSatish Balay 
496fd3458f5SBarry Smith   PetscInt  *rp1,*rp2,ii,nrow1,nrow2,_i,rmax1,rmax2,N,low1,high1,low2,high2,t,lastcol1,lastcol2;
4978d76821aSHong Zhang   PetscInt  nonew;
498a77337e4SBarry Smith   MatScalar *ap1,*ap2;
4994ee7247eSSatish Balay 
5003a40ed3dSBarry Smith   PetscFunctionBegin;
5018a729477SBarry Smith   for (i=0; i<m; i++) {
5025ef9f2a5SBarry Smith     if (im[i] < 0) continue;
5032515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
504e32f2f54SBarry 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);
5050a198c4cSBarry Smith #endif
5064b0e389bSBarry Smith     if (im[i] >= rstart && im[i] < rend) {
5074b0e389bSBarry Smith       row      = im[i] - rstart;
508fd3458f5SBarry Smith       lastcol1 = -1;
509fd3458f5SBarry Smith       rp1      = aj + ai[row];
510fd3458f5SBarry Smith       ap1      = aa + ai[row];
511fd3458f5SBarry Smith       rmax1    = aimax[row];
512fd3458f5SBarry Smith       nrow1    = ailen[row];
513fd3458f5SBarry Smith       low1     = 0;
514fd3458f5SBarry Smith       high1    = nrow1;
515fd3458f5SBarry Smith       lastcol2 = -1;
516fd3458f5SBarry Smith       rp2      = bj + bi[row];
517d498b1e9SBarry Smith       ap2      = ba + bi[row];
518fd3458f5SBarry Smith       rmax2    = bimax[row];
519d498b1e9SBarry Smith       nrow2    = bilen[row];
520fd3458f5SBarry Smith       low2     = 0;
521fd3458f5SBarry Smith       high2    = nrow2;
522fd3458f5SBarry Smith 
5231eb62cbbSBarry Smith       for (j=0; j<n; j++) {
524db4deed7SKarl Rupp         if (roworiented) value = v[i*n+j];
525db4deed7SKarl Rupp         else             value = v[i+j*m];
526abc0a331SBarry Smith         if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES)) continue;
527fd3458f5SBarry Smith         if (in[j] >= cstart && in[j] < cend) {
528fd3458f5SBarry Smith           col   = in[j] - cstart;
5298d76821aSHong Zhang           nonew = a->nonew;
53030770e4dSSatish Balay           MatSetValues_SeqAIJ_A_Private(row,col,value,addv);
531273d9f13SBarry Smith         } else if (in[j] < 0) continue;
5322515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
533cb9801acSJed 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);
5340a198c4cSBarry Smith #endif
5351eb62cbbSBarry Smith         else {
536227d817aSBarry Smith           if (mat->was_assembled) {
537905e6a2fSBarry Smith             if (!aij->colmap) {
538ab9863d7SBarry Smith               ierr = MatCreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
539905e6a2fSBarry Smith             }
540aa482453SBarry Smith #if defined(PETSC_USE_CTABLE)
5410f5bd95cSBarry Smith             ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr);
542fa46199cSSatish Balay             col--;
543b1fc9764SSatish Balay #else
544905e6a2fSBarry Smith             col = aij->colmap[in[j]] - 1;
545b1fc9764SSatish Balay #endif
5460e9bae81SBarry Smith             if (col < 0 && !((Mat_SeqAIJ*)(aij->B->data))->nonew) {
547ab9863d7SBarry Smith               ierr = MatDisAssemble_MPIAIJ(mat);CHKERRQ(ierr);
5484b0e389bSBarry Smith               col  =  in[j];
5499bf004c3SSatish Balay               /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
550f9508a3cSSatish Balay               B     = aij->B;
551f9508a3cSSatish Balay               b     = (Mat_SeqAIJ*)B->data;
552e44c0bd4SBarry Smith               bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j; ba = b->a;
553d498b1e9SBarry Smith               rp2   = bj + bi[row];
554d498b1e9SBarry Smith               ap2   = ba + bi[row];
555d498b1e9SBarry Smith               rmax2 = bimax[row];
556d498b1e9SBarry Smith               nrow2 = bilen[row];
557d498b1e9SBarry Smith               low2  = 0;
558d498b1e9SBarry Smith               high2 = nrow2;
559d0f46423SBarry Smith               bm    = aij->B->rmap->n;
560f9508a3cSSatish Balay               ba    = b->a;
5610e9bae81SBarry Smith             } else if (col < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", im[i], in[j]);
562c48de900SBarry Smith           } else col = in[j];
5638d76821aSHong Zhang           nonew = b->nonew;
56430770e4dSSatish Balay           MatSetValues_SeqAIJ_B_Private(row,col,value,addv);
5651eb62cbbSBarry Smith         }
5661eb62cbbSBarry Smith       }
5675ef9f2a5SBarry Smith     } else {
5684cb17eb5SBarry 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]);
56990f02eecSBarry Smith       if (!aij->donotstash) {
5705080c13bSMatthew G Knepley         mat->assembled = PETSC_FALSE;
571d36fbae8SSatish Balay         if (roworiented) {
572ace3abfcSBarry Smith           ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr);
573d36fbae8SSatish Balay         } else {
574ace3abfcSBarry Smith           ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr);
5754b0e389bSBarry Smith         }
5761eb62cbbSBarry Smith       }
5778a729477SBarry Smith     }
57890f02eecSBarry Smith   }
5793a40ed3dSBarry Smith   PetscFunctionReturn(0);
5808a729477SBarry Smith }
5818a729477SBarry Smith 
5824a2ae208SSatish Balay #undef __FUNCT__
5834a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_MPIAIJ"
584b1d57f15SBarry Smith PetscErrorCode MatGetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[])
585b49de8d1SLois Curfman McInnes {
586b49de8d1SLois Curfman McInnes   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
587dfbe8321SBarry Smith   PetscErrorCode ierr;
588d0f46423SBarry Smith   PetscInt       i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend;
589d0f46423SBarry Smith   PetscInt       cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col;
590b49de8d1SLois Curfman McInnes 
5913a40ed3dSBarry Smith   PetscFunctionBegin;
592b49de8d1SLois Curfman McInnes   for (i=0; i<m; i++) {
593e32f2f54SBarry Smith     if (idxm[i] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",idxm[i]);*/
594e32f2f54SBarry 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);
595b49de8d1SLois Curfman McInnes     if (idxm[i] >= rstart && idxm[i] < rend) {
596b49de8d1SLois Curfman McInnes       row = idxm[i] - rstart;
597b49de8d1SLois Curfman McInnes       for (j=0; j<n; j++) {
598e32f2f54SBarry Smith         if (idxn[j] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",idxn[j]); */
599e32f2f54SBarry 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);
600b49de8d1SLois Curfman McInnes         if (idxn[j] >= cstart && idxn[j] < cend) {
601b49de8d1SLois Curfman McInnes           col  = idxn[j] - cstart;
602b49de8d1SLois Curfman McInnes           ierr = MatGetValues(aij->A,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
603fa852ad4SSatish Balay         } else {
604905e6a2fSBarry Smith           if (!aij->colmap) {
605ab9863d7SBarry Smith             ierr = MatCreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
606905e6a2fSBarry Smith           }
607aa482453SBarry Smith #if defined(PETSC_USE_CTABLE)
6080f5bd95cSBarry Smith           ierr = PetscTableFind(aij->colmap,idxn[j]+1,&col);CHKERRQ(ierr);
609fa46199cSSatish Balay           col--;
610b1fc9764SSatish Balay #else
611905e6a2fSBarry Smith           col = aij->colmap[idxn[j]] - 1;
612b1fc9764SSatish Balay #endif
613e60e1c95SSatish Balay           if ((col < 0) || (aij->garray[col] != idxn[j])) *(v+i*n+j) = 0.0;
614d9d09a02SSatish Balay           else {
615b49de8d1SLois Curfman McInnes             ierr = MatGetValues(aij->B,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
616b49de8d1SLois Curfman McInnes           }
617b49de8d1SLois Curfman McInnes         }
618b49de8d1SLois Curfman McInnes       }
619f23aa3ddSBarry Smith     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local values currently supported");
620b49de8d1SLois Curfman McInnes   }
6213a40ed3dSBarry Smith   PetscFunctionReturn(0);
622b49de8d1SLois Curfman McInnes }
623bc5ccf88SSatish Balay 
624bd0c2dcbSBarry Smith extern PetscErrorCode MatMultDiagonalBlock_MPIAIJ(Mat,Vec,Vec);
625bd0c2dcbSBarry Smith 
6264a2ae208SSatish Balay #undef __FUNCT__
6274a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyBegin_MPIAIJ"
628dfbe8321SBarry Smith PetscErrorCode MatAssemblyBegin_MPIAIJ(Mat mat,MatAssemblyType mode)
629bc5ccf88SSatish Balay {
630bc5ccf88SSatish Balay   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
631dfbe8321SBarry Smith   PetscErrorCode ierr;
632b1d57f15SBarry Smith   PetscInt       nstash,reallocs;
633bc5ccf88SSatish Balay 
634bc5ccf88SSatish Balay   PetscFunctionBegin;
6352205254eSKarl Rupp   if (aij->donotstash || mat->nooffprocentries) PetscFunctionReturn(0);
636bc5ccf88SSatish Balay 
637d0f46423SBarry Smith   ierr = MatStashScatterBegin_Private(mat,&mat->stash,mat->rmap->range);CHKERRQ(ierr);
6388798bf22SSatish Balay   ierr = MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);CHKERRQ(ierr);
639ae15b995SBarry Smith   ierr = PetscInfo2(aij->A,"Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);CHKERRQ(ierr);
640bc5ccf88SSatish Balay   PetscFunctionReturn(0);
641bc5ccf88SSatish Balay }
642bc5ccf88SSatish Balay 
6434a2ae208SSatish Balay #undef __FUNCT__
6444a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_MPIAIJ"
645dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_MPIAIJ(Mat mat,MatAssemblyType mode)
646bc5ccf88SSatish Balay {
647bc5ccf88SSatish Balay   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
64891c97fd4SSatish Balay   Mat_SeqAIJ     *a   = (Mat_SeqAIJ*)aij->A->data;
6496849ba73SBarry Smith   PetscErrorCode ierr;
650b1d57f15SBarry Smith   PetscMPIInt    n;
651b1d57f15SBarry Smith   PetscInt       i,j,rstart,ncols,flg;
652e44c0bd4SBarry Smith   PetscInt       *row,*col;
653ace3abfcSBarry Smith   PetscBool      other_disassembled;
65487828ca2SBarry Smith   PetscScalar    *val;
655bc5ccf88SSatish Balay 
65691c97fd4SSatish Balay   /* do not use 'b = (Mat_SeqAIJ*)aij->B->data' as B can be reset in disassembly */
6576e111a19SKarl Rupp 
658bc5ccf88SSatish Balay   PetscFunctionBegin;
6594cb17eb5SBarry Smith   if (!aij->donotstash && !mat->nooffprocentries) {
660a2d1c673SSatish Balay     while (1) {
6618798bf22SSatish Balay       ierr = MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);CHKERRQ(ierr);
662a2d1c673SSatish Balay       if (!flg) break;
663a2d1c673SSatish Balay 
664bc5ccf88SSatish Balay       for (i=0; i<n; ) {
665bc5ccf88SSatish Balay         /* Now identify the consecutive vals belonging to the same row */
6662205254eSKarl Rupp         for (j=i,rstart=row[j]; j<n; j++) {
6672205254eSKarl Rupp           if (row[j] != rstart) break;
6682205254eSKarl Rupp         }
669bc5ccf88SSatish Balay         if (j < n) ncols = j-i;
670bc5ccf88SSatish Balay         else       ncols = n-i;
671bc5ccf88SSatish Balay         /* Now assemble all these values with a single function call */
672*4b4eb8d3SJed Brown         ierr = MatSetValues_MPIAIJ(mat,1,row+i,ncols,col+i,val+i,mat->insertmode);CHKERRQ(ierr);
6732205254eSKarl Rupp 
674bc5ccf88SSatish Balay         i = j;
675bc5ccf88SSatish Balay       }
676bc5ccf88SSatish Balay     }
6778798bf22SSatish Balay     ierr = MatStashScatterEnd_Private(&mat->stash);CHKERRQ(ierr);
678bc5ccf88SSatish Balay   }
679bc5ccf88SSatish Balay   ierr = MatAssemblyBegin(aij->A,mode);CHKERRQ(ierr);
680bc5ccf88SSatish Balay   ierr = MatAssemblyEnd(aij->A,mode);CHKERRQ(ierr);
681bc5ccf88SSatish Balay 
682bc5ccf88SSatish Balay   /* determine if any processor has disassembled, if so we must
683bc5ccf88SSatish Balay      also disassemble ourselfs, in order that we may reassemble. */
684bc5ccf88SSatish Balay   /*
685bc5ccf88SSatish Balay      if nonzero structure of submatrix B cannot change then we know that
686bc5ccf88SSatish Balay      no processor disassembled thus we can skip this stuff
687bc5ccf88SSatish Balay   */
688bc5ccf88SSatish Balay   if (!((Mat_SeqAIJ*)aij->B->data)->nonew) {
689ce94432eSBarry Smith     ierr = MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPIU_BOOL,MPI_PROD,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
690bc5ccf88SSatish Balay     if (mat->was_assembled && !other_disassembled) {
691ab9863d7SBarry Smith       ierr = MatDisAssemble_MPIAIJ(mat);CHKERRQ(ierr);
692ad59fb31SSatish Balay     }
693ad59fb31SSatish Balay   }
694bc5ccf88SSatish Balay   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
695bc5ccf88SSatish Balay     ierr = MatSetUpMultiply_MPIAIJ(mat);CHKERRQ(ierr);
696bc5ccf88SSatish Balay   }
6974e0d8c25SBarry Smith   ierr = MatSetOption(aij->B,MAT_USE_INODES,PETSC_FALSE);CHKERRQ(ierr);
698bc5ccf88SSatish Balay   ierr = MatAssemblyBegin(aij->B,mode);CHKERRQ(ierr);
699bc5ccf88SSatish Balay   ierr = MatAssemblyEnd(aij->B,mode);CHKERRQ(ierr);
700bc5ccf88SSatish Balay 
7011d79065fSBarry Smith   ierr = PetscFree2(aij->rowvalues,aij->rowindices);CHKERRQ(ierr);
7022205254eSKarl Rupp 
703606d414cSSatish Balay   aij->rowvalues = 0;
704a30b2313SHong Zhang 
705a30b2313SHong Zhang   /* used by MatAXPY() */
70691c97fd4SSatish Balay   a->xtoy = 0; ((Mat_SeqAIJ*)aij->B->data)->xtoy = 0;   /* b->xtoy = 0 */
70791c97fd4SSatish Balay   a->XtoY = 0; ((Mat_SeqAIJ*)aij->B->data)->XtoY = 0;   /* b->XtoY = 0 */
708a30b2313SHong Zhang 
7096bf464f9SBarry Smith   ierr = VecDestroy(&aij->diag);CHKERRQ(ierr);
710bd0c2dcbSBarry Smith   if (a->inode.size) mat->ops->multdiagonalblock = MatMultDiagonalBlock_MPIAIJ;
711e56f5c9eSBarry Smith 
7124f9cfa9eSBarry Smith   /* if no new nonzero locations are allowed in matrix then only set the matrix state the first time through */
7134f9cfa9eSBarry Smith   if ((!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) || !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
714e56f5c9eSBarry Smith     PetscObjectState state = aij->A->nonzerostate + aij->B->nonzerostate;
71509e82e2bSBarry Smith     ierr = MPI_Allreduce(&state,&mat->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
716e56f5c9eSBarry Smith   }
717bc5ccf88SSatish Balay   PetscFunctionReturn(0);
718bc5ccf88SSatish Balay }
719bc5ccf88SSatish Balay 
7204a2ae208SSatish Balay #undef __FUNCT__
7214a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_MPIAIJ"
722dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_MPIAIJ(Mat A)
7231eb62cbbSBarry Smith {
72444a69424SLois Curfman McInnes   Mat_MPIAIJ     *l = (Mat_MPIAIJ*)A->data;
725dfbe8321SBarry Smith   PetscErrorCode ierr;
7263a40ed3dSBarry Smith 
7273a40ed3dSBarry Smith   PetscFunctionBegin;
72878b31e54SBarry Smith   ierr = MatZeroEntries(l->A);CHKERRQ(ierr);
72978b31e54SBarry Smith   ierr = MatZeroEntries(l->B);CHKERRQ(ierr);
7303a40ed3dSBarry Smith   PetscFunctionReturn(0);
7311eb62cbbSBarry Smith }
7321eb62cbbSBarry Smith 
7334a2ae208SSatish Balay #undef __FUNCT__
7344a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_MPIAIJ"
7352b40b63fSBarry Smith PetscErrorCode MatZeroRows_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
7361eb62cbbSBarry Smith {
7371b1dd7adSMatthew G. Knepley   Mat_MPIAIJ    *mat    = (Mat_MPIAIJ *) A->data;
7381b1dd7adSMatthew G. Knepley   PetscInt      *owners = A->rmap->range;
7391b1dd7adSMatthew G. Knepley   PetscInt       n      = A->rmap->n;
7401b1dd7adSMatthew G. Knepley   PetscSF        sf;
7411b1dd7adSMatthew G. Knepley   PetscInt      *lrows;
7421b1dd7adSMatthew G. Knepley   PetscSFNode   *rrows;
74369ea2d38SJed Brown   PetscInt       r, p = 0, len = 0;
7446849ba73SBarry Smith   PetscErrorCode ierr;
7451eb62cbbSBarry Smith 
7463a40ed3dSBarry Smith   PetscFunctionBegin;
7471b1dd7adSMatthew G. Knepley   /* Create SF where leaves are input rows and roots are owned rows */
748785e854fSJed Brown   ierr = PetscMalloc1(n, &lrows);CHKERRQ(ierr);
7499d80f4afSMatthew G. Knepley   for (r = 0; r < n; ++r) lrows[r] = -1;
750a34163a4SJed Brown   if (!A->nooffproczerorows) {ierr = PetscMalloc1(N, &rrows);CHKERRQ(ierr);}
7511b1dd7adSMatthew G. Knepley   for (r = 0; r < N; ++r) {
7521b1dd7adSMatthew G. Knepley     const PetscInt idx   = rows[r];
75369ea2d38SJed Brown     if (idx < 0 || A->rmap->N <= idx) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range [0,%D)",idx,A->rmap->N);
75469ea2d38SJed Brown     if (idx < owners[p] || owners[p+1] <= idx) { /* short-circuit the search if the last p owns this row too */
75569ea2d38SJed Brown       ierr = PetscLayoutFindOwner(A->rmap,idx,&p);CHKERRQ(ierr);
75669ea2d38SJed Brown     }
757a34163a4SJed Brown     if (A->nooffproczerorows) {
758a34163a4SJed Brown       if (p != mat->rank) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"MAT_NO_OFF_PROC_ZERO_ROWS set, but row %D is not owned by rank %d",idx,mat->rank);
759a34163a4SJed Brown       lrows[len++] = idx - owners[p];
760a34163a4SJed Brown     } else {
7611b1dd7adSMatthew G. Knepley       rrows[r].rank = p;
7621b1dd7adSMatthew G. Knepley       rrows[r].index = rows[r] - owners[p];
7631eb62cbbSBarry Smith     }
764a34163a4SJed Brown   }
765a34163a4SJed Brown   if (!A->nooffproczerorows) {
7661b1dd7adSMatthew G. Knepley     ierr = PetscSFCreate(PetscObjectComm((PetscObject) A), &sf);CHKERRQ(ierr);
7671b1dd7adSMatthew G. Knepley     ierr = PetscSFSetGraph(sf, n, N, NULL, PETSC_OWN_POINTER, rrows, PETSC_OWN_POINTER);CHKERRQ(ierr);
7681b1dd7adSMatthew G. Knepley     /* Collect flags for rows to be zeroed */
76982102fcfSJed Brown     ierr = PetscSFReduceBegin(sf, MPIU_INT, (PetscInt*)rows, lrows, MPI_LOR);CHKERRQ(ierr);
77082102fcfSJed Brown     ierr = PetscSFReduceEnd(sf, MPIU_INT, (PetscInt*)rows, lrows, MPI_LOR);CHKERRQ(ierr);
7711b1dd7adSMatthew G. Knepley     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
7721b1dd7adSMatthew G. Knepley     /* Compress and put in row numbers */
7739d80f4afSMatthew G. Knepley     for (r = 0; r < n; ++r) if (lrows[r] >= 0) lrows[len++] = r;
774a34163a4SJed Brown   }
77597b48c8fSBarry Smith   /* fix right hand side if needed */
77697b48c8fSBarry Smith   if (x && b) {
7771b1dd7adSMatthew G. Knepley     const PetscScalar *xx;
7781b1dd7adSMatthew G. Knepley     PetscScalar       *bb;
7791b1dd7adSMatthew G. Knepley 
78097b48c8fSBarry Smith     ierr = VecGetArrayRead(x, &xx);CHKERRQ(ierr);
78197b48c8fSBarry Smith     ierr = VecGetArray(b, &bb);CHKERRQ(ierr);
7821b1dd7adSMatthew G. Knepley     for (r = 0; r < len; ++r) bb[lrows[r]] = diag*xx[lrows[r]];
78397b48c8fSBarry Smith     ierr = VecRestoreArrayRead(x, &xx);CHKERRQ(ierr);
78497b48c8fSBarry Smith     ierr = VecRestoreArray(b, &bb);CHKERRQ(ierr);
78597b48c8fSBarry Smith   }
7861b1dd7adSMatthew G. Knepley   /* Must zero l->B before l->A because the (diag) case below may put values into l->B*/
787a34163a4SJed Brown   ierr = MatZeroRows(mat->B, len, lrows, 0.0, NULL, NULL);CHKERRQ(ierr);
7881b1dd7adSMatthew G. Knepley   if ((diag != 0.0) && (mat->A->rmap->N == mat->A->cmap->N)) {
7891b1dd7adSMatthew G. Knepley     ierr = MatZeroRows(mat->A, len, lrows, diag, NULL, NULL);CHKERRQ(ierr);
790f4df32b1SMatthew Knepley   } else if (diag != 0.0) {
7911b1dd7adSMatthew G. Knepley     ierr = MatZeroRows(mat->A, len, lrows, 0.0, NULL, NULL);CHKERRQ(ierr);
7921b1dd7adSMatthew G. Knepley     if (((Mat_SeqAIJ *) mat->A->data)->nonew) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "MatZeroRows() on rectangular matrices cannot be used with the Mat options\nMAT_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR");
7931b1dd7adSMatthew G. Knepley     for (r = 0; r < len; ++r) {
7941b1dd7adSMatthew G. Knepley       const PetscInt row = lrows[r] + A->rmap->rstart;
795f4df32b1SMatthew Knepley       ierr = MatSetValues(A, 1, &row, 1, &row, &diag, INSERT_VALUES);CHKERRQ(ierr);
796e2d53e46SBarry Smith     }
797e2d53e46SBarry Smith     ierr = MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
798e2d53e46SBarry Smith     ierr = MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7996eb55b6aSBarry Smith   } else {
8001b1dd7adSMatthew G. Knepley     ierr = MatZeroRows(mat->A, len, lrows, 0.0, NULL, NULL);CHKERRQ(ierr);
8016eb55b6aSBarry Smith   }
802606d414cSSatish Balay   ierr = PetscFree(lrows);CHKERRQ(ierr);
8034f9cfa9eSBarry Smith 
8044f9cfa9eSBarry Smith   /* only change matrix nonzero state if pattern was allowed to be changed */
8054f9cfa9eSBarry Smith   if (!((Mat_SeqAIJ*)(mat->A->data))->keepnonzeropattern) {
806e56f5c9eSBarry Smith     PetscObjectState state = mat->A->nonzerostate + mat->B->nonzerostate;
80709e82e2bSBarry Smith     ierr = MPI_Allreduce(&state,&A->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)A));CHKERRQ(ierr);
808e56f5c9eSBarry Smith   }
8093a40ed3dSBarry Smith   PetscFunctionReturn(0);
8101eb62cbbSBarry Smith }
8111eb62cbbSBarry Smith 
8124a2ae208SSatish Balay #undef __FUNCT__
8139c7c4993SBarry Smith #define __FUNCT__ "MatZeroRowsColumns_MPIAIJ"
8149c7c4993SBarry Smith PetscErrorCode MatZeroRowsColumns_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
8159c7c4993SBarry Smith {
8169c7c4993SBarry Smith   Mat_MPIAIJ        *l = (Mat_MPIAIJ*)A->data;
8179c7c4993SBarry Smith   PetscErrorCode    ierr;
8185ba17502SJed Brown   PetscMPIInt       n = A->rmap->n;
81978fab17bSMatthew G. Knepley   PetscInt          i,j,r,m,p = 0,len = 0;
82054bd4135SMatthew G. Knepley   PetscInt          *lrows,*owners = A->rmap->range;
82154bd4135SMatthew G. Knepley   PetscSFNode       *rrows;
82254bd4135SMatthew G. Knepley   PetscSF           sf;
8239c7c4993SBarry Smith   const PetscScalar *xx;
824564f14d6SBarry Smith   PetscScalar       *bb,*mask;
825564f14d6SBarry Smith   Vec               xmask,lmask;
826564f14d6SBarry Smith   Mat_SeqAIJ        *aij = (Mat_SeqAIJ*)l->B->data;
827564f14d6SBarry Smith   const PetscInt    *aj, *ii,*ridx;
828564f14d6SBarry Smith   PetscScalar       *aa;
8299c7c4993SBarry Smith 
8309c7c4993SBarry Smith   PetscFunctionBegin;
83154bd4135SMatthew G. Knepley   /* Create SF where leaves are input rows and roots are owned rows */
83254bd4135SMatthew G. Knepley   ierr = PetscMalloc1(n, &lrows);CHKERRQ(ierr);
83354bd4135SMatthew G. Knepley   for (r = 0; r < n; ++r) lrows[r] = -1;
83454bd4135SMatthew G. Knepley   ierr = PetscMalloc1(N, &rrows);CHKERRQ(ierr);
83554bd4135SMatthew G. Knepley   for (r = 0; r < N; ++r) {
83654bd4135SMatthew G. Knepley     const PetscInt idx   = rows[r];
8375ba17502SJed Brown     if (idx < 0 || A->rmap->N <= idx) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range [0,%D)",idx,A->rmap->N);
8385ba17502SJed Brown     if (idx < owners[p] || owners[p+1] <= idx) { /* short-circuit the search if the last p owns this row too */
8395ba17502SJed Brown       ierr = PetscLayoutFindOwner(A->rmap,idx,&p);CHKERRQ(ierr);
8405ba17502SJed Brown     }
84154bd4135SMatthew G. Knepley     rrows[r].rank  = p;
84254bd4135SMatthew G. Knepley     rrows[r].index = rows[r] - owners[p];
8439c7c4993SBarry Smith   }
84454bd4135SMatthew G. Knepley   ierr = PetscSFCreate(PetscObjectComm((PetscObject) A), &sf);CHKERRQ(ierr);
84554bd4135SMatthew G. Knepley   ierr = PetscSFSetGraph(sf, n, N, NULL, PETSC_OWN_POINTER, rrows, PETSC_OWN_POINTER);CHKERRQ(ierr);
84654bd4135SMatthew G. Knepley   /* Collect flags for rows to be zeroed */
84754bd4135SMatthew G. Knepley   ierr = PetscSFReduceBegin(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);CHKERRQ(ierr);
84854bd4135SMatthew G. Knepley   ierr = PetscSFReduceEnd(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);CHKERRQ(ierr);
84954bd4135SMatthew G. Knepley   ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
85054bd4135SMatthew G. Knepley   /* Compress and put in row numbers */
85154bd4135SMatthew G. Knepley   for (r = 0; r < n; ++r) if (lrows[r] >= 0) lrows[len++] = r;
852564f14d6SBarry Smith   /* zero diagonal part of matrix */
85354bd4135SMatthew G. Knepley   ierr = MatZeroRowsColumns(l->A,len,lrows,diag,x,b);CHKERRQ(ierr);
854564f14d6SBarry Smith   /* handle off diagonal part of matrix */
8550298fd71SBarry Smith   ierr = MatGetVecs(A,&xmask,NULL);CHKERRQ(ierr);
856564f14d6SBarry Smith   ierr = VecDuplicate(l->lvec,&lmask);CHKERRQ(ierr);
857564f14d6SBarry Smith   ierr = VecGetArray(xmask,&bb);CHKERRQ(ierr);
85854bd4135SMatthew G. Knepley   for (i=0; i<len; i++) bb[lrows[i]] = 1;
859564f14d6SBarry Smith   ierr = VecRestoreArray(xmask,&bb);CHKERRQ(ierr);
860564f14d6SBarry Smith   ierr = VecScatterBegin(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
861564f14d6SBarry Smith   ierr = VecScatterEnd(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
8626bf464f9SBarry Smith   ierr = VecDestroy(&xmask);CHKERRQ(ierr);
863377aa5a1SBarry Smith   if (x) {
86467caceb0SMatthew G. Knepley     ierr = VecScatterBegin(l->Mvctx,x,l->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
86567caceb0SMatthew G. Knepley     ierr = VecScatterEnd(l->Mvctx,x,l->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
866564f14d6SBarry Smith     ierr = VecGetArrayRead(l->lvec,&xx);CHKERRQ(ierr);
867564f14d6SBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
868377aa5a1SBarry Smith   }
869377aa5a1SBarry Smith   ierr = VecGetArray(lmask,&mask);CHKERRQ(ierr);
870564f14d6SBarry Smith   /* remove zeroed rows of off diagonal matrix */
871564f14d6SBarry Smith   ii = aij->i;
87254bd4135SMatthew G. Knepley   for (i=0; i<len; i++) {
873564f14d6SBarry Smith     ierr = PetscMemzero(aij->a + ii[lrows[i]],(ii[lrows[i]+1] - ii[lrows[i]])*sizeof(PetscScalar));CHKERRQ(ierr);
8749c7c4993SBarry Smith   }
875564f14d6SBarry Smith   /* loop over all elements of off process part of matrix zeroing removed columns*/
876564f14d6SBarry Smith   if (aij->compressedrow.use) {
877564f14d6SBarry Smith     m    = aij->compressedrow.nrows;
878564f14d6SBarry Smith     ii   = aij->compressedrow.i;
879564f14d6SBarry Smith     ridx = aij->compressedrow.rindex;
880564f14d6SBarry Smith     for (i=0; i<m; i++) {
881564f14d6SBarry Smith       n  = ii[i+1] - ii[i];
882564f14d6SBarry Smith       aj = aij->j + ii[i];
883564f14d6SBarry Smith       aa = aij->a + ii[i];
884564f14d6SBarry Smith 
885564f14d6SBarry Smith       for (j=0; j<n; j++) {
88625266a92SSatish Balay         if (PetscAbsScalar(mask[*aj])) {
887377aa5a1SBarry Smith           if (b) bb[*ridx] -= *aa*xx[*aj];
888564f14d6SBarry Smith           *aa = 0.0;
889564f14d6SBarry Smith         }
890564f14d6SBarry Smith         aa++;
891564f14d6SBarry Smith         aj++;
892564f14d6SBarry Smith       }
893564f14d6SBarry Smith       ridx++;
894564f14d6SBarry Smith     }
895564f14d6SBarry Smith   } else { /* do not use compressed row format */
896564f14d6SBarry Smith     m = l->B->rmap->n;
897564f14d6SBarry Smith     for (i=0; i<m; i++) {
898564f14d6SBarry Smith       n  = ii[i+1] - ii[i];
899564f14d6SBarry Smith       aj = aij->j + ii[i];
900564f14d6SBarry Smith       aa = aij->a + ii[i];
901564f14d6SBarry Smith       for (j=0; j<n; j++) {
90225266a92SSatish Balay         if (PetscAbsScalar(mask[*aj])) {
903377aa5a1SBarry Smith           if (b) bb[i] -= *aa*xx[*aj];
904564f14d6SBarry Smith           *aa = 0.0;
905564f14d6SBarry Smith         }
906564f14d6SBarry Smith         aa++;
907564f14d6SBarry Smith         aj++;
908564f14d6SBarry Smith       }
909564f14d6SBarry Smith     }
910564f14d6SBarry Smith   }
911377aa5a1SBarry Smith   if (x) {
912564f14d6SBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
913564f14d6SBarry Smith     ierr = VecRestoreArrayRead(l->lvec,&xx);CHKERRQ(ierr);
914377aa5a1SBarry Smith   }
915377aa5a1SBarry Smith   ierr = VecRestoreArray(lmask,&mask);CHKERRQ(ierr);
9166bf464f9SBarry Smith   ierr = VecDestroy(&lmask);CHKERRQ(ierr);
9179c7c4993SBarry Smith   ierr = PetscFree(lrows);CHKERRQ(ierr);
9184f9cfa9eSBarry Smith 
9194f9cfa9eSBarry Smith   /* only change matrix nonzero state if pattern was allowed to be changed */
9204f9cfa9eSBarry Smith   if (!((Mat_SeqAIJ*)(l->A->data))->keepnonzeropattern) {
9214f9cfa9eSBarry Smith     PetscObjectState state = l->A->nonzerostate + l->B->nonzerostate;
9224f9cfa9eSBarry Smith     ierr = MPI_Allreduce(&state,&A->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)A));CHKERRQ(ierr);
9234f9cfa9eSBarry Smith   }
9249c7c4993SBarry Smith   PetscFunctionReturn(0);
9259c7c4993SBarry Smith }
9269c7c4993SBarry Smith 
9279c7c4993SBarry Smith #undef __FUNCT__
9284a2ae208SSatish Balay #define __FUNCT__ "MatMult_MPIAIJ"
929dfbe8321SBarry Smith PetscErrorCode MatMult_MPIAIJ(Mat A,Vec xx,Vec yy)
9301eb62cbbSBarry Smith {
931416022c9SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
932dfbe8321SBarry Smith   PetscErrorCode ierr;
933b1d57f15SBarry Smith   PetscInt       nt;
934416022c9SBarry Smith 
9353a40ed3dSBarry Smith   PetscFunctionBegin;
936a2ce50c7SBarry Smith   ierr = VecGetLocalSize(xx,&nt);CHKERRQ(ierr);
93765e19b50SBarry 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);
938ca9f406cSSatish Balay   ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
939f830108cSBarry Smith   ierr = (*a->A->ops->mult)(a->A,xx,yy);CHKERRQ(ierr);
940ca9f406cSSatish Balay   ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
941f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);CHKERRQ(ierr);
9423a40ed3dSBarry Smith   PetscFunctionReturn(0);
9431eb62cbbSBarry Smith }
9441eb62cbbSBarry Smith 
9454a2ae208SSatish Balay #undef __FUNCT__
946bd0c2dcbSBarry Smith #define __FUNCT__ "MatMultDiagonalBlock_MPIAIJ"
947bd0c2dcbSBarry Smith PetscErrorCode MatMultDiagonalBlock_MPIAIJ(Mat A,Vec bb,Vec xx)
948bd0c2dcbSBarry Smith {
949bd0c2dcbSBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
950bd0c2dcbSBarry Smith   PetscErrorCode ierr;
951bd0c2dcbSBarry Smith 
952bd0c2dcbSBarry Smith   PetscFunctionBegin;
953bd0c2dcbSBarry Smith   ierr = MatMultDiagonalBlock(a->A,bb,xx);CHKERRQ(ierr);
954bd0c2dcbSBarry Smith   PetscFunctionReturn(0);
955bd0c2dcbSBarry Smith }
956bd0c2dcbSBarry Smith 
957bd0c2dcbSBarry Smith #undef __FUNCT__
9584a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_MPIAIJ"
959dfbe8321SBarry Smith PetscErrorCode MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
960da3a660dSBarry Smith {
961416022c9SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
962dfbe8321SBarry Smith   PetscErrorCode ierr;
9633a40ed3dSBarry Smith 
9643a40ed3dSBarry Smith   PetscFunctionBegin;
965ca9f406cSSatish Balay   ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
966f830108cSBarry Smith   ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
967ca9f406cSSatish Balay   ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
968f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);CHKERRQ(ierr);
9693a40ed3dSBarry Smith   PetscFunctionReturn(0);
970da3a660dSBarry Smith }
971da3a660dSBarry Smith 
9724a2ae208SSatish Balay #undef __FUNCT__
9734a2ae208SSatish Balay #define __FUNCT__ "MatMultTranspose_MPIAIJ"
974dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_MPIAIJ(Mat A,Vec xx,Vec yy)
975da3a660dSBarry Smith {
976416022c9SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
977dfbe8321SBarry Smith   PetscErrorCode ierr;
978ace3abfcSBarry Smith   PetscBool      merged;
979da3a660dSBarry Smith 
9803a40ed3dSBarry Smith   PetscFunctionBegin;
981a5ff213dSBarry Smith   ierr = VecScatterGetMerged(a->Mvctx,&merged);CHKERRQ(ierr);
982da3a660dSBarry Smith   /* do nondiagonal part */
9837c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
984a5ff213dSBarry Smith   if (!merged) {
985da3a660dSBarry Smith     /* send it on its way */
986ca9f406cSSatish Balay     ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
987da3a660dSBarry Smith     /* do local part */
9887c922b88SBarry Smith     ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr);
989da3a660dSBarry Smith     /* receive remote parts: note this assumes the values are not actually */
990a5ff213dSBarry Smith     /* added in yy until the next line, */
991ca9f406cSSatish Balay     ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
992a5ff213dSBarry Smith   } else {
993a5ff213dSBarry Smith     /* do local part */
994a5ff213dSBarry Smith     ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr);
995a5ff213dSBarry Smith     /* send it on its way */
996ca9f406cSSatish Balay     ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
997a5ff213dSBarry Smith     /* values actually were received in the Begin() but we need to call this nop */
998ca9f406cSSatish Balay     ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
999a5ff213dSBarry Smith   }
10003a40ed3dSBarry Smith   PetscFunctionReturn(0);
1001da3a660dSBarry Smith }
1002da3a660dSBarry Smith 
1003cd0d46ebSvictorle #undef __FUNCT__
10045fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_MPIAIJ"
10057087cfbeSBarry Smith PetscErrorCode  MatIsTranspose_MPIAIJ(Mat Amat,Mat Bmat,PetscReal tol,PetscBool  *f)
1006cd0d46ebSvictorle {
10074f423910Svictorle   MPI_Comm       comm;
1008cd0d46ebSvictorle   Mat_MPIAIJ     *Aij = (Mat_MPIAIJ*) Amat->data, *Bij;
100966501d38Svictorle   Mat            Adia = Aij->A, Bdia, Aoff,Boff,*Aoffs,*Boffs;
1010cd0d46ebSvictorle   IS             Me,Notme;
10116849ba73SBarry Smith   PetscErrorCode ierr;
1012b1d57f15SBarry Smith   PetscInt       M,N,first,last,*notme,i;
1013b1d57f15SBarry Smith   PetscMPIInt    size;
1014cd0d46ebSvictorle 
1015cd0d46ebSvictorle   PetscFunctionBegin;
101642e5f5b4Svictorle   /* Easy test: symmetric diagonal block */
101766501d38Svictorle   Bij  = (Mat_MPIAIJ*) Bmat->data; Bdia = Bij->A;
10185485867bSBarry Smith   ierr = MatIsTranspose(Adia,Bdia,tol,f);CHKERRQ(ierr);
1019cd0d46ebSvictorle   if (!*f) PetscFunctionReturn(0);
10204f423910Svictorle   ierr = PetscObjectGetComm((PetscObject)Amat,&comm);CHKERRQ(ierr);
1021b1d57f15SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
1022b1d57f15SBarry Smith   if (size == 1) PetscFunctionReturn(0);
102342e5f5b4Svictorle 
102442e5f5b4Svictorle   /* Hard test: off-diagonal block. This takes a MatGetSubMatrix. */
1025cd0d46ebSvictorle   ierr = MatGetSize(Amat,&M,&N);CHKERRQ(ierr);
1026cd0d46ebSvictorle   ierr = MatGetOwnershipRange(Amat,&first,&last);CHKERRQ(ierr);
1027785e854fSJed Brown   ierr = PetscMalloc1((N-last+first),&notme);CHKERRQ(ierr);
1028cd0d46ebSvictorle   for (i=0; i<first; i++) notme[i] = i;
1029cd0d46ebSvictorle   for (i=last; i<M; i++) notme[i-last+first] = i;
103070b3c8c7SBarry Smith   ierr = ISCreateGeneral(MPI_COMM_SELF,N-last+first,notme,PETSC_COPY_VALUES,&Notme);CHKERRQ(ierr);
1031268466fbSBarry Smith   ierr = ISCreateStride(MPI_COMM_SELF,last-first,first,1,&Me);CHKERRQ(ierr);
1032268466fbSBarry Smith   ierr = MatGetSubMatrices(Amat,1,&Me,&Notme,MAT_INITIAL_MATRIX,&Aoffs);CHKERRQ(ierr);
103366501d38Svictorle   Aoff = Aoffs[0];
1034268466fbSBarry Smith   ierr = MatGetSubMatrices(Bmat,1,&Notme,&Me,MAT_INITIAL_MATRIX,&Boffs);CHKERRQ(ierr);
103566501d38Svictorle   Boff = Boffs[0];
10365485867bSBarry Smith   ierr = MatIsTranspose(Aoff,Boff,tol,f);CHKERRQ(ierr);
103766501d38Svictorle   ierr = MatDestroyMatrices(1,&Aoffs);CHKERRQ(ierr);
103866501d38Svictorle   ierr = MatDestroyMatrices(1,&Boffs);CHKERRQ(ierr);
10396bf464f9SBarry Smith   ierr = ISDestroy(&Me);CHKERRQ(ierr);
10406bf464f9SBarry Smith   ierr = ISDestroy(&Notme);CHKERRQ(ierr);
10413e0d0d19SHong Zhang   ierr = PetscFree(notme);CHKERRQ(ierr);
1042cd0d46ebSvictorle   PetscFunctionReturn(0);
1043cd0d46ebSvictorle }
1044cd0d46ebSvictorle 
10454a2ae208SSatish Balay #undef __FUNCT__
10464a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_MPIAIJ"
1047dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1048da3a660dSBarry Smith {
1049416022c9SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
1050dfbe8321SBarry Smith   PetscErrorCode ierr;
1051da3a660dSBarry Smith 
10523a40ed3dSBarry Smith   PetscFunctionBegin;
1053da3a660dSBarry Smith   /* do nondiagonal part */
10547c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
1055da3a660dSBarry Smith   /* send it on its way */
1056ca9f406cSSatish Balay   ierr = VecScatterBegin(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1057da3a660dSBarry Smith   /* do local part */
10587c922b88SBarry Smith   ierr = (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
1059a5ff213dSBarry Smith   /* receive remote parts */
1060ca9f406cSSatish Balay   ierr = VecScatterEnd(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
10613a40ed3dSBarry Smith   PetscFunctionReturn(0);
1062da3a660dSBarry Smith }
1063da3a660dSBarry Smith 
10641eb62cbbSBarry Smith /*
10651eb62cbbSBarry Smith   This only works correctly for square matrices where the subblock A->A is the
10661eb62cbbSBarry Smith    diagonal block
10671eb62cbbSBarry Smith */
10684a2ae208SSatish Balay #undef __FUNCT__
10694a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_MPIAIJ"
1070dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_MPIAIJ(Mat A,Vec v)
10711eb62cbbSBarry Smith {
1072dfbe8321SBarry Smith   PetscErrorCode ierr;
1073416022c9SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
10743a40ed3dSBarry Smith 
10753a40ed3dSBarry Smith   PetscFunctionBegin;
1076ce94432eSBarry Smith   if (A->rmap->N != A->cmap->N) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block");
1077e7e72b3dSBarry 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");
10783a40ed3dSBarry Smith   ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr);
10793a40ed3dSBarry Smith   PetscFunctionReturn(0);
10801eb62cbbSBarry Smith }
10811eb62cbbSBarry Smith 
10824a2ae208SSatish Balay #undef __FUNCT__
10834a2ae208SSatish Balay #define __FUNCT__ "MatScale_MPIAIJ"
1084f4df32b1SMatthew Knepley PetscErrorCode MatScale_MPIAIJ(Mat A,PetscScalar aa)
1085052efed2SBarry Smith {
1086052efed2SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
1087dfbe8321SBarry Smith   PetscErrorCode ierr;
10883a40ed3dSBarry Smith 
10893a40ed3dSBarry Smith   PetscFunctionBegin;
1090f4df32b1SMatthew Knepley   ierr = MatScale(a->A,aa);CHKERRQ(ierr);
1091f4df32b1SMatthew Knepley   ierr = MatScale(a->B,aa);CHKERRQ(ierr);
10923a40ed3dSBarry Smith   PetscFunctionReturn(0);
1093052efed2SBarry Smith }
1094052efed2SBarry Smith 
10954a2ae208SSatish Balay #undef __FUNCT__
1096a3ca3016SBarry Smith #define __FUNCT__ "MatDestroy_Redundant"
1097a3ca3016SBarry Smith PetscErrorCode MatDestroy_Redundant(Mat_Redundant **redundant)
10984472c490SBarry Smith {
10994472c490SBarry Smith   PetscErrorCode ierr;
1100a3ca3016SBarry Smith   Mat_Redundant  *redund = *redundant;
11014472c490SBarry Smith   PetscInt       i;
11024472c490SBarry Smith 
11034472c490SBarry Smith   PetscFunctionBegin;
1104a3ca3016SBarry Smith   *redundant = NULL;
11054472c490SBarry Smith   if (redund){
11064472c490SBarry Smith     if (redund->matseq) { /* via MatGetSubMatrices()  */
11074472c490SBarry Smith       ierr = ISDestroy(&redund->isrow);CHKERRQ(ierr);
11084472c490SBarry Smith       ierr = ISDestroy(&redund->iscol);CHKERRQ(ierr);
11094472c490SBarry Smith       ierr = MatDestroy(&redund->matseq[0]);CHKERRQ(ierr);
11104472c490SBarry Smith       ierr = PetscFree(redund->matseq);CHKERRQ(ierr);
11114472c490SBarry Smith     } else {
11124472c490SBarry Smith       ierr = PetscFree2(redund->send_rank,redund->recv_rank);CHKERRQ(ierr);
11134472c490SBarry Smith       ierr = PetscFree(redund->sbuf_j);CHKERRQ(ierr);
11144472c490SBarry Smith       ierr = PetscFree(redund->sbuf_a);CHKERRQ(ierr);
11154472c490SBarry Smith       for (i=0; i<redund->nrecvs; i++) {
11164472c490SBarry Smith         ierr = PetscFree(redund->rbuf_j[i]);CHKERRQ(ierr);
11174472c490SBarry Smith         ierr = PetscFree(redund->rbuf_a[i]);CHKERRQ(ierr);
11184472c490SBarry Smith       }
11194472c490SBarry Smith       ierr = PetscFree4(redund->sbuf_nz,redund->rbuf_nz,redund->rbuf_j,redund->rbuf_a);CHKERRQ(ierr);
11204472c490SBarry Smith     }
11214472c490SBarry Smith 
11224472c490SBarry Smith     if (redund->psubcomm) {
11234472c490SBarry Smith       ierr = PetscSubcommDestroy(&redund->psubcomm);CHKERRQ(ierr);
11244472c490SBarry Smith     }
11254472c490SBarry Smith     ierr = PetscFree(redund);CHKERRQ(ierr);
11264472c490SBarry Smith   }
11274472c490SBarry Smith   PetscFunctionReturn(0);
11284472c490SBarry Smith }
11294472c490SBarry Smith 
11304472c490SBarry Smith #undef __FUNCT__
11314a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_MPIAIJ"
1132dfbe8321SBarry Smith PetscErrorCode MatDestroy_MPIAIJ(Mat mat)
11331eb62cbbSBarry Smith {
113444a69424SLois Curfman McInnes   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
1135dfbe8321SBarry Smith   PetscErrorCode ierr;
113683e2fdc7SBarry Smith 
11373a40ed3dSBarry Smith   PetscFunctionBegin;
1138aa482453SBarry Smith #if defined(PETSC_USE_LOG)
1139d0f46423SBarry Smith   PetscLogObjectState((PetscObject)mat,"Rows=%D, Cols=%D",mat->rmap->N,mat->cmap->N);
1140a5a9c739SBarry Smith #endif
1141a3ca3016SBarry Smith   ierr = MatDestroy_Redundant(&aij->redundant);CHKERRQ(ierr);
11428798bf22SSatish Balay   ierr = MatStashDestroy_Private(&mat->stash);CHKERRQ(ierr);
11436bf464f9SBarry Smith   ierr = VecDestroy(&aij->diag);CHKERRQ(ierr);
11446bf464f9SBarry Smith   ierr = MatDestroy(&aij->A);CHKERRQ(ierr);
11456bf464f9SBarry Smith   ierr = MatDestroy(&aij->B);CHKERRQ(ierr);
1146aa482453SBarry Smith #if defined(PETSC_USE_CTABLE)
11476bc0bbbfSBarry Smith   ierr = PetscTableDestroy(&aij->colmap);CHKERRQ(ierr);
1148b1fc9764SSatish Balay #else
114905b42c5fSBarry Smith   ierr = PetscFree(aij->colmap);CHKERRQ(ierr);
1150b1fc9764SSatish Balay #endif
115105b42c5fSBarry Smith   ierr = PetscFree(aij->garray);CHKERRQ(ierr);
11526bf464f9SBarry Smith   ierr = VecDestroy(&aij->lvec);CHKERRQ(ierr);
11536bf464f9SBarry Smith   ierr = VecScatterDestroy(&aij->Mvctx);CHKERRQ(ierr);
115403095fedSBarry Smith   ierr = PetscFree2(aij->rowvalues,aij->rowindices);CHKERRQ(ierr);
11558aa348c1SBarry Smith   ierr = PetscFree(aij->ld);CHKERRQ(ierr);
1156bf0cc555SLisandro Dalcin   ierr = PetscFree(mat->data);CHKERRQ(ierr);
1157901853e0SKris Buschelman 
1158dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)mat,0);CHKERRQ(ierr);
1159bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C",NULL);CHKERRQ(ierr);
1160bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C",NULL);CHKERRQ(ierr);
1161bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatGetDiagonalBlock_C",NULL);CHKERRQ(ierr);
1162bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatIsTranspose_C",NULL);CHKERRQ(ierr);
1163bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocation_C",NULL);CHKERRQ(ierr);
1164bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocationCSR_C",NULL);CHKERRQ(ierr);
1165bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatDiagonalScaleLocal_C",NULL);CHKERRQ(ierr);
1166bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_mpisbaij_C",NULL);CHKERRQ(ierr);
11673a40ed3dSBarry Smith   PetscFunctionReturn(0);
11681eb62cbbSBarry Smith }
1169ee50ffe9SBarry Smith 
11704a2ae208SSatish Balay #undef __FUNCT__
11718e2fed03SBarry Smith #define __FUNCT__ "MatView_MPIAIJ_Binary"
1172dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ_Binary(Mat mat,PetscViewer viewer)
11738e2fed03SBarry Smith {
11748e2fed03SBarry Smith   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
11758e2fed03SBarry Smith   Mat_SeqAIJ     *A   = (Mat_SeqAIJ*)aij->A->data;
11768e2fed03SBarry Smith   Mat_SeqAIJ     *B   = (Mat_SeqAIJ*)aij->B->data;
11776849ba73SBarry Smith   PetscErrorCode ierr;
117832dcc486SBarry Smith   PetscMPIInt    rank,size,tag = ((PetscObject)viewer)->tag;
11796f69ff64SBarry Smith   int            fd;
1180a788621eSSatish Balay   PetscInt       nz,header[4],*row_lengths,*range=0,rlen,i;
1181d892089bSMatthew G. Knepley   PetscInt       nzmax,*column_indices,j,k,col,*garray = aij->garray,cnt,cstart = mat->cmap->rstart,rnz = 0;
11828e2fed03SBarry Smith   PetscScalar    *column_values;
118385ebf7a4SBarry Smith   PetscInt       message_count,flowcontrolcount;
1184b37d52dbSMark F. Adams   FILE           *file;
11858e2fed03SBarry Smith 
11868e2fed03SBarry Smith   PetscFunctionBegin;
1187ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&rank);CHKERRQ(ierr);
1188ce94432eSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRQ(ierr);
11898e2fed03SBarry Smith   nz   = A->nz + B->nz;
1190958c9bccSBarry Smith   if (!rank) {
11910700a824SBarry Smith     header[0] = MAT_FILE_CLASSID;
1192d0f46423SBarry Smith     header[1] = mat->rmap->N;
1193d0f46423SBarry Smith     header[2] = mat->cmap->N;
11942205254eSKarl Rupp 
1195ce94432eSBarry Smith     ierr = MPI_Reduce(&nz,&header[3],1,MPIU_INT,MPI_SUM,0,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
11968e2fed03SBarry Smith     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
11976f69ff64SBarry Smith     ierr = PetscBinaryWrite(fd,header,4,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
11988e2fed03SBarry Smith     /* get largest number of rows any processor has */
1199d0f46423SBarry Smith     rlen  = mat->rmap->n;
1200d0f46423SBarry Smith     range = mat->rmap->range;
12012205254eSKarl Rupp     for (i=1; i<size; i++) rlen = PetscMax(rlen,range[i+1] - range[i]);
12028e2fed03SBarry Smith   } else {
1203ce94432eSBarry Smith     ierr = MPI_Reduce(&nz,0,1,MPIU_INT,MPI_SUM,0,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1204d0f46423SBarry Smith     rlen = mat->rmap->n;
12058e2fed03SBarry Smith   }
12068e2fed03SBarry Smith 
12078e2fed03SBarry Smith   /* load up the local row counts */
1208785e854fSJed Brown   ierr = PetscMalloc1((rlen+1),&row_lengths);CHKERRQ(ierr);
12092205254eSKarl Rupp   for (i=0; i<mat->rmap->n; i++) row_lengths[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i];
12108e2fed03SBarry Smith 
12118e2fed03SBarry Smith   /* store the row lengths to the file */
121285ebf7a4SBarry Smith   ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr);
1213958c9bccSBarry Smith   if (!rank) {
1214d0f46423SBarry Smith     ierr = PetscBinaryWrite(fd,row_lengths,mat->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
12158e2fed03SBarry Smith     for (i=1; i<size; i++) {
1216639ff905SBarry Smith       ierr = PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);CHKERRQ(ierr);
12178e2fed03SBarry Smith       rlen = range[i+1] - range[i];
1218ce94432eSBarry Smith       ierr = MPIULong_Recv(row_lengths,rlen,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
12196f69ff64SBarry Smith       ierr = PetscBinaryWrite(fd,row_lengths,rlen,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
12208e2fed03SBarry Smith     }
1221639ff905SBarry Smith     ierr = PetscViewerFlowControlEndMaster(viewer,&message_count);CHKERRQ(ierr);
12228e2fed03SBarry Smith   } else {
1223639ff905SBarry Smith     ierr = PetscViewerFlowControlStepWorker(viewer,rank,&message_count);CHKERRQ(ierr);
1224ce94432eSBarry Smith     ierr = MPIULong_Send(row_lengths,mat->rmap->n,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1225639ff905SBarry Smith     ierr = PetscViewerFlowControlEndWorker(viewer,&message_count);CHKERRQ(ierr);
12268e2fed03SBarry Smith   }
12278e2fed03SBarry Smith   ierr = PetscFree(row_lengths);CHKERRQ(ierr);
12288e2fed03SBarry Smith 
12298e2fed03SBarry Smith   /* load up the local column indices */
12301147fc2aSKarl Rupp   nzmax = nz; /* th processor needs space a largest processor needs */
1231ce94432eSBarry Smith   ierr  = MPI_Reduce(&nz,&nzmax,1,MPIU_INT,MPI_MAX,0,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1232785e854fSJed Brown   ierr  = PetscMalloc1((nzmax+1),&column_indices);CHKERRQ(ierr);
12338e2fed03SBarry Smith   cnt   = 0;
1234d0f46423SBarry Smith   for (i=0; i<mat->rmap->n; i++) {
12358e2fed03SBarry Smith     for (j=B->i[i]; j<B->i[i+1]; j++) {
12368e2fed03SBarry Smith       if ((col = garray[B->j[j]]) > cstart) break;
12378e2fed03SBarry Smith       column_indices[cnt++] = col;
12388e2fed03SBarry Smith     }
12392205254eSKarl Rupp     for (k=A->i[i]; k<A->i[i+1]; k++) column_indices[cnt++] = A->j[k] + cstart;
12402205254eSKarl Rupp     for (; j<B->i[i+1]; j++) column_indices[cnt++] = garray[B->j[j]];
12418e2fed03SBarry Smith   }
1242e32f2f54SBarry 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);
12438e2fed03SBarry Smith 
12448e2fed03SBarry Smith   /* store the column indices to the file */
124585ebf7a4SBarry Smith   ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr);
1246958c9bccSBarry Smith   if (!rank) {
12478e2fed03SBarry Smith     MPI_Status status;
12486f69ff64SBarry Smith     ierr = PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
12498e2fed03SBarry Smith     for (i=1; i<size; i++) {
1250639ff905SBarry Smith       ierr = PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);CHKERRQ(ierr);
1251ce94432eSBarry Smith       ierr = MPI_Recv(&rnz,1,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);CHKERRQ(ierr);
1252e32f2f54SBarry Smith       if (rnz > nzmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax);
1253ce94432eSBarry Smith       ierr = MPIULong_Recv(column_indices,rnz,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
12546f69ff64SBarry Smith       ierr = PetscBinaryWrite(fd,column_indices,rnz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
12558e2fed03SBarry Smith     }
1256639ff905SBarry Smith     ierr = PetscViewerFlowControlEndMaster(viewer,&message_count);CHKERRQ(ierr);
12578e2fed03SBarry Smith   } else {
1258639ff905SBarry Smith     ierr = PetscViewerFlowControlStepWorker(viewer,rank,&message_count);CHKERRQ(ierr);
1259ce94432eSBarry Smith     ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1260ce94432eSBarry Smith     ierr = MPIULong_Send(column_indices,nz,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1261639ff905SBarry Smith     ierr = PetscViewerFlowControlEndWorker(viewer,&message_count);CHKERRQ(ierr);
12628e2fed03SBarry Smith   }
12638e2fed03SBarry Smith   ierr = PetscFree(column_indices);CHKERRQ(ierr);
12648e2fed03SBarry Smith 
12658e2fed03SBarry Smith   /* load up the local column values */
1266785e854fSJed Brown   ierr = PetscMalloc1((nzmax+1),&column_values);CHKERRQ(ierr);
12678e2fed03SBarry Smith   cnt  = 0;
1268d0f46423SBarry Smith   for (i=0; i<mat->rmap->n; i++) {
12698e2fed03SBarry Smith     for (j=B->i[i]; j<B->i[i+1]; j++) {
12708e2fed03SBarry Smith       if (garray[B->j[j]] > cstart) break;
12718e2fed03SBarry Smith       column_values[cnt++] = B->a[j];
12728e2fed03SBarry Smith     }
12732205254eSKarl Rupp     for (k=A->i[i]; k<A->i[i+1]; k++) column_values[cnt++] = A->a[k];
12742205254eSKarl Rupp     for (; j<B->i[i+1]; j++) column_values[cnt++] = B->a[j];
12758e2fed03SBarry Smith   }
1276e32f2f54SBarry 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);
12778e2fed03SBarry Smith 
12788e2fed03SBarry Smith   /* store the column values to the file */
127985ebf7a4SBarry Smith   ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr);
1280958c9bccSBarry Smith   if (!rank) {
12818e2fed03SBarry Smith     MPI_Status status;
12826f69ff64SBarry Smith     ierr = PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr);
12838e2fed03SBarry Smith     for (i=1; i<size; i++) {
1284639ff905SBarry Smith       ierr = PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);CHKERRQ(ierr);
1285ce94432eSBarry Smith       ierr = MPI_Recv(&rnz,1,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);CHKERRQ(ierr);
1286e32f2f54SBarry Smith       if (rnz > nzmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax);
1287ce94432eSBarry Smith       ierr = MPIULong_Recv(column_values,rnz,MPIU_SCALAR,i,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
12886f69ff64SBarry Smith       ierr = PetscBinaryWrite(fd,column_values,rnz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr);
12898e2fed03SBarry Smith     }
1290639ff905SBarry Smith     ierr = PetscViewerFlowControlEndMaster(viewer,&message_count);CHKERRQ(ierr);
12918e2fed03SBarry Smith   } else {
1292639ff905SBarry Smith     ierr = PetscViewerFlowControlStepWorker(viewer,rank,&message_count);CHKERRQ(ierr);
1293ce94432eSBarry Smith     ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1294ce94432eSBarry Smith     ierr = MPIULong_Send(column_values,nz,MPIU_SCALAR,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1295639ff905SBarry Smith     ierr = PetscViewerFlowControlEndWorker(viewer,&message_count);CHKERRQ(ierr);
12968e2fed03SBarry Smith   }
12978e2fed03SBarry Smith   ierr = PetscFree(column_values);CHKERRQ(ierr);
1298b37d52dbSMark F. Adams 
1299b37d52dbSMark F. Adams   ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr);
130033d57670SJed Brown   if (file) fprintf(file,"-matload_block_size %d\n",(int)PetscAbs(mat->rmap->bs));
13018e2fed03SBarry Smith   PetscFunctionReturn(0);
13028e2fed03SBarry Smith }
13038e2fed03SBarry Smith 
13049804daf3SBarry Smith #include <petscdraw.h>
13058e2fed03SBarry Smith #undef __FUNCT__
13064a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ_ASCIIorDraworSocket"
1307dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
1308416022c9SBarry Smith {
130944a69424SLois Curfman McInnes   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*)mat->data;
1310dfbe8321SBarry Smith   PetscErrorCode    ierr;
131132dcc486SBarry Smith   PetscMPIInt       rank = aij->rank,size = aij->size;
1312ace3abfcSBarry Smith   PetscBool         isdraw,iascii,isbinary;
1313b0a32e0cSBarry Smith   PetscViewer       sviewer;
1314f3ef73ceSBarry Smith   PetscViewerFormat format;
1315416022c9SBarry Smith 
13163a40ed3dSBarry Smith   PetscFunctionBegin;
1317251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1318251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1319251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
132032077d6dSBarry Smith   if (iascii) {
1321b0a32e0cSBarry Smith     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
1322456192e2SBarry Smith     if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
13234e220ebcSLois Curfman McInnes       MatInfo   info;
1324ace3abfcSBarry Smith       PetscBool inodes;
1325923f20ffSKris Buschelman 
1326ce94432eSBarry Smith       ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&rank);CHKERRQ(ierr);
1327888f2ed8SSatish Balay       ierr = MatGetInfo(mat,MAT_LOCAL,&info);CHKERRQ(ierr);
13280298fd71SBarry Smith       ierr = MatInodeGetInodeSizes(aij->A,NULL,(PetscInt**)&inodes,NULL);CHKERRQ(ierr);
13297b23a99aSBarry Smith       ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_TRUE);CHKERRQ(ierr);
1330923f20ffSKris Buschelman       if (!inodes) {
133177431f27SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, not using I-node routines\n",
1332d0f46423SBarry Smith                                                   rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);CHKERRQ(ierr);
13336831982aSBarry Smith       } else {
133477431f27SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, using I-node routines\n",
1335d0f46423SBarry Smith                                                   rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);CHKERRQ(ierr);
13366831982aSBarry Smith       }
1337888f2ed8SSatish Balay       ierr = MatGetInfo(aij->A,MAT_LOCAL,&info);CHKERRQ(ierr);
133877431f27SBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr);
1339888f2ed8SSatish Balay       ierr = MatGetInfo(aij->B,MAT_LOCAL,&info);CHKERRQ(ierr);
134077431f27SBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr);
1341b0a32e0cSBarry Smith       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
13427b23a99aSBarry Smith       ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_FALSE);CHKERRQ(ierr);
134307d81ca4SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Information on VecScatter used in matrix-vector product: \n");CHKERRQ(ierr);
1344a40aa06bSLois Curfman McInnes       ierr = VecScatterView(aij->Mvctx,viewer);CHKERRQ(ierr);
13453a40ed3dSBarry Smith       PetscFunctionReturn(0);
1346fb9695e5SSatish Balay     } else if (format == PETSC_VIEWER_ASCII_INFO) {
1347923f20ffSKris Buschelman       PetscInt inodecount,inodelimit,*inodes;
1348923f20ffSKris Buschelman       ierr = MatInodeGetInodeSizes(aij->A,&inodecount,&inodes,&inodelimit);CHKERRQ(ierr);
1349923f20ffSKris Buschelman       if (inodes) {
1350923f20ffSKris Buschelman         ierr = PetscViewerASCIIPrintf(viewer,"using I-node (on process 0) routines: found %D nodes, limit used is %D\n",inodecount,inodelimit);CHKERRQ(ierr);
1351d38fa0fbSBarry Smith       } else {
1352d38fa0fbSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"not using I-node (on process 0) routines\n");CHKERRQ(ierr);
1353d38fa0fbSBarry Smith       }
13543a40ed3dSBarry Smith       PetscFunctionReturn(0);
13554aedb280SBarry Smith     } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
13564aedb280SBarry Smith       PetscFunctionReturn(0);
135708480c60SBarry Smith     }
13588e2fed03SBarry Smith   } else if (isbinary) {
13598e2fed03SBarry Smith     if (size == 1) {
13607adad957SLisandro Dalcin       ierr = PetscObjectSetName((PetscObject)aij->A,((PetscObject)mat)->name);CHKERRQ(ierr);
13618e2fed03SBarry Smith       ierr = MatView(aij->A,viewer);CHKERRQ(ierr);
13628e2fed03SBarry Smith     } else {
13638e2fed03SBarry Smith       ierr = MatView_MPIAIJ_Binary(mat,viewer);CHKERRQ(ierr);
13648e2fed03SBarry Smith     }
13658e2fed03SBarry Smith     PetscFunctionReturn(0);
13660f5bd95cSBarry Smith   } else if (isdraw) {
1367b0a32e0cSBarry Smith     PetscDraw draw;
1368ace3abfcSBarry Smith     PetscBool isnull;
1369b0a32e0cSBarry Smith     ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
1370b0a32e0cSBarry Smith     ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
137119bcc07fSBarry Smith   }
137219bcc07fSBarry Smith 
13737da1fb6eSBarry Smith   {
137495373324SBarry Smith     /* assemble the entire matrix onto first processor. */
137595373324SBarry Smith     Mat        A;
1376ec8511deSBarry Smith     Mat_SeqAIJ *Aloc;
1377d0f46423SBarry Smith     PetscInt   M = mat->rmap->N,N = mat->cmap->N,m,*ai,*aj,row,*cols,i,*ct;
1378dd6ea824SBarry Smith     MatScalar  *a;
13793e219373SBarry Smith     const char *matname;
13802ee70a88SLois Curfman McInnes 
1381ce94432eSBarry Smith     ierr = MatCreate(PetscObjectComm((PetscObject)mat),&A);CHKERRQ(ierr);
138217699dbbSLois Curfman McInnes     if (!rank) {
1383f69a0ea3SMatthew Knepley       ierr = MatSetSizes(A,M,N,M,N);CHKERRQ(ierr);
13843a40ed3dSBarry Smith     } else {
1385f69a0ea3SMatthew Knepley       ierr = MatSetSizes(A,0,0,M,N);CHKERRQ(ierr);
138695373324SBarry Smith     }
1387f204ca49SKris Buschelman     /* This is just a temporary matrix, so explicitly using MATMPIAIJ is probably best */
1388f204ca49SKris Buschelman     ierr = MatSetType(A,MATMPIAIJ);CHKERRQ(ierr);
13890298fd71SBarry Smith     ierr = MatMPIAIJSetPreallocation(A,0,NULL,0,NULL);CHKERRQ(ierr);
13902b82e772SSatish Balay     ierr = MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr);
13913bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)mat,(PetscObject)A);CHKERRQ(ierr);
1392416022c9SBarry Smith 
139395373324SBarry Smith     /* copy over the A part */
1394ec8511deSBarry Smith     Aloc = (Mat_SeqAIJ*)aij->A->data;
1395d0f46423SBarry Smith     m    = aij->A->rmap->n; ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
1396d0f46423SBarry Smith     row  = mat->rmap->rstart;
13972205254eSKarl Rupp     for (i=0; i<ai[m]; i++) aj[i] += mat->cmap->rstart;
139895373324SBarry Smith     for (i=0; i<m; i++) {
1399416022c9SBarry Smith       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);CHKERRQ(ierr);
140026fbe8dcSKarl Rupp       row++;
140126fbe8dcSKarl Rupp       a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
140295373324SBarry Smith     }
14032ee70a88SLois Curfman McInnes     aj = Aloc->j;
14042205254eSKarl Rupp     for (i=0; i<ai[m]; i++) aj[i] -= mat->cmap->rstart;
140595373324SBarry Smith 
140695373324SBarry Smith     /* copy over the B part */
1407ec8511deSBarry Smith     Aloc = (Mat_SeqAIJ*)aij->B->data;
1408d0f46423SBarry Smith     m    = aij->B->rmap->n;  ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
1409d0f46423SBarry Smith     row  = mat->rmap->rstart;
1410785e854fSJed Brown     ierr = PetscMalloc1((ai[m]+1),&cols);CHKERRQ(ierr);
1411b0a32e0cSBarry Smith     ct   = cols;
14122205254eSKarl Rupp     for (i=0; i<ai[m]; i++) cols[i] = aij->garray[aj[i]];
141395373324SBarry Smith     for (i=0; i<m; i++) {
1414416022c9SBarry Smith       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);CHKERRQ(ierr);
14152205254eSKarl Rupp       row++;
14162205254eSKarl Rupp       a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
141795373324SBarry Smith     }
1418606d414cSSatish Balay     ierr = PetscFree(ct);CHKERRQ(ierr);
14196d4a8577SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14206d4a8577SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
142155843e3eSBarry Smith     /*
142255843e3eSBarry Smith        Everyone has to call to draw the matrix since the graphics waits are
1423b0a32e0cSBarry Smith        synchronized across all processors that share the PetscDraw object
142455843e3eSBarry Smith     */
1425b0a32e0cSBarry Smith     ierr = PetscViewerGetSingleton(viewer,&sviewer);CHKERRQ(ierr);
1426ade3a672SBarry Smith     ierr = PetscObjectGetName((PetscObject)mat,&matname);CHKERRQ(ierr);
14273e219373SBarry Smith     if (!rank) {
1428ade3a672SBarry Smith       ierr = PetscObjectSetName((PetscObject)((Mat_MPIAIJ*)(A->data))->A,matname);CHKERRQ(ierr);
14297da1fb6eSBarry Smith       ierr = MatView_SeqAIJ(((Mat_MPIAIJ*)(A->data))->A,sviewer);CHKERRQ(ierr);
143095373324SBarry Smith     }
1431b0a32e0cSBarry Smith     ierr = PetscViewerRestoreSingleton(viewer,&sviewer);CHKERRQ(ierr);
14326bf464f9SBarry Smith     ierr = MatDestroy(&A);CHKERRQ(ierr);
143395373324SBarry Smith   }
14343a40ed3dSBarry Smith   PetscFunctionReturn(0);
14351eb62cbbSBarry Smith }
14361eb62cbbSBarry Smith 
14374a2ae208SSatish Balay #undef __FUNCT__
14384a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ"
1439dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ(Mat mat,PetscViewer viewer)
1440416022c9SBarry Smith {
1441dfbe8321SBarry Smith   PetscErrorCode ierr;
1442ace3abfcSBarry Smith   PetscBool      iascii,isdraw,issocket,isbinary;
1443416022c9SBarry Smith 
14443a40ed3dSBarry Smith   PetscFunctionBegin;
1445251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1446251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1447251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
1448251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSOCKET,&issocket);CHKERRQ(ierr);
144932077d6dSBarry Smith   if (iascii || isdraw || isbinary || issocket) {
14507b2a1423SBarry Smith     ierr = MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer);CHKERRQ(ierr);
1451416022c9SBarry Smith   }
14523a40ed3dSBarry Smith   PetscFunctionReturn(0);
1453416022c9SBarry Smith }
1454416022c9SBarry Smith 
14554a2ae208SSatish Balay #undef __FUNCT__
145641f059aeSBarry Smith #define __FUNCT__ "MatSOR_MPIAIJ"
145741f059aeSBarry Smith PetscErrorCode MatSOR_MPIAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
14588a729477SBarry Smith {
145944a69424SLois Curfman McInnes   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)matin->data;
1460dfbe8321SBarry Smith   PetscErrorCode ierr;
14616987fefcSBarry Smith   Vec            bb1 = 0;
1462ace3abfcSBarry Smith   PetscBool      hasop;
14638a729477SBarry Smith 
14643a40ed3dSBarry Smith   PetscFunctionBegin;
1465a2b30743SBarry Smith   if (flag == SOR_APPLY_UPPER) {
146641f059aeSBarry Smith     ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
1467a2b30743SBarry Smith     PetscFunctionReturn(0);
1468a2b30743SBarry Smith   }
1469a2b30743SBarry Smith 
14704e980039SJed Brown   if (its > 1 || ~flag & SOR_ZERO_INITIAL_GUESS || flag & SOR_EISENSTAT) {
14714e980039SJed Brown     ierr = VecDuplicate(bb,&bb1);CHKERRQ(ierr);
14724e980039SJed Brown   }
14734e980039SJed Brown 
1474c16cb8f2SBarry Smith   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP) {
1475da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
147641f059aeSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
14772798e883SHong Zhang       its--;
1478da3a660dSBarry Smith     }
14792798e883SHong Zhang 
14802798e883SHong Zhang     while (its--) {
1481ca9f406cSSatish Balay       ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1482ca9f406cSSatish Balay       ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
14832798e883SHong Zhang 
1484c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
1485efb30889SBarry Smith       ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr);
1486c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
14872798e883SHong Zhang 
1488c14dc6b6SHong Zhang       /* local sweep */
148941f059aeSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr);
14902798e883SHong Zhang     }
14913a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_FORWARD_SWEEP) {
1492da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
149341f059aeSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
14942798e883SHong Zhang       its--;
1495da3a660dSBarry Smith     }
14962798e883SHong Zhang     while (its--) {
1497ca9f406cSSatish Balay       ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1498ca9f406cSSatish Balay       ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
14992798e883SHong Zhang 
1500c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
1501efb30889SBarry Smith       ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr);
1502c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
1503c14dc6b6SHong Zhang 
1504c14dc6b6SHong Zhang       /* local sweep */
150541f059aeSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr);
15062798e883SHong Zhang     }
15073a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_BACKWARD_SWEEP) {
1508da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
150941f059aeSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
15102798e883SHong Zhang       its--;
1511da3a660dSBarry Smith     }
15122798e883SHong Zhang     while (its--) {
1513ca9f406cSSatish Balay       ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1514ca9f406cSSatish Balay       ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
15152798e883SHong Zhang 
1516c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
1517efb30889SBarry Smith       ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr);
1518c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
15192798e883SHong Zhang 
1520c14dc6b6SHong Zhang       /* local sweep */
152141f059aeSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr);
15222798e883SHong Zhang     }
1523a7420bb7SBarry Smith   } else if (flag & SOR_EISENSTAT) {
1524a7420bb7SBarry Smith     Vec xx1;
1525a7420bb7SBarry Smith 
1526a7420bb7SBarry Smith     ierr = VecDuplicate(bb,&xx1);CHKERRQ(ierr);
152741f059aeSBarry 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);
1528a7420bb7SBarry Smith 
1529a7420bb7SBarry Smith     ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1530a7420bb7SBarry Smith     ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1531a7420bb7SBarry Smith     if (!mat->diag) {
15320298fd71SBarry Smith       ierr = MatGetVecs(matin,&mat->diag,NULL);CHKERRQ(ierr);
1533a7420bb7SBarry Smith       ierr = MatGetDiagonal(matin,mat->diag);CHKERRQ(ierr);
1534a7420bb7SBarry Smith     }
1535bd0c2dcbSBarry Smith     ierr = MatHasOperation(matin,MATOP_MULT_DIAGONAL_BLOCK,&hasop);CHKERRQ(ierr);
1536bd0c2dcbSBarry Smith     if (hasop) {
1537bd0c2dcbSBarry Smith       ierr = MatMultDiagonalBlock(matin,xx,bb1);CHKERRQ(ierr);
1538bd0c2dcbSBarry Smith     } else {
1539a7420bb7SBarry Smith       ierr = VecPointwiseMult(bb1,mat->diag,xx);CHKERRQ(ierr);
1540bd0c2dcbSBarry Smith     }
1541887ee2caSBarry Smith     ierr = VecAYPX(bb1,(omega-2.0)/omega,bb);CHKERRQ(ierr);
1542887ee2caSBarry Smith 
1543a7420bb7SBarry Smith     ierr = MatMultAdd(mat->B,mat->lvec,bb1,bb1);CHKERRQ(ierr);
1544a7420bb7SBarry Smith 
1545a7420bb7SBarry Smith     /* local sweep */
154641f059aeSBarry 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);
1547a7420bb7SBarry Smith     ierr = VecAXPY(xx,1.0,xx1);CHKERRQ(ierr);
15486bf464f9SBarry Smith     ierr = VecDestroy(&xx1);CHKERRQ(ierr);
1549ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)matin),PETSC_ERR_SUP,"Parallel SOR not supported");
1550c14dc6b6SHong Zhang 
15516bf464f9SBarry Smith   ierr = VecDestroy(&bb1);CHKERRQ(ierr);
15523a40ed3dSBarry Smith   PetscFunctionReturn(0);
15538a729477SBarry Smith }
1554a66be287SLois Curfman McInnes 
15554a2ae208SSatish Balay #undef __FUNCT__
155642e855d1Svictor #define __FUNCT__ "MatPermute_MPIAIJ"
155742e855d1Svictor PetscErrorCode MatPermute_MPIAIJ(Mat A,IS rowp,IS colp,Mat *B)
155842e855d1Svictor {
155972e6a0cfSJed Brown   Mat            aA,aB,Aperm;
156072e6a0cfSJed Brown   const PetscInt *rwant,*cwant,*gcols,*ai,*bi,*aj,*bj;
156172e6a0cfSJed Brown   PetscScalar    *aa,*ba;
156272e6a0cfSJed Brown   PetscInt       i,j,m,n,ng,anz,bnz,*dnnz,*onnz,*tdnnz,*tonnz,*rdest,*cdest,*work,*gcdest;
156372e6a0cfSJed Brown   PetscSF        rowsf,sf;
15640298fd71SBarry Smith   IS             parcolp = NULL;
156572e6a0cfSJed Brown   PetscBool      done;
156642e855d1Svictor   PetscErrorCode ierr;
156742e855d1Svictor 
156842e855d1Svictor   PetscFunctionBegin;
156972e6a0cfSJed Brown   ierr = MatGetLocalSize(A,&m,&n);CHKERRQ(ierr);
157072e6a0cfSJed Brown   ierr = ISGetIndices(rowp,&rwant);CHKERRQ(ierr);
157172e6a0cfSJed Brown   ierr = ISGetIndices(colp,&cwant);CHKERRQ(ierr);
1572dcca6d9dSJed Brown   ierr = PetscMalloc3(PetscMax(m,n),&work,m,&rdest,n,&cdest);CHKERRQ(ierr);
157372e6a0cfSJed Brown 
157472e6a0cfSJed Brown   /* Invert row permutation to find out where my rows should go */
1575ce94432eSBarry Smith   ierr = PetscSFCreate(PetscObjectComm((PetscObject)A),&rowsf);CHKERRQ(ierr);
15760298fd71SBarry Smith   ierr = PetscSFSetGraphLayout(rowsf,A->rmap,A->rmap->n,NULL,PETSC_OWN_POINTER,rwant);CHKERRQ(ierr);
1577e9e74f11SJed Brown   ierr = PetscSFSetFromOptions(rowsf);CHKERRQ(ierr);
157872e6a0cfSJed Brown   for (i=0; i<m; i++) work[i] = A->rmap->rstart + i;
15798bfbc91cSJed Brown   ierr = PetscSFReduceBegin(rowsf,MPIU_INT,work,rdest,MPIU_REPLACE);CHKERRQ(ierr);
15808bfbc91cSJed Brown   ierr = PetscSFReduceEnd(rowsf,MPIU_INT,work,rdest,MPIU_REPLACE);CHKERRQ(ierr);
158172e6a0cfSJed Brown 
158272e6a0cfSJed Brown   /* Invert column permutation to find out where my columns should go */
1583ce94432eSBarry Smith   ierr = PetscSFCreate(PetscObjectComm((PetscObject)A),&sf);CHKERRQ(ierr);
15840298fd71SBarry Smith   ierr = PetscSFSetGraphLayout(sf,A->cmap,A->cmap->n,NULL,PETSC_OWN_POINTER,cwant);CHKERRQ(ierr);
1585e9e74f11SJed Brown   ierr = PetscSFSetFromOptions(sf);CHKERRQ(ierr);
158672e6a0cfSJed Brown   for (i=0; i<n; i++) work[i] = A->cmap->rstart + i;
15878bfbc91cSJed Brown   ierr = PetscSFReduceBegin(sf,MPIU_INT,work,cdest,MPIU_REPLACE);CHKERRQ(ierr);
15888bfbc91cSJed Brown   ierr = PetscSFReduceEnd(sf,MPIU_INT,work,cdest,MPIU_REPLACE);CHKERRQ(ierr);
158972e6a0cfSJed Brown   ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
159072e6a0cfSJed Brown 
159172e6a0cfSJed Brown   ierr = ISRestoreIndices(rowp,&rwant);CHKERRQ(ierr);
159272e6a0cfSJed Brown   ierr = ISRestoreIndices(colp,&cwant);CHKERRQ(ierr);
159372e6a0cfSJed Brown   ierr = MatMPIAIJGetSeqAIJ(A,&aA,&aB,&gcols);CHKERRQ(ierr);
159472e6a0cfSJed Brown 
159572e6a0cfSJed Brown   /* Find out where my gcols should go */
15960298fd71SBarry Smith   ierr = MatGetSize(aB,NULL,&ng);CHKERRQ(ierr);
1597785e854fSJed Brown   ierr = PetscMalloc1(ng,&gcdest);CHKERRQ(ierr);
1598ce94432eSBarry Smith   ierr = PetscSFCreate(PetscObjectComm((PetscObject)A),&sf);CHKERRQ(ierr);
15990298fd71SBarry Smith   ierr = PetscSFSetGraphLayout(sf,A->cmap,ng,NULL,PETSC_OWN_POINTER,gcols);CHKERRQ(ierr);
1600e9e74f11SJed Brown   ierr = PetscSFSetFromOptions(sf);CHKERRQ(ierr);
160172e6a0cfSJed Brown   ierr = PetscSFBcastBegin(sf,MPIU_INT,cdest,gcdest);CHKERRQ(ierr);
160272e6a0cfSJed Brown   ierr = PetscSFBcastEnd(sf,MPIU_INT,cdest,gcdest);CHKERRQ(ierr);
160372e6a0cfSJed Brown   ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
160472e6a0cfSJed Brown 
16051795a4d1SJed Brown   ierr = PetscCalloc4(m,&dnnz,m,&onnz,m,&tdnnz,m,&tonnz);CHKERRQ(ierr);
160672e6a0cfSJed Brown   ierr = MatGetRowIJ(aA,0,PETSC_FALSE,PETSC_FALSE,&anz,&ai,&aj,&done);CHKERRQ(ierr);
160772e6a0cfSJed Brown   ierr = MatGetRowIJ(aB,0,PETSC_FALSE,PETSC_FALSE,&bnz,&bi,&bj,&done);CHKERRQ(ierr);
160872e6a0cfSJed Brown   for (i=0; i<m; i++) {
160972e6a0cfSJed Brown     PetscInt row = rdest[i],rowner;
161072e6a0cfSJed Brown     ierr = PetscLayoutFindOwner(A->rmap,row,&rowner);CHKERRQ(ierr);
161172e6a0cfSJed Brown     for (j=ai[i]; j<ai[i+1]; j++) {
161272e6a0cfSJed Brown       PetscInt cowner,col = cdest[aj[j]];
161372e6a0cfSJed Brown       ierr = PetscLayoutFindOwner(A->cmap,col,&cowner);CHKERRQ(ierr); /* Could build an index for the columns to eliminate this search */
161472e6a0cfSJed Brown       if (rowner == cowner) dnnz[i]++;
161572e6a0cfSJed Brown       else onnz[i]++;
161672e6a0cfSJed Brown     }
161772e6a0cfSJed Brown     for (j=bi[i]; j<bi[i+1]; j++) {
161872e6a0cfSJed Brown       PetscInt cowner,col = gcdest[bj[j]];
161972e6a0cfSJed Brown       ierr = PetscLayoutFindOwner(A->cmap,col,&cowner);CHKERRQ(ierr);
162072e6a0cfSJed Brown       if (rowner == cowner) dnnz[i]++;
162172e6a0cfSJed Brown       else onnz[i]++;
162272e6a0cfSJed Brown     }
162372e6a0cfSJed Brown   }
162472e6a0cfSJed Brown   ierr = PetscSFBcastBegin(rowsf,MPIU_INT,dnnz,tdnnz);CHKERRQ(ierr);
162572e6a0cfSJed Brown   ierr = PetscSFBcastEnd(rowsf,MPIU_INT,dnnz,tdnnz);CHKERRQ(ierr);
162672e6a0cfSJed Brown   ierr = PetscSFBcastBegin(rowsf,MPIU_INT,onnz,tonnz);CHKERRQ(ierr);
162772e6a0cfSJed Brown   ierr = PetscSFBcastEnd(rowsf,MPIU_INT,onnz,tonnz);CHKERRQ(ierr);
162872e6a0cfSJed Brown   ierr = PetscSFDestroy(&rowsf);CHKERRQ(ierr);
162972e6a0cfSJed Brown 
1630ce94432eSBarry Smith   ierr = MatCreateAIJ(PetscObjectComm((PetscObject)A),A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N,0,tdnnz,0,tonnz,&Aperm);CHKERRQ(ierr);
163172e6a0cfSJed Brown   ierr = MatSeqAIJGetArray(aA,&aa);CHKERRQ(ierr);
163272e6a0cfSJed Brown   ierr = MatSeqAIJGetArray(aB,&ba);CHKERRQ(ierr);
163372e6a0cfSJed Brown   for (i=0; i<m; i++) {
163472e6a0cfSJed Brown     PetscInt *acols = dnnz,*bcols = onnz; /* Repurpose now-unneeded arrays */
1635970468b0SJed Brown     PetscInt j0,rowlen;
163672e6a0cfSJed Brown     rowlen = ai[i+1] - ai[i];
1637970468b0SJed Brown     for (j0=j=0; j<rowlen; j0=j) { /* rowlen could be larger than number of rows m, so sum in batches */
1638970468b0SJed Brown       for ( ; j<PetscMin(rowlen,j0+m); j++) acols[j-j0] = cdest[aj[ai[i]+j]];
1639970468b0SJed Brown       ierr = MatSetValues(Aperm,1,&rdest[i],j-j0,acols,aa+ai[i]+j0,INSERT_VALUES);CHKERRQ(ierr);
1640970468b0SJed Brown     }
164172e6a0cfSJed Brown     rowlen = bi[i+1] - bi[i];
1642970468b0SJed Brown     for (j0=j=0; j<rowlen; j0=j) {
1643970468b0SJed Brown       for ( ; j<PetscMin(rowlen,j0+m); j++) bcols[j-j0] = gcdest[bj[bi[i]+j]];
1644970468b0SJed Brown       ierr = MatSetValues(Aperm,1,&rdest[i],j-j0,bcols,ba+bi[i]+j0,INSERT_VALUES);CHKERRQ(ierr);
1645970468b0SJed Brown     }
164672e6a0cfSJed Brown   }
164772e6a0cfSJed Brown   ierr = MatAssemblyBegin(Aperm,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
164872e6a0cfSJed Brown   ierr = MatAssemblyEnd(Aperm,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
164972e6a0cfSJed Brown   ierr = MatRestoreRowIJ(aA,0,PETSC_FALSE,PETSC_FALSE,&anz,&ai,&aj,&done);CHKERRQ(ierr);
165072e6a0cfSJed Brown   ierr = MatRestoreRowIJ(aB,0,PETSC_FALSE,PETSC_FALSE,&bnz,&bi,&bj,&done);CHKERRQ(ierr);
165172e6a0cfSJed Brown   ierr = MatSeqAIJRestoreArray(aA,&aa);CHKERRQ(ierr);
165272e6a0cfSJed Brown   ierr = MatSeqAIJRestoreArray(aB,&ba);CHKERRQ(ierr);
165372e6a0cfSJed Brown   ierr = PetscFree4(dnnz,onnz,tdnnz,tonnz);CHKERRQ(ierr);
165472e6a0cfSJed Brown   ierr = PetscFree3(work,rdest,cdest);CHKERRQ(ierr);
165572e6a0cfSJed Brown   ierr = PetscFree(gcdest);CHKERRQ(ierr);
165672e6a0cfSJed Brown   if (parcolp) {ierr = ISDestroy(&colp);CHKERRQ(ierr);}
165772e6a0cfSJed Brown   *B = Aperm;
165842e855d1Svictor   PetscFunctionReturn(0);
165942e855d1Svictor }
166042e855d1Svictor 
166142e855d1Svictor #undef __FUNCT__
16624a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_MPIAIJ"
1663dfbe8321SBarry Smith PetscErrorCode MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info)
1664a66be287SLois Curfman McInnes {
1665a66be287SLois Curfman McInnes   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)matin->data;
1666a66be287SLois Curfman McInnes   Mat            A    = mat->A,B = mat->B;
1667dfbe8321SBarry Smith   PetscErrorCode ierr;
1668329f5518SBarry Smith   PetscReal      isend[5],irecv[5];
1669a66be287SLois Curfman McInnes 
16703a40ed3dSBarry Smith   PetscFunctionBegin;
16714e220ebcSLois Curfman McInnes   info->block_size = 1.0;
16724e220ebcSLois Curfman McInnes   ierr             = MatGetInfo(A,MAT_LOCAL,info);CHKERRQ(ierr);
16732205254eSKarl Rupp 
16744e220ebcSLois Curfman McInnes   isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
16754e220ebcSLois Curfman McInnes   isend[3] = info->memory;  isend[4] = info->mallocs;
16762205254eSKarl Rupp 
16774e220ebcSLois Curfman McInnes   ierr = MatGetInfo(B,MAT_LOCAL,info);CHKERRQ(ierr);
16782205254eSKarl Rupp 
16794e220ebcSLois Curfman McInnes   isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
16804e220ebcSLois Curfman McInnes   isend[3] += info->memory;  isend[4] += info->mallocs;
1681a66be287SLois Curfman McInnes   if (flag == MAT_LOCAL) {
16824e220ebcSLois Curfman McInnes     info->nz_used      = isend[0];
16834e220ebcSLois Curfman McInnes     info->nz_allocated = isend[1];
16844e220ebcSLois Curfman McInnes     info->nz_unneeded  = isend[2];
16854e220ebcSLois Curfman McInnes     info->memory       = isend[3];
16864e220ebcSLois Curfman McInnes     info->mallocs      = isend[4];
1687a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_MAX) {
1688ce94432eSBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)matin));CHKERRQ(ierr);
16892205254eSKarl Rupp 
16904e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
16914e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
16924e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
16934e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
16944e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1695a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_SUM) {
1696ce94432eSBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)matin));CHKERRQ(ierr);
16972205254eSKarl Rupp 
16984e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
16994e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
17004e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
17014e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
17024e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1703a66be287SLois Curfman McInnes   }
17044e220ebcSLois Curfman McInnes   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
17054e220ebcSLois Curfman McInnes   info->fill_ratio_needed = 0;
17064e220ebcSLois Curfman McInnes   info->factor_mallocs    = 0;
17073a40ed3dSBarry Smith   PetscFunctionReturn(0);
1708a66be287SLois Curfman McInnes }
1709a66be287SLois Curfman McInnes 
17104a2ae208SSatish Balay #undef __FUNCT__
17114a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_MPIAIJ"
1712ace3abfcSBarry Smith PetscErrorCode MatSetOption_MPIAIJ(Mat A,MatOption op,PetscBool flg)
1713c74985f6SBarry Smith {
1714c0bbcb79SLois Curfman McInnes   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
1715dfbe8321SBarry Smith   PetscErrorCode ierr;
1716c74985f6SBarry Smith 
17173a40ed3dSBarry Smith   PetscFunctionBegin;
171812c028f9SKris Buschelman   switch (op) {
1719512a5fc5SBarry Smith   case MAT_NEW_NONZERO_LOCATIONS:
172012c028f9SKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
172128b2fa4aSMatthew Knepley   case MAT_UNUSED_NONZERO_LOCATION_ERR:
1722a9817697SBarry Smith   case MAT_KEEP_NONZERO_PATTERN:
172312c028f9SKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
172412c028f9SKris Buschelman   case MAT_USE_INODES:
172512c028f9SKris Buschelman   case MAT_IGNORE_ZERO_ENTRIES:
1726fa1f0d2cSMatthew G Knepley     MatCheckPreallocated(A,1);
17274e0d8c25SBarry Smith     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
17284e0d8c25SBarry Smith     ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr);
172912c028f9SKris Buschelman     break;
173012c028f9SKris Buschelman   case MAT_ROW_ORIENTED:
17314e0d8c25SBarry Smith     a->roworiented = flg;
17322205254eSKarl Rupp 
17334e0d8c25SBarry Smith     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
17344e0d8c25SBarry Smith     ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr);
173512c028f9SKris Buschelman     break;
17364e0d8c25SBarry Smith   case MAT_NEW_DIAGONALS:
1737290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
173812c028f9SKris Buschelman     break;
173912c028f9SKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
17405c0f0b64SBarry Smith     a->donotstash = flg;
174112c028f9SKris Buschelman     break;
1742ffa07934SHong Zhang   case MAT_SPD:
1743ffa07934SHong Zhang     A->spd_set = PETSC_TRUE;
1744ffa07934SHong Zhang     A->spd     = flg;
1745ffa07934SHong Zhang     if (flg) {
1746ffa07934SHong Zhang       A->symmetric                  = PETSC_TRUE;
1747ffa07934SHong Zhang       A->structurally_symmetric     = PETSC_TRUE;
1748ffa07934SHong Zhang       A->symmetric_set              = PETSC_TRUE;
1749ffa07934SHong Zhang       A->structurally_symmetric_set = PETSC_TRUE;
1750ffa07934SHong Zhang     }
1751ffa07934SHong Zhang     break;
175277e54ba9SKris Buschelman   case MAT_SYMMETRIC:
17534e0d8c25SBarry Smith     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
175425f421beSHong Zhang     break;
175577e54ba9SKris Buschelman   case MAT_STRUCTURALLY_SYMMETRIC:
1756eeffb40dSHong Zhang     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
1757eeffb40dSHong Zhang     break;
1758bf108f30SBarry Smith   case MAT_HERMITIAN:
1759eeffb40dSHong Zhang     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
1760eeffb40dSHong Zhang     break;
1761bf108f30SBarry Smith   case MAT_SYMMETRY_ETERNAL:
17624e0d8c25SBarry Smith     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
176377e54ba9SKris Buschelman     break;
176412c028f9SKris Buschelman   default:
1765e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
17663a40ed3dSBarry Smith   }
17673a40ed3dSBarry Smith   PetscFunctionReturn(0);
1768c74985f6SBarry Smith }
1769c74985f6SBarry Smith 
17704a2ae208SSatish Balay #undef __FUNCT__
17714a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_MPIAIJ"
1772b1d57f15SBarry Smith PetscErrorCode MatGetRow_MPIAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
177339e00950SLois Curfman McInnes {
1774154123eaSLois Curfman McInnes   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)matin->data;
177587828ca2SBarry Smith   PetscScalar    *vworkA,*vworkB,**pvA,**pvB,*v_p;
17766849ba73SBarry Smith   PetscErrorCode ierr;
1777d0f46423SBarry Smith   PetscInt       i,*cworkA,*cworkB,**pcA,**pcB,cstart = matin->cmap->rstart;
1778d0f46423SBarry Smith   PetscInt       nztot,nzA,nzB,lrow,rstart = matin->rmap->rstart,rend = matin->rmap->rend;
1779b1d57f15SBarry Smith   PetscInt       *cmap,*idx_p;
178039e00950SLois Curfman McInnes 
17813a40ed3dSBarry Smith   PetscFunctionBegin;
1782e32f2f54SBarry Smith   if (mat->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Already active");
17837a0afa10SBarry Smith   mat->getrowactive = PETSC_TRUE;
17847a0afa10SBarry Smith 
178570f0671dSBarry Smith   if (!mat->rowvalues && (idx || v)) {
17867a0afa10SBarry Smith     /*
17877a0afa10SBarry Smith         allocate enough space to hold information from the longest row.
17887a0afa10SBarry Smith     */
17897a0afa10SBarry Smith     Mat_SeqAIJ *Aa = (Mat_SeqAIJ*)mat->A->data,*Ba = (Mat_SeqAIJ*)mat->B->data;
1790b1d57f15SBarry Smith     PetscInt   max = 1,tmp;
1791d0f46423SBarry Smith     for (i=0; i<matin->rmap->n; i++) {
17927a0afa10SBarry Smith       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
17932205254eSKarl Rupp       if (max < tmp) max = tmp;
17947a0afa10SBarry Smith     }
1795dcca6d9dSJed Brown     ierr = PetscMalloc2(max,&mat->rowvalues,max,&mat->rowindices);CHKERRQ(ierr);
17967a0afa10SBarry Smith   }
17977a0afa10SBarry Smith 
1798e7e72b3dSBarry Smith   if (row < rstart || row >= rend) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only local rows");
1799abc0e9e4SLois Curfman McInnes   lrow = row - rstart;
180039e00950SLois Curfman McInnes 
1801154123eaSLois Curfman McInnes   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1802154123eaSLois Curfman McInnes   if (!v)   {pvA = 0; pvB = 0;}
1803154123eaSLois Curfman McInnes   if (!idx) {pcA = 0; if (!v) pcB = 0;}
1804f830108cSBarry Smith   ierr  = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1805f830108cSBarry Smith   ierr  = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
1806154123eaSLois Curfman McInnes   nztot = nzA + nzB;
1807154123eaSLois Curfman McInnes 
180870f0671dSBarry Smith   cmap = mat->garray;
1809154123eaSLois Curfman McInnes   if (v  || idx) {
1810154123eaSLois Curfman McInnes     if (nztot) {
1811154123eaSLois Curfman McInnes       /* Sort by increasing column numbers, assuming A and B already sorted */
1812b1d57f15SBarry Smith       PetscInt imark = -1;
1813154123eaSLois Curfman McInnes       if (v) {
181470f0671dSBarry Smith         *v = v_p = mat->rowvalues;
181539e00950SLois Curfman McInnes         for (i=0; i<nzB; i++) {
181670f0671dSBarry Smith           if (cmap[cworkB[i]] < cstart) v_p[i] = vworkB[i];
1817154123eaSLois Curfman McInnes           else break;
1818154123eaSLois Curfman McInnes         }
1819154123eaSLois Curfman McInnes         imark = i;
182070f0671dSBarry Smith         for (i=0; i<nzA; i++)     v_p[imark+i] = vworkA[i];
182170f0671dSBarry Smith         for (i=imark; i<nzB; i++) v_p[nzA+i]   = vworkB[i];
1822154123eaSLois Curfman McInnes       }
1823154123eaSLois Curfman McInnes       if (idx) {
182470f0671dSBarry Smith         *idx = idx_p = mat->rowindices;
182570f0671dSBarry Smith         if (imark > -1) {
182670f0671dSBarry Smith           for (i=0; i<imark; i++) {
182770f0671dSBarry Smith             idx_p[i] = cmap[cworkB[i]];
182870f0671dSBarry Smith           }
182970f0671dSBarry Smith         } else {
1830154123eaSLois Curfman McInnes           for (i=0; i<nzB; i++) {
183170f0671dSBarry Smith             if (cmap[cworkB[i]] < cstart) idx_p[i] = cmap[cworkB[i]];
1832154123eaSLois Curfman McInnes             else break;
1833154123eaSLois Curfman McInnes           }
1834154123eaSLois Curfman McInnes           imark = i;
183570f0671dSBarry Smith         }
183670f0671dSBarry Smith         for (i=0; i<nzA; i++)     idx_p[imark+i] = cstart + cworkA[i];
183770f0671dSBarry Smith         for (i=imark; i<nzB; i++) idx_p[nzA+i]   = cmap[cworkB[i]];
183839e00950SLois Curfman McInnes       }
18393f97c4b0SBarry Smith     } else {
18401ca473b0SSatish Balay       if (idx) *idx = 0;
18411ca473b0SSatish Balay       if (v)   *v   = 0;
18421ca473b0SSatish Balay     }
1843154123eaSLois Curfman McInnes   }
184439e00950SLois Curfman McInnes   *nz  = nztot;
1845f830108cSBarry Smith   ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1846f830108cSBarry Smith   ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
18473a40ed3dSBarry Smith   PetscFunctionReturn(0);
184839e00950SLois Curfman McInnes }
184939e00950SLois Curfman McInnes 
18504a2ae208SSatish Balay #undef __FUNCT__
18514a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_MPIAIJ"
1852b1d57f15SBarry Smith PetscErrorCode MatRestoreRow_MPIAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
185339e00950SLois Curfman McInnes {
18547a0afa10SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
18553a40ed3dSBarry Smith 
18563a40ed3dSBarry Smith   PetscFunctionBegin;
1857e7e72b3dSBarry Smith   if (!aij->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"MatGetRow() must be called first");
18587a0afa10SBarry Smith   aij->getrowactive = PETSC_FALSE;
18593a40ed3dSBarry Smith   PetscFunctionReturn(0);
186039e00950SLois Curfman McInnes }
186139e00950SLois Curfman McInnes 
18624a2ae208SSatish Balay #undef __FUNCT__
18634a2ae208SSatish Balay #define __FUNCT__ "MatNorm_MPIAIJ"
1864dfbe8321SBarry Smith PetscErrorCode MatNorm_MPIAIJ(Mat mat,NormType type,PetscReal *norm)
1865855ac2c5SLois Curfman McInnes {
1866855ac2c5SLois Curfman McInnes   Mat_MPIAIJ     *aij  = (Mat_MPIAIJ*)mat->data;
1867ec8511deSBarry Smith   Mat_SeqAIJ     *amat = (Mat_SeqAIJ*)aij->A->data,*bmat = (Mat_SeqAIJ*)aij->B->data;
1868dfbe8321SBarry Smith   PetscErrorCode ierr;
1869d0f46423SBarry Smith   PetscInt       i,j,cstart = mat->cmap->rstart;
1870329f5518SBarry Smith   PetscReal      sum = 0.0;
1871a77337e4SBarry Smith   MatScalar      *v;
187204ca555eSLois Curfman McInnes 
18733a40ed3dSBarry Smith   PetscFunctionBegin;
187417699dbbSLois Curfman McInnes   if (aij->size == 1) {
187514183eadSLois Curfman McInnes     ierr =  MatNorm(aij->A,type,norm);CHKERRQ(ierr);
187637fa93a5SLois Curfman McInnes   } else {
187704ca555eSLois Curfman McInnes     if (type == NORM_FROBENIUS) {
187804ca555eSLois Curfman McInnes       v = amat->a;
187904ca555eSLois Curfman McInnes       for (i=0; i<amat->nz; i++) {
1880329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
188104ca555eSLois Curfman McInnes       }
188204ca555eSLois Curfman McInnes       v = bmat->a;
188304ca555eSLois Curfman McInnes       for (i=0; i<bmat->nz; i++) {
1884329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
188504ca555eSLois Curfman McInnes       }
1886ce94432eSBarry Smith       ierr  = MPI_Allreduce(&sum,norm,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
18878f1a2a5eSBarry Smith       *norm = PetscSqrtReal(*norm);
18883a40ed3dSBarry Smith     } else if (type == NORM_1) { /* max column norm */
1889329f5518SBarry Smith       PetscReal *tmp,*tmp2;
1890b1d57f15SBarry Smith       PetscInt  *jj,*garray = aij->garray;
18911795a4d1SJed Brown       ierr  = PetscCalloc1((mat->cmap->N+1),&tmp);CHKERRQ(ierr);
1892785e854fSJed Brown       ierr  = PetscMalloc1((mat->cmap->N+1),&tmp2);CHKERRQ(ierr);
189304ca555eSLois Curfman McInnes       *norm = 0.0;
189404ca555eSLois Curfman McInnes       v     = amat->a; jj = amat->j;
189504ca555eSLois Curfman McInnes       for (j=0; j<amat->nz; j++) {
1896bfec09a0SHong Zhang         tmp[cstart + *jj++] += PetscAbsScalar(*v);  v++;
189704ca555eSLois Curfman McInnes       }
189804ca555eSLois Curfman McInnes       v = bmat->a; jj = bmat->j;
189904ca555eSLois Curfman McInnes       for (j=0; j<bmat->nz; j++) {
1900bfec09a0SHong Zhang         tmp[garray[*jj++]] += PetscAbsScalar(*v); v++;
190104ca555eSLois Curfman McInnes       }
1902ce94432eSBarry Smith       ierr = MPI_Allreduce(tmp,tmp2,mat->cmap->N,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1903d0f46423SBarry Smith       for (j=0; j<mat->cmap->N; j++) {
190404ca555eSLois Curfman McInnes         if (tmp2[j] > *norm) *norm = tmp2[j];
190504ca555eSLois Curfman McInnes       }
1906606d414cSSatish Balay       ierr = PetscFree(tmp);CHKERRQ(ierr);
1907606d414cSSatish Balay       ierr = PetscFree(tmp2);CHKERRQ(ierr);
19083a40ed3dSBarry Smith     } else if (type == NORM_INFINITY) { /* max row norm */
1909329f5518SBarry Smith       PetscReal ntemp = 0.0;
1910d0f46423SBarry Smith       for (j=0; j<aij->A->rmap->n; j++) {
1911bfec09a0SHong Zhang         v   = amat->a + amat->i[j];
191204ca555eSLois Curfman McInnes         sum = 0.0;
191304ca555eSLois Curfman McInnes         for (i=0; i<amat->i[j+1]-amat->i[j]; i++) {
1914cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
191504ca555eSLois Curfman McInnes         }
1916bfec09a0SHong Zhang         v = bmat->a + bmat->i[j];
191704ca555eSLois Curfman McInnes         for (i=0; i<bmat->i[j+1]-bmat->i[j]; i++) {
1918cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
191904ca555eSLois Curfman McInnes         }
1920515d9167SLois Curfman McInnes         if (sum > ntemp) ntemp = sum;
192104ca555eSLois Curfman McInnes       }
1922ce94432eSBarry Smith       ierr = MPI_Allreduce(&ntemp,norm,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1923ce94432eSBarry Smith     } else SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"No support for two norm");
192437fa93a5SLois Curfman McInnes   }
19253a40ed3dSBarry Smith   PetscFunctionReturn(0);
1926855ac2c5SLois Curfman McInnes }
1927855ac2c5SLois Curfman McInnes 
19284a2ae208SSatish Balay #undef __FUNCT__
19294a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_MPIAIJ"
1930fc4dec0aSBarry Smith PetscErrorCode MatTranspose_MPIAIJ(Mat A,MatReuse reuse,Mat *matout)
1931b7c46309SBarry Smith {
1932b7c46309SBarry Smith   Mat_MPIAIJ     *a   = (Mat_MPIAIJ*)A->data;
1933da668accSHong Zhang   Mat_SeqAIJ     *Aloc=(Mat_SeqAIJ*)a->A->data,*Bloc=(Mat_SeqAIJ*)a->B->data;
1934dfbe8321SBarry Smith   PetscErrorCode ierr;
193580bcc5a1SJed Brown   PetscInt       M      = A->rmap->N,N = A->cmap->N,ma,na,mb,nb,*ai,*aj,*bi,*bj,row,*cols,*cols_tmp,i;
1936d0f46423SBarry Smith   PetscInt       cstart = A->cmap->rstart,ncol;
19373a40ed3dSBarry Smith   Mat            B;
1938a77337e4SBarry Smith   MatScalar      *array;
1939b7c46309SBarry Smith 
19403a40ed3dSBarry Smith   PetscFunctionBegin;
1941ce94432eSBarry Smith   if (reuse == MAT_REUSE_MATRIX && A == *matout && M != N) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1942da668accSHong Zhang 
194380bcc5a1SJed Brown   ma = A->rmap->n; na = A->cmap->n; mb = a->B->rmap->n; nb = a->B->cmap->n;
1944da668accSHong Zhang   ai = Aloc->i; aj = Aloc->j;
1945da668accSHong Zhang   bi = Bloc->i; bj = Bloc->j;
1946fc73b1b3SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *matout == A) {
194780bcc5a1SJed Brown     PetscInt             *d_nnz,*g_nnz,*o_nnz;
194880bcc5a1SJed Brown     PetscSFNode          *oloc;
1949713c93b4SJed Brown     PETSC_UNUSED PetscSF sf;
195080bcc5a1SJed Brown 
1951dcca6d9dSJed Brown     ierr = PetscMalloc4(na,&d_nnz,na,&o_nnz,nb,&g_nnz,nb,&oloc);CHKERRQ(ierr);
195280bcc5a1SJed Brown     /* compute d_nnz for preallocation */
195380bcc5a1SJed Brown     ierr = PetscMemzero(d_nnz,na*sizeof(PetscInt));CHKERRQ(ierr);
1954da668accSHong Zhang     for (i=0; i<ai[ma]; i++) {
1955da668accSHong Zhang       d_nnz[aj[i]]++;
1956da668accSHong Zhang       aj[i] += cstart; /* global col index to be used by MatSetValues() */
1957d4bb536fSBarry Smith     }
195880bcc5a1SJed Brown     /* compute local off-diagonal contributions */
19590beca09bSJed Brown     ierr = PetscMemzero(g_nnz,nb*sizeof(PetscInt));CHKERRQ(ierr);
196080bcc5a1SJed Brown     for (i=0; i<bi[ma]; i++) g_nnz[bj[i]]++;
196180bcc5a1SJed Brown     /* map those to global */
1962ce94432eSBarry Smith     ierr = PetscSFCreate(PetscObjectComm((PetscObject)A),&sf);CHKERRQ(ierr);
19630298fd71SBarry Smith     ierr = PetscSFSetGraphLayout(sf,A->cmap,nb,NULL,PETSC_USE_POINTER,a->garray);CHKERRQ(ierr);
1964e9e74f11SJed Brown     ierr = PetscSFSetFromOptions(sf);CHKERRQ(ierr);
196580bcc5a1SJed Brown     ierr = PetscMemzero(o_nnz,na*sizeof(PetscInt));CHKERRQ(ierr);
196680bcc5a1SJed Brown     ierr = PetscSFReduceBegin(sf,MPIU_INT,g_nnz,o_nnz,MPIU_SUM);CHKERRQ(ierr);
196780bcc5a1SJed Brown     ierr = PetscSFReduceEnd(sf,MPIU_INT,g_nnz,o_nnz,MPIU_SUM);CHKERRQ(ierr);
196880bcc5a1SJed Brown     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
1969d4bb536fSBarry Smith 
1970ce94432eSBarry Smith     ierr = MatCreate(PetscObjectComm((PetscObject)A),&B);CHKERRQ(ierr);
1971d0f46423SBarry Smith     ierr = MatSetSizes(B,A->cmap->n,A->rmap->n,N,M);CHKERRQ(ierr);
197233d57670SJed Brown     ierr = MatSetBlockSizes(B,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));CHKERRQ(ierr);
19737adad957SLisandro Dalcin     ierr = MatSetType(B,((PetscObject)A)->type_name);CHKERRQ(ierr);
197480bcc5a1SJed Brown     ierr = MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);CHKERRQ(ierr);
197580bcc5a1SJed Brown     ierr = PetscFree4(d_nnz,o_nnz,g_nnz,oloc);CHKERRQ(ierr);
1976fc4dec0aSBarry Smith   } else {
1977fc4dec0aSBarry Smith     B    = *matout;
19786ffab4bbSHong Zhang     ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
19792205254eSKarl Rupp     for (i=0; i<ai[ma]; i++) aj[i] += cstart; /* global col index to be used by MatSetValues() */
1980fc4dec0aSBarry Smith   }
1981b7c46309SBarry Smith 
1982b7c46309SBarry Smith   /* copy over the A part */
1983da668accSHong Zhang   array = Aloc->a;
1984d0f46423SBarry Smith   row   = A->rmap->rstart;
1985da668accSHong Zhang   for (i=0; i<ma; i++) {
1986da668accSHong Zhang     ncol = ai[i+1]-ai[i];
1987da668accSHong Zhang     ierr = MatSetValues(B,ncol,aj,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
19882205254eSKarl Rupp     row++;
19892205254eSKarl Rupp     array += ncol; aj += ncol;
1990b7c46309SBarry Smith   }
1991b7c46309SBarry Smith   aj = Aloc->j;
1992da668accSHong Zhang   for (i=0; i<ai[ma]; i++) aj[i] -= cstart; /* resume local col index */
1993b7c46309SBarry Smith 
1994b7c46309SBarry Smith   /* copy over the B part */
19951795a4d1SJed Brown   ierr  = PetscCalloc1(bi[mb],&cols);CHKERRQ(ierr);
1996da668accSHong Zhang   array = Bloc->a;
1997d0f46423SBarry Smith   row   = A->rmap->rstart;
19982205254eSKarl Rupp   for (i=0; i<bi[mb]; i++) cols[i] = a->garray[bj[i]];
199961a2fbbaSHong Zhang   cols_tmp = cols;
2000da668accSHong Zhang   for (i=0; i<mb; i++) {
2001da668accSHong Zhang     ncol = bi[i+1]-bi[i];
200261a2fbbaSHong Zhang     ierr = MatSetValues(B,ncol,cols_tmp,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
20032205254eSKarl Rupp     row++;
20042205254eSKarl Rupp     array += ncol; cols_tmp += ncol;
2005b7c46309SBarry Smith   }
2006fc73b1b3SBarry Smith   ierr = PetscFree(cols);CHKERRQ(ierr);
2007fc73b1b3SBarry Smith 
20086d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20096d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2010815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *matout != A) {
20110de55854SLois Curfman McInnes     *matout = B;
20120de55854SLois Curfman McInnes   } else {
2013eb6b5d47SBarry Smith     ierr = MatHeaderMerge(A,B);CHKERRQ(ierr);
20140de55854SLois Curfman McInnes   }
20153a40ed3dSBarry Smith   PetscFunctionReturn(0);
2016b7c46309SBarry Smith }
2017b7c46309SBarry Smith 
20184a2ae208SSatish Balay #undef __FUNCT__
20194a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_MPIAIJ"
2020dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr)
2021a008b906SSatish Balay {
20224b967eb1SSatish Balay   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
20234b967eb1SSatish Balay   Mat            a    = aij->A,b = aij->B;
2024dfbe8321SBarry Smith   PetscErrorCode ierr;
2025b1d57f15SBarry Smith   PetscInt       s1,s2,s3;
2026a008b906SSatish Balay 
20273a40ed3dSBarry Smith   PetscFunctionBegin;
20284b967eb1SSatish Balay   ierr = MatGetLocalSize(mat,&s2,&s3);CHKERRQ(ierr);
20294b967eb1SSatish Balay   if (rr) {
2030e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr);
2031e32f2f54SBarry Smith     if (s1!=s3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"right vector non-conforming local size");
20324b967eb1SSatish Balay     /* Overlap communication with computation. */
2033ca9f406cSSatish Balay     ierr = VecScatterBegin(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2034a008b906SSatish Balay   }
20354b967eb1SSatish Balay   if (ll) {
2036e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr);
2037e32f2f54SBarry Smith     if (s1!=s2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"left vector non-conforming local size");
2038f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,ll,0);CHKERRQ(ierr);
20394b967eb1SSatish Balay   }
20404b967eb1SSatish Balay   /* scale  the diagonal block */
2041f830108cSBarry Smith   ierr = (*a->ops->diagonalscale)(a,ll,rr);CHKERRQ(ierr);
20424b967eb1SSatish Balay 
20434b967eb1SSatish Balay   if (rr) {
20444b967eb1SSatish Balay     /* Do a scatter end and then right scale the off-diagonal block */
2045ca9f406cSSatish Balay     ierr = VecScatterEnd(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2046f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,0,aij->lvec);CHKERRQ(ierr);
20474b967eb1SSatish Balay   }
20483a40ed3dSBarry Smith   PetscFunctionReturn(0);
2049a008b906SSatish Balay }
2050a008b906SSatish Balay 
20514a2ae208SSatish Balay #undef __FUNCT__
20524a2ae208SSatish Balay #define __FUNCT__ "MatSetUnfactored_MPIAIJ"
2053dfbe8321SBarry Smith PetscErrorCode MatSetUnfactored_MPIAIJ(Mat A)
2054bb5a7306SBarry Smith {
2055bb5a7306SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
2056dfbe8321SBarry Smith   PetscErrorCode ierr;
20573a40ed3dSBarry Smith 
20583a40ed3dSBarry Smith   PetscFunctionBegin;
2059bb5a7306SBarry Smith   ierr = MatSetUnfactored(a->A);CHKERRQ(ierr);
20603a40ed3dSBarry Smith   PetscFunctionReturn(0);
2061bb5a7306SBarry Smith }
2062bb5a7306SBarry Smith 
20634a2ae208SSatish Balay #undef __FUNCT__
20644a2ae208SSatish Balay #define __FUNCT__ "MatEqual_MPIAIJ"
2065ace3abfcSBarry Smith PetscErrorCode MatEqual_MPIAIJ(Mat A,Mat B,PetscBool  *flag)
2066d4bb536fSBarry Smith {
2067d4bb536fSBarry Smith   Mat_MPIAIJ     *matB = (Mat_MPIAIJ*)B->data,*matA = (Mat_MPIAIJ*)A->data;
2068d4bb536fSBarry Smith   Mat            a,b,c,d;
2069ace3abfcSBarry Smith   PetscBool      flg;
2070dfbe8321SBarry Smith   PetscErrorCode ierr;
2071d4bb536fSBarry Smith 
20723a40ed3dSBarry Smith   PetscFunctionBegin;
2073d4bb536fSBarry Smith   a = matA->A; b = matA->B;
2074d4bb536fSBarry Smith   c = matB->A; d = matB->B;
2075d4bb536fSBarry Smith 
2076d4bb536fSBarry Smith   ierr = MatEqual(a,c,&flg);CHKERRQ(ierr);
2077abc0a331SBarry Smith   if (flg) {
2078d4bb536fSBarry Smith     ierr = MatEqual(b,d,&flg);CHKERRQ(ierr);
2079d4bb536fSBarry Smith   }
2080ce94432eSBarry Smith   ierr = MPI_Allreduce(&flg,flag,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)A));CHKERRQ(ierr);
20813a40ed3dSBarry Smith   PetscFunctionReturn(0);
2082d4bb536fSBarry Smith }
2083d4bb536fSBarry Smith 
20844a2ae208SSatish Balay #undef __FUNCT__
20854a2ae208SSatish Balay #define __FUNCT__ "MatCopy_MPIAIJ"
2086dfbe8321SBarry Smith PetscErrorCode MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str)
2087cb5b572fSBarry Smith {
2088dfbe8321SBarry Smith   PetscErrorCode ierr;
2089cb5b572fSBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
2090cb5b572fSBarry Smith   Mat_MPIAIJ     *b = (Mat_MPIAIJ*)B->data;
2091cb5b572fSBarry Smith 
2092cb5b572fSBarry Smith   PetscFunctionBegin;
209333f4a19fSKris Buschelman   /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
209433f4a19fSKris Buschelman   if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) {
2095cb5b572fSBarry Smith     /* because of the column compression in the off-processor part of the matrix a->B,
2096cb5b572fSBarry Smith        the number of columns in a->B and b->B may be different, hence we cannot call
2097cb5b572fSBarry Smith        the MatCopy() directly on the two parts. If need be, we can provide a more
2098cb5b572fSBarry Smith        efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices
2099cb5b572fSBarry Smith        then copying the submatrices */
2100cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2101cb5b572fSBarry Smith   } else {
2102cb5b572fSBarry Smith     ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr);
2103cb5b572fSBarry Smith     ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr);
2104cb5b572fSBarry Smith   }
2105cb5b572fSBarry Smith   PetscFunctionReturn(0);
2106cb5b572fSBarry Smith }
2107cb5b572fSBarry Smith 
21084a2ae208SSatish Balay #undef __FUNCT__
21094994cf47SJed Brown #define __FUNCT__ "MatSetUp_MPIAIJ"
21104994cf47SJed Brown PetscErrorCode MatSetUp_MPIAIJ(Mat A)
2111273d9f13SBarry Smith {
2112dfbe8321SBarry Smith   PetscErrorCode ierr;
2113273d9f13SBarry Smith 
2114273d9f13SBarry Smith   PetscFunctionBegin;
2115273d9f13SBarry Smith   ierr =  MatMPIAIJSetPreallocation(A,PETSC_DEFAULT,0,PETSC_DEFAULT,0);CHKERRQ(ierr);
2116273d9f13SBarry Smith   PetscFunctionReturn(0);
2117273d9f13SBarry Smith }
2118273d9f13SBarry Smith 
2119001ddc4fSHong Zhang /*
2120001ddc4fSHong Zhang    Computes the number of nonzeros per row needed for preallocation when X and Y
2121001ddc4fSHong Zhang    have different nonzero structure.
2122001ddc4fSHong Zhang */
2123ac90fabeSBarry Smith #undef __FUNCT__
2124001ddc4fSHong Zhang #define __FUNCT__ "MatAXPYGetPreallocation_MPIX_private"
2125001ddc4fSHong Zhang PetscErrorCode MatAXPYGetPreallocation_MPIX_private(PetscInt m,const PetscInt *xi,const PetscInt *xj,const PetscInt *xltog,const PetscInt *yi,const PetscInt *yj,const PetscInt *yltog,PetscInt *nnz)
212695b7e79eSJed Brown {
2127001ddc4fSHong Zhang   PetscInt       i,j,k,nzx,nzy;
212895b7e79eSJed Brown 
212995b7e79eSJed Brown   PetscFunctionBegin;
213095b7e79eSJed Brown   /* Set the number of nonzeros in the new matrix */
213195b7e79eSJed Brown   for (i=0; i<m; i++) {
2132001ddc4fSHong Zhang     const PetscInt *xjj = xj+xi[i],*yjj = yj+yi[i];
2133001ddc4fSHong Zhang     nzx = xi[i+1] - xi[i];
2134001ddc4fSHong Zhang     nzy = yi[i+1] - yi[i];
213595b7e79eSJed Brown     nnz[i] = 0;
213695b7e79eSJed Brown     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
2137001ddc4fSHong Zhang       for (; k<nzy && yltog[yjj[k]]<xltog[xjj[j]]; k++) nnz[i]++; /* Catch up to X */
2138001ddc4fSHong Zhang       if (k<nzy && yltog[yjj[k]]==xltog[xjj[j]]) k++;             /* Skip duplicate */
213995b7e79eSJed Brown       nnz[i]++;
214095b7e79eSJed Brown     }
214195b7e79eSJed Brown     for (; k<nzy; k++) nnz[i]++;
214295b7e79eSJed Brown   }
214395b7e79eSJed Brown   PetscFunctionReturn(0);
214495b7e79eSJed Brown }
214595b7e79eSJed Brown 
2146001ddc4fSHong Zhang /* This is the same as MatAXPYGetPreallocation_SeqAIJ, except that the local-to-global map is provided */
2147001ddc4fSHong Zhang #undef __FUNCT__
2148001ddc4fSHong Zhang #define __FUNCT__ "MatAXPYGetPreallocation_MPIAIJ"
2149001ddc4fSHong Zhang static PetscErrorCode MatAXPYGetPreallocation_MPIAIJ(Mat Y,const PetscInt *yltog,Mat X,const PetscInt *xltog,PetscInt *nnz)
2150001ddc4fSHong Zhang {
2151001ddc4fSHong Zhang   PetscErrorCode ierr;
2152001ddc4fSHong Zhang   PetscInt       m = Y->rmap->N;
2153001ddc4fSHong Zhang   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data;
2154001ddc4fSHong Zhang   Mat_SeqAIJ     *y = (Mat_SeqAIJ*)Y->data;
2155001ddc4fSHong Zhang 
2156001ddc4fSHong Zhang   PetscFunctionBegin;
2157001ddc4fSHong Zhang   ierr = MatAXPYGetPreallocation_MPIX_private(m,x->i,x->j,xltog,y->i,y->j,yltog,nnz);CHKERRQ(ierr);
2158001ddc4fSHong Zhang   PetscFunctionReturn(0);
2159001ddc4fSHong Zhang }
2160001ddc4fSHong Zhang 
216195b7e79eSJed Brown #undef __FUNCT__
2162ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_MPIAIJ"
2163f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_MPIAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2164ac90fabeSBarry Smith {
2165dfbe8321SBarry Smith   PetscErrorCode ierr;
2166ac90fabeSBarry Smith   Mat_MPIAIJ     *xx = (Mat_MPIAIJ*)X->data,*yy = (Mat_MPIAIJ*)Y->data;
21674ce68768SBarry Smith   PetscBLASInt   bnz,one=1;
2168ac90fabeSBarry Smith   Mat_SeqAIJ     *x,*y;
2169ac90fabeSBarry Smith 
2170ac90fabeSBarry Smith   PetscFunctionBegin;
2171ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2172f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2173ac90fabeSBarry Smith     x    = (Mat_SeqAIJ*)xx->A->data;
2174c5df96a5SBarry Smith     ierr = PetscBLASIntCast(x->nz,&bnz);CHKERRQ(ierr);
2175ac90fabeSBarry Smith     y    = (Mat_SeqAIJ*)yy->A->data;
21768b83055fSJed Brown     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2177ac90fabeSBarry Smith     x    = (Mat_SeqAIJ*)xx->B->data;
2178ac90fabeSBarry Smith     y    = (Mat_SeqAIJ*)yy->B->data;
2179c5df96a5SBarry Smith     ierr = PetscBLASIntCast(x->nz,&bnz);CHKERRQ(ierr);
21808b83055fSJed Brown     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2181a3fa217bSJose E. Roman     ierr = PetscObjectStateIncrease((PetscObject)Y);CHKERRQ(ierr);
2182a30b2313SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) {
21837b2ba04bSHong Zhang     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
2184ac90fabeSBarry Smith   } else {
21859f5f6813SShri Abhyankar     Mat      B;
21869f5f6813SShri Abhyankar     PetscInt *nnz_d,*nnz_o;
2187785e854fSJed Brown     ierr = PetscMalloc1(yy->A->rmap->N,&nnz_d);CHKERRQ(ierr);
2188785e854fSJed Brown     ierr = PetscMalloc1(yy->B->rmap->N,&nnz_o);CHKERRQ(ierr);
2189ce94432eSBarry Smith     ierr = MatCreate(PetscObjectComm((PetscObject)Y),&B);CHKERRQ(ierr);
2190bc5a2726SShri Abhyankar     ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr);
21919f5f6813SShri Abhyankar     ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr);
219233d57670SJed Brown     ierr = MatSetBlockSizesFromMats(B,Y,Y);CHKERRQ(ierr);
21939f5f6813SShri Abhyankar     ierr = MatSetType(B,MATMPIAIJ);CHKERRQ(ierr);
21949f5f6813SShri Abhyankar     ierr = MatAXPYGetPreallocation_SeqAIJ(yy->A,xx->A,nnz_d);CHKERRQ(ierr);
219595b7e79eSJed Brown     ierr = MatAXPYGetPreallocation_MPIAIJ(yy->B,yy->garray,xx->B,xx->garray,nnz_o);CHKERRQ(ierr);
2196ecd8bba6SJed Brown     ierr = MatMPIAIJSetPreallocation(B,0,nnz_d,0,nnz_o);CHKERRQ(ierr);
21979f5f6813SShri Abhyankar     ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr);
2198a2ea699eSBarry Smith     ierr = MatHeaderReplace(Y,B);CHKERRQ(ierr);
21999f5f6813SShri Abhyankar     ierr = PetscFree(nnz_d);CHKERRQ(ierr);
22009f5f6813SShri Abhyankar     ierr = PetscFree(nnz_o);CHKERRQ(ierr);
2201ac90fabeSBarry Smith   }
2202ac90fabeSBarry Smith   PetscFunctionReturn(0);
2203ac90fabeSBarry Smith }
2204ac90fabeSBarry Smith 
22057087cfbeSBarry Smith extern PetscErrorCode  MatConjugate_SeqAIJ(Mat);
2206354c94deSBarry Smith 
2207354c94deSBarry Smith #undef __FUNCT__
2208354c94deSBarry Smith #define __FUNCT__ "MatConjugate_MPIAIJ"
22097087cfbeSBarry Smith PetscErrorCode  MatConjugate_MPIAIJ(Mat mat)
2210354c94deSBarry Smith {
2211354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2212354c94deSBarry Smith   PetscErrorCode ierr;
2213354c94deSBarry Smith   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
2214354c94deSBarry Smith 
2215354c94deSBarry Smith   PetscFunctionBegin;
2216354c94deSBarry Smith   ierr = MatConjugate_SeqAIJ(aij->A);CHKERRQ(ierr);
2217354c94deSBarry Smith   ierr = MatConjugate_SeqAIJ(aij->B);CHKERRQ(ierr);
2218354c94deSBarry Smith #else
2219354c94deSBarry Smith   PetscFunctionBegin;
2220354c94deSBarry Smith #endif
2221354c94deSBarry Smith   PetscFunctionReturn(0);
2222354c94deSBarry Smith }
2223354c94deSBarry Smith 
222499cafbc1SBarry Smith #undef __FUNCT__
222599cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_MPIAIJ"
222699cafbc1SBarry Smith PetscErrorCode MatRealPart_MPIAIJ(Mat A)
222799cafbc1SBarry Smith {
222899cafbc1SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
222999cafbc1SBarry Smith   PetscErrorCode ierr;
223099cafbc1SBarry Smith 
223199cafbc1SBarry Smith   PetscFunctionBegin;
223299cafbc1SBarry Smith   ierr = MatRealPart(a->A);CHKERRQ(ierr);
223399cafbc1SBarry Smith   ierr = MatRealPart(a->B);CHKERRQ(ierr);
223499cafbc1SBarry Smith   PetscFunctionReturn(0);
223599cafbc1SBarry Smith }
223699cafbc1SBarry Smith 
223799cafbc1SBarry Smith #undef __FUNCT__
223899cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_MPIAIJ"
223999cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_MPIAIJ(Mat A)
224099cafbc1SBarry Smith {
224199cafbc1SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
224299cafbc1SBarry Smith   PetscErrorCode ierr;
224399cafbc1SBarry Smith 
224499cafbc1SBarry Smith   PetscFunctionBegin;
224599cafbc1SBarry Smith   ierr = MatImaginaryPart(a->A);CHKERRQ(ierr);
224699cafbc1SBarry Smith   ierr = MatImaginaryPart(a->B);CHKERRQ(ierr);
224799cafbc1SBarry Smith   PetscFunctionReturn(0);
224899cafbc1SBarry Smith }
224999cafbc1SBarry Smith 
2250519f805aSKarl Rupp #if defined(PETSC_HAVE_PBGL)
2251103bf8bdSMatthew Knepley 
2252103bf8bdSMatthew Knepley #include <boost/parallel/mpi/bsp_process_group.hpp>
2253a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_default_graph.hpp>
2254a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_0_block.hpp>
2255a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_preconditioner.hpp>
2256103bf8bdSMatthew Knepley #include <boost/graph/distributed/petsc/interface.hpp>
2257a2c909beSMatthew Knepley #include <boost/multi_array.hpp>
2258d0f46423SBarry Smith #include <boost/parallel/distributed_property_map->hpp>
2259103bf8bdSMatthew Knepley 
2260103bf8bdSMatthew Knepley #undef __FUNCT__
2261103bf8bdSMatthew Knepley #define __FUNCT__ "MatILUFactorSymbolic_MPIAIJ"
2262103bf8bdSMatthew Knepley /*
2263103bf8bdSMatthew Knepley   This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu>
2264103bf8bdSMatthew Knepley */
22650481f469SBarry Smith PetscErrorCode MatILUFactorSymbolic_MPIAIJ(Mat fact,Mat A, IS isrow, IS iscol, const MatFactorInfo *info)
2266103bf8bdSMatthew Knepley {
2267a2c909beSMatthew Knepley   namespace petsc = boost::distributed::petsc;
2268a2c909beSMatthew Knepley 
2269a2c909beSMatthew Knepley   namespace graph_dist = boost::graph::distributed;
2270a2c909beSMatthew Knepley   using boost::graph::distributed::ilu_default::process_group_type;
2271a2c909beSMatthew Knepley   using boost::graph::ilu_permuted;
2272a2c909beSMatthew Knepley 
2273ace3abfcSBarry Smith   PetscBool      row_identity, col_identity;
2274776b82aeSLisandro Dalcin   PetscContainer c;
2275103bf8bdSMatthew Knepley   PetscInt       m, n, M, N;
2276103bf8bdSMatthew Knepley   PetscErrorCode ierr;
2277103bf8bdSMatthew Knepley 
2278103bf8bdSMatthew Knepley   PetscFunctionBegin;
2279e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels = 0 supported for parallel ilu");
2280103bf8bdSMatthew Knepley   ierr = ISIdentity(isrow, &row_identity);CHKERRQ(ierr);
2281103bf8bdSMatthew Knepley   ierr = ISIdentity(iscol, &col_identity);CHKERRQ(ierr);
2282f23aa3ddSBarry Smith   if (!row_identity || !col_identity) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Row and column permutations must be identity for parallel ILU");
2283103bf8bdSMatthew Knepley 
2284103bf8bdSMatthew Knepley   process_group_type pg;
2285a2c909beSMatthew Knepley   typedef graph_dist::ilu_default::ilu_level_graph_type lgraph_type;
2286a2c909beSMatthew Knepley   lgraph_type  *lgraph_p   = new lgraph_type(petsc::num_global_vertices(A), pg, petsc::matrix_distribution(A, pg));
2287a2c909beSMatthew Knepley   lgraph_type& level_graph = *lgraph_p;
2288a2c909beSMatthew Knepley   graph_dist::ilu_default::graph_type&            graph(level_graph.graph);
2289a2c909beSMatthew Knepley 
2290103bf8bdSMatthew Knepley   petsc::read_matrix(A, graph, get(boost::edge_weight, graph));
2291a2c909beSMatthew Knepley   ilu_permuted(level_graph);
2292103bf8bdSMatthew Knepley 
2293103bf8bdSMatthew Knepley   /* put together the new matrix */
2294ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A), fact);CHKERRQ(ierr);
2295103bf8bdSMatthew Knepley   ierr = MatGetLocalSize(A, &m, &n);CHKERRQ(ierr);
2296103bf8bdSMatthew Knepley   ierr = MatGetSize(A, &M, &N);CHKERRQ(ierr);
2297719d5645SBarry Smith   ierr = MatSetSizes(fact, m, n, M, N);CHKERRQ(ierr);
229833d57670SJed Brown   ierr = MatSetBlockSizesFromMats(fact,A,A);CHKERRQ(ierr);
2299719d5645SBarry Smith   ierr = MatSetType(fact, ((PetscObject)A)->type_name);CHKERRQ(ierr);
2300719d5645SBarry Smith   ierr = MatAssemblyBegin(fact, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2301719d5645SBarry Smith   ierr = MatAssemblyEnd(fact, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2302103bf8bdSMatthew Knepley 
2303ce94432eSBarry Smith   ierr = PetscContainerCreate(PetscObjectComm((PetscObject)A), &c);
2304776b82aeSLisandro Dalcin   ierr = PetscContainerSetPointer(c, lgraph_p);
2305719d5645SBarry Smith   ierr = PetscObjectCompose((PetscObject) (fact), "graph", (PetscObject) c);
2306bf0cc555SLisandro Dalcin   ierr = PetscContainerDestroy(&c);
2307103bf8bdSMatthew Knepley   PetscFunctionReturn(0);
2308103bf8bdSMatthew Knepley }
2309103bf8bdSMatthew Knepley 
2310103bf8bdSMatthew Knepley #undef __FUNCT__
2311103bf8bdSMatthew Knepley #define __FUNCT__ "MatLUFactorNumeric_MPIAIJ"
23120481f469SBarry Smith PetscErrorCode MatLUFactorNumeric_MPIAIJ(Mat B,Mat A, const MatFactorInfo *info)
2313103bf8bdSMatthew Knepley {
2314103bf8bdSMatthew Knepley   PetscFunctionBegin;
2315103bf8bdSMatthew Knepley   PetscFunctionReturn(0);
2316103bf8bdSMatthew Knepley }
2317103bf8bdSMatthew Knepley 
2318103bf8bdSMatthew Knepley #undef __FUNCT__
2319103bf8bdSMatthew Knepley #define __FUNCT__ "MatSolve_MPIAIJ"
2320103bf8bdSMatthew Knepley /*
2321103bf8bdSMatthew Knepley   This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu>
2322103bf8bdSMatthew Knepley */
2323103bf8bdSMatthew Knepley PetscErrorCode MatSolve_MPIAIJ(Mat A, Vec b, Vec x)
2324103bf8bdSMatthew Knepley {
2325a2c909beSMatthew Knepley   namespace graph_dist = boost::graph::distributed;
2326a2c909beSMatthew Knepley 
2327a2c909beSMatthew Knepley   typedef graph_dist::ilu_default::ilu_level_graph_type lgraph_type;
2328a2c909beSMatthew Knepley   lgraph_type    *lgraph_p;
2329776b82aeSLisandro Dalcin   PetscContainer c;
2330103bf8bdSMatthew Knepley   PetscErrorCode ierr;
2331103bf8bdSMatthew Knepley 
2332103bf8bdSMatthew Knepley   PetscFunctionBegin;
2333103bf8bdSMatthew Knepley   ierr = PetscObjectQuery((PetscObject) A, "graph", (PetscObject*) &c);CHKERRQ(ierr);
2334776b82aeSLisandro Dalcin   ierr = PetscContainerGetPointer(c, (void**) &lgraph_p);CHKERRQ(ierr);
2335103bf8bdSMatthew Knepley   ierr = VecCopy(b, x);CHKERRQ(ierr);
2336a2c909beSMatthew Knepley 
2337a2c909beSMatthew Knepley   PetscScalar *array_x;
2338a2c909beSMatthew Knepley   ierr = VecGetArray(x, &array_x);CHKERRQ(ierr);
2339a2c909beSMatthew Knepley   PetscInt sx;
2340a2c909beSMatthew Knepley   ierr = VecGetSize(x, &sx);CHKERRQ(ierr);
2341a2c909beSMatthew Knepley 
2342a2c909beSMatthew Knepley   PetscScalar *array_b;
2343a2c909beSMatthew Knepley   ierr = VecGetArray(b, &array_b);CHKERRQ(ierr);
2344a2c909beSMatthew Knepley   PetscInt sb;
2345a2c909beSMatthew Knepley   ierr = VecGetSize(b, &sb);CHKERRQ(ierr);
2346a2c909beSMatthew Knepley 
2347a2c909beSMatthew Knepley   lgraph_type& level_graph = *lgraph_p;
2348a2c909beSMatthew Knepley   graph_dist::ilu_default::graph_type&            graph(level_graph.graph);
2349a2c909beSMatthew Knepley 
2350a2c909beSMatthew Knepley   typedef boost::multi_array_ref<PetscScalar, 1> array_ref_type;
23512205254eSKarl Rupp   array_ref_type                                 ref_b(array_b, boost::extents[num_vertices(graph)]);
23522205254eSKarl Rupp   array_ref_type                                 ref_x(array_x, boost::extents[num_vertices(graph)]);
2353a2c909beSMatthew Knepley 
2354a2c909beSMatthew Knepley   typedef boost::iterator_property_map<array_ref_type::iterator,
2355a2c909beSMatthew Knepley                                        boost::property_map<graph_dist::ilu_default::graph_type, boost::vertex_index_t>::type>  gvector_type;
23562205254eSKarl Rupp   gvector_type                                   vector_b(ref_b.begin(), get(boost::vertex_index, graph));
23572205254eSKarl Rupp   gvector_type                                   vector_x(ref_x.begin(), get(boost::vertex_index, graph));
2358a2c909beSMatthew Knepley 
2359a2c909beSMatthew Knepley   ilu_set_solve(*lgraph_p, vector_b, vector_x);
2360103bf8bdSMatthew Knepley   PetscFunctionReturn(0);
2361103bf8bdSMatthew Knepley }
2362103bf8bdSMatthew Knepley #endif
2363103bf8bdSMatthew Knepley 
236469db28dcSHong Zhang 
236569db28dcSHong Zhang #undef __FUNCT__
236622559b1cSHong Zhang #define __FUNCT__ "MatGetRedundantMatrix_MPIAIJ_interlaced"
23677cb6ea77SHong Zhang PetscErrorCode MatGetRedundantMatrix_MPIAIJ_interlaced(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,MatReuse reuse,Mat *matredundant)
2368b4617e5dSHong Zhang {
2369b4617e5dSHong Zhang   PetscMPIInt    rank,size;
23707cb6ea77SHong Zhang   MPI_Comm       comm;
2371b4617e5dSHong Zhang   PetscErrorCode ierr;
237234d19554SHong Zhang   PetscInt       nsends=0,nrecvs=0,i,rownz_max=0,M=mat->rmap->N,N=mat->cmap->N;
23735cc03489SHong Zhang   PetscMPIInt    *send_rank= NULL,*recv_rank=NULL,subrank,subsize;
2374b4617e5dSHong Zhang   PetscInt       *rowrange = mat->rmap->range;
2375b4617e5dSHong Zhang   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
2376b4617e5dSHong Zhang   Mat            A = aij->A,B=aij->B,C=*matredundant;
2377b4617e5dSHong Zhang   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*b=(Mat_SeqAIJ*)B->data;
2378b4617e5dSHong Zhang   PetscScalar    *sbuf_a;
2379b4617e5dSHong Zhang   PetscInt       nzlocal=a->nz+b->nz;
2380b4617e5dSHong Zhang   PetscInt       j,cstart=mat->cmap->rstart,cend=mat->cmap->rend,row,nzA,nzB,ncols,*cworkA,*cworkB;
238134d19554SHong Zhang   PetscInt       rstart=mat->rmap->rstart,rend=mat->rmap->rend,*bmap=aij->garray;
2382b4617e5dSHong Zhang   PetscInt       *cols,ctmp,lwrite,*rptr,l,*sbuf_j;
2383b4617e5dSHong Zhang   MatScalar      *aworkA,*aworkB;
2384b4617e5dSHong Zhang   PetscScalar    *vals;
2385b4617e5dSHong Zhang   PetscMPIInt    tag1,tag2,tag3,imdex;
2386b4617e5dSHong Zhang   MPI_Request    *s_waits1=NULL,*s_waits2=NULL,*s_waits3=NULL;
2387b4617e5dSHong Zhang   MPI_Request    *r_waits1=NULL,*r_waits2=NULL,*r_waits3=NULL;
2388b4617e5dSHong Zhang   MPI_Status     recv_status,*send_status;
2389b4617e5dSHong Zhang   PetscInt       *sbuf_nz=NULL,*rbuf_nz=NULL,count;
2390b4617e5dSHong Zhang   PetscInt       **rbuf_j=NULL;
2391b4617e5dSHong Zhang   PetscScalar    **rbuf_a=NULL;
2392b4617e5dSHong Zhang   Mat_Redundant  *redund =NULL;
2393b4617e5dSHong Zhang 
2394b4617e5dSHong Zhang   PetscFunctionBegin;
2395b4617e5dSHong Zhang   ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr);
2396b4617e5dSHong Zhang   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
2397b4617e5dSHong Zhang   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
23985cc03489SHong Zhang   ierr = MPI_Comm_rank(subcomm,&subrank);CHKERRQ(ierr);
23995cc03489SHong Zhang   ierr = MPI_Comm_size(subcomm,&subsize);CHKERRQ(ierr);
2400d3b23db5SHong Zhang 
2401b4617e5dSHong Zhang   if (reuse == MAT_REUSE_MATRIX) {
2402b4617e5dSHong Zhang     if (M != mat->rmap->N || N != mat->cmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong global size");
24035cc03489SHong Zhang     if (subsize == 1) {
24045cc03489SHong Zhang       Mat_SeqAIJ *c = (Mat_SeqAIJ*)C->data;
24055cc03489SHong Zhang       redund = c->redundant;
24065cc03489SHong Zhang     } else {
24075cc03489SHong Zhang       Mat_MPIAIJ *c = (Mat_MPIAIJ*)C->data;
24085cc03489SHong Zhang       redund = c->redundant;
24095cc03489SHong Zhang     }
2410b4617e5dSHong Zhang     if (nzlocal != redund->nzlocal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong nzlocal");
2411b4617e5dSHong Zhang 
2412b4617e5dSHong Zhang     nsends    = redund->nsends;
2413b4617e5dSHong Zhang     nrecvs    = redund->nrecvs;
2414b4617e5dSHong Zhang     send_rank = redund->send_rank;
2415b4617e5dSHong Zhang     recv_rank = redund->recv_rank;
2416b4617e5dSHong Zhang     sbuf_nz   = redund->sbuf_nz;
2417b4617e5dSHong Zhang     rbuf_nz   = redund->rbuf_nz;
2418b4617e5dSHong Zhang     sbuf_j    = redund->sbuf_j;
2419b4617e5dSHong Zhang     sbuf_a    = redund->sbuf_a;
2420b4617e5dSHong Zhang     rbuf_j    = redund->rbuf_j;
2421b4617e5dSHong Zhang     rbuf_a    = redund->rbuf_a;
2422b4617e5dSHong Zhang   }
2423b4617e5dSHong Zhang 
2424b4617e5dSHong Zhang   if (reuse == MAT_INITIAL_MATRIX) {
2425b4617e5dSHong Zhang     PetscInt    nleftover,np_subcomm;
2426b4617e5dSHong Zhang 
2427b4617e5dSHong Zhang     /* get the destination processors' id send_rank, nsends and nrecvs */
2428dcca6d9dSJed Brown     ierr = PetscMalloc2(size,&send_rank,size,&recv_rank);CHKERRQ(ierr);
2429b4617e5dSHong Zhang 
2430b4617e5dSHong Zhang     np_subcomm = size/nsubcomm;
2431b4617e5dSHong Zhang     nleftover  = size - nsubcomm*np_subcomm;
2432b4617e5dSHong Zhang 
243322559b1cSHong Zhang     /* block of codes below is specific for INTERLACED */
243422559b1cSHong Zhang     /* ------------------------------------------------*/
2435b4617e5dSHong Zhang     nsends = 0; nrecvs = 0;
2436b4617e5dSHong Zhang     for (i=0; i<size; i++) {
2437b4617e5dSHong Zhang       if (subrank == i/nsubcomm && i != rank) { /* my_subrank == other's subrank */
243822559b1cSHong Zhang         send_rank[nsends++] = i;
2439b4617e5dSHong Zhang         recv_rank[nrecvs++] = i;
2440b4617e5dSHong Zhang       }
2441b4617e5dSHong Zhang     }
2442b4617e5dSHong Zhang     if (rank >= size - nleftover) { /* this proc is a leftover processor */
2443b4617e5dSHong Zhang       i = size-nleftover-1;
2444b4617e5dSHong Zhang       j = 0;
2445b4617e5dSHong Zhang       while (j < nsubcomm - nleftover) {
2446b4617e5dSHong Zhang         send_rank[nsends++] = i;
2447b4617e5dSHong Zhang         i--; j++;
2448b4617e5dSHong Zhang       }
2449b4617e5dSHong Zhang     }
2450b4617e5dSHong Zhang 
2451b4617e5dSHong Zhang     if (nleftover && subsize == size/nsubcomm && subrank==subsize-1) { /* this proc recvs from leftover processors */
2452b4617e5dSHong Zhang       for (i=0; i<nleftover; i++) {
2453b4617e5dSHong Zhang         recv_rank[nrecvs++] = size-nleftover+i;
2454b4617e5dSHong Zhang       }
2455b4617e5dSHong Zhang     }
245622559b1cSHong Zhang     /*----------------------------------------------*/
2457b4617e5dSHong Zhang 
2458b4617e5dSHong Zhang     /* allocate sbuf_j, sbuf_a */
2459b4617e5dSHong Zhang     i    = nzlocal + rowrange[rank+1] - rowrange[rank] + 2;
2460785e854fSJed Brown     ierr = PetscMalloc1(i,&sbuf_j);CHKERRQ(ierr);
2461785e854fSJed Brown     ierr = PetscMalloc1((nzlocal+1),&sbuf_a);CHKERRQ(ierr);
2462e37c6257SHong Zhang     /*
2463e37c6257SHong Zhang     ierr = PetscSynchronizedPrintf(comm,"[%d] nsends %d, nrecvs %d\n",rank,nsends,nrecvs);CHKERRQ(ierr);
24640ec8b6e3SBarry Smith     ierr = PetscSynchronizedFlush(comm,PETSC_STDOUT);CHKERRQ(ierr);
2465e37c6257SHong Zhang      */
2466b4617e5dSHong Zhang   } /* endof if (reuse == MAT_INITIAL_MATRIX) */
2467b4617e5dSHong Zhang 
2468b4617e5dSHong Zhang   /* copy mat's local entries into the buffers */
2469b4617e5dSHong Zhang   if (reuse == MAT_INITIAL_MATRIX) {
2470b4617e5dSHong Zhang     rownz_max = 0;
2471b4617e5dSHong Zhang     rptr      = sbuf_j;
2472b4617e5dSHong Zhang     cols      = sbuf_j + rend-rstart + 1;
2473b4617e5dSHong Zhang     vals      = sbuf_a;
2474b4617e5dSHong Zhang     rptr[0]   = 0;
2475b4617e5dSHong Zhang     for (i=0; i<rend-rstart; i++) {
2476b4617e5dSHong Zhang       row    = i + rstart;
2477b4617e5dSHong Zhang       nzA    = a->i[i+1] - a->i[i]; nzB = b->i[i+1] - b->i[i];
2478b4617e5dSHong Zhang       ncols  = nzA + nzB;
2479b4617e5dSHong Zhang       cworkA = a->j + a->i[i]; cworkB = b->j + b->i[i];
2480b4617e5dSHong Zhang       aworkA = a->a + a->i[i]; aworkB = b->a + b->i[i];
2481b4617e5dSHong Zhang       /* load the column indices for this row into cols */
2482b4617e5dSHong Zhang       lwrite = 0;
2483b4617e5dSHong Zhang       for (l=0; l<nzB; l++) {
2484b4617e5dSHong Zhang         if ((ctmp = bmap[cworkB[l]]) < cstart) {
2485b4617e5dSHong Zhang           vals[lwrite]   = aworkB[l];
2486b4617e5dSHong Zhang           cols[lwrite++] = ctmp;
2487b4617e5dSHong Zhang         }
2488b4617e5dSHong Zhang       }
2489b4617e5dSHong Zhang       for (l=0; l<nzA; l++) {
2490b4617e5dSHong Zhang         vals[lwrite]   = aworkA[l];
2491b4617e5dSHong Zhang         cols[lwrite++] = cstart + cworkA[l];
2492b4617e5dSHong Zhang       }
2493b4617e5dSHong Zhang       for (l=0; l<nzB; l++) {
2494b4617e5dSHong Zhang         if ((ctmp = bmap[cworkB[l]]) >= cend) {
2495b4617e5dSHong Zhang           vals[lwrite]   = aworkB[l];
2496b4617e5dSHong Zhang           cols[lwrite++] = ctmp;
2497b4617e5dSHong Zhang         }
2498b4617e5dSHong Zhang       }
2499b4617e5dSHong Zhang       vals     += ncols;
2500b4617e5dSHong Zhang       cols     += ncols;
2501b4617e5dSHong Zhang       rptr[i+1] = rptr[i] + ncols;
2502b4617e5dSHong Zhang       if (rownz_max < ncols) rownz_max = ncols;
2503b4617e5dSHong Zhang     }
2504b4617e5dSHong Zhang     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);
2505b4617e5dSHong Zhang   } else { /* only copy matrix values into sbuf_a */
2506b4617e5dSHong Zhang     rptr    = sbuf_j;
2507b4617e5dSHong Zhang     vals    = sbuf_a;
2508b4617e5dSHong Zhang     rptr[0] = 0;
2509b4617e5dSHong Zhang     for (i=0; i<rend-rstart; i++) {
2510b4617e5dSHong Zhang       row    = i + rstart;
2511b4617e5dSHong Zhang       nzA    = a->i[i+1] - a->i[i]; nzB = b->i[i+1] - b->i[i];
2512b4617e5dSHong Zhang       ncols  = nzA + nzB;
2513b4617e5dSHong Zhang       cworkB = b->j + b->i[i];
2514b4617e5dSHong Zhang       aworkA = a->a + a->i[i];
2515b4617e5dSHong Zhang       aworkB = b->a + b->i[i];
2516b4617e5dSHong Zhang       lwrite = 0;
2517b4617e5dSHong Zhang       for (l=0; l<nzB; l++) {
2518b4617e5dSHong Zhang         if ((ctmp = bmap[cworkB[l]]) < cstart) vals[lwrite++] = aworkB[l];
2519b4617e5dSHong Zhang       }
2520b4617e5dSHong Zhang       for (l=0; l<nzA; l++) vals[lwrite++] = aworkA[l];
2521b4617e5dSHong Zhang       for (l=0; l<nzB; l++) {
2522b4617e5dSHong Zhang         if ((ctmp = bmap[cworkB[l]]) >= cend) vals[lwrite++] = aworkB[l];
2523b4617e5dSHong Zhang       }
2524b4617e5dSHong Zhang       vals     += ncols;
2525b4617e5dSHong Zhang       rptr[i+1] = rptr[i] + ncols;
2526b4617e5dSHong Zhang     }
2527b4617e5dSHong Zhang   } /* endof if (reuse == MAT_INITIAL_MATRIX) */
2528b4617e5dSHong Zhang 
2529b4617e5dSHong Zhang   /* send nzlocal to others, and recv other's nzlocal */
2530b4617e5dSHong Zhang   /*--------------------------------------------------*/
2531b4617e5dSHong Zhang   if (reuse == MAT_INITIAL_MATRIX) {
2532dcca6d9dSJed Brown     ierr = PetscMalloc2(3*(nsends + nrecvs)+1,&s_waits3,nsends+1,&send_status);CHKERRQ(ierr);
2533b4617e5dSHong Zhang 
2534b4617e5dSHong Zhang     s_waits2 = s_waits3 + nsends;
2535b4617e5dSHong Zhang     s_waits1 = s_waits2 + nsends;
2536b4617e5dSHong Zhang     r_waits1 = s_waits1 + nsends;
2537b4617e5dSHong Zhang     r_waits2 = r_waits1 + nrecvs;
2538b4617e5dSHong Zhang     r_waits3 = r_waits2 + nrecvs;
2539b4617e5dSHong Zhang   } else {
2540dcca6d9dSJed Brown     ierr = PetscMalloc2(nsends + nrecvs +1,&s_waits3,nsends+1,&send_status);CHKERRQ(ierr);
2541b4617e5dSHong Zhang 
2542b4617e5dSHong Zhang     r_waits3 = s_waits3 + nsends;
2543b4617e5dSHong Zhang   }
2544b4617e5dSHong Zhang 
2545b4617e5dSHong Zhang   ierr = PetscObjectGetNewTag((PetscObject)mat,&tag3);CHKERRQ(ierr);
2546b4617e5dSHong Zhang   if (reuse == MAT_INITIAL_MATRIX) {
2547b4617e5dSHong Zhang     /* get new tags to keep the communication clean */
2548b4617e5dSHong Zhang     ierr = PetscObjectGetNewTag((PetscObject)mat,&tag1);CHKERRQ(ierr);
2549b4617e5dSHong Zhang     ierr = PetscObjectGetNewTag((PetscObject)mat,&tag2);CHKERRQ(ierr);
2550dcca6d9dSJed Brown     ierr = PetscMalloc4(nsends,&sbuf_nz,nrecvs,&rbuf_nz,nrecvs,&rbuf_j,nrecvs,&rbuf_a);CHKERRQ(ierr);
2551b4617e5dSHong Zhang 
2552b4617e5dSHong Zhang     /* post receives of other's nzlocal */
2553b4617e5dSHong Zhang     for (i=0; i<nrecvs; i++) {
2554b4617e5dSHong Zhang       ierr = MPI_Irecv(rbuf_nz+i,1,MPIU_INT,MPI_ANY_SOURCE,tag1,comm,r_waits1+i);CHKERRQ(ierr);
2555b4617e5dSHong Zhang     }
2556b4617e5dSHong Zhang     /* send nzlocal to others */
2557b4617e5dSHong Zhang     for (i=0; i<nsends; i++) {
2558b4617e5dSHong Zhang       sbuf_nz[i] = nzlocal;
2559b4617e5dSHong Zhang       ierr       = MPI_Isend(sbuf_nz+i,1,MPIU_INT,send_rank[i],tag1,comm,s_waits1+i);CHKERRQ(ierr);
2560b4617e5dSHong Zhang     }
2561b4617e5dSHong Zhang     /* wait on receives of nzlocal; allocate space for rbuf_j, rbuf_a */
2562b4617e5dSHong Zhang     count = nrecvs;
2563b4617e5dSHong Zhang     while (count) {
2564b4617e5dSHong Zhang       ierr = MPI_Waitany(nrecvs,r_waits1,&imdex,&recv_status);CHKERRQ(ierr);
2565b4617e5dSHong Zhang 
2566b4617e5dSHong Zhang       recv_rank[imdex] = recv_status.MPI_SOURCE;
2567b4617e5dSHong Zhang       /* allocate rbuf_a and rbuf_j; then post receives of rbuf_j */
2568785e854fSJed Brown       ierr = PetscMalloc1((rbuf_nz[imdex]+1),&rbuf_a[imdex]);CHKERRQ(ierr);
2569b4617e5dSHong Zhang 
2570b4617e5dSHong Zhang       i = rowrange[recv_status.MPI_SOURCE+1] - rowrange[recv_status.MPI_SOURCE]; /* number of expected mat->i */
2571b4617e5dSHong Zhang 
2572b4617e5dSHong Zhang       rbuf_nz[imdex] += i + 2;
2573b4617e5dSHong Zhang 
2574785e854fSJed Brown       ierr = PetscMalloc1(rbuf_nz[imdex],&rbuf_j[imdex]);CHKERRQ(ierr);
2575b4617e5dSHong Zhang       ierr = MPI_Irecv(rbuf_j[imdex],rbuf_nz[imdex],MPIU_INT,recv_status.MPI_SOURCE,tag2,comm,r_waits2+imdex);CHKERRQ(ierr);
2576b4617e5dSHong Zhang       count--;
2577b4617e5dSHong Zhang     }
2578b4617e5dSHong Zhang     /* wait on sends of nzlocal */
2579b4617e5dSHong Zhang     if (nsends) {ierr = MPI_Waitall(nsends,s_waits1,send_status);CHKERRQ(ierr);}
2580b4617e5dSHong Zhang     /* send mat->i,j to others, and recv from other's */
2581b4617e5dSHong Zhang     /*------------------------------------------------*/
2582b4617e5dSHong Zhang     for (i=0; i<nsends; i++) {
2583b4617e5dSHong Zhang       j    = nzlocal + rowrange[rank+1] - rowrange[rank] + 1;
2584b4617e5dSHong Zhang       ierr = MPI_Isend(sbuf_j,j,MPIU_INT,send_rank[i],tag2,comm,s_waits2+i);CHKERRQ(ierr);
2585b4617e5dSHong Zhang     }
2586b4617e5dSHong Zhang     /* wait on receives of mat->i,j */
2587b4617e5dSHong Zhang     /*------------------------------*/
2588b4617e5dSHong Zhang     count = nrecvs;
2589b4617e5dSHong Zhang     while (count) {
2590b4617e5dSHong Zhang       ierr = MPI_Waitany(nrecvs,r_waits2,&imdex,&recv_status);CHKERRQ(ierr);
2591b4617e5dSHong Zhang       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);
2592b4617e5dSHong Zhang       count--;
2593b4617e5dSHong Zhang     }
2594b4617e5dSHong Zhang     /* wait on sends of mat->i,j */
2595b4617e5dSHong Zhang     /*---------------------------*/
2596b4617e5dSHong Zhang     if (nsends) {
2597b4617e5dSHong Zhang       ierr = MPI_Waitall(nsends,s_waits2,send_status);CHKERRQ(ierr);
2598b4617e5dSHong Zhang     }
2599b4617e5dSHong Zhang   } /* endof if (reuse == MAT_INITIAL_MATRIX) */
2600b4617e5dSHong Zhang 
2601b4617e5dSHong Zhang   /* post receives, send and receive mat->a */
2602b4617e5dSHong Zhang   /*----------------------------------------*/
2603b4617e5dSHong Zhang   for (imdex=0; imdex<nrecvs; imdex++) {
2604b4617e5dSHong Zhang     ierr = MPI_Irecv(rbuf_a[imdex],rbuf_nz[imdex],MPIU_SCALAR,recv_rank[imdex],tag3,comm,r_waits3+imdex);CHKERRQ(ierr);
2605b4617e5dSHong Zhang   }
2606b4617e5dSHong Zhang   for (i=0; i<nsends; i++) {
2607b4617e5dSHong Zhang     ierr = MPI_Isend(sbuf_a,nzlocal,MPIU_SCALAR,send_rank[i],tag3,comm,s_waits3+i);CHKERRQ(ierr);
2608b4617e5dSHong Zhang   }
2609b4617e5dSHong Zhang   count = nrecvs;
2610b4617e5dSHong Zhang   while (count) {
2611b4617e5dSHong Zhang     ierr = MPI_Waitany(nrecvs,r_waits3,&imdex,&recv_status);CHKERRQ(ierr);
2612b4617e5dSHong Zhang     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);
2613b4617e5dSHong Zhang     count--;
2614b4617e5dSHong Zhang   }
2615b4617e5dSHong Zhang   if (nsends) {
2616b4617e5dSHong Zhang     ierr = MPI_Waitall(nsends,s_waits3,send_status);CHKERRQ(ierr);
2617b4617e5dSHong Zhang   }
2618b4617e5dSHong Zhang 
2619b4617e5dSHong Zhang   ierr = PetscFree2(s_waits3,send_status);CHKERRQ(ierr);
2620b4617e5dSHong Zhang 
2621b4617e5dSHong Zhang   /* create redundant matrix */
2622b4617e5dSHong Zhang   /*-------------------------*/
2623b4617e5dSHong Zhang   if (reuse == MAT_INITIAL_MATRIX) {
262419171117SHong Zhang     const PetscInt *range;
262519171117SHong Zhang     PetscInt       rstart_sub,rend_sub,mloc_sub;
262619171117SHong Zhang 
2627b4617e5dSHong Zhang     /* compute rownz_max for preallocation */
2628b4617e5dSHong Zhang     for (imdex=0; imdex<nrecvs; imdex++) {
2629b4617e5dSHong Zhang       j    = rowrange[recv_rank[imdex]+1] - rowrange[recv_rank[imdex]];
2630b4617e5dSHong Zhang       rptr = rbuf_j[imdex];
2631b4617e5dSHong Zhang       for (i=0; i<j; i++) {
2632b4617e5dSHong Zhang         ncols = rptr[i+1] - rptr[i];
2633b4617e5dSHong Zhang         if (rownz_max < ncols) rownz_max = ncols;
2634b4617e5dSHong Zhang       }
2635b4617e5dSHong Zhang     }
2636b4617e5dSHong Zhang 
2637b4617e5dSHong Zhang     ierr = MatCreate(subcomm,&C);CHKERRQ(ierr);
263819171117SHong Zhang 
263919171117SHong Zhang     /* get local size of redundant matrix
264019171117SHong Zhang        - mloc_sub is chosen for PETSC_SUBCOMM_INTERLACED, works for other types, but may not efficient! */
264119171117SHong Zhang     ierr = MatGetOwnershipRanges(mat,&range);CHKERRQ(ierr);
264219171117SHong Zhang     rstart_sub = range[nsubcomm*subrank];
264319171117SHong Zhang     if (subrank+1 < subsize) { /* not the last proc in subcomm */
264419171117SHong Zhang       rend_sub = range[nsubcomm*(subrank+1)];
264519171117SHong Zhang     } else {
264619171117SHong Zhang       rend_sub = mat->rmap->N;
264719171117SHong Zhang     }
264819171117SHong Zhang     mloc_sub = rend_sub - rstart_sub;
264919171117SHong Zhang 
265034d19554SHong Zhang     if (M == N) {
2651b4617e5dSHong Zhang       ierr = MatSetSizes(C,mloc_sub,mloc_sub,PETSC_DECIDE,PETSC_DECIDE);CHKERRQ(ierr);
265234d19554SHong Zhang     } else { /* non-square matrix */
265334d19554SHong Zhang       ierr = MatSetSizes(C,mloc_sub,PETSC_DECIDE,PETSC_DECIDE,mat->cmap->N);CHKERRQ(ierr);
265434d19554SHong Zhang     }
265533d57670SJed Brown     ierr = MatSetBlockSizesFromMats(C,mat,mat);CHKERRQ(ierr);
2656b4617e5dSHong Zhang     ierr = MatSetFromOptions(C);CHKERRQ(ierr);
2657b4617e5dSHong Zhang     ierr = MatSeqAIJSetPreallocation(C,rownz_max,NULL);CHKERRQ(ierr);
2658b4617e5dSHong Zhang     ierr = MatMPIAIJSetPreallocation(C,rownz_max,NULL,rownz_max,NULL);CHKERRQ(ierr);
2659b4617e5dSHong Zhang   } else {
2660b4617e5dSHong Zhang     C = *matredundant;
2661b4617e5dSHong Zhang   }
2662b4617e5dSHong Zhang 
2663b4617e5dSHong Zhang   /* insert local matrix entries */
2664b4617e5dSHong Zhang   rptr = sbuf_j;
2665b4617e5dSHong Zhang   cols = sbuf_j + rend-rstart + 1;
2666b4617e5dSHong Zhang   vals = sbuf_a;
2667b4617e5dSHong Zhang   for (i=0; i<rend-rstart; i++) {
2668b4617e5dSHong Zhang     row   = i + rstart;
2669b4617e5dSHong Zhang     ncols = rptr[i+1] - rptr[i];
2670b4617e5dSHong Zhang     ierr  = MatSetValues(C,1,&row,ncols,cols,vals,INSERT_VALUES);CHKERRQ(ierr);
2671b4617e5dSHong Zhang     vals += ncols;
2672b4617e5dSHong Zhang     cols += ncols;
2673b4617e5dSHong Zhang   }
2674b4617e5dSHong Zhang   /* insert received matrix entries */
2675b4617e5dSHong Zhang   for (imdex=0; imdex<nrecvs; imdex++) {
2676b4617e5dSHong Zhang     rstart = rowrange[recv_rank[imdex]];
2677b4617e5dSHong Zhang     rend   = rowrange[recv_rank[imdex]+1];
2678e37c6257SHong Zhang     /* printf("[%d] insert rows %d - %d\n",rank,rstart,rend-1); */
2679b4617e5dSHong Zhang     rptr   = rbuf_j[imdex];
2680b4617e5dSHong Zhang     cols   = rbuf_j[imdex] + rend-rstart + 1;
2681b4617e5dSHong Zhang     vals   = rbuf_a[imdex];
2682b4617e5dSHong Zhang     for (i=0; i<rend-rstart; i++) {
2683b4617e5dSHong Zhang       row   = i + rstart;
2684b4617e5dSHong Zhang       ncols = rptr[i+1] - rptr[i];
2685b4617e5dSHong Zhang       ierr  = MatSetValues(C,1,&row,ncols,cols,vals,INSERT_VALUES);CHKERRQ(ierr);
2686b4617e5dSHong Zhang       vals += ncols;
2687b4617e5dSHong Zhang       cols += ncols;
2688b4617e5dSHong Zhang     }
2689b4617e5dSHong Zhang   }
2690b4617e5dSHong Zhang   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2691b4617e5dSHong Zhang   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2692b4617e5dSHong Zhang 
2693b4617e5dSHong Zhang   if (reuse == MAT_INITIAL_MATRIX) {
2694b4617e5dSHong Zhang     *matredundant = C;
26955cc03489SHong Zhang 
2696b4617e5dSHong Zhang     /* create a supporting struct and attach it to C for reuse */
2697b00a9115SJed Brown     ierr = PetscNewLog(C,&redund);CHKERRQ(ierr);
26985cc03489SHong Zhang     if (subsize == 1) {
26995cc03489SHong Zhang       Mat_SeqAIJ *c = (Mat_SeqAIJ*)C->data;
27005cc03489SHong Zhang       c->redundant = redund;
27015cc03489SHong Zhang     } else {
27025cc03489SHong Zhang       Mat_MPIAIJ *c = (Mat_MPIAIJ*)C->data;
27035cc03489SHong Zhang       c->redundant = redund;
27045cc03489SHong Zhang     }
2705b4617e5dSHong Zhang 
2706b4617e5dSHong Zhang     redund->nzlocal   = nzlocal;
2707b4617e5dSHong Zhang     redund->nsends    = nsends;
2708b4617e5dSHong Zhang     redund->nrecvs    = nrecvs;
2709b4617e5dSHong Zhang     redund->send_rank = send_rank;
2710b4617e5dSHong Zhang     redund->recv_rank = recv_rank;
2711b4617e5dSHong Zhang     redund->sbuf_nz   = sbuf_nz;
2712b4617e5dSHong Zhang     redund->rbuf_nz   = rbuf_nz;
2713b4617e5dSHong Zhang     redund->sbuf_j    = sbuf_j;
2714b4617e5dSHong Zhang     redund->sbuf_a    = sbuf_a;
2715b4617e5dSHong Zhang     redund->rbuf_j    = rbuf_j;
2716b4617e5dSHong Zhang     redund->rbuf_a    = rbuf_a;
27170b291e46SHong Zhang     redund->psubcomm  = NULL;
2718b4617e5dSHong Zhang   }
2719b4617e5dSHong Zhang   PetscFunctionReturn(0);
2720b4617e5dSHong Zhang }
2721b4617e5dSHong Zhang 
2722b4617e5dSHong Zhang #undef __FUNCT__
272369db28dcSHong Zhang #define __FUNCT__ "MatGetRedundantMatrix_MPIAIJ"
2724b2bf6370SHong Zhang PetscErrorCode MatGetRedundantMatrix_MPIAIJ(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,MatReuse reuse,Mat *matredundant)
272569db28dcSHong Zhang {
2726f38d543fSHong Zhang   PetscErrorCode ierr;
2727c79c5527SHong Zhang   MPI_Comm       comm;
2728c79c5527SHong Zhang   PetscMPIInt    size,subsize;
2729c79c5527SHong Zhang   PetscInt       mloc_sub,rstart,rend,M=mat->rmap->N,N=mat->cmap->N;
2730c79c5527SHong Zhang   Mat_Redundant  *redund=NULL;
27311f2d8ef4SHong Zhang   PetscSubcomm   psubcomm=NULL;
2732473f7991SHong Zhang   MPI_Comm       subcomm_in=subcomm;
27331f2d8ef4SHong Zhang   Mat            *matseq;
27341f2d8ef4SHong Zhang   IS             isrow,iscol;
273569db28dcSHong Zhang 
273669db28dcSHong Zhang   PetscFunctionBegin;
27371f2d8ef4SHong Zhang   if (subcomm_in == MPI_COMM_NULL) { /* user does not provide subcomm */
2738c79c5527SHong Zhang     if (reuse ==  MAT_INITIAL_MATRIX) {
27391f2d8ef4SHong Zhang       /* create psubcomm, then get subcomm */
2740ce94432eSBarry Smith       ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr);
274169db28dcSHong Zhang       ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
27427cb6ea77SHong Zhang       if (nsubcomm < 1 || nsubcomm > size) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"nsubcomm must between 1 and %D",size);
27437cb6ea77SHong Zhang 
2744d3b23db5SHong Zhang       ierr = PetscSubcommCreate(comm,&psubcomm);CHKERRQ(ierr);
2745d3b23db5SHong Zhang       ierr = PetscSubcommSetNumber(psubcomm,nsubcomm);CHKERRQ(ierr);
2746c79c5527SHong Zhang       ierr = PetscSubcommSetType(psubcomm,PETSC_SUBCOMM_CONTIGUOUS);CHKERRQ(ierr);
274719171117SHong Zhang       ierr = PetscSubcommSetFromOptions(psubcomm);CHKERRQ(ierr);
2748c79c5527SHong Zhang       subcomm = psubcomm->comm;
27497cb6ea77SHong Zhang     } else { /* retrieve psubcomm and subcomm */
2750c79c5527SHong Zhang       ierr = PetscObjectGetComm((PetscObject)(*matredundant),&subcomm);CHKERRQ(ierr);
2751c79c5527SHong Zhang       ierr = MPI_Comm_size(subcomm,&subsize);CHKERRQ(ierr);
2752c79c5527SHong Zhang       if (subsize == 1) {
2753c79c5527SHong Zhang         Mat_SeqAIJ *c = (Mat_SeqAIJ*)(*matredundant)->data;
27547cb6ea77SHong Zhang         redund = c->redundant;
2755c79c5527SHong Zhang       } else {
2756c79c5527SHong Zhang         Mat_MPIAIJ *c = (Mat_MPIAIJ*)(*matredundant)->data;
27577cb6ea77SHong Zhang         redund = c->redundant;
2758c79c5527SHong Zhang       }
27597cb6ea77SHong Zhang       psubcomm = redund->psubcomm;
2760fd7037dcSHong Zhang     }
27611f2d8ef4SHong Zhang     if (psubcomm->type == PETSC_SUBCOMM_INTERLACED) {
27627cb6ea77SHong Zhang       ierr = MatGetRedundantMatrix_MPIAIJ_interlaced(mat,nsubcomm,subcomm,reuse,matredundant);CHKERRQ(ierr);
2763a3ca3016SBarry Smith       if (reuse ==  MAT_INITIAL_MATRIX) { /* psubcomm is created in this routine, free it in MatDestroy_Redundant() */
27641f2d8ef4SHong Zhang         ierr = MPI_Comm_size(psubcomm->comm,&subsize);CHKERRQ(ierr);
27651f2d8ef4SHong Zhang         if (subsize == 1) {
27661f2d8ef4SHong Zhang           Mat_SeqAIJ *c = (Mat_SeqAIJ*)(*matredundant)->data;
27671f2d8ef4SHong Zhang           c->redundant->psubcomm = psubcomm;
27681f2d8ef4SHong Zhang         } else {
27691f2d8ef4SHong Zhang           Mat_MPIAIJ *c = (Mat_MPIAIJ*)(*matredundant)->data;
27701f2d8ef4SHong Zhang           c->redundant->psubcomm = psubcomm ;
27711f2d8ef4SHong Zhang         }
27721f2d8ef4SHong Zhang       }
27731f2d8ef4SHong Zhang       PetscFunctionReturn(0);
2774c79c5527SHong Zhang     }
2775c79c5527SHong Zhang   }
2776e37c6257SHong Zhang 
27771f2d8ef4SHong Zhang   /* use MPI subcomm via MatGetSubMatrices(); use subcomm_in or psubcomm->comm (psubcomm->type != INTERLACED) */
27787cb6ea77SHong Zhang   ierr = MPI_Comm_size(subcomm,&subsize);CHKERRQ(ierr);
2779c79c5527SHong Zhang   if (reuse == MAT_INITIAL_MATRIX) {
2780c79c5527SHong Zhang     /* create a local sequential matrix matseq[0] */
2781c79c5527SHong Zhang     mloc_sub = PETSC_DECIDE;
2782c79c5527SHong Zhang     ierr = PetscSplitOwnership(subcomm,&mloc_sub,&M);CHKERRQ(ierr);
2783c79c5527SHong Zhang     ierr = MPI_Scan(&mloc_sub,&rend,1,MPIU_INT,MPI_SUM,subcomm);CHKERRQ(ierr);
2784c79c5527SHong Zhang     rstart = rend - mloc_sub;
2785c79c5527SHong Zhang     ierr = ISCreateStride(PETSC_COMM_SELF,mloc_sub,rstart,1,&isrow);CHKERRQ(ierr);
2786c79c5527SHong Zhang     ierr = ISCreateStride(PETSC_COMM_SELF,N,0,1,&iscol);CHKERRQ(ierr);
2787c79c5527SHong Zhang   } else { /* reuse == MAT_REUSE_MATRIX */
2788c79c5527SHong Zhang     if (subsize == 1) {
2789c79c5527SHong Zhang       Mat_SeqAIJ *c = (Mat_SeqAIJ*)(*matredundant)->data;
2790c79c5527SHong Zhang       redund = c->redundant;
2791c79c5527SHong Zhang     } else {
2792c79c5527SHong Zhang       Mat_MPIAIJ *c = (Mat_MPIAIJ*)(*matredundant)->data;
2793c79c5527SHong Zhang       redund = c->redundant;
2794c79c5527SHong Zhang     }
2795c79c5527SHong Zhang 
2796c79c5527SHong Zhang     isrow  = redund->isrow;
2797c79c5527SHong Zhang     iscol  = redund->iscol;
2798c79c5527SHong Zhang     matseq = redund->matseq;
2799c79c5527SHong Zhang   }
2800c79c5527SHong Zhang   ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,reuse,&matseq);CHKERRQ(ierr);
2801c79c5527SHong Zhang   ierr = MatCreateMPIAIJConcatenateSeqAIJ(subcomm,matseq[0],PETSC_DECIDE,reuse,matredundant);CHKERRQ(ierr);
2802c79c5527SHong Zhang 
2803c79c5527SHong Zhang   if (reuse == MAT_INITIAL_MATRIX) {
2804c79c5527SHong Zhang     /* create a supporting struct and attach it to C for reuse */
2805b00a9115SJed Brown     ierr = PetscNewLog(*matredundant,&redund);CHKERRQ(ierr);
2806c79c5527SHong Zhang     if (subsize == 1) {
2807c79c5527SHong Zhang       Mat_SeqAIJ *c = (Mat_SeqAIJ*)(*matredundant)->data;
2808c79c5527SHong Zhang       c->redundant = redund;
2809c79c5527SHong Zhang     } else {
2810c79c5527SHong Zhang       Mat_MPIAIJ *c = (Mat_MPIAIJ*)(*matredundant)->data;
2811c79c5527SHong Zhang       c->redundant = redund;
2812c79c5527SHong Zhang     }
2813c79c5527SHong Zhang     redund->isrow    = isrow;
2814c79c5527SHong Zhang     redund->iscol    = iscol;
2815c79c5527SHong Zhang     redund->matseq   = matseq;
28161f2d8ef4SHong Zhang     redund->psubcomm = psubcomm;
2817c79c5527SHong Zhang   }
281869db28dcSHong Zhang   PetscFunctionReturn(0);
281969db28dcSHong Zhang }
282069db28dcSHong Zhang 
282103bc72f1SMatthew Knepley #undef __FUNCT__
2822c91732d9SHong Zhang #define __FUNCT__ "MatGetRowMaxAbs_MPIAIJ"
2823c91732d9SHong Zhang PetscErrorCode MatGetRowMaxAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[])
2824c91732d9SHong Zhang {
2825c91732d9SHong Zhang   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
2826c91732d9SHong Zhang   PetscErrorCode ierr;
2827c91732d9SHong Zhang   PetscInt       i,*idxb = 0;
2828c91732d9SHong Zhang   PetscScalar    *va,*vb;
2829c91732d9SHong Zhang   Vec            vtmp;
2830c91732d9SHong Zhang 
2831c91732d9SHong Zhang   PetscFunctionBegin;
2832c91732d9SHong Zhang   ierr = MatGetRowMaxAbs(a->A,v,idx);CHKERRQ(ierr);
2833c91732d9SHong Zhang   ierr = VecGetArray(v,&va);CHKERRQ(ierr);
2834c91732d9SHong Zhang   if (idx) {
2835192daf7cSBarry Smith     for (i=0; i<A->rmap->n; i++) {
2836d0f46423SBarry Smith       if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart;
2837c91732d9SHong Zhang     }
2838c91732d9SHong Zhang   }
2839c91732d9SHong Zhang 
2840d0f46423SBarry Smith   ierr = VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);CHKERRQ(ierr);
2841c91732d9SHong Zhang   if (idx) {
2842785e854fSJed Brown     ierr = PetscMalloc1(A->rmap->n,&idxb);CHKERRQ(ierr);
2843c91732d9SHong Zhang   }
2844c91732d9SHong Zhang   ierr = MatGetRowMaxAbs(a->B,vtmp,idxb);CHKERRQ(ierr);
2845c91732d9SHong Zhang   ierr = VecGetArray(vtmp,&vb);CHKERRQ(ierr);
2846c91732d9SHong Zhang 
2847d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
2848c91732d9SHong Zhang     if (PetscAbsScalar(va[i]) < PetscAbsScalar(vb[i])) {
2849c91732d9SHong Zhang       va[i] = vb[i];
2850c91732d9SHong Zhang       if (idx) idx[i] = a->garray[idxb[i]];
2851c91732d9SHong Zhang     }
2852c91732d9SHong Zhang   }
2853c91732d9SHong Zhang 
2854c91732d9SHong Zhang   ierr = VecRestoreArray(v,&va);CHKERRQ(ierr);
2855c91732d9SHong Zhang   ierr = VecRestoreArray(vtmp,&vb);CHKERRQ(ierr);
2856c91732d9SHong Zhang   ierr = PetscFree(idxb);CHKERRQ(ierr);
28576bf464f9SBarry Smith   ierr = VecDestroy(&vtmp);CHKERRQ(ierr);
2858c91732d9SHong Zhang   PetscFunctionReturn(0);
2859c91732d9SHong Zhang }
2860c91732d9SHong Zhang 
2861c91732d9SHong Zhang #undef __FUNCT__
2862c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_MPIAIJ"
2863c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[])
2864c87e5d42SMatthew Knepley {
2865c87e5d42SMatthew Knepley   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
2866c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2867c87e5d42SMatthew Knepley   PetscInt       i,*idxb = 0;
2868c87e5d42SMatthew Knepley   PetscScalar    *va,*vb;
2869c87e5d42SMatthew Knepley   Vec            vtmp;
2870c87e5d42SMatthew Knepley 
2871c87e5d42SMatthew Knepley   PetscFunctionBegin;
2872c87e5d42SMatthew Knepley   ierr = MatGetRowMinAbs(a->A,v,idx);CHKERRQ(ierr);
2873c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&va);CHKERRQ(ierr);
2874c87e5d42SMatthew Knepley   if (idx) {
2875c87e5d42SMatthew Knepley     for (i=0; i<A->cmap->n; i++) {
2876c87e5d42SMatthew Knepley       if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart;
2877c87e5d42SMatthew Knepley     }
2878c87e5d42SMatthew Knepley   }
2879c87e5d42SMatthew Knepley 
2880c87e5d42SMatthew Knepley   ierr = VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);CHKERRQ(ierr);
2881c87e5d42SMatthew Knepley   if (idx) {
2882785e854fSJed Brown     ierr = PetscMalloc1(A->rmap->n,&idxb);CHKERRQ(ierr);
2883c87e5d42SMatthew Knepley   }
2884c87e5d42SMatthew Knepley   ierr = MatGetRowMinAbs(a->B,vtmp,idxb);CHKERRQ(ierr);
2885c87e5d42SMatthew Knepley   ierr = VecGetArray(vtmp,&vb);CHKERRQ(ierr);
2886c87e5d42SMatthew Knepley 
2887c87e5d42SMatthew Knepley   for (i=0; i<A->rmap->n; i++) {
2888c87e5d42SMatthew Knepley     if (PetscAbsScalar(va[i]) > PetscAbsScalar(vb[i])) {
2889c87e5d42SMatthew Knepley       va[i] = vb[i];
2890c87e5d42SMatthew Knepley       if (idx) idx[i] = a->garray[idxb[i]];
2891c87e5d42SMatthew Knepley     }
2892c87e5d42SMatthew Knepley   }
2893c87e5d42SMatthew Knepley 
2894c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&va);CHKERRQ(ierr);
2895c87e5d42SMatthew Knepley   ierr = VecRestoreArray(vtmp,&vb);CHKERRQ(ierr);
2896c87e5d42SMatthew Knepley   ierr = PetscFree(idxb);CHKERRQ(ierr);
28976bf464f9SBarry Smith   ierr = VecDestroy(&vtmp);CHKERRQ(ierr);
2898c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
2899c87e5d42SMatthew Knepley }
2900c87e5d42SMatthew Knepley 
2901c87e5d42SMatthew Knepley #undef __FUNCT__
290203bc72f1SMatthew Knepley #define __FUNCT__ "MatGetRowMin_MPIAIJ"
290303bc72f1SMatthew Knepley PetscErrorCode MatGetRowMin_MPIAIJ(Mat A, Vec v, PetscInt idx[])
290403bc72f1SMatthew Knepley {
290503bc72f1SMatthew Knepley   Mat_MPIAIJ     *mat   = (Mat_MPIAIJ*) A->data;
2906d0f46423SBarry Smith   PetscInt       n      = A->rmap->n;
2907d0f46423SBarry Smith   PetscInt       cstart = A->cmap->rstart;
290803bc72f1SMatthew Knepley   PetscInt       *cmap  = mat->garray;
290903bc72f1SMatthew Knepley   PetscInt       *diagIdx, *offdiagIdx;
291003bc72f1SMatthew Knepley   Vec            diagV, offdiagV;
291103bc72f1SMatthew Knepley   PetscScalar    *a, *diagA, *offdiagA;
291203bc72f1SMatthew Knepley   PetscInt       r;
291303bc72f1SMatthew Knepley   PetscErrorCode ierr;
291403bc72f1SMatthew Knepley 
291503bc72f1SMatthew Knepley   PetscFunctionBegin;
2916dcca6d9dSJed Brown   ierr = PetscMalloc2(n,&diagIdx,n,&offdiagIdx);CHKERRQ(ierr);
2917ce94432eSBarry Smith   ierr = VecCreateSeq(PetscObjectComm((PetscObject)A), n, &diagV);CHKERRQ(ierr);
2918ce94432eSBarry Smith   ierr = VecCreateSeq(PetscObjectComm((PetscObject)A), n, &offdiagV);CHKERRQ(ierr);
291903bc72f1SMatthew Knepley   ierr = MatGetRowMin(mat->A, diagV,    diagIdx);CHKERRQ(ierr);
292003bc72f1SMatthew Knepley   ierr = MatGetRowMin(mat->B, offdiagV, offdiagIdx);CHKERRQ(ierr);
292103bc72f1SMatthew Knepley   ierr = VecGetArray(v,        &a);CHKERRQ(ierr);
292203bc72f1SMatthew Knepley   ierr = VecGetArray(diagV,    &diagA);CHKERRQ(ierr);
292303bc72f1SMatthew Knepley   ierr = VecGetArray(offdiagV, &offdiagA);CHKERRQ(ierr);
292403bc72f1SMatthew Knepley   for (r = 0; r < n; ++r) {
2925028cd4eaSSatish Balay     if (PetscAbsScalar(diagA[r]) <= PetscAbsScalar(offdiagA[r])) {
292603bc72f1SMatthew Knepley       a[r]   = diagA[r];
292703bc72f1SMatthew Knepley       idx[r] = cstart + diagIdx[r];
292803bc72f1SMatthew Knepley     } else {
292903bc72f1SMatthew Knepley       a[r]   = offdiagA[r];
293003bc72f1SMatthew Knepley       idx[r] = cmap[offdiagIdx[r]];
293103bc72f1SMatthew Knepley     }
293203bc72f1SMatthew Knepley   }
293303bc72f1SMatthew Knepley   ierr = VecRestoreArray(v,        &a);CHKERRQ(ierr);
293403bc72f1SMatthew Knepley   ierr = VecRestoreArray(diagV,    &diagA);CHKERRQ(ierr);
293503bc72f1SMatthew Knepley   ierr = VecRestoreArray(offdiagV, &offdiagA);CHKERRQ(ierr);
29366bf464f9SBarry Smith   ierr = VecDestroy(&diagV);CHKERRQ(ierr);
29376bf464f9SBarry Smith   ierr = VecDestroy(&offdiagV);CHKERRQ(ierr);
293803bc72f1SMatthew Knepley   ierr = PetscFree2(diagIdx, offdiagIdx);CHKERRQ(ierr);
293903bc72f1SMatthew Knepley   PetscFunctionReturn(0);
294003bc72f1SMatthew Knepley }
294103bc72f1SMatthew Knepley 
29425494a064SHong Zhang #undef __FUNCT__
2943c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMax_MPIAIJ"
2944c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMax_MPIAIJ(Mat A, Vec v, PetscInt idx[])
2945c87e5d42SMatthew Knepley {
2946c87e5d42SMatthew Knepley   Mat_MPIAIJ     *mat   = (Mat_MPIAIJ*) A->data;
2947c87e5d42SMatthew Knepley   PetscInt       n      = A->rmap->n;
2948c87e5d42SMatthew Knepley   PetscInt       cstart = A->cmap->rstart;
2949c87e5d42SMatthew Knepley   PetscInt       *cmap  = mat->garray;
2950c87e5d42SMatthew Knepley   PetscInt       *diagIdx, *offdiagIdx;
2951c87e5d42SMatthew Knepley   Vec            diagV, offdiagV;
2952c87e5d42SMatthew Knepley   PetscScalar    *a, *diagA, *offdiagA;
2953c87e5d42SMatthew Knepley   PetscInt       r;
2954c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2955c87e5d42SMatthew Knepley 
2956c87e5d42SMatthew Knepley   PetscFunctionBegin;
2957dcca6d9dSJed Brown   ierr = PetscMalloc2(n,&diagIdx,n,&offdiagIdx);CHKERRQ(ierr);
2958d11e49fbSSatish Balay   ierr = VecCreateSeq(PETSC_COMM_SELF, n, &diagV);CHKERRQ(ierr);
2959d11e49fbSSatish Balay   ierr = VecCreateSeq(PETSC_COMM_SELF, n, &offdiagV);CHKERRQ(ierr);
2960c87e5d42SMatthew Knepley   ierr = MatGetRowMax(mat->A, diagV,    diagIdx);CHKERRQ(ierr);
2961c87e5d42SMatthew Knepley   ierr = MatGetRowMax(mat->B, offdiagV, offdiagIdx);CHKERRQ(ierr);
2962c87e5d42SMatthew Knepley   ierr = VecGetArray(v,        &a);CHKERRQ(ierr);
2963c87e5d42SMatthew Knepley   ierr = VecGetArray(diagV,    &diagA);CHKERRQ(ierr);
2964c87e5d42SMatthew Knepley   ierr = VecGetArray(offdiagV, &offdiagA);CHKERRQ(ierr);
2965c87e5d42SMatthew Knepley   for (r = 0; r < n; ++r) {
2966c87e5d42SMatthew Knepley     if (PetscAbsScalar(diagA[r]) >= PetscAbsScalar(offdiagA[r])) {
2967c87e5d42SMatthew Knepley       a[r]   = diagA[r];
2968c87e5d42SMatthew Knepley       idx[r] = cstart + diagIdx[r];
2969c87e5d42SMatthew Knepley     } else {
2970c87e5d42SMatthew Knepley       a[r]   = offdiagA[r];
2971c87e5d42SMatthew Knepley       idx[r] = cmap[offdiagIdx[r]];
2972c87e5d42SMatthew Knepley     }
2973c87e5d42SMatthew Knepley   }
2974c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,        &a);CHKERRQ(ierr);
2975c87e5d42SMatthew Knepley   ierr = VecRestoreArray(diagV,    &diagA);CHKERRQ(ierr);
2976c87e5d42SMatthew Knepley   ierr = VecRestoreArray(offdiagV, &offdiagA);CHKERRQ(ierr);
29776bf464f9SBarry Smith   ierr = VecDestroy(&diagV);CHKERRQ(ierr);
29786bf464f9SBarry Smith   ierr = VecDestroy(&offdiagV);CHKERRQ(ierr);
2979c87e5d42SMatthew Knepley   ierr = PetscFree2(diagIdx, offdiagIdx);CHKERRQ(ierr);
2980c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
2981c87e5d42SMatthew Knepley }
2982c87e5d42SMatthew Knepley 
2983c87e5d42SMatthew Knepley #undef __FUNCT__
2984d1adec66SJed Brown #define __FUNCT__ "MatGetSeqNonzeroStructure_MPIAIJ"
2985d1adec66SJed Brown PetscErrorCode MatGetSeqNonzeroStructure_MPIAIJ(Mat mat,Mat *newmat)
29865494a064SHong Zhang {
29875494a064SHong Zhang   PetscErrorCode ierr;
2988f6d58c54SBarry Smith   Mat            *dummy;
29895494a064SHong Zhang 
29905494a064SHong Zhang   PetscFunctionBegin;
2991f6d58c54SBarry Smith   ierr    = MatGetSubMatrix_MPIAIJ_All(mat,MAT_DO_NOT_GET_VALUES,MAT_INITIAL_MATRIX,&dummy);CHKERRQ(ierr);
2992f6d58c54SBarry Smith   *newmat = *dummy;
2993f6d58c54SBarry Smith   ierr    = PetscFree(dummy);CHKERRQ(ierr);
29945494a064SHong Zhang   PetscFunctionReturn(0);
29955494a064SHong Zhang }
29965494a064SHong Zhang 
2997bbead8a2SBarry Smith #undef __FUNCT__
2998bbead8a2SBarry Smith #define __FUNCT__ "MatInvertBlockDiagonal_MPIAIJ"
2999713ccfa9SJed Brown PetscErrorCode  MatInvertBlockDiagonal_MPIAIJ(Mat A,const PetscScalar **values)
3000bbead8a2SBarry Smith {
3001bbead8a2SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*) A->data;
3002bbead8a2SBarry Smith   PetscErrorCode ierr;
3003bbead8a2SBarry Smith 
3004bbead8a2SBarry Smith   PetscFunctionBegin;
3005bbead8a2SBarry Smith   ierr = MatInvertBlockDiagonal(a->A,values);CHKERRQ(ierr);
3006bbead8a2SBarry Smith   PetscFunctionReturn(0);
3007bbead8a2SBarry Smith }
3008bbead8a2SBarry Smith 
300973a71a0fSBarry Smith #undef __FUNCT__
301073a71a0fSBarry Smith #define __FUNCT__ "MatSetRandom_MPIAIJ"
301173a71a0fSBarry Smith static PetscErrorCode  MatSetRandom_MPIAIJ(Mat x,PetscRandom rctx)
301273a71a0fSBarry Smith {
301373a71a0fSBarry Smith   PetscErrorCode ierr;
301473a71a0fSBarry Smith   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)x->data;
301573a71a0fSBarry Smith 
301673a71a0fSBarry Smith   PetscFunctionBegin;
301773a71a0fSBarry Smith   ierr = MatSetRandom(aij->A,rctx);CHKERRQ(ierr);
301873a71a0fSBarry Smith   ierr = MatSetRandom(aij->B,rctx);CHKERRQ(ierr);
301973a71a0fSBarry Smith   ierr = MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
302073a71a0fSBarry Smith   ierr = MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
302173a71a0fSBarry Smith   PetscFunctionReturn(0);
302273a71a0fSBarry Smith }
3023bbead8a2SBarry Smith 
30248a729477SBarry Smith /* -------------------------------------------------------------------*/
3025cda55fadSBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ,
3026cda55fadSBarry Smith                                        MatGetRow_MPIAIJ,
3027cda55fadSBarry Smith                                        MatRestoreRow_MPIAIJ,
3028cda55fadSBarry Smith                                        MatMult_MPIAIJ,
302997304618SKris Buschelman                                 /* 4*/ MatMultAdd_MPIAIJ,
30307c922b88SBarry Smith                                        MatMultTranspose_MPIAIJ,
30317c922b88SBarry Smith                                        MatMultTransposeAdd_MPIAIJ,
3032519f805aSKarl Rupp #if defined(PETSC_HAVE_PBGL)
3033103bf8bdSMatthew Knepley                                        MatSolve_MPIAIJ,
3034103bf8bdSMatthew Knepley #else
3035cda55fadSBarry Smith                                        0,
3036103bf8bdSMatthew Knepley #endif
3037cda55fadSBarry Smith                                        0,
3038cda55fadSBarry Smith                                        0,
303997304618SKris Buschelman                                 /*10*/ 0,
3040cda55fadSBarry Smith                                        0,
3041cda55fadSBarry Smith                                        0,
304241f059aeSBarry Smith                                        MatSOR_MPIAIJ,
3043b7c46309SBarry Smith                                        MatTranspose_MPIAIJ,
304497304618SKris Buschelman                                 /*15*/ MatGetInfo_MPIAIJ,
3045cda55fadSBarry Smith                                        MatEqual_MPIAIJ,
3046cda55fadSBarry Smith                                        MatGetDiagonal_MPIAIJ,
3047cda55fadSBarry Smith                                        MatDiagonalScale_MPIAIJ,
3048cda55fadSBarry Smith                                        MatNorm_MPIAIJ,
304997304618SKris Buschelman                                 /*20*/ MatAssemblyBegin_MPIAIJ,
3050cda55fadSBarry Smith                                        MatAssemblyEnd_MPIAIJ,
3051cda55fadSBarry Smith                                        MatSetOption_MPIAIJ,
3052cda55fadSBarry Smith                                        MatZeroEntries_MPIAIJ,
3053d519adbfSMatthew Knepley                                 /*24*/ MatZeroRows_MPIAIJ,
3054cda55fadSBarry Smith                                        0,
3055519f805aSKarl Rupp #if defined(PETSC_HAVE_PBGL)
3056719d5645SBarry Smith                                        0,
3057103bf8bdSMatthew Knepley #else
3058cda55fadSBarry Smith                                        0,
3059103bf8bdSMatthew Knepley #endif
3060cda55fadSBarry Smith                                        0,
3061cda55fadSBarry Smith                                        0,
30624994cf47SJed Brown                                 /*29*/ MatSetUp_MPIAIJ,
3063519f805aSKarl Rupp #if defined(PETSC_HAVE_PBGL)
3064719d5645SBarry Smith                                        0,
3065103bf8bdSMatthew Knepley #else
3066cda55fadSBarry Smith                                        0,
3067103bf8bdSMatthew Knepley #endif
3068cda55fadSBarry Smith                                        0,
3069cda55fadSBarry Smith                                        0,
3070cda55fadSBarry Smith                                        0,
3071d519adbfSMatthew Knepley                                 /*34*/ MatDuplicate_MPIAIJ,
3072cda55fadSBarry Smith                                        0,
3073cda55fadSBarry Smith                                        0,
3074cda55fadSBarry Smith                                        0,
3075cda55fadSBarry Smith                                        0,
3076d519adbfSMatthew Knepley                                 /*39*/ MatAXPY_MPIAIJ,
3077cda55fadSBarry Smith                                        MatGetSubMatrices_MPIAIJ,
3078cda55fadSBarry Smith                                        MatIncreaseOverlap_MPIAIJ,
3079cda55fadSBarry Smith                                        MatGetValues_MPIAIJ,
3080cb5b572fSBarry Smith                                        MatCopy_MPIAIJ,
3081d519adbfSMatthew Knepley                                 /*44*/ MatGetRowMax_MPIAIJ,
3082cda55fadSBarry Smith                                        MatScale_MPIAIJ,
3083cda55fadSBarry Smith                                        0,
3084cda55fadSBarry Smith                                        0,
3085564f14d6SBarry Smith                                        MatZeroRowsColumns_MPIAIJ,
308673a71a0fSBarry Smith                                 /*49*/ MatSetRandom_MPIAIJ,
3087cda55fadSBarry Smith                                        0,
3088cda55fadSBarry Smith                                        0,
3089cda55fadSBarry Smith                                        0,
3090cda55fadSBarry Smith                                        0,
309193dfae19SHong Zhang                                 /*54*/ MatFDColoringCreate_MPIXAIJ,
3092cda55fadSBarry Smith                                        0,
3093cda55fadSBarry Smith                                        MatSetUnfactored_MPIAIJ,
309472e6a0cfSJed Brown                                        MatPermute_MPIAIJ,
3095cda55fadSBarry Smith                                        0,
3096d519adbfSMatthew Knepley                                 /*59*/ MatGetSubMatrix_MPIAIJ,
3097e03a110bSBarry Smith                                        MatDestroy_MPIAIJ,
3098e03a110bSBarry Smith                                        MatView_MPIAIJ,
3099357abbc8SBarry Smith                                        0,
3100f996eeb8SHong Zhang                                        MatMatMatMult_MPIAIJ_MPIAIJ_MPIAIJ,
3101f996eeb8SHong Zhang                                 /*64*/ MatMatMatMultSymbolic_MPIAIJ_MPIAIJ_MPIAIJ,
3102f996eeb8SHong Zhang                                        MatMatMatMultNumeric_MPIAIJ_MPIAIJ_MPIAIJ,
3103a2243be0SBarry Smith                                        0,
3104a2243be0SBarry Smith                                        0,
3105a2243be0SBarry Smith                                        0,
3106d519adbfSMatthew Knepley                                 /*69*/ MatGetRowMaxAbs_MPIAIJ,
3107c87e5d42SMatthew Knepley                                        MatGetRowMinAbs_MPIAIJ,
3108a2243be0SBarry Smith                                        0,
3109a2243be0SBarry Smith                                        MatSetColoring_MPIAIJ,
3110dcf5cc72SBarry Smith                                        0,
311197304618SKris Buschelman                                        MatSetValuesAdifor_MPIAIJ,
31123acb8795SBarry Smith                                 /*75*/ MatFDColoringApply_AIJ,
311397304618SKris Buschelman                                        0,
311497304618SKris Buschelman                                        0,
311597304618SKris Buschelman                                        0,
3116f1f41ecbSJed Brown                                        MatFindZeroDiagonals_MPIAIJ,
311797304618SKris Buschelman                                 /*80*/ 0,
311897304618SKris Buschelman                                        0,
311997304618SKris Buschelman                                        0,
31205bba2384SShri Abhyankar                                 /*83*/ MatLoad_MPIAIJ,
31216284ec50SHong Zhang                                        0,
31226284ec50SHong Zhang                                        0,
31236284ec50SHong Zhang                                        0,
31246284ec50SHong Zhang                                        0,
3125865e5f61SKris Buschelman                                        0,
3126d519adbfSMatthew Knepley                                 /*89*/ MatMatMult_MPIAIJ_MPIAIJ,
312726be0446SHong Zhang                                        MatMatMultSymbolic_MPIAIJ_MPIAIJ,
312826be0446SHong Zhang                                        MatMatMultNumeric_MPIAIJ_MPIAIJ,
3129cf3ca8ceSHong Zhang                                        MatPtAP_MPIAIJ_MPIAIJ,
3130cf3ca8ceSHong Zhang                                        MatPtAPSymbolic_MPIAIJ_MPIAIJ,
3131cf3ca8ceSHong Zhang                                 /*94*/ MatPtAPNumeric_MPIAIJ_MPIAIJ,
31327a7894deSKris Buschelman                                        0,
31337a7894deSKris Buschelman                                        0,
31347a7894deSKris Buschelman                                        0,
31357a7894deSKris Buschelman                                        0,
3136d519adbfSMatthew Knepley                                 /*99*/ 0,
3137d2b207f1SPeter Brune                                        0,
3138d2b207f1SPeter Brune                                        0,
31392fd7e33dSBarry Smith                                        MatConjugate_MPIAIJ,
31402fd7e33dSBarry Smith                                        0,
3141d519adbfSMatthew Knepley                                 /*104*/MatSetValuesRow_MPIAIJ,
314299cafbc1SBarry Smith                                        MatRealPart_MPIAIJ,
314369db28dcSHong Zhang                                        MatImaginaryPart_MPIAIJ,
314469db28dcSHong Zhang                                        0,
314569db28dcSHong Zhang                                        0,
3146d519adbfSMatthew Knepley                                 /*109*/0,
314703bc72f1SMatthew Knepley                                        MatGetRedundantMatrix_MPIAIJ,
31485494a064SHong Zhang                                        MatGetRowMin_MPIAIJ,
31495494a064SHong Zhang                                        0,
31505494a064SHong Zhang                                        0,
3151d1adec66SJed Brown                                 /*114*/MatGetSeqNonzeroStructure_MPIAIJ,
3152bd0c2dcbSBarry Smith                                        0,
3153bd0c2dcbSBarry Smith                                        0,
3154bd0c2dcbSBarry Smith                                        0,
3155bd0c2dcbSBarry Smith                                        0,
31568fb81238SShri Abhyankar                                 /*119*/0,
31578fb81238SShri Abhyankar                                        0,
31588fb81238SShri Abhyankar                                        0,
3159d6037b41SHong Zhang                                        0,
3160b9614d88SDmitry Karpeev                                        MatGetMultiProcBlock_MPIAIJ,
3161f2c98031SJed Brown                                 /*124*/MatFindNonzeroRows_MPIAIJ,
31620716a85fSBarry Smith                                        MatGetColumnNorms_MPIAIJ,
3163bbead8a2SBarry Smith                                        MatInvertBlockDiagonal_MPIAIJ,
3164b9614d88SDmitry Karpeev                                        0,
316537868618SMatthew G Knepley                                        MatGetSubMatricesParallel_MPIAIJ,
3166187b3c17SHong Zhang                                 /*129*/0,
3167187b3c17SHong Zhang                                        MatTransposeMatMult_MPIAIJ_MPIAIJ,
3168187b3c17SHong Zhang                                        MatTransposeMatMultSymbolic_MPIAIJ_MPIAIJ,
3169187b3c17SHong Zhang                                        MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ,
3170187b3c17SHong Zhang                                        0,
3171187b3c17SHong Zhang                                 /*134*/0,
3172187b3c17SHong Zhang                                        0,
3173187b3c17SHong Zhang                                        0,
3174187b3c17SHong Zhang                                        0,
31753964eb88SJed Brown                                        0,
31763964eb88SJed Brown                                 /*139*/0,
3177f9426fe0SMark Adams                                        0,
3178f86b9fbaSHong Zhang                                        0,
3179f86b9fbaSHong Zhang                                        MatFDColoringSetUp_MPIXAIJ
3180bd0c2dcbSBarry Smith };
318136ce4990SBarry Smith 
31822e8a6d31SBarry Smith /* ----------------------------------------------------------------------------------------*/
31832e8a6d31SBarry Smith 
31844a2ae208SSatish Balay #undef __FUNCT__
31854a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_MPIAIJ"
31867087cfbeSBarry Smith PetscErrorCode  MatStoreValues_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 = MatStoreValues(aij->A);CHKERRQ(ierr);
31932e8a6d31SBarry Smith   ierr = MatStoreValues(aij->B);CHKERRQ(ierr);
31942e8a6d31SBarry Smith   PetscFunctionReturn(0);
31952e8a6d31SBarry Smith }
31962e8a6d31SBarry Smith 
31974a2ae208SSatish Balay #undef __FUNCT__
31984a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_MPIAIJ"
31997087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_MPIAIJ(Mat mat)
32002e8a6d31SBarry Smith {
32012e8a6d31SBarry Smith   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
3202dfbe8321SBarry Smith   PetscErrorCode ierr;
32032e8a6d31SBarry Smith 
32042e8a6d31SBarry Smith   PetscFunctionBegin;
32052e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr);
32062e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr);
32072e8a6d31SBarry Smith   PetscFunctionReturn(0);
32082e8a6d31SBarry Smith }
32098a729477SBarry Smith 
32104a2ae208SSatish Balay #undef __FUNCT__
3211a23d5eceSKris Buschelman #define __FUNCT__ "MatMPIAIJSetPreallocation_MPIAIJ"
32127087cfbeSBarry Smith PetscErrorCode  MatMPIAIJSetPreallocation_MPIAIJ(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
3213a23d5eceSKris Buschelman {
3214a23d5eceSKris Buschelman   Mat_MPIAIJ     *b;
3215dfbe8321SBarry Smith   PetscErrorCode ierr;
3216a23d5eceSKris Buschelman 
3217a23d5eceSKris Buschelman   PetscFunctionBegin;
321826283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
321926283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3220a23d5eceSKris Buschelman   b = (Mat_MPIAIJ*)B->data;
3221899cda47SBarry Smith 
3222526dfc15SBarry Smith   if (!B->preallocated) {
3223899cda47SBarry Smith     /* Explicitly create 2 MATSEQAIJ matrices. */
3224899cda47SBarry Smith     ierr = MatCreate(PETSC_COMM_SELF,&b->A);CHKERRQ(ierr);
3225d0f46423SBarry Smith     ierr = MatSetSizes(b->A,B->rmap->n,B->cmap->n,B->rmap->n,B->cmap->n);CHKERRQ(ierr);
322633d57670SJed Brown     ierr = MatSetBlockSizesFromMats(b->A,B,B);CHKERRQ(ierr);
3227899cda47SBarry Smith     ierr = MatSetType(b->A,MATSEQAIJ);CHKERRQ(ierr);
32283bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)B,(PetscObject)b->A);CHKERRQ(ierr);
3229899cda47SBarry Smith     ierr = MatCreate(PETSC_COMM_SELF,&b->B);CHKERRQ(ierr);
3230d0f46423SBarry Smith     ierr = MatSetSizes(b->B,B->rmap->n,B->cmap->N,B->rmap->n,B->cmap->N);CHKERRQ(ierr);
323133d57670SJed Brown     ierr = MatSetBlockSizesFromMats(b->B,B,B);CHKERRQ(ierr);
3232899cda47SBarry Smith     ierr = MatSetType(b->B,MATSEQAIJ);CHKERRQ(ierr);
32333bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)B,(PetscObject)b->B);CHKERRQ(ierr);
3234526dfc15SBarry Smith   }
3235899cda47SBarry Smith 
3236c60e587dSKris Buschelman   ierr = MatSeqAIJSetPreallocation(b->A,d_nz,d_nnz);CHKERRQ(ierr);
3237c60e587dSKris Buschelman   ierr = MatSeqAIJSetPreallocation(b->B,o_nz,o_nnz);CHKERRQ(ierr);
3238526dfc15SBarry Smith   B->preallocated = PETSC_TRUE;
3239a23d5eceSKris Buschelman   PetscFunctionReturn(0);
3240a23d5eceSKris Buschelman }
3241a23d5eceSKris Buschelman 
32424a2ae208SSatish Balay #undef __FUNCT__
32434a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_MPIAIJ"
3244dfbe8321SBarry Smith PetscErrorCode MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
3245d6dfbf8fSBarry Smith {
3246d6dfbf8fSBarry Smith   Mat            mat;
3247416022c9SBarry Smith   Mat_MPIAIJ     *a,*oldmat = (Mat_MPIAIJ*)matin->data;
3248dfbe8321SBarry Smith   PetscErrorCode ierr;
3249d6dfbf8fSBarry Smith 
32503a40ed3dSBarry Smith   PetscFunctionBegin;
3251416022c9SBarry Smith   *newmat = 0;
3252ce94432eSBarry Smith   ierr    = MatCreate(PetscObjectComm((PetscObject)matin),&mat);CHKERRQ(ierr);
3253d0f46423SBarry Smith   ierr    = MatSetSizes(mat,matin->rmap->n,matin->cmap->n,matin->rmap->N,matin->cmap->N);CHKERRQ(ierr);
325433d57670SJed Brown   ierr    = MatSetBlockSizesFromMats(mat,matin,matin);CHKERRQ(ierr);
32557adad957SLisandro Dalcin   ierr    = MatSetType(mat,((PetscObject)matin)->type_name);CHKERRQ(ierr);
32561d5dac46SHong Zhang   ierr    = PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
3257273d9f13SBarry Smith   a       = (Mat_MPIAIJ*)mat->data;
3258e1b6402fSHong Zhang 
3259d5f3da31SBarry Smith   mat->factortype   = matin->factortype;
3260c456f294SBarry Smith   mat->assembled    = PETSC_TRUE;
3261e7641de0SSatish Balay   mat->insertmode   = NOT_SET_VALUES;
3262273d9f13SBarry Smith   mat->preallocated = PETSC_TRUE;
3263d6dfbf8fSBarry Smith 
326417699dbbSLois Curfman McInnes   a->size         = oldmat->size;
326517699dbbSLois Curfman McInnes   a->rank         = oldmat->rank;
3266e7641de0SSatish Balay   a->donotstash   = oldmat->donotstash;
3267e7641de0SSatish Balay   a->roworiented  = oldmat->roworiented;
3268e7641de0SSatish Balay   a->rowindices   = 0;
3269bcd2baecSBarry Smith   a->rowvalues    = 0;
3270bcd2baecSBarry Smith   a->getrowactive = PETSC_FALSE;
3271d6dfbf8fSBarry Smith 
32721e1e43feSBarry Smith   ierr = PetscLayoutReference(matin->rmap,&mat->rmap);CHKERRQ(ierr);
32731e1e43feSBarry Smith   ierr = PetscLayoutReference(matin->cmap,&mat->cmap);CHKERRQ(ierr);
3274899cda47SBarry Smith 
32752ee70a88SLois Curfman McInnes   if (oldmat->colmap) {
3276aa482453SBarry Smith #if defined(PETSC_USE_CTABLE)
32770f5bd95cSBarry Smith     ierr = PetscTableCreateCopy(oldmat->colmap,&a->colmap);CHKERRQ(ierr);
3278b1fc9764SSatish Balay #else
3279785e854fSJed Brown     ierr = PetscMalloc1((mat->cmap->N),&a->colmap);CHKERRQ(ierr);
32803bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)mat,(mat->cmap->N)*sizeof(PetscInt));CHKERRQ(ierr);
3281d0f46423SBarry Smith     ierr = PetscMemcpy(a->colmap,oldmat->colmap,(mat->cmap->N)*sizeof(PetscInt));CHKERRQ(ierr);
3282b1fc9764SSatish Balay #endif
3283416022c9SBarry Smith   } else a->colmap = 0;
32843f41c07dSBarry Smith   if (oldmat->garray) {
3285b1d57f15SBarry Smith     PetscInt len;
3286d0f46423SBarry Smith     len  = oldmat->B->cmap->n;
3287785e854fSJed Brown     ierr = PetscMalloc1((len+1),&a->garray);CHKERRQ(ierr);
32883bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)mat,len*sizeof(PetscInt));CHKERRQ(ierr);
3289b1d57f15SBarry Smith     if (len) { ierr = PetscMemcpy(a->garray,oldmat->garray,len*sizeof(PetscInt));CHKERRQ(ierr); }
3290416022c9SBarry Smith   } else a->garray = 0;
3291d6dfbf8fSBarry Smith 
3292416022c9SBarry Smith   ierr    = VecDuplicate(oldmat->lvec,&a->lvec);CHKERRQ(ierr);
32933bb1ff40SBarry Smith   ierr    = PetscLogObjectParent((PetscObject)mat,(PetscObject)a->lvec);CHKERRQ(ierr);
3294a56f8943SBarry Smith   ierr    = VecScatterCopy(oldmat->Mvctx,&a->Mvctx);CHKERRQ(ierr);
32953bb1ff40SBarry Smith   ierr    = PetscLogObjectParent((PetscObject)mat,(PetscObject)a->Mvctx);CHKERRQ(ierr);
32962e8a6d31SBarry Smith   ierr    = MatDuplicate(oldmat->A,cpvalues,&a->A);CHKERRQ(ierr);
32973bb1ff40SBarry Smith   ierr    = PetscLogObjectParent((PetscObject)mat,(PetscObject)a->A);CHKERRQ(ierr);
32982e8a6d31SBarry Smith   ierr    = MatDuplicate(oldmat->B,cpvalues,&a->B);CHKERRQ(ierr);
32993bb1ff40SBarry Smith   ierr    = PetscLogObjectParent((PetscObject)mat,(PetscObject)a->B);CHKERRQ(ierr);
3300140e18c1SBarry Smith   ierr    = PetscFunctionListDuplicate(((PetscObject)matin)->qlist,&((PetscObject)mat)->qlist);CHKERRQ(ierr);
33018a729477SBarry Smith   *newmat = mat;
33023a40ed3dSBarry Smith   PetscFunctionReturn(0);
33038a729477SBarry Smith }
3304416022c9SBarry Smith 
33051a4ee126SBarry Smith 
33061a4ee126SBarry Smith 
33074a2ae208SSatish Balay #undef __FUNCT__
33085bba2384SShri Abhyankar #define __FUNCT__ "MatLoad_MPIAIJ"
3309112444f4SShri Abhyankar PetscErrorCode MatLoad_MPIAIJ(Mat newMat, PetscViewer viewer)
33108fb81238SShri Abhyankar {
33118fb81238SShri Abhyankar   PetscScalar    *vals,*svals;
3312ce94432eSBarry Smith   MPI_Comm       comm;
33138fb81238SShri Abhyankar   PetscErrorCode ierr;
33141a4ee126SBarry Smith   PetscMPIInt    rank,size,tag = ((PetscObject)viewer)->tag;
33158fb81238SShri Abhyankar   PetscInt       i,nz,j,rstart,rend,mmax,maxnz = 0,grows,gcols;
33168fb81238SShri Abhyankar   PetscInt       header[4],*rowlengths = 0,M,N,m,*cols;
33170298fd71SBarry Smith   PetscInt       *ourlens = NULL,*procsnz = NULL,*offlens = NULL,jj,*mycols,*smycols;
33188fb81238SShri Abhyankar   PetscInt       cend,cstart,n,*rowners,sizesset=1;
33198fb81238SShri Abhyankar   int            fd;
33203059b6faSBarry Smith   PetscInt       bs = newMat->rmap->bs;
33218fb81238SShri Abhyankar 
33228fb81238SShri Abhyankar   PetscFunctionBegin;
3323ce94432eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
33248fb81238SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
33258fb81238SShri Abhyankar   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
33268fb81238SShri Abhyankar   if (!rank) {
33278fb81238SShri Abhyankar     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
33288fb81238SShri Abhyankar     ierr = PetscBinaryRead(fd,(char*)header,4,PETSC_INT);CHKERRQ(ierr);
33298fb81238SShri Abhyankar     if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
33308fb81238SShri Abhyankar   }
33318fb81238SShri Abhyankar 
33320298fd71SBarry Smith   ierr = PetscOptionsBegin(comm,NULL,"Options for loading SEQAIJ matrix","Mat");CHKERRQ(ierr);
33330298fd71SBarry Smith   ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);CHKERRQ(ierr);
333408ea439dSMark F. Adams   ierr = PetscOptionsEnd();CHKERRQ(ierr);
33353059b6faSBarry Smith   if (bs < 0) bs = 1;
333608ea439dSMark F. Adams 
33378fb81238SShri Abhyankar   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) sizesset = 0;
33388fb81238SShri Abhyankar 
33398fb81238SShri Abhyankar   ierr = MPI_Bcast(header+1,3,MPIU_INT,0,comm);CHKERRQ(ierr);
33408fb81238SShri Abhyankar   M    = header[1]; N = header[2];
33418fb81238SShri Abhyankar   /* If global rows/cols are set to PETSC_DECIDE, set it to the sizes given in the file */
33428fb81238SShri Abhyankar   if (sizesset && newMat->rmap->N < 0) newMat->rmap->N = M;
33438fb81238SShri Abhyankar   if (sizesset && newMat->cmap->N < 0) newMat->cmap->N = N;
33448fb81238SShri Abhyankar 
33458fb81238SShri Abhyankar   /* If global sizes are set, check if they are consistent with that given in the file */
33468fb81238SShri Abhyankar   if (sizesset) {
33478fb81238SShri Abhyankar     ierr = MatGetSize(newMat,&grows,&gcols);CHKERRQ(ierr);
33488fb81238SShri Abhyankar   }
3349abd38a8fSBarry 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);
3350abd38a8fSBarry 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);
33518fb81238SShri Abhyankar 
335208ea439dSMark F. Adams   /* determine ownership of all (block) rows */
335308ea439dSMark F. Adams   if (M%bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Inconsistent # of rows (%d) and block size (%d)",M,bs);
335408ea439dSMark F. Adams   if (newMat->rmap->n < 0) m = bs*((M/bs)/size + (((M/bs) % size) > rank));    /* PETSC_DECIDE */
33554683f7a4SShri Abhyankar   else m = newMat->rmap->n; /* Set by user */
33568fb81238SShri Abhyankar 
3357785e854fSJed Brown   ierr = PetscMalloc1((size+1),&rowners);CHKERRQ(ierr);
33588fb81238SShri Abhyankar   ierr = MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);CHKERRQ(ierr);
33598fb81238SShri Abhyankar 
33608fb81238SShri Abhyankar   /* First process needs enough room for process with most rows */
33618fb81238SShri Abhyankar   if (!rank) {
33628fb81238SShri Abhyankar     mmax = rowners[1];
33635c4ea359SMatthew G Knepley     for (i=2; i<=size; i++) {
33648fb81238SShri Abhyankar       mmax = PetscMax(mmax, rowners[i]);
33658fb81238SShri Abhyankar     }
33663964eb88SJed Brown   } else mmax = -1;             /* unused, but compilers complain */
33678fb81238SShri Abhyankar 
33688fb81238SShri Abhyankar   rowners[0] = 0;
33698fb81238SShri Abhyankar   for (i=2; i<=size; i++) {
33708fb81238SShri Abhyankar     rowners[i] += rowners[i-1];
33718fb81238SShri Abhyankar   }
33728fb81238SShri Abhyankar   rstart = rowners[rank];
33738fb81238SShri Abhyankar   rend   = rowners[rank+1];
33748fb81238SShri Abhyankar 
33758fb81238SShri Abhyankar   /* distribute row lengths to all processors */
3376dcca6d9dSJed Brown   ierr = PetscMalloc2(m,&ourlens,m,&offlens);CHKERRQ(ierr);
33778fb81238SShri Abhyankar   if (!rank) {
33788fb81238SShri Abhyankar     ierr = PetscBinaryRead(fd,ourlens,m,PETSC_INT);CHKERRQ(ierr);
3379785e854fSJed Brown     ierr = PetscMalloc1(mmax,&rowlengths);CHKERRQ(ierr);
33801795a4d1SJed Brown     ierr = PetscCalloc1(size,&procsnz);CHKERRQ(ierr);
33818fb81238SShri Abhyankar     for (j=0; j<m; j++) {
33828fb81238SShri Abhyankar       procsnz[0] += ourlens[j];
33838fb81238SShri Abhyankar     }
33848fb81238SShri Abhyankar     for (i=1; i<size; i++) {
33858fb81238SShri Abhyankar       ierr = PetscBinaryRead(fd,rowlengths,rowners[i+1]-rowners[i],PETSC_INT);CHKERRQ(ierr);
33868fb81238SShri Abhyankar       /* calculate the number of nonzeros on each processor */
33878fb81238SShri Abhyankar       for (j=0; j<rowners[i+1]-rowners[i]; j++) {
33888fb81238SShri Abhyankar         procsnz[i] += rowlengths[j];
33898fb81238SShri Abhyankar       }
3390a25532f0SBarry Smith       ierr = MPIULong_Send(rowlengths,rowners[i+1]-rowners[i],MPIU_INT,i,tag,comm);CHKERRQ(ierr);
33918fb81238SShri Abhyankar     }
33928fb81238SShri Abhyankar     ierr = PetscFree(rowlengths);CHKERRQ(ierr);
33938fb81238SShri Abhyankar   } else {
3394a25532f0SBarry Smith     ierr = MPIULong_Recv(ourlens,m,MPIU_INT,0,tag,comm);CHKERRQ(ierr);
33958fb81238SShri Abhyankar   }
33968fb81238SShri Abhyankar 
33978fb81238SShri Abhyankar   if (!rank) {
33988fb81238SShri Abhyankar     /* determine max buffer needed and allocate it */
33998fb81238SShri Abhyankar     maxnz = 0;
34008fb81238SShri Abhyankar     for (i=0; i<size; i++) {
34018fb81238SShri Abhyankar       maxnz = PetscMax(maxnz,procsnz[i]);
34028fb81238SShri Abhyankar     }
3403785e854fSJed Brown     ierr = PetscMalloc1(maxnz,&cols);CHKERRQ(ierr);
34048fb81238SShri Abhyankar 
34058fb81238SShri Abhyankar     /* read in my part of the matrix column indices  */
34068fb81238SShri Abhyankar     nz   = procsnz[0];
3407785e854fSJed Brown     ierr = PetscMalloc1(nz,&mycols);CHKERRQ(ierr);
34088fb81238SShri Abhyankar     ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr);
34098fb81238SShri Abhyankar 
34108fb81238SShri Abhyankar     /* read in every one elses and ship off */
34118fb81238SShri Abhyankar     for (i=1; i<size; i++) {
34128fb81238SShri Abhyankar       nz   = procsnz[i];
34138fb81238SShri Abhyankar       ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr);
3414a25532f0SBarry Smith       ierr = MPIULong_Send(cols,nz,MPIU_INT,i,tag,comm);CHKERRQ(ierr);
34158fb81238SShri Abhyankar     }
34168fb81238SShri Abhyankar     ierr = PetscFree(cols);CHKERRQ(ierr);
34178fb81238SShri Abhyankar   } else {
34188fb81238SShri Abhyankar     /* determine buffer space needed for message */
34198fb81238SShri Abhyankar     nz = 0;
34208fb81238SShri Abhyankar     for (i=0; i<m; i++) {
34218fb81238SShri Abhyankar       nz += ourlens[i];
34228fb81238SShri Abhyankar     }
3423785e854fSJed Brown     ierr = PetscMalloc1(nz,&mycols);CHKERRQ(ierr);
34248fb81238SShri Abhyankar 
34258fb81238SShri Abhyankar     /* receive message of column indices*/
3426a25532f0SBarry Smith     ierr = MPIULong_Recv(mycols,nz,MPIU_INT,0,tag,comm);CHKERRQ(ierr);
34278fb81238SShri Abhyankar   }
34288fb81238SShri Abhyankar 
34298fb81238SShri Abhyankar   /* determine column ownership if matrix is not square */
34308fb81238SShri Abhyankar   if (N != M) {
34318fb81238SShri Abhyankar     if (newMat->cmap->n < 0) n = N/size + ((N % size) > rank);
34328fb81238SShri Abhyankar     else n = newMat->cmap->n;
34338fb81238SShri Abhyankar     ierr   = MPI_Scan(&n,&cend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr);
34348fb81238SShri Abhyankar     cstart = cend - n;
34358fb81238SShri Abhyankar   } else {
34368fb81238SShri Abhyankar     cstart = rstart;
34378fb81238SShri Abhyankar     cend   = rend;
34388fb81238SShri Abhyankar     n      = cend - cstart;
34398fb81238SShri Abhyankar   }
34408fb81238SShri Abhyankar 
34418fb81238SShri Abhyankar   /* loop over local rows, determining number of off diagonal entries */
34428fb81238SShri Abhyankar   ierr = PetscMemzero(offlens,m*sizeof(PetscInt));CHKERRQ(ierr);
34438fb81238SShri Abhyankar   jj   = 0;
34448fb81238SShri Abhyankar   for (i=0; i<m; i++) {
34458fb81238SShri Abhyankar     for (j=0; j<ourlens[i]; j++) {
34468fb81238SShri Abhyankar       if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++;
34478fb81238SShri Abhyankar       jj++;
34488fb81238SShri Abhyankar     }
34498fb81238SShri Abhyankar   }
34508fb81238SShri Abhyankar 
34518fb81238SShri Abhyankar   for (i=0; i<m; i++) {
34528fb81238SShri Abhyankar     ourlens[i] -= offlens[i];
34538fb81238SShri Abhyankar   }
34548fb81238SShri Abhyankar   if (!sizesset) {
34558fb81238SShri Abhyankar     ierr = MatSetSizes(newMat,m,n,M,N);CHKERRQ(ierr);
34568fb81238SShri Abhyankar   }
345708ea439dSMark F. Adams 
345808ea439dSMark F. Adams   if (bs > 1) {ierr = MatSetBlockSize(newMat,bs);CHKERRQ(ierr);}
345908ea439dSMark F. Adams 
34608fb81238SShri Abhyankar   ierr = MatMPIAIJSetPreallocation(newMat,0,ourlens,0,offlens);CHKERRQ(ierr);
34618fb81238SShri Abhyankar 
34628fb81238SShri Abhyankar   for (i=0; i<m; i++) {
34638fb81238SShri Abhyankar     ourlens[i] += offlens[i];
34648fb81238SShri Abhyankar   }
34658fb81238SShri Abhyankar 
34668fb81238SShri Abhyankar   if (!rank) {
3467785e854fSJed Brown     ierr = PetscMalloc1((maxnz+1),&vals);CHKERRQ(ierr);
34688fb81238SShri Abhyankar 
34698fb81238SShri Abhyankar     /* read in my part of the matrix numerical values  */
34708fb81238SShri Abhyankar     nz   = procsnz[0];
34718fb81238SShri Abhyankar     ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
34728fb81238SShri Abhyankar 
34738fb81238SShri Abhyankar     /* insert into matrix */
34748fb81238SShri Abhyankar     jj      = rstart;
34758fb81238SShri Abhyankar     smycols = mycols;
34768fb81238SShri Abhyankar     svals   = vals;
34778fb81238SShri Abhyankar     for (i=0; i<m; i++) {
34788fb81238SShri Abhyankar       ierr     = MatSetValues_MPIAIJ(newMat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
34798fb81238SShri Abhyankar       smycols += ourlens[i];
34808fb81238SShri Abhyankar       svals   += ourlens[i];
34818fb81238SShri Abhyankar       jj++;
34828fb81238SShri Abhyankar     }
34838fb81238SShri Abhyankar 
34848fb81238SShri Abhyankar     /* read in other processors and ship out */
34858fb81238SShri Abhyankar     for (i=1; i<size; i++) {
34868fb81238SShri Abhyankar       nz   = procsnz[i];
34878fb81238SShri Abhyankar       ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
3488a25532f0SBarry Smith       ierr = MPIULong_Send(vals,nz,MPIU_SCALAR,i,((PetscObject)newMat)->tag,comm);CHKERRQ(ierr);
34898fb81238SShri Abhyankar     }
34908fb81238SShri Abhyankar     ierr = PetscFree(procsnz);CHKERRQ(ierr);
34918fb81238SShri Abhyankar   } else {
34928fb81238SShri Abhyankar     /* receive numeric values */
3493785e854fSJed Brown     ierr = PetscMalloc1((nz+1),&vals);CHKERRQ(ierr);
34948fb81238SShri Abhyankar 
34958fb81238SShri Abhyankar     /* receive message of values*/
3496a25532f0SBarry Smith     ierr = MPIULong_Recv(vals,nz,MPIU_SCALAR,0,((PetscObject)newMat)->tag,comm);CHKERRQ(ierr);
34978fb81238SShri Abhyankar 
34988fb81238SShri Abhyankar     /* insert into matrix */
34998fb81238SShri Abhyankar     jj      = rstart;
35008fb81238SShri Abhyankar     smycols = mycols;
35018fb81238SShri Abhyankar     svals   = vals;
35028fb81238SShri Abhyankar     for (i=0; i<m; i++) {
35038fb81238SShri Abhyankar       ierr     = MatSetValues_MPIAIJ(newMat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
35048fb81238SShri Abhyankar       smycols += ourlens[i];
35058fb81238SShri Abhyankar       svals   += ourlens[i];
35068fb81238SShri Abhyankar       jj++;
35078fb81238SShri Abhyankar     }
35088fb81238SShri Abhyankar   }
35098fb81238SShri Abhyankar   ierr = PetscFree2(ourlens,offlens);CHKERRQ(ierr);
35108fb81238SShri Abhyankar   ierr = PetscFree(vals);CHKERRQ(ierr);
35118fb81238SShri Abhyankar   ierr = PetscFree(mycols);CHKERRQ(ierr);
35128fb81238SShri Abhyankar   ierr = PetscFree(rowners);CHKERRQ(ierr);
35138fb81238SShri Abhyankar   ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
35148fb81238SShri Abhyankar   ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
35158fb81238SShri Abhyankar   PetscFunctionReturn(0);
35168fb81238SShri Abhyankar }
35178fb81238SShri Abhyankar 
35188fb81238SShri Abhyankar #undef __FUNCT__
35194a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_MPIAIJ"
35204aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,MatReuse call,Mat *newmat)
35214aa3045dSJed Brown {
35224aa3045dSJed Brown   PetscErrorCode ierr;
35234aa3045dSJed Brown   IS             iscol_local;
35244aa3045dSJed Brown   PetscInt       csize;
35254aa3045dSJed Brown 
35264aa3045dSJed Brown   PetscFunctionBegin;
35274aa3045dSJed Brown   ierr = ISGetLocalSize(iscol,&csize);CHKERRQ(ierr);
3528b79d0421SJed Brown   if (call == MAT_REUSE_MATRIX) {
3529b79d0421SJed Brown     ierr = PetscObjectQuery((PetscObject)*newmat,"ISAllGather",(PetscObject*)&iscol_local);CHKERRQ(ierr);
3530e32f2f54SBarry Smith     if (!iscol_local) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
3531b79d0421SJed Brown   } else {
3532c5bfad50SMark F. Adams     PetscInt cbs;
3533c5bfad50SMark F. Adams     ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
35344aa3045dSJed Brown     ierr = ISAllGather(iscol,&iscol_local);CHKERRQ(ierr);
3535c5bfad50SMark F. Adams     ierr = ISSetBlockSize(iscol_local,cbs);CHKERRQ(ierr);
3536b79d0421SJed Brown   }
35374aa3045dSJed Brown   ierr = MatGetSubMatrix_MPIAIJ_Private(mat,isrow,iscol_local,csize,call,newmat);CHKERRQ(ierr);
3538b79d0421SJed Brown   if (call == MAT_INITIAL_MATRIX) {
3539b79d0421SJed Brown     ierr = PetscObjectCompose((PetscObject)*newmat,"ISAllGather",(PetscObject)iscol_local);CHKERRQ(ierr);
35406bf464f9SBarry Smith     ierr = ISDestroy(&iscol_local);CHKERRQ(ierr);
3541b79d0421SJed Brown   }
35424aa3045dSJed Brown   PetscFunctionReturn(0);
35434aa3045dSJed Brown }
35444aa3045dSJed Brown 
354529dcf524SDmitry Karpeev extern PetscErrorCode MatGetSubMatrices_MPIAIJ_Local(Mat,PetscInt,const IS[],const IS[],MatReuse,PetscBool*,Mat*);
35464aa3045dSJed Brown #undef __FUNCT__
35474aa3045dSJed Brown #define __FUNCT__ "MatGetSubMatrix_MPIAIJ_Private"
3548a0ff6018SBarry Smith /*
354929da9460SBarry Smith     Not great since it makes two copies of the submatrix, first an SeqAIJ
355029da9460SBarry Smith   in local and then by concatenating the local matrices the end result.
355129da9460SBarry Smith   Writing it directly would be much like MatGetSubMatrices_MPIAIJ()
35524aa3045dSJed Brown 
35534aa3045dSJed Brown   Note: This requires a sequential iscol with all indices.
3554a0ff6018SBarry Smith */
35554aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIAIJ_Private(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse call,Mat *newmat)
3556a0ff6018SBarry Smith {
3557dfbe8321SBarry Smith   PetscErrorCode ierr;
355832dcc486SBarry Smith   PetscMPIInt    rank,size;
3559a2f3521dSMark F. Adams   PetscInt       i,m,n,rstart,row,rend,nz,*cwork,j,bs,cbs;
356029dcf524SDmitry Karpeev   PetscInt       *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal,ncol;
356129dcf524SDmitry Karpeev   PetscBool      allcolumns, colflag;
356229dcf524SDmitry Karpeev   Mat            M,Mreuse;
3563a77337e4SBarry Smith   MatScalar      *vwork,*aa;
3564ce94432eSBarry Smith   MPI_Comm       comm;
356500e6dbe6SBarry Smith   Mat_SeqAIJ     *aij;
35667e2c5f70SBarry Smith 
3567a0ff6018SBarry Smith   PetscFunctionBegin;
3568ce94432eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr);
35691dab6e02SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
35701dab6e02SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
357100e6dbe6SBarry Smith 
357229dcf524SDmitry Karpeev   ierr = ISIdentity(iscol,&colflag);CHKERRQ(ierr);
357329dcf524SDmitry Karpeev   ierr = ISGetLocalSize(iscol,&ncol);CHKERRQ(ierr);
357429dcf524SDmitry Karpeev   if (colflag && ncol == mat->cmap->N) {
357529dcf524SDmitry Karpeev     allcolumns = PETSC_TRUE;
357629dcf524SDmitry Karpeev   } else {
357729dcf524SDmitry Karpeev     allcolumns = PETSC_FALSE;
357829dcf524SDmitry Karpeev   }
3579fee21e36SBarry Smith   if (call ==  MAT_REUSE_MATRIX) {
3580fee21e36SBarry Smith     ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject*)&Mreuse);CHKERRQ(ierr);
3581e32f2f54SBarry Smith     if (!Mreuse) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
358229dcf524SDmitry Karpeev     ierr = MatGetSubMatrices_MPIAIJ_Local(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&allcolumns,&Mreuse);CHKERRQ(ierr);
3583fee21e36SBarry Smith   } else {
358429dcf524SDmitry Karpeev     ierr = MatGetSubMatrices_MPIAIJ_Local(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&allcolumns,&Mreuse);CHKERRQ(ierr);
3585fee21e36SBarry Smith   }
3586a0ff6018SBarry Smith 
3587a0ff6018SBarry Smith   /*
3588a0ff6018SBarry Smith       m - number of local rows
3589a0ff6018SBarry Smith       n - number of columns (same on all processors)
3590a0ff6018SBarry Smith       rstart - first row in new global matrix generated
3591a0ff6018SBarry Smith   */
3592fee21e36SBarry Smith   ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr);
3593a2f3521dSMark F. Adams   ierr = MatGetBlockSizes(Mreuse,&bs,&cbs);CHKERRQ(ierr);
3594a0ff6018SBarry Smith   if (call == MAT_INITIAL_MATRIX) {
3595fee21e36SBarry Smith     aij = (Mat_SeqAIJ*)(Mreuse)->data;
359600e6dbe6SBarry Smith     ii  = aij->i;
359700e6dbe6SBarry Smith     jj  = aij->j;
359800e6dbe6SBarry Smith 
3599a0ff6018SBarry Smith     /*
360000e6dbe6SBarry Smith         Determine the number of non-zeros in the diagonal and off-diagonal
360100e6dbe6SBarry Smith         portions of the matrix in order to do correct preallocation
3602a0ff6018SBarry Smith     */
360300e6dbe6SBarry Smith 
360400e6dbe6SBarry Smith     /* first get start and end of "diagonal" columns */
36056a6a5d1dSBarry Smith     if (csize == PETSC_DECIDE) {
3606ab50ec6bSBarry Smith       ierr = ISGetSize(isrow,&mglobal);CHKERRQ(ierr);
3607ab50ec6bSBarry Smith       if (mglobal == n) { /* square matrix */
3608e2c4fddaSBarry Smith         nlocal = m;
36096a6a5d1dSBarry Smith       } else {
3610ab50ec6bSBarry Smith         nlocal = n/size + ((n % size) > rank);
3611ab50ec6bSBarry Smith       }
3612ab50ec6bSBarry Smith     } else {
36136a6a5d1dSBarry Smith       nlocal = csize;
36146a6a5d1dSBarry Smith     }
3615b1d57f15SBarry Smith     ierr   = MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr);
361600e6dbe6SBarry Smith     rstart = rend - nlocal;
361765e19b50SBarry 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);
361800e6dbe6SBarry Smith 
361900e6dbe6SBarry Smith     /* next, compute all the lengths */
3620785e854fSJed Brown     ierr  = PetscMalloc1((2*m+1),&dlens);CHKERRQ(ierr);
362100e6dbe6SBarry Smith     olens = dlens + m;
362200e6dbe6SBarry Smith     for (i=0; i<m; i++) {
362300e6dbe6SBarry Smith       jend = ii[i+1] - ii[i];
362400e6dbe6SBarry Smith       olen = 0;
362500e6dbe6SBarry Smith       dlen = 0;
362600e6dbe6SBarry Smith       for (j=0; j<jend; j++) {
362700e6dbe6SBarry Smith         if (*jj < rstart || *jj >= rend) olen++;
362800e6dbe6SBarry Smith         else dlen++;
362900e6dbe6SBarry Smith         jj++;
363000e6dbe6SBarry Smith       }
363100e6dbe6SBarry Smith       olens[i] = olen;
363200e6dbe6SBarry Smith       dlens[i] = dlen;
363300e6dbe6SBarry Smith     }
3634f69a0ea3SMatthew Knepley     ierr = MatCreate(comm,&M);CHKERRQ(ierr);
3635f69a0ea3SMatthew Knepley     ierr = MatSetSizes(M,m,nlocal,PETSC_DECIDE,n);CHKERRQ(ierr);
3636a2f3521dSMark F. Adams     ierr = MatSetBlockSizes(M,bs,cbs);CHKERRQ(ierr);
36377adad957SLisandro Dalcin     ierr = MatSetType(M,((PetscObject)mat)->type_name);CHKERRQ(ierr);
3638e2d9671bSKris Buschelman     ierr = MatMPIAIJSetPreallocation(M,0,dlens,0,olens);CHKERRQ(ierr);
3639606d414cSSatish Balay     ierr = PetscFree(dlens);CHKERRQ(ierr);
3640a0ff6018SBarry Smith   } else {
3641b1d57f15SBarry Smith     PetscInt ml,nl;
3642a0ff6018SBarry Smith 
3643a0ff6018SBarry Smith     M    = *newmat;
3644a0ff6018SBarry Smith     ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr);
3645e32f2f54SBarry Smith     if (ml != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
3646a0ff6018SBarry Smith     ierr = MatZeroEntries(M);CHKERRQ(ierr);
3647c48de900SBarry Smith     /*
3648c48de900SBarry Smith          The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
3649c48de900SBarry Smith        rather than the slower MatSetValues().
3650c48de900SBarry Smith     */
3651c48de900SBarry Smith     M->was_assembled = PETSC_TRUE;
3652c48de900SBarry Smith     M->assembled     = PETSC_FALSE;
3653a0ff6018SBarry Smith   }
3654a0ff6018SBarry Smith   ierr = MatGetOwnershipRange(M,&rstart,&rend);CHKERRQ(ierr);
3655fee21e36SBarry Smith   aij  = (Mat_SeqAIJ*)(Mreuse)->data;
365600e6dbe6SBarry Smith   ii   = aij->i;
365700e6dbe6SBarry Smith   jj   = aij->j;
365800e6dbe6SBarry Smith   aa   = aij->a;
3659a0ff6018SBarry Smith   for (i=0; i<m; i++) {
3660a0ff6018SBarry Smith     row   = rstart + i;
366100e6dbe6SBarry Smith     nz    = ii[i+1] - ii[i];
366200e6dbe6SBarry Smith     cwork = jj;     jj += nz;
366300e6dbe6SBarry Smith     vwork = aa;     aa += nz;
36648c638d02SBarry Smith     ierr  = MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr);
3665a0ff6018SBarry Smith   }
3666a0ff6018SBarry Smith 
3667a0ff6018SBarry Smith   ierr    = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3668a0ff6018SBarry Smith   ierr    = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3669a0ff6018SBarry Smith   *newmat = M;
3670fee21e36SBarry Smith 
3671fee21e36SBarry Smith   /* save submatrix used in processor for next request */
3672fee21e36SBarry Smith   if (call ==  MAT_INITIAL_MATRIX) {
3673fee21e36SBarry Smith     ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr);
3674bf0cc555SLisandro Dalcin     ierr = MatDestroy(&Mreuse);CHKERRQ(ierr);
3675fee21e36SBarry Smith   }
3676a0ff6018SBarry Smith   PetscFunctionReturn(0);
3677a0ff6018SBarry Smith }
3678273d9f13SBarry Smith 
36794a2ae208SSatish Balay #undef __FUNCT__
3680ccd8e176SBarry Smith #define __FUNCT__ "MatMPIAIJSetPreallocationCSR_MPIAIJ"
36817087cfbeSBarry Smith PetscErrorCode  MatMPIAIJSetPreallocationCSR_MPIAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3682ccd8e176SBarry Smith {
3683899cda47SBarry Smith   PetscInt       m,cstart, cend,j,nnz,i,d;
3684899cda47SBarry Smith   PetscInt       *d_nnz,*o_nnz,nnz_max = 0,rstart,ii;
3685ccd8e176SBarry Smith   const PetscInt *JJ;
3686ccd8e176SBarry Smith   PetscScalar    *values;
3687ccd8e176SBarry Smith   PetscErrorCode ierr;
3688ccd8e176SBarry Smith 
3689ccd8e176SBarry Smith   PetscFunctionBegin;
3690e32f2f54SBarry Smith   if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Ii[0] must be 0 it is %D",Ii[0]);
3691899cda47SBarry Smith 
369226283091SBarry Smith   ierr   = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
369326283091SBarry Smith   ierr   = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3694d0f46423SBarry Smith   m      = B->rmap->n;
3695d0f46423SBarry Smith   cstart = B->cmap->rstart;
3696d0f46423SBarry Smith   cend   = B->cmap->rend;
3697d0f46423SBarry Smith   rstart = B->rmap->rstart;
3698899cda47SBarry Smith 
3699dcca6d9dSJed Brown   ierr = PetscMalloc2(m,&d_nnz,m,&o_nnz);CHKERRQ(ierr);
3700ccd8e176SBarry Smith 
3701ecc77c7aSBarry Smith #if defined(PETSC_USE_DEBUGGING)
3702ecc77c7aSBarry Smith   for (i=0; i<m; i++) {
3703ecc77c7aSBarry Smith     nnz = Ii[i+1]- Ii[i];
3704ecc77c7aSBarry Smith     JJ  = J + Ii[i];
3705e32f2f54SBarry Smith     if (nnz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative %D number of columns",i,nnz);
3706ecc77c7aSBarry Smith     if (nnz && (JJ[0] < 0)) SETERRRQ1(PETSC_ERR_ARG_WRONGSTATE,"Row %D starts with negative column index",i,j);
3707d0f46423SBarry 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);
3708ecc77c7aSBarry Smith   }
3709ecc77c7aSBarry Smith #endif
3710ecc77c7aSBarry Smith 
3711ccd8e176SBarry Smith   for (i=0; i<m; i++) {
3712b7940d39SSatish Balay     nnz     = Ii[i+1]- Ii[i];
3713b7940d39SSatish Balay     JJ      = J + Ii[i];
3714ccd8e176SBarry Smith     nnz_max = PetscMax(nnz_max,nnz);
3715ccd8e176SBarry Smith     d       = 0;
37160daa03b5SJed Brown     for (j=0; j<nnz; j++) {
37170daa03b5SJed Brown       if (cstart <= JJ[j] && JJ[j] < cend) d++;
3718ccd8e176SBarry Smith     }
3719ccd8e176SBarry Smith     d_nnz[i] = d;
3720ccd8e176SBarry Smith     o_nnz[i] = nnz - d;
3721ccd8e176SBarry Smith   }
3722ccd8e176SBarry Smith   ierr = MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);CHKERRQ(ierr);
37231d79065fSBarry Smith   ierr = PetscFree2(d_nnz,o_nnz);CHKERRQ(ierr);
3724ccd8e176SBarry Smith 
3725ccd8e176SBarry Smith   if (v) values = (PetscScalar*)v;
3726ccd8e176SBarry Smith   else {
37271795a4d1SJed Brown     ierr = PetscCalloc1((nnz_max+1),&values);CHKERRQ(ierr);
3728ccd8e176SBarry Smith   }
3729ccd8e176SBarry Smith 
3730ccd8e176SBarry Smith   for (i=0; i<m; i++) {
3731ccd8e176SBarry Smith     ii   = i + rstart;
3732b7940d39SSatish Balay     nnz  = Ii[i+1]- Ii[i];
3733b7940d39SSatish Balay     ierr = MatSetValues_MPIAIJ(B,1,&ii,nnz,J+Ii[i],values+(v ? Ii[i] : 0),INSERT_VALUES);CHKERRQ(ierr);
3734ccd8e176SBarry Smith   }
3735ccd8e176SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3736ccd8e176SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3737ccd8e176SBarry Smith 
3738ccd8e176SBarry Smith   if (!v) {
3739ccd8e176SBarry Smith     ierr = PetscFree(values);CHKERRQ(ierr);
3740ccd8e176SBarry Smith   }
37417827cd58SJed Brown   ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
3742ccd8e176SBarry Smith   PetscFunctionReturn(0);
3743ccd8e176SBarry Smith }
3744ccd8e176SBarry Smith 
3745ccd8e176SBarry Smith #undef __FUNCT__
3746ccd8e176SBarry Smith #define __FUNCT__ "MatMPIAIJSetPreallocationCSR"
37471eea217eSSatish Balay /*@
3748ccd8e176SBarry Smith    MatMPIAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in AIJ format
3749ccd8e176SBarry Smith    (the default parallel PETSc format).
3750ccd8e176SBarry Smith 
3751ccd8e176SBarry Smith    Collective on MPI_Comm
3752ccd8e176SBarry Smith 
3753ccd8e176SBarry Smith    Input Parameters:
3754a1661176SMatthew Knepley +  B - the matrix
3755ccd8e176SBarry Smith .  i - the indices into j for the start of each local row (starts with zero)
37560daa03b5SJed Brown .  j - the column indices for each local row (starts with zero)
3757ccd8e176SBarry Smith -  v - optional values in the matrix
3758ccd8e176SBarry Smith 
3759ccd8e176SBarry Smith    Level: developer
3760ccd8e176SBarry Smith 
376112251496SSatish Balay    Notes:
376212251496SSatish Balay        The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc;
376312251496SSatish Balay      thus you CANNOT change the matrix entries by changing the values of a[] after you have
376412251496SSatish Balay      called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays.
376512251496SSatish Balay 
376612251496SSatish Balay        The i and j indices are 0 based, and i indices are indices corresponding to the local j array.
376712251496SSatish Balay 
376812251496SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
376912251496SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
377012251496SSatish Balay     as shown:
377112251496SSatish Balay 
377212251496SSatish Balay         1 0 0
377312251496SSatish Balay         2 0 3     P0
377412251496SSatish Balay        -------
377512251496SSatish Balay         4 5 6     P1
377612251496SSatish Balay 
377712251496SSatish Balay      Process0 [P0]: rows_owned=[0,1]
377812251496SSatish Balay         i =  {0,1,3}  [size = nrow+1  = 2+1]
377912251496SSatish Balay         j =  {0,0,2}  [size = nz = 6]
378012251496SSatish Balay         v =  {1,2,3}  [size = nz = 6]
378112251496SSatish Balay 
378212251496SSatish Balay      Process1 [P1]: rows_owned=[2]
378312251496SSatish Balay         i =  {0,3}    [size = nrow+1  = 1+1]
378412251496SSatish Balay         j =  {0,1,2}  [size = nz = 6]
378512251496SSatish Balay         v =  {4,5,6}  [size = nz = 6]
378612251496SSatish Balay 
3787ccd8e176SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
3788ccd8e176SBarry Smith 
378969b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatCreateAIJ(), MPIAIJ,
37908d7a6e47SBarry Smith           MatCreateSeqAIJWithArrays(), MatCreateMPIAIJWithSplitArrays()
3791ccd8e176SBarry Smith @*/
37927087cfbeSBarry Smith PetscErrorCode  MatMPIAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
3793ccd8e176SBarry Smith {
37944ac538c5SBarry Smith   PetscErrorCode ierr;
3795ccd8e176SBarry Smith 
3796ccd8e176SBarry Smith   PetscFunctionBegin;
37974ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatMPIAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr);
3798ccd8e176SBarry Smith   PetscFunctionReturn(0);
3799ccd8e176SBarry Smith }
3800ccd8e176SBarry Smith 
3801ccd8e176SBarry Smith #undef __FUNCT__
38024a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJSetPreallocation"
3803273d9f13SBarry Smith /*@C
3804ccd8e176SBarry Smith    MatMPIAIJSetPreallocation - Preallocates memory for a sparse parallel matrix in AIJ format
3805273d9f13SBarry Smith    (the default parallel PETSc format).  For good matrix assembly performance
3806273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameters
3807273d9f13SBarry Smith    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
3808273d9f13SBarry Smith    performance can be increased by more than a factor of 50.
3809273d9f13SBarry Smith 
3810273d9f13SBarry Smith    Collective on MPI_Comm
3811273d9f13SBarry Smith 
3812273d9f13SBarry Smith    Input Parameters:
38131c4f3114SJed Brown +  B - the matrix
3814273d9f13SBarry Smith .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
3815273d9f13SBarry Smith            (same value is used for all local rows)
3816273d9f13SBarry Smith .  d_nnz - array containing the number of nonzeros in the various rows of the
3817273d9f13SBarry Smith            DIAGONAL portion of the local submatrix (possibly different for each row)
38180298fd71SBarry Smith            or NULL, if d_nz is used to specify the nonzero structure.
3819273d9f13SBarry Smith            The size of this array is equal to the number of local rows, i.e 'm'.
38203287b5eaSJed Brown            For matrices that will be factored, you must leave room for (and set)
38213287b5eaSJed Brown            the diagonal entry even if it is zero.
3822273d9f13SBarry Smith .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
3823273d9f13SBarry Smith            submatrix (same value is used for all local rows).
3824273d9f13SBarry Smith -  o_nnz - array containing the number of nonzeros in the various rows of the
3825273d9f13SBarry Smith            OFF-DIAGONAL portion of the local submatrix (possibly different for
38260298fd71SBarry Smith            each row) or NULL, if o_nz is used to specify the nonzero
3827273d9f13SBarry Smith            structure. The size of this array is equal to the number
3828273d9f13SBarry Smith            of local rows, i.e 'm'.
3829273d9f13SBarry Smith 
383049a6f317SBarry Smith    If the *_nnz parameter is given then the *_nz parameter is ignored
383149a6f317SBarry Smith 
3832273d9f13SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
3833ccd8e176SBarry Smith    compressed row storage (CSR)), is fully compatible with standard Fortran 77
38340598bfebSBarry Smith    storage.  The stored row and column indices begin with zero.
3835a7f22e61SSatish Balay    See Users-Manual: ch_mat for details.
3836273d9f13SBarry Smith 
3837273d9f13SBarry Smith    The parallel matrix is partitioned such that the first m0 rows belong to
3838273d9f13SBarry Smith    process 0, the next m1 rows belong to process 1, the next m2 rows belong
3839273d9f13SBarry Smith    to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.
3840273d9f13SBarry Smith 
3841273d9f13SBarry Smith    The DIAGONAL portion of the local submatrix of a processor can be defined
3842a05b864aSJed Brown    as the submatrix which is obtained by extraction the part corresponding to
3843a05b864aSJed Brown    the rows r1-r2 and columns c1-c2 of the global matrix, where r1 is the
3844a05b864aSJed Brown    first row that belongs to the processor, r2 is the last row belonging to
3845a05b864aSJed Brown    the this processor, and c1-c2 is range of indices of the local part of a
3846a05b864aSJed Brown    vector suitable for applying the matrix to.  This is an mxn matrix.  In the
3847a05b864aSJed Brown    common case of a square matrix, the row and column ranges are the same and
3848a05b864aSJed Brown    the DIAGONAL part is also square. The remaining portion of the local
3849a05b864aSJed Brown    submatrix (mxN) constitute the OFF-DIAGONAL portion.
3850273d9f13SBarry Smith 
3851273d9f13SBarry Smith    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
3852273d9f13SBarry Smith 
3853aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3854aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3855aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3856aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3857aa95bbe8SBarry Smith 
3858273d9f13SBarry Smith    Example usage:
3859273d9f13SBarry Smith 
3860273d9f13SBarry Smith    Consider the following 8x8 matrix with 34 non-zero values, that is
3861273d9f13SBarry Smith    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
3862273d9f13SBarry Smith    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
3863273d9f13SBarry Smith    as follows:
3864273d9f13SBarry Smith 
3865273d9f13SBarry Smith .vb
3866273d9f13SBarry Smith             1  2  0  |  0  3  0  |  0  4
3867273d9f13SBarry Smith     Proc0   0  5  6  |  7  0  0  |  8  0
3868273d9f13SBarry Smith             9  0 10  | 11  0  0  | 12  0
3869273d9f13SBarry Smith     -------------------------------------
3870273d9f13SBarry Smith            13  0 14  | 15 16 17  |  0  0
3871273d9f13SBarry Smith     Proc1   0 18  0  | 19 20 21  |  0  0
3872273d9f13SBarry Smith             0  0  0  | 22 23  0  | 24  0
3873273d9f13SBarry Smith     -------------------------------------
3874273d9f13SBarry Smith     Proc2  25 26 27  |  0  0 28  | 29  0
3875273d9f13SBarry Smith            30  0  0  | 31 32 33  |  0 34
3876273d9f13SBarry Smith .ve
3877273d9f13SBarry Smith 
3878273d9f13SBarry Smith    This can be represented as a collection of submatrices as:
3879273d9f13SBarry Smith 
3880273d9f13SBarry Smith .vb
3881273d9f13SBarry Smith       A B C
3882273d9f13SBarry Smith       D E F
3883273d9f13SBarry Smith       G H I
3884273d9f13SBarry Smith .ve
3885273d9f13SBarry Smith 
3886273d9f13SBarry Smith    Where the submatrices A,B,C are owned by proc0, D,E,F are
3887273d9f13SBarry Smith    owned by proc1, G,H,I are owned by proc2.
3888273d9f13SBarry Smith 
3889273d9f13SBarry Smith    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
3890273d9f13SBarry Smith    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
3891273d9f13SBarry Smith    The 'M','N' parameters are 8,8, and have the same values on all procs.
3892273d9f13SBarry Smith 
3893273d9f13SBarry Smith    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
3894273d9f13SBarry Smith    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
3895273d9f13SBarry Smith    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
3896273d9f13SBarry Smith    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
3897273d9f13SBarry Smith    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
3898273d9f13SBarry Smith    matrix, ans [DF] as another SeqAIJ matrix.
3899273d9f13SBarry Smith 
3900273d9f13SBarry Smith    When d_nz, o_nz parameters are specified, d_nz storage elements are
3901273d9f13SBarry Smith    allocated for every row of the local diagonal submatrix, and o_nz
3902273d9f13SBarry Smith    storage locations are allocated for every row of the OFF-DIAGONAL submat.
3903273d9f13SBarry Smith    One way to choose d_nz and o_nz is to use the max nonzerors per local
3904273d9f13SBarry Smith    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
3905273d9f13SBarry Smith    In this case, the values of d_nz,o_nz are:
3906273d9f13SBarry Smith .vb
3907273d9f13SBarry Smith      proc0 : dnz = 2, o_nz = 2
3908273d9f13SBarry Smith      proc1 : dnz = 3, o_nz = 2
3909273d9f13SBarry Smith      proc2 : dnz = 1, o_nz = 4
3910273d9f13SBarry Smith .ve
3911273d9f13SBarry Smith    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
3912273d9f13SBarry Smith    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
3913273d9f13SBarry Smith    for proc3. i.e we are using 12+15+10=37 storage locations to store
3914273d9f13SBarry Smith    34 values.
3915273d9f13SBarry Smith 
3916273d9f13SBarry Smith    When d_nnz, o_nnz parameters are specified, the storage is specified
3917273d9f13SBarry Smith    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
3918273d9f13SBarry Smith    In the above case the values for d_nnz,o_nnz are:
3919273d9f13SBarry Smith .vb
3920273d9f13SBarry Smith      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
3921273d9f13SBarry Smith      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
3922273d9f13SBarry Smith      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
3923273d9f13SBarry Smith .ve
3924273d9f13SBarry Smith    Here the space allocated is sum of all the above values i.e 34, and
3925273d9f13SBarry Smith    hence pre-allocation is perfect.
3926273d9f13SBarry Smith 
3927273d9f13SBarry Smith    Level: intermediate
3928273d9f13SBarry Smith 
3929273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
3930273d9f13SBarry Smith 
393169b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateAIJ(), MatMPIAIJSetPreallocationCSR(),
3932ab978733SBarry Smith           MPIAIJ, MatGetInfo(), PetscSplitOwnership()
3933273d9f13SBarry Smith @*/
39347087cfbeSBarry Smith PetscErrorCode  MatMPIAIJSetPreallocation(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
3935273d9f13SBarry Smith {
39364ac538c5SBarry Smith   PetscErrorCode ierr;
3937273d9f13SBarry Smith 
3938273d9f13SBarry Smith   PetscFunctionBegin;
39396ba663aaSJed Brown   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
39406ba663aaSJed Brown   PetscValidType(B,1);
39414ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatMPIAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[]),(B,d_nz,d_nnz,o_nz,o_nnz));CHKERRQ(ierr);
3942273d9f13SBarry Smith   PetscFunctionReturn(0);
3943273d9f13SBarry Smith }
3944273d9f13SBarry Smith 
39454a2ae208SSatish Balay #undef __FUNCT__
39462fb0ec9aSBarry Smith #define __FUNCT__ "MatCreateMPIAIJWithArrays"
394758d36128SBarry Smith /*@
39482fb0ec9aSBarry Smith      MatCreateMPIAIJWithArrays - creates a MPI AIJ matrix using arrays that contain in standard
39492fb0ec9aSBarry Smith          CSR format the local rows.
39502fb0ec9aSBarry Smith 
39512fb0ec9aSBarry Smith    Collective on MPI_Comm
39522fb0ec9aSBarry Smith 
39532fb0ec9aSBarry Smith    Input Parameters:
39542fb0ec9aSBarry Smith +  comm - MPI communicator
39552fb0ec9aSBarry Smith .  m - number of local rows (Cannot be PETSC_DECIDE)
39562fb0ec9aSBarry Smith .  n - This value should be the same as the local size used in creating the
39572fb0ec9aSBarry Smith        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
39582fb0ec9aSBarry Smith        calculated if N is given) For square matrices n is almost always m.
39592fb0ec9aSBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
39602fb0ec9aSBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
39612fb0ec9aSBarry Smith .   i - row indices
39622fb0ec9aSBarry Smith .   j - column indices
39632fb0ec9aSBarry Smith -   a - matrix values
39642fb0ec9aSBarry Smith 
39652fb0ec9aSBarry Smith    Output Parameter:
39662fb0ec9aSBarry Smith .   mat - the matrix
396703bfb495SBarry Smith 
39682fb0ec9aSBarry Smith    Level: intermediate
39692fb0ec9aSBarry Smith 
39702fb0ec9aSBarry Smith    Notes:
39712fb0ec9aSBarry Smith        The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc;
39722fb0ec9aSBarry Smith      thus you CANNOT change the matrix entries by changing the values of a[] after you have
39738d7a6e47SBarry Smith      called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays.
39742fb0ec9aSBarry Smith 
397512251496SSatish Balay        The i and j indices are 0 based, and i indices are indices corresponding to the local j array.
397612251496SSatish Balay 
397712251496SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
397812251496SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
397912251496SSatish Balay     as shown:
398012251496SSatish Balay 
398112251496SSatish Balay         1 0 0
398212251496SSatish Balay         2 0 3     P0
398312251496SSatish Balay        -------
398412251496SSatish Balay         4 5 6     P1
398512251496SSatish Balay 
398612251496SSatish Balay      Process0 [P0]: rows_owned=[0,1]
398712251496SSatish Balay         i =  {0,1,3}  [size = nrow+1  = 2+1]
398812251496SSatish Balay         j =  {0,0,2}  [size = nz = 6]
398912251496SSatish Balay         v =  {1,2,3}  [size = nz = 6]
399012251496SSatish Balay 
399112251496SSatish Balay      Process1 [P1]: rows_owned=[2]
399212251496SSatish Balay         i =  {0,3}    [size = nrow+1  = 1+1]
399312251496SSatish Balay         j =  {0,1,2}  [size = nz = 6]
399412251496SSatish Balay         v =  {4,5,6}  [size = nz = 6]
39952fb0ec9aSBarry Smith 
39962fb0ec9aSBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
39972fb0ec9aSBarry Smith 
39982fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
399969b1f4b7SBarry Smith           MPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithSplitArrays()
40002fb0ec9aSBarry Smith @*/
40017087cfbeSBarry 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)
40022fb0ec9aSBarry Smith {
40032fb0ec9aSBarry Smith   PetscErrorCode ierr;
40042fb0ec9aSBarry Smith 
40052fb0ec9aSBarry Smith   PetscFunctionBegin;
400669b1f4b7SBarry Smith   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4007e32f2f54SBarry Smith   if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative");
40082fb0ec9aSBarry Smith   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
4009d4146a68SBarry Smith   ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr);
4010a2f3521dSMark F. Adams   /* ierr = MatSetBlockSizes(M,bs,cbs);CHKERRQ(ierr); */
40112fb0ec9aSBarry Smith   ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr);
40122fb0ec9aSBarry Smith   ierr = MatMPIAIJSetPreallocationCSR(*mat,i,j,a);CHKERRQ(ierr);
40132fb0ec9aSBarry Smith   PetscFunctionReturn(0);
40142fb0ec9aSBarry Smith }
40152fb0ec9aSBarry Smith 
40162fb0ec9aSBarry Smith #undef __FUNCT__
401769b1f4b7SBarry Smith #define __FUNCT__ "MatCreateAIJ"
4018273d9f13SBarry Smith /*@C
401969b1f4b7SBarry Smith    MatCreateAIJ - Creates a sparse parallel matrix in AIJ format
4020273d9f13SBarry Smith    (the default parallel PETSc format).  For good matrix assembly performance
4021273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameters
4022273d9f13SBarry Smith    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
4023273d9f13SBarry Smith    performance can be increased by more than a factor of 50.
4024273d9f13SBarry Smith 
4025273d9f13SBarry Smith    Collective on MPI_Comm
4026273d9f13SBarry Smith 
4027273d9f13SBarry Smith    Input Parameters:
4028273d9f13SBarry Smith +  comm - MPI communicator
4029273d9f13SBarry Smith .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
4030273d9f13SBarry Smith            This value should be the same as the local size used in creating the
4031273d9f13SBarry Smith            y vector for the matrix-vector product y = Ax.
4032273d9f13SBarry Smith .  n - This value should be the same as the local size used in creating the
4033273d9f13SBarry Smith        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
4034273d9f13SBarry Smith        calculated if N is given) For square matrices n is almost always m.
4035273d9f13SBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
4036273d9f13SBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
4037273d9f13SBarry Smith .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
4038273d9f13SBarry Smith            (same value is used for all local rows)
4039273d9f13SBarry Smith .  d_nnz - array containing the number of nonzeros in the various rows of the
4040273d9f13SBarry Smith            DIAGONAL portion of the local submatrix (possibly different for each row)
40410298fd71SBarry Smith            or NULL, if d_nz is used to specify the nonzero structure.
4042273d9f13SBarry Smith            The size of this array is equal to the number of local rows, i.e 'm'.
4043273d9f13SBarry Smith .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
4044273d9f13SBarry Smith            submatrix (same value is used for all local rows).
4045273d9f13SBarry Smith -  o_nnz - array containing the number of nonzeros in the various rows of the
4046273d9f13SBarry Smith            OFF-DIAGONAL portion of the local submatrix (possibly different for
40470298fd71SBarry Smith            each row) or NULL, if o_nz is used to specify the nonzero
4048273d9f13SBarry Smith            structure. The size of this array is equal to the number
4049273d9f13SBarry Smith            of local rows, i.e 'm'.
4050273d9f13SBarry Smith 
4051273d9f13SBarry Smith    Output Parameter:
4052273d9f13SBarry Smith .  A - the matrix
4053273d9f13SBarry Smith 
4054175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
4055ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
4056175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
4057175b88e8SBarry Smith 
4058273d9f13SBarry Smith    Notes:
405949a6f317SBarry Smith    If the *_nnz parameter is given then the *_nz parameter is ignored
406049a6f317SBarry Smith 
4061273d9f13SBarry Smith    m,n,M,N parameters specify the size of the matrix, and its partitioning across
4062273d9f13SBarry Smith    processors, while d_nz,d_nnz,o_nz,o_nnz parameters specify the approximate
4063273d9f13SBarry Smith    storage requirements for this matrix.
4064273d9f13SBarry Smith 
4065273d9f13SBarry Smith    If PETSC_DECIDE or  PETSC_DETERMINE is used for a particular argument on one
4066273d9f13SBarry Smith    processor than it must be used on all processors that share the object for
4067273d9f13SBarry Smith    that argument.
4068273d9f13SBarry Smith 
4069273d9f13SBarry Smith    The user MUST specify either the local or global matrix dimensions
4070273d9f13SBarry Smith    (possibly both).
4071273d9f13SBarry Smith 
407233a7c187SSatish Balay    The parallel matrix is partitioned across processors such that the
407333a7c187SSatish Balay    first m0 rows belong to process 0, the next m1 rows belong to
407433a7c187SSatish Balay    process 1, the next m2 rows belong to process 2 etc.. where
407533a7c187SSatish Balay    m0,m1,m2,.. are the input parameter 'm'. i.e each processor stores
407633a7c187SSatish Balay    values corresponding to [m x N] submatrix.
4077273d9f13SBarry Smith 
407833a7c187SSatish Balay    The columns are logically partitioned with the n0 columns belonging
407933a7c187SSatish Balay    to 0th partition, the next n1 columns belonging to the next
4080df3898eeSBarry Smith    partition etc.. where n0,n1,n2... are the input parameter 'n'.
408133a7c187SSatish Balay 
408233a7c187SSatish Balay    The DIAGONAL portion of the local submatrix on any given processor
408333a7c187SSatish Balay    is the submatrix corresponding to the rows and columns m,n
408433a7c187SSatish Balay    corresponding to the given processor. i.e diagonal matrix on
408533a7c187SSatish Balay    process 0 is [m0 x n0], diagonal matrix on process 1 is [m1 x n1]
408633a7c187SSatish Balay    etc. The remaining portion of the local submatrix [m x (N-n)]
408733a7c187SSatish Balay    constitute the OFF-DIAGONAL portion. The example below better
408833a7c187SSatish Balay    illustrates this concept.
408933a7c187SSatish Balay 
409033a7c187SSatish Balay    For a square global matrix we define each processor's diagonal portion
409133a7c187SSatish Balay    to be its local rows and the corresponding columns (a square submatrix);
409233a7c187SSatish Balay    each processor's off-diagonal portion encompasses the remainder of the
409333a7c187SSatish Balay    local matrix (a rectangular submatrix).
4094273d9f13SBarry Smith 
4095273d9f13SBarry Smith    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
4096273d9f13SBarry Smith 
409797d05335SKris Buschelman    When calling this routine with a single process communicator, a matrix of
409897d05335SKris Buschelman    type SEQAIJ is returned.  If a matrix of type MPIAIJ is desired for this
409997d05335SKris Buschelman    type of communicator, use the construction mechanism:
410078102f6cSMatthew Knepley      MatCreate(...,&A); MatSetType(A,MATMPIAIJ); MatSetSizes(A, m,n,M,N); MatMPIAIJSetPreallocation(A,...);
410197d05335SKris Buschelman 
4102273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible.
4103273d9f13SBarry Smith    We search for consecutive rows with the same nonzero structure, thereby
4104273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
4105273d9f13SBarry Smith 
4106273d9f13SBarry Smith    Options Database Keys:
4107923f20ffSKris Buschelman +  -mat_no_inode  - Do not use inodes
4108923f20ffSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
4109273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
4110273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
4111273d9f13SBarry Smith         the user still MUST index entries starting at 0!
4112273d9f13SBarry Smith 
4113273d9f13SBarry Smith 
4114273d9f13SBarry Smith    Example usage:
4115273d9f13SBarry Smith 
4116273d9f13SBarry Smith    Consider the following 8x8 matrix with 34 non-zero values, that is
4117273d9f13SBarry Smith    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
4118273d9f13SBarry Smith    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
4119273d9f13SBarry Smith    as follows:
4120273d9f13SBarry Smith 
4121273d9f13SBarry Smith .vb
4122273d9f13SBarry Smith             1  2  0  |  0  3  0  |  0  4
4123273d9f13SBarry Smith     Proc0   0  5  6  |  7  0  0  |  8  0
4124273d9f13SBarry Smith             9  0 10  | 11  0  0  | 12  0
4125273d9f13SBarry Smith     -------------------------------------
4126273d9f13SBarry Smith            13  0 14  | 15 16 17  |  0  0
4127273d9f13SBarry Smith     Proc1   0 18  0  | 19 20 21  |  0  0
4128273d9f13SBarry Smith             0  0  0  | 22 23  0  | 24  0
4129273d9f13SBarry Smith     -------------------------------------
4130273d9f13SBarry Smith     Proc2  25 26 27  |  0  0 28  | 29  0
4131273d9f13SBarry Smith            30  0  0  | 31 32 33  |  0 34
4132273d9f13SBarry Smith .ve
4133273d9f13SBarry Smith 
4134273d9f13SBarry Smith    This can be represented as a collection of submatrices as:
4135273d9f13SBarry Smith 
4136273d9f13SBarry Smith .vb
4137273d9f13SBarry Smith       A B C
4138273d9f13SBarry Smith       D E F
4139273d9f13SBarry Smith       G H I
4140273d9f13SBarry Smith .ve
4141273d9f13SBarry Smith 
4142273d9f13SBarry Smith    Where the submatrices A,B,C are owned by proc0, D,E,F are
4143273d9f13SBarry Smith    owned by proc1, G,H,I are owned by proc2.
4144273d9f13SBarry Smith 
4145273d9f13SBarry Smith    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
4146273d9f13SBarry Smith    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
4147273d9f13SBarry Smith    The 'M','N' parameters are 8,8, and have the same values on all procs.
4148273d9f13SBarry Smith 
4149273d9f13SBarry Smith    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
4150273d9f13SBarry Smith    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
4151273d9f13SBarry Smith    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
4152273d9f13SBarry Smith    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
4153273d9f13SBarry Smith    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
4154273d9f13SBarry Smith    matrix, ans [DF] as another SeqAIJ matrix.
4155273d9f13SBarry Smith 
4156273d9f13SBarry Smith    When d_nz, o_nz parameters are specified, d_nz storage elements are
4157273d9f13SBarry Smith    allocated for every row of the local diagonal submatrix, and o_nz
4158273d9f13SBarry Smith    storage locations are allocated for every row of the OFF-DIAGONAL submat.
4159273d9f13SBarry Smith    One way to choose d_nz and o_nz is to use the max nonzerors per local
4160273d9f13SBarry Smith    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
4161273d9f13SBarry Smith    In this case, the values of d_nz,o_nz are:
4162273d9f13SBarry Smith .vb
4163273d9f13SBarry Smith      proc0 : dnz = 2, o_nz = 2
4164273d9f13SBarry Smith      proc1 : dnz = 3, o_nz = 2
4165273d9f13SBarry Smith      proc2 : dnz = 1, o_nz = 4
4166273d9f13SBarry Smith .ve
4167273d9f13SBarry Smith    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
4168273d9f13SBarry Smith    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
4169273d9f13SBarry Smith    for proc3. i.e we are using 12+15+10=37 storage locations to store
4170273d9f13SBarry Smith    34 values.
4171273d9f13SBarry Smith 
4172273d9f13SBarry Smith    When d_nnz, o_nnz parameters are specified, the storage is specified
4173273d9f13SBarry Smith    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
4174273d9f13SBarry Smith    In the above case the values for d_nnz,o_nnz are:
4175273d9f13SBarry Smith .vb
4176273d9f13SBarry Smith      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
4177273d9f13SBarry Smith      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
4178273d9f13SBarry Smith      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
4179273d9f13SBarry Smith .ve
4180273d9f13SBarry Smith    Here the space allocated is sum of all the above values i.e 34, and
4181273d9f13SBarry Smith    hence pre-allocation is perfect.
4182273d9f13SBarry Smith 
4183273d9f13SBarry Smith    Level: intermediate
4184273d9f13SBarry Smith 
4185273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
4186273d9f13SBarry Smith 
4187ccd8e176SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
41882fb0ec9aSBarry Smith           MPIAIJ, MatCreateMPIAIJWithArrays()
4189273d9f13SBarry Smith @*/
419069b1f4b7SBarry 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)
4191273d9f13SBarry Smith {
41926849ba73SBarry Smith   PetscErrorCode ierr;
4193b1d57f15SBarry Smith   PetscMPIInt    size;
4194273d9f13SBarry Smith 
4195273d9f13SBarry Smith   PetscFunctionBegin;
4196f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
4197f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*A,m,n,M,N);CHKERRQ(ierr);
4198273d9f13SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
4199273d9f13SBarry Smith   if (size > 1) {
4200273d9f13SBarry Smith     ierr = MatSetType(*A,MATMPIAIJ);CHKERRQ(ierr);
4201273d9f13SBarry Smith     ierr = MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr);
4202273d9f13SBarry Smith   } else {
4203273d9f13SBarry Smith     ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
4204273d9f13SBarry Smith     ierr = MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);CHKERRQ(ierr);
4205273d9f13SBarry Smith   }
4206273d9f13SBarry Smith   PetscFunctionReturn(0);
4207273d9f13SBarry Smith }
4208195d93cdSBarry Smith 
42094a2ae208SSatish Balay #undef __FUNCT__
42104a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJGetSeqAIJ"
42119230625dSJed Brown PetscErrorCode  MatMPIAIJGetSeqAIJ(Mat A,Mat *Ad,Mat *Ao,const PetscInt *colmap[])
4212195d93cdSBarry Smith {
4213195d93cdSBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
4214b1d57f15SBarry Smith 
4215195d93cdSBarry Smith   PetscFunctionBegin;
421621e72a00SBarry Smith   if (Ad)     *Ad     = a->A;
421721e72a00SBarry Smith   if (Ao)     *Ao     = a->B;
421821e72a00SBarry Smith   if (colmap) *colmap = a->garray;
4219195d93cdSBarry Smith   PetscFunctionReturn(0);
4220195d93cdSBarry Smith }
4221a2243be0SBarry Smith 
4222a2243be0SBarry Smith #undef __FUNCT__
4223a2243be0SBarry Smith #define __FUNCT__ "MatSetColoring_MPIAIJ"
4224dfbe8321SBarry Smith PetscErrorCode MatSetColoring_MPIAIJ(Mat A,ISColoring coloring)
4225a2243be0SBarry Smith {
4226dfbe8321SBarry Smith   PetscErrorCode ierr;
4227b1d57f15SBarry Smith   PetscInt       i;
4228a2243be0SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
4229a2243be0SBarry Smith 
4230a2243be0SBarry Smith   PetscFunctionBegin;
42318ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
423208b6dcc0SBarry Smith     ISColoringValue *allcolors,*colors;
4233a2243be0SBarry Smith     ISColoring      ocoloring;
4234a2243be0SBarry Smith 
4235a2243be0SBarry Smith     /* set coloring for diagonal portion */
4236a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->A,coloring);CHKERRQ(ierr);
4237a2243be0SBarry Smith 
4238a2243be0SBarry Smith     /* set coloring for off-diagonal portion */
4239ce94432eSBarry Smith     ierr = ISAllGatherColors(PetscObjectComm((PetscObject)A),coloring->n,coloring->colors,NULL,&allcolors);CHKERRQ(ierr);
4240785e854fSJed Brown     ierr = PetscMalloc1((a->B->cmap->n+1),&colors);CHKERRQ(ierr);
4241d0f46423SBarry Smith     for (i=0; i<a->B->cmap->n; i++) {
4242a2243be0SBarry Smith       colors[i] = allcolors[a->garray[i]];
4243a2243be0SBarry Smith     }
4244a2243be0SBarry Smith     ierr = PetscFree(allcolors);CHKERRQ(ierr);
4245d0f46423SBarry Smith     ierr = ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
4246a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr);
42476bf464f9SBarry Smith     ierr = ISColoringDestroy(&ocoloring);CHKERRQ(ierr);
4248a2243be0SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
424908b6dcc0SBarry Smith     ISColoringValue *colors;
4250b1d57f15SBarry Smith     PetscInt        *larray;
4251a2243be0SBarry Smith     ISColoring      ocoloring;
4252a2243be0SBarry Smith 
4253a2243be0SBarry Smith     /* set coloring for diagonal portion */
4254785e854fSJed Brown     ierr = PetscMalloc1((a->A->cmap->n+1),&larray);CHKERRQ(ierr);
4255d0f46423SBarry Smith     for (i=0; i<a->A->cmap->n; i++) {
4256d0f46423SBarry Smith       larray[i] = i + A->cmap->rstart;
4257a2243be0SBarry Smith     }
42580298fd71SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,a->A->cmap->n,larray,NULL,larray);CHKERRQ(ierr);
4259785e854fSJed Brown     ierr = PetscMalloc1((a->A->cmap->n+1),&colors);CHKERRQ(ierr);
4260d0f46423SBarry Smith     for (i=0; i<a->A->cmap->n; i++) {
4261a2243be0SBarry Smith       colors[i] = coloring->colors[larray[i]];
4262a2243be0SBarry Smith     }
4263a2243be0SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
4264d0f46423SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,a->A->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
4265a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->A,ocoloring);CHKERRQ(ierr);
42666bf464f9SBarry Smith     ierr = ISColoringDestroy(&ocoloring);CHKERRQ(ierr);
4267a2243be0SBarry Smith 
4268a2243be0SBarry Smith     /* set coloring for off-diagonal portion */
4269785e854fSJed Brown     ierr = PetscMalloc1((a->B->cmap->n+1),&larray);CHKERRQ(ierr);
42700298fd71SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,a->B->cmap->n,a->garray,NULL,larray);CHKERRQ(ierr);
4271785e854fSJed Brown     ierr = PetscMalloc1((a->B->cmap->n+1),&colors);CHKERRQ(ierr);
4272d0f46423SBarry Smith     for (i=0; i<a->B->cmap->n; i++) {
4273a2243be0SBarry Smith       colors[i] = coloring->colors[larray[i]];
4274a2243be0SBarry Smith     }
4275a2243be0SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
4276d0f46423SBarry Smith     ierr = ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
4277a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr);
42786bf464f9SBarry Smith     ierr = ISColoringDestroy(&ocoloring);CHKERRQ(ierr);
42796bf464f9SBarry Smith   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support ISColoringType %d",(int)coloring->ctype);
4280a2243be0SBarry Smith   PetscFunctionReturn(0);
4281a2243be0SBarry Smith }
4282a2243be0SBarry Smith 
4283779c1a83SBarry Smith #undef __FUNCT__
4284779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdifor_MPIAIJ"
4285b1d57f15SBarry Smith PetscErrorCode MatSetValuesAdifor_MPIAIJ(Mat A,PetscInt nl,void *advalues)
4286779c1a83SBarry Smith {
4287779c1a83SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
4288dfbe8321SBarry Smith   PetscErrorCode ierr;
4289779c1a83SBarry Smith 
4290779c1a83SBarry Smith   PetscFunctionBegin;
4291779c1a83SBarry Smith   ierr = MatSetValuesAdifor_SeqAIJ(a->A,nl,advalues);CHKERRQ(ierr);
4292779c1a83SBarry Smith   ierr = MatSetValuesAdifor_SeqAIJ(a->B,nl,advalues);CHKERRQ(ierr);
4293a2243be0SBarry Smith   PetscFunctionReturn(0);
4294a2243be0SBarry Smith }
4295c5d6d63eSBarry Smith 
4296c5d6d63eSBarry Smith #undef __FUNCT__
429790431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJConcatenateSeqAIJSymbolic"
429890431a8fSHong Zhang PetscErrorCode  MatCreateMPIAIJConcatenateSeqAIJSymbolic(MPI_Comm comm,Mat inmat,PetscInt n,Mat *outmat)
42999b8102ccSHong Zhang {
43009b8102ccSHong Zhang   PetscErrorCode ierr;
4301a2f3521dSMark F. Adams   PetscInt       m,N,i,rstart,nnz,*dnz,*onz,sum,bs,cbs;
43029b8102ccSHong Zhang   PetscInt       *indx;
43039b8102ccSHong Zhang 
43049b8102ccSHong Zhang   PetscFunctionBegin;
43059b8102ccSHong Zhang   /* This routine will ONLY return MPIAIJ type matrix */
43069b8102ccSHong Zhang   ierr = MatGetSize(inmat,&m,&N);CHKERRQ(ierr);
4307a2f3521dSMark F. Adams   ierr = MatGetBlockSizes(inmat,&bs,&cbs);CHKERRQ(ierr);
43089b8102ccSHong Zhang   if (n == PETSC_DECIDE) {
43099b8102ccSHong Zhang     ierr = PetscSplitOwnership(comm,&n,&N);CHKERRQ(ierr);
43109b8102ccSHong Zhang   }
4311a22543b6SHong Zhang   /* Check sum(n) = N */
4312a95133b1SBarry Smith   ierr = MPI_Allreduce(&n,&sum,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr);
4313a22543b6SHong Zhang   if (sum != N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Sum of local columns != global columns %d",N);
4314a22543b6SHong Zhang 
43159b8102ccSHong Zhang   ierr    = MPI_Scan(&m, &rstart,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr);
43169b8102ccSHong Zhang   rstart -= m;
43179b8102ccSHong Zhang 
43189b8102ccSHong Zhang   ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr);
43199b8102ccSHong Zhang   for (i=0; i<m; i++) {
43200298fd71SBarry Smith     ierr = MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,NULL);CHKERRQ(ierr);
43219b8102ccSHong Zhang     ierr = MatPreallocateSet(i+rstart,nnz,indx,dnz,onz);CHKERRQ(ierr);
43220298fd71SBarry Smith     ierr = MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,NULL);CHKERRQ(ierr);
43239b8102ccSHong Zhang   }
43249b8102ccSHong Zhang 
43259b8102ccSHong Zhang   ierr = MatCreate(comm,outmat);CHKERRQ(ierr);
43269b8102ccSHong Zhang   ierr = MatSetSizes(*outmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
4327a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(*outmat,bs,cbs);CHKERRQ(ierr);
43289b8102ccSHong Zhang   ierr = MatSetType(*outmat,MATMPIAIJ);CHKERRQ(ierr);
43299b8102ccSHong Zhang   ierr = MatMPIAIJSetPreallocation(*outmat,0,dnz,0,onz);CHKERRQ(ierr);
43309b8102ccSHong Zhang   ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr);
43319b8102ccSHong Zhang   PetscFunctionReturn(0);
43329b8102ccSHong Zhang }
43339b8102ccSHong Zhang 
43349b8102ccSHong Zhang #undef __FUNCT__
433590431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJConcatenateSeqAIJNumeric"
433690431a8fSHong Zhang PetscErrorCode  MatCreateMPIAIJConcatenateSeqAIJNumeric(MPI_Comm comm,Mat inmat,PetscInt n,Mat outmat)
43379b8102ccSHong Zhang {
43389b8102ccSHong Zhang   PetscErrorCode ierr;
43399b8102ccSHong Zhang   PetscInt       m,N,i,rstart,nnz,Ii;
43409b8102ccSHong Zhang   PetscInt       *indx;
43419b8102ccSHong Zhang   PetscScalar    *values;
43429b8102ccSHong Zhang 
43439b8102ccSHong Zhang   PetscFunctionBegin;
43449b8102ccSHong Zhang   ierr = MatGetSize(inmat,&m,&N);CHKERRQ(ierr);
43450298fd71SBarry Smith   ierr = MatGetOwnershipRange(outmat,&rstart,NULL);CHKERRQ(ierr);
43469b8102ccSHong Zhang   for (i=0; i<m; i++) {
43479b8102ccSHong Zhang     ierr = MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
43489b8102ccSHong Zhang     Ii   = i + rstart;
43493c79b8e7SHong Zhang     ierr = MatSetValues(outmat,1,&Ii,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr);
43509b8102ccSHong Zhang     ierr = MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
43519b8102ccSHong Zhang   }
43529b8102ccSHong Zhang   ierr = MatAssemblyBegin(outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
43539b8102ccSHong Zhang   ierr = MatAssemblyEnd(outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
43549b8102ccSHong Zhang   PetscFunctionReturn(0);
43559b8102ccSHong Zhang }
43569b8102ccSHong Zhang 
43579b8102ccSHong Zhang #undef __FUNCT__
435890431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJConcatenateSeqAIJ"
4359bc08b0f1SBarry Smith /*@
436090431a8fSHong Zhang       MatCreateMPIAIJConcatenateSeqAIJ - Creates a single large PETSc matrix by concatenating sequential
436151dd7536SBarry Smith                  matrices from each processor
4362c5d6d63eSBarry Smith 
4363c5d6d63eSBarry Smith     Collective on MPI_Comm
4364c5d6d63eSBarry Smith 
4365c5d6d63eSBarry Smith    Input Parameters:
436651dd7536SBarry Smith +    comm - the communicators the parallel matrix will live on
4367d6bb3c2dSHong Zhang .    inmat - the input sequential matrices
43680e36024fSHong Zhang .    n - number of local columns (or PETSC_DECIDE)
4369d6bb3c2dSHong Zhang -    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
437051dd7536SBarry Smith 
437151dd7536SBarry Smith    Output Parameter:
437251dd7536SBarry Smith .    outmat - the parallel matrix generated
4373c5d6d63eSBarry Smith 
43747e25d530SSatish Balay     Level: advanced
43757e25d530SSatish Balay 
4376f08fae4eSHong Zhang    Notes: The number of columns of the matrix in EACH processor MUST be the same.
4377c5d6d63eSBarry Smith 
4378c5d6d63eSBarry Smith @*/
437990431a8fSHong Zhang PetscErrorCode  MatCreateMPIAIJConcatenateSeqAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
4380c5d6d63eSBarry Smith {
4381dfbe8321SBarry Smith   PetscErrorCode ierr;
4382f4703a44SHong Zhang   PetscMPIInt    size;
4383c5d6d63eSBarry Smith 
4384c5d6d63eSBarry Smith   PetscFunctionBegin;
4385f4703a44SHong Zhang   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
43869b8102ccSHong Zhang   ierr = PetscLogEventBegin(MAT_Merge,inmat,0,0,0);CHKERRQ(ierr);
4387f4703a44SHong Zhang   if (size == 1) {
4388f4703a44SHong Zhang     if (scall == MAT_INITIAL_MATRIX) {
4389f4703a44SHong Zhang       ierr = MatDuplicate(inmat,MAT_COPY_VALUES,outmat);CHKERRQ(ierr);
4390f4703a44SHong Zhang     } else {
4391f4703a44SHong Zhang       ierr = MatCopy(inmat,*outmat,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
4392f4703a44SHong Zhang     }
4393f4703a44SHong Zhang   } else {
4394d6bb3c2dSHong Zhang     if (scall == MAT_INITIAL_MATRIX) {
439590431a8fSHong Zhang       ierr = MatCreateMPIAIJConcatenateSeqAIJSymbolic(comm,inmat,n,outmat);CHKERRQ(ierr);
43960e36024fSHong Zhang     }
439790431a8fSHong Zhang     ierr = MatCreateMPIAIJConcatenateSeqAIJNumeric(comm,inmat,n,*outmat);CHKERRQ(ierr);
4398f4703a44SHong Zhang   }
43999b8102ccSHong Zhang   ierr = PetscLogEventEnd(MAT_Merge,inmat,0,0,0);CHKERRQ(ierr);
4400c5d6d63eSBarry Smith   PetscFunctionReturn(0);
4401c5d6d63eSBarry Smith }
4402c5d6d63eSBarry Smith 
4403c5d6d63eSBarry Smith #undef __FUNCT__
4404c5d6d63eSBarry Smith #define __FUNCT__ "MatFileSplit"
4405dfbe8321SBarry Smith PetscErrorCode MatFileSplit(Mat A,char *outfile)
4406c5d6d63eSBarry Smith {
4407dfbe8321SBarry Smith   PetscErrorCode    ierr;
440832dcc486SBarry Smith   PetscMPIInt       rank;
4409b1d57f15SBarry Smith   PetscInt          m,N,i,rstart,nnz;
4410de4209c5SBarry Smith   size_t            len;
4411b1d57f15SBarry Smith   const PetscInt    *indx;
4412c5d6d63eSBarry Smith   PetscViewer       out;
4413c5d6d63eSBarry Smith   char              *name;
4414c5d6d63eSBarry Smith   Mat               B;
4415b3cc6726SBarry Smith   const PetscScalar *values;
4416c5d6d63eSBarry Smith 
4417c5d6d63eSBarry Smith   PetscFunctionBegin;
4418c5d6d63eSBarry Smith   ierr = MatGetLocalSize(A,&m,0);CHKERRQ(ierr);
4419c5d6d63eSBarry Smith   ierr = MatGetSize(A,0,&N);CHKERRQ(ierr);
4420f204ca49SKris Buschelman   /* Should this be the type of the diagonal block of A? */
4421f69a0ea3SMatthew Knepley   ierr = MatCreate(PETSC_COMM_SELF,&B);CHKERRQ(ierr);
4422f69a0ea3SMatthew Knepley   ierr = MatSetSizes(B,m,N,m,N);CHKERRQ(ierr);
442333d57670SJed Brown   ierr = MatSetBlockSizesFromMats(B,A,A);CHKERRQ(ierr);
4424f204ca49SKris Buschelman   ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
44250298fd71SBarry Smith   ierr = MatSeqAIJSetPreallocation(B,0,NULL);CHKERRQ(ierr);
4426c5d6d63eSBarry Smith   ierr = MatGetOwnershipRange(A,&rstart,0);CHKERRQ(ierr);
4427c5d6d63eSBarry Smith   for (i=0; i<m; i++) {
4428c5d6d63eSBarry Smith     ierr = MatGetRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr);
4429c5d6d63eSBarry Smith     ierr = MatSetValues(B,1,&i,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr);
4430c5d6d63eSBarry Smith     ierr = MatRestoreRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr);
4431c5d6d63eSBarry Smith   }
4432c5d6d63eSBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4433c5d6d63eSBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4434c5d6d63eSBarry Smith 
4435ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A),&rank);CHKERRQ(ierr);
4436c5d6d63eSBarry Smith   ierr = PetscStrlen(outfile,&len);CHKERRQ(ierr);
4437785e854fSJed Brown   ierr = PetscMalloc1((len+5),&name);CHKERRQ(ierr);
4438c5d6d63eSBarry Smith   sprintf(name,"%s.%d",outfile,rank);
4439852598b0SBarry Smith   ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,name,FILE_MODE_APPEND,&out);CHKERRQ(ierr);
4440a2ea699eSBarry Smith   ierr = PetscFree(name);CHKERRQ(ierr);
4441c5d6d63eSBarry Smith   ierr = MatView(B,out);CHKERRQ(ierr);
44426bf464f9SBarry Smith   ierr = PetscViewerDestroy(&out);CHKERRQ(ierr);
44436bf464f9SBarry Smith   ierr = MatDestroy(&B);CHKERRQ(ierr);
4444c5d6d63eSBarry Smith   PetscFunctionReturn(0);
4445c5d6d63eSBarry Smith }
4446e5f2cdd8SHong Zhang 
444709573ac7SBarry Smith extern PetscErrorCode MatDestroy_MPIAIJ(Mat);
444851a7d1a8SHong Zhang #undef __FUNCT__
444951a7d1a8SHong Zhang #define __FUNCT__ "MatDestroy_MPIAIJ_SeqsToMPI"
44507087cfbeSBarry Smith PetscErrorCode  MatDestroy_MPIAIJ_SeqsToMPI(Mat A)
445151a7d1a8SHong Zhang {
445251a7d1a8SHong Zhang   PetscErrorCode      ierr;
4453671beff6SHong Zhang   Mat_Merge_SeqsToMPI *merge;
4454776b82aeSLisandro Dalcin   PetscContainer      container;
445551a7d1a8SHong Zhang 
445651a7d1a8SHong Zhang   PetscFunctionBegin;
4457671beff6SHong Zhang   ierr = PetscObjectQuery((PetscObject)A,"MatMergeSeqsToMPI",(PetscObject*)&container);CHKERRQ(ierr);
4458671beff6SHong Zhang   if (container) {
4459776b82aeSLisandro Dalcin     ierr = PetscContainerGetPointer(container,(void**)&merge);CHKERRQ(ierr);
446051a7d1a8SHong Zhang     ierr = PetscFree(merge->id_r);CHKERRQ(ierr);
44613e06a4e6SHong Zhang     ierr = PetscFree(merge->len_s);CHKERRQ(ierr);
44623e06a4e6SHong Zhang     ierr = PetscFree(merge->len_r);CHKERRQ(ierr);
446351a7d1a8SHong Zhang     ierr = PetscFree(merge->bi);CHKERRQ(ierr);
446451a7d1a8SHong Zhang     ierr = PetscFree(merge->bj);CHKERRQ(ierr);
4465533163c2SBarry Smith     ierr = PetscFree(merge->buf_ri[0]);CHKERRQ(ierr);
446602c68681SHong Zhang     ierr = PetscFree(merge->buf_ri);CHKERRQ(ierr);
4467533163c2SBarry Smith     ierr = PetscFree(merge->buf_rj[0]);CHKERRQ(ierr);
446802c68681SHong Zhang     ierr = PetscFree(merge->buf_rj);CHKERRQ(ierr);
446905b42c5fSBarry Smith     ierr = PetscFree(merge->coi);CHKERRQ(ierr);
447005b42c5fSBarry Smith     ierr = PetscFree(merge->coj);CHKERRQ(ierr);
447105b42c5fSBarry Smith     ierr = PetscFree(merge->owners_co);CHKERRQ(ierr);
44726bf464f9SBarry Smith     ierr = PetscLayoutDestroy(&merge->rowmap);CHKERRQ(ierr);
4473bf0cc555SLisandro Dalcin     ierr = PetscFree(merge);CHKERRQ(ierr);
4474671beff6SHong Zhang     ierr = PetscObjectCompose((PetscObject)A,"MatMergeSeqsToMPI",0);CHKERRQ(ierr);
4475671beff6SHong Zhang   }
447651a7d1a8SHong Zhang   ierr = MatDestroy_MPIAIJ(A);CHKERRQ(ierr);
447751a7d1a8SHong Zhang   PetscFunctionReturn(0);
447851a7d1a8SHong Zhang }
447951a7d1a8SHong Zhang 
4480c6db04a5SJed Brown #include <../src/mat/utils/freespace.h>
4481c6db04a5SJed Brown #include <petscbt.h>
44824ebed01fSBarry Smith 
4483e5f2cdd8SHong Zhang #undef __FUNCT__
448490431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJSumSeqAIJNumeric"
448590431a8fSHong Zhang PetscErrorCode  MatCreateMPIAIJSumSeqAIJNumeric(Mat seqmat,Mat mpimat)
448655d1abb9SHong Zhang {
448755d1abb9SHong Zhang   PetscErrorCode      ierr;
4488ce94432eSBarry Smith   MPI_Comm            comm;
448955d1abb9SHong Zhang   Mat_SeqAIJ          *a  =(Mat_SeqAIJ*)seqmat->data;
4490b1d57f15SBarry Smith   PetscMPIInt         size,rank,taga,*len_s;
4491a2ea699eSBarry Smith   PetscInt            N=mpimat->cmap->N,i,j,*owners,*ai=a->i,*aj;
4492b1d57f15SBarry Smith   PetscInt            proc,m;
4493b1d57f15SBarry Smith   PetscInt            **buf_ri,**buf_rj;
4494b1d57f15SBarry Smith   PetscInt            k,anzi,*bj_i,*bi,*bj,arow,bnzi,nextaj;
4495b1d57f15SBarry Smith   PetscInt            nrows,**buf_ri_k,**nextrow,**nextai;
449655d1abb9SHong Zhang   MPI_Request         *s_waits,*r_waits;
449755d1abb9SHong Zhang   MPI_Status          *status;
4498a77337e4SBarry Smith   MatScalar           *aa=a->a;
4499dd6ea824SBarry Smith   MatScalar           **abuf_r,*ba_i;
450055d1abb9SHong Zhang   Mat_Merge_SeqsToMPI *merge;
4501776b82aeSLisandro Dalcin   PetscContainer      container;
450255d1abb9SHong Zhang 
450355d1abb9SHong Zhang   PetscFunctionBegin;
4504bedda5b1SHong Zhang   ierr = PetscObjectGetComm((PetscObject)mpimat,&comm);CHKERRQ(ierr);
45054ebed01fSBarry Smith   ierr = PetscLogEventBegin(MAT_Seqstompinum,seqmat,0,0,0);CHKERRQ(ierr);
45063c2c1871SHong Zhang 
450755d1abb9SHong Zhang   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
450855d1abb9SHong Zhang   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
450955d1abb9SHong Zhang 
451055d1abb9SHong Zhang   ierr = PetscObjectQuery((PetscObject)mpimat,"MatMergeSeqsToMPI",(PetscObject*)&container);CHKERRQ(ierr);
4511776b82aeSLisandro Dalcin   ierr = PetscContainerGetPointer(container,(void**)&merge);CHKERRQ(ierr);
4512bf0cc555SLisandro Dalcin 
451355d1abb9SHong Zhang   bi     = merge->bi;
451455d1abb9SHong Zhang   bj     = merge->bj;
451555d1abb9SHong Zhang   buf_ri = merge->buf_ri;
451655d1abb9SHong Zhang   buf_rj = merge->buf_rj;
451755d1abb9SHong Zhang 
4518785e854fSJed Brown   ierr   = PetscMalloc1(size,&status);CHKERRQ(ierr);
45197a2fc3feSBarry Smith   owners = merge->rowmap->range;
452055d1abb9SHong Zhang   len_s  = merge->len_s;
452155d1abb9SHong Zhang 
452255d1abb9SHong Zhang   /* send and recv matrix values */
452355d1abb9SHong Zhang   /*-----------------------------*/
4524357abbc8SBarry Smith   ierr = PetscObjectGetNewTag((PetscObject)mpimat,&taga);CHKERRQ(ierr);
452555d1abb9SHong Zhang   ierr = PetscPostIrecvScalar(comm,taga,merge->nrecv,merge->id_r,merge->len_r,&abuf_r,&r_waits);CHKERRQ(ierr);
452655d1abb9SHong Zhang 
4527785e854fSJed Brown   ierr = PetscMalloc1((merge->nsend+1),&s_waits);CHKERRQ(ierr);
452855d1abb9SHong Zhang   for (proc=0,k=0; proc<size; proc++) {
452955d1abb9SHong Zhang     if (!len_s[proc]) continue;
453055d1abb9SHong Zhang     i    = owners[proc];
453155d1abb9SHong Zhang     ierr = MPI_Isend(aa+ai[i],len_s[proc],MPIU_MATSCALAR,proc,taga,comm,s_waits+k);CHKERRQ(ierr);
453255d1abb9SHong Zhang     k++;
453355d1abb9SHong Zhang   }
453455d1abb9SHong Zhang 
45350c468ba9SBarry Smith   if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,r_waits,status);CHKERRQ(ierr);}
45360c468ba9SBarry Smith   if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,s_waits,status);CHKERRQ(ierr);}
453755d1abb9SHong Zhang   ierr = PetscFree(status);CHKERRQ(ierr);
453855d1abb9SHong Zhang 
453955d1abb9SHong Zhang   ierr = PetscFree(s_waits);CHKERRQ(ierr);
454055d1abb9SHong Zhang   ierr = PetscFree(r_waits);CHKERRQ(ierr);
454155d1abb9SHong Zhang 
454255d1abb9SHong Zhang   /* insert mat values of mpimat */
454355d1abb9SHong Zhang   /*----------------------------*/
4544785e854fSJed Brown   ierr = PetscMalloc1(N,&ba_i);CHKERRQ(ierr);
4545dcca6d9dSJed Brown   ierr = PetscMalloc3(merge->nrecv,&buf_ri_k,merge->nrecv,&nextrow,merge->nrecv,&nextai);CHKERRQ(ierr);
454655d1abb9SHong Zhang 
454755d1abb9SHong Zhang   for (k=0; k<merge->nrecv; k++) {
454855d1abb9SHong Zhang     buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
454955d1abb9SHong Zhang     nrows       = *(buf_ri_k[k]);
455055d1abb9SHong Zhang     nextrow[k]  = buf_ri_k[k]+1;  /* next row number of k-th recved i-structure */
455155d1abb9SHong Zhang     nextai[k]   = buf_ri_k[k] + (nrows + 1); /* poins to the next i-structure of k-th recved i-structure  */
455255d1abb9SHong Zhang   }
455355d1abb9SHong Zhang 
455455d1abb9SHong Zhang   /* set values of ba */
45557a2fc3feSBarry Smith   m = merge->rowmap->n;
455655d1abb9SHong Zhang   for (i=0; i<m; i++) {
455755d1abb9SHong Zhang     arow = owners[rank] + i;
455855d1abb9SHong Zhang     bj_i = bj+bi[i];  /* col indices of the i-th row of mpimat */
455955d1abb9SHong Zhang     bnzi = bi[i+1] - bi[i];
4560a77337e4SBarry Smith     ierr = PetscMemzero(ba_i,bnzi*sizeof(PetscScalar));CHKERRQ(ierr);
456155d1abb9SHong Zhang 
456255d1abb9SHong Zhang     /* add local non-zero vals of this proc's seqmat into ba */
456355d1abb9SHong Zhang     anzi   = ai[arow+1] - ai[arow];
456455d1abb9SHong Zhang     aj     = a->j + ai[arow];
456555d1abb9SHong Zhang     aa     = a->a + ai[arow];
456655d1abb9SHong Zhang     nextaj = 0;
456755d1abb9SHong Zhang     for (j=0; nextaj<anzi; j++) {
456855d1abb9SHong Zhang       if (*(bj_i + j) == aj[nextaj]) { /* bcol == acol */
456955d1abb9SHong Zhang         ba_i[j] += aa[nextaj++];
457055d1abb9SHong Zhang       }
457155d1abb9SHong Zhang     }
457255d1abb9SHong Zhang 
457355d1abb9SHong Zhang     /* add received vals into ba */
457455d1abb9SHong Zhang     for (k=0; k<merge->nrecv; k++) { /* k-th received message */
457555d1abb9SHong Zhang       /* i-th row */
457655d1abb9SHong Zhang       if (i == *nextrow[k]) {
457755d1abb9SHong Zhang         anzi   = *(nextai[k]+1) - *nextai[k];
457855d1abb9SHong Zhang         aj     = buf_rj[k] + *(nextai[k]);
457955d1abb9SHong Zhang         aa     = abuf_r[k] + *(nextai[k]);
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         nextrow[k]++; nextai[k]++;
458755d1abb9SHong Zhang       }
458855d1abb9SHong Zhang     }
458955d1abb9SHong Zhang     ierr = MatSetValues(mpimat,1,&arow,bnzi,bj_i,ba_i,INSERT_VALUES);CHKERRQ(ierr);
459055d1abb9SHong Zhang   }
459155d1abb9SHong Zhang   ierr = MatAssemblyBegin(mpimat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
459255d1abb9SHong Zhang   ierr = MatAssemblyEnd(mpimat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
459355d1abb9SHong Zhang 
4594533163c2SBarry Smith   ierr = PetscFree(abuf_r[0]);CHKERRQ(ierr);
459555d1abb9SHong Zhang   ierr = PetscFree(abuf_r);CHKERRQ(ierr);
459655d1abb9SHong Zhang   ierr = PetscFree(ba_i);CHKERRQ(ierr);
45971d79065fSBarry Smith   ierr = PetscFree3(buf_ri_k,nextrow,nextai);CHKERRQ(ierr);
45984ebed01fSBarry Smith   ierr = PetscLogEventEnd(MAT_Seqstompinum,seqmat,0,0,0);CHKERRQ(ierr);
459955d1abb9SHong Zhang   PetscFunctionReturn(0);
460055d1abb9SHong Zhang }
460138f152feSBarry Smith 
46026bc0bbbfSBarry Smith extern PetscErrorCode  MatDestroy_MPIAIJ_SeqsToMPI(Mat);
46036bc0bbbfSBarry Smith 
460438f152feSBarry Smith #undef __FUNCT__
460590431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJSumSeqAIJSymbolic"
460690431a8fSHong Zhang PetscErrorCode  MatCreateMPIAIJSumSeqAIJSymbolic(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,Mat *mpimat)
4607e5f2cdd8SHong Zhang {
4608f08fae4eSHong Zhang   PetscErrorCode      ierr;
460955a3bba9SHong Zhang   Mat                 B_mpi;
4610c2234fe3SHong Zhang   Mat_SeqAIJ          *a=(Mat_SeqAIJ*)seqmat->data;
4611b1d57f15SBarry Smith   PetscMPIInt         size,rank,tagi,tagj,*len_s,*len_si,*len_ri;
4612b1d57f15SBarry Smith   PetscInt            **buf_rj,**buf_ri,**buf_ri_k;
4613d0f46423SBarry Smith   PetscInt            M=seqmat->rmap->n,N=seqmat->cmap->n,i,*owners,*ai=a->i,*aj=a->j;
4614a2f3521dSMark F. Adams   PetscInt            len,proc,*dnz,*onz,bs,cbs;
4615b1d57f15SBarry Smith   PetscInt            k,anzi,*bi,*bj,*lnk,nlnk,arow,bnzi,nspacedouble=0;
4616b1d57f15SBarry Smith   PetscInt            nrows,*buf_s,*buf_si,*buf_si_i,**nextrow,**nextai;
461755d1abb9SHong Zhang   MPI_Request         *si_waits,*sj_waits,*ri_waits,*rj_waits;
461858cb9c82SHong Zhang   MPI_Status          *status;
46190298fd71SBarry Smith   PetscFreeSpaceList  free_space=NULL,current_space=NULL;
4620be0fcf8dSHong Zhang   PetscBT             lnkbt;
462151a7d1a8SHong Zhang   Mat_Merge_SeqsToMPI *merge;
4622776b82aeSLisandro Dalcin   PetscContainer      container;
462302c68681SHong Zhang 
4624e5f2cdd8SHong Zhang   PetscFunctionBegin;
46254ebed01fSBarry Smith   ierr = PetscLogEventBegin(MAT_Seqstompisym,seqmat,0,0,0);CHKERRQ(ierr);
46263c2c1871SHong Zhang 
462738f152feSBarry Smith   /* make sure it is a PETSc comm */
46280298fd71SBarry Smith   ierr = PetscCommDuplicate(comm,&comm,NULL);CHKERRQ(ierr);
4629e5f2cdd8SHong Zhang   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
4630e5f2cdd8SHong Zhang   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
463155d1abb9SHong Zhang 
4632b00a9115SJed Brown   ierr = PetscNew(&merge);CHKERRQ(ierr);
4633785e854fSJed Brown   ierr = PetscMalloc1(size,&status);CHKERRQ(ierr);
4634e5f2cdd8SHong Zhang 
46356abd8857SHong Zhang   /* determine row ownership */
4636f08fae4eSHong Zhang   /*---------------------------------------------------------*/
463726283091SBarry Smith   ierr = PetscLayoutCreate(comm,&merge->rowmap);CHKERRQ(ierr);
463826283091SBarry Smith   ierr = PetscLayoutSetLocalSize(merge->rowmap,m);CHKERRQ(ierr);
463926283091SBarry Smith   ierr = PetscLayoutSetSize(merge->rowmap,M);CHKERRQ(ierr);
464026283091SBarry Smith   ierr = PetscLayoutSetBlockSize(merge->rowmap,1);CHKERRQ(ierr);
464126283091SBarry Smith   ierr = PetscLayoutSetUp(merge->rowmap);CHKERRQ(ierr);
4642785e854fSJed Brown   ierr = PetscMalloc1(size,&len_si);CHKERRQ(ierr);
4643785e854fSJed Brown   ierr = PetscMalloc1(size,&merge->len_s);CHKERRQ(ierr);
464455d1abb9SHong Zhang 
46457a2fc3feSBarry Smith   m      = merge->rowmap->n;
46467a2fc3feSBarry Smith   owners = merge->rowmap->range;
46476abd8857SHong Zhang 
46486abd8857SHong Zhang   /* determine the number of messages to send, their lengths */
46496abd8857SHong Zhang   /*---------------------------------------------------------*/
46503e06a4e6SHong Zhang   len_s = merge->len_s;
465151a7d1a8SHong Zhang 
46522257cef7SHong Zhang   len          = 0; /* length of buf_si[] */
4653c2234fe3SHong Zhang   merge->nsend = 0;
4654409913e3SHong Zhang   for (proc=0; proc<size; proc++) {
46552257cef7SHong Zhang     len_si[proc] = 0;
46563e06a4e6SHong Zhang     if (proc == rank) {
46576abd8857SHong Zhang       len_s[proc] = 0;
46583e06a4e6SHong Zhang     } else {
465902c68681SHong Zhang       len_si[proc] = owners[proc+1] - owners[proc] + 1;
46603e06a4e6SHong Zhang       len_s[proc]  = ai[owners[proc+1]] - ai[owners[proc]]; /* num of rows to be sent to [proc] */
46613e06a4e6SHong Zhang     }
46623e06a4e6SHong Zhang     if (len_s[proc]) {
4663c2234fe3SHong Zhang       merge->nsend++;
46642257cef7SHong Zhang       nrows = 0;
46652257cef7SHong Zhang       for (i=owners[proc]; i<owners[proc+1]; i++) {
46662257cef7SHong Zhang         if (ai[i+1] > ai[i]) nrows++;
46672257cef7SHong Zhang       }
46682257cef7SHong Zhang       len_si[proc] = 2*(nrows+1);
46692257cef7SHong Zhang       len         += len_si[proc];
4670409913e3SHong Zhang     }
467158cb9c82SHong Zhang   }
4672409913e3SHong Zhang 
46732257cef7SHong Zhang   /* determine the number and length of messages to receive for ij-structure */
46742257cef7SHong Zhang   /*-------------------------------------------------------------------------*/
46750298fd71SBarry Smith   ierr = PetscGatherNumberOfMessages(comm,NULL,len_s,&merge->nrecv);CHKERRQ(ierr);
467655d1abb9SHong Zhang   ierr = PetscGatherMessageLengths2(comm,merge->nsend,merge->nrecv,len_s,len_si,&merge->id_r,&merge->len_r,&len_ri);CHKERRQ(ierr);
4677671beff6SHong Zhang 
46783e06a4e6SHong Zhang   /* post the Irecv of j-structure */
46793e06a4e6SHong Zhang   /*-------------------------------*/
46802c72b5baSSatish Balay   ierr = PetscCommGetNewTag(comm,&tagj);CHKERRQ(ierr);
46813e06a4e6SHong Zhang   ierr = PetscPostIrecvInt(comm,tagj,merge->nrecv,merge->id_r,merge->len_r,&buf_rj,&rj_waits);CHKERRQ(ierr);
468202c68681SHong Zhang 
46833e06a4e6SHong Zhang   /* post the Isend of j-structure */
4684affca5deSHong Zhang   /*--------------------------------*/
4685dcca6d9dSJed Brown   ierr = PetscMalloc2(merge->nsend,&si_waits,merge->nsend,&sj_waits);CHKERRQ(ierr);
46863e06a4e6SHong Zhang 
46872257cef7SHong Zhang   for (proc=0, k=0; proc<size; proc++) {
4688409913e3SHong Zhang     if (!len_s[proc]) continue;
468902c68681SHong Zhang     i    = owners[proc];
4690b1d57f15SBarry Smith     ierr = MPI_Isend(aj+ai[i],len_s[proc],MPIU_INT,proc,tagj,comm,sj_waits+k);CHKERRQ(ierr);
469151a7d1a8SHong Zhang     k++;
469251a7d1a8SHong Zhang   }
469351a7d1a8SHong Zhang 
46943e06a4e6SHong Zhang   /* receives and sends of j-structure are complete */
46953e06a4e6SHong Zhang   /*------------------------------------------------*/
46960c468ba9SBarry Smith   if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,rj_waits,status);CHKERRQ(ierr);}
46970c468ba9SBarry Smith   if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,sj_waits,status);CHKERRQ(ierr);}
469802c68681SHong Zhang 
469902c68681SHong Zhang   /* send and recv i-structure */
470002c68681SHong Zhang   /*---------------------------*/
47012c72b5baSSatish Balay   ierr = PetscCommGetNewTag(comm,&tagi);CHKERRQ(ierr);
470202c68681SHong Zhang   ierr = PetscPostIrecvInt(comm,tagi,merge->nrecv,merge->id_r,len_ri,&buf_ri,&ri_waits);CHKERRQ(ierr);
470302c68681SHong Zhang 
4704785e854fSJed Brown   ierr   = PetscMalloc1((len+1),&buf_s);CHKERRQ(ierr);
47053e06a4e6SHong Zhang   buf_si = buf_s;  /* points to the beginning of k-th msg to be sent */
47062257cef7SHong Zhang   for (proc=0,k=0; proc<size; proc++) {
470702c68681SHong Zhang     if (!len_s[proc]) continue;
47083e06a4e6SHong Zhang     /* form outgoing message for i-structure:
47093e06a4e6SHong Zhang          buf_si[0]:                 nrows to be sent
47103e06a4e6SHong Zhang                [1:nrows]:           row index (global)
47113e06a4e6SHong Zhang                [nrows+1:2*nrows+1]: i-structure index
47123e06a4e6SHong Zhang     */
47133e06a4e6SHong Zhang     /*-------------------------------------------*/
47142257cef7SHong Zhang     nrows       = len_si[proc]/2 - 1;
47153e06a4e6SHong Zhang     buf_si_i    = buf_si + nrows+1;
47163e06a4e6SHong Zhang     buf_si[0]   = nrows;
47173e06a4e6SHong Zhang     buf_si_i[0] = 0;
47183e06a4e6SHong Zhang     nrows       = 0;
47193e06a4e6SHong Zhang     for (i=owners[proc]; i<owners[proc+1]; i++) {
47203e06a4e6SHong Zhang       anzi = ai[i+1] - ai[i];
47213e06a4e6SHong Zhang       if (anzi) {
47223e06a4e6SHong Zhang         buf_si_i[nrows+1] = buf_si_i[nrows] + anzi; /* i-structure */
47233e06a4e6SHong Zhang         buf_si[nrows+1]   = i-owners[proc]; /* local row index */
47243e06a4e6SHong Zhang         nrows++;
47253e06a4e6SHong Zhang       }
47263e06a4e6SHong Zhang     }
4727b1d57f15SBarry Smith     ierr = MPI_Isend(buf_si,len_si[proc],MPIU_INT,proc,tagi,comm,si_waits+k);CHKERRQ(ierr);
472802c68681SHong Zhang     k++;
47292257cef7SHong Zhang     buf_si += len_si[proc];
473002c68681SHong Zhang   }
47312257cef7SHong Zhang 
47320c468ba9SBarry Smith   if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,ri_waits,status);CHKERRQ(ierr);}
47330c468ba9SBarry Smith   if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,si_waits,status);CHKERRQ(ierr);}
473402c68681SHong Zhang 
4735ae15b995SBarry Smith   ierr = PetscInfo2(seqmat,"nsend: %D, nrecv: %D\n",merge->nsend,merge->nrecv);CHKERRQ(ierr);
47363e06a4e6SHong Zhang   for (i=0; i<merge->nrecv; i++) {
4737ae15b995SBarry 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);
47383e06a4e6SHong Zhang   }
47393e06a4e6SHong Zhang 
47403e06a4e6SHong Zhang   ierr = PetscFree(len_si);CHKERRQ(ierr);
474102c68681SHong Zhang   ierr = PetscFree(len_ri);CHKERRQ(ierr);
474202c68681SHong Zhang   ierr = PetscFree(rj_waits);CHKERRQ(ierr);
47431d79065fSBarry Smith   ierr = PetscFree2(si_waits,sj_waits);CHKERRQ(ierr);
47442257cef7SHong Zhang   ierr = PetscFree(ri_waits);CHKERRQ(ierr);
47453e06a4e6SHong Zhang   ierr = PetscFree(buf_s);CHKERRQ(ierr);
4746bcc1bcd5SHong Zhang   ierr = PetscFree(status);CHKERRQ(ierr);
474758cb9c82SHong Zhang 
4748bcc1bcd5SHong Zhang   /* compute a local seq matrix in each processor */
4749bcc1bcd5SHong Zhang   /*----------------------------------------------*/
475058cb9c82SHong Zhang   /* allocate bi array and free space for accumulating nonzero column info */
4751785e854fSJed Brown   ierr  = PetscMalloc1((m+1),&bi);CHKERRQ(ierr);
475258cb9c82SHong Zhang   bi[0] = 0;
475358cb9c82SHong Zhang 
4754be0fcf8dSHong Zhang   /* create and initialize a linked list */
4755be0fcf8dSHong Zhang   nlnk = N+1;
4756be0fcf8dSHong Zhang   ierr = PetscLLCreate(N,N,nlnk,lnk,lnkbt);CHKERRQ(ierr);
475758cb9c82SHong Zhang 
4758bcc1bcd5SHong Zhang   /* initial FreeSpace size is 2*(num of local nnz(seqmat)) */
4759bcc1bcd5SHong Zhang   len  = ai[owners[rank+1]] - ai[owners[rank]];
4760a1a86e44SBarry Smith   ierr = PetscFreeSpaceGet((PetscInt)(2*len+1),&free_space);CHKERRQ(ierr);
47612205254eSKarl Rupp 
476258cb9c82SHong Zhang   current_space = free_space;
476358cb9c82SHong Zhang 
4764bcc1bcd5SHong Zhang   /* determine symbolic info for each local row */
4765dcca6d9dSJed Brown   ierr = PetscMalloc3(merge->nrecv,&buf_ri_k,merge->nrecv,&nextrow,merge->nrecv,&nextai);CHKERRQ(ierr);
47661d79065fSBarry Smith 
47673e06a4e6SHong Zhang   for (k=0; k<merge->nrecv; k++) {
47682257cef7SHong Zhang     buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
47693e06a4e6SHong Zhang     nrows       = *buf_ri_k[k];
47703e06a4e6SHong Zhang     nextrow[k]  = buf_ri_k[k] + 1;  /* next row number of k-th recved i-structure */
47712257cef7SHong Zhang     nextai[k]   = buf_ri_k[k] + (nrows + 1); /* poins to the next i-structure of k-th recved i-structure  */
47723e06a4e6SHong Zhang   }
47732257cef7SHong Zhang 
4774bcc1bcd5SHong Zhang   ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr);
4775bcc1bcd5SHong Zhang   len  = 0;
477658cb9c82SHong Zhang   for (i=0; i<m; i++) {
477758cb9c82SHong Zhang     bnzi = 0;
477858cb9c82SHong Zhang     /* add local non-zero cols of this proc's seqmat into lnk */
477958cb9c82SHong Zhang     arow  = owners[rank] + i;
478058cb9c82SHong Zhang     anzi  = ai[arow+1] - ai[arow];
478158cb9c82SHong Zhang     aj    = a->j + ai[arow];
4782dadf0e6bSHong Zhang     ierr  = PetscLLAddSorted(anzi,aj,N,nlnk,lnk,lnkbt);CHKERRQ(ierr);
478358cb9c82SHong Zhang     bnzi += nlnk;
478458cb9c82SHong Zhang     /* add received col data into lnk */
478551a7d1a8SHong Zhang     for (k=0; k<merge->nrecv; k++) { /* k-th received message */
478655d1abb9SHong Zhang       if (i == *nextrow[k]) { /* i-th row */
47873e06a4e6SHong Zhang         anzi  = *(nextai[k]+1) - *nextai[k];
47883e06a4e6SHong Zhang         aj    = buf_rj[k] + *nextai[k];
4789dadf0e6bSHong Zhang         ierr  = PetscLLAddSorted(anzi,aj,N,nlnk,lnk,lnkbt);CHKERRQ(ierr);
47903e06a4e6SHong Zhang         bnzi += nlnk;
47913e06a4e6SHong Zhang         nextrow[k]++; nextai[k]++;
47923e06a4e6SHong Zhang       }
479358cb9c82SHong Zhang     }
4794bcc1bcd5SHong Zhang     if (len < bnzi) len = bnzi;  /* =max(bnzi) */
479558cb9c82SHong Zhang 
479658cb9c82SHong Zhang     /* if free space is not available, make more free space */
479758cb9c82SHong Zhang     if (current_space->local_remaining<bnzi) {
47984238b7adSHong Zhang       ierr = PetscFreeSpaceGet(bnzi+current_space->total_array_size,&current_space);CHKERRQ(ierr);
479958cb9c82SHong Zhang       nspacedouble++;
480058cb9c82SHong Zhang     }
480158cb9c82SHong Zhang     /* copy data into free space, then initialize lnk */
4802be0fcf8dSHong Zhang     ierr = PetscLLClean(N,N,bnzi,lnk,current_space->array,lnkbt);CHKERRQ(ierr);
4803bcc1bcd5SHong Zhang     ierr = MatPreallocateSet(i+owners[rank],bnzi,current_space->array,dnz,onz);CHKERRQ(ierr);
4804bcc1bcd5SHong Zhang 
480558cb9c82SHong Zhang     current_space->array           += bnzi;
480658cb9c82SHong Zhang     current_space->local_used      += bnzi;
480758cb9c82SHong Zhang     current_space->local_remaining -= bnzi;
480858cb9c82SHong Zhang 
480958cb9c82SHong Zhang     bi[i+1] = bi[i] + bnzi;
481058cb9c82SHong Zhang   }
4811bcc1bcd5SHong Zhang 
48121d79065fSBarry Smith   ierr = PetscFree3(buf_ri_k,nextrow,nextai);CHKERRQ(ierr);
4813bcc1bcd5SHong Zhang 
4814785e854fSJed Brown   ierr = PetscMalloc1((bi[m]+1),&bj);CHKERRQ(ierr);
4815a1a86e44SBarry Smith   ierr = PetscFreeSpaceContiguous(&free_space,bj);CHKERRQ(ierr);
4816be0fcf8dSHong Zhang   ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr);
4817409913e3SHong Zhang 
4818bcc1bcd5SHong Zhang   /* create symbolic parallel matrix B_mpi */
4819bcc1bcd5SHong Zhang   /*---------------------------------------*/
4820a2f3521dSMark F. Adams   ierr = MatGetBlockSizes(seqmat,&bs,&cbs);CHKERRQ(ierr);
4821f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,&B_mpi);CHKERRQ(ierr);
482254b84b50SHong Zhang   if (n==PETSC_DECIDE) {
4823f69a0ea3SMatthew Knepley     ierr = MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,N);CHKERRQ(ierr);
482454b84b50SHong Zhang   } else {
4825f69a0ea3SMatthew Knepley     ierr = MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
482654b84b50SHong Zhang   }
4827a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(B_mpi,bs,cbs);CHKERRQ(ierr);
4828bcc1bcd5SHong Zhang   ierr = MatSetType(B_mpi,MATMPIAIJ);CHKERRQ(ierr);
4829bcc1bcd5SHong Zhang   ierr = MatMPIAIJSetPreallocation(B_mpi,0,dnz,0,onz);CHKERRQ(ierr);
4830bcc1bcd5SHong Zhang   ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr);
48317e63b356SHong Zhang   ierr = MatSetOption(B_mpi,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr);
483258cb9c82SHong Zhang 
483390431a8fSHong Zhang   /* B_mpi is not ready for use - assembly will be done by MatCreateMPIAIJSumSeqAIJNumeric() */
48346abd8857SHong Zhang   B_mpi->assembled    = PETSC_FALSE;
4835affca5deSHong Zhang   B_mpi->ops->destroy = MatDestroy_MPIAIJ_SeqsToMPI;
4836affca5deSHong Zhang   merge->bi           = bi;
4837affca5deSHong Zhang   merge->bj           = bj;
483802c68681SHong Zhang   merge->buf_ri       = buf_ri;
483902c68681SHong Zhang   merge->buf_rj       = buf_rj;
48400298fd71SBarry Smith   merge->coi          = NULL;
48410298fd71SBarry Smith   merge->coj          = NULL;
48420298fd71SBarry Smith   merge->owners_co    = NULL;
4843affca5deSHong Zhang 
4844bf0cc555SLisandro Dalcin   ierr = PetscCommDestroy(&comm);CHKERRQ(ierr);
4845bf0cc555SLisandro Dalcin 
4846affca5deSHong Zhang   /* attach the supporting struct to B_mpi for reuse */
4847776b82aeSLisandro Dalcin   ierr    = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr);
4848776b82aeSLisandro Dalcin   ierr    = PetscContainerSetPointer(container,merge);CHKERRQ(ierr);
4849affca5deSHong Zhang   ierr    = PetscObjectCompose((PetscObject)B_mpi,"MatMergeSeqsToMPI",(PetscObject)container);CHKERRQ(ierr);
4850bf0cc555SLisandro Dalcin   ierr    = PetscContainerDestroy(&container);CHKERRQ(ierr);
4851affca5deSHong Zhang   *mpimat = B_mpi;
485238f152feSBarry Smith 
48534ebed01fSBarry Smith   ierr = PetscLogEventEnd(MAT_Seqstompisym,seqmat,0,0,0);CHKERRQ(ierr);
4854e5f2cdd8SHong Zhang   PetscFunctionReturn(0);
4855e5f2cdd8SHong Zhang }
485625616d81SHong Zhang 
485738f152feSBarry Smith #undef __FUNCT__
485890431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJSumSeqAIJ"
4859d4036a1aSHong Zhang /*@C
486090431a8fSHong Zhang       MatCreateMPIAIJSumSeqAIJ - Creates a MPIAIJ matrix by adding sequential
4861d4036a1aSHong Zhang                  matrices from each processor
4862d4036a1aSHong Zhang 
4863d4036a1aSHong Zhang     Collective on MPI_Comm
4864d4036a1aSHong Zhang 
4865d4036a1aSHong Zhang    Input Parameters:
4866d4036a1aSHong Zhang +    comm - the communicators the parallel matrix will live on
4867d4036a1aSHong Zhang .    seqmat - the input sequential matrices
4868d4036a1aSHong Zhang .    m - number of local rows (or PETSC_DECIDE)
4869d4036a1aSHong Zhang .    n - number of local columns (or PETSC_DECIDE)
4870d4036a1aSHong Zhang -    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
4871d4036a1aSHong Zhang 
4872d4036a1aSHong Zhang    Output Parameter:
4873d4036a1aSHong Zhang .    mpimat - the parallel matrix generated
4874d4036a1aSHong Zhang 
4875d4036a1aSHong Zhang     Level: advanced
4876d4036a1aSHong Zhang 
4877d4036a1aSHong Zhang    Notes:
4878d4036a1aSHong Zhang      The dimensions of the sequential matrix in each processor MUST be the same.
4879d4036a1aSHong Zhang      The input seqmat is included into the container "Mat_Merge_SeqsToMPI", and will be
4880d4036a1aSHong Zhang      destroyed when mpimat is destroyed. Call PetscObjectQuery() to access seqmat.
4881d4036a1aSHong Zhang @*/
488290431a8fSHong Zhang PetscErrorCode  MatCreateMPIAIJSumSeqAIJ(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,MatReuse scall,Mat *mpimat)
488355d1abb9SHong Zhang {
488455d1abb9SHong Zhang   PetscErrorCode ierr;
48857e63b356SHong Zhang   PetscMPIInt    size;
488655d1abb9SHong Zhang 
488755d1abb9SHong Zhang   PetscFunctionBegin;
48887e63b356SHong Zhang   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
48897e63b356SHong Zhang   if (size == 1) {
48907e63b356SHong Zhang     ierr = PetscLogEventBegin(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr);
48917e63b356SHong Zhang     if (scall == MAT_INITIAL_MATRIX) {
48927e63b356SHong Zhang       ierr = MatDuplicate(seqmat,MAT_COPY_VALUES,mpimat);CHKERRQ(ierr);
48937e63b356SHong Zhang     } else {
48947e63b356SHong Zhang       ierr = MatCopy(seqmat,*mpimat,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
48957e63b356SHong Zhang     }
48967e63b356SHong Zhang     ierr = PetscLogEventEnd(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr);
48977e63b356SHong Zhang     PetscFunctionReturn(0);
48987e63b356SHong Zhang   }
48994ebed01fSBarry Smith   ierr = PetscLogEventBegin(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr);
490055d1abb9SHong Zhang   if (scall == MAT_INITIAL_MATRIX) {
490190431a8fSHong Zhang     ierr = MatCreateMPIAIJSumSeqAIJSymbolic(comm,seqmat,m,n,mpimat);CHKERRQ(ierr);
490255d1abb9SHong Zhang   }
490390431a8fSHong Zhang   ierr = MatCreateMPIAIJSumSeqAIJNumeric(seqmat,*mpimat);CHKERRQ(ierr);
49044ebed01fSBarry Smith   ierr = PetscLogEventEnd(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr);
490555d1abb9SHong Zhang   PetscFunctionReturn(0);
490655d1abb9SHong Zhang }
49074ebed01fSBarry Smith 
490825616d81SHong Zhang #undef __FUNCT__
49094a2b5492SBarry Smith #define __FUNCT__ "MatMPIAIJGetLocalMat"
4910bc08b0f1SBarry Smith /*@
49114a2b5492SBarry Smith      MatMPIAIJGetLocalMat - Creates a SeqAIJ from a MPIAIJ matrix by taking all its local rows and putting them into a sequential vector with
49128661ff28SBarry Smith           mlocal rows and n columns. Where mlocal is the row count obtained with MatGetLocalSize() and n is the global column count obtained
49138661ff28SBarry Smith           with MatGetSize()
491425616d81SHong Zhang 
491532fba14fSHong Zhang     Not Collective
491625616d81SHong Zhang 
491725616d81SHong Zhang    Input Parameters:
491825616d81SHong Zhang +    A - the matrix
491925616d81SHong Zhang .    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
492025616d81SHong Zhang 
492125616d81SHong Zhang    Output Parameter:
492225616d81SHong Zhang .    A_loc - the local sequential matrix generated
492325616d81SHong Zhang 
492425616d81SHong Zhang     Level: developer
492525616d81SHong Zhang 
4926ba264940SBarry Smith .seealso: MatGetOwnerShipRange(), MatMPIAIJGetLocalMatCondensed()
49278661ff28SBarry Smith 
492825616d81SHong Zhang @*/
49294a2b5492SBarry Smith PetscErrorCode  MatMPIAIJGetLocalMat(Mat A,MatReuse scall,Mat *A_loc)
493025616d81SHong Zhang {
493125616d81SHong Zhang   PetscErrorCode ierr;
493201b7ae99SHong Zhang   Mat_MPIAIJ     *mpimat=(Mat_MPIAIJ*)A->data;
4933b78526a6SJose E. Roman   Mat_SeqAIJ     *mat,*a,*b;
4934b78526a6SJose E. Roman   PetscInt       *ai,*aj,*bi,*bj,*cmap=mpimat->garray;
4935b78526a6SJose E. Roman   MatScalar      *aa,*ba,*cam;
4936a77337e4SBarry Smith   PetscScalar    *ca;
4937d0f46423SBarry Smith   PetscInt       am=A->rmap->n,i,j,k,cstart=A->cmap->rstart;
49385a7d977cSHong Zhang   PetscInt       *ci,*cj,col,ncols_d,ncols_o,jo;
49398661ff28SBarry Smith   PetscBool      match;
494070a9ba44SHong Zhang   MPI_Comm       comm;
494170a9ba44SHong Zhang   PetscMPIInt    size;
494225616d81SHong Zhang 
494325616d81SHong Zhang   PetscFunctionBegin;
4944251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&match);CHKERRQ(ierr);
4945ce94432eSBarry Smith   if (!match) SETERRQ(PetscObjectComm((PetscObject)A), PETSC_ERR_SUP,"Requires MPIAIJ matrix as input");
494670a9ba44SHong Zhang   ierr = PetscObjectGetComm((PetscObject)A,&comm);CHKERRQ(ierr);
494770a9ba44SHong Zhang   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
494870a9ba44SHong Zhang   if (size == 1 && scall == MAT_REUSE_MATRIX) PetscFunctionReturn(0);
494970a9ba44SHong Zhang 
49504ebed01fSBarry Smith   ierr = PetscLogEventBegin(MAT_Getlocalmat,A,0,0,0);CHKERRQ(ierr);
4951b78526a6SJose E. Roman   a = (Mat_SeqAIJ*)(mpimat->A)->data;
4952b78526a6SJose E. Roman   b = (Mat_SeqAIJ*)(mpimat->B)->data;
4953b78526a6SJose E. Roman   ai = a->i; aj = a->j; bi = b->i; bj = b->j;
4954b78526a6SJose E. Roman   aa = a->a; ba = b->a;
495501b7ae99SHong Zhang   if (scall == MAT_INITIAL_MATRIX) {
495670a9ba44SHong Zhang     if (size == 1) {
495770a9ba44SHong Zhang       ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,am,A->cmap->N,ai,aj,aa,A_loc);CHKERRQ(ierr);
495870a9ba44SHong Zhang       PetscFunctionReturn(0);
495970a9ba44SHong Zhang     }
496070a9ba44SHong Zhang 
4961785e854fSJed Brown     ierr  = PetscMalloc1((1+am),&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     }
4966785e854fSJed Brown     ierr = PetscMalloc1((1+ci[am]),&cj);CHKERRQ(ierr);
4967785e854fSJed Brown     ierr = PetscMalloc1((1+ci[am]),&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
50320298fd71SBarry Smith -    row, col - index sets of rows and columns to extract (or 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);
5053ce94432eSBarry Smith   if (!match) SETERRQ(PetscObjectComm((PetscObject)A), 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;
5066785e854fSJed Brown     ierr  = PetscMalloc1((nzA+nzB), &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
51060298fd71SBarry Smith -    rowb, colb - index sets of rows and columns of B to extract (or 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;
51210298fd71SBarry Smith   Mat            *bseq=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;
5134785e854fSJed Brown     ierr  = PetscMalloc1((nzA+nzB), &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:
51810298fd71SBarry Smith +    startsj_s - starting point in B's sending j-arrays, saved for MAT_REUSE (or NULL)
51820298fd71SBarry Smith .    startsj_r - starting point in B's receiving j-arrays, saved for MAT_REUSE (or NULL)
51830298fd71SBarry Smith .    bufa_ptr - array for sending matrix values, saved for MAT_REUSE (or 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;
5196ce94432eSBarry Smith   MPI_Comm               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;
52020298fd71SBarry Smith   MPI_Request            *rwaits = NULL,*swaits = NULL;
520387025532SHong Zhang   MPI_Status             *sstatus,rstatus;
5204a7c7454dSHong Zhang   PetscMPIInt            jj,size;
5205e42f35eeSHong Zhang   PetscInt               *cols,sbs,rbs;
5206ba8c8a56SBarry Smith   PetscScalar            *vals;
5207429d309bSHong Zhang 
5208429d309bSHong Zhang   PetscFunctionBegin;
5209ce94432eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)A,&comm);CHKERRQ(ierr);
5210a7c7454dSHong Zhang   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
5211a7c7454dSHong Zhang   if (size == 1) PetscFunctionReturn(0);
5212a7c7454dSHong Zhang 
5213d0f46423SBarry Smith   if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend) {
5214e32f2f54SBarry 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);
5215429d309bSHong Zhang   }
52164ebed01fSBarry Smith   ierr = PetscLogEventBegin(MAT_GetBrowsOfAocols,A,B,0,0);CHKERRQ(ierr);
5217a6b2eed2SHong Zhang   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
5218a6b2eed2SHong Zhang 
5219a6b2eed2SHong Zhang   gen_to   = (VecScatter_MPI_General*)ctx->todata;
5220a6b2eed2SHong Zhang   gen_from = (VecScatter_MPI_General*)ctx->fromdata;
5221e42f35eeSHong Zhang   rvalues  = gen_from->values; /* holds the length of receiving row */
5222e42f35eeSHong Zhang   svalues  = gen_to->values;   /* holds the length of sending row */
5223a6b2eed2SHong Zhang   nrecvs   = gen_from->n;
5224a6b2eed2SHong Zhang   nsends   = gen_to->n;
5225d7ee0231SBarry Smith 
5226dcca6d9dSJed Brown   ierr    = PetscMalloc2(nrecvs,&rwaits,nsends,&swaits);CHKERRQ(ierr);
5227a6b2eed2SHong Zhang   srow    = gen_to->indices;    /* local row index to be sent */
5228a6b2eed2SHong Zhang   sstarts = gen_to->starts;
5229a6b2eed2SHong Zhang   sprocs  = gen_to->procs;
5230a6b2eed2SHong Zhang   sstatus = gen_to->sstatus;
5231e42f35eeSHong Zhang   sbs     = gen_to->bs;
5232e42f35eeSHong Zhang   rstarts = gen_from->starts;
5233e42f35eeSHong Zhang   rprocs  = gen_from->procs;
5234e42f35eeSHong Zhang   rbs     = gen_from->bs;
5235429d309bSHong Zhang 
5236b7f45c76SHong Zhang   if (!startsj_s || !bufa_ptr) scall = MAT_INITIAL_MATRIX;
5237429d309bSHong Zhang   if (scall == MAT_INITIAL_MATRIX) {
5238a6b2eed2SHong Zhang     /* i-array */
5239a6b2eed2SHong Zhang     /*---------*/
5240a6b2eed2SHong Zhang     /*  post receives */
5241a6b2eed2SHong Zhang     for (i=0; i<nrecvs; i++) {
5242e42f35eeSHong Zhang       rowlen = (PetscInt*)rvalues + rstarts[i]*rbs;
5243e42f35eeSHong Zhang       nrows  = (rstarts[i+1]-rstarts[i])*rbs; /* num of indices to be received */
524487025532SHong Zhang       ierr   = MPI_Irecv(rowlen,nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr);
5245429d309bSHong Zhang     }
5246a6b2eed2SHong Zhang 
5247a6b2eed2SHong Zhang     /* pack the outgoing message */
5248dcca6d9dSJed Brown     ierr = PetscMalloc2(nsends+1,&sstartsj,nrecvs+1,&rstartsj);CHKERRQ(ierr);
52492205254eSKarl Rupp 
52502205254eSKarl Rupp     sstartsj[0] = 0;
52512205254eSKarl Rupp     rstartsj[0] = 0;
5252a6b2eed2SHong Zhang     len         = 0; /* total length of j or a array to be sent */
5253a6b2eed2SHong Zhang     k           = 0;
5254a6b2eed2SHong Zhang     for (i=0; i<nsends; i++) {
5255e42f35eeSHong Zhang       rowlen = (PetscInt*)svalues + sstarts[i]*sbs;
5256e42f35eeSHong Zhang       nrows  = sstarts[i+1]-sstarts[i]; /* num of block rows */
525787025532SHong Zhang       for (j=0; j<nrows; j++) {
5258d0f46423SBarry Smith         row = srow[k] + B->rmap->range[rank]; /* global row idx */
5259e42f35eeSHong Zhang         for (l=0; l<sbs; l++) {
52600298fd71SBarry Smith           ierr = MatGetRow_MPIAIJ(B,row+l,&ncols,NULL,NULL);CHKERRQ(ierr); /* rowlength */
52612205254eSKarl Rupp 
5262e42f35eeSHong Zhang           rowlen[j*sbs+l] = ncols;
52632205254eSKarl Rupp 
5264e42f35eeSHong Zhang           len += ncols;
52650298fd71SBarry Smith           ierr = MatRestoreRow_MPIAIJ(B,row+l,&ncols,NULL,NULL);CHKERRQ(ierr);
5266e42f35eeSHong Zhang         }
5267a6b2eed2SHong Zhang         k++;
5268429d309bSHong Zhang       }
5269e42f35eeSHong Zhang       ierr = MPI_Isend(rowlen,nrows*sbs,MPIU_INT,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr);
52702205254eSKarl Rupp 
5271dea91ad1SHong Zhang       sstartsj[i+1] = len;  /* starting point of (i+1)-th outgoing msg in bufj and bufa */
5272429d309bSHong Zhang     }
527387025532SHong Zhang     /* recvs and sends of i-array are completed */
527487025532SHong Zhang     i = nrecvs;
527587025532SHong Zhang     while (i--) {
5276aa5bb8c0SSatish Balay       ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr);
527787025532SHong Zhang     }
52780c468ba9SBarry Smith     if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);}
5279e42f35eeSHong Zhang 
5280a6b2eed2SHong Zhang     /* allocate buffers for sending j and a arrays */
5281785e854fSJed Brown     ierr = PetscMalloc1((len+1),&bufj);CHKERRQ(ierr);
5282785e854fSJed Brown     ierr = PetscMalloc1((len+1),&bufa);CHKERRQ(ierr);
5283a6b2eed2SHong Zhang 
528487025532SHong Zhang     /* create i-array of B_oth */
5285785e854fSJed Brown     ierr = PetscMalloc1((aBn+2),&b_othi);CHKERRQ(ierr);
52862205254eSKarl Rupp 
528787025532SHong Zhang     b_othi[0] = 0;
5288a6b2eed2SHong Zhang     len       = 0; /* total length of j or a array to be received */
5289a6b2eed2SHong Zhang     k         = 0;
5290a6b2eed2SHong Zhang     for (i=0; i<nrecvs; i++) {
5291fd0ff01cSHong Zhang       rowlen = (PetscInt*)rvalues + rstarts[i]*rbs;
5292e42f35eeSHong Zhang       nrows  = rbs*(rstarts[i+1]-rstarts[i]); /* num of rows to be recieved */
529387025532SHong Zhang       for (j=0; j<nrows; j++) {
529487025532SHong Zhang         b_othi[k+1] = b_othi[k] + rowlen[j];
5295a6b2eed2SHong Zhang         len        += rowlen[j]; k++;
5296a6b2eed2SHong Zhang       }
5297dea91ad1SHong Zhang       rstartsj[i+1] = len; /* starting point of (i+1)-th incoming msg in bufj and bufa */
5298a6b2eed2SHong Zhang     }
5299a6b2eed2SHong Zhang 
530087025532SHong Zhang     /* allocate space for j and a arrrays of B_oth */
5301785e854fSJed Brown     ierr = PetscMalloc1((b_othi[aBn]+1),&b_othj);CHKERRQ(ierr);
5302785e854fSJed Brown     ierr = PetscMalloc1((b_othi[aBn]+1),&b_otha);CHKERRQ(ierr);
5303a6b2eed2SHong Zhang 
530487025532SHong Zhang     /* j-array */
530587025532SHong Zhang     /*---------*/
5306a6b2eed2SHong Zhang     /*  post receives of j-array */
5307a6b2eed2SHong Zhang     for (i=0; i<nrecvs; i++) {
530887025532SHong Zhang       nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */
530987025532SHong Zhang       ierr  = MPI_Irecv(b_othj+rstartsj[i],nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr);
5310a6b2eed2SHong Zhang     }
5311e42f35eeSHong Zhang 
5312e42f35eeSHong Zhang     /* pack the outgoing message j-array */
5313a6b2eed2SHong Zhang     k = 0;
5314a6b2eed2SHong Zhang     for (i=0; i<nsends; i++) {
5315e42f35eeSHong Zhang       nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */
5316a6b2eed2SHong Zhang       bufJ  = bufj+sstartsj[i];
531787025532SHong Zhang       for (j=0; j<nrows; j++) {
5318d0f46423SBarry Smith         row = srow[k++] + B->rmap->range[rank];  /* global row idx */
5319e42f35eeSHong Zhang         for (ll=0; ll<sbs; ll++) {
53200298fd71SBarry Smith           ierr = MatGetRow_MPIAIJ(B,row+ll,&ncols,&cols,NULL);CHKERRQ(ierr);
5321a6b2eed2SHong Zhang           for (l=0; l<ncols; l++) {
5322a6b2eed2SHong Zhang             *bufJ++ = cols[l];
532387025532SHong Zhang           }
53240298fd71SBarry Smith           ierr = MatRestoreRow_MPIAIJ(B,row+ll,&ncols,&cols,NULL);CHKERRQ(ierr);
5325e42f35eeSHong Zhang         }
532687025532SHong Zhang       }
532787025532SHong Zhang       ierr = MPI_Isend(bufj+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_INT,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr);
532887025532SHong Zhang     }
532987025532SHong Zhang 
533087025532SHong Zhang     /* recvs and sends of j-array are completed */
533187025532SHong Zhang     i = nrecvs;
533287025532SHong Zhang     while (i--) {
5333aa5bb8c0SSatish Balay       ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr);
533487025532SHong Zhang     }
53350c468ba9SBarry Smith     if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);}
533687025532SHong Zhang   } else if (scall == MAT_REUSE_MATRIX) {
5337b7f45c76SHong Zhang     sstartsj = *startsj_s;
53381d79065fSBarry Smith     rstartsj = *startsj_r;
533987025532SHong Zhang     bufa     = *bufa_ptr;
534087025532SHong Zhang     b_oth    = (Mat_SeqAIJ*)(*B_oth)->data;
534187025532SHong Zhang     b_otha   = b_oth->a;
5342f23aa3ddSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Matrix P does not posses an object container");
534387025532SHong Zhang 
534487025532SHong Zhang   /* a-array */
534587025532SHong Zhang   /*---------*/
534687025532SHong Zhang   /*  post receives of a-array */
534787025532SHong Zhang   for (i=0; i<nrecvs; i++) {
534887025532SHong Zhang     nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */
534987025532SHong Zhang     ierr  = MPI_Irecv(b_otha+rstartsj[i],nrows,MPIU_SCALAR,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr);
535087025532SHong Zhang   }
5351e42f35eeSHong Zhang 
5352e42f35eeSHong Zhang   /* pack the outgoing message a-array */
535387025532SHong Zhang   k = 0;
535487025532SHong Zhang   for (i=0; i<nsends; i++) {
5355e42f35eeSHong Zhang     nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */
535687025532SHong Zhang     bufA  = bufa+sstartsj[i];
535787025532SHong Zhang     for (j=0; j<nrows; j++) {
5358d0f46423SBarry Smith       row = srow[k++] + B->rmap->range[rank];  /* global row idx */
5359e42f35eeSHong Zhang       for (ll=0; ll<sbs; ll++) {
53600298fd71SBarry Smith         ierr = MatGetRow_MPIAIJ(B,row+ll,&ncols,NULL,&vals);CHKERRQ(ierr);
536187025532SHong Zhang         for (l=0; l<ncols; l++) {
5362a6b2eed2SHong Zhang           *bufA++ = vals[l];
5363a6b2eed2SHong Zhang         }
53640298fd71SBarry Smith         ierr = MatRestoreRow_MPIAIJ(B,row+ll,&ncols,NULL,&vals);CHKERRQ(ierr);
5365e42f35eeSHong Zhang       }
5366a6b2eed2SHong Zhang     }
536787025532SHong Zhang     ierr = MPI_Isend(bufa+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_SCALAR,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr);
5368a6b2eed2SHong Zhang   }
536987025532SHong Zhang   /* recvs and sends of a-array are completed */
537087025532SHong Zhang   i = nrecvs;
537187025532SHong Zhang   while (i--) {
5372aa5bb8c0SSatish Balay     ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr);
537387025532SHong Zhang   }
53740c468ba9SBarry Smith   if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);}
5375d7ee0231SBarry Smith   ierr = PetscFree2(rwaits,swaits);CHKERRQ(ierr);
5376a6b2eed2SHong Zhang 
537787025532SHong Zhang   if (scall == MAT_INITIAL_MATRIX) {
5378a6b2eed2SHong Zhang     /* put together the new matrix */
5379d0f46423SBarry Smith     ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,aBn,B->cmap->N,b_othi,b_othj,b_otha,B_oth);CHKERRQ(ierr);
5380a6b2eed2SHong Zhang 
5381a6b2eed2SHong Zhang     /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
5382a6b2eed2SHong Zhang     /* Since these are PETSc arrays, change flags to free them as necessary. */
538387025532SHong Zhang     b_oth          = (Mat_SeqAIJ*)(*B_oth)->data;
5384e6b907acSBarry Smith     b_oth->free_a  = PETSC_TRUE;
5385e6b907acSBarry Smith     b_oth->free_ij = PETSC_TRUE;
538687025532SHong Zhang     b_oth->nonew   = 0;
5387a6b2eed2SHong Zhang 
5388a6b2eed2SHong Zhang     ierr = PetscFree(bufj);CHKERRQ(ierr);
5389b7f45c76SHong Zhang     if (!startsj_s || !bufa_ptr) {
53901d79065fSBarry Smith       ierr = PetscFree2(sstartsj,rstartsj);CHKERRQ(ierr);
5391dea91ad1SHong Zhang       ierr = PetscFree(bufa_ptr);CHKERRQ(ierr);
5392dea91ad1SHong Zhang     } else {
5393b7f45c76SHong Zhang       *startsj_s = sstartsj;
53941d79065fSBarry Smith       *startsj_r = rstartsj;
539587025532SHong Zhang       *bufa_ptr  = bufa;
539687025532SHong Zhang     }
5397dea91ad1SHong Zhang   }
53984ebed01fSBarry Smith   ierr = PetscLogEventEnd(MAT_GetBrowsOfAocols,A,B,0,0);CHKERRQ(ierr);
5399429d309bSHong Zhang   PetscFunctionReturn(0);
5400429d309bSHong Zhang }
5401ccd8e176SBarry Smith 
540243eb5e2fSMatthew Knepley #undef __FUNCT__
540343eb5e2fSMatthew Knepley #define __FUNCT__ "MatGetCommunicationStructs"
540443eb5e2fSMatthew Knepley /*@C
540543eb5e2fSMatthew Knepley   MatGetCommunicationStructs - Provides access to the communication structures used in matrix-vector multiplication.
540643eb5e2fSMatthew Knepley 
540743eb5e2fSMatthew Knepley   Not Collective
540843eb5e2fSMatthew Knepley 
540943eb5e2fSMatthew Knepley   Input Parameters:
541043eb5e2fSMatthew Knepley . A - The matrix in mpiaij format
541143eb5e2fSMatthew Knepley 
541243eb5e2fSMatthew Knepley   Output Parameter:
541343eb5e2fSMatthew Knepley + lvec - The local vector holding off-process values from the argument to a matrix-vector product
541443eb5e2fSMatthew Knepley . colmap - A map from global column index to local index into lvec
541543eb5e2fSMatthew Knepley - multScatter - A scatter from the argument of a matrix-vector product to lvec
541643eb5e2fSMatthew Knepley 
541743eb5e2fSMatthew Knepley   Level: developer
541843eb5e2fSMatthew Knepley 
541943eb5e2fSMatthew Knepley @*/
542043eb5e2fSMatthew Knepley #if defined(PETSC_USE_CTABLE)
54217087cfbeSBarry Smith PetscErrorCode  MatGetCommunicationStructs(Mat A, Vec *lvec, PetscTable *colmap, VecScatter *multScatter)
542243eb5e2fSMatthew Knepley #else
54237087cfbeSBarry Smith PetscErrorCode  MatGetCommunicationStructs(Mat A, Vec *lvec, PetscInt *colmap[], VecScatter *multScatter)
542443eb5e2fSMatthew Knepley #endif
542543eb5e2fSMatthew Knepley {
542643eb5e2fSMatthew Knepley   Mat_MPIAIJ *a;
542743eb5e2fSMatthew Knepley 
542843eb5e2fSMatthew Knepley   PetscFunctionBegin;
54290700a824SBarry Smith   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
5430e414b56bSJed Brown   PetscValidPointer(lvec, 2);
5431e414b56bSJed Brown   PetscValidPointer(colmap, 3);
5432e414b56bSJed Brown   PetscValidPointer(multScatter, 4);
543343eb5e2fSMatthew Knepley   a = (Mat_MPIAIJ*) A->data;
543443eb5e2fSMatthew Knepley   if (lvec) *lvec = a->lvec;
543543eb5e2fSMatthew Knepley   if (colmap) *colmap = a->colmap;
543643eb5e2fSMatthew Knepley   if (multScatter) *multScatter = a->Mvctx;
543743eb5e2fSMatthew Knepley   PetscFunctionReturn(0);
543843eb5e2fSMatthew Knepley }
543943eb5e2fSMatthew Knepley 
54408cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatConvert_MPIAIJ_MPIAIJCRL(Mat,MatType,MatReuse,Mat*);
54418cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatConvert_MPIAIJ_MPIAIJPERM(Mat,MatType,MatReuse,Mat*);
54428cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatConvert_MPIAIJ_MPISBAIJ(Mat,MatType,MatReuse,Mat*);
544317667f90SBarry Smith 
5444fc4dec0aSBarry Smith #undef __FUNCT__
5445fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMultNumeric_MPIDense_MPIAIJ"
5446fc4dec0aSBarry Smith /*
5447fc4dec0aSBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
5448fc4dec0aSBarry Smith 
5449fc4dec0aSBarry Smith                n                       p                          p
5450fc4dec0aSBarry Smith         (              )       (              )         (                  )
5451fc4dec0aSBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
5452fc4dec0aSBarry Smith         (              )       (              )         (                  )
5453fc4dec0aSBarry Smith 
5454fc4dec0aSBarry Smith */
5455fc4dec0aSBarry Smith PetscErrorCode MatMatMultNumeric_MPIDense_MPIAIJ(Mat A,Mat B,Mat C)
5456fc4dec0aSBarry Smith {
5457fc4dec0aSBarry Smith   PetscErrorCode ierr;
5458fc4dec0aSBarry Smith   Mat            At,Bt,Ct;
5459fc4dec0aSBarry Smith 
5460fc4dec0aSBarry Smith   PetscFunctionBegin;
5461fc4dec0aSBarry Smith   ierr = MatTranspose(A,MAT_INITIAL_MATRIX,&At);CHKERRQ(ierr);
5462fc4dec0aSBarry Smith   ierr = MatTranspose(B,MAT_INITIAL_MATRIX,&Bt);CHKERRQ(ierr);
5463fc4dec0aSBarry Smith   ierr = MatMatMult(Bt,At,MAT_INITIAL_MATRIX,1.0,&Ct);CHKERRQ(ierr);
54646bf464f9SBarry Smith   ierr = MatDestroy(&At);CHKERRQ(ierr);
54656bf464f9SBarry Smith   ierr = MatDestroy(&Bt);CHKERRQ(ierr);
5466fc4dec0aSBarry Smith   ierr = MatTranspose(Ct,MAT_REUSE_MATRIX,&C);CHKERRQ(ierr);
54676bf464f9SBarry Smith   ierr = MatDestroy(&Ct);CHKERRQ(ierr);
5468fc4dec0aSBarry Smith   PetscFunctionReturn(0);
5469fc4dec0aSBarry Smith }
5470fc4dec0aSBarry Smith 
5471fc4dec0aSBarry Smith #undef __FUNCT__
5472fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMultSymbolic_MPIDense_MPIAIJ"
5473fc4dec0aSBarry Smith PetscErrorCode MatMatMultSymbolic_MPIDense_MPIAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
5474fc4dec0aSBarry Smith {
5475fc4dec0aSBarry Smith   PetscErrorCode ierr;
5476d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
5477fc4dec0aSBarry Smith   Mat            Cmat;
5478fc4dec0aSBarry Smith 
5479fc4dec0aSBarry Smith   PetscFunctionBegin;
5480e32f2f54SBarry 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);
5481ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),&Cmat);CHKERRQ(ierr);
5482fc4dec0aSBarry Smith   ierr = MatSetSizes(Cmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
548333d57670SJed Brown   ierr = MatSetBlockSizesFromMats(Cmat,A,B);CHKERRQ(ierr);
5484fc4dec0aSBarry Smith   ierr = MatSetType(Cmat,MATMPIDENSE);CHKERRQ(ierr);
54850298fd71SBarry Smith   ierr = MatMPIDenseSetPreallocation(Cmat,NULL);CHKERRQ(ierr);
548638556019SBarry Smith   ierr = MatAssemblyBegin(Cmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
548738556019SBarry Smith   ierr = MatAssemblyEnd(Cmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
5488f75ecaa4SHong Zhang 
5489f75ecaa4SHong Zhang   Cmat->ops->matmultnumeric = MatMatMultNumeric_MPIDense_MPIAIJ;
54902205254eSKarl Rupp 
5491fc4dec0aSBarry Smith   *C = Cmat;
5492fc4dec0aSBarry Smith   PetscFunctionReturn(0);
5493fc4dec0aSBarry Smith }
5494fc4dec0aSBarry Smith 
5495fc4dec0aSBarry Smith /* ----------------------------------------------------------------*/
5496fc4dec0aSBarry Smith #undef __FUNCT__
5497fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMult_MPIDense_MPIAIJ"
5498fc4dec0aSBarry Smith PetscErrorCode MatMatMult_MPIDense_MPIAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
5499fc4dec0aSBarry Smith {
5500fc4dec0aSBarry Smith   PetscErrorCode ierr;
5501fc4dec0aSBarry Smith 
5502fc4dec0aSBarry Smith   PetscFunctionBegin;
5503fc4dec0aSBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
55043ff4c91cSHong Zhang     ierr = PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
5505fc4dec0aSBarry Smith     ierr = MatMatMultSymbolic_MPIDense_MPIAIJ(A,B,fill,C);CHKERRQ(ierr);
55063ff4c91cSHong Zhang     ierr = PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
5507fc4dec0aSBarry Smith   }
55083ff4c91cSHong Zhang   ierr = PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
5509fc4dec0aSBarry Smith   ierr = MatMatMultNumeric_MPIDense_MPIAIJ(A,B,*C);CHKERRQ(ierr);
55103ff4c91cSHong Zhang   ierr = PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
5511fc4dec0aSBarry Smith   PetscFunctionReturn(0);
5512fc4dec0aSBarry Smith }
5513fc4dec0aSBarry Smith 
5514611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
55158cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*);
5516611f576cSBarry Smith #endif
55173bf14a46SMatthew Knepley #if defined(PETSC_HAVE_PASTIX)
55188cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_mpiaij_pastix(Mat,MatFactorType,Mat*);
55193bf14a46SMatthew Knepley #endif
5520611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU_DIST)
55218cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_mpiaij_superlu_dist(Mat,MatFactorType,Mat*);
5522611f576cSBarry Smith #endif
552317f1a0eaSHong Zhang #if defined(PETSC_HAVE_CLIQUE)
55248cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_aij_clique(Mat,MatFactorType,Mat*);
552517f1a0eaSHong Zhang #endif
55265c9eb25fSBarry Smith 
5527ccd8e176SBarry Smith /*MC
5528ccd8e176SBarry Smith    MATMPIAIJ - MATMPIAIJ = "mpiaij" - A matrix type to be used for parallel sparse matrices.
5529ccd8e176SBarry Smith 
5530ccd8e176SBarry Smith    Options Database Keys:
5531ccd8e176SBarry Smith . -mat_type mpiaij - sets the matrix type to "mpiaij" during a call to MatSetFromOptions()
5532ccd8e176SBarry Smith 
5533ccd8e176SBarry Smith   Level: beginner
5534ccd8e176SBarry Smith 
553569b1f4b7SBarry Smith .seealso: MatCreateAIJ()
5536ccd8e176SBarry Smith M*/
5537ccd8e176SBarry Smith 
5538ccd8e176SBarry Smith #undef __FUNCT__
5539ccd8e176SBarry Smith #define __FUNCT__ "MatCreate_MPIAIJ"
55408cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatCreate_MPIAIJ(Mat B)
5541ccd8e176SBarry Smith {
5542ccd8e176SBarry Smith   Mat_MPIAIJ     *b;
5543ccd8e176SBarry Smith   PetscErrorCode ierr;
5544ccd8e176SBarry Smith   PetscMPIInt    size;
5545ccd8e176SBarry Smith 
5546ccd8e176SBarry Smith   PetscFunctionBegin;
5547ce94432eSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);CHKERRQ(ierr);
55482205254eSKarl Rupp 
5549b00a9115SJed Brown   ierr          = PetscNewLog(B,&b);CHKERRQ(ierr);
5550ccd8e176SBarry Smith   B->data       = (void*)b;
5551ccd8e176SBarry Smith   ierr          = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
5552ccd8e176SBarry Smith   B->assembled  = PETSC_FALSE;
5553ccd8e176SBarry Smith   B->insertmode = NOT_SET_VALUES;
5554ccd8e176SBarry Smith   b->size       = size;
55552205254eSKarl Rupp 
5556ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)B),&b->rank);CHKERRQ(ierr);
5557ccd8e176SBarry Smith 
5558ccd8e176SBarry Smith   /* build cache for off array entries formed */
5559ce94432eSBarry Smith   ierr = MatStashCreate_Private(PetscObjectComm((PetscObject)B),1,&B->stash);CHKERRQ(ierr);
55602205254eSKarl Rupp 
5561ccd8e176SBarry Smith   b->donotstash  = PETSC_FALSE;
5562ccd8e176SBarry Smith   b->colmap      = 0;
5563ccd8e176SBarry Smith   b->garray      = 0;
5564ccd8e176SBarry Smith   b->roworiented = PETSC_TRUE;
5565ccd8e176SBarry Smith 
5566ccd8e176SBarry Smith   /* stuff used for matrix vector multiply */
55670298fd71SBarry Smith   b->lvec  = NULL;
55680298fd71SBarry Smith   b->Mvctx = NULL;
5569ccd8e176SBarry Smith 
5570ccd8e176SBarry Smith   /* stuff for MatGetRow() */
5571ccd8e176SBarry Smith   b->rowindices   = 0;
5572ccd8e176SBarry Smith   b->rowvalues    = 0;
5573ccd8e176SBarry Smith   b->getrowactive = PETSC_FALSE;
5574ccd8e176SBarry Smith 
5575bbf3fe20SPaul Mullowney   /* flexible pointer used in CUSP/CUSPARSE classes */
55760298fd71SBarry Smith   b->spptr = NULL;
5577f60c3dc2SHong Zhang 
5578611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
5579bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_mumps_C",MatGetFactor_aij_mumps);CHKERRQ(ierr);
5580611f576cSBarry Smith #endif
55813bf14a46SMatthew Knepley #if defined(PETSC_HAVE_PASTIX)
5582bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_pastix_C",MatGetFactor_mpiaij_pastix);CHKERRQ(ierr);
55833bf14a46SMatthew Knepley #endif
5584611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU_DIST)
5585bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_superlu_dist_C",MatGetFactor_mpiaij_superlu_dist);CHKERRQ(ierr);
5586611f576cSBarry Smith #endif
558717f1a0eaSHong Zhang #if defined(PETSC_HAVE_CLIQUE)
5588bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_clique_C",MatGetFactor_aij_clique);CHKERRQ(ierr);
558917f1a0eaSHong Zhang #endif
5590bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_MPIAIJ);CHKERRQ(ierr);
5591bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_MPIAIJ);CHKERRQ(ierr);
5592bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetDiagonalBlock_C",MatGetDiagonalBlock_MPIAIJ);CHKERRQ(ierr);
5593bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_MPIAIJ);CHKERRQ(ierr);
5594bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMPIAIJSetPreallocation_C",MatMPIAIJSetPreallocation_MPIAIJ);CHKERRQ(ierr);
5595bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMPIAIJSetPreallocationCSR_C",MatMPIAIJSetPreallocationCSR_MPIAIJ);CHKERRQ(ierr);
5596bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatDiagonalScaleLocal_C",MatDiagonalScaleLocal_MPIAIJ);CHKERRQ(ierr);
5597bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpiaijperm_C",MatConvert_MPIAIJ_MPIAIJPERM);CHKERRQ(ierr);
5598bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpiaijcrl_C",MatConvert_MPIAIJ_MPIAIJCRL);CHKERRQ(ierr);
5599bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpisbaij_C",MatConvert_MPIAIJ_MPISBAIJ);CHKERRQ(ierr);
5600bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_mpidense_mpiaij_C",MatMatMult_MPIDense_MPIAIJ);CHKERRQ(ierr);
5601bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_mpidense_mpiaij_C",MatMatMultSymbolic_MPIDense_MPIAIJ);CHKERRQ(ierr);
5602bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_mpidense_mpiaij_C",MatMatMultNumeric_MPIDense_MPIAIJ);CHKERRQ(ierr);
560317667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATMPIAIJ);CHKERRQ(ierr);
5604ccd8e176SBarry Smith   PetscFunctionReturn(0);
5605ccd8e176SBarry Smith }
560681824310SBarry Smith 
560703bfb495SBarry Smith #undef __FUNCT__
560803bfb495SBarry Smith #define __FUNCT__ "MatCreateMPIAIJWithSplitArrays"
5609e72c4023SBarry Smith /*@C
561003bfb495SBarry Smith      MatCreateMPIAIJWithSplitArrays - creates a MPI AIJ matrix using arrays that contain the "diagonal"
561103bfb495SBarry Smith          and "off-diagonal" part of the matrix in CSR format.
561203bfb495SBarry Smith 
561303bfb495SBarry Smith    Collective on MPI_Comm
561403bfb495SBarry Smith 
561503bfb495SBarry Smith    Input Parameters:
561603bfb495SBarry Smith +  comm - MPI communicator
561703bfb495SBarry Smith .  m - number of local rows (Cannot be PETSC_DECIDE)
561803bfb495SBarry Smith .  n - This value should be the same as the local size used in creating the
561903bfb495SBarry Smith        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
562003bfb495SBarry Smith        calculated if N is given) For square matrices n is almost always m.
562103bfb495SBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
562203bfb495SBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
562303bfb495SBarry Smith .   i - row indices for "diagonal" portion of matrix
562403bfb495SBarry Smith .   j - column indices
562503bfb495SBarry Smith .   a - matrix values
562603bfb495SBarry Smith .   oi - row indices for "off-diagonal" portion of matrix
562703bfb495SBarry Smith .   oj - column indices
562803bfb495SBarry Smith -   oa - matrix values
562903bfb495SBarry Smith 
563003bfb495SBarry Smith    Output Parameter:
563103bfb495SBarry Smith .   mat - the matrix
563203bfb495SBarry Smith 
563303bfb495SBarry Smith    Level: advanced
563403bfb495SBarry Smith 
563503bfb495SBarry Smith    Notes:
5636292fb18eSBarry Smith        The i, j, and a arrays ARE NOT copied by this routine into the internal format used by PETSc. The user
5637292fb18eSBarry Smith        must free the arrays once the matrix has been destroyed and not before.
563803bfb495SBarry Smith 
563903bfb495SBarry Smith        The i and j indices are 0 based
564003bfb495SBarry Smith 
564169b1f4b7SBarry Smith        See MatCreateAIJ() for the definition of "diagonal" and "off-diagonal" portion of the matrix
564203bfb495SBarry Smith 
56437b55108eSBarry Smith        This sets local rows and cannot be used to set off-processor values.
56447b55108eSBarry Smith 
5645dca341c0SJed Brown        Use of this routine is discouraged because it is inflexible and cumbersome to use. It is extremely rare that a
5646dca341c0SJed Brown        legacy application natively assembles into exactly this split format. The code to do so is nontrivial and does
5647dca341c0SJed Brown        not easily support in-place reassembly. It is recommended to use MatSetValues() (or a variant thereof) because
5648dca341c0SJed Brown        the resulting assembly is easier to implement, will work with any matrix format, and the user does not have to
5649dca341c0SJed Brown        keep track of the underlying array. Use MatSetOption(A,MAT_IGNORE_OFF_PROC_ENTRIES,PETSC_TRUE) to disable all
5650dca341c0SJed Brown        communication if it is known that only local entries will be set.
565103bfb495SBarry Smith 
565203bfb495SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
565303bfb495SBarry Smith 
565403bfb495SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
565569b1f4b7SBarry Smith           MPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithArrays()
5656e72c4023SBarry Smith C@*/
56572205254eSKarl Rupp PetscErrorCode  MatCreateMPIAIJWithSplitArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,PetscInt i[],PetscInt j[],PetscScalar a[],PetscInt oi[], PetscInt oj[],PetscScalar oa[],Mat *mat)
565803bfb495SBarry Smith {
565903bfb495SBarry Smith   PetscErrorCode ierr;
566003bfb495SBarry Smith   Mat_MPIAIJ     *maij;
566103bfb495SBarry Smith 
566203bfb495SBarry Smith   PetscFunctionBegin;
5663e32f2f54SBarry Smith   if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative");
5664ea345e14SBarry Smith   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
5665ea345e14SBarry Smith   if (oi[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"oi (row indices) must start with 0");
566603bfb495SBarry Smith   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
566703bfb495SBarry Smith   ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr);
566803bfb495SBarry Smith   ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr);
566903bfb495SBarry Smith   maij = (Mat_MPIAIJ*) (*mat)->data;
56702205254eSKarl Rupp 
56718d7a6e47SBarry Smith   (*mat)->preallocated = PETSC_TRUE;
567203bfb495SBarry Smith 
567326283091SBarry Smith   ierr = PetscLayoutSetUp((*mat)->rmap);CHKERRQ(ierr);
567426283091SBarry Smith   ierr = PetscLayoutSetUp((*mat)->cmap);CHKERRQ(ierr);
567503bfb495SBarry Smith 
567603bfb495SBarry Smith   ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,n,i,j,a,&maij->A);CHKERRQ(ierr);
5677d0f46423SBarry Smith   ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,(*mat)->cmap->N,oi,oj,oa,&maij->B);CHKERRQ(ierr);
567803bfb495SBarry Smith 
56798d7a6e47SBarry Smith   ierr = MatAssemblyBegin(maij->A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
56808d7a6e47SBarry Smith   ierr = MatAssemblyEnd(maij->A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
56818d7a6e47SBarry Smith   ierr = MatAssemblyBegin(maij->B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
56828d7a6e47SBarry Smith   ierr = MatAssemblyEnd(maij->B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
56838d7a6e47SBarry Smith 
568403bfb495SBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
568503bfb495SBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
5686dca341c0SJed Brown   ierr = MatSetOption(*mat,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
568703bfb495SBarry Smith   PetscFunctionReturn(0);
568803bfb495SBarry Smith }
568903bfb495SBarry Smith 
569081824310SBarry Smith /*
569181824310SBarry Smith     Special version for direct calls from Fortran
569281824310SBarry Smith */
5693b45d2f2cSJed Brown #include <petsc-private/fortranimpl.h>
56947087cfbeSBarry Smith 
569581824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
569681824310SBarry Smith #define matsetvaluesmpiaij_ MATSETVALUESMPIAIJ
569781824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
569881824310SBarry Smith #define matsetvaluesmpiaij_ matsetvaluesmpiaij
569981824310SBarry Smith #endif
570081824310SBarry Smith 
570181824310SBarry Smith /* Change these macros so can be used in void function */
570281824310SBarry Smith #undef CHKERRQ
5703e32f2f54SBarry Smith #define CHKERRQ(ierr) CHKERRABORT(PETSC_COMM_WORLD,ierr)
570481824310SBarry Smith #undef SETERRQ2
5705e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
57064994cf47SJed Brown #undef SETERRQ3
57074994cf47SJed Brown #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)
570881824310SBarry Smith #undef SETERRQ
5709e32f2f54SBarry Smith #define SETERRQ(c,ierr,b) CHKERRABORT(c,ierr)
571081824310SBarry Smith 
571181824310SBarry Smith #undef __FUNCT__
571281824310SBarry Smith #define __FUNCT__ "matsetvaluesmpiaij_"
57138cc058d9SJed Brown PETSC_EXTERN void PETSC_STDCALL matsetvaluesmpiaij_(Mat *mmat,PetscInt *mm,const PetscInt im[],PetscInt *mn,const PetscInt in[],const PetscScalar v[],InsertMode *maddv,PetscErrorCode *_ierr)
571481824310SBarry Smith {
571581824310SBarry Smith   Mat            mat  = *mmat;
571681824310SBarry Smith   PetscInt       m    = *mm, n = *mn;
571781824310SBarry Smith   InsertMode     addv = *maddv;
571881824310SBarry Smith   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
571981824310SBarry Smith   PetscScalar    value;
572081824310SBarry Smith   PetscErrorCode ierr;
5721899cda47SBarry Smith 
57224994cf47SJed Brown   MatCheckPreallocated(mat,1);
57232205254eSKarl Rupp   if (mat->insertmode == NOT_SET_VALUES) mat->insertmode = addv;
57242205254eSKarl Rupp 
572581824310SBarry Smith #if defined(PETSC_USE_DEBUG)
5726f23aa3ddSBarry Smith   else if (mat->insertmode != addv) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
572781824310SBarry Smith #endif
572881824310SBarry Smith   {
5729d0f46423SBarry Smith     PetscInt  i,j,rstart  = mat->rmap->rstart,rend = mat->rmap->rend;
5730d0f46423SBarry Smith     PetscInt  cstart      = mat->cmap->rstart,cend = mat->cmap->rend,row,col;
5731ace3abfcSBarry Smith     PetscBool roworiented = aij->roworiented;
573281824310SBarry Smith 
573381824310SBarry Smith     /* Some Variables required in the macro */
573481824310SBarry Smith     Mat        A                 = aij->A;
573581824310SBarry Smith     Mat_SeqAIJ *a                = (Mat_SeqAIJ*)A->data;
573681824310SBarry Smith     PetscInt   *aimax            = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j;
5737dd6ea824SBarry Smith     MatScalar  *aa               = a->a;
5738ace3abfcSBarry Smith     PetscBool  ignorezeroentries = (((a->ignorezeroentries)&&(addv==ADD_VALUES)) ? PETSC_TRUE : PETSC_FALSE);
573981824310SBarry Smith     Mat        B                 = aij->B;
574081824310SBarry Smith     Mat_SeqAIJ *b                = (Mat_SeqAIJ*)B->data;
5741d0f46423SBarry Smith     PetscInt   *bimax            = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap->n,am = aij->A->rmap->n;
5742dd6ea824SBarry Smith     MatScalar  *ba               = b->a;
574381824310SBarry Smith 
574481824310SBarry Smith     PetscInt  *rp1,*rp2,ii,nrow1,nrow2,_i,rmax1,rmax2,N,low1,high1,low2,high2,t,lastcol1,lastcol2;
574581824310SBarry Smith     PetscInt  nonew = a->nonew;
5746dd6ea824SBarry Smith     MatScalar *ap1,*ap2;
574781824310SBarry Smith 
574881824310SBarry Smith     PetscFunctionBegin;
574981824310SBarry Smith     for (i=0; i<m; i++) {
575081824310SBarry Smith       if (im[i] < 0) continue;
575181824310SBarry Smith #if defined(PETSC_USE_DEBUG)
5752e32f2f54SBarry 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);
575381824310SBarry Smith #endif
575481824310SBarry Smith       if (im[i] >= rstart && im[i] < rend) {
575581824310SBarry Smith         row      = im[i] - rstart;
575681824310SBarry Smith         lastcol1 = -1;
575781824310SBarry Smith         rp1      = aj + ai[row];
575881824310SBarry Smith         ap1      = aa + ai[row];
575981824310SBarry Smith         rmax1    = aimax[row];
576081824310SBarry Smith         nrow1    = ailen[row];
576181824310SBarry Smith         low1     = 0;
576281824310SBarry Smith         high1    = nrow1;
576381824310SBarry Smith         lastcol2 = -1;
576481824310SBarry Smith         rp2      = bj + bi[row];
576581824310SBarry Smith         ap2      = ba + bi[row];
576681824310SBarry Smith         rmax2    = bimax[row];
576781824310SBarry Smith         nrow2    = bilen[row];
576881824310SBarry Smith         low2     = 0;
576981824310SBarry Smith         high2    = nrow2;
577081824310SBarry Smith 
577181824310SBarry Smith         for (j=0; j<n; j++) {
57722205254eSKarl Rupp           if (roworiented) value = v[i*n+j];
57732205254eSKarl Rupp           else value = v[i+j*m];
577481824310SBarry Smith           if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES)) continue;
577581824310SBarry Smith           if (in[j] >= cstart && in[j] < cend) {
577681824310SBarry Smith             col = in[j] - cstart;
577781824310SBarry Smith             MatSetValues_SeqAIJ_A_Private(row,col,value,addv);
577881824310SBarry Smith           } else if (in[j] < 0) continue;
577981824310SBarry Smith #if defined(PETSC_USE_DEBUG)
5780cb9801acSJed 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);
578181824310SBarry Smith #endif
578281824310SBarry Smith           else {
578381824310SBarry Smith             if (mat->was_assembled) {
578481824310SBarry Smith               if (!aij->colmap) {
5785ab9863d7SBarry Smith                 ierr = MatCreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
578681824310SBarry Smith               }
578781824310SBarry Smith #if defined(PETSC_USE_CTABLE)
578881824310SBarry Smith               ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr);
578981824310SBarry Smith               col--;
579081824310SBarry Smith #else
579181824310SBarry Smith               col = aij->colmap[in[j]] - 1;
579281824310SBarry Smith #endif
579381824310SBarry Smith               if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
5794ab9863d7SBarry Smith                 ierr = MatDisAssemble_MPIAIJ(mat);CHKERRQ(ierr);
579581824310SBarry Smith                 col  =  in[j];
579681824310SBarry Smith                 /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
579781824310SBarry Smith                 B     = aij->B;
579881824310SBarry Smith                 b     = (Mat_SeqAIJ*)B->data;
579981824310SBarry Smith                 bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j;
580081824310SBarry Smith                 rp2   = bj + bi[row];
580181824310SBarry Smith                 ap2   = ba + bi[row];
580281824310SBarry Smith                 rmax2 = bimax[row];
580381824310SBarry Smith                 nrow2 = bilen[row];
580481824310SBarry Smith                 low2  = 0;
580581824310SBarry Smith                 high2 = nrow2;
5806d0f46423SBarry Smith                 bm    = aij->B->rmap->n;
580781824310SBarry Smith                 ba    = b->a;
580881824310SBarry Smith               }
580981824310SBarry Smith             } else col = in[j];
581081824310SBarry Smith             MatSetValues_SeqAIJ_B_Private(row,col,value,addv);
581181824310SBarry Smith           }
581281824310SBarry Smith         }
58132205254eSKarl Rupp       } else if (!aij->donotstash) {
581481824310SBarry Smith         if (roworiented) {
5815ace3abfcSBarry Smith           ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr);
581681824310SBarry Smith         } else {
5817ace3abfcSBarry Smith           ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr);
581881824310SBarry Smith         }
581981824310SBarry Smith       }
582081824310SBarry Smith     }
58212205254eSKarl Rupp   }
582281824310SBarry Smith   PetscFunctionReturnVoid();
582381824310SBarry Smith }
582403bfb495SBarry Smith 
5825