xref: /petsc/src/mat/utils/axpy.c (revision 72ce74bd30d5d09d1371a3eb387704f4af395d1a)
1 
2 #include "src/mat/matimpl.h"  /*I   "petscmat.h"  I*/
3 
4 #undef __FUNCT__
5 #define __FUNCT__ "MatAXPY"
6 /*@
7    MatAXPY - Computes Y = a*X + Y.
8 
9    Collective on Mat
10 
11    Input Parameters:
12 +  a - the scalar multiplier
13 .  X - the first matrix
14 .  Y - the second matrix
15 -  str - either SAME_NONZERO_PATTERN, DIFFERENT_NONZERO_PATTERN or SUBSET_NONZERO_PATTERN
16 
17    Contributed by: Matthew Knepley
18 
19    Notes:
20      Will only be efficient if one has the SAME_NONZERO_PATTERN or SUBSET_NONZERO_PATTERN
21 
22    Level: intermediate
23 
24 .keywords: matrix, add
25 
26 .seealso: MatAYPX()
27  @*/
28 PetscErrorCode MatAXPY(const PetscScalar *a,Mat X,Mat Y,MatStructure str)
29 {
30   int         m1,m2,n1,n2,ierr;
31 
32   PetscFunctionBegin;
33   PetscValidScalarPointer(a,1);
34   PetscValidHeaderSpecific(X,MAT_COOKIE,2);
35   PetscValidHeaderSpecific(Y,MAT_COOKIE,3);
36 
37   ierr = MatGetSize(X,&m1,&n1);CHKERRQ(ierr);
38   ierr = MatGetSize(Y,&m2,&n2);CHKERRQ(ierr);
39   if (m1 != m2 || n1 != n2) SETERRQ4(PETSC_ERR_ARG_SIZ,"Non conforming matrix add: %d %d %d %d",m1,m2,n1,n2);
40 
41   if (X->ops->axpy) {
42     ierr = (*X->ops->axpy)(a,X,Y,str);CHKERRQ(ierr);
43   } else {
44     ierr = MatAXPY_Basic(a,X,Y,str);CHKERRQ(ierr);
45   }
46   PetscFunctionReturn(0);
47 }
48 
49 
50 #undef __FUNCT__
51 #define __FUNCT__ "MatAXPY_Basic"
52 PetscErrorCode MatAXPY_Basic(const PetscScalar *a,Mat X,Mat Y,MatStructure str)
53 {
54   int               i,start,end,j,ncols,ierr,m,n;
55   const int         *row;
56   PetscScalar       *val;
57   const PetscScalar *vals;
58 
59   PetscFunctionBegin;
60   ierr = MatGetSize(X,&m,&n);CHKERRQ(ierr);
61   ierr = MatGetOwnershipRange(X,&start,&end);CHKERRQ(ierr);
62   if (*a == 1.0) {
63     for (i = start; i < end; i++) {
64       ierr = MatGetRow(X,i,&ncols,&row,&vals);CHKERRQ(ierr);
65       ierr = MatSetValues(Y,1,&i,ncols,row,vals,ADD_VALUES);CHKERRQ(ierr);
66       ierr = MatRestoreRow(X,i,&ncols,&row,&vals);CHKERRQ(ierr);
67     }
68   } else {
69     ierr = PetscMalloc((n+1)*sizeof(PetscScalar),&val);CHKERRQ(ierr);
70     for (i=start; i<end; i++) {
71       ierr = MatGetRow(X,i,&ncols,&row,&vals);CHKERRQ(ierr);
72       for (j=0; j<ncols; j++) {
73 	val[j] = (*a)*vals[j];
74       }
75       ierr = MatSetValues(Y,1,&i,ncols,row,val,ADD_VALUES);CHKERRQ(ierr);
76       ierr = MatRestoreRow(X,i,&ncols,&row,&vals);CHKERRQ(ierr);
77     }
78     ierr = PetscFree(val);CHKERRQ(ierr);
79   }
80   ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
81   ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
82   PetscFunctionReturn(0);
83 }
84 
85 #undef __FUNCT__
86 #define __FUNCT__ "MatShift"
87 /*@
88    MatShift - Computes Y =  Y + a I, where a is a PetscScalar and I is the identity matrix.
89 
90    Collective on Mat
91 
92    Input Parameters:
93 +  Y - the matrices
94 -  a - the PetscScalar
95 
96    Level: intermediate
97 
98 .keywords: matrix, add, shift
99 
100 .seealso: MatDiagonalSet()
101  @*/
102 PetscErrorCode MatShift(const PetscScalar *a,Mat Y)
103 {
104   int    i,start,end,ierr;
105 
106   PetscFunctionBegin;
107   PetscValidScalarPointer(a,1);
108   PetscValidHeaderSpecific(Y,MAT_COOKIE,2);
109   if (Y->ops->shift) {
110     ierr = (*Y->ops->shift)(a,Y);CHKERRQ(ierr);
111   } else {
112     ierr = MatGetOwnershipRange(Y,&start,&end);CHKERRQ(ierr);
113     for (i=start; i<end; i++) {
114       ierr = MatSetValues(Y,1,&i,1,&i,a,ADD_VALUES);CHKERRQ(ierr);
115     }
116     ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
117     ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
118   }
119   PetscFunctionReturn(0);
120 }
121 
122 #undef __FUNCT__
123 #define __FUNCT__ "MatDiagonalSet"
124 /*@
125    MatDiagonalSet - Computes Y = Y + D, where D is a diagonal matrix
126    that is represented as a vector. Or Y[i,i] = D[i] if InsertMode is
127    INSERT_VALUES.
128 
129    Input Parameters:
130 +  Y - the input matrix
131 .  D - the diagonal matrix, represented as a vector
132 -  i - INSERT_VALUES or ADD_VALUES
133 
134    Collective on Mat and Vec
135 
136    Level: intermediate
137 
138 .keywords: matrix, add, shift, diagonal
139 
140 .seealso: MatShift()
141 @*/
142 PetscErrorCode MatDiagonalSet(Mat Y,Vec D,InsertMode is)
143 {
144   int    i,start,end,ierr;
145 
146   PetscFunctionBegin;
147   PetscValidHeaderSpecific(Y,MAT_COOKIE,1);
148   PetscValidHeaderSpecific(D,VEC_COOKIE,2);
149   if (Y->ops->diagonalset) {
150     ierr = (*Y->ops->diagonalset)(Y,D,is);CHKERRQ(ierr);
151   } else {
152     int    vstart,vend;
153     PetscScalar *v;
154     ierr = VecGetOwnershipRange(D,&vstart,&vend);CHKERRQ(ierr);
155     ierr = MatGetOwnershipRange(Y,&start,&end);CHKERRQ(ierr);
156     if (vstart != start || vend != end) {
157       SETERRQ4(PETSC_ERR_ARG_SIZ,"Vector ownership range not compatible with matrix: %d %d vec %d %d mat",vstart,vend,start,end);
158     }
159     ierr = VecGetArray(D,&v);CHKERRQ(ierr);
160     for (i=start; i<end; i++) {
161       ierr = MatSetValues(Y,1,&i,1,&i,v+i-start,is);CHKERRQ(ierr);
162     }
163     ierr = VecRestoreArray(D,&v);CHKERRQ(ierr);
164     ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
165     ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
166   }
167   PetscFunctionReturn(0);
168 }
169 
170 #undef __FUNCT__
171 #define __FUNCT__ "MatAYPX"
172 /*@
173    MatAYPX - Computes Y = X + a*Y.
174 
175    Collective on Mat
176 
177    Input Parameters:
178 +  X,Y - the matrices
179 -  a - the PetscScalar multiplier
180 
181    Contributed by: Matthew Knepley
182 
183    Notes:
184    This routine currently uses the MatAXPY() implementation.
185 
186    This is slow, if you need it fast send email to petsc-maint@mcs.anl.gov
187 
188    Level: intermediate
189 
190 .keywords: matrix, add
191 
192 .seealso: MatAXPY()
193  @*/
194 PetscErrorCode MatAYPX(const PetscScalar *a,Mat X,Mat Y)
195 {
196   PetscScalar one = 1.0;
197   int         mX,mY,nX,nY,ierr;
198 
199   PetscFunctionBegin;
200   PetscValidScalarPointer(a,1);
201   PetscValidHeaderSpecific(X,MAT_COOKIE,2);
202   PetscValidHeaderSpecific(Y,MAT_COOKIE,3);
203 
204   ierr = MatGetSize(X,&mX,&nX);CHKERRQ(ierr);
205   ierr = MatGetSize(X,&mY,&nY);CHKERRQ(ierr);
206   if (mX != mY || nX != nY) SETERRQ4(PETSC_ERR_ARG_SIZ,"Non conforming matrices: %d %d first %d %d second",mX,mY,nX,nY);
207 
208   ierr = MatScale(a,Y);CHKERRQ(ierr);
209   ierr = MatAXPY(&one,X,Y,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr);
210   PetscFunctionReturn(0);
211 }
212 
213 #undef __FUNCT__
214 #define __FUNCT__ "MatComputeExplicitOperator"
215 /*@
216     MatComputeExplicitOperator - Computes the explicit matrix
217 
218     Collective on Mat
219 
220     Input Parameter:
221 .   inmat - the matrix
222 
223     Output Parameter:
224 .   mat - the explict preconditioned operator
225 
226     Notes:
227     This computation is done by applying the operators to columns of the
228     identity matrix.
229 
230     Currently, this routine uses a dense matrix format when 1 processor
231     is used and a sparse format otherwise.  This routine is costly in general,
232     and is recommended for use only with relatively small systems.
233 
234     Level: advanced
235 
236 .keywords: Mat, compute, explicit, operator
237 
238 @*/
239 PetscErrorCode MatComputeExplicitOperator(Mat inmat,Mat *mat)
240 {
241   Vec      in,out;
242   PetscErrorCode ierr;
243   int i,M,m,size,*rows,start,end;
244   MPI_Comm comm;
245   PetscScalar   *array,zero = 0.0,one = 1.0;
246 
247   PetscFunctionBegin;
248   PetscValidHeaderSpecific(inmat,MAT_COOKIE,1);
249   PetscValidPointer(mat,2);
250 
251   comm = inmat->comm;
252   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
253 
254   ierr = MatGetLocalSize(inmat,&m,0);CHKERRQ(ierr);
255   ierr = MatGetSize(inmat,&M,0);CHKERRQ(ierr);
256   ierr = VecCreateMPI(comm,m,M,&in);CHKERRQ(ierr);
257   ierr = VecDuplicate(in,&out);CHKERRQ(ierr);
258   ierr = VecGetOwnershipRange(in,&start,&end);CHKERRQ(ierr);
259   ierr = PetscMalloc((m+1)*sizeof(int),&rows);CHKERRQ(ierr);
260   for (i=0; i<m; i++) {rows[i] = start + i;}
261 
262   ierr = MatCreate(comm,m,m,M,M,mat);CHKERRQ(ierr);
263   if (size == 1) {
264     ierr = MatSetType(*mat,MATSEQDENSE);CHKERRQ(ierr);
265     ierr = MatSeqDenseSetPreallocation(*mat,PETSC_NULL);CHKERRQ(ierr);
266   } else {
267     ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr);
268     ierr = MatMPIAIJSetPreallocation(*mat,0,PETSC_NULL,0,PETSC_NULL);CHKERRQ(ierr);
269   }
270 
271   for (i=0; i<M; i++) {
272 
273     ierr = VecSet(&zero,in);CHKERRQ(ierr);
274     ierr = VecSetValues(in,1,&i,&one,INSERT_VALUES);CHKERRQ(ierr);
275     ierr = VecAssemblyBegin(in);CHKERRQ(ierr);
276     ierr = VecAssemblyEnd(in);CHKERRQ(ierr);
277 
278     ierr = MatMult(inmat,in,out);CHKERRQ(ierr);
279 
280     ierr = VecGetArray(out,&array);CHKERRQ(ierr);
281     ierr = MatSetValues(*mat,m,rows,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
282     ierr = VecRestoreArray(out,&array);CHKERRQ(ierr);
283 
284   }
285   ierr = PetscFree(rows);CHKERRQ(ierr);
286   ierr = VecDestroy(out);CHKERRQ(ierr);
287   ierr = VecDestroy(in);CHKERRQ(ierr);
288   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
289   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
290   PetscFunctionReturn(0);
291 }
292 
293 /* Get the map xtoy which is used by MatAXPY() in the case of SUBSET_NONZERO_PATTERN */
294 #undef __FUNCT__
295 #define __FUNCT__ "MatAXPYGetxtoy_Private"
296 PetscErrorCode MatAXPYGetxtoy_Private(int m,int *xi,int *xj,int *xgarray, int *yi,int *yj,int *ygarray, int **xtoy)
297 {
298   PetscErrorCode ierr,row,i,nz,xcol,ycol,jx,jy,*x2y;
299 
300   PetscFunctionBegin;
301   ierr = PetscMalloc(xi[m]*sizeof(int),&x2y);CHKERRQ(ierr);
302   i = 0;
303   for (row=0; row<m; row++){
304     nz = xi[1] - xi[0];
305     jy = 0;
306     for (jx=0; jx<nz; jx++,jy++){
307       if (xgarray && ygarray){
308         xcol = xgarray[xj[*xi + jx]];
309         ycol = ygarray[yj[*yi + jy]];
310       } else {
311         xcol = xj[*xi + jx];
312         ycol = yj[*yi + jy];  /* col index for y */
313       }
314       while ( ycol < xcol ) {
315         jy++;
316         if (ygarray){
317           ycol = ygarray[yj[*yi + jy]];
318         } else {
319           ycol = yj[*yi + jy];
320         }
321       }
322       if (xcol != ycol) SETERRQ2(PETSC_ERR_ARG_WRONG,"X matrix entry (%d,%d) is not in Y matrix",row,ycol);
323       x2y[i++] = *yi + jy;
324     }
325     xi++; yi++;
326   }
327   *xtoy = x2y;
328   PetscFunctionReturn(0);
329 }
330