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