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