xref: /petsc/src/mat/impls/aij/mpi/mpiaij.c (revision 5d7652ec95e2c90ac2468c49615d72a98e547fc4)
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__
10899e65526SBarry Smith #define __FUNCT__ "MatDiagonalSet_MPIAIJ"
10999e65526SBarry Smith PetscErrorCode  MatDiagonalSet_MPIAIJ(Mat Y,Vec D,InsertMode is)
11099e65526SBarry Smith {
11199e65526SBarry Smith   PetscErrorCode    ierr;
11299e65526SBarry Smith   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*) Y->data;
11399e65526SBarry Smith 
11499e65526SBarry Smith   PetscFunctionBegin;
11599e65526SBarry Smith   if (Y->assembled && Y->rmap->rstart == Y->cmap->rstart && Y->rmap->rend == Y->cmap->rend) {
11699e65526SBarry Smith     ierr = MatDiagonalSet(aij->A,D,is);CHKERRQ(ierr);
11799e65526SBarry Smith   } else {
11899e65526SBarry Smith     ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr);
11999e65526SBarry Smith   }
12099e65526SBarry Smith   PetscFunctionReturn(0);
12199e65526SBarry Smith }
12299e65526SBarry Smith 
12399e65526SBarry Smith 
12499e65526SBarry Smith #undef __FUNCT__
125f1f41ecbSJed Brown #define __FUNCT__ "MatFindZeroDiagonals_MPIAIJ"
126f1f41ecbSJed Brown PetscErrorCode MatFindZeroDiagonals_MPIAIJ(Mat M,IS *zrows)
127f1f41ecbSJed Brown {
128f1f41ecbSJed Brown   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)M->data;
129f1f41ecbSJed Brown   PetscErrorCode ierr;
130f1f41ecbSJed Brown   PetscInt       i,rstart,nrows,*rows;
131f1f41ecbSJed Brown 
132f1f41ecbSJed Brown   PetscFunctionBegin;
1330298fd71SBarry Smith   *zrows = NULL;
134f1f41ecbSJed Brown   ierr   = MatFindZeroDiagonals_SeqAIJ_Private(aij->A,&nrows,&rows);CHKERRQ(ierr);
1350298fd71SBarry Smith   ierr   = MatGetOwnershipRange(M,&rstart,NULL);CHKERRQ(ierr);
136f1f41ecbSJed Brown   for (i=0; i<nrows; i++) rows[i] += rstart;
137ce94432eSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject)M),nrows,rows,PETSC_OWN_POINTER,zrows);CHKERRQ(ierr);
138f1f41ecbSJed Brown   PetscFunctionReturn(0);
139f1f41ecbSJed Brown }
140f1f41ecbSJed Brown 
141f1f41ecbSJed Brown #undef __FUNCT__
1420716a85fSBarry Smith #define __FUNCT__ "MatGetColumnNorms_MPIAIJ"
1430716a85fSBarry Smith PetscErrorCode MatGetColumnNorms_MPIAIJ(Mat A,NormType type,PetscReal *norms)
1440716a85fSBarry Smith {
1450716a85fSBarry Smith   PetscErrorCode ierr;
1460716a85fSBarry Smith   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)A->data;
1470716a85fSBarry Smith   PetscInt       i,n,*garray = aij->garray;
1480716a85fSBarry Smith   Mat_SeqAIJ     *a_aij = (Mat_SeqAIJ*) aij->A->data;
1490716a85fSBarry Smith   Mat_SeqAIJ     *b_aij = (Mat_SeqAIJ*) aij->B->data;
1500716a85fSBarry Smith   PetscReal      *work;
1510716a85fSBarry Smith 
1520716a85fSBarry Smith   PetscFunctionBegin;
1530298fd71SBarry Smith   ierr = MatGetSize(A,NULL,&n);CHKERRQ(ierr);
1541795a4d1SJed Brown   ierr = PetscCalloc1(n,&work);CHKERRQ(ierr);
1550716a85fSBarry Smith   if (type == NORM_2) {
1560716a85fSBarry Smith     for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) {
1570716a85fSBarry Smith       work[A->cmap->rstart + a_aij->j[i]] += PetscAbsScalar(a_aij->a[i]*a_aij->a[i]);
1580716a85fSBarry Smith     }
1590716a85fSBarry Smith     for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) {
1600716a85fSBarry Smith       work[garray[b_aij->j[i]]] += PetscAbsScalar(b_aij->a[i]*b_aij->a[i]);
1610716a85fSBarry Smith     }
1620716a85fSBarry Smith   } else if (type == NORM_1) {
1630716a85fSBarry Smith     for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) {
1640716a85fSBarry Smith       work[A->cmap->rstart + a_aij->j[i]] += PetscAbsScalar(a_aij->a[i]);
1650716a85fSBarry Smith     }
1660716a85fSBarry Smith     for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) {
1670716a85fSBarry Smith       work[garray[b_aij->j[i]]] += PetscAbsScalar(b_aij->a[i]);
1680716a85fSBarry Smith     }
1690716a85fSBarry Smith   } else if (type == NORM_INFINITY) {
1700716a85fSBarry Smith     for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) {
1710716a85fSBarry Smith       work[A->cmap->rstart + a_aij->j[i]] = PetscMax(PetscAbsScalar(a_aij->a[i]), work[A->cmap->rstart + a_aij->j[i]]);
1720716a85fSBarry Smith     }
1730716a85fSBarry Smith     for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) {
1740716a85fSBarry Smith       work[garray[b_aij->j[i]]] = PetscMax(PetscAbsScalar(b_aij->a[i]),work[garray[b_aij->j[i]]]);
1750716a85fSBarry Smith     }
1760716a85fSBarry Smith 
177ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONG,"Unknown NormType");
1780716a85fSBarry Smith   if (type == NORM_INFINITY) {
1797ec4195eSRémi Lacroix     ierr = MPI_Allreduce(work,norms,n,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)A));CHKERRQ(ierr);
1800716a85fSBarry Smith   } else {
1817ec4195eSRémi Lacroix     ierr = MPI_Allreduce(work,norms,n,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)A));CHKERRQ(ierr);
1820716a85fSBarry Smith   }
1830716a85fSBarry Smith   ierr = PetscFree(work);CHKERRQ(ierr);
1840716a85fSBarry Smith   if (type == NORM_2) {
1858f1a2a5eSBarry Smith     for (i=0; i<n; i++) norms[i] = PetscSqrtReal(norms[i]);
1860716a85fSBarry Smith   }
1870716a85fSBarry Smith   PetscFunctionReturn(0);
1880716a85fSBarry Smith }
1890716a85fSBarry Smith 
1900716a85fSBarry Smith #undef __FUNCT__
191dd6ea824SBarry Smith #define __FUNCT__ "MatDistribute_MPIAIJ"
192dd6ea824SBarry Smith /*
193dd6ea824SBarry Smith     Distributes a SeqAIJ matrix across a set of processes. Code stolen from
194dd6ea824SBarry Smith     MatLoad_MPIAIJ(). Horrible lack of reuse. Should be a routine for each matrix type.
195dd6ea824SBarry Smith 
196dd6ea824SBarry Smith     Only for square matrices
197b30237c6SBarry Smith 
198b30237c6SBarry Smith     Used by a preconditioner, hence PETSC_EXTERN
199dd6ea824SBarry Smith */
2005a576424SJed Brown PETSC_EXTERN PetscErrorCode MatDistribute_MPIAIJ(MPI_Comm comm,Mat gmat,PetscInt m,MatReuse reuse,Mat *inmat)
201dd6ea824SBarry Smith {
202dd6ea824SBarry Smith   PetscMPIInt    rank,size;
203d892089bSMatthew G. Knepley   PetscInt       *rowners,*dlens,*olens,i,rstart,rend,j,jj,nz = 0,*gmataj,cnt,row,*ld,bses[2];
204dd6ea824SBarry Smith   PetscErrorCode ierr;
205dd6ea824SBarry Smith   Mat            mat;
206dd6ea824SBarry Smith   Mat_SeqAIJ     *gmata;
207dd6ea824SBarry Smith   PetscMPIInt    tag;
208dd6ea824SBarry Smith   MPI_Status     status;
209ace3abfcSBarry Smith   PetscBool      aij;
210dd6ea824SBarry Smith   MatScalar      *gmataa,*ao,*ad,*gmataarestore=0;
211dd6ea824SBarry Smith 
212dd6ea824SBarry Smith   PetscFunctionBegin;
213dd6ea824SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
214dd6ea824SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
215dd6ea824SBarry Smith   if (!rank) {
216251f4c67SDmitry Karpeev     ierr = PetscObjectTypeCompare((PetscObject)gmat,MATSEQAIJ,&aij);CHKERRQ(ierr);
217ce94432eSBarry Smith     if (!aij) SETERRQ1(PetscObjectComm((PetscObject)gmat),PETSC_ERR_SUP,"Currently no support for input matrix of type %s\n",((PetscObject)gmat)->type_name);
218dd6ea824SBarry Smith   }
219dd6ea824SBarry Smith   if (reuse == MAT_INITIAL_MATRIX) {
220dd6ea824SBarry Smith     ierr = MatCreate(comm,&mat);CHKERRQ(ierr);
221dd6ea824SBarry Smith     ierr = MatSetSizes(mat,m,m,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
22233d57670SJed Brown     ierr = MatGetBlockSizes(gmat,&bses[0],&bses[1]);CHKERRQ(ierr);
223efcf75d5SBarry Smith     ierr = MPI_Bcast(bses,2,MPIU_INT,0,comm);CHKERRQ(ierr);
224efcf75d5SBarry Smith     ierr = MatSetBlockSizes(mat,bses[0],bses[1]);CHKERRQ(ierr);
225dd6ea824SBarry Smith     ierr = MatSetType(mat,MATAIJ);CHKERRQ(ierr);
226785e854fSJed Brown     ierr = PetscMalloc1((size+1),&rowners);CHKERRQ(ierr);
227dcca6d9dSJed Brown     ierr = PetscMalloc2(m,&dlens,m,&olens);CHKERRQ(ierr);
228dd6ea824SBarry Smith     ierr = MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);CHKERRQ(ierr);
2292205254eSKarl Rupp 
230dd6ea824SBarry Smith     rowners[0] = 0;
2312205254eSKarl Rupp     for (i=2; i<=size; i++) rowners[i] += rowners[i-1];
232dd6ea824SBarry Smith     rstart = rowners[rank];
233dd6ea824SBarry Smith     rend   = rowners[rank+1];
234dd6ea824SBarry Smith     ierr   = PetscObjectGetNewTag((PetscObject)mat,&tag);CHKERRQ(ierr);
235dd6ea824SBarry Smith     if (!rank) {
236dd6ea824SBarry Smith       gmata = (Mat_SeqAIJ*) gmat->data;
237dd6ea824SBarry Smith       /* send row lengths to all processors */
238dd6ea824SBarry Smith       for (i=0; i<m; i++) dlens[i] = gmata->ilen[i];
239dd6ea824SBarry Smith       for (i=1; i<size; i++) {
240dd6ea824SBarry Smith         ierr = MPI_Send(gmata->ilen + rowners[i],rowners[i+1]-rowners[i],MPIU_INT,i,tag,comm);CHKERRQ(ierr);
241dd6ea824SBarry Smith       }
242dd6ea824SBarry Smith       /* determine number diagonal and off-diagonal counts */
243dd6ea824SBarry Smith       ierr = PetscMemzero(olens,m*sizeof(PetscInt));CHKERRQ(ierr);
2441795a4d1SJed Brown       ierr = PetscCalloc1(m,&ld);CHKERRQ(ierr);
245dd6ea824SBarry Smith       jj   = 0;
246dd6ea824SBarry Smith       for (i=0; i<m; i++) {
247dd6ea824SBarry Smith         for (j=0; j<dlens[i]; j++) {
248dd6ea824SBarry Smith           if (gmata->j[jj] < rstart) ld[i]++;
249dd6ea824SBarry Smith           if (gmata->j[jj] < rstart || gmata->j[jj] >= rend) olens[i]++;
250dd6ea824SBarry Smith           jj++;
251dd6ea824SBarry Smith         }
252dd6ea824SBarry Smith       }
253dd6ea824SBarry Smith       /* send column indices to other processes */
254dd6ea824SBarry Smith       for (i=1; i<size; i++) {
255dd6ea824SBarry Smith         nz   = gmata->i[rowners[i+1]]-gmata->i[rowners[i]];
256dd6ea824SBarry Smith         ierr = MPI_Send(&nz,1,MPIU_INT,i,tag,comm);CHKERRQ(ierr);
257dd6ea824SBarry Smith         ierr = MPI_Send(gmata->j + gmata->i[rowners[i]],nz,MPIU_INT,i,tag,comm);CHKERRQ(ierr);
258dd6ea824SBarry Smith       }
259dd6ea824SBarry Smith 
260dd6ea824SBarry Smith       /* send numerical values to other processes */
261dd6ea824SBarry Smith       for (i=1; i<size; i++) {
262dd6ea824SBarry Smith         nz   = gmata->i[rowners[i+1]]-gmata->i[rowners[i]];
263dd6ea824SBarry Smith         ierr = MPI_Send(gmata->a + gmata->i[rowners[i]],nz,MPIU_SCALAR,i,tag,comm);CHKERRQ(ierr);
264dd6ea824SBarry Smith       }
265dd6ea824SBarry Smith       gmataa = gmata->a;
266dd6ea824SBarry Smith       gmataj = gmata->j;
267dd6ea824SBarry Smith 
268dd6ea824SBarry Smith     } else {
269dd6ea824SBarry Smith       /* receive row lengths */
270dd6ea824SBarry Smith       ierr = MPI_Recv(dlens,m,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr);
271dd6ea824SBarry Smith       /* receive column indices */
272dd6ea824SBarry Smith       ierr = MPI_Recv(&nz,1,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr);
273dcca6d9dSJed Brown       ierr = PetscMalloc2(nz,&gmataa,nz,&gmataj);CHKERRQ(ierr);
274dd6ea824SBarry Smith       ierr = MPI_Recv(gmataj,nz,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr);
275dd6ea824SBarry Smith       /* determine number diagonal and off-diagonal counts */
276dd6ea824SBarry Smith       ierr = PetscMemzero(olens,m*sizeof(PetscInt));CHKERRQ(ierr);
2771795a4d1SJed Brown       ierr = PetscCalloc1(m,&ld);CHKERRQ(ierr);
278dd6ea824SBarry Smith       jj   = 0;
279dd6ea824SBarry Smith       for (i=0; i<m; i++) {
280dd6ea824SBarry Smith         for (j=0; j<dlens[i]; j++) {
281dd6ea824SBarry Smith           if (gmataj[jj] < rstart) ld[i]++;
282dd6ea824SBarry Smith           if (gmataj[jj] < rstart || gmataj[jj] >= rend) olens[i]++;
283dd6ea824SBarry Smith           jj++;
284dd6ea824SBarry Smith         }
285dd6ea824SBarry Smith       }
286dd6ea824SBarry Smith       /* receive numerical values */
287dd6ea824SBarry Smith       ierr = PetscMemzero(gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr);
288dd6ea824SBarry Smith       ierr = MPI_Recv(gmataa,nz,MPIU_SCALAR,0,tag,comm,&status);CHKERRQ(ierr);
289dd6ea824SBarry Smith     }
290dd6ea824SBarry Smith     /* set preallocation */
291dd6ea824SBarry Smith     for (i=0; i<m; i++) {
292dd6ea824SBarry Smith       dlens[i] -= olens[i];
293dd6ea824SBarry Smith     }
294dd6ea824SBarry Smith     ierr = MatSeqAIJSetPreallocation(mat,0,dlens);CHKERRQ(ierr);
295dd6ea824SBarry Smith     ierr = MatMPIAIJSetPreallocation(mat,0,dlens,0,olens);CHKERRQ(ierr);
296dd6ea824SBarry Smith 
297dd6ea824SBarry Smith     for (i=0; i<m; i++) {
298dd6ea824SBarry Smith       dlens[i] += olens[i];
299dd6ea824SBarry Smith     }
300dd6ea824SBarry Smith     cnt = 0;
301dd6ea824SBarry Smith     for (i=0; i<m; i++) {
302dd6ea824SBarry Smith       row  = rstart + i;
303dd6ea824SBarry Smith       ierr = MatSetValues(mat,1,&row,dlens[i],gmataj+cnt,gmataa+cnt,INSERT_VALUES);CHKERRQ(ierr);
304dd6ea824SBarry Smith       cnt += dlens[i];
305dd6ea824SBarry Smith     }
306dd6ea824SBarry Smith     if (rank) {
307dd6ea824SBarry Smith       ierr = PetscFree2(gmataa,gmataj);CHKERRQ(ierr);
308dd6ea824SBarry Smith     }
309dd6ea824SBarry Smith     ierr = PetscFree2(dlens,olens);CHKERRQ(ierr);
310dd6ea824SBarry Smith     ierr = PetscFree(rowners);CHKERRQ(ierr);
3112205254eSKarl Rupp 
312dd6ea824SBarry Smith     ((Mat_MPIAIJ*)(mat->data))->ld = ld;
3132205254eSKarl Rupp 
314dd6ea824SBarry Smith     *inmat = mat;
315dd6ea824SBarry Smith   } else {   /* column indices are already set; only need to move over numerical values from process 0 */
316dd6ea824SBarry Smith     Mat_SeqAIJ *Ad = (Mat_SeqAIJ*)((Mat_MPIAIJ*)((*inmat)->data))->A->data;
317dd6ea824SBarry Smith     Mat_SeqAIJ *Ao = (Mat_SeqAIJ*)((Mat_MPIAIJ*)((*inmat)->data))->B->data;
318dd6ea824SBarry Smith     mat  = *inmat;
319dd6ea824SBarry Smith     ierr = PetscObjectGetNewTag((PetscObject)mat,&tag);CHKERRQ(ierr);
320dd6ea824SBarry Smith     if (!rank) {
321dd6ea824SBarry Smith       /* send numerical values to other processes */
322dd6ea824SBarry Smith       gmata  = (Mat_SeqAIJ*) gmat->data;
323dd6ea824SBarry Smith       ierr   = MatGetOwnershipRanges(mat,(const PetscInt**)&rowners);CHKERRQ(ierr);
324dd6ea824SBarry Smith       gmataa = gmata->a;
325dd6ea824SBarry Smith       for (i=1; i<size; i++) {
326dd6ea824SBarry Smith         nz   = gmata->i[rowners[i+1]]-gmata->i[rowners[i]];
327dd6ea824SBarry Smith         ierr = MPI_Send(gmataa + gmata->i[rowners[i]],nz,MPIU_SCALAR,i,tag,comm);CHKERRQ(ierr);
328dd6ea824SBarry Smith       }
329dd6ea824SBarry Smith       nz = gmata->i[rowners[1]]-gmata->i[rowners[0]];
330dd6ea824SBarry Smith     } else {
331dd6ea824SBarry Smith       /* receive numerical values from process 0*/
332dd6ea824SBarry Smith       nz   = Ad->nz + Ao->nz;
333785e854fSJed Brown       ierr = PetscMalloc1(nz,&gmataa);CHKERRQ(ierr); gmataarestore = gmataa;
334dd6ea824SBarry Smith       ierr = MPI_Recv(gmataa,nz,MPIU_SCALAR,0,tag,comm,&status);CHKERRQ(ierr);
335dd6ea824SBarry Smith     }
336dd6ea824SBarry Smith     /* transfer numerical values into the diagonal A and off diagonal B parts of mat */
337dd6ea824SBarry Smith     ld = ((Mat_MPIAIJ*)(mat->data))->ld;
338dd6ea824SBarry Smith     ad = Ad->a;
339dd6ea824SBarry Smith     ao = Ao->a;
340d0f46423SBarry Smith     if (mat->rmap->n) {
341dd6ea824SBarry Smith       i  = 0;
342dd6ea824SBarry Smith       nz = ld[i];                                   ierr = PetscMemcpy(ao,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); ao += nz; gmataa += nz;
343dd6ea824SBarry Smith       nz = Ad->i[i+1] - Ad->i[i];                   ierr = PetscMemcpy(ad,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); ad += nz; gmataa += nz;
344dd6ea824SBarry Smith     }
345d0f46423SBarry Smith     for (i=1; i<mat->rmap->n; i++) {
346dd6ea824SBarry 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;
347dd6ea824SBarry Smith       nz = Ad->i[i+1] - Ad->i[i];                   ierr = PetscMemcpy(ad,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); ad += nz; gmataa += nz;
348dd6ea824SBarry Smith     }
349dd6ea824SBarry Smith     i--;
350d0f46423SBarry Smith     if (mat->rmap->n) {
35122d28d08SBarry Smith       nz = Ao->i[i+1] - Ao->i[i] - ld[i];           ierr = PetscMemcpy(ao,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr);
352dd6ea824SBarry Smith     }
353dd6ea824SBarry Smith     if (rank) {
354dd6ea824SBarry Smith       ierr = PetscFree(gmataarestore);CHKERRQ(ierr);
355dd6ea824SBarry Smith     }
356dd6ea824SBarry Smith   }
357dd6ea824SBarry Smith   ierr = MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
358dd6ea824SBarry Smith   ierr = MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
359dd6ea824SBarry Smith   PetscFunctionReturn(0);
360dd6ea824SBarry Smith }
361dd6ea824SBarry Smith 
3620f5bd95cSBarry Smith /*
3630f5bd95cSBarry Smith   Local utility routine that creates a mapping from the global column
3649e25ed09SBarry Smith number to the local number in the off-diagonal part of the local
3650f5bd95cSBarry Smith storage of the matrix.  When PETSC_USE_CTABLE is used this is scalable at
3660f5bd95cSBarry Smith a slightly higher hash table cost; without it it is not scalable (each processor
3670f5bd95cSBarry Smith has an order N integer array but is fast to acess.
3689e25ed09SBarry Smith */
3694a2ae208SSatish Balay #undef __FUNCT__
370ab9863d7SBarry Smith #define __FUNCT__ "MatCreateColmap_MPIAIJ_Private"
371ab9863d7SBarry Smith PetscErrorCode MatCreateColmap_MPIAIJ_Private(Mat mat)
3729e25ed09SBarry Smith {
37344a69424SLois Curfman McInnes   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
3746849ba73SBarry Smith   PetscErrorCode ierr;
375d0f46423SBarry Smith   PetscInt       n = aij->B->cmap->n,i;
376dbb450caSBarry Smith 
3773a40ed3dSBarry Smith   PetscFunctionBegin;
3785e1f6667SBarry Smith   if (!aij->garray) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"MPIAIJ Matrix was assembled but is missing garray");
379aa482453SBarry Smith #if defined(PETSC_USE_CTABLE)
380e23dfa41SBarry Smith   ierr = PetscTableCreate(n,mat->cmap->N+1,&aij->colmap);CHKERRQ(ierr);
381b1fc9764SSatish Balay   for (i=0; i<n; i++) {
3823861aac3SJed Brown     ierr = PetscTableAdd(aij->colmap,aij->garray[i]+1,i+1,INSERT_VALUES);CHKERRQ(ierr);
383b1fc9764SSatish Balay   }
384b1fc9764SSatish Balay #else
3851795a4d1SJed Brown   ierr = PetscCalloc1((mat->cmap->N+1),&aij->colmap);CHKERRQ(ierr);
3861795a4d1SJed Brown   ierr = PetscLogObjectMemory((PetscObject)mat,(mat->cmap->N+1)*sizeof(PetscInt));CHKERRQ(ierr);
387905e6a2fSBarry Smith   for (i=0; i<n; i++) aij->colmap[aij->garray[i]] = i+1;
388b1fc9764SSatish Balay #endif
3893a40ed3dSBarry Smith   PetscFunctionReturn(0);
3909e25ed09SBarry Smith }
3919e25ed09SBarry Smith 
39230770e4dSSatish Balay #define MatSetValues_SeqAIJ_A_Private(row,col,value,addv) \
3930520107fSSatish Balay { \
394db4deed7SKarl Rupp     if (col <= lastcol1)  low1 = 0;     \
395db4deed7SKarl Rupp     else                 high1 = nrow1; \
396fd3458f5SBarry Smith     lastcol1 = col;\
397fd3458f5SBarry Smith     while (high1-low1 > 5) { \
398fd3458f5SBarry Smith       t = (low1+high1)/2; \
399fd3458f5SBarry Smith       if (rp1[t] > col) high1 = t; \
400fd3458f5SBarry Smith       else              low1  = t; \
401ba4e3ef2SSatish Balay     } \
402fd3458f5SBarry Smith       for (_i=low1; _i<high1; _i++) { \
403fd3458f5SBarry Smith         if (rp1[_i] > col) break; \
404fd3458f5SBarry Smith         if (rp1[_i] == col) { \
405fd3458f5SBarry Smith           if (addv == ADD_VALUES) ap1[_i] += value;   \
406fd3458f5SBarry Smith           else                    ap1[_i] = value; \
40730770e4dSSatish Balay           goto a_noinsert; \
4080520107fSSatish Balay         } \
4090520107fSSatish Balay       }  \
410e44c0bd4SBarry Smith       if (value == 0.0 && ignorezeroentries) {low1 = 0; high1 = nrow1;goto a_noinsert;} \
411e44c0bd4SBarry Smith       if (nonew == 1) {low1 = 0; high1 = nrow1; goto a_noinsert;}                \
412e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \
413fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,am,1,nrow1,row,col,rmax1,aa,ai,aj,rp1,ap1,aimax,nonew,MatScalar); \
414669a8dbcSSatish Balay       N = nrow1++ - 1; a->nz++; high1++; \
4150520107fSSatish Balay       /* shift up all the later entries in this row */ \
4160520107fSSatish Balay       for (ii=N; ii>=_i; ii--) { \
417fd3458f5SBarry Smith         rp1[ii+1] = rp1[ii]; \
418fd3458f5SBarry Smith         ap1[ii+1] = ap1[ii]; \
4190520107fSSatish Balay       } \
420fd3458f5SBarry Smith       rp1[_i] = col;  \
421fd3458f5SBarry Smith       ap1[_i] = value;  \
422e56f5c9eSBarry Smith       A->nonzerostate++;\
42330770e4dSSatish Balay       a_noinsert: ; \
424fd3458f5SBarry Smith       ailen[row] = nrow1; \
4250520107fSSatish Balay }
4260a198c4cSBarry Smith 
427085a36d4SBarry Smith 
42830770e4dSSatish Balay #define MatSetValues_SeqAIJ_B_Private(row,col,value,addv) \
42930770e4dSSatish Balay   { \
430db4deed7SKarl Rupp     if (col <= lastcol2) low2 = 0;                        \
431db4deed7SKarl Rupp     else high2 = nrow2;                                   \
432fd3458f5SBarry Smith     lastcol2 = col;                                       \
433fd3458f5SBarry Smith     while (high2-low2 > 5) {                              \
434fd3458f5SBarry Smith       t = (low2+high2)/2;                                 \
435fd3458f5SBarry Smith       if (rp2[t] > col) high2 = t;                        \
436fd3458f5SBarry Smith       else             low2  = t;                         \
437ba4e3ef2SSatish Balay     }                                                     \
438fd3458f5SBarry Smith     for (_i=low2; _i<high2; _i++) {                       \
439fd3458f5SBarry Smith       if (rp2[_i] > col) break;                           \
440fd3458f5SBarry Smith       if (rp2[_i] == col) {                               \
441fd3458f5SBarry Smith         if (addv == ADD_VALUES) ap2[_i] += value;         \
442fd3458f5SBarry Smith         else                    ap2[_i] = value;          \
44330770e4dSSatish Balay         goto b_noinsert;                                  \
44430770e4dSSatish Balay       }                                                   \
44530770e4dSSatish Balay     }                                                     \
446e44c0bd4SBarry Smith     if (value == 0.0 && ignorezeroentries) {low2 = 0; high2 = nrow2; goto b_noinsert;} \
447e44c0bd4SBarry Smith     if (nonew == 1) {low2 = 0; high2 = nrow2; goto b_noinsert;}                        \
448e32f2f54SBarry Smith     if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \
449fef13f97SBarry Smith     MatSeqXAIJReallocateAIJ(B,bm,1,nrow2,row,col,rmax2,ba,bi,bj,rp2,ap2,bimax,nonew,MatScalar); \
450669a8dbcSSatish Balay     N = nrow2++ - 1; b->nz++; high2++;                    \
45130770e4dSSatish Balay     /* shift up all the later entries in this row */      \
45230770e4dSSatish Balay     for (ii=N; ii>=_i; ii--) {                            \
453fd3458f5SBarry Smith       rp2[ii+1] = rp2[ii];                                \
454fd3458f5SBarry Smith       ap2[ii+1] = ap2[ii];                                \
45530770e4dSSatish Balay     }                                                     \
456fd3458f5SBarry Smith     rp2[_i] = col;                                        \
457fd3458f5SBarry Smith     ap2[_i] = value;                                      \
458e56f5c9eSBarry Smith     B->nonzerostate++;                                    \
45930770e4dSSatish Balay     b_noinsert: ;                                         \
460fd3458f5SBarry Smith     bilen[row] = nrow2;                                   \
46130770e4dSSatish Balay   }
46230770e4dSSatish Balay 
4634a2ae208SSatish Balay #undef __FUNCT__
4642fd7e33dSBarry Smith #define __FUNCT__ "MatSetValuesRow_MPIAIJ"
4652fd7e33dSBarry Smith PetscErrorCode MatSetValuesRow_MPIAIJ(Mat A,PetscInt row,const PetscScalar v[])
4662fd7e33dSBarry Smith {
4672fd7e33dSBarry Smith   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)A->data;
4682fd7e33dSBarry Smith   Mat_SeqAIJ     *a   = (Mat_SeqAIJ*)mat->A->data,*b = (Mat_SeqAIJ*)mat->B->data;
4692fd7e33dSBarry Smith   PetscErrorCode ierr;
4702fd7e33dSBarry Smith   PetscInt       l,*garray = mat->garray,diag;
4712fd7e33dSBarry Smith 
4722fd7e33dSBarry Smith   PetscFunctionBegin;
4732fd7e33dSBarry Smith   /* code only works for square matrices A */
4742fd7e33dSBarry Smith 
4752fd7e33dSBarry Smith   /* find size of row to the left of the diagonal part */
4762fd7e33dSBarry Smith   ierr = MatGetOwnershipRange(A,&diag,0);CHKERRQ(ierr);
4772fd7e33dSBarry Smith   row  = row - diag;
4782fd7e33dSBarry Smith   for (l=0; l<b->i[row+1]-b->i[row]; l++) {
4792fd7e33dSBarry Smith     if (garray[b->j[b->i[row]+l]] > diag) break;
4802fd7e33dSBarry Smith   }
4812fd7e33dSBarry Smith   ierr = PetscMemcpy(b->a+b->i[row],v,l*sizeof(PetscScalar));CHKERRQ(ierr);
4822fd7e33dSBarry Smith 
4832fd7e33dSBarry Smith   /* diagonal part */
4842fd7e33dSBarry Smith   ierr = PetscMemcpy(a->a+a->i[row],v+l,(a->i[row+1]-a->i[row])*sizeof(PetscScalar));CHKERRQ(ierr);
4852fd7e33dSBarry Smith 
4862fd7e33dSBarry Smith   /* right of diagonal part */
4872fd7e33dSBarry 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);
4882fd7e33dSBarry Smith   PetscFunctionReturn(0);
4892fd7e33dSBarry Smith }
4902fd7e33dSBarry Smith 
4912fd7e33dSBarry Smith #undef __FUNCT__
4924a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_MPIAIJ"
493b1d57f15SBarry Smith PetscErrorCode MatSetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
4948a729477SBarry Smith {
49544a69424SLois Curfman McInnes   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
49687828ca2SBarry Smith   PetscScalar    value;
497dfbe8321SBarry Smith   PetscErrorCode ierr;
498d0f46423SBarry Smith   PetscInt       i,j,rstart  = mat->rmap->rstart,rend = mat->rmap->rend;
499d0f46423SBarry Smith   PetscInt       cstart      = mat->cmap->rstart,cend = mat->cmap->rend,row,col;
500ace3abfcSBarry Smith   PetscBool      roworiented = aij->roworiented;
5018a729477SBarry Smith 
5020520107fSSatish Balay   /* Some Variables required in the macro */
5034ee7247eSSatish Balay   Mat        A                 = aij->A;
5044ee7247eSSatish Balay   Mat_SeqAIJ *a                = (Mat_SeqAIJ*)A->data;
50557809a77SBarry Smith   PetscInt   *aimax            = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j;
506a77337e4SBarry Smith   MatScalar  *aa               = a->a;
507ace3abfcSBarry Smith   PetscBool  ignorezeroentries = a->ignorezeroentries;
50830770e4dSSatish Balay   Mat        B                 = aij->B;
50930770e4dSSatish Balay   Mat_SeqAIJ *b                = (Mat_SeqAIJ*)B->data;
510d0f46423SBarry Smith   PetscInt   *bimax            = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap->n,am = aij->A->rmap->n;
511a77337e4SBarry Smith   MatScalar  *ba               = b->a;
51230770e4dSSatish Balay 
513fd3458f5SBarry Smith   PetscInt  *rp1,*rp2,ii,nrow1,nrow2,_i,rmax1,rmax2,N,low1,high1,low2,high2,t,lastcol1,lastcol2;
5148d76821aSHong Zhang   PetscInt  nonew;
515a77337e4SBarry Smith   MatScalar *ap1,*ap2;
5164ee7247eSSatish Balay 
5173a40ed3dSBarry Smith   PetscFunctionBegin;
5188a729477SBarry Smith   for (i=0; i<m; i++) {
5195ef9f2a5SBarry Smith     if (im[i] < 0) continue;
5202515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
521e32f2f54SBarry 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);
5220a198c4cSBarry Smith #endif
5234b0e389bSBarry Smith     if (im[i] >= rstart && im[i] < rend) {
5244b0e389bSBarry Smith       row      = im[i] - rstart;
525fd3458f5SBarry Smith       lastcol1 = -1;
526fd3458f5SBarry Smith       rp1      = aj + ai[row];
527fd3458f5SBarry Smith       ap1      = aa + ai[row];
528fd3458f5SBarry Smith       rmax1    = aimax[row];
529fd3458f5SBarry Smith       nrow1    = ailen[row];
530fd3458f5SBarry Smith       low1     = 0;
531fd3458f5SBarry Smith       high1    = nrow1;
532fd3458f5SBarry Smith       lastcol2 = -1;
533fd3458f5SBarry Smith       rp2      = bj + bi[row];
534d498b1e9SBarry Smith       ap2      = ba + bi[row];
535fd3458f5SBarry Smith       rmax2    = bimax[row];
536d498b1e9SBarry Smith       nrow2    = bilen[row];
537fd3458f5SBarry Smith       low2     = 0;
538fd3458f5SBarry Smith       high2    = nrow2;
539fd3458f5SBarry Smith 
5401eb62cbbSBarry Smith       for (j=0; j<n; j++) {
541db4deed7SKarl Rupp         if (roworiented) value = v[i*n+j];
542db4deed7SKarl Rupp         else             value = v[i+j*m];
543abc0a331SBarry Smith         if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES)) continue;
544fd3458f5SBarry Smith         if (in[j] >= cstart && in[j] < cend) {
545fd3458f5SBarry Smith           col   = in[j] - cstart;
5468d76821aSHong Zhang           nonew = a->nonew;
54730770e4dSSatish Balay           MatSetValues_SeqAIJ_A_Private(row,col,value,addv);
548273d9f13SBarry Smith         } else if (in[j] < 0) continue;
5492515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
550cb9801acSJed 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);
5510a198c4cSBarry Smith #endif
5521eb62cbbSBarry Smith         else {
553227d817aSBarry Smith           if (mat->was_assembled) {
554905e6a2fSBarry Smith             if (!aij->colmap) {
555ab9863d7SBarry Smith               ierr = MatCreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
556905e6a2fSBarry Smith             }
557aa482453SBarry Smith #if defined(PETSC_USE_CTABLE)
5580f5bd95cSBarry Smith             ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr);
559fa46199cSSatish Balay             col--;
560b1fc9764SSatish Balay #else
561905e6a2fSBarry Smith             col = aij->colmap[in[j]] - 1;
562b1fc9764SSatish Balay #endif
5630e9bae81SBarry Smith             if (col < 0 && !((Mat_SeqAIJ*)(aij->B->data))->nonew) {
564ab9863d7SBarry Smith               ierr = MatDisAssemble_MPIAIJ(mat);CHKERRQ(ierr);
5654b0e389bSBarry Smith               col  =  in[j];
5669bf004c3SSatish Balay               /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
567f9508a3cSSatish Balay               B     = aij->B;
568f9508a3cSSatish Balay               b     = (Mat_SeqAIJ*)B->data;
569e44c0bd4SBarry Smith               bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j; ba = b->a;
570d498b1e9SBarry Smith               rp2   = bj + bi[row];
571d498b1e9SBarry Smith               ap2   = ba + bi[row];
572d498b1e9SBarry Smith               rmax2 = bimax[row];
573d498b1e9SBarry Smith               nrow2 = bilen[row];
574d498b1e9SBarry Smith               low2  = 0;
575d498b1e9SBarry Smith               high2 = nrow2;
576d0f46423SBarry Smith               bm    = aij->B->rmap->n;
577f9508a3cSSatish Balay               ba    = b->a;
5780e9bae81SBarry 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]);
579c48de900SBarry Smith           } else col = in[j];
5808d76821aSHong Zhang           nonew = b->nonew;
58130770e4dSSatish Balay           MatSetValues_SeqAIJ_B_Private(row,col,value,addv);
5821eb62cbbSBarry Smith         }
5831eb62cbbSBarry Smith       }
5845ef9f2a5SBarry Smith     } else {
5854cb17eb5SBarry 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]);
58690f02eecSBarry Smith       if (!aij->donotstash) {
5875080c13bSMatthew G Knepley         mat->assembled = PETSC_FALSE;
588d36fbae8SSatish Balay         if (roworiented) {
589ace3abfcSBarry Smith           ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr);
590d36fbae8SSatish Balay         } else {
591ace3abfcSBarry Smith           ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr);
5924b0e389bSBarry Smith         }
5931eb62cbbSBarry Smith       }
5948a729477SBarry Smith     }
59590f02eecSBarry Smith   }
5963a40ed3dSBarry Smith   PetscFunctionReturn(0);
5978a729477SBarry Smith }
5988a729477SBarry Smith 
5994a2ae208SSatish Balay #undef __FUNCT__
6004a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_MPIAIJ"
601b1d57f15SBarry Smith PetscErrorCode MatGetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[])
602b49de8d1SLois Curfman McInnes {
603b49de8d1SLois Curfman McInnes   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
604dfbe8321SBarry Smith   PetscErrorCode ierr;
605d0f46423SBarry Smith   PetscInt       i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend;
606d0f46423SBarry Smith   PetscInt       cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col;
607b49de8d1SLois Curfman McInnes 
6083a40ed3dSBarry Smith   PetscFunctionBegin;
609b49de8d1SLois Curfman McInnes   for (i=0; i<m; i++) {
610e32f2f54SBarry Smith     if (idxm[i] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",idxm[i]);*/
611e32f2f54SBarry 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);
612b49de8d1SLois Curfman McInnes     if (idxm[i] >= rstart && idxm[i] < rend) {
613b49de8d1SLois Curfman McInnes       row = idxm[i] - rstart;
614b49de8d1SLois Curfman McInnes       for (j=0; j<n; j++) {
615e32f2f54SBarry Smith         if (idxn[j] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",idxn[j]); */
616e32f2f54SBarry 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);
617b49de8d1SLois Curfman McInnes         if (idxn[j] >= cstart && idxn[j] < cend) {
618b49de8d1SLois Curfman McInnes           col  = idxn[j] - cstart;
619b49de8d1SLois Curfman McInnes           ierr = MatGetValues(aij->A,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
620fa852ad4SSatish Balay         } else {
621905e6a2fSBarry Smith           if (!aij->colmap) {
622ab9863d7SBarry Smith             ierr = MatCreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
623905e6a2fSBarry Smith           }
624aa482453SBarry Smith #if defined(PETSC_USE_CTABLE)
6250f5bd95cSBarry Smith           ierr = PetscTableFind(aij->colmap,idxn[j]+1,&col);CHKERRQ(ierr);
626fa46199cSSatish Balay           col--;
627b1fc9764SSatish Balay #else
628905e6a2fSBarry Smith           col = aij->colmap[idxn[j]] - 1;
629b1fc9764SSatish Balay #endif
630e60e1c95SSatish Balay           if ((col < 0) || (aij->garray[col] != idxn[j])) *(v+i*n+j) = 0.0;
631d9d09a02SSatish Balay           else {
632b49de8d1SLois Curfman McInnes             ierr = MatGetValues(aij->B,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
633b49de8d1SLois Curfman McInnes           }
634b49de8d1SLois Curfman McInnes         }
635b49de8d1SLois Curfman McInnes       }
636f23aa3ddSBarry Smith     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local values currently supported");
637b49de8d1SLois Curfman McInnes   }
6383a40ed3dSBarry Smith   PetscFunctionReturn(0);
639b49de8d1SLois Curfman McInnes }
640bc5ccf88SSatish Balay 
641bd0c2dcbSBarry Smith extern PetscErrorCode MatMultDiagonalBlock_MPIAIJ(Mat,Vec,Vec);
642bd0c2dcbSBarry Smith 
6434a2ae208SSatish Balay #undef __FUNCT__
6444a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyBegin_MPIAIJ"
645dfbe8321SBarry Smith PetscErrorCode MatAssemblyBegin_MPIAIJ(Mat mat,MatAssemblyType mode)
646bc5ccf88SSatish Balay {
647bc5ccf88SSatish Balay   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
648dfbe8321SBarry Smith   PetscErrorCode ierr;
649b1d57f15SBarry Smith   PetscInt       nstash,reallocs;
650bc5ccf88SSatish Balay   InsertMode     addv;
651bc5ccf88SSatish Balay 
652bc5ccf88SSatish Balay   PetscFunctionBegin;
6532205254eSKarl Rupp   if (aij->donotstash || mat->nooffprocentries) PetscFunctionReturn(0);
654bc5ccf88SSatish Balay 
655bc5ccf88SSatish Balay   /* make sure all processors are either in INSERTMODE or ADDMODE */
656ce94432eSBarry Smith   ierr = MPI_Allreduce((PetscEnum*)&mat->insertmode,(PetscEnum*)&addv,1,MPIU_ENUM,MPI_BOR,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
657ce94432eSBarry Smith   if (addv == (ADD_VALUES|INSERT_VALUES)) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Some processors inserted others added");
658bc5ccf88SSatish Balay   mat->insertmode = addv; /* in case this processor had no cache */
659bc5ccf88SSatish Balay 
660d0f46423SBarry Smith   ierr = MatStashScatterBegin_Private(mat,&mat->stash,mat->rmap->range);CHKERRQ(ierr);
6618798bf22SSatish Balay   ierr = MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);CHKERRQ(ierr);
662ae15b995SBarry Smith   ierr = PetscInfo2(aij->A,"Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);CHKERRQ(ierr);
663bc5ccf88SSatish Balay   PetscFunctionReturn(0);
664bc5ccf88SSatish Balay }
665bc5ccf88SSatish Balay 
6664a2ae208SSatish Balay #undef __FUNCT__
6674a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_MPIAIJ"
668dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_MPIAIJ(Mat mat,MatAssemblyType mode)
669bc5ccf88SSatish Balay {
670bc5ccf88SSatish Balay   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
67191c97fd4SSatish Balay   Mat_SeqAIJ     *a   = (Mat_SeqAIJ*)aij->A->data;
6726849ba73SBarry Smith   PetscErrorCode ierr;
673b1d57f15SBarry Smith   PetscMPIInt    n;
674b1d57f15SBarry Smith   PetscInt       i,j,rstart,ncols,flg;
675e44c0bd4SBarry Smith   PetscInt       *row,*col;
676ace3abfcSBarry Smith   PetscBool      other_disassembled;
67787828ca2SBarry Smith   PetscScalar    *val;
678bc5ccf88SSatish Balay   InsertMode     addv = mat->insertmode;
679bc5ccf88SSatish Balay 
68091c97fd4SSatish Balay   /* do not use 'b = (Mat_SeqAIJ*)aij->B->data' as B can be reset in disassembly */
6816e111a19SKarl Rupp 
682bc5ccf88SSatish Balay   PetscFunctionBegin;
6834cb17eb5SBarry Smith   if (!aij->donotstash && !mat->nooffprocentries) {
684a2d1c673SSatish Balay     while (1) {
6858798bf22SSatish Balay       ierr = MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);CHKERRQ(ierr);
686a2d1c673SSatish Balay       if (!flg) break;
687a2d1c673SSatish Balay 
688bc5ccf88SSatish Balay       for (i=0; i<n; ) {
689bc5ccf88SSatish Balay         /* Now identify the consecutive vals belonging to the same row */
6902205254eSKarl Rupp         for (j=i,rstart=row[j]; j<n; j++) {
6912205254eSKarl Rupp           if (row[j] != rstart) break;
6922205254eSKarl Rupp         }
693bc5ccf88SSatish Balay         if (j < n) ncols = j-i;
694bc5ccf88SSatish Balay         else       ncols = n-i;
695bc5ccf88SSatish Balay         /* Now assemble all these values with a single function call */
696bc5ccf88SSatish Balay         ierr = MatSetValues_MPIAIJ(mat,1,row+i,ncols,col+i,val+i,addv);CHKERRQ(ierr);
6972205254eSKarl Rupp 
698bc5ccf88SSatish Balay         i = j;
699bc5ccf88SSatish Balay       }
700bc5ccf88SSatish Balay     }
7018798bf22SSatish Balay     ierr = MatStashScatterEnd_Private(&mat->stash);CHKERRQ(ierr);
702bc5ccf88SSatish Balay   }
703bc5ccf88SSatish Balay   ierr = MatAssemblyBegin(aij->A,mode);CHKERRQ(ierr);
704bc5ccf88SSatish Balay   ierr = MatAssemblyEnd(aij->A,mode);CHKERRQ(ierr);
705bc5ccf88SSatish Balay 
706bc5ccf88SSatish Balay   /* determine if any processor has disassembled, if so we must
707bc5ccf88SSatish Balay      also disassemble ourselfs, in order that we may reassemble. */
708bc5ccf88SSatish Balay   /*
709bc5ccf88SSatish Balay      if nonzero structure of submatrix B cannot change then we know that
710bc5ccf88SSatish Balay      no processor disassembled thus we can skip this stuff
711bc5ccf88SSatish Balay   */
712bc5ccf88SSatish Balay   if (!((Mat_SeqAIJ*)aij->B->data)->nonew) {
713ce94432eSBarry Smith     ierr = MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPIU_BOOL,MPI_PROD,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
714bc5ccf88SSatish Balay     if (mat->was_assembled && !other_disassembled) {
715ab9863d7SBarry Smith       ierr = MatDisAssemble_MPIAIJ(mat);CHKERRQ(ierr);
716ad59fb31SSatish Balay     }
717ad59fb31SSatish Balay   }
718bc5ccf88SSatish Balay   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
719bc5ccf88SSatish Balay     ierr = MatSetUpMultiply_MPIAIJ(mat);CHKERRQ(ierr);
720bc5ccf88SSatish Balay   }
7214e0d8c25SBarry Smith   ierr = MatSetOption(aij->B,MAT_USE_INODES,PETSC_FALSE);CHKERRQ(ierr);
722bc5ccf88SSatish Balay   ierr = MatAssemblyBegin(aij->B,mode);CHKERRQ(ierr);
723bc5ccf88SSatish Balay   ierr = MatAssemblyEnd(aij->B,mode);CHKERRQ(ierr);
724bc5ccf88SSatish Balay 
7251d79065fSBarry Smith   ierr = PetscFree2(aij->rowvalues,aij->rowindices);CHKERRQ(ierr);
7262205254eSKarl Rupp 
727606d414cSSatish Balay   aij->rowvalues = 0;
728a30b2313SHong Zhang 
729a30b2313SHong Zhang   /* used by MatAXPY() */
73091c97fd4SSatish Balay   a->xtoy = 0; ((Mat_SeqAIJ*)aij->B->data)->xtoy = 0;   /* b->xtoy = 0 */
73191c97fd4SSatish Balay   a->XtoY = 0; ((Mat_SeqAIJ*)aij->B->data)->XtoY = 0;   /* b->XtoY = 0 */
732a30b2313SHong Zhang 
7336bf464f9SBarry Smith   ierr = VecDestroy(&aij->diag);CHKERRQ(ierr);
734bd0c2dcbSBarry Smith   if (a->inode.size) mat->ops->multdiagonalblock = MatMultDiagonalBlock_MPIAIJ;
735e56f5c9eSBarry Smith 
7364f9cfa9eSBarry Smith   /* if no new nonzero locations are allowed in matrix then only set the matrix state the first time through */
7374f9cfa9eSBarry Smith   if ((!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) || !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
738e56f5c9eSBarry Smith     PetscObjectState state = aij->A->nonzerostate + aij->B->nonzerostate;
73909e82e2bSBarry Smith     ierr = MPI_Allreduce(&state,&mat->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
740e56f5c9eSBarry Smith   }
741bc5ccf88SSatish Balay   PetscFunctionReturn(0);
742bc5ccf88SSatish Balay }
743bc5ccf88SSatish Balay 
7444a2ae208SSatish Balay #undef __FUNCT__
7454a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_MPIAIJ"
746dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_MPIAIJ(Mat A)
7471eb62cbbSBarry Smith {
74844a69424SLois Curfman McInnes   Mat_MPIAIJ     *l = (Mat_MPIAIJ*)A->data;
749dfbe8321SBarry Smith   PetscErrorCode ierr;
7503a40ed3dSBarry Smith 
7513a40ed3dSBarry Smith   PetscFunctionBegin;
75278b31e54SBarry Smith   ierr = MatZeroEntries(l->A);CHKERRQ(ierr);
75378b31e54SBarry Smith   ierr = MatZeroEntries(l->B);CHKERRQ(ierr);
7543a40ed3dSBarry Smith   PetscFunctionReturn(0);
7551eb62cbbSBarry Smith }
7561eb62cbbSBarry Smith 
7574a2ae208SSatish Balay #undef __FUNCT__
7584a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_MPIAIJ"
7592b40b63fSBarry Smith PetscErrorCode MatZeroRows_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
7601eb62cbbSBarry Smith {
7611b1dd7adSMatthew G. Knepley   Mat_MPIAIJ    *mat    = (Mat_MPIAIJ *) A->data;
7621b1dd7adSMatthew G. Knepley   PetscInt      *owners = A->rmap->range;
7631b1dd7adSMatthew G. Knepley   PetscInt       n      = A->rmap->n;
7641b1dd7adSMatthew G. Knepley   PetscSF        sf;
7651b1dd7adSMatthew G. Knepley   PetscInt      *lrows;
7661b1dd7adSMatthew G. Knepley   PetscSFNode   *rrows;
76769ea2d38SJed Brown   PetscInt       r, p = 0, len = 0;
7686849ba73SBarry Smith   PetscErrorCode ierr;
7691eb62cbbSBarry Smith 
7703a40ed3dSBarry Smith   PetscFunctionBegin;
7711b1dd7adSMatthew G. Knepley   /* Create SF where leaves are input rows and roots are owned rows */
772785e854fSJed Brown   ierr = PetscMalloc1(n, &lrows);CHKERRQ(ierr);
7739d80f4afSMatthew G. Knepley   for (r = 0; r < n; ++r) lrows[r] = -1;
774a34163a4SJed Brown   if (!A->nooffproczerorows) {ierr = PetscMalloc1(N, &rrows);CHKERRQ(ierr);}
7751b1dd7adSMatthew G. Knepley   for (r = 0; r < N; ++r) {
7761b1dd7adSMatthew G. Knepley     const PetscInt idx   = rows[r];
77769ea2d38SJed 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);
77869ea2d38SJed Brown     if (idx < owners[p] || owners[p+1] <= idx) { /* short-circuit the search if the last p owns this row too */
77969ea2d38SJed Brown       ierr = PetscLayoutFindOwner(A->rmap,idx,&p);CHKERRQ(ierr);
78069ea2d38SJed Brown     }
781a34163a4SJed Brown     if (A->nooffproczerorows) {
782a34163a4SJed 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);
783a34163a4SJed Brown       lrows[len++] = idx - owners[p];
784a34163a4SJed Brown     } else {
7851b1dd7adSMatthew G. Knepley       rrows[r].rank = p;
7861b1dd7adSMatthew G. Knepley       rrows[r].index = rows[r] - owners[p];
7871eb62cbbSBarry Smith     }
7881eb62cbbSBarry Smith   }
789a34163a4SJed Brown   if (!A->nooffproczerorows) {
7901b1dd7adSMatthew G. Knepley     ierr = PetscSFCreate(PetscObjectComm((PetscObject) A), &sf);CHKERRQ(ierr);
7911b1dd7adSMatthew G. Knepley     ierr = PetscSFSetGraph(sf, n, N, NULL, PETSC_OWN_POINTER, rrows, PETSC_OWN_POINTER);CHKERRQ(ierr);
7921b1dd7adSMatthew G. Knepley     /* Collect flags for rows to be zeroed */
79358c26cb0SMatthew G. Knepley     ierr = PetscSFReduceBegin(sf, MPIU_INT, (PetscInt*)rows, lrows, MPI_LOR);CHKERRQ(ierr);
79458c26cb0SMatthew G. Knepley     ierr = PetscSFReduceEnd(sf, MPIU_INT, (PetscInt*)rows, lrows, MPI_LOR);CHKERRQ(ierr);
7951b1dd7adSMatthew G. Knepley     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
7961b1dd7adSMatthew G. Knepley     /* Compress and put in row numbers */
7979d80f4afSMatthew G. Knepley     for (r = 0; r < n; ++r) if (lrows[r] >= 0) lrows[len++] = r;
798a34163a4SJed Brown   }
79997b48c8fSBarry Smith   /* fix right hand side if needed */
80097b48c8fSBarry Smith   if (x && b) {
8011b1dd7adSMatthew G. Knepley     const PetscScalar *xx;
8021b1dd7adSMatthew G. Knepley     PetscScalar       *bb;
8031b1dd7adSMatthew G. Knepley 
80497b48c8fSBarry Smith     ierr = VecGetArrayRead(x, &xx);CHKERRQ(ierr);
80597b48c8fSBarry Smith     ierr = VecGetArray(b, &bb);CHKERRQ(ierr);
8061b1dd7adSMatthew G. Knepley     for (r = 0; r < len; ++r) bb[lrows[r]] = diag*xx[lrows[r]];
80797b48c8fSBarry Smith     ierr = VecRestoreArrayRead(x, &xx);CHKERRQ(ierr);
80897b48c8fSBarry Smith     ierr = VecRestoreArray(b, &bb);CHKERRQ(ierr);
80997b48c8fSBarry Smith   }
8101b1dd7adSMatthew G. Knepley   /* Must zero l->B before l->A because the (diag) case below may put values into l->B*/
811a34163a4SJed Brown   ierr = MatZeroRows(mat->B, len, lrows, 0.0, NULL, NULL);CHKERRQ(ierr);
8121b1dd7adSMatthew G. Knepley   if ((diag != 0.0) && (mat->A->rmap->N == mat->A->cmap->N)) {
8131b1dd7adSMatthew G. Knepley     ierr = MatZeroRows(mat->A, len, lrows, diag, NULL, NULL);CHKERRQ(ierr);
814f4df32b1SMatthew Knepley   } else if (diag != 0.0) {
8151b1dd7adSMatthew G. Knepley     ierr = MatZeroRows(mat->A, len, lrows, 0.0, NULL, NULL);CHKERRQ(ierr);
8161b1dd7adSMatthew 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");
8171b1dd7adSMatthew G. Knepley     for (r = 0; r < len; ++r) {
8181b1dd7adSMatthew G. Knepley       const PetscInt row = lrows[r] + A->rmap->rstart;
819f4df32b1SMatthew Knepley       ierr = MatSetValues(A, 1, &row, 1, &row, &diag, INSERT_VALUES);CHKERRQ(ierr);
820e2d53e46SBarry Smith     }
821e2d53e46SBarry Smith     ierr = MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
822e2d53e46SBarry Smith     ierr = MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
8236eb55b6aSBarry Smith   } else {
8241b1dd7adSMatthew G. Knepley     ierr = MatZeroRows(mat->A, len, lrows, 0.0, NULL, NULL);CHKERRQ(ierr);
8256eb55b6aSBarry Smith   }
826606d414cSSatish Balay   ierr = PetscFree(lrows);CHKERRQ(ierr);
8274f9cfa9eSBarry Smith 
8284f9cfa9eSBarry Smith   /* only change matrix nonzero state if pattern was allowed to be changed */
8294f9cfa9eSBarry Smith   if (!((Mat_SeqAIJ*)(mat->A->data))->keepnonzeropattern) {
830e56f5c9eSBarry Smith     PetscObjectState state = mat->A->nonzerostate + mat->B->nonzerostate;
83109e82e2bSBarry Smith     ierr = MPI_Allreduce(&state,&A->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)A));CHKERRQ(ierr);
832e56f5c9eSBarry Smith   }
8333a40ed3dSBarry Smith   PetscFunctionReturn(0);
8341eb62cbbSBarry Smith }
8351eb62cbbSBarry Smith 
8364a2ae208SSatish Balay #undef __FUNCT__
8379c7c4993SBarry Smith #define __FUNCT__ "MatZeroRowsColumns_MPIAIJ"
8389c7c4993SBarry Smith PetscErrorCode MatZeroRowsColumns_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
8399c7c4993SBarry Smith {
8409c7c4993SBarry Smith   Mat_MPIAIJ        *l = (Mat_MPIAIJ*)A->data;
8419c7c4993SBarry Smith   PetscErrorCode    ierr;
8425ba17502SJed Brown   PetscMPIInt       n = A->rmap->n;
84378fab17bSMatthew G. Knepley   PetscInt          i,j,r,m,p = 0,len = 0;
84454bd4135SMatthew G. Knepley   PetscInt          *lrows,*owners = A->rmap->range;
84554bd4135SMatthew G. Knepley   PetscSFNode       *rrows;
84654bd4135SMatthew G. Knepley   PetscSF           sf;
8479c7c4993SBarry Smith   const PetscScalar *xx;
848564f14d6SBarry Smith   PetscScalar       *bb,*mask;
849564f14d6SBarry Smith   Vec               xmask,lmask;
850564f14d6SBarry Smith   Mat_SeqAIJ        *aij = (Mat_SeqAIJ*)l->B->data;
851564f14d6SBarry Smith   const PetscInt    *aj, *ii,*ridx;
852564f14d6SBarry Smith   PetscScalar       *aa;
8539c7c4993SBarry Smith 
8549c7c4993SBarry Smith   PetscFunctionBegin;
85554bd4135SMatthew G. Knepley   /* Create SF where leaves are input rows and roots are owned rows */
85654bd4135SMatthew G. Knepley   ierr = PetscMalloc1(n, &lrows);CHKERRQ(ierr);
85754bd4135SMatthew G. Knepley   for (r = 0; r < n; ++r) lrows[r] = -1;
85854bd4135SMatthew G. Knepley   ierr = PetscMalloc1(N, &rrows);CHKERRQ(ierr);
85954bd4135SMatthew G. Knepley   for (r = 0; r < N; ++r) {
86054bd4135SMatthew G. Knepley     const PetscInt idx   = rows[r];
8615ba17502SJed 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);
8625ba17502SJed Brown     if (idx < owners[p] || owners[p+1] <= idx) { /* short-circuit the search if the last p owns this row too */
8635ba17502SJed Brown       ierr = PetscLayoutFindOwner(A->rmap,idx,&p);CHKERRQ(ierr);
8645ba17502SJed Brown     }
86554bd4135SMatthew G. Knepley     rrows[r].rank  = p;
86654bd4135SMatthew G. Knepley     rrows[r].index = rows[r] - owners[p];
8679c7c4993SBarry Smith   }
86854bd4135SMatthew G. Knepley   ierr = PetscSFCreate(PetscObjectComm((PetscObject) A), &sf);CHKERRQ(ierr);
86954bd4135SMatthew G. Knepley   ierr = PetscSFSetGraph(sf, n, N, NULL, PETSC_OWN_POINTER, rrows, PETSC_OWN_POINTER);CHKERRQ(ierr);
87054bd4135SMatthew G. Knepley   /* Collect flags for rows to be zeroed */
87154bd4135SMatthew G. Knepley   ierr = PetscSFReduceBegin(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);CHKERRQ(ierr);
87254bd4135SMatthew G. Knepley   ierr = PetscSFReduceEnd(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);CHKERRQ(ierr);
87354bd4135SMatthew G. Knepley   ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
87454bd4135SMatthew G. Knepley   /* Compress and put in row numbers */
87554bd4135SMatthew G. Knepley   for (r = 0; r < n; ++r) if (lrows[r] >= 0) lrows[len++] = r;
876564f14d6SBarry Smith   /* zero diagonal part of matrix */
87754bd4135SMatthew G. Knepley   ierr = MatZeroRowsColumns(l->A,len,lrows,diag,x,b);CHKERRQ(ierr);
878564f14d6SBarry Smith   /* handle off diagonal part of matrix */
8790298fd71SBarry Smith   ierr = MatGetVecs(A,&xmask,NULL);CHKERRQ(ierr);
880564f14d6SBarry Smith   ierr = VecDuplicate(l->lvec,&lmask);CHKERRQ(ierr);
881564f14d6SBarry Smith   ierr = VecGetArray(xmask,&bb);CHKERRQ(ierr);
88254bd4135SMatthew G. Knepley   for (i=0; i<len; i++) bb[lrows[i]] = 1;
883564f14d6SBarry Smith   ierr = VecRestoreArray(xmask,&bb);CHKERRQ(ierr);
884564f14d6SBarry Smith   ierr = VecScatterBegin(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
885564f14d6SBarry Smith   ierr = VecScatterEnd(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
8866bf464f9SBarry Smith   ierr = VecDestroy(&xmask);CHKERRQ(ierr);
887377aa5a1SBarry Smith   if (x) {
88867caceb0SMatthew G. Knepley     ierr = VecScatterBegin(l->Mvctx,x,l->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
88967caceb0SMatthew G. Knepley     ierr = VecScatterEnd(l->Mvctx,x,l->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
890564f14d6SBarry Smith     ierr = VecGetArrayRead(l->lvec,&xx);CHKERRQ(ierr);
891564f14d6SBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
892377aa5a1SBarry Smith   }
893377aa5a1SBarry Smith   ierr = VecGetArray(lmask,&mask);CHKERRQ(ierr);
894564f14d6SBarry Smith   /* remove zeroed rows of off diagonal matrix */
895564f14d6SBarry Smith   ii = aij->i;
89654bd4135SMatthew G. Knepley   for (i=0; i<len; i++) {
897564f14d6SBarry Smith     ierr = PetscMemzero(aij->a + ii[lrows[i]],(ii[lrows[i]+1] - ii[lrows[i]])*sizeof(PetscScalar));CHKERRQ(ierr);
8989c7c4993SBarry Smith   }
899564f14d6SBarry Smith   /* loop over all elements of off process part of matrix zeroing removed columns*/
900564f14d6SBarry Smith   if (aij->compressedrow.use) {
901564f14d6SBarry Smith     m    = aij->compressedrow.nrows;
902564f14d6SBarry Smith     ii   = aij->compressedrow.i;
903564f14d6SBarry Smith     ridx = aij->compressedrow.rindex;
904564f14d6SBarry Smith     for (i=0; i<m; i++) {
905564f14d6SBarry Smith       n  = ii[i+1] - ii[i];
906564f14d6SBarry Smith       aj = aij->j + ii[i];
907564f14d6SBarry Smith       aa = aij->a + ii[i];
908564f14d6SBarry Smith 
909564f14d6SBarry Smith       for (j=0; j<n; j++) {
91025266a92SSatish Balay         if (PetscAbsScalar(mask[*aj])) {
911377aa5a1SBarry Smith           if (b) bb[*ridx] -= *aa*xx[*aj];
912564f14d6SBarry Smith           *aa = 0.0;
913564f14d6SBarry Smith         }
914564f14d6SBarry Smith         aa++;
915564f14d6SBarry Smith         aj++;
916564f14d6SBarry Smith       }
917564f14d6SBarry Smith       ridx++;
918564f14d6SBarry Smith     }
919564f14d6SBarry Smith   } else { /* do not use compressed row format */
920564f14d6SBarry Smith     m = l->B->rmap->n;
921564f14d6SBarry Smith     for (i=0; i<m; i++) {
922564f14d6SBarry Smith       n  = ii[i+1] - ii[i];
923564f14d6SBarry Smith       aj = aij->j + ii[i];
924564f14d6SBarry Smith       aa = aij->a + ii[i];
925564f14d6SBarry Smith       for (j=0; j<n; j++) {
92625266a92SSatish Balay         if (PetscAbsScalar(mask[*aj])) {
927377aa5a1SBarry Smith           if (b) bb[i] -= *aa*xx[*aj];
928564f14d6SBarry Smith           *aa = 0.0;
929564f14d6SBarry Smith         }
930564f14d6SBarry Smith         aa++;
931564f14d6SBarry Smith         aj++;
932564f14d6SBarry Smith       }
933564f14d6SBarry Smith     }
934564f14d6SBarry Smith   }
935377aa5a1SBarry Smith   if (x) {
936564f14d6SBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
937564f14d6SBarry Smith     ierr = VecRestoreArrayRead(l->lvec,&xx);CHKERRQ(ierr);
938377aa5a1SBarry Smith   }
939377aa5a1SBarry Smith   ierr = VecRestoreArray(lmask,&mask);CHKERRQ(ierr);
9406bf464f9SBarry Smith   ierr = VecDestroy(&lmask);CHKERRQ(ierr);
9419c7c4993SBarry Smith   ierr = PetscFree(lrows);CHKERRQ(ierr);
9424f9cfa9eSBarry Smith 
9434f9cfa9eSBarry Smith   /* only change matrix nonzero state if pattern was allowed to be changed */
9444f9cfa9eSBarry Smith   if (!((Mat_SeqAIJ*)(l->A->data))->keepnonzeropattern) {
9454f9cfa9eSBarry Smith     PetscObjectState state = l->A->nonzerostate + l->B->nonzerostate;
9464f9cfa9eSBarry Smith     ierr = MPI_Allreduce(&state,&A->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)A));CHKERRQ(ierr);
9474f9cfa9eSBarry Smith   }
9489c7c4993SBarry Smith   PetscFunctionReturn(0);
9499c7c4993SBarry Smith }
9509c7c4993SBarry Smith 
9519c7c4993SBarry Smith #undef __FUNCT__
9524a2ae208SSatish Balay #define __FUNCT__ "MatMult_MPIAIJ"
953dfbe8321SBarry Smith PetscErrorCode MatMult_MPIAIJ(Mat A,Vec xx,Vec yy)
9541eb62cbbSBarry Smith {
955416022c9SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
956dfbe8321SBarry Smith   PetscErrorCode ierr;
957b1d57f15SBarry Smith   PetscInt       nt;
958416022c9SBarry Smith 
9593a40ed3dSBarry Smith   PetscFunctionBegin;
960a2ce50c7SBarry Smith   ierr = VecGetLocalSize(xx,&nt);CHKERRQ(ierr);
96165e19b50SBarry 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);
962ca9f406cSSatish Balay   ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
963f830108cSBarry Smith   ierr = (*a->A->ops->mult)(a->A,xx,yy);CHKERRQ(ierr);
964ca9f406cSSatish Balay   ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
965f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);CHKERRQ(ierr);
9663a40ed3dSBarry Smith   PetscFunctionReturn(0);
9671eb62cbbSBarry Smith }
9681eb62cbbSBarry Smith 
9694a2ae208SSatish Balay #undef __FUNCT__
970bd0c2dcbSBarry Smith #define __FUNCT__ "MatMultDiagonalBlock_MPIAIJ"
971bd0c2dcbSBarry Smith PetscErrorCode MatMultDiagonalBlock_MPIAIJ(Mat A,Vec bb,Vec xx)
972bd0c2dcbSBarry Smith {
973bd0c2dcbSBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
974bd0c2dcbSBarry Smith   PetscErrorCode ierr;
975bd0c2dcbSBarry Smith 
976bd0c2dcbSBarry Smith   PetscFunctionBegin;
977bd0c2dcbSBarry Smith   ierr = MatMultDiagonalBlock(a->A,bb,xx);CHKERRQ(ierr);
978bd0c2dcbSBarry Smith   PetscFunctionReturn(0);
979bd0c2dcbSBarry Smith }
980bd0c2dcbSBarry Smith 
981bd0c2dcbSBarry Smith #undef __FUNCT__
9824a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_MPIAIJ"
983dfbe8321SBarry Smith PetscErrorCode MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
984da3a660dSBarry Smith {
985416022c9SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
986dfbe8321SBarry Smith   PetscErrorCode ierr;
9873a40ed3dSBarry Smith 
9883a40ed3dSBarry Smith   PetscFunctionBegin;
989ca9f406cSSatish Balay   ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
990f830108cSBarry Smith   ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
991ca9f406cSSatish Balay   ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
992f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);CHKERRQ(ierr);
9933a40ed3dSBarry Smith   PetscFunctionReturn(0);
994da3a660dSBarry Smith }
995da3a660dSBarry Smith 
9964a2ae208SSatish Balay #undef __FUNCT__
9974a2ae208SSatish Balay #define __FUNCT__ "MatMultTranspose_MPIAIJ"
998dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_MPIAIJ(Mat A,Vec xx,Vec yy)
999da3a660dSBarry Smith {
1000416022c9SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
1001dfbe8321SBarry Smith   PetscErrorCode ierr;
1002ace3abfcSBarry Smith   PetscBool      merged;
1003da3a660dSBarry Smith 
10043a40ed3dSBarry Smith   PetscFunctionBegin;
1005a5ff213dSBarry Smith   ierr = VecScatterGetMerged(a->Mvctx,&merged);CHKERRQ(ierr);
1006da3a660dSBarry Smith   /* do nondiagonal part */
10077c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
1008a5ff213dSBarry Smith   if (!merged) {
1009da3a660dSBarry Smith     /* send it on its way */
1010ca9f406cSSatish Balay     ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1011da3a660dSBarry Smith     /* do local part */
10127c922b88SBarry Smith     ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr);
1013da3a660dSBarry Smith     /* receive remote parts: note this assumes the values are not actually */
1014a5ff213dSBarry Smith     /* added in yy until the next line, */
1015ca9f406cSSatish Balay     ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1016a5ff213dSBarry Smith   } else {
1017a5ff213dSBarry Smith     /* do local part */
1018a5ff213dSBarry Smith     ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr);
1019a5ff213dSBarry Smith     /* send it on its way */
1020ca9f406cSSatish Balay     ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1021a5ff213dSBarry Smith     /* values actually were received in the Begin() but we need to call this nop */
1022ca9f406cSSatish Balay     ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1023a5ff213dSBarry Smith   }
10243a40ed3dSBarry Smith   PetscFunctionReturn(0);
1025da3a660dSBarry Smith }
1026da3a660dSBarry Smith 
1027cd0d46ebSvictorle #undef __FUNCT__
10285fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_MPIAIJ"
10297087cfbeSBarry Smith PetscErrorCode  MatIsTranspose_MPIAIJ(Mat Amat,Mat Bmat,PetscReal tol,PetscBool  *f)
1030cd0d46ebSvictorle {
10314f423910Svictorle   MPI_Comm       comm;
1032cd0d46ebSvictorle   Mat_MPIAIJ     *Aij = (Mat_MPIAIJ*) Amat->data, *Bij;
103366501d38Svictorle   Mat            Adia = Aij->A, Bdia, Aoff,Boff,*Aoffs,*Boffs;
1034cd0d46ebSvictorle   IS             Me,Notme;
10356849ba73SBarry Smith   PetscErrorCode ierr;
1036b1d57f15SBarry Smith   PetscInt       M,N,first,last,*notme,i;
1037b1d57f15SBarry Smith   PetscMPIInt    size;
1038cd0d46ebSvictorle 
1039cd0d46ebSvictorle   PetscFunctionBegin;
104042e5f5b4Svictorle   /* Easy test: symmetric diagonal block */
104166501d38Svictorle   Bij  = (Mat_MPIAIJ*) Bmat->data; Bdia = Bij->A;
10425485867bSBarry Smith   ierr = MatIsTranspose(Adia,Bdia,tol,f);CHKERRQ(ierr);
1043cd0d46ebSvictorle   if (!*f) PetscFunctionReturn(0);
10444f423910Svictorle   ierr = PetscObjectGetComm((PetscObject)Amat,&comm);CHKERRQ(ierr);
1045b1d57f15SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
1046b1d57f15SBarry Smith   if (size == 1) PetscFunctionReturn(0);
104742e5f5b4Svictorle 
104842e5f5b4Svictorle   /* Hard test: off-diagonal block. This takes a MatGetSubMatrix. */
1049cd0d46ebSvictorle   ierr = MatGetSize(Amat,&M,&N);CHKERRQ(ierr);
1050cd0d46ebSvictorle   ierr = MatGetOwnershipRange(Amat,&first,&last);CHKERRQ(ierr);
1051785e854fSJed Brown   ierr = PetscMalloc1((N-last+first),&notme);CHKERRQ(ierr);
1052cd0d46ebSvictorle   for (i=0; i<first; i++) notme[i] = i;
1053cd0d46ebSvictorle   for (i=last; i<M; i++) notme[i-last+first] = i;
105470b3c8c7SBarry Smith   ierr = ISCreateGeneral(MPI_COMM_SELF,N-last+first,notme,PETSC_COPY_VALUES,&Notme);CHKERRQ(ierr);
1055268466fbSBarry Smith   ierr = ISCreateStride(MPI_COMM_SELF,last-first,first,1,&Me);CHKERRQ(ierr);
1056268466fbSBarry Smith   ierr = MatGetSubMatrices(Amat,1,&Me,&Notme,MAT_INITIAL_MATRIX,&Aoffs);CHKERRQ(ierr);
105766501d38Svictorle   Aoff = Aoffs[0];
1058268466fbSBarry Smith   ierr = MatGetSubMatrices(Bmat,1,&Notme,&Me,MAT_INITIAL_MATRIX,&Boffs);CHKERRQ(ierr);
105966501d38Svictorle   Boff = Boffs[0];
10605485867bSBarry Smith   ierr = MatIsTranspose(Aoff,Boff,tol,f);CHKERRQ(ierr);
106166501d38Svictorle   ierr = MatDestroyMatrices(1,&Aoffs);CHKERRQ(ierr);
106266501d38Svictorle   ierr = MatDestroyMatrices(1,&Boffs);CHKERRQ(ierr);
10636bf464f9SBarry Smith   ierr = ISDestroy(&Me);CHKERRQ(ierr);
10646bf464f9SBarry Smith   ierr = ISDestroy(&Notme);CHKERRQ(ierr);
10653e0d0d19SHong Zhang   ierr = PetscFree(notme);CHKERRQ(ierr);
1066cd0d46ebSvictorle   PetscFunctionReturn(0);
1067cd0d46ebSvictorle }
1068cd0d46ebSvictorle 
10694a2ae208SSatish Balay #undef __FUNCT__
10704a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_MPIAIJ"
1071dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1072da3a660dSBarry Smith {
1073416022c9SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
1074dfbe8321SBarry Smith   PetscErrorCode ierr;
1075da3a660dSBarry Smith 
10763a40ed3dSBarry Smith   PetscFunctionBegin;
1077da3a660dSBarry Smith   /* do nondiagonal part */
10787c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
1079da3a660dSBarry Smith   /* send it on its way */
1080ca9f406cSSatish Balay   ierr = VecScatterBegin(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1081da3a660dSBarry Smith   /* do local part */
10827c922b88SBarry Smith   ierr = (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
1083a5ff213dSBarry Smith   /* receive remote parts */
1084ca9f406cSSatish Balay   ierr = VecScatterEnd(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
10853a40ed3dSBarry Smith   PetscFunctionReturn(0);
1086da3a660dSBarry Smith }
1087da3a660dSBarry Smith 
10881eb62cbbSBarry Smith /*
10891eb62cbbSBarry Smith   This only works correctly for square matrices where the subblock A->A is the
10901eb62cbbSBarry Smith    diagonal block
10911eb62cbbSBarry Smith */
10924a2ae208SSatish Balay #undef __FUNCT__
10934a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_MPIAIJ"
1094dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_MPIAIJ(Mat A,Vec v)
10951eb62cbbSBarry Smith {
1096dfbe8321SBarry Smith   PetscErrorCode ierr;
1097416022c9SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
10983a40ed3dSBarry Smith 
10993a40ed3dSBarry Smith   PetscFunctionBegin;
1100ce94432eSBarry 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");
1101e7e72b3dSBarry 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");
11023a40ed3dSBarry Smith   ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr);
11033a40ed3dSBarry Smith   PetscFunctionReturn(0);
11041eb62cbbSBarry Smith }
11051eb62cbbSBarry Smith 
11064a2ae208SSatish Balay #undef __FUNCT__
11074a2ae208SSatish Balay #define __FUNCT__ "MatScale_MPIAIJ"
1108f4df32b1SMatthew Knepley PetscErrorCode MatScale_MPIAIJ(Mat A,PetscScalar aa)
1109052efed2SBarry Smith {
1110052efed2SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
1111dfbe8321SBarry Smith   PetscErrorCode ierr;
11123a40ed3dSBarry Smith 
11133a40ed3dSBarry Smith   PetscFunctionBegin;
1114f4df32b1SMatthew Knepley   ierr = MatScale(a->A,aa);CHKERRQ(ierr);
1115f4df32b1SMatthew Knepley   ierr = MatScale(a->B,aa);CHKERRQ(ierr);
11163a40ed3dSBarry Smith   PetscFunctionReturn(0);
1117052efed2SBarry Smith }
1118052efed2SBarry Smith 
11194a2ae208SSatish Balay #undef __FUNCT__
1120a3ca3016SBarry Smith #define __FUNCT__ "MatDestroy_Redundant"
1121a3ca3016SBarry Smith PetscErrorCode MatDestroy_Redundant(Mat_Redundant **redundant)
11224472c490SBarry Smith {
11234472c490SBarry Smith   PetscErrorCode ierr;
1124a3ca3016SBarry Smith   Mat_Redundant  *redund = *redundant;
11254472c490SBarry Smith   PetscInt       i;
11264472c490SBarry Smith 
11274472c490SBarry Smith   PetscFunctionBegin;
1128a3ca3016SBarry Smith   *redundant = NULL;
11294472c490SBarry Smith   if (redund){
11304472c490SBarry Smith     if (redund->matseq) { /* via MatGetSubMatrices()  */
11314472c490SBarry Smith       ierr = ISDestroy(&redund->isrow);CHKERRQ(ierr);
11324472c490SBarry Smith       ierr = ISDestroy(&redund->iscol);CHKERRQ(ierr);
11334472c490SBarry Smith       ierr = MatDestroy(&redund->matseq[0]);CHKERRQ(ierr);
11344472c490SBarry Smith       ierr = PetscFree(redund->matseq);CHKERRQ(ierr);
11354472c490SBarry Smith     } else {
11364472c490SBarry Smith       ierr = PetscFree2(redund->send_rank,redund->recv_rank);CHKERRQ(ierr);
11374472c490SBarry Smith       ierr = PetscFree(redund->sbuf_j);CHKERRQ(ierr);
11384472c490SBarry Smith       ierr = PetscFree(redund->sbuf_a);CHKERRQ(ierr);
11394472c490SBarry Smith       for (i=0; i<redund->nrecvs; i++) {
11404472c490SBarry Smith         ierr = PetscFree(redund->rbuf_j[i]);CHKERRQ(ierr);
11414472c490SBarry Smith         ierr = PetscFree(redund->rbuf_a[i]);CHKERRQ(ierr);
11424472c490SBarry Smith       }
11434472c490SBarry Smith       ierr = PetscFree4(redund->sbuf_nz,redund->rbuf_nz,redund->rbuf_j,redund->rbuf_a);CHKERRQ(ierr);
11444472c490SBarry Smith     }
11454472c490SBarry Smith 
11464472c490SBarry Smith     if (redund->psubcomm) {
11474472c490SBarry Smith       ierr = PetscSubcommDestroy(&redund->psubcomm);CHKERRQ(ierr);
11484472c490SBarry Smith     }
11494472c490SBarry Smith     ierr = PetscFree(redund);CHKERRQ(ierr);
11504472c490SBarry Smith   }
11514472c490SBarry Smith   PetscFunctionReturn(0);
11524472c490SBarry Smith }
11534472c490SBarry Smith 
11544472c490SBarry Smith #undef __FUNCT__
11554a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_MPIAIJ"
1156dfbe8321SBarry Smith PetscErrorCode MatDestroy_MPIAIJ(Mat mat)
11571eb62cbbSBarry Smith {
115844a69424SLois Curfman McInnes   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
1159dfbe8321SBarry Smith   PetscErrorCode ierr;
116083e2fdc7SBarry Smith 
11613a40ed3dSBarry Smith   PetscFunctionBegin;
1162aa482453SBarry Smith #if defined(PETSC_USE_LOG)
1163d0f46423SBarry Smith   PetscLogObjectState((PetscObject)mat,"Rows=%D, Cols=%D",mat->rmap->N,mat->cmap->N);
1164a5a9c739SBarry Smith #endif
1165a3ca3016SBarry Smith   ierr = MatDestroy_Redundant(&aij->redundant);CHKERRQ(ierr);
11668798bf22SSatish Balay   ierr = MatStashDestroy_Private(&mat->stash);CHKERRQ(ierr);
11676bf464f9SBarry Smith   ierr = VecDestroy(&aij->diag);CHKERRQ(ierr);
11686bf464f9SBarry Smith   ierr = MatDestroy(&aij->A);CHKERRQ(ierr);
11696bf464f9SBarry Smith   ierr = MatDestroy(&aij->B);CHKERRQ(ierr);
1170aa482453SBarry Smith #if defined(PETSC_USE_CTABLE)
11716bc0bbbfSBarry Smith   ierr = PetscTableDestroy(&aij->colmap);CHKERRQ(ierr);
1172b1fc9764SSatish Balay #else
117305b42c5fSBarry Smith   ierr = PetscFree(aij->colmap);CHKERRQ(ierr);
1174b1fc9764SSatish Balay #endif
117505b42c5fSBarry Smith   ierr = PetscFree(aij->garray);CHKERRQ(ierr);
11766bf464f9SBarry Smith   ierr = VecDestroy(&aij->lvec);CHKERRQ(ierr);
11776bf464f9SBarry Smith   ierr = VecScatterDestroy(&aij->Mvctx);CHKERRQ(ierr);
117803095fedSBarry Smith   ierr = PetscFree2(aij->rowvalues,aij->rowindices);CHKERRQ(ierr);
11798aa348c1SBarry Smith   ierr = PetscFree(aij->ld);CHKERRQ(ierr);
1180bf0cc555SLisandro Dalcin   ierr = PetscFree(mat->data);CHKERRQ(ierr);
1181901853e0SKris Buschelman 
1182dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)mat,0);CHKERRQ(ierr);
1183bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C",NULL);CHKERRQ(ierr);
1184bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C",NULL);CHKERRQ(ierr);
1185bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatGetDiagonalBlock_C",NULL);CHKERRQ(ierr);
1186bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatIsTranspose_C",NULL);CHKERRQ(ierr);
1187bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocation_C",NULL);CHKERRQ(ierr);
1188bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocationCSR_C",NULL);CHKERRQ(ierr);
1189bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatDiagonalScaleLocal_C",NULL);CHKERRQ(ierr);
1190bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_mpisbaij_C",NULL);CHKERRQ(ierr);
1191*5d7652ecSHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
1192*5d7652ecSHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_elemental_C",NULL);CHKERRQ(ierr);
1193*5d7652ecSHong Zhang #endif
11943a40ed3dSBarry Smith   PetscFunctionReturn(0);
11951eb62cbbSBarry Smith }
1196ee50ffe9SBarry Smith 
11974a2ae208SSatish Balay #undef __FUNCT__
11988e2fed03SBarry Smith #define __FUNCT__ "MatView_MPIAIJ_Binary"
1199dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ_Binary(Mat mat,PetscViewer viewer)
12008e2fed03SBarry Smith {
12018e2fed03SBarry Smith   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
12028e2fed03SBarry Smith   Mat_SeqAIJ     *A   = (Mat_SeqAIJ*)aij->A->data;
12038e2fed03SBarry Smith   Mat_SeqAIJ     *B   = (Mat_SeqAIJ*)aij->B->data;
12046849ba73SBarry Smith   PetscErrorCode ierr;
120532dcc486SBarry Smith   PetscMPIInt    rank,size,tag = ((PetscObject)viewer)->tag;
12066f69ff64SBarry Smith   int            fd;
1207a788621eSSatish Balay   PetscInt       nz,header[4],*row_lengths,*range=0,rlen,i;
1208d892089bSMatthew G. Knepley   PetscInt       nzmax,*column_indices,j,k,col,*garray = aij->garray,cnt,cstart = mat->cmap->rstart,rnz = 0;
12098e2fed03SBarry Smith   PetscScalar    *column_values;
121085ebf7a4SBarry Smith   PetscInt       message_count,flowcontrolcount;
1211b37d52dbSMark F. Adams   FILE           *file;
12128e2fed03SBarry Smith 
12138e2fed03SBarry Smith   PetscFunctionBegin;
1214ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&rank);CHKERRQ(ierr);
1215ce94432eSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRQ(ierr);
12168e2fed03SBarry Smith   nz   = A->nz + B->nz;
1217958c9bccSBarry Smith   if (!rank) {
12180700a824SBarry Smith     header[0] = MAT_FILE_CLASSID;
1219d0f46423SBarry Smith     header[1] = mat->rmap->N;
1220d0f46423SBarry Smith     header[2] = mat->cmap->N;
12212205254eSKarl Rupp 
1222ce94432eSBarry Smith     ierr = MPI_Reduce(&nz,&header[3],1,MPIU_INT,MPI_SUM,0,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
12238e2fed03SBarry Smith     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
12246f69ff64SBarry Smith     ierr = PetscBinaryWrite(fd,header,4,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
12258e2fed03SBarry Smith     /* get largest number of rows any processor has */
1226d0f46423SBarry Smith     rlen  = mat->rmap->n;
1227d0f46423SBarry Smith     range = mat->rmap->range;
12282205254eSKarl Rupp     for (i=1; i<size; i++) rlen = PetscMax(rlen,range[i+1] - range[i]);
12298e2fed03SBarry Smith   } else {
1230ce94432eSBarry Smith     ierr = MPI_Reduce(&nz,0,1,MPIU_INT,MPI_SUM,0,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1231d0f46423SBarry Smith     rlen = mat->rmap->n;
12328e2fed03SBarry Smith   }
12338e2fed03SBarry Smith 
12348e2fed03SBarry Smith   /* load up the local row counts */
1235785e854fSJed Brown   ierr = PetscMalloc1((rlen+1),&row_lengths);CHKERRQ(ierr);
12362205254eSKarl 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];
12378e2fed03SBarry Smith 
12388e2fed03SBarry Smith   /* store the row lengths to the file */
123985ebf7a4SBarry Smith   ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr);
1240958c9bccSBarry Smith   if (!rank) {
1241d0f46423SBarry Smith     ierr = PetscBinaryWrite(fd,row_lengths,mat->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
12428e2fed03SBarry Smith     for (i=1; i<size; i++) {
1243639ff905SBarry Smith       ierr = PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);CHKERRQ(ierr);
12448e2fed03SBarry Smith       rlen = range[i+1] - range[i];
1245ce94432eSBarry Smith       ierr = MPIULong_Recv(row_lengths,rlen,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
12466f69ff64SBarry Smith       ierr = PetscBinaryWrite(fd,row_lengths,rlen,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
12478e2fed03SBarry Smith     }
1248639ff905SBarry Smith     ierr = PetscViewerFlowControlEndMaster(viewer,&message_count);CHKERRQ(ierr);
12498e2fed03SBarry Smith   } else {
1250639ff905SBarry Smith     ierr = PetscViewerFlowControlStepWorker(viewer,rank,&message_count);CHKERRQ(ierr);
1251ce94432eSBarry Smith     ierr = MPIULong_Send(row_lengths,mat->rmap->n,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1252639ff905SBarry Smith     ierr = PetscViewerFlowControlEndWorker(viewer,&message_count);CHKERRQ(ierr);
12538e2fed03SBarry Smith   }
12548e2fed03SBarry Smith   ierr = PetscFree(row_lengths);CHKERRQ(ierr);
12558e2fed03SBarry Smith 
12568e2fed03SBarry Smith   /* load up the local column indices */
12571147fc2aSKarl Rupp   nzmax = nz; /* th processor needs space a largest processor needs */
1258ce94432eSBarry Smith   ierr  = MPI_Reduce(&nz,&nzmax,1,MPIU_INT,MPI_MAX,0,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1259785e854fSJed Brown   ierr  = PetscMalloc1((nzmax+1),&column_indices);CHKERRQ(ierr);
12608e2fed03SBarry Smith   cnt   = 0;
1261d0f46423SBarry Smith   for (i=0; i<mat->rmap->n; i++) {
12628e2fed03SBarry Smith     for (j=B->i[i]; j<B->i[i+1]; j++) {
12638e2fed03SBarry Smith       if ((col = garray[B->j[j]]) > cstart) break;
12648e2fed03SBarry Smith       column_indices[cnt++] = col;
12658e2fed03SBarry Smith     }
12662205254eSKarl Rupp     for (k=A->i[i]; k<A->i[i+1]; k++) column_indices[cnt++] = A->j[k] + cstart;
12672205254eSKarl Rupp     for (; j<B->i[i+1]; j++) column_indices[cnt++] = garray[B->j[j]];
12688e2fed03SBarry Smith   }
1269e32f2f54SBarry 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);
12708e2fed03SBarry Smith 
12718e2fed03SBarry Smith   /* store the column indices to the file */
127285ebf7a4SBarry Smith   ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr);
1273958c9bccSBarry Smith   if (!rank) {
12748e2fed03SBarry Smith     MPI_Status status;
12756f69ff64SBarry Smith     ierr = PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
12768e2fed03SBarry Smith     for (i=1; i<size; i++) {
1277639ff905SBarry Smith       ierr = PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);CHKERRQ(ierr);
1278ce94432eSBarry Smith       ierr = MPI_Recv(&rnz,1,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);CHKERRQ(ierr);
1279e32f2f54SBarry Smith       if (rnz > nzmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax);
1280ce94432eSBarry Smith       ierr = MPIULong_Recv(column_indices,rnz,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
12816f69ff64SBarry Smith       ierr = PetscBinaryWrite(fd,column_indices,rnz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
12828e2fed03SBarry Smith     }
1283639ff905SBarry Smith     ierr = PetscViewerFlowControlEndMaster(viewer,&message_count);CHKERRQ(ierr);
12848e2fed03SBarry Smith   } else {
1285639ff905SBarry Smith     ierr = PetscViewerFlowControlStepWorker(viewer,rank,&message_count);CHKERRQ(ierr);
1286ce94432eSBarry Smith     ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1287ce94432eSBarry Smith     ierr = MPIULong_Send(column_indices,nz,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1288639ff905SBarry Smith     ierr = PetscViewerFlowControlEndWorker(viewer,&message_count);CHKERRQ(ierr);
12898e2fed03SBarry Smith   }
12908e2fed03SBarry Smith   ierr = PetscFree(column_indices);CHKERRQ(ierr);
12918e2fed03SBarry Smith 
12928e2fed03SBarry Smith   /* load up the local column values */
1293785e854fSJed Brown   ierr = PetscMalloc1((nzmax+1),&column_values);CHKERRQ(ierr);
12948e2fed03SBarry Smith   cnt  = 0;
1295d0f46423SBarry Smith   for (i=0; i<mat->rmap->n; i++) {
12968e2fed03SBarry Smith     for (j=B->i[i]; j<B->i[i+1]; j++) {
12978e2fed03SBarry Smith       if (garray[B->j[j]] > cstart) break;
12988e2fed03SBarry Smith       column_values[cnt++] = B->a[j];
12998e2fed03SBarry Smith     }
13002205254eSKarl Rupp     for (k=A->i[i]; k<A->i[i+1]; k++) column_values[cnt++] = A->a[k];
13012205254eSKarl Rupp     for (; j<B->i[i+1]; j++) column_values[cnt++] = B->a[j];
13028e2fed03SBarry Smith   }
1303e32f2f54SBarry 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);
13048e2fed03SBarry Smith 
13058e2fed03SBarry Smith   /* store the column values to the file */
130685ebf7a4SBarry Smith   ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr);
1307958c9bccSBarry Smith   if (!rank) {
13088e2fed03SBarry Smith     MPI_Status status;
13096f69ff64SBarry Smith     ierr = PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr);
13108e2fed03SBarry Smith     for (i=1; i<size; i++) {
1311639ff905SBarry Smith       ierr = PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);CHKERRQ(ierr);
1312ce94432eSBarry Smith       ierr = MPI_Recv(&rnz,1,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);CHKERRQ(ierr);
1313e32f2f54SBarry Smith       if (rnz > nzmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax);
1314ce94432eSBarry Smith       ierr = MPIULong_Recv(column_values,rnz,MPIU_SCALAR,i,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
13156f69ff64SBarry Smith       ierr = PetscBinaryWrite(fd,column_values,rnz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr);
13168e2fed03SBarry Smith     }
1317639ff905SBarry Smith     ierr = PetscViewerFlowControlEndMaster(viewer,&message_count);CHKERRQ(ierr);
13188e2fed03SBarry Smith   } else {
1319639ff905SBarry Smith     ierr = PetscViewerFlowControlStepWorker(viewer,rank,&message_count);CHKERRQ(ierr);
1320ce94432eSBarry Smith     ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1321ce94432eSBarry Smith     ierr = MPIULong_Send(column_values,nz,MPIU_SCALAR,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1322639ff905SBarry Smith     ierr = PetscViewerFlowControlEndWorker(viewer,&message_count);CHKERRQ(ierr);
13238e2fed03SBarry Smith   }
13248e2fed03SBarry Smith   ierr = PetscFree(column_values);CHKERRQ(ierr);
1325b37d52dbSMark F. Adams 
1326b37d52dbSMark F. Adams   ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr);
132733d57670SJed Brown   if (file) fprintf(file,"-matload_block_size %d\n",(int)PetscAbs(mat->rmap->bs));
13288e2fed03SBarry Smith   PetscFunctionReturn(0);
13298e2fed03SBarry Smith }
13308e2fed03SBarry Smith 
13319804daf3SBarry Smith #include <petscdraw.h>
13328e2fed03SBarry Smith #undef __FUNCT__
13334a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ_ASCIIorDraworSocket"
1334dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
1335416022c9SBarry Smith {
133644a69424SLois Curfman McInnes   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*)mat->data;
1337dfbe8321SBarry Smith   PetscErrorCode    ierr;
133832dcc486SBarry Smith   PetscMPIInt       rank = aij->rank,size = aij->size;
1339ace3abfcSBarry Smith   PetscBool         isdraw,iascii,isbinary;
1340b0a32e0cSBarry Smith   PetscViewer       sviewer;
1341f3ef73ceSBarry Smith   PetscViewerFormat format;
1342416022c9SBarry Smith 
13433a40ed3dSBarry Smith   PetscFunctionBegin;
1344251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1345251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1346251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
134732077d6dSBarry Smith   if (iascii) {
1348b0a32e0cSBarry Smith     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
1349456192e2SBarry Smith     if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
13504e220ebcSLois Curfman McInnes       MatInfo   info;
1351ace3abfcSBarry Smith       PetscBool inodes;
1352923f20ffSKris Buschelman 
1353ce94432eSBarry Smith       ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&rank);CHKERRQ(ierr);
1354888f2ed8SSatish Balay       ierr = MatGetInfo(mat,MAT_LOCAL,&info);CHKERRQ(ierr);
13550298fd71SBarry Smith       ierr = MatInodeGetInodeSizes(aij->A,NULL,(PetscInt**)&inodes,NULL);CHKERRQ(ierr);
13567b23a99aSBarry Smith       ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_TRUE);CHKERRQ(ierr);
1357923f20ffSKris Buschelman       if (!inodes) {
135877431f27SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, not using I-node routines\n",
1359d0f46423SBarry Smith                                                   rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);CHKERRQ(ierr);
13606831982aSBarry Smith       } else {
136177431f27SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, using I-node routines\n",
1362d0f46423SBarry Smith                                                   rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);CHKERRQ(ierr);
13636831982aSBarry Smith       }
1364888f2ed8SSatish Balay       ierr = MatGetInfo(aij->A,MAT_LOCAL,&info);CHKERRQ(ierr);
136577431f27SBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr);
1366888f2ed8SSatish Balay       ierr = MatGetInfo(aij->B,MAT_LOCAL,&info);CHKERRQ(ierr);
136777431f27SBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr);
1368b0a32e0cSBarry Smith       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
13697b23a99aSBarry Smith       ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_FALSE);CHKERRQ(ierr);
137007d81ca4SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Information on VecScatter used in matrix-vector product: \n");CHKERRQ(ierr);
1371a40aa06bSLois Curfman McInnes       ierr = VecScatterView(aij->Mvctx,viewer);CHKERRQ(ierr);
13723a40ed3dSBarry Smith       PetscFunctionReturn(0);
1373fb9695e5SSatish Balay     } else if (format == PETSC_VIEWER_ASCII_INFO) {
1374923f20ffSKris Buschelman       PetscInt inodecount,inodelimit,*inodes;
1375923f20ffSKris Buschelman       ierr = MatInodeGetInodeSizes(aij->A,&inodecount,&inodes,&inodelimit);CHKERRQ(ierr);
1376923f20ffSKris Buschelman       if (inodes) {
1377923f20ffSKris Buschelman         ierr = PetscViewerASCIIPrintf(viewer,"using I-node (on process 0) routines: found %D nodes, limit used is %D\n",inodecount,inodelimit);CHKERRQ(ierr);
1378d38fa0fbSBarry Smith       } else {
1379d38fa0fbSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"not using I-node (on process 0) routines\n");CHKERRQ(ierr);
1380d38fa0fbSBarry Smith       }
13813a40ed3dSBarry Smith       PetscFunctionReturn(0);
13824aedb280SBarry Smith     } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
13834aedb280SBarry Smith       PetscFunctionReturn(0);
138408480c60SBarry Smith     }
13858e2fed03SBarry Smith   } else if (isbinary) {
13868e2fed03SBarry Smith     if (size == 1) {
13877adad957SLisandro Dalcin       ierr = PetscObjectSetName((PetscObject)aij->A,((PetscObject)mat)->name);CHKERRQ(ierr);
13888e2fed03SBarry Smith       ierr = MatView(aij->A,viewer);CHKERRQ(ierr);
13898e2fed03SBarry Smith     } else {
13908e2fed03SBarry Smith       ierr = MatView_MPIAIJ_Binary(mat,viewer);CHKERRQ(ierr);
13918e2fed03SBarry Smith     }
13928e2fed03SBarry Smith     PetscFunctionReturn(0);
13930f5bd95cSBarry Smith   } else if (isdraw) {
1394b0a32e0cSBarry Smith     PetscDraw draw;
1395ace3abfcSBarry Smith     PetscBool isnull;
1396b0a32e0cSBarry Smith     ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
1397b0a32e0cSBarry Smith     ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
139819bcc07fSBarry Smith   }
139919bcc07fSBarry Smith 
14007da1fb6eSBarry Smith   {
140195373324SBarry Smith     /* assemble the entire matrix onto first processor. */
140295373324SBarry Smith     Mat        A;
1403ec8511deSBarry Smith     Mat_SeqAIJ *Aloc;
1404d0f46423SBarry Smith     PetscInt   M = mat->rmap->N,N = mat->cmap->N,m,*ai,*aj,row,*cols,i,*ct;
1405dd6ea824SBarry Smith     MatScalar  *a;
14062ee70a88SLois Curfman McInnes 
1407ce94432eSBarry Smith     ierr = MatCreate(PetscObjectComm((PetscObject)mat),&A);CHKERRQ(ierr);
140817699dbbSLois Curfman McInnes     if (!rank) {
1409f69a0ea3SMatthew Knepley       ierr = MatSetSizes(A,M,N,M,N);CHKERRQ(ierr);
14103a40ed3dSBarry Smith     } else {
1411f69a0ea3SMatthew Knepley       ierr = MatSetSizes(A,0,0,M,N);CHKERRQ(ierr);
141295373324SBarry Smith     }
1413f204ca49SKris Buschelman     /* This is just a temporary matrix, so explicitly using MATMPIAIJ is probably best */
1414f204ca49SKris Buschelman     ierr = MatSetType(A,MATMPIAIJ);CHKERRQ(ierr);
14150298fd71SBarry Smith     ierr = MatMPIAIJSetPreallocation(A,0,NULL,0,NULL);CHKERRQ(ierr);
14162b82e772SSatish Balay     ierr = MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr);
14173bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)mat,(PetscObject)A);CHKERRQ(ierr);
1418416022c9SBarry Smith 
141995373324SBarry Smith     /* copy over the A part */
1420ec8511deSBarry Smith     Aloc = (Mat_SeqAIJ*)aij->A->data;
1421d0f46423SBarry Smith     m    = aij->A->rmap->n; ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
1422d0f46423SBarry Smith     row  = mat->rmap->rstart;
14232205254eSKarl Rupp     for (i=0; i<ai[m]; i++) aj[i] += mat->cmap->rstart;
142495373324SBarry Smith     for (i=0; i<m; i++) {
1425416022c9SBarry Smith       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);CHKERRQ(ierr);
142626fbe8dcSKarl Rupp       row++;
142726fbe8dcSKarl Rupp       a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
142895373324SBarry Smith     }
14292ee70a88SLois Curfman McInnes     aj = Aloc->j;
14302205254eSKarl Rupp     for (i=0; i<ai[m]; i++) aj[i] -= mat->cmap->rstart;
143195373324SBarry Smith 
143295373324SBarry Smith     /* copy over the B part */
1433ec8511deSBarry Smith     Aloc = (Mat_SeqAIJ*)aij->B->data;
1434d0f46423SBarry Smith     m    = aij->B->rmap->n;  ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
1435d0f46423SBarry Smith     row  = mat->rmap->rstart;
1436785e854fSJed Brown     ierr = PetscMalloc1((ai[m]+1),&cols);CHKERRQ(ierr);
1437b0a32e0cSBarry Smith     ct   = cols;
14382205254eSKarl Rupp     for (i=0; i<ai[m]; i++) cols[i] = aij->garray[aj[i]];
143995373324SBarry Smith     for (i=0; i<m; i++) {
1440416022c9SBarry Smith       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);CHKERRQ(ierr);
14412205254eSKarl Rupp       row++;
14422205254eSKarl Rupp       a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
144395373324SBarry Smith     }
1444606d414cSSatish Balay     ierr = PetscFree(ct);CHKERRQ(ierr);
14456d4a8577SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14466d4a8577SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
144755843e3eSBarry Smith     /*
144855843e3eSBarry Smith        Everyone has to call to draw the matrix since the graphics waits are
1449b0a32e0cSBarry Smith        synchronized across all processors that share the PetscDraw object
145055843e3eSBarry Smith     */
1451b0a32e0cSBarry Smith     ierr = PetscViewerGetSingleton(viewer,&sviewer);CHKERRQ(ierr);
1452e03a110bSBarry Smith     if (!rank) {
14537da1fb6eSBarry Smith       ierr = MatView_SeqAIJ(((Mat_MPIAIJ*)(A->data))->A,sviewer);CHKERRQ(ierr);
145495373324SBarry Smith     }
1455b0a32e0cSBarry Smith     ierr = PetscViewerRestoreSingleton(viewer,&sviewer);CHKERRQ(ierr);
14566bf464f9SBarry Smith     ierr = MatDestroy(&A);CHKERRQ(ierr);
145795373324SBarry Smith   }
14583a40ed3dSBarry Smith   PetscFunctionReturn(0);
14591eb62cbbSBarry Smith }
14601eb62cbbSBarry Smith 
14614a2ae208SSatish Balay #undef __FUNCT__
14624a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ"
1463dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ(Mat mat,PetscViewer viewer)
1464416022c9SBarry Smith {
1465dfbe8321SBarry Smith   PetscErrorCode ierr;
1466ace3abfcSBarry Smith   PetscBool      iascii,isdraw,issocket,isbinary;
1467416022c9SBarry Smith 
14683a40ed3dSBarry Smith   PetscFunctionBegin;
1469251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1470251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1471251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
1472251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSOCKET,&issocket);CHKERRQ(ierr);
147332077d6dSBarry Smith   if (iascii || isdraw || isbinary || issocket) {
14747b2a1423SBarry Smith     ierr = MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer);CHKERRQ(ierr);
1475416022c9SBarry Smith   }
14763a40ed3dSBarry Smith   PetscFunctionReturn(0);
1477416022c9SBarry Smith }
1478416022c9SBarry Smith 
14794a2ae208SSatish Balay #undef __FUNCT__
148041f059aeSBarry Smith #define __FUNCT__ "MatSOR_MPIAIJ"
148141f059aeSBarry Smith PetscErrorCode MatSOR_MPIAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
14828a729477SBarry Smith {
148344a69424SLois Curfman McInnes   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)matin->data;
1484dfbe8321SBarry Smith   PetscErrorCode ierr;
14856987fefcSBarry Smith   Vec            bb1 = 0;
1486ace3abfcSBarry Smith   PetscBool      hasop;
14878a729477SBarry Smith 
14883a40ed3dSBarry Smith   PetscFunctionBegin;
1489a2b30743SBarry Smith   if (flag == SOR_APPLY_UPPER) {
149041f059aeSBarry Smith     ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
1491a2b30743SBarry Smith     PetscFunctionReturn(0);
1492a2b30743SBarry Smith   }
1493a2b30743SBarry Smith 
14944e980039SJed Brown   if (its > 1 || ~flag & SOR_ZERO_INITIAL_GUESS || flag & SOR_EISENSTAT) {
14954e980039SJed Brown     ierr = VecDuplicate(bb,&bb1);CHKERRQ(ierr);
14964e980039SJed Brown   }
14974e980039SJed Brown 
1498c16cb8f2SBarry Smith   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP) {
1499da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
150041f059aeSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
15012798e883SHong Zhang       its--;
1502da3a660dSBarry Smith     }
15032798e883SHong Zhang 
15042798e883SHong Zhang     while (its--) {
1505ca9f406cSSatish Balay       ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1506ca9f406cSSatish Balay       ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
15072798e883SHong Zhang 
1508c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
1509efb30889SBarry Smith       ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr);
1510c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
15112798e883SHong Zhang 
1512c14dc6b6SHong Zhang       /* local sweep */
151341f059aeSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr);
15142798e883SHong Zhang     }
15153a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_FORWARD_SWEEP) {
1516da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
151741f059aeSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
15182798e883SHong Zhang       its--;
1519da3a660dSBarry Smith     }
15202798e883SHong Zhang     while (its--) {
1521ca9f406cSSatish Balay       ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1522ca9f406cSSatish Balay       ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
15232798e883SHong Zhang 
1524c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
1525efb30889SBarry Smith       ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr);
1526c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
1527c14dc6b6SHong Zhang 
1528c14dc6b6SHong Zhang       /* local sweep */
152941f059aeSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr);
15302798e883SHong Zhang     }
15313a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_BACKWARD_SWEEP) {
1532da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
153341f059aeSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
15342798e883SHong Zhang       its--;
1535da3a660dSBarry Smith     }
15362798e883SHong Zhang     while (its--) {
1537ca9f406cSSatish Balay       ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1538ca9f406cSSatish Balay       ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
15392798e883SHong Zhang 
1540c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
1541efb30889SBarry Smith       ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr);
1542c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
15432798e883SHong Zhang 
1544c14dc6b6SHong Zhang       /* local sweep */
154541f059aeSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr);
15462798e883SHong Zhang     }
1547a7420bb7SBarry Smith   } else if (flag & SOR_EISENSTAT) {
1548a7420bb7SBarry Smith     Vec xx1;
1549a7420bb7SBarry Smith 
1550a7420bb7SBarry Smith     ierr = VecDuplicate(bb,&xx1);CHKERRQ(ierr);
155141f059aeSBarry 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);
1552a7420bb7SBarry Smith 
1553a7420bb7SBarry Smith     ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1554a7420bb7SBarry Smith     ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1555a7420bb7SBarry Smith     if (!mat->diag) {
15560298fd71SBarry Smith       ierr = MatGetVecs(matin,&mat->diag,NULL);CHKERRQ(ierr);
1557a7420bb7SBarry Smith       ierr = MatGetDiagonal(matin,mat->diag);CHKERRQ(ierr);
1558a7420bb7SBarry Smith     }
1559bd0c2dcbSBarry Smith     ierr = MatHasOperation(matin,MATOP_MULT_DIAGONAL_BLOCK,&hasop);CHKERRQ(ierr);
1560bd0c2dcbSBarry Smith     if (hasop) {
1561bd0c2dcbSBarry Smith       ierr = MatMultDiagonalBlock(matin,xx,bb1);CHKERRQ(ierr);
1562bd0c2dcbSBarry Smith     } else {
1563a7420bb7SBarry Smith       ierr = VecPointwiseMult(bb1,mat->diag,xx);CHKERRQ(ierr);
1564bd0c2dcbSBarry Smith     }
1565887ee2caSBarry Smith     ierr = VecAYPX(bb1,(omega-2.0)/omega,bb);CHKERRQ(ierr);
1566887ee2caSBarry Smith 
1567a7420bb7SBarry Smith     ierr = MatMultAdd(mat->B,mat->lvec,bb1,bb1);CHKERRQ(ierr);
1568a7420bb7SBarry Smith 
1569a7420bb7SBarry Smith     /* local sweep */
157041f059aeSBarry 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);
1571a7420bb7SBarry Smith     ierr = VecAXPY(xx,1.0,xx1);CHKERRQ(ierr);
15726bf464f9SBarry Smith     ierr = VecDestroy(&xx1);CHKERRQ(ierr);
1573ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)matin),PETSC_ERR_SUP,"Parallel SOR not supported");
1574c14dc6b6SHong Zhang 
15756bf464f9SBarry Smith   ierr = VecDestroy(&bb1);CHKERRQ(ierr);
15763a40ed3dSBarry Smith   PetscFunctionReturn(0);
15778a729477SBarry Smith }
1578a66be287SLois Curfman McInnes 
15794a2ae208SSatish Balay #undef __FUNCT__
158042e855d1Svictor #define __FUNCT__ "MatPermute_MPIAIJ"
158142e855d1Svictor PetscErrorCode MatPermute_MPIAIJ(Mat A,IS rowp,IS colp,Mat *B)
158242e855d1Svictor {
158372e6a0cfSJed Brown   Mat            aA,aB,Aperm;
158472e6a0cfSJed Brown   const PetscInt *rwant,*cwant,*gcols,*ai,*bi,*aj,*bj;
158572e6a0cfSJed Brown   PetscScalar    *aa,*ba;
158672e6a0cfSJed Brown   PetscInt       i,j,m,n,ng,anz,bnz,*dnnz,*onnz,*tdnnz,*tonnz,*rdest,*cdest,*work,*gcdest;
158772e6a0cfSJed Brown   PetscSF        rowsf,sf;
15880298fd71SBarry Smith   IS             parcolp = NULL;
158972e6a0cfSJed Brown   PetscBool      done;
159042e855d1Svictor   PetscErrorCode ierr;
159142e855d1Svictor 
159242e855d1Svictor   PetscFunctionBegin;
159372e6a0cfSJed Brown   ierr = MatGetLocalSize(A,&m,&n);CHKERRQ(ierr);
159472e6a0cfSJed Brown   ierr = ISGetIndices(rowp,&rwant);CHKERRQ(ierr);
159572e6a0cfSJed Brown   ierr = ISGetIndices(colp,&cwant);CHKERRQ(ierr);
1596dcca6d9dSJed Brown   ierr = PetscMalloc3(PetscMax(m,n),&work,m,&rdest,n,&cdest);CHKERRQ(ierr);
159772e6a0cfSJed Brown 
159872e6a0cfSJed Brown   /* Invert row permutation to find out where my rows should go */
1599ce94432eSBarry Smith   ierr = PetscSFCreate(PetscObjectComm((PetscObject)A),&rowsf);CHKERRQ(ierr);
16000298fd71SBarry Smith   ierr = PetscSFSetGraphLayout(rowsf,A->rmap,A->rmap->n,NULL,PETSC_OWN_POINTER,rwant);CHKERRQ(ierr);
1601e9e74f11SJed Brown   ierr = PetscSFSetFromOptions(rowsf);CHKERRQ(ierr);
160272e6a0cfSJed Brown   for (i=0; i<m; i++) work[i] = A->rmap->rstart + i;
16038bfbc91cSJed Brown   ierr = PetscSFReduceBegin(rowsf,MPIU_INT,work,rdest,MPIU_REPLACE);CHKERRQ(ierr);
16048bfbc91cSJed Brown   ierr = PetscSFReduceEnd(rowsf,MPIU_INT,work,rdest,MPIU_REPLACE);CHKERRQ(ierr);
160572e6a0cfSJed Brown 
160672e6a0cfSJed Brown   /* Invert column permutation to find out where my columns should go */
1607ce94432eSBarry Smith   ierr = PetscSFCreate(PetscObjectComm((PetscObject)A),&sf);CHKERRQ(ierr);
16080298fd71SBarry Smith   ierr = PetscSFSetGraphLayout(sf,A->cmap,A->cmap->n,NULL,PETSC_OWN_POINTER,cwant);CHKERRQ(ierr);
1609e9e74f11SJed Brown   ierr = PetscSFSetFromOptions(sf);CHKERRQ(ierr);
161072e6a0cfSJed Brown   for (i=0; i<n; i++) work[i] = A->cmap->rstart + i;
16118bfbc91cSJed Brown   ierr = PetscSFReduceBegin(sf,MPIU_INT,work,cdest,MPIU_REPLACE);CHKERRQ(ierr);
16128bfbc91cSJed Brown   ierr = PetscSFReduceEnd(sf,MPIU_INT,work,cdest,MPIU_REPLACE);CHKERRQ(ierr);
161372e6a0cfSJed Brown   ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
161472e6a0cfSJed Brown 
161572e6a0cfSJed Brown   ierr = ISRestoreIndices(rowp,&rwant);CHKERRQ(ierr);
161672e6a0cfSJed Brown   ierr = ISRestoreIndices(colp,&cwant);CHKERRQ(ierr);
161772e6a0cfSJed Brown   ierr = MatMPIAIJGetSeqAIJ(A,&aA,&aB,&gcols);CHKERRQ(ierr);
161872e6a0cfSJed Brown 
161972e6a0cfSJed Brown   /* Find out where my gcols should go */
16200298fd71SBarry Smith   ierr = MatGetSize(aB,NULL,&ng);CHKERRQ(ierr);
1621785e854fSJed Brown   ierr = PetscMalloc1(ng,&gcdest);CHKERRQ(ierr);
1622ce94432eSBarry Smith   ierr = PetscSFCreate(PetscObjectComm((PetscObject)A),&sf);CHKERRQ(ierr);
16230298fd71SBarry Smith   ierr = PetscSFSetGraphLayout(sf,A->cmap,ng,NULL,PETSC_OWN_POINTER,gcols);CHKERRQ(ierr);
1624e9e74f11SJed Brown   ierr = PetscSFSetFromOptions(sf);CHKERRQ(ierr);
162572e6a0cfSJed Brown   ierr = PetscSFBcastBegin(sf,MPIU_INT,cdest,gcdest);CHKERRQ(ierr);
162672e6a0cfSJed Brown   ierr = PetscSFBcastEnd(sf,MPIU_INT,cdest,gcdest);CHKERRQ(ierr);
162772e6a0cfSJed Brown   ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
162872e6a0cfSJed Brown 
16291795a4d1SJed Brown   ierr = PetscCalloc4(m,&dnnz,m,&onnz,m,&tdnnz,m,&tonnz);CHKERRQ(ierr);
163072e6a0cfSJed Brown   ierr = MatGetRowIJ(aA,0,PETSC_FALSE,PETSC_FALSE,&anz,&ai,&aj,&done);CHKERRQ(ierr);
163172e6a0cfSJed Brown   ierr = MatGetRowIJ(aB,0,PETSC_FALSE,PETSC_FALSE,&bnz,&bi,&bj,&done);CHKERRQ(ierr);
163272e6a0cfSJed Brown   for (i=0; i<m; i++) {
163372e6a0cfSJed Brown     PetscInt row = rdest[i],rowner;
163472e6a0cfSJed Brown     ierr = PetscLayoutFindOwner(A->rmap,row,&rowner);CHKERRQ(ierr);
163572e6a0cfSJed Brown     for (j=ai[i]; j<ai[i+1]; j++) {
163672e6a0cfSJed Brown       PetscInt cowner,col = cdest[aj[j]];
163772e6a0cfSJed Brown       ierr = PetscLayoutFindOwner(A->cmap,col,&cowner);CHKERRQ(ierr); /* Could build an index for the columns to eliminate this search */
163872e6a0cfSJed Brown       if (rowner == cowner) dnnz[i]++;
163972e6a0cfSJed Brown       else onnz[i]++;
164072e6a0cfSJed Brown     }
164172e6a0cfSJed Brown     for (j=bi[i]; j<bi[i+1]; j++) {
164272e6a0cfSJed Brown       PetscInt cowner,col = gcdest[bj[j]];
164372e6a0cfSJed Brown       ierr = PetscLayoutFindOwner(A->cmap,col,&cowner);CHKERRQ(ierr);
164472e6a0cfSJed Brown       if (rowner == cowner) dnnz[i]++;
164572e6a0cfSJed Brown       else onnz[i]++;
164672e6a0cfSJed Brown     }
164772e6a0cfSJed Brown   }
164872e6a0cfSJed Brown   ierr = PetscSFBcastBegin(rowsf,MPIU_INT,dnnz,tdnnz);CHKERRQ(ierr);
164972e6a0cfSJed Brown   ierr = PetscSFBcastEnd(rowsf,MPIU_INT,dnnz,tdnnz);CHKERRQ(ierr);
165072e6a0cfSJed Brown   ierr = PetscSFBcastBegin(rowsf,MPIU_INT,onnz,tonnz);CHKERRQ(ierr);
165172e6a0cfSJed Brown   ierr = PetscSFBcastEnd(rowsf,MPIU_INT,onnz,tonnz);CHKERRQ(ierr);
165272e6a0cfSJed Brown   ierr = PetscSFDestroy(&rowsf);CHKERRQ(ierr);
165372e6a0cfSJed Brown 
1654ce94432eSBarry 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);
165572e6a0cfSJed Brown   ierr = MatSeqAIJGetArray(aA,&aa);CHKERRQ(ierr);
165672e6a0cfSJed Brown   ierr = MatSeqAIJGetArray(aB,&ba);CHKERRQ(ierr);
165772e6a0cfSJed Brown   for (i=0; i<m; i++) {
165872e6a0cfSJed Brown     PetscInt *acols = dnnz,*bcols = onnz; /* Repurpose now-unneeded arrays */
1659970468b0SJed Brown     PetscInt j0,rowlen;
166072e6a0cfSJed Brown     rowlen = ai[i+1] - ai[i];
1661970468b0SJed Brown     for (j0=j=0; j<rowlen; j0=j) { /* rowlen could be larger than number of rows m, so sum in batches */
1662970468b0SJed Brown       for ( ; j<PetscMin(rowlen,j0+m); j++) acols[j-j0] = cdest[aj[ai[i]+j]];
1663970468b0SJed Brown       ierr = MatSetValues(Aperm,1,&rdest[i],j-j0,acols,aa+ai[i]+j0,INSERT_VALUES);CHKERRQ(ierr);
1664970468b0SJed Brown     }
166572e6a0cfSJed Brown     rowlen = bi[i+1] - bi[i];
1666970468b0SJed Brown     for (j0=j=0; j<rowlen; j0=j) {
1667970468b0SJed Brown       for ( ; j<PetscMin(rowlen,j0+m); j++) bcols[j-j0] = gcdest[bj[bi[i]+j]];
1668970468b0SJed Brown       ierr = MatSetValues(Aperm,1,&rdest[i],j-j0,bcols,ba+bi[i]+j0,INSERT_VALUES);CHKERRQ(ierr);
1669970468b0SJed Brown     }
167072e6a0cfSJed Brown   }
167172e6a0cfSJed Brown   ierr = MatAssemblyBegin(Aperm,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
167272e6a0cfSJed Brown   ierr = MatAssemblyEnd(Aperm,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
167372e6a0cfSJed Brown   ierr = MatRestoreRowIJ(aA,0,PETSC_FALSE,PETSC_FALSE,&anz,&ai,&aj,&done);CHKERRQ(ierr);
167472e6a0cfSJed Brown   ierr = MatRestoreRowIJ(aB,0,PETSC_FALSE,PETSC_FALSE,&bnz,&bi,&bj,&done);CHKERRQ(ierr);
167572e6a0cfSJed Brown   ierr = MatSeqAIJRestoreArray(aA,&aa);CHKERRQ(ierr);
167672e6a0cfSJed Brown   ierr = MatSeqAIJRestoreArray(aB,&ba);CHKERRQ(ierr);
167772e6a0cfSJed Brown   ierr = PetscFree4(dnnz,onnz,tdnnz,tonnz);CHKERRQ(ierr);
167872e6a0cfSJed Brown   ierr = PetscFree3(work,rdest,cdest);CHKERRQ(ierr);
167972e6a0cfSJed Brown   ierr = PetscFree(gcdest);CHKERRQ(ierr);
168072e6a0cfSJed Brown   if (parcolp) {ierr = ISDestroy(&colp);CHKERRQ(ierr);}
168172e6a0cfSJed Brown   *B = Aperm;
168242e855d1Svictor   PetscFunctionReturn(0);
168342e855d1Svictor }
168442e855d1Svictor 
168542e855d1Svictor #undef __FUNCT__
16864a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_MPIAIJ"
1687dfbe8321SBarry Smith PetscErrorCode MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info)
1688a66be287SLois Curfman McInnes {
1689a66be287SLois Curfman McInnes   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)matin->data;
1690a66be287SLois Curfman McInnes   Mat            A    = mat->A,B = mat->B;
1691dfbe8321SBarry Smith   PetscErrorCode ierr;
1692329f5518SBarry Smith   PetscReal      isend[5],irecv[5];
1693a66be287SLois Curfman McInnes 
16943a40ed3dSBarry Smith   PetscFunctionBegin;
16954e220ebcSLois Curfman McInnes   info->block_size = 1.0;
16964e220ebcSLois Curfman McInnes   ierr             = MatGetInfo(A,MAT_LOCAL,info);CHKERRQ(ierr);
16972205254eSKarl Rupp 
16984e220ebcSLois Curfman McInnes   isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
16994e220ebcSLois Curfman McInnes   isend[3] = info->memory;  isend[4] = info->mallocs;
17002205254eSKarl Rupp 
17014e220ebcSLois Curfman McInnes   ierr = MatGetInfo(B,MAT_LOCAL,info);CHKERRQ(ierr);
17022205254eSKarl Rupp 
17034e220ebcSLois Curfman McInnes   isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
17044e220ebcSLois Curfman McInnes   isend[3] += info->memory;  isend[4] += info->mallocs;
1705a66be287SLois Curfman McInnes   if (flag == MAT_LOCAL) {
17064e220ebcSLois Curfman McInnes     info->nz_used      = isend[0];
17074e220ebcSLois Curfman McInnes     info->nz_allocated = isend[1];
17084e220ebcSLois Curfman McInnes     info->nz_unneeded  = isend[2];
17094e220ebcSLois Curfman McInnes     info->memory       = isend[3];
17104e220ebcSLois Curfman McInnes     info->mallocs      = isend[4];
1711a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_MAX) {
1712ce94432eSBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)matin));CHKERRQ(ierr);
17132205254eSKarl Rupp 
17144e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
17154e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
17164e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
17174e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
17184e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1719a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_SUM) {
1720ce94432eSBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)matin));CHKERRQ(ierr);
17212205254eSKarl Rupp 
17224e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
17234e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
17244e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
17254e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
17264e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1727a66be287SLois Curfman McInnes   }
17284e220ebcSLois Curfman McInnes   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
17294e220ebcSLois Curfman McInnes   info->fill_ratio_needed = 0;
17304e220ebcSLois Curfman McInnes   info->factor_mallocs    = 0;
17313a40ed3dSBarry Smith   PetscFunctionReturn(0);
1732a66be287SLois Curfman McInnes }
1733a66be287SLois Curfman McInnes 
17344a2ae208SSatish Balay #undef __FUNCT__
17354a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_MPIAIJ"
1736ace3abfcSBarry Smith PetscErrorCode MatSetOption_MPIAIJ(Mat A,MatOption op,PetscBool flg)
1737c74985f6SBarry Smith {
1738c0bbcb79SLois Curfman McInnes   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
1739dfbe8321SBarry Smith   PetscErrorCode ierr;
1740c74985f6SBarry Smith 
17413a40ed3dSBarry Smith   PetscFunctionBegin;
174212c028f9SKris Buschelman   switch (op) {
1743512a5fc5SBarry Smith   case MAT_NEW_NONZERO_LOCATIONS:
174412c028f9SKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
174528b2fa4aSMatthew Knepley   case MAT_UNUSED_NONZERO_LOCATION_ERR:
1746a9817697SBarry Smith   case MAT_KEEP_NONZERO_PATTERN:
174712c028f9SKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
174812c028f9SKris Buschelman   case MAT_USE_INODES:
174912c028f9SKris Buschelman   case MAT_IGNORE_ZERO_ENTRIES:
1750fa1f0d2cSMatthew G Knepley     MatCheckPreallocated(A,1);
17514e0d8c25SBarry Smith     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
17524e0d8c25SBarry Smith     ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr);
175312c028f9SKris Buschelman     break;
175412c028f9SKris Buschelman   case MAT_ROW_ORIENTED:
17554e0d8c25SBarry Smith     a->roworiented = flg;
17562205254eSKarl Rupp 
17574e0d8c25SBarry Smith     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
17584e0d8c25SBarry Smith     ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr);
175912c028f9SKris Buschelman     break;
17604e0d8c25SBarry Smith   case MAT_NEW_DIAGONALS:
1761290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
176212c028f9SKris Buschelman     break;
176312c028f9SKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
17645c0f0b64SBarry Smith     a->donotstash = flg;
176512c028f9SKris Buschelman     break;
1766ffa07934SHong Zhang   case MAT_SPD:
1767ffa07934SHong Zhang     A->spd_set = PETSC_TRUE;
1768ffa07934SHong Zhang     A->spd     = flg;
1769ffa07934SHong Zhang     if (flg) {
1770ffa07934SHong Zhang       A->symmetric                  = PETSC_TRUE;
1771ffa07934SHong Zhang       A->structurally_symmetric     = PETSC_TRUE;
1772ffa07934SHong Zhang       A->symmetric_set              = PETSC_TRUE;
1773ffa07934SHong Zhang       A->structurally_symmetric_set = PETSC_TRUE;
1774ffa07934SHong Zhang     }
1775ffa07934SHong Zhang     break;
177677e54ba9SKris Buschelman   case MAT_SYMMETRIC:
17774e0d8c25SBarry Smith     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
177825f421beSHong Zhang     break;
177977e54ba9SKris Buschelman   case MAT_STRUCTURALLY_SYMMETRIC:
1780eeffb40dSHong Zhang     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
1781eeffb40dSHong Zhang     break;
1782bf108f30SBarry Smith   case MAT_HERMITIAN:
1783eeffb40dSHong Zhang     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
1784eeffb40dSHong Zhang     break;
1785bf108f30SBarry Smith   case MAT_SYMMETRY_ETERNAL:
17864e0d8c25SBarry Smith     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
178777e54ba9SKris Buschelman     break;
178812c028f9SKris Buschelman   default:
1789e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
17903a40ed3dSBarry Smith   }
17913a40ed3dSBarry Smith   PetscFunctionReturn(0);
1792c74985f6SBarry Smith }
1793c74985f6SBarry Smith 
17944a2ae208SSatish Balay #undef __FUNCT__
17954a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_MPIAIJ"
1796b1d57f15SBarry Smith PetscErrorCode MatGetRow_MPIAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
179739e00950SLois Curfman McInnes {
1798154123eaSLois Curfman McInnes   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)matin->data;
179987828ca2SBarry Smith   PetscScalar    *vworkA,*vworkB,**pvA,**pvB,*v_p;
18006849ba73SBarry Smith   PetscErrorCode ierr;
1801d0f46423SBarry Smith   PetscInt       i,*cworkA,*cworkB,**pcA,**pcB,cstart = matin->cmap->rstart;
1802d0f46423SBarry Smith   PetscInt       nztot,nzA,nzB,lrow,rstart = matin->rmap->rstart,rend = matin->rmap->rend;
1803b1d57f15SBarry Smith   PetscInt       *cmap,*idx_p;
180439e00950SLois Curfman McInnes 
18053a40ed3dSBarry Smith   PetscFunctionBegin;
1806e32f2f54SBarry Smith   if (mat->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Already active");
18077a0afa10SBarry Smith   mat->getrowactive = PETSC_TRUE;
18087a0afa10SBarry Smith 
180970f0671dSBarry Smith   if (!mat->rowvalues && (idx || v)) {
18107a0afa10SBarry Smith     /*
18117a0afa10SBarry Smith         allocate enough space to hold information from the longest row.
18127a0afa10SBarry Smith     */
18137a0afa10SBarry Smith     Mat_SeqAIJ *Aa = (Mat_SeqAIJ*)mat->A->data,*Ba = (Mat_SeqAIJ*)mat->B->data;
1814b1d57f15SBarry Smith     PetscInt   max = 1,tmp;
1815d0f46423SBarry Smith     for (i=0; i<matin->rmap->n; i++) {
18167a0afa10SBarry Smith       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
18172205254eSKarl Rupp       if (max < tmp) max = tmp;
18187a0afa10SBarry Smith     }
1819dcca6d9dSJed Brown     ierr = PetscMalloc2(max,&mat->rowvalues,max,&mat->rowindices);CHKERRQ(ierr);
18207a0afa10SBarry Smith   }
18217a0afa10SBarry Smith 
1822e7e72b3dSBarry Smith   if (row < rstart || row >= rend) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only local rows");
1823abc0e9e4SLois Curfman McInnes   lrow = row - rstart;
182439e00950SLois Curfman McInnes 
1825154123eaSLois Curfman McInnes   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1826154123eaSLois Curfman McInnes   if (!v)   {pvA = 0; pvB = 0;}
1827154123eaSLois Curfman McInnes   if (!idx) {pcA = 0; if (!v) pcB = 0;}
1828f830108cSBarry Smith   ierr  = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1829f830108cSBarry Smith   ierr  = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
1830154123eaSLois Curfman McInnes   nztot = nzA + nzB;
1831154123eaSLois Curfman McInnes 
183270f0671dSBarry Smith   cmap = mat->garray;
1833154123eaSLois Curfman McInnes   if (v  || idx) {
1834154123eaSLois Curfman McInnes     if (nztot) {
1835154123eaSLois Curfman McInnes       /* Sort by increasing column numbers, assuming A and B already sorted */
1836b1d57f15SBarry Smith       PetscInt imark = -1;
1837154123eaSLois Curfman McInnes       if (v) {
183870f0671dSBarry Smith         *v = v_p = mat->rowvalues;
183939e00950SLois Curfman McInnes         for (i=0; i<nzB; i++) {
184070f0671dSBarry Smith           if (cmap[cworkB[i]] < cstart) v_p[i] = vworkB[i];
1841154123eaSLois Curfman McInnes           else break;
1842154123eaSLois Curfman McInnes         }
1843154123eaSLois Curfman McInnes         imark = i;
184470f0671dSBarry Smith         for (i=0; i<nzA; i++)     v_p[imark+i] = vworkA[i];
184570f0671dSBarry Smith         for (i=imark; i<nzB; i++) v_p[nzA+i]   = vworkB[i];
1846154123eaSLois Curfman McInnes       }
1847154123eaSLois Curfman McInnes       if (idx) {
184870f0671dSBarry Smith         *idx = idx_p = mat->rowindices;
184970f0671dSBarry Smith         if (imark > -1) {
185070f0671dSBarry Smith           for (i=0; i<imark; i++) {
185170f0671dSBarry Smith             idx_p[i] = cmap[cworkB[i]];
185270f0671dSBarry Smith           }
185370f0671dSBarry Smith         } else {
1854154123eaSLois Curfman McInnes           for (i=0; i<nzB; i++) {
185570f0671dSBarry Smith             if (cmap[cworkB[i]] < cstart) idx_p[i] = cmap[cworkB[i]];
1856154123eaSLois Curfman McInnes             else break;
1857154123eaSLois Curfman McInnes           }
1858154123eaSLois Curfman McInnes           imark = i;
185970f0671dSBarry Smith         }
186070f0671dSBarry Smith         for (i=0; i<nzA; i++)     idx_p[imark+i] = cstart + cworkA[i];
186170f0671dSBarry Smith         for (i=imark; i<nzB; i++) idx_p[nzA+i]   = cmap[cworkB[i]];
186239e00950SLois Curfman McInnes       }
18633f97c4b0SBarry Smith     } else {
18641ca473b0SSatish Balay       if (idx) *idx = 0;
18651ca473b0SSatish Balay       if (v)   *v   = 0;
18661ca473b0SSatish Balay     }
1867154123eaSLois Curfman McInnes   }
186839e00950SLois Curfman McInnes   *nz  = nztot;
1869f830108cSBarry Smith   ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1870f830108cSBarry Smith   ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
18713a40ed3dSBarry Smith   PetscFunctionReturn(0);
187239e00950SLois Curfman McInnes }
187339e00950SLois Curfman McInnes 
18744a2ae208SSatish Balay #undef __FUNCT__
18754a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_MPIAIJ"
1876b1d57f15SBarry Smith PetscErrorCode MatRestoreRow_MPIAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
187739e00950SLois Curfman McInnes {
18787a0afa10SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
18793a40ed3dSBarry Smith 
18803a40ed3dSBarry Smith   PetscFunctionBegin;
1881e7e72b3dSBarry Smith   if (!aij->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"MatGetRow() must be called first");
18827a0afa10SBarry Smith   aij->getrowactive = PETSC_FALSE;
18833a40ed3dSBarry Smith   PetscFunctionReturn(0);
188439e00950SLois Curfman McInnes }
188539e00950SLois Curfman McInnes 
18864a2ae208SSatish Balay #undef __FUNCT__
18874a2ae208SSatish Balay #define __FUNCT__ "MatNorm_MPIAIJ"
1888dfbe8321SBarry Smith PetscErrorCode MatNorm_MPIAIJ(Mat mat,NormType type,PetscReal *norm)
1889855ac2c5SLois Curfman McInnes {
1890855ac2c5SLois Curfman McInnes   Mat_MPIAIJ     *aij  = (Mat_MPIAIJ*)mat->data;
1891ec8511deSBarry Smith   Mat_SeqAIJ     *amat = (Mat_SeqAIJ*)aij->A->data,*bmat = (Mat_SeqAIJ*)aij->B->data;
1892dfbe8321SBarry Smith   PetscErrorCode ierr;
1893d0f46423SBarry Smith   PetscInt       i,j,cstart = mat->cmap->rstart;
1894329f5518SBarry Smith   PetscReal      sum = 0.0;
1895a77337e4SBarry Smith   MatScalar      *v;
189604ca555eSLois Curfman McInnes 
18973a40ed3dSBarry Smith   PetscFunctionBegin;
189817699dbbSLois Curfman McInnes   if (aij->size == 1) {
189914183eadSLois Curfman McInnes     ierr =  MatNorm(aij->A,type,norm);CHKERRQ(ierr);
190037fa93a5SLois Curfman McInnes   } else {
190104ca555eSLois Curfman McInnes     if (type == NORM_FROBENIUS) {
190204ca555eSLois Curfman McInnes       v = amat->a;
190304ca555eSLois Curfman McInnes       for (i=0; i<amat->nz; i++) {
1904329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
190504ca555eSLois Curfman McInnes       }
190604ca555eSLois Curfman McInnes       v = bmat->a;
190704ca555eSLois Curfman McInnes       for (i=0; i<bmat->nz; i++) {
1908329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
190904ca555eSLois Curfman McInnes       }
1910ce94432eSBarry Smith       ierr  = MPI_Allreduce(&sum,norm,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
19118f1a2a5eSBarry Smith       *norm = PetscSqrtReal(*norm);
19123a40ed3dSBarry Smith     } else if (type == NORM_1) { /* max column norm */
1913329f5518SBarry Smith       PetscReal *tmp,*tmp2;
1914b1d57f15SBarry Smith       PetscInt  *jj,*garray = aij->garray;
19151795a4d1SJed Brown       ierr  = PetscCalloc1((mat->cmap->N+1),&tmp);CHKERRQ(ierr);
1916785e854fSJed Brown       ierr  = PetscMalloc1((mat->cmap->N+1),&tmp2);CHKERRQ(ierr);
191704ca555eSLois Curfman McInnes       *norm = 0.0;
191804ca555eSLois Curfman McInnes       v     = amat->a; jj = amat->j;
191904ca555eSLois Curfman McInnes       for (j=0; j<amat->nz; j++) {
1920bfec09a0SHong Zhang         tmp[cstart + *jj++] += PetscAbsScalar(*v);  v++;
192104ca555eSLois Curfman McInnes       }
192204ca555eSLois Curfman McInnes       v = bmat->a; jj = bmat->j;
192304ca555eSLois Curfman McInnes       for (j=0; j<bmat->nz; j++) {
1924bfec09a0SHong Zhang         tmp[garray[*jj++]] += PetscAbsScalar(*v); v++;
192504ca555eSLois Curfman McInnes       }
1926ce94432eSBarry Smith       ierr = MPI_Allreduce(tmp,tmp2,mat->cmap->N,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1927d0f46423SBarry Smith       for (j=0; j<mat->cmap->N; j++) {
192804ca555eSLois Curfman McInnes         if (tmp2[j] > *norm) *norm = tmp2[j];
192904ca555eSLois Curfman McInnes       }
1930606d414cSSatish Balay       ierr = PetscFree(tmp);CHKERRQ(ierr);
1931606d414cSSatish Balay       ierr = PetscFree(tmp2);CHKERRQ(ierr);
19323a40ed3dSBarry Smith     } else if (type == NORM_INFINITY) { /* max row norm */
1933329f5518SBarry Smith       PetscReal ntemp = 0.0;
1934d0f46423SBarry Smith       for (j=0; j<aij->A->rmap->n; j++) {
1935bfec09a0SHong Zhang         v   = amat->a + amat->i[j];
193604ca555eSLois Curfman McInnes         sum = 0.0;
193704ca555eSLois Curfman McInnes         for (i=0; i<amat->i[j+1]-amat->i[j]; i++) {
1938cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
193904ca555eSLois Curfman McInnes         }
1940bfec09a0SHong Zhang         v = bmat->a + bmat->i[j];
194104ca555eSLois Curfman McInnes         for (i=0; i<bmat->i[j+1]-bmat->i[j]; i++) {
1942cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
194304ca555eSLois Curfman McInnes         }
1944515d9167SLois Curfman McInnes         if (sum > ntemp) ntemp = sum;
194504ca555eSLois Curfman McInnes       }
1946ce94432eSBarry Smith       ierr = MPI_Allreduce(&ntemp,norm,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1947ce94432eSBarry Smith     } else SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"No support for two norm");
194837fa93a5SLois Curfman McInnes   }
19493a40ed3dSBarry Smith   PetscFunctionReturn(0);
1950855ac2c5SLois Curfman McInnes }
1951855ac2c5SLois Curfman McInnes 
19524a2ae208SSatish Balay #undef __FUNCT__
19534a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_MPIAIJ"
1954fc4dec0aSBarry Smith PetscErrorCode MatTranspose_MPIAIJ(Mat A,MatReuse reuse,Mat *matout)
1955b7c46309SBarry Smith {
1956b7c46309SBarry Smith   Mat_MPIAIJ     *a   = (Mat_MPIAIJ*)A->data;
1957da668accSHong Zhang   Mat_SeqAIJ     *Aloc=(Mat_SeqAIJ*)a->A->data,*Bloc=(Mat_SeqAIJ*)a->B->data;
1958dfbe8321SBarry Smith   PetscErrorCode ierr;
195980bcc5a1SJed Brown   PetscInt       M      = A->rmap->N,N = A->cmap->N,ma,na,mb,nb,*ai,*aj,*bi,*bj,row,*cols,*cols_tmp,i;
1960d0f46423SBarry Smith   PetscInt       cstart = A->cmap->rstart,ncol;
19613a40ed3dSBarry Smith   Mat            B;
1962a77337e4SBarry Smith   MatScalar      *array;
1963b7c46309SBarry Smith 
19643a40ed3dSBarry Smith   PetscFunctionBegin;
1965ce94432eSBarry Smith   if (reuse == MAT_REUSE_MATRIX && A == *matout && M != N) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1966da668accSHong Zhang 
196780bcc5a1SJed Brown   ma = A->rmap->n; na = A->cmap->n; mb = a->B->rmap->n; nb = a->B->cmap->n;
1968da668accSHong Zhang   ai = Aloc->i; aj = Aloc->j;
1969da668accSHong Zhang   bi = Bloc->i; bj = Bloc->j;
1970fc73b1b3SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *matout == A) {
197180bcc5a1SJed Brown     PetscInt             *d_nnz,*g_nnz,*o_nnz;
197280bcc5a1SJed Brown     PetscSFNode          *oloc;
1973713c93b4SJed Brown     PETSC_UNUSED PetscSF sf;
197480bcc5a1SJed Brown 
1975dcca6d9dSJed Brown     ierr = PetscMalloc4(na,&d_nnz,na,&o_nnz,nb,&g_nnz,nb,&oloc);CHKERRQ(ierr);
197680bcc5a1SJed Brown     /* compute d_nnz for preallocation */
197780bcc5a1SJed Brown     ierr = PetscMemzero(d_nnz,na*sizeof(PetscInt));CHKERRQ(ierr);
1978da668accSHong Zhang     for (i=0; i<ai[ma]; i++) {
1979da668accSHong Zhang       d_nnz[aj[i]]++;
1980da668accSHong Zhang       aj[i] += cstart; /* global col index to be used by MatSetValues() */
1981d4bb536fSBarry Smith     }
198280bcc5a1SJed Brown     /* compute local off-diagonal contributions */
19830beca09bSJed Brown     ierr = PetscMemzero(g_nnz,nb*sizeof(PetscInt));CHKERRQ(ierr);
198480bcc5a1SJed Brown     for (i=0; i<bi[ma]; i++) g_nnz[bj[i]]++;
198580bcc5a1SJed Brown     /* map those to global */
1986ce94432eSBarry Smith     ierr = PetscSFCreate(PetscObjectComm((PetscObject)A),&sf);CHKERRQ(ierr);
19870298fd71SBarry Smith     ierr = PetscSFSetGraphLayout(sf,A->cmap,nb,NULL,PETSC_USE_POINTER,a->garray);CHKERRQ(ierr);
1988e9e74f11SJed Brown     ierr = PetscSFSetFromOptions(sf);CHKERRQ(ierr);
198980bcc5a1SJed Brown     ierr = PetscMemzero(o_nnz,na*sizeof(PetscInt));CHKERRQ(ierr);
199080bcc5a1SJed Brown     ierr = PetscSFReduceBegin(sf,MPIU_INT,g_nnz,o_nnz,MPIU_SUM);CHKERRQ(ierr);
199180bcc5a1SJed Brown     ierr = PetscSFReduceEnd(sf,MPIU_INT,g_nnz,o_nnz,MPIU_SUM);CHKERRQ(ierr);
199280bcc5a1SJed Brown     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
1993d4bb536fSBarry Smith 
1994ce94432eSBarry Smith     ierr = MatCreate(PetscObjectComm((PetscObject)A),&B);CHKERRQ(ierr);
1995d0f46423SBarry Smith     ierr = MatSetSizes(B,A->cmap->n,A->rmap->n,N,M);CHKERRQ(ierr);
199633d57670SJed Brown     ierr = MatSetBlockSizes(B,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));CHKERRQ(ierr);
19977adad957SLisandro Dalcin     ierr = MatSetType(B,((PetscObject)A)->type_name);CHKERRQ(ierr);
199880bcc5a1SJed Brown     ierr = MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);CHKERRQ(ierr);
199980bcc5a1SJed Brown     ierr = PetscFree4(d_nnz,o_nnz,g_nnz,oloc);CHKERRQ(ierr);
2000fc4dec0aSBarry Smith   } else {
2001fc4dec0aSBarry Smith     B    = *matout;
20026ffab4bbSHong Zhang     ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
20032205254eSKarl Rupp     for (i=0; i<ai[ma]; i++) aj[i] += cstart; /* global col index to be used by MatSetValues() */
2004fc4dec0aSBarry Smith   }
2005b7c46309SBarry Smith 
2006b7c46309SBarry Smith   /* copy over the A part */
2007da668accSHong Zhang   array = Aloc->a;
2008d0f46423SBarry Smith   row   = A->rmap->rstart;
2009da668accSHong Zhang   for (i=0; i<ma; i++) {
2010da668accSHong Zhang     ncol = ai[i+1]-ai[i];
2011da668accSHong Zhang     ierr = MatSetValues(B,ncol,aj,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
20122205254eSKarl Rupp     row++;
20132205254eSKarl Rupp     array += ncol; aj += ncol;
2014b7c46309SBarry Smith   }
2015b7c46309SBarry Smith   aj = Aloc->j;
2016da668accSHong Zhang   for (i=0; i<ai[ma]; i++) aj[i] -= cstart; /* resume local col index */
2017b7c46309SBarry Smith 
2018b7c46309SBarry Smith   /* copy over the B part */
20191795a4d1SJed Brown   ierr  = PetscCalloc1(bi[mb],&cols);CHKERRQ(ierr);
2020da668accSHong Zhang   array = Bloc->a;
2021d0f46423SBarry Smith   row   = A->rmap->rstart;
20222205254eSKarl Rupp   for (i=0; i<bi[mb]; i++) cols[i] = a->garray[bj[i]];
202361a2fbbaSHong Zhang   cols_tmp = cols;
2024da668accSHong Zhang   for (i=0; i<mb; i++) {
2025da668accSHong Zhang     ncol = bi[i+1]-bi[i];
202661a2fbbaSHong Zhang     ierr = MatSetValues(B,ncol,cols_tmp,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
20272205254eSKarl Rupp     row++;
20282205254eSKarl Rupp     array += ncol; cols_tmp += ncol;
2029b7c46309SBarry Smith   }
2030fc73b1b3SBarry Smith   ierr = PetscFree(cols);CHKERRQ(ierr);
2031fc73b1b3SBarry Smith 
20326d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20336d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2034815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *matout != A) {
20350de55854SLois Curfman McInnes     *matout = B;
20360de55854SLois Curfman McInnes   } else {
2037eb6b5d47SBarry Smith     ierr = MatHeaderMerge(A,B);CHKERRQ(ierr);
20380de55854SLois Curfman McInnes   }
20393a40ed3dSBarry Smith   PetscFunctionReturn(0);
2040b7c46309SBarry Smith }
2041b7c46309SBarry Smith 
20424a2ae208SSatish Balay #undef __FUNCT__
20434a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_MPIAIJ"
2044dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr)
2045a008b906SSatish Balay {
20464b967eb1SSatish Balay   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
20474b967eb1SSatish Balay   Mat            a    = aij->A,b = aij->B;
2048dfbe8321SBarry Smith   PetscErrorCode ierr;
2049b1d57f15SBarry Smith   PetscInt       s1,s2,s3;
2050a008b906SSatish Balay 
20513a40ed3dSBarry Smith   PetscFunctionBegin;
20524b967eb1SSatish Balay   ierr = MatGetLocalSize(mat,&s2,&s3);CHKERRQ(ierr);
20534b967eb1SSatish Balay   if (rr) {
2054e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr);
2055e32f2f54SBarry Smith     if (s1!=s3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"right vector non-conforming local size");
20564b967eb1SSatish Balay     /* Overlap communication with computation. */
2057ca9f406cSSatish Balay     ierr = VecScatterBegin(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2058a008b906SSatish Balay   }
20594b967eb1SSatish Balay   if (ll) {
2060e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr);
2061e32f2f54SBarry Smith     if (s1!=s2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"left vector non-conforming local size");
2062f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,ll,0);CHKERRQ(ierr);
20634b967eb1SSatish Balay   }
20644b967eb1SSatish Balay   /* scale  the diagonal block */
2065f830108cSBarry Smith   ierr = (*a->ops->diagonalscale)(a,ll,rr);CHKERRQ(ierr);
20664b967eb1SSatish Balay 
20674b967eb1SSatish Balay   if (rr) {
20684b967eb1SSatish Balay     /* Do a scatter end and then right scale the off-diagonal block */
2069ca9f406cSSatish Balay     ierr = VecScatterEnd(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2070f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,0,aij->lvec);CHKERRQ(ierr);
20714b967eb1SSatish Balay   }
20723a40ed3dSBarry Smith   PetscFunctionReturn(0);
2073a008b906SSatish Balay }
2074a008b906SSatish Balay 
20754a2ae208SSatish Balay #undef __FUNCT__
20764a2ae208SSatish Balay #define __FUNCT__ "MatSetUnfactored_MPIAIJ"
2077dfbe8321SBarry Smith PetscErrorCode MatSetUnfactored_MPIAIJ(Mat A)
2078bb5a7306SBarry Smith {
2079bb5a7306SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
2080dfbe8321SBarry Smith   PetscErrorCode ierr;
20813a40ed3dSBarry Smith 
20823a40ed3dSBarry Smith   PetscFunctionBegin;
2083bb5a7306SBarry Smith   ierr = MatSetUnfactored(a->A);CHKERRQ(ierr);
20843a40ed3dSBarry Smith   PetscFunctionReturn(0);
2085bb5a7306SBarry Smith }
2086bb5a7306SBarry Smith 
20874a2ae208SSatish Balay #undef __FUNCT__
20884a2ae208SSatish Balay #define __FUNCT__ "MatEqual_MPIAIJ"
2089ace3abfcSBarry Smith PetscErrorCode MatEqual_MPIAIJ(Mat A,Mat B,PetscBool  *flag)
2090d4bb536fSBarry Smith {
2091d4bb536fSBarry Smith   Mat_MPIAIJ     *matB = (Mat_MPIAIJ*)B->data,*matA = (Mat_MPIAIJ*)A->data;
2092d4bb536fSBarry Smith   Mat            a,b,c,d;
2093ace3abfcSBarry Smith   PetscBool      flg;
2094dfbe8321SBarry Smith   PetscErrorCode ierr;
2095d4bb536fSBarry Smith 
20963a40ed3dSBarry Smith   PetscFunctionBegin;
2097d4bb536fSBarry Smith   a = matA->A; b = matA->B;
2098d4bb536fSBarry Smith   c = matB->A; d = matB->B;
2099d4bb536fSBarry Smith 
2100d4bb536fSBarry Smith   ierr = MatEqual(a,c,&flg);CHKERRQ(ierr);
2101abc0a331SBarry Smith   if (flg) {
2102d4bb536fSBarry Smith     ierr = MatEqual(b,d,&flg);CHKERRQ(ierr);
2103d4bb536fSBarry Smith   }
2104ce94432eSBarry Smith   ierr = MPI_Allreduce(&flg,flag,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)A));CHKERRQ(ierr);
21053a40ed3dSBarry Smith   PetscFunctionReturn(0);
2106d4bb536fSBarry Smith }
2107d4bb536fSBarry Smith 
21084a2ae208SSatish Balay #undef __FUNCT__
21094a2ae208SSatish Balay #define __FUNCT__ "MatCopy_MPIAIJ"
2110dfbe8321SBarry Smith PetscErrorCode MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str)
2111cb5b572fSBarry Smith {
2112dfbe8321SBarry Smith   PetscErrorCode ierr;
2113cb5b572fSBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
2114cb5b572fSBarry Smith   Mat_MPIAIJ     *b = (Mat_MPIAIJ*)B->data;
2115cb5b572fSBarry Smith 
2116cb5b572fSBarry Smith   PetscFunctionBegin;
211733f4a19fSKris Buschelman   /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
211833f4a19fSKris Buschelman   if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) {
2119cb5b572fSBarry Smith     /* because of the column compression in the off-processor part of the matrix a->B,
2120cb5b572fSBarry Smith        the number of columns in a->B and b->B may be different, hence we cannot call
2121cb5b572fSBarry Smith        the MatCopy() directly on the two parts. If need be, we can provide a more
2122cb5b572fSBarry Smith        efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices
2123cb5b572fSBarry Smith        then copying the submatrices */
2124cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2125cb5b572fSBarry Smith   } else {
2126cb5b572fSBarry Smith     ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr);
2127cb5b572fSBarry Smith     ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr);
2128cb5b572fSBarry Smith   }
2129cb5b572fSBarry Smith   PetscFunctionReturn(0);
2130cb5b572fSBarry Smith }
2131cb5b572fSBarry Smith 
21324a2ae208SSatish Balay #undef __FUNCT__
21334994cf47SJed Brown #define __FUNCT__ "MatSetUp_MPIAIJ"
21344994cf47SJed Brown PetscErrorCode MatSetUp_MPIAIJ(Mat A)
2135273d9f13SBarry Smith {
2136dfbe8321SBarry Smith   PetscErrorCode ierr;
2137273d9f13SBarry Smith 
2138273d9f13SBarry Smith   PetscFunctionBegin;
2139273d9f13SBarry Smith   ierr =  MatMPIAIJSetPreallocation(A,PETSC_DEFAULT,0,PETSC_DEFAULT,0);CHKERRQ(ierr);
2140273d9f13SBarry Smith   PetscFunctionReturn(0);
2141273d9f13SBarry Smith }
2142273d9f13SBarry Smith 
2143001ddc4fSHong Zhang /*
2144001ddc4fSHong Zhang    Computes the number of nonzeros per row needed for preallocation when X and Y
2145001ddc4fSHong Zhang    have different nonzero structure.
2146001ddc4fSHong Zhang */
2147ac90fabeSBarry Smith #undef __FUNCT__
2148001ddc4fSHong Zhang #define __FUNCT__ "MatAXPYGetPreallocation_MPIX_private"
2149001ddc4fSHong 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)
215095b7e79eSJed Brown {
2151001ddc4fSHong Zhang   PetscInt       i,j,k,nzx,nzy;
215295b7e79eSJed Brown 
215395b7e79eSJed Brown   PetscFunctionBegin;
215495b7e79eSJed Brown   /* Set the number of nonzeros in the new matrix */
215595b7e79eSJed Brown   for (i=0; i<m; i++) {
2156001ddc4fSHong Zhang     const PetscInt *xjj = xj+xi[i],*yjj = yj+yi[i];
2157001ddc4fSHong Zhang     nzx = xi[i+1] - xi[i];
2158001ddc4fSHong Zhang     nzy = yi[i+1] - yi[i];
215995b7e79eSJed Brown     nnz[i] = 0;
216095b7e79eSJed Brown     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
2161001ddc4fSHong Zhang       for (; k<nzy && yltog[yjj[k]]<xltog[xjj[j]]; k++) nnz[i]++; /* Catch up to X */
2162001ddc4fSHong Zhang       if (k<nzy && yltog[yjj[k]]==xltog[xjj[j]]) k++;             /* Skip duplicate */
216395b7e79eSJed Brown       nnz[i]++;
216495b7e79eSJed Brown     }
216595b7e79eSJed Brown     for (; k<nzy; k++) nnz[i]++;
216695b7e79eSJed Brown   }
216795b7e79eSJed Brown   PetscFunctionReturn(0);
216895b7e79eSJed Brown }
216995b7e79eSJed Brown 
2170001ddc4fSHong Zhang /* This is the same as MatAXPYGetPreallocation_SeqAIJ, except that the local-to-global map is provided */
2171001ddc4fSHong Zhang #undef __FUNCT__
2172001ddc4fSHong Zhang #define __FUNCT__ "MatAXPYGetPreallocation_MPIAIJ"
2173001ddc4fSHong Zhang static PetscErrorCode MatAXPYGetPreallocation_MPIAIJ(Mat Y,const PetscInt *yltog,Mat X,const PetscInt *xltog,PetscInt *nnz)
2174001ddc4fSHong Zhang {
2175001ddc4fSHong Zhang   PetscErrorCode ierr;
2176001ddc4fSHong Zhang   PetscInt       m = Y->rmap->N;
2177001ddc4fSHong Zhang   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data;
2178001ddc4fSHong Zhang   Mat_SeqAIJ     *y = (Mat_SeqAIJ*)Y->data;
2179001ddc4fSHong Zhang 
2180001ddc4fSHong Zhang   PetscFunctionBegin;
2181001ddc4fSHong Zhang   ierr = MatAXPYGetPreallocation_MPIX_private(m,x->i,x->j,xltog,y->i,y->j,yltog,nnz);CHKERRQ(ierr);
2182001ddc4fSHong Zhang   PetscFunctionReturn(0);
2183001ddc4fSHong Zhang }
2184001ddc4fSHong Zhang 
218595b7e79eSJed Brown #undef __FUNCT__
2186ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_MPIAIJ"
2187f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_MPIAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2188ac90fabeSBarry Smith {
2189dfbe8321SBarry Smith   PetscErrorCode ierr;
2190b1d57f15SBarry Smith   PetscInt       i;
2191ac90fabeSBarry Smith   Mat_MPIAIJ     *xx = (Mat_MPIAIJ*)X->data,*yy = (Mat_MPIAIJ*)Y->data;
21924ce68768SBarry Smith   PetscBLASInt   bnz,one=1;
2193ac90fabeSBarry Smith   Mat_SeqAIJ     *x,*y;
2194ac90fabeSBarry Smith 
2195ac90fabeSBarry Smith   PetscFunctionBegin;
2196ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2197f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2198ac90fabeSBarry Smith     x    = (Mat_SeqAIJ*)xx->A->data;
2199c5df96a5SBarry Smith     ierr = PetscBLASIntCast(x->nz,&bnz);CHKERRQ(ierr);
2200ac90fabeSBarry Smith     y    = (Mat_SeqAIJ*)yy->A->data;
22018b83055fSJed Brown     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2202ac90fabeSBarry Smith     x    = (Mat_SeqAIJ*)xx->B->data;
2203ac90fabeSBarry Smith     y    = (Mat_SeqAIJ*)yy->B->data;
2204c5df96a5SBarry Smith     ierr = PetscBLASIntCast(x->nz,&bnz);CHKERRQ(ierr);
22058b83055fSJed Brown     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2206a3fa217bSJose E. Roman     ierr = PetscObjectStateIncrease((PetscObject)Y);CHKERRQ(ierr);
2207a30b2313SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) {
2208f4df32b1SMatthew Knepley     ierr = MatAXPY_SeqAIJ(yy->A,a,xx->A,str);CHKERRQ(ierr);
2209c537a176SHong Zhang 
2210c537a176SHong Zhang     x = (Mat_SeqAIJ*)xx->B->data;
2211a30b2313SHong Zhang     y = (Mat_SeqAIJ*)yy->B->data;
2212a30b2313SHong Zhang     if (y->xtoy && y->XtoY != xx->B) {
2213a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
22146bf464f9SBarry Smith       ierr = MatDestroy(&y->XtoY);CHKERRQ(ierr);
2215c537a176SHong Zhang     }
2216a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
2217d0f46423SBarry Smith       ierr    = MatAXPYGetxtoy_Private(xx->B->rmap->n,x->i,x->j,xx->garray,y->i,y->j,yy->garray,&y->xtoy);CHKERRQ(ierr);
2218a30b2313SHong Zhang       y->XtoY = xx->B;
2219407f6b05SHong Zhang       ierr    = PetscObjectReference((PetscObject)xx->B);CHKERRQ(ierr);
2220c537a176SHong Zhang     }
2221f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
2222a3fa217bSJose E. Roman     ierr = PetscObjectStateIncrease((PetscObject)Y);CHKERRQ(ierr);
2223ac90fabeSBarry Smith   } else {
22249f5f6813SShri Abhyankar     Mat      B;
22259f5f6813SShri Abhyankar     PetscInt *nnz_d,*nnz_o;
2226785e854fSJed Brown     ierr = PetscMalloc1(yy->A->rmap->N,&nnz_d);CHKERRQ(ierr);
2227785e854fSJed Brown     ierr = PetscMalloc1(yy->B->rmap->N,&nnz_o);CHKERRQ(ierr);
2228ce94432eSBarry Smith     ierr = MatCreate(PetscObjectComm((PetscObject)Y),&B);CHKERRQ(ierr);
2229bc5a2726SShri Abhyankar     ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr);
22309f5f6813SShri Abhyankar     ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr);
223133d57670SJed Brown     ierr = MatSetBlockSizesFromMats(B,Y,Y);CHKERRQ(ierr);
22329f5f6813SShri Abhyankar     ierr = MatSetType(B,MATMPIAIJ);CHKERRQ(ierr);
22339f5f6813SShri Abhyankar     ierr = MatAXPYGetPreallocation_SeqAIJ(yy->A,xx->A,nnz_d);CHKERRQ(ierr);
223495b7e79eSJed Brown     ierr = MatAXPYGetPreallocation_MPIAIJ(yy->B,yy->garray,xx->B,xx->garray,nnz_o);CHKERRQ(ierr);
2235ecd8bba6SJed Brown     ierr = MatMPIAIJSetPreallocation(B,0,nnz_d,0,nnz_o);CHKERRQ(ierr);
22369f5f6813SShri Abhyankar     ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr);
2237a2ea699eSBarry Smith     ierr = MatHeaderReplace(Y,B);CHKERRQ(ierr);
22389f5f6813SShri Abhyankar     ierr = PetscFree(nnz_d);CHKERRQ(ierr);
22399f5f6813SShri Abhyankar     ierr = PetscFree(nnz_o);CHKERRQ(ierr);
2240ac90fabeSBarry Smith   }
2241ac90fabeSBarry Smith   PetscFunctionReturn(0);
2242ac90fabeSBarry Smith }
2243ac90fabeSBarry Smith 
22447087cfbeSBarry Smith extern PetscErrorCode  MatConjugate_SeqAIJ(Mat);
2245354c94deSBarry Smith 
2246354c94deSBarry Smith #undef __FUNCT__
2247354c94deSBarry Smith #define __FUNCT__ "MatConjugate_MPIAIJ"
22487087cfbeSBarry Smith PetscErrorCode  MatConjugate_MPIAIJ(Mat mat)
2249354c94deSBarry Smith {
2250354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2251354c94deSBarry Smith   PetscErrorCode ierr;
2252354c94deSBarry Smith   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
2253354c94deSBarry Smith 
2254354c94deSBarry Smith   PetscFunctionBegin;
2255354c94deSBarry Smith   ierr = MatConjugate_SeqAIJ(aij->A);CHKERRQ(ierr);
2256354c94deSBarry Smith   ierr = MatConjugate_SeqAIJ(aij->B);CHKERRQ(ierr);
2257354c94deSBarry Smith #else
2258354c94deSBarry Smith   PetscFunctionBegin;
2259354c94deSBarry Smith #endif
2260354c94deSBarry Smith   PetscFunctionReturn(0);
2261354c94deSBarry Smith }
2262354c94deSBarry Smith 
226399cafbc1SBarry Smith #undef __FUNCT__
226499cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_MPIAIJ"
226599cafbc1SBarry Smith PetscErrorCode MatRealPart_MPIAIJ(Mat A)
226699cafbc1SBarry Smith {
226799cafbc1SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
226899cafbc1SBarry Smith   PetscErrorCode ierr;
226999cafbc1SBarry Smith 
227099cafbc1SBarry Smith   PetscFunctionBegin;
227199cafbc1SBarry Smith   ierr = MatRealPart(a->A);CHKERRQ(ierr);
227299cafbc1SBarry Smith   ierr = MatRealPart(a->B);CHKERRQ(ierr);
227399cafbc1SBarry Smith   PetscFunctionReturn(0);
227499cafbc1SBarry Smith }
227599cafbc1SBarry Smith 
227699cafbc1SBarry Smith #undef __FUNCT__
227799cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_MPIAIJ"
227899cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_MPIAIJ(Mat A)
227999cafbc1SBarry Smith {
228099cafbc1SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
228199cafbc1SBarry Smith   PetscErrorCode ierr;
228299cafbc1SBarry Smith 
228399cafbc1SBarry Smith   PetscFunctionBegin;
228499cafbc1SBarry Smith   ierr = MatImaginaryPart(a->A);CHKERRQ(ierr);
228599cafbc1SBarry Smith   ierr = MatImaginaryPart(a->B);CHKERRQ(ierr);
228699cafbc1SBarry Smith   PetscFunctionReturn(0);
228799cafbc1SBarry Smith }
228899cafbc1SBarry Smith 
2289519f805aSKarl Rupp #if defined(PETSC_HAVE_PBGL)
2290103bf8bdSMatthew Knepley 
2291103bf8bdSMatthew Knepley #include <boost/parallel/mpi/bsp_process_group.hpp>
2292a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_default_graph.hpp>
2293a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_0_block.hpp>
2294a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_preconditioner.hpp>
2295103bf8bdSMatthew Knepley #include <boost/graph/distributed/petsc/interface.hpp>
2296a2c909beSMatthew Knepley #include <boost/multi_array.hpp>
2297d0f46423SBarry Smith #include <boost/parallel/distributed_property_map->hpp>
2298103bf8bdSMatthew Knepley 
2299103bf8bdSMatthew Knepley #undef __FUNCT__
2300103bf8bdSMatthew Knepley #define __FUNCT__ "MatILUFactorSymbolic_MPIAIJ"
2301103bf8bdSMatthew Knepley /*
2302103bf8bdSMatthew Knepley   This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu>
2303103bf8bdSMatthew Knepley */
23040481f469SBarry Smith PetscErrorCode MatILUFactorSymbolic_MPIAIJ(Mat fact,Mat A, IS isrow, IS iscol, const MatFactorInfo *info)
2305103bf8bdSMatthew Knepley {
2306a2c909beSMatthew Knepley   namespace petsc = boost::distributed::petsc;
2307a2c909beSMatthew Knepley 
2308a2c909beSMatthew Knepley   namespace graph_dist = boost::graph::distributed;
2309a2c909beSMatthew Knepley   using boost::graph::distributed::ilu_default::process_group_type;
2310a2c909beSMatthew Knepley   using boost::graph::ilu_permuted;
2311a2c909beSMatthew Knepley 
2312ace3abfcSBarry Smith   PetscBool      row_identity, col_identity;
2313776b82aeSLisandro Dalcin   PetscContainer c;
2314103bf8bdSMatthew Knepley   PetscInt       m, n, M, N;
2315103bf8bdSMatthew Knepley   PetscErrorCode ierr;
2316103bf8bdSMatthew Knepley 
2317103bf8bdSMatthew Knepley   PetscFunctionBegin;
2318e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels = 0 supported for parallel ilu");
2319103bf8bdSMatthew Knepley   ierr = ISIdentity(isrow, &row_identity);CHKERRQ(ierr);
2320103bf8bdSMatthew Knepley   ierr = ISIdentity(iscol, &col_identity);CHKERRQ(ierr);
2321f23aa3ddSBarry Smith   if (!row_identity || !col_identity) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Row and column permutations must be identity for parallel ILU");
2322103bf8bdSMatthew Knepley 
2323103bf8bdSMatthew Knepley   process_group_type pg;
2324a2c909beSMatthew Knepley   typedef graph_dist::ilu_default::ilu_level_graph_type lgraph_type;
2325a2c909beSMatthew Knepley   lgraph_type  *lgraph_p   = new lgraph_type(petsc::num_global_vertices(A), pg, petsc::matrix_distribution(A, pg));
2326a2c909beSMatthew Knepley   lgraph_type& level_graph = *lgraph_p;
2327a2c909beSMatthew Knepley   graph_dist::ilu_default::graph_type&            graph(level_graph.graph);
2328a2c909beSMatthew Knepley 
2329103bf8bdSMatthew Knepley   petsc::read_matrix(A, graph, get(boost::edge_weight, graph));
2330a2c909beSMatthew Knepley   ilu_permuted(level_graph);
2331103bf8bdSMatthew Knepley 
2332103bf8bdSMatthew Knepley   /* put together the new matrix */
2333ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A), fact);CHKERRQ(ierr);
2334103bf8bdSMatthew Knepley   ierr = MatGetLocalSize(A, &m, &n);CHKERRQ(ierr);
2335103bf8bdSMatthew Knepley   ierr = MatGetSize(A, &M, &N);CHKERRQ(ierr);
2336719d5645SBarry Smith   ierr = MatSetSizes(fact, m, n, M, N);CHKERRQ(ierr);
233733d57670SJed Brown   ierr = MatSetBlockSizesFromMats(fact,A,A);CHKERRQ(ierr);
2338719d5645SBarry Smith   ierr = MatSetType(fact, ((PetscObject)A)->type_name);CHKERRQ(ierr);
2339719d5645SBarry Smith   ierr = MatAssemblyBegin(fact, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2340719d5645SBarry Smith   ierr = MatAssemblyEnd(fact, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2341103bf8bdSMatthew Knepley 
2342ce94432eSBarry Smith   ierr = PetscContainerCreate(PetscObjectComm((PetscObject)A), &c);
2343776b82aeSLisandro Dalcin   ierr = PetscContainerSetPointer(c, lgraph_p);
2344719d5645SBarry Smith   ierr = PetscObjectCompose((PetscObject) (fact), "graph", (PetscObject) c);
2345bf0cc555SLisandro Dalcin   ierr = PetscContainerDestroy(&c);
2346103bf8bdSMatthew Knepley   PetscFunctionReturn(0);
2347103bf8bdSMatthew Knepley }
2348103bf8bdSMatthew Knepley 
2349103bf8bdSMatthew Knepley #undef __FUNCT__
2350103bf8bdSMatthew Knepley #define __FUNCT__ "MatLUFactorNumeric_MPIAIJ"
23510481f469SBarry Smith PetscErrorCode MatLUFactorNumeric_MPIAIJ(Mat B,Mat A, const MatFactorInfo *info)
2352103bf8bdSMatthew Knepley {
2353103bf8bdSMatthew Knepley   PetscFunctionBegin;
2354103bf8bdSMatthew Knepley   PetscFunctionReturn(0);
2355103bf8bdSMatthew Knepley }
2356103bf8bdSMatthew Knepley 
2357103bf8bdSMatthew Knepley #undef __FUNCT__
2358103bf8bdSMatthew Knepley #define __FUNCT__ "MatSolve_MPIAIJ"
2359103bf8bdSMatthew Knepley /*
2360103bf8bdSMatthew Knepley   This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu>
2361103bf8bdSMatthew Knepley */
2362103bf8bdSMatthew Knepley PetscErrorCode MatSolve_MPIAIJ(Mat A, Vec b, Vec x)
2363103bf8bdSMatthew Knepley {
2364a2c909beSMatthew Knepley   namespace graph_dist = boost::graph::distributed;
2365a2c909beSMatthew Knepley 
2366a2c909beSMatthew Knepley   typedef graph_dist::ilu_default::ilu_level_graph_type lgraph_type;
2367a2c909beSMatthew Knepley   lgraph_type    *lgraph_p;
2368776b82aeSLisandro Dalcin   PetscContainer c;
2369103bf8bdSMatthew Knepley   PetscErrorCode ierr;
2370103bf8bdSMatthew Knepley 
2371103bf8bdSMatthew Knepley   PetscFunctionBegin;
2372103bf8bdSMatthew Knepley   ierr = PetscObjectQuery((PetscObject) A, "graph", (PetscObject*) &c);CHKERRQ(ierr);
2373776b82aeSLisandro Dalcin   ierr = PetscContainerGetPointer(c, (void**) &lgraph_p);CHKERRQ(ierr);
2374103bf8bdSMatthew Knepley   ierr = VecCopy(b, x);CHKERRQ(ierr);
2375a2c909beSMatthew Knepley 
2376a2c909beSMatthew Knepley   PetscScalar *array_x;
2377a2c909beSMatthew Knepley   ierr = VecGetArray(x, &array_x);CHKERRQ(ierr);
2378a2c909beSMatthew Knepley   PetscInt sx;
2379a2c909beSMatthew Knepley   ierr = VecGetSize(x, &sx);CHKERRQ(ierr);
2380a2c909beSMatthew Knepley 
2381a2c909beSMatthew Knepley   PetscScalar *array_b;
2382a2c909beSMatthew Knepley   ierr = VecGetArray(b, &array_b);CHKERRQ(ierr);
2383a2c909beSMatthew Knepley   PetscInt sb;
2384a2c909beSMatthew Knepley   ierr = VecGetSize(b, &sb);CHKERRQ(ierr);
2385a2c909beSMatthew Knepley 
2386a2c909beSMatthew Knepley   lgraph_type& level_graph = *lgraph_p;
2387a2c909beSMatthew Knepley   graph_dist::ilu_default::graph_type&            graph(level_graph.graph);
2388a2c909beSMatthew Knepley 
2389a2c909beSMatthew Knepley   typedef boost::multi_array_ref<PetscScalar, 1> array_ref_type;
23902205254eSKarl Rupp   array_ref_type                                 ref_b(array_b, boost::extents[num_vertices(graph)]);
23912205254eSKarl Rupp   array_ref_type                                 ref_x(array_x, boost::extents[num_vertices(graph)]);
2392a2c909beSMatthew Knepley 
2393a2c909beSMatthew Knepley   typedef boost::iterator_property_map<array_ref_type::iterator,
2394a2c909beSMatthew Knepley                                        boost::property_map<graph_dist::ilu_default::graph_type, boost::vertex_index_t>::type>  gvector_type;
23952205254eSKarl Rupp   gvector_type                                   vector_b(ref_b.begin(), get(boost::vertex_index, graph));
23962205254eSKarl Rupp   gvector_type                                   vector_x(ref_x.begin(), get(boost::vertex_index, graph));
2397a2c909beSMatthew Knepley 
2398a2c909beSMatthew Knepley   ilu_set_solve(*lgraph_p, vector_b, vector_x);
2399103bf8bdSMatthew Knepley   PetscFunctionReturn(0);
2400103bf8bdSMatthew Knepley }
2401103bf8bdSMatthew Knepley #endif
2402103bf8bdSMatthew Knepley 
240369db28dcSHong Zhang 
240469db28dcSHong Zhang #undef __FUNCT__
240522559b1cSHong Zhang #define __FUNCT__ "MatGetRedundantMatrix_MPIAIJ_interlaced"
24067cb6ea77SHong Zhang PetscErrorCode MatGetRedundantMatrix_MPIAIJ_interlaced(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,MatReuse reuse,Mat *matredundant)
2407b4617e5dSHong Zhang {
2408b4617e5dSHong Zhang   PetscMPIInt    rank,size;
24097cb6ea77SHong Zhang   MPI_Comm       comm;
2410b4617e5dSHong Zhang   PetscErrorCode ierr;
241134d19554SHong Zhang   PetscInt       nsends=0,nrecvs=0,i,rownz_max=0,M=mat->rmap->N,N=mat->cmap->N;
24125cc03489SHong Zhang   PetscMPIInt    *send_rank= NULL,*recv_rank=NULL,subrank,subsize;
2413b4617e5dSHong Zhang   PetscInt       *rowrange = mat->rmap->range;
2414b4617e5dSHong Zhang   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
2415b4617e5dSHong Zhang   Mat            A = aij->A,B=aij->B,C=*matredundant;
2416b4617e5dSHong Zhang   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*b=(Mat_SeqAIJ*)B->data;
2417b4617e5dSHong Zhang   PetscScalar    *sbuf_a;
2418b4617e5dSHong Zhang   PetscInt       nzlocal=a->nz+b->nz;
2419b4617e5dSHong Zhang   PetscInt       j,cstart=mat->cmap->rstart,cend=mat->cmap->rend,row,nzA,nzB,ncols,*cworkA,*cworkB;
242034d19554SHong Zhang   PetscInt       rstart=mat->rmap->rstart,rend=mat->rmap->rend,*bmap=aij->garray;
2421b4617e5dSHong Zhang   PetscInt       *cols,ctmp,lwrite,*rptr,l,*sbuf_j;
2422b4617e5dSHong Zhang   MatScalar      *aworkA,*aworkB;
2423b4617e5dSHong Zhang   PetscScalar    *vals;
2424b4617e5dSHong Zhang   PetscMPIInt    tag1,tag2,tag3,imdex;
2425b4617e5dSHong Zhang   MPI_Request    *s_waits1=NULL,*s_waits2=NULL,*s_waits3=NULL;
2426b4617e5dSHong Zhang   MPI_Request    *r_waits1=NULL,*r_waits2=NULL,*r_waits3=NULL;
2427b4617e5dSHong Zhang   MPI_Status     recv_status,*send_status;
2428b4617e5dSHong Zhang   PetscInt       *sbuf_nz=NULL,*rbuf_nz=NULL,count;
2429b4617e5dSHong Zhang   PetscInt       **rbuf_j=NULL;
2430b4617e5dSHong Zhang   PetscScalar    **rbuf_a=NULL;
2431b4617e5dSHong Zhang   Mat_Redundant  *redund =NULL;
2432b4617e5dSHong Zhang 
2433b4617e5dSHong Zhang   PetscFunctionBegin;
2434b4617e5dSHong Zhang   ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr);
2435b4617e5dSHong Zhang   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
2436b4617e5dSHong Zhang   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
24375cc03489SHong Zhang   ierr = MPI_Comm_rank(subcomm,&subrank);CHKERRQ(ierr);
24385cc03489SHong Zhang   ierr = MPI_Comm_size(subcomm,&subsize);CHKERRQ(ierr);
2439d3b23db5SHong Zhang 
2440b4617e5dSHong Zhang   if (reuse == MAT_REUSE_MATRIX) {
2441b4617e5dSHong Zhang     if (M != mat->rmap->N || N != mat->cmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong global size");
24425cc03489SHong Zhang     if (subsize == 1) {
24435cc03489SHong Zhang       Mat_SeqAIJ *c = (Mat_SeqAIJ*)C->data;
24445cc03489SHong Zhang       redund = c->redundant;
24455cc03489SHong Zhang     } else {
24465cc03489SHong Zhang       Mat_MPIAIJ *c = (Mat_MPIAIJ*)C->data;
24475cc03489SHong Zhang       redund = c->redundant;
24485cc03489SHong Zhang     }
2449b4617e5dSHong Zhang     if (nzlocal != redund->nzlocal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong nzlocal");
2450b4617e5dSHong Zhang 
2451b4617e5dSHong Zhang     nsends    = redund->nsends;
2452b4617e5dSHong Zhang     nrecvs    = redund->nrecvs;
2453b4617e5dSHong Zhang     send_rank = redund->send_rank;
2454b4617e5dSHong Zhang     recv_rank = redund->recv_rank;
2455b4617e5dSHong Zhang     sbuf_nz   = redund->sbuf_nz;
2456b4617e5dSHong Zhang     rbuf_nz   = redund->rbuf_nz;
2457b4617e5dSHong Zhang     sbuf_j    = redund->sbuf_j;
2458b4617e5dSHong Zhang     sbuf_a    = redund->sbuf_a;
2459b4617e5dSHong Zhang     rbuf_j    = redund->rbuf_j;
2460b4617e5dSHong Zhang     rbuf_a    = redund->rbuf_a;
2461b4617e5dSHong Zhang   }
2462b4617e5dSHong Zhang 
2463b4617e5dSHong Zhang   if (reuse == MAT_INITIAL_MATRIX) {
2464b4617e5dSHong Zhang     PetscInt    nleftover,np_subcomm;
2465b4617e5dSHong Zhang 
2466b4617e5dSHong Zhang     /* get the destination processors' id send_rank, nsends and nrecvs */
2467dcca6d9dSJed Brown     ierr = PetscMalloc2(size,&send_rank,size,&recv_rank);CHKERRQ(ierr);
2468b4617e5dSHong Zhang 
2469b4617e5dSHong Zhang     np_subcomm = size/nsubcomm;
2470b4617e5dSHong Zhang     nleftover  = size - nsubcomm*np_subcomm;
2471b4617e5dSHong Zhang 
247222559b1cSHong Zhang     /* block of codes below is specific for INTERLACED */
247322559b1cSHong Zhang     /* ------------------------------------------------*/
2474b4617e5dSHong Zhang     nsends = 0; nrecvs = 0;
2475b4617e5dSHong Zhang     for (i=0; i<size; i++) {
2476b4617e5dSHong Zhang       if (subrank == i/nsubcomm && i != rank) { /* my_subrank == other's subrank */
247722559b1cSHong Zhang         send_rank[nsends++] = i;
2478b4617e5dSHong Zhang         recv_rank[nrecvs++] = i;
2479b4617e5dSHong Zhang       }
2480b4617e5dSHong Zhang     }
2481b4617e5dSHong Zhang     if (rank >= size - nleftover) { /* this proc is a leftover processor */
2482b4617e5dSHong Zhang       i = size-nleftover-1;
2483b4617e5dSHong Zhang       j = 0;
2484b4617e5dSHong Zhang       while (j < nsubcomm - nleftover) {
2485b4617e5dSHong Zhang         send_rank[nsends++] = i;
2486b4617e5dSHong Zhang         i--; j++;
2487b4617e5dSHong Zhang       }
2488b4617e5dSHong Zhang     }
2489b4617e5dSHong Zhang 
2490b4617e5dSHong Zhang     if (nleftover && subsize == size/nsubcomm && subrank==subsize-1) { /* this proc recvs from leftover processors */
2491b4617e5dSHong Zhang       for (i=0; i<nleftover; i++) {
2492b4617e5dSHong Zhang         recv_rank[nrecvs++] = size-nleftover+i;
2493b4617e5dSHong Zhang       }
2494b4617e5dSHong Zhang     }
249522559b1cSHong Zhang     /*----------------------------------------------*/
2496b4617e5dSHong Zhang 
2497b4617e5dSHong Zhang     /* allocate sbuf_j, sbuf_a */
2498b4617e5dSHong Zhang     i    = nzlocal + rowrange[rank+1] - rowrange[rank] + 2;
2499785e854fSJed Brown     ierr = PetscMalloc1(i,&sbuf_j);CHKERRQ(ierr);
2500785e854fSJed Brown     ierr = PetscMalloc1((nzlocal+1),&sbuf_a);CHKERRQ(ierr);
2501e37c6257SHong Zhang     /*
2502e37c6257SHong Zhang     ierr = PetscSynchronizedPrintf(comm,"[%d] nsends %d, nrecvs %d\n",rank,nsends,nrecvs);CHKERRQ(ierr);
25030ec8b6e3SBarry Smith     ierr = PetscSynchronizedFlush(comm,PETSC_STDOUT);CHKERRQ(ierr);
2504e37c6257SHong Zhang      */
2505b4617e5dSHong Zhang   } /* endof if (reuse == MAT_INITIAL_MATRIX) */
2506b4617e5dSHong Zhang 
2507b4617e5dSHong Zhang   /* copy mat's local entries into the buffers */
2508b4617e5dSHong Zhang   if (reuse == MAT_INITIAL_MATRIX) {
2509b4617e5dSHong Zhang     rownz_max = 0;
2510b4617e5dSHong Zhang     rptr      = sbuf_j;
2511b4617e5dSHong Zhang     cols      = sbuf_j + rend-rstart + 1;
2512b4617e5dSHong Zhang     vals      = sbuf_a;
2513b4617e5dSHong Zhang     rptr[0]   = 0;
2514b4617e5dSHong Zhang     for (i=0; i<rend-rstart; i++) {
2515b4617e5dSHong Zhang       row    = i + rstart;
2516b4617e5dSHong Zhang       nzA    = a->i[i+1] - a->i[i]; nzB = b->i[i+1] - b->i[i];
2517b4617e5dSHong Zhang       ncols  = nzA + nzB;
2518b4617e5dSHong Zhang       cworkA = a->j + a->i[i]; cworkB = b->j + b->i[i];
2519b4617e5dSHong Zhang       aworkA = a->a + a->i[i]; aworkB = b->a + b->i[i];
2520b4617e5dSHong Zhang       /* load the column indices for this row into cols */
2521b4617e5dSHong Zhang       lwrite = 0;
2522b4617e5dSHong Zhang       for (l=0; l<nzB; l++) {
2523b4617e5dSHong Zhang         if ((ctmp = bmap[cworkB[l]]) < cstart) {
2524b4617e5dSHong Zhang           vals[lwrite]   = aworkB[l];
2525b4617e5dSHong Zhang           cols[lwrite++] = ctmp;
2526b4617e5dSHong Zhang         }
2527b4617e5dSHong Zhang       }
2528b4617e5dSHong Zhang       for (l=0; l<nzA; l++) {
2529b4617e5dSHong Zhang         vals[lwrite]   = aworkA[l];
2530b4617e5dSHong Zhang         cols[lwrite++] = cstart + cworkA[l];
2531b4617e5dSHong Zhang       }
2532b4617e5dSHong Zhang       for (l=0; l<nzB; l++) {
2533b4617e5dSHong Zhang         if ((ctmp = bmap[cworkB[l]]) >= cend) {
2534b4617e5dSHong Zhang           vals[lwrite]   = aworkB[l];
2535b4617e5dSHong Zhang           cols[lwrite++] = ctmp;
2536b4617e5dSHong Zhang         }
2537b4617e5dSHong Zhang       }
2538b4617e5dSHong Zhang       vals     += ncols;
2539b4617e5dSHong Zhang       cols     += ncols;
2540b4617e5dSHong Zhang       rptr[i+1] = rptr[i] + ncols;
2541b4617e5dSHong Zhang       if (rownz_max < ncols) rownz_max = ncols;
2542b4617e5dSHong Zhang     }
2543b4617e5dSHong 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);
2544b4617e5dSHong Zhang   } else { /* only copy matrix values into sbuf_a */
2545b4617e5dSHong Zhang     rptr    = sbuf_j;
2546b4617e5dSHong Zhang     vals    = sbuf_a;
2547b4617e5dSHong Zhang     rptr[0] = 0;
2548b4617e5dSHong Zhang     for (i=0; i<rend-rstart; i++) {
2549b4617e5dSHong Zhang       row    = i + rstart;
2550b4617e5dSHong Zhang       nzA    = a->i[i+1] - a->i[i]; nzB = b->i[i+1] - b->i[i];
2551b4617e5dSHong Zhang       ncols  = nzA + nzB;
2552b4617e5dSHong Zhang       cworkB = b->j + b->i[i];
2553b4617e5dSHong Zhang       aworkA = a->a + a->i[i];
2554b4617e5dSHong Zhang       aworkB = b->a + b->i[i];
2555b4617e5dSHong Zhang       lwrite = 0;
2556b4617e5dSHong Zhang       for (l=0; l<nzB; l++) {
2557b4617e5dSHong Zhang         if ((ctmp = bmap[cworkB[l]]) < cstart) vals[lwrite++] = aworkB[l];
2558b4617e5dSHong Zhang       }
2559b4617e5dSHong Zhang       for (l=0; l<nzA; l++) vals[lwrite++] = aworkA[l];
2560b4617e5dSHong Zhang       for (l=0; l<nzB; l++) {
2561b4617e5dSHong Zhang         if ((ctmp = bmap[cworkB[l]]) >= cend) vals[lwrite++] = aworkB[l];
2562b4617e5dSHong Zhang       }
2563b4617e5dSHong Zhang       vals     += ncols;
2564b4617e5dSHong Zhang       rptr[i+1] = rptr[i] + ncols;
2565b4617e5dSHong Zhang     }
2566b4617e5dSHong Zhang   } /* endof if (reuse == MAT_INITIAL_MATRIX) */
2567b4617e5dSHong Zhang 
2568b4617e5dSHong Zhang   /* send nzlocal to others, and recv other's nzlocal */
2569b4617e5dSHong Zhang   /*--------------------------------------------------*/
2570b4617e5dSHong Zhang   if (reuse == MAT_INITIAL_MATRIX) {
2571dcca6d9dSJed Brown     ierr = PetscMalloc2(3*(nsends + nrecvs)+1,&s_waits3,nsends+1,&send_status);CHKERRQ(ierr);
2572b4617e5dSHong Zhang 
2573b4617e5dSHong Zhang     s_waits2 = s_waits3 + nsends;
2574b4617e5dSHong Zhang     s_waits1 = s_waits2 + nsends;
2575b4617e5dSHong Zhang     r_waits1 = s_waits1 + nsends;
2576b4617e5dSHong Zhang     r_waits2 = r_waits1 + nrecvs;
2577b4617e5dSHong Zhang     r_waits3 = r_waits2 + nrecvs;
2578b4617e5dSHong Zhang   } else {
2579dcca6d9dSJed Brown     ierr = PetscMalloc2(nsends + nrecvs +1,&s_waits3,nsends+1,&send_status);CHKERRQ(ierr);
2580b4617e5dSHong Zhang 
2581b4617e5dSHong Zhang     r_waits3 = s_waits3 + nsends;
2582b4617e5dSHong Zhang   }
2583b4617e5dSHong Zhang 
2584b4617e5dSHong Zhang   ierr = PetscObjectGetNewTag((PetscObject)mat,&tag3);CHKERRQ(ierr);
2585b4617e5dSHong Zhang   if (reuse == MAT_INITIAL_MATRIX) {
2586b4617e5dSHong Zhang     /* get new tags to keep the communication clean */
2587b4617e5dSHong Zhang     ierr = PetscObjectGetNewTag((PetscObject)mat,&tag1);CHKERRQ(ierr);
2588b4617e5dSHong Zhang     ierr = PetscObjectGetNewTag((PetscObject)mat,&tag2);CHKERRQ(ierr);
2589dcca6d9dSJed Brown     ierr = PetscMalloc4(nsends,&sbuf_nz,nrecvs,&rbuf_nz,nrecvs,&rbuf_j,nrecvs,&rbuf_a);CHKERRQ(ierr);
2590b4617e5dSHong Zhang 
2591b4617e5dSHong Zhang     /* post receives of other's nzlocal */
2592b4617e5dSHong Zhang     for (i=0; i<nrecvs; i++) {
2593b4617e5dSHong Zhang       ierr = MPI_Irecv(rbuf_nz+i,1,MPIU_INT,MPI_ANY_SOURCE,tag1,comm,r_waits1+i);CHKERRQ(ierr);
2594b4617e5dSHong Zhang     }
2595b4617e5dSHong Zhang     /* send nzlocal to others */
2596b4617e5dSHong Zhang     for (i=0; i<nsends; i++) {
2597b4617e5dSHong Zhang       sbuf_nz[i] = nzlocal;
2598b4617e5dSHong Zhang       ierr       = MPI_Isend(sbuf_nz+i,1,MPIU_INT,send_rank[i],tag1,comm,s_waits1+i);CHKERRQ(ierr);
2599b4617e5dSHong Zhang     }
2600b4617e5dSHong Zhang     /* wait on receives of nzlocal; allocate space for rbuf_j, rbuf_a */
2601b4617e5dSHong Zhang     count = nrecvs;
2602b4617e5dSHong Zhang     while (count) {
2603b4617e5dSHong Zhang       ierr = MPI_Waitany(nrecvs,r_waits1,&imdex,&recv_status);CHKERRQ(ierr);
2604b4617e5dSHong Zhang 
2605b4617e5dSHong Zhang       recv_rank[imdex] = recv_status.MPI_SOURCE;
2606b4617e5dSHong Zhang       /* allocate rbuf_a and rbuf_j; then post receives of rbuf_j */
2607785e854fSJed Brown       ierr = PetscMalloc1((rbuf_nz[imdex]+1),&rbuf_a[imdex]);CHKERRQ(ierr);
2608b4617e5dSHong Zhang 
2609b4617e5dSHong Zhang       i = rowrange[recv_status.MPI_SOURCE+1] - rowrange[recv_status.MPI_SOURCE]; /* number of expected mat->i */
2610b4617e5dSHong Zhang 
2611b4617e5dSHong Zhang       rbuf_nz[imdex] += i + 2;
2612b4617e5dSHong Zhang 
2613785e854fSJed Brown       ierr = PetscMalloc1(rbuf_nz[imdex],&rbuf_j[imdex]);CHKERRQ(ierr);
2614b4617e5dSHong Zhang       ierr = MPI_Irecv(rbuf_j[imdex],rbuf_nz[imdex],MPIU_INT,recv_status.MPI_SOURCE,tag2,comm,r_waits2+imdex);CHKERRQ(ierr);
2615b4617e5dSHong Zhang       count--;
2616b4617e5dSHong Zhang     }
2617b4617e5dSHong Zhang     /* wait on sends of nzlocal */
2618b4617e5dSHong Zhang     if (nsends) {ierr = MPI_Waitall(nsends,s_waits1,send_status);CHKERRQ(ierr);}
2619b4617e5dSHong Zhang     /* send mat->i,j to others, and recv from other's */
2620b4617e5dSHong Zhang     /*------------------------------------------------*/
2621b4617e5dSHong Zhang     for (i=0; i<nsends; i++) {
2622b4617e5dSHong Zhang       j    = nzlocal + rowrange[rank+1] - rowrange[rank] + 1;
2623b4617e5dSHong Zhang       ierr = MPI_Isend(sbuf_j,j,MPIU_INT,send_rank[i],tag2,comm,s_waits2+i);CHKERRQ(ierr);
2624b4617e5dSHong Zhang     }
2625b4617e5dSHong Zhang     /* wait on receives of mat->i,j */
2626b4617e5dSHong Zhang     /*------------------------------*/
2627b4617e5dSHong Zhang     count = nrecvs;
2628b4617e5dSHong Zhang     while (count) {
2629b4617e5dSHong Zhang       ierr = MPI_Waitany(nrecvs,r_waits2,&imdex,&recv_status);CHKERRQ(ierr);
2630b4617e5dSHong 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);
2631b4617e5dSHong Zhang       count--;
2632b4617e5dSHong Zhang     }
2633b4617e5dSHong Zhang     /* wait on sends of mat->i,j */
2634b4617e5dSHong Zhang     /*---------------------------*/
2635b4617e5dSHong Zhang     if (nsends) {
2636b4617e5dSHong Zhang       ierr = MPI_Waitall(nsends,s_waits2,send_status);CHKERRQ(ierr);
2637b4617e5dSHong Zhang     }
2638b4617e5dSHong Zhang   } /* endof if (reuse == MAT_INITIAL_MATRIX) */
2639b4617e5dSHong Zhang 
2640b4617e5dSHong Zhang   /* post receives, send and receive mat->a */
2641b4617e5dSHong Zhang   /*----------------------------------------*/
2642b4617e5dSHong Zhang   for (imdex=0; imdex<nrecvs; imdex++) {
2643b4617e5dSHong Zhang     ierr = MPI_Irecv(rbuf_a[imdex],rbuf_nz[imdex],MPIU_SCALAR,recv_rank[imdex],tag3,comm,r_waits3+imdex);CHKERRQ(ierr);
2644b4617e5dSHong Zhang   }
2645b4617e5dSHong Zhang   for (i=0; i<nsends; i++) {
2646b4617e5dSHong Zhang     ierr = MPI_Isend(sbuf_a,nzlocal,MPIU_SCALAR,send_rank[i],tag3,comm,s_waits3+i);CHKERRQ(ierr);
2647b4617e5dSHong Zhang   }
2648b4617e5dSHong Zhang   count = nrecvs;
2649b4617e5dSHong Zhang   while (count) {
2650b4617e5dSHong Zhang     ierr = MPI_Waitany(nrecvs,r_waits3,&imdex,&recv_status);CHKERRQ(ierr);
2651b4617e5dSHong 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);
2652b4617e5dSHong Zhang     count--;
2653b4617e5dSHong Zhang   }
2654b4617e5dSHong Zhang   if (nsends) {
2655b4617e5dSHong Zhang     ierr = MPI_Waitall(nsends,s_waits3,send_status);CHKERRQ(ierr);
2656b4617e5dSHong Zhang   }
2657b4617e5dSHong Zhang 
2658b4617e5dSHong Zhang   ierr = PetscFree2(s_waits3,send_status);CHKERRQ(ierr);
2659b4617e5dSHong Zhang 
2660b4617e5dSHong Zhang   /* create redundant matrix */
2661b4617e5dSHong Zhang   /*-------------------------*/
2662b4617e5dSHong Zhang   if (reuse == MAT_INITIAL_MATRIX) {
266319171117SHong Zhang     const PetscInt *range;
266419171117SHong Zhang     PetscInt       rstart_sub,rend_sub,mloc_sub;
266519171117SHong Zhang 
2666b4617e5dSHong Zhang     /* compute rownz_max for preallocation */
2667b4617e5dSHong Zhang     for (imdex=0; imdex<nrecvs; imdex++) {
2668b4617e5dSHong Zhang       j    = rowrange[recv_rank[imdex]+1] - rowrange[recv_rank[imdex]];
2669b4617e5dSHong Zhang       rptr = rbuf_j[imdex];
2670b4617e5dSHong Zhang       for (i=0; i<j; i++) {
2671b4617e5dSHong Zhang         ncols = rptr[i+1] - rptr[i];
2672b4617e5dSHong Zhang         if (rownz_max < ncols) rownz_max = ncols;
2673b4617e5dSHong Zhang       }
2674b4617e5dSHong Zhang     }
2675b4617e5dSHong Zhang 
2676b4617e5dSHong Zhang     ierr = MatCreate(subcomm,&C);CHKERRQ(ierr);
267719171117SHong Zhang 
267819171117SHong Zhang     /* get local size of redundant matrix
267919171117SHong Zhang        - mloc_sub is chosen for PETSC_SUBCOMM_INTERLACED, works for other types, but may not efficient! */
268019171117SHong Zhang     ierr = MatGetOwnershipRanges(mat,&range);CHKERRQ(ierr);
268119171117SHong Zhang     rstart_sub = range[nsubcomm*subrank];
268219171117SHong Zhang     if (subrank+1 < subsize) { /* not the last proc in subcomm */
268319171117SHong Zhang       rend_sub = range[nsubcomm*(subrank+1)];
268419171117SHong Zhang     } else {
268519171117SHong Zhang       rend_sub = mat->rmap->N;
268619171117SHong Zhang     }
268719171117SHong Zhang     mloc_sub = rend_sub - rstart_sub;
268819171117SHong Zhang 
268934d19554SHong Zhang     if (M == N) {
2690b4617e5dSHong Zhang       ierr = MatSetSizes(C,mloc_sub,mloc_sub,PETSC_DECIDE,PETSC_DECIDE);CHKERRQ(ierr);
269134d19554SHong Zhang     } else { /* non-square matrix */
269234d19554SHong Zhang       ierr = MatSetSizes(C,mloc_sub,PETSC_DECIDE,PETSC_DECIDE,mat->cmap->N);CHKERRQ(ierr);
269334d19554SHong Zhang     }
269433d57670SJed Brown     ierr = MatSetBlockSizesFromMats(C,mat,mat);CHKERRQ(ierr);
2695b4617e5dSHong Zhang     ierr = MatSetFromOptions(C);CHKERRQ(ierr);
2696b4617e5dSHong Zhang     ierr = MatSeqAIJSetPreallocation(C,rownz_max,NULL);CHKERRQ(ierr);
2697b4617e5dSHong Zhang     ierr = MatMPIAIJSetPreallocation(C,rownz_max,NULL,rownz_max,NULL);CHKERRQ(ierr);
2698b4617e5dSHong Zhang   } else {
2699b4617e5dSHong Zhang     C = *matredundant;
2700b4617e5dSHong Zhang   }
2701b4617e5dSHong Zhang 
2702b4617e5dSHong Zhang   /* insert local matrix entries */
2703b4617e5dSHong Zhang   rptr = sbuf_j;
2704b4617e5dSHong Zhang   cols = sbuf_j + rend-rstart + 1;
2705b4617e5dSHong Zhang   vals = sbuf_a;
2706b4617e5dSHong Zhang   for (i=0; i<rend-rstart; i++) {
2707b4617e5dSHong Zhang     row   = i + rstart;
2708b4617e5dSHong Zhang     ncols = rptr[i+1] - rptr[i];
2709b4617e5dSHong Zhang     ierr  = MatSetValues(C,1,&row,ncols,cols,vals,INSERT_VALUES);CHKERRQ(ierr);
2710b4617e5dSHong Zhang     vals += ncols;
2711b4617e5dSHong Zhang     cols += ncols;
2712b4617e5dSHong Zhang   }
2713b4617e5dSHong Zhang   /* insert received matrix entries */
2714b4617e5dSHong Zhang   for (imdex=0; imdex<nrecvs; imdex++) {
2715b4617e5dSHong Zhang     rstart = rowrange[recv_rank[imdex]];
2716b4617e5dSHong Zhang     rend   = rowrange[recv_rank[imdex]+1];
2717e37c6257SHong Zhang     /* printf("[%d] insert rows %d - %d\n",rank,rstart,rend-1); */
2718b4617e5dSHong Zhang     rptr   = rbuf_j[imdex];
2719b4617e5dSHong Zhang     cols   = rbuf_j[imdex] + rend-rstart + 1;
2720b4617e5dSHong Zhang     vals   = rbuf_a[imdex];
2721b4617e5dSHong Zhang     for (i=0; i<rend-rstart; i++) {
2722b4617e5dSHong Zhang       row   = i + rstart;
2723b4617e5dSHong Zhang       ncols = rptr[i+1] - rptr[i];
2724b4617e5dSHong Zhang       ierr  = MatSetValues(C,1,&row,ncols,cols,vals,INSERT_VALUES);CHKERRQ(ierr);
2725b4617e5dSHong Zhang       vals += ncols;
2726b4617e5dSHong Zhang       cols += ncols;
2727b4617e5dSHong Zhang     }
2728b4617e5dSHong Zhang   }
2729b4617e5dSHong Zhang   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2730b4617e5dSHong Zhang   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2731b4617e5dSHong Zhang 
2732b4617e5dSHong Zhang   if (reuse == MAT_INITIAL_MATRIX) {
2733b4617e5dSHong Zhang     *matredundant = C;
27345cc03489SHong Zhang 
2735b4617e5dSHong Zhang     /* create a supporting struct and attach it to C for reuse */
2736b00a9115SJed Brown     ierr = PetscNewLog(C,&redund);CHKERRQ(ierr);
27375cc03489SHong Zhang     if (subsize == 1) {
27385cc03489SHong Zhang       Mat_SeqAIJ *c = (Mat_SeqAIJ*)C->data;
27395cc03489SHong Zhang       c->redundant = redund;
27405cc03489SHong Zhang     } else {
27415cc03489SHong Zhang       Mat_MPIAIJ *c = (Mat_MPIAIJ*)C->data;
27425cc03489SHong Zhang       c->redundant = redund;
27435cc03489SHong Zhang     }
2744b4617e5dSHong Zhang 
2745b4617e5dSHong Zhang     redund->nzlocal   = nzlocal;
2746b4617e5dSHong Zhang     redund->nsends    = nsends;
2747b4617e5dSHong Zhang     redund->nrecvs    = nrecvs;
2748b4617e5dSHong Zhang     redund->send_rank = send_rank;
2749b4617e5dSHong Zhang     redund->recv_rank = recv_rank;
2750b4617e5dSHong Zhang     redund->sbuf_nz   = sbuf_nz;
2751b4617e5dSHong Zhang     redund->rbuf_nz   = rbuf_nz;
2752b4617e5dSHong Zhang     redund->sbuf_j    = sbuf_j;
2753b4617e5dSHong Zhang     redund->sbuf_a    = sbuf_a;
2754b4617e5dSHong Zhang     redund->rbuf_j    = rbuf_j;
2755b4617e5dSHong Zhang     redund->rbuf_a    = rbuf_a;
27560b291e46SHong Zhang     redund->psubcomm  = NULL;
2757b4617e5dSHong Zhang   }
2758b4617e5dSHong Zhang   PetscFunctionReturn(0);
2759b4617e5dSHong Zhang }
2760b4617e5dSHong Zhang 
2761b4617e5dSHong Zhang #undef __FUNCT__
276269db28dcSHong Zhang #define __FUNCT__ "MatGetRedundantMatrix_MPIAIJ"
2763b2bf6370SHong Zhang PetscErrorCode MatGetRedundantMatrix_MPIAIJ(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,MatReuse reuse,Mat *matredundant)
276469db28dcSHong Zhang {
2765f38d543fSHong Zhang   PetscErrorCode ierr;
2766c79c5527SHong Zhang   MPI_Comm       comm;
2767c79c5527SHong Zhang   PetscMPIInt    size,subsize;
2768c79c5527SHong Zhang   PetscInt       mloc_sub,rstart,rend,M=mat->rmap->N,N=mat->cmap->N;
2769c79c5527SHong Zhang   Mat_Redundant  *redund=NULL;
27701f2d8ef4SHong Zhang   PetscSubcomm   psubcomm=NULL;
2771473f7991SHong Zhang   MPI_Comm       subcomm_in=subcomm;
27721f2d8ef4SHong Zhang   Mat            *matseq;
27731f2d8ef4SHong Zhang   IS             isrow,iscol;
277469db28dcSHong Zhang 
277569db28dcSHong Zhang   PetscFunctionBegin;
27761f2d8ef4SHong Zhang   if (subcomm_in == MPI_COMM_NULL) { /* user does not provide subcomm */
2777c79c5527SHong Zhang     if (reuse ==  MAT_INITIAL_MATRIX) {
27781f2d8ef4SHong Zhang       /* create psubcomm, then get subcomm */
2779ce94432eSBarry Smith       ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr);
278069db28dcSHong Zhang       ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
27817cb6ea77SHong Zhang       if (nsubcomm < 1 || nsubcomm > size) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"nsubcomm must between 1 and %D",size);
27827cb6ea77SHong Zhang 
2783d3b23db5SHong Zhang       ierr = PetscSubcommCreate(comm,&psubcomm);CHKERRQ(ierr);
2784d3b23db5SHong Zhang       ierr = PetscSubcommSetNumber(psubcomm,nsubcomm);CHKERRQ(ierr);
2785c79c5527SHong Zhang       ierr = PetscSubcommSetType(psubcomm,PETSC_SUBCOMM_CONTIGUOUS);CHKERRQ(ierr);
278619171117SHong Zhang       ierr = PetscSubcommSetFromOptions(psubcomm);CHKERRQ(ierr);
2787c79c5527SHong Zhang       subcomm = psubcomm->comm;
27887cb6ea77SHong Zhang     } else { /* retrieve psubcomm and subcomm */
2789c79c5527SHong Zhang       ierr = PetscObjectGetComm((PetscObject)(*matredundant),&subcomm);CHKERRQ(ierr);
2790c79c5527SHong Zhang       ierr = MPI_Comm_size(subcomm,&subsize);CHKERRQ(ierr);
2791c79c5527SHong Zhang       if (subsize == 1) {
2792c79c5527SHong Zhang         Mat_SeqAIJ *c = (Mat_SeqAIJ*)(*matredundant)->data;
27937cb6ea77SHong Zhang         redund = c->redundant;
2794c79c5527SHong Zhang       } else {
2795c79c5527SHong Zhang         Mat_MPIAIJ *c = (Mat_MPIAIJ*)(*matredundant)->data;
27967cb6ea77SHong Zhang         redund = c->redundant;
2797c79c5527SHong Zhang       }
27987cb6ea77SHong Zhang       psubcomm = redund->psubcomm;
2799fd7037dcSHong Zhang     }
28001f2d8ef4SHong Zhang     if (psubcomm->type == PETSC_SUBCOMM_INTERLACED) {
28017cb6ea77SHong Zhang       ierr = MatGetRedundantMatrix_MPIAIJ_interlaced(mat,nsubcomm,subcomm,reuse,matredundant);CHKERRQ(ierr);
2802a3ca3016SBarry Smith       if (reuse ==  MAT_INITIAL_MATRIX) { /* psubcomm is created in this routine, free it in MatDestroy_Redundant() */
28031f2d8ef4SHong Zhang         ierr = MPI_Comm_size(psubcomm->comm,&subsize);CHKERRQ(ierr);
28041f2d8ef4SHong Zhang         if (subsize == 1) {
28051f2d8ef4SHong Zhang           Mat_SeqAIJ *c = (Mat_SeqAIJ*)(*matredundant)->data;
28061f2d8ef4SHong Zhang           c->redundant->psubcomm = psubcomm;
28071f2d8ef4SHong Zhang         } else {
28081f2d8ef4SHong Zhang           Mat_MPIAIJ *c = (Mat_MPIAIJ*)(*matredundant)->data;
28091f2d8ef4SHong Zhang           c->redundant->psubcomm = psubcomm ;
28101f2d8ef4SHong Zhang         }
28111f2d8ef4SHong Zhang       }
28121f2d8ef4SHong Zhang       PetscFunctionReturn(0);
2813c79c5527SHong Zhang     }
2814c79c5527SHong Zhang   }
2815e37c6257SHong Zhang 
28161f2d8ef4SHong Zhang   /* use MPI subcomm via MatGetSubMatrices(); use subcomm_in or psubcomm->comm (psubcomm->type != INTERLACED) */
28177cb6ea77SHong Zhang   ierr = MPI_Comm_size(subcomm,&subsize);CHKERRQ(ierr);
2818c79c5527SHong Zhang   if (reuse == MAT_INITIAL_MATRIX) {
2819c79c5527SHong Zhang     /* create a local sequential matrix matseq[0] */
2820c79c5527SHong Zhang     mloc_sub = PETSC_DECIDE;
2821c79c5527SHong Zhang     ierr = PetscSplitOwnership(subcomm,&mloc_sub,&M);CHKERRQ(ierr);
2822c79c5527SHong Zhang     ierr = MPI_Scan(&mloc_sub,&rend,1,MPIU_INT,MPI_SUM,subcomm);CHKERRQ(ierr);
2823c79c5527SHong Zhang     rstart = rend - mloc_sub;
2824c79c5527SHong Zhang     ierr = ISCreateStride(PETSC_COMM_SELF,mloc_sub,rstart,1,&isrow);CHKERRQ(ierr);
2825c79c5527SHong Zhang     ierr = ISCreateStride(PETSC_COMM_SELF,N,0,1,&iscol);CHKERRQ(ierr);
2826c79c5527SHong Zhang   } else { /* reuse == MAT_REUSE_MATRIX */
2827c79c5527SHong Zhang     if (subsize == 1) {
2828c79c5527SHong Zhang       Mat_SeqAIJ *c = (Mat_SeqAIJ*)(*matredundant)->data;
2829c79c5527SHong Zhang       redund = c->redundant;
2830c79c5527SHong Zhang     } else {
2831c79c5527SHong Zhang       Mat_MPIAIJ *c = (Mat_MPIAIJ*)(*matredundant)->data;
2832c79c5527SHong Zhang       redund = c->redundant;
2833c79c5527SHong Zhang     }
2834c79c5527SHong Zhang 
2835c79c5527SHong Zhang     isrow  = redund->isrow;
2836c79c5527SHong Zhang     iscol  = redund->iscol;
2837c79c5527SHong Zhang     matseq = redund->matseq;
2838c79c5527SHong Zhang   }
2839c79c5527SHong Zhang   ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,reuse,&matseq);CHKERRQ(ierr);
2840c79c5527SHong Zhang   ierr = MatCreateMPIAIJConcatenateSeqAIJ(subcomm,matseq[0],PETSC_DECIDE,reuse,matredundant);CHKERRQ(ierr);
2841c79c5527SHong Zhang 
2842c79c5527SHong Zhang   if (reuse == MAT_INITIAL_MATRIX) {
2843c79c5527SHong Zhang     /* create a supporting struct and attach it to C for reuse */
2844b00a9115SJed Brown     ierr = PetscNewLog(*matredundant,&redund);CHKERRQ(ierr);
2845c79c5527SHong Zhang     if (subsize == 1) {
2846c79c5527SHong Zhang       Mat_SeqAIJ *c = (Mat_SeqAIJ*)(*matredundant)->data;
2847c79c5527SHong Zhang       c->redundant = redund;
2848c79c5527SHong Zhang     } else {
2849c79c5527SHong Zhang       Mat_MPIAIJ *c = (Mat_MPIAIJ*)(*matredundant)->data;
2850c79c5527SHong Zhang       c->redundant = redund;
2851c79c5527SHong Zhang     }
2852c79c5527SHong Zhang     redund->isrow    = isrow;
2853c79c5527SHong Zhang     redund->iscol    = iscol;
2854c79c5527SHong Zhang     redund->matseq   = matseq;
28551f2d8ef4SHong Zhang     redund->psubcomm = psubcomm;
2856c79c5527SHong Zhang   }
285769db28dcSHong Zhang   PetscFunctionReturn(0);
285869db28dcSHong Zhang }
285969db28dcSHong Zhang 
286003bc72f1SMatthew Knepley #undef __FUNCT__
2861c91732d9SHong Zhang #define __FUNCT__ "MatGetRowMaxAbs_MPIAIJ"
2862c91732d9SHong Zhang PetscErrorCode MatGetRowMaxAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[])
2863c91732d9SHong Zhang {
2864c91732d9SHong Zhang   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
2865c91732d9SHong Zhang   PetscErrorCode ierr;
2866c91732d9SHong Zhang   PetscInt       i,*idxb = 0;
2867c91732d9SHong Zhang   PetscScalar    *va,*vb;
2868c91732d9SHong Zhang   Vec            vtmp;
2869c91732d9SHong Zhang 
2870c91732d9SHong Zhang   PetscFunctionBegin;
2871c91732d9SHong Zhang   ierr = MatGetRowMaxAbs(a->A,v,idx);CHKERRQ(ierr);
2872c91732d9SHong Zhang   ierr = VecGetArray(v,&va);CHKERRQ(ierr);
2873c91732d9SHong Zhang   if (idx) {
2874192daf7cSBarry Smith     for (i=0; i<A->rmap->n; i++) {
2875d0f46423SBarry Smith       if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart;
2876c91732d9SHong Zhang     }
2877c91732d9SHong Zhang   }
2878c91732d9SHong Zhang 
2879d0f46423SBarry Smith   ierr = VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);CHKERRQ(ierr);
2880c91732d9SHong Zhang   if (idx) {
2881785e854fSJed Brown     ierr = PetscMalloc1(A->rmap->n,&idxb);CHKERRQ(ierr);
2882c91732d9SHong Zhang   }
2883c91732d9SHong Zhang   ierr = MatGetRowMaxAbs(a->B,vtmp,idxb);CHKERRQ(ierr);
2884c91732d9SHong Zhang   ierr = VecGetArray(vtmp,&vb);CHKERRQ(ierr);
2885c91732d9SHong Zhang 
2886d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
2887c91732d9SHong Zhang     if (PetscAbsScalar(va[i]) < PetscAbsScalar(vb[i])) {
2888c91732d9SHong Zhang       va[i] = vb[i];
2889c91732d9SHong Zhang       if (idx) idx[i] = a->garray[idxb[i]];
2890c91732d9SHong Zhang     }
2891c91732d9SHong Zhang   }
2892c91732d9SHong Zhang 
2893c91732d9SHong Zhang   ierr = VecRestoreArray(v,&va);CHKERRQ(ierr);
2894c91732d9SHong Zhang   ierr = VecRestoreArray(vtmp,&vb);CHKERRQ(ierr);
2895c91732d9SHong Zhang   ierr = PetscFree(idxb);CHKERRQ(ierr);
28966bf464f9SBarry Smith   ierr = VecDestroy(&vtmp);CHKERRQ(ierr);
2897c91732d9SHong Zhang   PetscFunctionReturn(0);
2898c91732d9SHong Zhang }
2899c91732d9SHong Zhang 
2900c91732d9SHong Zhang #undef __FUNCT__
2901c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_MPIAIJ"
2902c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[])
2903c87e5d42SMatthew Knepley {
2904c87e5d42SMatthew Knepley   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
2905c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2906c87e5d42SMatthew Knepley   PetscInt       i,*idxb = 0;
2907c87e5d42SMatthew Knepley   PetscScalar    *va,*vb;
2908c87e5d42SMatthew Knepley   Vec            vtmp;
2909c87e5d42SMatthew Knepley 
2910c87e5d42SMatthew Knepley   PetscFunctionBegin;
2911c87e5d42SMatthew Knepley   ierr = MatGetRowMinAbs(a->A,v,idx);CHKERRQ(ierr);
2912c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&va);CHKERRQ(ierr);
2913c87e5d42SMatthew Knepley   if (idx) {
2914c87e5d42SMatthew Knepley     for (i=0; i<A->cmap->n; i++) {
2915c87e5d42SMatthew Knepley       if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart;
2916c87e5d42SMatthew Knepley     }
2917c87e5d42SMatthew Knepley   }
2918c87e5d42SMatthew Knepley 
2919c87e5d42SMatthew Knepley   ierr = VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);CHKERRQ(ierr);
2920c87e5d42SMatthew Knepley   if (idx) {
2921785e854fSJed Brown     ierr = PetscMalloc1(A->rmap->n,&idxb);CHKERRQ(ierr);
2922c87e5d42SMatthew Knepley   }
2923c87e5d42SMatthew Knepley   ierr = MatGetRowMinAbs(a->B,vtmp,idxb);CHKERRQ(ierr);
2924c87e5d42SMatthew Knepley   ierr = VecGetArray(vtmp,&vb);CHKERRQ(ierr);
2925c87e5d42SMatthew Knepley 
2926c87e5d42SMatthew Knepley   for (i=0; i<A->rmap->n; i++) {
2927c87e5d42SMatthew Knepley     if (PetscAbsScalar(va[i]) > PetscAbsScalar(vb[i])) {
2928c87e5d42SMatthew Knepley       va[i] = vb[i];
2929c87e5d42SMatthew Knepley       if (idx) idx[i] = a->garray[idxb[i]];
2930c87e5d42SMatthew Knepley     }
2931c87e5d42SMatthew Knepley   }
2932c87e5d42SMatthew Knepley 
2933c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&va);CHKERRQ(ierr);
2934c87e5d42SMatthew Knepley   ierr = VecRestoreArray(vtmp,&vb);CHKERRQ(ierr);
2935c87e5d42SMatthew Knepley   ierr = PetscFree(idxb);CHKERRQ(ierr);
29366bf464f9SBarry Smith   ierr = VecDestroy(&vtmp);CHKERRQ(ierr);
2937c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
2938c87e5d42SMatthew Knepley }
2939c87e5d42SMatthew Knepley 
2940c87e5d42SMatthew Knepley #undef __FUNCT__
294103bc72f1SMatthew Knepley #define __FUNCT__ "MatGetRowMin_MPIAIJ"
294203bc72f1SMatthew Knepley PetscErrorCode MatGetRowMin_MPIAIJ(Mat A, Vec v, PetscInt idx[])
294303bc72f1SMatthew Knepley {
294403bc72f1SMatthew Knepley   Mat_MPIAIJ     *mat   = (Mat_MPIAIJ*) A->data;
2945d0f46423SBarry Smith   PetscInt       n      = A->rmap->n;
2946d0f46423SBarry Smith   PetscInt       cstart = A->cmap->rstart;
294703bc72f1SMatthew Knepley   PetscInt       *cmap  = mat->garray;
294803bc72f1SMatthew Knepley   PetscInt       *diagIdx, *offdiagIdx;
294903bc72f1SMatthew Knepley   Vec            diagV, offdiagV;
295003bc72f1SMatthew Knepley   PetscScalar    *a, *diagA, *offdiagA;
295103bc72f1SMatthew Knepley   PetscInt       r;
295203bc72f1SMatthew Knepley   PetscErrorCode ierr;
295303bc72f1SMatthew Knepley 
295403bc72f1SMatthew Knepley   PetscFunctionBegin;
2955dcca6d9dSJed Brown   ierr = PetscMalloc2(n,&diagIdx,n,&offdiagIdx);CHKERRQ(ierr);
2956ce94432eSBarry Smith   ierr = VecCreateSeq(PetscObjectComm((PetscObject)A), n, &diagV);CHKERRQ(ierr);
2957ce94432eSBarry Smith   ierr = VecCreateSeq(PetscObjectComm((PetscObject)A), n, &offdiagV);CHKERRQ(ierr);
295803bc72f1SMatthew Knepley   ierr = MatGetRowMin(mat->A, diagV,    diagIdx);CHKERRQ(ierr);
295903bc72f1SMatthew Knepley   ierr = MatGetRowMin(mat->B, offdiagV, offdiagIdx);CHKERRQ(ierr);
296003bc72f1SMatthew Knepley   ierr = VecGetArray(v,        &a);CHKERRQ(ierr);
296103bc72f1SMatthew Knepley   ierr = VecGetArray(diagV,    &diagA);CHKERRQ(ierr);
296203bc72f1SMatthew Knepley   ierr = VecGetArray(offdiagV, &offdiagA);CHKERRQ(ierr);
296303bc72f1SMatthew Knepley   for (r = 0; r < n; ++r) {
2964028cd4eaSSatish Balay     if (PetscAbsScalar(diagA[r]) <= PetscAbsScalar(offdiagA[r])) {
296503bc72f1SMatthew Knepley       a[r]   = diagA[r];
296603bc72f1SMatthew Knepley       idx[r] = cstart + diagIdx[r];
296703bc72f1SMatthew Knepley     } else {
296803bc72f1SMatthew Knepley       a[r]   = offdiagA[r];
296903bc72f1SMatthew Knepley       idx[r] = cmap[offdiagIdx[r]];
297003bc72f1SMatthew Knepley     }
297103bc72f1SMatthew Knepley   }
297203bc72f1SMatthew Knepley   ierr = VecRestoreArray(v,        &a);CHKERRQ(ierr);
297303bc72f1SMatthew Knepley   ierr = VecRestoreArray(diagV,    &diagA);CHKERRQ(ierr);
297403bc72f1SMatthew Knepley   ierr = VecRestoreArray(offdiagV, &offdiagA);CHKERRQ(ierr);
29756bf464f9SBarry Smith   ierr = VecDestroy(&diagV);CHKERRQ(ierr);
29766bf464f9SBarry Smith   ierr = VecDestroy(&offdiagV);CHKERRQ(ierr);
297703bc72f1SMatthew Knepley   ierr = PetscFree2(diagIdx, offdiagIdx);CHKERRQ(ierr);
297803bc72f1SMatthew Knepley   PetscFunctionReturn(0);
297903bc72f1SMatthew Knepley }
298003bc72f1SMatthew Knepley 
29815494a064SHong Zhang #undef __FUNCT__
2982c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMax_MPIAIJ"
2983c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMax_MPIAIJ(Mat A, Vec v, PetscInt idx[])
2984c87e5d42SMatthew Knepley {
2985c87e5d42SMatthew Knepley   Mat_MPIAIJ     *mat   = (Mat_MPIAIJ*) A->data;
2986c87e5d42SMatthew Knepley   PetscInt       n      = A->rmap->n;
2987c87e5d42SMatthew Knepley   PetscInt       cstart = A->cmap->rstart;
2988c87e5d42SMatthew Knepley   PetscInt       *cmap  = mat->garray;
2989c87e5d42SMatthew Knepley   PetscInt       *diagIdx, *offdiagIdx;
2990c87e5d42SMatthew Knepley   Vec            diagV, offdiagV;
2991c87e5d42SMatthew Knepley   PetscScalar    *a, *diagA, *offdiagA;
2992c87e5d42SMatthew Knepley   PetscInt       r;
2993c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2994c87e5d42SMatthew Knepley 
2995c87e5d42SMatthew Knepley   PetscFunctionBegin;
2996dcca6d9dSJed Brown   ierr = PetscMalloc2(n,&diagIdx,n,&offdiagIdx);CHKERRQ(ierr);
2997d11e49fbSSatish Balay   ierr = VecCreateSeq(PETSC_COMM_SELF, n, &diagV);CHKERRQ(ierr);
2998d11e49fbSSatish Balay   ierr = VecCreateSeq(PETSC_COMM_SELF, n, &offdiagV);CHKERRQ(ierr);
2999c87e5d42SMatthew Knepley   ierr = MatGetRowMax(mat->A, diagV,    diagIdx);CHKERRQ(ierr);
3000c87e5d42SMatthew Knepley   ierr = MatGetRowMax(mat->B, offdiagV, offdiagIdx);CHKERRQ(ierr);
3001c87e5d42SMatthew Knepley   ierr = VecGetArray(v,        &a);CHKERRQ(ierr);
3002c87e5d42SMatthew Knepley   ierr = VecGetArray(diagV,    &diagA);CHKERRQ(ierr);
3003c87e5d42SMatthew Knepley   ierr = VecGetArray(offdiagV, &offdiagA);CHKERRQ(ierr);
3004c87e5d42SMatthew Knepley   for (r = 0; r < n; ++r) {
3005c87e5d42SMatthew Knepley     if (PetscAbsScalar(diagA[r]) >= PetscAbsScalar(offdiagA[r])) {
3006c87e5d42SMatthew Knepley       a[r]   = diagA[r];
3007c87e5d42SMatthew Knepley       idx[r] = cstart + diagIdx[r];
3008c87e5d42SMatthew Knepley     } else {
3009c87e5d42SMatthew Knepley       a[r]   = offdiagA[r];
3010c87e5d42SMatthew Knepley       idx[r] = cmap[offdiagIdx[r]];
3011c87e5d42SMatthew Knepley     }
3012c87e5d42SMatthew Knepley   }
3013c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,        &a);CHKERRQ(ierr);
3014c87e5d42SMatthew Knepley   ierr = VecRestoreArray(diagV,    &diagA);CHKERRQ(ierr);
3015c87e5d42SMatthew Knepley   ierr = VecRestoreArray(offdiagV, &offdiagA);CHKERRQ(ierr);
30166bf464f9SBarry Smith   ierr = VecDestroy(&diagV);CHKERRQ(ierr);
30176bf464f9SBarry Smith   ierr = VecDestroy(&offdiagV);CHKERRQ(ierr);
3018c87e5d42SMatthew Knepley   ierr = PetscFree2(diagIdx, offdiagIdx);CHKERRQ(ierr);
3019c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
3020c87e5d42SMatthew Knepley }
3021c87e5d42SMatthew Knepley 
3022c87e5d42SMatthew Knepley #undef __FUNCT__
3023d1adec66SJed Brown #define __FUNCT__ "MatGetSeqNonzeroStructure_MPIAIJ"
3024d1adec66SJed Brown PetscErrorCode MatGetSeqNonzeroStructure_MPIAIJ(Mat mat,Mat *newmat)
30255494a064SHong Zhang {
30265494a064SHong Zhang   PetscErrorCode ierr;
3027f6d58c54SBarry Smith   Mat            *dummy;
30285494a064SHong Zhang 
30295494a064SHong Zhang   PetscFunctionBegin;
3030f6d58c54SBarry Smith   ierr    = MatGetSubMatrix_MPIAIJ_All(mat,MAT_DO_NOT_GET_VALUES,MAT_INITIAL_MATRIX,&dummy);CHKERRQ(ierr);
3031f6d58c54SBarry Smith   *newmat = *dummy;
3032f6d58c54SBarry Smith   ierr    = PetscFree(dummy);CHKERRQ(ierr);
30335494a064SHong Zhang   PetscFunctionReturn(0);
30345494a064SHong Zhang }
30355494a064SHong Zhang 
3036bbead8a2SBarry Smith #undef __FUNCT__
3037bbead8a2SBarry Smith #define __FUNCT__ "MatInvertBlockDiagonal_MPIAIJ"
3038713ccfa9SJed Brown PetscErrorCode  MatInvertBlockDiagonal_MPIAIJ(Mat A,const PetscScalar **values)
3039bbead8a2SBarry Smith {
3040bbead8a2SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*) A->data;
3041bbead8a2SBarry Smith   PetscErrorCode ierr;
3042bbead8a2SBarry Smith 
3043bbead8a2SBarry Smith   PetscFunctionBegin;
3044bbead8a2SBarry Smith   ierr = MatInvertBlockDiagonal(a->A,values);CHKERRQ(ierr);
3045bbead8a2SBarry Smith   PetscFunctionReturn(0);
3046bbead8a2SBarry Smith }
3047bbead8a2SBarry Smith 
304873a71a0fSBarry Smith #undef __FUNCT__
304973a71a0fSBarry Smith #define __FUNCT__ "MatSetRandom_MPIAIJ"
305073a71a0fSBarry Smith static PetscErrorCode  MatSetRandom_MPIAIJ(Mat x,PetscRandom rctx)
305173a71a0fSBarry Smith {
305273a71a0fSBarry Smith   PetscErrorCode ierr;
305373a71a0fSBarry Smith   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)x->data;
305473a71a0fSBarry Smith 
305573a71a0fSBarry Smith   PetscFunctionBegin;
305673a71a0fSBarry Smith   ierr = MatSetRandom(aij->A,rctx);CHKERRQ(ierr);
305773a71a0fSBarry Smith   ierr = MatSetRandom(aij->B,rctx);CHKERRQ(ierr);
305873a71a0fSBarry Smith   ierr = MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
305973a71a0fSBarry Smith   ierr = MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
306073a71a0fSBarry Smith   PetscFunctionReturn(0);
306173a71a0fSBarry Smith }
3062bbead8a2SBarry Smith 
30638a729477SBarry Smith /* -------------------------------------------------------------------*/
3064cda55fadSBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ,
3065cda55fadSBarry Smith                                        MatGetRow_MPIAIJ,
3066cda55fadSBarry Smith                                        MatRestoreRow_MPIAIJ,
3067cda55fadSBarry Smith                                        MatMult_MPIAIJ,
306897304618SKris Buschelman                                 /* 4*/ MatMultAdd_MPIAIJ,
30697c922b88SBarry Smith                                        MatMultTranspose_MPIAIJ,
30707c922b88SBarry Smith                                        MatMultTransposeAdd_MPIAIJ,
3071519f805aSKarl Rupp #if defined(PETSC_HAVE_PBGL)
3072103bf8bdSMatthew Knepley                                        MatSolve_MPIAIJ,
3073103bf8bdSMatthew Knepley #else
3074cda55fadSBarry Smith                                        0,
3075103bf8bdSMatthew Knepley #endif
3076cda55fadSBarry Smith                                        0,
3077cda55fadSBarry Smith                                        0,
307897304618SKris Buschelman                                 /*10*/ 0,
3079cda55fadSBarry Smith                                        0,
3080cda55fadSBarry Smith                                        0,
308141f059aeSBarry Smith                                        MatSOR_MPIAIJ,
3082b7c46309SBarry Smith                                        MatTranspose_MPIAIJ,
308397304618SKris Buschelman                                 /*15*/ MatGetInfo_MPIAIJ,
3084cda55fadSBarry Smith                                        MatEqual_MPIAIJ,
3085cda55fadSBarry Smith                                        MatGetDiagonal_MPIAIJ,
3086cda55fadSBarry Smith                                        MatDiagonalScale_MPIAIJ,
3087cda55fadSBarry Smith                                        MatNorm_MPIAIJ,
308897304618SKris Buschelman                                 /*20*/ MatAssemblyBegin_MPIAIJ,
3089cda55fadSBarry Smith                                        MatAssemblyEnd_MPIAIJ,
3090cda55fadSBarry Smith                                        MatSetOption_MPIAIJ,
3091cda55fadSBarry Smith                                        MatZeroEntries_MPIAIJ,
3092d519adbfSMatthew Knepley                                 /*24*/ MatZeroRows_MPIAIJ,
3093cda55fadSBarry Smith                                        0,
3094519f805aSKarl Rupp #if defined(PETSC_HAVE_PBGL)
3095719d5645SBarry Smith                                        0,
3096103bf8bdSMatthew Knepley #else
3097cda55fadSBarry Smith                                        0,
3098103bf8bdSMatthew Knepley #endif
3099cda55fadSBarry Smith                                        0,
3100cda55fadSBarry Smith                                        0,
31014994cf47SJed Brown                                 /*29*/ MatSetUp_MPIAIJ,
3102519f805aSKarl Rupp #if defined(PETSC_HAVE_PBGL)
3103719d5645SBarry Smith                                        0,
3104103bf8bdSMatthew Knepley #else
3105cda55fadSBarry Smith                                        0,
3106103bf8bdSMatthew Knepley #endif
3107cda55fadSBarry Smith                                        0,
3108cda55fadSBarry Smith                                        0,
3109cda55fadSBarry Smith                                        0,
3110d519adbfSMatthew Knepley                                 /*34*/ MatDuplicate_MPIAIJ,
3111cda55fadSBarry Smith                                        0,
3112cda55fadSBarry Smith                                        0,
3113cda55fadSBarry Smith                                        0,
3114cda55fadSBarry Smith                                        0,
3115d519adbfSMatthew Knepley                                 /*39*/ MatAXPY_MPIAIJ,
3116cda55fadSBarry Smith                                        MatGetSubMatrices_MPIAIJ,
3117cda55fadSBarry Smith                                        MatIncreaseOverlap_MPIAIJ,
3118cda55fadSBarry Smith                                        MatGetValues_MPIAIJ,
3119cb5b572fSBarry Smith                                        MatCopy_MPIAIJ,
3120d519adbfSMatthew Knepley                                 /*44*/ MatGetRowMax_MPIAIJ,
3121cda55fadSBarry Smith                                        MatScale_MPIAIJ,
3122cda55fadSBarry Smith                                        0,
312399e65526SBarry Smith                                        MatDiagonalSet_MPIAIJ,
3124564f14d6SBarry Smith                                        MatZeroRowsColumns_MPIAIJ,
312573a71a0fSBarry Smith                                 /*49*/ MatSetRandom_MPIAIJ,
3126cda55fadSBarry Smith                                        0,
3127cda55fadSBarry Smith                                        0,
3128cda55fadSBarry Smith                                        0,
3129cda55fadSBarry Smith                                        0,
313093dfae19SHong Zhang                                 /*54*/ MatFDColoringCreate_MPIXAIJ,
3131cda55fadSBarry Smith                                        0,
3132cda55fadSBarry Smith                                        MatSetUnfactored_MPIAIJ,
313372e6a0cfSJed Brown                                        MatPermute_MPIAIJ,
3134cda55fadSBarry Smith                                        0,
3135d519adbfSMatthew Knepley                                 /*59*/ MatGetSubMatrix_MPIAIJ,
3136e03a110bSBarry Smith                                        MatDestroy_MPIAIJ,
3137e03a110bSBarry Smith                                        MatView_MPIAIJ,
3138357abbc8SBarry Smith                                        0,
3139f996eeb8SHong Zhang                                        MatMatMatMult_MPIAIJ_MPIAIJ_MPIAIJ,
3140f996eeb8SHong Zhang                                 /*64*/ MatMatMatMultSymbolic_MPIAIJ_MPIAIJ_MPIAIJ,
3141f996eeb8SHong Zhang                                        MatMatMatMultNumeric_MPIAIJ_MPIAIJ_MPIAIJ,
3142a2243be0SBarry Smith                                        0,
3143a2243be0SBarry Smith                                        0,
3144a2243be0SBarry Smith                                        0,
3145d519adbfSMatthew Knepley                                 /*69*/ MatGetRowMaxAbs_MPIAIJ,
3146c87e5d42SMatthew Knepley                                        MatGetRowMinAbs_MPIAIJ,
3147a2243be0SBarry Smith                                        0,
3148a2243be0SBarry Smith                                        MatSetColoring_MPIAIJ,
3149dcf5cc72SBarry Smith                                        0,
315097304618SKris Buschelman                                        MatSetValuesAdifor_MPIAIJ,
31513acb8795SBarry Smith                                 /*75*/ MatFDColoringApply_AIJ,
315297304618SKris Buschelman                                        0,
315397304618SKris Buschelman                                        0,
315497304618SKris Buschelman                                        0,
3155f1f41ecbSJed Brown                                        MatFindZeroDiagonals_MPIAIJ,
315697304618SKris Buschelman                                 /*80*/ 0,
315797304618SKris Buschelman                                        0,
315897304618SKris Buschelman                                        0,
31595bba2384SShri Abhyankar                                 /*83*/ MatLoad_MPIAIJ,
31606284ec50SHong Zhang                                        0,
31616284ec50SHong Zhang                                        0,
31626284ec50SHong Zhang                                        0,
31636284ec50SHong Zhang                                        0,
3164865e5f61SKris Buschelman                                        0,
3165d519adbfSMatthew Knepley                                 /*89*/ MatMatMult_MPIAIJ_MPIAIJ,
316626be0446SHong Zhang                                        MatMatMultSymbolic_MPIAIJ_MPIAIJ,
316726be0446SHong Zhang                                        MatMatMultNumeric_MPIAIJ_MPIAIJ,
3168cf3ca8ceSHong Zhang                                        MatPtAP_MPIAIJ_MPIAIJ,
3169cf3ca8ceSHong Zhang                                        MatPtAPSymbolic_MPIAIJ_MPIAIJ,
3170cf3ca8ceSHong Zhang                                 /*94*/ MatPtAPNumeric_MPIAIJ_MPIAIJ,
31717a7894deSKris Buschelman                                        0,
31727a7894deSKris Buschelman                                        0,
31737a7894deSKris Buschelman                                        0,
31747a7894deSKris Buschelman                                        0,
3175d519adbfSMatthew Knepley                                 /*99*/ 0,
3176d2b207f1SPeter Brune                                        0,
3177d2b207f1SPeter Brune                                        0,
31782fd7e33dSBarry Smith                                        MatConjugate_MPIAIJ,
31792fd7e33dSBarry Smith                                        0,
3180d519adbfSMatthew Knepley                                 /*104*/MatSetValuesRow_MPIAIJ,
318199cafbc1SBarry Smith                                        MatRealPart_MPIAIJ,
318269db28dcSHong Zhang                                        MatImaginaryPart_MPIAIJ,
318369db28dcSHong Zhang                                        0,
318469db28dcSHong Zhang                                        0,
3185d519adbfSMatthew Knepley                                 /*109*/0,
318603bc72f1SMatthew Knepley                                        MatGetRedundantMatrix_MPIAIJ,
31875494a064SHong Zhang                                        MatGetRowMin_MPIAIJ,
31885494a064SHong Zhang                                        0,
31895494a064SHong Zhang                                        0,
3190d1adec66SJed Brown                                 /*114*/MatGetSeqNonzeroStructure_MPIAIJ,
3191bd0c2dcbSBarry Smith                                        0,
3192bd0c2dcbSBarry Smith                                        0,
3193bd0c2dcbSBarry Smith                                        0,
3194bd0c2dcbSBarry Smith                                        0,
31958fb81238SShri Abhyankar                                 /*119*/0,
31968fb81238SShri Abhyankar                                        0,
31978fb81238SShri Abhyankar                                        0,
3198d6037b41SHong Zhang                                        0,
3199b9614d88SDmitry Karpeev                                        MatGetMultiProcBlock_MPIAIJ,
3200f2c98031SJed Brown                                 /*124*/MatFindNonzeroRows_MPIAIJ,
32010716a85fSBarry Smith                                        MatGetColumnNorms_MPIAIJ,
3202bbead8a2SBarry Smith                                        MatInvertBlockDiagonal_MPIAIJ,
3203b9614d88SDmitry Karpeev                                        0,
320437868618SMatthew G Knepley                                        MatGetSubMatricesParallel_MPIAIJ,
3205187b3c17SHong Zhang                                 /*129*/0,
3206187b3c17SHong Zhang                                        MatTransposeMatMult_MPIAIJ_MPIAIJ,
3207187b3c17SHong Zhang                                        MatTransposeMatMultSymbolic_MPIAIJ_MPIAIJ,
3208187b3c17SHong Zhang                                        MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ,
3209187b3c17SHong Zhang                                        0,
3210187b3c17SHong Zhang                                 /*134*/0,
3211187b3c17SHong Zhang                                        0,
3212187b3c17SHong Zhang                                        0,
3213187b3c17SHong Zhang                                        0,
32143964eb88SJed Brown                                        0,
32153964eb88SJed Brown                                 /*139*/0,
3216f9426fe0SMark Adams                                        0,
3217f86b9fbaSHong Zhang                                        0,
3218f86b9fbaSHong Zhang                                        MatFDColoringSetUp_MPIXAIJ
3219bd0c2dcbSBarry Smith };
322036ce4990SBarry Smith 
32212e8a6d31SBarry Smith /* ----------------------------------------------------------------------------------------*/
32222e8a6d31SBarry Smith 
32234a2ae208SSatish Balay #undef __FUNCT__
32244a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_MPIAIJ"
32257087cfbeSBarry Smith PetscErrorCode  MatStoreValues_MPIAIJ(Mat mat)
32262e8a6d31SBarry Smith {
32272e8a6d31SBarry Smith   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
3228dfbe8321SBarry Smith   PetscErrorCode ierr;
32292e8a6d31SBarry Smith 
32302e8a6d31SBarry Smith   PetscFunctionBegin;
32312e8a6d31SBarry Smith   ierr = MatStoreValues(aij->A);CHKERRQ(ierr);
32322e8a6d31SBarry Smith   ierr = MatStoreValues(aij->B);CHKERRQ(ierr);
32332e8a6d31SBarry Smith   PetscFunctionReturn(0);
32342e8a6d31SBarry Smith }
32352e8a6d31SBarry Smith 
32364a2ae208SSatish Balay #undef __FUNCT__
32374a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_MPIAIJ"
32387087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_MPIAIJ(Mat mat)
32392e8a6d31SBarry Smith {
32402e8a6d31SBarry Smith   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
3241dfbe8321SBarry Smith   PetscErrorCode ierr;
32422e8a6d31SBarry Smith 
32432e8a6d31SBarry Smith   PetscFunctionBegin;
32442e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr);
32452e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr);
32462e8a6d31SBarry Smith   PetscFunctionReturn(0);
32472e8a6d31SBarry Smith }
32488a729477SBarry Smith 
32494a2ae208SSatish Balay #undef __FUNCT__
3250a23d5eceSKris Buschelman #define __FUNCT__ "MatMPIAIJSetPreallocation_MPIAIJ"
32517087cfbeSBarry Smith PetscErrorCode  MatMPIAIJSetPreallocation_MPIAIJ(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
3252a23d5eceSKris Buschelman {
3253a23d5eceSKris Buschelman   Mat_MPIAIJ     *b;
3254dfbe8321SBarry Smith   PetscErrorCode ierr;
3255a23d5eceSKris Buschelman 
3256a23d5eceSKris Buschelman   PetscFunctionBegin;
325726283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
325826283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3259a23d5eceSKris Buschelman   b = (Mat_MPIAIJ*)B->data;
3260899cda47SBarry Smith 
3261526dfc15SBarry Smith   if (!B->preallocated) {
3262899cda47SBarry Smith     /* Explicitly create 2 MATSEQAIJ matrices. */
3263899cda47SBarry Smith     ierr = MatCreate(PETSC_COMM_SELF,&b->A);CHKERRQ(ierr);
3264d0f46423SBarry Smith     ierr = MatSetSizes(b->A,B->rmap->n,B->cmap->n,B->rmap->n,B->cmap->n);CHKERRQ(ierr);
326533d57670SJed Brown     ierr = MatSetBlockSizesFromMats(b->A,B,B);CHKERRQ(ierr);
3266899cda47SBarry Smith     ierr = MatSetType(b->A,MATSEQAIJ);CHKERRQ(ierr);
32673bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)B,(PetscObject)b->A);CHKERRQ(ierr);
3268899cda47SBarry Smith     ierr = MatCreate(PETSC_COMM_SELF,&b->B);CHKERRQ(ierr);
3269d0f46423SBarry Smith     ierr = MatSetSizes(b->B,B->rmap->n,B->cmap->N,B->rmap->n,B->cmap->N);CHKERRQ(ierr);
327033d57670SJed Brown     ierr = MatSetBlockSizesFromMats(b->B,B,B);CHKERRQ(ierr);
3271899cda47SBarry Smith     ierr = MatSetType(b->B,MATSEQAIJ);CHKERRQ(ierr);
32723bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)B,(PetscObject)b->B);CHKERRQ(ierr);
3273526dfc15SBarry Smith   }
3274899cda47SBarry Smith 
3275c60e587dSKris Buschelman   ierr = MatSeqAIJSetPreallocation(b->A,d_nz,d_nnz);CHKERRQ(ierr);
3276c60e587dSKris Buschelman   ierr = MatSeqAIJSetPreallocation(b->B,o_nz,o_nnz);CHKERRQ(ierr);
3277526dfc15SBarry Smith   B->preallocated = PETSC_TRUE;
3278a23d5eceSKris Buschelman   PetscFunctionReturn(0);
3279a23d5eceSKris Buschelman }
3280a23d5eceSKris Buschelman 
32814a2ae208SSatish Balay #undef __FUNCT__
32824a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_MPIAIJ"
3283dfbe8321SBarry Smith PetscErrorCode MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
3284d6dfbf8fSBarry Smith {
3285d6dfbf8fSBarry Smith   Mat            mat;
3286416022c9SBarry Smith   Mat_MPIAIJ     *a,*oldmat = (Mat_MPIAIJ*)matin->data;
3287dfbe8321SBarry Smith   PetscErrorCode ierr;
3288d6dfbf8fSBarry Smith 
32893a40ed3dSBarry Smith   PetscFunctionBegin;
3290416022c9SBarry Smith   *newmat = 0;
3291ce94432eSBarry Smith   ierr    = MatCreate(PetscObjectComm((PetscObject)matin),&mat);CHKERRQ(ierr);
3292d0f46423SBarry Smith   ierr    = MatSetSizes(mat,matin->rmap->n,matin->cmap->n,matin->rmap->N,matin->cmap->N);CHKERRQ(ierr);
329333d57670SJed Brown   ierr    = MatSetBlockSizesFromMats(mat,matin,matin);CHKERRQ(ierr);
32947adad957SLisandro Dalcin   ierr    = MatSetType(mat,((PetscObject)matin)->type_name);CHKERRQ(ierr);
32951d5dac46SHong Zhang   ierr    = PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
3296273d9f13SBarry Smith   a       = (Mat_MPIAIJ*)mat->data;
3297e1b6402fSHong Zhang 
3298d5f3da31SBarry Smith   mat->factortype   = matin->factortype;
3299c456f294SBarry Smith   mat->assembled    = PETSC_TRUE;
3300e7641de0SSatish Balay   mat->insertmode   = NOT_SET_VALUES;
3301273d9f13SBarry Smith   mat->preallocated = PETSC_TRUE;
3302d6dfbf8fSBarry Smith 
330317699dbbSLois Curfman McInnes   a->size         = oldmat->size;
330417699dbbSLois Curfman McInnes   a->rank         = oldmat->rank;
3305e7641de0SSatish Balay   a->donotstash   = oldmat->donotstash;
3306e7641de0SSatish Balay   a->roworiented  = oldmat->roworiented;
3307e7641de0SSatish Balay   a->rowindices   = 0;
3308bcd2baecSBarry Smith   a->rowvalues    = 0;
3309bcd2baecSBarry Smith   a->getrowactive = PETSC_FALSE;
3310d6dfbf8fSBarry Smith 
33111e1e43feSBarry Smith   ierr = PetscLayoutReference(matin->rmap,&mat->rmap);CHKERRQ(ierr);
33121e1e43feSBarry Smith   ierr = PetscLayoutReference(matin->cmap,&mat->cmap);CHKERRQ(ierr);
3313899cda47SBarry Smith 
33142ee70a88SLois Curfman McInnes   if (oldmat->colmap) {
3315aa482453SBarry Smith #if defined(PETSC_USE_CTABLE)
33160f5bd95cSBarry Smith     ierr = PetscTableCreateCopy(oldmat->colmap,&a->colmap);CHKERRQ(ierr);
3317b1fc9764SSatish Balay #else
3318785e854fSJed Brown     ierr = PetscMalloc1((mat->cmap->N),&a->colmap);CHKERRQ(ierr);
33193bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)mat,(mat->cmap->N)*sizeof(PetscInt));CHKERRQ(ierr);
3320d0f46423SBarry Smith     ierr = PetscMemcpy(a->colmap,oldmat->colmap,(mat->cmap->N)*sizeof(PetscInt));CHKERRQ(ierr);
3321b1fc9764SSatish Balay #endif
3322416022c9SBarry Smith   } else a->colmap = 0;
33233f41c07dSBarry Smith   if (oldmat->garray) {
3324b1d57f15SBarry Smith     PetscInt len;
3325d0f46423SBarry Smith     len  = oldmat->B->cmap->n;
3326785e854fSJed Brown     ierr = PetscMalloc1((len+1),&a->garray);CHKERRQ(ierr);
33273bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)mat,len*sizeof(PetscInt));CHKERRQ(ierr);
3328b1d57f15SBarry Smith     if (len) { ierr = PetscMemcpy(a->garray,oldmat->garray,len*sizeof(PetscInt));CHKERRQ(ierr); }
3329416022c9SBarry Smith   } else a->garray = 0;
3330d6dfbf8fSBarry Smith 
3331416022c9SBarry Smith   ierr    = VecDuplicate(oldmat->lvec,&a->lvec);CHKERRQ(ierr);
33323bb1ff40SBarry Smith   ierr    = PetscLogObjectParent((PetscObject)mat,(PetscObject)a->lvec);CHKERRQ(ierr);
3333a56f8943SBarry Smith   ierr    = VecScatterCopy(oldmat->Mvctx,&a->Mvctx);CHKERRQ(ierr);
33343bb1ff40SBarry Smith   ierr    = PetscLogObjectParent((PetscObject)mat,(PetscObject)a->Mvctx);CHKERRQ(ierr);
33352e8a6d31SBarry Smith   ierr    = MatDuplicate(oldmat->A,cpvalues,&a->A);CHKERRQ(ierr);
33363bb1ff40SBarry Smith   ierr    = PetscLogObjectParent((PetscObject)mat,(PetscObject)a->A);CHKERRQ(ierr);
33372e8a6d31SBarry Smith   ierr    = MatDuplicate(oldmat->B,cpvalues,&a->B);CHKERRQ(ierr);
33383bb1ff40SBarry Smith   ierr    = PetscLogObjectParent((PetscObject)mat,(PetscObject)a->B);CHKERRQ(ierr);
3339140e18c1SBarry Smith   ierr    = PetscFunctionListDuplicate(((PetscObject)matin)->qlist,&((PetscObject)mat)->qlist);CHKERRQ(ierr);
33408a729477SBarry Smith   *newmat = mat;
33413a40ed3dSBarry Smith   PetscFunctionReturn(0);
33428a729477SBarry Smith }
3343416022c9SBarry Smith 
33441a4ee126SBarry Smith 
33451a4ee126SBarry Smith 
33464a2ae208SSatish Balay #undef __FUNCT__
33475bba2384SShri Abhyankar #define __FUNCT__ "MatLoad_MPIAIJ"
3348112444f4SShri Abhyankar PetscErrorCode MatLoad_MPIAIJ(Mat newMat, PetscViewer viewer)
33498fb81238SShri Abhyankar {
33508fb81238SShri Abhyankar   PetscScalar    *vals,*svals;
3351ce94432eSBarry Smith   MPI_Comm       comm;
33528fb81238SShri Abhyankar   PetscErrorCode ierr;
33531a4ee126SBarry Smith   PetscMPIInt    rank,size,tag = ((PetscObject)viewer)->tag;
33548fb81238SShri Abhyankar   PetscInt       i,nz,j,rstart,rend,mmax,maxnz = 0,grows,gcols;
33558fb81238SShri Abhyankar   PetscInt       header[4],*rowlengths = 0,M,N,m,*cols;
33560298fd71SBarry Smith   PetscInt       *ourlens = NULL,*procsnz = NULL,*offlens = NULL,jj,*mycols,*smycols;
33578fb81238SShri Abhyankar   PetscInt       cend,cstart,n,*rowners,sizesset=1;
33588fb81238SShri Abhyankar   int            fd;
335908ea439dSMark F. Adams   PetscInt       bs = 1;
33608fb81238SShri Abhyankar 
33618fb81238SShri Abhyankar   PetscFunctionBegin;
3362ce94432eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
33638fb81238SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
33648fb81238SShri Abhyankar   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
33658fb81238SShri Abhyankar   if (!rank) {
33668fb81238SShri Abhyankar     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
33678fb81238SShri Abhyankar     ierr = PetscBinaryRead(fd,(char*)header,4,PETSC_INT);CHKERRQ(ierr);
33688fb81238SShri Abhyankar     if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
33698fb81238SShri Abhyankar   }
33708fb81238SShri Abhyankar 
33710298fd71SBarry Smith   ierr = PetscOptionsBegin(comm,NULL,"Options for loading SEQAIJ matrix","Mat");CHKERRQ(ierr);
33720298fd71SBarry Smith   ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);CHKERRQ(ierr);
337308ea439dSMark F. Adams   ierr = PetscOptionsEnd();CHKERRQ(ierr);
337408ea439dSMark F. Adams 
33758fb81238SShri Abhyankar   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) sizesset = 0;
33768fb81238SShri Abhyankar 
33778fb81238SShri Abhyankar   ierr = MPI_Bcast(header+1,3,MPIU_INT,0,comm);CHKERRQ(ierr);
33788fb81238SShri Abhyankar   M    = header[1]; N = header[2];
33798fb81238SShri Abhyankar   /* If global rows/cols are set to PETSC_DECIDE, set it to the sizes given in the file */
33808fb81238SShri Abhyankar   if (sizesset && newMat->rmap->N < 0) newMat->rmap->N = M;
33818fb81238SShri Abhyankar   if (sizesset && newMat->cmap->N < 0) newMat->cmap->N = N;
33828fb81238SShri Abhyankar 
33838fb81238SShri Abhyankar   /* If global sizes are set, check if they are consistent with that given in the file */
33848fb81238SShri Abhyankar   if (sizesset) {
33858fb81238SShri Abhyankar     ierr = MatGetSize(newMat,&grows,&gcols);CHKERRQ(ierr);
33868fb81238SShri Abhyankar   }
3387abd38a8fSBarry 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);
3388abd38a8fSBarry 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);
33898fb81238SShri Abhyankar 
339008ea439dSMark F. Adams   /* determine ownership of all (block) rows */
339108ea439dSMark F. Adams   if (M%bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Inconsistent # of rows (%d) and block size (%d)",M,bs);
339208ea439dSMark F. Adams   if (newMat->rmap->n < 0) m = bs*((M/bs)/size + (((M/bs) % size) > rank));    /* PETSC_DECIDE */
33934683f7a4SShri Abhyankar   else m = newMat->rmap->n; /* Set by user */
33948fb81238SShri Abhyankar 
3395785e854fSJed Brown   ierr = PetscMalloc1((size+1),&rowners);CHKERRQ(ierr);
33968fb81238SShri Abhyankar   ierr = MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);CHKERRQ(ierr);
33978fb81238SShri Abhyankar 
33988fb81238SShri Abhyankar   /* First process needs enough room for process with most rows */
33998fb81238SShri Abhyankar   if (!rank) {
34008fb81238SShri Abhyankar     mmax = rowners[1];
34015c4ea359SMatthew G Knepley     for (i=2; i<=size; i++) {
34028fb81238SShri Abhyankar       mmax = PetscMax(mmax, rowners[i]);
34038fb81238SShri Abhyankar     }
34043964eb88SJed Brown   } else mmax = -1;             /* unused, but compilers complain */
34058fb81238SShri Abhyankar 
34068fb81238SShri Abhyankar   rowners[0] = 0;
34078fb81238SShri Abhyankar   for (i=2; i<=size; i++) {
34088fb81238SShri Abhyankar     rowners[i] += rowners[i-1];
34098fb81238SShri Abhyankar   }
34108fb81238SShri Abhyankar   rstart = rowners[rank];
34118fb81238SShri Abhyankar   rend   = rowners[rank+1];
34128fb81238SShri Abhyankar 
34138fb81238SShri Abhyankar   /* distribute row lengths to all processors */
3414dcca6d9dSJed Brown   ierr = PetscMalloc2(m,&ourlens,m,&offlens);CHKERRQ(ierr);
34158fb81238SShri Abhyankar   if (!rank) {
34168fb81238SShri Abhyankar     ierr = PetscBinaryRead(fd,ourlens,m,PETSC_INT);CHKERRQ(ierr);
3417785e854fSJed Brown     ierr = PetscMalloc1(mmax,&rowlengths);CHKERRQ(ierr);
34181795a4d1SJed Brown     ierr = PetscCalloc1(size,&procsnz);CHKERRQ(ierr);
34198fb81238SShri Abhyankar     for (j=0; j<m; j++) {
34208fb81238SShri Abhyankar       procsnz[0] += ourlens[j];
34218fb81238SShri Abhyankar     }
34228fb81238SShri Abhyankar     for (i=1; i<size; i++) {
34238fb81238SShri Abhyankar       ierr = PetscBinaryRead(fd,rowlengths,rowners[i+1]-rowners[i],PETSC_INT);CHKERRQ(ierr);
34248fb81238SShri Abhyankar       /* calculate the number of nonzeros on each processor */
34258fb81238SShri Abhyankar       for (j=0; j<rowners[i+1]-rowners[i]; j++) {
34268fb81238SShri Abhyankar         procsnz[i] += rowlengths[j];
34278fb81238SShri Abhyankar       }
3428a25532f0SBarry Smith       ierr = MPIULong_Send(rowlengths,rowners[i+1]-rowners[i],MPIU_INT,i,tag,comm);CHKERRQ(ierr);
34298fb81238SShri Abhyankar     }
34308fb81238SShri Abhyankar     ierr = PetscFree(rowlengths);CHKERRQ(ierr);
34318fb81238SShri Abhyankar   } else {
3432a25532f0SBarry Smith     ierr = MPIULong_Recv(ourlens,m,MPIU_INT,0,tag,comm);CHKERRQ(ierr);
34338fb81238SShri Abhyankar   }
34348fb81238SShri Abhyankar 
34358fb81238SShri Abhyankar   if (!rank) {
34368fb81238SShri Abhyankar     /* determine max buffer needed and allocate it */
34378fb81238SShri Abhyankar     maxnz = 0;
34388fb81238SShri Abhyankar     for (i=0; i<size; i++) {
34398fb81238SShri Abhyankar       maxnz = PetscMax(maxnz,procsnz[i]);
34408fb81238SShri Abhyankar     }
3441785e854fSJed Brown     ierr = PetscMalloc1(maxnz,&cols);CHKERRQ(ierr);
34428fb81238SShri Abhyankar 
34438fb81238SShri Abhyankar     /* read in my part of the matrix column indices  */
34448fb81238SShri Abhyankar     nz   = procsnz[0];
3445785e854fSJed Brown     ierr = PetscMalloc1(nz,&mycols);CHKERRQ(ierr);
34468fb81238SShri Abhyankar     ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr);
34478fb81238SShri Abhyankar 
34488fb81238SShri Abhyankar     /* read in every one elses and ship off */
34498fb81238SShri Abhyankar     for (i=1; i<size; i++) {
34508fb81238SShri Abhyankar       nz   = procsnz[i];
34518fb81238SShri Abhyankar       ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr);
3452a25532f0SBarry Smith       ierr = MPIULong_Send(cols,nz,MPIU_INT,i,tag,comm);CHKERRQ(ierr);
34538fb81238SShri Abhyankar     }
34548fb81238SShri Abhyankar     ierr = PetscFree(cols);CHKERRQ(ierr);
34558fb81238SShri Abhyankar   } else {
34568fb81238SShri Abhyankar     /* determine buffer space needed for message */
34578fb81238SShri Abhyankar     nz = 0;
34588fb81238SShri Abhyankar     for (i=0; i<m; i++) {
34598fb81238SShri Abhyankar       nz += ourlens[i];
34608fb81238SShri Abhyankar     }
3461785e854fSJed Brown     ierr = PetscMalloc1(nz,&mycols);CHKERRQ(ierr);
34628fb81238SShri Abhyankar 
34638fb81238SShri Abhyankar     /* receive message of column indices*/
3464a25532f0SBarry Smith     ierr = MPIULong_Recv(mycols,nz,MPIU_INT,0,tag,comm);CHKERRQ(ierr);
34658fb81238SShri Abhyankar   }
34668fb81238SShri Abhyankar 
34678fb81238SShri Abhyankar   /* determine column ownership if matrix is not square */
34688fb81238SShri Abhyankar   if (N != M) {
34698fb81238SShri Abhyankar     if (newMat->cmap->n < 0) n = N/size + ((N % size) > rank);
34708fb81238SShri Abhyankar     else n = newMat->cmap->n;
34718fb81238SShri Abhyankar     ierr   = MPI_Scan(&n,&cend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr);
34728fb81238SShri Abhyankar     cstart = cend - n;
34738fb81238SShri Abhyankar   } else {
34748fb81238SShri Abhyankar     cstart = rstart;
34758fb81238SShri Abhyankar     cend   = rend;
34768fb81238SShri Abhyankar     n      = cend - cstart;
34778fb81238SShri Abhyankar   }
34788fb81238SShri Abhyankar 
34798fb81238SShri Abhyankar   /* loop over local rows, determining number of off diagonal entries */
34808fb81238SShri Abhyankar   ierr = PetscMemzero(offlens,m*sizeof(PetscInt));CHKERRQ(ierr);
34818fb81238SShri Abhyankar   jj   = 0;
34828fb81238SShri Abhyankar   for (i=0; i<m; i++) {
34838fb81238SShri Abhyankar     for (j=0; j<ourlens[i]; j++) {
34848fb81238SShri Abhyankar       if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++;
34858fb81238SShri Abhyankar       jj++;
34868fb81238SShri Abhyankar     }
34878fb81238SShri Abhyankar   }
34888fb81238SShri Abhyankar 
34898fb81238SShri Abhyankar   for (i=0; i<m; i++) {
34908fb81238SShri Abhyankar     ourlens[i] -= offlens[i];
34918fb81238SShri Abhyankar   }
34928fb81238SShri Abhyankar   if (!sizesset) {
34938fb81238SShri Abhyankar     ierr = MatSetSizes(newMat,m,n,M,N);CHKERRQ(ierr);
34948fb81238SShri Abhyankar   }
349508ea439dSMark F. Adams 
349608ea439dSMark F. Adams   if (bs > 1) {ierr = MatSetBlockSize(newMat,bs);CHKERRQ(ierr);}
349708ea439dSMark F. Adams 
34988fb81238SShri Abhyankar   ierr = MatMPIAIJSetPreallocation(newMat,0,ourlens,0,offlens);CHKERRQ(ierr);
34998fb81238SShri Abhyankar 
35008fb81238SShri Abhyankar   for (i=0; i<m; i++) {
35018fb81238SShri Abhyankar     ourlens[i] += offlens[i];
35028fb81238SShri Abhyankar   }
35038fb81238SShri Abhyankar 
35048fb81238SShri Abhyankar   if (!rank) {
3505785e854fSJed Brown     ierr = PetscMalloc1((maxnz+1),&vals);CHKERRQ(ierr);
35068fb81238SShri Abhyankar 
35078fb81238SShri Abhyankar     /* read in my part of the matrix numerical values  */
35088fb81238SShri Abhyankar     nz   = procsnz[0];
35098fb81238SShri Abhyankar     ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
35108fb81238SShri Abhyankar 
35118fb81238SShri Abhyankar     /* insert into matrix */
35128fb81238SShri Abhyankar     jj      = rstart;
35138fb81238SShri Abhyankar     smycols = mycols;
35148fb81238SShri Abhyankar     svals   = vals;
35158fb81238SShri Abhyankar     for (i=0; i<m; i++) {
35168fb81238SShri Abhyankar       ierr     = MatSetValues_MPIAIJ(newMat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
35178fb81238SShri Abhyankar       smycols += ourlens[i];
35188fb81238SShri Abhyankar       svals   += ourlens[i];
35198fb81238SShri Abhyankar       jj++;
35208fb81238SShri Abhyankar     }
35218fb81238SShri Abhyankar 
35228fb81238SShri Abhyankar     /* read in other processors and ship out */
35238fb81238SShri Abhyankar     for (i=1; i<size; i++) {
35248fb81238SShri Abhyankar       nz   = procsnz[i];
35258fb81238SShri Abhyankar       ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
3526a25532f0SBarry Smith       ierr = MPIULong_Send(vals,nz,MPIU_SCALAR,i,((PetscObject)newMat)->tag,comm);CHKERRQ(ierr);
35278fb81238SShri Abhyankar     }
35288fb81238SShri Abhyankar     ierr = PetscFree(procsnz);CHKERRQ(ierr);
35298fb81238SShri Abhyankar   } else {
35308fb81238SShri Abhyankar     /* receive numeric values */
3531785e854fSJed Brown     ierr = PetscMalloc1((nz+1),&vals);CHKERRQ(ierr);
35328fb81238SShri Abhyankar 
35338fb81238SShri Abhyankar     /* receive message of values*/
3534a25532f0SBarry Smith     ierr = MPIULong_Recv(vals,nz,MPIU_SCALAR,0,((PetscObject)newMat)->tag,comm);CHKERRQ(ierr);
35358fb81238SShri Abhyankar 
35368fb81238SShri Abhyankar     /* insert into matrix */
35378fb81238SShri Abhyankar     jj      = rstart;
35388fb81238SShri Abhyankar     smycols = mycols;
35398fb81238SShri Abhyankar     svals   = vals;
35408fb81238SShri Abhyankar     for (i=0; i<m; i++) {
35418fb81238SShri Abhyankar       ierr     = MatSetValues_MPIAIJ(newMat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
35428fb81238SShri Abhyankar       smycols += ourlens[i];
35438fb81238SShri Abhyankar       svals   += ourlens[i];
35448fb81238SShri Abhyankar       jj++;
35458fb81238SShri Abhyankar     }
35468fb81238SShri Abhyankar   }
35478fb81238SShri Abhyankar   ierr = PetscFree2(ourlens,offlens);CHKERRQ(ierr);
35488fb81238SShri Abhyankar   ierr = PetscFree(vals);CHKERRQ(ierr);
35498fb81238SShri Abhyankar   ierr = PetscFree(mycols);CHKERRQ(ierr);
35508fb81238SShri Abhyankar   ierr = PetscFree(rowners);CHKERRQ(ierr);
35518fb81238SShri Abhyankar   ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
35528fb81238SShri Abhyankar   ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
35538fb81238SShri Abhyankar   PetscFunctionReturn(0);
35548fb81238SShri Abhyankar }
35558fb81238SShri Abhyankar 
35568fb81238SShri Abhyankar #undef __FUNCT__
35574a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_MPIAIJ"
35584aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,MatReuse call,Mat *newmat)
35594aa3045dSJed Brown {
35604aa3045dSJed Brown   PetscErrorCode ierr;
35614aa3045dSJed Brown   IS             iscol_local;
35624aa3045dSJed Brown   PetscInt       csize;
35634aa3045dSJed Brown 
35644aa3045dSJed Brown   PetscFunctionBegin;
35654aa3045dSJed Brown   ierr = ISGetLocalSize(iscol,&csize);CHKERRQ(ierr);
3566b79d0421SJed Brown   if (call == MAT_REUSE_MATRIX) {
3567b79d0421SJed Brown     ierr = PetscObjectQuery((PetscObject)*newmat,"ISAllGather",(PetscObject*)&iscol_local);CHKERRQ(ierr);
3568e32f2f54SBarry Smith     if (!iscol_local) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
3569b79d0421SJed Brown   } else {
3570c5bfad50SMark F. Adams     PetscInt cbs;
3571c5bfad50SMark F. Adams     ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
35724aa3045dSJed Brown     ierr = ISAllGather(iscol,&iscol_local);CHKERRQ(ierr);
3573c5bfad50SMark F. Adams     ierr = ISSetBlockSize(iscol_local,cbs);CHKERRQ(ierr);
3574b79d0421SJed Brown   }
35754aa3045dSJed Brown   ierr = MatGetSubMatrix_MPIAIJ_Private(mat,isrow,iscol_local,csize,call,newmat);CHKERRQ(ierr);
3576b79d0421SJed Brown   if (call == MAT_INITIAL_MATRIX) {
3577b79d0421SJed Brown     ierr = PetscObjectCompose((PetscObject)*newmat,"ISAllGather",(PetscObject)iscol_local);CHKERRQ(ierr);
35786bf464f9SBarry Smith     ierr = ISDestroy(&iscol_local);CHKERRQ(ierr);
3579b79d0421SJed Brown   }
35804aa3045dSJed Brown   PetscFunctionReturn(0);
35814aa3045dSJed Brown }
35824aa3045dSJed Brown 
358329dcf524SDmitry Karpeev extern PetscErrorCode MatGetSubMatrices_MPIAIJ_Local(Mat,PetscInt,const IS[],const IS[],MatReuse,PetscBool*,Mat*);
35844aa3045dSJed Brown #undef __FUNCT__
35854aa3045dSJed Brown #define __FUNCT__ "MatGetSubMatrix_MPIAIJ_Private"
3586a0ff6018SBarry Smith /*
358729da9460SBarry Smith     Not great since it makes two copies of the submatrix, first an SeqAIJ
358829da9460SBarry Smith   in local and then by concatenating the local matrices the end result.
358929da9460SBarry Smith   Writing it directly would be much like MatGetSubMatrices_MPIAIJ()
35904aa3045dSJed Brown 
35914aa3045dSJed Brown   Note: This requires a sequential iscol with all indices.
3592a0ff6018SBarry Smith */
35934aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIAIJ_Private(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse call,Mat *newmat)
3594a0ff6018SBarry Smith {
3595dfbe8321SBarry Smith   PetscErrorCode ierr;
359632dcc486SBarry Smith   PetscMPIInt    rank,size;
3597a2f3521dSMark F. Adams   PetscInt       i,m,n,rstart,row,rend,nz,*cwork,j,bs,cbs;
359829dcf524SDmitry Karpeev   PetscInt       *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal,ncol;
359929dcf524SDmitry Karpeev   PetscBool      allcolumns, colflag;
360029dcf524SDmitry Karpeev   Mat            M,Mreuse;
3601a77337e4SBarry Smith   MatScalar      *vwork,*aa;
3602ce94432eSBarry Smith   MPI_Comm       comm;
360300e6dbe6SBarry Smith   Mat_SeqAIJ     *aij;
36047e2c5f70SBarry Smith 
3605a0ff6018SBarry Smith   PetscFunctionBegin;
3606ce94432eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr);
36071dab6e02SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
36081dab6e02SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
360900e6dbe6SBarry Smith 
361029dcf524SDmitry Karpeev   ierr = ISIdentity(iscol,&colflag);CHKERRQ(ierr);
361129dcf524SDmitry Karpeev   ierr = ISGetLocalSize(iscol,&ncol);CHKERRQ(ierr);
361229dcf524SDmitry Karpeev   if (colflag && ncol == mat->cmap->N) {
361329dcf524SDmitry Karpeev     allcolumns = PETSC_TRUE;
361429dcf524SDmitry Karpeev   } else {
361529dcf524SDmitry Karpeev     allcolumns = PETSC_FALSE;
361629dcf524SDmitry Karpeev   }
3617fee21e36SBarry Smith   if (call ==  MAT_REUSE_MATRIX) {
3618fee21e36SBarry Smith     ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject*)&Mreuse);CHKERRQ(ierr);
3619e32f2f54SBarry Smith     if (!Mreuse) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
362029dcf524SDmitry Karpeev     ierr = MatGetSubMatrices_MPIAIJ_Local(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&allcolumns,&Mreuse);CHKERRQ(ierr);
3621fee21e36SBarry Smith   } else {
362229dcf524SDmitry Karpeev     ierr = MatGetSubMatrices_MPIAIJ_Local(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&allcolumns,&Mreuse);CHKERRQ(ierr);
3623fee21e36SBarry Smith   }
3624a0ff6018SBarry Smith 
3625a0ff6018SBarry Smith   /*
3626a0ff6018SBarry Smith       m - number of local rows
3627a0ff6018SBarry Smith       n - number of columns (same on all processors)
3628a0ff6018SBarry Smith       rstart - first row in new global matrix generated
3629a0ff6018SBarry Smith   */
3630fee21e36SBarry Smith   ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr);
3631a2f3521dSMark F. Adams   ierr = MatGetBlockSizes(Mreuse,&bs,&cbs);CHKERRQ(ierr);
3632a0ff6018SBarry Smith   if (call == MAT_INITIAL_MATRIX) {
3633fee21e36SBarry Smith     aij = (Mat_SeqAIJ*)(Mreuse)->data;
363400e6dbe6SBarry Smith     ii  = aij->i;
363500e6dbe6SBarry Smith     jj  = aij->j;
363600e6dbe6SBarry Smith 
3637a0ff6018SBarry Smith     /*
363800e6dbe6SBarry Smith         Determine the number of non-zeros in the diagonal and off-diagonal
363900e6dbe6SBarry Smith         portions of the matrix in order to do correct preallocation
3640a0ff6018SBarry Smith     */
364100e6dbe6SBarry Smith 
364200e6dbe6SBarry Smith     /* first get start and end of "diagonal" columns */
36436a6a5d1dSBarry Smith     if (csize == PETSC_DECIDE) {
3644ab50ec6bSBarry Smith       ierr = ISGetSize(isrow,&mglobal);CHKERRQ(ierr);
3645ab50ec6bSBarry Smith       if (mglobal == n) { /* square matrix */
3646e2c4fddaSBarry Smith         nlocal = m;
36476a6a5d1dSBarry Smith       } else {
3648ab50ec6bSBarry Smith         nlocal = n/size + ((n % size) > rank);
3649ab50ec6bSBarry Smith       }
3650ab50ec6bSBarry Smith     } else {
36516a6a5d1dSBarry Smith       nlocal = csize;
36526a6a5d1dSBarry Smith     }
3653b1d57f15SBarry Smith     ierr   = MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr);
365400e6dbe6SBarry Smith     rstart = rend - nlocal;
365565e19b50SBarry 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);
365600e6dbe6SBarry Smith 
365700e6dbe6SBarry Smith     /* next, compute all the lengths */
3658785e854fSJed Brown     ierr  = PetscMalloc1((2*m+1),&dlens);CHKERRQ(ierr);
365900e6dbe6SBarry Smith     olens = dlens + m;
366000e6dbe6SBarry Smith     for (i=0; i<m; i++) {
366100e6dbe6SBarry Smith       jend = ii[i+1] - ii[i];
366200e6dbe6SBarry Smith       olen = 0;
366300e6dbe6SBarry Smith       dlen = 0;
366400e6dbe6SBarry Smith       for (j=0; j<jend; j++) {
366500e6dbe6SBarry Smith         if (*jj < rstart || *jj >= rend) olen++;
366600e6dbe6SBarry Smith         else dlen++;
366700e6dbe6SBarry Smith         jj++;
366800e6dbe6SBarry Smith       }
366900e6dbe6SBarry Smith       olens[i] = olen;
367000e6dbe6SBarry Smith       dlens[i] = dlen;
367100e6dbe6SBarry Smith     }
3672f69a0ea3SMatthew Knepley     ierr = MatCreate(comm,&M);CHKERRQ(ierr);
3673f69a0ea3SMatthew Knepley     ierr = MatSetSizes(M,m,nlocal,PETSC_DECIDE,n);CHKERRQ(ierr);
3674a2f3521dSMark F. Adams     ierr = MatSetBlockSizes(M,bs,cbs);CHKERRQ(ierr);
36757adad957SLisandro Dalcin     ierr = MatSetType(M,((PetscObject)mat)->type_name);CHKERRQ(ierr);
3676e2d9671bSKris Buschelman     ierr = MatMPIAIJSetPreallocation(M,0,dlens,0,olens);CHKERRQ(ierr);
3677606d414cSSatish Balay     ierr = PetscFree(dlens);CHKERRQ(ierr);
3678a0ff6018SBarry Smith   } else {
3679b1d57f15SBarry Smith     PetscInt ml,nl;
3680a0ff6018SBarry Smith 
3681a0ff6018SBarry Smith     M    = *newmat;
3682a0ff6018SBarry Smith     ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr);
3683e32f2f54SBarry Smith     if (ml != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
3684a0ff6018SBarry Smith     ierr = MatZeroEntries(M);CHKERRQ(ierr);
3685c48de900SBarry Smith     /*
3686c48de900SBarry Smith          The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
3687c48de900SBarry Smith        rather than the slower MatSetValues().
3688c48de900SBarry Smith     */
3689c48de900SBarry Smith     M->was_assembled = PETSC_TRUE;
3690c48de900SBarry Smith     M->assembled     = PETSC_FALSE;
3691a0ff6018SBarry Smith   }
3692a0ff6018SBarry Smith   ierr = MatGetOwnershipRange(M,&rstart,&rend);CHKERRQ(ierr);
3693fee21e36SBarry Smith   aij  = (Mat_SeqAIJ*)(Mreuse)->data;
369400e6dbe6SBarry Smith   ii   = aij->i;
369500e6dbe6SBarry Smith   jj   = aij->j;
369600e6dbe6SBarry Smith   aa   = aij->a;
3697a0ff6018SBarry Smith   for (i=0; i<m; i++) {
3698a0ff6018SBarry Smith     row   = rstart + i;
369900e6dbe6SBarry Smith     nz    = ii[i+1] - ii[i];
370000e6dbe6SBarry Smith     cwork = jj;     jj += nz;
370100e6dbe6SBarry Smith     vwork = aa;     aa += nz;
37028c638d02SBarry Smith     ierr  = MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr);
3703a0ff6018SBarry Smith   }
3704a0ff6018SBarry Smith 
3705a0ff6018SBarry Smith   ierr    = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3706a0ff6018SBarry Smith   ierr    = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3707a0ff6018SBarry Smith   *newmat = M;
3708fee21e36SBarry Smith 
3709fee21e36SBarry Smith   /* save submatrix used in processor for next request */
3710fee21e36SBarry Smith   if (call ==  MAT_INITIAL_MATRIX) {
3711fee21e36SBarry Smith     ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr);
3712bf0cc555SLisandro Dalcin     ierr = MatDestroy(&Mreuse);CHKERRQ(ierr);
3713fee21e36SBarry Smith   }
3714a0ff6018SBarry Smith   PetscFunctionReturn(0);
3715a0ff6018SBarry Smith }
3716273d9f13SBarry Smith 
37174a2ae208SSatish Balay #undef __FUNCT__
3718ccd8e176SBarry Smith #define __FUNCT__ "MatMPIAIJSetPreallocationCSR_MPIAIJ"
37197087cfbeSBarry Smith PetscErrorCode  MatMPIAIJSetPreallocationCSR_MPIAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3720ccd8e176SBarry Smith {
3721899cda47SBarry Smith   PetscInt       m,cstart, cend,j,nnz,i,d;
3722899cda47SBarry Smith   PetscInt       *d_nnz,*o_nnz,nnz_max = 0,rstart,ii;
3723ccd8e176SBarry Smith   const PetscInt *JJ;
3724ccd8e176SBarry Smith   PetscScalar    *values;
3725ccd8e176SBarry Smith   PetscErrorCode ierr;
3726ccd8e176SBarry Smith 
3727ccd8e176SBarry Smith   PetscFunctionBegin;
3728e32f2f54SBarry Smith   if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Ii[0] must be 0 it is %D",Ii[0]);
3729899cda47SBarry Smith 
373026283091SBarry Smith   ierr   = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
373126283091SBarry Smith   ierr   = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3732d0f46423SBarry Smith   m      = B->rmap->n;
3733d0f46423SBarry Smith   cstart = B->cmap->rstart;
3734d0f46423SBarry Smith   cend   = B->cmap->rend;
3735d0f46423SBarry Smith   rstart = B->rmap->rstart;
3736899cda47SBarry Smith 
3737dcca6d9dSJed Brown   ierr = PetscMalloc2(m,&d_nnz,m,&o_nnz);CHKERRQ(ierr);
3738ccd8e176SBarry Smith 
3739ecc77c7aSBarry Smith #if defined(PETSC_USE_DEBUGGING)
3740ecc77c7aSBarry Smith   for (i=0; i<m; i++) {
3741ecc77c7aSBarry Smith     nnz = Ii[i+1]- Ii[i];
3742ecc77c7aSBarry Smith     JJ  = J + Ii[i];
3743e32f2f54SBarry Smith     if (nnz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative %D number of columns",i,nnz);
3744ecc77c7aSBarry Smith     if (nnz && (JJ[0] < 0)) SETERRRQ1(PETSC_ERR_ARG_WRONGSTATE,"Row %D starts with negative column index",i,j);
3745d0f46423SBarry 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);
3746ecc77c7aSBarry Smith   }
3747ecc77c7aSBarry Smith #endif
3748ecc77c7aSBarry Smith 
3749ccd8e176SBarry Smith   for (i=0; i<m; i++) {
3750b7940d39SSatish Balay     nnz     = Ii[i+1]- Ii[i];
3751b7940d39SSatish Balay     JJ      = J + Ii[i];
3752ccd8e176SBarry Smith     nnz_max = PetscMax(nnz_max,nnz);
3753ccd8e176SBarry Smith     d       = 0;
37540daa03b5SJed Brown     for (j=0; j<nnz; j++) {
37550daa03b5SJed Brown       if (cstart <= JJ[j] && JJ[j] < cend) d++;
3756ccd8e176SBarry Smith     }
3757ccd8e176SBarry Smith     d_nnz[i] = d;
3758ccd8e176SBarry Smith     o_nnz[i] = nnz - d;
3759ccd8e176SBarry Smith   }
3760ccd8e176SBarry Smith   ierr = MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);CHKERRQ(ierr);
37611d79065fSBarry Smith   ierr = PetscFree2(d_nnz,o_nnz);CHKERRQ(ierr);
3762ccd8e176SBarry Smith 
3763ccd8e176SBarry Smith   if (v) values = (PetscScalar*)v;
3764ccd8e176SBarry Smith   else {
37651795a4d1SJed Brown     ierr = PetscCalloc1((nnz_max+1),&values);CHKERRQ(ierr);
3766ccd8e176SBarry Smith   }
3767ccd8e176SBarry Smith 
3768ccd8e176SBarry Smith   for (i=0; i<m; i++) {
3769ccd8e176SBarry Smith     ii   = i + rstart;
3770b7940d39SSatish Balay     nnz  = Ii[i+1]- Ii[i];
3771b7940d39SSatish Balay     ierr = MatSetValues_MPIAIJ(B,1,&ii,nnz,J+Ii[i],values+(v ? Ii[i] : 0),INSERT_VALUES);CHKERRQ(ierr);
3772ccd8e176SBarry Smith   }
3773ccd8e176SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3774ccd8e176SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3775ccd8e176SBarry Smith 
3776ccd8e176SBarry Smith   if (!v) {
3777ccd8e176SBarry Smith     ierr = PetscFree(values);CHKERRQ(ierr);
3778ccd8e176SBarry Smith   }
37797827cd58SJed Brown   ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
3780ccd8e176SBarry Smith   PetscFunctionReturn(0);
3781ccd8e176SBarry Smith }
3782ccd8e176SBarry Smith 
3783ccd8e176SBarry Smith #undef __FUNCT__
3784ccd8e176SBarry Smith #define __FUNCT__ "MatMPIAIJSetPreallocationCSR"
37851eea217eSSatish Balay /*@
3786ccd8e176SBarry Smith    MatMPIAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in AIJ format
3787ccd8e176SBarry Smith    (the default parallel PETSc format).
3788ccd8e176SBarry Smith 
3789ccd8e176SBarry Smith    Collective on MPI_Comm
3790ccd8e176SBarry Smith 
3791ccd8e176SBarry Smith    Input Parameters:
3792a1661176SMatthew Knepley +  B - the matrix
3793ccd8e176SBarry Smith .  i - the indices into j for the start of each local row (starts with zero)
37940daa03b5SJed Brown .  j - the column indices for each local row (starts with zero)
3795ccd8e176SBarry Smith -  v - optional values in the matrix
3796ccd8e176SBarry Smith 
3797ccd8e176SBarry Smith    Level: developer
3798ccd8e176SBarry Smith 
379912251496SSatish Balay    Notes:
380012251496SSatish Balay        The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc;
380112251496SSatish Balay      thus you CANNOT change the matrix entries by changing the values of a[] after you have
380212251496SSatish Balay      called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays.
380312251496SSatish Balay 
380412251496SSatish Balay        The i and j indices are 0 based, and i indices are indices corresponding to the local j array.
380512251496SSatish Balay 
380612251496SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
380712251496SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
380812251496SSatish Balay     as shown:
380912251496SSatish Balay 
381012251496SSatish Balay         1 0 0
381112251496SSatish Balay         2 0 3     P0
381212251496SSatish Balay        -------
381312251496SSatish Balay         4 5 6     P1
381412251496SSatish Balay 
381512251496SSatish Balay      Process0 [P0]: rows_owned=[0,1]
381612251496SSatish Balay         i =  {0,1,3}  [size = nrow+1  = 2+1]
381712251496SSatish Balay         j =  {0,0,2}  [size = nz = 6]
381812251496SSatish Balay         v =  {1,2,3}  [size = nz = 6]
381912251496SSatish Balay 
382012251496SSatish Balay      Process1 [P1]: rows_owned=[2]
382112251496SSatish Balay         i =  {0,3}    [size = nrow+1  = 1+1]
382212251496SSatish Balay         j =  {0,1,2}  [size = nz = 6]
382312251496SSatish Balay         v =  {4,5,6}  [size = nz = 6]
382412251496SSatish Balay 
3825ccd8e176SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
3826ccd8e176SBarry Smith 
382769b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatCreateAIJ(), MPIAIJ,
38288d7a6e47SBarry Smith           MatCreateSeqAIJWithArrays(), MatCreateMPIAIJWithSplitArrays()
3829ccd8e176SBarry Smith @*/
38307087cfbeSBarry Smith PetscErrorCode  MatMPIAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
3831ccd8e176SBarry Smith {
38324ac538c5SBarry Smith   PetscErrorCode ierr;
3833ccd8e176SBarry Smith 
3834ccd8e176SBarry Smith   PetscFunctionBegin;
38354ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatMPIAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr);
3836ccd8e176SBarry Smith   PetscFunctionReturn(0);
3837ccd8e176SBarry Smith }
3838ccd8e176SBarry Smith 
3839ccd8e176SBarry Smith #undef __FUNCT__
38404a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJSetPreallocation"
3841273d9f13SBarry Smith /*@C
3842ccd8e176SBarry Smith    MatMPIAIJSetPreallocation - Preallocates memory for a sparse parallel matrix in AIJ format
3843273d9f13SBarry Smith    (the default parallel PETSc format).  For good matrix assembly performance
3844273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameters
3845273d9f13SBarry Smith    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
3846273d9f13SBarry Smith    performance can be increased by more than a factor of 50.
3847273d9f13SBarry Smith 
3848273d9f13SBarry Smith    Collective on MPI_Comm
3849273d9f13SBarry Smith 
3850273d9f13SBarry Smith    Input Parameters:
38511c4f3114SJed Brown +  B - the matrix
3852273d9f13SBarry Smith .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
3853273d9f13SBarry Smith            (same value is used for all local rows)
3854273d9f13SBarry Smith .  d_nnz - array containing the number of nonzeros in the various rows of the
3855273d9f13SBarry Smith            DIAGONAL portion of the local submatrix (possibly different for each row)
38560298fd71SBarry Smith            or NULL, if d_nz is used to specify the nonzero structure.
3857273d9f13SBarry Smith            The size of this array is equal to the number of local rows, i.e 'm'.
38583287b5eaSJed Brown            For matrices that will be factored, you must leave room for (and set)
38593287b5eaSJed Brown            the diagonal entry even if it is zero.
3860273d9f13SBarry Smith .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
3861273d9f13SBarry Smith            submatrix (same value is used for all local rows).
3862273d9f13SBarry Smith -  o_nnz - array containing the number of nonzeros in the various rows of the
3863273d9f13SBarry Smith            OFF-DIAGONAL portion of the local submatrix (possibly different for
38640298fd71SBarry Smith            each row) or NULL, if o_nz is used to specify the nonzero
3865273d9f13SBarry Smith            structure. The size of this array is equal to the number
3866273d9f13SBarry Smith            of local rows, i.e 'm'.
3867273d9f13SBarry Smith 
386849a6f317SBarry Smith    If the *_nnz parameter is given then the *_nz parameter is ignored
386949a6f317SBarry Smith 
3870273d9f13SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
3871ccd8e176SBarry Smith    compressed row storage (CSR)), is fully compatible with standard Fortran 77
38720598bfebSBarry Smith    storage.  The stored row and column indices begin with zero.
3873a7f22e61SSatish Balay    See Users-Manual: ch_mat for details.
3874273d9f13SBarry Smith 
3875273d9f13SBarry Smith    The parallel matrix is partitioned such that the first m0 rows belong to
3876273d9f13SBarry Smith    process 0, the next m1 rows belong to process 1, the next m2 rows belong
3877273d9f13SBarry Smith    to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.
3878273d9f13SBarry Smith 
3879273d9f13SBarry Smith    The DIAGONAL portion of the local submatrix of a processor can be defined
3880a05b864aSJed Brown    as the submatrix which is obtained by extraction the part corresponding to
3881a05b864aSJed Brown    the rows r1-r2 and columns c1-c2 of the global matrix, where r1 is the
3882a05b864aSJed Brown    first row that belongs to the processor, r2 is the last row belonging to
3883a05b864aSJed Brown    the this processor, and c1-c2 is range of indices of the local part of a
3884a05b864aSJed Brown    vector suitable for applying the matrix to.  This is an mxn matrix.  In the
3885a05b864aSJed Brown    common case of a square matrix, the row and column ranges are the same and
3886a05b864aSJed Brown    the DIAGONAL part is also square. The remaining portion of the local
3887a05b864aSJed Brown    submatrix (mxN) constitute the OFF-DIAGONAL portion.
3888273d9f13SBarry Smith 
3889273d9f13SBarry Smith    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
3890273d9f13SBarry Smith 
3891aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3892aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3893aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3894aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3895aa95bbe8SBarry Smith 
3896273d9f13SBarry Smith    Example usage:
3897273d9f13SBarry Smith 
3898273d9f13SBarry Smith    Consider the following 8x8 matrix with 34 non-zero values, that is
3899273d9f13SBarry Smith    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
3900273d9f13SBarry Smith    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
3901273d9f13SBarry Smith    as follows:
3902273d9f13SBarry Smith 
3903273d9f13SBarry Smith .vb
3904273d9f13SBarry Smith             1  2  0  |  0  3  0  |  0  4
3905273d9f13SBarry Smith     Proc0   0  5  6  |  7  0  0  |  8  0
3906273d9f13SBarry Smith             9  0 10  | 11  0  0  | 12  0
3907273d9f13SBarry Smith     -------------------------------------
3908273d9f13SBarry Smith            13  0 14  | 15 16 17  |  0  0
3909273d9f13SBarry Smith     Proc1   0 18  0  | 19 20 21  |  0  0
3910273d9f13SBarry Smith             0  0  0  | 22 23  0  | 24  0
3911273d9f13SBarry Smith     -------------------------------------
3912273d9f13SBarry Smith     Proc2  25 26 27  |  0  0 28  | 29  0
3913273d9f13SBarry Smith            30  0  0  | 31 32 33  |  0 34
3914273d9f13SBarry Smith .ve
3915273d9f13SBarry Smith 
3916273d9f13SBarry Smith    This can be represented as a collection of submatrices as:
3917273d9f13SBarry Smith 
3918273d9f13SBarry Smith .vb
3919273d9f13SBarry Smith       A B C
3920273d9f13SBarry Smith       D E F
3921273d9f13SBarry Smith       G H I
3922273d9f13SBarry Smith .ve
3923273d9f13SBarry Smith 
3924273d9f13SBarry Smith    Where the submatrices A,B,C are owned by proc0, D,E,F are
3925273d9f13SBarry Smith    owned by proc1, G,H,I are owned by proc2.
3926273d9f13SBarry Smith 
3927273d9f13SBarry Smith    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
3928273d9f13SBarry Smith    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
3929273d9f13SBarry Smith    The 'M','N' parameters are 8,8, and have the same values on all procs.
3930273d9f13SBarry Smith 
3931273d9f13SBarry Smith    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
3932273d9f13SBarry Smith    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
3933273d9f13SBarry Smith    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
3934273d9f13SBarry Smith    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
3935273d9f13SBarry Smith    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
3936273d9f13SBarry Smith    matrix, ans [DF] as another SeqAIJ matrix.
3937273d9f13SBarry Smith 
3938273d9f13SBarry Smith    When d_nz, o_nz parameters are specified, d_nz storage elements are
3939273d9f13SBarry Smith    allocated for every row of the local diagonal submatrix, and o_nz
3940273d9f13SBarry Smith    storage locations are allocated for every row of the OFF-DIAGONAL submat.
3941273d9f13SBarry Smith    One way to choose d_nz and o_nz is to use the max nonzerors per local
3942273d9f13SBarry Smith    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
3943273d9f13SBarry Smith    In this case, the values of d_nz,o_nz are:
3944273d9f13SBarry Smith .vb
3945273d9f13SBarry Smith      proc0 : dnz = 2, o_nz = 2
3946273d9f13SBarry Smith      proc1 : dnz = 3, o_nz = 2
3947273d9f13SBarry Smith      proc2 : dnz = 1, o_nz = 4
3948273d9f13SBarry Smith .ve
3949273d9f13SBarry Smith    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
3950273d9f13SBarry Smith    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
3951273d9f13SBarry Smith    for proc3. i.e we are using 12+15+10=37 storage locations to store
3952273d9f13SBarry Smith    34 values.
3953273d9f13SBarry Smith 
3954273d9f13SBarry Smith    When d_nnz, o_nnz parameters are specified, the storage is specified
3955273d9f13SBarry Smith    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
3956273d9f13SBarry Smith    In the above case the values for d_nnz,o_nnz are:
3957273d9f13SBarry Smith .vb
3958273d9f13SBarry Smith      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
3959273d9f13SBarry Smith      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
3960273d9f13SBarry Smith      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
3961273d9f13SBarry Smith .ve
3962273d9f13SBarry Smith    Here the space allocated is sum of all the above values i.e 34, and
3963273d9f13SBarry Smith    hence pre-allocation is perfect.
3964273d9f13SBarry Smith 
3965273d9f13SBarry Smith    Level: intermediate
3966273d9f13SBarry Smith 
3967273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
3968273d9f13SBarry Smith 
396969b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateAIJ(), MatMPIAIJSetPreallocationCSR(),
3970ab978733SBarry Smith           MPIAIJ, MatGetInfo(), PetscSplitOwnership()
3971273d9f13SBarry Smith @*/
39727087cfbeSBarry Smith PetscErrorCode  MatMPIAIJSetPreallocation(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
3973273d9f13SBarry Smith {
39744ac538c5SBarry Smith   PetscErrorCode ierr;
3975273d9f13SBarry Smith 
3976273d9f13SBarry Smith   PetscFunctionBegin;
39776ba663aaSJed Brown   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
39786ba663aaSJed Brown   PetscValidType(B,1);
39794ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatMPIAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[]),(B,d_nz,d_nnz,o_nz,o_nnz));CHKERRQ(ierr);
3980273d9f13SBarry Smith   PetscFunctionReturn(0);
3981273d9f13SBarry Smith }
3982273d9f13SBarry Smith 
39834a2ae208SSatish Balay #undef __FUNCT__
39842fb0ec9aSBarry Smith #define __FUNCT__ "MatCreateMPIAIJWithArrays"
398558d36128SBarry Smith /*@
39862fb0ec9aSBarry Smith      MatCreateMPIAIJWithArrays - creates a MPI AIJ matrix using arrays that contain in standard
39872fb0ec9aSBarry Smith          CSR format the local rows.
39882fb0ec9aSBarry Smith 
39892fb0ec9aSBarry Smith    Collective on MPI_Comm
39902fb0ec9aSBarry Smith 
39912fb0ec9aSBarry Smith    Input Parameters:
39922fb0ec9aSBarry Smith +  comm - MPI communicator
39932fb0ec9aSBarry Smith .  m - number of local rows (Cannot be PETSC_DECIDE)
39942fb0ec9aSBarry Smith .  n - This value should be the same as the local size used in creating the
39952fb0ec9aSBarry Smith        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
39962fb0ec9aSBarry Smith        calculated if N is given) For square matrices n is almost always m.
39972fb0ec9aSBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
39982fb0ec9aSBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
39992fb0ec9aSBarry Smith .   i - row indices
40002fb0ec9aSBarry Smith .   j - column indices
40012fb0ec9aSBarry Smith -   a - matrix values
40022fb0ec9aSBarry Smith 
40032fb0ec9aSBarry Smith    Output Parameter:
40042fb0ec9aSBarry Smith .   mat - the matrix
400503bfb495SBarry Smith 
40062fb0ec9aSBarry Smith    Level: intermediate
40072fb0ec9aSBarry Smith 
40082fb0ec9aSBarry Smith    Notes:
40092fb0ec9aSBarry Smith        The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc;
40102fb0ec9aSBarry Smith      thus you CANNOT change the matrix entries by changing the values of a[] after you have
40118d7a6e47SBarry Smith      called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays.
40122fb0ec9aSBarry Smith 
401312251496SSatish Balay        The i and j indices are 0 based, and i indices are indices corresponding to the local j array.
401412251496SSatish Balay 
401512251496SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
401612251496SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
401712251496SSatish Balay     as shown:
401812251496SSatish Balay 
401912251496SSatish Balay         1 0 0
402012251496SSatish Balay         2 0 3     P0
402112251496SSatish Balay        -------
402212251496SSatish Balay         4 5 6     P1
402312251496SSatish Balay 
402412251496SSatish Balay      Process0 [P0]: rows_owned=[0,1]
402512251496SSatish Balay         i =  {0,1,3}  [size = nrow+1  = 2+1]
402612251496SSatish Balay         j =  {0,0,2}  [size = nz = 6]
402712251496SSatish Balay         v =  {1,2,3}  [size = nz = 6]
402812251496SSatish Balay 
402912251496SSatish Balay      Process1 [P1]: rows_owned=[2]
403012251496SSatish Balay         i =  {0,3}    [size = nrow+1  = 1+1]
403112251496SSatish Balay         j =  {0,1,2}  [size = nz = 6]
403212251496SSatish Balay         v =  {4,5,6}  [size = nz = 6]
40332fb0ec9aSBarry Smith 
40342fb0ec9aSBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
40352fb0ec9aSBarry Smith 
40362fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
403769b1f4b7SBarry Smith           MPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithSplitArrays()
40382fb0ec9aSBarry Smith @*/
40397087cfbeSBarry 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)
40402fb0ec9aSBarry Smith {
40412fb0ec9aSBarry Smith   PetscErrorCode ierr;
40422fb0ec9aSBarry Smith 
40432fb0ec9aSBarry Smith   PetscFunctionBegin;
404469b1f4b7SBarry Smith   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4045e32f2f54SBarry Smith   if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative");
40462fb0ec9aSBarry Smith   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
4047d4146a68SBarry Smith   ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr);
4048a2f3521dSMark F. Adams   /* ierr = MatSetBlockSizes(M,bs,cbs);CHKERRQ(ierr); */
40492fb0ec9aSBarry Smith   ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr);
40502fb0ec9aSBarry Smith   ierr = MatMPIAIJSetPreallocationCSR(*mat,i,j,a);CHKERRQ(ierr);
40512fb0ec9aSBarry Smith   PetscFunctionReturn(0);
40522fb0ec9aSBarry Smith }
40532fb0ec9aSBarry Smith 
40542fb0ec9aSBarry Smith #undef __FUNCT__
405569b1f4b7SBarry Smith #define __FUNCT__ "MatCreateAIJ"
4056273d9f13SBarry Smith /*@C
405769b1f4b7SBarry Smith    MatCreateAIJ - Creates a sparse parallel matrix in AIJ format
4058273d9f13SBarry Smith    (the default parallel PETSc format).  For good matrix assembly performance
4059273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameters
4060273d9f13SBarry Smith    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
4061273d9f13SBarry Smith    performance can be increased by more than a factor of 50.
4062273d9f13SBarry Smith 
4063273d9f13SBarry Smith    Collective on MPI_Comm
4064273d9f13SBarry Smith 
4065273d9f13SBarry Smith    Input Parameters:
4066273d9f13SBarry Smith +  comm - MPI communicator
4067273d9f13SBarry Smith .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
4068273d9f13SBarry Smith            This value should be the same as the local size used in creating the
4069273d9f13SBarry Smith            y vector for the matrix-vector product y = Ax.
4070273d9f13SBarry Smith .  n - This value should be the same as the local size used in creating the
4071273d9f13SBarry Smith        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
4072273d9f13SBarry Smith        calculated if N is given) For square matrices n is almost always m.
4073273d9f13SBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
4074273d9f13SBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
4075273d9f13SBarry Smith .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
4076273d9f13SBarry Smith            (same value is used for all local rows)
4077273d9f13SBarry Smith .  d_nnz - array containing the number of nonzeros in the various rows of the
4078273d9f13SBarry Smith            DIAGONAL portion of the local submatrix (possibly different for each row)
40790298fd71SBarry Smith            or NULL, if d_nz is used to specify the nonzero structure.
4080273d9f13SBarry Smith            The size of this array is equal to the number of local rows, i.e 'm'.
4081273d9f13SBarry Smith .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
4082273d9f13SBarry Smith            submatrix (same value is used for all local rows).
4083273d9f13SBarry Smith -  o_nnz - array containing the number of nonzeros in the various rows of the
4084273d9f13SBarry Smith            OFF-DIAGONAL portion of the local submatrix (possibly different for
40850298fd71SBarry Smith            each row) or NULL, if o_nz is used to specify the nonzero
4086273d9f13SBarry Smith            structure. The size of this array is equal to the number
4087273d9f13SBarry Smith            of local rows, i.e 'm'.
4088273d9f13SBarry Smith 
4089273d9f13SBarry Smith    Output Parameter:
4090273d9f13SBarry Smith .  A - the matrix
4091273d9f13SBarry Smith 
4092175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
4093ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
4094175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
4095175b88e8SBarry Smith 
4096273d9f13SBarry Smith    Notes:
409749a6f317SBarry Smith    If the *_nnz parameter is given then the *_nz parameter is ignored
409849a6f317SBarry Smith 
4099273d9f13SBarry Smith    m,n,M,N parameters specify the size of the matrix, and its partitioning across
4100273d9f13SBarry Smith    processors, while d_nz,d_nnz,o_nz,o_nnz parameters specify the approximate
4101273d9f13SBarry Smith    storage requirements for this matrix.
4102273d9f13SBarry Smith 
4103273d9f13SBarry Smith    If PETSC_DECIDE or  PETSC_DETERMINE is used for a particular argument on one
4104273d9f13SBarry Smith    processor than it must be used on all processors that share the object for
4105273d9f13SBarry Smith    that argument.
4106273d9f13SBarry Smith 
4107273d9f13SBarry Smith    The user MUST specify either the local or global matrix dimensions
4108273d9f13SBarry Smith    (possibly both).
4109273d9f13SBarry Smith 
411033a7c187SSatish Balay    The parallel matrix is partitioned across processors such that the
411133a7c187SSatish Balay    first m0 rows belong to process 0, the next m1 rows belong to
411233a7c187SSatish Balay    process 1, the next m2 rows belong to process 2 etc.. where
411333a7c187SSatish Balay    m0,m1,m2,.. are the input parameter 'm'. i.e each processor stores
411433a7c187SSatish Balay    values corresponding to [m x N] submatrix.
4115273d9f13SBarry Smith 
411633a7c187SSatish Balay    The columns are logically partitioned with the n0 columns belonging
411733a7c187SSatish Balay    to 0th partition, the next n1 columns belonging to the next
4118df3898eeSBarry Smith    partition etc.. where n0,n1,n2... are the input parameter 'n'.
411933a7c187SSatish Balay 
412033a7c187SSatish Balay    The DIAGONAL portion of the local submatrix on any given processor
412133a7c187SSatish Balay    is the submatrix corresponding to the rows and columns m,n
412233a7c187SSatish Balay    corresponding to the given processor. i.e diagonal matrix on
412333a7c187SSatish Balay    process 0 is [m0 x n0], diagonal matrix on process 1 is [m1 x n1]
412433a7c187SSatish Balay    etc. The remaining portion of the local submatrix [m x (N-n)]
412533a7c187SSatish Balay    constitute the OFF-DIAGONAL portion. The example below better
412633a7c187SSatish Balay    illustrates this concept.
412733a7c187SSatish Balay 
412833a7c187SSatish Balay    For a square global matrix we define each processor's diagonal portion
412933a7c187SSatish Balay    to be its local rows and the corresponding columns (a square submatrix);
413033a7c187SSatish Balay    each processor's off-diagonal portion encompasses the remainder of the
413133a7c187SSatish Balay    local matrix (a rectangular submatrix).
4132273d9f13SBarry Smith 
4133273d9f13SBarry Smith    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
4134273d9f13SBarry Smith 
413597d05335SKris Buschelman    When calling this routine with a single process communicator, a matrix of
413697d05335SKris Buschelman    type SEQAIJ is returned.  If a matrix of type MPIAIJ is desired for this
413797d05335SKris Buschelman    type of communicator, use the construction mechanism:
413878102f6cSMatthew Knepley      MatCreate(...,&A); MatSetType(A,MATMPIAIJ); MatSetSizes(A, m,n,M,N); MatMPIAIJSetPreallocation(A,...);
413997d05335SKris Buschelman 
4140273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible.
4141273d9f13SBarry Smith    We search for consecutive rows with the same nonzero structure, thereby
4142273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
4143273d9f13SBarry Smith 
4144273d9f13SBarry Smith    Options Database Keys:
4145923f20ffSKris Buschelman +  -mat_no_inode  - Do not use inodes
4146923f20ffSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
4147273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
4148273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
4149273d9f13SBarry Smith         the user still MUST index entries starting at 0!
4150273d9f13SBarry Smith 
4151273d9f13SBarry Smith 
4152273d9f13SBarry Smith    Example usage:
4153273d9f13SBarry Smith 
4154273d9f13SBarry Smith    Consider the following 8x8 matrix with 34 non-zero values, that is
4155273d9f13SBarry Smith    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
4156273d9f13SBarry Smith    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
4157273d9f13SBarry Smith    as follows:
4158273d9f13SBarry Smith 
4159273d9f13SBarry Smith .vb
4160273d9f13SBarry Smith             1  2  0  |  0  3  0  |  0  4
4161273d9f13SBarry Smith     Proc0   0  5  6  |  7  0  0  |  8  0
4162273d9f13SBarry Smith             9  0 10  | 11  0  0  | 12  0
4163273d9f13SBarry Smith     -------------------------------------
4164273d9f13SBarry Smith            13  0 14  | 15 16 17  |  0  0
4165273d9f13SBarry Smith     Proc1   0 18  0  | 19 20 21  |  0  0
4166273d9f13SBarry Smith             0  0  0  | 22 23  0  | 24  0
4167273d9f13SBarry Smith     -------------------------------------
4168273d9f13SBarry Smith     Proc2  25 26 27  |  0  0 28  | 29  0
4169273d9f13SBarry Smith            30  0  0  | 31 32 33  |  0 34
4170273d9f13SBarry Smith .ve
4171273d9f13SBarry Smith 
4172273d9f13SBarry Smith    This can be represented as a collection of submatrices as:
4173273d9f13SBarry Smith 
4174273d9f13SBarry Smith .vb
4175273d9f13SBarry Smith       A B C
4176273d9f13SBarry Smith       D E F
4177273d9f13SBarry Smith       G H I
4178273d9f13SBarry Smith .ve
4179273d9f13SBarry Smith 
4180273d9f13SBarry Smith    Where the submatrices A,B,C are owned by proc0, D,E,F are
4181273d9f13SBarry Smith    owned by proc1, G,H,I are owned by proc2.
4182273d9f13SBarry Smith 
4183273d9f13SBarry Smith    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
4184273d9f13SBarry Smith    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
4185273d9f13SBarry Smith    The 'M','N' parameters are 8,8, and have the same values on all procs.
4186273d9f13SBarry Smith 
4187273d9f13SBarry Smith    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
4188273d9f13SBarry Smith    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
4189273d9f13SBarry Smith    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
4190273d9f13SBarry Smith    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
4191273d9f13SBarry Smith    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
4192273d9f13SBarry Smith    matrix, ans [DF] as another SeqAIJ matrix.
4193273d9f13SBarry Smith 
4194273d9f13SBarry Smith    When d_nz, o_nz parameters are specified, d_nz storage elements are
4195273d9f13SBarry Smith    allocated for every row of the local diagonal submatrix, and o_nz
4196273d9f13SBarry Smith    storage locations are allocated for every row of the OFF-DIAGONAL submat.
4197273d9f13SBarry Smith    One way to choose d_nz and o_nz is to use the max nonzerors per local
4198273d9f13SBarry Smith    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
4199273d9f13SBarry Smith    In this case, the values of d_nz,o_nz are:
4200273d9f13SBarry Smith .vb
4201273d9f13SBarry Smith      proc0 : dnz = 2, o_nz = 2
4202273d9f13SBarry Smith      proc1 : dnz = 3, o_nz = 2
4203273d9f13SBarry Smith      proc2 : dnz = 1, o_nz = 4
4204273d9f13SBarry Smith .ve
4205273d9f13SBarry Smith    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
4206273d9f13SBarry Smith    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
4207273d9f13SBarry Smith    for proc3. i.e we are using 12+15+10=37 storage locations to store
4208273d9f13SBarry Smith    34 values.
4209273d9f13SBarry Smith 
4210273d9f13SBarry Smith    When d_nnz, o_nnz parameters are specified, the storage is specified
4211273d9f13SBarry Smith    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
4212273d9f13SBarry Smith    In the above case the values for d_nnz,o_nnz are:
4213273d9f13SBarry Smith .vb
4214273d9f13SBarry Smith      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
4215273d9f13SBarry Smith      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
4216273d9f13SBarry Smith      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
4217273d9f13SBarry Smith .ve
4218273d9f13SBarry Smith    Here the space allocated is sum of all the above values i.e 34, and
4219273d9f13SBarry Smith    hence pre-allocation is perfect.
4220273d9f13SBarry Smith 
4221273d9f13SBarry Smith    Level: intermediate
4222273d9f13SBarry Smith 
4223273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
4224273d9f13SBarry Smith 
4225ccd8e176SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
42262fb0ec9aSBarry Smith           MPIAIJ, MatCreateMPIAIJWithArrays()
4227273d9f13SBarry Smith @*/
422869b1f4b7SBarry 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)
4229273d9f13SBarry Smith {
42306849ba73SBarry Smith   PetscErrorCode ierr;
4231b1d57f15SBarry Smith   PetscMPIInt    size;
4232273d9f13SBarry Smith 
4233273d9f13SBarry Smith   PetscFunctionBegin;
4234f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
4235f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*A,m,n,M,N);CHKERRQ(ierr);
4236273d9f13SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
4237273d9f13SBarry Smith   if (size > 1) {
4238273d9f13SBarry Smith     ierr = MatSetType(*A,MATMPIAIJ);CHKERRQ(ierr);
4239273d9f13SBarry Smith     ierr = MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr);
4240273d9f13SBarry Smith   } else {
4241273d9f13SBarry Smith     ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
4242273d9f13SBarry Smith     ierr = MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);CHKERRQ(ierr);
4243273d9f13SBarry Smith   }
4244273d9f13SBarry Smith   PetscFunctionReturn(0);
4245273d9f13SBarry Smith }
4246195d93cdSBarry Smith 
42474a2ae208SSatish Balay #undef __FUNCT__
42484a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJGetSeqAIJ"
42499230625dSJed Brown PetscErrorCode  MatMPIAIJGetSeqAIJ(Mat A,Mat *Ad,Mat *Ao,const PetscInt *colmap[])
4250195d93cdSBarry Smith {
4251195d93cdSBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
4252b1d57f15SBarry Smith 
4253195d93cdSBarry Smith   PetscFunctionBegin;
425421e72a00SBarry Smith   if (Ad)     *Ad     = a->A;
425521e72a00SBarry Smith   if (Ao)     *Ao     = a->B;
425621e72a00SBarry Smith   if (colmap) *colmap = a->garray;
4257195d93cdSBarry Smith   PetscFunctionReturn(0);
4258195d93cdSBarry Smith }
4259a2243be0SBarry Smith 
4260a2243be0SBarry Smith #undef __FUNCT__
4261a2243be0SBarry Smith #define __FUNCT__ "MatSetColoring_MPIAIJ"
4262dfbe8321SBarry Smith PetscErrorCode MatSetColoring_MPIAIJ(Mat A,ISColoring coloring)
4263a2243be0SBarry Smith {
4264dfbe8321SBarry Smith   PetscErrorCode ierr;
4265b1d57f15SBarry Smith   PetscInt       i;
4266a2243be0SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
4267a2243be0SBarry Smith 
4268a2243be0SBarry Smith   PetscFunctionBegin;
42698ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
427008b6dcc0SBarry Smith     ISColoringValue *allcolors,*colors;
4271a2243be0SBarry Smith     ISColoring      ocoloring;
4272a2243be0SBarry Smith 
4273a2243be0SBarry Smith     /* set coloring for diagonal portion */
4274a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->A,coloring);CHKERRQ(ierr);
4275a2243be0SBarry Smith 
4276a2243be0SBarry Smith     /* set coloring for off-diagonal portion */
4277ce94432eSBarry Smith     ierr = ISAllGatherColors(PetscObjectComm((PetscObject)A),coloring->n,coloring->colors,NULL,&allcolors);CHKERRQ(ierr);
4278785e854fSJed Brown     ierr = PetscMalloc1((a->B->cmap->n+1),&colors);CHKERRQ(ierr);
4279d0f46423SBarry Smith     for (i=0; i<a->B->cmap->n; i++) {
4280a2243be0SBarry Smith       colors[i] = allcolors[a->garray[i]];
4281a2243be0SBarry Smith     }
4282a2243be0SBarry Smith     ierr = PetscFree(allcolors);CHKERRQ(ierr);
4283d0f46423SBarry Smith     ierr = ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
4284a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr);
42856bf464f9SBarry Smith     ierr = ISColoringDestroy(&ocoloring);CHKERRQ(ierr);
4286a2243be0SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
428708b6dcc0SBarry Smith     ISColoringValue *colors;
4288b1d57f15SBarry Smith     PetscInt        *larray;
4289a2243be0SBarry Smith     ISColoring      ocoloring;
4290a2243be0SBarry Smith 
4291a2243be0SBarry Smith     /* set coloring for diagonal portion */
4292785e854fSJed Brown     ierr = PetscMalloc1((a->A->cmap->n+1),&larray);CHKERRQ(ierr);
4293d0f46423SBarry Smith     for (i=0; i<a->A->cmap->n; i++) {
4294d0f46423SBarry Smith       larray[i] = i + A->cmap->rstart;
4295a2243be0SBarry Smith     }
42960298fd71SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,a->A->cmap->n,larray,NULL,larray);CHKERRQ(ierr);
4297785e854fSJed Brown     ierr = PetscMalloc1((a->A->cmap->n+1),&colors);CHKERRQ(ierr);
4298d0f46423SBarry Smith     for (i=0; i<a->A->cmap->n; i++) {
4299a2243be0SBarry Smith       colors[i] = coloring->colors[larray[i]];
4300a2243be0SBarry Smith     }
4301a2243be0SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
4302d0f46423SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,a->A->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
4303a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->A,ocoloring);CHKERRQ(ierr);
43046bf464f9SBarry Smith     ierr = ISColoringDestroy(&ocoloring);CHKERRQ(ierr);
4305a2243be0SBarry Smith 
4306a2243be0SBarry Smith     /* set coloring for off-diagonal portion */
4307785e854fSJed Brown     ierr = PetscMalloc1((a->B->cmap->n+1),&larray);CHKERRQ(ierr);
43080298fd71SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,a->B->cmap->n,a->garray,NULL,larray);CHKERRQ(ierr);
4309785e854fSJed Brown     ierr = PetscMalloc1((a->B->cmap->n+1),&colors);CHKERRQ(ierr);
4310d0f46423SBarry Smith     for (i=0; i<a->B->cmap->n; i++) {
4311a2243be0SBarry Smith       colors[i] = coloring->colors[larray[i]];
4312a2243be0SBarry Smith     }
4313a2243be0SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
4314d0f46423SBarry Smith     ierr = ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
4315a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr);
43166bf464f9SBarry Smith     ierr = ISColoringDestroy(&ocoloring);CHKERRQ(ierr);
43176bf464f9SBarry Smith   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support ISColoringType %d",(int)coloring->ctype);
4318a2243be0SBarry Smith   PetscFunctionReturn(0);
4319a2243be0SBarry Smith }
4320a2243be0SBarry Smith 
4321779c1a83SBarry Smith #undef __FUNCT__
4322779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdifor_MPIAIJ"
4323b1d57f15SBarry Smith PetscErrorCode MatSetValuesAdifor_MPIAIJ(Mat A,PetscInt nl,void *advalues)
4324779c1a83SBarry Smith {
4325779c1a83SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
4326dfbe8321SBarry Smith   PetscErrorCode ierr;
4327779c1a83SBarry Smith 
4328779c1a83SBarry Smith   PetscFunctionBegin;
4329779c1a83SBarry Smith   ierr = MatSetValuesAdifor_SeqAIJ(a->A,nl,advalues);CHKERRQ(ierr);
4330779c1a83SBarry Smith   ierr = MatSetValuesAdifor_SeqAIJ(a->B,nl,advalues);CHKERRQ(ierr);
4331a2243be0SBarry Smith   PetscFunctionReturn(0);
4332a2243be0SBarry Smith }
4333c5d6d63eSBarry Smith 
4334c5d6d63eSBarry Smith #undef __FUNCT__
433590431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJConcatenateSeqAIJSymbolic"
433690431a8fSHong Zhang PetscErrorCode  MatCreateMPIAIJConcatenateSeqAIJSymbolic(MPI_Comm comm,Mat inmat,PetscInt n,Mat *outmat)
43379b8102ccSHong Zhang {
43389b8102ccSHong Zhang   PetscErrorCode ierr;
4339a2f3521dSMark F. Adams   PetscInt       m,N,i,rstart,nnz,*dnz,*onz,sum,bs,cbs;
43409b8102ccSHong Zhang   PetscInt       *indx;
43419b8102ccSHong Zhang 
43429b8102ccSHong Zhang   PetscFunctionBegin;
43439b8102ccSHong Zhang   /* This routine will ONLY return MPIAIJ type matrix */
43449b8102ccSHong Zhang   ierr = MatGetSize(inmat,&m,&N);CHKERRQ(ierr);
4345a2f3521dSMark F. Adams   ierr = MatGetBlockSizes(inmat,&bs,&cbs);CHKERRQ(ierr);
43469b8102ccSHong Zhang   if (n == PETSC_DECIDE) {
43479b8102ccSHong Zhang     ierr = PetscSplitOwnership(comm,&n,&N);CHKERRQ(ierr);
43489b8102ccSHong Zhang   }
4349a22543b6SHong Zhang   /* Check sum(n) = N */
4350a95133b1SBarry Smith   ierr = MPI_Allreduce(&n,&sum,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr);
4351a22543b6SHong Zhang   if (sum != N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Sum of local columns != global columns %d",N);
4352a22543b6SHong Zhang 
43539b8102ccSHong Zhang   ierr    = MPI_Scan(&m, &rstart,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr);
43549b8102ccSHong Zhang   rstart -= m;
43559b8102ccSHong Zhang 
43569b8102ccSHong Zhang   ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr);
43579b8102ccSHong Zhang   for (i=0; i<m; i++) {
43580298fd71SBarry Smith     ierr = MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,NULL);CHKERRQ(ierr);
43599b8102ccSHong Zhang     ierr = MatPreallocateSet(i+rstart,nnz,indx,dnz,onz);CHKERRQ(ierr);
43600298fd71SBarry Smith     ierr = MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,NULL);CHKERRQ(ierr);
43619b8102ccSHong Zhang   }
43629b8102ccSHong Zhang 
43639b8102ccSHong Zhang   ierr = MatCreate(comm,outmat);CHKERRQ(ierr);
43649b8102ccSHong Zhang   ierr = MatSetSizes(*outmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
4365a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(*outmat,bs,cbs);CHKERRQ(ierr);
43669b8102ccSHong Zhang   ierr = MatSetType(*outmat,MATMPIAIJ);CHKERRQ(ierr);
43679b8102ccSHong Zhang   ierr = MatMPIAIJSetPreallocation(*outmat,0,dnz,0,onz);CHKERRQ(ierr);
43689b8102ccSHong Zhang   ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr);
43699b8102ccSHong Zhang   PetscFunctionReturn(0);
43709b8102ccSHong Zhang }
43719b8102ccSHong Zhang 
43729b8102ccSHong Zhang #undef __FUNCT__
437390431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJConcatenateSeqAIJNumeric"
437490431a8fSHong Zhang PetscErrorCode  MatCreateMPIAIJConcatenateSeqAIJNumeric(MPI_Comm comm,Mat inmat,PetscInt n,Mat outmat)
43759b8102ccSHong Zhang {
43769b8102ccSHong Zhang   PetscErrorCode ierr;
43779b8102ccSHong Zhang   PetscInt       m,N,i,rstart,nnz,Ii;
43789b8102ccSHong Zhang   PetscInt       *indx;
43799b8102ccSHong Zhang   PetscScalar    *values;
43809b8102ccSHong Zhang 
43819b8102ccSHong Zhang   PetscFunctionBegin;
43829b8102ccSHong Zhang   ierr = MatGetSize(inmat,&m,&N);CHKERRQ(ierr);
43830298fd71SBarry Smith   ierr = MatGetOwnershipRange(outmat,&rstart,NULL);CHKERRQ(ierr);
43849b8102ccSHong Zhang   for (i=0; i<m; i++) {
43859b8102ccSHong Zhang     ierr = MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
43869b8102ccSHong Zhang     Ii   = i + rstart;
43873c79b8e7SHong Zhang     ierr = MatSetValues(outmat,1,&Ii,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr);
43889b8102ccSHong Zhang     ierr = MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
43899b8102ccSHong Zhang   }
43909b8102ccSHong Zhang   ierr = MatAssemblyBegin(outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
43919b8102ccSHong Zhang   ierr = MatAssemblyEnd(outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
43929b8102ccSHong Zhang   PetscFunctionReturn(0);
43939b8102ccSHong Zhang }
43949b8102ccSHong Zhang 
43959b8102ccSHong Zhang #undef __FUNCT__
439690431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJConcatenateSeqAIJ"
4397bc08b0f1SBarry Smith /*@
439890431a8fSHong Zhang       MatCreateMPIAIJConcatenateSeqAIJ - Creates a single large PETSc matrix by concatenating sequential
439951dd7536SBarry Smith                  matrices from each processor
4400c5d6d63eSBarry Smith 
4401c5d6d63eSBarry Smith     Collective on MPI_Comm
4402c5d6d63eSBarry Smith 
4403c5d6d63eSBarry Smith    Input Parameters:
440451dd7536SBarry Smith +    comm - the communicators the parallel matrix will live on
4405d6bb3c2dSHong Zhang .    inmat - the input sequential matrices
44060e36024fSHong Zhang .    n - number of local columns (or PETSC_DECIDE)
4407d6bb3c2dSHong Zhang -    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
440851dd7536SBarry Smith 
440951dd7536SBarry Smith    Output Parameter:
441051dd7536SBarry Smith .    outmat - the parallel matrix generated
4411c5d6d63eSBarry Smith 
44127e25d530SSatish Balay     Level: advanced
44137e25d530SSatish Balay 
4414f08fae4eSHong Zhang    Notes: The number of columns of the matrix in EACH processor MUST be the same.
4415c5d6d63eSBarry Smith 
4416c5d6d63eSBarry Smith @*/
441790431a8fSHong Zhang PetscErrorCode  MatCreateMPIAIJConcatenateSeqAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
4418c5d6d63eSBarry Smith {
4419dfbe8321SBarry Smith   PetscErrorCode ierr;
4420f4703a44SHong Zhang   PetscMPIInt    size;
4421c5d6d63eSBarry Smith 
4422c5d6d63eSBarry Smith   PetscFunctionBegin;
4423f4703a44SHong Zhang   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
44249b8102ccSHong Zhang   ierr = PetscLogEventBegin(MAT_Merge,inmat,0,0,0);CHKERRQ(ierr);
4425f4703a44SHong Zhang   if (size == 1) {
4426f4703a44SHong Zhang     if (scall == MAT_INITIAL_MATRIX) {
4427f4703a44SHong Zhang       ierr = MatDuplicate(inmat,MAT_COPY_VALUES,outmat);CHKERRQ(ierr);
4428f4703a44SHong Zhang     } else {
4429f4703a44SHong Zhang       ierr = MatCopy(inmat,*outmat,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
4430f4703a44SHong Zhang     }
4431f4703a44SHong Zhang   } else {
4432d6bb3c2dSHong Zhang     if (scall == MAT_INITIAL_MATRIX) {
443390431a8fSHong Zhang       ierr = MatCreateMPIAIJConcatenateSeqAIJSymbolic(comm,inmat,n,outmat);CHKERRQ(ierr);
44340e36024fSHong Zhang     }
443590431a8fSHong Zhang     ierr = MatCreateMPIAIJConcatenateSeqAIJNumeric(comm,inmat,n,*outmat);CHKERRQ(ierr);
4436f4703a44SHong Zhang   }
44379b8102ccSHong Zhang   ierr = PetscLogEventEnd(MAT_Merge,inmat,0,0,0);CHKERRQ(ierr);
4438c5d6d63eSBarry Smith   PetscFunctionReturn(0);
4439c5d6d63eSBarry Smith }
4440c5d6d63eSBarry Smith 
4441c5d6d63eSBarry Smith #undef __FUNCT__
4442c5d6d63eSBarry Smith #define __FUNCT__ "MatFileSplit"
4443dfbe8321SBarry Smith PetscErrorCode MatFileSplit(Mat A,char *outfile)
4444c5d6d63eSBarry Smith {
4445dfbe8321SBarry Smith   PetscErrorCode    ierr;
444632dcc486SBarry Smith   PetscMPIInt       rank;
4447b1d57f15SBarry Smith   PetscInt          m,N,i,rstart,nnz;
4448de4209c5SBarry Smith   size_t            len;
4449b1d57f15SBarry Smith   const PetscInt    *indx;
4450c5d6d63eSBarry Smith   PetscViewer       out;
4451c5d6d63eSBarry Smith   char              *name;
4452c5d6d63eSBarry Smith   Mat               B;
4453b3cc6726SBarry Smith   const PetscScalar *values;
4454c5d6d63eSBarry Smith 
4455c5d6d63eSBarry Smith   PetscFunctionBegin;
4456c5d6d63eSBarry Smith   ierr = MatGetLocalSize(A,&m,0);CHKERRQ(ierr);
4457c5d6d63eSBarry Smith   ierr = MatGetSize(A,0,&N);CHKERRQ(ierr);
4458f204ca49SKris Buschelman   /* Should this be the type of the diagonal block of A? */
4459f69a0ea3SMatthew Knepley   ierr = MatCreate(PETSC_COMM_SELF,&B);CHKERRQ(ierr);
4460f69a0ea3SMatthew Knepley   ierr = MatSetSizes(B,m,N,m,N);CHKERRQ(ierr);
446133d57670SJed Brown   ierr = MatSetBlockSizesFromMats(B,A,A);CHKERRQ(ierr);
4462f204ca49SKris Buschelman   ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
44630298fd71SBarry Smith   ierr = MatSeqAIJSetPreallocation(B,0,NULL);CHKERRQ(ierr);
4464c5d6d63eSBarry Smith   ierr = MatGetOwnershipRange(A,&rstart,0);CHKERRQ(ierr);
4465c5d6d63eSBarry Smith   for (i=0; i<m; i++) {
4466c5d6d63eSBarry Smith     ierr = MatGetRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr);
4467c5d6d63eSBarry Smith     ierr = MatSetValues(B,1,&i,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr);
4468c5d6d63eSBarry Smith     ierr = MatRestoreRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr);
4469c5d6d63eSBarry Smith   }
4470c5d6d63eSBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4471c5d6d63eSBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4472c5d6d63eSBarry Smith 
4473ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A),&rank);CHKERRQ(ierr);
4474c5d6d63eSBarry Smith   ierr = PetscStrlen(outfile,&len);CHKERRQ(ierr);
4475785e854fSJed Brown   ierr = PetscMalloc1((len+5),&name);CHKERRQ(ierr);
4476c5d6d63eSBarry Smith   sprintf(name,"%s.%d",outfile,rank);
4477852598b0SBarry Smith   ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,name,FILE_MODE_APPEND,&out);CHKERRQ(ierr);
4478a2ea699eSBarry Smith   ierr = PetscFree(name);CHKERRQ(ierr);
4479c5d6d63eSBarry Smith   ierr = MatView(B,out);CHKERRQ(ierr);
44806bf464f9SBarry Smith   ierr = PetscViewerDestroy(&out);CHKERRQ(ierr);
44816bf464f9SBarry Smith   ierr = MatDestroy(&B);CHKERRQ(ierr);
4482c5d6d63eSBarry Smith   PetscFunctionReturn(0);
4483c5d6d63eSBarry Smith }
4484e5f2cdd8SHong Zhang 
448509573ac7SBarry Smith extern PetscErrorCode MatDestroy_MPIAIJ(Mat);
448651a7d1a8SHong Zhang #undef __FUNCT__
448751a7d1a8SHong Zhang #define __FUNCT__ "MatDestroy_MPIAIJ_SeqsToMPI"
44887087cfbeSBarry Smith PetscErrorCode  MatDestroy_MPIAIJ_SeqsToMPI(Mat A)
448951a7d1a8SHong Zhang {
449051a7d1a8SHong Zhang   PetscErrorCode      ierr;
4491671beff6SHong Zhang   Mat_Merge_SeqsToMPI *merge;
4492776b82aeSLisandro Dalcin   PetscContainer      container;
449351a7d1a8SHong Zhang 
449451a7d1a8SHong Zhang   PetscFunctionBegin;
4495671beff6SHong Zhang   ierr = PetscObjectQuery((PetscObject)A,"MatMergeSeqsToMPI",(PetscObject*)&container);CHKERRQ(ierr);
4496671beff6SHong Zhang   if (container) {
4497776b82aeSLisandro Dalcin     ierr = PetscContainerGetPointer(container,(void**)&merge);CHKERRQ(ierr);
449851a7d1a8SHong Zhang     ierr = PetscFree(merge->id_r);CHKERRQ(ierr);
44993e06a4e6SHong Zhang     ierr = PetscFree(merge->len_s);CHKERRQ(ierr);
45003e06a4e6SHong Zhang     ierr = PetscFree(merge->len_r);CHKERRQ(ierr);
450151a7d1a8SHong Zhang     ierr = PetscFree(merge->bi);CHKERRQ(ierr);
450251a7d1a8SHong Zhang     ierr = PetscFree(merge->bj);CHKERRQ(ierr);
4503533163c2SBarry Smith     ierr = PetscFree(merge->buf_ri[0]);CHKERRQ(ierr);
450402c68681SHong Zhang     ierr = PetscFree(merge->buf_ri);CHKERRQ(ierr);
4505533163c2SBarry Smith     ierr = PetscFree(merge->buf_rj[0]);CHKERRQ(ierr);
450602c68681SHong Zhang     ierr = PetscFree(merge->buf_rj);CHKERRQ(ierr);
450705b42c5fSBarry Smith     ierr = PetscFree(merge->coi);CHKERRQ(ierr);
450805b42c5fSBarry Smith     ierr = PetscFree(merge->coj);CHKERRQ(ierr);
450905b42c5fSBarry Smith     ierr = PetscFree(merge->owners_co);CHKERRQ(ierr);
45106bf464f9SBarry Smith     ierr = PetscLayoutDestroy(&merge->rowmap);CHKERRQ(ierr);
4511bf0cc555SLisandro Dalcin     ierr = PetscFree(merge);CHKERRQ(ierr);
4512671beff6SHong Zhang     ierr = PetscObjectCompose((PetscObject)A,"MatMergeSeqsToMPI",0);CHKERRQ(ierr);
4513671beff6SHong Zhang   }
451451a7d1a8SHong Zhang   ierr = MatDestroy_MPIAIJ(A);CHKERRQ(ierr);
451551a7d1a8SHong Zhang   PetscFunctionReturn(0);
451651a7d1a8SHong Zhang }
451751a7d1a8SHong Zhang 
4518c6db04a5SJed Brown #include <../src/mat/utils/freespace.h>
4519c6db04a5SJed Brown #include <petscbt.h>
45204ebed01fSBarry Smith 
4521e5f2cdd8SHong Zhang #undef __FUNCT__
452290431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJSumSeqAIJNumeric"
452390431a8fSHong Zhang PetscErrorCode  MatCreateMPIAIJSumSeqAIJNumeric(Mat seqmat,Mat mpimat)
452455d1abb9SHong Zhang {
452555d1abb9SHong Zhang   PetscErrorCode      ierr;
4526ce94432eSBarry Smith   MPI_Comm            comm;
452755d1abb9SHong Zhang   Mat_SeqAIJ          *a  =(Mat_SeqAIJ*)seqmat->data;
4528b1d57f15SBarry Smith   PetscMPIInt         size,rank,taga,*len_s;
4529a2ea699eSBarry Smith   PetscInt            N=mpimat->cmap->N,i,j,*owners,*ai=a->i,*aj;
4530b1d57f15SBarry Smith   PetscInt            proc,m;
4531b1d57f15SBarry Smith   PetscInt            **buf_ri,**buf_rj;
4532b1d57f15SBarry Smith   PetscInt            k,anzi,*bj_i,*bi,*bj,arow,bnzi,nextaj;
4533b1d57f15SBarry Smith   PetscInt            nrows,**buf_ri_k,**nextrow,**nextai;
453455d1abb9SHong Zhang   MPI_Request         *s_waits,*r_waits;
453555d1abb9SHong Zhang   MPI_Status          *status;
4536a77337e4SBarry Smith   MatScalar           *aa=a->a;
4537dd6ea824SBarry Smith   MatScalar           **abuf_r,*ba_i;
453855d1abb9SHong Zhang   Mat_Merge_SeqsToMPI *merge;
4539776b82aeSLisandro Dalcin   PetscContainer      container;
454055d1abb9SHong Zhang 
454155d1abb9SHong Zhang   PetscFunctionBegin;
4542bedda5b1SHong Zhang   ierr = PetscObjectGetComm((PetscObject)mpimat,&comm);CHKERRQ(ierr);
45434ebed01fSBarry Smith   ierr = PetscLogEventBegin(MAT_Seqstompinum,seqmat,0,0,0);CHKERRQ(ierr);
45443c2c1871SHong Zhang 
454555d1abb9SHong Zhang   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
454655d1abb9SHong Zhang   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
454755d1abb9SHong Zhang 
454855d1abb9SHong Zhang   ierr = PetscObjectQuery((PetscObject)mpimat,"MatMergeSeqsToMPI",(PetscObject*)&container);CHKERRQ(ierr);
4549776b82aeSLisandro Dalcin   ierr = PetscContainerGetPointer(container,(void**)&merge);CHKERRQ(ierr);
4550bf0cc555SLisandro Dalcin 
455155d1abb9SHong Zhang   bi     = merge->bi;
455255d1abb9SHong Zhang   bj     = merge->bj;
455355d1abb9SHong Zhang   buf_ri = merge->buf_ri;
455455d1abb9SHong Zhang   buf_rj = merge->buf_rj;
455555d1abb9SHong Zhang 
4556785e854fSJed Brown   ierr   = PetscMalloc1(size,&status);CHKERRQ(ierr);
45577a2fc3feSBarry Smith   owners = merge->rowmap->range;
455855d1abb9SHong Zhang   len_s  = merge->len_s;
455955d1abb9SHong Zhang 
456055d1abb9SHong Zhang   /* send and recv matrix values */
456155d1abb9SHong Zhang   /*-----------------------------*/
4562357abbc8SBarry Smith   ierr = PetscObjectGetNewTag((PetscObject)mpimat,&taga);CHKERRQ(ierr);
456355d1abb9SHong Zhang   ierr = PetscPostIrecvScalar(comm,taga,merge->nrecv,merge->id_r,merge->len_r,&abuf_r,&r_waits);CHKERRQ(ierr);
456455d1abb9SHong Zhang 
4565785e854fSJed Brown   ierr = PetscMalloc1((merge->nsend+1),&s_waits);CHKERRQ(ierr);
456655d1abb9SHong Zhang   for (proc=0,k=0; proc<size; proc++) {
456755d1abb9SHong Zhang     if (!len_s[proc]) continue;
456855d1abb9SHong Zhang     i    = owners[proc];
456955d1abb9SHong Zhang     ierr = MPI_Isend(aa+ai[i],len_s[proc],MPIU_MATSCALAR,proc,taga,comm,s_waits+k);CHKERRQ(ierr);
457055d1abb9SHong Zhang     k++;
457155d1abb9SHong Zhang   }
457255d1abb9SHong Zhang 
45730c468ba9SBarry Smith   if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,r_waits,status);CHKERRQ(ierr);}
45740c468ba9SBarry Smith   if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,s_waits,status);CHKERRQ(ierr);}
457555d1abb9SHong Zhang   ierr = PetscFree(status);CHKERRQ(ierr);
457655d1abb9SHong Zhang 
457755d1abb9SHong Zhang   ierr = PetscFree(s_waits);CHKERRQ(ierr);
457855d1abb9SHong Zhang   ierr = PetscFree(r_waits);CHKERRQ(ierr);
457955d1abb9SHong Zhang 
458055d1abb9SHong Zhang   /* insert mat values of mpimat */
458155d1abb9SHong Zhang   /*----------------------------*/
4582785e854fSJed Brown   ierr = PetscMalloc1(N,&ba_i);CHKERRQ(ierr);
4583dcca6d9dSJed Brown   ierr = PetscMalloc3(merge->nrecv,&buf_ri_k,merge->nrecv,&nextrow,merge->nrecv,&nextai);CHKERRQ(ierr);
458455d1abb9SHong Zhang 
458555d1abb9SHong Zhang   for (k=0; k<merge->nrecv; k++) {
458655d1abb9SHong Zhang     buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
458755d1abb9SHong Zhang     nrows       = *(buf_ri_k[k]);
458855d1abb9SHong Zhang     nextrow[k]  = buf_ri_k[k]+1;  /* next row number of k-th recved i-structure */
458955d1abb9SHong Zhang     nextai[k]   = buf_ri_k[k] + (nrows + 1); /* poins to the next i-structure of k-th recved i-structure  */
459055d1abb9SHong Zhang   }
459155d1abb9SHong Zhang 
459255d1abb9SHong Zhang   /* set values of ba */
45937a2fc3feSBarry Smith   m = merge->rowmap->n;
459455d1abb9SHong Zhang   for (i=0; i<m; i++) {
459555d1abb9SHong Zhang     arow = owners[rank] + i;
459655d1abb9SHong Zhang     bj_i = bj+bi[i];  /* col indices of the i-th row of mpimat */
459755d1abb9SHong Zhang     bnzi = bi[i+1] - bi[i];
4598a77337e4SBarry Smith     ierr = PetscMemzero(ba_i,bnzi*sizeof(PetscScalar));CHKERRQ(ierr);
459955d1abb9SHong Zhang 
460055d1abb9SHong Zhang     /* add local non-zero vals of this proc's seqmat into ba */
460155d1abb9SHong Zhang     anzi   = ai[arow+1] - ai[arow];
460255d1abb9SHong Zhang     aj     = a->j + ai[arow];
460355d1abb9SHong Zhang     aa     = a->a + ai[arow];
460455d1abb9SHong Zhang     nextaj = 0;
460555d1abb9SHong Zhang     for (j=0; nextaj<anzi; j++) {
460655d1abb9SHong Zhang       if (*(bj_i + j) == aj[nextaj]) { /* bcol == acol */
460755d1abb9SHong Zhang         ba_i[j] += aa[nextaj++];
460855d1abb9SHong Zhang       }
460955d1abb9SHong Zhang     }
461055d1abb9SHong Zhang 
461155d1abb9SHong Zhang     /* add received vals into ba */
461255d1abb9SHong Zhang     for (k=0; k<merge->nrecv; k++) { /* k-th received message */
461355d1abb9SHong Zhang       /* i-th row */
461455d1abb9SHong Zhang       if (i == *nextrow[k]) {
461555d1abb9SHong Zhang         anzi   = *(nextai[k]+1) - *nextai[k];
461655d1abb9SHong Zhang         aj     = buf_rj[k] + *(nextai[k]);
461755d1abb9SHong Zhang         aa     = abuf_r[k] + *(nextai[k]);
461855d1abb9SHong Zhang         nextaj = 0;
461955d1abb9SHong Zhang         for (j=0; nextaj<anzi; j++) {
462055d1abb9SHong Zhang           if (*(bj_i + j) == aj[nextaj]) { /* bcol == acol */
462155d1abb9SHong Zhang             ba_i[j] += aa[nextaj++];
462255d1abb9SHong Zhang           }
462355d1abb9SHong Zhang         }
462455d1abb9SHong Zhang         nextrow[k]++; nextai[k]++;
462555d1abb9SHong Zhang       }
462655d1abb9SHong Zhang     }
462755d1abb9SHong Zhang     ierr = MatSetValues(mpimat,1,&arow,bnzi,bj_i,ba_i,INSERT_VALUES);CHKERRQ(ierr);
462855d1abb9SHong Zhang   }
462955d1abb9SHong Zhang   ierr = MatAssemblyBegin(mpimat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
463055d1abb9SHong Zhang   ierr = MatAssemblyEnd(mpimat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
463155d1abb9SHong Zhang 
4632533163c2SBarry Smith   ierr = PetscFree(abuf_r[0]);CHKERRQ(ierr);
463355d1abb9SHong Zhang   ierr = PetscFree(abuf_r);CHKERRQ(ierr);
463455d1abb9SHong Zhang   ierr = PetscFree(ba_i);CHKERRQ(ierr);
46351d79065fSBarry Smith   ierr = PetscFree3(buf_ri_k,nextrow,nextai);CHKERRQ(ierr);
46364ebed01fSBarry Smith   ierr = PetscLogEventEnd(MAT_Seqstompinum,seqmat,0,0,0);CHKERRQ(ierr);
463755d1abb9SHong Zhang   PetscFunctionReturn(0);
463855d1abb9SHong Zhang }
463938f152feSBarry Smith 
46406bc0bbbfSBarry Smith extern PetscErrorCode  MatDestroy_MPIAIJ_SeqsToMPI(Mat);
46416bc0bbbfSBarry Smith 
464238f152feSBarry Smith #undef __FUNCT__
464390431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJSumSeqAIJSymbolic"
464490431a8fSHong Zhang PetscErrorCode  MatCreateMPIAIJSumSeqAIJSymbolic(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,Mat *mpimat)
4645e5f2cdd8SHong Zhang {
4646f08fae4eSHong Zhang   PetscErrorCode      ierr;
464755a3bba9SHong Zhang   Mat                 B_mpi;
4648c2234fe3SHong Zhang   Mat_SeqAIJ          *a=(Mat_SeqAIJ*)seqmat->data;
4649b1d57f15SBarry Smith   PetscMPIInt         size,rank,tagi,tagj,*len_s,*len_si,*len_ri;
4650b1d57f15SBarry Smith   PetscInt            **buf_rj,**buf_ri,**buf_ri_k;
4651d0f46423SBarry Smith   PetscInt            M=seqmat->rmap->n,N=seqmat->cmap->n,i,*owners,*ai=a->i,*aj=a->j;
4652a2f3521dSMark F. Adams   PetscInt            len,proc,*dnz,*onz,bs,cbs;
4653b1d57f15SBarry Smith   PetscInt            k,anzi,*bi,*bj,*lnk,nlnk,arow,bnzi,nspacedouble=0;
4654b1d57f15SBarry Smith   PetscInt            nrows,*buf_s,*buf_si,*buf_si_i,**nextrow,**nextai;
465555d1abb9SHong Zhang   MPI_Request         *si_waits,*sj_waits,*ri_waits,*rj_waits;
465658cb9c82SHong Zhang   MPI_Status          *status;
46570298fd71SBarry Smith   PetscFreeSpaceList  free_space=NULL,current_space=NULL;
4658be0fcf8dSHong Zhang   PetscBT             lnkbt;
465951a7d1a8SHong Zhang   Mat_Merge_SeqsToMPI *merge;
4660776b82aeSLisandro Dalcin   PetscContainer      container;
466102c68681SHong Zhang 
4662e5f2cdd8SHong Zhang   PetscFunctionBegin;
46634ebed01fSBarry Smith   ierr = PetscLogEventBegin(MAT_Seqstompisym,seqmat,0,0,0);CHKERRQ(ierr);
46643c2c1871SHong Zhang 
466538f152feSBarry Smith   /* make sure it is a PETSc comm */
46660298fd71SBarry Smith   ierr = PetscCommDuplicate(comm,&comm,NULL);CHKERRQ(ierr);
4667e5f2cdd8SHong Zhang   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
4668e5f2cdd8SHong Zhang   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
466955d1abb9SHong Zhang 
4670b00a9115SJed Brown   ierr = PetscNew(&merge);CHKERRQ(ierr);
4671785e854fSJed Brown   ierr = PetscMalloc1(size,&status);CHKERRQ(ierr);
4672e5f2cdd8SHong Zhang 
46736abd8857SHong Zhang   /* determine row ownership */
4674f08fae4eSHong Zhang   /*---------------------------------------------------------*/
467526283091SBarry Smith   ierr = PetscLayoutCreate(comm,&merge->rowmap);CHKERRQ(ierr);
467626283091SBarry Smith   ierr = PetscLayoutSetLocalSize(merge->rowmap,m);CHKERRQ(ierr);
467726283091SBarry Smith   ierr = PetscLayoutSetSize(merge->rowmap,M);CHKERRQ(ierr);
467826283091SBarry Smith   ierr = PetscLayoutSetBlockSize(merge->rowmap,1);CHKERRQ(ierr);
467926283091SBarry Smith   ierr = PetscLayoutSetUp(merge->rowmap);CHKERRQ(ierr);
4680785e854fSJed Brown   ierr = PetscMalloc1(size,&len_si);CHKERRQ(ierr);
4681785e854fSJed Brown   ierr = PetscMalloc1(size,&merge->len_s);CHKERRQ(ierr);
468255d1abb9SHong Zhang 
46837a2fc3feSBarry Smith   m      = merge->rowmap->n;
46847a2fc3feSBarry Smith   owners = merge->rowmap->range;
46856abd8857SHong Zhang 
46866abd8857SHong Zhang   /* determine the number of messages to send, their lengths */
46876abd8857SHong Zhang   /*---------------------------------------------------------*/
46883e06a4e6SHong Zhang   len_s = merge->len_s;
468951a7d1a8SHong Zhang 
46902257cef7SHong Zhang   len          = 0; /* length of buf_si[] */
4691c2234fe3SHong Zhang   merge->nsend = 0;
4692409913e3SHong Zhang   for (proc=0; proc<size; proc++) {
46932257cef7SHong Zhang     len_si[proc] = 0;
46943e06a4e6SHong Zhang     if (proc == rank) {
46956abd8857SHong Zhang       len_s[proc] = 0;
46963e06a4e6SHong Zhang     } else {
469702c68681SHong Zhang       len_si[proc] = owners[proc+1] - owners[proc] + 1;
46983e06a4e6SHong Zhang       len_s[proc]  = ai[owners[proc+1]] - ai[owners[proc]]; /* num of rows to be sent to [proc] */
46993e06a4e6SHong Zhang     }
47003e06a4e6SHong Zhang     if (len_s[proc]) {
4701c2234fe3SHong Zhang       merge->nsend++;
47022257cef7SHong Zhang       nrows = 0;
47032257cef7SHong Zhang       for (i=owners[proc]; i<owners[proc+1]; i++) {
47042257cef7SHong Zhang         if (ai[i+1] > ai[i]) nrows++;
47052257cef7SHong Zhang       }
47062257cef7SHong Zhang       len_si[proc] = 2*(nrows+1);
47072257cef7SHong Zhang       len         += len_si[proc];
4708409913e3SHong Zhang     }
470958cb9c82SHong Zhang   }
4710409913e3SHong Zhang 
47112257cef7SHong Zhang   /* determine the number and length of messages to receive for ij-structure */
47122257cef7SHong Zhang   /*-------------------------------------------------------------------------*/
47130298fd71SBarry Smith   ierr = PetscGatherNumberOfMessages(comm,NULL,len_s,&merge->nrecv);CHKERRQ(ierr);
471455d1abb9SHong Zhang   ierr = PetscGatherMessageLengths2(comm,merge->nsend,merge->nrecv,len_s,len_si,&merge->id_r,&merge->len_r,&len_ri);CHKERRQ(ierr);
4715671beff6SHong Zhang 
47163e06a4e6SHong Zhang   /* post the Irecv of j-structure */
47173e06a4e6SHong Zhang   /*-------------------------------*/
47182c72b5baSSatish Balay   ierr = PetscCommGetNewTag(comm,&tagj);CHKERRQ(ierr);
47193e06a4e6SHong Zhang   ierr = PetscPostIrecvInt(comm,tagj,merge->nrecv,merge->id_r,merge->len_r,&buf_rj,&rj_waits);CHKERRQ(ierr);
472002c68681SHong Zhang 
47213e06a4e6SHong Zhang   /* post the Isend of j-structure */
4722affca5deSHong Zhang   /*--------------------------------*/
4723dcca6d9dSJed Brown   ierr = PetscMalloc2(merge->nsend,&si_waits,merge->nsend,&sj_waits);CHKERRQ(ierr);
47243e06a4e6SHong Zhang 
47252257cef7SHong Zhang   for (proc=0, k=0; proc<size; proc++) {
4726409913e3SHong Zhang     if (!len_s[proc]) continue;
472702c68681SHong Zhang     i    = owners[proc];
4728b1d57f15SBarry Smith     ierr = MPI_Isend(aj+ai[i],len_s[proc],MPIU_INT,proc,tagj,comm,sj_waits+k);CHKERRQ(ierr);
472951a7d1a8SHong Zhang     k++;
473051a7d1a8SHong Zhang   }
473151a7d1a8SHong Zhang 
47323e06a4e6SHong Zhang   /* receives and sends of j-structure are complete */
47333e06a4e6SHong Zhang   /*------------------------------------------------*/
47340c468ba9SBarry Smith   if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,rj_waits,status);CHKERRQ(ierr);}
47350c468ba9SBarry Smith   if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,sj_waits,status);CHKERRQ(ierr);}
473602c68681SHong Zhang 
473702c68681SHong Zhang   /* send and recv i-structure */
473802c68681SHong Zhang   /*---------------------------*/
47392c72b5baSSatish Balay   ierr = PetscCommGetNewTag(comm,&tagi);CHKERRQ(ierr);
474002c68681SHong Zhang   ierr = PetscPostIrecvInt(comm,tagi,merge->nrecv,merge->id_r,len_ri,&buf_ri,&ri_waits);CHKERRQ(ierr);
474102c68681SHong Zhang 
4742785e854fSJed Brown   ierr   = PetscMalloc1((len+1),&buf_s);CHKERRQ(ierr);
47433e06a4e6SHong Zhang   buf_si = buf_s;  /* points to the beginning of k-th msg to be sent */
47442257cef7SHong Zhang   for (proc=0,k=0; proc<size; proc++) {
474502c68681SHong Zhang     if (!len_s[proc]) continue;
47463e06a4e6SHong Zhang     /* form outgoing message for i-structure:
47473e06a4e6SHong Zhang          buf_si[0]:                 nrows to be sent
47483e06a4e6SHong Zhang                [1:nrows]:           row index (global)
47493e06a4e6SHong Zhang                [nrows+1:2*nrows+1]: i-structure index
47503e06a4e6SHong Zhang     */
47513e06a4e6SHong Zhang     /*-------------------------------------------*/
47522257cef7SHong Zhang     nrows       = len_si[proc]/2 - 1;
47533e06a4e6SHong Zhang     buf_si_i    = buf_si + nrows+1;
47543e06a4e6SHong Zhang     buf_si[0]   = nrows;
47553e06a4e6SHong Zhang     buf_si_i[0] = 0;
47563e06a4e6SHong Zhang     nrows       = 0;
47573e06a4e6SHong Zhang     for (i=owners[proc]; i<owners[proc+1]; i++) {
47583e06a4e6SHong Zhang       anzi = ai[i+1] - ai[i];
47593e06a4e6SHong Zhang       if (anzi) {
47603e06a4e6SHong Zhang         buf_si_i[nrows+1] = buf_si_i[nrows] + anzi; /* i-structure */
47613e06a4e6SHong Zhang         buf_si[nrows+1]   = i-owners[proc]; /* local row index */
47623e06a4e6SHong Zhang         nrows++;
47633e06a4e6SHong Zhang       }
47643e06a4e6SHong Zhang     }
4765b1d57f15SBarry Smith     ierr = MPI_Isend(buf_si,len_si[proc],MPIU_INT,proc,tagi,comm,si_waits+k);CHKERRQ(ierr);
476602c68681SHong Zhang     k++;
47672257cef7SHong Zhang     buf_si += len_si[proc];
476802c68681SHong Zhang   }
47692257cef7SHong Zhang 
47700c468ba9SBarry Smith   if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,ri_waits,status);CHKERRQ(ierr);}
47710c468ba9SBarry Smith   if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,si_waits,status);CHKERRQ(ierr);}
477202c68681SHong Zhang 
4773ae15b995SBarry Smith   ierr = PetscInfo2(seqmat,"nsend: %D, nrecv: %D\n",merge->nsend,merge->nrecv);CHKERRQ(ierr);
47743e06a4e6SHong Zhang   for (i=0; i<merge->nrecv; i++) {
4775ae15b995SBarry 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);
47763e06a4e6SHong Zhang   }
47773e06a4e6SHong Zhang 
47783e06a4e6SHong Zhang   ierr = PetscFree(len_si);CHKERRQ(ierr);
477902c68681SHong Zhang   ierr = PetscFree(len_ri);CHKERRQ(ierr);
478002c68681SHong Zhang   ierr = PetscFree(rj_waits);CHKERRQ(ierr);
47811d79065fSBarry Smith   ierr = PetscFree2(si_waits,sj_waits);CHKERRQ(ierr);
47822257cef7SHong Zhang   ierr = PetscFree(ri_waits);CHKERRQ(ierr);
47833e06a4e6SHong Zhang   ierr = PetscFree(buf_s);CHKERRQ(ierr);
4784bcc1bcd5SHong Zhang   ierr = PetscFree(status);CHKERRQ(ierr);
478558cb9c82SHong Zhang 
4786bcc1bcd5SHong Zhang   /* compute a local seq matrix in each processor */
4787bcc1bcd5SHong Zhang   /*----------------------------------------------*/
478858cb9c82SHong Zhang   /* allocate bi array and free space for accumulating nonzero column info */
4789785e854fSJed Brown   ierr  = PetscMalloc1((m+1),&bi);CHKERRQ(ierr);
479058cb9c82SHong Zhang   bi[0] = 0;
479158cb9c82SHong Zhang 
4792be0fcf8dSHong Zhang   /* create and initialize a linked list */
4793be0fcf8dSHong Zhang   nlnk = N+1;
4794be0fcf8dSHong Zhang   ierr = PetscLLCreate(N,N,nlnk,lnk,lnkbt);CHKERRQ(ierr);
479558cb9c82SHong Zhang 
4796bcc1bcd5SHong Zhang   /* initial FreeSpace size is 2*(num of local nnz(seqmat)) */
4797bcc1bcd5SHong Zhang   len  = ai[owners[rank+1]] - ai[owners[rank]];
4798a1a86e44SBarry Smith   ierr = PetscFreeSpaceGet((PetscInt)(2*len+1),&free_space);CHKERRQ(ierr);
47992205254eSKarl Rupp 
480058cb9c82SHong Zhang   current_space = free_space;
480158cb9c82SHong Zhang 
4802bcc1bcd5SHong Zhang   /* determine symbolic info for each local row */
4803dcca6d9dSJed Brown   ierr = PetscMalloc3(merge->nrecv,&buf_ri_k,merge->nrecv,&nextrow,merge->nrecv,&nextai);CHKERRQ(ierr);
48041d79065fSBarry Smith 
48053e06a4e6SHong Zhang   for (k=0; k<merge->nrecv; k++) {
48062257cef7SHong Zhang     buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
48073e06a4e6SHong Zhang     nrows       = *buf_ri_k[k];
48083e06a4e6SHong Zhang     nextrow[k]  = buf_ri_k[k] + 1;  /* next row number of k-th recved i-structure */
48092257cef7SHong Zhang     nextai[k]   = buf_ri_k[k] + (nrows + 1); /* poins to the next i-structure of k-th recved i-structure  */
48103e06a4e6SHong Zhang   }
48112257cef7SHong Zhang 
4812bcc1bcd5SHong Zhang   ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr);
4813bcc1bcd5SHong Zhang   len  = 0;
481458cb9c82SHong Zhang   for (i=0; i<m; i++) {
481558cb9c82SHong Zhang     bnzi = 0;
481658cb9c82SHong Zhang     /* add local non-zero cols of this proc's seqmat into lnk */
481758cb9c82SHong Zhang     arow  = owners[rank] + i;
481858cb9c82SHong Zhang     anzi  = ai[arow+1] - ai[arow];
481958cb9c82SHong Zhang     aj    = a->j + ai[arow];
4820dadf0e6bSHong Zhang     ierr  = PetscLLAddSorted(anzi,aj,N,nlnk,lnk,lnkbt);CHKERRQ(ierr);
482158cb9c82SHong Zhang     bnzi += nlnk;
482258cb9c82SHong Zhang     /* add received col data into lnk */
482351a7d1a8SHong Zhang     for (k=0; k<merge->nrecv; k++) { /* k-th received message */
482455d1abb9SHong Zhang       if (i == *nextrow[k]) { /* i-th row */
48253e06a4e6SHong Zhang         anzi  = *(nextai[k]+1) - *nextai[k];
48263e06a4e6SHong Zhang         aj    = buf_rj[k] + *nextai[k];
4827dadf0e6bSHong Zhang         ierr  = PetscLLAddSorted(anzi,aj,N,nlnk,lnk,lnkbt);CHKERRQ(ierr);
48283e06a4e6SHong Zhang         bnzi += nlnk;
48293e06a4e6SHong Zhang         nextrow[k]++; nextai[k]++;
48303e06a4e6SHong Zhang       }
483158cb9c82SHong Zhang     }
4832bcc1bcd5SHong Zhang     if (len < bnzi) len = bnzi;  /* =max(bnzi) */
483358cb9c82SHong Zhang 
483458cb9c82SHong Zhang     /* if free space is not available, make more free space */
483558cb9c82SHong Zhang     if (current_space->local_remaining<bnzi) {
48364238b7adSHong Zhang       ierr = PetscFreeSpaceGet(bnzi+current_space->total_array_size,&current_space);CHKERRQ(ierr);
483758cb9c82SHong Zhang       nspacedouble++;
483858cb9c82SHong Zhang     }
483958cb9c82SHong Zhang     /* copy data into free space, then initialize lnk */
4840be0fcf8dSHong Zhang     ierr = PetscLLClean(N,N,bnzi,lnk,current_space->array,lnkbt);CHKERRQ(ierr);
4841bcc1bcd5SHong Zhang     ierr = MatPreallocateSet(i+owners[rank],bnzi,current_space->array,dnz,onz);CHKERRQ(ierr);
4842bcc1bcd5SHong Zhang 
484358cb9c82SHong Zhang     current_space->array           += bnzi;
484458cb9c82SHong Zhang     current_space->local_used      += bnzi;
484558cb9c82SHong Zhang     current_space->local_remaining -= bnzi;
484658cb9c82SHong Zhang 
484758cb9c82SHong Zhang     bi[i+1] = bi[i] + bnzi;
484858cb9c82SHong Zhang   }
4849bcc1bcd5SHong Zhang 
48501d79065fSBarry Smith   ierr = PetscFree3(buf_ri_k,nextrow,nextai);CHKERRQ(ierr);
4851bcc1bcd5SHong Zhang 
4852785e854fSJed Brown   ierr = PetscMalloc1((bi[m]+1),&bj);CHKERRQ(ierr);
4853a1a86e44SBarry Smith   ierr = PetscFreeSpaceContiguous(&free_space,bj);CHKERRQ(ierr);
4854be0fcf8dSHong Zhang   ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr);
4855409913e3SHong Zhang 
4856bcc1bcd5SHong Zhang   /* create symbolic parallel matrix B_mpi */
4857bcc1bcd5SHong Zhang   /*---------------------------------------*/
4858a2f3521dSMark F. Adams   ierr = MatGetBlockSizes(seqmat,&bs,&cbs);CHKERRQ(ierr);
4859f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,&B_mpi);CHKERRQ(ierr);
486054b84b50SHong Zhang   if (n==PETSC_DECIDE) {
4861f69a0ea3SMatthew Knepley     ierr = MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,N);CHKERRQ(ierr);
486254b84b50SHong Zhang   } else {
4863f69a0ea3SMatthew Knepley     ierr = MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
486454b84b50SHong Zhang   }
4865a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(B_mpi,bs,cbs);CHKERRQ(ierr);
4866bcc1bcd5SHong Zhang   ierr = MatSetType(B_mpi,MATMPIAIJ);CHKERRQ(ierr);
4867bcc1bcd5SHong Zhang   ierr = MatMPIAIJSetPreallocation(B_mpi,0,dnz,0,onz);CHKERRQ(ierr);
4868bcc1bcd5SHong Zhang   ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr);
48697e63b356SHong Zhang   ierr = MatSetOption(B_mpi,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr);
487058cb9c82SHong Zhang 
487190431a8fSHong Zhang   /* B_mpi is not ready for use - assembly will be done by MatCreateMPIAIJSumSeqAIJNumeric() */
48726abd8857SHong Zhang   B_mpi->assembled    = PETSC_FALSE;
4873affca5deSHong Zhang   B_mpi->ops->destroy = MatDestroy_MPIAIJ_SeqsToMPI;
4874affca5deSHong Zhang   merge->bi           = bi;
4875affca5deSHong Zhang   merge->bj           = bj;
487602c68681SHong Zhang   merge->buf_ri       = buf_ri;
487702c68681SHong Zhang   merge->buf_rj       = buf_rj;
48780298fd71SBarry Smith   merge->coi          = NULL;
48790298fd71SBarry Smith   merge->coj          = NULL;
48800298fd71SBarry Smith   merge->owners_co    = NULL;
4881affca5deSHong Zhang 
4882bf0cc555SLisandro Dalcin   ierr = PetscCommDestroy(&comm);CHKERRQ(ierr);
4883bf0cc555SLisandro Dalcin 
4884affca5deSHong Zhang   /* attach the supporting struct to B_mpi for reuse */
4885776b82aeSLisandro Dalcin   ierr    = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr);
4886776b82aeSLisandro Dalcin   ierr    = PetscContainerSetPointer(container,merge);CHKERRQ(ierr);
4887affca5deSHong Zhang   ierr    = PetscObjectCompose((PetscObject)B_mpi,"MatMergeSeqsToMPI",(PetscObject)container);CHKERRQ(ierr);
4888bf0cc555SLisandro Dalcin   ierr    = PetscContainerDestroy(&container);CHKERRQ(ierr);
4889affca5deSHong Zhang   *mpimat = B_mpi;
489038f152feSBarry Smith 
48914ebed01fSBarry Smith   ierr = PetscLogEventEnd(MAT_Seqstompisym,seqmat,0,0,0);CHKERRQ(ierr);
4892e5f2cdd8SHong Zhang   PetscFunctionReturn(0);
4893e5f2cdd8SHong Zhang }
489425616d81SHong Zhang 
489538f152feSBarry Smith #undef __FUNCT__
489690431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJSumSeqAIJ"
4897d4036a1aSHong Zhang /*@C
489890431a8fSHong Zhang       MatCreateMPIAIJSumSeqAIJ - Creates a MPIAIJ matrix by adding sequential
4899d4036a1aSHong Zhang                  matrices from each processor
4900d4036a1aSHong Zhang 
4901d4036a1aSHong Zhang     Collective on MPI_Comm
4902d4036a1aSHong Zhang 
4903d4036a1aSHong Zhang    Input Parameters:
4904d4036a1aSHong Zhang +    comm - the communicators the parallel matrix will live on
4905d4036a1aSHong Zhang .    seqmat - the input sequential matrices
4906d4036a1aSHong Zhang .    m - number of local rows (or PETSC_DECIDE)
4907d4036a1aSHong Zhang .    n - number of local columns (or PETSC_DECIDE)
4908d4036a1aSHong Zhang -    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
4909d4036a1aSHong Zhang 
4910d4036a1aSHong Zhang    Output Parameter:
4911d4036a1aSHong Zhang .    mpimat - the parallel matrix generated
4912d4036a1aSHong Zhang 
4913d4036a1aSHong Zhang     Level: advanced
4914d4036a1aSHong Zhang 
4915d4036a1aSHong Zhang    Notes:
4916d4036a1aSHong Zhang      The dimensions of the sequential matrix in each processor MUST be the same.
4917d4036a1aSHong Zhang      The input seqmat is included into the container "Mat_Merge_SeqsToMPI", and will be
4918d4036a1aSHong Zhang      destroyed when mpimat is destroyed. Call PetscObjectQuery() to access seqmat.
4919d4036a1aSHong Zhang @*/
492090431a8fSHong Zhang PetscErrorCode  MatCreateMPIAIJSumSeqAIJ(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,MatReuse scall,Mat *mpimat)
492155d1abb9SHong Zhang {
492255d1abb9SHong Zhang   PetscErrorCode ierr;
49237e63b356SHong Zhang   PetscMPIInt    size;
492455d1abb9SHong Zhang 
492555d1abb9SHong Zhang   PetscFunctionBegin;
49267e63b356SHong Zhang   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
49277e63b356SHong Zhang   if (size == 1) {
49287e63b356SHong Zhang     ierr = PetscLogEventBegin(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr);
49297e63b356SHong Zhang     if (scall == MAT_INITIAL_MATRIX) {
49307e63b356SHong Zhang       ierr = MatDuplicate(seqmat,MAT_COPY_VALUES,mpimat);CHKERRQ(ierr);
49317e63b356SHong Zhang     } else {
49327e63b356SHong Zhang       ierr = MatCopy(seqmat,*mpimat,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
49337e63b356SHong Zhang     }
49347e63b356SHong Zhang     ierr = PetscLogEventEnd(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr);
49357e63b356SHong Zhang     PetscFunctionReturn(0);
49367e63b356SHong Zhang   }
49374ebed01fSBarry Smith   ierr = PetscLogEventBegin(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr);
493855d1abb9SHong Zhang   if (scall == MAT_INITIAL_MATRIX) {
493990431a8fSHong Zhang     ierr = MatCreateMPIAIJSumSeqAIJSymbolic(comm,seqmat,m,n,mpimat);CHKERRQ(ierr);
494055d1abb9SHong Zhang   }
494190431a8fSHong Zhang   ierr = MatCreateMPIAIJSumSeqAIJNumeric(seqmat,*mpimat);CHKERRQ(ierr);
49424ebed01fSBarry Smith   ierr = PetscLogEventEnd(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr);
494355d1abb9SHong Zhang   PetscFunctionReturn(0);
494455d1abb9SHong Zhang }
49454ebed01fSBarry Smith 
494625616d81SHong Zhang #undef __FUNCT__
49474a2b5492SBarry Smith #define __FUNCT__ "MatMPIAIJGetLocalMat"
4948bc08b0f1SBarry Smith /*@
49494a2b5492SBarry Smith      MatMPIAIJGetLocalMat - Creates a SeqAIJ from a MPIAIJ matrix by taking all its local rows and putting them into a sequential vector with
49508661ff28SBarry Smith           mlocal rows and n columns. Where mlocal is the row count obtained with MatGetLocalSize() and n is the global column count obtained
49518661ff28SBarry Smith           with MatGetSize()
495225616d81SHong Zhang 
495332fba14fSHong Zhang     Not Collective
495425616d81SHong Zhang 
495525616d81SHong Zhang    Input Parameters:
495625616d81SHong Zhang +    A - the matrix
495725616d81SHong Zhang .    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
495825616d81SHong Zhang 
495925616d81SHong Zhang    Output Parameter:
496025616d81SHong Zhang .    A_loc - the local sequential matrix generated
496125616d81SHong Zhang 
496225616d81SHong Zhang     Level: developer
496325616d81SHong Zhang 
4964ba264940SBarry Smith .seealso: MatGetOwnerShipRange(), MatMPIAIJGetLocalMatCondensed()
49658661ff28SBarry Smith 
496625616d81SHong Zhang @*/
49674a2b5492SBarry Smith PetscErrorCode  MatMPIAIJGetLocalMat(Mat A,MatReuse scall,Mat *A_loc)
496825616d81SHong Zhang {
496925616d81SHong Zhang   PetscErrorCode ierr;
497001b7ae99SHong Zhang   Mat_MPIAIJ     *mpimat=(Mat_MPIAIJ*)A->data;
4971b78526a6SJose E. Roman   Mat_SeqAIJ     *mat,*a,*b;
4972b78526a6SJose E. Roman   PetscInt       *ai,*aj,*bi,*bj,*cmap=mpimat->garray;
4973b78526a6SJose E. Roman   MatScalar      *aa,*ba,*cam;
4974a77337e4SBarry Smith   PetscScalar    *ca;
4975d0f46423SBarry Smith   PetscInt       am=A->rmap->n,i,j,k,cstart=A->cmap->rstart;
49765a7d977cSHong Zhang   PetscInt       *ci,*cj,col,ncols_d,ncols_o,jo;
49778661ff28SBarry Smith   PetscBool      match;
497825616d81SHong Zhang 
497925616d81SHong Zhang   PetscFunctionBegin;
4980251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&match);CHKERRQ(ierr);
4981ce94432eSBarry Smith   if (!match) SETERRQ(PetscObjectComm((PetscObject)A), PETSC_ERR_SUP,"Requires MPIAIJ matrix as input");
49824ebed01fSBarry Smith   ierr = PetscLogEventBegin(MAT_Getlocalmat,A,0,0,0);CHKERRQ(ierr);
4983b78526a6SJose E. Roman   a = (Mat_SeqAIJ*)(mpimat->A)->data;
4984b78526a6SJose E. Roman   b = (Mat_SeqAIJ*)(mpimat->B)->data;
4985b78526a6SJose E. Roman   ai = a->i; aj = a->j; bi = b->i; bj = b->j;
4986b78526a6SJose E. Roman   aa = a->a; ba = b->a;
498701b7ae99SHong Zhang   if (scall == MAT_INITIAL_MATRIX) {
4988785e854fSJed Brown     ierr  = PetscMalloc1((1+am),&ci);CHKERRQ(ierr);
4989dea91ad1SHong Zhang     ci[0] = 0;
499001b7ae99SHong Zhang     for (i=0; i<am; i++) {
4991dea91ad1SHong Zhang       ci[i+1] = ci[i] + (ai[i+1] - ai[i]) + (bi[i+1] - bi[i]);
499201b7ae99SHong Zhang     }
4993785e854fSJed Brown     ierr = PetscMalloc1((1+ci[am]),&cj);CHKERRQ(ierr);
4994785e854fSJed Brown     ierr = PetscMalloc1((1+ci[am]),&ca);CHKERRQ(ierr);
4995dea91ad1SHong Zhang     k    = 0;
499601b7ae99SHong Zhang     for (i=0; i<am; i++) {
49975a7d977cSHong Zhang       ncols_o = bi[i+1] - bi[i];
49985a7d977cSHong Zhang       ncols_d = ai[i+1] - ai[i];
499901b7ae99SHong Zhang       /* off-diagonal portion of A */
50005a7d977cSHong Zhang       for (jo=0; jo<ncols_o; jo++) {
50015a7d977cSHong Zhang         col = cmap[*bj];
50025a7d977cSHong Zhang         if (col >= cstart) break;
50035a7d977cSHong Zhang         cj[k]   = col; bj++;
50045a7d977cSHong Zhang         ca[k++] = *ba++;
50055a7d977cSHong Zhang       }
50065a7d977cSHong Zhang       /* diagonal portion of A */
50075a7d977cSHong Zhang       for (j=0; j<ncols_d; j++) {
50085a7d977cSHong Zhang         cj[k]   = cstart + *aj++;
50095a7d977cSHong Zhang         ca[k++] = *aa++;
50105a7d977cSHong Zhang       }
50115a7d977cSHong Zhang       /* off-diagonal portion of A */
50125a7d977cSHong Zhang       for (j=jo; j<ncols_o; j++) {
50135a7d977cSHong Zhang         cj[k]   = cmap[*bj++];
50145a7d977cSHong Zhang         ca[k++] = *ba++;
50155a7d977cSHong Zhang       }
501625616d81SHong Zhang     }
5017dea91ad1SHong Zhang     /* put together the new matrix */
5018d0f46423SBarry Smith     ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,am,A->cmap->N,ci,cj,ca,A_loc);CHKERRQ(ierr);
5019dea91ad1SHong Zhang     /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
5020dea91ad1SHong Zhang     /* Since these are PETSc arrays, change flags to free them as necessary. */
5021dea91ad1SHong Zhang     mat          = (Mat_SeqAIJ*)(*A_loc)->data;
5022e6b907acSBarry Smith     mat->free_a  = PETSC_TRUE;
5023e6b907acSBarry Smith     mat->free_ij = PETSC_TRUE;
5024dea91ad1SHong Zhang     mat->nonew   = 0;
50255a7d977cSHong Zhang   } else if (scall == MAT_REUSE_MATRIX) {
50265a7d977cSHong Zhang     mat=(Mat_SeqAIJ*)(*A_loc)->data;
5027a77337e4SBarry Smith     ci = mat->i; cj = mat->j; cam = mat->a;
50285a7d977cSHong Zhang     for (i=0; i<am; i++) {
50295a7d977cSHong Zhang       /* off-diagonal portion of A */
50305a7d977cSHong Zhang       ncols_o = bi[i+1] - bi[i];
50315a7d977cSHong Zhang       for (jo=0; jo<ncols_o; jo++) {
50325a7d977cSHong Zhang         col = cmap[*bj];
50335a7d977cSHong Zhang         if (col >= cstart) break;
5034a77337e4SBarry Smith         *cam++ = *ba++; bj++;
50355a7d977cSHong Zhang       }
50365a7d977cSHong Zhang       /* diagonal portion of A */
5037ecc9b87dSHong Zhang       ncols_d = ai[i+1] - ai[i];
5038a77337e4SBarry Smith       for (j=0; j<ncols_d; j++) *cam++ = *aa++;
50395a7d977cSHong Zhang       /* off-diagonal portion of A */
5040f33d1a9aSHong Zhang       for (j=jo; j<ncols_o; j++) {
5041a77337e4SBarry Smith         *cam++ = *ba++; bj++;
5042f33d1a9aSHong Zhang       }
50435a7d977cSHong Zhang     }
50448661ff28SBarry Smith   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall);
50454ebed01fSBarry Smith   ierr = PetscLogEventEnd(MAT_Getlocalmat,A,0,0,0);CHKERRQ(ierr);
504625616d81SHong Zhang   PetscFunctionReturn(0);
504725616d81SHong Zhang }
504825616d81SHong Zhang 
504932fba14fSHong Zhang #undef __FUNCT__
50504a2b5492SBarry Smith #define __FUNCT__ "MatMPIAIJGetLocalMatCondensed"
505132fba14fSHong Zhang /*@C
5052ba264940SBarry Smith      MatMPIAIJGetLocalMatCondensed - Creates a SeqAIJ matrix from an MPIAIJ matrix by taking all its local rows and NON-ZERO columns
505332fba14fSHong Zhang 
505432fba14fSHong Zhang     Not Collective
505532fba14fSHong Zhang 
505632fba14fSHong Zhang    Input Parameters:
505732fba14fSHong Zhang +    A - the matrix
505832fba14fSHong Zhang .    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
50590298fd71SBarry Smith -    row, col - index sets of rows and columns to extract (or NULL)
506032fba14fSHong Zhang 
506132fba14fSHong Zhang    Output Parameter:
506232fba14fSHong Zhang .    A_loc - the local sequential matrix generated
506332fba14fSHong Zhang 
506432fba14fSHong Zhang     Level: developer
506532fba14fSHong Zhang 
5066ba264940SBarry Smith .seealso: MatGetOwnershipRange(), MatMPIAIJGetLocalMat()
5067ba264940SBarry Smith 
506832fba14fSHong Zhang @*/
50694a2b5492SBarry Smith PetscErrorCode  MatMPIAIJGetLocalMatCondensed(Mat A,MatReuse scall,IS *row,IS *col,Mat *A_loc)
507032fba14fSHong Zhang {
507132fba14fSHong Zhang   Mat_MPIAIJ     *a=(Mat_MPIAIJ*)A->data;
507232fba14fSHong Zhang   PetscErrorCode ierr;
507332fba14fSHong Zhang   PetscInt       i,start,end,ncols,nzA,nzB,*cmap,imark,*idx;
507432fba14fSHong Zhang   IS             isrowa,iscola;
507532fba14fSHong Zhang   Mat            *aloc;
50764a2b5492SBarry Smith   PetscBool      match;
507732fba14fSHong Zhang 
507832fba14fSHong Zhang   PetscFunctionBegin;
5079251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&match);CHKERRQ(ierr);
5080ce94432eSBarry Smith   if (!match) SETERRQ(PetscObjectComm((PetscObject)A), PETSC_ERR_SUP,"Requires MPIAIJ matrix as input");
50814ebed01fSBarry Smith   ierr = PetscLogEventBegin(MAT_Getlocalmatcondensed,A,0,0,0);CHKERRQ(ierr);
508232fba14fSHong Zhang   if (!row) {
5083d0f46423SBarry Smith     start = A->rmap->rstart; end = A->rmap->rend;
508432fba14fSHong Zhang     ierr  = ISCreateStride(PETSC_COMM_SELF,end-start,start,1,&isrowa);CHKERRQ(ierr);
508532fba14fSHong Zhang   } else {
508632fba14fSHong Zhang     isrowa = *row;
508732fba14fSHong Zhang   }
508832fba14fSHong Zhang   if (!col) {
5089d0f46423SBarry Smith     start = A->cmap->rstart;
509032fba14fSHong Zhang     cmap  = a->garray;
5091d0f46423SBarry Smith     nzA   = a->A->cmap->n;
5092d0f46423SBarry Smith     nzB   = a->B->cmap->n;
5093785e854fSJed Brown     ierr  = PetscMalloc1((nzA+nzB), &idx);CHKERRQ(ierr);
509432fba14fSHong Zhang     ncols = 0;
509532fba14fSHong Zhang     for (i=0; i<nzB; i++) {
509632fba14fSHong Zhang       if (cmap[i] < start) idx[ncols++] = cmap[i];
509732fba14fSHong Zhang       else break;
509832fba14fSHong Zhang     }
509932fba14fSHong Zhang     imark = i;
510032fba14fSHong Zhang     for (i=0; i<nzA; i++) idx[ncols++] = start + i;
510132fba14fSHong Zhang     for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i];
5102d67e408aSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,&iscola);CHKERRQ(ierr);
510332fba14fSHong Zhang   } else {
510432fba14fSHong Zhang     iscola = *col;
510532fba14fSHong Zhang   }
510632fba14fSHong Zhang   if (scall != MAT_INITIAL_MATRIX) {
510732fba14fSHong Zhang     ierr    = PetscMalloc(sizeof(Mat),&aloc);CHKERRQ(ierr);
510832fba14fSHong Zhang     aloc[0] = *A_loc;
510932fba14fSHong Zhang   }
511032fba14fSHong Zhang   ierr   = MatGetSubMatrices(A,1,&isrowa,&iscola,scall,&aloc);CHKERRQ(ierr);
511132fba14fSHong Zhang   *A_loc = aloc[0];
511232fba14fSHong Zhang   ierr   = PetscFree(aloc);CHKERRQ(ierr);
511332fba14fSHong Zhang   if (!row) {
51146bf464f9SBarry Smith     ierr = ISDestroy(&isrowa);CHKERRQ(ierr);
511532fba14fSHong Zhang   }
511632fba14fSHong Zhang   if (!col) {
51176bf464f9SBarry Smith     ierr = ISDestroy(&iscola);CHKERRQ(ierr);
511832fba14fSHong Zhang   }
51194ebed01fSBarry Smith   ierr = PetscLogEventEnd(MAT_Getlocalmatcondensed,A,0,0,0);CHKERRQ(ierr);
512032fba14fSHong Zhang   PetscFunctionReturn(0);
512132fba14fSHong Zhang }
512232fba14fSHong Zhang 
512325616d81SHong Zhang #undef __FUNCT__
512425616d81SHong Zhang #define __FUNCT__ "MatGetBrowsOfAcols"
512525616d81SHong Zhang /*@C
512632fba14fSHong Zhang     MatGetBrowsOfAcols - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns of local A
512725616d81SHong Zhang 
512825616d81SHong Zhang     Collective on Mat
512925616d81SHong Zhang 
513025616d81SHong Zhang    Input Parameters:
5131e240928fSHong Zhang +    A,B - the matrices in mpiaij format
513225616d81SHong Zhang .    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
51330298fd71SBarry Smith -    rowb, colb - index sets of rows and columns of B to extract (or NULL)
513425616d81SHong Zhang 
513525616d81SHong Zhang    Output Parameter:
513625616d81SHong Zhang +    rowb, colb - index sets of rows and columns of B to extract
513725616d81SHong Zhang -    B_seq - the sequential matrix generated
513825616d81SHong Zhang 
513925616d81SHong Zhang     Level: developer
514025616d81SHong Zhang 
514125616d81SHong Zhang @*/
514266bfb163SHong Zhang PetscErrorCode  MatGetBrowsOfAcols(Mat A,Mat B,MatReuse scall,IS *rowb,IS *colb,Mat *B_seq)
514325616d81SHong Zhang {
5144899cda47SBarry Smith   Mat_MPIAIJ     *a=(Mat_MPIAIJ*)A->data;
514525616d81SHong Zhang   PetscErrorCode ierr;
5146b1d57f15SBarry Smith   PetscInt       *idx,i,start,ncols,nzA,nzB,*cmap,imark;
514725616d81SHong Zhang   IS             isrowb,iscolb;
51480298fd71SBarry Smith   Mat            *bseq=NULL;
514925616d81SHong Zhang 
515025616d81SHong Zhang   PetscFunctionBegin;
5151d0f46423SBarry Smith   if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend) {
5152e32f2f54SBarry 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);
515325616d81SHong Zhang   }
51544ebed01fSBarry Smith   ierr = PetscLogEventBegin(MAT_GetBrowsOfAcols,A,B,0,0);CHKERRQ(ierr);
515525616d81SHong Zhang 
515625616d81SHong Zhang   if (scall == MAT_INITIAL_MATRIX) {
5157d0f46423SBarry Smith     start = A->cmap->rstart;
515825616d81SHong Zhang     cmap  = a->garray;
5159d0f46423SBarry Smith     nzA   = a->A->cmap->n;
5160d0f46423SBarry Smith     nzB   = a->B->cmap->n;
5161785e854fSJed Brown     ierr  = PetscMalloc1((nzA+nzB), &idx);CHKERRQ(ierr);
516225616d81SHong Zhang     ncols = 0;
51630390132cSHong Zhang     for (i=0; i<nzB; i++) {  /* row < local row index */
516425616d81SHong Zhang       if (cmap[i] < start) idx[ncols++] = cmap[i];
516525616d81SHong Zhang       else break;
516625616d81SHong Zhang     }
516725616d81SHong Zhang     imark = i;
51680390132cSHong Zhang     for (i=0; i<nzA; i++) idx[ncols++] = start + i;  /* local rows */
51690390132cSHong Zhang     for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i]; /* row > local row index */
5170d67e408aSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,&isrowb);CHKERRQ(ierr);
5171d0f46423SBarry Smith     ierr = ISCreateStride(PETSC_COMM_SELF,B->cmap->N,0,1,&iscolb);CHKERRQ(ierr);
517225616d81SHong Zhang   } else {
5173e32f2f54SBarry Smith     if (!rowb || !colb) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"IS rowb and colb must be provided for MAT_REUSE_MATRIX");
517425616d81SHong Zhang     isrowb  = *rowb; iscolb = *colb;
517525616d81SHong Zhang     ierr    = PetscMalloc(sizeof(Mat),&bseq);CHKERRQ(ierr);
517625616d81SHong Zhang     bseq[0] = *B_seq;
517725616d81SHong Zhang   }
517825616d81SHong Zhang   ierr   = MatGetSubMatrices(B,1,&isrowb,&iscolb,scall,&bseq);CHKERRQ(ierr);
517925616d81SHong Zhang   *B_seq = bseq[0];
518025616d81SHong Zhang   ierr   = PetscFree(bseq);CHKERRQ(ierr);
518125616d81SHong Zhang   if (!rowb) {
51826bf464f9SBarry Smith     ierr = ISDestroy(&isrowb);CHKERRQ(ierr);
518325616d81SHong Zhang   } else {
518425616d81SHong Zhang     *rowb = isrowb;
518525616d81SHong Zhang   }
518625616d81SHong Zhang   if (!colb) {
51876bf464f9SBarry Smith     ierr = ISDestroy(&iscolb);CHKERRQ(ierr);
518825616d81SHong Zhang   } else {
518925616d81SHong Zhang     *colb = iscolb;
519025616d81SHong Zhang   }
51914ebed01fSBarry Smith   ierr = PetscLogEventEnd(MAT_GetBrowsOfAcols,A,B,0,0);CHKERRQ(ierr);
519225616d81SHong Zhang   PetscFunctionReturn(0);
519325616d81SHong Zhang }
5194429d309bSHong Zhang 
5195a61c8c0fSHong Zhang #undef __FUNCT__
5196f8487c73SHong Zhang #define __FUNCT__ "MatGetBrowsOfAoCols_MPIAIJ"
5197f8487c73SHong Zhang /*
5198f8487c73SHong Zhang     MatGetBrowsOfAoCols_MPIAIJ - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns
519901b7ae99SHong Zhang     of the OFF-DIAGONAL portion of local A
5200429d309bSHong Zhang 
5201429d309bSHong Zhang     Collective on Mat
5202429d309bSHong Zhang 
5203429d309bSHong Zhang    Input Parameters:
5204429d309bSHong Zhang +    A,B - the matrices in mpiaij format
5205598bc09dSHong Zhang -    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
5206429d309bSHong Zhang 
5207429d309bSHong Zhang    Output Parameter:
52080298fd71SBarry Smith +    startsj_s - starting point in B's sending j-arrays, saved for MAT_REUSE (or NULL)
52090298fd71SBarry Smith .    startsj_r - starting point in B's receiving j-arrays, saved for MAT_REUSE (or NULL)
52100298fd71SBarry Smith .    bufa_ptr - array for sending matrix values, saved for MAT_REUSE (or NULL)
5211598bc09dSHong Zhang -    B_oth - the sequential matrix generated with size aBn=a->B->cmap->n by B->cmap->N
5212429d309bSHong Zhang 
5213429d309bSHong Zhang     Level: developer
5214429d309bSHong Zhang 
5215f8487c73SHong Zhang */
5216b7f45c76SHong Zhang PetscErrorCode  MatGetBrowsOfAoCols_MPIAIJ(Mat A,Mat B,MatReuse scall,PetscInt **startsj_s,PetscInt **startsj_r,MatScalar **bufa_ptr,Mat *B_oth)
5217429d309bSHong Zhang {
5218a6b2eed2SHong Zhang   VecScatter_MPI_General *gen_to,*gen_from;
5219429d309bSHong Zhang   PetscErrorCode         ierr;
5220899cda47SBarry Smith   Mat_MPIAIJ             *a=(Mat_MPIAIJ*)A->data;
522187025532SHong Zhang   Mat_SeqAIJ             *b_oth;
5222a6b2eed2SHong Zhang   VecScatter             ctx =a->Mvctx;
5223ce94432eSBarry Smith   MPI_Comm               comm;
52247adad957SLisandro Dalcin   PetscMPIInt            *rprocs,*sprocs,tag=((PetscObject)ctx)->tag,rank;
5225d0f46423SBarry Smith   PetscInt               *rowlen,*bufj,*bufJ,ncols,aBn=a->B->cmap->n,row,*b_othi,*b_othj;
5226dd6ea824SBarry Smith   PetscScalar            *rvalues,*svalues;
5227dd6ea824SBarry Smith   MatScalar              *b_otha,*bufa,*bufA;
5228e42f35eeSHong Zhang   PetscInt               i,j,k,l,ll,nrecvs,nsends,nrows,*srow,*rstarts,*rstartsj = 0,*sstarts,*sstartsj,len;
52290298fd71SBarry Smith   MPI_Request            *rwaits = NULL,*swaits = NULL;
523087025532SHong Zhang   MPI_Status             *sstatus,rstatus;
5231aa5bb8c0SSatish Balay   PetscMPIInt            jj;
5232e42f35eeSHong Zhang   PetscInt               *cols,sbs,rbs;
5233ba8c8a56SBarry Smith   PetscScalar            *vals;
5234429d309bSHong Zhang 
5235429d309bSHong Zhang   PetscFunctionBegin;
5236ce94432eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)A,&comm);CHKERRQ(ierr);
5237d0f46423SBarry Smith   if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend) {
5238e32f2f54SBarry 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);
5239429d309bSHong Zhang   }
52404ebed01fSBarry Smith   ierr = PetscLogEventBegin(MAT_GetBrowsOfAocols,A,B,0,0);CHKERRQ(ierr);
5241a6b2eed2SHong Zhang   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
5242a6b2eed2SHong Zhang 
5243a6b2eed2SHong Zhang   gen_to   = (VecScatter_MPI_General*)ctx->todata;
5244a6b2eed2SHong Zhang   gen_from = (VecScatter_MPI_General*)ctx->fromdata;
5245e42f35eeSHong Zhang   rvalues  = gen_from->values; /* holds the length of receiving row */
5246e42f35eeSHong Zhang   svalues  = gen_to->values;   /* holds the length of sending row */
5247a6b2eed2SHong Zhang   nrecvs   = gen_from->n;
5248a6b2eed2SHong Zhang   nsends   = gen_to->n;
5249d7ee0231SBarry Smith 
5250dcca6d9dSJed Brown   ierr    = PetscMalloc2(nrecvs,&rwaits,nsends,&swaits);CHKERRQ(ierr);
5251a6b2eed2SHong Zhang   srow    = gen_to->indices;    /* local row index to be sent */
5252a6b2eed2SHong Zhang   sstarts = gen_to->starts;
5253a6b2eed2SHong Zhang   sprocs  = gen_to->procs;
5254a6b2eed2SHong Zhang   sstatus = gen_to->sstatus;
5255e42f35eeSHong Zhang   sbs     = gen_to->bs;
5256e42f35eeSHong Zhang   rstarts = gen_from->starts;
5257e42f35eeSHong Zhang   rprocs  = gen_from->procs;
5258e42f35eeSHong Zhang   rbs     = gen_from->bs;
5259429d309bSHong Zhang 
5260b7f45c76SHong Zhang   if (!startsj_s || !bufa_ptr) scall = MAT_INITIAL_MATRIX;
5261429d309bSHong Zhang   if (scall == MAT_INITIAL_MATRIX) {
5262a6b2eed2SHong Zhang     /* i-array */
5263a6b2eed2SHong Zhang     /*---------*/
5264a6b2eed2SHong Zhang     /*  post receives */
5265a6b2eed2SHong Zhang     for (i=0; i<nrecvs; i++) {
5266e42f35eeSHong Zhang       rowlen = (PetscInt*)rvalues + rstarts[i]*rbs;
5267e42f35eeSHong Zhang       nrows  = (rstarts[i+1]-rstarts[i])*rbs; /* num of indices to be received */
526887025532SHong Zhang       ierr   = MPI_Irecv(rowlen,nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr);
5269429d309bSHong Zhang     }
5270a6b2eed2SHong Zhang 
5271a6b2eed2SHong Zhang     /* pack the outgoing message */
5272dcca6d9dSJed Brown     ierr = PetscMalloc2(nsends+1,&sstartsj,nrecvs+1,&rstartsj);CHKERRQ(ierr);
52732205254eSKarl Rupp 
52742205254eSKarl Rupp     sstartsj[0] = 0;
52752205254eSKarl Rupp     rstartsj[0] = 0;
5276a6b2eed2SHong Zhang     len         = 0; /* total length of j or a array to be sent */
5277a6b2eed2SHong Zhang     k           = 0;
5278a6b2eed2SHong Zhang     for (i=0; i<nsends; i++) {
5279e42f35eeSHong Zhang       rowlen = (PetscInt*)svalues + sstarts[i]*sbs;
5280e42f35eeSHong Zhang       nrows  = sstarts[i+1]-sstarts[i]; /* num of block rows */
528187025532SHong Zhang       for (j=0; j<nrows; j++) {
5282d0f46423SBarry Smith         row = srow[k] + B->rmap->range[rank]; /* global row idx */
5283e42f35eeSHong Zhang         for (l=0; l<sbs; l++) {
52840298fd71SBarry Smith           ierr = MatGetRow_MPIAIJ(B,row+l,&ncols,NULL,NULL);CHKERRQ(ierr); /* rowlength */
52852205254eSKarl Rupp 
5286e42f35eeSHong Zhang           rowlen[j*sbs+l] = ncols;
52872205254eSKarl Rupp 
5288e42f35eeSHong Zhang           len += ncols;
52890298fd71SBarry Smith           ierr = MatRestoreRow_MPIAIJ(B,row+l,&ncols,NULL,NULL);CHKERRQ(ierr);
5290e42f35eeSHong Zhang         }
5291a6b2eed2SHong Zhang         k++;
5292429d309bSHong Zhang       }
5293e42f35eeSHong Zhang       ierr = MPI_Isend(rowlen,nrows*sbs,MPIU_INT,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr);
52942205254eSKarl Rupp 
5295dea91ad1SHong Zhang       sstartsj[i+1] = len;  /* starting point of (i+1)-th outgoing msg in bufj and bufa */
5296429d309bSHong Zhang     }
529787025532SHong Zhang     /* recvs and sends of i-array are completed */
529887025532SHong Zhang     i = nrecvs;
529987025532SHong Zhang     while (i--) {
5300aa5bb8c0SSatish Balay       ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr);
530187025532SHong Zhang     }
53020c468ba9SBarry Smith     if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);}
5303e42f35eeSHong Zhang 
5304a6b2eed2SHong Zhang     /* allocate buffers for sending j and a arrays */
5305785e854fSJed Brown     ierr = PetscMalloc1((len+1),&bufj);CHKERRQ(ierr);
5306785e854fSJed Brown     ierr = PetscMalloc1((len+1),&bufa);CHKERRQ(ierr);
5307a6b2eed2SHong Zhang 
530887025532SHong Zhang     /* create i-array of B_oth */
5309785e854fSJed Brown     ierr = PetscMalloc1((aBn+2),&b_othi);CHKERRQ(ierr);
53102205254eSKarl Rupp 
531187025532SHong Zhang     b_othi[0] = 0;
5312a6b2eed2SHong Zhang     len       = 0; /* total length of j or a array to be received */
5313a6b2eed2SHong Zhang     k         = 0;
5314a6b2eed2SHong Zhang     for (i=0; i<nrecvs; i++) {
5315fd0ff01cSHong Zhang       rowlen = (PetscInt*)rvalues + rstarts[i]*rbs;
5316e42f35eeSHong Zhang       nrows  = rbs*(rstarts[i+1]-rstarts[i]); /* num of rows to be recieved */
531787025532SHong Zhang       for (j=0; j<nrows; j++) {
531887025532SHong Zhang         b_othi[k+1] = b_othi[k] + rowlen[j];
5319a6b2eed2SHong Zhang         len        += rowlen[j]; k++;
5320a6b2eed2SHong Zhang       }
5321dea91ad1SHong Zhang       rstartsj[i+1] = len; /* starting point of (i+1)-th incoming msg in bufj and bufa */
5322a6b2eed2SHong Zhang     }
5323a6b2eed2SHong Zhang 
532487025532SHong Zhang     /* allocate space for j and a arrrays of B_oth */
5325785e854fSJed Brown     ierr = PetscMalloc1((b_othi[aBn]+1),&b_othj);CHKERRQ(ierr);
5326785e854fSJed Brown     ierr = PetscMalloc1((b_othi[aBn]+1),&b_otha);CHKERRQ(ierr);
5327a6b2eed2SHong Zhang 
532887025532SHong Zhang     /* j-array */
532987025532SHong Zhang     /*---------*/
5330a6b2eed2SHong Zhang     /*  post receives of j-array */
5331a6b2eed2SHong Zhang     for (i=0; i<nrecvs; i++) {
533287025532SHong Zhang       nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */
533387025532SHong Zhang       ierr  = MPI_Irecv(b_othj+rstartsj[i],nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr);
5334a6b2eed2SHong Zhang     }
5335e42f35eeSHong Zhang 
5336e42f35eeSHong Zhang     /* pack the outgoing message j-array */
5337a6b2eed2SHong Zhang     k = 0;
5338a6b2eed2SHong Zhang     for (i=0; i<nsends; i++) {
5339e42f35eeSHong Zhang       nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */
5340a6b2eed2SHong Zhang       bufJ  = bufj+sstartsj[i];
534187025532SHong Zhang       for (j=0; j<nrows; j++) {
5342d0f46423SBarry Smith         row = srow[k++] + B->rmap->range[rank];  /* global row idx */
5343e42f35eeSHong Zhang         for (ll=0; ll<sbs; ll++) {
53440298fd71SBarry Smith           ierr = MatGetRow_MPIAIJ(B,row+ll,&ncols,&cols,NULL);CHKERRQ(ierr);
5345a6b2eed2SHong Zhang           for (l=0; l<ncols; l++) {
5346a6b2eed2SHong Zhang             *bufJ++ = cols[l];
534787025532SHong Zhang           }
53480298fd71SBarry Smith           ierr = MatRestoreRow_MPIAIJ(B,row+ll,&ncols,&cols,NULL);CHKERRQ(ierr);
5349e42f35eeSHong Zhang         }
535087025532SHong Zhang       }
535187025532SHong Zhang       ierr = MPI_Isend(bufj+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_INT,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr);
535287025532SHong Zhang     }
535387025532SHong Zhang 
535487025532SHong Zhang     /* recvs and sends of j-array are completed */
535587025532SHong Zhang     i = nrecvs;
535687025532SHong Zhang     while (i--) {
5357aa5bb8c0SSatish Balay       ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr);
535887025532SHong Zhang     }
53590c468ba9SBarry Smith     if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);}
536087025532SHong Zhang   } else if (scall == MAT_REUSE_MATRIX) {
5361b7f45c76SHong Zhang     sstartsj = *startsj_s;
53621d79065fSBarry Smith     rstartsj = *startsj_r;
536387025532SHong Zhang     bufa     = *bufa_ptr;
536487025532SHong Zhang     b_oth    = (Mat_SeqAIJ*)(*B_oth)->data;
536587025532SHong Zhang     b_otha   = b_oth->a;
5366f23aa3ddSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Matrix P does not posses an object container");
536787025532SHong Zhang 
536887025532SHong Zhang   /* a-array */
536987025532SHong Zhang   /*---------*/
537087025532SHong Zhang   /*  post receives of a-array */
537187025532SHong Zhang   for (i=0; i<nrecvs; i++) {
537287025532SHong Zhang     nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */
537387025532SHong Zhang     ierr  = MPI_Irecv(b_otha+rstartsj[i],nrows,MPIU_SCALAR,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr);
537487025532SHong Zhang   }
5375e42f35eeSHong Zhang 
5376e42f35eeSHong Zhang   /* pack the outgoing message a-array */
537787025532SHong Zhang   k = 0;
537887025532SHong Zhang   for (i=0; i<nsends; i++) {
5379e42f35eeSHong Zhang     nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */
538087025532SHong Zhang     bufA  = bufa+sstartsj[i];
538187025532SHong Zhang     for (j=0; j<nrows; j++) {
5382d0f46423SBarry Smith       row = srow[k++] + B->rmap->range[rank];  /* global row idx */
5383e42f35eeSHong Zhang       for (ll=0; ll<sbs; ll++) {
53840298fd71SBarry Smith         ierr = MatGetRow_MPIAIJ(B,row+ll,&ncols,NULL,&vals);CHKERRQ(ierr);
538587025532SHong Zhang         for (l=0; l<ncols; l++) {
5386a6b2eed2SHong Zhang           *bufA++ = vals[l];
5387a6b2eed2SHong Zhang         }
53880298fd71SBarry Smith         ierr = MatRestoreRow_MPIAIJ(B,row+ll,&ncols,NULL,&vals);CHKERRQ(ierr);
5389e42f35eeSHong Zhang       }
5390a6b2eed2SHong Zhang     }
539187025532SHong Zhang     ierr = MPI_Isend(bufa+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_SCALAR,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr);
5392a6b2eed2SHong Zhang   }
539387025532SHong Zhang   /* recvs and sends of a-array are completed */
539487025532SHong Zhang   i = nrecvs;
539587025532SHong Zhang   while (i--) {
5396aa5bb8c0SSatish Balay     ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr);
539787025532SHong Zhang   }
53980c468ba9SBarry Smith   if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);}
5399d7ee0231SBarry Smith   ierr = PetscFree2(rwaits,swaits);CHKERRQ(ierr);
5400a6b2eed2SHong Zhang 
540187025532SHong Zhang   if (scall == MAT_INITIAL_MATRIX) {
5402a6b2eed2SHong Zhang     /* put together the new matrix */
5403d0f46423SBarry Smith     ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,aBn,B->cmap->N,b_othi,b_othj,b_otha,B_oth);CHKERRQ(ierr);
5404a6b2eed2SHong Zhang 
5405a6b2eed2SHong Zhang     /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
5406a6b2eed2SHong Zhang     /* Since these are PETSc arrays, change flags to free them as necessary. */
540787025532SHong Zhang     b_oth          = (Mat_SeqAIJ*)(*B_oth)->data;
5408e6b907acSBarry Smith     b_oth->free_a  = PETSC_TRUE;
5409e6b907acSBarry Smith     b_oth->free_ij = PETSC_TRUE;
541087025532SHong Zhang     b_oth->nonew   = 0;
5411a6b2eed2SHong Zhang 
5412a6b2eed2SHong Zhang     ierr = PetscFree(bufj);CHKERRQ(ierr);
5413b7f45c76SHong Zhang     if (!startsj_s || !bufa_ptr) {
54141d79065fSBarry Smith       ierr = PetscFree2(sstartsj,rstartsj);CHKERRQ(ierr);
5415dea91ad1SHong Zhang       ierr = PetscFree(bufa_ptr);CHKERRQ(ierr);
5416dea91ad1SHong Zhang     } else {
5417b7f45c76SHong Zhang       *startsj_s = sstartsj;
54181d79065fSBarry Smith       *startsj_r = rstartsj;
541987025532SHong Zhang       *bufa_ptr  = bufa;
542087025532SHong Zhang     }
5421dea91ad1SHong Zhang   }
54224ebed01fSBarry Smith   ierr = PetscLogEventEnd(MAT_GetBrowsOfAocols,A,B,0,0);CHKERRQ(ierr);
5423429d309bSHong Zhang   PetscFunctionReturn(0);
5424429d309bSHong Zhang }
5425ccd8e176SBarry Smith 
542643eb5e2fSMatthew Knepley #undef __FUNCT__
542743eb5e2fSMatthew Knepley #define __FUNCT__ "MatGetCommunicationStructs"
542843eb5e2fSMatthew Knepley /*@C
542943eb5e2fSMatthew Knepley   MatGetCommunicationStructs - Provides access to the communication structures used in matrix-vector multiplication.
543043eb5e2fSMatthew Knepley 
543143eb5e2fSMatthew Knepley   Not Collective
543243eb5e2fSMatthew Knepley 
543343eb5e2fSMatthew Knepley   Input Parameters:
543443eb5e2fSMatthew Knepley . A - The matrix in mpiaij format
543543eb5e2fSMatthew Knepley 
543643eb5e2fSMatthew Knepley   Output Parameter:
543743eb5e2fSMatthew Knepley + lvec - The local vector holding off-process values from the argument to a matrix-vector product
543843eb5e2fSMatthew Knepley . colmap - A map from global column index to local index into lvec
543943eb5e2fSMatthew Knepley - multScatter - A scatter from the argument of a matrix-vector product to lvec
544043eb5e2fSMatthew Knepley 
544143eb5e2fSMatthew Knepley   Level: developer
544243eb5e2fSMatthew Knepley 
544343eb5e2fSMatthew Knepley @*/
544443eb5e2fSMatthew Knepley #if defined(PETSC_USE_CTABLE)
54457087cfbeSBarry Smith PetscErrorCode  MatGetCommunicationStructs(Mat A, Vec *lvec, PetscTable *colmap, VecScatter *multScatter)
544643eb5e2fSMatthew Knepley #else
54477087cfbeSBarry Smith PetscErrorCode  MatGetCommunicationStructs(Mat A, Vec *lvec, PetscInt *colmap[], VecScatter *multScatter)
544843eb5e2fSMatthew Knepley #endif
544943eb5e2fSMatthew Knepley {
545043eb5e2fSMatthew Knepley   Mat_MPIAIJ *a;
545143eb5e2fSMatthew Knepley 
545243eb5e2fSMatthew Knepley   PetscFunctionBegin;
54530700a824SBarry Smith   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
5454e414b56bSJed Brown   PetscValidPointer(lvec, 2);
5455e414b56bSJed Brown   PetscValidPointer(colmap, 3);
5456e414b56bSJed Brown   PetscValidPointer(multScatter, 4);
545743eb5e2fSMatthew Knepley   a = (Mat_MPIAIJ*) A->data;
545843eb5e2fSMatthew Knepley   if (lvec) *lvec = a->lvec;
545943eb5e2fSMatthew Knepley   if (colmap) *colmap = a->colmap;
546043eb5e2fSMatthew Knepley   if (multScatter) *multScatter = a->Mvctx;
546143eb5e2fSMatthew Knepley   PetscFunctionReturn(0);
546243eb5e2fSMatthew Knepley }
546343eb5e2fSMatthew Knepley 
54648cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatConvert_MPIAIJ_MPIAIJCRL(Mat,MatType,MatReuse,Mat*);
54658cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatConvert_MPIAIJ_MPIAIJPERM(Mat,MatType,MatReuse,Mat*);
54668cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatConvert_MPIAIJ_MPISBAIJ(Mat,MatType,MatReuse,Mat*);
5467*5d7652ecSHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
5468*5d7652ecSHong Zhang PETSC_EXTERN PetscErrorCode MatConvert_MPIAIJ_Elemental(Mat,MatType,MatReuse,Mat*);
5469*5d7652ecSHong Zhang #endif
547017667f90SBarry Smith 
5471fc4dec0aSBarry Smith #undef __FUNCT__
5472fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMultNumeric_MPIDense_MPIAIJ"
5473fc4dec0aSBarry Smith /*
5474fc4dec0aSBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
5475fc4dec0aSBarry Smith 
5476fc4dec0aSBarry Smith                n                       p                          p
5477fc4dec0aSBarry Smith         (              )       (              )         (                  )
5478fc4dec0aSBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
5479fc4dec0aSBarry Smith         (              )       (              )         (                  )
5480fc4dec0aSBarry Smith 
5481fc4dec0aSBarry Smith */
5482fc4dec0aSBarry Smith PetscErrorCode MatMatMultNumeric_MPIDense_MPIAIJ(Mat A,Mat B,Mat C)
5483fc4dec0aSBarry Smith {
5484fc4dec0aSBarry Smith   PetscErrorCode ierr;
5485fc4dec0aSBarry Smith   Mat            At,Bt,Ct;
5486fc4dec0aSBarry Smith 
5487fc4dec0aSBarry Smith   PetscFunctionBegin;
5488fc4dec0aSBarry Smith   ierr = MatTranspose(A,MAT_INITIAL_MATRIX,&At);CHKERRQ(ierr);
5489fc4dec0aSBarry Smith   ierr = MatTranspose(B,MAT_INITIAL_MATRIX,&Bt);CHKERRQ(ierr);
5490fc4dec0aSBarry Smith   ierr = MatMatMult(Bt,At,MAT_INITIAL_MATRIX,1.0,&Ct);CHKERRQ(ierr);
54916bf464f9SBarry Smith   ierr = MatDestroy(&At);CHKERRQ(ierr);
54926bf464f9SBarry Smith   ierr = MatDestroy(&Bt);CHKERRQ(ierr);
5493fc4dec0aSBarry Smith   ierr = MatTranspose(Ct,MAT_REUSE_MATRIX,&C);CHKERRQ(ierr);
54946bf464f9SBarry Smith   ierr = MatDestroy(&Ct);CHKERRQ(ierr);
5495fc4dec0aSBarry Smith   PetscFunctionReturn(0);
5496fc4dec0aSBarry Smith }
5497fc4dec0aSBarry Smith 
5498fc4dec0aSBarry Smith #undef __FUNCT__
5499fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMultSymbolic_MPIDense_MPIAIJ"
5500fc4dec0aSBarry Smith PetscErrorCode MatMatMultSymbolic_MPIDense_MPIAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
5501fc4dec0aSBarry Smith {
5502fc4dec0aSBarry Smith   PetscErrorCode ierr;
5503d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
5504fc4dec0aSBarry Smith   Mat            Cmat;
5505fc4dec0aSBarry Smith 
5506fc4dec0aSBarry Smith   PetscFunctionBegin;
5507e32f2f54SBarry 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);
5508ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),&Cmat);CHKERRQ(ierr);
5509fc4dec0aSBarry Smith   ierr = MatSetSizes(Cmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
551033d57670SJed Brown   ierr = MatSetBlockSizesFromMats(Cmat,A,B);CHKERRQ(ierr);
5511fc4dec0aSBarry Smith   ierr = MatSetType(Cmat,MATMPIDENSE);CHKERRQ(ierr);
55120298fd71SBarry Smith   ierr = MatMPIDenseSetPreallocation(Cmat,NULL);CHKERRQ(ierr);
551338556019SBarry Smith   ierr = MatAssemblyBegin(Cmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
551438556019SBarry Smith   ierr = MatAssemblyEnd(Cmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
5515f75ecaa4SHong Zhang 
5516f75ecaa4SHong Zhang   Cmat->ops->matmultnumeric = MatMatMultNumeric_MPIDense_MPIAIJ;
55172205254eSKarl Rupp 
5518fc4dec0aSBarry Smith   *C = Cmat;
5519fc4dec0aSBarry Smith   PetscFunctionReturn(0);
5520fc4dec0aSBarry Smith }
5521fc4dec0aSBarry Smith 
5522fc4dec0aSBarry Smith /* ----------------------------------------------------------------*/
5523fc4dec0aSBarry Smith #undef __FUNCT__
5524fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMult_MPIDense_MPIAIJ"
5525fc4dec0aSBarry Smith PetscErrorCode MatMatMult_MPIDense_MPIAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
5526fc4dec0aSBarry Smith {
5527fc4dec0aSBarry Smith   PetscErrorCode ierr;
5528fc4dec0aSBarry Smith 
5529fc4dec0aSBarry Smith   PetscFunctionBegin;
5530fc4dec0aSBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
55313ff4c91cSHong Zhang     ierr = PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
5532fc4dec0aSBarry Smith     ierr = MatMatMultSymbolic_MPIDense_MPIAIJ(A,B,fill,C);CHKERRQ(ierr);
55333ff4c91cSHong Zhang     ierr = PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
5534fc4dec0aSBarry Smith   }
55353ff4c91cSHong Zhang   ierr = PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
5536fc4dec0aSBarry Smith   ierr = MatMatMultNumeric_MPIDense_MPIAIJ(A,B,*C);CHKERRQ(ierr);
55373ff4c91cSHong Zhang   ierr = PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
5538fc4dec0aSBarry Smith   PetscFunctionReturn(0);
5539fc4dec0aSBarry Smith }
5540fc4dec0aSBarry Smith 
5541611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
55428cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*);
5543611f576cSBarry Smith #endif
55443bf14a46SMatthew Knepley #if defined(PETSC_HAVE_PASTIX)
55458cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_mpiaij_pastix(Mat,MatFactorType,Mat*);
55463bf14a46SMatthew Knepley #endif
5547611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU_DIST)
55488cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_mpiaij_superlu_dist(Mat,MatFactorType,Mat*);
5549611f576cSBarry Smith #endif
555017f1a0eaSHong Zhang #if defined(PETSC_HAVE_CLIQUE)
55518cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_aij_clique(Mat,MatFactorType,Mat*);
555217f1a0eaSHong Zhang #endif
55535c9eb25fSBarry Smith 
5554ccd8e176SBarry Smith /*MC
5555ccd8e176SBarry Smith    MATMPIAIJ - MATMPIAIJ = "mpiaij" - A matrix type to be used for parallel sparse matrices.
5556ccd8e176SBarry Smith 
5557ccd8e176SBarry Smith    Options Database Keys:
5558ccd8e176SBarry Smith . -mat_type mpiaij - sets the matrix type to "mpiaij" during a call to MatSetFromOptions()
5559ccd8e176SBarry Smith 
5560ccd8e176SBarry Smith   Level: beginner
5561ccd8e176SBarry Smith 
556269b1f4b7SBarry Smith .seealso: MatCreateAIJ()
5563ccd8e176SBarry Smith M*/
5564ccd8e176SBarry Smith 
5565ccd8e176SBarry Smith #undef __FUNCT__
5566ccd8e176SBarry Smith #define __FUNCT__ "MatCreate_MPIAIJ"
55678cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatCreate_MPIAIJ(Mat B)
5568ccd8e176SBarry Smith {
5569ccd8e176SBarry Smith   Mat_MPIAIJ     *b;
5570ccd8e176SBarry Smith   PetscErrorCode ierr;
5571ccd8e176SBarry Smith   PetscMPIInt    size;
5572ccd8e176SBarry Smith 
5573ccd8e176SBarry Smith   PetscFunctionBegin;
5574ce94432eSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);CHKERRQ(ierr);
55752205254eSKarl Rupp 
5576b00a9115SJed Brown   ierr          = PetscNewLog(B,&b);CHKERRQ(ierr);
5577ccd8e176SBarry Smith   B->data       = (void*)b;
5578ccd8e176SBarry Smith   ierr          = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
5579ccd8e176SBarry Smith   B->assembled  = PETSC_FALSE;
5580ccd8e176SBarry Smith   B->insertmode = NOT_SET_VALUES;
5581ccd8e176SBarry Smith   b->size       = size;
55822205254eSKarl Rupp 
5583ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)B),&b->rank);CHKERRQ(ierr);
5584ccd8e176SBarry Smith 
5585ccd8e176SBarry Smith   /* build cache for off array entries formed */
5586ce94432eSBarry Smith   ierr = MatStashCreate_Private(PetscObjectComm((PetscObject)B),1,&B->stash);CHKERRQ(ierr);
55872205254eSKarl Rupp 
5588ccd8e176SBarry Smith   b->donotstash  = PETSC_FALSE;
5589ccd8e176SBarry Smith   b->colmap      = 0;
5590ccd8e176SBarry Smith   b->garray      = 0;
5591ccd8e176SBarry Smith   b->roworiented = PETSC_TRUE;
5592ccd8e176SBarry Smith 
5593ccd8e176SBarry Smith   /* stuff used for matrix vector multiply */
55940298fd71SBarry Smith   b->lvec  = NULL;
55950298fd71SBarry Smith   b->Mvctx = NULL;
5596ccd8e176SBarry Smith 
5597ccd8e176SBarry Smith   /* stuff for MatGetRow() */
5598ccd8e176SBarry Smith   b->rowindices   = 0;
5599ccd8e176SBarry Smith   b->rowvalues    = 0;
5600ccd8e176SBarry Smith   b->getrowactive = PETSC_FALSE;
5601ccd8e176SBarry Smith 
5602bbf3fe20SPaul Mullowney   /* flexible pointer used in CUSP/CUSPARSE classes */
56030298fd71SBarry Smith   b->spptr = NULL;
5604f60c3dc2SHong Zhang 
5605611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
5606bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_mumps_C",MatGetFactor_aij_mumps);CHKERRQ(ierr);
5607611f576cSBarry Smith #endif
56083bf14a46SMatthew Knepley #if defined(PETSC_HAVE_PASTIX)
5609bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_pastix_C",MatGetFactor_mpiaij_pastix);CHKERRQ(ierr);
56103bf14a46SMatthew Knepley #endif
5611611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU_DIST)
5612bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_superlu_dist_C",MatGetFactor_mpiaij_superlu_dist);CHKERRQ(ierr);
5613611f576cSBarry Smith #endif
561417f1a0eaSHong Zhang #if defined(PETSC_HAVE_CLIQUE)
5615bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_clique_C",MatGetFactor_aij_clique);CHKERRQ(ierr);
561617f1a0eaSHong Zhang #endif
5617bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_MPIAIJ);CHKERRQ(ierr);
5618bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_MPIAIJ);CHKERRQ(ierr);
5619bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetDiagonalBlock_C",MatGetDiagonalBlock_MPIAIJ);CHKERRQ(ierr);
5620bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_MPIAIJ);CHKERRQ(ierr);
5621bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMPIAIJSetPreallocation_C",MatMPIAIJSetPreallocation_MPIAIJ);CHKERRQ(ierr);
5622bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMPIAIJSetPreallocationCSR_C",MatMPIAIJSetPreallocationCSR_MPIAIJ);CHKERRQ(ierr);
5623bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatDiagonalScaleLocal_C",MatDiagonalScaleLocal_MPIAIJ);CHKERRQ(ierr);
5624bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpiaijperm_C",MatConvert_MPIAIJ_MPIAIJPERM);CHKERRQ(ierr);
5625bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpiaijcrl_C",MatConvert_MPIAIJ_MPIAIJCRL);CHKERRQ(ierr);
5626bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpisbaij_C",MatConvert_MPIAIJ_MPISBAIJ);CHKERRQ(ierr);
5627*5d7652ecSHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
5628*5d7652ecSHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_elemental_C",MatConvert_MPIAIJ_Elemental);CHKERRQ(ierr);
5629*5d7652ecSHong Zhang #endif
5630bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_mpidense_mpiaij_C",MatMatMult_MPIDense_MPIAIJ);CHKERRQ(ierr);
5631bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_mpidense_mpiaij_C",MatMatMultSymbolic_MPIDense_MPIAIJ);CHKERRQ(ierr);
5632bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_mpidense_mpiaij_C",MatMatMultNumeric_MPIDense_MPIAIJ);CHKERRQ(ierr);
563317667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATMPIAIJ);CHKERRQ(ierr);
5634ccd8e176SBarry Smith   PetscFunctionReturn(0);
5635ccd8e176SBarry Smith }
563681824310SBarry Smith 
563703bfb495SBarry Smith #undef __FUNCT__
563803bfb495SBarry Smith #define __FUNCT__ "MatCreateMPIAIJWithSplitArrays"
563958d36128SBarry Smith /*@
564003bfb495SBarry Smith      MatCreateMPIAIJWithSplitArrays - creates a MPI AIJ matrix using arrays that contain the "diagonal"
564103bfb495SBarry Smith          and "off-diagonal" part of the matrix in CSR format.
564203bfb495SBarry Smith 
564303bfb495SBarry Smith    Collective on MPI_Comm
564403bfb495SBarry Smith 
564503bfb495SBarry Smith    Input Parameters:
564603bfb495SBarry Smith +  comm - MPI communicator
564703bfb495SBarry Smith .  m - number of local rows (Cannot be PETSC_DECIDE)
564803bfb495SBarry Smith .  n - This value should be the same as the local size used in creating the
564903bfb495SBarry Smith        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
565003bfb495SBarry Smith        calculated if N is given) For square matrices n is almost always m.
565103bfb495SBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
565203bfb495SBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
565303bfb495SBarry Smith .   i - row indices for "diagonal" portion of matrix
565403bfb495SBarry Smith .   j - column indices
565503bfb495SBarry Smith .   a - matrix values
565603bfb495SBarry Smith .   oi - row indices for "off-diagonal" portion of matrix
565703bfb495SBarry Smith .   oj - column indices
565803bfb495SBarry Smith -   oa - matrix values
565903bfb495SBarry Smith 
566003bfb495SBarry Smith    Output Parameter:
566103bfb495SBarry Smith .   mat - the matrix
566203bfb495SBarry Smith 
566303bfb495SBarry Smith    Level: advanced
566403bfb495SBarry Smith 
566503bfb495SBarry Smith    Notes:
5666292fb18eSBarry Smith        The i, j, and a arrays ARE NOT copied by this routine into the internal format used by PETSc. The user
5667292fb18eSBarry Smith        must free the arrays once the matrix has been destroyed and not before.
566803bfb495SBarry Smith 
566903bfb495SBarry Smith        The i and j indices are 0 based
567003bfb495SBarry Smith 
567169b1f4b7SBarry Smith        See MatCreateAIJ() for the definition of "diagonal" and "off-diagonal" portion of the matrix
567203bfb495SBarry Smith 
56737b55108eSBarry Smith        This sets local rows and cannot be used to set off-processor values.
56747b55108eSBarry Smith 
5675dca341c0SJed Brown        Use of this routine is discouraged because it is inflexible and cumbersome to use. It is extremely rare that a
5676dca341c0SJed Brown        legacy application natively assembles into exactly this split format. The code to do so is nontrivial and does
5677dca341c0SJed Brown        not easily support in-place reassembly. It is recommended to use MatSetValues() (or a variant thereof) because
5678dca341c0SJed Brown        the resulting assembly is easier to implement, will work with any matrix format, and the user does not have to
5679dca341c0SJed Brown        keep track of the underlying array. Use MatSetOption(A,MAT_IGNORE_OFF_PROC_ENTRIES,PETSC_TRUE) to disable all
5680dca341c0SJed Brown        communication if it is known that only local entries will be set.
568103bfb495SBarry Smith 
568203bfb495SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
568303bfb495SBarry Smith 
568403bfb495SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
568569b1f4b7SBarry Smith           MPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithArrays()
568603bfb495SBarry Smith @*/
56872205254eSKarl 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)
568803bfb495SBarry Smith {
568903bfb495SBarry Smith   PetscErrorCode ierr;
569003bfb495SBarry Smith   Mat_MPIAIJ     *maij;
569103bfb495SBarry Smith 
569203bfb495SBarry Smith   PetscFunctionBegin;
5693e32f2f54SBarry Smith   if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative");
5694ea345e14SBarry Smith   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
5695ea345e14SBarry Smith   if (oi[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"oi (row indices) must start with 0");
569603bfb495SBarry Smith   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
569703bfb495SBarry Smith   ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr);
569803bfb495SBarry Smith   ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr);
569903bfb495SBarry Smith   maij = (Mat_MPIAIJ*) (*mat)->data;
57002205254eSKarl Rupp 
57018d7a6e47SBarry Smith   (*mat)->preallocated = PETSC_TRUE;
570203bfb495SBarry Smith 
570326283091SBarry Smith   ierr = PetscLayoutSetUp((*mat)->rmap);CHKERRQ(ierr);
570426283091SBarry Smith   ierr = PetscLayoutSetUp((*mat)->cmap);CHKERRQ(ierr);
570503bfb495SBarry Smith 
570603bfb495SBarry Smith   ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,n,i,j,a,&maij->A);CHKERRQ(ierr);
5707d0f46423SBarry Smith   ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,(*mat)->cmap->N,oi,oj,oa,&maij->B);CHKERRQ(ierr);
570803bfb495SBarry Smith 
57098d7a6e47SBarry Smith   ierr = MatAssemblyBegin(maij->A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
57108d7a6e47SBarry Smith   ierr = MatAssemblyEnd(maij->A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
57118d7a6e47SBarry Smith   ierr = MatAssemblyBegin(maij->B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
57128d7a6e47SBarry Smith   ierr = MatAssemblyEnd(maij->B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
57138d7a6e47SBarry Smith 
571403bfb495SBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
571503bfb495SBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
5716dca341c0SJed Brown   ierr = MatSetOption(*mat,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
571703bfb495SBarry Smith   PetscFunctionReturn(0);
571803bfb495SBarry Smith }
571903bfb495SBarry Smith 
572081824310SBarry Smith /*
572181824310SBarry Smith     Special version for direct calls from Fortran
572281824310SBarry Smith */
5723b45d2f2cSJed Brown #include <petsc-private/fortranimpl.h>
57247087cfbeSBarry Smith 
572581824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
572681824310SBarry Smith #define matsetvaluesmpiaij_ MATSETVALUESMPIAIJ
572781824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
572881824310SBarry Smith #define matsetvaluesmpiaij_ matsetvaluesmpiaij
572981824310SBarry Smith #endif
573081824310SBarry Smith 
573181824310SBarry Smith /* Change these macros so can be used in void function */
573281824310SBarry Smith #undef CHKERRQ
5733e32f2f54SBarry Smith #define CHKERRQ(ierr) CHKERRABORT(PETSC_COMM_WORLD,ierr)
573481824310SBarry Smith #undef SETERRQ2
5735e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
57364994cf47SJed Brown #undef SETERRQ3
57374994cf47SJed Brown #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)
573881824310SBarry Smith #undef SETERRQ
5739e32f2f54SBarry Smith #define SETERRQ(c,ierr,b) CHKERRABORT(c,ierr)
574081824310SBarry Smith 
574181824310SBarry Smith #undef __FUNCT__
574281824310SBarry Smith #define __FUNCT__ "matsetvaluesmpiaij_"
57438cc058d9SJed 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)
574481824310SBarry Smith {
574581824310SBarry Smith   Mat            mat  = *mmat;
574681824310SBarry Smith   PetscInt       m    = *mm, n = *mn;
574781824310SBarry Smith   InsertMode     addv = *maddv;
574881824310SBarry Smith   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
574981824310SBarry Smith   PetscScalar    value;
575081824310SBarry Smith   PetscErrorCode ierr;
5751899cda47SBarry Smith 
57524994cf47SJed Brown   MatCheckPreallocated(mat,1);
57532205254eSKarl Rupp   if (mat->insertmode == NOT_SET_VALUES) mat->insertmode = addv;
57542205254eSKarl Rupp 
575581824310SBarry Smith #if defined(PETSC_USE_DEBUG)
5756f23aa3ddSBarry Smith   else if (mat->insertmode != addv) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
575781824310SBarry Smith #endif
575881824310SBarry Smith   {
5759d0f46423SBarry Smith     PetscInt  i,j,rstart  = mat->rmap->rstart,rend = mat->rmap->rend;
5760d0f46423SBarry Smith     PetscInt  cstart      = mat->cmap->rstart,cend = mat->cmap->rend,row,col;
5761ace3abfcSBarry Smith     PetscBool roworiented = aij->roworiented;
576281824310SBarry Smith 
576381824310SBarry Smith     /* Some Variables required in the macro */
576481824310SBarry Smith     Mat        A                 = aij->A;
576581824310SBarry Smith     Mat_SeqAIJ *a                = (Mat_SeqAIJ*)A->data;
576681824310SBarry Smith     PetscInt   *aimax            = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j;
5767dd6ea824SBarry Smith     MatScalar  *aa               = a->a;
5768ace3abfcSBarry Smith     PetscBool  ignorezeroentries = (((a->ignorezeroentries)&&(addv==ADD_VALUES)) ? PETSC_TRUE : PETSC_FALSE);
576981824310SBarry Smith     Mat        B                 = aij->B;
577081824310SBarry Smith     Mat_SeqAIJ *b                = (Mat_SeqAIJ*)B->data;
5771d0f46423SBarry Smith     PetscInt   *bimax            = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap->n,am = aij->A->rmap->n;
5772dd6ea824SBarry Smith     MatScalar  *ba               = b->a;
577381824310SBarry Smith 
577481824310SBarry Smith     PetscInt  *rp1,*rp2,ii,nrow1,nrow2,_i,rmax1,rmax2,N,low1,high1,low2,high2,t,lastcol1,lastcol2;
577581824310SBarry Smith     PetscInt  nonew = a->nonew;
5776dd6ea824SBarry Smith     MatScalar *ap1,*ap2;
577781824310SBarry Smith 
577881824310SBarry Smith     PetscFunctionBegin;
577981824310SBarry Smith     for (i=0; i<m; i++) {
578081824310SBarry Smith       if (im[i] < 0) continue;
578181824310SBarry Smith #if defined(PETSC_USE_DEBUG)
5782e32f2f54SBarry 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);
578381824310SBarry Smith #endif
578481824310SBarry Smith       if (im[i] >= rstart && im[i] < rend) {
578581824310SBarry Smith         row      = im[i] - rstart;
578681824310SBarry Smith         lastcol1 = -1;
578781824310SBarry Smith         rp1      = aj + ai[row];
578881824310SBarry Smith         ap1      = aa + ai[row];
578981824310SBarry Smith         rmax1    = aimax[row];
579081824310SBarry Smith         nrow1    = ailen[row];
579181824310SBarry Smith         low1     = 0;
579281824310SBarry Smith         high1    = nrow1;
579381824310SBarry Smith         lastcol2 = -1;
579481824310SBarry Smith         rp2      = bj + bi[row];
579581824310SBarry Smith         ap2      = ba + bi[row];
579681824310SBarry Smith         rmax2    = bimax[row];
579781824310SBarry Smith         nrow2    = bilen[row];
579881824310SBarry Smith         low2     = 0;
579981824310SBarry Smith         high2    = nrow2;
580081824310SBarry Smith 
580181824310SBarry Smith         for (j=0; j<n; j++) {
58022205254eSKarl Rupp           if (roworiented) value = v[i*n+j];
58032205254eSKarl Rupp           else value = v[i+j*m];
580481824310SBarry Smith           if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES)) continue;
580581824310SBarry Smith           if (in[j] >= cstart && in[j] < cend) {
580681824310SBarry Smith             col = in[j] - cstart;
580781824310SBarry Smith             MatSetValues_SeqAIJ_A_Private(row,col,value,addv);
580881824310SBarry Smith           } else if (in[j] < 0) continue;
580981824310SBarry Smith #if defined(PETSC_USE_DEBUG)
5810cb9801acSJed 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);
581181824310SBarry Smith #endif
581281824310SBarry Smith           else {
581381824310SBarry Smith             if (mat->was_assembled) {
581481824310SBarry Smith               if (!aij->colmap) {
5815ab9863d7SBarry Smith                 ierr = MatCreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
581681824310SBarry Smith               }
581781824310SBarry Smith #if defined(PETSC_USE_CTABLE)
581881824310SBarry Smith               ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr);
581981824310SBarry Smith               col--;
582081824310SBarry Smith #else
582181824310SBarry Smith               col = aij->colmap[in[j]] - 1;
582281824310SBarry Smith #endif
582381824310SBarry Smith               if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
5824ab9863d7SBarry Smith                 ierr = MatDisAssemble_MPIAIJ(mat);CHKERRQ(ierr);
582581824310SBarry Smith                 col  =  in[j];
582681824310SBarry Smith                 /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
582781824310SBarry Smith                 B     = aij->B;
582881824310SBarry Smith                 b     = (Mat_SeqAIJ*)B->data;
582981824310SBarry Smith                 bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j;
583081824310SBarry Smith                 rp2   = bj + bi[row];
583181824310SBarry Smith                 ap2   = ba + bi[row];
583281824310SBarry Smith                 rmax2 = bimax[row];
583381824310SBarry Smith                 nrow2 = bilen[row];
583481824310SBarry Smith                 low2  = 0;
583581824310SBarry Smith                 high2 = nrow2;
5836d0f46423SBarry Smith                 bm    = aij->B->rmap->n;
583781824310SBarry Smith                 ba    = b->a;
583881824310SBarry Smith               }
583981824310SBarry Smith             } else col = in[j];
584081824310SBarry Smith             MatSetValues_SeqAIJ_B_Private(row,col,value,addv);
584181824310SBarry Smith           }
584281824310SBarry Smith         }
58432205254eSKarl Rupp       } else if (!aij->donotstash) {
584481824310SBarry Smith         if (roworiented) {
5845ace3abfcSBarry Smith           ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr);
584681824310SBarry Smith         } else {
5847ace3abfcSBarry Smith           ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr);
584881824310SBarry Smith         }
584981824310SBarry Smith       }
585081824310SBarry Smith     }
58512205254eSKarl Rupp   }
585281824310SBarry Smith   PetscFunctionReturnVoid();
585381824310SBarry Smith }
585403bfb495SBarry Smith 
5855