xref: /petsc/src/mat/utils/axpy.c (revision c5eb91543d2ee8daf880d93389b892228ddada03)
1be1d678aSKris Buschelman #define PETSCMAT_DLL
26f79c3a4SBarry Smith 
37c4f633dSBarry Smith #include "private/matimpl.h"  /*I   "petscmat.h"  I*/
46f79c3a4SBarry Smith 
54a2ae208SSatish Balay #undef __FUNCT__
64a2ae208SSatish Balay #define __FUNCT__ "MatAXPY"
706be10caSBarry Smith /*@
821c89e3eSBarry Smith    MatAXPY - Computes Y = a*X + Y.
96f79c3a4SBarry Smith 
103f9fe445SBarry Smith    Logically  Collective on Mat
11fee21e36SBarry Smith 
1298a79cdbSBarry Smith    Input Parameters:
13607cd303SBarry Smith +  a - the scalar multiplier
14607cd303SBarry Smith .  X - the first matrix
15607cd303SBarry Smith .  Y - the second matrix
16407f6b05SHong Zhang -  str - either SAME_NONZERO_PATTERN, DIFFERENT_NONZERO_PATTERN
17407f6b05SHong Zhang          or SUBSET_NONZERO_PATTERN (nonzeros of X is a subset of Y's)
1898a79cdbSBarry 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  @*/
28f4df32b1SMatthew Knepley PetscErrorCode PETSCMAT_DLLEXPORT MatAXPY(Mat Y,PetscScalar a,Mat X,MatStructure str)
296f79c3a4SBarry Smith {
306849ba73SBarry Smith   PetscErrorCode ierr;
31c1ac3661SBarry Smith   PetscInt       m1,m2,n1,n2;
326f79c3a4SBarry Smith 
333a40ed3dSBarry Smith   PetscFunctionBegin;
340700a824SBarry Smith   PetscValidHeaderSpecific(X,MAT_CLASSID,3);
350700a824SBarry Smith   PetscValidHeaderSpecific(Y,MAT_CLASSID,1);
36*c5eb9154SBarry Smith   PetscValidLogicalCollectiveScalar(Y,a,2);
37273d9f13SBarry Smith   ierr = MatGetSize(X,&m1,&n1);CHKERRQ(ierr);
38273d9f13SBarry Smith   ierr = MatGetSize(Y,&m2,&n2);CHKERRQ(ierr);
39e32f2f54SBarry Smith   if (m1 != m2 || n1 != n2) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Non conforming matrix add: %D %D %D %D",m1,m2,n1,n2);
401987afe7SBarry Smith 
41e8136da8SHong Zhang   ierr = PetscLogEventBegin(MAT_AXPY,Y,0,0,0);CHKERRQ(ierr);
42f4df32b1SMatthew Knepley   if (Y->ops->axpy) {
43f4df32b1SMatthew Knepley     ierr = (*Y->ops->axpy)(Y,a,X,str);CHKERRQ(ierr);
44d4bb536fSBarry Smith   } else {
45f4df32b1SMatthew Knepley     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
46607cd303SBarry Smith   }
47e8136da8SHong Zhang   ierr = PetscLogEventEnd(MAT_AXPY,Y,0,0,0);CHKERRQ(ierr);
48607cd303SBarry Smith   PetscFunctionReturn(0);
49607cd303SBarry Smith }
50607cd303SBarry Smith 
51607cd303SBarry Smith 
52607cd303SBarry Smith #undef __FUNCT__
53607cd303SBarry Smith #define __FUNCT__ "MatAXPY_Basic"
54f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_Basic(Mat Y,PetscScalar a,Mat X,MatStructure str)
55607cd303SBarry Smith {
5638baddfdSBarry Smith   PetscInt          i,start,end,j,ncols,m,n;
576849ba73SBarry Smith   PetscErrorCode    ierr;
5838baddfdSBarry Smith   const PetscInt    *row;
59b3cc6726SBarry Smith   PetscScalar       *val;
60b3cc6726SBarry Smith   const PetscScalar *vals;
61607cd303SBarry Smith 
62607cd303SBarry Smith   PetscFunctionBegin;
638dadbd76SSatish Balay   ierr = MatGetSize(X,&m,&n);CHKERRQ(ierr);
6490f02eecSBarry Smith   ierr = MatGetOwnershipRange(X,&start,&end);CHKERRQ(ierr);
65f4df32b1SMatthew Knepley   if (a == 1.0) {
66d4bb536fSBarry Smith     for (i = start; i < end; i++) {
67d4bb536fSBarry Smith       ierr = MatGetRow(X,i,&ncols,&row,&vals);CHKERRQ(ierr);
68d4bb536fSBarry Smith       ierr = MatSetValues(Y,1,&i,ncols,row,vals,ADD_VALUES);CHKERRQ(ierr);
69d4bb536fSBarry Smith       ierr = MatRestoreRow(X,i,&ncols,&row,&vals);CHKERRQ(ierr);
70d4bb536fSBarry Smith     }
71d4bb536fSBarry Smith   } else {
72b3cc6726SBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscScalar),&val);CHKERRQ(ierr);
7306be10caSBarry Smith     for (i=start; i<end; i++) {
74b3cc6726SBarry Smith       ierr = MatGetRow(X,i,&ncols,&row,&vals);CHKERRQ(ierr);
7506be10caSBarry Smith       for (j=0; j<ncols; j++) {
76f4df32b1SMatthew Knepley 	val[j] = a*vals[j];
776f79c3a4SBarry Smith       }
78b3cc6726SBarry Smith       ierr = MatSetValues(Y,1,&i,ncols,row,val,ADD_VALUES);CHKERRQ(ierr);
79b3cc6726SBarry Smith       ierr = MatRestoreRow(X,i,&ncols,&row,&vals);CHKERRQ(ierr);
806f79c3a4SBarry Smith     }
81b3cc6726SBarry Smith     ierr = PetscFree(val);CHKERRQ(ierr);
82d4bb536fSBarry Smith   }
836d4a8577SBarry Smith   ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
846d4a8577SBarry Smith   ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
853a40ed3dSBarry Smith   PetscFunctionReturn(0);
866f79c3a4SBarry Smith }
87052efed2SBarry Smith 
884a2ae208SSatish Balay #undef __FUNCT__
894a2ae208SSatish Balay #define __FUNCT__ "MatShift"
90052efed2SBarry Smith /*@
9187828ca2SBarry Smith    MatShift - Computes Y =  Y + a I, where a is a PetscScalar and I is the identity matrix.
92052efed2SBarry Smith 
933f9fe445SBarry Smith    Neighbor-wise Collective on Mat
94fee21e36SBarry Smith 
9598a79cdbSBarry Smith    Input Parameters:
9698a79cdbSBarry Smith +  Y - the matrices
9787828ca2SBarry Smith -  a - the PetscScalar
9898a79cdbSBarry Smith 
992860a424SLois Curfman McInnes    Level: intermediate
1002860a424SLois Curfman McInnes 
101052efed2SBarry Smith .keywords: matrix, add, shift
1026b9ee512SLois Curfman McInnes 
103f56f2b3fSBarry Smith .seealso: MatDiagonalSet()
104052efed2SBarry Smith  @*/
105f4df32b1SMatthew Knepley PetscErrorCode PETSCMAT_DLLEXPORT MatShift(Mat Y,PetscScalar a)
106052efed2SBarry Smith {
1076849ba73SBarry Smith   PetscErrorCode ierr;
10838baddfdSBarry Smith   PetscInt       i,start,end;
109052efed2SBarry Smith 
1103a40ed3dSBarry Smith   PetscFunctionBegin;
1110700a824SBarry Smith   PetscValidHeaderSpecific(Y,MAT_CLASSID,1);
11217186662SBarry Smith   if (!Y->assembled) SETERRQ(((PetscObject)Y)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
11317186662SBarry Smith   if (Y->factortype) SETERRQ(((PetscObject)Y)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
114b50b34bdSBarry Smith   ierr = MatPreallocated(Y);CHKERRQ(ierr);
115b50b34bdSBarry Smith 
116f830108cSBarry Smith   if (Y->ops->shift) {
117f4df32b1SMatthew Knepley     ierr = (*Y->ops->shift)(Y,a);CHKERRQ(ierr);
11862d58ce1SBarry Smith   } else {
119f4df32b1SMatthew Knepley     PetscScalar alpha = a;
120d4bb536fSBarry Smith     ierr = MatGetOwnershipRange(Y,&start,&end);CHKERRQ(ierr);
121052efed2SBarry Smith     for (i=start; i<end; i++) {
122f4df32b1SMatthew Knepley       ierr = MatSetValues(Y,1,&i,1,&i,&alpha,ADD_VALUES);CHKERRQ(ierr);
123052efed2SBarry Smith     }
1246d4a8577SBarry Smith     ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1256d4a8577SBarry Smith     ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
126052efed2SBarry Smith   }
1273a40ed3dSBarry Smith   PetscFunctionReturn(0);
128052efed2SBarry Smith }
1296d84be18SBarry Smith 
1304a2ae208SSatish Balay #undef __FUNCT__
13109f38230SBarry Smith #define __FUNCT__ "MatDiagonalSet_Default"
13209f38230SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatDiagonalSet_Default(Mat Y,Vec D,InsertMode is)
13309f38230SBarry Smith {
13409f38230SBarry Smith   PetscErrorCode ierr;
13509f38230SBarry Smith   PetscInt       i,start,end,vstart,vend;
13609f38230SBarry Smith   PetscScalar    *v;
13709f38230SBarry Smith 
13809f38230SBarry Smith   PetscFunctionBegin;
13909f38230SBarry Smith   ierr = VecGetOwnershipRange(D,&vstart,&vend);CHKERRQ(ierr);
14009f38230SBarry Smith   ierr = MatGetOwnershipRange(Y,&start,&end);CHKERRQ(ierr);
14109f38230SBarry Smith   if (vstart != start || vend != end) {
142e32f2f54SBarry Smith     SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Vector ownership range not compatible with matrix: %D %D vec %D %D mat",vstart,vend,start,end);
14309f38230SBarry Smith   }
14409f38230SBarry Smith   ierr = VecGetArray(D,&v);CHKERRQ(ierr);
14509f38230SBarry Smith   for (i=start; i<end; i++) {
14609f38230SBarry Smith     ierr = MatSetValues(Y,1,&i,1,&i,v+i-start,is);CHKERRQ(ierr);
14709f38230SBarry Smith   }
14809f38230SBarry Smith   ierr = VecRestoreArray(D,&v);CHKERRQ(ierr);
14909f38230SBarry Smith   ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
15009f38230SBarry Smith   ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
15109f38230SBarry Smith   PetscFunctionReturn(0);
15209f38230SBarry Smith }
15309f38230SBarry Smith 
15409f38230SBarry Smith #undef __FUNCT__
1554a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalSet"
1566d84be18SBarry Smith /*@
157f56f2b3fSBarry Smith    MatDiagonalSet - Computes Y = Y + D, where D is a diagonal matrix
158f56f2b3fSBarry Smith    that is represented as a vector. Or Y[i,i] = D[i] if InsertMode is
159f56f2b3fSBarry Smith    INSERT_VALUES.
1606d84be18SBarry Smith 
1616d84be18SBarry Smith    Input Parameters:
16298a79cdbSBarry Smith +  Y - the input matrix
163f56f2b3fSBarry Smith .  D - the diagonal matrix, represented as a vector
164f56f2b3fSBarry Smith -  i - INSERT_VALUES or ADD_VALUES
1656d84be18SBarry Smith 
1663f9fe445SBarry Smith    Neighbor-wise Collective on Mat and Vec
167fee21e36SBarry Smith 
1682860a424SLois Curfman McInnes    Level: intermediate
1692860a424SLois Curfman McInnes 
1706b9ee512SLois Curfman McInnes .keywords: matrix, add, shift, diagonal
1716b9ee512SLois Curfman McInnes 
1726b9ee512SLois Curfman McInnes .seealso: MatShift()
1736d84be18SBarry Smith @*/
174be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatDiagonalSet(Mat Y,Vec D,InsertMode is)
1756d84be18SBarry Smith {
1766849ba73SBarry Smith   PetscErrorCode ierr;
1776d84be18SBarry Smith 
1783a40ed3dSBarry Smith   PetscFunctionBegin;
1790700a824SBarry Smith   PetscValidHeaderSpecific(Y,MAT_CLASSID,1);
1800700a824SBarry Smith   PetscValidHeaderSpecific(D,VEC_CLASSID,2);
181f56f2b3fSBarry Smith   if (Y->ops->diagonalset) {
182f56f2b3fSBarry Smith     ierr = (*Y->ops->diagonalset)(Y,D,is);CHKERRQ(ierr);
18394d884c6SBarry Smith   } else {
18409f38230SBarry Smith     ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr);
1856d84be18SBarry Smith   }
1863a40ed3dSBarry Smith   PetscFunctionReturn(0);
1876d84be18SBarry Smith }
188d4bb536fSBarry Smith 
1894a2ae208SSatish Balay #undef __FUNCT__
1904a2ae208SSatish Balay #define __FUNCT__ "MatAYPX"
191d4bb536fSBarry Smith /*@
19204aac2b0SHong Zhang    MatAYPX - Computes Y = a*Y + X.
193d4bb536fSBarry Smith 
1943f9fe445SBarry Smith    Logically on Mat
195fee21e36SBarry Smith 
19698a79cdbSBarry Smith    Input Parameters:
19704aac2b0SHong Zhang +  a - the PetscScalar multiplier
19804aac2b0SHong Zhang .  Y - the first matrix
19904aac2b0SHong Zhang .  X - the second matrix
20004aac2b0SHong Zhang -  str - either SAME_NONZERO_PATTERN, DIFFERENT_NONZERO_PATTERN or SUBSET_NONZERO_PATTERN
201d4bb536fSBarry Smith 
2022860a424SLois Curfman McInnes    Notes:
20304aac2b0SHong Zhang      Will only be efficient if one has the SAME_NONZERO_PATTERN or SUBSET_NONZERO_PATTERN
204607cd303SBarry Smith 
2052860a424SLois Curfman McInnes    Level: intermediate
2062860a424SLois Curfman McInnes 
207d4bb536fSBarry Smith .keywords: matrix, add
208d4bb536fSBarry Smith 
2092860a424SLois Curfman McInnes .seealso: MatAXPY()
210d4bb536fSBarry Smith  @*/
21104aac2b0SHong Zhang PetscErrorCode PETSCMAT_DLLEXPORT MatAYPX(Mat Y,PetscScalar a,Mat X,MatStructure str)
212d4bb536fSBarry Smith {
21387828ca2SBarry Smith   PetscScalar    one = 1.0;
2146849ba73SBarry Smith   PetscErrorCode ierr;
21538baddfdSBarry Smith   PetscInt       mX,mY,nX,nY;
216d4bb536fSBarry Smith 
2173a40ed3dSBarry Smith   PetscFunctionBegin;
218*c5eb9154SBarry Smith   PetscValidHeaderSpecific(X,MAT_CLASSID,3);
2190700a824SBarry Smith   PetscValidHeaderSpecific(Y,MAT_CLASSID,1);
220*c5eb9154SBarry Smith   PetscValidLogicalCollectiveScalar(Y,a,2);
221329f5518SBarry Smith   ierr = MatGetSize(X,&mX,&nX);CHKERRQ(ierr);
222329f5518SBarry Smith   ierr = MatGetSize(X,&mY,&nY);CHKERRQ(ierr);
223e32f2f54SBarry Smith   if (mX != mY || nX != nY) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Non conforming matrices: %D %D first %D %D second",mX,mY,nX,nY);
224d4bb536fSBarry Smith 
225f4df32b1SMatthew Knepley   ierr = MatScale(Y,a);CHKERRQ(ierr);
226cb9801acSJed Brown   ierr = MatAXPY(Y,one,X,str);CHKERRQ(ierr);
2273a40ed3dSBarry Smith   PetscFunctionReturn(0);
228d4bb536fSBarry Smith }
229b0a32e0cSBarry Smith 
2304a2ae208SSatish Balay #undef __FUNCT__
2314a2ae208SSatish Balay #define __FUNCT__ "MatComputeExplicitOperator"
232b0a32e0cSBarry Smith /*@
233b0a32e0cSBarry Smith     MatComputeExplicitOperator - Computes the explicit matrix
234b0a32e0cSBarry Smith 
235b0a32e0cSBarry Smith     Collective on Mat
236b0a32e0cSBarry Smith 
237b0a32e0cSBarry Smith     Input Parameter:
238b0a32e0cSBarry Smith .   inmat - the matrix
239b0a32e0cSBarry Smith 
240b0a32e0cSBarry Smith     Output Parameter:
241b0a32e0cSBarry Smith .   mat - the explict preconditioned operator
242b0a32e0cSBarry Smith 
243b0a32e0cSBarry Smith     Notes:
244b0a32e0cSBarry Smith     This computation is done by applying the operators to columns of the
245b0a32e0cSBarry Smith     identity matrix.
246b0a32e0cSBarry Smith 
247b0a32e0cSBarry Smith     Currently, this routine uses a dense matrix format when 1 processor
248b0a32e0cSBarry Smith     is used and a sparse format otherwise.  This routine is costly in general,
249b0a32e0cSBarry Smith     and is recommended for use only with relatively small systems.
250b0a32e0cSBarry Smith 
251b0a32e0cSBarry Smith     Level: advanced
252b0a32e0cSBarry Smith 
253b0a32e0cSBarry Smith .keywords: Mat, compute, explicit, operator
254b0a32e0cSBarry Smith 
255b0a32e0cSBarry Smith @*/
256be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatComputeExplicitOperator(Mat inmat,Mat *mat)
257b0a32e0cSBarry Smith {
258b0a32e0cSBarry Smith   Vec            in,out;
259dfbe8321SBarry Smith   PetscErrorCode ierr;
2600b12b109SJed Brown   PetscInt       i,m,n,M,N,*rows,start,end;
261b0a32e0cSBarry Smith   MPI_Comm       comm;
26287828ca2SBarry Smith   PetscScalar    *array,zero = 0.0,one = 1.0;
26338baddfdSBarry Smith   PetscMPIInt    size;
264b0a32e0cSBarry Smith 
265b0a32e0cSBarry Smith   PetscFunctionBegin;
2660700a824SBarry Smith   PetscValidHeaderSpecific(inmat,MAT_CLASSID,1);
2674482741eSBarry Smith   PetscValidPointer(mat,2);
268b0a32e0cSBarry Smith 
2697adad957SLisandro Dalcin   comm = ((PetscObject)inmat)->comm;
270b0a32e0cSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
271b0a32e0cSBarry Smith 
2720b12b109SJed Brown   ierr = MatGetLocalSize(inmat,&m,&n);CHKERRQ(ierr);
2730b12b109SJed Brown   ierr = MatGetSize(inmat,&M,&N);CHKERRQ(ierr);
2740b12b109SJed Brown   ierr = MatGetVecs(inmat,&in,&out);CHKERRQ(ierr);
2750b12b109SJed Brown   ierr = VecSetOption(in,VEC_IGNORE_OFF_PROC_ENTRIES,PETSC_TRUE);CHKERRQ(ierr);
2760b12b109SJed Brown   ierr = VecGetOwnershipRange(out,&start,&end);CHKERRQ(ierr);
2770b12b109SJed Brown   ierr = PetscMalloc(m*sizeof(PetscInt),&rows);CHKERRQ(ierr);
278b0a32e0cSBarry Smith   for (i=0; i<m; i++) {rows[i] = start + i;}
279b0a32e0cSBarry Smith 
280f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
2810b12b109SJed Brown   ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr);
282b0a32e0cSBarry Smith   if (size == 1) {
283be5d1d56SKris Buschelman     ierr = MatSetType(*mat,MATSEQDENSE);CHKERRQ(ierr);
284be5d1d56SKris Buschelman     ierr = MatSeqDenseSetPreallocation(*mat,PETSC_NULL);CHKERRQ(ierr);
285b0a32e0cSBarry Smith   } else {
286be5d1d56SKris Buschelman     ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr);
2870b12b109SJed Brown     ierr = MatMPIAIJSetPreallocation(*mat,n,PETSC_NULL,N-n,PETSC_NULL);CHKERRQ(ierr);
288b0a32e0cSBarry Smith   }
289b0a32e0cSBarry Smith 
2900b12b109SJed Brown   for (i=0; i<N; i++) {
291b0a32e0cSBarry Smith 
2922dcb1b2aSMatthew Knepley     ierr = VecSet(in,zero);CHKERRQ(ierr);
293b0a32e0cSBarry Smith     ierr = VecSetValues(in,1,&i,&one,INSERT_VALUES);CHKERRQ(ierr);
294b0a32e0cSBarry Smith     ierr = VecAssemblyBegin(in);CHKERRQ(ierr);
295b0a32e0cSBarry Smith     ierr = VecAssemblyEnd(in);CHKERRQ(ierr);
296b0a32e0cSBarry Smith 
297b0a32e0cSBarry Smith     ierr = MatMult(inmat,in,out);CHKERRQ(ierr);
298b0a32e0cSBarry Smith 
299b0a32e0cSBarry Smith     ierr = VecGetArray(out,&array);CHKERRQ(ierr);
300b0a32e0cSBarry Smith     ierr = MatSetValues(*mat,m,rows,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
301b0a32e0cSBarry Smith     ierr = VecRestoreArray(out,&array);CHKERRQ(ierr);
302b0a32e0cSBarry Smith 
303b0a32e0cSBarry Smith   }
304b0a32e0cSBarry Smith   ierr = PetscFree(rows);CHKERRQ(ierr);
305b0a32e0cSBarry Smith   ierr = VecDestroy(out);CHKERRQ(ierr);
306b0a32e0cSBarry Smith   ierr = VecDestroy(in);CHKERRQ(ierr);
307b0a32e0cSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
308b0a32e0cSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
309b0a32e0cSBarry Smith   PetscFunctionReturn(0);
310b0a32e0cSBarry Smith }
311b0a32e0cSBarry Smith 
31224f910e3SHong Zhang /* Get the map xtoy which is used by MatAXPY() in the case of SUBSET_NONZERO_PATTERN */
31324f910e3SHong Zhang #undef __FUNCT__
31424f910e3SHong Zhang #define __FUNCT__ "MatAXPYGetxtoy_Private"
31538baddfdSBarry Smith PetscErrorCode MatAXPYGetxtoy_Private(PetscInt m,PetscInt *xi,PetscInt *xj,PetscInt *xgarray, PetscInt *yi,PetscInt *yj,PetscInt *ygarray, PetscInt **xtoy)
31624f910e3SHong Zhang {
3176849ba73SBarry Smith   PetscErrorCode ierr;
31838baddfdSBarry Smith   PetscInt       row,i,nz,xcol,ycol,jx,jy,*x2y;
31924f910e3SHong Zhang 
32024f910e3SHong Zhang   PetscFunctionBegin;
32138baddfdSBarry Smith   ierr = PetscMalloc(xi[m]*sizeof(PetscInt),&x2y);CHKERRQ(ierr);
32224f910e3SHong Zhang   i = 0;
32324f910e3SHong Zhang   for (row=0; row<m; row++){
32424f910e3SHong Zhang     nz = xi[1] - xi[0];
32524f910e3SHong Zhang     jy = 0;
32624f910e3SHong Zhang     for (jx=0; jx<nz; jx++,jy++){
32724f910e3SHong Zhang       if (xgarray && ygarray){
32824f910e3SHong Zhang         xcol = xgarray[xj[*xi + jx]];
32924f910e3SHong Zhang         ycol = ygarray[yj[*yi + jy]];
33024f910e3SHong Zhang       } else {
33124f910e3SHong Zhang         xcol = xj[*xi + jx];
33224f910e3SHong Zhang         ycol = yj[*yi + jy];  /* col index for y */
33324f910e3SHong Zhang       }
33424f910e3SHong Zhang       while ( ycol < xcol ) {
33524f910e3SHong Zhang         jy++;
33624f910e3SHong Zhang         if (ygarray){
33724f910e3SHong Zhang           ycol = ygarray[yj[*yi + jy]];
33824f910e3SHong Zhang         } else {
33924f910e3SHong Zhang           ycol = yj[*yi + jy];
34024f910e3SHong Zhang         }
34124f910e3SHong Zhang       }
342e32f2f54SBarry Smith       if (xcol != ycol) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"X matrix entry (%D,%D) is not in Y matrix",row,ycol);
34324f910e3SHong Zhang       x2y[i++] = *yi + jy;
34424f910e3SHong Zhang     }
34524f910e3SHong Zhang     xi++; yi++;
34624f910e3SHong Zhang   }
34724f910e3SHong Zhang   *xtoy = x2y;
34824f910e3SHong Zhang   PetscFunctionReturn(0);
34924f910e3SHong Zhang }
350