xref: /petsc/src/mat/utils/axpy.c (revision 32dfb669cb18c8e13334f2d48c3fd09c1fb43e18)
16f79c3a4SBarry Smith 
2e090d566SSatish Balay #include "src/mat/matimpl.h"  /*I   "petscmat.h"  I*/
36f79c3a4SBarry Smith 
44a2ae208SSatish Balay #undef __FUNCT__
54a2ae208SSatish Balay #define __FUNCT__ "MatAXPY"
606be10caSBarry Smith /*@
721c89e3eSBarry Smith    MatAXPY - Computes Y = a*X + Y.
86f79c3a4SBarry Smith 
9fee21e36SBarry Smith    Collective on Mat
10fee21e36SBarry Smith 
1198a79cdbSBarry Smith    Input Parameters:
12607cd303SBarry Smith +  a - the scalar multiplier
13607cd303SBarry Smith .  X - the first matrix
14607cd303SBarry Smith .  Y - the second matrix
151a5ee19eSHong Zhang -  str - either SAME_NONZERO_PATTERN, DIFFERENT_NONZERO_PATTERN or SUBSET_NONZERO_PATTERN
1698a79cdbSBarry Smith 
17e182c471SBarry Smith    Contributed by: Matthew Knepley
18d4bb536fSBarry Smith 
192860a424SLois Curfman McInnes    Notes:
201a5ee19eSHong Zhang      Will only be efficient if one has the SAME_NONZERO_PATTERN or SUBSET_NONZERO_PATTERN
212860a424SLois Curfman McInnes 
222860a424SLois Curfman McInnes    Level: intermediate
232860a424SLois Curfman McInnes 
249cf4f1e8SLois Curfman McInnes .keywords: matrix, add
25d4bb536fSBarry Smith 
262860a424SLois Curfman McInnes .seealso: MatAYPX()
2706be10caSBarry Smith  @*/
28dfbe8321SBarry Smith PetscErrorCode MatAXPY(const PetscScalar *a,Mat X,Mat Y,MatStructure str)
296f79c3a4SBarry Smith {
306849ba73SBarry Smith   PetscErrorCode ierr;
31c1ac3661SBarry Smith   PetscInt       m1,m2,n1,n2;
326f79c3a4SBarry Smith 
333a40ed3dSBarry Smith   PetscFunctionBegin;
344482741eSBarry Smith   PetscValidScalarPointer(a,1);
354482741eSBarry Smith   PetscValidHeaderSpecific(X,MAT_COOKIE,2);
364482741eSBarry Smith   PetscValidHeaderSpecific(Y,MAT_COOKIE,3);
3790f02eecSBarry Smith 
38273d9f13SBarry Smith   ierr = MatGetSize(X,&m1,&n1);CHKERRQ(ierr);
39273d9f13SBarry Smith   ierr = MatGetSize(Y,&m2,&n2);CHKERRQ(ierr);
4077431f27SBarry Smith   if (m1 != m2 || n1 != n2) SETERRQ4(PETSC_ERR_ARG_SIZ,"Non conforming matrix add: %D %D %D %D",m1,m2,n1,n2);
411987afe7SBarry Smith 
42f830108cSBarry Smith   if (X->ops->axpy) {
43607cd303SBarry Smith     ierr = (*X->ops->axpy)(a,X,Y,str);CHKERRQ(ierr);
44d4bb536fSBarry Smith   } else {
45607cd303SBarry Smith     ierr = MatAXPY_Basic(a,X,Y,str);CHKERRQ(ierr);
46607cd303SBarry Smith   }
47607cd303SBarry Smith   PetscFunctionReturn(0);
48607cd303SBarry Smith }
49607cd303SBarry Smith 
50607cd303SBarry Smith 
51607cd303SBarry Smith #undef __FUNCT__
52607cd303SBarry Smith #define __FUNCT__ "MatAXPY_Basic"
53dfbe8321SBarry Smith PetscErrorCode MatAXPY_Basic(const PetscScalar *a,Mat X,Mat Y,MatStructure str)
54607cd303SBarry Smith {
5538baddfdSBarry Smith   PetscInt          i,start,end,j,ncols,m,n;
566849ba73SBarry Smith   PetscErrorCode    ierr;
5738baddfdSBarry Smith   const PetscInt    *row;
58b3cc6726SBarry Smith   PetscScalar       *val;
59b3cc6726SBarry Smith   const PetscScalar *vals;
60607cd303SBarry Smith 
61607cd303SBarry Smith   PetscFunctionBegin;
628dadbd76SSatish Balay   ierr = MatGetSize(X,&m,&n);CHKERRQ(ierr);
6390f02eecSBarry Smith   ierr = MatGetOwnershipRange(X,&start,&end);CHKERRQ(ierr);
64d4bb536fSBarry Smith   if (*a == 1.0) {
65d4bb536fSBarry Smith     for (i = start; i < end; i++) {
66d4bb536fSBarry Smith       ierr = MatGetRow(X,i,&ncols,&row,&vals);CHKERRQ(ierr);
67d4bb536fSBarry Smith       ierr = MatSetValues(Y,1,&i,ncols,row,vals,ADD_VALUES);CHKERRQ(ierr);
68d4bb536fSBarry Smith       ierr = MatRestoreRow(X,i,&ncols,&row,&vals);CHKERRQ(ierr);
69d4bb536fSBarry Smith     }
70d4bb536fSBarry Smith   } else {
71b3cc6726SBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscScalar),&val);CHKERRQ(ierr);
7206be10caSBarry Smith     for (i=start; i<end; i++) {
73b3cc6726SBarry Smith       ierr = MatGetRow(X,i,&ncols,&row,&vals);CHKERRQ(ierr);
7406be10caSBarry Smith       for (j=0; j<ncols; j++) {
75b3cc6726SBarry Smith 	val[j] = (*a)*vals[j];
766f79c3a4SBarry Smith       }
77b3cc6726SBarry Smith       ierr = MatSetValues(Y,1,&i,ncols,row,val,ADD_VALUES);CHKERRQ(ierr);
78b3cc6726SBarry Smith       ierr = MatRestoreRow(X,i,&ncols,&row,&vals);CHKERRQ(ierr);
796f79c3a4SBarry Smith     }
80b3cc6726SBarry Smith     ierr = PetscFree(val);CHKERRQ(ierr);
81d4bb536fSBarry Smith   }
826d4a8577SBarry Smith   ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
836d4a8577SBarry Smith   ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
843a40ed3dSBarry Smith   PetscFunctionReturn(0);
856f79c3a4SBarry Smith }
86052efed2SBarry Smith 
874a2ae208SSatish Balay #undef __FUNCT__
884a2ae208SSatish Balay #define __FUNCT__ "MatShift"
89052efed2SBarry Smith /*@
9087828ca2SBarry Smith    MatShift - Computes Y =  Y + a I, where a is a PetscScalar and I is the identity matrix.
91052efed2SBarry Smith 
92fee21e36SBarry Smith    Collective on Mat
93fee21e36SBarry Smith 
9498a79cdbSBarry Smith    Input Parameters:
9598a79cdbSBarry Smith +  Y - the matrices
9687828ca2SBarry Smith -  a - the PetscScalar
9798a79cdbSBarry Smith 
982860a424SLois Curfman McInnes    Level: intermediate
992860a424SLois Curfman McInnes 
100052efed2SBarry Smith .keywords: matrix, add, shift
1016b9ee512SLois Curfman McInnes 
102f56f2b3fSBarry Smith .seealso: MatDiagonalSet()
103052efed2SBarry Smith  @*/
104dfbe8321SBarry Smith PetscErrorCode MatShift(const PetscScalar *a,Mat Y)
105052efed2SBarry Smith {
1066849ba73SBarry Smith   PetscErrorCode ierr;
10738baddfdSBarry Smith   PetscInt       i,start,end;
108052efed2SBarry Smith 
1093a40ed3dSBarry Smith   PetscFunctionBegin;
1104482741eSBarry Smith   PetscValidScalarPointer(a,1);
1114482741eSBarry Smith   PetscValidHeaderSpecific(Y,MAT_COOKIE,2);
112*32dfb669SBarry Smith   MatPreallocated(Y);
113*32dfb669SBarry Smith   if (!Y->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
114*32dfb669SBarry Smith   if (Y->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
115f830108cSBarry Smith   if (Y->ops->shift) {
116f830108cSBarry Smith     ierr = (*Y->ops->shift)(a,Y);CHKERRQ(ierr);
11762d58ce1SBarry Smith   } else {
118d4bb536fSBarry Smith     ierr = MatGetOwnershipRange(Y,&start,&end);CHKERRQ(ierr);
119052efed2SBarry Smith     for (i=start; i<end; i++) {
120052efed2SBarry Smith       ierr = MatSetValues(Y,1,&i,1,&i,a,ADD_VALUES);CHKERRQ(ierr);
121052efed2SBarry Smith     }
1226d4a8577SBarry Smith     ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1236d4a8577SBarry Smith     ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
124052efed2SBarry Smith   }
1253a40ed3dSBarry Smith   PetscFunctionReturn(0);
126052efed2SBarry Smith }
1276d84be18SBarry Smith 
1284a2ae208SSatish Balay #undef __FUNCT__
1294a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalSet"
1306d84be18SBarry Smith /*@
131f56f2b3fSBarry Smith    MatDiagonalSet - Computes Y = Y + D, where D is a diagonal matrix
132f56f2b3fSBarry Smith    that is represented as a vector. Or Y[i,i] = D[i] if InsertMode is
133f56f2b3fSBarry Smith    INSERT_VALUES.
1346d84be18SBarry Smith 
1356d84be18SBarry Smith    Input Parameters:
13698a79cdbSBarry Smith +  Y - the input matrix
137f56f2b3fSBarry Smith .  D - the diagonal matrix, represented as a vector
138f56f2b3fSBarry Smith -  i - INSERT_VALUES or ADD_VALUES
1396d84be18SBarry Smith 
140fee21e36SBarry Smith    Collective on Mat and Vec
141fee21e36SBarry Smith 
1422860a424SLois Curfman McInnes    Level: intermediate
1432860a424SLois Curfman McInnes 
1446b9ee512SLois Curfman McInnes .keywords: matrix, add, shift, diagonal
1456b9ee512SLois Curfman McInnes 
1466b9ee512SLois Curfman McInnes .seealso: MatShift()
1476d84be18SBarry Smith @*/
148dfbe8321SBarry Smith PetscErrorCode MatDiagonalSet(Mat Y,Vec D,InsertMode is)
1496d84be18SBarry Smith {
1506849ba73SBarry Smith   PetscErrorCode ierr;
15138baddfdSBarry Smith   PetscInt       i,start,end;
1526d84be18SBarry Smith 
1533a40ed3dSBarry Smith   PetscFunctionBegin;
1544482741eSBarry Smith   PetscValidHeaderSpecific(Y,MAT_COOKIE,1);
1554482741eSBarry Smith   PetscValidHeaderSpecific(D,VEC_COOKIE,2);
156f56f2b3fSBarry Smith   if (Y->ops->diagonalset) {
157f56f2b3fSBarry Smith     ierr = (*Y->ops->diagonalset)(Y,D,is);CHKERRQ(ierr);
15894d884c6SBarry Smith   } else {
15938baddfdSBarry Smith     PetscInt    vstart,vend;
16087828ca2SBarry Smith     PetscScalar *v;
1616d84be18SBarry Smith     ierr = VecGetOwnershipRange(D,&vstart,&vend);CHKERRQ(ierr);
1626d84be18SBarry Smith     ierr = MatGetOwnershipRange(Y,&start,&end);CHKERRQ(ierr);
163d4bb536fSBarry Smith     if (vstart != start || vend != end) {
16477431f27SBarry Smith       SETERRQ4(PETSC_ERR_ARG_SIZ,"Vector ownership range not compatible with matrix: %D %D vec %D %D mat",vstart,vend,start,end);
165d4bb536fSBarry Smith     }
1666d84be18SBarry Smith     ierr = VecGetArray(D,&v);CHKERRQ(ierr);
1676d84be18SBarry Smith     for (i=start; i<end; i++) {
168f56f2b3fSBarry Smith       ierr = MatSetValues(Y,1,&i,1,&i,v+i-start,is);CHKERRQ(ierr);
1696d84be18SBarry Smith     }
1702e8a6d31SBarry Smith     ierr = VecRestoreArray(D,&v);CHKERRQ(ierr);
1716d4a8577SBarry Smith     ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1726d4a8577SBarry Smith     ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1736d84be18SBarry Smith   }
1743a40ed3dSBarry Smith   PetscFunctionReturn(0);
1756d84be18SBarry Smith }
176d4bb536fSBarry Smith 
1774a2ae208SSatish Balay #undef __FUNCT__
1784a2ae208SSatish Balay #define __FUNCT__ "MatAYPX"
179d4bb536fSBarry Smith /*@
180d4bb536fSBarry Smith    MatAYPX - Computes Y = X + a*Y.
181d4bb536fSBarry Smith 
182fee21e36SBarry Smith    Collective on Mat
183fee21e36SBarry Smith 
18498a79cdbSBarry Smith    Input Parameters:
18598a79cdbSBarry Smith +  X,Y - the matrices
18687828ca2SBarry Smith -  a - the PetscScalar multiplier
18798a79cdbSBarry Smith 
188e182c471SBarry Smith    Contributed by: Matthew Knepley
189d4bb536fSBarry Smith 
1902860a424SLois Curfman McInnes    Notes:
1912860a424SLois Curfman McInnes    This routine currently uses the MatAXPY() implementation.
1922860a424SLois Curfman McInnes 
193607cd303SBarry Smith    This is slow, if you need it fast send email to petsc-maint@mcs.anl.gov
194607cd303SBarry Smith 
1952860a424SLois Curfman McInnes    Level: intermediate
1962860a424SLois Curfman McInnes 
197d4bb536fSBarry Smith .keywords: matrix, add
198d4bb536fSBarry Smith 
1992860a424SLois Curfman McInnes .seealso: MatAXPY()
200d4bb536fSBarry Smith  @*/
201dfbe8321SBarry Smith PetscErrorCode MatAYPX(const PetscScalar *a,Mat X,Mat Y)
202d4bb536fSBarry Smith {
20387828ca2SBarry Smith   PetscScalar    one = 1.0;
2046849ba73SBarry Smith   PetscErrorCode ierr;
20538baddfdSBarry Smith   PetscInt       mX,mY,nX,nY;
206d4bb536fSBarry Smith 
2073a40ed3dSBarry Smith   PetscFunctionBegin;
2084482741eSBarry Smith   PetscValidScalarPointer(a,1);
2094482741eSBarry Smith   PetscValidHeaderSpecific(X,MAT_COOKIE,2);
2104482741eSBarry Smith   PetscValidHeaderSpecific(Y,MAT_COOKIE,3);
211d4bb536fSBarry Smith 
212329f5518SBarry Smith   ierr = MatGetSize(X,&mX,&nX);CHKERRQ(ierr);
213329f5518SBarry Smith   ierr = MatGetSize(X,&mY,&nY);CHKERRQ(ierr);
21477431f27SBarry Smith   if (mX != mY || nX != nY) SETERRQ4(PETSC_ERR_ARG_SIZ,"Non conforming matrices: %D %D first %D %D second",mX,mY,nX,nY);
215d4bb536fSBarry Smith 
216d4bb536fSBarry Smith   ierr = MatScale(a,Y);CHKERRQ(ierr);
217607cd303SBarry Smith   ierr = MatAXPY(&one,X,Y,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr);
2183a40ed3dSBarry Smith   PetscFunctionReturn(0);
219d4bb536fSBarry Smith }
220b0a32e0cSBarry Smith 
2214a2ae208SSatish Balay #undef __FUNCT__
2224a2ae208SSatish Balay #define __FUNCT__ "MatComputeExplicitOperator"
223b0a32e0cSBarry Smith /*@
224b0a32e0cSBarry Smith     MatComputeExplicitOperator - Computes the explicit matrix
225b0a32e0cSBarry Smith 
226b0a32e0cSBarry Smith     Collective on Mat
227b0a32e0cSBarry Smith 
228b0a32e0cSBarry Smith     Input Parameter:
229b0a32e0cSBarry Smith .   inmat - the matrix
230b0a32e0cSBarry Smith 
231b0a32e0cSBarry Smith     Output Parameter:
232b0a32e0cSBarry Smith .   mat - the explict preconditioned operator
233b0a32e0cSBarry Smith 
234b0a32e0cSBarry Smith     Notes:
235b0a32e0cSBarry Smith     This computation is done by applying the operators to columns of the
236b0a32e0cSBarry Smith     identity matrix.
237b0a32e0cSBarry Smith 
238b0a32e0cSBarry Smith     Currently, this routine uses a dense matrix format when 1 processor
239b0a32e0cSBarry Smith     is used and a sparse format otherwise.  This routine is costly in general,
240b0a32e0cSBarry Smith     and is recommended for use only with relatively small systems.
241b0a32e0cSBarry Smith 
242b0a32e0cSBarry Smith     Level: advanced
243b0a32e0cSBarry Smith 
244b0a32e0cSBarry Smith .keywords: Mat, compute, explicit, operator
245b0a32e0cSBarry Smith 
246b0a32e0cSBarry Smith @*/
247dfbe8321SBarry Smith PetscErrorCode MatComputeExplicitOperator(Mat inmat,Mat *mat)
248b0a32e0cSBarry Smith {
249b0a32e0cSBarry Smith   Vec            in,out;
250dfbe8321SBarry Smith   PetscErrorCode ierr;
25138baddfdSBarry Smith   PetscInt       i,M,m,*rows,start,end;
252b0a32e0cSBarry Smith   MPI_Comm       comm;
25387828ca2SBarry Smith   PetscScalar    *array,zero = 0.0,one = 1.0;
25438baddfdSBarry Smith   PetscMPIInt    size;
255b0a32e0cSBarry Smith 
256b0a32e0cSBarry Smith   PetscFunctionBegin;
2574482741eSBarry Smith   PetscValidHeaderSpecific(inmat,MAT_COOKIE,1);
2584482741eSBarry Smith   PetscValidPointer(mat,2);
259b0a32e0cSBarry Smith 
260b0a32e0cSBarry Smith   comm = inmat->comm;
261b0a32e0cSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
262b0a32e0cSBarry Smith 
263b22afee1SSatish Balay   ierr = MatGetLocalSize(inmat,&m,0);CHKERRQ(ierr);
264b22afee1SSatish Balay   ierr = MatGetSize(inmat,&M,0);CHKERRQ(ierr);
265b0a32e0cSBarry Smith   ierr = VecCreateMPI(comm,m,M,&in);CHKERRQ(ierr);
266b0a32e0cSBarry Smith   ierr = VecDuplicate(in,&out);CHKERRQ(ierr);
267b0a32e0cSBarry Smith   ierr = VecGetOwnershipRange(in,&start,&end);CHKERRQ(ierr);
26838baddfdSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&rows);CHKERRQ(ierr);
269b0a32e0cSBarry Smith   for (i=0; i<m; i++) {rows[i] = start + i;}
270b0a32e0cSBarry Smith 
271be5d1d56SKris Buschelman   ierr = MatCreate(comm,m,m,M,M,mat);CHKERRQ(ierr);
272b0a32e0cSBarry Smith   if (size == 1) {
273be5d1d56SKris Buschelman     ierr = MatSetType(*mat,MATSEQDENSE);CHKERRQ(ierr);
274b24ad042SBarry Smith #if !defined(PETSC_USE_64BIT_INT)
275be5d1d56SKris Buschelman     ierr = MatSeqDenseSetPreallocation(*mat,PETSC_NULL);CHKERRQ(ierr);
276b24ad042SBarry Smith #endif
277b0a32e0cSBarry Smith   } else {
278be5d1d56SKris Buschelman     ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr);
279be5d1d56SKris Buschelman     ierr = MatMPIAIJSetPreallocation(*mat,0,PETSC_NULL,0,PETSC_NULL);CHKERRQ(ierr);
280b0a32e0cSBarry Smith   }
281b0a32e0cSBarry Smith 
282b0a32e0cSBarry Smith   for (i=0; i<M; i++) {
283b0a32e0cSBarry Smith 
284b0a32e0cSBarry Smith     ierr = VecSet(&zero,in);CHKERRQ(ierr);
285b0a32e0cSBarry Smith     ierr = VecSetValues(in,1,&i,&one,INSERT_VALUES);CHKERRQ(ierr);
286b0a32e0cSBarry Smith     ierr = VecAssemblyBegin(in);CHKERRQ(ierr);
287b0a32e0cSBarry Smith     ierr = VecAssemblyEnd(in);CHKERRQ(ierr);
288b0a32e0cSBarry Smith 
289b0a32e0cSBarry Smith     ierr = MatMult(inmat,in,out);CHKERRQ(ierr);
290b0a32e0cSBarry Smith 
291b0a32e0cSBarry Smith     ierr = VecGetArray(out,&array);CHKERRQ(ierr);
292b0a32e0cSBarry Smith     ierr = MatSetValues(*mat,m,rows,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
293b0a32e0cSBarry Smith     ierr = VecRestoreArray(out,&array);CHKERRQ(ierr);
294b0a32e0cSBarry Smith 
295b0a32e0cSBarry Smith   }
296b0a32e0cSBarry Smith   ierr = PetscFree(rows);CHKERRQ(ierr);
297b0a32e0cSBarry Smith   ierr = VecDestroy(out);CHKERRQ(ierr);
298b0a32e0cSBarry Smith   ierr = VecDestroy(in);CHKERRQ(ierr);
299b0a32e0cSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
300b0a32e0cSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
301b0a32e0cSBarry Smith   PetscFunctionReturn(0);
302b0a32e0cSBarry Smith }
303b0a32e0cSBarry Smith 
30424f910e3SHong Zhang /* Get the map xtoy which is used by MatAXPY() in the case of SUBSET_NONZERO_PATTERN */
30524f910e3SHong Zhang #undef __FUNCT__
30624f910e3SHong Zhang #define __FUNCT__ "MatAXPYGetxtoy_Private"
30738baddfdSBarry Smith PetscErrorCode MatAXPYGetxtoy_Private(PetscInt m,PetscInt *xi,PetscInt *xj,PetscInt *xgarray, PetscInt *yi,PetscInt *yj,PetscInt *ygarray, PetscInt **xtoy)
30824f910e3SHong Zhang {
3096849ba73SBarry Smith   PetscErrorCode ierr;
31038baddfdSBarry Smith   PetscInt       row,i,nz,xcol,ycol,jx,jy,*x2y;
31124f910e3SHong Zhang 
31224f910e3SHong Zhang   PetscFunctionBegin;
31338baddfdSBarry Smith   ierr = PetscMalloc(xi[m]*sizeof(PetscInt),&x2y);CHKERRQ(ierr);
31424f910e3SHong Zhang   i = 0;
31524f910e3SHong Zhang   for (row=0; row<m; row++){
31624f910e3SHong Zhang     nz = xi[1] - xi[0];
31724f910e3SHong Zhang     jy = 0;
31824f910e3SHong Zhang     for (jx=0; jx<nz; jx++,jy++){
31924f910e3SHong Zhang       if (xgarray && ygarray){
32024f910e3SHong Zhang         xcol = xgarray[xj[*xi + jx]];
32124f910e3SHong Zhang         ycol = ygarray[yj[*yi + jy]];
32224f910e3SHong Zhang       } else {
32324f910e3SHong Zhang         xcol = xj[*xi + jx];
32424f910e3SHong Zhang         ycol = yj[*yi + jy];  /* col index for y */
32524f910e3SHong Zhang       }
32624f910e3SHong Zhang       while ( ycol < xcol ) {
32724f910e3SHong Zhang         jy++;
32824f910e3SHong Zhang         if (ygarray){
32924f910e3SHong Zhang           ycol = ygarray[yj[*yi + jy]];
33024f910e3SHong Zhang         } else {
33124f910e3SHong Zhang           ycol = yj[*yi + jy];
33224f910e3SHong Zhang         }
33324f910e3SHong Zhang       }
33477431f27SBarry Smith       if (xcol != ycol) SETERRQ2(PETSC_ERR_ARG_WRONG,"X matrix entry (%D,%D) is not in Y matrix",row,ycol);
33524f910e3SHong Zhang       x2y[i++] = *yi + jy;
33624f910e3SHong Zhang     }
33724f910e3SHong Zhang     xi++; yi++;
33824f910e3SHong Zhang   }
33924f910e3SHong Zhang   *xtoy = x2y;
34024f910e3SHong Zhang   PetscFunctionReturn(0);
34124f910e3SHong Zhang }
342