xref: /petsc/src/mat/impls/aij/mpi/mpiaij.c (revision c5df96a59d3f7e354f9770fb35f00818af2dc9fc)
18a729477SBarry Smith 
2c6db04a5SJed Brown #include <../src/mat/impls/aij/mpi/mpiaij.h>   /*I "petscmat.h" I*/
3c6db04a5SJed Brown #include <petscblaslapack.h>
48a729477SBarry Smith 
501bebe75SBarry Smith /*MC
601bebe75SBarry Smith    MATAIJ - MATAIJ = "aij" - A matrix type to be used for sparse matrices.
701bebe75SBarry Smith 
801bebe75SBarry Smith    This matrix type is identical to MATSEQAIJ when constructed with a single process communicator,
901bebe75SBarry Smith    and MATMPIAIJ otherwise.  As a result, for single process communicators,
1001bebe75SBarry Smith   MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation is supported
1101bebe75SBarry Smith   for communicators controlling multiple processes.  It is recommended that you call both of
1201bebe75SBarry Smith   the above preallocation routines for simplicity.
1301bebe75SBarry Smith 
1401bebe75SBarry Smith    Options Database Keys:
1501bebe75SBarry Smith . -mat_type aij - sets the matrix type to "aij" during a call to MatSetFromOptions()
1601bebe75SBarry Smith 
179ae82921SPaul Mullowney   Developer Notes: Subclasses include MATAIJCUSP, MATAIJCUSPARSE, MATAIJPERM, MATAIJCRL, and also automatically switches over to use inodes when
1801bebe75SBarry Smith    enough exist.
1901bebe75SBarry Smith 
2001bebe75SBarry Smith   Level: beginner
2101bebe75SBarry Smith 
2269b1f4b7SBarry Smith .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ
2301bebe75SBarry Smith M*/
2401bebe75SBarry Smith 
2501bebe75SBarry Smith /*MC
2601bebe75SBarry Smith    MATAIJCRL - MATAIJCRL = "aijcrl" - A matrix type to be used for sparse matrices.
2701bebe75SBarry Smith 
2801bebe75SBarry Smith    This matrix type is identical to MATSEQAIJCRL when constructed with a single process communicator,
2901bebe75SBarry Smith    and MATMPIAIJCRL otherwise.  As a result, for single process communicators,
3001bebe75SBarry Smith    MatSeqAIJSetPreallocation() is supported, and similarly MatMPIAIJSetPreallocation() is supported
3101bebe75SBarry Smith   for communicators controlling multiple processes.  It is recommended that you call both of
3201bebe75SBarry Smith   the above preallocation routines for simplicity.
3301bebe75SBarry Smith 
3401bebe75SBarry Smith    Options Database Keys:
3501bebe75SBarry Smith . -mat_type aijcrl - sets the matrix type to "aijcrl" during a call to MatSetFromOptions()
3601bebe75SBarry Smith 
3701bebe75SBarry Smith   Level: beginner
3801bebe75SBarry Smith 
3901bebe75SBarry Smith .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL
4001bebe75SBarry Smith M*/
4101bebe75SBarry Smith 
42dd6ea824SBarry Smith #undef __FUNCT__
43f2c98031SJed Brown #define __FUNCT__ "MatFindNonzeroRows_MPIAIJ"
44f2c98031SJed Brown PetscErrorCode MatFindNonzeroRows_MPIAIJ(Mat M,IS *keptrows)
4527d4218bSShri Abhyankar {
4627d4218bSShri Abhyankar   PetscErrorCode  ierr;
4727d4218bSShri Abhyankar   Mat_MPIAIJ      *mat = (Mat_MPIAIJ*)M->data;
4827d4218bSShri Abhyankar   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)mat->A->data;
4927d4218bSShri Abhyankar   Mat_SeqAIJ      *b = (Mat_SeqAIJ*)mat->B->data;
5027d4218bSShri Abhyankar   const PetscInt  *ia,*ib;
5127d4218bSShri Abhyankar   const MatScalar *aa,*bb;
5227d4218bSShri Abhyankar   PetscInt        na,nb,i,j,*rows,cnt=0,n0rows;
5327d4218bSShri Abhyankar   PetscInt        m = M->rmap->n,rstart = M->rmap->rstart;
5427d4218bSShri Abhyankar 
5527d4218bSShri Abhyankar   PetscFunctionBegin;
5627d4218bSShri Abhyankar   *keptrows = 0;
5727d4218bSShri Abhyankar   ia = a->i;
5827d4218bSShri Abhyankar   ib = b->i;
5927d4218bSShri Abhyankar   for (i=0; i<m; i++) {
6027d4218bSShri Abhyankar     na = ia[i+1] - ia[i];
6127d4218bSShri Abhyankar     nb = ib[i+1] - ib[i];
6227d4218bSShri Abhyankar     if (!na && !nb) {
6327d4218bSShri Abhyankar       cnt++;
6427d4218bSShri Abhyankar       goto ok1;
6527d4218bSShri Abhyankar     }
6627d4218bSShri Abhyankar     aa = a->a + ia[i];
6727d4218bSShri Abhyankar     for (j=0; j<na; j++) {
6827d4218bSShri Abhyankar       if (aa[j] != 0.0) goto ok1;
6927d4218bSShri Abhyankar     }
7027d4218bSShri Abhyankar     bb = b->a + ib[i];
7127d4218bSShri Abhyankar     for (j=0; j <nb; j++) {
7227d4218bSShri Abhyankar       if (bb[j] != 0.0) goto ok1;
7327d4218bSShri Abhyankar     }
7427d4218bSShri Abhyankar     cnt++;
7527d4218bSShri Abhyankar     ok1:;
7627d4218bSShri Abhyankar   }
7727d4218bSShri Abhyankar   ierr = MPI_Allreduce(&cnt,&n0rows,1,MPIU_INT,MPI_SUM,((PetscObject)M)->comm);CHKERRQ(ierr);
7827d4218bSShri Abhyankar   if (!n0rows) PetscFunctionReturn(0);
7927d4218bSShri Abhyankar   ierr = PetscMalloc((M->rmap->n-cnt)*sizeof(PetscInt),&rows);CHKERRQ(ierr);
8027d4218bSShri Abhyankar   cnt = 0;
8127d4218bSShri Abhyankar   for (i=0; i<m; i++) {
8227d4218bSShri Abhyankar     na = ia[i+1] - ia[i];
8327d4218bSShri Abhyankar     nb = ib[i+1] - ib[i];
8427d4218bSShri Abhyankar     if (!na && !nb) continue;
8527d4218bSShri Abhyankar     aa = a->a + ia[i];
8627d4218bSShri Abhyankar     for (j=0; j<na;j++) {
8727d4218bSShri Abhyankar       if (aa[j] != 0.0) {
8827d4218bSShri Abhyankar         rows[cnt++] = rstart + i;
8927d4218bSShri Abhyankar         goto ok2;
9027d4218bSShri Abhyankar       }
9127d4218bSShri Abhyankar     }
9227d4218bSShri Abhyankar     bb = b->a + ib[i];
9327d4218bSShri Abhyankar     for (j=0; j<nb; j++) {
9427d4218bSShri Abhyankar       if (bb[j] != 0.0) {
9527d4218bSShri Abhyankar         rows[cnt++] = rstart + i;
9627d4218bSShri Abhyankar         goto ok2;
9727d4218bSShri Abhyankar       }
9827d4218bSShri Abhyankar     }
9927d4218bSShri Abhyankar     ok2:;
10027d4218bSShri Abhyankar   }
10177c8aea5SJed Brown   ierr = ISCreateGeneral(((PetscObject)M)->comm,cnt,rows,PETSC_OWN_POINTER,keptrows);CHKERRQ(ierr);
10227d4218bSShri Abhyankar   PetscFunctionReturn(0);
10327d4218bSShri Abhyankar }
10427d4218bSShri Abhyankar 
10527d4218bSShri Abhyankar #undef __FUNCT__
106f1f41ecbSJed Brown #define __FUNCT__ "MatFindZeroDiagonals_MPIAIJ"
107f1f41ecbSJed Brown PetscErrorCode MatFindZeroDiagonals_MPIAIJ(Mat M,IS *zrows)
108f1f41ecbSJed Brown {
109f1f41ecbSJed Brown   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)M->data;
110f1f41ecbSJed Brown   PetscErrorCode ierr;
111f1f41ecbSJed Brown   PetscInt       i,rstart,nrows,*rows;
112f1f41ecbSJed Brown 
113f1f41ecbSJed Brown   PetscFunctionBegin;
114f1f41ecbSJed Brown   *zrows = PETSC_NULL;
115f1f41ecbSJed Brown   ierr = MatFindZeroDiagonals_SeqAIJ_Private(aij->A,&nrows,&rows);CHKERRQ(ierr);
116f1f41ecbSJed Brown   ierr = MatGetOwnershipRange(M,&rstart,PETSC_NULL);CHKERRQ(ierr);
117f1f41ecbSJed Brown   for (i=0; i<nrows; i++) rows[i] += rstart;
118f1f41ecbSJed Brown   ierr = ISCreateGeneral(((PetscObject)M)->comm,nrows,rows,PETSC_OWN_POINTER,zrows);CHKERRQ(ierr);
119f1f41ecbSJed Brown   PetscFunctionReturn(0);
120f1f41ecbSJed Brown }
121f1f41ecbSJed Brown 
122f1f41ecbSJed Brown #undef __FUNCT__
1230716a85fSBarry Smith #define __FUNCT__ "MatGetColumnNorms_MPIAIJ"
1240716a85fSBarry Smith PetscErrorCode MatGetColumnNorms_MPIAIJ(Mat A,NormType type,PetscReal *norms)
1250716a85fSBarry Smith {
1260716a85fSBarry Smith   PetscErrorCode ierr;
1270716a85fSBarry Smith   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)A->data;
1280716a85fSBarry Smith   PetscInt       i,n,*garray = aij->garray;
1290716a85fSBarry Smith   Mat_SeqAIJ     *a_aij = (Mat_SeqAIJ*) aij->A->data;
1300716a85fSBarry Smith   Mat_SeqAIJ     *b_aij = (Mat_SeqAIJ*) aij->B->data;
1310716a85fSBarry Smith   PetscReal      *work;
1320716a85fSBarry Smith 
1330716a85fSBarry Smith   PetscFunctionBegin;
1340716a85fSBarry Smith   ierr = MatGetSize(A,PETSC_NULL,&n);CHKERRQ(ierr);
1350716a85fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscReal),&work);CHKERRQ(ierr);
1360716a85fSBarry Smith   ierr = PetscMemzero(work,n*sizeof(PetscReal));CHKERRQ(ierr);
1370716a85fSBarry Smith   if (type == NORM_2) {
1380716a85fSBarry Smith     for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) {
1390716a85fSBarry Smith       work[A->cmap->rstart + a_aij->j[i]] += PetscAbsScalar(a_aij->a[i]*a_aij->a[i]);
1400716a85fSBarry Smith     }
1410716a85fSBarry Smith     for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) {
1420716a85fSBarry Smith       work[garray[b_aij->j[i]]] += PetscAbsScalar(b_aij->a[i]*b_aij->a[i]);
1430716a85fSBarry Smith     }
1440716a85fSBarry Smith   } else if (type == NORM_1) {
1450716a85fSBarry Smith     for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) {
1460716a85fSBarry Smith       work[A->cmap->rstart + a_aij->j[i]] += PetscAbsScalar(a_aij->a[i]);
1470716a85fSBarry Smith     }
1480716a85fSBarry Smith     for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) {
1490716a85fSBarry Smith       work[garray[b_aij->j[i]]] += PetscAbsScalar(b_aij->a[i]);
1500716a85fSBarry Smith     }
1510716a85fSBarry Smith   } else if (type == NORM_INFINITY) {
1520716a85fSBarry Smith     for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) {
1530716a85fSBarry Smith       work[A->cmap->rstart + a_aij->j[i]] = PetscMax(PetscAbsScalar(a_aij->a[i]), work[A->cmap->rstart + a_aij->j[i]]);
1540716a85fSBarry Smith     }
1550716a85fSBarry Smith     for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) {
1560716a85fSBarry Smith       work[garray[b_aij->j[i]]] = PetscMax(PetscAbsScalar(b_aij->a[i]),work[garray[b_aij->j[i]]]);
1570716a85fSBarry Smith     }
1580716a85fSBarry Smith 
1590716a85fSBarry Smith   } else SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONG,"Unknown NormType");
1600716a85fSBarry Smith   if (type == NORM_INFINITY) {
1610716a85fSBarry Smith     ierr = MPI_Allreduce(work,norms,n,MPIU_REAL,MPIU_MAX,A->hdr.comm);CHKERRQ(ierr);
1620716a85fSBarry Smith   } else {
1630716a85fSBarry Smith     ierr = MPI_Allreduce(work,norms,n,MPIU_REAL,MPIU_SUM,A->hdr.comm);CHKERRQ(ierr);
1640716a85fSBarry Smith   }
1650716a85fSBarry Smith   ierr = PetscFree(work);CHKERRQ(ierr);
1660716a85fSBarry Smith   if (type == NORM_2) {
1678f1a2a5eSBarry Smith     for (i=0; i<n; i++) norms[i] = PetscSqrtReal(norms[i]);
1680716a85fSBarry Smith   }
1690716a85fSBarry Smith   PetscFunctionReturn(0);
1700716a85fSBarry Smith }
1710716a85fSBarry Smith 
1720716a85fSBarry Smith #undef __FUNCT__
173dd6ea824SBarry Smith #define __FUNCT__ "MatDistribute_MPIAIJ"
174dd6ea824SBarry Smith /*
175dd6ea824SBarry Smith     Distributes a SeqAIJ matrix across a set of processes. Code stolen from
176dd6ea824SBarry Smith     MatLoad_MPIAIJ(). Horrible lack of reuse. Should be a routine for each matrix type.
177dd6ea824SBarry Smith 
178dd6ea824SBarry Smith     Only for square matrices
179dd6ea824SBarry Smith */
180dd6ea824SBarry Smith PetscErrorCode MatDistribute_MPIAIJ(MPI_Comm comm,Mat gmat,PetscInt m,MatReuse reuse,Mat *inmat)
181dd6ea824SBarry Smith {
182dd6ea824SBarry Smith   PetscMPIInt    rank,size;
183efcf75d5SBarry Smith   PetscInt       *rowners,*dlens,*olens,i,rstart,rend,j,jj,nz,*gmataj,cnt,row,*ld,bses[2];
184dd6ea824SBarry Smith   PetscErrorCode ierr;
185dd6ea824SBarry Smith   Mat            mat;
186dd6ea824SBarry Smith   Mat_SeqAIJ     *gmata;
187dd6ea824SBarry Smith   PetscMPIInt    tag;
188dd6ea824SBarry Smith   MPI_Status     status;
189ace3abfcSBarry Smith   PetscBool      aij;
190dd6ea824SBarry Smith   MatScalar      *gmataa,*ao,*ad,*gmataarestore=0;
191dd6ea824SBarry Smith 
192dd6ea824SBarry Smith   PetscFunctionBegin;
193dd6ea824SBarry Smith   CHKMEMQ;
194dd6ea824SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
195dd6ea824SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
196dd6ea824SBarry Smith   if (!rank) {
197251f4c67SDmitry Karpeev     ierr = PetscObjectTypeCompare((PetscObject)gmat,MATSEQAIJ,&aij);CHKERRQ(ierr);
19865e19b50SBarry Smith     if (!aij) SETERRQ1(((PetscObject)gmat)->comm,PETSC_ERR_SUP,"Currently no support for input matrix of type %s\n",((PetscObject)gmat)->type_name);
199dd6ea824SBarry Smith   }
200dd6ea824SBarry Smith   if (reuse == MAT_INITIAL_MATRIX) {
201dd6ea824SBarry Smith     ierr = MatCreate(comm,&mat);CHKERRQ(ierr);
202dd6ea824SBarry Smith     ierr = MatSetSizes(mat,m,m,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
203efcf75d5SBarry Smith     if (!rank) {
204efcf75d5SBarry Smith       bses[0] = gmat->rmap->bs;
205efcf75d5SBarry Smith       bses[1] = gmat->cmap->bs;
206efcf75d5SBarry Smith     }
207efcf75d5SBarry Smith     ierr = MPI_Bcast(bses,2,MPIU_INT,0,comm);CHKERRQ(ierr);
208efcf75d5SBarry Smith     ierr = MatSetBlockSizes(mat,bses[0],bses[1]);CHKERRQ(ierr);
209dd6ea824SBarry Smith     ierr = MatSetType(mat,MATAIJ);CHKERRQ(ierr);
210dd6ea824SBarry Smith     ierr = PetscMalloc((size+1)*sizeof(PetscInt),&rowners);CHKERRQ(ierr);
211dd6ea824SBarry Smith     ierr = PetscMalloc2(m,PetscInt,&dlens,m,PetscInt,&olens);CHKERRQ(ierr);
212dd6ea824SBarry Smith     ierr = MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);CHKERRQ(ierr);
213dd6ea824SBarry Smith     rowners[0] = 0;
214dd6ea824SBarry Smith     for (i=2; i<=size; i++) {
215dd6ea824SBarry Smith       rowners[i] += rowners[i-1];
216dd6ea824SBarry Smith     }
217dd6ea824SBarry Smith     rstart = rowners[rank];
218dd6ea824SBarry Smith     rend   = rowners[rank+1];
219dd6ea824SBarry Smith     ierr   = PetscObjectGetNewTag((PetscObject)mat,&tag);CHKERRQ(ierr);
220dd6ea824SBarry Smith     if (!rank) {
221dd6ea824SBarry Smith       gmata = (Mat_SeqAIJ*) gmat->data;
222dd6ea824SBarry Smith       /* send row lengths to all processors */
223dd6ea824SBarry Smith       for (i=0; i<m; i++) dlens[i] = gmata->ilen[i];
224dd6ea824SBarry Smith       for (i=1; i<size; i++) {
225dd6ea824SBarry Smith         ierr = MPI_Send(gmata->ilen + rowners[i],rowners[i+1]-rowners[i],MPIU_INT,i,tag,comm);CHKERRQ(ierr);
226dd6ea824SBarry Smith       }
227dd6ea824SBarry Smith       /* determine number diagonal and off-diagonal counts */
228dd6ea824SBarry Smith       ierr = PetscMemzero(olens,m*sizeof(PetscInt));CHKERRQ(ierr);
229dd6ea824SBarry Smith       ierr = PetscMalloc(m*sizeof(PetscInt),&ld);CHKERRQ(ierr);
230dd6ea824SBarry Smith       ierr = PetscMemzero(ld,m*sizeof(PetscInt));CHKERRQ(ierr);
231dd6ea824SBarry Smith       jj = 0;
232dd6ea824SBarry Smith       for (i=0; i<m; i++) {
233dd6ea824SBarry Smith         for (j=0; j<dlens[i]; j++) {
234dd6ea824SBarry Smith           if (gmata->j[jj] < rstart) ld[i]++;
235dd6ea824SBarry Smith           if (gmata->j[jj] < rstart || gmata->j[jj] >= rend) olens[i]++;
236dd6ea824SBarry Smith           jj++;
237dd6ea824SBarry Smith         }
238dd6ea824SBarry Smith       }
239dd6ea824SBarry Smith       /* send column indices to other processes */
240dd6ea824SBarry Smith       for (i=1; i<size; i++) {
241dd6ea824SBarry Smith         nz   = gmata->i[rowners[i+1]]-gmata->i[rowners[i]];
242dd6ea824SBarry Smith         ierr = MPI_Send(&nz,1,MPIU_INT,i,tag,comm);CHKERRQ(ierr);
243dd6ea824SBarry Smith         ierr = MPI_Send(gmata->j + gmata->i[rowners[i]],nz,MPIU_INT,i,tag,comm);CHKERRQ(ierr);
244dd6ea824SBarry Smith       }
245dd6ea824SBarry Smith 
246dd6ea824SBarry Smith       /* send numerical values to other processes */
247dd6ea824SBarry Smith       for (i=1; i<size; i++) {
248dd6ea824SBarry Smith         nz   = gmata->i[rowners[i+1]]-gmata->i[rowners[i]];
249dd6ea824SBarry Smith         ierr = MPI_Send(gmata->a + gmata->i[rowners[i]],nz,MPIU_SCALAR,i,tag,comm);CHKERRQ(ierr);
250dd6ea824SBarry Smith       }
251dd6ea824SBarry Smith       gmataa = gmata->a;
252dd6ea824SBarry Smith       gmataj = gmata->j;
253dd6ea824SBarry Smith 
254dd6ea824SBarry Smith     } else {
255dd6ea824SBarry Smith       /* receive row lengths */
256dd6ea824SBarry Smith       ierr = MPI_Recv(dlens,m,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr);
257dd6ea824SBarry Smith       /* receive column indices */
258dd6ea824SBarry Smith       ierr = MPI_Recv(&nz,1,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr);
259dd6ea824SBarry Smith       ierr = PetscMalloc2(nz,PetscScalar,&gmataa,nz,PetscInt,&gmataj);CHKERRQ(ierr);
260dd6ea824SBarry Smith       ierr = MPI_Recv(gmataj,nz,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr);
261dd6ea824SBarry Smith       /* determine number diagonal and off-diagonal counts */
262dd6ea824SBarry Smith       ierr = PetscMemzero(olens,m*sizeof(PetscInt));CHKERRQ(ierr);
263dd6ea824SBarry Smith       ierr = PetscMalloc(m*sizeof(PetscInt),&ld);CHKERRQ(ierr);
264dd6ea824SBarry Smith       ierr = PetscMemzero(ld,m*sizeof(PetscInt));CHKERRQ(ierr);
265dd6ea824SBarry Smith       jj = 0;
266dd6ea824SBarry Smith       for (i=0; i<m; i++) {
267dd6ea824SBarry Smith         for (j=0; j<dlens[i]; j++) {
268dd6ea824SBarry Smith           if (gmataj[jj] < rstart) ld[i]++;
269dd6ea824SBarry Smith           if (gmataj[jj] < rstart || gmataj[jj] >= rend) olens[i]++;
270dd6ea824SBarry Smith           jj++;
271dd6ea824SBarry Smith         }
272dd6ea824SBarry Smith       }
273dd6ea824SBarry Smith       /* receive numerical values */
274dd6ea824SBarry Smith       ierr = PetscMemzero(gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr);
275dd6ea824SBarry Smith       ierr = MPI_Recv(gmataa,nz,MPIU_SCALAR,0,tag,comm,&status);CHKERRQ(ierr);
276dd6ea824SBarry Smith     }
277dd6ea824SBarry Smith     /* set preallocation */
278dd6ea824SBarry Smith     for (i=0; i<m; i++) {
279dd6ea824SBarry Smith       dlens[i] -= olens[i];
280dd6ea824SBarry Smith     }
281dd6ea824SBarry Smith     ierr = MatSeqAIJSetPreallocation(mat,0,dlens);CHKERRQ(ierr);
282dd6ea824SBarry Smith     ierr = MatMPIAIJSetPreallocation(mat,0,dlens,0,olens);CHKERRQ(ierr);
283dd6ea824SBarry Smith 
284dd6ea824SBarry Smith     for (i=0; i<m; i++) {
285dd6ea824SBarry Smith       dlens[i] += olens[i];
286dd6ea824SBarry Smith     }
287dd6ea824SBarry Smith     cnt  = 0;
288dd6ea824SBarry Smith     for (i=0; i<m; i++) {
289dd6ea824SBarry Smith       row  = rstart + i;
290dd6ea824SBarry Smith       ierr = MatSetValues(mat,1,&row,dlens[i],gmataj+cnt,gmataa+cnt,INSERT_VALUES);CHKERRQ(ierr);
291dd6ea824SBarry Smith       cnt += dlens[i];
292dd6ea824SBarry Smith     }
293dd6ea824SBarry Smith     if (rank) {
294dd6ea824SBarry Smith       ierr = PetscFree2(gmataa,gmataj);CHKERRQ(ierr);
295dd6ea824SBarry Smith     }
296dd6ea824SBarry Smith     ierr = PetscFree2(dlens,olens);CHKERRQ(ierr);
297dd6ea824SBarry Smith     ierr = PetscFree(rowners);CHKERRQ(ierr);
298dd6ea824SBarry Smith     ((Mat_MPIAIJ*)(mat->data))->ld = ld;
299dd6ea824SBarry Smith     *inmat = mat;
300dd6ea824SBarry Smith   } else {   /* column indices are already set; only need to move over numerical values from process 0 */
301dd6ea824SBarry Smith     Mat_SeqAIJ *Ad = (Mat_SeqAIJ*)((Mat_MPIAIJ*)((*inmat)->data))->A->data;
302dd6ea824SBarry Smith     Mat_SeqAIJ *Ao = (Mat_SeqAIJ*)((Mat_MPIAIJ*)((*inmat)->data))->B->data;
303dd6ea824SBarry Smith     mat   = *inmat;
304dd6ea824SBarry Smith     ierr  = PetscObjectGetNewTag((PetscObject)mat,&tag);CHKERRQ(ierr);
305dd6ea824SBarry Smith     if (!rank) {
306dd6ea824SBarry Smith       /* send numerical values to other processes */
307dd6ea824SBarry Smith       gmata = (Mat_SeqAIJ*) gmat->data;
308dd6ea824SBarry Smith       ierr   = MatGetOwnershipRanges(mat,(const PetscInt**)&rowners);CHKERRQ(ierr);
309dd6ea824SBarry Smith       gmataa = gmata->a;
310dd6ea824SBarry Smith       for (i=1; i<size; i++) {
311dd6ea824SBarry Smith         nz   = gmata->i[rowners[i+1]]-gmata->i[rowners[i]];
312dd6ea824SBarry Smith         ierr = MPI_Send(gmataa + gmata->i[rowners[i]],nz,MPIU_SCALAR,i,tag,comm);CHKERRQ(ierr);
313dd6ea824SBarry Smith       }
314dd6ea824SBarry Smith       nz   = gmata->i[rowners[1]]-gmata->i[rowners[0]];
315dd6ea824SBarry Smith     } else {
316dd6ea824SBarry Smith       /* receive numerical values from process 0*/
317dd6ea824SBarry Smith       nz   = Ad->nz + Ao->nz;
318dd6ea824SBarry Smith       ierr = PetscMalloc(nz*sizeof(PetscScalar),&gmataa);CHKERRQ(ierr); gmataarestore = gmataa;
319dd6ea824SBarry Smith       ierr = MPI_Recv(gmataa,nz,MPIU_SCALAR,0,tag,comm,&status);CHKERRQ(ierr);
320dd6ea824SBarry Smith     }
321dd6ea824SBarry Smith     /* transfer numerical values into the diagonal A and off diagonal B parts of mat */
322dd6ea824SBarry Smith     ld = ((Mat_MPIAIJ*)(mat->data))->ld;
323dd6ea824SBarry Smith     ad = Ad->a;
324dd6ea824SBarry Smith     ao = Ao->a;
325d0f46423SBarry Smith     if (mat->rmap->n) {
326dd6ea824SBarry Smith       i  = 0;
327dd6ea824SBarry Smith       nz = ld[i];                                   ierr = PetscMemcpy(ao,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); ao += nz; gmataa += nz;
328dd6ea824SBarry Smith       nz = Ad->i[i+1] - Ad->i[i];                   ierr = PetscMemcpy(ad,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); ad += nz; gmataa += nz;
329dd6ea824SBarry Smith     }
330d0f46423SBarry Smith     for (i=1; i<mat->rmap->n; i++) {
331dd6ea824SBarry 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;
332dd6ea824SBarry Smith       nz = Ad->i[i+1] - Ad->i[i];                   ierr = PetscMemcpy(ad,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); ad += nz; gmataa += nz;
333dd6ea824SBarry Smith     }
334dd6ea824SBarry Smith     i--;
335d0f46423SBarry Smith     if (mat->rmap->n) {
33622d28d08SBarry Smith       nz = Ao->i[i+1] - Ao->i[i] - ld[i];           ierr = PetscMemcpy(ao,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr);
337dd6ea824SBarry Smith     }
338dd6ea824SBarry Smith     if (rank) {
339dd6ea824SBarry Smith       ierr = PetscFree(gmataarestore);CHKERRQ(ierr);
340dd6ea824SBarry Smith     }
341dd6ea824SBarry Smith   }
342dd6ea824SBarry Smith   ierr = MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
343dd6ea824SBarry Smith   ierr = MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
344dd6ea824SBarry Smith   CHKMEMQ;
345dd6ea824SBarry Smith   PetscFunctionReturn(0);
346dd6ea824SBarry Smith }
347dd6ea824SBarry Smith 
3480f5bd95cSBarry Smith /*
3490f5bd95cSBarry Smith   Local utility routine that creates a mapping from the global column
3509e25ed09SBarry Smith number to the local number in the off-diagonal part of the local
3510f5bd95cSBarry Smith storage of the matrix.  When PETSC_USE_CTABLE is used this is scalable at
3520f5bd95cSBarry Smith a slightly higher hash table cost; without it it is not scalable (each processor
3530f5bd95cSBarry Smith has an order N integer array but is fast to acess.
3549e25ed09SBarry Smith */
3554a2ae208SSatish Balay #undef __FUNCT__
356ab9863d7SBarry Smith #define __FUNCT__ "MatCreateColmap_MPIAIJ_Private"
357ab9863d7SBarry Smith PetscErrorCode MatCreateColmap_MPIAIJ_Private(Mat mat)
3589e25ed09SBarry Smith {
35944a69424SLois Curfman McInnes   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
3606849ba73SBarry Smith   PetscErrorCode ierr;
361d0f46423SBarry Smith   PetscInt       n = aij->B->cmap->n,i;
362dbb450caSBarry Smith 
3633a40ed3dSBarry Smith   PetscFunctionBegin;
3645e1f6667SBarry Smith   if (!aij->garray) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"MPIAIJ Matrix was assembled but is missing garray");
365aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
366e23dfa41SBarry Smith   ierr = PetscTableCreate(n,mat->cmap->N+1,&aij->colmap);CHKERRQ(ierr);
367b1fc9764SSatish Balay   for (i=0; i<n; i++) {
3683861aac3SJed Brown     ierr = PetscTableAdd(aij->colmap,aij->garray[i]+1,i+1,INSERT_VALUES);CHKERRQ(ierr);
369b1fc9764SSatish Balay   }
370b1fc9764SSatish Balay #else
371d0f46423SBarry Smith   ierr = PetscMalloc((mat->cmap->N+1)*sizeof(PetscInt),&aij->colmap);CHKERRQ(ierr);
372d0f46423SBarry Smith   ierr = PetscLogObjectMemory(mat,mat->cmap->N*sizeof(PetscInt));CHKERRQ(ierr);
373d0f46423SBarry Smith   ierr = PetscMemzero(aij->colmap,mat->cmap->N*sizeof(PetscInt));CHKERRQ(ierr);
374905e6a2fSBarry Smith   for (i=0; i<n; i++) aij->colmap[aij->garray[i]] = i+1;
375b1fc9764SSatish Balay #endif
3763a40ed3dSBarry Smith   PetscFunctionReturn(0);
3779e25ed09SBarry Smith }
3789e25ed09SBarry Smith 
37930770e4dSSatish Balay #define MatSetValues_SeqAIJ_A_Private(row,col,value,addv) \
3800520107fSSatish Balay { \
381db4deed7SKarl Rupp     if (col <= lastcol1)  low1 = 0;     \
382db4deed7SKarl Rupp     else                 high1 = nrow1; \
383fd3458f5SBarry Smith     lastcol1 = col;\
384fd3458f5SBarry Smith     while (high1-low1 > 5) { \
385fd3458f5SBarry Smith       t = (low1+high1)/2; \
386fd3458f5SBarry Smith       if (rp1[t] > col) high1 = t; \
387fd3458f5SBarry Smith       else              low1  = t; \
388ba4e3ef2SSatish Balay     } \
389fd3458f5SBarry Smith       for (_i=low1; _i<high1; _i++) { \
390fd3458f5SBarry Smith         if (rp1[_i] > col) break; \
391fd3458f5SBarry Smith         if (rp1[_i] == col) { \
392fd3458f5SBarry Smith           if (addv == ADD_VALUES) ap1[_i] += value;   \
393fd3458f5SBarry Smith           else                    ap1[_i] = value; \
39430770e4dSSatish Balay           goto a_noinsert; \
3950520107fSSatish Balay         } \
3960520107fSSatish Balay       }  \
397e44c0bd4SBarry Smith       if (value == 0.0 && ignorezeroentries) {low1 = 0; high1 = nrow1;goto a_noinsert;} \
398e44c0bd4SBarry Smith       if (nonew == 1) {low1 = 0; high1 = nrow1; goto a_noinsert;}                \
399e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \
400fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,am,1,nrow1,row,col,rmax1,aa,ai,aj,rp1,ap1,aimax,nonew,MatScalar); \
401669a8dbcSSatish Balay       N = nrow1++ - 1; a->nz++; high1++; \
4020520107fSSatish Balay       /* shift up all the later entries in this row */ \
4030520107fSSatish Balay       for (ii=N; ii>=_i; ii--) { \
404fd3458f5SBarry Smith         rp1[ii+1] = rp1[ii]; \
405fd3458f5SBarry Smith         ap1[ii+1] = ap1[ii]; \
4060520107fSSatish Balay       } \
407fd3458f5SBarry Smith       rp1[_i] = col;  \
408fd3458f5SBarry Smith       ap1[_i] = value;  \
40930770e4dSSatish Balay       a_noinsert: ; \
410fd3458f5SBarry Smith       ailen[row] = nrow1; \
4110520107fSSatish Balay }
4120a198c4cSBarry Smith 
413085a36d4SBarry Smith 
41430770e4dSSatish Balay #define MatSetValues_SeqAIJ_B_Private(row,col,value,addv) \
41530770e4dSSatish Balay { \
416db4deed7SKarl Rupp     if (col <= lastcol2) low2 = 0;                        \
417db4deed7SKarl Rupp     else high2 = nrow2;                                   \
418fd3458f5SBarry Smith     lastcol2 = col;                                       \
419fd3458f5SBarry Smith     while (high2-low2 > 5) {                              \
420fd3458f5SBarry Smith       t = (low2+high2)/2;                                 \
421fd3458f5SBarry Smith       if (rp2[t] > col) high2 = t;                        \
422fd3458f5SBarry Smith       else             low2  = t;                         \
423ba4e3ef2SSatish Balay     }                                                     \
424fd3458f5SBarry Smith     for (_i=low2; _i<high2; _i++) {                       \
425fd3458f5SBarry Smith       if (rp2[_i] > col) break;                           \
426fd3458f5SBarry Smith       if (rp2[_i] == col) {                               \
427fd3458f5SBarry Smith         if (addv == ADD_VALUES) ap2[_i] += value;         \
428fd3458f5SBarry Smith         else                    ap2[_i] = value;          \
42930770e4dSSatish Balay         goto b_noinsert;                                  \
43030770e4dSSatish Balay       }                                                   \
43130770e4dSSatish Balay     }                                                     \
432e44c0bd4SBarry Smith     if (value == 0.0 && ignorezeroentries) {low2 = 0; high2 = nrow2; goto b_noinsert;} \
433e44c0bd4SBarry Smith     if (nonew == 1) {low2 = 0; high2 = nrow2; goto b_noinsert;}                        \
434e32f2f54SBarry Smith     if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \
435fef13f97SBarry Smith     MatSeqXAIJReallocateAIJ(B,bm,1,nrow2,row,col,rmax2,ba,bi,bj,rp2,ap2,bimax,nonew,MatScalar); \
436669a8dbcSSatish Balay     N = nrow2++ - 1; b->nz++; high2++;                    \
43730770e4dSSatish Balay     /* shift up all the later entries in this row */      \
43830770e4dSSatish Balay     for (ii=N; ii>=_i; ii--) {                            \
439fd3458f5SBarry Smith       rp2[ii+1] = rp2[ii];                                \
440fd3458f5SBarry Smith       ap2[ii+1] = ap2[ii];                                \
44130770e4dSSatish Balay     }                                                     \
442fd3458f5SBarry Smith     rp2[_i] = col;                                        \
443fd3458f5SBarry Smith     ap2[_i] = value;                                      \
44430770e4dSSatish Balay     b_noinsert: ;                                         \
445fd3458f5SBarry Smith     bilen[row] = nrow2;                                   \
44630770e4dSSatish Balay }
44730770e4dSSatish Balay 
4484a2ae208SSatish Balay #undef __FUNCT__
4492fd7e33dSBarry Smith #define __FUNCT__ "MatSetValuesRow_MPIAIJ"
4502fd7e33dSBarry Smith PetscErrorCode MatSetValuesRow_MPIAIJ(Mat A,PetscInt row,const PetscScalar v[])
4512fd7e33dSBarry Smith {
4522fd7e33dSBarry Smith   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)A->data;
4532fd7e33dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)mat->A->data,*b = (Mat_SeqAIJ*)mat->B->data;
4542fd7e33dSBarry Smith   PetscErrorCode ierr;
4552fd7e33dSBarry Smith   PetscInt       l,*garray = mat->garray,diag;
4562fd7e33dSBarry Smith 
4572fd7e33dSBarry Smith   PetscFunctionBegin;
4582fd7e33dSBarry Smith   /* code only works for square matrices A */
4592fd7e33dSBarry Smith 
4602fd7e33dSBarry Smith   /* find size of row to the left of the diagonal part */
4612fd7e33dSBarry Smith   ierr = MatGetOwnershipRange(A,&diag,0);CHKERRQ(ierr);
4622fd7e33dSBarry Smith   row  = row - diag;
4632fd7e33dSBarry Smith   for (l=0; l<b->i[row+1]-b->i[row]; l++) {
4642fd7e33dSBarry Smith     if (garray[b->j[b->i[row]+l]] > diag) break;
4652fd7e33dSBarry Smith   }
4662fd7e33dSBarry Smith   ierr = PetscMemcpy(b->a+b->i[row],v,l*sizeof(PetscScalar));CHKERRQ(ierr);
4672fd7e33dSBarry Smith 
4682fd7e33dSBarry Smith   /* diagonal part */
4692fd7e33dSBarry Smith   ierr = PetscMemcpy(a->a+a->i[row],v+l,(a->i[row+1]-a->i[row])*sizeof(PetscScalar));CHKERRQ(ierr);
4702fd7e33dSBarry Smith 
4712fd7e33dSBarry Smith   /* right of diagonal part */
4722fd7e33dSBarry 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);
4732fd7e33dSBarry Smith   PetscFunctionReturn(0);
4742fd7e33dSBarry Smith }
4752fd7e33dSBarry Smith 
4762fd7e33dSBarry Smith #undef __FUNCT__
4774a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_MPIAIJ"
478b1d57f15SBarry Smith PetscErrorCode MatSetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
4798a729477SBarry Smith {
48044a69424SLois Curfman McInnes   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
48187828ca2SBarry Smith   PetscScalar    value;
482dfbe8321SBarry Smith   PetscErrorCode ierr;
483d0f46423SBarry Smith   PetscInt       i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend;
484d0f46423SBarry Smith   PetscInt       cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col;
485ace3abfcSBarry Smith   PetscBool      roworiented = aij->roworiented;
4868a729477SBarry Smith 
4870520107fSSatish Balay   /* Some Variables required in the macro */
4884ee7247eSSatish Balay   Mat            A = aij->A;
4894ee7247eSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
49057809a77SBarry Smith   PetscInt       *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j;
491a77337e4SBarry Smith   MatScalar      *aa = a->a;
492ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
49330770e4dSSatish Balay   Mat            B = aij->B;
49430770e4dSSatish Balay   Mat_SeqAIJ     *b = (Mat_SeqAIJ*)B->data;
495d0f46423SBarry Smith   PetscInt       *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap->n,am = aij->A->rmap->n;
496a77337e4SBarry Smith   MatScalar      *ba = b->a;
49730770e4dSSatish Balay 
498fd3458f5SBarry Smith   PetscInt       *rp1,*rp2,ii,nrow1,nrow2,_i,rmax1,rmax2,N,low1,high1,low2,high2,t,lastcol1,lastcol2;
4998d76821aSHong Zhang   PetscInt       nonew;
500a77337e4SBarry Smith   MatScalar      *ap1,*ap2;
5014ee7247eSSatish Balay 
5023a40ed3dSBarry Smith   PetscFunctionBegin;
50371fd2e92SBarry Smith   if (v) PetscValidScalarPointer(v,6);
5048a729477SBarry Smith   for (i=0; i<m; i++) {
5055ef9f2a5SBarry Smith     if (im[i] < 0) continue;
5062515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
507e32f2f54SBarry 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);
5080a198c4cSBarry Smith #endif
5094b0e389bSBarry Smith     if (im[i] >= rstart && im[i] < rend) {
5104b0e389bSBarry Smith       row      = im[i] - rstart;
511fd3458f5SBarry Smith       lastcol1 = -1;
512fd3458f5SBarry Smith       rp1      = aj + ai[row];
513fd3458f5SBarry Smith       ap1      = aa + ai[row];
514fd3458f5SBarry Smith       rmax1    = aimax[row];
515fd3458f5SBarry Smith       nrow1    = ailen[row];
516fd3458f5SBarry Smith       low1     = 0;
517fd3458f5SBarry Smith       high1    = nrow1;
518fd3458f5SBarry Smith       lastcol2 = -1;
519fd3458f5SBarry Smith       rp2      = bj + bi[row];
520d498b1e9SBarry Smith       ap2      = ba + bi[row];
521fd3458f5SBarry Smith       rmax2    = bimax[row];
522d498b1e9SBarry Smith       nrow2    = bilen[row];
523fd3458f5SBarry Smith       low2     = 0;
524fd3458f5SBarry Smith       high2    = nrow2;
525fd3458f5SBarry Smith 
5261eb62cbbSBarry Smith       for (j=0; j<n; j++) {
527db4deed7SKarl Rupp         if (v) {
528db4deed7SKarl Rupp           if (roworiented) value = v[i*n+j];
529db4deed7SKarl Rupp           else             value = v[i+j*m];
530db4deed7SKarl Rupp         } else value = 0.0;
531abc0a331SBarry Smith         if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES)) continue;
532fd3458f5SBarry Smith         if (in[j] >= cstart && in[j] < cend) {
533fd3458f5SBarry Smith           col = in[j] - cstart;
5348d76821aSHong Zhang           nonew = a->nonew;
53530770e4dSSatish Balay           MatSetValues_SeqAIJ_A_Private(row,col,value,addv);
536273d9f13SBarry Smith         } else if (in[j] < 0) continue;
5372515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
538cb9801acSJed 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);
5390a198c4cSBarry Smith #endif
5401eb62cbbSBarry Smith         else {
541227d817aSBarry Smith           if (mat->was_assembled) {
542905e6a2fSBarry Smith             if (!aij->colmap) {
543ab9863d7SBarry Smith               ierr = MatCreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
544905e6a2fSBarry Smith             }
545aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
5460f5bd95cSBarry Smith             ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr);
547fa46199cSSatish Balay             col--;
548b1fc9764SSatish Balay #else
549905e6a2fSBarry Smith             col = aij->colmap[in[j]] - 1;
550b1fc9764SSatish Balay #endif
5510e9bae81SBarry Smith             if (col < 0 && !((Mat_SeqAIJ*)(aij->B->data))->nonew) {
552ab9863d7SBarry Smith               ierr = MatDisAssemble_MPIAIJ(mat);CHKERRQ(ierr);
5534b0e389bSBarry Smith               col =  in[j];
5549bf004c3SSatish Balay               /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
555f9508a3cSSatish Balay               B = aij->B;
556f9508a3cSSatish Balay               b = (Mat_SeqAIJ*)B->data;
557e44c0bd4SBarry Smith               bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j; ba = b->a;
558d498b1e9SBarry Smith               rp2      = bj + bi[row];
559d498b1e9SBarry Smith               ap2      = ba + bi[row];
560d498b1e9SBarry Smith               rmax2    = bimax[row];
561d498b1e9SBarry Smith               nrow2    = bilen[row];
562d498b1e9SBarry Smith               low2     = 0;
563d498b1e9SBarry Smith               high2    = nrow2;
564d0f46423SBarry Smith               bm       = aij->B->rmap->n;
565f9508a3cSSatish Balay               ba = b->a;
5660e9bae81SBarry 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]);
567c48de900SBarry Smith           } else col = in[j];
5688d76821aSHong Zhang           nonew = b->nonew;
56930770e4dSSatish Balay           MatSetValues_SeqAIJ_B_Private(row,col,value,addv);
5701eb62cbbSBarry Smith         }
5711eb62cbbSBarry Smith       }
5725ef9f2a5SBarry Smith     } else {
5734cb17eb5SBarry 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]);
57490f02eecSBarry Smith       if (!aij->donotstash) {
5755080c13bSMatthew G Knepley         mat->assembled = PETSC_FALSE;
576d36fbae8SSatish Balay         if (roworiented) {
577ace3abfcSBarry Smith           ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr);
578d36fbae8SSatish Balay         } else {
579ace3abfcSBarry Smith           ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr);
5804b0e389bSBarry Smith         }
5811eb62cbbSBarry Smith       }
5828a729477SBarry Smith     }
58390f02eecSBarry Smith   }
5843a40ed3dSBarry Smith   PetscFunctionReturn(0);
5858a729477SBarry Smith }
5868a729477SBarry Smith 
5874a2ae208SSatish Balay #undef __FUNCT__
5884a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_MPIAIJ"
589b1d57f15SBarry Smith PetscErrorCode MatGetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[])
590b49de8d1SLois Curfman McInnes {
591b49de8d1SLois Curfman McInnes   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
592dfbe8321SBarry Smith   PetscErrorCode ierr;
593d0f46423SBarry Smith   PetscInt       i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend;
594d0f46423SBarry Smith   PetscInt       cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col;
595b49de8d1SLois Curfman McInnes 
5963a40ed3dSBarry Smith   PetscFunctionBegin;
597b49de8d1SLois Curfman McInnes   for (i=0; i<m; i++) {
598e32f2f54SBarry Smith     if (idxm[i] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",idxm[i]);*/
599e32f2f54SBarry 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);
600b49de8d1SLois Curfman McInnes     if (idxm[i] >= rstart && idxm[i] < rend) {
601b49de8d1SLois Curfman McInnes       row = idxm[i] - rstart;
602b49de8d1SLois Curfman McInnes       for (j=0; j<n; j++) {
603e32f2f54SBarry Smith         if (idxn[j] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",idxn[j]); */
604e32f2f54SBarry 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);
605b49de8d1SLois Curfman McInnes         if (idxn[j] >= cstart && idxn[j] < cend) {
606b49de8d1SLois Curfman McInnes           col = idxn[j] - cstart;
607b49de8d1SLois Curfman McInnes           ierr = MatGetValues(aij->A,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
608fa852ad4SSatish Balay         } else {
609905e6a2fSBarry Smith           if (!aij->colmap) {
610ab9863d7SBarry Smith             ierr = MatCreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
611905e6a2fSBarry Smith           }
612aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
6130f5bd95cSBarry Smith           ierr = PetscTableFind(aij->colmap,idxn[j]+1,&col);CHKERRQ(ierr);
614fa46199cSSatish Balay           col --;
615b1fc9764SSatish Balay #else
616905e6a2fSBarry Smith           col = aij->colmap[idxn[j]] - 1;
617b1fc9764SSatish Balay #endif
618e60e1c95SSatish Balay           if ((col < 0) || (aij->garray[col] != idxn[j])) *(v+i*n+j) = 0.0;
619d9d09a02SSatish Balay           else {
620b49de8d1SLois Curfman McInnes             ierr = MatGetValues(aij->B,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
621b49de8d1SLois Curfman McInnes           }
622b49de8d1SLois Curfman McInnes         }
623b49de8d1SLois Curfman McInnes       }
624f23aa3ddSBarry Smith     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local values currently supported");
625b49de8d1SLois Curfman McInnes   }
6263a40ed3dSBarry Smith   PetscFunctionReturn(0);
627b49de8d1SLois Curfman McInnes }
628bc5ccf88SSatish Balay 
629bd0c2dcbSBarry Smith extern PetscErrorCode MatMultDiagonalBlock_MPIAIJ(Mat,Vec,Vec);
630bd0c2dcbSBarry Smith 
6314a2ae208SSatish Balay #undef __FUNCT__
6324a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyBegin_MPIAIJ"
633dfbe8321SBarry Smith PetscErrorCode MatAssemblyBegin_MPIAIJ(Mat mat,MatAssemblyType mode)
634bc5ccf88SSatish Balay {
635bc5ccf88SSatish Balay   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
636dfbe8321SBarry Smith   PetscErrorCode ierr;
637b1d57f15SBarry Smith   PetscInt       nstash,reallocs;
638bc5ccf88SSatish Balay   InsertMode     addv;
639bc5ccf88SSatish Balay 
640bc5ccf88SSatish Balay   PetscFunctionBegin;
6414cb17eb5SBarry Smith   if (aij->donotstash || mat->nooffprocentries) {
642bc5ccf88SSatish Balay     PetscFunctionReturn(0);
643bc5ccf88SSatish Balay   }
644bc5ccf88SSatish Balay 
645bc5ccf88SSatish Balay   /* make sure all processors are either in INSERTMODE or ADDMODE */
646c3aae356SJed Brown   ierr = MPI_Allreduce((PetscEnum*)&mat->insertmode,(PetscEnum*)&addv,1,MPIU_ENUM,MPI_BOR,((PetscObject)mat)->comm);CHKERRQ(ierr);
647e7e72b3dSBarry Smith   if (addv == (ADD_VALUES|INSERT_VALUES)) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Some processors inserted others added");
648bc5ccf88SSatish Balay   mat->insertmode = addv; /* in case this processor had no cache */
649bc5ccf88SSatish Balay 
650d0f46423SBarry Smith   ierr = MatStashScatterBegin_Private(mat,&mat->stash,mat->rmap->range);CHKERRQ(ierr);
6518798bf22SSatish Balay   ierr = MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);CHKERRQ(ierr);
652ae15b995SBarry Smith   ierr = PetscInfo2(aij->A,"Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);CHKERRQ(ierr);
653bc5ccf88SSatish Balay   PetscFunctionReturn(0);
654bc5ccf88SSatish Balay }
655bc5ccf88SSatish Balay 
6564a2ae208SSatish Balay #undef __FUNCT__
6574a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_MPIAIJ"
658dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_MPIAIJ(Mat mat,MatAssemblyType mode)
659bc5ccf88SSatish Balay {
660bc5ccf88SSatish Balay   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
66191c97fd4SSatish Balay   Mat_SeqAIJ     *a=(Mat_SeqAIJ *)aij->A->data;
6626849ba73SBarry Smith   PetscErrorCode ierr;
663b1d57f15SBarry Smith   PetscMPIInt    n;
664b1d57f15SBarry Smith   PetscInt       i,j,rstart,ncols,flg;
665e44c0bd4SBarry Smith   PetscInt       *row,*col;
666ace3abfcSBarry Smith   PetscBool      other_disassembled;
66787828ca2SBarry Smith   PetscScalar    *val;
668bc5ccf88SSatish Balay   InsertMode     addv = mat->insertmode;
669bc5ccf88SSatish Balay 
67091c97fd4SSatish Balay   /* do not use 'b = (Mat_SeqAIJ *)aij->B->data' as B can be reset in disassembly */
6716e111a19SKarl Rupp 
672bc5ccf88SSatish Balay   PetscFunctionBegin;
6734cb17eb5SBarry Smith   if (!aij->donotstash && !mat->nooffprocentries) {
674a2d1c673SSatish Balay     while (1) {
6758798bf22SSatish Balay       ierr = MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);CHKERRQ(ierr);
676a2d1c673SSatish Balay       if (!flg) break;
677a2d1c673SSatish Balay 
678bc5ccf88SSatish Balay       for (i=0; i<n;) {
679bc5ccf88SSatish Balay         /* Now identify the consecutive vals belonging to the same row */
680bc5ccf88SSatish Balay         for (j=i,rstart=row[j]; j<n; j++) { if (row[j] != rstart) break; }
681bc5ccf88SSatish Balay         if (j < n) ncols = j-i;
682bc5ccf88SSatish Balay         else       ncols = n-i;
683bc5ccf88SSatish Balay         /* Now assemble all these values with a single function call */
684bc5ccf88SSatish Balay         ierr = MatSetValues_MPIAIJ(mat,1,row+i,ncols,col+i,val+i,addv);CHKERRQ(ierr);
685bc5ccf88SSatish Balay         i = j;
686bc5ccf88SSatish Balay       }
687bc5ccf88SSatish Balay     }
6888798bf22SSatish Balay     ierr = MatStashScatterEnd_Private(&mat->stash);CHKERRQ(ierr);
689bc5ccf88SSatish Balay   }
690bc5ccf88SSatish Balay   ierr = MatAssemblyBegin(aij->A,mode);CHKERRQ(ierr);
691bc5ccf88SSatish Balay   ierr = MatAssemblyEnd(aij->A,mode);CHKERRQ(ierr);
692bc5ccf88SSatish Balay 
693bc5ccf88SSatish Balay   /* determine if any processor has disassembled, if so we must
694bc5ccf88SSatish Balay      also disassemble ourselfs, in order that we may reassemble. */
695bc5ccf88SSatish Balay   /*
696bc5ccf88SSatish Balay      if nonzero structure of submatrix B cannot change then we know that
697bc5ccf88SSatish Balay      no processor disassembled thus we can skip this stuff
698bc5ccf88SSatish Balay   */
699bc5ccf88SSatish Balay   if (!((Mat_SeqAIJ*)aij->B->data)->nonew)  {
700c3aae356SJed Brown     ierr = MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPIU_BOOL,MPI_PROD,((PetscObject)mat)->comm);CHKERRQ(ierr);
701bc5ccf88SSatish Balay     if (mat->was_assembled && !other_disassembled) {
702ab9863d7SBarry Smith       ierr = MatDisAssemble_MPIAIJ(mat);CHKERRQ(ierr);
703ad59fb31SSatish Balay     }
704ad59fb31SSatish Balay   }
705bc5ccf88SSatish Balay   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
706bc5ccf88SSatish Balay     ierr = MatSetUpMultiply_MPIAIJ(mat);CHKERRQ(ierr);
707bc5ccf88SSatish Balay   }
7084e0d8c25SBarry Smith   ierr = MatSetOption(aij->B,MAT_USE_INODES,PETSC_FALSE);CHKERRQ(ierr);
7094e35b6f3SSatish Balay   ierr = MatSetOption(aij->B,MAT_CHECK_COMPRESSED_ROW,PETSC_FALSE);CHKERRQ(ierr);
710bc5ccf88SSatish Balay   ierr = MatAssemblyBegin(aij->B,mode);CHKERRQ(ierr);
711bc5ccf88SSatish Balay   ierr = MatAssemblyEnd(aij->B,mode);CHKERRQ(ierr);
712bc5ccf88SSatish Balay 
7131d79065fSBarry Smith   ierr = PetscFree2(aij->rowvalues,aij->rowindices);CHKERRQ(ierr);
714606d414cSSatish Balay   aij->rowvalues = 0;
715a30b2313SHong Zhang 
716a30b2313SHong Zhang   /* used by MatAXPY() */
71791c97fd4SSatish Balay   a->xtoy = 0; ((Mat_SeqAIJ *)aij->B->data)->xtoy = 0;  /* b->xtoy = 0 */
71891c97fd4SSatish Balay   a->XtoY = 0; ((Mat_SeqAIJ *)aij->B->data)->XtoY = 0;  /* b->XtoY = 0 */
719a30b2313SHong Zhang 
7206bf464f9SBarry Smith   ierr = VecDestroy(&aij->diag);CHKERRQ(ierr);
721bd0c2dcbSBarry Smith   if (a->inode.size) mat->ops->multdiagonalblock = MatMultDiagonalBlock_MPIAIJ;
722bc5ccf88SSatish Balay   PetscFunctionReturn(0);
723bc5ccf88SSatish Balay }
724bc5ccf88SSatish Balay 
7254a2ae208SSatish Balay #undef __FUNCT__
7264a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_MPIAIJ"
727dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_MPIAIJ(Mat A)
7281eb62cbbSBarry Smith {
72944a69424SLois Curfman McInnes   Mat_MPIAIJ     *l = (Mat_MPIAIJ*)A->data;
730dfbe8321SBarry Smith   PetscErrorCode ierr;
7313a40ed3dSBarry Smith 
7323a40ed3dSBarry Smith   PetscFunctionBegin;
73378b31e54SBarry Smith   ierr = MatZeroEntries(l->A);CHKERRQ(ierr);
73478b31e54SBarry Smith   ierr = MatZeroEntries(l->B);CHKERRQ(ierr);
7353a40ed3dSBarry Smith   PetscFunctionReturn(0);
7361eb62cbbSBarry Smith }
7371eb62cbbSBarry Smith 
7384a2ae208SSatish Balay #undef __FUNCT__
7394a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_MPIAIJ"
7402b40b63fSBarry Smith PetscErrorCode MatZeroRows_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
7411eb62cbbSBarry Smith {
74244a69424SLois Curfman McInnes   Mat_MPIAIJ        *l = (Mat_MPIAIJ*)A->data;
7436849ba73SBarry Smith   PetscErrorCode    ierr;
7447adad957SLisandro Dalcin   PetscMPIInt       size = l->size,imdex,n,rank = l->rank,tag = ((PetscObject)A)->tag,lastidx = -1;
745d0f46423SBarry Smith   PetscInt          i,*owners = A->rmap->range;
746b1d57f15SBarry Smith   PetscInt          *nprocs,j,idx,nsends,row;
747b1d57f15SBarry Smith   PetscInt          nmax,*svalues,*starts,*owner,nrecvs;
748b1d57f15SBarry Smith   PetscInt          *rvalues,count,base,slen,*source;
749d0f46423SBarry Smith   PetscInt          *lens,*lrows,*values,rstart=A->rmap->rstart;
7507adad957SLisandro Dalcin   MPI_Comm          comm = ((PetscObject)A)->comm;
7511eb62cbbSBarry Smith   MPI_Request       *send_waits,*recv_waits;
7521eb62cbbSBarry Smith   MPI_Status        recv_status,*send_status;
75397b48c8fSBarry Smith   const PetscScalar *xx;
75497b48c8fSBarry Smith   PetscScalar       *bb;
7556543fbbaSBarry Smith #if defined(PETSC_DEBUG)
756ace3abfcSBarry Smith   PetscBool      found = PETSC_FALSE;
7576543fbbaSBarry Smith #endif
7581eb62cbbSBarry Smith 
7593a40ed3dSBarry Smith   PetscFunctionBegin;
7601eb62cbbSBarry Smith   /*  first count number of contributors to each processor */
761b1d57f15SBarry Smith   ierr = PetscMalloc(2*size*sizeof(PetscInt),&nprocs);CHKERRQ(ierr);
762b1d57f15SBarry Smith   ierr = PetscMemzero(nprocs,2*size*sizeof(PetscInt));CHKERRQ(ierr);
763b1d57f15SBarry Smith   ierr = PetscMalloc((N+1)*sizeof(PetscInt),&owner);CHKERRQ(ierr); /* see note*/
7646543fbbaSBarry Smith   j = 0;
7651eb62cbbSBarry Smith   for (i=0; i<N; i++) {
7666543fbbaSBarry Smith     if (lastidx > (idx = rows[i])) j = 0;
7676543fbbaSBarry Smith     lastidx = idx;
7686543fbbaSBarry Smith     for (; j<size; j++) {
7691eb62cbbSBarry Smith       if (idx >= owners[j] && idx < owners[j+1]) {
7706543fbbaSBarry Smith         nprocs[2*j]++;
7716543fbbaSBarry Smith         nprocs[2*j+1] = 1;
7726543fbbaSBarry Smith         owner[i] = j;
7736543fbbaSBarry Smith #if defined(PETSC_DEBUG)
7746543fbbaSBarry Smith         found = PETSC_TRUE;
7756543fbbaSBarry Smith #endif
7766543fbbaSBarry Smith         break;
7771eb62cbbSBarry Smith       }
7781eb62cbbSBarry Smith     }
7796543fbbaSBarry Smith #if defined(PETSC_DEBUG)
780e32f2f54SBarry Smith     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Index out of range");
7816543fbbaSBarry Smith     found = PETSC_FALSE;
7826543fbbaSBarry Smith #endif
7831eb62cbbSBarry Smith   }
784c1dc657dSBarry Smith   nsends = 0;  for (i=0; i<size; i++) { nsends += nprocs[2*i+1];}
7851eb62cbbSBarry Smith 
7867367270fSBarry Smith   if (A->nooffproczerorows) {
7877367270fSBarry Smith     if (nsends > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"You called MatSetOption(,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) but set an off process zero row");
7887367270fSBarry Smith     nrecvs = nsends;
7897367270fSBarry Smith     nmax   = N;
7907367270fSBarry Smith   } else {
7911eb62cbbSBarry Smith     /* inform other processors of number of messages and max length*/
792c1dc657dSBarry Smith     ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr);
7937367270fSBarry Smith   }
7941eb62cbbSBarry Smith 
7951eb62cbbSBarry Smith   /* post receives:   */
796b1d57f15SBarry Smith   ierr = PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(PetscInt),&rvalues);CHKERRQ(ierr);
797b0a32e0cSBarry Smith   ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr);
7981eb62cbbSBarry Smith   for (i=0; i<nrecvs; i++) {
799b1d57f15SBarry Smith     ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPIU_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr);
8001eb62cbbSBarry Smith   }
8011eb62cbbSBarry Smith 
8021eb62cbbSBarry Smith   /* do sends:
8031eb62cbbSBarry Smith       1) starts[i] gives the starting index in svalues for stuff going to
8041eb62cbbSBarry Smith          the ith processor
8051eb62cbbSBarry Smith   */
806b1d57f15SBarry Smith   ierr = PetscMalloc((N+1)*sizeof(PetscInt),&svalues);CHKERRQ(ierr);
807b0a32e0cSBarry Smith   ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr);
808b1d57f15SBarry Smith   ierr = PetscMalloc((size+1)*sizeof(PetscInt),&starts);CHKERRQ(ierr);
8091eb62cbbSBarry Smith   starts[0] = 0;
810c1dc657dSBarry Smith   for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
8111eb62cbbSBarry Smith   for (i=0; i<N; i++) {
8121eb62cbbSBarry Smith     svalues[starts[owner[i]]++] = rows[i];
8131eb62cbbSBarry Smith   }
8141eb62cbbSBarry Smith 
8151eb62cbbSBarry Smith   starts[0] = 0;
816c1dc657dSBarry Smith   for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
8171eb62cbbSBarry Smith   count = 0;
81817699dbbSLois Curfman McInnes   for (i=0; i<size; i++) {
819c1dc657dSBarry Smith     if (nprocs[2*i+1]) {
820b1d57f15SBarry Smith       ierr = MPI_Isend(svalues+starts[i],nprocs[2*i],MPIU_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr);
8211eb62cbbSBarry Smith     }
8221eb62cbbSBarry Smith   }
823606d414cSSatish Balay   ierr = PetscFree(starts);CHKERRQ(ierr);
8241eb62cbbSBarry Smith 
82517699dbbSLois Curfman McInnes   base = owners[rank];
8261eb62cbbSBarry Smith 
8271eb62cbbSBarry Smith   /*  wait on receives */
8281d79065fSBarry Smith   ierr   = PetscMalloc2(nrecvs,PetscInt,&lens,nrecvs,PetscInt,&source);CHKERRQ(ierr);
8291eb62cbbSBarry Smith   count  = nrecvs; slen = 0;
8301eb62cbbSBarry Smith   while (count) {
831ca161407SBarry Smith     ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
8321eb62cbbSBarry Smith     /* unpack receives into our local space */
833b1d57f15SBarry Smith     ierr = MPI_Get_count(&recv_status,MPIU_INT,&n);CHKERRQ(ierr);
834d6dfbf8fSBarry Smith     source[imdex]  = recv_status.MPI_SOURCE;
835d6dfbf8fSBarry Smith     lens[imdex]    = n;
8361eb62cbbSBarry Smith     slen          += n;
8371eb62cbbSBarry Smith     count--;
8381eb62cbbSBarry Smith   }
839606d414cSSatish Balay   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
8401eb62cbbSBarry Smith 
8411eb62cbbSBarry Smith   /* move the data into the send scatter */
842b1d57f15SBarry Smith   ierr = PetscMalloc((slen+1)*sizeof(PetscInt),&lrows);CHKERRQ(ierr);
8431eb62cbbSBarry Smith   count = 0;
8441eb62cbbSBarry Smith   for (i=0; i<nrecvs; i++) {
8451eb62cbbSBarry Smith     values = rvalues + i*nmax;
8461eb62cbbSBarry Smith     for (j=0; j<lens[i]; j++) {
8471eb62cbbSBarry Smith       lrows[count++] = values[j] - base;
8481eb62cbbSBarry Smith     }
8491eb62cbbSBarry Smith   }
850606d414cSSatish Balay   ierr = PetscFree(rvalues);CHKERRQ(ierr);
8511d79065fSBarry Smith   ierr = PetscFree2(lens,source);CHKERRQ(ierr);
852606d414cSSatish Balay   ierr = PetscFree(owner);CHKERRQ(ierr);
853606d414cSSatish Balay   ierr = PetscFree(nprocs);CHKERRQ(ierr);
8541eb62cbbSBarry Smith 
85597b48c8fSBarry Smith   /* fix right hand side if needed */
85697b48c8fSBarry Smith   if (x && b) {
85797b48c8fSBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
85897b48c8fSBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
859564f14d6SBarry Smith     for (i=0; i<slen; i++) {
86097b48c8fSBarry Smith       bb[lrows[i]] = diag*xx[lrows[i]];
86197b48c8fSBarry Smith     }
86297b48c8fSBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
86397b48c8fSBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
86497b48c8fSBarry Smith   }
8656eb55b6aSBarry Smith   /*
8666eb55b6aSBarry Smith         Zero the required rows. If the "diagonal block" of the matrix
867a8c7a070SBarry Smith      is square and the user wishes to set the diagonal we use separate
8686eb55b6aSBarry Smith      code so that MatSetValues() is not called for each diagonal allocating
8696eb55b6aSBarry Smith      new memory, thus calling lots of mallocs and slowing things down.
8706eb55b6aSBarry Smith 
8716eb55b6aSBarry Smith   */
872e2d53e46SBarry Smith   /* must zero l->B before l->A because the (diag) case below may put values into l->B*/
8732b40b63fSBarry Smith   ierr = MatZeroRows(l->B,slen,lrows,0.0,0,0);CHKERRQ(ierr);
874d0f46423SBarry Smith   if ((diag != 0.0) && (l->A->rmap->N == l->A->cmap->N)) {
8752b40b63fSBarry Smith     ierr = MatZeroRows(l->A,slen,lrows,diag,0,0);CHKERRQ(ierr);
876f4df32b1SMatthew Knepley   } else if (diag != 0.0) {
8772b40b63fSBarry Smith     ierr = MatZeroRows(l->A,slen,lrows,0.0,0,0);CHKERRQ(ierr);
878fa46199cSSatish Balay     if (((Mat_SeqAIJ*)l->A->data)->nonew) {
879e32f2f54SBarry Smith       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatZeroRows() on rectangular matrices cannot be used with the Mat options\n\
880512a5fc5SBarry Smith MAT_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR");
8816525c446SSatish Balay     }
882e2d53e46SBarry Smith     for (i = 0; i < slen; i++) {
883e2d53e46SBarry Smith       row  = lrows[i] + rstart;
884f4df32b1SMatthew Knepley       ierr = MatSetValues(A,1,&row,1,&row,&diag,INSERT_VALUES);CHKERRQ(ierr);
885e2d53e46SBarry Smith     }
886e2d53e46SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
887e2d53e46SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
8886eb55b6aSBarry Smith   } else {
8892b40b63fSBarry Smith     ierr = MatZeroRows(l->A,slen,lrows,0.0,0,0);CHKERRQ(ierr);
8906eb55b6aSBarry Smith   }
891606d414cSSatish Balay   ierr = PetscFree(lrows);CHKERRQ(ierr);
89272dacd9aSBarry Smith 
8931eb62cbbSBarry Smith   /* wait on sends */
8941eb62cbbSBarry Smith   if (nsends) {
895b0a32e0cSBarry Smith     ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr);
896ca161407SBarry Smith     ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr);
897606d414cSSatish Balay     ierr = PetscFree(send_status);CHKERRQ(ierr);
8981eb62cbbSBarry Smith   }
899606d414cSSatish Balay   ierr = PetscFree(send_waits);CHKERRQ(ierr);
900606d414cSSatish Balay   ierr = PetscFree(svalues);CHKERRQ(ierr);
9013a40ed3dSBarry Smith   PetscFunctionReturn(0);
9021eb62cbbSBarry Smith }
9031eb62cbbSBarry Smith 
9044a2ae208SSatish Balay #undef __FUNCT__
9059c7c4993SBarry Smith #define __FUNCT__ "MatZeroRowsColumns_MPIAIJ"
9069c7c4993SBarry Smith PetscErrorCode MatZeroRowsColumns_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
9079c7c4993SBarry Smith {
9089c7c4993SBarry Smith   Mat_MPIAIJ        *l = (Mat_MPIAIJ*)A->data;
9099c7c4993SBarry Smith   PetscErrorCode    ierr;
9109c7c4993SBarry Smith   PetscMPIInt       size = l->size,imdex,n,rank = l->rank,tag = ((PetscObject)A)->tag,lastidx = -1;
9119c7c4993SBarry Smith   PetscInt          i,*owners = A->rmap->range;
912564f14d6SBarry Smith   PetscInt          *nprocs,j,idx,nsends;
9139c7c4993SBarry Smith   PetscInt          nmax,*svalues,*starts,*owner,nrecvs;
9149c7c4993SBarry Smith   PetscInt          *rvalues,count,base,slen,*source;
915564f14d6SBarry Smith   PetscInt          *lens,*lrows,*values,m;
9169c7c4993SBarry Smith   MPI_Comm          comm = ((PetscObject)A)->comm;
9179c7c4993SBarry Smith   MPI_Request       *send_waits,*recv_waits;
9189c7c4993SBarry Smith   MPI_Status        recv_status,*send_status;
9199c7c4993SBarry Smith   const PetscScalar *xx;
920564f14d6SBarry Smith   PetscScalar       *bb,*mask;
921564f14d6SBarry Smith   Vec               xmask,lmask;
922564f14d6SBarry Smith   Mat_SeqAIJ        *aij = (Mat_SeqAIJ*)l->B->data;
923564f14d6SBarry Smith   const PetscInt    *aj, *ii,*ridx;
924564f14d6SBarry Smith   PetscScalar       *aa;
9259c7c4993SBarry Smith #if defined(PETSC_DEBUG)
9269c7c4993SBarry Smith   PetscBool         found = PETSC_FALSE;
9279c7c4993SBarry Smith #endif
9289c7c4993SBarry Smith 
9299c7c4993SBarry Smith   PetscFunctionBegin;
9309c7c4993SBarry Smith   /*  first count number of contributors to each processor */
9319c7c4993SBarry Smith   ierr = PetscMalloc(2*size*sizeof(PetscInt),&nprocs);CHKERRQ(ierr);
9329c7c4993SBarry Smith   ierr = PetscMemzero(nprocs,2*size*sizeof(PetscInt));CHKERRQ(ierr);
9339c7c4993SBarry Smith   ierr = PetscMalloc((N+1)*sizeof(PetscInt),&owner);CHKERRQ(ierr); /* see note*/
9349c7c4993SBarry Smith   j = 0;
9359c7c4993SBarry Smith   for (i=0; i<N; i++) {
9369c7c4993SBarry Smith     if (lastidx > (idx = rows[i])) j = 0;
9379c7c4993SBarry Smith     lastidx = idx;
9389c7c4993SBarry Smith     for (; j<size; j++) {
9399c7c4993SBarry Smith       if (idx >= owners[j] && idx < owners[j+1]) {
9409c7c4993SBarry Smith         nprocs[2*j]++;
9419c7c4993SBarry Smith         nprocs[2*j+1] = 1;
9429c7c4993SBarry Smith         owner[i] = j;
9439c7c4993SBarry Smith #if defined(PETSC_DEBUG)
9449c7c4993SBarry Smith         found = PETSC_TRUE;
9459c7c4993SBarry Smith #endif
9469c7c4993SBarry Smith         break;
9479c7c4993SBarry Smith       }
9489c7c4993SBarry Smith     }
9499c7c4993SBarry Smith #if defined(PETSC_DEBUG)
9509c7c4993SBarry Smith     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Index out of range");
9519c7c4993SBarry Smith     found = PETSC_FALSE;
9529c7c4993SBarry Smith #endif
9539c7c4993SBarry Smith   }
9549c7c4993SBarry Smith   nsends = 0;  for (i=0; i<size; i++) { nsends += nprocs[2*i+1];}
9559c7c4993SBarry Smith 
9569c7c4993SBarry Smith   /* inform other processors of number of messages and max length*/
9579c7c4993SBarry Smith   ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr);
9589c7c4993SBarry Smith 
9599c7c4993SBarry Smith   /* post receives:   */
9609c7c4993SBarry Smith   ierr = PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(PetscInt),&rvalues);CHKERRQ(ierr);
9619c7c4993SBarry Smith   ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr);
9629c7c4993SBarry Smith   for (i=0; i<nrecvs; i++) {
9639c7c4993SBarry Smith     ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPIU_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr);
9649c7c4993SBarry Smith   }
9659c7c4993SBarry Smith 
9669c7c4993SBarry Smith   /* do sends:
9679c7c4993SBarry Smith       1) starts[i] gives the starting index in svalues for stuff going to
9689c7c4993SBarry Smith          the ith processor
9699c7c4993SBarry Smith   */
9709c7c4993SBarry Smith   ierr = PetscMalloc((N+1)*sizeof(PetscInt),&svalues);CHKERRQ(ierr);
9719c7c4993SBarry Smith   ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr);
9729c7c4993SBarry Smith   ierr = PetscMalloc((size+1)*sizeof(PetscInt),&starts);CHKERRQ(ierr);
9739c7c4993SBarry Smith   starts[0] = 0;
9749c7c4993SBarry Smith   for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
9759c7c4993SBarry Smith   for (i=0; i<N; i++) {
9769c7c4993SBarry Smith     svalues[starts[owner[i]]++] = rows[i];
9779c7c4993SBarry Smith   }
9789c7c4993SBarry Smith 
9799c7c4993SBarry Smith   starts[0] = 0;
9809c7c4993SBarry Smith   for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
9819c7c4993SBarry Smith   count = 0;
9829c7c4993SBarry Smith   for (i=0; i<size; i++) {
9839c7c4993SBarry Smith     if (nprocs[2*i+1]) {
9849c7c4993SBarry Smith       ierr = MPI_Isend(svalues+starts[i],nprocs[2*i],MPIU_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr);
9859c7c4993SBarry Smith     }
9869c7c4993SBarry Smith   }
9879c7c4993SBarry Smith   ierr = PetscFree(starts);CHKERRQ(ierr);
9889c7c4993SBarry Smith 
9899c7c4993SBarry Smith   base = owners[rank];
9909c7c4993SBarry Smith 
9919c7c4993SBarry Smith   /*  wait on receives */
9929c7c4993SBarry Smith   ierr   = PetscMalloc2(nrecvs,PetscInt,&lens,nrecvs,PetscInt,&source);CHKERRQ(ierr);
9939c7c4993SBarry Smith   count  = nrecvs; slen = 0;
9949c7c4993SBarry Smith   while (count) {
9959c7c4993SBarry Smith     ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
9969c7c4993SBarry Smith     /* unpack receives into our local space */
9979c7c4993SBarry Smith     ierr = MPI_Get_count(&recv_status,MPIU_INT,&n);CHKERRQ(ierr);
9989c7c4993SBarry Smith     source[imdex]  = recv_status.MPI_SOURCE;
9999c7c4993SBarry Smith     lens[imdex]    = n;
10009c7c4993SBarry Smith     slen          += n;
10019c7c4993SBarry Smith     count--;
10029c7c4993SBarry Smith   }
10039c7c4993SBarry Smith   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
10049c7c4993SBarry Smith 
10059c7c4993SBarry Smith   /* move the data into the send scatter */
10069c7c4993SBarry Smith   ierr = PetscMalloc((slen+1)*sizeof(PetscInt),&lrows);CHKERRQ(ierr);
10079c7c4993SBarry Smith   count = 0;
10089c7c4993SBarry Smith   for (i=0; i<nrecvs; i++) {
10099c7c4993SBarry Smith     values = rvalues + i*nmax;
10109c7c4993SBarry Smith     for (j=0; j<lens[i]; j++) {
10119c7c4993SBarry Smith       lrows[count++] = values[j] - base;
10129c7c4993SBarry Smith     }
10139c7c4993SBarry Smith   }
10149c7c4993SBarry Smith   ierr = PetscFree(rvalues);CHKERRQ(ierr);
10159c7c4993SBarry Smith   ierr = PetscFree2(lens,source);CHKERRQ(ierr);
10169c7c4993SBarry Smith   ierr = PetscFree(owner);CHKERRQ(ierr);
10179c7c4993SBarry Smith   ierr = PetscFree(nprocs);CHKERRQ(ierr);
1018564f14d6SBarry Smith   /* lrows are the local rows to be zeroed, slen is the number of local rows */
10199c7c4993SBarry Smith 
1020564f14d6SBarry Smith   /* zero diagonal part of matrix */
1021564f14d6SBarry Smith   ierr = MatZeroRowsColumns(l->A,slen,lrows,diag,x,b);CHKERRQ(ierr);
10229c7c4993SBarry Smith 
1023564f14d6SBarry Smith   /* handle off diagonal part of matrix */
1024564f14d6SBarry Smith   ierr = MatGetVecs(A,&xmask,PETSC_NULL);CHKERRQ(ierr);
1025564f14d6SBarry Smith   ierr = VecDuplicate(l->lvec,&lmask);CHKERRQ(ierr);
1026564f14d6SBarry Smith   ierr = VecGetArray(xmask,&bb);CHKERRQ(ierr);
10279c7c4993SBarry Smith   for (i=0; i<slen; i++) {
1028564f14d6SBarry Smith     bb[lrows[i]] = 1;
10299c7c4993SBarry Smith   }
1030564f14d6SBarry Smith   ierr = VecRestoreArray(xmask,&bb);CHKERRQ(ierr);
1031564f14d6SBarry Smith   ierr = VecScatterBegin(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1032564f14d6SBarry Smith   ierr = VecScatterEnd(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
10336bf464f9SBarry Smith   ierr = VecDestroy(&xmask);CHKERRQ(ierr);
1034377aa5a1SBarry Smith   if (x) {
1035564f14d6SBarry Smith     ierr = VecScatterBegin(l->Mvctx,x,l->lvec,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1036564f14d6SBarry Smith     ierr = VecScatterEnd(l->Mvctx,x,l->lvec,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1037564f14d6SBarry Smith     ierr = VecGetArrayRead(l->lvec,&xx);CHKERRQ(ierr);
1038564f14d6SBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
1039377aa5a1SBarry Smith   }
1040377aa5a1SBarry Smith   ierr = VecGetArray(lmask,&mask);CHKERRQ(ierr);
1041564f14d6SBarry Smith 
1042564f14d6SBarry Smith   /* remove zeroed rows of off diagonal matrix */
1043564f14d6SBarry Smith   ii = aij->i;
1044564f14d6SBarry Smith   for (i=0; i<slen; i++) {
1045564f14d6SBarry Smith     ierr = PetscMemzero(aij->a + ii[lrows[i]],(ii[lrows[i]+1] - ii[lrows[i]])*sizeof(PetscScalar));CHKERRQ(ierr);
10469c7c4993SBarry Smith   }
1047564f14d6SBarry Smith 
1048564f14d6SBarry Smith   /* loop over all elements of off process part of matrix zeroing removed columns*/
1049564f14d6SBarry Smith   if (aij->compressedrow.use) {
1050564f14d6SBarry Smith     m    = aij->compressedrow.nrows;
1051564f14d6SBarry Smith     ii   = aij->compressedrow.i;
1052564f14d6SBarry Smith     ridx = aij->compressedrow.rindex;
1053564f14d6SBarry Smith     for (i=0; i<m; i++) {
1054564f14d6SBarry Smith       n   = ii[i+1] - ii[i];
1055564f14d6SBarry Smith       aj  = aij->j + ii[i];
1056564f14d6SBarry Smith       aa  = aij->a + ii[i];
1057564f14d6SBarry Smith 
1058564f14d6SBarry Smith       for (j=0; j<n; j++) {
105925266a92SSatish Balay         if (PetscAbsScalar(mask[*aj])) {
1060377aa5a1SBarry Smith           if (b) bb[*ridx] -= *aa*xx[*aj];
1061564f14d6SBarry Smith           *aa        = 0.0;
1062564f14d6SBarry Smith         }
1063564f14d6SBarry Smith         aa++;
1064564f14d6SBarry Smith         aj++;
1065564f14d6SBarry Smith       }
1066564f14d6SBarry Smith       ridx++;
1067564f14d6SBarry Smith     }
1068564f14d6SBarry Smith   } else { /* do not use compressed row format */
1069564f14d6SBarry Smith     m = l->B->rmap->n;
1070564f14d6SBarry Smith     for (i=0; i<m; i++) {
1071564f14d6SBarry Smith       n   = ii[i+1] - ii[i];
1072564f14d6SBarry Smith       aj  = aij->j + ii[i];
1073564f14d6SBarry Smith       aa  = aij->a + ii[i];
1074564f14d6SBarry Smith       for (j=0; j<n; j++) {
107525266a92SSatish Balay         if (PetscAbsScalar(mask[*aj])) {
1076377aa5a1SBarry Smith           if (b) bb[i] -= *aa*xx[*aj];
1077564f14d6SBarry Smith           *aa    = 0.0;
1078564f14d6SBarry Smith         }
1079564f14d6SBarry Smith         aa++;
1080564f14d6SBarry Smith         aj++;
1081564f14d6SBarry Smith       }
1082564f14d6SBarry Smith     }
1083564f14d6SBarry Smith   }
1084377aa5a1SBarry Smith   if (x) {
1085564f14d6SBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
1086564f14d6SBarry Smith     ierr = VecRestoreArrayRead(l->lvec,&xx);CHKERRQ(ierr);
1087377aa5a1SBarry Smith   }
1088377aa5a1SBarry Smith   ierr = VecRestoreArray(lmask,&mask);CHKERRQ(ierr);
10896bf464f9SBarry Smith   ierr = VecDestroy(&lmask);CHKERRQ(ierr);
10909c7c4993SBarry Smith   ierr = PetscFree(lrows);CHKERRQ(ierr);
10919c7c4993SBarry Smith 
10929c7c4993SBarry Smith   /* wait on sends */
10939c7c4993SBarry Smith   if (nsends) {
10949c7c4993SBarry Smith     ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr);
10959c7c4993SBarry Smith     ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr);
10969c7c4993SBarry Smith     ierr = PetscFree(send_status);CHKERRQ(ierr);
10979c7c4993SBarry Smith   }
10989c7c4993SBarry Smith   ierr = PetscFree(send_waits);CHKERRQ(ierr);
10999c7c4993SBarry Smith   ierr = PetscFree(svalues);CHKERRQ(ierr);
11009c7c4993SBarry Smith 
11019c7c4993SBarry Smith   PetscFunctionReturn(0);
11029c7c4993SBarry Smith }
11039c7c4993SBarry Smith 
11049c7c4993SBarry Smith #undef __FUNCT__
11054a2ae208SSatish Balay #define __FUNCT__ "MatMult_MPIAIJ"
1106dfbe8321SBarry Smith PetscErrorCode MatMult_MPIAIJ(Mat A,Vec xx,Vec yy)
11071eb62cbbSBarry Smith {
1108416022c9SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
1109dfbe8321SBarry Smith   PetscErrorCode ierr;
1110b1d57f15SBarry Smith   PetscInt       nt;
1111416022c9SBarry Smith 
11123a40ed3dSBarry Smith   PetscFunctionBegin;
1113a2ce50c7SBarry Smith   ierr = VecGetLocalSize(xx,&nt);CHKERRQ(ierr);
111465e19b50SBarry 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);
1115ca9f406cSSatish Balay   ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1116f830108cSBarry Smith   ierr = (*a->A->ops->mult)(a->A,xx,yy);CHKERRQ(ierr);
1117ca9f406cSSatish Balay   ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1118f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);CHKERRQ(ierr);
11193a40ed3dSBarry Smith   PetscFunctionReturn(0);
11201eb62cbbSBarry Smith }
11211eb62cbbSBarry Smith 
11224a2ae208SSatish Balay #undef __FUNCT__
1123bd0c2dcbSBarry Smith #define __FUNCT__ "MatMultDiagonalBlock_MPIAIJ"
1124bd0c2dcbSBarry Smith PetscErrorCode MatMultDiagonalBlock_MPIAIJ(Mat A,Vec bb,Vec xx)
1125bd0c2dcbSBarry Smith {
1126bd0c2dcbSBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
1127bd0c2dcbSBarry Smith   PetscErrorCode ierr;
1128bd0c2dcbSBarry Smith 
1129bd0c2dcbSBarry Smith   PetscFunctionBegin;
1130bd0c2dcbSBarry Smith   ierr = MatMultDiagonalBlock(a->A,bb,xx);CHKERRQ(ierr);
1131bd0c2dcbSBarry Smith   PetscFunctionReturn(0);
1132bd0c2dcbSBarry Smith }
1133bd0c2dcbSBarry Smith 
1134bd0c2dcbSBarry Smith #undef __FUNCT__
11354a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_MPIAIJ"
1136dfbe8321SBarry Smith PetscErrorCode MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1137da3a660dSBarry Smith {
1138416022c9SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
1139dfbe8321SBarry Smith   PetscErrorCode ierr;
11403a40ed3dSBarry Smith 
11413a40ed3dSBarry Smith   PetscFunctionBegin;
1142ca9f406cSSatish Balay   ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1143f830108cSBarry Smith   ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
1144ca9f406cSSatish Balay   ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1145f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);CHKERRQ(ierr);
11463a40ed3dSBarry Smith   PetscFunctionReturn(0);
1147da3a660dSBarry Smith }
1148da3a660dSBarry Smith 
11494a2ae208SSatish Balay #undef __FUNCT__
11504a2ae208SSatish Balay #define __FUNCT__ "MatMultTranspose_MPIAIJ"
1151dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_MPIAIJ(Mat A,Vec xx,Vec yy)
1152da3a660dSBarry Smith {
1153416022c9SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
1154dfbe8321SBarry Smith   PetscErrorCode ierr;
1155ace3abfcSBarry Smith   PetscBool      merged;
1156da3a660dSBarry Smith 
11573a40ed3dSBarry Smith   PetscFunctionBegin;
1158a5ff213dSBarry Smith   ierr = VecScatterGetMerged(a->Mvctx,&merged);CHKERRQ(ierr);
1159da3a660dSBarry Smith   /* do nondiagonal part */
11607c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
1161a5ff213dSBarry Smith   if (!merged) {
1162da3a660dSBarry Smith     /* send it on its way */
1163ca9f406cSSatish Balay     ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1164da3a660dSBarry Smith     /* do local part */
11657c922b88SBarry Smith     ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr);
1166da3a660dSBarry Smith     /* receive remote parts: note this assumes the values are not actually */
1167a5ff213dSBarry Smith     /* added in yy until the next line, */
1168ca9f406cSSatish Balay     ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1169a5ff213dSBarry Smith   } else {
1170a5ff213dSBarry Smith     /* do local part */
1171a5ff213dSBarry Smith     ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr);
1172a5ff213dSBarry Smith     /* send it on its way */
1173ca9f406cSSatish Balay     ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1174a5ff213dSBarry Smith     /* values actually were received in the Begin() but we need to call this nop */
1175ca9f406cSSatish Balay     ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1176a5ff213dSBarry Smith   }
11773a40ed3dSBarry Smith   PetscFunctionReturn(0);
1178da3a660dSBarry Smith }
1179da3a660dSBarry Smith 
1180cd0d46ebSvictorle EXTERN_C_BEGIN
1181cd0d46ebSvictorle #undef __FUNCT__
11825fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_MPIAIJ"
11837087cfbeSBarry Smith PetscErrorCode  MatIsTranspose_MPIAIJ(Mat Amat,Mat Bmat,PetscReal tol,PetscBool  *f)
1184cd0d46ebSvictorle {
11854f423910Svictorle   MPI_Comm       comm;
1186cd0d46ebSvictorle   Mat_MPIAIJ     *Aij = (Mat_MPIAIJ *) Amat->data, *Bij;
118766501d38Svictorle   Mat            Adia = Aij->A, Bdia, Aoff,Boff,*Aoffs,*Boffs;
1188cd0d46ebSvictorle   IS             Me,Notme;
11896849ba73SBarry Smith   PetscErrorCode ierr;
1190b1d57f15SBarry Smith   PetscInt       M,N,first,last,*notme,i;
1191b1d57f15SBarry Smith   PetscMPIInt    size;
1192cd0d46ebSvictorle 
1193cd0d46ebSvictorle   PetscFunctionBegin;
119442e5f5b4Svictorle   /* Easy test: symmetric diagonal block */
119566501d38Svictorle   Bij = (Mat_MPIAIJ *) Bmat->data; Bdia = Bij->A;
11965485867bSBarry Smith   ierr = MatIsTranspose(Adia,Bdia,tol,f);CHKERRQ(ierr);
1197cd0d46ebSvictorle   if (!*f) PetscFunctionReturn(0);
11984f423910Svictorle   ierr = PetscObjectGetComm((PetscObject)Amat,&comm);CHKERRQ(ierr);
1199b1d57f15SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
1200b1d57f15SBarry Smith   if (size == 1) PetscFunctionReturn(0);
120142e5f5b4Svictorle 
120242e5f5b4Svictorle   /* Hard test: off-diagonal block. This takes a MatGetSubMatrix. */
1203cd0d46ebSvictorle   ierr = MatGetSize(Amat,&M,&N);CHKERRQ(ierr);
1204cd0d46ebSvictorle   ierr = MatGetOwnershipRange(Amat,&first,&last);CHKERRQ(ierr);
1205b1d57f15SBarry Smith   ierr = PetscMalloc((N-last+first)*sizeof(PetscInt),&notme);CHKERRQ(ierr);
1206cd0d46ebSvictorle   for (i=0; i<first; i++) notme[i] = i;
1207cd0d46ebSvictorle   for (i=last; i<M; i++) notme[i-last+first] = i;
120870b3c8c7SBarry Smith   ierr = ISCreateGeneral(MPI_COMM_SELF,N-last+first,notme,PETSC_COPY_VALUES,&Notme);CHKERRQ(ierr);
1209268466fbSBarry Smith   ierr = ISCreateStride(MPI_COMM_SELF,last-first,first,1,&Me);CHKERRQ(ierr);
1210268466fbSBarry Smith   ierr = MatGetSubMatrices(Amat,1,&Me,&Notme,MAT_INITIAL_MATRIX,&Aoffs);CHKERRQ(ierr);
121166501d38Svictorle   Aoff = Aoffs[0];
1212268466fbSBarry Smith   ierr = MatGetSubMatrices(Bmat,1,&Notme,&Me,MAT_INITIAL_MATRIX,&Boffs);CHKERRQ(ierr);
121366501d38Svictorle   Boff = Boffs[0];
12145485867bSBarry Smith   ierr = MatIsTranspose(Aoff,Boff,tol,f);CHKERRQ(ierr);
121566501d38Svictorle   ierr = MatDestroyMatrices(1,&Aoffs);CHKERRQ(ierr);
121666501d38Svictorle   ierr = MatDestroyMatrices(1,&Boffs);CHKERRQ(ierr);
12176bf464f9SBarry Smith   ierr = ISDestroy(&Me);CHKERRQ(ierr);
12186bf464f9SBarry Smith   ierr = ISDestroy(&Notme);CHKERRQ(ierr);
12193e0d0d19SHong Zhang   ierr = PetscFree(notme);CHKERRQ(ierr);
1220cd0d46ebSvictorle    PetscFunctionReturn(0);
1221cd0d46ebSvictorle }
1222cd0d46ebSvictorle EXTERN_C_END
1223cd0d46ebSvictorle 
12244a2ae208SSatish Balay #undef __FUNCT__
12254a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_MPIAIJ"
1226dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1227da3a660dSBarry Smith {
1228416022c9SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
1229dfbe8321SBarry Smith   PetscErrorCode ierr;
1230da3a660dSBarry Smith 
12313a40ed3dSBarry Smith   PetscFunctionBegin;
1232da3a660dSBarry Smith   /* do nondiagonal part */
12337c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
1234da3a660dSBarry Smith   /* send it on its way */
1235ca9f406cSSatish Balay   ierr = VecScatterBegin(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1236da3a660dSBarry Smith   /* do local part */
12377c922b88SBarry Smith   ierr = (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
1238a5ff213dSBarry Smith   /* receive remote parts */
1239ca9f406cSSatish Balay   ierr = VecScatterEnd(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
12403a40ed3dSBarry Smith   PetscFunctionReturn(0);
1241da3a660dSBarry Smith }
1242da3a660dSBarry Smith 
12431eb62cbbSBarry Smith /*
12441eb62cbbSBarry Smith   This only works correctly for square matrices where the subblock A->A is the
12451eb62cbbSBarry Smith    diagonal block
12461eb62cbbSBarry Smith */
12474a2ae208SSatish Balay #undef __FUNCT__
12484a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_MPIAIJ"
1249dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_MPIAIJ(Mat A,Vec v)
12501eb62cbbSBarry Smith {
1251dfbe8321SBarry Smith   PetscErrorCode ierr;
1252416022c9SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
12533a40ed3dSBarry Smith 
12543a40ed3dSBarry Smith   PetscFunctionBegin;
1255e7e72b3dSBarry Smith   if (A->rmap->N != A->cmap->N) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block");
1256e7e72b3dSBarry 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");
12573a40ed3dSBarry Smith   ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr);
12583a40ed3dSBarry Smith   PetscFunctionReturn(0);
12591eb62cbbSBarry Smith }
12601eb62cbbSBarry Smith 
12614a2ae208SSatish Balay #undef __FUNCT__
12624a2ae208SSatish Balay #define __FUNCT__ "MatScale_MPIAIJ"
1263f4df32b1SMatthew Knepley PetscErrorCode MatScale_MPIAIJ(Mat A,PetscScalar aa)
1264052efed2SBarry Smith {
1265052efed2SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
1266dfbe8321SBarry Smith   PetscErrorCode ierr;
12673a40ed3dSBarry Smith 
12683a40ed3dSBarry Smith   PetscFunctionBegin;
1269f4df32b1SMatthew Knepley   ierr = MatScale(a->A,aa);CHKERRQ(ierr);
1270f4df32b1SMatthew Knepley   ierr = MatScale(a->B,aa);CHKERRQ(ierr);
12713a40ed3dSBarry Smith   PetscFunctionReturn(0);
1272052efed2SBarry Smith }
1273052efed2SBarry Smith 
12744a2ae208SSatish Balay #undef __FUNCT__
12754a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_MPIAIJ"
1276dfbe8321SBarry Smith PetscErrorCode MatDestroy_MPIAIJ(Mat mat)
12771eb62cbbSBarry Smith {
127844a69424SLois Curfman McInnes   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
1279dfbe8321SBarry Smith   PetscErrorCode ierr;
128083e2fdc7SBarry Smith 
12813a40ed3dSBarry Smith   PetscFunctionBegin;
1282aa482453SBarry Smith #if defined(PETSC_USE_LOG)
1283d0f46423SBarry Smith   PetscLogObjectState((PetscObject)mat,"Rows=%D, Cols=%D",mat->rmap->N,mat->cmap->N);
1284a5a9c739SBarry Smith #endif
12858798bf22SSatish Balay   ierr = MatStashDestroy_Private(&mat->stash);CHKERRQ(ierr);
12866bf464f9SBarry Smith   ierr = VecDestroy(&aij->diag);CHKERRQ(ierr);
12876bf464f9SBarry Smith   ierr = MatDestroy(&aij->A);CHKERRQ(ierr);
12886bf464f9SBarry Smith   ierr = MatDestroy(&aij->B);CHKERRQ(ierr);
1289aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
12906bc0bbbfSBarry Smith   ierr = PetscTableDestroy(&aij->colmap);CHKERRQ(ierr);
1291b1fc9764SSatish Balay #else
129205b42c5fSBarry Smith   ierr = PetscFree(aij->colmap);CHKERRQ(ierr);
1293b1fc9764SSatish Balay #endif
129405b42c5fSBarry Smith   ierr = PetscFree(aij->garray);CHKERRQ(ierr);
12956bf464f9SBarry Smith   ierr = VecDestroy(&aij->lvec);CHKERRQ(ierr);
12966bf464f9SBarry Smith   ierr = VecScatterDestroy(&aij->Mvctx);CHKERRQ(ierr);
129703095fedSBarry Smith   ierr = PetscFree2(aij->rowvalues,aij->rowindices);CHKERRQ(ierr);
12988aa348c1SBarry Smith   ierr = PetscFree(aij->ld);CHKERRQ(ierr);
1299bf0cc555SLisandro Dalcin   ierr = PetscFree(mat->data);CHKERRQ(ierr);
1300901853e0SKris Buschelman 
1301dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)mat,0);CHKERRQ(ierr);
1302901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
1303901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
1304901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatGetDiagonalBlock_C","",PETSC_NULL);CHKERRQ(ierr);
1305901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr);
1306901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
1307ff69c46cSKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
1308901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatDiagonalScaleLocal_C","",PETSC_NULL);CHKERRQ(ierr);
1309471cc821SHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_mpisbaij_C","",PETSC_NULL);CHKERRQ(ierr);
13103a40ed3dSBarry Smith   PetscFunctionReturn(0);
13111eb62cbbSBarry Smith }
1312ee50ffe9SBarry Smith 
13134a2ae208SSatish Balay #undef __FUNCT__
13148e2fed03SBarry Smith #define __FUNCT__ "MatView_MPIAIJ_Binary"
1315dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ_Binary(Mat mat,PetscViewer viewer)
13168e2fed03SBarry Smith {
13178e2fed03SBarry Smith   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*)mat->data;
13188e2fed03SBarry Smith   Mat_SeqAIJ*       A = (Mat_SeqAIJ*)aij->A->data;
13198e2fed03SBarry Smith   Mat_SeqAIJ*       B = (Mat_SeqAIJ*)aij->B->data;
13206849ba73SBarry Smith   PetscErrorCode    ierr;
132132dcc486SBarry Smith   PetscMPIInt       rank,size,tag = ((PetscObject)viewer)->tag;
13226f69ff64SBarry Smith   int               fd;
1323a788621eSSatish Balay   PetscInt          nz,header[4],*row_lengths,*range=0,rlen,i;
1324d0f46423SBarry Smith   PetscInt          nzmax,*column_indices,j,k,col,*garray = aij->garray,cnt,cstart = mat->cmap->rstart,rnz;
13258e2fed03SBarry Smith   PetscScalar       *column_values;
132685ebf7a4SBarry Smith   PetscInt          message_count,flowcontrolcount;
1327b37d52dbSMark F. Adams   FILE              *file;
13288e2fed03SBarry Smith 
13298e2fed03SBarry Smith   PetscFunctionBegin;
13307adad957SLisandro Dalcin   ierr = MPI_Comm_rank(((PetscObject)mat)->comm,&rank);CHKERRQ(ierr);
13317adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr);
13328e2fed03SBarry Smith   nz   = A->nz + B->nz;
1333958c9bccSBarry Smith   if (!rank) {
13340700a824SBarry Smith     header[0] = MAT_FILE_CLASSID;
1335d0f46423SBarry Smith     header[1] = mat->rmap->N;
1336d0f46423SBarry Smith     header[2] = mat->cmap->N;
13377adad957SLisandro Dalcin     ierr = MPI_Reduce(&nz,&header[3],1,MPIU_INT,MPI_SUM,0,((PetscObject)mat)->comm);CHKERRQ(ierr);
13388e2fed03SBarry Smith     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
13396f69ff64SBarry Smith     ierr = PetscBinaryWrite(fd,header,4,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
13408e2fed03SBarry Smith     /* get largest number of rows any processor has */
1341d0f46423SBarry Smith     rlen = mat->rmap->n;
1342d0f46423SBarry Smith     range = mat->rmap->range;
13438e2fed03SBarry Smith     for (i=1; i<size; i++) {
13448e2fed03SBarry Smith       rlen = PetscMax(rlen,range[i+1] - range[i]);
13458e2fed03SBarry Smith     }
13468e2fed03SBarry Smith   } else {
13477adad957SLisandro Dalcin     ierr = MPI_Reduce(&nz,0,1,MPIU_INT,MPI_SUM,0,((PetscObject)mat)->comm);CHKERRQ(ierr);
1348d0f46423SBarry Smith     rlen = mat->rmap->n;
13498e2fed03SBarry Smith   }
13508e2fed03SBarry Smith 
13518e2fed03SBarry Smith   /* load up the local row counts */
1352b1d57f15SBarry Smith   ierr = PetscMalloc((rlen+1)*sizeof(PetscInt),&row_lengths);CHKERRQ(ierr);
1353d0f46423SBarry Smith   for (i=0; i<mat->rmap->n; i++) {
13548e2fed03SBarry Smith     row_lengths[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i];
13558e2fed03SBarry Smith   }
13568e2fed03SBarry Smith 
13578e2fed03SBarry Smith   /* store the row lengths to the file */
135885ebf7a4SBarry Smith   ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr);
1359958c9bccSBarry Smith   if (!rank) {
1360d0f46423SBarry Smith     ierr = PetscBinaryWrite(fd,row_lengths,mat->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
13618e2fed03SBarry Smith     for (i=1; i<size; i++) {
1362639ff905SBarry Smith       ierr = PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);CHKERRQ(ierr);
13638e2fed03SBarry Smith       rlen = range[i+1] - range[i];
1364a25532f0SBarry Smith       ierr = MPIULong_Recv(row_lengths,rlen,MPIU_INT,i,tag,((PetscObject)mat)->comm);CHKERRQ(ierr);
13656f69ff64SBarry Smith       ierr = PetscBinaryWrite(fd,row_lengths,rlen,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
13668e2fed03SBarry Smith     }
1367639ff905SBarry Smith     ierr = PetscViewerFlowControlEndMaster(viewer,&message_count);CHKERRQ(ierr);
13688e2fed03SBarry Smith   } else {
1369639ff905SBarry Smith     ierr = PetscViewerFlowControlStepWorker(viewer,rank,&message_count);CHKERRQ(ierr);
1370a25532f0SBarry Smith     ierr = MPIULong_Send(row_lengths,mat->rmap->n,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr);
1371639ff905SBarry Smith     ierr = PetscViewerFlowControlEndWorker(viewer,&message_count);CHKERRQ(ierr);
13728e2fed03SBarry Smith   }
13738e2fed03SBarry Smith   ierr = PetscFree(row_lengths);CHKERRQ(ierr);
13748e2fed03SBarry Smith 
13758e2fed03SBarry Smith   /* load up the local column indices */
13761147fc2aSKarl Rupp   nzmax = nz; /* th processor needs space a largest processor needs */
13777adad957SLisandro Dalcin   ierr = MPI_Reduce(&nz,&nzmax,1,MPIU_INT,MPI_MAX,0,((PetscObject)mat)->comm);CHKERRQ(ierr);
1378b1d57f15SBarry Smith   ierr = PetscMalloc((nzmax+1)*sizeof(PetscInt),&column_indices);CHKERRQ(ierr);
13798e2fed03SBarry Smith   cnt  = 0;
1380d0f46423SBarry Smith   for (i=0; i<mat->rmap->n; i++) {
13818e2fed03SBarry Smith     for (j=B->i[i]; j<B->i[i+1]; j++) {
13828e2fed03SBarry Smith       if ((col = garray[B->j[j]]) > cstart) break;
13838e2fed03SBarry Smith       column_indices[cnt++] = col;
13848e2fed03SBarry Smith     }
13858e2fed03SBarry Smith     for (k=A->i[i]; k<A->i[i+1]; k++) {
13868e2fed03SBarry Smith       column_indices[cnt++] = A->j[k] + cstart;
13878e2fed03SBarry Smith     }
13888e2fed03SBarry Smith     for (; j<B->i[i+1]; j++) {
13898e2fed03SBarry Smith       column_indices[cnt++] = garray[B->j[j]];
13908e2fed03SBarry Smith     }
13918e2fed03SBarry Smith   }
1392e32f2f54SBarry 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);
13938e2fed03SBarry Smith 
13948e2fed03SBarry Smith   /* store the column indices to the file */
139585ebf7a4SBarry Smith    ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr);
1396958c9bccSBarry Smith   if (!rank) {
13978e2fed03SBarry Smith     MPI_Status status;
13986f69ff64SBarry Smith     ierr = PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
13998e2fed03SBarry Smith     for (i=1; i<size; i++) {
1400639ff905SBarry Smith       ierr = PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);CHKERRQ(ierr);
14017adad957SLisandro Dalcin       ierr = MPI_Recv(&rnz,1,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr);
1402e32f2f54SBarry Smith       if (rnz > nzmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax);
1403a25532f0SBarry Smith       ierr = MPIULong_Recv(column_indices,rnz,MPIU_INT,i,tag,((PetscObject)mat)->comm);CHKERRQ(ierr);
14046f69ff64SBarry Smith       ierr = PetscBinaryWrite(fd,column_indices,rnz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
14058e2fed03SBarry Smith     }
1406639ff905SBarry Smith      ierr = PetscViewerFlowControlEndMaster(viewer,&message_count);CHKERRQ(ierr);
14078e2fed03SBarry Smith   } else {
1408639ff905SBarry Smith     ierr = PetscViewerFlowControlStepWorker(viewer,rank,&message_count);CHKERRQ(ierr);
14097adad957SLisandro Dalcin     ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr);
1410a25532f0SBarry Smith     ierr = MPIULong_Send(column_indices,nz,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr);
1411639ff905SBarry Smith     ierr = PetscViewerFlowControlEndWorker(viewer,&message_count);CHKERRQ(ierr);
14128e2fed03SBarry Smith   }
14138e2fed03SBarry Smith   ierr = PetscFree(column_indices);CHKERRQ(ierr);
14148e2fed03SBarry Smith 
14158e2fed03SBarry Smith   /* load up the local column values */
14168e2fed03SBarry Smith   ierr = PetscMalloc((nzmax+1)*sizeof(PetscScalar),&column_values);CHKERRQ(ierr);
14178e2fed03SBarry Smith   cnt  = 0;
1418d0f46423SBarry Smith   for (i=0; i<mat->rmap->n; i++) {
14198e2fed03SBarry Smith     for (j=B->i[i]; j<B->i[i+1]; j++) {
14208e2fed03SBarry Smith       if (garray[B->j[j]] > cstart) break;
14218e2fed03SBarry Smith       column_values[cnt++] = B->a[j];
14228e2fed03SBarry Smith     }
14238e2fed03SBarry Smith     for (k=A->i[i]; k<A->i[i+1]; k++) {
14248e2fed03SBarry Smith       column_values[cnt++] = A->a[k];
14258e2fed03SBarry Smith     }
14268e2fed03SBarry Smith     for (; j<B->i[i+1]; j++) {
14278e2fed03SBarry Smith       column_values[cnt++] = B->a[j];
14288e2fed03SBarry Smith     }
14298e2fed03SBarry Smith   }
1430e32f2f54SBarry 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);
14318e2fed03SBarry Smith 
14328e2fed03SBarry Smith   /* store the column values to the file */
143385ebf7a4SBarry Smith    ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr);
1434958c9bccSBarry Smith   if (!rank) {
14358e2fed03SBarry Smith     MPI_Status status;
14366f69ff64SBarry Smith     ierr = PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr);
14378e2fed03SBarry Smith     for (i=1; i<size; i++) {
1438639ff905SBarry Smith        ierr = PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);CHKERRQ(ierr);
14397adad957SLisandro Dalcin       ierr = MPI_Recv(&rnz,1,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr);
1440e32f2f54SBarry Smith       if (rnz > nzmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax);
1441a25532f0SBarry Smith       ierr = MPIULong_Recv(column_values,rnz,MPIU_SCALAR,i,tag,((PetscObject)mat)->comm);CHKERRQ(ierr);
14426f69ff64SBarry Smith       ierr = PetscBinaryWrite(fd,column_values,rnz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr);
14438e2fed03SBarry Smith     }
1444639ff905SBarry Smith     ierr = PetscViewerFlowControlEndMaster(viewer,&message_count);CHKERRQ(ierr);
14458e2fed03SBarry Smith   } else {
1446639ff905SBarry Smith     ierr = PetscViewerFlowControlStepWorker(viewer,rank,&message_count);CHKERRQ(ierr);
14477adad957SLisandro Dalcin     ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr);
1448a25532f0SBarry Smith     ierr = MPIULong_Send(column_values,nz,MPIU_SCALAR,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr);
1449639ff905SBarry Smith     ierr = PetscViewerFlowControlEndWorker(viewer,&message_count);CHKERRQ(ierr);
14508e2fed03SBarry Smith   }
14518e2fed03SBarry Smith   ierr = PetscFree(column_values);CHKERRQ(ierr);
1452b37d52dbSMark F. Adams 
1453b37d52dbSMark F. Adams   ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr);
1454b37d52dbSMark F. Adams   if (file) {
1455b37d52dbSMark F. Adams     fprintf(file,"-matload_block_size %d\n",(int)mat->rmap->bs);
1456b37d52dbSMark F. Adams   }
1457b37d52dbSMark F. Adams 
14588e2fed03SBarry Smith   PetscFunctionReturn(0);
14598e2fed03SBarry Smith }
14608e2fed03SBarry Smith 
14618e2fed03SBarry Smith #undef __FUNCT__
14624a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ_ASCIIorDraworSocket"
1463dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
1464416022c9SBarry Smith {
146544a69424SLois Curfman McInnes   Mat_MPIAIJ        *aij = (Mat_MPIAIJ*)mat->data;
1466dfbe8321SBarry Smith   PetscErrorCode    ierr;
146732dcc486SBarry Smith   PetscMPIInt       rank = aij->rank,size = aij->size;
1468ace3abfcSBarry Smith   PetscBool         isdraw,iascii,isbinary;
1469b0a32e0cSBarry Smith   PetscViewer       sviewer;
1470f3ef73ceSBarry Smith   PetscViewerFormat format;
1471416022c9SBarry Smith 
14723a40ed3dSBarry Smith   PetscFunctionBegin;
1473251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1474251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1475251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
147632077d6dSBarry Smith   if (iascii) {
1477b0a32e0cSBarry Smith     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
1478456192e2SBarry Smith     if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
14794e220ebcSLois Curfman McInnes       MatInfo    info;
1480ace3abfcSBarry Smith       PetscBool  inodes;
1481923f20ffSKris Buschelman 
14827adad957SLisandro Dalcin       ierr = MPI_Comm_rank(((PetscObject)mat)->comm,&rank);CHKERRQ(ierr);
1483888f2ed8SSatish Balay       ierr = MatGetInfo(mat,MAT_LOCAL,&info);CHKERRQ(ierr);
1484923f20ffSKris Buschelman       ierr = MatInodeGetInodeSizes(aij->A,PETSC_NULL,(PetscInt **)&inodes,PETSC_NULL);CHKERRQ(ierr);
14857b23a99aSBarry Smith       ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_TRUE);CHKERRQ(ierr);
1486923f20ffSKris Buschelman       if (!inodes) {
148777431f27SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, not using I-node routines\n",
1488d0f46423SBarry Smith                                                   rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);CHKERRQ(ierr);
14896831982aSBarry Smith       } else {
149077431f27SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, using I-node routines\n",
1491d0f46423SBarry Smith                                                   rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);CHKERRQ(ierr);
14926831982aSBarry Smith       }
1493888f2ed8SSatish Balay       ierr = MatGetInfo(aij->A,MAT_LOCAL,&info);CHKERRQ(ierr);
149477431f27SBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr);
1495888f2ed8SSatish Balay       ierr = MatGetInfo(aij->B,MAT_LOCAL,&info);CHKERRQ(ierr);
149677431f27SBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr);
1497b0a32e0cSBarry Smith       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
14987b23a99aSBarry Smith       ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_FALSE);CHKERRQ(ierr);
149907d81ca4SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Information on VecScatter used in matrix-vector product: \n");CHKERRQ(ierr);
1500a40aa06bSLois Curfman McInnes       ierr = VecScatterView(aij->Mvctx,viewer);CHKERRQ(ierr);
15013a40ed3dSBarry Smith       PetscFunctionReturn(0);
1502fb9695e5SSatish Balay     } else if (format == PETSC_VIEWER_ASCII_INFO) {
1503923f20ffSKris Buschelman       PetscInt   inodecount,inodelimit,*inodes;
1504923f20ffSKris Buschelman       ierr = MatInodeGetInodeSizes(aij->A,&inodecount,&inodes,&inodelimit);CHKERRQ(ierr);
1505923f20ffSKris Buschelman       if (inodes) {
1506923f20ffSKris Buschelman         ierr = PetscViewerASCIIPrintf(viewer,"using I-node (on process 0) routines: found %D nodes, limit used is %D\n",inodecount,inodelimit);CHKERRQ(ierr);
1507d38fa0fbSBarry Smith       } else {
1508d38fa0fbSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"not using I-node (on process 0) routines\n");CHKERRQ(ierr);
1509d38fa0fbSBarry Smith       }
15103a40ed3dSBarry Smith       PetscFunctionReturn(0);
15114aedb280SBarry Smith     } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
15124aedb280SBarry Smith       PetscFunctionReturn(0);
151308480c60SBarry Smith     }
15148e2fed03SBarry Smith   } else if (isbinary) {
15158e2fed03SBarry Smith     if (size == 1) {
15167adad957SLisandro Dalcin       ierr = PetscObjectSetName((PetscObject)aij->A,((PetscObject)mat)->name);CHKERRQ(ierr);
15178e2fed03SBarry Smith       ierr = MatView(aij->A,viewer);CHKERRQ(ierr);
15188e2fed03SBarry Smith     } else {
15198e2fed03SBarry Smith       ierr = MatView_MPIAIJ_Binary(mat,viewer);CHKERRQ(ierr);
15208e2fed03SBarry Smith     }
15218e2fed03SBarry Smith     PetscFunctionReturn(0);
15220f5bd95cSBarry Smith   } else if (isdraw) {
1523b0a32e0cSBarry Smith     PetscDraw  draw;
1524ace3abfcSBarry Smith     PetscBool  isnull;
1525b0a32e0cSBarry Smith     ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
1526b0a32e0cSBarry Smith     ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
152719bcc07fSBarry Smith   }
152819bcc07fSBarry Smith 
152917699dbbSLois Curfman McInnes   if (size == 1) {
15307adad957SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)aij->A,((PetscObject)mat)->name);CHKERRQ(ierr);
153178b31e54SBarry Smith     ierr = MatView(aij->A,viewer);CHKERRQ(ierr);
15323a40ed3dSBarry Smith   } else {
153395373324SBarry Smith     /* assemble the entire matrix onto first processor. */
153495373324SBarry Smith     Mat         A;
1535ec8511deSBarry Smith     Mat_SeqAIJ  *Aloc;
1536d0f46423SBarry Smith     PetscInt    M = mat->rmap->N,N = mat->cmap->N,m,*ai,*aj,row,*cols,i,*ct;
1537dd6ea824SBarry Smith     MatScalar   *a;
15382ee70a88SLois Curfman McInnes 
153932a366e4SMatthew Knepley     if (mat->rmap->N > 1024) {
1540ace3abfcSBarry Smith       PetscBool  flg = PETSC_FALSE;
154132a366e4SMatthew Knepley 
1542acfcf0e5SJed Brown       ierr = PetscOptionsGetBool(((PetscObject) mat)->prefix, "-mat_ascii_output_large", &flg,PETSC_NULL);CHKERRQ(ierr);
1543f23aa3ddSBarry Smith       if (!flg) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_OUTOFRANGE,"ASCII matrix output not allowed for matrices with more than 1024 rows, use binary format instead.\nYou can override this restriction using -mat_ascii_output_large.");
154432a366e4SMatthew Knepley     }
15450805154bSBarry Smith 
15467adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)mat)->comm,&A);CHKERRQ(ierr);
154717699dbbSLois Curfman McInnes     if (!rank) {
1548f69a0ea3SMatthew Knepley       ierr = MatSetSizes(A,M,N,M,N);CHKERRQ(ierr);
15493a40ed3dSBarry Smith     } else {
1550f69a0ea3SMatthew Knepley       ierr = MatSetSizes(A,0,0,M,N);CHKERRQ(ierr);
155195373324SBarry Smith     }
1552f204ca49SKris Buschelman     /* This is just a temporary matrix, so explicitly using MATMPIAIJ is probably best */
1553f204ca49SKris Buschelman     ierr = MatSetType(A,MATMPIAIJ);CHKERRQ(ierr);
1554f204ca49SKris Buschelman     ierr = MatMPIAIJSetPreallocation(A,0,PETSC_NULL,0,PETSC_NULL);CHKERRQ(ierr);
15552b82e772SSatish Balay     ierr = MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr);
155652e6d16bSBarry Smith     ierr = PetscLogObjectParent(mat,A);CHKERRQ(ierr);
1557416022c9SBarry Smith 
155895373324SBarry Smith     /* copy over the A part */
1559ec8511deSBarry Smith     Aloc = (Mat_SeqAIJ*)aij->A->data;
1560d0f46423SBarry Smith     m = aij->A->rmap->n; ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
1561d0f46423SBarry Smith     row = mat->rmap->rstart;
1562d0f46423SBarry Smith     for (i=0; i<ai[m]; i++) {aj[i] += mat->cmap->rstart ;}
156395373324SBarry Smith     for (i=0; i<m; i++) {
1564416022c9SBarry Smith       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);CHKERRQ(ierr);
156595373324SBarry Smith       row++; a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
156695373324SBarry Smith     }
15672ee70a88SLois Curfman McInnes     aj = Aloc->j;
1568d0f46423SBarry Smith     for (i=0; i<ai[m]; i++) {aj[i] -= mat->cmap->rstart;}
156995373324SBarry Smith 
157095373324SBarry Smith     /* copy over the B part */
1571ec8511deSBarry Smith     Aloc = (Mat_SeqAIJ*)aij->B->data;
1572d0f46423SBarry Smith     m    = aij->B->rmap->n;  ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
1573d0f46423SBarry Smith     row  = mat->rmap->rstart;
1574b1d57f15SBarry Smith     ierr = PetscMalloc((ai[m]+1)*sizeof(PetscInt),&cols);CHKERRQ(ierr);
1575b0a32e0cSBarry Smith     ct   = cols;
1576bfec09a0SHong Zhang     for (i=0; i<ai[m]; i++) {cols[i] = aij->garray[aj[i]];}
157795373324SBarry Smith     for (i=0; i<m; i++) {
1578416022c9SBarry Smith       ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);CHKERRQ(ierr);
157995373324SBarry Smith       row++; a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
158095373324SBarry Smith     }
1581606d414cSSatish Balay     ierr = PetscFree(ct);CHKERRQ(ierr);
15826d4a8577SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
15836d4a8577SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
158455843e3eSBarry Smith     /*
158555843e3eSBarry Smith        Everyone has to call to draw the matrix since the graphics waits are
1586b0a32e0cSBarry Smith        synchronized across all processors that share the PetscDraw object
158755843e3eSBarry Smith     */
1588b0a32e0cSBarry Smith     ierr = PetscViewerGetSingleton(viewer,&sviewer);CHKERRQ(ierr);
1589e03a110bSBarry Smith     if (!rank) {
15907adad957SLisandro Dalcin       ierr = PetscObjectSetName((PetscObject)((Mat_MPIAIJ*)(A->data))->A,((PetscObject)mat)->name);CHKERRQ(ierr);
15917566de4bSShri Abhyankar       /* Set the type name to MATMPIAIJ so that the correct type can be printed out by PetscObjectPrintClassNamePrefixType() in MatView_SeqAIJ_ASCII()*/
15927566de4bSShri Abhyankar       PetscStrcpy(((PetscObject)((Mat_MPIAIJ*)(A->data))->A)->type_name,MATMPIAIJ);
15936831982aSBarry Smith       ierr = MatView(((Mat_MPIAIJ*)(A->data))->A,sviewer);CHKERRQ(ierr);
159495373324SBarry Smith     }
1595b0a32e0cSBarry Smith     ierr = PetscViewerRestoreSingleton(viewer,&sviewer);CHKERRQ(ierr);
15966bf464f9SBarry Smith     ierr = MatDestroy(&A);CHKERRQ(ierr);
159795373324SBarry Smith   }
15983a40ed3dSBarry Smith   PetscFunctionReturn(0);
15991eb62cbbSBarry Smith }
16001eb62cbbSBarry Smith 
16014a2ae208SSatish Balay #undef __FUNCT__
16024a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ"
1603dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ(Mat mat,PetscViewer viewer)
1604416022c9SBarry Smith {
1605dfbe8321SBarry Smith   PetscErrorCode ierr;
1606ace3abfcSBarry Smith   PetscBool      iascii,isdraw,issocket,isbinary;
1607416022c9SBarry Smith 
16083a40ed3dSBarry Smith   PetscFunctionBegin;
1609251f4c67SDmitry Karpeev   ierr  = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1610251f4c67SDmitry Karpeev   ierr  = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1611251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
1612251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSOCKET,&issocket);CHKERRQ(ierr);
161332077d6dSBarry Smith   if (iascii || isdraw || isbinary || issocket) {
16147b2a1423SBarry Smith     ierr = MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer);CHKERRQ(ierr);
1615416022c9SBarry Smith   }
16163a40ed3dSBarry Smith   PetscFunctionReturn(0);
1617416022c9SBarry Smith }
1618416022c9SBarry Smith 
16194a2ae208SSatish Balay #undef __FUNCT__
162041f059aeSBarry Smith #define __FUNCT__ "MatSOR_MPIAIJ"
162141f059aeSBarry Smith PetscErrorCode MatSOR_MPIAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
16228a729477SBarry Smith {
162344a69424SLois Curfman McInnes   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)matin->data;
1624dfbe8321SBarry Smith   PetscErrorCode ierr;
16256987fefcSBarry Smith   Vec            bb1 = 0;
1626ace3abfcSBarry Smith   PetscBool      hasop;
16278a729477SBarry Smith 
16283a40ed3dSBarry Smith   PetscFunctionBegin;
1629a2b30743SBarry Smith   if (flag == SOR_APPLY_UPPER) {
163041f059aeSBarry Smith     ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
1631a2b30743SBarry Smith     PetscFunctionReturn(0);
1632a2b30743SBarry Smith   }
1633a2b30743SBarry Smith 
16344e980039SJed Brown   if (its > 1 || ~flag & SOR_ZERO_INITIAL_GUESS || flag & SOR_EISENSTAT) {
16354e980039SJed Brown     ierr = VecDuplicate(bb,&bb1);CHKERRQ(ierr);
16364e980039SJed Brown   }
16374e980039SJed Brown 
1638c16cb8f2SBarry Smith   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP) {
1639da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
164041f059aeSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
16412798e883SHong Zhang       its--;
1642da3a660dSBarry Smith     }
16432798e883SHong Zhang 
16442798e883SHong Zhang     while (its--) {
1645ca9f406cSSatish Balay       ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1646ca9f406cSSatish Balay       ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
16472798e883SHong Zhang 
1648c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
1649efb30889SBarry Smith       ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr);
1650c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
16512798e883SHong Zhang 
1652c14dc6b6SHong Zhang       /* local sweep */
165341f059aeSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr);
16542798e883SHong Zhang     }
16553a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_FORWARD_SWEEP) {
1656da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
165741f059aeSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
16582798e883SHong Zhang       its--;
1659da3a660dSBarry Smith     }
16602798e883SHong Zhang     while (its--) {
1661ca9f406cSSatish Balay       ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1662ca9f406cSSatish Balay       ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
16632798e883SHong Zhang 
1664c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
1665efb30889SBarry Smith       ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr);
1666c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
1667c14dc6b6SHong Zhang 
1668c14dc6b6SHong Zhang       /* local sweep */
166941f059aeSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr);
16702798e883SHong Zhang     }
16713a40ed3dSBarry Smith   } else if (flag & SOR_LOCAL_BACKWARD_SWEEP) {
1672da3a660dSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
167341f059aeSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
16742798e883SHong Zhang       its--;
1675da3a660dSBarry Smith     }
16762798e883SHong Zhang     while (its--) {
1677ca9f406cSSatish Balay       ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1678ca9f406cSSatish Balay       ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
16792798e883SHong Zhang 
1680c14dc6b6SHong Zhang       /* update rhs: bb1 = bb - B*x */
1681efb30889SBarry Smith       ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr);
1682c14dc6b6SHong Zhang       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
16832798e883SHong Zhang 
1684c14dc6b6SHong Zhang       /* local sweep */
168541f059aeSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr);
16862798e883SHong Zhang     }
1687a7420bb7SBarry Smith   } else if (flag & SOR_EISENSTAT) {
1688a7420bb7SBarry Smith     Vec         xx1;
1689a7420bb7SBarry Smith 
1690a7420bb7SBarry Smith     ierr = VecDuplicate(bb,&xx1);CHKERRQ(ierr);
169141f059aeSBarry 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);
1692a7420bb7SBarry Smith 
1693a7420bb7SBarry Smith     ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1694a7420bb7SBarry Smith     ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1695a7420bb7SBarry Smith     if (!mat->diag) {
1696a7420bb7SBarry Smith       ierr = MatGetVecs(matin,&mat->diag,PETSC_NULL);CHKERRQ(ierr);
1697a7420bb7SBarry Smith       ierr = MatGetDiagonal(matin,mat->diag);CHKERRQ(ierr);
1698a7420bb7SBarry Smith     }
1699bd0c2dcbSBarry Smith     ierr = MatHasOperation(matin,MATOP_MULT_DIAGONAL_BLOCK,&hasop);CHKERRQ(ierr);
1700bd0c2dcbSBarry Smith     if (hasop) {
1701bd0c2dcbSBarry Smith       ierr = MatMultDiagonalBlock(matin,xx,bb1);CHKERRQ(ierr);
1702bd0c2dcbSBarry Smith     } else {
1703a7420bb7SBarry Smith       ierr = VecPointwiseMult(bb1,mat->diag,xx);CHKERRQ(ierr);
1704bd0c2dcbSBarry Smith     }
1705887ee2caSBarry Smith     ierr = VecAYPX(bb1,(omega-2.0)/omega,bb);CHKERRQ(ierr);
1706887ee2caSBarry Smith 
1707a7420bb7SBarry Smith     ierr = MatMultAdd(mat->B,mat->lvec,bb1,bb1);CHKERRQ(ierr);
1708a7420bb7SBarry Smith 
1709a7420bb7SBarry Smith     /* local sweep */
171041f059aeSBarry 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);
1711a7420bb7SBarry Smith     ierr = VecAXPY(xx,1.0,xx1);CHKERRQ(ierr);
17126bf464f9SBarry Smith     ierr = VecDestroy(&xx1);CHKERRQ(ierr);
171344b1af1bSBarry Smith   } else SETERRQ(((PetscObject)matin)->comm,PETSC_ERR_SUP,"Parallel SOR not supported");
1714c14dc6b6SHong Zhang 
17156bf464f9SBarry Smith   ierr = VecDestroy(&bb1);CHKERRQ(ierr);
17163a40ed3dSBarry Smith   PetscFunctionReturn(0);
17178a729477SBarry Smith }
1718a66be287SLois Curfman McInnes 
17194a2ae208SSatish Balay #undef __FUNCT__
172042e855d1Svictor #define __FUNCT__ "MatPermute_MPIAIJ"
172142e855d1Svictor PetscErrorCode MatPermute_MPIAIJ(Mat A,IS rowp,IS colp,Mat *B)
172242e855d1Svictor {
172372e6a0cfSJed Brown   Mat            aA,aB,Aperm;
172472e6a0cfSJed Brown   const PetscInt *rwant,*cwant,*gcols,*ai,*bi,*aj,*bj;
172572e6a0cfSJed Brown   PetscScalar    *aa,*ba;
172672e6a0cfSJed Brown   PetscInt       i,j,m,n,ng,anz,bnz,*dnnz,*onnz,*tdnnz,*tonnz,*rdest,*cdest,*work,*gcdest;
172772e6a0cfSJed Brown   PetscSF        rowsf,sf;
172872e6a0cfSJed Brown   IS             parcolp = PETSC_NULL;
172972e6a0cfSJed Brown   PetscBool      done;
173042e855d1Svictor   PetscErrorCode ierr;
173142e855d1Svictor 
173242e855d1Svictor   PetscFunctionBegin;
173372e6a0cfSJed Brown   ierr = MatGetLocalSize(A,&m,&n);CHKERRQ(ierr);
173472e6a0cfSJed Brown   ierr = ISGetIndices(rowp,&rwant);CHKERRQ(ierr);
173572e6a0cfSJed Brown   ierr = ISGetIndices(colp,&cwant);CHKERRQ(ierr);
173672e6a0cfSJed Brown   ierr = PetscMalloc3(PetscMax(m,n),PetscInt,&work,m,PetscInt,&rdest,n,PetscInt,&cdest);CHKERRQ(ierr);
173772e6a0cfSJed Brown 
173872e6a0cfSJed Brown   /* Invert row permutation to find out where my rows should go */
173972e6a0cfSJed Brown   ierr = PetscSFCreate(((PetscObject)A)->comm,&rowsf);CHKERRQ(ierr);
174072e6a0cfSJed Brown   ierr = PetscSFSetGraphLayout(rowsf,A->rmap,A->rmap->n,PETSC_NULL,PETSC_OWN_POINTER,rwant);CHKERRQ(ierr);
1741e9e74f11SJed Brown   ierr = PetscSFSetFromOptions(rowsf);CHKERRQ(ierr);
174272e6a0cfSJed Brown   for (i=0; i<m; i++) work[i] = A->rmap->rstart + i;
174372e6a0cfSJed Brown   ierr = PetscSFReduceBegin(rowsf,MPIU_INT,work,rdest,MPI_REPLACE);CHKERRQ(ierr);
174472e6a0cfSJed Brown   ierr = PetscSFReduceEnd(rowsf,MPIU_INT,work,rdest,MPI_REPLACE);CHKERRQ(ierr);
174572e6a0cfSJed Brown 
174672e6a0cfSJed Brown   /* Invert column permutation to find out where my columns should go */
174772e6a0cfSJed Brown   ierr = PetscSFCreate(((PetscObject)A)->comm,&sf);CHKERRQ(ierr);
174872e6a0cfSJed Brown   ierr = PetscSFSetGraphLayout(sf,A->cmap,A->cmap->n,PETSC_NULL,PETSC_OWN_POINTER,cwant);CHKERRQ(ierr);
1749e9e74f11SJed Brown   ierr = PetscSFSetFromOptions(sf);CHKERRQ(ierr);
175072e6a0cfSJed Brown   for (i=0; i<n; i++) work[i] = A->cmap->rstart + i;
175172e6a0cfSJed Brown   ierr = PetscSFReduceBegin(sf,MPIU_INT,work,cdest,MPI_REPLACE);CHKERRQ(ierr);
175272e6a0cfSJed Brown   ierr = PetscSFReduceEnd(sf,MPIU_INT,work,cdest,MPI_REPLACE);CHKERRQ(ierr);
175372e6a0cfSJed Brown   ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
175472e6a0cfSJed Brown 
175572e6a0cfSJed Brown   ierr = ISRestoreIndices(rowp,&rwant);CHKERRQ(ierr);
175672e6a0cfSJed Brown   ierr = ISRestoreIndices(colp,&cwant);CHKERRQ(ierr);
175772e6a0cfSJed Brown   ierr = MatMPIAIJGetSeqAIJ(A,&aA,&aB,&gcols);CHKERRQ(ierr);
175872e6a0cfSJed Brown 
175972e6a0cfSJed Brown   /* Find out where my gcols should go */
176072e6a0cfSJed Brown   ierr = MatGetSize(aB,PETSC_NULL,&ng);CHKERRQ(ierr);
176172e6a0cfSJed Brown   ierr = PetscMalloc(ng*sizeof(PetscInt),&gcdest);CHKERRQ(ierr);
176272e6a0cfSJed Brown   ierr = PetscSFCreate(((PetscObject)A)->comm,&sf);CHKERRQ(ierr);
176372e6a0cfSJed Brown   ierr = PetscSFSetGraphLayout(sf,A->cmap,ng,PETSC_NULL,PETSC_OWN_POINTER,gcols);CHKERRQ(ierr);
1764e9e74f11SJed Brown   ierr = PetscSFSetFromOptions(sf);CHKERRQ(ierr);
176572e6a0cfSJed Brown   ierr = PetscSFBcastBegin(sf,MPIU_INT,cdest,gcdest);CHKERRQ(ierr);
176672e6a0cfSJed Brown   ierr = PetscSFBcastEnd(sf,MPIU_INT,cdest,gcdest);CHKERRQ(ierr);
176772e6a0cfSJed Brown   ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
176872e6a0cfSJed Brown 
176972e6a0cfSJed Brown   ierr = PetscMalloc4(m,PetscInt,&dnnz,m,PetscInt,&onnz,m,PetscInt,&tdnnz,m,PetscInt,&tonnz);CHKERRQ(ierr);
177072e6a0cfSJed Brown   ierr = PetscMemzero(dnnz,m*sizeof(PetscInt));CHKERRQ(ierr);
177172e6a0cfSJed Brown   ierr = PetscMemzero(onnz,m*sizeof(PetscInt));CHKERRQ(ierr);
177272e6a0cfSJed Brown   ierr = MatGetRowIJ(aA,0,PETSC_FALSE,PETSC_FALSE,&anz,&ai,&aj,&done);CHKERRQ(ierr);
177372e6a0cfSJed Brown   ierr = MatGetRowIJ(aB,0,PETSC_FALSE,PETSC_FALSE,&bnz,&bi,&bj,&done);CHKERRQ(ierr);
177472e6a0cfSJed Brown   for (i=0; i<m; i++) {
177572e6a0cfSJed Brown     PetscInt row = rdest[i],rowner;
177672e6a0cfSJed Brown     ierr = PetscLayoutFindOwner(A->rmap,row,&rowner);CHKERRQ(ierr);
177772e6a0cfSJed Brown     for (j=ai[i]; j<ai[i+1]; j++) {
177872e6a0cfSJed Brown       PetscInt cowner,col = cdest[aj[j]];
177972e6a0cfSJed Brown       ierr = PetscLayoutFindOwner(A->cmap,col,&cowner);CHKERRQ(ierr); /* Could build an index for the columns to eliminate this search */
178072e6a0cfSJed Brown       if (rowner == cowner) dnnz[i]++;
178172e6a0cfSJed Brown       else onnz[i]++;
178272e6a0cfSJed Brown     }
178372e6a0cfSJed Brown     for (j=bi[i]; j<bi[i+1]; j++) {
178472e6a0cfSJed Brown       PetscInt cowner,col = gcdest[bj[j]];
178572e6a0cfSJed Brown       ierr = PetscLayoutFindOwner(A->cmap,col,&cowner);CHKERRQ(ierr);
178672e6a0cfSJed Brown       if (rowner == cowner) dnnz[i]++;
178772e6a0cfSJed Brown       else onnz[i]++;
178872e6a0cfSJed Brown     }
178972e6a0cfSJed Brown   }
179072e6a0cfSJed Brown   ierr = PetscMemzero(tdnnz,m*sizeof(PetscInt));CHKERRQ(ierr);
179172e6a0cfSJed Brown   ierr = PetscMemzero(tonnz,m*sizeof(PetscInt));CHKERRQ(ierr);
179272e6a0cfSJed Brown   ierr = PetscSFBcastBegin(rowsf,MPIU_INT,dnnz,tdnnz);CHKERRQ(ierr);
179372e6a0cfSJed Brown   ierr = PetscSFBcastEnd(rowsf,MPIU_INT,dnnz,tdnnz);CHKERRQ(ierr);
179472e6a0cfSJed Brown   ierr = PetscSFBcastBegin(rowsf,MPIU_INT,onnz,tonnz);CHKERRQ(ierr);
179572e6a0cfSJed Brown   ierr = PetscSFBcastEnd(rowsf,MPIU_INT,onnz,tonnz);CHKERRQ(ierr);
179672e6a0cfSJed Brown   ierr = PetscSFDestroy(&rowsf);CHKERRQ(ierr);
179772e6a0cfSJed Brown 
1798568b9367SJed Brown   ierr = MatCreateAIJ(((PetscObject)A)->comm,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N,0,tdnnz,0,tonnz,&Aperm);CHKERRQ(ierr);
179972e6a0cfSJed Brown   ierr = MatSeqAIJGetArray(aA,&aa);CHKERRQ(ierr);
180072e6a0cfSJed Brown   ierr = MatSeqAIJGetArray(aB,&ba);CHKERRQ(ierr);
180172e6a0cfSJed Brown   for (i=0; i<m; i++) {
180272e6a0cfSJed Brown     PetscInt *acols = dnnz,*bcols = onnz; /* Repurpose now-unneeded arrays */
180372e6a0cfSJed Brown     PetscInt rowlen;
180472e6a0cfSJed Brown     rowlen = ai[i+1] - ai[i];
180572e6a0cfSJed Brown     for (j=0; j<rowlen; j++) acols[j] = cdest[aj[ai[i]+j]];
180672e6a0cfSJed Brown     ierr = MatSetValues(Aperm,1,&rdest[i],rowlen,acols,aa+ai[i],INSERT_VALUES);CHKERRQ(ierr);
180772e6a0cfSJed Brown     rowlen = bi[i+1] - bi[i];
180872e6a0cfSJed Brown     for (j=0; j<rowlen; j++) bcols[j] = gcdest[bj[bi[i]+j]];
180972e6a0cfSJed Brown     ierr = MatSetValues(Aperm,1,&rdest[i],rowlen,bcols,ba+bi[i],INSERT_VALUES);CHKERRQ(ierr);
181072e6a0cfSJed Brown   }
181172e6a0cfSJed Brown   ierr = MatAssemblyBegin(Aperm,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
181272e6a0cfSJed Brown   ierr = MatAssemblyEnd(Aperm,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
181372e6a0cfSJed Brown   ierr = MatRestoreRowIJ(aA,0,PETSC_FALSE,PETSC_FALSE,&anz,&ai,&aj,&done);CHKERRQ(ierr);
181472e6a0cfSJed Brown   ierr = MatRestoreRowIJ(aB,0,PETSC_FALSE,PETSC_FALSE,&bnz,&bi,&bj,&done);CHKERRQ(ierr);
181572e6a0cfSJed Brown   ierr = MatSeqAIJRestoreArray(aA,&aa);CHKERRQ(ierr);
181672e6a0cfSJed Brown   ierr = MatSeqAIJRestoreArray(aB,&ba);CHKERRQ(ierr);
181772e6a0cfSJed Brown   ierr = PetscFree4(dnnz,onnz,tdnnz,tonnz);CHKERRQ(ierr);
181872e6a0cfSJed Brown   ierr = PetscFree3(work,rdest,cdest);CHKERRQ(ierr);
181972e6a0cfSJed Brown   ierr = PetscFree(gcdest);CHKERRQ(ierr);
182072e6a0cfSJed Brown   if (parcolp) {ierr = ISDestroy(&colp);CHKERRQ(ierr);}
182172e6a0cfSJed Brown   *B = Aperm;
182242e855d1Svictor   PetscFunctionReturn(0);
182342e855d1Svictor }
182442e855d1Svictor 
182542e855d1Svictor #undef __FUNCT__
18264a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_MPIAIJ"
1827dfbe8321SBarry Smith PetscErrorCode MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info)
1828a66be287SLois Curfman McInnes {
1829a66be287SLois Curfman McInnes   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)matin->data;
1830a66be287SLois Curfman McInnes   Mat            A = mat->A,B = mat->B;
1831dfbe8321SBarry Smith   PetscErrorCode ierr;
1832329f5518SBarry Smith   PetscReal      isend[5],irecv[5];
1833a66be287SLois Curfman McInnes 
18343a40ed3dSBarry Smith   PetscFunctionBegin;
18354e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
18364e220ebcSLois Curfman McInnes   ierr = MatGetInfo(A,MAT_LOCAL,info);CHKERRQ(ierr);
18374e220ebcSLois Curfman McInnes   isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
18384e220ebcSLois Curfman McInnes   isend[3] = info->memory;  isend[4] = info->mallocs;
18394e220ebcSLois Curfman McInnes   ierr = MatGetInfo(B,MAT_LOCAL,info);CHKERRQ(ierr);
18404e220ebcSLois Curfman McInnes   isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
18414e220ebcSLois Curfman McInnes   isend[3] += info->memory;  isend[4] += info->mallocs;
1842a66be287SLois Curfman McInnes   if (flag == MAT_LOCAL) {
18434e220ebcSLois Curfman McInnes     info->nz_used      = isend[0];
18444e220ebcSLois Curfman McInnes     info->nz_allocated = isend[1];
18454e220ebcSLois Curfman McInnes     info->nz_unneeded  = isend[2];
18464e220ebcSLois Curfman McInnes     info->memory       = isend[3];
18474e220ebcSLois Curfman McInnes     info->mallocs      = isend[4];
1848a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_MAX) {
1849d9822059SBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_MAX,((PetscObject)matin)->comm);CHKERRQ(ierr);
18504e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
18514e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
18524e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
18534e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
18544e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1855a66be287SLois Curfman McInnes   } else if (flag == MAT_GLOBAL_SUM) {
1856d9822059SBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_SUM,((PetscObject)matin)->comm);CHKERRQ(ierr);
18574e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
18584e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
18594e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
18604e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
18614e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1862a66be287SLois Curfman McInnes   }
18634e220ebcSLois Curfman McInnes   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
18644e220ebcSLois Curfman McInnes   info->fill_ratio_needed = 0;
18654e220ebcSLois Curfman McInnes   info->factor_mallocs    = 0;
18663a40ed3dSBarry Smith   PetscFunctionReturn(0);
1867a66be287SLois Curfman McInnes }
1868a66be287SLois Curfman McInnes 
18694a2ae208SSatish Balay #undef __FUNCT__
18704a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_MPIAIJ"
1871ace3abfcSBarry Smith PetscErrorCode MatSetOption_MPIAIJ(Mat A,MatOption op,PetscBool  flg)
1872c74985f6SBarry Smith {
1873c0bbcb79SLois Curfman McInnes   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
1874dfbe8321SBarry Smith   PetscErrorCode ierr;
1875c74985f6SBarry Smith 
18763a40ed3dSBarry Smith   PetscFunctionBegin;
187712c028f9SKris Buschelman   switch (op) {
1878512a5fc5SBarry Smith   case MAT_NEW_NONZERO_LOCATIONS:
187912c028f9SKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
188028b2fa4aSMatthew Knepley   case MAT_UNUSED_NONZERO_LOCATION_ERR:
1881a9817697SBarry Smith   case MAT_KEEP_NONZERO_PATTERN:
188212c028f9SKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
188312c028f9SKris Buschelman   case MAT_USE_INODES:
188412c028f9SKris Buschelman   case MAT_IGNORE_ZERO_ENTRIES:
1885fa1f0d2cSMatthew G Knepley     MatCheckPreallocated(A,1);
18864e0d8c25SBarry Smith     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
18874e0d8c25SBarry Smith     ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr);
188812c028f9SKris Buschelman     break;
188912c028f9SKris Buschelman   case MAT_ROW_ORIENTED:
18904e0d8c25SBarry Smith     a->roworiented = flg;
18914e0d8c25SBarry Smith     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
18924e0d8c25SBarry Smith     ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr);
189312c028f9SKris Buschelman     break;
18944e0d8c25SBarry Smith   case MAT_NEW_DIAGONALS:
1895290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
189612c028f9SKris Buschelman     break;
189712c028f9SKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
18985c0f0b64SBarry Smith     a->donotstash = flg;
189912c028f9SKris Buschelman     break;
1900ffa07934SHong Zhang   case MAT_SPD:
1901ffa07934SHong Zhang     A->spd_set                         = PETSC_TRUE;
1902ffa07934SHong Zhang     A->spd                             = flg;
1903ffa07934SHong Zhang     if (flg) {
1904ffa07934SHong Zhang       A->symmetric                     = PETSC_TRUE;
1905ffa07934SHong Zhang       A->structurally_symmetric        = PETSC_TRUE;
1906ffa07934SHong Zhang       A->symmetric_set                 = PETSC_TRUE;
1907ffa07934SHong Zhang       A->structurally_symmetric_set    = PETSC_TRUE;
1908ffa07934SHong Zhang     }
1909ffa07934SHong Zhang     break;
191077e54ba9SKris Buschelman   case MAT_SYMMETRIC:
19114e0d8c25SBarry Smith     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
191225f421beSHong Zhang     break;
191377e54ba9SKris Buschelman   case MAT_STRUCTURALLY_SYMMETRIC:
1914eeffb40dSHong Zhang     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
1915eeffb40dSHong Zhang     break;
1916bf108f30SBarry Smith   case MAT_HERMITIAN:
1917eeffb40dSHong Zhang     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
1918eeffb40dSHong Zhang     break;
1919bf108f30SBarry Smith   case MAT_SYMMETRY_ETERNAL:
19204e0d8c25SBarry Smith     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
192177e54ba9SKris Buschelman     break;
192212c028f9SKris Buschelman   default:
1923e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
19243a40ed3dSBarry Smith   }
19253a40ed3dSBarry Smith   PetscFunctionReturn(0);
1926c74985f6SBarry Smith }
1927c74985f6SBarry Smith 
19284a2ae208SSatish Balay #undef __FUNCT__
19294a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_MPIAIJ"
1930b1d57f15SBarry Smith PetscErrorCode MatGetRow_MPIAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
193139e00950SLois Curfman McInnes {
1932154123eaSLois Curfman McInnes   Mat_MPIAIJ     *mat = (Mat_MPIAIJ*)matin->data;
193387828ca2SBarry Smith   PetscScalar    *vworkA,*vworkB,**pvA,**pvB,*v_p;
19346849ba73SBarry Smith   PetscErrorCode ierr;
1935d0f46423SBarry Smith   PetscInt       i,*cworkA,*cworkB,**pcA,**pcB,cstart = matin->cmap->rstart;
1936d0f46423SBarry Smith   PetscInt       nztot,nzA,nzB,lrow,rstart = matin->rmap->rstart,rend = matin->rmap->rend;
1937b1d57f15SBarry Smith   PetscInt       *cmap,*idx_p;
193839e00950SLois Curfman McInnes 
19393a40ed3dSBarry Smith   PetscFunctionBegin;
1940e32f2f54SBarry Smith   if (mat->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Already active");
19417a0afa10SBarry Smith   mat->getrowactive = PETSC_TRUE;
19427a0afa10SBarry Smith 
194370f0671dSBarry Smith   if (!mat->rowvalues && (idx || v)) {
19447a0afa10SBarry Smith     /*
19457a0afa10SBarry Smith         allocate enough space to hold information from the longest row.
19467a0afa10SBarry Smith     */
19477a0afa10SBarry Smith     Mat_SeqAIJ *Aa = (Mat_SeqAIJ*)mat->A->data,*Ba = (Mat_SeqAIJ*)mat->B->data;
1948b1d57f15SBarry Smith     PetscInt   max = 1,tmp;
1949d0f46423SBarry Smith     for (i=0; i<matin->rmap->n; i++) {
19507a0afa10SBarry Smith       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
19517a0afa10SBarry Smith       if (max < tmp) { max = tmp; }
19527a0afa10SBarry Smith     }
19531d79065fSBarry Smith     ierr = PetscMalloc2(max,PetscScalar,&mat->rowvalues,max,PetscInt,&mat->rowindices);CHKERRQ(ierr);
19547a0afa10SBarry Smith   }
19557a0afa10SBarry Smith 
1956e7e72b3dSBarry Smith   if (row < rstart || row >= rend) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only local rows");
1957abc0e9e4SLois Curfman McInnes   lrow = row - rstart;
195839e00950SLois Curfman McInnes 
1959154123eaSLois Curfman McInnes   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1960154123eaSLois Curfman McInnes   if (!v)   {pvA = 0; pvB = 0;}
1961154123eaSLois Curfman McInnes   if (!idx) {pcA = 0; if (!v) pcB = 0;}
1962f830108cSBarry Smith   ierr = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1963f830108cSBarry Smith   ierr = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
1964154123eaSLois Curfman McInnes   nztot = nzA + nzB;
1965154123eaSLois Curfman McInnes 
196670f0671dSBarry Smith   cmap  = mat->garray;
1967154123eaSLois Curfman McInnes   if (v  || idx) {
1968154123eaSLois Curfman McInnes     if (nztot) {
1969154123eaSLois Curfman McInnes       /* Sort by increasing column numbers, assuming A and B already sorted */
1970b1d57f15SBarry Smith       PetscInt imark = -1;
1971154123eaSLois Curfman McInnes       if (v) {
197270f0671dSBarry Smith         *v = v_p = mat->rowvalues;
197339e00950SLois Curfman McInnes         for (i=0; i<nzB; i++) {
197470f0671dSBarry Smith           if (cmap[cworkB[i]] < cstart)   v_p[i] = vworkB[i];
1975154123eaSLois Curfman McInnes           else break;
1976154123eaSLois Curfman McInnes         }
1977154123eaSLois Curfman McInnes         imark = i;
197870f0671dSBarry Smith         for (i=0; i<nzA; i++)     v_p[imark+i] = vworkA[i];
197970f0671dSBarry Smith         for (i=imark; i<nzB; i++) v_p[nzA+i]   = vworkB[i];
1980154123eaSLois Curfman McInnes       }
1981154123eaSLois Curfman McInnes       if (idx) {
198270f0671dSBarry Smith         *idx = idx_p = mat->rowindices;
198370f0671dSBarry Smith         if (imark > -1) {
198470f0671dSBarry Smith           for (i=0; i<imark; i++) {
198570f0671dSBarry Smith             idx_p[i] = cmap[cworkB[i]];
198670f0671dSBarry Smith           }
198770f0671dSBarry Smith         } else {
1988154123eaSLois Curfman McInnes           for (i=0; i<nzB; i++) {
198970f0671dSBarry Smith             if (cmap[cworkB[i]] < cstart)   idx_p[i] = cmap[cworkB[i]];
1990154123eaSLois Curfman McInnes             else break;
1991154123eaSLois Curfman McInnes           }
1992154123eaSLois Curfman McInnes           imark = i;
199370f0671dSBarry Smith         }
199470f0671dSBarry Smith         for (i=0; i<nzA; i++)     idx_p[imark+i] = cstart + cworkA[i];
199570f0671dSBarry Smith         for (i=imark; i<nzB; i++) idx_p[nzA+i]   = cmap[cworkB[i]];
199639e00950SLois Curfman McInnes       }
19973f97c4b0SBarry Smith     } else {
19981ca473b0SSatish Balay       if (idx) *idx = 0;
19991ca473b0SSatish Balay       if (v)   *v   = 0;
20001ca473b0SSatish Balay     }
2001154123eaSLois Curfman McInnes   }
200239e00950SLois Curfman McInnes   *nz = nztot;
2003f830108cSBarry Smith   ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
2004f830108cSBarry Smith   ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
20053a40ed3dSBarry Smith   PetscFunctionReturn(0);
200639e00950SLois Curfman McInnes }
200739e00950SLois Curfman McInnes 
20084a2ae208SSatish Balay #undef __FUNCT__
20094a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_MPIAIJ"
2010b1d57f15SBarry Smith PetscErrorCode MatRestoreRow_MPIAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
201139e00950SLois Curfman McInnes {
20127a0afa10SBarry Smith   Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
20133a40ed3dSBarry Smith 
20143a40ed3dSBarry Smith   PetscFunctionBegin;
2015e7e72b3dSBarry Smith   if (!aij->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"MatGetRow() must be called first");
20167a0afa10SBarry Smith   aij->getrowactive = PETSC_FALSE;
20173a40ed3dSBarry Smith   PetscFunctionReturn(0);
201839e00950SLois Curfman McInnes }
201939e00950SLois Curfman McInnes 
20204a2ae208SSatish Balay #undef __FUNCT__
20214a2ae208SSatish Balay #define __FUNCT__ "MatNorm_MPIAIJ"
2022dfbe8321SBarry Smith PetscErrorCode MatNorm_MPIAIJ(Mat mat,NormType type,PetscReal *norm)
2023855ac2c5SLois Curfman McInnes {
2024855ac2c5SLois Curfman McInnes   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
2025ec8511deSBarry Smith   Mat_SeqAIJ     *amat = (Mat_SeqAIJ*)aij->A->data,*bmat = (Mat_SeqAIJ*)aij->B->data;
2026dfbe8321SBarry Smith   PetscErrorCode ierr;
2027d0f46423SBarry Smith   PetscInt       i,j,cstart = mat->cmap->rstart;
2028329f5518SBarry Smith   PetscReal      sum = 0.0;
2029a77337e4SBarry Smith   MatScalar      *v;
203004ca555eSLois Curfman McInnes 
20313a40ed3dSBarry Smith   PetscFunctionBegin;
203217699dbbSLois Curfman McInnes   if (aij->size == 1) {
203314183eadSLois Curfman McInnes     ierr =  MatNorm(aij->A,type,norm);CHKERRQ(ierr);
203437fa93a5SLois Curfman McInnes   } else {
203504ca555eSLois Curfman McInnes     if (type == NORM_FROBENIUS) {
203604ca555eSLois Curfman McInnes       v = amat->a;
203704ca555eSLois Curfman McInnes       for (i=0; i<amat->nz; i++) {
2038329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
203904ca555eSLois Curfman McInnes       }
204004ca555eSLois Curfman McInnes       v = bmat->a;
204104ca555eSLois Curfman McInnes       for (i=0; i<bmat->nz; i++) {
2042329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
204304ca555eSLois Curfman McInnes       }
2044d9822059SBarry Smith       ierr = MPI_Allreduce(&sum,norm,1,MPIU_REAL,MPIU_SUM,((PetscObject)mat)->comm);CHKERRQ(ierr);
20458f1a2a5eSBarry Smith       *norm = PetscSqrtReal(*norm);
20463a40ed3dSBarry Smith     } else if (type == NORM_1) { /* max column norm */
2047329f5518SBarry Smith       PetscReal *tmp,*tmp2;
2048b1d57f15SBarry Smith       PetscInt  *jj,*garray = aij->garray;
2049d0f46423SBarry Smith       ierr = PetscMalloc((mat->cmap->N+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
2050d0f46423SBarry Smith       ierr = PetscMalloc((mat->cmap->N+1)*sizeof(PetscReal),&tmp2);CHKERRQ(ierr);
2051d0f46423SBarry Smith       ierr = PetscMemzero(tmp,mat->cmap->N*sizeof(PetscReal));CHKERRQ(ierr);
205204ca555eSLois Curfman McInnes       *norm = 0.0;
205304ca555eSLois Curfman McInnes       v = amat->a; jj = amat->j;
205404ca555eSLois Curfman McInnes       for (j=0; j<amat->nz; j++) {
2055bfec09a0SHong Zhang         tmp[cstart + *jj++ ] += PetscAbsScalar(*v);  v++;
205604ca555eSLois Curfman McInnes       }
205704ca555eSLois Curfman McInnes       v = bmat->a; jj = bmat->j;
205804ca555eSLois Curfman McInnes       for (j=0; j<bmat->nz; j++) {
2059bfec09a0SHong Zhang         tmp[garray[*jj++]] += PetscAbsScalar(*v); v++;
206004ca555eSLois Curfman McInnes       }
2061d9822059SBarry Smith       ierr = MPI_Allreduce(tmp,tmp2,mat->cmap->N,MPIU_REAL,MPIU_SUM,((PetscObject)mat)->comm);CHKERRQ(ierr);
2062d0f46423SBarry Smith       for (j=0; j<mat->cmap->N; j++) {
206304ca555eSLois Curfman McInnes         if (tmp2[j] > *norm) *norm = tmp2[j];
206404ca555eSLois Curfman McInnes       }
2065606d414cSSatish Balay       ierr = PetscFree(tmp);CHKERRQ(ierr);
2066606d414cSSatish Balay       ierr = PetscFree(tmp2);CHKERRQ(ierr);
20673a40ed3dSBarry Smith     } else if (type == NORM_INFINITY) { /* max row norm */
2068329f5518SBarry Smith       PetscReal ntemp = 0.0;
2069d0f46423SBarry Smith       for (j=0; j<aij->A->rmap->n; j++) {
2070bfec09a0SHong Zhang         v = amat->a + amat->i[j];
207104ca555eSLois Curfman McInnes         sum = 0.0;
207204ca555eSLois Curfman McInnes         for (i=0; i<amat->i[j+1]-amat->i[j]; i++) {
2073cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
207404ca555eSLois Curfman McInnes         }
2075bfec09a0SHong Zhang         v = bmat->a + bmat->i[j];
207604ca555eSLois Curfman McInnes         for (i=0; i<bmat->i[j+1]-bmat->i[j]; i++) {
2077cddf8d76SBarry Smith           sum += PetscAbsScalar(*v); v++;
207804ca555eSLois Curfman McInnes         }
2079515d9167SLois Curfman McInnes         if (sum > ntemp) ntemp = sum;
208004ca555eSLois Curfman McInnes       }
2081d9822059SBarry Smith       ierr = MPI_Allreduce(&ntemp,norm,1,MPIU_REAL,MPIU_MAX,((PetscObject)mat)->comm);CHKERRQ(ierr);
2082ca161407SBarry Smith     } else {
2083e7e72b3dSBarry Smith       SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_SUP,"No support for two norm");
208404ca555eSLois Curfman McInnes     }
208537fa93a5SLois Curfman McInnes   }
20863a40ed3dSBarry Smith   PetscFunctionReturn(0);
2087855ac2c5SLois Curfman McInnes }
2088855ac2c5SLois Curfman McInnes 
20894a2ae208SSatish Balay #undef __FUNCT__
20904a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_MPIAIJ"
2091fc4dec0aSBarry Smith PetscErrorCode MatTranspose_MPIAIJ(Mat A,MatReuse reuse,Mat *matout)
2092b7c46309SBarry Smith {
2093b7c46309SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
2094da668accSHong Zhang   Mat_SeqAIJ     *Aloc=(Mat_SeqAIJ*)a->A->data,*Bloc=(Mat_SeqAIJ*)a->B->data;
2095dfbe8321SBarry Smith   PetscErrorCode ierr;
209680bcc5a1SJed Brown   PetscInt       M = A->rmap->N,N = A->cmap->N,ma,na,mb,nb,*ai,*aj,*bi,*bj,row,*cols,*cols_tmp,i;
2097d0f46423SBarry Smith   PetscInt       cstart=A->cmap->rstart,ncol;
20983a40ed3dSBarry Smith   Mat            B;
2099a77337e4SBarry Smith   MatScalar      *array;
2100b7c46309SBarry Smith 
21013a40ed3dSBarry Smith   PetscFunctionBegin;
2102e7e72b3dSBarry Smith   if (reuse == MAT_REUSE_MATRIX && A == *matout && M != N) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
2103da668accSHong Zhang 
210480bcc5a1SJed Brown   ma = A->rmap->n; na = A->cmap->n; mb = a->B->rmap->n; nb = a->B->cmap->n;
2105da668accSHong Zhang   ai = Aloc->i; aj = Aloc->j;
2106da668accSHong Zhang   bi = Bloc->i; bj = Bloc->j;
2107fc73b1b3SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *matout == A) {
210880bcc5a1SJed Brown     PetscInt *d_nnz,*g_nnz,*o_nnz;
210980bcc5a1SJed Brown     PetscSFNode *oloc;
2110713c93b4SJed Brown     PETSC_UNUSED PetscSF sf;
211180bcc5a1SJed Brown 
211280bcc5a1SJed Brown     ierr = PetscMalloc4(na,PetscInt,&d_nnz,na,PetscInt,&o_nnz,nb,PetscInt,&g_nnz,nb,PetscSFNode,&oloc);CHKERRQ(ierr);
211380bcc5a1SJed Brown     /* compute d_nnz for preallocation */
211480bcc5a1SJed Brown     ierr = PetscMemzero(d_nnz,na*sizeof(PetscInt));CHKERRQ(ierr);
2115da668accSHong Zhang     for (i=0; i<ai[ma]; i++) {
2116da668accSHong Zhang       d_nnz[aj[i]] ++;
2117da668accSHong Zhang       aj[i] += cstart; /* global col index to be used by MatSetValues() */
2118d4bb536fSBarry Smith     }
211980bcc5a1SJed Brown     /* compute local off-diagonal contributions */
21200beca09bSJed Brown     ierr = PetscMemzero(g_nnz,nb*sizeof(PetscInt));CHKERRQ(ierr);
212180bcc5a1SJed Brown     for (i=0; i<bi[ma]; i++) g_nnz[bj[i]]++;
212280bcc5a1SJed Brown     /* map those to global */
212380bcc5a1SJed Brown     ierr = PetscSFCreate(((PetscObject)A)->comm,&sf);CHKERRQ(ierr);
212463f4a732SJed Brown     ierr = PetscSFSetGraphLayout(sf,A->cmap,nb,PETSC_NULL,PETSC_USE_POINTER,a->garray);CHKERRQ(ierr);
2125e9e74f11SJed Brown     ierr = PetscSFSetFromOptions(sf);CHKERRQ(ierr);
212680bcc5a1SJed Brown     ierr = PetscMemzero(o_nnz,na*sizeof(PetscInt));CHKERRQ(ierr);
212780bcc5a1SJed Brown     ierr = PetscSFReduceBegin(sf,MPIU_INT,g_nnz,o_nnz,MPIU_SUM);CHKERRQ(ierr);
212880bcc5a1SJed Brown     ierr = PetscSFReduceEnd(sf,MPIU_INT,g_nnz,o_nnz,MPIU_SUM);CHKERRQ(ierr);
212980bcc5a1SJed Brown     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
2130d4bb536fSBarry Smith 
21317adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)A)->comm,&B);CHKERRQ(ierr);
2132d0f46423SBarry Smith     ierr = MatSetSizes(B,A->cmap->n,A->rmap->n,N,M);CHKERRQ(ierr);
2133a2f3521dSMark F. Adams     ierr = MatSetBlockSizes(B,A->cmap->bs,A->rmap->bs);CHKERRQ(ierr);
21347adad957SLisandro Dalcin     ierr = MatSetType(B,((PetscObject)A)->type_name);CHKERRQ(ierr);
213580bcc5a1SJed Brown     ierr = MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);CHKERRQ(ierr);
213680bcc5a1SJed Brown     ierr = PetscFree4(d_nnz,o_nnz,g_nnz,oloc);CHKERRQ(ierr);
2137fc4dec0aSBarry Smith   } else {
2138fc4dec0aSBarry Smith     B = *matout;
21396ffab4bbSHong Zhang     ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
21406ffab4bbSHong Zhang     for (i=0; i<ai[ma]; i++) {
21416ffab4bbSHong Zhang       aj[i] += cstart; /* global col index to be used by MatSetValues() */
21426ffab4bbSHong Zhang     }
2143fc4dec0aSBarry Smith   }
2144b7c46309SBarry Smith 
2145b7c46309SBarry Smith   /* copy over the A part */
2146da668accSHong Zhang   array = Aloc->a;
2147d0f46423SBarry Smith   row = A->rmap->rstart;
2148da668accSHong Zhang   for (i=0; i<ma; i++) {
2149da668accSHong Zhang     ncol = ai[i+1]-ai[i];
2150da668accSHong Zhang     ierr = MatSetValues(B,ncol,aj,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
2151da668accSHong Zhang     row++; array += ncol; aj += ncol;
2152b7c46309SBarry Smith   }
2153b7c46309SBarry Smith   aj = Aloc->j;
2154da668accSHong Zhang   for (i=0; i<ai[ma]; i++) aj[i] -= cstart; /* resume local col index */
2155b7c46309SBarry Smith 
2156b7c46309SBarry Smith   /* copy over the B part */
2157fc73b1b3SBarry Smith   ierr = PetscMalloc(bi[mb]*sizeof(PetscInt),&cols);CHKERRQ(ierr);
2158fc73b1b3SBarry Smith   ierr = PetscMemzero(cols,bi[mb]*sizeof(PetscInt));CHKERRQ(ierr);
2159da668accSHong Zhang   array = Bloc->a;
2160d0f46423SBarry Smith   row = A->rmap->rstart;
2161da668accSHong Zhang   for (i=0; i<bi[mb]; i++) {cols[i] = a->garray[bj[i]];}
216261a2fbbaSHong Zhang   cols_tmp = cols;
2163da668accSHong Zhang   for (i=0; i<mb; i++) {
2164da668accSHong Zhang     ncol = bi[i+1]-bi[i];
216561a2fbbaSHong Zhang     ierr = MatSetValues(B,ncol,cols_tmp,1,&row,array,INSERT_VALUES);CHKERRQ(ierr);
216661a2fbbaSHong Zhang     row++; array += ncol; cols_tmp += ncol;
2167b7c46309SBarry Smith   }
2168fc73b1b3SBarry Smith   ierr = PetscFree(cols);CHKERRQ(ierr);
2169fc73b1b3SBarry Smith 
21706d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
21716d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2172815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *matout != A) {
21730de55854SLois Curfman McInnes     *matout = B;
21740de55854SLois Curfman McInnes   } else {
2175eb6b5d47SBarry Smith     ierr = MatHeaderMerge(A,B);CHKERRQ(ierr);
21760de55854SLois Curfman McInnes   }
21773a40ed3dSBarry Smith   PetscFunctionReturn(0);
2178b7c46309SBarry Smith }
2179b7c46309SBarry Smith 
21804a2ae208SSatish Balay #undef __FUNCT__
21814a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_MPIAIJ"
2182dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr)
2183a008b906SSatish Balay {
21844b967eb1SSatish Balay   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
21854b967eb1SSatish Balay   Mat            a = aij->A,b = aij->B;
2186dfbe8321SBarry Smith   PetscErrorCode ierr;
2187b1d57f15SBarry Smith   PetscInt       s1,s2,s3;
2188a008b906SSatish Balay 
21893a40ed3dSBarry Smith   PetscFunctionBegin;
21904b967eb1SSatish Balay   ierr = MatGetLocalSize(mat,&s2,&s3);CHKERRQ(ierr);
21914b967eb1SSatish Balay   if (rr) {
2192e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr);
2193e32f2f54SBarry Smith     if (s1!=s3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"right vector non-conforming local size");
21944b967eb1SSatish Balay     /* Overlap communication with computation. */
2195ca9f406cSSatish Balay     ierr = VecScatterBegin(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2196a008b906SSatish Balay   }
21974b967eb1SSatish Balay   if (ll) {
2198e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr);
2199e32f2f54SBarry Smith     if (s1!=s2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"left vector non-conforming local size");
2200f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,ll,0);CHKERRQ(ierr);
22014b967eb1SSatish Balay   }
22024b967eb1SSatish Balay   /* scale  the diagonal block */
2203f830108cSBarry Smith   ierr = (*a->ops->diagonalscale)(a,ll,rr);CHKERRQ(ierr);
22044b967eb1SSatish Balay 
22054b967eb1SSatish Balay   if (rr) {
22064b967eb1SSatish Balay     /* Do a scatter end and then right scale the off-diagonal block */
2207ca9f406cSSatish Balay     ierr = VecScatterEnd(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2208f830108cSBarry Smith     ierr = (*b->ops->diagonalscale)(b,0,aij->lvec);CHKERRQ(ierr);
22094b967eb1SSatish Balay   }
22104b967eb1SSatish Balay 
22113a40ed3dSBarry Smith   PetscFunctionReturn(0);
2212a008b906SSatish Balay }
2213a008b906SSatish Balay 
22144a2ae208SSatish Balay #undef __FUNCT__
22154a2ae208SSatish Balay #define __FUNCT__ "MatSetUnfactored_MPIAIJ"
2216dfbe8321SBarry Smith PetscErrorCode MatSetUnfactored_MPIAIJ(Mat A)
2217bb5a7306SBarry Smith {
2218bb5a7306SBarry Smith   Mat_MPIAIJ     *a   = (Mat_MPIAIJ*)A->data;
2219dfbe8321SBarry Smith   PetscErrorCode ierr;
22203a40ed3dSBarry Smith 
22213a40ed3dSBarry Smith   PetscFunctionBegin;
2222bb5a7306SBarry Smith   ierr = MatSetUnfactored(a->A);CHKERRQ(ierr);
22233a40ed3dSBarry Smith   PetscFunctionReturn(0);
2224bb5a7306SBarry Smith }
2225bb5a7306SBarry Smith 
22264a2ae208SSatish Balay #undef __FUNCT__
22274a2ae208SSatish Balay #define __FUNCT__ "MatEqual_MPIAIJ"
2228ace3abfcSBarry Smith PetscErrorCode MatEqual_MPIAIJ(Mat A,Mat B,PetscBool  *flag)
2229d4bb536fSBarry Smith {
2230d4bb536fSBarry Smith   Mat_MPIAIJ     *matB = (Mat_MPIAIJ*)B->data,*matA = (Mat_MPIAIJ*)A->data;
2231d4bb536fSBarry Smith   Mat            a,b,c,d;
2232ace3abfcSBarry Smith   PetscBool      flg;
2233dfbe8321SBarry Smith   PetscErrorCode ierr;
2234d4bb536fSBarry Smith 
22353a40ed3dSBarry Smith   PetscFunctionBegin;
2236d4bb536fSBarry Smith   a = matA->A; b = matA->B;
2237d4bb536fSBarry Smith   c = matB->A; d = matB->B;
2238d4bb536fSBarry Smith 
2239d4bb536fSBarry Smith   ierr = MatEqual(a,c,&flg);CHKERRQ(ierr);
2240abc0a331SBarry Smith   if (flg) {
2241d4bb536fSBarry Smith     ierr = MatEqual(b,d,&flg);CHKERRQ(ierr);
2242d4bb536fSBarry Smith   }
2243c3aae356SJed Brown   ierr = MPI_Allreduce(&flg,flag,1,MPIU_BOOL,MPI_LAND,((PetscObject)A)->comm);CHKERRQ(ierr);
22443a40ed3dSBarry Smith   PetscFunctionReturn(0);
2245d4bb536fSBarry Smith }
2246d4bb536fSBarry Smith 
22474a2ae208SSatish Balay #undef __FUNCT__
22484a2ae208SSatish Balay #define __FUNCT__ "MatCopy_MPIAIJ"
2249dfbe8321SBarry Smith PetscErrorCode MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str)
2250cb5b572fSBarry Smith {
2251dfbe8321SBarry Smith   PetscErrorCode ierr;
2252cb5b572fSBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ *)A->data;
2253cb5b572fSBarry Smith   Mat_MPIAIJ     *b = (Mat_MPIAIJ *)B->data;
2254cb5b572fSBarry Smith 
2255cb5b572fSBarry Smith   PetscFunctionBegin;
225633f4a19fSKris Buschelman   /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
225733f4a19fSKris Buschelman   if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) {
2258cb5b572fSBarry Smith     /* because of the column compression in the off-processor part of the matrix a->B,
2259cb5b572fSBarry Smith        the number of columns in a->B and b->B may be different, hence we cannot call
2260cb5b572fSBarry Smith        the MatCopy() directly on the two parts. If need be, we can provide a more
2261cb5b572fSBarry Smith        efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices
2262cb5b572fSBarry Smith        then copying the submatrices */
2263cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2264cb5b572fSBarry Smith   } else {
2265cb5b572fSBarry Smith     ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr);
2266cb5b572fSBarry Smith     ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr);
2267cb5b572fSBarry Smith   }
2268cb5b572fSBarry Smith   PetscFunctionReturn(0);
2269cb5b572fSBarry Smith }
2270cb5b572fSBarry Smith 
22714a2ae208SSatish Balay #undef __FUNCT__
22724994cf47SJed Brown #define __FUNCT__ "MatSetUp_MPIAIJ"
22734994cf47SJed Brown PetscErrorCode MatSetUp_MPIAIJ(Mat A)
2274273d9f13SBarry Smith {
2275dfbe8321SBarry Smith   PetscErrorCode ierr;
2276273d9f13SBarry Smith 
2277273d9f13SBarry Smith   PetscFunctionBegin;
2278273d9f13SBarry Smith   ierr =  MatMPIAIJSetPreallocation(A,PETSC_DEFAULT,0,PETSC_DEFAULT,0);CHKERRQ(ierr);
2279273d9f13SBarry Smith   PetscFunctionReturn(0);
2280273d9f13SBarry Smith }
2281273d9f13SBarry Smith 
2282ac90fabeSBarry Smith #undef __FUNCT__
228395b7e79eSJed Brown #define __FUNCT__ "MatAXPYGetPreallocation_MPIAIJ"
228495b7e79eSJed Brown /* This is the same as MatAXPYGetPreallocation_SeqAIJ, except that the local-to-global map is provided */
228595b7e79eSJed Brown static PetscErrorCode MatAXPYGetPreallocation_MPIAIJ(Mat Y,const PetscInt *yltog,Mat X,const PetscInt *xltog,PetscInt* nnz)
228695b7e79eSJed Brown {
228795b7e79eSJed Brown   PetscInt          i,m=Y->rmap->N;
228895b7e79eSJed Brown   Mat_SeqAIJ        *x = (Mat_SeqAIJ*)X->data;
228995b7e79eSJed Brown   Mat_SeqAIJ        *y = (Mat_SeqAIJ*)Y->data;
229095b7e79eSJed Brown   const PetscInt    *xi = x->i,*yi = y->i;
229195b7e79eSJed Brown 
229295b7e79eSJed Brown   PetscFunctionBegin;
229395b7e79eSJed Brown   /* Set the number of nonzeros in the new matrix */
229495b7e79eSJed Brown   for (i=0; i<m; i++) {
229595b7e79eSJed Brown     PetscInt j,k,nzx = xi[i+1] - xi[i],nzy = yi[i+1] - yi[i];
229695b7e79eSJed Brown     const PetscInt *xj = x->j+xi[i],*yj = y->j+yi[i];
229795b7e79eSJed Brown     nnz[i] = 0;
229895b7e79eSJed Brown     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
229995b7e79eSJed Brown       for (; k<nzy && yltog[yj[k]]<xltog[xj[j]]; k++) nnz[i]++; /* Catch up to X */
230095b7e79eSJed Brown       if (k<nzy && yltog[yj[k]]==xltog[xj[j]]) k++;             /* Skip duplicate */
230195b7e79eSJed Brown       nnz[i]++;
230295b7e79eSJed Brown     }
230395b7e79eSJed Brown     for (; k<nzy; k++) nnz[i]++;
230495b7e79eSJed Brown   }
230595b7e79eSJed Brown   PetscFunctionReturn(0);
230695b7e79eSJed Brown }
230795b7e79eSJed Brown 
230895b7e79eSJed Brown #undef __FUNCT__
2309ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_MPIAIJ"
2310f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_MPIAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2311ac90fabeSBarry Smith {
2312dfbe8321SBarry Smith   PetscErrorCode ierr;
2313b1d57f15SBarry Smith   PetscInt       i;
2314ac90fabeSBarry Smith   Mat_MPIAIJ     *xx = (Mat_MPIAIJ *)X->data,*yy = (Mat_MPIAIJ *)Y->data;
23154ce68768SBarry Smith   PetscBLASInt   bnz,one=1;
2316ac90fabeSBarry Smith   Mat_SeqAIJ     *x,*y;
2317ac90fabeSBarry Smith 
2318ac90fabeSBarry Smith   PetscFunctionBegin;
2319ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2320f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2321ac90fabeSBarry Smith     x    = (Mat_SeqAIJ *)xx->A->data;
2322*c5df96a5SBarry Smith     ierr = PetscBLASIntCast(x->nz,&bnz);CHKERRQ(ierr);
2323ac90fabeSBarry Smith     y    = (Mat_SeqAIJ *)yy->A->data;
2324f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
2325ac90fabeSBarry Smith     x    = (Mat_SeqAIJ *)xx->B->data;
2326ac90fabeSBarry Smith     y    = (Mat_SeqAIJ *)yy->B->data;
2327*c5df96a5SBarry Smith     ierr = PetscBLASIntCast(x->nz,&bnz);CHKERRQ(ierr);
2328f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
2329a30b2313SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) {
2330f4df32b1SMatthew Knepley     ierr = MatAXPY_SeqAIJ(yy->A,a,xx->A,str);CHKERRQ(ierr);
2331c537a176SHong Zhang 
2332c537a176SHong Zhang     x = (Mat_SeqAIJ *)xx->B->data;
2333a30b2313SHong Zhang     y = (Mat_SeqAIJ *)yy->B->data;
2334a30b2313SHong Zhang     if (y->xtoy && y->XtoY != xx->B) {
2335a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
23366bf464f9SBarry Smith       ierr = MatDestroy(&y->XtoY);CHKERRQ(ierr);
2337c537a176SHong Zhang     }
2338a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
2339d0f46423SBarry Smith       ierr = MatAXPYGetxtoy_Private(xx->B->rmap->n,x->i,x->j,xx->garray,y->i,y->j,yy->garray,&y->xtoy);CHKERRQ(ierr);
2340a30b2313SHong Zhang       y->XtoY = xx->B;
2341407f6b05SHong Zhang       ierr = PetscObjectReference((PetscObject)xx->B);CHKERRQ(ierr);
2342c537a176SHong Zhang     }
2343f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
2344ac90fabeSBarry Smith   } else {
23459f5f6813SShri Abhyankar     Mat B;
23469f5f6813SShri Abhyankar     PetscInt *nnz_d,*nnz_o;
23479f5f6813SShri Abhyankar     ierr = PetscMalloc(yy->A->rmap->N*sizeof(PetscInt),&nnz_d);CHKERRQ(ierr);
23489f5f6813SShri Abhyankar     ierr = PetscMalloc(yy->B->rmap->N*sizeof(PetscInt),&nnz_o);CHKERRQ(ierr);
23499f5f6813SShri Abhyankar     ierr = MatCreate(((PetscObject)Y)->comm,&B);CHKERRQ(ierr);
2350bc5a2726SShri Abhyankar     ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr);
23519f5f6813SShri Abhyankar     ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr);
2352a2f3521dSMark F. Adams     ierr = MatSetBlockSizes(B,Y->rmap->bs,Y->cmap->bs);CHKERRQ(ierr);
23539f5f6813SShri Abhyankar     ierr = MatSetType(B,MATMPIAIJ);CHKERRQ(ierr);
23549f5f6813SShri Abhyankar     ierr = MatAXPYGetPreallocation_SeqAIJ(yy->A,xx->A,nnz_d);CHKERRQ(ierr);
235595b7e79eSJed Brown     ierr = MatAXPYGetPreallocation_MPIAIJ(yy->B,yy->garray,xx->B,xx->garray,nnz_o);CHKERRQ(ierr);
2356ecd8bba6SJed Brown     ierr = MatMPIAIJSetPreallocation(B,0,nnz_d,0,nnz_o);CHKERRQ(ierr);
23579f5f6813SShri Abhyankar     ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr);
2358a2ea699eSBarry Smith     ierr = MatHeaderReplace(Y,B);CHKERRQ(ierr);
23599f5f6813SShri Abhyankar     ierr = PetscFree(nnz_d);CHKERRQ(ierr);
23609f5f6813SShri Abhyankar     ierr = PetscFree(nnz_o);CHKERRQ(ierr);
2361ac90fabeSBarry Smith   }
2362ac90fabeSBarry Smith   PetscFunctionReturn(0);
2363ac90fabeSBarry Smith }
2364ac90fabeSBarry Smith 
23657087cfbeSBarry Smith extern PetscErrorCode  MatConjugate_SeqAIJ(Mat);
2366354c94deSBarry Smith 
2367354c94deSBarry Smith #undef __FUNCT__
2368354c94deSBarry Smith #define __FUNCT__ "MatConjugate_MPIAIJ"
23697087cfbeSBarry Smith PetscErrorCode  MatConjugate_MPIAIJ(Mat mat)
2370354c94deSBarry Smith {
2371354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2372354c94deSBarry Smith   PetscErrorCode ierr;
2373354c94deSBarry Smith   Mat_MPIAIJ     *aij = (Mat_MPIAIJ *)mat->data;
2374354c94deSBarry Smith 
2375354c94deSBarry Smith   PetscFunctionBegin;
2376354c94deSBarry Smith   ierr = MatConjugate_SeqAIJ(aij->A);CHKERRQ(ierr);
2377354c94deSBarry Smith   ierr = MatConjugate_SeqAIJ(aij->B);CHKERRQ(ierr);
2378354c94deSBarry Smith #else
2379354c94deSBarry Smith   PetscFunctionBegin;
2380354c94deSBarry Smith #endif
2381354c94deSBarry Smith   PetscFunctionReturn(0);
2382354c94deSBarry Smith }
2383354c94deSBarry Smith 
238499cafbc1SBarry Smith #undef __FUNCT__
238599cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_MPIAIJ"
238699cafbc1SBarry Smith PetscErrorCode MatRealPart_MPIAIJ(Mat A)
238799cafbc1SBarry Smith {
238899cafbc1SBarry Smith   Mat_MPIAIJ   *a = (Mat_MPIAIJ*)A->data;
238999cafbc1SBarry Smith   PetscErrorCode ierr;
239099cafbc1SBarry Smith 
239199cafbc1SBarry Smith   PetscFunctionBegin;
239299cafbc1SBarry Smith   ierr = MatRealPart(a->A);CHKERRQ(ierr);
239399cafbc1SBarry Smith   ierr = MatRealPart(a->B);CHKERRQ(ierr);
239499cafbc1SBarry Smith   PetscFunctionReturn(0);
239599cafbc1SBarry Smith }
239699cafbc1SBarry Smith 
239799cafbc1SBarry Smith #undef __FUNCT__
239899cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_MPIAIJ"
239999cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_MPIAIJ(Mat A)
240099cafbc1SBarry Smith {
240199cafbc1SBarry Smith   Mat_MPIAIJ   *a = (Mat_MPIAIJ*)A->data;
240299cafbc1SBarry Smith   PetscErrorCode ierr;
240399cafbc1SBarry Smith 
240499cafbc1SBarry Smith   PetscFunctionBegin;
240599cafbc1SBarry Smith   ierr = MatImaginaryPart(a->A);CHKERRQ(ierr);
240699cafbc1SBarry Smith   ierr = MatImaginaryPart(a->B);CHKERRQ(ierr);
240799cafbc1SBarry Smith   PetscFunctionReturn(0);
240899cafbc1SBarry Smith }
240999cafbc1SBarry Smith 
2410519f805aSKarl Rupp #if defined(PETSC_HAVE_PBGL)
2411103bf8bdSMatthew Knepley 
2412103bf8bdSMatthew Knepley #include <boost/parallel/mpi/bsp_process_group.hpp>
2413a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_default_graph.hpp>
2414a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_0_block.hpp>
2415a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_preconditioner.hpp>
2416103bf8bdSMatthew Knepley #include <boost/graph/distributed/petsc/interface.hpp>
2417a2c909beSMatthew Knepley #include <boost/multi_array.hpp>
2418d0f46423SBarry Smith #include <boost/parallel/distributed_property_map->hpp>
2419103bf8bdSMatthew Knepley 
2420103bf8bdSMatthew Knepley #undef __FUNCT__
2421103bf8bdSMatthew Knepley #define __FUNCT__ "MatILUFactorSymbolic_MPIAIJ"
2422103bf8bdSMatthew Knepley /*
2423103bf8bdSMatthew Knepley   This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu>
2424103bf8bdSMatthew Knepley */
24250481f469SBarry Smith PetscErrorCode MatILUFactorSymbolic_MPIAIJ(Mat fact,Mat A, IS isrow, IS iscol, const MatFactorInfo *info)
2426103bf8bdSMatthew Knepley {
2427a2c909beSMatthew Knepley   namespace petsc = boost::distributed::petsc;
2428a2c909beSMatthew Knepley 
2429a2c909beSMatthew Knepley   namespace graph_dist = boost::graph::distributed;
2430a2c909beSMatthew Knepley   using boost::graph::distributed::ilu_default::process_group_type;
2431a2c909beSMatthew Knepley   using boost::graph::ilu_permuted;
2432a2c909beSMatthew Knepley 
2433ace3abfcSBarry Smith   PetscBool       row_identity, col_identity;
2434776b82aeSLisandro Dalcin   PetscContainer  c;
2435103bf8bdSMatthew Knepley   PetscInt        m, n, M, N;
2436103bf8bdSMatthew Knepley   PetscErrorCode  ierr;
2437103bf8bdSMatthew Knepley 
2438103bf8bdSMatthew Knepley   PetscFunctionBegin;
2439e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels = 0 supported for parallel ilu");
2440103bf8bdSMatthew Knepley   ierr = ISIdentity(isrow, &row_identity);CHKERRQ(ierr);
2441103bf8bdSMatthew Knepley   ierr = ISIdentity(iscol, &col_identity);CHKERRQ(ierr);
2442f23aa3ddSBarry Smith   if (!row_identity || !col_identity) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Row and column permutations must be identity for parallel ILU");
2443103bf8bdSMatthew Knepley 
2444103bf8bdSMatthew Knepley   process_group_type pg;
2445a2c909beSMatthew Knepley   typedef graph_dist::ilu_default::ilu_level_graph_type  lgraph_type;
2446a2c909beSMatthew Knepley   lgraph_type*   lgraph_p = new lgraph_type(petsc::num_global_vertices(A), pg, petsc::matrix_distribution(A, pg));
2447a2c909beSMatthew Knepley   lgraph_type&   level_graph = *lgraph_p;
2448a2c909beSMatthew Knepley   graph_dist::ilu_default::graph_type&            graph(level_graph.graph);
2449a2c909beSMatthew Knepley 
2450103bf8bdSMatthew Knepley   petsc::read_matrix(A, graph, get(boost::edge_weight, graph));
2451a2c909beSMatthew Knepley   ilu_permuted(level_graph);
2452103bf8bdSMatthew Knepley 
2453103bf8bdSMatthew Knepley   /* put together the new matrix */
24547adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)A)->comm, fact);CHKERRQ(ierr);
2455103bf8bdSMatthew Knepley   ierr = MatGetLocalSize(A, &m, &n);CHKERRQ(ierr);
2456103bf8bdSMatthew Knepley   ierr = MatGetSize(A, &M, &N);CHKERRQ(ierr);
2457719d5645SBarry Smith   ierr = MatSetSizes(fact, m, n, M, N);CHKERRQ(ierr);
2458a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(fact,A->rmap->bs,A->cmap->bs);CHKERRQ(ierr);
2459719d5645SBarry Smith   ierr = MatSetType(fact, ((PetscObject)A)->type_name);CHKERRQ(ierr);
2460719d5645SBarry Smith   ierr = MatAssemblyBegin(fact, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2461719d5645SBarry Smith   ierr = MatAssemblyEnd(fact, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2462103bf8bdSMatthew Knepley 
24637adad957SLisandro Dalcin   ierr = PetscContainerCreate(((PetscObject)A)->comm, &c);
2464776b82aeSLisandro Dalcin   ierr = PetscContainerSetPointer(c, lgraph_p);
2465719d5645SBarry Smith   ierr = PetscObjectCompose((PetscObject) (fact), "graph", (PetscObject) c);
2466bf0cc555SLisandro Dalcin   ierr = PetscContainerDestroy(&c);
2467103bf8bdSMatthew Knepley   PetscFunctionReturn(0);
2468103bf8bdSMatthew Knepley }
2469103bf8bdSMatthew Knepley 
2470103bf8bdSMatthew Knepley #undef __FUNCT__
2471103bf8bdSMatthew Knepley #define __FUNCT__ "MatLUFactorNumeric_MPIAIJ"
24720481f469SBarry Smith PetscErrorCode MatLUFactorNumeric_MPIAIJ(Mat B,Mat A, const MatFactorInfo *info)
2473103bf8bdSMatthew Knepley {
2474103bf8bdSMatthew Knepley   PetscFunctionBegin;
2475103bf8bdSMatthew Knepley   PetscFunctionReturn(0);
2476103bf8bdSMatthew Knepley }
2477103bf8bdSMatthew Knepley 
2478103bf8bdSMatthew Knepley #undef __FUNCT__
2479103bf8bdSMatthew Knepley #define __FUNCT__ "MatSolve_MPIAIJ"
2480103bf8bdSMatthew Knepley /*
2481103bf8bdSMatthew Knepley   This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu>
2482103bf8bdSMatthew Knepley */
2483103bf8bdSMatthew Knepley PetscErrorCode MatSolve_MPIAIJ(Mat A, Vec b, Vec x)
2484103bf8bdSMatthew Knepley {
2485a2c909beSMatthew Knepley   namespace graph_dist = boost::graph::distributed;
2486a2c909beSMatthew Knepley 
2487a2c909beSMatthew Knepley   typedef graph_dist::ilu_default::ilu_level_graph_type  lgraph_type;
2488a2c909beSMatthew Knepley   lgraph_type*   lgraph_p;
2489776b82aeSLisandro Dalcin   PetscContainer c;
2490103bf8bdSMatthew Knepley   PetscErrorCode ierr;
2491103bf8bdSMatthew Knepley 
2492103bf8bdSMatthew Knepley   PetscFunctionBegin;
2493103bf8bdSMatthew Knepley   ierr = PetscObjectQuery((PetscObject) A, "graph", (PetscObject *) &c);CHKERRQ(ierr);
2494776b82aeSLisandro Dalcin   ierr = PetscContainerGetPointer(c, (void **) &lgraph_p);CHKERRQ(ierr);
2495103bf8bdSMatthew Knepley   ierr = VecCopy(b, x);CHKERRQ(ierr);
2496a2c909beSMatthew Knepley 
2497a2c909beSMatthew Knepley   PetscScalar* array_x;
2498a2c909beSMatthew Knepley   ierr = VecGetArray(x, &array_x);CHKERRQ(ierr);
2499a2c909beSMatthew Knepley   PetscInt sx;
2500a2c909beSMatthew Knepley   ierr = VecGetSize(x, &sx);CHKERRQ(ierr);
2501a2c909beSMatthew Knepley 
2502a2c909beSMatthew Knepley   PetscScalar* array_b;
2503a2c909beSMatthew Knepley   ierr = VecGetArray(b, &array_b);CHKERRQ(ierr);
2504a2c909beSMatthew Knepley   PetscInt sb;
2505a2c909beSMatthew Knepley   ierr = VecGetSize(b, &sb);CHKERRQ(ierr);
2506a2c909beSMatthew Knepley 
2507a2c909beSMatthew Knepley   lgraph_type&   level_graph = *lgraph_p;
2508a2c909beSMatthew Knepley   graph_dist::ilu_default::graph_type&            graph(level_graph.graph);
2509a2c909beSMatthew Knepley 
2510a2c909beSMatthew Knepley   typedef boost::multi_array_ref<PetscScalar, 1> array_ref_type;
2511a2c909beSMatthew Knepley   array_ref_type                                 ref_b(array_b, boost::extents[num_vertices(graph)]),
2512a2c909beSMatthew Knepley                                                  ref_x(array_x, boost::extents[num_vertices(graph)]);
2513a2c909beSMatthew Knepley 
2514a2c909beSMatthew Knepley   typedef boost::iterator_property_map<array_ref_type::iterator,
2515a2c909beSMatthew Knepley                                 boost::property_map<graph_dist::ilu_default::graph_type, boost::vertex_index_t>::type>  gvector_type;
2516a2c909beSMatthew Knepley   gvector_type                                   vector_b(ref_b.begin(), get(boost::vertex_index, graph)),
2517a2c909beSMatthew Knepley                                                  vector_x(ref_x.begin(), get(boost::vertex_index, graph));
2518a2c909beSMatthew Knepley 
2519a2c909beSMatthew Knepley   ilu_set_solve(*lgraph_p, vector_b, vector_x);
2520a2c909beSMatthew Knepley 
2521103bf8bdSMatthew Knepley   PetscFunctionReturn(0);
2522103bf8bdSMatthew Knepley }
2523103bf8bdSMatthew Knepley #endif
2524103bf8bdSMatthew Knepley 
252569db28dcSHong Zhang typedef struct { /* used by MatGetRedundantMatrix() for reusing matredundant */
252669db28dcSHong Zhang   PetscInt       nzlocal,nsends,nrecvs;
25271d79065fSBarry Smith   PetscMPIInt    *send_rank,*recv_rank;
25281d79065fSBarry Smith   PetscInt       *sbuf_nz,*rbuf_nz,*sbuf_j,**rbuf_j;
252969db28dcSHong Zhang   PetscScalar    *sbuf_a,**rbuf_a;
2530bf0cc555SLisandro Dalcin   PetscErrorCode (*Destroy)(Mat);
253169db28dcSHong Zhang } Mat_Redundant;
253269db28dcSHong Zhang 
253369db28dcSHong Zhang #undef __FUNCT__
253469db28dcSHong Zhang #define __FUNCT__ "PetscContainerDestroy_MatRedundant"
253569db28dcSHong Zhang PetscErrorCode PetscContainerDestroy_MatRedundant(void *ptr)
253669db28dcSHong Zhang {
253769db28dcSHong Zhang   PetscErrorCode       ierr;
253869db28dcSHong Zhang   Mat_Redundant        *redund=(Mat_Redundant*)ptr;
253969db28dcSHong Zhang   PetscInt             i;
254069db28dcSHong Zhang 
254169db28dcSHong Zhang   PetscFunctionBegin;
25421d79065fSBarry Smith   ierr = PetscFree2(redund->send_rank,redund->recv_rank);CHKERRQ(ierr);
254369db28dcSHong Zhang   ierr = PetscFree(redund->sbuf_j);CHKERRQ(ierr);
254469db28dcSHong Zhang   ierr = PetscFree(redund->sbuf_a);CHKERRQ(ierr);
254569db28dcSHong Zhang   for (i=0; i<redund->nrecvs; i++) {
254669db28dcSHong Zhang     ierr = PetscFree(redund->rbuf_j[i]);CHKERRQ(ierr);
254769db28dcSHong Zhang     ierr = PetscFree(redund->rbuf_a[i]);CHKERRQ(ierr);
254869db28dcSHong Zhang   }
25491d79065fSBarry Smith   ierr = PetscFree4(redund->sbuf_nz,redund->rbuf_nz,redund->rbuf_j,redund->rbuf_a);CHKERRQ(ierr);
255069db28dcSHong Zhang   ierr = PetscFree(redund);CHKERRQ(ierr);
255169db28dcSHong Zhang   PetscFunctionReturn(0);
255269db28dcSHong Zhang }
255369db28dcSHong Zhang 
255469db28dcSHong Zhang #undef __FUNCT__
255569db28dcSHong Zhang #define __FUNCT__ "MatDestroy_MatRedundant"
255669db28dcSHong Zhang PetscErrorCode MatDestroy_MatRedundant(Mat A)
255769db28dcSHong Zhang {
255869db28dcSHong Zhang   PetscErrorCode  ierr;
255969db28dcSHong Zhang   PetscContainer  container;
256069db28dcSHong Zhang   Mat_Redundant   *redund=PETSC_NULL;
256169db28dcSHong Zhang 
256269db28dcSHong Zhang   PetscFunctionBegin;
256369db28dcSHong Zhang   ierr = PetscObjectQuery((PetscObject)A,"Mat_Redundant",(PetscObject *)&container);CHKERRQ(ierr);
2564bf0cc555SLisandro Dalcin   if (!container) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Container does not exit");
256569db28dcSHong Zhang   ierr = PetscContainerGetPointer(container,(void **)&redund);CHKERRQ(ierr);
2566bf0cc555SLisandro Dalcin   A->ops->destroy = redund->Destroy;
256769db28dcSHong Zhang   ierr = PetscObjectCompose((PetscObject)A,"Mat_Redundant",0);CHKERRQ(ierr);
2568bf0cc555SLisandro Dalcin   if (A->ops->destroy) {
256969db28dcSHong Zhang     ierr = (*A->ops->destroy)(A);CHKERRQ(ierr);
2570bf0cc555SLisandro Dalcin   }
257169db28dcSHong Zhang   PetscFunctionReturn(0);
257269db28dcSHong Zhang }
257369db28dcSHong Zhang 
257469db28dcSHong Zhang #undef __FUNCT__
257569db28dcSHong Zhang #define __FUNCT__ "MatGetRedundantMatrix_MPIAIJ"
257669db28dcSHong Zhang PetscErrorCode MatGetRedundantMatrix_MPIAIJ(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,PetscInt mlocal_sub,MatReuse reuse,Mat *matredundant)
257769db28dcSHong Zhang {
257869db28dcSHong Zhang   PetscMPIInt    rank,size;
25797adad957SLisandro Dalcin   MPI_Comm       comm=((PetscObject)mat)->comm;
258069db28dcSHong Zhang   PetscErrorCode ierr;
258169db28dcSHong Zhang   PetscInt       nsends=0,nrecvs=0,i,rownz_max=0;
258269db28dcSHong Zhang   PetscMPIInt    *send_rank=PETSC_NULL,*recv_rank=PETSC_NULL;
2583d0f46423SBarry Smith   PetscInt       *rowrange=mat->rmap->range;
258469db28dcSHong Zhang   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)mat->data;
258569db28dcSHong Zhang   Mat            A=aij->A,B=aij->B,C=*matredundant;
258669db28dcSHong Zhang   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data,*b=(Mat_SeqAIJ*)B->data;
258769db28dcSHong Zhang   PetscScalar    *sbuf_a;
258869db28dcSHong Zhang   PetscInt       nzlocal=a->nz+b->nz;
2589d0f46423SBarry Smith   PetscInt       j,cstart=mat->cmap->rstart,cend=mat->cmap->rend,row,nzA,nzB,ncols,*cworkA,*cworkB;
2590d0f46423SBarry Smith   PetscInt       rstart=mat->rmap->rstart,rend=mat->rmap->rend,*bmap=aij->garray,M,N;
259169db28dcSHong Zhang   PetscInt       *cols,ctmp,lwrite,*rptr,l,*sbuf_j;
2592a77337e4SBarry Smith   MatScalar      *aworkA,*aworkB;
2593a77337e4SBarry Smith   PetscScalar    *vals;
259469db28dcSHong Zhang   PetscMPIInt    tag1,tag2,tag3,imdex;
259569db28dcSHong Zhang   MPI_Request    *s_waits1=PETSC_NULL,*s_waits2=PETSC_NULL,*s_waits3=PETSC_NULL,
259669db28dcSHong Zhang                  *r_waits1=PETSC_NULL,*r_waits2=PETSC_NULL,*r_waits3=PETSC_NULL;
259769db28dcSHong Zhang   MPI_Status     recv_status,*send_status;
259869db28dcSHong Zhang   PetscInt       *sbuf_nz=PETSC_NULL,*rbuf_nz=PETSC_NULL,count;
259969db28dcSHong Zhang   PetscInt       **rbuf_j=PETSC_NULL;
260069db28dcSHong Zhang   PetscScalar    **rbuf_a=PETSC_NULL;
260169db28dcSHong Zhang   Mat_Redundant  *redund=PETSC_NULL;
260269db28dcSHong Zhang   PetscContainer container;
260369db28dcSHong Zhang 
260469db28dcSHong Zhang   PetscFunctionBegin;
260569db28dcSHong Zhang   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
260669db28dcSHong Zhang   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
260769db28dcSHong Zhang 
260869db28dcSHong Zhang   if (reuse == MAT_REUSE_MATRIX) {
260969db28dcSHong Zhang     ierr = MatGetSize(C,&M,&N);CHKERRQ(ierr);
2610e32f2f54SBarry Smith     if (M != N || M != mat->rmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong global size");
261169db28dcSHong Zhang     ierr = MatGetLocalSize(C,&M,&N);CHKERRQ(ierr);
2612e32f2f54SBarry Smith     if (M != N || M != mlocal_sub) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong local size");
261369db28dcSHong Zhang     ierr = PetscObjectQuery((PetscObject)C,"Mat_Redundant",(PetscObject *)&container);CHKERRQ(ierr);
2614bf0cc555SLisandro Dalcin     if (!container) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Container does not exit");
261569db28dcSHong Zhang     ierr = PetscContainerGetPointer(container,(void **)&redund);CHKERRQ(ierr);
2616e32f2f54SBarry Smith     if (nzlocal != redund->nzlocal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong nzlocal");
261769db28dcSHong Zhang 
261869db28dcSHong Zhang     nsends    = redund->nsends;
261969db28dcSHong Zhang     nrecvs    = redund->nrecvs;
26201d79065fSBarry Smith     send_rank = redund->send_rank;
26211d79065fSBarry Smith     recv_rank = redund->recv_rank;
26221d79065fSBarry Smith     sbuf_nz   = redund->sbuf_nz;
26231d79065fSBarry Smith     rbuf_nz   = redund->rbuf_nz;
262469db28dcSHong Zhang     sbuf_j    = redund->sbuf_j;
262569db28dcSHong Zhang     sbuf_a    = redund->sbuf_a;
262669db28dcSHong Zhang     rbuf_j    = redund->rbuf_j;
262769db28dcSHong Zhang     rbuf_a    = redund->rbuf_a;
262869db28dcSHong Zhang   }
262969db28dcSHong Zhang 
263069db28dcSHong Zhang   if (reuse == MAT_INITIAL_MATRIX) {
263169db28dcSHong Zhang     PetscMPIInt  subrank,subsize;
263269db28dcSHong Zhang     PetscInt     nleftover,np_subcomm;
263369db28dcSHong Zhang     /* get the destination processors' id send_rank, nsends and nrecvs */
263469db28dcSHong Zhang     ierr = MPI_Comm_rank(subcomm,&subrank);CHKERRQ(ierr);
263569db28dcSHong Zhang     ierr = MPI_Comm_size(subcomm,&subsize);CHKERRQ(ierr);
2636a2ea699eSBarry Smith     ierr = PetscMalloc2(size,PetscMPIInt,&send_rank,size,PetscMPIInt,&recv_rank);CHKERRQ(ierr);
263769db28dcSHong Zhang     np_subcomm = size/nsubcomm;
263869db28dcSHong Zhang     nleftover  = size - nsubcomm*np_subcomm;
263969db28dcSHong Zhang     nsends = 0; nrecvs = 0;
264069db28dcSHong Zhang     for (i=0; i<size; i++) { /* i=rank*/
264169db28dcSHong Zhang       if (subrank == i/nsubcomm && rank != i) { /* my_subrank == other's subrank */
264269db28dcSHong Zhang         send_rank[nsends] = i; nsends++;
264369db28dcSHong Zhang         recv_rank[nrecvs++] = i;
264469db28dcSHong Zhang       }
264569db28dcSHong Zhang     }
264669db28dcSHong Zhang     if (rank >= size - nleftover) {/* this proc is a leftover processor */
264769db28dcSHong Zhang       i = size-nleftover-1;
264869db28dcSHong Zhang       j = 0;
264969db28dcSHong Zhang       while (j < nsubcomm - nleftover) {
265069db28dcSHong Zhang         send_rank[nsends++] = i;
265169db28dcSHong Zhang         i--; j++;
265269db28dcSHong Zhang       }
265369db28dcSHong Zhang     }
265469db28dcSHong Zhang 
265569db28dcSHong Zhang     if (nleftover && subsize == size/nsubcomm && subrank==subsize-1) { /* this proc recvs from leftover processors */
265669db28dcSHong Zhang       for (i=0; i<nleftover; i++) {
265769db28dcSHong Zhang         recv_rank[nrecvs++] = size-nleftover+i;
265869db28dcSHong Zhang       }
265969db28dcSHong Zhang     }
266069db28dcSHong Zhang 
266169db28dcSHong Zhang     /* allocate sbuf_j, sbuf_a */
266269db28dcSHong Zhang     i = nzlocal + rowrange[rank+1] - rowrange[rank] + 2;
266369db28dcSHong Zhang     ierr = PetscMalloc(i*sizeof(PetscInt),&sbuf_j);CHKERRQ(ierr);
266469db28dcSHong Zhang     ierr = PetscMalloc((nzlocal+1)*sizeof(PetscScalar),&sbuf_a);CHKERRQ(ierr);
266569db28dcSHong Zhang   } /* endof if (reuse == MAT_INITIAL_MATRIX) */
266669db28dcSHong Zhang 
266769db28dcSHong Zhang   /* copy mat's local entries into the buffers */
266869db28dcSHong Zhang   if (reuse == MAT_INITIAL_MATRIX) {
266969db28dcSHong Zhang     rownz_max = 0;
267069db28dcSHong Zhang     rptr = sbuf_j;
267169db28dcSHong Zhang     cols = sbuf_j + rend-rstart + 1;
267269db28dcSHong Zhang     vals = sbuf_a;
267369db28dcSHong Zhang     rptr[0] = 0;
267469db28dcSHong Zhang     for (i=0; i<rend-rstart; i++) {
267569db28dcSHong Zhang       row = i + rstart;
267669db28dcSHong Zhang       nzA    = a->i[i+1] - a->i[i]; nzB = b->i[i+1] - b->i[i];
267769db28dcSHong Zhang       ncols  = nzA + nzB;
267869db28dcSHong Zhang       cworkA = a->j + a->i[i]; cworkB = b->j + b->i[i];
267969db28dcSHong Zhang       aworkA = a->a + a->i[i]; aworkB = b->a + b->i[i];
268069db28dcSHong Zhang       /* load the column indices for this row into cols */
268169db28dcSHong Zhang       lwrite = 0;
268269db28dcSHong Zhang       for (l=0; l<nzB; l++) {
268369db28dcSHong Zhang         if ((ctmp = bmap[cworkB[l]]) < cstart) {
268469db28dcSHong Zhang           vals[lwrite]   = aworkB[l];
268569db28dcSHong Zhang           cols[lwrite++] = ctmp;
268669db28dcSHong Zhang         }
268769db28dcSHong Zhang       }
268869db28dcSHong Zhang       for (l=0; l<nzA; l++) {
268969db28dcSHong Zhang         vals[lwrite]   = aworkA[l];
269069db28dcSHong Zhang         cols[lwrite++] = cstart + cworkA[l];
269169db28dcSHong Zhang       }
269269db28dcSHong Zhang       for (l=0; l<nzB; l++) {
269369db28dcSHong Zhang         if ((ctmp = bmap[cworkB[l]]) >= cend) {
269469db28dcSHong Zhang           vals[lwrite]   = aworkB[l];
269569db28dcSHong Zhang           cols[lwrite++] = ctmp;
269669db28dcSHong Zhang         }
269769db28dcSHong Zhang       }
269869db28dcSHong Zhang       vals += ncols;
269969db28dcSHong Zhang       cols += ncols;
270069db28dcSHong Zhang       rptr[i+1] = rptr[i] + ncols;
270169db28dcSHong Zhang       if (rownz_max < ncols) rownz_max = ncols;
270269db28dcSHong Zhang     }
2703e32f2f54SBarry Smith     if (rptr[rend-rstart] != a->nz + b->nz) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_PLIB, "rptr[%d] %d != %d + %d",rend-rstart,rptr[rend-rstart+1],a->nz,b->nz);
270469db28dcSHong Zhang   } else { /* only copy matrix values into sbuf_a */
270569db28dcSHong Zhang     rptr = sbuf_j;
270669db28dcSHong Zhang     vals = sbuf_a;
270769db28dcSHong Zhang     rptr[0] = 0;
270869db28dcSHong Zhang     for (i=0; i<rend-rstart; i++) {
270969db28dcSHong Zhang       row = i + rstart;
271069db28dcSHong Zhang       nzA    = a->i[i+1] - a->i[i]; nzB = b->i[i+1] - b->i[i];
271169db28dcSHong Zhang       ncols  = nzA + nzB;
2712a2ea699eSBarry Smith       cworkB = b->j + b->i[i];
2713a2ea699eSBarry Smith       aworkA = a->a + a->i[i];
2714a2ea699eSBarry Smith       aworkB = b->a + b->i[i];
271569db28dcSHong Zhang       lwrite = 0;
271669db28dcSHong Zhang       for (l=0; l<nzB; l++) {
271769db28dcSHong Zhang         if ((ctmp = bmap[cworkB[l]]) < cstart) vals[lwrite++] = aworkB[l];
271869db28dcSHong Zhang       }
271969db28dcSHong Zhang       for (l=0; l<nzA; l++) vals[lwrite++] = aworkA[l];
272069db28dcSHong Zhang       for (l=0; l<nzB; l++) {
272169db28dcSHong Zhang         if ((ctmp = bmap[cworkB[l]]) >= cend) vals[lwrite++] = aworkB[l];
272269db28dcSHong Zhang       }
272369db28dcSHong Zhang       vals += ncols;
272469db28dcSHong Zhang       rptr[i+1] = rptr[i] + ncols;
272569db28dcSHong Zhang     }
272669db28dcSHong Zhang   } /* endof if (reuse == MAT_INITIAL_MATRIX) */
272769db28dcSHong Zhang 
272869db28dcSHong Zhang   /* send nzlocal to others, and recv other's nzlocal */
272969db28dcSHong Zhang   /*--------------------------------------------------*/
273069db28dcSHong Zhang   if (reuse == MAT_INITIAL_MATRIX) {
273169db28dcSHong Zhang     ierr = PetscMalloc2(3*(nsends + nrecvs)+1,MPI_Request,&s_waits3,nsends+1,MPI_Status,&send_status);CHKERRQ(ierr);
273269db28dcSHong Zhang     s_waits2 = s_waits3 + nsends;
273369db28dcSHong Zhang     s_waits1 = s_waits2 + nsends;
273469db28dcSHong Zhang     r_waits1 = s_waits1 + nsends;
273569db28dcSHong Zhang     r_waits2 = r_waits1 + nrecvs;
273669db28dcSHong Zhang     r_waits3 = r_waits2 + nrecvs;
273769db28dcSHong Zhang   } else {
273869db28dcSHong Zhang     ierr = PetscMalloc2(nsends + nrecvs +1,MPI_Request,&s_waits3,nsends+1,MPI_Status,&send_status);CHKERRQ(ierr);
273969db28dcSHong Zhang     r_waits3 = s_waits3 + nsends;
274069db28dcSHong Zhang   }
274169db28dcSHong Zhang 
274269db28dcSHong Zhang   ierr = PetscObjectGetNewTag((PetscObject)mat,&tag3);CHKERRQ(ierr);
274369db28dcSHong Zhang   if (reuse == MAT_INITIAL_MATRIX) {
274469db28dcSHong Zhang     /* get new tags to keep the communication clean */
274569db28dcSHong Zhang     ierr = PetscObjectGetNewTag((PetscObject)mat,&tag1);CHKERRQ(ierr);
274669db28dcSHong Zhang     ierr = PetscObjectGetNewTag((PetscObject)mat,&tag2);CHKERRQ(ierr);
27471d79065fSBarry Smith     ierr = PetscMalloc4(nsends,PetscInt,&sbuf_nz,nrecvs,PetscInt,&rbuf_nz,nrecvs,PetscInt*,&rbuf_j,nrecvs,PetscScalar*,&rbuf_a);CHKERRQ(ierr);
274869db28dcSHong Zhang 
274969db28dcSHong Zhang     /* post receives of other's nzlocal */
275069db28dcSHong Zhang     for (i=0; i<nrecvs; i++) {
275169db28dcSHong Zhang       ierr = MPI_Irecv(rbuf_nz+i,1,MPIU_INT,MPI_ANY_SOURCE,tag1,comm,r_waits1+i);CHKERRQ(ierr);
275269db28dcSHong Zhang     }
275369db28dcSHong Zhang     /* send nzlocal to others */
275469db28dcSHong Zhang     for (i=0; i<nsends; i++) {
275569db28dcSHong Zhang       sbuf_nz[i] = nzlocal;
275669db28dcSHong Zhang       ierr = MPI_Isend(sbuf_nz+i,1,MPIU_INT,send_rank[i],tag1,comm,s_waits1+i);CHKERRQ(ierr);
275769db28dcSHong Zhang     }
275869db28dcSHong Zhang     /* wait on receives of nzlocal; allocate space for rbuf_j, rbuf_a */
275969db28dcSHong Zhang     count = nrecvs;
276069db28dcSHong Zhang     while (count) {
276169db28dcSHong Zhang       ierr = MPI_Waitany(nrecvs,r_waits1,&imdex,&recv_status);CHKERRQ(ierr);
276269db28dcSHong Zhang       recv_rank[imdex] = recv_status.MPI_SOURCE;
276369db28dcSHong Zhang       /* allocate rbuf_a and rbuf_j; then post receives of rbuf_j */
276469db28dcSHong Zhang       ierr = PetscMalloc((rbuf_nz[imdex]+1)*sizeof(PetscScalar),&rbuf_a[imdex]);CHKERRQ(ierr);
276569db28dcSHong Zhang 
276669db28dcSHong Zhang       i = rowrange[recv_status.MPI_SOURCE+1] - rowrange[recv_status.MPI_SOURCE]; /* number of expected mat->i */
276769db28dcSHong Zhang       rbuf_nz[imdex] += i + 2;
276869db28dcSHong Zhang       ierr = PetscMalloc(rbuf_nz[imdex]*sizeof(PetscInt),&rbuf_j[imdex]);CHKERRQ(ierr);
276969db28dcSHong Zhang       ierr = MPI_Irecv(rbuf_j[imdex],rbuf_nz[imdex],MPIU_INT,recv_status.MPI_SOURCE,tag2,comm,r_waits2+imdex);CHKERRQ(ierr);
277069db28dcSHong Zhang       count--;
277169db28dcSHong Zhang     }
277269db28dcSHong Zhang     /* wait on sends of nzlocal */
277369db28dcSHong Zhang     if (nsends) {ierr = MPI_Waitall(nsends,s_waits1,send_status);CHKERRQ(ierr);}
277469db28dcSHong Zhang     /* send mat->i,j to others, and recv from other's */
277569db28dcSHong Zhang     /*------------------------------------------------*/
277669db28dcSHong Zhang     for (i=0; i<nsends; i++) {
277769db28dcSHong Zhang       j = nzlocal + rowrange[rank+1] - rowrange[rank] + 1;
277869db28dcSHong Zhang       ierr = MPI_Isend(sbuf_j,j,MPIU_INT,send_rank[i],tag2,comm,s_waits2+i);CHKERRQ(ierr);
277969db28dcSHong Zhang     }
278069db28dcSHong Zhang     /* wait on receives of mat->i,j */
278169db28dcSHong Zhang     /*------------------------------*/
278269db28dcSHong Zhang     count = nrecvs;
278369db28dcSHong Zhang     while (count) {
278469db28dcSHong Zhang       ierr = MPI_Waitany(nrecvs,r_waits2,&imdex,&recv_status);CHKERRQ(ierr);
2785e32f2f54SBarry Smith       if (recv_rank[imdex] != recv_status.MPI_SOURCE) SETERRQ2(PETSC_COMM_SELF,1, "recv_rank %d != MPI_SOURCE %d",recv_rank[imdex],recv_status.MPI_SOURCE);
278669db28dcSHong Zhang       count--;
278769db28dcSHong Zhang     }
278869db28dcSHong Zhang     /* wait on sends of mat->i,j */
278969db28dcSHong Zhang     /*---------------------------*/
279069db28dcSHong Zhang     if (nsends) {
279169db28dcSHong Zhang       ierr = MPI_Waitall(nsends,s_waits2,send_status);CHKERRQ(ierr);
279269db28dcSHong Zhang     }
279369db28dcSHong Zhang   } /* endof if (reuse == MAT_INITIAL_MATRIX) */
279469db28dcSHong Zhang 
279569db28dcSHong Zhang   /* post receives, send and receive mat->a */
279669db28dcSHong Zhang   /*----------------------------------------*/
279769db28dcSHong Zhang   for (imdex=0; imdex<nrecvs; imdex++) {
279869db28dcSHong Zhang     ierr = MPI_Irecv(rbuf_a[imdex],rbuf_nz[imdex],MPIU_SCALAR,recv_rank[imdex],tag3,comm,r_waits3+imdex);CHKERRQ(ierr);
279969db28dcSHong Zhang   }
280069db28dcSHong Zhang   for (i=0; i<nsends; i++) {
280169db28dcSHong Zhang     ierr = MPI_Isend(sbuf_a,nzlocal,MPIU_SCALAR,send_rank[i],tag3,comm,s_waits3+i);CHKERRQ(ierr);
280269db28dcSHong Zhang   }
280369db28dcSHong Zhang   count = nrecvs;
280469db28dcSHong Zhang   while (count) {
280569db28dcSHong Zhang     ierr = MPI_Waitany(nrecvs,r_waits3,&imdex,&recv_status);CHKERRQ(ierr);
2806e32f2f54SBarry Smith     if (recv_rank[imdex] != recv_status.MPI_SOURCE) SETERRQ2(PETSC_COMM_SELF,1, "recv_rank %d != MPI_SOURCE %d",recv_rank[imdex],recv_status.MPI_SOURCE);
280769db28dcSHong Zhang     count--;
280869db28dcSHong Zhang   }
280969db28dcSHong Zhang   if (nsends) {
281069db28dcSHong Zhang     ierr = MPI_Waitall(nsends,s_waits3,send_status);CHKERRQ(ierr);
281169db28dcSHong Zhang   }
281269db28dcSHong Zhang 
281369db28dcSHong Zhang   ierr = PetscFree2(s_waits3,send_status);CHKERRQ(ierr);
281469db28dcSHong Zhang 
281569db28dcSHong Zhang   /* create redundant matrix */
281669db28dcSHong Zhang   /*-------------------------*/
281769db28dcSHong Zhang   if (reuse == MAT_INITIAL_MATRIX) {
281869db28dcSHong Zhang     /* compute rownz_max for preallocation */
281969db28dcSHong Zhang     for (imdex=0; imdex<nrecvs; imdex++) {
282069db28dcSHong Zhang       j = rowrange[recv_rank[imdex]+1] - rowrange[recv_rank[imdex]];
282169db28dcSHong Zhang       rptr = rbuf_j[imdex];
282269db28dcSHong Zhang       for (i=0; i<j; i++) {
282369db28dcSHong Zhang         ncols = rptr[i+1] - rptr[i];
282469db28dcSHong Zhang         if (rownz_max < ncols) rownz_max = ncols;
282569db28dcSHong Zhang       }
282669db28dcSHong Zhang     }
282769db28dcSHong Zhang 
282869db28dcSHong Zhang     ierr = MatCreate(subcomm,&C);CHKERRQ(ierr);
282969db28dcSHong Zhang     ierr = MatSetSizes(C,mlocal_sub,mlocal_sub,PETSC_DECIDE,PETSC_DECIDE);CHKERRQ(ierr);
2830a2f3521dSMark F. Adams     ierr = MatSetBlockSizes(C,mat->rmap->bs,mat->cmap->bs);CHKERRQ(ierr);
283169db28dcSHong Zhang     ierr = MatSetFromOptions(C);CHKERRQ(ierr);
283269db28dcSHong Zhang     ierr = MatSeqAIJSetPreallocation(C,rownz_max,PETSC_NULL);CHKERRQ(ierr);
283369db28dcSHong Zhang     ierr = MatMPIAIJSetPreallocation(C,rownz_max,PETSC_NULL,rownz_max,PETSC_NULL);CHKERRQ(ierr);
283469db28dcSHong Zhang   } else {
283569db28dcSHong Zhang     C = *matredundant;
283669db28dcSHong Zhang   }
283769db28dcSHong Zhang 
283869db28dcSHong Zhang   /* insert local matrix entries */
283969db28dcSHong Zhang   rptr = sbuf_j;
284069db28dcSHong Zhang   cols = sbuf_j + rend-rstart + 1;
284169db28dcSHong Zhang   vals = sbuf_a;
284269db28dcSHong Zhang   for (i=0; i<rend-rstart; i++) {
284369db28dcSHong Zhang     row   = i + rstart;
284469db28dcSHong Zhang     ncols = rptr[i+1] - rptr[i];
284569db28dcSHong Zhang     ierr = MatSetValues(C,1,&row,ncols,cols,vals,INSERT_VALUES);CHKERRQ(ierr);
284669db28dcSHong Zhang     vals += ncols;
284769db28dcSHong Zhang     cols += ncols;
284869db28dcSHong Zhang   }
284969db28dcSHong Zhang   /* insert received matrix entries */
285069db28dcSHong Zhang   for (imdex=0; imdex<nrecvs; imdex++) {
285169db28dcSHong Zhang     rstart = rowrange[recv_rank[imdex]];
285269db28dcSHong Zhang     rend   = rowrange[recv_rank[imdex]+1];
285369db28dcSHong Zhang     rptr = rbuf_j[imdex];
285469db28dcSHong Zhang     cols = rbuf_j[imdex] + rend-rstart + 1;
285569db28dcSHong Zhang     vals = rbuf_a[imdex];
285669db28dcSHong Zhang     for (i=0; i<rend-rstart; i++) {
285769db28dcSHong Zhang       row   = i + rstart;
285869db28dcSHong Zhang       ncols = rptr[i+1] - rptr[i];
285969db28dcSHong Zhang       ierr = MatSetValues(C,1,&row,ncols,cols,vals,INSERT_VALUES);CHKERRQ(ierr);
286069db28dcSHong Zhang       vals += ncols;
286169db28dcSHong Zhang       cols += ncols;
286269db28dcSHong Zhang     }
286369db28dcSHong Zhang   }
286469db28dcSHong Zhang   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
286569db28dcSHong Zhang   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
286669db28dcSHong Zhang   ierr = MatGetSize(C,&M,&N);CHKERRQ(ierr);
2867e32f2f54SBarry Smith   if (M != mat->rmap->N || N != mat->cmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"redundant mat size %d != input mat size %d",M,mat->rmap->N);
286869db28dcSHong Zhang   if (reuse == MAT_INITIAL_MATRIX) {
286969db28dcSHong Zhang     PetscContainer container;
287069db28dcSHong Zhang     *matredundant = C;
287169db28dcSHong Zhang     /* create a supporting struct and attach it to C for reuse */
287238f2d2fdSLisandro Dalcin     ierr = PetscNewLog(C,Mat_Redundant,&redund);CHKERRQ(ierr);
287369db28dcSHong Zhang     ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr);
287469db28dcSHong Zhang     ierr = PetscContainerSetPointer(container,redund);CHKERRQ(ierr);
287569db28dcSHong Zhang     ierr = PetscContainerSetUserDestroy(container,PetscContainerDestroy_MatRedundant);CHKERRQ(ierr);
2876bf0cc555SLisandro Dalcin     ierr = PetscObjectCompose((PetscObject)C,"Mat_Redundant",(PetscObject)container);CHKERRQ(ierr);
2877bf0cc555SLisandro Dalcin     ierr = PetscContainerDestroy(&container);CHKERRQ(ierr);
287869db28dcSHong Zhang 
287969db28dcSHong Zhang     redund->nzlocal = nzlocal;
288069db28dcSHong Zhang     redund->nsends  = nsends;
288169db28dcSHong Zhang     redund->nrecvs  = nrecvs;
288269db28dcSHong Zhang     redund->send_rank = send_rank;
28831d79065fSBarry Smith     redund->recv_rank = recv_rank;
288469db28dcSHong Zhang     redund->sbuf_nz = sbuf_nz;
28851d79065fSBarry Smith     redund->rbuf_nz = rbuf_nz;
288669db28dcSHong Zhang     redund->sbuf_j  = sbuf_j;
288769db28dcSHong Zhang     redund->sbuf_a  = sbuf_a;
288869db28dcSHong Zhang     redund->rbuf_j  = rbuf_j;
288969db28dcSHong Zhang     redund->rbuf_a  = rbuf_a;
289069db28dcSHong Zhang 
2891bf0cc555SLisandro Dalcin     redund->Destroy = C->ops->destroy;
289269db28dcSHong Zhang     C->ops->destroy = MatDestroy_MatRedundant;
289369db28dcSHong Zhang   }
289469db28dcSHong Zhang   PetscFunctionReturn(0);
289569db28dcSHong Zhang }
289669db28dcSHong Zhang 
289703bc72f1SMatthew Knepley #undef __FUNCT__
2898c91732d9SHong Zhang #define __FUNCT__ "MatGetRowMaxAbs_MPIAIJ"
2899c91732d9SHong Zhang PetscErrorCode MatGetRowMaxAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[])
2900c91732d9SHong Zhang {
2901c91732d9SHong Zhang   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
2902c91732d9SHong Zhang   PetscErrorCode ierr;
2903c91732d9SHong Zhang   PetscInt       i,*idxb = 0;
2904c91732d9SHong Zhang   PetscScalar    *va,*vb;
2905c91732d9SHong Zhang   Vec            vtmp;
2906c91732d9SHong Zhang 
2907c91732d9SHong Zhang   PetscFunctionBegin;
2908c91732d9SHong Zhang   ierr = MatGetRowMaxAbs(a->A,v,idx);CHKERRQ(ierr);
2909c91732d9SHong Zhang   ierr = VecGetArray(v,&va);CHKERRQ(ierr);
2910c91732d9SHong Zhang   if (idx) {
2911192daf7cSBarry Smith     for (i=0; i<A->rmap->n; i++) {
2912d0f46423SBarry Smith       if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart;
2913c91732d9SHong Zhang     }
2914c91732d9SHong Zhang   }
2915c91732d9SHong Zhang 
2916d0f46423SBarry Smith   ierr = VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);CHKERRQ(ierr);
2917c91732d9SHong Zhang   if (idx) {
2918d0f46423SBarry Smith     ierr = PetscMalloc(A->rmap->n*sizeof(PetscInt),&idxb);CHKERRQ(ierr);
2919c91732d9SHong Zhang   }
2920c91732d9SHong Zhang   ierr = MatGetRowMaxAbs(a->B,vtmp,idxb);CHKERRQ(ierr);
2921c91732d9SHong Zhang   ierr = VecGetArray(vtmp,&vb);CHKERRQ(ierr);
2922c91732d9SHong Zhang 
2923d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
2924c91732d9SHong Zhang     if (PetscAbsScalar(va[i]) < PetscAbsScalar(vb[i])) {
2925c91732d9SHong Zhang       va[i] = vb[i];
2926c91732d9SHong Zhang       if (idx) idx[i] = a->garray[idxb[i]];
2927c91732d9SHong Zhang     }
2928c91732d9SHong Zhang   }
2929c91732d9SHong Zhang 
2930c91732d9SHong Zhang   ierr = VecRestoreArray(v,&va);CHKERRQ(ierr);
2931c91732d9SHong Zhang   ierr = VecRestoreArray(vtmp,&vb);CHKERRQ(ierr);
2932c91732d9SHong Zhang   ierr = PetscFree(idxb);CHKERRQ(ierr);
29336bf464f9SBarry Smith   ierr = VecDestroy(&vtmp);CHKERRQ(ierr);
2934c91732d9SHong Zhang   PetscFunctionReturn(0);
2935c91732d9SHong Zhang }
2936c91732d9SHong Zhang 
2937c91732d9SHong Zhang #undef __FUNCT__
2938c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_MPIAIJ"
2939c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[])
2940c87e5d42SMatthew Knepley {
2941c87e5d42SMatthew Knepley   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
2942c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2943c87e5d42SMatthew Knepley   PetscInt       i,*idxb = 0;
2944c87e5d42SMatthew Knepley   PetscScalar    *va,*vb;
2945c87e5d42SMatthew Knepley   Vec            vtmp;
2946c87e5d42SMatthew Knepley 
2947c87e5d42SMatthew Knepley   PetscFunctionBegin;
2948c87e5d42SMatthew Knepley   ierr = MatGetRowMinAbs(a->A,v,idx);CHKERRQ(ierr);
2949c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&va);CHKERRQ(ierr);
2950c87e5d42SMatthew Knepley   if (idx) {
2951c87e5d42SMatthew Knepley     for (i=0; i<A->cmap->n; i++) {
2952c87e5d42SMatthew Knepley       if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart;
2953c87e5d42SMatthew Knepley     }
2954c87e5d42SMatthew Knepley   }
2955c87e5d42SMatthew Knepley 
2956c87e5d42SMatthew Knepley   ierr = VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);CHKERRQ(ierr);
2957c87e5d42SMatthew Knepley   if (idx) {
2958c87e5d42SMatthew Knepley     ierr = PetscMalloc(A->rmap->n*sizeof(PetscInt),&idxb);CHKERRQ(ierr);
2959c87e5d42SMatthew Knepley   }
2960c87e5d42SMatthew Knepley   ierr = MatGetRowMinAbs(a->B,vtmp,idxb);CHKERRQ(ierr);
2961c87e5d42SMatthew Knepley   ierr = VecGetArray(vtmp,&vb);CHKERRQ(ierr);
2962c87e5d42SMatthew Knepley 
2963c87e5d42SMatthew Knepley   for (i=0; i<A->rmap->n; i++) {
2964c87e5d42SMatthew Knepley     if (PetscAbsScalar(va[i]) > PetscAbsScalar(vb[i])) {
2965c87e5d42SMatthew Knepley       va[i] = vb[i];
2966c87e5d42SMatthew Knepley       if (idx) idx[i] = a->garray[idxb[i]];
2967c87e5d42SMatthew Knepley     }
2968c87e5d42SMatthew Knepley   }
2969c87e5d42SMatthew Knepley 
2970c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&va);CHKERRQ(ierr);
2971c87e5d42SMatthew Knepley   ierr = VecRestoreArray(vtmp,&vb);CHKERRQ(ierr);
2972c87e5d42SMatthew Knepley   ierr = PetscFree(idxb);CHKERRQ(ierr);
29736bf464f9SBarry Smith   ierr = VecDestroy(&vtmp);CHKERRQ(ierr);
2974c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
2975c87e5d42SMatthew Knepley }
2976c87e5d42SMatthew Knepley 
2977c87e5d42SMatthew Knepley #undef __FUNCT__
297803bc72f1SMatthew Knepley #define __FUNCT__ "MatGetRowMin_MPIAIJ"
297903bc72f1SMatthew Knepley PetscErrorCode MatGetRowMin_MPIAIJ(Mat A, Vec v, PetscInt idx[])
298003bc72f1SMatthew Knepley {
298103bc72f1SMatthew Knepley   Mat_MPIAIJ    *mat    = (Mat_MPIAIJ *) A->data;
2982d0f46423SBarry Smith   PetscInt       n      = A->rmap->n;
2983d0f46423SBarry Smith   PetscInt       cstart = A->cmap->rstart;
298403bc72f1SMatthew Knepley   PetscInt      *cmap   = mat->garray;
298503bc72f1SMatthew Knepley   PetscInt      *diagIdx, *offdiagIdx;
298603bc72f1SMatthew Knepley   Vec            diagV, offdiagV;
298703bc72f1SMatthew Knepley   PetscScalar   *a, *diagA, *offdiagA;
298803bc72f1SMatthew Knepley   PetscInt       r;
298903bc72f1SMatthew Knepley   PetscErrorCode ierr;
299003bc72f1SMatthew Knepley 
299103bc72f1SMatthew Knepley   PetscFunctionBegin;
299203bc72f1SMatthew Knepley   ierr = PetscMalloc2(n,PetscInt,&diagIdx,n,PetscInt,&offdiagIdx);CHKERRQ(ierr);
2993e64afeacSLisandro Dalcin   ierr = VecCreateSeq(((PetscObject)A)->comm, n, &diagV);CHKERRQ(ierr);
2994e64afeacSLisandro Dalcin   ierr = VecCreateSeq(((PetscObject)A)->comm, n, &offdiagV);CHKERRQ(ierr);
299503bc72f1SMatthew Knepley   ierr = MatGetRowMin(mat->A, diagV,    diagIdx);CHKERRQ(ierr);
299603bc72f1SMatthew Knepley   ierr = MatGetRowMin(mat->B, offdiagV, offdiagIdx);CHKERRQ(ierr);
299703bc72f1SMatthew Knepley   ierr = VecGetArray(v,        &a);CHKERRQ(ierr);
299803bc72f1SMatthew Knepley   ierr = VecGetArray(diagV,    &diagA);CHKERRQ(ierr);
299903bc72f1SMatthew Knepley   ierr = VecGetArray(offdiagV, &offdiagA);CHKERRQ(ierr);
300003bc72f1SMatthew Knepley   for (r = 0; r < n; ++r) {
3001028cd4eaSSatish Balay     if (PetscAbsScalar(diagA[r]) <= PetscAbsScalar(offdiagA[r])) {
300203bc72f1SMatthew Knepley       a[r]   = diagA[r];
300303bc72f1SMatthew Knepley       idx[r] = cstart + diagIdx[r];
300403bc72f1SMatthew Knepley     } else {
300503bc72f1SMatthew Knepley       a[r]   = offdiagA[r];
300603bc72f1SMatthew Knepley       idx[r] = cmap[offdiagIdx[r]];
300703bc72f1SMatthew Knepley     }
300803bc72f1SMatthew Knepley   }
300903bc72f1SMatthew Knepley   ierr = VecRestoreArray(v,        &a);CHKERRQ(ierr);
301003bc72f1SMatthew Knepley   ierr = VecRestoreArray(diagV,    &diagA);CHKERRQ(ierr);
301103bc72f1SMatthew Knepley   ierr = VecRestoreArray(offdiagV, &offdiagA);CHKERRQ(ierr);
30126bf464f9SBarry Smith   ierr = VecDestroy(&diagV);CHKERRQ(ierr);
30136bf464f9SBarry Smith   ierr = VecDestroy(&offdiagV);CHKERRQ(ierr);
301403bc72f1SMatthew Knepley   ierr = PetscFree2(diagIdx, offdiagIdx);CHKERRQ(ierr);
301503bc72f1SMatthew Knepley   PetscFunctionReturn(0);
301603bc72f1SMatthew Knepley }
301703bc72f1SMatthew Knepley 
30185494a064SHong Zhang #undef __FUNCT__
3019c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMax_MPIAIJ"
3020c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMax_MPIAIJ(Mat A, Vec v, PetscInt idx[])
3021c87e5d42SMatthew Knepley {
3022c87e5d42SMatthew Knepley   Mat_MPIAIJ    *mat    = (Mat_MPIAIJ *) A->data;
3023c87e5d42SMatthew Knepley   PetscInt       n      = A->rmap->n;
3024c87e5d42SMatthew Knepley   PetscInt       cstart = A->cmap->rstart;
3025c87e5d42SMatthew Knepley   PetscInt      *cmap   = mat->garray;
3026c87e5d42SMatthew Knepley   PetscInt      *diagIdx, *offdiagIdx;
3027c87e5d42SMatthew Knepley   Vec            diagV, offdiagV;
3028c87e5d42SMatthew Knepley   PetscScalar   *a, *diagA, *offdiagA;
3029c87e5d42SMatthew Knepley   PetscInt       r;
3030c87e5d42SMatthew Knepley   PetscErrorCode ierr;
3031c87e5d42SMatthew Knepley 
3032c87e5d42SMatthew Knepley   PetscFunctionBegin;
3033c87e5d42SMatthew Knepley   ierr = PetscMalloc2(n,PetscInt,&diagIdx,n,PetscInt,&offdiagIdx);CHKERRQ(ierr);
3034d11e49fbSSatish Balay   ierr = VecCreateSeq(PETSC_COMM_SELF, n, &diagV);CHKERRQ(ierr);
3035d11e49fbSSatish Balay   ierr = VecCreateSeq(PETSC_COMM_SELF, n, &offdiagV);CHKERRQ(ierr);
3036c87e5d42SMatthew Knepley   ierr = MatGetRowMax(mat->A, diagV,    diagIdx);CHKERRQ(ierr);
3037c87e5d42SMatthew Knepley   ierr = MatGetRowMax(mat->B, offdiagV, offdiagIdx);CHKERRQ(ierr);
3038c87e5d42SMatthew Knepley   ierr = VecGetArray(v,        &a);CHKERRQ(ierr);
3039c87e5d42SMatthew Knepley   ierr = VecGetArray(diagV,    &diagA);CHKERRQ(ierr);
3040c87e5d42SMatthew Knepley   ierr = VecGetArray(offdiagV, &offdiagA);CHKERRQ(ierr);
3041c87e5d42SMatthew Knepley   for (r = 0; r < n; ++r) {
3042c87e5d42SMatthew Knepley     if (PetscAbsScalar(diagA[r]) >= PetscAbsScalar(offdiagA[r])) {
3043c87e5d42SMatthew Knepley       a[r]   = diagA[r];
3044c87e5d42SMatthew Knepley       idx[r] = cstart + diagIdx[r];
3045c87e5d42SMatthew Knepley     } else {
3046c87e5d42SMatthew Knepley       a[r]   = offdiagA[r];
3047c87e5d42SMatthew Knepley       idx[r] = cmap[offdiagIdx[r]];
3048c87e5d42SMatthew Knepley     }
3049c87e5d42SMatthew Knepley   }
3050c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,        &a);CHKERRQ(ierr);
3051c87e5d42SMatthew Knepley   ierr = VecRestoreArray(diagV,    &diagA);CHKERRQ(ierr);
3052c87e5d42SMatthew Knepley   ierr = VecRestoreArray(offdiagV, &offdiagA);CHKERRQ(ierr);
30536bf464f9SBarry Smith   ierr = VecDestroy(&diagV);CHKERRQ(ierr);
30546bf464f9SBarry Smith   ierr = VecDestroy(&offdiagV);CHKERRQ(ierr);
3055c87e5d42SMatthew Knepley   ierr = PetscFree2(diagIdx, offdiagIdx);CHKERRQ(ierr);
3056c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
3057c87e5d42SMatthew Knepley }
3058c87e5d42SMatthew Knepley 
3059c87e5d42SMatthew Knepley #undef __FUNCT__
3060d1adec66SJed Brown #define __FUNCT__ "MatGetSeqNonzeroStructure_MPIAIJ"
3061d1adec66SJed Brown PetscErrorCode MatGetSeqNonzeroStructure_MPIAIJ(Mat mat,Mat *newmat)
30625494a064SHong Zhang {
30635494a064SHong Zhang   PetscErrorCode ierr;
3064f6d58c54SBarry Smith   Mat            *dummy;
30655494a064SHong Zhang 
30665494a064SHong Zhang   PetscFunctionBegin;
3067f6d58c54SBarry Smith   ierr = MatGetSubMatrix_MPIAIJ_All(mat,MAT_DO_NOT_GET_VALUES,MAT_INITIAL_MATRIX,&dummy);CHKERRQ(ierr);
3068f6d58c54SBarry Smith   *newmat = *dummy;
3069f6d58c54SBarry Smith   ierr = PetscFree(dummy);CHKERRQ(ierr);
30705494a064SHong Zhang   PetscFunctionReturn(0);
30715494a064SHong Zhang }
30725494a064SHong Zhang 
30737087cfbeSBarry Smith extern PetscErrorCode  MatFDColoringApply_AIJ(Mat,MatFDColoring,Vec,MatStructure*,void*);
3074bbead8a2SBarry Smith 
3075bbead8a2SBarry Smith #undef __FUNCT__
3076bbead8a2SBarry Smith #define __FUNCT__ "MatInvertBlockDiagonal_MPIAIJ"
3077713ccfa9SJed Brown PetscErrorCode  MatInvertBlockDiagonal_MPIAIJ(Mat A,const PetscScalar **values)
3078bbead8a2SBarry Smith {
3079bbead8a2SBarry Smith   Mat_MPIAIJ    *a = (Mat_MPIAIJ*) A->data;
3080bbead8a2SBarry Smith   PetscErrorCode ierr;
3081bbead8a2SBarry Smith 
3082bbead8a2SBarry Smith   PetscFunctionBegin;
3083bbead8a2SBarry Smith   ierr = MatInvertBlockDiagonal(a->A,values);CHKERRQ(ierr);
3084bbead8a2SBarry Smith   PetscFunctionReturn(0);
3085bbead8a2SBarry Smith }
3086bbead8a2SBarry Smith 
308773a71a0fSBarry Smith #undef __FUNCT__
308873a71a0fSBarry Smith #define __FUNCT__ "MatSetRandom_MPIAIJ"
308973a71a0fSBarry Smith static PetscErrorCode  MatSetRandom_MPIAIJ(Mat x,PetscRandom rctx)
309073a71a0fSBarry Smith {
309173a71a0fSBarry Smith   PetscErrorCode ierr;
309273a71a0fSBarry Smith   Mat_MPIAIJ     *aij = (Mat_MPIAIJ*)x->data;
309373a71a0fSBarry Smith 
309473a71a0fSBarry Smith   PetscFunctionBegin;
309573a71a0fSBarry Smith   ierr = MatSetRandom(aij->A,rctx);CHKERRQ(ierr);
309673a71a0fSBarry Smith   ierr = MatSetRandom(aij->B,rctx);CHKERRQ(ierr);
309773a71a0fSBarry Smith   ierr = MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
309873a71a0fSBarry Smith   ierr = MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
309973a71a0fSBarry Smith   PetscFunctionReturn(0);
310073a71a0fSBarry Smith }
3101bbead8a2SBarry Smith 
31028a729477SBarry Smith /* -------------------------------------------------------------------*/
3103cda55fadSBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ,
3104cda55fadSBarry Smith        MatGetRow_MPIAIJ,
3105cda55fadSBarry Smith        MatRestoreRow_MPIAIJ,
3106cda55fadSBarry Smith        MatMult_MPIAIJ,
310797304618SKris Buschelman /* 4*/ MatMultAdd_MPIAIJ,
31087c922b88SBarry Smith        MatMultTranspose_MPIAIJ,
31097c922b88SBarry Smith        MatMultTransposeAdd_MPIAIJ,
3110519f805aSKarl Rupp #if defined(PETSC_HAVE_PBGL)
3111103bf8bdSMatthew Knepley        MatSolve_MPIAIJ,
3112103bf8bdSMatthew Knepley #else
3113cda55fadSBarry Smith        0,
3114103bf8bdSMatthew Knepley #endif
3115cda55fadSBarry Smith        0,
3116cda55fadSBarry Smith        0,
311797304618SKris Buschelman /*10*/ 0,
3118cda55fadSBarry Smith        0,
3119cda55fadSBarry Smith        0,
312041f059aeSBarry Smith        MatSOR_MPIAIJ,
3121b7c46309SBarry Smith        MatTranspose_MPIAIJ,
312297304618SKris Buschelman /*15*/ MatGetInfo_MPIAIJ,
3123cda55fadSBarry Smith        MatEqual_MPIAIJ,
3124cda55fadSBarry Smith        MatGetDiagonal_MPIAIJ,
3125cda55fadSBarry Smith        MatDiagonalScale_MPIAIJ,
3126cda55fadSBarry Smith        MatNorm_MPIAIJ,
312797304618SKris Buschelman /*20*/ MatAssemblyBegin_MPIAIJ,
3128cda55fadSBarry Smith        MatAssemblyEnd_MPIAIJ,
3129cda55fadSBarry Smith        MatSetOption_MPIAIJ,
3130cda55fadSBarry Smith        MatZeroEntries_MPIAIJ,
3131d519adbfSMatthew Knepley /*24*/ MatZeroRows_MPIAIJ,
3132cda55fadSBarry Smith        0,
3133519f805aSKarl Rupp #if defined(PETSC_HAVE_PBGL)
3134719d5645SBarry Smith        0,
3135103bf8bdSMatthew Knepley #else
3136cda55fadSBarry Smith        0,
3137103bf8bdSMatthew Knepley #endif
3138cda55fadSBarry Smith        0,
3139cda55fadSBarry Smith        0,
31404994cf47SJed Brown /*29*/ MatSetUp_MPIAIJ,
3141519f805aSKarl Rupp #if defined(PETSC_HAVE_PBGL)
3142719d5645SBarry Smith        0,
3143103bf8bdSMatthew Knepley #else
3144cda55fadSBarry Smith        0,
3145103bf8bdSMatthew Knepley #endif
3146cda55fadSBarry Smith        0,
3147cda55fadSBarry Smith        0,
3148cda55fadSBarry Smith        0,
3149d519adbfSMatthew Knepley /*34*/ MatDuplicate_MPIAIJ,
3150cda55fadSBarry Smith        0,
3151cda55fadSBarry Smith        0,
3152cda55fadSBarry Smith        0,
3153cda55fadSBarry Smith        0,
3154d519adbfSMatthew Knepley /*39*/ MatAXPY_MPIAIJ,
3155cda55fadSBarry Smith        MatGetSubMatrices_MPIAIJ,
3156cda55fadSBarry Smith        MatIncreaseOverlap_MPIAIJ,
3157cda55fadSBarry Smith        MatGetValues_MPIAIJ,
3158cb5b572fSBarry Smith        MatCopy_MPIAIJ,
3159d519adbfSMatthew Knepley /*44*/ MatGetRowMax_MPIAIJ,
3160cda55fadSBarry Smith        MatScale_MPIAIJ,
3161cda55fadSBarry Smith        0,
3162cda55fadSBarry Smith        0,
3163564f14d6SBarry Smith        MatZeroRowsColumns_MPIAIJ,
316473a71a0fSBarry Smith /*49*/ MatSetRandom_MPIAIJ,
3165cda55fadSBarry Smith        0,
3166cda55fadSBarry Smith        0,
3167cda55fadSBarry Smith        0,
3168cda55fadSBarry Smith        0,
3169d519adbfSMatthew Knepley /*54*/ MatFDColoringCreate_MPIAIJ,
3170cda55fadSBarry Smith        0,
3171cda55fadSBarry Smith        MatSetUnfactored_MPIAIJ,
317272e6a0cfSJed Brown        MatPermute_MPIAIJ,
3173cda55fadSBarry Smith        0,
3174d519adbfSMatthew Knepley /*59*/ MatGetSubMatrix_MPIAIJ,
3175e03a110bSBarry Smith        MatDestroy_MPIAIJ,
3176e03a110bSBarry Smith        MatView_MPIAIJ,
3177357abbc8SBarry Smith        0,
3178f996eeb8SHong Zhang        MatMatMatMult_MPIAIJ_MPIAIJ_MPIAIJ,
3179f996eeb8SHong Zhang /*64*/ MatMatMatMultSymbolic_MPIAIJ_MPIAIJ_MPIAIJ,
3180f996eeb8SHong Zhang        MatMatMatMultNumeric_MPIAIJ_MPIAIJ_MPIAIJ,
3181a2243be0SBarry Smith        0,
3182a2243be0SBarry Smith        0,
3183a2243be0SBarry Smith        0,
3184d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_MPIAIJ,
3185c87e5d42SMatthew Knepley        MatGetRowMinAbs_MPIAIJ,
3186a2243be0SBarry Smith        0,
3187a2243be0SBarry Smith        MatSetColoring_MPIAIJ,
3188dcf5cc72SBarry Smith        0,
318997304618SKris Buschelman        MatSetValuesAdifor_MPIAIJ,
31903acb8795SBarry Smith /*75*/ MatFDColoringApply_AIJ,
319197304618SKris Buschelman        0,
319297304618SKris Buschelman        0,
319397304618SKris Buschelman        0,
3194f1f41ecbSJed Brown        MatFindZeroDiagonals_MPIAIJ,
319597304618SKris Buschelman /*80*/ 0,
319697304618SKris Buschelman        0,
319797304618SKris Buschelman        0,
31985bba2384SShri Abhyankar /*83*/ MatLoad_MPIAIJ,
31996284ec50SHong Zhang        0,
32006284ec50SHong Zhang        0,
32016284ec50SHong Zhang        0,
32026284ec50SHong Zhang        0,
3203865e5f61SKris Buschelman        0,
3204d519adbfSMatthew Knepley /*89*/ MatMatMult_MPIAIJ_MPIAIJ,
320526be0446SHong Zhang        MatMatMultSymbolic_MPIAIJ_MPIAIJ,
320626be0446SHong Zhang        MatMatMultNumeric_MPIAIJ_MPIAIJ,
3207cf3ca8ceSHong Zhang        MatPtAP_MPIAIJ_MPIAIJ,
3208cf3ca8ceSHong Zhang        MatPtAPSymbolic_MPIAIJ_MPIAIJ,
3209cf3ca8ceSHong Zhang /*94*/ MatPtAPNumeric_MPIAIJ_MPIAIJ,
32107a7894deSKris Buschelman        0,
32117a7894deSKris Buschelman        0,
32127a7894deSKris Buschelman        0,
32137a7894deSKris Buschelman        0,
3214d519adbfSMatthew Knepley /*99*/ 0,
3215d2b207f1SPeter Brune        0,
3216d2b207f1SPeter Brune        0,
32172fd7e33dSBarry Smith        MatConjugate_MPIAIJ,
32182fd7e33dSBarry Smith        0,
3219d519adbfSMatthew Knepley /*104*/MatSetValuesRow_MPIAIJ,
322099cafbc1SBarry Smith        MatRealPart_MPIAIJ,
322169db28dcSHong Zhang        MatImaginaryPart_MPIAIJ,
322269db28dcSHong Zhang        0,
322369db28dcSHong Zhang        0,
3224d519adbfSMatthew Knepley /*109*/0,
322503bc72f1SMatthew Knepley        MatGetRedundantMatrix_MPIAIJ,
32265494a064SHong Zhang        MatGetRowMin_MPIAIJ,
32275494a064SHong Zhang        0,
32285494a064SHong Zhang        0,
3229d1adec66SJed Brown /*114*/MatGetSeqNonzeroStructure_MPIAIJ,
3230bd0c2dcbSBarry Smith        0,
3231bd0c2dcbSBarry Smith        0,
3232bd0c2dcbSBarry Smith        0,
3233bd0c2dcbSBarry Smith        0,
32348fb81238SShri Abhyankar /*119*/0,
32358fb81238SShri Abhyankar        0,
32368fb81238SShri Abhyankar        0,
3237d6037b41SHong Zhang        0,
3238b9614d88SDmitry Karpeev        MatGetMultiProcBlock_MPIAIJ,
3239f2c98031SJed Brown /*124*/MatFindNonzeroRows_MPIAIJ,
32400716a85fSBarry Smith        MatGetColumnNorms_MPIAIJ,
3241bbead8a2SBarry Smith        MatInvertBlockDiagonal_MPIAIJ,
3242b9614d88SDmitry Karpeev        0,
324337868618SMatthew G Knepley        MatGetSubMatricesParallel_MPIAIJ,
3244187b3c17SHong Zhang /*129*/0,
3245187b3c17SHong Zhang        MatTransposeMatMult_MPIAIJ_MPIAIJ,
3246187b3c17SHong Zhang        MatTransposeMatMultSymbolic_MPIAIJ_MPIAIJ,
3247187b3c17SHong Zhang        MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ,
3248187b3c17SHong Zhang        0,
3249187b3c17SHong Zhang /*134*/0,
3250187b3c17SHong Zhang        0,
3251187b3c17SHong Zhang        0,
3252187b3c17SHong Zhang        0,
3253187b3c17SHong Zhang        0
3254bd0c2dcbSBarry Smith };
325536ce4990SBarry Smith 
32562e8a6d31SBarry Smith /* ----------------------------------------------------------------------------------------*/
32572e8a6d31SBarry Smith 
3258fb2e594dSBarry Smith EXTERN_C_BEGIN
32594a2ae208SSatish Balay #undef __FUNCT__
32604a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_MPIAIJ"
32617087cfbeSBarry Smith PetscErrorCode  MatStoreValues_MPIAIJ(Mat mat)
32622e8a6d31SBarry Smith {
32632e8a6d31SBarry Smith   Mat_MPIAIJ     *aij = (Mat_MPIAIJ *)mat->data;
3264dfbe8321SBarry Smith   PetscErrorCode ierr;
32652e8a6d31SBarry Smith 
32662e8a6d31SBarry Smith   PetscFunctionBegin;
32672e8a6d31SBarry Smith   ierr = MatStoreValues(aij->A);CHKERRQ(ierr);
32682e8a6d31SBarry Smith   ierr = MatStoreValues(aij->B);CHKERRQ(ierr);
32692e8a6d31SBarry Smith   PetscFunctionReturn(0);
32702e8a6d31SBarry Smith }
3271fb2e594dSBarry Smith EXTERN_C_END
32722e8a6d31SBarry Smith 
3273fb2e594dSBarry Smith EXTERN_C_BEGIN
32744a2ae208SSatish Balay #undef __FUNCT__
32754a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_MPIAIJ"
32767087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_MPIAIJ(Mat mat)
32772e8a6d31SBarry Smith {
32782e8a6d31SBarry Smith   Mat_MPIAIJ     *aij = (Mat_MPIAIJ *)mat->data;
3279dfbe8321SBarry Smith   PetscErrorCode ierr;
32802e8a6d31SBarry Smith 
32812e8a6d31SBarry Smith   PetscFunctionBegin;
32822e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr);
32832e8a6d31SBarry Smith   ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr);
32842e8a6d31SBarry Smith   PetscFunctionReturn(0);
32852e8a6d31SBarry Smith }
3286fb2e594dSBarry Smith EXTERN_C_END
32878a729477SBarry Smith 
328827508adbSBarry Smith EXTERN_C_BEGIN
32894a2ae208SSatish Balay #undef __FUNCT__
3290a23d5eceSKris Buschelman #define __FUNCT__ "MatMPIAIJSetPreallocation_MPIAIJ"
32917087cfbeSBarry Smith PetscErrorCode  MatMPIAIJSetPreallocation_MPIAIJ(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
3292a23d5eceSKris Buschelman {
3293a23d5eceSKris Buschelman   Mat_MPIAIJ     *b;
3294dfbe8321SBarry Smith   PetscErrorCode ierr;
3295b1d57f15SBarry Smith   PetscInt       i;
32962576faa2SJed Brown   PetscBool      d_realalloc = PETSC_FALSE,o_realalloc = PETSC_FALSE;
3297a23d5eceSKris Buschelman 
3298a23d5eceSKris Buschelman   PetscFunctionBegin;
32992576faa2SJed Brown   if (d_nz >= 0 || d_nnz) d_realalloc = PETSC_TRUE;
33002576faa2SJed Brown   if (o_nz >= 0 || o_nnz) o_realalloc = PETSC_TRUE;
3301a23d5eceSKris Buschelman   if (d_nz == PETSC_DEFAULT || d_nz == PETSC_DECIDE) d_nz = 5;
3302a23d5eceSKris Buschelman   if (o_nz == PETSC_DEFAULT || o_nz == PETSC_DECIDE) o_nz = 2;
3303e32f2f54SBarry Smith   if (d_nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"d_nz cannot be less than 0: value %D",d_nz);
3304e32f2f54SBarry Smith   if (o_nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"o_nz cannot be less than 0: value %D",o_nz);
3305899cda47SBarry Smith 
330626283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
330726283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3308a23d5eceSKris Buschelman   if (d_nnz) {
3309d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
3310e32f2f54SBarry Smith       if (d_nnz[i] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"d_nnz cannot be less than 0: local row %D value %D",i,d_nnz[i]);
3311a23d5eceSKris Buschelman     }
3312a23d5eceSKris Buschelman   }
3313a23d5eceSKris Buschelman   if (o_nnz) {
3314d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
3315e32f2f54SBarry Smith       if (o_nnz[i] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"o_nnz cannot be less than 0: local row %D value %D",i,o_nnz[i]);
3316a23d5eceSKris Buschelman     }
3317a23d5eceSKris Buschelman   }
3318a23d5eceSKris Buschelman   b = (Mat_MPIAIJ*)B->data;
3319899cda47SBarry Smith 
3320526dfc15SBarry Smith   if (!B->preallocated) {
3321899cda47SBarry Smith     /* Explicitly create 2 MATSEQAIJ matrices. */
3322899cda47SBarry Smith     ierr = MatCreate(PETSC_COMM_SELF,&b->A);CHKERRQ(ierr);
3323d0f46423SBarry Smith     ierr = MatSetSizes(b->A,B->rmap->n,B->cmap->n,B->rmap->n,B->cmap->n);CHKERRQ(ierr);
3324f9e9af59SJed Brown     ierr = MatSetBlockSizes(b->A,B->rmap->bs,B->cmap->bs);CHKERRQ(ierr);
3325899cda47SBarry Smith     ierr = MatSetType(b->A,MATSEQAIJ);CHKERRQ(ierr);
3326899cda47SBarry Smith     ierr = PetscLogObjectParent(B,b->A);CHKERRQ(ierr);
3327899cda47SBarry Smith     ierr = MatCreate(PETSC_COMM_SELF,&b->B);CHKERRQ(ierr);
3328d0f46423SBarry Smith     ierr = MatSetSizes(b->B,B->rmap->n,B->cmap->N,B->rmap->n,B->cmap->N);CHKERRQ(ierr);
3329f9e9af59SJed Brown     ierr = MatSetBlockSizes(b->B,B->rmap->bs,B->cmap->bs);CHKERRQ(ierr);
3330899cda47SBarry Smith     ierr = MatSetType(b->B,MATSEQAIJ);CHKERRQ(ierr);
3331899cda47SBarry Smith     ierr = PetscLogObjectParent(B,b->B);CHKERRQ(ierr);
3332526dfc15SBarry Smith   }
3333899cda47SBarry Smith 
3334c60e587dSKris Buschelman   ierr = MatSeqAIJSetPreallocation(b->A,d_nz,d_nnz);CHKERRQ(ierr);
3335c60e587dSKris Buschelman   ierr = MatSeqAIJSetPreallocation(b->B,o_nz,o_nnz);CHKERRQ(ierr);
33362576faa2SJed Brown   /* Do not error if the user did not give real preallocation information. Ugly because this would overwrite a previous user call to MatSetOption(). */
33372576faa2SJed Brown   if (!d_realalloc) {ierr = MatSetOption(b->A,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr);}
33382576faa2SJed Brown   if (!o_realalloc) {ierr = MatSetOption(b->B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr);}
3339526dfc15SBarry Smith   B->preallocated = PETSC_TRUE;
3340a23d5eceSKris Buschelman   PetscFunctionReturn(0);
3341a23d5eceSKris Buschelman }
3342a23d5eceSKris Buschelman EXTERN_C_END
3343a23d5eceSKris Buschelman 
33444a2ae208SSatish Balay #undef __FUNCT__
33454a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_MPIAIJ"
3346dfbe8321SBarry Smith PetscErrorCode MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
3347d6dfbf8fSBarry Smith {
3348d6dfbf8fSBarry Smith   Mat            mat;
3349416022c9SBarry Smith   Mat_MPIAIJ     *a,*oldmat = (Mat_MPIAIJ*)matin->data;
3350dfbe8321SBarry Smith   PetscErrorCode ierr;
3351d6dfbf8fSBarry Smith 
33523a40ed3dSBarry Smith   PetscFunctionBegin;
3353416022c9SBarry Smith   *newmat       = 0;
33547adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)matin)->comm,&mat);CHKERRQ(ierr);
3355d0f46423SBarry Smith   ierr = MatSetSizes(mat,matin->rmap->n,matin->cmap->n,matin->rmap->N,matin->cmap->N);CHKERRQ(ierr);
3356a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(mat,matin->rmap->bs,matin->cmap->bs);CHKERRQ(ierr);
33577adad957SLisandro Dalcin   ierr = MatSetType(mat,((PetscObject)matin)->type_name);CHKERRQ(ierr);
33581d5dac46SHong Zhang   ierr = PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
3359273d9f13SBarry Smith   a    = (Mat_MPIAIJ*)mat->data;
3360e1b6402fSHong Zhang 
3361d5f3da31SBarry Smith   mat->factortype    = matin->factortype;
3362d0f46423SBarry Smith   mat->rmap->bs      = matin->rmap->bs;
3363a2f3521dSMark F. Adams   mat->cmap->bs      = matin->cmap->bs;
3364c456f294SBarry Smith   mat->assembled    = PETSC_TRUE;
3365e7641de0SSatish Balay   mat->insertmode   = NOT_SET_VALUES;
3366273d9f13SBarry Smith   mat->preallocated = PETSC_TRUE;
3367d6dfbf8fSBarry Smith 
336817699dbbSLois Curfman McInnes   a->size           = oldmat->size;
336917699dbbSLois Curfman McInnes   a->rank           = oldmat->rank;
3370e7641de0SSatish Balay   a->donotstash     = oldmat->donotstash;
3371e7641de0SSatish Balay   a->roworiented    = oldmat->roworiented;
3372e7641de0SSatish Balay   a->rowindices     = 0;
3373bcd2baecSBarry Smith   a->rowvalues      = 0;
3374bcd2baecSBarry Smith   a->getrowactive   = PETSC_FALSE;
3375d6dfbf8fSBarry Smith 
33761e1e43feSBarry Smith   ierr = PetscLayoutReference(matin->rmap,&mat->rmap);CHKERRQ(ierr);
33771e1e43feSBarry Smith   ierr = PetscLayoutReference(matin->cmap,&mat->cmap);CHKERRQ(ierr);
3378899cda47SBarry Smith 
33792ee70a88SLois Curfman McInnes   if (oldmat->colmap) {
3380aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
33810f5bd95cSBarry Smith     ierr = PetscTableCreateCopy(oldmat->colmap,&a->colmap);CHKERRQ(ierr);
3382b1fc9764SSatish Balay #else
3383d0f46423SBarry Smith     ierr = PetscMalloc((mat->cmap->N)*sizeof(PetscInt),&a->colmap);CHKERRQ(ierr);
3384d0f46423SBarry Smith     ierr = PetscLogObjectMemory(mat,(mat->cmap->N)*sizeof(PetscInt));CHKERRQ(ierr);
3385d0f46423SBarry Smith     ierr = PetscMemcpy(a->colmap,oldmat->colmap,(mat->cmap->N)*sizeof(PetscInt));CHKERRQ(ierr);
3386b1fc9764SSatish Balay #endif
3387416022c9SBarry Smith   } else a->colmap = 0;
33883f41c07dSBarry Smith   if (oldmat->garray) {
3389b1d57f15SBarry Smith     PetscInt len;
3390d0f46423SBarry Smith     len  = oldmat->B->cmap->n;
3391b1d57f15SBarry Smith     ierr = PetscMalloc((len+1)*sizeof(PetscInt),&a->garray);CHKERRQ(ierr);
339252e6d16bSBarry Smith     ierr = PetscLogObjectMemory(mat,len*sizeof(PetscInt));CHKERRQ(ierr);
3393b1d57f15SBarry Smith     if (len) { ierr = PetscMemcpy(a->garray,oldmat->garray,len*sizeof(PetscInt));CHKERRQ(ierr); }
3394416022c9SBarry Smith   } else a->garray = 0;
3395d6dfbf8fSBarry Smith 
3396416022c9SBarry Smith   ierr = VecDuplicate(oldmat->lvec,&a->lvec);CHKERRQ(ierr);
339752e6d16bSBarry Smith   ierr = PetscLogObjectParent(mat,a->lvec);CHKERRQ(ierr);
3398a56f8943SBarry Smith   ierr = VecScatterCopy(oldmat->Mvctx,&a->Mvctx);CHKERRQ(ierr);
339952e6d16bSBarry Smith   ierr = PetscLogObjectParent(mat,a->Mvctx);CHKERRQ(ierr);
34002e8a6d31SBarry Smith   ierr = MatDuplicate(oldmat->A,cpvalues,&a->A);CHKERRQ(ierr);
340152e6d16bSBarry Smith   ierr = PetscLogObjectParent(mat,a->A);CHKERRQ(ierr);
34022e8a6d31SBarry Smith   ierr = MatDuplicate(oldmat->B,cpvalues,&a->B);CHKERRQ(ierr);
340352e6d16bSBarry Smith   ierr = PetscLogObjectParent(mat,a->B);CHKERRQ(ierr);
3404140e18c1SBarry Smith   ierr = PetscFunctionListDuplicate(((PetscObject)matin)->qlist,&((PetscObject)mat)->qlist);CHKERRQ(ierr);
34058a729477SBarry Smith   *newmat = mat;
34063a40ed3dSBarry Smith   PetscFunctionReturn(0);
34078a729477SBarry Smith }
3408416022c9SBarry Smith 
34091a4ee126SBarry Smith 
34101a4ee126SBarry Smith 
34114a2ae208SSatish Balay #undef __FUNCT__
34125bba2384SShri Abhyankar #define __FUNCT__ "MatLoad_MPIAIJ"
3413112444f4SShri Abhyankar PetscErrorCode MatLoad_MPIAIJ(Mat newMat, PetscViewer viewer)
34148fb81238SShri Abhyankar {
34158fb81238SShri Abhyankar   PetscScalar    *vals,*svals;
34168fb81238SShri Abhyankar   MPI_Comm       comm = ((PetscObject)viewer)->comm;
34178fb81238SShri Abhyankar   PetscErrorCode ierr;
34181a4ee126SBarry Smith   PetscMPIInt    rank,size,tag = ((PetscObject)viewer)->tag;
34198fb81238SShri Abhyankar   PetscInt       i,nz,j,rstart,rend,mmax,maxnz = 0,grows,gcols;
34208fb81238SShri Abhyankar   PetscInt       header[4],*rowlengths = 0,M,N,m,*cols;
34218fb81238SShri Abhyankar   PetscInt       *ourlens = PETSC_NULL,*procsnz = PETSC_NULL,*offlens = PETSC_NULL,jj,*mycols,*smycols;
34228fb81238SShri Abhyankar   PetscInt       cend,cstart,n,*rowners,sizesset=1;
34238fb81238SShri Abhyankar   int            fd;
342408ea439dSMark F. Adams   PetscInt       bs = 1;
34258fb81238SShri Abhyankar 
34268fb81238SShri Abhyankar   PetscFunctionBegin;
34278fb81238SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
34288fb81238SShri Abhyankar   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
34298fb81238SShri Abhyankar   if (!rank) {
34308fb81238SShri Abhyankar     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
34318fb81238SShri Abhyankar     ierr = PetscBinaryRead(fd,(char *)header,4,PETSC_INT);CHKERRQ(ierr);
34328fb81238SShri Abhyankar     if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
34338fb81238SShri Abhyankar   }
34348fb81238SShri Abhyankar 
343508ea439dSMark F. Adams   ierr = PetscOptionsBegin(comm,PETSC_NULL,"Options for loading SEQAIJ matrix","Mat");CHKERRQ(ierr);
343608ea439dSMark F. Adams   ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,PETSC_NULL);CHKERRQ(ierr);
343708ea439dSMark F. Adams   ierr = PetscOptionsEnd();CHKERRQ(ierr);
343808ea439dSMark F. Adams 
34398fb81238SShri Abhyankar   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) sizesset = 0;
34408fb81238SShri Abhyankar 
34418fb81238SShri Abhyankar   ierr = MPI_Bcast(header+1,3,MPIU_INT,0,comm);CHKERRQ(ierr);
34428fb81238SShri Abhyankar   M = header[1]; N = header[2];
34438fb81238SShri Abhyankar   /* If global rows/cols are set to PETSC_DECIDE, set it to the sizes given in the file */
34448fb81238SShri Abhyankar   if (sizesset && newMat->rmap->N < 0) newMat->rmap->N = M;
34458fb81238SShri Abhyankar   if (sizesset && newMat->cmap->N < 0) newMat->cmap->N = N;
34468fb81238SShri Abhyankar 
34478fb81238SShri Abhyankar   /* If global sizes are set, check if they are consistent with that given in the file */
34488fb81238SShri Abhyankar   if (sizesset) {
34498fb81238SShri Abhyankar     ierr = MatGetSize(newMat,&grows,&gcols);CHKERRQ(ierr);
34508fb81238SShri Abhyankar   }
3451abd38a8fSBarry 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);
3452abd38a8fSBarry 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);
34538fb81238SShri Abhyankar 
345408ea439dSMark F. Adams   /* determine ownership of all (block) rows */
345508ea439dSMark F. Adams   if (M%bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Inconsistent # of rows (%d) and block size (%d)",M,bs);
345608ea439dSMark F. Adams   if (newMat->rmap->n < 0) m    = bs*((M/bs)/size + (((M/bs) % size) > rank)); /* PETSC_DECIDE */
34574683f7a4SShri Abhyankar   else m = newMat->rmap->n; /* Set by user */
34588fb81238SShri Abhyankar 
34598fb81238SShri Abhyankar   ierr = PetscMalloc((size+1)*sizeof(PetscInt),&rowners);CHKERRQ(ierr);
34608fb81238SShri Abhyankar   ierr = MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);CHKERRQ(ierr);
34618fb81238SShri Abhyankar 
34628fb81238SShri Abhyankar   /* First process needs enough room for process with most rows */
34638fb81238SShri Abhyankar   if (!rank) {
34648fb81238SShri Abhyankar     mmax = rowners[1];
34655c4ea359SMatthew G Knepley     for (i=2; i<=size; i++) {
34668fb81238SShri Abhyankar       mmax = PetscMax(mmax, rowners[i]);
34678fb81238SShri Abhyankar     }
34688fb81238SShri Abhyankar   } else mmax = m;
34698fb81238SShri Abhyankar 
34708fb81238SShri Abhyankar   rowners[0] = 0;
34718fb81238SShri Abhyankar   for (i=2; i<=size; i++) {
34728fb81238SShri Abhyankar     rowners[i] += rowners[i-1];
34738fb81238SShri Abhyankar   }
34748fb81238SShri Abhyankar   rstart = rowners[rank];
34758fb81238SShri Abhyankar   rend   = rowners[rank+1];
34768fb81238SShri Abhyankar 
34778fb81238SShri Abhyankar   /* distribute row lengths to all processors */
34788fb81238SShri Abhyankar   ierr    = PetscMalloc2(mmax,PetscInt,&ourlens,mmax,PetscInt,&offlens);CHKERRQ(ierr);
34798fb81238SShri Abhyankar   if (!rank) {
34808fb81238SShri Abhyankar     ierr = PetscBinaryRead(fd,ourlens,m,PETSC_INT);CHKERRQ(ierr);
34815c4ea359SMatthew G Knepley     ierr = PetscMalloc(mmax*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
34828fb81238SShri Abhyankar     ierr = PetscMalloc(size*sizeof(PetscInt),&procsnz);CHKERRQ(ierr);
34838fb81238SShri Abhyankar     ierr = PetscMemzero(procsnz,size*sizeof(PetscInt));CHKERRQ(ierr);
34848fb81238SShri Abhyankar     for (j=0; j<m; j++) {
34858fb81238SShri Abhyankar       procsnz[0] += ourlens[j];
34868fb81238SShri Abhyankar     }
34878fb81238SShri Abhyankar     for (i=1; i<size; i++) {
34888fb81238SShri Abhyankar       ierr = PetscBinaryRead(fd,rowlengths,rowners[i+1]-rowners[i],PETSC_INT);CHKERRQ(ierr);
34898fb81238SShri Abhyankar       /* calculate the number of nonzeros on each processor */
34908fb81238SShri Abhyankar       for (j=0; j<rowners[i+1]-rowners[i]; j++) {
34918fb81238SShri Abhyankar         procsnz[i] += rowlengths[j];
34928fb81238SShri Abhyankar       }
3493a25532f0SBarry Smith       ierr = MPIULong_Send(rowlengths,rowners[i+1]-rowners[i],MPIU_INT,i,tag,comm);CHKERRQ(ierr);
34948fb81238SShri Abhyankar     }
34958fb81238SShri Abhyankar     ierr = PetscFree(rowlengths);CHKERRQ(ierr);
34968fb81238SShri Abhyankar   } else {
3497a25532f0SBarry Smith     ierr = MPIULong_Recv(ourlens,m,MPIU_INT,0,tag,comm);CHKERRQ(ierr);
34988fb81238SShri Abhyankar   }
34998fb81238SShri Abhyankar 
35008fb81238SShri Abhyankar   if (!rank) {
35018fb81238SShri Abhyankar     /* determine max buffer needed and allocate it */
35028fb81238SShri Abhyankar     maxnz = 0;
35038fb81238SShri Abhyankar     for (i=0; i<size; i++) {
35048fb81238SShri Abhyankar       maxnz = PetscMax(maxnz,procsnz[i]);
35058fb81238SShri Abhyankar     }
35068fb81238SShri Abhyankar     ierr = PetscMalloc(maxnz*sizeof(PetscInt),&cols);CHKERRQ(ierr);
35078fb81238SShri Abhyankar 
35088fb81238SShri Abhyankar     /* read in my part of the matrix column indices  */
35098fb81238SShri Abhyankar     nz   = procsnz[0];
35108fb81238SShri Abhyankar     ierr = PetscMalloc(nz*sizeof(PetscInt),&mycols);CHKERRQ(ierr);
35118fb81238SShri Abhyankar     ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr);
35128fb81238SShri Abhyankar 
35138fb81238SShri Abhyankar     /* read in every one elses and ship off */
35148fb81238SShri Abhyankar     for (i=1; i<size; i++) {
35158fb81238SShri Abhyankar       nz     = procsnz[i];
35168fb81238SShri Abhyankar       ierr   = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr);
3517a25532f0SBarry Smith       ierr   = MPIULong_Send(cols,nz,MPIU_INT,i,tag,comm);CHKERRQ(ierr);
35188fb81238SShri Abhyankar     }
35198fb81238SShri Abhyankar     ierr = PetscFree(cols);CHKERRQ(ierr);
35208fb81238SShri Abhyankar   } else {
35218fb81238SShri Abhyankar     /* determine buffer space needed for message */
35228fb81238SShri Abhyankar     nz = 0;
35238fb81238SShri Abhyankar     for (i=0; i<m; i++) {
35248fb81238SShri Abhyankar       nz += ourlens[i];
35258fb81238SShri Abhyankar     }
35268fb81238SShri Abhyankar     ierr = PetscMalloc(nz*sizeof(PetscInt),&mycols);CHKERRQ(ierr);
35278fb81238SShri Abhyankar 
35288fb81238SShri Abhyankar     /* receive message of column indices*/
3529a25532f0SBarry Smith     ierr = MPIULong_Recv(mycols,nz,MPIU_INT,0,tag,comm);CHKERRQ(ierr);
35308fb81238SShri Abhyankar   }
35318fb81238SShri Abhyankar 
35328fb81238SShri Abhyankar   /* determine column ownership if matrix is not square */
35338fb81238SShri Abhyankar   if (N != M) {
35348fb81238SShri Abhyankar     if (newMat->cmap->n < 0) n      = N/size + ((N % size) > rank);
35358fb81238SShri Abhyankar     else n = newMat->cmap->n;
35368fb81238SShri Abhyankar     ierr   = MPI_Scan(&n,&cend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr);
35378fb81238SShri Abhyankar     cstart = cend - n;
35388fb81238SShri Abhyankar   } else {
35398fb81238SShri Abhyankar     cstart = rstart;
35408fb81238SShri Abhyankar     cend   = rend;
35418fb81238SShri Abhyankar     n      = cend - cstart;
35428fb81238SShri Abhyankar   }
35438fb81238SShri Abhyankar 
35448fb81238SShri Abhyankar   /* loop over local rows, determining number of off diagonal entries */
35458fb81238SShri Abhyankar   ierr = PetscMemzero(offlens,m*sizeof(PetscInt));CHKERRQ(ierr);
35468fb81238SShri Abhyankar   jj = 0;
35478fb81238SShri Abhyankar   for (i=0; i<m; i++) {
35488fb81238SShri Abhyankar     for (j=0; j<ourlens[i]; j++) {
35498fb81238SShri Abhyankar       if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++;
35508fb81238SShri Abhyankar       jj++;
35518fb81238SShri Abhyankar     }
35528fb81238SShri Abhyankar   }
35538fb81238SShri Abhyankar 
35548fb81238SShri Abhyankar   for (i=0; i<m; i++) {
35558fb81238SShri Abhyankar     ourlens[i] -= offlens[i];
35568fb81238SShri Abhyankar   }
35578fb81238SShri Abhyankar   if (!sizesset) {
35588fb81238SShri Abhyankar     ierr = MatSetSizes(newMat,m,n,M,N);CHKERRQ(ierr);
35598fb81238SShri Abhyankar   }
356008ea439dSMark F. Adams 
356108ea439dSMark F. Adams   if (bs > 1) {ierr = MatSetBlockSize(newMat,bs);CHKERRQ(ierr);}
356208ea439dSMark F. Adams 
35638fb81238SShri Abhyankar   ierr = MatMPIAIJSetPreallocation(newMat,0,ourlens,0,offlens);CHKERRQ(ierr);
35648fb81238SShri Abhyankar 
35658fb81238SShri Abhyankar   for (i=0; i<m; i++) {
35668fb81238SShri Abhyankar     ourlens[i] += offlens[i];
35678fb81238SShri Abhyankar   }
35688fb81238SShri Abhyankar 
35698fb81238SShri Abhyankar   if (!rank) {
35708fb81238SShri Abhyankar     ierr = PetscMalloc((maxnz+1)*sizeof(PetscScalar),&vals);CHKERRQ(ierr);
35718fb81238SShri Abhyankar 
35728fb81238SShri Abhyankar     /* read in my part of the matrix numerical values  */
35738fb81238SShri Abhyankar     nz   = procsnz[0];
35748fb81238SShri Abhyankar     ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
35758fb81238SShri Abhyankar 
35768fb81238SShri Abhyankar     /* insert into matrix */
35778fb81238SShri Abhyankar     jj      = rstart;
35788fb81238SShri Abhyankar     smycols = mycols;
35798fb81238SShri Abhyankar     svals   = vals;
35808fb81238SShri Abhyankar     for (i=0; i<m; i++) {
35818fb81238SShri Abhyankar       ierr = MatSetValues_MPIAIJ(newMat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
35828fb81238SShri Abhyankar       smycols += ourlens[i];
35838fb81238SShri Abhyankar       svals   += ourlens[i];
35848fb81238SShri Abhyankar       jj++;
35858fb81238SShri Abhyankar     }
35868fb81238SShri Abhyankar 
35878fb81238SShri Abhyankar     /* read in other processors and ship out */
35888fb81238SShri Abhyankar     for (i=1; i<size; i++) {
35898fb81238SShri Abhyankar       nz     = procsnz[i];
35908fb81238SShri Abhyankar       ierr   = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
3591a25532f0SBarry Smith       ierr   = MPIULong_Send(vals,nz,MPIU_SCALAR,i,((PetscObject)newMat)->tag,comm);CHKERRQ(ierr);
35928fb81238SShri Abhyankar     }
35938fb81238SShri Abhyankar     ierr = PetscFree(procsnz);CHKERRQ(ierr);
35948fb81238SShri Abhyankar   } else {
35958fb81238SShri Abhyankar     /* receive numeric values */
35968fb81238SShri Abhyankar     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&vals);CHKERRQ(ierr);
35978fb81238SShri Abhyankar 
35988fb81238SShri Abhyankar     /* receive message of values*/
3599a25532f0SBarry Smith     ierr   = MPIULong_Recv(vals,nz,MPIU_SCALAR,0,((PetscObject)newMat)->tag,comm);CHKERRQ(ierr);
36008fb81238SShri Abhyankar 
36018fb81238SShri Abhyankar     /* insert into matrix */
36028fb81238SShri Abhyankar     jj      = rstart;
36038fb81238SShri Abhyankar     smycols = mycols;
36048fb81238SShri Abhyankar     svals   = vals;
36058fb81238SShri Abhyankar     for (i=0; i<m; i++) {
36068fb81238SShri Abhyankar       ierr     = MatSetValues_MPIAIJ(newMat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr);
36078fb81238SShri Abhyankar       smycols += ourlens[i];
36088fb81238SShri Abhyankar       svals   += ourlens[i];
36098fb81238SShri Abhyankar       jj++;
36108fb81238SShri Abhyankar     }
36118fb81238SShri Abhyankar   }
36128fb81238SShri Abhyankar   ierr = PetscFree2(ourlens,offlens);CHKERRQ(ierr);
36138fb81238SShri Abhyankar   ierr = PetscFree(vals);CHKERRQ(ierr);
36148fb81238SShri Abhyankar   ierr = PetscFree(mycols);CHKERRQ(ierr);
36158fb81238SShri Abhyankar   ierr = PetscFree(rowners);CHKERRQ(ierr);
36168fb81238SShri Abhyankar   ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
36178fb81238SShri Abhyankar   ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
361808ea439dSMark F. Adams 
36198fb81238SShri Abhyankar   PetscFunctionReturn(0);
36208fb81238SShri Abhyankar }
36218fb81238SShri Abhyankar 
36228fb81238SShri Abhyankar #undef __FUNCT__
36234a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_MPIAIJ"
36244aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,MatReuse call,Mat *newmat)
36254aa3045dSJed Brown {
36264aa3045dSJed Brown   PetscErrorCode ierr;
36274aa3045dSJed Brown   IS             iscol_local;
36284aa3045dSJed Brown   PetscInt       csize;
36294aa3045dSJed Brown 
36304aa3045dSJed Brown   PetscFunctionBegin;
36314aa3045dSJed Brown   ierr = ISGetLocalSize(iscol,&csize);CHKERRQ(ierr);
3632b79d0421SJed Brown   if (call == MAT_REUSE_MATRIX) {
3633b79d0421SJed Brown     ierr = PetscObjectQuery((PetscObject)*newmat,"ISAllGather",(PetscObject*)&iscol_local);CHKERRQ(ierr);
3634e32f2f54SBarry Smith     if (!iscol_local) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
3635b79d0421SJed Brown   } else {
3636c5bfad50SMark F. Adams     PetscInt cbs;
3637c5bfad50SMark F. Adams     ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
36384aa3045dSJed Brown     ierr = ISAllGather(iscol,&iscol_local);CHKERRQ(ierr);
3639c5bfad50SMark F. Adams     ierr = ISSetBlockSize(iscol_local,cbs);CHKERRQ(ierr);
3640b79d0421SJed Brown   }
36414aa3045dSJed Brown   ierr = MatGetSubMatrix_MPIAIJ_Private(mat,isrow,iscol_local,csize,call,newmat);CHKERRQ(ierr);
3642b79d0421SJed Brown   if (call == MAT_INITIAL_MATRIX) {
3643b79d0421SJed Brown     ierr = PetscObjectCompose((PetscObject)*newmat,"ISAllGather",(PetscObject)iscol_local);CHKERRQ(ierr);
36446bf464f9SBarry Smith     ierr = ISDestroy(&iscol_local);CHKERRQ(ierr);
3645b79d0421SJed Brown   }
36464aa3045dSJed Brown   PetscFunctionReturn(0);
36474aa3045dSJed Brown }
36484aa3045dSJed Brown 
364929dcf524SDmitry Karpeev extern PetscErrorCode MatGetSubMatrices_MPIAIJ_Local(Mat,PetscInt,const IS[],const IS[],MatReuse,PetscBool*,Mat*);
36504aa3045dSJed Brown #undef __FUNCT__
36514aa3045dSJed Brown #define __FUNCT__ "MatGetSubMatrix_MPIAIJ_Private"
3652a0ff6018SBarry Smith /*
365329da9460SBarry Smith     Not great since it makes two copies of the submatrix, first an SeqAIJ
365429da9460SBarry Smith   in local and then by concatenating the local matrices the end result.
365529da9460SBarry Smith   Writing it directly would be much like MatGetSubMatrices_MPIAIJ()
36564aa3045dSJed Brown 
36574aa3045dSJed Brown   Note: This requires a sequential iscol with all indices.
3658a0ff6018SBarry Smith */
36594aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIAIJ_Private(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse call,Mat *newmat)
3660a0ff6018SBarry Smith {
3661dfbe8321SBarry Smith   PetscErrorCode ierr;
366232dcc486SBarry Smith   PetscMPIInt    rank,size;
3663a2f3521dSMark F. Adams   PetscInt       i,m,n,rstart,row,rend,nz,*cwork,j,bs,cbs;
366429dcf524SDmitry Karpeev   PetscInt       *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal,ncol;
366529dcf524SDmitry Karpeev   PetscBool      allcolumns, colflag;
366629dcf524SDmitry Karpeev   Mat            M,Mreuse;
3667a77337e4SBarry Smith   MatScalar      *vwork,*aa;
36687adad957SLisandro Dalcin   MPI_Comm       comm = ((PetscObject)mat)->comm;
366900e6dbe6SBarry Smith   Mat_SeqAIJ     *aij;
36707e2c5f70SBarry Smith 
3671a0ff6018SBarry Smith 
3672a0ff6018SBarry Smith   PetscFunctionBegin;
36731dab6e02SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
36741dab6e02SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
367500e6dbe6SBarry Smith 
367629dcf524SDmitry Karpeev   ierr = ISIdentity(iscol,&colflag);CHKERRQ(ierr);
367729dcf524SDmitry Karpeev   ierr = ISGetLocalSize(iscol,&ncol);CHKERRQ(ierr);
367829dcf524SDmitry Karpeev   if (colflag && ncol == mat->cmap->N) {
367929dcf524SDmitry Karpeev     allcolumns = PETSC_TRUE;
368029dcf524SDmitry Karpeev   } else {
368129dcf524SDmitry Karpeev     allcolumns = PETSC_FALSE;
368229dcf524SDmitry Karpeev   }
3683fee21e36SBarry Smith   if (call ==  MAT_REUSE_MATRIX) {
3684fee21e36SBarry Smith     ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);CHKERRQ(ierr);
3685e32f2f54SBarry Smith     if (!Mreuse) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
368629dcf524SDmitry Karpeev     ierr  = MatGetSubMatrices_MPIAIJ_Local(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&allcolumns,&Mreuse);CHKERRQ(ierr);
3687fee21e36SBarry Smith   } else {
368829dcf524SDmitry Karpeev     ierr   = MatGetSubMatrices_MPIAIJ_Local(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&allcolumns,&Mreuse);CHKERRQ(ierr);
3689fee21e36SBarry Smith   }
3690a0ff6018SBarry Smith 
3691a0ff6018SBarry Smith   /*
3692a0ff6018SBarry Smith       m - number of local rows
3693a0ff6018SBarry Smith       n - number of columns (same on all processors)
3694a0ff6018SBarry Smith       rstart - first row in new global matrix generated
3695a0ff6018SBarry Smith   */
3696fee21e36SBarry Smith   ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr);
3697a2f3521dSMark F. Adams   ierr = MatGetBlockSizes(Mreuse,&bs,&cbs);CHKERRQ(ierr);
3698a0ff6018SBarry Smith   if (call == MAT_INITIAL_MATRIX) {
3699fee21e36SBarry Smith     aij = (Mat_SeqAIJ*)(Mreuse)->data;
370000e6dbe6SBarry Smith     ii  = aij->i;
370100e6dbe6SBarry Smith     jj  = aij->j;
370200e6dbe6SBarry Smith 
3703a0ff6018SBarry Smith     /*
370400e6dbe6SBarry Smith         Determine the number of non-zeros in the diagonal and off-diagonal
370500e6dbe6SBarry Smith         portions of the matrix in order to do correct preallocation
3706a0ff6018SBarry Smith     */
370700e6dbe6SBarry Smith 
370800e6dbe6SBarry Smith     /* first get start and end of "diagonal" columns */
37096a6a5d1dSBarry Smith     if (csize == PETSC_DECIDE) {
3710ab50ec6bSBarry Smith       ierr = ISGetSize(isrow,&mglobal);CHKERRQ(ierr);
3711ab50ec6bSBarry Smith       if (mglobal == n) { /* square matrix */
3712e2c4fddaSBarry Smith         nlocal = m;
37136a6a5d1dSBarry Smith       } else {
3714ab50ec6bSBarry Smith         nlocal = n/size + ((n % size) > rank);
3715ab50ec6bSBarry Smith       }
3716ab50ec6bSBarry Smith     } else {
37176a6a5d1dSBarry Smith       nlocal = csize;
37186a6a5d1dSBarry Smith     }
3719b1d57f15SBarry Smith     ierr   = MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr);
372000e6dbe6SBarry Smith     rstart = rend - nlocal;
372165e19b50SBarry 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);
372200e6dbe6SBarry Smith 
372300e6dbe6SBarry Smith     /* next, compute all the lengths */
3724b1d57f15SBarry Smith     ierr  = PetscMalloc((2*m+1)*sizeof(PetscInt),&dlens);CHKERRQ(ierr);
372500e6dbe6SBarry Smith     olens = dlens + m;
372600e6dbe6SBarry Smith     for (i=0; i<m; i++) {
372700e6dbe6SBarry Smith       jend = ii[i+1] - ii[i];
372800e6dbe6SBarry Smith       olen = 0;
372900e6dbe6SBarry Smith       dlen = 0;
373000e6dbe6SBarry Smith       for (j=0; j<jend; j++) {
373100e6dbe6SBarry Smith         if (*jj < rstart || *jj >= rend) olen++;
373200e6dbe6SBarry Smith         else dlen++;
373300e6dbe6SBarry Smith         jj++;
373400e6dbe6SBarry Smith       }
373500e6dbe6SBarry Smith       olens[i] = olen;
373600e6dbe6SBarry Smith       dlens[i] = dlen;
373700e6dbe6SBarry Smith     }
3738f69a0ea3SMatthew Knepley     ierr = MatCreate(comm,&M);CHKERRQ(ierr);
3739f69a0ea3SMatthew Knepley     ierr = MatSetSizes(M,m,nlocal,PETSC_DECIDE,n);CHKERRQ(ierr);
3740a2f3521dSMark F. Adams     ierr = MatSetBlockSizes(M,bs,cbs);CHKERRQ(ierr);
37417adad957SLisandro Dalcin     ierr = MatSetType(M,((PetscObject)mat)->type_name);CHKERRQ(ierr);
3742e2d9671bSKris Buschelman     ierr = MatMPIAIJSetPreallocation(M,0,dlens,0,olens);CHKERRQ(ierr);
3743606d414cSSatish Balay     ierr = PetscFree(dlens);CHKERRQ(ierr);
3744a0ff6018SBarry Smith   } else {
3745b1d57f15SBarry Smith     PetscInt ml,nl;
3746a0ff6018SBarry Smith 
3747a0ff6018SBarry Smith     M = *newmat;
3748a0ff6018SBarry Smith     ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr);
3749e32f2f54SBarry Smith     if (ml != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
3750a0ff6018SBarry Smith     ierr = MatZeroEntries(M);CHKERRQ(ierr);
3751c48de900SBarry Smith     /*
3752c48de900SBarry Smith          The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
3753c48de900SBarry Smith        rather than the slower MatSetValues().
3754c48de900SBarry Smith     */
3755c48de900SBarry Smith     M->was_assembled = PETSC_TRUE;
3756c48de900SBarry Smith     M->assembled     = PETSC_FALSE;
3757a0ff6018SBarry Smith   }
3758a0ff6018SBarry Smith   ierr = MatGetOwnershipRange(M,&rstart,&rend);CHKERRQ(ierr);
3759fee21e36SBarry Smith   aij = (Mat_SeqAIJ*)(Mreuse)->data;
376000e6dbe6SBarry Smith   ii  = aij->i;
376100e6dbe6SBarry Smith   jj  = aij->j;
376200e6dbe6SBarry Smith   aa  = aij->a;
3763a0ff6018SBarry Smith   for (i=0; i<m; i++) {
3764a0ff6018SBarry Smith     row   = rstart + i;
376500e6dbe6SBarry Smith     nz    = ii[i+1] - ii[i];
376600e6dbe6SBarry Smith     cwork = jj;     jj += nz;
376700e6dbe6SBarry Smith     vwork = aa;     aa += nz;
37688c638d02SBarry Smith     ierr = MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr);
3769a0ff6018SBarry Smith   }
3770a0ff6018SBarry Smith 
3771a0ff6018SBarry Smith   ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3772a0ff6018SBarry Smith   ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3773a0ff6018SBarry Smith   *newmat = M;
3774fee21e36SBarry Smith 
3775fee21e36SBarry Smith   /* save submatrix used in processor for next request */
3776fee21e36SBarry Smith   if (call ==  MAT_INITIAL_MATRIX) {
3777fee21e36SBarry Smith     ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr);
3778bf0cc555SLisandro Dalcin     ierr = MatDestroy(&Mreuse);CHKERRQ(ierr);
3779fee21e36SBarry Smith   }
3780fee21e36SBarry Smith 
3781a0ff6018SBarry Smith   PetscFunctionReturn(0);
3782a0ff6018SBarry Smith }
3783273d9f13SBarry Smith 
3784e2e86b8fSSatish Balay EXTERN_C_BEGIN
37854a2ae208SSatish Balay #undef __FUNCT__
3786ccd8e176SBarry Smith #define __FUNCT__ "MatMPIAIJSetPreallocationCSR_MPIAIJ"
37877087cfbeSBarry Smith PetscErrorCode  MatMPIAIJSetPreallocationCSR_MPIAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3788ccd8e176SBarry Smith {
3789899cda47SBarry Smith   PetscInt       m,cstart, cend,j,nnz,i,d;
3790899cda47SBarry Smith   PetscInt       *d_nnz,*o_nnz,nnz_max = 0,rstart,ii;
3791ccd8e176SBarry Smith   const PetscInt *JJ;
3792ccd8e176SBarry Smith   PetscScalar    *values;
3793ccd8e176SBarry Smith   PetscErrorCode ierr;
3794ccd8e176SBarry Smith 
3795ccd8e176SBarry Smith   PetscFunctionBegin;
3796e32f2f54SBarry Smith   if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Ii[0] must be 0 it is %D",Ii[0]);
3797899cda47SBarry Smith 
379826283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
379926283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3800d0f46423SBarry Smith   m      = B->rmap->n;
3801d0f46423SBarry Smith   cstart = B->cmap->rstart;
3802d0f46423SBarry Smith   cend   = B->cmap->rend;
3803d0f46423SBarry Smith   rstart = B->rmap->rstart;
3804899cda47SBarry Smith 
38051d79065fSBarry Smith   ierr  = PetscMalloc2(m,PetscInt,&d_nnz,m,PetscInt,&o_nnz);CHKERRQ(ierr);
3806ccd8e176SBarry Smith 
3807ecc77c7aSBarry Smith #if defined(PETSC_USE_DEBUGGING)
3808ecc77c7aSBarry Smith   for (i=0; i<m; i++) {
3809ecc77c7aSBarry Smith     nnz     = Ii[i+1]- Ii[i];
3810ecc77c7aSBarry Smith     JJ      = J + Ii[i];
3811e32f2f54SBarry Smith     if (nnz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative %D number of columns",i,nnz);
3812ecc77c7aSBarry Smith     if (nnz && (JJ[0] < 0)) SETERRRQ1(PETSC_ERR_ARG_WRONGSTATE,"Row %D starts with negative column index",i,j);
3813d0f46423SBarry 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);
3814ecc77c7aSBarry Smith   }
3815ecc77c7aSBarry Smith #endif
3816ecc77c7aSBarry Smith 
3817ccd8e176SBarry Smith   for (i=0; i<m; i++) {
3818b7940d39SSatish Balay     nnz     = Ii[i+1]- Ii[i];
3819b7940d39SSatish Balay     JJ      = J + Ii[i];
3820ccd8e176SBarry Smith     nnz_max = PetscMax(nnz_max,nnz);
3821ccd8e176SBarry Smith     d       = 0;
38220daa03b5SJed Brown     for (j=0; j<nnz; j++) {
38230daa03b5SJed Brown       if (cstart <= JJ[j] && JJ[j] < cend) d++;
3824ccd8e176SBarry Smith     }
3825ccd8e176SBarry Smith     d_nnz[i] = d;
3826ccd8e176SBarry Smith     o_nnz[i] = nnz - d;
3827ccd8e176SBarry Smith   }
3828ccd8e176SBarry Smith   ierr = MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);CHKERRQ(ierr);
38291d79065fSBarry Smith   ierr = PetscFree2(d_nnz,o_nnz);CHKERRQ(ierr);
3830ccd8e176SBarry Smith 
3831ccd8e176SBarry Smith   if (v) values = (PetscScalar*)v;
3832ccd8e176SBarry Smith   else {
3833ccd8e176SBarry Smith     ierr = PetscMalloc((nnz_max+1)*sizeof(PetscScalar),&values);CHKERRQ(ierr);
3834ccd8e176SBarry Smith     ierr = PetscMemzero(values,nnz_max*sizeof(PetscScalar));CHKERRQ(ierr);
3835ccd8e176SBarry Smith   }
3836ccd8e176SBarry Smith 
3837ccd8e176SBarry Smith   for (i=0; i<m; i++) {
3838ccd8e176SBarry Smith     ii   = i + rstart;
3839b7940d39SSatish Balay     nnz  = Ii[i+1]- Ii[i];
3840b7940d39SSatish Balay     ierr = MatSetValues_MPIAIJ(B,1,&ii,nnz,J+Ii[i],values+(v ? Ii[i] : 0),INSERT_VALUES);CHKERRQ(ierr);
3841ccd8e176SBarry Smith   }
3842ccd8e176SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3843ccd8e176SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3844ccd8e176SBarry Smith 
3845ccd8e176SBarry Smith   if (!v) {
3846ccd8e176SBarry Smith     ierr = PetscFree(values);CHKERRQ(ierr);
3847ccd8e176SBarry Smith   }
38487827cd58SJed Brown   ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
3849ccd8e176SBarry Smith   PetscFunctionReturn(0);
3850ccd8e176SBarry Smith }
3851e2e86b8fSSatish Balay EXTERN_C_END
3852ccd8e176SBarry Smith 
3853ccd8e176SBarry Smith #undef __FUNCT__
3854ccd8e176SBarry Smith #define __FUNCT__ "MatMPIAIJSetPreallocationCSR"
38551eea217eSSatish Balay /*@
3856ccd8e176SBarry Smith    MatMPIAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in AIJ format
3857ccd8e176SBarry Smith    (the default parallel PETSc format).
3858ccd8e176SBarry Smith 
3859ccd8e176SBarry Smith    Collective on MPI_Comm
3860ccd8e176SBarry Smith 
3861ccd8e176SBarry Smith    Input Parameters:
3862a1661176SMatthew Knepley +  B - the matrix
3863ccd8e176SBarry Smith .  i - the indices into j for the start of each local row (starts with zero)
38640daa03b5SJed Brown .  j - the column indices for each local row (starts with zero)
3865ccd8e176SBarry Smith -  v - optional values in the matrix
3866ccd8e176SBarry Smith 
3867ccd8e176SBarry Smith    Level: developer
3868ccd8e176SBarry Smith 
386912251496SSatish Balay    Notes:
387012251496SSatish Balay        The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc;
387112251496SSatish Balay      thus you CANNOT change the matrix entries by changing the values of a[] after you have
387212251496SSatish Balay      called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays.
387312251496SSatish Balay 
387412251496SSatish Balay        The i and j indices are 0 based, and i indices are indices corresponding to the local j array.
387512251496SSatish Balay 
387612251496SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
387712251496SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
387812251496SSatish Balay     as shown:
387912251496SSatish Balay 
388012251496SSatish Balay         1 0 0
388112251496SSatish Balay         2 0 3     P0
388212251496SSatish Balay        -------
388312251496SSatish Balay         4 5 6     P1
388412251496SSatish Balay 
388512251496SSatish Balay      Process0 [P0]: rows_owned=[0,1]
388612251496SSatish Balay         i =  {0,1,3}  [size = nrow+1  = 2+1]
388712251496SSatish Balay         j =  {0,0,2}  [size = nz = 6]
388812251496SSatish Balay         v =  {1,2,3}  [size = nz = 6]
388912251496SSatish Balay 
389012251496SSatish Balay      Process1 [P1]: rows_owned=[2]
389112251496SSatish Balay         i =  {0,3}    [size = nrow+1  = 1+1]
389212251496SSatish Balay         j =  {0,1,2}  [size = nz = 6]
389312251496SSatish Balay         v =  {4,5,6}  [size = nz = 6]
389412251496SSatish Balay 
3895ccd8e176SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
3896ccd8e176SBarry Smith 
389769b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatCreateAIJ(), MPIAIJ,
38988d7a6e47SBarry Smith           MatCreateSeqAIJWithArrays(), MatCreateMPIAIJWithSplitArrays()
3899ccd8e176SBarry Smith @*/
39007087cfbeSBarry Smith PetscErrorCode  MatMPIAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
3901ccd8e176SBarry Smith {
39024ac538c5SBarry Smith   PetscErrorCode ierr;
3903ccd8e176SBarry Smith 
3904ccd8e176SBarry Smith   PetscFunctionBegin;
39054ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatMPIAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr);
3906ccd8e176SBarry Smith   PetscFunctionReturn(0);
3907ccd8e176SBarry Smith }
3908ccd8e176SBarry Smith 
3909ccd8e176SBarry Smith #undef __FUNCT__
39104a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJSetPreallocation"
3911273d9f13SBarry Smith /*@C
3912ccd8e176SBarry Smith    MatMPIAIJSetPreallocation - Preallocates memory for a sparse parallel matrix in AIJ format
3913273d9f13SBarry Smith    (the default parallel PETSc format).  For good matrix assembly performance
3914273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameters
3915273d9f13SBarry Smith    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
3916273d9f13SBarry Smith    performance can be increased by more than a factor of 50.
3917273d9f13SBarry Smith 
3918273d9f13SBarry Smith    Collective on MPI_Comm
3919273d9f13SBarry Smith 
3920273d9f13SBarry Smith    Input Parameters:
3921273d9f13SBarry Smith +  A - the matrix
3922273d9f13SBarry Smith .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
3923273d9f13SBarry Smith            (same value is used for all local rows)
3924273d9f13SBarry Smith .  d_nnz - array containing the number of nonzeros in the various rows of the
3925273d9f13SBarry Smith            DIAGONAL portion of the local submatrix (possibly different for each row)
3926273d9f13SBarry Smith            or PETSC_NULL, if d_nz is used to specify the nonzero structure.
3927273d9f13SBarry Smith            The size of this array is equal to the number of local rows, i.e 'm'.
39283287b5eaSJed Brown            For matrices that will be factored, you must leave room for (and set)
39293287b5eaSJed Brown            the diagonal entry even if it is zero.
3930273d9f13SBarry Smith .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
3931273d9f13SBarry Smith            submatrix (same value is used for all local rows).
3932273d9f13SBarry Smith -  o_nnz - array containing the number of nonzeros in the various rows of the
3933273d9f13SBarry Smith            OFF-DIAGONAL portion of the local submatrix (possibly different for
3934273d9f13SBarry Smith            each row) or PETSC_NULL, if o_nz is used to specify the nonzero
3935273d9f13SBarry Smith            structure. The size of this array is equal to the number
3936273d9f13SBarry Smith            of local rows, i.e 'm'.
3937273d9f13SBarry Smith 
393849a6f317SBarry Smith    If the *_nnz parameter is given then the *_nz parameter is ignored
393949a6f317SBarry Smith 
3940273d9f13SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
3941ccd8e176SBarry Smith    compressed row storage (CSR)), is fully compatible with standard Fortran 77
39420598bfebSBarry Smith    storage.  The stored row and column indices begin with zero.
39430598bfebSBarry Smith    See the <A href="../../docs/manual.pdf#nameddest=ch_mat">Mat chapter of the users manual</A> for details.
3944273d9f13SBarry Smith 
3945273d9f13SBarry Smith    The parallel matrix is partitioned such that the first m0 rows belong to
3946273d9f13SBarry Smith    process 0, the next m1 rows belong to process 1, the next m2 rows belong
3947273d9f13SBarry Smith    to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.
3948273d9f13SBarry Smith 
3949273d9f13SBarry Smith    The DIAGONAL portion of the local submatrix of a processor can be defined
3950a05b864aSJed Brown    as the submatrix which is obtained by extraction the part corresponding to
3951a05b864aSJed Brown    the rows r1-r2 and columns c1-c2 of the global matrix, where r1 is the
3952a05b864aSJed Brown    first row that belongs to the processor, r2 is the last row belonging to
3953a05b864aSJed Brown    the this processor, and c1-c2 is range of indices of the local part of a
3954a05b864aSJed Brown    vector suitable for applying the matrix to.  This is an mxn matrix.  In the
3955a05b864aSJed Brown    common case of a square matrix, the row and column ranges are the same and
3956a05b864aSJed Brown    the DIAGONAL part is also square. The remaining portion of the local
3957a05b864aSJed Brown    submatrix (mxN) constitute the OFF-DIAGONAL portion.
3958273d9f13SBarry Smith 
3959273d9f13SBarry Smith    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
3960273d9f13SBarry Smith 
3961aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3962aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3963aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3964aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3965aa95bbe8SBarry Smith 
3966273d9f13SBarry Smith    Example usage:
3967273d9f13SBarry Smith 
3968273d9f13SBarry Smith    Consider the following 8x8 matrix with 34 non-zero values, that is
3969273d9f13SBarry Smith    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
3970273d9f13SBarry Smith    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
3971273d9f13SBarry Smith    as follows:
3972273d9f13SBarry Smith 
3973273d9f13SBarry Smith .vb
3974273d9f13SBarry Smith             1  2  0  |  0  3  0  |  0  4
3975273d9f13SBarry Smith     Proc0   0  5  6  |  7  0  0  |  8  0
3976273d9f13SBarry Smith             9  0 10  | 11  0  0  | 12  0
3977273d9f13SBarry Smith     -------------------------------------
3978273d9f13SBarry Smith            13  0 14  | 15 16 17  |  0  0
3979273d9f13SBarry Smith     Proc1   0 18  0  | 19 20 21  |  0  0
3980273d9f13SBarry Smith             0  0  0  | 22 23  0  | 24  0
3981273d9f13SBarry Smith     -------------------------------------
3982273d9f13SBarry Smith     Proc2  25 26 27  |  0  0 28  | 29  0
3983273d9f13SBarry Smith            30  0  0  | 31 32 33  |  0 34
3984273d9f13SBarry Smith .ve
3985273d9f13SBarry Smith 
3986273d9f13SBarry Smith    This can be represented as a collection of submatrices as:
3987273d9f13SBarry Smith 
3988273d9f13SBarry Smith .vb
3989273d9f13SBarry Smith       A B C
3990273d9f13SBarry Smith       D E F
3991273d9f13SBarry Smith       G H I
3992273d9f13SBarry Smith .ve
3993273d9f13SBarry Smith 
3994273d9f13SBarry Smith    Where the submatrices A,B,C are owned by proc0, D,E,F are
3995273d9f13SBarry Smith    owned by proc1, G,H,I are owned by proc2.
3996273d9f13SBarry Smith 
3997273d9f13SBarry Smith    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
3998273d9f13SBarry Smith    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
3999273d9f13SBarry Smith    The 'M','N' parameters are 8,8, and have the same values on all procs.
4000273d9f13SBarry Smith 
4001273d9f13SBarry Smith    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
4002273d9f13SBarry Smith    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
4003273d9f13SBarry Smith    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
4004273d9f13SBarry Smith    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
4005273d9f13SBarry Smith    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
4006273d9f13SBarry Smith    matrix, ans [DF] as another SeqAIJ matrix.
4007273d9f13SBarry Smith 
4008273d9f13SBarry Smith    When d_nz, o_nz parameters are specified, d_nz storage elements are
4009273d9f13SBarry Smith    allocated for every row of the local diagonal submatrix, and o_nz
4010273d9f13SBarry Smith    storage locations are allocated for every row of the OFF-DIAGONAL submat.
4011273d9f13SBarry Smith    One way to choose d_nz and o_nz is to use the max nonzerors per local
4012273d9f13SBarry Smith    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
4013273d9f13SBarry Smith    In this case, the values of d_nz,o_nz are:
4014273d9f13SBarry Smith .vb
4015273d9f13SBarry Smith      proc0 : dnz = 2, o_nz = 2
4016273d9f13SBarry Smith      proc1 : dnz = 3, o_nz = 2
4017273d9f13SBarry Smith      proc2 : dnz = 1, o_nz = 4
4018273d9f13SBarry Smith .ve
4019273d9f13SBarry Smith    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
4020273d9f13SBarry Smith    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
4021273d9f13SBarry Smith    for proc3. i.e we are using 12+15+10=37 storage locations to store
4022273d9f13SBarry Smith    34 values.
4023273d9f13SBarry Smith 
4024273d9f13SBarry Smith    When d_nnz, o_nnz parameters are specified, the storage is specified
4025273d9f13SBarry Smith    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
4026273d9f13SBarry Smith    In the above case the values for d_nnz,o_nnz are:
4027273d9f13SBarry Smith .vb
4028273d9f13SBarry Smith      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
4029273d9f13SBarry Smith      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
4030273d9f13SBarry Smith      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
4031273d9f13SBarry Smith .ve
4032273d9f13SBarry Smith    Here the space allocated is sum of all the above values i.e 34, and
4033273d9f13SBarry Smith    hence pre-allocation is perfect.
4034273d9f13SBarry Smith 
4035273d9f13SBarry Smith    Level: intermediate
4036273d9f13SBarry Smith 
4037273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
4038273d9f13SBarry Smith 
403969b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateAIJ(), MatMPIAIJSetPreallocationCSR(),
4040ab978733SBarry Smith           MPIAIJ, MatGetInfo(), PetscSplitOwnership()
4041273d9f13SBarry Smith @*/
40427087cfbeSBarry Smith PetscErrorCode  MatMPIAIJSetPreallocation(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
4043273d9f13SBarry Smith {
40444ac538c5SBarry Smith   PetscErrorCode ierr;
4045273d9f13SBarry Smith 
4046273d9f13SBarry Smith   PetscFunctionBegin;
40476ba663aaSJed Brown   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
40486ba663aaSJed Brown   PetscValidType(B,1);
40494ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatMPIAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[]),(B,d_nz,d_nnz,o_nz,o_nnz));CHKERRQ(ierr);
4050273d9f13SBarry Smith   PetscFunctionReturn(0);
4051273d9f13SBarry Smith }
4052273d9f13SBarry Smith 
40534a2ae208SSatish Balay #undef __FUNCT__
40542fb0ec9aSBarry Smith #define __FUNCT__ "MatCreateMPIAIJWithArrays"
405558d36128SBarry Smith /*@
40562fb0ec9aSBarry Smith      MatCreateMPIAIJWithArrays - creates a MPI AIJ matrix using arrays that contain in standard
40572fb0ec9aSBarry Smith          CSR format the local rows.
40582fb0ec9aSBarry Smith 
40592fb0ec9aSBarry Smith    Collective on MPI_Comm
40602fb0ec9aSBarry Smith 
40612fb0ec9aSBarry Smith    Input Parameters:
40622fb0ec9aSBarry Smith +  comm - MPI communicator
40632fb0ec9aSBarry Smith .  m - number of local rows (Cannot be PETSC_DECIDE)
40642fb0ec9aSBarry Smith .  n - This value should be the same as the local size used in creating the
40652fb0ec9aSBarry Smith        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
40662fb0ec9aSBarry Smith        calculated if N is given) For square matrices n is almost always m.
40672fb0ec9aSBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
40682fb0ec9aSBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
40692fb0ec9aSBarry Smith .   i - row indices
40702fb0ec9aSBarry Smith .   j - column indices
40712fb0ec9aSBarry Smith -   a - matrix values
40722fb0ec9aSBarry Smith 
40732fb0ec9aSBarry Smith    Output Parameter:
40742fb0ec9aSBarry Smith .   mat - the matrix
407503bfb495SBarry Smith 
40762fb0ec9aSBarry Smith    Level: intermediate
40772fb0ec9aSBarry Smith 
40782fb0ec9aSBarry Smith    Notes:
40792fb0ec9aSBarry Smith        The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc;
40802fb0ec9aSBarry Smith      thus you CANNOT change the matrix entries by changing the values of a[] after you have
40818d7a6e47SBarry Smith      called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays.
40822fb0ec9aSBarry Smith 
408312251496SSatish Balay        The i and j indices are 0 based, and i indices are indices corresponding to the local j array.
408412251496SSatish Balay 
408512251496SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
408612251496SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
408712251496SSatish Balay     as shown:
408812251496SSatish Balay 
408912251496SSatish Balay         1 0 0
409012251496SSatish Balay         2 0 3     P0
409112251496SSatish Balay        -------
409212251496SSatish Balay         4 5 6     P1
409312251496SSatish Balay 
409412251496SSatish Balay      Process0 [P0]: rows_owned=[0,1]
409512251496SSatish Balay         i =  {0,1,3}  [size = nrow+1  = 2+1]
409612251496SSatish Balay         j =  {0,0,2}  [size = nz = 6]
409712251496SSatish Balay         v =  {1,2,3}  [size = nz = 6]
409812251496SSatish Balay 
409912251496SSatish Balay      Process1 [P1]: rows_owned=[2]
410012251496SSatish Balay         i =  {0,3}    [size = nrow+1  = 1+1]
410112251496SSatish Balay         j =  {0,1,2}  [size = nz = 6]
410212251496SSatish Balay         v =  {4,5,6}  [size = nz = 6]
41032fb0ec9aSBarry Smith 
41042fb0ec9aSBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
41052fb0ec9aSBarry Smith 
41062fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
410769b1f4b7SBarry Smith           MPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithSplitArrays()
41082fb0ec9aSBarry Smith @*/
41097087cfbeSBarry 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)
41102fb0ec9aSBarry Smith {
41112fb0ec9aSBarry Smith   PetscErrorCode ierr;
41122fb0ec9aSBarry Smith 
41132fb0ec9aSBarry Smith   PetscFunctionBegin;
411469b1f4b7SBarry Smith   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4115e32f2f54SBarry Smith   if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative");
41162fb0ec9aSBarry Smith   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
4117d4146a68SBarry Smith   ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr);
4118a2f3521dSMark F. Adams   /* ierr = MatSetBlockSizes(M,bs,cbs);CHKERRQ(ierr); */
41192fb0ec9aSBarry Smith   ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr);
41202fb0ec9aSBarry Smith   ierr = MatMPIAIJSetPreallocationCSR(*mat,i,j,a);CHKERRQ(ierr);
41212fb0ec9aSBarry Smith   PetscFunctionReturn(0);
41222fb0ec9aSBarry Smith }
41232fb0ec9aSBarry Smith 
41242fb0ec9aSBarry Smith #undef __FUNCT__
412569b1f4b7SBarry Smith #define __FUNCT__ "MatCreateAIJ"
4126273d9f13SBarry Smith /*@C
412769b1f4b7SBarry Smith    MatCreateAIJ - Creates a sparse parallel matrix in AIJ format
4128273d9f13SBarry Smith    (the default parallel PETSc format).  For good matrix assembly performance
4129273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameters
4130273d9f13SBarry Smith    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
4131273d9f13SBarry Smith    performance can be increased by more than a factor of 50.
4132273d9f13SBarry Smith 
4133273d9f13SBarry Smith    Collective on MPI_Comm
4134273d9f13SBarry Smith 
4135273d9f13SBarry Smith    Input Parameters:
4136273d9f13SBarry Smith +  comm - MPI communicator
4137273d9f13SBarry Smith .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
4138273d9f13SBarry Smith            This value should be the same as the local size used in creating the
4139273d9f13SBarry Smith            y vector for the matrix-vector product y = Ax.
4140273d9f13SBarry Smith .  n - This value should be the same as the local size used in creating the
4141273d9f13SBarry Smith        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
4142273d9f13SBarry Smith        calculated if N is given) For square matrices n is almost always m.
4143273d9f13SBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
4144273d9f13SBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
4145273d9f13SBarry Smith .  d_nz  - number of nonzeros per row in DIAGONAL portion of local submatrix
4146273d9f13SBarry Smith            (same value is used for all local rows)
4147273d9f13SBarry Smith .  d_nnz - array containing the number of nonzeros in the various rows of the
4148273d9f13SBarry Smith            DIAGONAL portion of the local submatrix (possibly different for each row)
4149273d9f13SBarry Smith            or PETSC_NULL, if d_nz is used to specify the nonzero structure.
4150273d9f13SBarry Smith            The size of this array is equal to the number of local rows, i.e 'm'.
4151273d9f13SBarry Smith .  o_nz  - number of nonzeros per row in the OFF-DIAGONAL portion of local
4152273d9f13SBarry Smith            submatrix (same value is used for all local rows).
4153273d9f13SBarry Smith -  o_nnz - array containing the number of nonzeros in the various rows of the
4154273d9f13SBarry Smith            OFF-DIAGONAL portion of the local submatrix (possibly different for
4155273d9f13SBarry Smith            each row) or PETSC_NULL, if o_nz is used to specify the nonzero
4156273d9f13SBarry Smith            structure. The size of this array is equal to the number
4157273d9f13SBarry Smith            of local rows, i.e 'm'.
4158273d9f13SBarry Smith 
4159273d9f13SBarry Smith    Output Parameter:
4160273d9f13SBarry Smith .  A - the matrix
4161273d9f13SBarry Smith 
4162175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
4163ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
4164175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
4165175b88e8SBarry Smith 
4166273d9f13SBarry Smith    Notes:
416749a6f317SBarry Smith    If the *_nnz parameter is given then the *_nz parameter is ignored
416849a6f317SBarry Smith 
4169273d9f13SBarry Smith    m,n,M,N parameters specify the size of the matrix, and its partitioning across
4170273d9f13SBarry Smith    processors, while d_nz,d_nnz,o_nz,o_nnz parameters specify the approximate
4171273d9f13SBarry Smith    storage requirements for this matrix.
4172273d9f13SBarry Smith 
4173273d9f13SBarry Smith    If PETSC_DECIDE or  PETSC_DETERMINE is used for a particular argument on one
4174273d9f13SBarry Smith    processor than it must be used on all processors that share the object for
4175273d9f13SBarry Smith    that argument.
4176273d9f13SBarry Smith 
4177273d9f13SBarry Smith    The user MUST specify either the local or global matrix dimensions
4178273d9f13SBarry Smith    (possibly both).
4179273d9f13SBarry Smith 
418033a7c187SSatish Balay    The parallel matrix is partitioned across processors such that the
418133a7c187SSatish Balay    first m0 rows belong to process 0, the next m1 rows belong to
418233a7c187SSatish Balay    process 1, the next m2 rows belong to process 2 etc.. where
418333a7c187SSatish Balay    m0,m1,m2,.. are the input parameter 'm'. i.e each processor stores
418433a7c187SSatish Balay    values corresponding to [m x N] submatrix.
4185273d9f13SBarry Smith 
418633a7c187SSatish Balay    The columns are logically partitioned with the n0 columns belonging
418733a7c187SSatish Balay    to 0th partition, the next n1 columns belonging to the next
418833a7c187SSatish Balay    partition etc.. where n0,n1,n2... are the the input parameter 'n'.
418933a7c187SSatish Balay 
419033a7c187SSatish Balay    The DIAGONAL portion of the local submatrix on any given processor
419133a7c187SSatish Balay    is the submatrix corresponding to the rows and columns m,n
419233a7c187SSatish Balay    corresponding to the given processor. i.e diagonal matrix on
419333a7c187SSatish Balay    process 0 is [m0 x n0], diagonal matrix on process 1 is [m1 x n1]
419433a7c187SSatish Balay    etc. The remaining portion of the local submatrix [m x (N-n)]
419533a7c187SSatish Balay    constitute the OFF-DIAGONAL portion. The example below better
419633a7c187SSatish Balay    illustrates this concept.
419733a7c187SSatish Balay 
419833a7c187SSatish Balay    For a square global matrix we define each processor's diagonal portion
419933a7c187SSatish Balay    to be its local rows and the corresponding columns (a square submatrix);
420033a7c187SSatish Balay    each processor's off-diagonal portion encompasses the remainder of the
420133a7c187SSatish Balay    local matrix (a rectangular submatrix).
4202273d9f13SBarry Smith 
4203273d9f13SBarry Smith    If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
4204273d9f13SBarry Smith 
420597d05335SKris Buschelman    When calling this routine with a single process communicator, a matrix of
420697d05335SKris Buschelman    type SEQAIJ is returned.  If a matrix of type MPIAIJ is desired for this
420797d05335SKris Buschelman    type of communicator, use the construction mechanism:
420878102f6cSMatthew Knepley      MatCreate(...,&A); MatSetType(A,MATMPIAIJ); MatSetSizes(A, m,n,M,N); MatMPIAIJSetPreallocation(A,...);
420997d05335SKris Buschelman 
4210273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible.
4211273d9f13SBarry Smith    We search for consecutive rows with the same nonzero structure, thereby
4212273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
4213273d9f13SBarry Smith 
4214273d9f13SBarry Smith    Options Database Keys:
4215923f20ffSKris Buschelman +  -mat_no_inode  - Do not use inodes
4216923f20ffSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
4217273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
4218273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
4219273d9f13SBarry Smith         the user still MUST index entries starting at 0!
4220273d9f13SBarry Smith 
4221273d9f13SBarry Smith 
4222273d9f13SBarry Smith    Example usage:
4223273d9f13SBarry Smith 
4224273d9f13SBarry Smith    Consider the following 8x8 matrix with 34 non-zero values, that is
4225273d9f13SBarry Smith    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
4226273d9f13SBarry Smith    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
4227273d9f13SBarry Smith    as follows:
4228273d9f13SBarry Smith 
4229273d9f13SBarry Smith .vb
4230273d9f13SBarry Smith             1  2  0  |  0  3  0  |  0  4
4231273d9f13SBarry Smith     Proc0   0  5  6  |  7  0  0  |  8  0
4232273d9f13SBarry Smith             9  0 10  | 11  0  0  | 12  0
4233273d9f13SBarry Smith     -------------------------------------
4234273d9f13SBarry Smith            13  0 14  | 15 16 17  |  0  0
4235273d9f13SBarry Smith     Proc1   0 18  0  | 19 20 21  |  0  0
4236273d9f13SBarry Smith             0  0  0  | 22 23  0  | 24  0
4237273d9f13SBarry Smith     -------------------------------------
4238273d9f13SBarry Smith     Proc2  25 26 27  |  0  0 28  | 29  0
4239273d9f13SBarry Smith            30  0  0  | 31 32 33  |  0 34
4240273d9f13SBarry Smith .ve
4241273d9f13SBarry Smith 
4242273d9f13SBarry Smith    This can be represented as a collection of submatrices as:
4243273d9f13SBarry Smith 
4244273d9f13SBarry Smith .vb
4245273d9f13SBarry Smith       A B C
4246273d9f13SBarry Smith       D E F
4247273d9f13SBarry Smith       G H I
4248273d9f13SBarry Smith .ve
4249273d9f13SBarry Smith 
4250273d9f13SBarry Smith    Where the submatrices A,B,C are owned by proc0, D,E,F are
4251273d9f13SBarry Smith    owned by proc1, G,H,I are owned by proc2.
4252273d9f13SBarry Smith 
4253273d9f13SBarry Smith    The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
4254273d9f13SBarry Smith    The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
4255273d9f13SBarry Smith    The 'M','N' parameters are 8,8, and have the same values on all procs.
4256273d9f13SBarry Smith 
4257273d9f13SBarry Smith    The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
4258273d9f13SBarry Smith    submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
4259273d9f13SBarry Smith    corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
4260273d9f13SBarry Smith    Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
4261273d9f13SBarry Smith    part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
4262273d9f13SBarry Smith    matrix, ans [DF] as another SeqAIJ matrix.
4263273d9f13SBarry Smith 
4264273d9f13SBarry Smith    When d_nz, o_nz parameters are specified, d_nz storage elements are
4265273d9f13SBarry Smith    allocated for every row of the local diagonal submatrix, and o_nz
4266273d9f13SBarry Smith    storage locations are allocated for every row of the OFF-DIAGONAL submat.
4267273d9f13SBarry Smith    One way to choose d_nz and o_nz is to use the max nonzerors per local
4268273d9f13SBarry Smith    rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
4269273d9f13SBarry Smith    In this case, the values of d_nz,o_nz are:
4270273d9f13SBarry Smith .vb
4271273d9f13SBarry Smith      proc0 : dnz = 2, o_nz = 2
4272273d9f13SBarry Smith      proc1 : dnz = 3, o_nz = 2
4273273d9f13SBarry Smith      proc2 : dnz = 1, o_nz = 4
4274273d9f13SBarry Smith .ve
4275273d9f13SBarry Smith    We are allocating m*(d_nz+o_nz) storage locations for every proc. This
4276273d9f13SBarry Smith    translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
4277273d9f13SBarry Smith    for proc3. i.e we are using 12+15+10=37 storage locations to store
4278273d9f13SBarry Smith    34 values.
4279273d9f13SBarry Smith 
4280273d9f13SBarry Smith    When d_nnz, o_nnz parameters are specified, the storage is specified
4281273d9f13SBarry Smith    for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
4282273d9f13SBarry Smith    In the above case the values for d_nnz,o_nnz are:
4283273d9f13SBarry Smith .vb
4284273d9f13SBarry Smith      proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
4285273d9f13SBarry Smith      proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
4286273d9f13SBarry Smith      proc2: d_nnz = [1,1]   and o_nnz = [4,4]
4287273d9f13SBarry Smith .ve
4288273d9f13SBarry Smith    Here the space allocated is sum of all the above values i.e 34, and
4289273d9f13SBarry Smith    hence pre-allocation is perfect.
4290273d9f13SBarry Smith 
4291273d9f13SBarry Smith    Level: intermediate
4292273d9f13SBarry Smith 
4293273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
4294273d9f13SBarry Smith 
4295ccd8e176SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
42962fb0ec9aSBarry Smith           MPIAIJ, MatCreateMPIAIJWithArrays()
4297273d9f13SBarry Smith @*/
429869b1f4b7SBarry 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)
4299273d9f13SBarry Smith {
43006849ba73SBarry Smith   PetscErrorCode ierr;
4301b1d57f15SBarry Smith   PetscMPIInt    size;
4302273d9f13SBarry Smith 
4303273d9f13SBarry Smith   PetscFunctionBegin;
4304f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
4305f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*A,m,n,M,N);CHKERRQ(ierr);
4306273d9f13SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
4307273d9f13SBarry Smith   if (size > 1) {
4308273d9f13SBarry Smith     ierr = MatSetType(*A,MATMPIAIJ);CHKERRQ(ierr);
4309273d9f13SBarry Smith     ierr = MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr);
4310273d9f13SBarry Smith   } else {
4311273d9f13SBarry Smith     ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
4312273d9f13SBarry Smith     ierr = MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);CHKERRQ(ierr);
4313273d9f13SBarry Smith   }
4314273d9f13SBarry Smith   PetscFunctionReturn(0);
4315273d9f13SBarry Smith }
4316195d93cdSBarry Smith 
43174a2ae208SSatish Balay #undef __FUNCT__
43184a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJGetSeqAIJ"
43199230625dSJed Brown PetscErrorCode  MatMPIAIJGetSeqAIJ(Mat A,Mat *Ad,Mat *Ao,const PetscInt *colmap[])
4320195d93cdSBarry Smith {
4321195d93cdSBarry Smith   Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;
4322b1d57f15SBarry Smith 
4323195d93cdSBarry Smith   PetscFunctionBegin;
4324195d93cdSBarry Smith   *Ad     = a->A;
4325195d93cdSBarry Smith   *Ao     = a->B;
4326195d93cdSBarry Smith   *colmap = a->garray;
4327195d93cdSBarry Smith   PetscFunctionReturn(0);
4328195d93cdSBarry Smith }
4329a2243be0SBarry Smith 
4330a2243be0SBarry Smith #undef __FUNCT__
4331a2243be0SBarry Smith #define __FUNCT__ "MatSetColoring_MPIAIJ"
4332dfbe8321SBarry Smith PetscErrorCode MatSetColoring_MPIAIJ(Mat A,ISColoring coloring)
4333a2243be0SBarry Smith {
4334dfbe8321SBarry Smith   PetscErrorCode ierr;
4335b1d57f15SBarry Smith   PetscInt       i;
4336a2243be0SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
4337a2243be0SBarry Smith 
4338a2243be0SBarry Smith   PetscFunctionBegin;
43398ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
434008b6dcc0SBarry Smith     ISColoringValue *allcolors,*colors;
4341a2243be0SBarry Smith     ISColoring      ocoloring;
4342a2243be0SBarry Smith 
4343a2243be0SBarry Smith     /* set coloring for diagonal portion */
4344a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->A,coloring);CHKERRQ(ierr);
4345a2243be0SBarry Smith 
4346a2243be0SBarry Smith     /* set coloring for off-diagonal portion */
43477adad957SLisandro Dalcin     ierr = ISAllGatherColors(((PetscObject)A)->comm,coloring->n,coloring->colors,PETSC_NULL,&allcolors);CHKERRQ(ierr);
4348d0f46423SBarry Smith     ierr = PetscMalloc((a->B->cmap->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
4349d0f46423SBarry Smith     for (i=0; i<a->B->cmap->n; i++) {
4350a2243be0SBarry Smith       colors[i] = allcolors[a->garray[i]];
4351a2243be0SBarry Smith     }
4352a2243be0SBarry Smith     ierr = PetscFree(allcolors);CHKERRQ(ierr);
4353d0f46423SBarry Smith     ierr = ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
4354a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr);
43556bf464f9SBarry Smith     ierr = ISColoringDestroy(&ocoloring);CHKERRQ(ierr);
4356a2243be0SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
435708b6dcc0SBarry Smith     ISColoringValue *colors;
4358b1d57f15SBarry Smith     PetscInt        *larray;
4359a2243be0SBarry Smith     ISColoring      ocoloring;
4360a2243be0SBarry Smith 
4361a2243be0SBarry Smith     /* set coloring for diagonal portion */
4362d0f46423SBarry Smith     ierr = PetscMalloc((a->A->cmap->n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr);
4363d0f46423SBarry Smith     for (i=0; i<a->A->cmap->n; i++) {
4364d0f46423SBarry Smith       larray[i] = i + A->cmap->rstart;
4365a2243be0SBarry Smith     }
4366992144d0SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,a->A->cmap->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
4367d0f46423SBarry Smith     ierr = PetscMalloc((a->A->cmap->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
4368d0f46423SBarry Smith     for (i=0; i<a->A->cmap->n; i++) {
4369a2243be0SBarry Smith       colors[i] = coloring->colors[larray[i]];
4370a2243be0SBarry Smith     }
4371a2243be0SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
4372d0f46423SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,a->A->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
4373a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->A,ocoloring);CHKERRQ(ierr);
43746bf464f9SBarry Smith     ierr = ISColoringDestroy(&ocoloring);CHKERRQ(ierr);
4375a2243be0SBarry Smith 
4376a2243be0SBarry Smith     /* set coloring for off-diagonal portion */
4377d0f46423SBarry Smith     ierr = PetscMalloc((a->B->cmap->n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr);
4378992144d0SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,a->B->cmap->n,a->garray,PETSC_NULL,larray);CHKERRQ(ierr);
4379d0f46423SBarry Smith     ierr = PetscMalloc((a->B->cmap->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
4380d0f46423SBarry Smith     for (i=0; i<a->B->cmap->n; i++) {
4381a2243be0SBarry Smith       colors[i] = coloring->colors[larray[i]];
4382a2243be0SBarry Smith     }
4383a2243be0SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
4384d0f46423SBarry Smith     ierr = ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
4385a2243be0SBarry Smith     ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr);
43866bf464f9SBarry Smith     ierr = ISColoringDestroy(&ocoloring);CHKERRQ(ierr);
43876bf464f9SBarry Smith   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support ISColoringType %d",(int)coloring->ctype);
4388a2243be0SBarry Smith 
4389a2243be0SBarry Smith   PetscFunctionReturn(0);
4390a2243be0SBarry Smith }
4391a2243be0SBarry Smith 
4392779c1a83SBarry Smith #undef __FUNCT__
4393779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdifor_MPIAIJ"
4394b1d57f15SBarry Smith PetscErrorCode MatSetValuesAdifor_MPIAIJ(Mat A,PetscInt nl,void *advalues)
4395779c1a83SBarry Smith {
4396779c1a83SBarry Smith   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
4397dfbe8321SBarry Smith   PetscErrorCode ierr;
4398779c1a83SBarry Smith 
4399779c1a83SBarry Smith   PetscFunctionBegin;
4400779c1a83SBarry Smith   ierr = MatSetValuesAdifor_SeqAIJ(a->A,nl,advalues);CHKERRQ(ierr);
4401779c1a83SBarry Smith   ierr = MatSetValuesAdifor_SeqAIJ(a->B,nl,advalues);CHKERRQ(ierr);
4402a2243be0SBarry Smith   PetscFunctionReturn(0);
4403a2243be0SBarry Smith }
4404c5d6d63eSBarry Smith 
4405c5d6d63eSBarry Smith #undef __FUNCT__
440690431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJConcatenateSeqAIJSymbolic"
440790431a8fSHong Zhang PetscErrorCode  MatCreateMPIAIJConcatenateSeqAIJSymbolic(MPI_Comm comm,Mat inmat,PetscInt n,Mat *outmat)
44089b8102ccSHong Zhang {
44099b8102ccSHong Zhang   PetscErrorCode ierr;
4410a2f3521dSMark F. Adams   PetscInt       m,N,i,rstart,nnz,*dnz,*onz,sum,bs,cbs;
44119b8102ccSHong Zhang   PetscInt       *indx;
44129b8102ccSHong Zhang 
44139b8102ccSHong Zhang   PetscFunctionBegin;
44149b8102ccSHong Zhang   /* This routine will ONLY return MPIAIJ type matrix */
44159b8102ccSHong Zhang   ierr = MatGetSize(inmat,&m,&N);CHKERRQ(ierr);
4416a2f3521dSMark F. Adams   ierr = MatGetBlockSizes(inmat,&bs,&cbs);CHKERRQ(ierr);
44179b8102ccSHong Zhang   if (n == PETSC_DECIDE) {
44189b8102ccSHong Zhang     ierr = PetscSplitOwnership(comm,&n,&N);CHKERRQ(ierr);
44199b8102ccSHong Zhang   }
4420a22543b6SHong Zhang   /* Check sum(n) = N */
4421a95133b1SBarry Smith   ierr = MPI_Allreduce(&n,&sum,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr);
4422a22543b6SHong Zhang   if (sum != N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Sum of local columns != global columns %d",N);
4423a22543b6SHong Zhang 
44249b8102ccSHong Zhang   ierr = MPI_Scan(&m, &rstart,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr);
44259b8102ccSHong Zhang   rstart -= m;
44269b8102ccSHong Zhang 
44279b8102ccSHong Zhang   ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr);
44289b8102ccSHong Zhang   for (i=0;i<m;i++) {
44299b8102ccSHong Zhang     ierr = MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,PETSC_NULL);CHKERRQ(ierr);
44309b8102ccSHong Zhang     ierr = MatPreallocateSet(i+rstart,nnz,indx,dnz,onz);CHKERRQ(ierr);
44319b8102ccSHong Zhang     ierr = MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,PETSC_NULL);CHKERRQ(ierr);
44329b8102ccSHong Zhang   }
44339b8102ccSHong Zhang 
44349b8102ccSHong Zhang   ierr = MatCreate(comm,outmat);CHKERRQ(ierr);
44359b8102ccSHong Zhang   ierr = MatSetSizes(*outmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
4436a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(*outmat,bs,cbs);CHKERRQ(ierr);
44379b8102ccSHong Zhang   ierr = MatSetType(*outmat,MATMPIAIJ);CHKERRQ(ierr);
44389b8102ccSHong Zhang   ierr = MatMPIAIJSetPreallocation(*outmat,0,dnz,0,onz);CHKERRQ(ierr);
44399b8102ccSHong Zhang   ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr);
44409b8102ccSHong Zhang   PetscFunctionReturn(0);
44419b8102ccSHong Zhang }
44429b8102ccSHong Zhang 
44439b8102ccSHong Zhang #undef __FUNCT__
444490431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJConcatenateSeqAIJNumeric"
444590431a8fSHong Zhang PetscErrorCode  MatCreateMPIAIJConcatenateSeqAIJNumeric(MPI_Comm comm,Mat inmat,PetscInt n,Mat outmat)
44469b8102ccSHong Zhang {
44479b8102ccSHong Zhang   PetscErrorCode ierr;
44489b8102ccSHong Zhang   PetscInt       m,N,i,rstart,nnz,Ii;
44499b8102ccSHong Zhang   PetscInt       *indx;
44509b8102ccSHong Zhang   PetscScalar    *values;
44519b8102ccSHong Zhang 
44529b8102ccSHong Zhang   PetscFunctionBegin;
44539b8102ccSHong Zhang   ierr = MatGetSize(inmat,&m,&N);CHKERRQ(ierr);
44549b8102ccSHong Zhang   ierr = MatGetOwnershipRange(outmat,&rstart,PETSC_NULL);CHKERRQ(ierr);
44559b8102ccSHong Zhang   for (i=0;i<m;i++) {
44569b8102ccSHong Zhang     ierr = MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
44579b8102ccSHong Zhang     Ii    = i + rstart;
4458a22543b6SHong Zhang     ierr = MatSetValues_MPIAIJ(outmat,1,&Ii,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr);
44599b8102ccSHong Zhang     ierr = MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr);
44609b8102ccSHong Zhang   }
44619b8102ccSHong Zhang   ierr = MatAssemblyBegin(outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
44629b8102ccSHong Zhang   ierr = MatAssemblyEnd(outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
44639b8102ccSHong Zhang   PetscFunctionReturn(0);
44649b8102ccSHong Zhang }
44659b8102ccSHong Zhang 
44669b8102ccSHong Zhang #undef __FUNCT__
446790431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJConcatenateSeqAIJ"
4468bc08b0f1SBarry Smith /*@
446990431a8fSHong Zhang       MatCreateMPIAIJConcatenateSeqAIJ - Creates a single large PETSc matrix by concatenating sequential
447051dd7536SBarry Smith                  matrices from each processor
4471c5d6d63eSBarry Smith 
4472c5d6d63eSBarry Smith     Collective on MPI_Comm
4473c5d6d63eSBarry Smith 
4474c5d6d63eSBarry Smith    Input Parameters:
447551dd7536SBarry Smith +    comm - the communicators the parallel matrix will live on
4476d6bb3c2dSHong Zhang .    inmat - the input sequential matrices
44770e36024fSHong Zhang .    n - number of local columns (or PETSC_DECIDE)
4478d6bb3c2dSHong Zhang -    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
447951dd7536SBarry Smith 
448051dd7536SBarry Smith    Output Parameter:
448151dd7536SBarry Smith .    outmat - the parallel matrix generated
4482c5d6d63eSBarry Smith 
44837e25d530SSatish Balay     Level: advanced
44847e25d530SSatish Balay 
4485f08fae4eSHong Zhang    Notes: The number of columns of the matrix in EACH processor MUST be the same.
4486c5d6d63eSBarry Smith 
4487c5d6d63eSBarry Smith @*/
448890431a8fSHong Zhang PetscErrorCode  MatCreateMPIAIJConcatenateSeqAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
4489c5d6d63eSBarry Smith {
4490dfbe8321SBarry Smith   PetscErrorCode ierr;
4491c5d6d63eSBarry Smith 
4492c5d6d63eSBarry Smith   PetscFunctionBegin;
44939b8102ccSHong Zhang   ierr = PetscLogEventBegin(MAT_Merge,inmat,0,0,0);CHKERRQ(ierr);
4494d6bb3c2dSHong Zhang   if (scall == MAT_INITIAL_MATRIX) {
449590431a8fSHong Zhang     ierr = MatCreateMPIAIJConcatenateSeqAIJSymbolic(comm,inmat,n,outmat);CHKERRQ(ierr);
44960e36024fSHong Zhang   }
449790431a8fSHong Zhang   ierr = MatCreateMPIAIJConcatenateSeqAIJNumeric(comm,inmat,n,*outmat);CHKERRQ(ierr);
44989b8102ccSHong Zhang   ierr = PetscLogEventEnd(MAT_Merge,inmat,0,0,0);CHKERRQ(ierr);
4499c5d6d63eSBarry Smith   PetscFunctionReturn(0);
4500c5d6d63eSBarry Smith }
4501c5d6d63eSBarry Smith 
4502c5d6d63eSBarry Smith #undef __FUNCT__
4503c5d6d63eSBarry Smith #define __FUNCT__ "MatFileSplit"
4504dfbe8321SBarry Smith PetscErrorCode MatFileSplit(Mat A,char *outfile)
4505c5d6d63eSBarry Smith {
4506dfbe8321SBarry Smith   PetscErrorCode    ierr;
450732dcc486SBarry Smith   PetscMPIInt       rank;
4508b1d57f15SBarry Smith   PetscInt          m,N,i,rstart,nnz;
4509de4209c5SBarry Smith   size_t            len;
4510b1d57f15SBarry Smith   const PetscInt    *indx;
4511c5d6d63eSBarry Smith   PetscViewer       out;
4512c5d6d63eSBarry Smith   char              *name;
4513c5d6d63eSBarry Smith   Mat               B;
4514b3cc6726SBarry Smith   const PetscScalar *values;
4515c5d6d63eSBarry Smith 
4516c5d6d63eSBarry Smith   PetscFunctionBegin;
4517c5d6d63eSBarry Smith   ierr = MatGetLocalSize(A,&m,0);CHKERRQ(ierr);
4518c5d6d63eSBarry Smith   ierr = MatGetSize(A,0,&N);CHKERRQ(ierr);
4519f204ca49SKris Buschelman   /* Should this be the type of the diagonal block of A? */
4520f69a0ea3SMatthew Knepley   ierr = MatCreate(PETSC_COMM_SELF,&B);CHKERRQ(ierr);
4521f69a0ea3SMatthew Knepley   ierr = MatSetSizes(B,m,N,m,N);CHKERRQ(ierr);
4522a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(B,A->rmap->bs,A->cmap->bs);CHKERRQ(ierr);
4523f204ca49SKris Buschelman   ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
4524f204ca49SKris Buschelman   ierr = MatSeqAIJSetPreallocation(B,0,PETSC_NULL);CHKERRQ(ierr);
4525c5d6d63eSBarry Smith   ierr = MatGetOwnershipRange(A,&rstart,0);CHKERRQ(ierr);
4526c5d6d63eSBarry Smith   for (i=0;i<m;i++) {
4527c5d6d63eSBarry Smith     ierr = MatGetRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr);
4528c5d6d63eSBarry Smith     ierr = MatSetValues(B,1,&i,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr);
4529c5d6d63eSBarry Smith     ierr = MatRestoreRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr);
4530c5d6d63eSBarry Smith   }
4531c5d6d63eSBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4532c5d6d63eSBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4533c5d6d63eSBarry Smith 
45347adad957SLisandro Dalcin   ierr = MPI_Comm_rank(((PetscObject)A)->comm,&rank);CHKERRQ(ierr);
4535c5d6d63eSBarry Smith   ierr = PetscStrlen(outfile,&len);CHKERRQ(ierr);
4536c5d6d63eSBarry Smith   ierr = PetscMalloc((len+5)*sizeof(char),&name);CHKERRQ(ierr);
4537c5d6d63eSBarry Smith   sprintf(name,"%s.%d",outfile,rank);
4538852598b0SBarry Smith   ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,name,FILE_MODE_APPEND,&out);CHKERRQ(ierr);
4539a2ea699eSBarry Smith   ierr = PetscFree(name);CHKERRQ(ierr);
4540c5d6d63eSBarry Smith   ierr = MatView(B,out);CHKERRQ(ierr);
45416bf464f9SBarry Smith   ierr = PetscViewerDestroy(&out);CHKERRQ(ierr);
45426bf464f9SBarry Smith   ierr = MatDestroy(&B);CHKERRQ(ierr);
4543c5d6d63eSBarry Smith   PetscFunctionReturn(0);
4544c5d6d63eSBarry Smith }
4545e5f2cdd8SHong Zhang 
454609573ac7SBarry Smith extern PetscErrorCode MatDestroy_MPIAIJ(Mat);
454751a7d1a8SHong Zhang #undef __FUNCT__
454851a7d1a8SHong Zhang #define __FUNCT__ "MatDestroy_MPIAIJ_SeqsToMPI"
45497087cfbeSBarry Smith PetscErrorCode  MatDestroy_MPIAIJ_SeqsToMPI(Mat A)
455051a7d1a8SHong Zhang {
455151a7d1a8SHong Zhang   PetscErrorCode       ierr;
4552671beff6SHong Zhang   Mat_Merge_SeqsToMPI  *merge;
4553776b82aeSLisandro Dalcin   PetscContainer       container;
455451a7d1a8SHong Zhang 
455551a7d1a8SHong Zhang   PetscFunctionBegin;
4556671beff6SHong Zhang   ierr = PetscObjectQuery((PetscObject)A,"MatMergeSeqsToMPI",(PetscObject *)&container);CHKERRQ(ierr);
4557671beff6SHong Zhang   if (container) {
4558776b82aeSLisandro Dalcin     ierr = PetscContainerGetPointer(container,(void **)&merge);CHKERRQ(ierr);
455951a7d1a8SHong Zhang     ierr = PetscFree(merge->id_r);CHKERRQ(ierr);
45603e06a4e6SHong Zhang     ierr = PetscFree(merge->len_s);CHKERRQ(ierr);
45613e06a4e6SHong Zhang     ierr = PetscFree(merge->len_r);CHKERRQ(ierr);
456251a7d1a8SHong Zhang     ierr = PetscFree(merge->bi);CHKERRQ(ierr);
456351a7d1a8SHong Zhang     ierr = PetscFree(merge->bj);CHKERRQ(ierr);
4564533163c2SBarry Smith     ierr = PetscFree(merge->buf_ri[0]);CHKERRQ(ierr);
456502c68681SHong Zhang     ierr = PetscFree(merge->buf_ri);CHKERRQ(ierr);
4566533163c2SBarry Smith     ierr = PetscFree(merge->buf_rj[0]);CHKERRQ(ierr);
456702c68681SHong Zhang     ierr = PetscFree(merge->buf_rj);CHKERRQ(ierr);
456805b42c5fSBarry Smith     ierr = PetscFree(merge->coi);CHKERRQ(ierr);
456905b42c5fSBarry Smith     ierr = PetscFree(merge->coj);CHKERRQ(ierr);
457005b42c5fSBarry Smith     ierr = PetscFree(merge->owners_co);CHKERRQ(ierr);
45716bf464f9SBarry Smith     ierr = PetscLayoutDestroy(&merge->rowmap);CHKERRQ(ierr);
4572bf0cc555SLisandro Dalcin     ierr = PetscFree(merge);CHKERRQ(ierr);
4573671beff6SHong Zhang     ierr = PetscObjectCompose((PetscObject)A,"MatMergeSeqsToMPI",0);CHKERRQ(ierr);
4574671beff6SHong Zhang   }
457551a7d1a8SHong Zhang   ierr = MatDestroy_MPIAIJ(A);CHKERRQ(ierr);
457651a7d1a8SHong Zhang   PetscFunctionReturn(0);
457751a7d1a8SHong Zhang }
457851a7d1a8SHong Zhang 
4579c6db04a5SJed Brown #include <../src/mat/utils/freespace.h>
4580c6db04a5SJed Brown #include <petscbt.h>
45814ebed01fSBarry Smith 
4582e5f2cdd8SHong Zhang #undef __FUNCT__
458390431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJSumSeqAIJNumeric"
458490431a8fSHong Zhang PetscErrorCode  MatCreateMPIAIJSumSeqAIJNumeric(Mat seqmat,Mat mpimat)
458555d1abb9SHong Zhang {
458655d1abb9SHong Zhang   PetscErrorCode       ierr;
45877adad957SLisandro Dalcin   MPI_Comm             comm=((PetscObject)mpimat)->comm;
458855d1abb9SHong Zhang   Mat_SeqAIJ           *a=(Mat_SeqAIJ*)seqmat->data;
4589b1d57f15SBarry Smith   PetscMPIInt          size,rank,taga,*len_s;
4590a2ea699eSBarry Smith   PetscInt             N=mpimat->cmap->N,i,j,*owners,*ai=a->i,*aj;
4591b1d57f15SBarry Smith   PetscInt             proc,m;
4592b1d57f15SBarry Smith   PetscInt             **buf_ri,**buf_rj;
4593b1d57f15SBarry Smith   PetscInt             k,anzi,*bj_i,*bi,*bj,arow,bnzi,nextaj;
4594b1d57f15SBarry Smith   PetscInt             nrows,**buf_ri_k,**nextrow,**nextai;
459555d1abb9SHong Zhang   MPI_Request          *s_waits,*r_waits;
459655d1abb9SHong Zhang   MPI_Status           *status;
4597a77337e4SBarry Smith   MatScalar            *aa=a->a;
4598dd6ea824SBarry Smith   MatScalar            **abuf_r,*ba_i;
459955d1abb9SHong Zhang   Mat_Merge_SeqsToMPI  *merge;
4600776b82aeSLisandro Dalcin   PetscContainer       container;
460155d1abb9SHong Zhang 
460255d1abb9SHong Zhang   PetscFunctionBegin;
46034ebed01fSBarry Smith   ierr = PetscLogEventBegin(MAT_Seqstompinum,seqmat,0,0,0);CHKERRQ(ierr);
46043c2c1871SHong Zhang 
460555d1abb9SHong Zhang   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
460655d1abb9SHong Zhang   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
460755d1abb9SHong Zhang 
460855d1abb9SHong Zhang   ierr = PetscObjectQuery((PetscObject)mpimat,"MatMergeSeqsToMPI",(PetscObject *)&container);CHKERRQ(ierr);
4609776b82aeSLisandro Dalcin   ierr  = PetscContainerGetPointer(container,(void **)&merge);CHKERRQ(ierr);
4610bf0cc555SLisandro Dalcin 
461155d1abb9SHong Zhang   bi     = merge->bi;
461255d1abb9SHong Zhang   bj     = merge->bj;
461355d1abb9SHong Zhang   buf_ri = merge->buf_ri;
461455d1abb9SHong Zhang   buf_rj = merge->buf_rj;
461555d1abb9SHong Zhang 
461655d1abb9SHong Zhang   ierr   = PetscMalloc(size*sizeof(MPI_Status),&status);CHKERRQ(ierr);
46177a2fc3feSBarry Smith   owners = merge->rowmap->range;
461855d1abb9SHong Zhang   len_s  = merge->len_s;
461955d1abb9SHong Zhang 
462055d1abb9SHong Zhang   /* send and recv matrix values */
462155d1abb9SHong Zhang   /*-----------------------------*/
4622357abbc8SBarry Smith   ierr = PetscObjectGetNewTag((PetscObject)mpimat,&taga);CHKERRQ(ierr);
462355d1abb9SHong Zhang   ierr = PetscPostIrecvScalar(comm,taga,merge->nrecv,merge->id_r,merge->len_r,&abuf_r,&r_waits);CHKERRQ(ierr);
462455d1abb9SHong Zhang 
462555d1abb9SHong Zhang   ierr = PetscMalloc((merge->nsend+1)*sizeof(MPI_Request),&s_waits);CHKERRQ(ierr);
462655d1abb9SHong Zhang   for (proc=0,k=0; proc<size; proc++) {
462755d1abb9SHong Zhang     if (!len_s[proc]) continue;
462855d1abb9SHong Zhang     i = owners[proc];
462955d1abb9SHong Zhang     ierr = MPI_Isend(aa+ai[i],len_s[proc],MPIU_MATSCALAR,proc,taga,comm,s_waits+k);CHKERRQ(ierr);
463055d1abb9SHong Zhang     k++;
463155d1abb9SHong Zhang   }
463255d1abb9SHong Zhang 
46330c468ba9SBarry Smith   if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,r_waits,status);CHKERRQ(ierr);}
46340c468ba9SBarry Smith   if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,s_waits,status);CHKERRQ(ierr);}
463555d1abb9SHong Zhang   ierr = PetscFree(status);CHKERRQ(ierr);
463655d1abb9SHong Zhang 
463755d1abb9SHong Zhang   ierr = PetscFree(s_waits);CHKERRQ(ierr);
463855d1abb9SHong Zhang   ierr = PetscFree(r_waits);CHKERRQ(ierr);
463955d1abb9SHong Zhang 
464055d1abb9SHong Zhang   /* insert mat values of mpimat */
464155d1abb9SHong Zhang   /*----------------------------*/
4642a77337e4SBarry Smith   ierr = PetscMalloc(N*sizeof(PetscScalar),&ba_i);CHKERRQ(ierr);
46430572522cSBarry Smith   ierr = PetscMalloc3(merge->nrecv,PetscInt*,&buf_ri_k,merge->nrecv,PetscInt*,&nextrow,merge->nrecv,PetscInt*,&nextai);CHKERRQ(ierr);
464455d1abb9SHong Zhang 
464555d1abb9SHong Zhang   for (k=0; k<merge->nrecv; k++) {
464655d1abb9SHong Zhang     buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
464755d1abb9SHong Zhang     nrows = *(buf_ri_k[k]);
464855d1abb9SHong Zhang     nextrow[k]  = buf_ri_k[k]+1;  /* next row number of k-th recved i-structure */
464955d1abb9SHong Zhang     nextai[k]   = buf_ri_k[k] + (nrows + 1);/* poins to the next i-structure of k-th recved i-structure  */
465055d1abb9SHong Zhang   }
465155d1abb9SHong Zhang 
465255d1abb9SHong Zhang   /* set values of ba */
46537a2fc3feSBarry Smith   m = merge->rowmap->n;
465455d1abb9SHong Zhang   for (i=0; i<m; i++) {
465555d1abb9SHong Zhang     arow = owners[rank] + i;
465655d1abb9SHong Zhang     bj_i = bj+bi[i];  /* col indices of the i-th row of mpimat */
465755d1abb9SHong Zhang     bnzi = bi[i+1] - bi[i];
4658a77337e4SBarry Smith     ierr = PetscMemzero(ba_i,bnzi*sizeof(PetscScalar));CHKERRQ(ierr);
465955d1abb9SHong Zhang 
466055d1abb9SHong Zhang     /* add local non-zero vals of this proc's seqmat into ba */
466155d1abb9SHong Zhang     anzi = ai[arow+1] - ai[arow];
466255d1abb9SHong Zhang     aj   = a->j + ai[arow];
466355d1abb9SHong Zhang     aa   = a->a + ai[arow];
466455d1abb9SHong Zhang     nextaj = 0;
466555d1abb9SHong Zhang     for (j=0; nextaj<anzi; j++) {
466655d1abb9SHong Zhang       if (*(bj_i + j) == aj[nextaj]) { /* bcol == acol */
466755d1abb9SHong Zhang         ba_i[j] += aa[nextaj++];
466855d1abb9SHong Zhang       }
466955d1abb9SHong Zhang     }
467055d1abb9SHong Zhang 
467155d1abb9SHong Zhang     /* add received vals into ba */
467255d1abb9SHong Zhang     for (k=0; k<merge->nrecv; k++) { /* k-th received message */
467355d1abb9SHong Zhang       /* i-th row */
467455d1abb9SHong Zhang       if (i == *nextrow[k]) {
467555d1abb9SHong Zhang         anzi = *(nextai[k]+1) - *nextai[k];
467655d1abb9SHong Zhang         aj   = buf_rj[k] + *(nextai[k]);
467755d1abb9SHong Zhang         aa   = abuf_r[k] + *(nextai[k]);
467855d1abb9SHong Zhang         nextaj = 0;
467955d1abb9SHong Zhang         for (j=0; nextaj<anzi; j++) {
468055d1abb9SHong Zhang           if (*(bj_i + j) == aj[nextaj]) { /* bcol == acol */
468155d1abb9SHong Zhang             ba_i[j] += aa[nextaj++];
468255d1abb9SHong Zhang           }
468355d1abb9SHong Zhang         }
468455d1abb9SHong Zhang         nextrow[k]++; nextai[k]++;
468555d1abb9SHong Zhang       }
468655d1abb9SHong Zhang     }
468755d1abb9SHong Zhang     ierr = MatSetValues(mpimat,1,&arow,bnzi,bj_i,ba_i,INSERT_VALUES);CHKERRQ(ierr);
468855d1abb9SHong Zhang   }
468955d1abb9SHong Zhang   ierr = MatAssemblyBegin(mpimat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
469055d1abb9SHong Zhang   ierr = MatAssemblyEnd(mpimat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
469155d1abb9SHong Zhang 
4692533163c2SBarry Smith   ierr = PetscFree(abuf_r[0]);CHKERRQ(ierr);
469355d1abb9SHong Zhang   ierr = PetscFree(abuf_r);CHKERRQ(ierr);
469455d1abb9SHong Zhang   ierr = PetscFree(ba_i);CHKERRQ(ierr);
46951d79065fSBarry Smith   ierr = PetscFree3(buf_ri_k,nextrow,nextai);CHKERRQ(ierr);
46964ebed01fSBarry Smith   ierr = PetscLogEventEnd(MAT_Seqstompinum,seqmat,0,0,0);CHKERRQ(ierr);
469755d1abb9SHong Zhang   PetscFunctionReturn(0);
469855d1abb9SHong Zhang }
469938f152feSBarry Smith 
47006bc0bbbfSBarry Smith extern PetscErrorCode  MatDestroy_MPIAIJ_SeqsToMPI(Mat);
47016bc0bbbfSBarry Smith 
470238f152feSBarry Smith #undef __FUNCT__
470390431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJSumSeqAIJSymbolic"
470490431a8fSHong Zhang PetscErrorCode  MatCreateMPIAIJSumSeqAIJSymbolic(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,Mat *mpimat)
4705e5f2cdd8SHong Zhang {
4706f08fae4eSHong Zhang   PetscErrorCode       ierr;
470755a3bba9SHong Zhang   Mat                  B_mpi;
4708c2234fe3SHong Zhang   Mat_SeqAIJ           *a=(Mat_SeqAIJ*)seqmat->data;
4709b1d57f15SBarry Smith   PetscMPIInt          size,rank,tagi,tagj,*len_s,*len_si,*len_ri;
4710b1d57f15SBarry Smith   PetscInt             **buf_rj,**buf_ri,**buf_ri_k;
4711d0f46423SBarry Smith   PetscInt             M=seqmat->rmap->n,N=seqmat->cmap->n,i,*owners,*ai=a->i,*aj=a->j;
4712a2f3521dSMark F. Adams   PetscInt             len,proc,*dnz,*onz,bs,cbs;
4713b1d57f15SBarry Smith   PetscInt             k,anzi,*bi,*bj,*lnk,nlnk,arow,bnzi,nspacedouble=0;
4714b1d57f15SBarry Smith   PetscInt             nrows,*buf_s,*buf_si,*buf_si_i,**nextrow,**nextai;
471555d1abb9SHong Zhang   MPI_Request          *si_waits,*sj_waits,*ri_waits,*rj_waits;
471658cb9c82SHong Zhang   MPI_Status           *status;
4717a1a86e44SBarry Smith   PetscFreeSpaceList   free_space=PETSC_NULL,current_space=PETSC_NULL;
4718be0fcf8dSHong Zhang   PetscBT              lnkbt;
471951a7d1a8SHong Zhang   Mat_Merge_SeqsToMPI  *merge;
4720776b82aeSLisandro Dalcin   PetscContainer       container;
472102c68681SHong Zhang 
4722e5f2cdd8SHong Zhang   PetscFunctionBegin;
47234ebed01fSBarry Smith   ierr = PetscLogEventBegin(MAT_Seqstompisym,seqmat,0,0,0);CHKERRQ(ierr);
47243c2c1871SHong Zhang 
472538f152feSBarry Smith   /* make sure it is a PETSc comm */
472638f152feSBarry Smith   ierr = PetscCommDuplicate(comm,&comm,PETSC_NULL);CHKERRQ(ierr);
4727e5f2cdd8SHong Zhang   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
4728e5f2cdd8SHong Zhang   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
472955d1abb9SHong Zhang 
473051a7d1a8SHong Zhang   ierr = PetscNew(Mat_Merge_SeqsToMPI,&merge);CHKERRQ(ierr);
4731c2234fe3SHong Zhang   ierr = PetscMalloc(size*sizeof(MPI_Status),&status);CHKERRQ(ierr);
4732e5f2cdd8SHong Zhang 
47336abd8857SHong Zhang   /* determine row ownership */
4734f08fae4eSHong Zhang   /*---------------------------------------------------------*/
473526283091SBarry Smith   ierr = PetscLayoutCreate(comm,&merge->rowmap);CHKERRQ(ierr);
473626283091SBarry Smith   ierr = PetscLayoutSetLocalSize(merge->rowmap,m);CHKERRQ(ierr);
473726283091SBarry Smith   ierr = PetscLayoutSetSize(merge->rowmap,M);CHKERRQ(ierr);
473826283091SBarry Smith   ierr = PetscLayoutSetBlockSize(merge->rowmap,1);CHKERRQ(ierr);
473926283091SBarry Smith   ierr = PetscLayoutSetUp(merge->rowmap);CHKERRQ(ierr);
4740b1d57f15SBarry Smith   ierr = PetscMalloc(size*sizeof(PetscMPIInt),&len_si);CHKERRQ(ierr);
4741b1d57f15SBarry Smith   ierr = PetscMalloc(size*sizeof(PetscMPIInt),&merge->len_s);CHKERRQ(ierr);
474255d1abb9SHong Zhang 
47437a2fc3feSBarry Smith   m      = merge->rowmap->n;
47447a2fc3feSBarry Smith   owners = merge->rowmap->range;
47456abd8857SHong Zhang 
47466abd8857SHong Zhang   /* determine the number of messages to send, their lengths */
47476abd8857SHong Zhang   /*---------------------------------------------------------*/
47483e06a4e6SHong Zhang   len_s  = merge->len_s;
474951a7d1a8SHong Zhang 
47502257cef7SHong Zhang   len = 0;  /* length of buf_si[] */
4751c2234fe3SHong Zhang   merge->nsend = 0;
4752409913e3SHong Zhang   for (proc=0; proc<size; proc++) {
47532257cef7SHong Zhang     len_si[proc] = 0;
47543e06a4e6SHong Zhang     if (proc == rank) {
47556abd8857SHong Zhang       len_s[proc] = 0;
47563e06a4e6SHong Zhang     } else {
475702c68681SHong Zhang       len_si[proc] = owners[proc+1] - owners[proc] + 1;
47583e06a4e6SHong Zhang       len_s[proc] = ai[owners[proc+1]] - ai[owners[proc]]; /* num of rows to be sent to [proc] */
47593e06a4e6SHong Zhang     }
47603e06a4e6SHong Zhang     if (len_s[proc]) {
4761c2234fe3SHong Zhang       merge->nsend++;
47622257cef7SHong Zhang       nrows = 0;
47632257cef7SHong Zhang       for (i=owners[proc]; i<owners[proc+1]; i++) {
47642257cef7SHong Zhang         if (ai[i+1] > ai[i]) nrows++;
47652257cef7SHong Zhang       }
47662257cef7SHong Zhang       len_si[proc] = 2*(nrows+1);
47672257cef7SHong Zhang       len += len_si[proc];
4768409913e3SHong Zhang     }
476958cb9c82SHong Zhang   }
4770409913e3SHong Zhang 
47712257cef7SHong Zhang   /* determine the number and length of messages to receive for ij-structure */
47722257cef7SHong Zhang   /*-------------------------------------------------------------------------*/
477351a7d1a8SHong Zhang   ierr = PetscGatherNumberOfMessages(comm,PETSC_NULL,len_s,&merge->nrecv);CHKERRQ(ierr);
477455d1abb9SHong Zhang   ierr = PetscGatherMessageLengths2(comm,merge->nsend,merge->nrecv,len_s,len_si,&merge->id_r,&merge->len_r,&len_ri);CHKERRQ(ierr);
4775671beff6SHong Zhang 
47763e06a4e6SHong Zhang   /* post the Irecv of j-structure */
47773e06a4e6SHong Zhang   /*-------------------------------*/
47782c72b5baSSatish Balay   ierr = PetscCommGetNewTag(comm,&tagj);CHKERRQ(ierr);
47793e06a4e6SHong Zhang   ierr = PetscPostIrecvInt(comm,tagj,merge->nrecv,merge->id_r,merge->len_r,&buf_rj,&rj_waits);CHKERRQ(ierr);
478002c68681SHong Zhang 
47813e06a4e6SHong Zhang   /* post the Isend of j-structure */
4782affca5deSHong Zhang   /*--------------------------------*/
47831d79065fSBarry Smith   ierr = PetscMalloc2(merge->nsend,MPI_Request,&si_waits,merge->nsend,MPI_Request,&sj_waits);CHKERRQ(ierr);
47843e06a4e6SHong Zhang 
47852257cef7SHong Zhang   for (proc=0, k=0; proc<size; proc++) {
4786409913e3SHong Zhang     if (!len_s[proc]) continue;
478702c68681SHong Zhang     i = owners[proc];
4788b1d57f15SBarry Smith     ierr = MPI_Isend(aj+ai[i],len_s[proc],MPIU_INT,proc,tagj,comm,sj_waits+k);CHKERRQ(ierr);
478951a7d1a8SHong Zhang     k++;
479051a7d1a8SHong Zhang   }
479151a7d1a8SHong Zhang 
47923e06a4e6SHong Zhang   /* receives and sends of j-structure are complete */
47933e06a4e6SHong Zhang   /*------------------------------------------------*/
47940c468ba9SBarry Smith   if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,rj_waits,status);CHKERRQ(ierr);}
47950c468ba9SBarry Smith   if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,sj_waits,status);CHKERRQ(ierr);}
479602c68681SHong Zhang 
479702c68681SHong Zhang   /* send and recv i-structure */
479802c68681SHong Zhang   /*---------------------------*/
47992c72b5baSSatish Balay   ierr = PetscCommGetNewTag(comm,&tagi);CHKERRQ(ierr);
480002c68681SHong Zhang   ierr = PetscPostIrecvInt(comm,tagi,merge->nrecv,merge->id_r,len_ri,&buf_ri,&ri_waits);CHKERRQ(ierr);
480102c68681SHong Zhang 
4802b1d57f15SBarry Smith   ierr = PetscMalloc((len+1)*sizeof(PetscInt),&buf_s);CHKERRQ(ierr);
48033e06a4e6SHong Zhang   buf_si = buf_s;  /* points to the beginning of k-th msg to be sent */
48042257cef7SHong Zhang   for (proc=0,k=0; proc<size; proc++) {
480502c68681SHong Zhang     if (!len_s[proc]) continue;
48063e06a4e6SHong Zhang     /* form outgoing message for i-structure:
48073e06a4e6SHong Zhang          buf_si[0]:                 nrows to be sent
48083e06a4e6SHong Zhang                [1:nrows]:           row index (global)
48093e06a4e6SHong Zhang                [nrows+1:2*nrows+1]: i-structure index
48103e06a4e6SHong Zhang     */
48113e06a4e6SHong Zhang     /*-------------------------------------------*/
48122257cef7SHong Zhang     nrows = len_si[proc]/2 - 1;
48133e06a4e6SHong Zhang     buf_si_i    = buf_si + nrows+1;
48143e06a4e6SHong Zhang     buf_si[0]   = nrows;
48153e06a4e6SHong Zhang     buf_si_i[0] = 0;
48163e06a4e6SHong Zhang     nrows = 0;
48173e06a4e6SHong Zhang     for (i=owners[proc]; i<owners[proc+1]; i++) {
48183e06a4e6SHong Zhang       anzi = ai[i+1] - ai[i];
48193e06a4e6SHong Zhang       if (anzi) {
48203e06a4e6SHong Zhang         buf_si_i[nrows+1] = buf_si_i[nrows] + anzi; /* i-structure */
48213e06a4e6SHong Zhang         buf_si[nrows+1] = i-owners[proc]; /* local row index */
48223e06a4e6SHong Zhang         nrows++;
48233e06a4e6SHong Zhang       }
48243e06a4e6SHong Zhang     }
4825b1d57f15SBarry Smith     ierr = MPI_Isend(buf_si,len_si[proc],MPIU_INT,proc,tagi,comm,si_waits+k);CHKERRQ(ierr);
482602c68681SHong Zhang     k++;
48272257cef7SHong Zhang     buf_si += len_si[proc];
482802c68681SHong Zhang   }
48292257cef7SHong Zhang 
48300c468ba9SBarry Smith   if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,ri_waits,status);CHKERRQ(ierr);}
48310c468ba9SBarry Smith   if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,si_waits,status);CHKERRQ(ierr);}
483202c68681SHong Zhang 
4833ae15b995SBarry Smith   ierr = PetscInfo2(seqmat,"nsend: %D, nrecv: %D\n",merge->nsend,merge->nrecv);CHKERRQ(ierr);
48343e06a4e6SHong Zhang   for (i=0; i<merge->nrecv; i++) {
4835ae15b995SBarry 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);
48363e06a4e6SHong Zhang   }
48373e06a4e6SHong Zhang 
48383e06a4e6SHong Zhang   ierr = PetscFree(len_si);CHKERRQ(ierr);
483902c68681SHong Zhang   ierr = PetscFree(len_ri);CHKERRQ(ierr);
484002c68681SHong Zhang   ierr = PetscFree(rj_waits);CHKERRQ(ierr);
48411d79065fSBarry Smith   ierr = PetscFree2(si_waits,sj_waits);CHKERRQ(ierr);
48422257cef7SHong Zhang   ierr = PetscFree(ri_waits);CHKERRQ(ierr);
48433e06a4e6SHong Zhang   ierr = PetscFree(buf_s);CHKERRQ(ierr);
4844bcc1bcd5SHong Zhang   ierr = PetscFree(status);CHKERRQ(ierr);
484558cb9c82SHong Zhang 
4846bcc1bcd5SHong Zhang   /* compute a local seq matrix in each processor */
4847bcc1bcd5SHong Zhang   /*----------------------------------------------*/
484858cb9c82SHong Zhang   /* allocate bi array and free space for accumulating nonzero column info */
4849b1d57f15SBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&bi);CHKERRQ(ierr);
485058cb9c82SHong Zhang   bi[0] = 0;
485158cb9c82SHong Zhang 
4852be0fcf8dSHong Zhang   /* create and initialize a linked list */
4853be0fcf8dSHong Zhang   nlnk = N+1;
4854be0fcf8dSHong Zhang   ierr = PetscLLCreate(N,N,nlnk,lnk,lnkbt);CHKERRQ(ierr);
485558cb9c82SHong Zhang 
4856bcc1bcd5SHong Zhang   /* initial FreeSpace size is 2*(num of local nnz(seqmat)) */
4857bcc1bcd5SHong Zhang   len  = ai[owners[rank+1]] - ai[owners[rank]];
4858a1a86e44SBarry Smith   ierr = PetscFreeSpaceGet((PetscInt)(2*len+1),&free_space);CHKERRQ(ierr);
485958cb9c82SHong Zhang   current_space = free_space;
486058cb9c82SHong Zhang 
4861bcc1bcd5SHong Zhang   /* determine symbolic info for each local row */
48620572522cSBarry Smith   ierr = PetscMalloc3(merge->nrecv,PetscInt*,&buf_ri_k,merge->nrecv,PetscInt*,&nextrow,merge->nrecv,PetscInt*,&nextai);CHKERRQ(ierr);
48631d79065fSBarry Smith 
48643e06a4e6SHong Zhang   for (k=0; k<merge->nrecv; k++) {
48652257cef7SHong Zhang     buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
48663e06a4e6SHong Zhang     nrows = *buf_ri_k[k];
48673e06a4e6SHong Zhang     nextrow[k]  = buf_ri_k[k] + 1;  /* next row number of k-th recved i-structure */
48682257cef7SHong Zhang     nextai[k]   = buf_ri_k[k] + (nrows + 1);/* poins to the next i-structure of k-th recved i-structure  */
48693e06a4e6SHong Zhang   }
48702257cef7SHong Zhang 
4871bcc1bcd5SHong Zhang   ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr);
4872bcc1bcd5SHong Zhang   len = 0;
487358cb9c82SHong Zhang   for (i=0;i<m;i++) {
487458cb9c82SHong Zhang     bnzi   = 0;
487558cb9c82SHong Zhang     /* add local non-zero cols of this proc's seqmat into lnk */
487658cb9c82SHong Zhang     arow   = owners[rank] + i;
487758cb9c82SHong Zhang     anzi   = ai[arow+1] - ai[arow];
487858cb9c82SHong Zhang     aj     = a->j + ai[arow];
4879dadf0e6bSHong Zhang     ierr = PetscLLAddSorted(anzi,aj,N,nlnk,lnk,lnkbt);CHKERRQ(ierr);
488058cb9c82SHong Zhang     bnzi += nlnk;
488158cb9c82SHong Zhang     /* add received col data into lnk */
488251a7d1a8SHong Zhang     for (k=0; k<merge->nrecv; k++) { /* k-th received message */
488355d1abb9SHong Zhang       if (i == *nextrow[k]) { /* i-th row */
48843e06a4e6SHong Zhang         anzi = *(nextai[k]+1) - *nextai[k];
48853e06a4e6SHong Zhang         aj   = buf_rj[k] + *nextai[k];
4886dadf0e6bSHong Zhang         ierr = PetscLLAddSorted(anzi,aj,N,nlnk,lnk,lnkbt);CHKERRQ(ierr);
48873e06a4e6SHong Zhang         bnzi += nlnk;
48883e06a4e6SHong Zhang         nextrow[k]++; nextai[k]++;
48893e06a4e6SHong Zhang       }
489058cb9c82SHong Zhang     }
4891bcc1bcd5SHong Zhang     if (len < bnzi) len = bnzi;  /* =max(bnzi) */
489258cb9c82SHong Zhang 
489358cb9c82SHong Zhang     /* if free space is not available, make more free space */
489458cb9c82SHong Zhang     if (current_space->local_remaining<bnzi) {
48954238b7adSHong Zhang       ierr = PetscFreeSpaceGet(bnzi+current_space->total_array_size,&current_space);CHKERRQ(ierr);
489658cb9c82SHong Zhang       nspacedouble++;
489758cb9c82SHong Zhang     }
489858cb9c82SHong Zhang     /* copy data into free space, then initialize lnk */
4899be0fcf8dSHong Zhang     ierr = PetscLLClean(N,N,bnzi,lnk,current_space->array,lnkbt);CHKERRQ(ierr);
4900bcc1bcd5SHong Zhang     ierr = MatPreallocateSet(i+owners[rank],bnzi,current_space->array,dnz,onz);CHKERRQ(ierr);
4901bcc1bcd5SHong Zhang 
490258cb9c82SHong Zhang     current_space->array           += bnzi;
490358cb9c82SHong Zhang     current_space->local_used      += bnzi;
490458cb9c82SHong Zhang     current_space->local_remaining -= bnzi;
490558cb9c82SHong Zhang 
490658cb9c82SHong Zhang     bi[i+1] = bi[i] + bnzi;
490758cb9c82SHong Zhang   }
4908bcc1bcd5SHong Zhang 
49091d79065fSBarry Smith   ierr = PetscFree3(buf_ri_k,nextrow,nextai);CHKERRQ(ierr);
4910bcc1bcd5SHong Zhang 
4911b1d57f15SBarry Smith   ierr = PetscMalloc((bi[m]+1)*sizeof(PetscInt),&bj);CHKERRQ(ierr);
4912a1a86e44SBarry Smith   ierr = PetscFreeSpaceContiguous(&free_space,bj);CHKERRQ(ierr);
4913be0fcf8dSHong Zhang   ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr);
4914409913e3SHong Zhang 
4915bcc1bcd5SHong Zhang   /* create symbolic parallel matrix B_mpi */
4916bcc1bcd5SHong Zhang   /*---------------------------------------*/
4917a2f3521dSMark F. Adams     ierr = MatGetBlockSizes(seqmat,&bs,&cbs);CHKERRQ(ierr);
4918f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,&B_mpi);CHKERRQ(ierr);
491954b84b50SHong Zhang   if (n==PETSC_DECIDE) {
4920f69a0ea3SMatthew Knepley     ierr = MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,N);CHKERRQ(ierr);
492154b84b50SHong Zhang   } else {
4922f69a0ea3SMatthew Knepley     ierr = MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
492354b84b50SHong Zhang   }
4924a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(B_mpi,bs,cbs);CHKERRQ(ierr);
4925bcc1bcd5SHong Zhang   ierr = MatSetType(B_mpi,MATMPIAIJ);CHKERRQ(ierr);
4926bcc1bcd5SHong Zhang   ierr = MatMPIAIJSetPreallocation(B_mpi,0,dnz,0,onz);CHKERRQ(ierr);
4927bcc1bcd5SHong Zhang   ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr);
49287e63b356SHong Zhang   ierr = MatSetOption(B_mpi,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr);
492958cb9c82SHong Zhang 
493090431a8fSHong Zhang   /* B_mpi is not ready for use - assembly will be done by MatCreateMPIAIJSumSeqAIJNumeric() */
49316abd8857SHong Zhang   B_mpi->assembled     = PETSC_FALSE;
4932affca5deSHong Zhang   B_mpi->ops->destroy  = MatDestroy_MPIAIJ_SeqsToMPI;
4933affca5deSHong Zhang   merge->bi            = bi;
4934affca5deSHong Zhang   merge->bj            = bj;
493502c68681SHong Zhang   merge->buf_ri        = buf_ri;
493602c68681SHong Zhang   merge->buf_rj        = buf_rj;
4937de0260b3SHong Zhang   merge->coi           = PETSC_NULL;
4938de0260b3SHong Zhang   merge->coj           = PETSC_NULL;
4939de0260b3SHong Zhang   merge->owners_co     = PETSC_NULL;
4940affca5deSHong Zhang 
4941bf0cc555SLisandro Dalcin   ierr = PetscCommDestroy(&comm);CHKERRQ(ierr);
4942bf0cc555SLisandro Dalcin 
4943affca5deSHong Zhang   /* attach the supporting struct to B_mpi for reuse */
4944776b82aeSLisandro Dalcin   ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr);
4945776b82aeSLisandro Dalcin   ierr = PetscContainerSetPointer(container,merge);CHKERRQ(ierr);
4946affca5deSHong Zhang   ierr = PetscObjectCompose((PetscObject)B_mpi,"MatMergeSeqsToMPI",(PetscObject)container);CHKERRQ(ierr);
4947bf0cc555SLisandro Dalcin   ierr = PetscContainerDestroy(&container);CHKERRQ(ierr);
4948affca5deSHong Zhang   *mpimat = B_mpi;
494938f152feSBarry Smith 
49504ebed01fSBarry Smith   ierr = PetscLogEventEnd(MAT_Seqstompisym,seqmat,0,0,0);CHKERRQ(ierr);
4951e5f2cdd8SHong Zhang   PetscFunctionReturn(0);
4952e5f2cdd8SHong Zhang }
495325616d81SHong Zhang 
495438f152feSBarry Smith #undef __FUNCT__
495590431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJSumSeqAIJ"
4956d4036a1aSHong Zhang /*@C
495790431a8fSHong Zhang       MatCreateMPIAIJSumSeqAIJ - Creates a MPIAIJ matrix by adding sequential
4958d4036a1aSHong Zhang                  matrices from each processor
4959d4036a1aSHong Zhang 
4960d4036a1aSHong Zhang     Collective on MPI_Comm
4961d4036a1aSHong Zhang 
4962d4036a1aSHong Zhang    Input Parameters:
4963d4036a1aSHong Zhang +    comm - the communicators the parallel matrix will live on
4964d4036a1aSHong Zhang .    seqmat - the input sequential matrices
4965d4036a1aSHong Zhang .    m - number of local rows (or PETSC_DECIDE)
4966d4036a1aSHong Zhang .    n - number of local columns (or PETSC_DECIDE)
4967d4036a1aSHong Zhang -    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
4968d4036a1aSHong Zhang 
4969d4036a1aSHong Zhang    Output Parameter:
4970d4036a1aSHong Zhang .    mpimat - the parallel matrix generated
4971d4036a1aSHong Zhang 
4972d4036a1aSHong Zhang     Level: advanced
4973d4036a1aSHong Zhang 
4974d4036a1aSHong Zhang    Notes:
4975d4036a1aSHong Zhang      The dimensions of the sequential matrix in each processor MUST be the same.
4976d4036a1aSHong Zhang      The input seqmat is included into the container "Mat_Merge_SeqsToMPI", and will be
4977d4036a1aSHong Zhang      destroyed when mpimat is destroyed. Call PetscObjectQuery() to access seqmat.
4978d4036a1aSHong Zhang @*/
497990431a8fSHong Zhang PetscErrorCode  MatCreateMPIAIJSumSeqAIJ(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,MatReuse scall,Mat *mpimat)
498055d1abb9SHong Zhang {
498155d1abb9SHong Zhang   PetscErrorCode   ierr;
49827e63b356SHong Zhang   PetscMPIInt     size;
498355d1abb9SHong Zhang 
498455d1abb9SHong Zhang   PetscFunctionBegin;
49857e63b356SHong Zhang   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
49867e63b356SHong Zhang   if (size == 1) {
49877e63b356SHong Zhang      ierr = PetscLogEventBegin(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr);
49887e63b356SHong Zhang      if (scall == MAT_INITIAL_MATRIX) {
49897e63b356SHong Zhang        ierr = MatDuplicate(seqmat,MAT_COPY_VALUES,mpimat);CHKERRQ(ierr);
49907e63b356SHong Zhang      } else {
49917e63b356SHong Zhang        ierr = MatCopy(seqmat,*mpimat,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
49927e63b356SHong Zhang      }
49937e63b356SHong Zhang      ierr = PetscLogEventEnd(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr);
49947e63b356SHong Zhang      PetscFunctionReturn(0);
49957e63b356SHong Zhang   }
49964ebed01fSBarry Smith   ierr = PetscLogEventBegin(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr);
499755d1abb9SHong Zhang   if (scall == MAT_INITIAL_MATRIX) {
499890431a8fSHong Zhang     ierr = MatCreateMPIAIJSumSeqAIJSymbolic(comm,seqmat,m,n,mpimat);CHKERRQ(ierr);
499955d1abb9SHong Zhang   }
500090431a8fSHong Zhang   ierr = MatCreateMPIAIJSumSeqAIJNumeric(seqmat,*mpimat);CHKERRQ(ierr);
50014ebed01fSBarry Smith   ierr = PetscLogEventEnd(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr);
500255d1abb9SHong Zhang   PetscFunctionReturn(0);
500355d1abb9SHong Zhang }
50044ebed01fSBarry Smith 
500525616d81SHong Zhang #undef __FUNCT__
50064a2b5492SBarry Smith #define __FUNCT__ "MatMPIAIJGetLocalMat"
5007bc08b0f1SBarry Smith /*@
50084a2b5492SBarry Smith      MatMPIAIJGetLocalMat - Creates a SeqAIJ from a MPIAIJ matrix by taking all its local rows and putting them into a sequential vector with
50098661ff28SBarry Smith           mlocal rows and n columns. Where mlocal is the row count obtained with MatGetLocalSize() and n is the global column count obtained
50108661ff28SBarry Smith           with MatGetSize()
501125616d81SHong Zhang 
501232fba14fSHong Zhang     Not Collective
501325616d81SHong Zhang 
501425616d81SHong Zhang    Input Parameters:
501525616d81SHong Zhang +    A - the matrix
501625616d81SHong Zhang .    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
501725616d81SHong Zhang 
501825616d81SHong Zhang    Output Parameter:
501925616d81SHong Zhang .    A_loc - the local sequential matrix generated
502025616d81SHong Zhang 
502125616d81SHong Zhang     Level: developer
502225616d81SHong Zhang 
5023ba264940SBarry Smith .seealso: MatGetOwnerShipRange(), MatMPIAIJGetLocalMatCondensed()
50248661ff28SBarry Smith 
502525616d81SHong Zhang @*/
50264a2b5492SBarry Smith PetscErrorCode  MatMPIAIJGetLocalMat(Mat A,MatReuse scall,Mat *A_loc)
502725616d81SHong Zhang {
502825616d81SHong Zhang   PetscErrorCode  ierr;
502901b7ae99SHong Zhang   Mat_MPIAIJ      *mpimat=(Mat_MPIAIJ*)A->data;
503001b7ae99SHong Zhang   Mat_SeqAIJ      *mat,*a=(Mat_SeqAIJ*)(mpimat->A)->data,*b=(Mat_SeqAIJ*)(mpimat->B)->data;
503101b7ae99SHong Zhang   PetscInt        *ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j,*cmap=mpimat->garray;
5032a77337e4SBarry Smith   MatScalar       *aa=a->a,*ba=b->a,*cam;
5033a77337e4SBarry Smith   PetscScalar     *ca;
5034d0f46423SBarry Smith   PetscInt        am=A->rmap->n,i,j,k,cstart=A->cmap->rstart;
50355a7d977cSHong Zhang   PetscInt        *ci,*cj,col,ncols_d,ncols_o,jo;
50368661ff28SBarry Smith   PetscBool       match;
503725616d81SHong Zhang 
503825616d81SHong Zhang   PetscFunctionBegin;
5039251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&match);CHKERRQ(ierr);
50408661ff28SBarry Smith   if (!match) SETERRQ(((PetscObject)A)->comm, PETSC_ERR_SUP,"Requires MPIAIJ matrix as input");
50414ebed01fSBarry Smith   ierr = PetscLogEventBegin(MAT_Getlocalmat,A,0,0,0);CHKERRQ(ierr);
504201b7ae99SHong Zhang   if (scall == MAT_INITIAL_MATRIX) {
5043dea91ad1SHong Zhang     ierr = PetscMalloc((1+am)*sizeof(PetscInt),&ci);CHKERRQ(ierr);
5044dea91ad1SHong Zhang     ci[0] = 0;
504501b7ae99SHong Zhang     for (i=0; i<am; i++) {
5046dea91ad1SHong Zhang       ci[i+1] = ci[i] + (ai[i+1] - ai[i]) + (bi[i+1] - bi[i]);
504701b7ae99SHong Zhang     }
5048dea91ad1SHong Zhang     ierr = PetscMalloc((1+ci[am])*sizeof(PetscInt),&cj);CHKERRQ(ierr);
5049dea91ad1SHong Zhang     ierr = PetscMalloc((1+ci[am])*sizeof(PetscScalar),&ca);CHKERRQ(ierr);
5050dea91ad1SHong Zhang     k = 0;
505101b7ae99SHong Zhang     for (i=0; i<am; i++) {
50525a7d977cSHong Zhang       ncols_o = bi[i+1] - bi[i];
50535a7d977cSHong Zhang       ncols_d = ai[i+1] - ai[i];
505401b7ae99SHong Zhang       /* off-diagonal portion of A */
50555a7d977cSHong Zhang       for (jo=0; jo<ncols_o; jo++) {
50565a7d977cSHong Zhang         col = cmap[*bj];
50575a7d977cSHong Zhang         if (col >= cstart) break;
50585a7d977cSHong Zhang         cj[k]   = col; bj++;
50595a7d977cSHong Zhang         ca[k++] = *ba++;
50605a7d977cSHong Zhang       }
50615a7d977cSHong Zhang       /* diagonal portion of A */
50625a7d977cSHong Zhang       for (j=0; j<ncols_d; j++) {
50635a7d977cSHong Zhang         cj[k]   = cstart + *aj++;
50645a7d977cSHong Zhang         ca[k++] = *aa++;
50655a7d977cSHong Zhang       }
50665a7d977cSHong Zhang       /* off-diagonal portion of A */
50675a7d977cSHong Zhang       for (j=jo; j<ncols_o; j++) {
50685a7d977cSHong Zhang         cj[k]   = cmap[*bj++];
50695a7d977cSHong Zhang         ca[k++] = *ba++;
50705a7d977cSHong Zhang       }
507125616d81SHong Zhang     }
5072dea91ad1SHong Zhang     /* put together the new matrix */
5073d0f46423SBarry Smith     ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,am,A->cmap->N,ci,cj,ca,A_loc);CHKERRQ(ierr);
5074dea91ad1SHong Zhang     /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
5075dea91ad1SHong Zhang     /* Since these are PETSc arrays, change flags to free them as necessary. */
5076dea91ad1SHong Zhang     mat          = (Mat_SeqAIJ*)(*A_loc)->data;
5077e6b907acSBarry Smith     mat->free_a  = PETSC_TRUE;
5078e6b907acSBarry Smith     mat->free_ij = PETSC_TRUE;
5079dea91ad1SHong Zhang     mat->nonew   = 0;
50805a7d977cSHong Zhang   } else if (scall == MAT_REUSE_MATRIX) {
50815a7d977cSHong Zhang     mat=(Mat_SeqAIJ*)(*A_loc)->data;
5082a77337e4SBarry Smith     ci = mat->i; cj = mat->j; cam = mat->a;
50835a7d977cSHong Zhang     for (i=0; i<am; i++) {
50845a7d977cSHong Zhang       /* off-diagonal portion of A */
50855a7d977cSHong Zhang       ncols_o = bi[i+1] - bi[i];
50865a7d977cSHong Zhang       for (jo=0; jo<ncols_o; jo++) {
50875a7d977cSHong Zhang         col = cmap[*bj];
50885a7d977cSHong Zhang         if (col >= cstart) break;
5089a77337e4SBarry Smith         *cam++ = *ba++; bj++;
50905a7d977cSHong Zhang       }
50915a7d977cSHong Zhang       /* diagonal portion of A */
5092ecc9b87dSHong Zhang       ncols_d = ai[i+1] - ai[i];
5093a77337e4SBarry Smith       for (j=0; j<ncols_d; j++) *cam++ = *aa++;
50945a7d977cSHong Zhang       /* off-diagonal portion of A */
5095f33d1a9aSHong Zhang       for (j=jo; j<ncols_o; j++) {
5096a77337e4SBarry Smith         *cam++ = *ba++; bj++;
5097f33d1a9aSHong Zhang       }
50985a7d977cSHong Zhang     }
50998661ff28SBarry Smith   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall);
51004ebed01fSBarry Smith   ierr = PetscLogEventEnd(MAT_Getlocalmat,A,0,0,0);CHKERRQ(ierr);
510125616d81SHong Zhang   PetscFunctionReturn(0);
510225616d81SHong Zhang }
510325616d81SHong Zhang 
510432fba14fSHong Zhang #undef __FUNCT__
51054a2b5492SBarry Smith #define __FUNCT__ "MatMPIAIJGetLocalMatCondensed"
510632fba14fSHong Zhang /*@C
5107ba264940SBarry Smith      MatMPIAIJGetLocalMatCondensed - Creates a SeqAIJ matrix from an MPIAIJ matrix by taking all its local rows and NON-ZERO columns
510832fba14fSHong Zhang 
510932fba14fSHong Zhang     Not Collective
511032fba14fSHong Zhang 
511132fba14fSHong Zhang    Input Parameters:
511232fba14fSHong Zhang +    A - the matrix
511332fba14fSHong Zhang .    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
511432fba14fSHong Zhang -    row, col - index sets of rows and columns to extract (or PETSC_NULL)
511532fba14fSHong Zhang 
511632fba14fSHong Zhang    Output Parameter:
511732fba14fSHong Zhang .    A_loc - the local sequential matrix generated
511832fba14fSHong Zhang 
511932fba14fSHong Zhang     Level: developer
512032fba14fSHong Zhang 
5121ba264940SBarry Smith .seealso: MatGetOwnershipRange(), MatMPIAIJGetLocalMat()
5122ba264940SBarry Smith 
512332fba14fSHong Zhang @*/
51244a2b5492SBarry Smith PetscErrorCode  MatMPIAIJGetLocalMatCondensed(Mat A,MatReuse scall,IS *row,IS *col,Mat *A_loc)
512532fba14fSHong Zhang {
512632fba14fSHong Zhang   Mat_MPIAIJ        *a=(Mat_MPIAIJ*)A->data;
512732fba14fSHong Zhang   PetscErrorCode    ierr;
512832fba14fSHong Zhang   PetscInt          i,start,end,ncols,nzA,nzB,*cmap,imark,*idx;
512932fba14fSHong Zhang   IS                isrowa,iscola;
513032fba14fSHong Zhang   Mat               *aloc;
51314a2b5492SBarry Smith   PetscBool       match;
513232fba14fSHong Zhang 
513332fba14fSHong Zhang   PetscFunctionBegin;
5134251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&match);CHKERRQ(ierr);
51354a2b5492SBarry Smith   if (!match) SETERRQ(((PetscObject)A)->comm, PETSC_ERR_SUP,"Requires MPIAIJ matrix as input");
51364ebed01fSBarry Smith   ierr = PetscLogEventBegin(MAT_Getlocalmatcondensed,A,0,0,0);CHKERRQ(ierr);
513732fba14fSHong Zhang   if (!row) {
5138d0f46423SBarry Smith     start = A->rmap->rstart; end = A->rmap->rend;
513932fba14fSHong Zhang     ierr = ISCreateStride(PETSC_COMM_SELF,end-start,start,1,&isrowa);CHKERRQ(ierr);
514032fba14fSHong Zhang   } else {
514132fba14fSHong Zhang     isrowa = *row;
514232fba14fSHong Zhang   }
514332fba14fSHong Zhang   if (!col) {
5144d0f46423SBarry Smith     start = A->cmap->rstart;
514532fba14fSHong Zhang     cmap  = a->garray;
5146d0f46423SBarry Smith     nzA   = a->A->cmap->n;
5147d0f46423SBarry Smith     nzB   = a->B->cmap->n;
514832fba14fSHong Zhang     ierr  = PetscMalloc((nzA+nzB)*sizeof(PetscInt), &idx);CHKERRQ(ierr);
514932fba14fSHong Zhang     ncols = 0;
515032fba14fSHong Zhang     for (i=0; i<nzB; i++) {
515132fba14fSHong Zhang       if (cmap[i] < start) idx[ncols++] = cmap[i];
515232fba14fSHong Zhang       else break;
515332fba14fSHong Zhang     }
515432fba14fSHong Zhang     imark = i;
515532fba14fSHong Zhang     for (i=0; i<nzA; i++) idx[ncols++] = start + i;
515632fba14fSHong Zhang     for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i];
5157d67e408aSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,&iscola);CHKERRQ(ierr);
515832fba14fSHong Zhang   } else {
515932fba14fSHong Zhang     iscola = *col;
516032fba14fSHong Zhang   }
516132fba14fSHong Zhang   if (scall != MAT_INITIAL_MATRIX) {
516232fba14fSHong Zhang     ierr = PetscMalloc(sizeof(Mat),&aloc);CHKERRQ(ierr);
516332fba14fSHong Zhang     aloc[0] = *A_loc;
516432fba14fSHong Zhang   }
516532fba14fSHong Zhang   ierr = MatGetSubMatrices(A,1,&isrowa,&iscola,scall,&aloc);CHKERRQ(ierr);
516632fba14fSHong Zhang   *A_loc = aloc[0];
516732fba14fSHong Zhang   ierr = PetscFree(aloc);CHKERRQ(ierr);
516832fba14fSHong Zhang   if (!row) {
51696bf464f9SBarry Smith     ierr = ISDestroy(&isrowa);CHKERRQ(ierr);
517032fba14fSHong Zhang   }
517132fba14fSHong Zhang   if (!col) {
51726bf464f9SBarry Smith     ierr = ISDestroy(&iscola);CHKERRQ(ierr);
517332fba14fSHong Zhang   }
51744ebed01fSBarry Smith   ierr = PetscLogEventEnd(MAT_Getlocalmatcondensed,A,0,0,0);CHKERRQ(ierr);
517532fba14fSHong Zhang   PetscFunctionReturn(0);
517632fba14fSHong Zhang }
517732fba14fSHong Zhang 
517825616d81SHong Zhang #undef __FUNCT__
517925616d81SHong Zhang #define __FUNCT__ "MatGetBrowsOfAcols"
518025616d81SHong Zhang /*@C
518132fba14fSHong Zhang     MatGetBrowsOfAcols - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns of local A
518225616d81SHong Zhang 
518325616d81SHong Zhang     Collective on Mat
518425616d81SHong Zhang 
518525616d81SHong Zhang    Input Parameters:
5186e240928fSHong Zhang +    A,B - the matrices in mpiaij format
518725616d81SHong Zhang .    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
518825616d81SHong Zhang -    rowb, colb - index sets of rows and columns of B to extract (or PETSC_NULL)
518925616d81SHong Zhang 
519025616d81SHong Zhang    Output Parameter:
519125616d81SHong Zhang +    rowb, colb - index sets of rows and columns of B to extract
519225616d81SHong Zhang -    B_seq - the sequential matrix generated
519325616d81SHong Zhang 
519425616d81SHong Zhang     Level: developer
519525616d81SHong Zhang 
519625616d81SHong Zhang @*/
519766bfb163SHong Zhang PetscErrorCode  MatGetBrowsOfAcols(Mat A,Mat B,MatReuse scall,IS *rowb,IS *colb,Mat *B_seq)
519825616d81SHong Zhang {
5199899cda47SBarry Smith   Mat_MPIAIJ        *a=(Mat_MPIAIJ*)A->data;
520025616d81SHong Zhang   PetscErrorCode    ierr;
5201b1d57f15SBarry Smith   PetscInt          *idx,i,start,ncols,nzA,nzB,*cmap,imark;
520225616d81SHong Zhang   IS                isrowb,iscolb;
520366bfb163SHong Zhang   Mat               *bseq=PETSC_NULL;
520425616d81SHong Zhang 
520525616d81SHong Zhang   PetscFunctionBegin;
5206d0f46423SBarry Smith   if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend) {
5207e32f2f54SBarry 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);
520825616d81SHong Zhang   }
52094ebed01fSBarry Smith   ierr = PetscLogEventBegin(MAT_GetBrowsOfAcols,A,B,0,0);CHKERRQ(ierr);
521025616d81SHong Zhang 
521125616d81SHong Zhang   if (scall == MAT_INITIAL_MATRIX) {
5212d0f46423SBarry Smith     start = A->cmap->rstart;
521325616d81SHong Zhang     cmap  = a->garray;
5214d0f46423SBarry Smith     nzA   = a->A->cmap->n;
5215d0f46423SBarry Smith     nzB   = a->B->cmap->n;
5216b1d57f15SBarry Smith     ierr  = PetscMalloc((nzA+nzB)*sizeof(PetscInt), &idx);CHKERRQ(ierr);
521725616d81SHong Zhang     ncols = 0;
52180390132cSHong Zhang     for (i=0; i<nzB; i++) {  /* row < local row index */
521925616d81SHong Zhang       if (cmap[i] < start) idx[ncols++] = cmap[i];
522025616d81SHong Zhang       else break;
522125616d81SHong Zhang     }
522225616d81SHong Zhang     imark = i;
52230390132cSHong Zhang     for (i=0; i<nzA; i++) idx[ncols++] = start + i;  /* local rows */
52240390132cSHong Zhang     for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i]; /* row > local row index */
5225d67e408aSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,&isrowb);CHKERRQ(ierr);
5226d0f46423SBarry Smith     ierr = ISCreateStride(PETSC_COMM_SELF,B->cmap->N,0,1,&iscolb);CHKERRQ(ierr);
522725616d81SHong Zhang   } else {
5228e32f2f54SBarry Smith     if (!rowb || !colb) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"IS rowb and colb must be provided for MAT_REUSE_MATRIX");
522925616d81SHong Zhang     isrowb = *rowb; iscolb = *colb;
523025616d81SHong Zhang     ierr = PetscMalloc(sizeof(Mat),&bseq);CHKERRQ(ierr);
523125616d81SHong Zhang     bseq[0] = *B_seq;
523225616d81SHong Zhang   }
523325616d81SHong Zhang   ierr = MatGetSubMatrices(B,1,&isrowb,&iscolb,scall,&bseq);CHKERRQ(ierr);
523425616d81SHong Zhang   *B_seq = bseq[0];
523525616d81SHong Zhang   ierr = PetscFree(bseq);CHKERRQ(ierr);
523625616d81SHong Zhang   if (!rowb) {
52376bf464f9SBarry Smith     ierr = ISDestroy(&isrowb);CHKERRQ(ierr);
523825616d81SHong Zhang   } else {
523925616d81SHong Zhang     *rowb = isrowb;
524025616d81SHong Zhang   }
524125616d81SHong Zhang   if (!colb) {
52426bf464f9SBarry Smith     ierr = ISDestroy(&iscolb);CHKERRQ(ierr);
524325616d81SHong Zhang   } else {
524425616d81SHong Zhang     *colb = iscolb;
524525616d81SHong Zhang   }
52464ebed01fSBarry Smith   ierr = PetscLogEventEnd(MAT_GetBrowsOfAcols,A,B,0,0);CHKERRQ(ierr);
524725616d81SHong Zhang   PetscFunctionReturn(0);
524825616d81SHong Zhang }
5249429d309bSHong Zhang 
5250a61c8c0fSHong Zhang #undef __FUNCT__
5251f8487c73SHong Zhang #define __FUNCT__ "MatGetBrowsOfAoCols_MPIAIJ"
5252f8487c73SHong Zhang /*
5253f8487c73SHong Zhang     MatGetBrowsOfAoCols_MPIAIJ - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns
525401b7ae99SHong Zhang     of the OFF-DIAGONAL portion of local A
5255429d309bSHong Zhang 
5256429d309bSHong Zhang     Collective on Mat
5257429d309bSHong Zhang 
5258429d309bSHong Zhang    Input Parameters:
5259429d309bSHong Zhang +    A,B - the matrices in mpiaij format
5260598bc09dSHong Zhang -    scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
5261429d309bSHong Zhang 
5262429d309bSHong Zhang    Output Parameter:
5263b7f45c76SHong Zhang +    startsj_s - starting point in B's sending j-arrays, saved for MAT_REUSE (or PETSC_NULL)
5264b7f45c76SHong Zhang .    startsj_r - starting point in B's receiving j-arrays, saved for MAT_REUSE (or PETSC_NULL)
5265598bc09dSHong Zhang .    bufa_ptr - array for sending matrix values, saved for MAT_REUSE (or PETSC_NULL)
5266598bc09dSHong Zhang -    B_oth - the sequential matrix generated with size aBn=a->B->cmap->n by B->cmap->N
5267429d309bSHong Zhang 
5268429d309bSHong Zhang     Level: developer
5269429d309bSHong Zhang 
5270f8487c73SHong Zhang */
5271b7f45c76SHong Zhang PetscErrorCode  MatGetBrowsOfAoCols_MPIAIJ(Mat A,Mat B,MatReuse scall,PetscInt **startsj_s,PetscInt **startsj_r,MatScalar **bufa_ptr,Mat *B_oth)
5272429d309bSHong Zhang {
5273a6b2eed2SHong Zhang   VecScatter_MPI_General *gen_to,*gen_from;
5274429d309bSHong Zhang   PetscErrorCode         ierr;
5275899cda47SBarry Smith   Mat_MPIAIJ             *a=(Mat_MPIAIJ*)A->data;
527687025532SHong Zhang   Mat_SeqAIJ             *b_oth;
5277a6b2eed2SHong Zhang   VecScatter             ctx=a->Mvctx;
52787adad957SLisandro Dalcin   MPI_Comm               comm=((PetscObject)ctx)->comm;
52797adad957SLisandro Dalcin   PetscMPIInt            *rprocs,*sprocs,tag=((PetscObject)ctx)->tag,rank;
5280d0f46423SBarry Smith   PetscInt               *rowlen,*bufj,*bufJ,ncols,aBn=a->B->cmap->n,row,*b_othi,*b_othj;
5281dd6ea824SBarry Smith   PetscScalar            *rvalues,*svalues;
5282dd6ea824SBarry Smith   MatScalar              *b_otha,*bufa,*bufA;
5283e42f35eeSHong Zhang   PetscInt               i,j,k,l,ll,nrecvs,nsends,nrows,*srow,*rstarts,*rstartsj = 0,*sstarts,*sstartsj,len;
5284910ba992SMatthew Knepley   MPI_Request            *rwaits = PETSC_NULL,*swaits = PETSC_NULL;
528587025532SHong Zhang   MPI_Status             *sstatus,rstatus;
5286aa5bb8c0SSatish Balay   PetscMPIInt            jj;
5287e42f35eeSHong Zhang   PetscInt               *cols,sbs,rbs;
5288ba8c8a56SBarry Smith   PetscScalar            *vals;
5289429d309bSHong Zhang 
5290429d309bSHong Zhang   PetscFunctionBegin;
5291d0f46423SBarry Smith   if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend) {
5292e32f2f54SBarry 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);
5293429d309bSHong Zhang   }
52944ebed01fSBarry Smith   ierr = PetscLogEventBegin(MAT_GetBrowsOfAocols,A,B,0,0);CHKERRQ(ierr);
5295a6b2eed2SHong Zhang   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
5296a6b2eed2SHong Zhang 
5297a6b2eed2SHong Zhang   gen_to   = (VecScatter_MPI_General*)ctx->todata;
5298a6b2eed2SHong Zhang   gen_from = (VecScatter_MPI_General*)ctx->fromdata;
5299e42f35eeSHong Zhang   rvalues  = gen_from->values; /* holds the length of receiving row */
5300e42f35eeSHong Zhang   svalues  = gen_to->values;   /* holds the length of sending row */
5301a6b2eed2SHong Zhang   nrecvs   = gen_from->n;
5302a6b2eed2SHong Zhang   nsends   = gen_to->n;
5303d7ee0231SBarry Smith 
5304d7ee0231SBarry Smith   ierr = PetscMalloc2(nrecvs,MPI_Request,&rwaits,nsends,MPI_Request,&swaits);CHKERRQ(ierr);
5305a6b2eed2SHong Zhang   srow     = gen_to->indices;   /* local row index to be sent */
5306a6b2eed2SHong Zhang   sstarts  = gen_to->starts;
5307a6b2eed2SHong Zhang   sprocs   = gen_to->procs;
5308a6b2eed2SHong Zhang   sstatus  = gen_to->sstatus;
5309e42f35eeSHong Zhang   sbs      = gen_to->bs;
5310e42f35eeSHong Zhang   rstarts  = gen_from->starts;
5311e42f35eeSHong Zhang   rprocs   = gen_from->procs;
5312e42f35eeSHong Zhang   rbs      = gen_from->bs;
5313429d309bSHong Zhang 
5314b7f45c76SHong Zhang   if (!startsj_s || !bufa_ptr) scall = MAT_INITIAL_MATRIX;
5315429d309bSHong Zhang   if (scall == MAT_INITIAL_MATRIX) {
5316a6b2eed2SHong Zhang     /* i-array */
5317a6b2eed2SHong Zhang     /*---------*/
5318a6b2eed2SHong Zhang     /*  post receives */
5319a6b2eed2SHong Zhang     for (i=0; i<nrecvs; i++) {
5320e42f35eeSHong Zhang       rowlen = (PetscInt*)rvalues + rstarts[i]*rbs;
5321e42f35eeSHong Zhang       nrows = (rstarts[i+1]-rstarts[i])*rbs; /* num of indices to be received */
532287025532SHong Zhang       ierr = MPI_Irecv(rowlen,nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr);
5323429d309bSHong Zhang     }
5324a6b2eed2SHong Zhang 
5325a6b2eed2SHong Zhang     /* pack the outgoing message */
53261d79065fSBarry Smith     ierr = PetscMalloc2(nsends+1,PetscInt,&sstartsj,nrecvs+1,PetscInt,&rstartsj);CHKERRQ(ierr);
5327a6b2eed2SHong Zhang     sstartsj[0] = 0;  rstartsj[0] = 0;
5328a6b2eed2SHong Zhang     len = 0; /* total length of j or a array to be sent */
5329a6b2eed2SHong Zhang     k = 0;
5330a6b2eed2SHong Zhang     for (i=0; i<nsends; i++) {
5331e42f35eeSHong Zhang       rowlen = (PetscInt*)svalues + sstarts[i]*sbs;
5332e42f35eeSHong Zhang       nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */
533387025532SHong Zhang       for (j=0; j<nrows; j++) {
5334d0f46423SBarry Smith         row = srow[k] + B->rmap->range[rank]; /* global row idx */
5335e42f35eeSHong Zhang         for (l=0; l<sbs; l++) {
5336e42f35eeSHong Zhang           ierr = MatGetRow_MPIAIJ(B,row+l,&ncols,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); /* rowlength */
5337e42f35eeSHong Zhang           rowlen[j*sbs+l] = ncols;
5338e42f35eeSHong Zhang           len += ncols;
5339e42f35eeSHong Zhang           ierr = MatRestoreRow_MPIAIJ(B,row+l,&ncols,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
5340e42f35eeSHong Zhang         }
5341a6b2eed2SHong Zhang         k++;
5342429d309bSHong Zhang       }
5343e42f35eeSHong Zhang       ierr = MPI_Isend(rowlen,nrows*sbs,MPIU_INT,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr);
5344dea91ad1SHong Zhang       sstartsj[i+1] = len;  /* starting point of (i+1)-th outgoing msg in bufj and bufa */
5345429d309bSHong Zhang     }
534687025532SHong Zhang     /* recvs and sends of i-array are completed */
534787025532SHong Zhang     i = nrecvs;
534887025532SHong Zhang     while (i--) {
5349aa5bb8c0SSatish Balay       ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr);
535087025532SHong Zhang     }
53510c468ba9SBarry Smith     if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);}
5352e42f35eeSHong Zhang 
5353a6b2eed2SHong Zhang     /* allocate buffers for sending j and a arrays */
5354a6b2eed2SHong Zhang     ierr = PetscMalloc((len+1)*sizeof(PetscInt),&bufj);CHKERRQ(ierr);
5355a6b2eed2SHong Zhang     ierr = PetscMalloc((len+1)*sizeof(PetscScalar),&bufa);CHKERRQ(ierr);
5356a6b2eed2SHong Zhang 
535787025532SHong Zhang     /* create i-array of B_oth */
535887025532SHong Zhang     ierr = PetscMalloc((aBn+2)*sizeof(PetscInt),&b_othi);CHKERRQ(ierr);
535987025532SHong Zhang     b_othi[0] = 0;
5360a6b2eed2SHong Zhang     len = 0; /* total length of j or a array to be received */
5361a6b2eed2SHong Zhang     k = 0;
5362a6b2eed2SHong Zhang     for (i=0; i<nrecvs; i++) {
5363fd0ff01cSHong Zhang       rowlen = (PetscInt*)rvalues + rstarts[i]*rbs;
5364e42f35eeSHong Zhang       nrows = rbs*(rstarts[i+1]-rstarts[i]); /* num of rows to be recieved */
536587025532SHong Zhang       for (j=0; j<nrows; j++) {
536687025532SHong Zhang         b_othi[k+1] = b_othi[k] + rowlen[j];
5367a6b2eed2SHong Zhang         len += rowlen[j]; k++;
5368a6b2eed2SHong Zhang       }
5369dea91ad1SHong Zhang       rstartsj[i+1] = len; /* starting point of (i+1)-th incoming msg in bufj and bufa */
5370a6b2eed2SHong Zhang     }
5371a6b2eed2SHong Zhang 
537287025532SHong Zhang     /* allocate space for j and a arrrays of B_oth */
537387025532SHong Zhang     ierr = PetscMalloc((b_othi[aBn]+1)*sizeof(PetscInt),&b_othj);CHKERRQ(ierr);
5374dd6ea824SBarry Smith     ierr = PetscMalloc((b_othi[aBn]+1)*sizeof(MatScalar),&b_otha);CHKERRQ(ierr);
5375a6b2eed2SHong Zhang 
537687025532SHong Zhang     /* j-array */
537787025532SHong Zhang     /*---------*/
5378a6b2eed2SHong Zhang     /*  post receives of j-array */
5379a6b2eed2SHong Zhang     for (i=0; i<nrecvs; i++) {
538087025532SHong Zhang       nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */
538187025532SHong Zhang       ierr = MPI_Irecv(b_othj+rstartsj[i],nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr);
5382a6b2eed2SHong Zhang     }
5383e42f35eeSHong Zhang 
5384e42f35eeSHong Zhang     /* pack the outgoing message j-array */
5385a6b2eed2SHong Zhang     k = 0;
5386a6b2eed2SHong Zhang     for (i=0; i<nsends; i++) {
5387e42f35eeSHong Zhang       nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */
5388a6b2eed2SHong Zhang       bufJ = bufj+sstartsj[i];
538987025532SHong Zhang       for (j=0; j<nrows; j++) {
5390d0f46423SBarry Smith         row  = srow[k++] + B->rmap->range[rank]; /* global row idx */
5391e42f35eeSHong Zhang         for (ll=0; ll<sbs; ll++) {
5392e42f35eeSHong Zhang           ierr = MatGetRow_MPIAIJ(B,row+ll,&ncols,&cols,PETSC_NULL);CHKERRQ(ierr);
5393a6b2eed2SHong Zhang           for (l=0; l<ncols; l++) {
5394a6b2eed2SHong Zhang             *bufJ++ = cols[l];
539587025532SHong Zhang           }
5396e42f35eeSHong Zhang           ierr = MatRestoreRow_MPIAIJ(B,row+ll,&ncols,&cols,PETSC_NULL);CHKERRQ(ierr);
5397e42f35eeSHong Zhang         }
539887025532SHong Zhang       }
539987025532SHong Zhang       ierr = MPI_Isend(bufj+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_INT,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr);
540087025532SHong Zhang     }
540187025532SHong Zhang 
540287025532SHong Zhang     /* recvs and sends of j-array are completed */
540387025532SHong Zhang     i = nrecvs;
540487025532SHong Zhang     while (i--) {
5405aa5bb8c0SSatish Balay       ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr);
540687025532SHong Zhang     }
54070c468ba9SBarry Smith     if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);}
540887025532SHong Zhang   } else if (scall == MAT_REUSE_MATRIX) {
5409b7f45c76SHong Zhang     sstartsj = *startsj_s;
54101d79065fSBarry Smith     rstartsj = *startsj_r;
541187025532SHong Zhang     bufa     = *bufa_ptr;
541287025532SHong Zhang     b_oth    = (Mat_SeqAIJ*)(*B_oth)->data;
541387025532SHong Zhang     b_otha   = b_oth->a;
5414f23aa3ddSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Matrix P does not posses an object container");
541587025532SHong Zhang 
541687025532SHong Zhang   /* a-array */
541787025532SHong Zhang   /*---------*/
541887025532SHong Zhang   /*  post receives of a-array */
541987025532SHong Zhang   for (i=0; i<nrecvs; i++) {
542087025532SHong Zhang     nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */
542187025532SHong Zhang     ierr = MPI_Irecv(b_otha+rstartsj[i],nrows,MPIU_SCALAR,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr);
542287025532SHong Zhang   }
5423e42f35eeSHong Zhang 
5424e42f35eeSHong Zhang   /* pack the outgoing message a-array */
542587025532SHong Zhang   k = 0;
542687025532SHong Zhang   for (i=0; i<nsends; i++) {
5427e42f35eeSHong Zhang     nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */
542887025532SHong Zhang     bufA = bufa+sstartsj[i];
542987025532SHong Zhang     for (j=0; j<nrows; j++) {
5430d0f46423SBarry Smith       row  = srow[k++] + B->rmap->range[rank]; /* global row idx */
5431e42f35eeSHong Zhang       for (ll=0; ll<sbs; ll++) {
5432e42f35eeSHong Zhang         ierr = MatGetRow_MPIAIJ(B,row+ll,&ncols,PETSC_NULL,&vals);CHKERRQ(ierr);
543387025532SHong Zhang         for (l=0; l<ncols; l++) {
5434a6b2eed2SHong Zhang           *bufA++ = vals[l];
5435a6b2eed2SHong Zhang         }
5436e42f35eeSHong Zhang         ierr = MatRestoreRow_MPIAIJ(B,row+ll,&ncols,PETSC_NULL,&vals);CHKERRQ(ierr);
5437e42f35eeSHong Zhang       }
5438a6b2eed2SHong Zhang     }
543987025532SHong Zhang     ierr = MPI_Isend(bufa+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_SCALAR,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr);
5440a6b2eed2SHong Zhang   }
544187025532SHong Zhang   /* recvs and sends of a-array are completed */
544287025532SHong Zhang   i = nrecvs;
544387025532SHong Zhang   while (i--) {
5444aa5bb8c0SSatish Balay     ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr);
544587025532SHong Zhang   }
54460c468ba9SBarry Smith   if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);}
5447d7ee0231SBarry Smith   ierr = PetscFree2(rwaits,swaits);CHKERRQ(ierr);
5448a6b2eed2SHong Zhang 
544987025532SHong Zhang   if (scall == MAT_INITIAL_MATRIX) {
5450a6b2eed2SHong Zhang     /* put together the new matrix */
5451d0f46423SBarry Smith     ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,aBn,B->cmap->N,b_othi,b_othj,b_otha,B_oth);CHKERRQ(ierr);
5452a6b2eed2SHong Zhang 
5453a6b2eed2SHong Zhang     /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
5454a6b2eed2SHong Zhang     /* Since these are PETSc arrays, change flags to free them as necessary. */
545587025532SHong Zhang     b_oth          = (Mat_SeqAIJ *)(*B_oth)->data;
5456e6b907acSBarry Smith     b_oth->free_a  = PETSC_TRUE;
5457e6b907acSBarry Smith     b_oth->free_ij = PETSC_TRUE;
545887025532SHong Zhang     b_oth->nonew   = 0;
5459a6b2eed2SHong Zhang 
5460a6b2eed2SHong Zhang     ierr = PetscFree(bufj);CHKERRQ(ierr);
5461b7f45c76SHong Zhang     if (!startsj_s || !bufa_ptr) {
54621d79065fSBarry Smith       ierr = PetscFree2(sstartsj,rstartsj);CHKERRQ(ierr);
5463dea91ad1SHong Zhang       ierr = PetscFree(bufa_ptr);CHKERRQ(ierr);
5464dea91ad1SHong Zhang     } else {
5465b7f45c76SHong Zhang       *startsj_s = sstartsj;
54661d79065fSBarry Smith       *startsj_r = rstartsj;
546787025532SHong Zhang       *bufa_ptr  = bufa;
546887025532SHong Zhang     }
5469dea91ad1SHong Zhang   }
54704ebed01fSBarry Smith   ierr = PetscLogEventEnd(MAT_GetBrowsOfAocols,A,B,0,0);CHKERRQ(ierr);
5471429d309bSHong Zhang   PetscFunctionReturn(0);
5472429d309bSHong Zhang }
5473ccd8e176SBarry Smith 
547443eb5e2fSMatthew Knepley #undef __FUNCT__
547543eb5e2fSMatthew Knepley #define __FUNCT__ "MatGetCommunicationStructs"
547643eb5e2fSMatthew Knepley /*@C
547743eb5e2fSMatthew Knepley   MatGetCommunicationStructs - Provides access to the communication structures used in matrix-vector multiplication.
547843eb5e2fSMatthew Knepley 
547943eb5e2fSMatthew Knepley   Not Collective
548043eb5e2fSMatthew Knepley 
548143eb5e2fSMatthew Knepley   Input Parameters:
548243eb5e2fSMatthew Knepley . A - The matrix in mpiaij format
548343eb5e2fSMatthew Knepley 
548443eb5e2fSMatthew Knepley   Output Parameter:
548543eb5e2fSMatthew Knepley + lvec - The local vector holding off-process values from the argument to a matrix-vector product
548643eb5e2fSMatthew Knepley . colmap - A map from global column index to local index into lvec
548743eb5e2fSMatthew Knepley - multScatter - A scatter from the argument of a matrix-vector product to lvec
548843eb5e2fSMatthew Knepley 
548943eb5e2fSMatthew Knepley   Level: developer
549043eb5e2fSMatthew Knepley 
549143eb5e2fSMatthew Knepley @*/
549243eb5e2fSMatthew Knepley #if defined (PETSC_USE_CTABLE)
54937087cfbeSBarry Smith PetscErrorCode  MatGetCommunicationStructs(Mat A, Vec *lvec, PetscTable *colmap, VecScatter *multScatter)
549443eb5e2fSMatthew Knepley #else
54957087cfbeSBarry Smith PetscErrorCode  MatGetCommunicationStructs(Mat A, Vec *lvec, PetscInt *colmap[], VecScatter *multScatter)
549643eb5e2fSMatthew Knepley #endif
549743eb5e2fSMatthew Knepley {
549843eb5e2fSMatthew Knepley   Mat_MPIAIJ *a;
549943eb5e2fSMatthew Knepley 
550043eb5e2fSMatthew Knepley   PetscFunctionBegin;
55010700a824SBarry Smith   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
5502e414b56bSJed Brown   PetscValidPointer(lvec, 2);
5503e414b56bSJed Brown   PetscValidPointer(colmap, 3);
5504e414b56bSJed Brown   PetscValidPointer(multScatter, 4);
550543eb5e2fSMatthew Knepley   a = (Mat_MPIAIJ *) A->data;
550643eb5e2fSMatthew Knepley   if (lvec) *lvec = a->lvec;
550743eb5e2fSMatthew Knepley   if (colmap) *colmap = a->colmap;
550843eb5e2fSMatthew Knepley   if (multScatter) *multScatter = a->Mvctx;
550943eb5e2fSMatthew Knepley   PetscFunctionReturn(0);
551043eb5e2fSMatthew Knepley }
551143eb5e2fSMatthew Knepley 
551217667f90SBarry Smith EXTERN_C_BEGIN
551319fd82e9SBarry Smith extern PetscErrorCode  MatConvert_MPIAIJ_MPIAIJCRL(Mat,MatType,MatReuse,Mat*);
551419fd82e9SBarry Smith extern PetscErrorCode  MatConvert_MPIAIJ_MPIAIJPERM(Mat,MatType,MatReuse,Mat*);
551519fd82e9SBarry Smith extern PetscErrorCode  MatConvert_MPIAIJ_MPISBAIJ(Mat,MatType,MatReuse,Mat*);
551617667f90SBarry Smith EXTERN_C_END
551717667f90SBarry Smith 
5518fc4dec0aSBarry Smith #undef __FUNCT__
5519fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMultNumeric_MPIDense_MPIAIJ"
5520fc4dec0aSBarry Smith /*
5521fc4dec0aSBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
5522fc4dec0aSBarry Smith 
5523fc4dec0aSBarry Smith                n                       p                          p
5524fc4dec0aSBarry Smith         (              )       (              )         (                  )
5525fc4dec0aSBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
5526fc4dec0aSBarry Smith         (              )       (              )         (                  )
5527fc4dec0aSBarry Smith 
5528fc4dec0aSBarry Smith */
5529fc4dec0aSBarry Smith PetscErrorCode MatMatMultNumeric_MPIDense_MPIAIJ(Mat A,Mat B,Mat C)
5530fc4dec0aSBarry Smith {
5531fc4dec0aSBarry Smith   PetscErrorCode     ierr;
5532fc4dec0aSBarry Smith   Mat                At,Bt,Ct;
5533fc4dec0aSBarry Smith 
5534fc4dec0aSBarry Smith   PetscFunctionBegin;
5535fc4dec0aSBarry Smith   ierr = MatTranspose(A,MAT_INITIAL_MATRIX,&At);CHKERRQ(ierr);
5536fc4dec0aSBarry Smith   ierr = MatTranspose(B,MAT_INITIAL_MATRIX,&Bt);CHKERRQ(ierr);
5537fc4dec0aSBarry Smith   ierr = MatMatMult(Bt,At,MAT_INITIAL_MATRIX,1.0,&Ct);CHKERRQ(ierr);
55386bf464f9SBarry Smith   ierr = MatDestroy(&At);CHKERRQ(ierr);
55396bf464f9SBarry Smith   ierr = MatDestroy(&Bt);CHKERRQ(ierr);
5540fc4dec0aSBarry Smith   ierr = MatTranspose(Ct,MAT_REUSE_MATRIX,&C);CHKERRQ(ierr);
55416bf464f9SBarry Smith   ierr = MatDestroy(&Ct);CHKERRQ(ierr);
5542fc4dec0aSBarry Smith   PetscFunctionReturn(0);
5543fc4dec0aSBarry Smith }
5544fc4dec0aSBarry Smith 
5545fc4dec0aSBarry Smith #undef __FUNCT__
5546fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMultSymbolic_MPIDense_MPIAIJ"
5547fc4dec0aSBarry Smith PetscErrorCode MatMatMultSymbolic_MPIDense_MPIAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
5548fc4dec0aSBarry Smith {
5549fc4dec0aSBarry Smith   PetscErrorCode ierr;
5550d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
5551fc4dec0aSBarry Smith   Mat            Cmat;
5552fc4dec0aSBarry Smith 
5553fc4dec0aSBarry Smith   PetscFunctionBegin;
5554e32f2f54SBarry 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);
555539804f7cSBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,&Cmat);CHKERRQ(ierr);
5556fc4dec0aSBarry Smith   ierr = MatSetSizes(Cmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
5557a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(Cmat,A->rmap->bs,B->cmap->bs);CHKERRQ(ierr);
5558fc4dec0aSBarry Smith   ierr = MatSetType(Cmat,MATMPIDENSE);CHKERRQ(ierr);
5559fc4dec0aSBarry Smith   ierr = MatMPIDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr);
556038556019SBarry Smith   ierr = MatAssemblyBegin(Cmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
556138556019SBarry Smith   ierr = MatAssemblyEnd(Cmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
5562f75ecaa4SHong Zhang 
5563f75ecaa4SHong Zhang   Cmat->ops->matmultnumeric = MatMatMultNumeric_MPIDense_MPIAIJ;
5564fc4dec0aSBarry Smith   *C = Cmat;
5565fc4dec0aSBarry Smith   PetscFunctionReturn(0);
5566fc4dec0aSBarry Smith }
5567fc4dec0aSBarry Smith 
5568fc4dec0aSBarry Smith /* ----------------------------------------------------------------*/
5569fc4dec0aSBarry Smith #undef __FUNCT__
5570fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMult_MPIDense_MPIAIJ"
5571fc4dec0aSBarry Smith PetscErrorCode MatMatMult_MPIDense_MPIAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
5572fc4dec0aSBarry Smith {
5573fc4dec0aSBarry Smith   PetscErrorCode ierr;
5574fc4dec0aSBarry Smith 
5575fc4dec0aSBarry Smith   PetscFunctionBegin;
5576fc4dec0aSBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
5577fc4dec0aSBarry Smith     ierr = MatMatMultSymbolic_MPIDense_MPIAIJ(A,B,fill,C);CHKERRQ(ierr);
5578fc4dec0aSBarry Smith   }
5579fc4dec0aSBarry Smith   ierr = MatMatMultNumeric_MPIDense_MPIAIJ(A,B,*C);CHKERRQ(ierr);
5580fc4dec0aSBarry Smith   PetscFunctionReturn(0);
5581fc4dec0aSBarry Smith }
5582fc4dec0aSBarry Smith 
55835c9eb25fSBarry Smith EXTERN_C_BEGIN
5584611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
5585bccb9932SShri Abhyankar extern PetscErrorCode MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*);
5586611f576cSBarry Smith #endif
55873bf14a46SMatthew Knepley #if defined(PETSC_HAVE_PASTIX)
55883bf14a46SMatthew Knepley extern PetscErrorCode MatGetFactor_mpiaij_pastix(Mat,MatFactorType,Mat*);
55893bf14a46SMatthew Knepley #endif
5590611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU_DIST)
55915c9eb25fSBarry Smith extern PetscErrorCode MatGetFactor_mpiaij_superlu_dist(Mat,MatFactorType,Mat*);
5592611f576cSBarry Smith #endif
559317f1a0eaSHong Zhang #if defined(PETSC_HAVE_CLIQUE)
559417f1a0eaSHong Zhang extern PetscErrorCode MatGetFactor_aij_clique(Mat,MatFactorType,Mat*);
559517f1a0eaSHong Zhang #endif
55965c9eb25fSBarry Smith EXTERN_C_END
55975c9eb25fSBarry Smith 
5598ccd8e176SBarry Smith /*MC
5599ccd8e176SBarry Smith    MATMPIAIJ - MATMPIAIJ = "mpiaij" - A matrix type to be used for parallel sparse matrices.
5600ccd8e176SBarry Smith 
5601ccd8e176SBarry Smith    Options Database Keys:
5602ccd8e176SBarry Smith . -mat_type mpiaij - sets the matrix type to "mpiaij" during a call to MatSetFromOptions()
5603ccd8e176SBarry Smith 
5604ccd8e176SBarry Smith   Level: beginner
5605ccd8e176SBarry Smith 
560669b1f4b7SBarry Smith .seealso: MatCreateAIJ()
5607ccd8e176SBarry Smith M*/
5608ccd8e176SBarry Smith 
5609ccd8e176SBarry Smith EXTERN_C_BEGIN
5610ccd8e176SBarry Smith #undef __FUNCT__
5611ccd8e176SBarry Smith #define __FUNCT__ "MatCreate_MPIAIJ"
56127087cfbeSBarry Smith PetscErrorCode  MatCreate_MPIAIJ(Mat B)
5613ccd8e176SBarry Smith {
5614ccd8e176SBarry Smith   Mat_MPIAIJ     *b;
5615ccd8e176SBarry Smith   PetscErrorCode ierr;
5616ccd8e176SBarry Smith   PetscMPIInt    size;
5617ccd8e176SBarry Smith 
5618ccd8e176SBarry Smith   PetscFunctionBegin;
56197adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
562038f2d2fdSLisandro Dalcin   ierr            = PetscNewLog(B,Mat_MPIAIJ,&b);CHKERRQ(ierr);
5621ccd8e176SBarry Smith   B->data         = (void*)b;
5622ccd8e176SBarry Smith   ierr            = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
5623ccd8e176SBarry Smith   B->assembled    = PETSC_FALSE;
5624ccd8e176SBarry Smith   B->insertmode   = NOT_SET_VALUES;
5625ccd8e176SBarry Smith   b->size         = size;
56267adad957SLisandro Dalcin   ierr = MPI_Comm_rank(((PetscObject)B)->comm,&b->rank);CHKERRQ(ierr);
5627ccd8e176SBarry Smith 
5628ccd8e176SBarry Smith   /* build cache for off array entries formed */
56297adad957SLisandro Dalcin   ierr = MatStashCreate_Private(((PetscObject)B)->comm,1,&B->stash);CHKERRQ(ierr);
5630ccd8e176SBarry Smith   b->donotstash  = PETSC_FALSE;
5631ccd8e176SBarry Smith   b->colmap      = 0;
5632ccd8e176SBarry Smith   b->garray      = 0;
5633ccd8e176SBarry Smith   b->roworiented = PETSC_TRUE;
5634ccd8e176SBarry Smith 
5635ccd8e176SBarry Smith   /* stuff used for matrix vector multiply */
5636ccd8e176SBarry Smith   b->lvec      = PETSC_NULL;
5637ccd8e176SBarry Smith   b->Mvctx     = PETSC_NULL;
5638ccd8e176SBarry Smith 
5639ccd8e176SBarry Smith   /* stuff for MatGetRow() */
5640ccd8e176SBarry Smith   b->rowindices   = 0;
5641ccd8e176SBarry Smith   b->rowvalues    = 0;
5642ccd8e176SBarry Smith   b->getrowactive = PETSC_FALSE;
5643ccd8e176SBarry Smith 
5644bbf3fe20SPaul Mullowney   /* flexible pointer used in CUSP/CUSPARSE classes */
5645bbf3fe20SPaul Mullowney   b->spptr        = PETSC_NULL;
5646f60c3dc2SHong Zhang 
5647611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
5648ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C",
5649bccb9932SShri Abhyankar                                      "MatGetFactor_aij_mumps",
5650bccb9932SShri Abhyankar                                      MatGetFactor_aij_mumps);CHKERRQ(ierr);
5651611f576cSBarry Smith #endif
56523bf14a46SMatthew Knepley #if defined(PETSC_HAVE_PASTIX)
5653ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C",
56543bf14a46SMatthew Knepley                                            "MatGetFactor_mpiaij_pastix",
56553bf14a46SMatthew Knepley                                            MatGetFactor_mpiaij_pastix);CHKERRQ(ierr);
56563bf14a46SMatthew Knepley #endif
5657611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU_DIST)
5658ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_dist_C",
56595c9eb25fSBarry Smith                                      "MatGetFactor_mpiaij_superlu_dist",
56605c9eb25fSBarry Smith                                      MatGetFactor_mpiaij_superlu_dist);CHKERRQ(ierr);
5661611f576cSBarry Smith #endif
566217f1a0eaSHong Zhang #if defined(PETSC_HAVE_CLIQUE)
566317f1a0eaSHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_clique_C",
566417f1a0eaSHong Zhang                                      "MatGetFactor_aij_clique",
566517f1a0eaSHong Zhang                                      MatGetFactor_aij_clique);CHKERRQ(ierr);
566617f1a0eaSHong Zhang #endif
5667ccd8e176SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
5668ccd8e176SBarry Smith                                      "MatStoreValues_MPIAIJ",
5669ccd8e176SBarry Smith                                      MatStoreValues_MPIAIJ);CHKERRQ(ierr);
5670ccd8e176SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
5671ccd8e176SBarry Smith                                      "MatRetrieveValues_MPIAIJ",
5672ccd8e176SBarry Smith                                      MatRetrieveValues_MPIAIJ);CHKERRQ(ierr);
5673ccd8e176SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetDiagonalBlock_C",
5674ccd8e176SBarry Smith                                      "MatGetDiagonalBlock_MPIAIJ",
5675ccd8e176SBarry Smith                                      MatGetDiagonalBlock_MPIAIJ);CHKERRQ(ierr);
5676ccd8e176SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C",
5677ccd8e176SBarry Smith                                      "MatIsTranspose_MPIAIJ",
5678ccd8e176SBarry Smith                                      MatIsTranspose_MPIAIJ);CHKERRQ(ierr);
5679ccd8e176SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAIJSetPreallocation_C",
5680ccd8e176SBarry Smith                                      "MatMPIAIJSetPreallocation_MPIAIJ",
5681ccd8e176SBarry Smith                                      MatMPIAIJSetPreallocation_MPIAIJ);CHKERRQ(ierr);
5682ccd8e176SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAIJSetPreallocationCSR_C",
5683ccd8e176SBarry Smith                                      "MatMPIAIJSetPreallocationCSR_MPIAIJ",
5684ccd8e176SBarry Smith                                      MatMPIAIJSetPreallocationCSR_MPIAIJ);CHKERRQ(ierr);
5685ccd8e176SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatDiagonalScaleLocal_C",
5686ccd8e176SBarry Smith                                      "MatDiagonalScaleLocal_MPIAIJ",
5687ccd8e176SBarry Smith                                      MatDiagonalScaleLocal_MPIAIJ);CHKERRQ(ierr);
56885a11e1b2SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_mpiaijperm_C",
56895a11e1b2SBarry Smith                                      "MatConvert_MPIAIJ_MPIAIJPERM",
56905a11e1b2SBarry Smith                                       MatConvert_MPIAIJ_MPIAIJPERM);CHKERRQ(ierr);
56915a11e1b2SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_mpiaijcrl_C",
56925a11e1b2SBarry Smith                                      "MatConvert_MPIAIJ_MPIAIJCRL",
56935a11e1b2SBarry Smith                                       MatConvert_MPIAIJ_MPIAIJCRL);CHKERRQ(ierr);
5694471cc821SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_mpisbaij_C",
5695471cc821SHong Zhang                                      "MatConvert_MPIAIJ_MPISBAIJ",
5696471cc821SHong Zhang                                       MatConvert_MPIAIJ_MPISBAIJ);CHKERRQ(ierr);
5697fc4dec0aSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_mpidense_mpiaij_C",
5698fc4dec0aSBarry Smith                                      "MatMatMult_MPIDense_MPIAIJ",
5699fc4dec0aSBarry Smith                                       MatMatMult_MPIDense_MPIAIJ);CHKERRQ(ierr);
5700fc4dec0aSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_mpidense_mpiaij_C",
5701fc4dec0aSBarry Smith                                      "MatMatMultSymbolic_MPIDense_MPIAIJ",
5702fc4dec0aSBarry Smith                                      MatMatMultSymbolic_MPIDense_MPIAIJ);CHKERRQ(ierr);
5703fc4dec0aSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_mpidense_mpiaij_C",
5704fc4dec0aSBarry Smith                                      "MatMatMultNumeric_MPIDense_MPIAIJ",
5705fc4dec0aSBarry Smith                                       MatMatMultNumeric_MPIDense_MPIAIJ);CHKERRQ(ierr);
570617667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATMPIAIJ);CHKERRQ(ierr);
5707ccd8e176SBarry Smith   PetscFunctionReturn(0);
5708ccd8e176SBarry Smith }
5709ccd8e176SBarry Smith EXTERN_C_END
571081824310SBarry Smith 
571103bfb495SBarry Smith #undef __FUNCT__
571203bfb495SBarry Smith #define __FUNCT__ "MatCreateMPIAIJWithSplitArrays"
571358d36128SBarry Smith /*@
571403bfb495SBarry Smith      MatCreateMPIAIJWithSplitArrays - creates a MPI AIJ matrix using arrays that contain the "diagonal"
571503bfb495SBarry Smith          and "off-diagonal" part of the matrix in CSR format.
571603bfb495SBarry Smith 
571703bfb495SBarry Smith    Collective on MPI_Comm
571803bfb495SBarry Smith 
571903bfb495SBarry Smith    Input Parameters:
572003bfb495SBarry Smith +  comm - MPI communicator
572103bfb495SBarry Smith .  m - number of local rows (Cannot be PETSC_DECIDE)
572203bfb495SBarry Smith .  n - This value should be the same as the local size used in creating the
572303bfb495SBarry Smith        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
572403bfb495SBarry Smith        calculated if N is given) For square matrices n is almost always m.
572503bfb495SBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
572603bfb495SBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
572703bfb495SBarry Smith .   i - row indices for "diagonal" portion of matrix
572803bfb495SBarry Smith .   j - column indices
572903bfb495SBarry Smith .   a - matrix values
573003bfb495SBarry Smith .   oi - row indices for "off-diagonal" portion of matrix
573103bfb495SBarry Smith .   oj - column indices
573203bfb495SBarry Smith -   oa - matrix values
573303bfb495SBarry Smith 
573403bfb495SBarry Smith    Output Parameter:
573503bfb495SBarry Smith .   mat - the matrix
573603bfb495SBarry Smith 
573703bfb495SBarry Smith    Level: advanced
573803bfb495SBarry Smith 
573903bfb495SBarry Smith    Notes:
5740292fb18eSBarry Smith        The i, j, and a arrays ARE NOT copied by this routine into the internal format used by PETSc. The user
5741292fb18eSBarry Smith        must free the arrays once the matrix has been destroyed and not before.
574203bfb495SBarry Smith 
574303bfb495SBarry Smith        The i and j indices are 0 based
574403bfb495SBarry Smith 
574569b1f4b7SBarry Smith        See MatCreateAIJ() for the definition of "diagonal" and "off-diagonal" portion of the matrix
574603bfb495SBarry Smith 
57477b55108eSBarry Smith        This sets local rows and cannot be used to set off-processor values.
57487b55108eSBarry Smith 
57497b55108eSBarry Smith        You cannot later use MatSetValues() to change values in this matrix.
575003bfb495SBarry Smith 
575103bfb495SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
575203bfb495SBarry Smith 
575303bfb495SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
575469b1f4b7SBarry Smith           MPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithArrays()
575503bfb495SBarry Smith @*/
57567087cfbeSBarry Smith PetscErrorCode  MatCreateMPIAIJWithSplitArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,PetscInt i[],PetscInt j[],PetscScalar a[],
575703bfb495SBarry Smith                                                PetscInt oi[], PetscInt oj[],PetscScalar oa[],Mat *mat)
575803bfb495SBarry Smith {
575903bfb495SBarry Smith   PetscErrorCode ierr;
576003bfb495SBarry Smith   Mat_MPIAIJ     *maij;
576103bfb495SBarry Smith 
576203bfb495SBarry Smith   PetscFunctionBegin;
5763e32f2f54SBarry Smith   if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative");
5764ea345e14SBarry Smith   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
5765ea345e14SBarry Smith   if (oi[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"oi (row indices) must start with 0");
576603bfb495SBarry Smith   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
576703bfb495SBarry Smith   ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr);
576803bfb495SBarry Smith   ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr);
576903bfb495SBarry Smith   maij = (Mat_MPIAIJ*) (*mat)->data;
57708d7a6e47SBarry Smith   maij->donotstash     = PETSC_TRUE;
57718d7a6e47SBarry Smith   (*mat)->preallocated = PETSC_TRUE;
577203bfb495SBarry Smith 
577326283091SBarry Smith   ierr = PetscLayoutSetUp((*mat)->rmap);CHKERRQ(ierr);
577426283091SBarry Smith   ierr = PetscLayoutSetUp((*mat)->cmap);CHKERRQ(ierr);
577503bfb495SBarry Smith 
577603bfb495SBarry Smith   ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,n,i,j,a,&maij->A);CHKERRQ(ierr);
5777d0f46423SBarry Smith   ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,(*mat)->cmap->N,oi,oj,oa,&maij->B);CHKERRQ(ierr);
577803bfb495SBarry Smith 
57798d7a6e47SBarry Smith   ierr = MatAssemblyBegin(maij->A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
57808d7a6e47SBarry Smith   ierr = MatAssemblyEnd(maij->A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
57818d7a6e47SBarry Smith   ierr = MatAssemblyBegin(maij->B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
57828d7a6e47SBarry Smith   ierr = MatAssemblyEnd(maij->B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
57838d7a6e47SBarry Smith 
578403bfb495SBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
578503bfb495SBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
578603bfb495SBarry Smith   PetscFunctionReturn(0);
578703bfb495SBarry Smith }
578803bfb495SBarry Smith 
578981824310SBarry Smith /*
579081824310SBarry Smith     Special version for direct calls from Fortran
579181824310SBarry Smith */
5792b45d2f2cSJed Brown #include <petsc-private/fortranimpl.h>
57937087cfbeSBarry Smith 
579481824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
579581824310SBarry Smith #define matsetvaluesmpiaij_ MATSETVALUESMPIAIJ
579681824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
579781824310SBarry Smith #define matsetvaluesmpiaij_ matsetvaluesmpiaij
579881824310SBarry Smith #endif
579981824310SBarry Smith 
580081824310SBarry Smith /* Change these macros so can be used in void function */
580181824310SBarry Smith #undef CHKERRQ
5802e32f2f54SBarry Smith #define CHKERRQ(ierr) CHKERRABORT(PETSC_COMM_WORLD,ierr)
580381824310SBarry Smith #undef SETERRQ2
5804e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
58054994cf47SJed Brown #undef SETERRQ3
58064994cf47SJed Brown #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)
580781824310SBarry Smith #undef SETERRQ
5808e32f2f54SBarry Smith #define SETERRQ(c,ierr,b) CHKERRABORT(c,ierr)
580981824310SBarry Smith 
581081824310SBarry Smith EXTERN_C_BEGIN
581181824310SBarry Smith #undef __FUNCT__
581281824310SBarry Smith #define __FUNCT__ "matsetvaluesmpiaij_"
58131f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesmpiaij_(Mat *mmat,PetscInt *mm,const PetscInt im[],PetscInt *mn,const PetscInt in[],const PetscScalar v[],InsertMode *maddv,PetscErrorCode *_ierr)
581481824310SBarry Smith {
581581824310SBarry Smith   Mat             mat = *mmat;
581681824310SBarry Smith   PetscInt        m = *mm, n = *mn;
581781824310SBarry Smith   InsertMode      addv = *maddv;
581881824310SBarry Smith   Mat_MPIAIJ      *aij = (Mat_MPIAIJ*)mat->data;
581981824310SBarry Smith   PetscScalar     value;
582081824310SBarry Smith   PetscErrorCode  ierr;
5821899cda47SBarry Smith 
58224994cf47SJed Brown   MatCheckPreallocated(mat,1);
582381824310SBarry Smith   if (mat->insertmode == NOT_SET_VALUES) {
582481824310SBarry Smith     mat->insertmode = addv;
582581824310SBarry Smith   }
582681824310SBarry Smith #if defined(PETSC_USE_DEBUG)
5827f23aa3ddSBarry Smith   else if (mat->insertmode != addv) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
582881824310SBarry Smith #endif
582981824310SBarry Smith   {
5830d0f46423SBarry Smith   PetscInt        i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend;
5831d0f46423SBarry Smith   PetscInt        cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col;
5832ace3abfcSBarry Smith   PetscBool       roworiented = aij->roworiented;
583381824310SBarry Smith 
583481824310SBarry Smith   /* Some Variables required in the macro */
583581824310SBarry Smith   Mat             A = aij->A;
583681824310SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
583781824310SBarry Smith   PetscInt        *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j;
5838dd6ea824SBarry Smith   MatScalar       *aa = a->a;
5839ace3abfcSBarry Smith   PetscBool       ignorezeroentries = (((a->ignorezeroentries)&&(addv==ADD_VALUES))?PETSC_TRUE:PETSC_FALSE);
584081824310SBarry Smith   Mat             B = aij->B;
584181824310SBarry Smith   Mat_SeqAIJ      *b = (Mat_SeqAIJ*)B->data;
5842d0f46423SBarry Smith   PetscInt        *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap->n,am = aij->A->rmap->n;
5843dd6ea824SBarry Smith   MatScalar       *ba = b->a;
584481824310SBarry Smith 
584581824310SBarry Smith   PetscInt        *rp1,*rp2,ii,nrow1,nrow2,_i,rmax1,rmax2,N,low1,high1,low2,high2,t,lastcol1,lastcol2;
584681824310SBarry Smith   PetscInt        nonew = a->nonew;
5847dd6ea824SBarry Smith   MatScalar       *ap1,*ap2;
584881824310SBarry Smith 
584981824310SBarry Smith   PetscFunctionBegin;
585081824310SBarry Smith   for (i=0; i<m; i++) {
585181824310SBarry Smith     if (im[i] < 0) continue;
585281824310SBarry Smith #if defined(PETSC_USE_DEBUG)
5853e32f2f54SBarry 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);
585481824310SBarry Smith #endif
585581824310SBarry Smith     if (im[i] >= rstart && im[i] < rend) {
585681824310SBarry Smith       row      = im[i] - rstart;
585781824310SBarry Smith       lastcol1 = -1;
585881824310SBarry Smith       rp1      = aj + ai[row];
585981824310SBarry Smith       ap1      = aa + ai[row];
586081824310SBarry Smith       rmax1    = aimax[row];
586181824310SBarry Smith       nrow1    = ailen[row];
586281824310SBarry Smith       low1     = 0;
586381824310SBarry Smith       high1    = nrow1;
586481824310SBarry Smith       lastcol2 = -1;
586581824310SBarry Smith       rp2      = bj + bi[row];
586681824310SBarry Smith       ap2      = ba + bi[row];
586781824310SBarry Smith       rmax2    = bimax[row];
586881824310SBarry Smith       nrow2    = bilen[row];
586981824310SBarry Smith       low2     = 0;
587081824310SBarry Smith       high2    = nrow2;
587181824310SBarry Smith 
587281824310SBarry Smith       for (j=0; j<n; j++) {
587381824310SBarry Smith         if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
587481824310SBarry Smith         if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES)) continue;
587581824310SBarry Smith         if (in[j] >= cstart && in[j] < cend) {
587681824310SBarry Smith           col = in[j] - cstart;
587781824310SBarry Smith           MatSetValues_SeqAIJ_A_Private(row,col,value,addv);
587881824310SBarry Smith         } else if (in[j] < 0) continue;
587981824310SBarry Smith #if defined(PETSC_USE_DEBUG)
5880cb9801acSJed 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);
588181824310SBarry Smith #endif
588281824310SBarry Smith         else {
588381824310SBarry Smith           if (mat->was_assembled) {
588481824310SBarry Smith             if (!aij->colmap) {
5885ab9863d7SBarry Smith               ierr = MatCreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
588681824310SBarry Smith             }
588781824310SBarry Smith #if defined (PETSC_USE_CTABLE)
588881824310SBarry Smith             ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr);
588981824310SBarry Smith             col--;
589081824310SBarry Smith #else
589181824310SBarry Smith             col = aij->colmap[in[j]] - 1;
589281824310SBarry Smith #endif
589381824310SBarry Smith             if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
5894ab9863d7SBarry Smith               ierr = MatDisAssemble_MPIAIJ(mat);CHKERRQ(ierr);
589581824310SBarry Smith               col =  in[j];
589681824310SBarry Smith               /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
589781824310SBarry Smith               B = aij->B;
589881824310SBarry Smith               b = (Mat_SeqAIJ*)B->data;
589981824310SBarry Smith               bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j;
590081824310SBarry Smith               rp2      = bj + bi[row];
590181824310SBarry Smith               ap2      = ba + bi[row];
590281824310SBarry Smith               rmax2    = bimax[row];
590381824310SBarry Smith               nrow2    = bilen[row];
590481824310SBarry Smith               low2     = 0;
590581824310SBarry Smith               high2    = nrow2;
5906d0f46423SBarry Smith               bm       = aij->B->rmap->n;
590781824310SBarry Smith               ba = b->a;
590881824310SBarry Smith             }
590981824310SBarry Smith           } else col = in[j];
591081824310SBarry Smith           MatSetValues_SeqAIJ_B_Private(row,col,value,addv);
591181824310SBarry Smith         }
591281824310SBarry Smith       }
591381824310SBarry Smith     } else {
591481824310SBarry Smith       if (!aij->donotstash) {
591581824310SBarry Smith         if (roworiented) {
5916ace3abfcSBarry Smith           ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr);
591781824310SBarry Smith         } else {
5918ace3abfcSBarry Smith           ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr);
591981824310SBarry Smith         }
592081824310SBarry Smith       }
592181824310SBarry Smith     }
592281824310SBarry Smith   }}
592381824310SBarry Smith   PetscFunctionReturnVoid();
592481824310SBarry Smith }
592581824310SBarry Smith EXTERN_C_END
592603bfb495SBarry Smith 
5927