xref: /petsc/src/mat/utils/gcreate.c (revision 87699089ef18ff85054d6b69ea9ff7a12dee57d7)
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   PetscObjectState state;
358 
359   PetscFunctionBegin;
360   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
361   PetscValidHeaderSpecific(*C,MAT_CLASSID,2);
362   if (A == *C) PetscFunctionReturn(0);
363   PetscCheckSameComm(A,1,*C,2);
364   /* save the parts of A we need */
365   Abops = ((PetscObject)A)->bops[0];
366   Aops  = A->ops[0];
367   refct = ((PetscObject)A)->refct;
368   mtype = ((PetscObject)A)->type_name;
369   mname = ((PetscObject)A)->name;
370   state = ((PetscObject)A)->state;
371   mprefix = ((PetscObject)A)->prefix;
372   product = A->product;
373 
374   /* zero these so the destroy below does not free them */
375   ((PetscObject)A)->type_name = NULL;
376   ((PetscObject)A)->name      = NULL;
377 
378   /* free all the interior data structures from mat */
379   ierr = (*A->ops->destroy)(A);CHKERRQ(ierr);
380 
381   ierr = PetscFree(A->defaultvectype);CHKERRQ(ierr);
382   ierr = PetscLayoutDestroy(&A->rmap);CHKERRQ(ierr);
383   ierr = PetscLayoutDestroy(&A->cmap);CHKERRQ(ierr);
384   ierr = PetscFunctionListDestroy(&((PetscObject)A)->qlist);CHKERRQ(ierr);
385   ierr = PetscObjectListDestroy(&((PetscObject)A)->olist);CHKERRQ(ierr);
386   ierr = PetscComposedQuantitiesDestroy((PetscObject)A);CHKERRQ(ierr);
387 
388   /* copy C over to A */
389   ierr = PetscMemcpy(A,*C,sizeof(struct _p_Mat));CHKERRQ(ierr);
390 
391   /* return the parts of A we saved */
392   ((PetscObject)A)->bops[0]   = Abops;
393   A->ops[0]                   = Aops;
394   ((PetscObject)A)->refct     = refct;
395   ((PetscObject)A)->type_name = mtype;
396   ((PetscObject)A)->name      = mname;
397   ((PetscObject)A)->prefix    = mprefix;
398   ((PetscObject)A)->state     = state + 1;
399   A->product                  = product;
400 
401   /* since these two are copied into A we do not want them destroyed in C */
402   ((PetscObject)*C)->qlist = NULL;
403   ((PetscObject)*C)->olist = NULL;
404 
405   ierr = PetscHeaderDestroy(C);CHKERRQ(ierr);
406   PetscFunctionReturn(0);
407 }
408 /*
409         Replace A's header with that of C; the C object is then destroyed
410 
411         This is essentially code moved from MatDestroy()
412 
413         This is somewhat different from MatHeaderMerge() it would be nice to merge the code
414 
415         Used in DM hence is declared PETSC_EXTERN
416 */
417 PETSC_EXTERN PetscErrorCode MatHeaderReplace(Mat A,Mat *C)
418 {
419   PetscErrorCode   ierr;
420   PetscInt         refct;
421   PetscObjectState state;
422   struct _p_Mat    buffer;
423   MatStencilInfo   stencil;
424 
425   PetscFunctionBegin;
426   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
427   PetscValidHeaderSpecific(*C,MAT_CLASSID,2);
428   if (A == *C) PetscFunctionReturn(0);
429   PetscCheckSameComm(A,1,*C,2);
430   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);
431 
432   /* swap C and A */
433   refct   = ((PetscObject)A)->refct;
434   state   = ((PetscObject)A)->state;
435   stencil = A->stencil;
436   ierr  = PetscMemcpy(&buffer,A,sizeof(struct _p_Mat));CHKERRQ(ierr);
437   ierr  = PetscMemcpy(A,*C,sizeof(struct _p_Mat));CHKERRQ(ierr);
438   ierr  = PetscMemcpy(*C,&buffer,sizeof(struct _p_Mat));CHKERRQ(ierr);
439   ((PetscObject)A)->refct   = refct;
440   ((PetscObject)A)->state   = state + 1;
441   A->stencil                = stencil;
442 
443   ((PetscObject)*C)->refct = 1;
444   ierr = MatShellSetOperation(*C,MATOP_DESTROY,(void(*)(void))NULL);CHKERRQ(ierr);
445   ierr = MatDestroy(C);CHKERRQ(ierr);
446   PetscFunctionReturn(0);
447 }
448 
449 /*@
450      MatBindToCPU - marks a matrix to temporarily stay on the CPU and perform computations on the CPU
451 
452    Logically collective on Mat
453 
454    Input Parameters:
455 +   A - the matrix
456 -   flg - bind to the CPU if value of PETSC_TRUE
457 
458    Level: intermediate
459 
460 .seealso: MatBoundToCPU()
461 @*/
462 PetscErrorCode MatBindToCPU(Mat A,PetscBool flg)
463 {
464   PetscFunctionBegin;
465   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
466   PetscValidLogicalCollectiveBool(A,flg,2);
467 #if defined(PETSC_HAVE_DEVICE)
468   if (A->boundtocpu == flg) PetscFunctionReturn(0);
469   A->boundtocpu = flg;
470   if (A->ops->bindtocpu) {
471     PetscErrorCode ierr;
472     ierr = (*A->ops->bindtocpu)(A,flg);CHKERRQ(ierr);
473   }
474 #endif
475   PetscFunctionReturn(0);
476 }
477 
478 /*@
479      MatBoundToCPU - query if a matrix is bound to the CPU
480 
481    Input Parameter:
482 .   A - the matrix
483 
484    Output Parameter:
485 .   flg - the logical flag
486 
487    Level: intermediate
488 
489 .seealso: MatBindToCPU()
490 @*/
491 PetscErrorCode MatBoundToCPU(Mat A,PetscBool *flg)
492 {
493   PetscFunctionBegin;
494   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
495   PetscValidPointer(flg,2);
496 #if defined(PETSC_HAVE_DEVICE)
497   *flg = A->boundtocpu;
498 #else
499   *flg = PETSC_TRUE;
500 #endif
501   PetscFunctionReturn(0);
502 }
503 
504 PetscErrorCode MatSetValuesCOO_Basic(Mat A,const PetscScalar coo_v[],InsertMode imode)
505 {
506   IS             is_coo_i,is_coo_j;
507   const PetscInt *coo_i,*coo_j;
508   PetscInt       n,n_i,n_j;
509   PetscScalar    zero = 0.;
510   PetscErrorCode ierr;
511 
512   PetscFunctionBegin;
513   ierr = PetscObjectQuery((PetscObject)A,"__PETSc_coo_i",(PetscObject*)&is_coo_i);CHKERRQ(ierr);
514   ierr = PetscObjectQuery((PetscObject)A,"__PETSc_coo_j",(PetscObject*)&is_coo_j);CHKERRQ(ierr);
515   if (!is_coo_i) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_COR,"Missing coo_i IS");
516   if (!is_coo_j) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_COR,"Missing coo_j IS");
517   ierr = ISGetLocalSize(is_coo_i,&n_i);CHKERRQ(ierr);
518   ierr = ISGetLocalSize(is_coo_j,&n_j);CHKERRQ(ierr);
519   if (n_i != n_j)  SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_COR,"Wrong local size %D != %D",n_i,n_j);
520   ierr = ISGetIndices(is_coo_i,&coo_i);CHKERRQ(ierr);
521   ierr = ISGetIndices(is_coo_j,&coo_j);CHKERRQ(ierr);
522   if (imode != ADD_VALUES) {
523     ierr = MatZeroEntries(A);CHKERRQ(ierr);
524   }
525   for (n = 0; n < n_i; n++) {
526     ierr = MatSetValue(A,coo_i[n],coo_j[n],coo_v ? coo_v[n] : zero,ADD_VALUES);CHKERRQ(ierr);
527   }
528   ierr = ISRestoreIndices(is_coo_i,&coo_i);CHKERRQ(ierr);
529   ierr = ISRestoreIndices(is_coo_j,&coo_j);CHKERRQ(ierr);
530   ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
531   ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
532   PetscFunctionReturn(0);
533 }
534 
535 PetscErrorCode MatSetPreallocationCOO_Basic(Mat A,PetscInt ncoo,const PetscInt coo_i[],const PetscInt coo_j[])
536 {
537   Mat            preallocator;
538   IS             is_coo_i,is_coo_j;
539   PetscScalar    zero = 0.0;
540   PetscInt       n;
541   PetscErrorCode ierr;
542 
543   PetscFunctionBegin;
544   ierr = PetscLayoutSetUp(A->rmap);CHKERRQ(ierr);
545   ierr = PetscLayoutSetUp(A->cmap);CHKERRQ(ierr);
546   ierr = MatCreate(PetscObjectComm((PetscObject)A),&preallocator);CHKERRQ(ierr);
547   ierr = MatSetType(preallocator,MATPREALLOCATOR);CHKERRQ(ierr);
548   ierr = MatSetSizes(preallocator,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);CHKERRQ(ierr);
549   ierr = MatSetLayouts(preallocator,A->rmap,A->cmap);CHKERRQ(ierr);
550   ierr = MatSetUp(preallocator);CHKERRQ(ierr);
551   for (n = 0; n < ncoo; n++) {
552     ierr = MatSetValue(preallocator,coo_i[n],coo_j[n],zero,INSERT_VALUES);CHKERRQ(ierr);
553   }
554   ierr = MatAssemblyBegin(preallocator,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
555   ierr = MatAssemblyEnd(preallocator,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
556   ierr = MatPreallocatorPreallocate(preallocator,PETSC_TRUE,A);CHKERRQ(ierr);
557   ierr = MatDestroy(&preallocator);CHKERRQ(ierr);
558   ierr = ISCreateGeneral(PETSC_COMM_SELF,ncoo,coo_i,PETSC_COPY_VALUES,&is_coo_i);CHKERRQ(ierr);
559   ierr = ISCreateGeneral(PETSC_COMM_SELF,ncoo,coo_j,PETSC_COPY_VALUES,&is_coo_j);CHKERRQ(ierr);
560   ierr = PetscObjectCompose((PetscObject)A,"__PETSc_coo_i",(PetscObject)is_coo_i);CHKERRQ(ierr);
561   ierr = PetscObjectCompose((PetscObject)A,"__PETSc_coo_j",(PetscObject)is_coo_j);CHKERRQ(ierr);
562   ierr = ISDestroy(&is_coo_i);CHKERRQ(ierr);
563   ierr = ISDestroy(&is_coo_j);CHKERRQ(ierr);
564   PetscFunctionReturn(0);
565 }
566 
567 /*@
568    MatSetPreallocationCOO - set preallocation for matrices using a coordinate format of the entries
569 
570    Collective on Mat
571 
572    Input Parameters:
573 +  A - matrix being preallocated
574 .  ncoo - number of entries in the locally owned part of the parallel matrix
575 .  coo_i - row indices
576 -  coo_j - column indices
577 
578    Level: beginner
579 
580    Notes: Entries can be repeated, see MatSetValuesCOO(). Currently optimized for cuSPARSE matrices only.
581 
582 .seealso: MatSetValuesCOO(), MatSeqAIJSetPreallocation(), MatMPIAIJSetPreallocation(), MatSeqBAIJSetPreallocation(), MatMPIBAIJSetPreallocation(), MatSeqSBAIJSetPreallocation(), MatMPISBAIJSetPreallocation()
583 @*/
584 PetscErrorCode MatSetPreallocationCOO(Mat A,PetscInt ncoo,const PetscInt coo_i[],const PetscInt coo_j[])
585 {
586   PetscErrorCode (*f)(Mat,PetscInt,const PetscInt[],const PetscInt[]) = NULL;
587   PetscErrorCode ierr;
588 
589   PetscFunctionBegin;
590   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
591   PetscValidType(A,1);
592   if (ncoo) PetscValidIntPointer(coo_i,3);
593   if (ncoo) PetscValidIntPointer(coo_j,4);
594   ierr = PetscLayoutSetUp(A->rmap);CHKERRQ(ierr);
595   ierr = PetscLayoutSetUp(A->cmap);CHKERRQ(ierr);
596   if (PetscDefined(USE_DEBUG)) {
597     PetscInt i;
598     for (i = 0; i < ncoo; i++) {
599       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);
600       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);
601     }
602   }
603   ierr = PetscObjectQueryFunction((PetscObject)A,"MatSetPreallocationCOO_C",&f);CHKERRQ(ierr);
604   ierr = PetscLogEventBegin(MAT_PreallCOO,A,0,0,0);CHKERRQ(ierr);
605   if (f) {
606     ierr = (*f)(A,ncoo,coo_i,coo_j);CHKERRQ(ierr);
607   } else { /* allow fallback, very slow */
608     ierr = MatSetPreallocationCOO_Basic(A,ncoo,coo_i,coo_j);CHKERRQ(ierr);
609   }
610   ierr = PetscLogEventEnd(MAT_PreallCOO,A,0,0,0);CHKERRQ(ierr);
611   PetscFunctionReturn(0);
612 }
613 
614 /*@
615    MatSetValuesCOO - set values at once in a matrix preallocated using MatSetPreallocationCOO()
616 
617    Collective on Mat
618 
619    Input Parameters:
620 +  A - matrix being preallocated
621 .  coo_v - the matrix values (can be NULL)
622 -  imode - the insert mode
623 
624    Level: beginner
625 
626    Notes: The values must follow the order of the indices prescribed with MatSetPreallocationCOO().
627           When repeated entries are specified in the COO indices the coo_v values are first properly summed.
628           The imode flag indicates if coo_v must be added to the current values of the matrix (ADD_VALUES) or overwritten (INSERT_VALUES).
629           Currently optimized for cuSPARSE matrices only.
630           Passing coo_v == NULL is equivalent to passing an array of zeros.
631 
632 .seealso: MatSetPreallocationCOO(), InsertMode, INSERT_VALUES, ADD_VALUES
633 @*/
634 PetscErrorCode MatSetValuesCOO(Mat A, const PetscScalar coo_v[], InsertMode imode)
635 {
636   PetscErrorCode (*f)(Mat,const PetscScalar[],InsertMode) = NULL;
637   PetscErrorCode ierr;
638 
639   PetscFunctionBegin;
640   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
641   PetscValidType(A,1);
642   MatCheckPreallocated(A,1);
643   PetscValidLogicalCollectiveEnum(A,imode,3);
644   ierr = PetscObjectQueryFunction((PetscObject)A,"MatSetValuesCOO_C",&f);CHKERRQ(ierr);
645   ierr = PetscLogEventBegin(MAT_SetVCOO,A,0,0,0);CHKERRQ(ierr);
646   if (f) {
647     ierr = (*f)(A,coo_v,imode);CHKERRQ(ierr);
648   } else { /* allow fallback */
649     ierr = MatSetValuesCOO_Basic(A,coo_v,imode);CHKERRQ(ierr);
650   }
651   ierr = PetscLogEventEnd(MAT_SetVCOO,A,0,0,0);CHKERRQ(ierr);
652   ierr = PetscObjectStateIncrease((PetscObject)A);CHKERRQ(ierr);
653   PetscFunctionReturn(0);
654 }
655 
656 /*@
657    MatSetBindingPropagates - Sets whether the state of being bound to the CPU for a GPU matrix type propagates to child and some other associated objects
658 
659    Input Parameters:
660 +  A - the matrix
661 -  flg - flag indicating whether the boundtocpu flag should be propagated
662 
663    Level: developer
664 
665    Notes:
666    If the value of flg is set to true, the following will occur:
667 
668    MatCreateSubMatrices() and MatCreateRedundantMatrix() will bind created matrices to CPU if the input matrix is bound to the CPU.
669    MatCreateVecs() will bind created vectors to CPU if the input matrix is bound to the CPU.
670    The bindingpropagates flag itself is also propagated by the above routines.
671 
672    Developer Notes:
673    If the fine-scale DMDA has the -dm_bind_below option set to true, then DMCreateInterpolationScale() calls MatSetBindingPropagates()
674    on the restriction/interpolation operator to set the bindingpropagates flag to true.
675 
676 .seealso: VecSetBindingPropagates(), MatGetBindingPropagates()
677 @*/
678 PetscErrorCode MatSetBindingPropagates(Mat A,PetscBool flg)
679 {
680   PetscFunctionBegin;
681   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
682 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
683   A->bindingpropagates = flg;
684 #endif
685   PetscFunctionReturn(0);
686 }
687 
688 /*@
689    MatGetBindingPropagates - Gets whether the state of being bound to the CPU for a GPU matrix type propagates to child and some other associated objects
690 
691    Input Parameter:
692 .  A - the matrix
693 
694    Output Parameter:
695 .  flg - flag indicating whether the boundtocpu flag will be propagated
696 
697    Level: developer
698 
699 .seealso: MatSetBindingPropagates()
700 @*/
701 PetscErrorCode MatGetBindingPropagates(Mat A,PetscBool *flg)
702 {
703   PetscFunctionBegin;
704   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
705   PetscValidBoolPointer(flg,2);
706 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
707   *flg = A->bindingpropagates;
708 #else
709   *flg = PETSC_FALSE;
710 #endif
711   PetscFunctionReturn(0);
712 }
713