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