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