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",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 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 MatPreallocated(Y); 113 if (!Y->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 114 if (Y->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 115 if (Y->ops->shift) { 116 ierr = (*Y->ops->shift)(a,Y);CHKERRQ(ierr); 117 } else { 118 ierr = MatGetOwnershipRange(Y,&start,&end);CHKERRQ(ierr); 119 for (i=start; i<end; i++) { 120 ierr = MatSetValues(Y,1,&i,1,&i,a,ADD_VALUES);CHKERRQ(ierr); 121 } 122 ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 123 ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 124 } 125 PetscFunctionReturn(0); 126 } 127 128 #undef __FUNCT__ 129 #define __FUNCT__ "MatDiagonalSet" 130 /*@ 131 MatDiagonalSet - Computes Y = Y + D, where D is a diagonal matrix 132 that is represented as a vector. Or Y[i,i] = D[i] if InsertMode is 133 INSERT_VALUES. 134 135 Input Parameters: 136 + Y - the input matrix 137 . D - the diagonal matrix, represented as a vector 138 - i - INSERT_VALUES or ADD_VALUES 139 140 Collective on Mat and Vec 141 142 Level: intermediate 143 144 .keywords: matrix, add, shift, diagonal 145 146 .seealso: MatShift() 147 @*/ 148 PetscErrorCode MatDiagonalSet(Mat Y,Vec D,InsertMode is) 149 { 150 PetscErrorCode ierr; 151 PetscInt i,start,end; 152 153 PetscFunctionBegin; 154 PetscValidHeaderSpecific(Y,MAT_COOKIE,1); 155 PetscValidHeaderSpecific(D,VEC_COOKIE,2); 156 if (Y->ops->diagonalset) { 157 ierr = (*Y->ops->diagonalset)(Y,D,is);CHKERRQ(ierr); 158 } else { 159 PetscInt vstart,vend; 160 PetscScalar *v; 161 ierr = VecGetOwnershipRange(D,&vstart,&vend);CHKERRQ(ierr); 162 ierr = MatGetOwnershipRange(Y,&start,&end);CHKERRQ(ierr); 163 if (vstart != start || vend != end) { 164 SETERRQ4(PETSC_ERR_ARG_SIZ,"Vector ownership range not compatible with matrix: %D %D vec %D %D mat",vstart,vend,start,end); 165 } 166 ierr = VecGetArray(D,&v);CHKERRQ(ierr); 167 for (i=start; i<end; i++) { 168 ierr = MatSetValues(Y,1,&i,1,&i,v+i-start,is);CHKERRQ(ierr); 169 } 170 ierr = VecRestoreArray(D,&v);CHKERRQ(ierr); 171 ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 172 ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 173 } 174 PetscFunctionReturn(0); 175 } 176 177 #undef __FUNCT__ 178 #define __FUNCT__ "MatAYPX" 179 /*@ 180 MatAYPX - Computes Y = X + a*Y. 181 182 Collective on Mat 183 184 Input Parameters: 185 + X,Y - the matrices 186 - a - the PetscScalar multiplier 187 188 Contributed by: Matthew Knepley 189 190 Notes: 191 This routine currently uses the MatAXPY() implementation. 192 193 This is slow, if you need it fast send email to petsc-maint@mcs.anl.gov 194 195 Level: intermediate 196 197 .keywords: matrix, add 198 199 .seealso: MatAXPY() 200 @*/ 201 PetscErrorCode MatAYPX(const PetscScalar *a,Mat X,Mat Y) 202 { 203 PetscScalar one = 1.0; 204 PetscErrorCode ierr; 205 PetscInt mX,mY,nX,nY; 206 207 PetscFunctionBegin; 208 PetscValidScalarPointer(a,1); 209 PetscValidHeaderSpecific(X,MAT_COOKIE,2); 210 PetscValidHeaderSpecific(Y,MAT_COOKIE,3); 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(a,Y);CHKERRQ(ierr); 217 ierr = MatAXPY(&one,X,Y,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 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,m,m,M,M,mat);CHKERRQ(ierr); 272 if (size == 1) { 273 ierr = MatSetType(*mat,MATSEQDENSE);CHKERRQ(ierr); 274 #if !defined(PETSC_USE_64BIT_INT) 275 ierr = MatSeqDenseSetPreallocation(*mat,PETSC_NULL);CHKERRQ(ierr); 276 #endif 277 } else { 278 ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr); 279 ierr = MatMPIAIJSetPreallocation(*mat,0,PETSC_NULL,0,PETSC_NULL);CHKERRQ(ierr); 280 } 281 282 for (i=0; i<M; i++) { 283 284 ierr = VecSet(&zero,in);CHKERRQ(ierr); 285 ierr = VecSetValues(in,1,&i,&one,INSERT_VALUES);CHKERRQ(ierr); 286 ierr = VecAssemblyBegin(in);CHKERRQ(ierr); 287 ierr = VecAssemblyEnd(in);CHKERRQ(ierr); 288 289 ierr = MatMult(inmat,in,out);CHKERRQ(ierr); 290 291 ierr = VecGetArray(out,&array);CHKERRQ(ierr); 292 ierr = MatSetValues(*mat,m,rows,1,&i,array,INSERT_VALUES);CHKERRQ(ierr); 293 ierr = VecRestoreArray(out,&array);CHKERRQ(ierr); 294 295 } 296 ierr = PetscFree(rows);CHKERRQ(ierr); 297 ierr = VecDestroy(out);CHKERRQ(ierr); 298 ierr = VecDestroy(in);CHKERRQ(ierr); 299 ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 300 ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 301 PetscFunctionReturn(0); 302 } 303 304 /* Get the map xtoy which is used by MatAXPY() in the case of SUBSET_NONZERO_PATTERN */ 305 #undef __FUNCT__ 306 #define __FUNCT__ "MatAXPYGetxtoy_Private" 307 PetscErrorCode MatAXPYGetxtoy_Private(PetscInt m,PetscInt *xi,PetscInt *xj,PetscInt *xgarray, PetscInt *yi,PetscInt *yj,PetscInt *ygarray, PetscInt **xtoy) 308 { 309 PetscErrorCode ierr; 310 PetscInt row,i,nz,xcol,ycol,jx,jy,*x2y; 311 312 PetscFunctionBegin; 313 ierr = PetscMalloc(xi[m]*sizeof(PetscInt),&x2y);CHKERRQ(ierr); 314 i = 0; 315 for (row=0; row<m; row++){ 316 nz = xi[1] - xi[0]; 317 jy = 0; 318 for (jx=0; jx<nz; jx++,jy++){ 319 if (xgarray && ygarray){ 320 xcol = xgarray[xj[*xi + jx]]; 321 ycol = ygarray[yj[*yi + jy]]; 322 } else { 323 xcol = xj[*xi + jx]; 324 ycol = yj[*yi + jy]; /* col index for y */ 325 } 326 while ( ycol < xcol ) { 327 jy++; 328 if (ygarray){ 329 ycol = ygarray[yj[*yi + jy]]; 330 } else { 331 ycol = yj[*yi + jy]; 332 } 333 } 334 if (xcol != ycol) SETERRQ2(PETSC_ERR_ARG_WRONG,"X matrix entry (%D,%D) is not in Y matrix",row,ycol); 335 x2y[i++] = *yi + jy; 336 } 337 xi++; yi++; 338 } 339 *xtoy = x2y; 340 PetscFunctionReturn(0); 341 } 342