1 2 #include <petsc/private/matimpl.h> /*I "petscmat.h" I*/ 3 4 PETSC_INTERN PetscErrorCode MatSetBlockSizes_Default(Mat mat,PetscInt rbs, PetscInt cbs) 5 { 6 PetscFunctionBegin; 7 if (!mat->preallocated) PetscFunctionReturn(0); 8 if (mat->rmap->bs > 0 && mat->rmap->bs != rbs) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Cannot change row block size %D to %D\n",mat->rmap->bs,rbs); 9 if (mat->cmap->bs > 0 && mat->cmap->bs != cbs) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Cannot change column block size %D to %D\n",mat->cmap->bs,cbs); 10 PetscFunctionReturn(0); 11 } 12 13 PETSC_INTERN PetscErrorCode MatShift_Basic(Mat Y,PetscScalar a) 14 { 15 PetscErrorCode ierr; 16 PetscInt i,start,end; 17 PetscScalar alpha = a; 18 PetscBool prevoption; 19 20 PetscFunctionBegin; 21 ierr = MatGetOption(Y,MAT_NO_OFF_PROC_ENTRIES,&prevoption);CHKERRQ(ierr); 22 ierr = MatSetOption(Y,MAT_NO_OFF_PROC_ENTRIES,PETSC_TRUE);CHKERRQ(ierr); 23 ierr = MatGetOwnershipRange(Y,&start,&end);CHKERRQ(ierr); 24 for (i=start; i<end; i++) { 25 if (i < Y->cmap->N) { 26 ierr = MatSetValues(Y,1,&i,1,&i,&alpha,ADD_VALUES);CHKERRQ(ierr); 27 } 28 } 29 ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 30 ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 31 ierr = MatSetOption(Y,MAT_NO_OFF_PROC_ENTRIES,prevoption);CHKERRQ(ierr); 32 PetscFunctionReturn(0); 33 } 34 35 /*@ 36 MatCreate - Creates a matrix where the type is determined 37 from either a call to MatSetType() or from the options database 38 with a call to MatSetFromOptions(). The default matrix type is 39 AIJ, using the routines MatCreateSeqAIJ() or MatCreateAIJ() 40 if you do not set a type in the options database. If you never 41 call MatSetType() or MatSetFromOptions() it will generate an 42 error when you try to use the matrix. 43 44 Collective 45 46 Input Parameter: 47 . comm - MPI communicator 48 49 Output Parameter: 50 . A - the matrix 51 52 Options Database Keys: 53 + -mat_type seqaij - AIJ type, uses MatCreateSeqAIJ() 54 . -mat_type mpiaij - AIJ type, uses MatCreateAIJ() 55 . -mat_type seqdense - dense type, uses MatCreateSeqDense() 56 . -mat_type mpidense - dense type, uses MatCreateDense() 57 . -mat_type seqbaij - block AIJ type, uses MatCreateSeqBAIJ() 58 - -mat_type mpibaij - block AIJ type, uses MatCreateBAIJ() 59 60 Even More Options Database Keys: 61 See the manpages for particular formats (e.g., MatCreateSeqAIJ()) 62 for additional format-specific options. 63 64 Level: beginner 65 66 .seealso: MatCreateSeqAIJ(), MatCreateAIJ(), 67 MatCreateSeqDense(), MatCreateDense(), 68 MatCreateSeqBAIJ(), MatCreateBAIJ(), 69 MatCreateSeqSBAIJ(), MatCreateSBAIJ(), 70 MatConvert() 71 @*/ 72 PetscErrorCode MatCreate(MPI_Comm comm,Mat *A) 73 { 74 Mat B; 75 PetscErrorCode ierr; 76 77 PetscFunctionBegin; 78 PetscValidPointer(A,2); 79 80 *A = NULL; 81 ierr = MatInitializePackage();CHKERRQ(ierr); 82 83 ierr = PetscHeaderCreate(B,MAT_CLASSID,"Mat","Matrix","Mat",comm,MatDestroy,MatView);CHKERRQ(ierr); 84 ierr = PetscLayoutCreate(comm,&B->rmap);CHKERRQ(ierr); 85 ierr = PetscLayoutCreate(comm,&B->cmap);CHKERRQ(ierr); 86 ierr = PetscStrallocpy(VECSTANDARD,&B->defaultvectype);CHKERRQ(ierr); 87 88 B->congruentlayouts = PETSC_DECIDE; 89 B->preallocated = PETSC_FALSE; 90 #if defined(PETSC_HAVE_DEVICE) 91 B->boundtocpu = PETSC_TRUE; 92 #endif 93 *A = B; 94 PetscFunctionReturn(0); 95 } 96 97 /*@ 98 MatSetErrorIfFailure - Causes Mat to generate an error, for example a zero pivot, is detected. 99 100 Logically Collective on Mat 101 102 Input Parameters: 103 + mat - matrix obtained from MatCreate() 104 - flg - PETSC_TRUE indicates you want the error generated 105 106 Level: advanced 107 108 .seealso: PCSetErrorIfFailure() 109 @*/ 110 PetscErrorCode MatSetErrorIfFailure(Mat mat,PetscBool flg) 111 { 112 PetscFunctionBegin; 113 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 114 PetscValidLogicalCollectiveBool(mat,flg,2); 115 mat->erroriffailure = flg; 116 PetscFunctionReturn(0); 117 } 118 119 /*@ 120 MatSetSizes - Sets the local and global sizes, and checks to determine compatibility 121 122 Collective on Mat 123 124 Input Parameters: 125 + A - the matrix 126 . m - number of local rows (or PETSC_DECIDE) 127 . n - number of local columns (or PETSC_DECIDE) 128 . M - number of global rows (or PETSC_DETERMINE) 129 - N - number of global columns (or PETSC_DETERMINE) 130 131 Notes: 132 m (n) and M (N) cannot be both PETSC_DECIDE 133 If one processor calls this with M (N) of PETSC_DECIDE then all processors must, otherwise the program will hang. 134 135 If PETSC_DECIDE is not used for the arguments 'm' and 'n', then the 136 user must ensure that they are chosen to be compatible with the 137 vectors. To do this, one first considers the matrix-vector product 138 'y = A x'. The 'm' that is used in the above routine must match the 139 local size used in the vector creation routine VecCreateMPI() for 'y'. 140 Likewise, the 'n' used must match that used as the local size in 141 VecCreateMPI() for 'x'. 142 143 You cannot change the sizes once they have been set. 144 145 The sizes must be set before MatSetUp() or MatXXXSetPreallocation() is called. 146 147 Level: beginner 148 149 .seealso: MatGetSize(), PetscSplitOwnership() 150 @*/ 151 PetscErrorCode MatSetSizes(Mat A, PetscInt m, PetscInt n, PetscInt M, PetscInt N) 152 { 153 PetscFunctionBegin; 154 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 155 PetscValidLogicalCollectiveInt(A,M,4); 156 PetscValidLogicalCollectiveInt(A,N,5); 157 if (M > 0 && m > M) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Local row size %D cannot be larger than global row size %D",m,M); 158 if (N > 0 && n > N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Local column size %D cannot be larger than global column size %D",n,N); 159 if ((A->rmap->n >= 0 && A->rmap->N >= 0) && (A->rmap->n != m || (M > 0 && A->rmap->N != M))) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot change/reset row sizes to %D local %D global after previously setting them to %D local %D global",m,M,A->rmap->n,A->rmap->N); 160 if ((A->cmap->n >= 0 && A->cmap->N >= 0) && (A->cmap->n != n || (N > 0 && A->cmap->N != N))) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot change/reset column sizes to %D local %D global after previously setting them to %D local %D global",n,N,A->cmap->n,A->cmap->N); 161 A->rmap->n = m; 162 A->cmap->n = n; 163 A->rmap->N = M > -1 ? M : A->rmap->N; 164 A->cmap->N = N > -1 ? N : A->cmap->N; 165 PetscFunctionReturn(0); 166 } 167 168 /*@ 169 MatSetFromOptions - Creates a matrix where the type is determined 170 from the options database. Generates a parallel MPI matrix if the 171 communicator has more than one processor. The default matrix type is 172 AIJ, using the routines MatCreateSeqAIJ() and MatCreateAIJ() if 173 you do not select a type in the options database. 174 175 Collective on Mat 176 177 Input Parameter: 178 . A - the matrix 179 180 Options Database Keys: 181 + -mat_type seqaij - AIJ type, uses MatCreateSeqAIJ() 182 . -mat_type mpiaij - AIJ type, uses MatCreateAIJ() 183 . -mat_type seqdense - dense type, uses MatCreateSeqDense() 184 . -mat_type mpidense - dense type, uses MatCreateDense() 185 . -mat_type seqbaij - block AIJ type, uses MatCreateSeqBAIJ() 186 - -mat_type mpibaij - block AIJ type, uses MatCreateBAIJ() 187 188 Even More Options Database Keys: 189 See the manpages for particular formats (e.g., MatCreateSeqAIJ()) 190 for additional format-specific options. 191 192 Level: beginner 193 194 .seealso: MatCreateSeqAIJ((), MatCreateAIJ(), 195 MatCreateSeqDense(), MatCreateDense(), 196 MatCreateSeqBAIJ(), MatCreateBAIJ(), 197 MatCreateSeqSBAIJ(), MatCreateSBAIJ(), 198 MatConvert() 199 @*/ 200 PetscErrorCode MatSetFromOptions(Mat B) 201 { 202 PetscErrorCode ierr; 203 const char *deft = MATAIJ; 204 char type[256]; 205 PetscBool flg,set; 206 PetscInt bind_below = 0; 207 208 PetscFunctionBegin; 209 PetscValidHeaderSpecific(B,MAT_CLASSID,1); 210 211 ierr = PetscObjectOptionsBegin((PetscObject)B);CHKERRQ(ierr); 212 213 if (B->rmap->bs < 0) { 214 PetscInt newbs = -1; 215 ierr = PetscOptionsInt("-mat_block_size","Set the blocksize used to store the matrix","MatSetBlockSize",newbs,&newbs,&flg);CHKERRQ(ierr); 216 if (flg) { 217 ierr = PetscLayoutSetBlockSize(B->rmap,newbs);CHKERRQ(ierr); 218 ierr = PetscLayoutSetBlockSize(B->cmap,newbs);CHKERRQ(ierr); 219 } 220 } 221 222 ierr = PetscOptionsFList("-mat_type","Matrix type","MatSetType",MatList,deft,type,256,&flg);CHKERRQ(ierr); 223 if (flg) { 224 ierr = MatSetType(B,type);CHKERRQ(ierr); 225 } else if (!((PetscObject)B)->type_name) { 226 ierr = MatSetType(B,deft);CHKERRQ(ierr); 227 } 228 229 ierr = PetscOptionsName("-mat_is_symmetric","Checks if mat is symmetric on MatAssemblyEnd()","MatIsSymmetric",&B->checksymmetryonassembly);CHKERRQ(ierr); 230 ierr = PetscOptionsReal("-mat_is_symmetric","Checks if mat is symmetric on MatAssemblyEnd()","MatIsSymmetric",B->checksymmetrytol,&B->checksymmetrytol,NULL);CHKERRQ(ierr); 231 ierr = PetscOptionsBool("-mat_null_space_test","Checks if provided null space is correct in MatAssemblyEnd()","MatSetNullSpaceTest",B->checknullspaceonassembly,&B->checknullspaceonassembly,NULL);CHKERRQ(ierr); 232 ierr = PetscOptionsBool("-mat_error_if_failure","Generate an error if an error occurs when factoring the matrix","MatSetErrorIfFailure",B->erroriffailure,&B->erroriffailure,NULL);CHKERRQ(ierr); 233 234 if (B->ops->setfromoptions) { 235 ierr = (*B->ops->setfromoptions)(PetscOptionsObject,B);CHKERRQ(ierr); 236 } 237 238 flg = PETSC_FALSE; 239 ierr = PetscOptionsBool("-mat_new_nonzero_location_err","Generate an error if new nonzeros are created in the matrix structure (useful to test preallocation)","MatSetOption",flg,&flg,&set);CHKERRQ(ierr); 240 if (set) {ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,flg);CHKERRQ(ierr);} 241 flg = PETSC_FALSE; 242 ierr = PetscOptionsBool("-mat_new_nonzero_allocation_err","Generate an error if new nonzeros are allocated in the matrix structure (useful to test preallocation)","MatSetOption",flg,&flg,&set);CHKERRQ(ierr); 243 if (set) {ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,flg);CHKERRQ(ierr);} 244 flg = PETSC_FALSE; 245 ierr = PetscOptionsBool("-mat_ignore_zero_entries","For AIJ/IS matrices this will stop zero values from creating a zero location in the matrix","MatSetOption",flg,&flg,&set);CHKERRQ(ierr); 246 if (set) {ierr = MatSetOption(B,MAT_IGNORE_ZERO_ENTRIES,flg);CHKERRQ(ierr);} 247 248 flg = PETSC_FALSE; 249 ierr = PetscOptionsBool("-mat_form_explicit_transpose","Hint to form an explicit transpose for operations like MatMultTranspose","MatSetOption",flg,&flg,&set);CHKERRQ(ierr); 250 if (set) {ierr = MatSetOption(B,MAT_FORM_EXPLICIT_TRANSPOSE,flg);CHKERRQ(ierr);} 251 252 /* Bind to CPU if below a user-specified size threshold. 253 * This perhaps belongs in the options for the GPU Mat types, but MatBindToCPU() does nothing when called on non-GPU types, 254 * and putting it here makes is more maintainable than duplicating this for all. */ 255 ierr = PetscOptionsInt("-mat_bind_below","Set the size threshold (in local rows) below which the Mat is bound to the CPU","MatBindToCPU",bind_below,&bind_below,&flg);CHKERRQ(ierr); 256 if (flg && B->rmap->n < bind_below) { 257 ierr = MatBindToCPU(B,PETSC_TRUE);CHKERRQ(ierr); 258 } 259 260 /* process any options handlers added with PetscObjectAddOptionsHandler() */ 261 ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)B);CHKERRQ(ierr); 262 ierr = PetscOptionsEnd();CHKERRQ(ierr); 263 PetscFunctionReturn(0); 264 } 265 266 /*@C 267 MatXAIJSetPreallocation - set preallocation for serial and parallel AIJ, BAIJ, and SBAIJ matrices and their unassembled versions. 268 269 Collective on Mat 270 271 Input Parameters: 272 + A - matrix being preallocated 273 . bs - block size 274 . dnnz - number of nonzero column blocks per block row of diagonal part of parallel matrix 275 . onnz - number of nonzero column blocks per block row of off-diagonal part of parallel matrix 276 . dnnzu - number of nonzero column blocks per block row of upper-triangular part of diagonal part of parallel matrix 277 - onnzu - number of nonzero column blocks per block row of upper-triangular part of off-diagonal part of parallel matrix 278 279 Level: beginner 280 281 .seealso: MatSeqAIJSetPreallocation(), MatMPIAIJSetPreallocation(), MatSeqBAIJSetPreallocation(), MatMPIBAIJSetPreallocation(), MatSeqSBAIJSetPreallocation(), MatMPISBAIJSetPreallocation(), 282 PetscSplitOwnership() 283 @*/ 284 PetscErrorCode MatXAIJSetPreallocation(Mat A,PetscInt bs,const PetscInt dnnz[],const PetscInt onnz[],const PetscInt dnnzu[],const PetscInt onnzu[]) 285 { 286 PetscErrorCode ierr; 287 PetscInt cbs; 288 void (*aij)(void); 289 void (*is)(void); 290 void (*hyp)(void) = NULL; 291 292 PetscFunctionBegin; 293 if (bs != PETSC_DECIDE) { /* don't mess with an already set block size */ 294 ierr = MatSetBlockSize(A,bs);CHKERRQ(ierr); 295 } 296 ierr = PetscLayoutSetUp(A->rmap);CHKERRQ(ierr); 297 ierr = PetscLayoutSetUp(A->cmap);CHKERRQ(ierr); 298 ierr = MatGetBlockSizes(A,&bs,&cbs);CHKERRQ(ierr); 299 /* these routines assumes bs == cbs, this should be checked somehow */ 300 ierr = MatSeqBAIJSetPreallocation(A,bs,0,dnnz);CHKERRQ(ierr); 301 ierr = MatMPIBAIJSetPreallocation(A,bs,0,dnnz,0,onnz);CHKERRQ(ierr); 302 ierr = MatSeqSBAIJSetPreallocation(A,bs,0,dnnzu);CHKERRQ(ierr); 303 ierr = MatMPISBAIJSetPreallocation(A,bs,0,dnnzu,0,onnzu);CHKERRQ(ierr); 304 /* 305 In general, we have to do extra work to preallocate for scalar (AIJ) or unassembled (IS) matrices so we check whether it will do any 306 good before going on with it. 307 */ 308 ierr = PetscObjectQueryFunction((PetscObject)A,"MatMPIAIJSetPreallocation_C",&aij);CHKERRQ(ierr); 309 ierr = PetscObjectQueryFunction((PetscObject)A,"MatISSetPreallocation_C",&is);CHKERRQ(ierr); 310 #if defined(PETSC_HAVE_HYPRE) 311 ierr = PetscObjectQueryFunction((PetscObject)A,"MatHYPRESetPreallocation_C",&hyp);CHKERRQ(ierr); 312 #endif 313 if (!aij && !is && !hyp) { 314 ierr = PetscObjectQueryFunction((PetscObject)A,"MatSeqAIJSetPreallocation_C",&aij);CHKERRQ(ierr); 315 } 316 if (aij || is || hyp) { 317 if (bs == cbs && bs == 1) { 318 ierr = MatSeqAIJSetPreallocation(A,0,dnnz);CHKERRQ(ierr); 319 ierr = MatMPIAIJSetPreallocation(A,0,dnnz,0,onnz);CHKERRQ(ierr); 320 ierr = MatISSetPreallocation(A,0,dnnz,0,onnz);CHKERRQ(ierr); 321 #if defined(PETSC_HAVE_HYPRE) 322 ierr = MatHYPRESetPreallocation(A,0,dnnz,0,onnz);CHKERRQ(ierr); 323 #endif 324 } else { /* Convert block-row precallocation to scalar-row */ 325 PetscInt i,m,*sdnnz,*sonnz; 326 ierr = MatGetLocalSize(A,&m,NULL);CHKERRQ(ierr); 327 ierr = PetscMalloc2((!!dnnz)*m,&sdnnz,(!!onnz)*m,&sonnz);CHKERRQ(ierr); 328 for (i=0; i<m; i++) { 329 if (dnnz) sdnnz[i] = dnnz[i/bs] * cbs; 330 if (onnz) sonnz[i] = onnz[i/bs] * cbs; 331 } 332 ierr = MatSeqAIJSetPreallocation(A,0,dnnz ? sdnnz : NULL);CHKERRQ(ierr); 333 ierr = MatMPIAIJSetPreallocation(A,0,dnnz ? sdnnz : NULL,0,onnz ? sonnz : NULL);CHKERRQ(ierr); 334 ierr = MatISSetPreallocation(A,0,dnnz ? sdnnz : NULL,0,onnz ? sonnz : NULL);CHKERRQ(ierr); 335 #if defined(PETSC_HAVE_HYPRE) 336 ierr = MatHYPRESetPreallocation(A,0,dnnz ? sdnnz : NULL,0,onnz ? sonnz : NULL);CHKERRQ(ierr); 337 #endif 338 ierr = PetscFree2(sdnnz,sonnz);CHKERRQ(ierr); 339 } 340 } 341 PetscFunctionReturn(0); 342 } 343 344 /* 345 Merges some information from Cs header to A; the C object is then destroyed 346 347 This is somewhat different from MatHeaderReplace() it would be nice to merge the code 348 */ 349 PetscErrorCode MatHeaderMerge(Mat A,Mat *C) 350 { 351 PetscErrorCode ierr; 352 PetscInt refct; 353 PetscOps Abops; 354 struct _MatOps Aops; 355 char *mtype,*mname,*mprefix; 356 Mat_Product *product; 357 358 PetscFunctionBegin; 359 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 360 PetscValidHeaderSpecific(*C,MAT_CLASSID,2); 361 if (A == *C) PetscFunctionReturn(0); 362 PetscCheckSameComm(A,1,*C,2); 363 /* save the parts of A we need */ 364 Abops = ((PetscObject)A)->bops[0]; 365 Aops = A->ops[0]; 366 refct = ((PetscObject)A)->refct; 367 mtype = ((PetscObject)A)->type_name; 368 mname = ((PetscObject)A)->name; 369 mprefix = ((PetscObject)A)->prefix; 370 product = A->product; 371 372 /* zero these so the destroy below does not free them */ 373 ((PetscObject)A)->type_name = NULL; 374 ((PetscObject)A)->name = NULL; 375 376 /* free all the interior data structures from mat */ 377 ierr = (*A->ops->destroy)(A);CHKERRQ(ierr); 378 379 ierr = PetscFree(A->defaultvectype);CHKERRQ(ierr); 380 ierr = PetscLayoutDestroy(&A->rmap);CHKERRQ(ierr); 381 ierr = PetscLayoutDestroy(&A->cmap);CHKERRQ(ierr); 382 ierr = PetscFunctionListDestroy(&((PetscObject)A)->qlist);CHKERRQ(ierr); 383 ierr = PetscObjectListDestroy(&((PetscObject)A)->olist);CHKERRQ(ierr); 384 385 /* copy C over to A */ 386 ierr = PetscMemcpy(A,*C,sizeof(struct _p_Mat));CHKERRQ(ierr); 387 388 /* return the parts of A we saved */ 389 ((PetscObject)A)->bops[0] = Abops; 390 A->ops[0] = Aops; 391 ((PetscObject)A)->refct = refct; 392 ((PetscObject)A)->type_name = mtype; 393 ((PetscObject)A)->name = mname; 394 ((PetscObject)A)->prefix = mprefix; 395 A->product = product; 396 397 /* since these two are copied into A we do not want them destroyed in C */ 398 ((PetscObject)*C)->qlist = NULL; 399 ((PetscObject)*C)->olist = NULL; 400 401 ierr = PetscHeaderDestroy(C);CHKERRQ(ierr); 402 PetscFunctionReturn(0); 403 } 404 /* 405 Replace A's header with that of C; the C object is then destroyed 406 407 This is essentially code moved from MatDestroy() 408 409 This is somewhat different from MatHeaderMerge() it would be nice to merge the code 410 411 Used in DM hence is declared PETSC_EXTERN 412 */ 413 PETSC_EXTERN PetscErrorCode MatHeaderReplace(Mat A,Mat *C) 414 { 415 PetscErrorCode ierr; 416 PetscInt refct; 417 PetscObjectState state; 418 struct _p_Mat buffer; 419 MatStencilInfo stencil; 420 421 PetscFunctionBegin; 422 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 423 PetscValidHeaderSpecific(*C,MAT_CLASSID,2); 424 if (A == *C) PetscFunctionReturn(0); 425 PetscCheckSameComm(A,1,*C,2); 426 if (((PetscObject)*C)->refct != 1) SETERRQ1(PetscObjectComm((PetscObject)C),PETSC_ERR_ARG_WRONGSTATE,"Object C has refct %D > 1, would leave hanging reference",((PetscObject)*C)->refct); 427 428 /* swap C and A */ 429 refct = ((PetscObject)A)->refct; 430 state = ((PetscObject)A)->state; 431 stencil = A->stencil; 432 ierr = PetscMemcpy(&buffer,A,sizeof(struct _p_Mat));CHKERRQ(ierr); 433 ierr = PetscMemcpy(A,*C,sizeof(struct _p_Mat));CHKERRQ(ierr); 434 ierr = PetscMemcpy(*C,&buffer,sizeof(struct _p_Mat));CHKERRQ(ierr); 435 ((PetscObject)A)->refct = refct; 436 ((PetscObject)A)->state = state + 1; 437 A->stencil = stencil; 438 439 ((PetscObject)*C)->refct = 1; 440 ierr = MatShellSetOperation(*C,MATOP_DESTROY,(void(*)(void))NULL);CHKERRQ(ierr); 441 ierr = MatDestroy(C);CHKERRQ(ierr); 442 PetscFunctionReturn(0); 443 } 444 445 /*@ 446 MatBindToCPU - marks a matrix to temporarily stay on the CPU and perform computations on the CPU 447 448 Logically collective on Mat 449 450 Input Parameters: 451 + A - the matrix 452 - flg - bind to the CPU if value of PETSC_TRUE 453 454 Level: intermediate 455 456 .seealso: MatBoundToCPU() 457 @*/ 458 PetscErrorCode MatBindToCPU(Mat A,PetscBool flg) 459 { 460 PetscFunctionBegin; 461 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 462 PetscValidLogicalCollectiveBool(A,flg,2); 463 #if defined(PETSC_HAVE_DEVICE) 464 if (A->boundtocpu == flg) PetscFunctionReturn(0); 465 A->boundtocpu = flg; 466 if (A->ops->bindtocpu) { 467 PetscErrorCode ierr; 468 ierr = (*A->ops->bindtocpu)(A,flg);CHKERRQ(ierr); 469 } 470 #endif 471 PetscFunctionReturn(0); 472 } 473 474 /*@ 475 MatBoundToCPU - query if a matrix is bound to the CPU 476 477 Input Parameter: 478 . A - the matrix 479 480 Output Parameter: 481 . flg - the logical flag 482 483 Level: intermediate 484 485 .seealso: MatBindToCPU() 486 @*/ 487 PetscErrorCode MatBoundToCPU(Mat A,PetscBool *flg) 488 { 489 PetscFunctionBegin; 490 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 491 PetscValidPointer(flg,2); 492 #if defined(PETSC_HAVE_DEVICE) 493 *flg = A->boundtocpu; 494 #else 495 *flg = PETSC_TRUE; 496 #endif 497 PetscFunctionReturn(0); 498 } 499 500 PetscErrorCode MatSetValuesCOO_Basic(Mat A,const PetscScalar coo_v[],InsertMode imode) 501 { 502 IS is_coo_i,is_coo_j; 503 const PetscInt *coo_i,*coo_j; 504 PetscInt n,n_i,n_j; 505 PetscScalar zero = 0.; 506 PetscErrorCode ierr; 507 508 PetscFunctionBegin; 509 ierr = PetscObjectQuery((PetscObject)A,"__PETSc_coo_i",(PetscObject*)&is_coo_i);CHKERRQ(ierr); 510 ierr = PetscObjectQuery((PetscObject)A,"__PETSc_coo_j",(PetscObject*)&is_coo_j);CHKERRQ(ierr); 511 if (!is_coo_i) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_COR,"Missing coo_i IS"); 512 if (!is_coo_j) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_COR,"Missing coo_j IS"); 513 ierr = ISGetLocalSize(is_coo_i,&n_i);CHKERRQ(ierr); 514 ierr = ISGetLocalSize(is_coo_j,&n_j);CHKERRQ(ierr); 515 if (n_i != n_j) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_COR,"Wrong local size %D != %D",n_i,n_j); 516 ierr = ISGetIndices(is_coo_i,&coo_i);CHKERRQ(ierr); 517 ierr = ISGetIndices(is_coo_j,&coo_j);CHKERRQ(ierr); 518 if (imode != ADD_VALUES) { 519 ierr = MatZeroEntries(A);CHKERRQ(ierr); 520 } 521 for (n = 0; n < n_i; n++) { 522 ierr = MatSetValue(A,coo_i[n],coo_j[n],coo_v ? coo_v[n] : zero,ADD_VALUES);CHKERRQ(ierr); 523 } 524 ierr = ISRestoreIndices(is_coo_i,&coo_i);CHKERRQ(ierr); 525 ierr = ISRestoreIndices(is_coo_j,&coo_j);CHKERRQ(ierr); 526 ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 527 ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 528 PetscFunctionReturn(0); 529 } 530 531 PetscErrorCode MatSetPreallocationCOO_Basic(Mat A,PetscInt ncoo,const PetscInt coo_i[],const PetscInt coo_j[]) 532 { 533 Mat preallocator; 534 IS is_coo_i,is_coo_j; 535 PetscScalar zero = 0.0; 536 PetscInt n; 537 PetscErrorCode ierr; 538 539 PetscFunctionBegin; 540 ierr = PetscLayoutSetUp(A->rmap);CHKERRQ(ierr); 541 ierr = PetscLayoutSetUp(A->cmap);CHKERRQ(ierr); 542 ierr = MatCreate(PetscObjectComm((PetscObject)A),&preallocator);CHKERRQ(ierr); 543 ierr = MatSetType(preallocator,MATPREALLOCATOR);CHKERRQ(ierr); 544 ierr = MatSetSizes(preallocator,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);CHKERRQ(ierr); 545 ierr = MatSetLayouts(preallocator,A->rmap,A->cmap);CHKERRQ(ierr); 546 ierr = MatSetUp(preallocator);CHKERRQ(ierr); 547 for (n = 0; n < ncoo; n++) { 548 ierr = MatSetValue(preallocator,coo_i[n],coo_j[n],zero,INSERT_VALUES);CHKERRQ(ierr); 549 } 550 ierr = MatAssemblyBegin(preallocator,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 551 ierr = MatAssemblyEnd(preallocator,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 552 ierr = MatPreallocatorPreallocate(preallocator,PETSC_TRUE,A);CHKERRQ(ierr); 553 ierr = MatDestroy(&preallocator);CHKERRQ(ierr); 554 ierr = ISCreateGeneral(PETSC_COMM_SELF,ncoo,coo_i,PETSC_COPY_VALUES,&is_coo_i);CHKERRQ(ierr); 555 ierr = ISCreateGeneral(PETSC_COMM_SELF,ncoo,coo_j,PETSC_COPY_VALUES,&is_coo_j);CHKERRQ(ierr); 556 ierr = PetscObjectCompose((PetscObject)A,"__PETSc_coo_i",(PetscObject)is_coo_i);CHKERRQ(ierr); 557 ierr = PetscObjectCompose((PetscObject)A,"__PETSc_coo_j",(PetscObject)is_coo_j);CHKERRQ(ierr); 558 ierr = ISDestroy(&is_coo_i);CHKERRQ(ierr); 559 ierr = ISDestroy(&is_coo_j);CHKERRQ(ierr); 560 PetscFunctionReturn(0); 561 } 562 563 /*@ 564 MatSetPreallocationCOO - set preallocation for matrices using a coordinate format of the entries 565 566 Collective on Mat 567 568 Input Parameters: 569 + A - matrix being preallocated 570 . ncoo - number of entries in the locally owned part of the parallel matrix 571 . coo_i - row indices 572 - coo_j - column indices 573 574 Level: beginner 575 576 Notes: Entries can be repeated, see MatSetValuesCOO(). Currently optimized for cuSPARSE matrices only. 577 578 .seealso: MatSetValuesCOO(), MatSeqAIJSetPreallocation(), MatMPIAIJSetPreallocation(), MatSeqBAIJSetPreallocation(), MatMPIBAIJSetPreallocation(), MatSeqSBAIJSetPreallocation(), MatMPISBAIJSetPreallocation() 579 @*/ 580 PetscErrorCode MatSetPreallocationCOO(Mat A,PetscInt ncoo,const PetscInt coo_i[],const PetscInt coo_j[]) 581 { 582 PetscErrorCode (*f)(Mat,PetscInt,const PetscInt[],const PetscInt[]) = NULL; 583 PetscErrorCode ierr; 584 585 PetscFunctionBegin; 586 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 587 PetscValidType(A,1); 588 if (ncoo) PetscValidIntPointer(coo_i,3); 589 if (ncoo) PetscValidIntPointer(coo_j,4); 590 ierr = PetscLayoutSetUp(A->rmap);CHKERRQ(ierr); 591 ierr = PetscLayoutSetUp(A->cmap);CHKERRQ(ierr); 592 if (PetscDefined(USE_DEBUG)) { 593 PetscInt i; 594 for (i = 0; i < ncoo; i++) { 595 if (coo_i[i] < A->rmap->rstart || coo_i[i] >= A->rmap->rend) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_USER,"Invalid row index %D! Must be in [%D,%D)",coo_i[i],A->rmap->rstart,A->rmap->rend); 596 if (coo_j[i] < 0 || coo_j[i] >= A->cmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"Invalid col index %D! Must be in [0,%D)",coo_j[i],A->cmap->N); 597 } 598 } 599 ierr = PetscObjectQueryFunction((PetscObject)A,"MatSetPreallocationCOO_C",&f);CHKERRQ(ierr); 600 ierr = PetscLogEventBegin(MAT_PreallCOO,A,0,0,0);CHKERRQ(ierr); 601 if (f) { 602 ierr = (*f)(A,ncoo,coo_i,coo_j);CHKERRQ(ierr); 603 } else { /* allow fallback, very slow */ 604 ierr = MatSetPreallocationCOO_Basic(A,ncoo,coo_i,coo_j);CHKERRQ(ierr); 605 } 606 ierr = PetscLogEventEnd(MAT_PreallCOO,A,0,0,0);CHKERRQ(ierr); 607 PetscFunctionReturn(0); 608 } 609 610 /*@ 611 MatSetValuesCOO - set values at once in a matrix preallocated using MatSetPreallocationCOO() 612 613 Collective on Mat 614 615 Input Parameters: 616 + A - matrix being preallocated 617 . coo_v - the matrix values (can be NULL) 618 - imode - the insert mode 619 620 Level: beginner 621 622 Notes: The values must follow the order of the indices prescribed with MatSetPreallocationCOO(). 623 When repeated entries are specified in the COO indices the coo_v values are first properly summed. 624 The imode flag indicates if coo_v must be added to the current values of the matrix (ADD_VALUES) or overwritten (INSERT_VALUES). 625 Currently optimized for cuSPARSE matrices only. 626 Passing coo_v == NULL is equivalent to passing an array of zeros. 627 628 .seealso: MatSetPreallocationCOO(), InsertMode, INSERT_VALUES, ADD_VALUES 629 @*/ 630 PetscErrorCode MatSetValuesCOO(Mat A, const PetscScalar coo_v[], InsertMode imode) 631 { 632 PetscErrorCode (*f)(Mat,const PetscScalar[],InsertMode) = NULL; 633 PetscErrorCode ierr; 634 635 PetscFunctionBegin; 636 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 637 PetscValidType(A,1); 638 MatCheckPreallocated(A,1); 639 PetscValidLogicalCollectiveEnum(A,imode,3); 640 ierr = PetscObjectQueryFunction((PetscObject)A,"MatSetValuesCOO_C",&f);CHKERRQ(ierr); 641 ierr = PetscLogEventBegin(MAT_SetVCOO,A,0,0,0);CHKERRQ(ierr); 642 if (f) { 643 ierr = (*f)(A,coo_v,imode);CHKERRQ(ierr); 644 } else { /* allow fallback */ 645 ierr = MatSetValuesCOO_Basic(A,coo_v,imode);CHKERRQ(ierr); 646 } 647 ierr = PetscLogEventEnd(MAT_SetVCOO,A,0,0,0);CHKERRQ(ierr); 648 ierr = PetscObjectStateIncrease((PetscObject)A);CHKERRQ(ierr); 649 PetscFunctionReturn(0); 650 } 651 652 /*@ 653 MatSetBindingPropagates - Sets whether the state of being bound to the CPU for a GPU matrix type propagates to child and some other associated objects 654 655 Input Parameters: 656 + A - the matrix 657 - flg - flag indicating whether the boundtocpu flag should be propagated 658 659 Level: developer 660 661 Notes: 662 If the value of flg is set to true, the following will occur: 663 664 MatCreateSubMatrices() and MatCreateRedundantMatrix() will bind created matrices to CPU if the input matrix is bound to the CPU. 665 MatCreateVecs() will bind created vectors to CPU if the input matrix is bound to the CPU. 666 The bindingpropagates flag itself is also propagated by the above routines. 667 668 Developer Notes: 669 If the fine-scale DMDA has the -dm_bind_below option set to true, then DMCreateInterpolationScale() calls MatSetBindingPropagates() 670 on the restriction/interpolation operator to set the bindingpropagates flag to true. 671 672 .seealso: VecSetBindingPropagates(), MatGetBindingPropagates() 673 @*/ 674 PetscErrorCode MatSetBindingPropagates(Mat A,PetscBool flg) 675 { 676 PetscFunctionBegin; 677 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 678 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 679 A->bindingpropagates = flg; 680 #endif 681 PetscFunctionReturn(0); 682 } 683 684 /*@ 685 MatGetBindingPropagates - Gets whether the state of being bound to the CPU for a GPU matrix type propagates to child and some other associated objects 686 687 Input Parameter: 688 . A - the matrix 689 690 Output Parameter: 691 . flg - flag indicating whether the boundtocpu flag will be propagated 692 693 Level: developer 694 695 .seealso: MatSetBindingPropagates() 696 @*/ 697 PetscErrorCode MatGetBindingPropagates(Mat A,PetscBool *flg) 698 { 699 PetscFunctionBegin; 700 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 701 PetscValidBoolPointer(flg,2); 702 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 703 *flg = A->bindingpropagates; 704 #else 705 *flg = PETSC_FALSE; 706 #endif 707 PetscFunctionReturn(0); 708 } 709