1 2 /* 3 This provides a matrix that applies a VecScatter to a vector. 4 */ 5 6 #include <petsc/private/matimpl.h> /*I "petscmat.h" I*/ 7 #include <petsc/private/vecimpl.h> 8 9 typedef struct { 10 VecScatter scatter; 11 } Mat_Scatter; 12 13 /*@ 14 MatScatterGetVecScatter - Returns the user-provided scatter set with MatScatterSetVecScatter() 15 16 Not Collective, but not cannot use scatter if not used collectively on Mat 17 18 Input Parameter: 19 . mat - the matrix, should have been created with MatCreateScatter() or have type MATSCATTER 20 21 Output Parameter: 22 . scatter - the scatter context 23 24 Level: intermediate 25 26 .seealso: `MatCreateScatter()`, `MatScatterSetVecScatter()`, `MATSCATTER` 27 @*/ 28 PetscErrorCode MatScatterGetVecScatter(Mat mat,VecScatter *scatter) 29 { 30 Mat_Scatter *mscatter; 31 32 PetscFunctionBegin; 33 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 34 PetscValidPointer(scatter,2); 35 mscatter = (Mat_Scatter*)mat->data; 36 *scatter = mscatter->scatter; 37 PetscFunctionReturn(0); 38 } 39 40 PetscErrorCode MatDestroy_Scatter(Mat mat) 41 { 42 Mat_Scatter *scatter = (Mat_Scatter*)mat->data; 43 44 PetscFunctionBegin; 45 PetscCall(VecScatterDestroy(&scatter->scatter)); 46 PetscCall(PetscFree(mat->data)); 47 PetscFunctionReturn(0); 48 } 49 50 PetscErrorCode MatMult_Scatter(Mat A,Vec x,Vec y) 51 { 52 Mat_Scatter *scatter = (Mat_Scatter*)A->data; 53 54 PetscFunctionBegin; 55 PetscCheck(scatter->scatter,PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Need to first call MatScatterSetScatter()"); 56 PetscCall(VecZeroEntries(y)); 57 PetscCall(VecScatterBegin(scatter->scatter,x,y,ADD_VALUES,SCATTER_FORWARD)); 58 PetscCall(VecScatterEnd(scatter->scatter,x,y,ADD_VALUES,SCATTER_FORWARD)); 59 PetscFunctionReturn(0); 60 } 61 62 PetscErrorCode MatMultAdd_Scatter(Mat A,Vec x,Vec y,Vec z) 63 { 64 Mat_Scatter *scatter = (Mat_Scatter*)A->data; 65 66 PetscFunctionBegin; 67 PetscCheck(scatter->scatter,PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Need to first call MatScatterSetScatter()"); 68 if (z != y) PetscCall(VecCopy(y,z)); 69 PetscCall(VecScatterBegin(scatter->scatter,x,z,ADD_VALUES,SCATTER_FORWARD)); 70 PetscCall(VecScatterEnd(scatter->scatter,x,z,ADD_VALUES,SCATTER_FORWARD)); 71 PetscFunctionReturn(0); 72 } 73 74 PetscErrorCode MatMultTranspose_Scatter(Mat A,Vec x,Vec y) 75 { 76 Mat_Scatter *scatter = (Mat_Scatter*)A->data; 77 78 PetscFunctionBegin; 79 PetscCheck(scatter->scatter,PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Need to first call MatScatterSetScatter()"); 80 PetscCall(VecZeroEntries(y)); 81 PetscCall(VecScatterBegin(scatter->scatter,x,y,ADD_VALUES,SCATTER_REVERSE)); 82 PetscCall(VecScatterEnd(scatter->scatter,x,y,ADD_VALUES,SCATTER_REVERSE)); 83 PetscFunctionReturn(0); 84 } 85 86 PetscErrorCode MatMultTransposeAdd_Scatter(Mat A,Vec x,Vec y,Vec z) 87 { 88 Mat_Scatter *scatter = (Mat_Scatter*)A->data; 89 90 PetscFunctionBegin; 91 PetscCheck(scatter->scatter,PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Need to first call MatScatterSetScatter()"); 92 if (z != y) PetscCall(VecCopy(y,z)); 93 PetscCall(VecScatterBegin(scatter->scatter,x,z,ADD_VALUES,SCATTER_REVERSE)); 94 PetscCall(VecScatterEnd(scatter->scatter,x,z,ADD_VALUES,SCATTER_REVERSE)); 95 PetscFunctionReturn(0); 96 } 97 98 static struct _MatOps MatOps_Values = {NULL, 99 NULL, 100 NULL, 101 MatMult_Scatter, 102 /* 4*/ MatMultAdd_Scatter, 103 MatMultTranspose_Scatter, 104 MatMultTransposeAdd_Scatter, 105 NULL, 106 NULL, 107 NULL, 108 /* 10*/ NULL, 109 NULL, 110 NULL, 111 NULL, 112 NULL, 113 /* 15*/ NULL, 114 NULL, 115 NULL, 116 NULL, 117 NULL, 118 /* 20*/ NULL, 119 NULL, 120 NULL, 121 NULL, 122 /* 24*/ NULL, 123 NULL, 124 NULL, 125 NULL, 126 NULL, 127 /* 29*/ NULL, 128 NULL, 129 NULL, 130 NULL, 131 NULL, 132 /* 34*/ NULL, 133 NULL, 134 NULL, 135 NULL, 136 NULL, 137 /* 39*/ NULL, 138 NULL, 139 NULL, 140 NULL, 141 NULL, 142 /* 44*/ NULL, 143 NULL, 144 MatShift_Basic, 145 NULL, 146 NULL, 147 /* 49*/ NULL, 148 NULL, 149 NULL, 150 NULL, 151 NULL, 152 /* 54*/ NULL, 153 NULL, 154 NULL, 155 NULL, 156 NULL, 157 /* 59*/ NULL, 158 MatDestroy_Scatter, 159 NULL, 160 NULL, 161 NULL, 162 /* 64*/ NULL, 163 NULL, 164 NULL, 165 NULL, 166 NULL, 167 /* 69*/ NULL, 168 NULL, 169 NULL, 170 NULL, 171 NULL, 172 /* 74*/ NULL, 173 NULL, 174 NULL, 175 NULL, 176 NULL, 177 /* 79*/ NULL, 178 NULL, 179 NULL, 180 NULL, 181 NULL, 182 /* 84*/ NULL, 183 NULL, 184 NULL, 185 NULL, 186 NULL, 187 /* 89*/ NULL, 188 NULL, 189 NULL, 190 NULL, 191 NULL, 192 /* 94*/ NULL, 193 NULL, 194 NULL, 195 NULL, 196 NULL, 197 /*99*/ NULL, 198 NULL, 199 NULL, 200 NULL, 201 NULL, 202 /*104*/ NULL, 203 NULL, 204 NULL, 205 NULL, 206 NULL, 207 /*109*/ NULL, 208 NULL, 209 NULL, 210 NULL, 211 NULL, 212 /*114*/ NULL, 213 NULL, 214 NULL, 215 NULL, 216 NULL, 217 /*119*/ NULL, 218 NULL, 219 NULL, 220 NULL, 221 NULL, 222 /*124*/ NULL, 223 NULL, 224 NULL, 225 NULL, 226 NULL, 227 /*129*/ NULL, 228 NULL, 229 NULL, 230 NULL, 231 NULL, 232 /*134*/ NULL, 233 NULL, 234 NULL, 235 NULL, 236 NULL, 237 /*139*/NULL, 238 NULL, 239 NULL, 240 NULL, 241 NULL, 242 /*144*/NULL, 243 NULL, 244 NULL, 245 NULL 246 }; 247 248 /*MC 249 MATSCATTER - MATSCATTER = "scatter" - A matrix type that simply applies a VecScatterBegin/End() 250 251 Level: advanced 252 253 .seealso: `MatCreateScatter()`, `MatScatterSetVecScatter()`, `MatScatterGetVecScatter()` 254 255 M*/ 256 257 PETSC_EXTERN PetscErrorCode MatCreate_Scatter(Mat A) 258 { 259 Mat_Scatter *b; 260 261 PetscFunctionBegin; 262 PetscCall(PetscMemcpy(A->ops,&MatOps_Values,sizeof(struct _MatOps))); 263 PetscCall(PetscNewLog(A,&b)); 264 265 A->data = (void*)b; 266 267 PetscCall(PetscLayoutSetUp(A->rmap)); 268 PetscCall(PetscLayoutSetUp(A->cmap)); 269 270 A->assembled = PETSC_TRUE; 271 A->preallocated = PETSC_FALSE; 272 273 PetscCall(PetscObjectChangeTypeName((PetscObject)A,MATSCATTER)); 274 PetscFunctionReturn(0); 275 } 276 277 #include <petsc/private/sfimpl.h> 278 /*@C 279 MatCreateScatter - Creates a new matrix based on a VecScatter 280 281 Collective 282 283 Input Parameters: 284 + comm - MPI communicator 285 - scatter - a VecScatterContext 286 287 Output Parameter: 288 . A - the matrix 289 290 Level: intermediate 291 292 PETSc requires that matrices and vectors being used for certain 293 operations are partitioned accordingly. For example, when 294 creating a scatter matrix, A, that supports parallel matrix-vector 295 products using MatMult(A,x,y) the user should set the number 296 of local matrix rows to be the number of local elements of the 297 corresponding result vector, y. Note that this is information is 298 required for use of the matrix interface routines, even though 299 the scatter matrix may not actually be physically partitioned. 300 301 Developer Notes: This directly accesses information inside the VecScatter associated with the matrix-vector product 302 for this matrix. This is not desirable.. 303 304 .seealso: `MatScatterSetVecScatter()`, `MatScatterGetVecScatter()`, `MATSCATTER` 305 @*/ 306 PetscErrorCode MatCreateScatter(MPI_Comm comm,VecScatter scatter,Mat *A) 307 { 308 PetscFunctionBegin; 309 PetscCall(MatCreate(comm,A)); 310 PetscCall(MatSetSizes(*A,scatter->vscat.to_n,scatter->vscat.from_n,PETSC_DETERMINE,PETSC_DETERMINE)); 311 PetscCall(MatSetType(*A,MATSCATTER)); 312 PetscCall(MatScatterSetVecScatter(*A,scatter)); 313 PetscCall(MatSetUp(*A)); 314 PetscFunctionReturn(0); 315 } 316 317 /*@ 318 MatScatterSetVecScatter - sets that scatter that the matrix is to apply as its linear operator 319 320 Collective on Mat 321 322 Input Parameters: 323 + mat - the scatter matrix 324 - scatter - the scatter context create with VecScatterCreate() 325 326 Level: advanced 327 328 .seealso: `MatCreateScatter()`, `MATSCATTER` 329 @*/ 330 PetscErrorCode MatScatterSetVecScatter(Mat mat,VecScatter scatter) 331 { 332 Mat_Scatter *mscatter = (Mat_Scatter*)mat->data; 333 334 PetscFunctionBegin; 335 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 336 PetscValidHeaderSpecific(scatter,PETSCSF_CLASSID,2); 337 PetscCheckSameComm((PetscObject)scatter,2,(PetscObject)mat,1); 338 PetscCheck(mat->rmap->n == scatter->vscat.to_n,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Number of local rows in matrix %" PetscInt_FMT " not equal local scatter size %" PetscInt_FMT,mat->rmap->n,scatter->vscat.to_n); 339 PetscCheck(mat->cmap->n == scatter->vscat.from_n,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Number of local columns in matrix %" PetscInt_FMT " not equal local scatter size %" PetscInt_FMT,mat->cmap->n,scatter->vscat.from_n); 340 341 PetscCall(PetscObjectReference((PetscObject)scatter)); 342 PetscCall(VecScatterDestroy(&mscatter->scatter)); 343 344 mscatter->scatter = scatter; 345 PetscFunctionReturn(0); 346 } 347