xref: /petsc/src/mat/utils/axpy.c (revision 958c9bcce270cdc364c8832dcd56bbb1c4a38e24) !
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 int 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 int 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 int 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 int 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 int 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 int MatComputeExplicitOperator(Mat inmat,Mat *mat)
240 {
241   Vec      in,out;
242   int      ierr,i,M,m,size,*rows,start,end;
243   MPI_Comm comm;
244   PetscScalar   *array,zero = 0.0,one = 1.0;
245 
246   PetscFunctionBegin;
247   PetscValidHeaderSpecific(inmat,MAT_COOKIE,1);
248   PetscValidPointer(mat,2);
249 
250   comm = inmat->comm;
251   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
252 
253   ierr = MatGetLocalSize(inmat,&m,0);CHKERRQ(ierr);
254   ierr = MatGetSize(inmat,&M,0);CHKERRQ(ierr);
255   ierr = VecCreateMPI(comm,m,M,&in);CHKERRQ(ierr);
256   ierr = VecDuplicate(in,&out);CHKERRQ(ierr);
257   ierr = VecGetOwnershipRange(in,&start,&end);CHKERRQ(ierr);
258   ierr = PetscMalloc((m+1)*sizeof(int),&rows);CHKERRQ(ierr);
259   for (i=0; i<m; i++) {rows[i] = start + i;}
260 
261   ierr = MatCreate(comm,m,m,M,M,mat);CHKERRQ(ierr);
262   if (size == 1) {
263     ierr = MatSetType(*mat,MATSEQDENSE);CHKERRQ(ierr);
264     ierr = MatSeqDenseSetPreallocation(*mat,PETSC_NULL);CHKERRQ(ierr);
265   } else {
266     ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr);
267     ierr = MatMPIAIJSetPreallocation(*mat,0,PETSC_NULL,0,PETSC_NULL);CHKERRQ(ierr);
268   }
269 
270   for (i=0; i<M; i++) {
271 
272     ierr = VecSet(&zero,in);CHKERRQ(ierr);
273     ierr = VecSetValues(in,1,&i,&one,INSERT_VALUES);CHKERRQ(ierr);
274     ierr = VecAssemblyBegin(in);CHKERRQ(ierr);
275     ierr = VecAssemblyEnd(in);CHKERRQ(ierr);
276 
277     ierr = MatMult(inmat,in,out);CHKERRQ(ierr);
278 
279     ierr = VecGetArray(out,&array);CHKERRQ(ierr);
280     ierr = MatSetValues(*mat,m,rows,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
281     ierr = VecRestoreArray(out,&array);CHKERRQ(ierr);
282 
283   }
284   ierr = PetscFree(rows);CHKERRQ(ierr);
285   ierr = VecDestroy(out);CHKERRQ(ierr);
286   ierr = VecDestroy(in);CHKERRQ(ierr);
287   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
288   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
289   PetscFunctionReturn(0);
290 }
291 
292 /* Get the map xtoy which is used by MatAXPY() in the case of SUBSET_NONZERO_PATTERN */
293 #undef __FUNCT__
294 #define __FUNCT__ "MatAXPYGetxtoy_Private"
295 int MatAXPYGetxtoy_Private(int m,int *xi,int *xj,int *xgarray, int *yi,int *yj,int *ygarray, int **xtoy)
296 {
297   int ierr,row,i,nz,xcol,ycol,jx,jy,*x2y;
298 
299   PetscFunctionBegin;
300   ierr = PetscMalloc(xi[m]*sizeof(int),&x2y);CHKERRQ(ierr);
301   i = 0;
302   for (row=0; row<m; row++){
303     nz = xi[1] - xi[0];
304     jy = 0;
305     for (jx=0; jx<nz; jx++,jy++){
306       if (xgarray && ygarray){
307         xcol = xgarray[xj[*xi + jx]];
308         ycol = ygarray[yj[*yi + jy]];
309       } else {
310         xcol = xj[*xi + jx];
311         ycol = yj[*yi + jy];  /* col index for y */
312       }
313       while ( ycol < xcol ) {
314         jy++;
315         if (ygarray){
316           ycol = ygarray[yj[*yi + jy]];
317         } else {
318           ycol = yj[*yi + jy];
319         }
320       }
321       if (xcol != ycol) SETERRQ2(PETSC_ERR_ARG_WRONG,"X matrix entry (%d,%d) is not in Y matrix",row,ycol);
322       x2y[i++] = *yi + jy;
323     }
324     xi++; yi++;
325   }
326   *xtoy = x2y;
327   PetscFunctionReturn(0);
328 }
329