xref: /petsc/src/mat/utils/axpy.c (revision 38baddfdda9fcd85a080f141519fb97fb01bc0a4)
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);
40*38baddfdSBarry Smith   if (m1 != m2 || n1 != n2) SETERRQ4(PETSC_ERR_ARG_SIZ,"Non conforming matrix add: %d %d %d %d",(int)m1,(int)m2,(int)n1,(int)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 {
55*38baddfdSBarry Smith   PetscInt          i,start,end,j,ncols,m,n;
566849ba73SBarry Smith   PetscErrorCode    ierr;
57*38baddfdSBarry 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;
107*38baddfdSBarry Smith   PetscInt       i,start,end;
108052efed2SBarry Smith 
1093a40ed3dSBarry Smith   PetscFunctionBegin;
1104482741eSBarry Smith   PetscValidScalarPointer(a,1);
1114482741eSBarry Smith   PetscValidHeaderSpecific(Y,MAT_COOKIE,2);
112f830108cSBarry Smith   if (Y->ops->shift) {
113f830108cSBarry Smith     ierr = (*Y->ops->shift)(a,Y);CHKERRQ(ierr);
11462d58ce1SBarry Smith   } else {
115d4bb536fSBarry Smith     ierr = MatGetOwnershipRange(Y,&start,&end);CHKERRQ(ierr);
116052efed2SBarry Smith     for (i=start; i<end; i++) {
117052efed2SBarry Smith       ierr = MatSetValues(Y,1,&i,1,&i,a,ADD_VALUES);CHKERRQ(ierr);
118052efed2SBarry Smith     }
1196d4a8577SBarry Smith     ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1206d4a8577SBarry Smith     ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
121052efed2SBarry Smith   }
1223a40ed3dSBarry Smith   PetscFunctionReturn(0);
123052efed2SBarry Smith }
1246d84be18SBarry Smith 
1254a2ae208SSatish Balay #undef __FUNCT__
1264a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalSet"
1276d84be18SBarry Smith /*@
128f56f2b3fSBarry Smith    MatDiagonalSet - Computes Y = Y + D, where D is a diagonal matrix
129f56f2b3fSBarry Smith    that is represented as a vector. Or Y[i,i] = D[i] if InsertMode is
130f56f2b3fSBarry Smith    INSERT_VALUES.
1316d84be18SBarry Smith 
1326d84be18SBarry Smith    Input Parameters:
13398a79cdbSBarry Smith +  Y - the input matrix
134f56f2b3fSBarry Smith .  D - the diagonal matrix, represented as a vector
135f56f2b3fSBarry Smith -  i - INSERT_VALUES or ADD_VALUES
1366d84be18SBarry Smith 
137fee21e36SBarry Smith    Collective on Mat and Vec
138fee21e36SBarry Smith 
1392860a424SLois Curfman McInnes    Level: intermediate
1402860a424SLois Curfman McInnes 
1416b9ee512SLois Curfman McInnes .keywords: matrix, add, shift, diagonal
1426b9ee512SLois Curfman McInnes 
1436b9ee512SLois Curfman McInnes .seealso: MatShift()
1446d84be18SBarry Smith @*/
145dfbe8321SBarry Smith PetscErrorCode MatDiagonalSet(Mat Y,Vec D,InsertMode is)
1466d84be18SBarry Smith {
1476849ba73SBarry Smith   PetscErrorCode ierr;
148*38baddfdSBarry Smith   PetscInt       i,start,end;
1496d84be18SBarry Smith 
1503a40ed3dSBarry Smith   PetscFunctionBegin;
1514482741eSBarry Smith   PetscValidHeaderSpecific(Y,MAT_COOKIE,1);
1524482741eSBarry Smith   PetscValidHeaderSpecific(D,VEC_COOKIE,2);
153f56f2b3fSBarry Smith   if (Y->ops->diagonalset) {
154f56f2b3fSBarry Smith     ierr = (*Y->ops->diagonalset)(Y,D,is);CHKERRQ(ierr);
15594d884c6SBarry Smith   } else {
156*38baddfdSBarry Smith     PetscInt    vstart,vend;
15787828ca2SBarry Smith     PetscScalar *v;
1586d84be18SBarry Smith     ierr = VecGetOwnershipRange(D,&vstart,&vend);CHKERRQ(ierr);
1596d84be18SBarry Smith     ierr = MatGetOwnershipRange(Y,&start,&end);CHKERRQ(ierr);
160d4bb536fSBarry Smith     if (vstart != start || vend != end) {
161*38baddfdSBarry Smith       SETERRQ4(PETSC_ERR_ARG_SIZ,"Vector ownership range not compatible with matrix: %d %d vec %d %d mat",(int)vstart,(int)vend,(int)start,(int)end);
162d4bb536fSBarry Smith     }
1636d84be18SBarry Smith     ierr = VecGetArray(D,&v);CHKERRQ(ierr);
1646d84be18SBarry Smith     for (i=start; i<end; i++) {
165f56f2b3fSBarry Smith       ierr = MatSetValues(Y,1,&i,1,&i,v+i-start,is);CHKERRQ(ierr);
1666d84be18SBarry Smith     }
1672e8a6d31SBarry Smith     ierr = VecRestoreArray(D,&v);CHKERRQ(ierr);
1686d4a8577SBarry Smith     ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1696d4a8577SBarry Smith     ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1706d84be18SBarry Smith   }
1713a40ed3dSBarry Smith   PetscFunctionReturn(0);
1726d84be18SBarry Smith }
173d4bb536fSBarry Smith 
1744a2ae208SSatish Balay #undef __FUNCT__
1754a2ae208SSatish Balay #define __FUNCT__ "MatAYPX"
176d4bb536fSBarry Smith /*@
177d4bb536fSBarry Smith    MatAYPX - Computes Y = X + a*Y.
178d4bb536fSBarry Smith 
179fee21e36SBarry Smith    Collective on Mat
180fee21e36SBarry Smith 
18198a79cdbSBarry Smith    Input Parameters:
18298a79cdbSBarry Smith +  X,Y - the matrices
18387828ca2SBarry Smith -  a - the PetscScalar multiplier
18498a79cdbSBarry Smith 
185e182c471SBarry Smith    Contributed by: Matthew Knepley
186d4bb536fSBarry Smith 
1872860a424SLois Curfman McInnes    Notes:
1882860a424SLois Curfman McInnes    This routine currently uses the MatAXPY() implementation.
1892860a424SLois Curfman McInnes 
190607cd303SBarry Smith    This is slow, if you need it fast send email to petsc-maint@mcs.anl.gov
191607cd303SBarry Smith 
1922860a424SLois Curfman McInnes    Level: intermediate
1932860a424SLois Curfman McInnes 
194d4bb536fSBarry Smith .keywords: matrix, add
195d4bb536fSBarry Smith 
1962860a424SLois Curfman McInnes .seealso: MatAXPY()
197d4bb536fSBarry Smith  @*/
198dfbe8321SBarry Smith PetscErrorCode MatAYPX(const PetscScalar *a,Mat X,Mat Y)
199d4bb536fSBarry Smith {
20087828ca2SBarry Smith   PetscScalar    one = 1.0;
2016849ba73SBarry Smith   PetscErrorCode ierr;
202*38baddfdSBarry Smith   PetscInt       mX,mY,nX,nY;
203d4bb536fSBarry Smith 
2043a40ed3dSBarry Smith   PetscFunctionBegin;
2054482741eSBarry Smith   PetscValidScalarPointer(a,1);
2064482741eSBarry Smith   PetscValidHeaderSpecific(X,MAT_COOKIE,2);
2074482741eSBarry Smith   PetscValidHeaderSpecific(Y,MAT_COOKIE,3);
208d4bb536fSBarry Smith 
209329f5518SBarry Smith   ierr = MatGetSize(X,&mX,&nX);CHKERRQ(ierr);
210329f5518SBarry Smith   ierr = MatGetSize(X,&mY,&nY);CHKERRQ(ierr);
211*38baddfdSBarry Smith   if (mX != mY || nX != nY) SETERRQ4(PETSC_ERR_ARG_SIZ,"Non conforming matrices: %d %d first %d %d second",(int)mX,(int)mY,(int)nX,(int)nY);
212d4bb536fSBarry Smith 
213d4bb536fSBarry Smith   ierr = MatScale(a,Y);CHKERRQ(ierr);
214607cd303SBarry Smith   ierr = MatAXPY(&one,X,Y,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr);
2153a40ed3dSBarry Smith   PetscFunctionReturn(0);
216d4bb536fSBarry Smith }
217b0a32e0cSBarry Smith 
2184a2ae208SSatish Balay #undef __FUNCT__
2194a2ae208SSatish Balay #define __FUNCT__ "MatComputeExplicitOperator"
220b0a32e0cSBarry Smith /*@
221b0a32e0cSBarry Smith     MatComputeExplicitOperator - Computes the explicit matrix
222b0a32e0cSBarry Smith 
223b0a32e0cSBarry Smith     Collective on Mat
224b0a32e0cSBarry Smith 
225b0a32e0cSBarry Smith     Input Parameter:
226b0a32e0cSBarry Smith .   inmat - the matrix
227b0a32e0cSBarry Smith 
228b0a32e0cSBarry Smith     Output Parameter:
229b0a32e0cSBarry Smith .   mat - the explict preconditioned operator
230b0a32e0cSBarry Smith 
231b0a32e0cSBarry Smith     Notes:
232b0a32e0cSBarry Smith     This computation is done by applying the operators to columns of the
233b0a32e0cSBarry Smith     identity matrix.
234b0a32e0cSBarry Smith 
235b0a32e0cSBarry Smith     Currently, this routine uses a dense matrix format when 1 processor
236b0a32e0cSBarry Smith     is used and a sparse format otherwise.  This routine is costly in general,
237b0a32e0cSBarry Smith     and is recommended for use only with relatively small systems.
238b0a32e0cSBarry Smith 
239b0a32e0cSBarry Smith     Level: advanced
240b0a32e0cSBarry Smith 
241b0a32e0cSBarry Smith .keywords: Mat, compute, explicit, operator
242b0a32e0cSBarry Smith 
243b0a32e0cSBarry Smith @*/
244dfbe8321SBarry Smith PetscErrorCode MatComputeExplicitOperator(Mat inmat,Mat *mat)
245b0a32e0cSBarry Smith {
246b0a32e0cSBarry Smith   Vec            in,out;
247dfbe8321SBarry Smith   PetscErrorCode ierr;
248*38baddfdSBarry Smith   PetscInt       i,M,m,*rows,start,end;
249b0a32e0cSBarry Smith   MPI_Comm       comm;
25087828ca2SBarry Smith   PetscScalar    *array,zero = 0.0,one = 1.0;
251*38baddfdSBarry Smith   PetscMPIInt    size;
252b0a32e0cSBarry Smith 
253b0a32e0cSBarry Smith   PetscFunctionBegin;
2544482741eSBarry Smith   PetscValidHeaderSpecific(inmat,MAT_COOKIE,1);
2554482741eSBarry Smith   PetscValidPointer(mat,2);
256b0a32e0cSBarry Smith 
257b0a32e0cSBarry Smith   comm = inmat->comm;
258b0a32e0cSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
259b0a32e0cSBarry Smith 
260b22afee1SSatish Balay   ierr = MatGetLocalSize(inmat,&m,0);CHKERRQ(ierr);
261b22afee1SSatish Balay   ierr = MatGetSize(inmat,&M,0);CHKERRQ(ierr);
262b0a32e0cSBarry Smith   ierr = VecCreateMPI(comm,m,M,&in);CHKERRQ(ierr);
263b0a32e0cSBarry Smith   ierr = VecDuplicate(in,&out);CHKERRQ(ierr);
264b0a32e0cSBarry Smith   ierr = VecGetOwnershipRange(in,&start,&end);CHKERRQ(ierr);
265*38baddfdSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&rows);CHKERRQ(ierr);
266b0a32e0cSBarry Smith   for (i=0; i<m; i++) {rows[i] = start + i;}
267b0a32e0cSBarry Smith 
268be5d1d56SKris Buschelman   ierr = MatCreate(comm,m,m,M,M,mat);CHKERRQ(ierr);
269b0a32e0cSBarry Smith   if (size == 1) {
270be5d1d56SKris Buschelman     ierr = MatSetType(*mat,MATSEQDENSE);CHKERRQ(ierr);
271be5d1d56SKris Buschelman     ierr = MatSeqDenseSetPreallocation(*mat,PETSC_NULL);CHKERRQ(ierr);
272b0a32e0cSBarry Smith   } else {
273be5d1d56SKris Buschelman     ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr);
274be5d1d56SKris Buschelman     ierr = MatMPIAIJSetPreallocation(*mat,0,PETSC_NULL,0,PETSC_NULL);CHKERRQ(ierr);
275b0a32e0cSBarry Smith   }
276b0a32e0cSBarry Smith 
277b0a32e0cSBarry Smith   for (i=0; i<M; i++) {
278b0a32e0cSBarry Smith 
279b0a32e0cSBarry Smith     ierr = VecSet(&zero,in);CHKERRQ(ierr);
280b0a32e0cSBarry Smith     ierr = VecSetValues(in,1,&i,&one,INSERT_VALUES);CHKERRQ(ierr);
281b0a32e0cSBarry Smith     ierr = VecAssemblyBegin(in);CHKERRQ(ierr);
282b0a32e0cSBarry Smith     ierr = VecAssemblyEnd(in);CHKERRQ(ierr);
283b0a32e0cSBarry Smith 
284b0a32e0cSBarry Smith     ierr = MatMult(inmat,in,out);CHKERRQ(ierr);
285b0a32e0cSBarry Smith 
286b0a32e0cSBarry Smith     ierr = VecGetArray(out,&array);CHKERRQ(ierr);
287b0a32e0cSBarry Smith     ierr = MatSetValues(*mat,m,rows,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
288b0a32e0cSBarry Smith     ierr = VecRestoreArray(out,&array);CHKERRQ(ierr);
289b0a32e0cSBarry Smith 
290b0a32e0cSBarry Smith   }
291b0a32e0cSBarry Smith   ierr = PetscFree(rows);CHKERRQ(ierr);
292b0a32e0cSBarry Smith   ierr = VecDestroy(out);CHKERRQ(ierr);
293b0a32e0cSBarry Smith   ierr = VecDestroy(in);CHKERRQ(ierr);
294b0a32e0cSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
295b0a32e0cSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
296b0a32e0cSBarry Smith   PetscFunctionReturn(0);
297b0a32e0cSBarry Smith }
298b0a32e0cSBarry Smith 
29924f910e3SHong Zhang /* Get the map xtoy which is used by MatAXPY() in the case of SUBSET_NONZERO_PATTERN */
30024f910e3SHong Zhang #undef __FUNCT__
30124f910e3SHong Zhang #define __FUNCT__ "MatAXPYGetxtoy_Private"
302*38baddfdSBarry Smith PetscErrorCode MatAXPYGetxtoy_Private(PetscInt m,PetscInt *xi,PetscInt *xj,PetscInt *xgarray, PetscInt *yi,PetscInt *yj,PetscInt *ygarray, PetscInt **xtoy)
30324f910e3SHong Zhang {
3046849ba73SBarry Smith   PetscErrorCode ierr;
305*38baddfdSBarry Smith   PetscInt       row,i,nz,xcol,ycol,jx,jy,*x2y;
30624f910e3SHong Zhang 
30724f910e3SHong Zhang   PetscFunctionBegin;
308*38baddfdSBarry Smith   ierr = PetscMalloc(xi[m]*sizeof(PetscInt),&x2y);CHKERRQ(ierr);
30924f910e3SHong Zhang   i = 0;
31024f910e3SHong Zhang   for (row=0; row<m; row++){
31124f910e3SHong Zhang     nz = xi[1] - xi[0];
31224f910e3SHong Zhang     jy = 0;
31324f910e3SHong Zhang     for (jx=0; jx<nz; jx++,jy++){
31424f910e3SHong Zhang       if (xgarray && ygarray){
31524f910e3SHong Zhang         xcol = xgarray[xj[*xi + jx]];
31624f910e3SHong Zhang         ycol = ygarray[yj[*yi + jy]];
31724f910e3SHong Zhang       } else {
31824f910e3SHong Zhang         xcol = xj[*xi + jx];
31924f910e3SHong Zhang         ycol = yj[*yi + jy];  /* col index for y */
32024f910e3SHong Zhang       }
32124f910e3SHong Zhang       while ( ycol < xcol ) {
32224f910e3SHong Zhang         jy++;
32324f910e3SHong Zhang         if (ygarray){
32424f910e3SHong Zhang           ycol = ygarray[yj[*yi + jy]];
32524f910e3SHong Zhang         } else {
32624f910e3SHong Zhang           ycol = yj[*yi + jy];
32724f910e3SHong Zhang         }
32824f910e3SHong Zhang       }
329*38baddfdSBarry Smith       if (xcol != ycol) SETERRQ2(PETSC_ERR_ARG_WRONG,"X matrix entry (%d,%d) is not in Y matrix",(int)row,(int)ycol);
33024f910e3SHong Zhang       x2y[i++] = *yi + jy;
33124f910e3SHong Zhang     }
33224f910e3SHong Zhang     xi++; yi++;
33324f910e3SHong Zhang   }
33424f910e3SHong Zhang   *xtoy = x2y;
33524f910e3SHong Zhang   PetscFunctionReturn(0);
33624f910e3SHong Zhang }
337