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