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 MatPreallocated(Y); 114 if (!Y->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 115 if (Y->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 116 if (Y->ops->shift) { 117 ierr = (*Y->ops->shift)(a,Y);CHKERRQ(ierr); 118 } else { 119 ierr = MatGetOwnershipRange(Y,&start,&end);CHKERRQ(ierr); 120 for (i=start; i<end; i++) { 121 ierr = MatSetValues(Y,1,&i,1,&i,a,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(const PetscScalar *a,Mat X,Mat Y) 203 { 204 PetscScalar one = 1.0; 205 PetscErrorCode ierr; 206 PetscInt mX,mY,nX,nY; 207 208 PetscFunctionBegin; 209 PetscValidScalarPointer(a,1); 210 PetscValidHeaderSpecific(X,MAT_COOKIE,2); 211 PetscValidHeaderSpecific(Y,MAT_COOKIE,3); 212 213 ierr = MatGetSize(X,&mX,&nX);CHKERRQ(ierr); 214 ierr = MatGetSize(X,&mY,&nY);CHKERRQ(ierr); 215 if (mX != mY || nX != nY) SETERRQ4(PETSC_ERR_ARG_SIZ,"Non conforming matrices: %D %D first %D %D second",mX,mY,nX,nY); 216 217 ierr = MatScale(a,Y);CHKERRQ(ierr); 218 ierr = MatAXPY(&one,X,Y,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr); 219 PetscFunctionReturn(0); 220 } 221 222 #undef __FUNCT__ 223 #define __FUNCT__ "MatComputeExplicitOperator" 224 /*@ 225 MatComputeExplicitOperator - Computes the explicit matrix 226 227 Collective on Mat 228 229 Input Parameter: 230 . inmat - the matrix 231 232 Output Parameter: 233 . mat - the explict preconditioned operator 234 235 Notes: 236 This computation is done by applying the operators to columns of the 237 identity matrix. 238 239 Currently, this routine uses a dense matrix format when 1 processor 240 is used and a sparse format otherwise. This routine is costly in general, 241 and is recommended for use only with relatively small systems. 242 243 Level: advanced 244 245 .keywords: Mat, compute, explicit, operator 246 247 @*/ 248 PetscErrorCode PETSCMAT_DLLEXPORT MatComputeExplicitOperator(Mat inmat,Mat *mat) 249 { 250 Vec in,out; 251 PetscErrorCode ierr; 252 PetscInt i,M,m,*rows,start,end; 253 MPI_Comm comm; 254 PetscScalar *array,zero = 0.0,one = 1.0; 255 PetscMPIInt size; 256 257 PetscFunctionBegin; 258 PetscValidHeaderSpecific(inmat,MAT_COOKIE,1); 259 PetscValidPointer(mat,2); 260 261 comm = inmat->comm; 262 ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 263 264 ierr = MatGetLocalSize(inmat,&m,0);CHKERRQ(ierr); 265 ierr = MatGetSize(inmat,&M,0);CHKERRQ(ierr); 266 ierr = VecCreateMPI(comm,m,M,&in);CHKERRQ(ierr); 267 ierr = VecDuplicate(in,&out);CHKERRQ(ierr); 268 ierr = VecGetOwnershipRange(in,&start,&end);CHKERRQ(ierr); 269 ierr = PetscMalloc((m+1)*sizeof(PetscInt),&rows);CHKERRQ(ierr); 270 for (i=0; i<m; i++) {rows[i] = start + i;} 271 272 ierr = MatCreate(comm,m,m,M,M,mat);CHKERRQ(ierr); 273 if (size == 1) { 274 ierr = MatSetType(*mat,MATSEQDENSE);CHKERRQ(ierr); 275 #if !defined(PETSC_USE_64BIT_INT) 276 ierr = MatSeqDenseSetPreallocation(*mat,PETSC_NULL);CHKERRQ(ierr); 277 #endif 278 } else { 279 ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr); 280 ierr = MatMPIAIJSetPreallocation(*mat,0,PETSC_NULL,0,PETSC_NULL);CHKERRQ(ierr); 281 } 282 283 for (i=0; i<M; i++) { 284 285 ierr = VecSet(&zero,in);CHKERRQ(ierr); 286 ierr = VecSetValues(in,1,&i,&one,INSERT_VALUES);CHKERRQ(ierr); 287 ierr = VecAssemblyBegin(in);CHKERRQ(ierr); 288 ierr = VecAssemblyEnd(in);CHKERRQ(ierr); 289 290 ierr = MatMult(inmat,in,out);CHKERRQ(ierr); 291 292 ierr = VecGetArray(out,&array);CHKERRQ(ierr); 293 ierr = MatSetValues(*mat,m,rows,1,&i,array,INSERT_VALUES);CHKERRQ(ierr); 294 ierr = VecRestoreArray(out,&array);CHKERRQ(ierr); 295 296 } 297 ierr = PetscFree(rows);CHKERRQ(ierr); 298 ierr = VecDestroy(out);CHKERRQ(ierr); 299 ierr = VecDestroy(in);CHKERRQ(ierr); 300 ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 301 ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 302 PetscFunctionReturn(0); 303 } 304 305 /* Get the map xtoy which is used by MatAXPY() in the case of SUBSET_NONZERO_PATTERN */ 306 #undef __FUNCT__ 307 #define __FUNCT__ "MatAXPYGetxtoy_Private" 308 PetscErrorCode MatAXPYGetxtoy_Private(PetscInt m,PetscInt *xi,PetscInt *xj,PetscInt *xgarray, PetscInt *yi,PetscInt *yj,PetscInt *ygarray, PetscInt **xtoy) 309 { 310 PetscErrorCode ierr; 311 PetscInt row,i,nz,xcol,ycol,jx,jy,*x2y; 312 313 PetscFunctionBegin; 314 ierr = PetscMalloc(xi[m]*sizeof(PetscInt),&x2y);CHKERRQ(ierr); 315 i = 0; 316 for (row=0; row<m; row++){ 317 nz = xi[1] - xi[0]; 318 jy = 0; 319 for (jx=0; jx<nz; jx++,jy++){ 320 if (xgarray && ygarray){ 321 xcol = xgarray[xj[*xi + jx]]; 322 ycol = ygarray[yj[*yi + jy]]; 323 } else { 324 xcol = xj[*xi + jx]; 325 ycol = yj[*yi + jy]; /* col index for y */ 326 } 327 while ( ycol < xcol ) { 328 jy++; 329 if (ygarray){ 330 ycol = ygarray[yj[*yi + jy]]; 331 } else { 332 ycol = yj[*yi + jy]; 333 } 334 } 335 if (xcol != ycol) SETERRQ2(PETSC_ERR_ARG_WRONG,"X matrix entry (%D,%D) is not in Y matrix",row,ycol); 336 x2y[i++] = *yi + jy; 337 } 338 xi++; yi++; 339 } 340 *xtoy = x2y; 341 PetscFunctionReturn(0); 342 } 343